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