expire.c 14 KB

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