CVS commit: src/sys/dev/dkwedge

2024-04-06 Thread Juergen Hannken-Illjes
Module Name:src
Committed By:   hannken
Date:   Sat Apr  6 09:51:34 UTC 2024

Modified Files:
src/sys/dev/dkwedge: dkwedge_tos.c

Log Message:
Use "%zu" for type "size_t".  Kernel ALL/i386 compiles again.


To generate a diff of this commit:
cvs rdiff -u -r1.1 -r1.2 src/sys/dev/dkwedge/dkwedge_tos.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/dev/dkwedge/dkwedge_tos.c
diff -u src/sys/dev/dkwedge/dkwedge_tos.c:1.1 src/sys/dev/dkwedge/dkwedge_tos.c:1.2
--- src/sys/dev/dkwedge/dkwedge_tos.c:1.1	Tue Apr  2 22:30:03 2024
+++ src/sys/dev/dkwedge/dkwedge_tos.c	Sat Apr  6 09:51:34 2024
@@ -1,4 +1,4 @@
-/* $NetBSD: dkwedge_tos.c,v 1.1 2024/04/02 22:30:03 charlotte Exp $ */
+/* $NetBSD: dkwedge_tos.c,v 1.2 2024/04/06 09:51:34 hannken Exp $ */
 
 /*
  * Copyright (c) 2024 The NetBSD Foundation, Inc.
@@ -150,7 +150,7 @@ dkwedge_discover_tos(struct disk *pdk, s
 		safe_type[TOS_PART_TYPE_LEN] = '\0';
 
 		/* Finally, make the wedge. */
-		snprintf(dkw.dkw_wname, sizeof(dkw.dkw_wname), "ATARI_%s_%02lu",
+		snprintf(dkw.dkw_wname, sizeof(dkw.dkw_wname), "ATARI_%s_%02zu",
 		safe_type, i);
 		dkw.dkw_offset = be32toh(trs->parts[i].offset);
 		dkw.dkw_size = be32toh(trs->parts[i].size);



CVS commit: src/sys/dev/dkwedge

2024-04-06 Thread Juergen Hannken-Illjes
Module Name:src
Committed By:   hannken
Date:   Sat Apr  6 09:51:34 UTC 2024

Modified Files:
src/sys/dev/dkwedge: dkwedge_tos.c

Log Message:
Use "%zu" for type "size_t".  Kernel ALL/i386 compiles again.


To generate a diff of this commit:
cvs rdiff -u -r1.1 -r1.2 src/sys/dev/dkwedge/dkwedge_tos.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/sys/dev/dkwedge

2024-02-26 Thread Charlotte Koch
Module Name:src
Committed By:   charlotte
Date:   Mon Feb 26 21:55:05 UTC 2024

Modified Files:
src/sys/dev/dkwedge: dkwedge_mbr.c

Log Message:
In case of trouble reading an MBR, make sure error field is set before 
reporting it


To generate a diff of this commit:
cvs rdiff -u -r1.12 -r1.13 src/sys/dev/dkwedge/dkwedge_mbr.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/dev/dkwedge/dkwedge_mbr.c
diff -u src/sys/dev/dkwedge/dkwedge_mbr.c:1.12 src/sys/dev/dkwedge/dkwedge_mbr.c:1.13
--- src/sys/dev/dkwedge/dkwedge_mbr.c:1.12	Sat Apr 11 16:00:34 2020
+++ src/sys/dev/dkwedge/dkwedge_mbr.c	Mon Feb 26 21:55:05 2024
@@ -1,4 +1,4 @@
-/*	$NetBSD: dkwedge_mbr.c,v 1.12 2020/04/11 16:00:34 jdolecek Exp $	*/
+/*	$NetBSD: dkwedge_mbr.c,v 1.13 2024/02/26 21:55:05 charlotte Exp $	*/
 
 /*-
  * Copyright (c) 2004 The NetBSD Foundation, Inc.
@@ -34,7 +34,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: dkwedge_mbr.c,v 1.12 2020/04/11 16:00:34 jdolecek Exp $");
+__KERNEL_RCSID(0, "$NetBSD: dkwedge_mbr.c,v 1.13 2024/02/26 21:55:05 charlotte Exp $");
 
 #include 
 #include 
@@ -90,9 +90,9 @@ getparts(mbr_args_t *a, uint32_t off, ui
 
 	error = dkwedge_read(a->pdk, a->vp, off, a->bp->b_data, a->secsize);
 	if (error) {
+		a->error = error;
 		aprint_error("%s: unable to read MBR @ %u/%u, "
 		"error = %d\n", a->pdk->dk_name, off, a->secsize, a->error);
-		a->error = error;
 		return;
 	}
 



CVS commit: src/sys/dev/dkwedge

2024-02-26 Thread Charlotte Koch
Module Name:src
Committed By:   charlotte
Date:   Mon Feb 26 21:55:05 UTC 2024

Modified Files:
src/sys/dev/dkwedge: dkwedge_mbr.c

Log Message:
In case of trouble reading an MBR, make sure error field is set before 
reporting it


To generate a diff of this commit:
cvs rdiff -u -r1.12 -r1.13 src/sys/dev/dkwedge/dkwedge_mbr.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/sys/dev/dkwedge

2023-05-22 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Mon May 22 15:00:17 UTC 2023

Modified Files:
src/sys/dev/dkwedge: dk.c

Log Message:
dk(4): Add locking notes.


To generate a diff of this commit:
cvs rdiff -u -r1.170 -r1.171 src/sys/dev/dkwedge/dk.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/sys/dev/dkwedge

2023-05-22 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Mon May 22 14:59:58 UTC 2023

Modified Files:
src/sys/dev/dkwedge: dk.c

Log Message:
dk(4): Strengthen preconditions of various devsw operations.

These can only happen between dkopen and dkclose, so there's no need
to test -- we can assert instead that the wedge exists and is fully
initialized.


To generate a diff of this commit:
cvs rdiff -u -r1.168 -r1.169 src/sys/dev/dkwedge/dk.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/sys/dev/dkwedge

2023-05-22 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Mon May 22 14:59:50 UTC 2023

Modified Files:
src/sys/dev/dkwedge: dk.c

Log Message:
dk(4): Strengthen dkclose preconditions.

Like dkopen, except it is possible for this to be called after the
wedge has transitioned to dying.

XXX sc_state read here races with sc_state write in dkwedge_detach.
Could change this to atomic_load/store.


To generate a diff of this commit:
cvs rdiff -u -r1.167 -r1.168 src/sys/dev/dkwedge/dk.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/sys/dev/dkwedge

2023-05-22 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Mon May 22 14:59:42 UTC 2023

Modified Files:
src/sys/dev/dkwedge: dk.c

Log Message:
dk(4): Strengthen dkopen preconditions.

This cannot be called before dkwedge_attach for the same unit
returns, so sc->sc_dev is guaranteed to be set to a nonnull device_t
and the state is guaranteed not to be larval.

And this cannot be called concurrently with dkwedge_detach, or after
dkwedge_detach does vdevgone until another wedge with the same number
is attached (which can't happen until dkwedge_detach completes), so
the state is guaranteed not to be dying or dead.

Hence sc->sc_dev != NULL and sc->sc_state == DKW_STATE_RUNNING.


To generate a diff of this commit:
cvs rdiff -u -r1.166 -r1.167 src/sys/dev/dkwedge/dk.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/dev/dkwedge/dk.c
diff -u src/sys/dev/dkwedge/dk.c:1.166 src/sys/dev/dkwedge/dk.c:1.167
--- src/sys/dev/dkwedge/dk.c:1.166	Mon May 22 14:59:34 2023
+++ src/sys/dev/dkwedge/dk.c	Mon May 22 14:59:42 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: dk.c,v 1.166 2023/05/22 14:59:34 riastradh Exp $	*/
+/*	$NetBSD: dk.c,v 1.167 2023/05/22 14:59:42 riastradh Exp $	*/
 
 /*-
  * Copyright (c) 2004, 2005, 2006, 2007 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: dk.c,v 1.166 2023/05/22 14:59:34 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: dk.c,v 1.167 2023/05/22 14:59:42 riastradh Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_dkwedge.h"
@@ -1277,8 +1277,8 @@ dkopen(dev_t dev, int flags, int fmt, st
 
 	if (sc == NULL)
 		return ENXIO;
-	if (sc->sc_state != DKW_STATE_RUNNING)
-		return ENXIO;
+	KASSERT(sc->sc_dev != NULL);
+	KASSERT(sc->sc_state == DKW_STATE_RUNNING);
 
 	/*
 	 * We go through a complicated little dance to only open the parent



CVS commit: src/sys/dev/dkwedge

2023-05-22 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Mon May 22 14:59:34 UTC 2023

Modified Files:
src/sys/dev/dkwedge: dk.c

Log Message:
dk(4): Prevent race between dkwedge_get_parent_name and wedge detach.

Still races with parent detach but maybe this is better.

XXX Maybe we should ditch dkwedge_get_parent_name -- it's used only
by rf_containsboot, which kinda suggests it shouldn't exist.


To generate a diff of this commit:
cvs rdiff -u -r1.165 -r1.166 src/sys/dev/dkwedge/dk.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/dev/dkwedge/dk.c
diff -u src/sys/dev/dkwedge/dk.c:1.165 src/sys/dev/dkwedge/dk.c:1.166
--- src/sys/dev/dkwedge/dk.c:1.165	Mon May 22 14:59:25 2023
+++ src/sys/dev/dkwedge/dk.c	Mon May 22 14:59:34 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: dk.c,v 1.165 2023/05/22 14:59:25 riastradh Exp $	*/
+/*	$NetBSD: dk.c,v 1.166 2023/05/22 14:59:34 riastradh Exp $	*/
 
 /*-
  * Copyright (c) 2004, 2005, 2006, 2007 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: dk.c,v 1.165 2023/05/22 14:59:25 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: dk.c,v 1.166 2023/05/22 14:59:34 riastradh Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_dkwedge.h"
@@ -1210,6 +1210,16 @@ dkwedge_lookup(dev_t dev)
 	return device_lookup_private(_cd, minor(dev));
 }
 
+static struct dkwedge_softc *
+dkwedge_lookup_acquire(dev_t dev)
+{
+	device_t dv = device_lookup_acquire(_cd, minor(dev));
+
+	if (dv == NULL)
+		return NULL;
+	return device_private(dv);
+}
+
 static int
 dk_open_parent(dev_t dev, int mode, struct vnode **vpp)
 {
@@ -1925,8 +1935,11 @@ dkwedge_get_parent_name(dev_t dev)
 
 	if (major(dev) != bmaj && major(dev) != cmaj)
 		return NULL;
-	struct dkwedge_softc *sc = dkwedge_lookup(dev);
+
+	struct dkwedge_softc *const sc = dkwedge_lookup_acquire(dev);
 	if (sc == NULL)
 		return NULL;
-	return sc->sc_parent->dk_name;
+	const char *const name = sc->sc_parent->dk_name;
+	device_release(sc->sc_dev);
+	return name;
 }



CVS commit: src/sys/dev/dkwedge

2023-05-22 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Mon May 22 14:59:34 UTC 2023

Modified Files:
src/sys/dev/dkwedge: dk.c

Log Message:
dk(4): Prevent race between dkwedge_get_parent_name and wedge detach.

Still races with parent detach but maybe this is better.

XXX Maybe we should ditch dkwedge_get_parent_name -- it's used only
by rf_containsboot, which kinda suggests it shouldn't exist.


To generate a diff of this commit:
cvs rdiff -u -r1.165 -r1.166 src/sys/dev/dkwedge/dk.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/sys/dev/dkwedge

2023-05-22 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Mon May 22 14:59:25 UTC 2023

Modified Files:
src/sys/dev/dkwedge: dk.c

Log Message:
dk(4): Split unsafe lookups into safe subroutines and unsafe wrappers.

No functional change intended.

Eventually we should adjust the callers to use the safe subroutines
instead and device_release when done.


To generate a diff of this commit:
cvs rdiff -u -r1.164 -r1.165 src/sys/dev/dkwedge/dk.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/sys/dev/dkwedge

2023-05-22 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Mon May 22 14:59:17 UTC 2023

Modified Files:
src/sys/dev/dkwedge: dk.c

Log Message:
dk(4): Don't hold lock around uiomove in dkwedge_list.

Instead, hold a device reference.  dkwedge_detach will not run until
the device reference is released.


To generate a diff of this commit:
cvs rdiff -u -r1.163 -r1.164 src/sys/dev/dkwedge/dk.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/sys/dev/dkwedge

2023-05-22 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Mon May 22 14:59:08 UTC 2023

Modified Files:
src/sys/dev/dkwedge: dk.c

Log Message:
dk(4): Skip larval wedges in various lookup routines.

These have not yet finished a concurent dkwedge_attach, so there's
nothing we can safely do with them.  Just pretend they don't exist --
as if we had arrived at the lookup a moment earlier.


To generate a diff of this commit:
cvs rdiff -u -r1.162 -r1.163 src/sys/dev/dkwedge/dk.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/dev/dkwedge/dk.c
diff -u src/sys/dev/dkwedge/dk.c:1.162 src/sys/dev/dkwedge/dk.c:1.163
--- src/sys/dev/dkwedge/dk.c:1.162	Mon May 22 14:58:59 2023
+++ src/sys/dev/dkwedge/dk.c	Mon May 22 14:59:07 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: dk.c,v 1.162 2023/05/22 14:58:59 riastradh Exp $	*/
+/*	$NetBSD: dk.c,v 1.163 2023/05/22 14:59:07 riastradh Exp $	*/
 
 /*-
  * Copyright (c) 2004, 2005, 2006, 2007 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: dk.c,v 1.162 2023/05/22 14:58:59 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: dk.c,v 1.163 2023/05/22 14:59:07 riastradh Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_dkwedge.h"
@@ -849,7 +849,7 @@ dkwedge_list(struct disk *pdk, struct dk
 		if (uio.uio_resid < sizeof(dkw))
 			break;
 
-		if (sc->sc_state != DKW_STATE_RUNNING)
+		if (sc->sc_dev == NULL)
 			continue;
 
 		strlcpy(dkw.dkw_devname, device_xname(sc->sc_dev),
@@ -882,7 +882,7 @@ dkwedge_find_by_wname(const char *wname)
 
 	rw_enter(_lock, RW_READER);
 	for (i = 0; i < ndkwedges; i++) {
-		if ((sc = dkwedges[i]) == NULL)
+		if ((sc = dkwedges[i]) == NULL || sc->sc_dev == NULL)
 			continue;
 		if (strcmp(sc->sc_wname, wname) == 0) {
 			if (dv != NULL) {
@@ -906,7 +906,7 @@ dkwedge_find_by_parent(const char *name,
 	rw_enter(_lock, RW_READER);
 	for (; *i < (size_t)ndkwedges; (*i)++) {
 		struct dkwedge_softc *sc;
-		if ((sc = dkwedges[*i]) == NULL)
+		if ((sc = dkwedges[*i]) == NULL || sc->sc_dev == NULL)
 			continue;
 		if (strcmp(sc->sc_parent->dk_name, name) != 0)
 			continue;
@@ -925,7 +925,7 @@ dkwedge_print_wnames(void)
 
 	rw_enter(_lock, RW_READER);
 	for (i = 0; i < ndkwedges; i++) {
-		if ((sc = dkwedges[i]) == NULL)
+		if ((sc = dkwedges[i]) == NULL || sc->sc_dev == NULL)
 			continue;
 		printf(" wedge:%s", sc->sc_wname);
 	}
@@ -1844,7 +1844,7 @@ dkwedge_find_partition(device_t parent, 
 
 	rw_enter(_lock, RW_READER);
 	for (i = 0; i < ndkwedges; i++) {
-		if ((sc = dkwedges[i]) == NULL)
+		if ((sc = dkwedges[i]) == NULL || sc->sc_dev == NULL)
 			continue;
 		if (strcmp(sc->sc_parent->dk_name, device_xname(parent)) == 0 &&
 		sc->sc_offset == startblk &&



CVS commit: src/sys/dev/dkwedge

2023-05-22 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Mon May 22 14:58:59 UTC 2023

Modified Files:
src/sys/dev/dkwedge: dk.c

Log Message:
dk(4): Simplify dkwedge_delall by detaching directly.

No need for O(n^2) algorithm and potentially racy lookups -- not that
n is large enough for n^2 to matter, but the mechanism is simpler
this way.


To generate a diff of this commit:
cvs rdiff -u -r1.161 -r1.162 src/sys/dev/dkwedge/dk.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/dev/dkwedge/dk.c
diff -u src/sys/dev/dkwedge/dk.c:1.161 src/sys/dev/dkwedge/dk.c:1.162
--- src/sys/dev/dkwedge/dk.c:1.161	Mon May 22 14:58:50 2023
+++ src/sys/dev/dkwedge/dk.c	Mon May 22 14:58:59 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: dk.c,v 1.161 2023/05/22 14:58:50 riastradh Exp $	*/
+/*	$NetBSD: dk.c,v 1.162 2023/05/22 14:58:59 riastradh Exp $	*/
 
 /*-
  * Copyright (c) 2004, 2005, 2006, 2007 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: dk.c,v 1.161 2023/05/22 14:58:50 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: dk.c,v 1.162 2023/05/22 14:58:59 riastradh Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_dkwedge.h"
@@ -781,7 +781,6 @@ dkwedge_delidle(struct disk *pdk)
 static void
 dkwedge_delall1(struct disk *pdk, bool idleonly)
 {
-	struct dkwedge_info dkw;
 	struct dkwedge_softc *sc;
 	int flags;
 
@@ -793,8 +792,18 @@ dkwedge_delall1(struct disk *pdk, bool i
 		mutex_enter(>dk_rawlock); /* for sc->sc_dk.dk_openmask */
 		mutex_enter(>dk_openlock);
 		LIST_FOREACH(sc, >dk_wedges, sc_plink) {
-			if (!idleonly || sc->sc_dk.dk_openmask == 0)
+			/*
+			 * Wedge is not yet created.  This is a race --
+			 * it may as well have been added just after we
+			 * deleted all the wedges, so pretend it's not
+			 * here yet.
+			 */
+			if (sc->sc_dev == NULL)
+continue;
+			if (!idleonly || sc->sc_dk.dk_openmask == 0) {
+device_acquire(sc->sc_dev);
 break;
+			}
 		}
 		if (sc == NULL) {
 			KASSERT(idleonly || pdk->dk_nwedges == 0);
@@ -802,12 +811,9 @@ dkwedge_delall1(struct disk *pdk, bool i
 			mutex_exit(>dk_rawlock);
 			return;
 		}
-		strlcpy(dkw.dkw_parent, pdk->dk_name, sizeof(dkw.dkw_parent));
-		strlcpy(dkw.dkw_devname, device_xname(sc->sc_dev),
-		sizeof(dkw.dkw_devname));
 		mutex_exit(>dk_openlock);
 		mutex_exit(>dk_rawlock);
-		(void) dkwedge_del1(, flags);
+		(void)config_detach_release(sc->sc_dev, flags);
 	}
 }
 



CVS commit: src/sys/dev/dkwedge

2023-05-22 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Mon May 22 14:58:59 UTC 2023

Modified Files:
src/sys/dev/dkwedge: dk.c

Log Message:
dk(4): Simplify dkwedge_delall by detaching directly.

No need for O(n^2) algorithm and potentially racy lookups -- not that
n is large enough for n^2 to matter, but the mechanism is simpler
this way.


To generate a diff of this commit:
cvs rdiff -u -r1.161 -r1.162 src/sys/dev/dkwedge/dk.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/sys/dev/dkwedge

2023-05-22 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Mon May 22 14:58:51 UTC 2023

Modified Files:
src/sys/dev/dkwedge: dk.c

Log Message:
dk(4): Use device_lookup_private for dkwedge_lookup.

No longer necessary to go through the dkwedges array.

Currently device_lookup_private still involves touching other global
locks, but that will change eventually to a lockless pserialized fast
path.


To generate a diff of this commit:
cvs rdiff -u -r1.160 -r1.161 src/sys/dev/dkwedge/dk.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/sys/dev/dkwedge

2023-05-22 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Mon May 22 15:00:06 UTC 2023

Modified Files:
src/sys/dev/dkwedge: dk.c

Log Message:
dk(4): Explain why no need for device reference in dksize, dkdump.


To generate a diff of this commit:
cvs rdiff -u -r1.169 -r1.170 src/sys/dev/dkwedge/dk.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/dev/dkwedge/dk.c
diff -u src/sys/dev/dkwedge/dk.c:1.169 src/sys/dev/dkwedge/dk.c:1.170
--- src/sys/dev/dkwedge/dk.c:1.169	Mon May 22 14:59:58 2023
+++ src/sys/dev/dkwedge/dk.c	Mon May 22 15:00:06 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: dk.c,v 1.169 2023/05/22 14:59:58 riastradh Exp $	*/
+/*	$NetBSD: dk.c,v 1.170 2023/05/22 15:00:06 riastradh Exp $	*/
 
 /*-
  * Copyright (c) 2004, 2005, 2006, 2007 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: dk.c,v 1.169 2023/05/22 14:59:58 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: dk.c,v 1.170 2023/05/22 15:00:06 riastradh Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_dkwedge.h"
@@ -1824,6 +1824,11 @@ dkdiscard(dev_t dev, off_t pos, off_t le
 static int
 dksize(dev_t dev)
 {
+	/*
+	 * Don't bother taking a reference because this is only used
+	 * either (a) while the device is open (for swap), or (b) while
+	 * any multiprocessing is quiescent (for crash dumps).
+	 */
 	struct dkwedge_softc *sc = dkwedge_lookup(dev);
 	uint64_t p_size;
 	int rv = -1;
