inode.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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. #define DEBUGFS_MAGIC 0x64626720
  27. /* declared over in file.c */
  28. extern struct file_operations debugfs_file_operations;
  29. static struct vfsmount *debugfs_mount;
  30. static int debugfs_mount_count;
  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_uid = 0;
  37. inode->i_gid = 0;
  38. inode->i_blocks = 0;
  39. inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  40. switch (mode & S_IFMT) {
  41. default:
  42. init_special_inode(inode, mode, dev);
  43. break;
  44. case S_IFREG:
  45. inode->i_fop = &debugfs_file_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_create(struct inode *dir, struct dentry *dentry, int mode)
  86. {
  87. int res;
  88. mode = (mode & S_IALLUGO) | S_IFREG;
  89. res = debugfs_mknod(dir, dentry, mode, 0);
  90. if (!res)
  91. fsnotify_create(dir, dentry);
  92. return res;
  93. }
  94. static inline int debugfs_positive(struct dentry *dentry)
  95. {
  96. return dentry->d_inode && !d_unhashed(dentry);
  97. }
  98. static int debug_fill_super(struct super_block *sb, void *data, int silent)
  99. {
  100. static struct tree_descr debug_files[] = {{""}};
  101. return simple_fill_super(sb, DEBUGFS_MAGIC, debug_files);
  102. }
  103. static int debug_get_sb(struct file_system_type *fs_type,
  104. int flags, const char *dev_name,
  105. void *data, struct vfsmount *mnt)
  106. {
  107. return get_sb_single(fs_type, flags, data, debug_fill_super, mnt);
  108. }
  109. static struct file_system_type debug_fs_type = {
  110. .owner = THIS_MODULE,
  111. .name = "debugfs",
  112. .get_sb = debug_get_sb,
  113. .kill_sb = kill_litter_super,
  114. };
  115. static int debugfs_create_by_name(const char *name, mode_t mode,
  116. struct dentry *parent,
  117. struct dentry **dentry)
  118. {
  119. int error = 0;
  120. /* If the parent is not specified, we create it in the root.
  121. * We need the root dentry to do this, which is in the super
  122. * block. A pointer to that is in the struct vfsmount that we
  123. * have around.
  124. */
  125. if (!parent) {
  126. if (debugfs_mount && debugfs_mount->mnt_sb) {
  127. parent = debugfs_mount->mnt_sb->s_root;
  128. }
  129. }
  130. if (!parent) {
  131. pr_debug("debugfs: Ah! can not find a parent!\n");
  132. return -EFAULT;
  133. }
  134. *dentry = NULL;
  135. mutex_lock(&parent->d_inode->i_mutex);
  136. *dentry = lookup_one_len(name, parent, strlen(name));
  137. if (!IS_ERR(*dentry)) {
  138. if ((mode & S_IFMT) == S_IFDIR)
  139. error = debugfs_mkdir(parent->d_inode, *dentry, mode);
  140. else
  141. error = debugfs_create(parent->d_inode, *dentry, mode);
  142. dput(*dentry);
  143. } else
  144. error = PTR_ERR(*dentry);
  145. mutex_unlock(&parent->d_inode->i_mutex);
  146. return error;
  147. }
  148. /**
  149. * debugfs_create_file - create a file in the debugfs filesystem
  150. * @name: a pointer to a string containing the name of the file to create.
  151. * @mode: the permission that the file should have
  152. * @parent: a pointer to the parent dentry for this file. This should be a
  153. * directory dentry if set. If this paramater is NULL, then the
  154. * file will be created in the root of the debugfs filesystem.
  155. * @data: a pointer to something that the caller will want to get to later
  156. * on. The inode.i_private pointer will point to this value on
  157. * the open() call.
  158. * @fops: a pointer to a struct file_operations that should be used for
  159. * this file.
  160. *
  161. * This is the basic "create a file" function for debugfs. It allows for a
  162. * wide range of flexibility in createing a file, or a directory (if you
  163. * want to create a directory, the debugfs_create_dir() function is
  164. * recommended to be used instead.)
  165. *
  166. * This function will return a pointer to a dentry if it succeeds. This
  167. * pointer must be passed to the debugfs_remove() function when the file is
  168. * to be removed (no automatic cleanup happens if your module is unloaded,
  169. * you are responsible here.) If an error occurs, %NULL will be returned.
  170. *
  171. * If debugfs is not enabled in the kernel, the value -%ENODEV will be
  172. * returned. It is not wise to check for this value, but rather, check for
  173. * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
  174. * code.
  175. */
  176. struct dentry *debugfs_create_file(const char *name, mode_t mode,
  177. struct dentry *parent, void *data,
  178. const struct file_operations *fops)
  179. {
  180. struct dentry *dentry = NULL;
  181. int error;
  182. pr_debug("debugfs: creating file '%s'\n",name);
  183. error = simple_pin_fs(&debug_fs_type, &debugfs_mount,
  184. &debugfs_mount_count);
  185. if (error)
  186. goto exit;
  187. error = debugfs_create_by_name(name, mode, parent, &dentry);
  188. if (error) {
  189. dentry = NULL;
  190. simple_release_fs(&debugfs_mount, &debugfs_mount_count);
  191. goto exit;
  192. }
  193. if (dentry->d_inode) {
  194. if (data)
  195. dentry->d_inode->i_private = data;
  196. if (fops)
  197. dentry->d_inode->i_fop = fops;
  198. }
  199. exit:
  200. return dentry;
  201. }
  202. EXPORT_SYMBOL_GPL(debugfs_create_file);
  203. /**
  204. * debugfs_create_dir - create a directory in the debugfs filesystem
  205. * @name: a pointer to a string containing the name of the directory to
  206. * create.
  207. * @parent: a pointer to the parent dentry for this file. This should be a
  208. * directory dentry if set. If this paramater is NULL, then the
  209. * directory will be created in the root of the debugfs filesystem.
  210. *
  211. * This function creates a directory in debugfs with the given name.
  212. *
  213. * This function will return a pointer to a dentry if it succeeds. This
  214. * pointer must be passed to the debugfs_remove() function when the file is
  215. * to be removed (no automatic cleanup happens if your module is unloaded,
  216. * you are responsible here.) If an error occurs, %NULL will be returned.
  217. *
  218. * If debugfs is not enabled in the kernel, the value -%ENODEV will be
  219. * returned. It is not wise to check for this value, but rather, check for
  220. * %NULL or !%NULL instead as to eliminate the need for #ifdef in the calling
  221. * code.
  222. */
  223. struct dentry *debugfs_create_dir(const char *name, struct dentry *parent)
  224. {
  225. return debugfs_create_file(name,
  226. S_IFDIR | S_IRWXU | S_IRUGO | S_IXUGO,
  227. parent, NULL, NULL);
  228. }
  229. EXPORT_SYMBOL_GPL(debugfs_create_dir);
  230. /**
  231. * debugfs_remove - removes a file or directory from the debugfs filesystem
  232. * @dentry: a pointer to a the dentry of the file or directory to be
  233. * removed.
  234. *
  235. * This function removes a file or directory in debugfs that was previously
  236. * created with a call to another debugfs function (like
  237. * debugfs_create_file() or variants thereof.)
  238. *
  239. * This function is required to be called in order for the file to be
  240. * removed, no automatic cleanup of files will happen when a module is
  241. * removed, you are responsible here.
  242. */
  243. void debugfs_remove(struct dentry *dentry)
  244. {
  245. struct dentry *parent;
  246. int ret = 0;
  247. if (!dentry)
  248. return;
  249. parent = dentry->d_parent;
  250. if (!parent || !parent->d_inode)
  251. return;
  252. mutex_lock(&parent->d_inode->i_mutex);
  253. if (debugfs_positive(dentry)) {
  254. if (dentry->d_inode) {
  255. dget(dentry);
  256. if (S_ISDIR(dentry->d_inode->i_mode)) {
  257. ret = simple_rmdir(parent->d_inode, dentry);
  258. if (ret)
  259. printk(KERN_ERR
  260. "DebugFS rmdir on %s failed : "
  261. "directory not empty.\n",
  262. dentry->d_name.name);
  263. } else
  264. simple_unlink(parent->d_inode, dentry);
  265. if (!ret)
  266. d_delete(dentry);
  267. dput(dentry);
  268. }
  269. }
  270. mutex_unlock(&parent->d_inode->i_mutex);
  271. simple_release_fs(&debugfs_mount, &debugfs_mount_count);
  272. }
  273. EXPORT_SYMBOL_GPL(debugfs_remove);
  274. static decl_subsys(debug, NULL, NULL);
  275. static int __init debugfs_init(void)
  276. {
  277. int retval;
  278. kset_set_kset_s(&debug_subsys, kernel_subsys);
  279. retval = subsystem_register(&debug_subsys);
  280. if (retval)
  281. return retval;
  282. retval = register_filesystem(&debug_fs_type);
  283. if (retval)
  284. subsystem_unregister(&debug_subsys);
  285. return retval;
  286. }
  287. static void __exit debugfs_exit(void)
  288. {
  289. simple_release_fs(&debugfs_mount, &debugfs_mount_count);
  290. unregister_filesystem(&debug_fs_type);
  291. subsystem_unregister(&debug_subsys);
  292. }
  293. core_initcall(debugfs_init);
  294. module_exit(debugfs_exit);
  295. MODULE_LICENSE("GPL");