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