1 /* cached-worm file server */
6 extern int oldcachefmt;
19 * Put a string on the console.
33 * Print a string on the console.
36 putstrn(char *str, int n)
42 * get a character from the console
47 return Bgetrune(&bin);
58 n = vseprint(buf, buf + sizeof buf, fmt, arg) - buf;
61 print("panic: %s\n", buf);
70 print("okay to %s? ", quest);
71 if ((ln = Brdline(&bin, '\n')) == nil)
73 ln[Blinelen(&bin)-1] = '\0';
74 if (isascii(*ln) && isupper(*ln))
80 mapinit(char *mapfile)
90 bp = Bopen(mapfile, OREAD);
92 sysfatal("can't read %s", mapfile);
94 while ((ln = Brdline(bp, '\n')) != nil) {
95 ln[Blinelen(bp)-1] = '\0';
96 if (*ln == '\0' || *ln == '#')
98 nf = tokenize(ln, fields, nelem(fields));
101 if(testconfig(fields[0]) != 0) {
102 print("bad `from' device %s in %s\n",
106 map = malloc(sizeof *map);
107 map->from = strdup(fields[0]);
108 map->to = strdup(fields[1]);
109 map->fdev = iconfig(fields[0]);
111 if (access(map->to, AEXIST) < 0) {
113 * map->to isn't an existing file, so it had better be
114 * a config string for a device.
116 if(testconfig(fields[1]) == 0)
117 map->tdev = iconfig(fields[1]);
119 /* else map->to is the replacement file name */
131 conf.mem = meminit();
134 conf.nserve = 15; /* tunable */
141 conf.nwpath = conf.nfile*8;
142 conf.nauth = conf.nfile/10;
143 conf.gidspace = conf.nuid*3;
148 mapinit(conf.devmap);
152 * compute BUFSIZE*(NDBLOCK+INDPERBUF+INDPERBUF+INDPERBUF+INDPERBUF⁴)
153 * while watching for overflow; in that case, return 0.
157 adduvlongov(uvlong a, uvlong b)
167 muluvlongov(uvlong a, uvlong b)
171 if (a != 0 && r/a != b || r < a || r < b)
180 uvlong max = NDBLOCK, ind = 1;
182 for (i = 0; i < NIBLOCK; i++) {
183 ind = muluvlongov(ind, INDPERBUF); /* power of INDPERBUF */
186 max = adduvlongov(max, ind);
190 return muluvlongov(max, BUFSIZE);
194 INDPERBUF = ((uvlong)INDPERBUF *INDPERBUF),
195 INDPERBUF⁴ = ((uvlong)INDPERBUF*INDPERBUF),
201 uvlong max = maxsize();
203 print("\tblock size = %d; ", RBUFSIZE);
205 print("max file size exceeds 2⁶⁴ bytes\n");
207 uvlong offlim = 1ULL << (sizeof(Off)*8 - 1);
211 print("max file size = %,llud\n", (Wideoff)max);
213 if (INDPERBUF/INDPERBUF != INDPERBUF)
214 print("overflow computing INDPERBUF\n");
215 if (INDPERBUF⁴/INDPERBUF != INDPERBUF)
216 print("overflow computing INDPERBUF⁴\n");
217 print("\tINDPERBUF = %d, INDPERBUF^4 = %,lld, ", INDPERBUF,
218 (Wideoff)INDPERBUF⁴);
219 print("CEPERBK = %d\n", CEPERBK);
220 print("\tsizeofs: Dentry = %d, Cache = %d\n",
221 sizeof(Dentry), sizeof(Cache));
227 fprint(2, "usage: %s [-cf][-a ann-str][-m dev-map] config-dev\n",
233 main(int argc, char **argv)
241 conf.confdev = "n"; /* Devnone */
244 case 'a': /* announce on this net */
245 ann = EARGF(usage());
246 if (nets >= Maxnets) {
247 fprint(2, "%s: too many networks to announce: %s\n",
249 exits("too many nets");
251 annstrs[nets++] = ann;
253 case 'c': /* use new, faster cache layout */
256 case 'f': /* enter configuration mode first */
259 case 'm': /* name device-map file */
260 conf.devmap = EARGF(usage());
269 conf.confdev = argv[0]; /* config string for dev holding full config */
271 Binit(&bin, 0, OREAD);
274 print("\nPlan 9 %d-bit cached-worm file server with %d-deep indir blks\n",
275 sizeof(Off)*8 - 1, NIBLOCK);
280 serveq = newqueue(1000, "9P service"); /* tunable */
281 raheadq = newqueue(1000, "readahead"); /* tunable */
287 files = malloc(conf.nfile * sizeof *files);
288 for(i=0; i < conf.nfile; i++) {
293 wpaths = malloc(conf.nwpath * sizeof(*wpaths));
294 uid = malloc(conf.nuid * sizeof(*uid));
295 gidspace = malloc(conf.gidspace * sizeof(*gidspace));
298 print("iobufinit\n");
302 boottime = time(nil);
308 * Ethernet i/o processes
313 * read ahead processes
315 newproc(rahead, 0, "rah");
320 for(i=0; i < conf.nserve; i++)
321 newproc(serve, 0, "srv");
324 * worm "dump" copy process
326 newproc(wormcopy, 0, "wcp");
329 * processes to read the console
334 * "sync" copy process
335 * this doesn't return.
342 * read ahead processes.
343 * read message from q and then
347 rbcmp(void *va, void *vb)
357 if(ra->dev > rb->dev)
359 if(ra->dev < rb->dev)
361 if(ra->addr > rb->addr)
363 if(ra->addr < rb->addr)
376 rb[0] = fs_recv(raheadq, 0);
377 for(n = 1; n < nelem(rb); n++) {
378 if(raheadq->count <= 0)
380 rb[n] = fs_recv(raheadq, 0);
382 qsort(rb, n, sizeof rb[0], rbcmp);
383 for(i = 0; i < n; i++) {
386 p = getbuf(rb[i]->dev, rb[i]->addr, Brd);
390 rb[i]->link = rabuffree;
398 * main filesystem server loop.
399 * entered by many processes.
400 * they wait for message buffers and
412 /* read 9P request from a network input process */
413 mb = fs_recv(serveq, 0);
414 assert(mb->magic == Mbmagic);
415 /* fs kernel sets chan in /sys/src/fs/ip/il.c:/^getchan */
418 panic("serve: nil mb->chan");
425 panic("serve: nil mb->data");
426 /* better sniffing code in /sys/src/cmd/disk/kfs/9p12.c */
427 if(cp->protocol == nil){
428 /* do we recognise the protocol in this packet? */
429 /* better sniffing code: /sys/src/cmd/disk/kfs/9p12.c */
430 for(i = 0; fsprotocol[i] != nil; i++)
431 if(fsprotocol[i](mb) != 0) {
432 cp->protocol = fsprotocol[i];
435 if(cp->protocol == nil){
436 print("no protocol for message\n");
437 for(i = 0; i < 12; i++)
438 print(" %2.2uX", mb->data[i]);
442 /* process the request, generate an answer and reply */
447 runlock(&cp->reflock);
458 print("halted at %T.\n", time(nil));
459 postnote(PNGROUP, getpid(), "die");
464 DUMPTIME = 5, /* 5 am */
465 WEEKMASK = 0, /* every day (1=sun, 2=mon, 4=tue, etc.) */
469 * calculate the next dump time.
470 * minimum delay is 100 minutes.
475 Timet nddate = nextime(t+MINUTE(100), DUMPTIME, WEEKMASK);
478 print("next dump at %T\n", nddate);
483 * process to copy dump blocks from
484 * cache to worm. it runs flat out when
485 * it gets work, but only looks for
486 * work every 10 seconds.
492 Timet dt, t = 0, nddate = 0, ntoytime = 0;
499 nddate = nextdump(t); /* chatters */
500 ntoytime = time(nil);
503 if(dt < 0 || dt > MINUTE(100)) {
505 print("time went back\n");
507 print("time jumped ahead\n");
514 ntoytime = time(nil) + HOUR(1);
515 else if(t > nddate) {
517 print("automatic dump %T\n", t);
518 for(fs=filsys; fs->name; fs++)
519 if(fs->dev->type == Devcw)
525 for(fs=filsys; fs->name; fs++)
526 if(fs->dev->type == Devcw)
527 f |= dumpblock(fs->dev);
537 * process to synch blocks
538 * it puts out a block/cache-line every second
539 * it waits 10 seconds if caught up.
540 * in both cases, it takes about 10 seconds
563 char *ln, *end, *data = malloc(strlen(file) + 5 + 1);
569 end = strstr(data, "/data");
571 strcat(data, "/ctl");
574 bp = Bopen(data, OREAD);
576 while (rv < 0 && (ln = Brdline(bp, '\n')) != nil) {
577 ln[Blinelen(bp)-1] = '\0';
578 nf = tokenize(ln, fields, nelem(fields));
579 if (nf == 3 && strcmp(fields[0], "geometry") == 0)
580 rv = atoi(fields[2]);