code: mafs

ref: b3ccded71dc0330ba63fdb7d7a0e6fc6ded1657e
dir: /all.h/

View raw version
#include <u.h>
#include <libc.h>

#include <fcall.h>
#include <auth.h>
#include <authsrv.h>
#include <thread.h>
#include <9p.h>

#include "dat.h"
#include "extents.h"

enum
{
	/*
	https://cs.stackexchange.com/questions/11029/why-is-it-best-to-use-a-prime-number-as-a-mod-in-a-hashing-function
		using small numbers for testing

	Size of buffer cache is approximately (Nbuckets * Ncollisions * Rawblocksize).

	If you have RAM to spare, increase the Nbuckets instead of
	Ncollisions as the hash index lookup is faster than searching
	through a linked list.
	 */
	Ncollisions = 3,	/* soft limit after which we start reusing older Iobuf's */
};

typedef	struct	Hiob	Hiob;
typedef	struct	Iobuf	Iobuf;

struct Hiob		/* Hash bucket */
{
	Iobuf*	link;	/* least recently used of the circular list */
	QLock;		/* controls access to this hash bucket */
};

/*
	using Ref to avoid locking up the hash bucket when a process
	is waiting for a lock on an Iobuf in that hash bucket.

	The Ref ensures that an Iobuf is not stolen before another process
	can get to wlock()'ing it after letting go of the lock on the hash bucket.
	We cannot hold the lock on the hash bucket until we wlock() the iobuf
	as that blocks other processes from using the hash bucket. This could
	also result in a deadlock. For example, the directory entry is block 18,
	which hashes to a hash index of 7. A writer() locked the directory entry
	iobuf and wants to add a data block 84 to the directory entry. Block 84
	hashes to the same hash index of 7. Another process wanting to access the
	directory entry is waiting for a lock on that io buffer. While doing so,
	it has locked the hash bucket. Now, this has caused a deadlock between
	both these processes. The first process cannot proceed until it can
	lock the hash bucket holding block 84 and is still holding the lock
	on the directory entry in block 18. The second process cannot lock
	block 18 and is holding the lock on the hash bucket.

	for locking a buffer:
		qlock(hash bucket); incref(buffer); qunlock(hash bucket);
			wlock(buffer); decref(buffer);

	for stealing an unused buffer:
		qlock(hash bucket);
		find a buffer with ref == 0 and wlock()'able.
		qunlock(hash bucket);

	for unlocking a buffer:
		wunlock(buffer);
 */
struct Iobuf
{
	Ref;
	RWLock;			/* controls access to this Iobuf */
	u64	blkno;		/* block number on the disk, primary key */
	Iobuf	*fore;	/* for lru */
	Iobuf	*back;	/* for lru */
	union{
		u8	*xiobuf;	/* "real" buffer pointer */
		Content *io;	/* cast'able to contents */
	};

	/*
		Using Ref to avoid using a wlock() in dowrite.
		wlock() in dowrite() causes a deadlock with putwrite()

		dirties is decremented without a wlock() on the buffer in dowrite().
		Using a wlock() in dowrite() deadlocks with putwrite().
		getbuf() guarantees that even a free'ed block cannot be
		stolen until the dirties == 0. This avoids dirty blocks
		being stolen by other block numbers.
		incref(dirties) only happens with a wlock() in putwrite().
	 */
	Ref	dirties;	/* number of versions of this block yet to be written by the writer */
};

extern	u64	nbuckets;		/* n hash buckets for i/o */
extern	Extents frees;		/* extents of free blocks on the disk */

#include "fns.h"

/* Iobuf routines - contents of the blocks in memory */
void	initmemunitpool(u64 nunits);
u8 *allocmemunit(void);
void	freememunit(u8 *m);
int		checktag(Iobuf *p, u16 tag, u64 qpath);
Iobuf*	getbuf(u64 blkno, u8 readonly, u8 freshalloc);
Iobuf*	getbufchk(u64 blkno, u8 readonly, int tag, u64 qpath);
void	iobufinit(void);
void	putbuf(Iobuf *p);
void	putbuffree(Iobuf *p);
void	settag(Iobuf *p, int tag, u64 qpath);
void	showbuf(Iobuf *p);

/* writer functions */
void	initwriter(void);
void	putwrite(Iobuf *b);
void	stopwriter(void);
u64		pendingwrites(void);

