expire.c 12 KB

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