dir.c 40 KB

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