expire.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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-2003 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 (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. int status = 1;
  43. DPRINTK("dentry %p %.*s",
  44. dentry, (int)dentry->d_name.len, dentry->d_name.name);
  45. mntget(mnt);
  46. dget(dentry);
  47. if (!autofs4_follow_mount(&mnt, &dentry))
  48. goto done;
  49. /* This is an autofs submount, we can't expire it */
  50. if (is_autofs4_dentry(dentry))
  51. goto done;
  52. /* The big question */
  53. if (may_umount_tree(mnt) == 0)
  54. status = 0;
  55. done:
  56. DPRINTK("returning = %d", status);
  57. mntput(mnt);
  58. dput(dentry);
  59. return status;
  60. }
  61. /*
  62. * Calculate next entry in top down tree traversal.
  63. * From next_mnt in namespace.c - elegant.
  64. */
  65. static struct dentry *next_dentry(struct dentry *p, struct dentry *root)
  66. {
  67. struct list_head *next = p->d_subdirs.next;
  68. if (next == &p->d_subdirs) {
  69. while (1) {
  70. if (p == root)
  71. return NULL;
  72. next = p->d_u.d_child.next;
  73. if (next != &p->d_parent->d_subdirs)
  74. break;
  75. p = p->d_parent;
  76. }
  77. }
  78. return list_entry(next, struct dentry, d_u.d_child);
  79. }
  80. /* Check a directory tree of mount points for busyness
  81. * The tree is not busy iff no mountpoints are busy
  82. */
  83. static int autofs4_tree_busy(struct vfsmount *mnt,
  84. struct dentry *top,
  85. unsigned long timeout,
  86. int do_now)
  87. {
  88. struct dentry *p;
  89. DPRINTK("top %p %.*s",
  90. top, (int)top->d_name.len, top->d_name.name);
  91. /* Negative dentry - give up */
  92. if (!simple_positive(top))
  93. return 1;
  94. /* Timeout of a tree mount is determined by its top dentry */
  95. if (!autofs4_can_expire(top, timeout, do_now))
  96. return 1;
  97. /* Is someone visiting anywhere in the tree ? */
  98. if (may_umount_tree(mnt))
  99. return 1;
  100. spin_lock(&dcache_lock);
  101. for (p = top; p; p = next_dentry(p, top)) {
  102. /* Negative dentry - give up */
  103. if (!simple_positive(p))
  104. continue;
  105. DPRINTK("dentry %p %.*s",
  106. p, (int) p->d_name.len, p->d_name.name);
  107. p = dget(p);
  108. spin_unlock(&dcache_lock);
  109. if (d_mountpoint(p)) {
  110. /* First busy => tree busy */
  111. if (autofs4_mount_busy(mnt, p)) {
  112. dput(p);
  113. return 1;
  114. }
  115. }
  116. dput(p);
  117. spin_lock(&dcache_lock);
  118. }
  119. spin_unlock(&dcache_lock);
  120. return 0;
  121. }
  122. static struct dentry *autofs4_check_leaves(struct vfsmount *mnt,
  123. struct dentry *parent,
  124. unsigned long timeout,
  125. int do_now)
  126. {
  127. struct dentry *p;
  128. DPRINTK("parent %p %.*s",
  129. parent, (int)parent->d_name.len, parent->d_name.name);
  130. spin_lock(&dcache_lock);
  131. for (p = parent; p; p = next_dentry(p, parent)) {
  132. /* Negative dentry - give up */
  133. if (!simple_positive(p))
  134. continue;
  135. DPRINTK("dentry %p %.*s",
  136. p, (int) p->d_name.len, p->d_name.name);
  137. p = dget(p);
  138. spin_unlock(&dcache_lock);
  139. if (d_mountpoint(p)) {
  140. /* Can we expire this guy */
  141. if (!autofs4_can_expire(p, timeout, do_now))
  142. goto cont;
  143. /* Can we umount this guy */
  144. if (!autofs4_mount_busy(mnt, p))
  145. return p;
  146. }
  147. cont:
  148. dput(p);
  149. spin_lock(&dcache_lock);
  150. }
  151. spin_unlock(&dcache_lock);
  152. return NULL;
  153. }
  154. /*
  155. * Find an eligible tree to time-out
  156. * A tree is eligible if :-
  157. * - it is unused by any user process
  158. * - it has been unused for exp_timeout time
  159. */
  160. static struct dentry *autofs4_expire(struct super_block *sb,
  161. struct vfsmount *mnt,
  162. struct autofs_sb_info *sbi,
  163. int how)
  164. {
  165. unsigned long timeout;
  166. struct dentry *root = sb->s_root;
  167. struct dentry *expired = NULL;
  168. struct list_head *next;
  169. int do_now = how & AUTOFS_EXP_IMMEDIATE;
  170. int exp_leaves = how & AUTOFS_EXP_LEAVES;
  171. if ( !sbi->exp_timeout || !root )
  172. return NULL;
  173. now = jiffies;
  174. timeout = sbi->exp_timeout;
  175. spin_lock(&dcache_lock);
  176. next = root->d_subdirs.next;
  177. /* On exit from the loop expire is set to a dgot dentry
  178. * to expire or it's NULL */
  179. while ( next != &root->d_subdirs ) {
  180. struct dentry *dentry = list_entry(next, struct dentry, d_u.d_child);
  181. /* Negative dentry - give up */
  182. if (!simple_positive(dentry)) {
  183. next = next->next;
  184. continue;
  185. }
  186. dentry = dget(dentry);
  187. spin_unlock(&dcache_lock);
  188. /* Case 1: indirect mount or top level direct mount */
  189. if (d_mountpoint(dentry)) {
  190. DPRINTK("checking mountpoint %p %.*s",
  191. dentry, (int)dentry->d_name.len, dentry->d_name.name);
  192. /* Can we expire this guy */
  193. if (!autofs4_can_expire(dentry, timeout, do_now))
  194. goto next;
  195. /* Can we umount this guy */
  196. if (!autofs4_mount_busy(mnt, dentry)) {
  197. expired = dentry;
  198. break;
  199. }
  200. goto next;
  201. }
  202. if (simple_empty(dentry))
  203. goto next;
  204. /* Case 2: tree mount, expire iff entire tree is not busy */
  205. if (!exp_leaves) {
  206. /* Lock the tree as we must expire as a whole */
  207. spin_lock(&sbi->fs_lock);
  208. if (!autofs4_tree_busy(mnt, dentry, timeout, do_now)) {
  209. struct autofs_info *inf = autofs4_dentry_ino(dentry);
  210. /* Set this flag early to catch sys_chdir and the like */
  211. inf->flags |= AUTOFS_INF_EXPIRING;
  212. spin_unlock(&sbi->fs_lock);
  213. expired = dentry;
  214. break;
  215. }
  216. spin_unlock(&sbi->fs_lock);
  217. /* Case 3: direct mount, expire individual leaves */
  218. } else {
  219. expired = autofs4_check_leaves(mnt, dentry, timeout, do_now);
  220. if (expired) {
  221. dput(dentry);
  222. break;
  223. }
  224. }
  225. next:
  226. dput(dentry);
  227. spin_lock(&dcache_lock);
  228. next = next->next;
  229. }
  230. if (expired) {
  231. DPRINTK("returning %p %.*s",
  232. expired, (int)expired->d_name.len, expired->d_name.name);
  233. spin_lock(&dcache_lock);
  234. list_del(&expired->d_parent->d_subdirs);
  235. list_add(&expired->d_parent->d_subdirs, &expired->d_u.d_child);
  236. spin_unlock(&dcache_lock);
  237. return expired;
  238. }
  239. spin_unlock(&dcache_lock);
  240. return NULL;
  241. }
  242. /* Perform an expiry operation */
  243. int autofs4_expire_run(struct super_block *sb,
  244. struct vfsmount *mnt,
  245. struct autofs_sb_info *sbi,
  246. struct autofs_packet_expire __user *pkt_p)
  247. {
  248. struct autofs_packet_expire pkt;
  249. struct dentry *dentry;
  250. memset(&pkt,0,sizeof pkt);
  251. pkt.hdr.proto_version = sbi->version;
  252. pkt.hdr.type = autofs_ptype_expire;
  253. if ((dentry = autofs4_expire(sb, mnt, sbi, 0)) == NULL)
  254. return -EAGAIN;
  255. pkt.len = dentry->d_name.len;
  256. memcpy(pkt.name, dentry->d_name.name, pkt.len);
  257. pkt.name[pkt.len] = '\0';
  258. dput(dentry);
  259. if ( copy_to_user(pkt_p, &pkt, sizeof(struct autofs_packet_expire)) )
  260. return -EFAULT;
  261. return 0;
  262. }
  263. /* Call repeatedly until it returns -EAGAIN, meaning there's nothing
  264. more to be done */
  265. int autofs4_expire_multi(struct super_block *sb, struct vfsmount *mnt,
  266. struct autofs_sb_info *sbi, int __user *arg)
  267. {
  268. struct dentry *dentry;
  269. int ret = -EAGAIN;
  270. int do_now = 0;
  271. if (arg && get_user(do_now, arg))
  272. return -EFAULT;
  273. if ((dentry = autofs4_expire(sb, mnt, sbi, do_now)) != NULL) {
  274. struct autofs_info *ino = autofs4_dentry_ino(dentry);
  275. /* This is synchronous because it makes the daemon a
  276. little easier */
  277. ino->flags |= AUTOFS_INF_EXPIRING;
  278. ret = autofs4_wait(sbi, dentry, NFY_EXPIRE);
  279. ino->flags &= ~AUTOFS_INF_EXPIRING;
  280. dput(dentry);
  281. }
  282. return ret;
  283. }