inode.c 8.8 KB

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