dir.c 36 KB

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