From patchwork Mon Oct 23 21:40:42 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Biggers X-Patchwork-Id: 10023235 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 74D45603D7 for ; Mon, 23 Oct 2017 21:44:03 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 668FF28921 for ; Mon, 23 Oct 2017 21:44:03 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 5B8FB2893D; Mon, 23 Oct 2017 21:44:03 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.3 required=2.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, FREEMAIL_FROM, RCVD_IN_DNSWL_HI, RCVD_IN_SORBS_SPAM, T_DKIM_INVALID autolearn=unavailable version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 0F55E28921 for ; Mon, 23 Oct 2017 21:44:03 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932347AbdJWVn7 (ORCPT ); Mon, 23 Oct 2017 17:43:59 -0400 Received: from mail-it0-f47.google.com ([209.85.214.47]:47443 "EHLO mail-it0-f47.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751667AbdJWVmW (ORCPT ); Mon, 23 Oct 2017 17:42:22 -0400 Received: by mail-it0-f47.google.com with SMTP id p138so7663534itp.2; Mon, 23 Oct 2017 14:42:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=MwU8NLXH/25/d6bIsCheKX9gJkr397GYZ6MiKkLdbzw=; b=rZT3CF0VFfVKR0XZA5ggZMYq2xtwDj92msG2b5uHreLbWb6eLQOp4ubM4OBMtDyiGP UxX80cJWNu9l08zAvlQoOOGe42eTrDYaueZ1BLMFerF2L1T8Hn3itdW3kV+tgAF/J1Iy bCpd41E0HhVzKbOankK42Np3dgy5aH6HjjdqZFi+MQEAvTMjeORcuE9nLKmwHDP3Satm zAJ/WrYkaQ0e6k2Z7+DtFez3LtG8q7Yd8i/kpsnlPg02S1MewJQTg5HrB7XWdHAUd2Pp HKHqAEBpLOs2vCW8d3dOVTbONvmKDKYWkroY5i2viH2rdhDYzFJz6lv7iDqzyYeTb1yA Epsw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=MwU8NLXH/25/d6bIsCheKX9gJkr397GYZ6MiKkLdbzw=; b=pXoq+9a+9voVvf98wSjeeZhduyJECw4RnlzvCSaLGQiETYWMJd3d5YBobxy83UEjqA ueEd+6zO6zcjw/n/veqBrwZYfHeKNOA9Ti8VHxCY9K3VUMEOKTWeoSDYefa9yk1A2dhl /9M4VSsDP63gJwdLJCzsxjLUneQ6/2CTdnz4e4ygGFr3rzkBHU0PzP4PyjZ8p3RAB2U6 rcyUAW/bjwIf1TpW5tczyzSly0I2fh+LBB/rcK9WAi6AMw6AgXtOEAP+m2AJwtrAE4Xy xSOKBsrVavJmzJm4Ygxm7lPz+eFrPX06pFRH7i/DOzjR8xqUcZpPPVKIgX4qZ5UHHVYv ct6A== X-Gm-Message-State: AMCzsaWHKmjKz5KDopR6dpqtdO16xsbysM1xXfl6+KWQWthxwGvOhksO 41ogZpeIEB5FNDYorhSEXdz4c/GV X-Google-Smtp-Source: ABhQp+RJH5bdwhb+eDMUqLDVaXUw+70J7zLZGdOQU2/7Fgf/3gFqtLdScknrr1jnLT4jh9laeuBtew== X-Received: by 10.36.244.69 with SMTP id u5mr11939455iti.67.1508794941530; Mon, 23 Oct 2017 14:42:21 -0700 (PDT) Received: from ebiggers-linuxstation.kir.corp.google.com ([100.66.175.88]) by smtp.gmail.com with ESMTPSA id i63sm3558482ioi.68.2017.10.23.14.42.20 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 23 Oct 2017 14:42:21 -0700 (PDT) From: Eric Biggers To: linux-fscrypt@vger.kernel.org Cc: linux-fsdevel@vger.kernel.org, linux-ext4@vger.kernel.org, linux-f2fs-devel@lists.sourceforge.net, linux-mtd@lists.infradead.org, linux-api@vger.kernel.org, keyrings@vger.kernel.org, "Theodore Y . Ts'o" , Jaegeuk Kim , Gwendal Grignou , Ryo Hashimoto , Sarthak Kukreti , Nick Desaulniers , Michael Halcrow , 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> X-Mailer: git-send-email 2.15.0.rc0.271.g36b669edcc-goog In-Reply-To: <20171023214058.128121-1-ebiggers3@gmail.com> References: <20171023214058.128121-1-ebiggers3@gmail.com> Sender: linux-fscrypt-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-fscrypt@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP 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 *);