1 #define	KNAMELEN		28	/* max length of name held in kernel */
2 #define	DOMLEN			64
3 
4 #define	BLOCKALIGN		8
5 
6 typedef struct Alarms	Alarms;
7 typedef struct Block	Block;
8 typedef struct CSN	CSN;
9 typedef struct Chan	Chan;
10 typedef struct Cmdbuf	Cmdbuf;
11 typedef struct Cmdtab	Cmdtab;
12 typedef struct Cname	Cname;
13 typedef struct Conf	Conf;
14 typedef struct Dev	Dev;
15 typedef struct Dirtab	Dirtab;
16 typedef struct Edfinterface	Edfinterface;
17 typedef struct Egrp	Egrp;
18 typedef struct Evalue	Evalue;
19 typedef struct Fgrp	Fgrp;
20 typedef struct FPsave	FPsave;
21 typedef struct DevConf	DevConf;
22 typedef struct Label	Label;
23 typedef struct List	List;
24 typedef struct Log	Log;
25 typedef struct Logflag	Logflag;
26 typedef struct Mntcache Mntcache;
27 typedef struct Mount	Mount;
28 typedef struct Mntrpc	Mntrpc;
29 typedef struct Mntwalk	Mntwalk;
30 typedef struct Mnt	Mnt;
31 typedef struct Mhead	Mhead;
32 typedef struct Note	Note;
33 typedef struct Page	Page;
34 typedef struct Palloc	Palloc;
35 typedef struct Perf	Perf;
36 typedef struct Pgrps	Pgrps;
37 typedef struct PhysUart	PhysUart;
38 typedef struct Pgrp	Pgrp;
39 typedef struct Physseg	Physseg;
40 typedef struct Proc	Proc;
41 typedef struct Pte	Pte;
42 typedef struct Pthash	Pthash;
43 typedef struct Queue	Queue;
44 typedef struct Ref	Ref;
45 typedef struct Rendez	Rendez;
46 typedef struct Rgrp	Rgrp;
47 typedef struct RWlock	RWlock;
48 typedef struct Schedq	Schedq;
49 typedef struct Segment	Segment;
50 typedef struct Session	Session;
51 typedef struct Task	Task;
52 typedef struct Talarm	Talarm;
53 typedef struct Timer	Timer;
54 typedef struct Uart	Uart;
55 typedef struct Ureg Ureg;
56 typedef struct Waitq	Waitq;
57 typedef struct Walkqid	Walkqid;
58 typedef int    Devgen(Chan*, char*, Dirtab*, int, int, Dir*);
59 
60 #include "fcall.h"
61 
62 enum
63 {
64 	SnarfSize = 64*1024,
65 };
66 
67 struct Conf
68 {
69 	ulong	nmach;		/* processors */
70 	ulong	nproc;		/* processes */
71 	ulong	monitor;	/* has monitor? */
72 	ulong	npage0;		/* total physical pages of memory */
73 	ulong	npage1;		/* total physical pages of memory */
74 	ulong	npage;		/* total physical pages of memory */
75 	ulong	upages;		/* user page pool */
76 	ulong	nimage;		/* number of page cache image headers */
77 	ulong	nswap;		/* number of swap pages */
78 	int	nswppo;		/* max # of pageouts per segment pass */
79 	ulong	base0;		/* base of bank 0 */
80 	ulong	base1;		/* base of bank 1 */
81 	ulong	copymode;	/* 0 is copy on write, 1 is copy on reference */
82 	ulong	ialloc;		/* max interrupt time allocation in bytes */
83 	ulong	pipeqsize;	/* size in bytes of pipe queues */
84 	int	nuart;		/* number of uart devices */
85 };
86 
87 struct Label
88 {
89 	jmp_buf	buf;
90 };
91 
92 struct Ref
93 {
94 	Lock lk;
95 	long	ref;
96 };
97 
98 struct Rendez
99 {
100 	Lock lk;
101 	Proc	*p;
102 };
103 
104 struct RWlock	/* changed from kernel */
105 {
106 	int	readers;
107 	Lock	lk;
108 	QLock	x;
109 	QLock	k;
110 };
111 
112 struct Talarm
113 {
114 	Lock lk;
115 	Proc	*list;
116 };
117 
118 struct Alarms
119 {
120 	QLock lk;
121 	Proc	*head;
122 };
123 
124 /*
125  * Access types in namec & channel flags
126  */
127 enum
128 {
129 	Aaccess,			/* as in stat, wstat */
130 	Abind,			/* for left-hand-side of bind */
131 	Atodir,				/* as in chdir */
132 	Aopen,				/* for i/o */
133 	Amount,				/* to be mounted or mounted upon */
134 	Acreate,			/* is to be created */
135 	Aremove,			/* will be removed by caller */
136 
137 	COPEN	= 0x0001,		/* for i/o */
138 	CMSG	= 0x0002,		/* the message channel for a mount */
139 /*	CCREATE	= 0x0004,		permits creation if c->mnt */
140 	CCEXEC	= 0x0008,		/* close on exec */
141 	CFREE	= 0x0010,		/* not in use */
142 	CRCLOSE	= 0x0020,		/* remove on close */
143 	CCACHE	= 0x0080,		/* client cache */
144 };
145 
146 /* flag values */
147 enum
148 {
149 	BINTR	=	(1<<0),
150 	BFREE	=	(1<<1),
151 	Bipck	=	(1<<2),		/* ip checksum */
152 	Budpck	=	(1<<3),		/* udp checksum */
153 	Btcpck	=	(1<<4),		/* tcp checksum */
154 	Bpktck	=	(1<<5),		/* packet checksum */
155 };
156 
157 struct Block
158 {
159 	Block*	next;
160 	Block*	list;
161 	uchar*	rp;			/* first unconsumed byte */
162 	uchar*	wp;			/* first empty byte */
163 	uchar*	lim;			/* 1 past the end of the buffer */
164 	uchar*	base;			/* start of the buffer */
165 	void	(*free)(Block*);
166 	ushort	flag;
167 	ushort	checksum;		/* IP checksum of complete packet (minus media header) */
168 };
169 #define BLEN(s)	((s)->wp - (s)->rp)
170 #define BALLOC(s) ((s)->lim - (s)->base)
171 
172 struct Chan
173 {
174 	Ref ref;
175 	Chan*	next;			/* allocation */
176 	Chan*	link;
177 	vlong	offset;			/* in file */
178 	ushort	type;
179 	ulong	dev;
180 	ushort	mode;			/* read/write */
181 	ushort	flag;
182 	Qid	qid;
183 	int	fid;			/* for devmnt */
184 	ulong	iounit;	/* chunk size for i/o; 0==default */
185 	Mhead*	umh;			/* mount point that derived Chan; used in unionread */
186 	Chan*	umc;			/* channel in union; held for union read */
187 	QLock	umqlock;		/* serialize unionreads */
188 	int	uri;			/* union read index */
189 	int	dri;			/* devdirread index */
190 	ulong	mountid;
191 	Mntcache *mcp;			/* Mount cache pointer */
192 	Mnt		*mux;		/* Mnt for clients using me for messages */
193 	void*	aux;
194 	Qid	pgrpid;		/* for #p/notepg */
195 	ulong	mid;		/* for ns in devproc */
196 	Chan*	mchan;			/* channel to mounted server */
197 	Qid	mqid;			/* qid of root of mount point */
198 	Session*session;
199 	Cname	*name;
200 };
201 
202 struct Cname
203 {
204 	Ref ref;
205 	int	alen;			/* allocated length */
206 	int	len;			/* strlen(s) */
207 	char	*s;
208 };
209 
210 struct Dev
211 {
212 	int	dc;
213 	char*	name;
214 
215 	void	(*reset)(void);
216 	void	(*init)(void);
217 	void	(*shutdown)(void);
218 	Chan*	(*attach)(char*);
219 	Walkqid*	(*walk)(Chan*, Chan*, char**, int);
220 	int	(*stat)(Chan*, uchar*, int);
221 	Chan*	(*open)(Chan*, int);
222 	void	(*create)(Chan*, char*, int, ulong);
223 	void	(*close)(Chan*);
224 	long	(*read)(Chan*, void*, long, vlong);
225 	Block*	(*bread)(Chan*, long, ulong);
226 	long	(*write)(Chan*, void*, long, vlong);
227 	long	(*bwrite)(Chan*, Block*, ulong);
228 	void	(*remove)(Chan*);
229 	int	(*wstat)(Chan*, uchar*, int);
230 	void	(*power)(int);	/* power mgt: power(1) => on, power (0) => off */
231 	int	(*config)(int, char*, DevConf*);	// returns nil on error
232 };
233 
234 struct Dirtab
235 {
236 	char	name[KNAMELEN];
237 	Qid	qid;
238 	vlong length;
239 	ulong	perm;
240 };
241 
242 struct Walkqid
243 {
244 	Chan	*clone;
245 	int	nqid;
246 	Qid	qid[1];
247 };
248 
249 enum
250 {
251 	NSMAX	=	1000,
252 	NSLOG	=	7,
253 	NSCACHE	=	(1<<NSLOG),
254 };
255 
256 struct Mntwalk				/* state for /proc/#/ns */
257 {
258 	int		cddone;
259 	ulong	id;
260 	Mhead*	mh;
261 	Mount*	cm;
262 };
263 
264 struct Mount
265 {
266 	ulong	mountid;
267 	Mount*	next;
268 	Mhead*	head;
269 	Mount*	copy;
270 	Mount*	order;
271 	Chan*	to;			/* channel replacing channel */
272 	int	mflag;
273 	char	*spec;
274 };
275 
276 struct Mhead
277 {
278 	Ref ref;
279 	RWlock	lock;
280 	Chan*	from;			/* channel mounted upon */
281 	Mount*	mount;			/* what's mounted upon it */
282 	Mhead*	hash;			/* Hash chain */
283 };
284 
285 struct Mnt
286 {
287 	Lock lk;
288 	/* references are counted using c->ref; channels on this mount point incref(c->mchan) == Mnt.c */
289 	Chan	*c;		/* Channel to file service */
290 	Proc	*rip;		/* Reader in progress */
291 	Mntrpc	*queue;		/* Queue of pending requests on this channel */
292 	ulong	id;		/* Multiplexer id for channel check */
293 	Mnt	*list;		/* Free list */
294 	int	flags;		/* cache */
295 	int	msize;		/* data + IOHDRSZ */
296 	char	*version;			/* 9P version */
297 	Queue	*q;		/* input queue */
298 };
299 
300 enum
301 {
302 	NUser,				/* note provided externally */
303 	NExit,				/* deliver note quietly */
304 	NDebug,				/* print debug message */
305 };
306 
307 struct Note
308 {
309 	char	msg[ERRMAX];
310 	int	flag;			/* whether system posted it */
311 };
312 
313 enum
314 {
315 	RENDLOG	=	5,
316 	RENDHASH =	1<<RENDLOG,		/* Hash to lookup rendezvous tags */
317 	MNTLOG	=	5,
318 	MNTHASH =	1<<MNTLOG,		/* Hash to walk mount table */
319 	NFD =		100,		/* per process file descriptors */
320 	PGHLOG  =	9,
321 	PGHSIZE	=	1<<PGHLOG,	/* Page hash for image lookup */
322 };
323 #define REND(p,s)	((p)->rendhash[(s)&((1<<RENDLOG)-1)])
324 #define MOUNTH(p,qid)	((p)->mnthash[(qid).path&((1<<MNTLOG)-1)])
325 
326 struct Pgrp
327 {
328 	Ref ref;				/* also used as a lock when mounting */
329 	int	noattach;
330 	ulong	pgrpid;
331 	QLock	debug;			/* single access via devproc.c */
332 	RWlock	ns;			/* Namespace n read/one write lock */
333 	Mhead	*mnthash[MNTHASH];
334 };
335 
336 struct Rgrp
337 {
338 	Ref ref;
339 	Proc	*rendhash[RENDHASH];	/* Rendezvous tag hash */
340 };
341 
342 struct Egrp
343 {
344 	Ref ref;
345 	RWlock lk;
346 	Evalue	**ent;
347 	int nent;
348 	int ment;
349 	ulong	path;	/* qid.path of next Evalue to be allocated */
350 	ulong	vers;	/* of Egrp */
351 };
352 
353 struct Evalue
354 {
355 	char	*name;
356 	char	*value;
357 	int	len;
358 	Evalue	*link;
359 	Qid	qid;
360 };
361 
362 struct Fgrp
363 {
364 	Ref ref;
365 	Chan	**fd;
366 	int	nfd;			/* number allocated */
367 	int	maxfd;			/* highest fd in use */
368 	int	exceed;			/* debugging */
369 };
370 
371 enum
372 {
373 	DELTAFD	= 20,		/* incremental increase in Fgrp.fd's */
374 	NERR = 20
375 };
376 
377 typedef uvlong	Ticks;
378 
379 enum
380 {
381 	Running,
382 	Rendezvous,
383 	Wakeme,
384 };
385 
386 struct Proc
387 {
388 	uint		state;
389 	uint		mach;
390 
391 	ulong	pid;
392 	ulong	parentpid;
393 
394 	Pgrp	*pgrp;		/* Process group for namespace */
395 	Fgrp	*fgrp;		/* File descriptor group */
396 	Rgrp *rgrp;
397 
398 	Lock	rlock;		/* sync sleep/wakeup with postnote */
399 	Rendez	*r;		/* rendezvous point slept on */
400 	Rendez	sleep;		/* place for syssleep/debug */
401 	int	notepending;	/* note issued but not acted on */
402 	int	kp;		/* true if a kernel process */
403 
404 	void*	rendtag;	/* Tag for rendezvous */
405 	void*	rendval;	/* Value for rendezvous */
406 	Proc	*rendhash;	/* Hash list for tag values */
407 
408 	int	nerrlab;
409 	Label	errlab[NERR];
410 	char user[KNAMELEN];
411 	char	*syserrstr;	/* last error from a system call, errbuf0 or 1 */
412 	char	*errstr;	/* reason we're unwinding the error stack, errbuf1 or 0 */
413 	char	errbuf0[ERRMAX];
414 	char	errbuf1[ERRMAX];
415 	char	genbuf[128];	/* buffer used e.g. for last name element from namec */
416 	char text[KNAMELEN];
417 
418 	Chan	*slash;
419 	Chan	*dot;
420 
421 	Proc		*qnext;
422 
423 	void	(*fn)(void*);
424 	void *arg;
425 
426 	char oproc[1024];	/* reserved for os */
427 
428 };
429 
430 enum
431 {
432 	PRINTSIZE =	256,
433 	MAXCRYPT = 	127,
434 	NUMSIZE	=	12,		/* size of formatted number */
435 	MB =		(1024*1024),
436 	READSTR =	1000,		/* temporary buffer size for device reads */
437 };
438 
439 extern	char*	conffile;
440 extern	int	cpuserver;
441 extern	Dev*	devtab[];
442 extern  char	*eve;
443 extern	char	hostdomain[];
444 extern	uchar	initcode[];
445 extern  Queue*	kbdq;
446 extern  Queue*	kprintoq;
447 extern  Ref	noteidalloc;
448 extern	Palloc	palloc;
449 extern  Queue	*serialoq;
450 extern	char*	statename[];
451 extern	int	nsyscall;
452 extern	char	*sysname;
453 extern	uint	qiomaxatomic;
454 extern	Conf	conf;
455 enum
456 {
457 	LRESPROF	= 3,
458 };
459 
460 /*
461  *  action log
462  */
463 struct Log {
464 	Lock lk;
465 	int	opens;
466 	char*	buf;
467 	char	*end;
468 	char	*rptr;
469 	int	len;
470 	int	nlog;
471 	int	minread;
472 
473 	int	logmask;	/* mask of things to debug */
474 
475 	QLock	readq;
476 	Rendez	readr;
477 };
478 
479 struct Logflag {
480 	char*	name;
481 	int	mask;
482 };
483 
484 enum
485 {
486 	NCMDFIELD = 128
487 };
488 
489 struct Cmdbuf
490 {
491 	char	*buf;
492 	char	**f;
493 	int	nf;
494 };
495 
496 struct Cmdtab
497 {
498 	int	index;	/* used by client to switch on result */
499 	char	*cmd;	/* command name */
500 	int	narg;	/* expected #args; 0 ==> variadic */
501 };
502 
503 /* queue state bits,  Qmsg, Qcoalesce, and Qkick can be set in qopen */
504 enum
505 {
506 	/* Queue.state */
507 	Qstarve		= (1<<0),	/* consumer starved */
508 	Qmsg		= (1<<1),	/* message stream */
509 	Qclosed		= (1<<2),	/* queue has been closed/hungup */
510 	Qflow		= (1<<3),	/* producer flow controlled */
511 	Qcoalesce	= (1<<4),	/* coallesce packets on read */
512 	Qkick		= (1<<5),	/* always call the kick routine after qwrite */
513 };
514 
515 #define DEVDOTDOT -1
516 
517 extern Proc *_getproc(void);
518 extern void _setproc(Proc*);
519 #define	up	(_getproc())
520