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