dir.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077
  1. /*
  2. * fs/kernfs/dir.c - kernfs directory implementation
  3. *
  4. * Copyright (c) 2001-3 Patrick Mochel
  5. * Copyright (c) 2007 SUSE Linux Products GmbH
  6. * Copyright (c) 2007, 2013 Tejun Heo <tj@kernel.org>
  7. *
  8. * This file is released under the GPLv2.
  9. */
  10. #include <linux/fs.h>
  11. #include <linux/namei.h>
  12. #include <linux/idr.h>
  13. #include <linux/slab.h>
  14. #include <linux/security.h>
  15. #include <linux/hash.h>
  16. #include "kernfs-internal.h"
  17. DEFINE_MUTEX(kernfs_mutex);
  18. #define rb_to_kn(X) rb_entry((X), struct kernfs_node, rb)
  19. /**
  20. * kernfs_name_hash
  21. * @name: Null terminated string to hash
  22. * @ns: Namespace tag to hash
  23. *
  24. * Returns 31 bit hash of ns + name (so it fits in an off_t )
  25. */
  26. static unsigned int kernfs_name_hash(const char *name, const void *ns)
  27. {
  28. unsigned long hash = init_name_hash();
  29. unsigned int len = strlen(name);
  30. while (len--)
  31. hash = partial_name_hash(*name++, hash);
  32. hash = (end_name_hash(hash) ^ hash_ptr((void *)ns, 31));
  33. hash &= 0x7fffffffU;
  34. /* Reserve hash numbers 0, 1 and INT_MAX for magic directory entries */
  35. if (hash < 1)
  36. hash += 2;
  37. if (hash >= INT_MAX)
  38. hash = INT_MAX - 1;
  39. return hash;
  40. }
  41. static int kernfs_name_compare(unsigned int hash, const char *name,
  42. const void *ns, const struct kernfs_node *kn)
  43. {
  44. if (hash != kn->hash)
  45. return hash - kn->hash;
  46. if (ns != kn->ns)
  47. return ns - kn->ns;
  48. return strcmp(name, kn->name);
  49. }
  50. static int kernfs_sd_compare(const struct kernfs_node *left,
  51. const struct kernfs_node *right)
  52. {
  53. return kernfs_name_compare(left->hash, left->name, left->ns, right);
  54. }
  55. /**
  56. * kernfs_link_sibling - link kernfs_node into sibling rbtree
  57. * @kn: kernfs_node of interest
  58. *
  59. * Link @kn into its sibling rbtree which starts from
  60. * @kn->parent->dir.children.
  61. *
  62. * Locking:
  63. * mutex_lock(kernfs_mutex)
  64. *
  65. * RETURNS:
  66. * 0 on susccess -EEXIST on failure.
  67. */
  68. static int kernfs_link_sibling(struct kernfs_node *kn)
  69. {
  70. struct rb_node **node = &kn->parent->dir.children.rb_node;
  71. struct rb_node *parent = NULL;
  72. if (kernfs_type(kn) == KERNFS_DIR)
  73. kn->parent->dir.subdirs++;
  74. while (*node) {
  75. struct kernfs_node *pos;
  76. int result;
  77. pos = rb_to_kn(*node);
  78. parent = *node;
  79. result = kernfs_sd_compare(kn, pos);
  80. if (result < 0)
  81. node = &pos->rb.rb_left;
  82. else if (result > 0)
  83. node = &pos->rb.rb_right;
  84. else
  85. return -EEXIST;
  86. }
  87. /* add new node and rebalance the tree */
  88. rb_link_node(&kn->rb, parent, node);
  89. rb_insert_color(&kn->rb, &kn->parent->dir.children);
  90. return 0;
  91. }
  92. /**
  93. * kernfs_unlink_sibling - unlink kernfs_node from sibling rbtree
  94. * @kn: kernfs_node of interest
  95. *
  96. * Unlink @kn from its sibling rbtree which starts from
  97. * kn->parent->dir.children.
  98. *
  99. * Locking:
  100. * mutex_lock(kernfs_mutex)
  101. */
  102. static void kernfs_unlink_sibling(struct kernfs_node *kn)
  103. {
  104. if (kernfs_type(kn) == KERNFS_DIR)
  105. kn->parent->dir.subdirs--;
  106. rb_erase(&kn->rb, &kn->parent->dir.children);
  107. }
  108. /**
  109. * kernfs_get_active - get an active reference to kernfs_node
  110. * @kn: kernfs_node to get an active reference to
  111. *
  112. * Get an active reference of @kn. This function is noop if @kn
  113. * is NULL.
  114. *
  115. * RETURNS:
  116. * Pointer to @kn on success, NULL on failure.
  117. */
  118. struct kernfs_node *kernfs_get_active(struct kernfs_node *kn)
  119. {
  120. if (unlikely(!kn))
  121. return NULL;
  122. if (!atomic_inc_unless_negative(&kn->active))
  123. return NULL;
  124. if (kn->flags & KERNFS_LOCKDEP)
  125. rwsem_acquire_read(&kn->dep_map, 0, 1, _RET_IP_);
  126. return kn;
  127. }
  128. /**
  129. * kernfs_put_active - put an active reference to kernfs_node
  130. * @kn: kernfs_node to put an active reference to
  131. *
  132. * Put an active reference to @kn. This function is noop if @kn
  133. * is NULL.
  134. */
  135. void kernfs_put_active(struct kernfs_node *kn)
  136. {
  137. int v;
  138. if (unlikely(!kn))
  139. return;
  140. if (kn->flags & KERNFS_LOCKDEP)
  141. rwsem_release(&kn->dep_map, 1, _RET_IP_);
  142. v = atomic_dec_return(&kn->active);
  143. if (likely(v != KN_DEACTIVATED_BIAS))
  144. return;
  145. /*
  146. * atomic_dec_return() is a mb(), we'll always see the updated
  147. * kn->u.completion.
  148. */
  149. complete(kn->u.completion);
  150. }
  151. /**
  152. * kernfs_deactivate - deactivate kernfs_node
  153. * @kn: kernfs_node to deactivate
  154. *
  155. * Deny new active references and drain existing ones.
  156. */
  157. static void kernfs_deactivate(struct kernfs_node *kn)
  158. {
  159. DECLARE_COMPLETION_ONSTACK(wait);
  160. int v;
  161. BUG_ON(!(kn->flags & KERNFS_REMOVED));
  162. if (!(kernfs_type(kn) & KERNFS_ACTIVE_REF))
  163. return;
  164. kn->u.completion = (void *)&wait;
  165. if (kn->flags & KERNFS_LOCKDEP)
  166. rwsem_acquire(&kn->dep_map, 0, 0, _RET_IP_);
  167. /* atomic_add_return() is a mb(), put_active() will always see
  168. * the updated kn->u.completion.
  169. */
  170. v = atomic_add_return(KN_DEACTIVATED_BIAS, &kn->active);
  171. if (v != KN_DEACTIVATED_BIAS) {
  172. if (kn->flags & KERNFS_LOCKDEP)
  173. lock_contended(&kn->dep_map, _RET_IP_);
  174. wait_for_completion(&wait);
  175. }
  176. if (kn->flags & KERNFS_LOCKDEP) {
  177. lock_acquired(&kn->dep_map, _RET_IP_);
  178. rwsem_release(&kn->dep_map, 1, _RET_IP_);
  179. }
  180. }
  181. /**
  182. * kernfs_get - get a reference count on a kernfs_node
  183. * @kn: the target kernfs_node
  184. */
  185. void kernfs_get(struct kernfs_node *kn)
  186. {
  187. if (kn) {
  188. WARN_ON(!atomic_read(&kn->count));
  189. atomic_inc(&kn->count);
  190. }
  191. }
  192. EXPORT_SYMBOL_GPL(kernfs_get);
  193. /**
  194. * kernfs_put - put a reference count on a kernfs_node
  195. * @kn: the target kernfs_node
  196. *
  197. * Put a reference count of @kn and destroy it if it reached zero.
  198. */
  199. void kernfs_put(struct kernfs_node *kn)
  200. {
  201. struct kernfs_node *parent;
  202. struct kernfs_root *root;
  203. if (!kn || !atomic_dec_and_test(&kn->count))
  204. return;
  205. root = kernfs_root(kn);
  206. repeat:
  207. /* Moving/renaming is always done while holding reference.
  208. * kn->parent won't change beneath us.
  209. */
  210. parent = kn->parent;
  211. WARN(!(kn->flags & KERNFS_REMOVED), "kernfs: free using entry: %s/%s\n",
  212. parent ? parent->name : "", kn->name);
  213. if (kernfs_type(kn) == KERNFS_LINK)
  214. kernfs_put(kn->symlink.target_kn);
  215. if (!(kn->flags & KERNFS_STATIC_NAME))
  216. kfree(kn->name);
  217. if (kn->iattr) {
  218. if (kn->iattr->ia_secdata)
  219. security_release_secctx(kn->iattr->ia_secdata,
  220. kn->iattr->ia_secdata_len);
  221. simple_xattrs_free(&kn->iattr->xattrs);
  222. }
  223. kfree(kn->iattr);
  224. ida_simple_remove(&root->ino_ida, kn->ino);
  225. kmem_cache_free(kernfs_node_cache, kn);
  226. kn = parent;
  227. if (kn) {
  228. if (atomic_dec_and_test(&kn->count))
  229. goto repeat;
  230. } else {
  231. /* just released the root kn, free @root too */
  232. ida_destroy(&root->ino_ida);
  233. kfree(root);
  234. }
  235. }
  236. EXPORT_SYMBOL_GPL(kernfs_put);
  237. static int kernfs_dop_revalidate(struct dentry *dentry, unsigned int flags)
  238. {
  239. struct kernfs_node *kn;
  240. if (flags & LOOKUP_RCU)
  241. return -ECHILD;
  242. /* Always perform fresh lookup for negatives */
  243. if (!dentry->d_inode)
  244. goto out_bad_unlocked;
  245. kn = dentry->d_fsdata;
  246. mutex_lock(&kernfs_mutex);
  247. /* The kernfs node has been deleted */
  248. if (kn->flags & KERNFS_REMOVED)
  249. goto out_bad;
  250. /* The kernfs node has been moved? */
  251. if (dentry->d_parent->d_fsdata != kn->parent)
  252. goto out_bad;
  253. /* The kernfs node has been renamed */
  254. if (strcmp(dentry->d_name.name, kn->name) != 0)
  255. goto out_bad;
  256. /* The kernfs node has been moved to a different namespace */
  257. if (kn->parent && kernfs_ns_enabled(kn->parent) &&
  258. kernfs_info(dentry->d_sb)->ns != kn->ns)
  259. goto out_bad;
  260. mutex_unlock(&kernfs_mutex);
  261. out_valid:
  262. return 1;
  263. out_bad:
  264. mutex_unlock(&kernfs_mutex);
  265. out_bad_unlocked:
  266. /*
  267. * @dentry doesn't match the underlying kernfs node, drop the
  268. * dentry and force lookup. If we have submounts we must allow the
  269. * vfs caches to lie about the state of the filesystem to prevent
  270. * leaks and other nasty things, so use check_submounts_and_drop()
  271. * instead of d_drop().
  272. */
  273. if (check_submounts_and_drop(dentry) != 0)
  274. goto out_valid;
  275. return 0;
  276. }
  277. static void kernfs_dop_release(struct dentry *dentry)
  278. {
  279. kernfs_put(dentry->d_fsdata);
  280. }
  281. const struct dentry_operations kernfs_dops = {
  282. .d_revalidate = kernfs_dop_revalidate,
  283. .d_release = kernfs_dop_release,
  284. };
  285. static struct kernfs_node *__kernfs_new_node(struct kernfs_root *root,
  286. const char *name, umode_t mode,
  287. unsigned flags)
  288. {
  289. char *dup_name = NULL;
  290. struct kernfs_node *kn;
  291. int ret;
  292. if (!(flags & KERNFS_STATIC_NAME)) {
  293. name = dup_name = kstrdup(name, GFP_KERNEL);
  294. if (!name)
  295. return NULL;
  296. }
  297. kn = kmem_cache_zalloc(kernfs_node_cache, GFP_KERNEL);
  298. if (!kn)
  299. goto err_out1;
  300. ret = ida_simple_get(&root->ino_ida, 1, 0, GFP_KERNEL);
  301. if (ret < 0)
  302. goto err_out2;
  303. kn->ino = ret;
  304. atomic_set(&kn->count, 1);
  305. atomic_set(&kn->active, 0);
  306. kn->name = name;
  307. kn->mode = mode;
  308. kn->flags = flags | KERNFS_REMOVED;
  309. return kn;
  310. err_out2:
  311. kmem_cache_free(kernfs_node_cache, kn);
  312. err_out1:
  313. kfree(dup_name);
  314. return NULL;
  315. }
  316. struct kernfs_node *kernfs_new_node(struct kernfs_node *parent,
  317. const char *name, umode_t mode,
  318. unsigned flags)
  319. {
  320. struct kernfs_node *kn;
  321. kn = __kernfs_new_node(kernfs_root(parent), name, mode, flags);
  322. if (kn) {
  323. kernfs_get(parent);
  324. kn->parent = parent;
  325. }
  326. return kn;
  327. }
  328. /**
  329. * kernfs_addrm_start - prepare for kernfs_node add/remove
  330. * @acxt: pointer to kernfs_addrm_cxt to be used
  331. *
  332. * This function is called when the caller is about to add or remove
  333. * kernfs_node. This function acquires kernfs_mutex. @acxt is used
  334. * to keep and pass context to other addrm functions.
  335. *
  336. * LOCKING:
  337. * Kernel thread context (may sleep). kernfs_mutex is locked on
  338. * return.
  339. */
  340. void kernfs_addrm_start(struct kernfs_addrm_cxt *acxt)
  341. __acquires(kernfs_mutex)
  342. {
  343. memset(acxt, 0, sizeof(*acxt));
  344. mutex_lock(&kernfs_mutex);
  345. }
  346. /**
  347. * kernfs_add_one - add kernfs_node to parent without warning
  348. * @acxt: addrm context to use
  349. * @kn: kernfs_node to be added
  350. *
  351. * The caller must already have initialized @kn->parent. This
  352. * function increments nlink of the parent's inode if @kn is a
  353. * directory and link into the children list of the parent.
  354. *
  355. * This function should be called between calls to
  356. * kernfs_addrm_start() and kernfs_addrm_finish() and should be passed
  357. * the same @acxt as passed to kernfs_addrm_start().
  358. *
  359. * LOCKING:
  360. * Determined by kernfs_addrm_start().
  361. *
  362. * RETURNS:
  363. * 0 on success, -EEXIST if entry with the given name already
  364. * exists.
  365. */
  366. int kernfs_add_one(struct kernfs_addrm_cxt *acxt, struct kernfs_node *kn)
  367. {
  368. struct kernfs_node *parent = kn->parent;
  369. bool has_ns = kernfs_ns_enabled(parent);
  370. struct kernfs_iattrs *ps_iattr;
  371. int ret;
  372. if (has_ns != (bool)kn->ns) {
  373. WARN(1, KERN_WARNING "kernfs: ns %s in '%s' for '%s'\n",
  374. has_ns ? "required" : "invalid", parent->name, kn->name);
  375. return -EINVAL;
  376. }
  377. if (kernfs_type(parent) != KERNFS_DIR)
  378. return -EINVAL;
  379. if (parent->flags & KERNFS_REMOVED)
  380. return -ENOENT;
  381. kn->hash = kernfs_name_hash(kn->name, kn->ns);
  382. ret = kernfs_link_sibling(kn);
  383. if (ret)
  384. return ret;
  385. /* Update timestamps on the parent */
  386. ps_iattr = parent->iattr;
  387. if (ps_iattr) {
  388. struct iattr *ps_iattrs = &ps_iattr->ia_iattr;
  389. ps_iattrs->ia_ctime = ps_iattrs->ia_mtime = CURRENT_TIME;
  390. }
  391. /* Mark the entry added into directory tree */
  392. kn->flags &= ~KERNFS_REMOVED;
  393. return 0;
  394. }
  395. /**
  396. * kernfs_remove_one - remove kernfs_node from parent
  397. * @acxt: addrm context to use
  398. * @kn: kernfs_node to be removed
  399. *
  400. * Mark @kn removed and drop nlink of parent inode if @kn is a
  401. * directory. @kn is unlinked from the children list.
  402. *
  403. * This function should be called between calls to
  404. * kernfs_addrm_start() and kernfs_addrm_finish() and should be
  405. * passed the same @acxt as passed to kernfs_addrm_start().
  406. *
  407. * LOCKING:
  408. * Determined by kernfs_addrm_start().
  409. */
  410. static void kernfs_remove_one(struct kernfs_addrm_cxt *acxt,
  411. struct kernfs_node *kn)
  412. {
  413. struct kernfs_iattrs *ps_iattr;
  414. /*
  415. * Removal can be called multiple times on the same node. Only the
  416. * first invocation is effective and puts the base ref.
  417. */
  418. if (kn->flags & KERNFS_REMOVED)
  419. return;
  420. if (kn->parent) {
  421. kernfs_unlink_sibling(kn);
  422. /* Update timestamps on the parent */
  423. ps_iattr = kn->parent->iattr;
  424. if (ps_iattr) {
  425. ps_iattr->ia_iattr.ia_ctime = CURRENT_TIME;
  426. ps_iattr->ia_iattr.ia_mtime = CURRENT_TIME;
  427. }
  428. }
  429. kn->flags |= KERNFS_REMOVED;
  430. kn->u.removed_list = acxt->removed;
  431. acxt->removed = kn;
  432. }
  433. /**
  434. * kernfs_addrm_finish - finish up kernfs_node add/remove
  435. * @acxt: addrm context to finish up
  436. *
  437. * Finish up kernfs_node add/remove. Resources acquired by
  438. * kernfs_addrm_start() are released and removed kernfs_nodes are
  439. * cleaned up.
  440. *
  441. * LOCKING:
  442. * kernfs_mutex is released.
  443. */
  444. void kernfs_addrm_finish(struct kernfs_addrm_cxt *acxt)
  445. __releases(kernfs_mutex)
  446. {
  447. /* release resources acquired by kernfs_addrm_start() */
  448. mutex_unlock(&kernfs_mutex);
  449. /* kill removed kernfs_nodes */
  450. while (acxt->removed) {
  451. struct kernfs_node *kn = acxt->removed;
  452. acxt->removed = kn->u.removed_list;
  453. kernfs_deactivate(kn);
  454. kernfs_unmap_bin_file(kn);
  455. kernfs_put(kn);
  456. }
  457. }
  458. /**
  459. * kernfs_find_ns - find kernfs_node with the given name
  460. * @parent: kernfs_node to search under
  461. * @name: name to look for
  462. * @ns: the namespace tag to use
  463. *
  464. * Look for kernfs_node with name @name under @parent. Returns pointer to
  465. * the found kernfs_node on success, %NULL on failure.
  466. */
  467. static struct kernfs_node *kernfs_find_ns(struct kernfs_node *parent,
  468. const unsigned char *name,
  469. const void *ns)
  470. {
  471. struct rb_node *node = parent->dir.children.rb_node;
  472. bool has_ns = kernfs_ns_enabled(parent);
  473. unsigned int hash;
  474. lockdep_assert_held(&kernfs_mutex);
  475. if (has_ns != (bool)ns) {
  476. WARN(1, KERN_WARNING "kernfs: ns %s in '%s' for '%s'\n",
  477. has_ns ? "required" : "invalid", parent->name, name);
  478. return NULL;
  479. }
  480. hash = kernfs_name_hash(name, ns);
  481. while (node) {
  482. struct kernfs_node *kn;
  483. int result;
  484. kn = rb_to_kn(node);
  485. result = kernfs_name_compare(hash, name, ns, kn);
  486. if (result < 0)
  487. node = node->rb_left;
  488. else if (result > 0)
  489. node = node->rb_right;
  490. else
  491. return kn;
  492. }
  493. return NULL;
  494. }
  495. /**
  496. * kernfs_find_and_get_ns - find and get kernfs_node with the given name
  497. * @parent: kernfs_node to search under
  498. * @name: name to look for
  499. * @ns: the namespace tag to use
  500. *
  501. * Look for kernfs_node with name @name under @parent and get a reference
  502. * if found. This function may sleep and returns pointer to the found
  503. * kernfs_node on success, %NULL on failure.
  504. */
  505. struct kernfs_node *kernfs_find_and_get_ns(struct kernfs_node *parent,
  506. const char *name, const void *ns)
  507. {
  508. struct kernfs_node *kn;
  509. mutex_lock(&kernfs_mutex);
  510. kn = kernfs_find_ns(parent, name, ns);
  511. kernfs_get(kn);
  512. mutex_unlock(&kernfs_mutex);
  513. return kn;
  514. }
  515. EXPORT_SYMBOL_GPL(kernfs_find_and_get_ns);
  516. /**
  517. * kernfs_create_root - create a new kernfs hierarchy
  518. * @kdops: optional directory syscall operations for the hierarchy
  519. * @priv: opaque data associated with the new directory
  520. *
  521. * Returns the root of the new hierarchy on success, ERR_PTR() value on
  522. * failure.
  523. */
  524. struct kernfs_root *kernfs_create_root(struct kernfs_dir_ops *kdops, void *priv)
  525. {
  526. struct kernfs_root *root;
  527. struct kernfs_node *kn;
  528. root = kzalloc(sizeof(*root), GFP_KERNEL);
  529. if (!root)
  530. return ERR_PTR(-ENOMEM);
  531. ida_init(&root->ino_ida);
  532. kn = __kernfs_new_node(root, "", S_IFDIR | S_IRUGO | S_IXUGO,
  533. KERNFS_DIR);
  534. if (!kn) {
  535. ida_destroy(&root->ino_ida);
  536. kfree(root);
  537. return ERR_PTR(-ENOMEM);
  538. }
  539. kn->flags &= ~KERNFS_REMOVED;
  540. kn->priv = priv;
  541. kn->dir.root = root;
  542. root->dir_ops = kdops;
  543. root->kn = kn;
  544. return root;
  545. }
  546. /**
  547. * kernfs_destroy_root - destroy a kernfs hierarchy
  548. * @root: root of the hierarchy to destroy
  549. *
  550. * Destroy the hierarchy anchored at @root by removing all existing
  551. * directories and destroying @root.
  552. */
  553. void kernfs_destroy_root(struct kernfs_root *root)
  554. {
  555. kernfs_remove(root->kn); /* will also free @root */
  556. }
  557. /**
  558. * kernfs_create_dir_ns - create a directory
  559. * @parent: parent in which to create a new directory
  560. * @name: name of the new directory
  561. * @mode: mode of the new directory
  562. * @priv: opaque data associated with the new directory
  563. * @ns: optional namespace tag of the directory
  564. *
  565. * Returns the created node on success, ERR_PTR() value on failure.
  566. */
  567. struct kernfs_node *kernfs_create_dir_ns(struct kernfs_node *parent,
  568. const char *name, umode_t mode,
  569. void *priv, const void *ns)
  570. {
  571. struct kernfs_addrm_cxt acxt;
  572. struct kernfs_node *kn;
  573. int rc;
  574. /* allocate */
  575. kn = kernfs_new_node(parent, name, mode | S_IFDIR, KERNFS_DIR);
  576. if (!kn)
  577. return ERR_PTR(-ENOMEM);
  578. kn->dir.root = parent->dir.root;
  579. kn->ns = ns;
  580. kn->priv = priv;
  581. /* link in */
  582. kernfs_addrm_start(&acxt);
  583. rc = kernfs_add_one(&acxt, kn);
  584. kernfs_addrm_finish(&acxt);
  585. if (!rc)
  586. return kn;
  587. kernfs_put(kn);
  588. return ERR_PTR(rc);
  589. }
  590. static struct dentry *kernfs_iop_lookup(struct inode *dir,
  591. struct dentry *dentry,
  592. unsigned int flags)
  593. {
  594. struct dentry *ret;
  595. struct kernfs_node *parent = dentry->d_parent->d_fsdata;
  596. struct kernfs_node *kn;
  597. struct inode *inode;
  598. const void *ns = NULL;
  599. mutex_lock(&kernfs_mutex);
  600. if (kernfs_ns_enabled(parent))
  601. ns = kernfs_info(dir->i_sb)->ns;
  602. kn = kernfs_find_ns(parent, dentry->d_name.name, ns);
  603. /* no such entry */
  604. if (!kn) {
  605. ret = NULL;
  606. goto out_unlock;
  607. }
  608. kernfs_get(kn);
  609. dentry->d_fsdata = kn;
  610. /* attach dentry and inode */
  611. inode = kernfs_get_inode(dir->i_sb, kn);
  612. if (!inode) {
  613. ret = ERR_PTR(-ENOMEM);
  614. goto out_unlock;
  615. }
  616. /* instantiate and hash dentry */
  617. ret = d_materialise_unique(dentry, inode);
  618. out_unlock:
  619. mutex_unlock(&kernfs_mutex);
  620. return ret;
  621. }
  622. static int kernfs_iop_mkdir(struct inode *dir, struct dentry *dentry,
  623. umode_t mode)
  624. {
  625. struct kernfs_node *parent = dir->i_private;
  626. struct kernfs_dir_ops *kdops = kernfs_root(parent)->dir_ops;
  627. if (!kdops || !kdops->mkdir)
  628. return -EPERM;
  629. return kdops->mkdir(parent, dentry->d_name.name, mode);
  630. }
  631. static int kernfs_iop_rmdir(struct inode *dir, struct dentry *dentry)
  632. {
  633. struct kernfs_node *kn = dentry->d_fsdata;
  634. struct kernfs_dir_ops *kdops = kernfs_root(kn)->dir_ops;
  635. if (!kdops || !kdops->rmdir)
  636. return -EPERM;
  637. return kdops->rmdir(kn);
  638. }
  639. static int kernfs_iop_rename(struct inode *old_dir, struct dentry *old_dentry,
  640. struct inode *new_dir, struct dentry *new_dentry)
  641. {
  642. struct kernfs_node *kn = old_dentry->d_fsdata;
  643. struct kernfs_node *new_parent = new_dir->i_private;
  644. struct kernfs_dir_ops *kdops = kernfs_root(kn)->dir_ops;
  645. if (!kdops || !kdops->rename)
  646. return -EPERM;
  647. return kdops->rename(kn, new_parent, new_dentry->d_name.name);
  648. }
  649. const struct inode_operations kernfs_dir_iops = {
  650. .lookup = kernfs_iop_lookup,
  651. .permission = kernfs_iop_permission,
  652. .setattr = kernfs_iop_setattr,
  653. .getattr = kernfs_iop_getattr,
  654. .setxattr = kernfs_iop_setxattr,
  655. .removexattr = kernfs_iop_removexattr,
  656. .getxattr = kernfs_iop_getxattr,
  657. .listxattr = kernfs_iop_listxattr,
  658. .mkdir = kernfs_iop_mkdir,
  659. .rmdir = kernfs_iop_rmdir,
  660. .rename = kernfs_iop_rename,
  661. };
  662. static struct kernfs_node *kernfs_leftmost_descendant(struct kernfs_node *pos)
  663. {
  664. struct kernfs_node *last;
  665. while (true) {
  666. struct rb_node *rbn;
  667. last = pos;
  668. if (kernfs_type(pos) != KERNFS_DIR)
  669. break;
  670. rbn = rb_first(&pos->dir.children);
  671. if (!rbn)
  672. break;
  673. pos = rb_to_kn(rbn);
  674. }
  675. return last;
  676. }
  677. /**
  678. * kernfs_next_descendant_post - find the next descendant for post-order walk
  679. * @pos: the current position (%NULL to initiate traversal)
  680. * @root: kernfs_node whose descendants to walk
  681. *
  682. * Find the next descendant to visit for post-order traversal of @root's
  683. * descendants. @root is included in the iteration and the last node to be
  684. * visited.
  685. */
  686. static struct kernfs_node *kernfs_next_descendant_post(struct kernfs_node *pos,
  687. struct kernfs_node *root)
  688. {
  689. struct rb_node *rbn;
  690. lockdep_assert_held(&kernfs_mutex);
  691. /* if first iteration, visit leftmost descendant which may be root */
  692. if (!pos)
  693. return kernfs_leftmost_descendant(root);
  694. /* if we visited @root, we're done */
  695. if (pos == root)
  696. return NULL;
  697. /* if there's an unvisited sibling, visit its leftmost descendant */
  698. rbn = rb_next(&pos->rb);
  699. if (rbn)
  700. return kernfs_leftmost_descendant(rb_to_kn(rbn));
  701. /* no sibling left, visit parent */
  702. return pos->parent;
  703. }
  704. static void __kernfs_remove(struct kernfs_addrm_cxt *acxt,
  705. struct kernfs_node *kn)
  706. {
  707. struct kernfs_node *pos, *next;
  708. if (!kn)
  709. return;
  710. pr_debug("kernfs %s: removing\n", kn->name);
  711. next = NULL;
  712. do {
  713. pos = next;
  714. next = kernfs_next_descendant_post(pos, kn);
  715. if (pos)
  716. kernfs_remove_one(acxt, pos);
  717. } while (next);
  718. }
  719. /**
  720. * kernfs_remove - remove a kernfs_node recursively
  721. * @kn: the kernfs_node to remove
  722. *
  723. * Remove @kn along with all its subdirectories and files.
  724. */
  725. void kernfs_remove(struct kernfs_node *kn)
  726. {
  727. struct kernfs_addrm_cxt acxt;
  728. kernfs_addrm_start(&acxt);
  729. __kernfs_remove(&acxt, kn);
  730. kernfs_addrm_finish(&acxt);
  731. }
  732. /**
  733. * kernfs_remove_by_name_ns - find a kernfs_node by name and remove it
  734. * @parent: parent of the target
  735. * @name: name of the kernfs_node to remove
  736. * @ns: namespace tag of the kernfs_node to remove
  737. *
  738. * Look for the kernfs_node with @name and @ns under @parent and remove it.
  739. * Returns 0 on success, -ENOENT if such entry doesn't exist.
  740. */
  741. int kernfs_remove_by_name_ns(struct kernfs_node *parent, const char *name,
  742. const void *ns)
  743. {
  744. struct kernfs_addrm_cxt acxt;
  745. struct kernfs_node *kn;
  746. if (!parent) {
  747. WARN(1, KERN_WARNING "kernfs: can not remove '%s', no directory\n",
  748. name);
  749. return -ENOENT;
  750. }
  751. kernfs_addrm_start(&acxt);
  752. kn = kernfs_find_ns(parent, name, ns);
  753. if (kn)
  754. __kernfs_remove(&acxt, kn);
  755. kernfs_addrm_finish(&acxt);
  756. if (kn)
  757. return 0;
  758. else
  759. return -ENOENT;
  760. }
  761. /**
  762. * kernfs_rename_ns - move and rename a kernfs_node
  763. * @kn: target node
  764. * @new_parent: new parent to put @sd under
  765. * @new_name: new name
  766. * @new_ns: new namespace tag
  767. */
  768. int kernfs_rename_ns(struct kernfs_node *kn, struct kernfs_node *new_parent,
  769. const char *new_name, const void *new_ns)
  770. {
  771. int error;
  772. mutex_lock(&kernfs_mutex);
  773. error = -ENOENT;
  774. if ((kn->flags | new_parent->flags) & KERNFS_REMOVED)
  775. goto out;
  776. error = 0;
  777. if ((kn->parent == new_parent) && (kn->ns == new_ns) &&
  778. (strcmp(kn->name, new_name) == 0))
  779. goto out; /* nothing to rename */
  780. error = -EEXIST;
  781. if (kernfs_find_ns(new_parent, new_name, new_ns))
  782. goto out;
  783. /* rename kernfs_node */
  784. if (strcmp(kn->name, new_name) != 0) {
  785. error = -ENOMEM;
  786. new_name = kstrdup(new_name, GFP_KERNEL);
  787. if (!new_name)
  788. goto out;
  789. if (kn->flags & KERNFS_STATIC_NAME)
  790. kn->flags &= ~KERNFS_STATIC_NAME;
  791. else
  792. kfree(kn->name);
  793. kn->name = new_name;
  794. }
  795. /*
  796. * Move to the appropriate place in the appropriate directories rbtree.
  797. */
  798. kernfs_unlink_sibling(kn);
  799. kernfs_get(new_parent);
  800. kernfs_put(kn->parent);
  801. kn->ns = new_ns;
  802. kn->hash = kernfs_name_hash(kn->name, kn->ns);
  803. kn->parent = new_parent;
  804. kernfs_link_sibling(kn);
  805. error = 0;
  806. out:
  807. mutex_unlock(&kernfs_mutex);
  808. return error;
  809. }
  810. /* Relationship between s_mode and the DT_xxx types */
  811. static inline unsigned char dt_type(struct kernfs_node *kn)
  812. {
  813. return (kn->mode >> 12) & 15;
  814. }
  815. static int kernfs_dir_fop_release(struct inode *inode, struct file *filp)
  816. {
  817. kernfs_put(filp->private_data);
  818. return 0;
  819. }
  820. static struct kernfs_node *kernfs_dir_pos(const void *ns,
  821. struct kernfs_node *parent, loff_t hash, struct kernfs_node *pos)
  822. {
  823. if (pos) {
  824. int valid = !(pos->flags & KERNFS_REMOVED) &&
  825. pos->parent == parent && hash == pos->hash;
  826. kernfs_put(pos);
  827. if (!valid)
  828. pos = NULL;
  829. }
  830. if (!pos && (hash > 1) && (hash < INT_MAX)) {
  831. struct rb_node *node = parent->dir.children.rb_node;
  832. while (node) {
  833. pos = rb_to_kn(node);
  834. if (hash < pos->hash)
  835. node = node->rb_left;
  836. else if (hash > pos->hash)
  837. node = node->rb_right;
  838. else
  839. break;
  840. }
  841. }
  842. /* Skip over entries in the wrong namespace */
  843. while (pos && pos->ns != ns) {
  844. struct rb_node *node = rb_next(&pos->rb);
  845. if (!node)
  846. pos = NULL;
  847. else
  848. pos = rb_to_kn(node);
  849. }
  850. return pos;
  851. }
  852. static struct kernfs_node *kernfs_dir_next_pos(const void *ns,
  853. struct kernfs_node *parent, ino_t ino, struct kernfs_node *pos)
  854. {
  855. pos = kernfs_dir_pos(ns, parent, ino, pos);
  856. if (pos)
  857. do {
  858. struct rb_node *node = rb_next(&pos->rb);
  859. if (!node)
  860. pos = NULL;
  861. else
  862. pos = rb_to_kn(node);
  863. } while (pos && pos->ns != ns);
  864. return pos;
  865. }
  866. static int kernfs_fop_readdir(struct file *file, struct dir_context *ctx)
  867. {
  868. struct dentry *dentry = file->f_path.dentry;
  869. struct kernfs_node *parent = dentry->d_fsdata;
  870. struct kernfs_node *pos = file->private_data;
  871. const void *ns = NULL;
  872. if (!dir_emit_dots(file, ctx))
  873. return 0;
  874. mutex_lock(&kernfs_mutex);
  875. if (kernfs_ns_enabled(parent))
  876. ns = kernfs_info(dentry->d_sb)->ns;
  877. for (pos = kernfs_dir_pos(ns, parent, ctx->pos, pos);
  878. pos;
  879. pos = kernfs_dir_next_pos(ns, parent, ctx->pos, pos)) {
  880. const char *name = pos->name;
  881. unsigned int type = dt_type(pos);
  882. int len = strlen(name);
  883. ino_t ino = pos->ino;
  884. ctx->pos = pos->hash;
  885. file->private_data = pos;
  886. kernfs_get(pos);
  887. mutex_unlock(&kernfs_mutex);
  888. if (!dir_emit(ctx, name, len, ino, type))
  889. return 0;
  890. mutex_lock(&kernfs_mutex);
  891. }
  892. mutex_unlock(&kernfs_mutex);
  893. file->private_data = NULL;
  894. ctx->pos = INT_MAX;
  895. return 0;
  896. }
  897. static loff_t kernfs_dir_fop_llseek(struct file *file, loff_t offset,
  898. int whence)
  899. {
  900. struct inode *inode = file_inode(file);
  901. loff_t ret;
  902. mutex_lock(&inode->i_mutex);
  903. ret = generic_file_llseek(file, offset, whence);
  904. mutex_unlock(&inode->i_mutex);
  905. return ret;
  906. }
  907. const struct file_operations kernfs_dir_fops = {
  908. .read = generic_read_dir,
  909. .iterate = kernfs_fop_readdir,
  910. .release = kernfs_dir_fop_release,
  911. .llseek = kernfs_dir_fop_llseek,
  912. };