inode.c 16 KB

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