git: 9front

Download patch

ref: af2c50d3803f44521e4a6289a347f4823b2d8386
parent: 9e67e703b65bbf38ac4f16f3360be915671e574e
author: ment <thement@ibawizard.net>
date: Sun May 8 19:55:58 EDT 2011

usb/disk: fixed offsets in read(ctl), bug in offset calculation

--- a/sys/src/cmd/usb/disk/disk.c
+++ b/sys/src/cmd/usb/disk/disk.c
@@ -109,6 +109,24 @@
 }
 
 void
+fixlength(Umsc *lun, vlong blocks)
+{
+	Part *part, *p;
+	
+	part = lun->part;
+	part[Qdata].length = blocks;
+	for(p=&part[Qdata+1]; p < &part[Qmax]; p++){
+		if(p->inuse && p->offset + p->length > blocks){
+			if(p->offset > blocks){
+				p->offset =blocks;
+				p->length = 0;
+			}else
+				p->length = blocks - p->offset;
+		}
+	}
+}
+
+void
 makeparts(Umsc *lun)
 {
 	addpart(lun, "/", 0, 0, DMDIR | 0555);
@@ -143,7 +161,7 @@
 	for (p = &part[Qdata+1]; p < &part[Qmax]; p++)
 		if (p->inuse)
 			fmtprint(&fmt, "part %s %lld %lld\n",
-				p->name, p->offset, p->length);
+				p->name, p->offset, p->offset + p->length);
 	return fmtstrflush(&fmt);
 }
 
@@ -296,7 +314,7 @@
 	}
 	lun->blocks++; /* SRcapacity returns LBA of last block */
 	lun->capacity = (vlong)lun->blocks * lun->lbsize;
-	lun->part[Qdata].length = lun->blocks;
+	fixlength(lun, lun->blocks);
 	if(diskdebug)
 		fprint(2, "disk: logical block size %lud, # blocks %llud\n",
 			lun->lbsize, lun->blocks);
@@ -583,7 +601,6 @@
 
 	bno = offset >> lbshift;	/* offset / lbsize */
 	nb = ((offset + count + lbsize - 1) >> lbshift) - bno;
-	bno += p->offset;		/* start of partition */
 
 	if(bno + nb > p->length)		/* past end of partition? */
 		nb = p->length - bno;
@@ -593,6 +610,7 @@
 	if(bno >= p->length || nb == 0)
 		return 0;
 
+	bno += p->offset;		/* start of partition */
 	lun->offset = bno;
 	lun->off = offset & lbmask;		/* offset % lbsize */
 	if(lun->off == 0 && (count & lbmask) == 0)
--