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 %pd", dentry
, dentry
);
48 if (!follow_down_one(&path
))
51 if (is_autofs4_dentry(path
.dentry
)) {
52 struct autofs_sb_info
*sbi
= autofs4_sbi(path
.dentry
->d_sb
);
54 /* This is an autofs submount, we can't expire it */
55 if (autofs_type_indirect(sbi
->type
))
59 /* Update the expiry counter if fs is busy */
60 if (!may_umount_tree(path
.mnt
)) {
61 struct autofs_info
*ino
= autofs4_dentry_ino(top
);
62 ino
->last_used
= jiffies
;
68 DPRINTK("returning = %d", status
);
74 * Calculate and dget next entry in the subdirs list under root.
76 static struct dentry
*get_next_positive_subdir(struct dentry
*prev
,
79 struct autofs_sb_info
*sbi
= autofs4_sbi(root
->d_sb
);
80 struct list_head
*next
;
83 spin_lock(&sbi
->lookup_lock
);
84 spin_lock(&root
->d_lock
);
87 next
= prev
->d_child
.next
;
89 prev
= dget_dlock(root
);
90 next
= prev
->d_subdirs
.next
;
94 if (next
== &root
->d_subdirs
) {
95 spin_unlock(&root
->d_lock
);
96 spin_unlock(&sbi
->lookup_lock
);
101 q
= list_entry(next
, struct dentry
, d_child
);
103 spin_lock_nested(&q
->d_lock
, DENTRY_D_LOCK_NESTED
);
104 /* Already gone or negative dentry (under construction) - try next */
105 if (!d_count(q
) || !simple_positive(q
)) {
106 spin_unlock(&q
->d_lock
);
107 next
= q
->d_child
.next
;
111 spin_unlock(&q
->d_lock
);
112 spin_unlock(&root
->d_lock
);
113 spin_unlock(&sbi
->lookup_lock
);
121 * Calculate and dget next entry in top down tree traversal.
123 static struct dentry
*get_next_positive_dentry(struct dentry
*prev
,
126 struct autofs_sb_info
*sbi
= autofs4_sbi(root
->d_sb
);
127 struct list_head
*next
;
128 struct dentry
*p
, *ret
;
133 spin_lock(&sbi
->lookup_lock
);
136 spin_lock(&p
->d_lock
);
138 next
= p
->d_subdirs
.next
;
139 if (next
== &p
->d_subdirs
) {
141 struct dentry
*parent
;
144 spin_unlock(&p
->d_lock
);
145 spin_unlock(&sbi
->lookup_lock
);
150 parent
= p
->d_parent
;
151 if (!spin_trylock(&parent
->d_lock
)) {
152 spin_unlock(&p
->d_lock
);
156 spin_unlock(&p
->d_lock
);
157 next
= p
->d_child
.next
;
159 if (next
!= &parent
->d_subdirs
)
163 ret
= list_entry(next
, struct dentry
, d_child
);
165 spin_lock_nested(&ret
->d_lock
, DENTRY_D_LOCK_NESTED
);
166 /* Negative dentry - try next */
167 if (!simple_positive(ret
)) {
168 spin_unlock(&p
->d_lock
);
169 lock_set_subclass(&ret
->d_lock
.dep_map
, 0, _RET_IP_
);
174 spin_unlock(&ret
->d_lock
);
175 spin_unlock(&p
->d_lock
);
176 spin_unlock(&sbi
->lookup_lock
);
184 * Check a direct mount point for busyness.
185 * Direct mounts have similar expiry semantics to tree mounts.
186 * The tree is not busy iff no mountpoints are busy and there are no
189 static int autofs4_direct_busy(struct vfsmount
*mnt
,
191 unsigned long timeout
,
194 DPRINTK("top %p %pd", top
, top
);
196 /* If it's busy update the expiry counters */
197 if (!may_umount_tree(mnt
)) {
198 struct autofs_info
*ino
= autofs4_dentry_ino(top
);
200 ino
->last_used
= jiffies
;
204 /* Timeout of a direct mount is determined by its top dentry */
205 if (!autofs4_can_expire(top
, timeout
, do_now
))
211 /* Check a directory tree of mount points for busyness
212 * The tree is not busy iff no mountpoints are busy
214 static int autofs4_tree_busy(struct vfsmount
*mnt
,
216 unsigned long timeout
,
219 struct autofs_info
*top_ino
= autofs4_dentry_ino(top
);
222 DPRINTK("top %p %pd", top
, top
);
224 /* Negative dentry - give up */
225 if (!simple_positive(top
))
229 while ((p
= get_next_positive_dentry(p
, top
))) {
230 DPRINTK("dentry %p %pd", p
, p
);
233 * Is someone visiting anywhere in the subtree ?
234 * If there's no mount we need to check the usage
235 * count for the autofs dentry.
236 * If the fs is busy update the expiry counter.
238 if (d_mountpoint(p
)) {
239 if (autofs4_mount_busy(mnt
, p
)) {
240 top_ino
->last_used
= jiffies
;
245 struct autofs_info
*ino
= autofs4_dentry_ino(p
);
246 unsigned int ino_count
= atomic_read(&ino
->count
);
248 /* allow for dget above and top is already dgot */
254 if (d_count(p
) > ino_count
) {
255 top_ino
->last_used
= jiffies
;
262 /* Timeout of a tree mount is ultimately determined by its top dentry */
263 if (!autofs4_can_expire(top
, timeout
, do_now
))
269 static struct dentry
*autofs4_check_leaves(struct vfsmount
*mnt
,
270 struct dentry
*parent
,
271 unsigned long timeout
,
276 DPRINTK("parent %p %pd", parent
, parent
);
279 while ((p
= get_next_positive_dentry(p
, parent
))) {
280 DPRINTK("dentry %p %pd", p
, p
);
282 if (d_mountpoint(p
)) {
283 /* Can we umount this guy */
284 if (autofs4_mount_busy(mnt
, p
))
287 /* Can we expire this guy */
288 if (autofs4_can_expire(p
, timeout
, do_now
))
295 /* Check if we can expire a direct mount (possibly a tree) */
296 struct dentry
*autofs4_expire_direct(struct super_block
*sb
,
297 struct vfsmount
*mnt
,
298 struct autofs_sb_info
*sbi
,
301 unsigned long timeout
;
302 struct dentry
*root
= dget(sb
->s_root
);
303 int do_now
= how
& AUTOFS_EXP_IMMEDIATE
;
304 struct autofs_info
*ino
;
310 timeout
= sbi
->exp_timeout
;
312 spin_lock(&sbi
->fs_lock
);
313 ino
= autofs4_dentry_ino(root
);
314 /* No point expiring a pending mount */
315 if (ino
->flags
& AUTOFS_INF_PENDING
)
317 if (!autofs4_direct_busy(mnt
, root
, timeout
, do_now
)) {
318 ino
->flags
|= AUTOFS_INF_NO_RCU
;
319 spin_unlock(&sbi
->fs_lock
);
321 spin_lock(&sbi
->fs_lock
);
322 if (!autofs4_direct_busy(mnt
, root
, timeout
, do_now
)) {
323 ino
->flags
|= AUTOFS_INF_EXPIRING
;
325 ino
->flags
&= ~AUTOFS_INF_NO_RCU
;
326 init_completion(&ino
->expire_complete
);
327 spin_unlock(&sbi
->fs_lock
);
330 ino
->flags
&= ~AUTOFS_INF_NO_RCU
;
333 spin_unlock(&sbi
->fs_lock
);
339 /* Check if 'dentry' should expire, or return a nearby
340 * dentry that is suitable.
341 * If returned dentry is different from arg dentry,
342 * then a dget() reference was taken, else not.
344 static struct dentry
*should_expire(struct dentry
*dentry
,
345 struct vfsmount
*mnt
,
346 unsigned long timeout
,
349 int do_now
= how
& AUTOFS_EXP_IMMEDIATE
;
350 int exp_leaves
= how
& AUTOFS_EXP_LEAVES
;
351 struct autofs_info
*ino
= autofs4_dentry_ino(dentry
);
352 unsigned int ino_count
;
354 /* No point expiring a pending mount */
355 if (ino
->flags
& AUTOFS_INF_PENDING
)
359 * Case 1: (i) indirect mount or top level pseudo direct mount
361 * (ii) indirect mount with offset mount, check the "/"
362 * offset (autofs-5.0+).
364 if (d_mountpoint(dentry
)) {
365 DPRINTK("checking mountpoint %p %pd", dentry
, dentry
);
367 /* Can we umount this guy */
368 if (autofs4_mount_busy(mnt
, dentry
))
371 /* Can we expire this guy */
372 if (autofs4_can_expire(dentry
, timeout
, do_now
))
377 if (d_really_is_positive(dentry
) && d_is_symlink(dentry
)) {
378 DPRINTK("checking symlink %p %pd", dentry
, dentry
);
380 * A symlink can't be "busy" in the usual sense so
381 * just check last used for expire timeout.
383 if (autofs4_can_expire(dentry
, timeout
, do_now
))
388 if (simple_empty(dentry
))
391 /* Case 2: tree mount, expire iff entire tree is not busy */
393 /* Path walk currently on this dentry? */
394 ino_count
= atomic_read(&ino
->count
) + 1;
395 if (d_count(dentry
) > ino_count
)
398 if (!autofs4_tree_busy(mnt
, dentry
, timeout
, do_now
))
401 * Case 3: pseudo direct mount, expire individual leaves
405 /* Path walk currently on this dentry? */
406 struct dentry
*expired
;
407 ino_count
= atomic_read(&ino
->count
) + 1;
408 if (d_count(dentry
) > ino_count
)
411 expired
= autofs4_check_leaves(mnt
, dentry
, timeout
, do_now
);
413 if (expired
== dentry
)
421 * Find an eligible tree to time-out
422 * A tree is eligible if :-
423 * - it is unused by any user process
424 * - it has been unused for exp_timeout time
426 struct dentry
*autofs4_expire_indirect(struct super_block
*sb
,
427 struct vfsmount
*mnt
,
428 struct autofs_sb_info
*sbi
,
431 unsigned long timeout
;
432 struct dentry
*root
= sb
->s_root
;
433 struct dentry
*dentry
;
434 struct dentry
*expired
;
435 struct autofs_info
*ino
;
441 timeout
= sbi
->exp_timeout
;
444 while ((dentry
= get_next_positive_subdir(dentry
, root
))) {
445 spin_lock(&sbi
->fs_lock
);
446 ino
= autofs4_dentry_ino(dentry
);
447 if (ino
->flags
& AUTOFS_INF_NO_RCU
)
450 expired
= should_expire(dentry
, mnt
, timeout
, how
);
452 spin_unlock(&sbi
->fs_lock
);
455 ino
= autofs4_dentry_ino(expired
);
456 ino
->flags
|= AUTOFS_INF_NO_RCU
;
457 spin_unlock(&sbi
->fs_lock
);
459 spin_lock(&sbi
->fs_lock
);
460 if (should_expire(expired
, mnt
, timeout
, how
)) {
461 if (expired
!= dentry
)
466 ino
->flags
&= ~AUTOFS_INF_NO_RCU
;
467 if (expired
!= dentry
)
469 spin_unlock(&sbi
->fs_lock
);
474 DPRINTK("returning %p %pd", expired
, expired
);
475 ino
->flags
|= AUTOFS_INF_EXPIRING
;
477 ino
->flags
&= ~AUTOFS_INF_NO_RCU
;
478 init_completion(&ino
->expire_complete
);
479 spin_unlock(&sbi
->fs_lock
);
480 spin_lock(&sbi
->lookup_lock
);
481 spin_lock(&expired
->d_parent
->d_lock
);
482 spin_lock_nested(&expired
->d_lock
, DENTRY_D_LOCK_NESTED
);
483 list_move(&expired
->d_parent
->d_subdirs
, &expired
->d_child
);
484 spin_unlock(&expired
->d_lock
);
485 spin_unlock(&expired
->d_parent
->d_lock
);
486 spin_unlock(&sbi
->lookup_lock
);
490 int autofs4_expire_wait(struct dentry
*dentry
, int rcu_walk
)
492 struct autofs_sb_info
*sbi
= autofs4_sbi(dentry
->d_sb
);
493 struct autofs_info
*ino
= autofs4_dentry_ino(dentry
);
496 /* Block on any pending expire */
497 if (!(ino
->flags
& (AUTOFS_INF_EXPIRING
| AUTOFS_INF_NO_RCU
)))
502 spin_lock(&sbi
->fs_lock
);
503 if (ino
->flags
& AUTOFS_INF_EXPIRING
) {
504 spin_unlock(&sbi
->fs_lock
);
506 DPRINTK("waiting for expire %p name=%pd", dentry
, dentry
);
508 status
= autofs4_wait(sbi
, dentry
, NFY_NONE
);
509 wait_for_completion(&ino
->expire_complete
);
511 DPRINTK("expire done status=%d", status
);
513 if (d_unhashed(dentry
))
518 spin_unlock(&sbi
->fs_lock
);
523 /* Perform an expiry operation */
524 int autofs4_expire_run(struct super_block
*sb
,
525 struct vfsmount
*mnt
,
526 struct autofs_sb_info
*sbi
,
527 struct autofs_packet_expire __user
*pkt_p
)
529 struct autofs_packet_expire pkt
;
530 struct autofs_info
*ino
;
531 struct dentry
*dentry
;
534 memset(&pkt
,0,sizeof pkt
);
536 pkt
.hdr
.proto_version
= sbi
->version
;
537 pkt
.hdr
.type
= autofs_ptype_expire
;
539 if ((dentry
= autofs4_expire_indirect(sb
, mnt
, sbi
, 0)) == NULL
)
542 pkt
.len
= dentry
->d_name
.len
;
543 memcpy(pkt
.name
, dentry
->d_name
.name
, pkt
.len
);
544 pkt
.name
[pkt
.len
] = '\0';
547 if ( copy_to_user(pkt_p
, &pkt
, sizeof(struct autofs_packet_expire
)) )
550 spin_lock(&sbi
->fs_lock
);
551 ino
= autofs4_dentry_ino(dentry
);
552 /* avoid rapid-fire expire attempts if expiry fails */
553 ino
->last_used
= now
;
554 ino
->flags
&= ~AUTOFS_INF_EXPIRING
;
555 complete_all(&ino
->expire_complete
);
556 spin_unlock(&sbi
->fs_lock
);
561 int autofs4_do_expire_multi(struct super_block
*sb
, struct vfsmount
*mnt
,
562 struct autofs_sb_info
*sbi
, int when
)
564 struct dentry
*dentry
;
567 if (autofs_type_trigger(sbi
->type
))
568 dentry
= autofs4_expire_direct(sb
, mnt
, sbi
, when
);
570 dentry
= autofs4_expire_indirect(sb
, mnt
, sbi
, when
);
573 struct autofs_info
*ino
= autofs4_dentry_ino(dentry
);
575 /* This is synchronous because it makes the daemon a
577 ret
= autofs4_wait(sbi
, dentry
, NFY_EXPIRE
);
579 spin_lock(&sbi
->fs_lock
);
580 /* avoid rapid-fire expire attempts if expiry fails */
581 ino
->last_used
= now
;
582 ino
->flags
&= ~AUTOFS_INF_EXPIRING
;
583 complete_all(&ino
->expire_complete
);
584 spin_unlock(&sbi
->fs_lock
);
591 /* Call repeatedly until it returns -EAGAIN, meaning there's nothing
593 int autofs4_expire_multi(struct super_block
*sb
, struct vfsmount
*mnt
,
594 struct autofs_sb_info
*sbi
, int __user
*arg
)
598 if (arg
&& get_user(do_now
, arg
))
601 return autofs4_do_expire_multi(sb
, mnt
, sbi
, do_now
);