expire.c 14 KB

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