From patchwork Tue Oct 23 21:20:05 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Phillip Potter X-Patchwork-Id: 10653685 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id A5F1113B5 for ; Tue, 23 Oct 2018 21:20:18 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 81E9129CB8 for ; Tue, 23 Oct 2018 21:20:14 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 70CB02A387; Tue, 23 Oct 2018 21:20:14 +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=-7.9 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI autolearn=ham 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 06C6029CB8 for ; Tue, 23 Oct 2018 21:20:13 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728699AbeJXFpT (ORCPT ); Wed, 24 Oct 2018 01:45:19 -0400 Received: from mail-wm1-f65.google.com ([209.85.128.65]:35289 "EHLO mail-wm1-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725266AbeJXFpR (ORCPT ); Wed, 24 Oct 2018 01:45:17 -0400 Received: by mail-wm1-f65.google.com with SMTP id w186-v6so3248150wmf.0 for ; Tue, 23 Oct 2018 14:20:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=philpotter-co-uk.20150623.gappssmtp.com; s=20150623; h=date:from:to:cc:subject:message-id:mime-version:content-disposition :user-agent; bh=jdd0D8AR1ApPlV0fOV572ah65ZvPkOSB5huK6z0QU0s=; b=JvAjZcNskoH3MGQFq7KxqX9Pb8V6RmE+HtvftWEDoKNjwvz82fLr9353EYRp/L4o8o J4Y3m+S5uPfy9Nre7t6IatK3AYQkf5n+iXH+bNB6t21R5Ufwi8QIcXpAL7u1p/ZuH13Q 1p/GGEhZ4hcZsN4fSpeZMcxCudTz5zM43RWo4S0FENC7HX+9h1QFjVXqmV2FuTJzFnOb 01bVQ9mgsL5MI65Mz4iQkGnonCRGUPASc4aTBFunqifFk8cO33n6YB6h4QoItdeydw2I Uc+gNbqw2R4AkKvEHpI5UXd6hQJDl8KabcWlC9fR/o68GpTMhFSNcfRpPKtwa+hOO8V3 wtsg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:mime-version :content-disposition:user-agent; bh=jdd0D8AR1ApPlV0fOV572ah65ZvPkOSB5huK6z0QU0s=; b=kUANUJSwQ4hGq0M/R6pqAVXi5OONlde8pA9eNQYfCVQiXVVY7E71zSj6TbPlBC1BUJ 32Bcn5MMF65G+TRRW06fysezXLRBWSBUGfmWmZqK6EgPnLPb7nURkfuvsGcpCa3uk27U qBngG40nKnObySHbT0sPTtzYn17qFvkpTcl7HIMDHKDzIo30x9vXrngxPpwtOQMy+zkr GwJ6hopl9wSzlXGEDyR51qMXPiK78Qme71qRUB6QjIx6RDo6IzNDKsO6RmrDmR06Awx2 6cKqEcF42sAO092Nf9cqY2wu/h7zy3O0gJM/pntyX92rKicYFX7QrptWJLF7Rskg1iHC pb+Q== X-Gm-Message-State: ABuFfoi8TQFOU5lmKE+kWF/kiKnSAW5AB2KsNViQlXV42EZXR1VTuKxf E2Rk6KMQjPJM3/U7BmpCybJeVcRDLmmPaEaq X-Google-Smtp-Source: ACcGV61CSsReMTCgd9mQqZT8G2UL5SJew8gHMCNWF30VFc1w141A+XHmS7ZlVd4nKvCa6QHbyD6oDQ== X-Received: by 2002:a1c:5e49:: with SMTP id s70-v6mr21544193wmb.131.1540329606955; Tue, 23 Oct 2018 14:20:06 -0700 (PDT) Received: from pathfinder (62-64-249-96.dynamic.dsl.as9105.com. [62.64.249.96]) by smtp.gmail.com with ESMTPSA id x8-v6sm5000735wrd.54.2018.10.23.14.20.06 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Tue, 23 Oct 2018 14:20:06 -0700 (PDT) Date: Tue, 23 Oct 2018 22:20:05 +0100 From: Phillip Potter To: jack@suse.com Cc: linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, amir73il@gmail.com, viro@zeniv.linux.org.uk, linux-ext4@vger.kernel.org Subject: [RFC][PATCH v2 04/10] ext2: use common file type conversion Message-ID: <20181023212005.GA16631@pathfinder> MIME-Version: 1.0 Content-Disposition: inline User-Agent: Mutt/1.10.1 (2018-07-13) Sender: linux-fsdevel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Deduplicate the ext2 file type conversion implementation. Original patch by Amir Goldstein. v2: - Rebased against Linux 4.19 by Phillip Potter - This version does not remove EXT2_FT_x enum from fs/ext2/ext2.h, as these values are now used in compile-time checks added by Phillip Potter to make sure they remain the same as FT_x values v1: - Initial implementation Signed-off-by: Phillip Potter --- fs/ext2/dir.c | 51 ++++++++++++++++++++++----------------------------- 1 file changed, 22 insertions(+), 29 deletions(-) diff --git a/fs/ext2/dir.c b/fs/ext2/dir.c index 3b8114def693..420d4b9e8980 100644 --- a/fs/ext2/dir.c +++ b/fs/ext2/dir.c @@ -252,33 +252,10 @@ ext2_validate_entry(char *base, unsigned offset, unsigned mask) return (char *)p - base; } -static unsigned char ext2_filetype_table[EXT2_FT_MAX] = { - [EXT2_FT_UNKNOWN] = DT_UNKNOWN, - [EXT2_FT_REG_FILE] = DT_REG, - [EXT2_FT_DIR] = DT_DIR, - [EXT2_FT_CHRDEV] = DT_CHR, - [EXT2_FT_BLKDEV] = DT_BLK, - [EXT2_FT_FIFO] = DT_FIFO, - [EXT2_FT_SOCK] = DT_SOCK, - [EXT2_FT_SYMLINK] = DT_LNK, -}; - -#define S_SHIFT 12 -static unsigned char ext2_type_by_mode[S_IFMT >> S_SHIFT] = { - [S_IFREG >> S_SHIFT] = EXT2_FT_REG_FILE, - [S_IFDIR >> S_SHIFT] = EXT2_FT_DIR, - [S_IFCHR >> S_SHIFT] = EXT2_FT_CHRDEV, - [S_IFBLK >> S_SHIFT] = EXT2_FT_BLKDEV, - [S_IFIFO >> S_SHIFT] = EXT2_FT_FIFO, - [S_IFSOCK >> S_SHIFT] = EXT2_FT_SOCK, - [S_IFLNK >> S_SHIFT] = EXT2_FT_SYMLINK, -}; - static inline void ext2_set_de_type(ext2_dirent *de, struct inode *inode) { - umode_t mode = inode->i_mode; if (EXT2_HAS_INCOMPAT_FEATURE(inode->i_sb, EXT2_FEATURE_INCOMPAT_FILETYPE)) - de->file_type = ext2_type_by_mode[(mode & S_IFMT)>>S_SHIFT]; + de->file_type = fs_umode_to_ftype(inode->i_mode); else de->file_type = 0; } @@ -293,14 +270,14 @@ ext2_readdir(struct file *file, struct dir_context *ctx) unsigned long n = pos >> PAGE_SHIFT; unsigned long npages = dir_pages(inode); unsigned chunk_mask = ~(ext2_chunk_size(inode)-1); - unsigned char *types = NULL; bool need_revalidate = !inode_eq_iversion(inode, file->f_version); + bool has_filetype; if (pos > inode->i_size - EXT2_DIR_REC_LEN(1)) return 0; - if (EXT2_HAS_INCOMPAT_FEATURE(sb, EXT2_FEATURE_INCOMPAT_FILETYPE)) - types = ext2_filetype_table; + has_filetype = + EXT2_HAS_INCOMPAT_FEATURE(sb, EXT2_FEATURE_INCOMPAT_FILETYPE); for ( ; n < npages; n++, offset = 0) { char *kaddr, *limit; @@ -335,8 +312,24 @@ ext2_readdir(struct file *file, struct dir_context *ctx) if (de->inode) { unsigned char d_type = DT_UNKNOWN; - if (types && de->file_type < EXT2_FT_MAX) - d_type = types[de->file_type]; + /* + * compile-time asserts that generic FT_x types + * still match EXT2_FT_x types - no need to list + * for other functions as well as build will + * fail either way + */ + BUILD_BUG_ON(EXT2_FT_UNKNOWN != FT_UNKNOWN); + BUILD_BUG_ON(EXT2_FT_REG_FILE != FT_REG_FILE); + BUILD_BUG_ON(EXT2_FT_DIR != FT_DIR); + BUILD_BUG_ON(EXT2_FT_CHRDEV != FT_CHRDEV); + BUILD_BUG_ON(EXT2_FT_BLKDEV != FT_BLKDEV); + BUILD_BUG_ON(EXT2_FT_FIFO != FT_FIFO); + BUILD_BUG_ON(EXT2_FT_SOCK != FT_SOCK); + BUILD_BUG_ON(EXT2_FT_SYMLINK != FT_SYMLINK); + BUILD_BUG_ON(EXT2_FT_MAX != FT_MAX); + + if (has_filetype) + d_type = fs_dtype(de->file_type); if (!dir_emit(ctx, de->name, de->name_len, le32_to_cpu(de->inode), From patchwork Tue Oct 23 21:17:28 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Phillip Potter X-Patchwork-Id: 10653683 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 5C6D61709 for ; Tue, 23 Oct 2018 21:17:38 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 442082A4FD for ; Tue, 23 Oct 2018 21:17:38 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 37CB32A503; Tue, 23 Oct 2018 21:17:38 +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=-7.9 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI 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 B6C182A500 for ; Tue, 23 Oct 2018 21:17:37 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726913AbeJXFmk (ORCPT ); Wed, 24 Oct 2018 01:42:40 -0400 Received: from mail-wr1-f65.google.com ([209.85.221.65]:41258 "EHLO mail-wr1-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725859AbeJXFmk (ORCPT ); Wed, 24 Oct 2018 01:42:40 -0400 Received: by mail-wr1-f65.google.com with SMTP id q7-v6so3225988wrr.8 for ; Tue, 23 Oct 2018 14:17:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=philpotter-co-uk.20150623.gappssmtp.com; s=20150623; h=date:from:to:cc:subject:message-id:mime-version:content-disposition :user-agent; bh=VnD+CLavQLnq9oG7intxx34ZowLx+fsbcrRX+5LIf+I=; b=bxuJlNMEGjdkKTmh4lJdvFQ+j/muy8n8crXTE4zbACMsbdUmd9HIEtoDYn5OTMYW7R edkejjHUxc4IQjntSBZxkRqNG+2zT2J9iYCx9Upsgpoafxiwyvo+PD2h2Sj9kgmJqems jb8bNx8JvAseqyoYLeRBdLgGnHJ+pWFq7ghMwPcSUB7lvZX3LofShdB1YlRi/Qvb2TSV o4QP4YJjV/eQJgCJguoLaYZBdQjqFKYhABb7usrbUbMMqybhEb8peOz+9n4EQ1th24TS XjrLcRlPN7q774kN+G7OkdN0WT+stZbS/2ncI99yXMcUc86XnlWLxZjrlLfVElucgxIM Jf5g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:mime-version :content-disposition:user-agent; bh=VnD+CLavQLnq9oG7intxx34ZowLx+fsbcrRX+5LIf+I=; b=jwJ/Li1ZmJzBQEqrkVHKQDs5TpaQz0fBbJa2aAXfUXFKEhqkV63ZbAMlLP5zqH3Fc2 5Zscn82804nUx8DLMCq0bwhPo5MRAA51ssdEt1PVjPYY+opUDtdPuicQv7ROEnGSLhpZ fi0scOT0OvNHgIPijOFXGiKXPZH1IO+GYK7+ufpQlGG/qWp1c1GnbE28YBkNCuFFpfmk CKWYeXHsBLUEXq0XV0HDeCSXcHm304GwgteG9DC2UfWO3fte0GuxLIz2btcRk26ZI0F3 nWz/6OFLccJaLyqAliJD+2d9n7yziPw65vs9NtSv1tWtAp+tw/uXfN9gbv5XE3dW17Bs gYFQ== X-Gm-Message-State: AGRZ1gJ4UYcddunBEtSu1yI/HXkOtb5VY03N+tRdUADOmx6xYahzuEUX AlzDnGmAHU863V3YrdVtYSfFbg== X-Google-Smtp-Source: AJdET5dhFgrLz5h1q/d6A6wrEH/Mp0I9P41YR+355HRaY223VeGT+xZfnD0DxMGjbK2BbkYwGiI/7Q== X-Received: by 2002:a5d:6b4f:: with SMTP id x15-v6mr23038176wrw.304.1540329450688; Tue, 23 Oct 2018 14:17:30 -0700 (PDT) Received: from pathfinder (62-64-249-96.dynamic.dsl.as9105.com. [62.64.249.96]) by smtp.gmail.com with ESMTPSA id j203-v6sm4380200wmd.46.2018.10.23.14.17.29 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Tue, 23 Oct 2018 14:17:29 -0700 (PDT) Date: Tue, 23 Oct 2018 22:17:28 +0100 From: Phillip Potter To: clm@fb.com Cc: linux-kernel@vger.kernel.org, amir73il@gmail.com, viro@zeniv.linux.org.uk, jbacik@fb.com, dsterba@suse.com, linux-fsdevel@vger.kernel.org, linux-btrfs@vger.kernel.org Subject: [RFC][PATCH v2 10/10] btrfs: use common file type conversion Message-ID: <20181023211728.GA16584@pathfinder> MIME-Version: 1.0 Content-Disposition: inline User-Agent: Mutt/1.10.1 (2018-07-13) Sender: linux-fsdevel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Deduplicate the btrfs file type conversion implementation. Original patch by Amir Goldstein. v2: - Rebased against Linux 4.19 by Phillip Potter - Compile-time checks added by Phillip Potter to make sure the BTRFS_FT_x enum values stay same as FT_x values v1: - Initial implementation Signed-off-by: Phillip Potter --- fs/btrfs/btrfs_inode.h | 2 -- fs/btrfs/delayed-inode.c | 2 +- fs/btrfs/inode.c | 35 +++++++++++++++++---------------- include/uapi/linux/btrfs_tree.h | 2 ++ 4 files changed, 21 insertions(+), 20 deletions(-) diff --git a/fs/btrfs/btrfs_inode.h b/fs/btrfs/btrfs_inode.h index 1343ac57b438..c7c6db6b4a35 100644 --- a/fs/btrfs/btrfs_inode.h +++ b/fs/btrfs/btrfs_inode.h @@ -196,8 +196,6 @@ struct btrfs_inode { struct inode vfs_inode; }; -extern unsigned char btrfs_filetype_table[]; - static inline struct btrfs_inode *BTRFS_I(const struct inode *inode) { return container_of(inode, struct btrfs_inode, vfs_inode); diff --git a/fs/btrfs/delayed-inode.c b/fs/btrfs/delayed-inode.c index f51b509f2d9b..c1da34e3a775 100644 --- a/fs/btrfs/delayed-inode.c +++ b/fs/btrfs/delayed-inode.c @@ -1691,7 +1691,7 @@ int btrfs_readdir_delayed_dir_index(struct dir_context *ctx, name = (char *)(di + 1); name_len = btrfs_stack_dir_name_len(di); - d_type = btrfs_filetype_table[di->type]; + d_type = fs_dtype(di->type); btrfs_disk_key_to_cpu(&location, &di->location); over = !dir_emit(ctx, name, name_len, diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c index 3ea5339603cf..4cbdcba4799a 100644 --- a/fs/btrfs/inode.c +++ b/fs/btrfs/inode.c @@ -73,17 +73,6 @@ struct kmem_cache *btrfs_trans_handle_cachep; struct kmem_cache *btrfs_path_cachep; struct kmem_cache *btrfs_free_space_cachep; -#define S_SHIFT 12 -static const unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = { - [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE, - [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR, - [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV, - [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV, - [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO, - [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK, - [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK, -}; - static int btrfs_setsize(struct inode *inode, struct iattr *attr); static int btrfs_truncate(struct inode *inode, bool skip_writeback); static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent); @@ -5803,10 +5792,6 @@ static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry, return d_splice_alias(inode, dentry); } -unsigned char btrfs_filetype_table[] = { - DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK -}; - /* * All this infrastructure exists because dir_emit can fault, and we are holding * the tree lock when doing readdir. For now just allocate a buffer and copy @@ -5945,7 +5930,23 @@ static int btrfs_real_readdir(struct file *file, struct dir_context *ctx) name_ptr = (char *)(entry + 1); read_extent_buffer(leaf, name_ptr, (unsigned long)(di + 1), name_len); - put_unaligned(btrfs_filetype_table[btrfs_dir_type(leaf, di)], + + /* + * compile-time asserts that generic FT_x types + * still match BTRFS_FT_x types - no need to list + * in other functions as well as build will + * fail either way + */ + BUILD_BUG_ON(BTRFS_FT_UNKNOWN != FT_UNKNOWN); + BUILD_BUG_ON(BTRFS_FT_REG_FILE != FT_REG_FILE); + BUILD_BUG_ON(BTRFS_FT_DIR != FT_DIR); + BUILD_BUG_ON(BTRFS_FT_CHRDEV != FT_CHRDEV); + BUILD_BUG_ON(BTRFS_FT_BLKDEV != FT_BLKDEV); + BUILD_BUG_ON(BTRFS_FT_FIFO != FT_FIFO); + BUILD_BUG_ON(BTRFS_FT_SOCK != FT_SOCK); + BUILD_BUG_ON(BTRFS_FT_SYMLINK != FT_SYMLINK); + + put_unaligned(fs_dtype(btrfs_dir_type(leaf, di)), &entry->type); btrfs_dir_item_key_to_cpu(leaf, di, &location); put_unaligned(location.objectid, &entry->ino); @@ -6350,7 +6351,7 @@ static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans, static inline u8 btrfs_inode_type(struct inode *inode) { - return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT]; + return fs_umode_to_ftype(inode->i_mode); } /* diff --git a/include/uapi/linux/btrfs_tree.h b/include/uapi/linux/btrfs_tree.h index aff1356c2bb8..5747cffa09fa 100644 --- a/include/uapi/linux/btrfs_tree.h +++ b/include/uapi/linux/btrfs_tree.h @@ -307,6 +307,8 @@ * * Used by: * struct btrfs_dir_item.type + * + * Values 0..7 should match common file type values in file_type.h. */ #define BTRFS_FT_UNKNOWN 0 #define BTRFS_FT_REG_FILE 1