xref: /linux-6.15/fs/debugfs/inode.c (revision f7862dfe)
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>
17a20971c1SDavid Howells #include <linux/fs_context.h>
18a20971c1SDavid Howells #include <linux/fs_parser.h>
191da177e4SLinus Torvalds #include <linux/pagemap.h>
201da177e4SLinus Torvalds #include <linux/init.h>
214d8ebddcSRandy Dunlap #include <linux/kobject.h>
221da177e4SLinus Torvalds #include <linux/namei.h>
231da177e4SLinus Torvalds #include <linux/debugfs.h>
244f36557fSMathieu Desnoyers #include <linux/fsnotify.h>
2566f54963SPeter Oberparleiter #include <linux/string.h>
26d6e48686SLudwig Nussel #include <linux/seq_file.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  */
debugfs_setattr(struct mnt_idmap * idmap,struct dentry * dentry,struct iattr * ia)45c1632a0fSChristian 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 	}
55c1632a0fSChristian 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 
debugfs_get_inode(struct super_block * sb)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();
75d21b3c32SJeff Layton 		simple_inode_init_ts(inode);
761da177e4SLinus Torvalds 	}
771da177e4SLinus Torvalds 	return inode;
781da177e4SLinus Torvalds }
791da177e4SLinus Torvalds 
80a20971c1SDavid Howells struct debugfs_fs_info {
817dc05881SEric W. Biederman 	kuid_t uid;
827dc05881SEric W. Biederman 	kgid_t gid;
83d6e48686SLudwig Nussel 	umode_t mode;
84b8de524cSBrian Norris 	/* Opt_* bitfield. */
85b8de524cSBrian Norris 	unsigned int opts;
86d6e48686SLudwig Nussel };
87d6e48686SLudwig Nussel 
88d6e48686SLudwig Nussel enum {
89d6e48686SLudwig Nussel 	Opt_uid,
90d6e48686SLudwig Nussel 	Opt_gid,
91d6e48686SLudwig Nussel 	Opt_mode,
923a987b88SMarc Aurèle La France 	Opt_source,
93d6e48686SLudwig Nussel };
94d6e48686SLudwig Nussel 
95a20971c1SDavid Howells static const struct fs_parameter_spec debugfs_param_specs[] = {
9649abee59SEric Sandeen 	fsparam_gid	("gid",		Opt_gid),
97a20971c1SDavid Howells 	fsparam_u32oct	("mode",	Opt_mode),
9849abee59SEric Sandeen 	fsparam_uid	("uid",		Opt_uid),
993a987b88SMarc Aurèle La France 	fsparam_string	("source",	Opt_source),
100a20971c1SDavid Howells 	{}
101d6e48686SLudwig Nussel };
102d6e48686SLudwig Nussel 
debugfs_parse_param(struct fs_context * fc,struct fs_parameter * param)103a20971c1SDavid Howells static int debugfs_parse_param(struct fs_context *fc, struct fs_parameter *param)
104d6e48686SLudwig Nussel {
105a20971c1SDavid Howells 	struct debugfs_fs_info *opts = fc->s_fs_info;
106a20971c1SDavid Howells 	struct fs_parse_result result;
107a20971c1SDavid Howells 	int opt;
108d6e48686SLudwig Nussel 
109a20971c1SDavid Howells 	opt = fs_parse(fc, debugfs_param_specs, param, &result);
1100c07c273SChristian Brauner 	if (opt < 0) {
1110c07c273SChristian Brauner 		/*
1120c07c273SChristian Brauner                 * We might like to report bad mount options here; but
1130c07c273SChristian Brauner                 * traditionally debugfs has ignored all mount options
1140c07c273SChristian Brauner                 */
1150c07c273SChristian Brauner 		if (opt == -ENOPARAM)
1160c07c273SChristian Brauner 			return 0;
1170c07c273SChristian Brauner 
118a20971c1SDavid Howells 		return opt;
1190c07c273SChristian Brauner 	}
120d6e48686SLudwig Nussel 
121a20971c1SDavid Howells 	switch (opt) {
122d6e48686SLudwig Nussel 	case Opt_uid:
12349abee59SEric Sandeen 		opts->uid = result.uid;
124d6e48686SLudwig Nussel 		break;
125d6e48686SLudwig Nussel 	case Opt_gid:
12649abee59SEric Sandeen 		opts->gid = result.gid;
127d6e48686SLudwig Nussel 		break;
128d6e48686SLudwig Nussel 	case Opt_mode:
129a20971c1SDavid Howells 		opts->mode = result.uint_32 & S_IALLUGO;
130d6e48686SLudwig Nussel 		break;
1313a987b88SMarc Aurèle La France 	case Opt_source:
1323a987b88SMarc Aurèle La France 		if (fc->source)
1333a987b88SMarc Aurèle La France 			return invalfc(fc, "Multiple sources specified");
1343a987b88SMarc Aurèle La France 		fc->source = param->string;
1353a987b88SMarc Aurèle La France 		param->string = NULL;
1363a987b88SMarc Aurèle La France 		break;
137d6e48686SLudwig Nussel 	/*
138d6e48686SLudwig Nussel 	 * We might like to report bad mount options here;
139d6e48686SLudwig Nussel 	 * but traditionally debugfs has ignored all mount options
140d6e48686SLudwig Nussel 	 */
141d6e48686SLudwig Nussel 	}
142b8de524cSBrian Norris 
143a20971c1SDavid Howells 	opts->opts |= BIT(opt);
144d6e48686SLudwig Nussel 
145d6e48686SLudwig Nussel 	return 0;
146d6e48686SLudwig Nussel }
147d6e48686SLudwig Nussel 
_debugfs_apply_options(struct super_block * sb,bool remount)148b8de524cSBrian Norris static void _debugfs_apply_options(struct super_block *sb, bool remount)
149d6e48686SLudwig Nussel {
150d6e48686SLudwig Nussel 	struct debugfs_fs_info *fsi = sb->s_fs_info;
1512b0143b5SDavid Howells 	struct inode *inode = d_inode(sb->s_root);
152d6e48686SLudwig Nussel 
153b8de524cSBrian Norris 	/*
154b8de524cSBrian Norris 	 * On remount, only reset mode/uid/gid if they were provided as mount
155b8de524cSBrian Norris 	 * options.
156b8de524cSBrian Norris 	 */
157b8de524cSBrian Norris 
158a20971c1SDavid Howells 	if (!remount || fsi->opts & BIT(Opt_mode)) {
159d6e48686SLudwig Nussel 		inode->i_mode &= ~S_IALLUGO;
160a20971c1SDavid Howells 		inode->i_mode |= fsi->mode;
161b8de524cSBrian Norris 	}
162d6e48686SLudwig Nussel 
163a20971c1SDavid Howells 	if (!remount || fsi->opts & BIT(Opt_uid))
164a20971c1SDavid Howells 		inode->i_uid = fsi->uid;
165d6e48686SLudwig Nussel 
166a20971c1SDavid Howells 	if (!remount || fsi->opts & BIT(Opt_gid))
167a20971c1SDavid Howells 		inode->i_gid = fsi->gid;
168b8de524cSBrian Norris }
169b8de524cSBrian Norris 
debugfs_apply_options(struct super_block * sb)170b8de524cSBrian Norris static void debugfs_apply_options(struct super_block *sb)
171b8de524cSBrian Norris {
172b8de524cSBrian Norris 	_debugfs_apply_options(sb, false);
173b8de524cSBrian Norris }
174b8de524cSBrian Norris 
debugfs_apply_options_remount(struct super_block * sb)175b8de524cSBrian Norris static void debugfs_apply_options_remount(struct super_block *sb)
176b8de524cSBrian Norris {
177b8de524cSBrian Norris 	_debugfs_apply_options(sb, true);
178d6e48686SLudwig Nussel }
179d6e48686SLudwig Nussel 
debugfs_reconfigure(struct fs_context * fc)180a20971c1SDavid Howells static int debugfs_reconfigure(struct fs_context *fc)
181d6e48686SLudwig Nussel {
182a20971c1SDavid Howells 	struct super_block *sb = fc->root->d_sb;
183a20971c1SDavid Howells 	struct debugfs_fs_info *sb_opts = sb->s_fs_info;
184a20971c1SDavid Howells 	struct debugfs_fs_info *new_opts = fc->s_fs_info;
185d6e48686SLudwig Nussel 
18602b9984dSTheodore Ts'o 	sync_filesystem(sb);
187d6e48686SLudwig Nussel 
188a20971c1SDavid Howells 	/* structure copy of new mount options to sb */
189a20971c1SDavid Howells 	*sb_opts = *new_opts;
190b8de524cSBrian Norris 	debugfs_apply_options_remount(sb);
191d6e48686SLudwig Nussel 
192a20971c1SDavid Howells 	return 0;
193d6e48686SLudwig Nussel }
194d6e48686SLudwig Nussel 
debugfs_show_options(struct seq_file * m,struct dentry * root)195d6e48686SLudwig Nussel static int debugfs_show_options(struct seq_file *m, struct dentry *root)
196d6e48686SLudwig Nussel {
197d6e48686SLudwig Nussel 	struct debugfs_fs_info *fsi = root->d_sb->s_fs_info;
198d6e48686SLudwig Nussel 
199a20971c1SDavid Howells 	if (!uid_eq(fsi->uid, GLOBAL_ROOT_UID))
2007dc05881SEric W. Biederman 		seq_printf(m, ",uid=%u",
201a20971c1SDavid Howells 			   from_kuid_munged(&init_user_ns, fsi->uid));
202a20971c1SDavid Howells 	if (!gid_eq(fsi->gid, GLOBAL_ROOT_GID))
2037dc05881SEric W. Biederman 		seq_printf(m, ",gid=%u",
204a20971c1SDavid Howells 			   from_kgid_munged(&init_user_ns, fsi->gid));
205a20971c1SDavid Howells 	if (fsi->mode != DEBUGFS_DEFAULT_MODE)
206a20971c1SDavid Howells 		seq_printf(m, ",mode=%o", fsi->mode);
207d6e48686SLudwig Nussel 
208d6e48686SLudwig Nussel 	return 0;
209d6e48686SLudwig Nussel }
210d6e48686SLudwig Nussel 
211268b3611SAl Viro static struct kmem_cache *debugfs_inode_cachep __ro_after_init;
212268b3611SAl Viro 
init_once(void * foo)213268b3611SAl Viro static void init_once(void *foo)
214268b3611SAl Viro {
215268b3611SAl Viro 	struct debugfs_inode_info *info = foo;
216268b3611SAl Viro 	inode_init_once(&info->vfs_inode);
217268b3611SAl Viro }
218268b3611SAl Viro 
debugfs_alloc_inode(struct super_block * sb)219268b3611SAl Viro static struct inode *debugfs_alloc_inode(struct super_block *sb)
220268b3611SAl Viro {
221268b3611SAl Viro 	struct debugfs_inode_info *info;
222268b3611SAl Viro 	info = alloc_inode_sb(sb, debugfs_inode_cachep, GFP_KERNEL);
223268b3611SAl Viro 	if (!info)
224268b3611SAl Viro 		return NULL;
225268b3611SAl Viro 	return &info->vfs_inode;
226268b3611SAl Viro }
227268b3611SAl Viro 
debugfs_free_inode(struct inode * inode)2286234ddf4SAl Viro static void debugfs_free_inode(struct inode *inode)
2290db59e59SAl Viro {
2300db59e59SAl Viro 	if (S_ISLNK(inode->i_mode))
2315723cb01SAl Viro 		kfree(inode->i_link);
232268b3611SAl Viro 	kmem_cache_free(debugfs_inode_cachep, DEBUGFS_I(inode));
23393b919daSAl Viro }
23493b919daSAl Viro 
235d6e48686SLudwig Nussel static const struct super_operations debugfs_super_operations = {
236d6e48686SLudwig Nussel 	.statfs		= simple_statfs,
237d6e48686SLudwig Nussel 	.show_options	= debugfs_show_options,
238268b3611SAl Viro 	.alloc_inode	= debugfs_alloc_inode,
2396234ddf4SAl Viro 	.free_inode	= debugfs_free_inode,
240d6e48686SLudwig Nussel };
241d6e48686SLudwig Nussel 
debugfs_release_dentry(struct dentry * dentry)2427c8d4698SNicolai Stange static void debugfs_release_dentry(struct dentry *dentry)
2437c8d4698SNicolai Stange {
2440ed04a18SJohannes Berg 	struct debugfs_fsdata *fsd = dentry->d_fsdata;
2457d39bc50SNicolai Stange 
246bacaaf83SAl Viro 	if (fsd) {
2478c88a474SJohannes Berg 		WARN_ON(!list_empty(&fsd->cancellations));
2488c88a474SJohannes Berg 		mutex_destroy(&fsd->cancellations_mtx);
249f4acfcd4SJohannes Berg 	}
2500ed04a18SJohannes Berg 	kfree(fsd);
2517c8d4698SNicolai Stange }
2527c8d4698SNicolai Stange 
debugfs_automount(struct path * path)25377b3da6eSAl Viro static struct vfsmount *debugfs_automount(struct path *path)
25477b3da6eSAl Viro {
255bacaaf83SAl Viro 	struct inode *inode = path->dentry->d_inode;
2560ed04a18SJohannes Berg 
257bacaaf83SAl Viro 	return DEBUGFS_I(inode)->automount(path->dentry, inode->i_private);
25877b3da6eSAl Viro }
25977b3da6eSAl Viro 
26077b3da6eSAl Viro static const struct dentry_operations debugfs_dops = {
26177b3da6eSAl Viro 	.d_delete = always_delete_dentry,
2627c8d4698SNicolai Stange 	.d_release = debugfs_release_dentry,
26377b3da6eSAl Viro 	.d_automount = debugfs_automount,
26477b3da6eSAl Viro };
26577b3da6eSAl Viro 
debugfs_fill_super(struct super_block * sb,struct fs_context * fc)266a20971c1SDavid Howells static int debugfs_fill_super(struct super_block *sb, struct fs_context *fc)
2671da177e4SLinus Torvalds {
268cda37124SEric Biggers 	static const struct tree_descr debug_files[] = {{""}};
269d6e48686SLudwig Nussel 	int err;
2701da177e4SLinus Torvalds 
271d6e48686SLudwig Nussel 	err = simple_fill_super(sb, DEBUGFS_MAGIC, debug_files);
272d6e48686SLudwig Nussel 	if (err)
273a20971c1SDavid Howells 		return err;
274d6e48686SLudwig Nussel 
275d6e48686SLudwig Nussel 	sb->s_op = &debugfs_super_operations;
27677b3da6eSAl Viro 	sb->s_d_op = &debugfs_dops;
277d6e48686SLudwig Nussel 
278d6e48686SLudwig Nussel 	debugfs_apply_options(sb);
279d6e48686SLudwig Nussel 
280d6e48686SLudwig Nussel 	return 0;
2811da177e4SLinus Torvalds }
2821da177e4SLinus Torvalds 
debugfs_get_tree(struct fs_context * fc)283a20971c1SDavid Howells static int debugfs_get_tree(struct fs_context *fc)
2841da177e4SLinus Torvalds {
285a24c6f7bSPeter Enderborg 	if (!(debugfs_allow & DEBUGFS_ALLOW_API))
286a20971c1SDavid Howells 		return -EPERM;
287a24c6f7bSPeter Enderborg 
288a20971c1SDavid Howells 	return get_tree_single(fc, debugfs_fill_super);
289a20971c1SDavid Howells }
290a20971c1SDavid Howells 
debugfs_free_fc(struct fs_context * fc)291a20971c1SDavid Howells static void debugfs_free_fc(struct fs_context *fc)
292a20971c1SDavid Howells {
293a20971c1SDavid Howells 	kfree(fc->s_fs_info);
294a20971c1SDavid Howells }
295a20971c1SDavid Howells 
296a20971c1SDavid Howells static const struct fs_context_operations debugfs_context_ops = {
297a20971c1SDavid Howells 	.free		= debugfs_free_fc,
298a20971c1SDavid Howells 	.parse_param	= debugfs_parse_param,
299a20971c1SDavid Howells 	.get_tree	= debugfs_get_tree,
300a20971c1SDavid Howells 	.reconfigure	= debugfs_reconfigure,
301a20971c1SDavid Howells };
302a20971c1SDavid Howells 
debugfs_init_fs_context(struct fs_context * fc)303a20971c1SDavid Howells static int debugfs_init_fs_context(struct fs_context *fc)
304a20971c1SDavid Howells {
305a20971c1SDavid Howells 	struct debugfs_fs_info *fsi;
306a20971c1SDavid Howells 
307a20971c1SDavid Howells 	fsi = kzalloc(sizeof(struct debugfs_fs_info), GFP_KERNEL);
308a20971c1SDavid Howells 	if (!fsi)
309a20971c1SDavid Howells 		return -ENOMEM;
310a20971c1SDavid Howells 
311a20971c1SDavid Howells 	fsi->mode = DEBUGFS_DEFAULT_MODE;
312a20971c1SDavid Howells 
313a20971c1SDavid Howells 	fc->s_fs_info = fsi;
314a20971c1SDavid Howells 	fc->ops = &debugfs_context_ops;
315a20971c1SDavid Howells 	return 0;
3161da177e4SLinus Torvalds }
3171da177e4SLinus Torvalds 
3181da177e4SLinus Torvalds static struct file_system_type debug_fs_type = {
3191da177e4SLinus Torvalds 	.owner =	THIS_MODULE,
3201da177e4SLinus Torvalds 	.name =		"debugfs",
321a20971c1SDavid Howells 	.init_fs_context = debugfs_init_fs_context,
322a20971c1SDavid Howells 	.parameters =	debugfs_param_specs,
3231da177e4SLinus Torvalds 	.kill_sb =	kill_litter_super,
3241da177e4SLinus Torvalds };
3257f78e035SEric W. Biederman MODULE_ALIAS_FS("debugfs");
3261da177e4SLinus Torvalds 
327a7c5437bSOmar Sandoval /**
328a7c5437bSOmar Sandoval  * debugfs_lookup() - look up an existing debugfs file
329a7c5437bSOmar Sandoval  * @name: a pointer to a string containing the name of the file to look up.
330a7c5437bSOmar Sandoval  * @parent: a pointer to the parent dentry of the file.
331a7c5437bSOmar Sandoval  *
332a7c5437bSOmar Sandoval  * This function will return a pointer to a dentry if it succeeds.  If the file
333a7c5437bSOmar Sandoval  * doesn't exist or an error occurs, %NULL will be returned.  The returned
334a7c5437bSOmar Sandoval  * dentry must be passed to dput() when it is no longer needed.
335a7c5437bSOmar Sandoval  *
336a7c5437bSOmar Sandoval  * If debugfs is not enabled in the kernel, the value -%ENODEV will be
337a7c5437bSOmar Sandoval  * returned.
338a7c5437bSOmar Sandoval  */
debugfs_lookup(const char * name,struct dentry * parent)339a7c5437bSOmar Sandoval struct dentry *debugfs_lookup(const char *name, struct dentry *parent)
340a7c5437bSOmar Sandoval {
341a7c5437bSOmar Sandoval 	struct dentry *dentry;
342a7c5437bSOmar Sandoval 
343bc6de804SGreg Kroah-Hartman 	if (!debugfs_initialized() || IS_ERR_OR_NULL(name) || IS_ERR(parent))
344a7c5437bSOmar Sandoval 		return NULL;
345a7c5437bSOmar Sandoval 
346a7c5437bSOmar Sandoval 	if (!parent)
347a7c5437bSOmar Sandoval 		parent = debugfs_mount->mnt_root;
348a7c5437bSOmar Sandoval 
3496c2d4798SAl Viro 	dentry = lookup_positive_unlocked(name, parent, strlen(name));
350a7c5437bSOmar Sandoval 	if (IS_ERR(dentry))
351a7c5437bSOmar Sandoval 		return NULL;
352a7c5437bSOmar Sandoval 	return dentry;
353a7c5437bSOmar Sandoval }
354a7c5437bSOmar Sandoval EXPORT_SYMBOL_GPL(debugfs_lookup);
355a7c5437bSOmar Sandoval 
start_creating(const char * name,struct dentry * parent)356190afd81SAl Viro static struct dentry *start_creating(const char *name, struct dentry *parent)
357c3b1a350SAl Viro {
358190afd81SAl Viro 	struct dentry *dentry;
359c3b1a350SAl Viro 	int error;
360c3b1a350SAl Viro 
361a24c6f7bSPeter Enderborg 	if (!(debugfs_allow & DEBUGFS_ALLOW_API))
362a24c6f7bSPeter Enderborg 		return ERR_PTR(-EPERM);
363a24c6f7bSPeter Enderborg 
36456348560SGreg Kroah-Hartman 	if (!debugfs_initialized())
36556348560SGreg Kroah-Hartman 		return ERR_PTR(-ENOENT);
36656348560SGreg Kroah-Hartman 
367d03ae477SGreg Kroah-Hartman 	pr_debug("creating file '%s'\n", name);
368c3b1a350SAl Viro 
369c9e15f25SGreg KH 	if (IS_ERR(parent))
370c9e15f25SGreg KH 		return parent;
371c9e15f25SGreg KH 
372c3b1a350SAl Viro 	error = simple_pin_fs(&debug_fs_type, &debugfs_mount,
373c3b1a350SAl Viro 			      &debugfs_mount_count);
37443e23b6cSGreg Kroah-Hartman 	if (error) {
37543e23b6cSGreg Kroah-Hartman 		pr_err("Unable to pin filesystem for file '%s'\n", name);
376190afd81SAl Viro 		return ERR_PTR(error);
37743e23b6cSGreg Kroah-Hartman 	}
378c3b1a350SAl Viro 
379cfa57c11SAl Viro 	/* If the parent is not specified, we create it in the root.
380cfa57c11SAl Viro 	 * We need the root dentry to do this, which is in the super
381cfa57c11SAl Viro 	 * block. A pointer to that is in the struct vfsmount that we
382cfa57c11SAl Viro 	 * have around.
383cfa57c11SAl Viro 	 */
384cfa57c11SAl Viro 	if (!parent)
385cfa57c11SAl Viro 		parent = debugfs_mount->mnt_root;
386cfa57c11SAl Viro 
3875955102cSAl Viro 	inode_lock(d_inode(parent));
388a3d1e7ebSAl Viro 	if (unlikely(IS_DEADDIR(d_inode(parent))))
389a3d1e7ebSAl Viro 		dentry = ERR_PTR(-ENOENT);
390a3d1e7ebSAl Viro 	else
391cfa57c11SAl Viro 		dentry = lookup_one_len(name, parent, strlen(name));
3922b0143b5SDavid Howells 	if (!IS_ERR(dentry) && d_really_is_positive(dentry)) {
393c33d4423SGreg Kroah-Hartman 		if (d_is_dir(dentry))
394c33d4423SGreg Kroah-Hartman 			pr_err("Directory '%s' with parent '%s' already present!\n",
395c33d4423SGreg Kroah-Hartman 			       name, parent->d_name.name);
396c33d4423SGreg Kroah-Hartman 		else
397c33d4423SGreg Kroah-Hartman 			pr_err("File '%s' in directory '%s' already present!\n",
398c33d4423SGreg Kroah-Hartman 			       name, parent->d_name.name);
399190afd81SAl Viro 		dput(dentry);
400190afd81SAl Viro 		dentry = ERR_PTR(-EEXIST);
401190afd81SAl Viro 	}
4020ee9608cSDaniel Borkmann 
4030ee9608cSDaniel Borkmann 	if (IS_ERR(dentry)) {
4045955102cSAl Viro 		inode_unlock(d_inode(parent));
4050ee9608cSDaniel Borkmann 		simple_release_fs(&debugfs_mount, &debugfs_mount_count);
4060ee9608cSDaniel Borkmann 	}
4070ee9608cSDaniel Borkmann 
408190afd81SAl Viro 	return dentry;
409190afd81SAl Viro }
410190afd81SAl Viro 
failed_creating(struct dentry * dentry)4115233e311SAl Viro static struct dentry *failed_creating(struct dentry *dentry)
412190afd81SAl Viro {
4135955102cSAl Viro 	inode_unlock(d_inode(dentry->d_parent));
414190afd81SAl Viro 	dput(dentry);
415190afd81SAl Viro 	simple_release_fs(&debugfs_mount, &debugfs_mount_count);
416ff9fb72bSGreg Kroah-Hartman 	return ERR_PTR(-ENOMEM);
417190afd81SAl Viro }
4185233e311SAl Viro 
end_creating(struct dentry * dentry)4195233e311SAl Viro static struct dentry *end_creating(struct dentry *dentry)
4205233e311SAl Viro {
4215955102cSAl Viro 	inode_unlock(d_inode(dentry->d_parent));
422190afd81SAl Viro 	return dentry;
423190afd81SAl Viro }
424190afd81SAl Viro 
__debugfs_create_file(const char * name,umode_t mode,struct dentry * parent,void * data,const void * aux,const struct file_operations * proxy_fops,const void * real_fops)42549d200deSNicolai Stange static struct dentry *__debugfs_create_file(const char *name, umode_t mode,
42649d200deSNicolai Stange 				struct dentry *parent, void *data,
42712c92098SAl Viro 				const void *aux,
42849d200deSNicolai Stange 				const struct file_operations *proxy_fops,
4298dc6d81cSJohannes Berg 				const void *real_fops)
43049d200deSNicolai Stange {
43149d200deSNicolai Stange 	struct dentry *dentry;
43249d200deSNicolai Stange 	struct inode *inode;
43349d200deSNicolai Stange 
43449d200deSNicolai Stange 	if (!(mode & S_IFMT))
43549d200deSNicolai Stange 		mode |= S_IFREG;
43649d200deSNicolai Stange 	BUG_ON(!S_ISREG(mode));
43749d200deSNicolai Stange 	dentry = start_creating(name, parent);
43849d200deSNicolai Stange 
4397d39bc50SNicolai Stange 	if (IS_ERR(dentry))
440ff9fb72bSGreg Kroah-Hartman 		return dentry;
44149d200deSNicolai Stange 
442a24c6f7bSPeter Enderborg 	if (!(debugfs_allow & DEBUGFS_ALLOW_API)) {
443a24c6f7bSPeter Enderborg 		failed_creating(dentry);
444a24c6f7bSPeter Enderborg 		return ERR_PTR(-EPERM);
445a24c6f7bSPeter Enderborg 	}
446a24c6f7bSPeter Enderborg 
44749d200deSNicolai Stange 	inode = debugfs_get_inode(dentry->d_sb);
44843e23b6cSGreg Kroah-Hartman 	if (unlikely(!inode)) {
44943e23b6cSGreg Kroah-Hartman 		pr_err("out of free dentries, can not create file '%s'\n",
45043e23b6cSGreg Kroah-Hartman 		       name);
45149d200deSNicolai Stange 		return failed_creating(dentry);
45243e23b6cSGreg Kroah-Hartman 	}
45349d200deSNicolai Stange 
45449d200deSNicolai Stange 	inode->i_mode = mode;
45549d200deSNicolai Stange 	inode->i_private = data;
45649d200deSNicolai Stange 
4575496197fSDavid Howells 	inode->i_op = &debugfs_file_inode_operations;
45895688800SAl Viro 	if (!real_fops)
45995688800SAl Viro 		proxy_fops = &debugfs_noop_file_operations;
46049d200deSNicolai Stange 	inode->i_fop = proxy_fops;
46195688800SAl Viro 	DEBUGFS_I(inode)->raw = real_fops;
46212c92098SAl Viro 	DEBUGFS_I(inode)->aux = aux;
46349d200deSNicolai Stange 
46449d200deSNicolai Stange 	d_instantiate(dentry, inode);
46549d200deSNicolai Stange 	fsnotify_create(d_inode(dentry->d_parent), dentry);
46649d200deSNicolai Stange 	return end_creating(dentry);
46749d200deSNicolai Stange }
46849d200deSNicolai Stange 
debugfs_create_file_full(const char * name,umode_t mode,struct dentry * parent,void * data,const void * aux,const struct file_operations * fops)4698dc6d81cSJohannes Berg struct dentry *debugfs_create_file_full(const char *name, umode_t mode,
4701da177e4SLinus Torvalds 					struct dentry *parent, void *data,
47112c92098SAl Viro 					const void *aux,
47299ac48f5SArjan van de Ven 					const struct file_operations *fops)
4731da177e4SLinus Torvalds {
47412c92098SAl Viro 	return __debugfs_create_file(name, mode, parent, data, aux,
47595688800SAl Viro 				&debugfs_full_proxy_file_operations,
47649d200deSNicolai Stange 				fops);
4771da177e4SLinus Torvalds }
4788dc6d81cSJohannes Berg EXPORT_SYMBOL_GPL(debugfs_create_file_full);
4798dc6d81cSJohannes Berg 
debugfs_create_file_short(const char * name,umode_t mode,struct dentry * parent,void * data,const void * aux,const struct debugfs_short_fops * fops)4808dc6d81cSJohannes Berg struct dentry *debugfs_create_file_short(const char *name, umode_t mode,
4818dc6d81cSJohannes Berg 					struct dentry *parent, void *data,
48212c92098SAl Viro 					const void *aux,
4838dc6d81cSJohannes Berg 					const struct debugfs_short_fops *fops)
4848dc6d81cSJohannes Berg {
48512c92098SAl Viro 	return __debugfs_create_file(name, mode, parent, data, aux,
48695688800SAl Viro 				&debugfs_full_short_proxy_file_operations,
487f8f25893SJohannes Berg 				fops);
4888dc6d81cSJohannes Berg }
4898dc6d81cSJohannes Berg EXPORT_SYMBOL_GPL(debugfs_create_file_short);
4901da177e4SLinus Torvalds 
491c6468808SNicolai Stange /**
492c6468808SNicolai Stange  * debugfs_create_file_unsafe - create a file in the debugfs filesystem
493c6468808SNicolai Stange  * @name: a pointer to a string containing the name of the file to create.
494c6468808SNicolai Stange  * @mode: the permission that the file should have.
495c6468808SNicolai Stange  * @parent: a pointer to the parent dentry for this file.  This should be a
496c6468808SNicolai Stange  *          directory dentry if set.  If this parameter is NULL, then the
497c6468808SNicolai Stange  *          file will be created in the root of the debugfs filesystem.
498c6468808SNicolai Stange  * @data: a pointer to something that the caller will want to get to later
499c6468808SNicolai Stange  *        on.  The inode.i_private pointer will point to this value on
500c6468808SNicolai Stange  *        the open() call.
501c6468808SNicolai Stange  * @fops: a pointer to a struct file_operations that should be used for
502c6468808SNicolai Stange  *        this file.
503c6468808SNicolai Stange  *
504c6468808SNicolai Stange  * debugfs_create_file_unsafe() is completely analogous to
505c6468808SNicolai Stange  * debugfs_create_file(), the only difference being that the fops
506c6468808SNicolai Stange  * handed it will not get protected against file removals by the
507c6468808SNicolai Stange  * debugfs core.
508c6468808SNicolai Stange  *
509c6468808SNicolai Stange  * It is your responsibility to protect your struct file_operation
5100eeb2731SSergey Senozhatsky  * methods against file removals by means of debugfs_file_get()
5110eeb2731SSergey Senozhatsky  * and debugfs_file_put(). ->open() is still protected by
512c6468808SNicolai Stange  * debugfs though.
513c6468808SNicolai Stange  *
514c6468808SNicolai Stange  * Any struct file_operations defined by means of
515c6468808SNicolai Stange  * DEFINE_DEBUGFS_ATTRIBUTE() is protected against file removals and
516c6468808SNicolai Stange  * thus, may be used here.
517c6468808SNicolai Stange  */
debugfs_create_file_unsafe(const char * name,umode_t mode,struct dentry * parent,void * data,const struct file_operations * fops)51849d200deSNicolai Stange struct dentry *debugfs_create_file_unsafe(const char *name, umode_t mode,
51949d200deSNicolai Stange 				   struct dentry *parent, void *data,
52049d200deSNicolai Stange 				   const struct file_operations *fops)
52149d200deSNicolai Stange {
52249d200deSNicolai Stange 
52312c92098SAl Viro 	return __debugfs_create_file(name, mode, parent, data, NULL,
52495688800SAl Viro 				&debugfs_open_proxy_file_operations,
52549d200deSNicolai Stange 				fops);
52649d200deSNicolai Stange }
527c6468808SNicolai Stange EXPORT_SYMBOL_GPL(debugfs_create_file_unsafe);
52849d200deSNicolai Stange 
5291da177e4SLinus Torvalds /**
530e59b4e91SDavid Howells  * debugfs_create_file_size - create a file in the debugfs filesystem
531e59b4e91SDavid Howells  * @name: a pointer to a string containing the name of the file to create.
532e59b4e91SDavid Howells  * @mode: the permission that the file should have.
533e59b4e91SDavid Howells  * @parent: a pointer to the parent dentry for this file.  This should be a
534e59b4e91SDavid Howells  *          directory dentry if set.  If this parameter is NULL, then the
535e59b4e91SDavid Howells  *          file will be created in the root of the debugfs filesystem.
536e59b4e91SDavid Howells  * @data: a pointer to something that the caller will want to get to later
537e59b4e91SDavid Howells  *        on.  The inode.i_private pointer will point to this value on
538e59b4e91SDavid Howells  *        the open() call.
539e59b4e91SDavid Howells  * @fops: a pointer to a struct file_operations that should be used for
540e59b4e91SDavid Howells  *        this file.
541e59b4e91SDavid Howells  * @file_size: initial file size
542e59b4e91SDavid Howells  *
543e59b4e91SDavid Howells  * This is the basic "create a file" function for debugfs.  It allows for a
544e59b4e91SDavid Howells  * wide range of flexibility in creating a file, or a directory (if you want
545e59b4e91SDavid Howells  * to create a directory, the debugfs_create_dir() function is
546e59b4e91SDavid Howells  * recommended to be used instead.)
547e59b4e91SDavid Howells  */
debugfs_create_file_size(const char * name,umode_t mode,struct dentry * parent,void * data,const struct file_operations * fops,loff_t file_size)548526ee72dSGreg Kroah-Hartman void debugfs_create_file_size(const char *name, umode_t mode,
549e59b4e91SDavid Howells 			      struct dentry *parent, void *data,
550e59b4e91SDavid Howells 			      const struct file_operations *fops,
551e59b4e91SDavid Howells 			      loff_t file_size)
552e59b4e91SDavid Howells {
553e59b4e91SDavid Howells 	struct dentry *de = debugfs_create_file(name, mode, parent, data, fops);
554e59b4e91SDavid Howells 
555af505cadSNirmoy Das 	if (!IS_ERR(de))
5562b0143b5SDavid Howells 		d_inode(de)->i_size = file_size;
557e59b4e91SDavid Howells }
558e59b4e91SDavid Howells EXPORT_SYMBOL_GPL(debugfs_create_file_size);
559e59b4e91SDavid Howells 
560e59b4e91SDavid Howells /**
5611da177e4SLinus Torvalds  * debugfs_create_dir - create a directory in the debugfs filesystem
5621da177e4SLinus Torvalds  * @name: a pointer to a string containing the name of the directory to
5631da177e4SLinus Torvalds  *        create.
5641da177e4SLinus Torvalds  * @parent: a pointer to the parent dentry for this file.  This should be a
565e227867fSMasanari Iida  *          directory dentry if set.  If this parameter is NULL, then the
5661da177e4SLinus Torvalds  *          directory will be created in the root of the debugfs filesystem.
5671da177e4SLinus Torvalds  *
5681da177e4SLinus Torvalds  * This function creates a directory in debugfs with the given name.
5691da177e4SLinus Torvalds  *
5701da177e4SLinus Torvalds  * This function will return a pointer to a dentry if it succeeds.  This
5711da177e4SLinus Torvalds  * pointer must be passed to the debugfs_remove() function when the file is
5721da177e4SLinus Torvalds  * to be removed (no automatic cleanup happens if your module is unloaded,
573adc92dd4SDaniel W. S. Almeida  * you are responsible here.)  If an error occurs, ERR_PTR(-ERROR) will be
574ff9fb72bSGreg Kroah-Hartman  * returned.
5751da177e4SLinus Torvalds  *
5766468b3afSRandy Dunlap  * If debugfs is not enabled in the kernel, the value -%ENODEV will be
577873760fbSCornelia Huck  * returned.
5788bcbde2bSDouglas Anderson  *
5798bcbde2bSDouglas Anderson  * NOTE: it's expected that most callers should _ignore_ the errors returned
5808bcbde2bSDouglas Anderson  * by this function. Other debugfs functions handle the fact that the "dentry"
5818bcbde2bSDouglas Anderson  * passed to them could be an error and they don't crash in that case.
5828bcbde2bSDouglas Anderson  * Drivers should generally work fine even if debugfs fails to init anyway.
5831da177e4SLinus Torvalds  */
debugfs_create_dir(const char * name,struct dentry * parent)5841da177e4SLinus Torvalds struct dentry *debugfs_create_dir(const char *name, struct dentry *parent)
5851da177e4SLinus Torvalds {
586ad5abd5bSAl Viro 	struct dentry *dentry = start_creating(name, parent);
587680b3024SAl Viro 	struct inode *inode;
588ad5abd5bSAl Viro 
589ad5abd5bSAl Viro 	if (IS_ERR(dentry))
590ff9fb72bSGreg Kroah-Hartman 		return dentry;
591ad5abd5bSAl Viro 
592a24c6f7bSPeter Enderborg 	if (!(debugfs_allow & DEBUGFS_ALLOW_API)) {
593a24c6f7bSPeter Enderborg 		failed_creating(dentry);
594a24c6f7bSPeter Enderborg 		return ERR_PTR(-EPERM);
595a24c6f7bSPeter Enderborg 	}
596a24c6f7bSPeter Enderborg 
597edac65eaSAl Viro 	inode = debugfs_get_inode(dentry->d_sb);
59843e23b6cSGreg Kroah-Hartman 	if (unlikely(!inode)) {
59943e23b6cSGreg Kroah-Hartman 		pr_err("out of free dentries, can not create directory '%s'\n",
60043e23b6cSGreg Kroah-Hartman 		       name);
6015233e311SAl Viro 		return failed_creating(dentry);
60243e23b6cSGreg Kroah-Hartman 	}
603680b3024SAl Viro 
604f5b7769eSLinus Torvalds 	inode->i_mode = S_IFDIR | S_IRWXU | S_IRUGO | S_IXUGO;
6055496197fSDavid Howells 	inode->i_op = &debugfs_dir_inode_operations;
606edac65eaSAl Viro 	inode->i_fop = &simple_dir_operations;
607edac65eaSAl Viro 
608edac65eaSAl Viro 	/* directory inodes start off with i_nlink == 2 (for "." entry) */
609edac65eaSAl Viro 	inc_nlink(inode);
610680b3024SAl Viro 	d_instantiate(dentry, inode);
6112b0143b5SDavid Howells 	inc_nlink(d_inode(dentry->d_parent));
6122b0143b5SDavid Howells 	fsnotify_mkdir(d_inode(dentry->d_parent), dentry);
6135233e311SAl Viro 	return end_creating(dentry);
6141da177e4SLinus Torvalds }
6151da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(debugfs_create_dir);
6161da177e4SLinus Torvalds 
6171da177e4SLinus Torvalds /**
61877b3da6eSAl Viro  * debugfs_create_automount - create automount point in the debugfs filesystem
61977b3da6eSAl Viro  * @name: a pointer to a string containing the name of the file to create.
62077b3da6eSAl Viro  * @parent: a pointer to the parent dentry for this file.  This should be a
62177b3da6eSAl Viro  *          directory dentry if set.  If this parameter is NULL, then the
62277b3da6eSAl Viro  *          file will be created in the root of the debugfs filesystem.
62377b3da6eSAl Viro  * @f: function to be called when pathname resolution steps on that one.
62477b3da6eSAl Viro  * @data: opaque argument to pass to f().
62577b3da6eSAl Viro  *
62677b3da6eSAl Viro  * @f should return what ->d_automount() would.
62777b3da6eSAl Viro  */
debugfs_create_automount(const char * name,struct dentry * parent,debugfs_automount_t f,void * data)62877b3da6eSAl Viro struct dentry *debugfs_create_automount(const char *name,
62977b3da6eSAl Viro 					struct dentry *parent,
63093faccbbSEric W. Biederman 					debugfs_automount_t f,
63177b3da6eSAl Viro 					void *data)
63277b3da6eSAl Viro {
63377b3da6eSAl Viro 	struct dentry *dentry = start_creating(name, parent);
63477b3da6eSAl Viro 	struct inode *inode;
63577b3da6eSAl Viro 
63677b3da6eSAl Viro 	if (IS_ERR(dentry))
637ff9fb72bSGreg Kroah-Hartman 		return dentry;
63877b3da6eSAl Viro 
639a24c6f7bSPeter Enderborg 	if (!(debugfs_allow & DEBUGFS_ALLOW_API)) {
640a24c6f7bSPeter Enderborg 		failed_creating(dentry);
641a24c6f7bSPeter Enderborg 		return ERR_PTR(-EPERM);
642a24c6f7bSPeter Enderborg 	}
643a24c6f7bSPeter Enderborg 
64477b3da6eSAl Viro 	inode = debugfs_get_inode(dentry->d_sb);
64543e23b6cSGreg Kroah-Hartman 	if (unlikely(!inode)) {
64643e23b6cSGreg Kroah-Hartman 		pr_err("out of free dentries, can not create automount '%s'\n",
64743e23b6cSGreg Kroah-Hartman 		       name);
64877b3da6eSAl Viro 		return failed_creating(dentry);
64943e23b6cSGreg Kroah-Hartman 	}
65077b3da6eSAl Viro 
65187243debSSeth Forshee 	make_empty_dir_inode(inode);
65277b3da6eSAl Viro 	inode->i_flags |= S_AUTOMOUNT;
65377b3da6eSAl Viro 	inode->i_private = data;
654bacaaf83SAl Viro 	DEBUGFS_I(inode)->automount = f;
655a8f324a4SRoman Pen 	/* directory inodes start off with i_nlink == 2 (for "." entry) */
656a8f324a4SRoman Pen 	inc_nlink(inode);
65777b3da6eSAl Viro 	d_instantiate(dentry, inode);
658a8f324a4SRoman Pen 	inc_nlink(d_inode(dentry->d_parent));
659a8f324a4SRoman Pen 	fsnotify_mkdir(d_inode(dentry->d_parent), dentry);
66077b3da6eSAl Viro 	return end_creating(dentry);
66177b3da6eSAl Viro }
66277b3da6eSAl Viro EXPORT_SYMBOL(debugfs_create_automount);
66377b3da6eSAl Viro 
66477b3da6eSAl Viro /**
66566f54963SPeter Oberparleiter  * debugfs_create_symlink- create a symbolic link in the debugfs filesystem
66666f54963SPeter Oberparleiter  * @name: a pointer to a string containing the name of the symbolic link to
66766f54963SPeter Oberparleiter  *        create.
66866f54963SPeter Oberparleiter  * @parent: a pointer to the parent dentry for this symbolic link.  This
669e227867fSMasanari Iida  *          should be a directory dentry if set.  If this parameter is NULL,
67066f54963SPeter Oberparleiter  *          then the symbolic link will be created in the root of the debugfs
67166f54963SPeter Oberparleiter  *          filesystem.
67266f54963SPeter Oberparleiter  * @target: a pointer to a string containing the path to the target of the
67366f54963SPeter Oberparleiter  *          symbolic link.
67466f54963SPeter Oberparleiter  *
67566f54963SPeter Oberparleiter  * This function creates a symbolic link with the given name in debugfs that
67666f54963SPeter Oberparleiter  * links to the given target path.
67766f54963SPeter Oberparleiter  *
67866f54963SPeter Oberparleiter  * This function will return a pointer to a dentry if it succeeds.  This
67966f54963SPeter Oberparleiter  * pointer must be passed to the debugfs_remove() function when the symbolic
68066f54963SPeter Oberparleiter  * link is to be removed (no automatic cleanup happens if your module is
681adc92dd4SDaniel W. S. Almeida  * unloaded, you are responsible here.)  If an error occurs, ERR_PTR(-ERROR)
682ff9fb72bSGreg Kroah-Hartman  * will be returned.
68366f54963SPeter Oberparleiter  *
68466f54963SPeter Oberparleiter  * If debugfs is not enabled in the kernel, the value -%ENODEV will be
685873760fbSCornelia Huck  * returned.
68666f54963SPeter Oberparleiter  */
debugfs_create_symlink(const char * name,struct dentry * parent,const char * target)68766f54963SPeter Oberparleiter struct dentry *debugfs_create_symlink(const char *name, struct dentry *parent,
68866f54963SPeter Oberparleiter 				      const char *target)
68966f54963SPeter Oberparleiter {
690ad5abd5bSAl Viro 	struct dentry *dentry;
691680b3024SAl Viro 	struct inode *inode;
692680b3024SAl Viro 	char *link = kstrdup(target, GFP_KERNEL);
69366f54963SPeter Oberparleiter 	if (!link)
694ff9fb72bSGreg Kroah-Hartman 		return ERR_PTR(-ENOMEM);
69566f54963SPeter Oberparleiter 
696ad5abd5bSAl Viro 	dentry = start_creating(name, parent);
697ad5abd5bSAl Viro 	if (IS_ERR(dentry)) {
69866f54963SPeter Oberparleiter 		kfree(link);
699ff9fb72bSGreg Kroah-Hartman 		return dentry;
700ad5abd5bSAl Viro 	}
701ad5abd5bSAl Viro 
702edac65eaSAl Viro 	inode = debugfs_get_inode(dentry->d_sb);
703680b3024SAl Viro 	if (unlikely(!inode)) {
70443e23b6cSGreg Kroah-Hartman 		pr_err("out of free dentries, can not create symlink '%s'\n",
70543e23b6cSGreg Kroah-Hartman 		       name);
706ad5abd5bSAl Viro 		kfree(link);
7075233e311SAl Viro 		return failed_creating(dentry);
708680b3024SAl Viro 	}
709edac65eaSAl Viro 	inode->i_mode = S_IFLNK | S_IRWXUGO;
7105496197fSDavid Howells 	inode->i_op = &debugfs_symlink_inode_operations;
7115723cb01SAl Viro 	inode->i_link = link;
712680b3024SAl Viro 	d_instantiate(dentry, inode);
7135233e311SAl Viro 	return end_creating(dentry);
71466f54963SPeter Oberparleiter }
71566f54963SPeter Oberparleiter EXPORT_SYMBOL_GPL(debugfs_create_symlink);
71666f54963SPeter Oberparleiter 
__debugfs_file_removed(struct dentry * dentry)717823e545cSAmir Goldstein static void __debugfs_file_removed(struct dentry *dentry)
718e9117a5aSNicolai Stange {
719e9117a5aSNicolai Stange 	struct debugfs_fsdata *fsd;
720e9117a5aSNicolai Stange 
7217d39bc50SNicolai Stange 	/*
7227d39bc50SNicolai Stange 	 * Paired with the closing smp_mb() implied by a successful
7237d39bc50SNicolai Stange 	 * cmpxchg() in debugfs_file_get(): either
7247d39bc50SNicolai Stange 	 * debugfs_file_get() must see a dead dentry or we must see a
7257d39bc50SNicolai Stange 	 * debugfs_fsdata instance at ->d_fsdata here (or both).
7267d39bc50SNicolai Stange 	 */
7277d39bc50SNicolai Stange 	smp_mb();
7287d39bc50SNicolai Stange 	fsd = READ_ONCE(dentry->d_fsdata);
72995688800SAl Viro 	if (!fsd)
7307d39bc50SNicolai Stange 		return;
731f4acfcd4SJohannes Berg 
732952c3fceSJohannes Berg 	/* if this was the last reference, we're done */
733952c3fceSJohannes Berg 	if (refcount_dec_and_test(&fsd->active_users))
7348c88a474SJohannes Berg 		return;
7358c88a474SJohannes Berg 
736952c3fceSJohannes Berg 	/*
737952c3fceSJohannes Berg 	 * If there's still a reference, the code that obtained it can
738952c3fceSJohannes Berg 	 * be in different states:
739952c3fceSJohannes Berg 	 *  - The common case of not using cancellations, or already
740952c3fceSJohannes Berg 	 *    after debugfs_leave_cancellation(), where we just need
741952c3fceSJohannes Berg 	 *    to wait for debugfs_file_put() which signals the completion;
742952c3fceSJohannes Berg 	 *  - inside a cancellation section, i.e. between
743952c3fceSJohannes Berg 	 *    debugfs_enter_cancellation() and debugfs_leave_cancellation(),
744952c3fceSJohannes Berg 	 *    in which case we need to trigger the ->cancel() function,
745952c3fceSJohannes Berg 	 *    and then wait for debugfs_file_put() just like in the
746952c3fceSJohannes Berg 	 *    previous case;
747952c3fceSJohannes Berg 	 *  - before debugfs_enter_cancellation() (but obviously after
748952c3fceSJohannes Berg 	 *    debugfs_file_get()), in which case we may not see the
749952c3fceSJohannes Berg 	 *    cancellation in the list on the first round of the loop,
750952c3fceSJohannes Berg 	 *    but debugfs_enter_cancellation() signals the completion
751952c3fceSJohannes Berg 	 *    after adding it, so this code gets woken up to call the
752952c3fceSJohannes Berg 	 *    ->cancel() function.
753952c3fceSJohannes Berg 	 */
7548c88a474SJohannes Berg 	while (refcount_read(&fsd->active_users)) {
7558c88a474SJohannes Berg 		struct debugfs_cancellation *c;
7568c88a474SJohannes Berg 
7578c88a474SJohannes Berg 		/*
7588c88a474SJohannes Berg 		 * Lock the cancellations. Note that the cancellations
7598c88a474SJohannes Berg 		 * structs are meant to be on the stack, so we need to
7608c88a474SJohannes Berg 		 * ensure we either use them here or don't touch them,
7618c88a474SJohannes Berg 		 * and debugfs_leave_cancellation() will wait for this
7628c88a474SJohannes Berg 		 * to be finished processing before exiting one. It may
7638c88a474SJohannes Berg 		 * of course win and remove the cancellation, but then
7648c88a474SJohannes Berg 		 * chances are we never even got into this bit, we only
7658c88a474SJohannes Berg 		 * do if the refcount isn't zero already.
7668c88a474SJohannes Berg 		 */
7678c88a474SJohannes Berg 		mutex_lock(&fsd->cancellations_mtx);
7688c88a474SJohannes Berg 		while ((c = list_first_entry_or_null(&fsd->cancellations,
7698c88a474SJohannes Berg 						     typeof(*c), list))) {
7708c88a474SJohannes Berg 			list_del_init(&c->list);
7718c88a474SJohannes Berg 			c->cancel(dentry, c->cancel_data);
7728c88a474SJohannes Berg 		}
7738c88a474SJohannes Berg 		mutex_unlock(&fsd->cancellations_mtx);
7748c88a474SJohannes Berg 
7758c88a474SJohannes Berg 		wait_for_completion(&fsd->active_users_drained);
7768c88a474SJohannes Berg 	}
777e9117a5aSNicolai Stange }
778e9117a5aSNicolai Stange 
remove_one(struct dentry * victim)779a3d1e7ebSAl Viro static void remove_one(struct dentry *victim)
7801da177e4SLinus Torvalds {
781a3d1e7ebSAl Viro         if (d_is_reg(victim))
782a3d1e7ebSAl Viro 		__debugfs_file_removed(victim);
7831da177e4SLinus Torvalds 	simple_release_fs(&debugfs_mount, &debugfs_mount_count);
7841da177e4SLinus Torvalds }
7851da177e4SLinus Torvalds 
786cfc94cdfSJan Kara /**
787a3d1e7ebSAl Viro  * debugfs_remove - recursively removes a directory
788398dc4adSUlf Magnusson  * @dentry: a pointer to a the dentry of the directory to be removed.  If this
789398dc4adSUlf Magnusson  *          parameter is NULL or an error value, nothing will be done.
7909505e637SHaavard Skinnemoen  *
7919505e637SHaavard Skinnemoen  * This function recursively removes a directory tree in debugfs that
7929505e637SHaavard Skinnemoen  * was previously created with a call to another debugfs function
7939505e637SHaavard Skinnemoen  * (like debugfs_create_file() or variants thereof.)
7949505e637SHaavard Skinnemoen  *
7959505e637SHaavard Skinnemoen  * This function is required to be called in order for the file to be
7969505e637SHaavard Skinnemoen  * removed, no automatic cleanup of files will happen when a module is
7979505e637SHaavard Skinnemoen  * removed, you are responsible here.
7989505e637SHaavard Skinnemoen  */
debugfs_remove(struct dentry * dentry)799a3d1e7ebSAl Viro void debugfs_remove(struct dentry *dentry)
8009505e637SHaavard Skinnemoen {
801a59d6293SArend van Spriel 	if (IS_ERR_OR_NULL(dentry))
8029505e637SHaavard Skinnemoen 		return;
8039505e637SHaavard Skinnemoen 
804a3d1e7ebSAl Viro 	simple_pin_fs(&debug_fs_type, &debugfs_mount, &debugfs_mount_count);
805a3d1e7ebSAl Viro 	simple_recursive_removal(dentry, remove_one);
806776164c1SOleg Nesterov 	simple_release_fs(&debugfs_mount, &debugfs_mount_count);
807776164c1SOleg Nesterov }
808a3d1e7ebSAl Viro EXPORT_SYMBOL_GPL(debugfs_remove);
8099505e637SHaavard Skinnemoen 
8109505e637SHaavard Skinnemoen /**
811dec9b2f1SGreg Kroah-Hartman  * debugfs_lookup_and_remove - lookup a directory or file and recursively remove it
812dec9b2f1SGreg Kroah-Hartman  * @name: a pointer to a string containing the name of the item to look up.
813dec9b2f1SGreg Kroah-Hartman  * @parent: a pointer to the parent dentry of the item.
814dec9b2f1SGreg Kroah-Hartman  *
815dec9b2f1SGreg Kroah-Hartman  * This is the equlivant of doing something like
816dec9b2f1SGreg Kroah-Hartman  * debugfs_remove(debugfs_lookup(..)) but with the proper reference counting
817dec9b2f1SGreg Kroah-Hartman  * handled for the directory being looked up.
818dec9b2f1SGreg Kroah-Hartman  */
debugfs_lookup_and_remove(const char * name,struct dentry * parent)819dec9b2f1SGreg Kroah-Hartman void debugfs_lookup_and_remove(const char *name, struct dentry *parent)
820dec9b2f1SGreg Kroah-Hartman {
821dec9b2f1SGreg Kroah-Hartman 	struct dentry *dentry;
822dec9b2f1SGreg Kroah-Hartman 
823dec9b2f1SGreg Kroah-Hartman 	dentry = debugfs_lookup(name, parent);
824dec9b2f1SGreg Kroah-Hartman 	if (!dentry)
825dec9b2f1SGreg Kroah-Hartman 		return;
826dec9b2f1SGreg Kroah-Hartman 
827dec9b2f1SGreg Kroah-Hartman 	debugfs_remove(dentry);
828dec9b2f1SGreg Kroah-Hartman 	dput(dentry);
829dec9b2f1SGreg Kroah-Hartman }
830dec9b2f1SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(debugfs_lookup_and_remove);
831dec9b2f1SGreg Kroah-Hartman 
832dec9b2f1SGreg Kroah-Hartman /**
833*f7862dfeSAl Viro  * debugfs_change_name - rename a file/directory in the debugfs filesystem
834*f7862dfeSAl Viro  * @dentry: dentry of an object to be renamed.
835*f7862dfeSAl Viro  * @fmt: format for new name
836cfc94cdfSJan Kara  *
837cfc94cdfSJan Kara  * This function renames a file/directory in debugfs.  The target must not
838cfc94cdfSJan Kara  * exist for rename to succeed.
839cfc94cdfSJan Kara  *
840*f7862dfeSAl Viro  * This function will return 0 on success and -E... on failure.
841cfc94cdfSJan Kara  *
842cfc94cdfSJan Kara  * If debugfs is not enabled in the kernel, the value -%ENODEV will be
843cfc94cdfSJan Kara  * returned.
844cfc94cdfSJan Kara  */
debugfs_change_name(struct dentry * dentry,const char * fmt,...)845*f7862dfeSAl Viro int __printf(2, 3) debugfs_change_name(struct dentry *dentry, const char *fmt, ...)
846cfc94cdfSJan Kara {
847*f7862dfeSAl Viro 	int error = 0;
848*f7862dfeSAl Viro 	const char *new_name;
84949d31c2fSAl Viro 	struct name_snapshot old_name;
850*f7862dfeSAl Viro 	struct dentry *parent, *target;
851*f7862dfeSAl Viro 	struct inode *dir;
852*f7862dfeSAl Viro 	va_list ap;
853cfc94cdfSJan Kara 
854*f7862dfeSAl Viro 	if (IS_ERR_OR_NULL(dentry))
855*f7862dfeSAl Viro 		return 0;
856d88c93f0SGreg Kroah-Hartman 
857*f7862dfeSAl Viro 	va_start(ap, fmt);
858*f7862dfeSAl Viro 	new_name = kvasprintf_const(GFP_KERNEL, fmt, ap);
859*f7862dfeSAl Viro 	va_end(ap);
860*f7862dfeSAl Viro 	if (!new_name)
861*f7862dfeSAl Viro 		return -ENOMEM;
862cfc94cdfSJan Kara 
863*f7862dfeSAl Viro 	parent = dget_parent(dentry);
864*f7862dfeSAl Viro 	dir = d_inode(parent);
865*f7862dfeSAl Viro 	inode_lock(dir);
866cfc94cdfSJan Kara 
867*f7862dfeSAl Viro 	take_dentry_name_snapshot(&old_name, dentry);
868*f7862dfeSAl Viro 
869*f7862dfeSAl Viro 	if (WARN_ON_ONCE(dentry->d_parent != parent)) {
870*f7862dfeSAl Viro 		error = -EINVAL;
871*f7862dfeSAl Viro 		goto out;
872cfc94cdfSJan Kara 	}
873*f7862dfeSAl Viro 	if (strcmp(old_name.name.name, new_name) == 0)
874*f7862dfeSAl Viro 		goto out;
875*f7862dfeSAl Viro 	target = lookup_one_len(new_name, parent, strlen(new_name));
876*f7862dfeSAl Viro 	if (IS_ERR(target)) {
877*f7862dfeSAl Viro 		error = PTR_ERR(target);
878*f7862dfeSAl Viro 		goto out;
879cfc94cdfSJan Kara 	}
880*f7862dfeSAl Viro 	if (d_really_is_positive(target)) {
881*f7862dfeSAl Viro 		dput(target);
882*f7862dfeSAl Viro 		error = -EINVAL;
883*f7862dfeSAl Viro 		goto out;
884*f7862dfeSAl Viro 	}
885*f7862dfeSAl Viro 	simple_rename_timestamp(dir, dentry, dir, target);
886*f7862dfeSAl Viro 	d_move(dentry, target);
887*f7862dfeSAl Viro 	dput(target);
888*f7862dfeSAl Viro 	fsnotify_move(dir, dir, &old_name.name, d_is_dir(dentry), NULL, dentry);
889*f7862dfeSAl Viro out:
890*f7862dfeSAl Viro 	release_dentry_name_snapshot(&old_name);
891*f7862dfeSAl Viro 	inode_unlock(dir);
892*f7862dfeSAl Viro 	dput(parent);
893*f7862dfeSAl Viro 	kfree_const(new_name);
894*f7862dfeSAl Viro 	return error;
895*f7862dfeSAl Viro }
896*f7862dfeSAl Viro EXPORT_SYMBOL_GPL(debugfs_change_name);
897cfc94cdfSJan Kara 
898c0f92ba9SFrederic Weisbecker /**
899c0f92ba9SFrederic Weisbecker  * debugfs_initialized - Tells whether debugfs has been registered
900c0f92ba9SFrederic Weisbecker  */
debugfs_initialized(void)901c0f92ba9SFrederic Weisbecker bool debugfs_initialized(void)
902c0f92ba9SFrederic Weisbecker {
903c0f92ba9SFrederic Weisbecker 	return debugfs_registered;
904c0f92ba9SFrederic Weisbecker }
905c0f92ba9SFrederic Weisbecker EXPORT_SYMBOL_GPL(debugfs_initialized);
906c0f92ba9SFrederic Weisbecker 
debugfs_kernel(char * str)907a24c6f7bSPeter Enderborg static int __init debugfs_kernel(char *str)
908a24c6f7bSPeter Enderborg {
909a24c6f7bSPeter Enderborg 	if (str) {
910a24c6f7bSPeter Enderborg 		if (!strcmp(str, "on"))
911a24c6f7bSPeter Enderborg 			debugfs_allow = DEBUGFS_ALLOW_API | DEBUGFS_ALLOW_MOUNT;
912a24c6f7bSPeter Enderborg 		else if (!strcmp(str, "no-mount"))
913a24c6f7bSPeter Enderborg 			debugfs_allow = DEBUGFS_ALLOW_API;
914a24c6f7bSPeter Enderborg 		else if (!strcmp(str, "off"))
915a24c6f7bSPeter Enderborg 			debugfs_allow = 0;
916a24c6f7bSPeter Enderborg 	}
917a24c6f7bSPeter Enderborg 
918a24c6f7bSPeter Enderborg 	return 0;
919a24c6f7bSPeter Enderborg }
920a24c6f7bSPeter Enderborg early_param("debugfs", debugfs_kernel);
debugfs_init(void)9211da177e4SLinus Torvalds static int __init debugfs_init(void)
9221da177e4SLinus Torvalds {
9231da177e4SLinus Torvalds 	int retval;
9241da177e4SLinus Torvalds 
925a24c6f7bSPeter Enderborg 	if (!(debugfs_allow & DEBUGFS_ALLOW_MOUNT))
926a24c6f7bSPeter Enderborg 		return -EPERM;
927a24c6f7bSPeter Enderborg 
928f9bb4882SEric W. Biederman 	retval = sysfs_create_mount_point(kernel_kobj, "debug");
929f9bb4882SEric W. Biederman 	if (retval)
930f9bb4882SEric W. Biederman 		return retval;
9311da177e4SLinus Torvalds 
932268b3611SAl Viro 	debugfs_inode_cachep = kmem_cache_create("debugfs_inode_cache",
933268b3611SAl Viro 				sizeof(struct debugfs_inode_info), 0,
934268b3611SAl Viro 				SLAB_RECLAIM_ACCOUNT | SLAB_ACCOUNT,
935268b3611SAl Viro 				init_once);
936268b3611SAl Viro 	if (debugfs_inode_cachep == NULL) {
937f9bb4882SEric W. Biederman 		sysfs_remove_mount_point(kernel_kobj, "debug");
938268b3611SAl Viro 		return -ENOMEM;
939268b3611SAl Viro 	}
940c0f92ba9SFrederic Weisbecker 
941268b3611SAl Viro 	retval = register_filesystem(&debug_fs_type);
942268b3611SAl Viro 	if (retval) { // Really not going to happen
943268b3611SAl Viro 		sysfs_remove_mount_point(kernel_kobj, "debug");
944268b3611SAl Viro 		kmem_cache_destroy(debugfs_inode_cachep);
9451da177e4SLinus Torvalds 		return retval;
9461da177e4SLinus Torvalds 	}
947268b3611SAl Viro 	debugfs_registered = true;
948268b3611SAl Viro 	return 0;
949268b3611SAl Viro }
9501da177e4SLinus Torvalds core_initcall(debugfs_init);
951