Module Name:    src
Committed By:   martin
Date:           Sun Mar 29 12:10:37 UTC 2020

Modified Files:
        src/sys/dev/scsipi [netbsd-8]: cd.c
        src/sys/kern [netbsd-8]: subr_disk.c

Log Message:
Pull up following revision(s) (requested by mlelstv in ticket #1527):

        sys/dev/scsipi/cd.c: revision 1.343
        sys/kern/subr_disk.c: revision 1.130

Avoid division by zero if label isn't valid.
Allow open of RAWPART even when no medium is loaded.
Keep errors silent if no medium is loaded.
Fixes PR kern/55104


To generate a diff of this commit:
cvs rdiff -u -r1.340.6.1 -r1.340.6.2 src/sys/dev/scsipi/cd.c
cvs rdiff -u -r1.119.2.2 -r1.119.2.3 src/sys/kern/subr_disk.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/scsipi/cd.c
diff -u src/sys/dev/scsipi/cd.c:1.340.6.1 src/sys/dev/scsipi/cd.c:1.340.6.2
--- src/sys/dev/scsipi/cd.c:1.340.6.1	Wed Jun 21 18:18:55 2017
+++ src/sys/dev/scsipi/cd.c	Sun Mar 29 12:10:37 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: cd.c,v 1.340.6.1 2017/06/21 18:18:55 snj Exp $	*/
+/*	$NetBSD: cd.c,v 1.340.6.2 2020/03/29 12:10:37 martin Exp $	*/
 
 /*-
  * Copyright (c) 1998, 2001, 2003, 2004, 2005, 2008 The NetBSD Foundation,
@@ -50,7 +50,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: cd.c,v 1.340.6.1 2017/06/21 18:18:55 snj Exp $");
+__KERNEL_RCSID(0, "$NetBSD: cd.c,v 1.340.6.2 2020/03/29 12:10:37 martin Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -401,8 +401,8 @@ cd_firstopen(device_t self, dev_t dev, i
         else
                 silent = 0;
 
-	/* make cdclose() loud again */
-	cd->flags &= ~CDF_EJECTED;
+	/* make cdclose() silent */
+	cd->flags |= CDF_EJECTED;
 
 	/* Check that it is still responding and ok. */
 	error = scsipi_test_unit_ready(periph,
@@ -419,8 +419,11 @@ cd_firstopen(device_t self, dev_t dev, i
 		if (error == EINVAL)
 			error = EIO;
 	}
-	if (error)
+	if (error) {
+		if (part == RAW_PART)
+			goto out;
 		goto bad;
+	}
 
 	/* Lock the pack in. */
 	error = scsipi_prevent(periph, SPAMR_PREVENT_DT,
@@ -448,6 +451,9 @@ cd_firstopen(device_t self, dev_t dev, i
 		SC_DEBUG(periph, SCSIPI_DB3, ("Params loaded "));
 
 		cd_set_geometry(cd);
+
+		/* make cdclose() loud again */
+		cd->flags &= ~CDF_EJECTED;
 	}
 
 	periph->periph_flags |= PERIPH_OPEN;
@@ -519,7 +525,8 @@ cd_lastclose(device_t self)
 	struct scsipi_adapter *adapt = periph->periph_channel->chan_adapter;
 	int silent;
 
-	if (cd->flags & CDF_EJECTED)
+	if ((cd->flags & CDF_EJECTED) != 0 ||
+	    (periph->periph_flags & PERIPH_MEDIA_LOADED) == 0)
 		silent = XS_CTL_SILENT;
 	else
 		silent = 0;
@@ -1213,6 +1220,14 @@ cdioctl(dev_t dev, u_long cmd, void *add
 		return (EIO);
 
 	switch (cmd) {
+	case DIOCTUR: {
+		/* test unit ready */
+		error = scsipi_test_unit_ready(cd->sc_periph, XS_CTL_SILENT);
+		*((int*)addr) = (error == 0);
+		if (error == ENODEV || error == EIO || error == 0)
+			return 0;                       
+		return error;
+	}
 	case CDIOCPLAYTRACKS: {
 		/* PLAY_MSF command */
 		struct ioc_play_track *args = addr;
@@ -1447,15 +1462,18 @@ static void
 cd_label(device_t self, struct disklabel *lp)
 {
 	struct cd_softc *cd = device_private(self);
+	struct scsipi_periph *periph = cd->sc_periph;
 	struct cd_formatted_toc toc;
-	int lastsession;
+	int lastsession = 0;
 
 	strncpy(lp->d_typename, "optical media", 16);
 	lp->d_rpm = 300;
-	lp->d_flags |= D_REMOVABLE | D_SCSI_MMC;
+	lp->d_flags |= D_REMOVABLE;
 
-	if (cdreadmsaddr(cd, &toc, &lastsession) != 0)
-		lastsession = 0;
+	if ((periph->periph_flags & PERIPH_MEDIA_LOADED) != 0) {
+		lp->d_flags |= D_SCSI_MMC;
+		(void) cdreadmsaddr(cd, &toc, &lastsession);
+	}
 
 	lp->d_partitions[0].p_offset = 0;
 	lp->d_partitions[0].p_size = lp->d_secperunit;

Index: src/sys/kern/subr_disk.c
diff -u src/sys/kern/subr_disk.c:1.119.2.2 src/sys/kern/subr_disk.c:1.119.2.3
--- src/sys/kern/subr_disk.c:1.119.2.2	Fri Nov  1 09:29:25 2019
+++ src/sys/kern/subr_disk.c	Sun Mar 29 12:10:37 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: subr_disk.c,v 1.119.2.2 2019/11/01 09:29:25 martin Exp $	*/
+/*	$NetBSD: subr_disk.c,v 1.119.2.3 2020/03/29 12:10:37 martin Exp $	*/
 
 /*-
  * Copyright (c) 1996, 1997, 1999, 2000, 2009 The NetBSD Foundation, Inc.
@@ -67,7 +67,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: subr_disk.c,v 1.119.2.2 2019/11/01 09:29:25 martin Exp $");
+__KERNEL_RCSID(0, "$NetBSD: subr_disk.c,v 1.119.2.3 2020/03/29 12:10:37 martin Exp $");
 
 #include <sys/param.h>
 #include <sys/kernel.h>
@@ -429,6 +429,10 @@ int
 disk_read_sectors(void (*strat)(struct buf *), const struct disklabel *lp,
     struct buf *bp, unsigned int sector, int count)
 {
+
+	if ((lp->d_secsize / DEV_BSIZE) == 0 || lp->d_secpercyl == 0)
+		return EINVAL;
+
 	bp->b_blkno = btodb((off_t)sector * lp->d_secsize);
 	bp->b_bcount = count * lp->d_secsize;
 	bp->b_flags = (bp->b_flags & ~B_WRITE) | B_READ;

Reply via email to