inode.c 16 KB

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