git: 9front

Download patch

ref: 1cb466d417a9911484a6e097b78c53633e07076a
parent: 5ec0e280e38d1add469e0765f4889d57b99407ea
author: cinap_lenrek <cinap_lenrek@gmx.de>
date: Wed Dec 26 12:53:12 EST 2012

devsd: fix possible sdbio() race with inquiry data changing (due to ahci hotplug)

the unit inquiry data might change in case the drive got pulled
with ahci. so keep track if we locked the ctl in a local stack
variable instead of relying on that the inquiry data stays the
same.

--- a/sys/src/9/port/devsd.c
+++ b/sys/src/9/port/devsd.c
@@ -770,7 +770,7 @@
 static long
 sdbio(Chan* c, int write, char* a, long len, uvlong off)
 {
-	int nchange, hard, allocd;
+	int nchange, hard, allocd, locked;
 	long l;
 	uchar *b;
 	SDpart *pp;
@@ -832,7 +832,8 @@
 		poperror();
 		return 0;
 	}
-	if(!(unit->inquiry[1] & 0x80)){
+	locked = (unit->inquiry[1] & 0x80) != 0;
+	if(!locked){
 		qunlock(&unit->ctl);
 		poperror();
 	}
@@ -854,7 +855,7 @@
 	if(waserror()){
 		if(allocd)
 			sdfree(b);
-		if(!(unit->inquiry[1] & 0x80))
+		if(!locked)
 			decref(&sdev->r);		/* gadverdamme! */
 		nexterror();
 	}
@@ -896,7 +897,7 @@
 		sdfree(b);
 	poperror();
 
-	if(unit->inquiry[1] & 0x80){
+	if(locked){
 		qunlock(&unit->ctl);
 		poperror();
 	}
--