Skip to content

Commit a312c10

Browse files
committed
Merge patch series "API for exporting connectable file handles to userspace"
Amir Goldstein <amir73il@gmail.com> says: These patches bring the NFS connectable file handles feature to userspace servers. They rely on Christian's and Aleksa's changes recently merged to v6.12. The API I chose for encoding conenctable file handles is pretty conventional (AT_HANDLE_CONNECTABLE). open_by_handle_at(2) does not have AT_ flags argument, but also, I find it more useful API that encoding a connectable file handle can mandate the resolving of a connected fd, without having to opt-in for a connected fd independently. I chose to implemnent this by using upper bits in the handle type field It may be that out-of-tree filesystems return a handle type with upper bits set, but AFAIK, no in-tree filesystem does that. I added some warnings just in case we encouter that. I have written an fstest [1] and a man page draft [2] for the feature. [1] https://github.com/amir73il/xfstests/commits/connectable-fh/ [2] https://github.com/amir73il/man-pages/commits/connectable-fh/ * patches from https://lore.kernel.org/r/20241011090023.655623-1-amir73il@gmail.com: fs: open_by_handle_at() support for decoding "explicit connectable" file handles fs: name_to_handle_at() support for "explicit connectable" file handles fs: prepare for "explicit connectable" file handles Link: https://lore.kernel.org/r/20241011090023.655623-1-amir73il@gmail.com Signed-off-by: Christian Brauner <brauner@kernel.org>
2 parents 8e929cb + a20853a commit a312c10

File tree

4 files changed

+98
-8
lines changed

4 files changed

+98
-8
lines changed

fs/exportfs/expfs.c

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -382,14 +382,24 @@ int exportfs_encode_inode_fh(struct inode *inode, struct fid *fid,
382382
int *max_len, struct inode *parent, int flags)
383383
{
384384
const struct export_operations *nop = inode->i_sb->s_export_op;
385+
enum fid_type type;
385386

386387
if (!exportfs_can_encode_fh(nop, flags))
387388
return -EOPNOTSUPP;
388389

389390
if (!nop && (flags & EXPORT_FH_FID))
390-
return exportfs_encode_ino64_fid(inode, fid, max_len);
391+
type = exportfs_encode_ino64_fid(inode, fid, max_len);
392+
else
393+
type = nop->encode_fh(inode, fid->raw, max_len, parent);
394+
395+
if (type > 0 && FILEID_USER_FLAGS(type)) {
396+
pr_warn_once("%s: unexpected fh type value 0x%x from fstype %s.\n",
397+
__func__, type, inode->i_sb->s_type->name);
398+
return -EINVAL;
399+
}
400+
401+
return type;
391402

392-
return nop->encode_fh(inode, fid->raw, max_len, parent);
393403
}
394404
EXPORT_SYMBOL_GPL(exportfs_encode_inode_fh);
395405

