Blob


1 #ifdef PLAN9PORT /* SORRY! */
2 # include <u.h>
3 # include <sys/types.h>
4 # ifdef __linux__ /* REALLY SORRY! */
5 # define CANBLOCKSIZE 1
6 # include <sys/vfs.h>
7 # elif defined(__FreeBSD__)
8 # define CANBLOCKSIZE 1
9 # include <sys/param.h>
10 # include <sys/stat.h>
11 # include <sys/mount.h>
12 # endif
13 #endif
14 #include "stdinc.h"
15 #include <ctype.h>
16 #include "dat.h"
17 #include "fns.h"
19 u32int maxblocksize;
20 int readonly;
22 static int
23 strtoullsuf(char *p, char **pp, int rad, u64int *u)
24 {
25 u64int v;
27 if(!isdigit((uchar)*p))
28 return -1;
29 v = strtoull(p, &p, rad);
30 switch(*p){
31 case 'k':
32 case 'K':
33 v *= 1024;
34 p++;
35 break;
36 case 'm':
37 case 'M':
38 v *= 1024*1024;
39 p++;
40 break;
41 case 'g':
42 case 'G':
43 v *= 1024*1024*1024;
44 p++;
45 break;
46 case 't':
47 case 'T':
48 v *= 1024*1024;
49 v *= 1024*1024;
50 p++;
51 break;
52 }
53 *pp = p;
54 *u = v;
55 return 0;
56 }
58 static int
59 parsepart(char *name, char **file, u64int *lo, u64int *hi)
60 {
61 char *p;
63 *file = estrdup(name);
64 if((p = strrchr(*file, ':')) == nil){
65 *lo = 0;
66 *hi = 0;
67 return 0;
68 }
69 *p++ = 0;
70 if(*p == '-')
71 *lo = 0;
72 else{
73 if(strtoullsuf(p, &p, 0, lo) < 0){
74 free(*file);
75 return -1;
76 }
77 }
78 if(*p == '-')
79 p++;
80 if(*p == 0){
81 *hi = 0;
82 return 0;
83 }
84 if(strtoullsuf(p, &p, 0, hi) < 0 || *p != 0){
85 free(*file);
86 return -1;
87 }
88 return 0;
89 }
91 Part*
92 initpart(char *name, int mode)
93 {
94 Part *part;
95 Dir *dir;
96 char *file;
97 u64int lo, hi;
99 if(parsepart(name, &file, &lo, &hi) < 0)
100 return nil;
101 trace(TraceDisk, "initpart %s file %s lo 0x%llx hi 0x%llx", name, file, lo, hi);
102 part = MKZ(Part);
103 part->name = estrdup(name);
104 part->filename = estrdup(file);
105 if(readonly){
106 mode &= (OREAD|OWRITE|ORDWR);
107 mode |= OREAD;
109 part->fd = open(file, mode);
110 if(part->fd < 0){
111 if((mode&(OREAD|OWRITE|ORDWR)) == ORDWR)
112 part->fd = open(file, (mode&~ORDWR)|OREAD);
113 if(part->fd < 0){
114 freepart(part);
115 fprint(2, "can't open partition='%s': %r\n", file);
116 seterr(EOk, "can't open partition='%s': %r", file);
117 fprint(2, "%r\n");
118 free(file);
119 return nil;
121 fprint(2, "warning: %s opened for reading only\n", name);
123 part->offset = lo;
124 dir = dirfstat(part->fd);
125 if(dir == nil){
126 freepart(part);
127 seterr(EOk, "can't stat partition='%s': %r", file);
128 free(file);
129 return nil;
131 if(dir->length == 0){
132 free(dir);
133 freepart(part);
134 seterr(EOk, "can't determine size of partition %s", file);
135 free(file);
136 return nil;
138 if(dir->length < hi || dir->length < lo){
139 freepart(part);
140 seterr(EOk, "partition '%s': bounds out of range (max %lld)", name, dir->length);
141 free(dir);
142 free(file);
143 return nil;
145 if(hi == 0)
146 hi = 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);
150 #ifdef CANBLOCKSIZE
152 struct statfs sfs;
153 if(fstatfs(part->fd, &sfs) >= 0)
154 part->fsblocksize = sfs.f_bsize;
156 #endif
157 free(dir);
158 return part;
161 void
162 freepart(Part *part)
164 if(part == nil)
165 return;
166 if(part->fd >= 0)
167 close(part->fd);
168 free(part->name);
169 free(part);
172 void
173 partblocksize(Part *part, u32int blocksize)
175 if(part->blocksize)
176 sysfatal("resetting partition=%s's block size", part->name);
177 part->blocksize = blocksize;
178 if(blocksize > maxblocksize)
179 maxblocksize = blocksize;
182 /*
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.
195 */
196 #undef min
197 #define min(a, b) ((a) < (b) ? (a) : (b))
198 int
199 prwb(char *name, int fd, int isread, u64int offset, void *vbuf, u32int count, u32int blocksize)
201 char *op;
202 u8int *buf, *tmp, *freetmp, *dst;
203 u32int c, delta, icount, opsize;
204 int r;
206 buf = vbuf;
207 tmp = nil;
208 freetmp = nil;
209 icount = count;
210 opsize = blocksize;
212 if(count == 0){
213 logerr(EStrange, "pwrb %s called to %s 0 bytes", name, isread ? "read" : "write");
214 return 0;
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)
222 return -1;
223 tmp = freetmp;
224 tmp += blocksize - (ulong)tmp%blocksize;
227 /* handle beginning fringe */
228 if((delta = (ulong)offset%blocksize) != 0){
229 assert(tmp != nil);
230 if((r=pread(fd, tmp, blocksize, offset-delta)) != blocksize){
231 dst = tmp;
232 offset = offset-delta;
233 op = "read";
234 goto Error;
236 c = min(count, blocksize-delta);
237 assert(c > 0 && c < blocksize);
238 if(isread)
239 memmove(buf, tmp+delta, c);
240 else{
241 memmove(tmp+delta, buf, c);
242 if((r=pwrite(fd, tmp, blocksize, offset-delta)) != blocksize){
243 dst = tmp;
244 offset = offset-delta;
245 op = "read";
246 goto Error;
249 assert(c > 0);
250 offset += c;
251 buf += c;
252 count -= c;
255 /* handle full blocks */
256 while(count >= blocksize){
257 assert((ulong)offset%blocksize == 0);
258 if((ulong)buf%blocksize){
259 assert(tmp != nil);
260 dst = tmp;
261 opsize = blocksize;
262 }else{
263 dst = buf;
264 opsize = count - count%blocksize;
265 if(opsize > MaxIo)
266 opsize = MaxIo;
268 if(isread){
269 if((r=pread(fd, dst, opsize, offset))<=0 || r%blocksize){
270 op = "read";
271 goto Error;
273 if(dst == tmp){
274 assert(r == blocksize);
275 memmove(buf, tmp, blocksize);
277 }else{
278 if(dst == tmp){
279 assert(opsize == blocksize);
280 memmove(dst, buf, blocksize);
282 if((r=pwrite(fd, dst, opsize, offset))<=0 || r%blocksize){
283 op = "write";
284 goto Error;
286 if(dst == tmp)
287 assert(r == blocksize);
289 assert(r > 0);
290 offset += r;
291 buf += r;
292 count -= r;
295 /* handle ending fringe */
296 if(count > 0){
297 assert((ulong)offset%blocksize == 0);
298 assert(tmp != nil);
299 /*
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.)
304 */
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);
308 dst = tmp;
309 op = "read";
310 goto Error;
312 if(isread)
313 memmove(buf, tmp, count);
314 else{
315 memmove(tmp, buf, count);
316 if(pwrite(fd, tmp, opsize, offset) != blocksize){
317 dst = tmp;
318 op = "write";
319 goto Error;
323 if(freetmp)
324 free(freetmp);
325 return icount;
327 Error:
328 seterr(EAdmin, "%s %s offset 0x%llux count %ud buf %p returned %d: %r",
329 op, name, offset, opsize, dst, r);
330 if(freetmp)
331 free(freetmp);
332 return -1;
335 int
336 rwpart(Part *part, int isread, u64int offset, u8int *buf, u32int count)
338 u32int blocksize;
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);
345 return -1;
348 blocksize = part->fsblocksize;
349 if(blocksize == 0)
350 blocksize = part->blocksize;
351 if(blocksize == 0)
352 blocksize = 4096;
354 return prwb(part->filename, part->fd, isread, part->offset+offset, buf, count, blocksize);
357 int
358 readpart(Part *part, u64int offset, u8int *buf, u32int count)
360 return rwpart(part, 1, offset, buf, count);
363 int
364 writepart(Part *part, u64int offset, u8int *buf, u32int count)
366 return rwpart(part, 0, offset, buf, count);
369 ZBlock*
370 readfile(char *name)
372 Part *p;
373 ZBlock *b;
375 p = initpart(name, OREAD);
376 if(p == nil)
377 return nil;
378 b = alloczblock(p->size, 0, p->blocksize);
379 if(b == nil){
380 seterr(EOk, "can't alloc %s: %r", name);
381 freepart(p);
382 return nil;
384 if(readpart(p, 0, b->data, p->size) < 0){
385 seterr(EOk, "can't read %s: %r", name);
386 freepart(p);
387 freezblock(b);
388 return nil;
390 freepart(p);
391 return b;