All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH RFC] fhandle: expose u64 mount id to name_to_handle_at(2)
@ 2024-05-20 21:35 Aleksa Sarai
  2024-05-20 21:53 ` Jeff Layton
                   ` (5 more replies)
  0 siblings, 6 replies; 21+ messages in thread
From: Aleksa Sarai @ 2024-05-20 21:35 UTC (permalink / raw)
  To: Alexander Viro, Christian Brauner, Jan Kara, Chuck Lever,
	Jeff Layton, Amir Goldstein, Alexander Aring
  Cc: linux-fsdevel, linux-nfs, linux-kernel, Aleksa Sarai

Now that we have stabilised the unique 64-bit mount ID interface in
statx, we can now provide a race-free way for name_to_handle_at(2) to
provide a file handle and corresponding mount without needing to worry
about racing with /proc/mountinfo parsing.

As with AT_HANDLE_FID, AT_HANDLE_UNIQUE_MNT_ID reuses a statx AT_* bit
that doesn't make sense for name_to_handle_at(2).

Signed-off-by: Aleksa Sarai <cyphar@cyphar.com>
---
 fs/fhandle.c               | 27 +++++++++++++++++++--------
 include/uapi/linux/fcntl.h |  2 ++
 2 files changed, 21 insertions(+), 8 deletions(-)

diff --git a/fs/fhandle.c b/fs/fhandle.c
index 8a7f86c2139a..6bc7ffccff8c 100644
--- a/fs/fhandle.c
+++ b/fs/fhandle.c
@@ -16,7 +16,8 @@
 
 static long do_sys_name_to_handle(const struct path *path,
 				  struct file_handle __user *ufh,
-				  int __user *mnt_id, int fh_flags)
+				  void __user *mnt_id, bool unique_mntid,
+				  int fh_flags)
 {
 	long retval;
 	struct file_handle f_handle;
@@ -69,10 +70,16 @@ static long do_sys_name_to_handle(const struct path *path,
 	} else
 		retval = 0;
 	/* copy the mount id */
-	if (put_user(real_mount(path->mnt)->mnt_id, mnt_id) ||
-	    copy_to_user(ufh, handle,
-			 struct_size(handle, f_handle, handle_bytes)))
-		retval = -EFAULT;
+	if (unique_mntid)
+		retval = put_user(real_mount(path->mnt)->mnt_id_unique,
+				  (u64 __user *) mnt_id);
+	else
+		retval = put_user(real_mount(path->mnt)->mnt_id,
+				  (int __user *) mnt_id);
+	/* copy the handle */
+	if (!retval)
+		retval = copy_to_user(ufh, handle,
+				struct_size(handle, f_handle, handle_bytes));
 	kfree(handle);
 	return retval;
 }