@@ -436,6 +446,9 @@ exportfs_decode_fh_raw(struct vfsmount *mnt, struct fid *fid, int fh_len,
436446
char nbuf[NAME_MAX+1];
437447
int err;
438448

449+
if (fileid_type < 0 || FILEID_USER_FLAGS(fileid_type))
450+
return ERR_PTR(-EINVAL);
451+
439452
/*
440453
* Try to get any dentry for the given file handle from the filesystem.
441454
*/

fs/fhandle.c

Lines changed: 69 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,14 @@ static long do_sys_name_to_handle(const struct path *path,
3131
if (!exportfs_can_encode_fh(path->dentry->d_sb->s_export_op, fh_flags))
3232
return -EOPNOTSUPP;
3333

34+
/*
35+
* A request to encode a connectable handle for a disconnected dentry
36+
* is unexpected since AT_EMPTY_PATH is not allowed.
37+
*/
38+
if (fh_flags & EXPORT_FH_CONNECTABLE &&
39+
WARN_ON(path->dentry->d_flags & DCACHE_DISCONNECTED))
40+
return -EINVAL;
41+
3442
if (copy_from_user(&f_handle, ufh, sizeof(struct file_handle)))
3543
return -EFAULT;
3644

@@ -45,7 +53,7 @@ static long do_sys_name_to_handle(const struct path *path,
4553
/* convert handle size to multiple of sizeof(u32) */
4654
handle_dwords = f_handle.handle_bytes >> 2;
4755

48-
/* we ask for a non connectable maybe decodeable file handle */
56+
/* Encode a possibly decodeable/connectable file handle */
4957
retval = exportfs_encode_fh(path->dentry,
5058
(struct fid *)handle->f_handle,
5159
&handle_dwords, fh_flags);
@@ -67,8 +75,23 @@ static long do_sys_name_to_handle(const struct path *path,
6775
* non variable part of the file_handle
6876
*/
6977
handle_bytes = 0;
70-
} else
78+
} else {
79+
/*
80+
* When asked to encode a connectable file handle, encode this
81+
* property in the file handle itself, so that we later know
82+
* how to decode it.
83+
* For sanity, also encode in the file handle if the encoded
84+
* object is a directory and verify this during decode, because
85+
* decoding directory file handles is quite different than
86+
* decoding connectable non-directory file handles.
87+
*/
88+
if (fh_flags & EXPORT_FH_CONNECTABLE) {
89+
handle->handle_type |= FILEID_IS_CONNECTABLE;
90+
if (d_is_dir(path->dentry))
91+
fh_flags |= FILEID_IS_DIR;
92+
}
7193
retval = 0;
94+
}
7295
/* copy the mount id */
7396
if (unique_mntid) {
7497
if (put_user(real_mount(path->mnt)->mnt_id_unique,
@@ -109,15 +132,30 @@ SYSCALL_DEFINE5(name_to_handle_at, int, dfd, const char __user *, name,
109132
{
110133
struct path path;
111134
int lookup_flags;
112-
int fh_flags;
135+
int fh_flags = 0;
113136
int err;
114137

115138
if (flag & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH | AT_HANDLE_FID |
116-
AT_HANDLE_MNT_ID_UNIQUE))
139+
AT_HANDLE_MNT_ID_UNIQUE | AT_HANDLE_CONNECTABLE))
140+
return -EINVAL;
141+
142+
/*
143+
* AT_HANDLE_FID means there is no intention to decode file handle
144+
* AT_HANDLE_CONNECTABLE means there is an intention to decode a
145+
* connected fd (with known path), so these flags are conflicting.
146+
* AT_EMPTY_PATH could be used along with a dfd that refers to a
147+
* disconnected non-directory, which cannot be used to encode a
148+
* connectable file handle, because its parent is unknown.
149+
*/
150+
if (flag & AT_HANDLE_CONNECTABLE &&
151+
flag & (AT_HANDLE_FID | AT_EMPTY_PATH))
117152
return -EINVAL;
153+
else if (flag & AT_HANDLE_FID)
154+
fh_flags |= EXPORT_FH_FID;
155+
else if (flag & AT_HANDLE_CONNECTABLE)
156+
fh_flags |= EXPORT_FH_CONNECTABLE;
118157

119158
lookup_flags = (flag & AT_SYMLINK_FOLLOW) ? LOOKUP_FOLLOW : 0;
120-
fh_flags = (flag & AT_HANDLE_FID) ? EXPORT_FH_FID : 0;
121159
if (flag & AT_EMPTY_PATH)
122160
lookup_flags |= LOOKUP_EMPTY;
123161
err = user_path_at(dfd, name, lookup_flags, &path);
@@ -208,7 +246,13 @@ static int vfs_dentry_acceptable(void *context, struct dentry *dentry)
208246

209247
if (!(ctx->flags & HANDLE_CHECK_SUBTREE) || d == root)
210248
retval = 1;
211-
WARN_ON_ONCE(d != root && d != root->d_sb->s_root);
249+
/*
250+
* exportfs_decode_fh_raw() does not call acceptable() callback with
251+
* a disconnected directory dentry, so we should have reached either
252+
* mount fd directory or sb root.
253+
*/
254+
if (ctx->fh_flags & EXPORT_FH_DIR_ONLY)
255+
WARN_ON_ONCE(d != root && d != root->d_sb->s_root);
212256
dput(d);
213257
return retval;
214258
}
@@ -307,6 +351,12 @@ static int handle_to_path(int mountdirfd, struct file_handle __user *ufh,
307351
retval = -EINVAL;
308352
goto out_path;
309353
}
354+
if (f_handle.handle_type < 0 ||
355+
FILEID_USER_FLAGS(f_handle.handle_type) & ~FILEID_VALID_USER_FLAGS) {
356+
retval = -EINVAL;
357+
goto out_path;
358+
}
359+
310360
handle = kmalloc(struct_size(handle, f_handle, f_handle.handle_bytes),
311361
GFP_KERNEL);
312362
if (!handle) {
@@ -322,6 +372,19 @@ static int handle_to_path(int mountdirfd, struct file_handle __user *ufh,
322372
goto out_handle;
323373
}
324374

375+
/*
376+
* If handle was encoded with AT_HANDLE_CONNECTABLE, verify that we
377+
* are decoding an fd with connected path, which is accessible from
378+
* the mount fd path.
379+
*/
380+
if (f_handle.handle_type & FILEID_IS_CONNECTABLE) {
381+
ctx.fh_flags |= EXPORT_FH_CONNECTABLE;
382+
ctx.flags |= HANDLE_CHECK_SUBTREE;
383+
}
384+
if (f_handle.handle_type & FILEID_IS_DIR)
385+
ctx.fh_flags |= EXPORT_FH_DIR_ONLY;
386+
/* Filesystem code should not be exposed to user flags */
387+
handle->handle_type &= ~FILEID_USER_FLAGS_MASK;
325388
retval = do_handle_to_path(handle, path, &ctx);
326389

327390
out_handle:

include/linux/exportfs.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,19 @@ struct fid {
160160
#define EXPORT_FH_FID 0x2 /* File handle may be non-decodeable */
161161
#define EXPORT_FH_DIR_ONLY 0x4 /* Only decode file handle for a directory */
162162

163+
/*
164+
* Filesystems use only lower 8 bits of file_handle type for fid_type.
165+
* name_to_handle_at() uses upper 16 bits of type as user flags to be
166+
* interpreted by open_by_handle_at().
167+
*/
168+
#define FILEID_USER_FLAGS_MASK 0xffff0000
169+
#define FILEID_USER_FLAGS(type) ((type) & FILEID_USER_FLAGS_MASK)
170+
171+
/* Flags supported in encoded handle_type that is exported to user */
172+
#define FILEID_IS_CONNECTABLE 0x10000
173+
#define FILEID_IS_DIR 0x20000
174+
#define FILEID_VALID_USER_FLAGS (FILEID_IS_CONNECTABLE | FILEID_IS_DIR)
175+
163176
/**
164177
* struct export_operations - for nfsd to communicate with file systems
165178
* @encode_fh: encode a file handle fragment from a dentry

include/uapi/linux/fcntl.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@
153153
object identity and may not be
154154
usable with open_by_handle_at(2). */
155155
#define AT_HANDLE_MNT_ID_UNIQUE 0x001 /* Return the u64 unique mount ID. */
156+
#define AT_HANDLE_CONNECTABLE 0x002 /* Request a connectable file handle */
156157

157158
#if defined(__KERNEL__)
158159
#define AT_GETATTR_NOSEC 0x80000000

0 commit comments

Comments
 (0)