From: Jan Kara <jack@xxxxxxx> Subject: fs: return EAGAIN when O_NONBLOCK write should block on frozen fs When user asks for O_NONBLOCK behavior for a file descriptor, return EAGAIN instead of blocking on a frozen filesystem. This is needed so we can fix a hang with BSD accounting on frozen filesystems. Signed-off-by: Jan Kara <jack@xxxxxxx> Reviewed-by: Dave Chinner <dchinner@xxxxxxxxxx> Cc: Al Viro <viro@xxxxxxxxxxxxxxxxxx> Acked-by: Joel Becker <jlbec@xxxxxxxxxxxx> Cc: Mark Fasheh <mfasheh@xxxxxxxx> Cc: Nikola Ciprich <nikola.ciprich@xxxxxxxxxxx> Cc: Marco Stornelli <marco.stornelli@xxxxxxxxx> Signed-off-by: Andrew Morton <akpm@xxxxxxxxxxxxxxxxxxxx> --- fs/btrfs/file.c | 3 ++- fs/cifs/file.c | 3 ++- fs/fuse/file.c | 3 ++- fs/ntfs/file.c | 3 ++- fs/ocfs2/file.c | 3 ++- fs/splice.c | 3 ++- fs/xfs/xfs_file.c | 3 ++- include/linux/fs.h | 10 ++++++++++ mm/filemap.c | 3 ++- 9 files changed, 26 insertions(+), 8 deletions(-) diff -puN fs/btrfs/file.c~fs-return-eagain-when-o_nonblock-write-should-block-on-frozen-fs fs/btrfs/file.c --- a/fs/btrfs/file.c~fs-return-eagain-when-o_nonblock-write-should-block-on-frozen-fs +++ a/fs/btrfs/file.c @@ -1509,7 +1509,8 @@ static ssize_t btrfs_file_aio_write(stru size_t count, ocount; bool sync = (file->f_flags & O_DSYNC) || IS_SYNC(file->f_mapping->host); - sb_start_write(inode->i_sb); + if (!sb_start_file_write(file)) + return -EAGAIN; mutex_lock(&inode->i_mutex); diff -puN fs/cifs/file.c~fs-return-eagain-when-o_nonblock-write-should-block-on-frozen-fs fs/cifs/file.c --- a/fs/cifs/file.c~fs-return-eagain-when-o_nonblock-write-should-block-on-frozen-fs +++ a/fs/cifs/file.c @@ -2510,7 +2510,8 @@ cifs_writev(struct kiocb *iocb, const st BUG_ON(iocb->ki_pos != pos); - sb_start_write(inode->i_sb); + if (!sb_start_file_write(file)) + return -EAGAIN; /* * We need to hold the sem to be sure nobody modifies lock list diff -puN fs/fuse/file.c~fs-return-eagain-when-o_nonblock-write-should-block-on-frozen-fs fs/fuse/file.c --- a/fs/fuse/file.c~fs-return-eagain-when-o_nonblock-write-should-block-on-frozen-fs +++ a/fs/fuse/file.c @@ -971,7 +971,8 @@ static ssize_t fuse_file_aio_write(struc return err; count = ocount; - sb_start_write(inode->i_sb); + if (!sb_start_file_write(file)) + return -EAGAIN; mutex_lock(&inode->i_mutex); /* We can write back this queue in page reclaim */ diff -puN fs/ntfs/file.c~fs-return-eagain-when-o_nonblock-write-should-block-on-frozen-fs fs/ntfs/file.c --- a/fs/ntfs/file.c~fs-return-eagain-when-o_nonblock-write-should-block-on-frozen-fs +++ a/fs/ntfs/file.c @@ -2129,7 +2129,8 @@ static ssize_t ntfs_file_aio_write(struc BUG_ON(iocb->ki_pos != pos); - sb_start_write(inode->i_sb); + if (!sb_start_file_write(file)) + return -EAGAIN; mutex_lock(&inode->i_mutex); ret = ntfs_file_aio_write_nolock(iocb, iov, nr_segs, &iocb->ki_pos); mutex_unlock(&inode->i_mutex); diff -puN fs/ocfs2/file.c~fs-return-eagain-when-o_nonblock-write-should-block-on-frozen-fs fs/ocfs2/file.c --- a/fs/ocfs2/file.c~fs-return-eagain-when-o_nonblock-write-should-block-on-frozen-fs +++ a/fs/ocfs2/file.c @@ -2247,7 +2247,8 @@ static ssize_t ocfs2_file_aio_write(stru if (iocb->ki_left == 0) return 0; - sb_start_write(inode->i_sb); + if (!sb_start_file_write(file)) + return -EAGAIN; appending = file->f_flags & O_APPEND ? 1 : 0; direct_io = file->f_flags & O_DIRECT ? 1 : 0; diff -puN fs/splice.c~fs-return-eagain-when-o_nonblock-write-should-block-on-frozen-fs fs/splice.c --- a/fs/splice.c~fs-return-eagain-when-o_nonblock-write-should-block-on-frozen-fs +++ a/fs/splice.c @@ -998,7 +998,8 @@ generic_file_splice_write(struct pipe_in }; ssize_t ret; - sb_start_write(inode->i_sb); + if (!sb_start_file_write(out)) + return -EAGAIN; pipe_lock(pipe); diff -puN fs/xfs/xfs_file.c~fs-return-eagain-when-o_nonblock-write-should-block-on-frozen-fs fs/xfs/xfs_file.c --- a/fs/xfs/xfs_file.c~fs-return-eagain-when-o_nonblock-write-should-block-on-frozen-fs +++ a/fs/xfs/xfs_file.c @@ -775,7 +775,8 @@ xfs_file_aio_write( if (ocount == 0) return 0; - sb_start_write(inode->i_sb); + if (!sb_start_file_write(file)) + return -EAGAIN; if (XFS_FORCED_SHUTDOWN(ip->i_mount)) { ret = -EIO; diff -puN include/linux/fs.h~fs-return-eagain-when-o_nonblock-write-should-block-on-frozen-fs include/linux/fs.h --- a/include/linux/fs.h~fs-return-eagain-when-o_nonblock-write-should-block-on-frozen-fs +++ a/include/linux/fs.h @@ -1400,6 +1400,16 @@ static inline int sb_start_write_trylock return __sb_start_write(sb, SB_FREEZE_WRITE, false); } +/* + * sb_start_write() for writing into a file. When file has O_NONBLOCK set, + * we use trylock semantics, otherwise we block on frozen filesystem. + */ +static inline int sb_start_file_write(struct file *file) +{ + return __sb_start_write(file->f_mapping->host->i_sb, SB_FREEZE_WRITE, + !(file->f_flags & O_NONBLOCK)); +} + /** * sb_start_pagefault - get write access to a superblock from a page fault * @sb: the super we write to diff -puN mm/filemap.c~fs-return-eagain-when-o_nonblock-write-should-block-on-frozen-fs mm/filemap.c --- a/mm/filemap.c~fs-return-eagain-when-o_nonblock-write-should-block-on-frozen-fs +++ a/mm/filemap.c @@ -2527,7 +2527,8 @@ ssize_t generic_file_aio_write(struct ki BUG_ON(iocb->ki_pos != pos); - sb_start_write(inode->i_sb); + if (!sb_start_file_write(file)) + return -EAGAIN; mutex_lock(&inode->i_mutex); ret = __generic_file_aio_write(iocb, iov, nr_segs, &iocb->ki_pos); mutex_unlock(&inode->i_mutex); _ -- To unsubscribe from this list: send the line "unsubscribe linux-fsdevel" in the body of a message to majordomo@xxxxxxxxxxxxxxx More majordomo info at http://vger.kernel.org/majordomo-info.html