dir.c 38 KB

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