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 * Otherwise it's an offset mount and we need to check
67 * if we can umount its mount, if there is one.
69 if (!d_mountpoint(path.dentry)) {
75 /* Update the expiry counter if fs is busy */
76 if (!may_umount_tree(path.mnt)) {
77 struct autofs_info *ino = autofs4_dentry_ino(top);
78 ino->last_used = jiffies;
84 DPRINTK("returning = %d", status);
90 * Calculate and dget next entry in the subdirs list under root.
92 static struct dentry *get_next_positive_subdir(struct dentry *prev,
95 struct list_head *next;
98 spin_lock(&autofs4_lock);
101 spin_lock(&root->d_lock);
102 prev = dget_dlock(root);
103 next = prev->d_subdirs.next;
109 spin_lock(&p->d_lock);
111 next = p->d_u.d_child.next;
113 if (next == &root->d_subdirs) {
114 spin_unlock(&p->d_lock);
115 spin_unlock(&autofs4_lock);
120 q = list_entry(next, struct dentry, d_u.d_child);
122 spin_lock_nested(&q->d_lock, DENTRY_D_LOCK_NESTED);
123 /* Negative dentry - try next */
124 if (!simple_positive(q)) {
125 spin_unlock(&p->d_lock);
130 spin_unlock(&q->d_lock);
131 spin_unlock(&p->d_lock);
132 spin_unlock(&autofs4_lock);
140 * Calculate and dget next entry in top down tree traversal.
142 static struct dentry *get_next_positive_dentry(struct dentry *prev,
145 struct list_head *next;
146 struct dentry *p, *ret;
151 spin_lock(&autofs4_lock);
154 spin_lock(&p->d_lock);
156 next = p->d_subdirs.next;
157 if (next == &p->d_subdirs) {
159 struct dentry *parent;
162 spin_unlock(&p->d_lock);
163 spin_unlock(&autofs4_lock);
168 parent = p->d_parent;
169 if (!spin_trylock(&parent->d_lock)) {
170 spin_unlock(&p->d_lock);
174 spin_unlock(&p->d_lock);
175 next = p->d_u.d_child.next;
177 if (next != &parent->d_subdirs)
181 ret = list_entry(next, struct dentry, d_u.d_child);
183 spin_lock_nested(&ret->d_lock, DENTRY_D_LOCK_NESTED);
184 /* Negative dentry - try next */
185 if (!simple_positive(ret)) {
186 spin_unlock(&p->d_lock);
191 spin_unlock(&ret->d_lock);
192 spin_unlock(&p->d_lock);
193 spin_unlock(&autofs4_lock);
201 * Check a direct mount point for busyness.
202 * Direct mounts have similar expiry semantics to tree mounts.
203 * The tree is not busy iff no mountpoints are busy and there are no
206 static int autofs4_direct_busy(struct vfsmount *mnt,
208 unsigned long timeout,
211 DPRINTK("top %p %.*s",
212 top, (int) top->d_name.len, top->d_name.name);
214 /* If it's busy update the expiry counters */
215 if (!may_umount_tree(mnt)) {
216 struct autofs_info *ino = autofs4_dentry_ino(top);
218 ino->last_used = jiffies;
222 /* Timeout of a direct mount is determined by its top dentry */
223 if (!autofs4_can_expire(top, timeout, do_now))
229 /* Check a directory tree of mount points for busyness
230 * The tree is not busy iff no mountpoints are busy
232 static int autofs4_tree_busy(struct vfsmount *mnt,
234 unsigned long timeout,
237 struct autofs_info *top_ino = autofs4_dentry_ino(top);
240 DPRINTK("top %p %.*s",
241 top, (int)top->d_name.len, top->d_name.name);
243 /* Negative dentry - give up */
244 if (!simple_positive(top))
248 while ((p = get_next_positive_dentry(p, top))) {
249 DPRINTK("dentry %p %.*s",
250 p, (int) p->d_name.len, p->d_name.name);
253 * Is someone visiting anywhere in the subtree ?
254 * If there's no mount we need to check the usage
255 * count for the autofs dentry.
256 * If the fs is busy update the expiry counter.
258 if (d_mountpoint(p)) {
259 if (autofs4_mount_busy(mnt, p)) {
260 top_ino->last_used = jiffies;
265 struct autofs_info *ino = autofs4_dentry_ino(p);
266 unsigned int ino_count = atomic_read(&ino->count);
269 * Clean stale dentries below that have not been
270 * invalidated after a mount fail during lookup
274 /* allow for dget above and top is already dgot */
280 if (p->d_count > ino_count) {
281 top_ino->last_used = jiffies;
288 /* Timeout of a tree mount is ultimately determined by its top dentry */
289 if (!autofs4_can_expire(top, timeout, do_now))
295 static struct dentry *autofs4_check_leaves(struct vfsmount *mnt,
296 struct dentry *parent,
297 unsigned long timeout,
302 DPRINTK("parent %p %.*s",
303 parent, (int)parent->d_name.len, parent->d_name.name);
306 while ((p = get_next_positive_dentry(p, parent))) {
307 DPRINTK("dentry %p %.*s",
308 p, (int) p->d_name.len, p->d_name.name);
310 if (d_mountpoint(p)) {
311 /* Can we umount this guy */
312 if (autofs4_mount_busy(mnt, p))
315 /* Can we expire this guy */
316 if (autofs4_can_expire(p, timeout, do_now))
323 /* Check if we can expire a direct mount (possibly a tree) */
324 struct dentry *autofs4_expire_direct(struct super_block *sb,
325 struct vfsmount *mnt,
326 struct autofs_sb_info *sbi,
329 unsigned long timeout;
330 struct dentry *root = dget(sb->s_root);
331 int do_now = how & AUTOFS_EXP_IMMEDIATE;
332 struct autofs_info *ino;
338 timeout = sbi->exp_timeout;
340 spin_lock(&sbi->fs_lock);
341 ino = autofs4_dentry_ino(root);
342 /* No point expiring a pending mount */
343 if (ino->flags & AUTOFS_INF_PENDING)
345 if (!autofs4_direct_busy(mnt, root, timeout, do_now)) {
346 struct autofs_info *ino = autofs4_dentry_ino(root);
347 ino->flags |= AUTOFS_INF_EXPIRING;
348 init_completion(&ino->expire_complete);
349 spin_unlock(&sbi->fs_lock);
353 spin_unlock(&sbi->fs_lock);
360 * Find an eligible tree to time-out
361 * A tree is eligible if :-
362 * - it is unused by any user process
363 * - it has been unused for exp_timeout time
365 struct dentry *autofs4_expire_indirect(struct super_block *sb,
366 struct vfsmount *mnt,
367 struct autofs_sb_info *sbi,
370 unsigned long timeout;
371 struct dentry *root = sb->s_root;
372 struct dentry *dentry;
373 struct dentry *expired = NULL;
374 int do_now = how & AUTOFS_EXP_IMMEDIATE;
375 int exp_leaves = how & AUTOFS_EXP_LEAVES;
376 struct autofs_info *ino;
377 unsigned int ino_count;
383 timeout = sbi->exp_timeout;
386 while ((dentry = get_next_positive_subdir(dentry, root))) {
387 spin_lock(&sbi->fs_lock);
388 ino = autofs4_dentry_ino(dentry);
389 /* No point expiring a pending mount */
390 if (ino->flags & AUTOFS_INF_PENDING)
394 * Case 1: (i) indirect mount or top level pseudo direct mount
396 * (ii) indirect mount with offset mount, check the "/"
397 * offset (autofs-5.0+).
399 if (d_mountpoint(dentry)) {
400 DPRINTK("checking mountpoint %p %.*s",
401 dentry, (int)dentry->d_name.len, dentry->d_name.name);
403 /* Path walk currently on this dentry? */
404 ino_count = atomic_read(&ino->count) + 2;
405 if (dentry->d_count > ino_count)
408 /* Can we umount this guy */
409 if (autofs4_mount_busy(mnt, dentry))
412 /* Can we expire this guy */
413 if (autofs4_can_expire(dentry, timeout, do_now)) {
420 if (simple_empty(dentry))
423 /* Case 2: tree mount, expire iff entire tree is not busy */
425 /* Path walk currently on this dentry? */
426 ino_count = atomic_read(&ino->count) + 1;
427 if (dentry->d_count > ino_count)
430 if (!autofs4_tree_busy(mnt, dentry, timeout, do_now)) {
435 * Case 3: pseudo direct mount, expire individual leaves
439 /* Path walk currently on this dentry? */
440 ino_count = atomic_read(&ino->count) + 1;
441 if (dentry->d_count > ino_count)
444 expired = autofs4_check_leaves(mnt, dentry, timeout, do_now);
451 spin_unlock(&sbi->fs_lock);
456 DPRINTK("returning %p %.*s",
457 expired, (int)expired->d_name.len, expired->d_name.name);
458 ino = autofs4_dentry_ino(expired);
459 ino->flags |= AUTOFS_INF_EXPIRING;
460 init_completion(&ino->expire_complete);
461 spin_unlock(&sbi->fs_lock);
462 spin_lock(&autofs4_lock);
463 spin_lock(&expired->d_parent->d_lock);
464 spin_lock_nested(&expired->d_lock, DENTRY_D_LOCK_NESTED);
465 list_move(&expired->d_parent->d_subdirs, &expired->d_u.d_child);
466 spin_unlock(&expired->d_lock);
467 spin_unlock(&expired->d_parent->d_lock);
468 spin_unlock(&autofs4_lock);
472 int autofs4_expire_wait(struct dentry *dentry)
474 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
475 struct autofs_info *ino = autofs4_dentry_ino(dentry);
478 /* Block on any pending expire */
479 spin_lock(&sbi->fs_lock);
480 if (ino->flags & AUTOFS_INF_EXPIRING) {
481 spin_unlock(&sbi->fs_lock);
483 DPRINTK("waiting for expire %p name=%.*s",
484 dentry, dentry->d_name.len, dentry->d_name.name);
486 status = autofs4_wait(sbi, dentry, NFY_NONE);
487 wait_for_completion(&ino->expire_complete);
489 DPRINTK("expire done status=%d", status);
491 if (d_unhashed(dentry))
496 spin_unlock(&sbi->fs_lock);
501 /* Perform an expiry operation */
502 int autofs4_expire_run(struct super_block *sb,
503 struct vfsmount *mnt,
504 struct autofs_sb_info *sbi,
505 struct autofs_packet_expire __user *pkt_p)
507 struct autofs_packet_expire pkt;
508 struct autofs_info *ino;
509 struct dentry *dentry;
512 memset(&pkt,0,sizeof pkt);
514 pkt.hdr.proto_version = sbi->version;
515 pkt.hdr.type = autofs_ptype_expire;
517 if ((dentry = autofs4_expire_indirect(sb, mnt, sbi, 0)) == NULL)
520 pkt.len = dentry->d_name.len;
521 memcpy(pkt.name, dentry->d_name.name, pkt.len);
522 pkt.name[pkt.len] = '\0';
525 if ( copy_to_user(pkt_p, &pkt, sizeof(struct autofs_packet_expire)) )
528 spin_lock(&sbi->fs_lock);
529 ino = autofs4_dentry_ino(dentry);
530 ino->flags &= ~AUTOFS_INF_EXPIRING;
531 complete_all(&ino->expire_complete);
532 spin_unlock(&sbi->fs_lock);
537 int autofs4_do_expire_multi(struct super_block *sb, struct vfsmount *mnt,
538 struct autofs_sb_info *sbi, int when)
540 struct dentry *dentry;
543 if (autofs_type_trigger(sbi->type))
544 dentry = autofs4_expire_direct(sb, mnt, sbi, when);
546 dentry = autofs4_expire_indirect(sb, mnt, sbi, when);
549 struct autofs_info *ino = autofs4_dentry_ino(dentry);
551 /* This is synchronous because it makes the daemon a
553 ret = autofs4_wait(sbi, dentry, NFY_EXPIRE);
555 spin_lock(&sbi->fs_lock);
556 ino->flags &= ~AUTOFS_INF_EXPIRING;
557 spin_lock(&dentry->d_lock);
559 if ((IS_ROOT(dentry) ||
560 (autofs_type_indirect(sbi->type) &&
561 IS_ROOT(dentry->d_parent))) &&
562 !(dentry->d_flags & DCACHE_NEED_AUTOMOUNT))
563 __managed_dentry_set_automount(dentry);
565 spin_unlock(&dentry->d_lock);
566 complete_all(&ino->expire_complete);
567 spin_unlock(&sbi->fs_lock);
574 /* Call repeatedly until it returns -EAGAIN, meaning there's nothing
576 int autofs4_expire_multi(struct super_block *sb, struct vfsmount *mnt,
577 struct autofs_sb_info *sbi, int __user *arg)
581 if (arg && get_user(do_now, arg))
584 return autofs4_do_expire_multi(sb, mnt, sbi, do_now);