shithub: neoventi

ref: 4b60d3dd32efd00a1d07cb08662926ba9836719f
dir: /neoventi.h/

View raw version
#define	U8GET(p)	((p)[0])
#define	U16GET(p)	(((p)[0]<<8)|(p)[1])
#define	U32GET(p)	((u32int)(((p)[0]<<24)|((p)[1]<<16)|((p)[2]<<8)|(p)[3]))
#define	U64GET(p)	(((u64int)U32GET(p)<<32)|(u64int)U32GET((p)+4))
#define	U16PUT(p,v)	(p)[0]=(v)>>8;(p)[1]=(v)

enum {
	VtRerror = 1,
	VtTping,
	VtRping,
	VtThello,
	VtRhello,
	VtTgoodbye,
	VtRgoodbye,
	VtTauth0,
	VtRauth0,
	VtTauth1,
	VtRauth1,
	VtTread,
	VtRread,
	VtTwrite,
	VtRwrite,
	VtTsync,
	VtRsync,
	VtTmax,
};

enum {
	/* blank space at beginning of partition - useful for config
	 * or for when you accidentally {mount /dev/.../arenas /mnt/venti} */
	PartBlank = 256 * 1024,
	NameSize = 64,
	/* FIXME(design break) make arena part head a full block? breaking change for new arena partitions, removes
	 * need for special casing and magic around it. Just read the damn block. */
	HeadSize = 512,
	ArenaPartMagic = 0xa9e4a5e7U,
	ISectMagic = 0xd15c5ec7U,
	IBucketSize		= 6,
	IEntrySize		= 38,
	MaxAMap = 31*1024,
	ClumpInfoSize = 25,
	ClumpSize = ClumpInfoSize + 13,
	ABlockLog = 9, /* All reads are of 512 byte sectors??? Yikes. We should probably use a larger size, FIXME. */
};

typedef struct {
	int fd;
	jmp_buf bounce;
} VtConn;

typedef struct {
	char name[NameSize];
	u32int clumpmagic;
	u32int clumpmax;
	u32int blocksize;
	u32int version;
	u32int ctime, wtime;
	// used for caching. only 64K arenas may reside in the cache at a time.
	u16int index;

	/* disk info */
	u64int size;
	u64int base;
	int fd;
	struct {
		u32int clumps;
	} memstats;
} VtArena;

typedef struct {
	VtArena *s_arena; // shared reference
	u64int offset;
	u16int size;
	u8int blocks;
} VtAddress;

typedef struct {
	int		blocklog;		/* log2(blocksize) */
	int		buckmax;		/* max. entries in a index bucket */
	u32int		tabbase;		/* base address of index config table on disk */
	u32int		tabsize;		/* max. bytes in index config */

	u32int		version;
	u32int		bucketmagic;
	char		name[NameSize];	/* text label */
	char		index[NameSize];	/* index owning the section */
	u32int		blocksize;		/* size of hash buckets in index */
	u32int		blockbase;		/* address of start of on disk index table */
	u32int		blocks;			/* total blocks on disk; some may be unused */
	u32int		start;			/* first bucket in this section */
	u32int		stop;			/* limit of buckets in this section */
	int fd;

	/* used for the cache key */
	u16int cacheindex;
} VtISect;

typedef struct {
	char name[NameSize];
	u64int start, stop;
	VtArena *arena;
} MapEntry;

enum
{
	WhackStats	= 8,
	WhackMaxOff	= 16*1024,	/* max allowed offset */

	HashLog		= 14,
	HashSize	= 1<<HashLog,
	HashMask	= HashSize - 1,

	MinMatch	= 3,		/* shortest match possible */

	MinDecode	= 8,			/* minimum bits to decode a match or lit; >= 8 */

	MaxSeqMask	= 8,	/* number of bits in coding block mask */
	MaxSeqStart	= 256,	/* max offset of initial coding block */

	MaxPacketSize = 0x10008, /* largest possible protocol message */
}; 

int	unwhack(uchar *dst, u16int dsize, uchar *src, u16int ssize);
void serve(char *addr);
/* Looks up the address of a score on disk using the index */
int vtreadlookup(u8int *score, VtAddress *addr);
u16int vtreadarena(VtArena *arena, u64int addr, uchar *dbuf, u16int reqsize);
int readclump(uchar *dst, VtAddress addr);
int Brdu32(Biobufhdr *bio, u32int *u32);
int stru32int(char *s, u32int *r);
int stru64int(char *s, u64int *r);
int u64log2(u64int v);

void initarenas(void);
void initindex(void);
void cacheinit(void);
int cachelookup(char **buf, u16int arenaindex, u32int blockindex);
void cacheunlock(u16int arenaindex, u32int blockindex);

extern char *arenapath;
extern char *isectpath;