@@ -1855,6 +1860,10 @@ dksize(dev_t dev)
 static int
 dkdump(dev_t dev, daddr_t blkno, void *va, size_t size)
 {
+	/*
+	 * Don't bother taking a reference because this is only used
+	 * while any multiprocessing is quiescent.
+	 */
 	struct dkwedge_softc *sc = dkwedge_lookup(dev);
 	const struct bdevsw *bdev;
 	uint64_t p_size, p_offset;



CVS commit: src/sys/dev/dkwedge

2023-05-22 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Mon May 22 15:00:06 UTC 2023

Modified Files:
src/sys/dev/dkwedge: dk.c

Log Message:
dk(4): Explain why no need for device reference in dksize, dkdump.


To generate a diff of this commit:
cvs rdiff -u -r1.169 -r1.170 src/sys/dev/dkwedge/dk.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/sys/dev/dkwedge

2023-05-22 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Mon May 22 15:00:17 UTC 2023

Modified Files:
src/sys/dev/dkwedge: dk.c

Log Message:
dk(4): Add locking notes.


To generate a diff of this commit:
cvs rdiff -u -r1.170 -r1.171 src/sys/dev/dkwedge/dk.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/dev/dkwedge/dk.c
diff -u src/sys/dev/dkwedge/dk.c:1.170 src/sys/dev/dkwedge/dk.c:1.171
--- src/sys/dev/dkwedge/dk.c:1.170	Mon May 22 15:00:06 2023
+++ src/sys/dev/dkwedge/dk.c	Mon May 22 15:00:17 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: dk.c,v 1.170 2023/05/22 15:00:06 riastradh Exp $	*/
+/*	$NetBSD: dk.c,v 1.171 2023/05/22 15:00:17 riastradh Exp $	*/
 
 /*-
  * Copyright (c) 2004, 2005, 2006, 2007 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: dk.c,v 1.170 2023/05/22 15:00:06 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: dk.c,v 1.171 2023/05/22 15:00:17 riastradh Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_dkwedge.h"
@@ -70,29 +70,65 @@ typedef enum {
 	DKW_STATE_DEAD		= 666
 } dkwedge_state_t;
 
+/*
+ * Lock order:
+ *
+ *	sc->sc_dk.dk_openlock
+ *	=> sc->sc_parent->dk_rawlock
+ *	=> sc->sc_parent->dk_openlock
+ *	=> dkwedges_lock
+ *	=> sc->sc_sizelock
+ *
+ * Locking notes:
+ *
+ *	W	dkwedges_lock
+ *	D	device reference
+ *	O	sc->sc_dk.dk_openlock
+ *	P	sc->sc_parent->dk_openlock
+ *	R	sc->sc_parent->dk_rawlock
+ *	S	sc->sc_sizelock
+ *	I	sc->sc_iolock
+ *	$	stable after initialization
+ *	1	used only by a single thread
+ *
+ * x means both x and y must be held to write (with a write lock if
+ * one is rwlock), and either x or y must be held to read.
+ */
+
 struct dkwedge_softc {
-	device_t	sc_dev;	/* pointer to our pseudo-device */
-	struct cfdata	sc_cfdata;	/* our cfdata structure */
-	uint8_t		sc_wname[128];	/* wedge name (Unicode, UTF-8) */
+	device_t	sc_dev;	/* P: pointer to our pseudo-device */
+		/* sc_dev is also stable while device is referenced */
+	struct cfdata	sc_cfdata;	/* 1: our cfdata structure */
+	uint8_t		sc_wname[128];	/* $: wedge name (Unicode, UTF-8) */
 
 	dkwedge_state_t sc_state;	/* state this wedge is in */
+		/* stable while device is referenced */
+		/* used only in assertions when stable, and in dump in ddb */
 
-	struct disk	*sc_parent;	/* parent disk */
-	daddr_t		sc_offset;	/* LBA offset of wedge in parent */
+	struct disk	*sc_parent;	/* $: parent disk */
+		/* P: sc_parent->dk_openmask */
+		/* P: sc_parent->dk_nwedges */
+		/* P: sc_parent->dk_wedges */
+		/* R: sc_parent->dk_rawopens */
+		/* R: sc_parent->dk_rawvp (also stable while wedge is open) */
+	daddr_t		sc_offset;	/* $: LBA offset of wedge in parent */
 	krwlock_t	sc_sizelock;
-	uint64_t	sc_size;	/* size of wedge in blocks */
-	char		sc_ptype[32];	/* partition type */
-	dev_t		sc_pdev;	/* cached parent's dev_t */
-	/* link on parent's wedge list */
+	uint64_t	sc_size;	/* S: size of wedge in blocks */
+	char		sc_ptype[32];	/* $: partition type */
+	dev_t		sc_pdev;	/* $: cached parent's dev_t */
+	/* P: link on parent's wedge list */
 	LIST_ENTRY(dkwedge_softc) sc_plink;
 
 	struct disk	sc_dk;		/* our own disk structure */
-	struct bufq_state *sc_bufq;	/* buffer queue */
-	struct callout	sc_restart_ch;	/* callout to restart I/O */
+		/* O: sc_dk.dk_bopenmask */
+		/* O: sc_dk.dk_copenmask */
+		/* O: sc_dk.dk_openmask */
+	struct bufq_state *sc_bufq;	/* $: buffer queue */
+	struct callout	sc_restart_ch;	/* I: callout to restart I/O */
 
 	kmutex_t	sc_iolock;
-	bool		sc_iostop;	/* don't schedule restart */
-	int		sc_mode;	/* parent open mode */
+	bool		sc_iostop;	/* I: don't schedule restart */
+	int		sc_mode;	/* O: parent open mode */
 };
 
 static int	dkwedge_match(device_t, cfdata_t, void *);



CVS commit: src/sys/dev/dkwedge

2023-05-22 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Mon May 22 14:59:58 UTC 2023

Modified Files:
src/sys/dev/dkwedge: dk.c

Log Message:
dk(4): Strengthen preconditions of various devsw operations.

These can only happen between dkopen and dkclose, so there's no need
to test -- we can assert instead that the wedge exists and is fully
initialized.


To generate a diff of this commit:
cvs rdiff -u -r1.168 -r1.169 src/sys/dev/dkwedge/dk.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/dev/dkwedge/dk.c
diff -u src/sys/dev/dkwedge/dk.c:1.168 src/sys/dev/dkwedge/dk.c:1.169
--- src/sys/dev/dkwedge/dk.c:1.168	Mon May 22 14:59:50 2023
+++ src/sys/dev/dkwedge/dk.c	Mon May 22 14:59:58 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: dk.c,v 1.168 2023/05/22 14:59:50 riastradh Exp $	*/
+/*	$NetBSD: dk.c,v 1.169 2023/05/22 14:59:58 riastradh Exp $	*/
 
 /*-
  * Copyright (c) 2004, 2005, 2006, 2007 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: dk.c,v 1.168 2023/05/22 14:59:50 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: dk.c,v 1.169 2023/05/22 14:59:58 riastradh Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_dkwedge.h"
@@ -1477,6 +1477,7 @@ dkstrategy(struct buf *bp)
 	uint64_t p_size, p_offset;
 
 	KASSERT(sc != NULL);
+	KASSERT(sc->sc_dev != NULL);
 	KASSERT(sc->sc_state != DKW_STATE_LARVAL);
 	KASSERT(sc->sc_state != DKW_STATE_DEAD);
 	KASSERT(sc->sc_parent->dk_rawvp != NULL);
@@ -1605,6 +1606,9 @@ dkiodone(struct buf *bp)
 	struct buf *obp = bp->b_private;
 	struct dkwedge_softc *sc = dkwedge_lookup(obp->b_dev);
 
+	KASSERT(sc != NULL);
+	KASSERT(sc->sc_dev != NULL);
+
 	if (bp->b_error != 0)
 		obp->b_error = bp->b_error;
 	obp->b_resid = bp->b_resid;
@@ -1646,6 +1650,9 @@ dkminphys(struct buf *bp)
 	struct dkwedge_softc *sc = dkwedge_lookup(bp->b_dev);
 	dev_t dev;
 
+	KASSERT(sc != NULL);
+	KASSERT(sc->sc_dev != NULL);
+
 	dev = bp->b_dev;
 	bp->b_dev = sc->sc_pdev;
 	if (sc->sc_parent->dk_driver && sc->sc_parent->dk_driver->d_minphys)
@@ -1666,6 +1673,7 @@ dkread(dev_t dev, struct uio *uio, int f
 	struct dkwedge_softc *sc __diagused = dkwedge_lookup(dev);
 
 	KASSERT(sc != NULL);
+	KASSERT(sc->sc_dev != NULL);
 	KASSERT(sc->sc_state != DKW_STATE_LARVAL);
 	KASSERT(sc->sc_state != DKW_STATE_DEAD);
 
@@ -1683,6 +1691,7 @@ dkwrite(dev_t dev, struct uio *uio, int 
 	struct dkwedge_softc *sc __diagused = dkwedge_lookup(dev);
 
 	KASSERT(sc != NULL);
+	KASSERT(sc->sc_dev != NULL);
 	KASSERT(sc->sc_state != DKW_STATE_LARVAL);
 	KASSERT(sc->sc_state != DKW_STATE_DEAD);
 
@@ -1701,6 +1710,7 @@ dkioctl(dev_t dev, u_long cmd, void *dat
 	int error = 0;
 
 	KASSERT(sc != NULL);
+	KASSERT(sc->sc_dev != NULL);
 	KASSERT(sc->sc_state != DKW_STATE_LARVAL);
 	KASSERT(sc->sc_state != DKW_STATE_DEAD);
 	KASSERT(sc->sc_parent->dk_rawvp != NULL);
@@ -1776,6 +1786,7 @@ dkdiscard(dev_t dev, off_t pos, off_t le
 	int error;
 
 	KASSERT(sc != NULL);
+	KASSERT(sc->sc_dev != NULL);
 	KASSERT(sc->sc_state != DKW_STATE_LARVAL);
 	KASSERT(sc->sc_state != DKW_STATE_DEAD);
 	KASSERT(sc->sc_parent->dk_rawvp != NULL);



CVS commit: src/sys/dev/dkwedge

2023-05-22 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Mon May 22 14:58:33 UTC 2023

Modified Files:
src/sys/dev/dkwedge: dk.c

Log Message:
dk(4): Use config_attach_pseudo_acquire to create wedges.

This way, indexing of the dkwedges array coincides with numbering of
autoconf dk(4) instances.

As a side effect, this plugs a race in dkwedge_add with concurrent
drvctl -r.  There are a lot of such races in dk(4) left -- to be
addressed with more device references.


To generate a diff of this commit:
cvs rdiff -u -r1.158 -r1.159 src/sys/dev/dkwedge/dk.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/dev/dkwedge/dk.c
diff -u src/sys/dev/dkwedge/dk.c:1.158 src/sys/dev/dkwedge/dk.c:1.159
--- src/sys/dev/dkwedge/dk.c:1.158	Sat May 13 10:11:36 2023
+++ src/sys/dev/dkwedge/dk.c	Mon May 22 14:58:32 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: dk.c,v 1.158 2023/05/13 10:11:36 riastradh Exp $	*/
+/*	$NetBSD: dk.c,v 1.159 2023/05/22 14:58:32 riastradh Exp $	*/
 
 /*-
  * Copyright (c) 2004, 2005, 2006, 2007 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: dk.c,v 1.158 2023/05/13 10:11:36 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: dk.c,v 1.159 2023/05/22 14:58:32 riastradh Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_dkwedge.h"
@@ -99,6 +99,8 @@ static int	dkwedge_match(device_t, cfdat
 static void	dkwedge_attach(device_t, device_t, void *);
 static int	dkwedge_detach(device_t, int);
 
+static void	dk_set_geometry(struct dkwedge_softc *, struct disk *);
+
 static void	dkstart(struct dkwedge_softc *);
 static void	dkiodone(struct buf *);
 static void	dkrestart(void *);
@@ -190,9 +192,34 @@ dkwedge_match(device_t parent, cfdata_t 
 static void
 dkwedge_attach(device_t parent, device_t self, void *aux)
 {
+	struct dkwedge_softc *sc = aux;
+	struct disk *pdk = sc->sc_parent;
+	int unit = device_unit(self);
+
+	KASSERTMSG(unit >= 0, "unit=%d", unit);
 
 	if (!pmf_device_register(self, NULL, NULL))
 		aprint_error_dev(self, "couldn't establish power handler\n");
+
+	mutex_enter(>dk_openlock);
+	rw_enter(_lock, RW_WRITER);
+	KASSERTMSG(unit < ndkwedges, "unit=%d ndkwedges=%u", unit, ndkwedges);
+	KASSERTMSG(sc == dkwedges[unit], "sc=%p dkwedges[%d]=%p",
+	sc, unit, dkwedges[unit]);
+	KASSERTMSG(sc->sc_dev == NULL, "sc=%p sc->sc_dev=%p", sc, sc->sc_dev);
+	sc->sc_dev = self;
+	rw_exit(_lock);
+	mutex_exit(>dk_openlock);
+
+	disk_init(>sc_dk, device_xname(sc->sc_dev), NULL);
+	mutex_enter(>dk_openlock);
+	dk_set_geometry(sc, pdk);
+	mutex_exit(>dk_openlock);
+	disk_attach(>sc_dk);
+
+	/* Disk wedge is ready for use! */
+	device_set_private(self, sc);
+	sc->sc_state = DKW_STATE_RUNNING;
 }
 
 /*
@@ -364,6 +391,7 @@ dkwedge_add(struct dkwedge_info *dkw)
 	u_int unit;
 	int error;
 	dev_t pdev;
+	device_t dev __diagused;
 
 	dkw->dkw_parent[sizeof(dkw->dkw_parent) - 1] = '\0';
 	pdk = disk_find(dkw->dkw_parent);
@@ -393,8 +421,11 @@ dkwedge_add(struct dkwedge_info *dkw)
 			break;
 		if (dkwedge_size(lsc) > dkw->dkw_size)
 			break;
+		if (lsc->sc_dev == NULL)
+			break;
 
 		sc = lsc;
+		device_acquire(sc->sc_dev);
 		dkwedge_size_increase(sc, dkw->dkw_size);
 		dk_set_geometry(sc, pdk);
 
@@ -477,7 +508,7 @@ dkwedge_add(struct dkwedge_info *dkw)
 	sc->sc_cfdata.cf_name = dk_cd.cd_name;
 	sc->sc_cfdata.cf_atname = dk_ca.ca_name;
 	/* sc->sc_cfdata.cf_unit set below */
-	sc->sc_cfdata.cf_fstate = FSTATE_STAR;
+	sc->sc_cfdata.cf_fstate = FSTATE_NOTFOUND; /* use chosen cf_unit */
 
 	/* Insert the larval wedge into the array. */
 	rw_enter(_lock, RW_WRITER);
@@ -538,7 +569,7 @@ dkwedge_add(struct dkwedge_info *dkw)
 	 * This should never fail, unless we're almost totally out of
 	 * memory.
 	 */
-	if ((sc->sc_dev = config_attach_pseudo(>sc_cfdata)) == NULL) {
+	if ((dev = config_attach_pseudo_acquire(>sc_cfdata, sc)) == NULL) {
 		aprint_error("%s%u: unable to attach pseudo-device\n",
 		sc->sc_cfdata.cf_name, sc->sc_cfdata.cf_unit);
 
@@ -559,19 +590,7 @@ dkwedge_add(struct dkwedge_info *dkw)
 		return ENOMEM;
 	}
 
-	/*
-	 * XXX Really ought to make the disk_attach() and the changing
-	 * of state to RUNNING atomic.
-	 */
-
-	disk_init(>sc_dk, device_xname(sc->sc_dev), NULL);
-	mutex_enter(>dk_openlock);
-	dk_set_geometry(sc, pdk);
-	mutex_exit(>dk_openlock);
-	disk_attach(>sc_dk);
-
-	/* Disk wedge is ready for use! */
-	sc->sc_state = DKW_STATE_RUNNING;
+	KASSERT(dev == sc->sc_dev);
 
 announce:
 	/* Announce our arrival. */
@@ -586,11 +605,12 @@ announce:
 	strlcpy(dkw->dkw_devname, device_xname(sc->sc_dev),
 	sizeof(dkw->dkw_devname));
 
+	device_release(sc->sc_dev);
 	return 0;
 }
 
 /*
- * dkwedge_find:
+ * dkwedge_find_acquire:
  *
  *	Lookup a disk wedge based on the provided information.
  *	NOTE: We look up the wedge based on the wedge devname,
@@ -598,10 +618,11 @@ announce:
  *
  *	Return NULL if the wedge is not found, otherwise return
  *	the wedge's 

CVS commit: src/sys/dev/dkwedge

2023-05-22 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Mon May 22 14:59:50 UTC 2023

Modified Files:
src/sys/dev/dkwedge: dk.c

Log Message:
dk(4): Strengthen dkclose preconditions.

Like dkopen, except it is possible for this to be called after the
wedge has transitioned to dying.

XXX sc_state read here races with sc_state write in dkwedge_detach.
Could change this to atomic_load/store.


To generate a diff of this commit:
cvs rdiff -u -r1.167 -r1.168 src/sys/dev/dkwedge/dk.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/dev/dkwedge/dk.c
diff -u src/sys/dev/dkwedge/dk.c:1.167 src/sys/dev/dkwedge/dk.c:1.168
--- src/sys/dev/dkwedge/dk.c:1.167	Mon May 22 14:59:42 2023
+++ src/sys/dev/dkwedge/dk.c	Mon May 22 14:59:50 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: dk.c,v 1.167 2023/05/22 14:59:42 riastradh Exp $	*/
+/*	$NetBSD: dk.c,v 1.168 2023/05/22 14:59:50 riastradh Exp $	*/
 
 /*-
  * Copyright (c) 2004, 2005, 2006, 2007 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: dk.c,v 1.167 2023/05/22 14:59:42 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: dk.c,v 1.168 2023/05/22 14:59:50 riastradh Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_dkwedge.h"
@@ -1404,11 +1404,16 @@ dkclose(dev_t dev, int flags, int fmt, s
 {
 	struct dkwedge_softc *sc = dkwedge_lookup(dev);
 
+	/*
+	 * dkclose can be called even if dkopen didn't succeed, so we
+	 * have to handle the same possibility that the wedge may not
+	 * exist.
+	 */
 	if (sc == NULL)
 		return ENXIO;
-	if (sc->sc_state != DKW_STATE_RUNNING &&
-	sc->sc_state != DKW_STATE_DYING)
-		return ENXIO;
+	KASSERT(sc->sc_dev != NULL);
+	KASSERT(sc->sc_state != DKW_STATE_LARVAL);
+	KASSERT(sc->sc_state != DKW_STATE_DEAD);
 
 	mutex_enter(>sc_dk.dk_openlock);
 	mutex_enter(>sc_parent->dk_rawlock);



CVS commit: src/sys/dev/dkwedge

2023-05-22 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Mon May 22 14:58:41 UTC 2023

Modified Files:
src/sys/dev/dkwedge: dk.c

Log Message:
dk(4): dkunit is no longer needed; nix it.

dkwedges array indexing now coincides with autoconf device numbering.


To generate a diff of this commit:
cvs rdiff -u -r1.159 -r1.160 src/sys/dev/dkwedge/dk.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/sys/dev/dkwedge

2023-05-22 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Mon May 22 14:59:42 UTC 2023

Modified Files:
src/sys/dev/dkwedge: dk.c

Log Message:
dk(4): Strengthen dkopen preconditions.

This cannot be called before dkwedge_attach for the same unit
returns, so sc->sc_dev is guaranteed to be set to a nonnull device_t
and the state is guaranteed not to be larval.

