config.c 20 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072
  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. if (from->type != Devfworm && from->type != Devcw) {
  650. print("main file system is not a worm; copyworm may not do what you want!\n");
  651. print("waiting for 20 seconds...\n");
  652. delay(20000);
  653. }
  654. f2 = fsstr("output");
  655. if(f2 == nil) {
  656. print("no output file system - check only\n\n");
  657. print("reading worm from %Z (worm %Z)\n", fdev, from);
  658. } else {
  659. to = f2->dev;
  660. print("\ncopying worm from %Z (worm %Z) to %Z, starting in 8 seconds\n",
  661. fdev, from, to);
  662. delay(8000);
  663. }
  664. if (userabort("preparing to copy"))
  665. return;
  666. /*
  667. * initialise devices, size them, more sanity checking.
  668. */
  669. devinit(from);
  670. if (0 && fdev != from) {
  671. devinit(fdev);
  672. print("debugging, sizing %Z first\n", fdev);
  673. writtensize(fdev);
  674. }
  675. lim = writtensize(from);
  676. if(lim == 0)
  677. panic("no blocks to copy on %Z", from);
  678. if (to) {
  679. print("reaming %Z in 8 seconds\n", to);
  680. delay(8000);
  681. if (userabort("preparing to ream & copy"))
  682. return;
  683. devream(to, 0);
  684. devinit(to);
  685. print("copying worm: %lld blocks from %Z to %Z\n",
  686. (Wideoff)lim, from, to);
  687. }
  688. /* can't read to's blocks in case to is a real WORM device */
  689. /*
  690. * Copy written fs blocks, a block at a time (or just read
  691. * if no "output" fs).
  692. */
  693. for (a = 0; a < lim; a++) {
  694. if (userabort("copy"))
  695. break;
  696. p = getbuf(from, a, Bread);
  697. /*
  698. * if from is a real WORM device, we'll get errors trying to
  699. * read unwritten blocks, but the unwritten blocks need not
  700. * be contiguous.
  701. */
  702. if (p == 0) {
  703. print("%lld not written yet; can't read\n", (Wideoff)a);
  704. continue;
  705. }
  706. if (to != 0 && devwrite(to, p->addr, p->iobuf) != 0) {
  707. print("out block %lld: write error; bailing",
  708. (Wideoff)a);
  709. break;
  710. }
  711. putbuf(p);
  712. if(a % 20000 == 0)
  713. print("block %lld %T\n", (Wideoff)a, time());
  714. }
  715. /*
  716. * wrap up: sync target, loop
  717. */
  718. print("copied %lld blocks from %Z to %Z\n", (Wideoff)a, from, to);
  719. sync("wormcopy");
  720. delay(2000);
  721. print("looping; reset the machine at any time.\n");
  722. for (; ; )
  723. continue; /* await reset */
  724. }
  725. /* copy device from src to dest */
  726. static int
  727. dodevcopy(void)
  728. {
  729. Device *from, *to;
  730. Iobuf *p;
  731. Off a;
  732. Devsize lim, tosize;
  733. /*
  734. * convert config strings into Devices.
  735. */
  736. from = iconfig(src);
  737. if(f.error || from == nil) {
  738. print("bad src device %s\n", src);
  739. return -1;
  740. }
  741. to = iconfig(dest);
  742. if(f.error || to == nil) {
  743. print("bad dest device %s\n", dest);
  744. return -1;
  745. }
  746. /*
  747. * initialise devices, size them, more sanity checking.
  748. */
  749. devinit(from);
  750. lim = devsize(from);
  751. if(lim == 0)
  752. panic("no blocks to copy on %Z", from);
  753. devinit(to);
  754. tosize = devsize(to);
  755. if(tosize == 0)
  756. panic("no blocks to copy on %Z", to);
  757. /* use smaller of the device sizes */
  758. if (tosize < lim)
  759. lim = tosize;
  760. print("copy %Z to %Z in 8 seconds\n", from, to);
  761. delay(8000);
  762. if (userabort("preparing to copy"))
  763. return -1;
  764. print("copying dev: %lld blocks from %Z to %Z\n", (Wideoff)lim,
  765. from, to);
  766. /*
  767. * Copy all blocks, a block at a time.
  768. */
  769. for (a = 0; a < lim; a++) {
  770. if (userabort("copy"))
  771. break;
  772. p = getbuf(from, a, Bread);
  773. /*
  774. * if from is a real WORM device, we'll get errors trying to
  775. * read unwritten blocks, but the unwritten blocks need not
  776. * be contiguous.
  777. */
  778. if (p == 0) {
  779. print("%lld not written yet; can't read\n", (Wideoff)a);
  780. continue;
  781. }
  782. if (to != 0 && devwrite(to, p->addr, p->iobuf) != 0) {
  783. print("out block %lld: write error; bailing",
  784. (Wideoff)a);
  785. break;
  786. }
  787. putbuf(p);
  788. if(a % 20000 == 0)
  789. print("block %lld %T\n", (Wideoff)a, time());
  790. }
  791. /*
  792. * wrap up: sync target
  793. */
  794. print("copied %lld blocks from %Z to %Z\n", (Wideoff)a, from, to);
  795. sync("devcopy");
  796. return 0;
  797. }
  798. void
  799. getline(char *line)
  800. {
  801. char *p;
  802. int c;
  803. p = line;
  804. for(;;) {
  805. c = rawchar(0);
  806. if(c == 0 || c == '\n') {
  807. *p = 0;
  808. return;
  809. }
  810. if(c == '\b') {
  811. p--;
  812. continue;
  813. }
  814. *p++ = c;
  815. }
  816. }
  817. void
  818. arginit(void)
  819. {
  820. int verb, c;
  821. char line[2*Maxword], word[Maxword+1], *cp;
  822. uchar localip[Pasize];
  823. Filsys *fs;
  824. if(nvrcheck() == 0){
  825. print("for config mode hit a key within 5 seconds\n");
  826. c = rawchar(5);
  827. if(c == 0) {
  828. print(" no config\n");
  829. return;
  830. }
  831. }
  832. for (;;) {
  833. print("config: ");
  834. getline(line);
  835. cp = getwd(word, line);
  836. if (word[0] == '\0' || word[0] == '#')
  837. continue;
  838. if(strcmp(word, "end") == 0)
  839. return;
  840. if(strcmp(word, "halt") == 0) {
  841. floppyhalt();
  842. exit();
  843. }
  844. if(strcmp(word, "allow") == 0) {
  845. wstatallow = 1;
  846. writeallow = 1;
  847. continue;
  848. }
  849. if(strcmp(word, "copyworm") == 0) {
  850. copyworm = 1;
  851. continue;
  852. }
  853. if(strcmp(word, "copydev") == 0) { /* not yet documented */
  854. cp = getwd(word, cp);
  855. if(testconfig(word))
  856. continue;
  857. src = strdup(word);
  858. getwd(word, cp);
  859. if(testconfig(word))
  860. continue;
  861. dest = strdup(word);
  862. copydev = 1;
  863. continue;
  864. }
  865. if(strcmp(word, "noauth") == 0) {
  866. noauth = !noauth;
  867. continue;
  868. }
  869. if(strcmp(word, "noattach") == 0) {
  870. noattach = !noattach;
  871. continue;
  872. }
  873. if(strcmp(word, "readonly") == 0) {
  874. readonly = 1;
  875. continue;
  876. }
  877. if(strcmp(word, "ream") == 0) {
  878. verb = FREAM;
  879. goto gfsname;
  880. }
  881. if(strcmp(word, "recover") == 0) {
  882. verb = FRECOVER;
  883. goto gfsname;
  884. }
  885. if(strcmp(word, "filsys") == 0) {
  886. verb = FEDIT;
  887. goto gfsname;
  888. }
  889. if(strcmp(word, "nvram") == 0) {
  890. getwd(word, cp);
  891. if(testconfig(word))
  892. continue;
  893. /* if it fails, it will complain */
  894. nvrsetconfig(word);
  895. continue;
  896. }
  897. if(strcmp(word, "config") == 0) {
  898. getwd(word, cp);
  899. if(!testconfig(word) && nvrsetconfig(word) == 0)
  900. f.newconf = 1;
  901. continue;
  902. }
  903. if(strcmp(word, "service") == 0) {
  904. getwd(word, cp);
  905. strcpy(service, word);
  906. f.modconf = 1;
  907. continue;
  908. }
  909. if(strcmp(word, "ipauth") == 0) {
  910. f.ipauthset = 1;
  911. verb = 2;
  912. } else if(astrcmp(word, "ip") == 0)
  913. verb = 0;
  914. else if(astrcmp(word, "ipgw") == 0)
  915. verb = 1;
  916. else if(astrcmp(word, "ipmask") == 0)
  917. verb = 3;
  918. else if(astrcmp(word, "ipsntp") == 0)
  919. verb = 4;
  920. else {
  921. print("unknown config command\n");
  922. print(" type end to get out\n");
  923. continue;
  924. }
  925. getwd(word, cp);
  926. if(chartoip(localip, word)) {
  927. print("bad ip address\n");
  928. continue;
  929. }
  930. switch(verb) {
  931. case 0:
  932. memmove(ipaddr[aindex].sysip, localip,
  933. sizeof(ipaddr[aindex].sysip));
  934. break;
  935. case 1:
  936. memmove(ipaddr[aindex].defgwip, localip,
  937. sizeof(ipaddr[aindex].defgwip));
  938. break;
  939. case 2:
  940. memmove(authip, localip, sizeof(authip));
  941. break;
  942. case 3:
  943. memmove(ipaddr[aindex].defmask, localip,
  944. sizeof(ipaddr[aindex].defmask));
  945. break;
  946. case 4:
  947. memmove(sntpip, localip, sizeof(sntpip));
  948. break;
  949. }
  950. f.modconf = 1;
  951. continue;
  952. gfsname:
  953. cp = getwd(word, cp);
  954. for(fs=filsys; fs->name; fs++)
  955. if(strcmp(word, fs->name) == 0)
  956. break;
  957. if (fs->name == nil) {
  958. memset(fs, 0, sizeof(*fs));
  959. fs->name = strdup(word);
  960. }
  961. switch(verb) {
  962. case FREAM:
  963. if(strcmp(fs->name, "main") == 0)
  964. wstatallow = 1; /* only set, never reset */
  965. case FRECOVER:
  966. fs->flags |= verb;
  967. break;
  968. case FEDIT:
  969. f.modconf = 1;
  970. getwd(word, cp);
  971. fs->flags |= verb;
  972. if(word[0] == 0)
  973. fs->conf = nil;
  974. else if(!testconfig(word))
  975. fs->conf = strdup(word);
  976. break;
  977. }
  978. }
  979. }