From patchwork Sat Oct 27 00:53:31 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Phillip Potter X-Patchwork-Id: 10657989 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 7742613BF for ; Sat, 27 Oct 2018 00:53:38 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 5CB61286E5 for ; Sat, 27 Oct 2018 00:53:38 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 5ADCD2BBB2; Sat, 27 Oct 2018 00:53: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=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 A515C2C33F for ; Sat, 27 Oct 2018 00:53:37 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726296AbeJ0Jcq (ORCPT ); Sat, 27 Oct 2018 05:32:46 -0400 Received: from mail-wm1-f66.google.com ([209.85.128.66]:35480 "EHLO mail-wm1-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725828AbeJ0Jcq (ORCPT ); Sat, 27 Oct 2018 05:32:46 -0400 Received: by mail-wm1-f66.google.com with SMTP id q12-v6so172028wmq.0 for ; Fri, 26 Oct 2018 17:53:34 -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=LyEssPGD6lhpsSMQVokRlfXP4v75jBeycdhakKexY7Q=; b=i4jdVlg2sUXvBQ2XatmUAYcC30ppAvQUBgwpBMzBhRhap+9kk78sPTmA/u3OJQelNJ Mh+KEBUqgAx/NEMRSYSJ9wo/H544bHJSQCQntr84yB7O48WpW6EHR3FEGEotZywDstCi n1V+z5Ys/2ZQ3MbVoE7W1AhXN4xUufgn1mvDIFNNUI2yW6r5ojr0xL81TaOvP8Jg46Mj +7/cBG5ckNJkiKVRJlZmWnOJoVqZgibvrwJCNgICzz+1luahJWoDbpUiBIZYYNyzr4sz PfrATPBUP9O5qnyfAchnJQYdEBxvdBl/gubBwFwm7/b8nKGE0Fs2VmTwz+jUuamW2uKw /f+A== 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=LyEssPGD6lhpsSMQVokRlfXP4v75jBeycdhakKexY7Q=; b=K7QeBWN8fB1bZxd9Qz07llpeZIt4xBpqrsBX+ENP4s2Bxe6gsRm3oh00GX6RrwNs6y RVMA0kAnDHqzJBerGw//Y0e57xcit+HO82k7BSgkQicRRz2WF6xgtqCOUZ95blhHw/3t c+JTiUqA2m+i+XfhXJtU+BCpAjLhxkC0UQo+wktCbObbo8EyObWfc5tZpSm0ZfxF05Bk cHB/tpWgvhzyHT1Hk31dyUFFpbGQpV+MIEXBCM368xvyHF+o1IJtXdDx41GcHMB1nEjg GN+PR3WhiDMEjDQZV0g0Sqszhrx4RPlV8mQjEqW9c4EAAL3OSjexGuIVYTlYK6IrJp+O jmnQ== X-Gm-Message-State: AGRZ1gKwbrJQlzPVi+3JzJB/vJ+fbukOZ2/SoAFPFrGkSPJiEiLAkvl+ dsfKUFMlx6YYTM5nIT8MioxLkD5OqXY= X-Google-Smtp-Source: AJdET5fRw9Zca1RvHSJH/dYOIaw/VpaYR2MFGfx6iI/0kuDOKpymV+tLK1oGGEgbo2zRlJM4cGub6A== X-Received: by 2002:a1c:6655:: with SMTP id a82-v6mr7371122wmc.10.1540601613025; Fri, 26 Oct 2018 17:53:33 -0700 (PDT) Received: from pathfinder (62-64-249-96.dynamic.dsl.as9105.com. [62.64.249.96]) by smtp.gmail.com with ESMTPSA id r16-v6sm11053375wrv.21.2018.10.26.17.53.31 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Fri, 26 Oct 2018 17:53:32 -0700 (PDT) Date: Sat, 27 Oct 2018 01:53:31 +0100 From: Phillip Potter To: viro@zeniv.linux.org.uk Cc: amir73il@gmail.com, linux-fsdevel@vger.kernel.org Subject: [RFC][PATCH v3 00/10] common implementation of dirent file types Message-ID: <20181027005331.GA9924@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 This cleanup series is a respin of Amir Goldstein's work, created in late 2016. It removes several instances of duplicated code. Most of the duplication dates back to git pre-historic era. The controversial aspect of this cleanup is that it uses common code to handle file system specific on-disk format bits. We handled this aspect with care by placing compile-time checks. All 7 file systems use a single byte to store dirent file type on-disk and all of them use the same conversion routines from i_mode to 4bits DT_* constants to 3bits on-disk FT_* constants. Patch 1 places a common implementation in fs_types.c with some useful conversion helpers. The corresponding header fs_types.h contains related macros and definitions, as well as helper declarations. Patches 2-3 make use of the helpers in ufs and hfsplus without any on-disk implications. Patches 4-10 replace the specific implementations in ext2, exofs, ext4, ocfs2, f2fs, nilfs and btrfs with the common implementation. The ext2 implementation appears to have been copied as a basis of the others, and in particular, for code that tries to access the ext2_type_by_mode array there is a bug that this patch series fixes. Essentially, it is defined with size S_IFMT >> S_SHIFT, so 15. This means that it is possible with a malformed inode to get an index of 15, as the array is always accessed with: ext2_type_by_mode[(mode & S_IFMT)>>S_SHIFT]; These patches also now include compile-time checks to ensure that the file system specific on-disk bits are equivalent to the common implementation FT_* bits. These compile-time checks are only included once per file system, as my reasoning is that regardless of their location, the build will fail/succeed anyway. In addition, where needed (for patches which no longer apply), I've rebased them to apply to the newest 4.19 kernel sources. Each patch is independent of the others, except for the common implementation itself which they all depend on. General motivations for the patch (other than the bugfix above) are the fact that it reduces the amount of code for most filesystems (except ext4): ufs: -27 lines hfsplus: -12 lines ext2: -9 lines exofs: -16 lines ext4: +1 line ocfs2: -9 lines f2fs: -7 lines nilsfs2: -17 lines btrfs: -2 lines It also makes it easier to add hypothetical future filesystems which wish to reuse these POSIX-type definitions and on-disk formats, as they can simply now use the common implementation. I welcome feedback, and thank those who have already provided it. Hopefully, the relevant maintainers will see fit to merge this work. v3: - Moved compile-time checks to better locations, and shortened comments, as well as formatting to 80 columns - Renamed header to fs_types.h, and placed comment regarding DT_* constants relation to POSIX and glibc dirent.h - Changed "should" to "must" in comments mentioning the need for fs specific values to match the common values in fs_types.h - Split functions and lookup tables to separate C file (fs/fs_types.c) - Added kernel-doc comments for all three utility functions, and removed therefore unnecessary lines from comment in fs_types.h - Tweaked commit messages slightly to explain that file systems using POSIX file types now don't need to define their own conversion routines - Renamed fs_dtype function to fs_ftype_to_dtype for consistency with other helper functions - Tweaked fs_ftype_to_dtype to take unsigned int argument, to prevent out of bounds access of memory - Added additional text to comment in fs_types.h explaining the definitions must never change - Changed DT_* types in fs_types.h to explicit constants as they can't change anyway - 80 line violation in the ext2 patch - left as is due to the fact the code would look worse if fixed, other patches tweaked slightly to keep within 80 line limit (by moving last parameter in various if statements) v2: - Rebased against Linux 4.19 by Phillip Potter - This version does not remove filesystem specific XXX_FT_* definitions, as these values are now used in compile-time checks added by Phillip Potter to make sure they remain the same as the generic FT_* values - Removed xfs patch (a variant of original patch has already been applied) - Added SPDX tag to new header file and included it in MAINTAINERS v1: - Initial implementation by Amir Goldstein: https://marc.info/?l=linux-fsdevel&m=148217829301701&w=2 Phillip Potter (10): fs: common implementation of file type conversions ufs: use fs_umode_to_dtype() helper hfsplus: use fs_umode_to_dtype() helper ext2: use common file type conversion exofs: use common file type conversion ext4: use common file type conversion ocfs2: use common file type conversion f2fs: use common file type conversion nilfs2: use common file type conversion btrfs: use common file type conversion MAINTAINERS | 1 + fs/Makefile | 3 +- fs/btrfs/btrfs_inode.h | 2 - fs/btrfs/delayed-inode.c | 2 +- fs/btrfs/inode.c | 32 +++++---- fs/exofs/dir.c | 48 +++++-------- fs/ext2/dir.c | 49 ++++++-------- fs/ext4/ext4.h | 33 ++++----- fs/f2fs/dir.c | 41 +++++------ fs/f2fs/inline.c | 2 +- fs/fs_types.c | 105 +++++++++++++++++++++++++++++ fs/hfsplus/dir.c | 16 +---- fs/nilfs2/dir.c | 52 +++++--------- fs/ocfs2/dir.c | 18 +---- fs/ocfs2/ocfs2_fs.h | 27 ++++---- fs/ufs/util.h | 29 +------- include/linux/f2fs_fs.h | 8 ++- include/linux/fs.h | 17 +---- include/linux/fs_types.h | 73 ++++++++++++++++++++ include/uapi/linux/btrfs_tree.h | 2 + include/uapi/linux/nilfs2_ondisk.h | 1 + 21 files changed, 314 insertions(+), 247 deletions(-) create mode 100644 fs/fs_types.c create mode 100644 include/linux/fs_types.h --- 2.17.2