expire.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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 (!follow_down(&mnt, &dentry))
  48. goto done;
  49. while (d_mountpoint(dentry) && follow_down(&mnt, &dentry))
  50. ;
  51. /* This is an autofs submount, we can't expire it */
  52. if (is_autofs4_dentry(dentry))
  53. goto done;
  54. /* The big question */
  55. if (may_umount_tree(mnt) == 0)
  56. status = 1;
  57. done:
  58. DPRINTK("returning = %d", status);
  59. mntput(mnt);
  60. dput(dentry);
  61. return status;
  62. }
  63. /* Check a directory tree of mount points for busyness
  64. * The tree is not busy iff no mountpoints are busy
  65. * Return 1 if the tree is busy or 0 otherwise
  66. */
  67. static int autofs4_check_tree(struct vfsmount *mnt,
  68. struct dentry *top,
  69. unsigned long timeout,
  70. int do_now)
  71. {
  72. struct dentry *this_parent = top;
  73. struct list_head *next;
  74. DPRINTK("parent %p %.*s",
  75. top, (int)top->d_name.len, top->d_name.name);
  76. /* Negative dentry - give up */
  77. if (!simple_positive(top))
  78. return 0;
  79. /* Timeout of a tree mount is determined by its top dentry */
  80. if (!autofs4_can_expire(top, timeout, do_now))
  81. return 0;
  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_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_check_mount(mnt, dentry)) {
  104. dput(dentry);
  105. return 0;
  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_child.next;
  114. this_parent = this_parent->d_parent;
  115. goto resume;
  116. }
  117. spin_unlock(&dcache_lock);
  118. return 1;
  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_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_check_mount(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_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_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_check_mount(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. if (autofs4_check_tree(mnt, dentry, timeout, do_now)) {
  222. expired = dentry;
  223. break;
  224. }
  225. /* Case 3: direct mount, expire individual leaves */
  226. } else {
  227. expired = autofs4_check_leaves(mnt, dentry, timeout, do_now);
  228. if (expired) {
  229. dput(dentry);
  230. break;
  231. }
  232. }
  233. next:
  234. dput(dentry);
  235. spin_lock(&dcache_lock);
  236. next = next->next;
  237. }
  238. if ( expired ) {
  239. DPRINTK("returning %p %.*s",
  240. expired, (int)expired->d_name.len, expired->d_name.name);
  241. spin_lock(&dcache_lock);
  242. list_del(&expired->d_parent->d_subdirs);
  243. list_add(&expired->d_parent->d_subdirs, &expired->d_child);
  244. spin_unlock(&dcache_lock);
  245. return expired;
  246. }
  247. spin_unlock(&dcache_lock);
  248. return NULL;
  249. }
  250. /* Perform an expiry operation */
  251. int autofs4_expire_run(struct super_block *sb,
  252. struct vfsmount *mnt,
  253. struct autofs_sb_info *sbi,
  254. struct autofs_packet_expire __user *pkt_p)
  255. {
  256. struct autofs_packet_expire pkt;
  257. struct dentry *dentry;
  258. memset(&pkt,0,sizeof pkt);
  259. pkt.hdr.proto_version = sbi->version;
  260. pkt.hdr.type = autofs_ptype_expire;
  261. if ((dentry = autofs4_expire(sb, mnt, sbi, 0)) == NULL)
  262. return -EAGAIN;
  263. pkt.len = dentry->d_name.len;
  264. memcpy(pkt.name, dentry->d_name.name, pkt.len);
  265. pkt.name[pkt.len] = '\0';
  266. dput(dentry);
  267. if ( copy_to_user(pkt_p, &pkt, sizeof(struct autofs_packet_expire)) )
  268. return -EFAULT;
  269. return 0;
  270. }
  271. /* Call repeatedly until it returns -EAGAIN, meaning there's nothing
  272. more to be done */
  273. int autofs4_expire_multi(struct super_block *sb, struct vfsmount *mnt,
  274. struct autofs_sb_info *sbi, int __user *arg)
  275. {
  276. struct dentry *dentry;
  277. int ret = -EAGAIN;
  278. int do_now = 0;
  279. if (arg && get_user(do_now, arg))
  280. return -EFAULT;
  281. if ((dentry = autofs4_expire(sb, mnt, sbi, do_now)) != NULL) {
  282. struct autofs_info *de_info = autofs4_dentry_ino(dentry);
  283. /* This is synchronous because it makes the daemon a
  284. little easier */
  285. de_info->flags |= AUTOFS_INF_EXPIRING;
  286. ret = autofs4_wait(sbi, dentry, NFY_EXPIRE);
  287. de_info->flags &= ~AUTOFS_INF_EXPIRING;
  288. dput(dentry);
  289. }
  290. return ret;
  291. }