/* routines to manipulate the contents */
Iobuf*	allocblock(int tag, u64 qpath);
void	freeblockbuf(Iobuf *buf);
void	freeblock(u64 blkno, u16 tag, u64 qpath);
void	fsok(int ok);
void	init(int doream, u64 size);
u64		newqpath();
u64		nperiblock(u16 tag);
void	ream(u64 size);
u64	rel2abs(Dentry *d, u64 reli);
void	rmfile(u64 qpath, u64 dblkno);
void	rmdirectory(u64 qpath, u64 dblkno);
void	rootream(void);
Dentry	*searchdir(u64 dblkno, u64 qpath, u16 uid, char *searchname, u64 searchidx, Iobuf **dbuf, Iobuf **buf);
void	shutdown(void);
void	start9p(char **nets, int stdio);
void	superream(u64 size, u64 nblocks);
Tlock*	tlocked(Iobuf*, Dentry*);

/* dentry routines */
u64 	addrelative(Dentry *d, u64 dblkno, u64 reli, u64 blkno);
Iobuf*	getdatablkat(Dentry *d, u64 reli, int flags);
void	loadfrees(u64 dblkno);
s32		readfile(u64 dblkno, u64 qpath, char *rbuf, s32 rbufsize, u64 offset);
s32		readfilesize(u64 dblkno, u64 qpath);
void	savefrees(u64 dblkno);
void	truncatefile(u64 qpath, u64 dblkno, s16 uid);
s32		writefile(u64 dblkno, u64 qpath, s16 uid, char *wbuf, s32 wbufsize, u64 offset);

/* user access routines */
int	byname(void*, void*);
int	byuid(void*, void*);
int canaccess(s16 uid, Dentry *d, u32 mode);
int	checkname(char*);
int	checkname9p2(char*);
void	chkqunlock(QLock *q);
void	chkrunlock(RWLock *q);
u8	chkwunlock(RWLock *q);
void 	cmd_user(void);
char*	cname(char*);
void	consproc(void);
void	cprint(char*, ...);
void	ctlread(Req *req);
void	ctlwrite(Req *req);
void	datestr(char*, long);
Qid	dentryqid(Dentry*);
void	dofilter(Filter*);
int		dprint(char *fmt, ...);
void	exit(void);
/*Float	famd(Float, int, int, int);*/
int	fchar(void);
/*ulong	fdf(Float, int);*/
void	sublockinit(void);
int	fname(char*);
void	formatinit(void);
void	freeaux(Aux *a);
void   freesearchstate(Iobuf **dbuf, Iobuf **buf);
Dentry*	getdir(Iobuf*, int);
long	getraddr(Device);
Wpath*	getwp(Wpath*);
void	hexdump(void*, int);
int	iaccess(short uid, Dentry *d, int m);
int	ingroup(s16 uid, s16 gid, int locked);
void	initconfig(u64 bno);
int	leadgroup(s16 uid, s16 gid);
s16	lookupid(char *name, int locked);
u64	min(u64 a, u64 b);
int	mkqidcmp(Qid*, Dentry*);
void	mkqid9p1(Qid9p1*, Qid*);
void	mkqid9p2(Qid*, Qid9p1*, int);
int	netserve(char*);
Aux	*newaux(u64 addr, u16 uid);
u64	newqpath(void);
void	newstart(void);
u64	nperindunit(u16 tag);
int	oconvD2M(Dentry*, void*);
int	oconvM2D(void*, Dentry*);
int	ofcallfmt(Fmt*);
u64	power( u64 base, int n);
int	prime(long);
void	putsuper(int locked);
void	strrand(void*, int);
int	strtouid(char*);
int	strtouid1(char*);
void	sync(void);
int	syncblock(void);
int	Tfmt(Fmt*);
char	*username(s16 uid, int locked, char *username);
void	usersinit(void);
void	writeconfig(u64 bno);

#pragma varargck	argpos	cprint	1
#pragma varargck	argpos	panic	1

#pragma varargck	type 	"A"	Filta
#pragma varargck	type	"G"	int
#pragma varargck	type	"T"	long
#pragma varargck	type	"F"	Fcall*

long	belong(char *);
int 	cmd_exec(char*);
void	consserve(void);
void	confinit(void);
int	fsinit(int, int);
int	nextelem(void);
long	number(int, int);
int	skipbl(int);