And this cannot be called concurrently with dkwedge_detach, or after
dkwedge_detach does vdevgone until another wedge with the same number
is attached (which can't happen until dkwedge_detach completes), so
the state is guaranteed not to be dying or dead.

Hence sc->sc_dev != NULL and sc->sc_state == DKW_STATE_RUNNING.


To generate a diff of this commit:
cvs rdiff -u -r1.166 -r1.167 src/sys/dev/dkwedge/dk.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/sys/dev/dkwedge

2023-05-22 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Mon May 22 14:59:25 UTC 2023

Modified Files:
src/sys/dev/dkwedge: dk.c

Log Message:
dk(4): Split unsafe lookups into safe subroutines and unsafe wrappers.

No functional change intended.

Eventually we should adjust the callers to use the safe subroutines
instead and device_release when done.


To generate a diff of this commit:
cvs rdiff -u -r1.164 -r1.165 src/sys/dev/dkwedge/dk.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/dev/dkwedge/dk.c
diff -u src/sys/dev/dkwedge/dk.c:1.164 src/sys/dev/dkwedge/dk.c:1.165
--- src/sys/dev/dkwedge/dk.c:1.164	Mon May 22 14:59:16 2023
+++ src/sys/dev/dkwedge/dk.c	Mon May 22 14:59:25 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: dk.c,v 1.164 2023/05/22 14:59:16 riastradh Exp $	*/
+/*	$NetBSD: dk.c,v 1.165 2023/05/22 14:59:25 riastradh Exp $	*/
 
 /*-
  * Copyright (c) 2004, 2005, 2006, 2007 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: dk.c,v 1.164 2023/05/22 14:59:16 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: dk.c,v 1.165 2023/05/22 14:59:25 riastradh Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_dkwedge.h"
@@ -883,8 +883,8 @@ dkwedge_list(struct disk *pdk, struct dk
 	return error;
 }
 
-device_t
-dkwedge_find_by_wname(const char *wname)
+static device_t
+dkwedge_find_by_wname_acquire(const char *wname)
 {
 	device_t dv = NULL;
 	struct dkwedge_softc *sc;
@@ -902,6 +902,7 @@ dkwedge_find_by_wname(const char *wname)
 device_xname(sc->sc_dev));
 continue;
 			}
+			device_acquire(sc->sc_dev);
 			dv = sc->sc_dev;
 		}
 	}
@@ -909,8 +910,8 @@ dkwedge_find_by_wname(const char *wname)
 	return dv;
 }
 
-device_t
-dkwedge_find_by_parent(const char *name, size_t *i)
+static device_t
+dkwedge_find_by_parent_acquire(const char *name, size_t *i)
 {
 
 	rw_enter(_lock, RW_READER);
@@ -920,6 +921,7 @@ dkwedge_find_by_parent(const char *name,
 			continue;
 		if (strcmp(sc->sc_parent->dk_name, name) != 0)
 			continue;
+		device_acquire(sc->sc_dev);
 		rw_exit(_lock);
 		return sc->sc_dev;
 	}
@@ -927,6 +929,30 @@ dkwedge_find_by_parent(const char *name,
 	return NULL;
 }
 
+/* XXX unsafe */
+device_t
+dkwedge_find_by_wname(const char *wname)
+{
+	device_t dv;
+
+	if ((dv = dkwedge_find_by_wname_acquire(wname)) == NULL)
+		return NULL;
+	device_release(dv);
+	return dv;
+}
+
+/* XXX unsafe */
+device_t
+dkwedge_find_by_parent(const char *name, size_t *i)
+{
+	device_t dv;
+
+	if ((dv = dkwedge_find_by_parent_acquire(name, i)) == NULL)
+		return NULL;
+	device_release(dv);
+	return dv;
+}
+
 void
 dkwedge_print_wnames(void)
 {
@@ -1845,8 +1871,9 @@ dkdump(dev_t dev, daddr_t blkno, void *v
  *	Find wedge corresponding to the specified parent name
  *	and offset/length.
  */
-device_t
-dkwedge_find_partition(device_t parent, daddr_t startblk, uint64_t nblks)
+static device_t
+dkwedge_find_partition_acquire(device_t parent, daddr_t startblk,
+uint64_t nblks)
 {
 	struct dkwedge_softc *sc;
 	int i;
@@ -1867,6 +1894,7 @@ dkwedge_find_partition(device_t parent, 
 continue;
 			}
 			wedge = sc->sc_dev;
+			device_acquire(wedge);
 		}
 	}
 	rw_exit(_lock);
@@ -1874,6 +1902,20 @@ dkwedge_find_partition(device_t parent, 
 	return wedge;
 }
 
+/* XXX unsafe */
+device_t
+dkwedge_find_partition(device_t parent, daddr_t startblk,
+uint64_t nblks)
+{
+	device_t dv;
+
+	if ((dv = dkwedge_find_partition_acquire(parent, startblk, nblks))
+	== NULL)
+		return NULL;
+	device_release(dv);
+	return dv;
+}
+
 const char *
 dkwedge_get_parent_name(dev_t dev)
 {



CVS commit: src/sys/dev/dkwedge

2023-05-22 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Mon May 22 14:59:17 UTC 2023

Modified Files:
src/sys/dev/dkwedge: dk.c

Log Message:
dk(4): Don't hold lock around uiomove in dkwedge_list.

Instead, hold a device reference.  dkwedge_detach will not run until
the device reference is released.


To generate a diff of this commit:
cvs rdiff -u -r1.163 -r1.164 src/sys/dev/dkwedge/dk.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/dev/dkwedge/dk.c
diff -u src/sys/dev/dkwedge/dk.c:1.163 src/sys/dev/dkwedge/dk.c:1.164
--- src/sys/dev/dkwedge/dk.c:1.163	Mon May 22 14:59:07 2023
+++ src/sys/dev/dkwedge/dk.c	Mon May 22 14:59:16 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: dk.c,v 1.163 2023/05/22 14:59:07 riastradh Exp $	*/
+/*	$NetBSD: dk.c,v 1.164 2023/05/22 14:59:16 riastradh Exp $	*/
 
 /*-
  * Copyright (c) 2004, 2005, 2006, 2007 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: dk.c,v 1.163 2023/05/22 14:59:07 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: dk.c,v 1.164 2023/05/22 14:59:16 riastradh Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_dkwedge.h"
@@ -862,9 +862,19 @@ dkwedge_list(struct disk *pdk, struct dk
 		dkw.dkw_size = dkwedge_size(sc);
 		strlcpy(dkw.dkw_ptype, sc->sc_ptype, sizeof(dkw.dkw_ptype));
 
+		/*
+		 * Acquire a device reference so this wedge doesn't go
+		 * away before our next iteration in LIST_FOREACH, and
+		 * then release the lock for uiomove.
+		 */
+		device_acquire(sc->sc_dev);
+		mutex_exit(>dk_openlock);
 		error = uiomove(, sizeof(dkw), );
+		mutex_enter(>dk_openlock);
+		device_release(sc->sc_dev);
 		if (error)
 			break;
+
 		dkwl->dkwl_ncopied++;
 	}
 	dkwl->dkwl_nwedges = pdk->dk_nwedges;



CVS commit: src/sys/dev/dkwedge

2023-05-22 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Mon May 22 14:59:08 UTC 2023

Modified Files:
src/sys/dev/dkwedge: dk.c

Log Message:
dk(4): Skip larval wedges in various lookup routines.

These have not yet finished a concurent dkwedge_attach, so there's
nothing we can safely do with them.  Just pretend they don't exist --
as if we had arrived at the lookup a moment earlier.


To generate a diff of this commit:
cvs rdiff -u -r1.162 -r1.163 src/sys/dev/dkwedge/dk.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/sys/dev/dkwedge

2023-05-22 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Mon May 22 14:58:51 UTC 2023

Modified Files:
src/sys/dev/dkwedge: dk.c

Log Message:
dk(4): Use device_lookup_private for dkwedge_lookup.

No longer necessary to go through the dkwedges array.

Currently device_lookup_private still involves touching other global
locks, but that will change eventually to a lockless pserialized fast
path.


To generate a diff of this commit:
cvs rdiff -u -r1.160 -r1.161 src/sys/dev/dkwedge/dk.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/dev/dkwedge/dk.c
diff -u src/sys/dev/dkwedge/dk.c:1.160 src/sys/dev/dkwedge/dk.c:1.161
--- src/sys/dev/dkwedge/dk.c:1.160	Mon May 22 14:58:41 2023
+++ src/sys/dev/dkwedge/dk.c	Mon May 22 14:58:50 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: dk.c,v 1.160 2023/05/22 14:58:41 riastradh Exp $	*/
+/*	$NetBSD: dk.c,v 1.161 2023/05/22 14:58:50 riastradh Exp $	*/
 
 /*-
  * Copyright (c) 2004, 2005, 2006, 2007 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: dk.c,v 1.160 2023/05/22 14:58:41 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: dk.c,v 1.161 2023/05/22 14:58:50 riastradh Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_dkwedge.h"
@@ -1164,17 +1164,8 @@ dkwedge_read(struct disk *pdk, struct vn
 static struct dkwedge_softc *
 dkwedge_lookup(dev_t dev)
 {
-	const int unit = minor(dev);
-	struct dkwedge_softc *sc;
-
-	rw_enter(_lock, RW_READER);
-	if (unit < 0 || unit >= ndkwedges)
-		sc = NULL;
-	else
-		sc = dkwedges[unit];
-	rw_exit(_lock);
 
-	return sc;
+	return device_lookup_private(_cd, minor(dev));
 }
 
 static int



CVS commit: src/sys/dev/dkwedge

2023-05-22 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Mon May 22 14:58:41 UTC 2023

Modified Files:
src/sys/dev/dkwedge: dk.c

Log Message:
dk(4): dkunit is no longer needed; nix it.

dkwedges array indexing now coincides with autoconf device numbering.


To generate a diff of this commit:
cvs rdiff -u -r1.159 -r1.160 src/sys/dev/dkwedge/dk.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/dev/dkwedge/dk.c
diff -u src/sys/dev/dkwedge/dk.c:1.159 src/sys/dev/dkwedge/dk.c:1.160
--- src/sys/dev/dkwedge/dk.c:1.159	Mon May 22 14:58:32 2023
+++ src/sys/dev/dkwedge/dk.c	Mon May 22 14:58:41 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: dk.c,v 1.159 2023/05/22 14:58:32 riastradh Exp $	*/
+/*	$NetBSD: dk.c,v 1.160 2023/05/22 14:58:41 riastradh Exp $	*/
 
 /*-
  * Copyright (c) 2004, 2005, 2006, 2007 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: dk.c,v 1.159 2023/05/22 14:58:32 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: dk.c,v 1.160 2023/05/22 14:58:41 riastradh Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_dkwedge.h"
@@ -114,8 +114,6 @@ static int	dkwedge_del1(struct dkwedge_i
 static int	dk_open_parent(dev_t, int, struct vnode **);
 static int	dk_close_parent(struct vnode *, int);
 
-static int	dkunit(dev_t);
-
 static dev_type_open(dkopen);
 static dev_type_close(dkclose);
 static dev_type_cancel(dkcancel);
@@ -142,7 +140,7 @@ const struct bdevsw dk_bdevsw = {
 	.d_psize = dksize,
 	.d_discard = dkdiscard,
 	.d_cfdriver = _cd,
-	.d_devtounit = dkunit,
+	.d_devtounit = dev_minor_unit,
 	.d_flag = D_DISK | D_MPSAFE
 };
 
@@ -160,7 +158,7 @@ const struct cdevsw dk_cdevsw = {
 	.d_kqfilter = nokqfilter,
 	.d_discard = dkdiscard,
 	.d_cfdriver = _cd,
-	.d_devtounit = dkunit,
+	.d_devtounit = dev_minor_unit,
 	.d_flag = D_DISK | D_MPSAFE
 };
 
@@ -1224,36 +1222,6 @@ dk_close_parent(struct vnode *vp, int mo
 }
 
 /*
- * dkunit:		[devsw entry point]
- *
- *	Return the autoconf device_t unit number of a wedge by its
- *	devsw dev_t number, or -1 if there is none.
- *
- *	XXX This is a temporary hack until dkwedge numbering is made to
- *	correspond 1:1 to autoconf device numbering.
- */
-static int
-dkunit(dev_t dev)
-{
-	int mn = minor(dev);
-	struct dkwedge_softc *sc;
-	device_t dv;
-	int unit = -1;
-
-	if (mn < 0)
-		return -1;
-
-	rw_enter(_lock, RW_READER);
-	if (mn < ndkwedges &&
-	(sc = dkwedges[minor(dev)]) != NULL &&
-	(dv = sc->sc_dev) != NULL)
-		unit = device_unit(dv);
-	rw_exit(_lock);
-
-	return unit;
-}
-
-/*
  * dkopen:		[devsw entry point]
  *
  *	Open a wedge.



CVS commit: src/sys/dev/dkwedge

2023-05-22 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Mon May 22 14:58:33 UTC 2023

Modified Files:
src/sys/dev/dkwedge: dk.c

Log Message:
dk(4): Use config_attach_pseudo_acquire to create wedges.

This way, indexing of the dkwedges array coincides with numbering of
autoconf dk(4) instances.

As a side effect, this plugs a race in dkwedge_add with concurrent
drvctl -r.  There are a lot of such races in dk(4) left -- to be
addressed with more device references.


To generate a diff of this commit:
cvs rdiff -u -r1.158 -r1.159 src/sys/dev/dkwedge/dk.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/sys/dev/dkwedge

2023-05-13 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Sat May 13 10:11:36 UTC 2023

Modified Files:
src/sys/dev/dkwedge: dk.c

Log Message:
dk(4): Need pdk->dk_openlock to read pdk->dk_wedges.


To generate a diff of this commit:
cvs rdiff -u -r1.157 -r1.158 src/sys/dev/dkwedge/dk.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/sys/dev/dkwedge

2023-05-13 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Sat May 13 10:11:36 UTC 2023

Modified Files:
src/sys/dev/dkwedge: dk.c

Log Message:
dk(4): Need pdk->dk_openlock to read pdk->dk_wedges.


To generate a diff of this commit:
cvs rdiff -u -r1.157 -r1.158 src/sys/dev/dkwedge/dk.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/dev/dkwedge/dk.c
diff -u src/sys/dev/dkwedge/dk.c:1.157 src/sys/dev/dkwedge/dk.c:1.158
--- src/sys/dev/dkwedge/dk.c:1.157	Wed May 10 23:08:24 2023
+++ src/sys/dev/dkwedge/dk.c	Sat May 13 10:11:36 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: dk.c,v 1.157 2023/05/10 23:08:24 riastradh Exp $	*/
+/*	$NetBSD: dk.c,v 1.158 2023/05/13 10:11:36 riastradh Exp $	*/
 
 /*-
  * Copyright (c) 2004, 2005, 2006, 2007 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: dk.c,v 1.157 2023/05/10 23:08:24 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: dk.c,v 1.158 2023/05/13 10:11:36 riastradh Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_dkwedge.h"
@@ -1333,12 +1333,14 @@ dkfirstopen(struct dkwedge_softc *sc, in
 		KASSERT(sc->sc_parent->dk_rawopens < UINT_MAX);
 		KASSERT(sc->sc_parent->dk_rawvp != NULL);
 		mode = 0;
+		mutex_enter(>sc_parent->dk_openlock);
 		LIST_FOREACH(nsc, >sc_parent->dk_wedges, sc_plink) {
 			if (nsc == sc || nsc->sc_dk.dk_openmask == 0)
 continue;
 			mode = nsc->sc_mode;
 			break;
 		}
+		mutex_exit(>sc_parent->dk_openlock);
 	}
 	sc->sc_mode = mode;
 	sc->sc_parent->dk_rawopens++;



CVS commit: src/sys/dev/dkwedge

2023-05-10 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Wed May 10 23:08:24 UTC 2023

Modified Files:
src/sys/dev/dkwedge: dk.c

Log Message:
dk(4): Make it clearer that dkopen EROFS branch doesn't leak.

It looked like we may need to sometimes call dklastclose in error
branch for the case of (flags & ~sc->sc_mode & FWRITE) != 0, but it
is not actually possible to reach that case: if the caller requested
read/write, and the parent is read-only, and it is the first time
we've opened the parent, then dkfirstopen will fail with EROFS so we
never get there.

But this is confusing and it looked like the error branch is wrong,
so let's rearrange the conditional to make it clearer that we cannot
goto out after dkfirstopen has succeeded.  And then assert that the
case cannot happen when we do call dkfirstopen.


To generate a diff of this commit:
cvs rdiff -u -r1.156 -r1.157 src/sys/dev/dkwedge/dk.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/sys/dev/dkwedge

2023-05-10 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Wed May 10 23:08:24 UTC 2023

Modified Files:
src/sys/dev/dkwedge: dk.c

Log Message:
dk(4): Make it clearer that dkopen EROFS branch doesn't leak.

It looked like we may need to sometimes call dklastclose in error
branch for the case of (flags & ~sc->sc_mode & FWRITE) != 0, but it
is not actually possible to reach that case: if the caller requested
read/write, and the parent is read-only, and it is the first time
we've opened the parent, then dkfirstopen will fail with EROFS so we
never get there.

But this is confusing and it looked like the error branch is wrong,
so let's rearrange the conditional to make it clearer that we cannot
goto out after dkfirstopen has succeeded.  And then assert that the
case cannot happen when we do call dkfirstopen.


To generate a diff of this commit:
cvs rdiff -u -r1.156 -r1.157 src/sys/dev/dkwedge/dk.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/dev/dkwedge/dk.c
diff -u src/sys/dev/dkwedge/dk.c:1.156 src/sys/dev/dkwedge/dk.c:1.157
--- src/sys/dev/dkwedge/dk.c:1.156	Tue May  9 13:14:14 2023
+++ src/sys/dev/dkwedge/dk.c	Wed May 10 23:08:24 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: dk.c,v 1.156 2023/05/09 13:14:14 riastradh Exp $	*/
+/*	$NetBSD: dk.c,v 1.157 2023/05/10 23:08:24 riastradh Exp $	*/
 
 /*-
  * Copyright (c) 2004, 2005, 2006, 2007 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: dk.c,v 1.156 2023/05/09 13:14:14 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: dk.c,v 1.157 2023/05/10 23:08:24 riastradh Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_dkwedge.h"
@@ -1266,12 +1266,22 @@ dkopen(dev_t dev, int flags, int fmt, st
 		error = dkfirstopen(sc, flags);
 		if (error)
 			goto out;
-	}
-	KASSERT(sc->sc_mode != 0);
-	if (flags & ~sc->sc_mode & FWRITE) {
+	} else if (flags & ~sc->sc_mode & FWRITE) {
+		/*
+		 * The parent is already open, but the previous attempt
+		 * to open it read/write failed and fell back to
+		 * read-only.  In that case, we assume the medium is
+		 * read-only and fail to open the wedge read/write.
+		 */
 		error = EROFS;
 		goto out;
 	}
+	KASSERT(sc->sc_mode != 0);
+	KASSERTMSG(sc->sc_mode & FREAD, "%s: sc_mode=%x",
+	device_xname(sc->sc_dev), sc->sc_mode);
+	KASSERTMSG((flags & FWRITE) ? (sc->sc_mode & FWRITE) : 1,
+	"%s: flags=%x sc_mode=%x",
+	device_xname(sc->sc_dev), flags, sc->sc_mode);
 	if (fmt == S_IFCHR)
 		sc->sc_dk.dk_copenmask |= 1;
 	else



CVS commit: src/sys/dev/dkwedge

2023-05-09 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Tue May  9 13:14:14 UTC 2023

Modified Files:
src/sys/dev/dkwedge: dk.c

Log Message:
dk(4): Fix typo: sc_state, not sc_satte.

Had tested a patch series, but not every patch in it, and I
inadvertently fixed the typo in a later patch in the series, not in
the one I committed.


To generate a diff of this commit:
cvs rdiff -u -r1.155 -r1.156 src/sys/dev/dkwedge/dk.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/dev/dkwedge/dk.c
diff -u src/sys/dev/dkwedge/dk.c:1.155 src/sys/dev/dkwedge/dk.c:1.156
--- src/sys/dev/dkwedge/dk.c:1.155	Tue May  9 12:49:00 2023
+++ src/sys/dev/dkwedge/dk.c	Tue May  9 13:14:14 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: dk.c,v 1.155 2023/05/09 12:49:00 riastradh Exp $	*/
+/*	$NetBSD: dk.c,v 1.156 2023/05/09 13:14:14 riastradh Exp $	*/
 
 /*-
  * Copyright (c) 2004, 2005, 2006, 2007 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: dk.c,v 1.155 2023/05/09 12:49:00 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: dk.c,v 1.156 2023/05/09 13:14:14 riastradh Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_dkwedge.h"
@@ -1369,7 +1369,7 @@ dkclose(dev_t dev, int flags, int fmt, s
 	if (sc == NULL)
 		return ENXIO;
 	if (sc->sc_state != DKW_STATE_RUNNING &&
-	sc->sc_satte != DKW_STATE_DYING)
+	sc->sc_state != DKW_STATE_DYING)
 		return ENXIO;
 
 	mutex_enter(>sc_dk.dk_openlock);



CVS commit: src/sys/dev/dkwedge

2023-05-09 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Tue May  9 13:14:14 UTC 2023

Modified Files:
src/sys/dev/dkwedge: dk.c

Log Message:
dk(4): Fix typo: sc_state, not sc_satte.

Had tested a patch series, but not every patch in it, and I
inadvertently fixed the typo in a later patch in the series, not in
the one I committed.


To generate a diff of this commit:
cvs rdiff -u -r1.155 -r1.156 src/sys/dev/dkwedge/dk.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/sys/dev/dkwedge

2023-05-09 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Tue May  9 12:49:00 UTC 2023

Modified Files:
src/sys/dev/dkwedge: dk.c

Log Message:
dk(4): Omit needless sc_iopend, sc_dkdrn mechanism.

vdevgone guarantees that all instances are closed by the time it
returns, which in turn guarantees all I/O operations (read, write,
ioctl, ) have completed, and, if the block device is open,
vinvalbuf(V_SAVE) -> vflushbuf has completed, which forces all
buffered transfers to be issued and waits for them to complete.

So by the time vdevgone returns, no further transfers can be
submitted and the bufq must be empty.


To generate a diff of this commit:
cvs rdiff -u -r1.154 -r1.155 src/sys/dev/dkwedge/dk.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/dev/dkwedge/dk.c
diff -u src/sys/dev/dkwedge/dk.c:1.154 src/sys/dev/dkwedge/dk.c:1.155
--- src/sys/dev/dkwedge/dk.c:1.154	Tue May  9 12:04:04 2023
+++ src/sys/dev/dkwedge/dk.c	Tue May  9 12:49:00 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: dk.c,v 1.154 2023/05/09 12:04:04 riastradh Exp $	*/
+/*	$NetBSD: dk.c,v 1.155 2023/05/09 12:49:00 riastradh Exp $	*/
 
 /*-
  * Copyright (c) 2004, 2005, 2006, 2007 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: dk.c,v 1.154 2023/05/09 12:04:04 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: dk.c,v 1.155 2023/05/09 12:49:00 riastradh Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_dkwedge.h"
@@ -91,8 +91,6 @@ struct dkwedge_softc {
 	struct callout	sc_restart_ch;	/* callout to restart I/O */
 
 	kmutex_t	sc_iolock;
-	kcondvar_t	sc_dkdrn;
-	u_int		sc_iopend;	/* I/Os pending */
 	bool		sc_iostop;	/* don't schedule restart */
 	int		sc_mode;	/* parent open mode */
 };
@@ -198,21 +196,6 @@ dkwedge_attach(device_t parent, device_t
 }
 
 /*
- * dkwedge_wait_drain:
- *
- *	Wait for I/O on the wedge to drain.
- */
-static void
-dkwedge_wait_drain(struct dkwedge_softc *sc)
-{
-
-	mutex_enter(>sc_iolock);
-	while (sc->sc_iopend != 0)
-		cv_wait(>sc_dkdrn, >sc_iolock);
-	mutex_exit(>sc_iolock);
-}
-
-/*
  * dkwedge_compute_pdev:
  *
  *	Compute the parent disk's dev_t.
@@ -441,7 +424,6 @@ dkwedge_add(struct dkwedge_info *dkw)
 	callout_setfunc(>sc_restart_ch, dkrestart, sc);
 
 	mutex_init(>sc_iolock, MUTEX_DEFAULT, IPL_BIO);
-	cv_init(>sc_dkdrn, "dkdrn");
 
 	/*
 	 * Wedge will be added; increment the wedge count for the parent.
@@ -484,7 +466,6 @@ dkwedge_add(struct dkwedge_info *dkw)
 	}
 	mutex_exit(>dk_openlock);
 	if (error) {
-		cv_destroy(>sc_dkdrn);
 		mutex_destroy(>sc_iolock);
 		bufq_free(sc->sc_bufq);
 		dkwedge_size_fini(sc);
@@ -542,7 +523,6 @@ dkwedge_add(struct dkwedge_info *dkw)
 		LIST_REMOVE(sc, sc_plink);
 		mutex_exit(>dk_openlock);
 
-		cv_destroy(>sc_dkdrn);
 		mutex_destroy(>sc_iolock);
 		bufq_free(sc->sc_bufq);
 		dkwedge_size_fini(sc);
@@ -572,7 +552,6 @@ dkwedge_add(struct dkwedge_info *dkw)
 		LIST_REMOVE(sc, sc_plink);
 		mutex_exit(>dk_openlock);
 
-		cv_destroy(>sc_dkdrn);
 		mutex_destroy(>sc_iolock);
 		bufq_free(sc->sc_bufq);
 		dkwedge_size_fini(sc);
@@ -710,14 +689,6 @@ dkwedge_detach(device_t self, int flags)
 	mutex_exit(>sc_iolock);
 	callout_halt(>sc_restart_ch, NULL);
 
-	/*
-	 * dkstart() will kill any queued buffers now that the
-	 * state of the wedge is not RUNNING.  Once we've done
-	 * that, wait for any other pending I/O to complete.
-	 */
-	dkstart(sc);
-	dkwedge_wait_drain(sc);
-
 	/* Locate the wedge major numbers. */
 	bmaj = bdevsw_lookup_major(_bdevsw);
 	cmaj = cdevsw_lookup_major(_cdevsw);
@@ -763,7 +734,6 @@ dkwedge_detach(device_t self, int flags)
 	rw_exit(_lock);
 
 	mutex_destroy(>sc_iolock);
-	cv_destroy(>sc_dkdrn);
 	dkwedge_size_fini(sc);
 
 	free(sc, M_DKWEDGE);
