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