inode.c 21 KB

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