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);
382 * post filedescriptors to /srv
387 * Ethernet i/o processes
392 * read ahead processes
394 newproc(rahead, 0, "rah");
399 for(i=0; i < conf.nserve; i++)
400 newproc(serve, 0, "srv");
403 * worm "dump" copy process
405 newproc(wormcopy, 0, "wcp");
408 * "sync" copy process
410 newproc(synccopy, 0, "scp");
413 * processes to read the console
422 * read ahead processes.
423 * read message from q and then
427 rbcmp(void *va, void *vb)
437 if(ra->dev > rb->dev)
439 if(ra->dev < rb->dev)
441 if(ra->addr > rb->addr)
443 if(ra->addr < rb->addr)
456 rb[0] = fs_recv(raheadq, 0);
457 for(n = 1; n < nelem(rb); n++) {
458 if(raheadq->count <= 0)
460 rb[n] = fs_recv(raheadq, 0);
462 qsort(rb, n, sizeof rb[0], rbcmp);
463 for(i = 0; i < n; i++) {
466 p = getbuf(rb[i]->dev, rb[i]->addr, Brd);
470 rb[i]->link = rabuffree;
478 * main filesystem server loop.
479 * entered by many processes.
480 * they wait for message buffers and
492 /* read 9P request from a network input process */
493 mb = fs_recv(serveq, 0);
494 assert(mb->magic == Mbmagic);
495 /* fs kernel sets chan in /sys/src/fs/ip/il.c:/^getchan */
498 panic("serve: nil mb->chan");
505 panic("serve: nil mb->data");
506 /* better sniffing code in /sys/src/cmd/disk/kfs/9p12.c */
507 if(cp->protocol == nil){
508 /* do we recognise the protocol in this packet? */
509 /* better sniffing code: /sys/src/cmd/disk/kfs/9p12.c */
510 for(i = 0; fsprotocol[i] != nil; i++)
511 if(fsprotocol[i](mb) != 0) {
512 cp->protocol = fsprotocol[i];
515 if(cp->protocol == nil && (chatty > 1)){
516 print("no protocol for message\n");
517 hexdump(mb->data, 12);
520 /* process the request, generate an answer and reply */
525 runlock(&cp->reflock);
536 print("halted at %T.\n", time(nil));
537 postnote(PNGROUP, getpid(), "die");
542 DUMPTIME = 5, /* 5 am */
543 WEEKMASK = 0, /* every day (1=sun, 2=mon, 4=tue, etc.) */
547 * calculate the next dump time.
548 * minimum delay is 100 minutes.
553 Timet nddate = nextime(t+MINUTE(100), DUMPTIME, WEEKMASK);
555 if(!conf.nodump && chatty)
556 print("next dump at %T\n", nddate);
561 * process to copy dump blocks from
562 * cache to worm. it runs flat out when
563 * it gets work, but only looks for
564 * work every 10 seconds.
570 Timet dt, t = 0, nddate = 0, ntoytime = 0;
577 nddate = nextdump(t); /* chatters */
578 ntoytime = time(nil);
581 if(dt < 0 || dt > MINUTE(100)) {
588 ntoytime = time(nil) + HOUR(1);
589 else if(t > nddate) {
591 print("automatic dump %T\n", t);
592 for(fs=filsys; fs->name; fs++)
593 if(fs->dev->type == Devcw)
599 for(fs=filsys; fs->name; fs++)
600 if(fs->dev->type == Devcw)
601 f |= dumpblock(fs->dev);
611 * process to synch blocks
612 * it puts out a block/cache-line every second
613 * it waits 10 seconds if caught up.
614 * in both cases, it takes about 10 seconds
637 char *ln, *end, *data = malloc(strlen(file) + 5 + 1);
643 end = strstr(data, "/data");
645 strcat(data, "/ctl");
648 bp = Bopen(data, OREAD);
650 while (rv < 0 && (ln = Brdline(bp, '\n')) != nil) {
651 ln[Blinelen(bp)-1] = '\0';
652 nf = tokenize(ln, fields, nelem(fields));
653 if (nf == 3 && strcmp(fields[0], "geometry") == 0)
654 rv = atoi(fields[2]);