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 #ifdef CANBLOCKSIZE
150 struct statfs sfs;
151 if(fstatfs(part->fd, &sfs) >= 0)
152 part->fsblocksize = sfs.f_bsize;
154 #endif
155 free(dir);
156 return part;
159 void
160 freepart(Part *part)
162 if(part == nil)
163 return;
164 if(part->fd >= 0)
165 close(part->fd);
166 free(part->name);
167 free(part);
170 void
171 partblocksize(Part *part, u32int blocksize)
173 if(part->blocksize)
174 sysfatal("resetting partition=%s's block size", part->name);
175 part->blocksize = blocksize;
176 if(blocksize > maxblocksize)
177 maxblocksize = blocksize;
180 /*
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.
193 */
194 #undef min
195 #define min(a, b) ((a) < (b) ? (a) : (b))
196 int
197 prwb(char *name, int fd, int isread, u64int offset, void *vbuf, u32int count, u32int blocksize)
199 char *op;
200 u8int *buf, *tmp, *freetmp, *dst;
201 u32int c, delta, icount, opsize;
202 int r;
204 buf = vbuf;
205 tmp = nil;
206 freetmp = nil;
207 icount = count;
208 opsize = blocksize;
210 if(count == 0){
211 logerr(EStrange, "pwrb %s called to %s 0 bytes", name, isread ? "read" : "write");
212 return 0;
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)
220 return -1;
221 tmp = freetmp;
222 tmp += blocksize - (ulong)tmp%blocksize;
225 /* handle beginning fringe */
226 if((delta = (ulong)offset%blocksize) != 0){
227 assert(tmp != nil);
228 if((r=pread(fd, tmp, blocksize, offset-delta)) != blocksize){
229 dst = tmp;
230 offset = offset-delta;
231 op = "read";
232 goto Error;
234 c = min(count, blocksize-delta);
235 assert(c > 0 && c < blocksize);
236 if(isread)
237 memmove(buf, tmp+delta, c);
238 else{
239 memmove(tmp+delta, buf, c);
240 if((r=pwrite(fd, tmp, blocksize, offset-delta)) != blocksize){
241 dst = tmp;
242 offset = offset-delta;
243 op = "read";
244 goto Error;
247 assert(c > 0);
248 offset += c;
249 buf += c;
250 count -= c;
253 /* handle full blocks */
254 while(count >= blocksize){
255 assert((ulong)offset%blocksize == 0);
256 if((ulong)buf%blocksize){
257 assert(tmp != nil);
258 dst = tmp;
259 opsize = blocksize;
260 }else{
261 dst = buf;
262 opsize = count - count%blocksize;
263 if(opsize > MaxIo)
264 opsize = MaxIo;
266 if(isread){
267 if((r=pread(fd, dst, opsize, offset))<=0 || r%blocksize){
268 op = "read";
269 goto Error;
271 if(dst == tmp){
272 assert(r == blocksize);
273 memmove(buf, tmp, blocksize);
275 }else{
276 if(dst == tmp){
277 assert(opsize == blocksize);
278 memmove(dst, buf, blocksize);
280 if((r=pwrite(fd, dst, opsize, offset))<=0 || r%blocksize){
281 op = "write";
282 goto Error;
284 if(dst == tmp)
285 assert(r == blocksize);
287 assert(r > 0);
288 offset += r;
289 buf += r;
290 count -= r;
293 /* handle ending fringe */
294 if(count > 0){
295 assert((ulong)offset%blocksize == 0);
296 assert(tmp != nil);
297 /*
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.)
302 */
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);
306 dst = tmp;
307 op = "read";
308 goto Error;
310 if(isread)
311 memmove(buf, tmp, count);
312 else{
313 memmove(tmp, buf, count);
314 if(pwrite(fd, tmp, opsize, offset) != blocksize){
315 dst = tmp;
316 op = "write";
317 goto Error;
321 if(freetmp)
322 free(freetmp);
323 return icount;
325 Error:
326 seterr(EAdmin, "%s %s offset 0x%llux count %ud buf %p returned %d: %r",
327 op, name, offset, opsize, dst, r);
328 if(freetmp)
329 free(freetmp);
330 return -1;
333 int
334 rwpart(Part *part, int isread, u64int offset, u8int *buf, u32int count)
336 u32int blocksize;
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);
343 return -1;
346 blocksize = part->fsblocksize;
347 if(blocksize == 0)
348 blocksize = part->blocksize;
349 if(blocksize == 0)
350 blocksize = 4096;
352 return prwb(part->filename, part->fd, isread, part->offset+offset, buf, count, blocksize);
355 int
356 readpart(Part *part, u64int offset, u8int *buf, u32int count)
358 return rwpart(part, 1, offset, buf, count);
361 int
362 writepart(Part *part, u64int offset, u8int *buf, u32int count)
364 return rwpart(part, 0, offset, buf, count);
367 ZBlock*
368 readfile(char *name)
370 Part *p;
371 ZBlock *b;
373 p = initpart(name, OREAD);
374 if(p == nil)
375 return nil;
376 b = alloczblock(p->size, 0, p->blocksize);
377 if(b == nil){
378 seterr(EOk, "can't alloc %s: %r", name);
379 freepart(p);
380 return nil;
382 if(readpart(p, 0, b->data, p->size) < 0){
383 seterr(EOk, "can't read %s: %r", name);
384 freepart(p);
385 freezblock(b);
386 return nil;
388 freepart(p);
389 return b;