expire.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  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-2006 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. /*
  86. * Check a direct mount point for busyness.
  87. * Direct mounts have similar expiry semantics to tree mounts.
  88. * The tree is not busy iff no mountpoints are busy and there are no
  89. * autofs submounts.
  90. */
  91. static int autofs4_direct_busy(struct vfsmount *mnt,
  92. struct dentry *top,
  93. unsigned long timeout,
  94. int do_now)
  95. {
  96. DPRINTK("top %p %.*s",
  97. top, (int) top->d_name.len, top->d_name.name);
  98. /* Not a mountpoint - give up */
  99. if (!d_mountpoint(top))
  100. return 1;
  101. /* If it's busy update the expiry counters */
  102. if (!may_umount_tree(mnt)) {
  103. struct autofs_info *ino = autofs4_dentry_ino(top);
  104. if (ino)
  105. ino->last_used = jiffies;
  106. return 1;
  107. }
  108. /* Timeout of a direct mount is determined by its top dentry */
  109. if (!autofs4_can_expire(top, timeout, do_now))
  110. return 1;
  111. return 0;
  112. }
  113. /* Check a directory tree of mount points for busyness
  114. * The tree is not busy iff no mountpoints are busy
  115. */
  116. static int autofs4_tree_busy(struct vfsmount *mnt,
  117. struct dentry *top,
  118. unsigned long timeout,
  119. int do_now)
  120. {
  121. struct autofs_info *top_ino = autofs4_dentry_ino(top);
  122. struct dentry *p;
  123. DPRINTK("top %p %.*s",
  124. top, (int)top->d_name.len, top->d_name.name);
  125. /* Negative dentry - give up */
  126. if (!simple_positive(top))
  127. return 1;
  128. spin_lock(&dcache_lock);
  129. for (p = top; p; p = next_dentry(p, top)) {
  130. /* Negative dentry - give up */
  131. if (!simple_positive(p))
  132. continue;
  133. DPRINTK("dentry %p %.*s",
  134. p, (int) p->d_name.len, p->d_name.name);
  135. p = dget(p);
  136. spin_unlock(&dcache_lock);
  137. /*
  138. * Is someone visiting anywhere in the subtree ?
  139. * If there's no mount we need to check the usage
  140. * count for the autofs dentry.
  141. * If the fs is busy update the expiry counter.
  142. */
  143. if (d_mountpoint(p)) {
  144. if (autofs4_mount_busy(mnt, p)) {
  145. top_ino->last_used = jiffies;
  146. dput(p);
  147. return 1;
  148. }
  149. } else {
  150. struct autofs_info *ino = autofs4_dentry_ino(p);
  151. unsigned int ino_count = atomic_read(&ino->count);
  152. /* allow for dget above and top is already dgot */
  153. if (p == top)
  154. ino_count += 2;
  155. else
  156. ino_count++;
  157. if (atomic_read(&p->d_count) > ino_count) {
  158. top_ino->last_used = jiffies;
  159. dput(p);
  160. return 1;
  161. }
  162. }
  163. dput(p);
  164. spin_lock(&dcache_lock);
  165. }
  166. spin_unlock(&dcache_lock);
  167. /* Timeout of a tree mount is ultimately determined by its top dentry */
  168. if (!autofs4_can_expire(top, timeout, do_now))
  169. return 1;
  170. return 0;
  171. }
  172. static struct dentry *autofs4_check_leaves(struct vfsmount *mnt,
  173. struct dentry *parent,
  174. unsigned long timeout,
  175. int do_now)
  176. {
  177. struct dentry *p;
  178. DPRINTK("parent %p %.*s",
  179. parent, (int)parent->d_name.len, parent->d_name.name);
  180. spin_lock(&dcache_lock);
  181. for (p = parent; p; p = next_dentry(p, parent)) {
  182. /* Negative dentry - give up */
  183. if (!simple_positive(p))
  184. continue;
  185. DPRINTK("dentry %p %.*s",
  186. p, (int) p->d_name.len, p->d_name.name);
  187. p = dget(p);
  188. spin_unlock(&dcache_lock);
  189. if (d_mountpoint(p)) {
  190. /* Can we umount this guy */
  191. if (autofs4_mount_busy(mnt, p))
  192. goto cont;
  193. /* Can we expire this guy */
  194. if (autofs4_can_expire(p, timeout, do_now))
  195. return p;
  196. }
  197. cont:
  198. dput(p);
  199. spin_lock(&dcache_lock);
  200. }
  201. spin_unlock(&dcache_lock);
  202. return NULL;
  203. }
  204. /* Check if we can expire a direct mount (possibly a tree) */
  205. static struct dentry *autofs4_expire_direct(struct super_block *sb,
  206. struct vfsmount *mnt,
  207. struct autofs_sb_info *sbi,
  208. int how)
  209. {
  210. unsigned long timeout;
  211. struct dentry *root = dget(sb->s_root);
  212. int do_now = how & AUTOFS_EXP_IMMEDIATE;
  213. if (!sbi->exp_timeout || !root)
  214. return NULL;
  215. now = jiffies;
  216. timeout = sbi->exp_timeout;
  217. /* Lock the tree as we must expire as a whole */
  218. spin_lock(&sbi->fs_lock);
  219. if (!autofs4_direct_busy(mnt, root, timeout, do_now)) {
  220. struct autofs_info *ino = autofs4_dentry_ino(root);
  221. /* Set this flag early to catch sys_chdir and the like */
  222. ino->flags |= AUTOFS_INF_EXPIRING;
  223. spin_unlock(&sbi->fs_lock);
  224. return root;
  225. }
  226. spin_unlock(&sbi->fs_lock);
  227. dput(root);
  228. return NULL;
  229. }
  230. /*
  231. * Find an eligible tree to time-out
  232. * A tree is eligible if :-
  233. * - it is unused by any user process
  234. * - it has been unused for exp_timeout time
  235. */
  236. static struct dentry *autofs4_expire_indirect(struct super_block *sb,
  237. struct vfsmount *mnt,
  238. struct autofs_sb_info *sbi,
  239. int how)
  240. {
  241. unsigned long timeout;
  242. struct dentry *root = sb->s_root;
  243. struct dentry *expired = NULL;
  244. struct list_head *next;
  245. int do_now = how & AUTOFS_EXP_IMMEDIATE;
  246. int exp_leaves = how & AUTOFS_EXP_LEAVES;
  247. if ( !sbi->exp_timeout || !root )
  248. return NULL;
  249. now = jiffies;
  250. timeout = sbi->exp_timeout;
  251. spin_lock(&dcache_lock);
  252. next = root->d_subdirs.next;
  253. /* On exit from the loop expire is set to a dgot dentry
  254. * to expire or it's NULL */
  255. while ( next != &root->d_subdirs ) {
  256. struct dentry *dentry = list_entry(next, struct dentry, d_u.d_child);
  257. /* Negative dentry - give up */
  258. if (!simple_positive(dentry)) {
  259. next = next->next;
  260. continue;
  261. }
  262. dentry = dget(dentry);
  263. spin_unlock(&dcache_lock);
  264. /*
  265. * Case 1: (i) indirect mount or top level pseudo direct mount
  266. * (autofs-4.1).
  267. * (ii) indirect mount with offset mount, check the "/"
  268. * offset (autofs-5.0+).
  269. */
  270. if (d_mountpoint(dentry)) {
  271. DPRINTK("checking mountpoint %p %.*s",
  272. dentry, (int)dentry->d_name.len, dentry->d_name.name);
  273. /* Can we umount this guy */
  274. if (autofs4_mount_busy(mnt, dentry))
  275. goto next;
  276. /* Can we expire this guy */
  277. if (autofs4_can_expire(dentry, timeout, do_now)) {
  278. expired = dentry;
  279. break;
  280. }
  281. goto next;
  282. }
  283. if (simple_empty(dentry))
  284. goto next;
  285. /* Case 2: tree mount, expire iff entire tree is not busy */
  286. if (!exp_leaves) {
  287. /* Lock the tree as we must expire as a whole */
  288. spin_lock(&sbi->fs_lock);
  289. if (!autofs4_tree_busy(mnt, dentry, timeout, do_now)) {
  290. struct autofs_info *inf = autofs4_dentry_ino(dentry);
  291. /* Set this flag early to catch sys_chdir and the like */
  292. inf->flags |= AUTOFS_INF_EXPIRING;
  293. spin_unlock(&sbi->fs_lock);
  294. expired = dentry;
  295. break;
  296. }
  297. spin_unlock(&sbi->fs_lock);
  298. /*
  299. * Case 3: pseudo direct mount, expire individual leaves
  300. * (autofs-4.1).
  301. */
  302. } else {
  303. expired = autofs4_check_leaves(mnt, dentry, timeout, do_now);
  304. if (expired) {
  305. dput(dentry);
  306. break;
  307. }
  308. }
  309. next:
  310. dput(dentry);
  311. spin_lock(&dcache_lock);
  312. next = next->next;
  313. }
  314. if (expired) {
  315. DPRINTK("returning %p %.*s",
  316. expired, (int)expired->d_name.len, expired->d_name.name);
  317. spin_lock(&dcache_lock);
  318. list_del(&expired->d_parent->d_subdirs);
  319. list_add(&expired->d_parent->d_subdirs, &expired->d_u.d_child);
  320. spin_unlock(&dcache_lock);
  321. return expired;
  322. }
  323. spin_unlock(&dcache_lock);
  324. return NULL;
  325. }
  326. /* Perform an expiry operation */
  327. int autofs4_expire_run(struct super_block *sb,
  328. struct vfsmount *mnt,
  329. struct autofs_sb_info *sbi,
  330. struct autofs_packet_expire __user *pkt_p)
  331. {
  332. struct autofs_packet_expire pkt;
  333. struct dentry *dentry;
  334. memset(&pkt,0,sizeof pkt);
  335. pkt.hdr.proto_version = sbi->version;
  336. pkt.hdr.type = autofs_ptype_expire;
  337. if ((dentry = autofs4_expire_indirect(sb, mnt, sbi, 0)) == NULL)
  338. return -EAGAIN;
  339. pkt.len = dentry->d_name.len;
  340. memcpy(pkt.name, dentry->d_name.name, pkt.len);
  341. pkt.name[pkt.len] = '\0';
  342. dput(dentry);
  343. if ( copy_to_user(pkt_p, &pkt, sizeof(struct autofs_packet_expire)) )
  344. return -EFAULT;
  345. return 0;
  346. }
  347. /* Call repeatedly until it returns -EAGAIN, meaning there's nothing
  348. more to be done */
  349. int autofs4_expire_multi(struct super_block *sb, struct vfsmount *mnt,
  350. struct autofs_sb_info *sbi, int __user *arg)
  351. {
  352. struct dentry *dentry;
  353. int ret = -EAGAIN;
  354. int do_now = 0;
  355. if (arg && get_user(do_now, arg))
  356. return -EFAULT;
  357. if (sbi->type & AUTOFS_TYPE_DIRECT)
  358. dentry = autofs4_expire_direct(sb, mnt, sbi, do_now);
  359. else
  360. dentry = autofs4_expire_indirect(sb, mnt, sbi, do_now);
  361. if (dentry) {
  362. struct autofs_info *ino = autofs4_dentry_ino(dentry);
  363. /* This is synchronous because it makes the daemon a
  364. little easier */
  365. ino->flags |= AUTOFS_INF_EXPIRING;
  366. ret = autofs4_wait(sbi, dentry, NFY_EXPIRE);
  367. ino->flags &= ~AUTOFS_INF_EXPIRING;
  368. dput(dentry);
  369. }
  370. return ret;
  371. }