inode.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  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/namei.h>
  19. #include <linux/tracefs.h>
  20. #include <linux/fsnotify.h>
  21. #include <linux/seq_file.h>
  22. #include <linux/parser.h>
  23. #include <linux/magic.h>
  24. #include <linux/slab.h>
  25. #define TRACEFS_DEFAULT_MODE 0700
  26. static struct vfsmount *tracefs_mount;
  27. static int tracefs_mount_count;
  28. static bool tracefs_registered;
  29. static ssize_t default_read_file(struct file *file, char __user *buf,
  30. size_t count, loff_t *ppos)
  31. {
  32. return 0;
  33. }
  34. static ssize_t default_write_file(struct file *file, const char __user *buf,
  35. size_t count, loff_t *ppos)
  36. {
  37. return count;
  38. }
  39. static const struct file_operations tracefs_file_operations = {
  40. .read = default_read_file,
  41. .write = default_write_file,
  42. .open = simple_open,
  43. .llseek = noop_llseek,
  44. };
  45. static struct inode *tracefs_get_inode(struct super_block *sb)
  46. {
  47. struct inode *inode = new_inode(sb);
  48. if (inode) {
  49. inode->i_ino = get_next_ino();
  50. inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  51. }
  52. return inode;
  53. }
  54. struct tracefs_mount_opts {
  55. kuid_t uid;
  56. kgid_t gid;
  57. umode_t mode;
  58. };
  59. enum {
  60. Opt_uid,
  61. Opt_gid,
  62. Opt_mode,
  63. Opt_err
  64. };
  65. static const match_table_t tokens = {
  66. {Opt_uid, "uid=%u"},
  67. {Opt_gid, "gid=%u"},
  68. {Opt_mode, "mode=%o"},
  69. {Opt_err, NULL}
  70. };
  71. struct tracefs_fs_info {
  72. struct tracefs_mount_opts mount_opts;
  73. };
  74. static int tracefs_parse_options(char *data, struct tracefs_mount_opts *opts)
  75. {
  76. substring_t args[MAX_OPT_ARGS];
  77. int option;
  78. int token;
  79. kuid_t uid;
  80. kgid_t gid;
  81. char *p;
  82. opts->mode = TRACEFS_DEFAULT_MODE;
  83. while ((p = strsep(&data, ",")) != NULL) {
  84. if (!*p)
  85. continue;
  86. token = match_token(p, tokens, args);
  87. switch (token) {
  88. case Opt_uid:
  89. if (match_int(&args[0], &option))
  90. return -EINVAL;
  91. uid = make_kuid(current_user_ns(), option);
  92. if (!uid_valid(uid))
  93. return -EINVAL;
  94. opts->uid = uid;
  95. break;
  96. case Opt_gid:
  97. if (match_int(&args[0], &option))
  98. return -EINVAL;
  99. gid = make_kgid(current_user_ns(), option);
  100. if (!gid_valid(gid))
  101. return -EINVAL;
  102. opts->gid = gid;
  103. break;
  104. case Opt_mode:
  105. if (match_octal(&args[0], &option))
  106. return -EINVAL;
  107. opts->mode = option & S_IALLUGO;
  108. break;
  109. /*
  110. * We might like to report bad mount options here;
  111. * but traditionally tracefs has ignored all mount options
  112. */
  113. }
  114. }
  115. return 0;
  116. }
  117. static int tracefs_apply_options(struct super_block *sb)
  118. {
  119. struct tracefs_fs_info *fsi = sb->s_fs_info;
  120. struct inode *inode = sb->s_root->d_inode;
  121. struct tracefs_mount_opts *opts = &fsi->mount_opts;
  122. inode->i_mode &= ~S_IALLUGO;
  123. inode->i_mode |= opts->mode;
  124. inode->i_uid = opts->uid;
  125. inode->i_gid = opts->gid;
  126. return 0;
  127. }
  128. static int tracefs_remount(struct super_block *sb, int *flags, char *data)
  129. {
  130. int err;
  131. struct tracefs_fs_info *fsi = sb->s_fs_info;
  132. sync_filesystem(sb);
  133. err = tracefs_parse_options(data, &fsi->mount_opts);
  134. if (err)
  135. goto fail;
  136. tracefs_apply_options(sb);
  137. fail:
  138. return err;
  139. }
  140. static int tracefs_show_options(struct seq_file *m, struct dentry *root)
  141. {
  142. struct tracefs_fs_info *fsi = root->d_sb->s_fs_info;
  143. struct tracefs_mount_opts *opts = &fsi->mount_opts;
  144. if (!uid_eq(opts->uid, GLOBAL_ROOT_UID))
  145. seq_printf(m, ",uid=%u",
  146. from_kuid_munged(&init_user_ns, opts->uid));
  147. if (!gid_eq(opts->gid, GLOBAL_ROOT_GID))
  148. seq_printf(m, ",gid=%u",
  149. from_kgid_munged(&init_user_ns, opts->gid));
  150. if (opts->mode != TRACEFS_DEFAULT_MODE)
  151. seq_printf(m, ",mode=%o", opts->mode);
  152. return 0;
  153. }
  154. static const struct super_operations tracefs_super_operations = {
  155. .statfs = simple_statfs,
  156. .remount_fs = tracefs_remount,
  157. .show_options = tracefs_show_options,
  158. };
  159. static int trace_fill_super(struct super_block *sb, void *data, int silent)
  160. {
  161. static struct tree_descr trace_files[] = {{""}};
  162. struct tracefs_fs_info *fsi;
  163. int err;
  164. save_mount_options(sb, data);
  165. fsi = kzalloc(sizeof(struct tracefs_fs_info), GFP_KERNEL);
  166. sb->s_fs_info = fsi;
  167. if (!fsi) {
  168. err = -ENOMEM;
  169. goto fail;
  170. }
  171. err = tracefs_parse_options(data, &fsi->mount_opts);
  172. if (err)
  173. goto fail;
  174. err = simple_fill_super(sb, TRACEFS_MAGIC, trace_files);
  175. if (err)
  176. goto fail;
  177. sb->s_op = &tracefs_super_operations;
  178. tracefs_apply_options(sb);
  179. return 0;
  180. fail:
  181. kfree(fsi);
  182. sb->s_fs_info = NULL;
  183. return err;
  184. }
  185. static struct dentry *trace_mount(struct file_system_type *fs_type,
  186. int flags, const char *dev_name,
  187. void *data)
  188. {
  189. return mount_single(fs_type, flags, data, trace_fill_super);
  190. }
  191. static struct file_system_type trace_fs_type = {
  192. .owner = THIS_MODULE,
  193. .name = "tracefs",
  194. .mount = trace_mount,
  195. .kill_sb = kill_litter_super,
  196. };
  197. MODULE_ALIAS_FS("tracefs");
  198. static struct dentry *start_creating(const char *name, struct dentry *parent)
  199. {
  200. struct dentry *dentry;
  201. int error;
  202. pr_debug("tracefs: creating file '%s'\n",name);
  203. error = simple_pin_fs(&trace_fs_type, &tracefs_mount,
  204. &tracefs_mount_count);
  205. if (error)
  206. return ERR_PTR(error);
  207. /* If the parent is not specified, we create it in the root.
  208. * We need the root dentry to do this, which is in the super
  209. * block. A pointer to that is in the struct vfsmount that we
  210. * have around.
  211. */
  212. if (!parent)
  213. parent = tracefs_mount->mnt_root;
  214. mutex_lock(&parent->d_inode->i_mutex);
  215. dentry = lookup_one_len(name, parent, strlen(name));
  216. if (!IS_ERR(dentry) && dentry->d_inode) {
  217. dput(dentry);
  218. dentry = ERR_PTR(-EEXIST);
  219. }
  220. if (IS_ERR(dentry))
  221. mutex_unlock(&parent->d_inode->i_mutex);
  222. return dentry;
  223. }
  224. static struct dentry *failed_creating(struct dentry *dentry)
  225. {
  226. mutex_unlock(&dentry->d_parent->d_inode->i_mutex);
  227. dput(dentry);
  228. simple_release_fs(&tracefs_mount, &tracefs_mount_count);
  229. return NULL;
  230. }
  231. static struct dentry *end_creating(struct dentry *dentry)
  232. {
  233. mutex_unlock(&dentry->d_parent->d_inode->i_mutex);
  234. return dentry;
  235. }
  236. /**
  237. * tracefs_create_file - create a file in the tracefs filesystem
  238. * @name: a pointer to a string containing the name of the file to create.
  239. * @mode: the permission that the file should have.
  240. * @parent: a pointer to the parent dentry for this file. This should be a
  241. * directory dentry if set. If this parameter is NULL, then the
  242. * file will be created in the root of the tracefs filesystem.
  243. * @data: a pointer to something that the caller will want to get to later
  244. * on. The inode.i_private pointer will point to this value on
  245. * the open() call.
  246. * @fops: a pointer to a struct file_operations that should be used for
  247. * this file.
  248. *
  249. * This is the basic "create a file" function for tracefs. It allows for a
  250. * wide range of flexibility in creating a file, or a directory (if you want
  251. * to create a directory, the tracefs_create_dir() function is
  252. * recommended to be used instead.)
  253. *
  254. * This function will return a pointer to a dentry if it succeeds. This
  255. * pointer must be passed to the tracefs_remove() function when the file is
  256. * to be removed (no automatic cleanup happens if your module is unloaded,
  257. * you are responsible here.) If an error occurs, %NULL will be returned.
  258. *
  259. * If tracefs is not enabled in the kernel, the value -%ENODEV will be
  260. * returned.
  261. */
  262. struct dentry *tracefs_create_file(const char *name, umode_t mode,
  263. struct dentry *parent, void *data,
  264. const struct file_operations *fops)
  265. {
  266. struct dentry *dentry;
  267. struct inode *inode;
  268. if (!(mode & S_IFMT))
  269. mode |= S_IFREG;
  270. BUG_ON(!S_ISREG(mode));
  271. dentry = start_creating(name, parent);
  272. if (IS_ERR(dentry))
  273. return NULL;
  274. inode = tracefs_get_inode(dentry->d_sb);
  275. if (unlikely(!inode))
  276. return failed_creating(dentry);
  277. inode->i_mode = mode;
  278. inode->i_fop = fops ? fops : &tracefs_file_operations;
  279. inode->i_private = data;
  280. d_instantiate(dentry, inode);
  281. fsnotify_create(dentry->d_parent->d_inode, dentry);
  282. return end_creating(dentry);
  283. }
  284. /**
  285. * tracefs_create_dir - create a directory in the tracefs filesystem
  286. * @name: a pointer to a string containing the name of the directory to
  287. * create.
  288. * @parent: a pointer to the parent dentry for this file. This should be a
  289. * directory dentry if set. If this parameter is NULL, then the
  290. * directory will be created in the root of the tracefs filesystem.
  291. *
  292. * This function creates a directory in tracefs with the given name.
  293. *
  294. * This function will return a pointer to a dentry if it succeeds. This
  295. * pointer must be passed to the tracefs_remove() function when the file is
  296. * to be removed. If an error occurs, %NULL will be returned.
  297. *
  298. * If tracing is not enabled in the kernel, the value -%ENODEV will be
  299. * returned.
  300. */
  301. struct dentry *tracefs_create_dir(const char *name, struct dentry *parent)
  302. {
  303. struct dentry *dentry = start_creating(name, parent);
  304. struct inode *inode;
  305. if (IS_ERR(dentry))
  306. return NULL;
  307. inode = tracefs_get_inode(dentry->d_sb);
  308. if (unlikely(!inode))
  309. return failed_creating(dentry);
  310. inode->i_mode = S_IFDIR | S_IRWXU | S_IRUGO | S_IXUGO;
  311. inode->i_op = &simple_dir_inode_operations;
  312. inode->i_fop = &simple_dir_operations;
  313. /* directory inodes start off with i_nlink == 2 (for "." entry) */
  314. inc_nlink(inode);
  315. d_instantiate(dentry, inode);
  316. inc_nlink(dentry->d_parent->d_inode);
  317. fsnotify_mkdir(dentry->d_parent->d_inode, dentry);
  318. return end_creating(dentry);
  319. }
  320. static inline int tracefs_positive(struct dentry *dentry)
  321. {
  322. return dentry->d_inode && !d_unhashed(dentry);
  323. }
  324. static int __tracefs_remove(struct dentry *dentry, struct dentry *parent)
  325. {
  326. int ret = 0;
  327. if (tracefs_positive(dentry)) {
  328. if (dentry->d_inode) {
  329. dget(dentry);
  330. switch (dentry->d_inode->i_mode & S_IFMT) {
  331. case S_IFDIR:
  332. ret = simple_rmdir(parent->d_inode, dentry);
  333. break;
  334. default:
  335. simple_unlink(parent->d_inode, dentry);
  336. break;
  337. }
  338. if (!ret)
  339. d_delete(dentry);
  340. dput(dentry);
  341. }
  342. }
  343. return ret;
  344. }
  345. /**
  346. * tracefs_remove - removes a file or directory from the tracefs filesystem
  347. * @dentry: a pointer to a the dentry of the file or directory to be
  348. * removed.
  349. *
  350. * This function removes a file or directory in tracefs that was previously
  351. * created with a call to another tracefs function (like
  352. * tracefs_create_file() or variants thereof.)
  353. */
  354. void tracefs_remove(struct dentry *dentry)
  355. {
  356. struct dentry *parent;
  357. int ret;
  358. if (IS_ERR_OR_NULL(dentry))
  359. return;
  360. parent = dentry->d_parent;
  361. if (!parent || !parent->d_inode)
  362. return;
  363. mutex_lock(&parent->d_inode->i_mutex);
  364. ret = __tracefs_remove(dentry, parent);
  365. mutex_unlock(&parent->d_inode->i_mutex);
  366. if (!ret)
  367. simple_release_fs(&tracefs_mount, &tracefs_mount_count);
  368. }
  369. /**
  370. * tracefs_remove_recursive - recursively removes a directory
  371. * @dentry: a pointer to a the dentry of the directory to be removed.
  372. *
  373. * This function recursively removes a directory tree in tracefs that
  374. * was previously created with a call to another tracefs function
  375. * (like tracefs_create_file() or variants thereof.)
  376. */
  377. void tracefs_remove_recursive(struct dentry *dentry)
  378. {
  379. struct dentry *child, *parent;
  380. if (IS_ERR_OR_NULL(dentry))
  381. return;
  382. parent = dentry->d_parent;
  383. if (!parent || !parent->d_inode)
  384. return;
  385. parent = dentry;
  386. down:
  387. mutex_lock(&parent->d_inode->i_mutex);
  388. loop:
  389. /*
  390. * The parent->d_subdirs is protected by the d_lock. Outside that
  391. * lock, the child can be unlinked and set to be freed which can
  392. * use the d_u.d_child as the rcu head and corrupt this list.
  393. */
  394. spin_lock(&parent->d_lock);
  395. list_for_each_entry(child, &parent->d_subdirs, d_child) {
  396. if (!tracefs_positive(child))
  397. continue;
  398. /* perhaps simple_empty(child) makes more sense */
  399. if (!list_empty(&child->d_subdirs)) {
  400. spin_unlock(&parent->d_lock);
  401. mutex_unlock(&parent->d_inode->i_mutex);
  402. parent = child;
  403. goto down;
  404. }
  405. spin_unlock(&parent->d_lock);
  406. if (!__tracefs_remove(child, parent))
  407. simple_release_fs(&tracefs_mount, &tracefs_mount_count);
  408. /*
  409. * The parent->d_lock protects agaist child from unlinking
  410. * from d_subdirs. When releasing the parent->d_lock we can
  411. * no longer trust that the next pointer is valid.
  412. * Restart the loop. We'll skip this one with the
  413. * tracefs_positive() check.
  414. */
  415. goto loop;
  416. }
  417. spin_unlock(&parent->d_lock);
  418. mutex_unlock(&parent->d_inode->i_mutex);
  419. child = parent;
  420. parent = parent->d_parent;
  421. mutex_lock(&parent->d_inode->i_mutex);
  422. if (child != dentry)
  423. /* go up */
  424. goto loop;
  425. if (!__tracefs_remove(child, parent))
  426. simple_release_fs(&tracefs_mount, &tracefs_mount_count);
  427. mutex_unlock(&parent->d_inode->i_mutex);
  428. }
  429. /**
  430. * tracefs_initialized - Tells whether tracefs has been registered
  431. */
  432. bool tracefs_initialized(void)
  433. {
  434. return tracefs_registered;
  435. }
  436. static int __init tracefs_init(void)
  437. {
  438. int retval;
  439. retval = register_filesystem(&trace_fs_type);
  440. if (!retval)
  441. tracefs_registered = true;
  442. return retval;
  443. }
  444. core_initcall(tracefs_init);