fs: Fix hang with BSD accounting on frozen filesystem
Subsystems (8)
Jan Kara
maintainerPATCH 1/1 fs: Fix hang with BSD accounting on frozen filesystem
When BSD process accounting is enabled and logs information to a filesystem which gets frozen, system easily becomes unusable because each attempt to account process information blocks. Thus e.g. every task gets blocked in exit.
It seems better to drop accounting information (which can already happen when filesystem is running out of space) instead of locking system up. This is implemented using a special flag FMODE_NO_FREEZE_WAIT in file->f_mode of a file to which accounting information is written.
---
This feels a bit hacky but I didn't find a better solution... If someone
has idea, please speak up.
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/open.c | 2 +- fs/xfs/xfs_file.c | 3 ++- include/linux/fs.h | 14 ++++++++++++++ kernel/acct.c | 1 + mm/filemap.c | 3 ++- 10 files changed, 30 insertions(+), 8 deletions(-)
fs/btrfs/file.c +2 -1
@@ -1411,7 +1411,8 @@ static ssize_t btrfs_file_aio_write(struct kiocb *iocb,ssize_t err =0 ;size_t count, ocount; -sb_start_write (inode->i_sb); +if (!sb_start_file_write (file)) +return -EAGAIN ;mutex_lock (&inode->i_mutex);
fs/cifs/file.c +2 -1
@@ -2448,7 +2448,8 @@ cifs_writev(struct kiocb *iocb, const struct iovec *iov,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
fs/fuse/file.c +2 -1
@@ -947,7 +947,8 @@ static ssize_t fuse_file_aio_write(struct kiocb *iocb, const struct iovec *iov,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 */
fs/ntfs/file.c +2 -1
@@ -2118,7 +2118,8 @@ static ssize_t ntfs_file_aio_write(struct kiocb *iocb, const struct iovec *iov,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);
fs/ocfs2/file.c +2 -1
@@ -2265,7 +2265,8 @@ static ssize_t ocfs2_file_aio_write(struct kiocb *iocb,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 ;
fs/open.c +1 -1
@@ -808,7 +808,7 @@ static inline int build_open_flags(int flags, umode_t mode, struct open_flags *o op->mode =0 ;/* Must never be set by userspace */ - flags &= ~FMODE_NONOTIFY & ~O_CLOEXEC ; + flags &= ~FMODE_NONOTIFY & ~O_CLOEXEC & ~FMODE_NO_FREEZE_WAIT ;/* * O_SYNC is implemented as __O_SYNC|O_DSYNC. As many places only
fs/xfs/xfs_file.c +2 -1
@@ -771,7 +771,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 ;
include/linux/fs.h +14 -0
@@ -123,6 +123,9 @@ typedef void (dio_iodone_t)(struct kiocb *iocb, loff_t offset,/* File was opened by fanotify and shouldn't generate fanotify events */ #define FMODE_NONOTIFY ((__force fmode_t)0x1000000) +/* Write to file should fail on frozen fs rather than block */ +#define FMODE_NO_FREEZE_WAIT ((__force fmode_t)0x2000000) +/* * Flag for rw_copy_check_uvector and compat_rw_copy_check_uvector * that indicates that they should check the contents of the iovec are
@@ -1401,6 +1404,17 @@ static inline int sb_start_write_trylock(struct super_block *sb)return __sb_start_write (sb,SB_FREEZE_WRITE , false); } +/* +* We use trylock semantics if write originates in kernel and normal lock +* semantics otherwise. This is a hack but solves problems with deadlocking +* of e.g. psacct when filesystem is frozen. +*/ +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_mode &FMODE_NO_FREEZE_WAIT )); +} +/** * sb_start_pagefault - get write access to a superblock from a page fault * @sb: the super we write to
kernel/acct.c +1 -0
@@ -183,6 +183,7 @@ static void acct_file_reopen(struct bsd_acct_struct *acct, struct file *file, acct->needcheck = jiffies +ACCT_TIMEOUT *HZ ; acct->active =1 ;list_add (&acct->list, &acct_list); + file->f_mode |=FMODE_NO_FREEZE_WAIT ; }if (old_acct) {mnt_unpin (old_acct->f_path.mnt);
mm/filemap.c +2 -1
@@ -2527,7 +2527,8 @@ ssize_t generic_file_aio_write(struct kiocb *iocb, const struct iovec *iov,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);
Signature · 1 line
1.7.1
Quoted 8 lines · Jan Kara
When BSD process accounting is enabled and logs information to a filesystem which gets frozen, system easily becomes unusable because each attempt to account process information blocks. Thus e.g. every task gets blocked in exit.
It seems better to drop accounting information (which can already happen when filesystem is running out of space) instead of locking system up. This is implemented using a special flag
FMODE_NO_FREEZE_WAITinfile->f_modeof a file to which accounting information is written.
I have no problems with making freeze waiting non-blocking, by why invent a new flag for what is essentially an O_NONBLOCK operation?
Indeed, if someone opens a file O_NONBLOCK, shouldn't if behave exactly the same on a frozen filesystem as this special FMODE_NO_FREEZE_WAIT flag?
FWIW, nfsd could use this as well so that it doesn't block all the nfsd threads trying to write to a frozen filesystem but instead returns EJUKEBOX to the client to tell it ot wait for a while before trying the operation again...
Cheers,
Dave.
Signature · 2 lines
Dave Chinner [email protected]
2012/11/8 Dave Chinner <[email protected]>:
Quoted 5 lines · Jan Kara
Quoted 4 lines
When BSD process accounting is enabled and logs information to a filesystem which gets frozen, system easily becomes unusable because each attempt to account process information blocks. Thus e.g. every task gets blocked in exit.
Quoted 8 lines
Quoted 4 lines
It seems better to drop accounting information (which can already happen when filesystem is running out of space) instead of locking system up. This is implemented using a special flag
FMODE_NO_FREEZE_WAITinfile->f_modeof a file to which accounting information is written.I have no problems with making freeze waiting non-blocking, by why invent a new flag for what is essentially an
O_NONBLOCKoperation?
It's a general problem. I didn't try but I don't think O_NONBLOCK works on a frozen fs, even without BSD accounting. And this behaviour it's not related with the Jan's fsfreeze patches.
Marco
Jan Kara
maintainerQuoted 10 lines · Dave Chinner
Quoted 8 lines · Jan Kara
When BSD process accounting is enabled and logs information to a filesystem which gets frozen, system easily becomes unusable because each attempt to account process information blocks. Thus e.g. every task gets blocked in exit.
It seems better to drop accounting information (which can already happen when filesystem is running out of space) instead of locking system up. This is implemented using a special flag
FMODE_NO_FREEZE_WAITinfile->f_modeof a file to which accounting information is written.
Quoted 6 lines
I have no problems with making freeze waiting non-blocking, by why invent a new flag for what is essentially an
O_NONBLOCKoperation?Indeed, if someone opens a file
O_NONBLOCK, shouldn't if behave exactly the same on a frozen filesystem as this specialFMODE_NO_FREEZE_WAITflag?
Originally I didn't want to cause user visible effects for other files
with the fix. But you are right that O_NONBLOCK means exactly what we need and userspace could actually expect something like this to work. I'm somewhat worried about broken applications which don't expect EAGAIN from O_NONBLOCK write to a file (as traditionally O_NONBLOCK was no-op for buffered file writes) but I guess it's worth a try. Thanks for idea, I'll send an updated patch shortly.
Quoted 4 lines
FWIW, nfsd could use this as well so that it doesn't block all the nfsd threads trying to write to a frozen filesystem but instead returns EJUKEBOX to the client to tell it ot wait for a while before trying the operation again...
Yes, that would be another possible use of O_NONBLOCK.
Honza
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR