Skip to content

Commit 66c6276

Browse files
amir73ilbrauner
authored andcommitted
exportfs: add helpers to check if filesystem can encode/decode file handles
The logic of whether filesystem can encode/decode file handles is open coded in many places. In preparation to changing the logic, move the open coded logic into inline helpers. Reviewed-by: Jan Kara <[email protected]> Reviewed-by: Jeff Layton <[email protected]> Signed-off-by: Amir Goldstein <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Christian Brauner <[email protected]>
1 parent 05d3ef8 commit 66c6276

File tree

6 files changed

+34
-16
lines changed

6 files changed

+34
-16
lines changed

fs/exportfs/expfs.c

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -396,11 +396,7 @@ int exportfs_encode_inode_fh(struct inode *inode, struct fid *fid,
396396
{
397397
const struct export_operations *nop = inode->i_sb->s_export_op;
398398

399-
/*
400-
* If a decodeable file handle was requested, we need to make sure that
401-
* filesystem can decode file handles.
402-
*/
403-
if (nop && !(flags & EXPORT_FH_FID) && !nop->fh_to_dentry)
399+
if (!exportfs_can_encode_fh(nop, flags))
404400
return -EOPNOTSUPP;
405401

406402
if (nop && nop->encode_fh)
@@ -456,7 +452,7 @@ exportfs_decode_fh_raw(struct vfsmount *mnt, struct fid *fid, int fh_len,
456452
/*
457453
* Try to get any dentry for the given file handle from the filesystem.
458454
*/
459-
if (!nop || !nop->fh_to_dentry)
455+
if (!exportfs_can_decode_fh(nop))
460456
return ERR_PTR(-ESTALE);
461457
result = nop->fh_to_dentry(mnt->mnt_sb, fid, fh_len, fileid_type);
462458
if (IS_ERR_OR_NULL(result))

fs/fhandle.c

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,8 @@ static long do_sys_name_to_handle(const struct path *path,
2626
/*
2727
* We need to make sure whether the file system support decoding of
2828
* the file handle if decodeable file handle was requested.
29-
* Otherwise, even empty export_operations are sufficient to opt-in
30-
* to encoding FIDs.
3129
*/
32-
if (!path->dentry->d_sb->s_export_op ||
33-
(!(fh_flags & EXPORT_FH_FID) &&
34-
!path->dentry->d_sb->s_export_op->fh_to_dentry))
30+
if (!exportfs_can_encode_fh(path->dentry->d_sb->s_export_op, fh_flags))
3531
return -EOPNOTSUPP;
3632

3733
if (copy_from_user(&f_handle, ufh, sizeof(struct file_handle)))

fs/nfsd/export.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -421,8 +421,7 @@ static int check_export(struct path *path, int *flags, unsigned char *uuid)
421421
return -EINVAL;
422422
}
423423

424-
if (!inode->i_sb->s_export_op ||
425-
!inode->i_sb->s_export_op->fh_to_dentry) {
424+
if (!exportfs_can_decode_fh(inode->i_sb->s_export_op)) {
426425
dprintk("exp_export: export of invalid fs type.\n");
427426
return -EINVAL;
428427
}

fs/notify/fanotify/fanotify_user.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1595,15 +1595,15 @@ static int fanotify_test_fid(struct dentry *dentry, unsigned int flags)
15951595
* file handles so user can use name_to_handle_at() to compare fids
15961596
* reported with events to the file handle of watched objects.
15971597
*/
1598-
if (!nop)
1598+
if (!exportfs_can_encode_fid(nop))
15991599
return -EOPNOTSUPP;
16001600

16011601
/*
16021602
* For sb/mount mark, we also need to make sure that the filesystem
16031603
* supports decoding file handles, so user has a way to map back the
16041604
* reported fids to filesystem objects.
16051605
*/
1606-
if (mark_type != FAN_MARK_INODE && !nop->fh_to_dentry)
1606+
if (mark_type != FAN_MARK_INODE && !exportfs_can_decode_fh(nop))
16071607
return -EOPNOTSUPP;
16081608

16091609
return 0;

fs/overlayfs/util.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ int ovl_can_decode_fh(struct super_block *sb)
5555
if (!capable(CAP_DAC_READ_SEARCH))
5656
return 0;
5757

58-
if (!sb->s_export_op || !sb->s_export_op->fh_to_dentry)
58+
if (!exportfs_can_decode_fh(sb->s_export_op))
5959
return 0;
6060

6161
return sb->s_export_op->encode_fh ? -1 : FILEID_INO32_GEN;

include/linux/exportfs.h

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,33 @@ extern int exportfs_encode_inode_fh(struct inode *inode, struct fid *fid,
233233
extern int exportfs_encode_fh(struct dentry *dentry, struct fid *fid,
234234
int *max_len, int flags);
235235

236+
static inline bool exportfs_can_encode_fid(const struct export_operations *nop)
237+
{
238+
return nop;
239+
}
240+
241+
static inline bool exportfs_can_decode_fh(const struct export_operations *nop)
242+
{
243+
return nop && nop->fh_to_dentry;
244+
}
245+
246+
static inline bool exportfs_can_encode_fh(const struct export_operations *nop,
247+
int fh_flags)
248+
{
249+
/*
250+
* If a non-decodeable file handle was requested, we only need to make
251+
* sure that filesystem can encode file handles.
252+
*/
253+
if (fh_flags & EXPORT_FH_FID)
254+
return exportfs_can_encode_fid(nop);
255+
256+
/*
257+
* If a decodeable file handle was requested, we need to make sure that
258+
* filesystem can also decode file handles.
259+
*/
260+
return exportfs_can_decode_fh(nop);
261+
}
262+
236263
static inline int exportfs_encode_fid(struct inode *inode, struct fid *fid,
237264
int *max_len)
238265
{

0 commit comments

Comments
 (0)