@@ -83,6 +90,7 @@ static long do_sys_name_to_handle(const struct path *path,
  * @name: name that should be converted to handle.
  * @handle: resulting file handle
  * @mnt_id: mount id of the file system containing the file
+ *          (u64 if AT_HANDLE_UNIQUE_MNT_ID, otherwise int)
  * @flag: flag value to indicate whether to follow symlink or not
  *        and whether a decodable file handle is required.
  *
@@ -92,7 +100,7 @@ static long do_sys_name_to_handle(const struct path *path,
  * value required.
  */
 SYSCALL_DEFINE5(name_to_handle_at, int, dfd, const char __user *, name,
-		struct file_handle __user *, handle, int __user *, mnt_id,
+		struct file_handle __user *, handle, void __user *, mnt_id,
 		int, flag)
 {
 	struct path path;
@@ -100,7 +108,8 @@ SYSCALL_DEFINE5(name_to_handle_at, int, dfd, const char __user *, name,
 	int fh_flags;
 	int err;
 
-	if (flag & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH | AT_HANDLE_FID))
+	if (flag & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH | AT_HANDLE_FID |
+		     AT_HANDLE_UNIQUE_MNT_ID))
 		return -EINVAL;
 
 	lookup_flags = (flag & AT_SYMLINK_FOLLOW) ? LOOKUP_FOLLOW : 0;
@@ -109,7 +118,9 @@ SYSCALL_DEFINE5(name_to_handle_at, int, dfd, const char __user *, name,
 		lookup_flags |= LOOKUP_EMPTY;
 	err = user_path_at(dfd, name, lookup_flags, &path);
 	if (!err) {
-		err = do_sys_name_to_handle(&path, handle, mnt_id, fh_flags);
+		err = do_sys_name_to_handle(&path, handle, mnt_id,
+					    flag & AT_HANDLE_UNIQUE_MNT_ID,
+					    fh_flags);
 		path_put(&path);
 	}
 	return err;
diff --git a/include/uapi/linux/fcntl.h b/include/uapi/linux/fcntl.h
index c0bcc185fa48..fda970f92fba 100644
--- a/include/uapi/linux/fcntl.h
+++ b/include/uapi/linux/fcntl.h
@@ -118,6 +118,8 @@
 #define AT_HANDLE_FID		AT_REMOVEDIR	/* file handle is needed to
 					compare object identity and may not
 					be usable to open_by_handle_at(2) */
+#define AT_HANDLE_UNIQUE_MNT_ID	AT_STATX_FORCE_SYNC /* returned mount id is
+					the u64 unique mount id */
 #if defined(__KERNEL__)
 #define AT_GETATTR_NOSEC	0x80000000
 #endif

---
base-commit: 584bbf439d0fa83d728ec49f3a38c581bdc828b4
change-id: 20240515-exportfs-u64-mount-id-9ebb5c58b53c

Best regards,
-- 
Aleksa Sarai <cyphar@cyphar.com>


^ permalink raw reply related	[flat|nested] 21+ messages in thread
* Re: [PATCH RFC] fhandle: expose u64 mount id to name_to_handle_at(2)
@ 2024-05-21 12:16 kernel test robot
  0 siblings, 0 replies; 21+ messages in thread
From: kernel test robot @ 2024-05-21 12:16 UTC (permalink / raw)
  To: oe-kbuild; +Cc: lkp, Dan Carpenter

BCC: lkp@intel.com
CC: oe-kbuild-all@lists.linux.dev
In-Reply-To: <20240520-exportfs-u64-mount-id-v1-1-f55fd9215b8e@cyphar.com>
References: <20240520-exportfs-u64-mount-id-v1-1-f55fd9215b8e@cyphar.com>
TO: Aleksa Sarai <cyphar@cyphar.com>

Hi Aleksa,

[This is a private test report for your RFC patch.]
kernel test robot noticed the following build warnings:

[auto build test WARNING on 584bbf439d0fa83d728ec49f3a38c581bdc828b4]

url:    https://github.com/intel-lab-lkp/linux/commits/Aleksa-Sarai/fhandle-expose-u64-mount-id-to-name_to_handle_at-2/20240521-053815
base:   584bbf439d0fa83d728ec49f3a38c581bdc828b4
patch link:    https://lore.kernel.org/r/20240520-exportfs-u64-mount-id-v1-1-f55fd9215b8e%40cyphar.com
patch subject: [PATCH RFC] fhandle: expose u64 mount id to name_to_handle_at(2)
:::::: branch date: 14 hours ago
:::::: commit date: 14 hours ago
config: x86_64-randconfig-161-20240521 (https://download.01.org/0day-ci/archive/20240521/202405211900.ZcYOJf1E-lkp@intel.com/config)
compiler: gcc-13 (Ubuntu 13.2.0-4ubuntu3) 13.2.0

If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <lkp@intel.com>
| Reported-by: Dan Carpenter <error27@gmail.com>
| Closes: https://lore.kernel.org/r/202405211900.ZcYOJf1E-lkp@intel.com/

smatch warnings:
fs/fhandle.c:84 do_sys_name_to_handle() warn: maybe return -EFAULT instead of the bytes remaining?

vim +84 fs/fhandle.c

990d6c2d7aee92 Aneesh Kumar K.V    2011-01-29  16  
6ccaaf59c335a8 Al Viro             2022-08-04  17  static long do_sys_name_to_handle(const struct path *path,
990d6c2d7aee92 Aneesh Kumar K.V    2011-01-29  18  				  struct file_handle __user *ufh,
1702d25fd14170 Aleksa Sarai        2024-05-20  19  				  void __user *mnt_id, bool unique_mntid,
1702d25fd14170 Aleksa Sarai        2024-05-20  20  				  int fh_flags)
990d6c2d7aee92 Aneesh Kumar K.V    2011-01-29  21  {
990d6c2d7aee92 Aneesh Kumar K.V    2011-01-29  22  	long retval;
990d6c2d7aee92 Aneesh Kumar K.V    2011-01-29  23  	struct file_handle f_handle;
990d6c2d7aee92 Aneesh Kumar K.V    2011-01-29  24  	int handle_dwords, handle_bytes;
990d6c2d7aee92 Aneesh Kumar K.V    2011-01-29  25  	struct file_handle *handle = NULL;
990d6c2d7aee92 Aneesh Kumar K.V    2011-01-29  26  
990d6c2d7aee92 Aneesh Kumar K.V    2011-01-29  27  	/*
96b2b072ee62be Amir Goldstein      2023-05-02  28  	 * We need to make sure whether the file system support decoding of
96b2b072ee62be Amir Goldstein      2023-05-02  29  	 * the file handle if decodeable file handle was requested.
990d6c2d7aee92 Aneesh Kumar K.V    2011-01-29  30  	 */
66c62769bcf6aa Amir Goldstein      2023-10-23  31  	if (!exportfs_can_encode_fh(path->dentry->d_sb->s_export_op, fh_flags))
990d6c2d7aee92 Aneesh Kumar K.V    2011-01-29  32  		return -EOPNOTSUPP;
990d6c2d7aee92 Aneesh Kumar K.V    2011-01-29  33  
990d6c2d7aee92 Aneesh Kumar K.V    2011-01-29  34  	if (copy_from_user(&f_handle, ufh, sizeof(struct file_handle)))
990d6c2d7aee92 Aneesh Kumar K.V    2011-01-29  35  		return -EFAULT;
990d6c2d7aee92 Aneesh Kumar K.V    2011-01-29  36  
990d6c2d7aee92 Aneesh Kumar K.V    2011-01-29  37  	if (f_handle.handle_bytes > MAX_HANDLE_SZ)
990d6c2d7aee92 Aneesh Kumar K.V    2011-01-29  38  		return -EINVAL;
990d6c2d7aee92 Aneesh Kumar K.V    2011-01-29  39  
68d6f4f3fbd9b1 Gustavo A. R. Silva 2024-03-25  40  	handle = kzalloc(struct_size(handle, f_handle, f_handle.handle_bytes),
990d6c2d7aee92 Aneesh Kumar K.V    2011-01-29  41  			 GFP_KERNEL);
990d6c2d7aee92 Aneesh Kumar K.V    2011-01-29  42  	if (!handle)
990d6c2d7aee92 Aneesh Kumar K.V    2011-01-29  43  		return -ENOMEM;
990d6c2d7aee92 Aneesh Kumar K.V    2011-01-29  44  
990d6c2d7aee92 Aneesh Kumar K.V    2011-01-29  45  	/* convert handle size to multiple of sizeof(u32) */
990d6c2d7aee92 Aneesh Kumar K.V    2011-01-29  46  	handle_dwords = f_handle.handle_bytes >> 2;
990d6c2d7aee92 Aneesh Kumar K.V    2011-01-29  47  
96b2b072ee62be Amir Goldstein      2023-05-02  48  	/* we ask for a non connectable maybe decodeable file handle */
990d6c2d7aee92 Aneesh Kumar K.V    2011-01-29  49  	retval = exportfs_encode_fh(path->dentry,
990d6c2d7aee92 Aneesh Kumar K.V    2011-01-29  50  				    (struct fid *)handle->f_handle,
96b2b072ee62be Amir Goldstein      2023-05-02  51  				    &handle_dwords, fh_flags);
990d6c2d7aee92 Aneesh Kumar K.V    2011-01-29  52  	handle->handle_type = retval;
990d6c2d7aee92 Aneesh Kumar K.V    2011-01-29  53  	/* convert handle size to bytes */
990d6c2d7aee92 Aneesh Kumar K.V    2011-01-29  54  	handle_bytes = handle_dwords * sizeof(u32);
990d6c2d7aee92 Aneesh Kumar K.V    2011-01-29  55  	handle->handle_bytes = handle_bytes;
990d6c2d7aee92 Aneesh Kumar K.V    2011-01-29  56  	if ((handle->handle_bytes > f_handle.handle_bytes) ||
7cdafe6cc4a6ee Amir Goldstein      2023-05-24  57  	    (retval == FILEID_INVALID) || (retval < 0)) {
990d6c2d7aee92 Aneesh Kumar K.V    2011-01-29  58  		/* As per old exportfs_encode_fh documentation
990d6c2d7aee92 Aneesh Kumar K.V    2011-01-29  59  		 * we could return ENOSPC to indicate overflow
990d6c2d7aee92 Aneesh Kumar K.V    2011-01-29  60  		 * But file system returned 255 always. So handle
990d6c2d7aee92 Aneesh Kumar K.V    2011-01-29  61  		 * both the values
990d6c2d7aee92 Aneesh Kumar K.V    2011-01-29  62  		 */
7cdafe6cc4a6ee Amir Goldstein      2023-05-24  63  		if (retval == FILEID_INVALID || retval == -ENOSPC)
7cdafe6cc4a6ee Amir Goldstein      2023-05-24  64  			retval = -EOVERFLOW;
990d6c2d7aee92 Aneesh Kumar K.V    2011-01-29  65  		/*
990d6c2d7aee92 Aneesh Kumar K.V    2011-01-29  66  		 * set the handle size to zero so we copy only
990d6c2d7aee92 Aneesh Kumar K.V    2011-01-29  67  		 * non variable part of the file_handle
990d6c2d7aee92 Aneesh Kumar K.V    2011-01-29  68  		 */
990d6c2d7aee92 Aneesh Kumar K.V    2011-01-29  69  		handle_bytes = 0;
990d6c2d7aee92 Aneesh Kumar K.V    2011-01-29  70  	} else
990d6c2d7aee92 Aneesh Kumar K.V    2011-01-29  71  		retval = 0;
990d6c2d7aee92 Aneesh Kumar K.V    2011-01-29  72  	/* copy the mount id */
1702d25fd14170 Aleksa Sarai        2024-05-20  73  	if (unique_mntid)
1702d25fd14170 Aleksa Sarai        2024-05-20  74  		retval = put_user(real_mount(path->mnt)->mnt_id_unique,
1702d25fd14170 Aleksa Sarai        2024-05-20  75  				  (u64 __user *) mnt_id);
1702d25fd14170 Aleksa Sarai        2024-05-20  76  	else
1702d25fd14170 Aleksa Sarai        2024-05-20  77  		retval = put_user(real_mount(path->mnt)->mnt_id,
1702d25fd14170 Aleksa Sarai        2024-05-20  78  				  (int __user *) mnt_id);
1702d25fd14170 Aleksa Sarai        2024-05-20  79  	/* copy the handle */
1702d25fd14170 Aleksa Sarai        2024-05-20  80  	if (!retval)
1702d25fd14170 Aleksa Sarai        2024-05-20  81  		retval = copy_to_user(ufh, handle,
1702d25fd14170 Aleksa Sarai        2024-05-20  82  				struct_size(handle, f_handle, handle_bytes));
990d6c2d7aee92 Aneesh Kumar K.V    2011-01-29  83  	kfree(handle);
990d6c2d7aee92 Aneesh Kumar K.V    2011-01-29 @84  	return retval;
990d6c2d7aee92 Aneesh Kumar K.V    2011-01-29  85  }
990d6c2d7aee92 Aneesh Kumar K.V    2011-01-29  86  

-- 
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki

^ permalink raw reply	[flat|nested] 21+ messages in thread

end of thread, other threads:[~2024-05-27 13:40 UTC | newest]

Thread overview: 21+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-05-20 21:35 [PATCH RFC] fhandle: expose u64 mount id to name_to_handle_at(2) Aleksa Sarai
2024-05-20 21:53 ` Jeff Layton
2024-05-20 22:27   ` Aleksa Sarai
2024-05-21  5:04     ` Amir Goldstein
2024-05-21 10:42       ` Aleksa Sarai
2024-05-21  2:18 ` kernel test robot
2024-05-21  3:02 ` kernel test robot
2024-05-21 13:45 ` Christian Brauner
2024-05-21 14:11   ` Christian Brauner
2024-05-21 14:27     ` Jeff Layton
2024-05-21 16:42       ` Amir Goldstein
2024-05-23 19:16         ` Aleksa Sarai
2024-05-23 15:52   ` Aleksa Sarai
2024-05-24 12:25     ` Christian Brauner
2024-05-24 15:58       ` Aleksa Sarai
2024-05-27  0:48         ` Dave Chinner
2024-05-27  0:06       ` Dave Chinner
2024-05-27 13:39         ` Christian Brauner
2024-05-23 14:47 ` Dan Carpenter
2024-05-26  8:55 ` Christoph Hellwig
  -- strict thread matches above, loose matches on Subject: below --
2024-05-21 12:16 kernel test robot

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.