/*
 * P_R_P_Q_# (C) COPYRIGHT IBM CORPORATION 1987, 1988
 * LICENSED MATERIALS - PROPERTY OF IBM
 * REFER TO COPYRIGHT INSTRUCTIONS FORM NUMBER G120-2083

 * AFS system call opcodes
 */

#ifndef	__AFS_CONFIG_INCL_ENV_
#define	__AFS_CONFIG_INCL_ENV_	1

#define	AFSOP_START_RXCALLBACK	  0	/* no aux parms */
#define	AFSOP_START_AFS		  1	/* no aux parms */
#define	AFSOP_START_BKG		  2	/* no aux parms */

#define	AFSOP_ADDCELL		  5	/* parm 2 = cell str */
#define	AFSOP_CACHEINIT		  6	/* parms 2-4 -> cache sizes */
#define	AFSOP_CACHEINFO		  7	/* the cacheinfo file */
#define	AFSOP_VOLUMEINFO	  8	/* the volumeinfo file */
#define	AFSOP_CACHEFILE		  9	/* a random cache file (V*) */
#define	AFSOP_CACHEINODE	 10	/* random cache file by inode */
#define	AFSOP_AFSLOG		 11	/* output log file */
#define	AFSOP_ROOTVOLUME	 12	/* non-standard root volume name */

#define	AFSOP_STARTLOG		 14	/* temporary: Start afs logging */
#define	AFSOP_ENDLOG		 15	/* temporary: End afs logging */
#define AFSOP_AFS_VFSMOUNT	 16     /* vfsmount cover for hpux */
#define AFSOP_ADVISEADDR	 17	/* to init rx cid generator */
#define AFSOP_CLOSEWAIT 	 18	/* make all closes synchronous */
#define	AFSOP_RXEVENT_DAEMON	 19	/* rxevent daemon */
#define	AFSOP_GETMTU		 20	/* stand-in for SIOCGIFMTU, for now */
#define	AFSOP_ADDCELL2		 29	/* 2nd cell interface, newer than 5 */
/* 30 is reserved by AFSCALL_ICL for SGI */

/* The range 20-30 is reserved for AFS system offsets in the afs_syscall */
#define	AFSCALL_PIOCTL		20
#define	AFSCALL_SETPAG		21
#define	AFSCALL_IOPEN		22
#define	AFSCALL_ICREATE		23
#define	AFSCALL_IREAD		24
#define	AFSCALL_IWRITE		25
#define	AFSCALL_IINC		26
#define	AFSCALL_IDEC		27
#define	AFSCALL_CALL		28

#define AFSCALL_ICL             30

#define	AFSOP_GO		100	/* whether settime is being done */
/* not for initialization: debugging assist */
#define	AFSOP_CHECKLOCKS	200	/* dump lock state */
#define	AFSOP_SHUTDOWN		201	/* Totally shutdown afs (deallocate all) */

/* The following aren't used by afs_initState but by afs_termState! */
#define	AFSOP_STOP_RXCALLBACK	210	/* Stop CALLBACK process */
#define	AFSOP_STOP_AFS		211	/* Stop AFS process */
#define	AFSOP_STOP_BKG		212	/* Stop BKG process */

/* Main afs syscall entry; this number may vary per system (i.e. defined in afs/param.h) */
#ifndef	AFS_SYSCALL
#define	AFS_SYSCALL		31
#endif

/* arguments passed by afsd */
struct afs_cacheParams {
    long cacheScaches;
    long cacheFiles;
    long cacheBlocks;
    long cacheDcaches;
    long cacheVolumes;
    long chunkSize;
    long setTimeFlag;
    long memCacheFlag;
    long inodes;
    long users;
};

/*
 * Macros to uniquely identify the AFS vfs struct
 */
#define	AFS_VFSMAGIC		0x1234
#if    defined(AFS_SUN_ENV) || defined(AFS_NEXT_ENV) || defined(AFS_HPUX90_ENV) || defined(AFS_LINUX_ENV)
#define	AFS_VFSFSID		99
#else
#if defined(AFS_SGI_ENV)
#define AFS_VFSFSID		afs_fstype
#else
#define	AFS_VFSFSID		AFS_MOUNT_AFS
#endif
#endif

/* The following are various levels of afs debugging */
#define	AFSDEB_GENERAL		1	/* Standard debugging */
#define	AFSDEB_NETWORK		2	/* low level afs networking */
#define	AFSDEB_RX		4	/* RX debugging */
#define	AFSDEB_VNLAYER		8	/* interface layer to AFS (aixops, gfsops, etc) */

/* generic undefined vice id */
#define	UNDEFVID	    (-1)

/* The basic defines for the Andrew file system
    better keep things powers of two so "& (foo-1)" hack works for masking bits */
