Skip to content

Commit 02c24a8

Browse files
Josef BacikAl Viro
authored andcommitted
fs: push i_mutex and filemap_write_and_wait down into ->fsync() handlers
Btrfs needs to be able to control how filemap_write_and_wait_range() is called in fsync to make it less of a painful operation, so push down taking i_mutex and the calling of filemap_write_and_wait() down into the ->fsync() handlers. Some file systems can drop taking the i_mutex altogether it seems, like ext3 and ocfs2. For correctness sake I just pushed everything down in all cases to make sure that we keep the current behavior the same for everybody, and then each individual fs maintainer can make up their mind about what to do from there. Thanks, Acked-by: Jan Kara <jack@suse.cz> Signed-off-by: Josef Bacik <josef@redhat.com> Signed-off-by: Al Viro <viro@zeniv.linux.org.uk>
1 parent 2273506 commit 02c24a8

71 files changed

Lines changed: 462 additions & 164 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎Documentation/filesystems/Locking‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,7 @@ prototypes:
412412
int (*open) (struct inode *, struct file *);
413413
int (*flush) (struct file *);
414414
int (*release) (struct inode *, struct file *);
415-
int (*fsync) (struct file *, int datasync);
415+
int (*fsync) (struct file *, loff_t start, loff_t end, int datasync);
416416
int (*aio_fsync) (struct kiocb *, int datasync);
417417
int (*fasync) (int, struct file *, int);
418418
int (*lock) (struct file *, int, struct file_lock *);
@@ -438,9 +438,7 @@ prototypes:
438438

439439
locking rules:
440440
All may block except for ->setlease.
441-
No VFS locks held on entry except for ->fsync and ->setlease.
442-
443-
->fsync() has i_mutex on inode.
441+
No VFS locks held on entry except for ->setlease.
444442

445443
->setlease has the file_list_lock held and must not sleep.
446444

‎Documentation/filesystems/porting‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,3 +421,10 @@ data and there is a virtual hole at the end of the file. So if the provided
421421
offset is less than i_size and SEEK_DATA is specified, return the same offset.
422422
If the above is true for the offset and you are given SEEK_HOLE, return the end
423423
of the file. If the offset is i_size or greater return -ENXIO in either case.
424+
425+
[mandatory]
426+
If you have your own ->fsync() you must make sure to call
427+
filemap_write_and_wait_range() so that all dirty pages are synced out properly.
428+
You must also keep in mind that ->fsync() is not called with i_mutex held
429+
anymore, so if you require i_mutex locking you must make sure to take it and
430+
release it yourself.

‎Documentation/filesystems/vfs.txt‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -777,7 +777,7 @@ struct file_operations {
777777
int (*open) (struct inode *, struct file *);
778778
int (*flush) (struct file *);
779779
int (*release) (struct inode *, struct file *);
780-
int (*fsync) (struct file *, int datasync);
780+
int (*fsync) (struct file *, loff_t, loff_t, int datasync);
781781
int (*aio_fsync) (struct kiocb *, int datasync);
782782
int (*fasync) (int, struct file *, int);
783783
int (*lock) (struct file *, int, struct file_lock *);

‎arch/powerpc/platforms/cell/spufs/file.c‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1850,9 +1850,16 @@ static int spufs_mfc_flush(struct file *file, fl_owner_t id)
18501850
return ret;
18511851
}
18521852

1853-
static int spufs_mfc_fsync(struct file *file, int datasync)
1853+
static int spufs_mfc_fsync(struct file *file, loff_t start, loff_t end, int datasync)
18541854
{
1855-
return spufs_mfc_flush(file, NULL);
1855+
struct inode *inode = file->f_path.dentry->d_inode;
1856+
int err = filemap_write_and_wait_range(inode->i_mapping, start, end);
1857+
if (!err) {
1858+
mutex_lock(&inode->i_mutex);
1859+
err = spufs_mfc_flush(file, NULL);
1860+
mutex_unlock(&inode->i_mutex);
1861+
}
1862+
return err;
18561863
}
18571864

18581865
static int spufs_mfc_fasync(int fd, struct file *file, int on)

‎drivers/char/ps3flash.c‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -309,9 +309,14 @@ static int ps3flash_flush(struct file *file, fl_owner_t id)
309309
return ps3flash_writeback(ps3flash_dev);
310310
}
311311

312-
static int ps3flash_fsync(struct file *file, int datasync)
312+
static int ps3flash_fsync(struct file *file, loff_t start, loff_t end, int datasync)
313313
{
314-
return ps3flash_writeback(ps3flash_dev);
314+
struct inode *inode = file->f_path.dentry->d_inode;
315+
int err;
316+
mutex_lock(&inode->i_mutex);
317+
err = ps3flash_writeback(ps3flash_dev);
318+
mutex_unlock(&inode->i_mutex);
319+
return err;
315320
}
316321

317322
static irqreturn_t ps3flash_interrupt(int irq, void *data)

‎drivers/mtd/ubi/cdev.c‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -189,12 +189,16 @@ static loff_t vol_cdev_llseek(struct file *file, loff_t offset, int origin)
189189
return new_offset;
190190
}
191191

