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