dir.c 36 KB

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