debugfs: Provide a file creation function that also takes an initial size
authorDavid Howells <dhowells@redhat.com>
Wed, 21 Jan 2015 20:03:40 +0000 (20:03 +0000)
committerAl Viro <viro@zeniv.linux.org.uk>
Tue, 17 Feb 2015 17:21:51 +0000 (12:21 -0500)
Provide a file creation function that also takes an initial size so that the
caller doesn't have to set i_size, thus meaning that we don't have to call
deal with ->d_inode in the callers.

Signed-off-by: David Howells <dhowells@redhat.com>
Signed-off-by: Al Viro <viro@zeniv.linux.org.uk>
drivers/infiniband/hw/cxgb4/device.c
drivers/net/ethernet/chelsio/cxgb4/cxgb4_debugfs.c
drivers/scsi/csiostor/csio_init.c
drivers/usb/gadget/udc/atmel_usba_udc.c
fs/debugfs/inode.c
include/linux/debugfs.h

index eb5df4e62703d8adb254a9a4f6163a90d40ebd18..391309a553607a0b1acb83df78cc2aa8ee1d4849 100644 (file)
@@ -700,37 +700,24 @@ static const struct file_operations ep_debugfs_fops = {
 
 static int setup_debugfs(struct c4iw_dev *devp)
 {
-       struct dentry *de;
-
        if (!devp->debugfs_root)
                return -1;
 
-       de = debugfs_create_file("qps", S_IWUSR, devp->debugfs_root,
-                                (void *)devp, &qp_debugfs_fops);
-       if (de && de->d_inode)
-               de->d_inode->i_size = 4096;
+       debugfs_create_file_size("qps", S_IWUSR, devp->debugfs_root,
+                                (void *)devp, &qp_debugfs_fops, 4096);
 
-       de = debugfs_create_file("stags", S_IWUSR, devp->debugfs_root,
-                                (void *)devp, &stag_debugfs_fops);
-       if (de && de->d_inode)
-               de->d_inode->i_size = 4096;
+       debugfs_create_file_size("stags", S_IWUSR, devp->debugfs_root,
+                                (void *)devp, &stag_debugfs_fops, 4096);
 
-       de = debugfs_create_file("stats", S_IWUSR, devp->debugfs_root,
-                       (void *)devp, &stats_debugfs_fops);
-       if (de && de->d_inode)
-               de->d_inode->i_size = 4096;
+       debugfs_create_file_size("stats", S_IWUSR, devp->debugfs_root,
+                                (void *)devp, &stats_debugfs_fops, 4096);
 
-       de = debugfs_create_file("eps", S_IWUSR, devp->debugfs_root,
-                       (void *)devp, &ep_debugfs_fops);
-       if (de && de->d_inode)
-               de->d_inode->i_size = 4096;
+       debugfs_create_file_size("eps", S_IWUSR, devp->debugfs_root,
+                                (void *)devp, &ep_debugfs_fops, 4096);
 
-       if (c4iw_wr_log) {
-               de = debugfs_create_file("wr_log", S_IWUSR, devp->debugfs_root,
-                                        (void *)devp, &wr_log_debugfs_fops);
-               if (de && de->d_inode)
-                       de->d_inode->i_size = 4096;
-       }
+       if (c4iw_wr_log)
+               debugfs_create_file_size("wr_log", S_IWUSR, devp->debugfs_root,
+                                        (void *)devp, &wr_log_debugfs_fops, 4096);
        return 0;
 }
 
index c98a350d857e27ac3f3216765e6ec797f689d908..4c7fe447e4071f573f6f0c6077603e42a7df291b 100644 (file)
@@ -91,12 +91,9 @@ static const struct file_operations mem_debugfs_fops = {
 static void add_debugfs_mem(struct adapter *adap, const char *name,
                            unsigned int idx, unsigned int size_mb)
 {
-       struct dentry *de;
-
-       de = debugfs_create_file(name, S_IRUSR, adap->debugfs_root,
-                                (void *)adap + idx, &mem_debugfs_fops);
-       if (de && de->d_inode)
-               de->d_inode->i_size = size_mb << 20;
+       debugfs_create_file_size(name, S_IRUSR, adap->debugfs_root,
+                                (void *)adap + idx, &mem_debugfs_fops,
+                                size_mb << 20);
 }
 
 /* Add an array of Debug FS files.
index 34d20cc3e110fcc1ec99aad913b74e87651734e0..2617f15eaeeb0cfb48dcce35032338258ccab575 100644 (file)
@@ -113,12 +113,9 @@ static const struct file_operations csio_mem_debugfs_fops = {
 void csio_add_debugfs_mem(struct csio_hw *hw, const char *name,
                                 unsigned int idx, unsigned int size_mb)
 {
-       struct dentry *de;
-
-       de = debugfs_create_file(name, S_IRUSR, hw->debugfs_root,
-                                (void *)hw + idx, &csio_mem_debugfs_fops);
-       if (de && de->d_inode)
-               de->d_inode->i_size = size_mb << 20;
+       debugfs_create_file_size(name, S_IRUSR, hw->debugfs_root,
+                                (void *)hw + idx, &csio_mem_debugfs_fops,
+                                size_mb << 20);
 }
 
 static int csio_setup_debugfs(struct csio_hw *hw)
index 9f93bed42052cb5c910d5f34218287e58a558c6b..b2239a2d49412f12c8ee8f169fc2c21da7c7e141 100644 (file)
@@ -264,14 +264,17 @@ static void usba_init_debugfs(struct usba_udc *udc)
                goto err_root;
        udc->debugfs_root = root;
 
-       regs = debugfs_create_file("regs", 0400, root, udc, &regs_dbg_fops);
-       if (!regs)
-               goto err_regs;
-
        regs_resource = platform_get_resource(udc->pdev, IORESOURCE_MEM,
                                CTRL_IOMEM_ID);
-       regs->d_inode->i_size = resource_size(regs_resource);
-       udc->debugfs_regs = regs;
+
+       if (regs_resource) {
+               regs = debugfs_create_file_size("regs", 0400, root, udc,
+                                               &regs_dbg_fops,
+                                               resource_size(regs_resource));
+               if (!regs)
+                       goto err_regs;
+               udc->debugfs_regs = regs;
+       }
 
        usba_ep_init_debugfs(udc, to_usba_ep(udc->gadget.ep0));
 
index 957c40ce09f62d02a3a4039f5a5dd91ff30c5345..45b18a5e225c3bc6fffe927cbad3257ccafc2109 100644 (file)
@@ -337,6 +337,46 @@ struct dentry *debugfs_create_file(const char *name, umode_t mode,
 }
 EXPORT_SYMBOL_GPL(debugfs_create_file);
 
+/**
+ * debugfs_create_file_size - create a file in the debugfs filesystem
+ * @name: a pointer to a string containing the name of the file to create.
+ * @mode: the permission that the file should have.
+ * @parent: a pointer to the parent dentry for this file.  This should be a
+ *          directory dentry if set.  If this parameter is NULL, then the
+ *          file will be created in the root of the debugfs filesystem.
+ * @data: a pointer to something that the caller will want to get to later
+ *        on.  The inode.i_private pointer will point to this value on
+ *        the open() call.
+ * @fops: a pointer to a struct file_operations that should be used for
+ *        this file.
+ * @file_size: initial file size
+ *
+ * This is the basic "create a file" function for debugfs.  It allows for a
+ * wide range of flexibility in creating a file, or a directory (if you want
+ * to create a directory, the debugfs_create_dir() function is
+ * recommended to be used instead.)
+ *
+ * This function will return a pointer to a dentry if it succeeds.  This
+ * pointer must be passed to the debugfs_remove() function when the file is
+ * to be removed (no automatic cleanup happens if your module is unloaded,
+ * you are responsible here.)  If an error occurs, %NULL will be returned.
+ *
+ * If debugfs is not enabled in the kernel, the value -%ENODEV will be
+ * returned.
+ */
+struct dentry *debugfs_create_file_size(const char *name, umode_t mode,
+                                       struct dentry *parent, void *data,
+                                       const struct file_operations *fops,
+                                       loff_t file_size)
+{
+       struct dentry *de = debugfs_create_file(name, mode, parent, data, fops);
+
+       if (de)
+               de->d_inode->i_size = file_size;
+       return de;
+}
+EXPORT_SYMBOL_GPL(debugfs_create_file_size);
+
 /**
  * debugfs_create_dir - create a directory in the debugfs filesystem
  * @name: a pointer to a string containing the name of the directory to
index ea149a24a1f20008eb0d6709beebad4971e5cef4..cb25af46105406908e5a825ca3812f0ff63d3194 100644 (file)
@@ -51,6 +51,11 @@ struct dentry *debugfs_create_file(const char *name, umode_t mode,
                                   struct dentry *parent, void *data,
                                   const struct file_operations *fops);
 
+struct dentry *debugfs_create_file_size(const char *name, umode_t mode,
+                                       struct dentry *parent, void *data,
+                                       const struct file_operations *fops,
+                                       loff_t file_size);
+
 struct dentry *debugfs_create_dir(const char *name, struct dentry *parent);
 
 struct dentry *debugfs_create_symlink(const char *name, struct dentry *parent,
@@ -129,6 +134,14 @@ static inline struct dentry *debugfs_create_file(const char *name, umode_t mode,
        return ERR_PTR(-ENODEV);
 }
 
+static inline struct dentry *debugfs_create_file_size(const char *name, umode_t mode,
+                                       struct dentry *parent, void *data,
+                                       const struct file_operations *fops,
+                                       loff_t file_size)
+{
+       return ERR_PTR(-ENODEV);
+}
+
 static inline struct dentry *debugfs_create_dir(const char *name,
                                                struct dentry *parent)
 {