1 /* -*- c -*- --------------------------------------------------------------- *
3 * linux/fs/autofs/expire.c
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>
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.
13 * ------------------------------------------------------------------------- */
17 static unsigned long now;
19 /* Check if a dentry can be expired */
20 static inline int autofs4_can_expire(struct dentry *dentry,
21 unsigned long timeout, int do_now)
23 struct autofs_info *ino = autofs4_dentry_ino(dentry);
25 /* dentry in the process of being deleted */
30 /* Too young to die */
31 if (!timeout || time_after(ino->last_used + timeout, now))
34 /* update last_used here :-
35 - obviously makes sense if it is in use now
36 - less obviously, prevents rapid-fire expire
37 attempts if expire fails the first time */
43 /* Check a mount point for busyness */
44 static int autofs4_mount_busy(struct vfsmount *mnt, struct dentry *dentry)
46 struct dentry *top = dentry;
47 struct path path = {.mnt = mnt, .dentry = dentry};
50 DPRINTK("dentry %p %.*s",
51 dentry, (int)dentry->d_name.len, dentry->d_name.name);
55 if (!follow_down_one(&path))
58 if (is_autofs4_dentry(path.dentry)) {
59 struct autofs_sb_info *sbi = autofs4_sbi(path.dentry->d_sb);
61 /* This is an autofs submount, we can't expire it */
62 if (autofs_type_indirect(sbi->type))
66 /* Update the expiry counter if fs is busy */
67 if (!may_umount_tree(path.mnt)) {
68 struct autofs_info *ino = autofs4_dentry_ino(top);
69 ino->last_used = jiffies;
75 DPRINTK("returning = %d", status);
81 * Calculate and dget next entry in the subdirs list under root.
83 static struct dentry *get_next_positive_subdir(struct dentry *prev,
86 struct autofs_sb_info *sbi = autofs4_sbi(root->d_sb);
87 struct list_head *next;
90 spin_lock(&sbi->lookup_lock);
93 spin_lock(&root->d_lock);
94 prev = dget_dlock(root);
95 next = prev->d_subdirs.next;
101 spin_lock(&p->d_lock);
103 next = p->d_child.next;
105 if (next == &root->d_subdirs) {
106 spin_unlock(&p->d_lock);
107 spin_unlock(&sbi->lookup_lock);
112 q = list_entry(next, struct dentry, d_child);
114 spin_lock_nested(&q->d_lock, DENTRY_D_LOCK_NESTED);
115 /* Negative dentry - try next */
116 if (!simple_positive(q)) {
117 spin_unlock(&p->d_lock);
122 spin_unlock(&q->d_lock);
123 spin_unlock(&p->d_lock);
124 spin_unlock(&sbi->lookup_lock);
132 * Calculate and dget next entry in top down tree traversal.
134 static struct dentry *get_next_positive_dentry(struct dentry *prev,
137 struct autofs_sb_info *sbi = autofs4_sbi(root->d_sb);
138 struct list_head *next;
139 struct dentry *p, *ret;
144 spin_lock(&sbi->lookup_lock);
147 spin_lock(&p->d_lock);
149 next = p->d_subdirs.next;
150 if (next == &p->d_subdirs) {
152 struct dentry *parent;
155 spin_unlock(&p->d_lock);
156 spin_unlock(&sbi->lookup_lock);
161 parent = p->d_parent;
162 if (!spin_trylock(&parent->d_lock)) {
163 spin_unlock(&p->d_lock);
167 spin_unlock(&p->d_lock);
168 next = p->d_child.next;
170 if (next != &parent->d_subdirs)
174 ret = list_entry(next, struct dentry, d_child);
176 spin_lock_nested(&ret->d_lock, DENTRY_D_LOCK_NESTED);
177 /* Negative dentry - try next */
178 if (!simple_positive(ret)) {
179 spin_unlock(&p->d_lock);
184 spin_unlock(&ret->d_lock);
185 spin_unlock(&p->d_lock);
186 spin_unlock(&sbi->lookup_lock);
194 * Check a direct mount point for busyness.
195 * Direct mounts have similar expiry semantics to tree mounts.
196 * The tree is not busy iff no mountpoints are busy and there are no
199 static int autofs4_direct_busy(struct vfsmount *mnt,
201 unsigned long timeout,
204 DPRINTK("top %p %.*s",
205 top, (int) top->d_name.len, top->d_name.name);
207 /* If it's busy update the expiry counters */
208 if (!may_umount_tree(mnt)) {
209 struct autofs_info *ino = autofs4_dentry_ino(top);
211 ino->last_used = jiffies;
215 /* Timeout of a direct mount is determined by its top dentry */
216 if (!autofs4_can_expire(top, timeout, do_now))
222 /* Check a directory tree of mount points for busyness
223 * The tree is not busy iff no mountpoints are busy
225 static int autofs4_tree_busy(struct vfsmount *mnt,
227 unsigned long timeout,
230 struct autofs_info *top_ino = autofs4_dentry_ino(top);
233 DPRINTK("top %p %.*s",
234 top, (int)top->d_name.len, top->d_name.name);
236 /* Negative dentry - give up */
237 if (!simple_positive(top))
241 while ((p = get_next_positive_dentry(p, top))) {
242 DPRINTK("dentry %p %.*s",
243 p, (int) p->d_name.len, p->d_name.name);
246 * Is someone visiting anywhere in the subtree ?
247 * If there's no mount we need to check the usage
248 * count for the autofs dentry.
249 * If the fs is busy update the expiry counter.
251 if (d_mountpoint(p)) {
252 if (autofs4_mount_busy(mnt, p)) {
253 top_ino->last_used = jiffies;
258 struct autofs_info *ino = autofs4_dentry_ino(p);
259 unsigned int ino_count = atomic_read(&ino->count);
262 * Clean stale dentries below that have not been
263 * invalidated after a mount fail during lookup
267 /* allow for dget above and top is already dgot */
273 if (p->d_count > ino_count) {
274 top_ino->last_used = jiffies;
281 /* Timeout of a tree mount is ultimately determined by its top dentry */
282 if (!autofs4_can_expire(top, timeout, do_now))
288 static struct dentry *autofs4_check_leaves(struct vfsmount *mnt,
289 struct dentry *parent,
290 unsigned long timeout,
295 DPRINTK("parent %p %.*s",
296 parent, (int)parent->d_name.len, parent->d_name.name);
299 while ((p = get_next_positive_dentry(p, parent))) {
300 DPRINTK("dentry %p %.*s",
301 p, (int) p->d_name.len, p->d_name.name);
303 if (d_mountpoint(p)) {
304 /* Can we umount this guy */
305 if (autofs4_mount_busy(mnt, p))
308 /* Can we expire this guy */
309 if (autofs4_can_expire(p, timeout, do_now))
316 /* Check if we can expire a direct mount (possibly a tree) */
317 struct dentry *autofs4_expire_direct(struct super_block *sb,
318 struct vfsmount *mnt,
319 struct autofs_sb_info *sbi,
322 unsigned long timeout;
323 struct dentry *root = dget(sb->s_root);
324 int do_now = how & AUTOFS_EXP_IMMEDIATE;
325 struct autofs_info *ino;
331 timeout = sbi->exp_timeout;
333 spin_lock(&sbi->fs_lock);
334 ino = autofs4_dentry_ino(root);
335 /* No point expiring a pending mount */
336 if (ino->flags & AUTOFS_INF_PENDING)
338 if (!autofs4_direct_busy(mnt, root, timeout, do_now)) {
339 struct autofs_info *ino = autofs4_dentry_ino(root);
340 ino->flags |= AUTOFS_INF_EXPIRING;
341 init_completion(&ino->expire_complete);
342 spin_unlock(&sbi->fs_lock);
346 spin_unlock(&sbi->fs_lock);
353 * Find an eligible tree to time-out
354 * A tree is eligible if :-
355 * - it is unused by any user process
356 * - it has been unused for exp_timeout time
358 struct dentry *autofs4_expire_indirect(struct super_block *sb,
359 struct vfsmount *mnt,
360 struct autofs_sb_info *sbi,
363 unsigned long timeout;
364 struct dentry *root = sb->s_root;
365 struct dentry *dentry;
366 struct dentry *expired = NULL;
367 int do_now = how & AUTOFS_EXP_IMMEDIATE;
368 int exp_leaves = how & AUTOFS_EXP_LEAVES;
369 struct autofs_info *ino;
370 unsigned int ino_count;
376 timeout = sbi->exp_timeout;
379 while ((dentry = get_next_positive_subdir(dentry, root))) {
380 spin_lock(&sbi->fs_lock);
381 ino = autofs4_dentry_ino(dentry);
382 /* No point expiring a pending mount */
383 if (ino->flags & AUTOFS_INF_PENDING)
387 * Case 1: (i) indirect mount or top level pseudo direct mount
389 * (ii) indirect mount with offset mount, check the "/"
390 * offset (autofs-5.0+).
392 if (d_mountpoint(dentry)) {
393 DPRINTK("checking mountpoint %p %.*s",
394 dentry, (int)dentry->d_name.len, dentry->d_name.name);
396 /* Path walk currently on this dentry? */
397 ino_count = atomic_read(&ino->count) + 2;
398 if (dentry->d_count > ino_count)
401 /* Can we umount this guy */
402 if (autofs4_mount_busy(mnt, dentry))
405 /* Can we expire this guy */
406 if (autofs4_can_expire(dentry, timeout, do_now)) {
413 if (simple_empty(dentry))
416 /* Case 2: tree mount, expire iff entire tree is not busy */
418 /* Path walk currently on this dentry? */
419 ino_count = atomic_read(&ino->count) + 1;
420 if (dentry->d_count > ino_count)
423 if (!autofs4_tree_busy(mnt, dentry, timeout, do_now)) {
428 * Case 3: pseudo direct mount, expire individual leaves
432 /* Path walk currently on this dentry? */
433 ino_count = atomic_read(&ino->count) + 1;
434 if (dentry->d_count > ino_count)
437 expired = autofs4_check_leaves(mnt, dentry, timeout, do_now);
444 spin_unlock(&sbi->fs_lock);
449 DPRINTK("returning %p %.*s",
450 expired, (int)expired->d_name.len, expired->d_name.name);
451 ino = autofs4_dentry_ino(expired);
452 ino->flags |= AUTOFS_INF_EXPIRING;
453 init_completion(&ino->expire_complete);
454 spin_unlock(&sbi->fs_lock);
455 spin_lock(&sbi->lookup_lock);
456 spin_lock(&expired->d_parent->d_lock);
457 spin_lock_nested(&expired->d_lock, DENTRY_D_LOCK_NESTED);
458 list_move(&expired->d_parent->d_subdirs, &expired->d_child);
459 spin_unlock(&expired->d_lock);
460 spin_unlock(&expired->d_parent->d_lock);
461 spin_unlock(&sbi->lookup_lock);
465 int autofs4_expire_wait(struct dentry *dentry)
467 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
468 struct autofs_info *ino = autofs4_dentry_ino(dentry);
471 /* Block on any pending expire */
472 spin_lock(&sbi->fs_lock);
473 if (ino->flags & AUTOFS_INF_EXPIRING) {
474 spin_unlock(&sbi->fs_lock);
476 DPRINTK("waiting for expire %p name=%.*s",
477 dentry, dentry->d_name.len, dentry->d_name.name);
479 status = autofs4_wait(sbi, dentry, NFY_NONE);
480 wait_for_completion(&ino->expire_complete);
482 DPRINTK("expire done status=%d", status);
484 if (d_unhashed(dentry))
489 spin_unlock(&sbi->fs_lock);
494 /* Perform an expiry operation */
495 int autofs4_expire_run(struct super_block *sb,
496 struct vfsmount *mnt,
497 struct autofs_sb_info *sbi,
498 struct autofs_packet_expire __user *pkt_p)
500 struct autofs_packet_expire pkt;
501 struct autofs_info *ino;
502 struct dentry *dentry;
505 memset(&pkt,0,sizeof pkt);
507 pkt.hdr.proto_version = sbi->version;
508 pkt.hdr.type = autofs_ptype_expire;
510 if ((dentry = autofs4_expire_indirect(sb, mnt, sbi, 0)) == NULL)
513 pkt.len = dentry->d_name.len;
514 memcpy(pkt.name, dentry->d_name.name, pkt.len);
515 pkt.name[pkt.len] = '\0';
518 if ( copy_to_user(pkt_p, &pkt, sizeof(struct autofs_packet_expire)) )
521 spin_lock(&sbi->fs_lock);
522 ino = autofs4_dentry_ino(dentry);
523 ino->flags &= ~AUTOFS_INF_EXPIRING;
524 complete_all(&ino->expire_complete);
525 spin_unlock(&sbi->fs_lock);
530 int autofs4_do_expire_multi(struct super_block *sb, struct vfsmount *mnt,
531 struct autofs_sb_info *sbi, int when)
533 struct dentry *dentry;
536 if (autofs_type_trigger(sbi->type))
537 dentry = autofs4_expire_direct(sb, mnt, sbi, when);
539 dentry = autofs4_expire_indirect(sb, mnt, sbi, when);
542 struct autofs_info *ino = autofs4_dentry_ino(dentry);
544 /* This is synchronous because it makes the daemon a
546 ret = autofs4_wait(sbi, dentry, NFY_EXPIRE);
548 spin_lock(&sbi->fs_lock);
549 ino->flags &= ~AUTOFS_INF_EXPIRING;
550 spin_lock(&dentry->d_lock);
552 if ((IS_ROOT(dentry) ||
553 (autofs_type_indirect(sbi->type) &&
554 IS_ROOT(dentry->d_parent))) &&
555 !(dentry->d_flags & DCACHE_NEED_AUTOMOUNT))
556 __managed_dentry_set_automount(dentry);
558 spin_unlock(&dentry->d_lock);
559 complete_all(&ino->expire_complete);
560 spin_unlock(&sbi->fs_lock);
567 /* Call repeatedly until it returns -EAGAIN, meaning there's nothing
569 int autofs4_expire_multi(struct super_block *sb, struct vfsmount *mnt,
570 struct autofs_sb_info *sbi, int __user *arg)
574 if (arg && get_user(do_now, arg))
577 return autofs4_do_expire_multi(sb, mnt, sbi, do_now);