1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright 1997-1998 Transmeta Corporation -- All Rights Reserved
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 static struct dentry *positive_after(struct dentry *p, struct dentry *child)
76 child = child ? d_next_sibling(child) : d_first_child(p);
78 hlist_for_each_entry_from(child, d_sib) {
79 spin_lock_nested(&child->d_lock, DENTRY_D_LOCK_NESTED);
80 if (simple_positive(child)) {
82 spin_unlock(&child->d_lock);
85 spin_unlock(&child->d_lock);
92 * Calculate and dget next entry in the subdirs list under root.
94 static struct dentry *get_next_positive_subdir(struct dentry *prev,
97 struct autofs_sb_info *sbi = autofs_sbi(root->d_sb);
100 spin_lock(&sbi->lookup_lock);
101 spin_lock(&root->d_lock);
102 q = positive_after(root, prev);
103 spin_unlock(&root->d_lock);
104 spin_unlock(&sbi->lookup_lock);
110 * Calculate and dget next entry in top down tree traversal.
112 static struct dentry *get_next_positive_dentry(struct dentry *prev,
115 struct autofs_sb_info *sbi = autofs_sbi(root->d_sb);
116 struct dentry *p = prev, *ret = NULL, *d = NULL;
121 spin_lock(&sbi->lookup_lock);
122 spin_lock(&p->d_lock);
124 struct dentry *parent;
126 ret = positive_after(p, d);
127 if (ret || p == root)
129 parent = p->d_parent;
130 spin_unlock(&p->d_lock);
131 spin_lock(&parent->d_lock);
135 spin_unlock(&p->d_lock);
136 spin_unlock(&sbi->lookup_lock);
142 * Check a direct mount point for busyness.
143 * Direct mounts have similar expiry semantics to tree mounts.
144 * The tree is not busy iff no mountpoints are busy and there are no
147 static int autofs_direct_busy(struct vfsmount *mnt,
149 unsigned long timeout,
152 pr_debug("top %p %pd\n", top, top);
154 /* Forced expire, user space handles busy mounts */
155 if (how & AUTOFS_EXP_FORCED)
158 /* If it's busy update the expiry counters */
159 if (!may_umount_tree(mnt)) {
160 struct autofs_info *ino;
162 ino = autofs_dentry_ino(top);
164 ino->last_used = jiffies;
168 /* Timeout of a direct mount is determined by its top dentry */
169 if (!autofs_can_expire(top, timeout, how))
176 * Check a directory tree of mount points for busyness
177 * The tree is not busy iff no mountpoints are busy
179 static int autofs_tree_busy(struct vfsmount *mnt,
181 unsigned long timeout,
184 struct autofs_info *top_ino = autofs_dentry_ino(top);
187 pr_debug("top %p %pd\n", top, top);
189 /* Negative dentry - give up */
190 if (!simple_positive(top))
194 while ((p = get_next_positive_dentry(p, top))) {
195 pr_debug("dentry %p %pd\n", p, p);
198 * Is someone visiting anywhere in the subtree ?
199 * If there's no mount we need to check the usage
200 * count for the autofs dentry.
201 * If the fs is busy update the expiry counter.
203 if (d_mountpoint(p)) {
204 if (autofs_mount_busy(mnt, p, how)) {
205 top_ino->last_used = jiffies;
210 struct autofs_info *ino = autofs_dentry_ino(p);
211 unsigned int ino_count = READ_ONCE(ino->count);
213 /* allow for dget above and top is already dgot */
219 if (d_count(p) > ino_count) {
220 top_ino->last_used = jiffies;
227 /* Forced expire, user space handles busy mounts */
228 if (how & AUTOFS_EXP_FORCED)
231 /* Timeout of a tree mount is ultimately determined by its top dentry */
232 if (!autofs_can_expire(top, timeout, how))
238 static struct dentry *autofs_check_leaves(struct vfsmount *mnt,
239 struct dentry *parent,
240 unsigned long timeout,
245 pr_debug("parent %p %pd\n", parent, parent);
248 while ((p = get_next_positive_dentry(p, parent))) {
249 pr_debug("dentry %p %pd\n", p, p);
251 if (d_mountpoint(p)) {
252 /* Can we umount this guy */
253 if (autofs_mount_busy(mnt, p, how))
256 /* This isn't a submount so if a forced expire
257 * has been requested, user space handles busy
259 if (how & AUTOFS_EXP_FORCED)
262 /* Can we expire this guy */
263 if (autofs_can_expire(p, timeout, how))
270 /* Check if we can expire a direct mount (possibly a tree) */
271 static struct dentry *autofs_expire_direct(struct super_block *sb,
272 struct vfsmount *mnt,
273 struct autofs_sb_info *sbi,
276 struct dentry *root = dget(sb->s_root);
277 struct autofs_info *ino;
278 unsigned long timeout;
283 timeout = sbi->exp_timeout;
285 if (!autofs_direct_busy(mnt, root, timeout, how)) {
286 spin_lock(&sbi->fs_lock);
287 ino = autofs_dentry_ino(root);
288 /* No point expiring a pending mount */
289 if (ino->flags & AUTOFS_INF_PENDING) {
290 spin_unlock(&sbi->fs_lock);
293 ino->flags |= AUTOFS_INF_WANT_EXPIRE;
294 spin_unlock(&sbi->fs_lock);
296 if (!autofs_direct_busy(mnt, root, timeout, how)) {
297 spin_lock(&sbi->fs_lock);
298 ino->flags |= AUTOFS_INF_EXPIRING;
299 init_completion(&ino->expire_complete);
300 spin_unlock(&sbi->fs_lock);
303 spin_lock(&sbi->fs_lock);
304 ino->flags &= ~AUTOFS_INF_WANT_EXPIRE;
305 spin_unlock(&sbi->fs_lock);
313 /* Check if 'dentry' should expire, or return a nearby
314 * dentry that is suitable.
315 * If returned dentry is different from arg dentry,
316 * then a dget() reference was taken, else not.
318 static struct dentry *should_expire(struct dentry *dentry,
319 struct vfsmount *mnt,
320 unsigned long timeout,
323 struct autofs_info *ino = autofs_dentry_ino(dentry);
324 unsigned int ino_count;
326 /* No point expiring a pending mount */
327 if (ino->flags & AUTOFS_INF_PENDING)
331 * Case 1: (i) indirect mount or top level pseudo direct mount
333 * (ii) indirect mount with offset mount, check the "/"
334 * offset (autofs-5.0+).
336 if (d_mountpoint(dentry)) {
337 pr_debug("checking mountpoint %p %pd\n", dentry, dentry);
339 /* Can we umount this guy */
340 if (autofs_mount_busy(mnt, dentry, how))
343 /* This isn't a submount so if a forced expire
344 * has been requested, user space handles busy
346 if (how & AUTOFS_EXP_FORCED)
349 /* Can we expire this guy */
350 if (autofs_can_expire(dentry, timeout, how))
355 if (d_is_symlink(dentry)) {
356 pr_debug("checking symlink %p %pd\n", dentry, dentry);
358 /* Forced expire, user space handles busy mounts */
359 if (how & AUTOFS_EXP_FORCED)
363 * A symlink can't be "busy" in the usual sense so
364 * just check last used for expire timeout.
366 if (autofs_can_expire(dentry, timeout, how))
371 if (autofs_empty(ino))
374 /* Case 2: tree mount, expire iff entire tree is not busy */
375 if (!(how & AUTOFS_EXP_LEAVES)) {
376 /* Not a forced expire? */
377 if (!(how & AUTOFS_EXP_FORCED)) {
378 /* ref-walk currently on this dentry? */
379 ino_count = READ_ONCE(ino->count) + 1;
380 if (d_count(dentry) > ino_count)
384 if (!autofs_tree_busy(mnt, dentry, timeout, how))
387 * Case 3: pseudo direct mount, expire individual leaves
391 struct dentry *expired;
393 /* Not a forced expire? */
394 if (!(how & AUTOFS_EXP_FORCED)) {
395 /* ref-walk currently on this dentry? */
396 ino_count = READ_ONCE(ino->count) + 1;
397 if (d_count(dentry) > ino_count)
401 expired = autofs_check_leaves(mnt, dentry, timeout, how);
403 if (expired == dentry)
412 * Find an eligible tree to time-out
413 * A tree is eligible if :-
414 * - it is unused by any user process
415 * - it has been unused for exp_timeout time
417 static struct dentry *autofs_expire_indirect(struct super_block *sb,
418 struct vfsmount *mnt,
419 struct autofs_sb_info *sbi,
422 unsigned long timeout;
423 struct dentry *root = sb->s_root;
424 struct dentry *dentry;
425 struct dentry *expired;
426 struct dentry *found;
427 struct autofs_info *ino;
432 timeout = sbi->exp_timeout;
435 while ((dentry = get_next_positive_subdir(dentry, root))) {
436 spin_lock(&sbi->fs_lock);
437 ino = autofs_dentry_ino(dentry);
438 if (ino->flags & AUTOFS_INF_WANT_EXPIRE) {
439 spin_unlock(&sbi->fs_lock);
442 spin_unlock(&sbi->fs_lock);
444 expired = should_expire(dentry, mnt, timeout, how);
448 spin_lock(&sbi->fs_lock);
449 ino = autofs_dentry_ino(expired);
450 ino->flags |= AUTOFS_INF_WANT_EXPIRE;
451 spin_unlock(&sbi->fs_lock);
454 /* Make sure a reference is not taken on found if
455 * things have changed.
457 how &= ~AUTOFS_EXP_LEAVES;
458 found = should_expire(expired, mnt, timeout, how);
459 if (found != expired) { // something has changed, continue
464 if (expired != dentry)
467 spin_lock(&sbi->fs_lock);
470 spin_lock(&sbi->fs_lock);
471 ino->flags &= ~AUTOFS_INF_WANT_EXPIRE;
472 spin_unlock(&sbi->fs_lock);
473 if (expired != dentry)
479 pr_debug("returning %p %pd\n", expired, expired);
480 ino->flags |= AUTOFS_INF_EXPIRING;
481 init_completion(&ino->expire_complete);
482 spin_unlock(&sbi->fs_lock);
486 int autofs_expire_wait(const struct path *path, int rcu_walk)
488 struct dentry *dentry = path->dentry;
489 struct autofs_sb_info *sbi = autofs_sbi(dentry->d_sb);
490 struct autofs_info *ino = autofs_dentry_ino(dentry);
494 /* Block on any pending expire */
495 if (!(ino->flags & AUTOFS_INF_WANT_EXPIRE))
501 spin_lock(&sbi->fs_lock);
502 state = ino->flags & (AUTOFS_INF_WANT_EXPIRE | AUTOFS_INF_EXPIRING);
503 if (state == AUTOFS_INF_WANT_EXPIRE) {
504 spin_unlock(&sbi->fs_lock);
506 * Possibly being selected for expire, wait until
507 * it's selected or not.
509 schedule_timeout_uninterruptible(HZ/10);
512 if (state & AUTOFS_INF_EXPIRING) {
513 spin_unlock(&sbi->fs_lock);
515 pr_debug("waiting for expire %p name=%pd\n", dentry, dentry);
517 status = autofs_wait(sbi, path, NFY_NONE);
518 wait_for_completion(&ino->expire_complete);
520 pr_debug("expire done status=%d\n", status);
522 if (d_unhashed(dentry))
527 spin_unlock(&sbi->fs_lock);
532 /* Perform an expiry operation */
533 int autofs_expire_run(struct super_block *sb,
534 struct vfsmount *mnt,
535 struct autofs_sb_info *sbi,
536 struct autofs_packet_expire __user *pkt_p)
538 struct autofs_packet_expire pkt;
539 struct autofs_info *ino;
540 struct dentry *dentry;
543 memset(&pkt, 0, sizeof(pkt));
545 pkt.hdr.proto_version = sbi->version;
546 pkt.hdr.type = autofs_ptype_expire;
548 dentry = autofs_expire_indirect(sb, mnt, sbi, 0);
552 pkt.len = dentry->d_name.len;
553 memcpy(pkt.name, dentry->d_name.name, pkt.len);
554 pkt.name[pkt.len] = '\0';
556 if (copy_to_user(pkt_p, &pkt, sizeof(struct autofs_packet_expire)))
559 spin_lock(&sbi->fs_lock);
560 ino = autofs_dentry_ino(dentry);
561 /* avoid rapid-fire expire attempts if expiry fails */
562 ino->last_used = jiffies;
563 ino->flags &= ~(AUTOFS_INF_EXPIRING|AUTOFS_INF_WANT_EXPIRE);
564 complete_all(&ino->expire_complete);
565 spin_unlock(&sbi->fs_lock);
572 int autofs_do_expire_multi(struct super_block *sb, struct vfsmount *mnt,
573 struct autofs_sb_info *sbi, unsigned int how)
575 struct dentry *dentry;
578 if (autofs_type_trigger(sbi->type))
579 dentry = autofs_expire_direct(sb, mnt, sbi, how);
581 dentry = autofs_expire_indirect(sb, mnt, sbi, how);
584 struct autofs_info *ino = autofs_dentry_ino(dentry);
585 const struct path path = { .mnt = mnt, .dentry = dentry };
587 /* This is synchronous because it makes the daemon a
590 ret = autofs_wait(sbi, &path, NFY_EXPIRE);
592 spin_lock(&sbi->fs_lock);
593 /* avoid rapid-fire expire attempts if expiry fails */
594 ino->last_used = jiffies;
595 ino->flags &= ~(AUTOFS_INF_EXPIRING|AUTOFS_INF_WANT_EXPIRE);
596 complete_all(&ino->expire_complete);
597 spin_unlock(&sbi->fs_lock);
605 * Call repeatedly until it returns -EAGAIN, meaning there's nothing
608 int autofs_expire_multi(struct super_block *sb, struct vfsmount *mnt,
609 struct autofs_sb_info *sbi, int __user *arg)
611 unsigned int how = 0;
613 if (arg && get_user(how, arg))
616 return autofs_do_expire_multi(sb, mnt, sbi, how);