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 (!autofs4_follow_mount(&mnt
, &dentry
))
62 /* This is an autofs submount, we can't expire it */
63 if (is_autofs4_dentry(dentry
))
66 /* Update the expiry counter if fs is busy */
67 if (!may_umount_tree(mnt
)) {
68 struct autofs_info
*ino
= autofs4_dentry_ino(top
);
69 ino
->last_used
= jiffies
;
75 DPRINTK("returning = %d", status
);
82 * Calculate next entry in top down tree traversal.
83 * From next_mnt in namespace.c - elegant.
85 static struct dentry
*next_dentry(struct dentry
*p
, struct dentry
*root
)
87 struct list_head
*next
= p
->d_subdirs
.next
;
89 if (next
== &p
->d_subdirs
) {
93 next
= p
->d_u
.d_child
.next
;
94 if (next
!= &p
->d_parent
->d_subdirs
)
99 return list_entry(next
, struct dentry
, d_u
.d_child
);
103 * Check a direct mount point for busyness.
104 * Direct mounts have similar expiry semantics to tree mounts.
105 * The tree is not busy iff no mountpoints are busy and there are no
108 static int autofs4_direct_busy(struct vfsmount
*mnt
,
110 unsigned long timeout
,
113 DPRINTK("top %p %.*s",
114 top
, (int) top
->d_name
.len
, top
->d_name
.name
);
116 /* If it's busy update the expiry counters */
117 if (!may_umount_tree(mnt
)) {
118 struct autofs_info
*ino
= autofs4_dentry_ino(top
);
120 ino
->last_used
= jiffies
;
124 /* Timeout of a direct mount is determined by its top dentry */
125 if (!autofs4_can_expire(top
, timeout
, do_now
))
131 /* Check a directory tree of mount points for busyness
132 * The tree is not busy iff no mountpoints are busy
134 static int autofs4_tree_busy(struct vfsmount
*mnt
,
136 unsigned long timeout
,
139 struct autofs_info
*top_ino
= autofs4_dentry_ino(top
);
142 DPRINTK("top %p %.*s",
143 top
, (int)top
->d_name
.len
, top
->d_name
.name
);
145 /* Negative dentry - give up */
146 if (!simple_positive(top
))
149 spin_lock(&dcache_lock
);
150 for (p
= top
; p
; p
= next_dentry(p
, top
)) {
151 /* Negative dentry - give up */
152 if (!simple_positive(p
))
155 DPRINTK("dentry %p %.*s",
156 p
, (int) p
->d_name
.len
, p
->d_name
.name
);
159 spin_unlock(&dcache_lock
);
162 * Is someone visiting anywhere in the subtree ?
163 * If there's no mount we need to check the usage
164 * count for the autofs dentry.
165 * If the fs is busy update the expiry counter.
167 if (d_mountpoint(p
)) {
168 if (autofs4_mount_busy(mnt
, p
)) {
169 top_ino
->last_used
= jiffies
;
174 struct autofs_info
*ino
= autofs4_dentry_ino(p
);
175 unsigned int ino_count
= atomic_read(&ino
->count
);
178 * Clean stale dentries below that have not been
179 * invalidated after a mount fail during lookup
183 /* allow for dget above and top is already dgot */
189 if (atomic_read(&p
->d_count
) > ino_count
) {
190 top_ino
->last_used
= jiffies
;
196 spin_lock(&dcache_lock
);
198 spin_unlock(&dcache_lock
);
200 /* Timeout of a tree mount is ultimately determined by its top dentry */
201 if (!autofs4_can_expire(top
, timeout
, do_now
))
207 static struct dentry
*autofs4_check_leaves(struct vfsmount
*mnt
,
208 struct dentry
*parent
,
209 unsigned long timeout
,
214 DPRINTK("parent %p %.*s",
215 parent
, (int)parent
->d_name
.len
, parent
->d_name
.name
);
217 spin_lock(&dcache_lock
);
218 for (p
= parent
; p
; p
= next_dentry(p
, parent
)) {
219 /* Negative dentry - give up */
220 if (!simple_positive(p
))
223 DPRINTK("dentry %p %.*s",
224 p
, (int) p
->d_name
.len
, p
->d_name
.name
);
227 spin_unlock(&dcache_lock
);
229 if (d_mountpoint(p
)) {
230 /* Can we umount this guy */
231 if (autofs4_mount_busy(mnt
, p
))
234 /* Can we expire this guy */
235 if (autofs4_can_expire(p
, timeout
, do_now
))
240 spin_lock(&dcache_lock
);
242 spin_unlock(&dcache_lock
);
246 /* Check if we can expire a direct mount (possibly a tree) */
247 static struct dentry
*autofs4_expire_direct(struct super_block
*sb
,
248 struct vfsmount
*mnt
,
249 struct autofs_sb_info
*sbi
,
252 unsigned long timeout
;
253 struct dentry
*root
= dget(sb
->s_root
);
254 int do_now
= how
& AUTOFS_EXP_IMMEDIATE
;
260 timeout
= sbi
->exp_timeout
;
262 spin_lock(&sbi
->fs_lock
);
263 if (!autofs4_direct_busy(mnt
, root
, timeout
, do_now
)) {
264 struct autofs_info
*ino
= autofs4_dentry_ino(root
);
265 if (d_mountpoint(root
)) {
266 ino
->flags
|= AUTOFS_INF_MOUNTPOINT
;
269 ino
->flags
|= AUTOFS_INF_EXPIRING
;
270 init_completion(&ino
->expire_complete
);
271 spin_unlock(&sbi
->fs_lock
);
274 spin_unlock(&sbi
->fs_lock
);
281 * Find an eligible tree to time-out
282 * A tree is eligible if :-
283 * - it is unused by any user process
284 * - it has been unused for exp_timeout time
286 static struct dentry
*autofs4_expire_indirect(struct super_block
*sb
,
287 struct vfsmount
*mnt
,
288 struct autofs_sb_info
*sbi
,
291 unsigned long timeout
;
292 struct dentry
*root
= sb
->s_root
;
293 struct dentry
*expired
= NULL
;
294 struct list_head
*next
;
295 int do_now
= how
& AUTOFS_EXP_IMMEDIATE
;
296 int exp_leaves
= how
& AUTOFS_EXP_LEAVES
;
297 struct autofs_info
*ino
;
298 unsigned int ino_count
;
304 timeout
= sbi
->exp_timeout
;
306 spin_lock(&dcache_lock
);
307 next
= root
->d_subdirs
.next
;
309 /* On exit from the loop expire is set to a dgot dentry
310 * to expire or it's NULL */
311 while ( next
!= &root
->d_subdirs
) {
312 struct dentry
*dentry
= list_entry(next
, struct dentry
, d_u
.d_child
);
314 /* Negative dentry - give up */
315 if (!simple_positive(dentry
)) {
320 dentry
= dget(dentry
);
321 spin_unlock(&dcache_lock
);
323 spin_lock(&sbi
->fs_lock
);
324 ino
= autofs4_dentry_ino(dentry
);
327 * Case 1: (i) indirect mount or top level pseudo direct mount
329 * (ii) indirect mount with offset mount, check the "/"
330 * offset (autofs-5.0+).
332 if (d_mountpoint(dentry
)) {
333 DPRINTK("checking mountpoint %p %.*s",
334 dentry
, (int)dentry
->d_name
.len
, dentry
->d_name
.name
);
336 /* Path walk currently on this dentry? */
337 ino_count
= atomic_read(&ino
->count
) + 2;
338 if (atomic_read(&dentry
->d_count
) > ino_count
)
341 /* Can we umount this guy */
342 if (autofs4_mount_busy(mnt
, dentry
))
345 /* Can we expire this guy */
346 if (autofs4_can_expire(dentry
, timeout
, do_now
)) {
353 if (simple_empty(dentry
))
356 /* Case 2: tree mount, expire iff entire tree is not busy */
358 /* Path walk currently on this dentry? */
359 ino_count
= atomic_read(&ino
->count
) + 1;
360 if (atomic_read(&dentry
->d_count
) > ino_count
)
363 if (!autofs4_tree_busy(mnt
, dentry
, timeout
, do_now
)) {
368 * Case 3: pseudo direct mount, expire individual leaves
372 /* Path walk currently on this dentry? */
373 ino_count
= atomic_read(&ino
->count
) + 1;
374 if (atomic_read(&dentry
->d_count
) > ino_count
)
377 expired
= autofs4_check_leaves(mnt
, dentry
, timeout
, do_now
);
384 spin_unlock(&sbi
->fs_lock
);
386 spin_lock(&dcache_lock
);
389 spin_unlock(&dcache_lock
);
393 DPRINTK("returning %p %.*s",
394 expired
, (int)expired
->d_name
.len
, expired
->d_name
.name
);
395 ino
= autofs4_dentry_ino(expired
);
396 ino
->flags
|= AUTOFS_INF_EXPIRING
;
397 init_completion(&ino
->expire_complete
);
398 spin_unlock(&sbi
->fs_lock
);
399 spin_lock(&dcache_lock
);
400 list_move(&expired
->d_parent
->d_subdirs
, &expired
->d_u
.d_child
);
401 spin_unlock(&dcache_lock
);
405 int autofs4_expire_wait(struct dentry
*dentry
)
407 struct autofs_sb_info
*sbi
= autofs4_sbi(dentry
->d_sb
);
408 struct autofs_info
*ino
= autofs4_dentry_ino(dentry
);
411 /* Block on any pending expire */
412 spin_lock(&sbi
->fs_lock
);
413 if (ino
->flags
& AUTOFS_INF_EXPIRING
) {
414 spin_unlock(&sbi
->fs_lock
);
416 DPRINTK("waiting for expire %p name=%.*s",
417 dentry
, dentry
->d_name
.len
, dentry
->d_name
.name
);
419 status
= autofs4_wait(sbi
, dentry
, NFY_NONE
);
420 wait_for_completion(&ino
->expire_complete
);
422 DPRINTK("expire done status=%d", status
);
424 if (d_unhashed(dentry
))
429 spin_unlock(&sbi
->fs_lock
);
434 /* Perform an expiry operation */
435 int autofs4_expire_run(struct super_block
*sb
,
436 struct vfsmount
*mnt
,
437 struct autofs_sb_info
*sbi
,
438 struct autofs_packet_expire __user
*pkt_p
)
440 struct autofs_packet_expire pkt
;
441 struct autofs_info
*ino
;
442 struct dentry
*dentry
;
445 memset(&pkt
,0,sizeof pkt
);
447 pkt
.hdr
.proto_version
= sbi
->version
;
448 pkt
.hdr
.type
= autofs_ptype_expire
;
450 if ((dentry
= autofs4_expire_indirect(sb
, mnt
, sbi
, 0)) == NULL
)
453 pkt
.len
= dentry
->d_name
.len
;
454 memcpy(pkt
.name
, dentry
->d_name
.name
, pkt
.len
);
455 pkt
.name
[pkt
.len
] = '\0';
458 if ( copy_to_user(pkt_p
, &pkt
, sizeof(struct autofs_packet_expire
)) )
461 spin_lock(&sbi
->fs_lock
);
462 ino
= autofs4_dentry_ino(dentry
);
463 ino
->flags
&= ~AUTOFS_INF_EXPIRING
;
464 complete_all(&ino
->expire_complete
);
465 spin_unlock(&sbi
->fs_lock
);
470 /* Call repeatedly until it returns -EAGAIN, meaning there's nothing
472 int autofs4_expire_multi(struct super_block
*sb
, struct vfsmount
*mnt
,
473 struct autofs_sb_info
*sbi
, int __user
*arg
)
475 struct dentry
*dentry
;
479 if (arg
&& get_user(do_now
, arg
))
482 if (sbi
->type
& AUTOFS_TYPE_DIRECT
)
483 dentry
= autofs4_expire_direct(sb
, mnt
, sbi
, do_now
);
485 dentry
= autofs4_expire_indirect(sb
, mnt
, sbi
, do_now
);
488 struct autofs_info
*ino
= autofs4_dentry_ino(dentry
);
490 /* This is synchronous because it makes the daemon a
492 ret
= autofs4_wait(sbi
, dentry
, NFY_EXPIRE
);
494 spin_lock(&sbi
->fs_lock
);
495 if (ino
->flags
& AUTOFS_INF_MOUNTPOINT
) {
496 sb
->s_root
->d_mounted
++;
497 ino
->flags
&= ~AUTOFS_INF_MOUNTPOINT
;
499 ino
->flags
&= ~AUTOFS_INF_EXPIRING
;
500 complete_all(&ino
->expire_complete
);
501 spin_unlock(&sbi
->fs_lock
);