dir.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428
  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 (d_really_is_negative(dentry))
  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. /*
  445. * If the ino of the sysfs entry created for a kmem cache gets
  446. * allocated from an ida layer, which is accounted to the memcg that
  447. * owns the cache, the memcg will get pinned forever. So do not account
  448. * ino ida allocations.
  449. */
  450. ret = ida_simple_get(&root->ino_ida, 1, 0,
  451. GFP_KERNEL | __GFP_NOACCOUNT);
  452. if (ret < 0)
  453. goto err_out2;
  454. kn->ino = ret;
  455. atomic_set(&kn->count, 1);
  456. atomic_set(&kn->active, KN_DEACTIVATED_BIAS);
  457. RB_CLEAR_NODE(&kn->rb);
  458. kn->name = name;
  459. kn->mode = mode;
  460. kn->flags = flags;
  461. return kn;
  462. err_out2:
  463. kmem_cache_free(kernfs_node_cache, kn);
  464. err_out1:
  465. kfree_const(name);
  466. return NULL;
  467. }
  468. struct kernfs_node *kernfs_new_node(struct kernfs_node *parent,
  469. const char *name, umode_t mode,
  470. unsigned flags)
  471. {
  472. struct kernfs_node *kn;
  473. kn = __kernfs_new_node(kernfs_root(parent), name, mode, flags);
  474. if (kn) {
  475. kernfs_get(parent);
  476. kn->parent = parent;
  477. }
  478. return kn;
  479. }
  480. /**
  481. * kernfs_add_one - add kernfs_node to parent without warning
  482. * @kn: kernfs_node to be added
  483. *
  484. * The caller must already have initialized @kn->parent. This
  485. * function increments nlink of the parent's inode if @kn is a
  486. * directory and link into the children list of the parent.
  487. *
  488. * RETURNS:
  489. * 0 on success, -EEXIST if entry with the given name already
  490. * exists.
  491. */
  492. int kernfs_add_one(struct kernfs_node *kn)
  493. {
  494. struct kernfs_node *parent = kn->parent;
  495. struct kernfs_iattrs *ps_iattr;
  496. bool has_ns;
  497. int ret;
  498. mutex_lock(&kernfs_mutex);
  499. ret = -EINVAL;
  500. has_ns = kernfs_ns_enabled(parent);
  501. if (WARN(has_ns != (bool)kn->ns, KERN_WARNING "kernfs: ns %s in '%s' for '%s'\n",
  502. has_ns ? "required" : "invalid", parent->name, kn->name))
  503. goto out_unlock;
  504. if (kernfs_type(parent) != KERNFS_DIR)
  505. goto out_unlock;
  506. ret = -ENOENT;
  507. if ((parent->flags & KERNFS_ACTIVATED) && !kernfs_active(parent))
  508. goto out_unlock;
  509. kn->hash = kernfs_name_hash(kn->name, kn->ns);
  510. ret = kernfs_link_sibling(kn);
  511. if (ret)
  512. goto out_unlock;
  513. /* Update timestamps on the parent */
  514. ps_iattr = parent->iattr;
  515. if (ps_iattr) {
  516. struct iattr *ps_iattrs = &ps_iattr->ia_iattr;
  517. ps_iattrs->ia_ctime = ps_iattrs->ia_mtime = CURRENT_TIME;
  518. }
  519. mutex_unlock(&kernfs_mutex);
  520. /*
  521. * Activate the new node unless CREATE_DEACTIVATED is requested.
  522. * If not activated here, the kernfs user is responsible for
  523. * activating the node with kernfs_activate(). A node which hasn't
  524. * been activated is not visible to userland and its removal won't
  525. * trigger deactivation.
  526. */
  527. if (!(kernfs_root(kn)->flags & KERNFS_ROOT_CREATE_DEACTIVATED))
  528. kernfs_activate(kn);
  529. return 0;
  530. out_unlock:
  531. mutex_unlock(&kernfs_mutex);
  532. return ret;
  533. }
  534. /**
  535. * kernfs_find_ns - find kernfs_node with the given name
  536. * @parent: kernfs_node to search under
  537. * @name: name to look for
  538. * @ns: the namespace tag to use
  539. *
  540. * Look for kernfs_node with name @name under @parent. Returns pointer to
  541. * the found kernfs_node on success, %NULL on failure.
  542. */
  543. static struct kernfs_node *kernfs_find_ns(struct kernfs_node *parent,
  544. const unsigned char *name,
  545. const void *ns)
  546. {
  547. struct rb_node *node = parent->dir.children.rb_node;
  548. bool has_ns = kernfs_ns_enabled(parent);
  549. unsigned int hash;
  550. lockdep_assert_held(&kernfs_mutex);
  551. if (has_ns != (bool)ns) {
  552. WARN(1, KERN_WARNING "kernfs: ns %s in '%s' for '%s'\n",
  553. has_ns ? "required" : "invalid", parent->name, name);
  554. return NULL;
  555. }
  556. hash = kernfs_name_hash(name, ns);
  557. while (node) {
  558. struct kernfs_node *kn;
  559. int result;
  560. kn = rb_to_kn(node);
  561. result = kernfs_name_compare(hash, name, ns, kn);
  562. if (result < 0)
  563. node = node->rb_left;
  564. else if (result > 0)
  565. node = node->rb_right;
  566. else
  567. return kn;
  568. }
  569. return NULL;
  570. }
  571. /**
  572. * kernfs_find_and_get_ns - find and get kernfs_node with the given name
  573. * @parent: kernfs_node to search under
  574. * @name: name to look for
  575. * @ns: the namespace tag to use
  576. *
  577. * Look for kernfs_node with name @name under @parent and get a reference
  578. * if found. This function may sleep and returns pointer to the found
  579. * kernfs_node on success, %NULL on failure.
  580. */
  581. struct kernfs_node *kernfs_find_and_get_ns(struct kernfs_node *parent,
  582. const char *name, const void *ns)
  583. {
  584. struct kernfs_node *kn;
  585. mutex_lock(&kernfs_mutex);
  586. kn = kernfs_find_ns(parent, name, ns);
  587. kernfs_get(kn);
  588. mutex_unlock(&kernfs_mutex);
  589. return kn;
  590. }
  591. EXPORT_SYMBOL_GPL(kernfs_find_and_get_ns);
  592. /**
  593. * kernfs_create_root - create a new kernfs hierarchy
  594. * @scops: optional syscall operations for the hierarchy
  595. * @flags: KERNFS_ROOT_* flags
  596. * @priv: opaque data associated with the new directory
  597. *
  598. * Returns the root of the new hierarchy on success, ERR_PTR() value on
  599. * failure.
  600. */
  601. struct kernfs_root *kernfs_create_root(struct kernfs_syscall_ops *scops,
  602. unsigned int flags, void *priv)
  603. {
  604. struct kernfs_root *root;
  605. struct kernfs_node *kn;
  606. root = kzalloc(sizeof(*root), GFP_KERNEL);
  607. if (!root)
  608. return ERR_PTR(-ENOMEM);
  609. ida_init(&root->ino_ida);
  610. INIT_LIST_HEAD(&root->supers);
  611. kn = __kernfs_new_node(root, "", S_IFDIR | S_IRUGO | S_IXUGO,
  612. KERNFS_DIR);
  613. if (!kn) {
  614. ida_destroy(&root->ino_ida);
  615. kfree(root);
  616. return ERR_PTR(-ENOMEM);
  617. }
  618. kn->priv = priv;
  619. kn->dir.root = root;
  620. root->syscall_ops = scops;
  621. root->flags = flags;
  622. root->kn = kn;
  623. init_waitqueue_head(&root->deactivate_waitq);
  624. if (!(root->flags & KERNFS_ROOT_CREATE_DEACTIVATED))
  625. kernfs_activate(kn);
  626. return root;
  627. }
  628. /**
  629. * kernfs_destroy_root - destroy a kernfs hierarchy
  630. * @root: root of the hierarchy to destroy
  631. *
  632. * Destroy the hierarchy anchored at @root by removing all existing
  633. * directories and destroying @root.
  634. */
  635. void kernfs_destroy_root(struct kernfs_root *root)
  636. {
  637. kernfs_remove(root->kn); /* will also free @root */
  638. }
  639. /**
  640. * kernfs_create_dir_ns - create a directory
  641. * @parent: parent in which to create a new directory
  642. * @name: name of the new directory
  643. * @mode: mode of the new directory
  644. * @priv: opaque data associated with the new directory
  645. * @ns: optional namespace tag of the directory
  646. *
  647. * Returns the created node on success, ERR_PTR() value on failure.
  648. */
  649. struct kernfs_node *kernfs_create_dir_ns(struct kernfs_node *parent,
  650. const char *name, umode_t mode,
  651. void *priv, const void *ns)
  652. {
  653. struct kernfs_node *kn;
  654. int rc;
  655. /* allocate */
  656. kn = kernfs_new_node(parent, name, mode | S_IFDIR, KERNFS_DIR);
  657. if (!kn)
  658. return ERR_PTR(-ENOMEM);
  659. kn->dir.root = parent->dir.root;
  660. kn->ns = ns;
  661. kn->priv = priv;
  662. /* link in */
  663. rc = kernfs_add_one(kn);
  664. if (!rc)
  665. return kn;
  666. kernfs_put(kn);
  667. return ERR_PTR(rc);
  668. }
  669. static struct dentry *kernfs_iop_lookup(struct inode *dir,
  670. struct dentry *dentry,
  671. unsigned int flags)
  672. {
  673. struct dentry *ret;
  674. struct kernfs_node *parent = dentry->d_parent->d_fsdata;
  675. struct kernfs_node *kn;
  676. struct inode *inode;
  677. const void *ns = NULL;
  678. mutex_lock(&kernfs_mutex);
  679. if (kernfs_ns_enabled(parent))
  680. ns = kernfs_info(dir->i_sb)->ns;
  681. kn = kernfs_find_ns(parent, dentry->d_name.name, ns);
  682. /* no such entry */
  683. if (!kn || !kernfs_active(kn)) {
  684. ret = NULL;
  685. goto out_unlock;
  686. }
  687. kernfs_get(kn);
  688. dentry->d_fsdata = kn;
  689. /* attach dentry and inode */
  690. inode = kernfs_get_inode(dir->i_sb, kn);
  691. if (!inode) {
  692. ret = ERR_PTR(-ENOMEM);
  693. goto out_unlock;
  694. }
  695. /* instantiate and hash dentry */
  696. ret = d_splice_alias(inode, dentry);
  697. out_unlock:
  698. mutex_unlock(&kernfs_mutex);
  699. return ret;
  700. }
  701. static int kernfs_iop_mkdir(struct inode *dir, struct dentry *dentry,
  702. umode_t mode)
  703. {
  704. struct kernfs_node *parent = dir->i_private;
  705. struct kernfs_syscall_ops *scops = kernfs_root(parent)->syscall_ops;
  706. int ret;
  707. if (!scops || !scops->mkdir)
  708. return -EPERM;
  709. if (!kernfs_get_active(parent))
  710. return -ENODEV;
  711. ret = scops->mkdir(parent, dentry->d_name.name, mode);
  712. kernfs_put_active(parent);
  713. return ret;
  714. }
  715. static int kernfs_iop_rmdir(struct inode *dir, struct dentry *dentry)
  716. {
  717. struct kernfs_node *kn = dentry->d_fsdata;
  718. struct kernfs_syscall_ops *scops = kernfs_root(kn)->syscall_ops;
  719. int ret;
  720. if (!scops || !scops->rmdir)
  721. return -EPERM;
  722. if (!kernfs_get_active(kn))
  723. return -ENODEV;
  724. ret = scops->rmdir(kn);
  725. kernfs_put_active(kn);
  726. return ret;
  727. }
  728. static int kernfs_iop_rename(struct inode *old_dir, struct dentry *old_dentry,
  729. struct inode *new_dir, struct dentry *new_dentry)
  730. {
  731. struct kernfs_node *kn = old_dentry->d_fsdata;
  732. struct kernfs_node *new_parent = new_dir->i_private;
  733. struct kernfs_syscall_ops *scops = kernfs_root(kn)->syscall_ops;
  734. int ret;
  735. if (!scops || !scops->rename)
  736. return -EPERM;
  737. if (!kernfs_get_active(kn))
  738. return -ENODEV;
  739. if (!kernfs_get_active(new_parent)) {
  740. kernfs_put_active(kn);
  741. return -ENODEV;
  742. }
  743. ret = scops->rename(kn, new_parent, new_dentry->d_name.name);
  744. kernfs_put_active(new_parent);
  745. kernfs_put_active(kn);
  746. return ret;
  747. }
  748. const struct inode_operations kernfs_dir_iops = {
  749. .lookup = kernfs_iop_lookup,
  750. .permission = kernfs_iop_permission,
  751. .setattr = kernfs_iop_setattr,
  752. .getattr = kernfs_iop_getattr,
  753. .setxattr = kernfs_iop_setxattr,
  754. .removexattr = kernfs_iop_removexattr,
  755. .getxattr = kernfs_iop_getxattr,
  756. .listxattr = kernfs_iop_listxattr,
  757. .mkdir = kernfs_iop_mkdir,
  758. .rmdir = kernfs_iop_rmdir,
  759. .rename = kernfs_iop_rename,
  760. };
  761. static struct kernfs_node *kernfs_leftmost_descendant(struct kernfs_node *pos)
  762. {
  763. struct kernfs_node *last;
  764. while (true) {
  765. struct rb_node *rbn;
  766. last = pos;
  767. if (kernfs_type(pos) != KERNFS_DIR)
  768. break;
  769. rbn = rb_first(&pos->dir.children);
  770. if (!rbn)
  771. break;
  772. pos = rb_to_kn(rbn);
  773. }
  774. return last;
  775. }
  776. /**
  777. * kernfs_next_descendant_post - find the next descendant for post-order walk
  778. * @pos: the current position (%NULL to initiate traversal)
  779. * @root: kernfs_node whose descendants to walk
  780. *
  781. * Find the next descendant to visit for post-order traversal of @root's
  782. * descendants. @root is included in the iteration and the last node to be
  783. * visited.
  784. */
  785. static struct kernfs_node *kernfs_next_descendant_post(struct kernfs_node *pos,
  786. struct kernfs_node *root)
  787. {
  788. struct rb_node *rbn;
  789. lockdep_assert_held(&kernfs_mutex);
  790. /* if first iteration, visit leftmost descendant which may be root */
  791. if (!pos)
  792. return kernfs_leftmost_descendant(root);
  793. /* if we visited @root, we're done */
  794. if (pos == root)
  795. return NULL;
  796. /* if there's an unvisited sibling, visit its leftmost descendant */
  797. rbn = rb_next(&pos->rb);
  798. if (rbn)
  799. return kernfs_leftmost_descendant(rb_to_kn(rbn));
  800. /* no sibling left, visit parent */
  801. return pos->parent;
  802. }
  803. /**
  804. * kernfs_activate - activate a node which started deactivated
  805. * @kn: kernfs_node whose subtree is to be activated
  806. *
  807. * If the root has KERNFS_ROOT_CREATE_DEACTIVATED set, a newly created node
  808. * needs to be explicitly activated. A node which hasn't been activated
  809. * isn't visible to userland and deactivation is skipped during its
  810. * removal. This is useful to construct atomic init sequences where
  811. * creation of multiple nodes should either succeed or fail atomically.
  812. *
  813. * The caller is responsible for ensuring that this function is not called
  814. * after kernfs_remove*() is invoked on @kn.
  815. */
  816. void kernfs_activate(struct kernfs_node *kn)
  817. {
  818. struct kernfs_node *pos;
  819. mutex_lock(&kernfs_mutex);
  820. pos = NULL;
  821. while ((pos = kernfs_next_descendant_post(pos, kn))) {
  822. if (!pos || (pos->flags & KERNFS_ACTIVATED))
  823. continue;
  824. WARN_ON_ONCE(pos->parent && RB_EMPTY_NODE(&pos->rb));
  825. WARN_ON_ONCE(atomic_read(&pos->active) != KN_DEACTIVATED_BIAS);
  826. atomic_sub(KN_DEACTIVATED_BIAS, &pos->active);
  827. pos->flags |= KERNFS_ACTIVATED;
  828. }
  829. mutex_unlock(&kernfs_mutex);
  830. }
  831. static void __kernfs_remove(struct kernfs_node *kn)
  832. {
  833. struct kernfs_node *pos;
  834. lockdep_assert_held(&kernfs_mutex);
  835. /*
  836. * Short-circuit if non-root @kn has already finished removal.
  837. * This is for kernfs_remove_self() which plays with active ref
  838. * after removal.
  839. */
  840. if (!kn || (kn->parent && RB_EMPTY_NODE(&kn->rb)))
  841. return;
  842. pr_debug("kernfs %s: removing\n", kn->name);
  843. /* prevent any new usage under @kn by deactivating all nodes */
  844. pos = NULL;
  845. while ((pos = kernfs_next_descendant_post(pos, kn)))
  846. if (kernfs_active(pos))
  847. atomic_add(KN_DEACTIVATED_BIAS, &pos->active);
  848. /* deactivate and unlink the subtree node-by-node */
  849. do {
  850. pos = kernfs_leftmost_descendant(kn);
  851. /*
  852. * kernfs_drain() drops kernfs_mutex temporarily and @pos's
  853. * base ref could have been put by someone else by the time
  854. * the function returns. Make sure it doesn't go away
  855. * underneath us.
  856. */
  857. kernfs_get(pos);
  858. /*
  859. * Drain iff @kn was activated. This avoids draining and
  860. * its lockdep annotations for nodes which have never been
  861. * activated and allows embedding kernfs_remove() in create
  862. * error paths without worrying about draining.
  863. */
  864. if (kn->flags & KERNFS_ACTIVATED)
  865. kernfs_drain(pos);
  866. else
  867. WARN_ON_ONCE(atomic_read(&kn->active) != KN_DEACTIVATED_BIAS);
  868. /*
  869. * kernfs_unlink_sibling() succeeds once per node. Use it
  870. * to decide who's responsible for cleanups.
  871. */
  872. if (!pos->parent || kernfs_unlink_sibling(pos)) {
  873. struct kernfs_iattrs *ps_iattr =
  874. pos->parent ? pos->parent->iattr : NULL;
  875. /* update timestamps on the parent */
  876. if (ps_iattr) {
  877. ps_iattr->ia_iattr.ia_ctime = CURRENT_TIME;
  878. ps_iattr->ia_iattr.ia_mtime = CURRENT_TIME;
  879. }
  880. kernfs_put(pos);
  881. }
  882. kernfs_put(pos);
  883. } while (pos != kn);
  884. }
  885. /**
  886. * kernfs_remove - remove a kernfs_node recursively
  887. * @kn: the kernfs_node to remove
  888. *
  889. * Remove @kn along with all its subdirectories and files.
  890. */
  891. void kernfs_remove(struct kernfs_node *kn)
  892. {
  893. mutex_lock(&kernfs_mutex);
  894. __kernfs_remove(kn);
  895. mutex_unlock(&kernfs_mutex);
  896. }
  897. /**
  898. * kernfs_break_active_protection - break out of active protection
  899. * @kn: the self kernfs_node
  900. *
  901. * The caller must be running off of a kernfs operation which is invoked
  902. * with an active reference - e.g. one of kernfs_ops. Each invocation of
  903. * this function must also be matched with an invocation of
  904. * kernfs_unbreak_active_protection().
  905. *
  906. * This function releases the active reference of @kn the caller is
  907. * holding. Once this function is called, @kn may be removed at any point
  908. * and the caller is solely responsible for ensuring that the objects it
  909. * dereferences are accessible.
  910. */
  911. void kernfs_break_active_protection(struct kernfs_node *kn)
  912. {
  913. /*
  914. * Take out ourself out of the active ref dependency chain. If
  915. * we're called without an active ref, lockdep will complain.
  916. */
  917. kernfs_put_active(kn);
  918. }
  919. /**
  920. * kernfs_unbreak_active_protection - undo kernfs_break_active_protection()
  921. * @kn: the self kernfs_node
  922. *
  923. * If kernfs_break_active_protection() was called, this function must be
  924. * invoked before finishing the kernfs operation. Note that while this
  925. * function restores the active reference, it doesn't and can't actually
  926. * restore the active protection - @kn may already or be in the process of
  927. * being removed. Once kernfs_break_active_protection() is invoked, that
  928. * protection is irreversibly gone for the kernfs operation instance.
  929. *
  930. * While this function may be called at any point after
  931. * kernfs_break_active_protection() is invoked, its most useful location
  932. * would be right before the enclosing kernfs operation returns.
  933. */
  934. void kernfs_unbreak_active_protection(struct kernfs_node *kn)
  935. {
  936. /*
  937. * @kn->active could be in any state; however, the increment we do
  938. * here will be undone as soon as the enclosing kernfs operation
  939. * finishes and this temporary bump can't break anything. If @kn
  940. * is alive, nothing changes. If @kn is being deactivated, the
  941. * soon-to-follow put will either finish deactivation or restore
  942. * deactivated state. If @kn is already removed, the temporary
  943. * bump is guaranteed to be gone before @kn is released.
  944. */
  945. atomic_inc(&kn->active);
  946. if (kernfs_lockdep(kn))
  947. rwsem_acquire(&kn->dep_map, 0, 1, _RET_IP_);
  948. }
  949. /**
  950. * kernfs_remove_self - remove a kernfs_node from its own method
  951. * @kn: the self kernfs_node to remove
  952. *
  953. * The caller must be running off of a kernfs operation which is invoked
  954. * with an active reference - e.g. one of kernfs_ops. This can be used to
  955. * implement a file operation which deletes itself.
  956. *
  957. * For example, the "delete" file for a sysfs device directory can be
  958. * implemented by invoking kernfs_remove_self() on the "delete" file
  959. * itself. This function breaks the circular dependency of trying to
  960. * deactivate self while holding an active ref itself. It isn't necessary
  961. * to modify the usual removal path to use kernfs_remove_self(). The
  962. * "delete" implementation can simply invoke kernfs_remove_self() on self
  963. * before proceeding with the usual removal path. kernfs will ignore later
  964. * kernfs_remove() on self.
  965. *
  966. * kernfs_remove_self() can be called multiple times concurrently on the
  967. * same kernfs_node. Only the first one actually performs removal and
  968. * returns %true. All others will wait until the kernfs operation which
  969. * won self-removal finishes and return %false. Note that the losers wait
  970. * for the completion of not only the winning kernfs_remove_self() but also
  971. * the whole kernfs_ops which won the arbitration. This can be used to
  972. * guarantee, for example, all concurrent writes to a "delete" file to
  973. * finish only after the whole operation is complete.
  974. */
  975. bool kernfs_remove_self(struct kernfs_node *kn)
  976. {
  977. bool ret;
  978. mutex_lock(&kernfs_mutex);
  979. kernfs_break_active_protection(kn);
  980. /*
  981. * SUICIDAL is used to arbitrate among competing invocations. Only
  982. * the first one will actually perform removal. When the removal
  983. * is complete, SUICIDED is set and the active ref is restored
  984. * while holding kernfs_mutex. The ones which lost arbitration
  985. * waits for SUICDED && drained which can happen only after the
  986. * enclosing kernfs operation which executed the winning instance
  987. * of kernfs_remove_self() finished.
  988. */
  989. if (!(kn->flags & KERNFS_SUICIDAL)) {
  990. kn->flags |= KERNFS_SUICIDAL;
  991. __kernfs_remove(kn);
  992. kn->flags |= KERNFS_SUICIDED;
  993. ret = true;
  994. } else {
  995. wait_queue_head_t *waitq = &kernfs_root(kn)->deactivate_waitq;
  996. DEFINE_WAIT(wait);
  997. while (true) {
  998. prepare_to_wait(waitq, &wait, TASK_UNINTERRUPTIBLE);
  999. if ((kn->flags & KERNFS_SUICIDED) &&
  1000. atomic_read(&kn->active) == KN_DEACTIVATED_BIAS)
  1001. break;
  1002. mutex_unlock(&kernfs_mutex);
  1003. schedule();
  1004. mutex_lock(&kernfs_mutex);
  1005. }
  1006. finish_wait(waitq, &wait);
  1007. WARN_ON_ONCE(!RB_EMPTY_NODE(&kn->rb));
  1008. ret = false;
  1009. }
  1010. /*
  1011. * This must be done while holding kernfs_mutex; otherwise, waiting
  1012. * for SUICIDED && deactivated could finish prematurely.
  1013. */
  1014. kernfs_unbreak_active_protection(kn);
  1015. mutex_unlock(&kernfs_mutex);
  1016. return ret;
  1017. }
  1018. /**
  1019. * kernfs_remove_by_name_ns - find a kernfs_node by name and remove it
  1020. * @parent: parent of the target
  1021. * @name: name of the kernfs_node to remove
  1022. * @ns: namespace tag of the kernfs_node to remove
  1023. *
  1024. * Look for the kernfs_node with @name and @ns under @parent and remove it.
  1025. * Returns 0 on success, -ENOENT if such entry doesn't exist.
  1026. */
  1027. int kernfs_remove_by_name_ns(struct kernfs_node *parent, const char *name,
  1028. const void *ns)
  1029. {
  1030. struct kernfs_node *kn;
  1031. if (!parent) {
  1032. WARN(1, KERN_WARNING "kernfs: can not remove '%s', no directory\n",
  1033. name);
  1034. return -ENOENT;
  1035. }
  1036. mutex_lock(&kernfs_mutex);
  1037. kn = kernfs_find_ns(parent, name, ns);
  1038. if (kn)
  1039. __kernfs_remove(kn);
  1040. mutex_unlock(&kernfs_mutex);
  1041. if (kn)
  1042. return 0;
  1043. else
  1044. return -ENOENT;
  1045. }
  1046. /**
  1047. * kernfs_rename_ns - move and rename a kernfs_node
  1048. * @kn: target node
  1049. * @new_parent: new parent to put @sd under
  1050. * @new_name: new name
  1051. * @new_ns: new namespace tag
  1052. */
  1053. int kernfs_rename_ns(struct kernfs_node *kn, struct kernfs_node *new_parent,
  1054. const char *new_name, const void *new_ns)
  1055. {
  1056. struct kernfs_node *old_parent;
  1057. const char *old_name = NULL;
  1058. int error;
  1059. /* can't move or rename root */
  1060. if (!kn->parent)
  1061. return -EINVAL;
  1062. mutex_lock(&kernfs_mutex);
  1063. error = -ENOENT;
  1064. if (!kernfs_active(kn) || !kernfs_active(new_parent))
  1065. goto out;
  1066. error = 0;
  1067. if ((kn->parent == new_parent) && (kn->ns == new_ns) &&
  1068. (strcmp(kn->name, new_name) == 0))
  1069. goto out; /* nothing to rename */
  1070. error = -EEXIST;
  1071. if (kernfs_find_ns(new_parent, new_name, new_ns))
  1072. goto out;
  1073. /* rename kernfs_node */
  1074. if (strcmp(kn->name, new_name) != 0) {
  1075. error = -ENOMEM;
  1076. new_name = kstrdup_const(new_name, GFP_KERNEL);
  1077. if (!new_name)
  1078. goto out;
  1079. } else {
  1080. new_name = NULL;
  1081. }
  1082. /*
  1083. * Move to the appropriate place in the appropriate directories rbtree.
  1084. */
  1085. kernfs_unlink_sibling(kn);
  1086. kernfs_get(new_parent);
  1087. /* rename_lock protects ->parent and ->name accessors */
  1088. spin_lock_irq(&kernfs_rename_lock);
  1089. old_parent = kn->parent;
  1090. kn->parent = new_parent;
  1091. kn->ns = new_ns;
  1092. if (new_name) {
  1093. old_name = kn->name;
  1094. kn->name = new_name;
  1095. }
  1096. spin_unlock_irq(&kernfs_rename_lock);
  1097. kn->hash = kernfs_name_hash(kn->name, kn->ns);
  1098. kernfs_link_sibling(kn);
  1099. kernfs_put(old_parent);
  1100. kfree_const(old_name);
  1101. error = 0;
  1102. out:
  1103. mutex_unlock(&kernfs_mutex);
  1104. return error;
  1105. }
  1106. /* Relationship between s_mode and the DT_xxx types */
  1107. static inline unsigned char dt_type(struct kernfs_node *kn)
  1108. {
  1109. return (kn->mode >> 12) & 15;
  1110. }
  1111. static int kernfs_dir_fop_release(struct inode *inode, struct file *filp)
  1112. {
  1113. kernfs_put(filp->private_data);
  1114. return 0;
  1115. }
  1116. static struct kernfs_node *kernfs_dir_pos(const void *ns,
  1117. struct kernfs_node *parent, loff_t hash, struct kernfs_node *pos)
  1118. {
  1119. if (pos) {
  1120. int valid = kernfs_active(pos) &&
  1121. pos->parent == parent && hash == pos->hash;
  1122. kernfs_put(pos);
  1123. if (!valid)
  1124. pos = NULL;
  1125. }
  1126. if (!pos && (hash > 1) && (hash < INT_MAX)) {
  1127. struct rb_node *node = parent->dir.children.rb_node;
  1128. while (node) {
  1129. pos = rb_to_kn(node);
  1130. if (hash < pos->hash)
  1131. node = node->rb_left;
  1132. else if (hash > pos->hash)
  1133. node = node->rb_right;
  1134. else
  1135. break;
  1136. }
  1137. }
  1138. /* Skip over entries which are dying/dead or in the wrong namespace */
  1139. while (pos && (!kernfs_active(pos) || pos->ns != ns)) {
  1140. struct rb_node *node = rb_next(&pos->rb);
  1141. if (!node)
  1142. pos = NULL;
  1143. else
  1144. pos = rb_to_kn(node);
  1145. }
  1146. return pos;
  1147. }
  1148. static struct kernfs_node *kernfs_dir_next_pos(const void *ns,
  1149. struct kernfs_node *parent, ino_t ino, struct kernfs_node *pos)
  1150. {
  1151. pos = kernfs_dir_pos(ns, parent, ino, pos);
  1152. if (pos) {
  1153. do {
  1154. struct rb_node *node = rb_next(&pos->rb);
  1155. if (!node)
  1156. pos = NULL;
  1157. else
  1158. pos = rb_to_kn(node);
  1159. } while (pos && (!kernfs_active(pos) || pos->ns != ns));
  1160. }
  1161. return pos;
  1162. }
  1163. static int kernfs_fop_readdir(struct file *file, struct dir_context *ctx)
  1164. {
  1165. struct dentry *dentry = file->f_path.dentry;
  1166. struct kernfs_node *parent = dentry->d_fsdata;
  1167. struct kernfs_node *pos = file->private_data;
  1168. const void *ns = NULL;
  1169. if (!dir_emit_dots(file, ctx))
  1170. return 0;
  1171. mutex_lock(&kernfs_mutex);
  1172. if (kernfs_ns_enabled(parent))
  1173. ns = kernfs_info(dentry->d_sb)->ns;
  1174. for (pos = kernfs_dir_pos(ns, parent, ctx->pos, pos);
  1175. pos;
  1176. pos = kernfs_dir_next_pos(ns, parent, ctx->pos, pos)) {
  1177. const char *name = pos->name;
  1178. unsigned int type = dt_type(pos);
  1179. int len = strlen(name);
  1180. ino_t ino = pos->ino;
  1181. ctx->pos = pos->hash;
  1182. file->private_data = pos;
  1183. kernfs_get(pos);
  1184. mutex_unlock(&kernfs_mutex);
  1185. if (!dir_emit(ctx, name, len, ino, type))
  1186. return 0;
  1187. mutex_lock(&kernfs_mutex);
  1188. }
  1189. mutex_unlock(&kernfs_mutex);
  1190. file->private_data = NULL;
  1191. ctx->pos = INT_MAX;
  1192. return 0;
  1193. }
  1194. static loff_t kernfs_dir_fop_llseek(struct file *file, loff_t offset,
  1195. int whence)
  1196. {
  1197. struct inode *inode = file_inode(file);
  1198. loff_t ret;
  1199. mutex_lock(&inode->i_mutex);
  1200. ret = generic_file_llseek(file, offset, whence);
  1201. mutex_unlock(&inode->i_mutex);
  1202. return ret;
  1203. }
  1204. const struct file_operations kernfs_dir_fops = {
  1205. .read = generic_read_dir,
  1206. .iterate = kernfs_fop_readdir,
  1207. .release = kernfs_dir_fop_release,
  1208. .llseek = kernfs_dir_fop_llseek,
  1209. };