xref: /linux-6.15/fs/debugfs/inode.c (revision f4acfcd4)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  *  inode.c - part of debugfs, a tiny little debug file system
4  *
5  *  Copyright (C) 2004,2019 Greg Kroah-Hartman <[email protected]>
6  *  Copyright (C) 2004 IBM Inc.
7  *  Copyright (C) 2019 Linux Foundation <[email protected]>
8  *
9  *  debugfs is for people to use instead of /proc or /sys.
10  *  See ./Documentation/core-api/kernel-api.rst for more details.
11  */
12 
13 #define pr_fmt(fmt)	"debugfs: " fmt
14 
15 #include <linux/module.h>
16 #include <linux/fs.h>
17 #include <linux/mount.h>
18 #include <linux/pagemap.h>
19 #include <linux/init.h>
20 #include <linux/kobject.h>
21 #include <linux/namei.h>
22 #include <linux/debugfs.h>
23 #include <linux/fsnotify.h>
24 #include <linux/string.h>
25 #include <linux/seq_file.h>
26 #include <linux/parser.h>
27 #include <linux/magic.h>
28 #include <linux/slab.h>
29 #include <linux/security.h>
30 
31 #include "internal.h"
32 
33 #define DEBUGFS_DEFAULT_MODE	0700
34 
35 static struct vfsmount *debugfs_mount;
36 static int debugfs_mount_count;
37 static bool debugfs_registered;
38 static unsigned int debugfs_allow __ro_after_init = DEFAULT_DEBUGFS_ALLOW_BITS;
39 
40 /*
41  * Don't allow access attributes to be changed whilst the kernel is locked down
42  * so that we can use the file mode as part of a heuristic to determine whether
43  * to lock down individual files.
44  */
45 static int debugfs_setattr(struct mnt_idmap *idmap,
46 			   struct dentry *dentry, struct iattr *ia)
47 {
48 	int ret;
49 
50 	if (ia->ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID)) {
51 		ret = security_locked_down(LOCKDOWN_DEBUGFS);
52 		if (ret)
53 			return ret;
54 	}
55 	return simple_setattr(&nop_mnt_idmap, dentry, ia);
56 }
57 
58 static const struct inode_operations debugfs_file_inode_operations = {
59 	.setattr	= debugfs_setattr,
60 };
61 static const struct inode_operations debugfs_dir_inode_operations = {
62 	.lookup		= simple_lookup,
63 	.setattr	= debugfs_setattr,
64 };
65 static const struct inode_operations debugfs_symlink_inode_operations = {
66 	.get_link	= simple_get_link,
67 	.setattr	= debugfs_setattr,
68 };
69 
70 static struct inode *debugfs_get_inode(struct super_block *sb)
71 {
72 	struct inode *inode = new_inode(sb);
73 	if (inode) {
74 		inode->i_ino = get_next_ino();
75 		inode->i_atime = inode->i_mtime = inode_set_ctime_current(inode);
76 	}
77 	return inode;
78 }
79 
80 struct debugfs_mount_opts {
81 	kuid_t uid;
82 	kgid_t gid;
83 	umode_t mode;
84 	/* Opt_* bitfield. */
85 	unsigned int opts;
86 };
87 
88 enum {
89 	Opt_uid,
90 	Opt_gid,
91 	Opt_mode,
92 	Opt_err
93 };
94 
95 static const match_table_t tokens = {
96 	{Opt_uid, "uid=%u"},
97 	{Opt_gid, "gid=%u"},
98 	{Opt_mode, "mode=%o"},
99 	{Opt_err, NULL}
100 };
101 
102 struct debugfs_fs_info {
103 	struct debugfs_mount_opts mount_opts;
104 };
105 
106 static int debugfs_parse_options(char *data, struct debugfs_mount_opts *opts)
107 {
108 	substring_t args[MAX_OPT_ARGS];
109 	int option;
110 	int token;
111 	kuid_t uid;
112 	kgid_t gid;
113 	char *p;
114 
115 	opts->opts = 0;
116 	opts->mode = DEBUGFS_DEFAULT_MODE;
117 
118 	while ((p = strsep(&data, ",")) != NULL) {
119 		if (!*p)
120 			continue;
121 
122 		token = match_token(p, tokens, args);
123 		switch (token) {
124 		case Opt_uid:
125 			if (match_int(&args[0], &option))
126 				return -EINVAL;
127 			uid = make_kuid(current_user_ns(), option);
128 			if (!uid_valid(uid))
129 				return -EINVAL;
130 			opts->uid = uid;
131 			break;
132 		case Opt_gid:
133 			if (match_int(&args[0], &option))
134 				return -EINVAL;
135 			gid = make_kgid(current_user_ns(), option);
136 			if (!gid_valid(gid))
137 				return -EINVAL;
138 			opts->gid = gid;
139 			break;
140 		case Opt_mode:
141 			if (match_octal(&args[0], &option))
142 				return -EINVAL;
143 			opts->mode = option & S_IALLUGO;
144 			break;
145 		/*
146 		 * We might like to report bad mount options here;
147 		 * but traditionally debugfs has ignored all mount options
148 		 */
149 		}
150 
151 		opts->opts |= BIT(token);
152 	}
153 
154 	return 0;
155 }
156 
157 static void _debugfs_apply_options(struct super_block *sb, bool remount)
158 {
159 	struct debugfs_fs_info *fsi = sb->s_fs_info;
160 	struct inode *inode = d_inode(sb->s_root);
161 	struct debugfs_mount_opts *opts = &fsi->mount_opts;
162 
163 	/*
164 	 * On remount, only reset mode/uid/gid if they were provided as mount
165 	 * options.
166 	 */
167 
168 	if (!remount || opts->opts & BIT(Opt_mode)) {
169 		inode->i_mode &= ~S_IALLUGO;
170 		inode->i_mode |= opts->mode;
171 	}
172 
173 	if (!remount || opts->opts & BIT(Opt_uid))
174 		inode->i_uid = opts->uid;
175 
176 	if (!remount || opts->opts & BIT(Opt_gid))
177 		inode->i_gid = opts->gid;
178 }
179 
180 static void debugfs_apply_options(struct super_block *sb)
181 {
182 	_debugfs_apply_options(sb, false);
183 }
184 
185 static void debugfs_apply_options_remount(struct super_block *sb)
186 {
187 	_debugfs_apply_options(sb, true);
188 }
189 
190 static int debugfs_remount(struct super_block *sb, int *flags, char *data)
191 {
192 	int err;
193 	struct debugfs_fs_info *fsi = sb->s_fs_info;
194 
195 	sync_filesystem(sb);
196 	err = debugfs_parse_options(data, &fsi->mount_opts);
197 	if (err)
198 		goto fail;
199 
200 	debugfs_apply_options_remount(sb);
201 
202 fail:
203 	return err;
204 }
205 
206 static int debugfs_show_options(struct seq_file *m, struct dentry *root)
207 {
208 	struct debugfs_fs_info *fsi = root->d_sb->s_fs_info;
209 	struct debugfs_mount_opts *opts = &fsi->mount_opts;
210 
211 	if (!uid_eq(opts->uid, GLOBAL_ROOT_UID))
212 		seq_printf(m, ",uid=%u",
213 			   from_kuid_munged(&init_user_ns, opts->uid));
214 	if (!gid_eq(opts->gid, GLOBAL_ROOT_GID))
215 		seq_printf(m, ",gid=%u",
216 			   from_kgid_munged(&init_user_ns, opts->gid));
217 	if (opts->mode != DEBUGFS_DEFAULT_MODE)
218 		seq_printf(m, ",mode=%o", opts->mode);
219 
220 	return 0;
221 }
222 
223 static void debugfs_free_inode(struct inode *inode)
224 {
225 	if (S_ISLNK(inode->i_mode))
226 		kfree(inode->i_link);
227 	free_inode_nonrcu(inode);
228 }
229 
230 static const struct super_operations debugfs_super_operations = {
231 	.statfs		= simple_statfs,
232 	.remount_fs	= debugfs_remount,
233 	.show_options	= debugfs_show_options,
234 	.free_inode	= debugfs_free_inode,
235 };
236 
237 static void debugfs_release_dentry(struct dentry *dentry)
238 {
239 	struct debugfs_fsdata *fsd = dentry->d_fsdata;
240 
241 	if ((unsigned long)fsd & DEBUGFS_FSDATA_IS_REAL_FOPS_BIT)
242 		return;
243 
244 	/* check it wasn't a dir (no fsdata) or automount (no real_fops) */
245 	if (fsd && fsd->real_fops) {
246 #ifdef CONFIG_LOCKDEP
247 		lockdep_unregister_key(&fsd->key);
248 		kfree(fsd->lock_name);
249 #endif
250 	}
251 
252 	kfree(fsd);
253 }
254 
255 static struct vfsmount *debugfs_automount(struct path *path)
256 {
257 	struct debugfs_fsdata *fsd = path->dentry->d_fsdata;
258 
259 	return fsd->automount(path->dentry, d_inode(path->dentry)->i_private);
260 }
261 
262 static const struct dentry_operations debugfs_dops = {
263 	.d_delete = always_delete_dentry,
264 	.d_release = debugfs_release_dentry,
265 	.d_automount = debugfs_automount,
266 };
267 
268 static int debug_fill_super(struct super_block *sb, void *data, int silent)
269 {
270 	static const struct tree_descr debug_files[] = {{""}};
271 	struct debugfs_fs_info *fsi;
272 	int err;
273 
274 	fsi = kzalloc(sizeof(struct debugfs_fs_info), GFP_KERNEL);
275 	sb->s_fs_info = fsi;
276 	if (!fsi) {
277 		err = -ENOMEM;
278 		goto fail;
279 	}
280 
281 	err = debugfs_parse_options(data, &fsi->mount_opts);
282 	if (err)
283 		goto fail;
284 
285 	err  =  simple_fill_super(sb, DEBUGFS_MAGIC, debug_files);
286 	if (err)
287 		goto fail;
288 
289 	sb->s_op = &debugfs_super_operations;
290 	sb->s_d_op = &debugfs_dops;
291 
292 	debugfs_apply_options(sb);
293 
294 	return 0;
295 
296 fail:
297 	kfree(fsi);
298 	sb->s_fs_info = NULL;
299 	return err;
300 }
301 
302 static struct dentry *debug_mount(struct file_system_type *fs_type,
303 			int flags, const char *dev_name,
304 			void *data)
305 {
306 	if (!(debugfs_allow & DEBUGFS_ALLOW_API))
307 		return ERR_PTR(-EPERM);
308 
309 	return mount_single(fs_type, flags, data, debug_fill_super);
310 }
311 
312 static struct file_system_type debug_fs_type = {
313 	.owner =	THIS_MODULE,
314 	.name =		"debugfs",
315 	.mount =	debug_mount,
316 	.kill_sb =	kill_litter_super,
317 };
318 MODULE_ALIAS_FS("debugfs");
319 
320 /**
321  * debugfs_lookup() - look up an existing debugfs file
322  * @name: a pointer to a string containing the name of the file to look up.
323  * @parent: a pointer to the parent dentry of the file.
324  *
325  * This function will return a pointer to a dentry if it succeeds.  If the file
326  * doesn't exist or an error occurs, %NULL will be returned.  The returned
327  * dentry must be passed to dput() when it is no longer needed.
328  *
329  * If debugfs is not enabled in the kernel, the value -%ENODEV will be
330  * returned.
331  */
332 struct dentry *debugfs_lookup(const char *name, struct dentry *parent)
333 {
334 	struct dentry *dentry;
335 
336 	if (!debugfs_initialized() || IS_ERR_OR_NULL(name) || IS_ERR(parent))
337 		return NULL;
338 
339 	if (!parent)
340 		parent = debugfs_mount->mnt_root;
341 
342 	dentry = lookup_positive_unlocked(name, parent, strlen(name));
343 	if (IS_ERR(dentry))
344 		return NULL;
345 	return dentry;
346 }
347 EXPORT_SYMBOL_GPL(debugfs_lookup);
348 
349 static struct dentry *start_creating(const char *name, struct dentry *parent)
350 {
351 	struct dentry *dentry;
352 	int error;
353 
354 	if (!(debugfs_allow & DEBUGFS_ALLOW_API))
355 		return ERR_PTR(-EPERM);
356 
357 	if (!debugfs_initialized())
358 		return ERR_PTR(-ENOENT);
359 
360 	pr_debug("creating file '%s'\n", name);
361 
362 	if (IS_ERR(parent))
363 		return parent;
364 
365 	error = simple_pin_fs(&debug_fs_type, &debugfs_mount,
366 			      &debugfs_mount_count);
367 	if (error) {
368 		pr_err("Unable to pin filesystem for file '%s'\n", name);
369 		return ERR_PTR(error);
370 	}
371 
372 	/* If the parent is not specified, we create it in the root.
373 	 * We need the root dentry to do this, which is in the super
374 	 * block. A pointer to that is in the struct vfsmount that we
375 	 * have around.
376 	 */
377 	if (!parent)
378 		parent = debugfs_mount->mnt_root;
379 
380 	inode_lock(d_inode(parent));
381 	if (unlikely(IS_DEADDIR(d_inode(parent))))
382 		dentry = ERR_PTR(-ENOENT);
383 	else
384 		dentry = lookup_one_len(name, parent, strlen(name));
385 	if (!IS_ERR(dentry) && d_really_is_positive(dentry)) {
386 		if (d_is_dir(dentry))
387 			pr_err("Directory '%s' with parent '%s' already present!\n",
388 			       name, parent->d_name.name);
389 		else
390 			pr_err("File '%s' in directory '%s' already present!\n",
391 			       name, parent->d_name.name);
392 		dput(dentry);
393 		dentry = ERR_PTR(-EEXIST);
394 	}
395 
396 	if (IS_ERR(dentry)) {
397 		inode_unlock(d_inode(parent));
398 		simple_release_fs(&debugfs_mount, &debugfs_mount_count);
399 	}
400 
401 	return dentry;
402 }
403 
404 static struct dentry *failed_creating(struct dentry *dentry)
405 {
406 	inode_unlock(d_inode(dentry->d_parent));
407 	dput(dentry);
408 	simple_release_fs(&debugfs_mount, &debugfs_mount_count);
409 	return ERR_PTR(-ENOMEM);
410 }
411 
412 static struct dentry *end_creating(struct dentry *dentry)
413 {
414 	inode_unlock(d_inode(dentry->d_parent));
415 	return dentry;
416 }
417 
418 static struct dentry *__debugfs_create_file(const char *name, umode_t mode,
419 				struct dentry *parent, void *data,
420 				const struct file_operations *proxy_fops,
421 				const struct file_operations *real_fops)
422 {
423 	struct dentry *dentry;
424 	struct inode *inode;
425 
426 	if (!(mode & S_IFMT))
427 		mode |= S_IFREG;
428 	BUG_ON(!S_ISREG(mode));
429 	dentry = start_creating(name, parent);
430 
431 	if (IS_ERR(dentry))
432 		return dentry;
433 
434 	if (!(debugfs_allow & DEBUGFS_ALLOW_API)) {
435 		failed_creating(dentry);
436 		return ERR_PTR(-EPERM);
437 	}
438 
439 	inode = debugfs_get_inode(dentry->d_sb);
440 	if (unlikely(!inode)) {
441 		pr_err("out of free dentries, can not create file '%s'\n",
442 		       name);
443 		return failed_creating(dentry);
444 	}
445 
446 	inode->i_mode = mode;
447 	inode->i_private = data;
448 
449 	inode->i_op = &debugfs_file_inode_operations;
450 	inode->i_fop = proxy_fops;
451 	dentry->d_fsdata = (void *)((unsigned long)real_fops |
452 				DEBUGFS_FSDATA_IS_REAL_FOPS_BIT);
453 
454 	d_instantiate(dentry, inode);
455 	fsnotify_create(d_inode(dentry->d_parent), dentry);
456 	return end_creating(dentry);
457 }
458 
459 /**
460  * debugfs_create_file - create a file in the debugfs filesystem
461  * @name: a pointer to a string containing the name of the file to create.
462  * @mode: the permission that the file should have.
463  * @parent: a pointer to the parent dentry for this file.  This should be a
464  *          directory dentry if set.  If this parameter is NULL, then the
465  *          file will be created in the root of the debugfs filesystem.
466  * @data: a pointer to something that the caller will want to get to later
467  *        on.  The inode.i_private pointer will point to this value on
468  *        the open() call.
469  * @fops: a pointer to a struct file_operations that should be used for
470  *        this file.
471  *
472  * This is the basic "create a file" function for debugfs.  It allows for a
473  * wide range of flexibility in creating a file, or a directory (if you want
474  * to create a directory, the debugfs_create_dir() function is
475  * recommended to be used instead.)
476  *
477  * This function will return a pointer to a dentry if it succeeds.  This
478  * pointer must be passed to the debugfs_remove() function when the file is
479  * to be removed (no automatic cleanup happens if your module is unloaded,
480  * you are responsible here.)  If an error occurs, ERR_PTR(-ERROR) will be
481  * returned.
482  *
483  * If debugfs is not enabled in the kernel, the value -%ENODEV will be
484  * returned.
485  *
486  * NOTE: it's expected that most callers should _ignore_ the errors returned
487  * by this function. Other debugfs functions handle the fact that the "dentry"
488  * passed to them could be an error and they don't crash in that case.
489  * Drivers should generally work fine even if debugfs fails to init anyway.
490  */
491 struct dentry *debugfs_create_file(const char *name, umode_t mode,
492 				   struct dentry *parent, void *data,
493 				   const struct file_operations *fops)
494 {
495 
496 	return __debugfs_create_file(name, mode, parent, data,
497 				fops ? &debugfs_full_proxy_file_operations :
498 					&debugfs_noop_file_operations,
499 				fops);
500 }
501 EXPORT_SYMBOL_GPL(debugfs_create_file);
502 
503 /**
504  * debugfs_create_file_unsafe - create a file in the debugfs filesystem
505  * @name: a pointer to a string containing the name of the file to create.
506  * @mode: the permission that the file should have.
507  * @parent: a pointer to the parent dentry for this file.  This should be a
508  *          directory dentry if set.  If this parameter is NULL, then the
509  *          file will be created in the root of the debugfs filesystem.
510  * @data: a pointer to something that the caller will want to get to later
511  *        on.  The inode.i_private pointer will point to this value on
512  *        the open() call.
513  * @fops: a pointer to a struct file_operations that should be used for
514  *        this file.
515  *
516  * debugfs_create_file_unsafe() is completely analogous to
517  * debugfs_create_file(), the only difference being that the fops
518  * handed it will not get protected against file removals by the
519  * debugfs core.
520  *
521  * It is your responsibility to protect your struct file_operation
522  * methods against file removals by means of debugfs_file_get()
523  * and debugfs_file_put(). ->open() is still protected by
524  * debugfs though.
525  *
526  * Any struct file_operations defined by means of
527  * DEFINE_DEBUGFS_ATTRIBUTE() is protected against file removals and
528  * thus, may be used here.
529  */
530 struct dentry *debugfs_create_file_unsafe(const char *name, umode_t mode,
531 				   struct dentry *parent, void *data,
532 				   const struct file_operations *fops)
533 {
534 
535 	return __debugfs_create_file(name, mode, parent, data,
536 				fops ? &debugfs_open_proxy_file_operations :
537 					&debugfs_noop_file_operations,
538 				fops);
539 }
540 EXPORT_SYMBOL_GPL(debugfs_create_file_unsafe);
541 
542 /**
543  * debugfs_create_file_size - create a file in the debugfs filesystem
544  * @name: a pointer to a string containing the name of the file to create.
545  * @mode: the permission that the file should have.
546  * @parent: a pointer to the parent dentry for this file.  This should be a
547  *          directory dentry if set.  If this parameter is NULL, then the
548  *          file will be created in the root of the debugfs filesystem.
549  * @data: a pointer to something that the caller will want to get to later
550  *        on.  The inode.i_private pointer will point to this value on
551  *        the open() call.
552  * @fops: a pointer to a struct file_operations that should be used for
553  *        this file.
554  * @file_size: initial file size
555  *
556  * This is the basic "create a file" function for debugfs.  It allows for a
557  * wide range of flexibility in creating a file, or a directory (if you want
558  * to create a directory, the debugfs_create_dir() function is
559  * recommended to be used instead.)
560  */
561 void debugfs_create_file_size(const char *name, umode_t mode,
562 			      struct dentry *parent, void *data,
563 			      const struct file_operations *fops,
564 			      loff_t file_size)
565 {
566 	struct dentry *de = debugfs_create_file(name, mode, parent, data, fops);
567 
568 	if (!IS_ERR(de))
569 		d_inode(de)->i_size = file_size;
570 }
571 EXPORT_SYMBOL_GPL(debugfs_create_file_size);
572 
573 /**
574  * debugfs_create_dir - create a directory in the debugfs filesystem
575  * @name: a pointer to a string containing the name of the directory to
576  *        create.
577  * @parent: a pointer to the parent dentry for this file.  This should be a
578  *          directory dentry if set.  If this parameter is NULL, then the
579  *          directory will be created in the root of the debugfs filesystem.
580  *
581  * This function creates a directory in debugfs with the given name.
582  *
583  * This function will return a pointer to a dentry if it succeeds.  This
584  * pointer must be passed to the debugfs_remove() function when the file is
585  * to be removed (no automatic cleanup happens if your module is unloaded,
586  * you are responsible here.)  If an error occurs, ERR_PTR(-ERROR) will be
587  * returned.
588  *
589  * If debugfs is not enabled in the kernel, the value -%ENODEV will be
590  * returned.
591  *
592  * NOTE: it's expected that most callers should _ignore_ the errors returned
593  * by this function. Other debugfs functions handle the fact that the "dentry"
594  * passed to them could be an error and they don't crash in that case.
595  * Drivers should generally work fine even if debugfs fails to init anyway.
596  */
597 struct dentry *debugfs_create_dir(const char *name, struct dentry *parent)
598 {
599 	struct dentry *dentry = start_creating(name, parent);
600 	struct inode *inode;
601 
602 	if (IS_ERR(dentry))
603 		return dentry;
604 
605 	if (!(debugfs_allow & DEBUGFS_ALLOW_API)) {
606 		failed_creating(dentry);
607 		return ERR_PTR(-EPERM);
608 	}
609 
610 	inode = debugfs_get_inode(dentry->d_sb);
611 	if (unlikely(!inode)) {
612 		pr_err("out of free dentries, can not create directory '%s'\n",
613 		       name);
614 		return failed_creating(dentry);
615 	}
616 
617 	inode->i_mode = S_IFDIR | S_IRWXU | S_IRUGO | S_IXUGO;
618 	inode->i_op = &debugfs_dir_inode_operations;
619 	inode->i_fop = &simple_dir_operations;
620 
621 	/* directory inodes start off with i_nlink == 2 (for "." entry) */
622 	inc_nlink(inode);
623 	d_instantiate(dentry, inode);
624 	inc_nlink(d_inode(dentry->d_parent));
625 	fsnotify_mkdir(d_inode(dentry->d_parent), dentry);
626 	return end_creating(dentry);
627 }
628 EXPORT_SYMBOL_GPL(debugfs_create_dir);
629 
630 /**
631  * debugfs_create_automount - create automount point in the debugfs filesystem
632  * @name: a pointer to a string containing the name of the file to create.
633  * @parent: a pointer to the parent dentry for this file.  This should be a
634  *          directory dentry if set.  If this parameter is NULL, then the
635  *          file will be created in the root of the debugfs filesystem.
636  * @f: function to be called when pathname resolution steps on that one.
637  * @data: opaque argument to pass to f().
638  *
639  * @f should return what ->d_automount() would.
640  */
641 struct dentry *debugfs_create_automount(const char *name,
642 					struct dentry *parent,
643 					debugfs_automount_t f,
644 					void *data)
645 {
646 	struct dentry *dentry = start_creating(name, parent);
647 	struct debugfs_fsdata *fsd;
648 	struct inode *inode;
649 
650 	if (IS_ERR(dentry))
651 		return dentry;
652 
653 	fsd = kzalloc(sizeof(*fsd), GFP_KERNEL);
654 	if (!fsd) {
655 		failed_creating(dentry);
656 		return ERR_PTR(-ENOMEM);
657 	}
658 
659 	fsd->automount = f;
660 
661 	if (!(debugfs_allow & DEBUGFS_ALLOW_API)) {
662 		failed_creating(dentry);
663 		kfree(fsd);
664 		return ERR_PTR(-EPERM);
665 	}
666 
667 	inode = debugfs_get_inode(dentry->d_sb);
668 	if (unlikely(!inode)) {
669 		pr_err("out of free dentries, can not create automount '%s'\n",
670 		       name);
671 		kfree(fsd);
672 		return failed_creating(dentry);
673 	}
674 
675 	make_empty_dir_inode(inode);
676 	inode->i_flags |= S_AUTOMOUNT;
677 	inode->i_private = data;
678 	dentry->d_fsdata = fsd;
679 	/* directory inodes start off with i_nlink == 2 (for "." entry) */
680 	inc_nlink(inode);
681 	d_instantiate(dentry, inode);
682 	inc_nlink(d_inode(dentry->d_parent));
683 	fsnotify_mkdir(d_inode(dentry->d_parent), dentry);
684 	return end_creating(dentry);
685 }
686 EXPORT_SYMBOL(debugfs_create_automount);
687 
688 /**
689  * debugfs_create_symlink- create a symbolic link in the debugfs filesystem
690  * @name: a pointer to a string containing the name of the symbolic link to
691  *        create.
692  * @parent: a pointer to the parent dentry for this symbolic link.  This
693  *          should be a directory dentry if set.  If this parameter is NULL,
694  *          then the symbolic link will be created in the root of the debugfs
695  *          filesystem.
696  * @target: a pointer to a string containing the path to the target of the
697  *          symbolic link.
698  *
699  * This function creates a symbolic link with the given name in debugfs that
700  * links to the given target path.
701  *
702  * This function will return a pointer to a dentry if it succeeds.  This
703  * pointer must be passed to the debugfs_remove() function when the symbolic
704  * link is to be removed (no automatic cleanup happens if your module is
705  * unloaded, you are responsible here.)  If an error occurs, ERR_PTR(-ERROR)
706  * will be returned.
707  *
708  * If debugfs is not enabled in the kernel, the value -%ENODEV will be
709  * returned.
710  */
711 struct dentry *debugfs_create_symlink(const char *name, struct dentry *parent,
712 				      const char *target)
713 {
714 	struct dentry *dentry;
715 	struct inode *inode;
716 	char *link = kstrdup(target, GFP_KERNEL);
717 	if (!link)
718 		return ERR_PTR(-ENOMEM);
719 
720 	dentry = start_creating(name, parent);
721 	if (IS_ERR(dentry)) {
722 		kfree(link);
723 		return dentry;
724 	}
725 
726 	inode = debugfs_get_inode(dentry->d_sb);
727 	if (unlikely(!inode)) {
728 		pr_err("out of free dentries, can not create symlink '%s'\n",
729 		       name);
730 		kfree(link);
731 		return failed_creating(dentry);
732 	}
733 	inode->i_mode = S_IFLNK | S_IRWXUGO;
734 	inode->i_op = &debugfs_symlink_inode_operations;
735 	inode->i_link = link;
736 	d_instantiate(dentry, inode);
737 	return end_creating(dentry);
738 }
739 EXPORT_SYMBOL_GPL(debugfs_create_symlink);
740 
741 static void __debugfs_file_removed(struct dentry *dentry)
742 {
743 	struct debugfs_fsdata *fsd;
744 
745 	/*
746 	 * Paired with the closing smp_mb() implied by a successful
747 	 * cmpxchg() in debugfs_file_get(): either
748 	 * debugfs_file_get() must see a dead dentry or we must see a
749 	 * debugfs_fsdata instance at ->d_fsdata here (or both).
750 	 */
751 	smp_mb();
752 	fsd = READ_ONCE(dentry->d_fsdata);
753 	if ((unsigned long)fsd & DEBUGFS_FSDATA_IS_REAL_FOPS_BIT)
754 		return;
755 
756 	lock_map_acquire(&fsd->lockdep_map);
757 	lock_map_release(&fsd->lockdep_map);
758 
759 	if (!refcount_dec_and_test(&fsd->active_users))
760 		wait_for_completion(&fsd->active_users_drained);
761 }
762 
763 static void remove_one(struct dentry *victim)
764 {
765         if (d_is_reg(victim))
766 		__debugfs_file_removed(victim);
767 	simple_release_fs(&debugfs_mount, &debugfs_mount_count);
768 }
769 
770 /**
771  * debugfs_remove - recursively removes a directory
772  * @dentry: a pointer to a the dentry of the directory to be removed.  If this
773  *          parameter is NULL or an error value, nothing will be done.
774  *
775  * This function recursively removes a directory tree in debugfs that
776  * was previously created with a call to another debugfs function
777  * (like debugfs_create_file() or variants thereof.)
778  *
779  * This function is required to be called in order for the file to be
780  * removed, no automatic cleanup of files will happen when a module is
781  * removed, you are responsible here.
782  */
783 void debugfs_remove(struct dentry *dentry)
784 {
785 	if (IS_ERR_OR_NULL(dentry))
786 		return;
787 
788 	simple_pin_fs(&debug_fs_type, &debugfs_mount, &debugfs_mount_count);
789 	simple_recursive_removal(dentry, remove_one);
790 	simple_release_fs(&debugfs_mount, &debugfs_mount_count);
791 }
792 EXPORT_SYMBOL_GPL(debugfs_remove);
793 
794 /**
795  * debugfs_lookup_and_remove - lookup a directory or file and recursively remove it
796  * @name: a pointer to a string containing the name of the item to look up.
797  * @parent: a pointer to the parent dentry of the item.
798  *
799  * This is the equlivant of doing something like
800  * debugfs_remove(debugfs_lookup(..)) but with the proper reference counting
801  * handled for the directory being looked up.
802  */
803 void debugfs_lookup_and_remove(const char *name, struct dentry *parent)
804 {
805 	struct dentry *dentry;
806 
807 	dentry = debugfs_lookup(name, parent);
808 	if (!dentry)
809 		return;
810 
811 	debugfs_remove(dentry);
812 	dput(dentry);
813 }
814 EXPORT_SYMBOL_GPL(debugfs_lookup_and_remove);
815 
816 /**
817  * debugfs_rename - rename a file/directory in the debugfs filesystem
818  * @old_dir: a pointer to the parent dentry for the renamed object. This
819  *          should be a directory dentry.
820  * @old_dentry: dentry of an object to be renamed.
821  * @new_dir: a pointer to the parent dentry where the object should be
822  *          moved. This should be a directory dentry.
823  * @new_name: a pointer to a string containing the target name.
824  *
825  * This function renames a file/directory in debugfs.  The target must not
826  * exist for rename to succeed.
827  *
828  * This function will return a pointer to old_dentry (which is updated to
829  * reflect renaming) if it succeeds. If an error occurs, ERR_PTR(-ERROR)
830  * will be returned.
831  *
832  * If debugfs is not enabled in the kernel, the value -%ENODEV will be
833  * returned.
834  */
835 struct dentry *debugfs_rename(struct dentry *old_dir, struct dentry *old_dentry,
836 		struct dentry *new_dir, const char *new_name)
837 {
838 	int error;
839 	struct dentry *dentry = NULL, *trap;
840 	struct name_snapshot old_name;
841 
842 	if (IS_ERR(old_dir))
843 		return old_dir;
844 	if (IS_ERR(new_dir))
845 		return new_dir;
846 	if (IS_ERR_OR_NULL(old_dentry))
847 		return old_dentry;
848 
849 	trap = lock_rename(new_dir, old_dir);
850 	/* Source or destination directories don't exist? */
851 	if (d_really_is_negative(old_dir) || d_really_is_negative(new_dir))
852 		goto exit;
853 	/* Source does not exist, cyclic rename, or mountpoint? */
854 	if (d_really_is_negative(old_dentry) || old_dentry == trap ||
855 	    d_mountpoint(old_dentry))
856 		goto exit;
857 	dentry = lookup_one_len(new_name, new_dir, strlen(new_name));
858 	/* Lookup failed, cyclic rename or target exists? */
859 	if (IS_ERR(dentry) || dentry == trap || d_really_is_positive(dentry))
860 		goto exit;
861 
862 	take_dentry_name_snapshot(&old_name, old_dentry);
863 
864 	error = simple_rename(&nop_mnt_idmap, d_inode(old_dir), old_dentry,
865 			      d_inode(new_dir), dentry, 0);
866 	if (error) {
867 		release_dentry_name_snapshot(&old_name);
868 		goto exit;
869 	}
870 	d_move(old_dentry, dentry);
871 	fsnotify_move(d_inode(old_dir), d_inode(new_dir), &old_name.name,
872 		d_is_dir(old_dentry),
873 		NULL, old_dentry);
874 	release_dentry_name_snapshot(&old_name);
875 	unlock_rename(new_dir, old_dir);
876 	dput(dentry);
877 	return old_dentry;
878 exit:
879 	if (dentry && !IS_ERR(dentry))
880 		dput(dentry);
881 	unlock_rename(new_dir, old_dir);
882 	if (IS_ERR(dentry))
883 		return dentry;
884 	return ERR_PTR(-EINVAL);
885 }
886 EXPORT_SYMBOL_GPL(debugfs_rename);
887 
888 /**
889  * debugfs_initialized - Tells whether debugfs has been registered
890  */
891 bool debugfs_initialized(void)
892 {
893 	return debugfs_registered;
894 }
895 EXPORT_SYMBOL_GPL(debugfs_initialized);
896 
897 static int __init debugfs_kernel(char *str)
898 {
899 	if (str) {
900 		if (!strcmp(str, "on"))
901 			debugfs_allow = DEBUGFS_ALLOW_API | DEBUGFS_ALLOW_MOUNT;
902 		else if (!strcmp(str, "no-mount"))
903 			debugfs_allow = DEBUGFS_ALLOW_API;
904 		else if (!strcmp(str, "off"))
905 			debugfs_allow = 0;
906 	}
907 
908 	return 0;
909 }
910 early_param("debugfs", debugfs_kernel);
911 static int __init debugfs_init(void)
912 {
913 	int retval;
914 
915 	if (!(debugfs_allow & DEBUGFS_ALLOW_MOUNT))
916 		return -EPERM;
917 
918 	retval = sysfs_create_mount_point(kernel_kobj, "debug");
919 	if (retval)
920 		return retval;
921 
922 	retval = register_filesystem(&debug_fs_type);
923 	if (retval)
924 		sysfs_remove_mount_point(kernel_kobj, "debug");
925 	else
926 		debugfs_registered = true;
927 
928 	return retval;
929 }
930 core_initcall(debugfs_init);
931