inode.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644
  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. mutex_unlock(&inode->i_mutex);
  76. ret = tracefs_ops.mkdir(name);
  77. mutex_lock(&inode->i_mutex);
  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. mutex_unlock(&inode->i_mutex);
  96. mutex_unlock(&dentry->d_inode->i_mutex);
  97. ret = tracefs_ops.rmdir(name);
  98. mutex_lock_nested(&inode->i_mutex, I_MUTEX_PARENT);
  99. mutex_lock(&dentry->d_inode->i_mutex);
  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;
  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. mutex_lock(&parent->d_inode->i_mutex);
  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. mutex_unlock(&parent->d_inode->i_mutex);
  285. return dentry;
  286. }
  287. static struct dentry *failed_creating(struct dentry *dentry)
  288. {
  289. mutex_unlock(&dentry->d_parent->d_inode->i_mutex);
  290. dput(dentry);
  291. simple_release_fs(&tracefs_mount, &tracefs_mount_count);
  292. return NULL;
  293. }
  294. static struct dentry *end_creating(struct dentry *dentry)
  295. {
  296. mutex_unlock(&dentry->d_parent->d_inode->i_mutex);
  297. return dentry;
  298. }
  299. /**
  300. * tracefs_create_file - create a file in the tracefs filesystem
  301. * @name: a pointer to a string containing the name of the file to create.
  302. * @mode: the permission that the file should have.
  303. * @parent: a pointer to the parent dentry for this file. This should be a
  304. * directory dentry if set. If this parameter is NULL, then the
  305. * file will be created in the root of the tracefs filesystem.
  306. * @data: a pointer to something that the caller will want to get to later
  307. * on. The inode.i_private pointer will point to this value on
  308. * the open() call.
  309. * @fops: a pointer to a struct file_operations that should be used for
  310. * this file.
  311. *
  312. * This is the basic "create a file" function for tracefs. It allows for a
  313. * wide range of flexibility in creating a file, or a directory (if you want
  314. * to create a directory, the tracefs_create_dir() function is
  315. * recommended to be used instead.)
  316. *
  317. * This function will return a pointer to a dentry if it succeeds. This
  318. * pointer must be passed to the tracefs_remove() function when the file is
  319. * to be removed (no automatic cleanup happens if your module is unloaded,
  320. * you are responsible here.) If an error occurs, %NULL will be returned.
  321. *
  322. * If tracefs is not enabled in the kernel, the value -%ENODEV will be
  323. * returned.
  324. */
  325. struct dentry *tracefs_create_file(const char *name, umode_t mode,
  326. struct dentry *parent, void *data,
  327. const struct file_operations *fops)
  328. {
  329. struct dentry *dentry;
  330. struct inode *inode;
  331. if (!(mode & S_IFMT))
  332. mode |= S_IFREG;
  333. BUG_ON(!S_ISREG(mode));
  334. dentry = start_creating(name, parent);
  335. if (IS_ERR(dentry))
  336. return NULL;
  337. inode = tracefs_get_inode(dentry->d_sb);
  338. if (unlikely(!inode))
  339. return failed_creating(dentry);
  340. inode->i_mode = mode;
  341. inode->i_fop = fops ? fops : &tracefs_file_operations;
  342. inode->i_private = data;
  343. d_instantiate(dentry, inode);
  344. fsnotify_create(dentry->d_parent->d_inode, dentry);
  345. return end_creating(dentry);
  346. }
  347. static struct dentry *__create_dir(const char *name, struct dentry *parent,
  348. const struct inode_operations *ops)
  349. {
  350. struct dentry *dentry = start_creating(name, parent);
  351. struct inode *inode;
  352. if (IS_ERR(dentry))
  353. return NULL;
  354. inode = tracefs_get_inode(dentry->d_sb);
  355. if (unlikely(!inode))
  356. return failed_creating(dentry);
  357. inode->i_mode = S_IFDIR | S_IRWXU | S_IRUGO | S_IXUGO;
  358. inode->i_op = ops;
  359. inode->i_fop = &simple_dir_operations;
  360. /* directory inodes start off with i_nlink == 2 (for "." entry) */
  361. inc_nlink(inode);
  362. d_instantiate(dentry, inode);
  363. inc_nlink(dentry->d_parent->d_inode);
  364. fsnotify_mkdir(dentry->d_parent->d_inode, dentry);
  365. return end_creating(dentry);
  366. }
  367. /**
  368. * tracefs_create_dir - create a directory in the tracefs filesystem
  369. * @name: a pointer to a string containing the name of the directory to
  370. * create.
  371. * @parent: a pointer to the parent dentry for this file. This should be a
  372. * directory dentry if set. If this parameter is NULL, then the
  373. * directory will be created in the root of the tracefs filesystem.
  374. *
  375. * This function creates a directory in tracefs with the given name.
  376. *
  377. * This function will return a pointer to a dentry if it succeeds. This
  378. * pointer must be passed to the tracefs_remove() function when the file is
  379. * to be removed. If an error occurs, %NULL will be returned.
  380. *
  381. * If tracing is not enabled in the kernel, the value -%ENODEV will be
  382. * returned.
  383. */
  384. struct dentry *tracefs_create_dir(const char *name, struct dentry *parent)
  385. {
  386. return __create_dir(name, parent, &simple_dir_inode_operations);
  387. }
  388. /**
  389. * tracefs_create_instance_dir - create the tracing instances directory
  390. * @name: The name of the instances directory to create
  391. * @parent: The parent directory that the instances directory will exist
  392. * @mkdir: The function to call when a mkdir is performed.
  393. * @rmdir: The function to call when a rmdir is performed.
  394. *
  395. * Only one instances directory is allowed.
  396. *
  397. * The instances directory is special as it allows for mkdir and rmdir to
  398. * to be done by userspace. When a mkdir or rmdir is performed, the inode
  399. * locks are released and the methhods passed in (@mkdir and @rmdir) are
  400. * called without locks and with the name of the directory being created
  401. * within the instances directory.
  402. *
  403. * Returns the dentry of the instances directory.
  404. */
  405. struct dentry *tracefs_create_instance_dir(const char *name, struct dentry *parent,
  406. int (*mkdir)(const char *name),
  407. int (*rmdir)(const char *name))
  408. {
  409. struct dentry *dentry;
  410. /* Only allow one instance of the instances directory. */
  411. if (WARN_ON(tracefs_ops.mkdir || tracefs_ops.rmdir))
  412. return NULL;
  413. dentry = __create_dir(name, parent, &tracefs_dir_inode_operations);
  414. if (!dentry)
  415. return NULL;
  416. tracefs_ops.mkdir = mkdir;
  417. tracefs_ops.rmdir = rmdir;
  418. return dentry;
  419. }
  420. static int __tracefs_remove(struct dentry *dentry, struct dentry *parent)
  421. {
  422. int ret = 0;
  423. if (simple_positive(dentry)) {
  424. if (dentry->d_inode) {
  425. dget(dentry);
  426. switch (dentry->d_inode->i_mode & S_IFMT) {
  427. case S_IFDIR:
  428. ret = simple_rmdir(parent->d_inode, dentry);
  429. break;
  430. default:
  431. simple_unlink(parent->d_inode, dentry);
  432. break;
  433. }
  434. if (!ret)
  435. d_delete(dentry);
  436. dput(dentry);
  437. }
  438. }
  439. return ret;
  440. }
  441. /**
  442. * tracefs_remove - removes a file or directory from the tracefs filesystem
  443. * @dentry: a pointer to a the dentry of the file or directory to be
  444. * removed.
  445. *
  446. * This function removes a file or directory in tracefs that was previously
  447. * created with a call to another tracefs function (like
  448. * tracefs_create_file() or variants thereof.)
  449. */
  450. void tracefs_remove(struct dentry *dentry)
  451. {
  452. struct dentry *parent;
  453. int ret;
  454. if (IS_ERR_OR_NULL(dentry))
  455. return;
  456. parent = dentry->d_parent;
  457. if (!parent || !parent->d_inode)
  458. return;
  459. mutex_lock(&parent->d_inode->i_mutex);
  460. ret = __tracefs_remove(dentry, parent);
  461. mutex_unlock(&parent->d_inode->i_mutex);
  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->d_parent;
  479. if (!parent || !parent->d_inode)
  480. return;
  481. parent = dentry;
  482. down:
  483. mutex_lock(&parent->d_inode->i_mutex);
  484. loop:
  485. /*
  486. * The parent->d_subdirs is protected by the d_lock. Outside that
  487. * lock, the child can be unlinked and set to be freed which can
  488. * use the d_u.d_child as the rcu head and corrupt this list.
  489. */
  490. spin_lock(&parent->d_lock);
  491. list_for_each_entry(child, &parent->d_subdirs, d_child) {
  492. if (!simple_positive(child))
  493. continue;
  494. /* perhaps simple_empty(child) makes more sense */
  495. if (!list_empty(&child->d_subdirs)) {
  496. spin_unlock(&parent->d_lock);
  497. mutex_unlock(&parent->d_inode->i_mutex);
  498. parent = child;
  499. goto down;
  500. }
  501. spin_unlock(&parent->d_lock);
  502. if (!__tracefs_remove(child, parent))
  503. simple_release_fs(&tracefs_mount, &tracefs_mount_count);
  504. /*
  505. * The parent->d_lock protects agaist child from unlinking
  506. * from d_subdirs. When releasing the parent->d_lock we can
  507. * no longer trust that the next pointer is valid.
  508. * Restart the loop. We'll skip this one with the
  509. * simple_positive() check.
  510. */
  511. goto loop;
  512. }
  513. spin_unlock(&parent->d_lock);
  514. mutex_unlock(&parent->d_inode->i_mutex);
  515. child = parent;
  516. parent = parent->d_parent;
  517. mutex_lock(&parent->d_inode->i_mutex);
  518. if (child != dentry)
  519. /* go up */
  520. goto loop;
  521. if (!__tracefs_remove(child, parent))
  522. simple_release_fs(&tracefs_mount, &tracefs_mount_count);
  523. mutex_unlock(&parent->d_inode->i_mutex);
  524. }
  525. /**
  526. * tracefs_initialized - Tells whether tracefs has been registered
  527. */
  528. bool tracefs_initialized(void)
  529. {
  530. return tracefs_registered;
  531. }
  532. static int __init tracefs_init(void)
  533. {
  534. int retval;
  535. retval = sysfs_create_mount_point(kernel_kobj, "tracing");
  536. if (retval)
  537. return -EINVAL;
  538. retval = register_filesystem(&trace_fs_type);
  539. if (!retval)
  540. tracefs_registered = true;
  541. return retval;
  542. }
  543. core_initcall(tracefs_init);