inode.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  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. #define DEBUGFS_MAGIC 0x64626720
  28. /* declared over in file.c */
  29. extern struct file_operations debugfs_file_operations;
  30. extern struct inode_operations debugfs_link_operations;
  31. static struct vfsmount *debugfs_mount;
  32. static int debugfs_mount_count;
  33. static struct inode *debugfs_get_inode(struct super_block *sb, int mode, dev_t dev)
  34. {
  35. struct inode *inode = new_inode(sb);
  36. if (inode) {
  37. inode->i_mode = mode;
  38. inode->i_uid = 0;
  39. inode->i_gid = 0;
  40. inode->i_blocks = 0;
  41. inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  42. switch (mode & S_IFMT) {
  43. default:
  44. init_special_inode(inode, mode, dev);
  45. break;
  46. case S_IFREG:
  47. inode->i_fop = &debugfs_file_operations;
  48. break;
  49. case S_IFLNK:
  50. inode->i_op = &debugfs_link_operations;
  51. break;
  52. case S_IFDIR:
  53. inode->i_op = &simple_dir_inode_operations;
  54. inode->i_fop = &simple_dir_operations;
  55. /* directory inodes start off with i_nlink == 2
  56. * (for "." entry) */
  57. inc_nlink(inode);
  58. break;
  59. }
  60. }
  61. return inode;
  62. }
  63. /* SMP-safe */
  64. static int debugfs_mknod(struct inode *dir, struct dentry *dentry,
  65. int mode, dev_t dev)
  66. {
  67. struct inode *inode;
  68. int error = -EPERM;
  69. if (dentry->d_inode)
  70. return -EEXIST;
  71. inode = debugfs_get_inode(dir->i_sb, mode, dev);
  72. if (inode) {
  73. d_instantiate(dentry, inode);
  74. dget(dentry);
  75. error = 0;
  76. }
  77. return error;
  78. }
  79. static int debugfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  80. {
  81. int res;
  82. mode = (mode & (S_IRWXUGO | S_ISVTX)) | S_IFDIR;
  83. res = debugfs_mknod(dir, dentry, mode, 0);
  84. if (!res) {
  85. inc_nlink(dir);
  86. fsnotify_mkdir(dir, dentry);
  87. }
  88. return res;
  89. }
  90. static int debugfs_link(struct inode *dir, struct dentry *dentry, int mode)
  91. {
  92. mode = (mode & S_IALLUGO) | S_IFLNK;
  93. return debugfs_mknod(dir, dentry, mode, 0);
  94. }
  95. static int debugfs_create(struct inode *dir, struct dentry *dentry, int mode)
  96. {
  97. int res;
  98. mode = (mode & S_IALLUGO) | S_IFREG;
  99. res = debugfs_mknod(dir, dentry, mode, 0);
  100. if (!res)
  101. fsnotify_create(dir, dentry);
  102. return res;
  103. }
  104. static inline int debugfs_positive(struct dentry *dentry)
  105. {
  106. return dentry->d_inode && !d_unhashed(dentry);
  107. }
  108. static int debug_fill_super(struct super_block *sb, void *data, int silent)
  109. {
  110. static struct tree_descr debug_files[] = {{""}};
  111. return simple_fill_super(sb, DEBUGFS_MAGIC, debug_files);
  112. }
  113. static int debug_get_sb(struct file_system_type *fs_type,
  114. int flags, const char *dev_name,
  115. void *data, struct vfsmount *mnt)
  116. {
  117. return get_sb_single(fs_type, flags, data, debug_fill_super, mnt);
  118. }
  119. static struct file_system_type debug_fs_type = {
  120. .owner = THIS_MODULE,
  121. .name = "debugfs",
  122. .get_sb = debug_get_sb,
  123. .kill_sb = kill_litter_super,
  124. };
  125. static int debugfs_create_by_name(const char *name, mode_t mode,
  126. struct dentry *parent,
  127. struct dentry **dentry)
  128. {
  129. int error = 0;
  130. /* If the parent is not specified, we create it in the root.
  131. * We need the root dentry to do this, which is in the super
  132. * block. A pointer to that is in the struct vfsmount that we
  133. * have around.
  134. */
  135. if (!parent) {
  136. if (debugfs_mount && debugfs_mount->mnt_sb) {
  137. parent = debugfs_mount->mnt_sb->s_root;
  138. }
  139. }
  140. if (!parent) {
  141. pr_debug("debugfs: Ah! can not find a parent!\n");
  142. return -EFAULT;
  143. }
  144. *dentry = NULL;
  145. mutex_lock(&parent->d_inode->i_mutex);
  146. *dentry = lookup_one_len(name, parent, strlen(name));
  147. if (!IS_ERR(*dentry)) {
  148. switch (mode & S_IFMT) {
  149. case S_IFDIR:
  150. error = debugfs_mkdir(parent->d_inode, *dentry, mode);
  151. break;
  152. case S_IFLNK:
  153. error = debugfs_link(parent->d_inode, *dentry, mode);
  154. break;
  155. default:
  156. error = debugfs_create(parent->d_inode, *dentry, mode);
  157. break;
  158. }
  159. dput(*dentry);
  160. } else
  161. error = PTR_ERR(*dentry);
  162. mutex_unlock(&parent->d_inode->i_mutex);
  163. return error;
  164. }
  165. /**
  166. * debugfs_create_file - create a file in the debugfs filesystem
  167. * @name: a pointer to a string containing the name of the file to create.
  168. * @mode: the permission that the file should have
  169. * @parent: a pointer to the parent dentry for this file. This should be a
  170. * directory dentry if set. If this paramater is NULL, then the
  171. * file will be created in the root of the debugfs filesystem.
  172. * @data: a pointer to something that the caller will want to get to later
  173. * on. The inode.i_private pointer will point to this value on
  174. * the open() call.
  175. * @fops: a pointer to a struct file_operations that should be used for
  176. * this file.
  177. *
  178. * This is the basic "create a file" function for debugfs. It allows for a
  179. * wide range of flexibility in createing a file, or a directory (if you
  180. * want to create a directory, the debugfs_create_dir() function is
  181. * recommended to be used instead.)
  182. *
  183. * This function will return a pointer to a dentry if it succeeds. This
  184. * pointer must be passed to the debugfs_remove() function when the file is
  185. * to be removed (no automatic cleanup happens if your module is unloaded,
  186. * you are responsible here.) If an error occurs, %NULL will be returned.
  187. *
  188. * If debugfs is not enabled in the kernel, the value -%ENODEV will be
  189. * returned. It is not wise to check for this value, but rather, check for
  190. * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
  191. * code.
  192. */
  193. struct dentry *debugfs_create_file(const char *name, mode_t mode,
  194. struct dentry *parent, void *data,
  195. const struct file_operations *fops)
  196. {
  197. struct dentry *dentry = NULL;
  198. int error;
  199. pr_debug("debugfs: creating file '%s'\n",name);
  200. error = simple_pin_fs(&debug_fs_type, &debugfs_mount,
  201. &debugfs_mount_count);
  202. if (error)
  203. goto exit;
  204. error = debugfs_create_by_name(name, mode, parent, &dentry);
  205. if (error) {
  206. dentry = NULL;
  207. simple_release_fs(&debugfs_mount, &debugfs_mount_count);
  208. goto exit;
  209. }
  210. if (dentry->d_inode) {
  211. if (data)
  212. dentry->d_inode->i_private = data;
  213. if (fops)
  214. dentry->d_inode->i_fop = fops;
  215. }
  216. exit:
  217. return dentry;
  218. }
  219. EXPORT_SYMBOL_GPL(debugfs_create_file);
  220. /**
  221. * debugfs_create_dir - create a directory in the debugfs filesystem
  222. * @name: a pointer to a string containing the name of the directory to
  223. * create.
  224. * @parent: a pointer to the parent dentry for this file. This should be a
  225. * directory dentry if set. If this paramater is NULL, then the
  226. * directory will be created in the root of the debugfs filesystem.
  227. *
  228. * This function creates a directory in debugfs with the given name.
  229. *
  230. * This function will return a pointer to a dentry if it succeeds. This
  231. * pointer must be passed to the debugfs_remove() function when the file is
  232. * to be removed (no automatic cleanup happens if your module is unloaded,
  233. * you are responsible here.) If an error occurs, %NULL will be returned.
  234. *
  235. * If debugfs is not enabled in the kernel, the value -%ENODEV will be
  236. * returned. It is not wise to check for this value, but rather, check for
  237. * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
  238. * code.
  239. */
  240. struct dentry *debugfs_create_dir(const char *name, struct dentry *parent)
  241. {
  242. return debugfs_create_file(name,
  243. S_IFDIR | S_IRWXU | S_IRUGO | S_IXUGO,
  244. parent, NULL, NULL);
  245. }
  246. EXPORT_SYMBOL_GPL(debugfs_create_dir);
  247. /**
  248. * debugfs_create_symlink- create a symbolic link in the debugfs filesystem
  249. * @name: a pointer to a string containing the name of the symbolic link to
  250. * create.
  251. * @parent: a pointer to the parent dentry for this symbolic link. This
  252. * should be a directory dentry if set. If this paramater is NULL,
  253. * then the symbolic link will be created in the root of the debugfs
  254. * filesystem.
  255. * @target: a pointer to a string containing the path to the target of the
  256. * symbolic link.
  257. *
  258. * This function creates a symbolic link with the given name in debugfs that
  259. * links to the given target path.
  260. *
  261. * This function will return a pointer to a dentry if it succeeds. This
  262. * pointer must be passed to the debugfs_remove() function when the symbolic
  263. * link is to be removed (no automatic cleanup happens if your module is
  264. * unloaded, you are responsible here.) If an error occurs, %NULL will be
  265. * returned.
  266. *
  267. * If debugfs is not enabled in the kernel, the value -%ENODEV will be
  268. * returned. It is not wise to check for this value, but rather, check for
  269. * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
  270. * code.
  271. */
  272. struct dentry *debugfs_create_symlink(const char *name, struct dentry *parent,
  273. const char *target)
  274. {
  275. struct dentry *result;
  276. char *link;
  277. link = kstrdup(target, GFP_KERNEL);
  278. if (!link)
  279. return NULL;
  280. result = debugfs_create_file(name, S_IFLNK | S_IRWXUGO, parent, link,
  281. NULL);
  282. if (!result)
  283. kfree(link);
  284. return result;
  285. }
  286. EXPORT_SYMBOL_GPL(debugfs_create_symlink);
  287. /**
  288. * debugfs_remove - removes a file or directory from the debugfs filesystem
  289. * @dentry: a pointer to a the dentry of the file or directory to be
  290. * removed.
  291. *
  292. * This function removes a file or directory in debugfs that was previously
  293. * created with a call to another debugfs function (like
  294. * debugfs_create_file() or variants thereof.)
  295. *
  296. * This function is required to be called in order for the file to be
  297. * removed, no automatic cleanup of files will happen when a module is
  298. * removed, you are responsible here.
  299. */
  300. void debugfs_remove(struct dentry *dentry)
  301. {
  302. struct dentry *parent;
  303. int ret = 0;
  304. if (!dentry)
  305. return;
  306. parent = dentry->d_parent;
  307. if (!parent || !parent->d_inode)
  308. return;
  309. mutex_lock(&parent->d_inode->i_mutex);
  310. if (debugfs_positive(dentry)) {
  311. if (dentry->d_inode) {
  312. dget(dentry);
  313. switch (dentry->d_inode->i_mode & S_IFMT) {
  314. case S_IFDIR:
  315. ret = simple_rmdir(parent->d_inode, dentry);
  316. if (ret)
  317. printk(KERN_ERR
  318. "DebugFS rmdir on %s failed : "
  319. "directory not empty.\n",
  320. dentry->d_name.name);
  321. break;
  322. case S_IFLNK:
  323. kfree(dentry->d_inode->i_private);
  324. /* fall through */
  325. default:
  326. simple_unlink(parent->d_inode, dentry);
  327. break;
  328. }
  329. if (!ret)
  330. d_delete(dentry);
  331. dput(dentry);
  332. }
  333. }
  334. mutex_unlock(&parent->d_inode->i_mutex);
  335. simple_release_fs(&debugfs_mount, &debugfs_mount_count);
  336. }
  337. EXPORT_SYMBOL_GPL(debugfs_remove);
  338. static decl_subsys(debug, NULL, NULL);
  339. static int __init debugfs_init(void)
  340. {
  341. int retval;
  342. kset_set_kset_s(&debug_subsys, kernel_subsys);
  343. retval = subsystem_register(&debug_subsys);
  344. if (retval)
  345. return retval;
  346. retval = register_filesystem(&debug_fs_type);
  347. if (retval)
  348. subsystem_unregister(&debug_subsys);
  349. return retval;
  350. }
  351. static void __exit debugfs_exit(void)
  352. {
  353. simple_release_fs(&debugfs_mount, &debugfs_mount_count);
  354. unregister_filesystem(&debug_fs_type);
  355. subsystem_unregister(&debug_subsys);
  356. }
  357. core_initcall(debugfs_init);
  358. module_exit(debugfs_exit);
  359. MODULE_LICENSE("GPL");