expire.c 12 KB

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