Module Name:    src
Committed By:   msaitoh
Date:           Wed Nov 18 08:48:47 UTC 2015

Modified Files:
        src/sys/dev [netbsd-7]: vnd.c
        src/sys/sys [netbsd-7]: disk.h

Log Message:
Pull up following revision(s) (requested by manu in ticket #1038):
        sys/dev/vnd.c: revision 1.252
        sys/dev/vnd.c: revision 1.253
        sys/dev/vnd.c: revision 1.254
        sys/dev/vnd.c: revision 1.249
        sys/sys/disk.h: revision 1.66
        sys/dev/vnd.c: revision 1.250
        sys/dev/vnd.c: revision 1.251
- Add DK_DEV_BSIZE_OK()
- Simplify ioctl handling a little.
- disable debugging
- Return ENXIO if the get ioctl exceeds the number of configured devices.
  XXX: pullup-7
- explain why the int cast works (suggested by kre)
- fix incorrect memset.
- Use the new DK_DEV_BSIZE_OK() macro.


To generate a diff of this commit:
cvs rdiff -u -r1.232.2.3 -r1.232.2.4 src/sys/dev/vnd.c
cvs rdiff -u -r1.60 -r1.60.4.1 src/sys/sys/disk.h

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/vnd.c
diff -u src/sys/dev/vnd.c:1.232.2.3 src/sys/dev/vnd.c:1.232.2.4
--- src/sys/dev/vnd.c:1.232.2.3	Wed Jan 28 19:16:21 2015
+++ src/sys/dev/vnd.c	Wed Nov 18 08:48:46 2015
@@ -1,4 +1,4 @@
-/*	$NetBSD: vnd.c,v 1.232.2.3 2015/01/28 19:16:21 martin Exp $	*/
+/*	$NetBSD: vnd.c,v 1.232.2.4 2015/11/18 08:48:46 msaitoh Exp $	*/
 
 /*-
  * Copyright (c) 1996, 1997, 1998, 2008 The NetBSD Foundation, Inc.
@@ -91,7 +91,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: vnd.c,v 1.232.2.3 2015/01/28 19:16:21 martin Exp $");
+__KERNEL_RCSID(0, "$NetBSD: vnd.c,v 1.232.2.4 2015/11/18 08:48:46 msaitoh Exp $");
 
 #if defined(_KERNEL_OPT)
 #include "opt_vnd.h"
@@ -137,7 +137,7 @@ int dovndcluster = 1;
 #define VDB_INIT	0x02
 #define VDB_IO		0x04
 #define VDB_LABEL	0x08
-int vnddebug = 0x00;
+int vnddebug = 0;
 #endif
 
 #define vndunit(x)	DISKUNIT(x)
@@ -1030,7 +1030,7 @@ vnddoclear(struct vnd_softc *vnd, int pm
 	vndclear(vnd, minor);
 #ifdef DEBUG
 	if (vnddebug & VDB_INIT)
-		printf("vndioctl: CLRed\n");
+		printf("%s: CLRed\n", __func__);
 #endif
 
 	/* Destroy the xfer and buffer pools. */
@@ -1042,6 +1042,29 @@ vnddoclear(struct vnd_softc *vnd, int pm
 	return 0;
 }
 
+static int
+vndioctl_get(struct lwp *l, void *data, int unit, struct vattr *va)
+{
+	int error;
+
+	KASSERT(l);
+
+	/* the first member is always int vnd_unit in all the versions */
+	if (*(int *)data >= vnd_cd.cd_ndevs)
+		return ENXIO;
+
+	switch (error = vnd_cget(l, unit, (int *)data, va)) {
+	case -1:
+		/* unused is not an error */
+		memset(va, 0, sizeof(*va));
+		/*FALLTHROUGH*/
+	case 0:
+		return 0;
+	default:
+		return error;
+	}
+}
+
 /* ARGSUSED */
 static int
 vndioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
@@ -1067,15 +1090,46 @@ vndioctl(dev_t dev, u_long cmd, void *da
 		printf("vndioctl(0x%"PRIx64", 0x%lx, %p, 0x%x, %p): unit %d\n",
 		    dev, cmd, data, flag, l->l_proc, unit);
 #endif
-	vnd = device_lookup_private(&vnd_cd, unit);
-	if (vnd == NULL &&
+	/* Do the get's first; they don't need initialization or verification */
+	switch (cmd) {
 #ifdef COMPAT_30
-	    cmd != VNDIOCGET30 &&
+	case VNDIOCGET30: {
+		if ((error = vndioctl_get(l, data, unit, &vattr)) != 0)
+			return error;
+
+		struct vnd_user30 *vnu = data;
+		vnu->vnu_dev = vattr.va_fsid;
+		vnu->vnu_ino = vattr.va_fileid;
+		return 0;
+	}
 #endif
 #ifdef COMPAT_50
-	    cmd != VNDIOCGET50 &&
+	case VNDIOCGET50: {
+		if ((error = vndioctl_get(l, data, unit, &vattr)) != 0)
+			return error;
+
+		struct vnd_user50 *vnu = data;
+		vnu->vnu_dev = vattr.va_fsid;
+		vnu->vnu_ino = vattr.va_fileid;
+		return 0;
+	}
 #endif
-	    cmd != VNDIOCGET)
+
+	case VNDIOCGET: {
+		if ((error = vndioctl_get(l, data, unit, &vattr)) != 0)
+			return error;
+
+		struct vnd_user *vnu = data;
+		vnu->vnu_dev = vattr.va_fsid;
+		vnu->vnu_ino = vattr.va_fileid;
+		return 0;
+	}
+	default:
+		break;
+	}
+
+	vnd = device_lookup_private(&vnd_cd, unit);
+	if (vnd == NULL)
 		return ENXIO;
 	vio = (struct vnd_ioctl *)data;
 
@@ -1190,8 +1244,7 @@ vndioctl(dev_t dev, u_long cmd, void *da
 			/* note last offset is the file byte size */
 			vnd->sc_comp_numoffs = ntohl(ch->num_blocks)+1;
 			free(ch, M_TEMP);
-			if (vnd->sc_comp_blksz == 0 ||
-			    vnd->sc_comp_blksz % DEV_BSIZE !=0) {
+			if (!DK_DEV_BSIZE_OK(vnd->sc_comp_blksz)) {
 				VOP_UNLOCK(nd.ni_vp);
 				error = EINVAL;
 				goto close_and_exit;
@@ -1286,14 +1339,11 @@ vndioctl(dev_t dev, u_long cmd, void *da
 
 			/*
 			 * Sanity-check the sector size.
-			 * XXX Don't allow secsize < DEV_BSIZE.	 Should
-			 * XXX we?
 			 */
-			if (vnd->sc_geom.vng_secsize < DEV_BSIZE ||
-			    (vnd->sc_geom.vng_secsize % DEV_BSIZE) != 0 ||
+			if (!DK_DEV_BSIZE_OK(vnd->sc_geom.vng_secsize) ||
 			    vnd->sc_geom.vng_ncylinders == 0 ||
-			    (vnd->sc_geom.vng_ntracks *
-			     vnd->sc_geom.vng_nsectors) == 0) {
+			    vnd->sc_geom.vng_ntracks == 0 ||
+			    vnd->sc_geom.vng_nsectors == 0) {
 				error = EINVAL;
 				goto close_and_exit;
 			}
@@ -1418,72 +1468,6 @@ unlock_and_exit:
 
 		break;
 
-#ifdef COMPAT_30
-	case VNDIOCGET30: {
-		struct vnd_user30 *vnu;
-		struct vattr va;
-		vnu = (struct vnd_user30 *)data;
-		KASSERT(l);
-		switch (error = vnd_cget(l, unit, &vnu->vnu_unit, &va)) {
-		case 0:
-			vnu->vnu_dev = va.va_fsid;
-			vnu->vnu_ino = va.va_fileid;
-			break;
-		case -1:
-			/* unused is not an error */
-			vnu->vnu_dev = 0;
-			vnu->vnu_ino = 0;
-			break;
-		default:
-			return error;
-		}
-		break;
-	}
-#endif
-
-#ifdef COMPAT_50
-	case VNDIOCGET50: {
-		struct vnd_user50 *vnu;
-		struct vattr va;
-		vnu = (struct vnd_user50 *)data;
-		KASSERT(l);
-		switch (error = vnd_cget(l, unit, &vnu->vnu_unit, &va)) {
-		case 0:
-			vnu->vnu_dev = va.va_fsid;
-			vnu->vnu_ino = va.va_fileid;
-			break;
-		case -1:
-			/* unused is not an error */
-			vnu->vnu_dev = 0;
-			vnu->vnu_ino = 0;
-			break;
-		default:
-			return error;
-		}
-		break;
-	}
-#endif
-
-	case VNDIOCGET: {
-		struct vnd_user *vnu;
-		struct vattr va;
-		vnu = (struct vnd_user *)data;
-		KASSERT(l);
-		switch (error = vnd_cget(l, unit, &vnu->vnu_unit, &va)) {
-		case 0:
-			vnu->vnu_dev = va.va_fsid;
-			vnu->vnu_ino = va.va_fileid;
-			break;
-		case -1:
-			/* unused is not an error */
-			vnu->vnu_dev = 0;
-			vnu->vnu_ino = 0;
-			break;
-		default:
-			return error;
-		}
-		break;
-	}
 
 	case DIOCGDINFO:
 		*(struct disklabel *)data = *(vnd->sc_dkdev.dk_label);

Index: src/sys/sys/disk.h
diff -u src/sys/sys/disk.h:1.60 src/sys/sys/disk.h:1.60.4.1
--- src/sys/sys/disk.h:1.60	Thu Apr  3 15:24:20 2014
+++ src/sys/sys/disk.h	Wed Nov 18 08:48:46 2015
@@ -1,4 +1,4 @@
-/*	$NetBSD: disk.h,v 1.60 2014/04/03 15:24:20 christos Exp $	*/
+/*	$NetBSD: disk.h,v 1.60.4.1 2015/11/18 08:48:46 msaitoh Exp $	*/
 
 /*-
  * Copyright (c) 1996, 1997, 2004 The NetBSD Foundation, Inc.
@@ -507,6 +507,8 @@ struct disk_strategy {
 
 #define	DK_BSIZE2BLKSHIFT(b)	((ffs((b) / DEV_BSIZE)) - 1)
 #define	DK_BSIZE2BYTESHIFT(b)	(ffs((b)) - 1)
+#define DK_DEV_BSIZE_OK(b) \
+    ((b) >= DEV_BSIZE && ((b) & ((b) - 1)) == 0 && (b) <= MAXPHYS)
 
 #ifdef _KERNEL
 extern	int disk_count;			/* number of disks in global disklist */

Reply via email to