original development tree for Linux kernel GTP module; now long in mainline.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

697 lines
18 KiB

include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit slab.h inclusion from percpu.h percpu.h is included by sched.h and module.h and thus ends up being included when building most .c files. percpu.h includes slab.h which in turn includes gfp.h making everything defined by the two files universally available and complicating inclusion dependencies. percpu.h -> slab.h dependency is about to be removed. Prepare for this change by updating users of gfp and slab facilities include those headers directly instead of assuming availability. As this conversion needs to touch large number of source files, the following script is used as the basis of conversion. http://userweb.kernel.org/~tj/misc/slabh-sweep.py The script does the followings. * Scan files for gfp and slab usages and update includes such that only the necessary includes are there. ie. if only gfp is used, gfp.h, if slab is used, slab.h. * When the script inserts a new include, it looks at the include blocks and try to put the new include such that its order conforms to its surrounding. It's put in the include block which contains core kernel includes, in the same order that the rest are ordered - alphabetical, Christmas tree, rev-Xmas-tree or at the end if there doesn't seem to be any matching order. * If the script can't find a place to put a new include (mostly because the file doesn't have fitting include block), it prints out an error message indicating which .h file needs to be added to the file. The conversion was done in the following steps. 1. The initial automatic conversion of all .c files updated slightly over 4000 files, deleting around 700 includes and adding ~480 gfp.h and ~3000 slab.h inclusions. The script emitted errors for ~400 files. 2. Each error was manually checked. Some didn't need the inclusion, some needed manual addition while adding it to implementation .h or embedding .c file was more appropriate for others. This step added inclusions to around 150 files. 3. The script was run again and the output was compared to the edits from #2 to make sure no file was left behind. 4. Several build tests were done and a couple of problems were fixed. e.g. lib/decompress_*.c used malloc/free() wrappers around slab APIs requiring slab.h to be added manually. 5. The script was run on all .h files but without automatically editing them as sprinkling gfp.h and slab.h inclusions around .h files could easily lead to inclusion dependency hell. Most gfp.h inclusion directives were ignored as stuff from gfp.h was usually wildly available and often used in preprocessor macros. Each slab.h inclusion directive was examined and added manually as necessary. 6. percpu.h was updated not to include slab.h. 7. Build test were done on the following configurations and failures were fixed. CONFIG_GCOV_KERNEL was turned off for all tests (as my distributed build env didn't work with gcov compiles) and a few more options had to be turned off depending on archs to make things build (like ipr on powerpc/64 which failed due to missing writeq). * x86 and x86_64 UP and SMP allmodconfig and a custom test config. * powerpc and powerpc64 SMP allmodconfig * sparc and sparc64 SMP allmodconfig * ia64 SMP allmodconfig * s390 SMP allmodconfig * alpha SMP allmodconfig * um on x86_64 SMP allmodconfig 8. percpu.h modifications were reverted so that it could be applied as a separate patch and serve as bisection point. Given the fact that I had only a couple of failures from tests on step 6, I'm fairly confident about the coverage of this conversion patch. If there is a breakage, it's likely to be something in one of the arch headers which should be easily discoverable easily on most builds of the specific arch. Signed-off-by: Tejun Heo <tj@kernel.org> Guess-its-ok-by: Christoph Lameter <cl@linux-foundation.org> Cc: Ingo Molnar <mingo@redhat.com> Cc: Lee Schermerhorn <Lee.Schermerhorn@hp.com>
12 years ago
[PATCH] VFS: Permit filesystem to override root dentry on mount Extend the get_sb() filesystem operation to take an extra argument that permits the VFS to pass in the target vfsmount that defines the mountpoint. The filesystem is then required to manually set the superblock and root dentry pointers. For most filesystems, this should be done with simple_set_mnt() which will set the superblock pointer and then set the root dentry to the superblock's s_root (as per the old default behaviour). The get_sb() op now returns an integer as there's now no need to return the superblock pointer. This patch permits a superblock to be implicitly shared amongst several mount points, such as can be done with NFS to avoid potential inode aliasing. In such a case, simple_set_mnt() would not be called, and instead the mnt_root and mnt_sb would be set directly. The patch also makes the following changes: (*) the get_sb_*() convenience functions in the core kernel now take a vfsmount pointer argument and return an integer, so most filesystems have to change very little. (*) If one of the convenience function is not used, then get_sb() should normally call simple_set_mnt() to instantiate the vfsmount. This will always return 0, and so can be tail-called from get_sb(). (*) generic_shutdown_super() now calls shrink_dcache_sb() to clean up the dcache upon superblock destruction rather than shrink_dcache_anon(). This is required because the superblock may now have multiple trees that aren't actually bound to s_root, but that still need to be cleaned up. The currently called functions assume that the whole tree is rooted at s_root, and that anonymous dentries are not the roots of trees which results in dentries being left unculled. However, with the way NFS superblock sharing are currently set to be implemented, these assumptions are violated: the root of the filesystem is simply a dummy dentry and inode (the real inode for '/' may well be inaccessible), and all the vfsmounts are rooted on anonymous[*] dentries with child trees. [*] Anonymous until discovered from another tree. (*) The documentation has been adjusted, including the additional bit of changing ext2_* into foo_* in the documentation. [akpm@osdl.org: convert ipath_fs, do other stuff] Signed-off-by: David Howells <dhowells@redhat.com> Acked-by: Al Viro <viro@zeniv.linux.org.uk> Cc: Nathan Scott <nathans@sgi.com> Cc: Roland Dreier <rolandd@cisco.com> Signed-off-by: Andrew Morton <akpm@osdl.org> Signed-off-by: Linus Torvalds <torvalds@osdl.org>
16 years ago
fs: Limit sys_mount to only request filesystem modules. Modify the request_module to prefix the file system type with "fs-" and add aliases to all of the filesystems that can be built as modules to match. A common practice is to build all of the kernel code and leave code that is not commonly needed as modules, with the result that many users are exposed to any bug anywhere in the kernel. Looking for filesystems with a fs- prefix limits the pool of possible modules that can be loaded by mount to just filesystems trivially making things safer with no real cost. Using aliases means user space can control the policy of which filesystem modules are auto-loaded by editing /etc/modprobe.d/*.conf with blacklist and alias directives. Allowing simple, safe, well understood work-arounds to known problematic software. This also addresses a rare but unfortunate problem where the filesystem name is not the same as it's module name and module auto-loading would not work. While writing this patch I saw a handful of such cases. The most significant being autofs that lives in the module autofs4. This is relevant to user namespaces because we can reach the request module in get_fs_type() without having any special permissions, and people get uncomfortable when a user specified string (in this case the filesystem type) goes all of the way to request_module. After having looked at this issue I don't think there is any particular reason to perform any filtering or permission checks beyond making it clear in the module request that we want a filesystem module. The common pattern in the kernel is to call request_module() without regards to the users permissions. In general all a filesystem module does once loaded is call register_filesystem() and go to sleep. Which means there is not much attack surface exposed by loading a filesytem module unless the filesystem is mounted. In a user namespace filesystems are not mounted unless .fs_flags = FS_USERNS_MOUNT, which most filesystems do not set today. Acked-by: Serge Hallyn <serge.hallyn@canonical.com> Acked-by: Kees Cook <keescook@chromium.org> Reported-by: Kees Cook <keescook@google.com> Signed-off-by: "Eric W. Biederman" <ebiederm@xmission.com>
9 years ago
debugfs: dont stop on first failed recursive delete debugfs: dont stop on first failed recursive delete While running a while loop of removing a module that removes a debugfs directory with debugfs_remove_recursive, and at the same time doing a while loop of cat of a file in that directory, I would hit a point where somehow the cat of the file caused the remove to fail. The result is that other files did not get removed when the module was removed. I simple read of one of those file can oops the kernel because the operations to the file no longer exist (removed by module). The funny thing is that the file being cat'ed was removed. It was the siblings that were not. I see in the code to debugfs_remove_recursive there's a test that checks if the child fails to bail out of the loop to prevent an infinite loop. What this patch does is to still try any siblings in that directory. If all the siblings fail, or there are no more siblings, then we exit the loop. This fixes the above symptom, but... This is no full proof. It makes the debugfs_remove_recursive a bit more robust, but it does not explain why the one file failed. There may be some kind of delay deletion that makes the debugfs think it did not succeed. So this patch is more of a fix for the symptom but not the disease. This patch still makes the debugfs_remove_recursive more robust and until I can find out why the bug exists, this patch will keep the kernel from oopsing in most cases. Even after the cause is found I think this change can stand on its own and should be kept. [ Impact: prevent kernel oops on module unload and reading debugfs files ] Signed-off-by: Steven Rostedt <rostedt@goodmis.org> Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
13 years ago
debugfs: dont stop on first failed recursive delete debugfs: dont stop on first failed recursive delete While running a while loop of removing a module that removes a debugfs directory with debugfs_remove_recursive, and at the same time doing a while loop of cat of a file in that directory, I would hit a point where somehow the cat of the file caused the remove to fail. The result is that other files did not get removed when the module was removed. I simple read of one of those file can oops the kernel because the operations to the file no longer exist (removed by module). The funny thing is that the file being cat'ed was removed. It was the siblings that were not. I see in the code to debugfs_remove_recursive there's a test that checks if the child fails to bail out of the loop to prevent an infinite loop. What this patch does is to still try any siblings in that directory. If all the siblings fail, or there are no more siblings, then we exit the loop. This fixes the above symptom, but... This is no full proof. It makes the debugfs_remove_recursive a bit more robust, but it does not explain why the one file failed. There may be some kind of delay deletion that makes the debugfs think it did not succeed. So this patch is more of a fix for the symptom but not the disease. This patch still makes the debugfs_remove_recursive more robust and until I can find out why the bug exists, this patch will keep the kernel from oopsing in most cases. Even after the cause is found I think this change can stand on its own and should be kept. [ Impact: prevent kernel oops on module unload and reading debugfs files ] Signed-off-by: Steven Rostedt <rostedt@goodmis.org> Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
13 years ago
  1. /*
  2. * inode.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. #include <linux/module.h>
  16. #include <linux/fs.h>
  17. #include <linux/mount.h>
  18. #include <linux/pagemap.h>
  19. #include <linux/init.h>
  20. #include <linux/kobject.h>
  21. #include <linux/namei.h>
  22. #include <linux/debugfs.h>
  23. #include <linux/fsnotify.h>
  24. #include <linux/string.h>
  25. #include <linux/seq_file.h>
  26. #include <linux/parser.h>
  27. #include <linux/magic.h>
  28. #include <linux/slab.h>
  29. #define DEBUGFS_DEFAULT_MODE 0700
  30. static struct vfsmount *debugfs_mount;
  31. static int debugfs_mount_count;
  32. static bool debugfs_registered;
  33. static struct inode *debugfs_get_inode(struct super_block *sb, umode_t mode, dev_t dev,
  34. void *data, const struct file_operations *fops)
  35. {
  36. struct inode *inode = new_inode(sb);
  37. if (inode) {
  38. inode->i_ino = get_next_ino();
  39. inode->i_mode = mode;
  40. inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  41. switch (mode & S_IFMT) {
  42. default:
  43. init_special_inode(inode, mode, dev);
  44. break;
  45. case S_IFREG:
  46. inode->i_fop = fops ? fops : &debugfs_file_operations;
  47. inode->i_private = data;
  48. break;
  49. case S_IFLNK:
  50. inode->i_op = &debugfs_link_operations;
  51. inode->i_private = data;
  52. break;
  53. case S_IFDIR:
  54. inode->i_op = &simple_dir_inode_operations;
  55. inode->i_fop = &simple_dir_operations;
  56. /* directory inodes start off with i_nlink == 2
  57. * (for "." entry) */
  58. inc_nlink(inode);
  59. break;
  60. }
  61. }
  62. return inode;
  63. }
  64. /* SMP-safe */
  65. static int debugfs_mknod(struct inode *dir, struct dentry *dentry,
  66. umode_t mode, dev_t dev, void *data,
  67. const struct file_operations *fops)
  68. {
  69. struct inode *inode;
  70. int error = -EPERM;
  71. if (dentry->d_inode)
  72. return -EEXIST;
  73. inode = debugfs_get_inode(dir->i_sb, mode, dev, data, fops);
  74. if (inode) {
  75. d_instantiate(dentry, inode);
  76. dget(dentry);
  77. error = 0;
  78. }
  79. return error;
  80. }
  81. static int debugfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
  82. {
  83. int res;
  84. mode = (mode & (S_IRWXUGO | S_ISVTX)) | S_IFDIR;
  85. res = debugfs_mknod(dir, dentry, mode, 0, NULL, NULL);
  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, umode_t mode,
  93. void *data)
  94. {
  95. mode = (mode & S_IALLUGO) | S_IFLNK;
  96. return debugfs_mknod(dir, dentry, mode, 0, data, NULL);
  97. }
  98. static int debugfs_create(struct inode *dir, struct dentry *dentry, umode_t 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. struct debugfs_mount_opts {
  113. kuid_t uid;
  114. kgid_t gid;
  115. umode_t mode;
  116. };
  117. enum {
  118. Opt_uid,
  119. Opt_gid,
  120. Opt_mode,
  121. Opt_err
  122. };
  123. static const match_table_t tokens = {
  124. {Opt_uid, "uid=%u"},
  125. {Opt_gid, "gid=%u"},
  126. {Opt_mode, "mode=%o"},
  127. {Opt_err, NULL}
  128. };
  129. struct debugfs_fs_info {
  130. struct debugfs_mount_opts mount_opts;
  131. };
  132. static int debugfs_parse_options(char *data, struct debugfs_mount_opts *opts)
  133. {
  134. substring_t args[MAX_OPT_ARGS];
  135. int option;
  136. int token;
  137. kuid_t uid;
  138. kgid_t gid;
  139. char *p;
  140. opts->mode = DEBUGFS_DEFAULT_MODE;
  141. while ((p = strsep(&data, ",")) != NULL) {
  142. if (!*p)
  143. continue;
  144. token = match_token(p, tokens, args);
  145. switch (token) {
  146. case Opt_uid:
  147. if (match_int(&args[0], &option))
  148. return -EINVAL;
  149. uid = make_kuid(current_user_ns(), option);
  150. if (!uid_valid(uid))
  151. return -EINVAL;
  152. opts->uid = uid;
  153. break;
  154. case Opt_gid:
  155. if (match_int(&args[0], &option))
  156. return -EINVAL;
  157. gid = make_kgid(current_user_ns(), option);
  158. if (!gid_valid(gid))
  159. return -EINVAL;
  160. opts->gid = gid;
  161. break;
  162. case Opt_mode:
  163. if (match_octal(&args[0], &option))
  164. return -EINVAL;
  165. opts->mode = option & S_IALLUGO;
  166. break;
  167. /*
  168. * We might like to report bad mount options here;
  169. * but traditionally debugfs has ignored all mount options
  170. */
  171. }
  172. }
  173. return 0;
  174. }
  175. static int debugfs_apply_options(struct super_block *sb)
  176. {
  177. struct debugfs_fs_info *fsi = sb->s_fs_info;
  178. struct inode *inode = sb->s_root->d_inode;
  179. struct debugfs_mount_opts *opts = &fsi->mount_opts;
  180. inode->i_mode &= ~S_IALLUGO;
  181. inode->i_mode |= opts->mode;
  182. inode->i_uid = opts->uid;
  183. inode->i_gid = opts->gid;
  184. return 0;
  185. }
  186. static int debugfs_remount(struct super_block *sb, int *flags, char *data)
  187. {
  188. int err;
  189. struct debugfs_fs_info *fsi = sb->s_fs_info;
  190. err = debugfs_parse_options(data, &fsi->mount_opts);
  191. if (err)
  192. goto fail;
  193. debugfs_apply_options(sb);
  194. fail:
  195. return err;
  196. }
  197. static int debugfs_show_options(struct seq_file *m, struct dentry *root)
  198. {
  199. struct debugfs_fs_info *fsi = root->d_sb->s_fs_info;
  200. struct debugfs_mount_opts *opts = &fsi->mount_opts;
  201. if (!uid_eq(opts->uid, GLOBAL_ROOT_UID))
  202. seq_printf(m, ",uid=%u",
  203. from_kuid_munged(&init_user_ns, opts->uid));
  204. if (!gid_eq(opts->gid, GLOBAL_ROOT_GID))
  205. seq_printf(m, ",gid=%u",
  206. from_kgid_munged(&init_user_ns, opts->gid));
  207. if (opts->mode != DEBUGFS_DEFAULT_MODE)
  208. seq_printf(m, ",mode=%o", opts->mode);
  209. return 0;
  210. }
  211. static const struct super_operations debugfs_super_operations = {
  212. .statfs = simple_statfs,
  213. .remount_fs = debugfs_remount,
  214. .show_options = debugfs_show_options,
  215. };
  216. static int debug_fill_super(struct super_block *sb, void *data, int silent)
  217. {
  218. static struct tree_descr debug_files[] = {{""}};
  219. struct debugfs_fs_info *fsi;
  220. int err;
  221. save_mount_options(sb, data);
  222. fsi = kzalloc(sizeof(struct debugfs_fs_info), GFP_KERNEL);
  223. sb->s_fs_info = fsi;
  224. if (!fsi) {
  225. err = -ENOMEM;
  226. goto fail;
  227. }
  228. err = debugfs_parse_options(data, &fsi->mount_opts);
  229. if (err)
  230. goto fail;
  231. err = simple_fill_super(sb, DEBUGFS_MAGIC, debug_files);
  232. if (err)
  233. goto fail;
  234. sb->s_op = &debugfs_super_operations;
  235. debugfs_apply_options(sb);
  236. return 0;
  237. fail:
  238. kfree(fsi);
  239. sb->s_fs_info = NULL;
  240. return err;
  241. }
  242. static struct dentry *debug_mount(struct file_system_type *fs_type,
  243. int flags, const char *dev_name,
  244. void *data)
  245. {
  246. return mount_single(fs_type, flags, data, debug_fill_super);
  247. }
  248. static struct file_system_type debug_fs_type = {
  249. .owner = THIS_MODULE,
  250. .name = "debugfs",
  251. .mount = debug_mount,
  252. .kill_sb = kill_litter_super,
  253. };
  254. MODULE_ALIAS_FS("debugfs");
  255. static struct dentry *__create_file(const char *name, umode_t mode,
  256. struct dentry *parent, void *data,
  257. const struct file_operations *fops)
  258. {
  259. struct dentry *dentry = NULL;
  260. int error;
  261. pr_debug("debugfs: creating file '%s'\n",name);
  262. error = simple_pin_fs(&debug_fs_type, &debugfs_mount,
  263. &debugfs_mount_count);
  264. if (error)
  265. goto exit;
  266. /* If the parent is not specified, we create it in the root.
  267. * We need the root dentry to do this, which is in the super
  268. * block. A pointer to that is in the struct vfsmount that we
  269. * have around.
  270. */
  271. if (!parent)
  272. parent = debugfs_mount->mnt_root;
  273. mutex_lock(&parent->d_inode->i_mutex);
  274. dentry = lookup_one_len(name, parent, strlen(name));
  275. if (!IS_ERR(dentry)) {
  276. switch (mode & S_IFMT) {
  277. case S_IFDIR:
  278. error = debugfs_mkdir(parent->d_inode, dentry, mode);
  279. break;
  280. case S_IFLNK:
  281. error = debugfs_link(parent->d_inode, dentry, mode,
  282. data);
  283. break;
  284. default:
  285. error = debugfs_create(parent->d_inode, dentry, mode,
  286. data, fops);
  287. break;
  288. }
  289. dput(dentry);
  290. } else
  291. error = PTR_ERR(dentry);
  292. mutex_unlock(&parent->d_inode->i_mutex);
  293. if (error) {
  294. dentry = NULL;
  295. simple_release_fs(&debugfs_mount, &debugfs_mount_count);
  296. }
  297. exit:
  298. return dentry;
  299. }
  300. /**
  301. * debugfs_create_file - create a file in the debugfs filesystem
  302. * @name: a pointer to a string containing the name of the file to create.
  303. * @mode: the permission that the file should have.
  304. * @parent: a pointer to the parent dentry for this file. This should be a
  305. * directory dentry if set. If this paramater is NULL, then the
  306. * file will be created in the root of the debugfs filesystem.
  307. * @data: a pointer to something that the caller will want to get to later
  308. * on. The inode.i_private pointer will point to this value on
  309. * the open() call.
  310. * @fops: a pointer to a struct file_operations that should be used for
  311. * this file.
  312. *
  313. * This is the basic "create a file" function for debugfs. It allows for a
  314. * wide range of flexibility in creating a file, or a directory (if you want
  315. * to create a directory, the debugfs_create_dir() function is
  316. * recommended to be used instead.)
  317. *
  318. * This function will return a pointer to a dentry if it succeeds. This
  319. * pointer must be passed to the debugfs_remove() function when the file is
  320. * to be removed (no automatic cleanup happens if your module is unloaded,
  321. * you are responsible here.) If an error occurs, %NULL will be returned.
  322. *
  323. * If debugfs is not enabled in the kernel, the value -%ENODEV will be
  324. * returned.
  325. */
  326. struct dentry *debugfs_create_file(const char *name, umode_t mode,
  327. struct dentry *parent, void *data,
  328. const struct file_operations *fops)
  329. {
  330. switch (mode & S_IFMT) {
  331. case S_IFREG:
  332. case 0:
  333. break;
  334. default:
  335. BUG();
  336. }
  337. return __create_file(name, mode, parent, data, fops);
  338. }
  339. EXPORT_SYMBOL_GPL(debugfs_create_file);
  340. /**
  341. * debugfs_create_dir - create a directory in the debugfs filesystem
  342. * @name: a pointer to a string containing the name of the directory to
  343. * create.
  344. * @parent: a pointer to the parent dentry for this file. This should be a
  345. * directory dentry if set. If this paramater is NULL, then the
  346. * directory will be created in the root of the debugfs filesystem.
  347. *
  348. * This function creates a directory in debugfs with the given name.
  349. *
  350. * This function will return a pointer to a dentry if it succeeds. This
  351. * pointer must be passed to the debugfs_remove() function when the file is
  352. * to be removed (no automatic cleanup happens if your module is unloaded,
  353. * you are responsible here.) If an error occurs, %NULL will be returned.
  354. *
  355. * If debugfs is not enabled in the kernel, the value -%ENODEV will be
  356. * returned.
  357. */
  358. struct dentry *debugfs_create_dir(const char *name, struct dentry *parent)
  359. {
  360. return __create_file(name, S_IFDIR | S_IRWXU | S_IRUGO | S_IXUGO,
  361. parent, NULL, NULL);
  362. }
  363. EXPORT_SYMBOL_GPL(debugfs_create_dir);
  364. /**
  365. * debugfs_create_symlink- create a symbolic link in the debugfs filesystem
  366. * @name: a pointer to a string containing the name of the symbolic link to
  367. * create.
  368. * @parent: a pointer to the parent dentry for this symbolic link. This
  369. * should be a directory dentry if set. If this paramater is NULL,
  370. * then the symbolic link will be created in the root of the debugfs
  371. * filesystem.
  372. * @target: a pointer to a string containing the path to the target of the
  373. * symbolic link.
  374. *
  375. * This function creates a symbolic link with the given name in debugfs that
  376. * links to the given target path.
  377. *
  378. * This function will return a pointer to a dentry if it succeeds. This
  379. * pointer must be passed to the debugfs_remove() function when the symbolic
  380. * link is to be removed (no automatic cleanup happens if your module is
  381. * unloaded, you are responsible here.) If an error occurs, %NULL will be
  382. * returned.
  383. *
  384. * If debugfs is not enabled in the kernel, the value -%ENODEV will be
  385. * returned.
  386. */
  387. struct dentry *debugfs_create_symlink(const char *name, struct dentry *parent,
  388. const char *target)
  389. {
  390. struct dentry *result;
  391. char *link;
  392. link = kstrdup(target, GFP_KERNEL);
  393. if (!link)
  394. return NULL;
  395. result = __create_file(name, S_IFLNK | S_IRWXUGO, parent, link, NULL);
  396. if (!result)
  397. kfree(link);
  398. return result;
  399. }
  400. EXPORT_SYMBOL_GPL(debugfs_create_symlink);
  401. static int __debugfs_remove(struct dentry *dentry, struct dentry *parent)
  402. {
  403. int ret = 0;
  404. if (debugfs_positive(dentry)) {
  405. if (dentry->d_inode) {
  406. dget(dentry);
  407. switch (dentry->d_inode->i_mode & S_IFMT) {
  408. case S_IFDIR:
  409. ret = simple_rmdir(parent->d_inode, dentry);
  410. break;
  411. case S_IFLNK:
  412. kfree(dentry->d_inode->i_private);
  413. /* fall through */
  414. default:
  415. simple_unlink(parent->d_inode, dentry);
  416. break;
  417. }
  418. if (!ret)
  419. d_delete(dentry);
  420. dput(dentry);
  421. }
  422. }
  423. return ret;
  424. }
  425. /**
  426. * debugfs_remove - removes a file or directory from the debugfs filesystem
  427. * @dentry: a pointer to a the dentry of the file or directory to be
  428. * removed.
  429. *
  430. * This function removes a file or directory in debugfs that was previously
  431. * created with a call to another debugfs function (like
  432. * debugfs_create_file() or variants thereof.)
  433. *
  434. * This function is required to be called in order for the file to be
  435. * removed, no automatic cleanup of files will happen when a module is
  436. * removed, you are responsible here.
  437. */
  438. void debugfs_remove(struct dentry *dentry)
  439. {
  440. struct dentry *parent;
  441. int ret;
  442. if (IS_ERR_OR_NULL(dentry))
  443. return;
  444. parent = dentry->d_parent;
  445. if (!parent || !parent->d_inode)
  446. return;
  447. mutex_lock(&parent->d_inode->i_mutex);
  448. ret = __debugfs_remove(dentry, parent);
  449. mutex_unlock(&parent->d_inode->i_mutex);
  450. if (!ret)
  451. simple_release_fs(&debugfs_mount, &debugfs_mount_count);
  452. }
  453. EXPORT_SYMBOL_GPL(debugfs_remove);
  454. /**
  455. * debugfs_remove_recursive - recursively removes a directory
  456. * @dentry: a pointer to a the dentry of the directory to be removed.
  457. *
  458. * This function recursively removes a directory tree in debugfs that
  459. * was previously created with a call to another debugfs function
  460. * (like debugfs_create_file() or variants thereof.)
  461. *
  462. * This function is required to be called in order for the file to be
  463. * removed, no automatic cleanup of files will happen when a module is
  464. * removed, you are responsible here.
  465. */
  466. void debugfs_remove_recursive(struct dentry *dentry)
  467. {
  468. struct dentry *child;
  469. struct dentry *parent;
  470. if (IS_ERR_OR_NULL(dentry))
  471. return;
  472. parent = dentry->d_parent;
  473. if (!parent || !parent->d_inode)
  474. return;
  475. parent = dentry;
  476. mutex_lock(&parent->d_inode->i_mutex);
  477. while (1) {
  478. /*
  479. * When all dentries under "parent" has been removed,
  480. * walk up the tree until we reach our starting point.
  481. */
  482. if (list_empty(&parent->d_subdirs)) {
  483. mutex_unlock(&parent->d_inode->i_mutex);
  484. if (parent == dentry)
  485. break;
  486. parent = parent->d_parent;
  487. mutex_lock(&parent->d_inode->i_mutex);
  488. }
  489. child = list_entry(parent->d_subdirs.next, struct dentry,
  490. d_u.d_child);
  491. next_sibling:
  492. /*
  493. * If "child" isn't empty, walk down the tree and
  494. * remove all its descendants first.
  495. */
  496. if (!list_empty(&child->d_subdirs)) {
  497. mutex_unlock(&parent->d_inode->i_mutex);
  498. parent = child;
  499. mutex_lock(&parent->d_inode->i_mutex);
  500. continue;
  501. }
  502. __debugfs_remove(child, parent);
  503. if (parent->d_subdirs.next == &child->d_u.d_child) {
  504. /*
  505. * Try the next sibling.
  506. */
  507. if (child->d_u.d_child.next != &parent->d_subdirs) {
  508. child = list_entry(child->d_u.d_child.next,
  509. struct dentry,
  510. d_u.d_child);
  511. goto next_sibling;
  512. }
  513. /*
  514. * Avoid infinite loop if we fail to remove
  515. * one dentry.
  516. */
  517. mutex_unlock(&parent->d_inode->i_mutex);
  518. break;
  519. }
  520. simple_release_fs(&debugfs_mount, &debugfs_mount_count);
  521. }
  522. parent = dentry->d_parent;
  523. mutex_lock(&parent->d_inode->i_mutex);
  524. __debugfs_remove(dentry, parent);
  525. mutex_unlock(&parent->d_inode->i_mutex);
  526. simple_release_fs(&debugfs_mount, &debugfs_mount_count);
  527. }
  528. EXPORT_SYMBOL_GPL(debugfs_remove_recursive);
  529. /**
  530. * debugfs_rename - rename a file/directory in the debugfs filesystem
  531. * @old_dir: a pointer to the parent dentry for the renamed object. This
  532. * should be a directory dentry.
  533. * @old_dentry: dentry of an object to be renamed.
  534. * @new_dir: a pointer to the parent dentry where the object should be
  535. * moved. This should be a directory dentry.
  536. * @new_name: a pointer to a string containing the target name.
  537. *
  538. * This function renames a file/directory in debugfs. The target must not
  539. * exist for rename to succeed.
  540. *
  541. * This function will return a pointer to old_dentry (which is updated to
  542. * reflect renaming) if it succeeds. If an error occurs, %NULL will be
  543. * returned.
  544. *
  545. * If debugfs is not enabled in the kernel, the value -%ENODEV will be
  546. * returned.
  547. */
  548. struct dentry *debugfs_rename(struct dentry *old_dir, struct dentry *old_dentry,
  549. struct dentry *new_dir, const char *new_name)
  550. {
  551. int error;
  552. struct dentry *dentry = NULL, *trap;
  553. const char *old_name;
  554. trap = lock_rename(new_dir, old_dir);
  555. /* Source or destination directories don't exist? */
  556. if (!old_dir->d_inode || !new_dir->d_inode)
  557. goto exit;
  558. /* Source does not exist, cyclic rename, or mountpoint? */
  559. if (!old_dentry->d_inode || old_dentry == trap ||
  560. d_mountpoint(old_dentry))
  561. goto exit;
  562. dentry = lookup_one_len(new_name, new_dir, strlen(new_name));
  563. /* Lookup failed, cyclic rename or target exists? */
  564. if (IS_ERR(dentry) || dentry == trap || dentry->d_inode)
  565. goto exit;
  566. old_name = fsnotify_oldname_init(old_dentry->d_name.name);
  567. error = simple_rename(old_dir->d_inode, old_dentry, new_dir->d_inode,
  568. dentry);
  569. if (error) {
  570. fsnotify_oldname_free(old_name);
  571. goto exit;
  572. }
  573. d_move(old_dentry, dentry);
  574. fsnotify_move(old_dir->d_inode, new_dir->d_inode, old_name,
  575. S_ISDIR(old_dentry->d_inode->i_mode),
  576. NULL, old_dentry);
  577. fsnotify_oldname_free(old_name);
  578. unlock_rename(new_dir, old_dir);
  579. dput(dentry);
  580. return old_dentry;
  581. exit:
  582. if (dentry && !IS_ERR(dentry))
  583. dput(dentry);
  584. unlock_rename(new_dir, old_dir);
  585. return NULL;
  586. }
  587. EXPORT_SYMBOL_GPL(debugfs_rename);
  588. /**
  589. * debugfs_initialized - Tells whether debugfs has been registered
  590. */
  591. bool debugfs_initialized(void)
  592. {
  593. return debugfs_registered;
  594. }
  595. EXPORT_SYMBOL_GPL(debugfs_initialized);
  596. static struct kobject *debug_kobj;
  597. static int __init debugfs_init(void)
  598. {
  599. int retval;
  600. debug_kobj = kobject_create_and_add("debug", kernel_kobj);
  601. if (!debug_kobj)
  602. return -EINVAL;
  603. retval = register_filesystem(&debug_fs_type);
  604. if (retval)
  605. kobject_put(debug_kobj);
  606. else
  607. debugfs_registered = true;
  608. return retval;
  609. }
  610. core_initcall(debugfs_init);