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