#define	MAXHOSTS	13	    /* max hosts per single volume */
#define	OMAXHOSTS	 8	    /* backwards compatibility */
#define	NBRS		15	    /* max number of queued daemon requests */
#define	NUSERS		16	    /* hash table size for unixuser table */
#define	NSERVERS	16	    /* hash table size for server table */
#define	NVOLS		64	    /* hash table size for volume table */
#define	NFENTRIES	256	    /* hash table size for disk volume table */
#define	VCSIZE	       1024	    /* stat cache hash table size */
#define	DCSIZE		512	    /* disk cache hash table size */
#define	PIGGYSIZE	1350	    /* max piggyback size */
#define	MAXVOLS		128	    /* max vols we can store */
#define	MAXSYSNAME	128	    /* max sysname (i.e. @sys) size */
#define	NOTOKTIMEOUT	(2*3600)    /* time after which to timeout conns sans tokens */
#define	NOPAG		0xffffffff
#define AFS_NCBRS	300	    /* max # of call back return entries */
#define AFS_MAXCBRSCALL	16	    /* max to return in a given call */
#if	defined(AFS_AIX32_ENV) || defined(AFS_HPUX_ENV)  /* XXX Because of rxkad_cprivate... XXX */
#define	AFS_SMALLOCSIZ 	428	    /* "Small" allocated size */
#define	AFS_SALLOC_LOW_WATER	250 /* Min free blocks before allocating more */
#else
#define	AFS_SMALLOCSIZ 	256	    /* "Small" allocated size */
#endif
#ifdef	AFS_NEXT_ENV
#define	AFS_LRALLOCSIZ	8192
#else
#define	AFS_LRALLOCSIZ 	4096	    /* "Large" allocated size */
#endif
#define	VCACHE_FREE	5
#define	AFS_NRXPACKETS	80
#define	AFS_RXDEADTIME	50

/* flags to use with AFSOP_CACHEINIT */
#define AFSCALL_INIT_MEMCACHE        0x1         /* use a memory-based cache */

/* below here used only for kernel procedures */
#ifdef KERNEL
#define MAXSCACHE "@(#) MAXSCACHE!"
/* Store synchrony flags - SYNC means that data should be forced to server's
 * disk immediately upon completion. */
#define AFS_ASYNC 	0
#define AFS_SYNC  	1
#define AFS_DONTVMSYNC  2

/* background request structure */
#define	BPARMS		4

#define	BOP_NOOP	0	    /* leave 0 unused */
#define	BOP_FETCH	1	    /* parm1 is chunk to get */
#define	BOP_STORE	2	    /* parm1 is chunk to store */
#define	BOP_PATH	3	    /* parm1 is path, parm2 is chunk to fetch */

#define	B_DONTWAIT	1	    /* On failure return; don't wait */

/* protocol is: refCount is incremented by user to take block out of free pool.
    Next, BSTARTED is set when daemon finds request.  This prevents
    other daemons from picking up the same request.  Finally, when
    request is done, refCount is zeroed.  BDONE and BWAIT are used by
    dudes waiting for operation to proceed to a certain point before returning.
*/
#define	BSTARTED	1	    /* request picked up by a daemon */
#define	BUVALID		2	    /* code is valid (store) */
#define	BUWAIT		4	    /* someone is waiting for BUVALID */
struct brequest {
    struct vcache *vnode;	    /* vnode to use, with vrefcount bumped */
#ifdef	AFS_FINEGR_SUNLOCK
    kmutex_t lock;
    kcondvar_t cv;
#endif
    struct AFS_UCRED *cred;	    /* credentials to use for operation */
    long parm[BPARMS];		    /* random parameters */
    long code;			    /* return code */
    short refCount;		    /* use counter for this structure */
    char opcode;		    /* what to do (store, fetch, etc) */
    char flags;			    /* free, etc */
};

struct SecretToken {
    char data[56];
};

struct ClearToken {
	long AuthHandle;
	char HandShakeKey[8];
	long ViceId;
	long BeginTimestamp;
	long EndTimestamp;
};

struct VenusFid {
    long Cell;			    /* better sun packing if at end of structure */
    struct AFSFid Fid;
};

/* Temporary struct to be passed between afs_fid and afs_vget; in SunOS4.x we can only pass a maximum of 10 bytes for a handle (we ideally need 16!) */
struct SmallFid {
    long Volume;
    long CellAndUnique;
    u_short Vnode;
};

struct vrequest {
    long uid;			/* user id making the request */
    long busyCount;		/* how many busies we've seen so far */
    long flags;                 /* things like O_SYNC, O_NONBLOCK go here */
    char initd;			/* if non-zero, non-uid fields meaningful */
    char accessError;		/* flags for overriding error return code */
    char volumeError;		/* encountered a missing or busy volume */
    char networkError;		/* encountered network problems */
};
#define VOLMISSING 1
#define VOLBUSY 2

/* structure linked off of a server to keep track of queued returned
 * callbacks.  Sent asynchronously when we run a little low on free dudes.
 */
struct afs_cbr {
    struct afs_cbr *next;
    struct AFSFid fid;
};


#define	LOCALCELL	    1	/* local cell's number is always 1 */

