dir.c 36 KB

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