config.c 20 KB

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