/* cell flags */
#define	CPrimary	    1	    /* on if this is the primary cell */
#define	CNoSUID		    2	    /* 1 if no suid progs can run from this cell */
#define CHasVolRef	   16	    /* Volumes were referenced in this cell*/
#define CLinkedCell	   32

struct cell {
    struct cell *next;
#ifdef	AFS_FINEGR_SUNLOCK
    afs_rwlock_t lock;
#endif
    long cell;				    /* unique id assigned by venus */
    char *cellName;			    /* char string name of cell */
    struct server *cellHosts[MAXHOSTS];    /* volume *location* hosts for this cell */
    struct cell *lcellp;		    /* Associated linked cell */
    long fsport;			    /* file server port */
    long vlport;			    /* volume server port */
    short states;			    /* state flags */
    short cellIndex;			    /* relative index number per cell */
};

#ifdef	AFS_FINEGR_SUNLOCK
#define	afs_PutCell(cellp, locktype)	ReleaseLock(&(cellp)->lock, (locktype))
#else
#define	afs_PutCell(cellp, locktype)
#endif

/* the unixuser flag bit definitions */
#define	UHasTokens	1	    /* are the st and ct fields valid (ever set)? */
#define	UTokensBad	2	    /* are tokens bad? */
#define UPrimary        4           /* on iff primary identity */
#define UNeedsReset	8	    /* needs afs_ResetAccessCache call done */
#define UPAGCounted    16	    /* entry seen during PAG search (for stats) */

struct unixuser {
    struct unixuser *next;	    /* next hash pointer */
    long uid;			    /* search based on uid and cell */
    long cell;
    long vid;			    /* corresponding vice id in specified cell */
    short refCount;		    /* reference count for allocation */
    char states;		    /* flag info */
    long tokenTime;		    /* last time tokens were set, used for timing out conn data */
    long stLen;			    /* ticket length (if kerberos, includes kvno at head) */
    char *stp;			    /* pointer to ticket itself */
#ifdef	AFS_FINEGR_SUNLOCK
    afs_rwlock_t lock;
    kmutex_t mlock;
    kcondvar_t cv;
#endif
    struct ClearToken ct;
    struct afs_exporter	*exporter;  /* more info about the exporter for the remote user */
};

struct conn {
    /* Per-connection block. */
    struct conn	*next;		    /* Next dude same server. */
#ifdef	AFS_FINEGR_SUNLOCK
    afs_rwlock_t lock;	
#endif
    struct unixuser *user;	    /* user validated with respect to. */
    struct rx_connection *id;	    /* RPC connid. */
    struct server *server;	    /* server associated with this conn */
    short refCount;		    /* reference count for allocation */
    unsigned short port;	    /* port associated with this connection */
    char forceConnectFS;	    /* Should we try again with these tokens? */
};

/*
  * Queues implemented with both pointers and short offsets into a disk file.
  */
struct afs_q {
    struct afs_q *next;
    struct afs_q *prev;
};

#define SQNULL -1

/* Fid comparison routines */
#define	FidCmp(a,b) ((a)->Fid.Unique != (b)->Fid.Unique \
    || (a)->Fid.Vnode != (b)->Fid.Vnode \
    || (a)->Fid.Volume != (b)->Fid.Volume \
    || (a)->Cell != (b)->Cell)

/*
  * Operations on circular queues implemented with pointers.  Note: these queue
  * objects are always located at the beginning of the structures they are linking.
  */
#define	QInit(q)    ((q)->prev = (q)->next = (q))
#define	QAdd(q,e)   ((e)->next = (q)->next, (e)->prev = (q), \
			(q)->next->prev = (e), (q)->next = (e))
#define	QRemove(e)  ((e)->next->prev = (e)->prev, (e)->prev->next = (e)->next)
#define	QNext(e)    ((e)->next)
#define QPrev(e)    ((e)->prev)
#define QEmpty(q)   ((q)->prev == (q))
/* this one takes q1 and sticks it on the end of q2 - that is, the other end, not the end
 * that things are added onto.  q1 shouldn't be empty, it's silly */
#define QCat(q1,q2) ((q2)->prev->next = (q1)->next, (q1)->next->prev=(q2)->prev, (q1)->prev->next=(q2), (q2)->prev=(q1)->prev, (q1)->prev=(q1)->next=(q1))
/*
 * Do lots of address arithmetic to go from lruq to the base of the vcache
 * structure.  Don't move struct vnode, since we think of a struct vcache as
 * a specialization of a struct vnode
 */
#define	QTOV(e)	    ((struct vcache *)(((char *) (e)) - (((char *)(&(((struct vcache *)(e))->lruq))) - ((char *)(e)))))

#ifdef notdef
/*
 * Operations on circular dcache queues implemented with short file offsets.
 * Note: these queue objects are never located at the beginning of the structures
 * they are linking, so we always need to provide the queue's name in the dcache
 * record.
 */
