expire.c 13 KB

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