2 * Copyright 1997-1998 Transmeta Corporation -- All Rights Reserved
3 * Copyright 1999-2000 Jeremy Fitzhardinge <jeremy@goop.org>
4 * Copyright 2001-2006 Ian Kent <raven@themaw.net>
6 * This file is part of the Linux kernel and is made available under
7 * the terms of the GNU General Public License, version 2, or at your
8 * option, any later version, incorporated herein by reference.
13 /* Check if a dentry can be expired */
14 static inline int autofs_can_expire(struct dentry
*dentry
,
15 unsigned long timeout
, unsigned int how
)
17 struct autofs_info
*ino
= autofs_dentry_ino(dentry
);
19 /* dentry in the process of being deleted */
23 if (!(how
& AUTOFS_EXP_IMMEDIATE
)) {
24 /* Too young to die */
25 if (!timeout
|| time_after(ino
->last_used
+ timeout
, jiffies
))
31 /* Check a mount point for busyness */
32 static int autofs_mount_busy(struct vfsmount
*mnt
,
33 struct dentry
*dentry
, unsigned int how
)
35 struct dentry
*top
= dentry
;
36 struct path path
= {.mnt
= mnt
, .dentry
= dentry
};
39 pr_debug("dentry %p %pd\n", dentry
, dentry
);
43 if (!follow_down_one(&path
))
46 if (is_autofs_dentry(path
.dentry
)) {
47 struct autofs_sb_info
*sbi
= autofs_sbi(path
.dentry
->d_sb
);
49 /* This is an autofs submount, we can't expire it */
50 if (autofs_type_indirect(sbi
->type
))
54 /* Not a submount, has a forced expire been requested */
55 if (how
& AUTOFS_EXP_FORCED
) {
60 /* Update the expiry counter if fs is busy */
61 if (!may_umount_tree(path
.mnt
)) {
62 struct autofs_info
*ino
;
64 ino
= autofs_dentry_ino(top
);
65 ino
->last_used
= jiffies
;
71 pr_debug("returning = %d\n", status
);
77 * Calculate and dget next entry in the subdirs list under root.
79 static struct dentry
*get_next_positive_subdir(struct dentry
*prev
,
82 struct autofs_sb_info
*sbi
= autofs_sbi(root
->d_sb
);
83 struct list_head
*next
;
86 spin_lock(&sbi
->lookup_lock
);
87 spin_lock(&root
->d_lock
);
90 next
= prev
->d_child
.next
;
92 prev
= dget_dlock(root
);
93 next
= prev
->d_subdirs
.next
;
97 if (next
== &root
->d_subdirs
) {
98 spin_unlock(&root
->d_lock
);
99 spin_unlock(&sbi
->lookup_lock
);
104 q
= list_entry(next
, struct dentry
, d_child
);
106 spin_lock_nested(&q
->d_lock
, DENTRY_D_LOCK_NESTED
);
107 /* Already gone or negative dentry (under construction) - try next */
108 if (!d_count(q
) || !simple_positive(q
)) {
109 spin_unlock(&q
->d_lock
);
110 next
= q
->d_child
.next
;
114 spin_unlock(&q
->d_lock
);
115 spin_unlock(&root
->d_lock
);
116 spin_unlock(&sbi
->lookup_lock
);
124 * Calculate and dget next entry in top down tree traversal.
126 static struct dentry
*get_next_positive_dentry(struct dentry
*prev
,
129 struct autofs_sb_info
*sbi
= autofs_sbi(root
->d_sb
);
130 struct list_head
*next
;
131 struct dentry
*p
, *ret
;
136 spin_lock(&sbi
->lookup_lock
);
139 spin_lock(&p
->d_lock
);
141 next
= p
->d_subdirs
.next
;
142 if (next
== &p
->d_subdirs
) {
144 struct dentry
*parent
;
147 spin_unlock(&p
->d_lock
);
148 spin_unlock(&sbi
->lookup_lock
);
153 parent
= p
->d_parent
;
154 if (!spin_trylock(&parent
->d_lock
)) {
155 spin_unlock(&p
->d_lock
);
159 spin_unlock(&p
->d_lock
);
160 next
= p
->d_child
.next
;
162 if (next
!= &parent
->d_subdirs
)
166 ret
= list_entry(next
, struct dentry
, d_child
);
168 spin_lock_nested(&ret
->d_lock
, DENTRY_D_LOCK_NESTED
);
169 /* Negative dentry - try next */
170 if (!simple_positive(ret
)) {
171 spin_unlock(&p
->d_lock
);
172 lock_set_subclass(&ret
->d_lock
.dep_map
, 0, _RET_IP_
);
177 spin_unlock(&ret
->d_lock
);
178 spin_unlock(&p
->d_lock
);
179 spin_unlock(&sbi
->lookup_lock
);
187 * Check a direct mount point for busyness.
188 * Direct mounts have similar expiry semantics to tree mounts.
189 * The tree is not busy iff no mountpoints are busy and there are no
192 static int autofs_direct_busy(struct vfsmount
*mnt
,
194 unsigned long timeout
,
197 pr_debug("top %p %pd\n", top
, top
);
199 /* Forced expire, user space handles busy mounts */
200 if (how
& AUTOFS_EXP_FORCED
)
203 /* If it's busy update the expiry counters */
204 if (!may_umount_tree(mnt
)) {
205 struct autofs_info
*ino
;
207 ino
= autofs_dentry_ino(top
);
209 ino
->last_used
= jiffies
;
213 /* Timeout of a direct mount is determined by its top dentry */
214 if (!autofs_can_expire(top
, timeout
, how
))
221 * Check a directory tree of mount points for busyness
222 * The tree is not busy iff no mountpoints are busy
224 static int autofs_tree_busy(struct vfsmount
*mnt
,
226 unsigned long timeout
,
229 struct autofs_info
*top_ino
= autofs_dentry_ino(top
);
232 pr_debug("top %p %pd\n", top
, top
);
234 /* Negative dentry - give up */
235 if (!simple_positive(top
))
239 while ((p
= get_next_positive_dentry(p
, top
))) {
240 pr_debug("dentry %p %pd\n", p
, p
);
243 * Is someone visiting anywhere in the subtree ?
244 * If there's no mount we need to check the usage
245 * count for the autofs dentry.
246 * If the fs is busy update the expiry counter.
248 if (d_mountpoint(p
)) {
249 if (autofs_mount_busy(mnt
, p
, how
)) {
250 top_ino
->last_used
= jiffies
;
255 struct autofs_info
*ino
= autofs_dentry_ino(p
);
256 unsigned int ino_count
= atomic_read(&ino
->count
);
258 /* allow for dget above and top is already dgot */
264 if (d_count(p
) > ino_count
) {
265 top_ino
->last_used
= jiffies
;
272 /* Forced expire, user space handles busy mounts */
273 if (how
& AUTOFS_EXP_FORCED
)
276 /* Timeout of a tree mount is ultimately determined by its top dentry */
277 if (!autofs_can_expire(top
, timeout
, how
))
283 static struct dentry
*autofs_check_leaves(struct vfsmount
*mnt
,
284 struct dentry
*parent
,
285 unsigned long timeout
,
290 pr_debug("parent %p %pd\n", parent
, parent
);
293 while ((p
= get_next_positive_dentry(p
, parent
))) {
294 pr_debug("dentry %p %pd\n", p
, p
);
296 if (d_mountpoint(p
)) {
297 /* Can we umount this guy */
298 if (autofs_mount_busy(mnt
, p
, how
))
301 /* This isn't a submount so if a forced expire
302 * has been requested, user space handles busy
304 if (how
& AUTOFS_EXP_FORCED
)
307 /* Can we expire this guy */
308 if (autofs_can_expire(p
, timeout
, how
))
315 /* Check if we can expire a direct mount (possibly a tree) */
316 static struct dentry
*autofs_expire_direct(struct super_block
*sb
,
317 struct vfsmount
*mnt
,
318 struct autofs_sb_info
*sbi
,
321 struct dentry
*root
= dget(sb
->s_root
);
322 struct autofs_info
*ino
;
323 unsigned long timeout
;
328 timeout
= sbi
->exp_timeout
;
330 if (!autofs_direct_busy(mnt
, root
, timeout
, how
)) {
331 spin_lock(&sbi
->fs_lock
);
332 ino
= autofs_dentry_ino(root
);
333 /* No point expiring a pending mount */
334 if (ino
->flags
& AUTOFS_INF_PENDING
) {
335 spin_unlock(&sbi
->fs_lock
);
338 ino
->flags
|= AUTOFS_INF_WANT_EXPIRE
;
339 spin_unlock(&sbi
->fs_lock
);
341 if (!autofs_direct_busy(mnt
, root
, timeout
, how
)) {
342 spin_lock(&sbi
->fs_lock
);
343 ino
->flags
|= AUTOFS_INF_EXPIRING
;
344 init_completion(&ino
->expire_complete
);
345 spin_unlock(&sbi
->fs_lock
);
348 spin_lock(&sbi
->fs_lock
);
349 ino
->flags
&= ~AUTOFS_INF_WANT_EXPIRE
;
350 spin_unlock(&sbi
->fs_lock
);
358 /* Check if 'dentry' should expire, or return a nearby
359 * dentry that is suitable.
360 * If returned dentry is different from arg dentry,
361 * then a dget() reference was taken, else not.
363 static struct dentry
*should_expire(struct dentry
*dentry
,
364 struct vfsmount
*mnt
,
365 unsigned long timeout
,
368 struct autofs_info
*ino
= autofs_dentry_ino(dentry
);
369 unsigned int ino_count
;
371 /* No point expiring a pending mount */
372 if (ino
->flags
& AUTOFS_INF_PENDING
)
376 * Case 1: (i) indirect mount or top level pseudo direct mount
378 * (ii) indirect mount with offset mount, check the "/"
379 * offset (autofs-5.0+).
381 if (d_mountpoint(dentry
)) {
382 pr_debug("checking mountpoint %p %pd\n", dentry
, dentry
);
384 /* Can we umount this guy */
385 if (autofs_mount_busy(mnt
, dentry
, how
))
388 /* This isn't a submount so if a forced expire
389 * has been requested, user space handles busy
391 if (how
& AUTOFS_EXP_FORCED
)
394 /* Can we expire this guy */
395 if (autofs_can_expire(dentry
, timeout
, how
))
400 if (d_really_is_positive(dentry
) && d_is_symlink(dentry
)) {
401 pr_debug("checking symlink %p %pd\n", dentry
, dentry
);
403 /* Forced expire, user space handles busy mounts */
404 if (how
& AUTOFS_EXP_FORCED
)
408 * A symlink can't be "busy" in the usual sense so
409 * just check last used for expire timeout.
411 if (autofs_can_expire(dentry
, timeout
, how
))
416 if (simple_empty(dentry
))
419 /* Case 2: tree mount, expire iff entire tree is not busy */
420 if (!(how
& AUTOFS_EXP_LEAVES
)) {
421 /* Not a forced expire? */
422 if (!(how
& AUTOFS_EXP_FORCED
)) {
423 /* ref-walk currently on this dentry? */
424 ino_count
= atomic_read(&ino
->count
) + 1;
425 if (d_count(dentry
) > ino_count
)
429 if (!autofs_tree_busy(mnt
, dentry
, timeout
, how
))
432 * Case 3: pseudo direct mount, expire individual leaves
436 struct dentry
*expired
;
438 /* Not a forced expire? */
439 if (!(how
& AUTOFS_EXP_FORCED
)) {
440 /* ref-walk currently on this dentry? */
441 ino_count
= atomic_read(&ino
->count
) + 1;
442 if (d_count(dentry
) > ino_count
)
446 expired
= autofs_check_leaves(mnt
, dentry
, timeout
, how
);
448 if (expired
== dentry
)
457 * Find an eligible tree to time-out
458 * A tree is eligible if :-
459 * - it is unused by any user process
460 * - it has been unused for exp_timeout time
462 static struct dentry
*autofs_expire_indirect(struct super_block
*sb
,
463 struct vfsmount
*mnt
,
464 struct autofs_sb_info
*sbi
,
467 unsigned long timeout
;
468 struct dentry
*root
= sb
->s_root
;
469 struct dentry
*dentry
;
470 struct dentry
*expired
;
471 struct dentry
*found
;
472 struct autofs_info
*ino
;
477 timeout
= sbi
->exp_timeout
;
480 while ((dentry
= get_next_positive_subdir(dentry
, root
))) {
481 spin_lock(&sbi
->fs_lock
);
482 ino
= autofs_dentry_ino(dentry
);
483 if (ino
->flags
& AUTOFS_INF_WANT_EXPIRE
) {
484 spin_unlock(&sbi
->fs_lock
);
487 spin_unlock(&sbi
->fs_lock
);
489 expired
= should_expire(dentry
, mnt
, timeout
, how
);
493 spin_lock(&sbi
->fs_lock
);
494 ino
= autofs_dentry_ino(expired
);
495 ino
->flags
|= AUTOFS_INF_WANT_EXPIRE
;
496 spin_unlock(&sbi
->fs_lock
);
499 /* Make sure a reference is not taken on found if
500 * things have changed.
502 how
&= ~AUTOFS_EXP_LEAVES
;
503 found
= should_expire(expired
, mnt
, timeout
, how
);
504 if (!found
|| found
!= expired
)
505 /* Something has changed, continue */
508 if (expired
!= dentry
)
511 spin_lock(&sbi
->fs_lock
);
514 spin_lock(&sbi
->fs_lock
);
515 ino
->flags
&= ~AUTOFS_INF_WANT_EXPIRE
;
516 spin_unlock(&sbi
->fs_lock
);
517 if (expired
!= dentry
)
523 pr_debug("returning %p %pd\n", expired
, expired
);
524 ino
->flags
|= AUTOFS_INF_EXPIRING
;
525 init_completion(&ino
->expire_complete
);
526 spin_unlock(&sbi
->fs_lock
);
530 int autofs_expire_wait(const struct path
*path
, int rcu_walk
)
532 struct dentry
*dentry
= path
->dentry
;
533 struct autofs_sb_info
*sbi
= autofs_sbi(dentry
->d_sb
);
534 struct autofs_info
*ino
= autofs_dentry_ino(dentry
);
538 /* Block on any pending expire */
539 if (!(ino
->flags
& AUTOFS_INF_WANT_EXPIRE
))
545 spin_lock(&sbi
->fs_lock
);
546 state
= ino
->flags
& (AUTOFS_INF_WANT_EXPIRE
| AUTOFS_INF_EXPIRING
);
547 if (state
== AUTOFS_INF_WANT_EXPIRE
) {
548 spin_unlock(&sbi
->fs_lock
);
550 * Possibly being selected for expire, wait until
551 * it's selected or not.
553 schedule_timeout_uninterruptible(HZ
/10);
556 if (state
& AUTOFS_INF_EXPIRING
) {
557 spin_unlock(&sbi
->fs_lock
);
559 pr_debug("waiting for expire %p name=%pd\n", dentry
, dentry
);
561 status
= autofs_wait(sbi
, path
, NFY_NONE
);
562 wait_for_completion(&ino
->expire_complete
);
564 pr_debug("expire done status=%d\n", status
);
566 if (d_unhashed(dentry
))
571 spin_unlock(&sbi
->fs_lock
);
576 /* Perform an expiry operation */
577 int autofs_expire_run(struct super_block
*sb
,
578 struct vfsmount
*mnt
,
579 struct autofs_sb_info
*sbi
,
580 struct autofs_packet_expire __user
*pkt_p
)
582 struct autofs_packet_expire pkt
;
583 struct autofs_info
*ino
;
584 struct dentry
*dentry
;
587 memset(&pkt
, 0, sizeof(pkt
));
589 pkt
.hdr
.proto_version
= sbi
->version
;
590 pkt
.hdr
.type
= autofs_ptype_expire
;
592 dentry
= autofs_expire_indirect(sb
, mnt
, sbi
, 0);
596 pkt
.len
= dentry
->d_name
.len
;
597 memcpy(pkt
.name
, dentry
->d_name
.name
, pkt
.len
);
598 pkt
.name
[pkt
.len
] = '\0';
600 if (copy_to_user(pkt_p
, &pkt
, sizeof(struct autofs_packet_expire
)))
603 spin_lock(&sbi
->fs_lock
);
604 ino
= autofs_dentry_ino(dentry
);
605 /* avoid rapid-fire expire attempts if expiry fails */
606 ino
->last_used
= jiffies
;
607 ino
->flags
&= ~(AUTOFS_INF_EXPIRING
|AUTOFS_INF_WANT_EXPIRE
);
608 complete_all(&ino
->expire_complete
);
609 spin_unlock(&sbi
->fs_lock
);
616 int autofs_do_expire_multi(struct super_block
*sb
, struct vfsmount
*mnt
,
617 struct autofs_sb_info
*sbi
, unsigned int how
)
619 struct dentry
*dentry
;
622 if (autofs_type_trigger(sbi
->type
))
623 dentry
= autofs_expire_direct(sb
, mnt
, sbi
, how
);
625 dentry
= autofs_expire_indirect(sb
, mnt
, sbi
, how
);
628 struct autofs_info
*ino
= autofs_dentry_ino(dentry
);
629 const struct path path
= { .mnt
= mnt
, .dentry
= dentry
};
631 /* This is synchronous because it makes the daemon a
634 ret
= autofs_wait(sbi
, &path
, NFY_EXPIRE
);
636 spin_lock(&sbi
->fs_lock
);
637 /* avoid rapid-fire expire attempts if expiry fails */
638 ino
->last_used
= jiffies
;
639 ino
->flags
&= ~(AUTOFS_INF_EXPIRING
|AUTOFS_INF_WANT_EXPIRE
);
640 complete_all(&ino
->expire_complete
);
641 spin_unlock(&sbi
->fs_lock
);
649 * Call repeatedly until it returns -EAGAIN, meaning there's nothing
652 int autofs_expire_multi(struct super_block
*sb
, struct vfsmount
*mnt
,
653 struct autofs_sb_info
*sbi
, int __user
*arg
)
655 unsigned int how
= 0;
657 if (arg
&& get_user(how
, arg
))
660 return autofs_do_expire_multi(sb
, mnt
, sbi
, how
);