expire.c 14 KB

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