#define	sQInit(q)		((q).next = (q).prev = SQNULL)
#define	sQAdd(q, qname, eidx)	(Fill this in)
#define	sQRemove(q, qname, eidx)	(Fill this in)
#define	sQNext(qname, eidx, nextidx)	(Fill this in)
#define	sQPrev(qname, eidx, previdx)	(Fill this in)
#endif /* notdef */

#define sum_preferences(s) (s->rank)

/*
 * Values used in the flags field of the server structure below.
 *
 *	AFS_SERVER_FLAG_ACTIVATED Has the server ever had a user connection
 *				  associated with it?
 */
#define AFS_SERVER_FLAG_ACTIVATED	0x1
#define AFS_DOES_VOL_CALLBACKS          0x2
#define	SNO_LHOSTS			0x4
#define	SYES_LHOSTS			0x8

struct server {
    struct server *next;	/* Ptr to next server record */
#ifdef	AFS_FINEGR_SUNLOCK
    afs_rwlock_t lock;	
#endif
    struct cell	*cell;		/* Cell in which this host resides */
    struct conn	*conns;		/* All user connections to this server */
    struct afs_cbr *cbrs;	/* Return list of callbacks */
    long host;			/* Host addr in network byte order */
    long portal;		/* Port in network byte order */
    unsigned int rank;		/* Server priority, used for distributing requests */
    long activationTime;	/* Time when this record was first activated */
    long lastDowntimeStart;	/* Time when last downtime incident began */
    long numDowntimeIncidents;	/* # (completed) downtime incidents */
    long sumOfDowntimes;	/* Total downtime experienced, in seconds */
    char isDown;		/* Is server considered down now? */
    char flags;			/* Misc flags*/
#ifdef	AFS_FINEGR_SUNLOCK
    kmutex_t lockcv;
    kcondvar_t cv;
#endif
};

#ifdef	AFS_FINEGR_SUNLOCK
#define	afs_PutServer(servp, locktype)	if (locktype) ReleaseLock(&(servp)->lock, (locktype))
#else
#define	afs_PutServer(servp, locktype)	
#endif

#ifndef AFS_LINUX_ENV
#define random rank
#endif

/* structs for some pioctls  - these are (or should be) 
 * also in venus.h
 */
struct spref {
	struct in_addr host;
	unsigned short rank;
};

struct sprefrequest {
	unsigned short offset;
	unsigned short num_servers;
};

struct sprefinfo {
	unsigned short next_offset;
	unsigned short num_servers;
	struct spref servers[1];		/* we overrun this array intentionally...*/
};
/* struct for GAG pioctl
 */
struct gaginfo {
	unsigned long showflags, logflags, logwritethruflag, spare[3];
	unsigned char spare2[128];
};
#define GAGUSER    1
#define GAGCONSOLE 2
#define logwritethruON	1

struct rxparams {
	unsigned long maxsendsize, maxreceivesize;
	unsigned long spare[30];
      };

/* struct for checkservers */

struct chservinfo
{
        int magic;
	char tbuffer[128];
	int tsize;
	long tinterval;
	long tflags;
}
;


/* state bits for volume */
#define VRO			1		/* volume is readonly */
#define VRecheck		2		/* recheck volume info with server */
#define	VBackup			4		/* is this a backup volume? */
#define	VForeign		8		/* this is a non-afs volume */

struct volume {
    /* One structure per volume, describing where the volume is located
	  and where its mount points are. */
    struct volume *next;	/* Next volume in hash list. */
    long cell;			/* the cell in which the volume resides */
    afs_rwlock_t lock;		/* the lock for this structure */
    long volume;		/* This volume's ID number. */
    char *name;			/* This volume's name, or 0 if unknown */
    struct server *serverHost[MAXHOSTS];    /* servers serving this volume */
    struct VenusFid dotdot;	/* dir to access as .. */
    struct VenusFid mtpoint;	/* The mount point for this volume. */
    long rootVnode, rootUnique;	/* Volume's root fid */
    long roVol;
    long backVol;
    long rwVol;			/* For r/o vols, original read/write volume. */
    long accessTime;		/* last time we used it */
    long vtix;			/* volume table index */
    long copyDate;		/* copyDate field, for tracking vol releases */
    long expireTime;            /* for per-volume callbacks... */
    short refCount;		/* reference count for allocation */
    char states;		/* snuck here for alignment reasons */
};

#ifndef	AFS_FINEGR_SUNLOCK
#define afs_PutVolume(av, locktype) ((av)->refCount--)
#else
#define afs_PutVolume(av, locktype) 	(av)->refCount --; \
                                        if (locktype) ReleaseLock(&(av)->lock, (locktype))
#endif

/* format of an entry in volume info file */
struct fvolume {
    long cell;			/* cell for this entry */
    long volume;		/* volume */
    long next;			/* has index */
    struct VenusFid dotdot;	/* .. value */
    struct VenusFid mtpoint;	/* mt point's fid */
    long rootVnode, rootUnique;	/* Volume's root fid */
};

struct SimpleLocks {
    struct SimpleLocks *next;
    int type;
    long boff, eoff;
    long pid;
#if	defined(AFS_AIX32_ENV) || defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV)
    long sysid;
#endif
};

