expire.c 14 KB

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