inode.c 8.9 KB

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