/* vcache state bits */
#define	CStatd	    1		/* has this file ever been stat'd? */
#define CBackup	    2		/* file is on a backup volume */
#define	CRO	    4		/* is it on a read-only volume */
#define	CMValid	    8		/* is the mount point info valid? */
#define	CCore	    0x10	/* storing a core file, needed since we don't get an open */
#define CDirty      0x20        /* file has been modified since first open (... O_RDWR) */
#define	CSafeStore  0x40	/* close must wait for store to finish (should be in fd) */
#define	CMAPPED	    0x80	/* Mapped files; primarily used by SunOS 4.0.x */
#define	CNSHARE	    0x100	/* support O_NSHARE semantics */
#define	CLied	    0x200
#define	CTruth	    0x400
#define	CWRITE_IGN  0x800	/* Next OS hack only */
#define	CUnique	    0x1000	/* vc's uniquifier - latest unifiquier for fid */
#define	CForeign    0x2000	/* this is a non-afs vcache */
#define CHasPages   0x4000
#define CRevokeWait 0x8000

#define	CPSIZE	    2
#ifdef AFS_NETBSD_ENV
#define vrefCount   v->v_usecount
#else
#define	vrefCount   v.v_count
#endif

#define	AFS_MAXDV   0x7fffffff	    /* largest dataversion number */
#define	AFS_NOTRUNC 0x7fffffff	    /* largest dataversion number */

/*
 * Fast map from vcache to dcache courtesy mts@umich.edu
 */
struct	vtodc
	{
	struct dcache * dc;
	u_long		stamp;
	struct osi_file * f;
	u_long		minLoc;	/* smallest offset into dc. */
	u_long		len;	/* largest offset into dc. */
	};

extern u_long afs_stampValue;		/* stamp for pair's usage */
#define	MakeStamp()	(++afs_stampValue)

#ifdef AFS_NETBSD_ENV
#define VCTOV(vc) ((vc)->v)
#define VTOVC(v) ((struct vcache *)(v)->v_data)
#else
#define VCTOV(vc) (&((vc)->v))
#define VTOVC(v) ((struct vcache *)(v))
#endif

struct vcache {
#if defined(AFS_NETBSD_ENV)
    struct vnode *v;			/* Has reference count in v.v_count */
#else
    struct vnode v;			/* Has reference count in v.v_count */
#endif
    struct afs_q lruq;			/* lru q next and prev */
    struct vcache *hnext;		/* Hash next */
    struct VenusFid fid;
    struct mstat {
        u_long Length;
        hyper DataVersion;
        u_long Date, ServerDate;
        u_long Owner;
	u_long Group;
        ushort Mode;			/* XXXX Should be long XXXX */
        unsigned char LinkCount;
	/* vnode type is in v.v_type */
    } m;
    afs_rwlock_t lock;			/* The lock on the vcache contents. */
#ifdef AFS_SUN_ENV
#ifdef	AFS_SUN5_ENV
    krwlock_t rwlock;
    struct cred *credp;
#endif
#ifdef	AFS_FINEGR_SUNLOCK
    afs_lock_t vm_lock;
    kcondvar_t vm_cv;    
#endif
    afs_bozoLock_t pvnLock;	/* lock for pvn layer, always set before vcache.lock */
#endif
#ifdef	AFS_AIX32_ENV
    afs_lock_t pvmlock;
    vmhandle_t vmh;
    int  segid;
    struct ucred *credp;
#endif
    long parentVnode;			/* Parent dir, if a file. */
    long parentUnique;
    struct VenusFid *mvid;		/* Either parent dir (if root) or root (if mt pt) */
    char *linkData;			/* Link data if a symlink. */
    hyper flushDV;			/* data version last flushed from text */
    hyper mapDV;			/* data version last flushed from map */
    u_long truncPos;			/* truncate file to this position at next store */
    u_long callback;			/* The callback host, if any */
    u_long cbExpires;			/* time the callback expires */
    long anyAccess;			/* System:AnyUser's access to this. */
    long activeV;
    struct SimpleLocks *slocks;
#ifdef AFS_NEXT_ENV
    short mfsCount;			/* count of mfs_fsync-type ops in progress */
#endif
    short opens;			/* The numbers of opens, read or write, on this file. */
    short execsOrWriters;		/* The number of execs (if < 0) or writers (if > 0) of
					 this file. */
    short flockCount;			/* count of flock readers, or -1 if writer */
	
/* defect 3083 */

#ifdef AFS_AIX_ENV
	int ownslock;	/* pid of owner of excl lock, else 0 */
#endif

