inode.c 16 KB

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