dir.c 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679
  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. unsigned flags)
  533. {
  534. struct kernfs_node *kn;
  535. u32 gen;
  536. int cursor;
  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. cursor = idr_get_cursor(&root->ino_idr);
  547. ret = idr_alloc_cyclic(&root->ino_idr, kn, 1, 0, GFP_ATOMIC);
  548. if (ret >= 0 && ret < cursor)
  549. root->next_generation++;
  550. gen = root->next_generation;
  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 barrier is paired with atomic_inc_not_zero in
  559. * kernfs_find_and_get_node_by_ino
  560. */
  561. smp_mb__before_atomic();
  562. atomic_set(&kn->count, 1);
  563. atomic_set(&kn->active, KN_DEACTIVATED_BIAS);
  564. RB_CLEAR_NODE(&kn->rb);
  565. kn->name = name;
  566. kn->mode = mode;
  567. kn->flags = flags;
  568. return kn;
  569. err_out2:
  570. kmem_cache_free(kernfs_node_cache, kn);
  571. err_out1:
  572. kfree_const(name);
  573. return NULL;
  574. }
  575. struct kernfs_node *kernfs_new_node(struct kernfs_node *parent,
  576. const char *name, umode_t mode,
  577. unsigned flags)
  578. {
  579. struct kernfs_node *kn;
  580. kn = __kernfs_new_node(kernfs_root(parent), name, mode, flags);
  581. if (kn) {
  582. kernfs_get(parent);
  583. kn->parent = parent;
  584. }
  585. return kn;
  586. }
  587. /*
  588. * kernfs_find_and_get_node_by_ino - get kernfs_node from inode number
  589. * @root: the kernfs root
  590. * @ino: inode number
  591. *
  592. * RETURNS:
  593. * NULL on failure. Return a kernfs node with reference counter incremented
  594. */
  595. struct kernfs_node *kernfs_find_and_get_node_by_ino(struct kernfs_root *root,
  596. unsigned int ino)
  597. {
  598. struct kernfs_node *kn;
  599. rcu_read_lock();
  600. kn = idr_find(&root->ino_idr, ino);
  601. if (!kn)
  602. goto out;
  603. /*
  604. * Since kernfs_node is freed in RCU, it's possible an old node for ino
  605. * is freed, but reused before RCU grace period. But a freed node (see
  606. * kernfs_put) or an incompletedly initialized node (see
  607. * __kernfs_new_node) should have 'count' 0. We can use this fact to
  608. * filter out such node.
  609. */
  610. if (!atomic_inc_not_zero(&kn->count)) {
  611. kn = NULL;
  612. goto out;
  613. }
  614. /*
  615. * The node could be a new node or a reused node. If it's a new node,
  616. * we are ok. If it's reused because of RCU (because of
  617. * SLAB_TYPESAFE_BY_RCU), the __kernfs_new_node always sets its 'ino'
  618. * before 'count'. So if 'count' is uptodate, 'ino' should be uptodate,
  619. * hence we can use 'ino' to filter stale node.
  620. */
  621. if (kn->id.ino != ino)
  622. goto out;
  623. rcu_read_unlock();
  624. return kn;
  625. out:
  626. rcu_read_unlock();
  627. kernfs_put(kn);
  628. return NULL;
  629. }
  630. /**
  631. * kernfs_add_one - add kernfs_node to parent without warning
  632. * @kn: kernfs_node to be added
  633. *
  634. * The caller must already have initialized @kn->parent. This
  635. * function increments nlink of the parent's inode if @kn is a
  636. * directory and link into the children list of the parent.
  637. *
  638. * RETURNS:
  639. * 0 on success, -EEXIST if entry with the given name already
  640. * exists.
  641. */
  642. int kernfs_add_one(struct kernfs_node *kn)
  643. {
  644. struct kernfs_node *parent = kn->parent;
  645. struct kernfs_iattrs *ps_iattr;
  646. bool has_ns;
  647. int ret;
  648. mutex_lock(&kernfs_mutex);
  649. ret = -EINVAL;
  650. has_ns = kernfs_ns_enabled(parent);
  651. if (WARN(has_ns != (bool)kn->ns, KERN_WARNING "kernfs: ns %s in '%s' for '%s'\n",
  652. has_ns ? "required" : "invalid", parent->name, kn->name))
  653. goto out_unlock;
  654. if (kernfs_type(parent) != KERNFS_DIR)
  655. goto out_unlock;
  656. ret = -ENOENT;
  657. if (parent->flags & KERNFS_EMPTY_DIR)
  658. goto out_unlock;
  659. if ((parent->flags & KERNFS_ACTIVATED) && !kernfs_active(parent))
  660. goto out_unlock;
  661. kn->hash = kernfs_name_hash(kn->name, kn->ns);
  662. ret = kernfs_link_sibling(kn);
  663. if (ret)
  664. goto out_unlock;
  665. /* Update timestamps on the parent */
  666. ps_iattr = parent->iattr;
  667. if (ps_iattr) {
  668. struct iattr *ps_iattrs = &ps_iattr->ia_iattr;
  669. ktime_get_real_ts64(&ps_iattrs->ia_ctime);
  670. ps_iattrs->ia_mtime = ps_iattrs->ia_ctime;
  671. }
  672. mutex_unlock(&kernfs_mutex);
  673. /*
  674. * Activate the new node unless CREATE_DEACTIVATED is requested.
  675. * If not activated here, the kernfs user is responsible for
  676. * activating the node with kernfs_activate(). A node which hasn't
  677. * been activated is not visible to userland and its removal won't
  678. * trigger deactivation.
  679. */
  680. if (!(kernfs_root(kn)->flags & KERNFS_ROOT_CREATE_DEACTIVATED))
  681. kernfs_activate(kn);
  682. return 0;
  683. out_unlock:
  684. mutex_unlock(&kernfs_mutex);
  685. return ret;
  686. }
  687. /**
  688. * kernfs_find_ns - find kernfs_node with the given name
  689. * @parent: kernfs_node to search under
  690. * @name: name to look for
  691. * @ns: the namespace tag to use
  692. *
  693. * Look for kernfs_node with name @name under @parent. Returns pointer to
  694. * the found kernfs_node on success, %NULL on failure.
  695. */
  696. static struct kernfs_node *kernfs_find_ns(struct kernfs_node *parent,
  697. const unsigned char *name,
  698. const void *ns)
  699. {
  700. struct rb_node *node = parent->dir.children.rb_node;
  701. bool has_ns = kernfs_ns_enabled(parent);
  702. unsigned int hash;
  703. lockdep_assert_held(&kernfs_mutex);
  704. if (has_ns != (bool)ns) {
  705. WARN(1, KERN_WARNING "kernfs: ns %s in '%s' for '%s'\n",
  706. has_ns ? "required" : "invalid", parent->name, name);
  707. return NULL;
  708. }
  709. hash = kernfs_name_hash(name, ns);
  710. while (node) {
  711. struct kernfs_node *kn;
  712. int result;
  713. kn = rb_to_kn(node);
  714. result = kernfs_name_compare(hash, name, ns, kn);
  715. if (result < 0)
  716. node = node->rb_left;
  717. else if (result > 0)
  718. node = node->rb_right;
  719. else
  720. return kn;
  721. }
  722. return NULL;
  723. }
  724. static struct kernfs_node *kernfs_walk_ns(struct kernfs_node *parent,
  725. const unsigned char *path,
  726. const void *ns)
  727. {
  728. size_t len;
  729. char *p, *name;
  730. lockdep_assert_held(&kernfs_mutex);
  731. /* grab kernfs_rename_lock to piggy back on kernfs_pr_cont_buf */
  732. spin_lock_irq(&kernfs_rename_lock);
  733. len = strlcpy(kernfs_pr_cont_buf, path, sizeof(kernfs_pr_cont_buf));
  734. if (len >= sizeof(kernfs_pr_cont_buf)) {
  735. spin_unlock_irq(&kernfs_rename_lock);
  736. return NULL;
  737. }
  738. p = kernfs_pr_cont_buf;
  739. while ((name = strsep(&p, "/")) && parent) {
  740. if (*name == '\0')
  741. continue;
  742. parent = kernfs_find_ns(parent, name, ns);
  743. }
  744. spin_unlock_irq(&kernfs_rename_lock);
  745. return parent;
  746. }
  747. /**
  748. * kernfs_find_and_get_ns - find and get kernfs_node with the given name
  749. * @parent: kernfs_node to search under
  750. * @name: name to look for
  751. * @ns: the namespace tag to use
  752. *
  753. * Look for kernfs_node with name @name under @parent and get a reference
  754. * if found. This function may sleep and returns pointer to the found
  755. * kernfs_node on success, %NULL on failure.
  756. */
  757. struct kernfs_node *kernfs_find_and_get_ns(struct kernfs_node *parent,
  758. const char *name, const void *ns)
  759. {
  760. struct kernfs_node *kn;
  761. mutex_lock(&kernfs_mutex);
  762. kn = kernfs_find_ns(parent, name, ns);
  763. kernfs_get(kn);
  764. mutex_unlock(&kernfs_mutex);
  765. return kn;
  766. }
  767. EXPORT_SYMBOL_GPL(kernfs_find_and_get_ns);
  768. /**
  769. * kernfs_walk_and_get_ns - find and get kernfs_node with the given path
  770. * @parent: kernfs_node to search under
  771. * @path: path to look for
  772. * @ns: the namespace tag to use
  773. *
  774. * Look for kernfs_node with path @path under @parent and get a reference
  775. * if found. This function may sleep and returns pointer to the found
  776. * kernfs_node on success, %NULL on failure.
  777. */
  778. struct kernfs_node *kernfs_walk_and_get_ns(struct kernfs_node *parent,
  779. const char *path, const void *ns)
  780. {
  781. struct kernfs_node *kn;
  782. mutex_lock(&kernfs_mutex);
  783. kn = kernfs_walk_ns(parent, path, ns);
  784. kernfs_get(kn);
  785. mutex_unlock(&kernfs_mutex);
  786. return kn;
  787. }
  788. /**
  789. * kernfs_create_root - create a new kernfs hierarchy
  790. * @scops: optional syscall operations for the hierarchy
  791. * @flags: KERNFS_ROOT_* flags
  792. * @priv: opaque data associated with the new directory
  793. *
  794. * Returns the root of the new hierarchy on success, ERR_PTR() value on
  795. * failure.
  796. */
  797. struct kernfs_root *kernfs_create_root(struct kernfs_syscall_ops *scops,
  798. unsigned int flags, void *priv)
  799. {
  800. struct kernfs_root *root;
  801. struct kernfs_node *kn;
  802. root = kzalloc(sizeof(*root), GFP_KERNEL);
  803. if (!root)
  804. return ERR_PTR(-ENOMEM);
  805. idr_init(&root->ino_idr);
  806. INIT_LIST_HEAD(&root->supers);
  807. root->next_generation = 1;
  808. kn = __kernfs_new_node(root, "", S_IFDIR | S_IRUGO | S_IXUGO,
  809. KERNFS_DIR);
  810. if (!kn) {
  811. idr_destroy(&root->ino_idr);
  812. kfree(root);
  813. return ERR_PTR(-ENOMEM);
  814. }
  815. kn->priv = priv;
  816. kn->dir.root = root;
  817. root->syscall_ops = scops;
  818. root->flags = flags;
  819. root->kn = kn;
  820. init_waitqueue_head(&root->deactivate_waitq);
  821. if (!(root->flags & KERNFS_ROOT_CREATE_DEACTIVATED))
  822. kernfs_activate(kn);
  823. return root;
  824. }
  825. /**
  826. * kernfs_destroy_root - destroy a kernfs hierarchy
  827. * @root: root of the hierarchy to destroy
  828. *
  829. * Destroy the hierarchy anchored at @root by removing all existing
  830. * directories and destroying @root.
  831. */
  832. void kernfs_destroy_root(struct kernfs_root *root)
  833. {
  834. kernfs_remove(root->kn); /* will also free @root */
  835. }
  836. /**
  837. * kernfs_create_dir_ns - create a directory
  838. * @parent: parent in which to create a new directory
  839. * @name: name of the new directory
  840. * @mode: mode of the new directory
  841. * @priv: opaque data associated with the new directory
  842. * @ns: optional namespace tag of the directory
  843. *
  844. * Returns the created node on success, ERR_PTR() value on failure.
  845. */
  846. struct kernfs_node *kernfs_create_dir_ns(struct kernfs_node *parent,
  847. const char *name, umode_t mode,
  848. void *priv, const void *ns)
  849. {
  850. struct kernfs_node *kn;
  851. int rc;
  852. /* allocate */
  853. kn = kernfs_new_node(parent, name, mode | S_IFDIR, KERNFS_DIR);
  854. if (!kn)
  855. return ERR_PTR(-ENOMEM);
  856. kn->dir.root = parent->dir.root;
  857. kn->ns = ns;
  858. kn->priv = priv;
  859. /* link in */
  860. rc = kernfs_add_one(kn);
  861. if (!rc)
  862. return kn;
  863. kernfs_put(kn);
  864. return ERR_PTR(rc);
  865. }
  866. /**
  867. * kernfs_create_empty_dir - create an always empty directory
  868. * @parent: parent in which to create a new directory
  869. * @name: name of the new directory
  870. *
  871. * Returns the created node on success, ERR_PTR() value on failure.
  872. */
  873. struct kernfs_node *kernfs_create_empty_dir(struct kernfs_node *parent,
  874. const char *name)
  875. {
  876. struct kernfs_node *kn;
  877. int rc;
  878. /* allocate */
  879. kn = kernfs_new_node(parent, name, S_IRUGO|S_IXUGO|S_IFDIR, KERNFS_DIR);
  880. if (!kn)
  881. return ERR_PTR(-ENOMEM);
  882. kn->flags |= KERNFS_EMPTY_DIR;
  883. kn->dir.root = parent->dir.root;
  884. kn->ns = NULL;
  885. kn->priv = NULL;
  886. /* link in */
  887. rc = kernfs_add_one(kn);
  888. if (!rc)
  889. return kn;
  890. kernfs_put(kn);
  891. return ERR_PTR(rc);
  892. }
  893. static struct dentry *kernfs_iop_lookup(struct inode *dir,
  894. struct dentry *dentry,
  895. unsigned int flags)
  896. {
  897. struct dentry *ret;
  898. struct kernfs_node *parent = dir->i_private;
  899. struct kernfs_node *kn;
  900. struct inode *inode;
  901. const void *ns = NULL;
  902. mutex_lock(&kernfs_mutex);
  903. if (kernfs_ns_enabled(parent))
  904. ns = kernfs_info(dir->i_sb)->ns;
  905. kn = kernfs_find_ns(parent, dentry->d_name.name, ns);
  906. /* no such entry */
  907. if (!kn || !kernfs_active(kn)) {
  908. ret = NULL;
  909. goto out_unlock;
  910. }
  911. /* attach dentry and inode */
  912. inode = kernfs_get_inode(dir->i_sb, kn);
  913. if (!inode) {
  914. ret = ERR_PTR(-ENOMEM);
  915. goto out_unlock;
  916. }
  917. /* instantiate and hash dentry */
  918. ret = d_splice_alias(inode, dentry);
  919. out_unlock:
  920. mutex_unlock(&kernfs_mutex);
  921. return ret;
  922. }
  923. static int kernfs_iop_mkdir(struct inode *dir, struct dentry *dentry,
  924. umode_t mode)
  925. {
  926. struct kernfs_node *parent = dir->i_private;
  927. struct kernfs_syscall_ops *scops = kernfs_root(parent)->syscall_ops;
  928. int ret;
  929. if (!scops || !scops->mkdir)
  930. return -EPERM;
  931. if (!kernfs_get_active(parent))
  932. return -ENODEV;
  933. ret = scops->mkdir(parent, dentry->d_name.name, mode);
  934. kernfs_put_active(parent);
  935. return ret;
  936. }
  937. static int kernfs_iop_rmdir(struct inode *dir, struct dentry *dentry)
  938. {
  939. struct kernfs_node *kn = kernfs_dentry_node(dentry);
  940. struct kernfs_syscall_ops *scops = kernfs_root(kn)->syscall_ops;
  941. int ret;
  942. if (!scops || !scops->rmdir)
  943. return -EPERM;
  944. if (!kernfs_get_active(kn))
  945. return -ENODEV;
  946. ret = scops->rmdir(kn);
  947. kernfs_put_active(kn);
  948. return ret;
  949. }
  950. static int kernfs_iop_rename(struct inode *old_dir, struct dentry *old_dentry,
  951. struct inode *new_dir, struct dentry *new_dentry,
  952. unsigned int flags)
  953. {
  954. struct kernfs_node *kn = kernfs_dentry_node(old_dentry);
  955. struct kernfs_node *new_parent = new_dir->i_private;
  956. struct kernfs_syscall_ops *scops = kernfs_root(kn)->syscall_ops;
  957. int ret;
  958. if (flags)
  959. return -EINVAL;
  960. if (!scops || !scops->rename)
  961. return -EPERM;
  962. if (!kernfs_get_active(kn))
  963. return -ENODEV;
  964. if (!kernfs_get_active(new_parent)) {
  965. kernfs_put_active(kn);
  966. return -ENODEV;
  967. }
  968. ret = scops->rename(kn, new_parent, new_dentry->d_name.name);
  969. kernfs_put_active(new_parent);
  970. kernfs_put_active(kn);
  971. return ret;
  972. }
  973. const struct inode_operations kernfs_dir_iops = {
  974. .lookup = kernfs_iop_lookup,
  975. .permission = kernfs_iop_permission,
  976. .setattr = kernfs_iop_setattr,
  977. .getattr = kernfs_iop_getattr,
  978. .listxattr = kernfs_iop_listxattr,
  979. .mkdir = kernfs_iop_mkdir,
  980. .rmdir = kernfs_iop_rmdir,
  981. .rename = kernfs_iop_rename,
  982. };
  983. static struct kernfs_node *kernfs_leftmost_descendant(struct kernfs_node *pos)
  984. {
  985. struct kernfs_node *last;
  986. while (true) {
  987. struct rb_node *rbn;
  988. last = pos;
  989. if (kernfs_type(pos) != KERNFS_DIR)
  990. break;
  991. rbn = rb_first(&pos->dir.children);
  992. if (!rbn)
  993. break;
  994. pos = rb_to_kn(rbn);
  995. }
  996. return last;
  997. }
  998. /**
  999. * kernfs_next_descendant_post - find the next descendant for post-order walk
  1000. * @pos: the current position (%NULL to initiate traversal)
  1001. * @root: kernfs_node whose descendants to walk
  1002. *
  1003. * Find the next descendant to visit for post-order traversal of @root's
  1004. * descendants. @root is included in the iteration and the last node to be
  1005. * visited.
  1006. */
  1007. static struct kernfs_node *kernfs_next_descendant_post(struct kernfs_node *pos,
  1008. struct kernfs_node *root)
  1009. {
  1010. struct rb_node *rbn;
  1011. lockdep_assert_held(&kernfs_mutex);
  1012. /* if first iteration, visit leftmost descendant which may be root */
  1013. if (!pos)
  1014. return kernfs_leftmost_descendant(root);
  1015. /* if we visited @root, we're done */
  1016. if (pos == root)
  1017. return NULL;
  1018. /* if there's an unvisited sibling, visit its leftmost descendant */
  1019. rbn = rb_next(&pos->rb);
  1020. if (rbn)
  1021. return kernfs_leftmost_descendant(rb_to_kn(rbn));
  1022. /* no sibling left, visit parent */
  1023. return pos->parent;
  1024. }
  1025. /**
  1026. * kernfs_activate - activate a node which started deactivated
  1027. * @kn: kernfs_node whose subtree is to be activated
  1028. *
  1029. * If the root has KERNFS_ROOT_CREATE_DEACTIVATED set, a newly created node
  1030. * needs to be explicitly activated. A node which hasn't been activated
  1031. * isn't visible to userland and deactivation is skipped during its
  1032. * removal. This is useful to construct atomic init sequences where
  1033. * creation of multiple nodes should either succeed or fail atomically.
  1034. *
  1035. * The caller is responsible for ensuring that this function is not called
  1036. * after kernfs_remove*() is invoked on @kn.
  1037. */
  1038. void kernfs_activate(struct kernfs_node *kn)
  1039. {
  1040. struct kernfs_node *pos;
  1041. mutex_lock(&kernfs_mutex);
  1042. pos = NULL;
  1043. while ((pos = kernfs_next_descendant_post(pos, kn))) {
  1044. if (!pos || (pos->flags & KERNFS_ACTIVATED))
  1045. continue;
  1046. WARN_ON_ONCE(pos->parent && RB_EMPTY_NODE(&pos->rb));
  1047. WARN_ON_ONCE(atomic_read(&pos->active) != KN_DEACTIVATED_BIAS);
  1048. atomic_sub(KN_DEACTIVATED_BIAS, &pos->active);
  1049. pos->flags |= KERNFS_ACTIVATED;
  1050. }
  1051. mutex_unlock(&kernfs_mutex);
  1052. }
  1053. static void __kernfs_remove(struct kernfs_node *kn)
  1054. {
  1055. struct kernfs_node *pos;
  1056. lockdep_assert_held(&kernfs_mutex);
  1057. /*
  1058. * Short-circuit if non-root @kn has already finished removal.
  1059. * This is for kernfs_remove_self() which plays with active ref
  1060. * after removal.
  1061. */
  1062. if (!kn || (kn->parent && RB_EMPTY_NODE(&kn->rb)))
  1063. return;
  1064. pr_debug("kernfs %s: removing\n", kn->name);
  1065. /* prevent any new usage under @kn by deactivating all nodes */
  1066. pos = NULL;
  1067. while ((pos = kernfs_next_descendant_post(pos, kn)))
  1068. if (kernfs_active(pos))
  1069. atomic_add(KN_DEACTIVATED_BIAS, &pos->active);
  1070. /* deactivate and unlink the subtree node-by-node */
  1071. do {
  1072. pos = kernfs_leftmost_descendant(kn);
  1073. /*
  1074. * kernfs_drain() drops kernfs_mutex temporarily and @pos's
  1075. * base ref could have been put by someone else by the time
  1076. * the function returns. Make sure it doesn't go away
  1077. * underneath us.
  1078. */
  1079. kernfs_get(pos);
  1080. /*
  1081. * Drain iff @kn was activated. This avoids draining and
  1082. * its lockdep annotations for nodes which have never been
  1083. * activated and allows embedding kernfs_remove() in create
  1084. * error paths without worrying about draining.
  1085. */
  1086. if (kn->flags & KERNFS_ACTIVATED)
  1087. kernfs_drain(pos);
  1088. else
  1089. WARN_ON_ONCE(atomic_read(&kn->active) != KN_DEACTIVATED_BIAS);
  1090. /*
  1091. * kernfs_unlink_sibling() succeeds once per node. Use it
  1092. * to decide who's responsible for cleanups.
  1093. */
  1094. if (!pos->parent || kernfs_unlink_sibling(pos)) {
  1095. struct kernfs_iattrs *ps_iattr =
  1096. pos->parent ? pos->parent->iattr : NULL;
  1097. /* update timestamps on the parent */
  1098. if (ps_iattr) {
  1099. ktime_get_real_ts64(&ps_iattr->ia_iattr.ia_ctime);
  1100. ps_iattr->ia_iattr.ia_mtime =
  1101. ps_iattr->ia_iattr.ia_ctime;
  1102. }
  1103. kernfs_put(pos);
  1104. }
  1105. kernfs_put(pos);
  1106. } while (pos != kn);
  1107. }
  1108. /**
  1109. * kernfs_remove - remove a kernfs_node recursively
  1110. * @kn: the kernfs_node to remove
  1111. *
  1112. * Remove @kn along with all its subdirectories and files.
  1113. */
  1114. void kernfs_remove(struct kernfs_node *kn)
  1115. {
  1116. mutex_lock(&kernfs_mutex);
  1117. __kernfs_remove(kn);
  1118. mutex_unlock(&kernfs_mutex);
  1119. }
  1120. /**
  1121. * kernfs_break_active_protection - break out of active protection
  1122. * @kn: the self kernfs_node
  1123. *
  1124. * The caller must be running off of a kernfs operation which is invoked
  1125. * with an active reference - e.g. one of kernfs_ops. Each invocation of
  1126. * this function must also be matched with an invocation of
  1127. * kernfs_unbreak_active_protection().
  1128. *
  1129. * This function releases the active reference of @kn the caller is
  1130. * holding. Once this function is called, @kn may be removed at any point
  1131. * and the caller is solely responsible for ensuring that the objects it
  1132. * dereferences are accessible.
  1133. */
  1134. void kernfs_break_active_protection(struct kernfs_node *kn)
  1135. {
  1136. /*
  1137. * Take out ourself out of the active ref dependency chain. If
  1138. * we're called without an active ref, lockdep will complain.
  1139. */
  1140. kernfs_put_active(kn);
  1141. }
  1142. /**
  1143. * kernfs_unbreak_active_protection - undo kernfs_break_active_protection()
  1144. * @kn: the self kernfs_node
  1145. *
  1146. * If kernfs_break_active_protection() was called, this function must be
  1147. * invoked before finishing the kernfs operation. Note that while this
  1148. * function restores the active reference, it doesn't and can't actually
  1149. * restore the active protection - @kn may already or be in the process of
  1150. * being removed. Once kernfs_break_active_protection() is invoked, that
  1151. * protection is irreversibly gone for the kernfs operation instance.
  1152. *
  1153. * While this function may be called at any point after
  1154. * kernfs_break_active_protection() is invoked, its most useful location
  1155. * would be right before the enclosing kernfs operation returns.
  1156. */
  1157. void kernfs_unbreak_active_protection(struct kernfs_node *kn)
  1158. {
  1159. /*
  1160. * @kn->active could be in any state; however, the increment we do
  1161. * here will be undone as soon as the enclosing kernfs operation
  1162. * finishes and this temporary bump can't break anything. If @kn
  1163. * is alive, nothing changes. If @kn is being deactivated, the
  1164. * soon-to-follow put will either finish deactivation or restore
  1165. * deactivated state. If @kn is already removed, the temporary
  1166. * bump is guaranteed to be gone before @kn is released.
  1167. */
  1168. atomic_inc(&kn->active);
  1169. if (kernfs_lockdep(kn))
  1170. rwsem_acquire(&kn->dep_map, 0, 1, _RET_IP_);
  1171. }
  1172. /**
  1173. * kernfs_remove_self - remove a kernfs_node from its own method
  1174. * @kn: the self kernfs_node to remove
  1175. *
  1176. * The caller must be running off of a kernfs operation which is invoked
  1177. * with an active reference - e.g. one of kernfs_ops. This can be used to
  1178. * implement a file operation which deletes itself.
  1179. *
  1180. * For example, the "delete" file for a sysfs device directory can be
  1181. * implemented by invoking kernfs_remove_self() on the "delete" file
  1182. * itself. This function breaks the circular dependency of trying to
  1183. * deactivate self while holding an active ref itself. It isn't necessary
  1184. * to modify the usual removal path to use kernfs_remove_self(). The
  1185. * "delete" implementation can simply invoke kernfs_remove_self() on self
  1186. * before proceeding with the usual removal path. kernfs will ignore later
  1187. * kernfs_remove() on self.
  1188. *
  1189. * kernfs_remove_self() can be called multiple times concurrently on the
  1190. * same kernfs_node. Only the first one actually performs removal and
  1191. * returns %true. All others will wait until the kernfs operation which
  1192. * won self-removal finishes and return %false. Note that the losers wait
  1193. * for the completion of not only the winning kernfs_remove_self() but also
  1194. * the whole kernfs_ops which won the arbitration. This can be used to
  1195. * guarantee, for example, all concurrent writes to a "delete" file to
  1196. * finish only after the whole operation is complete.
  1197. */
  1198. bool kernfs_remove_self(struct kernfs_node *kn)
  1199. {
  1200. bool ret;
  1201. mutex_lock(&kernfs_mutex);
  1202. kernfs_break_active_protection(kn);
  1203. /*
  1204. * SUICIDAL is used to arbitrate among competing invocations. Only
  1205. * the first one will actually perform removal. When the removal
  1206. * is complete, SUICIDED is set and the active ref is restored
  1207. * while holding kernfs_mutex. The ones which lost arbitration
  1208. * waits for SUICDED && drained which can happen only after the
  1209. * enclosing kernfs operation which executed the winning instance
  1210. * of kernfs_remove_self() finished.
  1211. */
  1212. if (!(kn->flags & KERNFS_SUICIDAL)) {
  1213. kn->flags |= KERNFS_SUICIDAL;
  1214. __kernfs_remove(kn);
  1215. kn->flags |= KERNFS_SUICIDED;
  1216. ret = true;
  1217. } else {
  1218. wait_queue_head_t *waitq = &kernfs_root(kn)->deactivate_waitq;
  1219. DEFINE_WAIT(wait);
  1220. while (true) {
  1221. prepare_to_wait(waitq, &wait, TASK_UNINTERRUPTIBLE);
  1222. if ((kn->flags & KERNFS_SUICIDED) &&
  1223. atomic_read(&kn->active) == KN_DEACTIVATED_BIAS)
  1224. break;
  1225. mutex_unlock(&kernfs_mutex);
  1226. schedule();
  1227. mutex_lock(&kernfs_mutex);
  1228. }
  1229. finish_wait(waitq, &wait);
  1230. WARN_ON_ONCE(!RB_EMPTY_NODE(&kn->rb));
  1231. ret = false;
  1232. }
  1233. /*
  1234. * This must be done while holding kernfs_mutex; otherwise, waiting
  1235. * for SUICIDED && deactivated could finish prematurely.
  1236. */
  1237. kernfs_unbreak_active_protection(kn);
  1238. mutex_unlock(&kernfs_mutex);
  1239. return ret;
  1240. }
  1241. /**
  1242. * kernfs_remove_by_name_ns - find a kernfs_node by name and remove it
  1243. * @parent: parent of the target
  1244. * @name: name of the kernfs_node to remove
  1245. * @ns: namespace tag of the kernfs_node to remove
  1246. *
  1247. * Look for the kernfs_node with @name and @ns under @parent and remove it.
  1248. * Returns 0 on success, -ENOENT if such entry doesn't exist.
  1249. */
  1250. int kernfs_remove_by_name_ns(struct kernfs_node *parent, const char *name,
  1251. const void *ns)
  1252. {
  1253. struct kernfs_node *kn;
  1254. if (!parent) {
  1255. WARN(1, KERN_WARNING "kernfs: can not remove '%s', no directory\n",
  1256. name);
  1257. return -ENOENT;
  1258. }
  1259. mutex_lock(&kernfs_mutex);
  1260. kn = kernfs_find_ns(parent, name, ns);
  1261. if (kn)
  1262. __kernfs_remove(kn);
  1263. mutex_unlock(&kernfs_mutex);
  1264. if (kn)
  1265. return 0;
  1266. else
  1267. return -ENOENT;
  1268. }
  1269. /**
  1270. * kernfs_rename_ns - move and rename a kernfs_node
  1271. * @kn: target node
  1272. * @new_parent: new parent to put @sd under
  1273. * @new_name: new name
  1274. * @new_ns: new namespace tag
  1275. */
  1276. int kernfs_rename_ns(struct kernfs_node *kn, struct kernfs_node *new_parent,
  1277. const char *new_name, const void *new_ns)
  1278. {
  1279. struct kernfs_node *old_parent;
  1280. const char *old_name = NULL;
  1281. int error;
  1282. /* can't move or rename root */
  1283. if (!kn->parent)
  1284. return -EINVAL;
  1285. mutex_lock(&kernfs_mutex);
  1286. error = -ENOENT;
  1287. if (!kernfs_active(kn) || !kernfs_active(new_parent) ||
  1288. (new_parent->flags & KERNFS_EMPTY_DIR))
  1289. goto out;
  1290. error = 0;
  1291. if ((kn->parent == new_parent) && (kn->ns == new_ns) &&
  1292. (strcmp(kn->name, new_name) == 0))
  1293. goto out; /* nothing to rename */
  1294. error = -EEXIST;
  1295. if (kernfs_find_ns(new_parent, new_name, new_ns))
  1296. goto out;
  1297. /* rename kernfs_node */
  1298. if (strcmp(kn->name, new_name) != 0) {
  1299. error = -ENOMEM;
  1300. new_name = kstrdup_const(new_name, GFP_KERNEL);
  1301. if (!new_name)
  1302. goto out;
  1303. } else {
  1304. new_name = NULL;
  1305. }
  1306. /*
  1307. * Move to the appropriate place in the appropriate directories rbtree.
  1308. */
  1309. kernfs_unlink_sibling(kn);
  1310. kernfs_get(new_parent);
  1311. /* rename_lock protects ->parent and ->name accessors */
  1312. spin_lock_irq(&kernfs_rename_lock);
  1313. old_parent = kn->parent;
  1314. kn->parent = new_parent;
  1315. kn->ns = new_ns;
  1316. if (new_name) {
  1317. old_name = kn->name;
  1318. kn->name = new_name;
  1319. }
  1320. spin_unlock_irq(&kernfs_rename_lock);
  1321. kn->hash = kernfs_name_hash(kn->name, kn->ns);
  1322. kernfs_link_sibling(kn);
  1323. kernfs_put(old_parent);
  1324. kfree_const(old_name);
  1325. error = 0;
  1326. out:
  1327. mutex_unlock(&kernfs_mutex);
  1328. return error;
  1329. }
  1330. /* Relationship between s_mode and the DT_xxx types */
  1331. static inline unsigned char dt_type(struct kernfs_node *kn)
  1332. {
  1333. return (kn->mode >> 12) & 15;
  1334. }
  1335. static int kernfs_dir_fop_release(struct inode *inode, struct file *filp)
  1336. {
  1337. kernfs_put(filp->private_data);
  1338. return 0;
  1339. }
  1340. static struct kernfs_node *kernfs_dir_pos(const void *ns,
  1341. struct kernfs_node *parent, loff_t hash, struct kernfs_node *pos)
  1342. {
  1343. if (pos) {
  1344. int valid = kernfs_active(pos) &&
  1345. pos->parent == parent && hash == pos->hash;
  1346. kernfs_put(pos);
  1347. if (!valid)
  1348. pos = NULL;
  1349. }
  1350. if (!pos && (hash > 1) && (hash < INT_MAX)) {
  1351. struct rb_node *node = parent->dir.children.rb_node;
  1352. while (node) {
  1353. pos = rb_to_kn(node);
  1354. if (hash < pos->hash)
  1355. node = node->rb_left;
  1356. else if (hash > pos->hash)
  1357. node = node->rb_right;
  1358. else
  1359. break;
  1360. }
  1361. }
  1362. /* Skip over entries which are dying/dead or in the wrong namespace */
  1363. while (pos && (!kernfs_active(pos) || pos->ns != ns)) {
  1364. struct rb_node *node = rb_next(&pos->rb);
  1365. if (!node)
  1366. pos = NULL;
  1367. else
  1368. pos = rb_to_kn(node);
  1369. }
  1370. return pos;
  1371. }
  1372. static struct kernfs_node *kernfs_dir_next_pos(const void *ns,
  1373. struct kernfs_node *parent, ino_t ino, struct kernfs_node *pos)
  1374. {
  1375. pos = kernfs_dir_pos(ns, parent, ino, pos);
  1376. if (pos) {
  1377. do {
  1378. struct rb_node *node = rb_next(&pos->rb);
  1379. if (!node)
  1380. pos = NULL;
  1381. else
  1382. pos = rb_to_kn(node);
  1383. } while (pos && (!kernfs_active(pos) || pos->ns != ns));
  1384. }
  1385. return pos;
  1386. }
  1387. static int kernfs_fop_readdir(struct file *file, struct dir_context *ctx)
  1388. {
  1389. struct dentry *dentry = file->f_path.dentry;
  1390. struct kernfs_node *parent = kernfs_dentry_node(dentry);
  1391. struct kernfs_node *pos = file->private_data;
  1392. const void *ns = NULL;
  1393. if (!dir_emit_dots(file, ctx))
  1394. return 0;
  1395. mutex_lock(&kernfs_mutex);
  1396. if (kernfs_ns_enabled(parent))
  1397. ns = kernfs_info(dentry->d_sb)->ns;
  1398. for (pos = kernfs_dir_pos(ns, parent, ctx->pos, pos);
  1399. pos;
  1400. pos = kernfs_dir_next_pos(ns, parent, ctx->pos, pos)) {
  1401. const char *name = pos->name;
  1402. unsigned int type = dt_type(pos);
  1403. int len = strlen(name);
  1404. ino_t ino = pos->id.ino;
  1405. ctx->pos = pos->hash;
  1406. file->private_data = pos;
  1407. kernfs_get(pos);
  1408. mutex_unlock(&kernfs_mutex);
  1409. if (!dir_emit(ctx, name, len, ino, type))
  1410. return 0;
  1411. mutex_lock(&kernfs_mutex);
  1412. }
  1413. mutex_unlock(&kernfs_mutex);
  1414. file->private_data = NULL;
  1415. ctx->pos = INT_MAX;
  1416. return 0;
  1417. }
  1418. const struct file_operations kernfs_dir_fops = {
  1419. .read = generic_read_dir,
  1420. .iterate_shared = kernfs_fop_readdir,
  1421. .release = kernfs_dir_fop_release,
  1422. .llseek = generic_file_llseek,
  1423. };