@@ -1484,7 +1454,6 @@ dkstrategy(struct buf *bp)
 
 	/* Place it in the queue and start I/O on the unit. */
 	mutex_enter(>sc_iolock);
-	sc->sc_iopend++;
 	disk_wait(>sc_dk);
 	bufq_put(sc->sc_bufq, bp);
 	mutex_exit(>sc_iolock);
@@ -1514,8 +1483,6 @@ dkstart(struct dkwedge_softc *sc)
 	while ((bp = bufq_peek(sc->sc_bufq)) != NULL) {
 		if (sc->sc_iostop) {
 			(void) bufq_get(sc->sc_bufq);
-			if (--sc->sc_iopend == 0)
-cv_broadcast(>sc_dkdrn);
 			mutex_exit(>sc_iolock);
 			bp->b_error = ENXIO;
 			bp->b_resid = bp->b_bcount;
@@ -1601,9 +1568,6 @@ dkiodone(struct buf *bp)
 	putiobuf(bp);
 
 	mutex_enter(>sc_iolock);
-	if (--sc->sc_iopend == 0)
-		cv_broadcast(>sc_dkdrn);
-
 	disk_unbusy(>sc_dk, obp->b_bcount - obp->b_resid,
 	obp->b_flags & B_READ);
 	mutex_exit(>sc_iolock);



CVS commit: src/sys/dev/dkwedge

2023-05-09 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Tue May  9 12:49:00 UTC 2023

Modified Files:
src/sys/dev/dkwedge: dk.c

Log Message:
dk(4): Omit needless sc_iopend, sc_dkdrn mechanism.

vdevgone guarantees that all instances are closed by the time it
returns, which in turn guarantees all I/O operations (read, write,
ioctl, ) have completed, and, if the block device is open,
vinvalbuf(V_SAVE) -> vflushbuf has completed, which forces all
buffered transfers to be issued and waits for them to complete.

So by the time vdevgone returns, no further transfers can be
submitted and the bufq must be empty.


To generate a diff of this commit:
cvs rdiff -u -r1.154 -r1.155 src/sys/dev/dkwedge/dk.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/sys/dev/dkwedge

2023-05-09 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Tue May  9 12:03:55 UTC 2023

Modified Files:
src/sys/dev/dkwedge: dk.c

Log Message:
dk(4): dkclose must handle a dying wedge too to close the parent.

Otherwise the parent open leaks on detach (or revoke) when the wedge
was open and had to be forcibly closed.

Reported-by: syzbot+e46f31fe56e04f567...@syzkaller.appspotmail.com
https://syzkaller.appspot.com/bug?id=8a00fd7f2e7459748d7a274098180a4708ff0f61

Fixes assertion sc->sc_dk.dk_openmask == 0.


To generate a diff of this commit:
cvs rdiff -u -r1.152 -r1.153 src/sys/dev/dkwedge/dk.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/sys/dev/dkwedge

2023-05-09 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Tue May  9 12:03:55 UTC 2023

Modified Files:
src/sys/dev/dkwedge: dk.c

Log Message:
dk(4): dkclose must handle a dying wedge too to close the parent.

Otherwise the parent open leaks on detach (or revoke) when the wedge
was open and had to be forcibly closed.

Reported-by: syzbot+e46f31fe56e04f567...@syzkaller.appspotmail.com
https://syzkaller.appspot.com/bug?id=8a00fd7f2e7459748d7a274098180a4708ff0f61

Fixes assertion sc->sc_dk.dk_openmask == 0.


To generate a diff of this commit:
cvs rdiff -u -r1.152 -r1.153 src/sys/dev/dkwedge/dk.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/dev/dkwedge/dk.c
diff -u src/sys/dev/dkwedge/dk.c:1.152 src/sys/dev/dkwedge/dk.c:1.153
--- src/sys/dev/dkwedge/dk.c:1.152	Sat Apr 29 13:00:17 2023
+++ src/sys/dev/dkwedge/dk.c	Tue May  9 12:03:55 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: dk.c,v 1.152 2023/04/29 13:00:17 riastradh Exp $	*/
+/*	$NetBSD: dk.c,v 1.153 2023/05/09 12:03:55 riastradh Exp $	*/
 
 /*-
  * Copyright (c) 2004, 2005, 2006, 2007 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: dk.c,v 1.152 2023/04/29 13:00:17 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: dk.c,v 1.153 2023/05/09 12:03:55 riastradh Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_dkwedge.h"
@@ -1385,7 +1385,8 @@ dkclose(dev_t dev, int flags, int fmt, s
 
 	if (sc == NULL)
 		return ENXIO;
-	if (sc->sc_state != DKW_STATE_RUNNING)
+	if (sc->sc_state != DKW_STATE_RUNNING &&
+	sc->sc_satte != DKW_STATE_DYING)
 		return ENXIO;
 
 	mutex_enter(>sc_dk.dk_openlock);



CVS commit: src/sys/dev/dkwedge

2023-04-29 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Sat Apr 29 13:00:17 UTC 2023

Modified Files:
src/sys/dev/dkwedge: dk.c

Log Message:
dk(4): Rename label for consistency.  No functional change intended.


To generate a diff of this commit:
cvs rdiff -u -r1.151 -r1.152 src/sys/dev/dkwedge/dk.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/dev/dkwedge/dk.c
diff -u src/sys/dev/dkwedge/dk.c:1.151 src/sys/dev/dkwedge/dk.c:1.152
--- src/sys/dev/dkwedge/dk.c:1.151	Sat Apr 29 06:23:37 2023
+++ src/sys/dev/dkwedge/dk.c	Sat Apr 29 13:00:17 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: dk.c,v 1.151 2023/04/29 06:23:37 riastradh Exp $	*/
+/*	$NetBSD: dk.c,v 1.152 2023/04/29 13:00:17 riastradh Exp $	*/
 
 /*-
  * Copyright (c) 2004, 2005, 2006, 2007 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: dk.c,v 1.151 2023/04/29 06:23:37 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: dk.c,v 1.152 2023/04/29 13:00:17 riastradh Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_dkwedge.h"
@@ -1282,12 +1282,12 @@ dkopen(dev_t dev, int flags, int fmt, st
 	if (sc->sc_dk.dk_openmask == 0) {
 		error = dkfirstopen(sc, flags);
 		if (error)
-			goto popen_fail;
+			goto out;
 	}
 	KASSERT(sc->sc_mode != 0);
 	if (flags & ~sc->sc_mode & FWRITE) {
 		error = EROFS;
-		goto popen_fail;
+		goto out;
 	}
 	if (fmt == S_IFCHR)
 		sc->sc_dk.dk_copenmask |= 1;
@@ -1296,8 +1296,7 @@ dkopen(dev_t dev, int flags, int fmt, st
 	sc->sc_dk.dk_openmask =
 	sc->sc_dk.dk_copenmask | sc->sc_dk.dk_bopenmask;
 
-popen_fail:
-	mutex_exit(>sc_parent->dk_rawlock);
+out:	mutex_exit(>sc_parent->dk_rawlock);
 	mutex_exit(>sc_dk.dk_openlock);
 	return error;
 }



CVS commit: src/sys/dev/dkwedge

2023-04-29 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Sat Apr 29 13:00:17 UTC 2023

Modified Files:
src/sys/dev/dkwedge: dk.c

Log Message:
dk(4): Rename label for consistency.  No functional change intended.


To generate a diff of this commit:
cvs rdiff -u -r1.151 -r1.152 src/sys/dev/dkwedge/dk.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/sys/dev/dkwedge

2023-04-29 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Sat Apr 29 06:23:37 UTC 2023

Modified Files:
src/sys/dev/dkwedge: dk.c

Log Message:
dk(4): Fix lock assertion in size increase: parent's, not wedge's.

Reported-by: syzbot+d4dc610473cacc518...@syzkaller.appspotmail.com
https://syzkaller.appspot.com/bug?id=e18ddae8283d6fab44cfb1ac7e3f8e791f8c0700


To generate a diff of this commit:
cvs rdiff -u -r1.150 -r1.151 src/sys/dev/dkwedge/dk.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/dev/dkwedge/dk.c
diff -u src/sys/dev/dkwedge/dk.c:1.150 src/sys/dev/dkwedge/dk.c:1.151
--- src/sys/dev/dkwedge/dk.c:1.150	Sat Apr 22 13:11:50 2023
+++ src/sys/dev/dkwedge/dk.c	Sat Apr 29 06:23:37 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: dk.c,v 1.150 2023/04/22 13:11:50 riastradh Exp $	*/
+/*	$NetBSD: dk.c,v 1.151 2023/04/29 06:23:37 riastradh Exp $	*/
 
 /*-
  * Copyright (c) 2004, 2005, 2006, 2007 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: dk.c,v 1.150 2023/04/22 13:11:50 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: dk.c,v 1.151 2023/04/29 06:23:37 riastradh Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_dkwedge.h"
@@ -333,7 +333,7 @@ static void
 dkwedge_size_increase(struct dkwedge_softc *sc, uint64_t size)
 {
 
-	KASSERT(mutex_owned(>sc_dk.dk_openlock));
+	KASSERT(mutex_owned(>sc_parent->dk_openlock));
 
 	rw_enter(>sc_sizelock, RW_WRITER);
 	KASSERTMSG(size >= sc->sc_size,



CVS commit: src/sys/dev/dkwedge

2023-04-29 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Sat Apr 29 06:23:37 UTC 2023

Modified Files:
src/sys/dev/dkwedge: dk.c

Log Message:
dk(4): Fix lock assertion in size increase: parent's, not wedge's.

Reported-by: syzbot+d4dc610473cacc518...@syzkaller.appspotmail.com
https://syzkaller.appspot.com/bug?id=e18ddae8283d6fab44cfb1ac7e3f8e791f8c0700


To generate a diff of this commit:
cvs rdiff -u -r1.150 -r1.151 src/sys/dev/dkwedge/dk.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/sys/dev/dkwedge

2023-04-22 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Sat Apr 22 13:11:50 UTC 2023

Modified Files:
src/sys/dev/dkwedge: dk.c

Log Message:
dk(4): Convert tests to assertions in various devsw operations.

.d_cancel, .d_strategy, .d_read, .d_write, .d_ioctl, and .d_discard
are only ever used between successful .d_open return and entry to
.d_close.  .d_open doesn't return until sc is nonnull and sc_state is
RUNNING, and dkwedge_detach waits for the last .d_close before
setting sc_state to DEAD.  So there is no possibility for sc to be
null or for sc_state to be anything other than RUNNING or DYING.

There is a small functional change here but only in the event of a
race: in the short window between when dkwedge_detach is entered, and
when .d_close runs, any I/O operations (read, write, ioctl, ) may
be issued that would have failed with ENXIO before.

This shouldn't matter for anything: disk I/O operations are supposed
to complete reasonably promptly, and these operations _could_ have
begun milliseconds prior, before dkwedge_detach was entered, so it's
not a significant distinction.

Notes:

- .d_open must still contend with trying to open a nonexistent wedge,
  of course.

- .d_close must also contend with closing a nonexistent wedge, in
  case there were two calls to open in quick succession and the first
  failed while the second hadn't yet determined it would fail.

- .d_size and .d_dump are used from ddb without any open/close.


To generate a diff of this commit:
cvs rdiff -u -r1.149 -r1.150 src/sys/dev/dkwedge/dk.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/sys/dev/dkwedge

2023-04-22 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Sat Apr 22 13:11:50 UTC 2023

Modified Files:
src/sys/dev/dkwedge: dk.c

Log Message:
dk(4): Convert tests to assertions in various devsw operations.

.d_cancel, .d_strategy, .d_read, .d_write, .d_ioctl, and .d_discard
are only ever used between successful .d_open return and entry to
.d_close.  .d_open doesn't return until sc is nonnull and sc_state is
RUNNING, and dkwedge_detach waits for the last .d_close before
setting sc_state to DEAD.  So there is no possibility for sc to be
null or for sc_state to be anything other than RUNNING or DYING.

There is a small functional change here but only in the event of a
race: in the short window between when dkwedge_detach is entered, and
when .d_close runs, any I/O operations (read, write, ioctl, ) may
be issued that would have failed with ENXIO before.

This shouldn't matter for anything: disk I/O operations are supposed
to complete reasonably promptly, and these operations _could_ have
begun milliseconds prior, before dkwedge_detach was entered, so it's
not a significant distinction.

Notes:

- .d_open must still contend with trying to open a nonexistent wedge,
  of course.

- .d_close must also contend with closing a nonexistent wedge, in
  case there were two calls to open in quick succession and the first
  failed while the second hadn't yet determined it would fail.

- .d_size and .d_dump are used from ddb without any open/close.


To generate a diff of this commit:
cvs rdiff -u -r1.149 -r1.150 src/sys/dev/dkwedge/dk.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/dev/dkwedge/dk.c
diff -u src/sys/dev/dkwedge/dk.c:1.149 src/sys/dev/dkwedge/dk.c:1.150
--- src/sys/dev/dkwedge/dk.c:1.149	Sat Apr 22 12:33:46 2023
+++ src/sys/dev/dkwedge/dk.c	Sat Apr 22 13:11:50 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: dk.c,v 1.149 2023/04/22 12:33:46 riastradh Exp $	*/
+/*	$NetBSD: dk.c,v 1.150 2023/04/22 13:11:50 riastradh Exp $	*/
 
 /*-
  * Copyright (c) 2004, 2005, 2006, 2007 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: dk.c,v 1.149 2023/04/22 12:33:46 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: dk.c,v 1.150 2023/04/22 13:11:50 riastradh Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_dkwedge.h"
@@ -1423,6 +1423,8 @@ dkcancel(dev_t dev, int flags, int fmt, 
 
 	KASSERT(sc != NULL);
 	KASSERT(sc->sc_dev != NULL);
+	KASSERT(sc->sc_state != DKW_STATE_LARVAL);
+	KASSERT(sc->sc_state != DKW_STATE_DEAD);
 
 	/*
 	 * Disk I/O is expected to complete or fail within a reasonable
@@ -1448,16 +1450,10 @@ dkstrategy(struct buf *bp)
 	struct dkwedge_softc *sc = dkwedge_lookup(bp->b_dev);
 	uint64_t p_size, p_offset;
 
-	if (sc == NULL) {
-		bp->b_error = ENXIO;
-		goto done;
-	}
-
-	if (sc->sc_state != DKW_STATE_RUNNING ||
-	sc->sc_parent->dk_rawvp == NULL) {
-		bp->b_error = ENXIO;
-		goto done;
-	}
+	KASSERT(sc != NULL);
+	KASSERT(sc->sc_state != DKW_STATE_LARVAL);
+	KASSERT(sc->sc_state != DKW_STATE_DEAD);
+	KASSERT(sc->sc_parent->dk_rawvp != NULL);
 
 	/* If it's an empty transfer, wake up the top half now. */
 	if (bp->b_bcount == 0)
@@ -1647,12 +1643,11 @@ dkminphys(struct buf *bp)
 static int
 dkread(dev_t dev, struct uio *uio, int flags)
 {
-	struct dkwedge_softc *sc = dkwedge_lookup(dev);
+	struct dkwedge_softc *sc __diagused = dkwedge_lookup(dev);
 
-	if (sc == NULL)
-		return ENXIO;
-	if (sc->sc_state != DKW_STATE_RUNNING)
-		return ENXIO;
+	KASSERT(sc != NULL);
+	KASSERT(sc->sc_state != DKW_STATE_LARVAL);
+	KASSERT(sc->sc_state != DKW_STATE_DEAD);
 
 	return physio(dkstrategy, NULL, dev, B_READ, dkminphys, uio);
 }
@@ -1665,12 +1660,11 @@ dkread(dev_t dev, struct uio *uio, int f
 static int
 dkwrite(dev_t dev, struct uio *uio, int flags)
 {
-	struct dkwedge_softc *sc = dkwedge_lookup(dev);
+	struct dkwedge_softc *sc __diagused = dkwedge_lookup(dev);
 
-	if (sc == NULL)
-		return ENXIO;
-	if (sc->sc_state != DKW_STATE_RUNNING)
-		return ENXIO;
+	KASSERT(sc != NULL);
+	KASSERT(sc->sc_state != DKW_STATE_LARVAL);
+	KASSERT(sc->sc_state != DKW_STATE_DEAD);
 
 	return physio(dkstrategy, NULL, dev, B_WRITE, dkminphys, uio);
 }
@@ -1686,12 +1680,10 @@ dkioctl(dev_t dev, u_long cmd, void *dat
 	struct dkwedge_softc *sc = dkwedge_lookup(dev);
 	int error = 0;
 
-	if (sc == NULL)
-		return ENXIO;
-	if (sc->sc_state != DKW_STATE_RUNNING)
-		return ENXIO;
-	if (sc->sc_parent->dk_rawvp == NULL)
-		return ENXIO;
+	KASSERT(sc != NULL);
+	KASSERT(sc->sc_state != DKW_STATE_LARVAL);
+	KASSERT(sc->sc_state != DKW_STATE_DEAD);
+	KASSERT(sc->sc_parent->dk_rawvp != NULL);
 
 	/*
 	 * We pass NODEV instead of our device to indicate we don't
@@ -1763,12 +1755,10 @@ dkdiscard(dev_t dev, off_t pos, off_t le
 	off_t offset, maxlen;
 	int error;
 
-	if (sc == NULL)
-		return ENXIO;
-	if (sc->sc_state != DKW_STATE_RUNNING)
-		return ENXIO;
-	if (sc->sc_parent->dk_rawvp == NULL)
-		return ENXIO;

CVS commit: src/sys/dev/dkwedge

2023-04-22 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Sat Apr 22 12:33:46 UTC 2023

Modified Files:
src/sys/dev/dkwedge: dk.c

Log Message:
dk(4): Fix racy access to sc->sc_dk.dk_openmask in dkwedge_delall1.

Need sc->sc_parent->dk_rawlock for this, as used in dkopen/dkclose.


To generate a diff of this commit:
cvs rdiff -u -r1.148 -r1.149 src/sys/dev/dkwedge/dk.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/dev/dkwedge/dk.c
diff -u src/sys/dev/dkwedge/dk.c:1.148 src/sys/dev/dkwedge/dk.c:1.149
--- src/sys/dev/dkwedge/dk.c:1.148	Fri Apr 21 18:54:09 2023
+++ src/sys/dev/dkwedge/dk.c	Sat Apr 22 12:33:46 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: dk.c,v 1.148 2023/04/21 18:54:09 riastradh Exp $	*/
+/*	$NetBSD: dk.c,v 1.149 2023/04/22 12:33:46 riastradh Exp $	*/
 
 /*-
  * Copyright (c) 2004, 2005, 2006, 2007 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: dk.c,v 1.148 2023/04/21 18:54:09 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: dk.c,v 1.149 2023/04/22 12:33:46 riastradh Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_dkwedge.h"
@@ -796,6 +796,7 @@ dkwedge_delall1(struct disk *pdk, bool i
 		flags |= DETACH_FORCE;
 
 	for (;;) {
+		mutex_enter(>dk_rawlock); /* for sc->sc_dk.dk_openmask */
 		mutex_enter(>dk_openlock);
 		LIST_FOREACH(sc, >dk_wedges, sc_plink) {
 			if (!idleonly || sc->sc_dk.dk_openmask == 0)
@@ -804,12 +805,14 @@ dkwedge_delall1(struct disk *pdk, bool i
 		if (sc == NULL) {
 			KASSERT(idleonly || pdk->dk_nwedges == 0);
 			mutex_exit(>dk_openlock);
+			mutex_exit(>dk_rawlock);
 			return;
 		}
 		strlcpy(dkw.dkw_parent, pdk->dk_name, sizeof(dkw.dkw_parent));
 		strlcpy(dkw.dkw_devname, device_xname(sc->sc_dev),
 		sizeof(dkw.dkw_devname));
 		mutex_exit(>dk_openlock);
+		mutex_exit(>dk_rawlock);
 		(void) dkwedge_del1(, flags);
 	}
 }



CVS commit: src/sys/dev/dkwedge

2023-04-22 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Sat Apr 22 12:33:46 UTC 2023

Modified Files:
src/sys/dev/dkwedge: dk.c

Log Message:
dk(4): Fix racy access to sc->sc_dk.dk_openmask in dkwedge_delall1.

Need sc->sc_parent->dk_rawlock for this, as used in dkopen/dkclose.


To generate a diff of this commit:
cvs rdiff -u -r1.148 -r1.149 src/sys/dev/dkwedge/dk.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/sys/dev/dkwedge

2023-04-21 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Fri Apr 21 18:54:09 UTC 2023

Modified Files:
src/sys/dev/dkwedge: dk.c

Log Message:
dk(4): Narrow the scope of the device numbering lookup on detach.

Just need it for vdevgone, order relative to other things in detach
doesn't matter.

No functional change intended.


To generate a diff of this commit:
cvs rdiff -u -r1.147 -r1.148 src/sys/dev/dkwedge/dk.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/dev/dkwedge/dk.c
diff -u src/sys/dev/dkwedge/dk.c:1.147 src/sys/dev/dkwedge/dk.c:1.148
--- src/sys/dev/dkwedge/dk.c:1.147	Fri Apr 21 18:45:13 2023
+++ src/sys/dev/dkwedge/dk.c	Fri Apr 21 18:54:09 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: dk.c,v 1.147 2023/04/21 18:45:13 riastradh Exp $	*/
+/*	$NetBSD: dk.c,v 1.148 2023/04/21 18:54:09 riastradh Exp $	*/
 
 /*-
  * Copyright (c) 2004, 2005, 2006, 2007 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: dk.c,v 1.147 2023/04/21 18:45:13 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: dk.c,v 1.148 2023/04/21 18:54:09 riastradh Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_dkwedge.h"
@@ -704,10 +704,6 @@ dkwedge_detach(device_t self, int flags)
 
 	pmf_device_deregister(self);
 
-	/* Locate the wedge major numbers. */
-	bmaj = bdevsw_lookup_major(_bdevsw);
-	cmaj = cdevsw_lookup_major(_cdevsw);
-
 	/* Kill any pending restart. */
 	mutex_enter(>sc_iolock);
 	sc->sc_iostop = true;
@@ -722,6 +718,10 @@ dkwedge_detach(device_t self, int flags)
 	dkstart(sc);
 	dkwedge_wait_drain(sc);
 
+	/* Locate the wedge major numbers. */
+	bmaj = bdevsw_lookup_major(_bdevsw);
+	cmaj = cdevsw_lookup_major(_cdevsw);
+
 	/* Nuke the vnodes for any open instances. */
 	vdevgone(bmaj, unit, unit, VBLK);
 	vdevgone(cmaj, unit, unit, VCHR);



CVS commit: src/sys/dev/dkwedge

2023-04-21 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Fri Apr 21 18:54:09 UTC 2023

Modified Files:
src/sys/dev/dkwedge: dk.c

Log Message:
dk(4): Narrow the scope of the device numbering lookup on detach.

Just need it for vdevgone, order relative to other things in detach
doesn't matter.

No functional change intended.


To generate a diff of this commit:
cvs rdiff -u -r1.147 -r1.148 src/sys/dev/dkwedge/dk.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/sys/dev/dkwedge

2023-04-21 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Fri Apr 21 18:45:13 UTC 2023

Modified Files:
src/sys/dev/dkwedge: dk.c

Log Message:
dk(4): dkdump: Simplify.  No functional change intended.


To generate a diff of this commit:
cvs rdiff -u -r1.146 -r1.147 src/sys/dev/dkwedge/dk.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/dev/dkwedge/dk.c
diff -u src/sys/dev/dkwedge/dk.c:1.146 src/sys/dev/dkwedge/dk.c:1.147
--- src/sys/dev/dkwedge/dk.c:1.146	Fri Apr 21 18:44:58 2023
+++ src/sys/dev/dkwedge/dk.c	Fri Apr 21 18:45:13 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: dk.c,v 1.146 2023/04/21 18:44:58 riastradh Exp $	*/
+/*	$NetBSD: dk.c,v 1.147 2023/04/21 18:45:13 riastradh Exp $	*/
 
 /*-
  * Copyright (c) 2004, 2005, 2006, 2007 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: dk.c,v 1.146 2023/04/21 18:44:58 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: dk.c,v 1.147 2023/04/21 18:45:13 riastradh Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_dkwedge.h"
@@ -1834,7 +1834,6 @@ dkdump(dev_t dev, daddr_t blkno, void *v
 	struct dkwedge_softc *sc = dkwedge_lookup(dev);
 	const struct bdevsw *bdev;
 	uint64_t p_size, p_offset;
-	int rv = 0;
 
 	if (sc == NULL)
 		return ENXIO;
@@ -1845,14 +1844,10 @@ dkdump(dev_t dev, daddr_t blkno, void *v
 
 	if (strcmp(sc->sc_ptype, DKW_PTYPE_SWAP) != 0 &&
 	strcmp(sc->sc_ptype, DKW_PTYPE_RAID) != 0 &&
-	strcmp(sc->sc_ptype, DKW_PTYPE_CGD) != 0) {
-		rv = ENXIO;
-		goto out;
-	}
-	if (size % DEV_BSIZE != 0) {
-		rv = EINVAL;
-		goto out;
-	}
+	strcmp(sc->sc_ptype, DKW_PTYPE_CGD) != 0)
+		return ENXIO;
+	if (size % DEV_BSIZE != 0)
+		return EINVAL;
 
 	p_offset = sc->sc_offset << sc->sc_parent->dk_blkshift;
 	p_size = dkwedge_size(sc) << sc->sc_parent->dk_blkshift;
@@ -1861,15 +1856,11 @@ dkdump(dev_t dev, daddr_t blkno, void *v
 		printf("%s: blkno (%" PRIu64 ") + size / DEV_BSIZE (%zu) > "
 		"p_size (%" PRIu64 ")\n", __func__, blkno,
 		size/DEV_BSIZE, p_size);
-		rv = EINVAL;
-		goto out;
+		return EINVAL;
 	}
 
 	bdev = bdevsw_lookup(sc->sc_pdev);
-	rv = (*bdev->d_dump)(sc->sc_pdev, blkno + p_offset, va, size);
-
-out:
-	return rv;
+	return (*bdev->d_dump)(sc->sc_pdev, blkno + p_offset, va, size);
 }
 
 /*



CVS commit: src/sys/dev/dkwedge

2023-04-21 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Fri Apr 21 18:45:13 UTC 2023

Modified Files:
src/sys/dev/dkwedge: dk.c

Log Message:
dk(4): dkdump: Simplify.  No functional change intended.


To generate a diff of this commit:
cvs rdiff -u -r1.146 -r1.147 src/sys/dev/dkwedge/dk.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/sys/dev/dkwedge

2023-04-21 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Fri Apr 21 18:44:59 UTC 2023

Modified Files:
src/sys/dev/dkwedge: dk.c

Log Message:
dk(4): Omit needless locking in dksize, dkdump.

All the members these use are stable after initialization, except for
the wedge size, which dkwedge_size safely reads a snapshot of without
locking in the caller.


To generate a diff of this commit:
cvs rdiff -u -r1.145 -r1.146 src/sys/dev/dkwedge/dk.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/dev/dkwedge/dk.c
diff -u src/sys/dev/dkwedge/dk.c:1.145 src/sys/dev/dkwedge/dk.c:1.146
--- src/sys/dev/dkwedge/dk.c:1.145	Fri Apr 21 18:44:18 2023
+++ src/sys/dev/dkwedge/dk.c	Fri Apr 21 18:44:58 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: dk.c,v 1.145 2023/04/21 18:44:18 riastradh Exp $	*/
+/*	$NetBSD: dk.c,v 1.146 2023/04/21 18:44:58 riastradh Exp $	*/
 
 /*-
  * Copyright (c) 2004, 2005, 2006, 2007 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: dk.c,v 1.145 2023/04/21 18:44:18 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: dk.c,v 1.146 2023/04/21 18:44:58 riastradh Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_dkwedge.h"
@@ -1809,9 +1809,6 @@ dksize(dev_t dev)
 	if (sc->sc_state != DKW_STATE_RUNNING)
 		return -1;
 
-	mutex_enter(>sc_dk.dk_openlock);
-	mutex_enter(>sc_parent->dk_rawlock);
-
 	/* Our content type is static, no need to open the device. */
 
 	p_size = dkwedge_size(sc) << sc->sc_parent->dk_blkshift;
@@ -1823,9 +1820,6 @@ dksize(dev_t dev)
 			rv = (int)p_size;
 	}
 
-	mutex_exit(>sc_parent->dk_rawlock);
-	mutex_exit(>sc_dk.dk_openlock);
-
 	return rv;
 }
 
@@ -1847,9 +1841,6 @@ dkdump(dev_t dev, daddr_t blkno, void *v
 	if (sc->sc_state != DKW_STATE_RUNNING)
 		return ENXIO;
 
-	mutex_enter(>sc_dk.dk_openlock);
-	mutex_enter(>sc_parent->dk_rawlock);
-
 	/* Our content type is static, no need to open the device. */
 
 	if (strcmp(sc->sc_ptype, DKW_PTYPE_SWAP) != 0 &&
@@ -1878,9 +1869,6 @@ dkdump(dev_t dev, daddr_t blkno, void *v
 	rv = (*bdev->d_dump)(sc->sc_pdev, blkno + p_offset, va, size);
 
 out:
-	mutex_exit(>sc_parent->dk_rawlock);
-	mutex_exit(>sc_dk.dk_openlock);
-
 	return rv;
 }
 



CVS commit: src/sys/dev/dkwedge

2023-04-21 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Fri Apr 21 18:44:59 UTC 2023

Modified Files:
src/sys/dev/dkwedge: dk.c

Log Message:
dk(4): Omit needless locking in dksize, dkdump.

All the members these use are stable after initialization, except for
the wedge size, which dkwedge_size safely reads a snapshot of without
locking in the caller.


To generate a diff of this commit:
cvs rdiff -u -r1.145 -r1.146 src/sys/dev/dkwedge/dk.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/sys/dev/dkwedge

2023-04-21 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Fri Apr 21 18:44:18 UTC 2023

Modified Files:
src/sys/dev/dkwedge: dk.c

Log Message:
dk(4): Take a read-lock on dkwedges_lock if we're only reading.

- dkwedge_find_by_name
- dkwedge_find_by_parent
- dkwedge_print_wnames


To generate a diff of this commit:
cvs rdiff -u -r1.144 -r1.145 src/sys/dev/dkwedge/dk.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/dev/dkwedge/dk.c
diff -u src/sys/dev/dkwedge/dk.c:1.144 src/sys/dev/dkwedge/dk.c:1.145
--- src/sys/dev/dkwedge/dk.c:1.144	Fri Apr 21 18:31:00 2023
+++ src/sys/dev/dkwedge/dk.c	Fri Apr 21 18:44:18 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: dk.c,v 1.144 2023/04/21 18:31:00 riastradh Exp $	*/
+/*	$NetBSD: dk.c,v 1.145 2023/04/21 18:44:18 riastradh Exp $	*/
 
 /*-
  * Copyright (c) 2004, 2005, 2006, 2007 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: dk.c,v 1.144 2023/04/21 18:31:00 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: dk.c,v 1.145 2023/04/21 18:44:18 riastradh Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_dkwedge.h"
@@ -877,7 +877,7 @@ dkwedge_find_by_wname(const char *wname)
 	struct dkwedge_softc *sc;
 	int i;
 
-	rw_enter(_lock, RW_WRITER);
+	rw_enter(_lock, RW_READER);
 	for (i = 0; i < ndkwedges; i++) {
 		if ((sc = dkwedges[i]) == NULL)
 			continue;
@@ -900,7 +900,7 @@ device_t
 dkwedge_find_by_parent(const char *name, size_t *i)
 {
 
-	rw_enter(_lock, RW_WRITER);
+	rw_enter(_lock, RW_READER);
 	for (; *i < (size_t)ndkwedges; (*i)++) {
 		struct dkwedge_softc *sc;
 		if ((sc = dkwedges[*i]) == NULL)
@@ -920,7 +920,7 @@ dkwedge_print_wnames(void)
 	struct dkwedge_softc *sc;
 	int i;
 
-	rw_enter(_lock, RW_WRITER);
+	rw_enter(_lock, RW_READER);
 	for (i = 0; i < ndkwedges; i++) {
 		if ((sc = dkwedges[i]) == NULL)
 			continue;



CVS commit: src/sys/dev/dkwedge

2023-04-21 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Fri Apr 21 18:44:18 UTC 2023

Modified Files:
src/sys/dev/dkwedge: dk.c

Log Message:
dk(4): Take a read-lock on dkwedges_lock if we're only reading.

- dkwedge_find_by_name
- dkwedge_find_by_parent
- dkwedge_print_wnames


To generate a diff of this commit:
cvs rdiff -u -r1.144 -r1.145 src/sys/dev/dkwedge/dk.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/sys/dev/dkwedge

2023-04-21 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Fri Apr 21 18:31:00 UTC 2023

Modified Files:
src/sys/dev/dkwedge: dk.c

Log Message:
dk(4): Set .d_cfdriver and .d_devtounit to plug open/detach race.

This way, opening dkN or rdkN will wait if attach or detach is still
in progress, and vdevgone will wake up such pending opens and make
them fail.  So it is no longer possible for a wedge to be detached
after dkopen has already started using it.

For now, we use a custom .d_devtounit function that looks up the
autoconf unit number via the dkwedges array, which conceivably may
use an independent unit numbering system -- nothing guarantees they
match up.  (In practice they will mostly match up, but concurrent
wedge creation could lead to different numbering.)  Eventually this
should be changed so the two numbering systems match, which would let
us delete the new dkunit function and just use dev_minor_unit like
many other drivers can.


To generate a diff of this commit:
cvs rdiff -u -r1.143 -r1.144 src/sys/dev/dkwedge/dk.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/dev/dkwedge/dk.c
diff -u src/sys/dev/dkwedge/dk.c:1.143 src/sys/dev/dkwedge/dk.c:1.144
--- src/sys/dev/dkwedge/dk.c:1.143	Fri Apr 21 18:30:52 2023
+++ src/sys/dev/dkwedge/dk.c	Fri Apr 21 18:31:00 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: dk.c,v 1.143 2023/04/21 18:30:52 riastradh Exp $	*/
+/*	$NetBSD: dk.c,v 1.144 2023/04/21 18:31:00 riastradh Exp $	*/
 
 /*-
  * Copyright (c) 2004, 2005, 2006, 2007 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: dk.c,v 1.143 2023/04/21 18:30:52 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: dk.c,v 1.144 2023/04/21 18:31:00 riastradh Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_dkwedge.h"
@@ -114,6 +114,8 @@ static int	dkwedge_del1(struct dkwedge_i
 static int	dk_open_parent(dev_t, int, struct vnode **);
 static int	dk_close_parent(struct vnode *, int);
 
+static int	dkunit(dev_t);
+
 static dev_type_open(dkopen);
 static dev_type_close(dkclose);
 static dev_type_cancel(dkcancel);
@@ -139,6 +141,8 @@ const struct bdevsw dk_bdevsw = {
 	.d_dump = dkdump,
 	.d_psize = dksize,
 	.d_discard = dkdiscard,
+	.d_cfdriver = _cd,
+	.d_devtounit = dkunit,
 	.d_flag = D_DISK | D_MPSAFE
 };
 
@@ -155,6 +159,8 @@ const struct cdevsw dk_cdevsw = {
 	.d_mmap = nommap,
 	.d_kqfilter = nokqfilter,
 	.d_discard = dkdiscard,
+	.d_cfdriver = _cd,
+	.d_devtounit = dkunit,
 	.d_flag = D_DISK | D_MPSAFE
 };
 
@@ -1217,6 +1223,36 @@ dk_close_parent(struct vnode *vp, int mo
 }
 
 /*
+ * dkunit:		[devsw entry point]
+ *
+ *	Return the autoconf device_t unit number of a wedge by its
+ *	devsw dev_t number, or -1 if there is none.
+ *
+ *	XXX This is a temporary hack until dkwedge numbering is made to
+ *	correspond 1:1 to autoconf device numbering.
+ */
+static int
+dkunit(dev_t dev)
+{
+	int mn = minor(dev);
+	struct dkwedge_softc *sc;
+	device_t dv;
+	int unit = -1;
+
+	if (mn < 0)
+		return -1;
+
+	rw_enter(_lock, RW_READER);
+	if (mn < ndkwedges &&
+	(sc = dkwedges[minor(dev)]) != NULL &&
+	(dv = sc->sc_dev) != NULL)
+		unit = device_unit(dv);
+	rw_exit(_lock);
+
+	return unit;
+}
+
+/*
  * dkopen:		[devsw entry point]
  *
  *	Open a wedge.



CVS commit: src/sys/dev/dkwedge

2023-04-21 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Fri Apr 21 18:31:00 UTC 2023

Modified Files:
src/sys/dev/dkwedge: dk.c

Log Message:
dk(4): Set .d_cfdriver and .d_devtounit to plug open/detach race.

This way, opening dkN or rdkN will wait if attach or detach is still
in progress, and vdevgone will wake up such pending opens and make
them fail.  So it is no longer possible for a wedge to be detached
after dkopen has already started using it.

For now, we use a custom .d_devtounit function that looks up the
autoconf unit number via the dkwedges array, which conceivably may
use an independent unit numbering system -- nothing guarantees they
match up.  (In practice they will mostly match up, but concurrent
wedge creation could lead to different numbering.)  Eventually this
should be changed so the two numbering systems match, which would let
us delete the new dkunit function and just use dev_minor_unit like
many other drivers can.


To generate a diff of this commit:
cvs rdiff -u -r1.143 -r1.144 src/sys/dev/dkwedge/dk.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/sys/dev/dkwedge

2023-04-21 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Fri Apr 21 18:30:52 UTC 2023

Modified Files:
src/sys/dev/dkwedge: dk.c

Log Message:
dk(4): Use disk_begindetach and rely on vdevgone to close instances.

The first step is to decide whether we can detach (if forced, yes; if
not forced, only if not already open), and prevent new opens if so.
There's no need to start closing open instances at this point --
we're just making a decision to detach, and preventing new opens by
transitioning state that dkopen will respect[*].

The second step is to force all open instances to close.  This is
done by vdevgone.  By the time vdevgone returns, there can be no open
instances, so if there _were_ any, closing them via vdevgone will
have passed through dklastclose.

After that point, there can be no opens and no I/O operations, so
dk_openmask must already be zero and the bufq must be empty.

Thus, there's no need to have an explicit call to dklastclose (via
dkwedge_cleanup_parent) before or after making the decision to
detach.

[*] Currently access to this state is racy: nothing serializes
dkwedge_detach's state transition with dkopen's test.  TBD in a
separate commit shortly.


To generate a diff of this commit:
cvs rdiff -u -r1.142 -r1.143 src/sys/dev/dkwedge/dk.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/dev/dkwedge/dk.c
diff -u src/sys/dev/dkwedge/dk.c:1.142 src/sys/dev/dkwedge/dk.c:1.143
--- src/sys/dev/dkwedge/dk.c:1.142	Fri Apr 21 18:30:32 2023
+++ src/sys/dev/dkwedge/dk.c	Fri Apr 21 18:30:52 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: dk.c,v 1.142 2023/04/21 18:30:32 riastradh Exp $	*/
+/*	$NetBSD: dk.c,v 1.143 2023/04/21 18:30:52 riastradh Exp $	*/
 
 /*-
  * Copyright (c) 2004, 2005, 2006, 2007 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: dk.c,v 1.142 2023/04/21 18:30:32 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: dk.c,v 1.143 2023/04/21 18:30:52 riastradh Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_dkwedge.h"
@@ -108,7 +108,6 @@ static void	dkminphys(struct buf *);
 
 static int	dkfirstopen(struct dkwedge_softc *, int);
 static void	dklastclose(struct dkwedge_softc *);
-static int	dkwedge_cleanup_parent(struct dkwedge_softc *, int);
 static int	dkwedge_detach(device_t, int);
 static void	dkwedge_delall1(struct disk *, bool);
 static int	dkwedge_del1(struct dkwedge_info *, int);
@@ -668,28 +667,6 @@ dkwedge_del1(struct dkwedge_info *dkw, i
 	return config_detach(sc->sc_dev, flags);
 }
 
-static int
-dkwedge_cleanup_parent(struct dkwedge_softc *sc, int flags)
-{
-	struct disk *dk = >sc_dk;
-	int rc;
-
-	rc = 0;
-	mutex_enter(>dk_openlock);
-	if (dk->dk_openmask == 0) {
-		/* nothing to do */
-	} else if ((flags & DETACH_FORCE) == 0) {
-		rc = EBUSY;
-	}  else {
-		mutex_enter(>sc_parent->dk_rawlock);
-		dklastclose(sc);
-		mutex_exit(>sc_parent->dk_rawlock);
-	}
-	mutex_exit(>sc_dk.dk_openlock);
-
-	return rc;
-}
-
 /*
  * dkwedge_detach:
  *
@@ -709,7 +686,8 @@ dkwedge_detach(device_t self, int flags)
 	}
 	if (unit == ndkwedges)
 		rc = ENXIO;
-	else if ((rc = dkwedge_cleanup_parent(sc, flags)) == 0) {
+	else if ((rc = disk_begindetach(>sc_dk, /*lastclose*/NULL, self,
+		flags)) == 0) {
 		/* Mark the wedge as dying. */
 		sc->sc_state = DKW_STATE_DYING;
 	}
