inode.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  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 bool debugfs_registered;
  31. static struct inode *debugfs_get_inode(struct super_block *sb, int mode, dev_t dev,
  32. void *data, const struct file_operations *fops)
  33. {
  34. struct inode *inode = new_inode(sb);
  35. if (inode) {
  36. inode->i_mode = mode;
  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 = fops ? fops : &debugfs_file_operations;
  44. inode->i_private = data;
  45. break;
  46. case S_IFLNK:
  47. inode->i_op = &debugfs_link_operations;
  48. inode->i_fop = fops;
  49. inode->i_private = data;
  50. break;
  51. case S_IFDIR:
  52. inode->i_op = &simple_dir_inode_operations;
  53. inode->i_fop = fops ? fops : &simple_dir_operations;
  54. inode->i_private = data;
  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, void *data,
  66. const struct file_operations *fops)
  67. {
  68. struct inode *inode;
  69. int error = -EPERM;
  70. if (dentry->d_inode)
  71. return -EEXIST;
  72. inode = debugfs_get_inode(dir->i_sb, mode, dev, data, fops);
  73. if (inode) {
  74. d_instantiate(dentry, inode);
  75. dget(dentry);
  76. error = 0;
  77. }
  78. return error;
  79. }
  80. static int debugfs_mkdir(struct inode *dir, struct dentry *dentry, int mode,
  81. void *data, const struct file_operations *fops)
  82. {
  83. int res;
  84. mode = (mode & (S_IRWXUGO | S_ISVTX)) | S_IFDIR;
  85. res = debugfs_mknod(dir, dentry, mode, 0, data, fops);
  86. if (!res) {
  87. inc_nlink(dir);
  88. fsnotify_mkdir(dir, dentry);
  89. }
  90. return res;
  91. }
  92. static int debugfs_link(struct inode *dir, struct dentry *dentry, int mode,
  93. void *data, const struct file_operations *fops)
  94. {
  95. mode = (mode & S_IALLUGO) | S_IFLNK;
  96. return debugfs_mknod(dir, dentry, mode, 0, data, fops);
  97. }
  98. static int debugfs_create(struct inode *dir, struct dentry *dentry, int mode,
  99. void *data, const struct file_operations *fops)
  100. {
  101. int res;
  102. mode = (mode & S_IALLUGO) | S_IFREG;
  103. res = debugfs_mknod(dir, dentry, mode, 0, data, fops);
  104. if (!res)
  105. fsnotify_create(dir, dentry);
  106. return res;
  107. }
  108. static inline int debugfs_positive(struct dentry *dentry)
  109. {
  110. return dentry->d_inode && !d_unhashed(dentry);
  111. }
  112. static int debug_fill_super(struct super_block *sb, void *data, int silent)
  113. {
  114. static struct tree_descr debug_files[] = {{""}};
  115. return simple_fill_super(sb, DEBUGFS_MAGIC, debug_files);
  116. }
  117. static int debug_get_sb(struct file_system_type *fs_type,
  118. int flags, const char *dev_name,
  119. void *data, struct vfsmount *mnt)
  120. {
  121. return get_sb_single(fs_type, flags, data, debug_fill_super, mnt);
  122. }
  123. static struct file_system_type debug_fs_type = {
  124. .owner = THIS_MODULE,
  125. .name = "debugfs",
  126. .get_sb = debug_get_sb,
  127. .kill_sb = kill_litter_super,
  128. };
  129. static int debugfs_create_by_name(const char *name, mode_t mode,
  130. struct dentry *parent,
  131. struct dentry **dentry,
  132. void *data,
  133. const struct file_operations *fops)
  134. {
  135. int error = 0;
  136. /* If the parent is not specified, we create it in the root.
  137. * We need the root dentry to do this, which is in the super
  138. * block. A pointer to that is in the struct vfsmount that we
  139. * have around.
  140. */
  141. if (!parent)
  142. parent = debugfs_mount->mnt_sb->s_root;
  143. *dentry = NULL;
  144. mutex_lock(&parent->d_inode->i_mutex);
  145. *dentry = lookup_one_len(name, parent, strlen(name));
  146. if (!IS_ERR(*dentry)) {
  147. switch (mode & S_IFMT) {
  148. case S_IFDIR:
  149. error = debugfs_mkdir(parent->d_inode, *dentry, mode,
  150. data, fops);
  151. break;
  152. case S_IFLNK:
  153. error = debugfs_link(parent->d_inode, *dentry, mode,
  154. data, fops);
  155. break;
  156. default:
  157. error = debugfs_create(parent->d_inode, *dentry, mode,
  158. data, fops);
  159. break;
  160. }
  161. dput(*dentry);
  162. } else
  163. error = PTR_ERR(*dentry);
  164. mutex_unlock(&parent->d_inode->i_mutex);
  165. return error;
  166. }
  167. /**
  168. * debugfs_create_file - create a file in the debugfs filesystem
  169. * @name: a pointer to a string containing the name of the file to create.
  170. * @mode: the permission that the file should have.
  171. * @parent: a pointer to the parent dentry for this file. This should be a
  172. * directory dentry if set. If this paramater is NULL, then the
  173. * file will be created in the root of the debugfs filesystem.
  174. * @data: a pointer to something that the caller will want to get to later
  175. * on. The inode.i_private pointer will point to this value on
  176. * the open() call.
  177. * @fops: a pointer to a struct file_operations that should be used for
  178. * this file.
  179. *
  180. * This is the basic "create a file" function for debugfs. It allows for a
  181. * wide range of flexibility in creating a file, or a directory (if you want
  182. * to create a directory, the debugfs_create_dir() function is
  183. * recommended to be used instead.)
  184. *
  185. * This function will return a pointer to a dentry if it succeeds. This
  186. * pointer must be passed to the debugfs_remove() function when the file is
  187. * to be removed (no automatic cleanup happens if your module is unloaded,
  188. * you are responsible here.) If an error occurs, %NULL will be returned.
  189. *
  190. * If debugfs is not enabled in the kernel, the value -%ENODEV will be
  191. * returned.
  192. */
  193. struct dentry *debugfs_create_file(const char *name, mode_t mode,
  194. struct dentry *parent, void *data,
  195. const struct file_operations *fops)
  196. {
  197. struct dentry *dentry = NULL;
  198. int error;
  199. pr_debug("debugfs: creating file '%s'\n",name);
  200. error = simple_pin_fs(&debug_fs_type, &debugfs_mount,
  201. &debugfs_mount_count);
  202. if (error)
  203. goto exit;
  204. error = debugfs_create_by_name(name, mode, parent, &dentry,
  205. data, fops);
  206. if (error) {
  207. dentry = NULL;
  208. simple_release_fs(&debugfs_mount, &debugfs_mount_count);
  209. goto exit;
  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. next_sibling:
  366. /*
  367. * If "child" isn't empty, walk down the tree and
  368. * remove all its descendants first.
  369. */
  370. if (!list_empty(&child->d_subdirs)) {
  371. mutex_unlock(&parent->d_inode->i_mutex);
  372. parent = child;
  373. mutex_lock(&parent->d_inode->i_mutex);
  374. continue;
  375. }
  376. __debugfs_remove(child, parent);
  377. if (parent->d_subdirs.next == &child->d_u.d_child) {
  378. /*
  379. * Try the next sibling.
  380. */
  381. if (child->d_u.d_child.next != &parent->d_subdirs) {
  382. child = list_entry(child->d_u.d_child.next,
  383. struct dentry,
  384. d_u.d_child);
  385. goto next_sibling;
  386. }
  387. /*
  388. * Avoid infinite loop if we fail to remove
  389. * one dentry.
  390. */
  391. mutex_unlock(&parent->d_inode->i_mutex);
  392. break;
  393. }
  394. simple_release_fs(&debugfs_mount, &debugfs_mount_count);
  395. }
  396. parent = dentry->d_parent;
  397. mutex_lock(&parent->d_inode->i_mutex);
  398. __debugfs_remove(dentry, parent);
  399. mutex_unlock(&parent->d_inode->i_mutex);
  400. simple_release_fs(&debugfs_mount, &debugfs_mount_count);
  401. }
  402. EXPORT_SYMBOL_GPL(debugfs_remove_recursive);
  403. /**
  404. * debugfs_rename - rename a file/directory in the debugfs filesystem
  405. * @old_dir: a pointer to the parent dentry for the renamed object. This
  406. * should be a directory dentry.
  407. * @old_dentry: dentry of an object to be renamed.
  408. * @new_dir: a pointer to the parent dentry where the object should be
  409. * moved. This should be a directory dentry.
  410. * @new_name: a pointer to a string containing the target name.
  411. *
  412. * This function renames a file/directory in debugfs. The target must not
  413. * exist for rename to succeed.
  414. *
  415. * This function will return a pointer to old_dentry (which is updated to
  416. * reflect renaming) if it succeeds. If an error occurs, %NULL will be
  417. * returned.
  418. *
  419. * If debugfs is not enabled in the kernel, the value -%ENODEV will be
  420. * returned.
  421. */
  422. struct dentry *debugfs_rename(struct dentry *old_dir, struct dentry *old_dentry,
  423. struct dentry *new_dir, const char *new_name)
  424. {
  425. int error;
  426. struct dentry *dentry = NULL, *trap;
  427. const char *old_name;
  428. trap = lock_rename(new_dir, old_dir);
  429. /* Source or destination directories don't exist? */
  430. if (!old_dir->d_inode || !new_dir->d_inode)
  431. goto exit;
  432. /* Source does not exist, cyclic rename, or mountpoint? */
  433. if (!old_dentry->d_inode || old_dentry == trap ||
  434. d_mountpoint(old_dentry))
  435. goto exit;
  436. dentry = lookup_one_len(new_name, new_dir, strlen(new_name));
  437. /* Lookup failed, cyclic rename or target exists? */
  438. if (IS_ERR(dentry) || dentry == trap || dentry->d_inode)
  439. goto exit;
  440. old_name = fsnotify_oldname_init(old_dentry->d_name.name);
  441. error = simple_rename(old_dir->d_inode, old_dentry, new_dir->d_inode,
  442. dentry);
  443. if (error) {
  444. fsnotify_oldname_free(old_name);
  445. goto exit;
  446. }
  447. d_move(old_dentry, dentry);
  448. fsnotify_move(old_dir->d_inode, new_dir->d_inode, old_name,
  449. old_dentry->d_name.name, S_ISDIR(old_dentry->d_inode->i_mode),
  450. NULL, old_dentry);
  451. fsnotify_oldname_free(old_name);
  452. unlock_rename(new_dir, old_dir);
  453. dput(dentry);
  454. return old_dentry;
  455. exit:
  456. if (dentry && !IS_ERR(dentry))
  457. dput(dentry);
  458. unlock_rename(new_dir, old_dir);
  459. return NULL;
  460. }
  461. EXPORT_SYMBOL_GPL(debugfs_rename);
  462. /**
  463. * debugfs_initialized - Tells whether debugfs has been registered
  464. */
  465. bool debugfs_initialized(void)
  466. {
  467. return debugfs_registered;
  468. }
  469. EXPORT_SYMBOL_GPL(debugfs_initialized);
  470. static struct kobject *debug_kobj;
  471. static int __init debugfs_init(void)
  472. {
  473. int retval;
  474. debug_kobj = kobject_create_and_add("debug", kernel_kobj);
  475. if (!debug_kobj)
  476. return -EINVAL;
  477. retval = register_filesystem(&debug_fs_type);
  478. if (retval)
  479. kobject_put(debug_kobj);
  480. else
  481. debugfs_registered = true;
  482. return retval;
  483. }
  484. static void __exit debugfs_exit(void)
  485. {
  486. debugfs_registered = false;
  487. simple_release_fs(&debugfs_mount, &debugfs_mount_count);
  488. unregister_filesystem(&debug_fs_type);
  489. kobject_put(debug_kobj);
  490. }
  491. core_initcall(debugfs_init);
  492. module_exit(debugfs_exit);
  493. MODULE_LICENSE("GPL");