Skip to content

Commit e349470

Browse files
Christoph HellwigChristoph Hellwig
authored andcommitted
hfsplus: optimize fsync
Avoid doing unessecary work in fsync. Do nothing unless the inode was marked dirty, and only write the various metadata inodes out if they contain any dirty state from this inode. This is archived by adding three new dirty bits to the hfsplus-specific inode which are set in the correct places. Signed-off-by: Christoph Hellwig <hch@tuxera.com>
1 parent b33b792 commit e349470

6 files changed

Lines changed: 78 additions & 32 deletions

File tree

‎fs/hfsplus/catalog.c‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,8 @@ int hfsplus_create_cat(u32 cnid, struct inode *dir, struct qstr *str, struct ino
227227

228228
dir->i_size++;
229229
dir->i_mtime = dir->i_ctime = CURRENT_TIME_SEC;
230-
mark_inode_dirty(dir);
230+
hfsplus_mark_inode_dirty(dir, HFSPLUS_I_CAT_DIRTY);
231+
231232
hfs_find_exit(&fd);
232233
return 0;
233234

@@ -308,7 +309,7 @@ int hfsplus_delete_cat(u32 cnid, struct inode *dir, struct qstr *str)
308309

309310
dir->i_size--;
310311
dir->i_mtime = dir->i_ctime = CURRENT_TIME_SEC;
311-
mark_inode_dirty(dir);
312+
hfsplus_mark_inode_dirty(dir, HFSPLUS_I_CAT_DIRTY);
312313
out:
313314
hfs_find_exit(&fd);
314315

@@ -353,7 +354,6 @@ int hfsplus_rename_cat(u32 cnid,
353354
goto out;
354355
dst_dir->i_size++;
355356
dst_dir->i_mtime = dst_dir->i_ctime = CURRENT_TIME_SEC;
356-
mark_inode_dirty(dst_dir);
357357

358358
/* finally remove the old entry */
359359
hfsplus_cat_build_key(sb, src_fd.search_key, src_dir->i_ino, src_name);
@@ -365,7 +365,6 @@ int hfsplus_rename_cat(u32 cnid,
365365
goto out;
366366
src_dir->i_size--;
367367
src_dir->i_mtime = src_dir->i_ctime = CURRENT_TIME_SEC;
368-
mark_inode_dirty(src_dir);
369368

370369
/* remove old thread entry */
371370
hfsplus_cat_build_key(sb, src_fd.search_key, cnid, NULL);
@@ -387,6 +386,9 @@ int hfsplus_rename_cat(u32 cnid,
387386
goto out;
388387
}
389388
err = hfs_brec_insert(&dst_fd, &entry, entry_size);
389+
390+
hfsplus_mark_inode_dirty(dst_dir, HFSPLUS_I_CAT_DIRTY);
391+
hfsplus_mark_inode_dirty(src_dir, HFSPLUS_I_CAT_DIRTY);
390392
out:
391393
hfs_bnode_put(dst_fd.bnode);
392394
hfs_find_exit(&src_fd);

‎fs/hfsplus/extents.c‎

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,14 @@ static void __hfsplus_ext_write_extent(struct inode *inode, struct hfs_find_data
108108
fd->entryoffset, fd->entrylength);
109109
hip->extent_state &= ~HFSPLUS_EXT_DIRTY;
110110
}
111+
112+
/*
113+
* We can't just use hfsplus_mark_inode_dirty here, because we
114+
* also get called from hfsplus_write_inode, which should not
115+
* redirty the inode. Instead the callers have to be careful
116+
* to explicily mark the inode dirty, too.
117+
*/
118+
set_bit(HFSPLUS_I_EXT_DIRTY, &hip->flags);
111119
}
112120

113121
static void hfsplus_ext_write_extent_locked(struct inode *inode)
@@ -197,6 +205,7 @@ int hfsplus_get_block(struct inode *inode, sector_t iblock,
197205
struct hfsplus_inode_info *hip = HFSPLUS_I(inode);
198206
int res = -EIO;
199207
u32 ablock, dblock, mask;
208+
int was_dirty = 0;
200209
int shift;
201210

202211
/* Convert inode block to disk allocation block */
@@ -223,14 +232,20 @@ int hfsplus_get_block(struct inode *inode, sector_t iblock,
223232
return -EIO;
224233

225234
mutex_lock(&hip->extents_lock);
235+
236+
/*
237+
* hfsplus_ext_read_extent will write out a cached extent into
238+
* the extents btree. In that case we may have to mark the inode
239+
* dirty even for a pure read of an extent here.
240+
*/
241+
was_dirty = (hip->extent_state & HFSPLUS_EXT_DIRTY);
226242
res = hfsplus_ext_read_extent(inode, ablock);
227-
if (!res) {
228-
dblock = hfsplus_ext_find_block(hip->cached_extents,
229-
ablock - hip->cached_start);
230-
} else {
243+
if (res) {
231244
mutex_unlock(&hip->extents_lock);
232245
return -EIO;
233246
}
247+
dblock = hfsplus_ext_find_block(hip->cached_extents,
248+
ablock - hip->cached_start);
234249
mutex_unlock(&hip->extents_lock);
235250

236251
done:
@@ -242,8 +257,9 @@ int hfsplus_get_block(struct inode *inode, sector_t iblock,
242257
hip->phys_size += sb->s_blocksize;
243258
hip->fs_blocks++;
244259
inode_add_bytes(inode, sb->s_blocksize);
245-
mark_inode_dirty(inode);
246260
}
261+
if (create || was_dirty)
262+
mark_inode_dirty(inode);
247263
return 0;
248264
}
249265

@@ -438,7 +454,7 @@ int hfsplus_file_extend(struct inode *inode)
438454
mutex_unlock(&hip->extents_lock);
439455
if (!res) {
440456
hip->alloc_blocks += len;
441-
mark_inode_dirty(inode);
457+
hfsplus_mark_inode_dirty(inode, HFSPLUS_I_ALLOC_DIRTY);
442458
}
443459
return res;
444460

@@ -529,5 +545,5 @@ void hfsplus_file_truncate(struct inode *inode)
529545
hip->phys_size = inode->i_size;
530546
hip->fs_blocks = (inode->i_size + sb->s_blocksize - 1) >> sb->s_blocksize_bits;
531547
inode_set_bytes(inode, hip->fs_blocks << sb->s_blocksize_bits);
532-
mark_inode_dirty(inode);
548+
hfsplus_mark_inode_dirty(inode, HFSPLUS_I_ALLOC_DIRTY);
533549
}

‎fs/hfsplus/hfsplus_fs.h‎

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,11 @@ struct hfsplus_sb_info {
157157
#define HFSPLUS_SB_HFSX 3
158158
#define HFSPLUS_SB_CASEFOLD 4
159159

160+
static inline struct hfsplus_sb_info *HFSPLUS_SB(struct super_block *sb)
161+
{
162+
return sb->s_fs_info;
163+
}
164+
160165

161166
struct hfsplus_inode_info {
162167
atomic_t opencnt;
@@ -205,10 +210,31 @@ struct hfsplus_inode_info {
205210
#define HFSPLUS_EXT_NEW 0x0002
206211

207212
#define HFSPLUS_I_RSRC 0 /* represents a resource fork */
213+
#define HFSPLUS_I_CAT_DIRTY 1 /* has changes in the catalog tree */
214+
#define HFSPLUS_I_EXT_DIRTY 2 /* has changes in the extent tree */
215+
#define HFSPLUS_I_ALLOC_DIRTY 3 /* has changes in the allocation file */
208216

209217
#define HFSPLUS_IS_RSRC(inode) \
210218
test_bit(HFSPLUS_I_RSRC, &HFSPLUS_I(inode)->flags)
211219

220+
static inline struct hfsplus_inode_info *HFSPLUS_I(struct inode *inode)
221+
{
222+
return list_entry(inode, struct hfsplus_inode_info, vfs_inode);
223+
}
224+
225+
/*
226+
* Mark an inode dirty, and also mark the btree in which the
227+
* specific type of metadata is stored.
228+
* For data or metadata that gets written back by into the catalog btree
229+
* by hfsplus_write_inode a plain mark_inode_dirty call is enough.
230+
*/
231+
static inline void hfsplus_mark_inode_dirty(struct inode *inode,
232+
unsigned int flag)
233+
{
234+
set_bit(flag, &HFSPLUS_I(inode)->flags);
235+
mark_inode_dirty(inode);
236+
}
237+
212238
struct hfs_find_data {
213239
/* filled by caller */
214240
hfsplus_btree_key *search_key;
@@ -397,17 +423,6 @@ int hfs_part_find(struct super_block *, sector_t *, sector_t *);
397423
int hfsplus_submit_bio(struct block_device *bdev, sector_t sector,
398424
void *data, int rw);
399425

400-
/* access macros */
401-
static inline struct hfsplus_sb_info *HFSPLUS_SB(struct super_block *sb)
402-
{
403-
return sb->s_fs_info;
404-
}
405-
406-
static inline struct hfsplus_inode_info *HFSPLUS_I(struct inode *inode)
407-
{
408-
return list_entry(inode, struct hfsplus_inode_info, vfs_inode);
409-
}
410-
411426
/* time macros */
412427
#define __hfsp_mt2ut(t) (be32_to_cpu(t) - 2082844800U)
413428
#define __hfsp_ut2mt(t) (cpu_to_be32(t + 2082844800U))

‎fs/hfsplus/inode.c‎

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -307,8 +307,9 @@ static int hfsplus_setattr(struct dentry *dentry, struct iattr *attr)
307307
int hfsplus_file_fsync(struct file *file, int datasync)
308308
{
309309
struct inode *inode = file->f_mapping->host;
310+
struct hfsplus_inode_info *hip = HFSPLUS_I(inode);
310311
struct hfsplus_sb_info *sbi = HFSPLUS_SB(inode->i_sb);
311-
int error, error2;
312+
int error = 0, error2;
312313

313314
/*
314315
* Sync inode metadata into the catalog and extent trees.
@@ -318,13 +319,21 @@ int hfsplus_file_fsync(struct file *file, int datasync)
318319
/*
319320
* And explicitly write out the btrees.
320321
*/
321-
error = filemap_write_and_wait(sbi->cat_tree->inode->i_mapping);
322-
error2 = filemap_write_and_wait(sbi->ext_tree->inode->i_mapping);
323-
if (!error)
324-
error = error2;
325-
error2 = filemap_write_and_wait(sbi->alloc_file->i_mapping);
326-
if (!error)
327-
error = error2;
322+
if (test_and_clear_bit(HFSPLUS_I_CAT_DIRTY, &hip->flags))
323+
error = filemap_write_and_wait(sbi->cat_tree->inode->i_mapping);
324+
325+
if (test_and_clear_bit(HFSPLUS_I_EXT_DIRTY, &hip->flags)) {
326+
error2 = filemap_write_and_wait(sbi->ext_tree->inode->i_mapping);
327+
if (!error)
328+
error = error2;
329+
}
330+
331+
if (test_and_clear_bit(HFSPLUS_I_ALLOC_DIRTY, &hip->flags)) {
332+
error2 = filemap_write_and_wait(sbi->alloc_file->i_mapping);
333+
if (!error)
334+
error = error2;
335+
}
336+
328337
return error;
329338
}
330339

@@ -590,6 +599,8 @@ int hfsplus_cat_write_inode(struct inode *inode)
590599
hfs_bnode_write(fd.bnode, &entry, fd.entryoffset,
591600
sizeof(struct hfsplus_cat_file));
592601
}
602+
603+
set_bit(HFSPLUS_I_CAT_DIRTY, &HFSPLUS_I(inode)->flags);
593604
out:
594605
hfs_find_exit(&fd);
595606
return 0;

‎fs/hfsplus/ioctl.c‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,9 +147,11 @@ int hfsplus_setxattr(struct dentry *dentry, const char *name,
147147
res = -ERANGE;
148148
} else
149149
res = -EOPNOTSUPP;
150-
if (!res)
150+
if (!res) {
151151
hfs_bnode_write(fd.bnode, &entry, fd.entryoffset,
152152
sizeof(struct hfsplus_cat_file));
153+
hfsplus_mark_inode_dirty(inode, HFSPLUS_I_CAT_DIRTY);
154+
}
153155
out:
154156
hfs_find_exit(&fd);
155157
return res;

‎fs/hfsplus/super.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -472,7 +472,7 @@ static int hfsplus_fill_super(struct super_block *sb, void *data, int silent)
472472
&str, sbi->hidden_dir);
473473
mutex_unlock(&sbi->vh_mutex);
474474

475-
mark_inode_dirty(sbi->hidden_dir);
475+
hfsplus_mark_inode_dirty(sbi->hidden_dir, HFSPLUS_I_CAT_DIRTY);
476476
}
477477
out:
478478
unload_nls(sbi->nls);

0 commit comments

Comments
 (0)