expire.c 15 KB

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