dir.c 25 KB

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