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>
23 strtoullsuf(char *p, char **pp, int rad, u64int *u)
27 if(!isdigit((uchar)*p))
29 v = strtoull(p, &p, rad);
59 parsepart(char *name, char **file, u64int *lo, u64int *hi)
63 *file = estrdup(name);
64 if((p = strrchr(*file, ':')) == nil){
73 if(strtoullsuf(p, &p, 0, lo) < 0){
84 if(strtoullsuf(p, &p, 0, hi) < 0 || *p != 0){
92 initpart(char *name, int mode)
99 if(parsepart(name, &file, &lo, &hi) < 0)
101 trace(TraceDisk, "initpart %s file %s lo 0x%llx hi 0x%llx", name, file, lo, hi);
103 part->name = estrdup(name);
104 part->filename = estrdup(file);
106 mode &= (OREAD|OWRITE|ORDWR);
109 part->fd = open(file, mode);
111 if((mode&(OREAD|OWRITE|ORDWR)) == ORDWR)
112 part->fd = open(file, (mode&~ORDWR)|OREAD);
115 fprint(2, "can't open partition='%s': %r\n", file);
116 seterr(EOk, "can't open partition='%s': %r", file);
121 fprint(2, "warning: %s opened for reading only\n", name);
124 dir = dirfstat(part->fd);
127 seterr(EOk, "can't stat partition='%s': %r", file);
131 if(dir->length == 0){
134 seterr(EOk, "can't determine size of partition %s", file);
138 if(dir->length < hi || dir->length < lo){
140 seterr(EOk, "partition '%s': bounds out of range (max %lld)", name, dir->length);
147 part->size = hi - part->offset;
148 fprint(2, "part %s: file %s offset %,lld size %,lld\n",
149 name, file, part->offset, part->size);
153 if(fstatfs(part->fd, &sfs) >= 0)
154 part->fsblocksize = sfs.f_bsize;
173 partblocksize(Part *part, u32int blocksize)
176 sysfatal("resetting partition=%s's block size", part->name);
177 part->blocksize = blocksize;
178 if(blocksize > maxblocksize)
179 maxblocksize = blocksize;
183 * Read/write some amount of data between a block device or file and a memory buffer.
185 * Most Unix systems require that when accessing a block device directly,
186 * the buffer, offset, and count are all multiples of the device block size,
187 * making this a lot more complicated than it otherwise would be.
189 * Most of our callers will make things easy on us, but for some callers it's best
190 * if we just do the work here, with only one place to get it right (hopefully).
192 * If everything is aligned properly, prwb will try to do big transfers in the main
193 * body of the loop: up to MaxIo bytes at a time. If everything isn't aligned properly,
194 * we work one block at a time.
197 #define min(a, b) ((a) < (b) ? (a) : (b))
199 prwb(char *name, int fd, int isread, u64int offset, void *vbuf, u32int count, u32int blocksize)
202 u8int *buf, *tmp, *freetmp, *dst;
203 u32int c, delta, icount, opsize;
213 logerr(EStrange, "pwrb %s called to %s 0 bytes", name, isread ? "read" : "write");
217 assert(blocksize > 0);
219 /* allocate blocksize-aligned temp buffer if needed */
220 if((ulong)offset%blocksize || (ulong)buf%blocksize || count%blocksize){
221 if((freetmp = malloc(blocksize*2)) == nil)
224 tmp += blocksize - (ulong)tmp%blocksize;
227 /* handle beginning fringe */
228 if((delta = (ulong)offset%blocksize) != 0){
230 if((r=pread(fd, tmp, blocksize, offset-delta)) != blocksize){
232 offset = offset-delta;
236 c = min(count, blocksize-delta);
237 assert(c > 0 && c < blocksize);
239 memmove(buf, tmp+delta, c);
241 memmove(tmp+delta, buf, c);
242 if((r=pwrite(fd, tmp, blocksize, offset-delta)) != blocksize){
244 offset = offset-delta;
255 /* handle full blocks */
256 while(count >= blocksize){
257 assert((ulong)offset%blocksize == 0);
258 if((ulong)buf%blocksize){
264 opsize = count - count%blocksize;
269 if((r=pread(fd, dst, opsize, offset))<=0 || r%blocksize){
274 assert(r == blocksize);
275 memmove(buf, tmp, blocksize);
279 assert(opsize == blocksize);
280 memmove(dst, buf, blocksize);
282 if((r=pwrite(fd, dst, opsize, offset))<=0 || r%blocksize){
287 assert(r == blocksize);
295 /* handle ending fringe */
297 assert((ulong)offset%blocksize == 0);
300 * Complicated condition: if we're reading it's okay to get less than
301 * a block as long as it's enough to satisfy the read - maybe this is
302 * a normal file. (We never write to normal files, or else things would
303 * be even more complicated.)
305 r = pread(fd, tmp, blocksize, offset);
306 if((isread && r < count) || (!isread && r != blocksize)){
307 print("FAILED isread=%d r=%d count=%d blocksize=%d\n", isread, r, count, blocksize);
313 memmove(buf, tmp, count);
315 memmove(tmp, buf, count);
316 if(pwrite(fd, tmp, opsize, offset) != blocksize){
328 seterr(EAdmin, "%s %s offset 0x%llux count %ud buf %p returned %d: %r",
329 op, name, offset, opsize, dst, r);
336 rwpart(Part *part, int isread, u64int offset, u8int *buf, u32int count)
340 trace(TraceDisk, "%s %s %ud at 0x%llx",
341 isread ? "read" : "write", part->name, count, offset);
342 if(offset >= part->size || offset+count > part->size){
343 seterr(EStrange, "out of bounds %s offset 0x%llux count %ud to partition %s size 0x%llux",
344 isread ? "read" : "write", offset, count, part->name, part->size);
348 blocksize = part->fsblocksize;
350 blocksize = part->blocksize;
354 return prwb(part->filename, part->fd, isread, part->offset+offset, buf, count, blocksize);
358 readpart(Part *part, u64int offset, u8int *buf, u32int count)
360 return rwpart(part, 1, offset, buf, count);
364 writepart(Part *part, u64int offset, u8int *buf, u32int count)
366 return rwpart(part, 0, offset, buf, count);
375 p = initpart(name, OREAD);
378 b = alloczblock(p->size, 0, p->blocksize);
380 seterr(EOk, "can't alloc %s: %r", name);
384 if(readpart(p, 0, b->data, p->size) < 0){
385 seterr(EOk, "can't read %s: %r", name);