]> git.lizzy.rs Git - plan9front.git/blob - sys/src/cmd/cwfs/portdat.h
cwfs: remove reflock
[plan9front.git] / sys / src / cmd / cwfs / portdat.h
1 /*
2  * fundamental constants and types of the implementation
3  * changing any of these changes the layout on disk
4  */
5 enum {
6         SUPER_ADDR      = 2,            /* block address of superblock */
7         ROOT_ADDR       = 3,            /* block address of root directory */
8 };
9
10 /* more fundamental types */
11 typedef vlong Wideoff; /* type to widen Off to for printing; ≥ as wide as Off */
12 typedef short   Userid;         /* signed internal representation of user-id */
13 typedef long    Timet;          /* in seconds since epoch */
14 typedef vlong   Devsize;        /* in bytes */
15
16
17 /* macros */
18 #define NEXT(x, l)      (((x)+1) % (l))
19 #define PREV(x, l)      ((x) == 0? (l)-1: (x)-1)
20 #define HOWMANY(x, y)   (((x)+((y)-1)) / (y))
21 #define ROUNDUP(x, y)   (HOWMANY((x), (y)) * (y))
22
23 #define TK2MS(t) (((ulong)(t)*1000)/HZ) /* ticks to ms - beware rounding */
24 #define MS2TK(t) (((ulong)(t)*HZ)/1000) /* ms to ticks - beware rounding */
25 #define TK2SEC(t) ((t)/HZ)              /* ticks to seconds */
26
27 /* constants that don't affect disk layout */
28 enum {
29         MAXDAT          = 8192,         /* max allowable data message */
30         MAXMSG          = 128,          /* max protocol message sans data */
31
32         MB              = 1024*1024,
33
34         HZ              = 1,            /* clock frequency */
35 };
36
37 /*
38  * tunable parameters
39  */
40 enum {
41         Maxword         = 256,          /* max bytes per command-line word */
42         NTLOCK          = 200,          /* number of active file Tlocks */
43 };
44
45 typedef struct  Bp      Bp;
46 typedef struct  Bucket  Bucket;
47 typedef struct  Cache   Cache;
48 typedef struct  Centry  Centry;
49 typedef struct  Chan    Chan;
50 typedef struct  Command Command;
51 typedef struct  Conf    Conf;
52 typedef struct  Cons    Cons;
53 typedef struct  Dentry  Dentry;
54 typedef struct  Device  Device;
55 typedef struct  Fbuf    Fbuf;
56 typedef struct  File    File;
57 typedef struct  Filsys  Filsys;
58 typedef struct  Filter  Filter;
59 typedef struct  Flag    Flag;
60 typedef struct  Hiob    Hiob;
61 typedef struct  Iobuf   Iobuf;
62 typedef struct  Lock    Lock;
63 typedef struct  Msgbuf  Msgbuf;
64 typedef struct  QLock   QLock;
65 typedef struct  Qid9p1  Qid9p1;
66 typedef struct  Queue   Queue;
67 typedef union   Rabuf   Rabuf;
68 typedef struct  Rendez  Rendez;
69 typedef struct  Rtc     Rtc;
70 typedef struct  Startsb Startsb;
71 typedef struct  Super1  Super1;
72 typedef struct  Superb  Superb;
73 typedef struct  Tag     Tag;
74 typedef struct  Time    Time;
75 typedef struct  Tlock   Tlock;
76 typedef struct  Tm      Tm;
77 typedef struct  Uid     Uid;
78 typedef struct  Wpath   Wpath;
79
80 struct  Tag
81 {
82         short   pad;            /* make tag end at a long boundary */
83         short   tag;
84         Off     path;
85 };
86
87 /* DONT TOUCH, this is the disk structure */
88 struct  Qid9p1
89 {
90         Off     path;                   /* was long */
91         ulong   version;                /* should be Off */
92 };
93
94 /* DONT TOUCH, this is the disk structure */
95 struct  Super1
96 {
97         Off     fstart;
98         Off     fsize;
99         Off     tfree;
100         Off     qidgen;         /* generator for unique ids */
101         /*
102          * Stuff for WWC device
103          */
104         Off     cwraddr;        /* cfs root addr */
105         Off     roraddr;        /* dump root addr */
106         Off     last;           /* last super block addr */
107         Off     next;           /* next super block addr */
108 };
109
110 /* DONT TOUCH, this is the disk structure */
111 struct  Centry
112 {
113         ushort  age;
114         short   state;
115         Off     waddr;          /* worm addr */
116 };
117
118 /* DONT TOUCH, this is the disk structure */
119 struct  Dentry
120 {
121         char    name[NAMELEN];
122         Userid  uid;
123         Userid  gid;
124         ushort  mode;
125                 #define DALLOC  0x8000
126                 #define DDIR    0x4000
127                 #define DAPND   0x2000
128                 #define DLOCK   0x1000
129                 #define DTMP    0x0800
130                 #define DREAD   0x4
131                 #define DWRITE  0x2
132                 #define DEXEC   0x1
133         Userid  muid;
134         Qid9p1  qid;
135         Off     size;
136         Off     dblock[NDBLOCK];
137         Off     iblocks[NIBLOCK];
138         long    atime;
139         long    mtime;
140 };
141
142 /*
143  * derived constants
144  */
145 enum {
146         BUFSIZE         = RBUFSIZE - sizeof(Tag),
147         DIRPERBUF       = BUFSIZE / sizeof(Dentry),
148         INDPERBUF       = BUFSIZE / sizeof(Off),
149         FEPERBUF        = (BUFSIZE-sizeof(Super1)-sizeof(Off)) / sizeof(Off),
150         SMALLBUF        = MAXMSG,
151         LARGEBUF        = MAXMSG+MAXDAT+256,
152         RAGAP           = (300*1024)/BUFSIZE,   /* readahead parameter */
153         BKPERBLK        = 10,
154         CEPERBK         = (BUFSIZE - BKPERBLK*sizeof(Off)) /
155                                 (sizeof(Centry)*BKPERBLK),
156 };
157
158 /*
159  * send/recv queue structure
160  */
161 struct  Queue
162 {
163         char*   name;           /* for debugging */
164
165         int     size;           /* size of queue */
166
167         long    count;          /* how many in queue (semaphore) */
168         long    avail;          /* how many available to send (semaphore) */
169
170         Lock    rl, wl;         /* circular pointers */
171         void    **rp;
172         void    **wp;
173
174         void*   args[1];        /* list of saved pointers, [->size] */
175 };
176
177 struct  Device
178 {
179         uchar   type;
180         uchar   init;
181         Device* link;                   /* link for mcat/mlev/mirror */
182         Device* dlink;                  /* link all devices */
183         void*   private;
184         Devsize size;
185         union {
186                 struct {                /* disk, (l)worm in j.j, sides */
187                         int     ctrl;   /* disks only */
188                         int     targ;
189                         int     lun;    /* not implemented in sd(3) */
190
191                         int     mapped;
192                         char*   file;   /* ordinary file or dir instead */
193
194                         int     fd;
195                         char*   sddir;  /* /dev/sdXX name, for juke drives */
196                         char*   sddata; /* /dev/sdXX/data or other file */
197                 } wren;
198                 struct {                /* mcat mlev mirror */
199                         Device* first;
200                         Device* last;
201                         int     ndev;
202                 } cat;
203                 struct {                /* cw */
204                         Device* c;      /* cache device */
205                         Device* w;      /* worm device */
206                         Device* ro;     /* dump - readonly */
207                 } cw;
208                 struct {                /* juke */
209                         Device* j;      /* (robotics, worm drives) - wrens */
210                         Device* m;      /* (sides) - r or l devices */
211                 } j;
212                 struct {                /* ro */
213                         Device* parent;
214                 } ro;
215                 struct {                /* fworm */
216                         Device* fw;
217                 } fw;
218                 struct {                /* part */
219                         Device* d;
220                         long    base;   /* percentages */
221                         long    size;
222                 } part;
223                 struct {                /* byte-swapped */
224                         Device* d;
225                 } swab;
226         };
227 };
228
229 typedef struct Sidestarts {
230         Devsize sstart;                 /* blocks before start of side */
231         Devsize s1start;                /* blocks before start of next side */
232 } Sidestarts;
233
234 union Rabuf {
235         struct {
236                 Device* dev;
237                 Off     addr;
238         };
239         Rabuf*  link;
240 };
241
242 struct  Hiob
243 {
244         Iobuf*  link;
245         Lock;
246 };
247
248 /* a 9P connection */
249 struct  Chan
250 {
251         int     (*protocol)(Msgbuf*);   /* version */
252         int     msize;                  /* version */
253         char    whochan[50];
254         char    whoname[NAMELEN];
255         void    (*whoprint)(Chan*);
256         ulong   flags;
257         int     chan;                   /* overall channel #, mostly for printing */
258         int     nmsgs;                  /* outstanding messages, set under flock -- for flush */
259
260         Timet   whotime;
261         int     nfile;                  /* used by cmd_files */
262
263         RWLock  reflock;
264         Chan*   next;                   /* link list of chans */
265         Queue*  send;
266         Queue*  reply;
267
268         int     authok;
269         uchar   authinfo[64];
270
271         void*   pdata;                  /* sometimes is a Netconn* */
272
273         char    err[ERRMAX];
274 };
275
276 struct  Filsys
277 {
278         char*   name;                   /* name of filsys */
279         char*   conf;                   /* symbolic configuration */
280         Device* dev;                    /* device that filsys is on */
281         int     flags;
282                 #define FREAM           (1<<0)  /* mkfs */
283                 #define FRECOVER        (1<<1)  /* install last dump */
284                 #define FEDIT           (1<<2)  /* modified */
285 };
286
287 struct  Startsb
288 {
289         char*   name;
290         Off     startsb;
291 };
292
293 struct  Time
294 {
295         Timet   lasttoy;
296         Timet   offset;
297 };
298
299 /*
300  * array of qids that are locked
301  */
302 struct  Tlock
303 {
304         Device* dev;
305         Timet   time;
306         Off     qpath;
307         File*   file;
308 };
309
310 struct  Cons
311 {
312         ulong   flags;          /* overall flags for all channels */
313         QLock;                  /* generic qlock for mutex */
314         int     uid;            /* botch -- used to get uid on cons_create */
315         int     gid;            /* botch -- used to get gid on cons_create */
316         int     nuid;           /* number of uids */
317         int     ngid;           /* number of gids */
318         Off     offset;         /* used to read files, c.f. fchar */
319         int     chano;          /* generator for channel numbers */
320         Chan*   chan;           /* console channel */
321         Filsys* curfs;          /* current filesystem */
322
323         int     profile;        /* are we profiling? */
324         long*   profbuf;
325         ulong   minpc;
326         ulong   maxpc;
327         ulong   nprofbuf;
328
329         long    nlarge;         /* number of large message buffers */
330         long    nsmall;         /* ... small ... */
331         long    nwormre;        /* worm read errors */
332         long    nwormwe;        /* worm write errors */
333         long    nwormhit;       /* worm read cache hits */
334         long    nwormmiss;      /* worm read cache non-hits */
335         int     noage;          /* dont update cache age, dump and check */
336         long    nwrenre;        /* disk read errors */
337         long    nwrenwe;        /* disk write errors */
338         long    nreseq;         /* cache bucket resequence */
339
340 //      Filter  work[3];        /* thruput in messages */
341 //      Filter  rate[3];        /* thruput in bytes */
342 //      Filter  bhit[3];        /* getbufs that hit */
343 //      Filter  bread[3];       /* getbufs that miss and read */
344 //      Filter  brahead[3];     /* messages to readahead */
345 //      Filter  binit[3];       /* getbufs that miss and dont read */
346 };
347
348 struct  File
349 {
350         QLock;
351         Qid     qid;
352         Wpath*  wpath;
353         Chan*   cp;             /* null means a free slot */
354         Tlock*  tlock;          /* if file is locked */
355         File*   next;           /* in cp->flist */
356         Filsys* fs;
357         Off     addr;
358         long    slot;           /* ordinal # of Dentry with a directory block */
359         Off     lastra;         /* read ahead address */
360         ulong   fid;
361         Userid  uid;
362         void    *auth;
363         char    open;
364                 #define FREAD   1
365                 #define FWRITE  2
366                 #define FREMOV  4
367
368         Off     doffset;        /* directory reading */
369         ulong   dvers;
370         long    dslot;
371 };
372
373 struct  Wpath
374 {
375         Wpath*  up;             /* pointer upwards in path */
376         Off     addr;           /* directory entry addr */
377         long    slot;           /* directory entry slot */
378         short   refs;           /* number of files using this structure */
379 };
380
381 struct  Iobuf
382 {
383         QLock;
384         Device* dev;
385         Iobuf*  fore;           /* for lru */
386         Iobuf*  back;           /* for lru */
387         char*   iobuf;          /* only active while locked */
388         char*   xiobuf;         /* "real" buffer pointer */
389         Off     addr;
390         int     flags;
391 };
392
393 struct  Uid
394 {
395         Userid  uid;            /* user id */
396         Userid  lead;           /* leader of group */
397         Userid  *gtab;          /* group table */
398         int     ngrp;           /* number of group entries */
399         char    name[NAMELEN];  /* user name */
400 };
401
402 /* DONT TOUCH, this is the disk structure */
403 struct  Fbuf
404 {
405         Off     nfree;
406         Off     free[FEPERBUF];
407 };
408
409 /* DONT TOUCH, this is the disk structure */
410 struct  Superb
411 {
412         Fbuf    fbuf;
413         Super1;
414 };
415
416 struct  Conf
417 {
418         ulong   nmach;          /* processors */
419         ulong   nuid;           /* distinct uids */
420         ulong   nserve;         /* server processes */
421         ulong   nfile;          /* number of fid -- system wide */
422         ulong   nwpath;         /* number of active paths, derived from nfile */
423         ulong   gidspace;       /* space for gid names -- derived from nuid */
424
425         ulong   nlgmsg;         /* number of large message buffers */
426         ulong   nsmmsg;         /* number of small message buffers */
427
428         Off     recovcw;        /* recover addresses */
429         Off     recovro;
430         Off     firstsb;
431         Off     recovsb;
432
433         ulong   configfirst;    /* configure before starting normal operation */
434         char    *confdev;
435         char    *devmap;        /* name of config->file device mapping file */
436
437         uchar   nodump;         /* no periodic dumps */
438         uchar   dumpreread;     /* read and compare in dump copy */
439         uchar   newcache;
440 };
441
442 enum {
443         Mbmagic = 0xb0ffe3,
444 };
445
446 /*
447  * message buffers
448  * 2 types, large and small
449  */
450 struct  Msgbuf
451 {
452         ulong   magic;
453         short   count;
454         short   flags;
455                 #define LARGE   (1<<0)
456                 #define FREE    (1<<1)
457                 #define BFREE   (1<<2)
458                 #define BTRACE  (1<<7)
459         Chan*   chan;           /* file server conn within a net. conn */
460         Msgbuf* next;
461         uintptr param;          /* misc. use; keep Conn* here */
462
463         int     category;
464         uchar*  data;           /* rp or wp: current processing point */
465         uchar*  xdata;          /* base of allocation */
466 };
467
468 /*
469  * message buffer categories
470  */
471 enum
472 {
473         Mxxx            = 0,
474         Mbeth1,
475         Mbreply1,
476         Mbreply2,
477         Mbreply3,
478         Mbreply4,
479         MAXCAT,
480 };
481
482 enum { PRINTSIZE = 256 };
483
484 struct
485 {
486         Lock;
487         int     machs;
488         int     exiting;
489 } active;
490
491 struct  Command
492 {
493         char*   arg0;
494         char*   help;
495         void    (*func)(int, char*[]);
496 };
497
498 struct  Flag
499 {
500         char*   arg0;
501         char*   help;
502         ulong   flag;
503 };
504
505 struct  Rtc
506 {
507         int     sec;
508         int     min;
509         int     hour;
510         int     mday;
511         int     mon;
512         int     year;
513 };
514
515 typedef struct
516 {
517         /* constants during a given truncation */
518         Dentry  *d;
519         Iobuf   *p;                     /* the block containing *d */
520         int     uid;
521         Off     newsize;
522         Off     lastblk;                /* last data block of file to keep */
523
524         /* variables */
525         Off     relblk;                 /* # of current data blk within file */
526         int     pastlast;               /* have we walked past lastblk? */
527         int     err;
528 } Truncstate;
529
530 /*
531  * cw device
532  */
533
534 /* DONT TOUCH, this is the disk structure */
535 struct  Cache
536 {
537         Off     maddr;          /* cache map addr */
538         Off     msize;          /* cache map size in buckets */
539         Off     caddr;          /* cache addr */
540         Off     csize;          /* cache size */
541         Off     fsize;          /* current size of worm */
542         Off     wsize;          /* max size of the worm */
543         Off     wmax;           /* highwater write */
544
545         Off     sbaddr;         /* super block addr */
546         Off     cwraddr;        /* cw root addr */
547         Off     roraddr;        /* dump root addr */
548
549         Timet   toytime;        /* somewhere convienent */
550         Timet   time;
551 };
552
553 /* DONT TOUCH, this is the disk structure */
554 struct  Bucket
555 {
556         long    agegen;         /* generator for ages in this bkt */
557         Centry  entry[CEPERBK];
558 };
559
560 /* DONT TOUCH, this is in disk structures */
561 enum { Labmagic = 0xfeedfacedeadbeefULL, };
562
563 /* DONT TOUCH, this is the disk structure */
564 typedef struct Label Label;
565 struct Label                    /* label block on Devlworms, in last block */
566 {
567         uvlong  magic;
568         ushort  ord;            /* side number within Juke */
569         char    service[64];    /* documentation only */
570 };
571
572 typedef struct Map Map;
573 struct Map {
574         char    *from;
575         Device  *fdev;
576         char    *to;
577         Device  *tdev;
578         Map     *next;
579 };
580
581 /*
582  * scsi i/o
583  */
584 enum
585 {
586         SCSIread = 0,
587         SCSIwrite = 1,
588 };
589
590 /*
591  * Process states
592  */
593 enum
594 {
595         Dead = 0,
596         Moribund,
597         Zombie,
598         Ready,
599         Scheding,
600         Running,
601         Queueing,
602         Sending,
603         Recving,
604         MMUing,
605         Exiting,
606         Inwait,
607         Wakeme,
608         Broken,
609 };
610
611 /*
612  * devnone block numbers
613  */
614 enum
615 {
616         Cwio1   = 1,
617         Cwio2,
618         Cwxx1,
619         Cwxx2,
620         Cwxx3,
621         Cwxx4,
622         Cwdump1,
623         Cwdump2,
624         Cuidbuf,
625         Cckbuf,
626         Cwtmp,
627 };
628
629 /*
630  * error codes generated from the file server
631  */
632 enum
633 {
634         Ebadspc = 1,
635         Efid,
636         Echar,
637         Eopen,
638         Ecount,
639         Ealloc,
640         Eqid,
641         Eaccess,
642         Eentry,
643         Emode,
644         Edir1,
645         Edir2,
646         Ephase,
647         Eexist,
648         Edot,
649         Eempty,
650         Ebadu,
651         Enoattach,
652         Ewstatb,
653         Ewstatd,
654         Ewstatg,
655         Ewstatl,
656         Ewstatm,
657         Ewstato,
658         Ewstatp,
659         Ewstatq,
660         Ewstatu,
661         Ewstatv,
662         Ename,
663         Ewalk,
664         Eronly,
665         Efull,
666         Eoffset,
667         Elocked,
668         Ebroken,
669         Eauth,
670         Eauth2,
671         Efidinuse,
672         Etoolong,
673         Econvert,
674         Eversion,
675         Eauthdisabled,
676         Eauthnone,
677         Eauthfile,
678         Eedge,
679         MAXERR
680 };
681
682 /*
683  * device types
684  */
685 enum
686 {
687         Devnone = 0,
688         Devwren,                /* disk drive */
689         Devworm,                /* scsi optical drive */
690         Devlworm,               /* scsi optical drive (labeled) */
691         Devfworm,               /* fake read-only device */
692         Devjuke,                /* scsi jukebox */
693         Devcw,                  /* cache with worm */
694         Devro,                  /* readonly worm */
695         Devmcat,                /* multiple cat devices */
696         Devmlev,                /* multiple interleave devices */
697         Devnet,                 /* network connection */
698         Devpart,                /* partition */
699         Devfloppy,              /* floppy drive */
700         Devswab,                /* swab data between mem and device */
701         Devmirr,                /* mirror devices */
702         MAXDEV
703 };
704
705 /*
706  * tags on block
707  */
708 /* DONT TOUCH, this is in disk structures */
709 /* also, the order from Tdir to Tmaxind is exploited in indirck() & isdirty() */
710 enum
711 {
712         Tnone           = 0,
713         Tsuper,                 /* the super block */
714 #ifdef COMPAT32
715         Tdir,                   /* directory contents */
716         Tind1,                  /* points to blocks */
717         Tind2,                  /* points to Tind1 */
718 #else
719         Tdirold,
720         Tind1old,
721         Tind2old,
722 #endif
723         Tfile,                  /* file contents; also defined in disk.h */
724         Tfree,                  /* in free list */
725         Tbuck,                  /* cache fs bucket */
726         Tvirgo,                 /* fake worm virgin bits */
727         Tcache,                 /* cw cache things */
728         Tconfig,                /* configuration block */
729 #ifndef COMPAT32
730         /* Tdir & indirect blocks are last, to allow for greater depth */
731         Tdir,                   /* directory contents */
732         Tind1,                  /* points to blocks */
733         Tind2,                  /* points to Tind1 */
734         Tind3,                  /* points to Tind2 */
735         Tind4,                  /* points to Tind3 */
736         Maxtind,
737 #endif
738         /* gap for more indirect block depth in future */
739         Tlabel = 32,            /* Devlworm label in last block */
740         MAXTAG,
741
742 #ifdef COMPAT32
743         Tmaxind = Tind2,
744 #else
745         Tmaxind = Maxtind - 1,
746 #endif
747 };
748
749 /*
750  * flags to getbuf
751  */
752 enum
753 {
754         Brd     = (1<<0),       /* read the block if miss */
755         Bprobe  = (1<<1),       /* return null if miss */
756         Bmod    = (1<<2),       /* buffer is dirty, needs writing */
757         Bimm    = (1<<3),       /* write immediately on putbuf */
758         Bres    = (1<<4),       /* reserved, never renamed */
759 };
760
761 Conf    conf;
762 Cons    cons;
763
764 #pragma varargck        type    "Z"     Device*
765 #pragma varargck        type    "T"     Timet
766 #pragma varargck        type    "I"     uchar*
767 #pragma varargck        type    "E"     uchar*
768 #pragma varargck        type    "G"     int
769
770 extern char     *annstrs[];
771 extern Biobuf   bin;
772 extern Map      *devmap;
773 extern int      (*fsprotocol[])(Msgbuf*);
774 extern int      chatty;