Message ID | 20221122145920.17025-1-lhenriques@suse.de |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:f944:0:0:0:0:0 with SMTP id q4csp2260884wrr; Tue, 22 Nov 2022 07:07:00 -0800 (PST) X-Google-Smtp-Source: AA0mqf77mYJ6qvOdj+huG6vk8P/F4yzcmUOjif57BldrE49xtPVyFdFc87pBgcAZjYwbiCfvGOzn X-Received: by 2002:a05:6402:22fa:b0:469:fda0:7e3d with SMTP id dn26-20020a05640222fa00b00469fda07e3dmr1608747edb.303.1669129620385; Tue, 22 Nov 2022 07:07:00 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1669129620; cv=none; d=google.com; s=arc-20160816; b=aymD6xx3qgKH3C70T10B8I0XP5SRjyRRjl/pEC5avQqzryC2z1u+xnhiHIshmEBaV1 QaLjOeFRec6MSdTkjGYDx6hvDFD1cp7JwRT0s8AgdH+AYPFKsKtE/44cJJC8gl0cLXws QYOkB5d48XA1zHG8Fl6oeOQJdlGFI3WsW5QJzOBiQ5zHylX663fINE9BBfcRAp2/HFh1 QbUdqar8z+s+fMb2tftuMsuneimIlVuFv5fWVoCKwcpRWXkYWXyEoMLoY0SuxgfZOhfs 59ZxTPTPtuWvNChOdFLshhyoSpK+jXqzgw+8VwGQaVwwSR+Ka/iIdkOl0kXIkFJv0coT MBvA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature:dkim-signature; bh=PDY+LdTBwzDCxJ4wIwWjdpl3Ohcwlkb8QiK8iZmwvL0=; b=IhMZERDgVgAbgXG6egxZr8UGeqWzwohCSmKfKBSuGxg64WFe+6dVAkBFZn17r9kK6A GUIR/qKVj84tS7pPrJsKvqfHmmdSeVncddHWKiPqyQ/3zMoju/voElUbO9ZCxmS7ie+v tdzO7RshhqpRRxKBdDkTYyo80iqlCftnyXwsDiDAISdgJywFzWKiawsjKFr8bDa2pojj stPYN97upInHu2KXtX3jFf7ubBen+pMbL3SSRCJO2Bh439nENxZDnThE/UXyVks2hHpo ld8rRyvffbNFCIVEVoAlR8BkZdGcyEtXpz8Vyufnq8zoSgHq4qC5kstRGdacSo9W7ZvM wibQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@suse.de header.s=susede2_rsa header.b=tL67Z1gP; dkim=neutral (no key) header.i=@suse.de header.s=susede2_ed25519; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=suse.de Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id g10-20020a1709067c4a00b007309e8d567fsi9855662ejp.145.2022.11.22.07.05.42; Tue, 22 Nov 2022 07:07:00 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@suse.de header.s=susede2_rsa header.b=tL67Z1gP; dkim=neutral (no key) header.i=@suse.de header.s=susede2_ed25519; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=suse.de Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234070AbiKVPAF (ORCPT <rfc822;cjcooper78@gmail.com> + 99 others); Tue, 22 Nov 2022 10:00:05 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46792 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234056AbiKVO7g (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Tue, 22 Nov 2022 09:59:36 -0500 Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.220.29]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DD75D5DB9D; Tue, 22 Nov 2022 06:58:21 -0800 (PST) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out2.suse.de (Postfix) with ESMTPS id 2635B1F85D; Tue, 22 Nov 2022 14:58:20 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1669129100; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=PDY+LdTBwzDCxJ4wIwWjdpl3Ohcwlkb8QiK8iZmwvL0=; b=tL67Z1gPWT95CGg7F+ZpsjSCDrdBtddA367FQIekTVpnUCMkvzBtV1QNVJW81f7RDse9CA ZDkZyY6Mw5VouDmNMgCmCXnfx0Q79d3uvysnltpcyEWA5T/vWc+3drG9lz80Mpv7fIR3k/ nBeZXYklRAdOXFm9DVP3aR7YXRZjvis= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1669129100; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=PDY+LdTBwzDCxJ4wIwWjdpl3Ohcwlkb8QiK8iZmwvL0=; b=psxN47bl/JGtMwYOUlvgZSSxcZhqWPIkpUaWjc/+i3VSL+H2iR7WMlw4/YLQdLT+QmR6TG znO0imF+hcj51JDQ== Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id B904C13B01; Tue, 22 Nov 2022 14:58:19 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id exx3KovjfGNqBQAAMHmgww (envelope-from <lhenriques@suse.de>); Tue, 22 Nov 2022 14:58:19 +0000 Received: from localhost (brahms.olymp [local]) by brahms.olymp (OpenSMTPD) with ESMTPA id a7901fd8; Tue, 22 Nov 2022 14:59:21 +0000 (UTC) From: =?utf-8?q?Lu=C3=ADs_Henriques?= <lhenriques@suse.de> To: Xiubo Li <xiubli@redhat.com>, Ilya Dryomov <idryomov@gmail.com>, Jeff Layton <jlayton@kernel.org> Cc: ceph-devel@vger.kernel.org, linux-kernel@vger.kernel.org, =?utf-8?q?Lu?= =?utf-8?q?=C3=ADs_Henriques?= <lhenriques@suse.de> Subject: [PATCH v3] ceph: mark directory as non-complete complete after loading key Date: Tue, 22 Nov 2022 14:59:20 +0000 Message-Id: <20221122145920.17025-1-lhenriques@suse.de> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,SPF_HELO_NONE, SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1750209260511893716?= X-GMAIL-MSGID: =?utf-8?q?1750209260511893716?= |
Series |
[v3] ceph: mark directory as non-complete complete after loading key
|
|
Commit Message
Luis Henriques
Nov. 22, 2022, 2:59 p.m. UTC
When setting a directory's crypt context, ceph_dir_clear_complete() needs to
be called otherwise if it was complete before, any existing (old) dentry will
still be valid.
This patch adds a wrapper around __fscrypt_prepare_readdir() which will
ensure a directory is marked as non-complete if key status changes.
Signed-off-by: Luís Henriques <lhenriques@suse.de>
---
Hi!
Hopefully I'm not breaking anything else. I've run a bunch of fstests and
didn't saw any (new) failures.
Changes since v2:
- Created helper wrapper for __fscrypt_prepare_readdir()
- Added calls to the new helper
Changes since v1:
- Moved the __ceph_dir_clear_complete() call from ceph_crypt_get_context()
to ceph_lookup().
- Added an __fscrypt_prepare_readdir() wrapper to check key status changes
fs/ceph/crypto.c | 35 +++++++++++++++++++++++++++++++++--
fs/ceph/crypto.h | 6 ++++++
fs/ceph/dir.c | 8 ++++----
fs/ceph/mds_client.c | 6 +++---
4 files changed, 46 insertions(+), 9 deletions(-)
Comments
On 22/11/2022 22:59, Luís Henriques wrote: > When setting a directory's crypt context, ceph_dir_clear_complete() needs to > be called otherwise if it was complete before, any existing (old) dentry will > still be valid. > > This patch adds a wrapper around __fscrypt_prepare_readdir() which will > ensure a directory is marked as non-complete if key status changes. > > Signed-off-by: Luís Henriques <lhenriques@suse.de> > --- > Hi! > > Hopefully I'm not breaking anything else. I've run a bunch of fstests and > didn't saw any (new) failures. > > Changes since v2: > - Created helper wrapper for __fscrypt_prepare_readdir() > - Added calls to the new helper > > Changes since v1: > - Moved the __ceph_dir_clear_complete() call from ceph_crypt_get_context() > to ceph_lookup(). > - Added an __fscrypt_prepare_readdir() wrapper to check key status changes > > fs/ceph/crypto.c | 35 +++++++++++++++++++++++++++++++++-- > fs/ceph/crypto.h | 6 ++++++ > fs/ceph/dir.c | 8 ++++---- > fs/ceph/mds_client.c | 6 +++--- > 4 files changed, 46 insertions(+), 9 deletions(-) > > diff --git a/fs/ceph/crypto.c b/fs/ceph/crypto.c > index 35a2ccfe6899..5ef65a06af98 100644 > --- a/fs/ceph/crypto.c > +++ b/fs/ceph/crypto.c > @@ -397,8 +397,8 @@ int ceph_fname_to_usr(const struct ceph_fname *fname, struct fscrypt_str *tname, > goto out_inode; > } > > - ret = __fscrypt_prepare_readdir(dir); > - if (ret) > + ret = ceph_fscrypt_prepare_readdir(dir); > + if (ret < 0) > goto out_inode; > > /* > @@ -636,3 +636,34 @@ int ceph_fscrypt_encrypt_pages(struct inode *inode, struct page **page, u64 off, > } > return ret; > } > + > +/** > + * ceph_fscrypt_prepare_readdir - simple __fscrypt_prepare_readdir() wrapper > + * @dir: directory inode for readdir prep > + * > + * Simple wrapper around __fscrypt_prepare_readdir() that will mark directory as > + * non-complete if this call results in having the directory unlocked. > + * > + * Returns: > + * 1 - if directory was locked and key is now loaded (i.e. dir is unlocked) > + * 0 - if directory is still locked > + * < 0 - if __fscrypt_prepare_readdir() fails > + */ > +int ceph_fscrypt_prepare_readdir(struct inode *dir) > +{ > + bool had_key = fscrypt_has_encryption_key(dir); > + int err; > + > + if (!IS_ENCRYPTED(dir)) > + return 0; > + > + err = __fscrypt_prepare_readdir(dir); > + if (err) > + return err; > + if (!had_key && fscrypt_has_encryption_key(dir)) { > + /* directory just got unlocked, mark it as not complete */ > + ceph_dir_clear_complete(dir); > + return 1; > + } > + return 0; > +} > diff --git a/fs/ceph/crypto.h b/fs/ceph/crypto.h > index c6ee993f4ec8..cad53ec916fd 100644 > --- a/fs/ceph/crypto.h > +++ b/fs/ceph/crypto.h > @@ -154,6 +154,7 @@ int ceph_fscrypt_decrypt_extents(struct inode *inode, struct page **page, u64 of > struct ceph_sparse_extent *map, u32 ext_cnt); > int ceph_fscrypt_encrypt_pages(struct inode *inode, struct page **page, u64 off, > int len, gfp_t gfp); > +int ceph_fscrypt_prepare_readdir(struct inode *dir); > > static inline struct page *ceph_fscrypt_pagecache_page(struct page *page) > { > @@ -254,6 +255,11 @@ static inline struct page *ceph_fscrypt_pagecache_page(struct page *page) > { > return page; > } > + > +static inline int ceph_fscrypt_prepare_readdir(struct inode *dir) > +{ > + return 0; > +} > #endif /* CONFIG_FS_ENCRYPTION */ > > static inline loff_t ceph_fscrypt_page_offset(struct page *page) > diff --git a/fs/ceph/dir.c b/fs/ceph/dir.c > index edc2bf0aab83..5829f27d058d 100644 > --- a/fs/ceph/dir.c > +++ b/fs/ceph/dir.c > @@ -343,8 +343,8 @@ static int ceph_readdir(struct file *file, struct dir_context *ctx) > ctx->pos = 2; > } > > - err = fscrypt_prepare_readdir(inode); > - if (err) > + err = ceph_fscrypt_prepare_readdir(inode); > + if (err < 0) > return err; > > spin_lock(&ci->i_ceph_lock); > @@ -784,8 +784,8 @@ static struct dentry *ceph_lookup(struct inode *dir, struct dentry *dentry, > return ERR_PTR(-ENAMETOOLONG); > > if (IS_ENCRYPTED(dir)) { > - err = __fscrypt_prepare_readdir(dir); > - if (err) > + err = ceph_fscrypt_prepare_readdir(dir); > + if (err < 0) > return ERR_PTR(err); > if (!fscrypt_has_encryption_key(dir)) { > spin_lock(&dentry->d_lock); > diff --git a/fs/ceph/mds_client.c b/fs/ceph/mds_client.c > index 9518ac8e407d..4becc9eada4b 100644 > --- a/fs/ceph/mds_client.c > +++ b/fs/ceph/mds_client.c > @@ -2551,8 +2551,8 @@ static u8 *get_fscrypt_altname(const struct ceph_mds_request *req, u32 *plen) > if (!IS_ENCRYPTED(dir)) > goto success; > > - ret = __fscrypt_prepare_readdir(dir); > - if (ret) > + ret = ceph_fscrypt_prepare_readdir(dir); > + if (ret < 0) > return ERR_PTR(ret); > > /* No key? Just ignore it. */ > @@ -2668,7 +2668,7 @@ char *ceph_mdsc_build_path(struct dentry *dentry, int *plen, u64 *pbase, int for > spin_unlock(&cur->d_lock); > parent = dget_parent(cur); > > - ret = __fscrypt_prepare_readdir(d_inode(parent)); > + ret = ceph_fscrypt_prepare_readdir(d_inode(parent)); > if (ret < 0) { > dput(parent); > dput(cur); Hi Luis, This version LGTM. Just one nit. I think the following: if (fscrypt_has_encryption_key(d_inode(parent))) { is no needed any more. We can just switch to: if (ret) { And also other places ? Thanks! - Xiubo >
Xiubo Li <xiubli@redhat.com> writes: > On 22/11/2022 22:59, Luís Henriques wrote: >> When setting a directory's crypt context, ceph_dir_clear_complete() needs to >> be called otherwise if it was complete before, any existing (old) dentry will >> still be valid. >> >> This patch adds a wrapper around __fscrypt_prepare_readdir() which will >> ensure a directory is marked as non-complete if key status changes. >> >> Signed-off-by: Luís Henriques <lhenriques@suse.de> >> --- >> Hi! >> >> Hopefully I'm not breaking anything else. I've run a bunch of fstests and >> didn't saw any (new) failures. >> >> Changes since v2: >> - Created helper wrapper for __fscrypt_prepare_readdir() >> - Added calls to the new helper >> >> Changes since v1: >> - Moved the __ceph_dir_clear_complete() call from ceph_crypt_get_context() >> to ceph_lookup(). >> - Added an __fscrypt_prepare_readdir() wrapper to check key status changes >> >> fs/ceph/crypto.c | 35 +++++++++++++++++++++++++++++++++-- >> fs/ceph/crypto.h | 6 ++++++ >> fs/ceph/dir.c | 8 ++++---- >> fs/ceph/mds_client.c | 6 +++--- >> 4 files changed, 46 insertions(+), 9 deletions(-) >> >> diff --git a/fs/ceph/crypto.c b/fs/ceph/crypto.c >> index 35a2ccfe6899..5ef65a06af98 100644 >> --- a/fs/ceph/crypto.c >> +++ b/fs/ceph/crypto.c >> @@ -397,8 +397,8 @@ int ceph_fname_to_usr(const struct ceph_fname *fname, struct fscrypt_str *tname, >> goto out_inode; >> } >> - ret = __fscrypt_prepare_readdir(dir); >> - if (ret) >> + ret = ceph_fscrypt_prepare_readdir(dir); >> + if (ret < 0) >> goto out_inode; >> /* >> @@ -636,3 +636,34 @@ int ceph_fscrypt_encrypt_pages(struct inode *inode, struct page **page, u64 off, >> } >> return ret; >> } >> + >> +/** >> + * ceph_fscrypt_prepare_readdir - simple __fscrypt_prepare_readdir() wrapper >> + * @dir: directory inode for readdir prep >> + * >> + * Simple wrapper around __fscrypt_prepare_readdir() that will mark directory as >> + * non-complete if this call results in having the directory unlocked. >> + * >> + * Returns: >> + * 1 - if directory was locked and key is now loaded (i.e. dir is unlocked) >> + * 0 - if directory is still locked >> + * < 0 - if __fscrypt_prepare_readdir() fails >> + */ >> +int ceph_fscrypt_prepare_readdir(struct inode *dir) >> +{ >> + bool had_key = fscrypt_has_encryption_key(dir); >> + int err; >> + >> + if (!IS_ENCRYPTED(dir)) >> + return 0; >> + >> + err = __fscrypt_prepare_readdir(dir); >> + if (err) >> + return err; >> + if (!had_key && fscrypt_has_encryption_key(dir)) { >> + /* directory just got unlocked, mark it as not complete */ >> + ceph_dir_clear_complete(dir); >> + return 1; >> + } >> + return 0; >> +} >> diff --git a/fs/ceph/crypto.h b/fs/ceph/crypto.h >> index c6ee993f4ec8..cad53ec916fd 100644 >> --- a/fs/ceph/crypto.h >> +++ b/fs/ceph/crypto.h >> @@ -154,6 +154,7 @@ int ceph_fscrypt_decrypt_extents(struct inode *inode, struct page **page, u64 of >> struct ceph_sparse_extent *map, u32 ext_cnt); >> int ceph_fscrypt_encrypt_pages(struct inode *inode, struct page **page, u64 off, >> int len, gfp_t gfp); >> +int ceph_fscrypt_prepare_readdir(struct inode *dir); >> static inline struct page *ceph_fscrypt_pagecache_page(struct page *page) >> { >> @@ -254,6 +255,11 @@ static inline struct page *ceph_fscrypt_pagecache_page(struct page *page) >> { >> return page; >> } >> + >> +static inline int ceph_fscrypt_prepare_readdir(struct inode *dir) >> +{ >> + return 0; >> +} >> #endif /* CONFIG_FS_ENCRYPTION */ >> static inline loff_t ceph_fscrypt_page_offset(struct page *page) >> diff --git a/fs/ceph/dir.c b/fs/ceph/dir.c >> index edc2bf0aab83..5829f27d058d 100644 >> --- a/fs/ceph/dir.c >> +++ b/fs/ceph/dir.c >> @@ -343,8 +343,8 @@ static int ceph_readdir(struct file *file, struct dir_context *ctx) >> ctx->pos = 2; >> } >> - err = fscrypt_prepare_readdir(inode); >> - if (err) >> + err = ceph_fscrypt_prepare_readdir(inode); >> + if (err < 0) >> return err; >> spin_lock(&ci->i_ceph_lock); >> @@ -784,8 +784,8 @@ static struct dentry *ceph_lookup(struct inode *dir, struct dentry *dentry, >> return ERR_PTR(-ENAMETOOLONG); >> if (IS_ENCRYPTED(dir)) { >> - err = __fscrypt_prepare_readdir(dir); >> - if (err) >> + err = ceph_fscrypt_prepare_readdir(dir); >> + if (err < 0) >> return ERR_PTR(err); >> if (!fscrypt_has_encryption_key(dir)) { >> spin_lock(&dentry->d_lock); >> diff --git a/fs/ceph/mds_client.c b/fs/ceph/mds_client.c >> index 9518ac8e407d..4becc9eada4b 100644 >> --- a/fs/ceph/mds_client.c >> +++ b/fs/ceph/mds_client.c >> @@ -2551,8 +2551,8 @@ static u8 *get_fscrypt_altname(const struct ceph_mds_request *req, u32 *plen) >> if (!IS_ENCRYPTED(dir)) >> goto success; >> - ret = __fscrypt_prepare_readdir(dir); >> - if (ret) >> + ret = ceph_fscrypt_prepare_readdir(dir); >> + if (ret < 0) >> return ERR_PTR(ret); >> /* No key? Just ignore it. */ >> @@ -2668,7 +2668,7 @@ char *ceph_mdsc_build_path(struct dentry *dentry, int *plen, u64 *pbase, int for >> spin_unlock(&cur->d_lock); >> parent = dget_parent(cur); >> - ret = __fscrypt_prepare_readdir(d_inode(parent)); >> + ret = ceph_fscrypt_prepare_readdir(d_inode(parent)); >> if (ret < 0) { >> dput(parent); >> dput(cur); > > Hi Luis, > > This version LGTM. > > Just one nit. I think the following: > > if (fscrypt_has_encryption_key(d_inode(parent))) { > > is no needed any more. > > We can just switch to: > > if (ret) { > > And also other places ? No, that won't work, and I found that out the hard way :-) If the directory is unlocked (i.e. the key is loaded), then the helper function ceph_fscrypt_prepare_readdir() will return '0' because the status hasn't change. Thus, we still need to check that here. Cheers,
On 23/11/2022 17:59, Luís Henriques wrote: > Xiubo Li <xiubli@redhat.com> writes: > >> On 22/11/2022 22:59, Luís Henriques wrote: >>> When setting a directory's crypt context, ceph_dir_clear_complete() needs to >>> be called otherwise if it was complete before, any existing (old) dentry will >>> still be valid. >>> >>> This patch adds a wrapper around __fscrypt_prepare_readdir() which will >>> ensure a directory is marked as non-complete if key status changes. >>> >>> Signed-off-by: Luís Henriques <lhenriques@suse.de> >>> --- >>> Hi! >>> >>> Hopefully I'm not breaking anything else. I've run a bunch of fstests and >>> didn't saw any (new) failures. >>> >>> Changes since v2: >>> - Created helper wrapper for __fscrypt_prepare_readdir() >>> - Added calls to the new helper >>> >>> Changes since v1: >>> - Moved the __ceph_dir_clear_complete() call from ceph_crypt_get_context() >>> to ceph_lookup(). >>> - Added an __fscrypt_prepare_readdir() wrapper to check key status changes >>> >>> fs/ceph/crypto.c | 35 +++++++++++++++++++++++++++++++++-- >>> fs/ceph/crypto.h | 6 ++++++ >>> fs/ceph/dir.c | 8 ++++---- >>> fs/ceph/mds_client.c | 6 +++--- >>> 4 files changed, 46 insertions(+), 9 deletions(-) >>> >>> diff --git a/fs/ceph/crypto.c b/fs/ceph/crypto.c >>> index 35a2ccfe6899..5ef65a06af98 100644 >>> --- a/fs/ceph/crypto.c >>> +++ b/fs/ceph/crypto.c >>> @@ -397,8 +397,8 @@ int ceph_fname_to_usr(const struct ceph_fname *fname, struct fscrypt_str *tname, >>> goto out_inode; >>> } >>> - ret = __fscrypt_prepare_readdir(dir); >>> - if (ret) >>> + ret = ceph_fscrypt_prepare_readdir(dir); >>> + if (ret < 0) >>> goto out_inode; >>> /* >>> @@ -636,3 +636,34 @@ int ceph_fscrypt_encrypt_pages(struct inode *inode, struct page **page, u64 off, >>> } >>> return ret; >>> } >>> + >>> +/** >>> + * ceph_fscrypt_prepare_readdir - simple __fscrypt_prepare_readdir() wrapper >>> + * @dir: directory inode for readdir prep >>> + * >>> + * Simple wrapper around __fscrypt_prepare_readdir() that will mark directory as >>> + * non-complete if this call results in having the directory unlocked. >>> + * >>> + * Returns: >>> + * 1 - if directory was locked and key is now loaded (i.e. dir is unlocked) >>> + * 0 - if directory is still locked >>> + * < 0 - if __fscrypt_prepare_readdir() fails >>> + */ >>> +int ceph_fscrypt_prepare_readdir(struct inode *dir) >>> +{ >>> + bool had_key = fscrypt_has_encryption_key(dir); >>> + int err; >>> + >>> + if (!IS_ENCRYPTED(dir)) >>> + return 0; >>> + >>> + err = __fscrypt_prepare_readdir(dir); >>> + if (err) >>> + return err; >>> + if (!had_key && fscrypt_has_encryption_key(dir)) { >>> + /* directory just got unlocked, mark it as not complete */ >>> + ceph_dir_clear_complete(dir); >>> + return 1; >>> + } >>> + return 0; >>> +} >>> diff --git a/fs/ceph/crypto.h b/fs/ceph/crypto.h >>> index c6ee993f4ec8..cad53ec916fd 100644 >>> --- a/fs/ceph/crypto.h >>> +++ b/fs/ceph/crypto.h >>> @@ -154,6 +154,7 @@ int ceph_fscrypt_decrypt_extents(struct inode *inode, struct page **page, u64 of >>> struct ceph_sparse_extent *map, u32 ext_cnt); >>> int ceph_fscrypt_encrypt_pages(struct inode *inode, struct page **page, u64 off, >>> int len, gfp_t gfp); >>> +int ceph_fscrypt_prepare_readdir(struct inode *dir); >>> static inline struct page *ceph_fscrypt_pagecache_page(struct page *page) >>> { >>> @@ -254,6 +255,11 @@ static inline struct page *ceph_fscrypt_pagecache_page(struct page *page) >>> { >>> return page; >>> } >>> + >>> +static inline int ceph_fscrypt_prepare_readdir(struct inode *dir) >>> +{ >>> + return 0; >>> +} >>> #endif /* CONFIG_FS_ENCRYPTION */ >>> static inline loff_t ceph_fscrypt_page_offset(struct page *page) >>> diff --git a/fs/ceph/dir.c b/fs/ceph/dir.c >>> index edc2bf0aab83..5829f27d058d 100644 >>> --- a/fs/ceph/dir.c >>> +++ b/fs/ceph/dir.c >>> @@ -343,8 +343,8 @@ static int ceph_readdir(struct file *file, struct dir_context *ctx) >>> ctx->pos = 2; >>> } >>> - err = fscrypt_prepare_readdir(inode); >>> - if (err) >>> + err = ceph_fscrypt_prepare_readdir(inode); >>> + if (err < 0) >>> return err; >>> spin_lock(&ci->i_ceph_lock); >>> @@ -784,8 +784,8 @@ static struct dentry *ceph_lookup(struct inode *dir, struct dentry *dentry, >>> return ERR_PTR(-ENAMETOOLONG); >>> if (IS_ENCRYPTED(dir)) { >>> - err = __fscrypt_prepare_readdir(dir); >>> - if (err) >>> + err = ceph_fscrypt_prepare_readdir(dir); >>> + if (err < 0) >>> return ERR_PTR(err); >>> if (!fscrypt_has_encryption_key(dir)) { >>> spin_lock(&dentry->d_lock); >>> diff --git a/fs/ceph/mds_client.c b/fs/ceph/mds_client.c >>> index 9518ac8e407d..4becc9eada4b 100644 >>> --- a/fs/ceph/mds_client.c >>> +++ b/fs/ceph/mds_client.c >>> @@ -2551,8 +2551,8 @@ static u8 *get_fscrypt_altname(const struct ceph_mds_request *req, u32 *plen) >>> if (!IS_ENCRYPTED(dir)) >>> goto success; >>> - ret = __fscrypt_prepare_readdir(dir); >>> - if (ret) >>> + ret = ceph_fscrypt_prepare_readdir(dir); >>> + if (ret < 0) >>> return ERR_PTR(ret); >>> /* No key? Just ignore it. */ >>> @@ -2668,7 +2668,7 @@ char *ceph_mdsc_build_path(struct dentry *dentry, int *plen, u64 *pbase, int for >>> spin_unlock(&cur->d_lock); >>> parent = dget_parent(cur); >>> - ret = __fscrypt_prepare_readdir(d_inode(parent)); >>> + ret = ceph_fscrypt_prepare_readdir(d_inode(parent)); >>> if (ret < 0) { >>> dput(parent); >>> dput(cur); >> Hi Luis, >> >> This version LGTM. >> >> Just one nit. I think the following: >> >> if (fscrypt_has_encryption_key(d_inode(parent))) { >> >> is no needed any more. >> >> We can just switch to: >> >> if (ret) { >> >> And also other places ? > No, that won't work, and I found that out the hard way :-) > > If the directory is unlocked (i.e. the key is loaded), then the helper > function ceph_fscrypt_prepare_readdir() will return '0' because the status > hasn't change. Thus, we still need to check that here. Yeah, sounds reasonable! LGTM and I will run the test later! Thanks Luis. - Xiubo > Cheers,
Hi Luis, BTW, which branch are you using to generate this patch ? Could you rebase to the testing branch ? Thanks - Xiubo On 22/11/2022 22:59, Luís Henriques wrote: > When setting a directory's crypt context, ceph_dir_clear_complete() needs to > be called otherwise if it was complete before, any existing (old) dentry will > still be valid. > > This patch adds a wrapper around __fscrypt_prepare_readdir() which will > ensure a directory is marked as non-complete if key status changes. > > Signed-off-by: Luís Henriques <lhenriques@suse.de> > --- > Hi! > > Hopefully I'm not breaking anything else. I've run a bunch of fstests and > didn't saw any (new) failures. > > Changes since v2: > - Created helper wrapper for __fscrypt_prepare_readdir() > - Added calls to the new helper > > Changes since v1: > - Moved the __ceph_dir_clear_complete() call from ceph_crypt_get_context() > to ceph_lookup(). > - Added an __fscrypt_prepare_readdir() wrapper to check key status changes > > fs/ceph/crypto.c | 35 +++++++++++++++++++++++++++++++++-- > fs/ceph/crypto.h | 6 ++++++ > fs/ceph/dir.c | 8 ++++---- > fs/ceph/mds_client.c | 6 +++--- > 4 files changed, 46 insertions(+), 9 deletions(-) > > diff --git a/fs/ceph/crypto.c b/fs/ceph/crypto.c > index 35a2ccfe6899..5ef65a06af98 100644 > --- a/fs/ceph/crypto.c > +++ b/fs/ceph/crypto.c > @@ -397,8 +397,8 @@ int ceph_fname_to_usr(const struct ceph_fname *fname, struct fscrypt_str *tname, > goto out_inode; > } > > - ret = __fscrypt_prepare_readdir(dir); > - if (ret) > + ret = ceph_fscrypt_prepare_readdir(dir); > + if (ret < 0) > goto out_inode; > > /* > @@ -636,3 +636,34 @@ int ceph_fscrypt_encrypt_pages(struct inode *inode, struct page **page, u64 off, > } > return ret; > } > + > +/** > + * ceph_fscrypt_prepare_readdir - simple __fscrypt_prepare_readdir() wrapper > + * @dir: directory inode for readdir prep > + * > + * Simple wrapper around __fscrypt_prepare_readdir() that will mark directory as > + * non-complete if this call results in having the directory unlocked. > + * > + * Returns: > + * 1 - if directory was locked and key is now loaded (i.e. dir is unlocked) > + * 0 - if directory is still locked > + * < 0 - if __fscrypt_prepare_readdir() fails > + */ > +int ceph_fscrypt_prepare_readdir(struct inode *dir) > +{ > + bool had_key = fscrypt_has_encryption_key(dir); > + int err; > + > + if (!IS_ENCRYPTED(dir)) > + return 0; > + > + err = __fscrypt_prepare_readdir(dir); > + if (err) > + return err; > + if (!had_key && fscrypt_has_encryption_key(dir)) { > + /* directory just got unlocked, mark it as not complete */ > + ceph_dir_clear_complete(dir); > + return 1; > + } > + return 0; > +} > diff --git a/fs/ceph/crypto.h b/fs/ceph/crypto.h > index c6ee993f4ec8..cad53ec916fd 100644 > --- a/fs/ceph/crypto.h > +++ b/fs/ceph/crypto.h > @@ -154,6 +154,7 @@ int ceph_fscrypt_decrypt_extents(struct inode *inode, struct page **page, u64 of > struct ceph_sparse_extent *map, u32 ext_cnt); > int ceph_fscrypt_encrypt_pages(struct inode *inode, struct page **page, u64 off, > int len, gfp_t gfp); > +int ceph_fscrypt_prepare_readdir(struct inode *dir); > > static inline struct page *ceph_fscrypt_pagecache_page(struct page *page) > { > @@ -254,6 +255,11 @@ static inline struct page *ceph_fscrypt_pagecache_page(struct page *page) > { > return page; > } > + > +static inline int ceph_fscrypt_prepare_readdir(struct inode *dir) > +{ > + return 0; > +} > #endif /* CONFIG_FS_ENCRYPTION */ > > static inline loff_t ceph_fscrypt_page_offset(struct page *page) > diff --git a/fs/ceph/dir.c b/fs/ceph/dir.c > index edc2bf0aab83..5829f27d058d 100644 > --- a/fs/ceph/dir.c > +++ b/fs/ceph/dir.c > @@ -343,8 +343,8 @@ static int ceph_readdir(struct file *file, struct dir_context *ctx) > ctx->pos = 2; > } > > - err = fscrypt_prepare_readdir(inode); > - if (err) > + err = ceph_fscrypt_prepare_readdir(inode); > + if (err < 0) > return err; > > spin_lock(&ci->i_ceph_lock); > @@ -784,8 +784,8 @@ static struct dentry *ceph_lookup(struct inode *dir, struct dentry *dentry, > return ERR_PTR(-ENAMETOOLONG); > > if (IS_ENCRYPTED(dir)) { > - err = __fscrypt_prepare_readdir(dir); > - if (err) > + err = ceph_fscrypt_prepare_readdir(dir); > + if (err < 0) > return ERR_PTR(err); > if (!fscrypt_has_encryption_key(dir)) { > spin_lock(&dentry->d_lock); > diff --git a/fs/ceph/mds_client.c b/fs/ceph/mds_client.c > index 9518ac8e407d..4becc9eada4b 100644 > --- a/fs/ceph/mds_client.c > +++ b/fs/ceph/mds_client.c > @@ -2551,8 +2551,8 @@ static u8 *get_fscrypt_altname(const struct ceph_mds_request *req, u32 *plen) > if (!IS_ENCRYPTED(dir)) > goto success; > > - ret = __fscrypt_prepare_readdir(dir); > - if (ret) > + ret = ceph_fscrypt_prepare_readdir(dir); > + if (ret < 0) > return ERR_PTR(ret); > > /* No key? Just ignore it. */ > @@ -2668,7 +2668,7 @@ char *ceph_mdsc_build_path(struct dentry *dentry, int *plen, u64 *pbase, int for > spin_unlock(&cur->d_lock); > parent = dget_parent(cur); > > - ret = __fscrypt_prepare_readdir(d_inode(parent)); > + ret = ceph_fscrypt_prepare_readdir(d_inode(parent)); > if (ret < 0) { > dput(parent); > dput(cur); >
Xiubo Li <xiubli@redhat.com> writes: > Hi Luis, > > BTW, which branch are you using to generate this patch ? > > Could you rebase to the testing branch ? Hmmm, I think I've used the wip-fscrypt branch. OK, I'll resend this after running a few tests just to make sure. Cheers,
diff --git a/fs/ceph/crypto.c b/fs/ceph/crypto.c index 35a2ccfe6899..5ef65a06af98 100644 --- a/fs/ceph/crypto.c +++ b/fs/ceph/crypto.c @@ -397,8 +397,8 @@ int ceph_fname_to_usr(const struct ceph_fname *fname, struct fscrypt_str *tname, goto out_inode; } - ret = __fscrypt_prepare_readdir(dir); - if (ret) + ret = ceph_fscrypt_prepare_readdir(dir); + if (ret < 0) goto out_inode; /* @@ -636,3 +636,34 @@ int ceph_fscrypt_encrypt_pages(struct inode *inode, struct page **page, u64 off, } return ret; } + +/** + * ceph_fscrypt_prepare_readdir - simple __fscrypt_prepare_readdir() wrapper + * @dir: directory inode for readdir prep + * + * Simple wrapper around __fscrypt_prepare_readdir() that will mark directory as + * non-complete if this call results in having the directory unlocked. + * + * Returns: + * 1 - if directory was locked and key is now loaded (i.e. dir is unlocked) + * 0 - if directory is still locked + * < 0 - if __fscrypt_prepare_readdir() fails + */ +int ceph_fscrypt_prepare_readdir(struct inode *dir) +{ + bool had_key = fscrypt_has_encryption_key(dir); + int err; + + if (!IS_ENCRYPTED(dir)) + return 0; + + err = __fscrypt_prepare_readdir(dir); + if (err) + return err; + if (!had_key && fscrypt_has_encryption_key(dir)) { + /* directory just got unlocked, mark it as not complete */ + ceph_dir_clear_complete(dir); + return 1; + } + return 0; +} diff --git a/fs/ceph/crypto.h b/fs/ceph/crypto.h index c6ee993f4ec8..cad53ec916fd 100644 --- a/fs/ceph/crypto.h +++ b/fs/ceph/crypto.h @@ -154,6 +154,7 @@ int ceph_fscrypt_decrypt_extents(struct inode *inode, struct page **page, u64 of struct ceph_sparse_extent *map, u32 ext_cnt); int ceph_fscrypt_encrypt_pages(struct inode *inode, struct page **page, u64 off, int len, gfp_t gfp); +int ceph_fscrypt_prepare_readdir(struct inode *dir); static inline struct page *ceph_fscrypt_pagecache_page(struct page *page) { @@ -254,6 +255,11 @@ static inline struct page *ceph_fscrypt_pagecache_page(struct page *page) { return page; } + +static inline int ceph_fscrypt_prepare_readdir(struct inode *dir) +{ + return 0; +} #endif /* CONFIG_FS_ENCRYPTION */ static inline loff_t ceph_fscrypt_page_offset(struct page *page) diff --git a/fs/ceph/dir.c b/fs/ceph/dir.c index edc2bf0aab83..5829f27d058d 100644 --- a/fs/ceph/dir.c +++ b/fs/ceph/dir.c @@ -343,8 +343,8 @@ static int ceph_readdir(struct file *file, struct dir_context *ctx) ctx->pos = 2; } - err = fscrypt_prepare_readdir(inode); - if (err) + err = ceph_fscrypt_prepare_readdir(inode); + if (err < 0) return err; spin_lock(&ci->i_ceph_lock); @@ -784,8 +784,8 @@ static struct dentry *ceph_lookup(struct inode *dir, struct dentry *dentry, return ERR_PTR(-ENAMETOOLONG); if (IS_ENCRYPTED(dir)) { - err = __fscrypt_prepare_readdir(dir); - if (err) + err = ceph_fscrypt_prepare_readdir(dir); + if (err < 0) return ERR_PTR(err); if (!fscrypt_has_encryption_key(dir)) { spin_lock(&dentry->d_lock); diff --git a/fs/ceph/mds_client.c b/fs/ceph/mds_client.c index 9518ac8e407d..4becc9eada4b 100644 --- a/fs/ceph/mds_client.c +++ b/fs/ceph/mds_client.c @@ -2551,8 +2551,8 @@ static u8 *get_fscrypt_altname(const struct ceph_mds_request *req, u32 *plen) if (!IS_ENCRYPTED(dir)) goto success; - ret = __fscrypt_prepare_readdir(dir); - if (ret) + ret = ceph_fscrypt_prepare_readdir(dir); + if (ret < 0) return ERR_PTR(ret); /* No key? Just ignore it. */ @@ -2668,7 +2668,7 @@ char *ceph_mdsc_build_path(struct dentry *dentry, int *plen, u64 *pbase, int for spin_unlock(&cur->d_lock); parent = dget_parent(cur); - ret = __fscrypt_prepare_readdir(d_inode(parent)); + ret = ceph_fscrypt_prepare_readdir(d_inode(parent)); if (ret < 0) { dput(parent); dput(cur);