dir.c 40 KB

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