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;
151 if(fstatfs(part->fd, &sfs) >= 0)
152 part->fsblocksize = sfs.f_bsize;
171 partblocksize(Part *part, u32int blocksize)
174 sysfatal("resetting partition=%s's block size", part->name);
175 part->blocksize = blocksize;
176 if(blocksize > maxblocksize)
177 maxblocksize = blocksize;
181 * Read/write some amount of data between a block device or file and a memory buffer.
183 * Most Unix systems require that when accessing a block device directly,
184 * the buffer, offset, and count are all multiples of the device block size,
185 * making this a lot more complicated than it otherwise would be.
187 * Most of our callers will make things easy on us, but for some callers it's best
188 * if we just do the work here, with only one place to get it right (hopefully).
190 * If everything is aligned properly, prwb will try to do big transfers in the main
191 * body of the loop: up to MaxIo bytes at a time. If everything isn't aligned properly,
192 * we work one block at a time.
195 #define min(a, b) ((a) < (b) ? (a) : (b))
197 prwb(char *name, int fd, int isread, u64int offset, void *vbuf, u32int count, u32int blocksize)
200 u8int *buf, *tmp, *freetmp, *dst;
201 u32int c, delta, icount, opsize;
211 logerr(EStrange, "pwrb %s called to %s 0 bytes", name, isread ? "read" : "write");
215 assert(blocksize > 0);
217 /* allocate blocksize-aligned temp buffer if needed */
218 if((ulong)offset%blocksize || (ulong)buf%blocksize || count%blocksize){
219 if((freetmp = malloc(blocksize*2)) == nil)
222 tmp += blocksize - (ulong)tmp%blocksize;
225 /* handle beginning fringe */
226 if((delta = (ulong)offset%blocksize) != 0){
228 if((r=pread(fd, tmp, blocksize, offset-delta)) != blocksize){
230 offset = offset-delta;
234 c = min(count, blocksize-delta);
235 assert(c > 0 && c < blocksize);
237 memmove(buf, tmp+delta, c);
239 memmove(tmp+delta, buf, c);
240 if((r=pwrite(fd, tmp, blocksize, offset-delta)) != blocksize){
242 offset = offset-delta;
253 /* handle full blocks */
254 while(count >= blocksize){
255 assert((ulong)offset%blocksize == 0);
256 if((ulong)buf%blocksize){
262 opsize = count - count%blocksize;
267 if((r=pread(fd, dst, opsize, offset))<=0 || r%blocksize){
272 assert(r == blocksize);
273 memmove(buf, tmp, blocksize);
277 assert(opsize == blocksize);
278 memmove(dst, buf, blocksize);
280 if((r=pwrite(fd, dst, opsize, offset))<=0 || r%blocksize){
285 assert(r == blocksize);
293 /* handle ending fringe */
295 assert((ulong)offset%blocksize == 0);
298 * Complicated condition: if we're reading it's okay to get less than
299 * a block as long as it's enough to satisfy the read - maybe this is
300 * a normal file. (We never write to normal files, or else things would
301 * be even more complicated.)
303 r = pread(fd, tmp, blocksize, offset);
304 if((isread && r < count) || (!isread && r != blocksize)){
305 print("FAILED isread=%d r=%d count=%d blocksize=%d\n", isread, r, count, blocksize);
311 memmove(buf, tmp, count);
313 memmove(tmp, buf, count);
314 if(pwrite(fd, tmp, opsize, offset) != blocksize){
326 seterr(EAdmin, "%s %s offset 0x%llux count %ud buf %p returned %d: %r",
327 op, name, offset, opsize, dst, r);
334 rwpart(Part *part, int isread, u64int offset, u8int *buf, u32int count)
338 trace(TraceDisk, "%s %s %ud at 0x%llx",
339 isread ? "read" : "write", part->name, count, offset);
340 if(offset >= part->size || offset+count > part->size){
341 seterr(EStrange, "out of bounds %s offset 0x%llux count %ud to partition %s size 0x%llux",
342 isread ? "read" : "write", offset, count, part->name, part->size);
346 blocksize = part->fsblocksize;
348 blocksize = part->blocksize;
352 return prwb(part->filename, part->fd, isread, part->offset+offset, buf, count, blocksize);
356 readpart(Part *part, u64int offset, u8int *buf, u32int count)
358 return rwpart(part, 1, offset, buf, count);
362 writepart(Part *part, u64int offset, u8int *buf, u32int count)
364 return rwpart(part, 0, offset, buf, count);
373 p = initpart(name, OREAD);
376 b = alloczblock(p->size, 0, p->blocksize);
378 seterr(EOk, "can't alloc %s: %r", name);
382 if(readpart(p, 0, b->data, p->size) < 0){
383 seterr(EOk, "can't read %s: %r", name);