inode.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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.
  190. */
  191. struct dentry *debugfs_create_file(const char *name, mode_t mode,
  192. struct dentry *parent, void *data,
  193. const struct file_operations *fops)
  194. {
  195. struct dentry *dentry = NULL;
  196. int error;
  197. pr_debug("debugfs: creating file '%s'\n",name);
  198. error = simple_pin_fs(&debug_fs_type, &debugfs_mount,
  199. &debugfs_mount_count);
  200. if (error)
  201. goto exit;
  202. error = debugfs_create_by_name(name, mode, parent, &dentry);
  203. if (error) {
  204. dentry = NULL;
  205. simple_release_fs(&debugfs_mount, &debugfs_mount_count);
  206. goto exit;
  207. }
  208. if (dentry->d_inode) {
  209. if (data)
  210. dentry->d_inode->i_private = data;
  211. if (fops)
  212. dentry->d_inode->i_fop = fops;
  213. }
  214. exit:
  215. return dentry;
  216. }
  217. EXPORT_SYMBOL_GPL(debugfs_create_file);
  218. /**
  219. * debugfs_create_dir - create a directory in the debugfs filesystem
  220. * @name: a pointer to a string containing the name of the directory to
  221. * create.
  222. * @parent: a pointer to the parent dentry for this file. This should be a
  223. * directory dentry if set. If this paramater is NULL, then the
  224. * directory will be created in the root of the debugfs filesystem.
  225. *
  226. * This function creates a directory in debugfs with the given name.
  227. *
  228. * This function will return a pointer to a dentry if it succeeds. This
  229. * pointer must be passed to the debugfs_remove() function when the file is
  230. * to be removed (no automatic cleanup happens if your module is unloaded,
  231. * you are responsible here.) If an error occurs, %NULL will be returned.
  232. *
  233. * If debugfs is not enabled in the kernel, the value -%ENODEV will be
  234. * returned.
  235. */
  236. struct dentry *debugfs_create_dir(const char *name, struct dentry *parent)
  237. {
  238. return debugfs_create_file(name,
  239. S_IFDIR | S_IRWXU | S_IRUGO | S_IXUGO,
  240. parent, NULL, NULL);
  241. }
  242. EXPORT_SYMBOL_GPL(debugfs_create_dir);
  243. /**
  244. * debugfs_create_symlink- create a symbolic link in the debugfs filesystem
  245. * @name: a pointer to a string containing the name of the symbolic link to
  246. * create.
  247. * @parent: a pointer to the parent dentry for this symbolic link. This
  248. * should be a directory dentry if set. If this paramater is NULL,
  249. * then the symbolic link will be created in the root of the debugfs
  250. * filesystem.
  251. * @target: a pointer to a string containing the path to the target of the
  252. * symbolic link.
  253. *
  254. * This function creates a symbolic link with the given name in debugfs that
  255. * links to the given target path.
  256. *
  257. * This function will return a pointer to a dentry if it succeeds. This
  258. * pointer must be passed to the debugfs_remove() function when the symbolic
  259. * link is to be removed (no automatic cleanup happens if your module is
  260. * unloaded, you are responsible here.) If an error occurs, %NULL will be
  261. * returned.
  262. *
  263. * If debugfs is not enabled in the kernel, the value -%ENODEV will be
  264. * returned.
  265. */
  266. struct dentry *debugfs_create_symlink(const char *name, struct dentry *parent,
  267. const char *target)
  268. {
  269. struct dentry *result;
  270. char *link;
  271. link = kstrdup(target, GFP_KERNEL);
  272. if (!link)
  273. return NULL;
  274. result = debugfs_create_file(name, S_IFLNK | S_IRWXUGO, parent, link,
  275. NULL);
  276. if (!result)
  277. kfree(link);
  278. return result;
  279. }
  280. EXPORT_SYMBOL_GPL(debugfs_create_symlink);
  281. /**
  282. * debugfs_remove - removes a file or directory from the debugfs filesystem
  283. * @dentry: a pointer to a the dentry of the file or directory to be
  284. * removed.
  285. *
  286. * This function removes a file or directory in debugfs that was previously
  287. * created with a call to another debugfs function (like
  288. * debugfs_create_file() or variants thereof.)
  289. *
  290. * This function is required to be called in order for the file to be
  291. * removed, no automatic cleanup of files will happen when a module is
  292. * removed, you are responsible here.
  293. */
  294. void debugfs_remove(struct dentry *dentry)
  295. {
  296. struct dentry *parent;
  297. int ret = 0;
  298. if (!dentry)
  299. return;
  300. parent = dentry->d_parent;
  301. if (!parent || !parent->d_inode)
  302. return;
  303. mutex_lock(&parent->d_inode->i_mutex);
  304. if (debugfs_positive(dentry)) {
  305. if (dentry->d_inode) {
  306. dget(dentry);
  307. switch (dentry->d_inode->i_mode & S_IFMT) {
  308. case S_IFDIR:
  309. ret = simple_rmdir(parent->d_inode, dentry);
  310. if (ret)
  311. printk(KERN_ERR
  312. "DebugFS rmdir on %s failed : "
  313. "directory not empty.\n",
  314. dentry->d_name.name);
  315. break;
  316. case S_IFLNK:
  317. kfree(dentry->d_inode->i_private);
  318. /* fall through */
  319. default:
  320. simple_unlink(parent->d_inode, dentry);
  321. break;
  322. }
  323. if (!ret)
  324. d_delete(dentry);
  325. dput(dentry);
  326. }
  327. }
  328. mutex_unlock(&parent->d_inode->i_mutex);
  329. simple_release_fs(&debugfs_mount, &debugfs_mount_count);
  330. }
  331. EXPORT_SYMBOL_GPL(debugfs_remove);
  332. static decl_subsys(debug, NULL, NULL);
  333. static int __init debugfs_init(void)
  334. {
  335. int retval;
  336. kset_set_kset_s(&debug_subsys, kernel_subsys);
  337. retval = subsystem_register(&debug_subsys);
  338. if (retval)
  339. return retval;
  340. retval = register_filesystem(&debug_fs_type);
  341. if (retval)
  342. subsystem_unregister(&debug_subsys);
  343. return retval;
  344. }
  345. static void __exit debugfs_exit(void)
  346. {
  347. simple_release_fs(&debugfs_mount, &debugfs_mount_count);
  348. unregister_filesystem(&debug_fs_type);
  349. subsystem_unregister(&debug_subsys);
  350. }
  351. core_initcall(debugfs_init);
  352. module_exit(debugfs_exit);
  353. MODULE_LICENSE("GPL");