inode.c 20 KB

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