expire.c 13 KB

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