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
;
53 DPRINTK("dentry %p %.*s",
54 dentry
, (int)dentry
->d_name
.len
, dentry
->d_name
.name
);
59 if (!follow_down(&mnt
, &dentry
))
62 if (is_autofs4_dentry(dentry
)) {
63 struct autofs_sb_info
*sbi
= autofs4_sbi(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(dentry
))
77 /* Update the expiry counter if fs is busy */
78 if (!may_umount_tree(mnt
)) {
79 struct autofs_info
*ino
= autofs4_dentry_ino(top
);
80 ino
->last_used
= jiffies
;
86 DPRINTK("returning = %d", status
);
93 * Calculate next entry in top down tree traversal.
94 * From next_mnt in namespace.c - elegant.
96 static struct dentry
*next_dentry(struct dentry
*p
, struct dentry
*root
)
98 struct list_head
*next
= p
->d_subdirs
.next
;
100 if (next
== &p
->d_subdirs
) {
104 next
= p
->d_u
.d_child
.next
;
105 if (next
!= &p
->d_parent
->d_subdirs
)
110 return list_entry(next
, struct dentry
, d_u
.d_child
);
114 * Check a direct mount point for busyness.
115 * Direct mounts have similar expiry semantics to tree mounts.
116 * The tree is not busy iff no mountpoints are busy and there are no
119 static int autofs4_direct_busy(struct vfsmount
*mnt
,
121 unsigned long timeout
,
124 DPRINTK("top %p %.*s",
125 top
, (int) top
->d_name
.len
, top
->d_name
.name
);
127 /* If it's busy update the expiry counters */
128 if (!may_umount_tree(mnt
)) {
129 struct autofs_info
*ino
= autofs4_dentry_ino(top
);
131 ino
->last_used
= jiffies
;
135 /* Timeout of a direct mount is determined by its top dentry */
136 if (!autofs4_can_expire(top
, timeout
, do_now
))
142 /* Check a directory tree of mount points for busyness
143 * The tree is not busy iff no mountpoints are busy
145 static int autofs4_tree_busy(struct vfsmount
*mnt
,
147 unsigned long timeout
,
150 struct autofs_info
*top_ino
= autofs4_dentry_ino(top
);
153 DPRINTK("top %p %.*s",
154 top
, (int)top
->d_name
.len
, top
->d_name
.name
);
156 /* Negative dentry - give up */
157 if (!simple_positive(top
))
160 spin_lock(&dcache_lock
);
161 for (p
= top
; p
; p
= next_dentry(p
, top
)) {
162 /* Negative dentry - give up */
163 if (!simple_positive(p
))
166 DPRINTK("dentry %p %.*s",
167 p
, (int) p
->d_name
.len
, p
->d_name
.name
);
170 spin_unlock(&dcache_lock
);
173 * Is someone visiting anywhere in the subtree ?
174 * If there's no mount we need to check the usage
175 * count for the autofs dentry.
176 * If the fs is busy update the expiry counter.
178 if (d_mountpoint(p
)) {
179 if (autofs4_mount_busy(mnt
, p
)) {
180 top_ino
->last_used
= jiffies
;
185 struct autofs_info
*ino
= autofs4_dentry_ino(p
);
186 unsigned int ino_count
= atomic_read(&ino
->count
);
189 * Clean stale dentries below that have not been
190 * invalidated after a mount fail during lookup
194 /* allow for dget above and top is already dgot */
200 if (atomic_read(&p
->d_count
) > ino_count
) {
201 top_ino
->last_used
= jiffies
;
207 spin_lock(&dcache_lock
);
209 spin_unlock(&dcache_lock
);
211 /* Timeout of a tree mount is ultimately determined by its top dentry */
212 if (!autofs4_can_expire(top
, timeout
, do_now
))
218 static struct dentry
*autofs4_check_leaves(struct vfsmount
*mnt
,
219 struct dentry
*parent
,
220 unsigned long timeout
,
225 DPRINTK("parent %p %.*s",
226 parent
, (int)parent
->d_name
.len
, parent
->d_name
.name
);
228 spin_lock(&dcache_lock
);
229 for (p
= parent
; p
; p
= next_dentry(p
, parent
)) {
230 /* Negative dentry - give up */
231 if (!simple_positive(p
))
234 DPRINTK("dentry %p %.*s",
235 p
, (int) p
->d_name
.len
, p
->d_name
.name
);
238 spin_unlock(&dcache_lock
);
240 if (d_mountpoint(p
)) {
241 /* Can we umount this guy */
242 if (autofs4_mount_busy(mnt
, p
))
245 /* Can we expire this guy */
246 if (autofs4_can_expire(p
, timeout
, do_now
))
251 spin_lock(&dcache_lock
);
253 spin_unlock(&dcache_lock
);
257 /* Check if we can expire a direct mount (possibly a tree) */
258 struct dentry
*autofs4_expire_direct(struct super_block
*sb
,
259 struct vfsmount
*mnt
,
260 struct autofs_sb_info
*sbi
,
263 unsigned long timeout
;
264 struct dentry
*root
= dget(sb
->s_root
);
265 int do_now
= how
& AUTOFS_EXP_IMMEDIATE
;
271 timeout
= sbi
->exp_timeout
;
273 spin_lock(&sbi
->fs_lock
);
274 if (!autofs4_direct_busy(mnt
, root
, timeout
, do_now
)) {
275 struct autofs_info
*ino
= autofs4_dentry_ino(root
);
276 if (d_mountpoint(root
)) {
277 ino
->flags
|= AUTOFS_INF_MOUNTPOINT
;
280 ino
->flags
|= AUTOFS_INF_EXPIRING
;
281 init_completion(&ino
->expire_complete
);
282 spin_unlock(&sbi
->fs_lock
);
285 spin_unlock(&sbi
->fs_lock
);
292 * Find an eligible tree to time-out
293 * A tree is eligible if :-
294 * - it is unused by any user process
295 * - it has been unused for exp_timeout time
297 struct dentry
*autofs4_expire_indirect(struct super_block
*sb
,
298 struct vfsmount
*mnt
,
299 struct autofs_sb_info
*sbi
,
302 unsigned long timeout
;
303 struct dentry
*root
= sb
->s_root
;
304 struct dentry
*expired
= NULL
;
305 struct list_head
*next
;
306 int do_now
= how
& AUTOFS_EXP_IMMEDIATE
;
307 int exp_leaves
= how
& AUTOFS_EXP_LEAVES
;
308 struct autofs_info
*ino
;
309 unsigned int ino_count
;
315 timeout
= sbi
->exp_timeout
;
317 spin_lock(&dcache_lock
);
318 next
= root
->d_subdirs
.next
;
320 /* On exit from the loop expire is set to a dgot dentry
321 * to expire or it's NULL */
322 while ( next
!= &root
->d_subdirs
) {
323 struct dentry
*dentry
= list_entry(next
, struct dentry
, d_u
.d_child
);
325 /* Negative dentry - give up */
326 if (!simple_positive(dentry
)) {
331 dentry
= dget(dentry
);
332 spin_unlock(&dcache_lock
);
334 spin_lock(&sbi
->fs_lock
);
335 ino
= autofs4_dentry_ino(dentry
);
338 * Case 1: (i) indirect mount or top level pseudo direct mount
340 * (ii) indirect mount with offset mount, check the "/"
341 * offset (autofs-5.0+).
343 if (d_mountpoint(dentry
)) {
344 DPRINTK("checking mountpoint %p %.*s",
345 dentry
, (int)dentry
->d_name
.len
, dentry
->d_name
.name
);
347 /* Path walk currently on this dentry? */
348 ino_count
= atomic_read(&ino
->count
) + 2;
349 if (atomic_read(&dentry
->d_count
) > ino_count
)
352 /* Can we umount this guy */
353 if (autofs4_mount_busy(mnt
, dentry
))
356 /* Can we expire this guy */
357 if (autofs4_can_expire(dentry
, timeout
, do_now
)) {
364 if (simple_empty(dentry
))
367 /* Case 2: tree mount, expire iff entire tree is not busy */
369 /* Path walk currently on this dentry? */
370 ino_count
= atomic_read(&ino
->count
) + 1;
371 if (atomic_read(&dentry
->d_count
) > ino_count
)
374 if (!autofs4_tree_busy(mnt
, dentry
, timeout
, do_now
)) {
379 * Case 3: pseudo direct mount, expire individual leaves
383 /* Path walk currently on this dentry? */
384 ino_count
= atomic_read(&ino
->count
) + 1;
385 if (atomic_read(&dentry
->d_count
) > ino_count
)
388 expired
= autofs4_check_leaves(mnt
, dentry
, timeout
, do_now
);
395 spin_unlock(&sbi
->fs_lock
);
397 spin_lock(&dcache_lock
);
400 spin_unlock(&dcache_lock
);
404 DPRINTK("returning %p %.*s",
405 expired
, (int)expired
->d_name
.len
, expired
->d_name
.name
);
406 ino
= autofs4_dentry_ino(expired
);
407 ino
->flags
|= AUTOFS_INF_EXPIRING
;
408 init_completion(&ino
->expire_complete
);
409 spin_unlock(&sbi
->fs_lock
);
410 spin_lock(&dcache_lock
);
411 list_move(&expired
->d_parent
->d_subdirs
, &expired
->d_u
.d_child
);
412 spin_unlock(&dcache_lock
);
416 int autofs4_expire_wait(struct dentry
*dentry
)
418 struct autofs_sb_info
*sbi
= autofs4_sbi(dentry
->d_sb
);
419 struct autofs_info
*ino
= autofs4_dentry_ino(dentry
);
422 /* Block on any pending expire */
423 spin_lock(&sbi
->fs_lock
);
424 if (ino
->flags
& AUTOFS_INF_EXPIRING
) {
425 spin_unlock(&sbi
->fs_lock
);
427 DPRINTK("waiting for expire %p name=%.*s",
428 dentry
, dentry
->d_name
.len
, dentry
->d_name
.name
);
430 status
= autofs4_wait(sbi
, dentry
, NFY_NONE
);
431 wait_for_completion(&ino
->expire_complete
);
433 DPRINTK("expire done status=%d", status
);
435 if (d_unhashed(dentry
))
440 spin_unlock(&sbi
->fs_lock
);
445 /* Perform an expiry operation */
446 int autofs4_expire_run(struct super_block
*sb
,
447 struct vfsmount
*mnt
,
448 struct autofs_sb_info
*sbi
,
449 struct autofs_packet_expire __user
*pkt_p
)
451 struct autofs_packet_expire pkt
;
452 struct autofs_info
*ino
;
453 struct dentry
*dentry
;
456 memset(&pkt
,0,sizeof pkt
);
458 pkt
.hdr
.proto_version
= sbi
->version
;
459 pkt
.hdr
.type
= autofs_ptype_expire
;
461 if ((dentry
= autofs4_expire_indirect(sb
, mnt
, sbi
, 0)) == NULL
)
464 pkt
.len
= dentry
->d_name
.len
;
465 memcpy(pkt
.name
, dentry
->d_name
.name
, pkt
.len
);
466 pkt
.name
[pkt
.len
] = '\0';
469 if ( copy_to_user(pkt_p
, &pkt
, sizeof(struct autofs_packet_expire
)) )
472 spin_lock(&sbi
->fs_lock
);
473 ino
= autofs4_dentry_ino(dentry
);
474 ino
->flags
&= ~AUTOFS_INF_EXPIRING
;
475 complete_all(&ino
->expire_complete
);
476 spin_unlock(&sbi
->fs_lock
);
481 /* Call repeatedly until it returns -EAGAIN, meaning there's nothing
483 int autofs4_expire_multi(struct super_block
*sb
, struct vfsmount
*mnt
,
484 struct autofs_sb_info
*sbi
, int __user
*arg
)
486 struct dentry
*dentry
;
490 if (arg
&& get_user(do_now
, arg
))
493 if (autofs_type_trigger(sbi
->type
))
494 dentry
= autofs4_expire_direct(sb
, mnt
, sbi
, do_now
);
496 dentry
= autofs4_expire_indirect(sb
, mnt
, sbi
, do_now
);
499 struct autofs_info
*ino
= autofs4_dentry_ino(dentry
);
501 /* This is synchronous because it makes the daemon a
503 ret
= autofs4_wait(sbi
, dentry
, NFY_EXPIRE
);
505 spin_lock(&sbi
->fs_lock
);
506 if (ino
->flags
& AUTOFS_INF_MOUNTPOINT
) {
507 sb
->s_root
->d_mounted
++;
508 ino
->flags
&= ~AUTOFS_INF_MOUNTPOINT
;
510 ino
->flags
&= ~AUTOFS_INF_EXPIRING
;
511 complete_all(&ino
->expire_complete
);
512 spin_unlock(&sbi
->fs_lock
);