Skip to content

Commit

Permalink
Suspend file system after VFS_MOUNT() and before taking mnt_updating.
Browse files Browse the repository at this point in the history
Prevents deadlock against concurrent unmounts of layered file systems.
  • Loading branch information
hannken authored and hannken committed Jul 8, 2022
1 parent 42ef150 commit ba6f7f8
Showing 1 changed file with 16 additions and 14 deletions.
30 changes: 16 additions & 14 deletions sys/kern/vfs_mount.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* $NetBSD: vfs_mount.c,v 1.93 2022/04/09 23:38:33 riastradh Exp $ */
/* $NetBSD: vfs_mount.c,v 1.94 2022/07/08 07:43:19 hannken Exp $ */

/*-
* Copyright (c) 1997-2020 The NetBSD Foundation, Inc.
Expand Down Expand Up @@ -67,7 +67,7 @@
*/

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: vfs_mount.c,v 1.93 2022/04/09 23:38:33 riastradh Exp $");
__KERNEL_RCSID(0, "$NetBSD: vfs_mount.c,v 1.94 2022/07/08 07:43:19 hannken Exp $");

#include <sys/param.h>
#include <sys/kernel.h>
Expand Down Expand Up @@ -773,12 +773,20 @@ mount_domount(struct lwp *l, vnode_t **vpp, struct vfsops *vfsops,
*/
mp->mnt_flag = flags & (MNT_BASIC_FLAGS | MNT_FORCE | MNT_IGNORE);

mutex_enter(mp->mnt_updating);
error = VFS_MOUNT(mp, path, data, data_len);
mp->mnt_flag &= ~MNT_OP_FLAGS;

if (error != 0)
goto err_unmounted;
if (error != 0) {
vfs_rele(mp);
return error;
}

/* Suspend new file system before taking mnt_updating. */
do {
error2 = vfs_suspend(mp, 0);
} while (error2 == EINTR || error2 == ERESTART);
KASSERT(error2 == 0 || error2 == EOPNOTSUPP);
mutex_enter(mp->mnt_updating);

/*
* Validate and prepare the mount point.
Expand Down Expand Up @@ -823,6 +831,8 @@ mount_domount(struct lwp *l, vnode_t **vpp, struct vfsops *vfsops,

mount_checkdirs(vp);
mutex_exit(mp->mnt_updating);
if (error2 == 0)
vfs_resume(mp);

/* Hold an additional reference to the mount across VFS_START(). */
vfs_ref(mp);
Expand All @@ -840,19 +850,11 @@ mount_domount(struct lwp *l, vnode_t **vpp, struct vfsops *vfsops,
return error;

err_mounted:
do {
error2 = vfs_suspend(mp, 0);
} while (error2 == EINTR || error2 == ERESTART);
KASSERT(error2 == 0 || error2 == EOPNOTSUPP);

if (VFS_UNMOUNT(mp, MNT_FORCE) != 0)
panic("Unmounting fresh file system failed");

mutex_exit(mp->mnt_updating);
if (error2 == 0)
vfs_resume(mp);

err_unmounted:
mutex_exit(mp->mnt_updating);
vfs_rele(mp);

return error;
Expand Down

0 comments on commit ba6f7f8

Please sign in to comment.