From: Eric Biggers Subject: [RFC PATCH 09/25] fs/dcache.c: add shrink_dcache_inode() Date: Mon, 23 Oct 2017 14:40:42 -0700 Message-ID: <20171023214058.128121-10-ebiggers3@gmail.com> References: <20171023214058.128121-1-ebiggers3@gmail.com> Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Cc: Ryo Hashimoto , Gwendal Grignou , "Theodore Y . Ts'o" , Eric Biggers , linux-api@vger.kernel.org, Nick Desaulniers , linux-f2fs-devel@lists.sourceforge.net, keyrings@vger.kernel.org, linux-mtd@lists.infradead.org, Michael Halcrow , Sarthak Kukreti , linux-fsdevel@vger.kernel.org, Jaegeuk Kim , linux-ext4@vger.kernel.org To: linux-fscrypt@vger.kernel.org Return-path: In-Reply-To: <20171023214058.128121-1-ebiggers3@gmail.com> List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: linux-f2fs-devel-bounces@lists.sourceforge.net List-Id: linux-ext4.vger.kernel.org From: Eric Biggers When a filesystem encryption key is removed, we need all files which had been "unlocked" (had ->i_crypt_info set up) with it to appear "locked" again. This is most easily done by evicting the inodes. This can currently be done using 'echo 2 > /proc/sys/vm/drop_caches'; however, that is overkill and not usable by non-root users. To evict just the needed inodes we also need the ability to evict those inodes' dentries, since an inode is pinned by its dentries. Therefore, add a function shrink_dcache_inode() which iterates through an inode's dentries and evicts any unused ones as well as any unused descendants (since there may be negative dentries pinning the inode's dentries). Signed-off-by: Eric Biggers --- fs/dcache.c | 33 +++++++++++++++++++++++++++++++++ include/linux/dcache.h | 1 + 2 files changed, 34 insertions(+) diff --git a/fs/dcache.c b/fs/dcache.c index f90141387f01..455540e889f8 100644 --- a/fs/dcache.c +++ b/fs/dcache.c @@ -1456,6 +1456,39 @@ void shrink_dcache_parent(struct dentry *parent) } EXPORT_SYMBOL(shrink_dcache_parent); +/** + * shrink_dcache_inode - prune dcache for inode + * @inode: inode to prune + * + * Evict all unused aliases of the specified inode from the dcache. This is + * intended to be used when trying to evict a specific inode, since inodes are + * pinned by their dentries. We also have to descend to ->d_subdirs for each + * alias, since aliases may be pinned by negative child dentries. + */ +void shrink_dcache_inode(struct inode *inode) +{ + for (;;) { + struct select_data data; + struct dentry *dentry; + + INIT_LIST_HEAD(&data.dispose); + data.start = NULL; + data.found = 0; + + spin_lock(&inode->i_lock); + hlist_for_each_entry(dentry, &inode->i_dentry, d_u.d_alias) + d_walk(dentry, &data, select_collect, NULL); + spin_unlock(&inode->i_lock); + + if (!data.found) + break; + + shrink_dentry_list(&data.dispose); + cond_resched(); + } +} +EXPORT_SYMBOL(shrink_dcache_inode); + static enum d_walk_ret umount_check(void *_data, struct dentry *dentry) { /* it has busy descendents; complain about those instead */ diff --git a/include/linux/dcache.h b/include/linux/dcache.h index ed1a7cf6923a..fb08199d67d5 100644 --- a/include/linux/dcache.h +++ b/include/linux/dcache.h @@ -245,6 +245,7 @@ extern struct dentry * d_obtain_alias(struct inode *); extern struct dentry * d_obtain_root(struct inode *); extern void shrink_dcache_sb(struct super_block *); extern void shrink_dcache_parent(struct dentry *); +extern void shrink_dcache_inode(struct inode *); extern void shrink_dcache_for_umount(struct super_block *); extern void d_invalidate(struct dentry *); -- 2.15.0.rc0.271.g36b669edcc-goog ------------------------------------------------------------------------------ Check out the vibrant tech community on one of the world's most engaging tech sites, Slashdot.org! http://sdm.link/slashdot