192-
static int vol_cdev_fsync(struct file *file, int datasync)
192+
static int vol_cdev_fsync(struct file *file, loff_t start, loff_t end, int datasync)
193193
{
194194
struct ubi_volume_desc *desc = file->private_data;
195195
struct ubi_device *ubi = desc->vol->ubi;
196-
197-
return ubi_sync(ubi->ubi_num);
196+
struct inode *inode = file->f_path.dentry->d_inode;
197+
int err;
198+
mutex_lock(&inode->i_mutex);
199+
err = ubi_sync(ubi->ubi_num);
200+
mutex_unlock(&inode->i_mutex);
201+
return err;
198202
}
199203

200204

‎drivers/staging/pohmelfs/inode.c‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -887,11 +887,16 @@ static struct inode *pohmelfs_alloc_inode(struct super_block *sb)
887887
/*
888888
* We want fsync() to work on POHMELFS.
889889
*/
890-
static int pohmelfs_fsync(struct file *file, int datasync)
890+
static int pohmelfs_fsync(struct file *file, loff_t start, loff_t end, int datasync)
891891
{
892892
struct inode *inode = file->f_mapping->host;
893-
894-
return sync_inode_metadata(inode, 1);
893+
int err = filemap_write_and_wait_range(inode->i_mapping, start, end);
894+
if (!err) {
895+
mutex_lock(&inode->i_mutex);
896+
err = sync_inode_metadata(inode, 1);
897+
mutex_unlock(&inode->i_mutex);
898+
}
899+
return err;
895900
}
896901

897902
ssize_t pohmelfs_write(struct file *file, const char __user *buf,

‎drivers/usb/gadget/printer.c‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -795,12 +795,14 @@ printer_write(struct file *fd, const char __user *buf, size_t len, loff_t *ptr)
795795
}
796796

797797
static int
798-
printer_fsync(struct file *fd, int datasync)
798+
printer_fsync(struct file *fd, loff_t start, loff_t end, int datasync)
799799
{
800800
struct printer_dev *dev = fd->private_data;
801+
struct inode *inode = fd->f_path.dentry->d_inode;
801802
unsigned long flags;
802803
int tx_list_empty;
803804

805+
mutex_lock(&inode->i_mutex);
804806
spin_lock_irqsave(&dev->lock, flags);
805807
tx_list_empty = (likely(list_empty(&dev->tx_reqs)));
806808
spin_unlock_irqrestore(&dev->lock, flags);
@@ -810,6 +812,7 @@ printer_fsync(struct file *fd, int datasync)
810812
wait_event_interruptible(dev->tx_flush_wait,
811813
(likely(list_empty(&dev->tx_reqs_active))));
812814
}
815+
mutex_unlock(&inode->i_mutex);
813816

814817
return 0;
815818
}

‎drivers/video/fb_defio.c‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,19 +66,26 @@ static int fb_deferred_io_fault(struct vm_area_struct *vma,
6666
return 0;
6767
}
6868

69-
int fb_deferred_io_fsync(struct file *file, int datasync)
69+
int fb_deferred_io_fsync(struct file *file, loff_t start, loff_t end, int datasync)
7070
{
7171
struct fb_info *info = file->private_data;
72+
struct inode *inode = file->f_path.dentry->d_inode;
73+
int err = filemap_write_and_wait_range(inode->i_mapping, start, end);
74+
if (err)
75+
return err;
7276

7377
/* Skip if deferred io is compiled-in but disabled on this fbdev */
7478
if (!info->fbdefio)
7579
return 0;
7680

81+
mutex_lock(&inode->i_mutex);
7782
/* Kill off the delayed work */
7883
cancel_delayed_work_sync(&info->deferred_work);
7984

8085
/* Run it immediately */
81-
return schedule_delayed_work(&info->deferred_work, 0);
86+
err = schedule_delayed_work(&info->deferred_work, 0);
87+
mutex_unlock(&inode->i_mutex);
88+
return err;
8289
}
8390
EXPORT_SYMBOL_GPL(fb_deferred_io_fsync);
8491

‎fs/9p/v9fs_vfs.h‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,8 @@ ssize_t v9fs_file_readn(struct file *, char *, char __user *, u32, u64);
7070
ssize_t v9fs_fid_readn(struct p9_fid *, char *, char __user *, u32, u64);
7171
void v9fs_blank_wstat(struct p9_wstat *wstat);
7272
int v9fs_vfs_setattr_dotl(struct dentry *, struct iattr *);
73-
int v9fs_file_fsync_dotl(struct file *filp, int datasync);
73+
int v9fs_file_fsync_dotl(struct file *filp, loff_t start, loff_t end,
74+
int datasync);
7475
ssize_t v9fs_file_write_internal(struct inode *, struct p9_fid *,
7576
const char __user *, size_t, loff_t *, int);
7677
int v9fs_refresh_inode(struct p9_fid *fid, struct inode *inode);

0 commit comments

Comments
 (0)