ref: 5c21eca829380c28769f79beabb7cc51f1787f97
dir: /all.h/
#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" typedef struct Buffer Buffer; typedef struct Hiob Hiob; typedef struct Iobuf Iobuf; typedef struct Work Work; struct Work { void (*f)(Req *r); Req *r; }; struct Buffer { QLock lck; Work works[Nworks]; u16 hd, tl, nworks; Rendez isfull, isempty; }; struct Hiob /* Hash bucket */ { Iobuf* link; /* least recently used of the circular list */ QLock; }; /* 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; u64 blkno; /* block number on the disk, primary key */ u16 len; /* number of blocks of data xiobuf points to */ Iobuf *fore; /* for lru */ Iobuf *back; /* for lru */ union{ u8 *xiobuf; /* "real" buffer pointer */ Content *io; /* cast'able to contents */ }; int flags; }; extern u32 nbuckets; /* n hash buckets for i/o */ extern Extents frees; /* free extents */ #include "fns.h" /* Iobuf routines - contents of the blocks in memory */ int checktag(Iobuf *p, u16 tag, u64 qpath); Iobuf* getbuf(u64 blkno, u16 len, int flags); Iobuf* getbufchk(u64 blkno, u16 len, int flags, int tag, u64 qpath); void iobufinit(void); void putbuf(Iobuf *p); void settag(Iobuf *p, int tag, u64 qpath); void showbuf(Iobuf *p); /* routines to manipulate the contents */ Iobuf* allocblocks(u16 len, int flags, int tag, u64 qpath); void freeblockbuf(Iobuf *buf); void freeblocks(u64 blkno, u16 len, u16 tag, u64 qpath); void fsok(int ok); void init(int doream); u64 newqpath(); u64 nperiblock(u16 tag); void ream(u64 size); void rel2abs(Dentry *d, u64 reli, Spanid *s); 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, u16 len); Iobuf* getdataspanat(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); void 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);