main.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  1. #include "all.h"
  2. int sfd;
  3. int cmdmode = 0660;
  4. int rfd;
  5. int chat;
  6. extern char *wrenfile;
  7. extern int nwren;
  8. char *myname;
  9. int cmdfd;
  10. int writeallow; /* never on; for compatibility with fs */
  11. int wstatallow;
  12. int writegroup;
  13. int allownone;
  14. int noatime;
  15. int srvfd(char*, int, int);
  16. void usage(void);
  17. void confinit(void);
  18. Chan *chaninit(char*);
  19. void consinit(void);
  20. void forkserve(void);
  21. void
  22. main(int argc, char *argv[])
  23. {
  24. Filsys *fs;
  25. int ream, fsok;
  26. int newbufsize, nocheck;
  27. char buf[NAMELEN];
  28. int pid, ctl;
  29. progname = "kfs";
  30. procname = "init";
  31. /*
  32. * insulate from invoker's environment and keep it from swapping
  33. */
  34. rfork(RFNAMEG|RFNOTEG|RFREND);
  35. confinit();
  36. sfd = -1;
  37. ream = 0;
  38. newbufsize = 0;
  39. nocheck = 0;
  40. wrenfile = "/dev/sdC0/fs";
  41. pid = getpid();
  42. snprint(buf, sizeof buf, "/proc/%d/ctl", pid);
  43. ctl = open(buf, OWRITE);
  44. fprint(ctl, "noswap\n");
  45. close(ctl);
  46. buf[0] = '\0';
  47. ARGBEGIN{
  48. case 'b':
  49. newbufsize = atol(ARGF());
  50. break;
  51. case 'c':
  52. nocheck = 1;
  53. break;
  54. case 'f':
  55. wrenfile = ARGF();
  56. break;
  57. case 'm':
  58. nwren = atol(ARGF());
  59. break;
  60. case 'n':
  61. strncpy(buf, ARGF(), NAMELEN-1);
  62. buf[NAMELEN-1] = '\0';
  63. break;
  64. case 'p':
  65. cmdmode = atol(ARGF());
  66. break;
  67. case 'r':
  68. ream = 1;
  69. break;
  70. case 's':
  71. sfd = 0;
  72. rfd = dup(1, -1);
  73. close(1);
  74. if(open("/dev/cons", OWRITE) < 0)
  75. open("#c/cons", OWRITE);
  76. break;
  77. case 'B':
  78. conf.niobuf = strtoul(ARGF(), 0, 0);
  79. break;
  80. case 'C':
  81. chat = 1;
  82. break;
  83. default:
  84. usage();
  85. }ARGEND
  86. if(argc != 0)
  87. usage();
  88. cmdfd = 2;
  89. if (access(wrenfile, AREAD|AWRITE) == -1)
  90. sysfatal("%s cannot access device\n", wrenfile);
  91. formatinit();
  92. sublockinit();
  93. if(buf[0])
  94. sprint(service, "kfs.%s", buf);
  95. else
  96. strcpy(service, "kfs");
  97. chan = chaninit(service);
  98. consinit();
  99. tlocks = ialloc(NTLOCK * sizeof *tlocks);
  100. uid = ialloc(conf.nuid * sizeof(*uid));
  101. uidspace = ialloc(conf.uidspace * sizeof(*uidspace));
  102. gidspace = ialloc(conf.gidspace * sizeof(*gidspace));
  103. /*
  104. * init global locks
  105. */
  106. wlock(&mainlock); wunlock(&mainlock);
  107. /*
  108. * init the file system, ream it if needed, and get the block sizes
  109. */
  110. ream = fsinit(ream, newbufsize);
  111. iobufinit();
  112. for(fs=filesys; fs->name; fs++)
  113. if(fs->flags & FREAM){ /* set by fsinit if reamed */
  114. ream++;
  115. rootream(fs->dev, getraddr(fs->dev));
  116. superream(fs->dev, superaddr(fs->dev));
  117. }
  118. boottime = time(nil);
  119. consserve();
  120. fsok = superok(filesys[0].dev, superaddr(filesys[0].dev), 0);
  121. if(!nocheck && !ream && !fsok)
  122. cmd_exec("check fq");
  123. startproc(forkserve, "srv");
  124. startproc(syncproc, "sync");
  125. exits(0);
  126. }
  127. void
  128. forkserve(void)
  129. {
  130. serve(chan);
  131. }
  132. static
  133. struct
  134. {
  135. int nfilter;
  136. Filter* filters[100];
  137. }f;
  138. int alarmed;
  139. void
  140. catchalarm(void *regs, char *msg)
  141. {
  142. USED(regs, msg);
  143. if(strcmp(msg, "alarm") == 0){
  144. alarmed = 1;
  145. noted(NCONT);
  146. } else
  147. noted(NDFLT);
  148. }
  149. /*
  150. * process to synch blocks
  151. * it puts out a block/line every second
  152. * it waits 10 seconds if catches up.
  153. * in both cases, it takes about 10 seconds
  154. * to get up-to-date.
  155. *
  156. * it also updates the filter stats
  157. * and executes commands
  158. */
  159. void
  160. syncproc(void)
  161. {
  162. char buf[4*1024];
  163. Filter *ft;
  164. ulong c0, c1;
  165. long t, n, d;
  166. int i, p[2];
  167. /*
  168. * make a pipe for commands
  169. */
  170. if(pipe(p) < 0)
  171. panic("command pipe");
  172. sprint(buf, "#s/%s.cmd", service);
  173. srvfd(buf, cmdmode, p[0]);
  174. close(p[0]);
  175. cmdfd = p[1];
  176. notify(catchalarm);
  177. t = time(nil);
  178. for(;;){
  179. i = syncblock();
  180. alarmed = 0;
  181. alarm(i ? 1000: 10000);
  182. n = read(cmdfd, buf, sizeof buf - 1);
  183. if(n <= 0 && !alarmed)
  184. sleep(i ? 1000: 10000);
  185. alarm(0);
  186. if(n > 0){
  187. buf[n] = '\0';
  188. if(cmd_exec(buf))
  189. fprint(cmdfd, "done");
  190. else
  191. fprint(cmdfd, "unknown command");
  192. }
  193. n = time(nil);
  194. d = n - t;
  195. if(d < 0 || d > 5*60)
  196. d = 0;
  197. while(d >= 1) {
  198. d -= 1;
  199. for(i=0; i<f.nfilter; i++) {
  200. ft = f.filters[i];
  201. c0 = ft->count;
  202. c1 = c0 - ft->oldcount;
  203. ft->oldcount = c0;
  204. ft->filter[0] = famd(ft->filter[0], c1, 59, 60);
  205. ft->filter[1] = famd(ft->filter[1], c1, 599, 600);
  206. ft->filter[2] = famd(ft->filter[2], c1, 5999, 6000);
  207. }
  208. }
  209. t = n;
  210. }
  211. }
  212. void
  213. dofilter(Filter *ft)
  214. {
  215. int i;
  216. i = f.nfilter;
  217. if(i >= sizeof f.filters / sizeof f.filters[0]) {
  218. print("dofilter: too many filters\n");
  219. return;
  220. }
  221. f.filters[i] = ft;
  222. f.nfilter = i+1;
  223. }
  224. void
  225. startproc(void (*f)(void), char *name)
  226. {
  227. switch(rfork(RFMEM|RFFDG|RFPROC)){
  228. case -1:
  229. panic("can't fork");
  230. case 0:
  231. break;
  232. default:
  233. return;
  234. }
  235. procname = name;
  236. f();
  237. _exits(nil);
  238. }
  239. void
  240. confinit(void)
  241. {
  242. conf.niobuf = 0;
  243. conf.nuid = 600;
  244. conf.nserve = 2;
  245. conf.uidspace = conf.nuid*6;
  246. conf.gidspace = conf.nuid*3;
  247. cons.flags = 0;
  248. }
  249. static void
  250. dochaninit(Chan *cp, int fd)
  251. {
  252. cp->chan = fd;
  253. fileinit(cp);
  254. wlock(&cp->reflock);
  255. wunlock(&cp->reflock);
  256. lock(&cp->flock);
  257. unlock(&cp->flock);
  258. }
  259. Chan*
  260. chaninit(char *server)
  261. {
  262. Chan *cp;
  263. char buf[3*NAMELEN];
  264. int p[2];
  265. sprint(buf, "#s/%s", server);
  266. if(sfd < 0){
  267. if(pipe(p) < 0)
  268. panic("can't make a pipe");
  269. sfd = p[0];
  270. rfd = p[1];
  271. }
  272. srvfd(buf, 0666, sfd);
  273. close(sfd);
  274. cp = ialloc(sizeof *cp);
  275. cons.srvchan = cp;
  276. dochaninit(cp, rfd);
  277. return cp;
  278. }
  279. int
  280. netserve(char *netaddr)
  281. {
  282. int afd, lfd, fd;
  283. char adir[2*NAMELEN], ldir[2*NAMELEN];
  284. Chan *netchan;
  285. if(access("/net/il/clone", 0) < 0)
  286. bind("#I", "/net", MAFTER);
  287. if(access("/net.alt/il/clone", 0) < 0)
  288. bind("#I1", "/net.alt", MAFTER);
  289. afd = announce(netaddr, adir);
  290. if (afd < 0)
  291. return -1;
  292. switch (rfork(RFMEM|RFFDG|RFPROC)) {
  293. case -1:
  294. return -1;
  295. case 0:
  296. break;
  297. default:
  298. return 0;
  299. }
  300. for (;;) {
  301. lfd = listen(adir, ldir);
  302. if (lfd < 0)
  303. continue;
  304. fd = accept(lfd, ldir);
  305. if (fd < 0) {
  306. close(lfd);
  307. continue;
  308. }
  309. netchan = mallocz(sizeof(Chan), 1);
  310. if(netchan == nil)
  311. panic("out of memory");
  312. dochaninit(netchan, fd);
  313. switch (rfork(RFMEM|RFFDG|RFPROC)) {
  314. case -1:
  315. panic("can't fork");
  316. case 0:
  317. close(afd);
  318. close(lfd);
  319. serve(netchan);
  320. free(netchan);
  321. exits(0);
  322. default:
  323. close(fd);
  324. close(lfd);
  325. continue;
  326. }
  327. }
  328. }
  329. int
  330. srvfd(char *s, int mode, int sfd)
  331. {
  332. int fd;
  333. char buf[32];
  334. fd = create(s, ORCLOSE|OWRITE, mode);
  335. if(fd < 0){
  336. remove(s);
  337. fd = create(s, ORCLOSE|OWRITE, mode);
  338. if(fd < 0)
  339. panic(s);
  340. }
  341. sprint(buf, "%d", sfd);
  342. if(write(fd, buf, strlen(buf)) != strlen(buf))
  343. panic("srv write");
  344. return sfd;
  345. }
  346. void
  347. consinit(void)
  348. {
  349. int i;
  350. cons.chan = ialloc(sizeof(Chan));
  351. wlock(&cons.chan->reflock);
  352. wunlock(&cons.chan->reflock);
  353. lock(&cons.chan->flock);
  354. unlock(&cons.chan->flock);
  355. dofilter(&cons.work);
  356. dofilter(&cons.rate);
  357. dofilter(&cons.bhit);
  358. dofilter(&cons.bread);
  359. dofilter(&cons.binit);
  360. for(i = 0; i < MAXTAG; i++)
  361. dofilter(&cons.tags[i]);
  362. }
  363. /*
  364. * always called with mainlock locked
  365. */
  366. void
  367. syncall(void)
  368. {
  369. for(;;)
  370. if(!syncblock())
  371. return;
  372. }
  373. int
  374. askream(Filsys *fs)
  375. {
  376. char c;
  377. print("File system %s inconsistent\n", fs->name);
  378. print("Would you like to ream it (y/n)? ");
  379. read(0, &c, 1);
  380. return c == 'y';
  381. }
  382. ulong
  383. memsize(void)
  384. {
  385. char *p, buf[128];
  386. int fd, n, by2pg, secs;
  387. by2pg = 4*1024;
  388. p = getenv("cputype");
  389. if(p && strcmp(p, "68020") == 0)
  390. by2pg = 8*1024;
  391. secs = 4*1024*1024;
  392. fd = open("/dev/swap", OREAD);
  393. if(fd < 0)
  394. return secs;
  395. n = read(fd, buf, sizeof(buf)-1);
  396. close(fd);
  397. if(n <= 0)
  398. return secs;
  399. buf[n] = 0;
  400. p = strchr(buf, '/');
  401. if(p)
  402. secs = strtoul(p+1, 0, 0)*by2pg;
  403. return secs;
  404. }
  405. /*
  406. * init the devices
  407. * wipe some of the file systems, or all if ream is set
  408. * this code really assumes that only one file system exists
  409. */
  410. int
  411. fsinit(int ream, int newbufsize)
  412. {
  413. Filsys *fs;
  414. RBUFSIZE = 4 * 1024;
  415. for(fs=filesys; fs->name; fs++)
  416. (*devcall[fs->dev.type].init)(fs->dev);
  417. if(newbufsize == 0)
  418. newbufsize = RBUFSIZE;
  419. if(conf.niobuf == 0) {
  420. conf.niobuf = memsize()/10;
  421. if(conf.niobuf > 2*1024*1024)
  422. conf.niobuf = 2*1024*1024;
  423. conf.niobuf /= newbufsize;
  424. if(conf.niobuf < 30)
  425. conf.niobuf = 30;
  426. }
  427. BUFSIZE = RBUFSIZE - sizeof(Tag);
  428. for(fs=filesys; fs->name; fs++)
  429. if(ream || (*devcall[fs->dev.type].check)(fs->dev) && askream(fs)){
  430. RBUFSIZE = newbufsize;
  431. BUFSIZE = RBUFSIZE - sizeof(Tag);
  432. (*devcall[fs->dev.type].ream)(fs->dev);
  433. fs->flags |= FREAM;
  434. ream = 1;
  435. }
  436. /*
  437. * set up the block size dependant variables
  438. */
  439. BUFSIZE = RBUFSIZE - sizeof(Tag);
  440. DIRPERBUF = BUFSIZE / sizeof(Dentry);
  441. INDPERBUF = BUFSIZE / sizeof(long);
  442. INDPERBUF2 = INDPERBUF * INDPERBUF;
  443. FEPERBUF = (BUFSIZE - sizeof(Super1) - sizeof(long)) / sizeof(long);
  444. return ream;
  445. }
  446. /*
  447. * allocate rest of mem
  448. * for io buffers.
  449. */
  450. #define HWIDTH 5 /* buffers per hash */
  451. void
  452. iobufinit(void)
  453. {
  454. long i;
  455. Iobuf *p, *q;
  456. Hiob *hp;
  457. i = conf.niobuf*RBUFSIZE;
  458. niob = i / (sizeof(Iobuf) + RBUFSIZE + sizeof(Hiob)/HWIDTH);
  459. nhiob = niob / HWIDTH;
  460. while(!prime(nhiob))
  461. nhiob++;
  462. if(chat)
  463. print(" %ld buffers; %ld hashes\n", niob, nhiob);
  464. hiob = ialloc(nhiob * sizeof(Hiob));
  465. hp = hiob;
  466. for(i=0; i<nhiob; i++) {
  467. lock(hp);
  468. unlock(hp);
  469. hp++;
  470. }
  471. p = ialloc(niob * sizeof(Iobuf));
  472. hp = hiob;
  473. for(i=0; i<niob; i++) {
  474. qlock(p);
  475. qunlock(p);
  476. if(hp == hiob)
  477. hp = hiob + nhiob;
  478. hp--;
  479. q = hp->link;
  480. if(q) {
  481. p->fore = q;
  482. p->back = q->back;
  483. q->back = p;
  484. p->back->fore = p;
  485. } else {
  486. hp->link = p;
  487. p->fore = p;
  488. p->back = p;
  489. }
  490. p->dev = devnone;
  491. p->addr = -1;
  492. p->xiobuf = ialloc(RBUFSIZE);
  493. p->iobuf = (char*)-1;
  494. p++;
  495. }
  496. }
  497. void
  498. usage(void)
  499. {
  500. fprint(2, "usage: kfs [-cCr] [-b bufsize] [-s infd outfd] [-f fsfile]\n");
  501. exits(0);
  502. }