xref: /linux-6.15/fs/debugfs/inode.c (revision f8f25893)
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  */
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 
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 
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 
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 
170b8de524cSBrian Norris static void debugfs_apply_options(struct super_block *sb)
171b8de524cSBrian Norris {
172b8de524cSBrian Norris 	_debugfs_apply_options(sb, false);
173b8de524cSBrian Norris }
174b8de524cSBrian Norris 
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 
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 
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 
2116234ddf4SAl Viro static void debugfs_free_inode(struct inode *inode)
2120db59e59SAl Viro {
2130db59e59SAl Viro 	if (S_ISLNK(inode->i_mode))
2145723cb01SAl Viro 		kfree(inode->i_link);
21593b919daSAl Viro 	free_inode_nonrcu(inode);
21693b919daSAl Viro }
21793b919daSAl Viro 
218d6e48686SLudwig Nussel static const struct super_operations debugfs_super_operations = {
219d6e48686SLudwig Nussel 	.statfs		= simple_statfs,
220d6e48686SLudwig Nussel 	.show_options	= debugfs_show_options,
2216234ddf4SAl Viro 	.free_inode	= debugfs_free_inode,
222d6e48686SLudwig Nussel };
223d6e48686SLudwig Nussel 
2247c8d4698SNicolai Stange static void debugfs_release_dentry(struct dentry *dentry)
2257c8d4698SNicolai Stange {
2260ed04a18SJohannes Berg 	struct debugfs_fsdata *fsd = dentry->d_fsdata;
2277d39bc50SNicolai Stange 
2280ed04a18SJohannes Berg 	if ((unsigned long)fsd & DEBUGFS_FSDATA_IS_REAL_FOPS_BIT)
2290ed04a18SJohannes Berg 		return;
2300ed04a18SJohannes Berg 
231f4acfcd4SJohannes Berg 	/* check it wasn't a dir (no fsdata) or automount (no real_fops) */
232f4acfcd4SJohannes Berg 	if (fsd && fsd->real_fops) {
2338c88a474SJohannes Berg 		WARN_ON(!list_empty(&fsd->cancellations));
2348c88a474SJohannes Berg 		mutex_destroy(&fsd->cancellations_mtx);
235f4acfcd4SJohannes Berg 	}
236f4acfcd4SJohannes Berg 
2370ed04a18SJohannes Berg 	kfree(fsd);
2387c8d4698SNicolai Stange }
2397c8d4698SNicolai Stange 
24077b3da6eSAl Viro static struct vfsmount *debugfs_automount(struct path *path)
24177b3da6eSAl Viro {
2420ed04a18SJohannes Berg 	struct debugfs_fsdata *fsd = path->dentry->d_fsdata;
2430ed04a18SJohannes Berg 
2440ed04a18SJohannes Berg 	return fsd->automount(path->dentry, d_inode(path->dentry)->i_private);
24577b3da6eSAl Viro }
24677b3da6eSAl Viro 
24777b3da6eSAl Viro static const struct dentry_operations debugfs_dops = {
24877b3da6eSAl Viro 	.d_delete = always_delete_dentry,
2497c8d4698SNicolai Stange 	.d_release = debugfs_release_dentry,
25077b3da6eSAl Viro 	.d_automount = debugfs_automount,
25177b3da6eSAl Viro };
25277b3da6eSAl Viro 
253a20971c1SDavid Howells static int debugfs_fill_super(struct super_block *sb, struct fs_context *fc)
2541da177e4SLinus Torvalds {
255cda37124SEric Biggers 	static const struct tree_descr debug_files[] = {{""}};
256d6e48686SLudwig Nussel 	int err;
2571da177e4SLinus Torvalds 
258d6e48686SLudwig Nussel 	err = simple_fill_super(sb, DEBUGFS_MAGIC, debug_files);
259d6e48686SLudwig Nussel 	if (err)
260a20971c1SDavid Howells 		return err;
261d6e48686SLudwig Nussel 
262d6e48686SLudwig Nussel 	sb->s_op = &debugfs_super_operations;
26377b3da6eSAl Viro 	sb->s_d_op = &debugfs_dops;
264d6e48686SLudwig Nussel 
265d6e48686SLudwig Nussel 	debugfs_apply_options(sb);
266d6e48686SLudwig Nussel 
267d6e48686SLudwig Nussel 	return 0;
2681da177e4SLinus Torvalds }
2691da177e4SLinus Torvalds 
270a20971c1SDavid Howells static int debugfs_get_tree(struct fs_context *fc)
2711da177e4SLinus Torvalds {
272a24c6f7bSPeter Enderborg 	if (!(debugfs_allow & DEBUGFS_ALLOW_API))
273a20971c1SDavid Howells 		return -EPERM;
274a24c6f7bSPeter Enderborg 
275a20971c1SDavid Howells 	return get_tree_single(fc, debugfs_fill_super);
276a20971c1SDavid Howells }
277a20971c1SDavid Howells 
278a20971c1SDavid Howells static void debugfs_free_fc(struct fs_context *fc)
279a20971c1SDavid Howells {
280a20971c1SDavid Howells 	kfree(fc->s_fs_info);
281a20971c1SDavid Howells }
282a20971c1SDavid Howells 
283a20971c1SDavid Howells static const struct fs_context_operations debugfs_context_ops = {
284a20971c1SDavid Howells 	.free		= debugfs_free_fc,
285a20971c1SDavid Howells 	.parse_param	= debugfs_parse_param,
286a20971c1SDavid Howells 	.get_tree	= debugfs_get_tree,
287a20971c1SDavid Howells 	.reconfigure	= debugfs_reconfigure,
288a20971c1SDavid Howells };
289a20971c1SDavid Howells 
290a20971c1SDavid Howells static int debugfs_init_fs_context(struct fs_context *fc)
291a20971c1SDavid Howells {
292a20971c1SDavid Howells 	struct debugfs_fs_info *fsi;
293a20971c1SDavid Howells 
294a20971c1SDavid Howells 	fsi = kzalloc(sizeof(struct debugfs_fs_info), GFP_KERNEL);
295a20971c1SDavid Howells 	if (!fsi)
296a20971c1SDavid Howells 		return -ENOMEM;
297a20971c1SDavid Howells 
298a20971c1SDavid Howells 	fsi->mode = DEBUGFS_DEFAULT_MODE;
299a20971c1SDavid Howells 
300a20971c1SDavid Howells 	fc->s_fs_info = fsi;
301a20971c1SDavid Howells 	fc->ops = &debugfs_context_ops;
302a20971c1SDavid Howells 	return 0;
3031da177e4SLinus Torvalds }
3041da177e4SLinus Torvalds 
3051da177e4SLinus Torvalds static struct file_system_type debug_fs_type = {
3061da177e4SLinus Torvalds 	.owner =	THIS_MODULE,
3071da177e4SLinus Torvalds 	.name =		"debugfs",
308a20971c1SDavid Howells 	.init_fs_context = debugfs_init_fs_context,
309a20971c1SDavid Howells 	.parameters =	debugfs_param_specs,
3101da177e4SLinus Torvalds 	.kill_sb =	kill_litter_super,
3111da177e4SLinus Torvalds };
3127f78e035SEric W. Biederman MODULE_ALIAS_FS("debugfs");
3131da177e4SLinus Torvalds 
314a7c5437bSOmar Sandoval /**
315a7c5437bSOmar Sandoval  * debugfs_lookup() - look up an existing debugfs file
316a7c5437bSOmar Sandoval  * @name: a pointer to a string containing the name of the file to look up.
317a7c5437bSOmar Sandoval  * @parent: a pointer to the parent dentry of the file.
318a7c5437bSOmar Sandoval  *
319a7c5437bSOmar Sandoval  * This function will return a pointer to a dentry if it succeeds.  If the file
320a7c5437bSOmar Sandoval  * doesn't exist or an error occurs, %NULL will be returned.  The returned
321a7c5437bSOmar Sandoval  * dentry must be passed to dput() when it is no longer needed.
322a7c5437bSOmar Sandoval  *
323a7c5437bSOmar Sandoval  * If debugfs is not enabled in the kernel, the value -%ENODEV will be
324a7c5437bSOmar Sandoval  * returned.
325a7c5437bSOmar Sandoval  */
326a7c5437bSOmar Sandoval struct dentry *debugfs_lookup(const char *name, struct dentry *parent)
327a7c5437bSOmar Sandoval {
328a7c5437bSOmar Sandoval 	struct dentry *dentry;
329a7c5437bSOmar Sandoval 
330bc6de804SGreg Kroah-Hartman 	if (!debugfs_initialized() || IS_ERR_OR_NULL(name) || IS_ERR(parent))
331a7c5437bSOmar Sandoval 		return NULL;
332a7c5437bSOmar Sandoval 
333a7c5437bSOmar Sandoval 	if (!parent)
334a7c5437bSOmar Sandoval 		parent = debugfs_mount->mnt_root;
335a7c5437bSOmar Sandoval 
3366c2d4798SAl Viro 	dentry = lookup_positive_unlocked(name, parent, strlen(name));
337a7c5437bSOmar Sandoval 	if (IS_ERR(dentry))
338a7c5437bSOmar Sandoval 		return NULL;
339a7c5437bSOmar Sandoval 	return dentry;
340a7c5437bSOmar Sandoval }
341a7c5437bSOmar Sandoval EXPORT_SYMBOL_GPL(debugfs_lookup);
342a7c5437bSOmar Sandoval 
343190afd81SAl Viro static struct dentry *start_creating(const char *name, struct dentry *parent)
344c3b1a350SAl Viro {
345190afd81SAl Viro 	struct dentry *dentry;
346c3b1a350SAl Viro 	int error;
347c3b1a350SAl Viro 
348a24c6f7bSPeter Enderborg 	if (!(debugfs_allow & DEBUGFS_ALLOW_API))
349a24c6f7bSPeter Enderborg 		return ERR_PTR(-EPERM);
350a24c6f7bSPeter Enderborg 
35156348560SGreg Kroah-Hartman 	if (!debugfs_initialized())
35256348560SGreg Kroah-Hartman 		return ERR_PTR(-ENOENT);
35356348560SGreg Kroah-Hartman 
354d03ae477SGreg Kroah-Hartman 	pr_debug("creating file '%s'\n", name);
355c3b1a350SAl Viro 
356c9e15f25SGreg KH 	if (IS_ERR(parent))
357c9e15f25SGreg KH 		return parent;
358c9e15f25SGreg KH 
359c3b1a350SAl Viro 	error = simple_pin_fs(&debug_fs_type, &debugfs_mount,
360c3b1a350SAl Viro 			      &debugfs_mount_count);
36143e23b6cSGreg Kroah-Hartman 	if (error) {
36243e23b6cSGreg Kroah-Hartman 		pr_err("Unable to pin filesystem for file '%s'\n", name);
363190afd81SAl Viro 		return ERR_PTR(error);
36443e23b6cSGreg Kroah-Hartman 	}
365c3b1a350SAl Viro 
366cfa57c11SAl Viro 	/* If the parent is not specified, we create it in the root.
367cfa57c11SAl Viro 	 * We need the root dentry to do this, which is in the super
368cfa57c11SAl Viro 	 * block. A pointer to that is in the struct vfsmount that we
369cfa57c11SAl Viro 	 * have around.
370cfa57c11SAl Viro 	 */
371cfa57c11SAl Viro 	if (!parent)
372cfa57c11SAl Viro 		parent = debugfs_mount->mnt_root;
373cfa57c11SAl Viro 
3745955102cSAl Viro 	inode_lock(d_inode(parent));
375a3d1e7ebSAl Viro 	if (unlikely(IS_DEADDIR(d_inode(parent))))
376a3d1e7ebSAl Viro 		dentry = ERR_PTR(-ENOENT);
377a3d1e7ebSAl Viro 	else
378cfa57c11SAl Viro 		dentry = lookup_one_len(name, parent, strlen(name));
3792b0143b5SDavid Howells 	if (!IS_ERR(dentry) && d_really_is_positive(dentry)) {
380c33d4423SGreg Kroah-Hartman 		if (d_is_dir(dentry))
381c33d4423SGreg Kroah-Hartman 			pr_err("Directory '%s' with parent '%s' already present!\n",
382c33d4423SGreg Kroah-Hartman 			       name, parent->d_name.name);
383c33d4423SGreg Kroah-Hartman 		else
384c33d4423SGreg Kroah-Hartman 			pr_err("File '%s' in directory '%s' already present!\n",
385c33d4423SGreg Kroah-Hartman 			       name, parent->d_name.name);
386190afd81SAl Viro 		dput(dentry);
387190afd81SAl Viro 		dentry = ERR_PTR(-EEXIST);
388190afd81SAl Viro 	}
3890ee9608cSDaniel Borkmann 
3900ee9608cSDaniel Borkmann 	if (IS_ERR(dentry)) {
3915955102cSAl Viro 		inode_unlock(d_inode(parent));
3920ee9608cSDaniel Borkmann 		simple_release_fs(&debugfs_mount, &debugfs_mount_count);
3930ee9608cSDaniel Borkmann 	}
3940ee9608cSDaniel Borkmann 
395190afd81SAl Viro 	return dentry;
396190afd81SAl Viro }
397190afd81SAl Viro 
3985233e311SAl Viro static struct dentry *failed_creating(struct dentry *dentry)
399190afd81SAl Viro {
4005955102cSAl Viro 	inode_unlock(d_inode(dentry->d_parent));
401190afd81SAl Viro 	dput(dentry);
402190afd81SAl Viro 	simple_release_fs(&debugfs_mount, &debugfs_mount_count);
403ff9fb72bSGreg Kroah-Hartman 	return ERR_PTR(-ENOMEM);
404190afd81SAl Viro }
4055233e311SAl Viro 
4065233e311SAl Viro static struct dentry *end_creating(struct dentry *dentry)
4075233e311SAl Viro {
4085955102cSAl Viro 	inode_unlock(d_inode(dentry->d_parent));
409190afd81SAl Viro 	return dentry;
410190afd81SAl Viro }
411190afd81SAl Viro 
41249d200deSNicolai Stange static struct dentry *__debugfs_create_file(const char *name, umode_t mode,
41349d200deSNicolai Stange 				struct dentry *parent, void *data,
41449d200deSNicolai Stange 				const struct file_operations *proxy_fops,
4158dc6d81cSJohannes Berg 				const void *real_fops)
41649d200deSNicolai Stange {
41749d200deSNicolai Stange 	struct dentry *dentry;
41849d200deSNicolai Stange 	struct inode *inode;
41949d200deSNicolai Stange 
42049d200deSNicolai Stange 	if (!(mode & S_IFMT))
42149d200deSNicolai Stange 		mode |= S_IFREG;
42249d200deSNicolai Stange 	BUG_ON(!S_ISREG(mode));
42349d200deSNicolai Stange 	dentry = start_creating(name, parent);
42449d200deSNicolai Stange 
4257d39bc50SNicolai Stange 	if (IS_ERR(dentry))
426ff9fb72bSGreg Kroah-Hartman 		return dentry;
42749d200deSNicolai Stange 
428a24c6f7bSPeter Enderborg 	if (!(debugfs_allow & DEBUGFS_ALLOW_API)) {
429a24c6f7bSPeter Enderborg 		failed_creating(dentry);
430a24c6f7bSPeter Enderborg 		return ERR_PTR(-EPERM);
431a24c6f7bSPeter Enderborg 	}
432a24c6f7bSPeter Enderborg 
43349d200deSNicolai Stange 	inode = debugfs_get_inode(dentry->d_sb);
43443e23b6cSGreg Kroah-Hartman 	if (unlikely(!inode)) {
43543e23b6cSGreg Kroah-Hartman 		pr_err("out of free dentries, can not create file '%s'\n",
43643e23b6cSGreg Kroah-Hartman 		       name);
43749d200deSNicolai Stange 		return failed_creating(dentry);
43843e23b6cSGreg Kroah-Hartman 	}
43949d200deSNicolai Stange 
44049d200deSNicolai Stange 	inode->i_mode = mode;
44149d200deSNicolai Stange 	inode->i_private = data;
44249d200deSNicolai Stange 
4435496197fSDavid Howells 	inode->i_op = &debugfs_file_inode_operations;
44449d200deSNicolai Stange 	inode->i_fop = proxy_fops;
4457d39bc50SNicolai Stange 	dentry->d_fsdata = (void *)((unsigned long)real_fops |
4467d39bc50SNicolai Stange 				DEBUGFS_FSDATA_IS_REAL_FOPS_BIT);
44749d200deSNicolai Stange 
44849d200deSNicolai Stange 	d_instantiate(dentry, inode);
44949d200deSNicolai Stange 	fsnotify_create(d_inode(dentry->d_parent), dentry);
45049d200deSNicolai Stange 	return end_creating(dentry);
45149d200deSNicolai Stange }
45249d200deSNicolai Stange 
4538dc6d81cSJohannes Berg struct dentry *debugfs_create_file_full(const char *name, umode_t mode,
4541da177e4SLinus Torvalds 					struct dentry *parent, void *data,
45599ac48f5SArjan van de Ven 					const struct file_operations *fops)
4561da177e4SLinus Torvalds {
4578dc6d81cSJohannes Berg 	if (WARN_ON((unsigned long)fops &
458*f8f25893SJohannes Berg 		    DEBUGFS_FSDATA_IS_REAL_FOPS_BIT))
4598dc6d81cSJohannes Berg 		return ERR_PTR(-EINVAL);
460c3b1a350SAl Viro 
46149d200deSNicolai Stange 	return __debugfs_create_file(name, mode, parent, data,
46249d200deSNicolai Stange 				fops ? &debugfs_full_proxy_file_operations :
46349d200deSNicolai Stange 					&debugfs_noop_file_operations,
46449d200deSNicolai Stange 				fops);
4651da177e4SLinus Torvalds }
4668dc6d81cSJohannes Berg EXPORT_SYMBOL_GPL(debugfs_create_file_full);
4678dc6d81cSJohannes Berg 
4688dc6d81cSJohannes Berg struct dentry *debugfs_create_file_short(const char *name, umode_t mode,
4698dc6d81cSJohannes Berg 					 struct dentry *parent, void *data,
4708dc6d81cSJohannes Berg 					 const struct debugfs_short_fops *fops)
4718dc6d81cSJohannes Berg {
4728dc6d81cSJohannes Berg 	if (WARN_ON((unsigned long)fops &
473*f8f25893SJohannes Berg 		    DEBUGFS_FSDATA_IS_REAL_FOPS_BIT))
4748dc6d81cSJohannes Berg 		return ERR_PTR(-EINVAL);
4758dc6d81cSJohannes Berg 
4768dc6d81cSJohannes Berg 	return __debugfs_create_file(name, mode, parent, data,
477*f8f25893SJohannes Berg 				fops ? &debugfs_full_short_proxy_file_operations :
4788dc6d81cSJohannes Berg 					&debugfs_noop_file_operations,
479*f8f25893SJohannes Berg 				fops);
4808dc6d81cSJohannes Berg }
4818dc6d81cSJohannes Berg EXPORT_SYMBOL_GPL(debugfs_create_file_short);
4821da177e4SLinus Torvalds 
483c6468808SNicolai Stange /**
484c6468808SNicolai Stange  * debugfs_create_file_unsafe - create a file in the debugfs filesystem
485c6468808SNicolai Stange  * @name: a pointer to a string containing the name of the file to create.
486c6468808SNicolai Stange  * @mode: the permission that the file should have.
487c6468808SNicolai Stange  * @parent: a pointer to the parent dentry for this file.  This should be a
488c6468808SNicolai Stange  *          directory dentry if set.  If this parameter is NULL, then the
489c6468808SNicolai Stange  *          file will be created in the root of the debugfs filesystem.
490c6468808SNicolai Stange  * @data: a pointer to something that the caller will want to get to later
491c6468808SNicolai Stange  *        on.  The inode.i_private pointer will point to this value on
492c6468808SNicolai Stange  *        the open() call.
493c6468808SNicolai Stange  * @fops: a pointer to a struct file_operations that should be used for
494c6468808SNicolai Stange  *        this file.
495c6468808SNicolai Stange  *
496c6468808SNicolai Stange  * debugfs_create_file_unsafe() is completely analogous to
497c6468808SNicolai Stange  * debugfs_create_file(), the only difference being that the fops
498c6468808SNicolai Stange  * handed it will not get protected against file removals by the
499c6468808SNicolai Stange  * debugfs core.
500c6468808SNicolai Stange  *
501c6468808SNicolai Stange  * It is your responsibility to protect your struct file_operation
5020eeb2731SSergey Senozhatsky  * methods against file removals by means of debugfs_file_get()
5030eeb2731SSergey Senozhatsky  * and debugfs_file_put(). ->open() is still protected by
504c6468808SNicolai Stange  * debugfs though.
505c6468808SNicolai Stange  *
506c6468808SNicolai Stange  * Any struct file_operations defined by means of
507c6468808SNicolai Stange  * DEFINE_DEBUGFS_ATTRIBUTE() is protected against file removals and
508c6468808SNicolai Stange  * thus, may be used here.
509c6468808SNicolai Stange  */
51049d200deSNicolai Stange struct dentry *debugfs_create_file_unsafe(const char *name, umode_t mode,
51149d200deSNicolai Stange 				   struct dentry *parent, void *data,
51249d200deSNicolai Stange 				   const struct file_operations *fops)
51349d200deSNicolai Stange {
51449d200deSNicolai Stange 
51549d200deSNicolai Stange 	return __debugfs_create_file(name, mode, parent, data,
51649d200deSNicolai Stange 				fops ? &debugfs_open_proxy_file_operations :
51749d200deSNicolai Stange 					&debugfs_noop_file_operations,
51849d200deSNicolai Stange 				fops);
51949d200deSNicolai Stange }
520c6468808SNicolai Stange EXPORT_SYMBOL_GPL(debugfs_create_file_unsafe);
52149d200deSNicolai Stange 
5221da177e4SLinus Torvalds /**
523e59b4e91SDavid Howells  * debugfs_create_file_size - create a file in the debugfs filesystem
524e59b4e91SDavid Howells  * @name: a pointer to a string containing the name of the file to create.
525e59b4e91SDavid Howells  * @mode: the permission that the file should have.
526e59b4e91SDavid Howells  * @parent: a pointer to the parent dentry for this file.  This should be a
527e59b4e91SDavid Howells  *          directory dentry if set.  If this parameter is NULL, then the
528e59b4e91SDavid Howells  *          file will be created in the root of the debugfs filesystem.
529e59b4e91SDavid Howells  * @data: a pointer to something that the caller will want to get to later
530e59b4e91SDavid Howells  *        on.  The inode.i_private pointer will point to this value on
531e59b4e91SDavid Howells  *        the open() call.
532e59b4e91SDavid Howells  * @fops: a pointer to a struct file_operations that should be used for
533e59b4e91SDavid Howells  *        this file.
534e59b4e91SDavid Howells  * @file_size: initial file size
535e59b4e91SDavid Howells  *
536e59b4e91SDavid Howells  * This is the basic "create a file" function for debugfs.  It allows for a
537e59b4e91SDavid Howells  * wide range of flexibility in creating a file, or a directory (if you want
538e59b4e91SDavid Howells  * to create a directory, the debugfs_create_dir() function is
539e59b4e91SDavid Howells  * recommended to be used instead.)
540e59b4e91SDavid Howells  */
541526ee72dSGreg Kroah-Hartman void debugfs_create_file_size(const char *name, umode_t mode,
542e59b4e91SDavid Howells 			      struct dentry *parent, void *data,
543e59b4e91SDavid Howells 			      const struct file_operations *fops,
544e59b4e91SDavid Howells 			      loff_t file_size)
545e59b4e91SDavid Howells {
546e59b4e91SDavid Howells 	struct dentry *de = debugfs_create_file(name, mode, parent, data, fops);
547e59b4e91SDavid Howells 
548af505cadSNirmoy Das 	if (!IS_ERR(de))
5492b0143b5SDavid Howells 		d_inode(de)->i_size = file_size;
550e59b4e91SDavid Howells }
551e59b4e91SDavid Howells EXPORT_SYMBOL_GPL(debugfs_create_file_size);
552e59b4e91SDavid Howells 
553e59b4e91SDavid Howells /**
5541da177e4SLinus Torvalds  * debugfs_create_dir - create a directory in the debugfs filesystem
5551da177e4SLinus Torvalds  * @name: a pointer to a string containing the name of the directory to
5561da177e4SLinus Torvalds  *        create.
5571da177e4SLinus Torvalds  * @parent: a pointer to the parent dentry for this file.  This should be a
558e227867fSMasanari Iida  *          directory dentry if set.  If this parameter is NULL, then the
5591da177e4SLinus Torvalds  *          directory will be created in the root of the debugfs filesystem.
5601da177e4SLinus Torvalds  *
5611da177e4SLinus Torvalds  * This function creates a directory in debugfs with the given name.
5621da177e4SLinus Torvalds  *
5631da177e4SLinus Torvalds  * This function will return a pointer to a dentry if it succeeds.  This
5641da177e4SLinus Torvalds  * pointer must be passed to the debugfs_remove() function when the file is
5651da177e4SLinus Torvalds  * to be removed (no automatic cleanup happens if your module is unloaded,
566adc92dd4SDaniel W. S. Almeida  * you are responsible here.)  If an error occurs, ERR_PTR(-ERROR) will be
567ff9fb72bSGreg Kroah-Hartman  * returned.
5681da177e4SLinus Torvalds  *
5696468b3afSRandy Dunlap  * If debugfs is not enabled in the kernel, the value -%ENODEV will be
570873760fbSCornelia Huck  * returned.
5718bcbde2bSDouglas Anderson  *
5728bcbde2bSDouglas Anderson  * NOTE: it's expected that most callers should _ignore_ the errors returned
5738bcbde2bSDouglas Anderson  * by this function. Other debugfs functions handle the fact that the "dentry"
5748bcbde2bSDouglas Anderson  * passed to them could be an error and they don't crash in that case.
5758bcbde2bSDouglas Anderson  * Drivers should generally work fine even if debugfs fails to init anyway.
5761da177e4SLinus Torvalds  */
5771da177e4SLinus Torvalds struct dentry *debugfs_create_dir(const char *name, struct dentry *parent)
5781da177e4SLinus Torvalds {
579ad5abd5bSAl Viro 	struct dentry *dentry = start_creating(name, parent);
580680b3024SAl Viro 	struct inode *inode;
581ad5abd5bSAl Viro 
582ad5abd5bSAl Viro 	if (IS_ERR(dentry))
583ff9fb72bSGreg Kroah-Hartman 		return dentry;
584ad5abd5bSAl Viro 
585a24c6f7bSPeter Enderborg 	if (!(debugfs_allow & DEBUGFS_ALLOW_API)) {
586a24c6f7bSPeter Enderborg 		failed_creating(dentry);
587a24c6f7bSPeter Enderborg 		return ERR_PTR(-EPERM);
588a24c6f7bSPeter Enderborg 	}
589a24c6f7bSPeter Enderborg 
590edac65eaSAl Viro 	inode = debugfs_get_inode(dentry->d_sb);
59143e23b6cSGreg Kroah-Hartman 	if (unlikely(!inode)) {
59243e23b6cSGreg Kroah-Hartman 		pr_err("out of free dentries, can not create directory '%s'\n",
59343e23b6cSGreg Kroah-Hartman 		       name);
5945233e311SAl Viro 		return failed_creating(dentry);
59543e23b6cSGreg Kroah-Hartman 	}
596680b3024SAl Viro 
597f5b7769eSLinus Torvalds 	inode->i_mode = S_IFDIR | S_IRWXU | S_IRUGO | S_IXUGO;
5985496197fSDavid Howells 	inode->i_op = &debugfs_dir_inode_operations;
599edac65eaSAl Viro 	inode->i_fop = &simple_dir_operations;
600edac65eaSAl Viro 
601edac65eaSAl Viro 	/* directory inodes start off with i_nlink == 2 (for "." entry) */
602edac65eaSAl Viro 	inc_nlink(inode);
603680b3024SAl Viro 	d_instantiate(dentry, inode);
6042b0143b5SDavid Howells 	inc_nlink(d_inode(dentry->d_parent));
6052b0143b5SDavid Howells 	fsnotify_mkdir(d_inode(dentry->d_parent), dentry);
6065233e311SAl Viro 	return end_creating(dentry);
6071da177e4SLinus Torvalds }
6081da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(debugfs_create_dir);
6091da177e4SLinus Torvalds 
6101da177e4SLinus Torvalds /**
61177b3da6eSAl Viro  * debugfs_create_automount - create automount point in the debugfs filesystem
61277b3da6eSAl Viro  * @name: a pointer to a string containing the name of the file to create.
61377b3da6eSAl Viro  * @parent: a pointer to the parent dentry for this file.  This should be a
61477b3da6eSAl Viro  *          directory dentry if set.  If this parameter is NULL, then the
61577b3da6eSAl Viro  *          file will be created in the root of the debugfs filesystem.
61677b3da6eSAl Viro  * @f: function to be called when pathname resolution steps on that one.
61777b3da6eSAl Viro  * @data: opaque argument to pass to f().
61877b3da6eSAl Viro  *
61977b3da6eSAl Viro  * @f should return what ->d_automount() would.
62077b3da6eSAl Viro  */
62177b3da6eSAl Viro struct dentry *debugfs_create_automount(const char *name,
62277b3da6eSAl Viro 					struct dentry *parent,
62393faccbbSEric W. Biederman 					debugfs_automount_t f,
62477b3da6eSAl Viro 					void *data)
62577b3da6eSAl Viro {
62677b3da6eSAl Viro 	struct dentry *dentry = start_creating(name, parent);
6270ed04a18SJohannes Berg 	struct debugfs_fsdata *fsd;
62877b3da6eSAl Viro 	struct inode *inode;
62977b3da6eSAl Viro 
63077b3da6eSAl Viro 	if (IS_ERR(dentry))
631ff9fb72bSGreg Kroah-Hartman 		return dentry;
63277b3da6eSAl Viro 
6330ed04a18SJohannes Berg 	fsd = kzalloc(sizeof(*fsd), GFP_KERNEL);
6340ed04a18SJohannes Berg 	if (!fsd) {
6350ed04a18SJohannes Berg 		failed_creating(dentry);
6360ed04a18SJohannes Berg 		return ERR_PTR(-ENOMEM);
6370ed04a18SJohannes Berg 	}
6380ed04a18SJohannes Berg 
6390ed04a18SJohannes Berg 	fsd->automount = f;
6400ed04a18SJohannes Berg 
641a24c6f7bSPeter Enderborg 	if (!(debugfs_allow & DEBUGFS_ALLOW_API)) {
642a24c6f7bSPeter Enderborg 		failed_creating(dentry);
6430ed04a18SJohannes Berg 		kfree(fsd);
644a24c6f7bSPeter Enderborg 		return ERR_PTR(-EPERM);
645a24c6f7bSPeter Enderborg 	}
646a24c6f7bSPeter Enderborg 
64777b3da6eSAl Viro 	inode = debugfs_get_inode(dentry->d_sb);
64843e23b6cSGreg Kroah-Hartman 	if (unlikely(!inode)) {
64943e23b6cSGreg Kroah-Hartman 		pr_err("out of free dentries, can not create automount '%s'\n",
65043e23b6cSGreg Kroah-Hartman 		       name);
6510ed04a18SJohannes Berg 		kfree(fsd);
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;
6580ed04a18SJohannes Berg 	dentry->d_fsdata = fsd;
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;
735f4acfcd4SJohannes Berg 
736952c3fceSJohannes Berg 	/* if this was the last reference, we're done */
737952c3fceSJohannes Berg 	if (refcount_dec_and_test(&fsd->active_users))
7388c88a474SJohannes Berg 		return;
7398c88a474SJohannes Berg 
740952c3fceSJohannes Berg 	/*
741952c3fceSJohannes Berg 	 * If there's still a reference, the code that obtained it can
742952c3fceSJohannes Berg 	 * be in different states:
743952c3fceSJohannes Berg 	 *  - The common case of not using cancellations, or already
744952c3fceSJohannes Berg 	 *    after debugfs_leave_cancellation(), where we just need
745952c3fceSJohannes Berg 	 *    to wait for debugfs_file_put() which signals the completion;
746952c3fceSJohannes Berg 	 *  - inside a cancellation section, i.e. between
747952c3fceSJohannes Berg 	 *    debugfs_enter_cancellation() and debugfs_leave_cancellation(),
748952c3fceSJohannes Berg 	 *    in which case we need to trigger the ->cancel() function,
749952c3fceSJohannes Berg 	 *    and then wait for debugfs_file_put() just like in the
750952c3fceSJohannes Berg 	 *    previous case;
751952c3fceSJohannes Berg 	 *  - before debugfs_enter_cancellation() (but obviously after
752952c3fceSJohannes Berg 	 *    debugfs_file_get()), in which case we may not see the
753952c3fceSJohannes Berg 	 *    cancellation in the list on the first round of the loop,
754952c3fceSJohannes Berg 	 *    but debugfs_enter_cancellation() signals the completion
755952c3fceSJohannes Berg 	 *    after adding it, so this code gets woken up to call the
756952c3fceSJohannes Berg 	 *    ->cancel() function.
757952c3fceSJohannes Berg 	 */
7588c88a474SJohannes Berg 	while (refcount_read(&fsd->active_users)) {
7598c88a474SJohannes Berg 		struct debugfs_cancellation *c;
7608c88a474SJohannes Berg 
7618c88a474SJohannes Berg 		/*
7628c88a474SJohannes Berg 		 * Lock the cancellations. Note that the cancellations
7638c88a474SJohannes Berg 		 * structs are meant to be on the stack, so we need to
7648c88a474SJohannes Berg 		 * ensure we either use them here or don't touch them,
7658c88a474SJohannes Berg 		 * and debugfs_leave_cancellation() will wait for this
7668c88a474SJohannes Berg 		 * to be finished processing before exiting one. It may
7678c88a474SJohannes Berg 		 * of course win and remove the cancellation, but then
7688c88a474SJohannes Berg 		 * chances are we never even got into this bit, we only
7698c88a474SJohannes Berg 		 * do if the refcount isn't zero already.
7708c88a474SJohannes Berg 		 */
7718c88a474SJohannes Berg 		mutex_lock(&fsd->cancellations_mtx);
7728c88a474SJohannes Berg 		while ((c = list_first_entry_or_null(&fsd->cancellations,
7738c88a474SJohannes Berg 						     typeof(*c), list))) {
7748c88a474SJohannes Berg 			list_del_init(&c->list);
7758c88a474SJohannes Berg 			c->cancel(dentry, c->cancel_data);
7768c88a474SJohannes Berg 		}
7778c88a474SJohannes Berg 		mutex_unlock(&fsd->cancellations_mtx);
7788c88a474SJohannes Berg 
7798c88a474SJohannes Berg 		wait_for_completion(&fsd->active_users_drained);
7808c88a474SJohannes Berg 	}
781e9117a5aSNicolai Stange }
782e9117a5aSNicolai Stange 
783a3d1e7ebSAl Viro static void remove_one(struct dentry *victim)
7841da177e4SLinus Torvalds {
785a3d1e7ebSAl Viro         if (d_is_reg(victim))
786a3d1e7ebSAl Viro 		__debugfs_file_removed(victim);
7871da177e4SLinus Torvalds 	simple_release_fs(&debugfs_mount, &debugfs_mount_count);
7881da177e4SLinus Torvalds }
7891da177e4SLinus Torvalds 
790cfc94cdfSJan Kara /**
791a3d1e7ebSAl Viro  * debugfs_remove - recursively removes a directory
792398dc4adSUlf Magnusson  * @dentry: a pointer to a the dentry of the directory to be removed.  If this
793398dc4adSUlf Magnusson  *          parameter is NULL or an error value, nothing will be done.
7949505e637SHaavard Skinnemoen  *
7959505e637SHaavard Skinnemoen  * This function recursively removes a directory tree in debugfs that
7969505e637SHaavard Skinnemoen  * was previously created with a call to another debugfs function
7979505e637SHaavard Skinnemoen  * (like debugfs_create_file() or variants thereof.)
7989505e637SHaavard Skinnemoen  *
7999505e637SHaavard Skinnemoen  * This function is required to be called in order for the file to be
8009505e637SHaavard Skinnemoen  * removed, no automatic cleanup of files will happen when a module is
8019505e637SHaavard Skinnemoen  * removed, you are responsible here.
8029505e637SHaavard Skinnemoen  */
803a3d1e7ebSAl Viro void debugfs_remove(struct dentry *dentry)
8049505e637SHaavard Skinnemoen {
805a59d6293SArend van Spriel 	if (IS_ERR_OR_NULL(dentry))
8069505e637SHaavard Skinnemoen 		return;
8079505e637SHaavard Skinnemoen 
808a3d1e7ebSAl Viro 	simple_pin_fs(&debug_fs_type, &debugfs_mount, &debugfs_mount_count);
809a3d1e7ebSAl Viro 	simple_recursive_removal(dentry, remove_one);
810776164c1SOleg Nesterov 	simple_release_fs(&debugfs_mount, &debugfs_mount_count);
811776164c1SOleg Nesterov }
812a3d1e7ebSAl Viro EXPORT_SYMBOL_GPL(debugfs_remove);
8139505e637SHaavard Skinnemoen 
8149505e637SHaavard Skinnemoen /**
815dec9b2f1SGreg Kroah-Hartman  * debugfs_lookup_and_remove - lookup a directory or file and recursively remove it
816dec9b2f1SGreg Kroah-Hartman  * @name: a pointer to a string containing the name of the item to look up.
817dec9b2f1SGreg Kroah-Hartman  * @parent: a pointer to the parent dentry of the item.
818dec9b2f1SGreg Kroah-Hartman  *
819dec9b2f1SGreg Kroah-Hartman  * This is the equlivant of doing something like
820dec9b2f1SGreg Kroah-Hartman  * debugfs_remove(debugfs_lookup(..)) but with the proper reference counting
821dec9b2f1SGreg Kroah-Hartman  * handled for the directory being looked up.
822dec9b2f1SGreg Kroah-Hartman  */
823dec9b2f1SGreg Kroah-Hartman void debugfs_lookup_and_remove(const char *name, struct dentry *parent)
824dec9b2f1SGreg Kroah-Hartman {
825dec9b2f1SGreg Kroah-Hartman 	struct dentry *dentry;
826dec9b2f1SGreg Kroah-Hartman 
827dec9b2f1SGreg Kroah-Hartman 	dentry = debugfs_lookup(name, parent);
828dec9b2f1SGreg Kroah-Hartman 	if (!dentry)
829dec9b2f1SGreg Kroah-Hartman 		return;
830dec9b2f1SGreg Kroah-Hartman 
831dec9b2f1SGreg Kroah-Hartman 	debugfs_remove(dentry);
832dec9b2f1SGreg Kroah-Hartman 	dput(dentry);
833dec9b2f1SGreg Kroah-Hartman }
834dec9b2f1SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(debugfs_lookup_and_remove);
835dec9b2f1SGreg Kroah-Hartman 
836dec9b2f1SGreg Kroah-Hartman /**
837cfc94cdfSJan Kara  * debugfs_rename - rename a file/directory in the debugfs filesystem
838cfc94cdfSJan Kara  * @old_dir: a pointer to the parent dentry for the renamed object. This
839cfc94cdfSJan Kara  *          should be a directory dentry.
840cfc94cdfSJan Kara  * @old_dentry: dentry of an object to be renamed.
841cfc94cdfSJan Kara  * @new_dir: a pointer to the parent dentry where the object should be
842cfc94cdfSJan Kara  *          moved. This should be a directory dentry.
843cfc94cdfSJan Kara  * @new_name: a pointer to a string containing the target name.
844cfc94cdfSJan Kara  *
845cfc94cdfSJan Kara  * This function renames a file/directory in debugfs.  The target must not
846cfc94cdfSJan Kara  * exist for rename to succeed.
847cfc94cdfSJan Kara  *
848cfc94cdfSJan Kara  * This function will return a pointer to old_dentry (which is updated to
84988cd618dSBagas Sanjaya  * reflect renaming) if it succeeds. If an error occurs, ERR_PTR(-ERROR)
850d3002468SQi Zheng  * will be returned.
851cfc94cdfSJan Kara  *
852cfc94cdfSJan Kara  * If debugfs is not enabled in the kernel, the value -%ENODEV will be
853cfc94cdfSJan Kara  * returned.
854cfc94cdfSJan Kara  */
855cfc94cdfSJan Kara struct dentry *debugfs_rename(struct dentry *old_dir, struct dentry *old_dentry,
856cfc94cdfSJan Kara 		struct dentry *new_dir, const char *new_name)
857cfc94cdfSJan Kara {
858cfc94cdfSJan Kara 	int error;
859cfc94cdfSJan Kara 	struct dentry *dentry = NULL, *trap;
86049d31c2fSAl Viro 	struct name_snapshot old_name;
861cfc94cdfSJan Kara 
862d88c93f0SGreg Kroah-Hartman 	if (IS_ERR(old_dir))
863d88c93f0SGreg Kroah-Hartman 		return old_dir;
864d88c93f0SGreg Kroah-Hartman 	if (IS_ERR(new_dir))
865d88c93f0SGreg Kroah-Hartman 		return new_dir;
866d88c93f0SGreg Kroah-Hartman 	if (IS_ERR_OR_NULL(old_dentry))
867d88c93f0SGreg Kroah-Hartman 		return old_dentry;
868d88c93f0SGreg Kroah-Hartman 
869cfc94cdfSJan Kara 	trap = lock_rename(new_dir, old_dir);
870cfc94cdfSJan Kara 	/* Source or destination directories don't exist? */
8712b0143b5SDavid Howells 	if (d_really_is_negative(old_dir) || d_really_is_negative(new_dir))
872cfc94cdfSJan Kara 		goto exit;
873cfc94cdfSJan Kara 	/* Source does not exist, cyclic rename, or mountpoint? */
8742b0143b5SDavid Howells 	if (d_really_is_negative(old_dentry) || old_dentry == trap ||
875cfc94cdfSJan Kara 	    d_mountpoint(old_dentry))
876cfc94cdfSJan Kara 		goto exit;
877cfc94cdfSJan Kara 	dentry = lookup_one_len(new_name, new_dir, strlen(new_name));
878cfc94cdfSJan Kara 	/* Lookup failed, cyclic rename or target exists? */
8792b0143b5SDavid Howells 	if (IS_ERR(dentry) || dentry == trap || d_really_is_positive(dentry))
880cfc94cdfSJan Kara 		goto exit;
881cfc94cdfSJan Kara 
88249d31c2fSAl Viro 	take_dentry_name_snapshot(&old_name, old_dentry);
883cfc94cdfSJan Kara 
884e18275aeSChristian Brauner 	error = simple_rename(&nop_mnt_idmap, d_inode(old_dir), old_dentry,
885549c7297SChristian Brauner 			      d_inode(new_dir), dentry, 0);
886cfc94cdfSJan Kara 	if (error) {
88749d31c2fSAl Viro 		release_dentry_name_snapshot(&old_name);
888cfc94cdfSJan Kara 		goto exit;
889cfc94cdfSJan Kara 	}
890cfc94cdfSJan Kara 	d_move(old_dentry, dentry);
891f4ec3a3dSAl Viro 	fsnotify_move(d_inode(old_dir), d_inode(new_dir), &old_name.name,
892e36cb0b8SDavid Howells 		d_is_dir(old_dentry),
8935a190ae6SAl Viro 		NULL, old_dentry);
89449d31c2fSAl Viro 	release_dentry_name_snapshot(&old_name);
895cfc94cdfSJan Kara 	unlock_rename(new_dir, old_dir);
896cfc94cdfSJan Kara 	dput(dentry);
897cfc94cdfSJan Kara 	return old_dentry;
898cfc94cdfSJan Kara exit:
899cfc94cdfSJan Kara 	if (dentry && !IS_ERR(dentry))
900cfc94cdfSJan Kara 		dput(dentry);
901cfc94cdfSJan Kara 	unlock_rename(new_dir, old_dir);
902ff9fb72bSGreg Kroah-Hartman 	if (IS_ERR(dentry))
903ff9fb72bSGreg Kroah-Hartman 		return dentry;
904ff9fb72bSGreg Kroah-Hartman 	return ERR_PTR(-EINVAL);
905cfc94cdfSJan Kara }
906cfc94cdfSJan Kara EXPORT_SYMBOL_GPL(debugfs_rename);
907cfc94cdfSJan Kara 
908c0f92ba9SFrederic Weisbecker /**
909c0f92ba9SFrederic Weisbecker  * debugfs_initialized - Tells whether debugfs has been registered
910c0f92ba9SFrederic Weisbecker  */
911c0f92ba9SFrederic Weisbecker bool debugfs_initialized(void)
912c0f92ba9SFrederic Weisbecker {
913c0f92ba9SFrederic Weisbecker 	return debugfs_registered;
914c0f92ba9SFrederic Weisbecker }
915c0f92ba9SFrederic Weisbecker EXPORT_SYMBOL_GPL(debugfs_initialized);
916c0f92ba9SFrederic Weisbecker 
917a24c6f7bSPeter Enderborg static int __init debugfs_kernel(char *str)
918a24c6f7bSPeter Enderborg {
919a24c6f7bSPeter Enderborg 	if (str) {
920a24c6f7bSPeter Enderborg 		if (!strcmp(str, "on"))
921a24c6f7bSPeter Enderborg 			debugfs_allow = DEBUGFS_ALLOW_API | DEBUGFS_ALLOW_MOUNT;
922a24c6f7bSPeter Enderborg 		else if (!strcmp(str, "no-mount"))
923a24c6f7bSPeter Enderborg 			debugfs_allow = DEBUGFS_ALLOW_API;
924a24c6f7bSPeter Enderborg 		else if (!strcmp(str, "off"))
925a24c6f7bSPeter Enderborg 			debugfs_allow = 0;
926a24c6f7bSPeter Enderborg 	}
927a24c6f7bSPeter Enderborg 
928a24c6f7bSPeter Enderborg 	return 0;
929a24c6f7bSPeter Enderborg }
930a24c6f7bSPeter Enderborg early_param("debugfs", debugfs_kernel);
9311da177e4SLinus Torvalds static int __init debugfs_init(void)
9321da177e4SLinus Torvalds {
9331da177e4SLinus Torvalds 	int retval;
9341da177e4SLinus Torvalds 
935a24c6f7bSPeter Enderborg 	if (!(debugfs_allow & DEBUGFS_ALLOW_MOUNT))
936a24c6f7bSPeter Enderborg 		return -EPERM;
937a24c6f7bSPeter Enderborg 
938f9bb4882SEric W. Biederman 	retval = sysfs_create_mount_point(kernel_kobj, "debug");
939f9bb4882SEric W. Biederman 	if (retval)
940f9bb4882SEric W. Biederman 		return retval;
9411da177e4SLinus Torvalds 
9421da177e4SLinus Torvalds 	retval = register_filesystem(&debug_fs_type);
9431da177e4SLinus Torvalds 	if (retval)
944f9bb4882SEric W. Biederman 		sysfs_remove_mount_point(kernel_kobj, "debug");
945c0f92ba9SFrederic Weisbecker 	else
946c0f92ba9SFrederic Weisbecker 		debugfs_registered = true;
947c0f92ba9SFrederic Weisbecker 
9481da177e4SLinus Torvalds 	return retval;
9491da177e4SLinus Torvalds }
9501da177e4SLinus Torvalds core_initcall(debugfs_init);
951