Received: by 2002:a05:6a10:16a7:0:0:0:0 with SMTP id gp39csp218647pxb; Wed, 18 Nov 2020 22:12:02 -0800 (PST) X-Google-Smtp-Source: ABdhPJwaiDQezSjgwC/KJ30FLiueL5qUbfJ4n4X9EPp6esTxFhKc5GWX9XMmqEGkf1Qybf/aXW9P X-Received: by 2002:a17:906:cc4c:: with SMTP id mm12mr2066074ejb.47.1605766322299; Wed, 18 Nov 2020 22:12:02 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1605766322; cv=none; d=google.com; s=arc-20160816; b=D2a9KI1sGS0BJ0sZ8QqGeLTBJUw7GRdS/L1phQ0OES3kqvWLVaLrEKvVQesi37NSCD dZR/8VbJZEnUZCuPFe9Br2aVpU5Jc9bupa3h84UUki/en8Xoy5p7eNDmTXwY+P5Uuchx HWnP7h/eSlwHCEqFIq7WjsFrJzsd3HHke3N6zLZXAQv24Q4k1vfuC45AVyTuXvZk0HvZ 5nrYPQLrl+7aCmXiAb/aQrt61xhcElp071bvzOvXDNdtlOY64gc0VosYPnuSwf8eU1ot qxY8b19p2NKjVwPS1SBFfzsaCZ01uTNErqsZdwYMP+CK/g1hO/h7/hCc+RTDOA+OJtjw ltMQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:references:mime-version :message-id:in-reply-to:date:sender:dkim-signature; bh=pqAPKMbmNsV52HR4YSyUKpkLgaKSoP3ZrEZXGq4Xu/0=; b=MU0IIa7Lulga2fSn/Ser7LQYPyq5SDcpBZkcL6ep0GGRrT58fqkg7RxK9Jjj/fN2CP 8vR20Zc+wuiMg8+9buz3V/MffeORFImvLcn4XWRhV7wp5HQbo0S8NMmsKChYZy8hZCTa PuWP18m1M1CdPdXoNsXkGmrTuRaCuN642tJ2X9zxUogIVFt4MO8tcTrLA/6yEB1Z7jRw 2kZI+o5Gi1TiATZstp2HANBfDcTRdJQuDWSJiQqwyuJdSVhVEl5PifqB+OV0U1wq6/rX pnoX3K5/+VjWDzNguTSX/eKPdPBdeiBeHUUwAoXXp9gDo41nsHxCOiEodnzyOkJMjJg9 DNwA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=UdP4FsE2; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id d12si4870691edr.594.2020.11.18.22.11.39; Wed, 18 Nov 2020 22:12:02 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=UdP4FsE2; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726295AbgKSGJM (ORCPT + 99 others); Thu, 19 Nov 2020 01:09:12 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43116 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726235AbgKSGJK (ORCPT ); Thu, 19 Nov 2020 01:09:10 -0500 Received: from mail-qk1-x749.google.com (mail-qk1-x749.google.com [IPv6:2607:f8b0:4864:20::749]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1F606C061A04 for ; Wed, 18 Nov 2020 22:09:10 -0800 (PST) Received: by mail-qk1-x749.google.com with SMTP id o25so4291305qkj.1 for ; Wed, 18 Nov 2020 22:09:10 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=sender:date:in-reply-to:message-id:mime-version:references:subject :from:to:cc; bh=pqAPKMbmNsV52HR4YSyUKpkLgaKSoP3ZrEZXGq4Xu/0=; b=UdP4FsE2zGsI55Kgb/EznglwdoUlp7MKg1jEtYwlhVFENpfj/8/I9ohE9BAL2ok5SW 7LgqtKDD3wIFaZhPkypkIZridSnJI2/sFOORkqVJhHxiDVFZfQYtobc0z/NeGKeNjp7a 8Ywz9XEZBJpIHnJQetyCPYSVPN6b4Ue7u6CSNyVzNotQA5MaDINRMdi3n2uijg1PWolD 7KTinqSAcCMZE+PPmmm4zGpXuTr+BC5rbOh1izgdk3cRqU7hpK0JuOKTt0gS/3RP6t8p fNXDYX0i3U/yDTdywJCCUDm6eZHZgOToG1BPc8KI7hooQ3KATbgwTbzLOVBLV1OX6kOg LZYQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=pqAPKMbmNsV52HR4YSyUKpkLgaKSoP3ZrEZXGq4Xu/0=; b=g6ob2Yvh2QckPzGqh8+Zjt9MzlfcG+Ja8YLzjp4wB78L52+Gx8LeuKfojxpabywy22 PbAVpNbhevgKxSRRRgQxiZH0/ZsTGyXO52MFYy1cWkiFj4VVeSvAzcqOHnb6cjyaK/jI a4F7Q/cQ5WXK1HVQvMlbMStFpT1hMBwaH1ESqHNL59nBKfmbl7JavVknJWAuiZqKLRGM HEhhv0XmxD6ccqyk3e3llb6bxIHDCyfTnvgPcmtFWblIkOrBL9gssup9F8m8VLC6ihDj 2+brVdCS7LXibTJSYlKl3tT9H4jK2AnivX5yEZbHu07PVMHEegpF1sjKAZhqU4iFZggG 7fCg== X-Gm-Message-State: AOAM531ygSRE5x6xg9+t8NJMcc/KbLYbTpqZcOC+vNHHltABeqBLDejM Y9Ed1qjF8NNk+sGrNaH8uxiM/yVBmQ8= Sender: "drosen via sendgmr" X-Received: from drosen.c.googlers.com ([fda3:e722:ac3:10:24:72f4:c0a8:4e6f]) (user=drosen job=sendgmr) by 2002:a0c:a681:: with SMTP id t1mr9304343qva.16.1605766149235; Wed, 18 Nov 2020 22:09:09 -0800 (PST) Date: Thu, 19 Nov 2020 06:09:02 +0000 In-Reply-To: <20201119060904.463807-1-drosen@google.com> Message-Id: <20201119060904.463807-2-drosen@google.com> Mime-Version: 1.0 References: <20201119060904.463807-1-drosen@google.com> X-Mailer: git-send-email 2.29.2.454.gaff20da3a2-goog Subject: [PATCH v4 1/3] libfs: Add generic function for setting dentry_ops From: Daniel Rosenberg To: "Theodore Y . Ts'o" , Jaegeuk Kim , Eric Biggers , Andreas Dilger , Chao Yu , Alexander Viro , Richard Weinberger , linux-fscrypt@vger.kernel.org, linux-ext4@vger.kernel.org, linux-f2fs-devel@lists.sourceforge.net Cc: linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mtd@lists.infradead.org, Gabriel Krisman Bertazi , kernel-team@android.com, Daniel Rosenberg , Eric Biggers Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This adds a function to set dentry operations at lookup time that will work for both encrypted filenames and casefolded filenames. A filesystem that supports both features simultaneously can use this function during lookup preparations to set up its dentry operations once fscrypt no longer does that itself. Currently the casefolding dentry operation are always set if the filesystem defines an encoding because the features is toggleable on empty directories. Unlike in the encryption case, the dentry operations used come from the parent. Since we don't know what set of functions we'll eventually need, and cannot change them later, we enable the casefolding operations if the filesystem supports them at all. By splitting out the various cases, we support as few dentry operations as we can get away with, maximizing compatibility with overlayfs, which will not function if a filesystem supports certain dentry_operations. Signed-off-by: Daniel Rosenberg Reviewed-by: Eric Biggers --- fs/libfs.c | 70 ++++++++++++++++++++++++++++++++++++++++++++++ include/linux/fs.h | 1 + 2 files changed, 71 insertions(+) diff --git a/fs/libfs.c b/fs/libfs.c index fc34361c1489..bac918699022 100644 --- a/fs/libfs.c +++ b/fs/libfs.c @@ -1449,4 +1449,74 @@ int generic_ci_d_hash(const struct dentry *dentry, struct qstr *str) return 0; } EXPORT_SYMBOL(generic_ci_d_hash); + +static const struct dentry_operations generic_ci_dentry_ops = { + .d_hash = generic_ci_d_hash, + .d_compare = generic_ci_d_compare, +}; +#endif + +#ifdef CONFIG_FS_ENCRYPTION +static const struct dentry_operations generic_encrypted_dentry_ops = { + .d_revalidate = fscrypt_d_revalidate, +}; +#endif + +#if defined(CONFIG_FS_ENCRYPTION) && defined(CONFIG_UNICODE) +static const struct dentry_operations generic_encrypted_ci_dentry_ops = { + .d_hash = generic_ci_d_hash, + .d_compare = generic_ci_d_compare, + .d_revalidate = fscrypt_d_revalidate, +}; +#endif + +/** + * generic_set_encrypted_ci_d_ops - helper for setting d_ops for given dentry + * @dentry: dentry to set ops on + * + * Casefolded directories need d_hash and d_compare set, so that the dentries + * contained in them are handled case-insensitively. Note that these operations + * are needed on the parent directory rather than on the dentries in it, and + * while the casefolding flag can be toggled on and off on an empty directory, + * dentry_operations can't be changed later. As a result, if the filesystem has + * casefolding support enabled at all, we have to give all dentries the + * casefolding operations even if their inode doesn't have the casefolding flag + * currently (and thus the casefolding ops would be no-ops for now). + * + * Encryption works differently in that the only dentry operation it needs is + * d_revalidate, which it only needs on dentries that have the no-key name flag. + * The no-key flag can't be set "later", so we don't have to worry about that. + * + * Finally, to maximize compatibility with overlayfs (which isn't compatible + * with certain dentry operations) and to avoid taking an unnecessary + * performance hit, we use custom dentry_operations for each possible + * combination rather than always installing all operations. + */ +void generic_set_encrypted_ci_d_ops(struct dentry *dentry) +{ +#ifdef CONFIG_FS_ENCRYPTION + bool needs_encrypt_ops = dentry->d_flags & DCACHE_NOKEY_NAME; +#endif +#ifdef CONFIG_UNICODE + bool needs_ci_ops = dentry->d_sb->s_encoding; +#endif +#if defined(CONFIG_FS_ENCRYPTION) && defined(CONFIG_UNICODE) + if (needs_encrypt_ops && needs_ci_ops) { + d_set_d_op(dentry, &generic_encrypted_ci_dentry_ops); + return; + } #endif +#ifdef CONFIG_FS_ENCRYPTION + if (needs_encrypt_ops) { + d_set_d_op(dentry, &generic_encrypted_dentry_ops); + return; + } +#endif +#ifdef CONFIG_UNICODE + if (needs_ci_ops) { + d_set_d_op(dentry, &generic_ci_dentry_ops); + return; + } +#endif +} +EXPORT_SYMBOL(generic_set_encrypted_ci_d_ops); diff --git a/include/linux/fs.h b/include/linux/fs.h index 8667d0cdc71e..11345e66353b 100644 --- a/include/linux/fs.h +++ b/include/linux/fs.h @@ -3202,6 +3202,7 @@ extern int generic_ci_d_hash(const struct dentry *dentry, struct qstr *str); extern int generic_ci_d_compare(const struct dentry *dentry, unsigned int len, const char *str, const struct qstr *name); #endif +extern void generic_set_encrypted_ci_d_ops(struct dentry *dentry); #ifdef CONFIG_MIGRATION extern int buffer_migrate_page(struct address_space *, -- 2.29.2.454.gaff20da3a2-goog