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