Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
torvalds
GitHub Repository: torvalds/linux
Path: blob/master/fs/debugfs/inode.c
49070 views
1
// SPDX-License-Identifier: GPL-2.0
2
/*
3
* inode.c - part of debugfs, a tiny little debug file system
4
*
5
* Copyright (C) 2004,2019 Greg Kroah-Hartman <[email protected]>
6
* Copyright (C) 2004 IBM Inc.
7
* Copyright (C) 2019 Linux Foundation <[email protected]>
8
*
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
13
#define pr_fmt(fmt) "debugfs: " fmt
14
15
#include <linux/module.h>
16
#include <linux/fs.h>
17
#include <linux/fs_context.h>
18
#include <linux/fs_parser.h>
19
#include <linux/pagemap.h>
20
#include <linux/init.h>
21
#include <linux/kobject.h>
22
#include <linux/namei.h>
23
#include <linux/debugfs.h>
24
#include <linux/fsnotify.h>
25
#include <linux/string.h>
26
#include <linux/seq_file.h>
27
#include <linux/magic.h>
28
#include <linux/slab.h>
29
#include <linux/security.h>
30
31
#include "internal.h"
32
33
#define DEBUGFS_DEFAULT_MODE 0700
34
35
static struct vfsmount *debugfs_mount;
36
static int debugfs_mount_count;
37
static bool debugfs_registered;
38
static bool debugfs_enabled __ro_after_init = IS_ENABLED(CONFIG_DEBUG_FS_ALLOW_ALL);
39
40
/*
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
*/
45
static int debugfs_setattr(struct mnt_idmap *idmap,
46
struct dentry *dentry, struct iattr *ia)
47
{
48
int ret;
49
50
if (ia->ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID)) {
51
ret = security_locked_down(LOCKDOWN_DEBUGFS);
52
if (ret)
53
return ret;
54
}
55
return simple_setattr(&nop_mnt_idmap, dentry, ia);
56
}
57
58
static const struct inode_operations debugfs_file_inode_operations = {
59
.setattr = debugfs_setattr,
60
};
61
static const struct inode_operations debugfs_dir_inode_operations = {
62
.lookup = simple_lookup,
63
.setattr = debugfs_setattr,
64
};
65
static const struct inode_operations debugfs_symlink_inode_operations = {
66
.get_link = simple_get_link,
67
.setattr = debugfs_setattr,
68
};
69
70
static struct inode *debugfs_get_inode(struct super_block *sb)
71
{
72
struct inode *inode = new_inode(sb);
73
if (inode) {
74
inode->i_ino = get_next_ino();
75
simple_inode_init_ts(inode);
76
}
77
return inode;
78
}
79
80
struct debugfs_fs_info {
81
kuid_t uid;
82
kgid_t gid;
83
umode_t mode;
84
/* Opt_* bitfield. */
85
unsigned int opts;
86
};
87
88
enum {
89
Opt_uid,
90
Opt_gid,
91
Opt_mode,
92
Opt_source,
93
};
94
95
static const struct fs_parameter_spec debugfs_param_specs[] = {
96
fsparam_gid ("gid", Opt_gid),
97
fsparam_u32oct ("mode", Opt_mode),
98
fsparam_uid ("uid", Opt_uid),
99
fsparam_string ("source", Opt_source),
100
{}
101
};
102
103
static int debugfs_parse_param(struct fs_context *fc, struct fs_parameter *param)
104
{
105
struct debugfs_fs_info *opts = fc->s_fs_info;
106
struct fs_parse_result result;
107
int opt;
108
109
opt = fs_parse(fc, debugfs_param_specs, param, &result);
110
if (opt < 0) {
111
/*
112
* We might like to report bad mount options here; but
113
* traditionally debugfs has ignored all mount options
114
*/
115
if (opt == -ENOPARAM)
116
return 0;
117
118
return opt;
119
}
120
121
switch (opt) {
122
case Opt_uid:
123
opts->uid = result.uid;
124
break;
125
case Opt_gid:
126
opts->gid = result.gid;
127
break;
128
case Opt_mode:
129
opts->mode = result.uint_32 & S_IALLUGO;
130
break;
131
case Opt_source:
132
if (fc->source)
133
return invalfc(fc, "Multiple sources specified");
134
fc->source = param->string;
135
param->string = NULL;
136
break;
137
/*
138
* We might like to report bad mount options here;
139
* but traditionally debugfs has ignored all mount options
140
*/
141
}
142
143
opts->opts |= BIT(opt);
144
145
return 0;
146
}
147
148
static void _debugfs_apply_options(struct super_block *sb, bool remount)
149
{
150
struct debugfs_fs_info *fsi = sb->s_fs_info;
151
struct inode *inode = d_inode(sb->s_root);
152
153
/*
154
* On remount, only reset mode/uid/gid if they were provided as mount
155
* options.
156
*/
157
158
if (!remount || fsi->opts & BIT(Opt_mode)) {
159
inode->i_mode &= ~S_IALLUGO;
160
inode->i_mode |= fsi->mode;
161
}
162
163
if (!remount || fsi->opts & BIT(Opt_uid))
164
inode->i_uid = fsi->uid;
165
166
if (!remount || fsi->opts & BIT(Opt_gid))
167
inode->i_gid = fsi->gid;
168
}
169
170
static void debugfs_apply_options(struct super_block *sb)
171
{
172
_debugfs_apply_options(sb, false);
173
}
174
175
static void debugfs_apply_options_remount(struct super_block *sb)
176
{
177
_debugfs_apply_options(sb, true);
178
}
179
180
static int debugfs_reconfigure(struct fs_context *fc)
181
{
182
struct super_block *sb = fc->root->d_sb;
183
struct debugfs_fs_info *sb_opts = sb->s_fs_info;
184
struct debugfs_fs_info *new_opts = fc->s_fs_info;
185
186
if (!new_opts)
187
return 0;
188
189
sync_filesystem(sb);
190
191
/* structure copy of new mount options to sb */
192
*sb_opts = *new_opts;
193
debugfs_apply_options_remount(sb);
194
195
return 0;
196
}
197
198
static int debugfs_show_options(struct seq_file *m, struct dentry *root)
199
{
200
struct debugfs_fs_info *fsi = root->d_sb->s_fs_info;
201
202
if (!uid_eq(fsi->uid, GLOBAL_ROOT_UID))
203
seq_printf(m, ",uid=%u",
204
from_kuid_munged(&init_user_ns, fsi->uid));
205
if (!gid_eq(fsi->gid, GLOBAL_ROOT_GID))
206
seq_printf(m, ",gid=%u",
207
from_kgid_munged(&init_user_ns, fsi->gid));
208
if (fsi->mode != DEBUGFS_DEFAULT_MODE)
209
seq_printf(m, ",mode=%o", fsi->mode);
210
211
return 0;
212
}
213
214
static struct kmem_cache *debugfs_inode_cachep __ro_after_init;
215
216
static void init_once(void *foo)
217
{
218
struct debugfs_inode_info *info = foo;
219
inode_init_once(&info->vfs_inode);
220
}
221
222
static struct inode *debugfs_alloc_inode(struct super_block *sb)
223
{
224
struct debugfs_inode_info *info;
225
info = alloc_inode_sb(sb, debugfs_inode_cachep, GFP_KERNEL);
226
if (!info)
227
return NULL;
228
return &info->vfs_inode;
229
}
230
231
static void debugfs_free_inode(struct inode *inode)
232
{
233
if (S_ISLNK(inode->i_mode))
234
kfree(inode->i_link);
235
kmem_cache_free(debugfs_inode_cachep, DEBUGFS_I(inode));
236
}
237
238
static const struct super_operations debugfs_super_operations = {
239
.statfs = simple_statfs,
240
.show_options = debugfs_show_options,
241
.alloc_inode = debugfs_alloc_inode,
242
.free_inode = debugfs_free_inode,
243
};
244
245
static void debugfs_release_dentry(struct dentry *dentry)
246
{
247
struct debugfs_fsdata *fsd = dentry->d_fsdata;
248
249
if (fsd) {
250
WARN_ON(!list_empty(&fsd->cancellations));
251
mutex_destroy(&fsd->cancellations_mtx);
252
}
253
kfree(fsd);
254
}
255
256
static struct vfsmount *debugfs_automount(struct path *path)
257
{
258
struct inode *inode = path->dentry->d_inode;
259
260
return DEBUGFS_I(inode)->automount(path->dentry, inode->i_private);
261
}
262
263
static const struct dentry_operations debugfs_dops = {
264
.d_release = debugfs_release_dentry,
265
.d_automount = debugfs_automount,
266
};
267
268
static int debugfs_fill_super(struct super_block *sb, struct fs_context *fc)
269
{
270
static const struct tree_descr debug_files[] = {{""}};
271
int err;
272
273
err = simple_fill_super(sb, DEBUGFS_MAGIC, debug_files);
274
if (err)
275
return err;
276
277
sb->s_op = &debugfs_super_operations;
278
set_default_d_op(sb, &debugfs_dops);
279
sb->s_d_flags |= DCACHE_DONTCACHE;
280
281
debugfs_apply_options(sb);
282
283
return 0;
284
}
285
286
static int debugfs_get_tree(struct fs_context *fc)
287
{
288
int err;
289
290
err = get_tree_single(fc, debugfs_fill_super);
291
if (err)
292
return err;
293
294
return debugfs_reconfigure(fc);
295
}
296
297
static void debugfs_free_fc(struct fs_context *fc)
298
{
299
kfree(fc->s_fs_info);
300
}
301
302
static const struct fs_context_operations debugfs_context_ops = {
303
.free = debugfs_free_fc,
304
.parse_param = debugfs_parse_param,
305
.get_tree = debugfs_get_tree,
306
.reconfigure = debugfs_reconfigure,
307
};
308
309
static int debugfs_init_fs_context(struct fs_context *fc)
310
{
311
struct debugfs_fs_info *fsi;
312
313
fsi = kzalloc(sizeof(struct debugfs_fs_info), GFP_KERNEL);
314
if (!fsi)
315
return -ENOMEM;
316
317
fsi->mode = DEBUGFS_DEFAULT_MODE;
318
319
fc->s_fs_info = fsi;
320
fc->ops = &debugfs_context_ops;
321
return 0;
322
}
323
324
static struct file_system_type debug_fs_type = {
325
.owner = THIS_MODULE,
326
.name = "debugfs",
327
.init_fs_context = debugfs_init_fs_context,
328
.parameters = debugfs_param_specs,
329
.kill_sb = kill_anon_super,
330
};
331
MODULE_ALIAS_FS("debugfs");
332
333
/**
334
* debugfs_lookup() - look up an existing debugfs file
335
* @name: a pointer to a string containing the name of the file to look up.
336
* @parent: a pointer to the parent dentry of the file.
337
*
338
* This function will return a pointer to a dentry if it succeeds. If the file
339
* doesn't exist or an error occurs, %NULL will be returned. The returned
340
* dentry must be passed to dput() when it is no longer needed.
341
*
342
* If debugfs is not enabled in the kernel, the value -%ENODEV will be
343
* returned.
344
*/
345
struct dentry *debugfs_lookup(const char *name, struct dentry *parent)
346
{
347
struct dentry *dentry;
348
349
if (!debugfs_initialized() || IS_ERR_OR_NULL(name) || IS_ERR(parent))
350
return NULL;
351
352
if (!parent)
353
parent = debugfs_mount->mnt_root;
354
355
dentry = lookup_noperm_positive_unlocked(&QSTR(name), parent);
356
if (IS_ERR(dentry))
357
return NULL;
358
return dentry;
359
}
360
EXPORT_SYMBOL_GPL(debugfs_lookup);
361
362
static struct dentry *debugfs_start_creating(const char *name,
363
struct dentry *parent)
364
{
365
struct dentry *dentry;
366
int error;
367
368
if (!debugfs_enabled)
369
return ERR_PTR(-EPERM);
370
371
if (!debugfs_initialized())
372
return ERR_PTR(-ENOENT);
373
374
pr_debug("creating file '%s'\n", name);
375
376
if (IS_ERR(parent))
377
return parent;
378
379
error = simple_pin_fs(&debug_fs_type, &debugfs_mount,
380
&debugfs_mount_count);
381
if (error) {
382
pr_err("Unable to pin filesystem for file '%s'\n", name);
383
return ERR_PTR(error);
384
}
385
386
/* If the parent is not specified, we create it in the root.
387
* We need the root dentry to do this, which is in the super
388
* block. A pointer to that is in the struct vfsmount that we
389
* have around.
390
*/
391
if (!parent)
392
parent = debugfs_mount->mnt_root;
393
394
dentry = simple_start_creating(parent, name);
395
if (IS_ERR(dentry)) {
396
if (dentry == ERR_PTR(-EEXIST))
397
pr_err("'%s' already exists in '%pd'\n", name, parent);
398
simple_release_fs(&debugfs_mount, &debugfs_mount_count);
399
}
400
return dentry;
401
}
402
403
static struct dentry *debugfs_failed_creating(struct dentry *dentry)
404
{
405
simple_done_creating(dentry);
406
simple_release_fs(&debugfs_mount, &debugfs_mount_count);
407
return ERR_PTR(-ENOMEM);
408
}
409
410
static struct dentry *debugfs_end_creating(struct dentry *dentry)
411
{
412
simple_done_creating(dentry);
413
return dentry; // borrowed
414
}
415
416
static struct dentry *__debugfs_create_file(const char *name, umode_t mode,
417
struct dentry *parent, void *data,
418
const void *aux,
419
const struct file_operations *proxy_fops,
420
const void *real_fops)
421
{
422
struct dentry *dentry;
423
struct inode *inode;
424
425
if (!(mode & S_IFMT))
426
mode |= S_IFREG;
427
BUG_ON(!S_ISREG(mode));
428
dentry = debugfs_start_creating(name, parent);
429
430
if (IS_ERR(dentry))
431
return dentry;
432
433
inode = debugfs_get_inode(dentry->d_sb);
434
if (unlikely(!inode)) {
435
pr_err("out of free dentries, can not create file '%s'\n",
436
name);
437
return debugfs_failed_creating(dentry);
438
}
439
440
inode->i_mode = mode;
441
inode->i_private = data;
442
443
inode->i_op = &debugfs_file_inode_operations;
444
if (!real_fops)
445
proxy_fops = &debugfs_noop_file_operations;
446
inode->i_fop = proxy_fops;
447
DEBUGFS_I(inode)->raw = real_fops;
448
DEBUGFS_I(inode)->aux = (void *)aux;
449
450
d_make_persistent(dentry, inode);
451
fsnotify_create(d_inode(dentry->d_parent), dentry);
452
return debugfs_end_creating(dentry);
453
}
454
455
struct dentry *debugfs_create_file_full(const char *name, umode_t mode,
456
struct dentry *parent, void *data,
457
const void *aux,
458
const struct file_operations *fops)
459
{
460
return __debugfs_create_file(name, mode, parent, data, aux,
461
&debugfs_full_proxy_file_operations,
462
fops);
463
}
464
EXPORT_SYMBOL_GPL(debugfs_create_file_full);
465
466
struct dentry *debugfs_create_file_short(const char *name, umode_t mode,
467
struct dentry *parent, void *data,
468
const void *aux,
469
const struct debugfs_short_fops *fops)
470
{
471
return __debugfs_create_file(name, mode, parent, data, aux,
472
&debugfs_full_short_proxy_file_operations,
473
fops);
474
}
475
EXPORT_SYMBOL_GPL(debugfs_create_file_short);
476
477
/**
478
* debugfs_create_file_unsafe - create a file in the debugfs filesystem
479
* @name: a pointer to a string containing the name of the file to create.
480
* @mode: the permission that the file should have.
481
* @parent: a pointer to the parent dentry for this file. This should be a
482
* directory dentry if set. If this parameter is NULL, then the
483
* file will be created in the root of the debugfs filesystem.
484
* @data: a pointer to something that the caller will want to get to later
485
* on. The inode.i_private pointer will point to this value on
486
* the open() call.
487
* @fops: a pointer to a struct file_operations that should be used for
488
* this file.
489
*
490
* debugfs_create_file_unsafe() is completely analogous to
491
* debugfs_create_file(), the only difference being that the fops
492
* handed it will not get protected against file removals by the
493
* debugfs core.
494
*
495
* It is your responsibility to protect your struct file_operation
496
* methods against file removals by means of debugfs_file_get()
497
* and debugfs_file_put(). ->open() is still protected by
498
* debugfs though.
499
*
500
* Any struct file_operations defined by means of
501
* DEFINE_DEBUGFS_ATTRIBUTE() is protected against file removals and
502
* thus, may be used here.
503
*/
504
struct dentry *debugfs_create_file_unsafe(const char *name, umode_t mode,
505
struct dentry *parent, void *data,
506
const struct file_operations *fops)
507
{
508
509
return __debugfs_create_file(name, mode, parent, data, NULL,
510
&debugfs_open_proxy_file_operations,
511
fops);
512
}
513
EXPORT_SYMBOL_GPL(debugfs_create_file_unsafe);
514
515
/**
516
* debugfs_create_file_size - create a file in the debugfs filesystem
517
* @name: a pointer to a string containing the name of the file to create.
518
* @mode: the permission that the file should have.
519
* @parent: a pointer to the parent dentry for this file. This should be a
520
* directory dentry if set. If this parameter is NULL, then the
521
* file will be created in the root of the debugfs filesystem.
522
* @data: a pointer to something that the caller will want to get to later
523
* on. The inode.i_private pointer will point to this value on
524
* the open() call.
525
* @fops: a pointer to a struct file_operations that should be used for
526
* this file.
527
* @file_size: initial file size
528
*
529
* This is the basic "create a file" function for debugfs. It allows for a
530
* wide range of flexibility in creating a file, or a directory (if you want
531
* to create a directory, the debugfs_create_dir() function is
532
* recommended to be used instead.)
533
*/
534
void debugfs_create_file_size(const char *name, umode_t mode,
535
struct dentry *parent, void *data,
536
const struct file_operations *fops,
537
loff_t file_size)
538
{
539
struct dentry *de = debugfs_create_file(name, mode, parent, data, fops);
540
541
if (!IS_ERR(de))
542
d_inode(de)->i_size = file_size;
543
}
544
EXPORT_SYMBOL_GPL(debugfs_create_file_size);
545
546
/**
547
* debugfs_create_dir - create a directory in the debugfs filesystem
548
* @name: a pointer to a string containing the name of the directory to
549
* create.
550
* @parent: a pointer to the parent dentry for this file. This should be a
551
* directory dentry if set. If this parameter is NULL, then the
552
* directory will be created in the root of the debugfs filesystem.
553
*
554
* This function creates a directory in debugfs with the given name.
555
*
556
* This function will return a pointer to a dentry if it succeeds. This
557
* pointer must be passed to the debugfs_remove() function when the file is
558
* to be removed (no automatic cleanup happens if your module is unloaded,
559
* you are responsible here.) If an error occurs, ERR_PTR(-ERROR) will be
560
* returned.
561
*
562
* If debugfs is not enabled in the kernel, the value -%ENODEV will be
563
* returned.
564
*
565
* NOTE: it's expected that most callers should _ignore_ the errors returned
566
* by this function. Other debugfs functions handle the fact that the "dentry"
567
* passed to them could be an error and they don't crash in that case.
568
* Drivers should generally work fine even if debugfs fails to init anyway.
569
*/
570
struct dentry *debugfs_create_dir(const char *name, struct dentry *parent)
571
{
572
struct dentry *dentry = debugfs_start_creating(name, parent);
573
struct inode *inode;
574
575
if (IS_ERR(dentry))
576
return dentry;
577
578
inode = debugfs_get_inode(dentry->d_sb);
579
if (unlikely(!inode)) {
580
pr_err("out of free dentries, can not create directory '%s'\n",
581
name);
582
return debugfs_failed_creating(dentry);
583
}
584
585
inode->i_mode = S_IFDIR | S_IRWXU | S_IRUGO | S_IXUGO;
586
inode->i_op = &debugfs_dir_inode_operations;
587
inode->i_fop = &simple_dir_operations;
588
589
/* directory inodes start off with i_nlink == 2 (for "." entry) */
590
inc_nlink(inode);
591
d_make_persistent(dentry, inode);
592
inc_nlink(d_inode(dentry->d_parent));
593
fsnotify_mkdir(d_inode(dentry->d_parent), dentry);
594
return debugfs_end_creating(dentry);
595
}
596
EXPORT_SYMBOL_GPL(debugfs_create_dir);
597
598
/**
599
* debugfs_create_automount - create automount point in the debugfs filesystem
600
* @name: a pointer to a string containing the name of the file to create.
601
* @parent: a pointer to the parent dentry for this file. This should be a
602
* directory dentry if set. If this parameter is NULL, then the
603
* file will be created in the root of the debugfs filesystem.
604
* @f: function to be called when pathname resolution steps on that one.
605
* @data: opaque argument to pass to f().
606
*
607
* @f should return what ->d_automount() would.
608
*/
609
struct dentry *debugfs_create_automount(const char *name,
610
struct dentry *parent,
611
debugfs_automount_t f,
612
void *data)
613
{
614
struct dentry *dentry = debugfs_start_creating(name, parent);
615
struct inode *inode;
616
617
if (IS_ERR(dentry))
618
return dentry;
619
620
inode = debugfs_get_inode(dentry->d_sb);
621
if (unlikely(!inode)) {
622
pr_err("out of free dentries, can not create automount '%s'\n",
623
name);
624
return debugfs_failed_creating(dentry);
625
}
626
627
make_empty_dir_inode(inode);
628
inode->i_flags |= S_AUTOMOUNT;
629
inode->i_private = data;
630
DEBUGFS_I(inode)->automount = f;
631
/* directory inodes start off with i_nlink == 2 (for "." entry) */
632
inc_nlink(inode);
633
d_make_persistent(dentry, inode);
634
inc_nlink(d_inode(dentry->d_parent));
635
fsnotify_mkdir(d_inode(dentry->d_parent), dentry);
636
return debugfs_end_creating(dentry);
637
}
638
EXPORT_SYMBOL(debugfs_create_automount);
639
640
/**
641
* debugfs_create_symlink- create a symbolic link in the debugfs filesystem
642
* @name: a pointer to a string containing the name of the symbolic link to
643
* create.
644
* @parent: a pointer to the parent dentry for this symbolic link. This
645
* should be a directory dentry if set. If this parameter is NULL,
646
* then the symbolic link will be created in the root of the debugfs
647
* filesystem.
648
* @target: a pointer to a string containing the path to the target of the
649
* symbolic link.
650
*
651
* This function creates a symbolic link with the given name in debugfs that
652
* links to the given target path.
653
*
654
* This function will return a pointer to a dentry if it succeeds. This
655
* pointer must be passed to the debugfs_remove() function when the symbolic
656
* link is to be removed (no automatic cleanup happens if your module is
657
* unloaded, you are responsible here.) If an error occurs, ERR_PTR(-ERROR)
658
* will be returned.
659
*
660
* If debugfs is not enabled in the kernel, the value -%ENODEV will be
661
* returned.
662
*/
663
struct dentry *debugfs_create_symlink(const char *name, struct dentry *parent,
664
const char *target)
665
{
666
struct dentry *dentry;
667
struct inode *inode;
668
char *link = kstrdup(target, GFP_KERNEL);
669
if (!link)
670
return ERR_PTR(-ENOMEM);
671
672
dentry = debugfs_start_creating(name, parent);
673
if (IS_ERR(dentry)) {
674
kfree(link);
675
return dentry;
676
}
677
678
inode = debugfs_get_inode(dentry->d_sb);
679
if (unlikely(!inode)) {
680
pr_err("out of free dentries, can not create symlink '%s'\n",
681
name);
682
kfree(link);
683
return debugfs_failed_creating(dentry);
684
}
685
inode->i_mode = S_IFLNK | S_IRWXUGO;
686
inode->i_op = &debugfs_symlink_inode_operations;
687
inode->i_link = link;
688
d_make_persistent(dentry, inode);
689
return debugfs_end_creating(dentry);
690
}
691
EXPORT_SYMBOL_GPL(debugfs_create_symlink);
692
693
static void __debugfs_file_removed(struct dentry *dentry)
694
{
695
struct debugfs_fsdata *fsd;
696
697
/*
698
* Paired with the closing smp_mb() implied by a successful
699
* cmpxchg() in debugfs_file_get(): either
700
* debugfs_file_get() must see a dead dentry or we must see a
701
* debugfs_fsdata instance at ->d_fsdata here (or both).
702
*/
703
smp_mb();
704
fsd = READ_ONCE(dentry->d_fsdata);
705
if (!fsd)
706
return;
707
708
/* if this was the last reference, we're done */
709
if (refcount_dec_and_test(&fsd->active_users))
710
return;
711
712
/*
713
* If there's still a reference, the code that obtained it can
714
* be in different states:
715
* - The common case of not using cancellations, or already
716
* after debugfs_leave_cancellation(), where we just need
717
* to wait for debugfs_file_put() which signals the completion;
718
* - inside a cancellation section, i.e. between
719
* debugfs_enter_cancellation() and debugfs_leave_cancellation(),
720
* in which case we need to trigger the ->cancel() function,
721
* and then wait for debugfs_file_put() just like in the
722
* previous case;
723
* - before debugfs_enter_cancellation() (but obviously after
724
* debugfs_file_get()), in which case we may not see the
725
* cancellation in the list on the first round of the loop,
726
* but debugfs_enter_cancellation() signals the completion
727
* after adding it, so this code gets woken up to call the
728
* ->cancel() function.
729
*/
730
while (refcount_read(&fsd->active_users)) {
731
struct debugfs_cancellation *c;
732
733
/*
734
* Lock the cancellations. Note that the cancellations
735
* structs are meant to be on the stack, so we need to
736
* ensure we either use them here or don't touch them,
737
* and debugfs_leave_cancellation() will wait for this
738
* to be finished processing before exiting one. It may
739
* of course win and remove the cancellation, but then
740
* chances are we never even got into this bit, we only
741
* do if the refcount isn't zero already.
742
*/
743
mutex_lock(&fsd->cancellations_mtx);
744
while ((c = list_first_entry_or_null(&fsd->cancellations,
745
typeof(*c), list))) {
746
list_del_init(&c->list);
747
c->cancel(dentry, c->cancel_data);
748
}
749
mutex_unlock(&fsd->cancellations_mtx);
750
751
wait_for_completion(&fsd->active_users_drained);
752
}
753
}
754
755
static void remove_one(struct dentry *victim)
756
{
757
if (d_is_reg(victim))
758
__debugfs_file_removed(victim);
759
simple_release_fs(&debugfs_mount, &debugfs_mount_count);
760
}
761
762
/**
763
* debugfs_remove - recursively removes a directory
764
* @dentry: a pointer to a the dentry of the directory to be removed. If this
765
* parameter is NULL or an error value, nothing will be done.
766
*
767
* This function recursively removes a directory tree in debugfs that
768
* was previously created with a call to another debugfs function
769
* (like debugfs_create_file() or variants thereof.)
770
*
771
* This function is required to be called in order for the file to be
772
* removed, no automatic cleanup of files will happen when a module is
773
* removed, you are responsible here.
774
*/
775
void debugfs_remove(struct dentry *dentry)
776
{
777
if (IS_ERR_OR_NULL(dentry))
778
return;
779
780
simple_pin_fs(&debug_fs_type, &debugfs_mount, &debugfs_mount_count);
781
simple_recursive_removal(dentry, remove_one);
782
simple_release_fs(&debugfs_mount, &debugfs_mount_count);
783
}
784
EXPORT_SYMBOL_GPL(debugfs_remove);
785
786
/**
787
* debugfs_lookup_and_remove - lookup a directory or file and recursively remove it
788
* @name: a pointer to a string containing the name of the item to look up.
789
* @parent: a pointer to the parent dentry of the item.
790
*
791
* This is the equlivant of doing something like
792
* debugfs_remove(debugfs_lookup(..)) but with the proper reference counting
793
* handled for the directory being looked up.
794
*/
795
void debugfs_lookup_and_remove(const char *name, struct dentry *parent)
796
{
797
struct dentry *dentry;
798
799
dentry = debugfs_lookup(name, parent);
800
if (!dentry)
801
return;
802
803
debugfs_remove(dentry);
804
dput(dentry);
805
}
806
EXPORT_SYMBOL_GPL(debugfs_lookup_and_remove);
807
808
/**
809
* debugfs_change_name - rename a file/directory in the debugfs filesystem
810
* @dentry: dentry of an object to be renamed.
811
* @fmt: format for new name
812
*
813
* This function renames a file/directory in debugfs. The target must not
814
* exist for rename to succeed.
815
*
816
* This function will return 0 on success and -E... on failure.
817
*
818
* If debugfs is not enabled in the kernel, the value -%ENODEV will be
819
* returned.
820
*/
821
int __printf(2, 3) debugfs_change_name(struct dentry *dentry, const char *fmt, ...)
822
{
823
int error = 0;
824
const char *new_name;
825
struct name_snapshot old_name;
826
struct dentry *target;
827
struct renamedata rd = {};
828
struct inode *dir;
829
va_list ap;
830
831
if (IS_ERR_OR_NULL(dentry))
832
return 0;
833
834
va_start(ap, fmt);
835
new_name = kvasprintf_const(GFP_KERNEL, fmt, ap);
836
va_end(ap);
837
if (!new_name)
838
return -ENOMEM;
839
840
rd.old_parent = dget_parent(dentry);
841
rd.new_parent = rd.old_parent;
842
rd.flags = RENAME_NOREPLACE;
843
target = lookup_noperm_unlocked(&QSTR(new_name), rd.new_parent);
844
if (IS_ERR(target)) {
845
error = PTR_ERR(target);
846
goto out_free;
847
}
848
849
error = start_renaming_two_dentries(&rd, dentry, target);
850
if (error) {
851
if (error == -EEXIST && target == dentry)
852
/* it isn't an error to rename a thing to itself */
853
error = 0;
854
goto out;
855
}
856
857
dir = d_inode(rd.old_parent);
858
take_dentry_name_snapshot(&old_name, dentry);
859
simple_rename_timestamp(dir, dentry, dir, rd.new_dentry);
860
d_move(dentry, rd.new_dentry);
861
fsnotify_move(dir, dir, &old_name.name, d_is_dir(dentry), NULL, dentry);
862
release_dentry_name_snapshot(&old_name);
863
end_renaming(&rd);
864
out:
865
dput(rd.old_parent);
866
dput(target);
867
out_free:
868
kfree_const(new_name);
869
return error;
870
}
871
EXPORT_SYMBOL_GPL(debugfs_change_name);
872
873
/**
874
* debugfs_initialized - Tells whether debugfs has been registered
875
*/
876
bool debugfs_initialized(void)
877
{
878
return debugfs_registered;
879
}
880
EXPORT_SYMBOL_GPL(debugfs_initialized);
881
882
static int __init debugfs_kernel(char *str)
883
{
884
if (str) {
885
if (!strcmp(str, "on"))
886
debugfs_enabled = true;
887
else if (!strcmp(str, "off"))
888
debugfs_enabled = false;
889
else if (!strcmp(str, "no-mount")) {
890
pr_notice("debugfs=no-mount is a deprecated alias "
891
"for debugfs=off\n");
892
debugfs_enabled = false;
893
}
894
}
895
896
return 0;
897
}
898
early_param("debugfs", debugfs_kernel);
899
900
static int __init debugfs_init(void)
901
{
902
int retval;
903
904
if (!debugfs_enabled)
905
return -EPERM;
906
907
retval = sysfs_create_mount_point(kernel_kobj, "debug");
908
if (retval)
909
return retval;
910
911
debugfs_inode_cachep = kmem_cache_create("debugfs_inode_cache",
912
sizeof(struct debugfs_inode_info), 0,
913
SLAB_RECLAIM_ACCOUNT | SLAB_ACCOUNT,
914
init_once);
915
if (debugfs_inode_cachep == NULL) {
916
sysfs_remove_mount_point(kernel_kobj, "debug");
917
return -ENOMEM;
918
}
919
920
retval = register_filesystem(&debug_fs_type);
921
if (retval) { // Really not going to happen
922
sysfs_remove_mount_point(kernel_kobj, "debug");
923
kmem_cache_destroy(debugfs_inode_cachep);
924
return retval;
925
}
926
debugfs_registered = true;
927
return 0;
928
}
929
core_initcall(debugfs_init);
930
931