1
2
3
4
5
6
7
8
9
10
11
12#include <linux/module.h>
13#include <linux/fs.h>
14#include <linux/mount.h>
15#include <linux/kobject.h>
16#include <linux/namei.h>
17#include <linux/tracefs.h>
18#include <linux/fsnotify.h>
19#include <linux/seq_file.h>
20#include <linux/parser.h>
21#include <linux/magic.h>
22#include <linux/slab.h>
23
24#define TRACEFS_DEFAULT_MODE 0700
25
26static struct vfsmount *tracefs_mount;
27static int tracefs_mount_count;
28static bool tracefs_registered;
29
30static ssize_t default_read_file(struct file *file, char __user *buf,
31 size_t count, loff_t *ppos)
32{
33 return 0;
34}
35
36static ssize_t default_write_file(struct file *file, const char __user *buf,
37 size_t count, loff_t *ppos)
38{
39 return count;
40}
41
42static const struct file_operations tracefs_file_operations = {
43 .read = default_read_file,
44 .write = default_write_file,
45 .open = simple_open,
46 .llseek = noop_llseek,
47};
48
49static struct tracefs_dir_ops {
50 int (*mkdir)(const char *name);
51 int (*rmdir)(const char *name);
52} tracefs_ops __ro_after_init;
53
54static char *get_dname(struct dentry *dentry)
55{
56 const char *dname;
57 char *name;
58 int len = dentry->d_name.len;
59
60 dname = dentry->d_name.name;
61 name = kmalloc(len + 1, GFP_KERNEL);
62 if (!name)
63 return NULL;
64 memcpy(name, dname, len);
65 name[len] = 0;
66 return name;
67}
68
69static int tracefs_syscall_mkdir(struct inode *inode, struct dentry *dentry, umode_t mode)
70{
71 char *name;
72 int ret;
73
74 name = get_dname(dentry);
75 if (!name)
76 return -ENOMEM;
77
78
79
80
81
82
83 inode_unlock(inode);
84 ret = tracefs_ops.mkdir(name);
85 inode_lock(inode);
86
87 kfree(name);
88
89 return ret;
90}
91
92static int tracefs_syscall_rmdir(struct inode *inode, struct dentry *dentry)
93{
94 char *name;
95 int ret;
96
97 name = get_dname(dentry);
98 if (!name)
99 return -ENOMEM;
100
101
102
103
104
105
106
107
108 inode_unlock(inode);
109 inode_unlock(dentry->d_inode);
110
111 ret = tracefs_ops.rmdir(name);
112
113 inode_lock_nested(inode, I_MUTEX_PARENT);
114 inode_lock(dentry->d_inode);
115
116 kfree(name);
117
118 return ret;
119}
120
121static const struct inode_operations tracefs_dir_inode_operations = {
122 .lookup = simple_lookup,
123 .mkdir = tracefs_syscall_mkdir,
124 .rmdir = tracefs_syscall_rmdir,
125};
126
127static struct inode *tracefs_get_inode(struct super_block *sb)
128{
129 struct inode *inode = new_inode(sb);
130 if (inode) {
131 inode->i_ino = get_next_ino();
132 inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
133 }
134 return inode;
135}
136
137struct tracefs_mount_opts {
138 kuid_t uid;
139 kgid_t gid;
140 umode_t mode;
141};
142
143enum {
144 Opt_uid,
145 Opt_gid,
146 Opt_mode,
147 Opt_err
148};
149
150static const match_table_t tokens = {
151 {Opt_uid, "uid=%u"},
152 {Opt_gid, "gid=%u"},
153 {Opt_mode, "mode=%o"},
154 {Opt_err, NULL}
155};
156
157struct tracefs_fs_info {
158 struct tracefs_mount_opts mount_opts;
159};
160
161static int tracefs_parse_options(char *data, struct tracefs_mount_opts *opts)
162{
163 substring_t args[MAX_OPT_ARGS];
164 int option;
165 int token;
166 kuid_t uid;
167 kgid_t gid;
168 char *p;
169
170 opts->mode = TRACEFS_DEFAULT_MODE;
171
172 while ((p = strsep(&data, ",")) != NULL) {
173 if (!*p)
174 continue;
175
176 token = match_token(p, tokens, args);
177 switch (token) {
178 case Opt_uid:
179 if (match_int(&args[0], &option))
180 return -EINVAL;
181 uid = make_kuid(current_user_ns(), option);
182 if (!uid_valid(uid))
183 return -EINVAL;
184 opts->uid = uid;
185 break;
186 case Opt_gid:
187 if (match_int(&args[0], &option))
188 return -EINVAL;
189 gid = make_kgid(current_user_ns(), option);
190 if (!gid_valid(gid))
191 return -EINVAL;
192 opts->gid = gid;
193 break;
194 case Opt_mode:
195 if (match_octal(&args[0], &option))
196 return -EINVAL;
197 opts->mode = option & S_IALLUGO;
198 break;
199
200
201
202
203 }
204 }
205
206 return 0;
207}
208
209static int tracefs_apply_options(struct super_block *sb)
210{
211 struct tracefs_fs_info *fsi = sb->s_fs_info;
212 struct inode *inode = sb->s_root->d_inode;
213 struct tracefs_mount_opts *opts = &fsi->mount_opts;
214
215 inode->i_mode &= ~S_IALLUGO;
216 inode->i_mode |= opts->mode;
217
218 inode->i_uid = opts->uid;
219 inode->i_gid = opts->gid;
220
221 return 0;
222}
223
224static int tracefs_remount(struct super_block *sb, int *flags, char *data)
225{
226 int err;
227 struct tracefs_fs_info *fsi = sb->s_fs_info;
228
229 sync_filesystem(sb);
230 err = tracefs_parse_options(data, &fsi->mount_opts);
231 if (err)
232 goto fail;
233
234 tracefs_apply_options(sb);
235
236fail:
237 return err;
238}
239
240static int tracefs_show_options(struct seq_file *m, struct dentry *root)
241{
242 struct tracefs_fs_info *fsi = root->d_sb->s_fs_info;
243 struct tracefs_mount_opts *opts = &fsi->mount_opts;
244
245 if (!uid_eq(opts->uid, GLOBAL_ROOT_UID))
246 seq_printf(m, ",uid=%u",
247 from_kuid_munged(&init_user_ns, opts->uid));
248 if (!gid_eq(opts->gid, GLOBAL_ROOT_GID))
249 seq_printf(m, ",gid=%u",
250 from_kgid_munged(&init_user_ns, opts->gid));
251 if (opts->mode != TRACEFS_DEFAULT_MODE)
252 seq_printf(m, ",mode=%o", opts->mode);
253
254 return 0;
255}
256
257static const struct super_operations tracefs_super_operations = {
258 .statfs = simple_statfs,
259 .remount_fs = tracefs_remount,
260 .show_options = tracefs_show_options,
261};
262
263static int trace_fill_super(struct super_block *sb, void *data, int silent)
264{
265 static const struct tree_descr trace_files[] = {{""}};
266 struct tracefs_fs_info *fsi;
267 int err;
268
269 fsi = kzalloc(sizeof(struct tracefs_fs_info), GFP_KERNEL);
270 sb->s_fs_info = fsi;
271 if (!fsi) {
272 err = -ENOMEM;
273 goto fail;
274 }
275
276 err = tracefs_parse_options(data, &fsi->mount_opts);
277 if (err)
278 goto fail;
279
280 err = simple_fill_super(sb, TRACEFS_MAGIC, trace_files);
281 if (err)
282 goto fail;
283
284 sb->s_op = &tracefs_super_operations;
285
286 tracefs_apply_options(sb);
287
288 return 0;
289
290fail:
291 kfree(fsi);
292 sb->s_fs_info = NULL;
293 return err;
294}
295
296static struct dentry *trace_mount(struct file_system_type *fs_type,
297 int flags, const char *dev_name,
298 void *data)
299{
300 return mount_single(fs_type, flags, data, trace_fill_super);
301}
302
303static struct file_system_type trace_fs_type = {
304 .owner = THIS_MODULE,
305 .name = "tracefs",
306 .mount = trace_mount,
307 .kill_sb = kill_litter_super,
308};
309MODULE_ALIAS_FS("tracefs");
310
311static struct dentry *start_creating(const char *name, struct dentry *parent)
312{
313 struct dentry *dentry;
314 int error;
315
316 pr_debug("tracefs: creating file '%s'\n",name);
317
318 error = simple_pin_fs(&trace_fs_type, &tracefs_mount,
319 &tracefs_mount_count);
320 if (error)
321 return ERR_PTR(error);
322
323
324
325
326
327
328 if (!parent)
329 parent = tracefs_mount->mnt_root;
330
331 inode_lock(parent->d_inode);
332 dentry = lookup_one_len(name, parent, strlen(name));
333 if (!IS_ERR(dentry) && dentry->d_inode) {
334 dput(dentry);
335 dentry = ERR_PTR(-EEXIST);
336 }
337
338 if (IS_ERR(dentry)) {
339 inode_unlock(parent->d_inode);
340 simple_release_fs(&tracefs_mount, &tracefs_mount_count);
341 }
342
343 return dentry;
344}
345
346static struct dentry *failed_creating(struct dentry *dentry)
347{
348 inode_unlock(dentry->d_parent->d_inode);
349 dput(dentry);
350 simple_release_fs(&tracefs_mount, &tracefs_mount_count);
351 return NULL;
352}
353
354static struct dentry *end_creating(struct dentry *dentry)
355{
356 inode_unlock(dentry->d_parent->d_inode);
357 return dentry;
358}
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386struct dentry *tracefs_create_file(const char *name, umode_t mode,
387 struct dentry *parent, void *data,
388 const struct file_operations *fops)
389{
390 struct dentry *dentry;
391 struct inode *inode;
392
393 if (!(mode & S_IFMT))
394 mode |= S_IFREG;
395 BUG_ON(!S_ISREG(mode));
396 dentry = start_creating(name, parent);
397
398 if (IS_ERR(dentry))
399 return NULL;
400
401 inode = tracefs_get_inode(dentry->d_sb);
402 if (unlikely(!inode))
403 return failed_creating(dentry);
404
405 inode->i_mode = mode;
406 inode->i_fop = fops ? fops : &tracefs_file_operations;
407 inode->i_private = data;
408 d_instantiate(dentry, inode);
409 fsnotify_create(dentry->d_parent->d_inode, dentry);
410 return end_creating(dentry);
411}
412
413static struct dentry *__create_dir(const char *name, struct dentry *parent,
414 const struct inode_operations *ops)
415{
416 struct dentry *dentry = start_creating(name, parent);
417 struct inode *inode;
418
419 if (IS_ERR(dentry))
420 return NULL;
421
422 inode = tracefs_get_inode(dentry->d_sb);
423 if (unlikely(!inode))
424 return failed_creating(dentry);
425
426 inode->i_mode = S_IFDIR | S_IRWXU | S_IRUGO | S_IXUGO;
427 inode->i_op = ops;
428 inode->i_fop = &simple_dir_operations;
429
430
431 inc_nlink(inode);
432 d_instantiate(dentry, inode);
433 inc_nlink(dentry->d_parent->d_inode);
434 fsnotify_mkdir(dentry->d_parent->d_inode, dentry);
435 return end_creating(dentry);
436}
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455struct dentry *tracefs_create_dir(const char *name, struct dentry *parent)
456{
457 return __create_dir(name, parent, &simple_dir_inode_operations);
458}
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477__init struct dentry *tracefs_create_instance_dir(const char *name,
478 struct dentry *parent,
479 int (*mkdir)(const char *name),
480 int (*rmdir)(const char *name))
481{
482 struct dentry *dentry;
483
484
485 if (WARN_ON(tracefs_ops.mkdir || tracefs_ops.rmdir))
486 return NULL;
487
488 dentry = __create_dir(name, parent, &tracefs_dir_inode_operations);
489 if (!dentry)
490 return NULL;
491
492 tracefs_ops.mkdir = mkdir;
493 tracefs_ops.rmdir = rmdir;
494
495 return dentry;
496}
497
498static int __tracefs_remove(struct dentry *dentry, struct dentry *parent)
499{
500 int ret = 0;
501
502 if (simple_positive(dentry)) {
503 if (dentry->d_inode) {
504 dget(dentry);
505 switch (dentry->d_inode->i_mode & S_IFMT) {
506 case S_IFDIR:
507 ret = simple_rmdir(parent->d_inode, dentry);
508 if (!ret)
509 fsnotify_rmdir(parent->d_inode, dentry);
510 break;
511 default:
512 simple_unlink(parent->d_inode, dentry);
513 fsnotify_unlink(parent->d_inode, dentry);
514 break;
515 }
516 if (!ret)
517 d_delete(dentry);
518 dput(dentry);
519 }
520 }
521 return ret;
522}
523
524
525
526
527
528
529
530
531
532
533void tracefs_remove(struct dentry *dentry)
534{
535 struct dentry *parent;
536 int ret;
537
538 if (IS_ERR_OR_NULL(dentry))
539 return;
540
541 parent = dentry->d_parent;
542 inode_lock(parent->d_inode);
543 ret = __tracefs_remove(dentry, parent);
544 inode_unlock(parent->d_inode);
545 if (!ret)
546 simple_release_fs(&tracefs_mount, &tracefs_mount_count);
547}
548
549
550
551
552
553
554
555
556
557void tracefs_remove_recursive(struct dentry *dentry)
558{
559 struct dentry *child, *parent;
560
561 if (IS_ERR_OR_NULL(dentry))
562 return;
563
564 parent = dentry;
565 down:
566 inode_lock(parent->d_inode);
567 loop:
568
569
570
571
572
573 spin_lock(&parent->d_lock);
574 list_for_each_entry(child, &parent->d_subdirs, d_child) {
575 if (!simple_positive(child))
576 continue;
577
578
579 if (!list_empty(&child->d_subdirs)) {
580 spin_unlock(&parent->d_lock);
581 inode_unlock(parent->d_inode);
582 parent = child;
583 goto down;
584 }
585
586 spin_unlock(&parent->d_lock);
587
588 if (!__tracefs_remove(child, parent))
589 simple_release_fs(&tracefs_mount, &tracefs_mount_count);
590
591
592
593
594
595
596
597
598 goto loop;
599 }
600 spin_unlock(&parent->d_lock);
601
602 inode_unlock(parent->d_inode);
603 child = parent;
604 parent = parent->d_parent;
605 inode_lock(parent->d_inode);
606
607 if (child != dentry)
608
609 goto loop;
610
611 if (!__tracefs_remove(child, parent))
612 simple_release_fs(&tracefs_mount, &tracefs_mount_count);
613 inode_unlock(parent->d_inode);
614}
615
616
617
618
619bool tracefs_initialized(void)
620{
621 return tracefs_registered;
622}
623
624static int __init tracefs_init(void)
625{
626 int retval;
627
628 retval = sysfs_create_mount_point(kernel_kobj, "tracing");
629 if (retval)
630 return -EINVAL;
631
632 retval = register_filesystem(&trace_fs_type);
633 if (!retval)
634 tracefs_registered = true;
635
636 return retval;
637}
638core_initcall(tracefs_init);
639