1
2
3
4
5
6
7
8
9
10
11
12
13#define pr_fmt(fmt) "debugfs: " fmt
14
15#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>
29
30#include "internal.h"
31
32#define DEBUGFS_DEFAULT_MODE 0700
33
34static struct vfsmount *debugfs_mount;
35static int debugfs_mount_count;
36static bool debugfs_registered;
37
38static struct inode *debugfs_get_inode(struct super_block *sb)
39{
40 struct inode *inode = new_inode(sb);
41 if (inode) {
42 inode->i_ino = get_next_ino();
43 inode->i_atime = inode->i_mtime =
44 inode->i_ctime = current_time(inode);
45 }
46 return inode;
47}
48
49struct debugfs_mount_opts {
50 kuid_t uid;
51 kgid_t gid;
52 umode_t mode;
53};
54
55enum {
56 Opt_uid,
57 Opt_gid,
58 Opt_mode,
59 Opt_err
60};
61
62static const match_table_t tokens = {
63 {Opt_uid, "uid=%u"},
64 {Opt_gid, "gid=%u"},
65 {Opt_mode, "mode=%o"},
66 {Opt_err, NULL}
67};
68
69struct debugfs_fs_info {
70 struct debugfs_mount_opts mount_opts;
71};
72
73static int debugfs_parse_options(char *data, struct debugfs_mount_opts *opts)
74{
75 substring_t args[MAX_OPT_ARGS];
76 int option;
77 int token;
78 kuid_t uid;
79 kgid_t gid;
80 char *p;
81
82 opts->mode = DEBUGFS_DEFAULT_MODE;
83
84 while ((p = strsep(&data, ",")) != NULL) {
85 if (!*p)
86 continue;
87
88 token = match_token(p, tokens, args);
89 switch (token) {
90 case Opt_uid:
91 if (match_int(&args[0], &option))
92 return -EINVAL;
93 uid = make_kuid(current_user_ns(), option);
94 if (!uid_valid(uid))
95 return -EINVAL;
96 opts->uid = uid;
97 break;
98 case Opt_gid:
99 if (match_int(&args[0], &option))
100 return -EINVAL;
101 gid = make_kgid(current_user_ns(), option);
102 if (!gid_valid(gid))
103 return -EINVAL;
104 opts->gid = gid;
105 break;
106 case Opt_mode:
107 if (match_octal(&args[0], &option))
108 return -EINVAL;
109 opts->mode = option & S_IALLUGO;
110 break;
111
112
113
114
115 }
116 }
117
118 return 0;
119}
120
121static int debugfs_apply_options(struct super_block *sb)
122{
123 struct debugfs_fs_info *fsi = sb->s_fs_info;
124 struct inode *inode = d_inode(sb->s_root);
125 struct debugfs_mount_opts *opts = &fsi->mount_opts;
126
127 inode->i_mode &= ~S_IALLUGO;
128 inode->i_mode |= opts->mode;
129
130 inode->i_uid = opts->uid;
131 inode->i_gid = opts->gid;
132
133 return 0;
134}
135
136static int debugfs_remount(struct super_block *sb, int *flags, char *data)
137{
138 int err;
139 struct debugfs_fs_info *fsi = sb->s_fs_info;
140
141 sync_filesystem(sb);
142 err = debugfs_parse_options(data, &fsi->mount_opts);
143 if (err)
144 goto fail;
145
146 debugfs_apply_options(sb);
147
148fail:
149 return err;
150}
151
152static int debugfs_show_options(struct seq_file *m, struct dentry *root)
153{
154 struct debugfs_fs_info *fsi = root->d_sb->s_fs_info;
155 struct debugfs_mount_opts *opts = &fsi->mount_opts;
156
157 if (!uid_eq(opts->uid, GLOBAL_ROOT_UID))
158 seq_printf(m, ",uid=%u",
159 from_kuid_munged(&init_user_ns, opts->uid));
160 if (!gid_eq(opts->gid, GLOBAL_ROOT_GID))
161 seq_printf(m, ",gid=%u",
162 from_kgid_munged(&init_user_ns, opts->gid));
163 if (opts->mode != DEBUGFS_DEFAULT_MODE)
164 seq_printf(m, ",mode=%o", opts->mode);
165
166 return 0;
167}
168
169static void debugfs_free_inode(struct inode *inode)
170{
171 if (S_ISLNK(inode->i_mode))
172 kfree(inode->i_link);
173 free_inode_nonrcu(inode);
174}
175
176static const struct super_operations debugfs_super_operations = {
177 .statfs = simple_statfs,
178 .remount_fs = debugfs_remount,
179 .show_options = debugfs_show_options,
180 .free_inode = debugfs_free_inode,
181};
182
183static void debugfs_release_dentry(struct dentry *dentry)
184{
185 void *fsd = dentry->d_fsdata;
186
187 if (!((unsigned long)fsd & DEBUGFS_FSDATA_IS_REAL_FOPS_BIT))
188 kfree(dentry->d_fsdata);
189}
190
191static struct vfsmount *debugfs_automount(struct path *path)
192{
193 debugfs_automount_t f;
194 f = (debugfs_automount_t)path->dentry->d_fsdata;
195 return f(path->dentry, d_inode(path->dentry)->i_private);
196}
197
198static const struct dentry_operations debugfs_dops = {
199 .d_delete = always_delete_dentry,
200 .d_release = debugfs_release_dentry,
201 .d_automount = debugfs_automount,
202};
203
204static int debug_fill_super(struct super_block *sb, void *data, int silent)
205{
206 static const struct tree_descr debug_files[] = {{""}};
207 struct debugfs_fs_info *fsi;
208 int err;
209
210 fsi = kzalloc(sizeof(struct debugfs_fs_info), GFP_KERNEL);
211 sb->s_fs_info = fsi;
212 if (!fsi) {
213 err = -ENOMEM;
214 goto fail;
215 }
216
217 err = debugfs_parse_options(data, &fsi->mount_opts);
218 if (err)
219 goto fail;
220
221 err = simple_fill_super(sb, DEBUGFS_MAGIC, debug_files);
222 if (err)
223 goto fail;
224
225 sb->s_op = &debugfs_super_operations;
226 sb->s_d_op = &debugfs_dops;
227
228 debugfs_apply_options(sb);
229
230 return 0;
231
232fail:
233 kfree(fsi);
234 sb->s_fs_info = NULL;
235 return err;
236}
237
238static struct dentry *debug_mount(struct file_system_type *fs_type,
239 int flags, const char *dev_name,
240 void *data)
241{
242 return mount_single(fs_type, flags, data, debug_fill_super);
243}
244
245static struct file_system_type debug_fs_type = {
246 .owner = THIS_MODULE,
247 .name = "debugfs",
248 .mount = debug_mount,
249 .kill_sb = kill_litter_super,
250};
251MODULE_ALIAS_FS("debugfs");
252
253
254
255
256
257
258
259
260
261
262
263
264
265struct dentry *debugfs_lookup(const char *name, struct dentry *parent)
266{
267 struct dentry *dentry;
268
269 if (IS_ERR(parent))
270 return NULL;
271
272 if (!parent)
273 parent = debugfs_mount->mnt_root;
274
275 dentry = lookup_one_len_unlocked(name, parent, strlen(name));
276 if (IS_ERR(dentry))
277 return NULL;
278 if (!d_really_is_positive(dentry)) {
279 dput(dentry);
280 return NULL;
281 }
282 return dentry;
283}
284EXPORT_SYMBOL_GPL(debugfs_lookup);
285
286static struct dentry *start_creating(const char *name, struct dentry *parent)
287{
288 struct dentry *dentry;
289 int error;
290
291 pr_debug("creating file '%s'\n", name);
292
293 if (IS_ERR(parent))
294 return parent;
295
296 error = simple_pin_fs(&debug_fs_type, &debugfs_mount,
297 &debugfs_mount_count);
298 if (error) {
299 pr_err("Unable to pin filesystem for file '%s'\n", name);
300 return ERR_PTR(error);
301 }
302
303
304
305
306
307
308 if (!parent)
309 parent = debugfs_mount->mnt_root;
310
311 inode_lock(d_inode(parent));
312 dentry = lookup_one_len(name, parent, strlen(name));
313 if (!IS_ERR(dentry) && d_really_is_positive(dentry)) {
314 if (d_is_dir(dentry))
315 pr_err("Directory '%s' with parent '%s' already present!\n",
316 name, parent->d_name.name);
317 else
318 pr_err("File '%s' in directory '%s' already present!\n",
319 name, parent->d_name.name);
320 dput(dentry);
321 dentry = ERR_PTR(-EEXIST);
322 }
323
324 if (IS_ERR(dentry)) {
325 inode_unlock(d_inode(parent));
326 simple_release_fs(&debugfs_mount, &debugfs_mount_count);
327 }
328
329 return dentry;
330}
331
332static struct dentry *failed_creating(struct dentry *dentry)
333{
334 inode_unlock(d_inode(dentry->d_parent));
335 dput(dentry);
336 simple_release_fs(&debugfs_mount, &debugfs_mount_count);
337 return ERR_PTR(-ENOMEM);
338}
339
340static struct dentry *end_creating(struct dentry *dentry)
341{
342 inode_unlock(d_inode(dentry->d_parent));
343 return dentry;
344}
345
346static struct dentry *__debugfs_create_file(const char *name, umode_t mode,
347 struct dentry *parent, void *data,
348 const struct file_operations *proxy_fops,
349 const struct file_operations *real_fops)
350{
351 struct dentry *dentry;
352 struct inode *inode;
353
354 if (!(mode & S_IFMT))
355 mode |= S_IFREG;
356 BUG_ON(!S_ISREG(mode));
357 dentry = start_creating(name, parent);
358
359 if (IS_ERR(dentry))
360 return dentry;
361
362 inode = debugfs_get_inode(dentry->d_sb);
363 if (unlikely(!inode)) {
364 pr_err("out of free dentries, can not create file '%s'\n",
365 name);
366 return failed_creating(dentry);
367 }
368
369 inode->i_mode = mode;
370 inode->i_private = data;
371
372 inode->i_fop = proxy_fops;
373 dentry->d_fsdata = (void *)((unsigned long)real_fops |
374 DEBUGFS_FSDATA_IS_REAL_FOPS_BIT);
375
376 d_instantiate(dentry, inode);
377 fsnotify_create(d_inode(dentry->d_parent), dentry);
378 return end_creating(dentry);
379}
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408struct dentry *debugfs_create_file(const char *name, umode_t mode,
409 struct dentry *parent, void *data,
410 const struct file_operations *fops)
411{
412
413 return __debugfs_create_file(name, mode, parent, data,
414 fops ? &debugfs_full_proxy_file_operations :
415 &debugfs_noop_file_operations,
416 fops);
417}
418EXPORT_SYMBOL_GPL(debugfs_create_file);
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447struct dentry *debugfs_create_file_unsafe(const char *name, umode_t mode,
448 struct dentry *parent, void *data,
449 const struct file_operations *fops)
450{
451
452 return __debugfs_create_file(name, mode, parent, data,
453 fops ? &debugfs_open_proxy_file_operations :
454 &debugfs_noop_file_operations,
455 fops);
456}
457EXPORT_SYMBOL_GPL(debugfs_create_file_unsafe);
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487struct dentry *debugfs_create_file_size(const char *name, umode_t mode,
488 struct dentry *parent, void *data,
489 const struct file_operations *fops,
490 loff_t file_size)
491{
492 struct dentry *de = debugfs_create_file(name, mode, parent, data, fops);
493
494 if (de)
495 d_inode(de)->i_size = file_size;
496 return de;
497}
498EXPORT_SYMBOL_GPL(debugfs_create_file_size);
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519struct dentry *debugfs_create_dir(const char *name, struct dentry *parent)
520{
521 struct dentry *dentry = start_creating(name, parent);
522 struct inode *inode;
523
524 if (IS_ERR(dentry))
525 return dentry;
526
527 inode = debugfs_get_inode(dentry->d_sb);
528 if (unlikely(!inode)) {
529 pr_err("out of free dentries, can not create directory '%s'\n",
530 name);
531 return failed_creating(dentry);
532 }
533
534 inode->i_mode = S_IFDIR | S_IRWXU | S_IRUGO | S_IXUGO;
535 inode->i_op = &simple_dir_inode_operations;
536 inode->i_fop = &simple_dir_operations;
537
538
539 inc_nlink(inode);
540 d_instantiate(dentry, inode);
541 inc_nlink(d_inode(dentry->d_parent));
542 fsnotify_mkdir(d_inode(dentry->d_parent), dentry);
543 return end_creating(dentry);
544}
545EXPORT_SYMBOL_GPL(debugfs_create_dir);
546
547
548
549
550
551
552
553
554
555
556
557
558struct dentry *debugfs_create_automount(const char *name,
559 struct dentry *parent,
560 debugfs_automount_t f,
561 void *data)
562{
563 struct dentry *dentry = start_creating(name, parent);
564 struct inode *inode;
565
566 if (IS_ERR(dentry))
567 return dentry;
568
569 inode = debugfs_get_inode(dentry->d_sb);
570 if (unlikely(!inode)) {
571 pr_err("out of free dentries, can not create automount '%s'\n",
572 name);
573 return failed_creating(dentry);
574 }
575
576 make_empty_dir_inode(inode);
577 inode->i_flags |= S_AUTOMOUNT;
578 inode->i_private = data;
579 dentry->d_fsdata = (void *)f;
580
581 inc_nlink(inode);
582 d_instantiate(dentry, inode);
583 inc_nlink(d_inode(dentry->d_parent));
584 fsnotify_mkdir(d_inode(dentry->d_parent), dentry);
585 return end_creating(dentry);
586}
587EXPORT_SYMBOL(debugfs_create_automount);
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612struct dentry *debugfs_create_symlink(const char *name, struct dentry *parent,
613 const char *target)
614{
615 struct dentry *dentry;
616 struct inode *inode;
617 char *link = kstrdup(target, GFP_KERNEL);
618 if (!link)
619 return ERR_PTR(-ENOMEM);
620
621 dentry = start_creating(name, parent);
622 if (IS_ERR(dentry)) {
623 kfree(link);
624 return dentry;
625 }
626
627 inode = debugfs_get_inode(dentry->d_sb);
628 if (unlikely(!inode)) {
629 pr_err("out of free dentries, can not create symlink '%s'\n",
630 name);
631 kfree(link);
632 return failed_creating(dentry);
633 }
634 inode->i_mode = S_IFLNK | S_IRWXUGO;
635 inode->i_op = &simple_symlink_inode_operations;
636 inode->i_link = link;
637 d_instantiate(dentry, inode);
638 return end_creating(dentry);
639}
640EXPORT_SYMBOL_GPL(debugfs_create_symlink);
641
642static void __debugfs_file_removed(struct dentry *dentry)
643{
644 struct debugfs_fsdata *fsd;
645
646
647
648
649
650
651
652 smp_mb();
653 fsd = READ_ONCE(dentry->d_fsdata);
654 if ((unsigned long)fsd & DEBUGFS_FSDATA_IS_REAL_FOPS_BIT)
655 return;
656 if (!refcount_dec_and_test(&fsd->active_users))
657 wait_for_completion(&fsd->active_users_drained);
658}
659
660static int __debugfs_remove(struct dentry *dentry, struct dentry *parent)
661{
662 int ret = 0;
663
664 if (simple_positive(dentry)) {
665 dget(dentry);
666 if (d_is_dir(dentry)) {
667 ret = simple_rmdir(d_inode(parent), dentry);
668 if (!ret)
669 fsnotify_rmdir(d_inode(parent), dentry);
670 } else {
671 simple_unlink(d_inode(parent), dentry);
672 fsnotify_unlink(d_inode(parent), dentry);
673 }
674 if (!ret)
675 d_delete(dentry);
676 if (d_is_reg(dentry))
677 __debugfs_file_removed(dentry);
678 dput(dentry);
679 }
680 return ret;
681}
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697void debugfs_remove(struct dentry *dentry)
698{
699 struct dentry *parent;
700 int ret;
701
702 if (IS_ERR_OR_NULL(dentry))
703 return;
704
705 parent = dentry->d_parent;
706 inode_lock(d_inode(parent));
707 ret = __debugfs_remove(dentry, parent);
708 inode_unlock(d_inode(parent));
709 if (!ret)
710 simple_release_fs(&debugfs_mount, &debugfs_mount_count);
711}
712EXPORT_SYMBOL_GPL(debugfs_remove);
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727void debugfs_remove_recursive(struct dentry *dentry)
728{
729 struct dentry *child, *parent;
730
731 if (IS_ERR_OR_NULL(dentry))
732 return;
733
734 parent = dentry;
735 down:
736 inode_lock(d_inode(parent));
737 loop:
738
739
740
741
742
743 spin_lock(&parent->d_lock);
744 list_for_each_entry(child, &parent->d_subdirs, d_child) {
745 if (!simple_positive(child))
746 continue;
747
748
749 if (!list_empty(&child->d_subdirs)) {
750 spin_unlock(&parent->d_lock);
751 inode_unlock(d_inode(parent));
752 parent = child;
753 goto down;
754 }
755
756 spin_unlock(&parent->d_lock);
757
758 if (!__debugfs_remove(child, parent))
759 simple_release_fs(&debugfs_mount, &debugfs_mount_count);
760
761
762
763
764
765
766
767
768 goto loop;
769 }
770 spin_unlock(&parent->d_lock);
771
772 inode_unlock(d_inode(parent));
773 child = parent;
774 parent = parent->d_parent;
775 inode_lock(d_inode(parent));
776
777 if (child != dentry)
778
779 goto loop;
780
781 if (!__debugfs_remove(child, parent))
782 simple_release_fs(&debugfs_mount, &debugfs_mount_count);
783 inode_unlock(d_inode(parent));
784}
785EXPORT_SYMBOL_GPL(debugfs_remove_recursive);
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806struct dentry *debugfs_rename(struct dentry *old_dir, struct dentry *old_dentry,
807 struct dentry *new_dir, const char *new_name)
808{
809 int error;
810 struct dentry *dentry = NULL, *trap;
811 struct name_snapshot old_name;
812
813 if (IS_ERR(old_dir))
814 return old_dir;
815 if (IS_ERR(new_dir))
816 return new_dir;
817 if (IS_ERR_OR_NULL(old_dentry))
818 return old_dentry;
819
820 trap = lock_rename(new_dir, old_dir);
821
822 if (d_really_is_negative(old_dir) || d_really_is_negative(new_dir))
823 goto exit;
824
825 if (d_really_is_negative(old_dentry) || old_dentry == trap ||
826 d_mountpoint(old_dentry))
827 goto exit;
828 dentry = lookup_one_len(new_name, new_dir, strlen(new_name));
829
830 if (IS_ERR(dentry) || dentry == trap || d_really_is_positive(dentry))
831 goto exit;
832
833 take_dentry_name_snapshot(&old_name, old_dentry);
834
835 error = simple_rename(d_inode(old_dir), old_dentry, d_inode(new_dir),
836 dentry, 0);
837 if (error) {
838 release_dentry_name_snapshot(&old_name);
839 goto exit;
840 }
841 d_move(old_dentry, dentry);
842 fsnotify_move(d_inode(old_dir), d_inode(new_dir), &old_name.name,
843 d_is_dir(old_dentry),
844 NULL, old_dentry);
845 release_dentry_name_snapshot(&old_name);
846 unlock_rename(new_dir, old_dir);
847 dput(dentry);
848 return old_dentry;
849exit:
850 if (dentry && !IS_ERR(dentry))
851 dput(dentry);
852 unlock_rename(new_dir, old_dir);
853 if (IS_ERR(dentry))
854 return dentry;
855 return ERR_PTR(-EINVAL);
856}
857EXPORT_SYMBOL_GPL(debugfs_rename);
858
859
860
861
862bool debugfs_initialized(void)
863{
864 return debugfs_registered;
865}
866EXPORT_SYMBOL_GPL(debugfs_initialized);
867
868static int __init debugfs_init(void)
869{
870 int retval;
871
872 retval = sysfs_create_mount_point(kernel_kobj, "debug");
873 if (retval)
874 return retval;
875
876 retval = register_filesystem(&debug_fs_type);
877 if (retval)
878 sysfs_remove_mount_point(kernel_kobj, "debug");
879 else
880 debugfs_registered = true;
881
882 return retval;
883}
884core_initcall(debugfs_init);
885
886