dir.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487
  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. /*
  463. * If the ino of the sysfs entry created for a kmem cache gets
  464. * allocated from an ida layer, which is accounted to the memcg that
  465. * owns the cache, the memcg will get pinned forever. So do not account
  466. * ino ida allocations.
  467. */
  468. ret = ida_simple_get(&root->ino_ida, 1, 0,
  469. GFP_KERNEL | __GFP_NOACCOUNT);
  470. if (ret < 0)
  471. goto err_out2;
  472. kn->ino = ret;
  473. atomic_set(&kn->count, 1);
  474. atomic_set(&kn->active, KN_DEACTIVATED_BIAS);
  475. RB_CLEAR_NODE(&kn->rb);
  476. kn->name = name;
  477. kn->mode = mode;
  478. kn->flags = flags;
  479. return kn;
  480. err_out2:
  481. kmem_cache_free(kernfs_node_cache, kn);
  482. err_out1:
  483. kfree_const(name);
  484. return NULL;
  485. }
  486. struct kernfs_node *kernfs_new_node(struct kernfs_node *parent,
  487. const char *name, umode_t mode,
  488. unsigned flags)
  489. {
  490. struct kernfs_node *kn;
  491. kn = __kernfs_new_node(kernfs_root(parent), name, mode, flags);
  492. if (kn) {
  493. kernfs_get(parent);
  494. kn->parent = parent;
  495. }
  496. return kn;
  497. }
  498. /**
  499. * kernfs_add_one - add kernfs_node to parent without warning
  500. * @kn: kernfs_node to be added
  501. *
  502. * The caller must already have initialized @kn->parent. This
  503. * function increments nlink of the parent's inode if @kn is a
  504. * directory and link into the children list of the parent.
  505. *
  506. * RETURNS:
  507. * 0 on success, -EEXIST if entry with the given name already
  508. * exists.
  509. */
  510. int kernfs_add_one(struct kernfs_node *kn)
  511. {
  512. struct kernfs_node *parent = kn->parent;
  513. struct kernfs_iattrs *ps_iattr;
  514. bool has_ns;
  515. int ret;
  516. mutex_lock(&kernfs_mutex);
  517. ret = -EINVAL;
  518. has_ns = kernfs_ns_enabled(parent);
  519. if (WARN(has_ns != (bool)kn->ns, KERN_WARNING "kernfs: ns %s in '%s' for '%s'\n",
  520. has_ns ? "required" : "invalid", parent->name, kn->name))
  521. goto out_unlock;
  522. if (kernfs_type(parent) != KERNFS_DIR)
  523. goto out_unlock;
  524. ret = -ENOENT;
  525. if (parent->flags & KERNFS_EMPTY_DIR)
  526. goto out_unlock;
  527. if ((parent->flags & KERNFS_ACTIVATED) && !kernfs_active(parent))
  528. goto out_unlock;
  529. kn->hash = kernfs_name_hash(kn->name, kn->ns);
  530. ret = kernfs_link_sibling(kn);
  531. if (ret)
  532. goto out_unlock;
  533. /* Update timestamps on the parent */
  534. ps_iattr = parent->iattr;
  535. if (ps_iattr) {
  536. struct iattr *ps_iattrs = &ps_iattr->ia_iattr;
  537. ps_iattrs->ia_ctime = ps_iattrs->ia_mtime = CURRENT_TIME;
  538. }
  539. mutex_unlock(&kernfs_mutex);
  540. /*
  541. * Activate the new node unless CREATE_DEACTIVATED is requested.
  542. * If not activated here, the kernfs user is responsible for
  543. * activating the node with kernfs_activate(). A node which hasn't
  544. * been activated is not visible to userland and its removal won't
  545. * trigger deactivation.
  546. */
  547. if (!(kernfs_root(kn)->flags & KERNFS_ROOT_CREATE_DEACTIVATED))
  548. kernfs_activate(kn);
  549. return 0;
  550. out_unlock:
  551. mutex_unlock(&kernfs_mutex);
  552. return ret;
  553. }
  554. /**
  555. * kernfs_find_ns - find kernfs_node with the given name
  556. * @parent: kernfs_node to search under
  557. * @name: name to look for
  558. * @ns: the namespace tag to use
  559. *
  560. * Look for kernfs_node with name @name under @parent. Returns pointer to
  561. * the found kernfs_node on success, %NULL on failure.
  562. */
  563. static struct kernfs_node *kernfs_find_ns(struct kernfs_node *parent,
  564. const unsigned char *name,
  565. const void *ns)
  566. {
  567. struct rb_node *node = parent->dir.children.rb_node;
  568. bool has_ns = kernfs_ns_enabled(parent);
  569. unsigned int hash;
  570. lockdep_assert_held(&kernfs_mutex);
  571. if (has_ns != (bool)ns) {
  572. WARN(1, KERN_WARNING "kernfs: ns %s in '%s' for '%s'\n",
  573. has_ns ? "required" : "invalid", parent->name, name);
  574. return NULL;
  575. }
  576. hash = kernfs_name_hash(name, ns);
  577. while (node) {
  578. struct kernfs_node *kn;
  579. int result;
  580. kn = rb_to_kn(node);
  581. result = kernfs_name_compare(hash, name, ns, kn);
  582. if (result < 0)
  583. node = node->rb_left;
  584. else if (result > 0)
  585. node = node->rb_right;
  586. else
  587. return kn;
  588. }
  589. return NULL;
  590. }
  591. /**
  592. * kernfs_find_and_get_ns - find and get kernfs_node with the given name
  593. * @parent: kernfs_node to search under
  594. * @name: name to look for
  595. * @ns: the namespace tag to use
  596. *
  597. * Look for kernfs_node with name @name under @parent and get a reference
  598. * if found. This function may sleep and returns pointer to the found
  599. * kernfs_node on success, %NULL on failure.
  600. */
  601. struct kernfs_node *kernfs_find_and_get_ns(struct kernfs_node *parent,
  602. const char *name, const void *ns)
  603. {
  604. struct kernfs_node *kn;
  605. mutex_lock(&kernfs_mutex);
  606. kn = kernfs_find_ns(parent, name, ns);
  607. kernfs_get(kn);
  608. mutex_unlock(&kernfs_mutex);
  609. return kn;
  610. }
  611. EXPORT_SYMBOL_GPL(kernfs_find_and_get_ns);
  612. /**
  613. * kernfs_create_root - create a new kernfs hierarchy
  614. * @scops: optional syscall operations for the hierarchy
  615. * @flags: KERNFS_ROOT_* flags
  616. * @priv: opaque data associated with the new directory
  617. *
  618. * Returns the root of the new hierarchy on success, ERR_PTR() value on
  619. * failure.
  620. */
  621. struct kernfs_root *kernfs_create_root(struct kernfs_syscall_ops *scops,
  622. unsigned int flags, void *priv)
  623. {
  624. struct kernfs_root *root;
  625. struct kernfs_node *kn;
  626. root = kzalloc(sizeof(*root), GFP_KERNEL);
  627. if (!root)
  628. return ERR_PTR(-ENOMEM);
  629. ida_init(&root->ino_ida);
  630. INIT_LIST_HEAD(&root->supers);
  631. kn = __kernfs_new_node(root, "", S_IFDIR | S_IRUGO | S_IXUGO,
  632. KERNFS_DIR);
  633. if (!kn) {
  634. ida_destroy(&root->ino_ida);
  635. kfree(root);
  636. return ERR_PTR(-ENOMEM);
  637. }
  638. kn->priv = priv;
  639. kn->dir.root = root;
  640. root->syscall_ops = scops;
  641. root->flags = flags;
  642. root->kn = kn;
  643. init_waitqueue_head(&root->deactivate_waitq);
  644. if (!(root->flags & KERNFS_ROOT_CREATE_DEACTIVATED))
  645. kernfs_activate(kn);
  646. return root;
  647. }
  648. /**
  649. * kernfs_destroy_root - destroy a kernfs hierarchy
  650. * @root: root of the hierarchy to destroy
  651. *
  652. * Destroy the hierarchy anchored at @root by removing all existing
  653. * directories and destroying @root.
  654. */
  655. void kernfs_destroy_root(struct kernfs_root *root)
  656. {
  657. kernfs_remove(root->kn); /* will also free @root */
  658. }
  659. /**
  660. * kernfs_create_dir_ns - create a directory
  661. * @parent: parent in which to create a new directory
  662. * @name: name of the new directory
  663. * @mode: mode of the new directory
  664. * @priv: opaque data associated with the new directory
  665. * @ns: optional namespace tag of the directory
  666. *
  667. * Returns the created node on success, ERR_PTR() value on failure.
  668. */
  669. struct kernfs_node *kernfs_create_dir_ns(struct kernfs_node *parent,
  670. const char *name, umode_t mode,
  671. void *priv, const void *ns)
  672. {
  673. struct kernfs_node *kn;
  674. int rc;
  675. /* allocate */
  676. kn = kernfs_new_node(parent, name, mode | S_IFDIR, KERNFS_DIR);
  677. if (!kn)
  678. return ERR_PTR(-ENOMEM);
  679. kn->dir.root = parent->dir.root;
  680. kn->ns = ns;
  681. kn->priv = priv;
  682. /* link in */
  683. rc = kernfs_add_one(kn);
  684. if (!rc)
  685. return kn;
  686. kernfs_put(kn);
  687. return ERR_PTR(rc);
  688. }
  689. /**
  690. * kernfs_create_empty_dir - create an always empty directory
  691. * @parent: parent in which to create a new directory
  692. * @name: name of the new directory
  693. *
  694. * Returns the created node on success, ERR_PTR() value on failure.
  695. */
  696. struct kernfs_node *kernfs_create_empty_dir(struct kernfs_node *parent,
  697. const char *name)
  698. {
  699. struct kernfs_node *kn;
  700. int rc;
  701. /* allocate */
  702. kn = kernfs_new_node(parent, name, S_IRUGO|S_IXUGO|S_IFDIR, KERNFS_DIR);
  703. if (!kn)
  704. return ERR_PTR(-ENOMEM);
  705. kn->flags |= KERNFS_EMPTY_DIR;
  706. kn->dir.root = parent->dir.root;
  707. kn->ns = NULL;
  708. kn->priv = NULL;
  709. /* link in */
  710. rc = kernfs_add_one(kn);
  711. if (!rc)
  712. return kn;
  713. kernfs_put(kn);
  714. return ERR_PTR(rc);
  715. }
  716. static struct dentry *kernfs_iop_lookup(struct inode *dir,
  717. struct dentry *dentry,
  718. unsigned int flags)
  719. {
  720. struct dentry *ret;
  721. struct kernfs_node *parent = dentry->d_parent->d_fsdata;
  722. struct kernfs_node *kn;
  723. struct inode *inode;
  724. const void *ns = NULL;
  725. mutex_lock(&kernfs_mutex);
  726. if (kernfs_ns_enabled(parent))
  727. ns = kernfs_info(dir->i_sb)->ns;
  728. kn = kernfs_find_ns(parent, dentry->d_name.name, ns);
  729. /* no such entry */
  730. if (!kn || !kernfs_active(kn)) {
  731. ret = NULL;
  732. goto out_unlock;
  733. }
  734. kernfs_get(kn);
  735. dentry->d_fsdata = kn;
  736. /* attach dentry and inode */
  737. inode = kernfs_get_inode(dir->i_sb, kn);
  738. if (!inode) {
  739. ret = ERR_PTR(-ENOMEM);
  740. goto out_unlock;
  741. }
  742. /* instantiate and hash dentry */
  743. ret = d_splice_alias(inode, dentry);
  744. out_unlock:
  745. mutex_unlock(&kernfs_mutex);
  746. return ret;
  747. }
  748. static int kernfs_iop_mkdir(struct inode *dir, struct dentry *dentry,
  749. umode_t mode)
  750. {
  751. struct kernfs_node *parent = dir->i_private;
  752. struct kernfs_syscall_ops *scops = kernfs_root(parent)->syscall_ops;
  753. int ret;
  754. if (!scops || !scops->mkdir)
  755. return -EPERM;
  756. if (!kernfs_get_active(parent))
  757. return -ENODEV;
  758. ret = scops->mkdir(parent, dentry->d_name.name, mode);
  759. kernfs_put_active(parent);
  760. return ret;
  761. }
  762. static int kernfs_iop_rmdir(struct inode *dir, struct dentry *dentry)
  763. {
  764. struct kernfs_node *kn = dentry->d_fsdata;
  765. struct kernfs_syscall_ops *scops = kernfs_root(kn)->syscall_ops;
  766. int ret;
  767. if (!scops || !scops->rmdir)
  768. return -EPERM;
  769. if (!kernfs_get_active(kn))
  770. return -ENODEV;
  771. ret = scops->rmdir(kn);
  772. kernfs_put_active(kn);
  773. return ret;
  774. }
  775. static int kernfs_iop_rename(struct inode *old_dir, struct dentry *old_dentry,
  776. struct inode *new_dir, struct dentry *new_dentry)
  777. {
  778. struct kernfs_node *kn = old_dentry->d_fsdata;
  779. struct kernfs_node *new_parent = new_dir->i_private;
  780. struct kernfs_syscall_ops *scops = kernfs_root(kn)->syscall_ops;
  781. int ret;
  782. if (!scops || !scops->rename)
  783. return -EPERM;
  784. if (!kernfs_get_active(kn))
  785. return -ENODEV;
  786. if (!kernfs_get_active(new_parent)) {
  787. kernfs_put_active(kn);
  788. return -ENODEV;
  789. }
  790. ret = scops->rename(kn, new_parent, new_dentry->d_name.name);
  791. kernfs_put_active(new_parent);
  792. kernfs_put_active(kn);
  793. return ret;
  794. }
  795. const struct inode_operations kernfs_dir_iops = {
  796. .lookup = kernfs_iop_lookup,
  797. .permission = kernfs_iop_permission,
  798. .setattr = kernfs_iop_setattr,
  799. .getattr = kernfs_iop_getattr,
  800. .setxattr = kernfs_iop_setxattr,
  801. .removexattr = kernfs_iop_removexattr,
  802. .getxattr = kernfs_iop_getxattr,
  803. .listxattr = kernfs_iop_listxattr,
  804. .mkdir = kernfs_iop_mkdir,
  805. .rmdir = kernfs_iop_rmdir,
  806. .rename = kernfs_iop_rename,
  807. };
  808. static struct kernfs_node *kernfs_leftmost_descendant(struct kernfs_node *pos)
  809. {
  810. struct kernfs_node *last;
  811. while (true) {
  812. struct rb_node *rbn;
  813. last = pos;
  814. if (kernfs_type(pos) != KERNFS_DIR)
  815. break;
  816. rbn = rb_first(&pos->dir.children);
  817. if (!rbn)
  818. break;
  819. pos = rb_to_kn(rbn);
  820. }
  821. return last;
  822. }
  823. /**
  824. * kernfs_next_descendant_post - find the next descendant for post-order walk
  825. * @pos: the current position (%NULL to initiate traversal)
  826. * @root: kernfs_node whose descendants to walk
  827. *
  828. * Find the next descendant to visit for post-order traversal of @root's
  829. * descendants. @root is included in the iteration and the last node to be
  830. * visited.
  831. */
  832. static struct kernfs_node *kernfs_next_descendant_post(struct kernfs_node *pos,
  833. struct kernfs_node *root)
  834. {
  835. struct rb_node *rbn;
  836. lockdep_assert_held(&kernfs_mutex);
  837. /* if first iteration, visit leftmost descendant which may be root */
  838. if (!pos)
  839. return kernfs_leftmost_descendant(root);
  840. /* if we visited @root, we're done */
  841. if (pos == root)
  842. return NULL;
  843. /* if there's an unvisited sibling, visit its leftmost descendant */
  844. rbn = rb_next(&pos->rb);
  845. if (rbn)
  846. return kernfs_leftmost_descendant(rb_to_kn(rbn));
  847. /* no sibling left, visit parent */
  848. return pos->parent;
  849. }
  850. /**
  851. * kernfs_activate - activate a node which started deactivated
  852. * @kn: kernfs_node whose subtree is to be activated
  853. *
  854. * If the root has KERNFS_ROOT_CREATE_DEACTIVATED set, a newly created node
  855. * needs to be explicitly activated. A node which hasn't been activated
  856. * isn't visible to userland and deactivation is skipped during its
  857. * removal. This is useful to construct atomic init sequences where
  858. * creation of multiple nodes should either succeed or fail atomically.
  859. *
  860. * The caller is responsible for ensuring that this function is not called
  861. * after kernfs_remove*() is invoked on @kn.
  862. */
  863. void kernfs_activate(struct kernfs_node *kn)
  864. {
  865. struct kernfs_node *pos;
  866. mutex_lock(&kernfs_mutex);
  867. pos = NULL;
  868. while ((pos = kernfs_next_descendant_post(pos, kn))) {
  869. if (!pos || (pos->flags & KERNFS_ACTIVATED))
  870. continue;
  871. WARN_ON_ONCE(pos->parent && RB_EMPTY_NODE(&pos->rb));
  872. WARN_ON_ONCE(atomic_read(&pos->active) != KN_DEACTIVATED_BIAS);
  873. atomic_sub(KN_DEACTIVATED_BIAS, &pos->active);
  874. pos->flags |= KERNFS_ACTIVATED;
  875. }
  876. mutex_unlock(&kernfs_mutex);
  877. }
  878. static void __kernfs_remove(struct kernfs_node *kn)
  879. {
  880. struct kernfs_node *pos;
  881. lockdep_assert_held(&kernfs_mutex);
  882. /*
  883. * Short-circuit if non-root @kn has already finished removal.
  884. * This is for kernfs_remove_self() which plays with active ref
  885. * after removal.
  886. */
  887. if (!kn || (kn->parent && RB_EMPTY_NODE(&kn->rb)))
  888. return;
  889. pr_debug("kernfs %s: removing\n", kn->name);
  890. /* prevent any new usage under @kn by deactivating all nodes */
  891. pos = NULL;
  892. while ((pos = kernfs_next_descendant_post(pos, kn)))
  893. if (kernfs_active(pos))
  894. atomic_add(KN_DEACTIVATED_BIAS, &pos->active);
  895. /* deactivate and unlink the subtree node-by-node */
  896. do {
  897. pos = kernfs_leftmost_descendant(kn);
  898. /*
  899. * kernfs_drain() drops kernfs_mutex temporarily and @pos's
  900. * base ref could have been put by someone else by the time
  901. * the function returns. Make sure it doesn't go away
  902. * underneath us.
  903. */
  904. kernfs_get(pos);
  905. /*
  906. * Drain iff @kn was activated. This avoids draining and
  907. * its lockdep annotations for nodes which have never been
  908. * activated and allows embedding kernfs_remove() in create
  909. * error paths without worrying about draining.
  910. */
  911. if (kn->flags & KERNFS_ACTIVATED)
  912. kernfs_drain(pos);
  913. else
  914. WARN_ON_ONCE(atomic_read(&kn->active) != KN_DEACTIVATED_BIAS);
  915. /*
  916. * kernfs_unlink_sibling() succeeds once per node. Use it
  917. * to decide who's responsible for cleanups.
  918. */
  919. if (!pos->parent || kernfs_unlink_sibling(pos)) {
  920. struct kernfs_iattrs *ps_iattr =
  921. pos->parent ? pos->parent->iattr : NULL;
  922. /* update timestamps on the parent */
  923. if (ps_iattr) {
  924. ps_iattr->ia_iattr.ia_ctime = CURRENT_TIME;
  925. ps_iattr->ia_iattr.ia_mtime = CURRENT_TIME;
  926. }
  927. kernfs_put(pos);
  928. }
  929. kernfs_put(pos);
  930. } while (pos != kn);
  931. }
  932. /**
  933. * kernfs_remove - remove a kernfs_node recursively
  934. * @kn: the kernfs_node to remove
  935. *
  936. * Remove @kn along with all its subdirectories and files.
  937. */
  938. void kernfs_remove(struct kernfs_node *kn)
  939. {
  940. mutex_lock(&kernfs_mutex);
  941. __kernfs_remove(kn);
  942. mutex_unlock(&kernfs_mutex);
  943. }
  944. /**
  945. * kernfs_break_active_protection - break out of active protection
  946. * @kn: the self kernfs_node
  947. *
  948. * The caller must be running off of a kernfs operation which is invoked
  949. * with an active reference - e.g. one of kernfs_ops. Each invocation of
  950. * this function must also be matched with an invocation of
  951. * kernfs_unbreak_active_protection().
  952. *
  953. * This function releases the active reference of @kn the caller is
  954. * holding. Once this function is called, @kn may be removed at any point
  955. * and the caller is solely responsible for ensuring that the objects it
  956. * dereferences are accessible.
  957. */
  958. void kernfs_break_active_protection(struct kernfs_node *kn)
  959. {
  960. /*
  961. * Take out ourself out of the active ref dependency chain. If
  962. * we're called without an active ref, lockdep will complain.
  963. */
  964. kernfs_put_active(kn);
  965. }
  966. /**
  967. * kernfs_unbreak_active_protection - undo kernfs_break_active_protection()
  968. * @kn: the self kernfs_node
  969. *
  970. * If kernfs_break_active_protection() was called, this function must be
  971. * invoked before finishing the kernfs operation. Note that while this
  972. * function restores the active reference, it doesn't and can't actually
  973. * restore the active protection - @kn may already or be in the process of
  974. * being removed. Once kernfs_break_active_protection() is invoked, that
  975. * protection is irreversibly gone for the kernfs operation instance.
  976. *
  977. * While this function may be called at any point after
  978. * kernfs_break_active_protection() is invoked, its most useful location
  979. * would be right before the enclosing kernfs operation returns.
  980. */
  981. void kernfs_unbreak_active_protection(struct kernfs_node *kn)
  982. {
  983. /*
  984. * @kn->active could be in any state; however, the increment we do
  985. * here will be undone as soon as the enclosing kernfs operation
  986. * finishes and this temporary bump can't break anything. If @kn
  987. * is alive, nothing changes. If @kn is being deactivated, the
  988. * soon-to-follow put will either finish deactivation or restore
  989. * deactivated state. If @kn is already removed, the temporary
  990. * bump is guaranteed to be gone before @kn is released.
  991. */
  992. atomic_inc(&kn->active);
  993. if (kernfs_lockdep(kn))
  994. rwsem_acquire(&kn->dep_map, 0, 1, _RET_IP_);
  995. }
  996. /**
  997. * kernfs_remove_self - remove a kernfs_node from its own method
  998. * @kn: the self kernfs_node to remove
  999. *
  1000. * The caller must be running off of a kernfs operation which is invoked
  1001. * with an active reference - e.g. one of kernfs_ops. This can be used to
  1002. * implement a file operation which deletes itself.
  1003. *
  1004. * For example, the "delete" file for a sysfs device directory can be
  1005. * implemented by invoking kernfs_remove_self() on the "delete" file
  1006. * itself. This function breaks the circular dependency of trying to
  1007. * deactivate self while holding an active ref itself. It isn't necessary
  1008. * to modify the usual removal path to use kernfs_remove_self(). The
  1009. * "delete" implementation can simply invoke kernfs_remove_self() on self
  1010. * before proceeding with the usual removal path. kernfs will ignore later
  1011. * kernfs_remove() on self.
  1012. *
  1013. * kernfs_remove_self() can be called multiple times concurrently on the
  1014. * same kernfs_node. Only the first one actually performs removal and
  1015. * returns %true. All others will wait until the kernfs operation which
  1016. * won self-removal finishes and return %false. Note that the losers wait
  1017. * for the completion of not only the winning kernfs_remove_self() but also
  1018. * the whole kernfs_ops which won the arbitration. This can be used to
  1019. * guarantee, for example, all concurrent writes to a "delete" file to
  1020. * finish only after the whole operation is complete.
  1021. */
  1022. bool kernfs_remove_self(struct kernfs_node *kn)
  1023. {
  1024. bool ret;
  1025. mutex_lock(&kernfs_mutex);
  1026. kernfs_break_active_protection(kn);
  1027. /*
  1028. * SUICIDAL is used to arbitrate among competing invocations. Only
  1029. * the first one will actually perform removal. When the removal
  1030. * is complete, SUICIDED is set and the active ref is restored
  1031. * while holding kernfs_mutex. The ones which lost arbitration
  1032. * waits for SUICDED && drained which can happen only after the
  1033. * enclosing kernfs operation which executed the winning instance
  1034. * of kernfs_remove_self() finished.
  1035. */
  1036. if (!(kn->flags & KERNFS_SUICIDAL)) {
  1037. kn->flags |= KERNFS_SUICIDAL;
  1038. __kernfs_remove(kn);
  1039. kn->flags |= KERNFS_SUICIDED;
  1040. ret = true;
  1041. } else {
  1042. wait_queue_head_t *waitq = &kernfs_root(kn)->deactivate_waitq;
  1043. DEFINE_WAIT(wait);
  1044. while (true) {
  1045. prepare_to_wait(waitq, &wait, TASK_UNINTERRUPTIBLE);
  1046. if ((kn->flags & KERNFS_SUICIDED) &&
  1047. atomic_read(&kn->active) == KN_DEACTIVATED_BIAS)
  1048. break;
  1049. mutex_unlock(&kernfs_mutex);
  1050. schedule();
  1051. mutex_lock(&kernfs_mutex);
  1052. }
  1053. finish_wait(waitq, &wait);
  1054. WARN_ON_ONCE(!RB_EMPTY_NODE(&kn->rb));
  1055. ret = false;
  1056. }
  1057. /*
  1058. * This must be done while holding kernfs_mutex; otherwise, waiting
  1059. * for SUICIDED && deactivated could finish prematurely.
  1060. */
  1061. kernfs_unbreak_active_protection(kn);
  1062. mutex_unlock(&kernfs_mutex);
  1063. return ret;
  1064. }
  1065. /**
  1066. * kernfs_remove_by_name_ns - find a kernfs_node by name and remove it
  1067. * @parent: parent of the target
  1068. * @name: name of the kernfs_node to remove
  1069. * @ns: namespace tag of the kernfs_node to remove
  1070. *
  1071. * Look for the kernfs_node with @name and @ns under @parent and remove it.
  1072. * Returns 0 on success, -ENOENT if such entry doesn't exist.
  1073. */
  1074. int kernfs_remove_by_name_ns(struct kernfs_node *parent, const char *name,
  1075. const void *ns)
  1076. {
  1077. struct kernfs_node *kn;
  1078. if (!parent) {
  1079. WARN(1, KERN_WARNING "kernfs: can not remove '%s', no directory\n",
  1080. name);
  1081. return -ENOENT;
  1082. }
  1083. mutex_lock(&kernfs_mutex);
  1084. kn = kernfs_find_ns(parent, name, ns);
  1085. if (kn)
  1086. __kernfs_remove(kn);
  1087. mutex_unlock(&kernfs_mutex);
  1088. if (kn)
  1089. return 0;
  1090. else
  1091. return -ENOENT;
  1092. }
  1093. /**
  1094. * kernfs_rename_ns - move and rename a kernfs_node
  1095. * @kn: target node
  1096. * @new_parent: new parent to put @sd under
  1097. * @new_name: new name
  1098. * @new_ns: new namespace tag
  1099. */
  1100. int kernfs_rename_ns(struct kernfs_node *kn, struct kernfs_node *new_parent,
  1101. const char *new_name, const void *new_ns)
  1102. {
  1103. struct kernfs_node *old_parent;
  1104. const char *old_name = NULL;
  1105. int error;
  1106. /* can't move or rename root */
  1107. if (!kn->parent)
  1108. return -EINVAL;
  1109. mutex_lock(&kernfs_mutex);
  1110. error = -ENOENT;
  1111. if (!kernfs_active(kn) || !kernfs_active(new_parent) ||
  1112. (new_parent->flags & KERNFS_EMPTY_DIR))
  1113. goto out;
  1114. error = 0;
  1115. if ((kn->parent == new_parent) && (kn->ns == new_ns) &&
  1116. (strcmp(kn->name, new_name) == 0))
  1117. goto out; /* nothing to rename */
  1118. error = -EEXIST;
  1119. if (kernfs_find_ns(new_parent, new_name, new_ns))
  1120. goto out;
  1121. /* rename kernfs_node */
  1122. if (strcmp(kn->name, new_name) != 0) {
  1123. error = -ENOMEM;
  1124. new_name = kstrdup_const(new_name, GFP_KERNEL);
  1125. if (!new_name)
  1126. goto out;
  1127. } else {
  1128. new_name = NULL;
  1129. }
  1130. /*
  1131. * Move to the appropriate place in the appropriate directories rbtree.
  1132. */
  1133. kernfs_unlink_sibling(kn);
  1134. kernfs_get(new_parent);
  1135. /* rename_lock protects ->parent and ->name accessors */
  1136. spin_lock_irq(&kernfs_rename_lock);
  1137. old_parent = kn->parent;
  1138. kn->parent = new_parent;
  1139. kn->ns = new_ns;
  1140. if (new_name) {
  1141. old_name = kn->name;
  1142. kn->name = new_name;
  1143. }
  1144. spin_unlock_irq(&kernfs_rename_lock);
  1145. kn->hash = kernfs_name_hash(kn->name, kn->ns);
  1146. kernfs_link_sibling(kn);
  1147. kernfs_put(old_parent);
  1148. kfree_const(old_name);
  1149. error = 0;
  1150. out:
  1151. mutex_unlock(&kernfs_mutex);
  1152. return error;
  1153. }
  1154. /* Relationship between s_mode and the DT_xxx types */
  1155. static inline unsigned char dt_type(struct kernfs_node *kn)
  1156. {
  1157. return (kn->mode >> 12) & 15;
  1158. }
  1159. static int kernfs_dir_fop_release(struct inode *inode, struct file *filp)
  1160. {
  1161. kernfs_put(filp->private_data);
  1162. return 0;
  1163. }
  1164. static struct kernfs_node *kernfs_dir_pos(const void *ns,
  1165. struct kernfs_node *parent, loff_t hash, struct kernfs_node *pos)
  1166. {
  1167. if (pos) {
  1168. int valid = kernfs_active(pos) &&
  1169. pos->parent == parent && hash == pos->hash;
  1170. kernfs_put(pos);
  1171. if (!valid)
  1172. pos = NULL;
  1173. }
  1174. if (!pos && (hash > 1) && (hash < INT_MAX)) {
  1175. struct rb_node *node = parent->dir.children.rb_node;
  1176. while (node) {
  1177. pos = rb_to_kn(node);
  1178. if (hash < pos->hash)
  1179. node = node->rb_left;
  1180. else if (hash > pos->hash)
  1181. node = node->rb_right;
  1182. else
  1183. break;
  1184. }
  1185. }
  1186. /* Skip over entries which are dying/dead or in the wrong namespace */
  1187. while (pos && (!kernfs_active(pos) || pos->ns != ns)) {
  1188. struct rb_node *node = rb_next(&pos->rb);
  1189. if (!node)
  1190. pos = NULL;
  1191. else
  1192. pos = rb_to_kn(node);
  1193. }
  1194. return pos;
  1195. }
  1196. static struct kernfs_node *kernfs_dir_next_pos(const void *ns,
  1197. struct kernfs_node *parent, ino_t ino, struct kernfs_node *pos)
  1198. {
  1199. pos = kernfs_dir_pos(ns, parent, ino, pos);
  1200. if (pos) {
  1201. do {
  1202. struct rb_node *node = rb_next(&pos->rb);
  1203. if (!node)
  1204. pos = NULL;
  1205. else
  1206. pos = rb_to_kn(node);
  1207. } while (pos && (!kernfs_active(pos) || pos->ns != ns));
  1208. }
  1209. return pos;
  1210. }
  1211. static int kernfs_fop_readdir(struct file *file, struct dir_context *ctx)
  1212. {
  1213. struct dentry *dentry = file->f_path.dentry;
  1214. struct kernfs_node *parent = dentry->d_fsdata;
  1215. struct kernfs_node *pos = file->private_data;
  1216. const void *ns = NULL;
  1217. if (!dir_emit_dots(file, ctx))
  1218. return 0;
  1219. mutex_lock(&kernfs_mutex);
  1220. if (kernfs_ns_enabled(parent))
  1221. ns = kernfs_info(dentry->d_sb)->ns;
  1222. for (pos = kernfs_dir_pos(ns, parent, ctx->pos, pos);
  1223. pos;
  1224. pos = kernfs_dir_next_pos(ns, parent, ctx->pos, pos)) {
  1225. const char *name = pos->name;
  1226. unsigned int type = dt_type(pos);
  1227. int len = strlen(name);
  1228. ino_t ino = pos->ino;
  1229. ctx->pos = pos->hash;
  1230. file->private_data = pos;
  1231. kernfs_get(pos);
  1232. mutex_unlock(&kernfs_mutex);
  1233. if (!dir_emit(ctx, name, len, ino, type))
  1234. return 0;
  1235. mutex_lock(&kernfs_mutex);
  1236. }
  1237. mutex_unlock(&kernfs_mutex);
  1238. file->private_data = NULL;
  1239. ctx->pos = INT_MAX;
  1240. return 0;
  1241. }
  1242. static loff_t kernfs_dir_fop_llseek(struct file *file, loff_t offset,
  1243. int whence)
  1244. {
  1245. struct inode *inode = file_inode(file);
  1246. loff_t ret;
  1247. mutex_lock(&inode->i_mutex);
  1248. ret = generic_file_llseek(file, offset, whence);
  1249. mutex_unlock(&inode->i_mutex);
  1250. return ret;
  1251. }
  1252. const struct file_operations kernfs_dir_fops = {
  1253. .read = generic_read_dir,
  1254. .iterate = kernfs_fop_readdir,
  1255. .release = kernfs_dir_fop_release,
  1256. .llseek = kernfs_dir_fop_llseek,
  1257. };