Received: by 2002:a05:6902:102b:0:0:0:0 with SMTP id x11csp262117ybt; Tue, 23 Jun 2020 21:34:49 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwAEQz51kpitiY+sig7/XurTx3otq4WLvAE0N6Ts9bT5UOuZP5cBdI5dsxn0iEhXFIMlmC8 X-Received: by 2002:a17:906:eb4b:: with SMTP id mc11mr14101673ejb.5.1592973289816; Tue, 23 Jun 2020 21:34:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1592973289; cv=none; d=google.com; s=arc-20160816; b=cISZYaZFd6iGCA1l9hutQhWV4KITq7Uc+8LrK1eLy6f3E2g/WwoaxlaUOs/UxWU0oO gwGmJWR9iVfuXKWc3CcpG9o00TkAUzTD0IZbCzvC69lJVqIE91LWwXYm4DszC1gEyPWt +/YE4dMYHBeThCZVie1GNLVCw6newA0/mc56G4+e8644lzcn02OkEk3aY8IrVLiW7yRZ 1WOGQippqyHgH1YySY7B9TFV6br+aDXUKtO6UdoXXDXiq/H4uJeci2c9xtP/k8iQX9HN gQU82EM6qpYS20jfhWvQHzTPhNrrBvzMeHHR1glFD+KvBEj2rmn5V87Wp3ALU3e5SZOV 6dAg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:from:subject:references :mime-version:message-id:in-reply-to:date:dkim-signature; bh=QODIzVeJrdT1s8AIsgAZkNeVeFFuI7HH8hKtZUq7344=; b=MsnIpXSeCUVvSnXSto3KsdKFjKHQAcmrSNL5qYu2y1FdK+W0ph9R9gHazulHvUxa5p ZQBlUQYuXCv/iUXq7m4ofmHOWU5DpSD3D0FMRdjg9I6SsT5WPxFpR0LKHpf7KwbcUUfb 8s3jHeF6TWWfGFnrcc1E+VO99w5Fy3QBIVRfJJKl4bIu3Pk+g/DaPRhuCCqu7Tmv90Mw 6RPfNV50imSf6k8AWSszv9BmMIGGque7Zn/Ic2OlkmA1tWB6NsgKXjKin7MNkWfHg18C sT0FGx1RKC8TWkHIAjjQ+fJ0XeT4lLNPQssmuvN5vpWMawOc29yY3XxI/r2guSzjDEBH sspw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=hWHEcXJ9; spf=pass (google.com: domain of linux-ext4-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-ext4-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 dm17si12513110edb.312.2020.06.23.21.34.17; Tue, 23 Jun 2020 21:34:49 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-ext4-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=hWHEcXJ9; spf=pass (google.com: domain of linux-ext4-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-ext4-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 S2388823AbgFXEeG (ORCPT + 99 others); Wed, 24 Jun 2020 00:34:06 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43614 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728831AbgFXEeC (ORCPT ); Wed, 24 Jun 2020 00:34:02 -0400 Received: from mail-qv1-xf49.google.com (mail-qv1-xf49.google.com [IPv6:2607:f8b0:4864:20::f49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AD603C0613ED for ; Tue, 23 Jun 2020 21:34:00 -0700 (PDT) Received: by mail-qv1-xf49.google.com with SMTP id v20so828252qvt.15 for ; Tue, 23 Jun 2020 21:34:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=QODIzVeJrdT1s8AIsgAZkNeVeFFuI7HH8hKtZUq7344=; b=hWHEcXJ9aNrUXZyw33b3LmQOzvvcHuDW/A59nhjR5XQMLw5Qc925QSXhkERNImFcTW hANpGu19yKJ5+ofMIYmkF6TKYrS6Nz04N8XYUx1I3Pa4GgUquwMBJKxs29u86c7WgX2K RRe307Q7mfy+NjviZftXsu8kT999uG5LrB4rhhXsgo5tbdmLXVI5wJc18RWcgCYEfr1k ek2aoIIofzYBCRkoj8bTxC0G24p8GbTIhh9lWGZ9g6Nn5ntXhMQoIPDlw//uXtbDJSXC eLkDvnHk0IzkMvraG+Dsoo9PjzeSb8M7sIqjzEN4gF1fxbic3Daktg/Se4hGo4hYuOdt NXmA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=QODIzVeJrdT1s8AIsgAZkNeVeFFuI7HH8hKtZUq7344=; b=rkvbnEco1T9glo4TvuqSFv92MvaxS78cwH9X4eCaBQbZ2uMHB26R5oG8XYzESCIYPX 1rxcErVMsvAPXN3hZpBsLSWB3lQ/a796fogwGzzsJRa4aYG+deOnU88scpKKTlEZaXr/ 6P35zk6lR18ez67f/hlLrIyeMrtUCOTtJK52ZcRFk0W3Wd5Anqwaxxo3QOckZUbKgi0R FzddJs+sWoNNzfLZArpRxnlX85noy5VhYsn0G0ugVu2Yg+wey7pi3cHc22Z48P0+I15m Rm30FTDAsB3K/j13pIvFiGTxuvS0Ktw2W+tjYwG6Qx70IW6Q0hTi3qJK5sOMq6o6sHnK JKkg== X-Gm-Message-State: AOAM5325JlUUj4dTimW0UE3XhAjTPEvLdBteg5nr0O+HkCLBeVe7Uldc Hw+bhpYM4stxI9IGarIvMMuotvd2ZqA= X-Received: by 2002:ad4:5533:: with SMTP id ba19mr5259319qvb.110.1592973239889; Tue, 23 Jun 2020 21:33:59 -0700 (PDT) Date: Tue, 23 Jun 2020 21:33:39 -0700 In-Reply-To: <20200624043341.33364-1-drosen@google.com> Message-Id: <20200624043341.33364-3-drosen@google.com> Mime-Version: 1.0 References: <20200624043341.33364-1-drosen@google.com> X-Mailer: git-send-email 2.27.0.111.gc72c7da667-goog Subject: [PATCH v9 2/4] fs: Add standard casefolding support From: Daniel Rosenberg To: "Theodore Ts'o" , linux-ext4@vger.kernel.org, Jaegeuk Kim , Chao Yu , linux-f2fs-devel@lists.sourceforge.net, Eric Biggers , linux-fscrypt@vger.kernel.org, Alexander Viro , Richard Weinberger Cc: linux-mtd@lists.infradead.org, Andreas Dilger , Jonathan Corbet , linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, Gabriel Krisman Bertazi , kernel-team@android.com, Daniel Rosenberg Content-Type: text/plain; charset="UTF-8" Sender: linux-ext4-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-ext4@vger.kernel.org This adds general supporting functions for filesystems that use utf8 casefolding. It provides standard dentry_operations and adds the necessary structures in struct super_block to allow this standardization. Ext4 and F2fs will switch to these common implementations. Signed-off-by: Daniel Rosenberg --- fs/libfs.c | 101 +++++++++++++++++++++++++++++++++++++++++++++ include/linux/fs.h | 22 ++++++++++ 2 files changed, 123 insertions(+) diff --git a/fs/libfs.c b/fs/libfs.c index 4d08edf19c782..f7345a5ed562f 100644 --- a/fs/libfs.c +++ b/fs/libfs.c @@ -20,6 +20,8 @@ #include #include #include +#include +#include #include @@ -1363,3 +1365,102 @@ bool is_empty_dir_inode(struct inode *inode) return (inode->i_fop == &empty_dir_operations) && (inode->i_op == &empty_dir_inode_operations); } + +#ifdef CONFIG_UNICODE +/** + * needs_casefold - generic helper to determine if a filename should be casefolded + * @dir: Parent directory + * + * Generic helper for filesystems to use to determine if the name of a dentry + * should be casefolded. It does not make sense to casefold the no-key token of + * an encrypted filename. + * + * Return: if names will need casefolding + */ +bool needs_casefold(const struct inode *dir) +{ + return IS_CASEFOLDED(dir) && dir->i_sb->s_encoding && + (!IS_ENCRYPTED(dir) || fscrypt_has_encryption_key(dir)); +} +EXPORT_SYMBOL(needs_casefold); + +/** + * generic_ci_d_compare - generic d_compare implementation for casefolding filesystems + * @dentry: dentry whose name we are checking against + * @len: len of name of dentry + * @str: str pointer to name of dentry + * @name: Name to compare against + * + * Return: 0 if names match, 1 if mismatch, or -ERRNO + */ +int generic_ci_d_compare(const struct dentry *dentry, unsigned int len, + const char *str, const struct qstr *name) +{ + const struct dentry *parent = READ_ONCE(dentry->d_parent); + const struct inode *inode = READ_ONCE(parent->d_inode); + const struct super_block *sb = dentry->d_sb; + const struct unicode_map *um = sb->s_encoding; + struct qstr qstr = QSTR_INIT(str, len); + char strbuf[DNAME_INLINE_LEN]; + int ret; + + if (!inode || !needs_casefold(inode)) + goto fallback; + /* + * If the dentry name is stored in-line, then it may be concurrently + * modified by a rename. If this happens, the VFS will eventually retry + * the lookup, so it doesn't matter what ->d_compare() returns. + * However, it's unsafe to call utf8_strncasecmp() with an unstable + * string. Therefore, we have to copy the name into a temporary buffer. + */ + if (len <= DNAME_INLINE_LEN - 1) { + memcpy(strbuf, str, len); + strbuf[len] = 0; + qstr.name = strbuf; + /* prevent compiler from optimizing out the temporary buffer */ + barrier(); + } + ret = utf8_strncasecmp(um, name, &qstr); + if (ret >= 0) + return ret; + + if (sb_has_enc_strict_mode(sb)) + return -EINVAL; +fallback: + if (len != name->len) + return 1; + return !!memcmp(str, name->name, len); +} +EXPORT_SYMBOL(generic_ci_d_compare); + +/** + * generic_ci_d_hash - generic d_hash implementation for casefolding filesystems + * @dentry: dentry whose name we are hashing + * @str: qstr of name whose hash we should fill in + * + * Return: 0 if hash was successful, or -ERRNO + */ +int generic_ci_d_hash(const struct dentry *dentry, struct qstr *str) +{ + const struct inode *inode = READ_ONCE(dentry->d_inode); + struct super_block *sb = dentry->d_sb; + const struct unicode_map *um = sb->s_encoding; + int ret = 0; + + if (!inode || !needs_casefold(inode)) + return 0; + + ret = utf8_casefold_hash(um, dentry, str); + if (ret < 0) + goto err; + + return 0; +err: + if (sb_has_enc_strict_mode(sb)) + ret = -EINVAL; + else + ret = 0; + return ret; +} +EXPORT_SYMBOL(generic_ci_d_hash); +#endif diff --git a/include/linux/fs.h b/include/linux/fs.h index 3f881a892ea74..261904e06873b 100644 --- a/include/linux/fs.h +++ b/include/linux/fs.h @@ -1392,6 +1392,12 @@ extern int send_sigurg(struct fown_struct *fown); #define SB_ACTIVE (1<<30) #define SB_NOUSER (1<<31) +/* These flags relate to encoding and casefolding */ +#define SB_ENC_STRICT_MODE_FL (1 << 0) + +#define sb_has_enc_strict_mode(sb) \ + (sb->s_encoding_flags & SB_ENC_STRICT_MODE_FL) + /* * Umount options */ @@ -1461,6 +1467,10 @@ struct super_block { #endif #ifdef CONFIG_FS_VERITY const struct fsverity_operations *s_vop; +#endif +#ifdef CONFIG_UNICODE + struct unicode_map *s_encoding; + __u16 s_encoding_flags; #endif struct hlist_bl_head s_roots; /* alternate root dentries for NFS */ struct list_head s_mounts; /* list of mounts; _not_ for fs use */ @@ -3385,6 +3395,18 @@ extern int generic_file_fsync(struct file *, loff_t, loff_t, int); extern int generic_check_addressable(unsigned, u64); +#ifdef CONFIG_UNICODE +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); +extern bool needs_casefold(const struct inode *dir); +#else +static inline bool needs_casefold(const struct inode *dir) +{ + return 0; +} +#endif + #ifdef CONFIG_MIGRATION extern int buffer_migrate_page(struct address_space *, struct page *, struct page *, -- 2.27.0.111.gc72c7da667-goog