expire.c 8.7 KB

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