From patchwork Thu Aug 11 04:51:09 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hugh Dickins X-Patchwork-Id: 12941145 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id D69E7C19F2D for ; Thu, 11 Aug 2022 04:51:13 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 56F266B0074; Thu, 11 Aug 2022 00:51:13 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 51F1E6B0075; Thu, 11 Aug 2022 00:51:13 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 3E79D8E0001; Thu, 11 Aug 2022 00:51:13 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 316856B0074 for ; Thu, 11 Aug 2022 00:51:13 -0400 (EDT) Received: from smtpin29.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id EECC0A6441 for ; Thu, 11 Aug 2022 04:51:12 +0000 (UTC) X-FDA: 79786087584.29.86E767A Received: from mail-oi1-f181.google.com (mail-oi1-f181.google.com [209.85.167.181]) by imf28.hostedemail.com (Postfix) with ESMTP id 856E0C0161 for ; Thu, 11 Aug 2022 04:51:12 +0000 (UTC) Received: by mail-oi1-f181.google.com with SMTP id c185so20150383oia.7 for ; Wed, 10 Aug 2022 21:51:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=mime-version:message-id:subject:cc:to:from:date:from:to:cc; bh=m1OXKNq7+DtNWBynhSNEKrKaVxfMrlkQMnFw/pURbg0=; b=sTsT9dad5iemvIuEELMDiMVhOE0RThIC2/6RND1+gIR9BG0Tup4Ja0VLAyqCjMUQj9 +DIZ3WDS2XbbYkIXwp7ii2YLV3uDoUWuyylR8fUDndlLO/U/j+iWnin0bWNBiMfoaBd5 0Uu8sjJwP6rnNCfDnWxCUBJwpvqg6C5HSK2OdC48sFSdi7wz7GrnanfoD3gGnqd6pGHl wR8UWwPrD199kDC4fOjUbz4QaVYVrhkqLe5PknSYTD24vXiE8nQpAdGEIQbuv0VlKFsa OAz64t3JK/91dFMW3ucoHT/pHAtA3qbt94QVwPtopQRwyILKHlnFpUfw31PynvuAT1xU hZNw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=mime-version:message-id:subject:cc:to:from:date:x-gm-message-state :from:to:cc; bh=m1OXKNq7+DtNWBynhSNEKrKaVxfMrlkQMnFw/pURbg0=; b=4l4MqgXeYa89End5UgW8mUiMImdzG9fUbR8VBYuwUo7HhXGakETZoh++A12rQgwOKA y0he0jMC3JhnlqBhfYLsJppwyHnSTv7kwETqNB59F1scNf6X9P+30W88iOSxHCbpYYgr Qdc0qi5r8zvpo/exBX/+IA3Lkq2gE7DjRhWtW91ju36tpKf+IamyoosFGRel1Wbk70aT aYW46JH1IJty+UV8+8i1EKA/7qesVuCi35t9mCF8qfhGjd+MCFFk2fXWCvkQMY9cMyjD dCMxlK7jAoU4fSuhi0vb91SBilc0UtlmF1M8B49OAm5CcA3ZDtkGl4be78GG/0Mpruz1 +7qA== X-Gm-Message-State: ACgBeo1QJf4y8d+iF5xOJH5uV8TfYRkAnf5jq9ybJu+ELQspm9qFKMit hxIJZVqlK4KnVjC209zI/0IhmA== X-Google-Smtp-Source: AA6agR403qDz149yAHlea6yIhxoqoqCqmWgbvz0Cq6UeiC9cA/hbd16184lAZZZ/C/S+0UxZv8hIkA== X-Received: by 2002:aca:4182:0:b0:343:9d9:1d18 with SMTP id o124-20020aca4182000000b0034309d91d18mr2680250oia.86.1660193471466; Wed, 10 Aug 2022 21:51:11 -0700 (PDT) Received: from ripple.attlocal.net (172-10-233-147.lightspeed.sntcca.sbcglobal.net. [172.10.233.147]) by smtp.gmail.com with ESMTPSA id c84-20020acab357000000b003358e034f72sm931014oif.7.2022.08.10.21.51.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 10 Aug 2022 21:51:11 -0700 (PDT) Date: Wed, 10 Aug 2022 21:51:09 -0700 (PDT) From: Hugh Dickins X-X-Sender: hugh@ripple.anvils To: Andrew Morton cc: Theodore Ts'o , Radoslaw Burny , linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org Subject: [PATCH] mm/shmem: fix chattr fsflags support in tmpfs Message-ID: <2961dcb0-ddf3-b9f0-3268-12a4ff996856@google.com> MIME-Version: 1.0 ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1660193472; a=rsa-sha256; cv=none; b=Jhfwqit3bHfSacap/UNe+u44iTT33tskRbbh2ElLMobhZ8OvUpYryOZ+eeLsNR5TtQcdhJ T+GS5dGgQWckBNu5g6i2Os/YJhLPy++oTjdLbGyQdaJxMbwL/hfkX0aZPafnhPJPyBdwae DpkJTlNMvTXaRPYx9s6u23UlmGTM2RE= ARC-Authentication-Results: i=1; imf28.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=sTsT9dad; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf28.hostedemail.com: domain of hughd@google.com designates 209.85.167.181 as permitted sender) smtp.mailfrom=hughd@google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1660193472; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding:in-reply-to: references:dkim-signature; bh=m1OXKNq7+DtNWBynhSNEKrKaVxfMrlkQMnFw/pURbg0=; b=RJgGPcjmSZBdopZdUYYwiMI0ockwvby4VjDGVHQnCFyYZbXoT6yJvt/VcWJdYsBDVLnaon /y24xsIclb7uv7+3IJudJLKHQ5GR3AbuJrdcbWQj3EO8ewVYx89JodA7wa+U0r1omFbU1k bAkur+VwuERpUVl/pu219U7dutUhHV4= X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: 856E0C0161 Authentication-Results: imf28.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=sTsT9dad; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf28.hostedemail.com: domain of hughd@google.com designates 209.85.167.181 as permitted sender) smtp.mailfrom=hughd@google.com X-Stat-Signature: 5t4yyxik968tik34extg5jhuw1mdbiwh X-Rspam-User: X-HE-Tag: 1660193472-684715 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: ext[234] have always allowed unimplemented chattr flags to be set, but other filesystems have tended to be stricter. Follow the stricter approach for tmpfs: I don't want to have to explain why csu attributes don't actually work, and we won't need to update the chattr(1) manpage; and it's never wrong to start off strict, relaxing later if persuaded. Allow only a (append only) i (immutable) A (no atime) and d (no dump). Although lsattr showed 'A' inherited, the NOATIME behavior was not being inherited: because nothing sync'ed FS_NOATIME_FL to S_NOATIME. Add shmem_set_inode_flags() to sync the flags, using inode_set_flags() to avoid that instant of lost immutablility during fileattr_set(). But that change switched generic/079 from passing to failing: because FS_IMMUTABLE_FL and FS_APPEND_FL had been unconventionally included in the INHERITED fsflags: remove them and generic/079 is back to passing. Fixes: e408e695f5f1 ("mm/shmem: support FS_IOC_[SG]ETFLAGS in tmpfs") Signed-off-by: Hugh Dickins --- include/linux/shmem_fs.h | 13 +++------- mm/shmem.c | 54 +++++++++++++++++++++++----------------- 2 files changed, 35 insertions(+), 32 deletions(-) --- a/include/linux/shmem_fs.h +++ b/include/linux/shmem_fs.h @@ -29,15 +29,10 @@ struct shmem_inode_info { struct inode vfs_inode; }; -#define SHMEM_FL_USER_VISIBLE FS_FL_USER_VISIBLE -#define SHMEM_FL_USER_MODIFIABLE FS_FL_USER_MODIFIABLE -#define SHMEM_FL_INHERITED FS_FL_USER_MODIFIABLE - -/* Flags that are appropriate for regular files (all but dir-specific ones). */ -#define SHMEM_REG_FLMASK (~(FS_DIRSYNC_FL | FS_TOPDIR_FL)) - -/* Flags that are appropriate for non-directories/regular files. */ -#define SHMEM_OTHER_FLMASK (FS_NODUMP_FL | FS_NOATIME_FL) +#define SHMEM_FL_USER_VISIBLE FS_FL_USER_VISIBLE +#define SHMEM_FL_USER_MODIFIABLE \ + (FS_IMMUTABLE_FL | FS_APPEND_FL | FS_NODUMP_FL | FS_NOATIME_FL) +#define SHMEM_FL_INHERITED (FS_NODUMP_FL | FS_NOATIME_FL) struct shmem_sb_info { unsigned long max_blocks; /* How many blocks are allowed */ diff --git a/mm/shmem.c b/mm/shmem.c index e975fcd9d2e1..bd9b114a8650 100644 --- a/mm/shmem.c +++ b/mm/shmem.c @@ -2281,16 +2281,34 @@ static int shmem_mmap(struct file *file, struct vm_area_struct *vma) return 0; } -/* Mask out flags that are inappropriate for the given type of inode. */ -static unsigned shmem_mask_flags(umode_t mode, __u32 flags) +#ifdef CONFIG_TMPFS_XATTR +static int shmem_initxattrs(struct inode *, const struct xattr *, void *); + +/* + * chattr's fsflags are unrelated to extended attributes, + * but tmpfs has chosen to enable them under the same config option. + */ +static void shmem_set_inode_flags(struct inode *inode, unsigned int fsflags) +{ + unsigned int i_flags = 0; + + if (fsflags & FS_NOATIME_FL) + i_flags |= S_NOATIME; + if (fsflags & FS_APPEND_FL) + i_flags |= S_APPEND; + if (fsflags & FS_IMMUTABLE_FL) + i_flags |= S_IMMUTABLE; + /* + * But FS_NODUMP_FL does not require any action in i_flags. + */ + inode_set_flags(inode, i_flags, S_NOATIME | S_APPEND | S_IMMUTABLE); +} +#else +static void shmem_set_inode_flags(struct inode *inode, unsigned int fsflags) { - if (S_ISDIR(mode)) - return flags; - else if (S_ISREG(mode)) - return flags & SHMEM_REG_FLMASK; - else - return flags & SHMEM_OTHER_FLMASK; } +#define shmem_initxattrs NULL +#endif static struct inode *shmem_get_inode(struct super_block *sb, struct inode *dir, umode_t mode, dev_t dev, unsigned long flags) @@ -2319,7 +2337,8 @@ static struct inode *shmem_get_inode(struct super_block *sb, struct inode *dir, info->i_crtime = inode->i_mtime; info->fsflags = (dir == NULL) ? 0 : SHMEM_I(dir)->fsflags & SHMEM_FL_INHERITED; - info->fsflags = shmem_mask_flags(mode, info->fsflags); + if (info->fsflags) + shmem_set_inode_flags(inode, info->fsflags); INIT_LIST_HEAD(&info->shrinklist); INIT_LIST_HEAD(&info->swaplist); simple_xattrs_init(&info->xattrs); @@ -2468,12 +2487,6 @@ int shmem_mfill_atomic_pte(struct mm_struct *dst_mm, static const struct inode_operations shmem_symlink_inode_operations; static const struct inode_operations shmem_short_symlink_operations; -#ifdef CONFIG_TMPFS_XATTR -static int shmem_initxattrs(struct inode *, const struct xattr *, void *); -#else -#define shmem_initxattrs NULL -#endif - static int shmem_write_begin(struct file *file, struct address_space *mapping, loff_t pos, unsigned len, @@ -3179,18 +3192,13 @@ static int shmem_fileattr_set(struct user_namespace *mnt_userns, if (fileattr_has_fsx(fa)) return -EOPNOTSUPP; + if (fa->flags & ~SHMEM_FL_USER_MODIFIABLE) + return -EOPNOTSUPP; info->fsflags = (info->fsflags & ~SHMEM_FL_USER_MODIFIABLE) | (fa->flags & SHMEM_FL_USER_MODIFIABLE); - inode->i_flags &= ~(S_APPEND | S_IMMUTABLE | S_NOATIME); - if (info->fsflags & FS_APPEND_FL) - inode->i_flags |= S_APPEND; - if (info->fsflags & FS_IMMUTABLE_FL) - inode->i_flags |= S_IMMUTABLE; - if (info->fsflags & FS_NOATIME_FL) - inode->i_flags |= S_NOATIME; - + shmem_set_inode_flags(inode, info->fsflags); inode->i_ctime = current_time(inode); return 0; }