inode.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  1. /*
  2. * file.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. /* uncomment to get debug messages from the debug filesystem, ah the irony. */
  16. /* #define DEBUG */
  17. #include <linux/module.h>
  18. #include <linux/fs.h>
  19. #include <linux/mount.h>
  20. #include <linux/pagemap.h>
  21. #include <linux/init.h>
  22. #include <linux/kobject.h>
  23. #include <linux/namei.h>
  24. #include <linux/debugfs.h>
  25. #include <linux/fsnotify.h>
  26. #include <linux/string.h>
  27. #include <linux/magic.h>
  28. static struct vfsmount *debugfs_mount;
  29. static int debugfs_mount_count;
  30. static bool debugfs_registered;
  31. static struct inode *debugfs_get_inode(struct super_block *sb, int mode, dev_t dev)
  32. {
  33. struct inode *inode = new_inode(sb);
  34. if (inode) {
  35. inode->i_mode = mode;
  36. inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  37. switch (mode & S_IFMT) {
  38. default:
  39. init_special_inode(inode, mode, dev);
  40. break;
  41. case S_IFREG:
  42. inode->i_fop = &debugfs_file_operations;
  43. break;
  44. case S_IFLNK:
  45. inode->i_op = &debugfs_link_operations;
  46. break;
  47. case S_IFDIR:
  48. inode->i_op = &simple_dir_inode_operations;
  49. inode->i_fop = &simple_dir_operations;
  50. /* directory inodes start off with i_nlink == 2
  51. * (for "." entry) */
  52. inc_nlink(inode);
  53. break;
  54. }
  55. }
  56. return inode;
  57. }
  58. /* SMP-safe */
  59. static int debugfs_mknod(struct inode *dir, struct dentry *dentry,
  60. int mode, dev_t dev)
  61. {
  62. struct inode *inode;
  63. int error = -EPERM;
  64. if (dentry->d_inode)
  65. return -EEXIST;
  66. inode = debugfs_get_inode(dir->i_sb, mode, dev);
  67. if (inode) {
  68. d_instantiate(dentry, inode);
  69. dget(dentry);
  70. error = 0;
  71. }
  72. return error;
  73. }
  74. static int debugfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  75. {
  76. int res;
  77. mode = (mode & (S_IRWXUGO | S_ISVTX)) | S_IFDIR;
  78. res = debugfs_mknod(dir, dentry, mode, 0);
  79. if (!res) {
  80. inc_nlink(dir);
  81. fsnotify_mkdir(dir, dentry);
  82. }
  83. return res;
  84. }
  85. static int debugfs_link(struct inode *dir, struct dentry *dentry, int mode)
  86. {
  87. mode = (mode & S_IALLUGO) | S_IFLNK;
  88. return debugfs_mknod(dir, dentry, mode, 0);
  89. }
  90. static int debugfs_create(struct inode *dir, struct dentry *dentry, int mode)
  91. {
  92. int res;
  93. mode = (mode & S_IALLUGO) | S_IFREG;
  94. res = debugfs_mknod(dir, dentry, mode, 0);
  95. if (!res)
  96. fsnotify_create(dir, dentry);
  97. return res;
  98. }
  99. static inline int debugfs_positive(struct dentry *dentry)
  100. {
  101. return dentry->d_inode && !d_unhashed(dentry);
  102. }
  103. static int debug_fill_super(struct super_block *sb, void *data, int silent)
  104. {
  105. static struct tree_descr debug_files[] = {{""}};
  106. return simple_fill_super(sb, DEBUGFS_MAGIC, debug_files);
  107. }
  108. static int debug_get_sb(struct file_system_type *fs_type,
  109. int flags, const char *dev_name,
  110. void *data, struct vfsmount *mnt)
  111. {
  112. return get_sb_single(fs_type, flags, data, debug_fill_super, mnt);
  113. }
  114. static struct file_system_type debug_fs_type = {
  115. .owner = THIS_MODULE,
  116. .name = "debugfs",
  117. .get_sb = debug_get_sb,
  118. .kill_sb = kill_litter_super,
  119. };
  120. static int debugfs_create_by_name(const char *name, mode_t mode,
  121. struct dentry *parent,
  122. struct dentry **dentry)
  123. {
  124. int error = 0;
  125. /* If the parent is not specified, we create it in the root.
  126. * We need the root dentry to do this, which is in the super
  127. * block. A pointer to that is in the struct vfsmount that we
  128. * have around.
  129. */
  130. if (!parent) {
  131. if (debugfs_mount && debugfs_mount->mnt_sb) {
  132. parent = debugfs_mount->mnt_sb->s_root;
  133. }
  134. }
  135. if (!parent) {
  136. pr_debug("debugfs: Ah! can not find a parent!\n");
  137. return -EFAULT;
  138. }
  139. *dentry = NULL;
  140. mutex_lock(&parent->d_inode->i_mutex);
  141. *dentry = lookup_one_len(name, parent, strlen(name));
  142. if (!IS_ERR(*dentry)) {
  143. switch (mode & S_IFMT) {
  144. case S_IFDIR:
  145. error = debugfs_mkdir(parent->d_inode, *dentry, mode);
  146. break;
  147. case S_IFLNK:
  148. error = debugfs_link(parent->d_inode, *dentry, mode);
  149. break;
  150. default:
  151. error = debugfs_create(parent->d_inode, *dentry, mode);
  152. break;
  153. }
  154. dput(*dentry);
  155. } else
  156. error = PTR_ERR(*dentry);
  157. mutex_unlock(&parent->d_inode->i_mutex);
  158. return error;
  159. }
  160. /**
  161. * debugfs_create_file - create a file in the debugfs filesystem
  162. * @name: a pointer to a string containing the name of the file to create.
  163. * @mode: the permission that the file should have
  164. * @parent: a pointer to the parent dentry for this file. This should be a
  165. * directory dentry if set. If this paramater is NULL, then the
  166. * file will be created in the root of the debugfs filesystem.
  167. * @data: a pointer to something that the caller will want to get to later
  168. * on. The inode.i_private pointer will point to this value on
  169. * the open() call.
  170. * @fops: a pointer to a struct file_operations that should be used for
  171. * this file.
  172. *
  173. * This is the basic "create a file" function for debugfs. It allows for a
  174. * wide range of flexibility in createing a file, or a directory (if you
  175. * want to create a directory, the debugfs_create_dir() function is
  176. * recommended to be used instead.)
  177. *
  178. * This function will return a pointer to a dentry if it succeeds. This
  179. * pointer must be passed to the debugfs_remove() function when the file is
  180. * to be removed (no automatic cleanup happens if your module is unloaded,
  181. * you are responsible here.) If an error occurs, %NULL will be returned.
  182. *
  183. * If debugfs is not enabled in the kernel, the value -%ENODEV will be
  184. * returned.
  185. */
  186. struct dentry *debugfs_create_file(const char *name, mode_t mode,
  187. struct dentry *parent, void *data,
  188. const struct file_operations *fops)
  189. {
  190. struct dentry *dentry = NULL;
  191. int error;
  192. pr_debug("debugfs: creating file '%s'\n",name);
  193. error = simple_pin_fs(&debug_fs_type, &debugfs_mount,
  194. &debugfs_mount_count);
  195. if (error)
  196. goto exit;
  197. error = debugfs_create_by_name(name, mode, parent, &dentry);
  198. if (error) {
  199. dentry = NULL;
  200. simple_release_fs(&debugfs_mount, &debugfs_mount_count);
  201. goto exit;
  202. }
  203. if (dentry->d_inode) {
  204. if (data)
  205. dentry->d_inode->i_private = data;
  206. if (fops)
  207. dentry->d_inode->i_fop = fops;
  208. }
  209. exit:
  210. return dentry;
  211. }
  212. EXPORT_SYMBOL_GPL(debugfs_create_file);
  213. /**
  214. * debugfs_create_dir - create a directory in the debugfs filesystem
  215. * @name: a pointer to a string containing the name of the directory to
  216. * create.
  217. * @parent: a pointer to the parent dentry for this file. This should be a
  218. * directory dentry if set. If this paramater is NULL, then the
  219. * directory will be created in the root of the debugfs filesystem.
  220. *
  221. * This function creates a directory in debugfs with the given name.
  222. *
  223. * This function will return a pointer to a dentry if it succeeds. This
  224. * pointer must be passed to the debugfs_remove() function when the file is
  225. * to be removed (no automatic cleanup happens if your module is unloaded,
  226. * you are responsible here.) If an error occurs, %NULL will be returned.
  227. *
  228. * If debugfs is not enabled in the kernel, the value -%ENODEV will be
  229. * returned.
  230. */
  231. struct dentry *debugfs_create_dir(const char *name, struct dentry *parent)
  232. {
  233. return debugfs_create_file(name,
  234. S_IFDIR | S_IRWXU | S_IRUGO | S_IXUGO,
  235. parent, NULL, NULL);
  236. }
  237. EXPORT_SYMBOL_GPL(debugfs_create_dir);
  238. /**
  239. * debugfs_create_symlink- create a symbolic link in the debugfs filesystem
  240. * @name: a pointer to a string containing the name of the symbolic link to
  241. * create.
  242. * @parent: a pointer to the parent dentry for this symbolic link. This
  243. * should be a directory dentry if set. If this paramater is NULL,
  244. * then the symbolic link will be created in the root of the debugfs
  245. * filesystem.
  246. * @target: a pointer to a string containing the path to the target of the
  247. * symbolic link.
  248. *
  249. * This function creates a symbolic link with the given name in debugfs that
  250. * links to the given target path.
  251. *
  252. * This function will return a pointer to a dentry if it succeeds. This
  253. * pointer must be passed to the debugfs_remove() function when the symbolic
  254. * link is to be removed (no automatic cleanup happens if your module is
  255. * unloaded, you are responsible here.) If an error occurs, %NULL will be
  256. * returned.
  257. *
  258. * If debugfs is not enabled in the kernel, the value -%ENODEV will be
  259. * returned.
  260. */
  261. struct dentry *debugfs_create_symlink(const char *name, struct dentry *parent,
  262. const char *target)
  263. {
  264. struct dentry *result;
  265. char *link;
  266. link = kstrdup(target, GFP_KERNEL);
  267. if (!link)
  268. return NULL;
  269. result = debugfs_create_file(name, S_IFLNK | S_IRWXUGO, parent, link,
  270. NULL);
  271. if (!result)
  272. kfree(link);
  273. return result;
  274. }
  275. EXPORT_SYMBOL_GPL(debugfs_create_symlink);
  276. static void __debugfs_remove(struct dentry *dentry, struct dentry *parent)
  277. {
  278. int ret = 0;
  279. if (debugfs_positive(dentry)) {
  280. if (dentry->d_inode) {
  281. dget(dentry);
  282. switch (dentry->d_inode->i_mode & S_IFMT) {
  283. case S_IFDIR:
  284. ret = simple_rmdir(parent->d_inode, dentry);
  285. break;
  286. case S_IFLNK:
  287. kfree(dentry->d_inode->i_private);
  288. /* fall through */
  289. default:
  290. simple_unlink(parent->d_inode, dentry);
  291. break;
  292. }
  293. if (!ret)
  294. d_delete(dentry);
  295. dput(dentry);
  296. }
  297. }
  298. }
  299. /**
  300. * debugfs_remove - removes a file or directory from the debugfs filesystem
  301. * @dentry: a pointer to a the dentry of the file or directory to be
  302. * removed.
  303. *
  304. * This function removes a file or directory in debugfs that was previously
  305. * created with a call to another debugfs function (like
  306. * debugfs_create_file() or variants thereof.)
  307. *
  308. * This function is required to be called in order for the file to be
  309. * removed, no automatic cleanup of files will happen when a module is
  310. * removed, you are responsible here.
  311. */
  312. void debugfs_remove(struct dentry *dentry)
  313. {
  314. struct dentry *parent;
  315. if (!dentry)
  316. return;
  317. parent = dentry->d_parent;
  318. if (!parent || !parent->d_inode)
  319. return;
  320. mutex_lock(&parent->d_inode->i_mutex);
  321. __debugfs_remove(dentry, parent);
  322. mutex_unlock(&parent->d_inode->i_mutex);
  323. simple_release_fs(&debugfs_mount, &debugfs_mount_count);
  324. }
  325. EXPORT_SYMBOL_GPL(debugfs_remove);
  326. /**
  327. * debugfs_remove_recursive - recursively removes a directory
  328. * @dentry: a pointer to a the dentry of the directory to be removed.
  329. *
  330. * This function recursively removes a directory tree in debugfs that
  331. * was previously created with a call to another debugfs function
  332. * (like debugfs_create_file() or variants thereof.)
  333. *
  334. * This function is required to be called in order for the file to be
  335. * removed, no automatic cleanup of files will happen when a module is
  336. * removed, you are responsible here.
  337. */
  338. void debugfs_remove_recursive(struct dentry *dentry)
  339. {
  340. struct dentry *child;
  341. struct dentry *parent;
  342. if (!dentry)
  343. return;
  344. parent = dentry->d_parent;
  345. if (!parent || !parent->d_inode)
  346. return;
  347. parent = dentry;
  348. mutex_lock(&parent->d_inode->i_mutex);
  349. while (1) {
  350. /*
  351. * When all dentries under "parent" has been removed,
  352. * walk up the tree until we reach our starting point.
  353. */
  354. if (list_empty(&parent->d_subdirs)) {
  355. mutex_unlock(&parent->d_inode->i_mutex);
  356. if (parent == dentry)
  357. break;
  358. parent = parent->d_parent;
  359. mutex_lock(&parent->d_inode->i_mutex);
  360. }
  361. child = list_entry(parent->d_subdirs.next, struct dentry,
  362. d_u.d_child);
  363. next_sibling:
  364. /*
  365. * If "child" isn't empty, walk down the tree and
  366. * remove all its descendants first.
  367. */
  368. if (!list_empty(&child->d_subdirs)) {
  369. mutex_unlock(&parent->d_inode->i_mutex);
  370. parent = child;
  371. mutex_lock(&parent->d_inode->i_mutex);
  372. continue;
  373. }
  374. __debugfs_remove(child, parent);
  375. if (parent->d_subdirs.next == &child->d_u.d_child) {
  376. /*
  377. * Try the next sibling.
  378. */
  379. if (child->d_u.d_child.next != &parent->d_subdirs) {
  380. child = list_entry(child->d_u.d_child.next,
  381. struct dentry,
  382. d_u.d_child);
  383. goto next_sibling;
  384. }
  385. /*
  386. * Avoid infinite loop if we fail to remove
  387. * one dentry.
  388. */
  389. mutex_unlock(&parent->d_inode->i_mutex);
  390. break;
  391. }
  392. simple_release_fs(&debugfs_mount, &debugfs_mount_count);
  393. }
  394. parent = dentry->d_parent;
  395. mutex_lock(&parent->d_inode->i_mutex);
  396. __debugfs_remove(dentry, parent);
  397. mutex_unlock(&parent->d_inode->i_mutex);
  398. simple_release_fs(&debugfs_mount, &debugfs_mount_count);
  399. }
  400. EXPORT_SYMBOL_GPL(debugfs_remove_recursive);
  401. /**
  402. * debugfs_rename - rename a file/directory in the debugfs filesystem
  403. * @old_dir: a pointer to the parent dentry for the renamed object. This
  404. * should be a directory dentry.
  405. * @old_dentry: dentry of an object to be renamed.
  406. * @new_dir: a pointer to the parent dentry where the object should be
  407. * moved. This should be a directory dentry.
  408. * @new_name: a pointer to a string containing the target name.
  409. *
  410. * This function renames a file/directory in debugfs. The target must not
  411. * exist for rename to succeed.
  412. *
  413. * This function will return a pointer to old_dentry (which is updated to
  414. * reflect renaming) if it succeeds. If an error occurs, %NULL will be
  415. * returned.
  416. *
  417. * If debugfs is not enabled in the kernel, the value -%ENODEV will be
  418. * returned.
  419. */
  420. struct dentry *debugfs_rename(struct dentry *old_dir, struct dentry *old_dentry,
  421. struct dentry *new_dir, const char *new_name)
  422. {
  423. int error;
  424. struct dentry *dentry = NULL, *trap;
  425. const char *old_name;
  426. trap = lock_rename(new_dir, old_dir);
  427. /* Source or destination directories don't exist? */
  428. if (!old_dir->d_inode || !new_dir->d_inode)
  429. goto exit;
  430. /* Source does not exist, cyclic rename, or mountpoint? */
  431. if (!old_dentry->d_inode || old_dentry == trap ||
  432. d_mountpoint(old_dentry))
  433. goto exit;
  434. dentry = lookup_one_len(new_name, new_dir, strlen(new_name));
  435. /* Lookup failed, cyclic rename or target exists? */
  436. if (IS_ERR(dentry) || dentry == trap || dentry->d_inode)
  437. goto exit;
  438. old_name = fsnotify_oldname_init(old_dentry->d_name.name);
  439. error = simple_rename(old_dir->d_inode, old_dentry, new_dir->d_inode,
  440. dentry);
  441. if (error) {
  442. fsnotify_oldname_free(old_name);
  443. goto exit;
  444. }
  445. d_move(old_dentry, dentry);
  446. fsnotify_move(old_dir->d_inode, new_dir->d_inode, old_name,
  447. old_dentry->d_name.name, S_ISDIR(old_dentry->d_inode->i_mode),
  448. NULL, old_dentry);
  449. fsnotify_oldname_free(old_name);
  450. unlock_rename(new_dir, old_dir);
  451. dput(dentry);
  452. return old_dentry;
  453. exit:
  454. if (dentry && !IS_ERR(dentry))
  455. dput(dentry);
  456. unlock_rename(new_dir, old_dir);
  457. return NULL;
  458. }
  459. EXPORT_SYMBOL_GPL(debugfs_rename);
  460. /**
  461. * debugfs_initialized - Tells whether debugfs has been registered
  462. */
  463. bool debugfs_initialized(void)
  464. {
  465. return debugfs_registered;
  466. }
  467. EXPORT_SYMBOL_GPL(debugfs_initialized);
  468. static struct kobject *debug_kobj;
  469. static int __init debugfs_init(void)
  470. {
  471. int retval;
  472. debug_kobj = kobject_create_and_add("debug", kernel_kobj);
  473. if (!debug_kobj)
  474. return -EINVAL;
  475. retval = register_filesystem(&debug_fs_type);
  476. if (retval)
  477. kobject_put(debug_kobj);
  478. else
  479. debugfs_registered = true;
  480. return retval;
  481. }
  482. static void __exit debugfs_exit(void)
  483. {
  484. debugfs_registered = false;
  485. simple_release_fs(&debugfs_mount, &debugfs_mount_count);
  486. unregister_filesystem(&debug_fs_type);
  487. kobject_put(debug_kobj);
  488. }
  489. core_initcall(debugfs_init);
  490. module_exit(debugfs_exit);
  491. MODULE_LICENSE("GPL");