9fsys.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478
  1. #include "stdinc.h"
  2. #include "dat.h"
  3. #include "fns.h"
  4. #include "9.h"
  5. typedef struct Fsys Fsys;
  6. typedef struct Fsys {
  7. VtLock* lock;
  8. char* name;
  9. char* dev;
  10. char* venti;
  11. Fs* fs;
  12. VtSession* session;
  13. int ref;
  14. int noauth;
  15. int noperm;
  16. int wstatallow;
  17. Fsys* next;
  18. } Fsys;
  19. static struct {
  20. VtLock* lock;
  21. Fsys* head;
  22. Fsys* tail;
  23. char* curfsys;
  24. } sbox;
  25. static char *_argv0;
  26. #define argv0 _argv0
  27. static char EFsysBusy[] = "fsys: '%s' busy";
  28. static char EFsysExists[] = "fsys: '%s' already exists";
  29. static char EFsysNoCurrent[] = "fsys: no current fsys";
  30. static char EFsysNotFound[] = "fsys: '%s' not found";
  31. static char EFsysNotOpen[] = "fsys: '%s' not open";
  32. static Fsys*
  33. _fsysGet(char* name)
  34. {
  35. Fsys *fsys;
  36. if(name == nil || name[0] == '\0')
  37. name = "main";
  38. vtRLock(sbox.lock);
  39. for(fsys = sbox.head; fsys != nil; fsys = fsys->next){
  40. if(strcmp(name, fsys->name) == 0){
  41. fsys->ref++;
  42. break;
  43. }
  44. }
  45. if(fsys == nil)
  46. vtSetError(EFsysNotFound, name);
  47. vtRUnlock(sbox.lock);
  48. return fsys;
  49. }
  50. static int
  51. cmdPrintConfig(int argc, char* argv[])
  52. {
  53. Fsys *fsys;
  54. char *usage = "usage: printconfig";
  55. ARGBEGIN{
  56. default:
  57. return cliError(usage);
  58. }ARGEND
  59. if(argc)
  60. return cliError(usage);
  61. vtRLock(sbox.lock);
  62. for(fsys = sbox.head; fsys != nil; fsys = fsys->next){
  63. consPrint("\tfsys %s config %s\n", fsys->name, fsys->dev);
  64. if(fsys->venti && fsys->venti[0])
  65. consPrint("\tfsys %s venti %q\n", fsys->name, fsys->venti);
  66. }
  67. vtRUnlock(sbox.lock);
  68. return 1;
  69. }
  70. Fsys*
  71. fsysGet(char* name)
  72. {
  73. Fsys *fsys;
  74. if((fsys = _fsysGet(name)) == nil)
  75. return nil;
  76. vtLock(fsys->lock);
  77. if(fsys->fs == nil){
  78. vtSetError(EFsysNotOpen, fsys->name);
  79. vtUnlock(fsys->lock);
  80. fsysPut(fsys);
  81. return nil;
  82. }
  83. vtUnlock(fsys->lock);
  84. return fsys;
  85. }
  86. char*
  87. fsysGetName(Fsys* fsys)
  88. {
  89. return fsys->name;
  90. }
  91. Fsys*
  92. fsysIncRef(Fsys* fsys)
  93. {
  94. vtLock(sbox.lock);
  95. fsys->ref++;
  96. vtUnlock(sbox.lock);
  97. return fsys;
  98. }
  99. void
  100. fsysPut(Fsys* fsys)
  101. {
  102. vtLock(sbox.lock);
  103. assert(fsys->ref > 0);
  104. fsys->ref--;
  105. vtUnlock(sbox.lock);
  106. }
  107. Fs*
  108. fsysGetFs(Fsys* fsys)
  109. {
  110. assert(fsys != nil && fsys->fs != nil);
  111. return fsys->fs;
  112. }
  113. void
  114. fsysFsRlock(Fsys* fsys)
  115. {
  116. vtRLock(fsys->fs->elk);
  117. }
  118. void
  119. fsysFsRUnlock(Fsys* fsys)
  120. {
  121. vtRUnlock(fsys->fs->elk);
  122. }
  123. int
  124. fsysNoAuthCheck(Fsys* fsys)
  125. {
  126. return fsys->noauth;
  127. }
  128. int
  129. fsysNoPermCheck(Fsys* fsys)
  130. {
  131. return fsys->noperm;
  132. }
  133. int
  134. fsysWstatAllow(Fsys* fsys)
  135. {
  136. return fsys->wstatallow;
  137. }
  138. static char modechars[] = "YUGalLdHSATs";
  139. static ulong modebits[] = {
  140. ModeSticky,
  141. ModeSetUid,
  142. ModeSetGid,
  143. ModeAppend,
  144. ModeExclusive,
  145. ModeLink,
  146. ModeDir,
  147. ModeHidden,
  148. ModeSystem,
  149. ModeArchive,
  150. ModeTemporary,
  151. ModeSnapshot,
  152. 0
  153. };
  154. char*
  155. fsysModeString(ulong mode, char *buf)
  156. {
  157. int i;
  158. char *p;
  159. p = buf;
  160. for(i=0; modebits[i]; i++)
  161. if(mode & modebits[i])
  162. *p++ = modechars[i];
  163. sprint(p, "%luo", mode&0777);
  164. return buf;
  165. }
  166. int
  167. fsysParseMode(char* s, ulong* mode)
  168. {
  169. ulong x, y;
  170. char *p;
  171. x = 0;
  172. for(; *s < '0' || *s > '9'; s++){
  173. if(*s == 0)
  174. return 0;
  175. p = strchr(modechars, *s);
  176. if(p == nil)
  177. return 0;
  178. x |= modebits[p-modechars];
  179. }
  180. y = strtoul(s, &p, 8);
  181. if(*p != '\0' || y > 0777)
  182. return 0;
  183. *mode = x|y;
  184. return 1;
  185. }
  186. File*
  187. fsysGetRoot(Fsys* fsys, char* name)
  188. {
  189. File *root, *sub;
  190. assert(fsys != nil && fsys->fs != nil);
  191. root = fsGetRoot(fsys->fs);
  192. if(name == nil || strcmp(name, "") == 0)
  193. return root;
  194. sub = fileWalk(root, name);
  195. fileDecRef(root);
  196. return sub;
  197. }
  198. static Fsys*
  199. fsysAlloc(char* name, char* dev)
  200. {
  201. Fsys *fsys;
  202. vtLock(sbox.lock);
  203. for(fsys = sbox.head; fsys != nil; fsys = fsys->next){
  204. if(strcmp(fsys->name, name) != 0)
  205. continue;
  206. vtSetError(EFsysExists, name);
  207. vtUnlock(sbox.lock);
  208. return nil;
  209. }
  210. fsys = vtMemAllocZ(sizeof(Fsys));
  211. fsys->lock = vtLockAlloc();
  212. fsys->name = vtStrDup(name);
  213. fsys->dev = vtStrDup(dev);
  214. fsys->ref = 1;
  215. if(sbox.tail != nil)
  216. sbox.tail->next = fsys;
  217. else
  218. sbox.head = fsys;
  219. sbox.tail = fsys;
  220. vtUnlock(sbox.lock);
  221. return fsys;
  222. }
  223. static int
  224. fsysClose(Fsys* fsys, int argc, char* argv[])
  225. {
  226. char *usage = "usage: [fsys name] close";
  227. ARGBEGIN{
  228. default:
  229. return cliError(usage);
  230. }ARGEND
  231. if(argc)
  232. return cliError(usage);
  233. return cliError("close isn't working yet; sync and then kill fossil");
  234. /*
  235. * Oooh. This could be hard. What if fsys->ref != 1?
  236. * Also, fsClose() either does the job or panics, can we
  237. * gracefully detect it's still busy?
  238. *
  239. * More thought and care needed here.
  240. */
  241. fsClose(fsys->fs);
  242. fsys->fs = nil;
  243. vtClose(fsys->session);
  244. fsys->session = nil;
  245. if(sbox.curfsys != nil && strcmp(fsys->name, sbox.curfsys) == 0){
  246. sbox.curfsys = nil;
  247. consPrompt(nil);
  248. }
  249. return 1;
  250. }
  251. static int
  252. fsysVac(Fsys* fsys, int argc, char* argv[])
  253. {
  254. uchar score[VtScoreSize];
  255. char *usage = "usage: [fsys name] vac path";
  256. ARGBEGIN{
  257. default:
  258. return cliError(usage);
  259. }ARGEND
  260. if(argc != 1)
  261. return cliError(usage);
  262. if(!fsVac(fsys->fs, argv[0], score))
  263. return 0;
  264. consPrint("vac:%V\n", score);
  265. return 1;
  266. }
  267. static int
  268. fsysSnap(Fsys* fsys, int argc, char* argv[])
  269. {
  270. int doarchive;
  271. char *usage = "usage: [fsys name] snap [-a]";
  272. doarchive = 0;
  273. ARGBEGIN{
  274. default:
  275. return cliError(usage);
  276. case 'a':
  277. doarchive = 1;
  278. break;
  279. }ARGEND
  280. if(argc)
  281. return cliError(usage);
  282. if(!fsSnapshot(fsys->fs, doarchive))
  283. return 0;
  284. return 1;
  285. }
  286. static int
  287. fsysSnapTime(Fsys* fsys, int argc, char* argv[])
  288. {
  289. char buf[40], *x;
  290. int hh, mm;
  291. u32int arch, snap;
  292. char *usage = "usage: [fsys name] snaptime [-a hhmm] [-s minutes]";
  293. snapGetTimes(fsys->fs->snap, &arch, &snap);
  294. ARGBEGIN{
  295. case 'a':
  296. x = ARGF();
  297. if(x == nil)
  298. return cliError(usage);
  299. if(strcmp(x, "none") == 0){
  300. arch = ~(u32int)0;
  301. break;
  302. }
  303. if(strlen(x) != 4 || strspn(x, "0123456789") != 4)
  304. return cliError(usage);
  305. hh = (x[0]-'0')*10 + x[1]-'0';
  306. mm = (x[2]-'0')*10 + x[3]-'0';
  307. if(hh >= 24 || mm >= 60)
  308. return cliError(usage);
  309. arch = hh*60+mm;
  310. break;
  311. case 's':
  312. x = ARGF();
  313. if(x == nil)
  314. return cliError(usage);
  315. if(strcmp(x, "none") == 0){
  316. snap = ~(u32int)0;
  317. break;
  318. }
  319. snap = atoi(x);
  320. break;
  321. default:
  322. return cliError(usage);
  323. }ARGEND
  324. if(argc > 0)
  325. return cliError(usage);
  326. snapSetTimes(fsys->fs->snap, arch, snap);
  327. snapGetTimes(fsys->fs->snap, &arch, &snap);
  328. if(arch != ~(u32int)0)
  329. sprint(buf, "-a %02d%02d", arch/60, arch%60);
  330. else
  331. sprint(buf, "-a none");
  332. if(snap != ~(u32int)0)
  333. sprint(buf+strlen(buf), " -s %d", snap);
  334. else
  335. sprint(buf+strlen(buf), " -s none");
  336. consPrint("\tsnaptime %s\n", buf);
  337. return 1;
  338. }
  339. static int
  340. fsysSync(Fsys* fsys, int argc, char* argv[])
  341. {
  342. char *usage = "usage: [fsys name] sync";
  343. ARGBEGIN{
  344. default:
  345. return cliError(usage);
  346. }ARGEND
  347. if(argc > 0)
  348. return cliError(usage);
  349. fsSync(fsys->fs);
  350. return 1;
  351. }
  352. static int
  353. fsysRemove(Fsys* fsys, int argc, char* argv[])
  354. {
  355. File *file;
  356. char *usage = "usage: [fsys name] remove path ...";
  357. ARGBEGIN{
  358. default:
  359. return cliError(usage);
  360. }ARGEND
  361. if(argc == 0)
  362. return cliError(usage);
  363. vtRLock(fsys->fs->elk);
  364. while(argc > 0){
  365. if((file = fileOpen(fsys->fs, argv[0])) == nil)
  366. consPrint("%s: %R\n", argv[0]);
  367. else{
  368. if(!fileRemove(file, uidadm))
  369. consPrint("%s: %R\n", argv[0]);
  370. fileDecRef(file);
  371. }
  372. argc--;
  373. argv++;
  374. }
  375. vtRUnlock(fsys->fs->elk);
  376. return 1;
  377. }
  378. static int
  379. fsysClri(Fsys* fsys, int argc, char* argv[])
  380. {
  381. char *usage = "usage: [fsys name] clri path ...";
  382. ARGBEGIN{
  383. default:
  384. return cliError(usage);
  385. }ARGEND
  386. if(argc == 0)
  387. return cliError(usage);
  388. vtRLock(fsys->fs->elk);
  389. while(argc > 0){
  390. if(!fileClri(fsys->fs, argv[0], uidadm))
  391. consPrint("clri %s: %R\n", argv[0]);
  392. argc--;
  393. argv++;
  394. }
  395. vtRUnlock(fsys->fs->elk);
  396. return 1;
  397. }
  398. /*
  399. * Inspect and edit the labels for blocks on disk.
  400. */
  401. static int
  402. fsysLabel(Fsys* fsys, int argc, char* argv[])
  403. {
  404. Fs *fs;
  405. Label l;
  406. int n, r;
  407. u32int addr;
  408. Block *b, *bb;
  409. char *usage = "usage: [fsys name] label addr [type state epoch epochClose tag]";
  410. ARGBEGIN{
  411. default:
  412. return cliError(usage);
  413. }ARGEND
  414. if(argc != 1 && argc != 6)
  415. return cliError(usage);
  416. r = 0;
  417. vtRLock(fsys->fs->elk);
  418. fs = fsys->fs;
  419. addr = strtoul(argv[0], 0, 0);
  420. b = cacheLocal(fs->cache, PartData, addr, OReadOnly);
  421. if(b == nil)
  422. goto Out0;
  423. l = b->l;
  424. consPrint("%slabel %#ux %ud %ud %ud %ud %#x\n",
  425. argc==6 ? "old: " : "", addr, l.type, l.state,
  426. l.epoch, l.epochClose, l.tag);
  427. if(argc == 6){
  428. if(strcmp(argv[1], "-") != 0)
  429. l.type = atoi(argv[1]);
  430. if(strcmp(argv[2], "-") != 0)
  431. l.state = atoi(argv[2]);
  432. if(strcmp(argv[3], "-") != 0)
  433. l.epoch = strtoul(argv[3], 0, 0);
  434. if(strcmp(argv[4], "-") != 0)
  435. l.epochClose = strtoul(argv[4], 0, 0);
  436. if(strcmp(argv[5], "-") != 0)
  437. l.tag = strtoul(argv[5], 0, 0);
  438. consPrint("new: label %#ux %ud %ud %ud %ud %#x\n",
  439. addr, l.type, l.state, l.epoch, l.epochClose, l.tag);
  440. bb = _blockSetLabel(b, &l);
  441. if(bb == nil)
  442. goto Out1;
  443. n = 0;
  444. for(;;){
  445. if(blockWrite(bb)){
  446. while(bb->iostate != BioClean){
  447. assert(bb->iostate == BioWriting);
  448. vtSleep(bb->ioready);
  449. }
  450. break;
  451. }
  452. consPrint("blockWrite: %R\n");
  453. if(n++ >= 5){
  454. consPrint("giving up\n");
  455. break;
  456. }
  457. sleep(5*1000);
  458. }
  459. blockPut(bb);
  460. }
  461. r = 1;
  462. Out1:
  463. blockPut(b);
  464. Out0:
  465. vtRUnlock(fs->elk);
  466. return r;
  467. }
  468. /*
  469. * Inspect and edit the blocks on disk.
  470. */
  471. static int
  472. fsysBlock(Fsys* fsys, int argc, char* argv[])
  473. {
  474. Fs *fs;
  475. char *s;
  476. Block *b;
  477. uchar *buf;
  478. u32int addr;
  479. int c, count, i, offset;
  480. char *usage = "usage: [fsys name] block addr offset [count [data]]";
  481. ARGBEGIN{
  482. default:
  483. return cliError(usage);
  484. }ARGEND
  485. if(argc < 2 || argc > 4)
  486. return cliError(usage);
  487. fs = fsys->fs;
  488. addr = strtoul(argv[0], 0, 0);
  489. offset = strtoul(argv[1], 0, 0);
  490. if(offset < 0 || offset >= fs->blockSize){
  491. vtSetError("bad offset");
  492. return 0;
  493. }
  494. if(argc > 2)
  495. count = strtoul(argv[2], 0, 0);
  496. else
  497. count = 100000000;
  498. if(offset+count > fs->blockSize)
  499. count = fs->blockSize - count;
  500. vtRLock(fs->elk);
  501. b = cacheLocal(fs->cache, PartData, addr, argc==4 ? OReadWrite : OReadOnly);
  502. if(b == nil){
  503. vtSetError("cacheLocal %#ux: %R", addr);
  504. vtRUnlock(fs->elk);
  505. return 0;
  506. }
  507. consPrint("\t%sblock %#ux %ud %ud %.*H\n",
  508. argc==4 ? "old: " : "", addr, offset, count, count, b->data+offset);
  509. if(argc == 4){
  510. s = argv[3];
  511. if(strlen(s) != 2*count){
  512. vtSetError("bad data count");
  513. goto Out;
  514. }
  515. buf = vtMemAllocZ(count);
  516. for(i = 0; i < count*2; i++){
  517. if(s[i] >= '0' && s[i] <= '9')
  518. c = s[i] - '0';
  519. else if(s[i] >= 'a' && s[i] <= 'f')
  520. c = s[i] - 'a' + 10;
  521. else if(s[i] >= 'A' && s[i] <= 'F')
  522. c = s[i] - 'A' + 10;
  523. else{
  524. vtSetError("bad hex");
  525. vtMemFree(buf);
  526. goto Out;
  527. }
  528. if((i & 1) == 0)
  529. c <<= 4;
  530. buf[i>>1] |= c;
  531. }
  532. memmove(b->data+offset, buf, count);
  533. consPrint("\tnew: block %#ux %ud %ud %.*H\n",
  534. addr, offset, count, count, b->data+offset);
  535. blockDirty(b);
  536. }
  537. Out:
  538. blockPut(b);
  539. vtRUnlock(fs->elk);
  540. return 1;
  541. }
  542. /*
  543. * Free a disk block.
  544. */
  545. static int
  546. fsysBfree(Fsys* fsys, int argc, char* argv[])
  547. {
  548. Fs *fs;
  549. Label l;
  550. char *p;
  551. Block *b;
  552. u32int addr;
  553. char *usage = "usage: [fsys name] bfree addr ...";
  554. ARGBEGIN{
  555. default:
  556. return cliError(usage);
  557. }ARGEND
  558. if(argc == 0)
  559. return cliError(usage);
  560. fs = fsys->fs;
  561. vtRLock(fs->elk);
  562. while(argc > 0){
  563. addr = strtoul(argv[0], &p, 0);
  564. if(*p != '\0'){
  565. consPrint("bad address - '%s'\n", addr);
  566. /* syntax error; let's stop */
  567. vtRUnlock(fs->elk);
  568. return 0;
  569. }
  570. b = cacheLocal(fs->cache, PartData, addr, OReadOnly);
  571. if(b == nil){
  572. consPrint("loading %#ux: %R\n", addr);
  573. continue;
  574. }
  575. l = b->l;
  576. consPrint("label %#ux %ud %ud %ud %ud %#x\n",
  577. addr, l.type, l.state, l.epoch, l.epochClose, l.tag);
  578. l.state = BsFree;
  579. l.type = BtMax;
  580. l.tag = 0;
  581. l.epoch = 0;
  582. l.epochClose = 0;
  583. if(!blockSetLabel(b, &l))
  584. consPrint("freeing %#ux: %R\n", addr);
  585. blockPut(b);
  586. argc--;
  587. argv++;
  588. }
  589. vtRUnlock(fs->elk);
  590. return 1;
  591. }
  592. /*
  593. * Zero an entry or a pointer.
  594. */
  595. static int
  596. fsysClrep(Fsys* fsys, int argc, char* argv[], int ch)
  597. {
  598. Fs *fs;
  599. Entry e;
  600. Block *b;
  601. u32int addr;
  602. int i, max, offset, sz;
  603. uchar zero[VtEntrySize];
  604. char *usage = "usage: [fsys name] clr%c addr offset ...";
  605. ARGBEGIN{
  606. default:
  607. return cliError(usage, ch);
  608. }ARGEND
  609. if(argc < 2)
  610. return cliError(usage, ch);
  611. fs = fsys->fs;
  612. vtRLock(fsys->fs->elk);
  613. addr = strtoul(argv[0], 0, 0);
  614. b = cacheLocal(fs->cache, PartData, addr, argc==4 ? OReadWrite : OReadOnly);
  615. if(b == nil){
  616. vtSetError("cacheLocal %#ux: %R", addr);
  617. Err:
  618. vtRUnlock(fsys->fs->elk);
  619. return 0;
  620. }
  621. switch(ch){
  622. default:
  623. vtSetError("clrep");
  624. goto Err;
  625. case 'e':
  626. if(b->l.type != BtDir){
  627. vtSetError("wrong block type");
  628. goto Err;
  629. }
  630. sz = VtEntrySize;
  631. memset(&e, 0, sizeof e);
  632. entryPack(&e, zero, 0);
  633. break;
  634. case 'p':
  635. if(b->l.type == BtDir || b->l.type == BtData){
  636. vtSetError("wrong block type");
  637. goto Err;
  638. }
  639. sz = VtScoreSize;
  640. memmove(zero, vtZeroScore, VtScoreSize);
  641. break;
  642. }
  643. max = fs->blockSize/sz;
  644. for(i = 1; i < argc; i++){
  645. offset = atoi(argv[i]);
  646. if(offset >= max){
  647. consPrint("\toffset %d too large (>= %d)\n", i, max);
  648. continue;
  649. }
  650. consPrint("\tblock %#ux %d %d %.*H\n", addr, offset*sz, sz, sz, b->data+offset*sz);
  651. memmove(b->data+offset*sz, zero, sz);
  652. }
  653. blockDirty(b);
  654. blockPut(b);
  655. vtRUnlock(fsys->fs->elk);
  656. return 1;
  657. }
  658. static int
  659. fsysClre(Fsys* fsys, int argc, char* argv[])
  660. {
  661. return fsysClrep(fsys, argc, argv, 'e');
  662. }
  663. static int
  664. fsysClrp(Fsys* fsys, int argc, char* argv[])
  665. {
  666. return fsysClrep(fsys, argc, argv, 'p');
  667. }
  668. static int
  669. fsysEsearch1(File* f, char* s, u32int elo)
  670. {
  671. int n, r;
  672. DirEntry de;
  673. DirEntryEnum *dee;
  674. File *ff;
  675. Entry e, ee;
  676. char *t;
  677. dee = deeOpen(f);
  678. if(dee == nil)
  679. return 0;
  680. n = 0;
  681. for(;;){
  682. r = deeRead(dee, &de);
  683. if(r < 0){
  684. consPrint("\tdeeRead %s/%s: %R\n", s, de.elem);
  685. break;
  686. }
  687. if(r == 0)
  688. break;
  689. if(de.mode & ModeSnapshot){
  690. if((ff = fileWalk(f, de.elem)) == nil)
  691. consPrint("\tcannot walk %s/%s: %R\n", s, de.elem);
  692. else{
  693. if(!fileGetSources(ff, &e, &ee, 0))
  694. consPrint("\tcannot get sources for %s/%s: %R\n", s, de.elem);
  695. else if(e.snap != 0 && e.snap < elo){
  696. consPrint("\t%ud\tclri %s/%s\n", e.snap, s, de.elem);
  697. n++;
  698. }
  699. fileDecRef(ff);
  700. }
  701. }
  702. else if(de.mode & ModeDir){
  703. if((ff = fileWalk(f, de.elem)) == nil)
  704. consPrint("\tcannot walk %s/%s: %R\n", s, de.elem);
  705. else{
  706. t = smprint("%s/%s", s, de.elem);
  707. n += fsysEsearch1(ff, t, elo);
  708. vtMemFree(t);
  709. fileDecRef(ff);
  710. }
  711. }
  712. deCleanup(&de);
  713. if(r < 0)
  714. break;
  715. }
  716. deeClose(dee);
  717. return n;
  718. }
  719. static int
  720. fsysEsearch(Fs* fs, char* path, u32int elo)
  721. {
  722. int n;
  723. File *f;
  724. DirEntry de;
  725. f = fileOpen(fs, path);
  726. if(f == nil)
  727. return 0;
  728. if(!fileGetDir(f, &de)){
  729. consPrint("\tfileGetDir %s failed: %R\n", path);
  730. fileDecRef(f);
  731. return 0;
  732. }
  733. if((de.mode & ModeDir) == 0){
  734. fileDecRef(f);
  735. deCleanup(&de);
  736. return 0;
  737. }
  738. deCleanup(&de);
  739. n = fsysEsearch1(f, path, elo);
  740. fileDecRef(f);
  741. return n;
  742. }
  743. static int
  744. fsysEpoch(Fsys* fsys, int argc, char* argv[])
  745. {
  746. Fs *fs;
  747. int force, n;
  748. u32int low, old;
  749. char *usage = "usage: [fsys name] epoch [[-y] low]";
  750. force = 0;
  751. ARGBEGIN{
  752. case 'y':
  753. force = 1;
  754. break;
  755. default:
  756. return cliError(usage);
  757. }ARGEND
  758. if(argc > 1)
  759. return cliError(usage);
  760. if(argc > 0)
  761. low = strtoul(argv[0], 0, 0);
  762. else
  763. low = ~(u32int)0;
  764. fs = fsys->fs;
  765. vtRLock(fs->elk);
  766. consPrint("\tlow %ud hi %ud\n", fs->elo, fs->ehi);
  767. n = fsysEsearch(fsys->fs, "/archive", low);
  768. n += fsysEsearch(fsys->fs, "/snapshot", low);
  769. consPrint("\t%d snapshot%s found with epoch < %ud\n", n, n==1 ? "" : "s", low);
  770. vtRUnlock(fs->elk);
  771. /*
  772. * There's a small race here -- a new snapshot with epoch < low might
  773. * get introduced now that we unlocked fs->elk. Low has to
  774. * be <= fs->ehi. Of course, in order for this to happen low has
  775. * to be equal to the current fs->ehi _and_ a snapshot has to
  776. * run right now. This is a small enough window that I don't care.
  777. */
  778. if(n != 0 && !force){
  779. consPrint("\tnot setting low epoch\n");
  780. return 1;
  781. }
  782. old = fs->elo;
  783. if(!fsEpochLow(fs, low))
  784. consPrint("\tfsEpochLow: %R\n");
  785. else{
  786. consPrint("\told: epoch%s %ud\n", force ? " -y" : "", old);
  787. consPrint("\tnew: epoch%s %ud\n", force ? " -y" : "", fs->elo);
  788. if(fs->elo < low)
  789. consPrint("\twarning: new low epoch < old low epoch\n");
  790. }
  791. return 1;
  792. }
  793. static int
  794. fsysCreate(Fsys* fsys, int argc, char* argv[])
  795. {
  796. int r;
  797. ulong mode;
  798. char *elem, *p, *path;
  799. char *usage = "usage: [fsys name] create path uid gid perm";
  800. DirEntry de;
  801. File *file, *parent;
  802. ARGBEGIN{
  803. default:
  804. return cliError(usage);
  805. }ARGEND
  806. if(argc != 4)
  807. return cliError(usage);
  808. if(!fsysParseMode(argv[3], &mode))
  809. return cliError(usage);
  810. if(mode&ModeSnapshot)
  811. return cliError("create - cannot create with snapshot bit set");
  812. if(strcmp(argv[1], uidnoworld) == 0)
  813. return cliError("permission denied");
  814. vtRLock(fsys->fs->elk);
  815. path = vtStrDup(argv[0]);
  816. if((p = strrchr(path, '/')) != nil){
  817. *p++ = '\0';
  818. elem = p;
  819. p = path;
  820. if(*p == '\0')
  821. p = "/";
  822. }
  823. else{
  824. p = "/";
  825. elem = path;
  826. }
  827. r = 0;
  828. if((parent = fileOpen(fsys->fs, p)) == nil)
  829. goto out;
  830. file = fileCreate(parent, elem, mode, argv[1]);
  831. fileDecRef(parent);
  832. if(file == nil){
  833. vtSetError("create %s/%s: %R", p, elem);
  834. goto out;
  835. }
  836. if(!fileGetDir(file, &de)){
  837. vtSetError("stat failed after create: %R");
  838. goto out1;
  839. }
  840. if(strcmp(de.gid, argv[2]) != 0){
  841. vtMemFree(de.gid);
  842. de.gid = vtStrDup(argv[2]);
  843. if(!fileSetDir(file, &de, argv[1])){
  844. vtSetError("wstat failed after create: %R");
  845. goto out2;
  846. }
  847. }
  848. r = 1;
  849. out2:
  850. deCleanup(&de);
  851. out1:
  852. fileDecRef(file);
  853. out:
  854. vtMemFree(path);
  855. vtRUnlock(fsys->fs->elk);
  856. return r;
  857. }
  858. static void
  859. fsysPrintStat(char *prefix, char *file, DirEntry *de)
  860. {
  861. char buf[64];
  862. if(prefix == nil)
  863. prefix = "";
  864. consPrint("%sstat %q %q %q %q %s %llud\n", prefix,
  865. file, de->elem, de->uid, de->gid, fsysModeString(de->mode, buf), de->size);
  866. }
  867. static int
  868. fsysStat(Fsys* fsys, int argc, char* argv[])
  869. {
  870. int i;
  871. File *f;
  872. DirEntry de;
  873. char *usage = "usage: [fsys name] stat files...";
  874. ARGBEGIN{
  875. default:
  876. return cliError(usage);
  877. }ARGEND
  878. if(argc == 0)
  879. return cliError(usage);
  880. vtRLock(fsys->fs->elk);
  881. for(i=0; i<argc; i++){
  882. if((f = fileOpen(fsys->fs, argv[i])) == nil){
  883. consPrint("%s: %R\n");
  884. continue;
  885. }
  886. if(!fileGetDir(f, &de)){
  887. consPrint("%s: %R\n");
  888. fileDecRef(f);
  889. continue;
  890. }
  891. fsysPrintStat("\t", argv[i], &de);
  892. deCleanup(&de);
  893. fileDecRef(f);
  894. }
  895. vtRUnlock(fsys->fs->elk);
  896. return 1;
  897. }
  898. static int
  899. fsysWstat(Fsys *fsys, int argc, char* argv[])
  900. {
  901. File *f;
  902. char *p;
  903. DirEntry de;
  904. char *usage = "usage: [fsys name] wstat file elem uid gid mode length\n"
  905. "\tuse - for any field to mean don't change";
  906. ARGBEGIN{
  907. default:
  908. return cliError(usage);
  909. }ARGEND
  910. if(argc != 6)
  911. return cliError(usage);
  912. vtRLock(fsys->fs->elk);
  913. if((f = fileOpen(fsys->fs, argv[0])) == nil){
  914. vtSetError("console wstat - walk - %R");
  915. vtRUnlock(fsys->fs->elk);
  916. return 0;
  917. }
  918. if(!fileGetDir(f, &de)){
  919. vtSetError("console wstat - stat - %R");
  920. fileDecRef(f);
  921. vtRUnlock(fsys->fs->elk);
  922. return 0;
  923. }
  924. fsysPrintStat("\told: w", argv[0], &de);
  925. if(strcmp(argv[1], "-") != 0){
  926. if(!validFileName(argv[1])){
  927. vtSetError("console wstat - bad elem");
  928. goto error;
  929. }
  930. vtMemFree(de.elem);
  931. de.elem = vtStrDup(argv[1]);
  932. }
  933. if(strcmp(argv[2], "-") != 0){
  934. if(!validUserName(argv[2])){
  935. vtSetError("console wstat - bad uid");
  936. goto error;
  937. }
  938. vtMemFree(de.uid);
  939. de.uid = vtStrDup(argv[2]);
  940. }
  941. if(strcmp(argv[3], "-") != 0){
  942. if(!validUserName(argv[3])){
  943. vtSetError("console wstat - bad gid");
  944. goto error;
  945. }
  946. vtMemFree(de.gid);
  947. de.gid = vtStrDup(argv[3]);
  948. }
  949. if(strcmp(argv[4], "-") != 0){
  950. if(!fsysParseMode(argv[4], &de.mode)){
  951. vtSetError("console wstat - bad mode");
  952. goto error;
  953. }
  954. }
  955. if(strcmp(argv[5], "-") != 0){
  956. de.size = strtoull(argv[5], &p, 0);
  957. if(argv[5][0] == '\0' || *p != '\0' || de.size < 0){
  958. vtSetError("console wstat - bad length");
  959. goto error;
  960. }
  961. }
  962. if(!fileSetDir(f, &de, uidadm)){
  963. vtSetError("console wstat - %R");
  964. goto error;
  965. }
  966. deCleanup(&de);
  967. if(!fileGetDir(f, &de)){
  968. vtSetError("console wstat - stat2 - %R");
  969. goto error;
  970. }
  971. fsysPrintStat("\tnew: w", argv[0], &de);
  972. deCleanup(&de);
  973. fileDecRef(f);
  974. vtRUnlock(fsys->fs->elk);
  975. return 1;
  976. error:
  977. deCleanup(&de); /* okay to do this twice */
  978. fileDecRef(f);
  979. vtRUnlock(fsys->fs->elk);
  980. return 0;
  981. }
  982. static int
  983. fsysVenti(char* name, int argc, char* argv[])
  984. {
  985. int r;
  986. char *host;
  987. char *usage = "usage: [fsys name] venti [address]";
  988. Fsys *fsys;
  989. ARGBEGIN{
  990. default:
  991. return cliError(usage);
  992. }ARGEND
  993. if(argc == 0)
  994. host = nil;
  995. else if(argc == 1)
  996. host = argv[0];
  997. else
  998. return cliError(usage);
  999. if((fsys = _fsysGet(name)) == nil)
  1000. return 0;
  1001. vtLock(fsys->lock);
  1002. if(host == nil)
  1003. host = fsys->venti;
  1004. else{
  1005. vtMemFree(fsys->venti);
  1006. if(host[0])
  1007. fsys->venti = vtStrDup(host);
  1008. else{
  1009. host = nil;
  1010. fsys->venti = nil;
  1011. }
  1012. }
  1013. /* already open: do a redial */
  1014. if(fsys->fs != nil){
  1015. r = 1;
  1016. if(!vtRedial(fsys->session, host)
  1017. || !vtConnect(fsys->session, 0))
  1018. r = 0;
  1019. vtUnlock(fsys->lock);
  1020. fsysPut(fsys);
  1021. return r;
  1022. }
  1023. /* not yet open: try to dial */
  1024. if(fsys->session)
  1025. vtClose(fsys->session);
  1026. r = 1;
  1027. if((fsys->session = vtDial(host, 0)) == nil
  1028. || !vtConnect(fsys->session, 0))
  1029. r = 0;
  1030. vtUnlock(fsys->lock);
  1031. fsysPut(fsys);
  1032. return r;
  1033. }
  1034. static int
  1035. fsysOpen(char* name, int argc, char* argv[])
  1036. {
  1037. char *p, *host;
  1038. Fsys *fsys;
  1039. long ncache;
  1040. int noauth, noperm, rflag, wstatallow;
  1041. char *usage = "usage: fsys name open [-APWr] [-c ncache]";
  1042. ncache = 1000;
  1043. noauth = noperm = wstatallow = 0;
  1044. rflag = OReadWrite;
  1045. ARGBEGIN{
  1046. default:
  1047. return cliError(usage);
  1048. case 'A':
  1049. noauth = 1;
  1050. break;
  1051. case 'P':
  1052. noperm = 1;
  1053. break;
  1054. case 'W':
  1055. wstatallow = 1;
  1056. break;
  1057. case 'c':
  1058. p = ARGF();
  1059. if(p == nil)
  1060. return cliError(usage);
  1061. ncache = strtol(argv[0], &p, 0);
  1062. if(ncache <= 0 || p == argv[0] || *p != '\0')
  1063. return cliError(usage);
  1064. break;
  1065. case 'r':
  1066. rflag = OReadOnly;
  1067. break;
  1068. }ARGEND
  1069. if(argc)
  1070. return cliError(usage);
  1071. if((fsys = _fsysGet(name)) == nil)
  1072. return 0;
  1073. vtLock(fsys->lock);
  1074. if(fsys->fs != nil){
  1075. vtSetError(EFsysBusy, fsys->name);
  1076. vtUnlock(fsys->lock);
  1077. fsysPut(fsys);
  1078. return 0;
  1079. }
  1080. if(fsys->session == nil){
  1081. if(fsys->venti && fsys->venti[0])
  1082. host = fsys->venti;
  1083. else
  1084. host = nil;
  1085. fsys->session = vtDial(host, 1);
  1086. if(!vtConnect(fsys->session, nil))
  1087. fprint(2, "warning: connecting to venti: %R\n");
  1088. }
  1089. if((fsys->fs = fsOpen(fsys->dev, fsys->session, ncache, rflag)) == nil){
  1090. vtUnlock(fsys->lock);
  1091. fsysPut(fsys);
  1092. return 0;
  1093. }
  1094. fsys->noauth = noauth;
  1095. fsys->noperm = noperm;
  1096. fsys->wstatallow = wstatallow;
  1097. vtUnlock(fsys->lock);
  1098. fsysPut(fsys);
  1099. return 1;
  1100. }
  1101. static int
  1102. fsysUnconfig(char* name, int argc, char* argv[])
  1103. {
  1104. Fsys *fsys, **fp;
  1105. char *usage = "usage: fsys name unconfig";
  1106. ARGBEGIN{
  1107. default:
  1108. return cliError(usage);
  1109. }ARGEND
  1110. if(argc)
  1111. return cliError(usage);
  1112. vtLock(sbox.lock);
  1113. fp = &sbox.head;
  1114. for(fsys = *fp; fsys != nil; fsys = fsys->next){
  1115. if(strcmp(fsys->name, name) == 0)
  1116. break;
  1117. fp = &fsys->next;
  1118. }
  1119. if(fsys == nil){
  1120. vtSetError(EFsysNotFound, name);
  1121. vtUnlock(sbox.lock);
  1122. return 0;
  1123. }
  1124. if(fsys->ref != 0 || fsys->fs != nil){
  1125. vtSetError(EFsysBusy, fsys->name);
  1126. vtUnlock(sbox.lock);
  1127. return 0;
  1128. }
  1129. *fp = fsys->next;
  1130. vtUnlock(sbox.lock);
  1131. if(fsys->session != nil){
  1132. vtClose(fsys->session);
  1133. vtFree(fsys->session);
  1134. }
  1135. if(fsys->venti != nil)
  1136. vtMemFree(fsys->venti);
  1137. if(fsys->dev != nil)
  1138. vtMemFree(fsys->dev);
  1139. if(fsys->name != nil)
  1140. vtMemFree(fsys->name);
  1141. vtMemFree(fsys);
  1142. return 1;
  1143. }
  1144. static int
  1145. fsysConfig(char* name, int argc, char* argv[])
  1146. {
  1147. Fsys *fsys;
  1148. char *usage = "usage: fsys name config dev";
  1149. ARGBEGIN{
  1150. default:
  1151. return cliError(usage);
  1152. }ARGEND
  1153. if(argc != 1)
  1154. return cliError(usage);
  1155. if((fsys = _fsysGet(argv[0])) != nil){
  1156. vtLock(fsys->lock);
  1157. if(fsys->fs != nil){
  1158. vtSetError(EFsysBusy, fsys->name);
  1159. vtUnlock(fsys->lock);
  1160. fsysPut(fsys);
  1161. return 0;
  1162. }
  1163. vtMemFree(fsys->dev);
  1164. fsys->dev = vtStrDup(argv[0]);
  1165. vtUnlock(fsys->lock);
  1166. }
  1167. else if((fsys = fsysAlloc(name, argv[0])) == nil)
  1168. return 0;
  1169. fsysPut(fsys);
  1170. return 1;
  1171. }
  1172. static struct {
  1173. char* cmd;
  1174. int (*f)(Fsys*, int, char**);
  1175. int (*f1)(char*, int, char**);
  1176. } fsyscmd[] = {
  1177. { "close", fsysClose, },
  1178. { "config", nil, fsysConfig, },
  1179. { "open", nil, fsysOpen, },
  1180. { "unconfig", nil, fsysUnconfig, },
  1181. { "venti", nil, fsysVenti, },
  1182. { "bfree", fsysBfree, },
  1183. { "block", fsysBlock, },
  1184. { "clre", fsysClre, },
  1185. { "clri", fsysClri, },
  1186. { "clrp", fsysClrp, },
  1187. { "create", fsysCreate, },
  1188. { "epoch", fsysEpoch, },
  1189. { "label", fsysLabel, },
  1190. { "remove", fsysRemove, },
  1191. { "snap", fsysSnap, },
  1192. { "snaptime", fsysSnapTime, },
  1193. { "stat", fsysStat, },
  1194. { "sync", fsysSync, },
  1195. { "wstat", fsysWstat, },
  1196. { "vac", fsysVac, },
  1197. { nil, nil, },
  1198. };
  1199. static int
  1200. fsysXXX(char* name, int argc, char* argv[])
  1201. {
  1202. int i, r;
  1203. Fsys *fsys;
  1204. for(i = 0; fsyscmd[i].cmd != nil; i++){
  1205. if(strcmp(fsyscmd[i].cmd, argv[0]) == 0)
  1206. break;
  1207. }
  1208. if(fsyscmd[i].cmd == nil){
  1209. vtSetError("unknown command - '%s'", argv[0]);
  1210. return 0;
  1211. }
  1212. /* some commands want the name... */
  1213. if(fsyscmd[i].f1 != nil)
  1214. return (*fsyscmd[i].f1)(name, argc, argv);
  1215. /* ... but most commands want the Fsys */
  1216. if((fsys = _fsysGet(name)) == nil)
  1217. return 0;
  1218. vtLock(fsys->lock);
  1219. if(fsys->fs == nil){
  1220. vtUnlock(fsys->lock);
  1221. vtSetError(EFsysNotOpen, name);
  1222. fsysPut(fsys);
  1223. return 0;
  1224. }
  1225. r = (*fsyscmd[i].f)(fsys, argc, argv);
  1226. vtUnlock(fsys->lock);
  1227. fsysPut(fsys);
  1228. return r;
  1229. }
  1230. static int
  1231. cmdFsysXXX(int argc, char* argv[])
  1232. {
  1233. char *name;
  1234. if((name = sbox.curfsys) == nil){
  1235. vtSetError(EFsysNoCurrent, argv[0]);
  1236. return 0;
  1237. }
  1238. return fsysXXX(name, argc, argv);
  1239. }
  1240. static int
  1241. cmdFsys(int argc, char* argv[])
  1242. {
  1243. Fsys *fsys;
  1244. char *usage = "usage: fsys [name ...]";
  1245. ARGBEGIN{
  1246. default:
  1247. return cliError(usage);
  1248. }ARGEND
  1249. if(argc == 0){
  1250. vtRLock(sbox.lock);
  1251. for(fsys = sbox.head; fsys != nil; fsys = fsys->next)
  1252. consPrint("\t%s\n", fsys->name);
  1253. vtRUnlock(sbox.lock);
  1254. return 1;
  1255. }
  1256. if(argc == 1){
  1257. if((fsys = fsysGet(argv[0])) == nil)
  1258. return 0;
  1259. sbox.curfsys = vtStrDup(fsys->name);
  1260. consPrompt(sbox.curfsys);
  1261. fsysPut(fsys);
  1262. return 1;
  1263. }
  1264. return fsysXXX(argv[0], argc-1, argv+1);
  1265. }
  1266. int
  1267. fsysInit(void)
  1268. {
  1269. int i;
  1270. fmtinstall('H', encodefmt);
  1271. fmtinstall('V', scoreFmt);
  1272. fmtinstall('R', vtErrFmt);
  1273. fmtinstall('L', labelFmt);
  1274. sbox.lock = vtLockAlloc();
  1275. cliAddCmd("fsys", cmdFsys);
  1276. for(i = 0; fsyscmd[i].cmd != nil; i++){
  1277. if(fsyscmd[i].f != nil)
  1278. cliAddCmd(fsyscmd[i].cmd, cmdFsysXXX);
  1279. }
  1280. /* the venti cmd is special: the fs can be either open or closed */
  1281. cliAddCmd("venti", cmdFsysXXX);
  1282. cliAddCmd("printconfig", cmdPrintConfig);
  1283. return 1;
  1284. }