@@ -742,8 +720,17 @@ dkwedge_detach(device_t self, int flags)
 	vdevgone(bmaj, unit, unit, VBLK);
 	vdevgone(cmaj, unit, unit, VCHR);
 
-	/* Clean up the parent. */
-	dkwedge_cleanup_parent(sc, flags | DETACH_FORCE);
+	/*
+	 * At this point, all block device opens have been closed,
+	 * synchronously flushing any buffered writes; and all
+	 * character device I/O operations have completed
+	 * synchronously, and character device opens have been closed.
+	 *
+	 * So there can be no more opens or queued buffers by now.
+	 */
+	KASSERT(sc->sc_dk.dk_openmask == 0);
+	KASSERT(bufq_peek(sc->sc_bufq) == NULL);
+	bufq_drain(sc->sc_bufq);
 
 	/* Announce our departure. */
 	aprint_normal("%s at %s (%s) deleted\n", device_xname(sc->sc_dev),



CVS commit: src/sys/dev/dkwedge

2023-04-21 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Fri Apr 21 18:30:52 UTC 2023

Modified Files:
src/sys/dev/dkwedge: dk.c

Log Message:
dk(4): Use disk_begindetach and rely on vdevgone to close instances.

The first step is to decide whether we can detach (if forced, yes; if
not forced, only if not already open), and prevent new opens if so.
There's no need to start closing open instances at this point --
we're just making a decision to detach, and preventing new opens by
transitioning state that dkopen will respect[*].

The second step is to force all open instances to close.  This is
done by vdevgone.  By the time vdevgone returns, there can be no open
instances, so if there _were_ any, closing them via vdevgone will
have passed through dklastclose.

After that point, there can be no opens and no I/O operations, so
dk_openmask must already be zero and the bufq must be empty.

Thus, there's no need to have an explicit call to dklastclose (via
dkwedge_cleanup_parent) before or after making the decision to
detach.

[*] Currently access to this state is racy: nothing serializes
dkwedge_detach's state transition with dkopen's test.  TBD in a
separate commit shortly.


To generate a diff of this commit:
cvs rdiff -u -r1.142 -r1.143 src/sys/dev/dkwedge/dk.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/sys/dev/dkwedge

2023-04-21 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Fri Apr 21 18:30:32 UTC 2023

Modified Files:
src/sys/dev/dkwedge: dk.c

Log Message:
dk(4): Fix callout detach race.

1. Set a flag sc_iostop under the lock sc_iolock so dkwedge_detach
   and dkstart don't race over it.

2. Decline to schedule the callout if sc_iostop is set.  The callout
   is already only ever scheduled while the lock is held.

3. Use callout_halt to wait for any concurrent callout to complete.
   At this point, it can't reschedule itself.

Without this change, the callout could be concurrently rescheduling
itself as we issue callout_stop, leading to use-after-free later.


To generate a diff of this commit:
cvs rdiff -u -r1.141 -r1.142 src/sys/dev/dkwedge/dk.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/sys/dev/dkwedge

2023-04-21 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Fri Apr 21 18:30:21 UTC 2023

Modified Files:
src/sys/dev/dkwedge: dk.c

Log Message:
dk(4): Add null d_cancel routine to devsw.

This way, dkclose is guaranteed that dkopen, dkread, dkwrite,
dkioctl, , have all returned before it runs.  For block opens,
setting d_cancel also guarantees that any buffered writes are flushed
with vinvalbuf before dkclose is called.


