expire.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  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 (!timeout || 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 (!follow_down(&mnt, &dentry))
  49. goto done;
  50. if (is_autofs4_dentry(dentry)) {
  51. struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
  52. /* This is an autofs submount, we can't expire it */
  53. if (autofs_type_indirect(sbi->type))
  54. goto done;
  55. /*
  56. * Otherwise it's an offset mount and we need to check
  57. * if we can umount its mount, if there is one.
  58. */
  59. if (!d_mountpoint(dentry))
  60. goto done;
  61. }
  62. /* Update the expiry counter if fs is busy */
  63. if (!may_umount_tree(mnt)) {
  64. struct autofs_info *ino = autofs4_dentry_ino(top);
  65. ino->last_used = jiffies;
  66. goto done;
  67. }
  68. status = 0;
  69. done:
  70. DPRINTK("returning = %d", status);
  71. dput(dentry);
  72. mntput(mnt);
  73. return status;
  74. }
  75. /*
  76. * Calculate next entry in top down tree traversal.
  77. * From next_mnt in namespace.c - elegant.
  78. */
  79. static struct dentry *next_dentry(struct dentry *p, struct dentry *root)
  80. {
  81. struct list_head *next = p->d_subdirs.next;
  82. if (next == &p->d_subdirs) {
  83. while (1) {
  84. if (p == root)
  85. return NULL;
  86. next = p->d_u.d_child.next;
  87. if (next != &p->d_parent->d_subdirs)
  88. break;
  89. p = p->d_parent;
  90. }
  91. }
  92. return list_entry(next, struct dentry, d_u.d_child);
  93. }
  94. /*
  95. * Check a direct mount point for busyness.
  96. * Direct mounts have similar expiry semantics to tree mounts.
  97. * The tree is not busy iff no mountpoints are busy and there are no
  98. * autofs submounts.
  99. */
  100. static int autofs4_direct_busy(struct vfsmount *mnt,
  101. struct dentry *top,
  102. unsigned long timeout,
  103. int do_now)
  104. {
  105. DPRINTK("top %p %.*s",
  106. top, (int) top->d_name.len, top->d_name.name);
  107. /* If it's busy update the expiry counters */
  108. if (!may_umount_tree(mnt)) {
  109. struct autofs_info *ino = autofs4_dentry_ino(top);
  110. if (ino)
  111. ino->last_used = jiffies;
  112. return 1;
  113. }
  114. /* Timeout of a direct mount is determined by its top dentry */
  115. if (!autofs4_can_expire(top, timeout, do_now))
  116. return 1;
  117. return 0;
  118. }
  119. /* Check a directory tree of mount points for busyness
  120. * The tree is not busy iff no mountpoints are busy
  121. */
  122. static int autofs4_tree_busy(struct vfsmount *mnt,
  123. struct dentry *top,
  124. unsigned long timeout,
  125. int do_now)
  126. {
  127. struct autofs_info *top_ino = autofs4_dentry_ino(top);
  128. struct dentry *p;
  129. DPRINTK("top %p %.*s",
  130. top, (int)top->d_name.len, top->d_name.name);
  131. /* Negative dentry - give up */
  132. if (!simple_positive(top))
  133. return 1;
  134. spin_lock(&dcache_lock);
  135. for (p = top; p; p = next_dentry(p, top)) {
  136. /* Negative dentry - give up */
  137. if (!simple_positive(p))
  138. continue;
  139. DPRINTK("dentry %p %.*s",
  140. p, (int) p->d_name.len, p->d_name.name);
  141. p = dget(p);
  142. spin_unlock(&dcache_lock);
  143. /*
  144. * Is someone visiting anywhere in the subtree ?
  145. * If there's no mount we need to check the usage
  146. * count for the autofs dentry.
  147. * If the fs is busy update the expiry counter.
  148. */
  149. if (d_mountpoint(p)) {
  150. if (autofs4_mount_busy(mnt, p)) {
  151. top_ino->last_used = jiffies;
  152. dput(p);
  153. return 1;
  154. }
  155. } else {
  156. struct autofs_info *ino = autofs4_dentry_ino(p);
  157. unsigned int ino_count = atomic_read(&ino->count);
  158. /*
  159. * Clean stale dentries below that have not been
  160. * invalidated after a mount fail during lookup
  161. */
  162. d_invalidate(p);
  163. /* allow for dget above and top is already dgot */
  164. if (p == top)
  165. ino_count += 2;
  166. else
  167. ino_count++;
  168. if (atomic_read(&p->d_count) > ino_count) {
  169. top_ino->last_used = jiffies;
  170. dput(p);
  171. return 1;
  172. }
  173. }
  174. dput(p);
  175. spin_lock(&dcache_lock);
  176. }
  177. spin_unlock(&dcache_lock);
  178. /* Timeout of a tree mount is ultimately determined by its top dentry */
  179. if (!autofs4_can_expire(top, timeout, do_now))
  180. return 1;
  181. return 0;
  182. }
  183. static struct dentry *autofs4_check_leaves(struct vfsmount *mnt,
  184. struct dentry *parent,
  185. unsigned long timeout,
  186. int do_now)
  187. {
  188. struct dentry *p;
  189. DPRINTK("parent %p %.*s",
  190. parent, (int)parent->d_name.len, parent->d_name.name);
  191. spin_lock(&dcache_lock);
  192. for (p = parent; p; p = next_dentry(p, parent)) {
  193. /* Negative dentry - give up */
  194. if (!simple_positive(p))
  195. continue;
  196. DPRINTK("dentry %p %.*s",
  197. p, (int) p->d_name.len, p->d_name.name);
  198. p = dget(p);
  199. spin_unlock(&dcache_lock);
  200. if (d_mountpoint(p)) {
  201. /* Can we umount this guy */
  202. if (autofs4_mount_busy(mnt, p))
  203. goto cont;
  204. /* Can we expire this guy */
  205. if (autofs4_can_expire(p, timeout, do_now))
  206. return p;
  207. }
  208. cont:
  209. dput(p);
  210. spin_lock(&dcache_lock);
  211. }
  212. spin_unlock(&dcache_lock);
  213. return NULL;
  214. }
  215. /* Check if we can expire a direct mount (possibly a tree) */
  216. struct dentry *autofs4_expire_direct(struct super_block *sb,
  217. struct vfsmount *mnt,
  218. struct autofs_sb_info *sbi,
  219. int how)
  220. {
  221. unsigned long timeout;
  222. struct dentry *root = dget(sb->s_root);
  223. int do_now = how & AUTOFS_EXP_IMMEDIATE;
  224. if (!root)
  225. return NULL;
  226. now = jiffies;
  227. timeout = sbi->exp_timeout;
  228. spin_lock(&sbi->fs_lock);
  229. if (!autofs4_direct_busy(mnt, root, timeout, do_now)) {
  230. struct autofs_info *ino = autofs4_dentry_ino(root);
  231. if (d_mountpoint(root)) {
  232. ino->flags |= AUTOFS_INF_MOUNTPOINT;
  233. root->d_mounted--;
  234. }
  235. ino->flags |= AUTOFS_INF_EXPIRING;
  236. init_completion(&ino->expire_complete);
  237. spin_unlock(&sbi->fs_lock);
  238. return root;
  239. }
  240. spin_unlock(&sbi->fs_lock);
  241. dput(root);
  242. return NULL;
  243. }
  244. /*
  245. * Find an eligible tree to time-out
  246. * A tree is eligible if :-
  247. * - it is unused by any user process
  248. * - it has been unused for exp_timeout time
  249. */
  250. struct dentry *autofs4_expire_indirect(struct super_block *sb,
  251. struct vfsmount *mnt,
  252. struct autofs_sb_info *sbi,
  253. int how)
  254. {
  255. unsigned long timeout;
  256. struct dentry *root = sb->s_root;
  257. struct dentry *expired = NULL;
  258. struct list_head *next;
  259. int do_now = how & AUTOFS_EXP_IMMEDIATE;
  260. int exp_leaves = how & AUTOFS_EXP_LEAVES;
  261. struct autofs_info *ino;
  262. unsigned int ino_count;
  263. if (!root)
  264. return NULL;
  265. now = jiffies;
  266. timeout = sbi->exp_timeout;
  267. spin_lock(&dcache_lock);
  268. next = root->d_subdirs.next;
  269. /* On exit from the loop expire is set to a dgot dentry
  270. * to expire or it's NULL */
  271. while ( next != &root->d_subdirs ) {
  272. struct dentry *dentry = list_entry(next, struct dentry, d_u.d_child);
  273. /* Negative dentry - give up */
  274. if (!simple_positive(dentry)) {
  275. next = next->next;
  276. continue;
  277. }
  278. dentry = dget(dentry);
  279. spin_unlock(&dcache_lock);
  280. spin_lock(&sbi->fs_lock);
  281. ino = autofs4_dentry_ino(dentry);
  282. /*
  283. * Case 1: (i) indirect mount or top level pseudo direct mount
  284. * (autofs-4.1).
  285. * (ii) indirect mount with offset mount, check the "/"
  286. * offset (autofs-5.0+).
  287. */
  288. if (d_mountpoint(dentry)) {
  289. DPRINTK("checking mountpoint %p %.*s",
  290. dentry, (int)dentry->d_name.len, dentry->d_name.name);
  291. /* Path walk currently on this dentry? */
  292. ino_count = atomic_read(&ino->count) + 2;
  293. if (atomic_read(&dentry->d_count) > ino_count)
  294. goto next;
  295. /* Can we umount this guy */
  296. if (autofs4_mount_busy(mnt, dentry))
  297. goto next;
  298. /* Can we expire this guy */
  299. if (autofs4_can_expire(dentry, timeout, do_now)) {
  300. expired = dentry;
  301. goto found;
  302. }
  303. goto next;
  304. }
  305. if (simple_empty(dentry))
  306. goto next;
  307. /* Case 2: tree mount, expire iff entire tree is not busy */
  308. if (!exp_leaves) {
  309. /* Path walk currently on this dentry? */
  310. ino_count = atomic_read(&ino->count) + 1;
  311. if (atomic_read(&dentry->d_count) > ino_count)
  312. goto next;
  313. if (!autofs4_tree_busy(mnt, dentry, timeout, do_now)) {
  314. expired = dentry;
  315. goto found;
  316. }
  317. /*
  318. * Case 3: pseudo direct mount, expire individual leaves
  319. * (autofs-4.1).
  320. */
  321. } else {
  322. /* Path walk currently on this dentry? */
  323. ino_count = atomic_read(&ino->count) + 1;
  324. if (atomic_read(&dentry->d_count) > ino_count)
  325. goto next;
  326. expired = autofs4_check_leaves(mnt, dentry, timeout, do_now);
  327. if (expired) {
  328. dput(dentry);
  329. goto found;
  330. }
  331. }
  332. next:
  333. spin_unlock(&sbi->fs_lock);
  334. dput(dentry);
  335. spin_lock(&dcache_lock);
  336. next = next->next;
  337. }
  338. spin_unlock(&dcache_lock);
  339. return NULL;
  340. found:
  341. DPRINTK("returning %p %.*s",
  342. expired, (int)expired->d_name.len, expired->d_name.name);
  343. ino = autofs4_dentry_ino(expired);
  344. ino->flags |= AUTOFS_INF_EXPIRING;
  345. init_completion(&ino->expire_complete);
  346. spin_unlock(&sbi->fs_lock);
  347. spin_lock(&dcache_lock);
  348. list_move(&expired->d_parent->d_subdirs, &expired->d_u.d_child);
  349. spin_unlock(&dcache_lock);
  350. return expired;
  351. }
  352. int autofs4_expire_wait(struct dentry *dentry)
  353. {
  354. struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
  355. struct autofs_info *ino = autofs4_dentry_ino(dentry);
  356. int status;
  357. /* Block on any pending expire */
  358. spin_lock(&sbi->fs_lock);
  359. if (ino->flags & AUTOFS_INF_EXPIRING) {
  360. spin_unlock(&sbi->fs_lock);
  361. DPRINTK("waiting for expire %p name=%.*s",
  362. dentry, dentry->d_name.len, dentry->d_name.name);
  363. status = autofs4_wait(sbi, dentry, NFY_NONE);
  364. wait_for_completion(&ino->expire_complete);
  365. DPRINTK("expire done status=%d", status);
  366. if (d_unhashed(dentry))
  367. return -EAGAIN;
  368. return status;
  369. }
  370. spin_unlock(&sbi->fs_lock);
  371. return 0;
  372. }
  373. /* Perform an expiry operation */
  374. int autofs4_expire_run(struct super_block *sb,
  375. struct vfsmount *mnt,
  376. struct autofs_sb_info *sbi,
  377. struct autofs_packet_expire __user *pkt_p)
  378. {
  379. struct autofs_packet_expire pkt;
  380. struct autofs_info *ino;
  381. struct dentry *dentry;
  382. int ret = 0;
  383. memset(&pkt,0,sizeof pkt);
  384. pkt.hdr.proto_version = sbi->version;
  385. pkt.hdr.type = autofs_ptype_expire;
  386. if ((dentry = autofs4_expire_indirect(sb, mnt, sbi, 0)) == NULL)
  387. return -EAGAIN;
  388. pkt.len = dentry->d_name.len;
  389. memcpy(pkt.name, dentry->d_name.name, pkt.len);
  390. pkt.name[pkt.len] = '\0';
  391. dput(dentry);
  392. if ( copy_to_user(pkt_p, &pkt, sizeof(struct autofs_packet_expire)) )
  393. ret = -EFAULT;
  394. spin_lock(&sbi->fs_lock);
  395. ino = autofs4_dentry_ino(dentry);
  396. ino->flags &= ~AUTOFS_INF_EXPIRING;
  397. complete_all(&ino->expire_complete);
  398. spin_unlock(&sbi->fs_lock);
  399. return ret;
  400. }
  401. int autofs4_do_expire_multi(struct super_block *sb, struct vfsmount *mnt,
  402. struct autofs_sb_info *sbi, int when)
  403. {
  404. struct dentry *dentry;
  405. int ret = -EAGAIN;
  406. if (autofs_type_trigger(sbi->type))
  407. dentry = autofs4_expire_direct(sb, mnt, sbi, when);
  408. else
  409. dentry = autofs4_expire_indirect(sb, mnt, sbi, when);
  410. if (dentry) {
  411. struct autofs_info *ino = autofs4_dentry_ino(dentry);
  412. /* This is synchronous because it makes the daemon a
  413. little easier */
  414. ret = autofs4_wait(sbi, dentry, NFY_EXPIRE);
  415. spin_lock(&sbi->fs_lock);
  416. if (ino->flags & AUTOFS_INF_MOUNTPOINT) {
  417. sb->s_root->d_mounted++;
  418. ino->flags &= ~AUTOFS_INF_MOUNTPOINT;
  419. }
  420. ino->flags &= ~AUTOFS_INF_EXPIRING;
  421. complete_all(&ino->expire_complete);
  422. spin_unlock(&sbi->fs_lock);
  423. dput(dentry);
  424. }
  425. return ret;
  426. }
  427. /* Call repeatedly until it returns -EAGAIN, meaning there's nothing
  428. more to be done */
  429. int autofs4_expire_multi(struct super_block *sb, struct vfsmount *mnt,
  430. struct autofs_sb_info *sbi, int __user *arg)
  431. {
  432. int do_now = 0;
  433. if (arg && get_user(do_now, arg))
  434. return -EFAULT;
  435. return autofs4_do_expire_multi(sb, mnt, sbi, do_now);
  436. }