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