expire.c 13 KB

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