expire.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  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. /* update last_used here :-
  29. - obviously makes sense if it is in use now
  30. - less obviously, prevents rapid-fire expire
  31. attempts if expire fails the first time */
  32. ino->last_used = now;
  33. }
  34. return 1;
  35. }
  36. /* Check a mount point for busyness */
  37. static int autofs4_mount_busy(struct vfsmount *mnt, struct dentry *dentry)
  38. {
  39. struct dentry *top = dentry;
  40. struct path path = {.mnt = mnt, .dentry = dentry};
  41. int status = 1;
  42. DPRINTK("dentry %p %.*s",
  43. dentry, (int)dentry->d_name.len, dentry->d_name.name);
  44. path_get(&path);
  45. if (!follow_down_one(&path))
  46. goto done;
  47. if (is_autofs4_dentry(path.dentry)) {
  48. struct autofs_sb_info *sbi = autofs4_sbi(path.dentry->d_sb);
  49. /* This is an autofs submount, we can't expire it */
  50. if (autofs_type_indirect(sbi->type))
  51. goto done;
  52. /*
  53. * Otherwise it's an offset mount and we need to check
  54. * if we can umount its mount, if there is one.
  55. */
  56. if (!d_mountpoint(path.dentry)) {
  57. status = 0;
  58. goto done;
  59. }
  60. }
  61. /* Update the expiry counter if fs is busy */
  62. if (!may_umount_tree(path.mnt)) {
  63. struct autofs_info *ino = autofs4_dentry_ino(top);
  64. ino->last_used = jiffies;
  65. goto done;
  66. }
  67. status = 0;
  68. done:
  69. DPRINTK("returning = %d", status);
  70. path_put(&path);
  71. return status;
  72. }
  73. /*
  74. * Calculate and dget next entry in top down tree traversal.
  75. */
  76. static struct dentry *get_next_positive_dentry(struct dentry *prev,
  77. struct dentry *root)
  78. {
  79. struct list_head *next;
  80. struct dentry *p, *ret;
  81. if (prev == NULL)
  82. return dget(root);
  83. spin_lock(&autofs4_lock);
  84. relock:
  85. p = prev;
  86. spin_lock(&p->d_lock);
  87. again:
  88. next = p->d_subdirs.next;
  89. if (next == &p->d_subdirs) {
  90. while (1) {
  91. struct dentry *parent;
  92. if (p == root) {
  93. spin_unlock(&p->d_lock);
  94. spin_unlock(&autofs4_lock);
  95. dput(prev);
  96. return NULL;
  97. }
  98. parent = p->d_parent;
  99. if (!spin_trylock(&parent->d_lock)) {
  100. spin_unlock(&p->d_lock);
  101. cpu_relax();
  102. goto relock;
  103. }
  104. spin_unlock(&p->d_lock);
  105. next = p->d_u.d_child.next;
  106. p = parent;
  107. if (next != &parent->d_subdirs)
  108. break;
  109. }
  110. }
  111. ret = list_entry(next, struct dentry, d_u.d_child);
  112. spin_lock_nested(&ret->d_lock, DENTRY_D_LOCK_NESTED);
  113. /* Negative dentry - try next */
  114. if (!simple_positive(ret)) {
  115. spin_unlock(&p->d_lock);
  116. p = ret;
  117. goto again;
  118. }
  119. dget_dlock(ret);
  120. spin_unlock(&ret->d_lock);
  121. spin_unlock(&p->d_lock);
  122. spin_unlock(&autofs4_lock);
  123. dput(prev);
  124. return ret;
  125. }
  126. /*
  127. * Check a direct mount point for busyness.
  128. * Direct mounts have similar expiry semantics to tree mounts.
  129. * The tree is not busy iff no mountpoints are busy and there are no
  130. * autofs submounts.
  131. */
  132. static int autofs4_direct_busy(struct vfsmount *mnt,
  133. struct dentry *top,
  134. unsigned long timeout,
  135. int do_now)
  136. {
  137. DPRINTK("top %p %.*s",
  138. top, (int) top->d_name.len, top->d_name.name);
  139. /* If it's busy update the expiry counters */
  140. if (!may_umount_tree(mnt)) {
  141. struct autofs_info *ino = autofs4_dentry_ino(top);
  142. if (ino)
  143. ino->last_used = jiffies;
  144. return 1;
  145. }
  146. /* Timeout of a direct mount is determined by its top dentry */
  147. if (!autofs4_can_expire(top, timeout, do_now))
  148. return 1;
  149. return 0;
  150. }
  151. /* Check a directory tree of mount points for busyness
  152. * The tree is not busy iff no mountpoints are busy
  153. */
  154. static int autofs4_tree_busy(struct vfsmount *mnt,
  155. struct dentry *top,
  156. unsigned long timeout,
  157. int do_now)
  158. {
  159. struct autofs_info *top_ino = autofs4_dentry_ino(top);
  160. struct dentry *p;
  161. DPRINTK("top %p %.*s",
  162. top, (int)top->d_name.len, top->d_name.name);
  163. /* Negative dentry - give up */
  164. if (!simple_positive(top))
  165. return 1;
  166. p = NULL;
  167. while ((p = get_next_positive_dentry(p, top))) {
  168. DPRINTK("dentry %p %.*s",
  169. p, (int) p->d_name.len, p->d_name.name);
  170. /*
  171. * Is someone visiting anywhere in the subtree ?
  172. * If there's no mount we need to check the usage
  173. * count for the autofs dentry.
  174. * If the fs is busy update the expiry counter.
  175. */
  176. if (d_mountpoint(p)) {
  177. if (autofs4_mount_busy(mnt, p)) {
  178. top_ino->last_used = jiffies;
  179. dput(p);
  180. return 1;
  181. }
  182. } else {
  183. struct autofs_info *ino = autofs4_dentry_ino(p);
  184. unsigned int ino_count = atomic_read(&ino->count);
  185. /*
  186. * Clean stale dentries below that have not been
  187. * invalidated after a mount fail during lookup
  188. */
  189. d_invalidate(p);
  190. /* allow for dget above and top is already dgot */
  191. if (p == top)
  192. ino_count += 2;
  193. else
  194. ino_count++;
  195. if (p->d_count > ino_count) {
  196. top_ino->last_used = jiffies;
  197. dput(p);
  198. return 1;
  199. }
  200. }
  201. }
  202. /* Timeout of a tree mount is ultimately determined by its top dentry */
  203. if (!autofs4_can_expire(top, timeout, do_now))
  204. return 1;
  205. return 0;
  206. }
  207. static struct dentry *autofs4_check_leaves(struct vfsmount *mnt,
  208. struct dentry *parent,
  209. unsigned long timeout,
  210. int do_now)
  211. {
  212. struct dentry *p;
  213. DPRINTK("parent %p %.*s",
  214. parent, (int)parent->d_name.len, parent->d_name.name);
  215. p = NULL;
  216. while ((p = get_next_positive_dentry(p, parent))) {
  217. DPRINTK("dentry %p %.*s",
  218. p, (int) p->d_name.len, p->d_name.name);
  219. if (d_mountpoint(p)) {
  220. /* Can we umount this guy */
  221. if (autofs4_mount_busy(mnt, p))
  222. continue;
  223. /* Can we expire this guy */
  224. if (autofs4_can_expire(p, timeout, do_now))
  225. return p;
  226. }
  227. }
  228. return NULL;
  229. }
  230. /* Check if we can expire a direct mount (possibly a tree) */
  231. struct dentry *autofs4_expire_direct(struct super_block *sb,
  232. struct vfsmount *mnt,
  233. struct autofs_sb_info *sbi,
  234. int how)
  235. {
  236. unsigned long timeout;
  237. struct dentry *root = dget(sb->s_root);
  238. int do_now = how & AUTOFS_EXP_IMMEDIATE;
  239. struct autofs_info *ino;
  240. if (!root)
  241. return NULL;
  242. now = jiffies;
  243. timeout = sbi->exp_timeout;
  244. spin_lock(&sbi->fs_lock);
  245. ino = autofs4_dentry_ino(root);
  246. /* No point expiring a pending mount */
  247. if (ino->flags & AUTOFS_INF_PENDING) {
  248. spin_unlock(&sbi->fs_lock);
  249. return NULL;
  250. }
  251. managed_dentry_set_transit(root);
  252. if (!autofs4_direct_busy(mnt, root, timeout, do_now)) {
  253. struct autofs_info *ino = autofs4_dentry_ino(root);
  254. ino->flags |= AUTOFS_INF_EXPIRING;
  255. init_completion(&ino->expire_complete);
  256. spin_unlock(&sbi->fs_lock);
  257. return root;
  258. }
  259. managed_dentry_clear_transit(root);
  260. spin_unlock(&sbi->fs_lock);
  261. dput(root);
  262. return NULL;
  263. }
  264. /*
  265. * Find an eligible tree to time-out
  266. * A tree is eligible if :-
  267. * - it is unused by any user process
  268. * - it has been unused for exp_timeout time
  269. */
  270. struct dentry *autofs4_expire_indirect(struct super_block *sb,
  271. struct vfsmount *mnt,
  272. struct autofs_sb_info *sbi,
  273. int how)
  274. {
  275. unsigned long timeout;
  276. struct dentry *root = sb->s_root;
  277. struct dentry *dentry;
  278. struct dentry *expired = NULL;
  279. int do_now = how & AUTOFS_EXP_IMMEDIATE;
  280. int exp_leaves = how & AUTOFS_EXP_LEAVES;
  281. struct autofs_info *ino;
  282. unsigned int ino_count;
  283. if (!root)
  284. return NULL;
  285. now = jiffies;
  286. timeout = sbi->exp_timeout;
  287. dentry = NULL;
  288. while ((dentry = get_next_positive_dentry(dentry, root))) {
  289. spin_lock(&sbi->fs_lock);
  290. ino = autofs4_dentry_ino(dentry);
  291. /* No point expiring a pending mount */
  292. if (ino->flags & AUTOFS_INF_PENDING)
  293. goto cont;
  294. managed_dentry_set_transit(dentry);
  295. /*
  296. * Case 1: (i) indirect mount or top level pseudo direct mount
  297. * (autofs-4.1).
  298. * (ii) indirect mount with offset mount, check the "/"
  299. * offset (autofs-5.0+).
  300. */
  301. if (d_mountpoint(dentry)) {
  302. DPRINTK("checking mountpoint %p %.*s",
  303. dentry, (int)dentry->d_name.len, dentry->d_name.name);
  304. /* Path walk currently on this dentry? */
  305. ino_count = atomic_read(&ino->count) + 2;
  306. if (dentry->d_count > ino_count)
  307. goto next;
  308. /* Can we umount this guy */
  309. if (autofs4_mount_busy(mnt, dentry))
  310. goto next;
  311. /* Can we expire this guy */
  312. if (autofs4_can_expire(dentry, timeout, do_now)) {
  313. expired = dentry;
  314. goto found;
  315. }
  316. goto next;
  317. }
  318. if (simple_empty(dentry))
  319. goto next;
  320. /* Case 2: tree mount, expire iff entire tree is not busy */
  321. if (!exp_leaves) {
  322. /* Path walk currently on this dentry? */
  323. ino_count = atomic_read(&ino->count) + 1;
  324. if (dentry->d_count > ino_count)
  325. goto next;
  326. if (!autofs4_tree_busy(mnt, dentry, timeout, do_now)) {
  327. expired = dentry;
  328. goto found;
  329. }
  330. /*
  331. * Case 3: pseudo direct mount, expire individual leaves
  332. * (autofs-4.1).
  333. */
  334. } else {
  335. /* Path walk currently on this dentry? */
  336. ino_count = atomic_read(&ino->count) + 1;
  337. if (dentry->d_count > ino_count)
  338. goto next;
  339. expired = autofs4_check_leaves(mnt, dentry, timeout, do_now);
  340. if (expired) {
  341. dput(dentry);
  342. goto found;
  343. }
  344. }
  345. next:
  346. managed_dentry_clear_transit(dentry);
  347. cont:
  348. spin_unlock(&sbi->fs_lock);
  349. }
  350. return NULL;
  351. found:
  352. DPRINTK("returning %p %.*s",
  353. expired, (int)expired->d_name.len, expired->d_name.name);
  354. ino = autofs4_dentry_ino(expired);
  355. ino->flags |= AUTOFS_INF_EXPIRING;
  356. init_completion(&ino->expire_complete);
  357. spin_unlock(&sbi->fs_lock);
  358. spin_lock(&autofs4_lock);
  359. spin_lock(&expired->d_parent->d_lock);
  360. spin_lock_nested(&expired->d_lock, DENTRY_D_LOCK_NESTED);
  361. list_move(&expired->d_parent->d_subdirs, &expired->d_u.d_child);
  362. spin_unlock(&expired->d_lock);
  363. spin_unlock(&expired->d_parent->d_lock);
  364. spin_unlock(&autofs4_lock);
  365. return expired;
  366. }
  367. int autofs4_expire_wait(struct dentry *dentry)
  368. {
  369. struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
  370. struct autofs_info *ino = autofs4_dentry_ino(dentry);
  371. int status;
  372. /* Block on any pending expire */
  373. spin_lock(&sbi->fs_lock);
  374. if (ino->flags & AUTOFS_INF_EXPIRING) {
  375. spin_unlock(&sbi->fs_lock);
  376. DPRINTK("waiting for expire %p name=%.*s",
  377. dentry, dentry->d_name.len, dentry->d_name.name);
  378. status = autofs4_wait(sbi, dentry, NFY_NONE);
  379. wait_for_completion(&ino->expire_complete);
  380. DPRINTK("expire done status=%d", status);
  381. if (d_unhashed(dentry))
  382. return -EAGAIN;
  383. return status;
  384. }
  385. spin_unlock(&sbi->fs_lock);
  386. return 0;
  387. }
  388. /* Perform an expiry operation */
  389. int autofs4_expire_run(struct super_block *sb,
  390. struct vfsmount *mnt,
  391. struct autofs_sb_info *sbi,
  392. struct autofs_packet_expire __user *pkt_p)
  393. {
  394. struct autofs_packet_expire pkt;
  395. struct autofs_info *ino;
  396. struct dentry *dentry;
  397. int ret = 0;
  398. memset(&pkt,0,sizeof pkt);
  399. pkt.hdr.proto_version = sbi->version;
  400. pkt.hdr.type = autofs_ptype_expire;
  401. if ((dentry = autofs4_expire_indirect(sb, mnt, sbi, 0)) == NULL)
  402. return -EAGAIN;
  403. pkt.len = dentry->d_name.len;
  404. memcpy(pkt.name, dentry->d_name.name, pkt.len);
  405. pkt.name[pkt.len] = '\0';
  406. dput(dentry);
  407. if ( copy_to_user(pkt_p, &pkt, sizeof(struct autofs_packet_expire)) )
  408. ret = -EFAULT;
  409. spin_lock(&sbi->fs_lock);
  410. ino = autofs4_dentry_ino(dentry);
  411. ino->flags &= ~AUTOFS_INF_EXPIRING;
  412. if (!d_unhashed(dentry))
  413. managed_dentry_clear_transit(dentry);
  414. complete_all(&ino->expire_complete);
  415. spin_unlock(&sbi->fs_lock);
  416. return ret;
  417. }
  418. int autofs4_do_expire_multi(struct super_block *sb, struct vfsmount *mnt,
  419. struct autofs_sb_info *sbi, int when)
  420. {
  421. struct dentry *dentry;
  422. int ret = -EAGAIN;
  423. if (autofs_type_trigger(sbi->type))
  424. dentry = autofs4_expire_direct(sb, mnt, sbi, when);
  425. else
  426. dentry = autofs4_expire_indirect(sb, mnt, sbi, when);
  427. if (dentry) {
  428. struct autofs_info *ino = autofs4_dentry_ino(dentry);
  429. /* This is synchronous because it makes the daemon a
  430. little easier */
  431. ret = autofs4_wait(sbi, dentry, NFY_EXPIRE);
  432. spin_lock(&sbi->fs_lock);
  433. ino->flags &= ~AUTOFS_INF_EXPIRING;
  434. spin_lock(&dentry->d_lock);
  435. if (ret)
  436. __managed_dentry_clear_transit(dentry);
  437. else {
  438. if ((IS_ROOT(dentry) ||
  439. (autofs_type_indirect(sbi->type) &&
  440. IS_ROOT(dentry->d_parent))) &&
  441. !(dentry->d_flags & DCACHE_NEED_AUTOMOUNT))
  442. __managed_dentry_set_automount(dentry);
  443. }
  444. spin_unlock(&dentry->d_lock);
  445. complete_all(&ino->expire_complete);
  446. spin_unlock(&sbi->fs_lock);
  447. dput(dentry);
  448. }
  449. return ret;
  450. }
  451. /* Call repeatedly until it returns -EAGAIN, meaning there's nothing
  452. more to be done */
  453. int autofs4_expire_multi(struct super_block *sb, struct vfsmount *mnt,
  454. struct autofs_sb_info *sbi, int __user *arg)
  455. {
  456. int do_now = 0;
  457. if (arg && get_user(do_now, arg))
  458. return -EFAULT;
  459. return autofs4_do_expire_multi(sb, mnt, sbi, do_now);
  460. }