Skip to content

Commit c374196

Browse files
amir73ilbrauner
authored andcommitted
fs: name_to_handle_at() support for "explicit connectable" file handles
nfsd encodes "connectable" file handles for the subtree_check feature, which can be resolved to an open file with a connected path. So far, userspace nfs server could not make use of this functionality. Introduce a new flag AT_HANDLE_CONNECTABLE to name_to_handle_at(2). When used, the encoded file handle is "explicitly connectable". The "explicitly connectable" file handle sets bits in the high 16bit of the handle_type field, so open_by_handle_at(2) will know that it needs to open a file with a connected path. old kernels will now recognize the handle_type with high bits set, so "explicitly connectable" file handles cannot be decoded by open_by_handle_at(2) on old kernels. The flag AT_HANDLE_CONNECTABLE is not allowed together with either AT_HANDLE_FID or AT_EMPTY_PATH. Signed-off-by: Amir Goldstein <[email protected]> Link: https://lore.kernel.org/r/[email protected] Fixes: 570df4e ("ceph: snapshot nfs re-export") Acked-by: Reviewed-by: Jeff Layton <[email protected]> Signed-off-by: Christian Brauner <[email protected]>
1 parent 4a530a7 commit c374196

File tree

3 files changed

+46
-5
lines changed

3 files changed

+46
-5
lines changed

fs/fhandle.c

Lines changed: 43 additions & 5 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);

include/linux/exportfs.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,8 @@ struct fid {
169169
#define FILEID_USER_FLAGS(type) ((type) & FILEID_USER_FLAGS_MASK)
170170

171171
/* Flags supported in encoded handle_type that is exported to user */
172+
#define FILEID_IS_CONNECTABLE 0x10000
173+
#define FILEID_IS_DIR 0x20000
172174
#define FILEID_VALID_USER_FLAGS (0)
173175

174176
/**

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)