expire.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  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. mntput(mnt);
  63. dput(dentry);
  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. static 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. /* Lock the tree as we must expire as a whole */
  220. spin_lock(&sbi->fs_lock);
  221. if (!autofs4_direct_busy(mnt, root, timeout, do_now)) {
  222. struct autofs_info *ino = autofs4_dentry_ino(root);
  223. /* Set this flag early to catch sys_chdir and the like */
  224. ino->flags |= AUTOFS_INF_EXPIRING;
  225. spin_unlock(&sbi->fs_lock);
  226. return root;
  227. }
  228. spin_unlock(&sbi->fs_lock);
  229. dput(root);
  230. return NULL;
  231. }
  232. /*
  233. * Find an eligible tree to time-out
  234. * A tree is eligible if :-
  235. * - it is unused by any user process
  236. * - it has been unused for exp_timeout time
  237. */
  238. static struct dentry *autofs4_expire_indirect(struct super_block *sb,
  239. struct vfsmount *mnt,
  240. struct autofs_sb_info *sbi,
  241. int how)
  242. {
  243. unsigned long timeout;
  244. struct dentry *root = sb->s_root;
  245. struct dentry *expired = NULL;
  246. struct list_head *next;
  247. int do_now = how & AUTOFS_EXP_IMMEDIATE;
  248. int exp_leaves = how & AUTOFS_EXP_LEAVES;
  249. if (!root)
  250. return NULL;
  251. now = jiffies;
  252. timeout = sbi->exp_timeout;
  253. spin_lock(&dcache_lock);
  254. next = root->d_subdirs.next;
  255. /* On exit from the loop expire is set to a dgot dentry
  256. * to expire or it's NULL */
  257. while ( next != &root->d_subdirs ) {
  258. struct dentry *dentry = list_entry(next, struct dentry, d_u.d_child);
  259. /* Negative dentry - give up */
  260. if (!simple_positive(dentry)) {
  261. next = next->next;
  262. continue;
  263. }
  264. dentry = dget(dentry);
  265. spin_unlock(&dcache_lock);
  266. /*
  267. * Case 1: (i) indirect mount or top level pseudo direct mount
  268. * (autofs-4.1).
  269. * (ii) indirect mount with offset mount, check the "/"
  270. * offset (autofs-5.0+).
  271. */
  272. if (d_mountpoint(dentry)) {
  273. DPRINTK("checking mountpoint %p %.*s",
  274. dentry, (int)dentry->d_name.len, dentry->d_name.name);
  275. /* Can we umount this guy */
  276. if (autofs4_mount_busy(mnt, dentry))
  277. goto next;
  278. /* Can we expire this guy */
  279. if (autofs4_can_expire(dentry, timeout, do_now)) {
  280. expired = dentry;
  281. break;
  282. }
  283. goto next;
  284. }
  285. if (simple_empty(dentry))
  286. goto next;
  287. /* Case 2: tree mount, expire iff entire tree is not busy */
  288. if (!exp_leaves) {
  289. /* Lock the tree as we must expire as a whole */
  290. spin_lock(&sbi->fs_lock);
  291. if (!autofs4_tree_busy(mnt, dentry, timeout, do_now)) {
  292. struct autofs_info *inf = autofs4_dentry_ino(dentry);
  293. /* Set this flag early to catch sys_chdir and the like */
  294. inf->flags |= AUTOFS_INF_EXPIRING;
  295. spin_unlock(&sbi->fs_lock);
  296. expired = dentry;
  297. break;
  298. }
  299. spin_unlock(&sbi->fs_lock);
  300. /*
  301. * Case 3: pseudo direct mount, expire individual leaves
  302. * (autofs-4.1).
  303. */
  304. } else {
  305. expired = autofs4_check_leaves(mnt, dentry, timeout, do_now);
  306. if (expired) {
  307. dput(dentry);
  308. break;
  309. }
  310. }
  311. next:
  312. dput(dentry);
  313. spin_lock(&dcache_lock);
  314. next = next->next;
  315. }
  316. if (expired) {
  317. DPRINTK("returning %p %.*s",
  318. expired, (int)expired->d_name.len, expired->d_name.name);
  319. spin_lock(&dcache_lock);
  320. list_move(&expired->d_parent->d_subdirs, &expired->d_u.d_child);
  321. spin_unlock(&dcache_lock);
  322. return expired;
  323. }
  324. spin_unlock(&dcache_lock);
  325. return NULL;
  326. }
  327. /* Perform an expiry operation */
  328. int autofs4_expire_run(struct super_block *sb,
  329. struct vfsmount *mnt,
  330. struct autofs_sb_info *sbi,
  331. struct autofs_packet_expire __user *pkt_p)
  332. {
  333. struct autofs_packet_expire pkt;
  334. struct dentry *dentry;
  335. memset(&pkt,0,sizeof pkt);
  336. pkt.hdr.proto_version = sbi->version;
  337. pkt.hdr.type = autofs_ptype_expire;
  338. if ((dentry = autofs4_expire_indirect(sb, mnt, sbi, 0)) == NULL)
  339. return -EAGAIN;
  340. pkt.len = dentry->d_name.len;
  341. memcpy(pkt.name, dentry->d_name.name, pkt.len);
  342. pkt.name[pkt.len] = '\0';
  343. dput(dentry);
  344. if ( copy_to_user(pkt_p, &pkt, sizeof(struct autofs_packet_expire)) )
  345. return -EFAULT;
  346. return 0;
  347. }
  348. /* Call repeatedly until it returns -EAGAIN, meaning there's nothing
  349. more to be done */
  350. int autofs4_expire_multi(struct super_block *sb, struct vfsmount *mnt,
  351. struct autofs_sb_info *sbi, int __user *arg)
  352. {
  353. struct dentry *dentry;
  354. int ret = -EAGAIN;
  355. int do_now = 0;
  356. if (arg && get_user(do_now, arg))
  357. return -EFAULT;
  358. if (sbi->type & AUTOFS_TYPE_DIRECT)
  359. dentry = autofs4_expire_direct(sb, mnt, sbi, do_now);
  360. else
  361. dentry = autofs4_expire_indirect(sb, mnt, sbi, do_now);
  362. if (dentry) {
  363. struct autofs_info *ino = autofs4_dentry_ino(dentry);
  364. /* This is synchronous because it makes the daemon a
  365. little easier */
  366. ino->flags |= AUTOFS_INF_EXPIRING;
  367. ret = autofs4_wait(sbi, dentry, NFY_EXPIRE);
  368. ino->flags &= ~AUTOFS_INF_EXPIRING;
  369. dput(dentry);
  370. }
  371. return ret;
  372. }