From: David Howells Subject: [PATCH 30/30] VFS: Destroy the dentries contributed by a superblock on unmounting [try #11] Date: Thu, 27 Jul 2006 21:53:34 +0100 Message-ID: <20060727205333.8443.97943.stgit@warthog.cambridge.redhat.com> References: <20060727205222.8443.29381.stgit@warthog.cambridge.redhat.com> Reply-To: Linux filesystem caching discussion list Content-Type: text/plain; charset=utf-8; format=fixed Content-Transfer-Encoding: quoted-printable Cc: linux-fsdevel@vger.kernel.org, linux-cachefs@redhat.com, nfsv4@linux-nfs.org, linux-kernel@vger.kernel.org Return-path: To: torvalds@osdl.org, akpm@osdl.org, steved@redhat.com, trond.myklebust@fys.uio.no In-Reply-To: <20060727205222.8443.29381.stgit@warthog.cambridge.redhat.com> List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: linux-cachefs-bounces@redhat.com Errors-To: linux-cachefs-bounces@redhat.com List-Id: linux-fsdevel.vger.kernel.org The attached patch destroys all the dentries attached to a superblock in = one go by: (1) Destroying the tree rooted at s_root. (2) Destroying every entry in the anon list, one at a time. (3) Each entry in the anon list has its subtree consumed from the leaves inwards. This reduces the amount of work generic_shutdown_super() does, and avoids iterating through the dentry_unused list. Note that locking is almost entirely absent in the shrink_dcache_for_umou= nt*() functions added by this patch. This is because: (1) at the point the filesystem calls generic_shutdown_super(), it is no= t permitted to further touch the superblock's set of dentries, and nor= may it remove aliases from inodes; (2) the dcache memory shrinker now skips dentries that are being unmount= ed; and (3) the superblock no longer has any external references through which t= he VFS can reach it. Given these points, the only locking we need to do is when we remove dent= ries from the unused list and the name hashes, which we do a directory's worth= at a time. We also don't need to guard against reference counts going to zero unexpe= ctedly and removing bits of the tree we're working on as nothing else can call d= put(). A cut down version of dentry_iput() has been folded into shrink_dcache_for_umount_subtree() function. Apart from not needing to u= nlock things, it also doesn't need to check for inotify watches. In this version of the patch, the complaint about a dentry still being in= use has been expanded from a single BUG_ON() and now gives much more informat= ion. Signed-Off-By: David Howells Acked-by: NeilBrown Acked-by: Ian Kent --- fs/dcache.c | 133 ++++++++++++++++++++++++++++++++++++++++++= ++++++ fs/super.c | 12 ++-- include/linux/dcache.h | 1=20 3 files changed, 140 insertions(+), 6 deletions(-) diff --git a/fs/dcache.c b/fs/dcache.c index 17b392a..780f014 100644 --- a/fs/dcache.c +++ b/fs/dcache.c @@ -547,6 +547,139 @@ repeat: } =20 /* + * destroy a single subtree of dentries for unmount + * - see the comments on shrink_dcache_for_umount() for a description of= the + * locking + */ +static void shrink_dcache_for_umount_subtree(struct dentry *dentry) +{ + struct dentry *parent; + + BUG_ON(!IS_ROOT(dentry)); + + /* detach this root from the system */ + spin_lock(&dcache_lock); + if (!list_empty(&dentry->d_lru)) { + dentry_stat.nr_unused--; + list_del_init(&dentry->d_lru); + } + __d_drop(dentry); + spin_unlock(&dcache_lock); + + for (;;) { + /* descend to the first leaf in the current subtree */ + while (!list_empty(&dentry->d_subdirs)) { + struct dentry *loop; + + /* this is a branch with children - detach all of them + * from the system in one go */ + spin_lock(&dcache_lock); + list_for_each_entry(loop, &dentry->d_subdirs, + d_u.d_child) { + if (!list_empty(&loop->d_lru)) { + dentry_stat.nr_unused--; + list_del_init(&loop->d_lru); + } + + __d_drop(loop); + cond_resched_lock(&dcache_lock); + } + spin_unlock(&dcache_lock); + + /* move to the first child */ + dentry =3D list_entry(dentry->d_subdirs.next, + struct dentry, d_u.d_child); + } + + /* consume the dentries from this leaf up through its parents + * until we find one with children or run out altogether */ + do { + struct inode *inode; + + if (atomic_read(&dentry->d_count) !=3D 0) { + printk(KERN_ERR + "BUG: Dentry %p{i=3D%lx,n=3D%s}" + " still in use (%d)" + " [unmount of %s %s]\n", + dentry, + dentry->d_inode ? + dentry->d_inode->i_ino : 0UL, + dentry->d_name.name, + atomic_read(&dentry->d_count), + dentry->d_sb->s_type->name, + dentry->d_sb->s_id); + BUG(); + } + + parent =3D dentry->d_parent; + if (parent =3D=3D dentry) + parent =3D NULL; + else + atomic_dec(&parent->d_count); + + list_del(&dentry->d_u.d_child); + dentry_stat.nr_dentry--; /* For d_free, below */ + + inode =3D dentry->d_inode; + if (inode) { +#ifdef CONFIG_INOTIFY + BUG_ON(!list_empty(&inode->inotify_watches)); +#endif + dentry->d_inode =3D NULL; + list_del_init(&dentry->d_alias); + if (dentry->d_op && dentry->d_op->d_iput) + dentry->d_op->d_iput(dentry, inode); + else + iput(inode); + } + + d_free(dentry); + + /* finished when we fall off the top of the tree, + * otherwise we ascend to the parent and move to the + * next sibling if there is one */ + if (!parent) + return; + + dentry =3D parent; + + } while (list_empty(&dentry->d_subdirs)); + + dentry =3D list_entry(dentry->d_subdirs.next, + struct dentry, d_u.d_child); + } +} + +/* + * destroy the dentries attached to a superblock on unmounting + * - we don't need to use dentry->d_lock, and only need dcache_lock when + * removing the dentry from the system lists and hashes because: + * - the superblock is detached from all mountings and open files, so = the + * dentry trees will not be rearranged by the VFS + * - s_umount is write-locked, so the memory pressure shrinker will ig= nore + * any dentries belonging to this superblock that it comes across + * - the filesystem itself is no longer permitted to rearrange the den= tries + * in this superblock + */ +void shrink_dcache_for_umount(struct super_block *sb) +{ + struct dentry *dentry; + + if (down_read_trylock(&sb->s_umount)) + BUG(); + + dentry =3D sb->s_root; + sb->s_root =3D NULL; + atomic_dec(&dentry->d_count); + shrink_dcache_for_umount_subtree(dentry); + + while (!hlist_empty(&sb->s_anon)) { + dentry =3D hlist_entry(sb->s_anon.first, struct dentry, d_hash); + shrink_dcache_for_umount_subtree(dentry); + } +} + +/* * Search for at least 1 mount point in the dentry's subdirs. * We descend to the next level whenever the d_subdirs * list is non-empty and continue searching. diff --git a/fs/super.c b/fs/super.c index 6d4e817..3bf8e5f 100644 --- a/fs/super.c +++ b/fs/super.c @@ -228,17 +228,17 @@ static int grab_super(struct super_block * that need destruction out of superblock, call generic_shutdown_super(= ) * and release aforementioned objects. Note: dentries and inodes _are_ * taken care of and do not need specific handling. + * + * Upon calling this function, the filesystem may no longer alter or + * rearrange the set of dentries belonging to this super_block, nor may = it + * change the attachments of dentries to inodes. */ void generic_shutdown_super(struct super_block *sb) { - struct dentry *root =3D sb->s_root; struct super_operations *sop =3D sb->s_op; =20 - if (root) { - sb->s_root =3D NULL; - shrink_dcache_parent(root); - shrink_dcache_sb(sb); - dput(root); + if (sb->s_root) { + shrink_dcache_for_umount(sb); fsync_super(sb); lock_super(sb); sb->s_flags &=3D ~MS_ACTIVE; diff --git a/include/linux/dcache.h b/include/linux/dcache.h index 44605be..63f64a9 100644 --- a/include/linux/dcache.h +++ b/include/linux/dcache.h @@ -230,6 +230,7 @@ extern struct dentry * d_alloc_anon(stru extern struct dentry * d_splice_alias(struct inode *, struct dentry *); extern void shrink_dcache_sb(struct super_block *); extern void shrink_dcache_parent(struct dentry *); +extern void shrink_dcache_for_umount(struct super_block *); extern int d_invalidate(struct dentry *); =20 /* only used at mount-time */