code: mafs

ref: fb53d7e68bcb2108fb304c81dccb1d7ff50ffa1f
dir: /dev.c/

View raw version
#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;
}

s32
devread(u64 blkno, void *b, u16 len)
{
	s32 n;

	if((n= pread(devfd, b, len*Blocksize, blkno*Blocksize)) != len*Blocksize)
		panic("devread failed: %r\n");
	return n;
}

s32
devwrite(u64 blkno, void *b, u16 len)
{
	s32 wn;

	if((wn = pwrite(devfd, b, len*Blocksize, blkno*Blocksize)) != len*Blocksize)
		panic("devwrite failed: %r\n");
	return wn;
}

u64
devinit(char *devfile)
{
	devfd = open(devfile, ORDWR);
	if(devfd < 0)
		panic("can't open %s", devfile);
	seek(devfd, 0, 0);
	return devsize();
}