dir.c 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * fs/kernfs/dir.c - kernfs directory implementation
  4. *
  5. * Copyright (c) 2001-3 Patrick Mochel
  6. * Copyright (c) 2007 SUSE Linux Products GmbH
  7. * Copyright (c) 2007, 2013 Tejun Heo <tj@kernel.org>
  8. */
  9. #include <linux/sched.h>
  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 <linux/ns_common.h>
  17. #include "kernfs-internal.h"
  18. /*
  19. * Don't use rename_lock to piggy back on pr_cont_buf. We don't want to
  20. * call pr_cont() while holding rename_lock. Because sometimes pr_cont()
  21. * will perform wakeups when releasing console_sem. Holding rename_lock
  22. * will introduce deadlock if the scheduler reads the kernfs_name in the
  23. * wakeup path.
  24. */
  25. static DEFINE_SPINLOCK(kernfs_pr_cont_lock);
  26. static char kernfs_pr_cont_buf[PATH_MAX]; /* protected by pr_cont_lock */
  27. #define rb_to_kn(X) rb_entry((X), struct kernfs_node, rb)
  28. static bool __kernfs_active(struct kernfs_node *kn)
  29. {
  30. return atomic_read(&kn->active) >= 0;
  31. }
  32. static bool kernfs_active(struct kernfs_node *kn)
  33. {
  34. lockdep_assert_held(&kernfs_root(kn)->kernfs_rwsem);
  35. return __kernfs_active(kn);
  36. }
  37. static bool kernfs_lockdep(struct kernfs_node *kn)
  38. {
  39. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  40. return kn->flags & KERNFS_LOCKDEP;
  41. #else
  42. return false;
  43. #endif
  44. }
  45. /* kernfs_node_depth - compute depth from @from to @to */
  46. static size_t kernfs_depth(struct kernfs_node *from, struct kernfs_node *to)
  47. {
  48. size_t depth = 0;
  49. while (rcu_dereference(to->__parent) && to != from) {
  50. depth++;
  51. to = rcu_dereference(to->__parent);
  52. }
  53. return depth;
  54. }
  55. static struct kernfs_node *kernfs_common_ancestor(struct kernfs_node *a,
  56. struct kernfs_node *b)
  57. {
  58. size_t da, db;
  59. struct kernfs_root *ra = kernfs_root(a), *rb = kernfs_root(b);
  60. if (ra != rb)
  61. return NULL;
  62. da = kernfs_depth(ra->kn, a);
  63. db = kernfs_depth(rb->kn, b);
  64. while (da > db) {
  65. a = rcu_dereference(a->__parent);
  66. da--;
  67. }
  68. while (db > da) {
  69. b = rcu_dereference(b->__parent);
  70. db--;
  71. }
  72. /* worst case b and a will be the same at root */
  73. while (b != a) {
  74. b = rcu_dereference(b->__parent);
  75. a = rcu_dereference(a->__parent);
  76. }
  77. return a;
  78. }
  79. /**
  80. * kernfs_path_from_node_locked - find a pseudo-absolute path to @kn_to,
  81. * where kn_from is treated as root of the path.
  82. * @kn_from: kernfs node which should be treated as root for the path
  83. * @kn_to: kernfs node to which path is needed
  84. * @buf: buffer to copy the path into
  85. * @buflen: size of @buf
  86. *
  87. * We need to handle couple of scenarios here:
  88. * [1] when @kn_from is an ancestor of @kn_to at some level
  89. * kn_from: /n1/n2/n3
  90. * kn_to: /n1/n2/n3/n4/n5
  91. * result: /n4/n5
  92. *
  93. * [2] when @kn_from is on a different hierarchy and we need to find common
  94. * ancestor between @kn_from and @kn_to.
  95. * kn_from: /n1/n2/n3/n4
  96. * kn_to: /n1/n2/n5
  97. * result: /../../n5
  98. * OR
  99. * kn_from: /n1/n2/n3/n4/n5 [depth=5]
  100. * kn_to: /n1/n2/n3 [depth=3]
  101. * result: /../..
  102. *
  103. * [3] when @kn_to is %NULL result will be "(null)"
  104. *
  105. * Return: the length of the constructed path. If the path would have been
  106. * greater than @buflen, @buf contains the truncated path with the trailing
  107. * '\0'. On error, -errno is returned.
  108. */
  109. static int kernfs_path_from_node_locked(struct kernfs_node *kn_to,
  110. struct kernfs_node *kn_from,
  111. char *buf, size_t buflen)
  112. {
  113. struct kernfs_node *kn, *common;
  114. const char parent_str[] = "/..";
  115. size_t depth_from, depth_to, len = 0;
  116. ssize_t copied;
  117. int i, j;
  118. if (!kn_to)
  119. return strscpy(buf, "(null)", buflen);
  120. if (!kn_from)
  121. kn_from = kernfs_root(kn_to)->kn;
  122. if (kn_from == kn_to)
  123. return strscpy(buf, "/", buflen);
  124. common = kernfs_common_ancestor(kn_from, kn_to);
  125. if (WARN_ON(!common))
  126. return -EINVAL;
  127. depth_to = kernfs_depth(common, kn_to);
  128. depth_from = kernfs_depth(common, kn_from);
  129. buf[0] = '\0';
  130. for (i = 0; i < depth_from; i++) {
  131. copied = strscpy(buf + len, parent_str, buflen - len);
  132. if (copied < 0)
  133. return copied;
  134. len += copied;
  135. }
  136. /* Calculate how many bytes we need for the rest */
  137. for (i = depth_to - 1; i >= 0; i--) {
  138. const char *name;
  139. for (kn = kn_to, j = 0; j < i; j++)
  140. kn = rcu_dereference(kn->__parent);
  141. name = rcu_dereference(kn->name);
  142. len += scnprintf(buf + len, buflen - len, "/%s", name);
  143. }
  144. return len;
  145. }
  146. /**
  147. * kernfs_name - obtain the name of a given node
  148. * @kn: kernfs_node of interest
  149. * @buf: buffer to copy @kn's name into
  150. * @buflen: size of @buf
  151. *
  152. * Copies the name of @kn into @buf of @buflen bytes. The behavior is
  153. * similar to strscpy().
  154. *
  155. * Fills buffer with "(null)" if @kn is %NULL.
  156. *
  157. * Return: the resulting length of @buf. If @buf isn't long enough,
  158. * it's filled up to @buflen-1 and nul terminated, and returns -E2BIG.
  159. *
  160. * This function can be called from any context.
  161. */
  162. int kernfs_name(struct kernfs_node *kn, char *buf, size_t buflen)
  163. {
  164. struct kernfs_node *kn_parent;
  165. if (!kn)
  166. return strscpy(buf, "(null)", buflen);
  167. guard(rcu)();
  168. /*
  169. * KERNFS_ROOT_INVARIANT_PARENT is ignored here. The name is RCU freed and
  170. * the parent is either existing or not.
  171. */
  172. kn_parent = rcu_dereference(kn->__parent);
  173. return strscpy(buf, kn_parent ? rcu_dereference(kn->name) : "/", buflen);
  174. }
  175. /**
  176. * kernfs_path_from_node - build path of node @to relative to @from.
  177. * @from: parent kernfs_node relative to which we need to build the path
  178. * @to: kernfs_node of interest
  179. * @buf: buffer to copy @to's path into
  180. * @buflen: size of @buf
  181. *
  182. * Builds @to's path relative to @from in @buf. @from and @to must
  183. * be on the same kernfs-root. If @from is not parent of @to, then a relative
  184. * path (which includes '..'s) as needed to reach from @from to @to is
  185. * returned.
  186. *
  187. * Return: the length of the constructed path. If the path would have been
  188. * greater than @buflen, @buf contains the truncated path with the trailing
  189. * '\0'. On error, -errno is returned.
  190. */
  191. int kernfs_path_from_node(struct kernfs_node *to, struct kernfs_node *from,
  192. char *buf, size_t buflen)
  193. {
  194. struct kernfs_root *root;
  195. guard(rcu)();
  196. if (to) {
  197. root = kernfs_root(to);
  198. if (!(root->flags & KERNFS_ROOT_INVARIANT_PARENT)) {
  199. guard(read_lock_irqsave)(&root->kernfs_rename_lock);
  200. return kernfs_path_from_node_locked(to, from, buf, buflen);
  201. }
  202. }
  203. return kernfs_path_from_node_locked(to, from, buf, buflen);
  204. }
  205. EXPORT_SYMBOL_GPL(kernfs_path_from_node);
  206. /**
  207. * pr_cont_kernfs_name - pr_cont name of a kernfs_node
  208. * @kn: kernfs_node of interest
  209. *
  210. * This function can be called from any context.
  211. */
  212. void pr_cont_kernfs_name(struct kernfs_node *kn)
  213. {
  214. unsigned long flags;
  215. spin_lock_irqsave(&kernfs_pr_cont_lock, flags);
  216. kernfs_name(kn, kernfs_pr_cont_buf, sizeof(kernfs_pr_cont_buf));
  217. pr_cont("%s", kernfs_pr_cont_buf);
  218. spin_unlock_irqrestore(&kernfs_pr_cont_lock, flags);
  219. }
  220. /**
  221. * pr_cont_kernfs_path - pr_cont path of a kernfs_node
  222. * @kn: kernfs_node of interest
  223. *
  224. * This function can be called from any context.
  225. */
  226. void pr_cont_kernfs_path(struct kernfs_node *kn)
  227. {
  228. unsigned long flags;
  229. int sz;
  230. spin_lock_irqsave(&kernfs_pr_cont_lock, flags);
  231. sz = kernfs_path_from_node(kn, NULL, kernfs_pr_cont_buf,
  232. sizeof(kernfs_pr_cont_buf));
  233. if (sz < 0) {
  234. if (sz == -E2BIG)
  235. pr_cont("(name too long)");
  236. else
  237. pr_cont("(error)");
  238. goto out;
  239. }
  240. pr_cont("%s", kernfs_pr_cont_buf);
  241. out:
  242. spin_unlock_irqrestore(&kernfs_pr_cont_lock, flags);
  243. }
  244. /**
  245. * kernfs_get_parent - determine the parent node and pin it
  246. * @kn: kernfs_node of interest
  247. *
  248. * Determines @kn's parent, pins and returns it. This function can be
  249. * called from any context.
  250. *
  251. * Return: parent node of @kn
  252. */
  253. struct kernfs_node *kernfs_get_parent(struct kernfs_node *kn)
  254. {
  255. struct kernfs_node *parent;
  256. struct kernfs_root *root;
  257. unsigned long flags;
  258. root = kernfs_root(kn);
  259. read_lock_irqsave(&root->kernfs_rename_lock, flags);
  260. parent = kernfs_parent(kn);
  261. kernfs_get(parent);
  262. read_unlock_irqrestore(&root->kernfs_rename_lock, flags);
  263. return parent;
  264. }
  265. /*
  266. * kernfs_ns_id - return the namespace id for a given namespace
  267. * @ns: namespace tag (may be NULL)
  268. *
  269. * Use the 64-bit namespace id instead of raw pointers for hashing
  270. * and comparison to avoid leaking kernel addresses to userspace.
  271. */
  272. static u64 kernfs_ns_id(const struct ns_common *ns)
  273. {
  274. return ns ? ns->ns_id : 0;
  275. }
  276. /**
  277. * kernfs_name_hash - calculate hash of @ns + @name
  278. * @name: Null terminated string to hash
  279. * @ns: Namespace tag to hash
  280. *
  281. * Return: 31-bit hash of ns + name (so it fits in an off_t)
  282. */
  283. static unsigned int kernfs_name_hash(const char *name,
  284. const struct ns_common *ns)
  285. {
  286. unsigned long hash = init_name_hash(kernfs_ns_id(ns));
  287. unsigned int len = strlen(name);
  288. while (len--)
  289. hash = partial_name_hash(*name++, hash);
  290. hash = end_name_hash(hash);
  291. hash &= 0x7fffffffU;
  292. /* Reserve hash numbers 0, 1 and INT_MAX for magic directory entries */
  293. if (hash < 2)
  294. hash += 2;
  295. if (hash >= INT_MAX)
  296. hash = INT_MAX - 1;
  297. return hash;
  298. }
  299. static int kernfs_name_compare(unsigned int hash, const char *name,
  300. const struct ns_common *ns, const struct kernfs_node *kn)
  301. {
  302. u64 ns_id = kernfs_ns_id(ns);
  303. u64 kn_ns_id = kernfs_ns_id(kn->ns);
  304. if (hash < kn->hash)
  305. return -1;
  306. if (hash > kn->hash)
  307. return 1;
  308. if (ns_id < kn_ns_id)
  309. return -1;
  310. if (ns_id > kn_ns_id)
  311. return 1;
  312. return strcmp(name, kernfs_rcu_name(kn));
  313. }
  314. static int kernfs_sd_compare(const struct kernfs_node *left,
  315. const struct kernfs_node *right)
  316. {
  317. return kernfs_name_compare(left->hash, kernfs_rcu_name(left), left->ns, right);
  318. }
  319. /**
  320. * kernfs_link_sibling - link kernfs_node into sibling rbtree
  321. * @kn: kernfs_node of interest
  322. *
  323. * Link @kn into its sibling rbtree which starts from
  324. * @kn->parent->dir.children.
  325. *
  326. * Locking:
  327. * kernfs_rwsem held exclusive
  328. *
  329. * Return:
  330. * %0 on success, -EEXIST on failure.
  331. */
  332. static int kernfs_link_sibling(struct kernfs_node *kn)
  333. {
  334. struct rb_node *parent = NULL;
  335. struct kernfs_node *kn_parent;
  336. struct rb_node **node;
  337. kn_parent = kernfs_parent(kn);
  338. node = &kn_parent->dir.children.rb_node;
  339. while (*node) {
  340. struct kernfs_node *pos;
  341. int result;
  342. pos = rb_to_kn(*node);
  343. parent = *node;
  344. result = kernfs_sd_compare(kn, pos);
  345. if (result < 0)
  346. node = &pos->rb.rb_left;
  347. else if (result > 0)
  348. node = &pos->rb.rb_right;
  349. else
  350. return -EEXIST;
  351. }
  352. /* add new node and rebalance the tree */
  353. rb_link_node(&kn->rb, parent, node);
  354. rb_insert_color(&kn->rb, &kn_parent->dir.children);
  355. /* successfully added, account subdir number */
  356. down_write(&kernfs_root(kn)->kernfs_iattr_rwsem);
  357. if (kernfs_type(kn) == KERNFS_DIR)
  358. kn_parent->dir.subdirs++;
  359. kernfs_inc_rev(kn_parent);
  360. up_write(&kernfs_root(kn)->kernfs_iattr_rwsem);
  361. return 0;
  362. }
  363. /**
  364. * kernfs_unlink_sibling - unlink kernfs_node from sibling rbtree
  365. * @kn: kernfs_node of interest
  366. *
  367. * Try to unlink @kn from its sibling rbtree which starts from
  368. * kn->parent->dir.children.
  369. *
  370. * Return: %true if @kn was actually removed,
  371. * %false if @kn wasn't on the rbtree.
  372. *
  373. * Locking:
  374. * kernfs_rwsem held exclusive
  375. */
  376. static bool kernfs_unlink_sibling(struct kernfs_node *kn)
  377. {
  378. struct kernfs_node *kn_parent;
  379. if (RB_EMPTY_NODE(&kn->rb))
  380. return false;
  381. kn_parent = kernfs_parent(kn);
  382. down_write(&kernfs_root(kn)->kernfs_iattr_rwsem);
  383. if (kernfs_type(kn) == KERNFS_DIR)
  384. kn_parent->dir.subdirs--;
  385. kernfs_inc_rev(kn_parent);
  386. up_write(&kernfs_root(kn)->kernfs_iattr_rwsem);
  387. rb_erase(&kn->rb, &kn_parent->dir.children);
  388. RB_CLEAR_NODE(&kn->rb);
  389. return true;
  390. }
  391. /**
  392. * kernfs_get_active - get an active reference to kernfs_node
  393. * @kn: kernfs_node to get an active reference to
  394. *
  395. * Get an active reference of @kn. This function is noop if @kn
  396. * is %NULL.
  397. *
  398. * Return:
  399. * Pointer to @kn on success, %NULL on failure.
  400. */
  401. struct kernfs_node *kernfs_get_active(struct kernfs_node *kn)
  402. {
  403. if (unlikely(!kn))
  404. return NULL;
  405. if (!atomic_inc_unless_negative(&kn->active))
  406. return NULL;
  407. if (kernfs_lockdep(kn))
  408. rwsem_acquire_read(&kn->dep_map, 0, 1, _RET_IP_);
  409. return kn;
  410. }
  411. /**
  412. * kernfs_put_active - put an active reference to kernfs_node
  413. * @kn: kernfs_node to put an active reference to
  414. *
  415. * Put an active reference to @kn. This function is noop if @kn
  416. * is %NULL.
  417. */
  418. void kernfs_put_active(struct kernfs_node *kn)
  419. {
  420. int v;
  421. if (unlikely(!kn))
  422. return;
  423. if (kernfs_lockdep(kn))
  424. rwsem_release(&kn->dep_map, _RET_IP_);
  425. v = atomic_dec_return(&kn->active);
  426. if (likely(v != KN_DEACTIVATED_BIAS))
  427. return;
  428. wake_up_all(&kernfs_root(kn)->deactivate_waitq);
  429. }
  430. /**
  431. * kernfs_drain - drain kernfs_node
  432. * @kn: kernfs_node to drain
  433. *
  434. * Drain existing usages and nuke all existing mmaps of @kn. Multiple
  435. * removers may invoke this function concurrently on @kn and all will
  436. * return after draining is complete.
  437. */
  438. static void kernfs_drain(struct kernfs_node *kn)
  439. __releases(&kernfs_root(kn)->kernfs_rwsem)
  440. __acquires(&kernfs_root(kn)->kernfs_rwsem)
  441. {
  442. struct kernfs_root *root = kernfs_root(kn);
  443. lockdep_assert_held_write(&root->kernfs_rwsem);
  444. WARN_ON_ONCE(kernfs_active(kn));
  445. /*
  446. * Skip draining if already fully drained. This avoids draining and its
  447. * lockdep annotations for nodes which have never been activated
  448. * allowing embedding kernfs_remove() in create error paths without
  449. * worrying about draining.
  450. */
  451. if (atomic_read(&kn->active) == KN_DEACTIVATED_BIAS &&
  452. !kernfs_should_drain_open_files(kn))
  453. return;
  454. up_write(&root->kernfs_rwsem);
  455. if (kernfs_lockdep(kn)) {
  456. rwsem_acquire(&kn->dep_map, 0, 0, _RET_IP_);
  457. if (atomic_read(&kn->active) != KN_DEACTIVATED_BIAS)
  458. lock_contended(&kn->dep_map, _RET_IP_);
  459. }
  460. wait_event(root->deactivate_waitq,
  461. atomic_read(&kn->active) == KN_DEACTIVATED_BIAS);
  462. if (kernfs_lockdep(kn)) {
  463. lock_acquired(&kn->dep_map, _RET_IP_);
  464. rwsem_release(&kn->dep_map, _RET_IP_);
  465. }
  466. if (kernfs_should_drain_open_files(kn))
  467. kernfs_drain_open_files(kn);
  468. down_write(&root->kernfs_rwsem);
  469. }
  470. /**
  471. * kernfs_get - get a reference count on a kernfs_node
  472. * @kn: the target kernfs_node
  473. */
  474. void kernfs_get(struct kernfs_node *kn)
  475. {
  476. if (kn) {
  477. WARN_ON(!atomic_read(&kn->count));
  478. atomic_inc(&kn->count);
  479. }
  480. }
  481. EXPORT_SYMBOL_GPL(kernfs_get);
  482. static void kernfs_free_rcu(struct rcu_head *rcu)
  483. {
  484. struct kernfs_node *kn = container_of(rcu, struct kernfs_node, rcu);
  485. /* If the whole node goes away, then name can't be used outside */
  486. kfree_const(rcu_access_pointer(kn->name));
  487. if (kn->iattr) {
  488. simple_xattrs_free(&kn->iattr->xattrs, NULL);
  489. kmem_cache_free(kernfs_iattrs_cache, kn->iattr);
  490. }
  491. kmem_cache_free(kernfs_node_cache, kn);
  492. }
  493. /**
  494. * kernfs_put - put a reference count on a kernfs_node
  495. * @kn: the target kernfs_node
  496. *
  497. * Put a reference count of @kn and destroy it if it reached zero.
  498. */
  499. void kernfs_put(struct kernfs_node *kn)
  500. {
  501. struct kernfs_node *parent;
  502. struct kernfs_root *root;
  503. if (!kn || !atomic_dec_and_test(&kn->count))
  504. return;
  505. root = kernfs_root(kn);
  506. repeat:
  507. /*
  508. * Moving/renaming is always done while holding reference.
  509. * kn->parent won't change beneath us.
  510. */
  511. parent = kernfs_parent(kn);
  512. WARN_ONCE(atomic_read(&kn->active) != KN_DEACTIVATED_BIAS,
  513. "kernfs_put: %s/%s: released with incorrect active_ref %d\n",
  514. parent ? rcu_dereference(parent->name) : "",
  515. rcu_dereference(kn->name), atomic_read(&kn->active));
  516. if (kernfs_type(kn) == KERNFS_LINK)
  517. kernfs_put(kn->symlink.target_kn);
  518. spin_lock(&root->kernfs_idr_lock);
  519. idr_remove(&root->ino_idr, (u32)kernfs_ino(kn));
  520. spin_unlock(&root->kernfs_idr_lock);
  521. call_rcu(&kn->rcu, kernfs_free_rcu);
  522. kn = parent;
  523. if (kn) {
  524. if (atomic_dec_and_test(&kn->count))
  525. goto repeat;
  526. } else {
  527. /* just released the root kn, free @root too */
  528. idr_destroy(&root->ino_idr);
  529. kfree_rcu(root, rcu);
  530. }
  531. }
  532. EXPORT_SYMBOL_GPL(kernfs_put);
  533. /**
  534. * kernfs_node_from_dentry - determine kernfs_node associated with a dentry
  535. * @dentry: the dentry in question
  536. *
  537. * Return: the kernfs_node associated with @dentry. If @dentry is not a
  538. * kernfs one, %NULL is returned.
  539. *
  540. * While the returned kernfs_node will stay accessible as long as @dentry
  541. * is accessible, the returned node can be in any state and the caller is
  542. * fully responsible for determining what's accessible.
  543. */
  544. struct kernfs_node *kernfs_node_from_dentry(struct dentry *dentry)
  545. {
  546. if (dentry->d_sb->s_op == &kernfs_sops)
  547. return kernfs_dentry_node(dentry);
  548. return NULL;
  549. }
  550. static struct kernfs_node *__kernfs_new_node(struct kernfs_root *root,
  551. struct kernfs_node *parent,
  552. const char *name, umode_t mode,
  553. kuid_t uid, kgid_t gid,
  554. unsigned flags)
  555. {
  556. struct kernfs_node *kn;
  557. u32 id_highbits;
  558. int ret;
  559. name = kstrdup_const(name, GFP_KERNEL);
  560. if (!name)
  561. return NULL;
  562. kn = kmem_cache_zalloc(kernfs_node_cache, GFP_KERNEL);
  563. if (!kn)
  564. goto err_out1;
  565. idr_preload(GFP_KERNEL);
  566. spin_lock(&root->kernfs_idr_lock);
  567. ret = idr_alloc_cyclic(&root->ino_idr, kn, 1, 0, GFP_ATOMIC);
  568. if (ret >= 0 && ret < root->last_id_lowbits)
  569. root->id_highbits++;
  570. id_highbits = root->id_highbits;
  571. root->last_id_lowbits = ret;
  572. spin_unlock(&root->kernfs_idr_lock);
  573. idr_preload_end();
  574. if (ret < 0)
  575. goto err_out2;
  576. kn->id = (u64)id_highbits << 32 | ret;
  577. atomic_set(&kn->count, 1);
  578. atomic_set(&kn->active, KN_DEACTIVATED_BIAS);
  579. RB_CLEAR_NODE(&kn->rb);
  580. rcu_assign_pointer(kn->name, name);
  581. kn->mode = mode;
  582. kn->flags = flags;
  583. if (!uid_eq(uid, GLOBAL_ROOT_UID) || !gid_eq(gid, GLOBAL_ROOT_GID)) {
  584. struct iattr iattr = {
  585. .ia_valid = ATTR_UID | ATTR_GID,
  586. .ia_uid = uid,
  587. .ia_gid = gid,
  588. };
  589. ret = __kernfs_setattr(kn, &iattr);
  590. if (ret < 0)
  591. goto err_out3;
  592. }
  593. if (parent) {
  594. ret = security_kernfs_init_security(parent, kn);
  595. if (ret)
  596. goto err_out4;
  597. }
  598. return kn;
  599. err_out4:
  600. if (kn->iattr) {
  601. simple_xattrs_free(&kn->iattr->xattrs, NULL);
  602. kmem_cache_free(kernfs_iattrs_cache, kn->iattr);
  603. }
  604. err_out3:
  605. spin_lock(&root->kernfs_idr_lock);
  606. idr_remove(&root->ino_idr, (u32)kernfs_ino(kn));
  607. spin_unlock(&root->kernfs_idr_lock);
  608. err_out2:
  609. kmem_cache_free(kernfs_node_cache, kn);
  610. err_out1:
  611. kfree_const(name);
  612. return NULL;
  613. }
  614. struct kernfs_node *kernfs_new_node(struct kernfs_node *parent,
  615. const char *name, umode_t mode,
  616. kuid_t uid, kgid_t gid,
  617. unsigned flags)
  618. {
  619. struct kernfs_node *kn;
  620. if (parent->mode & S_ISGID) {
  621. /* this code block imitates inode_init_owner() for
  622. * kernfs
  623. */
  624. if (parent->iattr)
  625. gid = parent->iattr->ia_gid;
  626. if (flags & KERNFS_DIR)
  627. mode |= S_ISGID;
  628. }
  629. kn = __kernfs_new_node(kernfs_root(parent), parent,
  630. name, mode, uid, gid, flags);
  631. if (kn) {
  632. kernfs_get(parent);
  633. rcu_assign_pointer(kn->__parent, parent);
  634. }
  635. return kn;
  636. }
  637. /*
  638. * kernfs_find_and_get_node_by_id - get kernfs_node from node id
  639. * @root: the kernfs root
  640. * @id: the target node id
  641. *
  642. * @id's lower 32bits encode ino and upper gen. If the gen portion is
  643. * zero, all generations are matched.
  644. *
  645. * Return: %NULL on failure,
  646. * otherwise a kernfs node with reference counter incremented.
  647. */
  648. struct kernfs_node *kernfs_find_and_get_node_by_id(struct kernfs_root *root,
  649. u64 id)
  650. {
  651. struct kernfs_node *kn;
  652. ino_t ino = kernfs_id_ino(id);
  653. u32 gen = kernfs_id_gen(id);
  654. rcu_read_lock();
  655. kn = idr_find(&root->ino_idr, (u32)ino);
  656. if (!kn)
  657. goto err_unlock;
  658. if (sizeof(ino_t) >= sizeof(u64)) {
  659. /* we looked up with the low 32bits, compare the whole */
  660. if (kernfs_ino(kn) != ino)
  661. goto err_unlock;
  662. } else {
  663. /* 0 matches all generations */
  664. if (unlikely(gen && kernfs_gen(kn) != gen))
  665. goto err_unlock;
  666. }
  667. /*
  668. * We should fail if @kn has never been activated and guarantee success
  669. * if the caller knows that @kn is active. Both can be achieved by
  670. * __kernfs_active() which tests @kn->active without kernfs_rwsem.
  671. */
  672. if (unlikely(!__kernfs_active(kn) || !atomic_inc_not_zero(&kn->count)))
  673. goto err_unlock;
  674. rcu_read_unlock();
  675. return kn;
  676. err_unlock:
  677. rcu_read_unlock();
  678. return NULL;
  679. }
  680. /**
  681. * kernfs_add_one - add kernfs_node to parent without warning
  682. * @kn: kernfs_node to be added
  683. *
  684. * The caller must already have initialized @kn->parent. This
  685. * function increments nlink of the parent's inode if @kn is a
  686. * directory and link into the children list of the parent.
  687. *
  688. * Return:
  689. * %0 on success, -EEXIST if entry with the given name already
  690. * exists.
  691. */
  692. int kernfs_add_one(struct kernfs_node *kn)
  693. {
  694. struct kernfs_root *root = kernfs_root(kn);
  695. struct kernfs_iattrs *ps_iattr;
  696. struct kernfs_node *parent;
  697. bool has_ns;
  698. int ret;
  699. down_write(&root->kernfs_rwsem);
  700. parent = kernfs_parent(kn);
  701. ret = -EINVAL;
  702. has_ns = kernfs_ns_enabled(parent);
  703. if (WARN(has_ns != (bool)kn->ns, KERN_WARNING "kernfs: ns %s in '%s' for '%s'\n",
  704. has_ns ? "required" : "invalid",
  705. kernfs_rcu_name(parent), kernfs_rcu_name(kn)))
  706. goto out_unlock;
  707. if (kernfs_type(parent) != KERNFS_DIR)
  708. goto out_unlock;
  709. ret = -ENOENT;
  710. if (parent->flags & (KERNFS_REMOVING | KERNFS_EMPTY_DIR))
  711. goto out_unlock;
  712. kn->hash = kernfs_name_hash(kernfs_rcu_name(kn), kn->ns);
  713. ret = kernfs_link_sibling(kn);
  714. if (ret)
  715. goto out_unlock;
  716. /* Update timestamps on the parent */
  717. down_write(&root->kernfs_iattr_rwsem);
  718. ps_iattr = parent->iattr;
  719. if (ps_iattr) {
  720. ktime_get_real_ts64(&ps_iattr->ia_ctime);
  721. ps_iattr->ia_mtime = ps_iattr->ia_ctime;
  722. }
  723. up_write(&root->kernfs_iattr_rwsem);
  724. up_write(&root->kernfs_rwsem);
  725. /*
  726. * Activate the new node unless CREATE_DEACTIVATED is requested.
  727. * If not activated here, the kernfs user is responsible for
  728. * activating the node with kernfs_activate(). A node which hasn't
  729. * been activated is not visible to userland and its removal won't
  730. * trigger deactivation.
  731. */
  732. if (!(kernfs_root(kn)->flags & KERNFS_ROOT_CREATE_DEACTIVATED))
  733. kernfs_activate(kn);
  734. return 0;
  735. out_unlock:
  736. up_write(&root->kernfs_rwsem);
  737. return ret;
  738. }
  739. /**
  740. * kernfs_find_ns - find kernfs_node with the given name
  741. * @parent: kernfs_node to search under
  742. * @name: name to look for
  743. * @ns: the namespace tag to use
  744. *
  745. * Look for kernfs_node with name @name under @parent.
  746. *
  747. * Return: pointer to the found kernfs_node on success, %NULL on failure.
  748. */
  749. static struct kernfs_node *kernfs_find_ns(struct kernfs_node *parent,
  750. const unsigned char *name,
  751. const struct ns_common *ns)
  752. {
  753. struct rb_node *node = parent->dir.children.rb_node;
  754. bool has_ns = kernfs_ns_enabled(parent);
  755. unsigned int hash;
  756. lockdep_assert_held(&kernfs_root(parent)->kernfs_rwsem);
  757. if (has_ns != (bool)ns) {
  758. WARN(1, KERN_WARNING "kernfs: ns %s in '%s' for '%s'\n",
  759. has_ns ? "required" : "invalid", kernfs_rcu_name(parent), name);
  760. return NULL;
  761. }
  762. hash = kernfs_name_hash(name, ns);
  763. while (node) {
  764. struct kernfs_node *kn;
  765. int result;
  766. kn = rb_to_kn(node);
  767. result = kernfs_name_compare(hash, name, ns, kn);
  768. if (result < 0)
  769. node = node->rb_left;
  770. else if (result > 0)
  771. node = node->rb_right;
  772. else
  773. return kn;
  774. }
  775. return NULL;
  776. }
  777. static struct kernfs_node *kernfs_walk_ns(struct kernfs_node *parent,
  778. const unsigned char *path,
  779. const struct ns_common *ns)
  780. {
  781. ssize_t len;
  782. char *p, *name;
  783. lockdep_assert_held_read(&kernfs_root(parent)->kernfs_rwsem);
  784. spin_lock_irq(&kernfs_pr_cont_lock);
  785. len = strscpy(kernfs_pr_cont_buf, path, sizeof(kernfs_pr_cont_buf));
  786. if (len < 0) {
  787. spin_unlock_irq(&kernfs_pr_cont_lock);
  788. return NULL;
  789. }
  790. p = kernfs_pr_cont_buf;
  791. while ((name = strsep(&p, "/")) && parent) {
  792. if (*name == '\0')
  793. continue;
  794. parent = kernfs_find_ns(parent, name, ns);
  795. }
  796. spin_unlock_irq(&kernfs_pr_cont_lock);
  797. return parent;
  798. }
  799. /**
  800. * kernfs_find_and_get_ns - find and get kernfs_node with the given name
  801. * @parent: kernfs_node to search under
  802. * @name: name to look for
  803. * @ns: the namespace tag to use
  804. *
  805. * Look for kernfs_node with name @name under @parent and get a reference
  806. * if found. This function may sleep.
  807. *
  808. * Return: pointer to the found kernfs_node on success, %NULL on failure.
  809. */
  810. struct kernfs_node *kernfs_find_and_get_ns(struct kernfs_node *parent,
  811. const char *name,
  812. const struct ns_common *ns)
  813. {
  814. struct kernfs_node *kn;
  815. struct kernfs_root *root = kernfs_root(parent);
  816. down_read(&root->kernfs_rwsem);
  817. kn = kernfs_find_ns(parent, name, ns);
  818. kernfs_get(kn);
  819. up_read(&root->kernfs_rwsem);
  820. return kn;
  821. }
  822. EXPORT_SYMBOL_GPL(kernfs_find_and_get_ns);
  823. /**
  824. * kernfs_walk_and_get_ns - find and get kernfs_node with the given path
  825. * @parent: kernfs_node to search under
  826. * @path: path to look for
  827. * @ns: the namespace tag to use
  828. *
  829. * Look for kernfs_node with path @path under @parent and get a reference
  830. * if found. This function may sleep.
  831. *
  832. * Return: pointer to the found kernfs_node on success, %NULL on failure.
  833. */
  834. struct kernfs_node *kernfs_walk_and_get_ns(struct kernfs_node *parent,
  835. const char *path,
  836. const struct ns_common *ns)
  837. {
  838. struct kernfs_node *kn;
  839. struct kernfs_root *root = kernfs_root(parent);
  840. down_read(&root->kernfs_rwsem);
  841. kn = kernfs_walk_ns(parent, path, ns);
  842. kernfs_get(kn);
  843. up_read(&root->kernfs_rwsem);
  844. return kn;
  845. }
  846. unsigned int kernfs_root_flags(struct kernfs_node *kn)
  847. {
  848. return kernfs_root(kn)->flags;
  849. }
  850. /**
  851. * kernfs_create_root - create a new kernfs hierarchy
  852. * @scops: optional syscall operations for the hierarchy
  853. * @flags: KERNFS_ROOT_* flags
  854. * @priv: opaque data associated with the new directory
  855. *
  856. * Return: the root of the new hierarchy on success, ERR_PTR() value on
  857. * failure.
  858. */
  859. struct kernfs_root *kernfs_create_root(struct kernfs_syscall_ops *scops,
  860. unsigned int flags, void *priv)
  861. {
  862. struct kernfs_root *root;
  863. struct kernfs_node *kn;
  864. root = kzalloc_obj(*root);
  865. if (!root)
  866. return ERR_PTR(-ENOMEM);
  867. idr_init(&root->ino_idr);
  868. spin_lock_init(&root->kernfs_idr_lock);
  869. init_rwsem(&root->kernfs_rwsem);
  870. init_rwsem(&root->kernfs_iattr_rwsem);
  871. init_rwsem(&root->kernfs_supers_rwsem);
  872. INIT_LIST_HEAD(&root->supers);
  873. rwlock_init(&root->kernfs_rename_lock);
  874. /*
  875. * On 64bit ino setups, id is ino. On 32bit, low 32bits are ino.
  876. * High bits generation. The starting value for both ino and
  877. * genenration is 1. Initialize upper 32bit allocation
  878. * accordingly.
  879. */
  880. if (sizeof(ino_t) >= sizeof(u64))
  881. root->id_highbits = 0;
  882. else
  883. root->id_highbits = 1;
  884. kn = __kernfs_new_node(root, NULL, "", S_IFDIR | S_IRUGO | S_IXUGO,
  885. GLOBAL_ROOT_UID, GLOBAL_ROOT_GID,
  886. KERNFS_DIR);
  887. if (!kn) {
  888. idr_destroy(&root->ino_idr);
  889. kfree(root);
  890. return ERR_PTR(-ENOMEM);
  891. }
  892. kn->priv = priv;
  893. kn->dir.root = root;
  894. root->syscall_ops = scops;
  895. root->flags = flags;
  896. root->kn = kn;
  897. init_waitqueue_head(&root->deactivate_waitq);
  898. if (!(root->flags & KERNFS_ROOT_CREATE_DEACTIVATED))
  899. kernfs_activate(kn);
  900. return root;
  901. }
  902. /**
  903. * kernfs_destroy_root - destroy a kernfs hierarchy
  904. * @root: root of the hierarchy to destroy
  905. *
  906. * Destroy the hierarchy anchored at @root by removing all existing
  907. * directories and destroying @root.
  908. */
  909. void kernfs_destroy_root(struct kernfs_root *root)
  910. {
  911. /*
  912. * kernfs_remove holds kernfs_rwsem from the root so the root
  913. * shouldn't be freed during the operation.
  914. */
  915. kernfs_get(root->kn);
  916. kernfs_remove(root->kn);
  917. kernfs_put(root->kn); /* will also free @root */
  918. }
  919. /**
  920. * kernfs_root_to_node - return the kernfs_node associated with a kernfs_root
  921. * @root: root to use to lookup
  922. *
  923. * Return: @root's kernfs_node
  924. */
  925. struct kernfs_node *kernfs_root_to_node(struct kernfs_root *root)
  926. {
  927. return root->kn;
  928. }
  929. /**
  930. * kernfs_create_dir_ns - create a directory
  931. * @parent: parent in which to create a new directory
  932. * @name: name of the new directory
  933. * @mode: mode of the new directory
  934. * @uid: uid of the new directory
  935. * @gid: gid of the new directory
  936. * @priv: opaque data associated with the new directory
  937. * @ns: optional namespace tag of the directory
  938. *
  939. * Return: the created node on success, ERR_PTR() value on failure.
  940. */
  941. struct kernfs_node *kernfs_create_dir_ns(struct kernfs_node *parent,
  942. const char *name, umode_t mode,
  943. kuid_t uid, kgid_t gid,
  944. void *priv,
  945. const struct ns_common *ns)
  946. {
  947. struct kernfs_node *kn;
  948. int rc;
  949. /* allocate */
  950. kn = kernfs_new_node(parent, name, mode | S_IFDIR,
  951. uid, gid, KERNFS_DIR);
  952. if (!kn)
  953. return ERR_PTR(-ENOMEM);
  954. kn->dir.root = parent->dir.root;
  955. kn->ns = ns;
  956. kn->priv = priv;
  957. /* link in */
  958. rc = kernfs_add_one(kn);
  959. if (!rc)
  960. return kn;
  961. kernfs_put(kn);
  962. return ERR_PTR(rc);
  963. }
  964. /**
  965. * kernfs_create_empty_dir - create an always empty directory
  966. * @parent: parent in which to create a new directory
  967. * @name: name of the new directory
  968. *
  969. * Return: the created node on success, ERR_PTR() value on failure.
  970. */
  971. struct kernfs_node *kernfs_create_empty_dir(struct kernfs_node *parent,
  972. const char *name)
  973. {
  974. struct kernfs_node *kn;
  975. int rc;
  976. /* allocate */
  977. kn = kernfs_new_node(parent, name, S_IRUGO|S_IXUGO|S_IFDIR,
  978. GLOBAL_ROOT_UID, GLOBAL_ROOT_GID, KERNFS_DIR);
  979. if (!kn)
  980. return ERR_PTR(-ENOMEM);
  981. kn->flags |= KERNFS_EMPTY_DIR;
  982. kn->dir.root = parent->dir.root;
  983. kn->ns = NULL;
  984. kn->priv = NULL;
  985. /* link in */
  986. rc = kernfs_add_one(kn);
  987. if (!rc)
  988. return kn;
  989. kernfs_put(kn);
  990. return ERR_PTR(rc);
  991. }
  992. static int kernfs_dop_revalidate(struct inode *dir, const struct qstr *name,
  993. struct dentry *dentry, unsigned int flags)
  994. {
  995. struct kernfs_node *kn, *parent;
  996. struct kernfs_root *root;
  997. if (flags & LOOKUP_RCU)
  998. return -ECHILD;
  999. /* Negative hashed dentry? */
  1000. if (d_really_is_negative(dentry)) {
  1001. /* If the kernfs parent node has changed discard and
  1002. * proceed to ->lookup.
  1003. *
  1004. * There's nothing special needed here when getting the
  1005. * dentry parent, even if a concurrent rename is in
  1006. * progress. That's because the dentry is negative so
  1007. * it can only be the target of the rename and it will
  1008. * be doing a d_move() not a replace. Consequently the
  1009. * dentry d_parent won't change over the d_move().
  1010. *
  1011. * Also kernfs negative dentries transitioning from
  1012. * negative to positive during revalidate won't happen
  1013. * because they are invalidated on containing directory
  1014. * changes and the lookup re-done so that a new positive
  1015. * dentry can be properly created.
  1016. */
  1017. root = kernfs_root_from_sb(dentry->d_sb);
  1018. down_read(&root->kernfs_rwsem);
  1019. parent = kernfs_dentry_node(dentry->d_parent);
  1020. if (parent) {
  1021. if (kernfs_dir_changed(parent, dentry)) {
  1022. up_read(&root->kernfs_rwsem);
  1023. return 0;
  1024. }
  1025. }
  1026. up_read(&root->kernfs_rwsem);
  1027. /* The kernfs parent node hasn't changed, leave the
  1028. * dentry negative and return success.
  1029. */
  1030. return 1;
  1031. }
  1032. kn = kernfs_dentry_node(dentry);
  1033. root = kernfs_root(kn);
  1034. down_read(&root->kernfs_rwsem);
  1035. /* The kernfs node has been deactivated */
  1036. if (!kernfs_active(kn))
  1037. goto out_bad;
  1038. parent = kernfs_parent(kn);
  1039. /* The kernfs node has been moved? */
  1040. if (kernfs_dentry_node(dentry->d_parent) != parent)
  1041. goto out_bad;
  1042. /* The kernfs node has been renamed */
  1043. if (strcmp(dentry->d_name.name, kernfs_rcu_name(kn)) != 0)
  1044. goto out_bad;
  1045. /* The kernfs node has been moved to a different namespace */
  1046. if (parent && kernfs_ns_enabled(parent) &&
  1047. kernfs_ns_id(kernfs_info(dentry->d_sb)->ns) != kernfs_ns_id(kn->ns))
  1048. goto out_bad;
  1049. up_read(&root->kernfs_rwsem);
  1050. return 1;
  1051. out_bad:
  1052. up_read(&root->kernfs_rwsem);
  1053. return 0;
  1054. }
  1055. const struct dentry_operations kernfs_dops = {
  1056. .d_revalidate = kernfs_dop_revalidate,
  1057. };
  1058. static struct dentry *kernfs_iop_lookup(struct inode *dir,
  1059. struct dentry *dentry,
  1060. unsigned int flags)
  1061. {
  1062. struct kernfs_node *parent = dir->i_private;
  1063. struct kernfs_node *kn;
  1064. struct kernfs_root *root;
  1065. struct inode *inode = NULL;
  1066. const struct ns_common *ns = NULL;
  1067. root = kernfs_root(parent);
  1068. down_read(&root->kernfs_rwsem);
  1069. if (kernfs_ns_enabled(parent))
  1070. ns = kernfs_info(dir->i_sb)->ns;
  1071. kn = kernfs_find_ns(parent, dentry->d_name.name, ns);
  1072. /* attach dentry and inode */
  1073. if (kn) {
  1074. /* Inactive nodes are invisible to the VFS so don't
  1075. * create a negative.
  1076. */
  1077. if (!kernfs_active(kn)) {
  1078. up_read(&root->kernfs_rwsem);
  1079. return NULL;
  1080. }
  1081. inode = kernfs_get_inode(dir->i_sb, kn);
  1082. if (!inode)
  1083. inode = ERR_PTR(-ENOMEM);
  1084. }
  1085. /*
  1086. * Needed for negative dentry validation.
  1087. * The negative dentry can be created in kernfs_iop_lookup()
  1088. * or transforms from positive dentry in dentry_unlink_inode()
  1089. * called from vfs_rmdir().
  1090. */
  1091. if (!IS_ERR(inode))
  1092. kernfs_set_rev(parent, dentry);
  1093. up_read(&root->kernfs_rwsem);
  1094. /* instantiate and hash (possibly negative) dentry */
  1095. return d_splice_alias(inode, dentry);
  1096. }
  1097. static struct dentry *kernfs_iop_mkdir(struct mnt_idmap *idmap,
  1098. struct inode *dir, struct dentry *dentry,
  1099. umode_t mode)
  1100. {
  1101. struct kernfs_node *parent = dir->i_private;
  1102. struct kernfs_syscall_ops *scops = kernfs_root(parent)->syscall_ops;
  1103. int ret;
  1104. if (!scops || !scops->mkdir)
  1105. return ERR_PTR(-EPERM);
  1106. if (!kernfs_get_active(parent))
  1107. return ERR_PTR(-ENODEV);
  1108. ret = scops->mkdir(parent, dentry->d_name.name, mode);
  1109. kernfs_put_active(parent);
  1110. return ERR_PTR(ret);
  1111. }
  1112. static int kernfs_iop_rmdir(struct inode *dir, struct dentry *dentry)
  1113. {
  1114. struct kernfs_node *kn = kernfs_dentry_node(dentry);
  1115. struct kernfs_syscall_ops *scops = kernfs_root(kn)->syscall_ops;
  1116. int ret;
  1117. if (!scops || !scops->rmdir)
  1118. return -EPERM;
  1119. if (!kernfs_get_active(kn))
  1120. return -ENODEV;
  1121. ret = scops->rmdir(kn);
  1122. kernfs_put_active(kn);
  1123. return ret;
  1124. }
  1125. static int kernfs_iop_rename(struct mnt_idmap *idmap,
  1126. struct inode *old_dir, struct dentry *old_dentry,
  1127. struct inode *new_dir, struct dentry *new_dentry,
  1128. unsigned int flags)
  1129. {
  1130. struct kernfs_node *kn = kernfs_dentry_node(old_dentry);
  1131. struct kernfs_node *new_parent = new_dir->i_private;
  1132. struct kernfs_syscall_ops *scops = kernfs_root(kn)->syscall_ops;
  1133. int ret;
  1134. if (flags)
  1135. return -EINVAL;
  1136. if (!scops || !scops->rename)
  1137. return -EPERM;
  1138. if (!kernfs_get_active(kn))
  1139. return -ENODEV;
  1140. if (!kernfs_get_active(new_parent)) {
  1141. kernfs_put_active(kn);
  1142. return -ENODEV;
  1143. }
  1144. ret = scops->rename(kn, new_parent, new_dentry->d_name.name);
  1145. kernfs_put_active(new_parent);
  1146. kernfs_put_active(kn);
  1147. return ret;
  1148. }
  1149. const struct inode_operations kernfs_dir_iops = {
  1150. .lookup = kernfs_iop_lookup,
  1151. .permission = kernfs_iop_permission,
  1152. .setattr = kernfs_iop_setattr,
  1153. .getattr = kernfs_iop_getattr,
  1154. .listxattr = kernfs_iop_listxattr,
  1155. .mkdir = kernfs_iop_mkdir,
  1156. .rmdir = kernfs_iop_rmdir,
  1157. .rename = kernfs_iop_rename,
  1158. };
  1159. static struct kernfs_node *kernfs_leftmost_descendant(struct kernfs_node *pos)
  1160. {
  1161. struct kernfs_node *last;
  1162. while (true) {
  1163. struct rb_node *rbn;
  1164. last = pos;
  1165. if (kernfs_type(pos) != KERNFS_DIR)
  1166. break;
  1167. rbn = rb_first(&pos->dir.children);
  1168. if (!rbn)
  1169. break;
  1170. pos = rb_to_kn(rbn);
  1171. }
  1172. return last;
  1173. }
  1174. /**
  1175. * kernfs_next_descendant_post - find the next descendant for post-order walk
  1176. * @pos: the current position (%NULL to initiate traversal)
  1177. * @root: kernfs_node whose descendants to walk
  1178. *
  1179. * Find the next descendant to visit for post-order traversal of @root's
  1180. * descendants. @root is included in the iteration and the last node to be
  1181. * visited.
  1182. *
  1183. * Return: the next descendant to visit or %NULL when done.
  1184. */
  1185. static struct kernfs_node *kernfs_next_descendant_post(struct kernfs_node *pos,
  1186. struct kernfs_node *root)
  1187. {
  1188. struct rb_node *rbn;
  1189. lockdep_assert_held_write(&kernfs_root(root)->kernfs_rwsem);
  1190. /* if first iteration, visit leftmost descendant which may be root */
  1191. if (!pos)
  1192. return kernfs_leftmost_descendant(root);
  1193. /* if we visited @root, we're done */
  1194. if (pos == root)
  1195. return NULL;
  1196. /* if there's an unvisited sibling, visit its leftmost descendant */
  1197. rbn = rb_next(&pos->rb);
  1198. if (rbn)
  1199. return kernfs_leftmost_descendant(rb_to_kn(rbn));
  1200. /* no sibling left, visit parent */
  1201. return kernfs_parent(pos);
  1202. }
  1203. static void kernfs_activate_one(struct kernfs_node *kn)
  1204. {
  1205. lockdep_assert_held_write(&kernfs_root(kn)->kernfs_rwsem);
  1206. kn->flags |= KERNFS_ACTIVATED;
  1207. if (kernfs_active(kn) || (kn->flags & (KERNFS_HIDDEN | KERNFS_REMOVING)))
  1208. return;
  1209. WARN_ON_ONCE(rcu_access_pointer(kn->__parent) && RB_EMPTY_NODE(&kn->rb));
  1210. WARN_ON_ONCE(atomic_read(&kn->active) != KN_DEACTIVATED_BIAS);
  1211. atomic_sub(KN_DEACTIVATED_BIAS, &kn->active);
  1212. }
  1213. /**
  1214. * kernfs_activate - activate a node which started deactivated
  1215. * @kn: kernfs_node whose subtree is to be activated
  1216. *
  1217. * If the root has KERNFS_ROOT_CREATE_DEACTIVATED set, a newly created node
  1218. * needs to be explicitly activated. A node which hasn't been activated
  1219. * isn't visible to userland and deactivation is skipped during its
  1220. * removal. This is useful to construct atomic init sequences where
  1221. * creation of multiple nodes should either succeed or fail atomically.
  1222. *
  1223. * The caller is responsible for ensuring that this function is not called
  1224. * after kernfs_remove*() is invoked on @kn.
  1225. */
  1226. void kernfs_activate(struct kernfs_node *kn)
  1227. {
  1228. struct kernfs_node *pos;
  1229. struct kernfs_root *root = kernfs_root(kn);
  1230. down_write(&root->kernfs_rwsem);
  1231. pos = NULL;
  1232. while ((pos = kernfs_next_descendant_post(pos, kn)))
  1233. kernfs_activate_one(pos);
  1234. up_write(&root->kernfs_rwsem);
  1235. }
  1236. /**
  1237. * kernfs_show - show or hide a node
  1238. * @kn: kernfs_node to show or hide
  1239. * @show: whether to show or hide
  1240. *
  1241. * If @show is %false, @kn is marked hidden and deactivated. A hidden node is
  1242. * ignored in future activaitons. If %true, the mark is removed and activation
  1243. * state is restored. This function won't implicitly activate a new node in a
  1244. * %KERNFS_ROOT_CREATE_DEACTIVATED root which hasn't been activated yet.
  1245. *
  1246. * To avoid recursion complexities, directories aren't supported for now.
  1247. */
  1248. void kernfs_show(struct kernfs_node *kn, bool show)
  1249. {
  1250. struct kernfs_root *root = kernfs_root(kn);
  1251. if (WARN_ON_ONCE(kernfs_type(kn) == KERNFS_DIR))
  1252. return;
  1253. down_write(&root->kernfs_rwsem);
  1254. if (show) {
  1255. kn->flags &= ~KERNFS_HIDDEN;
  1256. if (kn->flags & KERNFS_ACTIVATED)
  1257. kernfs_activate_one(kn);
  1258. } else {
  1259. kn->flags |= KERNFS_HIDDEN;
  1260. if (kernfs_active(kn))
  1261. atomic_add(KN_DEACTIVATED_BIAS, &kn->active);
  1262. kernfs_drain(kn);
  1263. }
  1264. up_write(&root->kernfs_rwsem);
  1265. }
  1266. static void __kernfs_remove(struct kernfs_node *kn)
  1267. {
  1268. struct kernfs_node *pos, *parent;
  1269. /* Short-circuit if non-root @kn has already finished removal. */
  1270. if (!kn)
  1271. return;
  1272. lockdep_assert_held_write(&kernfs_root(kn)->kernfs_rwsem);
  1273. /*
  1274. * This is for kernfs_remove_self() which plays with active ref
  1275. * after removal.
  1276. */
  1277. if (kernfs_parent(kn) && RB_EMPTY_NODE(&kn->rb))
  1278. return;
  1279. pr_debug("kernfs %s: removing\n", kernfs_rcu_name(kn));
  1280. /* prevent new usage by marking all nodes removing and deactivating */
  1281. pos = NULL;
  1282. while ((pos = kernfs_next_descendant_post(pos, kn))) {
  1283. pos->flags |= KERNFS_REMOVING;
  1284. if (kernfs_active(pos))
  1285. atomic_add(KN_DEACTIVATED_BIAS, &pos->active);
  1286. }
  1287. /* deactivate and unlink the subtree node-by-node */
  1288. do {
  1289. pos = kernfs_leftmost_descendant(kn);
  1290. /*
  1291. * kernfs_drain() may drop kernfs_rwsem temporarily and @pos's
  1292. * base ref could have been put by someone else by the time
  1293. * the function returns. Make sure it doesn't go away
  1294. * underneath us.
  1295. */
  1296. kernfs_get(pos);
  1297. kernfs_drain(pos);
  1298. parent = kernfs_parent(pos);
  1299. /*
  1300. * kernfs_unlink_sibling() succeeds once per node. Use it
  1301. * to decide who's responsible for cleanups.
  1302. */
  1303. if (!parent || kernfs_unlink_sibling(pos)) {
  1304. struct kernfs_iattrs *ps_iattr =
  1305. parent ? parent->iattr : NULL;
  1306. /* update timestamps on the parent */
  1307. down_write(&kernfs_root(kn)->kernfs_iattr_rwsem);
  1308. if (ps_iattr) {
  1309. ktime_get_real_ts64(&ps_iattr->ia_ctime);
  1310. ps_iattr->ia_mtime = ps_iattr->ia_ctime;
  1311. }
  1312. up_write(&kernfs_root(kn)->kernfs_iattr_rwsem);
  1313. kernfs_put(pos);
  1314. }
  1315. kernfs_put(pos);
  1316. } while (pos != kn);
  1317. }
  1318. /**
  1319. * kernfs_remove - remove a kernfs_node recursively
  1320. * @kn: the kernfs_node to remove
  1321. *
  1322. * Remove @kn along with all its subdirectories and files.
  1323. */
  1324. void kernfs_remove(struct kernfs_node *kn)
  1325. {
  1326. struct kernfs_root *root;
  1327. if (!kn)
  1328. return;
  1329. root = kernfs_root(kn);
  1330. down_write(&root->kernfs_rwsem);
  1331. __kernfs_remove(kn);
  1332. up_write(&root->kernfs_rwsem);
  1333. }
  1334. /**
  1335. * kernfs_break_active_protection - break out of active protection
  1336. * @kn: the self kernfs_node
  1337. *
  1338. * The caller must be running off of a kernfs operation which is invoked
  1339. * with an active reference - e.g. one of kernfs_ops. Each invocation of
  1340. * this function must also be matched with an invocation of
  1341. * kernfs_unbreak_active_protection().
  1342. *
  1343. * This function releases the active reference of @kn the caller is
  1344. * holding. Once this function is called, @kn may be removed at any point
  1345. * and the caller is solely responsible for ensuring that the objects it
  1346. * dereferences are accessible.
  1347. */
  1348. void kernfs_break_active_protection(struct kernfs_node *kn)
  1349. {
  1350. /*
  1351. * Take out ourself out of the active ref dependency chain. If
  1352. * we're called without an active ref, lockdep will complain.
  1353. */
  1354. kernfs_put_active(kn);
  1355. }
  1356. /**
  1357. * kernfs_unbreak_active_protection - undo kernfs_break_active_protection()
  1358. * @kn: the self kernfs_node
  1359. *
  1360. * If kernfs_break_active_protection() was called, this function must be
  1361. * invoked before finishing the kernfs operation. Note that while this
  1362. * function restores the active reference, it doesn't and can't actually
  1363. * restore the active protection - @kn may already or be in the process of
  1364. * being drained and removed. Once kernfs_break_active_protection() is
  1365. * invoked, that protection is irreversibly gone for the kernfs operation
  1366. * instance.
  1367. *
  1368. * While this function may be called at any point after
  1369. * kernfs_break_active_protection() is invoked, its most useful location
  1370. * would be right before the enclosing kernfs operation returns.
  1371. */
  1372. void kernfs_unbreak_active_protection(struct kernfs_node *kn)
  1373. {
  1374. /*
  1375. * @kn->active could be in any state; however, the increment we do
  1376. * here will be undone as soon as the enclosing kernfs operation
  1377. * finishes and this temporary bump can't break anything. If @kn
  1378. * is alive, nothing changes. If @kn is being deactivated, the
  1379. * soon-to-follow put will either finish deactivation or restore
  1380. * deactivated state. If @kn is already removed, the temporary
  1381. * bump is guaranteed to be gone before @kn is released.
  1382. */
  1383. atomic_inc(&kn->active);
  1384. if (kernfs_lockdep(kn))
  1385. rwsem_acquire(&kn->dep_map, 0, 1, _RET_IP_);
  1386. }
  1387. /**
  1388. * kernfs_remove_self - remove a kernfs_node from its own method
  1389. * @kn: the self kernfs_node to remove
  1390. *
  1391. * The caller must be running off of a kernfs operation which is invoked
  1392. * with an active reference - e.g. one of kernfs_ops. This can be used to
  1393. * implement a file operation which deletes itself.
  1394. *
  1395. * For example, the "delete" file for a sysfs device directory can be
  1396. * implemented by invoking kernfs_remove_self() on the "delete" file
  1397. * itself. This function breaks the circular dependency of trying to
  1398. * deactivate self while holding an active ref itself. It isn't necessary
  1399. * to modify the usual removal path to use kernfs_remove_self(). The
  1400. * "delete" implementation can simply invoke kernfs_remove_self() on self
  1401. * before proceeding with the usual removal path. kernfs will ignore later
  1402. * kernfs_remove() on self.
  1403. *
  1404. * kernfs_remove_self() can be called multiple times concurrently on the
  1405. * same kernfs_node. Only the first one actually performs removal and
  1406. * returns %true. All others will wait until the kernfs operation which
  1407. * won self-removal finishes and return %false. Note that the losers wait
  1408. * for the completion of not only the winning kernfs_remove_self() but also
  1409. * the whole kernfs_ops which won the arbitration. This can be used to
  1410. * guarantee, for example, all concurrent writes to a "delete" file to
  1411. * finish only after the whole operation is complete.
  1412. *
  1413. * Return: %true if @kn is removed by this call, otherwise %false.
  1414. */
  1415. bool kernfs_remove_self(struct kernfs_node *kn)
  1416. {
  1417. bool ret;
  1418. struct kernfs_root *root = kernfs_root(kn);
  1419. down_write(&root->kernfs_rwsem);
  1420. kernfs_break_active_protection(kn);
  1421. /*
  1422. * SUICIDAL is used to arbitrate among competing invocations. Only
  1423. * the first one will actually perform removal. When the removal
  1424. * is complete, SUICIDED is set and the active ref is restored
  1425. * while kernfs_rwsem for held exclusive. The ones which lost
  1426. * arbitration waits for SUICIDED && drained which can happen only
  1427. * after the enclosing kernfs operation which executed the winning
  1428. * instance of kernfs_remove_self() finished.
  1429. */
  1430. if (!(kn->flags & KERNFS_SUICIDAL)) {
  1431. kn->flags |= KERNFS_SUICIDAL;
  1432. __kernfs_remove(kn);
  1433. kn->flags |= KERNFS_SUICIDED;
  1434. ret = true;
  1435. } else {
  1436. wait_queue_head_t *waitq = &kernfs_root(kn)->deactivate_waitq;
  1437. DEFINE_WAIT(wait);
  1438. while (true) {
  1439. prepare_to_wait(waitq, &wait, TASK_UNINTERRUPTIBLE);
  1440. if ((kn->flags & KERNFS_SUICIDED) &&
  1441. atomic_read(&kn->active) == KN_DEACTIVATED_BIAS)
  1442. break;
  1443. up_write(&root->kernfs_rwsem);
  1444. schedule();
  1445. down_write(&root->kernfs_rwsem);
  1446. }
  1447. finish_wait(waitq, &wait);
  1448. WARN_ON_ONCE(!RB_EMPTY_NODE(&kn->rb));
  1449. ret = false;
  1450. }
  1451. /*
  1452. * This must be done while kernfs_rwsem held exclusive; otherwise,
  1453. * waiting for SUICIDED && deactivated could finish prematurely.
  1454. */
  1455. kernfs_unbreak_active_protection(kn);
  1456. up_write(&root->kernfs_rwsem);
  1457. return ret;
  1458. }
  1459. /**
  1460. * kernfs_remove_by_name_ns - find a kernfs_node by name and remove it
  1461. * @parent: parent of the target
  1462. * @name: name of the kernfs_node to remove
  1463. * @ns: namespace tag of the kernfs_node to remove
  1464. *
  1465. * Look for the kernfs_node with @name and @ns under @parent and remove it.
  1466. *
  1467. * Return: %0 on success, -ENOENT if such entry doesn't exist.
  1468. */
  1469. int kernfs_remove_by_name_ns(struct kernfs_node *parent, const char *name,
  1470. const struct ns_common *ns)
  1471. {
  1472. struct kernfs_node *kn;
  1473. struct kernfs_root *root;
  1474. if (!parent) {
  1475. WARN(1, KERN_WARNING "kernfs: can not remove '%s', no directory\n",
  1476. name);
  1477. return -ENOENT;
  1478. }
  1479. root = kernfs_root(parent);
  1480. down_write(&root->kernfs_rwsem);
  1481. kn = kernfs_find_ns(parent, name, ns);
  1482. if (kn) {
  1483. kernfs_get(kn);
  1484. __kernfs_remove(kn);
  1485. kernfs_put(kn);
  1486. }
  1487. up_write(&root->kernfs_rwsem);
  1488. if (kn)
  1489. return 0;
  1490. else
  1491. return -ENOENT;
  1492. }
  1493. /**
  1494. * kernfs_rename_ns - move and rename a kernfs_node
  1495. * @kn: target node
  1496. * @new_parent: new parent to put @sd under
  1497. * @new_name: new name
  1498. * @new_ns: new namespace tag
  1499. *
  1500. * Return: %0 on success, -errno on failure.
  1501. */
  1502. int kernfs_rename_ns(struct kernfs_node *kn, struct kernfs_node *new_parent,
  1503. const char *new_name, const struct ns_common *new_ns)
  1504. {
  1505. struct kernfs_node *old_parent;
  1506. struct kernfs_root *root;
  1507. const char *old_name;
  1508. int error;
  1509. /* can't move or rename root */
  1510. if (!rcu_access_pointer(kn->__parent))
  1511. return -EINVAL;
  1512. root = kernfs_root(kn);
  1513. down_write(&root->kernfs_rwsem);
  1514. error = -ENOENT;
  1515. if (!kernfs_active(kn) || !kernfs_active(new_parent) ||
  1516. (new_parent->flags & KERNFS_EMPTY_DIR))
  1517. goto out;
  1518. old_parent = kernfs_parent(kn);
  1519. if (root->flags & KERNFS_ROOT_INVARIANT_PARENT) {
  1520. error = -EINVAL;
  1521. if (WARN_ON_ONCE(old_parent != new_parent))
  1522. goto out;
  1523. }
  1524. error = 0;
  1525. old_name = kernfs_rcu_name(kn);
  1526. if (!new_name)
  1527. new_name = old_name;
  1528. if ((old_parent == new_parent) &&
  1529. (kernfs_ns_id(kn->ns) == kernfs_ns_id(new_ns)) &&
  1530. (strcmp(old_name, new_name) == 0))
  1531. goto out; /* nothing to rename */
  1532. error = -EEXIST;
  1533. if (kernfs_find_ns(new_parent, new_name, new_ns))
  1534. goto out;
  1535. /* rename kernfs_node */
  1536. if (strcmp(old_name, new_name) != 0) {
  1537. error = -ENOMEM;
  1538. new_name = kstrdup_const(new_name, GFP_KERNEL);
  1539. if (!new_name)
  1540. goto out;
  1541. } else {
  1542. new_name = NULL;
  1543. }
  1544. /*
  1545. * Move to the appropriate place in the appropriate directories rbtree.
  1546. */
  1547. kernfs_unlink_sibling(kn);
  1548. /* rename_lock protects ->parent accessors */
  1549. if (old_parent != new_parent) {
  1550. kernfs_get(new_parent);
  1551. write_lock_irq(&root->kernfs_rename_lock);
  1552. rcu_assign_pointer(kn->__parent, new_parent);
  1553. kn->ns = new_ns;
  1554. if (new_name)
  1555. rcu_assign_pointer(kn->name, new_name);
  1556. write_unlock_irq(&root->kernfs_rename_lock);
  1557. kernfs_put(old_parent);
  1558. } else {
  1559. /* name assignment is RCU protected, parent is the same */
  1560. kn->ns = new_ns;
  1561. if (new_name)
  1562. rcu_assign_pointer(kn->name, new_name);
  1563. }
  1564. kn->hash = kernfs_name_hash(new_name ?: old_name, kn->ns);
  1565. kernfs_link_sibling(kn);
  1566. if (new_name && !is_kernel_rodata((unsigned long)old_name))
  1567. kfree_rcu_mightsleep(old_name);
  1568. error = 0;
  1569. out:
  1570. up_write(&root->kernfs_rwsem);
  1571. return error;
  1572. }
  1573. static int kernfs_dir_fop_release(struct inode *inode, struct file *filp)
  1574. {
  1575. kernfs_put(filp->private_data);
  1576. return 0;
  1577. }
  1578. static struct kernfs_node *kernfs_dir_pos(const struct ns_common *ns,
  1579. struct kernfs_node *parent, loff_t hash, struct kernfs_node *pos)
  1580. {
  1581. if (pos) {
  1582. int valid = kernfs_active(pos) &&
  1583. rcu_access_pointer(pos->__parent) == parent &&
  1584. hash == pos->hash;
  1585. kernfs_put(pos);
  1586. if (!valid)
  1587. pos = NULL;
  1588. }
  1589. if (!pos && (hash > 1) && (hash < INT_MAX)) {
  1590. struct rb_node *node = parent->dir.children.rb_node;
  1591. u64 ns_id = kernfs_ns_id(ns);
  1592. while (node) {
  1593. pos = rb_to_kn(node);
  1594. if (hash < pos->hash)
  1595. node = node->rb_left;
  1596. else if (hash > pos->hash)
  1597. node = node->rb_right;
  1598. else if (ns_id < kernfs_ns_id(pos->ns))
  1599. node = node->rb_left;
  1600. else if (ns_id > kernfs_ns_id(pos->ns))
  1601. node = node->rb_right;
  1602. else
  1603. break;
  1604. }
  1605. }
  1606. /* Skip over entries which are dying/dead or in the wrong namespace */
  1607. while (pos && (!kernfs_active(pos) ||
  1608. kernfs_ns_id(pos->ns) != kernfs_ns_id(ns))) {
  1609. struct rb_node *node = rb_next(&pos->rb);
  1610. if (!node)
  1611. pos = NULL;
  1612. else
  1613. pos = rb_to_kn(node);
  1614. }
  1615. return pos;
  1616. }
  1617. static struct kernfs_node *kernfs_dir_next_pos(const struct ns_common *ns,
  1618. struct kernfs_node *parent, ino_t ino, struct kernfs_node *pos)
  1619. {
  1620. pos = kernfs_dir_pos(ns, parent, ino, pos);
  1621. if (pos) {
  1622. do {
  1623. struct rb_node *node = rb_next(&pos->rb);
  1624. if (!node)
  1625. pos = NULL;
  1626. else
  1627. pos = rb_to_kn(node);
  1628. } while (pos && (!kernfs_active(pos) ||
  1629. kernfs_ns_id(pos->ns) != kernfs_ns_id(ns)));
  1630. }
  1631. return pos;
  1632. }
  1633. static int kernfs_fop_readdir(struct file *file, struct dir_context *ctx)
  1634. {
  1635. struct dentry *dentry = file->f_path.dentry;
  1636. struct kernfs_node *parent = kernfs_dentry_node(dentry);
  1637. struct kernfs_node *pos = file->private_data;
  1638. struct kernfs_root *root;
  1639. const struct ns_common *ns = NULL;
  1640. if (!dir_emit_dots(file, ctx))
  1641. return 0;
  1642. root = kernfs_root(parent);
  1643. down_read(&root->kernfs_rwsem);
  1644. if (kernfs_ns_enabled(parent))
  1645. ns = kernfs_info(dentry->d_sb)->ns;
  1646. for (pos = kernfs_dir_pos(ns, parent, ctx->pos, pos);
  1647. pos;
  1648. pos = kernfs_dir_next_pos(ns, parent, ctx->pos, pos)) {
  1649. const char *name = kernfs_rcu_name(pos);
  1650. unsigned int type = fs_umode_to_dtype(pos->mode);
  1651. int len = strlen(name);
  1652. ino_t ino = kernfs_ino(pos);
  1653. ctx->pos = pos->hash;
  1654. file->private_data = pos;
  1655. kernfs_get(pos);
  1656. if (!dir_emit(ctx, name, len, ino, type)) {
  1657. up_read(&root->kernfs_rwsem);
  1658. return 0;
  1659. }
  1660. }
  1661. up_read(&root->kernfs_rwsem);
  1662. file->private_data = NULL;
  1663. ctx->pos = INT_MAX;
  1664. return 0;
  1665. }
  1666. const struct file_operations kernfs_dir_fops = {
  1667. .read = generic_read_dir,
  1668. .iterate_shared = kernfs_fop_readdir,
  1669. .release = kernfs_dir_fop_release,
  1670. .llseek = generic_file_llseek,
  1671. };