From patchwork Wed Oct 25 13:50:46 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Amir Goldstein X-Patchwork-Id: 13436184 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id B9C59C07545 for ; Wed, 25 Oct 2023 13:51:05 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235032AbjJYNvF (ORCPT ); Wed, 25 Oct 2023 09:51:05 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52230 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344034AbjJYNvA (ORCPT ); Wed, 25 Oct 2023 09:51:00 -0400 Received: from mail-wm1-x32b.google.com (mail-wm1-x32b.google.com [IPv6:2a00:1450:4864:20::32b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 899BD1AA; Wed, 25 Oct 2023 06:50:55 -0700 (PDT) Received: by mail-wm1-x32b.google.com with SMTP id 5b1f17b1804b1-40836ea8cbaso42161255e9.0; Wed, 25 Oct 2023 06:50:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1698241854; x=1698846654; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=1diB7enl1V+4HSpm+PP9l4uUnLukeVGNUbE2JiZFbWw=; b=IRXhNJ6gKFzqJQGnTx8QG/hPPZjgh9j+v5LQAQVL4flFgvaiAfevLa0IvOG1ojO8gr +XsVUKSFD/vtqzQwK+uZFrNnVEZgd8wT1jSrW4v1K0a0vzEK4aqLTMAhwH6DYsqpRM/W vrWY631eviJs9+fITaJqQTTV7XdobVAd/7ezErQcq6vvZECGJLdWOvg5+BkJsQyiWDTN IRBsGdlmUhmK/6jLsBHSvhsz21MAhqTmDhOzAuEVE5uZ5yo1kKxNTquiJd8rNG06zuM5 GEmxXmdG9AKbnCsTj4voVPk2yNNN3kkYWXayKz9jPv5veHYkRwuJFADgJmYmTXrK1lej O6ig== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698241854; x=1698846654; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=1diB7enl1V+4HSpm+PP9l4uUnLukeVGNUbE2JiZFbWw=; b=fZxSaf2Sjd+h8o5I67mZ9PGVjI2GEeJQZ+ly02lk2QCE3eKN8bp+cXLdxCpde7rvWh wbX1nXQ5YiH8NKiuIB/C1tZ1J01IrcTtowpFTn6cBp+MlZk+C0k/CXv6xRP98iwSiCLy 0xvDwwQcM/eKzc9s8fSvs+i7ty5hYZfiGVNMxkzaOoE2/d7rFl75zkRym4aIoYsVX15q q/m76zw5rCvRCAsvnTQsM9DucZOLrETssPpczqIubWWZggYT7Ey8wdD22UAgsRddRu+z k3h7qVZI3BATixhe8yRZicewwy91a/b2NnwFAi0fC0GMP05e1ixHw+MJ6s7i4wE+Iyd/ 0IZg== X-Gm-Message-State: AOJu0Yy9BCdSU6ZozxxYIOVTHlcxgrXiRNWgTAn5D4PEX3vW+08+gB8T NP9x9luZvYTvh5SaBauTcC0= X-Google-Smtp-Source: AGHT+IFk5k8WXH8olXg67BAIosb3J+eExXg18C/JJhSWDjFSFaYigso/d60rY33wbnCJWjncCVIFTg== X-Received: by 2002:a5d:4686:0:b0:32d:b991:1a71 with SMTP id u6-20020a5d4686000000b0032db9911a71mr10032308wrq.0.1698241853542; Wed, 25 Oct 2023 06:50:53 -0700 (PDT) Received: from amir-ThinkPad-T480.lan ([5.29.249.86]) by smtp.gmail.com with ESMTPSA id t8-20020adff048000000b0032dc2110d01sm12143673wro.61.2023.10.25.06.50.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 25 Oct 2023 06:50:53 -0700 (PDT) From: Amir Goldstein To: Jan Kara Cc: Christian Brauner , Chris Mason , Josef Bacik , David Sterba , linux-btrfs@vger.kernel.org, linux-fsdevel@vger.kernel.org Subject: [PATCH 1/3] fs: define a new super operation to get fsid Date: Wed, 25 Oct 2023 16:50:46 +0300 Message-Id: <20231025135048.36153-2-amir73il@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231025135048.36153-1-amir73il@gmail.com> References: <20231025135048.36153-1-amir73il@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org If implemented, get_fsid() can be used as a cheaper way of getting the f_fsid memeber of kstatfs, for callers that only care about fsid. fanotify is going to make use of that to get btrfs fsid from inode on every event. Suggested-by: Jan Kara Link: https://lore.kernel.org/r/20230920110429.f4wkfuls73pd55pv@quack3/ Signed-off-by: Amir Goldstein --- Documentation/filesystems/locking.rst | 2 ++ Documentation/filesystems/vfs.rst | 4 ++++ fs/statfs.c | 14 ++++++++++++++ include/linux/fs.h | 1 + include/linux/statfs.h | 2 ++ 5 files changed, 23 insertions(+) diff --git a/Documentation/filesystems/locking.rst b/Documentation/filesystems/locking.rst index 7be2900806c8..a367950ee755 100644 --- a/Documentation/filesystems/locking.rst +++ b/Documentation/filesystems/locking.rst @@ -169,6 +169,7 @@ prototypes:: int (*freeze_fs) (struct super_block *); int (*unfreeze_fs) (struct super_block *); int (*statfs) (struct dentry *, struct kstatfs *); + int (*get_fsid) (struct inode *, __kernel_fsid_t *); int (*remount_fs) (struct super_block *, int *, char *); void (*umount_begin) (struct super_block *); int (*show_options)(struct seq_file *, struct dentry *); @@ -193,6 +194,7 @@ sync_fs: read freeze_fs: write unfreeze_fs: write statfs: maybe(read) (see below) +get_fsid: no remount_fs: write umount_begin: no show_options: no (namespace_sem) diff --git a/Documentation/filesystems/vfs.rst b/Documentation/filesystems/vfs.rst index 99acc2e98673..f30f39f056ab 100644 --- a/Documentation/filesystems/vfs.rst +++ b/Documentation/filesystems/vfs.rst @@ -267,6 +267,7 @@ filesystem. The following members are defined: enum freeze_wholder who); int (*unfreeze_fs) (struct super_block *); int (*statfs) (struct dentry *, struct kstatfs *); + int (*get_fsid) (struct inode *, __kernel_fsid_t *); int (*remount_fs) (struct super_block *, int *, char *); void (*umount_begin) (struct super_block *); @@ -374,6 +375,9 @@ or bottom half). ``statfs`` called when the VFS needs to get filesystem statistics. +``get_fsid`` + called when the VFS needs to get only the f_fsid member of statfs. + ``remount_fs`` called when the filesystem is remounted. This is called with the kernel lock held diff --git a/fs/statfs.c b/fs/statfs.c index 96d1c3edf289..60a6af7356b7 100644 --- a/fs/statfs.c +++ b/fs/statfs.c @@ -69,11 +69,25 @@ static int statfs_by_dentry(struct dentry *dentry, struct kstatfs *buf) return retval; } +int inode_get_fsid(struct inode *inode, __kernel_fsid_t *fsid) +{ + if (!inode->i_sb->s_op->get_fsid) + return -EOPNOTSUPP; + + return inode->i_sb->s_op->get_fsid(inode, fsid); +} +EXPORT_SYMBOL(inode_get_fsid); + int vfs_get_fsid(struct dentry *dentry, __kernel_fsid_t *fsid) { struct kstatfs st; int error; + /* Avoid statfs if fs supports cheaper get_fsid() */ + error = inode_get_fsid(d_inode(dentry), fsid); + if (!error) + return 0; + error = statfs_by_dentry(dentry, &st); if (error) return error; diff --git a/include/linux/fs.h b/include/linux/fs.h index 4a40823c3c67..30d7d3347c49 100644 --- a/include/linux/fs.h +++ b/include/linux/fs.h @@ -2008,6 +2008,7 @@ struct super_operations { int (*thaw_super) (struct super_block *, enum freeze_holder who); int (*unfreeze_fs) (struct super_block *); int (*statfs) (struct dentry *, struct kstatfs *); + int (*get_fsid)(struct inode *, __kernel_fsid_t *); int (*remount_fs) (struct super_block *, int *, char *); void (*umount_begin) (struct super_block *); diff --git a/include/linux/statfs.h b/include/linux/statfs.h index 02c862686ea3..c07f6d726e39 100644 --- a/include/linux/statfs.h +++ b/include/linux/statfs.h @@ -43,7 +43,9 @@ struct kstatfs { #define ST_RELATIME 0x1000 /* update atime relative to mtime/ctime */ #define ST_NOSYMFOLLOW 0x2000 /* do not follow symlinks */ +struct inode; struct dentry; +extern int inode_get_fsid(struct inode *inode, __kernel_fsid_t *fsid); extern int vfs_get_fsid(struct dentry *dentry, __kernel_fsid_t *fsid); static inline __kernel_fsid_t u64_to_fsid(u64 v) From patchwork Wed Oct 25 13:50:47 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Amir Goldstein X-Patchwork-Id: 13436185 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 38A35C25B47 for ; Wed, 25 Oct 2023 13:51:07 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235034AbjJYNvG (ORCPT ); Wed, 25 Oct 2023 09:51:06 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52216 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235015AbjJYNvF (ORCPT ); Wed, 25 Oct 2023 09:51:05 -0400 Received: from mail-wr1-x42e.google.com (mail-wr1-x42e.google.com [IPv6:2a00:1450:4864:20::42e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 52DA9199; Wed, 25 Oct 2023 06:50:56 -0700 (PDT) Received: by mail-wr1-x42e.google.com with SMTP id ffacd0b85a97d-32d80ae19f8so3937283f8f.2; Wed, 25 Oct 2023 06:50:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1698241855; x=1698846655; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=bSgXeEd7ULlQwvOdUJlS0OAnMD4LNWicUe5LQunKIfI=; b=c1Mgad7qoQF5XmxFTt1dymhti+oKhs5LPbIoB7iqzWLQdxTa/0vvuntX9JDDgi3EM4 RJPZ5LFjL06wLjhScm6uOSC4FL9o6ZjE5c/JntnS83FQTa9YYiyz1Te1N/TMji7CtMy/ 8EWWZKNqgA3RjAOwAYILIKXQdmXFXB6B+7TvWZ+m+/T3TNMp37CvZmFdzA7/FH1ulvOH luDucRtoqCs+gCsVy+d3nd/lP45477WmyUnT+S50RvX6xr7MKWzenWUBfZWArR7yfDfj fVzBPjr3t6cq0HIa0S7YuuDysAj1+zqbE55xug7B4t12cwSZW07r9wAGmnjt/KvMiscn wa9w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698241855; x=1698846655; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=bSgXeEd7ULlQwvOdUJlS0OAnMD4LNWicUe5LQunKIfI=; b=YAsEJzBFl+EP3INwWJN0kkcSdR58ebt0W4nzctd9aWlmYyFDn58HFGQo6QmlF/Fwoi lP1+kYcQJTmPkk073y9G9zgvbZHng8OUjen4BJQ8oRmayIOy39VLmGn6aAPmEfZF9k6x B+upFRE/iEYXIBivLieVZQt6GpSsCEr+CERBRKweNpM9XO3JoOv7FNV6S+oKbenudwnD f13xy4jM/USmnjwtFtQ+mVgjZN6N1S7dYtKYYsNLoziUH6e6p1ytCwr2LwD5r71r5xYC /CuEz8ZGk4XAP0y1P7WtFvQp2NakeKAMT44NqIRkyn8ERPEcXTD7og3OknREIpEcLsbd SUEQ== X-Gm-Message-State: AOJu0YxVN4KBgDM0UeUx0jEBALmD3vOEqOm51WbkVJj31tQOAYwI3eY0 aZOEs+3vmQWWTe6I5lAsxzzQXRfsgGw= X-Google-Smtp-Source: AGHT+IFFzIlXwxvcIgap54Fb94ldQ034AJ3oWh5Ftg+yG5ur8gVuFiH7cjvCbbF/naVjYX5QuK4AgA== X-Received: by 2002:adf:fe48:0:b0:32d:9fc9:d14c with SMTP id m8-20020adffe48000000b0032d9fc9d14cmr11863736wrs.47.1698241854748; Wed, 25 Oct 2023 06:50:54 -0700 (PDT) Received: from amir-ThinkPad-T480.lan ([5.29.249.86]) by smtp.gmail.com with ESMTPSA id t8-20020adff048000000b0032dc2110d01sm12143673wro.61.2023.10.25.06.50.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 25 Oct 2023 06:50:54 -0700 (PDT) From: Amir Goldstein To: Jan Kara Cc: Christian Brauner , Chris Mason , Josef Bacik , David Sterba , linux-btrfs@vger.kernel.org, linux-fsdevel@vger.kernel.org Subject: [PATCH 2/3] btrfs: implement super operation to get fsid Date: Wed, 25 Oct 2023 16:50:47 +0300 Message-Id: <20231025135048.36153-3-amir73il@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231025135048.36153-1-amir73il@gmail.com> References: <20231025135048.36153-1-amir73il@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org btrfs statfs can report a different fsid for different inodes. Implement btrfs_get_fsid() that can be used as a cheaper way of getting the f_fsid memeber of kstatfs, for callers that only care about fsid. fanotify is going to make use of that to get btrfs fsid from inode on every event. Signed-off-by: Amir Goldstein --- fs/btrfs/super.c | 33 ++++++++++++++++++++++----------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/fs/btrfs/super.c b/fs/btrfs/super.c index 1a093ec0f7e3..fd08f7e81f72 100644 --- a/fs/btrfs/super.c +++ b/fs/btrfs/super.c @@ -2016,6 +2016,26 @@ static inline int btrfs_calc_avail_data_space(struct btrfs_fs_info *fs_info, return 0; } +static int btrfs_get_fsid(struct inode *inode, __kernel_fsid_t *fsid) +{ + struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); + __be32 *__fsid = (__be32 *)fs_info->fs_devices->fsid; + + /* + * We treat it as constant endianness (it doesn't matter _which_) + * because we want the fsid to come out the same whether mounted + * on a big-endian or little-endian host + */ + fsid->val[0] = be32_to_cpu(__fsid[0]) ^ be32_to_cpu(__fsid[2]); + fsid->val[1] = be32_to_cpu(__fsid[1]) ^ be32_to_cpu(__fsid[3]); + + /* Mask in the root object ID too, to disambiguate subvols */ + fsid->val[0] ^= BTRFS_I(inode)->root->root_key.objectid >> 32; + fsid->val[1] ^= BTRFS_I(inode)->root->root_key.objectid; + + return 0; +} + /* * Calculate numbers for 'df', pessimistic in case of mixed raid profiles. * @@ -2038,7 +2058,6 @@ static int btrfs_statfs(struct dentry *dentry, struct kstatfs *buf) u64 total_free_data = 0; u64 total_free_meta = 0; u32 bits = fs_info->sectorsize_bits; - __be32 *fsid = (__be32 *)fs_info->fs_devices->fsid; unsigned factor = 1; struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv; int ret; @@ -2124,16 +2143,7 @@ static int btrfs_statfs(struct dentry *dentry, struct kstatfs *buf) buf->f_bsize = dentry->d_sb->s_blocksize; buf->f_namelen = BTRFS_NAME_LEN; - /* We treat it as constant endianness (it doesn't matter _which_) - because we want the fsid to come out the same whether mounted - on a big-endian or little-endian host */ - buf->f_fsid.val[0] = be32_to_cpu(fsid[0]) ^ be32_to_cpu(fsid[2]); - buf->f_fsid.val[1] = be32_to_cpu(fsid[1]) ^ be32_to_cpu(fsid[3]); - /* Mask in the root object ID too, to disambiguate subvols */ - buf->f_fsid.val[0] ^= - BTRFS_I(d_inode(dentry))->root->root_key.objectid >> 32; - buf->f_fsid.val[1] ^= - BTRFS_I(d_inode(dentry))->root->root_key.objectid; + btrfs_get_fsid(d_inode(dentry), &buf->f_fsid); return 0; } @@ -2362,6 +2372,7 @@ static const struct super_operations btrfs_super_ops = { .alloc_inode = btrfs_alloc_inode, .destroy_inode = btrfs_destroy_inode, .free_inode = btrfs_free_inode, + .get_fsid = btrfs_get_fsid, .statfs = btrfs_statfs, .remount_fs = btrfs_remount, .freeze_fs = btrfs_freeze, From patchwork Wed Oct 25 13:50:48 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Amir Goldstein X-Patchwork-Id: 13436186 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id B9820C25B6D for ; Wed, 25 Oct 2023 13:51:08 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234631AbjJYNvI (ORCPT ); Wed, 25 Oct 2023 09:51:08 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52242 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235046AbjJYNvG (ORCPT ); Wed, 25 Oct 2023 09:51:06 -0400 Received: from mail-wr1-x42c.google.com (mail-wr1-x42c.google.com [IPv6:2a00:1450:4864:20::42c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DED271B5; Wed, 25 Oct 2023 06:50:57 -0700 (PDT) Received: by mail-wr1-x42c.google.com with SMTP id ffacd0b85a97d-32d9d8284abso3776957f8f.3; Wed, 25 Oct 2023 06:50:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1698241856; x=1698846656; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=91nAEvCKcJvzkrpWMMB/KXjz6VNwzA38eTclBHG/U2o=; b=NAwL0g3Rzz0SXrGpvsvaDkHvG68uLjIjxbAx1yP0lhpmVgtoDpN7/i+3AAubnWhyap 5Sn88VHn+CgSNGi6bOZLs7DiwSTehRFkV9Ou+zESDIG8SSaLukORvvKtWvop/n8ozS7D 9tK+b6qDi1FVtJHQF7g9JSfcpPlSvDBBgaoClAQ3iVY9HvrlRqzGixPDIHpYjc1ALKqW 9B6ed1yOkinA0kzRgw22u3vOE4wLaee/vd/myrerxMEUIz2lh0i/lp5DsbO7M6acU9f8 y0a7jdsiOXSPKW9mvDINUsU/eI/0/p1KND+7Z6rTQiuBEYpy7Q0ANXXzlIxJrY2HrhzC Xu2g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698241856; x=1698846656; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=91nAEvCKcJvzkrpWMMB/KXjz6VNwzA38eTclBHG/U2o=; b=tE6SiDzJERvScw1FSMCSpGwyyon9DOkbKv86rb+ueQy7/tPOz4nNlUf9gKlyhCTXLk yWMO+JfG1hwHXFwRqbsXS5iNv4arkfvXgyzMb9PTzktNLEIstTuAbGxz7ul6baODoA8h uNciPDaq7WAzB22bMZCFbIqFiyk9lDfno4LXmBzFdGCCTW642OASG4AcrO/VIedJbQn/ QFOsmj4s1vVtNaoryMtGK/PXaskn0uDXSGTX+MFm0jSRdDKm5vXLwiPi9N7gHwpRpaqP fKlUOA7JdT9RmxhnY1BrcOG93F7ronrrUv55kmN4fVgXggyIdZLvProHz1/PZ64aP0qu NlIQ== X-Gm-Message-State: AOJu0YwDahsrQU+AmBSSTdh/5m+/4DJSnifnF031BzDcvQUP+AfGgkDd lB7cVxGaA9U0o6nWByb9+6M= X-Google-Smtp-Source: AGHT+IFDrYCgdccz69WUMHgXYj7KX+OhvzBcYbaetpCMZnVvqC94ZEPTrBCpnE/32qS2Q+Ktj/2j5w== X-Received: by 2002:adf:e7c6:0:b0:32d:a2d6:4058 with SMTP id e6-20020adfe7c6000000b0032da2d64058mr10569608wrn.62.1698241856136; Wed, 25 Oct 2023 06:50:56 -0700 (PDT) Received: from amir-ThinkPad-T480.lan ([5.29.249.86]) by smtp.gmail.com with ESMTPSA id t8-20020adff048000000b0032dc2110d01sm12143673wro.61.2023.10.25.06.50.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 25 Oct 2023 06:50:55 -0700 (PDT) From: Amir Goldstein To: Jan Kara Cc: Christian Brauner , Chris Mason , Josef Bacik , David Sterba , linux-btrfs@vger.kernel.org, linux-fsdevel@vger.kernel.org Subject: [PATCH 3/3] fanotify: support reporting events with fid on btrfs sub-volumes Date: Wed, 25 Oct 2023 16:50:48 +0300 Message-Id: <20231025135048.36153-4-amir73il@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231025135048.36153-1-amir73il@gmail.com> References: <20231025135048.36153-1-amir73il@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org Setting fanotify marks that report events with fid on btrfs sub-volumes is not supported, because in the case of btrfs sub-volumes, there is no uniform fsid that can be cached in the sb connector. If filesystem supports the cheap ->get_fsid() operation, do not use the cached fsid in connector and query fsid from inode on every event. This allows setting fanotify marks that report events with fid on btrfs sub-volumes. Signed-off-by: Amir Goldstein --- fs/notify/fanotify/fanotify.c | 28 +++++++++++++++++++++------- fs/notify/fanotify/fanotify_user.c | 14 ++++++++++---- 2 files changed, 31 insertions(+), 11 deletions(-) diff --git a/fs/notify/fanotify/fanotify.c b/fs/notify/fanotify/fanotify.c index 9dac7f6e72d2..25282ca0173d 100644 --- a/fs/notify/fanotify/fanotify.c +++ b/fs/notify/fanotify/fanotify.c @@ -535,6 +535,21 @@ static struct inode *fanotify_dfid_inode(u32 event_mask, const void *data, return dir; } +/* + * If @inode is available and if filesystem supports ->get_fsid(), return the + * fsid associated with the inode. Otherwise, return the pre cached fsid. + */ +static __kernel_fsid_t fanotify_inode_fsid(struct inode *inode, + __kernel_fsid_t *fsid) +{ + __kernel_fsid_t __fsid; + + if (inode && inode_get_fsid(inode, &__fsid) == 0) + return __fsid; + + return *fsid; +} + static struct fanotify_event *fanotify_alloc_path_event(const struct path *path, unsigned int *hash, gfp_t gfp) @@ -586,8 +601,8 @@ static struct fanotify_event *fanotify_alloc_fid_event(struct inode *id, return NULL; ffe->fae.type = FANOTIFY_EVENT_TYPE_FID; - ffe->fsid = *fsid; - *hash ^= fanotify_hash_fsid(fsid); + ffe->fsid = fanotify_inode_fsid(id, fsid); + *hash ^= fanotify_hash_fsid(&ffe->fsid); fanotify_encode_fh(&ffe->object_fh, id, fanotify_encode_fh_len(id), hash, gfp); @@ -627,8 +642,8 @@ static struct fanotify_event *fanotify_alloc_name_event(struct inode *dir, return NULL; fne->fae.type = FANOTIFY_EVENT_TYPE_FID_NAME; - fne->fsid = *fsid; - *hash ^= fanotify_hash_fsid(fsid); + fne->fsid = fanotify_inode_fsid(dir, fsid); + *hash ^= fanotify_hash_fsid(&fne->fsid); info = &fne->info; fanotify_info_init(info); if (dir_fh_len) { @@ -691,9 +706,10 @@ static struct fanotify_event *fanotify_alloc_error_event( fee->fae.type = FANOTIFY_EVENT_TYPE_FS_ERROR; fee->error = report->error; fee->err_count = 1; - fee->fsid = *fsid; inode = report->inode; + fee->fsid = fanotify_inode_fsid(inode, fsid); + *hash ^= fanotify_hash_fsid(&fee->fsid); fh_len = fanotify_encode_fh_len(inode); /* Bad fh_len. Fallback to using an invalid fh. Should never happen. */ @@ -702,8 +718,6 @@ static struct fanotify_event *fanotify_alloc_error_event( fanotify_encode_fh(&fee->object_fh, inode, fh_len, NULL, 0); - *hash ^= fanotify_hash_fsid(fsid); - return &fee->fae; } diff --git a/fs/notify/fanotify/fanotify_user.c b/fs/notify/fanotify/fanotify_user.c index 0eb9622e8a9f..ed67e5f973ab 100644 --- a/fs/notify/fanotify/fanotify_user.c +++ b/fs/notify/fanotify/fanotify_user.c @@ -1570,16 +1570,22 @@ static int fanotify_test_fsid(struct dentry *dentry, __kernel_fsid_t *fsid) return -ENODEV; /* - * Make sure dentry is not of a filesystem subvolume (e.g. btrfs) - * which uses a different fsid than sb root. + * If dentry is on a filesystem subvolume (e.g. btrfs), which uses a + * different fsid than sb root, then make sure that filesytem supports + * getting fsid from inode. */ err = vfs_get_fsid(dentry->d_sb->s_root, &root_fsid); if (err) return err; if (root_fsid.val[0] != fsid->val[0] || - root_fsid.val[1] != fsid->val[1]) - return -EXDEV; + root_fsid.val[1] != fsid->val[1]) { + if (!dentry->d_sb->s_op->get_fsid) + return -EXDEV; + + /* Cache root fsid in case inode is not available on event */ + *fsid = root_fsid; + } return 0; }