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 (dentry
->d_flags
& DCACHE_AUTOFS_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(&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(mnt
)) {
81 struct autofs_info
*ino
= autofs4_dentry_ino(top
);
82 ino
->last_used
= jiffies
;
88 DPRINTK("returning = %d", status
);
94 * Calculate next entry in top down tree traversal.
95 * From next_mnt in namespace.c - elegant.
97 static struct dentry
*next_dentry(struct dentry
*p
, struct dentry
*root
)
99 struct list_head
*next
= p
->d_subdirs
.next
;
101 if (next
== &p
->d_subdirs
) {
105 next
= p
->d_u
.d_child
.next
;
106 if (next
!= &p
->d_parent
->d_subdirs
)
111 return list_entry(next
, struct dentry
, d_u
.d_child
);
115 * Check a direct mount point for busyness.
116 * Direct mounts have similar expiry semantics to tree mounts.
117 * The tree is not busy iff no mountpoints are busy and there are no
120 static int autofs4_direct_busy(struct vfsmount
*mnt
,
122 unsigned long timeout
,
125 DPRINTK("top %p %.*s",
126 top
, (int) top
->d_name
.len
, top
->d_name
.name
);
128 /* If it's busy update the expiry counters */
129 if (!may_umount_tree(mnt
)) {
130 struct autofs_info
*ino
= autofs4_dentry_ino(top
);
132 ino
->last_used
= jiffies
;
136 /* Timeout of a direct mount is determined by its top dentry */
137 if (!autofs4_can_expire(top
, timeout
, do_now
))
143 /* Check a directory tree of mount points for busyness
144 * The tree is not busy iff no mountpoints are busy
146 static int autofs4_tree_busy(struct vfsmount
*mnt
,
148 unsigned long timeout
,
151 struct autofs_info
*top_ino
= autofs4_dentry_ino(top
);
154 DPRINTK("top %p %.*s",
155 top
, (int)top
->d_name
.len
, top
->d_name
.name
);
157 /* Negative dentry - give up */
158 if (!simple_positive(top
))
161 spin_lock(&dcache_lock
);
162 for (p
= top
; p
; p
= next_dentry(p
, top
)) {
163 /* Negative dentry - give up */
164 if (!simple_positive(p
))
167 DPRINTK("dentry %p %.*s",
168 p
, (int) p
->d_name
.len
, p
->d_name
.name
);
171 spin_unlock(&dcache_lock
);
174 * Is someone visiting anywhere in the subtree ?
175 * If there's no mount we need to check the usage
176 * count for the autofs dentry.
177 * If the fs is busy update the expiry counter.
179 if (d_mountpoint(p
)) {
180 if (autofs4_mount_busy(mnt
, p
)) {
181 top_ino
->last_used
= jiffies
;
186 struct autofs_info
*ino
= autofs4_dentry_ino(p
);
187 unsigned int ino_count
= atomic_read(&ino
->count
);
190 * Clean stale dentries below that have not been
191 * invalidated after a mount fail during lookup
195 /* allow for dget above and top is already dgot */
201 if (atomic_read(&p
->d_count
) > ino_count
) {
202 top_ino
->last_used
= jiffies
;
208 spin_lock(&dcache_lock
);
210 spin_unlock(&dcache_lock
);
212 /* Timeout of a tree mount is ultimately determined by its top dentry */
213 if (!autofs4_can_expire(top
, timeout
, do_now
))
219 static struct dentry
*autofs4_check_leaves(struct vfsmount
*mnt
,
220 struct dentry
*parent
,
221 unsigned long timeout
,
226 DPRINTK("parent %p %.*s",
227 parent
, (int)parent
->d_name
.len
, parent
->d_name
.name
);
229 spin_lock(&dcache_lock
);
230 for (p
= parent
; p
; p
= next_dentry(p
, parent
)) {
231 /* Negative dentry - give up */
232 if (!simple_positive(p
))
235 DPRINTK("dentry %p %.*s",
236 p
, (int) p
->d_name
.len
, p
->d_name
.name
);
239 spin_unlock(&dcache_lock
);
241 if (d_mountpoint(p
)) {
242 /* Can we umount this guy */
243 if (autofs4_mount_busy(mnt
, p
))
246 /* Can we expire this guy */
247 if (autofs4_can_expire(p
, timeout
, do_now
))
252 spin_lock(&dcache_lock
);
254 spin_unlock(&dcache_lock
);
258 /* Check if we can expire a direct mount (possibly a tree) */
259 struct dentry
*autofs4_expire_direct(struct super_block
*sb
,
260 struct vfsmount
*mnt
,
261 struct autofs_sb_info
*sbi
,
264 unsigned long timeout
;
265 struct dentry
*root
= dget(sb
->s_root
);
266 int do_now
= how
& AUTOFS_EXP_IMMEDIATE
;
272 timeout
= sbi
->exp_timeout
;
274 spin_lock(&sbi
->fs_lock
);
275 if (!autofs4_direct_busy(mnt
, root
, timeout
, do_now
)) {
276 struct autofs_info
*ino
= autofs4_dentry_ino(root
);
277 if (d_mountpoint(root
)) {
278 ino
->flags
|= AUTOFS_INF_MOUNTPOINT
;
281 ino
->flags
|= AUTOFS_INF_EXPIRING
;
282 init_completion(&ino
->expire_complete
);
283 spin_unlock(&sbi
->fs_lock
);
286 spin_unlock(&sbi
->fs_lock
);
293 * Find an eligible tree to time-out
294 * A tree is eligible if :-
295 * - it is unused by any user process
296 * - it has been unused for exp_timeout time
298 struct dentry
*autofs4_expire_indirect(struct super_block
*sb
,
299 struct vfsmount
*mnt
,
300 struct autofs_sb_info
*sbi
,
303 unsigned long timeout
;
304 struct dentry
*root
= sb
->s_root
;
305 struct dentry
*expired
= NULL
;
306 struct list_head
*next
;
307 int do_now
= how
& AUTOFS_EXP_IMMEDIATE
;
308 int exp_leaves
= how
& AUTOFS_EXP_LEAVES
;
309 struct autofs_info
*ino
;
310 unsigned int ino_count
;
316 timeout
= sbi
->exp_timeout
;
318 spin_lock(&dcache_lock
);
319 next
= root
->d_subdirs
.next
;
321 /* On exit from the loop expire is set to a dgot dentry
322 * to expire or it's NULL */
323 while ( next
!= &root
->d_subdirs
) {
324 struct dentry
*dentry
= list_entry(next
, struct dentry
, d_u
.d_child
);
326 /* Negative dentry - give up */
327 if (!simple_positive(dentry
)) {
332 dentry
= dget(dentry
);
333 spin_unlock(&dcache_lock
);
335 spin_lock(&sbi
->fs_lock
);
336 ino
= autofs4_dentry_ino(dentry
);
339 * Case 1: (i) indirect mount or top level pseudo direct mount
341 * (ii) indirect mount with offset mount, check the "/"
342 * offset (autofs-5.0+).
344 if (d_mountpoint(dentry
)) {
345 DPRINTK("checking mountpoint %p %.*s",
346 dentry
, (int)dentry
->d_name
.len
, dentry
->d_name
.name
);
348 /* Path walk currently on this dentry? */
349 ino_count
= atomic_read(&ino
->count
) + 2;
350 if (atomic_read(&dentry
->d_count
) > ino_count
)
353 /* Can we umount this guy */
354 if (autofs4_mount_busy(mnt
, dentry
))
357 /* Can we expire this guy */
358 if (autofs4_can_expire(dentry
, timeout
, do_now
)) {
365 if (simple_empty(dentry
))
368 /* Case 2: tree mount, expire iff entire tree is not busy */
370 /* Path walk currently on this dentry? */
371 ino_count
= atomic_read(&ino
->count
) + 1;
372 if (atomic_read(&dentry
->d_count
) > ino_count
)
375 if (!autofs4_tree_busy(mnt
, dentry
, timeout
, do_now
)) {
380 * Case 3: pseudo direct mount, expire individual leaves
384 /* Path walk currently on this dentry? */
385 ino_count
= atomic_read(&ino
->count
) + 1;
386 if (atomic_read(&dentry
->d_count
) > ino_count
)
389 expired
= autofs4_check_leaves(mnt
, dentry
, timeout
, do_now
);
396 spin_unlock(&sbi
->fs_lock
);
398 spin_lock(&dcache_lock
);
401 spin_unlock(&dcache_lock
);
405 DPRINTK("returning %p %.*s",
406 expired
, (int)expired
->d_name
.len
, expired
->d_name
.name
);
407 ino
= autofs4_dentry_ino(expired
);
408 ino
->flags
|= AUTOFS_INF_EXPIRING
;
409 init_completion(&ino
->expire_complete
);
410 spin_unlock(&sbi
->fs_lock
);
411 spin_lock(&dcache_lock
);
412 list_move(&expired
->d_parent
->d_subdirs
, &expired
->d_u
.d_child
);
413 spin_unlock(&dcache_lock
);
417 int autofs4_expire_wait(struct dentry
*dentry
)
419 struct autofs_sb_info
*sbi
= autofs4_sbi(dentry
->d_sb
);
420 struct autofs_info
*ino
= autofs4_dentry_ino(dentry
);
423 /* Block on any pending expire */
424 spin_lock(&sbi
->fs_lock
);
425 if (ino
->flags
& AUTOFS_INF_EXPIRING
) {
426 spin_unlock(&sbi
->fs_lock
);
428 DPRINTK("waiting for expire %p name=%.*s",
429 dentry
, dentry
->d_name
.len
, dentry
->d_name
.name
);
431 status
= autofs4_wait(sbi
, dentry
, NFY_NONE
);
432 wait_for_completion(&ino
->expire_complete
);
434 DPRINTK("expire done status=%d", status
);
436 if (d_unhashed(dentry
))
441 spin_unlock(&sbi
->fs_lock
);
446 /* Perform an expiry operation */
447 int autofs4_expire_run(struct super_block
*sb
,
448 struct vfsmount
*mnt
,
449 struct autofs_sb_info
*sbi
,
450 struct autofs_packet_expire __user
*pkt_p
)
452 struct autofs_packet_expire pkt
;
453 struct autofs_info
*ino
;
454 struct dentry
*dentry
;
457 memset(&pkt
,0,sizeof pkt
);
459 pkt
.hdr
.proto_version
= sbi
->version
;
460 pkt
.hdr
.type
= autofs_ptype_expire
;
462 if ((dentry
= autofs4_expire_indirect(sb
, mnt
, sbi
, 0)) == NULL
)
465 pkt
.len
= dentry
->d_name
.len
;
466 memcpy(pkt
.name
, dentry
->d_name
.name
, pkt
.len
);
467 pkt
.name
[pkt
.len
] = '\0';
470 if ( copy_to_user(pkt_p
, &pkt
, sizeof(struct autofs_packet_expire
)) )
473 spin_lock(&sbi
->fs_lock
);
474 ino
= autofs4_dentry_ino(dentry
);
475 ino
->flags
&= ~AUTOFS_INF_EXPIRING
;
476 complete_all(&ino
->expire_complete
);
477 spin_unlock(&sbi
->fs_lock
);
482 int autofs4_do_expire_multi(struct super_block
*sb
, struct vfsmount
*mnt
,
483 struct autofs_sb_info
*sbi
, int when
)
485 struct dentry
*dentry
;
488 if (autofs_type_trigger(sbi
->type
))
489 dentry
= autofs4_expire_direct(sb
, mnt
, sbi
, when
);
491 dentry
= autofs4_expire_indirect(sb
, mnt
, sbi
, when
);
494 struct autofs_info
*ino
= autofs4_dentry_ino(dentry
);
496 /* This is synchronous because it makes the daemon a
498 ret
= autofs4_wait(sbi
, dentry
, NFY_EXPIRE
);
500 spin_lock(&sbi
->fs_lock
);
501 if (ino
->flags
& AUTOFS_INF_MOUNTPOINT
) {
502 sb
->s_root
->d_mounted
++;
503 ino
->flags
&= ~AUTOFS_INF_MOUNTPOINT
;
505 ino
->flags
&= ~AUTOFS_INF_EXPIRING
;
506 complete_all(&ino
->expire_complete
);
507 spin_unlock(&sbi
->fs_lock
);
514 /* Call repeatedly until it returns -EAGAIN, meaning there's nothing
516 int autofs4_expire_multi(struct super_block
*sb
, struct vfsmount
*mnt
,
517 struct autofs_sb_info
*sbi
, int __user
*arg
)
521 if (arg
&& get_user(do_now
, arg
))
524 return autofs4_do_expire_multi(sb
, mnt
, sbi
, do_now
);