To generate a diff of this commit:
cvs rdiff -u -r1.140 -r1.141 src/sys/dev/dkwedge/dk.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/dev/dkwedge/dk.c
diff -u src/sys/dev/dkwedge/dk.c:1.140 src/sys/dev/dkwedge/dk.c:1.141
--- src/sys/dev/dkwedge/dk.c:1.140	Fri Apr 21 18:29:43 2023
+++ src/sys/dev/dkwedge/dk.c	Fri Apr 21 18:30:21 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: dk.c,v 1.140 2023/04/21 18:29:43 riastradh Exp $	*/
+/*	$NetBSD: dk.c,v 1.141 2023/04/21 18:30:21 riastradh Exp $	*/
 
 /*-
  * Copyright (c) 2004, 2005, 2006, 2007 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: dk.c,v 1.140 2023/04/21 18:29:43 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: dk.c,v 1.141 2023/04/21 18:30:21 riastradh Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_dkwedge.h"
@@ -116,6 +116,7 @@ static int	dk_close_parent(struct vnode 
 
 static dev_type_open(dkopen);
 static dev_type_close(dkclose);
+static dev_type_cancel(dkcancel);
 static dev_type_read(dkread);
 static dev_type_write(dkwrite);
 static dev_type_ioctl(dkioctl);
@@ -132,6 +133,7 @@ CFATTACH_DECL3_NEW(dk, 0,
 const struct bdevsw dk_bdevsw = {
 	.d_open = dkopen,
 	.d_close = dkclose,
+	.d_cancel = dkcancel,
 	.d_strategy = dkstrategy,
 	.d_ioctl = dkioctl,
 	.d_dump = dkdump,
@@ -143,6 +145,7 @@ const struct bdevsw dk_bdevsw = {
 const struct cdevsw dk_cdevsw = {
 	.d_open = dkopen,
 	.d_close = dkclose,
+	.d_cancel = dkcancel,
 	.d_read = dkread,
 	.d_write = dkwrite,
 	.d_ioctl = dkioctl,
@@ -1379,6 +1382,32 @@ dkclose(dev_t dev, int flags, int fmt, s
 }
 
 /*
+ * dkcancel:		[devsw entry point]
+ *
+ *	Cancel any pending I/O operations waiting on a wedge.
+ */
+static int
+dkcancel(dev_t dev, int flags, int fmt, struct lwp *l)
+{
+	struct dkwedge_softc *sc = dkwedge_lookup(dev);
+
+	KASSERT(sc != NULL);
+	KASSERT(sc->sc_dev != NULL);
+
+	/*
+	 * Disk I/O is expected to complete or fail within a reasonable
+	 * timeframe -- it's storage, not communication.  Further, the
+	 * character and block device interface guarantees that prior
+	 * reads and writes have completed or failed by the time close
+	 * returns -- we are not to cancel them here.  If the parent
+	 * device's hardware is gone, the parent driver can make them
+	 * fail.  Nothing for dk(4) itself to do.
+	 */
+
+	return 0;
+}
+
+/*
  * dkstrategy:		[devsw entry point]
  *
  *	Perform I/O based on the wedge I/O strategy.



CVS commit: src/sys/dev/dkwedge

2023-04-21 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Fri Apr 21 18:30:21 UTC 2023

Modified Files:
src/sys/dev/dkwedge: dk.c

Log Message:
dk(4): Add null d_cancel routine to devsw.

This way, dkclose is guaranteed that dkopen, dkread, dkwrite,
dkioctl, , have all returned before it runs.  For block opens,
setting d_cancel also guarantees that any buffered writes are flushed
with vinvalbuf before dkclose is called.


To generate a diff of this commit:
cvs rdiff -u -r1.140 -r1.141 src/sys/dev/dkwedge/dk.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/sys/dev/dkwedge

2023-04-21 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Fri Apr 21 18:29:43 UTC 2023

Modified Files:
src/sys/dev/dkwedge: dk.c

Log Message:
dk(4): Require dk_openlock in dk_set_geometry.

Not strictly necessary but this makes reasoning easier and documents
with an assertion how disk_set_info is serialized.


To generate a diff of this commit:
cvs rdiff -u -r1.139 -r1.140 src/sys/dev/dkwedge/dk.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/dev/dkwedge/dk.c
diff -u src/sys/dev/dkwedge/dk.c:1.139 src/sys/dev/dkwedge/dk.c:1.140
--- src/sys/dev/dkwedge/dk.c:1.139	Fri Apr 21 18:29:33 2023
+++ src/sys/dev/dkwedge/dk.c	Fri Apr 21 18:29:43 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: dk.c,v 1.139 2023/04/21 18:29:33 riastradh Exp $	*/
+/*	$NetBSD: dk.c,v 1.140 2023/04/21 18:29:43 riastradh Exp $	*/
 
 /*-
  * Copyright (c) 2004, 2005, 2006, 2007 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: dk.c,v 1.139 2023/04/21 18:29:33 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: dk.c,v 1.140 2023/04/21 18:29:43 riastradh Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_dkwedge.h"
@@ -340,6 +340,8 @@ dk_set_geometry(struct dkwedge_softc *sc
 	struct disk *dk = >sc_dk;
 	struct disk_geom *dg = >dk_geom;
 
+	KASSERT(mutex_owned(>dk_openlock));
+
 	memset(dg, 0, sizeof(*dg));
 
 	dg->dg_secperunit = dkwedge_size(sc);
@@ -575,7 +577,9 @@ dkwedge_add(struct dkwedge_info *dkw)
 	 */
 
 	disk_init(>sc_dk, device_xname(sc->sc_dev), NULL);
+	mutex_enter(>dk_openlock);
 	dk_set_geometry(sc, pdk);
+	mutex_exit(>dk_openlock);
 	disk_attach(>sc_dk);
 
 	/* Disk wedge is ready for use! */



CVS commit: src/sys/dev/dkwedge

2023-04-21 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Fri Apr 21 18:29:43 UTC 2023

Modified Files:
src/sys/dev/dkwedge: dk.c

Log Message:
dk(4): Require dk_openlock in dk_set_geometry.

Not strictly necessary but this makes reasoning easier and documents
with an assertion how disk_set_info is serialized.


To generate a diff of this commit:
cvs rdiff -u -r1.139 -r1.140 src/sys/dev/dkwedge/dk.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/sys/dev/dkwedge

2023-04-21 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Fri Apr 21 18:29:34 UTC 2023

Modified Files:
src/sys/dev/dkwedge: dk.c

Log Message:
dk(4): Assert dkwedges[unit] is the sc we're about to free.


To generate a diff of this commit:
cvs rdiff -u -r1.138 -r1.139 src/sys/dev/dkwedge/dk.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/dev/dkwedge/dk.c
diff -u src/sys/dev/dkwedge/dk.c:1.138 src/sys/dev/dkwedge/dk.c:1.139
--- src/sys/dev/dkwedge/dk.c:1.138	Fri Apr 21 18:29:26 2023
+++ src/sys/dev/dkwedge/dk.c	Fri Apr 21 18:29:33 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: dk.c,v 1.138 2023/04/21 18:29:26 riastradh Exp $	*/
+/*	$NetBSD: dk.c,v 1.139 2023/04/21 18:29:33 riastradh Exp $	*/
 
 /*-
  * Copyright (c) 2004, 2005, 2006, 2007 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: dk.c,v 1.138 2023/04/21 18:29:26 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: dk.c,v 1.139 2023/04/21 18:29:33 riastradh Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_dkwedge.h"
@@ -552,6 +552,7 @@ dkwedge_add(struct dkwedge_info *dkw)
 		sc->sc_cfdata.cf_name, sc->sc_cfdata.cf_unit);
 
 		rw_enter(_lock, RW_WRITER);
+		KASSERT(dkwedges[sc->sc_cfdata.cf_unit] == sc);
 		dkwedges[sc->sc_cfdata.cf_unit] = NULL;
 		rw_exit(_lock);
 
@@ -752,6 +753,7 @@ dkwedge_detach(device_t self, int flags)
 
 	/* Poof. */
 	rw_enter(_lock, RW_WRITER);
+	KASSERT(dkwedges[unit] == sc);
 	dkwedges[unit] = NULL;
 	sc->sc_state = DKW_STATE_DEAD;
 	rw_exit(_lock);



CVS commit: src/sys/dev/dkwedge

2023-04-21 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Fri Apr 21 18:29:34 UTC 2023

Modified Files:
src/sys/dev/dkwedge: dk.c

Log Message:
dk(4): Assert dkwedges[unit] is the sc we're about to free.


To generate a diff of this commit:
cvs rdiff -u -r1.138 -r1.139 src/sys/dev/dkwedge/dk.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/sys/dev/dkwedge

2023-04-21 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Fri Apr 21 18:29:26 UTC 2023

Modified Files:
src/sys/dev/dkwedge: dk.c

Log Message:
dk(4): Assert parent vp is nonnull before we stash it away.

Let's enable early attribution if this goes wrong.

If it's not the parent's first open, also assert the parent vp is
already nonnull.


To generate a diff of this commit:
cvs rdiff -u -r1.137 -r1.138 src/sys/dev/dkwedge/dk.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/dev/dkwedge/dk.c
diff -u src/sys/dev/dkwedge/dk.c:1.137 src/sys/dev/dkwedge/dk.c:1.138
--- src/sys/dev/dkwedge/dk.c:1.137	Fri Apr 21 18:29:18 2023
+++ src/sys/dev/dkwedge/dk.c	Fri Apr 21 18:29:26 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: dk.c,v 1.137 2023/04/21 18:29:18 riastradh Exp $	*/
+/*	$NetBSD: dk.c,v 1.138 2023/04/21 18:29:26 riastradh Exp $	*/
 
 /*-
  * Copyright (c) 2004, 2005, 2006, 2007 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: dk.c,v 1.137 2023/04/21 18:29:18 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: dk.c,v 1.138 2023/04/21 18:29:26 riastradh Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_dkwedge.h"
@@ -1288,6 +1288,7 @@ dkfirstopen(struct dkwedge_softc *sc, in
 		}
 		if (error)
 			return error;
+		KASSERT(vp != NULL);
 		sc->sc_parent->dk_rawvp = vp;
 	} else {
 		/*
@@ -1299,6 +1300,7 @@ dkfirstopen(struct dkwedge_softc *sc, in
 		 * dk_rawopens is unsigned, this can't overflow.
 		 */
 		KASSERT(sc->sc_parent->dk_rawopens < UINT_MAX);
+		KASSERT(sc->sc_parent->dk_rawvp != NULL);
 		mode = 0;
 		LIST_FOREACH(nsc, >sc_parent->dk_wedges, sc_plink) {
 			if (nsc == sc || nsc->sc_dk.dk_openmask == 0)



CVS commit: src/sys/dev/dkwedge

2023-04-21 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Fri Apr 21 18:29:18 UTC 2023

Modified Files:
src/sys/dev/dkwedge: dk.c

Log Message:
dk(4): Don't touch dkwedges or ndkwedges outside dkwedges_lock.


To generate a diff of this commit:
cvs rdiff -u -r1.136 -r1.137 src/sys/dev/dkwedge/dk.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/dev/dkwedge/dk.c
diff -u src/sys/dev/dkwedge/dk.c:1.136 src/sys/dev/dkwedge/dk.c:1.137
--- src/sys/dev/dkwedge/dk.c:1.136	Fri Apr 21 18:26:35 2023
+++ src/sys/dev/dkwedge/dk.c	Fri Apr 21 18:29:18 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: dk.c,v 1.136 2023/04/21 18:26:35 riastradh Exp $	*/
+/*	$NetBSD: dk.c,v 1.137 2023/04/21 18:29:18 riastradh Exp $	*/
 
 /*-
  * Copyright (c) 2004, 2005, 2006, 2007 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: dk.c,v 1.136 2023/04/21 18:26:35 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: dk.c,v 1.137 2023/04/21 18:29:18 riastradh Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_dkwedge.h"
@@ -624,7 +624,7 @@ dkwedge_find(struct dkwedge_info *dkw, u
 		}
 	}
 	rw_exit(_lock);
-	if (unit == ndkwedges)
+	if (sc == NULL)
 		return NULL;
 
 	if (unitp != NULL)
@@ -1159,14 +1159,17 @@ dkwedge_read(struct disk *pdk, struct vn
 static struct dkwedge_softc *
 dkwedge_lookup(dev_t dev)
 {
-	int unit = minor(dev);
-
-	if (unit >= ndkwedges)
-		return NULL;
+	const int unit = minor(dev);
+	struct dkwedge_softc *sc;
 
-	KASSERT(dkwedges != NULL);
+	rw_enter(_lock, RW_READER);
+	if (unit < 0 || unit >= ndkwedges)
+		sc = NULL;
+	else
+		sc = dkwedges[unit];
+	rw_exit(_lock);
 
-	return dkwedges[unit];
+	return sc;
 }
 
 static int



CVS commit: src/sys/dev/dkwedge

2023-04-21 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Fri Apr 21 18:29:26 UTC 2023

Modified Files:
src/sys/dev/dkwedge: dk.c

Log Message:
dk(4): Assert parent vp is nonnull before we stash it away.

Let's enable early attribution if this goes wrong.

If it's not the parent's first open, also assert the parent vp is
already nonnull.


To generate a diff of this commit:
cvs rdiff -u -r1.137 -r1.138 src/sys/dev/dkwedge/dk.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/sys/dev/dkwedge

2023-04-21 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Fri Apr 21 18:29:18 UTC 2023

Modified Files:
src/sys/dev/dkwedge: dk.c

Log Message:
dk(4): Don't touch dkwedges or ndkwedges outside dkwedges_lock.


To generate a diff of this commit:
cvs rdiff -u -r1.136 -r1.137 src/sys/dev/dkwedge/dk.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/sys/dev/dkwedge

2023-04-21 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Fri Apr 21 18:26:36 UTC 2023

Modified Files:
src/sys/dev/dkwedge: dk.c

Log Message:
dk(4): Move CFDRIVER_DECL and CFATTACH_DECL3_NEW earlier in file.

Follows the pattern of most drivers, and will be necessary for
referencing dk_cd in dk_bdevsw and dk_cdevsw soon, to prevent
open/detach races.

No functional change intended.


To generate a diff of this commit:
cvs rdiff -u -r1.135 -r1.136 src/sys/dev/dkwedge/dk.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/sys/dev/dkwedge

2023-04-21 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Fri Apr 21 18:26:36 UTC 2023

Modified Files:
src/sys/dev/dkwedge: dk.c

Log Message:
dk(4): Move CFDRIVER_DECL and CFATTACH_DECL3_NEW earlier in file.

Follows the pattern of most drivers, and will be necessary for
referencing dk_cd in dk_bdevsw and dk_cdevsw soon, to prevent
open/detach races.

No functional change intended.


To generate a diff of this commit:
cvs rdiff -u -r1.135 -r1.136 src/sys/dev/dkwedge/dk.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/dev/dkwedge/dk.c
diff -u src/sys/dev/dkwedge/dk.c:1.135 src/sys/dev/dkwedge/dk.c:1.136
--- src/sys/dev/dkwedge/dk.c:1.135	Fri Apr 21 18:25:49 2023
+++ src/sys/dev/dkwedge/dk.c	Fri Apr 21 18:26:35 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: dk.c,v 1.135 2023/04/21 18:25:49 riastradh Exp $	*/
+/*	$NetBSD: dk.c,v 1.136 2023/04/21 18:26:35 riastradh Exp $	*/
 
 /*-
  * Copyright (c) 2004, 2005, 2006, 2007 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: dk.c,v 1.135 2023/04/21 18:25:49 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: dk.c,v 1.136 2023/04/21 18:26:35 riastradh Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_dkwedge.h"
@@ -96,6 +96,10 @@ struct dkwedge_softc {
 	int		sc_mode;	/* parent open mode */
 };
 
+static int	dkwedge_match(device_t, cfdata_t, void *);
+static void	dkwedge_attach(device_t, device_t, void *);
+static int	dkwedge_detach(device_t, int);
+
 static void	dkstart(struct dkwedge_softc *);
 static void	dkiodone(struct buf *);
 static void	dkrestart(void *);
@@ -120,6 +124,11 @@ static dev_type_dump(dkdump);
 static dev_type_size(dksize);
 static dev_type_discard(dkdiscard);
 
+CFDRIVER_DECL(dk, DV_DISK, NULL);
+CFATTACH_DECL3_NEW(dk, 0,
+dkwedge_match, dkwedge_attach, dkwedge_detach, NULL, NULL, NULL,
+DVF_DETACH_SHUTDOWN);
+
 const struct bdevsw dk_bdevsw = {
 	.d_open = dkopen,
 	.d_close = dkclose,
@@ -179,11 +188,6 @@ dkwedge_attach(device_t parent, device_t
 		aprint_error_dev(self, "couldn't establish power handler\n");
 }
 
-CFDRIVER_DECL(dk, DV_DISK, NULL);
-CFATTACH_DECL3_NEW(dk, 0,
-dkwedge_match, dkwedge_attach, dkwedge_detach, NULL, NULL, NULL,
-DVF_DETACH_SHUTDOWN);
-
 /*
  * dkwedge_wait_drain:
  *



CVS commit: src/sys/dev/dkwedge

2023-04-21 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Fri Apr 21 18:25:49 UTC 2023

Modified Files:
src/sys/dev/dkwedge: dk.c

Log Message:
dk(4): Prevent races in access to struct dkwedge_softc::sc_size.

Rules:

1. Only ever increases, never decreases.

   (Decreases require removing and readding the wedge.)

2. Increases are serialized by dk_openlock.

3. Reads can happen unlocked in any context where the softc is valid.

Access is gathered into dkwedge_size* subroutines -- don't touch
sc_size outside these.  For now, we use rwlock(9) to keep the
reasoning simple.  This should be done with atomics on 64-bit
platforms and a seqlock on 32-bit platforms to avoid contention.
However, we can do that in a later change.


To generate a diff of this commit:
cvs rdiff -u -r1.134 -r1.135 src/sys/dev/dkwedge/dk.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/dev/dkwedge/dk.c
diff -u src/sys/dev/dkwedge/dk.c:1.134 src/sys/dev/dkwedge/dk.c:1.135
--- src/sys/dev/dkwedge/dk.c:1.134	Fri Apr 21 18:25:30 2023
+++ src/sys/dev/dkwedge/dk.c	Fri Apr 21 18:25:49 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: dk.c,v 1.134 2023/04/21 18:25:30 riastradh Exp $	*/
+/*	$NetBSD: dk.c,v 1.135 2023/04/21 18:25:49 riastradh Exp $	*/
 
 /*-
  * Copyright (c) 2004, 2005, 2006, 2007 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: dk.c,v 1.134 2023/04/21 18:25:30 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: dk.c,v 1.135 2023/04/21 18:25:49 riastradh Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_dkwedge.h"
@@ -79,6 +79,7 @@ struct dkwedge_softc {
 
 	struct disk	*sc_parent;	/* parent disk */
 	daddr_t		sc_offset;	/* LBA offset of wedge in parent */
+	krwlock_t	sc_sizelock;
 	uint64_t	sc_size;	/* size of wedge in blocks */
 	char		sc_ptype[32];	/* partition type */
 	dev_t		sc_pdev;	/* cached parent's dev_t */
@@ -289,6 +290,47 @@ out:	rw_exit(_lock);
 }
 
 static void
