xref: /linux-6.15/fs/debugfs/inode.c (revision 3bce94fd)
1*3bce94fdSGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
21da177e4SLinus Torvalds /*
3bd33d12fSHarry Wei  *  inode.c - part of debugfs, a tiny little debug file system
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  *  Copyright (C) 2004 Greg Kroah-Hartman <[email protected]>
61da177e4SLinus Torvalds  *  Copyright (C) 2004 IBM Inc.
71da177e4SLinus Torvalds  *
81da177e4SLinus Torvalds  *	This program is free software; you can redistribute it and/or
91da177e4SLinus Torvalds  *	modify it under the terms of the GNU General Public License version
101da177e4SLinus Torvalds  *	2 as published by the Free Software Foundation.
111da177e4SLinus Torvalds  *
121da177e4SLinus Torvalds  *  debugfs is for people to use instead of /proc or /sys.
13e1511a84SMauro Carvalho Chehab  *  See ./Documentation/core-api/kernel-api.rst for more details.
141da177e4SLinus Torvalds  *
151da177e4SLinus Torvalds  */
161da177e4SLinus Torvalds 
171da177e4SLinus Torvalds #include <linux/module.h>
181da177e4SLinus Torvalds #include <linux/fs.h>
191da177e4SLinus Torvalds #include <linux/mount.h>
201da177e4SLinus Torvalds #include <linux/pagemap.h>
211da177e4SLinus Torvalds #include <linux/init.h>
224d8ebddcSRandy Dunlap #include <linux/kobject.h>
231da177e4SLinus Torvalds #include <linux/namei.h>
241da177e4SLinus Torvalds #include <linux/debugfs.h>
254f36557fSMathieu Desnoyers #include <linux/fsnotify.h>
2666f54963SPeter Oberparleiter #include <linux/string.h>
27d6e48686SLudwig Nussel #include <linux/seq_file.h>
28d6e48686SLudwig Nussel #include <linux/parser.h>
2992562927SMimi Zohar #include <linux/magic.h>
305a0e3ad6STejun Heo #include <linux/slab.h>
319fd4dcecSNicolai Stange 
329fd4dcecSNicolai Stange #include "internal.h"
331da177e4SLinus Torvalds 
3482aceae4SKees Cook #define DEBUGFS_DEFAULT_MODE	0700
35d6e48686SLudwig Nussel 
361da177e4SLinus Torvalds static struct vfsmount *debugfs_mount;
371da177e4SLinus Torvalds static int debugfs_mount_count;
38c0f92ba9SFrederic Weisbecker static bool debugfs_registered;
391da177e4SLinus Torvalds 
40edac65eaSAl Viro static struct inode *debugfs_get_inode(struct super_block *sb)
411da177e4SLinus Torvalds {
421da177e4SLinus Torvalds 	struct inode *inode = new_inode(sb);
431da177e4SLinus Torvalds 	if (inode) {
4485fe4025SChristoph Hellwig 		inode->i_ino = get_next_ino();
451b48b530SDeepa Dinamani 		inode->i_atime = inode->i_mtime =
46c2050a45SDeepa Dinamani 			inode->i_ctime = current_time(inode);
471da177e4SLinus Torvalds 	}
481da177e4SLinus Torvalds 	return inode;
491da177e4SLinus Torvalds }
501da177e4SLinus Torvalds 
51d6e48686SLudwig Nussel struct debugfs_mount_opts {
527dc05881SEric W. Biederman 	kuid_t uid;
537dc05881SEric W. Biederman 	kgid_t gid;
54d6e48686SLudwig Nussel 	umode_t mode;
55d6e48686SLudwig Nussel };
56d6e48686SLudwig Nussel 
57d6e48686SLudwig Nussel enum {
58d6e48686SLudwig Nussel 	Opt_uid,
59d6e48686SLudwig Nussel 	Opt_gid,
60d6e48686SLudwig Nussel 	Opt_mode,
61d6e48686SLudwig Nussel 	Opt_err
62d6e48686SLudwig Nussel };
63d6e48686SLudwig Nussel 
64d6e48686SLudwig Nussel static const match_table_t tokens = {
65d6e48686SLudwig Nussel 	{Opt_uid, "uid=%u"},
66d6e48686SLudwig Nussel 	{Opt_gid, "gid=%u"},
67d6e48686SLudwig Nussel 	{Opt_mode, "mode=%o"},
68d6e48686SLudwig Nussel 	{Opt_err, NULL}
69d6e48686SLudwig Nussel };
70d6e48686SLudwig Nussel 
71d6e48686SLudwig Nussel struct debugfs_fs_info {
72d6e48686SLudwig Nussel 	struct debugfs_mount_opts mount_opts;
73d6e48686SLudwig Nussel };
74d6e48686SLudwig Nussel 
75d6e48686SLudwig Nussel static int debugfs_parse_options(char *data, struct debugfs_mount_opts *opts)
76d6e48686SLudwig Nussel {
77d6e48686SLudwig Nussel 	substring_t args[MAX_OPT_ARGS];
78d6e48686SLudwig Nussel 	int option;
79d6e48686SLudwig Nussel 	int token;
807dc05881SEric W. Biederman 	kuid_t uid;
817dc05881SEric W. Biederman 	kgid_t gid;
82d6e48686SLudwig Nussel 	char *p;
83d6e48686SLudwig Nussel 
84d6e48686SLudwig Nussel 	opts->mode = DEBUGFS_DEFAULT_MODE;
85d6e48686SLudwig Nussel 
86d6e48686SLudwig Nussel 	while ((p = strsep(&data, ",")) != NULL) {
87d6e48686SLudwig Nussel 		if (!*p)
88d6e48686SLudwig Nussel 			continue;
89d6e48686SLudwig Nussel 
90d6e48686SLudwig Nussel 		token = match_token(p, tokens, args);
91d6e48686SLudwig Nussel 		switch (token) {
92d6e48686SLudwig Nussel 		case Opt_uid:
93d6e48686SLudwig Nussel 			if (match_int(&args[0], &option))
94d6e48686SLudwig Nussel 				return -EINVAL;
957dc05881SEric W. Biederman 			uid = make_kuid(current_user_ns(), option);
967dc05881SEric W. Biederman 			if (!uid_valid(uid))
977dc05881SEric W. Biederman 				return -EINVAL;
987dc05881SEric W. Biederman 			opts->uid = uid;
99d6e48686SLudwig Nussel 			break;
100d6e48686SLudwig Nussel 		case Opt_gid:
101f1688e04SDave Reisner 			if (match_int(&args[0], &option))
102d6e48686SLudwig Nussel 				return -EINVAL;
1037dc05881SEric W. Biederman 			gid = make_kgid(current_user_ns(), option);
1047dc05881SEric W. Biederman 			if (!gid_valid(gid))
1057dc05881SEric W. Biederman 				return -EINVAL;
1067dc05881SEric W. Biederman 			opts->gid = gid;
107d6e48686SLudwig Nussel 			break;
108d6e48686SLudwig Nussel 		case Opt_mode:
109d6e48686SLudwig Nussel 			if (match_octal(&args[0], &option))
110d6e48686SLudwig Nussel 				return -EINVAL;
111d6e48686SLudwig Nussel 			opts->mode = option & S_IALLUGO;
112d6e48686SLudwig Nussel 			break;
113d6e48686SLudwig Nussel 		/*
114d6e48686SLudwig Nussel 		 * We might like to report bad mount options here;
115d6e48686SLudwig Nussel 		 * but traditionally debugfs has ignored all mount options
116d6e48686SLudwig Nussel 		 */
117d6e48686SLudwig Nussel 		}
118d6e48686SLudwig Nussel 	}
119d6e48686SLudwig Nussel 
120d6e48686SLudwig Nussel 	return 0;
121d6e48686SLudwig Nussel }
122d6e48686SLudwig Nussel 
123d6e48686SLudwig Nussel static int debugfs_apply_options(struct super_block *sb)
124d6e48686SLudwig Nussel {
125d6e48686SLudwig Nussel 	struct debugfs_fs_info *fsi = sb->s_fs_info;
1262b0143b5SDavid Howells 	struct inode *inode = d_inode(sb->s_root);
127d6e48686SLudwig Nussel 	struct debugfs_mount_opts *opts = &fsi->mount_opts;
128d6e48686SLudwig Nussel 
129d6e48686SLudwig Nussel 	inode->i_mode &= ~S_IALLUGO;
130d6e48686SLudwig Nussel 	inode->i_mode |= opts->mode;
131d6e48686SLudwig Nussel 
132d6e48686SLudwig Nussel 	inode->i_uid = opts->uid;
133d6e48686SLudwig Nussel 	inode->i_gid = opts->gid;
134d6e48686SLudwig Nussel 
135d6e48686SLudwig Nussel 	return 0;
136d6e48686SLudwig Nussel }
137d6e48686SLudwig Nussel 
138d6e48686SLudwig Nussel static int debugfs_remount(struct super_block *sb, int *flags, char *data)
139d6e48686SLudwig Nussel {
140d6e48686SLudwig Nussel 	int err;
141d6e48686SLudwig Nussel 	struct debugfs_fs_info *fsi = sb->s_fs_info;
142d6e48686SLudwig Nussel 
14302b9984dSTheodore Ts'o 	sync_filesystem(sb);
144d6e48686SLudwig Nussel 	err = debugfs_parse_options(data, &fsi->mount_opts);
145d6e48686SLudwig Nussel 	if (err)
146d6e48686SLudwig Nussel 		goto fail;
147d6e48686SLudwig Nussel 
148d6e48686SLudwig Nussel 	debugfs_apply_options(sb);
149d6e48686SLudwig Nussel 
150d6e48686SLudwig Nussel fail:
151d6e48686SLudwig Nussel 	return err;
152d6e48686SLudwig Nussel }
153d6e48686SLudwig Nussel 
154d6e48686SLudwig Nussel static int debugfs_show_options(struct seq_file *m, struct dentry *root)
155d6e48686SLudwig Nussel {
156d6e48686SLudwig Nussel 	struct debugfs_fs_info *fsi = root->d_sb->s_fs_info;
157d6e48686SLudwig Nussel 	struct debugfs_mount_opts *opts = &fsi->mount_opts;
158d6e48686SLudwig Nussel 
1597dc05881SEric W. Biederman 	if (!uid_eq(opts->uid, GLOBAL_ROOT_UID))
1607dc05881SEric W. Biederman 		seq_printf(m, ",uid=%u",
1617dc05881SEric W. Biederman 			   from_kuid_munged(&init_user_ns, opts->uid));
1627dc05881SEric W. Biederman 	if (!gid_eq(opts->gid, GLOBAL_ROOT_GID))
1637dc05881SEric W. Biederman 		seq_printf(m, ",gid=%u",
1647dc05881SEric W. Biederman 			   from_kgid_munged(&init_user_ns, opts->gid));
165d6e48686SLudwig Nussel 	if (opts->mode != DEBUGFS_DEFAULT_MODE)
166d6e48686SLudwig Nussel 		seq_printf(m, ",mode=%o", opts->mode);
167d6e48686SLudwig Nussel 
168d6e48686SLudwig Nussel 	return 0;
169d6e48686SLudwig Nussel }
170d6e48686SLudwig Nussel 
1710db59e59SAl Viro static void debugfs_evict_inode(struct inode *inode)
1720db59e59SAl Viro {
1730db59e59SAl Viro 	truncate_inode_pages_final(&inode->i_data);
1740db59e59SAl Viro 	clear_inode(inode);
1750db59e59SAl Viro 	if (S_ISLNK(inode->i_mode))
1765723cb01SAl Viro 		kfree(inode->i_link);
1770db59e59SAl Viro }
1780db59e59SAl Viro 
179d6e48686SLudwig Nussel static const struct super_operations debugfs_super_operations = {
180d6e48686SLudwig Nussel 	.statfs		= simple_statfs,
181d6e48686SLudwig Nussel 	.remount_fs	= debugfs_remount,
182d6e48686SLudwig Nussel 	.show_options	= debugfs_show_options,
1830db59e59SAl Viro 	.evict_inode	= debugfs_evict_inode,
184d6e48686SLudwig Nussel };
185d6e48686SLudwig Nussel 
1867c8d4698SNicolai Stange static void debugfs_release_dentry(struct dentry *dentry)
1877c8d4698SNicolai Stange {
1887d39bc50SNicolai Stange 	void *fsd = dentry->d_fsdata;
1897d39bc50SNicolai Stange 
1907d39bc50SNicolai Stange 	if (!((unsigned long)fsd & DEBUGFS_FSDATA_IS_REAL_FOPS_BIT))
1917c8d4698SNicolai Stange 		kfree(dentry->d_fsdata);
1927c8d4698SNicolai Stange }
1937c8d4698SNicolai Stange 
19477b3da6eSAl Viro static struct vfsmount *debugfs_automount(struct path *path)
19577b3da6eSAl Viro {
19693faccbbSEric W. Biederman 	debugfs_automount_t f;
19793faccbbSEric W. Biederman 	f = (debugfs_automount_t)path->dentry->d_fsdata;
19893faccbbSEric W. Biederman 	return f(path->dentry, d_inode(path->dentry)->i_private);
19977b3da6eSAl Viro }
20077b3da6eSAl Viro 
20177b3da6eSAl Viro static const struct dentry_operations debugfs_dops = {
20277b3da6eSAl Viro 	.d_delete = always_delete_dentry,
2037c8d4698SNicolai Stange 	.d_release = debugfs_release_dentry,
20477b3da6eSAl Viro 	.d_automount = debugfs_automount,
20577b3da6eSAl Viro };
20677b3da6eSAl Viro 
2071da177e4SLinus Torvalds static int debug_fill_super(struct super_block *sb, void *data, int silent)
2081da177e4SLinus Torvalds {
209cda37124SEric Biggers 	static const struct tree_descr debug_files[] = {{""}};
210d6e48686SLudwig Nussel 	struct debugfs_fs_info *fsi;
211d6e48686SLudwig Nussel 	int err;
2121da177e4SLinus Torvalds 
213d6e48686SLudwig Nussel 	fsi = kzalloc(sizeof(struct debugfs_fs_info), GFP_KERNEL);
214d6e48686SLudwig Nussel 	sb->s_fs_info = fsi;
215d6e48686SLudwig Nussel 	if (!fsi) {
216d6e48686SLudwig Nussel 		err = -ENOMEM;
217d6e48686SLudwig Nussel 		goto fail;
218d6e48686SLudwig Nussel 	}
219d6e48686SLudwig Nussel 
220d6e48686SLudwig Nussel 	err = debugfs_parse_options(data, &fsi->mount_opts);
221d6e48686SLudwig Nussel 	if (err)
222d6e48686SLudwig Nussel 		goto fail;
223d6e48686SLudwig Nussel 
224d6e48686SLudwig Nussel 	err  =  simple_fill_super(sb, DEBUGFS_MAGIC, debug_files);
225d6e48686SLudwig Nussel 	if (err)
226d6e48686SLudwig Nussel 		goto fail;
227d6e48686SLudwig Nussel 
228d6e48686SLudwig Nussel 	sb->s_op = &debugfs_super_operations;
22977b3da6eSAl Viro 	sb->s_d_op = &debugfs_dops;
230d6e48686SLudwig Nussel 
231d6e48686SLudwig Nussel 	debugfs_apply_options(sb);
232d6e48686SLudwig Nussel 
233d6e48686SLudwig Nussel 	return 0;
234d6e48686SLudwig Nussel 
235d6e48686SLudwig Nussel fail:
236d6e48686SLudwig Nussel 	kfree(fsi);
237d6e48686SLudwig Nussel 	sb->s_fs_info = NULL;
238d6e48686SLudwig Nussel 	return err;
2391da177e4SLinus Torvalds }
2401da177e4SLinus Torvalds 
241fc14f2feSAl Viro static struct dentry *debug_mount(struct file_system_type *fs_type,
2421da177e4SLinus Torvalds 			int flags, const char *dev_name,
243fc14f2feSAl Viro 			void *data)
2441da177e4SLinus Torvalds {
245fc14f2feSAl Viro 	return mount_single(fs_type, flags, data, debug_fill_super);
2461da177e4SLinus Torvalds }
2471da177e4SLinus Torvalds 
2481da177e4SLinus Torvalds static struct file_system_type debug_fs_type = {
2491da177e4SLinus Torvalds 	.owner =	THIS_MODULE,
2501da177e4SLinus Torvalds 	.name =		"debugfs",
251fc14f2feSAl Viro 	.mount =	debug_mount,
2521da177e4SLinus Torvalds 	.kill_sb =	kill_litter_super,
2531da177e4SLinus Torvalds };
2547f78e035SEric W. Biederman MODULE_ALIAS_FS("debugfs");
2551da177e4SLinus Torvalds 
256a7c5437bSOmar Sandoval /**
257a7c5437bSOmar Sandoval  * debugfs_lookup() - look up an existing debugfs file
258a7c5437bSOmar Sandoval  * @name: a pointer to a string containing the name of the file to look up.
259a7c5437bSOmar Sandoval  * @parent: a pointer to the parent dentry of the file.
260a7c5437bSOmar Sandoval  *
261a7c5437bSOmar Sandoval  * This function will return a pointer to a dentry if it succeeds.  If the file
262a7c5437bSOmar Sandoval  * doesn't exist or an error occurs, %NULL will be returned.  The returned
263a7c5437bSOmar Sandoval  * dentry must be passed to dput() when it is no longer needed.
264a7c5437bSOmar Sandoval  *
265a7c5437bSOmar Sandoval  * If debugfs is not enabled in the kernel, the value -%ENODEV will be
266a7c5437bSOmar Sandoval  * returned.
267a7c5437bSOmar Sandoval  */
268a7c5437bSOmar Sandoval struct dentry *debugfs_lookup(const char *name, struct dentry *parent)
269a7c5437bSOmar Sandoval {
270a7c5437bSOmar Sandoval 	struct dentry *dentry;
271a7c5437bSOmar Sandoval 
272a7c5437bSOmar Sandoval 	if (IS_ERR(parent))
273a7c5437bSOmar Sandoval 		return NULL;
274a7c5437bSOmar Sandoval 
275a7c5437bSOmar Sandoval 	if (!parent)
276a7c5437bSOmar Sandoval 		parent = debugfs_mount->mnt_root;
277a7c5437bSOmar Sandoval 
278a7c5437bSOmar Sandoval 	inode_lock(d_inode(parent));
279a7c5437bSOmar Sandoval 	dentry = lookup_one_len(name, parent, strlen(name));
280a7c5437bSOmar Sandoval 	inode_unlock(d_inode(parent));
281a7c5437bSOmar Sandoval 
282a7c5437bSOmar Sandoval 	if (IS_ERR(dentry))
283a7c5437bSOmar Sandoval 		return NULL;
284a7c5437bSOmar Sandoval 	if (!d_really_is_positive(dentry)) {
285a7c5437bSOmar Sandoval 		dput(dentry);
286a7c5437bSOmar Sandoval 		return NULL;
287a7c5437bSOmar Sandoval 	}
288a7c5437bSOmar Sandoval 	return dentry;
289a7c5437bSOmar Sandoval }
290a7c5437bSOmar Sandoval EXPORT_SYMBOL_GPL(debugfs_lookup);
291a7c5437bSOmar Sandoval 
292190afd81SAl Viro static struct dentry *start_creating(const char *name, struct dentry *parent)
293c3b1a350SAl Viro {
294190afd81SAl Viro 	struct dentry *dentry;
295c3b1a350SAl Viro 	int error;
296c3b1a350SAl Viro 
297c3b1a350SAl Viro 	pr_debug("debugfs: creating file '%s'\n",name);
298c3b1a350SAl Viro 
299c9e15f25SGreg KH 	if (IS_ERR(parent))
300c9e15f25SGreg KH 		return parent;
301c9e15f25SGreg KH 
302c3b1a350SAl Viro 	error = simple_pin_fs(&debug_fs_type, &debugfs_mount,
303c3b1a350SAl Viro 			      &debugfs_mount_count);
304c3b1a350SAl Viro 	if (error)
305190afd81SAl Viro 		return ERR_PTR(error);
306c3b1a350SAl Viro 
307cfa57c11SAl Viro 	/* If the parent is not specified, we create it in the root.
308cfa57c11SAl Viro 	 * We need the root dentry to do this, which is in the super
309cfa57c11SAl Viro 	 * block. A pointer to that is in the struct vfsmount that we
310cfa57c11SAl Viro 	 * have around.
311cfa57c11SAl Viro 	 */
312cfa57c11SAl Viro 	if (!parent)
313cfa57c11SAl Viro 		parent = debugfs_mount->mnt_root;
314cfa57c11SAl Viro 
3155955102cSAl Viro 	inode_lock(d_inode(parent));
316cfa57c11SAl Viro 	dentry = lookup_one_len(name, parent, strlen(name));
3172b0143b5SDavid Howells 	if (!IS_ERR(dentry) && d_really_is_positive(dentry)) {
318190afd81SAl Viro 		dput(dentry);
319190afd81SAl Viro 		dentry = ERR_PTR(-EEXIST);
320190afd81SAl Viro 	}
3210ee9608cSDaniel Borkmann 
3220ee9608cSDaniel Borkmann 	if (IS_ERR(dentry)) {
3235955102cSAl Viro 		inode_unlock(d_inode(parent));
3240ee9608cSDaniel Borkmann 		simple_release_fs(&debugfs_mount, &debugfs_mount_count);
3250ee9608cSDaniel Borkmann 	}
3260ee9608cSDaniel Borkmann 
327190afd81SAl Viro 	return dentry;
328190afd81SAl Viro }
329190afd81SAl Viro 
3305233e311SAl Viro static struct dentry *failed_creating(struct dentry *dentry)
331190afd81SAl Viro {
3325955102cSAl Viro 	inode_unlock(d_inode(dentry->d_parent));
333190afd81SAl Viro 	dput(dentry);
334190afd81SAl Viro 	simple_release_fs(&debugfs_mount, &debugfs_mount_count);
3355233e311SAl Viro 	return NULL;
336190afd81SAl Viro }
3375233e311SAl Viro 
3385233e311SAl Viro static struct dentry *end_creating(struct dentry *dentry)
3395233e311SAl Viro {
3405955102cSAl Viro 	inode_unlock(d_inode(dentry->d_parent));
341190afd81SAl Viro 	return dentry;
342190afd81SAl Viro }
343190afd81SAl Viro 
34449d200deSNicolai Stange static struct dentry *__debugfs_create_file(const char *name, umode_t mode,
34549d200deSNicolai Stange 				struct dentry *parent, void *data,
34649d200deSNicolai Stange 				const struct file_operations *proxy_fops,
34749d200deSNicolai Stange 				const struct file_operations *real_fops)
34849d200deSNicolai Stange {
34949d200deSNicolai Stange 	struct dentry *dentry;
35049d200deSNicolai Stange 	struct inode *inode;
35149d200deSNicolai Stange 
35249d200deSNicolai Stange 	if (!(mode & S_IFMT))
35349d200deSNicolai Stange 		mode |= S_IFREG;
35449d200deSNicolai Stange 	BUG_ON(!S_ISREG(mode));
35549d200deSNicolai Stange 	dentry = start_creating(name, parent);
35649d200deSNicolai Stange 
3577d39bc50SNicolai Stange 	if (IS_ERR(dentry))
35849d200deSNicolai Stange 		return NULL;
35949d200deSNicolai Stange 
36049d200deSNicolai Stange 	inode = debugfs_get_inode(dentry->d_sb);
3617d39bc50SNicolai Stange 	if (unlikely(!inode))
36249d200deSNicolai Stange 		return failed_creating(dentry);
36349d200deSNicolai Stange 
36449d200deSNicolai Stange 	inode->i_mode = mode;
36549d200deSNicolai Stange 	inode->i_private = data;
36649d200deSNicolai Stange 
36749d200deSNicolai Stange 	inode->i_fop = proxy_fops;
3687d39bc50SNicolai Stange 	dentry->d_fsdata = (void *)((unsigned long)real_fops |
3697d39bc50SNicolai Stange 				DEBUGFS_FSDATA_IS_REAL_FOPS_BIT);
37049d200deSNicolai Stange 
37149d200deSNicolai Stange 	d_instantiate(dentry, inode);
37249d200deSNicolai Stange 	fsnotify_create(d_inode(dentry->d_parent), dentry);
37349d200deSNicolai Stange 	return end_creating(dentry);
37449d200deSNicolai Stange }
37549d200deSNicolai Stange 
3761da177e4SLinus Torvalds /**
3771da177e4SLinus Torvalds  * debugfs_create_file - create a file in the debugfs filesystem
3781da177e4SLinus Torvalds  * @name: a pointer to a string containing the name of the file to create.
379be030e65SAlberto Bertogli  * @mode: the permission that the file should have.
3801da177e4SLinus Torvalds  * @parent: a pointer to the parent dentry for this file.  This should be a
381e227867fSMasanari Iida  *          directory dentry if set.  If this parameter is NULL, then the
3821da177e4SLinus Torvalds  *          file will be created in the root of the debugfs filesystem.
3831da177e4SLinus Torvalds  * @data: a pointer to something that the caller will want to get to later
3848e18e294STheodore Ts'o  *        on.  The inode.i_private pointer will point to this value on
3851da177e4SLinus Torvalds  *        the open() call.
3861da177e4SLinus Torvalds  * @fops: a pointer to a struct file_operations that should be used for
3871da177e4SLinus Torvalds  *        this file.
3881da177e4SLinus Torvalds  *
3891da177e4SLinus Torvalds  * This is the basic "create a file" function for debugfs.  It allows for a
390be030e65SAlberto Bertogli  * wide range of flexibility in creating a file, or a directory (if you want
391be030e65SAlberto Bertogli  * to create a directory, the debugfs_create_dir() function is
3921da177e4SLinus Torvalds  * recommended to be used instead.)
3931da177e4SLinus Torvalds  *
3941da177e4SLinus Torvalds  * This function will return a pointer to a dentry if it succeeds.  This
3951da177e4SLinus Torvalds  * pointer must be passed to the debugfs_remove() function when the file is
3961da177e4SLinus Torvalds  * to be removed (no automatic cleanup happens if your module is unloaded,
3976468b3afSRandy Dunlap  * you are responsible here.)  If an error occurs, %NULL will be returned.
3981da177e4SLinus Torvalds  *
3996468b3afSRandy Dunlap  * If debugfs is not enabled in the kernel, the value -%ENODEV will be
400873760fbSCornelia Huck  * returned.
4011da177e4SLinus Torvalds  */
402f4ae40a6SAl Viro struct dentry *debugfs_create_file(const char *name, umode_t mode,
4031da177e4SLinus Torvalds 				   struct dentry *parent, void *data,
40499ac48f5SArjan van de Ven 				   const struct file_operations *fops)
4051da177e4SLinus Torvalds {
406c3b1a350SAl Viro 
40749d200deSNicolai Stange 	return __debugfs_create_file(name, mode, parent, data,
40849d200deSNicolai Stange 				fops ? &debugfs_full_proxy_file_operations :
40949d200deSNicolai Stange 					&debugfs_noop_file_operations,
41049d200deSNicolai Stange 				fops);
4111da177e4SLinus Torvalds }
4121da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(debugfs_create_file);
4131da177e4SLinus Torvalds 
414c6468808SNicolai Stange /**
415c6468808SNicolai Stange  * debugfs_create_file_unsafe - create a file in the debugfs filesystem
416c6468808SNicolai Stange  * @name: a pointer to a string containing the name of the file to create.
417c6468808SNicolai Stange  * @mode: the permission that the file should have.
418c6468808SNicolai Stange  * @parent: a pointer to the parent dentry for this file.  This should be a
419c6468808SNicolai Stange  *          directory dentry if set.  If this parameter is NULL, then the
420c6468808SNicolai Stange  *          file will be created in the root of the debugfs filesystem.
421c6468808SNicolai Stange  * @data: a pointer to something that the caller will want to get to later
422c6468808SNicolai Stange  *        on.  The inode.i_private pointer will point to this value on
423c6468808SNicolai Stange  *        the open() call.
424c6468808SNicolai Stange  * @fops: a pointer to a struct file_operations that should be used for
425c6468808SNicolai Stange  *        this file.
426c6468808SNicolai Stange  *
427c6468808SNicolai Stange  * debugfs_create_file_unsafe() is completely analogous to
428c6468808SNicolai Stange  * debugfs_create_file(), the only difference being that the fops
429c6468808SNicolai Stange  * handed it will not get protected against file removals by the
430c6468808SNicolai Stange  * debugfs core.
431c6468808SNicolai Stange  *
432c6468808SNicolai Stange  * It is your responsibility to protect your struct file_operation
433c6468808SNicolai Stange  * methods against file removals by means of debugfs_use_file_start()
434c6468808SNicolai Stange  * and debugfs_use_file_finish(). ->open() is still protected by
435c6468808SNicolai Stange  * debugfs though.
436c6468808SNicolai Stange  *
437c6468808SNicolai Stange  * Any struct file_operations defined by means of
438c6468808SNicolai Stange  * DEFINE_DEBUGFS_ATTRIBUTE() is protected against file removals and
439c6468808SNicolai Stange  * thus, may be used here.
440c6468808SNicolai Stange  */
44149d200deSNicolai Stange struct dentry *debugfs_create_file_unsafe(const char *name, umode_t mode,
44249d200deSNicolai Stange 				   struct dentry *parent, void *data,
44349d200deSNicolai Stange 				   const struct file_operations *fops)
44449d200deSNicolai Stange {
44549d200deSNicolai Stange 
44649d200deSNicolai Stange 	return __debugfs_create_file(name, mode, parent, data,
44749d200deSNicolai Stange 				fops ? &debugfs_open_proxy_file_operations :
44849d200deSNicolai Stange 					&debugfs_noop_file_operations,
44949d200deSNicolai Stange 				fops);
45049d200deSNicolai Stange }
451c6468808SNicolai Stange EXPORT_SYMBOL_GPL(debugfs_create_file_unsafe);
45249d200deSNicolai Stange 
4531da177e4SLinus Torvalds /**
454e59b4e91SDavid Howells  * debugfs_create_file_size - create a file in the debugfs filesystem
455e59b4e91SDavid Howells  * @name: a pointer to a string containing the name of the file to create.
456e59b4e91SDavid Howells  * @mode: the permission that the file should have.
457e59b4e91SDavid Howells  * @parent: a pointer to the parent dentry for this file.  This should be a
458e59b4e91SDavid Howells  *          directory dentry if set.  If this parameter is NULL, then the
459e59b4e91SDavid Howells  *          file will be created in the root of the debugfs filesystem.
460e59b4e91SDavid Howells  * @data: a pointer to something that the caller will want to get to later
461e59b4e91SDavid Howells  *        on.  The inode.i_private pointer will point to this value on
462e59b4e91SDavid Howells  *        the open() call.
463e59b4e91SDavid Howells  * @fops: a pointer to a struct file_operations that should be used for
464e59b4e91SDavid Howells  *        this file.
465e59b4e91SDavid Howells  * @file_size: initial file size
466e59b4e91SDavid Howells  *
467e59b4e91SDavid Howells  * This is the basic "create a file" function for debugfs.  It allows for a
468e59b4e91SDavid Howells  * wide range of flexibility in creating a file, or a directory (if you want
469e59b4e91SDavid Howells  * to create a directory, the debugfs_create_dir() function is
470e59b4e91SDavid Howells  * recommended to be used instead.)
471e59b4e91SDavid Howells  *
472e59b4e91SDavid Howells  * This function will return a pointer to a dentry if it succeeds.  This
473e59b4e91SDavid Howells  * pointer must be passed to the debugfs_remove() function when the file is
474e59b4e91SDavid Howells  * to be removed (no automatic cleanup happens if your module is unloaded,
475e59b4e91SDavid Howells  * you are responsible here.)  If an error occurs, %NULL will be returned.
476e59b4e91SDavid Howells  *
477e59b4e91SDavid Howells  * If debugfs is not enabled in the kernel, the value -%ENODEV will be
478e59b4e91SDavid Howells  * returned.
479e59b4e91SDavid Howells  */
480e59b4e91SDavid Howells struct dentry *debugfs_create_file_size(const char *name, umode_t mode,
481e59b4e91SDavid Howells 					struct dentry *parent, void *data,
482e59b4e91SDavid Howells 					const struct file_operations *fops,
483e59b4e91SDavid Howells 					loff_t file_size)
484e59b4e91SDavid Howells {
485e59b4e91SDavid Howells 	struct dentry *de = debugfs_create_file(name, mode, parent, data, fops);
486e59b4e91SDavid Howells 
487e59b4e91SDavid Howells 	if (de)
4882b0143b5SDavid Howells 		d_inode(de)->i_size = file_size;
489e59b4e91SDavid Howells 	return de;
490e59b4e91SDavid Howells }
491e59b4e91SDavid Howells EXPORT_SYMBOL_GPL(debugfs_create_file_size);
492e59b4e91SDavid Howells 
493e59b4e91SDavid Howells /**
4941da177e4SLinus Torvalds  * debugfs_create_dir - create a directory in the debugfs filesystem
4951da177e4SLinus Torvalds  * @name: a pointer to a string containing the name of the directory to
4961da177e4SLinus Torvalds  *        create.
4971da177e4SLinus Torvalds  * @parent: a pointer to the parent dentry for this file.  This should be a
498e227867fSMasanari Iida  *          directory dentry if set.  If this parameter is NULL, then the
4991da177e4SLinus Torvalds  *          directory will be created in the root of the debugfs filesystem.
5001da177e4SLinus Torvalds  *
5011da177e4SLinus Torvalds  * This function creates a directory in debugfs with the given name.
5021da177e4SLinus Torvalds  *
5031da177e4SLinus Torvalds  * This function will return a pointer to a dentry if it succeeds.  This
5041da177e4SLinus Torvalds  * pointer must be passed to the debugfs_remove() function when the file is
5051da177e4SLinus Torvalds  * to be removed (no automatic cleanup happens if your module is unloaded,
5066468b3afSRandy Dunlap  * you are responsible here.)  If an error occurs, %NULL will be returned.
5071da177e4SLinus Torvalds  *
5086468b3afSRandy Dunlap  * If debugfs is not enabled in the kernel, the value -%ENODEV will be
509873760fbSCornelia Huck  * returned.
5101da177e4SLinus Torvalds  */
5111da177e4SLinus Torvalds struct dentry *debugfs_create_dir(const char *name, struct dentry *parent)
5121da177e4SLinus Torvalds {
513ad5abd5bSAl Viro 	struct dentry *dentry = start_creating(name, parent);
514680b3024SAl Viro 	struct inode *inode;
515ad5abd5bSAl Viro 
516ad5abd5bSAl Viro 	if (IS_ERR(dentry))
517ad5abd5bSAl Viro 		return NULL;
518ad5abd5bSAl Viro 
519edac65eaSAl Viro 	inode = debugfs_get_inode(dentry->d_sb);
520680b3024SAl Viro 	if (unlikely(!inode))
5215233e311SAl Viro 		return failed_creating(dentry);
522680b3024SAl Viro 
523edac65eaSAl Viro 	inode->i_mode = S_IFDIR | S_IRWXU | S_IRUGO | S_IXUGO;
524edac65eaSAl Viro 	inode->i_op = &simple_dir_inode_operations;
525edac65eaSAl Viro 	inode->i_fop = &simple_dir_operations;
526edac65eaSAl Viro 
527edac65eaSAl Viro 	/* directory inodes start off with i_nlink == 2 (for "." entry) */
528edac65eaSAl Viro 	inc_nlink(inode);
529680b3024SAl Viro 	d_instantiate(dentry, inode);
5302b0143b5SDavid Howells 	inc_nlink(d_inode(dentry->d_parent));
5312b0143b5SDavid Howells 	fsnotify_mkdir(d_inode(dentry->d_parent), dentry);
5325233e311SAl Viro 	return end_creating(dentry);
5331da177e4SLinus Torvalds }
5341da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(debugfs_create_dir);
5351da177e4SLinus Torvalds 
5361da177e4SLinus Torvalds /**
53777b3da6eSAl Viro  * debugfs_create_automount - create automount point in the debugfs filesystem
53877b3da6eSAl Viro  * @name: a pointer to a string containing the name of the file to create.
53977b3da6eSAl Viro  * @parent: a pointer to the parent dentry for this file.  This should be a
54077b3da6eSAl Viro  *          directory dentry if set.  If this parameter is NULL, then the
54177b3da6eSAl Viro  *          file will be created in the root of the debugfs filesystem.
54277b3da6eSAl Viro  * @f: function to be called when pathname resolution steps on that one.
54377b3da6eSAl Viro  * @data: opaque argument to pass to f().
54477b3da6eSAl Viro  *
54577b3da6eSAl Viro  * @f should return what ->d_automount() would.
54677b3da6eSAl Viro  */
54777b3da6eSAl Viro struct dentry *debugfs_create_automount(const char *name,
54877b3da6eSAl Viro 					struct dentry *parent,
54993faccbbSEric W. Biederman 					debugfs_automount_t f,
55077b3da6eSAl Viro 					void *data)
55177b3da6eSAl Viro {
55277b3da6eSAl Viro 	struct dentry *dentry = start_creating(name, parent);
55377b3da6eSAl Viro 	struct inode *inode;
55477b3da6eSAl Viro 
55577b3da6eSAl Viro 	if (IS_ERR(dentry))
55677b3da6eSAl Viro 		return NULL;
55777b3da6eSAl Viro 
55877b3da6eSAl Viro 	inode = debugfs_get_inode(dentry->d_sb);
55977b3da6eSAl Viro 	if (unlikely(!inode))
56077b3da6eSAl Viro 		return failed_creating(dentry);
56177b3da6eSAl Viro 
56287243debSSeth Forshee 	make_empty_dir_inode(inode);
56377b3da6eSAl Viro 	inode->i_flags |= S_AUTOMOUNT;
56477b3da6eSAl Viro 	inode->i_private = data;
56577b3da6eSAl Viro 	dentry->d_fsdata = (void *)f;
566a8f324a4SRoman Pen 	/* directory inodes start off with i_nlink == 2 (for "." entry) */
567a8f324a4SRoman Pen 	inc_nlink(inode);
56877b3da6eSAl Viro 	d_instantiate(dentry, inode);
569a8f324a4SRoman Pen 	inc_nlink(d_inode(dentry->d_parent));
570a8f324a4SRoman Pen 	fsnotify_mkdir(d_inode(dentry->d_parent), dentry);
57177b3da6eSAl Viro 	return end_creating(dentry);
57277b3da6eSAl Viro }
57377b3da6eSAl Viro EXPORT_SYMBOL(debugfs_create_automount);
57477b3da6eSAl Viro 
57577b3da6eSAl Viro /**
57666f54963SPeter Oberparleiter  * debugfs_create_symlink- create a symbolic link in the debugfs filesystem
57766f54963SPeter Oberparleiter  * @name: a pointer to a string containing the name of the symbolic link to
57866f54963SPeter Oberparleiter  *        create.
57966f54963SPeter Oberparleiter  * @parent: a pointer to the parent dentry for this symbolic link.  This
580e227867fSMasanari Iida  *          should be a directory dentry if set.  If this parameter is NULL,
58166f54963SPeter Oberparleiter  *          then the symbolic link will be created in the root of the debugfs
58266f54963SPeter Oberparleiter  *          filesystem.
58366f54963SPeter Oberparleiter  * @target: a pointer to a string containing the path to the target of the
58466f54963SPeter Oberparleiter  *          symbolic link.
58566f54963SPeter Oberparleiter  *
58666f54963SPeter Oberparleiter  * This function creates a symbolic link with the given name in debugfs that
58766f54963SPeter Oberparleiter  * links to the given target path.
58866f54963SPeter Oberparleiter  *
58966f54963SPeter Oberparleiter  * This function will return a pointer to a dentry if it succeeds.  This
59066f54963SPeter Oberparleiter  * pointer must be passed to the debugfs_remove() function when the symbolic
59166f54963SPeter Oberparleiter  * link is to be removed (no automatic cleanup happens if your module is
59266f54963SPeter Oberparleiter  * unloaded, you are responsible here.)  If an error occurs, %NULL will be
59366f54963SPeter Oberparleiter  * returned.
59466f54963SPeter Oberparleiter  *
59566f54963SPeter Oberparleiter  * If debugfs is not enabled in the kernel, the value -%ENODEV will be
596873760fbSCornelia Huck  * returned.
59766f54963SPeter Oberparleiter  */
59866f54963SPeter Oberparleiter struct dentry *debugfs_create_symlink(const char *name, struct dentry *parent,
59966f54963SPeter Oberparleiter 				      const char *target)
60066f54963SPeter Oberparleiter {
601ad5abd5bSAl Viro 	struct dentry *dentry;
602680b3024SAl Viro 	struct inode *inode;
603680b3024SAl Viro 	char *link = kstrdup(target, GFP_KERNEL);
60466f54963SPeter Oberparleiter 	if (!link)
60566f54963SPeter Oberparleiter 		return NULL;
60666f54963SPeter Oberparleiter 
607ad5abd5bSAl Viro 	dentry = start_creating(name, parent);
608ad5abd5bSAl Viro 	if (IS_ERR(dentry)) {
60966f54963SPeter Oberparleiter 		kfree(link);
610ad5abd5bSAl Viro 		return NULL;
611ad5abd5bSAl Viro 	}
612ad5abd5bSAl Viro 
613edac65eaSAl Viro 	inode = debugfs_get_inode(dentry->d_sb);
614680b3024SAl Viro 	if (unlikely(!inode)) {
615ad5abd5bSAl Viro 		kfree(link);
6165233e311SAl Viro 		return failed_creating(dentry);
617680b3024SAl Viro 	}
618edac65eaSAl Viro 	inode->i_mode = S_IFLNK | S_IRWXUGO;
6195723cb01SAl Viro 	inode->i_op = &simple_symlink_inode_operations;
6205723cb01SAl Viro 	inode->i_link = link;
621680b3024SAl Viro 	d_instantiate(dentry, inode);
6225233e311SAl Viro 	return end_creating(dentry);
62366f54963SPeter Oberparleiter }
62466f54963SPeter Oberparleiter EXPORT_SYMBOL_GPL(debugfs_create_symlink);
62566f54963SPeter Oberparleiter 
626e9117a5aSNicolai Stange static void __debugfs_remove_file(struct dentry *dentry, struct dentry *parent)
627e9117a5aSNicolai Stange {
628e9117a5aSNicolai Stange 	struct debugfs_fsdata *fsd;
629e9117a5aSNicolai Stange 
630e9117a5aSNicolai Stange 	simple_unlink(d_inode(parent), dentry);
631e9117a5aSNicolai Stange 	d_delete(dentry);
6327d39bc50SNicolai Stange 
6337d39bc50SNicolai Stange 	/*
6347d39bc50SNicolai Stange 	 * Paired with the closing smp_mb() implied by a successful
6357d39bc50SNicolai Stange 	 * cmpxchg() in debugfs_file_get(): either
6367d39bc50SNicolai Stange 	 * debugfs_file_get() must see a dead dentry or we must see a
6377d39bc50SNicolai Stange 	 * debugfs_fsdata instance at ->d_fsdata here (or both).
6387d39bc50SNicolai Stange 	 */
6397d39bc50SNicolai Stange 	smp_mb();
6407d39bc50SNicolai Stange 	fsd = READ_ONCE(dentry->d_fsdata);
6417d39bc50SNicolai Stange 	if ((unsigned long)fsd & DEBUGFS_FSDATA_IS_REAL_FOPS_BIT)
6427d39bc50SNicolai Stange 		return;
643e9117a5aSNicolai Stange 	if (!refcount_dec_and_test(&fsd->active_users))
644e9117a5aSNicolai Stange 		wait_for_completion(&fsd->active_users_drained);
645e9117a5aSNicolai Stange }
646e9117a5aSNicolai Stange 
64725d41d84SJan Kara static int __debugfs_remove(struct dentry *dentry, struct dentry *parent)
6481da177e4SLinus Torvalds {
64965c33336SMathieu Desnoyers 	int ret = 0;
6501da177e4SLinus Torvalds 
651dc3f4198SAl Viro 	if (simple_positive(dentry)) {
65229a7f3adSMathieu Desnoyers 		dget(dentry);
653e9117a5aSNicolai Stange 		if (!d_is_reg(dentry)) {
6547ceab50cSDavid Howells 			if (d_is_dir(dentry))
6552b0143b5SDavid Howells 				ret = simple_rmdir(d_inode(parent), dentry);
6560db59e59SAl Viro 			else
6572b0143b5SDavid Howells 				simple_unlink(d_inode(parent), dentry);
65829a7f3adSMathieu Desnoyers 			if (!ret)
65929a7f3adSMathieu Desnoyers 				d_delete(dentry);
660e9117a5aSNicolai Stange 		} else {
661e9117a5aSNicolai Stange 			__debugfs_remove_file(dentry, parent);
662e9117a5aSNicolai Stange 		}
66329a7f3adSMathieu Desnoyers 		dput(dentry);
6641da177e4SLinus Torvalds 	}
66525d41d84SJan Kara 	return ret;
6669505e637SHaavard Skinnemoen }
6679505e637SHaavard Skinnemoen 
6689505e637SHaavard Skinnemoen /**
6699505e637SHaavard Skinnemoen  * debugfs_remove - removes a file or directory from the debugfs filesystem
6709505e637SHaavard Skinnemoen  * @dentry: a pointer to a the dentry of the file or directory to be
671398dc4adSUlf Magnusson  *          removed.  If this parameter is NULL or an error value, nothing
672398dc4adSUlf Magnusson  *          will be done.
6739505e637SHaavard Skinnemoen  *
6749505e637SHaavard Skinnemoen  * This function removes a file or directory in debugfs that was previously
6759505e637SHaavard Skinnemoen  * created with a call to another debugfs function (like
6769505e637SHaavard Skinnemoen  * debugfs_create_file() or variants thereof.)
6779505e637SHaavard Skinnemoen  *
6789505e637SHaavard Skinnemoen  * This function is required to be called in order for the file to be
6799505e637SHaavard Skinnemoen  * removed, no automatic cleanup of files will happen when a module is
6809505e637SHaavard Skinnemoen  * removed, you are responsible here.
6819505e637SHaavard Skinnemoen  */
6829505e637SHaavard Skinnemoen void debugfs_remove(struct dentry *dentry)
6839505e637SHaavard Skinnemoen {
6849505e637SHaavard Skinnemoen 	struct dentry *parent;
68525d41d84SJan Kara 	int ret;
6869505e637SHaavard Skinnemoen 
687a59d6293SArend van Spriel 	if (IS_ERR_OR_NULL(dentry))
6889505e637SHaavard Skinnemoen 		return;
6899505e637SHaavard Skinnemoen 
6909505e637SHaavard Skinnemoen 	parent = dentry->d_parent;
6915955102cSAl Viro 	inode_lock(d_inode(parent));
69225d41d84SJan Kara 	ret = __debugfs_remove(dentry, parent);
6935955102cSAl Viro 	inode_unlock(d_inode(parent));
69425d41d84SJan Kara 	if (!ret)
6951da177e4SLinus Torvalds 		simple_release_fs(&debugfs_mount, &debugfs_mount_count);
6961da177e4SLinus Torvalds }
6971da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(debugfs_remove);
6981da177e4SLinus Torvalds 
699cfc94cdfSJan Kara /**
7009505e637SHaavard Skinnemoen  * debugfs_remove_recursive - recursively removes a directory
701398dc4adSUlf Magnusson  * @dentry: a pointer to a the dentry of the directory to be removed.  If this
702398dc4adSUlf Magnusson  *          parameter is NULL or an error value, nothing will be done.
7039505e637SHaavard Skinnemoen  *
7049505e637SHaavard Skinnemoen  * This function recursively removes a directory tree in debugfs that
7059505e637SHaavard Skinnemoen  * was previously created with a call to another debugfs function
7069505e637SHaavard Skinnemoen  * (like debugfs_create_file() or variants thereof.)
7079505e637SHaavard Skinnemoen  *
7089505e637SHaavard Skinnemoen  * This function is required to be called in order for the file to be
7099505e637SHaavard Skinnemoen  * removed, no automatic cleanup of files will happen when a module is
7109505e637SHaavard Skinnemoen  * removed, you are responsible here.
7119505e637SHaavard Skinnemoen  */
7129505e637SHaavard Skinnemoen void debugfs_remove_recursive(struct dentry *dentry)
7139505e637SHaavard Skinnemoen {
714485d4402SSteven Rostedt 	struct dentry *child, *parent;
7159505e637SHaavard Skinnemoen 
716a59d6293SArend van Spriel 	if (IS_ERR_OR_NULL(dentry))
7179505e637SHaavard Skinnemoen 		return;
7189505e637SHaavard Skinnemoen 
7199505e637SHaavard Skinnemoen 	parent = dentry;
720776164c1SOleg Nesterov  down:
7215955102cSAl Viro 	inode_lock(d_inode(parent));
722485d4402SSteven Rostedt  loop:
723485d4402SSteven Rostedt 	/*
724485d4402SSteven Rostedt 	 * The parent->d_subdirs is protected by the d_lock. Outside that
725485d4402SSteven Rostedt 	 * lock, the child can be unlinked and set to be freed which can
726485d4402SSteven Rostedt 	 * use the d_u.d_child as the rcu head and corrupt this list.
727485d4402SSteven Rostedt 	 */
728485d4402SSteven Rostedt 	spin_lock(&parent->d_lock);
729946e51f2SAl Viro 	list_for_each_entry(child, &parent->d_subdirs, d_child) {
730dc3f4198SAl Viro 		if (!simple_positive(child))
731776164c1SOleg Nesterov 			continue;
7329505e637SHaavard Skinnemoen 
733776164c1SOleg Nesterov 		/* perhaps simple_empty(child) makes more sense */
7349505e637SHaavard Skinnemoen 		if (!list_empty(&child->d_subdirs)) {
735485d4402SSteven Rostedt 			spin_unlock(&parent->d_lock);
7365955102cSAl Viro 			inode_unlock(d_inode(parent));
7379505e637SHaavard Skinnemoen 			parent = child;
738776164c1SOleg Nesterov 			goto down;
7399505e637SHaavard Skinnemoen 		}
740485d4402SSteven Rostedt 
741485d4402SSteven Rostedt 		spin_unlock(&parent->d_lock);
742485d4402SSteven Rostedt 
743776164c1SOleg Nesterov 		if (!__debugfs_remove(child, parent))
744776164c1SOleg Nesterov 			simple_release_fs(&debugfs_mount, &debugfs_mount_count);
745485d4402SSteven Rostedt 
746485d4402SSteven Rostedt 		/*
747485d4402SSteven Rostedt 		 * The parent->d_lock protects agaist child from unlinking
748485d4402SSteven Rostedt 		 * from d_subdirs. When releasing the parent->d_lock we can
749485d4402SSteven Rostedt 		 * no longer trust that the next pointer is valid.
750485d4402SSteven Rostedt 		 * Restart the loop. We'll skip this one with the
751dc3f4198SAl Viro 		 * simple_positive() check.
752485d4402SSteven Rostedt 		 */
753485d4402SSteven Rostedt 		goto loop;
754776164c1SOleg Nesterov 	}
755485d4402SSteven Rostedt 	spin_unlock(&parent->d_lock);
756776164c1SOleg Nesterov 
7575955102cSAl Viro 	inode_unlock(d_inode(parent));
758776164c1SOleg Nesterov 	child = parent;
759776164c1SOleg Nesterov 	parent = parent->d_parent;
7605955102cSAl Viro 	inode_lock(d_inode(parent));
761776164c1SOleg Nesterov 
762485d4402SSteven Rostedt 	if (child != dentry)
763485d4402SSteven Rostedt 		/* go up */
764485d4402SSteven Rostedt 		goto loop;
76556a83cc9SSteven Rostedt 
766776164c1SOleg Nesterov 	if (!__debugfs_remove(child, parent))
7679505e637SHaavard Skinnemoen 		simple_release_fs(&debugfs_mount, &debugfs_mount_count);
7685955102cSAl Viro 	inode_unlock(d_inode(parent));
7699505e637SHaavard Skinnemoen }
7709505e637SHaavard Skinnemoen EXPORT_SYMBOL_GPL(debugfs_remove_recursive);
7719505e637SHaavard Skinnemoen 
7729505e637SHaavard Skinnemoen /**
773cfc94cdfSJan Kara  * debugfs_rename - rename a file/directory in the debugfs filesystem
774cfc94cdfSJan Kara  * @old_dir: a pointer to the parent dentry for the renamed object. This
775cfc94cdfSJan Kara  *          should be a directory dentry.
776cfc94cdfSJan Kara  * @old_dentry: dentry of an object to be renamed.
777cfc94cdfSJan Kara  * @new_dir: a pointer to the parent dentry where the object should be
778cfc94cdfSJan Kara  *          moved. This should be a directory dentry.
779cfc94cdfSJan Kara  * @new_name: a pointer to a string containing the target name.
780cfc94cdfSJan Kara  *
781cfc94cdfSJan Kara  * This function renames a file/directory in debugfs.  The target must not
782cfc94cdfSJan Kara  * exist for rename to succeed.
783cfc94cdfSJan Kara  *
784cfc94cdfSJan Kara  * This function will return a pointer to old_dentry (which is updated to
785cfc94cdfSJan Kara  * reflect renaming) if it succeeds. If an error occurs, %NULL will be
786cfc94cdfSJan Kara  * returned.
787cfc94cdfSJan Kara  *
788cfc94cdfSJan Kara  * If debugfs is not enabled in the kernel, the value -%ENODEV will be
789cfc94cdfSJan Kara  * returned.
790cfc94cdfSJan Kara  */
791cfc94cdfSJan Kara struct dentry *debugfs_rename(struct dentry *old_dir, struct dentry *old_dentry,
792cfc94cdfSJan Kara 		struct dentry *new_dir, const char *new_name)
793cfc94cdfSJan Kara {
794cfc94cdfSJan Kara 	int error;
795cfc94cdfSJan Kara 	struct dentry *dentry = NULL, *trap;
79649d31c2fSAl Viro 	struct name_snapshot old_name;
797cfc94cdfSJan Kara 
798cfc94cdfSJan Kara 	trap = lock_rename(new_dir, old_dir);
799cfc94cdfSJan Kara 	/* Source or destination directories don't exist? */
8002b0143b5SDavid Howells 	if (d_really_is_negative(old_dir) || d_really_is_negative(new_dir))
801cfc94cdfSJan Kara 		goto exit;
802cfc94cdfSJan Kara 	/* Source does not exist, cyclic rename, or mountpoint? */
8032b0143b5SDavid Howells 	if (d_really_is_negative(old_dentry) || old_dentry == trap ||
804cfc94cdfSJan Kara 	    d_mountpoint(old_dentry))
805cfc94cdfSJan Kara 		goto exit;
806cfc94cdfSJan Kara 	dentry = lookup_one_len(new_name, new_dir, strlen(new_name));
807cfc94cdfSJan Kara 	/* Lookup failed, cyclic rename or target exists? */
8082b0143b5SDavid Howells 	if (IS_ERR(dentry) || dentry == trap || d_really_is_positive(dentry))
809cfc94cdfSJan Kara 		goto exit;
810cfc94cdfSJan Kara 
81149d31c2fSAl Viro 	take_dentry_name_snapshot(&old_name, old_dentry);
812cfc94cdfSJan Kara 
8132b0143b5SDavid Howells 	error = simple_rename(d_inode(old_dir), old_dentry, d_inode(new_dir),
814e0e0be8aSMiklos Szeredi 			      dentry, 0);
815cfc94cdfSJan Kara 	if (error) {
81649d31c2fSAl Viro 		release_dentry_name_snapshot(&old_name);
817cfc94cdfSJan Kara 		goto exit;
818cfc94cdfSJan Kara 	}
819cfc94cdfSJan Kara 	d_move(old_dentry, dentry);
82049d31c2fSAl Viro 	fsnotify_move(d_inode(old_dir), d_inode(new_dir), old_name.name,
821e36cb0b8SDavid Howells 		d_is_dir(old_dentry),
8225a190ae6SAl Viro 		NULL, old_dentry);
82349d31c2fSAl Viro 	release_dentry_name_snapshot(&old_name);
824cfc94cdfSJan Kara 	unlock_rename(new_dir, old_dir);
825cfc94cdfSJan Kara 	dput(dentry);
826cfc94cdfSJan Kara 	return old_dentry;
827cfc94cdfSJan Kara exit:
828cfc94cdfSJan Kara 	if (dentry && !IS_ERR(dentry))
829cfc94cdfSJan Kara 		dput(dentry);
830cfc94cdfSJan Kara 	unlock_rename(new_dir, old_dir);
831cfc94cdfSJan Kara 	return NULL;
832cfc94cdfSJan Kara }
833cfc94cdfSJan Kara EXPORT_SYMBOL_GPL(debugfs_rename);
834cfc94cdfSJan Kara 
835c0f92ba9SFrederic Weisbecker /**
836c0f92ba9SFrederic Weisbecker  * debugfs_initialized - Tells whether debugfs has been registered
837c0f92ba9SFrederic Weisbecker  */
838c0f92ba9SFrederic Weisbecker bool debugfs_initialized(void)
839c0f92ba9SFrederic Weisbecker {
840c0f92ba9SFrederic Weisbecker 	return debugfs_registered;
841c0f92ba9SFrederic Weisbecker }
842c0f92ba9SFrederic Weisbecker EXPORT_SYMBOL_GPL(debugfs_initialized);
843c0f92ba9SFrederic Weisbecker 
8441da177e4SLinus Torvalds static int __init debugfs_init(void)
8451da177e4SLinus Torvalds {
8461da177e4SLinus Torvalds 	int retval;
8471da177e4SLinus Torvalds 
848f9bb4882SEric W. Biederman 	retval = sysfs_create_mount_point(kernel_kobj, "debug");
849f9bb4882SEric W. Biederman 	if (retval)
850f9bb4882SEric W. Biederman 		return retval;
8511da177e4SLinus Torvalds 
8521da177e4SLinus Torvalds 	retval = register_filesystem(&debug_fs_type);
8531da177e4SLinus Torvalds 	if (retval)
854f9bb4882SEric W. Biederman 		sysfs_remove_mount_point(kernel_kobj, "debug");
855c0f92ba9SFrederic Weisbecker 	else
856c0f92ba9SFrederic Weisbecker 		debugfs_registered = true;
857c0f92ba9SFrederic Weisbecker 
8581da177e4SLinus Torvalds 	return retval;
8591da177e4SLinus Torvalds }
8601da177e4SLinus Torvalds core_initcall(debugfs_init);
8611da177e4SLinus Torvalds 
862