^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Copyright 1997-1998 Transmeta Corporation -- All Rights Reserved
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * Copyright 1999-2000 Jeremy Fitzhardinge <jeremy@goop.org>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright 2001-2006 Ian Kent <raven@themaw.net>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include "autofs_i.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) /* Check if a dentry can be expired */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) static inline int autofs_can_expire(struct dentry *dentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) unsigned long timeout, unsigned int how)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) struct autofs_info *ino = autofs_dentry_ino(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) /* dentry in the process of being deleted */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) if (ino == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) if (!(how & AUTOFS_EXP_IMMEDIATE)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) /* Too young to die */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) if (!timeout || time_after(ino->last_used + timeout, jiffies))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) /* Check a mount point for busyness */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) static int autofs_mount_busy(struct vfsmount *mnt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) struct dentry *dentry, unsigned int how)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) struct dentry *top = dentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) struct path path = {.mnt = mnt, .dentry = dentry};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) int status = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) pr_debug("dentry %p %pd\n", dentry, dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) path_get(&path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) if (!follow_down_one(&path))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) if (is_autofs_dentry(path.dentry)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) struct autofs_sb_info *sbi = autofs_sbi(path.dentry->d_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) /* This is an autofs submount, we can't expire it */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) if (autofs_type_indirect(sbi->type))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) /* Not a submount, has a forced expire been requested */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) if (how & AUTOFS_EXP_FORCED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) status = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) /* Update the expiry counter if fs is busy */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) if (!may_umount_tree(path.mnt)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) struct autofs_info *ino;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) ino = autofs_dentry_ino(top);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) ino->last_used = jiffies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) status = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) done:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) pr_debug("returning = %d\n", status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) path_put(&path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) return status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) /* p->d_lock held */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) static struct dentry *positive_after(struct dentry *p, struct dentry *child)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) if (child)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) child = list_next_entry(child, d_child);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) child = list_first_entry(&p->d_subdirs, struct dentry, d_child);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) list_for_each_entry_from(child, &p->d_subdirs, d_child) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) spin_lock_nested(&child->d_lock, DENTRY_D_LOCK_NESTED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) if (simple_positive(child)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) dget_dlock(child);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) spin_unlock(&child->d_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) return child;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) spin_unlock(&child->d_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) * Calculate and dget next entry in the subdirs list under root.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) static struct dentry *get_next_positive_subdir(struct dentry *prev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) struct dentry *root)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) struct autofs_sb_info *sbi = autofs_sbi(root->d_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) struct dentry *q;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) spin_lock(&sbi->lookup_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) spin_lock(&root->d_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) q = positive_after(root, prev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) spin_unlock(&root->d_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) spin_unlock(&sbi->lookup_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) dput(prev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) return q;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) * Calculate and dget next entry in top down tree traversal.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) static struct dentry *get_next_positive_dentry(struct dentry *prev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) struct dentry *root)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) struct autofs_sb_info *sbi = autofs_sbi(root->d_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) struct dentry *p = prev, *ret = NULL, *d = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) if (prev == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) return dget(root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) spin_lock(&sbi->lookup_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) spin_lock(&p->d_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) while (1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) struct dentry *parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) ret = positive_after(p, d);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) if (ret || p == root)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) parent = p->d_parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) spin_unlock(&p->d_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) spin_lock(&parent->d_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) d = p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) p = parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) spin_unlock(&p->d_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) spin_unlock(&sbi->lookup_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) dput(prev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) * Check a direct mount point for busyness.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) * Direct mounts have similar expiry semantics to tree mounts.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) * The tree is not busy iff no mountpoints are busy and there are no
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) * autofs submounts.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) static int autofs_direct_busy(struct vfsmount *mnt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) struct dentry *top,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) unsigned long timeout,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) unsigned int how)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) pr_debug("top %p %pd\n", top, top);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) /* Forced expire, user space handles busy mounts */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) if (how & AUTOFS_EXP_FORCED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) /* If it's busy update the expiry counters */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) if (!may_umount_tree(mnt)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) struct autofs_info *ino;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) ino = autofs_dentry_ino(top);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) if (ino)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) ino->last_used = jiffies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) /* Timeout of a direct mount is determined by its top dentry */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) if (!autofs_can_expire(top, timeout, how))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) * Check a directory tree of mount points for busyness
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) * The tree is not busy iff no mountpoints are busy
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) static int autofs_tree_busy(struct vfsmount *mnt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) struct dentry *top,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) unsigned long timeout,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) unsigned int how)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) struct autofs_info *top_ino = autofs_dentry_ino(top);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) struct dentry *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) pr_debug("top %p %pd\n", top, top);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) /* Negative dentry - give up */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) if (!simple_positive(top))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) p = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) while ((p = get_next_positive_dentry(p, top))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) pr_debug("dentry %p %pd\n", p, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) * Is someone visiting anywhere in the subtree ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) * If there's no mount we need to check the usage
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) * count for the autofs dentry.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) * If the fs is busy update the expiry counter.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) if (d_mountpoint(p)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) if (autofs_mount_busy(mnt, p, how)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) top_ino->last_used = jiffies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) dput(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) struct autofs_info *ino = autofs_dentry_ino(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) unsigned int ino_count = READ_ONCE(ino->count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) /* allow for dget above and top is already dgot */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) if (p == top)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) ino_count += 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) ino_count++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) if (d_count(p) > ino_count) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) top_ino->last_used = jiffies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) dput(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) /* Forced expire, user space handles busy mounts */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) if (how & AUTOFS_EXP_FORCED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) /* Timeout of a tree mount is ultimately determined by its top dentry */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) if (!autofs_can_expire(top, timeout, how))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) static struct dentry *autofs_check_leaves(struct vfsmount *mnt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) struct dentry *parent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) unsigned long timeout,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) unsigned int how)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) struct dentry *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) pr_debug("parent %p %pd\n", parent, parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) p = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) while ((p = get_next_positive_dentry(p, parent))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) pr_debug("dentry %p %pd\n", p, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) if (d_mountpoint(p)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) /* Can we umount this guy */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) if (autofs_mount_busy(mnt, p, how))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) /* This isn't a submount so if a forced expire
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) * has been requested, user space handles busy
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) * mounts */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) if (how & AUTOFS_EXP_FORCED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) return p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) /* Can we expire this guy */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) if (autofs_can_expire(p, timeout, how))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) return p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) /* Check if we can expire a direct mount (possibly a tree) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) static struct dentry *autofs_expire_direct(struct super_block *sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) struct vfsmount *mnt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) struct autofs_sb_info *sbi,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) unsigned int how)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) struct dentry *root = dget(sb->s_root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) struct autofs_info *ino;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) unsigned long timeout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) if (!root)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) timeout = sbi->exp_timeout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) if (!autofs_direct_busy(mnt, root, timeout, how)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) spin_lock(&sbi->fs_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) ino = autofs_dentry_ino(root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) /* No point expiring a pending mount */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) if (ino->flags & AUTOFS_INF_PENDING) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) spin_unlock(&sbi->fs_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) ino->flags |= AUTOFS_INF_WANT_EXPIRE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) spin_unlock(&sbi->fs_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) synchronize_rcu();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) if (!autofs_direct_busy(mnt, root, timeout, how)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) spin_lock(&sbi->fs_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) ino->flags |= AUTOFS_INF_EXPIRING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) init_completion(&ino->expire_complete);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) spin_unlock(&sbi->fs_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) return root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) spin_lock(&sbi->fs_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) ino->flags &= ~AUTOFS_INF_WANT_EXPIRE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) spin_unlock(&sbi->fs_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) dput(root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) /* Check if 'dentry' should expire, or return a nearby
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) * dentry that is suitable.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) * If returned dentry is different from arg dentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) * then a dget() reference was taken, else not.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) static struct dentry *should_expire(struct dentry *dentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) struct vfsmount *mnt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) unsigned long timeout,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) unsigned int how)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) struct autofs_info *ino = autofs_dentry_ino(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) unsigned int ino_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) /* No point expiring a pending mount */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) if (ino->flags & AUTOFS_INF_PENDING)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) * Case 1: (i) indirect mount or top level pseudo direct mount
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) * (autofs-4.1).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) * (ii) indirect mount with offset mount, check the "/"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) * offset (autofs-5.0+).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) if (d_mountpoint(dentry)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) pr_debug("checking mountpoint %p %pd\n", dentry, dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) /* Can we umount this guy */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) if (autofs_mount_busy(mnt, dentry, how))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) /* This isn't a submount so if a forced expire
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) * has been requested, user space handles busy
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) * mounts */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) if (how & AUTOFS_EXP_FORCED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) return dentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) /* Can we expire this guy */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) if (autofs_can_expire(dentry, timeout, how))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) return dentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) if (d_really_is_positive(dentry) && d_is_symlink(dentry)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) pr_debug("checking symlink %p %pd\n", dentry, dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) /* Forced expire, user space handles busy mounts */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) if (how & AUTOFS_EXP_FORCED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) return dentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) * A symlink can't be "busy" in the usual sense so
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) * just check last used for expire timeout.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) if (autofs_can_expire(dentry, timeout, how))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) return dentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) if (simple_empty(dentry))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) /* Case 2: tree mount, expire iff entire tree is not busy */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) if (!(how & AUTOFS_EXP_LEAVES)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) /* Not a forced expire? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) if (!(how & AUTOFS_EXP_FORCED)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) /* ref-walk currently on this dentry? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) ino_count = READ_ONCE(ino->count) + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) if (d_count(dentry) > ino_count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) if (!autofs_tree_busy(mnt, dentry, timeout, how))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) return dentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) * Case 3: pseudo direct mount, expire individual leaves
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) * (autofs-4.1).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) struct dentry *expired;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) /* Not a forced expire? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) if (!(how & AUTOFS_EXP_FORCED)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) /* ref-walk currently on this dentry? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) ino_count = READ_ONCE(ino->count) + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) if (d_count(dentry) > ino_count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) expired = autofs_check_leaves(mnt, dentry, timeout, how);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) if (expired) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) if (expired == dentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) dput(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) return expired;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) * Find an eligible tree to time-out
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) * A tree is eligible if :-
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) * - it is unused by any user process
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) * - it has been unused for exp_timeout time
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) static struct dentry *autofs_expire_indirect(struct super_block *sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) struct vfsmount *mnt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) struct autofs_sb_info *sbi,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) unsigned int how)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) unsigned long timeout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) struct dentry *root = sb->s_root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) struct dentry *dentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) struct dentry *expired;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) struct dentry *found;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) struct autofs_info *ino;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) if (!root)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) timeout = sbi->exp_timeout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) dentry = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) while ((dentry = get_next_positive_subdir(dentry, root))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) spin_lock(&sbi->fs_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) ino = autofs_dentry_ino(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) if (ino->flags & AUTOFS_INF_WANT_EXPIRE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) spin_unlock(&sbi->fs_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) spin_unlock(&sbi->fs_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) expired = should_expire(dentry, mnt, timeout, how);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) if (!expired)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) spin_lock(&sbi->fs_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) ino = autofs_dentry_ino(expired);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) ino->flags |= AUTOFS_INF_WANT_EXPIRE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) spin_unlock(&sbi->fs_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) synchronize_rcu();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) /* Make sure a reference is not taken on found if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) * things have changed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) how &= ~AUTOFS_EXP_LEAVES;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) found = should_expire(expired, mnt, timeout, how);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) if (found != expired) { // something has changed, continue
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) dput(found);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) goto next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) if (expired != dentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) dput(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) spin_lock(&sbi->fs_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) goto found;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) next:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) spin_lock(&sbi->fs_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) ino->flags &= ~AUTOFS_INF_WANT_EXPIRE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) spin_unlock(&sbi->fs_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) if (expired != dentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) dput(expired);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) found:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) pr_debug("returning %p %pd\n", expired, expired);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) ino->flags |= AUTOFS_INF_EXPIRING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) init_completion(&ino->expire_complete);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) spin_unlock(&sbi->fs_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) return expired;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) int autofs_expire_wait(const struct path *path, int rcu_walk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) struct dentry *dentry = path->dentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) struct autofs_sb_info *sbi = autofs_sbi(dentry->d_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) struct autofs_info *ino = autofs_dentry_ino(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) int status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) int state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) /* Block on any pending expire */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) if (!(ino->flags & AUTOFS_INF_WANT_EXPIRE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) if (rcu_walk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) return -ECHILD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) retry:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) spin_lock(&sbi->fs_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) state = ino->flags & (AUTOFS_INF_WANT_EXPIRE | AUTOFS_INF_EXPIRING);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) if (state == AUTOFS_INF_WANT_EXPIRE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) spin_unlock(&sbi->fs_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) * Possibly being selected for expire, wait until
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) * it's selected or not.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) schedule_timeout_uninterruptible(HZ/10);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) goto retry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) if (state & AUTOFS_INF_EXPIRING) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) spin_unlock(&sbi->fs_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) pr_debug("waiting for expire %p name=%pd\n", dentry, dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) status = autofs_wait(sbi, path, NFY_NONE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) wait_for_completion(&ino->expire_complete);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) pr_debug("expire done status=%d\n", status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) if (d_unhashed(dentry))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) return -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) return status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) spin_unlock(&sbi->fs_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) /* Perform an expiry operation */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) int autofs_expire_run(struct super_block *sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) struct vfsmount *mnt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) struct autofs_sb_info *sbi,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) struct autofs_packet_expire __user *pkt_p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) struct autofs_packet_expire pkt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) struct autofs_info *ino;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) struct dentry *dentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) memset(&pkt, 0, sizeof(pkt));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) pkt.hdr.proto_version = sbi->version;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) pkt.hdr.type = autofs_ptype_expire;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) dentry = autofs_expire_indirect(sb, mnt, sbi, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) if (!dentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) return -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) pkt.len = dentry->d_name.len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) memcpy(pkt.name, dentry->d_name.name, pkt.len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) pkt.name[pkt.len] = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) if (copy_to_user(pkt_p, &pkt, sizeof(struct autofs_packet_expire)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) ret = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) spin_lock(&sbi->fs_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) ino = autofs_dentry_ino(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) /* avoid rapid-fire expire attempts if expiry fails */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) ino->last_used = jiffies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) ino->flags &= ~(AUTOFS_INF_EXPIRING|AUTOFS_INF_WANT_EXPIRE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) complete_all(&ino->expire_complete);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) spin_unlock(&sbi->fs_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) dput(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) int autofs_do_expire_multi(struct super_block *sb, struct vfsmount *mnt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) struct autofs_sb_info *sbi, unsigned int how)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) struct dentry *dentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) int ret = -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) if (autofs_type_trigger(sbi->type))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) dentry = autofs_expire_direct(sb, mnt, sbi, how);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) dentry = autofs_expire_indirect(sb, mnt, sbi, how);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) if (dentry) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) struct autofs_info *ino = autofs_dentry_ino(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) const struct path path = { .mnt = mnt, .dentry = dentry };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) /* This is synchronous because it makes the daemon a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) * little easier
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) ret = autofs_wait(sbi, &path, NFY_EXPIRE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) spin_lock(&sbi->fs_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) /* avoid rapid-fire expire attempts if expiry fails */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) ino->last_used = jiffies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) ino->flags &= ~(AUTOFS_INF_EXPIRING|AUTOFS_INF_WANT_EXPIRE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) complete_all(&ino->expire_complete);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) spin_unlock(&sbi->fs_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) dput(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) * Call repeatedly until it returns -EAGAIN, meaning there's nothing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) * more to be done.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) int autofs_expire_multi(struct super_block *sb, struct vfsmount *mnt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) struct autofs_sb_info *sbi, int __user *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) unsigned int how = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) if (arg && get_user(how, arg))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) return autofs_do_expire_multi(sb, mnt, sbi, how);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) }