dir.c 25 KB

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