expire.c 14 KB

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