expire.c 8.5 KB

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