inode.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669
  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. sync_filesystem(sb);
  186. err = debugfs_parse_options(data, &fsi->mount_opts);
  187. if (err)
  188. goto fail;
  189. debugfs_apply_options(sb);
  190. fail:
  191. return err;
  192. }
  193. static int debugfs_show_options(struct seq_file *m, struct dentry *root)
  194. {
  195. struct debugfs_fs_info *fsi = root->d_sb->s_fs_info;
  196. struct debugfs_mount_opts *opts = &fsi->mount_opts;
  197. if (opts->uid != 0)
  198. seq_printf(m, ",uid=%u", opts->uid);
  199. if (opts->gid != 0)
  200. seq_printf(m, ",gid=%u", opts->gid);
  201. if (opts->mode != DEBUGFS_DEFAULT_MODE)
  202. seq_printf(m, ",mode=%o", opts->mode);
  203. return 0;
  204. }
  205. static void debugfs_evict_inode(struct inode *inode)
  206. {
  207. truncate_inode_pages(&inode->i_data, 0);
  208. end_writeback(inode);
  209. if (S_ISLNK(inode->i_mode))
  210. kfree(inode->i_private);
  211. }
  212. static const struct super_operations debugfs_super_operations = {
  213. .statfs = simple_statfs,
  214. .remount_fs = debugfs_remount,
  215. .show_options = debugfs_show_options,
  216. .evict_inode = debugfs_evict_inode,
  217. };
  218. static int debug_fill_super(struct super_block *sb, void *data, int silent)
  219. {
  220. static struct tree_descr debug_files[] = {{""}};
  221. struct debugfs_fs_info *fsi;
  222. int err;
  223. save_mount_options(sb, data);
  224. fsi = kzalloc(sizeof(struct debugfs_fs_info), GFP_KERNEL);
  225. sb->s_fs_info = fsi;
  226. if (!fsi) {
  227. err = -ENOMEM;
  228. goto fail;
  229. }
  230. err = debugfs_parse_options(data, &fsi->mount_opts);
  231. if (err)
  232. goto fail;
  233. err = simple_fill_super(sb, DEBUGFS_MAGIC, debug_files);
  234. if (err)
  235. goto fail;
  236. sb->s_op = &debugfs_super_operations;
  237. debugfs_apply_options(sb);
  238. return 0;
  239. fail:
  240. kfree(fsi);
  241. sb->s_fs_info = NULL;
  242. return err;
  243. }
  244. static struct dentry *debug_mount(struct file_system_type *fs_type,
  245. int flags, const char *dev_name,
  246. void *data)
  247. {
  248. return mount_single(fs_type, flags, data, debug_fill_super);
  249. }
  250. static struct file_system_type debug_fs_type = {
  251. .owner = THIS_MODULE,
  252. .name = "debugfs",
  253. .mount = debug_mount,
  254. .kill_sb = kill_litter_super,
  255. };
  256. MODULE_ALIAS_FS("debugfs");
  257. static int debugfs_create_by_name(const char *name, umode_t mode,
  258. struct dentry *parent,
  259. struct dentry **dentry,
  260. void *data,
  261. const struct file_operations *fops)
  262. {
  263. int error = 0;
  264. /* If the parent is not specified, we create it in the root.
  265. * We need the root dentry to do this, which is in the super
  266. * block. A pointer to that is in the struct vfsmount that we
  267. * have around.
  268. */
  269. if (!parent)
  270. parent = debugfs_mount->mnt_root;
  271. *dentry = NULL;
  272. mutex_lock(&parent->d_inode->i_mutex);
  273. *dentry = lookup_one_len(name, parent, strlen(name));
  274. if (!IS_ERR(*dentry)) {
  275. switch (mode & S_IFMT) {
  276. case S_IFDIR:
  277. error = debugfs_mkdir(parent->d_inode, *dentry, mode,
  278. data, fops);
  279. break;
  280. case S_IFLNK:
  281. error = debugfs_link(parent->d_inode, *dentry, mode,
  282. data, fops);
  283. break;
  284. default:
  285. error = debugfs_create(parent->d_inode, *dentry, mode,
  286. data, fops);
  287. break;
  288. }
  289. dput(*dentry);
  290. } else
  291. error = PTR_ERR(*dentry);
  292. mutex_unlock(&parent->d_inode->i_mutex);
  293. return error;
  294. }
  295. /**
  296. * debugfs_create_file - create a file in the debugfs filesystem
  297. * @name: a pointer to a string containing the name of the file to create.
  298. * @mode: the permission that the file should have.
  299. * @parent: a pointer to the parent dentry for this file. This should be a
  300. * directory dentry if set. If this paramater is NULL, then the
  301. * file will be created in the root of the debugfs filesystem.
  302. * @data: a pointer to something that the caller will want to get to later
  303. * on. The inode.i_private pointer will point to this value on
  304. * the open() call.
  305. * @fops: a pointer to a struct file_operations that should be used for
  306. * this file.
  307. *
  308. * This is the basic "create a file" function for debugfs. It allows for a
  309. * wide range of flexibility in creating a file, or a directory (if you want
  310. * to create a directory, the debugfs_create_dir() function is
  311. * recommended to be used instead.)
  312. *
  313. * This function will return a pointer to a dentry if it succeeds. This
  314. * pointer must be passed to the debugfs_remove() function when the file is
  315. * to be removed (no automatic cleanup happens if your module is unloaded,
  316. * you are responsible here.) If an error occurs, %NULL will be returned.
  317. *
  318. * If debugfs is not enabled in the kernel, the value -%ENODEV will be
  319. * returned.
  320. */
  321. struct dentry *debugfs_create_file(const char *name, umode_t mode,
  322. struct dentry *parent, void *data,
  323. const struct file_operations *fops)
  324. {
  325. struct dentry *dentry = NULL;
  326. int error;
  327. pr_debug("debugfs: creating file '%s'\n",name);
  328. error = simple_pin_fs(&debug_fs_type, &debugfs_mount,
  329. &debugfs_mount_count);
  330. if (error)
  331. goto exit;
  332. error = debugfs_create_by_name(name, mode, parent, &dentry,
  333. data, fops);
  334. if (error) {
  335. dentry = NULL;
  336. simple_release_fs(&debugfs_mount, &debugfs_mount_count);
  337. goto exit;
  338. }
  339. exit:
  340. return dentry;
  341. }
  342. EXPORT_SYMBOL_GPL(debugfs_create_file);
  343. /**
  344. * debugfs_create_dir - create a directory in the debugfs filesystem
  345. * @name: a pointer to a string containing the name of the directory to
  346. * create.
  347. * @parent: a pointer to the parent dentry for this file. This should be a
  348. * directory dentry if set. If this paramater is NULL, then the
  349. * directory will be created in the root of the debugfs filesystem.
  350. *
  351. * This function creates a directory in debugfs with the given name.
  352. *
  353. * This function will return a pointer to a dentry if it succeeds. This
  354. * pointer must be passed to the debugfs_remove() function when the file is
  355. * to be removed (no automatic cleanup happens if your module is unloaded,
  356. * you are responsible here.) If an error occurs, %NULL will be returned.
  357. *
  358. * If debugfs is not enabled in the kernel, the value -%ENODEV will be
  359. * returned.
  360. */
  361. struct dentry *debugfs_create_dir(const char *name, struct dentry *parent)
  362. {
  363. return debugfs_create_file(name,
  364. S_IFDIR | S_IRWXU | S_IRUGO | S_IXUGO,
  365. parent, NULL, NULL);
  366. }
  367. EXPORT_SYMBOL_GPL(debugfs_create_dir);
  368. /**
  369. * debugfs_create_symlink- create a symbolic link in the debugfs filesystem
  370. * @name: a pointer to a string containing the name of the symbolic link to
  371. * create.
  372. * @parent: a pointer to the parent dentry for this symbolic link. This
  373. * should be a directory dentry if set. If this paramater is NULL,
  374. * then the symbolic link will be created in the root of the debugfs
  375. * filesystem.
  376. * @target: a pointer to a string containing the path to the target of the
  377. * symbolic link.
  378. *
  379. * This function creates a symbolic link with the given name in debugfs that
  380. * links to the given target path.
  381. *
  382. * This function will return a pointer to a dentry if it succeeds. This
  383. * pointer must be passed to the debugfs_remove() function when the symbolic
  384. * link is to be removed (no automatic cleanup happens if your module is
  385. * unloaded, you are responsible here.) If an error occurs, %NULL will be
  386. * returned.
  387. *
  388. * If debugfs is not enabled in the kernel, the value -%ENODEV will be
  389. * returned.
  390. */
  391. struct dentry *debugfs_create_symlink(const char *name, struct dentry *parent,
  392. const char *target)
  393. {
  394. struct dentry *result;
  395. char *link;
  396. link = kstrdup(target, GFP_KERNEL);
  397. if (!link)
  398. return NULL;
  399. result = debugfs_create_file(name, S_IFLNK | S_IRWXUGO, parent, link,
  400. NULL);
  401. if (!result)
  402. kfree(link);
  403. return result;
  404. }
  405. EXPORT_SYMBOL_GPL(debugfs_create_symlink);
  406. static int __debugfs_remove(struct dentry *dentry, struct dentry *parent)
  407. {
  408. int ret = 0;
  409. if (debugfs_positive(dentry)) {
  410. dget(dentry);
  411. if (S_ISDIR(dentry->d_inode->i_mode))
  412. ret = simple_rmdir(parent->d_inode, dentry);
  413. else
  414. simple_unlink(parent->d_inode, dentry);
  415. if (!ret)
  416. d_delete(dentry);
  417. dput(dentry);
  418. }
  419. return ret;
  420. }
  421. /**
  422. * debugfs_remove - removes a file or directory from the debugfs filesystem
  423. * @dentry: a pointer to a the dentry of the file or directory to be
  424. * removed.
  425. *
  426. * This function removes a file or directory in debugfs that was previously
  427. * created with a call to another debugfs function (like
  428. * debugfs_create_file() or variants thereof.)
  429. *
  430. * This function is required to be called in order for the file to be
  431. * removed, no automatic cleanup of files will happen when a module is
  432. * removed, you are responsible here.
  433. */
  434. void debugfs_remove(struct dentry *dentry)
  435. {
  436. struct dentry *parent;
  437. int ret;
  438. if (!dentry)
  439. return;
  440. parent = dentry->d_parent;
  441. if (!parent || !parent->d_inode)
  442. return;
  443. mutex_lock(&parent->d_inode->i_mutex);
  444. ret = __debugfs_remove(dentry, parent);
  445. mutex_unlock(&parent->d_inode->i_mutex);
  446. if (!ret)
  447. simple_release_fs(&debugfs_mount, &debugfs_mount_count);
  448. }
  449. EXPORT_SYMBOL_GPL(debugfs_remove);
  450. /**
  451. * debugfs_remove_recursive - recursively removes a directory
  452. * @dentry: a pointer to a the dentry of the directory to be removed.
  453. *
  454. * This function recursively removes a directory tree in debugfs that
  455. * was previously created with a call to another debugfs function
  456. * (like debugfs_create_file() or variants thereof.)
  457. *
  458. * This function is required to be called in order for the file to be
  459. * removed, no automatic cleanup of files will happen when a module is
  460. * removed, you are responsible here.
  461. */
  462. void debugfs_remove_recursive(struct dentry *dentry)
  463. {
  464. struct dentry *child, *next, *parent;
  465. if (!dentry)
  466. return;
  467. parent = dentry->d_parent;
  468. if (!parent || !parent->d_inode)
  469. return;
  470. parent = dentry;
  471. down:
  472. mutex_lock(&parent->d_inode->i_mutex);
  473. list_for_each_entry_safe(child, next, &parent->d_subdirs, d_child) {
  474. if (!debugfs_positive(child))
  475. continue;
  476. /* perhaps simple_empty(child) makes more sense */
  477. if (!list_empty(&child->d_subdirs)) {
  478. mutex_unlock(&parent->d_inode->i_mutex);
  479. parent = child;
  480. goto down;
  481. }
  482. up:
  483. if (!__debugfs_remove(child, parent))
  484. simple_release_fs(&debugfs_mount, &debugfs_mount_count);
  485. }
  486. mutex_unlock(&parent->d_inode->i_mutex);
  487. child = parent;
  488. parent = parent->d_parent;
  489. mutex_lock(&parent->d_inode->i_mutex);
  490. if (child != dentry) {
  491. next = list_entry(child->d_child.next, struct dentry,
  492. d_child);
  493. goto up;
  494. }
  495. if (!__debugfs_remove(child, parent))
  496. simple_release_fs(&debugfs_mount, &debugfs_mount_count);
  497. mutex_unlock(&parent->d_inode->i_mutex);
  498. }
  499. EXPORT_SYMBOL_GPL(debugfs_remove_recursive);
  500. /**
  501. * debugfs_rename - rename a file/directory in the debugfs filesystem
  502. * @old_dir: a pointer to the parent dentry for the renamed object. This
  503. * should be a directory dentry.
  504. * @old_dentry: dentry of an object to be renamed.
  505. * @new_dir: a pointer to the parent dentry where the object should be
  506. * moved. This should be a directory dentry.
  507. * @new_name: a pointer to a string containing the target name.
  508. *
  509. * This function renames a file/directory in debugfs. The target must not
  510. * exist for rename to succeed.
  511. *
  512. * This function will return a pointer to old_dentry (which is updated to
  513. * reflect renaming) if it succeeds. If an error occurs, %NULL will be
  514. * returned.
  515. *
  516. * If debugfs is not enabled in the kernel, the value -%ENODEV will be
  517. * returned.
  518. */
  519. struct dentry *debugfs_rename(struct dentry *old_dir, struct dentry *old_dentry,
  520. struct dentry *new_dir, const char *new_name)
  521. {
  522. int error;
  523. struct dentry *dentry = NULL, *trap;
  524. struct name_snapshot old_name;
  525. trap = lock_rename(new_dir, old_dir);
  526. /* Source or destination directories don't exist? */
  527. if (!old_dir->d_inode || !new_dir->d_inode)
  528. goto exit;
  529. /* Source does not exist, cyclic rename, or mountpoint? */
  530. if (!old_dentry->d_inode || old_dentry == trap ||
  531. d_mountpoint(old_dentry))
  532. goto exit;
  533. dentry = lookup_one_len(new_name, new_dir, strlen(new_name));
  534. /* Lookup failed, cyclic rename or target exists? */
  535. if (IS_ERR(dentry) || dentry == trap || dentry->d_inode)
  536. goto exit;
  537. take_dentry_name_snapshot(&old_name, old_dentry);
  538. error = simple_rename(old_dir->d_inode, old_dentry, new_dir->d_inode,
  539. dentry);
  540. if (error) {
  541. release_dentry_name_snapshot(&old_name);
  542. goto exit;
  543. }
  544. d_move(old_dentry, dentry);
  545. fsnotify_move(old_dir->d_inode, new_dir->d_inode, old_name.name,
  546. S_ISDIR(old_dentry->d_inode->i_mode),
  547. NULL, old_dentry);
  548. release_dentry_name_snapshot(&old_name);
  549. unlock_rename(new_dir, old_dir);
  550. dput(dentry);
  551. return old_dentry;
  552. exit:
  553. if (dentry && !IS_ERR(dentry))
  554. dput(dentry);
  555. unlock_rename(new_dir, old_dir);
  556. return NULL;
  557. }
  558. EXPORT_SYMBOL_GPL(debugfs_rename);
  559. /**
  560. * debugfs_initialized - Tells whether debugfs has been registered
  561. */
  562. bool debugfs_initialized(void)
  563. {
  564. return debugfs_registered;
  565. }
  566. EXPORT_SYMBOL_GPL(debugfs_initialized);
  567. static struct kobject *debug_kobj;
  568. static int __init debugfs_init(void)
  569. {
  570. int retval;
  571. debug_kobj = kobject_create_and_add("debug", kernel_kobj);
  572. if (!debug_kobj)
  573. return -EINVAL;
  574. retval = register_filesystem(&debug_fs_type);
  575. if (retval)
  576. kobject_put(debug_kobj);
  577. else
  578. debugfs_registered = true;
  579. return retval;
  580. }
  581. core_initcall(debugfs_init);