+dkwedge_size_init(struct dkwedge_softc *sc, uint64_t size)
+{
+
+	rw_init(>sc_sizelock);
+	sc->sc_size = size;
+}
+
+static void
+dkwedge_size_fini(struct dkwedge_softc *sc)
+{
+
+	rw_destroy(>sc_sizelock);
+}
+
+static uint64_t
+dkwedge_size(struct dkwedge_softc *sc)
+{
+	uint64_t size;
+
+	rw_enter(>sc_sizelock, RW_READER);
+	size = sc->sc_size;
+	rw_exit(>sc_sizelock);
+
+	return size;
+}
+
+static void
+dkwedge_size_increase(struct dkwedge_softc *sc, uint64_t size)
+{
+
+	KASSERT(mutex_owned(>sc_dk.dk_openlock));
+
+	rw_enter(>sc_sizelock, RW_WRITER);
+	KASSERTMSG(size >= sc->sc_size,
+	"decreasing dkwedge size from %"PRIu64" to %"PRIu64,
+	sc->sc_size, size);
+	sc->sc_size = size;
+	rw_exit(>sc_sizelock);
+}
+
+static void
 dk_set_geometry(struct dkwedge_softc *sc, struct disk *pdk)
 {
 	struct disk *dk = >sc_dk;
@@ -296,7 +338,7 @@ dk_set_geometry(struct dkwedge_softc *sc
 
 	memset(dg, 0, sizeof(*dg));
 
-	dg->dg_secperunit = sc->sc_size;
+	dg->dg_secperunit = dkwedge_size(sc);
 	dg->dg_secsize = DEV_BSIZE << pdk->dk_blkshift;
 
 	/* fake numbers, 1 cylinder is 1 MB with default sector size */
@@ -351,11 +393,11 @@ dkwedge_add(struct dkwedge_info *dkw)
 			break;
 		if (strcmp(lsc->sc_ptype, dkw->dkw_ptype) != 0)
 			break;
-		if (lsc->sc_size > dkw->dkw_size)
+		if (dkwedge_size(lsc) > dkw->dkw_size)
 			break;
 
 		sc = lsc;
-		sc->sc_size = dkw->dkw_size;
+		dkwedge_size_increase(sc, dkw->dkw_size);
 		dk_set_geometry(sc, pdk);
 
 		break;
@@ -370,7 +412,7 @@ dkwedge_add(struct dkwedge_info *dkw)
 	sc->sc_parent = pdk;
 	sc->sc_pdev = pdev;
 	sc->sc_offset = dkw->dkw_offset;
-	sc->sc_size = dkw->dkw_size;
+	dkwedge_size_init(sc, dkw->dkw_size);
 
 	memcpy(sc->sc_wname, dkw->dkw_wname, sizeof(sc->sc_wname));
 	sc->sc_wname[sizeof(sc->sc_wname) - 1] = '\0';
@@ -396,8 +438,11 @@ dkwedge_add(struct dkwedge_info *dkw)
 	else {
 		/* Check for wedge overlap. */
 		LIST_FOREACH(lsc, >dk_wedges, sc_plink) {
-			daddr_t lastblk = sc->sc_offset + sc->sc_size - 1;
-			daddr_t llastblk = lsc->sc_offset + lsc->sc_size - 1;
+			/* XXX arithmetic overflow */
+			uint64_t size = dkwedge_size(sc);
+			uint64_t lsize = dkwedge_size(lsc);
+			daddr_t lastblk = sc->sc_offset + size - 1;
+			daddr_t llastblk = lsc->sc_offset + lsize - 1;
 
 			if (sc->sc_offset >= lsc->sc_offset &&
 			sc->sc_offset <= llastblk) {
@@ -412,7 +457,7 @@ dkwedge_add(struct dkwedge_info *dkw)
 		}
 		if (lsc != NULL) {
 			if (sc->sc_offset == lsc->sc_offset &&
-			sc->sc_size == lsc->sc_size &&
+			dkwedge_size(sc) == dkwedge_size(lsc) &&
 			strcmp(sc->sc_wname, lsc->sc_wname) == 0)
 error = EEXIST;
 			else
@@ -427,6 +472,7 @@ dkwedge_add(struct dkwedge_info *dkw)
 		cv_destroy(>sc_dkdrn);
 		mutex_destroy(>sc_iolock);
 		bufq_free(sc->sc_bufq);
+		dkwedge_size_fini(sc);
 		free(sc, M_DKWEDGE);
 		return error;
 	}
@@ -484,6 +530,7 @@ dkwedge_add(struct dkwedge_info *dkw)
 		cv_destroy(>sc_dkdrn);
 		mutex_destroy(>sc_iolock);
 		

CVS commit: src/sys/dev/dkwedge

2023-04-21 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Fri Apr 21 18:25:49 UTC 2023

Modified Files:
src/sys/dev/dkwedge: dk.c

Log Message:
dk(4): Prevent races in access to struct dkwedge_softc::sc_size.

Rules:

1. Only ever increases, never decreases.

   (Decreases require removing and readding the wedge.)

2. Increases are serialized by dk_openlock.

3. Reads can happen unlocked in any context where the softc is valid.

Access is gathered into dkwedge_size* subroutines -- don't touch
sc_size outside these.  For now, we use rwlock(9) to keep the
reasoning simple.  This should be done with atomics on 64-bit
platforms and a seqlock on 32-bit platforms to avoid contention.
However, we can do that in a later change.


To generate a diff of this commit:
cvs rdiff -u -r1.134 -r1.135 src/sys/dev/dkwedge/dk.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/sys/dev/dkwedge

2023-04-21 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Fri Apr 21 18:25:30 UTC 2023

Modified Files:
src/sys/dev/dkwedge: dk.c

Log Message:
dk(4):  for rwlock(9).


To generate a diff of this commit:
cvs rdiff -u -r1.133 -r1.134 src/sys/dev/dkwedge/dk.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/sys/dev/dkwedge

2023-04-21 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Fri Apr 21 18:25:30 UTC 2023

Modified Files:
src/sys/dev/dkwedge: dk.c

Log Message:
dk(4):  for rwlock(9).


To generate a diff of this commit:
cvs rdiff -u -r1.133 -r1.134 src/sys/dev/dkwedge/dk.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/dev/dkwedge/dk.c
diff -u src/sys/dev/dkwedge/dk.c:1.133 src/sys/dev/dkwedge/dk.c:1.134
--- src/sys/dev/dkwedge/dk.c:1.133	Fri Apr 21 18:25:22 2023
+++ src/sys/dev/dkwedge/dk.c	Fri Apr 21 18:25:30 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: dk.c,v 1.133 2023/04/21 18:25:22 riastradh Exp $	*/
+/*	$NetBSD: dk.c,v 1.134 2023/04/21 18:25:30 riastradh Exp $	*/
 
 /*-
  * Copyright (c) 2004, 2005, 2006, 2007 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: dk.c,v 1.133 2023/04/21 18:25:22 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: dk.c,v 1.134 2023/04/21 18:25:30 riastradh Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_dkwedge.h"
@@ -54,6 +54,7 @@ __KERNEL_RCSID(0, "$NetBSD: dk.c,v 1.133
 #include 
 #include 
 #include 
+#include 
 #include 
 #include 
 #include 



CVS commit: src/sys/dev/dkwedge

2023-04-21 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Fri Apr 21 18:25:22 UTC 2023

Modified Files:
src/sys/dev/dkwedge: dk.c

Log Message:
dk(4): KNF: Sort includes.

No functional change intended.


To generate a diff of this commit:
cvs rdiff -u -r1.132 -r1.133 src/sys/dev/dkwedge/dk.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/dev/dkwedge/dk.c
diff -u src/sys/dev/dkwedge/dk.c:1.132 src/sys/dev/dkwedge/dk.c:1.133
--- src/sys/dev/dkwedge/dk.c:1.132	Fri Apr 21 18:25:09 2023
+++ src/sys/dev/dkwedge/dk.c	Fri Apr 21 18:25:22 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: dk.c,v 1.132 2023/04/21 18:25:09 riastradh Exp $	*/
+/*	$NetBSD: dk.c,v 1.133 2023/04/21 18:25:22 riastradh Exp $	*/
 
 /*-
  * Copyright (c) 2004, 2005, 2006, 2007 The NetBSD Foundation, Inc.
@@ -30,31 +30,33 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: dk.c,v 1.132 2023/04/21 18:25:09 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: dk.c,v 1.133 2023/04/21 18:25:22 riastradh Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_dkwedge.h"
 #endif
 
 #include 
-#include 
-#include 
-#include 
-#include 
-#include 
-#include 
-#include 
-#include 
+#include 
+
 #include 
 #include 
-#include 
-#include 
-#include 
 #include 
-#include 
-#include 
+#include 
 #include 
+#include 
+#include 
+#include 
+#include 
+#include 
 #include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
 
 #include 
 



CVS commit: src/sys/dev/dkwedge

2023-04-21 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Fri Apr 21 18:25:22 UTC 2023

Modified Files:
src/sys/dev/dkwedge: dk.c

Log Message:
dk(4): KNF: Sort includes.

No functional change intended.


To generate a diff of this commit:
cvs rdiff -u -r1.132 -r1.133 src/sys/dev/dkwedge/dk.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/sys/dev/dkwedge

2023-04-21 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Fri Apr 21 18:25:09 UTC 2023

Modified Files:
src/sys/dev/dkwedge: dk.c

Log Message:
dk(4): ENXIO, not ENODEV, means no such device.

ENXIO is `device not configured', meaning there is no such device.

ENODEV is `operation not supported by device', meaning the device is
there but refuses the operation, like writing to a read-only medium.

Exception: For undefined ioctl commands, it's not ENODEV _or_ ENXIO,
but rather ENOTTY, because why make any of this obvious when you
could make it obscure Unix lore?


To generate a diff of this commit:
cvs rdiff -u -r1.131 -r1.132 src/sys/dev/dkwedge/dk.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/sys/dev/dkwedge

2023-04-21 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Fri Apr 21 18:25:09 UTC 2023

Modified Files:
src/sys/dev/dkwedge: dk.c

Log Message:
dk(4): ENXIO, not ENODEV, means no such device.

ENXIO is `device not configured', meaning there is no such device.

ENODEV is `operation not supported by device', meaning the device is
there but refuses the operation, like writing to a read-only medium.

Exception: For undefined ioctl commands, it's not ENODEV _or_ ENXIO,
but rather ENOTTY, because why make any of this obvious when you
could make it obscure Unix lore?


To generate a diff of this commit:
cvs rdiff -u -r1.131 -r1.132 src/sys/dev/dkwedge/dk.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/dev/dkwedge/dk.c
diff -u src/sys/dev/dkwedge/dk.c:1.131 src/sys/dev/dkwedge/dk.c:1.132
--- src/sys/dev/dkwedge/dk.c:1.131	Fri Apr 21 18:24:56 2023
+++ src/sys/dev/dkwedge/dk.c	Fri Apr 21 18:25:09 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: dk.c,v 1.131 2023/04/21 18:24:56 riastradh Exp $	*/
+/*	$NetBSD: dk.c,v 1.132 2023/04/21 18:25:09 riastradh Exp $	*/
 
 /*-
  * Copyright (c) 2004, 2005, 2006, 2007 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: dk.c,v 1.131 2023/04/21 18:24:56 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: dk.c,v 1.132 2023/04/21 18:25:09 riastradh Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_dkwedge.h"
@@ -221,14 +221,14 @@ dkwedge_compute_pdev(const char *pname, 
 		break;
 	}
 	if (pmaj == NODEVMAJOR)
-		return ENODEV;
+		return ENXIO;
 
 	name += strlen(devname);
 	for (cp = name, punit = 0; *cp >= '0' && *cp <= '9'; cp++)
 		punit = (punit * 10) + (*cp - '0');
 	if (cp == name) {
 		/* Invalid parent disk name. */
-		return ENODEV;
+		return ENXIO;
 	}
 
 	*pdevp = MAKEDISKDEV(pmaj, punit, RAW_PART);
@@ -325,7 +325,7 @@ dkwedge_add(struct dkwedge_info *dkw)
 	dkw->dkw_parent[sizeof(dkw->dkw_parent) - 1] = '\0';
 	pdk = disk_find(dkw->dkw_parent);
 	if (pdk == NULL)
-		return ENODEV;
+		return ENXIO;
 
 	error = dkwedge_compute_pdev(pdk->dk_name, , VBLK);
 	if (error)
@@ -989,7 +989,7 @@ dkwedge_discover(struct disk *pdk)
 
 	error = VOP_OPEN(vp, FREAD | FSILENT, NOCRED);
 	if (error) {
-		if (error != ENODEV)
+		if (error != ENXIO)
 			aprint_error("%s: unable to open device, error = %d\n",
 			pdk->dk_name, error);
 		vput(vp);
@@ -1169,7 +1169,7 @@ dkopen(dev_t dev, int flags, int fmt, st
 	int error = 0;
 
 	if (sc == NULL)
-		return ENODEV;
+		return ENXIO;
 	if (sc->sc_state != DKW_STATE_RUNNING)
 		return ENXIO;
 
@@ -1285,7 +1285,7 @@ dkclose(dev_t dev, int flags, int fmt, s
 	struct dkwedge_softc *sc = dkwedge_lookup(dev);
 
 	if (sc == NULL)
-		return ENODEV;
+		return ENXIO;
 	if (sc->sc_state != DKW_STATE_RUNNING)
 		return ENXIO;
 
@@ -1323,7 +1323,7 @@ dkstrategy(struct buf *bp)
 	uint64_t p_size, p_offset;
 
 	if (sc == NULL) {
-		bp->b_error = ENODEV;
+		bp->b_error = ENXIO;
 		goto done;
 	}
 
@@ -1523,7 +1523,7 @@ dkread(dev_t dev, struct uio *uio, int f
 	struct dkwedge_softc *sc = dkwedge_lookup(dev);
 
 	if (sc == NULL)
-		return ENODEV;
+		return ENXIO;
 	if (sc->sc_state != DKW_STATE_RUNNING)
 		return ENXIO;
 
@@ -1541,7 +1541,7 @@ dkwrite(dev_t dev, struct uio *uio, int 
 	struct dkwedge_softc *sc = dkwedge_lookup(dev);
 
 	if (sc == NULL)
-		return ENODEV;
+		return ENXIO;
 	if (sc->sc_state != DKW_STATE_RUNNING)
 		return ENXIO;
 
@@ -1560,7 +1560,7 @@ dkioctl(dev_t dev, u_long cmd, void *dat
 	int error = 0;
 
 	if (sc == NULL)
-		return ENODEV;
+		return ENXIO;
 	if (sc->sc_state != DKW_STATE_RUNNING)
 		return ENXIO;
 	if (sc->sc_parent->dk_rawvp == NULL)
@@ -1636,7 +1636,7 @@ dkdiscard(dev_t dev, off_t pos, off_t le
 	int error;
 
 	if (sc == NULL)
-		return ENODEV;
+		return ENXIO;
 	if (sc->sc_state != DKW_STATE_RUNNING)
 		return ENXIO;
 	if (sc->sc_parent->dk_rawvp == NULL)
@@ -1717,7 +1717,7 @@ dkdump(dev_t dev, daddr_t blkno, void *v
 	int rv = 0;
 
 	if (sc == NULL)
-		return ENODEV;
+		return ENXIO;
 	if (sc->sc_state != DKW_STATE_RUNNING)
 		return ENXIO;
 



CVS commit: src/sys/dev/dkwedge

2023-04-21 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Fri Apr 21 18:24:56 UTC 2023

Modified Files:
src/sys/dev/dkwedge: dk.c

Log Message:
dk(4): Fix typo in comment: dkstrategy, not dkstragegy.

No functional change intended.


To generate a diff of this commit:
cvs rdiff -u -r1.130 -r1.131 src/sys/dev/dkwedge/dk.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/sys/dev/dkwedge

2023-04-21 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Fri Apr 21 18:24:56 UTC 2023

Modified Files:
src/sys/dev/dkwedge: dk.c

Log Message:
dk(4): Fix typo in comment: dkstrategy, not dkstragegy.

No functional change intended.


To generate a diff of this commit:
cvs rdiff -u -r1.130 -r1.131 src/sys/dev/dkwedge/dk.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/dev/dkwedge/dk.c
diff -u src/sys/dev/dkwedge/dk.c:1.130 src/sys/dev/dkwedge/dk.c:1.131
--- src/sys/dev/dkwedge/dk.c:1.130	Fri Apr 21 18:24:47 2023
+++ src/sys/dev/dkwedge/dk.c	Fri Apr 21 18:24:56 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: dk.c,v 1.130 2023/04/21 18:24:47 riastradh Exp $	*/
+/*	$NetBSD: dk.c,v 1.131 2023/04/21 18:24:56 riastradh Exp $	*/
 
 /*-
  * Copyright (c) 2004, 2005, 2006, 2007 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: dk.c,v 1.130 2023/04/21 18:24:47 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: dk.c,v 1.131 2023/04/21 18:24:56 riastradh Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_dkwedge.h"
@@ -1312,7 +1312,7 @@ dkclose(dev_t dev, int flags, int fmt, s
 }
 
 /*
- * dkstragegy:		[devsw entry point]
+ * dkstrategy:		[devsw entry point]
  *
  *	Perform I/O based on the wedge I/O strategy.
  */



CVS commit: src/sys/dev/dkwedge

2023-04-21 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Fri Apr 21 18:24:48 UTC 2023

Modified Files:
src/sys/dev/dkwedge: dk.c

Log Message:
dk(4): Omit needless void * cast.

No functional change intended.


To generate a diff of this commit:
cvs rdiff -u -r1.129 -r1.130 src/sys/dev/dkwedge/dk.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/dev/dkwedge/dk.c
diff -u src/sys/dev/dkwedge/dk.c:1.129 src/sys/dev/dkwedge/dk.c:1.130
--- src/sys/dev/dkwedge/dk.c:1.129	Fri Apr 21 18:24:39 2023
+++ src/sys/dev/dkwedge/dk.c	Fri Apr 21 18:24:47 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: dk.c,v 1.129 2023/04/21 18:24:39 riastradh Exp $	*/
+/*	$NetBSD: dk.c,v 1.130 2023/04/21 18:24:47 riastradh Exp $	*/
 
 /*-
  * Copyright (c) 2004, 2005, 2006, 2007 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: dk.c,v 1.129 2023/04/21 18:24:39 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: dk.c,v 1.130 2023/04/21 18:24:47 riastradh Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_dkwedge.h"
@@ -1584,7 +1584,7 @@ dkioctl(dev_t dev, u_long cmd, void *dat
 		l != NULL ? l->l_cred : NOCRED);
 		break;
 	case DIOCGWEDGEINFO: {
-		struct dkwedge_info *dkw = (void *) data;
+		struct dkwedge_info *dkw = data;
 
 		strlcpy(dkw->dkw_devname, device_xname(sc->sc_dev),
 		sizeof(dkw->dkw_devname));



CVS commit: src/sys/dev/dkwedge

2023-04-21 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Fri Apr 21 18:24:48 UTC 2023

Modified Files:
src/sys/dev/dkwedge: dk.c

Log Message:
dk(4): Omit needless void * cast.

No functional change intended.


To generate a diff of this commit:
cvs rdiff -u -r1.129 -r1.130 src/sys/dev/dkwedge/dk.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/sys/dev/dkwedge

2023-04-21 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Fri Apr 21 18:24:39 UTC 2023

Modified Files:
src/sys/dev/dkwedge: dk.c

Log Message:
dk(4): KNF: Whitespace.

No functional change intended.


To generate a diff of this commit:
cvs rdiff -u -r1.128 -r1.129 src/sys/dev/dkwedge/dk.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/sys/dev/dkwedge

2023-04-21 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Fri Apr 21 18:24:39 UTC 2023

Modified Files:
src/sys/dev/dkwedge: dk.c

Log Message:
dk(4): KNF: Whitespace.

No functional change intended.


To generate a diff of this commit:
cvs rdiff -u -r1.128 -r1.129 src/sys/dev/dkwedge/dk.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/dev/dkwedge/dk.c
diff -u src/sys/dev/dkwedge/dk.c:1.128 src/sys/dev/dkwedge/dk.c:1.129
--- src/sys/dev/dkwedge/dk.c:1.128	Fri Apr 21 18:24:31 2023
+++ src/sys/dev/dkwedge/dk.c	Fri Apr 21 18:24:39 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: dk.c,v 1.128 2023/04/21 18:24:31 riastradh Exp $	*/
+/*	$NetBSD: dk.c,v 1.129 2023/04/21 18:24:39 riastradh Exp $	*/
 
 /*-
  * Copyright (c) 2004, 2005, 2006, 2007 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: dk.c,v 1.128 2023/04/21 18:24:31 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: dk.c,v 1.129 2023/04/21 18:24:39 riastradh Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_dkwedge.h"
@@ -155,8 +155,7 @@ static krwlock_t dkwedge_discovery_metho
  *	Autoconfiguration match function for pseudo-device glue.
  */
 static int
-dkwedge_match(device_t parent, cfdata_t match,
-void *aux)
+dkwedge_match(device_t parent, cfdata_t match, void *aux)
 {
 
 	/* Pseudo-device; always present. */
@@ -169,8 +168,7 @@ dkwedge_match(device_t parent, cfdata_t 
  *	Autoconfiguration attach function for pseudo-device glue.
  */
 static void
-dkwedge_attach(device_t parent, device_t self,
-void *aux)
+dkwedge_attach(device_t parent, device_t self, void *aux)
 {
 
 	if (!pmf_device_register(self, NULL, NULL))
@@ -301,7 +299,8 @@ dk_set_geometry(struct dkwedge_softc *sc
 	/* fake numbers, 1 cylinder is 1 MB with default sector size */
 	dg->dg_nsectors = 32;
 	dg->dg_ntracks = 64;
-	dg->dg_ncylinders = dg->dg_secperunit / (dg->dg_nsectors * dg->dg_ntracks);
+	dg->dg_ncylinders =
+	dg->dg_secperunit / (dg->dg_nsectors * dg->dg_ntracks);
 
 	disk_set_info(sc->sc_dev, dk, NULL);
 }
@@ -453,7 +452,7 @@ dkwedge_add(struct dkwedge_info *dkw)
 			} else {
 /* XXX Unicode. */
 if (strcmp(dkwedges[unit]->sc_wname,
-	   sc->sc_wname) == 0) {
+	sc->sc_wname) == 0) {
 	error = EEXIST;
 	break;
 }
@@ -537,7 +536,7 @@ announce:
 
 	/* Return the devname to the caller. */
 	strlcpy(dkw->dkw_devname, device_xname(sc->sc_dev),
-		sizeof(dkw->dkw_devname));
+	sizeof(dkw->dkw_devname));
 
 	return 0;
 }
@@ -589,6 +588,7 @@ dkwedge_find(struct dkwedge_info *dkw, u
 int
 dkwedge_del(struct dkwedge_info *dkw)
 {
+
 	return dkwedge_del1(dkw, 0);
 }
 
@@ -718,6 +718,7 @@ dkwedge_detach(device_t self, int flags)
 void
 dkwedge_delall(struct disk *pdk)
 {
+
 	dkwedge_delall1(pdk, false);
 }
 
@@ -729,7 +730,8 @@ dkwedge_delall1(struct disk *pdk, bool i
 	int flags;
 
 	flags = DETACH_QUIET;
-	if (!idleonly) flags |= DETACH_FORCE;
+	if (!idleonly)
+		flags |= DETACH_FORCE;
 
 	for (;;) {
 		mutex_enter(>dk_openlock);
@@ -744,7 +746,7 @@ dkwedge_delall1(struct disk *pdk, bool i
 		}
 		strlcpy(dkw.dkw_parent, pdk->dk_name, sizeof(dkw.dkw_parent));
 		strlcpy(dkw.dkw_devname, device_xname(sc->sc_dev),
-			sizeof(dkw.dkw_devname));
+		sizeof(dkw.dkw_devname));
 		mutex_exit(>dk_openlock);
 		(void) dkwedge_del1(, flags);
 	}
@@ -786,7 +788,7 @@ dkwedge_list(struct disk *pdk, struct dk
 			continue;
 
 		strlcpy(dkw.dkw_devname, device_xname(sc->sc_dev),
-			sizeof(dkw.dkw_devname));
+		sizeof(dkw.dkw_devname));
 		memcpy(dkw.dkw_wname, sc->sc_wname, sizeof(dkw.dkw_wname));
 		dkw.dkw_wname[sizeof(dkw.dkw_wname) - 1] = '\0';
 		strlcpy(dkw.dkw_parent, sc->sc_parent->dk_name,
@@ -835,6 +837,7 @@ dkwedge_find_by_wname(const char *wname)
 device_t
 dkwedge_find_by_parent(const char *name, size_t *i)
 {
+
 	rw_enter(_lock, RW_WRITER);
 	for (; *i < (size_t)ndkwedges; (*i)++) {
 		struct dkwedge_softc *sc;
@@ -901,7 +904,7 @@ dkwedge_init(void)
 			continue;
 		if (LIST_EMPTY(_discovery_methods)) {
 			LIST_INSERT_HEAD(_discovery_methods,
-	 ddm, ddm_list);
+			ddm, ddm_list);
 			continue;
 		}
 		LIST_FOREACH(lddm, _discovery_methods, ddm_list) {
@@ -1019,7 +1022,7 @@ dkwedge_discover(struct disk *pdk)
 		/* We'll just assume the vnode has been cleaned up. */
 	}
 
- out:
+out:
 	rw_exit(_discovery_methods_lock);
 }
 
@@ -1195,7 +1198,7 @@ dkopen(dev_t dev, int flags, int fmt, st
 	sc->sc_dk.dk_openmask =
 	sc->sc_dk.dk_copenmask | sc->sc_dk.dk_bopenmask;
 
- popen_fail:
+popen_fail:
 	mutex_exit(>sc_parent->dk_rawlock);
 	mutex_exit(>sc_dk.dk_openlock);
 	return error;
@@ -1354,7 +1357,7 @@ dkstrategy(struct buf *bp)
 	dkstart(sc);
 	return;
 
- done:
+done:
 	bp->b_resid = bp->b_bcount;
 	biodone(bp);
 }
@@ -1396,7 +1399,7 @@ dkstart(struct dkwedge_softc *sc)
 			 * buffer queued up, and schedule a timer to
 			 * restart the queue in 1/2 a second.
 			 */
-			

CVS commit: src/sys/dev/dkwedge

2023-04-21 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Fri Apr 21 18:24:32 UTC 2023

Modified Files:
src/sys/dev/dkwedge: dk.c

Log Message:
dk(4): KNF: return (v) -> return v.

No functional change intended.


To generate a diff of this commit:
cvs rdiff -u -r1.127 -r1.128 src/sys/dev/dkwedge/dk.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/dev/dkwedge/dk.c
diff -u src/sys/dev/dkwedge/dk.c:1.127 src/sys/dev/dkwedge/dk.c:1.128
--- src/sys/dev/dkwedge/dk.c:1.127	Fri Apr 21 18:24:19 2023
+++ src/sys/dev/dkwedge/dk.c	Fri Apr 21 18:24:31 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: dk.c,v 1.127 2023/04/21 18:24:19 riastradh Exp $	*/
+/*	$NetBSD: dk.c,v 1.128 2023/04/21 18:24:31 riastradh Exp $	*/
 
 /*-
  * Copyright (c) 2004, 2005, 2006, 2007 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: dk.c,v 1.127 2023/04/21 18:24:19 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: dk.c,v 1.128 2023/04/21 18:24:31 riastradh Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_dkwedge.h"
@@ -160,7 +160,7 @@ dkwedge_match(device_t parent, cfdata_t 
 {
 
 	/* Pseudo-device; always present. */
-	return (1);
+	return 1;
 }
 
 /*
@@ -223,19 +223,19 @@ dkwedge_compute_pdev(const char *pname, 
 		break;
 	}
 	if (pmaj == NODEVMAJOR)
-		return (ENODEV);
+		return ENODEV;
 
 	name += strlen(devname);
 	for (cp = name, punit = 0; *cp >= '0' && *cp <= '9'; cp++)
 		punit = (punit * 10) + (*cp - '0');
 	if (cp == name) {
 		/* Invalid parent disk name. */
-		return (ENODEV);
+		return ENODEV;
 	}
 
 	*pdevp = MAKEDISKDEV(pmaj, punit, RAW_PART);
 
-	return (0);
+	return 0;
 }
 
 /*
@@ -326,14 +326,14 @@ dkwedge_add(struct dkwedge_info *dkw)
 	dkw->dkw_parent[sizeof(dkw->dkw_parent) - 1] = '\0';
 	pdk = disk_find(dkw->dkw_parent);
 	if (pdk == NULL)
-		return (ENODEV);
+		return ENODEV;
 
 	error = dkwedge_compute_pdev(pdk->dk_name, , VBLK);
 	if (error)
-		return (error);
+		return error;
 
 	if (dkw->dkw_offset < 0)
-		return (EINVAL);
+		return EINVAL;
 
 	/*
 	 * Check for an existing wedge at the same disk offset. Allow
@@ -426,7 +426,7 @@ dkwedge_add(struct dkwedge_info *dkw)
 		mutex_destroy(>sc_iolock);
 		bufq_free(sc->sc_bufq);
 		free(sc, M_DKWEDGE);
-		return (error);
+		return error;
 	}
 
 	/* Fill in our cfdata for the pseudo-device glue. */
@@ -483,7 +483,7 @@ dkwedge_add(struct dkwedge_info *dkw)
 		mutex_destroy(>sc_iolock);
 		bufq_free(sc->sc_bufq);
 		free(sc, M_DKWEDGE);
-		return (error);
+		return error;
 	}
 
 	/*
@@ -511,7 +511,7 @@ dkwedge_add(struct dkwedge_info *dkw)
 		mutex_destroy(>sc_iolock);
 		bufq_free(sc->sc_bufq);
 		free(sc, M_DKWEDGE);
-		return (ENOMEM);
+		return ENOMEM;
 	}
 
 	/*
@@ -539,7 +539,7 @@ announce:
 	strlcpy(dkw->dkw_devname, device_xname(sc->sc_dev),
 		sizeof(dkw->dkw_devname));
 
-	return (0);
+	return 0;
 }
 
 /*
@@ -599,7 +599,7 @@ dkwedge_del1(struct dkwedge_info *dkw, i
 
 	/* Find our softc. */
 	if ((sc = dkwedge_find(dkw, NULL)) == NULL)
-		return (ESRCH);
+		return ESRCH;
 
 	return config_detach(sc->sc_dev, flags);
 }
@@ -803,7 +803,7 @@ dkwedge_list(struct disk *pdk, struct dk
 	dkwl->dkwl_nwedges = pdk->dk_nwedges;
 	mutex_exit(>dk_openlock);
 
-	return (error);
+	return error;
 }
 
 device_t
@@ -1103,11 +1103,11 @@ dkwedge_lookup(dev_t dev)
 	int unit = minor(dev);
 
 	if (unit >= ndkwedges)
-		return (NULL);
+		return NULL;
 
 	KASSERT(dkwedges != NULL);
 
-	return (dkwedges[unit]);
+	return dkwedges[unit];
 }
 
 static int
@@ -1166,9 +1166,9 @@ dkopen(dev_t dev, int flags, int fmt, st
 	int error = 0;
 
 	if (sc == NULL)
-		return (ENODEV);
+		return ENODEV;
 	if (sc->sc_state != DKW_STATE_RUNNING)
-		return (ENXIO);
+		return ENXIO;
 
 	/*
 	 * We go through a complicated little dance to only open the parent
@@ -1198,7 +1198,7 @@ dkopen(dev_t dev, int flags, int fmt, st
  popen_fail:
 	mutex_exit(>sc_parent->dk_rawlock);
 	mutex_exit(>sc_dk.dk_openlock);
-	return (error);
+	return error;
 }
 
 static int
@@ -1520,11 +1520,11 @@ dkread(dev_t dev, struct uio *uio, int f
 	struct dkwedge_softc *sc = dkwedge_lookup(dev);
 
 	if (sc == NULL)
-		return (ENODEV);
+		return ENODEV;
 	if (sc->sc_state != DKW_STATE_RUNNING)
-		return (ENXIO);
+		return ENXIO;
 
-	return (physio(dkstrategy, NULL, dev, B_READ, dkminphys, uio));
+	return physio(dkstrategy, NULL, dev, B_READ, dkminphys, uio);
 }
 
 /*
@@ -1538,11 +1538,11 @@ dkwrite(dev_t dev, struct uio *uio, int 
 	struct dkwedge_softc *sc = dkwedge_lookup(dev);
 
 	if (sc == NULL)
-		return (ENODEV);
+		return ENODEV;
 	if (sc->sc_state != DKW_STATE_RUNNING)
-		return (ENXIO);
+		return ENXIO;
 
-	return (physio(dkstrategy, NULL, dev, B_WRITE, dkminphys, uio));
+	return physio(dkstrategy, NULL, dev, B_WRITE, dkminphys, uio);
 }
 
 /*
@@ -1557,11 +1557,11 @@ dkioctl(dev_t dev, u_long cmd, void *dat
 	int error = 0;
 
 	if (sc == NULL)
-		return (ENODEV);
+		return ENODEV;
 	if 

CVS commit: src/sys/dev/dkwedge

2023-04-21 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Fri Apr 21 18:24:32 UTC 2023

Modified Files:
src/sys/dev/dkwedge: dk.c

Log Message:
dk(4): KNF: return (v) -> return v.

No functional change intended.


To generate a diff of this commit:
cvs rdiff -u -r1.127 -r1.128 src/sys/dev/dkwedge/dk.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/sys/dev/dkwedge

2023-04-21 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Fri Apr 21 18:24:19 UTC 2023

Modified Files:
src/sys/dev/dkwedge: dk.c

Log Message:
dk(4): Avoid holding dkwedges_lock while allocating array.

This is not great -- we shouldn't be choosing the unit number here
anyway; we should just let autoconf do it for us -- but it's better
than potentially blocking any dk_openlock or dk_rawlock (which are
sometimes held when waiting for dkwedges_lock) for memory allocation.


To generate a diff of this commit:
cvs rdiff -u -r1.126 -r1.127 src/sys/dev/dkwedge/dk.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/dev/dkwedge/dk.c
diff -u src/sys/dev/dkwedge/dk.c:1.126 src/sys/dev/dkwedge/dk.c:1.127
--- src/sys/dev/dkwedge/dk.c:1.126	Fri Apr 21 18:09:38 2023
+++ src/sys/dev/dkwedge/dk.c	Fri Apr 21 18:24:19 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: dk.c,v 1.126 2023/04/21 18:09:38 riastradh Exp $	*/
+/*	$NetBSD: dk.c,v 1.127 2023/04/21 18:24:19 riastradh Exp $	*/
 
 /*-
  * Copyright (c) 2004, 2005, 2006, 2007 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: dk.c,v 1.126 2023/04/21 18:09:38 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: dk.c,v 1.127 2023/04/21 18:24:19 riastradh Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_dkwedge.h"
@@ -242,21 +242,49 @@ dkwedge_compute_pdev(const char *pname, 
  * dkwedge_array_expand:
  *
  *	Expand the dkwedges array.
+ *
+ *	Releases and reacquires dkwedges_lock as a writer.
  */
-static void
+static int
 dkwedge_array_expand(void)
 {
-	int newcnt = ndkwedges + 16;
-	struct dkwedge_softc **newarray, **oldarray;
 
+	const unsigned incr = 16;
+	unsigned newcnt, oldcnt;
+	struct dkwedge_softc **newarray = NULL, **oldarray = NULL;
+
+	KASSERT(rw_write_held(_lock));
+
+	oldcnt = ndkwedges;
+	oldarray = dkwedges;
+
+	if (oldcnt >= INT_MAX - incr)
+		return ENFILE;	/* XXX */
+	newcnt = oldcnt + incr;
+
+	rw_exit(_lock);
 	newarray = malloc(newcnt * sizeof(*newarray), M_DKWEDGE,
 	M_WAITOK|M_ZERO);
-	if ((oldarray = dkwedges) != NULL)
+	rw_enter(_lock, RW_WRITER);
+
+	if (ndkwedges != oldcnt || dkwedges != oldarray) {
+		oldarray = NULL; /* already recycled */
+		goto out;
+	}
+
+	if (oldarray != NULL)
 		memcpy(newarray, dkwedges, ndkwedges * sizeof(*newarray));
 	dkwedges = newarray;
+	newarray = NULL;	/* transferred to dkwedges */
 	ndkwedges = newcnt;
+
+out:	rw_exit(_lock);
 	if (oldarray != NULL)
 		free(oldarray, M_DKWEDGE);
+	if (newarray != NULL)
+		free(newarray, M_DKWEDGE);
+	rw_enter(_lock, RW_WRITER);
+	return 0;
 }
 
 static void
@@ -434,9 +462,11 @@ dkwedge_add(struct dkwedge_info *dkw)
 		if (error)
 			break;
 		KASSERT(unit == ndkwedges);
-		if (scpp == NULL)
-			dkwedge_array_expand();
-		else {
+		if (scpp == NULL) {
+			error = dkwedge_array_expand();
+			if (error)
+break;
+		} else {
 			KASSERT(scpp == [sc->sc_cfdata.cf_unit]);
 			*scpp = sc;
 			break;



CVS commit: src/sys/dev/dkwedge

2023-04-21 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Fri Apr 21 18:24:19 UTC 2023

Modified Files:
src/sys/dev/dkwedge: dk.c

Log Message:
dk(4): Avoid holding dkwedges_lock while allocating array.

This is not great -- we shouldn't be choosing the unit number here
anyway; we should just let autoconf do it for us -- but it's better
than potentially blocking any dk_openlock or dk_rawlock (which are
sometimes held when waiting for dkwedges_lock) for memory allocation.


To generate a diff of this commit:
cvs rdiff -u -r1.126 -r1.127 src/sys/dev/dkwedge/dk.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/sys/dev/dkwedge

2023-04-21 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Fri Apr 21 18:09:38 UTC 2023

Modified Files:
src/sys/dev/dkwedge: dk.c

Log Message:
dk(4): Restore assertions in dklastclose.

We only enter dklastclose if the wedge is open (sc->sc_dk.dk_openmask
!= 0), which can happen only if dkfirstopen has succeeded, in which
case we hold a dk_rawopens reference to the parent that prevents
anyone else from closing it.  Hence sc->sc_parent->dk_rawopens > 0.

On open, sc->sc_parent->dk_rawvp is set to nonnull, and it is only
reset to null on close.  Hence if the parent is still open, as it
must be here, sc->sc_parent->dk_rawvp must be nonnull.


To generate a diff of this commit:
cvs rdiff -u -r1.125 -r1.126 src/sys/dev/dkwedge/dk.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/dev/dkwedge/dk.c
diff -u src/sys/dev/dkwedge/dk.c:1.125 src/sys/dev/dkwedge/dk.c:1.126
--- src/sys/dev/dkwedge/dk.c:1.125	Thu Apr 13 08:30:40 2023
+++ src/sys/dev/dkwedge/dk.c	Fri Apr 21 18:09:38 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: dk.c,v 1.125 2023/04/13 08:30:40 riastradh Exp $	*/
+/*	$NetBSD: dk.c,v 1.126 2023/04/21 18:09:38 riastradh Exp $	*/
 
 /*-
  * Copyright (c) 2004, 2005, 2006, 2007 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: dk.c,v 1.125 2023/04/13 08:30:40 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: dk.c,v 1.126 2023/04/21 18:09:38 riastradh Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_dkwedge.h"
@@ -1227,6 +1227,8 @@ dklastclose(struct dkwedge_softc *sc)
 
 	KASSERT(mutex_owned(>sc_dk.dk_openlock));
 	KASSERT(mutex_owned(>sc_parent->dk_rawlock));
+	KASSERT(sc->sc_parent->dk_rawopens > 0);
+	KASSERT(sc->sc_parent->dk_rawvp != NULL);
 
 	if (--sc->sc_parent->dk_rawopens == 0) {
 		struct vnode *const vp = sc->sc_parent->dk_rawvp;



CVS commit: src/sys/dev/dkwedge

2023-04-21 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Fri Apr 21 18:09:38 UTC 2023

Modified Files:
src/sys/dev/dkwedge: dk.c

Log Message:
dk(4): Restore assertions in dklastclose.

We only enter dklastclose if the wedge is open (sc->sc_dk.dk_openmask
!= 0), which can happen only if dkfirstopen has succeeded, in which
case we hold a dk_rawopens reference to the parent that prevents
anyone else from closing it.  Hence sc->sc_parent->dk_rawopens > 0.

On open, sc->sc_parent->dk_rawvp is set to nonnull, and it is only
reset to null on close.  Hence if the parent is still open, as it
must be here, sc->sc_parent->dk_rawvp must be nonnull.


To generate a diff of this commit:
cvs rdiff -u -r1.125 -r1.126 src/sys/dev/dkwedge/dk.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/sys/dev/dkwedge

2023-04-13 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Thu Apr 13 08:30:41 UTC 2023

Modified Files:
src/sys/dev/dkwedge: dk.c

Log Message:
dk(4): Explain why dk_rawopens can't overflow and assert it.


To generate a diff of this commit:
cvs rdiff -u -r1.124 -r1.125 src/sys/dev/dkwedge/dk.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/dev/dkwedge/dk.c
diff -u src/sys/dev/dkwedge/dk.c:1.124 src/sys/dev/dkwedge/dk.c:1.125
--- src/sys/dev/dkwedge/dk.c:1.124	Tue Sep 27 17:04:52 2022
+++ src/sys/dev/dkwedge/dk.c	Thu Apr 13 08:30:40 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: dk.c,v 1.124 2022/09/27 17:04:52 mlelstv Exp $	*/
+/*	$NetBSD: dk.c,v 1.125 2023/04/13 08:30:40 riastradh Exp $	*/
 
 /*-
  * Copyright (c) 2004, 2005, 2006, 2007 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: dk.c,v 1.124 2022/09/27 17:04:52 mlelstv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: dk.c,v 1.125 2023/04/13 08:30:40 riastradh Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_dkwedge.h"
@@ -1200,7 +1200,13 @@ dkfirstopen(struct dkwedge_softc *sc, in
 	} else {
 		/*
 		 * Retrieve mode from an already opened wedge.
+		 *
+		 * At this point, dk_rawopens is bounded by the number
+		 * of dkwedge devices in the system, which is limited
+		 * by autoconf device numbering to INT_MAX.  Since
+		 * dk_rawopens is unsigned, this can't overflow.
 		 */
+		KASSERT(sc->sc_parent->dk_rawopens < UINT_MAX);
 		mode = 0;
 		LIST_FOREACH(nsc, >sc_parent->dk_wedges, sc_plink) {
 			if (nsc == sc || nsc->sc_dk.dk_openmask == 0)



CVS commit: src/sys/dev/dkwedge

2023-04-13 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Thu Apr 13 08:30:41 UTC 2023

Modified Files:
src/sys/dev/dkwedge: dk.c

Log Message:
dk(4): Explain why dk_rawopens can't overflow and assert it.


To generate a diff of this commit:
cvs rdiff -u -r1.124 -r1.125 src/sys/dev/dkwedge/dk.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



Re: CVS commit: src/sys/dev/dkwedge

2023-04-13 Thread Taylor R Campbell
> Date: Tue, 11 Apr 2023 21:50:49 +0200
> From: Michael van Elst 
> 
> On Wed, Apr 12, 2023 at 01:10:40AM +0700, Robert Elz wrote:
> > 
> >   | In that state then decrementing dk_rawopens beyond zero will make
> >   | dklastclose do the right thing: nothing.
> > 
> > Except that if that happens, dk_rawopens will be left == ~0 and the next
> > open attempt will then increment it, back to 0 again, which is almost
> > certainly not what was wanted.
> > 
> > dklastclose() used to have code in it like
> > 
> > if (...->dk_rawopens > 0) {
> > if (--...->dk_rawopens == 0)
> 
> 
> Indeed, that part was simplified away.

Correct.  I first added the assertion dk_rawopens > 0 in the following
change change because, as I wrote in the commit message:

   It is not possible for us to be closing a wedge whose parent is not
   open by at least this wedge.

https://mail-index.netbsd.org/source-changes-hg/2022/08/22/msg359438.html

See
https://mail-index.netbsd.org/source-changes-d/2023/04/11/msg013937.html
for more details of why I believe the condition is always true here.

Then, on the grounds that the condition is always true (and asserted
to be so), I removed the conditional in a separate change:

https://mail-index.netbsd.org/source-changes-hg/2022/08/22/msg359439.html

So if you actually hit the assertion, please share diagnostics, or if
you believe there is a way that you could hit the assertion, please
explain how and we can figure out how to address it.

But if not, please put the assertions back, or I will next week.


Re: CVS commit: src/sys/dev/dkwedge

2023-04-11 Thread Michael van Elst
On Wed, Apr 12, 2023 at 01:10:40AM +0700, Robert Elz wrote:
> 
>   | In that state then decrementing dk_rawopens beyond zero will make
>   | dklastclose do the right thing: nothing.
> 
> Except that if that happens, dk_rawopens will be left == ~0 and the next
> open attempt will then increment it, back to 0 again, which is almost
> certainly not what was wanted.
> 
> dklastclose() used to have code in it like
> 
>   if (...->dk_rawopens > 0) {
>   if (--...->dk_rawopens == 0)


Indeed, that part was simplified away.


-- 
Michael van Elst
Internet: mlel...@serpens.de
"A potential Snark may lurk in every tree."


Re: CVS commit: src/sys/dev/dkwedge

2023-04-11 Thread Robert Elz
Date:Tue, 11 Apr 2023 17:21:17 +0200
From:Michael van Elst 
Message-ID:  

  | In that state then decrementing dk_rawopens beyond zero will make
  | dklastclose do the right thing: nothing.

Except that if that happens, dk_rawopens will be left == ~0 and the next
open attempt will then increment it, back to 0 again, which is almost
certainly not what was wanted.

dklastclose() used to have code in it like

if (...->dk_rawopens > 0) {
if (--...->dk_rawopens == 0)

so that the -- would never be performed if rawopens was 0 when entered.

  | When you want to check for overflows of dk_rawopens (which is difficult
  | to overflow as you had to create 2^32 wedges)

It wasn't the overflow that Taylor meant, but this underflow (from 0 -> ~0)
which might be a problem.

(Not really relevant, but it wouldn't be 2^32 wedges, but 2^32 simultaneous
opens of any single wedge, right ... but that's not the real issue, that one
probably can't happen on any normal system, the file table could never get
big enough to allow 2^32 simultaneous opens of everything, let alone one
device).

Either dklastclose() can be called when dk_rawopens == 0 (in which case
the current code is broken) or it cannot, in which case the assertion would
have just verified that.

kre



Re: CVS commit: src/sys/dev/dkwedge

2023-04-11 Thread Michael van Elst
On Tue, Apr 11, 2023 at 09:07:49AM +, Taylor R Campbell wrote:
> 
> (a) how we can legitimately enter a state where the assertions are
> violated, and

dklastclose is called when the close operation ends in no more openers.
There is nothing that guarantees that any open was successful before
with the effect that dk_rawopens is > 0 and dk_rawvp is not NULL.

In that state then decrementing dk_rawopens beyond zero will make
dklastclose do the right thing: nothing.

When you want to check for overflows of dk_rawopens (which is difficult
to overflow as you had to create 2^32 wedges) you need to watch it being
incremented (also temporarily). Crashing after the fact with an assertion
in dklastclose doesn't help.

-- 
Michael van Elst
Internet: mlel...@serpens.de
"A potential Snark may lurk in every tree."


Re: CVS commit: src/sys/dev/dkwedge

2023-04-11 Thread Taylor R Campbell
> Module Name:src
> Committed By:   mlelstv
> Date:   Tue Sep 27 17:04:52 UTC 2022
> 
> Modified Files:
> src/sys/dev/dkwedge: dk.c
> 
> Log Message:
> Remove bogus assertions.
> 
> @@ -1221,8 +1221,6 @@ dklastclose(struct dkwedge_softc *sc)
>  
> KASSERT(mutex_owned(>sc_dk.dk_openlock));
> KASSERT(mutex_owned(>sc_parent->dk_rawlock));
> -   KASSERT(sc->sc_parent->dk_rawopens > 0);
> -   KASSERT(sc->sc_parent->dk_rawvp != NULL);
>  
> if (--sc->sc_parent->dk_rawopens == 0) {
> struct vnode *const vp = sc->sc_parent->dk_rawvp;

If these assertions are bogus, please add a comment explaining:

(a) how we can legitimately enter a state where the assertions are
violated, and

(b) how this logic is supposed to work when dk_rawopens wraps around
from 0 to UINT_MAX and we try to dk_close_parent(NULL, ...).

Otherwise, please restore these assertions.


  1   2   3   >