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