expire.c 8.4 KB

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