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
))
37 /* Check a mount point for busyness */
38 static int autofs4_mount_busy(struct vfsmount
*mnt
, struct dentry
*dentry
)
40 struct dentry
*top
= dentry
;
41 struct path path
= {.mnt
= mnt
, .dentry
= dentry
};
44 DPRINTK("dentry %p %.*s",
45 dentry
, (int)dentry
->d_name
.len
, dentry
->d_name
.name
);
49 if (!follow_down_one(&path
))
52 if (is_autofs4_dentry(path
.dentry
)) {
53 struct autofs_sb_info
*sbi
= autofs4_sbi(path
.dentry
->d_sb
);
55 /* This is an autofs submount, we can't expire it */
56 if (autofs_type_indirect(sbi
->type
))
60 /* Update the expiry counter if fs is busy */
61 if (!may_umount_tree(path
.mnt
)) {
62 struct autofs_info
*ino
= autofs4_dentry_ino(top
);
63 ino
->last_used
= jiffies
;
69 DPRINTK("returning = %d", status
);
75 * Calculate and dget next entry in the subdirs list under root.
77 static struct dentry
*get_next_positive_subdir(struct dentry
*prev
,
80 struct autofs_sb_info
*sbi
= autofs4_sbi(root
->d_sb
);
81 struct list_head
*next
;
84 spin_lock(&sbi
->lookup_lock
);
85 spin_lock(&root
->d_lock
);
88 next
= prev
->d_u
.d_child
.next
;
90 prev
= dget_dlock(root
);
91 next
= prev
->d_subdirs
.next
;
95 if (next
== &root
->d_subdirs
) {
96 spin_unlock(&root
->d_lock
);
97 spin_unlock(&sbi
->lookup_lock
);
102 q
= list_entry(next
, struct dentry
, d_u
.d_child
);
104 spin_lock_nested(&q
->d_lock
, DENTRY_D_LOCK_NESTED
);
105 /* Already gone or negative dentry (under construction) - try next */
106 if (!d_count(q
) || !simple_positive(q
)) {
107 spin_unlock(&q
->d_lock
);
108 next
= q
->d_u
.d_child
.next
;
112 spin_unlock(&q
->d_lock
);
113 spin_unlock(&root
->d_lock
);
114 spin_unlock(&sbi
->lookup_lock
);
122 * Calculate and dget next entry in top down tree traversal.
124 static struct dentry
*get_next_positive_dentry(struct dentry
*prev
,
127 struct autofs_sb_info
*sbi
= autofs4_sbi(root
->d_sb
);
128 struct list_head
*next
;
129 struct dentry
*p
, *ret
;
134 spin_lock(&sbi
->lookup_lock
);
137 spin_lock(&p
->d_lock
);
139 next
= p
->d_subdirs
.next
;
140 if (next
== &p
->d_subdirs
) {
142 struct dentry
*parent
;
145 spin_unlock(&p
->d_lock
);
146 spin_unlock(&sbi
->lookup_lock
);
151 parent
= p
->d_parent
;
152 if (!spin_trylock(&parent
->d_lock
)) {
153 spin_unlock(&p
->d_lock
);
157 spin_unlock(&p
->d_lock
);
158 next
= p
->d_u
.d_child
.next
;
160 if (next
!= &parent
->d_subdirs
)
164 ret
= list_entry(next
, struct dentry
, d_u
.d_child
);
166 spin_lock_nested(&ret
->d_lock
, DENTRY_D_LOCK_NESTED
);
167 /* Negative dentry - try next */
168 if (!simple_positive(ret
)) {
169 spin_unlock(&p
->d_lock
);
170 lock_set_subclass(&ret
->d_lock
.dep_map
, 0, _RET_IP_
);
175 spin_unlock(&ret
->d_lock
);
176 spin_unlock(&p
->d_lock
);
177 spin_unlock(&sbi
->lookup_lock
);
185 * Check a direct mount point for busyness.
186 * Direct mounts have similar expiry semantics to tree mounts.
187 * The tree is not busy iff no mountpoints are busy and there are no
190 static int autofs4_direct_busy(struct vfsmount
*mnt
,
192 unsigned long timeout
,
195 DPRINTK("top %p %.*s",
196 top
, (int) top
->d_name
.len
, top
->d_name
.name
);
198 /* If it's busy update the expiry counters */
199 if (!may_umount_tree(mnt
)) {
200 struct autofs_info
*ino
= autofs4_dentry_ino(top
);
202 ino
->last_used
= jiffies
;
206 /* Timeout of a direct mount is determined by its top dentry */
207 if (!autofs4_can_expire(top
, timeout
, do_now
))
213 /* Check a directory tree of mount points for busyness
214 * The tree is not busy iff no mountpoints are busy
216 static int autofs4_tree_busy(struct vfsmount
*mnt
,
218 unsigned long timeout
,
221 struct autofs_info
*top_ino
= autofs4_dentry_ino(top
);
224 DPRINTK("top %p %.*s",
225 top
, (int)top
->d_name
.len
, top
->d_name
.name
);
227 /* Negative dentry - give up */
228 if (!simple_positive(top
))
232 while ((p
= get_next_positive_dentry(p
, top
))) {
233 DPRINTK("dentry %p %.*s",
234 p
, (int) p
->d_name
.len
, p
->d_name
.name
);
237 * Is someone visiting anywhere in the subtree ?
238 * If there's no mount we need to check the usage
239 * count for the autofs dentry.
240 * If the fs is busy update the expiry counter.
242 if (d_mountpoint(p
)) {
243 if (autofs4_mount_busy(mnt
, p
)) {
244 top_ino
->last_used
= jiffies
;
249 struct autofs_info
*ino
= autofs4_dentry_ino(p
);
250 unsigned int ino_count
= atomic_read(&ino
->count
);
252 /* allow for dget above and top is already dgot */
258 if (d_count(p
) > ino_count
) {
259 top_ino
->last_used
= jiffies
;
266 /* Timeout of a tree mount is ultimately determined by its top dentry */
267 if (!autofs4_can_expire(top
, timeout
, do_now
))
273 static struct dentry
*autofs4_check_leaves(struct vfsmount
*mnt
,
274 struct dentry
*parent
,
275 unsigned long timeout
,
280 DPRINTK("parent %p %.*s",
281 parent
, (int)parent
->d_name
.len
, parent
->d_name
.name
);
284 while ((p
= get_next_positive_dentry(p
, parent
))) {
285 DPRINTK("dentry %p %.*s",
286 p
, (int) p
->d_name
.len
, p
->d_name
.name
);
288 if (d_mountpoint(p
)) {
289 /* Can we umount this guy */
290 if (autofs4_mount_busy(mnt
, p
))
293 /* Can we expire this guy */
294 if (autofs4_can_expire(p
, timeout
, do_now
))
301 /* Check if we can expire a direct mount (possibly a tree) */
302 struct dentry
*autofs4_expire_direct(struct super_block
*sb
,
303 struct vfsmount
*mnt
,
304 struct autofs_sb_info
*sbi
,
307 unsigned long timeout
;
308 struct dentry
*root
= dget(sb
->s_root
);
309 int do_now
= how
& AUTOFS_EXP_IMMEDIATE
;
310 struct autofs_info
*ino
;
316 timeout
= sbi
->exp_timeout
;
318 spin_lock(&sbi
->fs_lock
);
319 ino
= autofs4_dentry_ino(root
);
320 /* No point expiring a pending mount */
321 if (ino
->flags
& AUTOFS_INF_PENDING
)
323 if (!autofs4_direct_busy(mnt
, root
, timeout
, do_now
)) {
324 ino
->flags
|= AUTOFS_INF_NO_RCU
;
325 spin_unlock(&sbi
->fs_lock
);
327 spin_lock(&sbi
->fs_lock
);
328 if (!autofs4_direct_busy(mnt
, root
, timeout
, do_now
)) {
329 ino
->flags
|= AUTOFS_INF_EXPIRING
;
331 ino
->flags
&= ~AUTOFS_INF_NO_RCU
;
332 init_completion(&ino
->expire_complete
);
333 spin_unlock(&sbi
->fs_lock
);
336 ino
->flags
&= ~AUTOFS_INF_NO_RCU
;
339 spin_unlock(&sbi
->fs_lock
);
345 /* Check if 'dentry' should expire, or return a nearby
346 * dentry that is suitable.
347 * If returned dentry is different from arg dentry,
348 * then a dget() reference was taken, else not.
350 static struct dentry
*should_expire(struct dentry
*dentry
,
351 struct vfsmount
*mnt
,
352 unsigned long timeout
,
355 int do_now
= how
& AUTOFS_EXP_IMMEDIATE
;
356 int exp_leaves
= how
& AUTOFS_EXP_LEAVES
;
357 struct autofs_info
*ino
= autofs4_dentry_ino(dentry
);
358 unsigned int ino_count
;
360 /* No point expiring a pending mount */
361 if (ino
->flags
& AUTOFS_INF_PENDING
)
365 * Case 1: (i) indirect mount or top level pseudo direct mount
367 * (ii) indirect mount with offset mount, check the "/"
368 * offset (autofs-5.0+).
370 if (d_mountpoint(dentry
)) {
371 DPRINTK("checking mountpoint %p %.*s",
372 dentry
, (int)dentry
->d_name
.len
, dentry
->d_name
.name
);
374 /* Can we umount this guy */
375 if (autofs4_mount_busy(mnt
, dentry
))
378 /* Can we expire this guy */
379 if (autofs4_can_expire(dentry
, timeout
, do_now
))
384 if (dentry
->d_inode
&& S_ISLNK(dentry
->d_inode
->i_mode
)) {
385 DPRINTK("checking symlink %p %.*s",
386 dentry
, (int)dentry
->d_name
.len
, dentry
->d_name
.name
);
388 * A symlink can't be "busy" in the usual sense so
389 * just check last used for expire timeout.
391 if (autofs4_can_expire(dentry
, timeout
, do_now
))
396 if (simple_empty(dentry
))
399 /* Case 2: tree mount, expire iff entire tree is not busy */
401 /* Path walk currently on this dentry? */
402 ino_count
= atomic_read(&ino
->count
) + 1;
403 if (d_count(dentry
) > ino_count
)
406 if (!autofs4_tree_busy(mnt
, dentry
, timeout
, do_now
))
409 * Case 3: pseudo direct mount, expire individual leaves
413 /* Path walk currently on this dentry? */
414 struct dentry
*expired
;
415 ino_count
= atomic_read(&ino
->count
) + 1;
416 if (d_count(dentry
) > ino_count
)
419 expired
= autofs4_check_leaves(mnt
, dentry
, timeout
, do_now
);
421 if (expired
== dentry
)
429 * Find an eligible tree to time-out
430 * A tree is eligible if :-
431 * - it is unused by any user process
432 * - it has been unused for exp_timeout time
434 struct dentry
*autofs4_expire_indirect(struct super_block
*sb
,
435 struct vfsmount
*mnt
,
436 struct autofs_sb_info
*sbi
,
439 unsigned long timeout
;
440 struct dentry
*root
= sb
->s_root
;
441 struct dentry
*dentry
;
442 struct dentry
*expired
;
443 struct autofs_info
*ino
;
449 timeout
= sbi
->exp_timeout
;
452 while ((dentry
= get_next_positive_subdir(dentry
, root
))) {
453 spin_lock(&sbi
->fs_lock
);
454 ino
= autofs4_dentry_ino(dentry
);
455 if (ino
->flags
& AUTOFS_INF_NO_RCU
)
458 expired
= should_expire(dentry
, mnt
, timeout
, how
);
460 spin_unlock(&sbi
->fs_lock
);
463 ino
= autofs4_dentry_ino(expired
);
464 ino
->flags
|= AUTOFS_INF_NO_RCU
;
465 spin_unlock(&sbi
->fs_lock
);
467 spin_lock(&sbi
->fs_lock
);
468 if (should_expire(expired
, mnt
, timeout
, how
)) {
469 if (expired
!= dentry
)
474 ino
->flags
&= ~AUTOFS_INF_NO_RCU
;
475 if (expired
!= dentry
)
477 spin_unlock(&sbi
->fs_lock
);
482 DPRINTK("returning %p %.*s",
483 expired
, (int)expired
->d_name
.len
, expired
->d_name
.name
);
484 ino
->flags
|= AUTOFS_INF_EXPIRING
;
486 ino
->flags
&= ~AUTOFS_INF_NO_RCU
;
487 init_completion(&ino
->expire_complete
);
488 spin_unlock(&sbi
->fs_lock
);
489 spin_lock(&sbi
->lookup_lock
);
490 spin_lock(&expired
->d_parent
->d_lock
);
491 spin_lock_nested(&expired
->d_lock
, DENTRY_D_LOCK_NESTED
);
492 list_move(&expired
->d_parent
->d_subdirs
, &expired
->d_u
.d_child
);
493 spin_unlock(&expired
->d_lock
);
494 spin_unlock(&expired
->d_parent
->d_lock
);
495 spin_unlock(&sbi
->lookup_lock
);
499 int autofs4_expire_wait(struct dentry
*dentry
, int rcu_walk
)
501 struct autofs_sb_info
*sbi
= autofs4_sbi(dentry
->d_sb
);
502 struct autofs_info
*ino
= autofs4_dentry_ino(dentry
);
505 /* Block on any pending expire */
506 if (!(ino
->flags
& (AUTOFS_INF_EXPIRING
| AUTOFS_INF_NO_RCU
)))
511 spin_lock(&sbi
->fs_lock
);
512 if (ino
->flags
& AUTOFS_INF_EXPIRING
) {
513 spin_unlock(&sbi
->fs_lock
);
515 DPRINTK("waiting for expire %p name=%.*s",
516 dentry
, dentry
->d_name
.len
, dentry
->d_name
.name
);
518 status
= autofs4_wait(sbi
, dentry
, NFY_NONE
);
519 wait_for_completion(&ino
->expire_complete
);
521 DPRINTK("expire done status=%d", status
);
523 if (d_unhashed(dentry
))
528 spin_unlock(&sbi
->fs_lock
);
533 /* Perform an expiry operation */
534 int autofs4_expire_run(struct super_block
*sb
,
535 struct vfsmount
*mnt
,
536 struct autofs_sb_info
*sbi
,
537 struct autofs_packet_expire __user
*pkt_p
)
539 struct autofs_packet_expire pkt
;
540 struct autofs_info
*ino
;
541 struct dentry
*dentry
;
544 memset(&pkt
,0,sizeof pkt
);
546 pkt
.hdr
.proto_version
= sbi
->version
;
547 pkt
.hdr
.type
= autofs_ptype_expire
;
549 if ((dentry
= autofs4_expire_indirect(sb
, mnt
, sbi
, 0)) == NULL
)
552 pkt
.len
= dentry
->d_name
.len
;
553 memcpy(pkt
.name
, dentry
->d_name
.name
, pkt
.len
);
554 pkt
.name
[pkt
.len
] = '\0';
557 if ( copy_to_user(pkt_p
, &pkt
, sizeof(struct autofs_packet_expire
)) )
560 spin_lock(&sbi
->fs_lock
);
561 ino
= autofs4_dentry_ino(dentry
);
562 /* avoid rapid-fire expire attempts if expiry fails */
563 ino
->last_used
= now
;
564 ino
->flags
&= ~AUTOFS_INF_EXPIRING
;
565 complete_all(&ino
->expire_complete
);
566 spin_unlock(&sbi
->fs_lock
);
571 int autofs4_do_expire_multi(struct super_block
*sb
, struct vfsmount
*mnt
,
572 struct autofs_sb_info
*sbi
, int when
)
574 struct dentry
*dentry
;
577 if (autofs_type_trigger(sbi
->type
))
578 dentry
= autofs4_expire_direct(sb
, mnt
, sbi
, when
);
580 dentry
= autofs4_expire_indirect(sb
, mnt
, sbi
, when
);
583 struct autofs_info
*ino
= autofs4_dentry_ino(dentry
);
585 /* This is synchronous because it makes the daemon a
587 ret
= autofs4_wait(sbi
, dentry
, NFY_EXPIRE
);
589 spin_lock(&sbi
->fs_lock
);
590 /* avoid rapid-fire expire attempts if expiry fails */
591 ino
->last_used
= now
;
592 ino
->flags
&= ~AUTOFS_INF_EXPIRING
;
593 complete_all(&ino
->expire_complete
);
594 spin_unlock(&sbi
->fs_lock
);
601 /* Call repeatedly until it returns -EAGAIN, meaning there's nothing
603 int autofs4_expire_multi(struct super_block
*sb
, struct vfsmount
*mnt
,
604 struct autofs_sb_info
*sbi
, int __user
*arg
)
608 if (arg
&& get_user(do_now
, arg
))
611 return autofs4_do_expire_multi(sb
, mnt
, sbi
, do_now
);