1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * inode.c - part of tracefs, a pseudo file system for activating tracing
4 *
5 * Based on debugfs by: Greg Kroah-Hartman <greg@kroah.com>
6 *
7 * Copyright (C) 2014 Red Hat Inc, author: Steven Rostedt <srostedt@redhat.com>
8 *
9 * tracefs is the file system that is used by the tracing infrastructure.
10 */
11
12 #include <linux/module.h>
13 #include <linux/fs.h>
14 #include <linux/fs_context.h>
15 #include <linux/fs_parser.h>
16 #include <linux/kobject.h>
17 #include <linux/namei.h>
18 #include <linux/tracefs.h>
19 #include <linux/fsnotify.h>
20 #include <linux/security.h>
21 #include <linux/seq_file.h>
22 #include <linux/magic.h>
23 #include <linux/slab.h>
24 #include "internal.h"
25
26 #define TRACEFS_DEFAULT_MODE 0700
27 static struct kmem_cache *tracefs_inode_cachep __ro_after_init;
28
29 static struct vfsmount *tracefs_mount;
30 static int tracefs_mount_count;
31 static bool tracefs_registered;
32
33 /*
34 * Keep track of all tracefs_inodes in order to update their
35 * flags if necessary on a remount.
36 */
37 static DEFINE_SPINLOCK(tracefs_inode_lock);
38 static LIST_HEAD(tracefs_inodes);
39
tracefs_alloc_inode(struct super_block * sb)40 static struct inode *tracefs_alloc_inode(struct super_block *sb)
41 {
42 struct tracefs_inode *ti;
43 unsigned long flags;
44
45 ti = alloc_inode_sb(sb, tracefs_inode_cachep, GFP_KERNEL);
46 if (!ti)
47 return NULL;
48
49 spin_lock_irqsave(&tracefs_inode_lock, flags);
50 list_add_rcu(&ti->list, &tracefs_inodes);
51 spin_unlock_irqrestore(&tracefs_inode_lock, flags);
52
53 return &ti->vfs_inode;
54 }
55
tracefs_free_inode(struct inode * inode)56 static void tracefs_free_inode(struct inode *inode)
57 {
58 struct tracefs_inode *ti = get_tracefs(inode);
59
60 kmem_cache_free(tracefs_inode_cachep, ti);
61 }
62
tracefs_destroy_inode(struct inode * inode)63 static void tracefs_destroy_inode(struct inode *inode)
64 {
65 struct tracefs_inode *ti = get_tracefs(inode);
66 unsigned long flags;
67
68 spin_lock_irqsave(&tracefs_inode_lock, flags);
69 list_del_rcu(&ti->list);
70 spin_unlock_irqrestore(&tracefs_inode_lock, flags);
71 }
72
default_read_file(struct file * file,char __user * buf,size_t count,loff_t * ppos)73 static ssize_t default_read_file(struct file *file, char __user *buf,
74 size_t count, loff_t *ppos)
75 {
76 return 0;
77 }
78
default_write_file(struct file * file,const char __user * buf,size_t count,loff_t * ppos)79 static ssize_t default_write_file(struct file *file, const char __user *buf,
80 size_t count, loff_t *ppos)
81 {
82 return count;
83 }
84
85 static const struct file_operations tracefs_file_operations = {
86 .read = default_read_file,
87 .write = default_write_file,
88 .open = simple_open,
89 .llseek = noop_llseek,
90 };
91
92 static struct tracefs_dir_ops {
93 int (*mkdir)(const char *name);
94 int (*rmdir)(const char *name);
95 } tracefs_ops __ro_after_init;
96
tracefs_syscall_mkdir(struct mnt_idmap * idmap,struct inode * inode,struct dentry * dentry,umode_t mode)97 static struct dentry *tracefs_syscall_mkdir(struct mnt_idmap *idmap,
98 struct inode *inode, struct dentry *dentry,
99 umode_t mode)
100 {
101 struct tracefs_inode *ti;
102 struct name_snapshot name;
103 int ret;
104
105 /*
106 * This is a new directory that does not take the default of
107 * the rootfs. It becomes the default permissions for all the
108 * files and directories underneath it.
109 */
110 ti = get_tracefs(inode);
111 ti->flags |= TRACEFS_INSTANCE_INODE;
112 ti->private = inode;
113
114 /*
115 * The mkdir call can call the generic functions that create
116 * the files within the tracefs system. It is up to the individual
117 * mkdir routine to handle races.
118 */
119 take_dentry_name_snapshot(&name, dentry);
120 inode_unlock(inode);
121 ret = tracefs_ops.mkdir(name.name.name);
122 inode_lock(inode);
123 release_dentry_name_snapshot(&name);
124
125 return ERR_PTR(ret);
126 }
127
tracefs_syscall_rmdir(struct inode * inode,struct dentry * dentry)128 static int tracefs_syscall_rmdir(struct inode *inode, struct dentry *dentry)
129 {
130 struct name_snapshot name;
131 int ret;
132
133 /*
134 * The rmdir call can call the generic functions that create
135 * the files within the tracefs system. It is up to the individual
136 * rmdir routine to handle races.
137 * This time we need to unlock not only the parent (inode) but
138 * also the directory that is being deleted.
139 */
140 take_dentry_name_snapshot(&name, dentry);
141 inode_unlock(inode);
142 inode_unlock(d_inode(dentry));
143
144 ret = tracefs_ops.rmdir(name.name.name);
145
146 inode_lock_nested(inode, I_MUTEX_PARENT);
147 inode_lock(d_inode(dentry));
148 release_dentry_name_snapshot(&name);
149
150 return ret;
151 }
152
set_tracefs_inode_owner(struct inode * inode)153 static void set_tracefs_inode_owner(struct inode *inode)
154 {
155 struct tracefs_inode *ti = get_tracefs(inode);
156 struct inode *root_inode = ti->private;
157 kuid_t uid;
158 kgid_t gid;
159
160 uid = root_inode->i_uid;
161 gid = root_inode->i_gid;
162
163 /*
164 * If the root is not the mount point, then check the root's
165 * permissions. If it was never set, then default to the
166 * mount point.
167 */
168 if (root_inode != d_inode(root_inode->i_sb->s_root)) {
169 struct tracefs_inode *rti;
170
171 rti = get_tracefs(root_inode);
172 root_inode = d_inode(root_inode->i_sb->s_root);
173
174 if (!(rti->flags & TRACEFS_UID_PERM_SET))
175 uid = root_inode->i_uid;
176
177 if (!(rti->flags & TRACEFS_GID_PERM_SET))
178 gid = root_inode->i_gid;
179 }
180
181 /*
182 * If this inode has never been referenced, then update
183 * the permissions to the superblock.
184 */
185 if (!(ti->flags & TRACEFS_UID_PERM_SET))
186 inode->i_uid = uid;
187
188 if (!(ti->flags & TRACEFS_GID_PERM_SET))
189 inode->i_gid = gid;
190 }
191
tracefs_permission(struct mnt_idmap * idmap,struct inode * inode,int mask)192 static int tracefs_permission(struct mnt_idmap *idmap,
193 struct inode *inode, int mask)
194 {
195 set_tracefs_inode_owner(inode);
196 return generic_permission(idmap, inode, mask);
197 }
198
tracefs_getattr(struct mnt_idmap * idmap,const struct path * path,struct kstat * stat,u32 request_mask,unsigned int flags)199 static int tracefs_getattr(struct mnt_idmap *idmap,
200 const struct path *path, struct kstat *stat,
201 u32 request_mask, unsigned int flags)
202 {
203 struct inode *inode = d_backing_inode(path->dentry);
204
205 set_tracefs_inode_owner(inode);
206 generic_fillattr(idmap, request_mask, inode, stat);
207 return 0;
208 }
209
tracefs_setattr(struct mnt_idmap * idmap,struct dentry * dentry,struct iattr * attr)210 static int tracefs_setattr(struct mnt_idmap *idmap, struct dentry *dentry,
211 struct iattr *attr)
212 {
213 unsigned int ia_valid = attr->ia_valid;
214 struct inode *inode = d_inode(dentry);
215 struct tracefs_inode *ti = get_tracefs(inode);
216
217 if (ia_valid & ATTR_UID)
218 ti->flags |= TRACEFS_UID_PERM_SET;
219
220 if (ia_valid & ATTR_GID)
221 ti->flags |= TRACEFS_GID_PERM_SET;
222
223 return simple_setattr(idmap, dentry, attr);
224 }
225
226 static const struct inode_operations tracefs_instance_dir_inode_operations = {
227 .lookup = simple_lookup,
228 .mkdir = tracefs_syscall_mkdir,
229 .rmdir = tracefs_syscall_rmdir,
230 .permission = tracefs_permission,
231 .getattr = tracefs_getattr,
232 .setattr = tracefs_setattr,
233 };
234
235 static const struct inode_operations tracefs_dir_inode_operations = {
236 .lookup = simple_lookup,
237 .permission = tracefs_permission,
238 .getattr = tracefs_getattr,
239 .setattr = tracefs_setattr,
240 };
241
242 static const struct inode_operations tracefs_file_inode_operations = {
243 .permission = tracefs_permission,
244 .getattr = tracefs_getattr,
245 .setattr = tracefs_setattr,
246 };
247
tracefs_get_inode(struct super_block * sb)248 struct inode *tracefs_get_inode(struct super_block *sb)
249 {
250 struct inode *inode = new_inode(sb);
251 if (inode) {
252 inode->i_ino = get_next_ino();
253 simple_inode_init_ts(inode);
254 }
255 return inode;
256 }
257
258 struct tracefs_fs_info {
259 kuid_t uid;
260 kgid_t gid;
261 umode_t mode;
262 /* Opt_* bitfield. */
263 unsigned int opts;
264 };
265
266 enum {
267 Opt_uid,
268 Opt_gid,
269 Opt_mode,
270 };
271
272 static const struct fs_parameter_spec tracefs_param_specs[] = {
273 fsparam_gid ("gid", Opt_gid),
274 fsparam_u32oct ("mode", Opt_mode),
275 fsparam_uid ("uid", Opt_uid),
276 {}
277 };
278
tracefs_parse_param(struct fs_context * fc,struct fs_parameter * param)279 static int tracefs_parse_param(struct fs_context *fc, struct fs_parameter *param)
280 {
281 struct tracefs_fs_info *opts = fc->s_fs_info;
282 struct fs_parse_result result;
283 int opt;
284
285 opt = fs_parse(fc, tracefs_param_specs, param, &result);
286 if (opt < 0)
287 return opt;
288
289 switch (opt) {
290 case Opt_uid:
291 opts->uid = result.uid;
292 break;
293 case Opt_gid:
294 opts->gid = result.gid;
295 break;
296 case Opt_mode:
297 opts->mode = result.uint_32 & S_IALLUGO;
298 break;
299 /*
300 * We might like to report bad mount options here;
301 * but traditionally tracefs has ignored all mount options
302 */
303 }
304
305 opts->opts |= BIT(opt);
306
307 return 0;
308 }
309
tracefs_apply_options(struct super_block * sb,bool remount)310 static int tracefs_apply_options(struct super_block *sb, bool remount)
311 {
312 struct tracefs_fs_info *fsi = sb->s_fs_info;
313 struct inode *inode = d_inode(sb->s_root);
314 struct tracefs_inode *ti;
315 bool update_uid, update_gid;
316 umode_t tmp_mode;
317
318 /*
319 * On remount, only reset mode/uid/gid if they were provided as mount
320 * options.
321 */
322
323 if (!remount || fsi->opts & BIT(Opt_mode)) {
324 tmp_mode = READ_ONCE(inode->i_mode) & ~S_IALLUGO;
325 tmp_mode |= fsi->mode;
326 WRITE_ONCE(inode->i_mode, tmp_mode);
327 }
328
329 if (!remount || fsi->opts & BIT(Opt_uid))
330 inode->i_uid = fsi->uid;
331
332 if (!remount || fsi->opts & BIT(Opt_gid))
333 inode->i_gid = fsi->gid;
334
335 if (remount && (fsi->opts & BIT(Opt_uid) || fsi->opts & BIT(Opt_gid))) {
336
337 update_uid = fsi->opts & BIT(Opt_uid);
338 update_gid = fsi->opts & BIT(Opt_gid);
339
340 rcu_read_lock();
341 list_for_each_entry_rcu(ti, &tracefs_inodes, list) {
342 if (update_uid) {
343 ti->flags &= ~TRACEFS_UID_PERM_SET;
344 ti->vfs_inode.i_uid = fsi->uid;
345 }
346
347 if (update_gid) {
348 ti->flags &= ~TRACEFS_GID_PERM_SET;
349 ti->vfs_inode.i_gid = fsi->gid;
350 }
351
352 /*
353 * Note, the above ti->vfs_inode updates are
354 * used in eventfs_remount() so they must come
355 * before calling it.
356 */
357 if (ti->flags & TRACEFS_EVENT_INODE)
358 eventfs_remount(ti, update_uid, update_gid);
359 }
360 rcu_read_unlock();
361 }
362
363 return 0;
364 }
365
tracefs_reconfigure(struct fs_context * fc)366 static int tracefs_reconfigure(struct fs_context *fc)
367 {
368 struct super_block *sb = fc->root->d_sb;
369 struct tracefs_fs_info *sb_opts = sb->s_fs_info;
370 struct tracefs_fs_info *new_opts = fc->s_fs_info;
371
372 if (!new_opts)
373 return 0;
374
375 sync_filesystem(sb);
376 /* structure copy of new mount options to sb */
377 *sb_opts = *new_opts;
378
379 return tracefs_apply_options(sb, true);
380 }
381
tracefs_show_options(struct seq_file * m,struct dentry * root)382 static int tracefs_show_options(struct seq_file *m, struct dentry *root)
383 {
384 struct tracefs_fs_info *fsi = root->d_sb->s_fs_info;
385
386 if (!uid_eq(fsi->uid, GLOBAL_ROOT_UID))
387 seq_printf(m, ",uid=%u",
388 from_kuid_munged(&init_user_ns, fsi->uid));
389 if (!gid_eq(fsi->gid, GLOBAL_ROOT_GID))
390 seq_printf(m, ",gid=%u",
391 from_kgid_munged(&init_user_ns, fsi->gid));
392 if (fsi->mode != TRACEFS_DEFAULT_MODE)
393 seq_printf(m, ",mode=%o", fsi->mode);
394
395 return 0;
396 }
397
tracefs_drop_inode(struct inode * inode)398 static int tracefs_drop_inode(struct inode *inode)
399 {
400 struct tracefs_inode *ti = get_tracefs(inode);
401
402 /*
403 * This inode is being freed and cannot be used for
404 * eventfs. Clear the flag so that it doesn't call into
405 * eventfs during the remount flag updates. The eventfs_inode
406 * gets freed after an RCU cycle, so the content will still
407 * be safe if the iteration is going on now.
408 */
409 ti->flags &= ~TRACEFS_EVENT_INODE;
410
411 return 1;
412 }
413
414 static const struct super_operations tracefs_super_operations = {
415 .alloc_inode = tracefs_alloc_inode,
416 .free_inode = tracefs_free_inode,
417 .destroy_inode = tracefs_destroy_inode,
418 .drop_inode = tracefs_drop_inode,
419 .statfs = simple_statfs,
420 .show_options = tracefs_show_options,
421 };
422
423 /*
424 * It would be cleaner if eventfs had its own dentry ops.
425 *
426 * Note that d_revalidate is called potentially under RCU,
427 * so it can't take the eventfs mutex etc. It's fine - if
428 * we open a file just as it's marked dead, things will
429 * still work just fine, and just see the old stale case.
430 */
tracefs_d_release(struct dentry * dentry)431 static void tracefs_d_release(struct dentry *dentry)
432 {
433 if (dentry->d_fsdata)
434 eventfs_d_release(dentry);
435 }
436
tracefs_d_revalidate(struct inode * inode,const struct qstr * name,struct dentry * dentry,unsigned int flags)437 static int tracefs_d_revalidate(struct inode *inode, const struct qstr *name,
438 struct dentry *dentry, unsigned int flags)
439 {
440 struct eventfs_inode *ei = dentry->d_fsdata;
441
442 return !(ei && ei->is_freed);
443 }
444
tracefs_d_delete(const struct dentry * dentry)445 static int tracefs_d_delete(const struct dentry *dentry)
446 {
447 /*
448 * We want to keep eventfs dentries around but not tracefs
449 * ones. eventfs dentries have content in d_fsdata.
450 * Use d_fsdata to determine if it's a eventfs dentry or not.
451 */
452 return dentry->d_fsdata == NULL;
453 }
454
455 static const struct dentry_operations tracefs_dentry_operations = {
456 .d_revalidate = tracefs_d_revalidate,
457 .d_release = tracefs_d_release,
458 .d_delete = tracefs_d_delete,
459 };
460
tracefs_fill_super(struct super_block * sb,struct fs_context * fc)461 static int tracefs_fill_super(struct super_block *sb, struct fs_context *fc)
462 {
463 static const struct tree_descr trace_files[] = {{""}};
464 int err;
465
466 err = simple_fill_super(sb, TRACEFS_MAGIC, trace_files);
467 if (err)
468 return err;
469
470 sb->s_op = &tracefs_super_operations;
471 tracefs_apply_options(sb, false);
472 set_default_d_op(sb, &tracefs_dentry_operations);
473
474 return 0;
475 }
476
tracefs_get_tree(struct fs_context * fc)477 static int tracefs_get_tree(struct fs_context *fc)
478 {
479 int err = get_tree_single(fc, tracefs_fill_super);
480
481 if (err)
482 return err;
483
484 return tracefs_reconfigure(fc);
485 }
486
tracefs_free_fc(struct fs_context * fc)487 static void tracefs_free_fc(struct fs_context *fc)
488 {
489 kfree(fc->s_fs_info);
490 }
491
492 static const struct fs_context_operations tracefs_context_ops = {
493 .free = tracefs_free_fc,
494 .parse_param = tracefs_parse_param,
495 .get_tree = tracefs_get_tree,
496 .reconfigure = tracefs_reconfigure,
497 };
498
tracefs_init_fs_context(struct fs_context * fc)499 static int tracefs_init_fs_context(struct fs_context *fc)
500 {
501 struct tracefs_fs_info *fsi;
502
503 fsi = kzalloc_obj(struct tracefs_fs_info);
504 if (!fsi)
505 return -ENOMEM;
506
507 fsi->mode = TRACEFS_DEFAULT_MODE;
508
509 fc->s_fs_info = fsi;
510 fc->ops = &tracefs_context_ops;
511 return 0;
512 }
513
514 static struct file_system_type trace_fs_type = {
515 .owner = THIS_MODULE,
516 .name = "tracefs",
517 .init_fs_context = tracefs_init_fs_context,
518 .parameters = tracefs_param_specs,
519 .kill_sb = kill_anon_super,
520 };
521 MODULE_ALIAS_FS("tracefs");
522
tracefs_start_creating(const char * name,struct dentry * parent)523 struct dentry *tracefs_start_creating(const char *name, struct dentry *parent)
524 {
525 struct dentry *dentry;
526 int error;
527
528 pr_debug("tracefs: creating file '%s'\n",name);
529
530 error = simple_pin_fs(&trace_fs_type, &tracefs_mount,
531 &tracefs_mount_count);
532 if (error)
533 return ERR_PTR(error);
534
535 /* If the parent is not specified, we create it in the root.
536 * We need the root dentry to do this, which is in the super
537 * block. A pointer to that is in the struct vfsmount that we
538 * have around.
539 */
540 if (!parent)
541 parent = tracefs_mount->mnt_root;
542
543 dentry = simple_start_creating(parent, name);
544 if (IS_ERR(dentry))
545 simple_release_fs(&tracefs_mount, &tracefs_mount_count);
546
547 return dentry;
548 }
549
tracefs_failed_creating(struct dentry * dentry)550 struct dentry *tracefs_failed_creating(struct dentry *dentry)
551 {
552 simple_done_creating(dentry);
553 simple_release_fs(&tracefs_mount, &tracefs_mount_count);
554 return NULL;
555 }
556
tracefs_end_creating(struct dentry * dentry)557 struct dentry *tracefs_end_creating(struct dentry *dentry)
558 {
559 simple_done_creating(dentry);
560 return dentry; // borrowed
561 }
562
563 /* Find the inode that this will use for default */
instance_inode(struct dentry * parent,struct inode * inode)564 static struct inode *instance_inode(struct dentry *parent, struct inode *inode)
565 {
566 struct tracefs_inode *ti;
567
568 /* If parent is NULL then use root inode */
569 if (!parent)
570 return d_inode(inode->i_sb->s_root);
571
572 /* Find the inode that is flagged as an instance or the root inode */
573 while (!IS_ROOT(parent)) {
574 ti = get_tracefs(d_inode(parent));
575 if (ti->flags & TRACEFS_INSTANCE_INODE)
576 break;
577 parent = parent->d_parent;
578 }
579
580 return d_inode(parent);
581 }
582
583 /**
584 * tracefs_create_file - create a file in the tracefs filesystem
585 * @name: a pointer to a string containing the name of the file to create.
586 * @mode: the permission that the file should have.
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 tracefs filesystem.
590 * @data: a pointer to something that the caller will want to get to later
591 * on. The inode.i_private pointer will point to this value on
592 * the open() call.
593 * @fops: a pointer to a struct file_operations that should be used for
594 * this file.
595 *
596 * This is the basic "create a file" function for tracefs. It allows for a
597 * wide range of flexibility in creating a file, or a directory (if you want
598 * to create a directory, the tracefs_create_dir() function is
599 * recommended to be used instead.)
600 *
601 * This function will return a pointer to a dentry if it succeeds. This
602 * pointer must be passed to the tracefs_remove() function when the file is
603 * to be removed (no automatic cleanup happens if your module is unloaded,
604 * you are responsible here.) If an error occurs, %NULL will be returned.
605 *
606 * If tracefs is not enabled in the kernel, the value -%ENODEV will be
607 * returned.
608 */
tracefs_create_file(const char * name,umode_t mode,struct dentry * parent,void * data,const struct file_operations * fops)609 struct dentry *tracefs_create_file(const char *name, umode_t mode,
610 struct dentry *parent, void *data,
611 const struct file_operations *fops)
612 {
613 struct tracefs_inode *ti;
614 struct dentry *dentry;
615 struct inode *inode;
616
617 if (security_locked_down(LOCKDOWN_TRACEFS))
618 return NULL;
619
620 if (!(mode & S_IFMT))
621 mode |= S_IFREG;
622 BUG_ON(!S_ISREG(mode));
623 dentry = tracefs_start_creating(name, parent);
624
625 if (IS_ERR(dentry))
626 return NULL;
627
628 inode = tracefs_get_inode(dentry->d_sb);
629 if (unlikely(!inode))
630 return tracefs_failed_creating(dentry);
631
632 ti = get_tracefs(inode);
633 ti->private = instance_inode(parent, inode);
634
635 inode->i_mode = mode;
636 inode->i_op = &tracefs_file_inode_operations;
637 inode->i_fop = fops ? fops : &tracefs_file_operations;
638 inode->i_private = data;
639 inode->i_uid = d_inode(dentry->d_parent)->i_uid;
640 inode->i_gid = d_inode(dentry->d_parent)->i_gid;
641 d_make_persistent(dentry, inode);
642 fsnotify_create(d_inode(dentry->d_parent), dentry);
643 return tracefs_end_creating(dentry);
644 }
645 EXPORT_SYMBOL_GPL(tracefs_create_file);
646
__create_dir(const char * name,struct dentry * parent,const struct inode_operations * ops)647 static struct dentry *__create_dir(const char *name, struct dentry *parent,
648 const struct inode_operations *ops)
649 {
650 struct tracefs_inode *ti;
651 struct dentry *dentry = tracefs_start_creating(name, parent);
652 struct inode *inode;
653
654 if (IS_ERR(dentry))
655 return NULL;
656
657 inode = tracefs_get_inode(dentry->d_sb);
658 if (unlikely(!inode))
659 return tracefs_failed_creating(dentry);
660
661 /* Do not set bits for OTH */
662 inode->i_mode = S_IFDIR | S_IRWXU | S_IRUSR| S_IRGRP | S_IXUSR | S_IXGRP;
663 inode->i_op = ops;
664 inode->i_fop = &simple_dir_operations;
665 inode->i_uid = d_inode(dentry->d_parent)->i_uid;
666 inode->i_gid = d_inode(dentry->d_parent)->i_gid;
667
668 ti = get_tracefs(inode);
669 ti->private = instance_inode(parent, inode);
670
671 /* directory inodes start off with i_nlink == 2 (for "." entry) */
672 inc_nlink(inode);
673 d_make_persistent(dentry, inode);
674 inc_nlink(d_inode(dentry->d_parent));
675 fsnotify_mkdir(d_inode(dentry->d_parent), dentry);
676 return tracefs_end_creating(dentry);
677 }
678
679 /**
680 * tracefs_create_dir - create a directory in the tracefs filesystem
681 * @name: a pointer to a string containing the name of the directory to
682 * create.
683 * @parent: a pointer to the parent dentry for this file. This should be a
684 * directory dentry if set. If this parameter is NULL, then the
685 * directory will be created in the root of the tracefs filesystem.
686 *
687 * This function creates a directory in tracefs with the given name.
688 *
689 * This function will return a pointer to a dentry if it succeeds. This
690 * pointer must be passed to the tracefs_remove() function when the file is
691 * to be removed. If an error occurs, %NULL will be returned.
692 *
693 * If tracing is not enabled in the kernel, the value -%ENODEV will be
694 * returned.
695 */
tracefs_create_dir(const char * name,struct dentry * parent)696 struct dentry *tracefs_create_dir(const char *name, struct dentry *parent)
697 {
698 if (security_locked_down(LOCKDOWN_TRACEFS))
699 return NULL;
700
701 return __create_dir(name, parent, &tracefs_dir_inode_operations);
702 }
703
704 /**
705 * tracefs_create_instance_dir - create the tracing instances directory
706 * @name: The name of the instances directory to create
707 * @parent: The parent directory that the instances directory will exist
708 * @mkdir: The function to call when a mkdir is performed.
709 * @rmdir: The function to call when a rmdir is performed.
710 *
711 * Only one instances directory is allowed.
712 *
713 * The instances directory is special as it allows for mkdir and rmdir
714 * to be done by userspace. When a mkdir or rmdir is performed, the inode
715 * locks are released and the methods passed in (@mkdir and @rmdir) are
716 * called without locks and with the name of the directory being created
717 * within the instances directory.
718 *
719 * Returns the dentry of the instances directory.
720 */
tracefs_create_instance_dir(const char * name,struct dentry * parent,int (* mkdir)(const char * name),int (* rmdir)(const char * name))721 __init struct dentry *tracefs_create_instance_dir(const char *name,
722 struct dentry *parent,
723 int (*mkdir)(const char *name),
724 int (*rmdir)(const char *name))
725 {
726 struct dentry *dentry;
727
728 /* Only allow one instance of the instances directory. */
729 if (WARN_ON(tracefs_ops.mkdir || tracefs_ops.rmdir))
730 return NULL;
731
732 dentry = __create_dir(name, parent, &tracefs_instance_dir_inode_operations);
733 if (!dentry)
734 return NULL;
735
736 tracefs_ops.mkdir = mkdir;
737 tracefs_ops.rmdir = rmdir;
738
739 return dentry;
740 }
741
remove_one(struct dentry * victim)742 static void remove_one(struct dentry *victim)
743 {
744 simple_release_fs(&tracefs_mount, &tracefs_mount_count);
745 }
746
747 /**
748 * tracefs_remove - recursively removes a directory
749 * @dentry: a pointer to a the dentry of the directory to be removed.
750 *
751 * This function recursively removes a directory tree in tracefs that
752 * was previously created with a call to another tracefs function
753 * (like tracefs_create_file() or variants thereof.)
754 */
tracefs_remove(struct dentry * dentry)755 void tracefs_remove(struct dentry *dentry)
756 {
757 if (IS_ERR_OR_NULL(dentry))
758 return;
759
760 simple_pin_fs(&trace_fs_type, &tracefs_mount, &tracefs_mount_count);
761 simple_recursive_removal(dentry, remove_one);
762 simple_release_fs(&tracefs_mount, &tracefs_mount_count);
763 }
764
765 /**
766 * tracefs_initialized - Tells whether tracefs has been registered
767 */
tracefs_initialized(void)768 bool tracefs_initialized(void)
769 {
770 return tracefs_registered;
771 }
772
init_once(void * foo)773 static void init_once(void *foo)
774 {
775 struct tracefs_inode *ti = (struct tracefs_inode *) foo;
776
777 /* inode_init_once() calls memset() on the vfs_inode portion */
778 inode_init_once(&ti->vfs_inode);
779
780 /* Zero out the rest */
781 memset_after(ti, 0, vfs_inode);
782 }
783
tracefs_init(void)784 static int __init tracefs_init(void)
785 {
786 int retval;
787
788 tracefs_inode_cachep = kmem_cache_create("tracefs_inode_cache",
789 sizeof(struct tracefs_inode),
790 0, (SLAB_RECLAIM_ACCOUNT|
791 SLAB_ACCOUNT),
792 init_once);
793 if (!tracefs_inode_cachep)
794 return -ENOMEM;
795
796 retval = sysfs_create_mount_point(kernel_kobj, "tracing");
797 if (retval)
798 return -EINVAL;
799
800 retval = register_filesystem(&trace_fs_type);
801 if (!retval)
802 tracefs_registered = true;
803
804 return retval;
805 }
806 core_initcall(tracefs_init);
807