    u_short states;			/* state bits */
    char mvstat;			/* 0->normal, 1->mt pt, 2->root. */
#ifndef MAXSCACHE
    long randomUid[CPSIZE];		/* random users' ids and access rights. */
    long randomAccess[CPSIZE];		/* watch for more rights in prs_fs.h */
#else
    struct axscache *Access;            /* the new access cache method */
#endif
    struct dcache *dchint;
    struct vtodc quick;
    struct afs_q callsort;
#if defined(AFS_SGI_ENV)
    daddr_t lastr;			/* for read-ahead */
    short rwlockpid;			/* pid of process owning rwlock */
    short locktrips;			/* # of rwlock reacquisitions */
    sema_t rwlock;			/* vop_rwlock for afs */
    pgno_t mapcnt;			/* # of pages mapped */
    struct cred *cred;			/* last writer's cred */
    int error;				/* errors from strategy */
#endif
#ifdef AFS_NETBSD_ENV
    pid_t rwlockpid;			/* pid of rwlocker */
    short locktrips;			/* how often locked? */
    short lockflags;			/* flags controlling lock protocol */
#define NBSD_LOCK_LOCKED	0x0001
#define NBSD_LOCK_WANTED	0x0002
#endif
};

#define	DONT_CHECK_MODE_BITS	0
#define	CHECK_MODE_BITS		1

#if defined(AFS_SGI_ENV)
#define AVCRWLOCK(avc)		(valusema(&(avc)->rwlock) <= 0)
#endif

struct vcxstat {
    struct VenusFid fid;
    hyper DataVersion;
    afs_rwlock_t lock;
    long parentVnode;	
    long parentUnique;
    hyper flushDV;
    hyper mapDV;
    long truncPos;
    long randomUid[CPSIZE];
    long callback;
    long cbExpires;
    long randomAccess[CPSIZE];
    long anyAccess;
    short opens;
    short execsOrWriters;
    short flockCount;
    char mvstat;
    char states;
};

/*----------------------------------------------------------------------
 * AFS Data cache definitions
 *
 * Each entry describes a Unix file on the local disk that is
 * is serving as a cached copy of all or part of a Vice file.
 * Entries live in circular queues for each hash table slot
 *
 * Which queue is this thing in?  Good question.
 * A struct dcache entry is in the freeDSlot queue when not associated with a cache slot (file).
 * Otherwise, it is in the DLRU queue.  The freeDSlot queue uses the lruq.next field as
 * its "next" pointer.
 *
 * Cache entries in the DLRU queue are either associated with vice files, in which case
 * they are hashed by hvNextp and hcNextp pointers, or they are in the freeDCList
 * and are not associated with any vice file.  This last list uses the hvNextp pointer for
 * its "next" pointer.
 *
 * Note that the DLRU and freeDSlot queues are *memory* queues, while the hvNextp and
 * hcNextp hash lists and the freeDCList all go through the disk elements.  Think of the
 * memory queues as lower-level queues caching a number of the disk entries.
 *----------------------------------------------------------------------*/

#define	NULLIDX	    (-1)	/* null index definition */
/* struct dcache states bits */
#define	DWriting    8		/* file being written (used for cache validation) */

/* dcache flags */
#define	DFNextStarted	1	/* next chunk has been prefetched already */
#define	DFEntryMod	2	/* has entry itself been modified? */
#define	DFFetching	4	/* file is currently being fetched */
#ifndef	AFS_FINEGR_SUNLOCK
#define	DFWaiting	8	/* someone waiting for file */
#endif
#define	DFFetchReq	0x10	/* someone is waiting for DFFetching to go on */


/* flags in afs_indexFlags array */
#define	IFEverUsed	1		/* index entry has >= 1 byte of data */
#define	IFFree		2		/* index entry in freeDCList */
#define	IFDataMod	4		/* file needs to be written out */
#define	IFFlag		8		/* utility flag */
#define	IFHasPages	16
#define	IFDirtyPages	32
#define	IFAnyPages	64
#define	IFLocked	128		/* otherwise locked in core... */

struct afs_ioctl {
	caddr_t in, out;	/* Data to be transferred in, or out */
	short in_size;		/* Size of input buffer <= 2K */
	short out_size;		/* Maximum size of output buffer, <= 2K */
};

/* CacheItems file has a header of type struct afs_fheader (keep aligned properly) */
struct afs_fheader {
    long magic;
    long firstCSize;
    long otherCSize;
    long spare;
};
#define	AFS_FHMAGIC	    0x7635faba

/* kept on disk and in dcache entries */
struct fcache {
    long hvNextp;		/* Next in vnode hash table, or freeDCList */
    long hcNextp;		/* Next index in [fid, chunk] hash table */
    struct VenusFid fid;	/* Fid for this file */
    long modTime;		/* last time this entry was modified */
    hyper versionNo;		/* Associated data version number */
    long chunk;			/* Relative chunk number */
    long inode;			/* Unix inode for this chunk */
    long chunkBytes;		/* Num bytes in this chunk */
    char states;		/* Has this chunk been modified? */
};

/* magic numbers to specify the cache type */

#define AFS_FCACHE_TYPE_UFS 0x0
#define AFS_FCACHE_TYPE_MEM 0x1
#define AFS_FCACHE_TYPE_NFS 0x2
#define AFS_FCACHE_TYPE_EPI 0x3

