Blob


1 #include <u.h>
2 #include <libc.h>
3 #include <thread.h>
4 #include <sunrpc.h>
5 #include <nfs3.h>
6 #include <diskfs.h>
7 #include "ext2.h"
9 static void parsedirent(Dirent*, uchar*);
10 static void parseinode(Inode*, uchar*);
11 static void parsegroup(Group*, uchar*);
12 static void parsesuper(Super*, uchar*);
14 #define debug 0
16 static int ext2sync(Fsys*);
17 static void ext2close(Fsys*);
18 static Block* ext2blockread(Fsys*, u64int);
20 static Nfs3Status ext2root(Fsys*, Nfs3Handle*);
21 static Nfs3Status ext2getattr(Fsys*, SunAuthUnix *au, Nfs3Handle*, Nfs3Attr*);
22 static Nfs3Status ext2lookup(Fsys*, SunAuthUnix *au, Nfs3Handle*, char*, Nfs3Handle*);
23 static Nfs3Status ext2readfile(Fsys*, SunAuthUnix *au, Nfs3Handle*, u32int, u64int, uchar**, u32int*, u1int*);
24 static Nfs3Status ext2readlink(Fsys *fsys, SunAuthUnix *au, Nfs3Handle *h, char **link);
25 static Nfs3Status ext2readdir(Fsys *fsys, SunAuthUnix *au, Nfs3Handle *h, u32int, u64int, uchar**, u32int*, u1int*);
26 static Nfs3Status ext2access(Fsys *fsys, SunAuthUnix *au, Nfs3Handle *h, u32int want, u32int *got, Nfs3Attr *attr);
28 Fsys*
29 fsysopenext2(Disk *disk)
30 {
31 Ext2 *fs;
32 Fsys *fsys;
34 fsys = emalloc(sizeof(Fsys));
35 fs = emalloc(sizeof(Ext2));
36 fs->disk = disk;
37 fsys->priv = fs;
38 fs->fsys = fsys;
39 fsys->type = "ext2";
40 fsys->_readblock = ext2blockread;
41 fsys->_sync = ext2sync;
42 fsys->_root = ext2root;
43 fsys->_getattr = ext2getattr;
44 fsys->_access = ext2access;
45 fsys->_lookup = ext2lookup;
46 fsys->_readfile = ext2readfile;
47 fsys->_readlink = ext2readlink;
48 fsys->_readdir = ext2readdir;
49 fsys->_close = ext2close;
51 if(ext2sync(fsys) < 0)
52 goto error;
54 return fsys;
56 error:
57 ext2close(fsys);
58 return nil;
59 }
61 static void
62 ext2close(Fsys *fsys)
63 {
64 Ext2 *fs;
66 fs = fsys->priv;
67 free(fs);
68 free(fsys);
69 }
71 static int
72 ext2group(Ext2 *fs, u32int i, Group *g)
73 {
74 Block *b;
75 u64int addr;
77 if(i >= fs->ngroup)
78 return -1;
80 addr = fs->groupaddr + i/fs->descperblock;
81 b = diskread(fs->disk, fs->blocksize, addr*fs->blocksize);
82 if(b == nil)
83 return -1;
84 parsegroup(g, b->data+i%fs->descperblock*GroupSize);
85 blockput(b);
86 return 0;
87 }
89 static Block*
90 ext2blockread(Fsys *fsys, u64int vbno)
91 {
92 Block *bitb;
93 Group g;
94 uchar *bits;
95 u32int bno, boff, bitblock;
96 u64int bitpos;
97 Ext2 *fs;
99 fs = fsys->priv;
100 if(vbno >= fs->nblock)
101 return nil;
102 bno = vbno;
103 if(bno != vbno)
104 return nil;
106 /*
107 if(bno < fs->firstblock)
108 return diskread(fs->disk, fs->blocksize, (u64int)bno*fs->blocksize);
109 */
110 if(bno < fs->firstblock)
111 return nil;
113 bno -= fs->firstblock;
114 if(ext2group(fs, bno/fs->blockspergroup, &g) < 0){
115 if(debug)
116 fprint(2, "loading group: %r...");
117 return nil;
119 /*
120 if(debug)
121 fprint(2, "ext2 group %d: bitblock=%ud inodebitblock=%ud inodeaddr=%ud freeblocks=%ud freeinodes=%ud useddirs=%ud\n",
122 (int)(bno/fs->blockspergroup),
123 g.bitblock,
124 g.inodebitblock,
125 g.inodeaddr,
126 g.freeblockscount,
127 g.freeinodescount,
128 g.useddirscount);
129 if(debug)
130 fprint(2, "group %d bitblock=%d...", bno/fs->blockspergroup, g.bitblock);
131 */
132 bitblock = g.bitblock;
133 bitpos = (u64int)bitblock*fs->blocksize;
135 if((bitb = diskread(fs->disk, fs->blocksize, bitpos)) == nil){
136 if(debug)
137 fprint(2, "loading bitblock: %r...");
138 return nil;
140 bits = bitb->data;
141 boff = bno%fs->blockspergroup;
142 if((bits[boff>>3] & (1<<(boff&7))) == 0){
143 if(debug)
144 fprint(2, "block %d not allocated in group %d: bitblock %d/%lld bits[%d] = %#x\n",
145 boff, bno/fs->blockspergroup,
146 (int)bitblock,
147 bitpos,
148 boff>>3,
149 bits[boff>>3]);
150 blockput(bitb);
151 return nil;
153 blockput(bitb);
155 bno += fs->firstblock;
156 return diskread(fs->disk, fs->blocksize, (u64int)bno*fs->blocksize);
159 static Block*
160 ext2datablock(Ext2 *fs, u32int bno, int size)
162 USED(size);
163 return ext2blockread(fs->fsys, bno);
166 static Block*
167 ext2fileblock(Ext2 *fs, Inode *ino, u32int bno, int size)
169 int ppb;
170 Block *b;
171 u32int *a;
172 u32int obno, pbno;
174 obno = bno;
175 if(bno < NDIRBLOCKS){
176 if(debug)
177 fprint(2, "fileblock %d -> %d...",
178 bno, ino->block[bno]);
179 return ext2datablock(fs, ino->block[bno], size);
181 bno -= NDIRBLOCKS;
182 ppb = fs->blocksize/4;
184 /* one indirect */
185 if(bno < ppb){
186 b = ext2datablock(fs, ino->block[INDBLOCK], fs->blocksize);
187 if(b == nil)
188 return nil;
189 a = (u32int*)b->data;
190 bno = a[bno];
191 blockput(b);
192 return ext2datablock(fs, bno, size);
194 bno -= ppb;
196 /* one double indirect */
197 if(bno < ppb*ppb){
198 b = ext2datablock(fs, ino->block[DINDBLOCK], fs->blocksize);
199 if(b == nil)
200 return nil;
201 a = (u32int*)b->data;
202 pbno = a[bno/ppb];
203 bno = bno%ppb;
204 blockput(b);
205 b = ext2datablock(fs, pbno, fs->blocksize);
206 if(b == nil)
207 return nil;
208 a = (u32int*)b->data;
209 bno = a[bno];
210 blockput(b);
211 return ext2datablock(fs, bno, size);
213 bno -= ppb*ppb;
215 /* one triple indirect */
216 if(bno < ppb*ppb*ppb){
217 b = ext2datablock(fs, ino->block[TINDBLOCK], fs->blocksize);
218 if(b == nil)
219 return nil;
220 a = (u32int*)b->data;
221 pbno = a[bno/(ppb*ppb)];
222 bno = bno%(ppb*ppb);
223 blockput(b);
224 b = ext2datablock(fs, pbno, fs->blocksize);
225 if(b == nil)
226 return nil;
227 a = (u32int*)b->data;
228 pbno = a[bno/ppb];
229 bno = bno%ppb;
230 blockput(b);
231 b = ext2datablock(fs, pbno, fs->blocksize);
232 if(b == nil)
233 return nil;
234 a = (u32int*)b->data;
235 bno = a[bno];
236 blockput(b);
237 return ext2datablock(fs, bno, size);
240 fprint(2, "ext2fileblock %ud: too big\n", obno);
241 return nil;
244 static int
245 checksuper(Super *super)
247 if(super->magic != SUPERMAGIC){
248 werrstr("bad magic 0x%ux wanted 0x%ux", super->magic, SUPERMAGIC);
249 return -1;
251 return 0;
254 static int
255 ext2sync(Fsys *fsys)
257 int i;
258 Group g;
259 Block *b;
260 Super super;
261 Ext2 *fs;
262 Disk *disk;
264 fs = fsys->priv;
265 disk = fs->disk;
266 if((b = diskread(disk, SBSIZE, SBOFF)) == nil)
267 return -1;
268 parsesuper(&super, b->data);
269 blockput(b);
270 if(checksuper(&super) < 0)
271 return -1;
272 fs->blocksize = MINBLOCKSIZE<<super.logblocksize;
273 fs->nblock = super.nblock;
274 fs->ngroup = (super.nblock+super.blockspergroup-1)
275 / super.blockspergroup;
276 fs->inospergroup = super.inospergroup;
277 fs->blockspergroup = super.blockspergroup;
278 fs->inosperblock = fs->blocksize / InodeSize;
279 if(fs->blocksize == SBOFF)
280 fs->groupaddr = 2;
281 else
282 fs->groupaddr = 1;
283 fs->descperblock = fs->blocksize / GroupSize;
284 fs->firstblock = super.firstdatablock;
286 fsys->blocksize = fs->blocksize;
287 fsys->nblock = fs->nblock;
288 if(debug) fprint(2, "ext2 %d %d-byte blocks, first data block %d, %d groups of %d\n",
289 fs->nblock, fs->blocksize, fs->firstblock, fs->ngroup, fs->blockspergroup);
291 if(0){
292 for(i=0; i<fs->ngroup; i++)
293 if(ext2group(fs, i, &g) >= 0)
294 fprint(2, "grp %d: bitblock=%d\n", i, g.bitblock);
296 return 0;
299 static void
300 mkhandle(Nfs3Handle *h, u64int ino)
302 h->h[0] = ino>>24;
303 h->h[1] = ino>>16;
304 h->h[2] = ino>>8;
305 h->h[3] = ino;
306 h->len = 4;
309 static u32int
310 byte2u32(uchar *p)
312 return (p[0]<<24) | (p[1]<<16) | (p[2]<<8) | p[3];
315 static Nfs3Status
316 handle2ino(Ext2 *fs, Nfs3Handle *h, u32int *pinum, Inode *ino)
318 int i;
319 uint ioff;
320 u32int inum;
321 u32int addr;
322 Block *b;
323 Group g;
325 if(h->len != 4)
326 return Nfs3ErrBadHandle;
327 inum = byte2u32(h->h);
328 if(pinum)
329 *pinum = inum;
330 i = (inum-1) / fs->inospergroup;
331 if(i >= fs->ngroup)
332 return Nfs3ErrBadHandle;
333 ioff = (inum-1) % fs->inospergroup;
334 if(ext2group(fs, i, &g) < 0)
335 return Nfs3ErrIo;
336 addr = g.inodeaddr + ioff/fs->inosperblock;
337 if((b = diskread(fs->disk, fs->blocksize, (u64int)addr*fs->blocksize)) == nil)
338 return Nfs3ErrIo;
339 parseinode(ino, b->data+InodeSize*(ioff%fs->inosperblock));
340 blockput(b);
341 return Nfs3Ok;
344 static Nfs3Status
345 ext2root(Fsys *fsys, Nfs3Handle *h)
347 USED(fsys);
348 mkhandle(h, ROOTINODE);
349 return Nfs3Ok;
352 static u64int
353 inosize(Inode* ino)
355 u64int size;
357 size = ino->size;
358 if((ino->mode&IFMT)==IFREG)
359 size |= (u64int)ino->diracl << 32;
360 return size;
363 static Nfs3Status
364 ino2attr(Ext2 *fs, Inode *ino, u32int inum, Nfs3Attr *attr)
366 u32int rdev;
368 attr->type = -1;
369 switch(ino->mode&IFMT){
370 case IFIFO:
371 attr->type = Nfs3FileFifo;
372 break;
373 case IFCHR:
374 attr->type = Nfs3FileChar;
375 break;
376 case IFDIR:
377 attr->type = Nfs3FileDir;
378 break;
379 case IFBLK:
380 attr->type = Nfs3FileBlock;
381 break;
382 case IFREG:
383 attr->type = Nfs3FileReg;
384 break;
385 case IFLNK:
386 attr->type = Nfs3FileSymlink;
387 break;
388 case IFSOCK:
389 attr->type = Nfs3FileSocket;
390 break;
391 case IFWHT:
392 default:
393 return Nfs3ErrBadHandle;
396 attr->mode = ino->mode&07777;
397 attr->nlink = ino->nlink;
398 attr->uid = ino->uid;
399 attr->gid = ino->gid;
400 attr->size = inosize(ino);
401 attr->used = (u64int)ino->nblock*fs->blocksize;
402 if(attr->type==Nfs3FileBlock || attr->type==Nfs3FileChar){
403 rdev = ino->block[0];
404 attr->major = (rdev>>8)&0xFF;
405 attr->minor = rdev & 0xFFFF00FF;
406 }else{
407 attr->major = 0;
408 attr->minor = 0;
410 attr->fsid = 0;
411 attr->fileid = inum;
412 attr->atime.sec = ino->atime;
413 attr->atime.nsec = 0;
414 attr->mtime.sec = ino->mtime;
415 attr->mtime.nsec = 0;
416 attr->ctime.sec = ino->ctime;
417 attr->ctime.nsec = 0;
418 return Nfs3Ok;
421 static int
422 ingroup(SunAuthUnix *au, uint gid)
424 int i;
426 for(i=0; i<au->ng; i++)
427 if(au->g[i] == gid)
428 return 1;
429 return 0;
432 static Nfs3Status
433 inoperm(Inode *ino, SunAuthUnix *au, int need)
435 int have;
437 if(allowall)
438 return Nfs3Ok;
440 have = ino->mode&0777;
441 if(ino->uid == au->uid)
442 have >>= 6;
443 else if(ino->gid == au->gid || ingroup(au, ino->gid))
444 have >>= 3;
446 if((have&need) != need)
447 return Nfs3ErrNotOwner; /* really EPERM */
448 return Nfs3Ok;
451 static Nfs3Status
452 ext2getattr(Fsys *fsys, SunAuthUnix *au, Nfs3Handle *h, Nfs3Attr *attr)
454 Inode ino;
455 u32int inum;
456 Ext2 *fs;
457 Nfs3Status ok;
459 fs = fsys->priv;
460 if((ok = handle2ino(fs, h, &inum, &ino)) != Nfs3Ok)
461 return ok;
463 USED(au); /* anyone can getattr */
464 return ino2attr(fs, &ino, inum, attr);
467 static Nfs3Status
468 ext2access(Fsys *fsys, SunAuthUnix *au, Nfs3Handle *h, u32int want, u32int *got, Nfs3Attr *attr)
470 int have;
471 Inode ino;
472 u32int inum;
473 Ext2 *fs;
474 Nfs3Status ok;
476 fs = fsys->priv;
477 if((ok = handle2ino(fs, h, &inum, &ino)) != Nfs3Ok)
478 return ok;
480 have = ino.mode&0777;
481 if(ino.uid == au->uid)
482 have >>= 6;
483 else if(ino.gid == au->gid || ingroup(au, ino.gid))
484 have >>= 3;
486 *got = 0;
487 if((want&Nfs3AccessRead) && (have&AREAD))
488 *got |= Nfs3AccessRead;
489 if((want&Nfs3AccessLookup) && (ino.mode&IFMT)==IFDIR && (have&AEXEC))
490 *got |= Nfs3AccessLookup;
491 if((want&Nfs3AccessExecute) && (ino.mode&IFMT)!=IFDIR && (have&AEXEC))
492 *got |= Nfs3AccessExecute;
494 return ino2attr(fs, &ino, inum, attr);
497 static Nfs3Status
498 ext2lookup(Fsys *fsys, SunAuthUnix *au, Nfs3Handle *h, char *name, Nfs3Handle *nh)
500 u32int nblock;
501 u32int i;
502 uchar *p, *ep;
503 Dirent de;
504 Inode ino;
505 Block *b;
506 Ext2 *fs;
507 Nfs3Status ok;
508 int len, want;
510 fs = fsys->priv;
511 if((ok = handle2ino(fs, h, nil, &ino)) != Nfs3Ok)
512 return ok;
514 if((ino.mode&IFMT) != IFDIR)
515 return Nfs3ErrNotDir;
517 if((ok = inoperm(&ino, au, AEXEC)) != Nfs3Ok)
518 return ok;
520 len = strlen(name);
521 nblock = (ino.size+fs->blocksize-1) / fs->blocksize;
522 if(debug) fprint(2, "%d blocks in dir...", nblock);
523 for(i=0; i<nblock; i++){
524 if(i==nblock-1)
525 want = ino.size % fs->blocksize;
526 else
527 want = fs->blocksize;
528 b = ext2fileblock(fs, &ino, i, want);
529 if(b == nil){
530 if(debug) fprint(2, "empty block...");
531 continue;
533 p = b->data;
534 ep = p+b->len;
535 while(p < ep){
536 parsedirent(&de, p);
537 if(de.reclen == 0){
538 if(debug)
539 fprint(2, "reclen 0 at offset %d of %d\n", (int)(p-b->data), b->len);
540 break;
542 p += de.reclen;
543 if(p > ep){
544 if(debug)
545 fprint(2, "bad len %d at offset %d of %d\n", de.reclen, (int)(p-b->data), b->len);
546 break;
548 if(de.ino == 0)
549 continue;
550 if(4+2+2+de.namlen > de.reclen){
551 if(debug)
552 fprint(2, "bad namelen %d at offset %d of %d\n", de.namlen, (int)(p-b->data), b->len);
553 break;
555 if(de.namlen == len && memcmp(de.name, name, len) == 0){
556 mkhandle(nh, de.ino);
557 blockput(b);
558 return Nfs3Ok;
561 blockput(b);
563 return Nfs3ErrNoEnt;
566 static Nfs3Status
567 ext2readdir(Fsys *fsys, SunAuthUnix *au, Nfs3Handle *h, u32int count, u64int cookie, uchar **pdata, u32int *pcount, u1int *peof)
569 u32int nblock;
570 u32int i;
571 int off, outofspace;
572 uchar *data, *dp, *dep, *p, *ep, *ndp;
573 Dirent de;
574 Inode ino;
575 Block *b;
576 Ext2 *fs;
577 Nfs3Status ok;
578 Nfs3Entry e;
579 int want;
581 fs = fsys->priv;
582 if((ok = handle2ino(fs, h, nil, &ino)) != Nfs3Ok)
583 return ok;
585 if((ino.mode&IFMT) != IFDIR)
586 return Nfs3ErrNotDir;
588 if((ok = inoperm(&ino, au, AREAD)) != Nfs3Ok)
589 return ok;
591 if(debug) print("readdir cookie %#llux ino.size %#llux\n",
592 (u64int)cookie, (u64int)ino.size);
594 if(cookie >= ino.size){
595 *peof = 1;
596 *pcount = 0;
597 *pdata = 0;
598 return Nfs3Ok;
601 dp = malloc(count);
602 data = dp;
603 if(dp == nil)
604 return Nfs3ErrNoMem;
605 dep = dp+count;
606 *peof = 0;
607 nblock = (ino.size+fs->blocksize-1) / fs->blocksize;
608 i = cookie/fs->blocksize;
609 off = cookie%fs->blocksize;
610 outofspace = 0;
611 for(; i<nblock && !outofspace; i++, off=0){
612 if(i==nblock-1)
613 want = ino.size % fs->blocksize;
614 else
615 want = fs->blocksize;
616 b = ext2fileblock(fs, &ino, i, want);
617 if(b == nil)
618 continue;
619 p = b->data;
620 ep = p+b->len;
621 memset(&e, 0, sizeof e);
622 while(p < ep){
623 parsedirent(&de, p);
624 if(de.reclen == 0){
625 if(debug) fprint(2, "reclen 0 at offset %d of %d\n", (int)(p-b->data), b->len);
626 break;
628 p += de.reclen;
629 if(p > ep){
630 if(debug) fprint(2, "reclen %d at offset %d of %d\n", de.reclen, (int)(p-b->data), b->len);
631 break;
633 if(de.ino == 0){
634 if(debug) fprint(2, "zero inode\n");
635 continue;
637 if(4+2+2+de.namlen > de.reclen){
638 if(debug) fprint(2, "bad namlen %d reclen %d at offset %d of %d\n", de.namlen, de.reclen, (int)(p-b->data), b->len);
639 break;
641 if(debug) print("%.*s/%d ", de.namlen, de.name, (int)de.ino);
642 if(p-de.reclen - b->data < off)
643 continue;
644 e.fileid = de.ino;
645 e.name = de.name;
646 e.namelen = de.namlen;
647 e.cookie = (u64int)i*fs->blocksize + (p - b->data);
648 if(debug) print("%.*s %#llux\n", utfnlen(e.name, e.namelen), e.name, (u64int)e.cookie);
649 if(nfs3entrypack(dp, dep, &ndp, &e) < 0){
650 outofspace = 1;
651 break;
653 dp = ndp;
655 blockput(b);
657 if(i==nblock && !outofspace)
658 *peof = 1;
660 *pcount = dp - data;
661 *pdata = data;
662 return Nfs3Ok;
665 static Nfs3Status
666 ext2readfile(Fsys *fsys, SunAuthUnix *au, Nfs3Handle *h, u32int count,
667 u64int offset, uchar **pdata, u32int *pcount, u1int *peof)
669 uchar *data;
670 Block *b;
671 Ext2 *fs;
672 int skip1, tot, want, fragcount;
673 Inode ino;
674 Nfs3Status ok;
675 u64int size;
677 fs = fsys->priv;
678 if((ok = handle2ino(fs, h, nil, &ino)) != Nfs3Ok)
679 return ok;
681 if((ok = inoperm(&ino, au, AREAD)) != Nfs3Ok)
682 return ok;
684 size = inosize(&ino);
685 if(offset >= size){
686 *pdata = 0;
687 *pcount = 0;
688 *peof = 1;
689 return Nfs3Ok;
691 if(offset+count > size)
692 count = size-offset;
694 data = malloc(count);
695 if(data == nil)
696 return Nfs3ErrNoMem;
697 memset(data, 0, count);
699 skip1 = offset%fs->blocksize;
700 offset -= skip1;
701 want = skip1+count;
703 /*
704 * have to read multiple blocks if we get asked for a big read.
705 * Linux NFS client assumes that if you ask for 8k and only get 4k
706 * back, the remaining 4k is zeros.
707 */
708 for(tot=0; tot<want; tot+=fragcount){
709 b = ext2fileblock(fs, &ino, (offset+tot)/fs->blocksize, fs->blocksize);
710 fragcount = fs->blocksize;
711 if(b == nil)
712 continue;
713 if(tot+fragcount > want)
714 fragcount = want - tot;
715 if(tot == 0)
716 memmove(data, b->data+skip1, fragcount-skip1);
717 else
718 memmove(data+tot-skip1, b->data, fragcount);
719 blockput(b);
721 count = tot - skip1;
723 *peof = (offset+count == size);
724 *pcount = count;
725 *pdata = data;
726 return Nfs3Ok;
729 static Nfs3Status
730 ext2readlink(Fsys *fsys, SunAuthUnix *au, Nfs3Handle *h, char **link)
732 Ext2 *fs;
733 Nfs3Status ok;
734 int len;
735 Inode ino;
736 Block *b;
738 fs = fsys->priv;
739 if((ok = handle2ino(fs, h, nil, &ino)) != Nfs3Ok)
740 return ok;
741 if((ok = inoperm(&ino, au, AREAD)) != Nfs3Ok)
742 return ok;
744 if(ino.size > 1024)
745 return Nfs3ErrIo;
746 len = ino.size;
748 if(ino.nblock != 0){
749 /* BUG: assumes symlink fits in one block */
750 b = ext2fileblock(fs, &ino, 0, len);
751 if(b == nil)
752 return Nfs3ErrIo;
753 if(memchr(b->data, 0, len) != nil){
754 blockput(b);
755 return Nfs3ErrIo;
757 *link = malloc(len+1);
758 if(*link == 0){
759 blockput(b);
760 return Nfs3ErrNoMem;
762 memmove(*link, b->data, len);
763 (*link)[len] = 0;
764 blockput(b);
765 return Nfs3Ok;
768 if(len > sizeof ino.block)
769 return Nfs3ErrIo;
771 *link = malloc(len+1);
772 if(*link == 0)
773 return Nfs3ErrNoMem;
774 memmove(*link, ino.block, ino.size);
775 (*link)[len] = 0;
776 return Nfs3Ok;
779 /*
780 * Ext2 is always little-endian, even on big-endian machines.
781 */
783 static u32int
784 l32(uchar *p)
786 return p[0] | (p[1]<<8) | (p[2]<<16) | (p[3]<<24);
789 static u16int
790 l16(uchar *p)
792 return p[0] | (p[1]<<8);
795 static u8int
796 l8(uchar *p)
798 return p[0];
801 static void
802 parsedirent(Dirent *de, uchar *p)
804 de->ino = l32(p);
805 de->reclen = l16(p+4);
806 de->namlen = l8(p+6);
807 /* 1 byte pad */
808 de->name = (char*)p+8;
811 static void
812 parseinode(Inode *ino, uchar *p)
814 int i;
816 ino->mode = l16(p);
817 ino->uid = l16(p+2);
818 ino->size = l32(p+4);
819 ino->atime = l32(p+8);
820 ino->ctime = l32(p+12);
821 ino->mtime = l32(p+16);
822 ino->dtime = l32(p+20);
823 ino->gid = l16(p+24);
824 ino->nlink = l16(p+26);
825 ino->nblock = l32(p+28);
826 ino->flags = l32(p+32);
827 /* 4 byte osd1 */
828 for(i=0; i<NBLOCKS; i++)
829 ino->block[i] = l32(p+40+i*4);
830 ino->version = l32(p+100);
831 ino->fileacl = l32(p+104);
832 ino->diracl = l32(p+108);
833 ino->faddr = l32(p+112);
834 /* 12 byte osd2 */
837 static void
838 parsegroup(Group *g, uchar *p)
840 g->bitblock = l32(p);
841 g->inodebitblock = l32(p+4);
842 g->inodeaddr = l32(p+8);
843 g->freeblockscount = l16(p+12);
844 g->freeinodescount = l16(p+14);
845 g->useddirscount = l16(p+16);
846 /* 2 byte pad */
847 /* 12 byte reserved */
850 static void
851 parsesuper(Super *s, uchar *p)
853 s->ninode = l32(p);
854 s->nblock = l32(p+4);
855 s->rblockcount = l32(p+8);
856 s->freeblockcount = l32(p+12);
857 s->freeinodecount = l32(p+16);
858 s->firstdatablock = l32(p+20);
859 s->logblocksize = l32(p+24);
860 s->logfragsize = l32(p+28);
861 s->blockspergroup = l32(p+32);
862 s->fragpergroup = l32(p+36);
863 s->inospergroup = l32(p+40);
864 s->mtime = l32(p+44);
865 s->wtime = l32(p+48);
866 s->mntcount = l16(p+52);
867 s->maxmntcount = l16(p+54);
868 s->magic = l16(p+56);
869 s->state = l16(p+58);
870 s->errors = l16(p+60);
871 /* 2 byte pad */
872 s->lastcheck = l32(p+64);
873 s->checkinterval = l32(p+68);
874 s->creatoros = l32(p+72);
875 s->revlevel = l32(p+76);
876 s->defresuid = l16(p+80);
877 s->defresgid = l16(p+82);
878 /* 940 byte reserved */