inode.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  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. #include <linux/magic.h>
  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_blocks = 0;
  38. inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  39. switch (mode & S_IFMT) {
  40. default:
  41. init_special_inode(inode, mode, dev);
  42. break;
  43. case S_IFREG:
  44. inode->i_fop = &debugfs_file_operations;
  45. break;
  46. case S_IFLNK:
  47. inode->i_op = &debugfs_link_operations;
  48. break;
  49. case S_IFDIR:
  50. inode->i_op = &simple_dir_inode_operations;
  51. inode->i_fop = &simple_dir_operations;
  52. /* directory inodes start off with i_nlink == 2
  53. * (for "." entry) */
  54. inc_nlink(inode);
  55. break;
  56. }
  57. }
  58. return inode;
  59. }
  60. /* SMP-safe */
  61. static int debugfs_mknod(struct inode *dir, struct dentry *dentry,
  62. int mode, dev_t dev)
  63. {
  64. struct inode *inode;
  65. int error = -EPERM;
  66. if (dentry->d_inode)
  67. return -EEXIST;
  68. inode = debugfs_get_inode(dir->i_sb, mode, dev);
  69. if (inode) {
  70. d_instantiate(dentry, inode);
  71. dget(dentry);
  72. error = 0;
  73. }
  74. return error;
  75. }
  76. static int debugfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  77. {
  78. int res;
  79. mode = (mode & (S_IRWXUGO | S_ISVTX)) | S_IFDIR;
  80. res = debugfs_mknod(dir, dentry, mode, 0);
  81. if (!res) {
  82. inc_nlink(dir);
  83. fsnotify_mkdir(dir, dentry);
  84. }
  85. return res;
  86. }
  87. static int debugfs_link(struct inode *dir, struct dentry *dentry, int mode)
  88. {
  89. mode = (mode & S_IALLUGO) | S_IFLNK;
  90. return debugfs_mknod(dir, dentry, mode, 0);
  91. }
  92. static int debugfs_create(struct inode *dir, struct dentry *dentry, int mode)
  93. {
  94. int res;
  95. mode = (mode & S_IALLUGO) | S_IFREG;
  96. res = debugfs_mknod(dir, dentry, mode, 0);
  97. if (!res)
  98. fsnotify_create(dir, dentry);
  99. return res;
  100. }
  101. static inline int debugfs_positive(struct dentry *dentry)
  102. {
  103. return dentry->d_inode && !d_unhashed(dentry);
  104. }
  105. static int debug_fill_super(struct super_block *sb, void *data, int silent)
  106. {
  107. static struct tree_descr debug_files[] = {{""}};
  108. return simple_fill_super(sb, DEBUGFS_MAGIC, debug_files);
  109. }
  110. static int debug_get_sb(struct file_system_type *fs_type,
  111. int flags, const char *dev_name,
  112. void *data, struct vfsmount *mnt)
  113. {
  114. return get_sb_single(fs_type, flags, data, debug_fill_super, mnt);
  115. }
  116. static struct file_system_type debug_fs_type = {
  117. .owner = THIS_MODULE,
  118. .name = "debugfs",
  119. .get_sb = debug_get_sb,
  120. .kill_sb = kill_litter_super,
  121. };
  122. static int debugfs_create_by_name(const char *name, mode_t mode,
  123. struct dentry *parent,
  124. struct dentry **dentry)
  125. {
  126. int error = 0;
  127. /* If the parent is not specified, we create it in the root.
  128. * We need the root dentry to do this, which is in the super
  129. * block. A pointer to that is in the struct vfsmount that we
  130. * have around.
  131. */
  132. if (!parent) {
  133. if (debugfs_mount && debugfs_mount->mnt_sb) {
  134. parent = debugfs_mount->mnt_sb->s_root;
  135. }
  136. }
  137. if (!parent) {
  138. pr_debug("debugfs: Ah! can not find a parent!\n");
  139. return -EFAULT;
  140. }
  141. *dentry = NULL;
  142. mutex_lock(&parent->d_inode->i_mutex);
  143. *dentry = lookup_one_len(name, parent, strlen(name));
  144. if (!IS_ERR(*dentry)) {
  145. switch (mode & S_IFMT) {
  146. case S_IFDIR:
  147. error = debugfs_mkdir(parent->d_inode, *dentry, mode);
  148. break;
  149. case S_IFLNK:
  150. error = debugfs_link(parent->d_inode, *dentry, mode);
  151. break;
  152. default:
  153. error = debugfs_create(parent->d_inode, *dentry, mode);
  154. break;
  155. }
  156. dput(*dentry);
  157. } else
  158. error = PTR_ERR(*dentry);
  159. mutex_unlock(&parent->d_inode->i_mutex);
  160. return error;
  161. }
  162. /**
  163. * debugfs_create_file - create a file in the debugfs filesystem
  164. * @name: a pointer to a string containing the name of the file to create.
  165. * @mode: the permission that the file should have
  166. * @parent: a pointer to the parent dentry for this file. This should be a
  167. * directory dentry if set. If this paramater is NULL, then the
  168. * file will be created in the root of the debugfs filesystem.
  169. * @data: a pointer to something that the caller will want to get to later
  170. * on. The inode.i_private pointer will point to this value on
  171. * the open() call.
  172. * @fops: a pointer to a struct file_operations that should be used for
  173. * this file.
  174. *
  175. * This is the basic "create a file" function for debugfs. It allows for a
  176. * wide range of flexibility in createing a file, or a directory (if you
  177. * want to create a directory, the debugfs_create_dir() function is
  178. * recommended to be used instead.)
  179. *
  180. * This function will return a pointer to a dentry if it succeeds. This
  181. * pointer must be passed to the debugfs_remove() function when the file is
  182. * to be removed (no automatic cleanup happens if your module is unloaded,
  183. * you are responsible here.) If an error occurs, %NULL will be returned.
  184. *
  185. * If debugfs is not enabled in the kernel, the value -%ENODEV will be
  186. * returned.
  187. */
  188. struct dentry *debugfs_create_file(const char *name, mode_t mode,
  189. struct dentry *parent, void *data,
  190. const struct file_operations *fops)
  191. {
  192. struct dentry *dentry = NULL;
  193. int error;
  194. pr_debug("debugfs: creating file '%s'\n",name);
  195. error = simple_pin_fs(&debug_fs_type, &debugfs_mount,
  196. &debugfs_mount_count);
  197. if (error)
  198. goto exit;
  199. error = debugfs_create_by_name(name, mode, parent, &dentry);
  200. if (error) {
  201. dentry = NULL;
  202. simple_release_fs(&debugfs_mount, &debugfs_mount_count);
  203. goto exit;
  204. }
  205. if (dentry->d_inode) {
  206. if (data)
  207. dentry->d_inode->i_private = data;
  208. if (fops)
  209. dentry->d_inode->i_fop = fops;
  210. }
  211. exit:
  212. return dentry;
  213. }
  214. EXPORT_SYMBOL_GPL(debugfs_create_file);
  215. /**
  216. * debugfs_create_dir - create a directory in the debugfs filesystem
  217. * @name: a pointer to a string containing the name of the directory to
  218. * create.
  219. * @parent: a pointer to the parent dentry for this file. This should be a
  220. * directory dentry if set. If this paramater is NULL, then the
  221. * directory will be created in the root of the debugfs filesystem.
  222. *
  223. * This function creates a directory in debugfs with the given name.
  224. *
  225. * This function will return a pointer to a dentry if it succeeds. This
  226. * pointer must be passed to the debugfs_remove() function when the file is
  227. * to be removed (no automatic cleanup happens if your module is unloaded,
  228. * you are responsible here.) If an error occurs, %NULL will be returned.
  229. *
  230. * If debugfs is not enabled in the kernel, the value -%ENODEV will be
  231. * returned.
  232. */
  233. struct dentry *debugfs_create_dir(const char *name, struct dentry *parent)
  234. {
  235. return debugfs_create_file(name,
  236. S_IFDIR | S_IRWXU | S_IRUGO | S_IXUGO,
  237. parent, NULL, NULL);
  238. }
  239. EXPORT_SYMBOL_GPL(debugfs_create_dir);
  240. /**
  241. * debugfs_create_symlink- create a symbolic link in the debugfs filesystem
  242. * @name: a pointer to a string containing the name of the symbolic link to
  243. * create.
  244. * @parent: a pointer to the parent dentry for this symbolic link. This
  245. * should be a directory dentry if set. If this paramater is NULL,
  246. * then the symbolic link will be created in the root of the debugfs
  247. * filesystem.
  248. * @target: a pointer to a string containing the path to the target of the
  249. * symbolic link.
  250. *
  251. * This function creates a symbolic link with the given name in debugfs that
  252. * links to the given target path.
  253. *
  254. * This function will return a pointer to a dentry if it succeeds. This
  255. * pointer must be passed to the debugfs_remove() function when the symbolic
  256. * link is to be removed (no automatic cleanup happens if your module is
  257. * unloaded, you are responsible here.) If an error occurs, %NULL will be
  258. * returned.
  259. *
  260. * If debugfs is not enabled in the kernel, the value -%ENODEV will be
  261. * returned.
  262. */
  263. struct dentry *debugfs_create_symlink(const char *name, struct dentry *parent,
  264. const char *target)
  265. {
  266. struct dentry *result;
  267. char *link;
  268. link = kstrdup(target, GFP_KERNEL);
  269. if (!link)
  270. return NULL;
  271. result = debugfs_create_file(name, S_IFLNK | S_IRWXUGO, parent, link,
  272. NULL);
  273. if (!result)
  274. kfree(link);
  275. return result;
  276. }
  277. EXPORT_SYMBOL_GPL(debugfs_create_symlink);
  278. static void __debugfs_remove(struct dentry *dentry, struct dentry *parent)
  279. {
  280. int ret = 0;
  281. if (debugfs_positive(dentry)) {
  282. if (dentry->d_inode) {
  283. dget(dentry);
  284. switch (dentry->d_inode->i_mode & S_IFMT) {
  285. case S_IFDIR:
  286. ret = simple_rmdir(parent->d_inode, dentry);
  287. break;
  288. case S_IFLNK:
  289. kfree(dentry->d_inode->i_private);
  290. /* fall through */
  291. default:
  292. simple_unlink(parent->d_inode, dentry);
  293. break;
  294. }
  295. if (!ret)
  296. d_delete(dentry);
  297. dput(dentry);
  298. }
  299. }
  300. }
  301. /**
  302. * debugfs_remove - removes a file or directory from the debugfs filesystem
  303. * @dentry: a pointer to a the dentry of the file or directory to be
  304. * removed.
  305. *
  306. * This function removes a file or directory in debugfs that was previously
  307. * created with a call to another debugfs function (like
  308. * debugfs_create_file() or variants thereof.)
  309. *
  310. * This function is required to be called in order for the file to be
  311. * removed, no automatic cleanup of files will happen when a module is
  312. * removed, you are responsible here.
  313. */
  314. void debugfs_remove(struct dentry *dentry)
  315. {
  316. struct dentry *parent;
  317. if (!dentry)
  318. return;
  319. parent = dentry->d_parent;
  320. if (!parent || !parent->d_inode)
  321. return;
  322. mutex_lock(&parent->d_inode->i_mutex);
  323. __debugfs_remove(dentry, parent);
  324. mutex_unlock(&parent->d_inode->i_mutex);
  325. simple_release_fs(&debugfs_mount, &debugfs_mount_count);
  326. }
  327. EXPORT_SYMBOL_GPL(debugfs_remove);
  328. /**
  329. * debugfs_remove_recursive - recursively removes a directory
  330. * @dentry: a pointer to a the dentry of the directory to be removed.
  331. *
  332. * This function recursively removes a directory tree in debugfs that
  333. * was previously created with a call to another debugfs function
  334. * (like debugfs_create_file() or variants thereof.)
  335. *
  336. * This function is required to be called in order for the file to be
  337. * removed, no automatic cleanup of files will happen when a module is
  338. * removed, you are responsible here.
  339. */
  340. void debugfs_remove_recursive(struct dentry *dentry)
  341. {
  342. struct dentry *child;
  343. struct dentry *parent;
  344. if (!dentry)
  345. return;
  346. parent = dentry->d_parent;
  347. if (!parent || !parent->d_inode)
  348. return;
  349. parent = dentry;
  350. mutex_lock(&parent->d_inode->i_mutex);
  351. while (1) {
  352. /*
  353. * When all dentries under "parent" has been removed,
  354. * walk up the tree until we reach our starting point.
  355. */
  356. if (list_empty(&parent->d_subdirs)) {
  357. mutex_unlock(&parent->d_inode->i_mutex);
  358. if (parent == dentry)
  359. break;
  360. parent = parent->d_parent;
  361. mutex_lock(&parent->d_inode->i_mutex);
  362. }
  363. child = list_entry(parent->d_subdirs.next, struct dentry,
  364. d_u.d_child);
  365. /*
  366. * If "child" isn't empty, walk down the tree and
  367. * remove all its descendants first.
  368. */
  369. if (!list_empty(&child->d_subdirs)) {
  370. mutex_unlock(&parent->d_inode->i_mutex);
  371. parent = child;
  372. mutex_lock(&parent->d_inode->i_mutex);
  373. continue;
  374. }
  375. __debugfs_remove(child, parent);
  376. if (parent->d_subdirs.next == &child->d_u.d_child) {
  377. /*
  378. * Avoid infinite loop if we fail to remove
  379. * one dentry.
  380. */
  381. mutex_unlock(&parent->d_inode->i_mutex);
  382. break;
  383. }
  384. simple_release_fs(&debugfs_mount, &debugfs_mount_count);
  385. }
  386. parent = dentry->d_parent;
  387. mutex_lock(&parent->d_inode->i_mutex);
  388. __debugfs_remove(dentry, parent);
  389. mutex_unlock(&parent->d_inode->i_mutex);
  390. simple_release_fs(&debugfs_mount, &debugfs_mount_count);
  391. }
  392. EXPORT_SYMBOL_GPL(debugfs_remove_recursive);
  393. /**
  394. * debugfs_rename - rename a file/directory in the debugfs filesystem
  395. * @old_dir: a pointer to the parent dentry for the renamed object. This
  396. * should be a directory dentry.
  397. * @old_dentry: dentry of an object to be renamed.
  398. * @new_dir: a pointer to the parent dentry where the object should be
  399. * moved. This should be a directory dentry.
  400. * @new_name: a pointer to a string containing the target name.
  401. *
  402. * This function renames a file/directory in debugfs. The target must not
  403. * exist for rename to succeed.
  404. *
  405. * This function will return a pointer to old_dentry (which is updated to
  406. * reflect renaming) if it succeeds. If an error occurs, %NULL will be
  407. * returned.
  408. *
  409. * If debugfs is not enabled in the kernel, the value -%ENODEV will be
  410. * returned.
  411. */
  412. struct dentry *debugfs_rename(struct dentry *old_dir, struct dentry *old_dentry,
  413. struct dentry *new_dir, const char *new_name)
  414. {
  415. int error;
  416. struct dentry *dentry = NULL, *trap;
  417. const char *old_name;
  418. trap = lock_rename(new_dir, old_dir);
  419. /* Source or destination directories don't exist? */
  420. if (!old_dir->d_inode || !new_dir->d_inode)
  421. goto exit;
  422. /* Source does not exist, cyclic rename, or mountpoint? */
  423. if (!old_dentry->d_inode || old_dentry == trap ||
  424. d_mountpoint(old_dentry))
  425. goto exit;
  426. dentry = lookup_one_len(new_name, new_dir, strlen(new_name));
  427. /* Lookup failed, cyclic rename or target exists? */
  428. if (IS_ERR(dentry) || dentry == trap || dentry->d_inode)
  429. goto exit;
  430. old_name = fsnotify_oldname_init(old_dentry->d_name.name);
  431. error = simple_rename(old_dir->d_inode, old_dentry, new_dir->d_inode,
  432. dentry);
  433. if (error) {
  434. fsnotify_oldname_free(old_name);
  435. goto exit;
  436. }
  437. d_move(old_dentry, dentry);
  438. fsnotify_move(old_dir->d_inode, new_dir->d_inode, old_name,
  439. old_dentry->d_name.name, S_ISDIR(old_dentry->d_inode->i_mode),
  440. NULL, old_dentry);
  441. fsnotify_oldname_free(old_name);
  442. unlock_rename(new_dir, old_dir);
  443. dput(dentry);
  444. return old_dentry;
  445. exit:
  446. if (dentry && !IS_ERR(dentry))
  447. dput(dentry);
  448. unlock_rename(new_dir, old_dir);
  449. return NULL;
  450. }
  451. EXPORT_SYMBOL_GPL(debugfs_rename);
  452. static struct kobject *debug_kobj;
  453. static int __init debugfs_init(void)
  454. {
  455. int retval;
  456. debug_kobj = kobject_create_and_add("debug", kernel_kobj);
  457. if (!debug_kobj)
  458. return -EINVAL;
  459. retval = register_filesystem(&debug_fs_type);
  460. if (retval)
  461. kobject_put(debug_kobj);
  462. return retval;
  463. }
  464. static void __exit debugfs_exit(void)
  465. {
  466. simple_release_fs(&debugfs_mount, &debugfs_mount_count);
  467. unregister_filesystem(&debug_fs_type);
  468. kobject_put(debug_kobj);
  469. }
  470. core_initcall(debugfs_init);
  471. module_exit(debugfs_exit);
  472. MODULE_LICENSE("GPL");