expire.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright 1997-1998 Transmeta Corporation -- All Rights Reserved
  4. * Copyright 1999-2000 Jeremy Fitzhardinge <jeremy@goop.org>
  5. * Copyright 2001-2006 Ian Kent <raven@themaw.net>
  6. */
  7. #include "autofs_i.h"
  8. /* Check if a dentry can be expired */
  9. static inline int autofs_can_expire(struct dentry *dentry,
  10. unsigned long timeout, unsigned int how)
  11. {
  12. struct autofs_info *ino = autofs_dentry_ino(dentry);
  13. /* dentry in the process of being deleted */
  14. if (ino == NULL)
  15. return 0;
  16. if (!(how & AUTOFS_EXP_IMMEDIATE)) {
  17. /* Too young to die */
  18. if (!timeout || time_after(ino->last_used + timeout, jiffies))
  19. return 0;
  20. }
  21. return 1;
  22. }
  23. /* Check a mount point for busyness */
  24. static int autofs_mount_busy(struct vfsmount *mnt,
  25. struct dentry *dentry, unsigned int how)
  26. {
  27. struct dentry *top = dentry;
  28. struct path path = {.mnt = mnt, .dentry = dentry};
  29. int status = 1;
  30. pr_debug("dentry %p %pd\n", dentry, dentry);
  31. path_get(&path);
  32. if (!follow_down_one(&path))
  33. goto done;
  34. if (is_autofs_dentry(path.dentry)) {
  35. struct autofs_sb_info *sbi = autofs_sbi(path.dentry->d_sb);
  36. /* This is an autofs submount, we can't expire it */
  37. if (autofs_type_indirect(sbi->type))
  38. goto done;
  39. }
  40. /* Not a submount, has a forced expire been requested */
  41. if (how & AUTOFS_EXP_FORCED) {
  42. status = 0;
  43. goto done;
  44. }
  45. /* Update the expiry counter if fs is busy */
  46. if (!may_umount_tree(path.mnt)) {
  47. struct autofs_info *ino;
  48. ino = autofs_dentry_ino(top);
  49. ino->last_used = jiffies;
  50. goto done;
  51. }
  52. status = 0;
  53. done:
  54. pr_debug("returning = %d\n", status);
  55. path_put(&path);
  56. return status;
  57. }
  58. /* p->d_lock held */
  59. static struct dentry *positive_after(struct dentry *p, struct dentry *child)
  60. {
  61. child = child ? d_next_sibling(child) : d_first_child(p);
  62. hlist_for_each_entry_from(child, d_sib) {
  63. spin_lock_nested(&child->d_lock, DENTRY_D_LOCK_NESTED);
  64. if (simple_positive(child)) {
  65. dget_dlock(child);
  66. spin_unlock(&child->d_lock);
  67. return child;
  68. }
  69. spin_unlock(&child->d_lock);
  70. }
  71. return NULL;
  72. }
  73. /*
  74. * Calculate and dget next entry in the subdirs list under root.
  75. */
  76. static struct dentry *get_next_positive_subdir(struct dentry *prev,
  77. struct dentry *root)
  78. {
  79. struct autofs_sb_info *sbi = autofs_sbi(root->d_sb);
  80. struct dentry *q;
  81. spin_lock(&sbi->lookup_lock);
  82. spin_lock(&root->d_lock);
  83. q = positive_after(root, prev);
  84. spin_unlock(&root->d_lock);
  85. spin_unlock(&sbi->lookup_lock);
  86. dput(prev);
  87. return q;
  88. }
  89. /*
  90. * Calculate and dget next entry in top down tree traversal.
  91. */
  92. static struct dentry *get_next_positive_dentry(struct dentry *prev,
  93. struct dentry *root)
  94. {
  95. struct autofs_sb_info *sbi = autofs_sbi(root->d_sb);
  96. struct dentry *p = prev, *ret = NULL, *d = NULL;
  97. if (prev == NULL)
  98. return dget(root);
  99. spin_lock(&sbi->lookup_lock);
  100. spin_lock(&p->d_lock);
  101. while (1) {
  102. struct dentry *parent;
  103. ret = positive_after(p, d);
  104. if (ret || p == root)
  105. break;
  106. parent = p->d_parent;
  107. spin_unlock(&p->d_lock);
  108. spin_lock(&parent->d_lock);
  109. d = p;
  110. p = parent;
  111. }
  112. spin_unlock(&p->d_lock);
  113. spin_unlock(&sbi->lookup_lock);
  114. dput(prev);
  115. return ret;
  116. }
  117. /*
  118. * Check a direct mount point for busyness.
  119. * Direct mounts have similar expiry semantics to tree mounts.
  120. * The tree is not busy iff no mountpoints are busy and there are no
  121. * autofs submounts.
  122. */
  123. static int autofs_direct_busy(struct vfsmount *mnt,
  124. struct dentry *top,
  125. unsigned long timeout,
  126. unsigned int how)
  127. {
  128. pr_debug("top %p %pd\n", top, top);
  129. /* Forced expire, user space handles busy mounts */
  130. if (how & AUTOFS_EXP_FORCED)
  131. return 0;
  132. /* If it's busy update the expiry counters */
  133. if (!may_umount_tree(mnt)) {
  134. struct autofs_info *ino;
  135. ino = autofs_dentry_ino(top);
  136. if (ino)
  137. ino->last_used = jiffies;
  138. return 1;
  139. }
  140. /* Timeout of a direct mount is determined by its top dentry */
  141. if (!autofs_can_expire(top, timeout, how))
  142. return 1;
  143. return 0;
  144. }
  145. /*
  146. * Check a directory tree of mount points for busyness
  147. * The tree is not busy iff no mountpoints are busy
  148. */
  149. static int autofs_tree_busy(struct vfsmount *mnt,
  150. struct dentry *top,
  151. unsigned long timeout,
  152. unsigned int how)
  153. {
  154. struct autofs_info *top_ino = autofs_dentry_ino(top);
  155. struct dentry *p;
  156. pr_debug("top %p %pd\n", top, top);
  157. /* Negative dentry - give up */
  158. if (!simple_positive(top))
  159. return 1;
  160. p = NULL;
  161. while ((p = get_next_positive_dentry(p, top))) {
  162. pr_debug("dentry %p %pd\n", p, p);
  163. /*
  164. * Is someone visiting anywhere in the subtree ?
  165. * If there's no mount we need to check the usage
  166. * count for the autofs dentry.
  167. * If the fs is busy update the expiry counter.
  168. */
  169. if (d_mountpoint(p)) {
  170. if (autofs_mount_busy(mnt, p, how)) {
  171. top_ino->last_used = jiffies;
  172. dput(p);
  173. return 1;
  174. }
  175. } else {
  176. struct autofs_info *ino = autofs_dentry_ino(p);
  177. unsigned int ino_count = READ_ONCE(ino->count);
  178. /* allow for dget above and top is already dgot */
  179. if (p == top)
  180. ino_count += 2;
  181. else
  182. ino_count++;
  183. if (d_count(p) > ino_count) {
  184. top_ino->last_used = jiffies;
  185. dput(p);
  186. return 1;
  187. }
  188. }
  189. }
  190. /* Forced expire, user space handles busy mounts */
  191. if (how & AUTOFS_EXP_FORCED)
  192. return 0;
  193. /* Timeout of a tree mount is ultimately determined by its top dentry */
  194. if (!autofs_can_expire(top, timeout, how))
  195. return 1;
  196. return 0;
  197. }
  198. static struct dentry *autofs_check_leaves(struct vfsmount *mnt,
  199. struct dentry *parent,
  200. unsigned long timeout,
  201. unsigned int how)
  202. {
  203. struct dentry *p;
  204. pr_debug("parent %p %pd\n", parent, parent);
  205. p = NULL;
  206. while ((p = get_next_positive_dentry(p, parent))) {
  207. pr_debug("dentry %p %pd\n", p, p);
  208. if (d_mountpoint(p)) {
  209. /* Can we umount this guy */
  210. if (autofs_mount_busy(mnt, p, how))
  211. continue;
  212. /* This isn't a submount so if a forced expire
  213. * has been requested, user space handles busy
  214. * mounts */
  215. if (how & AUTOFS_EXP_FORCED)
  216. return p;
  217. /* Can we expire this guy */
  218. if (autofs_can_expire(p, timeout, how))
  219. return p;
  220. }
  221. }
  222. return NULL;
  223. }
  224. /* Check if we can expire a direct mount (possibly a tree) */
  225. static struct dentry *autofs_expire_direct(struct super_block *sb,
  226. struct vfsmount *mnt,
  227. struct autofs_sb_info *sbi,
  228. unsigned int how)
  229. {
  230. struct dentry *root = dget(sb->s_root);
  231. struct autofs_info *ino;
  232. unsigned long timeout;
  233. if (!root)
  234. return NULL;
  235. timeout = sbi->exp_timeout;
  236. if (!autofs_direct_busy(mnt, root, timeout, how)) {
  237. spin_lock(&sbi->fs_lock);
  238. ino = autofs_dentry_ino(root);
  239. /* No point expiring a pending mount */
  240. if (ino->flags & AUTOFS_INF_PENDING) {
  241. spin_unlock(&sbi->fs_lock);
  242. goto out;
  243. }
  244. ino->flags |= AUTOFS_INF_WANT_EXPIRE;
  245. spin_unlock(&sbi->fs_lock);
  246. synchronize_rcu();
  247. if (!autofs_direct_busy(mnt, root, timeout, how)) {
  248. spin_lock(&sbi->fs_lock);
  249. ino->flags |= AUTOFS_INF_EXPIRING;
  250. init_completion(&ino->expire_complete);
  251. spin_unlock(&sbi->fs_lock);
  252. return root;
  253. }
  254. spin_lock(&sbi->fs_lock);
  255. ino->flags &= ~AUTOFS_INF_WANT_EXPIRE;
  256. spin_unlock(&sbi->fs_lock);
  257. }
  258. out:
  259. dput(root);
  260. return NULL;
  261. }
  262. /* Check if 'dentry' should expire, or return a nearby
  263. * dentry that is suitable.
  264. * If returned dentry is different from arg dentry,
  265. * then a dget() reference was taken, else not.
  266. */
  267. static struct dentry *should_expire(struct dentry *dentry,
  268. struct vfsmount *mnt,
  269. unsigned long timeout,
  270. unsigned int how)
  271. {
  272. struct autofs_info *ino = autofs_dentry_ino(dentry);
  273. unsigned int ino_count;
  274. /* No point expiring a pending mount */
  275. if (ino->flags & AUTOFS_INF_PENDING)
  276. return NULL;
  277. /*
  278. * Case 1: (i) indirect mount or top level pseudo direct mount
  279. * (autofs-4.1).
  280. * (ii) indirect mount with offset mount, check the "/"
  281. * offset (autofs-5.0+).
  282. */
  283. if (d_mountpoint(dentry)) {
  284. pr_debug("checking mountpoint %p %pd\n", dentry, dentry);
  285. /* Can we umount this guy */
  286. if (autofs_mount_busy(mnt, dentry, how))
  287. return NULL;
  288. /* This isn't a submount so if a forced expire
  289. * has been requested, user space handles busy
  290. * mounts */
  291. if (how & AUTOFS_EXP_FORCED)
  292. return dentry;
  293. /* Can we expire this guy */
  294. if (autofs_can_expire(dentry, timeout, how))
  295. return dentry;
  296. return NULL;
  297. }
  298. if (d_is_symlink(dentry)) {
  299. pr_debug("checking symlink %p %pd\n", dentry, dentry);
  300. /* Forced expire, user space handles busy mounts */
  301. if (how & AUTOFS_EXP_FORCED)
  302. return dentry;
  303. /*
  304. * A symlink can't be "busy" in the usual sense so
  305. * just check last used for expire timeout.
  306. */
  307. if (autofs_can_expire(dentry, timeout, how))
  308. return dentry;
  309. return NULL;
  310. }
  311. if (autofs_empty(ino))
  312. return NULL;
  313. /* Case 2: tree mount, expire iff entire tree is not busy */
  314. if (!(how & AUTOFS_EXP_LEAVES)) {
  315. /* Not a forced expire? */
  316. if (!(how & AUTOFS_EXP_FORCED)) {
  317. /* ref-walk currently on this dentry? */
  318. ino_count = READ_ONCE(ino->count) + 1;
  319. if (d_count(dentry) > ino_count)
  320. return NULL;
  321. }
  322. if (!autofs_tree_busy(mnt, dentry, timeout, how))
  323. return dentry;
  324. /*
  325. * Case 3: pseudo direct mount, expire individual leaves
  326. * (autofs-4.1).
  327. */
  328. } else {
  329. struct dentry *expired;
  330. /* Not a forced expire? */
  331. if (!(how & AUTOFS_EXP_FORCED)) {
  332. /* ref-walk currently on this dentry? */
  333. ino_count = READ_ONCE(ino->count) + 1;
  334. if (d_count(dentry) > ino_count)
  335. return NULL;
  336. }
  337. expired = autofs_check_leaves(mnt, dentry, timeout, how);
  338. if (expired) {
  339. if (expired == dentry)
  340. dput(dentry);
  341. return expired;
  342. }
  343. }
  344. return NULL;
  345. }
  346. /*
  347. * Find an eligible tree to time-out
  348. * A tree is eligible if :-
  349. * - it is unused by any user process
  350. * - it has been unused for exp_timeout time
  351. */
  352. static struct dentry *autofs_expire_indirect(struct super_block *sb,
  353. struct vfsmount *mnt,
  354. struct autofs_sb_info *sbi,
  355. unsigned int how)
  356. {
  357. unsigned long timeout;
  358. struct dentry *root = sb->s_root;
  359. struct dentry *dentry;
  360. struct dentry *expired;
  361. struct dentry *found;
  362. struct autofs_info *ino;
  363. if (!root)
  364. return NULL;
  365. dentry = NULL;
  366. while ((dentry = get_next_positive_subdir(dentry, root))) {
  367. spin_lock(&sbi->fs_lock);
  368. ino = autofs_dentry_ino(dentry);
  369. if (ino->flags & AUTOFS_INF_WANT_EXPIRE) {
  370. spin_unlock(&sbi->fs_lock);
  371. continue;
  372. }
  373. spin_unlock(&sbi->fs_lock);
  374. if (ino->flags & AUTOFS_INF_EXPIRE_SET)
  375. timeout = ino->exp_timeout;
  376. else
  377. timeout = sbi->exp_timeout;
  378. expired = should_expire(dentry, mnt, timeout, how);
  379. if (!expired)
  380. continue;
  381. spin_lock(&sbi->fs_lock);
  382. ino = autofs_dentry_ino(expired);
  383. ino->flags |= AUTOFS_INF_WANT_EXPIRE;
  384. spin_unlock(&sbi->fs_lock);
  385. synchronize_rcu();
  386. /* Make sure a reference is not taken on found if
  387. * things have changed.
  388. */
  389. how &= ~AUTOFS_EXP_LEAVES;
  390. found = should_expire(expired, mnt, timeout, how);
  391. if (found != expired) { // something has changed, continue
  392. dput(found);
  393. goto next;
  394. }
  395. if (expired != dentry)
  396. dput(dentry);
  397. spin_lock(&sbi->fs_lock);
  398. goto found;
  399. next:
  400. spin_lock(&sbi->fs_lock);
  401. ino->flags &= ~AUTOFS_INF_WANT_EXPIRE;
  402. spin_unlock(&sbi->fs_lock);
  403. if (expired != dentry)
  404. dput(expired);
  405. }
  406. return NULL;
  407. found:
  408. pr_debug("returning %p %pd\n", expired, expired);
  409. ino->flags |= AUTOFS_INF_EXPIRING;
  410. init_completion(&ino->expire_complete);
  411. spin_unlock(&sbi->fs_lock);
  412. return expired;
  413. }
  414. int autofs_expire_wait(const struct path *path, int rcu_walk)
  415. {
  416. struct dentry *dentry = path->dentry;
  417. struct autofs_sb_info *sbi = autofs_sbi(dentry->d_sb);
  418. struct autofs_info *ino = autofs_dentry_ino(dentry);
  419. int status;
  420. int state;
  421. /* Block on any pending expire */
  422. if (!(ino->flags & AUTOFS_INF_WANT_EXPIRE))
  423. return 0;
  424. if (rcu_walk)
  425. return -ECHILD;
  426. retry:
  427. spin_lock(&sbi->fs_lock);
  428. state = ino->flags & (AUTOFS_INF_WANT_EXPIRE | AUTOFS_INF_EXPIRING);
  429. if (state == AUTOFS_INF_WANT_EXPIRE) {
  430. spin_unlock(&sbi->fs_lock);
  431. /*
  432. * Possibly being selected for expire, wait until
  433. * it's selected or not.
  434. */
  435. schedule_timeout_uninterruptible(HZ/10);
  436. goto retry;
  437. }
  438. if (state & AUTOFS_INF_EXPIRING) {
  439. spin_unlock(&sbi->fs_lock);
  440. pr_debug("waiting for expire %p name=%pd\n", dentry, dentry);
  441. status = autofs_wait(sbi, path, NFY_NONE);
  442. wait_for_completion(&ino->expire_complete);
  443. pr_debug("expire done status=%d\n", status);
  444. if (d_unhashed(dentry))
  445. return -EAGAIN;
  446. return status;
  447. }
  448. spin_unlock(&sbi->fs_lock);
  449. return 0;
  450. }
  451. /* Perform an expiry operation */
  452. int autofs_expire_run(struct super_block *sb,
  453. struct vfsmount *mnt,
  454. struct autofs_sb_info *sbi,
  455. struct autofs_packet_expire __user *pkt_p)
  456. {
  457. struct autofs_packet_expire pkt;
  458. struct autofs_info *ino;
  459. struct dentry *dentry;
  460. int ret = 0;
  461. memset(&pkt, 0, sizeof(pkt));
  462. pkt.hdr.proto_version = sbi->version;
  463. pkt.hdr.type = autofs_ptype_expire;
  464. dentry = autofs_expire_indirect(sb, mnt, sbi, 0);
  465. if (!dentry)
  466. return -EAGAIN;
  467. pkt.len = dentry->d_name.len;
  468. memcpy(pkt.name, dentry->d_name.name, pkt.len);
  469. pkt.name[pkt.len] = '\0';
  470. if (copy_to_user(pkt_p, &pkt, sizeof(struct autofs_packet_expire)))
  471. ret = -EFAULT;
  472. spin_lock(&sbi->fs_lock);
  473. ino = autofs_dentry_ino(dentry);
  474. /* avoid rapid-fire expire attempts if expiry fails */
  475. ino->last_used = jiffies;
  476. ino->flags &= ~(AUTOFS_INF_EXPIRING|AUTOFS_INF_WANT_EXPIRE);
  477. complete_all(&ino->expire_complete);
  478. spin_unlock(&sbi->fs_lock);
  479. dput(dentry);
  480. return ret;
  481. }
  482. int autofs_do_expire_multi(struct super_block *sb, struct vfsmount *mnt,
  483. struct autofs_sb_info *sbi, unsigned int how)
  484. {
  485. struct dentry *dentry;
  486. int ret = -EAGAIN;
  487. if (autofs_type_trigger(sbi->type))
  488. dentry = autofs_expire_direct(sb, mnt, sbi, how);
  489. else
  490. dentry = autofs_expire_indirect(sb, mnt, sbi, how);
  491. if (dentry) {
  492. struct autofs_info *ino = autofs_dentry_ino(dentry);
  493. const struct path path = { .mnt = mnt, .dentry = dentry };
  494. /* This is synchronous because it makes the daemon a
  495. * little easier
  496. */
  497. ret = autofs_wait(sbi, &path, NFY_EXPIRE);
  498. spin_lock(&sbi->fs_lock);
  499. /* avoid rapid-fire expire attempts if expiry fails */
  500. ino->last_used = jiffies;
  501. ino->flags &= ~(AUTOFS_INF_EXPIRING|AUTOFS_INF_WANT_EXPIRE);
  502. complete_all(&ino->expire_complete);
  503. spin_unlock(&sbi->fs_lock);
  504. dput(dentry);
  505. }
  506. return ret;
  507. }
  508. /*
  509. * Call repeatedly until it returns -EAGAIN, meaning there's nothing
  510. * more to be done.
  511. */
  512. int autofs_expire_multi(struct super_block *sb, struct vfsmount *mnt,
  513. struct autofs_sb_info *sbi, int __user *arg)
  514. {
  515. unsigned int how = 0;
  516. if (arg && get_user(how, arg))
  517. return -EFAULT;
  518. return autofs_do_expire_multi(sb, mnt, sbi, how);
  519. }