dir.c 42 KB

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