inode.c 20 KB

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