Fix filesystem freezing deadlocks
Subsystems (17)
v4 cover Fix filesystem freezing deadlocks
Hello,
here is the fourth iteration of my patches to improve filesystem freezing.
Filesystem freezing is currently racy and thus we can end up with dirty data on frozen filesystem (see changelog patch 06 for detailed race description). This patch series aims at fixing this.
To be able to block all places where inodes get dirtied, I've moved filesystem freeze handling in mnt_want_write() / mnt_drop_write(). This however required some code shuffling and changes to kern_path_create() (see patches 02-05). I think the result is OK but opinions may differ ;). The advantage of this change also is that all filesystems get freeze protection almost for free - even ext2 can handle freezing well now.
Another potential contention point might be patch 19. In that patch we make freeze_super() refuse to freeze the filesystem when there are open but unlinked files which may be impractical in some cases. The main reason for this is the problem with handling of file deletion from fput() called with mmap_sem held (e.g. from munmap(2)), and then there's the fact that we cannot really force such filesystem into a consistent state... But if people think that freezing with open but unlinked files should happen, then I have some possible solutions in mind (maybe as a separate patchset since this is large enough).
I'm not able to hit any deadlocks, lockdep warnings, or dirty data on frozen filesystem despite beating it with fsstress and bash-shared-mapping while freezing and unfreezing for several hours (using ext4 and xfs) so I'm reasonably confident this could finally be the right solution.
And for people wanting to test - this patchset is based on patch series "Push file_update_time() into .page_mkwrite" so you'll need to pull that one in as well.
Changes since v3:
* added third level of freezing for fs internal purposes - hooked some filesystems to use it (XFS, nilfs2)
* removed racy i_size check from filemap_mkwrite()
Changes since v2:
* completely rewritten
* freezing is now blocked at VFS entry points
* two stage freezing to handle both mmapped writes and other IO
The biggest changes since v1:
* have two counters to provide safe state transitions for SB_FREEZE_WRITE and SB_FREEZE_TRANS states
* use percpu counters instead of own percpu structure
* added documentation fixes from the old fs freezing series
* converted XFS to use SB_FREEZE_TRANS counter instead of its private m_active_trans counter
Honza
Jan Kara
maintainerPATCH 1/19 mm: Make default vm_ops provide ->page_mkwrite handler
Make default vm_ops provide ->page_mkwrite handler. Currently it only updates file's modification times and gets locked page but later it will also handle filesystem freezing.
--- include/linux/mm.h | 1 + mm/filemap.c | 19 +++++++++++++++++++ mm/filemap_xip.c | 1 + 3 files changed, 21 insertions(+), 0 deletions(-)
include/linux/mm.h +1 -0
@@ -1419,6 +1419,7 @@ extern void truncate_inode_pages_range(struct address_space *,/* generic vm_area_ops exported for stackable file systems */ extern int filemap_fault (struct vm_area_struct *,struct vm_fault *); +extern int filemap_page_mkwrite (struct vm_area_struct *vma,struct vm_fault *vmf);/* mm/page-writeback.c */ int write_one_page (struct page *page,int wait);
mm/filemap.c +19 -0
@@ -1759,8 +1759,27 @@ page_not_uptodate: }EXPORT_SYMBOL (filemap_fault); +int filemap_page_mkwrite (struct vm_area_struct *vma,struct vm_fault *vmf) +{ +struct page *page = vmf->page; +struct inode *inode = vma->vm_file->f_path.dentry->d_inode; +int ret =VM_FAULT_LOCKED ; + +file_update_time (vma->vm_file); +lock_page (page); +if (page->mapping != inode->i_mapping) { +unlock_page (page); + ret =VM_FAULT_NOPAGE ; + goto out; + } +out: +return ret; +} +EXPORT_SYMBOL (filemap_page_mkwrite); +const struct vm_operations_struct generic_file_vm_ops = { .fault = filemap_fault, + .page_mkwrite = filemap_page_mkwrite, };/* This is used for a general mmap of a disk file */
mm/filemap_xip.c +1 -0
@@ -304,6 +304,7 @@ out:static const struct vm_operations_struct xip_file_vm_ops = { .fault = xip_file_fault, + .page_mkwrite = filemap_page_mkwrite, };int xip_file_mmap (struct file * file,struct vm_area_struct * vma)
Signature · 1 line
1.7.1
A message that is not in the archive (sent off-list or not yet received): 1332978214-15535-1-git-send-email-jack-AlSwsSmVLrQ@public.gmane.org
PATCH v4 5/19 nfsd: Push mnt_want_write() outside of i_mutex
When mnt_want_write() starts to handle freezing it will get a full lock semantics requiring proper lock ordering. So push mnt_want_write() call consistently outside of i_mutex.
--- fs/nfsd/nfs4recover.c | 9 +++-- fs/nfsd/nfsfh.c | 1 + fs/nfsd/nfsproc.c | 9 ++++- fs/nfsd/vfs.c | 79 ++++++++++++++++++++++--------------------- fs/nfsd/vfs.h | 11 +++++- include/linux/nfsd/nfsfh.h | 1 + 6 files changed, 64 insertions(+), 46 deletions(-)
fs/nfsd/nfs4recover.c +5 -4
@@ -135,6 +135,10 @@ void nfsd4_create_clid_dir(struct nfs4_client *clp)if (status <0 )return ; + status =mnt_want_write_file (rec_file); +if (status) +return ; + dir = rec_file->f_path.dentry;/* lock the parent */ mutex_lock (&dir->d_inode->i_mutex);
@@ -154,11 +158,7 @@ void nfsd4_create_clid_dir(struct nfs4_client *clp)* as well be forgiving and just succeed silently. */ goto out_put; - status =mnt_want_write_file (rec_file); -if (status) - goto out_put; status =vfs_mkdir (dir->d_inode, dentry,S_IRWXU ); -mnt_drop_write_file (rec_file); out_put:dput (dentry); out_unlock:
@@ -170,6 +170,7 @@ out_unlock:" (err %d); please check that %s exists" " and is writeable" , status, user_recovery_dirname); +mnt_drop_write_file (rec_file);nfs4_reset_creds (original_cred); }
fs/nfsd/nfsfh.c +1 -0
@@ -635,6 +635,7 @@ fh_put(struct svc_fh *fhp) fhp->fh_post_saved =0 ;#endif } +fh_drop_write (fhp);if (exp) {cache_put (&exp->h, &svc_export_cache); fhp->fh_export =NULL ;
fs/nfsd/nfsproc.c +8 -1
@@ -196,6 +196,7 @@ nfsd_proc_create(struct svc_rqst *rqstp, struct nfsd_createargs *argp,struct dentry *dchild;int type, mode;__be32 nfserr; +int hosterr;dev_t rdev =0 , wanted =new_decode_dev (attr->ia_size); dprintk("nfsd: CREATE %s %.*s\n",
@@ -214,6 +215,12 @@ nfsd_proc_create(struct svc_rqst *rqstp, struct nfsd_createargs *argp, nfserr = nfserr_exist;if (isdotent (argp->name, argp->len)) goto done; + hosterr =fh_want_write (dirfhp); +if (hosterr) { + nfserr =nfserrno (hosterr); + goto done; + } +fh_lock_nested (dirfhp,I_MUTEX_PARENT ); dchild =lookup_one_len (argp->name, dirfhp->fh_dentry, argp->len);if (IS_ERR (dchild)) {
@@ -330,7 +337,7 @@ nfsd_proc_create(struct svc_rqst *rqstp, struct nfsd_createargs *argp, out_unlock:/* We don't really need to unlock, as fh_put does it. */ fh_unlock (dirfhp); - +fh_drop_write (dirfhp); done:fh_put (dirfhp);return nfsd_return_dirop (nfserr, resp);
fs/nfsd/vfs.c +40 -39
221 lines not on this page · raw message
fs/nfsd/vfs.h +9 -2
@@ -108,12 +108,19 @@ int nfsd_set_posix_acl(struct svc_fh *, int, struct posix_acl *);static inline int fh_want_write (struct svc_fh *fh) { -return mnt_want_write (fh->fh_export->ex_path.mnt); +int ret =mnt_want_write (fh->fh_export->ex_path.mnt); + +if (!ret) + fh->fh_want_write =1 ; +return ret; }static inline void fh_drop_write (struct svc_fh *fh) { -mnt_drop_write (fh->fh_export->ex_path.mnt); +if (fh->fh_want_write) { + fh->fh_want_write =0 ; +mnt_drop_write (fh->fh_export->ex_path.mnt); + } }#endif /* LINUX_NFSD_VFS_H */
include/linux/nfsd/nfsfh.h +1 -0
@@ -143,6 +143,7 @@ typedef struct svc_fh {int fh_maxsize;/* max size for fh_handle */ unsigned char fh_locked;/* inode locked by us */ +unsigned char fh_want_write;/* remount protection taken */ #ifdef CONFIG_NFSD_V3 unsigned char fh_post_saved;/* post-op attrs saved */
Signature · 6 lines
1.7.1 -- To unsubscribe from this list: send the line "unsubscribe linux-nfs" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
PATCH v4 14/19 fuse: Convert to new freezing mechanism
In reply to a message not in the archiveConvert check in fuse_file_aio_write() to using new freeze protection.
--- fs/fuse/file.c | 3 ++- 1 files changed, 2 insertions(+), 1 deletions(-)
fs/fuse/file.c +2 -1
@@ -943,8 +943,8 @@ static ssize_t fuse_file_aio_write(struct kiocb *iocb, const struct iovec *iov,if (err)return err; +sb_start_write (inode->i_sb);mutex_lock (&inode->i_mutex); -vfs_check_frozen (inode->i_sb,SB_FREEZE_WRITE );/* We can write back this queue in page reclaim */ current->backing_dev_info = mapping->backing_dev_info;
@@ -970,6 +970,7 @@ static ssize_t fuse_file_aio_write(struct kiocb *iocb, const struct iovec *iov, out: current->backing_dev_info =NULL ;mutex_unlock (&inode->i_mutex); +sb_end_write (inode->i_sb);return written ? written : err; }
Signature · 7 lines
1.7.1 ------------------------------------------------------------------------------ This SF email is sponsosred by: Try Windows Azure free for 90 days Click Here http://p.sf.net/sfu/sfd2d-msazure
PATCH v4 16/19 nilfs2: Convert to new freezing mechanism
In reply to a message not in the archiveWe change nilfs_page_mkwrite() to provide proper freeze protection for writeable page faults (we must wait for frozen filesystem even if the page is fully mapped).
We remove all vfs_check_frozen() checks since they are now handled by the generic code.
--- fs/nilfs2/file.c | 18 +++++++++++------- fs/nilfs2/ioctl.c | 2 -- fs/nilfs2/segment.c | 5 ++++- 3 files changed, 15 insertions(+), 10 deletions(-)
fs/nilfs2/file.c +11 -7
@@ -65,16 +65,18 @@ static int nilfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)struct page *page = vmf->page;struct inode *inode = vma->vm_file->f_dentry->d_inode;struct nilfs_transaction_info ti; -int ret; +int ret =0 ;if (unlikely (nilfs_near_disk_full (inode->i_sb->s_fs_info)))return VM_FAULT_SIGBUS ;/* -ENOSPC */ +sb_start_pagefault (inode->i_sb);lock_page (page);if (page->mapping != inode->i_mapping ||page_offset (page) >=i_size_read (inode) || !PageUptodate (page)) {unlock_page (page); -return VM_FAULT_NOPAGE ;/* make the VM retry the fault */ + ret = -EFAULT ;/* make the VM retry the fault */ + goto out; }/*
@@ -108,19 +110,21 @@ static int nilfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf) ret =nilfs_transaction_begin (inode->i_sb, &ti,1 );/* never returns -ENOMEM, but may return -ENOSPC */ if (unlikely (ret)) -return VM_FAULT_SIGBUS ; + goto out; - ret =block_page_mkwrite (vma, vmf, nilfs_get_block); -if (ret !=VM_FAULT_LOCKED ) { + ret =__block_page_mkwrite (vma, vmf, nilfs_get_block); +if (ret) {nilfs_transaction_abort (inode->i_sb); -return ret; + goto out; }nilfs_set_file_dirty (inode,1 << (PAGE_SHIFT - inode->i_blkbits));nilfs_transaction_commit (inode->i_sb); mapped:wait_on_page_writeback (page); -return VM_FAULT_LOCKED ; + out: +sb_end_pagefault (inode->i_sb); +return block_page_mkwrite_return (ret); }static const struct vm_operations_struct nilfs_file_vm_ops = {
fs/nilfs2/ioctl.c +0 -2
@@ -660,8 +660,6 @@ static int nilfs_ioctl_clean_segments(struct inode *inode, struct file *filp, goto out_free; } -vfs_check_frozen (inode->i_sb,SB_FREEZE_WRITE ); - ret =nilfs_ioctl_move_blocks (inode->i_sb, &argv[0 ], kbufs[0 ]);if (ret <0 ) printk(KERN_ERR "NILFS: GC failed during preparation: "
fs/nilfs2/segment.c +4 -1
@@ -189,7 +189,7 @@ int nilfs_transaction_begin(struct super_block *sb,if (ret >0 )return 0 ; -vfs_check_frozen (sb,SB_FREEZE_WRITE ); +sb_start_intwrite (sb); nilfs = sb->s_fs_info;down_read (&nilfs->ns_segctor_sem);
@@ -205,6 +205,7 @@ int nilfs_transaction_begin(struct super_block *sb, current->journal_info = ti->ti_save;if (ti->ti_flags &NILFS_TI_DYNAMIC_ALLOC )kmem_cache_free (nilfs_transaction_cachep, ti); +sb_end_intwrite (sb);return ret; }
@@ -246,6 +247,7 @@ int nilfs_transaction_commit(struct super_block *sb) err =nilfs_construct_segment (sb);if (ti->ti_flags &NILFS_TI_DYNAMIC_ALLOC )kmem_cache_free (nilfs_transaction_cachep, ti); +sb_end_intwrite (sb);return err; }
@@ -264,6 +266,7 @@ void nilfs_transaction_abort(struct super_block *sb) current->journal_info = ti->ti_save;if (ti->ti_flags &NILFS_TI_DYNAMIC_ALLOC )kmem_cache_free (nilfs_transaction_cachep, ti); +sb_end_intwrite (sb); }void nilfs_relax_pressure_in_lock (struct super_block *sb)
Signature · 6 lines
1.7.1 -- To unsubscribe from this list: send the line "unsubscribe linux-nilfs" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
Jan Kara
maintainerPATCH 2/19 fs: Push mnt_want_write() outside of i_mutex
In reply to Jan KaraCurrently, mnt_want_write() is sometimes called with i_mutex held and sometimes without it. This isn't really a problem because mnt_want_write() is a non-blocking operation (essentially has a trylock semantics) but when the function starts to handle also frozen filesystems, it will get a full lock semantics and thus proper lock ordering has to be established. So move all mnt_want_write() calls outside of i_mutex.
One non-trivial case needing conversion is kern_path_create() / user_path_create() which didn't include mnt_want_write() but now needs to because it acquires i_mutex. Because there are virtual file systems which don't bother with freeze / remount-ro protection we actually provide both versions of the function - one which calls mnt_want_write() and one which does not.
--- fs/namei.c | 115 +++++++++++++++++++++++++++-------------------- fs/ocfs2/refcounttree.c | 10 +--- include/linux/namei.h | 2 + net/unix/af_unix.c | 13 ++---- 4 files changed, 74 insertions(+), 66 deletions(-)
fs/namei.c +66 -49
@@ -2383,7 +2383,9 @@ struct file *do_file_open_root(struct dentry *dentry, struct vfsmount *mnt,return file; } -struct dentry *kern_path_create (int dfd,const char *pathname,struct path *path,int is_dir) +static struct dentry *do_kern_path_create (int dfd,const char *pathname, +struct path *path,int is_dir, +int freeze_protect) {struct dentry *dentry =ERR_PTR (-EEXIST );struct nameidata nd;
@@ -2401,6 +2403,14 @@ struct dentry *kern_path_create(int dfd, const char *pathname, struct path *path nd.flags |=LOOKUP_CREATE |LOOKUP_EXCL ; nd.intent.open.flags =O_EXCL ; +if (freeze_protect) { + error =mnt_want_write (nd.path.mnt); +if (error) { + dentry =ERR_PTR (error); + goto out; + } + } +/* * Do the final lookup. */
@@ -2429,24 +2439,49 @@ eexist: dentry =ERR_PTR (-EEXIST ); fail:mutex_unlock (&nd.path.dentry->d_inode->i_mutex); +if (freeze_protect) +mnt_drop_write (nd.path.mnt); out:path_put (&nd.path);return dentry; } + +struct dentry *kern_path_create (int dfd,const char *pathname,struct path *path,int is_dir) +{ +return do_kern_path_create (dfd, pathname, path, is_dir,0 ); +}EXPORT_SYMBOL (kern_path_create); +struct dentry *kern_path_create_thawed (int dfd,const char *pathname,struct path *path,int is_dir) +{ +return do_kern_path_create (dfd, pathname, path, is_dir,1 ); +} +EXPORT_SYMBOL (kern_path_create_thawed); +struct dentry *user_path_create (int dfd,const char __user *pathname,struct path *path,int is_dir) {char *tmp =getname (pathname);struct dentry *res;if (IS_ERR (tmp))return ERR_CAST (tmp); - res =kern_path_create (dfd, tmp, path, is_dir); + res =do_kern_path_create (dfd, tmp, path, is_dir,0 );putname (tmp);return res; }EXPORT_SYMBOL (user_path_create); +struct dentry *user_path_create_thawed (int dfd,const char __user *pathname,struct path *path,int is_dir) +{ +char *tmp =getname (pathname); +struct dentry *res; +if (IS_ERR (tmp)) +return ERR_CAST (tmp); + res =do_kern_path_create (dfd, tmp, path, is_dir,1 ); +putname (tmp); +return res; +} +EXPORT_SYMBOL (user_path_create_thawed); +int vfs_mknod (struct inode *dir,struct dentry *dentry,umode_t mode,dev_t dev) {int error =may_create (dir, dentry);
@@ -2502,7 +2537,7 @@ SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, umode_t, mode,if (S_ISDIR (mode))return -EPERM ; - dentry =user_path_create (dfd, filename, &path,0 ); + dentry =user_path_create_thawed (dfd, filename, &path,0 );if (IS_ERR (dentry))return PTR_ERR (dentry);
@@ -2511,12 +2546,9 @@ SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, umode_t, mode, error =may_mknod (mode);if (error) goto out_dput; - error =mnt_want_write (path.mnt); -if (error) - goto out_dput; error =security_path_mknod (&path, dentry, mode, dev);if (error) - goto out_drop_write; + goto out_dput;switch (mode &S_IFMT ) {case 0 :case S_IFREG : error =vfs_create (path.dentry->d_inode,dentry,mode,NULL );
@@ -2529,11 +2561,10 @@ SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, umode_t, mode, error =vfs_mknod (path.dentry->d_inode,dentry,mode,0 );break ; } -out_drop_write: -mnt_drop_write (path.mnt); out_dput:dput (dentry);mutex_unlock (&path.dentry->d_inode->i_mutex); +mnt_drop_write (path.mnt);path_put (&path);return error;
@@ -2571,24 +2602,20 @@ SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, umode_t, mode)struct path path;int error; - dentry =user_path_create (dfd, pathname, &path,1 ); + dentry =user_path_create_thawed (dfd, pathname, &path,1 );if (IS_ERR (dentry))return PTR_ERR (dentry);if (!IS_POSIXACL (path.dentry->d_inode)) mode &= ~current_umask (); - error =mnt_want_write (path.mnt); -if (error) - goto out_dput; error =security_path_mkdir (&path, dentry, mode);if (error) - goto out_drop_write; + goto out_dput; error =vfs_mkdir (path.dentry->d_inode, dentry, mode); -out_drop_write: -mnt_drop_write (path.mnt); out_dput:dput (dentry);mutex_unlock (&path.dentry->d_inode->i_mutex); +mnt_drop_write (path.mnt);path_put (&path);return error; }
@@ -2683,6 +2710,9 @@ static long do_rmdir(int dfd, const char __user *pathname) } nd.flags &= ~LOOKUP_PARENT ; + error =mnt_want_write (nd.path.mnt); +if (error) + goto exit1;mutex_lock_nested (&nd.path.dentry->d_inode->i_mutex,I_MUTEX_PARENT ); dentry =lookup_hash (&nd);
@@ -2693,19 +2723,15 @@ static long do_rmdir(int dfd, const char __user *pathname) error = -ENOENT ; goto exit3; } - error =mnt_want_write (nd.path.mnt); -if (error) - goto exit3; error =security_path_rmdir (&nd.path, dentry);if (error) - goto exit4; + goto exit3; error =vfs_rmdir (nd.path.dentry->d_inode, dentry); -exit4: -mnt_drop_write (nd.path.mnt); exit3:dput (dentry); exit2:mutex_unlock (&nd.path.dentry->d_inode->i_mutex); +mnt_drop_write (nd.path.mnt); exit1:path_put (&nd.path);putname (name);
@@ -2772,6 +2798,9 @@ static long do_unlinkat(int dfd, const char __user *pathname) goto exit1; nd.flags &= ~LOOKUP_PARENT ; + error =mnt_want_write (nd.path.mnt); +if (error) + goto exit1;mutex_lock_nested (&nd.path.dentry->d_inode->i_mutex,I_MUTEX_PARENT ); dentry =lookup_hash (&nd);
@@ -2784,21 +2813,17 @@ static long do_unlinkat(int dfd, const char __user *pathname)if (!inode) goto slashes;ihold (inode); - error =mnt_want_write (nd.path.mnt); -if (error) - goto exit2; error =security_path_unlink (&nd.path, dentry);if (error) - goto exit3; + goto exit2; error =vfs_unlink (nd.path.dentry->d_inode, dentry); -exit3: -mnt_drop_write (nd.path.mnt); - exit2: +exit2:dput (dentry); }mutex_unlock (&nd.path.dentry->d_inode->i_mutex);if (inode)iput (inode);/* truncate the inode here */ +mnt_drop_write (nd.path.mnt); exit1:path_put (&nd.path);putname (name);
@@ -2858,23 +2883,19 @@ SYSCALL_DEFINE3(symlinkat, const char __user *, oldname,if (IS_ERR (from))return PTR_ERR (from); - dentry =user_path_create (newdfd, newname, &path,0 ); + dentry =user_path_create_thawed (newdfd, newname, &path,0 ); error =PTR_ERR (dentry);if (IS_ERR (dentry)) goto out_putname; - error =mnt_want_write (path.mnt); -if (error) - goto out_dput; error =security_path_symlink (&path, dentry, from);if (error) - goto out_drop_write; + goto out_dput; error =vfs_symlink (path.dentry->d_inode, dentry, from); -out_drop_write: -mnt_drop_write (path.mnt); out_dput:dput (dentry);mutex_unlock (&path.dentry->d_inode->i_mutex); +mnt_drop_write (path.mnt);path_put (&path); out_putname:putname (from);
@@ -2964,7 +2985,7 @@ SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname,if (error)return error; - new_dentry =user_path_create (newdfd, newname, &new_path,0 ); + new_dentry =user_path_create_thawed (newdfd, newname, &new_path,0 ); error =PTR_ERR (new_dentry);if (IS_ERR (new_dentry)) goto out;
@@ -2972,18 +2993,14 @@ SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname, error = -EXDEV ;if (old_path.mnt != new_path.mnt) goto out_dput; - error =mnt_want_write (new_path.mnt); -if (error) - goto out_dput; error =security_path_link (old_path.dentry, &new_path, new_dentry);if (error) - goto out_drop_write; + goto out_dput; error =vfs_link (old_path.dentry, new_path.dentry->d_inode, new_dentry); -out_drop_write: -mnt_drop_write (new_path.mnt); out_dput:dput (new_dentry);mutex_unlock (&new_path.dentry->d_inode->i_mutex); +mnt_drop_write (new_path.mnt);path_put (&new_path); out:path_put (&old_path);
@@ -3174,6 +3191,10 @@ SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname,if (newnd.last_type !=LAST_NORM ) goto exit2; + error =mnt_want_write (oldnd.path.mnt); +if (error) + goto exit2; + oldnd.flags &= ~LOOKUP_PARENT ; newnd.flags &= ~LOOKUP_PARENT ; newnd.flags |=LOOKUP_RENAME_TARGET ;
@@ -3209,23 +3230,19 @@ SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname,if (new_dentry == trap) goto exit5; - error =mnt_want_write (oldnd.path.mnt); -if (error) - goto exit5; error =security_path_rename (&oldnd.path, old_dentry, &newnd.path, new_dentry);if (error) - goto exit6; + goto exit5; error =vfs_rename (old_dir->d_inode, old_dentry, new_dir->d_inode, new_dentry); -exit6: -mnt_drop_write (oldnd.path.mnt); exit5:dput (new_dentry); exit4:dput (old_dentry); exit3:unlock_rename (new_dir, old_dir); +mnt_drop_write (oldnd.path.mnt); exit2:path_put (&newnd.path);putname (to);
fs/ocfs2/refcounttree.c +2 -8
@@ -4453,7 +4453,7 @@ int ocfs2_reflink_ioctl(struct inode *inode,return error; } - new_dentry =user_path_create (AT_FDCWD , newname, &new_path,0 ); + new_dentry =user_path_create_thawed (AT_FDCWD , newname, &new_path,0 ); error =PTR_ERR (new_dentry);if (IS_ERR (new_dentry)) {mlog_errno (error);
@@ -4466,19 +4466,13 @@ int ocfs2_reflink_ioctl(struct inode *inode, goto out_dput; } - error =mnt_want_write (new_path.mnt); -if (error) { -mlog_errno (error); - goto out_dput; - } - error =ocfs2_vfs_reflink (old_path.dentry, new_path.dentry->d_inode, new_dentry, preserve); -mnt_drop_write (new_path.mnt); out_dput:dput (new_dentry);mutex_unlock (&new_path.dentry->d_inode->i_mutex); +mnt_drop_write (new_path.mnt);path_put (&new_path); out:path_put (&old_path);
include/linux/namei.h +2 -0
@@ -77,7 +77,9 @@ extern int user_path_at_empty(int, const char __user *, unsigned, struct path *,extern int kern_path (const char *,unsigned ,struct path *);extern struct dentry *kern_path_create (int ,const char *,struct path *,int ); +extern struct dentry *kern_path_create_thawed (int ,const char *,struct path *,int );extern struct dentry *user_path_create (int ,const char __user *,struct path *,int ); +extern struct dentry *user_path_create_thawed (int ,const char __user *,struct path *,int );extern int kern_path_parent (const char *,struct nameidata *);extern int vfs_path_lookup (struct dentry *,struct vfsmount *,const char *,unsigned int ,struct path *);
net/unix/af_unix.c +4 -9
@@ -856,7 +856,7 @@ static int unix_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)* Get the parent directory, calculate the hash for last * component. */ - dentry =kern_path_create (AT_FDCWD , sun_path, &path,0 ); + dentry =kern_path_create_thawed (AT_FDCWD , sun_path, &path,0 ); err =PTR_ERR (dentry);if (IS_ERR (dentry)) goto out_mknod_parent;
@@ -866,19 +866,13 @@ static int unix_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)*/ mode =S_IFSOCK | (SOCK_INODE (sock)->i_mode & ~current_umask ()); - err =mnt_want_write (path.mnt); -if (err) - goto out_mknod_dput; err =security_path_mknod (&path, dentry, mode,0 );if (err) - goto out_mknod_drop_write; - err =vfs_mknod (path.dentry->d_inode, dentry, mode,0 ); -out_mknod_drop_write: -mnt_drop_write (path.mnt); -if (err) goto out_mknod_dput; + err =vfs_mknod (path.dentry->d_inode, dentry, mode,0 );mutex_unlock (&path.dentry->d_inode->i_mutex);dput (path.dentry); +mnt_drop_write (path.mnt); path.dentry = dentry; addr->hash =UNIX_HASH_SIZE ;
@@ -916,6 +910,7 @@ out: out_mknod_dput:dput (dentry);mutex_unlock (&path.dentry->d_inode->i_mutex); +mnt_drop_write (path.mnt);path_put (&path); out_mknod_parent:if (err == -EEXIST )
Signature · 1 line
1.7.1
Jan Kara
maintainerPATCH 3/19 fat: Push mnt_want_write() outside of i_mutex
In reply to Jan KaraWhen mnt_want_write() starts to handle freezing it will get a full lock semantics requiring proper lock ordering. So push mnt_want_write() call outside of i_mutex as in other places.
--- fs/fat/file.c | 15 +++++++-------- 1 files changed, 7 insertions(+), 8 deletions(-)
fs/fat/file.c +7 -8
@@ -43,10 +43,10 @@ static int fat_ioctl_set_attributes(struct file *file, u32 __user *user_attr)if (err) goto out; -mutex_lock (&inode->i_mutex); err =mnt_want_write_file (file);if (err) - goto out_unlock_inode; + goto out; +mutex_lock (&inode->i_mutex);/* * ATTR_VOLUME and ATTR_DIR cannot be changed; this also
@@ -73,14 +73,14 @@ static int fat_ioctl_set_attributes(struct file *file, u32 __user *user_attr)/* The root directory has no attributes */ if (inode->i_ino ==MSDOS_ROOT_INO && attr !=ATTR_DIR ) { err = -EINVAL ; - goto out_drop_write; + goto out_unlock_inode; }if (sbi->options.sys_immutable && ((attr | oldattr) &ATTR_SYS ) && !capable (CAP_LINUX_IMMUTABLE )) { err = -EPERM ; - goto out_drop_write; + goto out_unlock_inode; }/*
@@ -90,12 +90,12 @@ static int fat_ioctl_set_attributes(struct file *file, u32 __user *user_attr)*/ err =security_inode_setattr (file->f_path.dentry, &ia);if (err) - goto out_drop_write; + goto out_unlock_inode;/* This MUST be done before doing anything irreversible... */ err =fat_setattr (file->f_path.dentry, &ia);if (err) - goto out_drop_write; + goto out_unlock_inode;fsnotify_change (file->f_path.dentry, ia.ia_valid);if (sbi->options.sys_immutable) {
@@ -107,10 +107,9 @@ static int fat_ioctl_set_attributes(struct file *file, u32 __user *user_attr)fat_save_attrs (inode, attr);mark_inode_dirty (inode); -out_drop_write: -mnt_drop_write_file (file); out_unlock_inode:mutex_unlock (&inode->i_mutex); +mnt_drop_write_file (file); out:return err; }
Signature · 1 line
1.7.1
Jan Kara
maintainerPATCH 4/19 btrfs: Push mnt_want_write() outside of i_mutex
In reply to Jan KaraWhen mnt_want_write() starts to handle freezing it will get a full lock semantics requiring proper lock ordering. So push mnt_want_write() call consistently outside of i_mutex.
--- fs/btrfs/ioctl.c | 23 +++++++++++------------ 1 files changed, 11 insertions(+), 12 deletions(-)
fs/btrfs/ioctl.c +11 -12
@@ -192,6 +192,10 @@ static int btrfs_ioctl_setflags(struct file *file, void __user *arg)if (!inode_owner_or_capable (inode))return -EACCES ; + ret =mnt_want_write_file (file); +if (ret) +return ret; +mutex_lock (&inode->i_mutex); ip_oldflags = ip->flags;
@@ -206,10 +210,6 @@ static int btrfs_ioctl_setflags(struct file *file, void __user *arg) } } - ret =mnt_want_write_file (file); -if (ret) - goto out_unlock; -if (flags &FS_SYNC_FL ) ip->flags |=BTRFS_INODE_SYNC ;else
@@ -271,9 +271,9 @@ static int btrfs_ioctl_setflags(struct file *file, void __user *arg) inode->i_flags = i_oldflags; } -mnt_drop_write_file (file); out_unlock:mutex_unlock (&inode->i_mutex); +mnt_drop_write_file (file);return ret; }
@@ -624,6 +624,10 @@ static noinline int btrfs_mksubvol(struct path *parent,struct dentry *dentry;int error; + error =mnt_want_write (parent->mnt); +if (error) +return error; +mutex_lock_nested (&dir->i_mutex,I_MUTEX_PARENT ); dentry =lookup_one_len (name, parent->dentry, namelen);
@@ -635,13 +639,9 @@ static noinline int btrfs_mksubvol(struct path *parent,if (dentry->d_inode) goto out_dput; - error =mnt_want_write (parent->mnt); -if (error) - goto out_dput; - error =btrfs_may_create (dir, dentry);if (error) - goto out_drop_write; + goto out_dput;down_read (&BTRFS_I (dir)->root->fs_info->subvol_sem);
@@ -659,12 +659,11 @@ static noinline int btrfs_mksubvol(struct path *parent,fsnotify_mkdir (dir, dentry); out_up_read:up_read (&BTRFS_I (dir)->root->fs_info->subvol_sem); -out_drop_write: -mnt_drop_write (parent->mnt); out_dput:dput (dentry); out_unlock:mutex_unlock (&dir->i_mutex); +mnt_drop_write (parent->mnt);return error; }
Signature · 1 line
1.7.1
Jan Kara
maintainerPATCH 6/19 fs: Improve filesystem freezing handling
In reply to Jan Karavfs_check_frozen() tests are racy since the filesystem can be frozen just after the test is performed. Thus in write paths we can end up marking some pages or inodes dirty even though the file system is already frozen. This creates problems with flusher thread hanging on frozen filesystem.
Another problem is that exclusion between ->page_mkwrite() and filesystem freezing has been handled by setting page dirty and then verifying s_frozen. This guaranteed that either the freezing code sees the faulted page, writes it, and writeprotects it again or we see s_frozen set and bail out of page fault. This works to protect from page being marked writeable while filesystem freezing is running but has an unpleasant artefact of leaving dirty (although unmodified and writeprotected) pages on frozen filesystem resulting in similar problems with flusher thread as the first problem.
This patch aims at providing exclusion between write paths and filesystem freezing. We implement a writer-freeze read-write semaphore in the superblock. Actually, there are three such semaphores because of lock ranking reasons - one for page fault handlers (->page_mkwrite), one for all other writers, and one of internal filesystem purposes (used e.g. to track running transactions). Write paths which should block freezing (e.g. directory operations, ->aio_write(), ->page_mkwrite) hold reader side of the semaphore. Code freezing the filesystem takes the writer side.
Only that we don't really want to bounce cachelines of the semaphores between CPUs for each write happening. So we implement the reader side of the semaphore as a per-cpu counter and the writer side is implemented using s_writers.frozen superblock field.
--- fs/super.c | 304 ++++++++++++++++++++++++++++++++++++++++++++++++---- include/linux/fs.h | 48 +++++++-- 2 files changed, 323 insertions(+), 29 deletions(-)
fs/super.c +283 -21
@@ -32,12 +32,20 @@#include <linux/backing-dev.h> #include <linux/rculist_bl.h> #include <linux/cleancache.h> +#include <linux/lockdep.h> #include "internal.h" LIST_HEAD (super_blocks);DEFINE_SPINLOCK (sb_lock); +static struct lock_class_key sb_writers_key[SB_FREEZE_LEVELS ]; +static char *sb_writers_name[SB_FREEZE_LEVELS ] = { +"sb_writers" , +"sb_pagefaults" , +"sb_internal" , +}; +/* * One thing we have to be careful of with a per-sb shrinker is that we don't * drop the last active reference to the superblock from within the shrinker.
@@ -101,6 +109,35 @@ static int prune_super(struct shrinker *shrink, struct shrink_control *sc)return total_objects; } +static int init_sb_writers (struct super_block *s) +{ +int err; +int i; + +for (i =0 ; i <SB_FREEZE_LEVELS ; i++) { + err =percpu_counter_init (&s->s_writers.counter[i],0 ); +if (err <0 ) + goto err_out; +lockdep_init_map (&s->s_writers.lock_map[i], sb_writers_name[i], + &sb_writers_key[i],0 ); + } +init_waitqueue_head (&s->s_writers.wait); +init_waitqueue_head (&s->s_writers.wait_unfrozen); +return 0 ; +err_out: +while (--i >=0 ) +percpu_counter_destroy (&s->s_writers.counter[i]); +return err; +} + +static void destroy_sb_writers (struct super_block *s) +{ +int i; + +for (i =0 ; i <SB_FREEZE_LEVELS ; i++) +percpu_counter_destroy (&s->s_writers.counter[i]); +} +/** * alloc_super - create new superblock * @type: filesystem type superblock should belong to
@@ -115,18 +152,19 @@ static struct super_block *alloc_super(struct file_system_type *type)if (s) {if (security_sb_alloc (s)) { +/* +* We cannot call security_sb_free() without +* security_sb_alloc() succeeding. So bail out manually +*/ kfree (s); s =NULL ; goto out; }#ifdef CONFIG_SMP s->s_files = alloc_percpu(struct list_head); -if (!s->s_files) { -security_sb_free (s); -kfree (s); - s =NULL ; - goto out; - }else { +if (!s->s_files) + goto err_out; +else {int i;for_each_possible_cpu (i)
@@ -135,6 +173,9 @@ static struct super_block *alloc_super(struct file_system_type *type)#else INIT_LIST_HEAD (&s->s_files);#endif +if (init_sb_writers (s)) + goto err_out; + s->s_bdi = &default_backing_dev_info;INIT_HLIST_NODE (&s->s_instances);INIT_HLIST_BL_HEAD (&s->s_anon);
@@ -187,6 +228,16 @@ static struct super_block *alloc_super(struct file_system_type *type) } out:return s; +err_out: +security_sb_free (s); +#ifdef CONFIG_SMP +if (s->s_files) +free_percpu (s->s_files); +#endif +destroy_sb_writers (s); +kfree (s); + s =NULL ; + goto out; }/**
@@ -200,6 +251,7 @@ static inline void destroy_super(struct super_block *s)#ifdef CONFIG_SMP free_percpu (s->s_files);#endif +destroy_sb_writers (s);security_sb_free (s);WARN_ON (!list_empty (&s->s_mounts));kfree (s->s_subtype);
@@ -646,10 +698,11 @@ struct super_block *get_super_thawed(struct block_device *bdev) {while (1 ) {struct super_block *s =get_super (bdev); -if (!s || s->s_frozen ==SB_UNFROZEN ) +if (!s || s->s_writers.frozen ==SB_UNFROZEN )return s;up_read (&s->s_umount); -vfs_check_frozen (s,SB_FREEZE_WRITE ); +wait_event (s->s_writers.wait_unfrozen, + s->s_writers.frozen ==SB_UNFROZEN );put_super (s); } }
@@ -727,7 +780,7 @@ int do_remount_sb(struct super_block *sb, int flags, void *data, int force)int retval;int remount_ro; -if (sb->s_frozen !=SB_UNFROZEN ) +if (sb->s_writers.frozen !=SB_UNFROZEN )return -EBUSY ;#ifdef CONFIG_BLOCK
@@ -1162,6 +1215,196 @@ out:return ERR_PTR (error); } +static void __sb_end_write (struct super_block *sb,int level) +{ +percpu_counter_dec (&sb->s_writers.counter[level-1 ]); +/* +* Make sure s_writers are updated before we wake up waiters in +* freeze_super(). +*/ +smp_mb (); +if (waitqueue_active (&sb->s_writers.wait)) +wake_up (&sb->s_writers.wait); +rwsem_release (&sb->s_writers.lock_map[level-1 ],1 , _RET_IP_); +} + +/** +* sb_end_write - drop write access to a superblock +* @sb: the super we wrote to +* +* Decrement number of writers to the filesystem. Wake up possible waiters +* wanting to freeze the filesystem. +*/ +void sb_end_write (struct super_block *sb) +{ +__sb_end_write (sb,SB_FREEZE_WRITE ); +} +EXPORT_SYMBOL (sb_end_write); + +/** +* sb_end_pagefault - drop write access to a superblock from a page fault +* @sb: the super we wrote to +* +* Decrement number of processes handling write page fault to the filesystem. +* Wake up possible waiters wanting to freeze the filesystem. +*/ +void sb_end_pagefault (struct super_block *sb) +{ +__sb_end_write (sb,SB_FREEZE_PAGEFAULT ); +} +EXPORT_SYMBOL (sb_end_pagefault); + +/** +* sb_end_intwrite - drop write access to a superblock for internal fs purposes +* @sb: the super we wrote to +* +* Decrement fs-internal number of writers to the filesystem. Wake up possible +* waiters wanting to freeze the filesystem. +*/ +void sb_end_intwrite (struct super_block *sb) +{ +__sb_end_write (sb,SB_FREEZE_FS ); +} +EXPORT_SYMBOL (sb_end_intwrite); + +static int __sb_start_write (struct super_block *sb,int level,bool wait) +{ +retry: +if (wait) { +wait_event (sb->s_writers.wait_unfrozen, + sb->s_writers.frozen < level); + }else if (sb->s_writers.frozen >= level) +return 0 ; + +rwsem_acquire_read (&sb->s_writers.lock_map[level-1 ],0 , !wait, + _RET_IP_); +percpu_counter_inc (&sb->s_writers.counter[level-1 ]); +/* +* Make sure counter is updated before we check for frozen. +* freeze_super() first sets frozen and then checks the counter. +*/ +smp_mb (); +if (sb->s_writers.frozen >= level) { +__sb_end_write (sb, level); + goto retry; + } +return 1 ; +} + +/** +* sb_start_write - get write access to a superblock +* @sb: the super we write to +* +* When a process wants to write data or metadata to a file system (i.e. dirty +* a page or an inode), it should embed the operation in a sb_start_write() - +* sb_end_write() pair to get exclusion against file system freezing. This +* function increments number of writers preventing freezing. If the file +* system is already frozen, the function waits until the file system is +* thawed. +* +* Since freeze protection behaves as a lock, users have to preserve +* ordering of freeze protection and other filesystem locks. Generally, +* freeze protection should be the outermost lock. In particular, we have: +* +* sb_start_write +* -> i_mutex (write path, truncate, directory ops, ...) +* -> s_umount (freeze_super, thaw_super) +*/ +void sb_start_write (struct super_block *sb) +{ +__sb_start_write (sb,SB_FREEZE_WRITE , true); +} +EXPORT_SYMBOL (sb_start_write); + +int sb_start_write_trylock (struct super_block *sb) +{ +return __sb_start_write (sb,SB_FREEZE_WRITE , false); +} +EXPORT_SYMBOL (sb_start_write_trylock); + +/** +* sb_start_pagefault - get write access to a superblock from a page fault +* @sb: the super we write to +* +* When a process starts handling write page fault, it should embed the +* operation into sb_start_pagefault() - sb_end_pagefault() pair to get +* exclusion against file system freezing. This is needed since the page fault +* is going to dirty a page. This function increments number of running page +* faults preventing freezing. If the file system is already frozen, the +* function waits until the file system is thawed. +* +* Since page fault freeze protection behaves as a lock, users have to preserve +* ordering of freeze protection and other filesystem locks. It is advised to +* put sb_start_pagefault() close to mmap_sem in lock ordering. Page fault +* handling code implies lock dependency: +* +* mmap_sem +* -> sb_start_pagefault +*/ +void sb_start_pagefault (struct super_block *sb) +{ +__sb_start_write (sb,SB_FREEZE_PAGEFAULT , true); +} +EXPORT_SYMBOL (sb_start_pagefault); + +/* +* sb_start_intwrite - get write access to a superblock for internal fs purposes +* @sb: the super we write to +* +* This is the third level of protection against filesystem freezing. It is +* free for use by a filesystem. The only requirement is that it must rank +* below sb_start_pagefault. +* +* For example filesystem can call sb_start_intwrite() when starting a +* transaction which somewhat eases handling of freezing for internal sources +* of filesystem changes (internal fs threads, discarding preallocation on file +* close, etc.). +*/ +void sb_start_intwrite (struct super_block *sb) +{ +__sb_start_write (sb,SB_FREEZE_FS , true); +} +EXPORT_SYMBOL (sb_start_intwrite); + +/** +* sb_wait_write - wait until all writers to given file system finish +* @sb: the super for which we wait +* @level: type of writers we wait for (normal vs page fault) +* +* This function waits until there are no writers of given type to given file +* system. Caller of this function should make sure there can be no new writers +* of type @level before calling this function. Otherwise this function can +* livelock. +*/ +static void sb_wait_write (struct super_block *sb,int level) +{ +s64 writers; + +/* +* We just cycle-through lockdep here so that it does not complain +* about returning with lock to userspace +*/ +rwsem_acquire (&sb->s_writers.lock_map[level-1 ],0 ,0 , _THIS_IP_); +rwsem_release (&sb->s_writers.lock_map[level-1 ],1 , _THIS_IP_); + +do { +DEFINE_WAIT (wait); + +/* +* We use a barrier in prepare_to_wait() to separate setting +* of frozen and checking of the counter +*/ +prepare_to_wait (&sb->s_writers.wait, &wait, +TASK_UNINTERRUPTIBLE ); + + writers =percpu_counter_sum (&sb->s_writers.counter[level-1 ]); +if (writers) +schedule (); + +finish_wait (&sb->s_writers.wait, &wait); + }while (writers); +} +/** * freeze_super - lock the filesystem and force it into a consistent state * @sb: the super to lock
@@ -1176,7 +1419,7 @@ int freeze_super(struct super_block *sb)atomic_inc (&sb->s_active);down_write (&sb->s_umount); -if (sb->s_frozen) { +if (sb->s_writers.frozen !=SB_UNFROZEN ) {deactivate_locked_super (sb);return -EBUSY ; }
@@ -1187,33 +1430,53 @@ int freeze_super(struct super_block *sb) }if (sb->s_flags &MS_RDONLY ) { - sb->s_frozen =SB_FREEZE_TRANS ; -smp_wmb (); +/* Nothing to do really... */ + sb->s_writers.frozen =SB_FREEZE_COMPLETE ;up_write (&sb->s_umount);return 0 ; } - sb->s_frozen =SB_FREEZE_WRITE ; +/* From now on, no new normal writers can start */ + sb->s_writers.frozen =SB_FREEZE_WRITE ;smp_wmb (); +/* Release s_umount to preserve sb_start_write -> s_umount ordering */ +up_write (&sb->s_umount); + +sb_wait_write (sb,SB_FREEZE_WRITE ); + +/* Now we go and block page faults... */ +down_write (&sb->s_umount); + sb->s_writers.frozen =SB_FREEZE_PAGEFAULT ; +smp_wmb (); + +sb_wait_write (sb,SB_FREEZE_PAGEFAULT ); + +/* All writers are done so after syncing there won't be dirty data */ sync_filesystem (sb); - sb->s_frozen =SB_FREEZE_TRANS ; +/* Now wait for internal filesystem counter */ + sb->s_writers.frozen =SB_FREEZE_FS ;smp_wmb (); +sb_wait_write (sb,SB_FREEZE_FS ); -sync_blockdev (sb->s_bdev);if (sb->s_op->freeze_fs) { ret = sb->s_op->freeze_fs (sb);if (ret) {printk (KERN_ERR "VFS:Filesystem freeze failed\n" ); - sb->s_frozen =SB_UNFROZEN ; + sb->s_writers.frozen =SB_UNFROZEN ;smp_wmb (); -wake_up (&sb->s_wait_unfrozen); +wake_up (&sb->s_writers.wait_unfrozen);deactivate_locked_super (sb);return ret; } } +/* +* This is just for debugging purposes so that fs can warn if it +* sees write activity when frozen is set to SB_FREEZE_COMPLETE. +*/ + sb->s_writers.frozen =SB_FREEZE_COMPLETE ;up_write (&sb->s_umount);return 0 ; }
@@ -1230,7 +1493,7 @@ int thaw_super(struct super_block *sb)int error;down_write (&sb->s_umount); -if (sb->s_frozen ==SB_UNFROZEN ) { +if (sb->s_writers.frozen ==SB_UNFROZEN ) {up_write (&sb->s_umount);return -EINVAL ; }
@@ -1243,16 +1506,15 @@ int thaw_super(struct super_block *sb)if (error) {printk (KERN_ERR "VFS:Filesystem thaw failed\n" ); - sb->s_frozen =SB_FREEZE_TRANS ;up_write (&sb->s_umount);return error; } } out: - sb->s_frozen =SB_UNFROZEN ; + sb->s_writers.frozen =SB_UNFROZEN ;smp_wmb (); -wake_up (&sb->s_wait_unfrozen); +wake_up (&sb->s_writers.wait_unfrozen);deactivate_locked_super (sb);return 0 ;
include/linux/fs.h +40 -8
78 lines not on this page · raw message
Signature · 1 line
1.7.1
Jan Kara
maintainerPATCH 7/19 fs: Add freezing handling to mnt_want_write() / mnt_drop_write()
In reply to Jan KaraMost of places where we want freeze protection coincides with the places where we also have remount-ro protection. So make mnt_want_write() and mnt_drop_write() (and their _file alternative) prevent freezing as well. For the few cases that are really interested only in remount-ro protection provide new function variants.
--- fs/file_table.c | 2 +- fs/inode.c | 4 +- fs/namei.c | 16 +++++++- fs/namespace.c | 101 +++++++++++++++++++++++++++++++++++++++---------- fs/open.c | 2 +- include/linux/mount.h | 4 ++ 6 files changed, 103 insertions(+), 26 deletions(-)
fs/file_table.c +1 -1
@@ -216,7 +216,7 @@ void drop_file_write_access(struct file *file)return ;if (file_check_writeable (file) !=0 )return ; -mnt_drop_write (mnt); +__mnt_drop_write (mnt);file_release_write (file); }EXPORT_SYMBOL_GPL (drop_file_write_access);
fs/inode.c +2 -2
@@ -1580,7 +1580,7 @@ void file_update_time(struct file *file)return ;/* Finally allowed to write? Takes lock. */ -if (mnt_want_write_file (file)) +if (__mnt_want_write_file (file))return ;/* Only change inode inside the lock region */
@@ -1591,7 +1591,7 @@ void file_update_time(struct file *file)if (sync_it &S_MTIME ) inode->i_mtime = now;mark_inode_dirty_sync (inode); -mnt_drop_write_file (file); +__mnt_drop_write_file (file); }EXPORT_SYMBOL (file_update_time);
fs/namei.c +14 -2
@@ -2166,12 +2166,21 @@ static struct file *do_last(struct nameidata *nd, struct path *path,if (nd->last.name[nd->last.len]) goto exit; +/* +* We don't know whether write access will be needed or not. But we +* must get the protection before getting i_mutex due to locking +* constraints. Thus O_CREAT open will get blocked on frozen filesystem +* even if it didn't need to create anything. Not that surprising... +*/ +sb_start_write (dir->d_sb); +mutex_lock (&dir->d_inode->i_mutex); dentry =lookup_hash (nd); error =PTR_ERR (dentry);if (IS_ERR (dentry)) {mutex_unlock (&dir->d_inode->i_mutex); +sb_end_write (dir->d_sb); goto exit; }
@@ -2190,9 +2199,11 @@ static struct file *do_last(struct nameidata *nd, struct path *path,* a permanent write count is taken through * the 'struct file' in nameidata_to_filp(). */ - error =mnt_want_write (nd->path.mnt); -if (error) + error =__mnt_want_write (nd->path.mnt); +if (error) { +sb_end_write (dir->d_sb); goto exit_mutex_unlock; + } want_write =1 ;/* Don't check for write permission, don't truncate */ open_flag &= ~O_TRUNC ;
@@ -2214,6 +2225,7 @@ static struct file *do_last(struct nameidata *nd, struct path *path,* It already exists. */ mutex_unlock (&dir->d_inode->i_mutex); +sb_end_write (dir->d_sb);audit_inode (pathname, path->dentry); error = -EEXIST ;
fs/namespace.c +81 -20
149 lines not on this page · raw message
fs/open.c +1 -1
@@ -637,7 +637,7 @@ static inline int __get_file_write_access(struct inode *inode,/* * Balanced in __fput() */ - error =mnt_want_write (mnt); + error =__mnt_want_write (mnt);if (error)put_write_access (inode); }
include/linux/mount.h +4 -0
@@ -55,10 +55,14 @@ struct vfsmount {struct file ;/* forward dec */ +extern int __mnt_want_write (struct vfsmount *mnt);extern int mnt_want_write (struct vfsmount *mnt); +extern int __mnt_want_write_file (struct file *file);extern int mnt_want_write_file (struct file *file);extern int mnt_clone_write (struct vfsmount *mnt); +extern void __mnt_drop_write (struct vfsmount *mnt);extern void mnt_drop_write (struct vfsmount *mnt); +extern void __mnt_drop_write_file (struct file *file);extern void mnt_drop_write_file (struct file *file);extern void mntput (struct vfsmount *mnt);extern struct vfsmount *mntget (struct vfsmount *mnt);
Signature · 1 line
1.7.1
Szeredi Miklos
maintainerRe: [PATCH 07/19] fs: Add freezing handling to mnt_want_write() / mnt_drop_write()
Quoted 5 lines · Jan Kara
Most of places where we want freeze protection coincides with the places where we also have remount-ro protection. So make
mnt_want_write()andmnt_drop_write()(and their_filealternative) prevent freezing as well. For the few cases that are really interested only in remount-ro protection provide new function variants.
The underlying mechanism of mnt_want_write() and sb_start_write() is basically the same:
- increment per-CPU counter
- mb
- check flag
- retry loop for races
It is scalable but still there is a non-zero overhead, and duplicating it doesn't seem to make a lot of sense. Why not instead try to use a common abstraction for both?
Then the success case will be common, only the relatively rare failure case needs to be handled separately.
Thanks,
Miklos
Jan Kara
maintainerRe: [PATCH 07/19] fs: Add freezing handling to mnt_want_write() / mnt_drop_write()
Quoted 7 lines · Szeredi Miklos
Quoted 5 lines · Jan Kara
Most of places where we want freeze protection coincides with the places where we also have remount-ro protection. So make
mnt_want_write()andmnt_drop_write()(and their_filealternative) prevent freezing as well. For the few cases that are really interested only in remount-ro protection provide new function variants.
Quoted 7 lines
The underlying mechanism of
mnt_want_write()andsb_start_write()is basically the same:- increment per-CPU counter
- mb
- check flag
- retry loop for races
Well, __mnt_want_write() has the special handling of MNT_WRITE_HOLD.
Otherwise yes, the locking logic is rather similar.
Quoted 3 lines
It is scalable but still there is a non-zero overhead, and duplicating it doesn't seem to make a lot of sense. Why not instead try to use a common abstraction for both?
I'm not sure here. Are you concerned about performance - i.e. you would
like to merge the remount-ro counter and topmost anti-freeze counter? That cannot work because in some cases you want remount-ro protection but not freezing protection. In particular when you have file open for writing, you want to refuse remount-ro but still allow freezing.
If you are concerned about code / source size, the implementations of the protection mechanisms could be possibly merged but I'm not sure it's worth the hassle since we'd save like 50 LOC. MNT_WRITE_HOLD is one difference, how each of the implementation detects whether we have succeeded in taking our lock is another difference...
Honza
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Miklos Szeredi
maintainerRe: [PATCH 07/19] fs: Add freezing handling to mnt_want_write() / mnt_drop_write()
Quoted 15 lines · Jan Kara
Quoted 7 lines · Szeredi Miklos
Quoted 5 lines · Jan Kara
Most of places where we want freeze protection coincides with the places where we also have remount-ro protection. So make
mnt_want_write()andmnt_drop_write()(and their_filealternative) prevent freezing as well. For the few cases that are really interested only in remount-ro protection provide new function variants.Quoted 7 lines
The underlying mechanism of
mnt_want_write()andsb_start_write()is basically the same:- increment per-CPU counter
- mb
- check flag
- retry loop for races
Quoted 8 lines
Well,
__mnt_want_write()has the special handling ofMNT_WRITE_HOLD. Otherwise yes, the locking logic is rather similar.Quoted 3 lines
It is scalable but still there is a non-zero overhead, and duplicating it doesn't seem to make a lot of sense. Why not instead try to use a common abstraction for both?
I'm not sure here. Are you concerned about performance - i.e. you would like to merge the remount-ro counter and topmost anti-freeze counter?
Yes.
Quoted 4 lines
That
cannot work because in some cases you want remount-ro protection but not freezing protection. In particular when you have file open for writing, you want to refuse remount-ro but still allow freezing.
Well, you could have a separate counter that keeps track of the difference, which just needs to be modified when the two counters diverge (i.e. when opening a file for write and when releasing that file). But yeah, it may not be worth it.
Thanks,
Miklos
--
To unsubscribe from this list: send the line "unsubscribe linux-fsdevel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Jan Kara
maintainerPATCH 8/19 fs: Skip atime update on frozen filesystem
In reply to Jan KaraIt is unexpected to block reading of frozen filesystem because of atime update. Also handling blocking on frozen filesystem because of atime update would make locking more complex than it already is. So just skip atime update when filesystem is frozen like we skip it when filesystem is remounted read-only.
--- fs/inode.c | 9 +++++++-- 1 files changed, 7 insertions(+), 2 deletions(-)
fs/inode.c +7 -2
@@ -1535,12 +1535,17 @@ void touch_atime(struct vfsmount *mnt, struct dentry *dentry)if (timespec_equal (&inode->i_atime, &now))return ; -if (mnt_want_write (mnt)) +if (!sb_start_write_trylock (inode->i_sb))return ; +if (__mnt_want_write (mnt)) + goto skip_update; + inode->i_atime = now;mark_inode_dirty_sync (inode); -mnt_drop_write (mnt); +__mnt_drop_write (mnt); +skip_update: +sb_end_write (inode->i_sb); }EXPORT_SYMBOL (touch_atime);
Signature · 1 line
1.7.1
Jan Kara
maintainerPATCH 9/19 fs: Protect write paths by sb_start_write - sb_end_write
In reply to Jan KaraThere are several entry points which dirty pages in a filesystem. mmap (handled by block_page_mkwrite()), buffered write (handled by __generic_file_aio_write()), splice write (generic_file_splice_write), truncate, and fallocate (these can dirty last partial page - handled inside each filesystem separately). Protect these places with sb_start_write() and sb_end_write().
->page_mkwrite() calls are particularly complex since they are called with mmap_sem held and thus we cannot use standard sb_start_write() due to lock ordering constraints. We solve the problem by using a special freeze protection sb_start_pagefault() which ranks below mmap_sem.
--- fs/buffer.c | 22 ++++------------------ fs/open.c | 7 ++++++- fs/splice.c | 3 +++ mm/filemap.c | 12 ++++++++++-- mm/filemap_xip.c | 5 +++-- 5 files changed, 26 insertions(+), 23 deletions(-)
fs/buffer.c +4 -18
@@ -2288,8 +2288,8 @@ EXPORT_SYMBOL(block_commit_write);* beyond EOF, then the page is guaranteed safe against truncation until we * unlock the page. * -* Direct callers of this function should call vfs_check_frozen() so that page -* fault does not busyloop until the fs is thawed. +* Direct callers of this function should protect against filesystem freezing +* using sb_start_write() - sb_end_write() functions. */ int __block_page_mkwrite (struct vm_area_struct *vma,struct vm_fault *vmf,get_block_t get_block)
@@ -2327,18 +2327,7 @@ int __block_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf,if (unlikely (ret <0 )) goto out_unlock; -/* -* Freezing in progress? We check after the page is marked dirty and -* with page lock held so if the test here fails, we are sure freezing -* code will wait during syncing until the page fault is done - at that -* point page will be dirty and unlocked so freezing code will write it -* and writeprotect it again. -*/ set_page_dirty (page); -if (inode->i_sb->s_frozen !=SB_UNFROZEN ) { - ret = -EAGAIN ; - goto out_unlock; - }wait_on_page_writeback (page);return 0 ; out_unlock:
@@ -2353,12 +2342,9 @@ int block_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf,int ret;struct super_block *sb = vma->vm_file->f_path.dentry->d_inode->i_sb; -/* -* This check is racy but catches the common case. The check in -* __block_page_mkwrite() is reliable. -*/ -vfs_check_frozen (sb,SB_FREEZE_WRITE ); +sb_start_pagefault (sb); ret =__block_page_mkwrite (vma, vmf, get_block); +sb_end_pagefault (sb);return block_page_mkwrite_return (ret); }EXPORT_SYMBOL (block_page_mkwrite);
fs/open.c +6 -1
@@ -164,11 +164,13 @@ static long do_sys_ftruncate(unsigned int fd, loff_t length, int small)if (IS_APPEND (inode)) goto out_putf; +sb_start_write (inode->i_sb); error =locks_verify_truncate (inode, file, length);if (!error) error =security_path_truncate (&file->f_path);if (!error) error =do_truncate (dentry, length,ATTR_MTIME |ATTR_CTIME , file); +sb_end_write (inode->i_sb); out_putf:fput (file); out:
@@ -266,7 +268,10 @@ int do_fallocate(struct file *file, int mode, loff_t offset, loff_t len)if (!file->f_op->fallocate)return -EOPNOTSUPP ; -return file->f_op->fallocate (file, mode, offset, len); +sb_start_write (inode->i_sb); + ret = file->f_op->fallocate (file, mode, offset, len); +sb_end_write (inode->i_sb); +return ret; }SYSCALL_DEFINE (fallocate)(int fd,int mode,loff_t offset,loff_t len)
fs/splice.c +3 -0
@@ -992,6 +992,8 @@ generic_file_splice_write(struct pipe_inode_info *pipe, struct file *out, };ssize_t ret; +sb_start_write (inode->i_sb); +pipe_lock (pipe);splice_from_pipe_begin (&sd);
@@ -1028,6 +1030,7 @@ generic_file_splice_write(struct pipe_inode_info *pipe, struct file *out, *ppos += ret;balance_dirty_pages_ratelimited_nr (mapping, nr_pages); } +sb_end_write (inode->i_sb);return ret; }
mm/filemap.c +10 -2
@@ -1765,6 +1765,7 @@ int filemap_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)struct inode *inode = vma->vm_file->f_path.dentry->d_inode;int ret =VM_FAULT_LOCKED ; +sb_start_pagefault (inode->i_sb);file_update_time (vma->vm_file);lock_page (page);if (page->mapping != inode->i_mapping) {
@@ -1772,7 +1773,14 @@ int filemap_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf) ret =VM_FAULT_NOPAGE ; goto out; } +/* +* We mark the page dirty already here so that when freeze is in +* progress, we are guaranteed that writeback during freezing will +* see the dirty page and writeprotect it again. +*/ +set_page_dirty (page); out: +sb_end_pagefault (inode->i_sb);return ret; }EXPORT_SYMBOL (filemap_page_mkwrite);
@@ -2536,8 +2544,6 @@ ssize_t __generic_file_aio_write(struct kiocb *iocb, const struct iovec *iov, count = ocount; pos = *ppos; -vfs_check_frozen (inode->i_sb,SB_FREEZE_WRITE ); -/* We can write back this queue in page reclaim */ current->backing_dev_info = mapping->backing_dev_info; written =0 ;
@@ -2634,6 +2640,7 @@ 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);mutex_lock (&inode->i_mutex);blk_start_plug (&plug); ret =__generic_file_aio_write (iocb, iov, nr_segs, &iocb->ki_pos);
@@ -2647,6 +2654,7 @@ ssize_t generic_file_aio_write(struct kiocb *iocb, const struct iovec *iov, ret = err; }blk_finish_plug (&plug); +sb_end_write (inode->i_sb);return ret; }EXPORT_SYMBOL (generic_file_aio_write);
mm/filemap_xip.c +3 -2
@@ -402,6 +402,8 @@ xip_file_write(struct file *filp, const char __user *buf, size_t len,loff_t pos;ssize_t ret; +sb_start_write (inode->i_sb); +mutex_lock (&inode->i_mutex);if (!access_ok (VERIFY_READ , buf, len)) {
@@ -412,8 +414,6 @@ xip_file_write(struct file *filp, const char __user *buf, size_t len, pos = *ppos; count = len; -vfs_check_frozen (inode->i_sb,SB_FREEZE_WRITE ); -/* We can write back this queue in page reclaim */ current->backing_dev_info = mapping->backing_dev_info;
@@ -435,6 +435,7 @@ xip_file_write(struct file *filp, const char __user *buf, size_t len, current->backing_dev_info =NULL ; out_up:mutex_unlock (&inode->i_mutex); +sb_end_write (inode->i_sb);return ret; }EXPORT_SYMBOL_GPL (xip_file_write);
Signature · 1 line
1.7.1
Jan Kara
maintainerPATCH 10/19 ext4: Convert to new freezing mechanism
In reply to Jan KaraWe remove most of frozen checks since upper layer takes care of blocking all writes. We only have to handle protection in ext4_page_mkwrite() in a special way because we cannot use generic block_page_mkwrite().
--- fs/ext4/inode.c | 7 ++----- fs/ext4/mmp.c | 14 ++++++++++---- fs/ext4/super.c | 31 +++++++------------------------ 3 files changed, 19 insertions(+), 33 deletions(-)
fs/ext4/inode.c +2 -5
@@ -4593,11 +4593,7 @@ int ext4_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)get_block_t *get_block;int retries =0 ; -/* -* This check is racy but catches the common case. We rely on -* __block_page_mkwrite() to do a reliable check. -*/ -vfs_check_frozen (inode->i_sb,SB_FREEZE_WRITE ); +sb_start_pagefault (inode->i_sb);/* Delalloc case is easy... */ if (test_opt(inode->i_sb, DELALLOC) && !ext4_should_journal_data(inode) &&
@@ -4665,5 +4661,6 @@ retry_alloc: out_ret: ret =block_page_mkwrite_return (ret); out: +sb_end_pagefault (inode->i_sb);return ret; }
fs/ext4/mmp.c +10 -4
@@ -10,14 +10,20 @@* Write the MMP block using WRITE_SYNC to try to get the block on-disk * faster. */ -static int write_mmp_block (struct buffer_head *bh) +static int write_mmp_block (struct super_block *sb,struct buffer_head *bh) { +/* +* We protect against freezing so that we don't create dirty buffers +* on frozen filesystem. +*/ +sb_start_write (sb);mark_buffer_dirty (bh);lock_buffer (bh); bh->b_end_io = end_buffer_write_sync;get_bh (bh);submit_bh (WRITE_SYNC , bh);wait_on_buffer (bh); +sb_end_write (sb);if (unlikely (!buffer_uptodate (bh)))return 1 ;
@@ -120,7 +126,7 @@ static int kmmpd(void *data) mmp->mmp_time =cpu_to_le64 (get_seconds ()); last_update_time = jiffies; - retval =write_mmp_block (bh); + retval =write_mmp_block (sb, bh);/* * Don't spew too many error messages. Print one every * (s_mmp_update_interval * 60) seconds.
@@ -200,7 +206,7 @@ static int kmmpd(void *data) mmp->mmp_seq =cpu_to_le32 (EXT4_MMP_SEQ_CLEAN ); mmp->mmp_time =cpu_to_le64 (get_seconds ()); - retval =write_mmp_block (bh); + retval =write_mmp_block (sb, bh); failed:kfree (data);
@@ -299,7 +305,7 @@ skip: seq =mmp_new_seq (); mmp->mmp_seq =cpu_to_le32 (seq); - retval =write_mmp_block (bh); + retval =write_mmp_block (sb, bh);if (retval) goto failed;
fs/ext4/super.c +7 -24
@@ -289,33 +289,17 @@ static void ext4_put_nojournal(handle_t *handle)* journal_end calls result in the superblock being marked dirty, so * that sync() will call the filesystem's write_super callback if * appropriate. -* -* To avoid j_barrier hold in userspace when a user calls freeze(), -* ext4 prevents a new handle from being started by s_frozen, which -* is in an upper layer. */ handle_t *ext4_journal_start_sb (struct super_block *sb,int nblocks) {journal_t *journal; -handle_t *handle;trace_ext4_journal_start (sb, nblocks, _RET_IP_);if (sb->s_flags &MS_RDONLY )return ERR_PTR (-EROFS ); +WARN_ON (sb->s_writers.frozen ==SB_FREEZE_COMPLETE ); journal =EXT4_SB (sb)->s_journal; - handle =ext4_journal_current_handle (); - -/* -* If a handle has been started, it should be allowed to -* finish, otherwise deadlock could happen between freeze -* and others(e.g. truncate) due to the restart of the -* journal handle if the filesystem is forzen and active -* handles are not stopped. -*/ -if (!handle) -vfs_check_frozen (sb,SB_FREEZE_TRANS ); -if (!journal)return ext4_get_nojournal ();/*
@@ -2772,6 +2756,7 @@ static int ext4_run_li_request(struct ext4_li_request *elr) sb = elr->lr_super; ngroups =EXT4_SB (sb)->s_groups_count; +sb_start_write (sb);for (group = elr->lr_next_group; group < ngroups; group++) { gdp =ext4_get_group_desc (sb, group,NULL );if (!gdp) {
@@ -2798,6 +2783,7 @@ static int ext4_run_li_request(struct ext4_li_request *elr) elr->lr_next_sched = jiffies + elr->lr_timeout; elr->lr_next_group = group +1 ; } +sb_end_write (sb);return ret; }
@@ -4280,10 +4266,8 @@ int ext4_force_commit(struct super_block *sb)return 0 ; journal =EXT4_SB (sb)->s_journal; -if (journal) { -vfs_check_frozen (sb,SB_FREEZE_TRANS ); +if (journal) ret =ext4_journal_force_commit (journal); - }return ret; }
@@ -4315,9 +4299,8 @@ static int ext4_sync_fs(struct super_block *sb, int wait)* gives us a chance to flush the journal completely and mark the fs clean. * * Note that only this function cannot bring a filesystem to be in a clean -* state independently, because ext4 prevents a new handle from being started -* by @sb->s_frozen, which stays in an upper layer. It thus needs help from -* the upper layer. +* state independently. It relies on upper layer to stop all data & metadata +* modifications. */ static int ext4_freeze (struct super_block *sb) {
@@ -4344,7 +4327,7 @@ static int ext4_freeze(struct super_block *sb)EXT4_CLEAR_INCOMPAT_FEATURE (sb,EXT4_FEATURE_INCOMPAT_RECOVER ); error =ext4_commit_super (sb,1 ); out: -/* we rely on s_frozen to stop further updates */ +/* we rely on upper layer to stop further updates */ jbd2_journal_unlock_updates (EXT4_SB (sb)->s_journal);return error; }
Signature · 1 line
1.7.1
Jan Kara
maintainerPATCH 11/19 xfs: Convert to new freezing code
In reply to Jan KaraGeneric code now blocks all writers from standard write paths. So we add blocking of all writers coming from ioctl (we get a protection of ioctl against racing remount read-only as a bonus) and convert xfs_file_aio_write() to a non-racy freeze protection. We also keep freeze protection on transaction start to block internal filesystem writes such as removal of preallocated blocks.
--- fs/xfs/xfs_file.c | 10 ++++++-- fs/xfs/xfs_ioctl.c | 55 +++++++++++++++++++++++++++++++++++++++++++++++-- fs/xfs/xfs_ioctl32.c | 12 ++++++++++ fs/xfs/xfs_iomap.c | 4 +- fs/xfs/xfs_mount.c | 2 +- fs/xfs/xfs_mount.h | 3 -- fs/xfs/xfs_sync.c | 2 +- fs/xfs/xfs_trans.c | 17 ++++++++++++-- fs/xfs/xfs_trans.h | 2 + 9 files changed, 91 insertions(+), 16 deletions(-)
fs/xfs/xfs_file.c +7 -3
@@ -874,10 +874,12 @@ xfs_file_aio_write(if (ocount ==0 )return 0 ; -xfs_wait_for_freeze (ip->i_mount,SB_FREEZE_WRITE ); +sb_start_write (inode->i_sb); -if (XFS_FORCED_SHUTDOWN (ip->i_mount)) -return -EIO ; +if (XFS_FORCED_SHUTDOWN (ip->i_mount)) { + ret = -EIO ; + goto out; + }if (unlikely (file->f_flags &O_DIRECT )) ret =xfs_file_dio_aio_write (iocb, iovp, nr_segs, pos, ocount);
@@ -896,6 +898,8 @@ xfs_file_aio_write( ret = err; } +out: +sb_end_write (inode->i_sb);return ret; }
fs/xfs/xfs_ioctl.c +52 -3
145 lines not on this page · raw message
fs/xfs/xfs_ioctl32.c +12 -0
@@ -602,7 +602,11 @@ xfs_file_compat_ioctl(if (xfs_compat_growfs_data_copyin (&in, arg))return -XFS_ERROR (EFAULT ); + error =mnt_want_write_file (filp); +if (error) +return error; error =xfs_growfs_data (mp, &in); +mnt_drop_write_file (filp);return -error; }case XFS_IOC_FSGROWFSRT_32 : {
@@ -610,7 +614,11 @@ xfs_file_compat_ioctl(if (xfs_compat_growfs_rt_copyin (&in, arg))return -XFS_ERROR (EFAULT ); + error =mnt_want_write_file (filp); +if (error) +return error; error =xfs_growfs_rt (mp, &in); +mnt_drop_write_file (filp); return -error; }#endif
@@ -629,7 +637,11 @@ xfs_file_compat_ioctl( offsetof(struct xfs_swapext, sx_stat)) || xfs_ioctl32_bstat_copyin(&sxp.sx_stat, &sxu->sx_stat)) return -XFS_ERROR(EFAULT); + error =mnt_want_write_file (filp); +if (error) +return error; error =xfs_swapext (&sxp); +mnt_drop_write_file (filp);return -error; }case XFS_IOC_FSBULKSTAT_32 :
fs/xfs/xfs_iomap.c +2 -2
@@ -679,9 +679,9 @@ xfs_iomap_write_unwritten(* the same inode that we complete here and might deadlock * on the iolock. */ -xfs_wait_for_freeze (mp,SB_FREEZE_TRANS ); +sb_start_intwrite (mp->m_super); tp =_xfs_trans_alloc (mp,XFS_TRANS_STRAT_WRITE ,KM_NOFS ); - tp->t_flags |=XFS_TRANS_RESERVE ; + tp->t_flags |=XFS_TRANS_RESERVE |XFS_TRANS_FREEZE_PROT ; error = xfs_trans_reserve(tp, resblks, XFS_WRITE_LOG_RES(mp), 0, XFS_TRANS_PERM_LOG_RES,
fs/xfs/xfs_mount.c +1 -1
@@ -1541,7 +1541,7 @@ xfs_unmountfs(int xfs_fs_writable (xfs_mount_t *mp) { -return !(xfs_test_for_freeze (mp) ||XFS_FORCED_SHUTDOWN (mp) || +return !(mp->m_super->s_writers.frozen ||XFS_FORCED_SHUTDOWN (mp) || (mp->m_flags &XFS_MOUNT_RDONLY )); }
fs/xfs/xfs_mount.h +0 -3
@@ -310,9 +310,6 @@ void xfs_do_force_shutdown(struct xfs_mount *mp, int flags, char *fname,#define SHUTDOWN_REMOTE_REQ 0x0010/* shutdown came from remote cell */ #define SHUTDOWN_DEVICE_REQ 0x0020/* failed all paths to the device */ -#define xfs_test_for_freeze (mp) ((mp)->m_super->s_frozen) -#define xfs_wait_for_freeze (mp,l) vfs_check_frozen((mp)->m_super, (l)) -/* * Flags for xfs_mountfs */
fs/xfs/xfs_sync.c +1 -1
@@ -498,7 +498,7 @@ xfs_sync_worker(if (!(mp->m_flags &XFS_MOUNT_RDONLY )) {/* dgc: errors ignored here */ -if (mp->m_super->s_frozen ==SB_UNFROZEN && +if (mp->m_super->s_writers.frozen ==SB_UNFROZEN &&xfs_log_need_covered (mp)) error =xfs_fs_log_dummy (mp);else
fs/xfs/xfs_trans.c +14 -3
@@ -577,8 +577,12 @@ xfs_trans_alloc( xfs_mount_t *mp, uint type) { -xfs_wait_for_freeze (mp ,SB_FREEZE_TRANS ); -return _xfs_trans_alloc (mp, type,KM_SLEEP ); +xfs_trans_t *tp; + +sb_start_intwrite (mp->m_super); + tp =_xfs_trans_alloc (mp, type,KM_SLEEP ); + tp->t_flags |=XFS_TRANS_FREEZE_PROT ; +return tp; } xfs_trans_t *
@@ -589,6 +593,7 @@ _xfs_trans_alloc( {xfs_trans_t *tp; +WARN_ON (mp->m_super->s_writers.frozen ==SB_FREEZE_COMPLETE );atomic_inc (&mp->m_active_trans); tp =kmem_zone_zalloc (xfs_trans_zone, memflags);
@@ -612,6 +617,8 @@ xfs_trans_free(xfs_alloc_busy_clear (tp->t_mountp, &tp->t_busy, false);atomic_dec (&tp->t_mountp->m_active_trans); +if (tp->t_flags &XFS_TRANS_FREEZE_PROT ) +sb_end_intwrite (tp->t_mountp->m_super);xfs_trans_free_dqinfo (tp);kmem_zone_free (xfs_trans_zone, tp); }
@@ -644,7 +651,11 @@ xfs_trans_dup(ASSERT (tp->t_flags &XFS_TRANS_PERM_LOG_RES );ASSERT (tp->t_ticket !=NULL ); - ntp->t_flags =XFS_TRANS_PERM_LOG_RES | (tp->t_flags &XFS_TRANS_RESERVE ); + ntp->t_flags =XFS_TRANS_PERM_LOG_RES | + (tp->t_flags &XFS_TRANS_RESERVE ) | + (tp->t_flags &XFS_TRANS_FREEZE_PROT ); +/* We gave our writer reference to the new transaction */ + tp->t_flags &= ~XFS_TRANS_FREEZE_PROT ; ntp->t_ticket =xfs_log_ticket_get (tp->t_ticket); ntp->t_blk_res = tp->t_blk_res - tp->t_blk_res_used; tp->t_blk_res = tp->t_blk_res_used;
fs/xfs/xfs_trans.h +2 -0
@@ -179,6 +179,8 @@ struct xfs_log_item_desc {#define XFS_TRANS_SYNC 0x08/* make commit synchronous */ #define XFS_TRANS_DQ_DIRTY 0x10/* at least one dquot in trx dirty */ #define XFS_TRANS_RESERVE 0x20/* OK to use reserved data blocks */ +#define XFS_TRANS_FREEZE_PROT 0x40/* Transaction has elevated writer +count in superblock */ /* * Values for call flags parameter.
Signature · 6 lines
1.7.1 _______________________________________________ xfs mailing list [email protected] http://oss.sgi.com/mailman/listinfo/xfs
Jan Kara
maintainerPATCH 12/19 ocfs2: Convert to new freezing mechanism
In reply to Jan KaraProtect ocfs2_page_mkwrite() and ocfs2_file_aio_write() using the new freeze protection. We also protect several ioctl entry points which were missing the protection.
--- fs/ocfs2/file.c | 11 +++++++++-- fs/ocfs2/ioctl.c | 14 ++++++++++++-- fs/ocfs2/mmap.c | 2 ++ 3 files changed, 23 insertions(+), 4 deletions(-)
fs/ocfs2/file.c +9 -2
@@ -1971,6 +1971,7 @@ int ocfs2_change_file_space(struct file *file, unsigned int cmd, {struct inode *inode = file->f_path.dentry->d_inode;struct ocfs2_super *osb =OCFS2_SB (inode->i_sb); +int ret;if ((cmd ==OCFS2_IOC_RESVSP || cmd ==OCFS2_IOC_RESVSP64 ) && !ocfs2_writes_unwritten_extents (osb))
@@ -1985,7 +1986,12 @@ int ocfs2_change_file_space(struct file *file, unsigned int cmd,if (!(file->f_mode &FMODE_WRITE ))return -EBADF ; -return __ocfs2_change_file_space (file, inode, file->f_pos, cmd, sr,0 ); + ret =mnt_want_write_file (file); +if (ret) +return ret; + ret =__ocfs2_change_file_space (file, inode, file->f_pos, cmd, sr,0 ); +mnt_drop_write_file (file); +return ret; } static long ocfs2_fallocate(struct file *file, int mode, loff_t offset,
@@ -2261,7 +2267,7 @@ static ssize_t ocfs2_file_aio_write(struct kiocb *iocb,if (iocb->ki_left ==0 )return 0 ; -vfs_check_frozen (inode->i_sb,SB_FREEZE_WRITE ); +sb_start_write (inode->i_sb); appending = file->f_flags &O_APPEND ?1 :0 ; direct_io = file->f_flags &O_DIRECT ?1 :0 ;
@@ -2434,6 +2440,7 @@ out_sems:ocfs2_iocb_clear_sem_locked (iocb);mutex_unlock (&inode->i_mutex); +sb_end_write (inode->i_sb);if (written) ret = written;
fs/ocfs2/ioctl.c +12 -2
@@ -928,7 +928,12 @@ long ocfs2_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)if (get_user (new_clusters, (int __user *)arg))return -EFAULT ; -return ocfs2_group_extend (inode, new_clusters); + status =mnt_want_write_file (filp); +if (status) +return status; + status =ocfs2_group_extend (inode, new_clusters); +mnt_drop_write_file (filp); +return status;case OCFS2_IOC_GROUP_ADD :case OCFS2_IOC_GROUP_ADD64 :if (!capable (CAP_SYS_RESOURCE ))
@@ -937,7 +942,12 @@ long ocfs2_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)if (copy_from_user (&input, (int __user *) arg,sizeof (input)))return -EFAULT ; -return ocfs2_group_add (inode, &input); + status =mnt_want_write_file (filp); +if (status) +return status; + status =ocfs2_group_add (inode, &input); +mnt_drop_write_file (filp); +return status;case OCFS2_IOC_REFLINK :if (copy_from_user (&args, (struct reflink_arguments *)arg,sizeof (args)))
fs/ocfs2/mmap.c +2 -0
@@ -136,6 +136,7 @@ static int ocfs2_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)sigset_t oldset;int ret; +sb_start_pagefault (inode->i_sb);ocfs2_block_signals (&oldset);/*
@@ -165,6 +166,7 @@ static int ocfs2_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf) out:ocfs2_unblock_signals (&oldset); +sb_end_pagefault (inode->i_sb);return ret; }
Signature · 1 line
1.7.1
Jan Kara
maintainerPATCH 13/19 gfs2: Convert to new freezing mechanism
In reply to Jan KaraIt is enough to update gfs2_page_mkwrite() to use new freeze protection. Rest is handled by the generic code.
--- fs/gfs2/file.c | 15 +++------------ 1 files changed, 3 insertions(+), 12 deletions(-)
fs/gfs2/file.c +3 -12
@@ -369,11 +369,7 @@ static int gfs2_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)loff_t size;int ret; -/* Wait if fs is frozen. This is racy so we check again later on -* and retry if the fs has been frozen after the page lock has -* been acquired -*/ -vfs_check_frozen (inode->i_sb,SB_FREEZE_WRITE ); +sb_start_pagefault (inode->i_sb);/* Update file times before taking page lock */ file_update_time (vma->vm_file);
@@ -457,14 +453,9 @@ out:gfs2_holder_uninit (&gh);if (ret ==0 ) {set_page_dirty (page); -/* This check must be post dropping of transaction lock */ -if (inode->i_sb->s_frozen ==SB_UNFROZEN ) { -wait_on_page_writeback (page); - }else { - ret = -EAGAIN ; -unlock_page (page); - } +wait_on_page_writeback (page); } +sb_end_pagefault (inode->i_sb);return block_page_mkwrite_return (ret); }
Signature · 1 line
1.7.1
Re: [PATCH 13/19] gfs2: Convert to new freezing mechanism
Hi,
Quoted 6 lines · Jan Kara
It is enough to update
gfs2_page_mkwrite()to use new freeze protection. Rest is handled by the generic code.CC cluster-devel at redhat.comCC Steven Whitehouse <[email protected]>Signed-off-by Jan Kara <[email protected]>
I think that looks ok to me...
Steve.
Quoted 38 lines
--- fs/gfs2/file.c | 15 +++------------ 1 files changed, 3 insertions(+), 12 deletions(-)fs/gfs2/file.c +3
-12@@ -369,11 +369,7 @@ static int gfs2_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)loff_t size;int ret; -/* Wait if fs is frozen. This is racy so we check again later on -* and retry if the fs has been frozen after the page lock has -* been acquired -*/ -vfs_check_frozen (inode->i_sb,SB_FREEZE_WRITE ); +sb_start_pagefault (inode->i_sb);/* Update file times before taking page lock */ file_update_time (vma->vm_file);@@ -457,14 +453,9 @@ out:gfs2_holder_uninit (&gh);if (ret ==0 ) {set_page_dirty (page); -/* This check must be post dropping of transaction lock */ -if (inode->i_sb->s_frozen ==SB_UNFROZEN ) { -wait_on_page_writeback (page); - }else { - ret = -EAGAIN ; -unlock_page (page); - } +wait_on_page_writeback (page); } +sb_end_pagefault (inode->i_sb);return block_page_mkwrite_return (ret); }
Jan Kara
maintainerPATCH 15/19 ntfs: Convert to new freezing mechanism
In reply to Jan KaraMove check in ntfs_file_aio_write_nolock() to ntfs_file_aio_write() and use new freeze protection.
--- fs/ntfs/file.c | 3 ++- 1 files changed, 2 insertions(+), 1 deletions(-)
fs/ntfs/file.c +2 -1
@@ -2084,7 +2084,6 @@ static ssize_t ntfs_file_aio_write_nolock(struct kiocb *iocb,if (err)return err; pos = *ppos; -vfs_check_frozen (inode->i_sb,SB_FREEZE_WRITE );/* We can write back this queue in page reclaim. */ current->backing_dev_info = mapping->backing_dev_info; written =0 ;
@@ -2117,6 +2116,7 @@ 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);mutex_lock (&inode->i_mutex); ret =ntfs_file_aio_write_nolock (iocb, iov, nr_segs, &iocb->ki_pos);mutex_unlock (&inode->i_mutex);
@@ -2125,6 +2125,7 @@ static ssize_t ntfs_file_aio_write(struct kiocb *iocb, const struct iovec *iov,if (err <0 ) ret = err; } +sb_end_write (inode->i_sb);return ret; }
Signature · 1 line
1.7.1
Jan Kara
maintainerPATCH 17/19 btrfs: Convert to new freezing mechanism
In reply to Jan KaraWe convert btrfs_file_aio_write() to use new freeze check. We also add proper freeze protection to btrfs_page_mkwrite(). Checks in cleaner_kthread() and transaction_kthread() can be safely removed since btrfs_freeze() will lock the mutexes and thus block the threads (and they shouldn't have anything to do anyway).
--- fs/btrfs/disk-io.c | 3 --- fs/btrfs/file.c | 3 ++- fs/btrfs/inode.c | 6 +++++- 3 files changed, 7 insertions(+), 5 deletions(-)
fs/btrfs/disk-io.c +0 -3
@@ -1586,8 +1586,6 @@ static int cleaner_kthread(void *arg)struct btrfs_root *root = arg; do { - vfs_check_frozen(root->fs_info->sb, SB_FREEZE_WRITE); - if (!(root->fs_info->sb->s_flags & MS_RDONLY) && mutex_trylock(&root->fs_info->cleaner_mutex)) { btrfs_run_delayed_iputs(root);
@@ -1618,7 +1616,6 @@ static int transaction_kthread(void *arg) do { delay = HZ * 30; - vfs_check_frozen(root->fs_info->sb, SB_FREEZE_WRITE); mutex_lock(&root->fs_info->transaction_kthread_mutex); spin_lock(&root->fs_info->trans_lock);
fs/btrfs/file.c +2 -1
@@ -1348,7 +1348,7 @@ static ssize_t btrfs_file_aio_write(struct kiocb *iocb,ssize_t err =0 ;size_t count, ocount; -vfs_check_frozen (inode->i_sb,SB_FREEZE_WRITE ); +sb_start_write (inode->i_sb);mutex_lock (&inode->i_mutex);
@@ -1439,6 +1439,7 @@ static ssize_t btrfs_file_aio_write(struct kiocb *iocb, num_written = err; } out: +sb_end_write (inode->i_sb); current->backing_dev_info =NULL ;return num_written ? num_written : err; }
fs/btrfs/inode.c +5 -1
@@ -6405,6 +6405,7 @@ int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)u64 page_start;u64 page_end; +sb_start_pagefault (inode->i_sb); ret =btrfs_delalloc_reserve_space (inode,PAGE_CACHE_SIZE );if (!ret) { ret =btrfs_update_time (vma->vm_file);
@@ -6495,12 +6496,15 @@ again:unlock_extent_cached (io_tree, page_start, page_end, &cached_state,GFP_NOFS ); out_unlock: -if (!ret) +if (!ret) { +sb_end_pagefault (inode->i_sb);return VM_FAULT_LOCKED ; + }unlock_page (page); out:btrfs_delalloc_release_space (inode,PAGE_CACHE_SIZE ); out_noreserve: +sb_end_pagefault (inode->i_sb);return ret; }
Signature · 1 line
1.7.1
Jan Kara
maintainerPATCH 18/19 fs: Remove old freezing mechanism
In reply to Jan KaraNow that all users are converted, we can remove functions, variables, and constants defined by the old freezing mechanism.
--- fs/super.c | 1 - include/linux/fs.h | 9 --------- 2 files changed, 0 insertions(+), 10 deletions(-)
fs/super.c +0 -1
@@ -216,7 +216,6 @@ static struct super_block *alloc_super(struct file_system_type *type)mutex_init (&s->s_dquot.dqio_mutex);mutex_init (&s->s_dquot.dqonoff_mutex);init_rwsem (&s->s_dquot.dqptr_sem); -init_waitqueue_head (&s->s_wait_unfrozen); s->s_maxbytes =MAX_NON_LFS ; s->s_op = &default_op; s->s_time_gran =1000000000 ;
include/linux/fs.h +0 -9
@@ -1420,7 +1420,6 @@ extern spinlock_t sb_lock;enum {SB_UNFROZEN =0 ,/* FS is unfrozen */ SB_FREEZE_WRITE =1 ,/* Writes, dir ops, ioctls frozen */ -SB_FREEZE_TRANS =2 ,SB_FREEZE_PAGEFAULT =2 ,/* Page faults stopped as well */ SB_FREEZE_FS =3 ,/* For internal FS use (e.g. to stop * internal threads if needed) */
@@ -1489,8 +1488,6 @@ struct super_block {struct hlist_node s_instances;struct quota_info s_dquot;/* Diskquota specific options */ -int s_frozen; -wait_queue_head_t s_wait_unfrozen;struct sb_writers s_writers;char s_id[32 ];/* Informational name */
@@ -1543,12 +1540,6 @@ extern void prune_dcache_sb(struct super_block *sb, int nr_to_scan);extern struct timespec current_fs_time (struct super_block *sb);/* -* Snapshotting support. -*/ -/* Will go away when all users are converted */ -#define vfs_check_frozen (sb, level) do { } while (0) - -/* * until VFS tracks user namespaces for inodes, just make all files * belong to init_user_ns */
Signature · 1 line
1.7.1
Jan Kara
maintainerPATCH 19/19 fs: Refuse to freeze filesystem with open but unlinked files
In reply to Jan KaraFilesystem with frozen but unlinked files cannot be forced into a fully consistent state. Also handling of closing such files on frozen filesystem is problematic since fput() can be called with mmap_sem held and protection against frozen filesystem ranks above it.
--- fs/super.c | 26 +++++++++++++++++++------- 1 files changed, 19 insertions(+), 7 deletions(-)
fs/super.c +19 -7
@@ -1459,16 +1459,22 @@ int freeze_super(struct super_block *sb)smp_wmb ();sb_wait_write (sb,SB_FREEZE_FS ); +/* +* Check whether there are some open unlinked files (since all writers +* are blocked, new ones cannot be created now so the check is +* reliable). In that case filesystem cannot be forced into a +* consistent state so we just bail out. +*/ +if (atomic_long_read (&sb->s_remove_count)) { + ret = -EBUSY ; + goto bail; + } +if (sb->s_op->freeze_fs) { ret = sb->s_op->freeze_fs (sb);if (ret) { -printk (KERN_ERR -"VFS:Filesystem freeze failed\n" ); - sb->s_writers.frozen =SB_UNFROZEN ; -smp_wmb (); -wake_up (&sb->s_writers.wait_unfrozen); -deactivate_locked_super (sb); -return ret; +printk (KERN_ERR "VFS: Filesystem freeze failed\n" ); + goto bail; } }/*
@@ -1478,6 +1484,12 @@ int freeze_super(struct super_block *sb) sb->s_writers.frozen =SB_FREEZE_COMPLETE ;up_write (&sb->s_umount);return 0 ; +bail: + sb->s_writers.frozen =SB_UNFROZEN ; +smp_wmb (); +wake_up (&sb->s_writers.wait_unfrozen); +deactivate_locked_super (sb); +return ret; }EXPORT_SYMBOL (freeze_super);
Signature · 1 line
1.7.1