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 <greg@kroah.com>
6 * Copyright (C) 2004 IBM Inc.
7 * Copyright (C) 2019 Linux Foundation <gregkh@linuxfoundation.org>
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/fs_context.h>
18#include <linux/fs_parser.h>
19#include <linux/pagemap.h>
20#include <linux/init.h>
21#include <linux/kobject.h>
22#include <linux/namei.h>
23#include <linux/debugfs.h>
24#include <linux/fsnotify.h>
25#include <linux/string.h>
26#include <linux/seq_file.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
35static struct vfsmount *debugfs_mount;
36static int debugfs_mount_count;
37static bool debugfs_registered;
38static 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 */
45static 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(what: LOCKDOWN_DEBUGFS);
52 if (ret)
53 return ret;
54 }
55 return simple_setattr(&nop_mnt_idmap, dentry, ia);
56}
57
58static const struct inode_operations debugfs_file_inode_operations = {
59 .setattr = debugfs_setattr,
60};
61static const struct inode_operations debugfs_dir_inode_operations = {
62 .lookup = simple_lookup,
63 .setattr = debugfs_setattr,
64};
65static const struct inode_operations debugfs_symlink_inode_operations = {
66 .get_link = simple_get_link,
67 .setattr = debugfs_setattr,
68};
69
70static 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 simple_inode_init_ts(inode);
76 }
77 return inode;
78}
79
80struct debugfs_fs_info {
81 kuid_t uid;
82 kgid_t gid;
83 umode_t mode;
84 /* Opt_* bitfield. */
85 unsigned int opts;
86};
87
88enum {
89 Opt_uid,
90 Opt_gid,
91 Opt_mode,
92 Opt_source,
93};
94
95static const struct fs_parameter_spec debugfs_param_specs[] = {
96 fsparam_gid ("gid", Opt_gid),
97 fsparam_u32oct ("mode", Opt_mode),
98 fsparam_uid ("uid", Opt_uid),
99 fsparam_string ("source", Opt_source),
100 {}
101};
102
103static int debugfs_parse_param(struct fs_context *fc, struct fs_parameter *param)
104{
105 struct debugfs_fs_info *opts = fc->s_fs_info;
106 struct fs_parse_result result;
107 int opt;
108
109 opt = fs_parse(fc, desc: debugfs_param_specs, param, result: &result);
110 if (opt < 0) {
111 /*
112 * We might like to report bad mount options here; but
113 * traditionally debugfs has ignored all mount options
114 */
115 if (opt == -ENOPARAM)
116 return 0;
117
118 return opt;
119 }
120
121 switch (opt) {
122 case Opt_uid:
123 opts->uid = result.uid;
124 break;
125 case Opt_gid:
126 opts->gid = result.gid;
127 break;
128 case Opt_mode:
129 opts->mode = result.uint_32 & S_IALLUGO;
130 break;
131 case Opt_source:
132 if (fc->source)
133 return invalfc(fc, "Multiple sources specified");
134 fc->source = param->string;
135 param->string = NULL;
136 break;
137 /*
138 * We might like to report bad mount options here;
139 * but traditionally debugfs has ignored all mount options
140 */
141 }
142
143 opts->opts |= BIT(opt);
144
145 return 0;
146}
147
148static void _debugfs_apply_options(struct super_block *sb, bool remount)
149{
150 struct debugfs_fs_info *fsi = sb->s_fs_info;
151 struct inode *inode = d_inode(dentry: sb->s_root);
152
153 /*
154 * On remount, only reset mode/uid/gid if they were provided as mount
155 * options.
156 */
157
158 if (!remount || fsi->opts & BIT(Opt_mode)) {
159 inode->i_mode &= ~S_IALLUGO;
160 inode->i_mode |= fsi->mode;
161 }
162
163 if (!remount || fsi->opts & BIT(Opt_uid))
164 inode->i_uid = fsi->uid;
165
166 if (!remount || fsi->opts & BIT(Opt_gid))
167 inode->i_gid = fsi->gid;
168}
169
170static void debugfs_apply_options(struct super_block *sb)
171{
172 _debugfs_apply_options(sb, remount: false);
173}
174
175static void debugfs_apply_options_remount(struct super_block *sb)
176{
177 _debugfs_apply_options(sb, remount: true);
178}
179
180static int debugfs_reconfigure(struct fs_context *fc)
181{
182 struct super_block *sb = fc->root->d_sb;
183 struct debugfs_fs_info *sb_opts = sb->s_fs_info;
184 struct debugfs_fs_info *new_opts = fc->s_fs_info;
185
186 if (!new_opts)
187 return 0;
188
189 sync_filesystem(sb);
190
191 /* structure copy of new mount options to sb */
192 *sb_opts = *new_opts;
193 debugfs_apply_options_remount(sb);
194
195 return 0;
196}
197
198static int debugfs_show_options(struct seq_file *m, struct dentry *root)
199{
200 struct debugfs_fs_info *fsi = root->d_sb->s_fs_info;
201
202 if (!uid_eq(left: fsi->uid, GLOBAL_ROOT_UID))
203 seq_printf(m, ",uid=%u",
204 from_kuid_munged(&init_user_ns, fsi->uid));
205 if (!gid_eq(fsi->gid, GLOBAL_ROOT_GID))
206 seq_printf(m, ",gid=%u",
207 from_kgid_munged(&init_user_ns, fsi->gid));
208 if (fsi->mode != DEBUGFS_DEFAULT_MODE)
209 seq_printf(m, ",mode=%o", fsi->mode);
210
211 return 0;
212}
213
214static struct kmem_cache *debugfs_inode_cachep __ro_after_init;
215
216static void init_once(void *foo)
217{
218 struct debugfs_inode_info *info = foo;
219 inode_init_once(&info->vfs_inode);
220}
221
222static struct inode *debugfs_alloc_inode(struct super_block *sb)
223{
224 struct debugfs_inode_info *info;
225 info = alloc_inode_sb(sb, debugfs_inode_cachep, GFP_KERNEL);
226 if (!info)
227 return NULL;
228 return &info->vfs_inode;
229}
230
231static void debugfs_free_inode(struct inode *inode)
232{
233 if (S_ISLNK(inode->i_mode))
234 kfree(objp: inode->i_link);
235 kmem_cache_free(s: debugfs_inode_cachep, objp: DEBUGFS_I(inode));
236}
237
238static const struct super_operations debugfs_super_operations = {
239 .statfs = simple_statfs,
240 .show_options = debugfs_show_options,
241 .alloc_inode = debugfs_alloc_inode,
242 .free_inode = debugfs_free_inode,
243};
244
245static void debugfs_release_dentry(struct dentry *dentry)
246{
247 struct debugfs_fsdata *fsd = dentry->d_fsdata;
248
249 if (fsd) {
250 WARN_ON(!list_empty(&fsd->cancellations));
251 mutex_destroy(lock: &fsd->cancellations_mtx);
252 }
253 kfree(objp: fsd);
254}
255
256static struct vfsmount *debugfs_automount(struct path *path)
257{
258 struct inode *inode = path->dentry->d_inode;
259
260 return DEBUGFS_I(inode)->automount(path->dentry, inode->i_private);
261}
262
263static const struct dentry_operations debugfs_dops = {
264 .d_release = debugfs_release_dentry,
265 .d_automount = debugfs_automount,
266};
267
268static int debugfs_fill_super(struct super_block *sb, struct fs_context *fc)
269{
270 static const struct tree_descr debug_files[] = {{""}};
271 int err;
272
273 err = simple_fill_super(sb, DEBUGFS_MAGIC, debug_files);
274 if (err)
275 return err;
276
277 sb->s_op = &debugfs_super_operations;
278 set_default_d_op(sb, &debugfs_dops);
279 sb->s_d_flags |= DCACHE_DONTCACHE;
280
281 debugfs_apply_options(sb);
282
283 return 0;
284}
285
286static int debugfs_get_tree(struct fs_context *fc)
287{
288 int err;
289
290 if (!(debugfs_allow & DEBUGFS_ALLOW_API))
291 return -EPERM;
292
293 err = get_tree_single(fc, fill_super: debugfs_fill_super);
294 if (err)
295 return err;
296
297 return debugfs_reconfigure(fc);
298}
299
300static void debugfs_free_fc(struct fs_context *fc)
301{
302 kfree(objp: fc->s_fs_info);
303}
304
305static const struct fs_context_operations debugfs_context_ops = {
306 .free = debugfs_free_fc,
307 .parse_param = debugfs_parse_param,
308 .get_tree = debugfs_get_tree,
309 .reconfigure = debugfs_reconfigure,
310};
311
312static int debugfs_init_fs_context(struct fs_context *fc)
313{
314 struct debugfs_fs_info *fsi;
315
316 fsi = kzalloc(sizeof(struct debugfs_fs_info), GFP_KERNEL);
317 if (!fsi)
318 return -ENOMEM;
319
320 fsi->mode = DEBUGFS_DEFAULT_MODE;
321
322 fc->s_fs_info = fsi;
323 fc->ops = &debugfs_context_ops;
324 return 0;
325}
326
327static struct file_system_type debug_fs_type = {
328 .owner = THIS_MODULE,
329 .name = "debugfs",
330 .init_fs_context = debugfs_init_fs_context,
331 .parameters = debugfs_param_specs,
332 .kill_sb = kill_litter_super,
333};
334MODULE_ALIAS_FS("debugfs");
335
336/**
337 * debugfs_lookup() - look up an existing debugfs file
338 * @name: a pointer to a string containing the name of the file to look up.
339 * @parent: a pointer to the parent dentry of the file.
340 *
341 * This function will return a pointer to a dentry if it succeeds. If the file
342 * doesn't exist or an error occurs, %NULL will be returned. The returned
343 * dentry must be passed to dput() when it is no longer needed.
344 *
345 * If debugfs is not enabled in the kernel, the value -%ENODEV will be
346 * returned.
347 */
348struct dentry *debugfs_lookup(const char *name, struct dentry *parent)
349{
350 struct dentry *dentry;
351
352 if (!debugfs_initialized() || IS_ERR_OR_NULL(ptr: name) || IS_ERR(ptr: parent))
353 return NULL;
354
355 if (!parent)
356 parent = debugfs_mount->mnt_root;
357
358 dentry = lookup_noperm_positive_unlocked(&QSTR(name), parent);
359 if (IS_ERR(ptr: dentry))
360 return NULL;
361 return dentry;
362}
363EXPORT_SYMBOL_GPL(debugfs_lookup);
364
365static struct dentry *debugfs_start_creating(const char *name,
366 struct dentry *parent)
367{
368 struct dentry *dentry;
369 int error;
370
371 if (!(debugfs_allow & DEBUGFS_ALLOW_API))
372 return ERR_PTR(error: -EPERM);
373
374 if (!debugfs_initialized())
375 return ERR_PTR(error: -ENOENT);
376
377 pr_debug("creating file '%s'\n", name);
378
379 if (IS_ERR(ptr: parent))
380 return parent;
381
382 error = simple_pin_fs(&debug_fs_type, mount: &debugfs_mount,
383 count: &debugfs_mount_count);
384 if (error) {
385 pr_err("Unable to pin filesystem for file '%s'\n", name);
386 return ERR_PTR(error);
387 }
388
389 /* If the parent is not specified, we create it in the root.
390 * We need the root dentry to do this, which is in the super
391 * block. A pointer to that is in the struct vfsmount that we
392 * have around.
393 */
394 if (!parent)
395 parent = debugfs_mount->mnt_root;
396
397 dentry = simple_start_creating(parent, name);
398 if (IS_ERR(ptr: dentry)) {
399 if (dentry == ERR_PTR(error: -EEXIST))
400 pr_err("'%s' already exists in '%pd'\n", name, parent);
401 simple_release_fs(mount: &debugfs_mount, count: &debugfs_mount_count);
402 }
403 return dentry;
404}
405
406static struct dentry *failed_creating(struct dentry *dentry)
407{
408 inode_unlock(inode: d_inode(dentry: dentry->d_parent));
409 dput(dentry);
410 simple_release_fs(mount: &debugfs_mount, count: &debugfs_mount_count);
411 return ERR_PTR(error: -ENOMEM);
412}
413
414static struct dentry *end_creating(struct dentry *dentry)
415{
416 inode_unlock(inode: d_inode(dentry: dentry->d_parent));
417 return dentry;
418}
419
420static struct dentry *__debugfs_create_file(const char *name, umode_t mode,
421 struct dentry *parent, void *data,
422 const void *aux,
423 const struct file_operations *proxy_fops,
424 const void *real_fops)
425{
426 struct dentry *dentry;
427 struct inode *inode;
428
429 if (!(mode & S_IFMT))
430 mode |= S_IFREG;
431 BUG_ON(!S_ISREG(mode));
432 dentry = debugfs_start_creating(name, parent);
433
434 if (IS_ERR(ptr: dentry))
435 return dentry;
436
437 if (!(debugfs_allow & DEBUGFS_ALLOW_API)) {
438 failed_creating(dentry);
439 return ERR_PTR(error: -EPERM);
440 }
441
442 inode = debugfs_get_inode(sb: dentry->d_sb);
443 if (unlikely(!inode)) {
444 pr_err("out of free dentries, can not create file '%s'\n",
445 name);
446 return failed_creating(dentry);
447 }
448
449 inode->i_mode = mode;
450 inode->i_private = data;
451
452 inode->i_op = &debugfs_file_inode_operations;
453 if (!real_fops)
454 proxy_fops = &debugfs_noop_file_operations;
455 inode->i_fop = proxy_fops;
456 DEBUGFS_I(inode)->raw = real_fops;
457 DEBUGFS_I(inode)->aux = (void *)aux;
458
459 d_instantiate(dentry, inode);
460 fsnotify_create(dir: d_inode(dentry: dentry->d_parent), dentry);
461 return end_creating(dentry);
462}
463
464struct dentry *debugfs_create_file_full(const char *name, umode_t mode,
465 struct dentry *parent, void *data,
466 const void *aux,
467 const struct file_operations *fops)
468{
469 return __debugfs_create_file(name, mode, parent, data, aux,
470 proxy_fops: &debugfs_full_proxy_file_operations,
471 real_fops: fops);
472}
473EXPORT_SYMBOL_GPL(debugfs_create_file_full);
474
475struct dentry *debugfs_create_file_short(const char *name, umode_t mode,
476 struct dentry *parent, void *data,
477 const void *aux,
478 const struct debugfs_short_fops *fops)
479{
480 return __debugfs_create_file(name, mode, parent, data, aux,
481 proxy_fops: &debugfs_full_short_proxy_file_operations,
482 real_fops: fops);
483}
484EXPORT_SYMBOL_GPL(debugfs_create_file_short);
485
486/**
487 * debugfs_create_file_unsafe - create a file in the debugfs filesystem
488 * @name: a pointer to a string containing the name of the file to create.
489 * @mode: the permission that the file should have.
490 * @parent: a pointer to the parent dentry for this file. This should be a
491 * directory dentry if set. If this parameter is NULL, then the
492 * file will be created in the root of the debugfs filesystem.
493 * @data: a pointer to something that the caller will want to get to later
494 * on. The inode.i_private pointer will point to this value on
495 * the open() call.
496 * @fops: a pointer to a struct file_operations that should be used for
497 * this file.
498 *
499 * debugfs_create_file_unsafe() is completely analogous to
500 * debugfs_create_file(), the only difference being that the fops
501 * handed it will not get protected against file removals by the
502 * debugfs core.
503 *
504 * It is your responsibility to protect your struct file_operation
505 * methods against file removals by means of debugfs_file_get()
506 * and debugfs_file_put(). ->open() is still protected by
507 * debugfs though.
508 *
509 * Any struct file_operations defined by means of
510 * DEFINE_DEBUGFS_ATTRIBUTE() is protected against file removals and
511 * thus, may be used here.
512 */
513struct dentry *debugfs_create_file_unsafe(const char *name, umode_t mode,
514 struct dentry *parent, void *data,
515 const struct file_operations *fops)
516{
517
518 return __debugfs_create_file(name, mode, parent, data, NULL,
519 proxy_fops: &debugfs_open_proxy_file_operations,
520 real_fops: fops);
521}
522EXPORT_SYMBOL_GPL(debugfs_create_file_unsafe);
523
524/**
525 * debugfs_create_file_size - create a file in the debugfs filesystem
526 * @name: a pointer to a string containing the name of the file to create.
527 * @mode: the permission that the file should have.
528 * @parent: a pointer to the parent dentry for this file. This should be a
529 * directory dentry if set. If this parameter is NULL, then the
530 * file will be created in the root of the debugfs filesystem.
531 * @data: a pointer to something that the caller will want to get to later
532 * on. The inode.i_private pointer will point to this value on
533 * the open() call.
534 * @fops: a pointer to a struct file_operations that should be used for
535 * this file.
536 * @file_size: initial file size
537 *
538 * This is the basic "create a file" function for debugfs. It allows for a
539 * wide range of flexibility in creating a file, or a directory (if you want
540 * to create a directory, the debugfs_create_dir() function is
541 * recommended to be used instead.)
542 */
543void debugfs_create_file_size(const char *name, umode_t mode,
544 struct dentry *parent, void *data,
545 const struct file_operations *fops,
546 loff_t file_size)
547{
548 struct dentry *de = debugfs_create_file(name, mode, parent, data, fops);
549
550 if (!IS_ERR(ptr: de))
551 d_inode(dentry: de)->i_size = file_size;
552}
553EXPORT_SYMBOL_GPL(debugfs_create_file_size);
554
555/**
556 * debugfs_create_dir - create a directory in the debugfs filesystem
557 * @name: a pointer to a string containing the name of the directory to
558 * create.
559 * @parent: a pointer to the parent dentry for this file. This should be a
560 * directory dentry if set. If this parameter is NULL, then the
561 * directory will be created in the root of the debugfs filesystem.
562 *
563 * This function creates a directory in debugfs with the given name.
564 *
565 * This function will return a pointer to a dentry if it succeeds. This
566 * pointer must be passed to the debugfs_remove() function when the file is
567 * to be removed (no automatic cleanup happens if your module is unloaded,
568 * you are responsible here.) If an error occurs, ERR_PTR(-ERROR) will be
569 * returned.
570 *
571 * If debugfs is not enabled in the kernel, the value -%ENODEV will be
572 * returned.
573 *
574 * NOTE: it's expected that most callers should _ignore_ the errors returned
575 * by this function. Other debugfs functions handle the fact that the "dentry"
576 * passed to them could be an error and they don't crash in that case.
577 * Drivers should generally work fine even if debugfs fails to init anyway.
578 */
579struct dentry *debugfs_create_dir(const char *name, struct dentry *parent)
580{
581 struct dentry *dentry = debugfs_start_creating(name, parent);
582 struct inode *inode;
583
584 if (IS_ERR(ptr: dentry))
585 return dentry;
586
587 if (!(debugfs_allow & DEBUGFS_ALLOW_API)) {
588 failed_creating(dentry);
589 return ERR_PTR(error: -EPERM);
590 }
591
592 inode = debugfs_get_inode(sb: dentry->d_sb);
593 if (unlikely(!inode)) {
594 pr_err("out of free dentries, can not create directory '%s'\n",
595 name);
596 return failed_creating(dentry);
597 }
598
599 inode->i_mode = S_IFDIR | S_IRWXU | S_IRUGO | S_IXUGO;
600 inode->i_op = &debugfs_dir_inode_operations;
601 inode->i_fop = &simple_dir_operations;
602
603 /* directory inodes start off with i_nlink == 2 (for "." entry) */
604 inc_nlink(inode);
605 d_instantiate(dentry, inode);
606 inc_nlink(inode: d_inode(dentry: dentry->d_parent));
607 fsnotify_mkdir(dir: d_inode(dentry: dentry->d_parent), dentry);
608 return end_creating(dentry);
609}
610EXPORT_SYMBOL_GPL(debugfs_create_dir);
611
612/**
613 * debugfs_create_automount - create automount point in the debugfs filesystem
614 * @name: a pointer to a string containing the name of the file to create.
615 * @parent: a pointer to the parent dentry for this file. This should be a
616 * directory dentry if set. If this parameter is NULL, then the
617 * file will be created in the root of the debugfs filesystem.
618 * @f: function to be called when pathname resolution steps on that one.
619 * @data: opaque argument to pass to f().
620 *
621 * @f should return what ->d_automount() would.
622 */
623struct dentry *debugfs_create_automount(const char *name,
624 struct dentry *parent,
625 debugfs_automount_t f,
626 void *data)
627{
628 struct dentry *dentry = debugfs_start_creating(name, parent);
629 struct inode *inode;
630
631 if (IS_ERR(ptr: dentry))
632 return dentry;
633
634 if (!(debugfs_allow & DEBUGFS_ALLOW_API)) {
635 failed_creating(dentry);
636 return ERR_PTR(error: -EPERM);
637 }
638
639 inode = debugfs_get_inode(sb: dentry->d_sb);
640 if (unlikely(!inode)) {
641 pr_err("out of free dentries, can not create automount '%s'\n",
642 name);
643 return failed_creating(dentry);
644 }
645
646 make_empty_dir_inode(inode);
647 inode->i_flags |= S_AUTOMOUNT;
648 inode->i_private = data;
649 DEBUGFS_I(inode)->automount = f;
650 /* directory inodes start off with i_nlink == 2 (for "." entry) */
651 inc_nlink(inode);
652 d_instantiate(dentry, inode);
653 inc_nlink(inode: d_inode(dentry: dentry->d_parent));
654 fsnotify_mkdir(dir: d_inode(dentry: dentry->d_parent), dentry);
655 return end_creating(dentry);
656}
657EXPORT_SYMBOL(debugfs_create_automount);
658
659/**
660 * debugfs_create_symlink- create a symbolic link in the debugfs filesystem
661 * @name: a pointer to a string containing the name of the symbolic link to
662 * create.
663 * @parent: a pointer to the parent dentry for this symbolic link. This
664 * should be a directory dentry if set. If this parameter is NULL,
665 * then the symbolic link will be created in the root of the debugfs
666 * filesystem.
667 * @target: a pointer to a string containing the path to the target of the
668 * symbolic link.
669 *
670 * This function creates a symbolic link with the given name in debugfs that
671 * links to the given target path.
672 *
673 * This function will return a pointer to a dentry if it succeeds. This
674 * pointer must be passed to the debugfs_remove() function when the symbolic
675 * link is to be removed (no automatic cleanup happens if your module is
676 * unloaded, you are responsible here.) If an error occurs, ERR_PTR(-ERROR)
677 * will be returned.
678 *
679 * If debugfs is not enabled in the kernel, the value -%ENODEV will be
680 * returned.
681 */
682struct dentry *debugfs_create_symlink(const char *name, struct dentry *parent,
683 const char *target)
684{
685 struct dentry *dentry;
686 struct inode *inode;
687 char *link = kstrdup(s: target, GFP_KERNEL);
688 if (!link)
689 return ERR_PTR(error: -ENOMEM);
690
691 dentry = debugfs_start_creating(name, parent);
692 if (IS_ERR(ptr: dentry)) {
693 kfree(objp: link);
694 return dentry;
695 }
696
697 inode = debugfs_get_inode(sb: dentry->d_sb);
698 if (unlikely(!inode)) {
699 pr_err("out of free dentries, can not create symlink '%s'\n",
700 name);
701 kfree(objp: link);
702 return failed_creating(dentry);
703 }
704 inode->i_mode = S_IFLNK | S_IRWXUGO;
705 inode->i_op = &debugfs_symlink_inode_operations;
706 inode->i_link = link;
707 d_instantiate(dentry, inode);
708 return end_creating(dentry);
709}
710EXPORT_SYMBOL_GPL(debugfs_create_symlink);
711
712static void __debugfs_file_removed(struct dentry *dentry)
713{
714 struct debugfs_fsdata *fsd;
715
716 /*
717 * Paired with the closing smp_mb() implied by a successful
718 * cmpxchg() in debugfs_file_get(): either
719 * debugfs_file_get() must see a dead dentry or we must see a
720 * debugfs_fsdata instance at ->d_fsdata here (or both).
721 */
722 smp_mb();
723 fsd = READ_ONCE(dentry->d_fsdata);
724 if (!fsd)
725 return;
726
727 /* if this was the last reference, we're done */
728 if (refcount_dec_and_test(r: &fsd->active_users))
729 return;
730
731 /*
732 * If there's still a reference, the code that obtained it can
733 * be in different states:
734 * - The common case of not using cancellations, or already
735 * after debugfs_leave_cancellation(), where we just need
736 * to wait for debugfs_file_put() which signals the completion;
737 * - inside a cancellation section, i.e. between
738 * debugfs_enter_cancellation() and debugfs_leave_cancellation(),
739 * in which case we need to trigger the ->cancel() function,
740 * and then wait for debugfs_file_put() just like in the
741 * previous case;
742 * - before debugfs_enter_cancellation() (but obviously after
743 * debugfs_file_get()), in which case we may not see the
744 * cancellation in the list on the first round of the loop,
745 * but debugfs_enter_cancellation() signals the completion
746 * after adding it, so this code gets woken up to call the
747 * ->cancel() function.
748 */
749 while (refcount_read(r: &fsd->active_users)) {
750 struct debugfs_cancellation *c;
751
752 /*
753 * Lock the cancellations. Note that the cancellations
754 * structs are meant to be on the stack, so we need to
755 * ensure we either use them here or don't touch them,
756 * and debugfs_leave_cancellation() will wait for this
757 * to be finished processing before exiting one. It may
758 * of course win and remove the cancellation, but then
759 * chances are we never even got into this bit, we only
760 * do if the refcount isn't zero already.
761 */
762 mutex_lock(lock: &fsd->cancellations_mtx);
763 while ((c = list_first_entry_or_null(&fsd->cancellations,
764 typeof(*c), list))) {
765 list_del_init(entry: &c->list);
766 c->cancel(dentry, c->cancel_data);
767 }
768 mutex_unlock(lock: &fsd->cancellations_mtx);
769
770 wait_for_completion(&fsd->active_users_drained);
771 }
772}
773
774static void remove_one(struct dentry *victim)
775{
776 if (d_is_reg(dentry: victim))
777 __debugfs_file_removed(dentry: victim);
778 simple_release_fs(mount: &debugfs_mount, count: &debugfs_mount_count);
779}
780
781/**
782 * debugfs_remove - recursively removes a directory
783 * @dentry: a pointer to a the dentry of the directory to be removed. If this
784 * parameter is NULL or an error value, nothing will be done.
785 *
786 * This function recursively removes a directory tree in debugfs that
787 * was previously created with a call to another debugfs function
788 * (like debugfs_create_file() or variants thereof.)
789 *
790 * This function is required to be called in order for the file to be
791 * removed, no automatic cleanup of files will happen when a module is
792 * removed, you are responsible here.
793 */
794void debugfs_remove(struct dentry *dentry)
795{
796 if (IS_ERR_OR_NULL(ptr: dentry))
797 return;
798
799 simple_pin_fs(&debug_fs_type, mount: &debugfs_mount, count: &debugfs_mount_count);
800 simple_recursive_removal(dentry, callback: remove_one);
801 simple_release_fs(mount: &debugfs_mount, count: &debugfs_mount_count);
802}
803EXPORT_SYMBOL_GPL(debugfs_remove);
804
805/**
806 * debugfs_lookup_and_remove - lookup a directory or file and recursively remove it
807 * @name: a pointer to a string containing the name of the item to look up.
808 * @parent: a pointer to the parent dentry of the item.
809 *
810 * This is the equlivant of doing something like
811 * debugfs_remove(debugfs_lookup(..)) but with the proper reference counting
812 * handled for the directory being looked up.
813 */
814void debugfs_lookup_and_remove(const char *name, struct dentry *parent)
815{
816 struct dentry *dentry;
817
818 dentry = debugfs_lookup(name, parent);
819 if (!dentry)
820 return;
821
822 debugfs_remove(dentry);
823 dput(dentry);
824}
825EXPORT_SYMBOL_GPL(debugfs_lookup_and_remove);
826
827/**
828 * debugfs_change_name - rename a file/directory in the debugfs filesystem
829 * @dentry: dentry of an object to be renamed.
830 * @fmt: format for new name
831 *
832 * This function renames a file/directory in debugfs. The target must not
833 * exist for rename to succeed.
834 *
835 * This function will return 0 on success and -E... on failure.
836 *
837 * If debugfs is not enabled in the kernel, the value -%ENODEV will be
838 * returned.
839 */
840int __printf(2, 3) debugfs_change_name(struct dentry *dentry, const char *fmt, ...)
841{
842 int error = 0;
843 const char *new_name;
844 struct name_snapshot old_name;
845 struct dentry *parent, *target;
846 struct inode *dir;
847 va_list ap;
848
849 if (IS_ERR_OR_NULL(ptr: dentry))
850 return 0;
851
852 va_start(ap, fmt);
853 new_name = kvasprintf_const(GFP_KERNEL, fmt, args: ap);
854 va_end(ap);
855 if (!new_name)
856 return -ENOMEM;
857
858 parent = dget_parent(dentry);
859 dir = d_inode(dentry: parent);
860 inode_lock(inode: dir);
861
862 take_dentry_name_snapshot(&old_name, dentry);
863
864 if (WARN_ON_ONCE(dentry->d_parent != parent)) {
865 error = -EINVAL;
866 goto out;
867 }
868 if (strcmp(old_name.name.name, new_name) == 0)
869 goto out;
870 target = lookup_noperm(&QSTR(new_name), parent);
871 if (IS_ERR(ptr: target)) {
872 error = PTR_ERR(ptr: target);
873 goto out;
874 }
875 if (d_really_is_positive(dentry: target)) {
876 dput(target);
877 error = -EINVAL;
878 goto out;
879 }
880 simple_rename_timestamp(old_dir: dir, old_dentry: dentry, new_dir: dir, new_dentry: target);
881 d_move(dentry, target);
882 dput(target);
883 fsnotify_move(old_dir: dir, new_dir: dir, old_name: &old_name.name, isdir: d_is_dir(dentry), NULL, moved: dentry);
884out:
885 release_dentry_name_snapshot(&old_name);
886 inode_unlock(inode: dir);
887 dput(parent);
888 kfree_const(x: new_name);
889 return error;
890}
891EXPORT_SYMBOL_GPL(debugfs_change_name);
892
893/**
894 * debugfs_initialized - Tells whether debugfs has been registered
895 */
896bool debugfs_initialized(void)
897{
898 return debugfs_registered;
899}
900EXPORT_SYMBOL_GPL(debugfs_initialized);
901
902static int __init debugfs_kernel(char *str)
903{
904 if (str) {
905 if (!strcmp(str, "on"))
906 debugfs_allow = DEBUGFS_ALLOW_API | DEBUGFS_ALLOW_MOUNT;
907 else if (!strcmp(str, "no-mount"))
908 debugfs_allow = DEBUGFS_ALLOW_API;
909 else if (!strcmp(str, "off"))
910 debugfs_allow = 0;
911 }
912
913 return 0;
914}
915early_param("debugfs", debugfs_kernel);
916static int __init debugfs_init(void)
917{
918 int retval;
919
920 if (!(debugfs_allow & DEBUGFS_ALLOW_MOUNT))
921 return -EPERM;
922
923 retval = sysfs_create_mount_point(parent_kobj: kernel_kobj, name: "debug");
924 if (retval)
925 return retval;
926
927 debugfs_inode_cachep = kmem_cache_create("debugfs_inode_cache",
928 sizeof(struct debugfs_inode_info), 0,
929 SLAB_RECLAIM_ACCOUNT | SLAB_ACCOUNT,
930 init_once);
931 if (debugfs_inode_cachep == NULL) {
932 sysfs_remove_mount_point(parent_kobj: kernel_kobj, name: "debug");
933 return -ENOMEM;
934 }
935
936 retval = register_filesystem(&debug_fs_type);
937 if (retval) { // Really not going to happen
938 sysfs_remove_mount_point(parent_kobj: kernel_kobj, name: "debug");
939 kmem_cache_destroy(s: debugfs_inode_cachep);
940 return retval;
941 }
942 debugfs_registered = true;
943 return 0;
944}
945core_initcall(debugfs_init);
946