expire.c 13 KB

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