ref: 5c21eca829380c28769f79beabb7cc51f1787f97
dir: /dev.c/
#include <u.h> #include <libc.h> #include "dat.h" #include "fns.h" int devfd = -1; /* device fd */ u64 devsize(void) { Dir *dir; u64 size; if((dir = dirfstat(devfd)) == nil) panic("can't stat device\n"); size = dir->length; free(dir); return size; } /* TODO proper error handling */ int devread(u64 blkno, void *b, u16 len) { int n; if((n= pread(devfd, b, len*Rawblocksize, blkno*Rawblocksize)) != len*Rawblocksize) panic("devread failed: %r\n"); /* print("devread i %d n %d\n", i, n); */ return n; } int devwritedirty(u64 blkno, u8 dirty) { int n; if((n = pwrite(devfd, &dirty, 1, blkno*Rawblocksize +Nthdirty)) != 1) panic("devwrite failed: %r\n"); return n; } int devwritedirtyclear(u64 blkno) { return devwritedirty(blkno, 0); } /* obsolete int devwritedirtyset(u64 blkno) { return devwritedirty(blkno, 1); } */ int devwrite(u64 blkno, void *b, u16 len) { int n; if((n = pwrite(devfd, b, len*Rawblocksize, blkno*Rawblocksize)) != len*Rawblocksize) panic("devwrite failed: %r\n"); return n; } /* obsoleted, a hack for more efficient logging */ /*int devrawwrite(void *b, int len, u64 offset) { int i, n; qlock(&d); i = (n = pwrite(devfd, b, len, offset)) != len; qunlock(&d); if(i) panic("devrawwrite failed: %r\n"); return n; }*/ u64 devinit(char *devfile) { devfd = open(devfile, ORDWR); if(devfd < 0) panic("can't open %s", devfile); seek(devfd, 0, 0); return devsize(); }