/* kept in memory */
struct dcache {
    struct afs_q lruq;		/* Free queue for in-memory images */
#ifdef	AFS_FINEGR_SUNLOCK
    kmutex_t lock;
    kmutex_t lockw;
    kcondvar_t cv_waiting;
    kmutex_t lockf;
    kcondvar_t cv_fetchreq;
#else
    afs_rwlock_t lock;		/* XXX */
#endif
    short refCount;		/* Associated reference count. */
    long index;			/* The index in the CacheInfo file*/
    short flags;		/* more flags bits */
    long validPos;		/* number of valid bytes during fetch */
    struct fcache f;		/* disk image */
    long stamp; 		/* used with vtodc struct for hints */
};
/* this is obsolete and should be removed */
#define ihint stamp 

/* macro to mark a dcache entry as bad */
#define ZapDCE(x)	((x)->f.fid.Fid.Unique = 0, (x)->flags |= DFEntryMod)

/* macros to compute useful numbers from offsets.  AFS_CHUNK gives the chunk
    number for a given offset; AFS_CHUNKOFFSET gives the offset into the chunk
    and AFS_CHUNKBASE gives the byte offset of the base of the chunk.
      AFS_CHUNKSIZE gives the size of the chunk containing an offset.
      AFS_CHUNKTOBASE converts a chunk # to a base position.
      Chunks are 0 based and go up by exactly 1, covering the file.
      The other fields are internal and shouldn't be used */
/* basic parameters */
#ifdef AFS_NOCHUNKING

#define	AFS_OTHERCSIZE	0x10000
#define	AFS_LOGCHUNK	16
#define	AFS_FIRSTCSIZE	0x40000000

#else /* AFS_NOCHUNKING */

extern long afs_OtherCSize, afs_LogChunk, afs_FirstCSize;

#define AFS_OTHERCSIZE  (afs_OtherCSize)
#define AFS_LOGCHUNK    (afs_LogChunk)
#define AFS_FIRSTCSIZE  (afs_FirstCSize)

#define AFS_DEFAULTCSIZE 0x10000
#define AFS_DEFAULTLSIZE 16

#endif /* AFS_NOCHUNKING */

#define AFS_MINCHUNK 13  /* 8k is minimum */
#define AFS_MAXCHUNK 18  /* 256K is maximum */

#ifdef notdef
extern int afs_ChunkOffset(), afs_Chunk(), afs_ChunkBase(), afs_ChunkSize(), 
    afs_ChunkToBase(), afs_ChunkToSize();

/* macros */
#define	AFS_CHUNKOFFSET(x) afs_ChunkOffset(x)
#define	AFS_CHUNK(x) afs_Chunk(x)
#define	AFS_CHUNKBASE(x) afs_ChunkBase(x)
#define	AFS_CHUNKSIZE(x) afs_ChunkSize(x)
#define	AFS_CHUNKTOBASE(x) afs_ChunkToBase(x)
#define	AFS_CHUNKTOSIZE(x) afs_ChunkToSize(x)
#endif

#define AFS_CHUNKOFFSET(offset) ((offset < afs_FirstCSize) ? offset : \
			 ((offset - afs_FirstCSize) & (afs_OtherCSize - 1)))

#define AFS_CHUNK(offset) ((offset < afs_FirstCSize) ? 0 : \
	                 (((offset - afs_FirstCSize) >> afs_LogChunk) + 1))

#define AFS_CHUNKBASE(offset) ((offset < afs_FirstCSize) ? 0 : \
	(((offset - afs_FirstCSize) & ~(afs_OtherCSize - 1)) + afs_FirstCSize))

#define AFS_CHUNKSIZE(offset) ((offset < afs_FirstCSize) ? afs_FirstCSize : \
			       afs_OtherCSize)

#define AFS_CHUNKTOBASE(chunk) ((chunk == 0) ? 0 :               \
	(afs_FirstCSize + ((chunk - 1) << afs_LogChunk)))

#define AFS_CHUNKTOSIZE(chunk) ((chunk == 0) ? afs_FirstCSize :	afs_OtherCSize)

/* sizes are a power of two */
#define AFS_SETCHUNKSIZE(chunk) { afs_LogChunk = chunk; \
		      afs_FirstCSize = afs_OtherCSize = (1 << chunk);  }

/* FakeOpen and Fake Close used to be real subroutines.  They're only used in
 * sun_subr and afs_vnodeops, and they're very frequently called, so I made 
 * them into macros.  They do:
 * FakeOpen:  fake the file being open for writing.  avc->lock must be held
 * in write mode.  Having the file open for writing is like having a DFS
 * write-token: you're known to have the best version of the data around, 
 * and so the CM won't let it be overwritten by random server info.
 * FakeClose:  undo the effects of FakeOpen, noting that we want to ensure
 * that a real close eventually gets done.  We use CCore to achieve this if
 * we would end up closing the file.  avc->lock must be held in write mode */

