Merge git://git.infradead.org/intel-iommu
[firefly-linux-kernel-4.4.55.git] / fs / debugfs / file.c
index 8e0f2f410189e7d96e7b6bc3c0ef7185c7b6556b..830a7e76f5c64067e46fad8fd368e9112ddae7a9 100644 (file)
@@ -22,6 +22,7 @@
 #include <linux/io.h>
 #include <linux/slab.h>
 #include <linux/atomic.h>
+#include <linux/device.h>
 
 static ssize_t default_read_file(struct file *file, char __user *buf,
                                 size_t count, loff_t *ppos)
@@ -44,7 +45,7 @@ const struct file_operations debugfs_file_operations = {
 
 static void *debugfs_follow_link(struct dentry *dentry, struct nameidata *nd)
 {
-       nd_set_link(nd, dentry->d_inode->i_private);
+       nd_set_link(nd, d_inode(dentry)->i_private);
        return NULL;
 }
 
@@ -762,3 +763,56 @@ struct dentry *debugfs_create_regset32(const char *name, umode_t mode,
 EXPORT_SYMBOL_GPL(debugfs_create_regset32);
 
 #endif /* CONFIG_HAS_IOMEM */
+
+struct debugfs_devm_entry {
+       int (*read)(struct seq_file *seq, void *data);
+       struct device *dev;
+};
+
+static int debugfs_devm_entry_open(struct inode *inode, struct file *f)
+{
+       struct debugfs_devm_entry *entry = inode->i_private;
+
+       return single_open(f, entry->read, entry->dev);
+}
+
+static const struct file_operations debugfs_devm_entry_ops = {
+       .owner = THIS_MODULE,
+       .open = debugfs_devm_entry_open,
+       .release = single_release,
+       .read = seq_read,
+       .llseek = seq_lseek
+};
+
+/**
+ * debugfs_create_devm_seqfile - create a debugfs file that is bound to device.
+ *
+ * @dev: device related to this debugfs file.
+ * @name: name of the debugfs file.
+ * @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.
+ * @read_fn: function pointer called to print the seq_file content.
+ */
+struct dentry *debugfs_create_devm_seqfile(struct device *dev, const char *name,
+                                          struct dentry *parent,
+                                          int (*read_fn)(struct seq_file *s,
+                                                         void *data))
+{
+       struct debugfs_devm_entry *entry;
+
+       if (IS_ERR(parent))
+               return ERR_PTR(-ENOENT);
+
+       entry = devm_kzalloc(dev, sizeof(*entry), GFP_KERNEL);
+       if (!entry)
+               return ERR_PTR(-ENOMEM);
+
+       entry->read = read_fn;
+       entry->dev = dev;
+
+       return debugfs_create_file(name, S_IRUGO, parent, entry,
+                                  &debugfs_devm_entry_ops);
+}
+EXPORT_SYMBOL_GPL(debugfs_create_devm_seqfile);
+