expire.c 14 KB

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