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
)) {
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
);
95 * Calculate next entry in top down tree traversal.
96 * From next_mnt in namespace.c - elegant.
98 static struct dentry
*next_dentry(struct dentry
*p
, struct dentry
*root
)
100 struct list_head
*next
= p
->d_subdirs
.next
;
102 if (next
== &p
->d_subdirs
) {
106 next
= p
->d_u
.d_child
.next
;
107 if (next
!= &p
->d_parent
->d_subdirs
)
112 return list_entry(next
, struct dentry
, d_u
.d_child
);
116 * Check a direct mount point for busyness.
117 * Direct mounts have similar expiry semantics to tree mounts.
118 * The tree is not busy iff no mountpoints are busy and there are no
121 static int autofs4_direct_busy(struct vfsmount
*mnt
,
123 unsigned long timeout
,
126 DPRINTK("top %p %.*s",
127 top
, (int) top
->d_name
.len
, top
->d_name
.name
);
129 /* If it's busy update the expiry counters */
130 if (!may_umount_tree(mnt
)) {
131 struct autofs_info
*ino
= autofs4_dentry_ino(top
);
133 ino
->last_used
= jiffies
;
137 /* Timeout of a direct mount is determined by its top dentry */
138 if (!autofs4_can_expire(top
, timeout
, do_now
))
144 /* Check a directory tree of mount points for busyness
145 * The tree is not busy iff no mountpoints are busy
147 static int autofs4_tree_busy(struct vfsmount
*mnt
,
149 unsigned long timeout
,
152 struct autofs_info
*top_ino
= autofs4_dentry_ino(top
);
155 DPRINTK("top %p %.*s",
156 top
, (int)top
->d_name
.len
, top
->d_name
.name
);
158 /* Negative dentry - give up */
159 if (!simple_positive(top
))
162 spin_lock(&dcache_lock
);
163 for (p
= top
; p
; p
= next_dentry(p
, top
)) {
164 /* Negative dentry - give up */
165 if (!simple_positive(p
))
168 DPRINTK("dentry %p %.*s",
169 p
, (int) p
->d_name
.len
, p
->d_name
.name
);
172 spin_unlock(&dcache_lock
);
175 * Is someone visiting anywhere in the subtree ?
176 * If there's no mount we need to check the usage
177 * count for the autofs dentry.
178 * If the fs is busy update the expiry counter.
180 if (d_mountpoint(p
)) {
181 if (autofs4_mount_busy(mnt
, p
)) {
182 top_ino
->last_used
= jiffies
;
187 struct autofs_info
*ino
= autofs4_dentry_ino(p
);
188 unsigned int ino_count
= atomic_read(&ino
->count
);
191 * Clean stale dentries below that have not been
192 * invalidated after a mount fail during lookup
196 /* allow for dget above and top is already dgot */
202 if (atomic_read(&p
->d_count
) > ino_count
) {
203 top_ino
->last_used
= jiffies
;
209 spin_lock(&dcache_lock
);
211 spin_unlock(&dcache_lock
);
213 /* Timeout of a tree mount is ultimately determined by its top dentry */
214 if (!autofs4_can_expire(top
, timeout
, do_now
))
220 static struct dentry
*autofs4_check_leaves(struct vfsmount
*mnt
,
221 struct dentry
*parent
,
222 unsigned long timeout
,
227 DPRINTK("parent %p %.*s",
228 parent
, (int)parent
->d_name
.len
, parent
->d_name
.name
);
230 spin_lock(&dcache_lock
);
231 for (p
= parent
; p
; p
= next_dentry(p
, parent
)) {
232 /* Negative dentry - give up */
233 if (!simple_positive(p
))
236 DPRINTK("dentry %p %.*s",
237 p
, (int) p
->d_name
.len
, p
->d_name
.name
);
240 spin_unlock(&dcache_lock
);
242 if (d_mountpoint(p
)) {
243 /* Can we umount this guy */
244 if (autofs4_mount_busy(mnt
, p
))
247 /* Can we expire this guy */
248 if (autofs4_can_expire(p
, timeout
, do_now
))
253 spin_lock(&dcache_lock
);
255 spin_unlock(&dcache_lock
);
259 /* Check if we can expire a direct mount (possibly a tree) */
260 struct dentry
*autofs4_expire_direct(struct super_block
*sb
,
261 struct vfsmount
*mnt
,
262 struct autofs_sb_info
*sbi
,
265 unsigned long timeout
;
266 struct dentry
*root
= dget(sb
->s_root
);
267 int do_now
= how
& AUTOFS_EXP_IMMEDIATE
;
273 timeout
= sbi
->exp_timeout
;
275 spin_lock(&sbi
->fs_lock
);
276 if (!autofs4_direct_busy(mnt
, root
, timeout
, do_now
)) {
277 struct autofs_info
*ino
= autofs4_dentry_ino(root
);
278 if (d_mountpoint(root
)) {
279 ino
->flags
|= AUTOFS_INF_MOUNTPOINT
;
282 ino
->flags
|= AUTOFS_INF_EXPIRING
;
283 init_completion(&ino
->expire_complete
);
284 spin_unlock(&sbi
->fs_lock
);
287 spin_unlock(&sbi
->fs_lock
);
294 * Find an eligible tree to time-out
295 * A tree is eligible if :-
296 * - it is unused by any user process
297 * - it has been unused for exp_timeout time
299 struct dentry
*autofs4_expire_indirect(struct super_block
*sb
,
300 struct vfsmount
*mnt
,
301 struct autofs_sb_info
*sbi
,
304 unsigned long timeout
;
305 struct dentry
*root
= sb
->s_root
;
306 struct dentry
*expired
= NULL
;
307 struct list_head
*next
;
308 int do_now
= how
& AUTOFS_EXP_IMMEDIATE
;
309 int exp_leaves
= how
& AUTOFS_EXP_LEAVES
;
310 struct autofs_info
*ino
;
311 unsigned int ino_count
;
317 timeout
= sbi
->exp_timeout
;
319 spin_lock(&dcache_lock
);
320 next
= root
->d_subdirs
.next
;
322 /* On exit from the loop expire is set to a dgot dentry
323 * to expire or it's NULL */
324 while ( next
!= &root
->d_subdirs
) {
325 struct dentry
*dentry
= list_entry(next
, struct dentry
, d_u
.d_child
);
327 /* Negative dentry - give up */
328 if (!simple_positive(dentry
)) {
333 dentry
= dget(dentry
);
334 spin_unlock(&dcache_lock
);
336 spin_lock(&sbi
->fs_lock
);
337 ino
= autofs4_dentry_ino(dentry
);
340 * Case 1: (i) indirect mount or top level pseudo direct mount
342 * (ii) indirect mount with offset mount, check the "/"
343 * offset (autofs-5.0+).
345 if (d_mountpoint(dentry
)) {
346 DPRINTK("checking mountpoint %p %.*s",
347 dentry
, (int)dentry
->d_name
.len
, dentry
->d_name
.name
);
349 /* Path walk currently on this dentry? */
350 ino_count
= atomic_read(&ino
->count
) + 2;
351 if (atomic_read(&dentry
->d_count
) > ino_count
)
354 /* Can we umount this guy */
355 if (autofs4_mount_busy(mnt
, dentry
))
358 /* Can we expire this guy */
359 if (autofs4_can_expire(dentry
, timeout
, do_now
)) {
366 if (simple_empty(dentry
))
369 /* Case 2: tree mount, expire iff entire tree is not busy */
371 /* Path walk currently on this dentry? */
372 ino_count
= atomic_read(&ino
->count
) + 1;
373 if (atomic_read(&dentry
->d_count
) > ino_count
)
376 if (!autofs4_tree_busy(mnt
, dentry
, timeout
, do_now
)) {
381 * Case 3: pseudo direct mount, expire individual leaves
385 /* Path walk currently on this dentry? */
386 ino_count
= atomic_read(&ino
->count
) + 1;
387 if (atomic_read(&dentry
->d_count
) > ino_count
)
390 expired
= autofs4_check_leaves(mnt
, dentry
, timeout
, do_now
);
397 spin_unlock(&sbi
->fs_lock
);
399 spin_lock(&dcache_lock
);
402 spin_unlock(&dcache_lock
);
406 DPRINTK("returning %p %.*s",
407 expired
, (int)expired
->d_name
.len
, expired
->d_name
.name
);
408 ino
= autofs4_dentry_ino(expired
);
409 ino
->flags
|= AUTOFS_INF_EXPIRING
;
410 init_completion(&ino
->expire_complete
);
411 spin_unlock(&sbi
->fs_lock
);
412 spin_lock(&dcache_lock
);
413 list_move(&expired
->d_parent
->d_subdirs
, &expired
->d_u
.d_child
);
414 spin_unlock(&dcache_lock
);
418 int autofs4_expire_wait(struct dentry
*dentry
)
420 struct autofs_sb_info
*sbi
= autofs4_sbi(dentry
->d_sb
);
421 struct autofs_info
*ino
= autofs4_dentry_ino(dentry
);
424 /* Block on any pending expire */
425 spin_lock(&sbi
->fs_lock
);
426 if (ino
->flags
& AUTOFS_INF_EXPIRING
) {
427 spin_unlock(&sbi
->fs_lock
);
429 DPRINTK("waiting for expire %p name=%.*s",
430 dentry
, dentry
->d_name
.len
, dentry
->d_name
.name
);
432 status
= autofs4_wait(sbi
, dentry
, NFY_NONE
);
433 wait_for_completion(&ino
->expire_complete
);
435 DPRINTK("expire done status=%d", status
);
437 if (d_unhashed(dentry
))
442 spin_unlock(&sbi
->fs_lock
);
447 /* Perform an expiry operation */
448 int autofs4_expire_run(struct super_block
*sb
,
449 struct vfsmount
*mnt
,
450 struct autofs_sb_info
*sbi
,
451 struct autofs_packet_expire __user
*pkt_p
)
453 struct autofs_packet_expire pkt
;
454 struct autofs_info
*ino
;
455 struct dentry
*dentry
;
458 memset(&pkt
,0,sizeof pkt
);
460 pkt
.hdr
.proto_version
= sbi
->version
;
461 pkt
.hdr
.type
= autofs_ptype_expire
;
463 if ((dentry
= autofs4_expire_indirect(sb
, mnt
, sbi
, 0)) == NULL
)
466 pkt
.len
= dentry
->d_name
.len
;
467 memcpy(pkt
.name
, dentry
->d_name
.name
, pkt
.len
);
468 pkt
.name
[pkt
.len
] = '\0';
471 if ( copy_to_user(pkt_p
, &pkt
, sizeof(struct autofs_packet_expire
)) )
474 spin_lock(&sbi
->fs_lock
);
475 ino
= autofs4_dentry_ino(dentry
);
476 ino
->flags
&= ~AUTOFS_INF_EXPIRING
;
477 complete_all(&ino
->expire_complete
);
478 spin_unlock(&sbi
->fs_lock
);
483 int autofs4_do_expire_multi(struct super_block
*sb
, struct vfsmount
*mnt
,
484 struct autofs_sb_info
*sbi
, int when
)
486 struct dentry
*dentry
;
489 if (autofs_type_trigger(sbi
->type
))
490 dentry
= autofs4_expire_direct(sb
, mnt
, sbi
, when
);
492 dentry
= autofs4_expire_indirect(sb
, mnt
, sbi
, when
);
495 struct autofs_info
*ino
= autofs4_dentry_ino(dentry
);
497 /* This is synchronous because it makes the daemon a
499 ret
= autofs4_wait(sbi
, dentry
, NFY_EXPIRE
);
501 spin_lock(&sbi
->fs_lock
);
502 if (ino
->flags
& AUTOFS_INF_MOUNTPOINT
) {
503 sb
->s_root
->d_mounted
++;
504 ino
->flags
&= ~AUTOFS_INF_MOUNTPOINT
;
506 ino
->flags
&= ~AUTOFS_INF_EXPIRING
;
507 complete_all(&ino
->expire_complete
);
508 spin_unlock(&sbi
->fs_lock
);
515 /* Call repeatedly until it returns -EAGAIN, meaning there's nothing
517 int autofs4_expire_multi(struct super_block
*sb
, struct vfsmount
*mnt
,
518 struct autofs_sb_info
*sbi
, int __user
*arg
)
522 if (arg
&& get_user(do_now
, arg
))
525 return autofs4_do_expire_multi(sb
, mnt
, sbi
, do_now
);