1 a0d146ed 2005-07-12 devnull #ifdef PLAN9PORT /* SORRY! */
2 769eb198 2005-07-20 devnull # include <u.h>
3 769eb198 2005-07-20 devnull # include <sys/types.h>
4 769eb198 2005-07-20 devnull # ifdef __linux__ /* REALLY SORRY! */
5 769eb198 2005-07-20 devnull # define CANBLOCKSIZE 1
6 769eb198 2005-07-20 devnull # include <sys/vfs.h>
7 769eb198 2005-07-20 devnull # elif defined(__FreeBSD__)
8 769eb198 2005-07-20 devnull # define CANBLOCKSIZE 1
9 769eb198 2005-07-20 devnull # include <sys/param.h>
10 ba3c918e 2005-08-11 devnull # include <sys/stat.h>
11 769eb198 2005-07-20 devnull # include <sys/mount.h>
14 a0d146ed 2005-07-12 devnull #include "stdinc.h"
15 a0d146ed 2005-07-12 devnull #include <ctype.h>
16 a0d146ed 2005-07-12 devnull #include "dat.h"
17 a0d146ed 2005-07-12 devnull #include "fns.h"
19 a0d146ed 2005-07-12 devnull u32int maxblocksize;
20 a0d146ed 2005-07-12 devnull int readonly;
22 a0d146ed 2005-07-12 devnull static int
23 a0d146ed 2005-07-12 devnull strtoullsuf(char *p, char **pp, int rad, u64int *u)
25 a0d146ed 2005-07-12 devnull u64int v;
27 3bd56b04 2005-09-09 devnull if(!isdigit((uchar)*p))
28 a0d146ed 2005-07-12 devnull return -1;
29 a0d146ed 2005-07-12 devnull v = strtoull(p, &p, rad);
30 a0d146ed 2005-07-12 devnull switch(*p){
31 a0d146ed 2005-07-12 devnull case 'k':
32 a0d146ed 2005-07-12 devnull case 'K':
33 a0d146ed 2005-07-12 devnull v *= 1024;
36 a0d146ed 2005-07-12 devnull case 'm':
37 a0d146ed 2005-07-12 devnull case 'M':
38 a0d146ed 2005-07-12 devnull v *= 1024*1024;
41 a0d146ed 2005-07-12 devnull case 'g':
42 a0d146ed 2005-07-12 devnull case 'G':
43 a0d146ed 2005-07-12 devnull v *= 1024*1024*1024;
46 a0d146ed 2005-07-12 devnull case 't':
47 a0d146ed 2005-07-12 devnull case 'T':
48 a0d146ed 2005-07-12 devnull v *= 1024*1024;
49 a0d146ed 2005-07-12 devnull v *= 1024*1024;
55 a0d146ed 2005-07-12 devnull return 0;
58 a0d146ed 2005-07-12 devnull static int
59 a0d146ed 2005-07-12 devnull parsepart(char *name, char **file, u64int *lo, u64int *hi)
63 a0d146ed 2005-07-12 devnull *file = estrdup(name);
64 a0d146ed 2005-07-12 devnull if((p = strrchr(*file, ':')) == nil){
67 a0d146ed 2005-07-12 devnull return 0;
69 a0d146ed 2005-07-12 devnull *p++ = 0;
70 a0d146ed 2005-07-12 devnull if(*p == '-')
73 a0d146ed 2005-07-12 devnull if(strtoullsuf(p, &p, 0, lo) < 0){
74 a0d146ed 2005-07-12 devnull free(*file);
75 a0d146ed 2005-07-12 devnull return -1;
78 a0d146ed 2005-07-12 devnull if(*p == '-')
80 a0d146ed 2005-07-12 devnull if(*p == 0){
82 a0d146ed 2005-07-12 devnull return 0;
84 a0d146ed 2005-07-12 devnull if(strtoullsuf(p, &p, 0, hi) < 0 || *p != 0){
85 a0d146ed 2005-07-12 devnull free(*file);
86 a0d146ed 2005-07-12 devnull return -1;
88 a0d146ed 2005-07-12 devnull return 0;
92 a0d146ed 2005-07-12 devnull initpart(char *name, int mode)
94 a0d146ed 2005-07-12 devnull Part *part;
95 a0d146ed 2005-07-12 devnull Dir *dir;
96 a0d146ed 2005-07-12 devnull char *file;
97 a0d146ed 2005-07-12 devnull u64int lo, hi;
99 a0d146ed 2005-07-12 devnull if(parsepart(name, &file, &lo, &hi) < 0)
100 a0d146ed 2005-07-12 devnull return nil;
101 a0d146ed 2005-07-12 devnull trace(TraceDisk, "initpart %s file %s lo 0x%llx hi 0x%llx", name, file, lo, hi);
102 a0d146ed 2005-07-12 devnull part = MKZ(Part);
103 a0d146ed 2005-07-12 devnull part->name = estrdup(name);
104 a0d146ed 2005-07-12 devnull part->filename = estrdup(file);
105 a0d146ed 2005-07-12 devnull if(readonly){
106 a0d146ed 2005-07-12 devnull mode &= (OREAD|OWRITE|ORDWR);
107 a0d146ed 2005-07-12 devnull mode |= OREAD;
109 a0d146ed 2005-07-12 devnull part->fd = open(file, mode);
110 a0d146ed 2005-07-12 devnull if(part->fd < 0){
111 a0d146ed 2005-07-12 devnull if((mode&(OREAD|OWRITE|ORDWR)) == ORDWR)
112 a0d146ed 2005-07-12 devnull part->fd = open(file, (mode&~ORDWR)|OREAD);
113 a0d146ed 2005-07-12 devnull if(part->fd < 0){
114 a0d146ed 2005-07-12 devnull freepart(part);
115 a0d146ed 2005-07-12 devnull fprint(2, "can't open partition='%s': %r\n", file);
116 a0d146ed 2005-07-12 devnull seterr(EOk, "can't open partition='%s': %r", file);
117 a0d146ed 2005-07-12 devnull fprint(2, "%r\n");
118 a0d146ed 2005-07-12 devnull free(file);
119 a0d146ed 2005-07-12 devnull return nil;
121 a0d146ed 2005-07-12 devnull fprint(2, "warning: %s opened for reading only\n", name);
123 a0d146ed 2005-07-12 devnull part->offset = lo;
124 a0d146ed 2005-07-12 devnull dir = dirfstat(part->fd);
125 a0d146ed 2005-07-12 devnull if(dir == nil){
126 a0d146ed 2005-07-12 devnull freepart(part);
127 a0d146ed 2005-07-12 devnull seterr(EOk, "can't stat partition='%s': %r", file);
128 a0d146ed 2005-07-12 devnull free(file);
129 a0d146ed 2005-07-12 devnull return nil;
131 a0d146ed 2005-07-12 devnull if(dir->length == 0){
132 a0d146ed 2005-07-12 devnull free(dir);
133 a0d146ed 2005-07-12 devnull freepart(part);
134 a0d146ed 2005-07-12 devnull seterr(EOk, "can't determine size of partition %s", file);
135 a0d146ed 2005-07-12 devnull free(file);
136 a0d146ed 2005-07-12 devnull return nil;
138 a0d146ed 2005-07-12 devnull if(dir->length < hi || dir->length < lo){
139 a0d146ed 2005-07-12 devnull freepart(part);
140 a0d146ed 2005-07-12 devnull seterr(EOk, "partition '%s': bounds out of range (max %lld)", name, dir->length);
141 a0d146ed 2005-07-12 devnull free(dir);
142 a0d146ed 2005-07-12 devnull free(file);
143 a0d146ed 2005-07-12 devnull return nil;
145 a0d146ed 2005-07-12 devnull if(hi == 0)
146 a0d146ed 2005-07-12 devnull hi = dir->length;
147 a0d146ed 2005-07-12 devnull part->size = hi - part->offset;
148 bdbc55d3 2005-11-28 devnull fprint(2, "part %s: file %s offset %,lld size %,lld\n",
149 bdbc55d3 2005-11-28 devnull name, file, part->offset, part->size);
150 0613fe4b 2005-07-19 devnull #ifdef CANBLOCKSIZE
152 a0d146ed 2005-07-12 devnull struct statfs sfs;
153 a0d146ed 2005-07-12 devnull if(fstatfs(part->fd, &sfs) >= 0)
154 a0d146ed 2005-07-12 devnull part->fsblocksize = sfs.f_bsize;
157 a0d146ed 2005-07-12 devnull free(dir);
158 a0d146ed 2005-07-12 devnull return part;
162 a0d146ed 2005-07-12 devnull freepart(Part *part)
164 a0d146ed 2005-07-12 devnull if(part == nil)
166 a0d146ed 2005-07-12 devnull if(part->fd >= 0)
167 a0d146ed 2005-07-12 devnull close(part->fd);
168 a0d146ed 2005-07-12 devnull free(part->name);
169 a0d146ed 2005-07-12 devnull free(part);
173 a0d146ed 2005-07-12 devnull partblocksize(Part *part, u32int blocksize)
175 a0d146ed 2005-07-12 devnull if(part->blocksize)
176 a0d146ed 2005-07-12 devnull sysfatal("resetting partition=%s's block size", part->name);
177 a0d146ed 2005-07-12 devnull part->blocksize = blocksize;
178 a0d146ed 2005-07-12 devnull if(blocksize > maxblocksize)
179 a0d146ed 2005-07-12 devnull maxblocksize = blocksize;
183 a0d146ed 2005-07-12 devnull * Read/write some amount of data between a block device or file and a memory buffer.
185 a0d146ed 2005-07-12 devnull * Most Unix systems require that when accessing a block device directly,
186 a0d146ed 2005-07-12 devnull * the buffer, offset, and count are all multiples of the device block size,
187 a0d146ed 2005-07-12 devnull * making this a lot more complicated than it otherwise would be.
189 a0d146ed 2005-07-12 devnull * Most of our callers will make things easy on us, but for some callers it's best
190 a0d146ed 2005-07-12 devnull * if we just do the work here, with only one place to get it right (hopefully).
192 a0d146ed 2005-07-12 devnull * If everything is aligned properly, prwb will try to do big transfers in the main
193 a0d146ed 2005-07-12 devnull * body of the loop: up to MaxIo bytes at a time. If everything isn't aligned properly,
194 a0d146ed 2005-07-12 devnull * we work one block at a time.
196 a0d146ed 2005-07-12 devnull #undef min
197 a0d146ed 2005-07-12 devnull #define min(a, b) ((a) < (b) ? (a) : (b))
199 a0d146ed 2005-07-12 devnull prwb(char *name, int fd, int isread, u64int offset, void *vbuf, u32int count, u32int blocksize)
201 a0d146ed 2005-07-12 devnull char *op;
202 a0d146ed 2005-07-12 devnull u8int *buf, *tmp, *freetmp, *dst;
203 a0d146ed 2005-07-12 devnull u32int c, delta, icount, opsize;
206 a0d146ed 2005-07-12 devnull buf = vbuf;
207 a0d146ed 2005-07-12 devnull tmp = nil;
208 a0d146ed 2005-07-12 devnull freetmp = nil;
209 a0d146ed 2005-07-12 devnull icount = count;
210 a0d146ed 2005-07-12 devnull opsize = blocksize;
212 a0d146ed 2005-07-12 devnull if(count == 0){
213 a0d146ed 2005-07-12 devnull logerr(EStrange, "pwrb %s called to %s 0 bytes", name, isread ? "read" : "write");
214 a0d146ed 2005-07-12 devnull return 0;
217 a0d146ed 2005-07-12 devnull assert(blocksize > 0);
219 a0d146ed 2005-07-12 devnull /* allocate blocksize-aligned temp buffer if needed */
220 a0d146ed 2005-07-12 devnull if((ulong)offset%blocksize || (ulong)buf%blocksize || count%blocksize){
221 a0d146ed 2005-07-12 devnull if((freetmp = malloc(blocksize*2)) == nil)
222 a0d146ed 2005-07-12 devnull return -1;
223 a0d146ed 2005-07-12 devnull tmp = freetmp;
224 a0d146ed 2005-07-12 devnull tmp += blocksize - (ulong)tmp%blocksize;
227 a0d146ed 2005-07-12 devnull /* handle beginning fringe */
228 a0d146ed 2005-07-12 devnull if((delta = (ulong)offset%blocksize) != 0){
229 a0d146ed 2005-07-12 devnull assert(tmp != nil);
230 a0d146ed 2005-07-12 devnull if((r=pread(fd, tmp, blocksize, offset-delta)) != blocksize){
231 a0d146ed 2005-07-12 devnull dst = tmp;
232 a0d146ed 2005-07-12 devnull offset = offset-delta;
233 a0d146ed 2005-07-12 devnull op = "read";
234 a0d146ed 2005-07-12 devnull goto Error;
236 a0d146ed 2005-07-12 devnull c = min(count, blocksize-delta);
237 a0d146ed 2005-07-12 devnull assert(c > 0 && c < blocksize);
238 a0d146ed 2005-07-12 devnull if(isread)
239 a0d146ed 2005-07-12 devnull memmove(buf, tmp+delta, c);
241 a0d146ed 2005-07-12 devnull memmove(tmp+delta, buf, c);
242 a0d146ed 2005-07-12 devnull if((r=pwrite(fd, tmp, blocksize, offset-delta)) != blocksize){
243 a0d146ed 2005-07-12 devnull dst = tmp;
244 a0d146ed 2005-07-12 devnull offset = offset-delta;
245 a0d146ed 2005-07-12 devnull op = "read";
246 a0d146ed 2005-07-12 devnull goto Error;
249 a0d146ed 2005-07-12 devnull assert(c > 0);
250 a0d146ed 2005-07-12 devnull offset += c;
251 a0d146ed 2005-07-12 devnull buf += c;
252 a0d146ed 2005-07-12 devnull count -= c;
255 a0d146ed 2005-07-12 devnull /* handle full blocks */
256 a0d146ed 2005-07-12 devnull while(count >= blocksize){
257 a0d146ed 2005-07-12 devnull assert((ulong)offset%blocksize == 0);
258 a0d146ed 2005-07-12 devnull if((ulong)buf%blocksize){
259 a0d146ed 2005-07-12 devnull assert(tmp != nil);
260 a0d146ed 2005-07-12 devnull dst = tmp;
261 a0d146ed 2005-07-12 devnull opsize = blocksize;
263 a0d146ed 2005-07-12 devnull dst = buf;
264 a0d146ed 2005-07-12 devnull opsize = count - count%blocksize;
265 a0d146ed 2005-07-12 devnull if(opsize > MaxIo)
266 a0d146ed 2005-07-12 devnull opsize = MaxIo;
268 a0d146ed 2005-07-12 devnull if(isread){
269 a0d146ed 2005-07-12 devnull if((r=pread(fd, dst, opsize, offset))<=0 || r%blocksize){
270 a0d146ed 2005-07-12 devnull op = "read";
271 a0d146ed 2005-07-12 devnull goto Error;
273 a0d146ed 2005-07-12 devnull if(dst == tmp){
274 a0d146ed 2005-07-12 devnull assert(r == blocksize);
275 a0d146ed 2005-07-12 devnull memmove(buf, tmp, blocksize);
278 a0d146ed 2005-07-12 devnull if(dst == tmp){
279 a0d146ed 2005-07-12 devnull assert(opsize == blocksize);
280 a0d146ed 2005-07-12 devnull memmove(dst, buf, blocksize);
282 a0d146ed 2005-07-12 devnull if((r=pwrite(fd, dst, opsize, offset))<=0 || r%blocksize){
283 a0d146ed 2005-07-12 devnull op = "write";
284 a0d146ed 2005-07-12 devnull goto Error;
286 a0d146ed 2005-07-12 devnull if(dst == tmp)
287 a0d146ed 2005-07-12 devnull assert(r == blocksize);
289 a0d146ed 2005-07-12 devnull assert(r > 0);
290 a0d146ed 2005-07-12 devnull offset += r;
291 a0d146ed 2005-07-12 devnull buf += r;
292 a0d146ed 2005-07-12 devnull count -= r;
295 a0d146ed 2005-07-12 devnull /* handle ending fringe */
296 a0d146ed 2005-07-12 devnull if(count > 0){
297 a0d146ed 2005-07-12 devnull assert((ulong)offset%blocksize == 0);
298 a0d146ed 2005-07-12 devnull assert(tmp != nil);
300 a0d146ed 2005-07-12 devnull * Complicated condition: if we're reading it's okay to get less than
301 a0d146ed 2005-07-12 devnull * a block as long as it's enough to satisfy the read - maybe this is
302 a0d146ed 2005-07-12 devnull * a normal file. (We never write to normal files, or else things would
303 a0d146ed 2005-07-12 devnull * be even more complicated.)
305 a0d146ed 2005-07-12 devnull r = pread(fd, tmp, blocksize, offset);
306 a0d146ed 2005-07-12 devnull if((isread && r < count) || (!isread && r != blocksize)){
307 a0d146ed 2005-07-12 devnull print("FAILED isread=%d r=%d count=%d blocksize=%d\n", isread, r, count, blocksize);
308 a0d146ed 2005-07-12 devnull dst = tmp;
309 a0d146ed 2005-07-12 devnull op = "read";
310 a0d146ed 2005-07-12 devnull goto Error;
312 a0d146ed 2005-07-12 devnull if(isread)
313 a0d146ed 2005-07-12 devnull memmove(buf, tmp, count);
315 a0d146ed 2005-07-12 devnull memmove(tmp, buf, count);
316 a0d146ed 2005-07-12 devnull if(pwrite(fd, tmp, opsize, offset) != blocksize){
317 a0d146ed 2005-07-12 devnull dst = tmp;
318 a0d146ed 2005-07-12 devnull op = "write";
319 a0d146ed 2005-07-12 devnull goto Error;
323 a0d146ed 2005-07-12 devnull if(freetmp)
324 a0d146ed 2005-07-12 devnull free(freetmp);
325 a0d146ed 2005-07-12 devnull return icount;
328 a0d146ed 2005-07-12 devnull seterr(EAdmin, "%s %s offset 0x%llux count %ud buf %p returned %d: %r",
329 a0d146ed 2005-07-12 devnull op, name, offset, opsize, dst, r);
330 a0d146ed 2005-07-12 devnull if(freetmp)
331 a0d146ed 2005-07-12 devnull free(freetmp);
332 a0d146ed 2005-07-12 devnull return -1;
336 a0d146ed 2005-07-12 devnull rwpart(Part *part, int isread, u64int offset, u8int *buf, u32int count)
338 a0d146ed 2005-07-12 devnull u32int blocksize;
340 a0d146ed 2005-07-12 devnull trace(TraceDisk, "%s %s %ud at 0x%llx",
341 a0d146ed 2005-07-12 devnull isread ? "read" : "write", part->name, count, offset);
342 a0d146ed 2005-07-12 devnull if(offset >= part->size || offset+count > part->size){
343 a0d146ed 2005-07-12 devnull seterr(EStrange, "out of bounds %s offset 0x%llux count %ud to partition %s size 0x%llux",
344 a0d146ed 2005-07-12 devnull isread ? "read" : "write", offset, count, part->name, part->size);
345 a0d146ed 2005-07-12 devnull return -1;
348 a0d146ed 2005-07-12 devnull blocksize = part->fsblocksize;
349 a0d146ed 2005-07-12 devnull if(blocksize == 0)
350 a0d146ed 2005-07-12 devnull blocksize = part->blocksize;
351 a0d146ed 2005-07-12 devnull if(blocksize == 0)
352 a0d146ed 2005-07-12 devnull blocksize = 4096;
354 a0d146ed 2005-07-12 devnull return prwb(part->filename, part->fd, isread, part->offset+offset, buf, count, blocksize);
358 a0d146ed 2005-07-12 devnull readpart(Part *part, u64int offset, u8int *buf, u32int count)
360 a0d146ed 2005-07-12 devnull return rwpart(part, 1, offset, buf, count);
364 a0d146ed 2005-07-12 devnull writepart(Part *part, u64int offset, u8int *buf, u32int count)
366 a0d146ed 2005-07-12 devnull return rwpart(part, 0, offset, buf, count);
370 a0d146ed 2005-07-12 devnull readfile(char *name)
372 a0d146ed 2005-07-12 devnull Part *p;
373 a0d146ed 2005-07-12 devnull ZBlock *b;
375 a0d146ed 2005-07-12 devnull p = initpart(name, OREAD);
376 a0d146ed 2005-07-12 devnull if(p == nil)
377 a0d146ed 2005-07-12 devnull return nil;
378 a0d146ed 2005-07-12 devnull b = alloczblock(p->size, 0, p->blocksize);
379 a0d146ed 2005-07-12 devnull if(b == nil){
380 a0d146ed 2005-07-12 devnull seterr(EOk, "can't alloc %s: %r", name);
381 a0d146ed 2005-07-12 devnull freepart(p);
382 a0d146ed 2005-07-12 devnull return nil;
384 a0d146ed 2005-07-12 devnull if(readpart(p, 0, b->data, p->size) < 0){
385 a0d146ed 2005-07-12 devnull seterr(EOk, "can't read %s: %r", name);
386 a0d146ed 2005-07-12 devnull freepart(p);
387 a0d146ed 2005-07-12 devnull freezblock(b);
388 a0d146ed 2005-07-12 devnull return nil;
390 a0d146ed 2005-07-12 devnull freepart(p);
391 a0d146ed 2005-07-12 devnull return b;