9fsys.c 33 KB

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