exportfs: add explicit flag to request non-decodeable file handles
authorAmir Goldstein <amir73il@gmail.com>
Tue, 2 May 2023 12:48:15 +0000 (15:48 +0300)
committerJan Kara <jack@suse.cz>
Mon, 22 May 2023 16:08:37 +0000 (18:08 +0200)
So far, all callers of exportfs_encode_inode_fh(), except for fsnotify's
show_mark_fhandle(), check that filesystem can decode file handles, but
we would like to add more callers that do not require a file handle that
can be decoded.

Introduce a flag to explicitly request a file handle that may not to be
decoded later and a wrapper exportfs_encode_fid() that sets this flag
and convert show_mark_fhandle() to use the new wrapper.

This will be used to allow adding fanotify support to filesystems that
do not support NFS export.

Acked-by: Jeff Layton <jlayton@kernel.org>
Acked-by: Chuck Lever <chuck.lever@oracle.com>
Signed-off-by: Amir Goldstein <amir73il@gmail.com>
Signed-off-by: Jan Kara <jack@suse.cz>
Message-Id: <20230502124817.3070545-3-amir73il@gmail.com>

Documentation/filesystems/nfs/exporting.rst
fs/exportfs/expfs.c
fs/notify/fanotify/fanotify.c
fs/notify/fdinfo.c
include/linux/exportfs.h

index 0e98edd353b5f13920ec0837525e371bf16815ee..3d97b8d8f7354490f152254b3e446b8bfb7bb8f8 100644 (file)
@@ -122,8 +122,8 @@ are exportable by setting the s_export_op field in the struct
 super_block.  This field must point to a "struct export_operations"
 struct which has the following members:
 
encode_fh  (optional)
-    Takes a dentry and creates a filehandle fragment which can later be used
 encode_fh (optional)
+    Takes a dentry and creates a filehandle fragment which may later be used
     to find or create a dentry for the same object.  The default
     implementation creates a filehandle fragment that encodes a 32bit inode
     and generation number for the inode encoded, and if necessary the
index ab7feffe2d191248c5299551931efe8f2a7e2c61..40e624cf7e929194bb4ef9ff2d17519652e6b663 100644 (file)
@@ -381,11 +381,27 @@ static int export_encode_fh(struct inode *inode, struct fid *fid,
        return type;
 }
 
+/**
+ * exportfs_encode_inode_fh - encode a file handle from inode
+ * @inode:   the object to encode
+ * @fid:     where to store the file handle fragment
+ * @max_len: maximum length to store there
+ * @flags:   properties of the requested file handle
+ *
+ * Returns an enum fid_type or a negative errno.
+ */
 int exportfs_encode_inode_fh(struct inode *inode, struct fid *fid,
-                            int *max_len, struct inode *parent)
+                            int *max_len, struct inode *parent, int flags)
 {
        const struct export_operations *nop = inode->i_sb->s_export_op;
 
+       /*
+        * If a decodeable file handle was requested, we need to make sure that
+        * filesystem can decode file handles.
+        */
+       if (nop && !(flags & EXPORT_FH_FID) && !nop->fh_to_dentry)
+               return -EOPNOTSUPP;
+
        if (nop && nop->encode_fh)
                return nop->encode_fh(inode, fid->raw, max_len, parent);
 
@@ -418,7 +434,7 @@ int exportfs_encode_fh(struct dentry *dentry, struct fid *fid, int *max_len,
                parent = p->d_inode;
        }
 
-       error = exportfs_encode_inode_fh(inode, fid, max_len, parent);
+       error = exportfs_encode_inode_fh(inode, fid, max_len, parent, flags);
        dput(p);
 
        return error;
index 29bdd99b29fae05dddb30a5ef706b677ffeb5014..d1a49f5b6e6d89afc8793c7de6c05db99b35b1ff 100644 (file)
@@ -380,7 +380,7 @@ static int fanotify_encode_fh_len(struct inode *inode)
        if (!inode)
                return 0;
 
-       exportfs_encode_inode_fh(inode, NULL, &dwords, NULL);
+       exportfs_encode_inode_fh(inode, NULL, &dwords, NULL, 0);
        fh_len = dwords << 2;
 
        /*
@@ -443,7 +443,7 @@ static int fanotify_encode_fh(struct fanotify_fh *fh, struct inode *inode,
        }
 
        dwords = fh_len >> 2;
-       type = exportfs_encode_inode_fh(inode, buf, &dwords, NULL);
+       type = exportfs_encode_inode_fh(inode, buf, &dwords, NULL, 0);
        err = -EINVAL;
        if (!type || type == FILEID_INVALID || fh_len != dwords << 2)
                goto out_err;
index 55081ae3a6ec0d5ace8135e06f92184b4990d8c1..5c430736ec12c40d8385beab659f9e0410ac508f 100644 (file)
@@ -50,7 +50,7 @@ static void show_mark_fhandle(struct seq_file *m, struct inode *inode)
        f.handle.handle_bytes = sizeof(f.pad);
        size = f.handle.handle_bytes >> 2;
 
-       ret = exportfs_encode_inode_fh(inode, (struct fid *)f.handle.f_handle, &size, NULL);
+       ret = exportfs_encode_fid(inode, (struct fid *)f.handle.f_handle, &size);
        if ((ret == FILEID_INVALID) || (ret < 0)) {
                WARN_ONCE(1, "Can't encode file handler for inotify: %d\n", ret);
                return;
index fe4967ba61b206f142e2e73b5f510aa665134e1f..11fbd0ee1370809f9f1770aba8e8e3d3cec27be5 100644 (file)
@@ -136,6 +136,7 @@ struct fid {
 };
 
 #define EXPORT_FH_CONNECTABLE  0x1 /* Encode file handle with parent */
+#define EXPORT_FH_FID          0x2 /* File handle may be non-decodeable */
 
 /**
  * struct export_operations - for nfsd to communicate with file systems
@@ -227,9 +228,18 @@ struct export_operations {
 };
 
 extern int exportfs_encode_inode_fh(struct inode *inode, struct fid *fid,
-                                   int *max_len, struct inode *parent);
+                                   int *max_len, struct inode *parent,
+                                   int flags);
 extern int exportfs_encode_fh(struct dentry *dentry, struct fid *fid,
                              int *max_len, int flags);
+
+static inline int exportfs_encode_fid(struct inode *inode, struct fid *fid,
+                                     int *max_len)
+{
+       return exportfs_encode_inode_fh(inode, fid, max_len, NULL,
+                                       EXPORT_FH_FID);
+}
+
 extern struct dentry *exportfs_decode_fh_raw(struct vfsmount *mnt,
                                             struct fid *fid, int fh_len,
                                             int fileid_type,