#define afs_FakeOpen(avc) { avc->opens++; avc->execsOrWriters++; }
#define afs_FakeClose(avc, acred) \
{ if (avc->execsOrWriters == 1) {  \
	/* we're the last writer, just use CCore flag */   \
	avc->states |= CCore;	/* causes close to be called later */ \
                                                                      \
	/* The cred and vnode holds will be released in afs_FlushActiveVcaches */  \
	crhold(acred);                                                        \
	AFS_HOLD(VCTOV(avc));	/* So it won't disappear */           \
	avc->linkData =	(char *) acred;	/* Should use a better place for the creds */ \
    }                                                                         \
    else {                                                                    \
	/* we're not the last writer, let the last one do the store-back for us */    \
	avc->opens--;                                                         \
	avc->execsOrWriters--;                                                \
    }                                                                         \
}                                                                             

#define	AFS_ZEROS   64	    /* zero buffer */

/*#define afs_DirtyPages(avc)	(((avc)->states & CDirty) || osi_VMDirty_p((avc)))*/
#define	afs_DirtyPages(avc)	((avc)->states & CDirty)

#define DCHASHSIZE 1024
#define DVHASHSIZE 1024
/* The PFlush algorithm makes use of the fact that Fid.Unique is not used in
  below hash algorithms.  Change it if need be so that flushing algorithm
  doesn't move things from one hash chain to another
*/
#define	DCHash(v, c)	((((v)->Fid.Vnode + (v)->Fid.Volume + (c))) & (DCHASHSIZE-1))
	/*Vnode, Chunk -> Hash table index*/
#define	DVHash(v)	((((v)->Fid.Vnode + (v)->Fid.Volume )) & (DVHASHSIZE-1))
	/*Vnode -> Other hash table index*/
/* don't hash on the cell, our callback-breaking code sometimes fails to compute
    the cell correctly, and only scans one hash bucket */
#define	VCHash(fid)	(((fid)->Fid.Volume + (fid)->Fid.Vnode) & (VCSIZE-1))

extern struct dcache **afs_indexTable;		/*Pointers to in-memory dcache entries*/
extern long afs_cacheFiles;			/*Size of afs_indexTable*/
extern long afs_cacheBlocks;			/*1K blocks in cache*/
extern long afs_cacheStats;			/*Stat entries in cache*/
extern struct vcache *afs_vhashT[VCSIZE];	/*Stat cache hash table*/
extern long afs_initState;			/*Initialization state*/
extern long afs_termState;			/* Termination state */
extern struct VenusFid afs_rootFid;		/*Root for whole file system*/
extern long afs_allCBs;				/* Count of callbacks*/
extern long afs_oddCBs;				/* Count of odd callbacks*/
extern long afs_evenCBs;			/* Count of even callbacks*/
extern long afs_allZaps;			/* Count of fid deletes */
extern long afs_oddZaps;			/* Count of odd fid deletes */
extern long afs_evenZaps;			/* Count of even fid deletes */
extern struct brequest afs_brs[NBRS];		/* request structures */
extern long afs_debug;

#define	UHash(auid)	((auid) & (NUSERS-1))
#define	VHash(avol)	((avol)&(NVOLS-1))
#define	SHash(aserv)	((ntohl(aserv)) & (NSERVERS-1))
#define	FVHash(acell,avol)  (((avol)+(acell)) & (NFENTRIES-1))

extern struct cell	    *afs_GetCell();
extern struct cell	    *afs_GetCellByName();
extern struct unixuser	    *afs_GetUser();
extern struct volume	    *afs_GetVolume();
extern struct volume	    *afs_GetVolumeByName();
extern struct conn	    *afs_Conn();
extern struct conn	    *afs_ConnByHost();
extern struct conn	    *afs_ConnByMHosts();
extern struct server	    *afs_GetServer();
extern long		    afs_NewCell();
extern struct dcache	    *afs_GetDCache();
extern struct dcache	    *afs_FindDCache();
extern struct dcache	    *afs_NewDCache();
extern struct dcache	    *afs_GetDSlot();
extern struct vcache	    *afs_GetVCache();
extern struct brequest	    *afs_BQueue();

#ifdef	AFS_GLOBAL_SUNLOCK
extern kmutex_t afs_global_lock;
#endif

#if defined(AFS_SGI_ENV)
extern sema_t afs_sgibksync;
extern sema_t afs_sgibkwait;
extern lock_t afs_sgibklock;
extern struct dcache *afs_sgibklist;
#endif

/* Performance hack - we could replace VerifyVCache2 with the appropriate
 * GetVCache incantation, and could eliminate even this code from afs_UFSRead 
 * by making intentionally invalidating quick.stamp in the various callbacks
 * expiration/breaking code */
#define afs_VerifyVCache(avc, areq)  \
  (((avc)->states & CStatd) ? 0 : afs_VerifyVCache2((avc),areq))

#endif /* KERNEL */

#define	AFS_FSPORT	    htons(7000)
#define	AFS_VLPORT	    htons(7003)


#endif	__AFS_CONFIG_INCL_ENV_
