expire.c 15 KB

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