expire.c 12 KB

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