]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/commitdiff
debugfs: add debugfs_lookup()
authorOmar Sandoval <osandov@fb.com>
Tue, 31 Jan 2017 22:53:17 +0000 (14:53 -0800)
committerJens Axboe <axboe@fb.com>
Thu, 2 Feb 2017 17:20:16 +0000 (10:20 -0700)
We don't always have easy access to the dentry of a file or directory we
created in debugfs. Add a helper which allows us to get a dentry we
previously created.

The motivation for this change is a problem with blktrace and the blk-mq
debugfs entries introduced in 07e4fead45e6 ("blk-mq: create debugfs
directory tree"). Namely, in some cases, the directory that blktrace
needs to create may already exist, but in other cases, it may not. We
_could_ rely on a bunch of implied knowledge to decide whether to create
the directory or not, but it's much cleaner on our end to just look it
up.

Signed-off-by: Omar Sandoval <osandov@fb.com>
Acked-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Signed-off-by: Jens Axboe <axboe@fb.com>
fs/debugfs/inode.c
include/linux/debugfs.h

index f17fcf89e18eb077ca4231c344327852ea3b156f..7fb1732a3630bd82aba6e8d116bfca2317f57fd9 100644 (file)
@@ -248,6 +248,42 @@ static struct file_system_type debug_fs_type = {
 };
 MODULE_ALIAS_FS("debugfs");
 
+/**
+ * debugfs_lookup() - look up an existing debugfs file
+ * @name: a pointer to a string containing the name of the file to look up.
+ * @parent: a pointer to the parent dentry of the file.
+ *
+ * This function will return a pointer to a dentry if it succeeds.  If the file
+ * doesn't exist or an error occurs, %NULL will be returned.  The returned
+ * dentry must be passed to dput() when it is no longer needed.
+ *
+ * If debugfs is not enabled in the kernel, the value -%ENODEV will be
+ * returned.
+ */
+struct dentry *debugfs_lookup(const char *name, struct dentry *parent)
+{
+       struct dentry *dentry;
+
+       if (IS_ERR(parent))
+               return NULL;
+
+       if (!parent)
+               parent = debugfs_mount->mnt_root;
+
+       inode_lock(d_inode(parent));
+       dentry = lookup_one_len(name, parent, strlen(name));
+       inode_unlock(d_inode(parent));
+
+       if (IS_ERR(dentry))
+               return NULL;
+       if (!d_really_is_positive(dentry)) {
+               dput(dentry);
+               return NULL;
+       }
+       return dentry;
+}
+EXPORT_SYMBOL_GPL(debugfs_lookup);
+
 static struct dentry *start_creating(const char *name, struct dentry *parent)
 {
        struct dentry *dentry;
index 014cc564d1c437f34d1cbe259f087156f1d148c9..c0befcf41b585b7f0e8702a3599eb1c3435cb69b 100644 (file)
@@ -80,6 +80,8 @@ static const struct file_operations __fops = {                                \
 
 #if defined(CONFIG_DEBUG_FS)
 
+struct dentry *debugfs_lookup(const char *name, struct dentry *parent);
+
 struct dentry *debugfs_create_file(const char *name, umode_t mode,
                                   struct dentry *parent, void *data,
                                   const struct file_operations *fops);
@@ -181,6 +183,12 @@ ssize_t debugfs_write_file_bool(struct file *file, const char __user *user_buf,
  * want to duplicate the design decision mistakes of procfs and devfs again.
  */
 
+static inline struct dentry *debugfs_lookup(const char *name,
+                                           struct dentry *parent)
+{
+       return ERR_PTR(-ENODEV);
+}
+
 static inline struct dentry *debugfs_create_file(const char *name, umode_t mode,
                                        struct dentry *parent, void *data,
                                        const struct file_operations *fops)