inode.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668
  1. /*
  2. * inode.c - part of debugfs, a tiny little debug file system
  3. *
  4. * Copyright (C) 2004 Greg Kroah-Hartman <greg@kroah.com>
  5. * Copyright (C) 2004 IBM Inc.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License version
  9. * 2 as published by the Free Software Foundation.
  10. *
  11. * debugfs is for people to use instead of /proc or /sys.
  12. * See Documentation/DocBook/kernel-api for more details.
  13. *
  14. */
  15. #include <linux/module.h>
  16. #include <linux/fs.h>
  17. #include <linux/mount.h>
  18. #include <linux/pagemap.h>
  19. #include <linux/init.h>
  20. #include <linux/kobject.h>
  21. #include <linux/namei.h>
  22. #include <linux/debugfs.h>
  23. #include <linux/fsnotify.h>
  24. #include <linux/string.h>
  25. #include <linux/seq_file.h>
  26. #include <linux/parser.h>
  27. #include <linux/magic.h>
  28. #include <linux/slab.h>
  29. #define DEBUGFS_DEFAULT_MODE 0755
  30. static struct vfsmount *debugfs_mount;
  31. static int debugfs_mount_count;
  32. static bool debugfs_registered;
  33. static struct inode *debugfs_get_inode(struct super_block *sb, umode_t mode, dev_t dev,
  34. void *data, const struct file_operations *fops)
  35. {
  36. struct inode *inode = new_inode(sb);
  37. if (inode) {
  38. inode->i_ino = get_next_ino();
  39. inode->i_mode = mode;
  40. inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  41. switch (mode & S_IFMT) {
  42. default:
  43. init_special_inode(inode, mode, dev);
  44. break;
  45. case S_IFREG:
  46. inode->i_fop = fops ? fops : &debugfs_file_operations;
  47. inode->i_private = data;
  48. break;
  49. case S_IFLNK:
  50. inode->i_op = &debugfs_link_operations;
  51. inode->i_fop = fops;
  52. inode->i_private = data;
  53. break;
  54. case S_IFDIR:
  55. inode->i_op = &simple_dir_inode_operations;
  56. inode->i_fop = fops ? fops : &simple_dir_operations;
  57. inode->i_private = data;
  58. /* directory inodes start off with i_nlink == 2
  59. * (for "." entry) */
  60. inc_nlink(inode);
  61. break;
  62. }
  63. }
  64. return inode;
  65. }
  66. /* SMP-safe */
  67. static int debugfs_mknod(struct inode *dir, struct dentry *dentry,
  68. umode_t mode, dev_t dev, void *data,
  69. const struct file_operations *fops)
  70. {
  71. struct inode *inode;
  72. int error = -EPERM;
  73. if (dentry->d_inode)
  74. return -EEXIST;
  75. inode = debugfs_get_inode(dir->i_sb, mode, dev, data, fops);
  76. if (inode) {
  77. d_instantiate(dentry, inode);
  78. dget(dentry);
  79. error = 0;
  80. }
  81. return error;
  82. }
  83. static int debugfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode,
  84. void *data, const struct file_operations *fops)
  85. {
  86. int res;
  87. mode = (mode & (S_IRWXUGO | S_ISVTX)) | S_IFDIR;
  88. res = debugfs_mknod(dir, dentry, mode, 0, data, fops);
  89. if (!res) {
  90. inc_nlink(dir);
  91. fsnotify_mkdir(dir, dentry);
  92. }
  93. return res;
  94. }
  95. static int debugfs_link(struct inode *dir, struct dentry *dentry, umode_t mode,
  96. void *data, const struct file_operations *fops)
  97. {
  98. mode = (mode & S_IALLUGO) | S_IFLNK;
  99. return debugfs_mknod(dir, dentry, mode, 0, data, fops);
  100. }
  101. static int debugfs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
  102. void *data, const struct file_operations *fops)
  103. {
  104. int res;
  105. mode = (mode & S_IALLUGO) | S_IFREG;
  106. res = debugfs_mknod(dir, dentry, mode, 0, data, fops);
  107. if (!res)
  108. fsnotify_create(dir, dentry);
  109. return res;
  110. }
  111. static inline int debugfs_positive(struct dentry *dentry)
  112. {
  113. return dentry->d_inode && !d_unhashed(dentry);
  114. }
  115. struct debugfs_mount_opts {
  116. uid_t uid;
  117. gid_t gid;
  118. umode_t mode;
  119. };
  120. enum {
  121. Opt_uid,
  122. Opt_gid,
  123. Opt_mode,
  124. Opt_err
  125. };
  126. static const match_table_t tokens = {
  127. {Opt_uid, "uid=%u"},
  128. {Opt_gid, "gid=%u"},
  129. {Opt_mode, "mode=%o"},
  130. {Opt_err, NULL}
  131. };
  132. struct debugfs_fs_info {
  133. struct debugfs_mount_opts mount_opts;
  134. };
  135. static int debugfs_parse_options(char *data, struct debugfs_mount_opts *opts)
  136. {
  137. substring_t args[MAX_OPT_ARGS];
  138. int option;
  139. int token;
  140. char *p;
  141. opts->mode = DEBUGFS_DEFAULT_MODE;
  142. while ((p = strsep(&data, ",")) != NULL) {
  143. if (!*p)
  144. continue;
  145. token = match_token(p, tokens, args);
  146. switch (token) {
  147. case Opt_uid:
  148. if (match_int(&args[0], &option))
  149. return -EINVAL;
  150. opts->uid = option;
  151. break;
  152. case Opt_gid:
  153. if (match_octal(&args[0], &option))
  154. return -EINVAL;
  155. opts->gid = option;
  156. break;
  157. case Opt_mode:
  158. if (match_octal(&args[0], &option))
  159. return -EINVAL;
  160. opts->mode = option & S_IALLUGO;
  161. break;
  162. /*
  163. * We might like to report bad mount options here;
  164. * but traditionally debugfs has ignored all mount options
  165. */
  166. }
  167. }
  168. return 0;
  169. }
  170. static int debugfs_apply_options(struct super_block *sb)
  171. {
  172. struct debugfs_fs_info *fsi = sb->s_fs_info;
  173. struct inode *inode = sb->s_root->d_inode;
  174. struct debugfs_mount_opts *opts = &fsi->mount_opts;
  175. inode->i_mode &= ~S_IALLUGO;
  176. inode->i_mode |= opts->mode;
  177. inode->i_uid = opts->uid;
  178. inode->i_gid = opts->gid;
  179. return 0;
  180. }
  181. static int debugfs_remount(struct super_block *sb, int *flags, char *data)
  182. {
  183. int err;
  184. struct debugfs_fs_info *fsi = sb->s_fs_info;
  185. err = debugfs_parse_options(data, &fsi->mount_opts);
  186. if (err)
  187. goto fail;
  188. debugfs_apply_options(sb);
  189. fail:
  190. return err;
  191. }
  192. static int debugfs_show_options(struct seq_file *m, struct dentry *root)
  193. {
  194. struct debugfs_fs_info *fsi = root->d_sb->s_fs_info;
  195. struct debugfs_mount_opts *opts = &fsi->mount_opts;
  196. if (opts->uid != 0)
  197. seq_printf(m, ",uid=%u", opts->uid);
  198. if (opts->gid != 0)
  199. seq_printf(m, ",gid=%u", opts->gid);
  200. if (opts->mode != DEBUGFS_DEFAULT_MODE)
  201. seq_printf(m, ",mode=%o", opts->mode);
  202. return 0;
  203. }
  204. static void debugfs_evict_inode(struct inode *inode)
  205. {
  206. truncate_inode_pages(&inode->i_data, 0);
  207. end_writeback(inode);
  208. if (S_ISLNK(inode->i_mode))
  209. kfree(inode->i_private);
  210. }
  211. static const struct super_operations debugfs_super_operations = {
  212. .statfs = simple_statfs,
  213. .remount_fs = debugfs_remount,
  214. .show_options = debugfs_show_options,
  215. .evict_inode = debugfs_evict_inode,
  216. };
  217. static int debug_fill_super(struct super_block *sb, void *data, int silent)
  218. {
  219. static struct tree_descr debug_files[] = {{""}};
  220. struct debugfs_fs_info *fsi;
  221. int err;
  222. save_mount_options(sb, data);
  223. fsi = kzalloc(sizeof(struct debugfs_fs_info), GFP_KERNEL);
  224. sb->s_fs_info = fsi;
  225. if (!fsi) {
  226. err = -ENOMEM;
  227. goto fail;
  228. }
  229. err = debugfs_parse_options(data, &fsi->mount_opts);
  230. if (err)
  231. goto fail;
  232. err = simple_fill_super(sb, DEBUGFS_MAGIC, debug_files);
  233. if (err)
  234. goto fail;
  235. sb->s_op = &debugfs_super_operations;
  236. debugfs_apply_options(sb);
  237. return 0;
  238. fail:
  239. kfree(fsi);
  240. sb->s_fs_info = NULL;
  241. return err;
  242. }
  243. static struct dentry *debug_mount(struct file_system_type *fs_type,
  244. int flags, const char *dev_name,
  245. void *data)
  246. {
  247. return mount_single(fs_type, flags, data, debug_fill_super);
  248. }
  249. static struct file_system_type debug_fs_type = {
  250. .owner = THIS_MODULE,
  251. .name = "debugfs",
  252. .mount = debug_mount,
  253. .kill_sb = kill_litter_super,
  254. };
  255. MODULE_ALIAS_FS("debugfs");
  256. static int debugfs_create_by_name(const char *name, umode_t mode,
  257. struct dentry *parent,
  258. struct dentry **dentry,
  259. void *data,
  260. const struct file_operations *fops)
  261. {
  262. int error = 0;
  263. /* If the parent is not specified, we create it in the root.
  264. * We need the root dentry to do this, which is in the super
  265. * block. A pointer to that is in the struct vfsmount that we
  266. * have around.
  267. */
  268. if (!parent)
  269. parent = debugfs_mount->mnt_root;
  270. *dentry = NULL;
  271. mutex_lock(&parent->d_inode->i_mutex);
  272. *dentry = lookup_one_len(name, parent, strlen(name));
  273. if (!IS_ERR(*dentry)) {
  274. switch (mode & S_IFMT) {
  275. case S_IFDIR:
  276. error = debugfs_mkdir(parent->d_inode, *dentry, mode,
  277. data, fops);
  278. break;
  279. case S_IFLNK:
  280. error = debugfs_link(parent->d_inode, *dentry, mode,
  281. data, fops);
  282. break;
  283. default:
  284. error = debugfs_create(parent->d_inode, *dentry, mode,
  285. data, fops);
  286. break;
  287. }
  288. dput(*dentry);
  289. } else
  290. error = PTR_ERR(*dentry);
  291. mutex_unlock(&parent->d_inode->i_mutex);
  292. return error;
  293. }
  294. /**
  295. * debugfs_create_file - create a file in the debugfs filesystem
  296. * @name: a pointer to a string containing the name of the file to create.
  297. * @mode: the permission that the file should have.
  298. * @parent: a pointer to the parent dentry for this file. This should be a
  299. * directory dentry if set. If this paramater is NULL, then the
  300. * file will be created in the root of the debugfs filesystem.
  301. * @data: a pointer to something that the caller will want to get to later
  302. * on. The inode.i_private pointer will point to this value on
  303. * the open() call.
  304. * @fops: a pointer to a struct file_operations that should be used for
  305. * this file.
  306. *
  307. * This is the basic "create a file" function for debugfs. It allows for a
  308. * wide range of flexibility in creating a file, or a directory (if you want
  309. * to create a directory, the debugfs_create_dir() function is
  310. * recommended to be used instead.)
  311. *
  312. * This function will return a pointer to a dentry if it succeeds. This
  313. * pointer must be passed to the debugfs_remove() function when the file is
  314. * to be removed (no automatic cleanup happens if your module is unloaded,
  315. * you are responsible here.) If an error occurs, %NULL will be returned.
  316. *
  317. * If debugfs is not enabled in the kernel, the value -%ENODEV will be
  318. * returned.
  319. */
  320. struct dentry *debugfs_create_file(const char *name, umode_t mode,
  321. struct dentry *parent, void *data,
  322. const struct file_operations *fops)
  323. {
  324. struct dentry *dentry = NULL;
  325. int error;
  326. pr_debug("debugfs: creating file '%s'\n",name);
  327. error = simple_pin_fs(&debug_fs_type, &debugfs_mount,
  328. &debugfs_mount_count);
  329. if (error)
  330. goto exit;
  331. error = debugfs_create_by_name(name, mode, parent, &dentry,
  332. data, fops);
  333. if (error) {
  334. dentry = NULL;
  335. simple_release_fs(&debugfs_mount, &debugfs_mount_count);
  336. goto exit;
  337. }
  338. exit:
  339. return dentry;
  340. }
  341. EXPORT_SYMBOL_GPL(debugfs_create_file);
  342. /**
  343. * debugfs_create_dir - create a directory in the debugfs filesystem
  344. * @name: a pointer to a string containing the name of the directory to
  345. * create.
  346. * @parent: a pointer to the parent dentry for this file. This should be a
  347. * directory dentry if set. If this paramater is NULL, then the
  348. * directory will be created in the root of the debugfs filesystem.
  349. *
  350. * This function creates a directory in debugfs with the given name.
  351. *
  352. * This function will return a pointer to a dentry if it succeeds. This
  353. * pointer must be passed to the debugfs_remove() function when the file is
  354. * to be removed (no automatic cleanup happens if your module is unloaded,
  355. * you are responsible here.) If an error occurs, %NULL will be returned.
  356. *
  357. * If debugfs is not enabled in the kernel, the value -%ENODEV will be
  358. * returned.
  359. */
  360. struct dentry *debugfs_create_dir(const char *name, struct dentry *parent)
  361. {
  362. return debugfs_create_file(name,
  363. S_IFDIR | S_IRWXU | S_IRUGO | S_IXUGO,
  364. parent, NULL, NULL);
  365. }
  366. EXPORT_SYMBOL_GPL(debugfs_create_dir);
  367. /**
  368. * debugfs_create_symlink- create a symbolic link in the debugfs filesystem
  369. * @name: a pointer to a string containing the name of the symbolic link to
  370. * create.
  371. * @parent: a pointer to the parent dentry for this symbolic link. This
  372. * should be a directory dentry if set. If this paramater is NULL,
  373. * then the symbolic link will be created in the root of the debugfs
  374. * filesystem.
  375. * @target: a pointer to a string containing the path to the target of the
  376. * symbolic link.
  377. *
  378. * This function creates a symbolic link with the given name in debugfs that
  379. * links to the given target path.
  380. *
  381. * This function will return a pointer to a dentry if it succeeds. This
  382. * pointer must be passed to the debugfs_remove() function when the symbolic
  383. * link is to be removed (no automatic cleanup happens if your module is
  384. * unloaded, you are responsible here.) If an error occurs, %NULL will be
  385. * returned.
  386. *
  387. * If debugfs is not enabled in the kernel, the value -%ENODEV will be
  388. * returned.
  389. */
  390. struct dentry *debugfs_create_symlink(const char *name, struct dentry *parent,
  391. const char *target)
  392. {
  393. struct dentry *result;
  394. char *link;
  395. link = kstrdup(target, GFP_KERNEL);
  396. if (!link)
  397. return NULL;
  398. result = debugfs_create_file(name, S_IFLNK | S_IRWXUGO, parent, link,
  399. NULL);
  400. if (!result)
  401. kfree(link);
  402. return result;
  403. }
  404. EXPORT_SYMBOL_GPL(debugfs_create_symlink);
  405. static int __debugfs_remove(struct dentry *dentry, struct dentry *parent)
  406. {
  407. int ret = 0;
  408. if (debugfs_positive(dentry)) {
  409. dget(dentry);
  410. if (S_ISDIR(dentry->d_inode->i_mode))
  411. ret = simple_rmdir(parent->d_inode, dentry);
  412. else
  413. simple_unlink(parent->d_inode, dentry);
  414. if (!ret)
  415. d_delete(dentry);
  416. dput(dentry);
  417. }
  418. return ret;
  419. }
  420. /**
  421. * debugfs_remove - removes a file or directory from the debugfs filesystem
  422. * @dentry: a pointer to a the dentry of the file or directory to be
  423. * removed.
  424. *
  425. * This function removes a file or directory in debugfs that was previously
  426. * created with a call to another debugfs function (like
  427. * debugfs_create_file() or variants thereof.)
  428. *
  429. * This function is required to be called in order for the file to be
  430. * removed, no automatic cleanup of files will happen when a module is
  431. * removed, you are responsible here.
  432. */
  433. void debugfs_remove(struct dentry *dentry)
  434. {
  435. struct dentry *parent;
  436. int ret;
  437. if (!dentry)
  438. return;
  439. parent = dentry->d_parent;
  440. if (!parent || !parent->d_inode)
  441. return;
  442. mutex_lock(&parent->d_inode->i_mutex);
  443. ret = __debugfs_remove(dentry, parent);
  444. mutex_unlock(&parent->d_inode->i_mutex);
  445. if (!ret)
  446. simple_release_fs(&debugfs_mount, &debugfs_mount_count);
  447. }
  448. EXPORT_SYMBOL_GPL(debugfs_remove);
  449. /**
  450. * debugfs_remove_recursive - recursively removes a directory
  451. * @dentry: a pointer to a the dentry of the directory to be removed.
  452. *
  453. * This function recursively removes a directory tree in debugfs that
  454. * was previously created with a call to another debugfs function
  455. * (like debugfs_create_file() or variants thereof.)
  456. *
  457. * This function is required to be called in order for the file to be
  458. * removed, no automatic cleanup of files will happen when a module is
  459. * removed, you are responsible here.
  460. */
  461. void debugfs_remove_recursive(struct dentry *dentry)
  462. {
  463. struct dentry *child, *next, *parent;
  464. if (!dentry)
  465. return;
  466. parent = dentry->d_parent;
  467. if (!parent || !parent->d_inode)
  468. return;
  469. parent = dentry;
  470. down:
  471. mutex_lock(&parent->d_inode->i_mutex);
  472. list_for_each_entry_safe(child, next, &parent->d_subdirs, d_child) {
  473. if (!debugfs_positive(child))
  474. continue;
  475. /* perhaps simple_empty(child) makes more sense */
  476. if (!list_empty(&child->d_subdirs)) {
  477. mutex_unlock(&parent->d_inode->i_mutex);
  478. parent = child;
  479. goto down;
  480. }
  481. up:
  482. if (!__debugfs_remove(child, parent))
  483. simple_release_fs(&debugfs_mount, &debugfs_mount_count);
  484. }
  485. mutex_unlock(&parent->d_inode->i_mutex);
  486. child = parent;
  487. parent = parent->d_parent;
  488. mutex_lock(&parent->d_inode->i_mutex);
  489. if (child != dentry) {
  490. next = list_entry(child->d_child.next, struct dentry,
  491. d_child);
  492. goto up;
  493. }
  494. if (!__debugfs_remove(child, parent))
  495. simple_release_fs(&debugfs_mount, &debugfs_mount_count);
  496. mutex_unlock(&parent->d_inode->i_mutex);
  497. }
  498. EXPORT_SYMBOL_GPL(debugfs_remove_recursive);
  499. /**
  500. * debugfs_rename - rename a file/directory in the debugfs filesystem
  501. * @old_dir: a pointer to the parent dentry for the renamed object. This
  502. * should be a directory dentry.
  503. * @old_dentry: dentry of an object to be renamed.
  504. * @new_dir: a pointer to the parent dentry where the object should be
  505. * moved. This should be a directory dentry.
  506. * @new_name: a pointer to a string containing the target name.
  507. *
  508. * This function renames a file/directory in debugfs. The target must not
  509. * exist for rename to succeed.
  510. *
  511. * This function will return a pointer to old_dentry (which is updated to
  512. * reflect renaming) if it succeeds. If an error occurs, %NULL will be
  513. * returned.
  514. *
  515. * If debugfs is not enabled in the kernel, the value -%ENODEV will be
  516. * returned.
  517. */
  518. struct dentry *debugfs_rename(struct dentry *old_dir, struct dentry *old_dentry,
  519. struct dentry *new_dir, const char *new_name)
  520. {
  521. int error;
  522. struct dentry *dentry = NULL, *trap;
  523. struct name_snapshot old_name;
  524. trap = lock_rename(new_dir, old_dir);
  525. /* Source or destination directories don't exist? */
  526. if (!old_dir->d_inode || !new_dir->d_inode)
  527. goto exit;
  528. /* Source does not exist, cyclic rename, or mountpoint? */
  529. if (!old_dentry->d_inode || old_dentry == trap ||
  530. d_mountpoint(old_dentry))
  531. goto exit;
  532. dentry = lookup_one_len(new_name, new_dir, strlen(new_name));
  533. /* Lookup failed, cyclic rename or target exists? */
  534. if (IS_ERR(dentry) || dentry == trap || dentry->d_inode)
  535. goto exit;
  536. take_dentry_name_snapshot(&old_name, old_dentry);
  537. error = simple_rename(old_dir->d_inode, old_dentry, new_dir->d_inode,
  538. dentry);
  539. if (error) {
  540. release_dentry_name_snapshot(&old_name);
  541. goto exit;
  542. }
  543. d_move(old_dentry, dentry);
  544. fsnotify_move(old_dir->d_inode, new_dir->d_inode, old_name.name,
  545. S_ISDIR(old_dentry->d_inode->i_mode),
  546. NULL, old_dentry);
  547. release_dentry_name_snapshot(&old_name);
  548. unlock_rename(new_dir, old_dir);
  549. dput(dentry);
  550. return old_dentry;
  551. exit:
  552. if (dentry && !IS_ERR(dentry))
  553. dput(dentry);
  554. unlock_rename(new_dir, old_dir);
  555. return NULL;
  556. }
  557. EXPORT_SYMBOL_GPL(debugfs_rename);
  558. /**
  559. * debugfs_initialized - Tells whether debugfs has been registered
  560. */
  561. bool debugfs_initialized(void)
  562. {
  563. return debugfs_registered;
  564. }
  565. EXPORT_SYMBOL_GPL(debugfs_initialized);
  566. static struct kobject *debug_kobj;
  567. static int __init debugfs_init(void)
  568. {
  569. int retval;
  570. debug_kobj = kobject_create_and_add("debug", kernel_kobj);
  571. if (!debug_kobj)
  572. return -EINVAL;
  573. retval = register_filesystem(&debug_fs_type);
  574. if (retval)
  575. kobject_put(debug_kobj);
  576. else
  577. debugfs_registered = true;
  578. return retval;
  579. }
  580. core_initcall(debugfs_init);