inode.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739
  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)
  34. {
  35. struct inode *inode = new_inode(sb);
  36. if (inode) {
  37. inode->i_ino = get_next_ino();
  38. inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  39. }
  40. return inode;
  41. }
  42. static inline int debugfs_positive(struct dentry *dentry)
  43. {
  44. return d_really_is_positive(dentry) && !d_unhashed(dentry);
  45. }
  46. struct debugfs_mount_opts {
  47. kuid_t uid;
  48. kgid_t gid;
  49. umode_t mode;
  50. };
  51. enum {
  52. Opt_uid,
  53. Opt_gid,
  54. Opt_mode,
  55. Opt_err
  56. };
  57. static const match_table_t tokens = {
  58. {Opt_uid, "uid=%u"},
  59. {Opt_gid, "gid=%u"},
  60. {Opt_mode, "mode=%o"},
  61. {Opt_err, NULL}
  62. };
  63. struct debugfs_fs_info {
  64. struct debugfs_mount_opts mount_opts;
  65. };
  66. static int debugfs_parse_options(char *data, struct debugfs_mount_opts *opts)
  67. {
  68. substring_t args[MAX_OPT_ARGS];
  69. int option;
  70. int token;
  71. kuid_t uid;
  72. kgid_t gid;
  73. char *p;
  74. opts->mode = DEBUGFS_DEFAULT_MODE;
  75. while ((p = strsep(&data, ",")) != NULL) {
  76. if (!*p)
  77. continue;
  78. token = match_token(p, tokens, args);
  79. switch (token) {
  80. case Opt_uid:
  81. if (match_int(&args[0], &option))
  82. return -EINVAL;
  83. uid = make_kuid(current_user_ns(), option);
  84. if (!uid_valid(uid))
  85. return -EINVAL;
  86. opts->uid = uid;
  87. break;
  88. case Opt_gid:
  89. if (match_int(&args[0], &option))
  90. return -EINVAL;
  91. gid = make_kgid(current_user_ns(), option);
  92. if (!gid_valid(gid))
  93. return -EINVAL;
  94. opts->gid = gid;
  95. break;
  96. case Opt_mode:
  97. if (match_octal(&args[0], &option))
  98. return -EINVAL;
  99. opts->mode = option & S_IALLUGO;
  100. break;
  101. /*
  102. * We might like to report bad mount options here;
  103. * but traditionally debugfs has ignored all mount options
  104. */
  105. }
  106. }
  107. return 0;
  108. }
  109. static int debugfs_apply_options(struct super_block *sb)
  110. {
  111. struct debugfs_fs_info *fsi = sb->s_fs_info;
  112. struct inode *inode = d_inode(sb->s_root);
  113. struct debugfs_mount_opts *opts = &fsi->mount_opts;
  114. inode->i_mode &= ~S_IALLUGO;
  115. inode->i_mode |= opts->mode;
  116. inode->i_uid = opts->uid;
  117. inode->i_gid = opts->gid;
  118. return 0;
  119. }
  120. static int debugfs_remount(struct super_block *sb, int *flags, char *data)
  121. {
  122. int err;
  123. struct debugfs_fs_info *fsi = sb->s_fs_info;
  124. sync_filesystem(sb);
  125. err = debugfs_parse_options(data, &fsi->mount_opts);
  126. if (err)
  127. goto fail;
  128. debugfs_apply_options(sb);
  129. fail:
  130. return err;
  131. }
  132. static int debugfs_show_options(struct seq_file *m, struct dentry *root)
  133. {
  134. struct debugfs_fs_info *fsi = root->d_sb->s_fs_info;
  135. struct debugfs_mount_opts *opts = &fsi->mount_opts;
  136. if (!uid_eq(opts->uid, GLOBAL_ROOT_UID))
  137. seq_printf(m, ",uid=%u",
  138. from_kuid_munged(&init_user_ns, opts->uid));
  139. if (!gid_eq(opts->gid, GLOBAL_ROOT_GID))
  140. seq_printf(m, ",gid=%u",
  141. from_kgid_munged(&init_user_ns, opts->gid));
  142. if (opts->mode != DEBUGFS_DEFAULT_MODE)
  143. seq_printf(m, ",mode=%o", opts->mode);
  144. return 0;
  145. }
  146. static void debugfs_evict_inode(struct inode *inode)
  147. {
  148. truncate_inode_pages_final(&inode->i_data);
  149. clear_inode(inode);
  150. if (S_ISLNK(inode->i_mode))
  151. kfree(inode->i_private);
  152. }
  153. static const struct super_operations debugfs_super_operations = {
  154. .statfs = simple_statfs,
  155. .remount_fs = debugfs_remount,
  156. .show_options = debugfs_show_options,
  157. .evict_inode = debugfs_evict_inode,
  158. };
  159. static struct vfsmount *debugfs_automount(struct path *path)
  160. {
  161. struct vfsmount *(*f)(void *);
  162. f = (struct vfsmount *(*)(void *))path->dentry->d_fsdata;
  163. return f(d_inode(path->dentry)->i_private);
  164. }
  165. static const struct dentry_operations debugfs_dops = {
  166. .d_delete = always_delete_dentry,
  167. .d_automount = debugfs_automount,
  168. };
  169. static int debug_fill_super(struct super_block *sb, void *data, int silent)
  170. {
  171. static struct tree_descr debug_files[] = {{""}};
  172. struct debugfs_fs_info *fsi;
  173. int err;
  174. save_mount_options(sb, data);
  175. fsi = kzalloc(sizeof(struct debugfs_fs_info), GFP_KERNEL);
  176. sb->s_fs_info = fsi;
  177. if (!fsi) {
  178. err = -ENOMEM;
  179. goto fail;
  180. }
  181. err = debugfs_parse_options(data, &fsi->mount_opts);
  182. if (err)
  183. goto fail;
  184. err = simple_fill_super(sb, DEBUGFS_MAGIC, debug_files);
  185. if (err)
  186. goto fail;
  187. sb->s_op = &debugfs_super_operations;
  188. sb->s_d_op = &debugfs_dops;
  189. debugfs_apply_options(sb);
  190. return 0;
  191. fail:
  192. kfree(fsi);
  193. sb->s_fs_info = NULL;
  194. return err;
  195. }
  196. static struct dentry *debug_mount(struct file_system_type *fs_type,
  197. int flags, const char *dev_name,
  198. void *data)
  199. {
  200. return mount_single(fs_type, flags, data, debug_fill_super);
  201. }
  202. static struct file_system_type debug_fs_type = {
  203. .owner = THIS_MODULE,
  204. .name = "debugfs",
  205. .mount = debug_mount,
  206. .kill_sb = kill_litter_super,
  207. };
  208. MODULE_ALIAS_FS("debugfs");
  209. static struct dentry *start_creating(const char *name, struct dentry *parent)
  210. {
  211. struct dentry *dentry;
  212. int error;
  213. pr_debug("debugfs: creating file '%s'\n",name);
  214. if (IS_ERR(parent))
  215. return parent;
  216. error = simple_pin_fs(&debug_fs_type, &debugfs_mount,
  217. &debugfs_mount_count);
  218. if (error)
  219. return ERR_PTR(error);
  220. /* If the parent is not specified, we create it in the root.
  221. * We need the root dentry to do this, which is in the super
  222. * block. A pointer to that is in the struct vfsmount that we
  223. * have around.
  224. */
  225. if (!parent)
  226. parent = debugfs_mount->mnt_root;
  227. mutex_lock(&d_inode(parent)->i_mutex);
  228. dentry = lookup_one_len(name, parent, strlen(name));
  229. if (!IS_ERR(dentry) && d_really_is_positive(dentry)) {
  230. dput(dentry);
  231. dentry = ERR_PTR(-EEXIST);
  232. }
  233. if (IS_ERR(dentry))
  234. mutex_unlock(&d_inode(parent)->i_mutex);
  235. return dentry;
  236. }
  237. static struct dentry *failed_creating(struct dentry *dentry)
  238. {
  239. mutex_unlock(&d_inode(dentry->d_parent)->i_mutex);
  240. dput(dentry);
  241. simple_release_fs(&debugfs_mount, &debugfs_mount_count);
  242. return NULL;
  243. }
  244. static struct dentry *end_creating(struct dentry *dentry)
  245. {
  246. mutex_unlock(&d_inode(dentry->d_parent)->i_mutex);
  247. return dentry;
  248. }
  249. /**
  250. * debugfs_create_file - create a file in the debugfs filesystem
  251. * @name: a pointer to a string containing the name of the file to create.
  252. * @mode: the permission that the file should have.
  253. * @parent: a pointer to the parent dentry for this file. This should be a
  254. * directory dentry if set. If this parameter is NULL, then the
  255. * file will be created in the root of the debugfs filesystem.
  256. * @data: a pointer to something that the caller will want to get to later
  257. * on. The inode.i_private pointer will point to this value on
  258. * the open() call.
  259. * @fops: a pointer to a struct file_operations that should be used for
  260. * this file.
  261. *
  262. * This is the basic "create a file" function for debugfs. It allows for a
  263. * wide range of flexibility in creating a file, or a directory (if you want
  264. * to create a directory, the debugfs_create_dir() function is
  265. * recommended to be used instead.)
  266. *
  267. * This function will return a pointer to a dentry if it succeeds. This
  268. * pointer must be passed to the debugfs_remove() function when the file is
  269. * to be removed (no automatic cleanup happens if your module is unloaded,
  270. * you are responsible here.) If an error occurs, %NULL will be returned.
  271. *
  272. * If debugfs is not enabled in the kernel, the value -%ENODEV will be
  273. * returned.
  274. */
  275. struct dentry *debugfs_create_file(const char *name, umode_t mode,
  276. struct dentry *parent, void *data,
  277. const struct file_operations *fops)
  278. {
  279. struct dentry *dentry;
  280. struct inode *inode;
  281. if (!(mode & S_IFMT))
  282. mode |= S_IFREG;
  283. BUG_ON(!S_ISREG(mode));
  284. dentry = start_creating(name, parent);
  285. if (IS_ERR(dentry))
  286. return NULL;
  287. inode = debugfs_get_inode(dentry->d_sb);
  288. if (unlikely(!inode))
  289. return failed_creating(dentry);
  290. inode->i_mode = mode;
  291. inode->i_fop = fops ? fops : &debugfs_file_operations;
  292. inode->i_private = data;
  293. d_instantiate(dentry, inode);
  294. fsnotify_create(d_inode(dentry->d_parent), dentry);
  295. return end_creating(dentry);
  296. }
  297. EXPORT_SYMBOL_GPL(debugfs_create_file);
  298. /**
  299. * debugfs_create_file_size - create a file in the debugfs filesystem
  300. * @name: a pointer to a string containing the name of the file to create.
  301. * @mode: the permission that the file should have.
  302. * @parent: a pointer to the parent dentry for this file. This should be a
  303. * directory dentry if set. If this parameter is NULL, then the
  304. * file will be created in the root of the debugfs filesystem.
  305. * @data: a pointer to something that the caller will want to get to later
  306. * on. The inode.i_private pointer will point to this value on
  307. * the open() call.
  308. * @fops: a pointer to a struct file_operations that should be used for
  309. * this file.
  310. * @file_size: initial file size
  311. *
  312. * This is the basic "create a file" function for debugfs. It allows for a
  313. * wide range of flexibility in creating a file, or a directory (if you want
  314. * to create a directory, the debugfs_create_dir() function is
  315. * recommended to be used instead.)
  316. *
  317. * This function will return a pointer to a dentry if it succeeds. This
  318. * pointer must be passed to the debugfs_remove() function when the file is
  319. * to be removed (no automatic cleanup happens if your module is unloaded,
  320. * you are responsible here.) If an error occurs, %NULL will be returned.
  321. *
  322. * If debugfs is not enabled in the kernel, the value -%ENODEV will be
  323. * returned.
  324. */
  325. struct dentry *debugfs_create_file_size(const char *name, umode_t mode,
  326. struct dentry *parent, void *data,
  327. const struct file_operations *fops,
  328. loff_t file_size)
  329. {
  330. struct dentry *de = debugfs_create_file(name, mode, parent, data, fops);
  331. if (de)
  332. d_inode(de)->i_size = file_size;
  333. return de;
  334. }
  335. EXPORT_SYMBOL_GPL(debugfs_create_file_size);
  336. /**
  337. * debugfs_create_dir - create a directory in the debugfs filesystem
  338. * @name: a pointer to a string containing the name of the directory to
  339. * create.
  340. * @parent: a pointer to the parent dentry for this file. This should be a
  341. * directory dentry if set. If this parameter is NULL, then the
  342. * directory will be created in the root of the debugfs filesystem.
  343. *
  344. * This function creates a directory in debugfs with the given name.
  345. *
  346. * This function will return a pointer to a dentry if it succeeds. This
  347. * pointer must be passed to the debugfs_remove() function when the file is
  348. * to be removed (no automatic cleanup happens if your module is unloaded,
  349. * you are responsible here.) If an error occurs, %NULL will be returned.
  350. *
  351. * If debugfs is not enabled in the kernel, the value -%ENODEV will be
  352. * returned.
  353. */
  354. struct dentry *debugfs_create_dir(const char *name, struct dentry *parent)
  355. {
  356. struct dentry *dentry = start_creating(name, parent);
  357. struct inode *inode;
  358. if (IS_ERR(dentry))
  359. return NULL;
  360. inode = debugfs_get_inode(dentry->d_sb);
  361. if (unlikely(!inode))
  362. return failed_creating(dentry);
  363. inode->i_mode = S_IFDIR | S_IRWXU | S_IRUGO | S_IXUGO;
  364. inode->i_op = &simple_dir_inode_operations;
  365. inode->i_fop = &simple_dir_operations;
  366. /* directory inodes start off with i_nlink == 2 (for "." entry) */
  367. inc_nlink(inode);
  368. d_instantiate(dentry, inode);
  369. inc_nlink(d_inode(dentry->d_parent));
  370. fsnotify_mkdir(d_inode(dentry->d_parent), dentry);
  371. return end_creating(dentry);
  372. }
  373. EXPORT_SYMBOL_GPL(debugfs_create_dir);
  374. /**
  375. * debugfs_create_automount - create automount point in the debugfs filesystem
  376. * @name: a pointer to a string containing the name of the file to create.
  377. * @parent: a pointer to the parent dentry for this file. This should be a
  378. * directory dentry if set. If this parameter is NULL, then the
  379. * file will be created in the root of the debugfs filesystem.
  380. * @f: function to be called when pathname resolution steps on that one.
  381. * @data: opaque argument to pass to f().
  382. *
  383. * @f should return what ->d_automount() would.
  384. */
  385. struct dentry *debugfs_create_automount(const char *name,
  386. struct dentry *parent,
  387. struct vfsmount *(*f)(void *),
  388. void *data)
  389. {
  390. struct dentry *dentry = start_creating(name, parent);
  391. struct inode *inode;
  392. if (IS_ERR(dentry))
  393. return NULL;
  394. inode = debugfs_get_inode(dentry->d_sb);
  395. if (unlikely(!inode))
  396. return failed_creating(dentry);
  397. inode->i_mode = S_IFDIR | S_IRWXU | S_IRUGO | S_IXUGO;
  398. inode->i_flags |= S_AUTOMOUNT;
  399. inode->i_private = data;
  400. dentry->d_fsdata = (void *)f;
  401. d_instantiate(dentry, inode);
  402. return end_creating(dentry);
  403. }
  404. EXPORT_SYMBOL(debugfs_create_automount);
  405. /**
  406. * debugfs_create_symlink- create a symbolic link in the debugfs filesystem
  407. * @name: a pointer to a string containing the name of the symbolic link to
  408. * create.
  409. * @parent: a pointer to the parent dentry for this symbolic link. This
  410. * should be a directory dentry if set. If this parameter is NULL,
  411. * then the symbolic link will be created in the root of the debugfs
  412. * filesystem.
  413. * @target: a pointer to a string containing the path to the target of the
  414. * symbolic link.
  415. *
  416. * This function creates a symbolic link with the given name in debugfs that
  417. * links to the given target path.
  418. *
  419. * This function will return a pointer to a dentry if it succeeds. This
  420. * pointer must be passed to the debugfs_remove() function when the symbolic
  421. * link is to be removed (no automatic cleanup happens if your module is
  422. * unloaded, you are responsible here.) If an error occurs, %NULL will be
  423. * returned.
  424. *
  425. * If debugfs is not enabled in the kernel, the value -%ENODEV will be
  426. * returned.
  427. */
  428. struct dentry *debugfs_create_symlink(const char *name, struct dentry *parent,
  429. const char *target)
  430. {
  431. struct dentry *dentry;
  432. struct inode *inode;
  433. char *link = kstrdup(target, GFP_KERNEL);
  434. if (!link)
  435. return NULL;
  436. dentry = start_creating(name, parent);
  437. if (IS_ERR(dentry)) {
  438. kfree(link);
  439. return NULL;
  440. }
  441. inode = debugfs_get_inode(dentry->d_sb);
  442. if (unlikely(!inode)) {
  443. kfree(link);
  444. return failed_creating(dentry);
  445. }
  446. inode->i_mode = S_IFLNK | S_IRWXUGO;
  447. inode->i_op = &debugfs_link_operations;
  448. inode->i_private = link;
  449. d_instantiate(dentry, inode);
  450. return end_creating(dentry);
  451. }
  452. EXPORT_SYMBOL_GPL(debugfs_create_symlink);
  453. static int __debugfs_remove(struct dentry *dentry, struct dentry *parent)
  454. {
  455. int ret = 0;
  456. if (debugfs_positive(dentry)) {
  457. dget(dentry);
  458. if (d_is_dir(dentry))
  459. ret = simple_rmdir(d_inode(parent), dentry);
  460. else
  461. simple_unlink(d_inode(parent), dentry);
  462. if (!ret)
  463. d_delete(dentry);
  464. dput(dentry);
  465. }
  466. return ret;
  467. }
  468. /**
  469. * debugfs_remove - removes a file or directory from the debugfs filesystem
  470. * @dentry: a pointer to a the dentry of the file or directory to be
  471. * removed.
  472. *
  473. * This function removes a file or directory in debugfs that was previously
  474. * created with a call to another debugfs function (like
  475. * debugfs_create_file() or variants thereof.)
  476. *
  477. * This function is required to be called in order for the file to be
  478. * removed, no automatic cleanup of files will happen when a module is
  479. * removed, you are responsible here.
  480. */
  481. void debugfs_remove(struct dentry *dentry)
  482. {
  483. struct dentry *parent;
  484. int ret;
  485. if (IS_ERR_OR_NULL(dentry))
  486. return;
  487. parent = dentry->d_parent;
  488. if (!parent || d_really_is_negative(parent))
  489. return;
  490. mutex_lock(&d_inode(parent)->i_mutex);
  491. ret = __debugfs_remove(dentry, parent);
  492. mutex_unlock(&d_inode(parent)->i_mutex);
  493. if (!ret)
  494. simple_release_fs(&debugfs_mount, &debugfs_mount_count);
  495. }
  496. EXPORT_SYMBOL_GPL(debugfs_remove);
  497. /**
  498. * debugfs_remove_recursive - recursively removes a directory
  499. * @dentry: a pointer to a the dentry of the directory to be removed.
  500. *
  501. * This function recursively removes a directory tree in debugfs that
  502. * was previously created with a call to another debugfs function
  503. * (like debugfs_create_file() or variants thereof.)
  504. *
  505. * This function is required to be called in order for the file to be
  506. * removed, no automatic cleanup of files will happen when a module is
  507. * removed, you are responsible here.
  508. */
  509. void debugfs_remove_recursive(struct dentry *dentry)
  510. {
  511. struct dentry *child, *parent;
  512. if (IS_ERR_OR_NULL(dentry))
  513. return;
  514. parent = dentry->d_parent;
  515. if (!parent || d_really_is_negative(parent))
  516. return;
  517. parent = dentry;
  518. down:
  519. mutex_lock(&d_inode(parent)->i_mutex);
  520. loop:
  521. /*
  522. * The parent->d_subdirs is protected by the d_lock. Outside that
  523. * lock, the child can be unlinked and set to be freed which can
  524. * use the d_u.d_child as the rcu head and corrupt this list.
  525. */
  526. spin_lock(&parent->d_lock);
  527. list_for_each_entry(child, &parent->d_subdirs, d_child) {
  528. if (!debugfs_positive(child))
  529. continue;
  530. /* perhaps simple_empty(child) makes more sense */
  531. if (!list_empty(&child->d_subdirs)) {
  532. spin_unlock(&parent->d_lock);
  533. mutex_unlock(&d_inode(parent)->i_mutex);
  534. parent = child;
  535. goto down;
  536. }
  537. spin_unlock(&parent->d_lock);
  538. if (!__debugfs_remove(child, parent))
  539. simple_release_fs(&debugfs_mount, &debugfs_mount_count);
  540. /*
  541. * The parent->d_lock protects agaist child from unlinking
  542. * from d_subdirs. When releasing the parent->d_lock we can
  543. * no longer trust that the next pointer is valid.
  544. * Restart the loop. We'll skip this one with the
  545. * debugfs_positive() check.
  546. */
  547. goto loop;
  548. }
  549. spin_unlock(&parent->d_lock);
  550. mutex_unlock(&d_inode(parent)->i_mutex);
  551. child = parent;
  552. parent = parent->d_parent;
  553. mutex_lock(&d_inode(parent)->i_mutex);
  554. if (child != dentry)
  555. /* go up */
  556. goto loop;
  557. if (!__debugfs_remove(child, parent))
  558. simple_release_fs(&debugfs_mount, &debugfs_mount_count);
  559. mutex_unlock(&d_inode(parent)->i_mutex);
  560. }
  561. EXPORT_SYMBOL_GPL(debugfs_remove_recursive);
  562. /**
  563. * debugfs_rename - rename a file/directory in the debugfs filesystem
  564. * @old_dir: a pointer to the parent dentry for the renamed object. This
  565. * should be a directory dentry.
  566. * @old_dentry: dentry of an object to be renamed.
  567. * @new_dir: a pointer to the parent dentry where the object should be
  568. * moved. This should be a directory dentry.
  569. * @new_name: a pointer to a string containing the target name.
  570. *
  571. * This function renames a file/directory in debugfs. The target must not
  572. * exist for rename to succeed.
  573. *
  574. * This function will return a pointer to old_dentry (which is updated to
  575. * reflect renaming) if it succeeds. If an error occurs, %NULL will be
  576. * returned.
  577. *
  578. * If debugfs is not enabled in the kernel, the value -%ENODEV will be
  579. * returned.
  580. */
  581. struct dentry *debugfs_rename(struct dentry *old_dir, struct dentry *old_dentry,
  582. struct dentry *new_dir, const char *new_name)
  583. {
  584. int error;
  585. struct dentry *dentry = NULL, *trap;
  586. const char *old_name;
  587. trap = lock_rename(new_dir, old_dir);
  588. /* Source or destination directories don't exist? */
  589. if (d_really_is_negative(old_dir) || d_really_is_negative(new_dir))
  590. goto exit;
  591. /* Source does not exist, cyclic rename, or mountpoint? */
  592. if (d_really_is_negative(old_dentry) || old_dentry == trap ||
  593. d_mountpoint(old_dentry))
  594. goto exit;
  595. dentry = lookup_one_len(new_name, new_dir, strlen(new_name));
  596. /* Lookup failed, cyclic rename or target exists? */
  597. if (IS_ERR(dentry) || dentry == trap || d_really_is_positive(dentry))
  598. goto exit;
  599. old_name = fsnotify_oldname_init(old_dentry->d_name.name);
  600. error = simple_rename(d_inode(old_dir), old_dentry, d_inode(new_dir),
  601. dentry);
  602. if (error) {
  603. fsnotify_oldname_free(old_name);
  604. goto exit;
  605. }
  606. d_move(old_dentry, dentry);
  607. fsnotify_move(d_inode(old_dir), d_inode(new_dir), old_name,
  608. d_is_dir(old_dentry),
  609. NULL, old_dentry);
  610. fsnotify_oldname_free(old_name);
  611. unlock_rename(new_dir, old_dir);
  612. dput(dentry);
  613. return old_dentry;
  614. exit:
  615. if (dentry && !IS_ERR(dentry))
  616. dput(dentry);
  617. unlock_rename(new_dir, old_dir);
  618. return NULL;
  619. }
  620. EXPORT_SYMBOL_GPL(debugfs_rename);
  621. /**
  622. * debugfs_initialized - Tells whether debugfs has been registered
  623. */
  624. bool debugfs_initialized(void)
  625. {
  626. return debugfs_registered;
  627. }
  628. EXPORT_SYMBOL_GPL(debugfs_initialized);
  629. static struct kobject *debug_kobj;
  630. static int __init debugfs_init(void)
  631. {
  632. int retval;
  633. debug_kobj = kobject_create_and_add("debug", kernel_kobj);
  634. if (!debug_kobj)
  635. return -EINVAL;
  636. retval = register_filesystem(&debug_fs_type);
  637. if (retval)
  638. kobject_put(debug_kobj);
  639. else
  640. debugfs_registered = true;
  641. return retval;
  642. }
  643. core_initcall(debugfs_init);