config.c 20 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067
  1. #include "all.h"
  2. #include "io.h"
  3. static void dowormcopy(void);
  4. static int dodevcopy(void);
  5. /*
  6. * This is needed for IP configuration.
  7. */
  8. #include "../ip/ip.h"
  9. struct
  10. {
  11. char* icharp;
  12. char* charp;
  13. int error;
  14. int newconf; /* clear befor start */
  15. int modconf; /* write back when done */
  16. int nextiter;
  17. int lastiter;
  18. int diriter;
  19. int ipauthset;
  20. Device* lastcw;
  21. Device* devlist;
  22. } f;
  23. static Device* confdev;
  24. static int copyworm = 0, copydev = 0;
  25. static char *src, *dest;
  26. int
  27. devcmpr(Device *d1, Device *d2)
  28. {
  29. while (d1 != d2) {
  30. if(d1 == 0 || d2 == 0 || d1->type != d2->type)
  31. return 1;
  32. switch(d1->type) {
  33. default:
  34. print("can't compare dev: %Z\n", d1);
  35. panic("devcmp");
  36. return 1;
  37. case Devmcat:
  38. case Devmlev:
  39. case Devmirr:
  40. d1 = d1->cat.first;
  41. d2 = d2->cat.first;
  42. while(d1 && d2) {
  43. if(devcmpr(d1, d2))
  44. return 1;
  45. d1 = d1->link;
  46. d2 = d2->link;
  47. }
  48. break;
  49. case Devnone:
  50. return 0;
  51. case Devro:
  52. d1 = d1->ro.parent;
  53. d2 = d2->ro.parent;
  54. break;
  55. case Devjuke:
  56. case Devcw:
  57. if(devcmpr(d1->cw.c, d2->cw.c))
  58. return 1;
  59. d1 = d1->cw.w;
  60. d2 = d2->cw.w;
  61. break;
  62. case Devfworm:
  63. d1 = d1->fw.fw;
  64. d2 = d2->fw.fw;
  65. break;
  66. case Devwren:
  67. case Devworm:
  68. case Devlworm:
  69. case Devide:
  70. case Devmarvsata:
  71. if(d1->wren.ctrl == d2->wren.ctrl)
  72. if(d1->wren.targ == d2->wren.targ)
  73. if(d1->wren.lun == d2->wren.lun)
  74. return 0;
  75. return 1;
  76. case Devpart:
  77. if(d1->part.base == d2->part.base)
  78. if(d1->part.size == d2->part.size) {
  79. d1 = d1->part.d;
  80. d2 = d2->part.d;
  81. break;
  82. }
  83. return 1;
  84. }
  85. }
  86. return 0;
  87. }
  88. void
  89. cdiag(char *s, int c1)
  90. {
  91. f.charp--;
  92. if(f.error == 0) {
  93. print("config diag: %s -- <%c>\n", s, c1);
  94. f.error = 1;
  95. }
  96. }
  97. int
  98. cnumb(void)
  99. {
  100. int c, n;
  101. c = *f.charp++;
  102. if(c == '<') {
  103. n = f.nextiter;
  104. if(n >= 0) {
  105. f.nextiter = n+f.diriter;
  106. if(n == f.lastiter) {
  107. f.nextiter = -1;
  108. f.lastiter = -1;
  109. }
  110. do {
  111. c = *f.charp++;
  112. } while (c != '>');
  113. return n;
  114. }
  115. n = cnumb();
  116. if(*f.charp++ != '-') {
  117. cdiag("- expected", f.charp[-1]);
  118. return 0;
  119. }
  120. c = cnumb();
  121. if(*f.charp++ != '>') {
  122. cdiag("> expected", f.charp[-1]);
  123. return 0;
  124. }
  125. f.lastiter = c;
  126. f.diriter = 1;
  127. if(n > c)
  128. f.diriter = -1;
  129. f.nextiter = n+f.diriter;
  130. return n;
  131. }
  132. if(c < '0' || c > '9') {
  133. cdiag("number expected", c);
  134. return 0;
  135. }
  136. n = 0;
  137. while(c >= '0' && c <= '9') {
  138. n = n*10 + (c-'0');
  139. c = *f.charp++;
  140. }
  141. f.charp--;
  142. return n;
  143. }
  144. Device*
  145. config1(int c)
  146. {
  147. Device *d, *t;
  148. int m;
  149. d = ialloc(sizeof(Device), 0);
  150. for(;;) {
  151. t = config();
  152. if(d->cat.first == 0)
  153. d->cat.first = t;
  154. else
  155. d->cat.last->link = t;
  156. d->cat.last = t;
  157. if(f.error)
  158. return devnone;
  159. m = *f.charp;
  160. if(c == '(' && m == ')') {
  161. d->type = Devmcat;
  162. break;
  163. }
  164. if(c == '[' && m == ']') {
  165. d->type = Devmlev;
  166. break;
  167. }
  168. if(c == '{' && m == '}') {
  169. d->type = Devmirr;
  170. break;
  171. }
  172. }
  173. f.charp++;
  174. if(d->cat.first == d->cat.last)
  175. d = d->cat.first;
  176. return d;
  177. }
  178. Device*
  179. config(void)
  180. {
  181. int c, m;
  182. Device *d;
  183. char *icp;
  184. if(f.error)
  185. return devnone;
  186. d = ialloc(sizeof(Device), 0);
  187. c = *f.charp++;
  188. switch(c) {
  189. default:
  190. cdiag("unknown type", c);
  191. return devnone;
  192. case '(': /* (d+) one or multiple cat */
  193. case '[': /* [d+] one or multiple interleave */
  194. case '{': /* {d+} a mirrored device and optional mirrors */
  195. return config1(c);
  196. case 'f': /* fd fake worm */
  197. d->type = Devfworm;
  198. d->fw.fw = config();
  199. break;
  200. case 'n':
  201. d->type = Devnone;
  202. break;
  203. case 'w': /* w[#.]#[.#] wren [ctrl] unit [lun] */
  204. case 'h': /* h[#.]# ide [ctlr] unit */
  205. case 'm': /* m[#.]# marvell sata [ctlr] unit */
  206. case 'r': /* r# worm side */
  207. case 'l': /* l# labelled-worm side */
  208. icp = f.charp;
  209. if(c == 'm')
  210. d->type = Devmarvsata;
  211. else if(c == 'h')
  212. d->type = Devide;
  213. else
  214. d->type = Devwren;
  215. d->wren.ctrl = 0;
  216. d->wren.targ = cnumb();
  217. d->wren.lun = 0;
  218. m = *f.charp;
  219. if(m == '.') {
  220. f.charp++;
  221. d->wren.lun = cnumb();
  222. m = *f.charp;
  223. if(m == '.') {
  224. f.charp++;
  225. d->wren.ctrl = d->wren.targ;
  226. d->wren.targ = d->wren.lun;
  227. d->wren.lun = cnumb();
  228. }
  229. }
  230. if(f.nextiter >= 0)
  231. f.charp = icp-1;
  232. if(c == 'r') { /* worms are virtual and not uniqued */
  233. d->type = Devworm;
  234. break;
  235. }
  236. if(c == 'l') {
  237. d->type = Devlworm;
  238. break;
  239. }
  240. break;
  241. case 'o': /* o ro part of last cw */
  242. if(f.lastcw == 0) {
  243. cdiag("no cw to match", c);
  244. return devnone;
  245. }
  246. return f.lastcw->cw.ro;
  247. case 'j': /* DD jukebox */
  248. d->type = Devjuke;
  249. d->j.j = config();
  250. d->j.m = config();
  251. break;
  252. case 'c': /* cache/worm */
  253. d->type = Devcw;
  254. d->cw.c = config();
  255. d->cw.w = config();
  256. d->cw.ro = ialloc(sizeof(Device), 0);
  257. d->cw.ro->type = Devro;
  258. d->cw.ro->ro.parent = d;
  259. f.lastcw = d;
  260. break;
  261. case 'p': /* pd#.# partition base% size% */
  262. d->type = Devpart;
  263. d->part.d = config();
  264. d->part.base = cnumb();
  265. c = *f.charp++;
  266. if(c != '.')
  267. cdiag("dot expected", c);
  268. d->part.size = cnumb();
  269. break;
  270. case 'x': /* xD swab a device's metadata */
  271. d->type = Devswab;
  272. d->swab.d = config();
  273. break;
  274. }
  275. d->dlink = f.devlist;
  276. f.devlist = d;
  277. return d;
  278. }
  279. char*
  280. strdup(char *s)
  281. {
  282. int n;
  283. char *s1;
  284. n = strlen(s);
  285. s1 = ialloc(n+1, 0);
  286. strcpy(s1, s);
  287. return s1;
  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. int
  312. astrcmp(char *a, char *b)
  313. {
  314. int n, c;
  315. n = strlen(b);
  316. if(memcmp(a, b, n))
  317. return 1;
  318. c = a[n];
  319. if(c == 0) {
  320. aindex = 0;
  321. return 0;
  322. }
  323. if(a[n+1])
  324. return 1;
  325. if(c >= '0' && c <= '9') {
  326. aindex = c - '0';
  327. return 0;
  328. }
  329. return 1;
  330. }
  331. void
  332. mergeconf(Iobuf *p)
  333. {
  334. char word[Maxword+1];
  335. char *cp;
  336. Filsys *fs;
  337. for (cp = p->iobuf; ; cp++) {
  338. cp = getwd(word, cp);
  339. if(strcmp(word, "") == 0)
  340. return;
  341. else if(strcmp(word, "service") == 0) {
  342. cp = getwd(word, cp);
  343. if(service[0] == 0)
  344. strcpy(service, word);
  345. } else if(strcmp(word, "ipauth") == 0) {
  346. cp = getwd(word, cp);
  347. if(!f.ipauthset)
  348. if(chartoip(authip, word))
  349. goto bad;
  350. } else if(astrcmp(word, "ip") == 0) {
  351. cp = getwd(word, cp);
  352. if(!isvalidip(ipaddr[aindex].sysip))
  353. if(chartoip(ipaddr[aindex].sysip, word))
  354. goto bad;
  355. } else if(astrcmp(word, "ipgw") == 0) {
  356. cp = getwd(word, cp);
  357. if(!isvalidip(ipaddr[aindex].defgwip))
  358. if(chartoip(ipaddr[aindex].defgwip, word))
  359. goto bad;
  360. } else if(astrcmp(word, "ipsntp") == 0) {
  361. cp = getwd(word, cp);
  362. if (!isvalidip(sntpip))
  363. if (chartoip(sntpip, word))
  364. goto bad;
  365. } else if(astrcmp(word, "ipmask") == 0) {
  366. cp = getwd(word, cp);
  367. if(!isvalidip(ipaddr[aindex].defmask))
  368. if(chartoip(ipaddr[aindex].defmask, word))
  369. goto bad;
  370. } else if(strcmp(word, "filsys") == 0) {
  371. cp = getwd(word, cp);
  372. for(fs=filsys; fs->name; fs++)
  373. if(strcmp(fs->name, word) == 0) {
  374. if(fs->flags & FEDIT) {
  375. cp = getwd(word, cp);
  376. goto loop;
  377. }
  378. break;
  379. }
  380. fs->name = strdup(word);
  381. cp = getwd(word, cp);
  382. fs->conf = strdup(word);
  383. } else {
  384. bad:
  385. putbuf(p);
  386. panic("unknown word in config block: %s", word);
  387. }
  388. loop:
  389. if(*cp != '\n')
  390. goto bad;
  391. }
  392. }
  393. void
  394. cmd_printconf(int, char *[])
  395. {
  396. char *p, *s;
  397. Iobuf *iob;
  398. iob = getbuf(confdev, 0, Bread);
  399. if(iob == nil)
  400. return;
  401. if(checktag(iob, Tconfig, 0)){
  402. putbuf(iob);
  403. return;
  404. }
  405. print("config %s\n", nvrgetconfig());
  406. s = p = iob->iobuf;
  407. while(*p != 0 && p < iob->iobuf+BUFSIZE){
  408. if(*p++ != '\n')
  409. continue;
  410. print("%.*s", (int)(p-s), s);
  411. s = p;
  412. }
  413. if(p != s)
  414. print("%.*s", (int)(p-s), s);
  415. print("end\n");
  416. putbuf(iob);
  417. }
  418. extern void floppyhalt(void);
  419. void
  420. sysinit(void)
  421. {
  422. Filsys *fs;
  423. int error, i;
  424. Device *d;
  425. Iobuf *p;
  426. char *cp;
  427. dofilter(u->time+0, C0a, C0b, 1);
  428. dofilter(u->time+1, C1a, C1b, 1);
  429. dofilter(u->time+2, C2a, C2b, 1);
  430. dofilter(cons.work+0, C0a, C0b, 1);
  431. dofilter(cons.work+1, C1a, C1b, 1);
  432. dofilter(cons.work+2, C2a, C2b, 1);
  433. dofilter(cons.rate+0, C0a, C0b, 1000);
  434. dofilter(cons.rate+1, C1a, C1b, 1000);
  435. dofilter(cons.rate+2, C2a, C2b, 1000);
  436. dofilter(cons.bhit+0, C0a, C0b, 1);
  437. dofilter(cons.bhit+1, C1a, C1b, 1);
  438. dofilter(cons.bhit+2, C2a, C2b, 1);
  439. dofilter(cons.bread+0, C0a, C0b, 1);
  440. dofilter(cons.bread+1, C1a, C1b, 1);
  441. dofilter(cons.bread+2, C2a, C2b, 1);
  442. dofilter(cons.brahead+0, C0a, C0b, 1);
  443. dofilter(cons.brahead+1, C1a, C1b, 1);
  444. dofilter(cons.brahead+2, C2a, C2b, 1);
  445. dofilter(cons.binit+0, C0a, C0b, 1);
  446. dofilter(cons.binit+1, C1a, C1b, 1);
  447. dofilter(cons.binit+2, C2a, C2b, 1);
  448. cons.chan = 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, Bread|Bmod);
  464. if(!p || checktag(p, Tconfig, 0))
  465. panic("config io");
  466. mergeconf(p);
  467. if(f.modconf) {
  468. memset(p->iobuf, 0, BUFSIZE);
  469. p->flags |= Bmod|Bimm;
  470. if(service[0])
  471. sprint(strchr(p->iobuf, 0), "service %s\n", service);
  472. for(fs=filsys; fs->name; fs++)
  473. if(fs->conf)
  474. sprint(strchr(p->iobuf, 0),
  475. "filsys %s %s\n", fs->name, fs->conf);
  476. sprint(strchr(p->iobuf, 0), "ipauth %I\n", authip);
  477. sprint(strchr(p->iobuf, 0), "ipsntp %I\n", sntpip);
  478. for(i=0; i<10; i++) {
  479. if(isvalidip(ipaddr[i].sysip))
  480. sprint(strchr(p->iobuf, 0),
  481. "ip%d %I\n", i, ipaddr[i].sysip);
  482. if(isvalidip(ipaddr[i].defgwip))
  483. sprint(strchr(p->iobuf, 0),
  484. "ipgw%d %I\n", i, ipaddr[i].defgwip);
  485. if(isvalidip(ipaddr[i].defmask))
  486. sprint(strchr(p->iobuf, 0),
  487. "ipmask%d %I\n", i, ipaddr[i].defmask);
  488. }
  489. putbuf(p);
  490. f.modconf = 0;
  491. f.newconf = 0;
  492. print("config block written\n");
  493. goto start;
  494. }
  495. putbuf(p);
  496. print("service %s\n", service);
  497. print("ipauth %I\n", authip);
  498. print("ipsntp %I\n", sntpip);
  499. for(i=0; i<10; i++) {
  500. if(isvalidip(ipaddr[i].sysip)) {
  501. print("ip%d %I\n", i, ipaddr[i].sysip);
  502. print("ipgw%d %I\n", i, ipaddr[i].defgwip);
  503. print("ipmask%d %I\n", i, ipaddr[i].defmask);
  504. }
  505. }
  506. loop:
  507. /*
  508. * part 2 -- squeeze out the deleted filesystems
  509. */
  510. for(fs=filsys; fs->name; fs++)
  511. if(fs->conf == 0) {
  512. for(; fs->name; fs++)
  513. *fs = *(fs+1);
  514. goto loop;
  515. }
  516. if(filsys[0].name == 0)
  517. panic("no filsys");
  518. /*
  519. * part 3 -- compile the device expression
  520. */
  521. error = 0;
  522. for(fs=filsys; fs->name; fs++) {
  523. print("filsys %s %s\n", fs->name, fs->conf);
  524. fs->dev = iconfig(fs->conf);
  525. if(f.error) {
  526. error = 1;
  527. continue;
  528. }
  529. }
  530. if(error)
  531. panic("fs config");
  532. /*
  533. * part 4 -- initialize the devices
  534. */
  535. for(fs=filsys; fs->name; fs++) {
  536. delay(3000);
  537. print("sysinit: %s\n", fs->name);
  538. if(fs->flags & FREAM)
  539. devream(fs->dev, 1);
  540. if(fs->flags & FRECOVER)
  541. devrecover(fs->dev);
  542. devinit(fs->dev);
  543. }
  544. floppyhalt(); /* don't wear out the floppy */
  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. #ifdef IdeaIsFinished
  560. if (consgetcifany() == 'q') {
  561. print("aborting %s\n", msg);
  562. return 1;
  563. }
  564. #else
  565. USED(msg);
  566. #endif /* IdeaIsFinished */
  567. return 0;
  568. }
  569. static int
  570. blockok(Device *d, Off a)
  571. {
  572. Iobuf *p = getbuf(d, a, Bread);
  573. if (p == 0) {
  574. print("i/o error reading %Z block %lld\n", d, (Wideoff)a);
  575. return 0;
  576. }
  577. putbuf(p);
  578. return 1;
  579. }
  580. /*
  581. * special case for fake worms only:
  582. * we need to size the inner cw's worm device.
  583. * in particular, we want to avoid copying the fake-worm bitmap
  584. * at the end of the device.
  585. *
  586. * N.B.: for real worms (e.g. cw jukes), we need to compute devsize(cw(juke)),
  587. * *NOT* devsize(juke).
  588. */
  589. static Device *
  590. wormof(Device *dev)
  591. {
  592. Device *worm = dev, *cw;
  593. if (dev->type == Devfworm) {
  594. cw = dev->fw.fw;
  595. if (cw != nil && cw->type == Devcw)
  596. worm = cw->cw.w;
  597. }
  598. // print("wormof(%Z)=%Z\n", dev, worm);
  599. return worm;
  600. }
  601. /*
  602. * return the number of the highest-numbered block actually written, plus 1.
  603. * 0 indicates an error.
  604. */
  605. static Devsize
  606. writtensize(Device *worm)
  607. {
  608. Devsize lim = devsize(worm);
  609. Iobuf *p;
  610. print("devsize(%Z) = %lld\n", worm, (Wideoff)lim);
  611. if (!blockok(worm, 0) || !blockok(worm, lim-1))
  612. return 0;
  613. delay(5*1000);
  614. if (userabort("sanity checks"))
  615. return 0;
  616. /* find worm's last valid block in case "worm" is an (f)worm */
  617. while (lim > 0) {
  618. if (userabort("sizing")) {
  619. lim = 0; /* you lose */
  620. break;
  621. }
  622. --lim;
  623. p = getbuf(worm, lim, Bread);
  624. if (p != 0) { /* actually read one okay? */
  625. putbuf(p);
  626. break;
  627. }
  628. }
  629. print("limit(%Z) = %lld\n", worm, (Wideoff)lim);
  630. return lim <= 0? 0: lim + 1;
  631. }
  632. /* copy worm fs from "main"'s inner worm to "output" */
  633. static void
  634. dowormcopy(void)
  635. {
  636. Filsys *f1, *f2;
  637. Device *fdev, *from, *to = nil;
  638. Iobuf *p;
  639. Off a;
  640. Devsize lim;
  641. /*
  642. * convert file system names into Filsyss and Devices.
  643. */
  644. f1 = fsstr("main");
  645. if(f1 == nil)
  646. panic("main file system missing");
  647. fdev = f1->dev;
  648. from = wormof(fdev); /* fake worm special */
  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, Bread);
  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());
  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, Bread);
  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());
  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. void
  794. getline(char *line)
  795. {
  796. char *p;
  797. int c;
  798. p = line;
  799. for(;;) {
  800. c = rawchar(0);
  801. if(c == 0 || c == '\n') {
  802. *p = 0;
  803. return;
  804. }
  805. if(c == '\b') {
  806. p--;
  807. continue;
  808. }
  809. *p++ = c;
  810. }
  811. }
  812. void
  813. arginit(void)
  814. {
  815. int verb, c;
  816. char line[2*Maxword], word[Maxword+1], *cp;
  817. uchar localip[Pasize];
  818. Filsys *fs;
  819. if(nvrcheck() == 0){
  820. print("for config mode hit a key within 5 seconds\n");
  821. c = rawchar(5);
  822. if(c == 0) {
  823. print(" no config\n");
  824. return;
  825. }
  826. }
  827. for (;;) {
  828. print("config: ");
  829. getline(line);
  830. cp = getwd(word, line);
  831. if (word[0] == '\0' || word[0] == '#')
  832. continue;
  833. if(strcmp(word, "end") == 0)
  834. return;
  835. if(strcmp(word, "halt") == 0) {
  836. floppyhalt();
  837. exit();
  838. }
  839. if(strcmp(word, "allow") == 0) {
  840. wstatallow = 1;
  841. writeallow = 1;
  842. continue;
  843. }
  844. if(strcmp(word, "copyworm") == 0) {
  845. copyworm = 1;
  846. continue;
  847. }
  848. if(strcmp(word, "copydev") == 0) { /* not yet documented */
  849. cp = getwd(word, cp);
  850. if(testconfig(word))
  851. continue;
  852. src = strdup(word);
  853. getwd(word, cp);
  854. if(testconfig(word))
  855. continue;
  856. dest = strdup(word);
  857. copydev = 1;
  858. continue;
  859. }
  860. if(strcmp(word, "noauth") == 0) {
  861. noauth = !noauth;
  862. continue;
  863. }
  864. if(strcmp(word, "noattach") == 0) {
  865. noattach = !noattach;
  866. continue;
  867. }
  868. if(strcmp(word, "readonly") == 0) {
  869. readonly = 1;
  870. continue;
  871. }
  872. if(strcmp(word, "ream") == 0) {
  873. verb = FREAM;
  874. goto gfsname;
  875. }
  876. if(strcmp(word, "recover") == 0) {
  877. verb = FRECOVER;
  878. goto gfsname;
  879. }
  880. if(strcmp(word, "filsys") == 0) {
  881. verb = FEDIT;
  882. goto gfsname;
  883. }
  884. if(strcmp(word, "nvram") == 0) {
  885. getwd(word, cp);
  886. if(testconfig(word))
  887. continue;
  888. /* if it fails, it will complain */
  889. nvrsetconfig(word);
  890. continue;
  891. }
  892. if(strcmp(word, "config") == 0) {
  893. getwd(word, cp);
  894. if(!testconfig(word) && nvrsetconfig(word) == 0)
  895. f.newconf = 1;
  896. continue;
  897. }
  898. if(strcmp(word, "service") == 0) {
  899. getwd(word, cp);
  900. strcpy(service, word);
  901. f.modconf = 1;
  902. continue;
  903. }
  904. if(strcmp(word, "ipauth") == 0) {
  905. f.ipauthset = 1;
  906. verb = 2;
  907. } else if(astrcmp(word, "ip") == 0)
  908. verb = 0;
  909. else if(astrcmp(word, "ipgw") == 0)
  910. verb = 1;
  911. else if(astrcmp(word, "ipmask") == 0)
  912. verb = 3;
  913. else if(astrcmp(word, "ipsntp") == 0)
  914. verb = 4;
  915. else {
  916. print("unknown config command\n");
  917. print(" type end to get out\n");
  918. continue;
  919. }
  920. getwd(word, cp);
  921. if(chartoip(localip, word)) {
  922. print("bad ip address\n");
  923. continue;
  924. }
  925. switch(verb) {
  926. case 0:
  927. memmove(ipaddr[aindex].sysip, localip,
  928. sizeof(ipaddr[aindex].sysip));
  929. break;
  930. case 1:
  931. memmove(ipaddr[aindex].defgwip, localip,
  932. sizeof(ipaddr[aindex].defgwip));
  933. break;
  934. case 2:
  935. memmove(authip, localip, sizeof(authip));
  936. break;
  937. case 3:
  938. memmove(ipaddr[aindex].defmask, localip,
  939. sizeof(ipaddr[aindex].defmask));
  940. break;
  941. case 4:
  942. memmove(sntpip, localip, sizeof(sntpip));
  943. break;
  944. }
  945. f.modconf = 1;
  946. continue;
  947. gfsname:
  948. cp = getwd(word, cp);
  949. for(fs=filsys; fs->name; fs++)
  950. if(strcmp(word, fs->name) == 0)
  951. break;
  952. if (fs->name == nil) {
  953. memset(fs, 0, sizeof(*fs));
  954. fs->name = strdup(word);
  955. }
  956. switch(verb) {
  957. case FREAM:
  958. if(strcmp(fs->name, "main") == 0)
  959. wstatallow = 1; /* only set, never reset */
  960. case FRECOVER:
  961. fs->flags |= verb;
  962. break;
  963. case FEDIT:
  964. f.modconf = 1;
  965. getwd(word, cp);
  966. fs->flags |= verb;
  967. if(word[0] == 0)
  968. fs->conf = nil;
  969. else if(!testconfig(word))
  970. fs->conf = strdup(word);
  971. break;
  972. }
  973. }
  974. }