Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  *  inode.c - part of debugfs, a tiny little debug file system
0004  *
0005  *  Copyright (C) 2004,2019 Greg Kroah-Hartman <greg@kroah.com>
0006  *  Copyright (C) 2004 IBM Inc.
0007  *  Copyright (C) 2019 Linux Foundation <gregkh@linuxfoundation.org>
0008  *
0009  *  debugfs is for people to use instead of /proc or /sys.
0010  *  See ./Documentation/core-api/kernel-api.rst for more details.
0011  */
0012 
0013 #define pr_fmt(fmt) "debugfs: " fmt
0014 
0015 #include <linux/module.h>
0016 #include <linux/fs.h>
0017 #include <linux/mount.h>
0018 #include <linux/pagemap.h>
0019 #include <linux/init.h>
0020 #include <linux/kobject.h>
0021 #include <linux/namei.h>
0022 #include <linux/debugfs.h>
0023 #include <linux/fsnotify.h>
0024 #include <linux/string.h>
0025 #include <linux/seq_file.h>
0026 #include <linux/parser.h>
0027 #include <linux/magic.h>
0028 #include <linux/slab.h>
0029 #include <linux/security.h>
0030 
0031 #include "internal.h"
0032 
0033 #define DEBUGFS_DEFAULT_MODE    0700
0034 
0035 static struct vfsmount *debugfs_mount;
0036 static int debugfs_mount_count;
0037 static bool debugfs_registered;
0038 static unsigned int debugfs_allow __ro_after_init = DEFAULT_DEBUGFS_ALLOW_BITS;
0039 
0040 /*
0041  * Don't allow access attributes to be changed whilst the kernel is locked down
0042  * so that we can use the file mode as part of a heuristic to determine whether
0043  * to lock down individual files.
0044  */
0045 static int debugfs_setattr(struct user_namespace *mnt_userns,
0046                struct dentry *dentry, struct iattr *ia)
0047 {
0048     int ret;
0049 
0050     if (ia->ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID)) {
0051         ret = security_locked_down(LOCKDOWN_DEBUGFS);
0052         if (ret)
0053             return ret;
0054     }
0055     return simple_setattr(&init_user_ns, dentry, ia);
0056 }
0057 
0058 static const struct inode_operations debugfs_file_inode_operations = {
0059     .setattr    = debugfs_setattr,
0060 };
0061 static const struct inode_operations debugfs_dir_inode_operations = {
0062     .lookup     = simple_lookup,
0063     .setattr    = debugfs_setattr,
0064 };
0065 static const struct inode_operations debugfs_symlink_inode_operations = {
0066     .get_link   = simple_get_link,
0067     .setattr    = debugfs_setattr,
0068 };
0069 
0070 static struct inode *debugfs_get_inode(struct super_block *sb)
0071 {
0072     struct inode *inode = new_inode(sb);
0073     if (inode) {
0074         inode->i_ino = get_next_ino();
0075         inode->i_atime = inode->i_mtime =
0076             inode->i_ctime = current_time(inode);
0077     }
0078     return inode;
0079 }
0080 
0081 struct debugfs_mount_opts {
0082     kuid_t uid;
0083     kgid_t gid;
0084     umode_t mode;
0085 };
0086 
0087 enum {
0088     Opt_uid,
0089     Opt_gid,
0090     Opt_mode,
0091     Opt_err
0092 };
0093 
0094 static const match_table_t tokens = {
0095     {Opt_uid, "uid=%u"},
0096     {Opt_gid, "gid=%u"},
0097     {Opt_mode, "mode=%o"},
0098     {Opt_err, NULL}
0099 };
0100 
0101 struct debugfs_fs_info {
0102     struct debugfs_mount_opts mount_opts;
0103 };
0104 
0105 static int debugfs_parse_options(char *data, struct debugfs_mount_opts *opts)
0106 {
0107     substring_t args[MAX_OPT_ARGS];
0108     int option;
0109     int token;
0110     kuid_t uid;
0111     kgid_t gid;
0112     char *p;
0113 
0114     opts->mode = DEBUGFS_DEFAULT_MODE;
0115 
0116     while ((p = strsep(&data, ",")) != NULL) {
0117         if (!*p)
0118             continue;
0119 
0120         token = match_token(p, tokens, args);
0121         switch (token) {
0122         case Opt_uid:
0123             if (match_int(&args[0], &option))
0124                 return -EINVAL;
0125             uid = make_kuid(current_user_ns(), option);
0126             if (!uid_valid(uid))
0127                 return -EINVAL;
0128             opts->uid = uid;
0129             break;
0130         case Opt_gid:
0131             if (match_int(&args[0], &option))
0132                 return -EINVAL;
0133             gid = make_kgid(current_user_ns(), option);
0134             if (!gid_valid(gid))
0135                 return -EINVAL;
0136             opts->gid = gid;
0137             break;
0138         case Opt_mode:
0139             if (match_octal(&args[0], &option))
0140                 return -EINVAL;
0141             opts->mode = option & S_IALLUGO;
0142             break;
0143         /*
0144          * We might like to report bad mount options here;
0145          * but traditionally debugfs has ignored all mount options
0146          */
0147         }
0148     }
0149 
0150     return 0;
0151 }
0152 
0153 static int debugfs_apply_options(struct super_block *sb)
0154 {
0155     struct debugfs_fs_info *fsi = sb->s_fs_info;
0156     struct inode *inode = d_inode(sb->s_root);
0157     struct debugfs_mount_opts *opts = &fsi->mount_opts;
0158 
0159     inode->i_mode &= ~S_IALLUGO;
0160     inode->i_mode |= opts->mode;
0161 
0162     inode->i_uid = opts->uid;
0163     inode->i_gid = opts->gid;
0164 
0165     return 0;
0166 }
0167 
0168 static int debugfs_remount(struct super_block *sb, int *flags, char *data)
0169 {
0170     int err;
0171     struct debugfs_fs_info *fsi = sb->s_fs_info;
0172 
0173     sync_filesystem(sb);
0174     err = debugfs_parse_options(data, &fsi->mount_opts);
0175     if (err)
0176         goto fail;
0177 
0178     debugfs_apply_options(sb);
0179 
0180 fail:
0181     return err;
0182 }
0183 
0184 static int debugfs_show_options(struct seq_file *m, struct dentry *root)
0185 {
0186     struct debugfs_fs_info *fsi = root->d_sb->s_fs_info;
0187     struct debugfs_mount_opts *opts = &fsi->mount_opts;
0188 
0189     if (!uid_eq(opts->uid, GLOBAL_ROOT_UID))
0190         seq_printf(m, ",uid=%u",
0191                from_kuid_munged(&init_user_ns, opts->uid));
0192     if (!gid_eq(opts->gid, GLOBAL_ROOT_GID))
0193         seq_printf(m, ",gid=%u",
0194                from_kgid_munged(&init_user_ns, opts->gid));
0195     if (opts->mode != DEBUGFS_DEFAULT_MODE)
0196         seq_printf(m, ",mode=%o", opts->mode);
0197 
0198     return 0;
0199 }
0200 
0201 static void debugfs_free_inode(struct inode *inode)
0202 {
0203     if (S_ISLNK(inode->i_mode))
0204         kfree(inode->i_link);
0205     free_inode_nonrcu(inode);
0206 }
0207 
0208 static const struct super_operations debugfs_super_operations = {
0209     .statfs     = simple_statfs,
0210     .remount_fs = debugfs_remount,
0211     .show_options   = debugfs_show_options,
0212     .free_inode = debugfs_free_inode,
0213 };
0214 
0215 static void debugfs_release_dentry(struct dentry *dentry)
0216 {
0217     void *fsd = dentry->d_fsdata;
0218 
0219     if (!((unsigned long)fsd & DEBUGFS_FSDATA_IS_REAL_FOPS_BIT))
0220         kfree(dentry->d_fsdata);
0221 }
0222 
0223 static struct vfsmount *debugfs_automount(struct path *path)
0224 {
0225     debugfs_automount_t f;
0226     f = (debugfs_automount_t)path->dentry->d_fsdata;
0227     return f(path->dentry, d_inode(path->dentry)->i_private);
0228 }
0229 
0230 static const struct dentry_operations debugfs_dops = {
0231     .d_delete = always_delete_dentry,
0232     .d_release = debugfs_release_dentry,
0233     .d_automount = debugfs_automount,
0234 };
0235 
0236 static int debug_fill_super(struct super_block *sb, void *data, int silent)
0237 {
0238     static const struct tree_descr debug_files[] = {{""}};
0239     struct debugfs_fs_info *fsi;
0240     int err;
0241 
0242     fsi = kzalloc(sizeof(struct debugfs_fs_info), GFP_KERNEL);
0243     sb->s_fs_info = fsi;
0244     if (!fsi) {
0245         err = -ENOMEM;
0246         goto fail;
0247     }
0248 
0249     err = debugfs_parse_options(data, &fsi->mount_opts);
0250     if (err)
0251         goto fail;
0252 
0253     err  =  simple_fill_super(sb, DEBUGFS_MAGIC, debug_files);
0254     if (err)
0255         goto fail;
0256 
0257     sb->s_op = &debugfs_super_operations;
0258     sb->s_d_op = &debugfs_dops;
0259 
0260     debugfs_apply_options(sb);
0261 
0262     return 0;
0263 
0264 fail:
0265     kfree(fsi);
0266     sb->s_fs_info = NULL;
0267     return err;
0268 }
0269 
0270 static struct dentry *debug_mount(struct file_system_type *fs_type,
0271             int flags, const char *dev_name,
0272             void *data)
0273 {
0274     if (!(debugfs_allow & DEBUGFS_ALLOW_API))
0275         return ERR_PTR(-EPERM);
0276 
0277     return mount_single(fs_type, flags, data, debug_fill_super);
0278 }
0279 
0280 static struct file_system_type debug_fs_type = {
0281     .owner =    THIS_MODULE,
0282     .name =     "debugfs",
0283     .mount =    debug_mount,
0284     .kill_sb =  kill_litter_super,
0285 };
0286 MODULE_ALIAS_FS("debugfs");
0287 
0288 /**
0289  * debugfs_lookup() - look up an existing debugfs file
0290  * @name: a pointer to a string containing the name of the file to look up.
0291  * @parent: a pointer to the parent dentry of the file.
0292  *
0293  * This function will return a pointer to a dentry if it succeeds.  If the file
0294  * doesn't exist or an error occurs, %NULL will be returned.  The returned
0295  * dentry must be passed to dput() when it is no longer needed.
0296  *
0297  * If debugfs is not enabled in the kernel, the value -%ENODEV will be
0298  * returned.
0299  */
0300 struct dentry *debugfs_lookup(const char *name, struct dentry *parent)
0301 {
0302     struct dentry *dentry;
0303 
0304     if (!debugfs_initialized() || IS_ERR_OR_NULL(name) || IS_ERR(parent))
0305         return NULL;
0306 
0307     if (!parent)
0308         parent = debugfs_mount->mnt_root;
0309 
0310     dentry = lookup_positive_unlocked(name, parent, strlen(name));
0311     if (IS_ERR(dentry))
0312         return NULL;
0313     return dentry;
0314 }
0315 EXPORT_SYMBOL_GPL(debugfs_lookup);
0316 
0317 static struct dentry *start_creating(const char *name, struct dentry *parent)
0318 {
0319     struct dentry *dentry;
0320     int error;
0321 
0322     if (!(debugfs_allow & DEBUGFS_ALLOW_API))
0323         return ERR_PTR(-EPERM);
0324 
0325     if (!debugfs_initialized())
0326         return ERR_PTR(-ENOENT);
0327 
0328     pr_debug("creating file '%s'\n", name);
0329 
0330     if (IS_ERR(parent))
0331         return parent;
0332 
0333     error = simple_pin_fs(&debug_fs_type, &debugfs_mount,
0334                   &debugfs_mount_count);
0335     if (error) {
0336         pr_err("Unable to pin filesystem for file '%s'\n", name);
0337         return ERR_PTR(error);
0338     }
0339 
0340     /* If the parent is not specified, we create it in the root.
0341      * We need the root dentry to do this, which is in the super
0342      * block. A pointer to that is in the struct vfsmount that we
0343      * have around.
0344      */
0345     if (!parent)
0346         parent = debugfs_mount->mnt_root;
0347 
0348     inode_lock(d_inode(parent));
0349     if (unlikely(IS_DEADDIR(d_inode(parent))))
0350         dentry = ERR_PTR(-ENOENT);
0351     else
0352         dentry = lookup_one_len(name, parent, strlen(name));
0353     if (!IS_ERR(dentry) && d_really_is_positive(dentry)) {
0354         if (d_is_dir(dentry))
0355             pr_err("Directory '%s' with parent '%s' already present!\n",
0356                    name, parent->d_name.name);
0357         else
0358             pr_err("File '%s' in directory '%s' already present!\n",
0359                    name, parent->d_name.name);
0360         dput(dentry);
0361         dentry = ERR_PTR(-EEXIST);
0362     }
0363 
0364     if (IS_ERR(dentry)) {
0365         inode_unlock(d_inode(parent));
0366         simple_release_fs(&debugfs_mount, &debugfs_mount_count);
0367     }
0368 
0369     return dentry;
0370 }
0371 
0372 static struct dentry *failed_creating(struct dentry *dentry)
0373 {
0374     inode_unlock(d_inode(dentry->d_parent));
0375     dput(dentry);
0376     simple_release_fs(&debugfs_mount, &debugfs_mount_count);
0377     return ERR_PTR(-ENOMEM);
0378 }
0379 
0380 static struct dentry *end_creating(struct dentry *dentry)
0381 {
0382     inode_unlock(d_inode(dentry->d_parent));
0383     return dentry;
0384 }
0385 
0386 static struct dentry *__debugfs_create_file(const char *name, umode_t mode,
0387                 struct dentry *parent, void *data,
0388                 const struct file_operations *proxy_fops,
0389                 const struct file_operations *real_fops)
0390 {
0391     struct dentry *dentry;
0392     struct inode *inode;
0393 
0394     if (!(mode & S_IFMT))
0395         mode |= S_IFREG;
0396     BUG_ON(!S_ISREG(mode));
0397     dentry = start_creating(name, parent);
0398 
0399     if (IS_ERR(dentry))
0400         return dentry;
0401 
0402     if (!(debugfs_allow & DEBUGFS_ALLOW_API)) {
0403         failed_creating(dentry);
0404         return ERR_PTR(-EPERM);
0405     }
0406 
0407     inode = debugfs_get_inode(dentry->d_sb);
0408     if (unlikely(!inode)) {
0409         pr_err("out of free dentries, can not create file '%s'\n",
0410                name);
0411         return failed_creating(dentry);
0412     }
0413 
0414     inode->i_mode = mode;
0415     inode->i_private = data;
0416 
0417     inode->i_op = &debugfs_file_inode_operations;
0418     inode->i_fop = proxy_fops;
0419     dentry->d_fsdata = (void *)((unsigned long)real_fops |
0420                 DEBUGFS_FSDATA_IS_REAL_FOPS_BIT);
0421 
0422     d_instantiate(dentry, inode);
0423     fsnotify_create(d_inode(dentry->d_parent), dentry);
0424     return end_creating(dentry);
0425 }
0426 
0427 /**
0428  * debugfs_create_file - create a file in the debugfs filesystem
0429  * @name: a pointer to a string containing the name of the file to create.
0430  * @mode: the permission that the file should have.
0431  * @parent: a pointer to the parent dentry for this file.  This should be a
0432  *          directory dentry if set.  If this parameter is NULL, then the
0433  *          file will be created in the root of the debugfs filesystem.
0434  * @data: a pointer to something that the caller will want to get to later
0435  *        on.  The inode.i_private pointer will point to this value on
0436  *        the open() call.
0437  * @fops: a pointer to a struct file_operations that should be used for
0438  *        this file.
0439  *
0440  * This is the basic "create a file" function for debugfs.  It allows for a
0441  * wide range of flexibility in creating a file, or a directory (if you want
0442  * to create a directory, the debugfs_create_dir() function is
0443  * recommended to be used instead.)
0444  *
0445  * This function will return a pointer to a dentry if it succeeds.  This
0446  * pointer must be passed to the debugfs_remove() function when the file is
0447  * to be removed (no automatic cleanup happens if your module is unloaded,
0448  * you are responsible here.)  If an error occurs, ERR_PTR(-ERROR) will be
0449  * returned.
0450  *
0451  * If debugfs is not enabled in the kernel, the value -%ENODEV will be
0452  * returned.
0453  *
0454  * NOTE: it's expected that most callers should _ignore_ the errors returned
0455  * by this function. Other debugfs functions handle the fact that the "dentry"
0456  * passed to them could be an error and they don't crash in that case.
0457  * Drivers should generally work fine even if debugfs fails to init anyway.
0458  */
0459 struct dentry *debugfs_create_file(const char *name, umode_t mode,
0460                    struct dentry *parent, void *data,
0461                    const struct file_operations *fops)
0462 {
0463 
0464     return __debugfs_create_file(name, mode, parent, data,
0465                 fops ? &debugfs_full_proxy_file_operations :
0466                     &debugfs_noop_file_operations,
0467                 fops);
0468 }
0469 EXPORT_SYMBOL_GPL(debugfs_create_file);
0470 
0471 /**
0472  * debugfs_create_file_unsafe - create a file in the debugfs filesystem
0473  * @name: a pointer to a string containing the name of the file to create.
0474  * @mode: the permission that the file should have.
0475  * @parent: a pointer to the parent dentry for this file.  This should be a
0476  *          directory dentry if set.  If this parameter is NULL, then the
0477  *          file will be created in the root of the debugfs filesystem.
0478  * @data: a pointer to something that the caller will want to get to later
0479  *        on.  The inode.i_private pointer will point to this value on
0480  *        the open() call.
0481  * @fops: a pointer to a struct file_operations that should be used for
0482  *        this file.
0483  *
0484  * debugfs_create_file_unsafe() is completely analogous to
0485  * debugfs_create_file(), the only difference being that the fops
0486  * handed it will not get protected against file removals by the
0487  * debugfs core.
0488  *
0489  * It is your responsibility to protect your struct file_operation
0490  * methods against file removals by means of debugfs_file_get()
0491  * and debugfs_file_put(). ->open() is still protected by
0492  * debugfs though.
0493  *
0494  * Any struct file_operations defined by means of
0495  * DEFINE_DEBUGFS_ATTRIBUTE() is protected against file removals and
0496  * thus, may be used here.
0497  */
0498 struct dentry *debugfs_create_file_unsafe(const char *name, umode_t mode,
0499                    struct dentry *parent, void *data,
0500                    const struct file_operations *fops)
0501 {
0502 
0503     return __debugfs_create_file(name, mode, parent, data,
0504                 fops ? &debugfs_open_proxy_file_operations :
0505                     &debugfs_noop_file_operations,
0506                 fops);
0507 }
0508 EXPORT_SYMBOL_GPL(debugfs_create_file_unsafe);
0509 
0510 /**
0511  * debugfs_create_file_size - create a file in the debugfs filesystem
0512  * @name: a pointer to a string containing the name of the file to create.
0513  * @mode: the permission that the file should have.
0514  * @parent: a pointer to the parent dentry for this file.  This should be a
0515  *          directory dentry if set.  If this parameter is NULL, then the
0516  *          file will be created in the root of the debugfs filesystem.
0517  * @data: a pointer to something that the caller will want to get to later
0518  *        on.  The inode.i_private pointer will point to this value on
0519  *        the open() call.
0520  * @fops: a pointer to a struct file_operations that should be used for
0521  *        this file.
0522  * @file_size: initial file size
0523  *
0524  * This is the basic "create a file" function for debugfs.  It allows for a
0525  * wide range of flexibility in creating a file, or a directory (if you want
0526  * to create a directory, the debugfs_create_dir() function is
0527  * recommended to be used instead.)
0528  */
0529 void debugfs_create_file_size(const char *name, umode_t mode,
0530                   struct dentry *parent, void *data,
0531                   const struct file_operations *fops,
0532                   loff_t file_size)
0533 {
0534     struct dentry *de = debugfs_create_file(name, mode, parent, data, fops);
0535 
0536     if (!IS_ERR(de))
0537         d_inode(de)->i_size = file_size;
0538 }
0539 EXPORT_SYMBOL_GPL(debugfs_create_file_size);
0540 
0541 /**
0542  * debugfs_create_dir - create a directory in the debugfs filesystem
0543  * @name: a pointer to a string containing the name of the directory to
0544  *        create.
0545  * @parent: a pointer to the parent dentry for this file.  This should be a
0546  *          directory dentry if set.  If this parameter is NULL, then the
0547  *          directory will be created in the root of the debugfs filesystem.
0548  *
0549  * This function creates a directory in debugfs with the given name.
0550  *
0551  * This function will return a pointer to a dentry if it succeeds.  This
0552  * pointer must be passed to the debugfs_remove() function when the file is
0553  * to be removed (no automatic cleanup happens if your module is unloaded,
0554  * you are responsible here.)  If an error occurs, ERR_PTR(-ERROR) will be
0555  * returned.
0556  *
0557  * If debugfs is not enabled in the kernel, the value -%ENODEV will be
0558  * returned.
0559  *
0560  * NOTE: it's expected that most callers should _ignore_ the errors returned
0561  * by this function. Other debugfs functions handle the fact that the "dentry"
0562  * passed to them could be an error and they don't crash in that case.
0563  * Drivers should generally work fine even if debugfs fails to init anyway.
0564  */
0565 struct dentry *debugfs_create_dir(const char *name, struct dentry *parent)
0566 {
0567     struct dentry *dentry = start_creating(name, parent);
0568     struct inode *inode;
0569 
0570     if (IS_ERR(dentry))
0571         return dentry;
0572 
0573     if (!(debugfs_allow & DEBUGFS_ALLOW_API)) {
0574         failed_creating(dentry);
0575         return ERR_PTR(-EPERM);
0576     }
0577 
0578     inode = debugfs_get_inode(dentry->d_sb);
0579     if (unlikely(!inode)) {
0580         pr_err("out of free dentries, can not create directory '%s'\n",
0581                name);
0582         return failed_creating(dentry);
0583     }
0584 
0585     inode->i_mode = S_IFDIR | S_IRWXU | S_IRUGO | S_IXUGO;
0586     inode->i_op = &debugfs_dir_inode_operations;
0587     inode->i_fop = &simple_dir_operations;
0588 
0589     /* directory inodes start off with i_nlink == 2 (for "." entry) */
0590     inc_nlink(inode);
0591     d_instantiate(dentry, inode);
0592     inc_nlink(d_inode(dentry->d_parent));
0593     fsnotify_mkdir(d_inode(dentry->d_parent), dentry);
0594     return end_creating(dentry);
0595 }
0596 EXPORT_SYMBOL_GPL(debugfs_create_dir);
0597 
0598 /**
0599  * debugfs_create_automount - create automount point in the debugfs filesystem
0600  * @name: a pointer to a string containing the name of the file to create.
0601  * @parent: a pointer to the parent dentry for this file.  This should be a
0602  *          directory dentry if set.  If this parameter is NULL, then the
0603  *          file will be created in the root of the debugfs filesystem.
0604  * @f: function to be called when pathname resolution steps on that one.
0605  * @data: opaque argument to pass to f().
0606  *
0607  * @f should return what ->d_automount() would.
0608  */
0609 struct dentry *debugfs_create_automount(const char *name,
0610                     struct dentry *parent,
0611                     debugfs_automount_t f,
0612                     void *data)
0613 {
0614     struct dentry *dentry = start_creating(name, parent);
0615     struct inode *inode;
0616 
0617     if (IS_ERR(dentry))
0618         return dentry;
0619 
0620     if (!(debugfs_allow & DEBUGFS_ALLOW_API)) {
0621         failed_creating(dentry);
0622         return ERR_PTR(-EPERM);
0623     }
0624 
0625     inode = debugfs_get_inode(dentry->d_sb);
0626     if (unlikely(!inode)) {
0627         pr_err("out of free dentries, can not create automount '%s'\n",
0628                name);
0629         return failed_creating(dentry);
0630     }
0631 
0632     make_empty_dir_inode(inode);
0633     inode->i_flags |= S_AUTOMOUNT;
0634     inode->i_private = data;
0635     dentry->d_fsdata = (void *)f;
0636     /* directory inodes start off with i_nlink == 2 (for "." entry) */
0637     inc_nlink(inode);
0638     d_instantiate(dentry, inode);
0639     inc_nlink(d_inode(dentry->d_parent));
0640     fsnotify_mkdir(d_inode(dentry->d_parent), dentry);
0641     return end_creating(dentry);
0642 }
0643 EXPORT_SYMBOL(debugfs_create_automount);
0644 
0645 /**
0646  * debugfs_create_symlink- create a symbolic link in the debugfs filesystem
0647  * @name: a pointer to a string containing the name of the symbolic link to
0648  *        create.
0649  * @parent: a pointer to the parent dentry for this symbolic link.  This
0650  *          should be a directory dentry if set.  If this parameter is NULL,
0651  *          then the symbolic link will be created in the root of the debugfs
0652  *          filesystem.
0653  * @target: a pointer to a string containing the path to the target of the
0654  *          symbolic link.
0655  *
0656  * This function creates a symbolic link with the given name in debugfs that
0657  * links to the given target path.
0658  *
0659  * This function will return a pointer to a dentry if it succeeds.  This
0660  * pointer must be passed to the debugfs_remove() function when the symbolic
0661  * link is to be removed (no automatic cleanup happens if your module is
0662  * unloaded, you are responsible here.)  If an error occurs, ERR_PTR(-ERROR)
0663  * will be returned.
0664  *
0665  * If debugfs is not enabled in the kernel, the value -%ENODEV will be
0666  * returned.
0667  */
0668 struct dentry *debugfs_create_symlink(const char *name, struct dentry *parent,
0669                       const char *target)
0670 {
0671     struct dentry *dentry;
0672     struct inode *inode;
0673     char *link = kstrdup(target, GFP_KERNEL);
0674     if (!link)
0675         return ERR_PTR(-ENOMEM);
0676 
0677     dentry = start_creating(name, parent);
0678     if (IS_ERR(dentry)) {
0679         kfree(link);
0680         return dentry;
0681     }
0682 
0683     inode = debugfs_get_inode(dentry->d_sb);
0684     if (unlikely(!inode)) {
0685         pr_err("out of free dentries, can not create symlink '%s'\n",
0686                name);
0687         kfree(link);
0688         return failed_creating(dentry);
0689     }
0690     inode->i_mode = S_IFLNK | S_IRWXUGO;
0691     inode->i_op = &debugfs_symlink_inode_operations;
0692     inode->i_link = link;
0693     d_instantiate(dentry, inode);
0694     return end_creating(dentry);
0695 }
0696 EXPORT_SYMBOL_GPL(debugfs_create_symlink);
0697 
0698 static void __debugfs_file_removed(struct dentry *dentry)
0699 {
0700     struct debugfs_fsdata *fsd;
0701 
0702     /*
0703      * Paired with the closing smp_mb() implied by a successful
0704      * cmpxchg() in debugfs_file_get(): either
0705      * debugfs_file_get() must see a dead dentry or we must see a
0706      * debugfs_fsdata instance at ->d_fsdata here (or both).
0707      */
0708     smp_mb();
0709     fsd = READ_ONCE(dentry->d_fsdata);
0710     if ((unsigned long)fsd & DEBUGFS_FSDATA_IS_REAL_FOPS_BIT)
0711         return;
0712     if (!refcount_dec_and_test(&fsd->active_users))
0713         wait_for_completion(&fsd->active_users_drained);
0714 }
0715 
0716 static void remove_one(struct dentry *victim)
0717 {
0718         if (d_is_reg(victim))
0719         __debugfs_file_removed(victim);
0720     simple_release_fs(&debugfs_mount, &debugfs_mount_count);
0721 }
0722 
0723 /**
0724  * debugfs_remove - recursively removes a directory
0725  * @dentry: a pointer to a the dentry of the directory to be removed.  If this
0726  *          parameter is NULL or an error value, nothing will be done.
0727  *
0728  * This function recursively removes a directory tree in debugfs that
0729  * was previously created with a call to another debugfs function
0730  * (like debugfs_create_file() or variants thereof.)
0731  *
0732  * This function is required to be called in order for the file to be
0733  * removed, no automatic cleanup of files will happen when a module is
0734  * removed, you are responsible here.
0735  */
0736 void debugfs_remove(struct dentry *dentry)
0737 {
0738     if (IS_ERR_OR_NULL(dentry))
0739         return;
0740 
0741     simple_pin_fs(&debug_fs_type, &debugfs_mount, &debugfs_mount_count);
0742     simple_recursive_removal(dentry, remove_one);
0743     simple_release_fs(&debugfs_mount, &debugfs_mount_count);
0744 }
0745 EXPORT_SYMBOL_GPL(debugfs_remove);
0746 
0747 /**
0748  * debugfs_lookup_and_remove - lookup a directory or file and recursively remove it
0749  * @name: a pointer to a string containing the name of the item to look up.
0750  * @parent: a pointer to the parent dentry of the item.
0751  *
0752  * This is the equlivant of doing something like
0753  * debugfs_remove(debugfs_lookup(..)) but with the proper reference counting
0754  * handled for the directory being looked up.
0755  */
0756 void debugfs_lookup_and_remove(const char *name, struct dentry *parent)
0757 {
0758     struct dentry *dentry;
0759 
0760     dentry = debugfs_lookup(name, parent);
0761     if (!dentry)
0762         return;
0763 
0764     debugfs_remove(dentry);
0765     dput(dentry);
0766 }
0767 EXPORT_SYMBOL_GPL(debugfs_lookup_and_remove);
0768 
0769 /**
0770  * debugfs_rename - rename a file/directory in the debugfs filesystem
0771  * @old_dir: a pointer to the parent dentry for the renamed object. This
0772  *          should be a directory dentry.
0773  * @old_dentry: dentry of an object to be renamed.
0774  * @new_dir: a pointer to the parent dentry where the object should be
0775  *          moved. This should be a directory dentry.
0776  * @new_name: a pointer to a string containing the target name.
0777  *
0778  * This function renames a file/directory in debugfs.  The target must not
0779  * exist for rename to succeed.
0780  *
0781  * This function will return a pointer to old_dentry (which is updated to
0782  * reflect renaming) if it succeeds. If an error occurs, %NULL will be
0783  * returned.
0784  *
0785  * If debugfs is not enabled in the kernel, the value -%ENODEV will be
0786  * returned.
0787  */
0788 struct dentry *debugfs_rename(struct dentry *old_dir, struct dentry *old_dentry,
0789         struct dentry *new_dir, const char *new_name)
0790 {
0791     int error;
0792     struct dentry *dentry = NULL, *trap;
0793     struct name_snapshot old_name;
0794 
0795     if (IS_ERR(old_dir))
0796         return old_dir;
0797     if (IS_ERR(new_dir))
0798         return new_dir;
0799     if (IS_ERR_OR_NULL(old_dentry))
0800         return old_dentry;
0801 
0802     trap = lock_rename(new_dir, old_dir);
0803     /* Source or destination directories don't exist? */
0804     if (d_really_is_negative(old_dir) || d_really_is_negative(new_dir))
0805         goto exit;
0806     /* Source does not exist, cyclic rename, or mountpoint? */
0807     if (d_really_is_negative(old_dentry) || old_dentry == trap ||
0808         d_mountpoint(old_dentry))
0809         goto exit;
0810     dentry = lookup_one_len(new_name, new_dir, strlen(new_name));
0811     /* Lookup failed, cyclic rename or target exists? */
0812     if (IS_ERR(dentry) || dentry == trap || d_really_is_positive(dentry))
0813         goto exit;
0814 
0815     take_dentry_name_snapshot(&old_name, old_dentry);
0816 
0817     error = simple_rename(&init_user_ns, d_inode(old_dir), old_dentry,
0818                   d_inode(new_dir), dentry, 0);
0819     if (error) {
0820         release_dentry_name_snapshot(&old_name);
0821         goto exit;
0822     }
0823     d_move(old_dentry, dentry);
0824     fsnotify_move(d_inode(old_dir), d_inode(new_dir), &old_name.name,
0825         d_is_dir(old_dentry),
0826         NULL, old_dentry);
0827     release_dentry_name_snapshot(&old_name);
0828     unlock_rename(new_dir, old_dir);
0829     dput(dentry);
0830     return old_dentry;
0831 exit:
0832     if (dentry && !IS_ERR(dentry))
0833         dput(dentry);
0834     unlock_rename(new_dir, old_dir);
0835     if (IS_ERR(dentry))
0836         return dentry;
0837     return ERR_PTR(-EINVAL);
0838 }
0839 EXPORT_SYMBOL_GPL(debugfs_rename);
0840 
0841 /**
0842  * debugfs_initialized - Tells whether debugfs has been registered
0843  */
0844 bool debugfs_initialized(void)
0845 {
0846     return debugfs_registered;
0847 }
0848 EXPORT_SYMBOL_GPL(debugfs_initialized);
0849 
0850 static int __init debugfs_kernel(char *str)
0851 {
0852     if (str) {
0853         if (!strcmp(str, "on"))
0854             debugfs_allow = DEBUGFS_ALLOW_API | DEBUGFS_ALLOW_MOUNT;
0855         else if (!strcmp(str, "no-mount"))
0856             debugfs_allow = DEBUGFS_ALLOW_API;
0857         else if (!strcmp(str, "off"))
0858             debugfs_allow = 0;
0859     }
0860 
0861     return 0;
0862 }
0863 early_param("debugfs", debugfs_kernel);
0864 static int __init debugfs_init(void)
0865 {
0866     int retval;
0867 
0868     if (!(debugfs_allow & DEBUGFS_ALLOW_MOUNT))
0869         return -EPERM;
0870 
0871     retval = sysfs_create_mount_point(kernel_kobj, "debug");
0872     if (retval)
0873         return retval;
0874 
0875     retval = register_filesystem(&debug_fs_type);
0876     if (retval)
0877         sysfs_remove_mount_point(kernel_kobj, "debug");
0878     else
0879         debugfs_registered = true;
0880 
0881     return retval;
0882 }
0883 core_initcall(debugfs_init);