Skip to content

Commit a7c5437

Browse files
osandovaxboe
authored andcommitted
debugfs: add debugfs_lookup()
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 07e4fea ("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>
1 parent 0dba131 commit a7c5437

2 files changed

Lines changed: 44 additions & 0 deletions

File tree

‎fs/debugfs/inode.c‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,42 @@ static struct file_system_type debug_fs_type = {
248248
};
249249
MODULE_ALIAS_FS("debugfs");
250250

251+
/**
252+
* debugfs_lookup() - look up an existing debugfs file
253+
* @name: a pointer to a string containing the name of the file to look up.
254+
* @parent: a pointer to the parent dentry of the file.
255+
*
256+
* This function will return a pointer to a dentry if it succeeds. If the file
257+
* doesn't exist or an error occurs, %NULL will be returned. The returned
258+
* dentry must be passed to dput() when it is no longer needed.
259+
*
260+
* If debugfs is not enabled in the kernel, the value -%ENODEV will be
261+
* returned.
262+
*/
263+
struct dentry *debugfs_lookup(const char *name, struct dentry *parent)
264+
{
265+
struct dentry *dentry;
266+
267+
if (IS_ERR(parent))
268+
return NULL;
269+
270+
if (!parent)
271+
parent = debugfs_mount->mnt_root;
272+
273+
inode_lock(d_inode(parent));
274+
dentry = lookup_one_len(name, parent, strlen(name));
275+
inode_unlock(d_inode(parent));
276+
277+
if (IS_ERR(dentry))
278+
return NULL;
279+
if (!d_really_is_positive(dentry)) {
280+
dput(dentry);
281+
return NULL;
282+
}
283+
return dentry;
284+
}
285+
EXPORT_SYMBOL_GPL(debugfs_lookup);
286+
251287
static struct dentry *start_creating(const char *name, struct dentry *parent)
252288
{
253289
struct dentry *dentry;

‎include/linux/debugfs.h‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ static const struct file_operations __fops = { \
8080

8181
#if defined(CONFIG_DEBUG_FS)
8282

83+
struct dentry *debugfs_lookup(const char *name, struct dentry *parent);
84+
8385
struct dentry *debugfs_create_file(const char *name, umode_t mode,
8486
struct dentry *parent, void *data,
8587
const struct file_operations *fops);
@@ -181,6 +183,12 @@ ssize_t debugfs_write_file_bool(struct file *file, const char __user *user_buf,
181183
* want to duplicate the design decision mistakes of procfs and devfs again.
182184
*/
183185

186+
static inline struct dentry *debugfs_lookup(const char *name,
187+
struct dentry *parent)
188+
{
189+
return ERR_PTR(-ENODEV);
190+
}
191+
184192
static inline struct dentry *debugfs_create_file(const char *name, umode_t mode,
185193
struct dentry *parent, void *data,
186194
const struct file_operations *fops)

0 commit comments

Comments
 (0)