Received: by 2002:a05:6902:102b:0:0:0:0 with SMTP id x11csp966184ybt; Tue, 7 Jul 2020 04:46:55 -0700 (PDT) X-Google-Smtp-Source: ABdhPJx/VqLpHUgC3X/xKKdpWryQX/TOxEh5qdzWycYBrHuUPMtZpUFXV6xBQKzBAsIVCr/rCKCk X-Received: by 2002:a05:6402:1c8f:: with SMTP id cy15mr61303865edb.308.1594122414945; Tue, 07 Jul 2020 04:46:54 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1594122414; cv=none; d=google.com; s=arc-20160816; b=sDtnGr5SAOJQ734qy2esU0dL+hzEUGvLvUn3TsFfavUHot+LP3HjPNXv+6ScDjVoJn YDSGAz+dT1zDHAtX2K2CeejuXHKongzIs5XIGtqmWioYFeC71akpd0fkguEdFbQPrGWi YEPuYwLopJepGU3rTPq5y0J5UPmaAb3kbuDIYf0D2Rlj8yUIneTNIZBttWNQGY267Jvp nR8G3YQQ/RJxCW3XKjDwqq7sfuYN4wnEukQntTbHoowY7KVsuzIJd3kAJeH6kVBpAsg9 fVOZXQVzxB21ZAM+DYLjrREyHLsO38oPwiAFOAk6U1BXPXaTOx1rBEEsi5Edosx+zdcs lGUw== 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=d6FGoIGfQ0jy4H4DPDBQTUQ6jcb24MsxrcjNcngUmx4=; b=qgycvWYtUjO4FqvLTcSRXSuFn9ata6FTzRPSj/ViRo95kkACIodYnzZoIADqnE9qI6 F1c6kyzd4JIAccCjUB9YPjr4Ox03JJ4RjszJ+40JRdTPlSi+f2POP9bbxuVKOQsenH4k SrkFnoMAO2VxSUka8FguxH8bSmrVWT7ABlwbUSi1aII87OLZJGx7e17cay5i5NlGaVOW xEhuMXbxR5CwPZFUrwC7+gBzBSq3kvbwYjK9mQ8rFq/N5aCaCZ1foMDZ2M/z3BlwZn41 to+OSN78oCv4W2g543N67nZMqiidyN7ujG9GgZXs7L00N4T4r18A6VL4KamiNA/fYbKl dhmw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=u6XnDnby; 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 l14si15065853edq.355.2020.07.07.04.46.31; Tue, 07 Jul 2020 04:46:54 -0700 (PDT) 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=u6XnDnby; 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 S1728437AbgGGLpc (ORCPT + 99 others); Tue, 7 Jul 2020 07:45:32 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55078 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728440AbgGGLpQ (ORCPT ); Tue, 7 Jul 2020 07:45:16 -0400 Received: from mail-pj1-x1049.google.com (mail-pj1-x1049.google.com [IPv6:2607:f8b0:4864:20::1049]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E0EADC08C5E1 for ; Tue, 7 Jul 2020 04:45:15 -0700 (PDT) Received: by mail-pj1-x1049.google.com with SMTP id k7so24833733pjw.2 for ; Tue, 07 Jul 2020 04:45:15 -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=d6FGoIGfQ0jy4H4DPDBQTUQ6jcb24MsxrcjNcngUmx4=; b=u6XnDnbyIXd6p+RTjT7PJsDAOLaFGoxAZCIKkA3ptT0YRj71raFfPE3xOVU7qJqc9a u3AN5RlPdMR7YqEqh1M683omsV8sz3KhikKAe/aUo1Al5jH1Z7NzlIY2VVfi/XOGnBZO Tc36idYsDCq5/fyMInzISFw7loVgcE/KUBFeWnQmLZ9OK0k4ZX7+M8czsVamO7ERAX9O cDvGo7gLjopuHh7Zh61xmB0in2dD0issAJ5hCSHaiu/W2HM4jZYH8yi/nrsxw7pyXKpB ecJGK7HOsQ/oZfoD7neeU/GsHGWaiQrOAv0IYoF+VeEaobz1BZX/pJkm6ZCyaMfBY2Z1 C30g== 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=d6FGoIGfQ0jy4H4DPDBQTUQ6jcb24MsxrcjNcngUmx4=; b=gCLAM6RRcTcDSqysfHt1sx9gK39e87abAeJ+ypXnxIPV6xDpDAMaRxeq0/No8ey+n/ FL7T7n6mSyctyrJRqwCfv20y4TrvNQJ5vkI2dzauUW/IQ3y96DNx2gUmVXKL4ZjmjqWa NiN7ODmjDIboqhWCRiq1DFYlS0t7hoNmOrJ2WsOisJdIcZI03Oj9UJ/OmetG+uoDjarD 6dV1rxpEBq1KNclPPyAOun1XdMls2vJFOz6oe8yflUG9aVS92+bnjbjJVg555Z2Z/dFn ewYYMiMlc4NojU/BAsEoaujDGDSdsPukYu4pJpPqgmLmrE01HgtuELfgU/NcY3ntsaq4 heZg== X-Gm-Message-State: AOAM533fH4CIulpClfsKbCgRdLmU6QyYgeCO+EhFM+jEHVLpKXNLngRV ep9rq1tyTumTczK0Yl++gGikE0u2Vac= X-Received: by 2002:a62:8307:: with SMTP id h7mr34550200pfe.149.1594122315288; Tue, 07 Jul 2020 04:45:15 -0700 (PDT) Date: Tue, 7 Jul 2020 04:31:21 -0700 In-Reply-To: <20200707113123.3429337-1-drosen@google.com> Message-Id: <20200707113123.3429337-3-drosen@google.com> Mime-Version: 1.0 References: <20200707113123.3429337-1-drosen@google.com> X-Mailer: git-send-email 2.27.0.212.ge8ba1cc988-goog Subject: [PATCH v10 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 Cc: Andreas Dilger , 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-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@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. The new dentry operations are functionally equivalent to the existing operations in ext4 and f2fs, apart from ths use of utf8_casefold_hash to avoid an allocation, and dealing with casefolding and encryption. It doesn't make sense to casefold the no-key token for the encrypted name, so we don't casefold in that case. By providing a common implementation, all users can benefit from any optimizations without needing to port over improvements. Signed-off-by: Daniel Rosenberg --- fs/libfs.c | 96 ++++++++++++++++++++++++++++++++++++++++++++++ include/linux/fs.h | 16 ++++++++ 2 files changed, 112 insertions(+) diff --git a/fs/libfs.c b/fs/libfs.c index 4d08edf19c78..f7df13806f36 100644 --- a/fs/libfs.c +++ b/fs/libfs.c @@ -20,6 +20,8 @@ #include #include #include +#include +#include #include @@ -1363,3 +1365,97 @@ 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 +/* + * 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 + */ +static bool needs_casefold(const struct inode *dir, const struct dentry *dentry) +{ + return IS_CASEFOLDED(dir) && dir->i_sb->s_encoding && + !(dentry->d_flags & DCACHE_ENCRYPTED_NAME); +} + +/** + * 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, dentry)) + 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_strict_encoding(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, dentry)) + return 0; + + ret = utf8_casefold_hash(um, dentry, str); + if (ret < 0) + goto err; + + return 0; +err: + if (sb_has_strict_encoding(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 3f881a892ea7..af8f2ecec8ff 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_strict_encoding(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,12 @@ 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); +#endif + #ifdef CONFIG_MIGRATION extern int buffer_migrate_page(struct address_space *, struct page *, struct page *, -- 2.27.0.212.ge8ba1cc988-goog