1 /* cached-worm file server */
26 n = vseprint(buf, buf + sizeof buf, fmt, arg) - buf;
29 fprint(2, "panic: %s\n", buf);
38 print("okay to %s? ", quest);
39 if ((ln = Brdline(&bin, '\n')) == nil)
41 ln[Blinelen(&bin)-1] = '\0';
42 if (isascii(*ln) && isupper(*ln))
48 mapinit(char *mapfile)
58 bp = Bopen(mapfile, OREAD);
60 sysfatal("can't read %s", mapfile);
62 while((ln = Brdline(bp, '\n')) != nil) {
63 ln[Blinelen(bp)-1] = '\0';
64 if(*ln == '\0' || *ln == '#')
66 nf = tokenize(ln, fields, nelem(fields));
69 if(testconfig(fields[0]) != 0) {
70 print("bad `from' device %s in %s\n",
74 map = malloc(sizeof *map);
75 map->from = strdup(fields[0]);
76 map->to = strdup(fields[1]);
77 map->fdev = iconfig(fields[0]);
79 if(access(map->to, AEXIST) < 0) {
81 * map->to isn't an existing file, so it had better be
82 * a config string for a device.
84 if(testconfig(fields[1]) == 0)
85 map->tdev = iconfig(fields[1]);
87 /* else map->to is the replacement file name */
100 conf.nserve = 15; /* tunable */
107 conf.nwpath = conf.nfile*8;
108 conf.gidspace = conf.nuid*3;
113 mapinit(conf.devmap);
117 srvfd(char *s, int mode, int sfd)
122 fd = create(s, ORCLOSE|OWRITE, mode);
125 fd = create(s, ORCLOSE|OWRITE, mode);
129 sprint(buf, "%d", sfd);
130 if(write(fd, buf, strlen(buf)) != strlen(buf))
142 panic("no service name");
144 /* serve 9p for -s */
146 srvchan(sfd, "stdio");
150 /* post 9p service */
152 panic("can't make a pipe");
153 snprint(buf, sizeof(buf), "#s/%s", service);
154 srvfd(buf, 0666, p[0]);
158 /* post cmd service */
160 panic("can't make a pipe");
161 snprint(buf, sizeof(buf), "#s/%s.cmd", service);
162 srvfd(buf, 0660, p[0]);
165 /* use it as stdin */
171 * compute BUFSIZE*(NDBLOCK+INDPERBUF+INDPERBUF²+INDPERBUF³+INDPERBUF⁴)
172 * while watching for overflow; in that case, return 0.
176 adduvlongov(uvlong a, uvlong b)
186 muluvlongov(uvlong a, uvlong b)
190 if (a != 0 && r/a != b || r < a || r < b)
199 uvlong max = NDBLOCK, ind = 1;
201 for (i = 0; i < NIBLOCK; i++) {
202 ind = muluvlongov(ind, INDPERBUF); /* power of INDPERBUF */
205 max = adduvlongov(max, ind);
209 return muluvlongov(max, BUFSIZE);
213 INDPERBUF² = ((uvlong)INDPERBUF*INDPERBUF),
214 INDPERBUF⁴ = ((uvlong)INDPERBUF²*INDPERBUF²),
220 uvlong max = maxsize();
222 print("\tblock size = %d; ", RBUFSIZE);
224 print("max file size exceeds 2⁶⁴ bytes\n");
226 uvlong offlim = 1ULL << (sizeof(Off)*8 - 1);
230 print("max file size = %,llud\n", (Wideoff)max);
232 if (INDPERBUF²/INDPERBUF != INDPERBUF)
233 print("overflow computing INDPERBUF²\n");
234 if (INDPERBUF⁴/INDPERBUF² != INDPERBUF²)
235 print("overflow computing INDPERBUF⁴\n");
236 print("\tINDPERBUF = %d, INDPERBUF^4 = %,lld, ", INDPERBUF,
237 (Wideoff)INDPERBUF⁴);
238 print("CEPERBK = %d\n", CEPERBK);
239 print("\tsizeofs: Dentry = %d, Cache = %d\n",
240 sizeof(Dentry), sizeof(Cache));
246 fprint(2, "usage: %s [ -csC ] [-n service] [ -a ann-str ] [ -m dev-map ] [-f config-dev ]\n", argv0);
251 main(int argc, char **argv)
259 conf.confdev = "/dev/sdC0/fscache";
263 case 'a': /* announce on this net */
264 ann = EARGF(usage());
265 if (nets >= Maxnets) {
266 fprint(2, "%s: too many networks to announce: %s\n",
268 exits("too many nets");
270 annstrs[nets++] = ann;
273 strcpy(service, EARGF(usage()));
279 if(open("/dev/cons", OREAD) < 0)
280 open("#c/cons", OREAD);
282 if(open("/dev/cons", OWRITE) < 0)
283 open("#c/cons", OWRITE);
285 case 'C': /* use new, faster cache layout */
291 case 'f': /* device / partition / file */
292 conf.confdev = EARGF(usage());
294 case 'm': /* name device-map file */
295 conf.devmap = EARGF(usage());
308 Binit(&bin, 0, OREAD);
312 print("\nPlan 9 %d-bit cached-worm file server with %d-deep indir blks\n",
313 sizeof(Off)*8 - 1, NIBLOCK);
317 serveq = newqueue(1000, "9P service"); /* tunable */
318 raheadq = newqueue(1000, "readahead"); /* tunable */
324 files = malloc(conf.nfile * sizeof *files);
325 for(i=0; i < conf.nfile; i++) {
330 wpaths = malloc(conf.nwpath * sizeof(*wpaths));
331 uid = malloc(conf.nuid * sizeof(*uid));
332 gidspace = malloc(conf.gidspace * sizeof(*gidspace));
337 boottime = time(nil);
343 * post filedescriptors to /srv
348 * processes to read the console
353 * Ethernet i/o processes
358 * read ahead processes
360 newproc(rahead, 0, "rah");
365 for(i=0; i < conf.nserve; i++)
366 newproc(serve, 0, "srv");
369 * worm "dump" copy process
371 newproc(wormcopy, 0, "wcp");
374 * "sync" copy process
376 newproc(synccopy, 0, "scp");
383 * read ahead processes.
384 * read message from q and then
388 rbcmp(void *va, void *vb)
398 if(ra->dev > rb->dev)
400 if(ra->dev < rb->dev)
402 if(ra->addr > rb->addr)
404 if(ra->addr < rb->addr)
417 rb[0] = fs_recv(raheadq, 0);
418 for(n = 1; n < nelem(rb); n++) {
419 if(raheadq->count <= 0)
421 rb[n] = fs_recv(raheadq, 0);
423 qsort(rb, n, sizeof rb[0], rbcmp);
424 for(i = 0; i < n; i++) {
427 p = getbuf(rb[i]->dev, rb[i]->addr, Brd);
431 rb[i]->link = rabuffree;
439 * main filesystem server loop.
440 * entered by many processes.
441 * they wait for message buffers and
452 /* read 9P request from a network input process */
453 mb = fs_recv(serveq, 0);
454 assert(mb->magic == Mbmagic);
455 /* fs kernel sets chan in /sys/src/fs/ip/il.c:/^getchan */
458 panic("serve: nil mb->chan");
464 panic("serve: nil mb->data");
465 /* better sniffing code in /sys/src/cmd/disk/kfs/9p12.c */
466 if(cp->protocol == nil){
467 /* do we recognise the protocol in this packet? */
468 /* better sniffing code: /sys/src/cmd/disk/kfs/9p12.c */
469 for(i = 0; fsprotocol[i] != nil; i++)
470 if(fsprotocol[i](mb) != 0) {
471 cp->protocol = fsprotocol[i];
474 if(cp->protocol == nil && (chatty > 1)){
475 fprint(2, "no protocol for message\n");
476 hexdump(mb->data, 12);
479 /* process the request, generate an answer and reply */
484 runlock(&cp->reflock);
495 fprint(2, "halted at %T.\n", time(nil));
496 postnote(PNGROUP, getpid(), "die");
501 DUMPTIME = 5, /* 5 am */
502 WEEKMASK = 0, /* every day (1=sun, 2=mon, 4=tue, etc.) */
506 * calculate the next dump time.
507 * minimum delay is 100 minutes.
512 Timet nddate = nextime(t+MINUTE(100), DUMPTIME, WEEKMASK);
514 if(!conf.nodump && chatty)
515 fprint(2, "next dump at %T\n", nddate);
520 * process to copy dump blocks from
521 * cache to worm. it runs flat out when
522 * it gets work, but only looks for
523 * work every 10 seconds.
529 Timet dt, t = 0, nddate = 0, ntoytime = 0;
536 nddate = nextdump(t); /* chatters */
537 ntoytime = time(nil);
540 if(dt < 0 || dt > MINUTE(100)) {
547 ntoytime = time(nil) + HOUR(1);
548 else if(t > nddate) {
550 fprint(2, "automatic dump %T\n", t);
551 for(fs=filsys; fs->name; fs++)
552 if(fs->dev->type == Devcw)
558 for(fs=filsys; fs->name; fs++)
559 if(fs->dev->type == Devcw)
560 f |= dumpblock(fs->dev);
570 * process to synch blocks
571 * it puts out a block/cache-line every second
572 * it waits 10 seconds if caught up.
573 * in both cases, it takes about 10 seconds
596 char *ln, *end, *data = malloc(strlen(file) + 5 + 1);
602 end = strstr(data, "/data");
604 strcat(data, "/ctl");
607 bp = Bopen(data, OREAD);
609 while (rv < 0 && (ln = Brdline(bp, '\n')) != nil) {
610 ln[Blinelen(bp)-1] = '\0';
611 nf = tokenize(ln, fields, nelem(fields));
612 if (nf == 3 && strcmp(fields[0], "geometry") == 0)
613 rv = atoi(fields[2]);