inode.c 15 KB

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