1 #ifdef PLAN9PORT /* SORRY! */
3 # include <sys/types.h>
4 # ifdef __linux__ /* REALLY SORRY! */
5 # define CANBLOCKSIZE 1
7 # elif defined(__FreeBSD__)
8 # define CANBLOCKSIZE 1
9 # include <sys/param.h>
10 # include <sys/stat.h>
11 # include <sys/mount.h>
22 int findsubpart(Part *part, char *name);
25 strtoullsuf(char *p, char **pp, int rad, u64int *u)
29 if(!isdigit((uchar)*p))
31 v = strtoull(p, &p, rad);
61 parsepart(char *name, char **file, char **subpart, u64int *lo, u64int *hi)
65 *file = estrdup(name);
69 if((p = strrchr(*file, ':')) == nil)
79 if(strtoullsuf(p, &p, 0, lo) < 0){
90 if(strtoullsuf(p, &p, 0, hi) < 0 || *p != 0){
98 #define min(a, b) ((a) < (b) ? (a) : (b))
100 initpart(char *name, int mode)
104 char *file, *subname;
107 if(parsepart(name, &file, &subname, &lo, &hi) < 0){
108 werrstr("cannot parse name %s", name);
111 trace(TraceDisk, "initpart %s file %s lo 0x%llx hi 0x%llx", name, file, lo, hi);
113 part->name = estrdup(name);
114 part->filename = estrdup(file);
116 mode &= ~(OREAD|OWRITE|ORDWR);
119 #ifdef __linux__ /* sorry, but linus made O_DIRECT unusable! */
122 part->fd = open(file, mode);
124 if((mode&(OREAD|OWRITE|ORDWR)) == ORDWR)
125 part->fd = open(file, (mode&~ORDWR)|OREAD);
128 fprint(2, "can't open partition='%s': %r\n", file);
129 seterr(EOk, "can't open partition='%s': %r", file);
134 fprint(2, "warning: %s opened for reading only\n", name);
137 dir = dirfstat(part->fd);
140 seterr(EOk, "can't stat partition='%s': %r", file);
144 if(dir->length == 0){
147 if(dir == nil || dir->length == 0) {
149 seterr(EOk, "can't determine size of partition %s", file);
154 if(dir->length < hi || dir->length < lo){
156 seterr(EOk, "partition '%s': bounds out of range (max %lld)", name, dir->length);
163 part->size = hi - part->offset;
167 if(fstatfs(part->fd, &sfs) >= 0 && sfs.f_bsize > 512)
168 part->fsblocksize = sfs.f_bsize;
172 part->fsblocksize = min(part->fsblocksize, MaxIo);
174 if(subname && findsubpart(part, subname) < 0){
175 werrstr("cannot find subpartition %s", subname);
184 flushpart(Part *part)
187 #ifdef __linux__ /* grrr! */
188 if(fsync(part->fd) < 0){
189 logerr(EAdmin, "flushpart %s: %r", part->name);
192 posix_fadvise(part->fd, 0, 0, POSIX_FADV_DONTNEED);
209 partblocksize(Part *part, u32int blocksize)
212 sysfatal("resetting partition=%s's block size", part->name);
213 part->blocksize = blocksize;
214 if(blocksize > maxblocksize)
215 maxblocksize = blocksize;
219 * Read/write some amount of data between a block device or file and a memory buffer.
221 * Most Unix systems require that when accessing a block device directly,
222 * the buffer, offset, and count are all multiples of the device block size,
223 * making this a lot more complicated than it otherwise would be.
225 * Most of our callers will make things easy on us, but for some callers it's best
226 * if we just do the work here, with only one place to get it right (hopefully).
228 * If everything is aligned properly, prwb will try to do big transfers in the main
229 * body of the loop: up to MaxIo bytes at a time. If everything isn't aligned properly,
230 * we work one block at a time.
233 prwb(char *name, int fd, int isread, u64int offset, void *vbuf, u32int count, u32int blocksize)
236 u8int *buf, *freetmp, *dst;
237 u32int icount, opsize;
245 op = isread ? "read" : "write";
249 opsize = min(count, 131072 /* blocksize */);
251 r = pread(fd, dst, opsize, offset);
253 r = pwrite(fd, dst, opsize, offset);
274 logerr(EStrange, "pwrb %s called to %s 0 bytes", name, isread ? "read" : "write");
278 assert(blocksize > 0);
280 /* allocate blocksize-aligned temp buffer if needed */
281 if((ulong)offset%blocksize || (ulong)buf%blocksize || count%blocksize){
282 if((freetmp = malloc(blocksize*2)) == nil)
285 tmp += blocksize - (ulong)tmp%blocksize;
288 /* handle beginning fringe */
289 if((delta = (ulong)offset%blocksize) != 0){
291 if((r=pread(fd, tmp, blocksize, offset-delta)) != blocksize){
293 offset = offset-delta;
298 c = min(count, blocksize-delta);
299 assert(c > 0 && c < blocksize);
301 memmove(buf, tmp+delta, c);
303 memmove(tmp+delta, buf, c);
304 if((r=pwrite(fd, tmp, blocksize, offset-delta)) != blocksize){
306 offset = offset-delta;
318 /* handle full blocks */
319 while(count >= blocksize){
320 assert((ulong)offset%blocksize == 0);
321 if((ulong)buf%blocksize){
327 opsize = count - count%blocksize;
332 if((r=pread(fd, dst, opsize, offset))<=0 || r%blocksize){
338 assert(r == blocksize);
339 memmove(buf, tmp, blocksize);
343 assert(opsize == blocksize);
344 memmove(dst, buf, blocksize);
346 if((r=pwrite(fd, dst, opsize, offset))<=0 || r%blocksize){
352 assert(r == blocksize);
360 /* handle ending fringe */
362 assert((ulong)offset%blocksize == 0);
365 * Complicated condition: if we're reading it's okay to get less than
366 * a block as long as it's enough to satisfy the read - maybe this is
367 * a normal file. (We never write to normal files, or else things would
368 * be even more complicated.)
370 r = pread(fd, tmp, blocksize, offset);
371 if((isread && r < count) || (!isread && r != blocksize)){
372 print("FAILED isread=%d r=%d count=%d blocksize=%d\n", isread, r, count, blocksize);
379 memmove(buf, tmp, count);
381 memmove(tmp, buf, count);
382 if(pwrite(fd, tmp, blocksize, offset) != blocksize){
396 seterr(EAdmin, "%s %s offset 0x%llux count %ud buf %p returned %d: %r",
397 op, name, offset, count1, dst, r);
404 static int sdreset(Part*);
405 static int reopen(Part*);
406 static int threadspawnl(int[3], char*, char*, ...);
410 rwpart(Part *part, int isread, u64int offset, u8int *buf, u32int count)
415 trace(TraceDisk, "%s %s %ud at 0x%llx",
416 isread ? "read" : "write", part->name, count, offset);
417 if(offset >= part->size || offset+count > part->size){
418 seterr(EStrange, "out of bounds %s offset 0x%llux count %ud to partition %s size 0x%llux",
419 isread ? "read" : "write", offset, count, part->name, part->size);
423 blocksize = part->fsblocksize;
425 blocksize = part->blocksize;
430 n = prwb(part->filename, part->fd, isread, part->offset+offset, buf, count, blocksize);
431 if(n >= 0 || try > 10)
438 * This happens with the sdmv disks frustratingly often.
439 * Try to fix things up and continue.
441 rerrstr(err, sizeof err);
442 if(strstr(err, "i/o timeout") || strstr(err, "i/o error") || strstr(err, "partition has changed")){
450 #ifdef __linux__ /* sigh */
451 posix_fadvise(part->fd, part->offset+offset, n, POSIX_FADV_DONTNEED);
456 readpart(Part *part, u64int offset, u8int *buf, u32int count)
458 return rwpart(part, 1, offset, buf, count);
462 writepart(Part *part, u64int offset, u8int *buf, u32int count)
464 return rwpart(part, 0, offset, buf, count);
473 p = initpart(name, OREAD);
476 b = alloczblock(p->size, 0, p->blocksize);
478 seterr(EOk, "can't alloc %s: %r", name);
482 if(readpart(p, 0, b->data, p->size) < 0){
483 seterr(EOk, "can't read %s: %r", name);
493 * Search for the Plan 9 partition with the given name.
494 * This lets you write things like /dev/ad4:arenas
495 * if you move a disk from a Plan 9 system to a FreeBSD system.
497 * God I hope I never write this code again.
499 #define MAGIC "plan9 partitions"
501 tryplan9part(Part *part, char *name)
504 char *line[40], *f[4];
509 * Partition table in second sector.
510 * Could also look on 2nd last sector and last sector,
511 * but those disks died out long before venti came along.
513 if(readpart(part, 512, buf, 512) != 512)
516 /* Plan 9 partition table is just text strings */
517 if(strncmp((char*)buf, "part ", 5) != 0)
521 n = getfields((char*)buf, line, 40, 1, "\n");
523 if(getfields(line[i], f, 4, 1, " ") != 4)
525 if(strcmp(f[0], "part") != 0)
527 if(strcmp(f[1], name) == 0){
528 start = 512*strtoll(f[2], 0, 0);
529 end = 512*strtoll(f[3], 0, 0);
530 if(start < end && end <= part->size){
531 part->offset += start;
532 part->size = end - start;
541 #define GSHORT(p) (((p)[1]<<8)|(p)[0])
542 #define GLONG(p) ((GSHORT(p+2)<<16)|GSHORT(p))
544 typedef struct Dospart Dospart;
547 uchar flag; /* active flag */
548 uchar shead; /* starting head */
549 uchar scs[2]; /* starting cylinder/sector */
550 uchar type; /* partition type */
551 uchar ehead; /* ending head */
552 uchar ecs[2]; /* ending cylinder/sector */
553 uchar offset[4]; /* starting sector */
554 uchar size[4]; /* length in sectors */
559 findsubpart(Part *part, char *name)
566 /* See if this is a Plan 9 partition. */
567 if(tryplan9part(part, name) >= 0)
570 /* Otherwise try for an MBR and then narrow to Plan 9 partition. */
571 if(readpart(part, 0, buf, 512) != 512)
573 if(buf[0x1FE] != 0x55 || buf[0x1FF] != 0xAA)
575 dp = (Dospart*)(buf+0x1BE);
578 if(dp[i].type == '9'){
579 part->offset = 512LL*GLONG(dp[i].offset);
580 part->size = 512LL*GLONG(dp[i].size);
581 if(tryplan9part(part, name) >= 0)
586 /* Not implementing extended partitions - enough is enough. */