xref: /linux-6.15/fs/debugfs/inode.c (revision c33d4423)
13bce94fdSGreg 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  *
543e23b6cSGreg Kroah-Hartman  *  Copyright (C) 2004,2019 Greg Kroah-Hartman <[email protected]>
61da177e4SLinus Torvalds  *  Copyright (C) 2004 IBM Inc.
743e23b6cSGreg Kroah-Hartman  *  Copyright (C) 2019 Linux Foundation <[email protected]>
81da177e4SLinus Torvalds  *
91da177e4SLinus Torvalds  *  debugfs is for people to use instead of /proc or /sys.
10e1511a84SMauro Carvalho Chehab  *  See ./Documentation/core-api/kernel-api.rst for more details.
111da177e4SLinus Torvalds  */
121da177e4SLinus Torvalds 
13d03ae477SGreg Kroah-Hartman #define pr_fmt(fmt)	"debugfs: " fmt
14d03ae477SGreg Kroah-Hartman 
151da177e4SLinus Torvalds #include <linux/module.h>
161da177e4SLinus Torvalds #include <linux/fs.h>
171da177e4SLinus Torvalds #include <linux/mount.h>
181da177e4SLinus Torvalds #include <linux/pagemap.h>
191da177e4SLinus Torvalds #include <linux/init.h>
204d8ebddcSRandy Dunlap #include <linux/kobject.h>
211da177e4SLinus Torvalds #include <linux/namei.h>
221da177e4SLinus Torvalds #include <linux/debugfs.h>
234f36557fSMathieu Desnoyers #include <linux/fsnotify.h>
2466f54963SPeter Oberparleiter #include <linux/string.h>
25d6e48686SLudwig Nussel #include <linux/seq_file.h>
26d6e48686SLudwig Nussel #include <linux/parser.h>
2792562927SMimi Zohar #include <linux/magic.h>
285a0e3ad6STejun Heo #include <linux/slab.h>
299fd4dcecSNicolai Stange 
309fd4dcecSNicolai Stange #include "internal.h"
311da177e4SLinus Torvalds 
3282aceae4SKees Cook #define DEBUGFS_DEFAULT_MODE	0700
33d6e48686SLudwig Nussel 
341da177e4SLinus Torvalds static struct vfsmount *debugfs_mount;
351da177e4SLinus Torvalds static int debugfs_mount_count;
36c0f92ba9SFrederic Weisbecker static bool debugfs_registered;
371da177e4SLinus Torvalds 
38edac65eaSAl Viro static struct inode *debugfs_get_inode(struct super_block *sb)
391da177e4SLinus Torvalds {
401da177e4SLinus Torvalds 	struct inode *inode = new_inode(sb);
411da177e4SLinus Torvalds 	if (inode) {
4285fe4025SChristoph Hellwig 		inode->i_ino = get_next_ino();
431b48b530SDeepa Dinamani 		inode->i_atime = inode->i_mtime =
44c2050a45SDeepa Dinamani 			inode->i_ctime = current_time(inode);
451da177e4SLinus Torvalds 	}
461da177e4SLinus Torvalds 	return inode;
471da177e4SLinus Torvalds }
481da177e4SLinus Torvalds 
49d6e48686SLudwig Nussel struct debugfs_mount_opts {
507dc05881SEric W. Biederman 	kuid_t uid;
517dc05881SEric W. Biederman 	kgid_t gid;
52d6e48686SLudwig Nussel 	umode_t mode;
53d6e48686SLudwig Nussel };
54d6e48686SLudwig Nussel 
55d6e48686SLudwig Nussel enum {
56d6e48686SLudwig Nussel 	Opt_uid,
57d6e48686SLudwig Nussel 	Opt_gid,
58d6e48686SLudwig Nussel 	Opt_mode,
59d6e48686SLudwig Nussel 	Opt_err
60d6e48686SLudwig Nussel };
61d6e48686SLudwig Nussel 
62d6e48686SLudwig Nussel static const match_table_t tokens = {
63d6e48686SLudwig Nussel 	{Opt_uid, "uid=%u"},
64d6e48686SLudwig Nussel 	{Opt_gid, "gid=%u"},
65d6e48686SLudwig Nussel 	{Opt_mode, "mode=%o"},
66d6e48686SLudwig Nussel 	{Opt_err, NULL}
67d6e48686SLudwig Nussel };
68d6e48686SLudwig Nussel 
69d6e48686SLudwig Nussel struct debugfs_fs_info {
70d6e48686SLudwig Nussel 	struct debugfs_mount_opts mount_opts;
71d6e48686SLudwig Nussel };
72d6e48686SLudwig Nussel 
73d6e48686SLudwig Nussel static int debugfs_parse_options(char *data, struct debugfs_mount_opts *opts)
74d6e48686SLudwig Nussel {
75d6e48686SLudwig Nussel 	substring_t args[MAX_OPT_ARGS];
76d6e48686SLudwig Nussel 	int option;
77d6e48686SLudwig Nussel 	int token;
787dc05881SEric W. Biederman 	kuid_t uid;
797dc05881SEric W. Biederman 	kgid_t gid;
80d6e48686SLudwig Nussel 	char *p;
81d6e48686SLudwig Nussel 
82d6e48686SLudwig Nussel 	opts->mode = DEBUGFS_DEFAULT_MODE;
83d6e48686SLudwig Nussel 
84d6e48686SLudwig Nussel 	while ((p = strsep(&data, ",")) != NULL) {
85d6e48686SLudwig Nussel 		if (!*p)
86d6e48686SLudwig Nussel 			continue;
87d6e48686SLudwig Nussel 
88d6e48686SLudwig Nussel 		token = match_token(p, tokens, args);
89d6e48686SLudwig Nussel 		switch (token) {
90d6e48686SLudwig Nussel 		case Opt_uid:
91d6e48686SLudwig Nussel 			if (match_int(&args[0], &option))
92d6e48686SLudwig Nussel 				return -EINVAL;
937dc05881SEric W. Biederman 			uid = make_kuid(current_user_ns(), option);
947dc05881SEric W. Biederman 			if (!uid_valid(uid))
957dc05881SEric W. Biederman 				return -EINVAL;
967dc05881SEric W. Biederman 			opts->uid = uid;
97d6e48686SLudwig Nussel 			break;
98d6e48686SLudwig Nussel 		case Opt_gid:
99f1688e04SDave Reisner 			if (match_int(&args[0], &option))
100d6e48686SLudwig Nussel 				return -EINVAL;
1017dc05881SEric W. Biederman 			gid = make_kgid(current_user_ns(), option);
1027dc05881SEric W. Biederman 			if (!gid_valid(gid))
1037dc05881SEric W. Biederman 				return -EINVAL;
1047dc05881SEric W. Biederman 			opts->gid = gid;
105d6e48686SLudwig Nussel 			break;
106d6e48686SLudwig Nussel 		case Opt_mode:
107d6e48686SLudwig Nussel 			if (match_octal(&args[0], &option))
108d6e48686SLudwig Nussel 				return -EINVAL;
109d6e48686SLudwig Nussel 			opts->mode = option & S_IALLUGO;
110d6e48686SLudwig Nussel 			break;
111d6e48686SLudwig Nussel 		/*
112d6e48686SLudwig Nussel 		 * We might like to report bad mount options here;
113d6e48686SLudwig Nussel 		 * but traditionally debugfs has ignored all mount options
114d6e48686SLudwig Nussel 		 */
115d6e48686SLudwig Nussel 		}
116d6e48686SLudwig Nussel 	}
117d6e48686SLudwig Nussel 
118d6e48686SLudwig Nussel 	return 0;
119d6e48686SLudwig Nussel }
120d6e48686SLudwig Nussel 
121d6e48686SLudwig Nussel static int debugfs_apply_options(struct super_block *sb)
122d6e48686SLudwig Nussel {
123d6e48686SLudwig Nussel 	struct debugfs_fs_info *fsi = sb->s_fs_info;
1242b0143b5SDavid Howells 	struct inode *inode = d_inode(sb->s_root);
125d6e48686SLudwig Nussel 	struct debugfs_mount_opts *opts = &fsi->mount_opts;
126d6e48686SLudwig Nussel 
127d6e48686SLudwig Nussel 	inode->i_mode &= ~S_IALLUGO;
128d6e48686SLudwig Nussel 	inode->i_mode |= opts->mode;
129d6e48686SLudwig Nussel 
130d6e48686SLudwig Nussel 	inode->i_uid = opts->uid;
131d6e48686SLudwig Nussel 	inode->i_gid = opts->gid;
132d6e48686SLudwig Nussel 
133d6e48686SLudwig Nussel 	return 0;
134d6e48686SLudwig Nussel }
135d6e48686SLudwig Nussel 
136d6e48686SLudwig Nussel static int debugfs_remount(struct super_block *sb, int *flags, char *data)
137d6e48686SLudwig Nussel {
138d6e48686SLudwig Nussel 	int err;
139d6e48686SLudwig Nussel 	struct debugfs_fs_info *fsi = sb->s_fs_info;
140d6e48686SLudwig Nussel 
14102b9984dSTheodore Ts'o 	sync_filesystem(sb);
142d6e48686SLudwig Nussel 	err = debugfs_parse_options(data, &fsi->mount_opts);
143d6e48686SLudwig Nussel 	if (err)
144d6e48686SLudwig Nussel 		goto fail;
145d6e48686SLudwig Nussel 
146d6e48686SLudwig Nussel 	debugfs_apply_options(sb);
147d6e48686SLudwig Nussel 
148d6e48686SLudwig Nussel fail:
149d6e48686SLudwig Nussel 	return err;
150d6e48686SLudwig Nussel }
151d6e48686SLudwig Nussel 
152d6e48686SLudwig Nussel static int debugfs_show_options(struct seq_file *m, struct dentry *root)
153d6e48686SLudwig Nussel {
154d6e48686SLudwig Nussel 	struct debugfs_fs_info *fsi = root->d_sb->s_fs_info;
155d6e48686SLudwig Nussel 	struct debugfs_mount_opts *opts = &fsi->mount_opts;
156d6e48686SLudwig Nussel 
1577dc05881SEric W. Biederman 	if (!uid_eq(opts->uid, GLOBAL_ROOT_UID))
1587dc05881SEric W. Biederman 		seq_printf(m, ",uid=%u",
1597dc05881SEric W. Biederman 			   from_kuid_munged(&init_user_ns, opts->uid));
1607dc05881SEric W. Biederman 	if (!gid_eq(opts->gid, GLOBAL_ROOT_GID))
1617dc05881SEric W. Biederman 		seq_printf(m, ",gid=%u",
1627dc05881SEric W. Biederman 			   from_kgid_munged(&init_user_ns, opts->gid));
163d6e48686SLudwig Nussel 	if (opts->mode != DEBUGFS_DEFAULT_MODE)
164d6e48686SLudwig Nussel 		seq_printf(m, ",mode=%o", opts->mode);
165d6e48686SLudwig Nussel 
166d6e48686SLudwig Nussel 	return 0;
167d6e48686SLudwig Nussel }
168d6e48686SLudwig Nussel 
1696234ddf4SAl Viro static void debugfs_free_inode(struct inode *inode)
1700db59e59SAl Viro {
1710db59e59SAl Viro 	if (S_ISLNK(inode->i_mode))
1725723cb01SAl Viro 		kfree(inode->i_link);
17393b919daSAl Viro 	free_inode_nonrcu(inode);
17493b919daSAl Viro }
17593b919daSAl Viro 
176d6e48686SLudwig Nussel static const struct super_operations debugfs_super_operations = {
177d6e48686SLudwig Nussel 	.statfs		= simple_statfs,
178d6e48686SLudwig Nussel 	.remount_fs	= debugfs_remount,
179d6e48686SLudwig Nussel 	.show_options	= debugfs_show_options,
1806234ddf4SAl Viro 	.free_inode	= debugfs_free_inode,
181d6e48686SLudwig Nussel };
182d6e48686SLudwig Nussel 
1837c8d4698SNicolai Stange static void debugfs_release_dentry(struct dentry *dentry)
1847c8d4698SNicolai Stange {
1857d39bc50SNicolai Stange 	void *fsd = dentry->d_fsdata;
1867d39bc50SNicolai Stange 
1877d39bc50SNicolai Stange 	if (!((unsigned long)fsd & DEBUGFS_FSDATA_IS_REAL_FOPS_BIT))
1887c8d4698SNicolai Stange 		kfree(dentry->d_fsdata);
1897c8d4698SNicolai Stange }
1907c8d4698SNicolai Stange 
19177b3da6eSAl Viro static struct vfsmount *debugfs_automount(struct path *path)
19277b3da6eSAl Viro {
19393faccbbSEric W. Biederman 	debugfs_automount_t f;
19493faccbbSEric W. Biederman 	f = (debugfs_automount_t)path->dentry->d_fsdata;
19593faccbbSEric W. Biederman 	return f(path->dentry, d_inode(path->dentry)->i_private);
19677b3da6eSAl Viro }
19777b3da6eSAl Viro 
19877b3da6eSAl Viro static const struct dentry_operations debugfs_dops = {
19977b3da6eSAl Viro 	.d_delete = always_delete_dentry,
2007c8d4698SNicolai Stange 	.d_release = debugfs_release_dentry,
20177b3da6eSAl Viro 	.d_automount = debugfs_automount,
20277b3da6eSAl Viro };
20377b3da6eSAl Viro 
2041da177e4SLinus Torvalds static int debug_fill_super(struct super_block *sb, void *data, int silent)
2051da177e4SLinus Torvalds {
206cda37124SEric Biggers 	static const struct tree_descr debug_files[] = {{""}};
207d6e48686SLudwig Nussel 	struct debugfs_fs_info *fsi;
208d6e48686SLudwig Nussel 	int err;
2091da177e4SLinus Torvalds 
210d6e48686SLudwig Nussel 	fsi = kzalloc(sizeof(struct debugfs_fs_info), GFP_KERNEL);
211d6e48686SLudwig Nussel 	sb->s_fs_info = fsi;
212d6e48686SLudwig Nussel 	if (!fsi) {
213d6e48686SLudwig Nussel 		err = -ENOMEM;
214d6e48686SLudwig Nussel 		goto fail;
215d6e48686SLudwig Nussel 	}
216d6e48686SLudwig Nussel 
217d6e48686SLudwig Nussel 	err = debugfs_parse_options(data, &fsi->mount_opts);
218d6e48686SLudwig Nussel 	if (err)
219d6e48686SLudwig Nussel 		goto fail;
220d6e48686SLudwig Nussel 
221d6e48686SLudwig Nussel 	err  =  simple_fill_super(sb, DEBUGFS_MAGIC, debug_files);
222d6e48686SLudwig Nussel 	if (err)
223d6e48686SLudwig Nussel 		goto fail;
224d6e48686SLudwig Nussel 
225d6e48686SLudwig Nussel 	sb->s_op = &debugfs_super_operations;
22677b3da6eSAl Viro 	sb->s_d_op = &debugfs_dops;
227d6e48686SLudwig Nussel 
228d6e48686SLudwig Nussel 	debugfs_apply_options(sb);
229d6e48686SLudwig Nussel 
230d6e48686SLudwig Nussel 	return 0;
231d6e48686SLudwig Nussel 
232d6e48686SLudwig Nussel fail:
233d6e48686SLudwig Nussel 	kfree(fsi);
234d6e48686SLudwig Nussel 	sb->s_fs_info = NULL;
235d6e48686SLudwig Nussel 	return err;
2361da177e4SLinus Torvalds }
2371da177e4SLinus Torvalds 
238fc14f2feSAl Viro static struct dentry *debug_mount(struct file_system_type *fs_type,
2391da177e4SLinus Torvalds 			int flags, const char *dev_name,
240fc14f2feSAl Viro 			void *data)
2411da177e4SLinus Torvalds {
242fc14f2feSAl Viro 	return mount_single(fs_type, flags, data, debug_fill_super);
2431da177e4SLinus Torvalds }
2441da177e4SLinus Torvalds 
2451da177e4SLinus Torvalds static struct file_system_type debug_fs_type = {
2461da177e4SLinus Torvalds 	.owner =	THIS_MODULE,
2471da177e4SLinus Torvalds 	.name =		"debugfs",
248fc14f2feSAl Viro 	.mount =	debug_mount,
2491da177e4SLinus Torvalds 	.kill_sb =	kill_litter_super,
2501da177e4SLinus Torvalds };
2517f78e035SEric W. Biederman MODULE_ALIAS_FS("debugfs");
2521da177e4SLinus Torvalds 
253a7c5437bSOmar Sandoval /**
254a7c5437bSOmar Sandoval  * debugfs_lookup() - look up an existing debugfs file
255a7c5437bSOmar Sandoval  * @name: a pointer to a string containing the name of the file to look up.
256a7c5437bSOmar Sandoval  * @parent: a pointer to the parent dentry of the file.
257a7c5437bSOmar Sandoval  *
258a7c5437bSOmar Sandoval  * This function will return a pointer to a dentry if it succeeds.  If the file
259a7c5437bSOmar Sandoval  * doesn't exist or an error occurs, %NULL will be returned.  The returned
260a7c5437bSOmar Sandoval  * dentry must be passed to dput() when it is no longer needed.
261a7c5437bSOmar Sandoval  *
262a7c5437bSOmar Sandoval  * If debugfs is not enabled in the kernel, the value -%ENODEV will be
263a7c5437bSOmar Sandoval  * returned.
264a7c5437bSOmar Sandoval  */
265a7c5437bSOmar Sandoval struct dentry *debugfs_lookup(const char *name, struct dentry *parent)
266a7c5437bSOmar Sandoval {
267a7c5437bSOmar Sandoval 	struct dentry *dentry;
268a7c5437bSOmar Sandoval 
269a7c5437bSOmar Sandoval 	if (IS_ERR(parent))
270a7c5437bSOmar Sandoval 		return NULL;
271a7c5437bSOmar Sandoval 
272a7c5437bSOmar Sandoval 	if (!parent)
273a7c5437bSOmar Sandoval 		parent = debugfs_mount->mnt_root;
274a7c5437bSOmar Sandoval 
275cd1c0c93SAl Viro 	dentry = lookup_one_len_unlocked(name, parent, strlen(name));
276a7c5437bSOmar Sandoval 	if (IS_ERR(dentry))
277a7c5437bSOmar Sandoval 		return NULL;
278a7c5437bSOmar Sandoval 	if (!d_really_is_positive(dentry)) {
279a7c5437bSOmar Sandoval 		dput(dentry);
280a7c5437bSOmar Sandoval 		return NULL;
281a7c5437bSOmar Sandoval 	}
282a7c5437bSOmar Sandoval 	return dentry;
283a7c5437bSOmar Sandoval }
284a7c5437bSOmar Sandoval EXPORT_SYMBOL_GPL(debugfs_lookup);
285a7c5437bSOmar Sandoval 
286190afd81SAl Viro static struct dentry *start_creating(const char *name, struct dentry *parent)
287c3b1a350SAl Viro {
288190afd81SAl Viro 	struct dentry *dentry;
289c3b1a350SAl Viro 	int error;
290c3b1a350SAl Viro 
291d03ae477SGreg Kroah-Hartman 	pr_debug("creating file '%s'\n", name);
292c3b1a350SAl Viro 
293c9e15f25SGreg KH 	if (IS_ERR(parent))
294c9e15f25SGreg KH 		return parent;
295c9e15f25SGreg KH 
296c3b1a350SAl Viro 	error = simple_pin_fs(&debug_fs_type, &debugfs_mount,
297c3b1a350SAl Viro 			      &debugfs_mount_count);
29843e23b6cSGreg Kroah-Hartman 	if (error) {
29943e23b6cSGreg Kroah-Hartman 		pr_err("Unable to pin filesystem for file '%s'\n", name);
300190afd81SAl Viro 		return ERR_PTR(error);
30143e23b6cSGreg Kroah-Hartman 	}
302c3b1a350SAl Viro 
303cfa57c11SAl Viro 	/* If the parent is not specified, we create it in the root.
304cfa57c11SAl Viro 	 * We need the root dentry to do this, which is in the super
305cfa57c11SAl Viro 	 * block. A pointer to that is in the struct vfsmount that we
306cfa57c11SAl Viro 	 * have around.
307cfa57c11SAl Viro 	 */
308cfa57c11SAl Viro 	if (!parent)
309cfa57c11SAl Viro 		parent = debugfs_mount->mnt_root;
310cfa57c11SAl Viro 
3115955102cSAl Viro 	inode_lock(d_inode(parent));
312cfa57c11SAl Viro 	dentry = lookup_one_len(name, parent, strlen(name));
3132b0143b5SDavid Howells 	if (!IS_ERR(dentry) && d_really_is_positive(dentry)) {
314*c33d4423SGreg Kroah-Hartman 		if (d_is_dir(dentry))
315*c33d4423SGreg Kroah-Hartman 			pr_err("Directory '%s' with parent '%s' already present!\n",
316*c33d4423SGreg Kroah-Hartman 			       name, parent->d_name.name);
317*c33d4423SGreg Kroah-Hartman 		else
318*c33d4423SGreg Kroah-Hartman 			pr_err("File '%s' in directory '%s' already present!\n",
319*c33d4423SGreg Kroah-Hartman 			       name, parent->d_name.name);
320190afd81SAl Viro 		dput(dentry);
321190afd81SAl Viro 		dentry = ERR_PTR(-EEXIST);
322190afd81SAl Viro 	}
3230ee9608cSDaniel Borkmann 
3240ee9608cSDaniel Borkmann 	if (IS_ERR(dentry)) {
3255955102cSAl Viro 		inode_unlock(d_inode(parent));
3260ee9608cSDaniel Borkmann 		simple_release_fs(&debugfs_mount, &debugfs_mount_count);
3270ee9608cSDaniel Borkmann 	}
3280ee9608cSDaniel Borkmann 
329190afd81SAl Viro 	return dentry;
330190afd81SAl Viro }
331190afd81SAl Viro 
3325233e311SAl Viro static struct dentry *failed_creating(struct dentry *dentry)
333190afd81SAl Viro {
3345955102cSAl Viro 	inode_unlock(d_inode(dentry->d_parent));
335190afd81SAl Viro 	dput(dentry);
336190afd81SAl Viro 	simple_release_fs(&debugfs_mount, &debugfs_mount_count);
337ff9fb72bSGreg Kroah-Hartman 	return ERR_PTR(-ENOMEM);
338190afd81SAl Viro }
3395233e311SAl Viro 
3405233e311SAl Viro static struct dentry *end_creating(struct dentry *dentry)
3415233e311SAl Viro {
3425955102cSAl Viro 	inode_unlock(d_inode(dentry->d_parent));
343190afd81SAl Viro 	return dentry;
344190afd81SAl Viro }
345190afd81SAl Viro 
34649d200deSNicolai Stange static struct dentry *__debugfs_create_file(const char *name, umode_t mode,
34749d200deSNicolai Stange 				struct dentry *parent, void *data,
34849d200deSNicolai Stange 				const struct file_operations *proxy_fops,
34949d200deSNicolai Stange 				const struct file_operations *real_fops)
35049d200deSNicolai Stange {
35149d200deSNicolai Stange 	struct dentry *dentry;
35249d200deSNicolai Stange 	struct inode *inode;
35349d200deSNicolai Stange 
35449d200deSNicolai Stange 	if (!(mode & S_IFMT))
35549d200deSNicolai Stange 		mode |= S_IFREG;
35649d200deSNicolai Stange 	BUG_ON(!S_ISREG(mode));
35749d200deSNicolai Stange 	dentry = start_creating(name, parent);
35849d200deSNicolai Stange 
3597d39bc50SNicolai Stange 	if (IS_ERR(dentry))
360ff9fb72bSGreg Kroah-Hartman 		return dentry;
36149d200deSNicolai Stange 
36249d200deSNicolai Stange 	inode = debugfs_get_inode(dentry->d_sb);
36343e23b6cSGreg Kroah-Hartman 	if (unlikely(!inode)) {
36443e23b6cSGreg Kroah-Hartman 		pr_err("out of free dentries, can not create file '%s'\n",
36543e23b6cSGreg Kroah-Hartman 		       name);
36649d200deSNicolai Stange 		return failed_creating(dentry);
36743e23b6cSGreg Kroah-Hartman 	}
36849d200deSNicolai Stange 
36949d200deSNicolai Stange 	inode->i_mode = mode;
37049d200deSNicolai Stange 	inode->i_private = data;
37149d200deSNicolai Stange 
37249d200deSNicolai Stange 	inode->i_fop = proxy_fops;
3737d39bc50SNicolai Stange 	dentry->d_fsdata = (void *)((unsigned long)real_fops |
3747d39bc50SNicolai Stange 				DEBUGFS_FSDATA_IS_REAL_FOPS_BIT);
37549d200deSNicolai Stange 
37649d200deSNicolai Stange 	d_instantiate(dentry, inode);
37749d200deSNicolai Stange 	fsnotify_create(d_inode(dentry->d_parent), dentry);
37849d200deSNicolai Stange 	return end_creating(dentry);
37949d200deSNicolai Stange }
38049d200deSNicolai Stange 
3811da177e4SLinus Torvalds /**
3821da177e4SLinus Torvalds  * debugfs_create_file - create a file in the debugfs filesystem
3831da177e4SLinus Torvalds  * @name: a pointer to a string containing the name of the file to create.
384be030e65SAlberto Bertogli  * @mode: the permission that the file should have.
3851da177e4SLinus Torvalds  * @parent: a pointer to the parent dentry for this file.  This should be a
386e227867fSMasanari Iida  *          directory dentry if set.  If this parameter is NULL, then the
3871da177e4SLinus Torvalds  *          file will be created in the root of the debugfs filesystem.
3881da177e4SLinus Torvalds  * @data: a pointer to something that the caller will want to get to later
3898e18e294STheodore Ts'o  *        on.  The inode.i_private pointer will point to this value on
3901da177e4SLinus Torvalds  *        the open() call.
3911da177e4SLinus Torvalds  * @fops: a pointer to a struct file_operations that should be used for
3921da177e4SLinus Torvalds  *        this file.
3931da177e4SLinus Torvalds  *
3941da177e4SLinus Torvalds  * This is the basic "create a file" function for debugfs.  It allows for a
395be030e65SAlberto Bertogli  * wide range of flexibility in creating a file, or a directory (if you want
396be030e65SAlberto Bertogli  * to create a directory, the debugfs_create_dir() function is
3971da177e4SLinus Torvalds  * recommended to be used instead.)
3981da177e4SLinus Torvalds  *
3991da177e4SLinus Torvalds  * This function will return a pointer to a dentry if it succeeds.  This
4001da177e4SLinus Torvalds  * pointer must be passed to the debugfs_remove() function when the file is
4011da177e4SLinus Torvalds  * to be removed (no automatic cleanup happens if your module is unloaded,
402ff9fb72bSGreg Kroah-Hartman  * you are responsible here.)  If an error occurs, %ERR_PTR(-ERROR) will be
403ff9fb72bSGreg Kroah-Hartman  * returned.
4041da177e4SLinus Torvalds  *
4056468b3afSRandy Dunlap  * If debugfs is not enabled in the kernel, the value -%ENODEV will be
406873760fbSCornelia Huck  * returned.
4071da177e4SLinus Torvalds  */
408f4ae40a6SAl Viro struct dentry *debugfs_create_file(const char *name, umode_t mode,
4091da177e4SLinus Torvalds 				   struct dentry *parent, void *data,
41099ac48f5SArjan van de Ven 				   const struct file_operations *fops)
4111da177e4SLinus Torvalds {
412c3b1a350SAl Viro 
41349d200deSNicolai Stange 	return __debugfs_create_file(name, mode, parent, data,
41449d200deSNicolai Stange 				fops ? &debugfs_full_proxy_file_operations :
41549d200deSNicolai Stange 					&debugfs_noop_file_operations,
41649d200deSNicolai Stange 				fops);
4171da177e4SLinus Torvalds }
4181da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(debugfs_create_file);
4191da177e4SLinus Torvalds 
420c6468808SNicolai Stange /**
421c6468808SNicolai Stange  * debugfs_create_file_unsafe - create a file in the debugfs filesystem
422c6468808SNicolai Stange  * @name: a pointer to a string containing the name of the file to create.
423c6468808SNicolai Stange  * @mode: the permission that the file should have.
424c6468808SNicolai Stange  * @parent: a pointer to the parent dentry for this file.  This should be a
425c6468808SNicolai Stange  *          directory dentry if set.  If this parameter is NULL, then the
426c6468808SNicolai Stange  *          file will be created in the root of the debugfs filesystem.
427c6468808SNicolai Stange  * @data: a pointer to something that the caller will want to get to later
428c6468808SNicolai Stange  *        on.  The inode.i_private pointer will point to this value on
429c6468808SNicolai Stange  *        the open() call.
430c6468808SNicolai Stange  * @fops: a pointer to a struct file_operations that should be used for
431c6468808SNicolai Stange  *        this file.
432c6468808SNicolai Stange  *
433c6468808SNicolai Stange  * debugfs_create_file_unsafe() is completely analogous to
434c6468808SNicolai Stange  * debugfs_create_file(), the only difference being that the fops
435c6468808SNicolai Stange  * handed it will not get protected against file removals by the
436c6468808SNicolai Stange  * debugfs core.
437c6468808SNicolai Stange  *
438c6468808SNicolai Stange  * It is your responsibility to protect your struct file_operation
4390eeb2731SSergey Senozhatsky  * methods against file removals by means of debugfs_file_get()
4400eeb2731SSergey Senozhatsky  * and debugfs_file_put(). ->open() is still protected by
441c6468808SNicolai Stange  * debugfs though.
442c6468808SNicolai Stange  *
443c6468808SNicolai Stange  * Any struct file_operations defined by means of
444c6468808SNicolai Stange  * DEFINE_DEBUGFS_ATTRIBUTE() is protected against file removals and
445c6468808SNicolai Stange  * thus, may be used here.
446c6468808SNicolai Stange  */
44749d200deSNicolai Stange struct dentry *debugfs_create_file_unsafe(const char *name, umode_t mode,
44849d200deSNicolai Stange 				   struct dentry *parent, void *data,
44949d200deSNicolai Stange 				   const struct file_operations *fops)
45049d200deSNicolai Stange {
45149d200deSNicolai Stange 
45249d200deSNicolai Stange 	return __debugfs_create_file(name, mode, parent, data,
45349d200deSNicolai Stange 				fops ? &debugfs_open_proxy_file_operations :
45449d200deSNicolai Stange 					&debugfs_noop_file_operations,
45549d200deSNicolai Stange 				fops);
45649d200deSNicolai Stange }
457c6468808SNicolai Stange EXPORT_SYMBOL_GPL(debugfs_create_file_unsafe);
45849d200deSNicolai Stange 
4591da177e4SLinus Torvalds /**
460e59b4e91SDavid Howells  * debugfs_create_file_size - create a file in the debugfs filesystem
461e59b4e91SDavid Howells  * @name: a pointer to a string containing the name of the file to create.
462e59b4e91SDavid Howells  * @mode: the permission that the file should have.
463e59b4e91SDavid Howells  * @parent: a pointer to the parent dentry for this file.  This should be a
464e59b4e91SDavid Howells  *          directory dentry if set.  If this parameter is NULL, then the
465e59b4e91SDavid Howells  *          file will be created in the root of the debugfs filesystem.
466e59b4e91SDavid Howells  * @data: a pointer to something that the caller will want to get to later
467e59b4e91SDavid Howells  *        on.  The inode.i_private pointer will point to this value on
468e59b4e91SDavid Howells  *        the open() call.
469e59b4e91SDavid Howells  * @fops: a pointer to a struct file_operations that should be used for
470e59b4e91SDavid Howells  *        this file.
471e59b4e91SDavid Howells  * @file_size: initial file size
472e59b4e91SDavid Howells  *
473e59b4e91SDavid Howells  * This is the basic "create a file" function for debugfs.  It allows for a
474e59b4e91SDavid Howells  * wide range of flexibility in creating a file, or a directory (if you want
475e59b4e91SDavid Howells  * to create a directory, the debugfs_create_dir() function is
476e59b4e91SDavid Howells  * recommended to be used instead.)
477e59b4e91SDavid Howells  *
478e59b4e91SDavid Howells  * This function will return a pointer to a dentry if it succeeds.  This
479e59b4e91SDavid Howells  * pointer must be passed to the debugfs_remove() function when the file is
480e59b4e91SDavid Howells  * to be removed (no automatic cleanup happens if your module is unloaded,
481ff9fb72bSGreg Kroah-Hartman  * you are responsible here.)  If an error occurs, %ERR_PTR(-ERROR) will be
482ff9fb72bSGreg Kroah-Hartman  * returned.
483e59b4e91SDavid Howells  *
484e59b4e91SDavid Howells  * If debugfs is not enabled in the kernel, the value -%ENODEV will be
485e59b4e91SDavid Howells  * returned.
486e59b4e91SDavid Howells  */
487e59b4e91SDavid Howells struct dentry *debugfs_create_file_size(const char *name, umode_t mode,
488e59b4e91SDavid Howells 					struct dentry *parent, void *data,
489e59b4e91SDavid Howells 					const struct file_operations *fops,
490e59b4e91SDavid Howells 					loff_t file_size)
491e59b4e91SDavid Howells {
492e59b4e91SDavid Howells 	struct dentry *de = debugfs_create_file(name, mode, parent, data, fops);
493e59b4e91SDavid Howells 
494e59b4e91SDavid Howells 	if (de)
4952b0143b5SDavid Howells 		d_inode(de)->i_size = file_size;
496e59b4e91SDavid Howells 	return de;
497e59b4e91SDavid Howells }
498e59b4e91SDavid Howells EXPORT_SYMBOL_GPL(debugfs_create_file_size);
499e59b4e91SDavid Howells 
500e59b4e91SDavid Howells /**
5011da177e4SLinus Torvalds  * debugfs_create_dir - create a directory in the debugfs filesystem
5021da177e4SLinus Torvalds  * @name: a pointer to a string containing the name of the directory to
5031da177e4SLinus Torvalds  *        create.
5041da177e4SLinus Torvalds  * @parent: a pointer to the parent dentry for this file.  This should be a
505e227867fSMasanari Iida  *          directory dentry if set.  If this parameter is NULL, then the
5061da177e4SLinus Torvalds  *          directory will be created in the root of the debugfs filesystem.
5071da177e4SLinus Torvalds  *
5081da177e4SLinus Torvalds  * This function creates a directory in debugfs with the given name.
5091da177e4SLinus Torvalds  *
5101da177e4SLinus Torvalds  * This function will return a pointer to a dentry if it succeeds.  This
5111da177e4SLinus Torvalds  * pointer must be passed to the debugfs_remove() function when the file is
5121da177e4SLinus Torvalds  * to be removed (no automatic cleanup happens if your module is unloaded,
513ff9fb72bSGreg Kroah-Hartman  * you are responsible here.)  If an error occurs, %ERR_PTR(-ERROR) will be
514ff9fb72bSGreg Kroah-Hartman  * returned.
5151da177e4SLinus Torvalds  *
5166468b3afSRandy Dunlap  * If debugfs is not enabled in the kernel, the value -%ENODEV will be
517873760fbSCornelia Huck  * returned.
5181da177e4SLinus Torvalds  */
5191da177e4SLinus Torvalds struct dentry *debugfs_create_dir(const char *name, struct dentry *parent)
5201da177e4SLinus Torvalds {
521ad5abd5bSAl Viro 	struct dentry *dentry = start_creating(name, parent);
522680b3024SAl Viro 	struct inode *inode;
523ad5abd5bSAl Viro 
524ad5abd5bSAl Viro 	if (IS_ERR(dentry))
525ff9fb72bSGreg Kroah-Hartman 		return dentry;
526ad5abd5bSAl Viro 
527edac65eaSAl Viro 	inode = debugfs_get_inode(dentry->d_sb);
52843e23b6cSGreg Kroah-Hartman 	if (unlikely(!inode)) {
52943e23b6cSGreg Kroah-Hartman 		pr_err("out of free dentries, can not create directory '%s'\n",
53043e23b6cSGreg Kroah-Hartman 		       name);
5315233e311SAl Viro 		return failed_creating(dentry);
53243e23b6cSGreg Kroah-Hartman 	}
533680b3024SAl Viro 
534f5b7769eSLinus Torvalds 	inode->i_mode = S_IFDIR | S_IRWXU | S_IRUGO | S_IXUGO;
535edac65eaSAl Viro 	inode->i_op = &simple_dir_inode_operations;
536edac65eaSAl Viro 	inode->i_fop = &simple_dir_operations;
537edac65eaSAl Viro 
538edac65eaSAl Viro 	/* directory inodes start off with i_nlink == 2 (for "." entry) */
539edac65eaSAl Viro 	inc_nlink(inode);
540680b3024SAl Viro 	d_instantiate(dentry, inode);
5412b0143b5SDavid Howells 	inc_nlink(d_inode(dentry->d_parent));
5422b0143b5SDavid Howells 	fsnotify_mkdir(d_inode(dentry->d_parent), dentry);
5435233e311SAl Viro 	return end_creating(dentry);
5441da177e4SLinus Torvalds }
5451da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(debugfs_create_dir);
5461da177e4SLinus Torvalds 
5471da177e4SLinus Torvalds /**
54877b3da6eSAl Viro  * debugfs_create_automount - create automount point in the debugfs filesystem
54977b3da6eSAl Viro  * @name: a pointer to a string containing the name of the file to create.
55077b3da6eSAl Viro  * @parent: a pointer to the parent dentry for this file.  This should be a
55177b3da6eSAl Viro  *          directory dentry if set.  If this parameter is NULL, then the
55277b3da6eSAl Viro  *          file will be created in the root of the debugfs filesystem.
55377b3da6eSAl Viro  * @f: function to be called when pathname resolution steps on that one.
55477b3da6eSAl Viro  * @data: opaque argument to pass to f().
55577b3da6eSAl Viro  *
55677b3da6eSAl Viro  * @f should return what ->d_automount() would.
55777b3da6eSAl Viro  */
55877b3da6eSAl Viro struct dentry *debugfs_create_automount(const char *name,
55977b3da6eSAl Viro 					struct dentry *parent,
56093faccbbSEric W. Biederman 					debugfs_automount_t f,
56177b3da6eSAl Viro 					void *data)
56277b3da6eSAl Viro {
56377b3da6eSAl Viro 	struct dentry *dentry = start_creating(name, parent);
56477b3da6eSAl Viro 	struct inode *inode;
56577b3da6eSAl Viro 
56677b3da6eSAl Viro 	if (IS_ERR(dentry))
567ff9fb72bSGreg Kroah-Hartman 		return dentry;
56877b3da6eSAl Viro 
56977b3da6eSAl Viro 	inode = debugfs_get_inode(dentry->d_sb);
57043e23b6cSGreg Kroah-Hartman 	if (unlikely(!inode)) {
57143e23b6cSGreg Kroah-Hartman 		pr_err("out of free dentries, can not create automount '%s'\n",
57243e23b6cSGreg Kroah-Hartman 		       name);
57377b3da6eSAl Viro 		return failed_creating(dentry);
57443e23b6cSGreg Kroah-Hartman 	}
57577b3da6eSAl Viro 
57687243debSSeth Forshee 	make_empty_dir_inode(inode);
57777b3da6eSAl Viro 	inode->i_flags |= S_AUTOMOUNT;
57877b3da6eSAl Viro 	inode->i_private = data;
57977b3da6eSAl Viro 	dentry->d_fsdata = (void *)f;
580a8f324a4SRoman Pen 	/* directory inodes start off with i_nlink == 2 (for "." entry) */
581a8f324a4SRoman Pen 	inc_nlink(inode);
58277b3da6eSAl Viro 	d_instantiate(dentry, inode);
583a8f324a4SRoman Pen 	inc_nlink(d_inode(dentry->d_parent));
584a8f324a4SRoman Pen 	fsnotify_mkdir(d_inode(dentry->d_parent), dentry);
58577b3da6eSAl Viro 	return end_creating(dentry);
58677b3da6eSAl Viro }
58777b3da6eSAl Viro EXPORT_SYMBOL(debugfs_create_automount);
58877b3da6eSAl Viro 
58977b3da6eSAl Viro /**
59066f54963SPeter Oberparleiter  * debugfs_create_symlink- create a symbolic link in the debugfs filesystem
59166f54963SPeter Oberparleiter  * @name: a pointer to a string containing the name of the symbolic link to
59266f54963SPeter Oberparleiter  *        create.
59366f54963SPeter Oberparleiter  * @parent: a pointer to the parent dentry for this symbolic link.  This
594e227867fSMasanari Iida  *          should be a directory dentry if set.  If this parameter is NULL,
59566f54963SPeter Oberparleiter  *          then the symbolic link will be created in the root of the debugfs
59666f54963SPeter Oberparleiter  *          filesystem.
59766f54963SPeter Oberparleiter  * @target: a pointer to a string containing the path to the target of the
59866f54963SPeter Oberparleiter  *          symbolic link.
59966f54963SPeter Oberparleiter  *
60066f54963SPeter Oberparleiter  * This function creates a symbolic link with the given name in debugfs that
60166f54963SPeter Oberparleiter  * links to the given target path.
60266f54963SPeter Oberparleiter  *
60366f54963SPeter Oberparleiter  * This function will return a pointer to a dentry if it succeeds.  This
60466f54963SPeter Oberparleiter  * pointer must be passed to the debugfs_remove() function when the symbolic
60566f54963SPeter Oberparleiter  * link is to be removed (no automatic cleanup happens if your module is
606ff9fb72bSGreg Kroah-Hartman  * unloaded, you are responsible here.)  If an error occurs, %ERR_PTR(-ERROR)
607ff9fb72bSGreg Kroah-Hartman  * will be returned.
60866f54963SPeter Oberparleiter  *
60966f54963SPeter Oberparleiter  * If debugfs is not enabled in the kernel, the value -%ENODEV will be
610873760fbSCornelia Huck  * returned.
61166f54963SPeter Oberparleiter  */
61266f54963SPeter Oberparleiter struct dentry *debugfs_create_symlink(const char *name, struct dentry *parent,
61366f54963SPeter Oberparleiter 				      const char *target)
61466f54963SPeter Oberparleiter {
615ad5abd5bSAl Viro 	struct dentry *dentry;
616680b3024SAl Viro 	struct inode *inode;
617680b3024SAl Viro 	char *link = kstrdup(target, GFP_KERNEL);
61866f54963SPeter Oberparleiter 	if (!link)
619ff9fb72bSGreg Kroah-Hartman 		return ERR_PTR(-ENOMEM);
62066f54963SPeter Oberparleiter 
621ad5abd5bSAl Viro 	dentry = start_creating(name, parent);
622ad5abd5bSAl Viro 	if (IS_ERR(dentry)) {
62366f54963SPeter Oberparleiter 		kfree(link);
624ff9fb72bSGreg Kroah-Hartman 		return dentry;
625ad5abd5bSAl Viro 	}
626ad5abd5bSAl Viro 
627edac65eaSAl Viro 	inode = debugfs_get_inode(dentry->d_sb);
628680b3024SAl Viro 	if (unlikely(!inode)) {
62943e23b6cSGreg Kroah-Hartman 		pr_err("out of free dentries, can not create symlink '%s'\n",
63043e23b6cSGreg Kroah-Hartman 		       name);
631ad5abd5bSAl Viro 		kfree(link);
6325233e311SAl Viro 		return failed_creating(dentry);
633680b3024SAl Viro 	}
634edac65eaSAl Viro 	inode->i_mode = S_IFLNK | S_IRWXUGO;
6355723cb01SAl Viro 	inode->i_op = &simple_symlink_inode_operations;
6365723cb01SAl Viro 	inode->i_link = link;
637680b3024SAl Viro 	d_instantiate(dentry, inode);
6385233e311SAl Viro 	return end_creating(dentry);
63966f54963SPeter Oberparleiter }
64066f54963SPeter Oberparleiter EXPORT_SYMBOL_GPL(debugfs_create_symlink);
64166f54963SPeter Oberparleiter 
642e9117a5aSNicolai Stange static void __debugfs_remove_file(struct dentry *dentry, struct dentry *parent)
643e9117a5aSNicolai Stange {
644e9117a5aSNicolai Stange 	struct debugfs_fsdata *fsd;
645e9117a5aSNicolai Stange 
646e9117a5aSNicolai Stange 	simple_unlink(d_inode(parent), dentry);
647e9117a5aSNicolai Stange 	d_delete(dentry);
6487d39bc50SNicolai Stange 
6497d39bc50SNicolai Stange 	/*
6507d39bc50SNicolai Stange 	 * Paired with the closing smp_mb() implied by a successful
6517d39bc50SNicolai Stange 	 * cmpxchg() in debugfs_file_get(): either
6527d39bc50SNicolai Stange 	 * debugfs_file_get() must see a dead dentry or we must see a
6537d39bc50SNicolai Stange 	 * debugfs_fsdata instance at ->d_fsdata here (or both).
6547d39bc50SNicolai Stange 	 */
6557d39bc50SNicolai Stange 	smp_mb();
6567d39bc50SNicolai Stange 	fsd = READ_ONCE(dentry->d_fsdata);
6577d39bc50SNicolai Stange 	if ((unsigned long)fsd & DEBUGFS_FSDATA_IS_REAL_FOPS_BIT)
6587d39bc50SNicolai Stange 		return;
659e9117a5aSNicolai Stange 	if (!refcount_dec_and_test(&fsd->active_users))
660e9117a5aSNicolai Stange 		wait_for_completion(&fsd->active_users_drained);
661e9117a5aSNicolai Stange }
662e9117a5aSNicolai Stange 
66325d41d84SJan Kara static int __debugfs_remove(struct dentry *dentry, struct dentry *parent)
6641da177e4SLinus Torvalds {
66565c33336SMathieu Desnoyers 	int ret = 0;
6661da177e4SLinus Torvalds 
667dc3f4198SAl Viro 	if (simple_positive(dentry)) {
66829a7f3adSMathieu Desnoyers 		dget(dentry);
669e9117a5aSNicolai Stange 		if (!d_is_reg(dentry)) {
6707ceab50cSDavid Howells 			if (d_is_dir(dentry))
6712b0143b5SDavid Howells 				ret = simple_rmdir(d_inode(parent), dentry);
6720db59e59SAl Viro 			else
6732b0143b5SDavid Howells 				simple_unlink(d_inode(parent), dentry);
67429a7f3adSMathieu Desnoyers 			if (!ret)
67529a7f3adSMathieu Desnoyers 				d_delete(dentry);
676e9117a5aSNicolai Stange 		} else {
677e9117a5aSNicolai Stange 			__debugfs_remove_file(dentry, parent);
678e9117a5aSNicolai Stange 		}
67929a7f3adSMathieu Desnoyers 		dput(dentry);
6801da177e4SLinus Torvalds 	}
68125d41d84SJan Kara 	return ret;
6829505e637SHaavard Skinnemoen }
6839505e637SHaavard Skinnemoen 
6849505e637SHaavard Skinnemoen /**
6859505e637SHaavard Skinnemoen  * debugfs_remove - removes a file or directory from the debugfs filesystem
6869505e637SHaavard Skinnemoen  * @dentry: a pointer to a the dentry of the file or directory to be
687398dc4adSUlf Magnusson  *          removed.  If this parameter is NULL or an error value, nothing
688398dc4adSUlf Magnusson  *          will be done.
6899505e637SHaavard Skinnemoen  *
6909505e637SHaavard Skinnemoen  * This function removes a file or directory in debugfs that was previously
6919505e637SHaavard Skinnemoen  * created with a call to another debugfs function (like
6929505e637SHaavard Skinnemoen  * debugfs_create_file() or variants thereof.)
6939505e637SHaavard Skinnemoen  *
6949505e637SHaavard Skinnemoen  * This function is required to be called in order for the file to be
6959505e637SHaavard Skinnemoen  * removed, no automatic cleanup of files will happen when a module is
6969505e637SHaavard Skinnemoen  * removed, you are responsible here.
6979505e637SHaavard Skinnemoen  */
6989505e637SHaavard Skinnemoen void debugfs_remove(struct dentry *dentry)
6999505e637SHaavard Skinnemoen {
7009505e637SHaavard Skinnemoen 	struct dentry *parent;
70125d41d84SJan Kara 	int ret;
7029505e637SHaavard Skinnemoen 
703a59d6293SArend van Spriel 	if (IS_ERR_OR_NULL(dentry))
7049505e637SHaavard Skinnemoen 		return;
7059505e637SHaavard Skinnemoen 
7069505e637SHaavard Skinnemoen 	parent = dentry->d_parent;
7075955102cSAl Viro 	inode_lock(d_inode(parent));
70825d41d84SJan Kara 	ret = __debugfs_remove(dentry, parent);
7095955102cSAl Viro 	inode_unlock(d_inode(parent));
71025d41d84SJan Kara 	if (!ret)
7111da177e4SLinus Torvalds 		simple_release_fs(&debugfs_mount, &debugfs_mount_count);
7121da177e4SLinus Torvalds }
7131da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(debugfs_remove);
7141da177e4SLinus Torvalds 
715cfc94cdfSJan Kara /**
7169505e637SHaavard Skinnemoen  * debugfs_remove_recursive - recursively removes a directory
717398dc4adSUlf Magnusson  * @dentry: a pointer to a the dentry of the directory to be removed.  If this
718398dc4adSUlf Magnusson  *          parameter is NULL or an error value, nothing will be done.
7199505e637SHaavard Skinnemoen  *
7209505e637SHaavard Skinnemoen  * This function recursively removes a directory tree in debugfs that
7219505e637SHaavard Skinnemoen  * was previously created with a call to another debugfs function
7229505e637SHaavard Skinnemoen  * (like debugfs_create_file() or variants thereof.)
7239505e637SHaavard Skinnemoen  *
7249505e637SHaavard Skinnemoen  * This function is required to be called in order for the file to be
7259505e637SHaavard Skinnemoen  * removed, no automatic cleanup of files will happen when a module is
7269505e637SHaavard Skinnemoen  * removed, you are responsible here.
7279505e637SHaavard Skinnemoen  */
7289505e637SHaavard Skinnemoen void debugfs_remove_recursive(struct dentry *dentry)
7299505e637SHaavard Skinnemoen {
730485d4402SSteven Rostedt 	struct dentry *child, *parent;
7319505e637SHaavard Skinnemoen 
732a59d6293SArend van Spriel 	if (IS_ERR_OR_NULL(dentry))
7339505e637SHaavard Skinnemoen 		return;
7349505e637SHaavard Skinnemoen 
7359505e637SHaavard Skinnemoen 	parent = dentry;
736776164c1SOleg Nesterov  down:
7375955102cSAl Viro 	inode_lock(d_inode(parent));
738485d4402SSteven Rostedt  loop:
739485d4402SSteven Rostedt 	/*
740485d4402SSteven Rostedt 	 * The parent->d_subdirs is protected by the d_lock. Outside that
741485d4402SSteven Rostedt 	 * lock, the child can be unlinked and set to be freed which can
742485d4402SSteven Rostedt 	 * use the d_u.d_child as the rcu head and corrupt this list.
743485d4402SSteven Rostedt 	 */
744485d4402SSteven Rostedt 	spin_lock(&parent->d_lock);
745946e51f2SAl Viro 	list_for_each_entry(child, &parent->d_subdirs, d_child) {
746dc3f4198SAl Viro 		if (!simple_positive(child))
747776164c1SOleg Nesterov 			continue;
7489505e637SHaavard Skinnemoen 
749776164c1SOleg Nesterov 		/* perhaps simple_empty(child) makes more sense */
7509505e637SHaavard Skinnemoen 		if (!list_empty(&child->d_subdirs)) {
751485d4402SSteven Rostedt 			spin_unlock(&parent->d_lock);
7525955102cSAl Viro 			inode_unlock(d_inode(parent));
7539505e637SHaavard Skinnemoen 			parent = child;
754776164c1SOleg Nesterov 			goto down;
7559505e637SHaavard Skinnemoen 		}
756485d4402SSteven Rostedt 
757485d4402SSteven Rostedt 		spin_unlock(&parent->d_lock);
758485d4402SSteven Rostedt 
759776164c1SOleg Nesterov 		if (!__debugfs_remove(child, parent))
760776164c1SOleg Nesterov 			simple_release_fs(&debugfs_mount, &debugfs_mount_count);
761485d4402SSteven Rostedt 
762485d4402SSteven Rostedt 		/*
763485d4402SSteven Rostedt 		 * The parent->d_lock protects agaist child from unlinking
764485d4402SSteven Rostedt 		 * from d_subdirs. When releasing the parent->d_lock we can
765485d4402SSteven Rostedt 		 * no longer trust that the next pointer is valid.
766485d4402SSteven Rostedt 		 * Restart the loop. We'll skip this one with the
767dc3f4198SAl Viro 		 * simple_positive() check.
768485d4402SSteven Rostedt 		 */
769485d4402SSteven Rostedt 		goto loop;
770776164c1SOleg Nesterov 	}
771485d4402SSteven Rostedt 	spin_unlock(&parent->d_lock);
772776164c1SOleg Nesterov 
7735955102cSAl Viro 	inode_unlock(d_inode(parent));
774776164c1SOleg Nesterov 	child = parent;
775776164c1SOleg Nesterov 	parent = parent->d_parent;
7765955102cSAl Viro 	inode_lock(d_inode(parent));
777776164c1SOleg Nesterov 
778485d4402SSteven Rostedt 	if (child != dentry)
779485d4402SSteven Rostedt 		/* go up */
780485d4402SSteven Rostedt 		goto loop;
78156a83cc9SSteven Rostedt 
782776164c1SOleg Nesterov 	if (!__debugfs_remove(child, parent))
7839505e637SHaavard Skinnemoen 		simple_release_fs(&debugfs_mount, &debugfs_mount_count);
7845955102cSAl Viro 	inode_unlock(d_inode(parent));
7859505e637SHaavard Skinnemoen }
7869505e637SHaavard Skinnemoen EXPORT_SYMBOL_GPL(debugfs_remove_recursive);
7879505e637SHaavard Skinnemoen 
7889505e637SHaavard Skinnemoen /**
789cfc94cdfSJan Kara  * debugfs_rename - rename a file/directory in the debugfs filesystem
790cfc94cdfSJan Kara  * @old_dir: a pointer to the parent dentry for the renamed object. This
791cfc94cdfSJan Kara  *          should be a directory dentry.
792cfc94cdfSJan Kara  * @old_dentry: dentry of an object to be renamed.
793cfc94cdfSJan Kara  * @new_dir: a pointer to the parent dentry where the object should be
794cfc94cdfSJan Kara  *          moved. This should be a directory dentry.
795cfc94cdfSJan Kara  * @new_name: a pointer to a string containing the target name.
796cfc94cdfSJan Kara  *
797cfc94cdfSJan Kara  * This function renames a file/directory in debugfs.  The target must not
798cfc94cdfSJan Kara  * exist for rename to succeed.
799cfc94cdfSJan Kara  *
800cfc94cdfSJan Kara  * This function will return a pointer to old_dentry (which is updated to
801cfc94cdfSJan Kara  * reflect renaming) if it succeeds. If an error occurs, %NULL will be
802cfc94cdfSJan Kara  * returned.
803cfc94cdfSJan Kara  *
804cfc94cdfSJan Kara  * If debugfs is not enabled in the kernel, the value -%ENODEV will be
805cfc94cdfSJan Kara  * returned.
806cfc94cdfSJan Kara  */
807cfc94cdfSJan Kara struct dentry *debugfs_rename(struct dentry *old_dir, struct dentry *old_dentry,
808cfc94cdfSJan Kara 		struct dentry *new_dir, const char *new_name)
809cfc94cdfSJan Kara {
810cfc94cdfSJan Kara 	int error;
811cfc94cdfSJan Kara 	struct dentry *dentry = NULL, *trap;
81249d31c2fSAl Viro 	struct name_snapshot old_name;
813cfc94cdfSJan Kara 
814d88c93f0SGreg Kroah-Hartman 	if (IS_ERR(old_dir))
815d88c93f0SGreg Kroah-Hartman 		return old_dir;
816d88c93f0SGreg Kroah-Hartman 	if (IS_ERR(new_dir))
817d88c93f0SGreg Kroah-Hartman 		return new_dir;
818d88c93f0SGreg Kroah-Hartman 	if (IS_ERR_OR_NULL(old_dentry))
819d88c93f0SGreg Kroah-Hartman 		return old_dentry;
820d88c93f0SGreg Kroah-Hartman 
821cfc94cdfSJan Kara 	trap = lock_rename(new_dir, old_dir);
822cfc94cdfSJan Kara 	/* Source or destination directories don't exist? */
8232b0143b5SDavid Howells 	if (d_really_is_negative(old_dir) || d_really_is_negative(new_dir))
824cfc94cdfSJan Kara 		goto exit;
825cfc94cdfSJan Kara 	/* Source does not exist, cyclic rename, or mountpoint? */
8262b0143b5SDavid Howells 	if (d_really_is_negative(old_dentry) || old_dentry == trap ||
827cfc94cdfSJan Kara 	    d_mountpoint(old_dentry))
828cfc94cdfSJan Kara 		goto exit;
829cfc94cdfSJan Kara 	dentry = lookup_one_len(new_name, new_dir, strlen(new_name));
830cfc94cdfSJan Kara 	/* Lookup failed, cyclic rename or target exists? */
8312b0143b5SDavid Howells 	if (IS_ERR(dentry) || dentry == trap || d_really_is_positive(dentry))
832cfc94cdfSJan Kara 		goto exit;
833cfc94cdfSJan Kara 
83449d31c2fSAl Viro 	take_dentry_name_snapshot(&old_name, old_dentry);
835cfc94cdfSJan Kara 
8362b0143b5SDavid Howells 	error = simple_rename(d_inode(old_dir), old_dentry, d_inode(new_dir),
837e0e0be8aSMiklos Szeredi 			      dentry, 0);
838cfc94cdfSJan Kara 	if (error) {
83949d31c2fSAl Viro 		release_dentry_name_snapshot(&old_name);
840cfc94cdfSJan Kara 		goto exit;
841cfc94cdfSJan Kara 	}
842cfc94cdfSJan Kara 	d_move(old_dentry, dentry);
843f4ec3a3dSAl Viro 	fsnotify_move(d_inode(old_dir), d_inode(new_dir), &old_name.name,
844e36cb0b8SDavid Howells 		d_is_dir(old_dentry),
8455a190ae6SAl Viro 		NULL, old_dentry);
84649d31c2fSAl Viro 	release_dentry_name_snapshot(&old_name);
847cfc94cdfSJan Kara 	unlock_rename(new_dir, old_dir);
848cfc94cdfSJan Kara 	dput(dentry);
849cfc94cdfSJan Kara 	return old_dentry;
850cfc94cdfSJan Kara exit:
851cfc94cdfSJan Kara 	if (dentry && !IS_ERR(dentry))
852cfc94cdfSJan Kara 		dput(dentry);
853cfc94cdfSJan Kara 	unlock_rename(new_dir, old_dir);
854ff9fb72bSGreg Kroah-Hartman 	if (IS_ERR(dentry))
855ff9fb72bSGreg Kroah-Hartman 		return dentry;
856ff9fb72bSGreg Kroah-Hartman 	return ERR_PTR(-EINVAL);
857cfc94cdfSJan Kara }
858cfc94cdfSJan Kara EXPORT_SYMBOL_GPL(debugfs_rename);
859cfc94cdfSJan Kara 
860c0f92ba9SFrederic Weisbecker /**
861c0f92ba9SFrederic Weisbecker  * debugfs_initialized - Tells whether debugfs has been registered
862c0f92ba9SFrederic Weisbecker  */
863c0f92ba9SFrederic Weisbecker bool debugfs_initialized(void)
864c0f92ba9SFrederic Weisbecker {
865c0f92ba9SFrederic Weisbecker 	return debugfs_registered;
866c0f92ba9SFrederic Weisbecker }
867c0f92ba9SFrederic Weisbecker EXPORT_SYMBOL_GPL(debugfs_initialized);
868c0f92ba9SFrederic Weisbecker 
8691da177e4SLinus Torvalds static int __init debugfs_init(void)
8701da177e4SLinus Torvalds {
8711da177e4SLinus Torvalds 	int retval;
8721da177e4SLinus Torvalds 
873f9bb4882SEric W. Biederman 	retval = sysfs_create_mount_point(kernel_kobj, "debug");
874f9bb4882SEric W. Biederman 	if (retval)
875f9bb4882SEric W. Biederman 		return retval;
8761da177e4SLinus Torvalds 
8771da177e4SLinus Torvalds 	retval = register_filesystem(&debug_fs_type);
8781da177e4SLinus Torvalds 	if (retval)
879f9bb4882SEric W. Biederman 		sysfs_remove_mount_point(kernel_kobj, "debug");
880c0f92ba9SFrederic Weisbecker 	else
881c0f92ba9SFrederic Weisbecker 		debugfs_registered = true;
882c0f92ba9SFrederic Weisbecker 
8831da177e4SLinus Torvalds 	return retval;
8841da177e4SLinus Torvalds }
8851da177e4SLinus Torvalds core_initcall(debugfs_init);
8861da177e4SLinus Torvalds 
887