ref: eef0fe67ee4bb8c254f0f9e61c24ef5d34d05a4f
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) { int n; if((n= pread(devfd, b, Rawblocksize, blkno*Rawblocksize)) != 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) { int n; if((n = pwrite(devfd, b, Rawblocksize, blkno*Rawblocksize)) != 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(); }