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