inode.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648
  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 <[email protected]>
  6. *
  7. * Copyright (C) 2014 Red Hat Inc, author: Steven Rostedt <[email protected]>
  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/mount.h>
  14. #include <linux/kobject.h>
  15. #include <linux/namei.h>
  16. #include <linux/tracefs.h>
  17. #include <linux/fsnotify.h>
  18. #include <linux/security.h>
  19. #include <linux/seq_file.h>
  20. #include <linux/parser.h>
  21. #include <linux/magic.h>
  22. #include <linux/slab.h>
  23. #define TRACEFS_DEFAULT_MODE 0700
  24. static struct vfsmount *tracefs_mount;
  25. static int tracefs_mount_count;
  26. static bool tracefs_registered;
  27. static ssize_t default_read_file(struct file *file, char __user *buf,
  28. size_t count, loff_t *ppos)
  29. {
  30. return 0;
  31. }
  32. static ssize_t default_write_file(struct file *file, const char __user *buf,
  33. size_t count, loff_t *ppos)
  34. {
  35. return count;
  36. }
  37. static const struct file_operations tracefs_file_operations = {
  38. .read = default_read_file,
  39. .write = default_write_file,
  40. .open = simple_open,
  41. .llseek = noop_llseek,
  42. };
  43. static struct tracefs_dir_ops {
  44. int (*mkdir)(const char *name);
  45. int (*rmdir)(const char *name);
  46. } tracefs_ops __ro_after_init;
  47. static char *get_dname(struct dentry *dentry)
  48. {
  49. const char *dname;
  50. char *name;
  51. int len = dentry->d_name.len;
  52. dname = dentry->d_name.name;
  53. name = kmalloc(len + 1, GFP_KERNEL);
  54. if (!name)
  55. return NULL;
  56. memcpy(name, dname, len);
  57. name[len] = 0;
  58. return name;
  59. }
  60. static int tracefs_syscall_mkdir(struct user_namespace *mnt_userns,
  61. struct inode *inode, struct dentry *dentry,
  62. umode_t mode)
  63. {
  64. char *name;
  65. int ret;
  66. name = get_dname(dentry);
  67. if (!name)
  68. return -ENOMEM;
  69. /*
  70. * The mkdir call can call the generic functions that create
  71. * the files within the tracefs system. It is up to the individual
  72. * mkdir routine to handle races.
  73. */
  74. inode_unlock(inode);
  75. ret = tracefs_ops.mkdir(name);
  76. inode_lock(inode);
  77. kfree(name);
  78. return ret;
  79. }
  80. static int tracefs_syscall_rmdir(struct inode *inode, struct dentry *dentry)
  81. {
  82. char *name;
  83. int ret;
  84. name = get_dname(dentry);
  85. if (!name)
  86. return -ENOMEM;
  87. /*
  88. * The rmdir call can call the generic functions that create
  89. * the files within the tracefs system. It is up to the individual
  90. * rmdir routine to handle races.
  91. * This time we need to unlock not only the parent (inode) but
  92. * also the directory that is being deleted.
  93. */
  94. inode_unlock(inode);
  95. inode_unlock(d_inode(dentry));
  96. ret = tracefs_ops.rmdir(name);
  97. inode_lock_nested(inode, I_MUTEX_PARENT);
  98. inode_lock(d_inode(dentry));
  99. kfree(name);
  100. return ret;
  101. }
  102. static const struct inode_operations tracefs_dir_inode_operations = {
  103. .lookup = simple_lookup,
  104. .mkdir = tracefs_syscall_mkdir,
  105. .rmdir = tracefs_syscall_rmdir,
  106. };
  107. static struct inode *tracefs_get_inode(struct super_block *sb)
  108. {
  109. struct inode *inode = new_inode(sb);
  110. if (inode) {
  111. inode->i_ino = get_next_ino();
  112. inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
  113. }
  114. return inode;
  115. }
  116. struct tracefs_mount_opts {
  117. kuid_t uid;
  118. kgid_t gid;
  119. umode_t mode;
  120. /* Opt_* bitfield. */
  121. unsigned int opts;
  122. };
  123. enum {
  124. Opt_uid,
  125. Opt_gid,
  126. Opt_mode,
  127. Opt_err
  128. };
  129. static const match_table_t tokens = {
  130. {Opt_uid, "uid=%u"},
  131. {Opt_gid, "gid=%u"},
  132. {Opt_mode, "mode=%o"},
  133. {Opt_err, NULL}
  134. };
  135. struct tracefs_fs_info {
  136. struct tracefs_mount_opts mount_opts;
  137. };
  138. static void change_gid(struct dentry *dentry, kgid_t gid)
  139. {
  140. if (!dentry->d_inode)
  141. return;
  142. dentry->d_inode->i_gid = gid;
  143. }
  144. /*
  145. * Taken from d_walk, but without he need for handling renames.
  146. * Nothing can be renamed while walking the list, as tracefs
  147. * does not support renames. This is only called when mounting
  148. * or remounting the file system, to set all the files to
  149. * the given gid.
  150. */
  151. static void set_gid(struct dentry *parent, kgid_t gid)
  152. {
  153. struct dentry *this_parent;
  154. struct list_head *next;
  155. this_parent = parent;
  156. spin_lock(&this_parent->d_lock);
  157. change_gid(this_parent, gid);
  158. repeat:
  159. next = this_parent->d_subdirs.next;
  160. resume:
  161. while (next != &this_parent->d_subdirs) {
  162. struct list_head *tmp = next;
  163. struct dentry *dentry = list_entry(tmp, struct dentry, d_child);
  164. next = tmp->next;
  165. spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
  166. change_gid(dentry, gid);
  167. if (!list_empty(&dentry->d_subdirs)) {
  168. spin_unlock(&this_parent->d_lock);
  169. spin_release(&dentry->d_lock.dep_map, _RET_IP_);
  170. this_parent = dentry;
  171. spin_acquire(&this_parent->d_lock.dep_map, 0, 1, _RET_IP_);
  172. goto repeat;
  173. }
  174. spin_unlock(&dentry->d_lock);
  175. }
  176. /*
  177. * All done at this level ... ascend and resume the search.
  178. */
  179. rcu_read_lock();
  180. ascend:
  181. if (this_parent != parent) {
  182. struct dentry *child = this_parent;
  183. this_parent = child->d_parent;
  184. spin_unlock(&child->d_lock);
  185. spin_lock(&this_parent->d_lock);
  186. /* go into the first sibling still alive */
  187. do {
  188. next = child->d_child.next;
  189. if (next == &this_parent->d_subdirs)
  190. goto ascend;
  191. child = list_entry(next, struct dentry, d_child);
  192. } while (unlikely(child->d_flags & DCACHE_DENTRY_KILLED));
  193. rcu_read_unlock();
  194. goto resume;
  195. }
  196. rcu_read_unlock();
  197. spin_unlock(&this_parent->d_lock);
  198. return;
  199. }
  200. static int tracefs_parse_options(char *data, struct tracefs_mount_opts *opts)
  201. {
  202. substring_t args[MAX_OPT_ARGS];
  203. int option;
  204. int token;
  205. kuid_t uid;
  206. kgid_t gid;
  207. char *p;
  208. opts->opts = 0;
  209. opts->mode = TRACEFS_DEFAULT_MODE;
  210. while ((p = strsep(&data, ",")) != NULL) {
  211. if (!*p)
  212. continue;
  213. token = match_token(p, tokens, args);
  214. switch (token) {
  215. case Opt_uid:
  216. if (match_int(&args[0], &option))
  217. return -EINVAL;
  218. uid = make_kuid(current_user_ns(), option);
  219. if (!uid_valid(uid))
  220. return -EINVAL;
  221. opts->uid = uid;
  222. break;
  223. case Opt_gid:
  224. if (match_int(&args[0], &option))
  225. return -EINVAL;
  226. gid = make_kgid(current_user_ns(), option);
  227. if (!gid_valid(gid))
  228. return -EINVAL;
  229. opts->gid = gid;
  230. break;
  231. case Opt_mode:
  232. if (match_octal(&args[0], &option))
  233. return -EINVAL;
  234. opts->mode = option & S_IALLUGO;
  235. break;
  236. /*
  237. * We might like to report bad mount options here;
  238. * but traditionally tracefs has ignored all mount options
  239. */
  240. }
  241. opts->opts |= BIT(token);
  242. }
  243. return 0;
  244. }
  245. static int tracefs_apply_options(struct super_block *sb, bool remount)
  246. {
  247. struct tracefs_fs_info *fsi = sb->s_fs_info;
  248. struct inode *inode = d_inode(sb->s_root);
  249. struct tracefs_mount_opts *opts = &fsi->mount_opts;
  250. /*
  251. * On remount, only reset mode/uid/gid if they were provided as mount
  252. * options.
  253. */
  254. if (!remount || opts->opts & BIT(Opt_mode)) {
  255. inode->i_mode &= ~S_IALLUGO;
  256. inode->i_mode |= opts->mode;
  257. }
  258. if (!remount || opts->opts & BIT(Opt_uid))
  259. inode->i_uid = opts->uid;
  260. if (!remount || opts->opts & BIT(Opt_gid)) {
  261. /* Set all the group ids to the mount option */
  262. set_gid(sb->s_root, opts->gid);
  263. }
  264. return 0;
  265. }
  266. static int tracefs_remount(struct super_block *sb, int *flags, char *data)
  267. {
  268. int err;
  269. struct tracefs_fs_info *fsi = sb->s_fs_info;
  270. sync_filesystem(sb);
  271. err = tracefs_parse_options(data, &fsi->mount_opts);
  272. if (err)
  273. goto fail;
  274. tracefs_apply_options(sb, true);
  275. fail:
  276. return err;
  277. }
  278. static int tracefs_show_options(struct seq_file *m, struct dentry *root)
  279. {
  280. struct tracefs_fs_info *fsi = root->d_sb->s_fs_info;
  281. struct tracefs_mount_opts *opts = &fsi->mount_opts;
  282. if (!uid_eq(opts->uid, GLOBAL_ROOT_UID))
  283. seq_printf(m, ",uid=%u",
  284. from_kuid_munged(&init_user_ns, opts->uid));
  285. if (!gid_eq(opts->gid, GLOBAL_ROOT_GID))
  286. seq_printf(m, ",gid=%u",
  287. from_kgid_munged(&init_user_ns, opts->gid));
  288. if (opts->mode != TRACEFS_DEFAULT_MODE)
  289. seq_printf(m, ",mode=%o", opts->mode);
  290. return 0;
  291. }
  292. static const struct super_operations tracefs_super_operations = {
  293. .statfs = simple_statfs,
  294. .remount_fs = tracefs_remount,
  295. .show_options = tracefs_show_options,
  296. };
  297. static int trace_fill_super(struct super_block *sb, void *data, int silent)
  298. {
  299. static const struct tree_descr trace_files[] = {{""}};
  300. struct tracefs_fs_info *fsi;
  301. int err;
  302. fsi = kzalloc(sizeof(struct tracefs_fs_info), GFP_KERNEL);
  303. sb->s_fs_info = fsi;
  304. if (!fsi) {
  305. err = -ENOMEM;
  306. goto fail;
  307. }
  308. err = tracefs_parse_options(data, &fsi->mount_opts);
  309. if (err)
  310. goto fail;
  311. err = simple_fill_super(sb, TRACEFS_MAGIC, trace_files);
  312. if (err)
  313. goto fail;
  314. sb->s_op = &tracefs_super_operations;
  315. tracefs_apply_options(sb, false);
  316. return 0;
  317. fail:
  318. kfree(fsi);
  319. sb->s_fs_info = NULL;
  320. return err;
  321. }
  322. static struct dentry *trace_mount(struct file_system_type *fs_type,
  323. int flags, const char *dev_name,
  324. void *data)
  325. {
  326. return mount_single(fs_type, flags, data, trace_fill_super);
  327. }
  328. static struct file_system_type trace_fs_type = {
  329. .owner = THIS_MODULE,
  330. .name = "tracefs",
  331. .mount = trace_mount,
  332. .kill_sb = kill_litter_super,
  333. };
  334. MODULE_ALIAS_FS("tracefs");
  335. static struct dentry *start_creating(const char *name, struct dentry *parent)
  336. {
  337. struct dentry *dentry;
  338. int error;
  339. pr_debug("tracefs: creating file '%s'\n",name);
  340. error = simple_pin_fs(&trace_fs_type, &tracefs_mount,
  341. &tracefs_mount_count);
  342. if (error)
  343. return ERR_PTR(error);
  344. /* If the parent is not specified, we create it in the root.
  345. * We need the root dentry to do this, which is in the super
  346. * block. A pointer to that is in the struct vfsmount that we
  347. * have around.
  348. */
  349. if (!parent)
  350. parent = tracefs_mount->mnt_root;
  351. inode_lock(d_inode(parent));
  352. if (unlikely(IS_DEADDIR(d_inode(parent))))
  353. dentry = ERR_PTR(-ENOENT);
  354. else
  355. dentry = lookup_one_len(name, parent, strlen(name));
  356. if (!IS_ERR(dentry) && d_inode(dentry)) {
  357. dput(dentry);
  358. dentry = ERR_PTR(-EEXIST);
  359. }
  360. if (IS_ERR(dentry)) {
  361. inode_unlock(d_inode(parent));
  362. simple_release_fs(&tracefs_mount, &tracefs_mount_count);
  363. }
  364. return dentry;
  365. }
  366. static struct dentry *failed_creating(struct dentry *dentry)
  367. {
  368. inode_unlock(d_inode(dentry->d_parent));
  369. dput(dentry);
  370. simple_release_fs(&tracefs_mount, &tracefs_mount_count);
  371. return NULL;
  372. }
  373. static struct dentry *end_creating(struct dentry *dentry)
  374. {
  375. inode_unlock(d_inode(dentry->d_parent));
  376. return dentry;
  377. }
  378. /**
  379. * tracefs_create_file - create a file in the tracefs filesystem
  380. * @name: a pointer to a string containing the name of the file to create.
  381. * @mode: the permission that the file should have.
  382. * @parent: a pointer to the parent dentry for this file. This should be a
  383. * directory dentry if set. If this parameter is NULL, then the
  384. * file will be created in the root of the tracefs filesystem.
  385. * @data: a pointer to something that the caller will want to get to later
  386. * on. The inode.i_private pointer will point to this value on
  387. * the open() call.
  388. * @fops: a pointer to a struct file_operations that should be used for
  389. * this file.
  390. *
  391. * This is the basic "create a file" function for tracefs. It allows for a
  392. * wide range of flexibility in creating a file, or a directory (if you want
  393. * to create a directory, the tracefs_create_dir() function is
  394. * recommended to be used instead.)
  395. *
  396. * This function will return a pointer to a dentry if it succeeds. This
  397. * pointer must be passed to the tracefs_remove() function when the file is
  398. * to be removed (no automatic cleanup happens if your module is unloaded,
  399. * you are responsible here.) If an error occurs, %NULL will be returned.
  400. *
  401. * If tracefs is not enabled in the kernel, the value -%ENODEV will be
  402. * returned.
  403. */
  404. struct dentry *tracefs_create_file(const char *name, umode_t mode,
  405. struct dentry *parent, void *data,
  406. const struct file_operations *fops)
  407. {
  408. struct dentry *dentry;
  409. struct inode *inode;
  410. if (security_locked_down(LOCKDOWN_TRACEFS))
  411. return NULL;
  412. if (!(mode & S_IFMT))
  413. mode |= S_IFREG;
  414. BUG_ON(!S_ISREG(mode));
  415. dentry = start_creating(name, parent);
  416. if (IS_ERR(dentry))
  417. return NULL;
  418. inode = tracefs_get_inode(dentry->d_sb);
  419. if (unlikely(!inode))
  420. return failed_creating(dentry);
  421. inode->i_mode = mode;
  422. inode->i_fop = fops ? fops : &tracefs_file_operations;
  423. inode->i_private = data;
  424. inode->i_uid = d_inode(dentry->d_parent)->i_uid;
  425. inode->i_gid = d_inode(dentry->d_parent)->i_gid;
  426. d_instantiate(dentry, inode);
  427. fsnotify_create(d_inode(dentry->d_parent), dentry);
  428. return end_creating(dentry);
  429. }
  430. static struct dentry *__create_dir(const char *name, struct dentry *parent,
  431. const struct inode_operations *ops)
  432. {
  433. struct dentry *dentry = start_creating(name, parent);
  434. struct inode *inode;
  435. if (IS_ERR(dentry))
  436. return NULL;
  437. inode = tracefs_get_inode(dentry->d_sb);
  438. if (unlikely(!inode))
  439. return failed_creating(dentry);
  440. /* Do not set bits for OTH */
  441. inode->i_mode = S_IFDIR | S_IRWXU | S_IRUSR| S_IRGRP | S_IXUSR | S_IXGRP;
  442. inode->i_op = ops;
  443. inode->i_fop = &simple_dir_operations;
  444. inode->i_uid = d_inode(dentry->d_parent)->i_uid;
  445. inode->i_gid = d_inode(dentry->d_parent)->i_gid;
  446. /* directory inodes start off with i_nlink == 2 (for "." entry) */
  447. inc_nlink(inode);
  448. d_instantiate(dentry, inode);
  449. inc_nlink(d_inode(dentry->d_parent));
  450. fsnotify_mkdir(d_inode(dentry->d_parent), dentry);
  451. return end_creating(dentry);
  452. }
  453. /**
  454. * tracefs_create_dir - create a directory in the tracefs filesystem
  455. * @name: a pointer to a string containing the name of the directory to
  456. * create.
  457. * @parent: a pointer to the parent dentry for this file. This should be a
  458. * directory dentry if set. If this parameter is NULL, then the
  459. * directory will be created in the root of the tracefs filesystem.
  460. *
  461. * This function creates a directory in tracefs with the given name.
  462. *
  463. * This function will return a pointer to a dentry if it succeeds. This
  464. * pointer must be passed to the tracefs_remove() function when the file is
  465. * to be removed. If an error occurs, %NULL will be returned.
  466. *
  467. * If tracing is not enabled in the kernel, the value -%ENODEV will be
  468. * returned.
  469. */
  470. struct dentry *tracefs_create_dir(const char *name, struct dentry *parent)
  471. {
  472. if (security_locked_down(LOCKDOWN_TRACEFS))
  473. return NULL;
  474. return __create_dir(name, parent, &simple_dir_inode_operations);
  475. }
  476. /**
  477. * tracefs_create_instance_dir - create the tracing instances directory
  478. * @name: The name of the instances directory to create
  479. * @parent: The parent directory that the instances directory will exist
  480. * @mkdir: The function to call when a mkdir is performed.
  481. * @rmdir: The function to call when a rmdir is performed.
  482. *
  483. * Only one instances directory is allowed.
  484. *
  485. * The instances directory is special as it allows for mkdir and rmdir
  486. * to be done by userspace. When a mkdir or rmdir is performed, the inode
  487. * locks are released and the methods passed in (@mkdir and @rmdir) are
  488. * called without locks and with the name of the directory being created
  489. * within the instances directory.
  490. *
  491. * Returns the dentry of the instances directory.
  492. */
  493. __init struct dentry *tracefs_create_instance_dir(const char *name,
  494. struct dentry *parent,
  495. int (*mkdir)(const char *name),
  496. int (*rmdir)(const char *name))
  497. {
  498. struct dentry *dentry;
  499. /* Only allow one instance of the instances directory. */
  500. if (WARN_ON(tracefs_ops.mkdir || tracefs_ops.rmdir))
  501. return NULL;
  502. dentry = __create_dir(name, parent, &tracefs_dir_inode_operations);
  503. if (!dentry)
  504. return NULL;
  505. tracefs_ops.mkdir = mkdir;
  506. tracefs_ops.rmdir = rmdir;
  507. return dentry;
  508. }
  509. static void remove_one(struct dentry *victim)
  510. {
  511. simple_release_fs(&tracefs_mount, &tracefs_mount_count);
  512. }
  513. /**
  514. * tracefs_remove - recursively removes a directory
  515. * @dentry: a pointer to a the dentry of the directory to be removed.
  516. *
  517. * This function recursively removes a directory tree in tracefs that
  518. * was previously created with a call to another tracefs function
  519. * (like tracefs_create_file() or variants thereof.)
  520. */
  521. void tracefs_remove(struct dentry *dentry)
  522. {
  523. if (IS_ERR_OR_NULL(dentry))
  524. return;
  525. simple_pin_fs(&trace_fs_type, &tracefs_mount, &tracefs_mount_count);
  526. simple_recursive_removal(dentry, remove_one);
  527. simple_release_fs(&tracefs_mount, &tracefs_mount_count);
  528. }
  529. /**
  530. * tracefs_initialized - Tells whether tracefs has been registered
  531. */
  532. bool tracefs_initialized(void)
  533. {
  534. return tracefs_registered;
  535. }
  536. static int __init tracefs_init(void)
  537. {
  538. int retval;
  539. retval = sysfs_create_mount_point(kernel_kobj, "tracing");
  540. if (retval)
  541. return -EINVAL;
  542. retval = register_filesystem(&trace_fs_type);
  543. if (!retval)
  544. tracefs_registered = true;
  545. return retval;
  546. }
  547. core_initcall(tracefs_init);