portdat.h 15 KB

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