1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright 1997-1998 Transmeta Corporation -- All Rights Reserved
4 * Copyright 1999-2000 Jeremy Fitzhardinge <jeremy@goop.org>
5 * Copyright 2001-2006 Ian Kent <raven@themaw.net>
10 /* Check if a dentry can be expired */
11 static inline int autofs_can_expire(struct dentry *dentry,
12 unsigned long timeout, unsigned int how)
14 struct autofs_info *ino = autofs_dentry_ino(dentry);
16 /* dentry in the process of being deleted */
20 if (!(how & AUTOFS_EXP_IMMEDIATE)) {
21 /* Too young to die */
22 if (!timeout || time_after(ino->last_used + timeout, jiffies))
28 /* Check a mount point for busyness */
29 static int autofs_mount_busy(struct vfsmount *mnt,
30 struct dentry *dentry, unsigned int how)
32 struct dentry *top = dentry;
33 struct path path = {.mnt = mnt, .dentry = dentry};
36 pr_debug("dentry %p %pd\n", dentry, dentry);
40 if (!follow_down_one(&path))
43 if (is_autofs_dentry(path.dentry)) {
44 struct autofs_sb_info *sbi = autofs_sbi(path.dentry->d_sb);
46 /* This is an autofs submount, we can't expire it */
47 if (autofs_type_indirect(sbi->type))
51 /* Not a submount, has a forced expire been requested */
52 if (how & AUTOFS_EXP_FORCED) {
57 /* Update the expiry counter if fs is busy */
58 if (!may_umount_tree(path.mnt)) {
59 struct autofs_info *ino;
61 ino = autofs_dentry_ino(top);
62 ino->last_used = jiffies;
68 pr_debug("returning = %d\n", 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 = autofs_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 = autofs_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 autofs_direct_busy(struct vfsmount *mnt,
191 unsigned long timeout,
194 pr_debug("top %p %pd\n", top, top);
196 /* Forced expire, user space handles busy mounts */
197 if (how & AUTOFS_EXP_FORCED)
200 /* If it's busy update the expiry counters */
201 if (!may_umount_tree(mnt)) {
202 struct autofs_info *ino;
204 ino = autofs_dentry_ino(top);
206 ino->last_used = jiffies;
210 /* Timeout of a direct mount is determined by its top dentry */
211 if (!autofs_can_expire(top, timeout, how))
218 * Check a directory tree of mount points for busyness
219 * The tree is not busy iff no mountpoints are busy
221 static int autofs_tree_busy(struct vfsmount *mnt,
223 unsigned long timeout,
226 struct autofs_info *top_ino = autofs_dentry_ino(top);
229 pr_debug("top %p %pd\n", top, top);
231 /* Negative dentry - give up */
232 if (!simple_positive(top))
236 while ((p = get_next_positive_dentry(p, top))) {
237 pr_debug("dentry %p %pd\n", p, p);
240 * Is someone visiting anywhere in the subtree ?
241 * If there's no mount we need to check the usage
242 * count for the autofs dentry.
243 * If the fs is busy update the expiry counter.
245 if (d_mountpoint(p)) {
246 if (autofs_mount_busy(mnt, p, how)) {
247 top_ino->last_used = jiffies;
252 struct autofs_info *ino = autofs_dentry_ino(p);
253 unsigned int ino_count = atomic_read(&ino->count);
255 /* allow for dget above and top is already dgot */
261 if (d_count(p) > ino_count) {
262 top_ino->last_used = jiffies;
269 /* Forced expire, user space handles busy mounts */
270 if (how & AUTOFS_EXP_FORCED)
273 /* Timeout of a tree mount is ultimately determined by its top dentry */
274 if (!autofs_can_expire(top, timeout, how))
280 static struct dentry *autofs_check_leaves(struct vfsmount *mnt,
281 struct dentry *parent,
282 unsigned long timeout,
287 pr_debug("parent %p %pd\n", parent, parent);
290 while ((p = get_next_positive_dentry(p, parent))) {
291 pr_debug("dentry %p %pd\n", p, p);
293 if (d_mountpoint(p)) {
294 /* Can we umount this guy */
295 if (autofs_mount_busy(mnt, p, how))
298 /* This isn't a submount so if a forced expire
299 * has been requested, user space handles busy
301 if (how & AUTOFS_EXP_FORCED)
304 /* Can we expire this guy */
305 if (autofs_can_expire(p, timeout, how))
312 /* Check if we can expire a direct mount (possibly a tree) */
313 static struct dentry *autofs_expire_direct(struct super_block *sb,
314 struct vfsmount *mnt,
315 struct autofs_sb_info *sbi,
318 struct dentry *root = dget(sb->s_root);
319 struct autofs_info *ino;
320 unsigned long timeout;
325 timeout = sbi->exp_timeout;
327 if (!autofs_direct_busy(mnt, root, timeout, how)) {
328 spin_lock(&sbi->fs_lock);
329 ino = autofs_dentry_ino(root);
330 /* No point expiring a pending mount */
331 if (ino->flags & AUTOFS_INF_PENDING) {
332 spin_unlock(&sbi->fs_lock);
335 ino->flags |= AUTOFS_INF_WANT_EXPIRE;
336 spin_unlock(&sbi->fs_lock);
338 if (!autofs_direct_busy(mnt, root, timeout, how)) {
339 spin_lock(&sbi->fs_lock);
340 ino->flags |= AUTOFS_INF_EXPIRING;
341 init_completion(&ino->expire_complete);
342 spin_unlock(&sbi->fs_lock);
345 spin_lock(&sbi->fs_lock);
346 ino->flags &= ~AUTOFS_INF_WANT_EXPIRE;
347 spin_unlock(&sbi->fs_lock);
355 /* Check if 'dentry' should expire, or return a nearby
356 * dentry that is suitable.
357 * If returned dentry is different from arg dentry,
358 * then a dget() reference was taken, else not.
360 static struct dentry *should_expire(struct dentry *dentry,
361 struct vfsmount *mnt,
362 unsigned long timeout,
365 struct autofs_info *ino = autofs_dentry_ino(dentry);
366 unsigned int ino_count;
368 /* No point expiring a pending mount */
369 if (ino->flags & AUTOFS_INF_PENDING)
373 * Case 1: (i) indirect mount or top level pseudo direct mount
375 * (ii) indirect mount with offset mount, check the "/"
376 * offset (autofs-5.0+).
378 if (d_mountpoint(dentry)) {
379 pr_debug("checking mountpoint %p %pd\n", dentry, dentry);
381 /* Can we umount this guy */
382 if (autofs_mount_busy(mnt, dentry, how))
385 /* This isn't a submount so if a forced expire
386 * has been requested, user space handles busy
388 if (how & AUTOFS_EXP_FORCED)
391 /* Can we expire this guy */
392 if (autofs_can_expire(dentry, timeout, how))
397 if (d_really_is_positive(dentry) && d_is_symlink(dentry)) {
398 pr_debug("checking symlink %p %pd\n", dentry, dentry);
400 /* Forced expire, user space handles busy mounts */
401 if (how & AUTOFS_EXP_FORCED)
405 * A symlink can't be "busy" in the usual sense so
406 * just check last used for expire timeout.
408 if (autofs_can_expire(dentry, timeout, how))
413 if (simple_empty(dentry))
416 /* Case 2: tree mount, expire iff entire tree is not busy */
417 if (!(how & AUTOFS_EXP_LEAVES)) {
418 /* Not a forced expire? */
419 if (!(how & AUTOFS_EXP_FORCED)) {
420 /* ref-walk currently on this dentry? */
421 ino_count = atomic_read(&ino->count) + 1;
422 if (d_count(dentry) > ino_count)
426 if (!autofs_tree_busy(mnt, dentry, timeout, how))
429 * Case 3: pseudo direct mount, expire individual leaves
433 struct dentry *expired;
435 /* Not a forced expire? */
436 if (!(how & AUTOFS_EXP_FORCED)) {
437 /* ref-walk currently on this dentry? */
438 ino_count = atomic_read(&ino->count) + 1;
439 if (d_count(dentry) > ino_count)
443 expired = autofs_check_leaves(mnt, dentry, timeout, how);
445 if (expired == dentry)
454 * Find an eligible tree to time-out
455 * A tree is eligible if :-
456 * - it is unused by any user process
457 * - it has been unused for exp_timeout time
459 static struct dentry *autofs_expire_indirect(struct super_block *sb,
460 struct vfsmount *mnt,
461 struct autofs_sb_info *sbi,
464 unsigned long timeout;
465 struct dentry *root = sb->s_root;
466 struct dentry *dentry;
467 struct dentry *expired;
468 struct dentry *found;
469 struct autofs_info *ino;
474 timeout = sbi->exp_timeout;
477 while ((dentry = get_next_positive_subdir(dentry, root))) {
478 spin_lock(&sbi->fs_lock);
479 ino = autofs_dentry_ino(dentry);
480 if (ino->flags & AUTOFS_INF_WANT_EXPIRE) {
481 spin_unlock(&sbi->fs_lock);
484 spin_unlock(&sbi->fs_lock);
486 expired = should_expire(dentry, mnt, timeout, how);
490 spin_lock(&sbi->fs_lock);
491 ino = autofs_dentry_ino(expired);
492 ino->flags |= AUTOFS_INF_WANT_EXPIRE;
493 spin_unlock(&sbi->fs_lock);
496 /* Make sure a reference is not taken on found if
497 * things have changed.
499 how &= ~AUTOFS_EXP_LEAVES;
500 found = should_expire(expired, mnt, timeout, how);
501 if (!found || found != expired)
502 /* Something has changed, continue */
505 if (expired != dentry)
508 spin_lock(&sbi->fs_lock);
511 spin_lock(&sbi->fs_lock);
512 ino->flags &= ~AUTOFS_INF_WANT_EXPIRE;
513 spin_unlock(&sbi->fs_lock);
514 if (expired != dentry)
520 pr_debug("returning %p %pd\n", expired, expired);
521 ino->flags |= AUTOFS_INF_EXPIRING;
522 init_completion(&ino->expire_complete);
523 spin_unlock(&sbi->fs_lock);
527 int autofs_expire_wait(const struct path *path, int rcu_walk)
529 struct dentry *dentry = path->dentry;
530 struct autofs_sb_info *sbi = autofs_sbi(dentry->d_sb);
531 struct autofs_info *ino = autofs_dentry_ino(dentry);
535 /* Block on any pending expire */
536 if (!(ino->flags & AUTOFS_INF_WANT_EXPIRE))
542 spin_lock(&sbi->fs_lock);
543 state = ino->flags & (AUTOFS_INF_WANT_EXPIRE | AUTOFS_INF_EXPIRING);
544 if (state == AUTOFS_INF_WANT_EXPIRE) {
545 spin_unlock(&sbi->fs_lock);
547 * Possibly being selected for expire, wait until
548 * it's selected or not.
550 schedule_timeout_uninterruptible(HZ/10);
553 if (state & AUTOFS_INF_EXPIRING) {
554 spin_unlock(&sbi->fs_lock);
556 pr_debug("waiting for expire %p name=%pd\n", dentry, dentry);
558 status = autofs_wait(sbi, path, NFY_NONE);
559 wait_for_completion(&ino->expire_complete);
561 pr_debug("expire done status=%d\n", status);
563 if (d_unhashed(dentry))
568 spin_unlock(&sbi->fs_lock);
573 /* Perform an expiry operation */
574 int autofs_expire_run(struct super_block *sb,
575 struct vfsmount *mnt,
576 struct autofs_sb_info *sbi,
577 struct autofs_packet_expire __user *pkt_p)
579 struct autofs_packet_expire pkt;
580 struct autofs_info *ino;
581 struct dentry *dentry;
584 memset(&pkt, 0, sizeof(pkt));
586 pkt.hdr.proto_version = sbi->version;
587 pkt.hdr.type = autofs_ptype_expire;
589 dentry = autofs_expire_indirect(sb, mnt, sbi, 0);
593 pkt.len = dentry->d_name.len;
594 memcpy(pkt.name, dentry->d_name.name, pkt.len);
595 pkt.name[pkt.len] = '\0';
597 if (copy_to_user(pkt_p, &pkt, sizeof(struct autofs_packet_expire)))
600 spin_lock(&sbi->fs_lock);
601 ino = autofs_dentry_ino(dentry);
602 /* avoid rapid-fire expire attempts if expiry fails */
603 ino->last_used = jiffies;
604 ino->flags &= ~(AUTOFS_INF_EXPIRING|AUTOFS_INF_WANT_EXPIRE);
605 complete_all(&ino->expire_complete);
606 spin_unlock(&sbi->fs_lock);
613 int autofs_do_expire_multi(struct super_block *sb, struct vfsmount *mnt,
614 struct autofs_sb_info *sbi, unsigned int how)
616 struct dentry *dentry;
619 if (autofs_type_trigger(sbi->type))
620 dentry = autofs_expire_direct(sb, mnt, sbi, how);
622 dentry = autofs_expire_indirect(sb, mnt, sbi, how);
625 struct autofs_info *ino = autofs_dentry_ino(dentry);
626 const struct path path = { .mnt = mnt, .dentry = dentry };
628 /* This is synchronous because it makes the daemon a
631 ret = autofs_wait(sbi, &path, NFY_EXPIRE);
633 spin_lock(&sbi->fs_lock);
634 /* avoid rapid-fire expire attempts if expiry fails */
635 ino->last_used = jiffies;
636 ino->flags &= ~(AUTOFS_INF_EXPIRING|AUTOFS_INF_WANT_EXPIRE);
637 complete_all(&ino->expire_complete);
638 spin_unlock(&sbi->fs_lock);
646 * Call repeatedly until it returns -EAGAIN, meaning there's nothing
649 int autofs_expire_multi(struct super_block *sb, struct vfsmount *mnt,
650 struct autofs_sb_info *sbi, int __user *arg)
652 unsigned int how = 0;
654 if (arg && get_user(how, arg))
657 return autofs_do_expire_multi(sb, mnt, sbi, how);