expire.c 14 KB

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