From: John Groves <[email protected]>

Famfs file maps (fmaps) may reference multiple daxdevs. Before passing
an fmap that references a new daxdev, the daxdev is pushed into the
kernel via FAMFSIOC_DAXDEV_OPEN). This adds daxdevs to daxdev_table for
index-based resolution from famfs extents to daxdevs.

Signed-off-by: John Groves <[email protected]>
---
v13:
 - famfs_daxdev_open(): declare path with __free(kfree) and drop the goto/out
   label so it is released on every return path, per the cleanup.h guidance
   against mixing goto-unwinding with scope-based helpers (Sashiko bot).

 fs/famfs/famfs_file.c            | 74 ++++++++++++++++++++++++++++++++
 include/uapi/linux/famfs_ioctl.h | 24 +++++++++++
 2 files changed, 98 insertions(+)

diff --git a/fs/famfs/famfs_file.c b/fs/famfs/famfs_file.c
index 670c8f961f11..6f71805a297c 100644
--- a/fs/famfs/famfs_file.c
+++ b/fs/famfs/famfs_file.c
@@ -289,6 +289,76 @@ famfs_file_init_dax(struct file *file, void __user *arg)
        return rc;
 }
 
+/**
+ * famfs_daxdev_open() - FAMFSIOC_DAXDEV_OPEN ioctl handler
+ * @file: any file in the famfs mount (the table is per-superblock)
+ * @arg:  ptr to struct famfs_ioc_daxdev in user space
+ *
+ * Register a devdax device (identified by path) into the mount's daxdev table
+ * at the caller-specified index, so files whose extents reference that index
+ * can be mapped. The path is resolved by famfs_lookup_daxdev() - the same
+ * helper the mount uses for the primary daxdev - so every slot is resolved
+ * identically. Registering exposes raw device memory, so it requires
+ * CAP_SYS_RAWIO.
+ */
+static int
+famfs_daxdev_open(struct file *file, void __user *arg)
+{
+       struct super_block *sb = file_inode(file)->i_sb;
+       struct famfs_fs_info *fsi = sb->s_fs_info;
+       struct famfs_ioc_daxdev dd;
+       dev_t devno;
+       int rc;
+
+       char *path __free(kfree) = NULL;
+
+       if (!capable(CAP_SYS_RAWIO))
+               return -EPERM;
+
+       if (copy_from_user(&dd, arg, sizeof(dd)))
+               return -EFAULT;
+
+       /* @flags is reserved; reject non-zero so it stays available */
+       if (dd.flags)
+               return -EINVAL;
+
+       /*
+        * If this daxdev index is already populated there is nothing to do.
+        * The index is cluster-invariant, so a valid slot already names this
+        * device; skip the path resolution entirely. install_daxdev() rechecks
+        * ->valid under the write lock, so this is purely an optimization.
+        */
+       scoped_guard(rwsem_read, &fsi->devlist_sem) {
+               if (dd.daxdev_index >= fsi->dax_devlist->nslots)
+                       return -EINVAL;
+               if (fsi->dax_devlist->devlist[dd.daxdev_index].valid)
+                       return 0;
+       }
+
+       if (dd.daxdev_path_len == 0 || dd.daxdev_path_len >= PATH_MAX)
+               return -EINVAL;
+
+       /* +1 so the terminating NUL is included within the bound */
+       path = strndup_user((const char __user *)(uintptr_t)dd.daxdev_path,
+                           dd.daxdev_path_len + 1);
+       if (IS_ERR(path))
+               return PTR_ERR(no_free_ptr(path));
+
+       rc = famfs_lookup_daxdev(path, &devno);
+       if (rc)
+               return rc;
+
+       /*
+        * The daxdev table is allocated at mount time (for the slot-0 primary),
+        * so it is always present here; no need to allocate it.
+        */
+       rc = famfs_install_daxdev(fsi, sb, dd.daxdev_index, devno, path);
+       if (rc)
+               pr_debug("%s: failed to install daxdev index %llu (%s)\n",
+                      __func__, dd.daxdev_index, path);
+       return rc;
+}
+
 /**
  * famfs_file_ioctl() - Top-level famfs file ioctl handler
  * @file: the file
@@ -310,6 +380,10 @@ famfs_file_ioctl(struct file *file, unsigned int cmd, 
unsigned long arg)
                rc = 0;
                break;
 
+       case FAMFSIOC_DAXDEV_OPEN:
+               rc = famfs_daxdev_open(file, (void __user *)arg);
+               break;
+
        case FAMFSIOC_MAP_CREATE:
                rc = famfs_file_init_dax(file, (void __user *)arg);
                break;
diff --git a/include/uapi/linux/famfs_ioctl.h b/include/uapi/linux/famfs_ioctl.h
index b4eb373c1ade..751d8b033c2e 100644
--- a/include/uapi/linux/famfs_ioctl.h
+++ b/include/uapi/linux/famfs_ioctl.h
@@ -77,6 +77,29 @@ struct famfs_ioc_fmap_header {
        __u64 reserved1;
 };
 
+/**
+ * struct famfs_ioc_daxdev - register an additional backing daxdev by path
+ * @daxdev_index:    the (cluster-invariant) index this daxdev occupies in
+ *                   extent dev_index fields. Index 0 is the mount-time 
primary.
+ * @daxdev_path:     userspace pointer to the devdax device path (e.g.
+ *                   "/dev/dax0.0"); resolved in the kernel the same way the
+ *                   mount primary is.
+ * @daxdev_path_len: length of the path string, not counting the NUL.
+ * @flags:           reserved; must be zero.
+ *
+ * Standalone famfs registers every daxdev by path: the mount primary comes in
+ * as the mount device name, and slots 1..n come in here. (This deliberately
+ * differs from fuse's fd-based FUSE_DEV_IOC_DAXDEV_OPEN; each side is uniform
+ * within itself.) Passing the path by pointer keeps the struct fixed-size, so
+ * longer paths never require an ABI change.
+ */
+struct famfs_ioc_daxdev {
+       __u64 daxdev_index;
+       __u64 daxdev_path;
+       __u32 daxdev_path_len;
+       __u32 flags;
+};
+
 #define FAMFSIOC_MAGIC 'u'
 
 /* famfs file ioctl opcodes */
@@ -87,5 +110,6 @@ struct famfs_ioc_fmap_header {
  * famfs_ioc_fmap_header followed by the extent list (see above).
  */
 #define FAMFSIOC_MAP_CREATE    _IOW(FAMFSIOC_MAGIC,  0x51, struct 
famfs_ioc_fmap_header)
+#define FAMFSIOC_DAXDEV_OPEN   _IOW(FAMFSIOC_MAGIC,  0x52, struct 
famfs_ioc_daxdev)
 
 #endif /* FAMFS_IOCTL_H */
-- 
2.53.0


Reply via email to