dir.c 42 KB

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