1 /* cached-worm file server */
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 fprint(2, "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 */
132 conf.nserve = 15; /* tunable */
139 conf.nwpath = conf.nfile*8;
140 conf.gidspace = conf.nuid*3;
145 mapinit(conf.devmap);
149 srvfd(char *s, int mode, int sfd)
154 fd = create(s, ORCLOSE|OWRITE, mode);
157 fd = create(s, ORCLOSE|OWRITE, mode);
161 sprint(buf, "%d", sfd);
162 if(write(fd, buf, strlen(buf)) != strlen(buf))
174 panic("no service name");
176 /* serve 9p for -s */
178 srvchan(sfd, "stdio");
182 /* post 9p service */
184 panic("can't make a pipe");
185 snprint(buf, sizeof(buf), "#s/%s", service);
186 srvfd(buf, 0666, p[0]);
190 /* post cmd service */
192 panic("can't make a pipe");
193 snprint(buf, sizeof(buf), "#s/%s.cmd", service);
194 srvfd(buf, 0220, p[0]);
197 /* use it as stdin */
203 * compute BUFSIZE*(NDBLOCK+INDPERBUF+INDPERBUF+INDPERBUF+INDPERBUF⁴)
204 * while watching for overflow; in that case, return 0.
208 adduvlongov(uvlong a, uvlong b)
218 muluvlongov(uvlong a, uvlong b)
222 if (a != 0 && r/a != b || r < a || r < b)
231 uvlong max = NDBLOCK, ind = 1;
233 for (i = 0; i < NIBLOCK; i++) {
234 ind = muluvlongov(ind, INDPERBUF); /* power of INDPERBUF */
237 max = adduvlongov(max, ind);
241 return muluvlongov(max, BUFSIZE);
245 INDPERBUF = ((uvlong)INDPERBUF *INDPERBUF),
246 INDPERBUF⁴ = ((uvlong)INDPERBUF*INDPERBUF),
252 uvlong max = maxsize();
254 print("\tblock size = %d; ", RBUFSIZE);
256 print("max file size exceeds 2⁶⁴ bytes\n");
258 uvlong offlim = 1ULL << (sizeof(Off)*8 - 1);
262 print("max file size = %,llud\n", (Wideoff)max);
264 if (INDPERBUF/INDPERBUF != INDPERBUF)
265 print("overflow computing INDPERBUF\n");
266 if (INDPERBUF⁴/INDPERBUF != INDPERBUF)
267 print("overflow computing INDPERBUF⁴\n");
268 print("\tINDPERBUF = %d, INDPERBUF^4 = %,lld, ", INDPERBUF,
269 (Wideoff)INDPERBUF⁴);
270 print("CEPERBK = %d\n", CEPERBK);
271 print("\tsizeofs: Dentry = %d, Cache = %d\n",
272 sizeof(Dentry), sizeof(Cache));
278 fprint(2, "usage: %s [ -csC ] [-n service] [ -a ann-str ] [ -m dev-map ] [-f config-dev ]\n", argv0);
283 main(int argc, char **argv)
291 conf.confdev = "/dev/sdC0/fscache";
295 case 'a': /* announce on this net */
296 ann = EARGF(usage());
297 if (nets >= Maxnets) {
298 fprint(2, "%s: too many networks to announce: %s\n",
300 exits("too many nets");
302 annstrs[nets++] = ann;
305 strcpy(service, EARGF(usage()));
311 if(open("/dev/cons", OREAD) < 0)
312 open("#c/cons", OREAD);
314 if(open("/dev/cons", OWRITE) < 0)
315 open("#c/cons", OWRITE);
317 case 'C': /* use new, faster cache layout */
323 case 'f': /* device / partition / file */
324 conf.confdev = EARGF(usage());
326 case 'm': /* name device-map file */
327 conf.devmap = EARGF(usage());
340 Binit(&bin, 0, OREAD);
344 print("\nPlan 9 %d-bit cached-worm file server with %d-deep indir blks\n",
345 sizeof(Off)*8 - 1, NIBLOCK);
351 serveq = newqueue(1000, "9P service"); /* tunable */
352 raheadq = newqueue(1000, "readahead"); /* tunable */
358 files = malloc(conf.nfile * sizeof *files);
359 for(i=0; i < conf.nfile; i++) {
364 wpaths = malloc(conf.nwpath * sizeof(*wpaths));
365 uid = malloc(conf.nuid * sizeof(*uid));
366 gidspace = malloc(conf.gidspace * sizeof(*gidspace));
371 boottime = time(nil);
377 * post filedescriptors to /srv
382 * Ethernet i/o processes
387 * read ahead processes
389 newproc(rahead, 0, "rah");
394 for(i=0; i < conf.nserve; i++)
395 newproc(serve, 0, "srv");
398 * worm "dump" copy process
400 newproc(wormcopy, 0, "wcp");
403 * "sync" copy process
405 newproc(synccopy, 0, "scp");
408 * processes to read the console
417 * read ahead processes.
418 * read message from q and then
422 rbcmp(void *va, void *vb)
432 if(ra->dev > rb->dev)
434 if(ra->dev < rb->dev)
436 if(ra->addr > rb->addr)
438 if(ra->addr < rb->addr)
451 rb[0] = fs_recv(raheadq, 0);
452 for(n = 1; n < nelem(rb); n++) {
453 if(raheadq->count <= 0)
455 rb[n] = fs_recv(raheadq, 0);
457 qsort(rb, n, sizeof rb[0], rbcmp);
458 for(i = 0; i < n; i++) {
461 p = getbuf(rb[i]->dev, rb[i]->addr, Brd);
465 rb[i]->link = rabuffree;
473 * main filesystem server loop.
474 * entered by many processes.
475 * they wait for message buffers and
487 /* read 9P request from a network input process */
488 mb = fs_recv(serveq, 0);
489 assert(mb->magic == Mbmagic);
490 /* fs kernel sets chan in /sys/src/fs/ip/il.c:/^getchan */
493 panic("serve: nil mb->chan");
500 panic("serve: nil mb->data");
501 /* better sniffing code in /sys/src/cmd/disk/kfs/9p12.c */
502 if(cp->protocol == nil){
503 /* do we recognise the protocol in this packet? */
504 /* better sniffing code: /sys/src/cmd/disk/kfs/9p12.c */
505 for(i = 0; fsprotocol[i] != nil; i++)
506 if(fsprotocol[i](mb) != 0) {
507 cp->protocol = fsprotocol[i];
510 if(cp->protocol == nil && (chatty > 1)){
511 print("no protocol for message\n");
512 hexdump(mb->data, 12);
515 /* process the request, generate an answer and reply */
520 runlock(&cp->reflock);
531 print("halted at %T.\n", time(nil));
532 postnote(PNGROUP, getpid(), "die");
537 DUMPTIME = 5, /* 5 am */
538 WEEKMASK = 0, /* every day (1=sun, 2=mon, 4=tue, etc.) */
542 * calculate the next dump time.
543 * minimum delay is 100 minutes.
548 Timet nddate = nextime(t+MINUTE(100), DUMPTIME, WEEKMASK);
550 if(!conf.nodump && chatty)
551 print("next dump at %T\n", nddate);
556 * process to copy dump blocks from
557 * cache to worm. it runs flat out when
558 * it gets work, but only looks for
559 * work every 10 seconds.
565 Timet dt, t = 0, nddate = 0, ntoytime = 0;
572 nddate = nextdump(t); /* chatters */
573 ntoytime = time(nil);
576 if(dt < 0 || dt > MINUTE(100)) {
583 ntoytime = time(nil) + HOUR(1);
584 else if(t > nddate) {
586 print("automatic dump %T\n", t);
587 for(fs=filsys; fs->name; fs++)
588 if(fs->dev->type == Devcw)
594 for(fs=filsys; fs->name; fs++)
595 if(fs->dev->type == Devcw)
596 f |= dumpblock(fs->dev);
606 * process to synch blocks
607 * it puts out a block/cache-line every second
608 * it waits 10 seconds if caught up.
609 * in both cases, it takes about 10 seconds
632 char *ln, *end, *data = malloc(strlen(file) + 5 + 1);
638 end = strstr(data, "/data");
640 strcat(data, "/ctl");
643 bp = Bopen(data, OREAD);
645 while (rv < 0 && (ln = Brdline(bp, '\n')) != nil) {
646 ln[Blinelen(bp)-1] = '\0';
647 nf = tokenize(ln, fields, nelem(fields));
648 if (nf == 3 && strcmp(fields[0], "geometry") == 0)
649 rv = atoi(fields[2]);