dir.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421
  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. static DEFINE_SPINLOCK(kernfs_rename_lock); /* kn->parent and ->name */
  20. static char kernfs_pr_cont_buf[PATH_MAX]; /* protected by rename_lock */
  21. #define rb_to_kn(X) rb_entry((X), struct kernfs_node, rb)
  22. static bool kernfs_active(struct kernfs_node *kn)
  23. {
  24. lockdep_assert_held(&kernfs_mutex);
  25. return atomic_read(&kn->active) >= 0;
  26. }
  27. static bool kernfs_lockdep(struct kernfs_node *kn)
  28. {
  29. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  30. return kn->flags & KERNFS_LOCKDEP;
  31. #else
  32. return false;
  33. #endif
  34. }
  35. static int kernfs_name_locked(struct kernfs_node *kn, char *buf, size_t buflen)
  36. {
  37. return strlcpy(buf, kn->parent ? kn->name : "/", buflen);
  38. }
  39. static char * __must_check kernfs_path_locked(struct kernfs_node *kn, char *buf,
  40. size_t buflen)
  41. {
  42. char *p = buf + buflen;
  43. int len;
  44. *--p = '\0';
  45. do {
  46. len = strlen(kn->name);
  47. if (p - buf < len + 1) {
  48. buf[0] = '\0';
  49. p = NULL;
  50. break;
  51. }
  52. p -= len;
  53. memcpy(p, kn->name, len);
  54. *--p = '/';
  55. kn = kn->parent;
  56. } while (kn && kn->parent);
  57. return p;
  58. }
  59. /**
  60. * kernfs_name - obtain the name of a given node
  61. * @kn: kernfs_node of interest
  62. * @buf: buffer to copy @kn's name into
  63. * @buflen: size of @buf
  64. *
  65. * Copies the name of @kn into @buf of @buflen bytes. The behavior is
  66. * similar to strlcpy(). It returns the length of @kn's name and if @buf
  67. * isn't long enough, it's filled upto @buflen-1 and nul terminated.
  68. *
  69. * This function can be called from any context.
  70. */
  71. int kernfs_name(struct kernfs_node *kn, char *buf, size_t buflen)
  72. {
  73. unsigned long flags;
  74. int ret;
  75. spin_lock_irqsave(&kernfs_rename_lock, flags);
  76. ret = kernfs_name_locked(kn, buf, buflen);
  77. spin_unlock_irqrestore(&kernfs_rename_lock, flags);
  78. return ret;
  79. }
  80. /**
  81. * kernfs_path - build full path of a given node
  82. * @kn: kernfs_node of interest
  83. * @buf: buffer to copy @kn's name into
  84. * @buflen: size of @buf
  85. *
  86. * Builds and returns the full path of @kn in @buf of @buflen bytes. The
  87. * path is built from the end of @buf so the returned pointer usually
  88. * doesn't match @buf. If @buf isn't long enough, @buf is nul terminated
  89. * and %NULL is returned.
  90. */
  91. char *kernfs_path(struct kernfs_node *kn, char *buf, size_t buflen)
  92. {
  93. unsigned long flags;
  94. char *p;
  95. spin_lock_irqsave(&kernfs_rename_lock, flags);
  96. p = kernfs_path_locked(kn, buf, buflen);
  97. spin_unlock_irqrestore(&kernfs_rename_lock, flags);
  98. return p;
  99. }
  100. EXPORT_SYMBOL_GPL(kernfs_path);
  101. /**
  102. * pr_cont_kernfs_name - pr_cont name of a kernfs_node
  103. * @kn: kernfs_node of interest
  104. *
  105. * This function can be called from any context.
  106. */
  107. void pr_cont_kernfs_name(struct kernfs_node *kn)
  108. {
  109. unsigned long flags;
  110. spin_lock_irqsave(&kernfs_rename_lock, flags);
  111. kernfs_name_locked(kn, kernfs_pr_cont_buf, sizeof(kernfs_pr_cont_buf));
  112. pr_cont("%s", kernfs_pr_cont_buf);
  113. spin_unlock_irqrestore(&kernfs_rename_lock, flags);
  114. }
  115. /**
  116. * pr_cont_kernfs_path - pr_cont path of a kernfs_node
  117. * @kn: kernfs_node of interest
  118. *
  119. * This function can be called from any context.
  120. */
  121. void pr_cont_kernfs_path(struct kernfs_node *kn)
  122. {
  123. unsigned long flags;
  124. char *p;
  125. spin_lock_irqsave(&kernfs_rename_lock, flags);
  126. p = kernfs_path_locked(kn, kernfs_pr_cont_buf,
  127. sizeof(kernfs_pr_cont_buf));
  128. if (p)
  129. pr_cont("%s", p);
  130. else
  131. pr_cont("<name too long>");
  132. spin_unlock_irqrestore(&kernfs_rename_lock, flags);
  133. }
  134. /**
  135. * kernfs_get_parent - determine the parent node and pin it
  136. * @kn: kernfs_node of interest
  137. *
  138. * Determines @kn's parent, pins and returns it. This function can be
  139. * called from any context.
  140. */
  141. struct kernfs_node *kernfs_get_parent(struct kernfs_node *kn)
  142. {
  143. struct kernfs_node *parent;
  144. unsigned long flags;
  145. spin_lock_irqsave(&kernfs_rename_lock, flags);
  146. parent = kn->parent;
  147. kernfs_get(parent);
  148. spin_unlock_irqrestore(&kernfs_rename_lock, flags);
  149. return parent;
  150. }
  151. /**
  152. * kernfs_name_hash
  153. * @name: Null terminated string to hash
  154. * @ns: Namespace tag to hash
  155. *
  156. * Returns 31 bit hash of ns + name (so it fits in an off_t )
  157. */
  158. static unsigned int kernfs_name_hash(const char *name, const void *ns)
  159. {
  160. unsigned long hash = init_name_hash();
  161. unsigned int len = strlen(name);
  162. while (len--)
  163. hash = partial_name_hash(*name++, hash);
  164. hash = (end_name_hash(hash) ^ hash_ptr((void *)ns, 31));
  165. hash &= 0x7fffffffU;
  166. /* Reserve hash numbers 0, 1 and INT_MAX for magic directory entries */
  167. if (hash < 2)
  168. hash += 2;
  169. if (hash >= INT_MAX)
  170. hash = INT_MAX - 1;
  171. return hash;
  172. }
  173. static int kernfs_name_compare(unsigned int hash, const char *name,
  174. const void *ns, const struct kernfs_node *kn)
  175. {
  176. if (hash < kn->hash)
  177. return -1;
  178. if (hash > kn->hash)
  179. return 1;
  180. if (ns < kn->ns)
  181. return -1;
  182. if (ns > kn->ns)
  183. return 1;
  184. return strcmp(name, kn->name);
  185. }
  186. static int kernfs_sd_compare(const struct kernfs_node *left,
  187. const struct kernfs_node *right)
  188. {
  189. return kernfs_name_compare(left->hash, left->name, left->ns, right);
  190. }
  191. /**
  192. * kernfs_link_sibling - link kernfs_node into sibling rbtree
  193. * @kn: kernfs_node of interest
  194. *
  195. * Link @kn into its sibling rbtree which starts from
  196. * @kn->parent->dir.children.
  197. *
  198. * Locking:
  199. * mutex_lock(kernfs_mutex)
  200. *
  201. * RETURNS:
  202. * 0 on susccess -EEXIST on failure.
  203. */
  204. static int kernfs_link_sibling(struct kernfs_node *kn)
  205. {
  206. struct rb_node **node = &kn->parent->dir.children.rb_node;
  207. struct rb_node *parent = NULL;
  208. while (*node) {
  209. struct kernfs_node *pos;
  210. int result;
  211. pos = rb_to_kn(*node);
  212. parent = *node;
  213. result = kernfs_sd_compare(kn, pos);
  214. if (result < 0)
  215. node = &pos->rb.rb_left;
  216. else if (result > 0)
  217. node = &pos->rb.rb_right;
  218. else
  219. return -EEXIST;
  220. }
  221. /* add new node and rebalance the tree */
  222. rb_link_node(&kn->rb, parent, node);
  223. rb_insert_color(&kn->rb, &kn->parent->dir.children);
  224. /* successfully added, account subdir number */
  225. if (kernfs_type(kn) == KERNFS_DIR)
  226. kn->parent->dir.subdirs++;
  227. return 0;
  228. }
  229. /**
  230. * kernfs_unlink_sibling - unlink kernfs_node from sibling rbtree
  231. * @kn: kernfs_node of interest
  232. *
  233. * Try to unlink @kn from its sibling rbtree which starts from
  234. * kn->parent->dir.children. Returns %true if @kn was actually
  235. * removed, %false if @kn wasn't on the rbtree.
  236. *
  237. * Locking:
  238. * mutex_lock(kernfs_mutex)
  239. */
  240. static bool kernfs_unlink_sibling(struct kernfs_node *kn)
  241. {
  242. if (RB_EMPTY_NODE(&kn->rb))
  243. return false;
  244. if (kernfs_type(kn) == KERNFS_DIR)
  245. kn->parent->dir.subdirs--;
  246. rb_erase(&kn->rb, &kn->parent->dir.children);
  247. RB_CLEAR_NODE(&kn->rb);
  248. return true;
  249. }
  250. /**
  251. * kernfs_get_active - get an active reference to kernfs_node
  252. * @kn: kernfs_node to get an active reference to
  253. *
  254. * Get an active reference of @kn. This function is noop if @kn
  255. * is NULL.
  256. *
  257. * RETURNS:
  258. * Pointer to @kn on success, NULL on failure.
  259. */
  260. struct kernfs_node *kernfs_get_active(struct kernfs_node *kn)
  261. {
  262. if (unlikely(!kn))
  263. return NULL;
  264. if (!atomic_inc_unless_negative(&kn->active))
  265. return NULL;
  266. if (kernfs_lockdep(kn))
  267. rwsem_acquire_read(&kn->dep_map, 0, 1, _RET_IP_);
  268. return kn;
  269. }
  270. /**
  271. * kernfs_put_active - put an active reference to kernfs_node
  272. * @kn: kernfs_node to put an active reference to
  273. *
  274. * Put an active reference to @kn. This function is noop if @kn
  275. * is NULL.
  276. */
  277. void kernfs_put_active(struct kernfs_node *kn)
  278. {
  279. struct kernfs_root *root = kernfs_root(kn);
  280. int v;
  281. if (unlikely(!kn))
  282. return;
  283. if (kernfs_lockdep(kn))
  284. rwsem_release(&kn->dep_map, 1, _RET_IP_);
  285. v = atomic_dec_return(&kn->active);
  286. if (likely(v != KN_DEACTIVATED_BIAS))
  287. return;
  288. wake_up_all(&root->deactivate_waitq);
  289. }
  290. /**
  291. * kernfs_drain - drain kernfs_node
  292. * @kn: kernfs_node to drain
  293. *
  294. * Drain existing usages and nuke all existing mmaps of @kn. Mutiple
  295. * removers may invoke this function concurrently on @kn and all will
  296. * return after draining is complete.
  297. */
  298. static void kernfs_drain(struct kernfs_node *kn)
  299. __releases(&kernfs_mutex) __acquires(&kernfs_mutex)
  300. {
  301. struct kernfs_root *root = kernfs_root(kn);
  302. lockdep_assert_held(&kernfs_mutex);
  303. WARN_ON_ONCE(kernfs_active(kn));
  304. mutex_unlock(&kernfs_mutex);
  305. if (kernfs_lockdep(kn)) {
  306. rwsem_acquire(&kn->dep_map, 0, 0, _RET_IP_);
  307. if (atomic_read(&kn->active) != KN_DEACTIVATED_BIAS)
  308. lock_contended(&kn->dep_map, _RET_IP_);
  309. }
  310. /* but everyone should wait for draining */
  311. wait_event(root->deactivate_waitq,
  312. atomic_read(&kn->active) == KN_DEACTIVATED_BIAS);
  313. if (kernfs_lockdep(kn)) {
  314. lock_acquired(&kn->dep_map, _RET_IP_);
  315. rwsem_release(&kn->dep_map, 1, _RET_IP_);
  316. }
  317. kernfs_unmap_bin_file(kn);
  318. mutex_lock(&kernfs_mutex);
  319. }
  320. /**
  321. * kernfs_get - get a reference count on a kernfs_node
  322. * @kn: the target kernfs_node
  323. */
  324. void kernfs_get(struct kernfs_node *kn)
  325. {
  326. if (kn) {
  327. WARN_ON(!atomic_read(&kn->count));
  328. atomic_inc(&kn->count);
  329. }
  330. }
  331. EXPORT_SYMBOL_GPL(kernfs_get);
  332. /**
  333. * kernfs_put - put a reference count on a kernfs_node
  334. * @kn: the target kernfs_node
  335. *
  336. * Put a reference count of @kn and destroy it if it reached zero.
  337. */
  338. void kernfs_put(struct kernfs_node *kn)
  339. {
  340. struct kernfs_node *parent;
  341. struct kernfs_root *root;
  342. if (!kn || !atomic_dec_and_test(&kn->count))
  343. return;
  344. root = kernfs_root(kn);
  345. repeat:
  346. /*
  347. * Moving/renaming is always done while holding reference.
  348. * kn->parent won't change beneath us.
  349. */
  350. parent = kn->parent;
  351. WARN_ONCE(atomic_read(&kn->active) != KN_DEACTIVATED_BIAS,
  352. "kernfs_put: %s/%s: released with incorrect active_ref %d\n",
  353. parent ? parent->name : "", kn->name, atomic_read(&kn->active));
  354. if (kernfs_type(kn) == KERNFS_LINK)
  355. kernfs_put(kn->symlink.target_kn);
  356. kfree_const(kn->name);
  357. if (kn->iattr) {
  358. if (kn->iattr->ia_secdata)
  359. security_release_secctx(kn->iattr->ia_secdata,
  360. kn->iattr->ia_secdata_len);
  361. simple_xattrs_free(&kn->iattr->xattrs);
  362. }
  363. kfree(kn->iattr);
  364. ida_simple_remove(&root->ino_ida, kn->ino);
  365. kmem_cache_free(kernfs_node_cache, kn);
  366. kn = parent;
  367. if (kn) {
  368. if (atomic_dec_and_test(&kn->count))
  369. goto repeat;
  370. } else {
  371. /* just released the root kn, free @root too */
  372. ida_destroy(&root->ino_ida);
  373. kfree(root);
  374. }
  375. }
  376. EXPORT_SYMBOL_GPL(kernfs_put);
  377. static int kernfs_dop_revalidate(struct dentry *dentry, unsigned int flags)
  378. {
  379. struct kernfs_node *kn;
  380. if (flags & LOOKUP_RCU)
  381. return -ECHILD;
  382. /* Always perform fresh lookup for negatives */
  383. if (!dentry->d_inode)
  384. goto out_bad_unlocked;
  385. kn = dentry->d_fsdata;
  386. mutex_lock(&kernfs_mutex);
  387. /* The kernfs node has been deactivated */
  388. if (!kernfs_active(kn))
  389. goto out_bad;
  390. /* The kernfs node has been moved? */
  391. if (dentry->d_parent->d_fsdata != kn->parent)
  392. goto out_bad;
  393. /* The kernfs node has been renamed */
  394. if (strcmp(dentry->d_name.name, kn->name) != 0)
  395. goto out_bad;
  396. /* The kernfs node has been moved to a different namespace */
  397. if (kn->parent && kernfs_ns_enabled(kn->parent) &&
  398. kernfs_info(dentry->d_sb)->ns != kn->ns)
  399. goto out_bad;
  400. mutex_unlock(&kernfs_mutex);
  401. return 1;
  402. out_bad:
  403. mutex_unlock(&kernfs_mutex);
  404. out_bad_unlocked:
  405. return 0;
  406. }
  407. static void kernfs_dop_release(struct dentry *dentry)
  408. {
  409. kernfs_put(dentry->d_fsdata);
  410. }
  411. const struct dentry_operations kernfs_dops = {
  412. .d_revalidate = kernfs_dop_revalidate,
  413. .d_release = kernfs_dop_release,
  414. };
  415. /**
  416. * kernfs_node_from_dentry - determine kernfs_node associated with a dentry
  417. * @dentry: the dentry in question
  418. *
  419. * Return the kernfs_node associated with @dentry. If @dentry is not a
  420. * kernfs one, %NULL is returned.
  421. *
  422. * While the returned kernfs_node will stay accessible as long as @dentry
  423. * is accessible, the returned node can be in any state and the caller is
  424. * fully responsible for determining what's accessible.
  425. */
  426. struct kernfs_node *kernfs_node_from_dentry(struct dentry *dentry)
  427. {
  428. if (dentry->d_sb->s_op == &kernfs_sops)
  429. return dentry->d_fsdata;
  430. return NULL;
  431. }
  432. static struct kernfs_node *__kernfs_new_node(struct kernfs_root *root,
  433. const char *name, umode_t mode,
  434. unsigned flags)
  435. {
  436. struct kernfs_node *kn;
  437. int ret;
  438. name = kstrdup_const(name, GFP_KERNEL);
  439. if (!name)
  440. return NULL;
  441. kn = kmem_cache_zalloc(kernfs_node_cache, GFP_KERNEL);
  442. if (!kn)
  443. goto err_out1;
  444. ret = ida_simple_get(&root->ino_ida, 1, 0, GFP_KERNEL);
  445. if (ret < 0)
  446. goto err_out2;
  447. kn->ino = ret;
  448. atomic_set(&kn->count, 1);
  449. atomic_set(&kn->active, KN_DEACTIVATED_BIAS);
  450. RB_CLEAR_NODE(&kn->rb);
  451. kn->name = name;
  452. kn->mode = mode;
  453. kn->flags = flags;
  454. return kn;
  455. err_out2:
  456. kmem_cache_free(kernfs_node_cache, kn);
  457. err_out1:
  458. kfree_const(name);
  459. return NULL;
  460. }
  461. struct kernfs_node *kernfs_new_node(struct kernfs_node *parent,
  462. const char *name, umode_t mode,
  463. unsigned flags)
  464. {
  465. struct kernfs_node *kn;
  466. kn = __kernfs_new_node(kernfs_root(parent), name, mode, flags);
  467. if (kn) {
  468. kernfs_get(parent);
  469. kn->parent = parent;
  470. }
  471. return kn;
  472. }
  473. /**
  474. * kernfs_add_one - add kernfs_node to parent without warning
  475. * @kn: kernfs_node to be added
  476. *
  477. * The caller must already have initialized @kn->parent. This
  478. * function increments nlink of the parent's inode if @kn is a
  479. * directory and link into the children list of the parent.
  480. *
  481. * RETURNS:
  482. * 0 on success, -EEXIST if entry with the given name already
  483. * exists.
  484. */
  485. int kernfs_add_one(struct kernfs_node *kn)
  486. {
  487. struct kernfs_node *parent = kn->parent;
  488. struct kernfs_iattrs *ps_iattr;
  489. bool has_ns;
  490. int ret;
  491. mutex_lock(&kernfs_mutex);
  492. ret = -EINVAL;
  493. has_ns = kernfs_ns_enabled(parent);
  494. if (WARN(has_ns != (bool)kn->ns, KERN_WARNING "kernfs: ns %s in '%s' for '%s'\n",
  495. has_ns ? "required" : "invalid", parent->name, kn->name))
  496. goto out_unlock;
  497. if (kernfs_type(parent) != KERNFS_DIR)
  498. goto out_unlock;
  499. ret = -ENOENT;
  500. if ((parent->flags & KERNFS_ACTIVATED) && !kernfs_active(parent))
  501. goto out_unlock;
  502. kn->hash = kernfs_name_hash(kn->name, kn->ns);
  503. ret = kernfs_link_sibling(kn);
  504. if (ret)
  505. goto out_unlock;
  506. /* Update timestamps on the parent */
  507. ps_iattr = parent->iattr;
  508. if (ps_iattr) {
  509. struct iattr *ps_iattrs = &ps_iattr->ia_iattr;
  510. ps_iattrs->ia_ctime = ps_iattrs->ia_mtime = CURRENT_TIME;
  511. }
  512. mutex_unlock(&kernfs_mutex);
  513. /*
  514. * Activate the new node unless CREATE_DEACTIVATED is requested.
  515. * If not activated here, the kernfs user is responsible for
  516. * activating the node with kernfs_activate(). A node which hasn't
  517. * been activated is not visible to userland and its removal won't
  518. * trigger deactivation.
  519. */
  520. if (!(kernfs_root(kn)->flags & KERNFS_ROOT_CREATE_DEACTIVATED))
  521. kernfs_activate(kn);
  522. return 0;
  523. out_unlock:
  524. mutex_unlock(&kernfs_mutex);
  525. return ret;
  526. }
  527. /**
  528. * kernfs_find_ns - find kernfs_node with the given name
  529. * @parent: kernfs_node to search under
  530. * @name: name to look for
  531. * @ns: the namespace tag to use
  532. *
  533. * Look for kernfs_node with name @name under @parent. Returns pointer to
  534. * the found kernfs_node on success, %NULL on failure.
  535. */
  536. static struct kernfs_node *kernfs_find_ns(struct kernfs_node *parent,
  537. const unsigned char *name,
  538. const void *ns)
  539. {
  540. struct rb_node *node = parent->dir.children.rb_node;
  541. bool has_ns = kernfs_ns_enabled(parent);
  542. unsigned int hash;
  543. lockdep_assert_held(&kernfs_mutex);
  544. if (has_ns != (bool)ns) {
  545. WARN(1, KERN_WARNING "kernfs: ns %s in '%s' for '%s'\n",
  546. has_ns ? "required" : "invalid", parent->name, name);
  547. return NULL;
  548. }
  549. hash = kernfs_name_hash(name, ns);
  550. while (node) {
  551. struct kernfs_node *kn;
  552. int result;
  553. kn = rb_to_kn(node);
  554. result = kernfs_name_compare(hash, name, ns, kn);
  555. if (result < 0)
  556. node = node->rb_left;
  557. else if (result > 0)
  558. node = node->rb_right;
  559. else
  560. return kn;
  561. }
  562. return NULL;
  563. }
  564. /**
  565. * kernfs_find_and_get_ns - find and get kernfs_node with the given name
  566. * @parent: kernfs_node to search under
  567. * @name: name to look for
  568. * @ns: the namespace tag to use
  569. *
  570. * Look for kernfs_node with name @name under @parent and get a reference
  571. * if found. This function may sleep and returns pointer to the found
  572. * kernfs_node on success, %NULL on failure.
  573. */
  574. struct kernfs_node *kernfs_find_and_get_ns(struct kernfs_node *parent,
  575. const char *name, const void *ns)
  576. {
  577. struct kernfs_node *kn;
  578. mutex_lock(&kernfs_mutex);
  579. kn = kernfs_find_ns(parent, name, ns);
  580. kernfs_get(kn);
  581. mutex_unlock(&kernfs_mutex);
  582. return kn;
  583. }
  584. EXPORT_SYMBOL_GPL(kernfs_find_and_get_ns);
  585. /**
  586. * kernfs_create_root - create a new kernfs hierarchy
  587. * @scops: optional syscall operations for the hierarchy
  588. * @flags: KERNFS_ROOT_* flags
  589. * @priv: opaque data associated with the new directory
  590. *
  591. * Returns the root of the new hierarchy on success, ERR_PTR() value on
  592. * failure.
  593. */
  594. struct kernfs_root *kernfs_create_root(struct kernfs_syscall_ops *scops,
  595. unsigned int flags, void *priv)
  596. {
  597. struct kernfs_root *root;
  598. struct kernfs_node *kn;
  599. root = kzalloc(sizeof(*root), GFP_KERNEL);
  600. if (!root)
  601. return ERR_PTR(-ENOMEM);
  602. ida_init(&root->ino_ida);
  603. INIT_LIST_HEAD(&root->supers);
  604. kn = __kernfs_new_node(root, "", S_IFDIR | S_IRUGO | S_IXUGO,
  605. KERNFS_DIR);
  606. if (!kn) {
  607. ida_destroy(&root->ino_ida);
  608. kfree(root);
  609. return ERR_PTR(-ENOMEM);
  610. }
  611. kn->priv = priv;
  612. kn->dir.root = root;
  613. root->syscall_ops = scops;
  614. root->flags = flags;
  615. root->kn = kn;
  616. init_waitqueue_head(&root->deactivate_waitq);
  617. if (!(root->flags & KERNFS_ROOT_CREATE_DEACTIVATED))
  618. kernfs_activate(kn);
  619. return root;
  620. }
  621. /**
  622. * kernfs_destroy_root - destroy a kernfs hierarchy
  623. * @root: root of the hierarchy to destroy
  624. *
  625. * Destroy the hierarchy anchored at @root by removing all existing
  626. * directories and destroying @root.
  627. */
  628. void kernfs_destroy_root(struct kernfs_root *root)
  629. {
  630. kernfs_remove(root->kn); /* will also free @root */
  631. }
  632. /**
  633. * kernfs_create_dir_ns - create a directory
  634. * @parent: parent in which to create a new directory
  635. * @name: name of the new directory
  636. * @mode: mode of the new directory
  637. * @priv: opaque data associated with the new directory
  638. * @ns: optional namespace tag of the directory
  639. *
  640. * Returns the created node on success, ERR_PTR() value on failure.
  641. */
  642. struct kernfs_node *kernfs_create_dir_ns(struct kernfs_node *parent,
  643. const char *name, umode_t mode,
  644. void *priv, const void *ns)
  645. {
  646. struct kernfs_node *kn;
  647. int rc;
  648. /* allocate */
  649. kn = kernfs_new_node(parent, name, mode | S_IFDIR, KERNFS_DIR);
  650. if (!kn)
  651. return ERR_PTR(-ENOMEM);
  652. kn->dir.root = parent->dir.root;
  653. kn->ns = ns;
  654. kn->priv = priv;
  655. /* link in */
  656. rc = kernfs_add_one(kn);
  657. if (!rc)
  658. return kn;
  659. kernfs_put(kn);
  660. return ERR_PTR(rc);
  661. }
  662. static struct dentry *kernfs_iop_lookup(struct inode *dir,
  663. struct dentry *dentry,
  664. unsigned int flags)
  665. {
  666. struct dentry *ret;
  667. struct kernfs_node *parent = dentry->d_parent->d_fsdata;
  668. struct kernfs_node *kn;
  669. struct inode *inode;
  670. const void *ns = NULL;
  671. mutex_lock(&kernfs_mutex);
  672. if (kernfs_ns_enabled(parent))
  673. ns = kernfs_info(dir->i_sb)->ns;
  674. kn = kernfs_find_ns(parent, dentry->d_name.name, ns);
  675. /* no such entry */
  676. if (!kn || !kernfs_active(kn)) {
  677. ret = NULL;
  678. goto out_unlock;
  679. }
  680. kernfs_get(kn);
  681. dentry->d_fsdata = kn;
  682. /* attach dentry and inode */
  683. inode = kernfs_get_inode(dir->i_sb, kn);
  684. if (!inode) {
  685. ret = ERR_PTR(-ENOMEM);
  686. goto out_unlock;
  687. }
  688. /* instantiate and hash dentry */
  689. ret = d_splice_alias(inode, dentry);
  690. out_unlock:
  691. mutex_unlock(&kernfs_mutex);
  692. return ret;
  693. }
  694. static int kernfs_iop_mkdir(struct inode *dir, struct dentry *dentry,
  695. umode_t mode)
  696. {
  697. struct kernfs_node *parent = dir->i_private;
  698. struct kernfs_syscall_ops *scops = kernfs_root(parent)->syscall_ops;
  699. int ret;
  700. if (!scops || !scops->mkdir)
  701. return -EPERM;
  702. if (!kernfs_get_active(parent))
  703. return -ENODEV;
  704. ret = scops->mkdir(parent, dentry->d_name.name, mode);
  705. kernfs_put_active(parent);
  706. return ret;
  707. }
  708. static int kernfs_iop_rmdir(struct inode *dir, struct dentry *dentry)
  709. {
  710. struct kernfs_node *kn = dentry->d_fsdata;
  711. struct kernfs_syscall_ops *scops = kernfs_root(kn)->syscall_ops;
  712. int ret;
  713. if (!scops || !scops->rmdir)
  714. return -EPERM;
  715. if (!kernfs_get_active(kn))
  716. return -ENODEV;
  717. ret = scops->rmdir(kn);
  718. kernfs_put_active(kn);
  719. return ret;
  720. }
  721. static int kernfs_iop_rename(struct inode *old_dir, struct dentry *old_dentry,
  722. struct inode *new_dir, struct dentry *new_dentry)
  723. {
  724. struct kernfs_node *kn = old_dentry->d_fsdata;
  725. struct kernfs_node *new_parent = new_dir->i_private;
  726. struct kernfs_syscall_ops *scops = kernfs_root(kn)->syscall_ops;
  727. int ret;
  728. if (!scops || !scops->rename)
  729. return -EPERM;
  730. if (!kernfs_get_active(kn))
  731. return -ENODEV;
  732. if (!kernfs_get_active(new_parent)) {
  733. kernfs_put_active(kn);
  734. return -ENODEV;
  735. }
  736. ret = scops->rename(kn, new_parent, new_dentry->d_name.name);
  737. kernfs_put_active(new_parent);
  738. kernfs_put_active(kn);
  739. return ret;
  740. }
  741. const struct inode_operations kernfs_dir_iops = {
  742. .lookup = kernfs_iop_lookup,
  743. .permission = kernfs_iop_permission,
  744. .setattr = kernfs_iop_setattr,
  745. .getattr = kernfs_iop_getattr,
  746. .setxattr = kernfs_iop_setxattr,
  747. .removexattr = kernfs_iop_removexattr,
  748. .getxattr = kernfs_iop_getxattr,
  749. .listxattr = kernfs_iop_listxattr,
  750. .mkdir = kernfs_iop_mkdir,
  751. .rmdir = kernfs_iop_rmdir,
  752. .rename = kernfs_iop_rename,
  753. };
  754. static struct kernfs_node *kernfs_leftmost_descendant(struct kernfs_node *pos)
  755. {
  756. struct kernfs_node *last;
  757. while (true) {
  758. struct rb_node *rbn;
  759. last = pos;
  760. if (kernfs_type(pos) != KERNFS_DIR)
  761. break;
  762. rbn = rb_first(&pos->dir.children);
  763. if (!rbn)
  764. break;
  765. pos = rb_to_kn(rbn);
  766. }
  767. return last;
  768. }
  769. /**
  770. * kernfs_next_descendant_post - find the next descendant for post-order walk
  771. * @pos: the current position (%NULL to initiate traversal)
  772. * @root: kernfs_node whose descendants to walk
  773. *
  774. * Find the next descendant to visit for post-order traversal of @root's
  775. * descendants. @root is included in the iteration and the last node to be
  776. * visited.
  777. */
  778. static struct kernfs_node *kernfs_next_descendant_post(struct kernfs_node *pos,
  779. struct kernfs_node *root)
  780. {
  781. struct rb_node *rbn;
  782. lockdep_assert_held(&kernfs_mutex);
  783. /* if first iteration, visit leftmost descendant which may be root */
  784. if (!pos)
  785. return kernfs_leftmost_descendant(root);
  786. /* if we visited @root, we're done */
  787. if (pos == root)
  788. return NULL;
  789. /* if there's an unvisited sibling, visit its leftmost descendant */
  790. rbn = rb_next(&pos->rb);
  791. if (rbn)
  792. return kernfs_leftmost_descendant(rb_to_kn(rbn));
  793. /* no sibling left, visit parent */
  794. return pos->parent;
  795. }
  796. /**
  797. * kernfs_activate - activate a node which started deactivated
  798. * @kn: kernfs_node whose subtree is to be activated
  799. *
  800. * If the root has KERNFS_ROOT_CREATE_DEACTIVATED set, a newly created node
  801. * needs to be explicitly activated. A node which hasn't been activated
  802. * isn't visible to userland and deactivation is skipped during its
  803. * removal. This is useful to construct atomic init sequences where
  804. * creation of multiple nodes should either succeed or fail atomically.
  805. *
  806. * The caller is responsible for ensuring that this function is not called
  807. * after kernfs_remove*() is invoked on @kn.
  808. */
  809. void kernfs_activate(struct kernfs_node *kn)
  810. {
  811. struct kernfs_node *pos;
  812. mutex_lock(&kernfs_mutex);
  813. pos = NULL;
  814. while ((pos = kernfs_next_descendant_post(pos, kn))) {
  815. if (!pos || (pos->flags & KERNFS_ACTIVATED))
  816. continue;
  817. WARN_ON_ONCE(pos->parent && RB_EMPTY_NODE(&pos->rb));
  818. WARN_ON_ONCE(atomic_read(&pos->active) != KN_DEACTIVATED_BIAS);
  819. atomic_sub(KN_DEACTIVATED_BIAS, &pos->active);
  820. pos->flags |= KERNFS_ACTIVATED;
  821. }
  822. mutex_unlock(&kernfs_mutex);
  823. }
  824. static void __kernfs_remove(struct kernfs_node *kn)
  825. {
  826. struct kernfs_node *pos;
  827. lockdep_assert_held(&kernfs_mutex);
  828. /*
  829. * Short-circuit if non-root @kn has already finished removal.
  830. * This is for kernfs_remove_self() which plays with active ref
  831. * after removal.
  832. */
  833. if (!kn || (kn->parent && RB_EMPTY_NODE(&kn->rb)))
  834. return;
  835. pr_debug("kernfs %s: removing\n", kn->name);
  836. /* prevent any new usage under @kn by deactivating all nodes */
  837. pos = NULL;
  838. while ((pos = kernfs_next_descendant_post(pos, kn)))
  839. if (kernfs_active(pos))
  840. atomic_add(KN_DEACTIVATED_BIAS, &pos->active);
  841. /* deactivate and unlink the subtree node-by-node */
  842. do {
  843. pos = kernfs_leftmost_descendant(kn);
  844. /*
  845. * kernfs_drain() drops kernfs_mutex temporarily and @pos's
  846. * base ref could have been put by someone else by the time
  847. * the function returns. Make sure it doesn't go away
  848. * underneath us.
  849. */
  850. kernfs_get(pos);
  851. /*
  852. * Drain iff @kn was activated. This avoids draining and
  853. * its lockdep annotations for nodes which have never been
  854. * activated and allows embedding kernfs_remove() in create
  855. * error paths without worrying about draining.
  856. */
  857. if (kn->flags & KERNFS_ACTIVATED)
  858. kernfs_drain(pos);
  859. else
  860. WARN_ON_ONCE(atomic_read(&kn->active) != KN_DEACTIVATED_BIAS);
  861. /*
  862. * kernfs_unlink_sibling() succeeds once per node. Use it
  863. * to decide who's responsible for cleanups.
  864. */
  865. if (!pos->parent || kernfs_unlink_sibling(pos)) {
  866. struct kernfs_iattrs *ps_iattr =
  867. pos->parent ? pos->parent->iattr : NULL;
  868. /* update timestamps on the parent */
  869. if (ps_iattr) {
  870. ps_iattr->ia_iattr.ia_ctime = CURRENT_TIME;
  871. ps_iattr->ia_iattr.ia_mtime = CURRENT_TIME;
  872. }
  873. kernfs_put(pos);
  874. }
  875. kernfs_put(pos);
  876. } while (pos != kn);
  877. }
  878. /**
  879. * kernfs_remove - remove a kernfs_node recursively
  880. * @kn: the kernfs_node to remove
  881. *
  882. * Remove @kn along with all its subdirectories and files.
  883. */
  884. void kernfs_remove(struct kernfs_node *kn)
  885. {
  886. mutex_lock(&kernfs_mutex);
  887. __kernfs_remove(kn);
  888. mutex_unlock(&kernfs_mutex);
  889. }
  890. /**
  891. * kernfs_break_active_protection - break out of active protection
  892. * @kn: the self kernfs_node
  893. *
  894. * The caller must be running off of a kernfs operation which is invoked
  895. * with an active reference - e.g. one of kernfs_ops. Each invocation of
  896. * this function must also be matched with an invocation of
  897. * kernfs_unbreak_active_protection().
  898. *
  899. * This function releases the active reference of @kn the caller is
  900. * holding. Once this function is called, @kn may be removed at any point
  901. * and the caller is solely responsible for ensuring that the objects it
  902. * dereferences are accessible.
  903. */
  904. void kernfs_break_active_protection(struct kernfs_node *kn)
  905. {
  906. /*
  907. * Take out ourself out of the active ref dependency chain. If
  908. * we're called without an active ref, lockdep will complain.
  909. */
  910. kernfs_put_active(kn);
  911. }
  912. /**
  913. * kernfs_unbreak_active_protection - undo kernfs_break_active_protection()
  914. * @kn: the self kernfs_node
  915. *
  916. * If kernfs_break_active_protection() was called, this function must be
  917. * invoked before finishing the kernfs operation. Note that while this
  918. * function restores the active reference, it doesn't and can't actually
  919. * restore the active protection - @kn may already or be in the process of
  920. * being removed. Once kernfs_break_active_protection() is invoked, that
  921. * protection is irreversibly gone for the kernfs operation instance.
  922. *
  923. * While this function may be called at any point after
  924. * kernfs_break_active_protection() is invoked, its most useful location
  925. * would be right before the enclosing kernfs operation returns.
  926. */
  927. void kernfs_unbreak_active_protection(struct kernfs_node *kn)
  928. {
  929. /*
  930. * @kn->active could be in any state; however, the increment we do
  931. * here will be undone as soon as the enclosing kernfs operation
  932. * finishes and this temporary bump can't break anything. If @kn
  933. * is alive, nothing changes. If @kn is being deactivated, the
  934. * soon-to-follow put will either finish deactivation or restore
  935. * deactivated state. If @kn is already removed, the temporary
  936. * bump is guaranteed to be gone before @kn is released.
  937. */
  938. atomic_inc(&kn->active);
  939. if (kernfs_lockdep(kn))
  940. rwsem_acquire(&kn->dep_map, 0, 1, _RET_IP_);
  941. }
  942. /**
  943. * kernfs_remove_self - remove a kernfs_node from its own method
  944. * @kn: the self kernfs_node to remove
  945. *
  946. * The caller must be running off of a kernfs operation which is invoked
  947. * with an active reference - e.g. one of kernfs_ops. This can be used to
  948. * implement a file operation which deletes itself.
  949. *
  950. * For example, the "delete" file for a sysfs device directory can be
  951. * implemented by invoking kernfs_remove_self() on the "delete" file
  952. * itself. This function breaks the circular dependency of trying to
  953. * deactivate self while holding an active ref itself. It isn't necessary
  954. * to modify the usual removal path to use kernfs_remove_self(). The
  955. * "delete" implementation can simply invoke kernfs_remove_self() on self
  956. * before proceeding with the usual removal path. kernfs will ignore later
  957. * kernfs_remove() on self.
  958. *
  959. * kernfs_remove_self() can be called multiple times concurrently on the
  960. * same kernfs_node. Only the first one actually performs removal and
  961. * returns %true. All others will wait until the kernfs operation which
  962. * won self-removal finishes and return %false. Note that the losers wait
  963. * for the completion of not only the winning kernfs_remove_self() but also
  964. * the whole kernfs_ops which won the arbitration. This can be used to
  965. * guarantee, for example, all concurrent writes to a "delete" file to
  966. * finish only after the whole operation is complete.
  967. */
  968. bool kernfs_remove_self(struct kernfs_node *kn)
  969. {
  970. bool ret;
  971. mutex_lock(&kernfs_mutex);
  972. kernfs_break_active_protection(kn);
  973. /*
  974. * SUICIDAL is used to arbitrate among competing invocations. Only
  975. * the first one will actually perform removal. When the removal
  976. * is complete, SUICIDED is set and the active ref is restored
  977. * while holding kernfs_mutex. The ones which lost arbitration
  978. * waits for SUICDED && drained which can happen only after the
  979. * enclosing kernfs operation which executed the winning instance
  980. * of kernfs_remove_self() finished.
  981. */
  982. if (!(kn->flags & KERNFS_SUICIDAL)) {
  983. kn->flags |= KERNFS_SUICIDAL;
  984. __kernfs_remove(kn);
  985. kn->flags |= KERNFS_SUICIDED;
  986. ret = true;
  987. } else {
  988. wait_queue_head_t *waitq = &kernfs_root(kn)->deactivate_waitq;
  989. DEFINE_WAIT(wait);
  990. while (true) {
  991. prepare_to_wait(waitq, &wait, TASK_UNINTERRUPTIBLE);
  992. if ((kn->flags & KERNFS_SUICIDED) &&
  993. atomic_read(&kn->active) == KN_DEACTIVATED_BIAS)
  994. break;
  995. mutex_unlock(&kernfs_mutex);
  996. schedule();
  997. mutex_lock(&kernfs_mutex);
  998. }
  999. finish_wait(waitq, &wait);
  1000. WARN_ON_ONCE(!RB_EMPTY_NODE(&kn->rb));
  1001. ret = false;
  1002. }
  1003. /*
  1004. * This must be done while holding kernfs_mutex; otherwise, waiting
  1005. * for SUICIDED && deactivated could finish prematurely.
  1006. */
  1007. kernfs_unbreak_active_protection(kn);
  1008. mutex_unlock(&kernfs_mutex);
  1009. return ret;
  1010. }
  1011. /**
  1012. * kernfs_remove_by_name_ns - find a kernfs_node by name and remove it
  1013. * @parent: parent of the target
  1014. * @name: name of the kernfs_node to remove
  1015. * @ns: namespace tag of the kernfs_node to remove
  1016. *
  1017. * Look for the kernfs_node with @name and @ns under @parent and remove it.
  1018. * Returns 0 on success, -ENOENT if such entry doesn't exist.
  1019. */
  1020. int kernfs_remove_by_name_ns(struct kernfs_node *parent, const char *name,
  1021. const void *ns)
  1022. {
  1023. struct kernfs_node *kn;
  1024. if (!parent) {
  1025. WARN(1, KERN_WARNING "kernfs: can not remove '%s', no directory\n",
  1026. name);
  1027. return -ENOENT;
  1028. }
  1029. mutex_lock(&kernfs_mutex);
  1030. kn = kernfs_find_ns(parent, name, ns);
  1031. if (kn)
  1032. __kernfs_remove(kn);
  1033. mutex_unlock(&kernfs_mutex);
  1034. if (kn)
  1035. return 0;
  1036. else
  1037. return -ENOENT;
  1038. }
  1039. /**
  1040. * kernfs_rename_ns - move and rename a kernfs_node
  1041. * @kn: target node
  1042. * @new_parent: new parent to put @sd under
  1043. * @new_name: new name
  1044. * @new_ns: new namespace tag
  1045. */
  1046. int kernfs_rename_ns(struct kernfs_node *kn, struct kernfs_node *new_parent,
  1047. const char *new_name, const void *new_ns)
  1048. {
  1049. struct kernfs_node *old_parent;
  1050. const char *old_name = NULL;
  1051. int error;
  1052. /* can't move or rename root */
  1053. if (!kn->parent)
  1054. return -EINVAL;
  1055. mutex_lock(&kernfs_mutex);
  1056. error = -ENOENT;
  1057. if (!kernfs_active(kn) || !kernfs_active(new_parent))
  1058. goto out;
  1059. error = 0;
  1060. if ((kn->parent == new_parent) && (kn->ns == new_ns) &&
  1061. (strcmp(kn->name, new_name) == 0))
  1062. goto out; /* nothing to rename */
  1063. error = -EEXIST;
  1064. if (kernfs_find_ns(new_parent, new_name, new_ns))
  1065. goto out;
  1066. /* rename kernfs_node */
  1067. if (strcmp(kn->name, new_name) != 0) {
  1068. error = -ENOMEM;
  1069. new_name = kstrdup_const(new_name, GFP_KERNEL);
  1070. if (!new_name)
  1071. goto out;
  1072. } else {
  1073. new_name = NULL;
  1074. }
  1075. /*
  1076. * Move to the appropriate place in the appropriate directories rbtree.
  1077. */
  1078. kernfs_unlink_sibling(kn);
  1079. kernfs_get(new_parent);
  1080. /* rename_lock protects ->parent and ->name accessors */
  1081. spin_lock_irq(&kernfs_rename_lock);
  1082. old_parent = kn->parent;
  1083. kn->parent = new_parent;
  1084. kn->ns = new_ns;
  1085. if (new_name) {
  1086. old_name = kn->name;
  1087. kn->name = new_name;
  1088. }
  1089. spin_unlock_irq(&kernfs_rename_lock);
  1090. kn->hash = kernfs_name_hash(kn->name, kn->ns);
  1091. kernfs_link_sibling(kn);
  1092. kernfs_put(old_parent);
  1093. kfree_const(old_name);
  1094. error = 0;
  1095. out:
  1096. mutex_unlock(&kernfs_mutex);
  1097. return error;
  1098. }
  1099. /* Relationship between s_mode and the DT_xxx types */
  1100. static inline unsigned char dt_type(struct kernfs_node *kn)
  1101. {
  1102. return (kn->mode >> 12) & 15;
  1103. }
  1104. static int kernfs_dir_fop_release(struct inode *inode, struct file *filp)
  1105. {
  1106. kernfs_put(filp->private_data);
  1107. return 0;
  1108. }
  1109. static struct kernfs_node *kernfs_dir_pos(const void *ns,
  1110. struct kernfs_node *parent, loff_t hash, struct kernfs_node *pos)
  1111. {
  1112. if (pos) {
  1113. int valid = kernfs_active(pos) &&
  1114. pos->parent == parent && hash == pos->hash;
  1115. kernfs_put(pos);
  1116. if (!valid)
  1117. pos = NULL;
  1118. }
  1119. if (!pos && (hash > 1) && (hash < INT_MAX)) {
  1120. struct rb_node *node = parent->dir.children.rb_node;
  1121. while (node) {
  1122. pos = rb_to_kn(node);
  1123. if (hash < pos->hash)
  1124. node = node->rb_left;
  1125. else if (hash > pos->hash)
  1126. node = node->rb_right;
  1127. else
  1128. break;
  1129. }
  1130. }
  1131. /* Skip over entries which are dying/dead or in the wrong namespace */
  1132. while (pos && (!kernfs_active(pos) || pos->ns != ns)) {
  1133. struct rb_node *node = rb_next(&pos->rb);
  1134. if (!node)
  1135. pos = NULL;
  1136. else
  1137. pos = rb_to_kn(node);
  1138. }
  1139. return pos;
  1140. }
  1141. static struct kernfs_node *kernfs_dir_next_pos(const void *ns,
  1142. struct kernfs_node *parent, ino_t ino, struct kernfs_node *pos)
  1143. {
  1144. pos = kernfs_dir_pos(ns, parent, ino, pos);
  1145. if (pos) {
  1146. do {
  1147. struct rb_node *node = rb_next(&pos->rb);
  1148. if (!node)
  1149. pos = NULL;
  1150. else
  1151. pos = rb_to_kn(node);
  1152. } while (pos && (!kernfs_active(pos) || pos->ns != ns));
  1153. }
  1154. return pos;
  1155. }
  1156. static int kernfs_fop_readdir(struct file *file, struct dir_context *ctx)
  1157. {
  1158. struct dentry *dentry = file->f_path.dentry;
  1159. struct kernfs_node *parent = dentry->d_fsdata;
  1160. struct kernfs_node *pos = file->private_data;
  1161. const void *ns = NULL;
  1162. if (!dir_emit_dots(file, ctx))
  1163. return 0;
  1164. mutex_lock(&kernfs_mutex);
  1165. if (kernfs_ns_enabled(parent))
  1166. ns = kernfs_info(dentry->d_sb)->ns;
  1167. for (pos = kernfs_dir_pos(ns, parent, ctx->pos, pos);
  1168. pos;
  1169. pos = kernfs_dir_next_pos(ns, parent, ctx->pos, pos)) {
  1170. const char *name = pos->name;
  1171. unsigned int type = dt_type(pos);
  1172. int len = strlen(name);
  1173. ino_t ino = pos->ino;
  1174. ctx->pos = pos->hash;
  1175. file->private_data = pos;
  1176. kernfs_get(pos);
  1177. mutex_unlock(&kernfs_mutex);
  1178. if (!dir_emit(ctx, name, len, ino, type))
  1179. return 0;
  1180. mutex_lock(&kernfs_mutex);
  1181. }
  1182. mutex_unlock(&kernfs_mutex);
  1183. file->private_data = NULL;
  1184. ctx->pos = INT_MAX;
  1185. return 0;
  1186. }
  1187. static loff_t kernfs_dir_fop_llseek(struct file *file, loff_t offset,
  1188. int whence)
  1189. {
  1190. struct inode *inode = file_inode(file);
  1191. loff_t ret;
  1192. mutex_lock(&inode->i_mutex);
  1193. ret = generic_file_llseek(file, offset, whence);
  1194. mutex_unlock(&inode->i_mutex);
  1195. return ret;
  1196. }
  1197. const struct file_operations kernfs_dir_fops = {
  1198. .read = generic_read_dir,
  1199. .iterate = kernfs_fop_readdir,
  1200. .release = kernfs_dir_fop_release,
  1201. .llseek = kernfs_dir_fop_llseek,
  1202. };