expire.c 15 KB

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