1
0

config.c 20 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include "all.h"
  10. #include "io.h"
  11. static void dowormcopy(void);
  12. static int dodevcopy(void);
  13. struct {
  14. char* icharp;
  15. char* charp;
  16. int error;
  17. int newconf; /* clear before start */
  18. int modconf; /* write back when done */
  19. int nextiter;
  20. int lastiter;
  21. int diriter;
  22. Device* lastcw;
  23. Device* devlist;
  24. } f;
  25. static Device* confdev;
  26. static int copyworm = 0, copydev = 0;
  27. static char *src, *dest;
  28. static int resetparams;
  29. Fspar fspar[] = {
  30. { "blocksize", RBUFSIZE, },
  31. { "daddrbits", sizeof(Off)*8, },
  32. { "indirblks", NIBLOCK, },
  33. { "dirblks", NDBLOCK, },
  34. { "namelen", NAMELEN, },
  35. { nil, 0, },
  36. };
  37. int
  38. devcmpr(Device *d1, Device *d2)
  39. {
  40. while (d1 != d2) {
  41. if(d1 == nil || d2 == nil || d1->type != d2->type)
  42. return 1;
  43. switch(d1->type) {
  44. default:
  45. print("can't compare dev: %Z\n", d1);
  46. panic("devcmp");
  47. return 1;
  48. case Devmcat:
  49. case Devmlev:
  50. case Devmirr:
  51. d1 = d1->cat.first;
  52. d2 = d2->cat.first;
  53. while(d1 && d2) {
  54. if(devcmpr(d1, d2))
  55. return 1;
  56. d1 = d1->link;
  57. d2 = d2->link;
  58. }
  59. break;
  60. case Devnone:
  61. return 0;
  62. case Devro:
  63. d1 = d1->ro.parent;
  64. d2 = d2->ro.parent;
  65. break;
  66. case Devjuke:
  67. case Devcw:
  68. if(devcmpr(d1->cw.c, d2->cw.c))
  69. return 1;
  70. d1 = d1->cw.w;
  71. d2 = d2->cw.w;
  72. break;
  73. case Devfworm:
  74. d1 = d1->fw.fw;
  75. d2 = d2->fw.fw;
  76. break;
  77. case Devwren:
  78. case Devworm:
  79. case Devlworm:
  80. if(d1->wren.ctrl == d2->wren.ctrl)
  81. if(d1->wren.targ == d2->wren.targ)
  82. if(d1->wren.lun == d2->wren.lun)
  83. return 0;
  84. return 1;
  85. case Devpart:
  86. if(d1->part.base == d2->part.base)
  87. if(d1->part.size == d2->part.size) {
  88. d1 = d1->part.d;
  89. d2 = d2->part.d;
  90. break;
  91. }
  92. return 1;
  93. }
  94. }
  95. return 0;
  96. }
  97. void
  98. cdiag(char *s, int c1)
  99. {
  100. f.charp--;
  101. if(f.error == 0) {
  102. print("config diag: %s -- <%c>\n", s, c1);
  103. f.error = 1;
  104. }
  105. }
  106. int
  107. cnumb(void)
  108. {
  109. int c, n;
  110. c = *f.charp++;
  111. if(c == '<') {
  112. n = f.nextiter;
  113. if(n >= 0) {
  114. f.nextiter = n+f.diriter;
  115. if(n == f.lastiter) {
  116. f.nextiter = -1;
  117. f.lastiter = -1;
  118. }
  119. do {
  120. c = *f.charp++;
  121. } while (c != '>');
  122. return n;
  123. }
  124. n = cnumb();
  125. if(*f.charp++ != '-') {
  126. cdiag("- expected", f.charp[-1]);
  127. return 0;
  128. }
  129. c = cnumb();
  130. if(*f.charp++ != '>') {
  131. cdiag("> expected", f.charp[-1]);
  132. return 0;
  133. }
  134. f.lastiter = c;
  135. f.diriter = 1;
  136. if(n > c)
  137. f.diriter = -1;
  138. f.nextiter = n+f.diriter;
  139. return n;
  140. }
  141. if(!isascii(c) || !isdigit(c)) {
  142. cdiag("number expected", c);
  143. return 0;
  144. }
  145. n = 0;
  146. while(isascii(c) && isdigit(c)) {
  147. n = n*10 + (c-'0');
  148. c = *f.charp++;
  149. }
  150. f.charp--;
  151. return n;
  152. }
  153. Device*
  154. config1(int c)
  155. {
  156. Device *d, *t;
  157. int m;
  158. d = malloc(sizeof(Device));
  159. do {
  160. t = config();
  161. if(d->cat.first == 0)
  162. d->cat.first = t;
  163. else
  164. d->cat.last->link = t;
  165. d->cat.last = t;
  166. if(f.error)
  167. return devnone;
  168. m = *f.charp;
  169. if(c == '(' && m == ')')
  170. d->type = Devmcat;
  171. else if(c == '[' && m == ']')
  172. d->type = Devmlev;
  173. else if(c == '{' && m == '}')
  174. d->type = Devmirr;
  175. } while (d->type == 0);
  176. f.charp++;
  177. if(d->cat.first == d->cat.last)
  178. d = d->cat.first;
  179. return d;
  180. }
  181. static void
  182. map(Device *d)
  183. {
  184. Map *map;
  185. if (d->type != Devwren || d->wren.mapped)
  186. return;
  187. for (map = devmap; map != nil; map = map->next)
  188. if (devcmpr(d, map->fdev) == 0)
  189. break;
  190. if (map == nil)
  191. return;
  192. if (access(map->to, AEXIST) >= 0)
  193. { print("map: mapped wren %Z to existing file %s\n", d, map->to); // DEBUG
  194. d->wren.file = map->to; /* wren -> file mapping */
  195. }
  196. else if (map->tdev != nil)
  197. { print("map: mapped wren %Z to dev %Z\n", d, map->tdev); // DEBUG
  198. *d = *map->tdev; /* wren -> wren mapping */
  199. }
  200. else
  201. print("bad mapping %Z to %s; no such file or device",
  202. d, map->to);
  203. d->wren.mapped = 1;
  204. }
  205. Device*
  206. config(void)
  207. {
  208. int c, m;
  209. Device *d;
  210. char *icp;
  211. if(f.error)
  212. return devnone;
  213. d = malloc(sizeof(Device));
  214. c = *f.charp++;
  215. switch(c) {
  216. default:
  217. cdiag("unknown type", c);
  218. return devnone;
  219. case '(': /* (d+) one or multiple cat */
  220. case '[': /* [d+] one or multiple interleave */
  221. case '{': /* {d+} a mirrored device and optional mirrors */
  222. return config1(c);
  223. case 'f': /* fd fake worm */
  224. d->type = Devfworm;
  225. d->fw.fw = config();
  226. break;
  227. case 'n':
  228. d->type = Devnone;
  229. break;
  230. case 'w': /* w[#.]#[.#] wren [ctrl] unit [lun] */
  231. case 'r': /* r# worm side */
  232. case 'l': /* l# labelled-worm side */
  233. icp = f.charp;
  234. d->type = Devwren;
  235. d->wren.ctrl = 0;
  236. d->wren.targ = cnumb();
  237. d->wren.lun = 0;
  238. m = *f.charp;
  239. if(m == '.') {
  240. f.charp++;
  241. d->wren.lun = cnumb();
  242. m = *f.charp;
  243. if(m == '.') {
  244. f.charp++;
  245. d->wren.ctrl = d->wren.targ;
  246. d->wren.targ = d->wren.lun;
  247. d->wren.lun = cnumb();
  248. }
  249. }
  250. if(f.nextiter >= 0)
  251. f.charp = icp-1;
  252. if(c == 'r') /* worms are virtual and not uniqued */
  253. d->type = Devworm;
  254. else if(c == 'l')
  255. d->type = Devlworm;
  256. else
  257. map(d); /* subject wrens to optional mapping */
  258. break;
  259. case 'o': /* o ro part of last cw */
  260. if(f.lastcw == 0) {
  261. cdiag("no cw to match", c);
  262. return devnone;
  263. }
  264. return f.lastcw->cw.ro;
  265. case 'j': /* DD jukebox */
  266. d->type = Devjuke;
  267. d->j.j = config();
  268. d->j.m = config();
  269. break;
  270. case 'c': /* cache/worm */
  271. d->type = Devcw;
  272. d->cw.c = config();
  273. d->cw.w = config();
  274. d->cw.ro = malloc(sizeof(Device));
  275. d->cw.ro->type = Devro;
  276. d->cw.ro->ro.parent = d;
  277. f.lastcw = d;
  278. break;
  279. case 'p': /* pd#.# partition base% size% */
  280. d->type = Devpart;
  281. d->part.d = config();
  282. d->part.base = cnumb();
  283. c = *f.charp++;
  284. if(c != '.')
  285. cdiag("dot expected", c);
  286. d->part.size = cnumb();
  287. break;
  288. case 'x': /* xD swab a device's metadata */
  289. d->type = Devswab;
  290. d->swab.d = config();
  291. break;
  292. }
  293. d->dlink = f.devlist;
  294. f.devlist = d;
  295. return d;
  296. }
  297. Device*
  298. iconfig(char *s)
  299. {
  300. Device *d;
  301. f.nextiter = -1;
  302. f.lastiter = -1;
  303. f.error = 0;
  304. f.icharp = s;
  305. f.charp = f.icharp;
  306. d = config();
  307. if(*f.charp) {
  308. cdiag("junk on end", *f.charp);
  309. f.error = 1;
  310. }
  311. return d;
  312. }
  313. int
  314. testconfig(char *s)
  315. {
  316. iconfig(s);
  317. return f.error;
  318. }
  319. /*
  320. * if b is a prefix of a, return 0.
  321. */
  322. int
  323. astrcmp(char *a, char *b)
  324. {
  325. int n, c;
  326. n = strlen(b);
  327. if(memcmp(a, b, n) != 0)
  328. return 1;
  329. c = a[n];
  330. if(c == '\0')
  331. return 0;
  332. if(a[n+1])
  333. return 1;
  334. if(isascii(c) && isdigit(c))
  335. return 0;
  336. return 1;
  337. }
  338. static Fspar *
  339. getpar(char *name)
  340. {
  341. Fspar *fsp;
  342. for (fsp = fspar; fsp->name != nil; fsp++)
  343. if (strcmp(name, fsp->name) == 0)
  344. return fsp;
  345. return nil;
  346. }
  347. /*
  348. * continue to parse obsolete keywords so that old configurations can
  349. * still work.
  350. */
  351. void
  352. mergeconf(Iobuf *p)
  353. {
  354. char word[Maxword+1];
  355. char *cp;
  356. Filsys *fs;
  357. Fspar *fsp;
  358. for (cp = p->iobuf; *cp != '\0'; cp++) {
  359. cp = getwrd(word, cp);
  360. if(word[0] == '\0')
  361. break;
  362. else if (word[0] == '#')
  363. while (*cp != '\n' && *cp != '\0')
  364. cp++;
  365. else if(strcmp(word, "service") == 0) {
  366. cp = getwrd(word, cp);
  367. if(service[0] == 0)
  368. strncpy(service, word, sizeof service);
  369. } else if(strcmp(word, "ipauth") == 0) /* obsolete */
  370. cp = getwrd(word, cp);
  371. else if(astrcmp(word, "ip") == 0) /* obsolete */
  372. cp = getwrd(word, cp);
  373. else if(astrcmp(word, "ipgw") == 0) /* obsolete */
  374. cp = getwrd(word, cp);
  375. else if(astrcmp(word, "ipsntp") == 0) /* obsolete */
  376. cp = getwrd(word, cp);
  377. else if(astrcmp(word, "ipmask") == 0) /* obsolete */
  378. cp = getwrd(word, cp);
  379. else if(strcmp(word, "filsys") == 0) {
  380. cp = getwrd(word, cp);
  381. for(fs = filsys; fs < filsys + nelem(filsys) - 1 &&
  382. fs->name; fs++)
  383. if(strcmp(fs->name, word) == 0)
  384. break;
  385. if (fs >= filsys + nelem(filsys) - 1)
  386. panic("out of filsys structures");
  387. if (fs->name && strcmp(fs->name, word) == 0 &&
  388. fs->flags & FEDIT)
  389. cp = getwrd(word, cp); /* swallow conf */
  390. else {
  391. fs->name = strdup(word);
  392. cp = getwrd(word, cp);
  393. if (word[0] == '\0')
  394. fs->conf = nil;
  395. else
  396. fs->conf = strdup(word);
  397. }
  398. } else if ((fsp = getpar(word)) != nil) {
  399. cp = getwrd(word, cp);
  400. if (!isascii(word[0]) || !isdigit(word[0]))
  401. print("bad %s value: %s", fsp->name, word);
  402. else
  403. fsp->declared = atol(word);
  404. } else {
  405. putbuf(p);
  406. panic("unknown keyword in config block: %s", word);
  407. }
  408. if(*cp != '\n') {
  409. putbuf(p);
  410. panic("syntax error in config block at `%s'", word);
  411. }
  412. }
  413. }
  414. void
  415. cmd_printconf(int, char *[])
  416. {
  417. char *p, *s;
  418. Iobuf *iob;
  419. iob = getbuf(confdev, 0, Brd);
  420. if(iob == nil)
  421. return;
  422. if(checktag(iob, Tconfig, 0)){
  423. putbuf(iob);
  424. return;
  425. }
  426. print("config %s\n", nvrgetconfig());
  427. for(s = p = iob->iobuf; *p != 0 && p < iob->iobuf+BUFSIZE; ){
  428. if(*p++ != '\n')
  429. continue;
  430. if (strncmp(s, "ip", 2) != 0) /* don't print obsolete cmds */
  431. print("%.*s", (int)(p-s), s);
  432. s = p;
  433. }
  434. if(p != s)
  435. print("%.*s", (int)(p-s), s);
  436. print("end\n");
  437. putbuf(iob);
  438. }
  439. void
  440. sysinit(void)
  441. {
  442. int error;
  443. char *cp, *ep;
  444. Device *d;
  445. Filsys *fs;
  446. Fspar *fsp;
  447. Iobuf *p;
  448. cons.chan = fs_chaninit(Devcon, 1, 0);
  449. start:
  450. /*
  451. * part 1 -- read the config file
  452. */
  453. devnone = iconfig("n");
  454. cp = nvrgetconfig();
  455. print("config %s\n", cp);
  456. confdev = d = iconfig(cp);
  457. devinit(d);
  458. if(f.newconf) {
  459. p = getbuf(d, 0, Bmod);
  460. memset(p->iobuf, 0, RBUFSIZE);
  461. settag(p, Tconfig, 0);
  462. } else
  463. p = getbuf(d, 0, Brd|Bmod);
  464. if(!p || checktag(p, Tconfig, 0))
  465. panic("config io");
  466. mergeconf(p);
  467. if (resetparams) {
  468. for (fsp = fspar; fsp->name != nil; fsp++)
  469. fsp->declared = 0;
  470. resetparams = 0;
  471. }
  472. for (fsp = fspar; fsp->name != nil; fsp++) {
  473. /* supply defaults from this cwfs instance */
  474. if (fsp->declared == 0) {
  475. fsp->declared = fsp->actual;
  476. f.modconf = 1;
  477. }
  478. /* warn if declared value is not our compiled-in value */
  479. if (fsp->declared != fsp->actual)
  480. print("warning: config %s %ld != compiled-in %ld\n",
  481. fsp->name, fsp->declared, fsp->actual);
  482. }
  483. if(f.modconf) {
  484. memset(p->iobuf, 0, BUFSIZE);
  485. p->flags |= Bmod|Bimm;
  486. cp = p->iobuf;
  487. ep = p->iobuf + RBUFSIZE - 1;
  488. if(service[0])
  489. cp = seprint(cp, ep, "service %s\n", service);
  490. for(fs=filsys; fs->name; fs++)
  491. if(fs->conf && fs->conf[0] != '\0')
  492. cp = seprint(cp, ep, "filsys %s %s\n", fs->name,
  493. fs->conf);
  494. for (fsp = fspar; fsp->name != nil; fsp++)
  495. cp = seprint(cp, ep, "%s %ld\n",
  496. fsp->name, fsp->declared);
  497. putbuf(p);
  498. f.modconf = f.newconf = 0;
  499. print("config block written\n");
  500. goto start;
  501. }
  502. putbuf(p);
  503. print("service %s\n", service);
  504. loop:
  505. /*
  506. * part 2 -- squeeze out the deleted filesystems
  507. */
  508. for(fs=filsys; fs->name; fs++)
  509. if(fs->conf == nil || fs->conf[0] == '\0') {
  510. for(; fs->name; fs++)
  511. *fs = *(fs+1);
  512. goto loop;
  513. }
  514. if(filsys[0].name == nil)
  515. panic("no filsys");
  516. /*
  517. * part 3 -- compile the device expression
  518. */
  519. error = 0;
  520. for(fs=filsys; fs->name; fs++) {
  521. print("filsys %s %s\n", fs->name, fs->conf);
  522. fs->dev = iconfig(fs->conf);
  523. if(f.error) {
  524. error = 1;
  525. continue;
  526. }
  527. }
  528. if(error)
  529. panic("fs config");
  530. /*
  531. * part 4 -- initialize the devices
  532. */
  533. for(fs=filsys; fs->name; fs++) {
  534. delay(3000);
  535. print("sysinit: %s\n", fs->name);
  536. if(fs->flags & FREAM)
  537. devream(fs->dev, 1);
  538. if(fs->flags & FRECOVER)
  539. devrecover(fs->dev);
  540. devinit(fs->dev);
  541. }
  542. /*
  543. * part 5 -- optionally copy devices or worms
  544. */
  545. if (copyworm) {
  546. dowormcopy(); /* can return if user quits early */
  547. panic("copyworm bailed out!");
  548. }
  549. if (copydev)
  550. if (dodevcopy() < 0)
  551. panic("copydev failed!");
  552. else
  553. panic("copydev done.");
  554. }
  555. /* an unfinished idea. a non-blocking rawchar() would help. */
  556. static int
  557. userabort(char *msg)
  558. {
  559. USED(msg);
  560. return 0;
  561. }
  562. static int
  563. blockok(Device *d, Off a)
  564. {
  565. Iobuf *p = getbuf(d, a, Brd);
  566. if (p == 0) {
  567. print("i/o error reading %Z block %lld\n", d, (Wideoff)a);
  568. return 0;
  569. }
  570. putbuf(p);
  571. return 1;
  572. }
  573. /*
  574. * special case for fake worms only:
  575. * we need to size the inner cw's worm device.
  576. * in particular, we want to avoid copying the fake-worm bitmap
  577. * at the end of the device.
  578. *
  579. * N.B.: for real worms (e.g. cw jukes), we need to compute devsize(cw(juke)),
  580. * *NOT* devsize(juke).
  581. */
  582. static Device *
  583. wormof(Device *dev)
  584. {
  585. Device *worm = dev, *cw;
  586. if (dev->type == Devfworm) {
  587. cw = dev->fw.fw;
  588. if (cw != nil && cw->type == Devcw)
  589. worm = cw->cw.w;
  590. }
  591. // print("wormof(%Z)=%Z\n", dev, worm);
  592. return worm;
  593. }
  594. /*
  595. * return the number of the highest-numbered block actually written, plus 1.
  596. * 0 indicates an error.
  597. */
  598. static Devsize
  599. writtensize(Device *worm)
  600. {
  601. Devsize lim = devsize(worm);
  602. Iobuf *p;
  603. print("devsize(%Z) = %lld\n", worm, (Wideoff)lim);
  604. if (!blockok(worm, 0) || !blockok(worm, lim-1))
  605. return 0;
  606. delay(5*1000);
  607. if (userabort("sanity checks"))
  608. return 0;
  609. /* find worm's last valid block in case "worm" is an (f)worm */
  610. while (lim > 0) {
  611. if (userabort("sizing")) {
  612. lim = 0; /* you lose */
  613. break;
  614. }
  615. --lim;
  616. p = getbuf(worm, lim, Brd);
  617. if (p != 0) { /* actually read one okay? */
  618. putbuf(p);
  619. break;
  620. }
  621. }
  622. print("limit(%Z) = %lld\n", worm, (Wideoff)lim);
  623. if (lim <= 0)
  624. return 0;
  625. return lim + 1;
  626. }
  627. /* copy worm fs from "main"'s inner worm to "output" */
  628. static void
  629. dowormcopy(void)
  630. {
  631. Filsys *f1, *f2;
  632. Device *fdev, *from, *to = nil;
  633. Iobuf *p;
  634. Off a;
  635. Devsize lim;
  636. /*
  637. * convert file system names into Filsyss and Devices.
  638. */
  639. f1 = fsstr("main");
  640. if(f1 == nil)
  641. panic("main file system missing");
  642. fdev = f1->dev;
  643. from = wormof(fdev); /* fake worm special */
  644. if (from->type != Devfworm && from->type != Devcw) {
  645. print("main file system is not a worm; copyworm may not do what you want!\n");
  646. print("waiting for 20 seconds...\n");
  647. delay(20000);
  648. }
  649. f2 = fsstr("output");
  650. if(f2 == nil) {
  651. print("no output file system - check only\n\n");
  652. print("reading worm from %Z (worm %Z)\n", fdev, from);
  653. } else {
  654. to = f2->dev;
  655. print("\ncopying worm from %Z (worm %Z) to %Z, starting in 8 seconds\n",
  656. fdev, from, to);
  657. delay(8000);
  658. }
  659. if (userabort("preparing to copy"))
  660. return;
  661. /*
  662. * initialise devices, size them, more sanity checking.
  663. */
  664. devinit(from);
  665. if (0 && fdev != from) {
  666. devinit(fdev);
  667. print("debugging, sizing %Z first\n", fdev);
  668. writtensize(fdev);
  669. }
  670. lim = writtensize(from);
  671. if(lim == 0)
  672. panic("no blocks to copy on %Z", from);
  673. if (to) {
  674. print("reaming %Z in 8 seconds\n", to);
  675. delay(8000);
  676. if (userabort("preparing to ream & copy"))
  677. return;
  678. devream(to, 0);
  679. devinit(to);
  680. print("copying worm: %lld blocks from %Z to %Z\n",
  681. (Wideoff)lim, from, to);
  682. }
  683. /* can't read to's blocks in case to is a real WORM device */
  684. /*
  685. * Copy written fs blocks, a block at a time (or just read
  686. * if no "output" fs).
  687. */
  688. for (a = 0; a < lim; a++) {
  689. if (userabort("copy"))
  690. break;
  691. p = getbuf(from, a, Brd);
  692. /*
  693. * if from is a real WORM device, we'll get errors trying to
  694. * read unwritten blocks, but the unwritten blocks need not
  695. * be contiguous.
  696. */
  697. if (p == 0) {
  698. print("%lld not written yet; can't read\n", (Wideoff)a);
  699. continue;
  700. }
  701. if (to != 0 && devwrite(to, p->addr, p->iobuf) != 0) {
  702. print("out block %lld: write error; bailing",
  703. (Wideoff)a);
  704. break;
  705. }
  706. putbuf(p);
  707. if(a % 20000 == 0)
  708. print("block %lld %T\n", (Wideoff)a, time(nil));
  709. }
  710. /*
  711. * wrap up: sync target, loop
  712. */
  713. print("copied %lld blocks from %Z to %Z\n", (Wideoff)a, from, to);
  714. sync("wormcopy");
  715. delay(2000);
  716. print("looping; reset the machine at any time.\n");
  717. for (; ; )
  718. continue; /* await reset */
  719. }
  720. /* copy device from src to dest */
  721. static int
  722. dodevcopy(void)
  723. {
  724. Device *from, *to;
  725. Iobuf *p;
  726. Off a;
  727. Devsize lim, tosize;
  728. /*
  729. * convert config strings into Devices.
  730. */
  731. from = iconfig(src);
  732. if(f.error || from == nil) {
  733. print("bad src device %s\n", src);
  734. return -1;
  735. }
  736. to = iconfig(dest);
  737. if(f.error || to == nil) {
  738. print("bad dest device %s\n", dest);
  739. return -1;
  740. }
  741. /*
  742. * initialise devices, size them, more sanity checking.
  743. */
  744. devinit(from);
  745. lim = devsize(from);
  746. if(lim == 0)
  747. panic("no blocks to copy on %Z", from);
  748. devinit(to);
  749. tosize = devsize(to);
  750. if(tosize == 0)
  751. panic("no blocks to copy on %Z", to);
  752. /* use smaller of the device sizes */
  753. if (tosize < lim)
  754. lim = tosize;
  755. print("copy %Z to %Z in 8 seconds\n", from, to);
  756. delay(8000);
  757. if (userabort("preparing to copy"))
  758. return -1;
  759. print("copying dev: %lld blocks from %Z to %Z\n", (Wideoff)lim,
  760. from, to);
  761. /*
  762. * Copy all blocks, a block at a time.
  763. */
  764. for (a = 0; a < lim; a++) {
  765. if (userabort("copy"))
  766. break;
  767. p = getbuf(from, a, Brd);
  768. /*
  769. * if from is a real WORM device, we'll get errors trying to
  770. * read unwritten blocks, but the unwritten blocks need not
  771. * be contiguous.
  772. */
  773. if (p == 0) {
  774. print("%lld not written yet; can't read\n", (Wideoff)a);
  775. continue;
  776. }
  777. if (to != 0 && devwrite(to, p->addr, p->iobuf) != 0) {
  778. print("out block %lld: write error; bailing",
  779. (Wideoff)a);
  780. break;
  781. }
  782. putbuf(p);
  783. if(a % 20000 == 0)
  784. print("block %lld %T\n", (Wideoff)a, time(nil));
  785. }
  786. /*
  787. * wrap up: sync target
  788. */
  789. print("copied %lld blocks from %Z to %Z\n", (Wideoff)a, from, to);
  790. sync("devcopy");
  791. return 0;
  792. }
  793. static void
  794. setconfig(char *dev)
  795. {
  796. if (dev != nil && !testconfig(dev))
  797. nvrsetconfig(dev); /* if it fails, it will complain */
  798. }
  799. void
  800. arginit(void)
  801. {
  802. int verb;
  803. char *line;
  804. char word[Maxword+1], *cp;
  805. Filsys *fs;
  806. if(nvrcheck() == 0) {
  807. setconfig(conf.confdev);
  808. if (!conf.configfirst)
  809. return;
  810. }
  811. /* nvr was bad or invoker requested configuration step */
  812. setconfig(conf.confdev);
  813. for (;;) {
  814. print("config: ");
  815. if ((line = Brdline(&bin, '\n')) == nil)
  816. return;
  817. line[Blinelen(&bin)-1] = '\0';
  818. cp = getwrd(word, line);
  819. if (word[0] == '\0' || word[0] == '#')
  820. continue;
  821. if(strcmp(word, "end") == 0)
  822. return;
  823. if(strcmp(word, "halt") == 0)
  824. exit();
  825. if(strcmp(word, "queryjuke") == 0) {
  826. getwrd(word, cp);
  827. if(testconfig(word) == 0)
  828. querychanger(iconfig(word));
  829. continue;
  830. }
  831. if(strcmp(word, "allow") == 0) {
  832. wstatallow = 1;
  833. writeallow = 1;
  834. continue;
  835. }
  836. if(strcmp(word, "copyworm") == 0) {
  837. copyworm = 1;
  838. continue;
  839. }
  840. if(strcmp(word, "copydev") == 0) {
  841. cp = getwrd(word, cp);
  842. if(testconfig(word))
  843. continue;
  844. src = strdup(word);
  845. getwrd(word, cp);
  846. if(testconfig(word))
  847. continue;
  848. dest = strdup(word);
  849. copydev = 1;
  850. continue;
  851. }
  852. if(strcmp(word, "noauth") == 0) {
  853. noauth = !noauth;
  854. continue;
  855. }
  856. if(strcmp(word, "noattach") == 0) {
  857. noattach = !noattach;
  858. continue;
  859. }
  860. if(strcmp(word, "readonly") == 0) {
  861. readonly = 1;
  862. continue;
  863. }
  864. if(strcmp(word, "ream") == 0) {
  865. verb = FREAM;
  866. goto gfsname;
  867. }
  868. if(strcmp(word, "recover") == 0) {
  869. verb = FRECOVER;
  870. goto gfsname;
  871. }
  872. if(strcmp(word, "filsys") == 0) {
  873. verb = FEDIT;
  874. goto gfsname;
  875. }
  876. if(strcmp(word, "nvram") == 0) {
  877. getwrd(word, cp);
  878. if(testconfig(word))
  879. continue;
  880. /* if it fails, it will complain */
  881. nvrsetconfig(word);
  882. continue;
  883. }
  884. if(strcmp(word, "config") == 0) {
  885. getwrd(word, cp);
  886. if(!testconfig(word) && nvrsetconfig(word) == 0)
  887. f.newconf = 1;
  888. continue;
  889. }
  890. if(strcmp(word, "service") == 0) {
  891. getwrd(word, cp);
  892. strncpy(service, word, sizeof service);
  893. f.modconf = 1;
  894. continue;
  895. }
  896. if (strcmp(word, "resetparams") == 0) {
  897. resetparams++;
  898. continue;
  899. }
  900. /*
  901. * continue to parse obsolete keywords so that old
  902. * configurations can still work.
  903. */
  904. if (strcmp(word, "ipauth") != 0 &&
  905. astrcmp(word, "ip") != 0 &&
  906. astrcmp(word, "ipgw") != 0 &&
  907. astrcmp(word, "ipmask") != 0 &&
  908. astrcmp(word, "ipsntp") != 0) {
  909. print("unknown config command\n");
  910. print("\ttype end to get out\n");
  911. continue;
  912. }
  913. getwrd(word, cp);
  914. f.modconf = 1;
  915. continue;
  916. gfsname:
  917. cp = getwrd(word, cp);
  918. for(fs=filsys; fs->name; fs++)
  919. if(strcmp(word, fs->name) == 0)
  920. break;
  921. if (fs->name == nil) {
  922. memset(fs, 0, sizeof *fs);
  923. fs->name = strdup(word);
  924. }
  925. switch(verb) {
  926. case FREAM:
  927. if(strcmp(fs->name, "main") == 0)
  928. wstatallow = 1; /* only set, never reset */
  929. /* fallthrough */
  930. case FRECOVER:
  931. fs->flags |= verb;
  932. break;
  933. case FEDIT:
  934. f.modconf = 1;
  935. getwrd(word, cp);
  936. fs->flags |= verb;
  937. if(word[0] == 0)
  938. fs->conf = nil;
  939. else if(!testconfig(word))
  940. fs->conf = strdup(word);
  941. break;
  942. }
  943. }
  944. }