xref: /linux-6.15/fs/debugfs/inode.c (revision c1632a0f)
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>
295496197fSDavid Howells #include <linux/security.h>
309fd4dcecSNicolai Stange 
319fd4dcecSNicolai Stange #include "internal.h"
321da177e4SLinus Torvalds 
3382aceae4SKees Cook #define DEBUGFS_DEFAULT_MODE	0700
34d6e48686SLudwig Nussel 
351da177e4SLinus Torvalds static struct vfsmount *debugfs_mount;
361da177e4SLinus Torvalds static int debugfs_mount_count;
37c0f92ba9SFrederic Weisbecker static bool debugfs_registered;
38312723a0SKees Cook static unsigned int debugfs_allow __ro_after_init = DEFAULT_DEBUGFS_ALLOW_BITS;
391da177e4SLinus Torvalds 
405496197fSDavid Howells /*
415496197fSDavid Howells  * Don't allow access attributes to be changed whilst the kernel is locked down
425496197fSDavid Howells  * so that we can use the file mode as part of a heuristic to determine whether
435496197fSDavid Howells  * to lock down individual files.
445496197fSDavid Howells  */
45*c1632a0fSChristian Brauner static int debugfs_setattr(struct mnt_idmap *idmap,
46549c7297SChristian Brauner 			   struct dentry *dentry, struct iattr *ia)
475496197fSDavid Howells {
485881fa8dSOndrej Mosnacek 	int ret;
495496197fSDavid Howells 
505881fa8dSOndrej Mosnacek 	if (ia->ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID)) {
515881fa8dSOndrej Mosnacek 		ret = security_locked_down(LOCKDOWN_DEBUGFS);
525881fa8dSOndrej Mosnacek 		if (ret)
535496197fSDavid Howells 			return ret;
545881fa8dSOndrej Mosnacek 	}
55*c1632a0fSChristian Brauner 	return simple_setattr(&nop_mnt_idmap, dentry, ia);
565496197fSDavid Howells }
575496197fSDavid Howells 
585496197fSDavid Howells static const struct inode_operations debugfs_file_inode_operations = {
595496197fSDavid Howells 	.setattr	= debugfs_setattr,
605496197fSDavid Howells };
615496197fSDavid Howells static const struct inode_operations debugfs_dir_inode_operations = {
625496197fSDavid Howells 	.lookup		= simple_lookup,
635496197fSDavid Howells 	.setattr	= debugfs_setattr,
645496197fSDavid Howells };
655496197fSDavid Howells static const struct inode_operations debugfs_symlink_inode_operations = {
665496197fSDavid Howells 	.get_link	= simple_get_link,
675496197fSDavid Howells 	.setattr	= debugfs_setattr,
685496197fSDavid Howells };
695496197fSDavid Howells 
70edac65eaSAl Viro static struct inode *debugfs_get_inode(struct super_block *sb)
711da177e4SLinus Torvalds {
721da177e4SLinus Torvalds 	struct inode *inode = new_inode(sb);
731da177e4SLinus Torvalds 	if (inode) {
7485fe4025SChristoph Hellwig 		inode->i_ino = get_next_ino();
751b48b530SDeepa Dinamani 		inode->i_atime = inode->i_mtime =
76c2050a45SDeepa Dinamani 			inode->i_ctime = current_time(inode);
771da177e4SLinus Torvalds 	}
781da177e4SLinus Torvalds 	return inode;
791da177e4SLinus Torvalds }
801da177e4SLinus Torvalds 
81d6e48686SLudwig Nussel struct debugfs_mount_opts {
827dc05881SEric W. Biederman 	kuid_t uid;
837dc05881SEric W. Biederman 	kgid_t gid;
84d6e48686SLudwig Nussel 	umode_t mode;
85b8de524cSBrian Norris 	/* Opt_* bitfield. */
86b8de524cSBrian Norris 	unsigned int opts;
87d6e48686SLudwig Nussel };
88d6e48686SLudwig Nussel 
89d6e48686SLudwig Nussel enum {
90d6e48686SLudwig Nussel 	Opt_uid,
91d6e48686SLudwig Nussel 	Opt_gid,
92d6e48686SLudwig Nussel 	Opt_mode,
93d6e48686SLudwig Nussel 	Opt_err
94d6e48686SLudwig Nussel };
95d6e48686SLudwig Nussel 
96d6e48686SLudwig Nussel static const match_table_t tokens = {
97d6e48686SLudwig Nussel 	{Opt_uid, "uid=%u"},
98d6e48686SLudwig Nussel 	{Opt_gid, "gid=%u"},
99d6e48686SLudwig Nussel 	{Opt_mode, "mode=%o"},
100d6e48686SLudwig Nussel 	{Opt_err, NULL}
101d6e48686SLudwig Nussel };
102d6e48686SLudwig Nussel 
103d6e48686SLudwig Nussel struct debugfs_fs_info {
104d6e48686SLudwig Nussel 	struct debugfs_mount_opts mount_opts;
105d6e48686SLudwig Nussel };
106d6e48686SLudwig Nussel 
107d6e48686SLudwig Nussel static int debugfs_parse_options(char *data, struct debugfs_mount_opts *opts)
108d6e48686SLudwig Nussel {
109d6e48686SLudwig Nussel 	substring_t args[MAX_OPT_ARGS];
110d6e48686SLudwig Nussel 	int option;
111d6e48686SLudwig Nussel 	int token;
1127dc05881SEric W. Biederman 	kuid_t uid;
1137dc05881SEric W. Biederman 	kgid_t gid;
114d6e48686SLudwig Nussel 	char *p;
115d6e48686SLudwig Nussel 
116b8de524cSBrian Norris 	opts->opts = 0;
117d6e48686SLudwig Nussel 	opts->mode = DEBUGFS_DEFAULT_MODE;
118d6e48686SLudwig Nussel 
119d6e48686SLudwig Nussel 	while ((p = strsep(&data, ",")) != NULL) {
120d6e48686SLudwig Nussel 		if (!*p)
121d6e48686SLudwig Nussel 			continue;
122d6e48686SLudwig Nussel 
123d6e48686SLudwig Nussel 		token = match_token(p, tokens, args);
124d6e48686SLudwig Nussel 		switch (token) {
125d6e48686SLudwig Nussel 		case Opt_uid:
126d6e48686SLudwig Nussel 			if (match_int(&args[0], &option))
127d6e48686SLudwig Nussel 				return -EINVAL;
1287dc05881SEric W. Biederman 			uid = make_kuid(current_user_ns(), option);
1297dc05881SEric W. Biederman 			if (!uid_valid(uid))
1307dc05881SEric W. Biederman 				return -EINVAL;
1317dc05881SEric W. Biederman 			opts->uid = uid;
132d6e48686SLudwig Nussel 			break;
133d6e48686SLudwig Nussel 		case Opt_gid:
134f1688e04SDave Reisner 			if (match_int(&args[0], &option))
135d6e48686SLudwig Nussel 				return -EINVAL;
1367dc05881SEric W. Biederman 			gid = make_kgid(current_user_ns(), option);
1377dc05881SEric W. Biederman 			if (!gid_valid(gid))
1387dc05881SEric W. Biederman 				return -EINVAL;
1397dc05881SEric W. Biederman 			opts->gid = gid;
140d6e48686SLudwig Nussel 			break;
141d6e48686SLudwig Nussel 		case Opt_mode:
142d6e48686SLudwig Nussel 			if (match_octal(&args[0], &option))
143d6e48686SLudwig Nussel 				return -EINVAL;
144d6e48686SLudwig Nussel 			opts->mode = option & S_IALLUGO;
145d6e48686SLudwig Nussel 			break;
146d6e48686SLudwig Nussel 		/*
147d6e48686SLudwig Nussel 		 * We might like to report bad mount options here;
148d6e48686SLudwig Nussel 		 * but traditionally debugfs has ignored all mount options
149d6e48686SLudwig Nussel 		 */
150d6e48686SLudwig Nussel 		}
151b8de524cSBrian Norris 
152b8de524cSBrian Norris 		opts->opts |= BIT(token);
153d6e48686SLudwig Nussel 	}
154d6e48686SLudwig Nussel 
155d6e48686SLudwig Nussel 	return 0;
156d6e48686SLudwig Nussel }
157d6e48686SLudwig Nussel 
158b8de524cSBrian Norris static void _debugfs_apply_options(struct super_block *sb, bool remount)
159d6e48686SLudwig Nussel {
160d6e48686SLudwig Nussel 	struct debugfs_fs_info *fsi = sb->s_fs_info;
1612b0143b5SDavid Howells 	struct inode *inode = d_inode(sb->s_root);
162d6e48686SLudwig Nussel 	struct debugfs_mount_opts *opts = &fsi->mount_opts;
163d6e48686SLudwig Nussel 
164b8de524cSBrian Norris 	/*
165b8de524cSBrian Norris 	 * On remount, only reset mode/uid/gid if they were provided as mount
166b8de524cSBrian Norris 	 * options.
167b8de524cSBrian Norris 	 */
168b8de524cSBrian Norris 
169b8de524cSBrian Norris 	if (!remount || opts->opts & BIT(Opt_mode)) {
170d6e48686SLudwig Nussel 		inode->i_mode &= ~S_IALLUGO;
171d6e48686SLudwig Nussel 		inode->i_mode |= opts->mode;
172b8de524cSBrian Norris 	}
173d6e48686SLudwig Nussel 
174b8de524cSBrian Norris 	if (!remount || opts->opts & BIT(Opt_uid))
175d6e48686SLudwig Nussel 		inode->i_uid = opts->uid;
176d6e48686SLudwig Nussel 
177b8de524cSBrian Norris 	if (!remount || opts->opts & BIT(Opt_gid))
178b8de524cSBrian Norris 		inode->i_gid = opts->gid;
179b8de524cSBrian Norris }
180b8de524cSBrian Norris 
181b8de524cSBrian Norris static void debugfs_apply_options(struct super_block *sb)
182b8de524cSBrian Norris {
183b8de524cSBrian Norris 	_debugfs_apply_options(sb, false);
184b8de524cSBrian Norris }
185b8de524cSBrian Norris 
186b8de524cSBrian Norris static void debugfs_apply_options_remount(struct super_block *sb)
187b8de524cSBrian Norris {
188b8de524cSBrian Norris 	_debugfs_apply_options(sb, true);
189d6e48686SLudwig Nussel }
190d6e48686SLudwig Nussel 
191d6e48686SLudwig Nussel static int debugfs_remount(struct super_block *sb, int *flags, char *data)
192d6e48686SLudwig Nussel {
193d6e48686SLudwig Nussel 	int err;
194d6e48686SLudwig Nussel 	struct debugfs_fs_info *fsi = sb->s_fs_info;
195d6e48686SLudwig Nussel 
19602b9984dSTheodore Ts'o 	sync_filesystem(sb);
197d6e48686SLudwig Nussel 	err = debugfs_parse_options(data, &fsi->mount_opts);
198d6e48686SLudwig Nussel 	if (err)
199d6e48686SLudwig Nussel 		goto fail;
200d6e48686SLudwig Nussel 
201b8de524cSBrian Norris 	debugfs_apply_options_remount(sb);
202d6e48686SLudwig Nussel 
203d6e48686SLudwig Nussel fail:
204d6e48686SLudwig Nussel 	return err;
205d6e48686SLudwig Nussel }
206d6e48686SLudwig Nussel 
207d6e48686SLudwig Nussel static int debugfs_show_options(struct seq_file *m, struct dentry *root)
208d6e48686SLudwig Nussel {
209d6e48686SLudwig Nussel 	struct debugfs_fs_info *fsi = root->d_sb->s_fs_info;
210d6e48686SLudwig Nussel 	struct debugfs_mount_opts *opts = &fsi->mount_opts;
211d6e48686SLudwig Nussel 
2127dc05881SEric W. Biederman 	if (!uid_eq(opts->uid, GLOBAL_ROOT_UID))
2137dc05881SEric W. Biederman 		seq_printf(m, ",uid=%u",
2147dc05881SEric W. Biederman 			   from_kuid_munged(&init_user_ns, opts->uid));
2157dc05881SEric W. Biederman 	if (!gid_eq(opts->gid, GLOBAL_ROOT_GID))
2167dc05881SEric W. Biederman 		seq_printf(m, ",gid=%u",
2177dc05881SEric W. Biederman 			   from_kgid_munged(&init_user_ns, opts->gid));
218d6e48686SLudwig Nussel 	if (opts->mode != DEBUGFS_DEFAULT_MODE)
219d6e48686SLudwig Nussel 		seq_printf(m, ",mode=%o", opts->mode);
220d6e48686SLudwig Nussel 
221d6e48686SLudwig Nussel 	return 0;
222d6e48686SLudwig Nussel }
223d6e48686SLudwig Nussel 
2246234ddf4SAl Viro static void debugfs_free_inode(struct inode *inode)
2250db59e59SAl Viro {
2260db59e59SAl Viro 	if (S_ISLNK(inode->i_mode))
2275723cb01SAl Viro 		kfree(inode->i_link);
22893b919daSAl Viro 	free_inode_nonrcu(inode);
22993b919daSAl Viro }
23093b919daSAl Viro 
231d6e48686SLudwig Nussel static const struct super_operations debugfs_super_operations = {
232d6e48686SLudwig Nussel 	.statfs		= simple_statfs,
233d6e48686SLudwig Nussel 	.remount_fs	= debugfs_remount,
234d6e48686SLudwig Nussel 	.show_options	= debugfs_show_options,
2356234ddf4SAl Viro 	.free_inode	= debugfs_free_inode,
236d6e48686SLudwig Nussel };
237d6e48686SLudwig Nussel 
2387c8d4698SNicolai Stange static void debugfs_release_dentry(struct dentry *dentry)
2397c8d4698SNicolai Stange {
2407d39bc50SNicolai Stange 	void *fsd = dentry->d_fsdata;
2417d39bc50SNicolai Stange 
2427d39bc50SNicolai Stange 	if (!((unsigned long)fsd & DEBUGFS_FSDATA_IS_REAL_FOPS_BIT))
2437c8d4698SNicolai Stange 		kfree(dentry->d_fsdata);
2447c8d4698SNicolai Stange }
2457c8d4698SNicolai Stange 
24677b3da6eSAl Viro static struct vfsmount *debugfs_automount(struct path *path)
24777b3da6eSAl Viro {
24893faccbbSEric W. Biederman 	debugfs_automount_t f;
24993faccbbSEric W. Biederman 	f = (debugfs_automount_t)path->dentry->d_fsdata;
25093faccbbSEric W. Biederman 	return f(path->dentry, d_inode(path->dentry)->i_private);
25177b3da6eSAl Viro }
25277b3da6eSAl Viro 
25377b3da6eSAl Viro static const struct dentry_operations debugfs_dops = {
25477b3da6eSAl Viro 	.d_delete = always_delete_dentry,
2557c8d4698SNicolai Stange 	.d_release = debugfs_release_dentry,
25677b3da6eSAl Viro 	.d_automount = debugfs_automount,
25777b3da6eSAl Viro };
25877b3da6eSAl Viro 
2591da177e4SLinus Torvalds static int debug_fill_super(struct super_block *sb, void *data, int silent)
2601da177e4SLinus Torvalds {
261cda37124SEric Biggers 	static const struct tree_descr debug_files[] = {{""}};
262d6e48686SLudwig Nussel 	struct debugfs_fs_info *fsi;
263d6e48686SLudwig Nussel 	int err;
2641da177e4SLinus Torvalds 
265d6e48686SLudwig Nussel 	fsi = kzalloc(sizeof(struct debugfs_fs_info), GFP_KERNEL);
266d6e48686SLudwig Nussel 	sb->s_fs_info = fsi;
267d6e48686SLudwig Nussel 	if (!fsi) {
268d6e48686SLudwig Nussel 		err = -ENOMEM;
269d6e48686SLudwig Nussel 		goto fail;
270d6e48686SLudwig Nussel 	}
271d6e48686SLudwig Nussel 
272d6e48686SLudwig Nussel 	err = debugfs_parse_options(data, &fsi->mount_opts);
273d6e48686SLudwig Nussel 	if (err)
274d6e48686SLudwig Nussel 		goto fail;
275d6e48686SLudwig Nussel 
276d6e48686SLudwig Nussel 	err  =  simple_fill_super(sb, DEBUGFS_MAGIC, debug_files);
277d6e48686SLudwig Nussel 	if (err)
278d6e48686SLudwig Nussel 		goto fail;
279d6e48686SLudwig Nussel 
280d6e48686SLudwig Nussel 	sb->s_op = &debugfs_super_operations;
28177b3da6eSAl Viro 	sb->s_d_op = &debugfs_dops;
282d6e48686SLudwig Nussel 
283d6e48686SLudwig Nussel 	debugfs_apply_options(sb);
284d6e48686SLudwig Nussel 
285d6e48686SLudwig Nussel 	return 0;
286d6e48686SLudwig Nussel 
287d6e48686SLudwig Nussel fail:
288d6e48686SLudwig Nussel 	kfree(fsi);
289d6e48686SLudwig Nussel 	sb->s_fs_info = NULL;
290d6e48686SLudwig Nussel 	return err;
2911da177e4SLinus Torvalds }
2921da177e4SLinus Torvalds 
293fc14f2feSAl Viro static struct dentry *debug_mount(struct file_system_type *fs_type,
2941da177e4SLinus Torvalds 			int flags, const char *dev_name,
295fc14f2feSAl Viro 			void *data)
2961da177e4SLinus Torvalds {
297a24c6f7bSPeter Enderborg 	if (!(debugfs_allow & DEBUGFS_ALLOW_API))
298a24c6f7bSPeter Enderborg 		return ERR_PTR(-EPERM);
299a24c6f7bSPeter Enderborg 
300fc14f2feSAl Viro 	return mount_single(fs_type, flags, data, debug_fill_super);
3011da177e4SLinus Torvalds }
3021da177e4SLinus Torvalds 
3031da177e4SLinus Torvalds static struct file_system_type debug_fs_type = {
3041da177e4SLinus Torvalds 	.owner =	THIS_MODULE,
3051da177e4SLinus Torvalds 	.name =		"debugfs",
306fc14f2feSAl Viro 	.mount =	debug_mount,
3071da177e4SLinus Torvalds 	.kill_sb =	kill_litter_super,
3081da177e4SLinus Torvalds };
3097f78e035SEric W. Biederman MODULE_ALIAS_FS("debugfs");
3101da177e4SLinus Torvalds 
311a7c5437bSOmar Sandoval /**
312a7c5437bSOmar Sandoval  * debugfs_lookup() - look up an existing debugfs file
313a7c5437bSOmar Sandoval  * @name: a pointer to a string containing the name of the file to look up.
314a7c5437bSOmar Sandoval  * @parent: a pointer to the parent dentry of the file.
315a7c5437bSOmar Sandoval  *
316a7c5437bSOmar Sandoval  * This function will return a pointer to a dentry if it succeeds.  If the file
317a7c5437bSOmar Sandoval  * doesn't exist or an error occurs, %NULL will be returned.  The returned
318a7c5437bSOmar Sandoval  * dentry must be passed to dput() when it is no longer needed.
319a7c5437bSOmar Sandoval  *
320a7c5437bSOmar Sandoval  * If debugfs is not enabled in the kernel, the value -%ENODEV will be
321a7c5437bSOmar Sandoval  * returned.
322a7c5437bSOmar Sandoval  */
323a7c5437bSOmar Sandoval struct dentry *debugfs_lookup(const char *name, struct dentry *parent)
324a7c5437bSOmar Sandoval {
325a7c5437bSOmar Sandoval 	struct dentry *dentry;
326a7c5437bSOmar Sandoval 
327bc6de804SGreg Kroah-Hartman 	if (!debugfs_initialized() || IS_ERR_OR_NULL(name) || IS_ERR(parent))
328a7c5437bSOmar Sandoval 		return NULL;
329a7c5437bSOmar Sandoval 
330a7c5437bSOmar Sandoval 	if (!parent)
331a7c5437bSOmar Sandoval 		parent = debugfs_mount->mnt_root;
332a7c5437bSOmar Sandoval 
3336c2d4798SAl Viro 	dentry = lookup_positive_unlocked(name, parent, strlen(name));
334a7c5437bSOmar Sandoval 	if (IS_ERR(dentry))
335a7c5437bSOmar Sandoval 		return NULL;
336a7c5437bSOmar Sandoval 	return dentry;
337a7c5437bSOmar Sandoval }
338a7c5437bSOmar Sandoval EXPORT_SYMBOL_GPL(debugfs_lookup);
339a7c5437bSOmar Sandoval 
340190afd81SAl Viro static struct dentry *start_creating(const char *name, struct dentry *parent)
341c3b1a350SAl Viro {
342190afd81SAl Viro 	struct dentry *dentry;
343c3b1a350SAl Viro 	int error;
344c3b1a350SAl Viro 
345a24c6f7bSPeter Enderborg 	if (!(debugfs_allow & DEBUGFS_ALLOW_API))
346a24c6f7bSPeter Enderborg 		return ERR_PTR(-EPERM);
347a24c6f7bSPeter Enderborg 
34856348560SGreg Kroah-Hartman 	if (!debugfs_initialized())
34956348560SGreg Kroah-Hartman 		return ERR_PTR(-ENOENT);
35056348560SGreg Kroah-Hartman 
351d03ae477SGreg Kroah-Hartman 	pr_debug("creating file '%s'\n", name);
352c3b1a350SAl Viro 
353c9e15f25SGreg KH 	if (IS_ERR(parent))
354c9e15f25SGreg KH 		return parent;
355c9e15f25SGreg KH 
356c3b1a350SAl Viro 	error = simple_pin_fs(&debug_fs_type, &debugfs_mount,
357c3b1a350SAl Viro 			      &debugfs_mount_count);
35843e23b6cSGreg Kroah-Hartman 	if (error) {
35943e23b6cSGreg Kroah-Hartman 		pr_err("Unable to pin filesystem for file '%s'\n", name);
360190afd81SAl Viro 		return ERR_PTR(error);
36143e23b6cSGreg Kroah-Hartman 	}
362c3b1a350SAl Viro 
363cfa57c11SAl Viro 	/* If the parent is not specified, we create it in the root.
364cfa57c11SAl Viro 	 * We need the root dentry to do this, which is in the super
365cfa57c11SAl Viro 	 * block. A pointer to that is in the struct vfsmount that we
366cfa57c11SAl Viro 	 * have around.
367cfa57c11SAl Viro 	 */
368cfa57c11SAl Viro 	if (!parent)
369cfa57c11SAl Viro 		parent = debugfs_mount->mnt_root;
370cfa57c11SAl Viro 
3715955102cSAl Viro 	inode_lock(d_inode(parent));
372a3d1e7ebSAl Viro 	if (unlikely(IS_DEADDIR(d_inode(parent))))
373a3d1e7ebSAl Viro 		dentry = ERR_PTR(-ENOENT);
374a3d1e7ebSAl Viro 	else
375cfa57c11SAl Viro 		dentry = lookup_one_len(name, parent, strlen(name));
3762b0143b5SDavid Howells 	if (!IS_ERR(dentry) && d_really_is_positive(dentry)) {
377c33d4423SGreg Kroah-Hartman 		if (d_is_dir(dentry))
378c33d4423SGreg Kroah-Hartman 			pr_err("Directory '%s' with parent '%s' already present!\n",
379c33d4423SGreg Kroah-Hartman 			       name, parent->d_name.name);
380c33d4423SGreg Kroah-Hartman 		else
381c33d4423SGreg Kroah-Hartman 			pr_err("File '%s' in directory '%s' already present!\n",
382c33d4423SGreg Kroah-Hartman 			       name, parent->d_name.name);
383190afd81SAl Viro 		dput(dentry);
384190afd81SAl Viro 		dentry = ERR_PTR(-EEXIST);
385190afd81SAl Viro 	}
3860ee9608cSDaniel Borkmann 
3870ee9608cSDaniel Borkmann 	if (IS_ERR(dentry)) {
3885955102cSAl Viro 		inode_unlock(d_inode(parent));
3890ee9608cSDaniel Borkmann 		simple_release_fs(&debugfs_mount, &debugfs_mount_count);
3900ee9608cSDaniel Borkmann 	}
3910ee9608cSDaniel Borkmann 
392190afd81SAl Viro 	return dentry;
393190afd81SAl Viro }
394190afd81SAl Viro 
3955233e311SAl Viro static struct dentry *failed_creating(struct dentry *dentry)
396190afd81SAl Viro {
3975955102cSAl Viro 	inode_unlock(d_inode(dentry->d_parent));
398190afd81SAl Viro 	dput(dentry);
399190afd81SAl Viro 	simple_release_fs(&debugfs_mount, &debugfs_mount_count);
400ff9fb72bSGreg Kroah-Hartman 	return ERR_PTR(-ENOMEM);
401190afd81SAl Viro }
4025233e311SAl Viro 
4035233e311SAl Viro static struct dentry *end_creating(struct dentry *dentry)
4045233e311SAl Viro {
4055955102cSAl Viro 	inode_unlock(d_inode(dentry->d_parent));
406190afd81SAl Viro 	return dentry;
407190afd81SAl Viro }
408190afd81SAl Viro 
40949d200deSNicolai Stange static struct dentry *__debugfs_create_file(const char *name, umode_t mode,
41049d200deSNicolai Stange 				struct dentry *parent, void *data,
41149d200deSNicolai Stange 				const struct file_operations *proxy_fops,
41249d200deSNicolai Stange 				const struct file_operations *real_fops)
41349d200deSNicolai Stange {
41449d200deSNicolai Stange 	struct dentry *dentry;
41549d200deSNicolai Stange 	struct inode *inode;
41649d200deSNicolai Stange 
41749d200deSNicolai Stange 	if (!(mode & S_IFMT))
41849d200deSNicolai Stange 		mode |= S_IFREG;
41949d200deSNicolai Stange 	BUG_ON(!S_ISREG(mode));
42049d200deSNicolai Stange 	dentry = start_creating(name, parent);
42149d200deSNicolai Stange 
4227d39bc50SNicolai Stange 	if (IS_ERR(dentry))
423ff9fb72bSGreg Kroah-Hartman 		return dentry;
42449d200deSNicolai Stange 
425a24c6f7bSPeter Enderborg 	if (!(debugfs_allow & DEBUGFS_ALLOW_API)) {
426a24c6f7bSPeter Enderborg 		failed_creating(dentry);
427a24c6f7bSPeter Enderborg 		return ERR_PTR(-EPERM);
428a24c6f7bSPeter Enderborg 	}
429a24c6f7bSPeter Enderborg 
43049d200deSNicolai Stange 	inode = debugfs_get_inode(dentry->d_sb);
43143e23b6cSGreg Kroah-Hartman 	if (unlikely(!inode)) {
43243e23b6cSGreg Kroah-Hartman 		pr_err("out of free dentries, can not create file '%s'\n",
43343e23b6cSGreg Kroah-Hartman 		       name);
43449d200deSNicolai Stange 		return failed_creating(dentry);
43543e23b6cSGreg Kroah-Hartman 	}
43649d200deSNicolai Stange 
43749d200deSNicolai Stange 	inode->i_mode = mode;
43849d200deSNicolai Stange 	inode->i_private = data;
43949d200deSNicolai Stange 
4405496197fSDavid Howells 	inode->i_op = &debugfs_file_inode_operations;
44149d200deSNicolai Stange 	inode->i_fop = proxy_fops;
4427d39bc50SNicolai Stange 	dentry->d_fsdata = (void *)((unsigned long)real_fops |
4437d39bc50SNicolai Stange 				DEBUGFS_FSDATA_IS_REAL_FOPS_BIT);
44449d200deSNicolai Stange 
44549d200deSNicolai Stange 	d_instantiate(dentry, inode);
44649d200deSNicolai Stange 	fsnotify_create(d_inode(dentry->d_parent), dentry);
44749d200deSNicolai Stange 	return end_creating(dentry);
44849d200deSNicolai Stange }
44949d200deSNicolai Stange 
4501da177e4SLinus Torvalds /**
4511da177e4SLinus Torvalds  * debugfs_create_file - create a file in the debugfs filesystem
4521da177e4SLinus Torvalds  * @name: a pointer to a string containing the name of the file to create.
453be030e65SAlberto Bertogli  * @mode: the permission that the file should have.
4541da177e4SLinus Torvalds  * @parent: a pointer to the parent dentry for this file.  This should be a
455e227867fSMasanari Iida  *          directory dentry if set.  If this parameter is NULL, then the
4561da177e4SLinus Torvalds  *          file will be created in the root of the debugfs filesystem.
4571da177e4SLinus Torvalds  * @data: a pointer to something that the caller will want to get to later
4588e18e294STheodore Ts'o  *        on.  The inode.i_private pointer will point to this value on
4591da177e4SLinus Torvalds  *        the open() call.
4601da177e4SLinus Torvalds  * @fops: a pointer to a struct file_operations that should be used for
4611da177e4SLinus Torvalds  *        this file.
4621da177e4SLinus Torvalds  *
4631da177e4SLinus Torvalds  * This is the basic "create a file" function for debugfs.  It allows for a
464be030e65SAlberto Bertogli  * wide range of flexibility in creating a file, or a directory (if you want
465be030e65SAlberto Bertogli  * to create a directory, the debugfs_create_dir() function is
4661da177e4SLinus Torvalds  * recommended to be used instead.)
4671da177e4SLinus Torvalds  *
4681da177e4SLinus Torvalds  * This function will return a pointer to a dentry if it succeeds.  This
4691da177e4SLinus Torvalds  * pointer must be passed to the debugfs_remove() function when the file is
4701da177e4SLinus Torvalds  * to be removed (no automatic cleanup happens if your module is unloaded,
471adc92dd4SDaniel W. S. Almeida  * you are responsible here.)  If an error occurs, ERR_PTR(-ERROR) will be
472ff9fb72bSGreg Kroah-Hartman  * returned.
4731da177e4SLinus Torvalds  *
4746468b3afSRandy Dunlap  * If debugfs is not enabled in the kernel, the value -%ENODEV will be
475873760fbSCornelia Huck  * returned.
4768bcbde2bSDouglas Anderson  *
4778bcbde2bSDouglas Anderson  * NOTE: it's expected that most callers should _ignore_ the errors returned
4788bcbde2bSDouglas Anderson  * by this function. Other debugfs functions handle the fact that the "dentry"
4798bcbde2bSDouglas Anderson  * passed to them could be an error and they don't crash in that case.
4808bcbde2bSDouglas Anderson  * Drivers should generally work fine even if debugfs fails to init anyway.
4811da177e4SLinus Torvalds  */
482f4ae40a6SAl Viro struct dentry *debugfs_create_file(const char *name, umode_t mode,
4831da177e4SLinus Torvalds 				   struct dentry *parent, void *data,
48499ac48f5SArjan van de Ven 				   const struct file_operations *fops)
4851da177e4SLinus Torvalds {
486c3b1a350SAl Viro 
48749d200deSNicolai Stange 	return __debugfs_create_file(name, mode, parent, data,
48849d200deSNicolai Stange 				fops ? &debugfs_full_proxy_file_operations :
48949d200deSNicolai Stange 					&debugfs_noop_file_operations,
49049d200deSNicolai Stange 				fops);
4911da177e4SLinus Torvalds }
4921da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(debugfs_create_file);
4931da177e4SLinus Torvalds 
494c6468808SNicolai Stange /**
495c6468808SNicolai Stange  * debugfs_create_file_unsafe - create a file in the debugfs filesystem
496c6468808SNicolai Stange  * @name: a pointer to a string containing the name of the file to create.
497c6468808SNicolai Stange  * @mode: the permission that the file should have.
498c6468808SNicolai Stange  * @parent: a pointer to the parent dentry for this file.  This should be a
499c6468808SNicolai Stange  *          directory dentry if set.  If this parameter is NULL, then the
500c6468808SNicolai Stange  *          file will be created in the root of the debugfs filesystem.
501c6468808SNicolai Stange  * @data: a pointer to something that the caller will want to get to later
502c6468808SNicolai Stange  *        on.  The inode.i_private pointer will point to this value on
503c6468808SNicolai Stange  *        the open() call.
504c6468808SNicolai Stange  * @fops: a pointer to a struct file_operations that should be used for
505c6468808SNicolai Stange  *        this file.
506c6468808SNicolai Stange  *
507c6468808SNicolai Stange  * debugfs_create_file_unsafe() is completely analogous to
508c6468808SNicolai Stange  * debugfs_create_file(), the only difference being that the fops
509c6468808SNicolai Stange  * handed it will not get protected against file removals by the
510c6468808SNicolai Stange  * debugfs core.
511c6468808SNicolai Stange  *
512c6468808SNicolai Stange  * It is your responsibility to protect your struct file_operation
5130eeb2731SSergey Senozhatsky  * methods against file removals by means of debugfs_file_get()
5140eeb2731SSergey Senozhatsky  * and debugfs_file_put(). ->open() is still protected by
515c6468808SNicolai Stange  * debugfs though.
516c6468808SNicolai Stange  *
517c6468808SNicolai Stange  * Any struct file_operations defined by means of
518c6468808SNicolai Stange  * DEFINE_DEBUGFS_ATTRIBUTE() is protected against file removals and
519c6468808SNicolai Stange  * thus, may be used here.
520c6468808SNicolai Stange  */
52149d200deSNicolai Stange struct dentry *debugfs_create_file_unsafe(const char *name, umode_t mode,
52249d200deSNicolai Stange 				   struct dentry *parent, void *data,
52349d200deSNicolai Stange 				   const struct file_operations *fops)
52449d200deSNicolai Stange {
52549d200deSNicolai Stange 
52649d200deSNicolai Stange 	return __debugfs_create_file(name, mode, parent, data,
52749d200deSNicolai Stange 				fops ? &debugfs_open_proxy_file_operations :
52849d200deSNicolai Stange 					&debugfs_noop_file_operations,
52949d200deSNicolai Stange 				fops);
53049d200deSNicolai Stange }
531c6468808SNicolai Stange EXPORT_SYMBOL_GPL(debugfs_create_file_unsafe);
53249d200deSNicolai Stange 
5331da177e4SLinus Torvalds /**
534e59b4e91SDavid Howells  * debugfs_create_file_size - create a file in the debugfs filesystem
535e59b4e91SDavid Howells  * @name: a pointer to a string containing the name of the file to create.
536e59b4e91SDavid Howells  * @mode: the permission that the file should have.
537e59b4e91SDavid Howells  * @parent: a pointer to the parent dentry for this file.  This should be a
538e59b4e91SDavid Howells  *          directory dentry if set.  If this parameter is NULL, then the
539e59b4e91SDavid Howells  *          file will be created in the root of the debugfs filesystem.
540e59b4e91SDavid Howells  * @data: a pointer to something that the caller will want to get to later
541e59b4e91SDavid Howells  *        on.  The inode.i_private pointer will point to this value on
542e59b4e91SDavid Howells  *        the open() call.
543e59b4e91SDavid Howells  * @fops: a pointer to a struct file_operations that should be used for
544e59b4e91SDavid Howells  *        this file.
545e59b4e91SDavid Howells  * @file_size: initial file size
546e59b4e91SDavid Howells  *
547e59b4e91SDavid Howells  * This is the basic "create a file" function for debugfs.  It allows for a
548e59b4e91SDavid Howells  * wide range of flexibility in creating a file, or a directory (if you want
549e59b4e91SDavid Howells  * to create a directory, the debugfs_create_dir() function is
550e59b4e91SDavid Howells  * recommended to be used instead.)
551e59b4e91SDavid Howells  */
552526ee72dSGreg Kroah-Hartman void debugfs_create_file_size(const char *name, umode_t mode,
553e59b4e91SDavid Howells 			      struct dentry *parent, void *data,
554e59b4e91SDavid Howells 			      const struct file_operations *fops,
555e59b4e91SDavid Howells 			      loff_t file_size)
556e59b4e91SDavid Howells {
557e59b4e91SDavid Howells 	struct dentry *de = debugfs_create_file(name, mode, parent, data, fops);
558e59b4e91SDavid Howells 
559af505cadSNirmoy Das 	if (!IS_ERR(de))
5602b0143b5SDavid Howells 		d_inode(de)->i_size = file_size;
561e59b4e91SDavid Howells }
562e59b4e91SDavid Howells EXPORT_SYMBOL_GPL(debugfs_create_file_size);
563e59b4e91SDavid Howells 
564e59b4e91SDavid Howells /**
5651da177e4SLinus Torvalds  * debugfs_create_dir - create a directory in the debugfs filesystem
5661da177e4SLinus Torvalds  * @name: a pointer to a string containing the name of the directory to
5671da177e4SLinus Torvalds  *        create.
5681da177e4SLinus Torvalds  * @parent: a pointer to the parent dentry for this file.  This should be a
569e227867fSMasanari Iida  *          directory dentry if set.  If this parameter is NULL, then the
5701da177e4SLinus Torvalds  *          directory will be created in the root of the debugfs filesystem.
5711da177e4SLinus Torvalds  *
5721da177e4SLinus Torvalds  * This function creates a directory in debugfs with the given name.
5731da177e4SLinus Torvalds  *
5741da177e4SLinus Torvalds  * This function will return a pointer to a dentry if it succeeds.  This
5751da177e4SLinus Torvalds  * pointer must be passed to the debugfs_remove() function when the file is
5761da177e4SLinus Torvalds  * to be removed (no automatic cleanup happens if your module is unloaded,
577adc92dd4SDaniel W. S. Almeida  * you are responsible here.)  If an error occurs, ERR_PTR(-ERROR) will be
578ff9fb72bSGreg Kroah-Hartman  * returned.
5791da177e4SLinus Torvalds  *
5806468b3afSRandy Dunlap  * If debugfs is not enabled in the kernel, the value -%ENODEV will be
581873760fbSCornelia Huck  * returned.
5828bcbde2bSDouglas Anderson  *
5838bcbde2bSDouglas Anderson  * NOTE: it's expected that most callers should _ignore_ the errors returned
5848bcbde2bSDouglas Anderson  * by this function. Other debugfs functions handle the fact that the "dentry"
5858bcbde2bSDouglas Anderson  * passed to them could be an error and they don't crash in that case.
5868bcbde2bSDouglas Anderson  * Drivers should generally work fine even if debugfs fails to init anyway.
5871da177e4SLinus Torvalds  */
5881da177e4SLinus Torvalds struct dentry *debugfs_create_dir(const char *name, struct dentry *parent)
5891da177e4SLinus Torvalds {
590ad5abd5bSAl Viro 	struct dentry *dentry = start_creating(name, parent);
591680b3024SAl Viro 	struct inode *inode;
592ad5abd5bSAl Viro 
593ad5abd5bSAl Viro 	if (IS_ERR(dentry))
594ff9fb72bSGreg Kroah-Hartman 		return dentry;
595ad5abd5bSAl Viro 
596a24c6f7bSPeter Enderborg 	if (!(debugfs_allow & DEBUGFS_ALLOW_API)) {
597a24c6f7bSPeter Enderborg 		failed_creating(dentry);
598a24c6f7bSPeter Enderborg 		return ERR_PTR(-EPERM);
599a24c6f7bSPeter Enderborg 	}
600a24c6f7bSPeter Enderborg 
601edac65eaSAl Viro 	inode = debugfs_get_inode(dentry->d_sb);
60243e23b6cSGreg Kroah-Hartman 	if (unlikely(!inode)) {
60343e23b6cSGreg Kroah-Hartman 		pr_err("out of free dentries, can not create directory '%s'\n",
60443e23b6cSGreg Kroah-Hartman 		       name);
6055233e311SAl Viro 		return failed_creating(dentry);
60643e23b6cSGreg Kroah-Hartman 	}
607680b3024SAl Viro 
608f5b7769eSLinus Torvalds 	inode->i_mode = S_IFDIR | S_IRWXU | S_IRUGO | S_IXUGO;
6095496197fSDavid Howells 	inode->i_op = &debugfs_dir_inode_operations;
610edac65eaSAl Viro 	inode->i_fop = &simple_dir_operations;
611edac65eaSAl Viro 
612edac65eaSAl Viro 	/* directory inodes start off with i_nlink == 2 (for "." entry) */
613edac65eaSAl Viro 	inc_nlink(inode);
614680b3024SAl Viro 	d_instantiate(dentry, inode);
6152b0143b5SDavid Howells 	inc_nlink(d_inode(dentry->d_parent));
6162b0143b5SDavid Howells 	fsnotify_mkdir(d_inode(dentry->d_parent), dentry);
6175233e311SAl Viro 	return end_creating(dentry);
6181da177e4SLinus Torvalds }
6191da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(debugfs_create_dir);
6201da177e4SLinus Torvalds 
6211da177e4SLinus Torvalds /**
62277b3da6eSAl Viro  * debugfs_create_automount - create automount point in the debugfs filesystem
62377b3da6eSAl Viro  * @name: a pointer to a string containing the name of the file to create.
62477b3da6eSAl Viro  * @parent: a pointer to the parent dentry for this file.  This should be a
62577b3da6eSAl Viro  *          directory dentry if set.  If this parameter is NULL, then the
62677b3da6eSAl Viro  *          file will be created in the root of the debugfs filesystem.
62777b3da6eSAl Viro  * @f: function to be called when pathname resolution steps on that one.
62877b3da6eSAl Viro  * @data: opaque argument to pass to f().
62977b3da6eSAl Viro  *
63077b3da6eSAl Viro  * @f should return what ->d_automount() would.
63177b3da6eSAl Viro  */
63277b3da6eSAl Viro struct dentry *debugfs_create_automount(const char *name,
63377b3da6eSAl Viro 					struct dentry *parent,
63493faccbbSEric W. Biederman 					debugfs_automount_t f,
63577b3da6eSAl Viro 					void *data)
63677b3da6eSAl Viro {
63777b3da6eSAl Viro 	struct dentry *dentry = start_creating(name, parent);
63877b3da6eSAl Viro 	struct inode *inode;
63977b3da6eSAl Viro 
64077b3da6eSAl Viro 	if (IS_ERR(dentry))
641ff9fb72bSGreg Kroah-Hartman 		return dentry;
64277b3da6eSAl Viro 
643a24c6f7bSPeter Enderborg 	if (!(debugfs_allow & DEBUGFS_ALLOW_API)) {
644a24c6f7bSPeter Enderborg 		failed_creating(dentry);
645a24c6f7bSPeter Enderborg 		return ERR_PTR(-EPERM);
646a24c6f7bSPeter Enderborg 	}
647a24c6f7bSPeter Enderborg 
64877b3da6eSAl Viro 	inode = debugfs_get_inode(dentry->d_sb);
64943e23b6cSGreg Kroah-Hartman 	if (unlikely(!inode)) {
65043e23b6cSGreg Kroah-Hartman 		pr_err("out of free dentries, can not create automount '%s'\n",
65143e23b6cSGreg Kroah-Hartman 		       name);
65277b3da6eSAl Viro 		return failed_creating(dentry);
65343e23b6cSGreg Kroah-Hartman 	}
65477b3da6eSAl Viro 
65587243debSSeth Forshee 	make_empty_dir_inode(inode);
65677b3da6eSAl Viro 	inode->i_flags |= S_AUTOMOUNT;
65777b3da6eSAl Viro 	inode->i_private = data;
65877b3da6eSAl Viro 	dentry->d_fsdata = (void *)f;
659a8f324a4SRoman Pen 	/* directory inodes start off with i_nlink == 2 (for "." entry) */
660a8f324a4SRoman Pen 	inc_nlink(inode);
66177b3da6eSAl Viro 	d_instantiate(dentry, inode);
662a8f324a4SRoman Pen 	inc_nlink(d_inode(dentry->d_parent));
663a8f324a4SRoman Pen 	fsnotify_mkdir(d_inode(dentry->d_parent), dentry);
66477b3da6eSAl Viro 	return end_creating(dentry);
66577b3da6eSAl Viro }
66677b3da6eSAl Viro EXPORT_SYMBOL(debugfs_create_automount);
66777b3da6eSAl Viro 
66877b3da6eSAl Viro /**
66966f54963SPeter Oberparleiter  * debugfs_create_symlink- create a symbolic link in the debugfs filesystem
67066f54963SPeter Oberparleiter  * @name: a pointer to a string containing the name of the symbolic link to
67166f54963SPeter Oberparleiter  *        create.
67266f54963SPeter Oberparleiter  * @parent: a pointer to the parent dentry for this symbolic link.  This
673e227867fSMasanari Iida  *          should be a directory dentry if set.  If this parameter is NULL,
67466f54963SPeter Oberparleiter  *          then the symbolic link will be created in the root of the debugfs
67566f54963SPeter Oberparleiter  *          filesystem.
67666f54963SPeter Oberparleiter  * @target: a pointer to a string containing the path to the target of the
67766f54963SPeter Oberparleiter  *          symbolic link.
67866f54963SPeter Oberparleiter  *
67966f54963SPeter Oberparleiter  * This function creates a symbolic link with the given name in debugfs that
68066f54963SPeter Oberparleiter  * links to the given target path.
68166f54963SPeter Oberparleiter  *
68266f54963SPeter Oberparleiter  * This function will return a pointer to a dentry if it succeeds.  This
68366f54963SPeter Oberparleiter  * pointer must be passed to the debugfs_remove() function when the symbolic
68466f54963SPeter Oberparleiter  * link is to be removed (no automatic cleanup happens if your module is
685adc92dd4SDaniel W. S. Almeida  * unloaded, you are responsible here.)  If an error occurs, ERR_PTR(-ERROR)
686ff9fb72bSGreg Kroah-Hartman  * will be returned.
68766f54963SPeter Oberparleiter  *
68866f54963SPeter Oberparleiter  * If debugfs is not enabled in the kernel, the value -%ENODEV will be
689873760fbSCornelia Huck  * returned.
69066f54963SPeter Oberparleiter  */
69166f54963SPeter Oberparleiter struct dentry *debugfs_create_symlink(const char *name, struct dentry *parent,
69266f54963SPeter Oberparleiter 				      const char *target)
69366f54963SPeter Oberparleiter {
694ad5abd5bSAl Viro 	struct dentry *dentry;
695680b3024SAl Viro 	struct inode *inode;
696680b3024SAl Viro 	char *link = kstrdup(target, GFP_KERNEL);
69766f54963SPeter Oberparleiter 	if (!link)
698ff9fb72bSGreg Kroah-Hartman 		return ERR_PTR(-ENOMEM);
69966f54963SPeter Oberparleiter 
700ad5abd5bSAl Viro 	dentry = start_creating(name, parent);
701ad5abd5bSAl Viro 	if (IS_ERR(dentry)) {
70266f54963SPeter Oberparleiter 		kfree(link);
703ff9fb72bSGreg Kroah-Hartman 		return dentry;
704ad5abd5bSAl Viro 	}
705ad5abd5bSAl Viro 
706edac65eaSAl Viro 	inode = debugfs_get_inode(dentry->d_sb);
707680b3024SAl Viro 	if (unlikely(!inode)) {
70843e23b6cSGreg Kroah-Hartman 		pr_err("out of free dentries, can not create symlink '%s'\n",
70943e23b6cSGreg Kroah-Hartman 		       name);
710ad5abd5bSAl Viro 		kfree(link);
7115233e311SAl Viro 		return failed_creating(dentry);
712680b3024SAl Viro 	}
713edac65eaSAl Viro 	inode->i_mode = S_IFLNK | S_IRWXUGO;
7145496197fSDavid Howells 	inode->i_op = &debugfs_symlink_inode_operations;
7155723cb01SAl Viro 	inode->i_link = link;
716680b3024SAl Viro 	d_instantiate(dentry, inode);
7175233e311SAl Viro 	return end_creating(dentry);
71866f54963SPeter Oberparleiter }
71966f54963SPeter Oberparleiter EXPORT_SYMBOL_GPL(debugfs_create_symlink);
72066f54963SPeter Oberparleiter 
721823e545cSAmir Goldstein static void __debugfs_file_removed(struct dentry *dentry)
722e9117a5aSNicolai Stange {
723e9117a5aSNicolai Stange 	struct debugfs_fsdata *fsd;
724e9117a5aSNicolai Stange 
7257d39bc50SNicolai Stange 	/*
7267d39bc50SNicolai Stange 	 * Paired with the closing smp_mb() implied by a successful
7277d39bc50SNicolai Stange 	 * cmpxchg() in debugfs_file_get(): either
7287d39bc50SNicolai Stange 	 * debugfs_file_get() must see a dead dentry or we must see a
7297d39bc50SNicolai Stange 	 * debugfs_fsdata instance at ->d_fsdata here (or both).
7307d39bc50SNicolai Stange 	 */
7317d39bc50SNicolai Stange 	smp_mb();
7327d39bc50SNicolai Stange 	fsd = READ_ONCE(dentry->d_fsdata);
7337d39bc50SNicolai Stange 	if ((unsigned long)fsd & DEBUGFS_FSDATA_IS_REAL_FOPS_BIT)
7347d39bc50SNicolai Stange 		return;
735e9117a5aSNicolai Stange 	if (!refcount_dec_and_test(&fsd->active_users))
736e9117a5aSNicolai Stange 		wait_for_completion(&fsd->active_users_drained);
737e9117a5aSNicolai Stange }
738e9117a5aSNicolai Stange 
739a3d1e7ebSAl Viro static void remove_one(struct dentry *victim)
7401da177e4SLinus Torvalds {
741a3d1e7ebSAl Viro         if (d_is_reg(victim))
742a3d1e7ebSAl Viro 		__debugfs_file_removed(victim);
7431da177e4SLinus Torvalds 	simple_release_fs(&debugfs_mount, &debugfs_mount_count);
7441da177e4SLinus Torvalds }
7451da177e4SLinus Torvalds 
746cfc94cdfSJan Kara /**
747a3d1e7ebSAl Viro  * debugfs_remove - recursively removes a directory
748398dc4adSUlf Magnusson  * @dentry: a pointer to a the dentry of the directory to be removed.  If this
749398dc4adSUlf Magnusson  *          parameter is NULL or an error value, nothing will be done.
7509505e637SHaavard Skinnemoen  *
7519505e637SHaavard Skinnemoen  * This function recursively removes a directory tree in debugfs that
7529505e637SHaavard Skinnemoen  * was previously created with a call to another debugfs function
7539505e637SHaavard Skinnemoen  * (like debugfs_create_file() or variants thereof.)
7549505e637SHaavard Skinnemoen  *
7559505e637SHaavard Skinnemoen  * This function is required to be called in order for the file to be
7569505e637SHaavard Skinnemoen  * removed, no automatic cleanup of files will happen when a module is
7579505e637SHaavard Skinnemoen  * removed, you are responsible here.
7589505e637SHaavard Skinnemoen  */
759a3d1e7ebSAl Viro void debugfs_remove(struct dentry *dentry)
7609505e637SHaavard Skinnemoen {
761a59d6293SArend van Spriel 	if (IS_ERR_OR_NULL(dentry))
7629505e637SHaavard Skinnemoen 		return;
7639505e637SHaavard Skinnemoen 
764a3d1e7ebSAl Viro 	simple_pin_fs(&debug_fs_type, &debugfs_mount, &debugfs_mount_count);
765a3d1e7ebSAl Viro 	simple_recursive_removal(dentry, remove_one);
766776164c1SOleg Nesterov 	simple_release_fs(&debugfs_mount, &debugfs_mount_count);
767776164c1SOleg Nesterov }
768a3d1e7ebSAl Viro EXPORT_SYMBOL_GPL(debugfs_remove);
7699505e637SHaavard Skinnemoen 
7709505e637SHaavard Skinnemoen /**
771dec9b2f1SGreg Kroah-Hartman  * debugfs_lookup_and_remove - lookup a directory or file and recursively remove it
772dec9b2f1SGreg Kroah-Hartman  * @name: a pointer to a string containing the name of the item to look up.
773dec9b2f1SGreg Kroah-Hartman  * @parent: a pointer to the parent dentry of the item.
774dec9b2f1SGreg Kroah-Hartman  *
775dec9b2f1SGreg Kroah-Hartman  * This is the equlivant of doing something like
776dec9b2f1SGreg Kroah-Hartman  * debugfs_remove(debugfs_lookup(..)) but with the proper reference counting
777dec9b2f1SGreg Kroah-Hartman  * handled for the directory being looked up.
778dec9b2f1SGreg Kroah-Hartman  */
779dec9b2f1SGreg Kroah-Hartman void debugfs_lookup_and_remove(const char *name, struct dentry *parent)
780dec9b2f1SGreg Kroah-Hartman {
781dec9b2f1SGreg Kroah-Hartman 	struct dentry *dentry;
782dec9b2f1SGreg Kroah-Hartman 
783dec9b2f1SGreg Kroah-Hartman 	dentry = debugfs_lookup(name, parent);
784dec9b2f1SGreg Kroah-Hartman 	if (!dentry)
785dec9b2f1SGreg Kroah-Hartman 		return;
786dec9b2f1SGreg Kroah-Hartman 
787dec9b2f1SGreg Kroah-Hartman 	debugfs_remove(dentry);
788dec9b2f1SGreg Kroah-Hartman 	dput(dentry);
789dec9b2f1SGreg Kroah-Hartman }
790dec9b2f1SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(debugfs_lookup_and_remove);
791dec9b2f1SGreg Kroah-Hartman 
792dec9b2f1SGreg Kroah-Hartman /**
793cfc94cdfSJan Kara  * debugfs_rename - rename a file/directory in the debugfs filesystem
794cfc94cdfSJan Kara  * @old_dir: a pointer to the parent dentry for the renamed object. This
795cfc94cdfSJan Kara  *          should be a directory dentry.
796cfc94cdfSJan Kara  * @old_dentry: dentry of an object to be renamed.
797cfc94cdfSJan Kara  * @new_dir: a pointer to the parent dentry where the object should be
798cfc94cdfSJan Kara  *          moved. This should be a directory dentry.
799cfc94cdfSJan Kara  * @new_name: a pointer to a string containing the target name.
800cfc94cdfSJan Kara  *
801cfc94cdfSJan Kara  * This function renames a file/directory in debugfs.  The target must not
802cfc94cdfSJan Kara  * exist for rename to succeed.
803cfc94cdfSJan Kara  *
804cfc94cdfSJan Kara  * This function will return a pointer to old_dentry (which is updated to
805cfc94cdfSJan Kara  * reflect renaming) if it succeeds. If an error occurs, %NULL will be
806cfc94cdfSJan Kara  * returned.
807cfc94cdfSJan Kara  *
808cfc94cdfSJan Kara  * If debugfs is not enabled in the kernel, the value -%ENODEV will be
809cfc94cdfSJan Kara  * returned.
810cfc94cdfSJan Kara  */
811cfc94cdfSJan Kara struct dentry *debugfs_rename(struct dentry *old_dir, struct dentry *old_dentry,
812cfc94cdfSJan Kara 		struct dentry *new_dir, const char *new_name)
813cfc94cdfSJan Kara {
814cfc94cdfSJan Kara 	int error;
815cfc94cdfSJan Kara 	struct dentry *dentry = NULL, *trap;
81649d31c2fSAl Viro 	struct name_snapshot old_name;
817cfc94cdfSJan Kara 
818d88c93f0SGreg Kroah-Hartman 	if (IS_ERR(old_dir))
819d88c93f0SGreg Kroah-Hartman 		return old_dir;
820d88c93f0SGreg Kroah-Hartman 	if (IS_ERR(new_dir))
821d88c93f0SGreg Kroah-Hartman 		return new_dir;
822d88c93f0SGreg Kroah-Hartman 	if (IS_ERR_OR_NULL(old_dentry))
823d88c93f0SGreg Kroah-Hartman 		return old_dentry;
824d88c93f0SGreg Kroah-Hartman 
825cfc94cdfSJan Kara 	trap = lock_rename(new_dir, old_dir);
826cfc94cdfSJan Kara 	/* Source or destination directories don't exist? */
8272b0143b5SDavid Howells 	if (d_really_is_negative(old_dir) || d_really_is_negative(new_dir))
828cfc94cdfSJan Kara 		goto exit;
829cfc94cdfSJan Kara 	/* Source does not exist, cyclic rename, or mountpoint? */
8302b0143b5SDavid Howells 	if (d_really_is_negative(old_dentry) || old_dentry == trap ||
831cfc94cdfSJan Kara 	    d_mountpoint(old_dentry))
832cfc94cdfSJan Kara 		goto exit;
833cfc94cdfSJan Kara 	dentry = lookup_one_len(new_name, new_dir, strlen(new_name));
834cfc94cdfSJan Kara 	/* Lookup failed, cyclic rename or target exists? */
8352b0143b5SDavid Howells 	if (IS_ERR(dentry) || dentry == trap || d_really_is_positive(dentry))
836cfc94cdfSJan Kara 		goto exit;
837cfc94cdfSJan Kara 
83849d31c2fSAl Viro 	take_dentry_name_snapshot(&old_name, old_dentry);
839cfc94cdfSJan Kara 
840549c7297SChristian Brauner 	error = simple_rename(&init_user_ns, d_inode(old_dir), old_dentry,
841549c7297SChristian Brauner 			      d_inode(new_dir), dentry, 0);
842cfc94cdfSJan Kara 	if (error) {
84349d31c2fSAl Viro 		release_dentry_name_snapshot(&old_name);
844cfc94cdfSJan Kara 		goto exit;
845cfc94cdfSJan Kara 	}
846cfc94cdfSJan Kara 	d_move(old_dentry, dentry);
847f4ec3a3dSAl Viro 	fsnotify_move(d_inode(old_dir), d_inode(new_dir), &old_name.name,
848e36cb0b8SDavid Howells 		d_is_dir(old_dentry),
8495a190ae6SAl Viro 		NULL, old_dentry);
85049d31c2fSAl Viro 	release_dentry_name_snapshot(&old_name);
851cfc94cdfSJan Kara 	unlock_rename(new_dir, old_dir);
852cfc94cdfSJan Kara 	dput(dentry);
853cfc94cdfSJan Kara 	return old_dentry;
854cfc94cdfSJan Kara exit:
855cfc94cdfSJan Kara 	if (dentry && !IS_ERR(dentry))
856cfc94cdfSJan Kara 		dput(dentry);
857cfc94cdfSJan Kara 	unlock_rename(new_dir, old_dir);
858ff9fb72bSGreg Kroah-Hartman 	if (IS_ERR(dentry))
859ff9fb72bSGreg Kroah-Hartman 		return dentry;
860ff9fb72bSGreg Kroah-Hartman 	return ERR_PTR(-EINVAL);
861cfc94cdfSJan Kara }
862cfc94cdfSJan Kara EXPORT_SYMBOL_GPL(debugfs_rename);
863cfc94cdfSJan Kara 
864c0f92ba9SFrederic Weisbecker /**
865c0f92ba9SFrederic Weisbecker  * debugfs_initialized - Tells whether debugfs has been registered
866c0f92ba9SFrederic Weisbecker  */
867c0f92ba9SFrederic Weisbecker bool debugfs_initialized(void)
868c0f92ba9SFrederic Weisbecker {
869c0f92ba9SFrederic Weisbecker 	return debugfs_registered;
870c0f92ba9SFrederic Weisbecker }
871c0f92ba9SFrederic Weisbecker EXPORT_SYMBOL_GPL(debugfs_initialized);
872c0f92ba9SFrederic Weisbecker 
873a24c6f7bSPeter Enderborg static int __init debugfs_kernel(char *str)
874a24c6f7bSPeter Enderborg {
875a24c6f7bSPeter Enderborg 	if (str) {
876a24c6f7bSPeter Enderborg 		if (!strcmp(str, "on"))
877a24c6f7bSPeter Enderborg 			debugfs_allow = DEBUGFS_ALLOW_API | DEBUGFS_ALLOW_MOUNT;
878a24c6f7bSPeter Enderborg 		else if (!strcmp(str, "no-mount"))
879a24c6f7bSPeter Enderborg 			debugfs_allow = DEBUGFS_ALLOW_API;
880a24c6f7bSPeter Enderborg 		else if (!strcmp(str, "off"))
881a24c6f7bSPeter Enderborg 			debugfs_allow = 0;
882a24c6f7bSPeter Enderborg 	}
883a24c6f7bSPeter Enderborg 
884a24c6f7bSPeter Enderborg 	return 0;
885a24c6f7bSPeter Enderborg }
886a24c6f7bSPeter Enderborg early_param("debugfs", debugfs_kernel);
8871da177e4SLinus Torvalds static int __init debugfs_init(void)
8881da177e4SLinus Torvalds {
8891da177e4SLinus Torvalds 	int retval;
8901da177e4SLinus Torvalds 
891a24c6f7bSPeter Enderborg 	if (!(debugfs_allow & DEBUGFS_ALLOW_MOUNT))
892a24c6f7bSPeter Enderborg 		return -EPERM;
893a24c6f7bSPeter Enderborg 
894f9bb4882SEric W. Biederman 	retval = sysfs_create_mount_point(kernel_kobj, "debug");
895f9bb4882SEric W. Biederman 	if (retval)
896f9bb4882SEric W. Biederman 		return retval;
8971da177e4SLinus Torvalds 
8981da177e4SLinus Torvalds 	retval = register_filesystem(&debug_fs_type);
8991da177e4SLinus Torvalds 	if (retval)
900f9bb4882SEric W. Biederman 		sysfs_remove_mount_point(kernel_kobj, "debug");
901c0f92ba9SFrederic Weisbecker 	else
902c0f92ba9SFrederic Weisbecker 		debugfs_registered = true;
903c0f92ba9SFrederic Weisbecker 
9041da177e4SLinus Torvalds 	return retval;
9051da177e4SLinus Torvalds }
9061da177e4SLinus Torvalds core_initcall(debugfs_init);
907