expire.c 8.7 KB

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