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 */
29 /* No point expiring a pending mount */
30 if (ino->flags & AUTOFS_INF_PENDING)
34 /* Too young to die */
35 if (!timeout || time_after(ino->last_used + timeout, now))
38 /* update last_used here :-
39 - obviously makes sense if it is in use now
40 - less obviously, prevents rapid-fire expire
41 attempts if expire fails the first time */
47 /* Check a mount point for busyness */
48 static int autofs4_mount_busy(struct vfsmount *mnt, struct dentry *dentry)
50 struct dentry *top = dentry;
51 struct path path = {.mnt = mnt, .dentry = dentry};
54 DPRINTK("dentry %p %.*s",
55 dentry, (int)dentry->d_name.len, dentry->d_name.name);
59 if (!follow_down_one(&path))
62 if (is_autofs4_dentry(path.dentry)) {
63 struct autofs_sb_info *sbi = autofs4_sbi(path.dentry->d_sb);
65 /* This is an autofs submount, we can't expire it */
66 if (autofs_type_indirect(sbi->type))
70 * Otherwise it's an offset mount and we need to check
71 * if we can umount its mount, if there is one.
73 if (!d_mountpoint(path.dentry)) {
79 /* Update the expiry counter if fs is busy */
80 if (!may_umount_tree(path.mnt)) {
81 struct autofs_info *ino = autofs4_dentry_ino(top);
82 ino->last_used = jiffies;
88 DPRINTK("returning = %d", status);
94 * Calculate and dget next entry in top down tree traversal.
96 static struct dentry *get_next_positive_dentry(struct dentry *prev,
99 struct list_head *next;
100 struct dentry *p, *ret;
105 spin_lock(&autofs4_lock);
108 spin_lock(&p->d_lock);
110 next = p->d_subdirs.next;
111 if (next == &p->d_subdirs) {
113 struct dentry *parent;
116 spin_unlock(&p->d_lock);
117 spin_unlock(&autofs4_lock);
122 parent = p->d_parent;
123 if (!spin_trylock(&parent->d_lock)) {
124 spin_unlock(&p->d_lock);
128 spin_unlock(&p->d_lock);
129 next = p->d_u.d_child.next;
131 if (next != &parent->d_subdirs)
135 ret = list_entry(next, struct dentry, d_u.d_child);
137 spin_lock_nested(&ret->d_lock, DENTRY_D_LOCK_NESTED);
138 /* Negative dentry - try next */
139 if (!simple_positive(ret)) {
140 spin_unlock(&ret->d_lock);
145 spin_unlock(&ret->d_lock);
146 spin_unlock(&p->d_lock);
147 spin_unlock(&autofs4_lock);
155 * Check a direct mount point for busyness.
156 * Direct mounts have similar expiry semantics to tree mounts.
157 * The tree is not busy iff no mountpoints are busy and there are no
160 static int autofs4_direct_busy(struct vfsmount *mnt,
162 unsigned long timeout,
165 DPRINTK("top %p %.*s",
166 top, (int) top->d_name.len, top->d_name.name);
168 /* If it's busy update the expiry counters */
169 if (!may_umount_tree(mnt)) {
170 struct autofs_info *ino = autofs4_dentry_ino(top);
172 ino->last_used = jiffies;
176 /* Timeout of a direct mount is determined by its top dentry */
177 if (!autofs4_can_expire(top, timeout, do_now))
183 /* Check a directory tree of mount points for busyness
184 * The tree is not busy iff no mountpoints are busy
186 static int autofs4_tree_busy(struct vfsmount *mnt,
188 unsigned long timeout,
191 struct autofs_info *top_ino = autofs4_dentry_ino(top);
194 DPRINTK("top %p %.*s",
195 top, (int)top->d_name.len, top->d_name.name);
197 /* Negative dentry - give up */
198 if (!simple_positive(top))
202 while ((p = get_next_positive_dentry(p, top))) {
203 DPRINTK("dentry %p %.*s",
204 p, (int) p->d_name.len, p->d_name.name);
207 * Is someone visiting anywhere in the subtree ?
208 * If there's no mount we need to check the usage
209 * count for the autofs dentry.
210 * If the fs is busy update the expiry counter.
212 if (d_mountpoint(p)) {
213 if (autofs4_mount_busy(mnt, p)) {
214 top_ino->last_used = jiffies;
219 struct autofs_info *ino = autofs4_dentry_ino(p);
220 unsigned int ino_count = atomic_read(&ino->count);
223 * Clean stale dentries below that have not been
224 * invalidated after a mount fail during lookup
228 /* allow for dget above and top is already dgot */
234 if (p->d_count > ino_count) {
235 top_ino->last_used = jiffies;
242 /* Timeout of a tree mount is ultimately determined by its top dentry */
243 if (!autofs4_can_expire(top, timeout, do_now))
249 static struct dentry *autofs4_check_leaves(struct vfsmount *mnt,
250 struct dentry *parent,
251 unsigned long timeout,
256 DPRINTK("parent %p %.*s",
257 parent, (int)parent->d_name.len, parent->d_name.name);
260 while ((p = get_next_positive_dentry(p, parent))) {
261 DPRINTK("dentry %p %.*s",
262 p, (int) p->d_name.len, p->d_name.name);
264 if (d_mountpoint(p)) {
265 /* Can we umount this guy */
266 if (autofs4_mount_busy(mnt, p))
269 /* Can we expire this guy */
270 if (autofs4_can_expire(p, timeout, do_now))
277 /* Check if we can expire a direct mount (possibly a tree) */
278 struct dentry *autofs4_expire_direct(struct super_block *sb,
279 struct vfsmount *mnt,
280 struct autofs_sb_info *sbi,
283 unsigned long timeout;
284 struct dentry *root = dget(sb->s_root);
285 int do_now = how & AUTOFS_EXP_IMMEDIATE;
291 timeout = sbi->exp_timeout;
293 spin_lock(&sbi->fs_lock);
294 if (!autofs4_direct_busy(mnt, root, timeout, do_now)) {
295 struct autofs_info *ino = autofs4_dentry_ino(root);
296 if (d_mountpoint(root)) {
297 ino->flags |= AUTOFS_INF_MOUNTPOINT;
298 spin_lock(&root->d_lock);
299 root->d_flags &= ~DCACHE_MOUNTED;
300 spin_unlock(&root->d_lock);
302 ino->flags |= AUTOFS_INF_EXPIRING;
303 managed_dentry_set_automount(root);
304 init_completion(&ino->expire_complete);
305 spin_unlock(&sbi->fs_lock);
308 spin_unlock(&sbi->fs_lock);
315 * Find an eligible tree to time-out
316 * A tree is eligible if :-
317 * - it is unused by any user process
318 * - it has been unused for exp_timeout time
320 struct dentry *autofs4_expire_indirect(struct super_block *sb,
321 struct vfsmount *mnt,
322 struct autofs_sb_info *sbi,
325 unsigned long timeout;
326 struct dentry *root = sb->s_root;
327 struct dentry *dentry;
328 struct dentry *expired = NULL;
329 int do_now = how & AUTOFS_EXP_IMMEDIATE;
330 int exp_leaves = how & AUTOFS_EXP_LEAVES;
331 struct autofs_info *ino;
332 unsigned int ino_count;
338 timeout = sbi->exp_timeout;
341 while ((dentry = get_next_positive_dentry(dentry, root))) {
342 spin_lock(&sbi->fs_lock);
343 ino = autofs4_dentry_ino(dentry);
346 * Case 1: (i) indirect mount or top level pseudo direct mount
348 * (ii) indirect mount with offset mount, check the "/"
349 * offset (autofs-5.0+).
351 if (d_mountpoint(dentry)) {
352 DPRINTK("checking mountpoint %p %.*s",
353 dentry, (int)dentry->d_name.len, dentry->d_name.name);
355 /* Path walk currently on this dentry? */
356 ino_count = atomic_read(&ino->count) + 2;
357 if (dentry->d_count > ino_count)
360 /* Can we umount this guy */
361 if (autofs4_mount_busy(mnt, dentry))
364 /* Can we expire this guy */
365 if (autofs4_can_expire(dentry, timeout, do_now)) {
372 if (simple_empty(dentry))
375 /* Case 2: tree mount, expire iff entire tree is not busy */
377 /* Path walk currently on this dentry? */
378 ino_count = atomic_read(&ino->count) + 1;
379 if (dentry->d_count > ino_count)
382 if (!autofs4_tree_busy(mnt, dentry, timeout, do_now)) {
387 * Case 3: pseudo direct mount, expire individual leaves
391 /* Path walk currently on this dentry? */
392 ino_count = atomic_read(&ino->count) + 1;
393 if (dentry->d_count > ino_count)
396 expired = autofs4_check_leaves(mnt, dentry, timeout, do_now);
403 spin_unlock(&sbi->fs_lock);
408 DPRINTK("returning %p %.*s",
409 expired, (int)expired->d_name.len, expired->d_name.name);
410 ino = autofs4_dentry_ino(expired);
411 ino->flags |= AUTOFS_INF_EXPIRING;
412 managed_dentry_set_automount(expired);
413 init_completion(&ino->expire_complete);
414 spin_unlock(&sbi->fs_lock);
415 spin_lock(&autofs4_lock);
416 spin_lock(&expired->d_parent->d_lock);
417 spin_lock_nested(&expired->d_lock, DENTRY_D_LOCK_NESTED);
418 list_move(&expired->d_parent->d_subdirs, &expired->d_u.d_child);
419 spin_unlock(&expired->d_lock);
420 spin_unlock(&expired->d_parent->d_lock);
421 spin_unlock(&autofs4_lock);
425 int autofs4_expire_wait(struct dentry *dentry)
427 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
428 struct autofs_info *ino = autofs4_dentry_ino(dentry);
431 /* Block on any pending expire */
432 spin_lock(&sbi->fs_lock);
433 if (ino->flags & AUTOFS_INF_EXPIRING) {
434 spin_unlock(&sbi->fs_lock);
436 DPRINTK("waiting for expire %p name=%.*s",
437 dentry, dentry->d_name.len, dentry->d_name.name);
439 status = autofs4_wait(sbi, dentry, NFY_NONE);
440 wait_for_completion(&ino->expire_complete);
442 DPRINTK("expire done status=%d", status);
444 if (d_unhashed(dentry))
449 spin_unlock(&sbi->fs_lock);
454 /* Perform an expiry operation */
455 int autofs4_expire_run(struct super_block *sb,
456 struct vfsmount *mnt,
457 struct autofs_sb_info *sbi,
458 struct autofs_packet_expire __user *pkt_p)
460 struct autofs_packet_expire pkt;
461 struct autofs_info *ino;
462 struct dentry *dentry;
465 memset(&pkt,0,sizeof pkt);
467 pkt.hdr.proto_version = sbi->version;
468 pkt.hdr.type = autofs_ptype_expire;
470 if ((dentry = autofs4_expire_indirect(sb, mnt, sbi, 0)) == NULL)
473 pkt.len = dentry->d_name.len;
474 memcpy(pkt.name, dentry->d_name.name, pkt.len);
475 pkt.name[pkt.len] = '\0';
478 if ( copy_to_user(pkt_p, &pkt, sizeof(struct autofs_packet_expire)) )
481 spin_lock(&sbi->fs_lock);
482 ino = autofs4_dentry_ino(dentry);
483 ino->flags &= ~AUTOFS_INF_EXPIRING;
484 if (!d_unhashed(dentry))
485 managed_dentry_clear_automount(dentry);
486 complete_all(&ino->expire_complete);
487 spin_unlock(&sbi->fs_lock);
492 int autofs4_do_expire_multi(struct super_block *sb, struct vfsmount *mnt,
493 struct autofs_sb_info *sbi, int when)
495 struct dentry *dentry;
498 if (autofs_type_trigger(sbi->type))
499 dentry = autofs4_expire_direct(sb, mnt, sbi, when);
501 dentry = autofs4_expire_indirect(sb, mnt, sbi, when);
504 struct autofs_info *ino = autofs4_dentry_ino(dentry);
506 /* This is synchronous because it makes the daemon a
508 ret = autofs4_wait(sbi, dentry, NFY_EXPIRE);
510 spin_lock(&sbi->fs_lock);
511 if (ino->flags & AUTOFS_INF_MOUNTPOINT) {
512 spin_lock(&sb->s_root->d_lock);
514 * If we haven't been expired away, then reset
517 if (mnt->mnt_parent != mnt)
518 sb->s_root->d_flags |= DCACHE_MOUNTED;
519 spin_unlock(&sb->s_root->d_lock);
520 ino->flags &= ~AUTOFS_INF_MOUNTPOINT;
522 ino->flags &= ~AUTOFS_INF_EXPIRING;
524 managed_dentry_clear_automount(dentry);
525 complete_all(&ino->expire_complete);
526 spin_unlock(&sbi->fs_lock);
533 /* Call repeatedly until it returns -EAGAIN, meaning there's nothing
535 int autofs4_expire_multi(struct super_block *sb, struct vfsmount *mnt,
536 struct autofs_sb_info *sbi, int __user *arg)
540 if (arg && get_user(do_now, arg))
543 return autofs4_do_expire_multi(sb, mnt, sbi, do_now);