Blob


1 #include "stdinc.h"
2 #include "vac.h"
3 #include "dat.h"
4 #include "fns.h"
5 #include "error.h"
7 #define debug 0
9 /*
10 * Vac file system. This is a simplified version of the same code in Fossil.
11 *
12 * The locking order in the tree is upward: a thread can hold the lock
13 * for a VacFile and then acquire the lock of f->up (the parent),
14 * but not vice-versa.
15 *
16 * A vac file is one or two venti files. Plain data files are one venti file,
17 * while directores are two: a venti data file containing traditional
18 * directory entries, and a venti directory file containing venti
19 * directory entries. The traditional directory entries in the data file
20 * contain integers indexing into the venti directory entry file.
21 * It's a little complicated, but it makes the data usable by standard
22 * tools like venti/copy.
23 *
24 */
26 static int filemetaflush(VacFile*, char*);
28 struct VacFile
29 {
30 VacFs *fs; /* immutable */
32 /* meta data for file: protected by the lk in the parent */
33 int ref; /* holds this data structure up */
35 int partial; /* file was never really open */
36 int removed; /* file has been removed */
37 int dirty; /* dir is dirty with respect to meta data in block */
38 u32int boff; /* block offset within msource for this file's metadata */
39 VacDir dir; /* metadata for this file */
40 VacFile *up; /* parent file */
41 VacFile *next; /* sibling */
43 RWLock lk; /* lock for the following */
44 VtFile *source; /* actual data */
45 VtFile *msource; /* metadata for children in a directory */
46 VacFile *down; /* children */
47 int mode;
49 uvlong qidoffset; /* qid offset */
50 };
52 static VacFile*
53 filealloc(VacFs *fs)
54 {
55 VacFile *f;
57 f = vtmallocz(sizeof(VacFile));
58 f->ref = 1;
59 f->fs = fs;
60 f->boff = NilBlock;
61 f->mode = fs->mode;
62 return f;
63 }
65 static void
66 filefree(VacFile *f)
67 {
68 vtfileclose(f->source);
69 vtfileclose(f->msource);
70 vdcleanup(&f->dir);
71 memset(f, ~0, sizeof *f); /* paranoia */
72 vtfree(f);
73 }
75 static int
76 chksource(VacFile *f)
77 {
78 if(f->partial)
79 return 0;
81 if(f->source == nil
82 || ((f->dir.mode & ModeDir) && f->msource == nil)){
83 werrstr(ERemoved);
84 return -1;
85 }
86 return 0;
87 }
89 static int
90 filelock(VacFile *f)
91 {
92 wlock(&f->lk);
93 if(chksource(f) < 0){
94 wunlock(&f->lk);
95 return -1;
96 }
97 return 0;
98 }
100 static void
101 fileunlock(VacFile *f)
103 wunlock(&f->lk);
106 static int
107 filerlock(VacFile *f)
109 rlock(&f->lk);
110 if(chksource(f) < 0){
111 runlock(&f->lk);
112 return -1;
114 return 0;
117 static void
118 filerunlock(VacFile *f)
120 runlock(&f->lk);
123 /*
124 * The file metadata, like f->dir and f->ref,
125 * are synchronized via the parent's lock.
126 * This is why locking order goes up.
127 */
128 static void
129 filemetalock(VacFile *f)
131 assert(f->up != nil);
132 wlock(&f->up->lk);
135 static void
136 filemetaunlock(VacFile *f)
138 wunlock(&f->up->lk);
141 uvlong
142 vacfilegetid(VacFile *f)
144 /* immutable */
145 return f->qidoffset + f->dir.qid;
148 uvlong
149 vacfilegetqidoffset(VacFile *f)
151 return f->qidoffset;
154 ulong
155 vacfilegetmcount(VacFile *f)
157 ulong mcount;
159 filemetalock(f);
160 mcount = f->dir.mcount;
161 filemetaunlock(f);
162 return mcount;
165 ulong
166 vacfilegetmode(VacFile *f)
168 ulong mode;
170 filemetalock(f);
171 mode = f->dir.mode;
172 filemetaunlock(f);
173 return mode;
176 int
177 vacfileisdir(VacFile *f)
179 /* immutable */
180 return (f->dir.mode & ModeDir) != 0;
183 int
184 vacfileisroot(VacFile *f)
186 return f == f->fs->root;
189 /*
190 * The files are reference counted, and while the reference
191 * is bigger than zero, each file can be found in its parent's
192 * f->down list (chains via f->next), so that multiple threads
193 * end up sharing a VacFile* when referring to the same file.
195 * Each VacFile holds a reference to its parent.
196 */
197 VacFile*
198 vacfileincref(VacFile *vf)
200 filemetalock(vf);
201 assert(vf->ref > 0);
202 vf->ref++;
203 filemetaunlock(vf);
204 return vf;
207 int
208 vacfiledecref(VacFile *f)
210 VacFile *p, *q, **qq;
212 if(f->up == nil){
213 /* never linked in */
214 assert(f->ref == 1);
215 filefree(f);
216 return 0;
219 filemetalock(f);
220 f->ref--;
221 if(f->ref > 0){
222 filemetaunlock(f);
223 return -1;
225 assert(f->ref == 0);
226 assert(f->down == nil);
228 if(f->source && vtfilelock(f->source, -1) >= 0){
229 vtfileflush(f->source);
230 vtfileunlock(f->source);
232 if(f->msource && vtfilelock(f->msource, -1) >= 0){
233 vtfileflush(f->msource);
234 vtfileunlock(f->msource);
237 /*
238 * Flush f's directory information to the cache.
239 */
240 filemetaflush(f, nil);
242 p = f->up;
243 qq = &p->down;
244 for(q = *qq; q; q = *qq){
245 if(q == f)
246 break;
247 qq = &q->next;
249 assert(q != nil);
250 *qq = f->next;
252 filemetaunlock(f);
253 filefree(f);
254 vacfiledecref(p);
255 return 0;
259 /*
260 * Construct a vacfile for the root of a vac tree, given the
261 * venti file for the root information. That venti file is a
262 * directory file containing VtEntries for three more venti files:
263 * the two venti files making up the root directory, and a
264 * third venti file that would be the metadata half of the
265 * "root's parent".
267 * Fossil generates slightly different vac files, due to a now
268 * impossible-to-change bug, which contain a VtEntry
269 * for just one venti file, that itself contains the expected
270 * three directory entries. Sigh.
271 */
272 VacFile*
273 _vacfileroot(VacFs *fs, VtFile *r)
275 int redirected;
276 char err[ERRMAX];
277 VtBlock *b;
278 VtFile *r0, *r1, *r2;
279 MetaBlock mb;
280 MetaEntry me;
281 VacFile *root, *mr;
283 redirected = 0;
284 Top:
285 b = nil;
286 root = nil;
287 mr = nil;
288 r1 = nil;
289 r2 = nil;
291 if(vtfilelock(r, -1) < 0)
292 return nil;
293 r0 = vtfileopen(r, 0, fs->mode);
294 if(debug)
295 fprint(2, "r0 %p\n", r0);
296 if(r0 == nil)
297 goto Err;
298 r2 = vtfileopen(r, 2, fs->mode);
299 if(debug)
300 fprint(2, "r2 %p\n", r2);
301 if(r2 == nil){
302 /*
303 * some vac files (e.g., from fossil)
304 * have an extra layer of indirection.
305 */
306 rerrstr(err, sizeof err);
307 if(!redirected && strstr(err, "not active")){
308 redirected = 1;
309 vtfileunlock(r);
310 r = r0;
311 goto Top;
313 goto Err;
315 r1 = vtfileopen(r, 1, fs->mode);
316 if(debug)
317 fprint(2, "r1 %p\n", r1);
318 if(r1 == nil)
319 goto Err;
321 mr = filealloc(fs);
322 mr->msource = r2;
323 r2 = nil;
325 root = filealloc(fs);
326 root->boff = 0;
327 root->up = mr;
328 root->source = r0;
329 r0 = nil;
330 root->msource = r1;
331 r1 = nil;
333 mr->down = root;
334 vtfileunlock(r);
336 if(vtfilelock(mr->msource, VtOREAD) < 0)
337 goto Err1;
338 b = vtfileblock(mr->msource, 0, VtOREAD);
339 vtfileunlock(mr->msource);
340 if(b == nil)
341 goto Err1;
343 if(mbunpack(&mb, b->data, mr->msource->dsize) < 0)
344 goto Err1;
346 meunpack(&me, &mb, 0);
347 if(vdunpack(&root->dir, &me) < 0)
348 goto Err1;
349 vtblockput(b);
351 return root;
352 Err:
353 vtfileunlock(r);
354 Err1:
355 vtblockput(b);
356 if(r0)
357 vtfileclose(r0);
358 if(r1)
359 vtfileclose(r1);
360 if(r2)
361 vtfileclose(r2);
362 if(mr)
363 filefree(mr);
364 if(root)
365 filefree(root);
367 return nil;
370 /*
371 * Vac directories are a sequence of metablocks, each of which
372 * contains a bunch of metaentries sorted by file name.
373 * The whole sequence isn't sorted, though, so you still have
374 * to look at every block to find a given name.
375 * Dirlookup looks in f for an element name elem.
376 * It returns a new VacFile with the dir, boff, and mode
377 * filled in, but the sources (venti files) are not, and f is
378 * not yet linked into the tree. These details must be taken
379 * care of by the caller.
381 * f must be locked, f->msource must not.
382 */
383 static VacFile*
384 dirlookup(VacFile *f, char *elem)
386 int i;
387 MetaBlock mb;
388 MetaEntry me;
389 VtBlock *b;
390 VtFile *meta;
391 VacFile *ff;
392 u32int bo, nb;
394 meta = f->msource;
395 b = nil;
396 if(vtfilelock(meta, -1) < 0)
397 return nil;
398 nb = (vtfilegetsize(meta)+meta->dsize-1)/meta->dsize;
399 for(bo=0; bo<nb; bo++){
400 b = vtfileblock(meta, bo, VtOREAD);
401 if(b == nil)
402 goto Err;
403 if(mbunpack(&mb, b->data, meta->dsize) < 0)
404 goto Err;
405 if(mbsearch(&mb, elem, &i, &me) >= 0){
406 ff = filealloc(f->fs);
407 if(vdunpack(&ff->dir, &me) < 0){
408 filefree(ff);
409 goto Err;
411 ff->qidoffset = f->qidoffset + ff->dir.qidoffset;
412 vtfileunlock(meta);
413 vtblockput(b);
414 ff->boff = bo;
415 ff->mode = f->mode;
416 return ff;
418 vtblockput(b);
419 b = nil;
421 werrstr(ENoFile);
422 /* fall through */
423 Err:
424 vtfileunlock(meta);
425 vtblockput(b);
426 return nil;
429 /*
430 * Open the venti file at offset in the directory f->source.
431 * f is locked.
432 */
433 static VtFile *
434 fileopensource(VacFile *f, u32int offset, u32int gen, int dir, uint mode)
436 VtFile *r;
438 if((r = vtfileopen(f->source, offset, mode)) == nil)
439 return nil;
440 if(r == nil)
441 return nil;
442 if(r->gen != gen){
443 werrstr(ERemoved);
444 vtfileclose(r);
445 return nil;
447 if(r->dir != dir && r->mode != -1){
448 werrstr(EBadMeta);
449 vtfileclose(r);
450 return nil;
452 return r;
455 VacFile*
456 vacfilegetparent(VacFile *f)
458 if(vacfileisroot(f))
459 return vacfileincref(f);
460 return vacfileincref(f->up);
463 /*
464 * Given an unlocked vacfile (directory) f,
465 * return the vacfile named elem in f.
466 * Interprets . and .. as a convenience to callers.
467 */
468 VacFile*
469 vacfilewalk(VacFile *f, char *elem)
471 VacFile *ff;
473 if(elem[0] == 0){
474 werrstr(EBadPath);
475 return nil;
478 if(!vacfileisdir(f)){
479 werrstr(ENotDir);
480 return nil;
483 if(strcmp(elem, ".") == 0)
484 return vacfileincref(f);
486 if(strcmp(elem, "..") == 0)
487 return vacfilegetparent(f);
489 if(filelock(f) < 0)
490 return nil;
492 for(ff = f->down; ff; ff=ff->next){
493 if(strcmp(elem, ff->dir.elem) == 0 && !ff->removed){
494 ff->ref++;
495 goto Exit;
499 ff = dirlookup(f, elem);
500 if(ff == nil)
501 goto Err;
503 if(ff->dir.mode & ModeSnapshot)
504 ff->mode = VtOREAD;
506 if(vtfilelock(f->source, f->mode) < 0)
507 goto Err;
508 if(ff->dir.mode & ModeDir){
509 ff->source = fileopensource(f, ff->dir.entry, ff->dir.gen, 1, ff->mode);
510 ff->msource = fileopensource(f, ff->dir.mentry, ff->dir.mgen, 0, ff->mode);
511 if(ff->source == nil || ff->msource == nil)
512 goto Err1;
513 }else{
514 ff->source = fileopensource(f, ff->dir.entry, ff->dir.gen, 0, ff->mode);
515 if(ff->source == nil)
516 goto Err1;
518 vtfileunlock(f->source);
520 /* link in and up parent ref count */
521 ff->next = f->down;
522 f->down = ff;
523 ff->up = f;
524 vacfileincref(f);
525 Exit:
526 fileunlock(f);
527 return ff;
529 Err1:
530 vtfileunlock(f->source);
531 Err:
532 fileunlock(f);
533 if(ff != nil)
534 vacfiledecref(ff);
535 return nil;
538 /*
539 * Open a path in the vac file system:
540 * just walk each element one at a time.
541 */
542 VacFile*
543 vacfileopen(VacFs *fs, char *path)
545 VacFile *f, *ff;
546 char *p, elem[VtMaxStringSize], *opath;
547 int n;
549 f = fs->root;
550 vacfileincref(f);
551 opath = path;
552 while(*path != 0){
553 for(p = path; *p && *p != '/'; p++)
555 n = p - path;
556 if(n > 0){
557 if(n > VtMaxStringSize){
558 werrstr("%s: element too long", EBadPath);
559 goto Err;
561 memmove(elem, path, n);
562 elem[n] = 0;
563 ff = vacfilewalk(f, elem);
564 if(ff == nil){
565 werrstr("%.*s: %r", utfnlen(opath, p-opath), opath);
566 goto Err;
568 vacfiledecref(f);
569 f = ff;
571 if(*p == '/')
572 p++;
573 path = p;
575 return f;
576 Err:
577 vacfiledecref(f);
578 return nil;
581 /*
582 * Extract the score for the bn'th block in f.
583 */
584 int
585 vacfileblockscore(VacFile *f, u32int bn, u8int *score)
587 VtFile *s;
588 uvlong size;
589 int dsize, ret;
591 ret = -1;
592 if(filerlock(f) < 0)
593 return -1;
594 if(vtfilelock(f->source, VtOREAD) < 0)
595 goto out;
597 s = f->source;
598 dsize = s->dsize;
599 size = vtfilegetsize(s);
600 if((uvlong)bn*dsize >= size)
601 goto out1;
602 ret = vtfileblockscore(f->source, bn, score);
604 out1:
605 vtfileunlock(f->source);
606 out:
607 filerunlock(f);
608 return ret;
611 /*
612 * Read data from f.
613 */
614 int
615 vacfileread(VacFile *f, void *buf, int cnt, vlong offset)
617 int n;
619 if(offset < 0){
620 werrstr(EBadOffset);
621 return -1;
623 if(filerlock(f) < 0)
624 return -1;
625 if(vtfilelock(f->source, VtOREAD) < 0){
626 filerunlock(f);
627 return -1;
629 n = vtfileread(f->source, buf, cnt, offset);
630 vtfileunlock(f->source);
631 filerunlock(f);
632 return n;
635 static int
636 getentry(VtFile *f, VtEntry *e)
638 if(vtfilelock(f, VtOREAD) < 0)
639 return -1;
640 if(vtfilegetentry(f, e) < 0){
641 vtfileunlock(f);
642 return -1;
644 vtfileunlock(f);
645 if(vtglobaltolocal(e->score) != NilBlock){
646 werrstr("internal error - data not on venti");
647 return -1;
649 return 0;
652 /*
653 * Get the VtEntries for the data contained in f.
654 */
655 int
656 vacfilegetentries(VacFile *f, VtEntry *e, VtEntry *me)
658 if(filerlock(f) < 0)
659 return -1;
660 if(e && getentry(f->source, e) < 0){
661 filerunlock(f);
662 return -1;
664 if(me){
665 if(f->msource == nil)
666 memset(me, 0, sizeof *me);
667 else if(getentry(f->msource, me) < 0){
668 filerunlock(f);
669 return -1;
672 filerunlock(f);
673 return 0;
676 /*
677 * Get the file's size.
678 */
679 int
680 vacfilegetsize(VacFile *f, uvlong *size)
682 if(filerlock(f) < 0)
683 return -1;
684 if(vtfilelock(f->source, VtOREAD) < 0){
685 filerunlock(f);
686 return -1;
688 *size = vtfilegetsize(f->source);
689 vtfileunlock(f->source);
690 filerunlock(f);
692 return 0;
695 /*
696 * Directory reading.
698 * A VacDirEnum is a buffer containing directory entries.
699 * Directory entries contain malloced strings and need to
700 * be cleaned up with vdcleanup. The invariant in the
701 * VacDirEnum is that the directory entries between
702 * vde->i and vde->n are owned by the vde and need to
703 * be cleaned up if it is closed. Those from 0 up to vde->i
704 * have been handed to the reader, and the reader must
705 * take care of calling vdcleanup as appropriate.
706 */
707 VacDirEnum*
708 vdeopen(VacFile *f)
710 VacDirEnum *vde;
711 VacFile *p;
713 if(!vacfileisdir(f)){
714 werrstr(ENotDir);
715 return nil;
718 /*
719 * There might be changes to this directory's children
720 * that have not been flushed out into the cache yet.
721 * Those changes are only available if we look at the
722 * VacFile structures directory. But the directory reader
723 * is going to read the cache blocks directly, so update them.
724 */
725 if(filelock(f) < 0)
726 return nil;
727 for(p=f->down; p; p=p->next)
728 filemetaflush(p, nil);
729 fileunlock(f);
731 vde = vtmallocz(sizeof(VacDirEnum));
732 vde->file = vacfileincref(f);
734 return vde;
737 /*
738 * Figure out the size of the directory entry at offset.
739 * The rest of the metadata is kept in the data half,
740 * but since venti has to track the data size anyway,
741 * we just use that one and avoid updating the directory
742 * each time the file size changes.
743 */
744 static int
745 direntrysize(VtFile *s, ulong offset, ulong gen, uvlong *size)
747 VtBlock *b;
748 ulong bn;
749 VtEntry e;
750 int epb;
752 epb = s->dsize/VtEntrySize;
753 bn = offset/epb;
754 offset -= bn*epb;
756 b = vtfileblock(s, bn, VtOREAD);
757 if(b == nil)
758 goto Err;
759 if(vtentryunpack(&e, b->data, offset) < 0)
760 goto Err;
762 /* dangling entries are returned as zero size */
763 if(!(e.flags & VtEntryActive) || e.gen != gen)
764 *size = 0;
765 else
766 *size = e.size;
767 vtblockput(b);
768 return 0;
770 Err:
771 vtblockput(b);
772 return -1;
775 /*
776 * Fill in vde with a new batch of directory entries.
777 */
778 static int
779 vdefill(VacDirEnum *vde)
781 int i, n;
782 VtFile *meta, *source;
783 MetaBlock mb;
784 MetaEntry me;
785 VacFile *f;
786 VtBlock *b;
787 VacDir *de;
789 /* clean up first */
790 for(i=vde->i; i<vde->n; i++)
791 vdcleanup(vde->buf+i);
792 vtfree(vde->buf);
793 vde->buf = nil;
794 vde->i = 0;
795 vde->n = 0;
797 f = vde->file;
799 source = f->source;
800 meta = f->msource;
802 b = vtfileblock(meta, vde->boff, VtOREAD);
803 if(b == nil)
804 goto Err;
805 if(mbunpack(&mb, b->data, meta->dsize) < 0)
806 goto Err;
808 n = mb.nindex;
809 vde->buf = vtmalloc(n * sizeof(VacDir));
811 for(i=0; i<n; i++){
812 de = vde->buf + i;
813 meunpack(&me, &mb, i);
814 if(vdunpack(de, &me) < 0)
815 goto Err;
816 vde->n++;
817 if(!(de->mode & ModeDir))
818 if(direntrysize(source, de->entry, de->gen, &de->size) < 0)
819 goto Err;
821 vde->boff++;
822 vtblockput(b);
823 return 0;
824 Err:
825 vtblockput(b);
826 return -1;
829 /*
830 * Read a single directory entry from vde into de.
831 * Returns -1 on error, 0 on EOF, and 1 on success.
832 * When it returns 1, it becomes the caller's responsibility
833 * to call vdcleanup(de) to free the strings contained
834 * inside, or else to call vdunread to give it back.
835 */
836 int
837 vderead(VacDirEnum *vde, VacDir *de)
839 int ret;
840 VacFile *f;
841 u32int nb;
843 f = vde->file;
844 if(filerlock(f) < 0)
845 return -1;
847 if(vtfilelock2(f->source, f->msource, VtOREAD) < 0){
848 filerunlock(f);
849 return -1;
852 nb = (vtfilegetsize(f->msource)+f->msource->dsize-1)/f->msource->dsize;
854 while(vde->i >= vde->n){
855 if(vde->boff >= nb){
856 ret = 0;
857 goto Return;
859 if(vdefill(vde) < 0){
860 ret = -1;
861 goto Return;
865 memmove(de, vde->buf + vde->i, sizeof(VacDir));
866 vde->i++;
867 ret = 1;
869 Return:
870 vtfileunlock(f->source);
871 vtfileunlock(f->msource);
872 filerunlock(f);
874 return ret;
877 /*
878 * "Unread" the last directory entry that was read,
879 * so that the next vderead will return the same one.
880 * If the caller calls vdeunread(vde) it should not call
881 * vdcleanup on the entry being "unread".
882 */
883 int
884 vdeunread(VacDirEnum *vde)
886 if(vde->i > 0){
887 vde->i--;
888 return 0;
890 return -1;
893 /*
894 * Close the enumerator.
895 */
896 void
897 vdeclose(VacDirEnum *vde)
899 int i;
900 if(vde == nil)
901 return;
902 /* free the strings */
903 for(i=vde->i; i<vde->n; i++)
904 vdcleanup(vde->buf+i);
905 vtfree(vde->buf);
906 vacfiledecref(vde->file);
907 vtfree(vde);
911 /*
912 * On to mutation. If the vac file system has been opened
913 * read-write, then the files and directories can all be edited.
914 * Changes are kept in the in-memory cache until flushed out
915 * to venti, so we must be careful to explicitly flush data
916 * that we're not likely to modify again.
918 * Each VacFile has its own copy of its VacDir directory entry
919 * in f->dir, but otherwise the cache is the authoratative source
920 * for data. Thus, for the most part, it suffices if we just
921 * call vtfileflushbefore and vtfileflush when we modify things.
922 * There are a few places where we have to remember to write
923 * changed VacDirs back into the cache. If f->dir *is* out of sync,
924 * then f->dirty should be set.
926 * The metadata in a directory is, to venti, a plain data file,
927 * but as mentioned above it is actually a sequence of
928 * MetaBlocks that contain sorted lists of VacDir entries.
929 * The filemetaxxx routines manipulate that stream.
930 */
932 /*
933 * Find space in fp for the directory entry dir (not yet written to disk)
934 * and write it to disk, returning NilBlock on failure,
935 * or the block number on success.
937 * Start is a suggested block number to try.
938 * The caller must have filemetalock'ed f and have
939 * vtfilelock'ed f->up->msource.
940 */
941 static u32int
942 filemetaalloc(VacFile *fp, VacDir *dir, u32int start)
944 u32int nb, bo;
945 VtBlock *b;
946 MetaBlock mb;
947 int nn;
948 uchar *p;
949 int i, n;
950 MetaEntry me;
951 VtFile *ms;
953 ms = fp->msource;
954 n = vdsize(dir, VacDirVersion);
956 /* Look for a block with room for a new entry of size n. */
957 nb = (vtfilegetsize(ms)+ms->dsize-1)/ms->dsize;
958 if(start == NilBlock){
959 if(nb > 0)
960 start = nb - 1;
961 else
962 start = 0;
965 if(start > nb)
966 start = nb;
967 for(bo=start; bo<nb; bo++){
968 if((b = vtfileblock(ms, bo, VtOREAD)) == nil)
969 goto Err;
970 if(mbunpack(&mb, b->data, ms->dsize) < 0)
971 goto Err;
972 nn = (mb.maxsize*FullPercentage/100) - mb.size + mb.free;
973 if(n <= nn && mb.nindex < mb.maxindex){
974 /* reopen for writing */
975 vtblockput(b);
976 if((b = vtfileblock(ms, bo, VtORDWR)) == nil)
977 goto Err;
978 mbunpack(&mb, b->data, ms->dsize);
979 goto Found;
981 vtblockput(b);
984 /* No block found, extend the file by one metablock. */
985 vtfileflushbefore(ms, nb*(uvlong)ms->dsize);
986 if((b = vtfileblock(ms, nb, VtORDWR)) == nil)
987 goto Err;
988 vtfilesetsize(ms, (nb+1)*ms->dsize);
989 mbinit(&mb, b->data, ms->dsize, ms->dsize/BytesPerEntry);
991 Found:
992 /* Now we have a block; allocate space to write the entry. */
993 p = mballoc(&mb, n);
994 if(p == nil){
995 /* mballoc might have changed block */
996 mbpack(&mb);
997 werrstr(EBadMeta);
998 goto Err;
1001 /* Figure out where to put the index entry, and write it. */
1002 mbsearch(&mb, dir->elem, &i, &me);
1003 assert(me.p == nil); /* not already there */
1004 me.p = p;
1005 me.size = n;
1006 vdpack(dir, &me, VacDirVersion);
1007 mbinsert(&mb, i, &me);
1008 mbpack(&mb);
1009 vtblockput(b);
1010 return bo;
1012 Err:
1013 vtblockput(b);
1014 return NilBlock;
1018 * Update f's directory entry in the block cache.
1019 * We look for the directory entry by name;
1020 * if we're trying to rename the file, oelem is the old name.
1022 * Assumes caller has filemetalock'ed f.
1024 static int
1025 filemetaflush(VacFile *f, char *oelem)
1027 int i, n;
1028 MetaBlock mb;
1029 MetaEntry me, me2;
1030 VacFile *fp;
1031 VtBlock *b;
1032 u32int bo;
1034 if(!f->dirty)
1035 return 0;
1037 if(oelem == nil)
1038 oelem = f->dir.elem;
1041 * Locate f's old metadata in the parent's metadata file.
1042 * We know which block it was in, but not exactly where
1043 * in the block.
1045 fp = f->up;
1046 if(vtfilelock(fp->msource, -1) < 0)
1047 return -1;
1048 /* can happen if source is clri'ed out from under us */
1049 if(f->boff == NilBlock)
1050 goto Err1;
1051 b = vtfileblock(fp->msource, f->boff, VtORDWR);
1052 if(b == nil)
1053 goto Err1;
1054 if(mbunpack(&mb, b->data, fp->msource->dsize) < 0)
1055 goto Err;
1056 if(mbsearch(&mb, oelem, &i, &me) < 0)
1057 goto Err;
1060 * Check whether we can resize the entry and keep it
1061 * in this block.
1063 n = vdsize(&f->dir, VacDirVersion);
1064 if(mbresize(&mb, &me, n) >= 0){
1065 /* Okay, can be done without moving to another block. */
1067 /* Remove old data */
1068 mbdelete(&mb, i, &me);
1070 /* Find new location if renaming */
1071 if(strcmp(f->dir.elem, oelem) != 0)
1072 mbsearch(&mb, f->dir.elem, &i, &me2);
1074 /* Pack new data into new location. */
1075 vdpack(&f->dir, &me, VacDirVersion);
1076 vdunpack(&f->dir, &me);
1077 mbinsert(&mb, i, &me);
1078 mbpack(&mb);
1080 /* Done */
1081 vtblockput(b);
1082 vtfileunlock(fp->msource);
1083 f->dirty = 0;
1084 return 0;
1088 * The entry must be moved to another block.
1089 * This can only really happen on renames that
1090 * make the name very long.
1093 /* Allocate a spot in a new block. */
1094 if((bo = filemetaalloc(fp, &f->dir, f->boff+1)) == NilBlock){
1095 /* mbresize above might have modified block */
1096 mbpack(&mb);
1097 goto Err;
1099 f->boff = bo;
1101 /* Now we're committed. Delete entry in old block. */
1102 mbdelete(&mb, i, &me);
1103 mbpack(&mb);
1104 vtblockput(b);
1105 vtfileunlock(fp->msource);
1107 f->dirty = 0;
1108 return 0;
1110 Err:
1111 vtblockput(b);
1112 Err1:
1113 vtfileunlock(fp->msource);
1114 return -1;
1118 * Remove the directory entry for f.
1120 static int
1121 filemetaremove(VacFile *f)
1123 VtBlock *b;
1124 MetaBlock mb;
1125 MetaEntry me;
1126 int i;
1127 VacFile *fp;
1129 b = nil;
1130 fp = f->up;
1131 filemetalock(f);
1133 if(vtfilelock(fp->msource, VtORDWR) < 0)
1134 goto Err;
1135 b = vtfileblock(fp->msource, f->boff, VtORDWR);
1136 if(b == nil)
1137 goto Err;
1139 if(mbunpack(&mb, b->data, fp->msource->dsize) < 0)
1140 goto Err;
1141 if(mbsearch(&mb, f->dir.elem, &i, &me) < 0)
1142 goto Err;
1143 mbdelete(&mb, i, &me);
1144 mbpack(&mb);
1145 vtblockput(b);
1146 vtfileunlock(fp->msource);
1148 f->removed = 1;
1149 f->boff = NilBlock;
1150 f->dirty = 0;
1152 filemetaunlock(f);
1153 return 0;
1155 Err:
1156 vtfileunlock(fp->msource);
1157 vtblockput(b);
1158 filemetaunlock(f);
1159 return -1;
1163 * That was far too much effort for directory entries.
1164 * Now we can write code that *does* things.
1168 * Flush all data associated with f out of the cache and onto venti.
1169 * If recursive is set, flush f's children too.
1170 * Vacfiledecref knows how to flush source and msource too.
1172 int
1173 vacfileflush(VacFile *f, int recursive)
1175 int ret;
1176 VacFile **kids, *p;
1177 int i, nkids;
1179 if(f->mode == VtOREAD)
1180 return 0;
1182 ret = 0;
1183 filemetalock(f);
1184 if(filemetaflush(f, nil) < 0)
1185 ret = -1;
1186 filemetaunlock(f);
1188 if(filelock(f) < 0)
1189 return -1;
1192 * Lock order prevents us from flushing kids while holding
1193 * lock, so make a list and then flush without the lock.
1195 nkids = 0;
1196 kids = nil;
1197 if(recursive){
1198 nkids = 0;
1199 for(p=f->down; p; p=p->next)
1200 nkids++;
1201 kids = vtmalloc(nkids*sizeof(VacFile*));
1202 i = 0;
1203 for(p=f->down; p; p=p->next){
1204 kids[i++] = p;
1205 p->ref++;
1208 if(nkids > 0){
1209 fileunlock(f);
1210 for(i=0; i<nkids; i++){
1211 if(vacfileflush(kids[i], 1) < 0)
1212 ret = -1;
1213 vacfiledecref(kids[i]);
1215 filelock(f);
1217 free(kids);
1220 * Now we can flush our own data.
1222 vtfilelock(f->source, -1);
1223 if(vtfileflush(f->source) < 0)
1224 ret = -1;
1225 vtfileunlock(f->source);
1226 if(f->msource){
1227 vtfilelock(f->msource, -1);
1228 if(vtfileflush(f->msource) < 0)
1229 ret = -1;
1230 vtfileunlock(f->msource);
1232 fileunlock(f);
1234 return ret;
1238 * Create a new file named elem in fp with the given mode.
1239 * The mode can be changed later except for the ModeDir bit.
1241 VacFile*
1242 vacfilecreate(VacFile *fp, char *elem, ulong mode)
1244 VacFile *ff;
1245 VacDir *dir;
1246 VtFile *pr, *r, *mr;
1247 int type;
1248 u32int bo;
1250 if(filelock(fp) < 0)
1251 return nil;
1254 * First, look to see that there's not a file in memory
1255 * with the same name.
1257 for(ff = fp->down; ff; ff=ff->next){
1258 if(strcmp(elem, ff->dir.elem) == 0 && !ff->removed){
1259 ff = nil;
1260 werrstr(EExists);
1261 goto Err1;
1266 * Next check the venti blocks.
1268 ff = dirlookup(fp, elem);
1269 if(ff != nil){
1270 werrstr(EExists);
1271 goto Err1;
1275 * By the way, you can't create in a read-only file system.
1277 pr = fp->source;
1278 if(pr->mode != VtORDWR){
1279 werrstr(EReadOnly);
1280 goto Err1;
1284 * Okay, time to actually create something. Lock the two
1285 * halves of the directory and create a file.
1287 if(vtfilelock2(fp->source, fp->msource, -1) < 0)
1288 goto Err1;
1289 ff = filealloc(fp->fs);
1290 ff->qidoffset = fp->qidoffset; /* hopefully fp->qidoffset == 0 */
1291 type = VtDataType;
1292 if(mode & ModeDir)
1293 type = VtDirType;
1294 mr = nil;
1295 if((r = vtfilecreate(pr, pr->psize, pr->dsize, type)) == nil)
1296 goto Err;
1297 if(mode & ModeDir)
1298 if((mr = vtfilecreate(pr, pr->psize, pr->dsize, VtDataType)) == nil)
1299 goto Err;
1302 * Fill in the directory entry and write it to disk.
1304 dir = &ff->dir;
1305 dir->elem = vtstrdup(elem);
1306 dir->entry = r->offset;
1307 dir->gen = r->gen;
1308 if(mode & ModeDir){
1309 dir->mentry = mr->offset;
1310 dir->mgen = mr->gen;
1312 dir->size = 0;
1313 if(_vacfsnextqid(fp->fs, &dir->qid) < 0)
1314 goto Err;
1315 dir->uid = vtstrdup(fp->dir.uid);
1316 dir->gid = vtstrdup(fp->dir.gid);
1317 dir->mid = vtstrdup("");
1318 dir->mtime = time(0L);
1319 dir->mcount = 0;
1320 dir->ctime = dir->mtime;
1321 dir->atime = dir->mtime;
1322 dir->mode = mode;
1323 if((bo = filemetaalloc(fp, &ff->dir, NilBlock)) == NilBlock)
1324 goto Err;
1327 * Now we're committed.
1329 vtfileunlock(fp->source);
1330 vtfileunlock(fp->msource);
1331 ff->source = r;
1332 ff->msource = mr;
1333 ff->boff = bo;
1335 /* Link into tree. */
1336 ff->next = fp->down;
1337 fp->down = ff;
1338 ff->up = fp;
1339 vacfileincref(fp);
1341 fileunlock(fp);
1343 filelock(ff);
1344 vtfilelock(ff->source, -1);
1345 vtfileunlock(ff->source);
1346 fileunlock(ff);
1348 return ff;
1350 Err:
1351 vtfileunlock(fp->source);
1352 vtfileunlock(fp->msource);
1353 if(r){
1354 vtfilelock(r, -1);
1355 vtfileremove(r);
1357 if(mr){
1358 vtfilelock(mr, -1);
1359 vtfileremove(mr);
1361 Err1:
1362 if(ff)
1363 vacfiledecref(ff);
1364 fileunlock(fp);
1365 return nil;
1369 * Change the size of the file f.
1371 int
1372 vacfilesetsize(VacFile *f, uvlong size)
1374 if(vacfileisdir(f)){
1375 werrstr(ENotFile);
1376 return -1;
1379 if(filelock(f) < 0)
1380 return -1;
1382 if(f->source->mode != VtORDWR){
1383 werrstr(EReadOnly);
1384 goto Err;
1386 if(vtfilelock(f->source, -1) < 0)
1387 goto Err;
1388 if(vtfilesetsize(f->source, size) < 0){
1389 vtfileunlock(f->source);
1390 goto Err;
1392 vtfileunlock(f->source);
1393 fileunlock(f);
1394 return 0;
1396 Err:
1397 fileunlock(f);
1398 return -1;
1402 * Write data to f.
1404 int
1405 vacfilewrite(VacFile *f, void *buf, int cnt, vlong offset)
1407 if(vacfileisdir(f)){
1408 werrstr(ENotFile);
1409 return -1;
1411 if(filelock(f) < 0)
1412 return -1;
1413 if(f->source->mode != VtORDWR){
1414 werrstr(EReadOnly);
1415 goto Err;
1417 if(offset < 0){
1418 werrstr(EBadOffset);
1419 goto Err;
1422 if(vtfilelock(f->source, -1) < 0)
1423 goto Err;
1424 if(f->dir.mode & ModeAppend)
1425 offset = vtfilegetsize(f->source);
1426 if(vtfilewrite(f->source, buf, cnt, offset) != cnt
1427 || vtfileflushbefore(f->source, offset) < 0){
1428 vtfileunlock(f->source);
1429 goto Err;
1431 vtfileunlock(f->source);
1432 fileunlock(f);
1433 return cnt;
1435 Err:
1436 fileunlock(f);
1437 return -1;
1441 * Set (!) the VtEntry for the data contained in f.
1442 * This let's us efficiently copy data from one file to another.
1444 int
1445 vacfilesetentries(VacFile *f, VtEntry *e, VtEntry *me)
1447 int ret;
1449 vacfileflush(f, 0); /* flush blocks to venti, since we won't see them again */
1451 if(!(e->flags&VtEntryActive)){
1452 werrstr("missing entry for source");
1453 return -1;
1455 if(me && !(me->flags&VtEntryActive))
1456 me = nil;
1457 if(f->msource && !me){
1458 werrstr("missing entry for msource");
1459 return -1;
1461 if(me && !f->msource){
1462 werrstr("no msource to set");
1463 return -1;
1466 if(filelock(f) < 0)
1467 return -1;
1468 if(f->source->mode != VtORDWR
1469 || (f->msource && f->msource->mode != VtORDWR)){
1470 werrstr(EReadOnly);
1471 fileunlock(f);
1472 return -1;
1474 if(vtfilelock2(f->source, f->msource, -1) < 0){
1475 fileunlock(f);
1476 return -1;
1478 ret = 0;
1479 if(vtfilesetentry(f->source, e) < 0)
1480 ret = -1;
1481 else if(me && vtfilesetentry(f->msource, me) < 0)
1482 ret = -1;
1484 vtfileunlock(f->source);
1485 if(f->msource)
1486 vtfileunlock(f->msource);
1487 fileunlock(f);
1488 return ret;
1492 * Get the directory entry for f.
1494 int
1495 vacfilegetdir(VacFile *f, VacDir *dir)
1497 if(filerlock(f) < 0)
1498 return -1;
1500 filemetalock(f);
1501 vdcopy(dir, &f->dir);
1502 filemetaunlock(f);
1504 if(!vacfileisdir(f)){
1505 if(vtfilelock(f->source, VtOREAD) < 0){
1506 filerunlock(f);
1507 return -1;
1509 dir->size = vtfilegetsize(f->source);
1510 vtfileunlock(f->source);
1512 filerunlock(f);
1514 return 0;
1518 * Set the directory entry for f.
1520 int
1521 vacfilesetdir(VacFile *f, VacDir *dir)
1523 VacFile *ff;
1524 char *oelem;
1525 u32int mask;
1526 u64int size;
1528 /* can not set permissions for the root */
1529 if(vacfileisroot(f)){
1530 werrstr(ERoot);
1531 return -1;
1534 if(filelock(f) < 0)
1535 return -1;
1536 filemetalock(f);
1538 if(f->source->mode != VtORDWR){
1539 werrstr(EReadOnly);
1540 goto Err;
1543 /* On rename, check new name does not already exist */
1544 if(strcmp(f->dir.elem, dir->elem) != 0){
1545 for(ff = f->up->down; ff; ff=ff->next){
1546 if(strcmp(dir->elem, ff->dir.elem) == 0 && !ff->removed){
1547 werrstr(EExists);
1548 goto Err;
1551 ff = dirlookup(f->up, dir->elem);
1552 if(ff != nil){
1553 vacfiledecref(ff);
1554 werrstr(EExists);
1555 goto Err;
1557 werrstr(""); /* "failed" dirlookup poisoned it */
1560 /* Get ready... */
1561 if(vtfilelock2(f->source, f->msource, -1) < 0)
1562 goto Err;
1563 if(!vacfileisdir(f)){
1564 size = vtfilegetsize(f->source);
1565 if(size != dir->size){
1566 if(vtfilesetsize(f->source, dir->size) < 0){
1567 vtfileunlock(f->source);
1568 if(f->msource)
1569 vtfileunlock(f->msource);
1570 goto Err;
1574 /* ... now commited to changing it. */
1575 vtfileunlock(f->source);
1576 if(f->msource)
1577 vtfileunlock(f->msource);
1579 oelem = nil;
1580 if(strcmp(f->dir.elem, dir->elem) != 0){
1581 oelem = f->dir.elem;
1582 f->dir.elem = vtstrdup(dir->elem);
1585 if(strcmp(f->dir.uid, dir->uid) != 0){
1586 vtfree(f->dir.uid);
1587 f->dir.uid = vtstrdup(dir->uid);
1590 if(strcmp(f->dir.gid, dir->gid) != 0){
1591 vtfree(f->dir.gid);
1592 f->dir.gid = vtstrdup(dir->gid);
1595 f->dir.mtime = dir->mtime;
1596 f->dir.atime = dir->atime;
1598 mask = ~(ModeDir|ModeSnapshot);
1599 f->dir.mode &= ~mask;
1600 f->dir.mode |= mask & dir->mode;
1601 f->dirty = 1;
1603 if(filemetaflush(f, oelem) < 0){
1604 vtfree(oelem);
1605 goto Err; /* that sucks */
1607 vtfree(oelem);
1609 filemetaunlock(f);
1610 fileunlock(f);
1611 return 0;
1613 Err:
1614 filemetaunlock(f);
1615 fileunlock(f);
1616 return -1;
1620 * Set the qid space.
1622 int
1623 vacfilesetqidspace(VacFile *f, u64int offset, u64int max)
1625 int ret;
1627 if(filelock(f) < 0)
1628 return -1;
1629 if(f->source->mode != VtORDWR){
1630 fileunlock(f);
1631 werrstr(EReadOnly);
1632 return -1;
1634 filemetalock(f);
1635 f->dir.qidspace = 1;
1636 f->dir.qidoffset = offset;
1637 f->dir.qidmax = max;
1638 f->dirty = 1;
1639 ret = filemetaflush(f, nil);
1640 filemetaunlock(f);
1641 fileunlock(f);
1642 return ret;
1646 * Check that the file is empty, returning 0 if it is.
1647 * Returns -1 on error (and not being empty is an error).
1649 static int
1650 filecheckempty(VacFile *f)
1652 u32int i, n;
1653 VtBlock *b;
1654 MetaBlock mb;
1655 VtFile *r;
1657 r = f->msource;
1658 n = (vtfilegetsize(r)+r->dsize-1)/r->dsize;
1659 for(i=0; i<n; i++){
1660 b = vtfileblock(r, i, VtOREAD);
1661 if(b == nil)
1662 return -1;
1663 if(mbunpack(&mb, b->data, r->dsize) < 0)
1664 goto Err;
1665 if(mb.nindex > 0){
1666 werrstr(ENotEmpty);
1667 goto Err;
1669 vtblockput(b);
1671 return 0;
1673 Err:
1674 vtblockput(b);
1675 return -1;
1679 * Remove the vac file f.
1681 int
1682 vacfileremove(VacFile *f)
1684 VacFile *ff;
1686 /* Cannot remove the root */
1687 if(vacfileisroot(f)){
1688 werrstr(ERoot);
1689 return -1;
1692 if(filelock(f) < 0)
1693 return -1;
1694 if(f->source->mode != VtORDWR){
1695 werrstr(EReadOnly);
1696 goto Err1;
1698 if(vtfilelock2(f->source, f->msource, -1) < 0)
1699 goto Err1;
1700 if(vacfileisdir(f) && filecheckempty(f)<0)
1701 goto Err;
1703 for(ff=f->down; ff; ff=ff->next)
1704 assert(ff->removed);
1706 vtfileremove(f->source);
1707 f->source = nil;
1708 if(f->msource){
1709 vtfileremove(f->msource);
1710 f->msource = nil;
1712 fileunlock(f);
1714 if(filemetaremove(f) < 0)
1715 return -1;
1716 return 0;
1718 Err:
1719 vtfileunlock(f->source);
1720 if(f->msource)
1721 vtfileunlock(f->msource);
1722 Err1:
1723 fileunlock(f);
1724 return -1;
1728 * Vac file system format.
1730 static char EBadVacFormat[] = "bad format for vac file";
1732 static VacFs *
1733 vacfsalloc(VtConn *z, int bsize, ulong cachemem, int mode)
1735 VacFs *fs;
1737 fs = vtmallocz(sizeof(VacFs));
1738 fs->z = z;
1739 fs->bsize = bsize;
1740 fs->mode = mode;
1741 fs->cache = vtcachealloc(z, cachemem);
1742 return fs;
1745 static int
1746 readscore(int fd, uchar score[VtScoreSize])
1748 char buf[45], *pref;
1749 int n;
1751 n = readn(fd, buf, sizeof(buf)-1);
1752 if(n < sizeof(buf)-1) {
1753 werrstr("short read");
1754 return -1;
1756 buf[n] = 0;
1758 if(vtparsescore(buf, &pref, score) < 0){
1759 werrstr(EBadVacFormat);
1760 return -1;
1762 if(pref==nil || strcmp(pref, "vac") != 0) {
1763 werrstr("not a vac file");
1764 return -1;
1766 return 0;
1769 VacFs*
1770 vacfsopen(VtConn *z, char *file, int mode, ulong cachemem)
1772 int fd;
1773 uchar score[VtScoreSize];
1774 char *prefix;
1776 if(vtparsescore(file, &prefix, score) >= 0){
1777 if(strcmp(prefix, "vac") != 0){
1778 werrstr("not a vac file");
1779 return nil;
1781 }else{
1782 fd = open(file, OREAD);
1783 if(fd < 0)
1784 return nil;
1785 if(readscore(fd, score) < 0){
1786 close(fd);
1787 return nil;
1789 close(fd);
1791 fprint(2, "vacfsopen %V\n", score);
1792 return vacfsopenscore(z, score, mode, cachemem);
1795 VacFs*
1796 vacfsopenscore(VtConn *z, u8int *score, int mode, ulong cachemem)
1798 VacFs *fs;
1799 int n;
1800 VtRoot rt;
1801 uchar buf[VtRootSize];
1802 VacFile *root;
1803 VtFile *r;
1804 VtEntry e;
1806 n = vtread(z, score, VtRootType, buf, VtRootSize);
1807 if(n < 0) {
1808 fprint(2, "read %r\n");
1809 return nil;
1811 if(n != VtRootSize){
1812 werrstr("vtread on root too short");
1813 fprint(2, "size %d\n", n);
1814 return nil;
1817 if(vtrootunpack(&rt, buf) < 0) {
1818 fprint(2, "unpack: %r\n");
1819 return nil;
1822 if(strcmp(rt.type, "vac") != 0) {
1823 fprint(2, "bad type %s\n", rt.type);
1824 werrstr("not a vac root");
1825 return nil;
1828 fs = vacfsalloc(z, rt.blocksize, cachemem, mode);
1829 memmove(fs->score, score, VtScoreSize);
1830 fs->mode = mode;
1832 memmove(e.score, rt.score, VtScoreSize);
1833 e.gen = 0;
1835 // Don't waste cache memory on pointer blocks
1836 // when rt.blocksize is large.
1837 e.psize = (rt.blocksize/VtEntrySize)*VtEntrySize;
1838 if(e.psize > 60000)
1839 e.psize = (60000/VtEntrySize)*VtEntrySize;
1841 e.dsize = rt.blocksize;
1842 fprint(2, "openscore %d psize %d dsize %d\n", (int)rt.blocksize, (int)e.psize, (int)e.dsize);
1843 e.type = VtDirType;
1844 e.flags = VtEntryActive;
1845 e.size = 3*VtEntrySize;
1847 root = nil;
1848 if((r = vtfileopenroot(fs->cache, &e)) == nil)
1849 goto Err;
1850 if(debug)
1851 fprint(2, "r %p\n", r);
1852 root = _vacfileroot(fs, r);
1853 if(debug)
1854 fprint(2, "root %p\n", root);
1855 vtfileclose(r);
1856 if(root == nil)
1857 goto Err;
1858 fs->root = root;
1859 return fs;
1860 Err:
1861 if(root)
1862 vacfiledecref(root);
1863 vacfsclose(fs);
1864 return nil;
1867 int
1868 vacfsmode(VacFs *fs)
1870 return fs->mode;
1873 VacFile*
1874 vacfsgetroot(VacFs *fs)
1876 return vacfileincref(fs->root);
1879 int
1880 vacfsgetblocksize(VacFs *fs)
1882 return fs->bsize;
1885 int
1886 vacfsgetscore(VacFs *fs, u8int *score)
1888 memmove(score, fs->score, VtScoreSize);
1889 return 0;
1892 int
1893 _vacfsnextqid(VacFs *fs, uvlong *qid)
1895 ++fs->qid;
1896 *qid = fs->qid;
1897 return 0;
1900 void
1901 vacfsjumpqid(VacFs *fs, uvlong step)
1903 fs->qid += step;
1907 * Set *maxqid to the maximum qid expected in this file system.
1908 * In newer vac archives, the maximum qid is stored in the
1909 * qidspace VacDir annotation. In older vac archives, the root
1910 * got created last, so it had the maximum qid.
1912 int
1913 vacfsgetmaxqid(VacFs *fs, uvlong *maxqid)
1915 VacDir vd;
1917 if(vacfilegetdir(fs->root, &vd) < 0)
1918 return -1;
1919 if(vd.qidspace)
1920 *maxqid = vd.qidmax;
1921 else
1922 *maxqid = vd.qid;
1923 vdcleanup(&vd);
1924 return 0;
1928 void
1929 vacfsclose(VacFs *fs)
1931 if(fs->root)
1932 vacfiledecref(fs->root);
1933 fs->root = nil;
1934 vtcachefree(fs->cache);
1935 vtfree(fs);
1939 * Create a fresh vac fs.
1941 VacFs *
1942 vacfscreate(VtConn *z, int bsize, ulong cachemem)
1944 VacFs *fs;
1945 VtFile *f;
1946 uchar buf[VtEntrySize], metascore[VtScoreSize];
1947 VtEntry e;
1948 VtBlock *b;
1949 MetaBlock mb;
1950 VacDir vd;
1951 MetaEntry me;
1952 int psize;
1954 if((fs = vacfsalloc(z, bsize, cachemem, VtORDWR)) == nil)
1955 return nil;
1958 * Fake up an empty vac fs.
1960 psize = bsize/VtScoreSize*VtScoreSize;
1961 if(psize > 60000)
1962 psize = 60000/VtScoreSize*VtScoreSize;
1963 fprint(2, "create bsize %d psize %d\n", bsize, psize);
1965 f = vtfilecreateroot(fs->cache, psize, bsize, VtDirType);
1966 if(f == nil)
1967 sysfatal("vtfilecreateroot: %r");
1968 vtfilelock(f, VtORDWR);
1970 /* Write metablock containing root directory VacDir. */
1971 b = vtcacheallocblock(fs->cache, VtDataType, bsize);
1972 mbinit(&mb, b->data, bsize, bsize/BytesPerEntry);
1973 memset(&vd, 0, sizeof vd);
1974 vd.elem = "/";
1975 vd.mode = 0777|ModeDir;
1976 vd.uid = "vac";
1977 vd.gid = "vac";
1978 vd.mid = "";
1979 me.size = vdsize(&vd, VacDirVersion);
1980 me.p = mballoc(&mb, me.size);
1981 vdpack(&vd, &me, VacDirVersion);
1982 mbinsert(&mb, 0, &me);
1983 mbpack(&mb);
1984 vtblockwrite(b);
1985 memmove(metascore, b->score, VtScoreSize);
1986 vtblockput(b);
1988 /* First entry: empty venti directory stream. */
1989 memset(&e, 0, sizeof e);
1990 e.flags = VtEntryActive;
1991 e.psize = psize;
1992 e.dsize = bsize;
1993 e.type = VtDirType;
1994 memmove(e.score, vtzeroscore, VtScoreSize);
1995 vtentrypack(&e, buf, 0);
1996 vtfilewrite(f, buf, VtEntrySize, 0);
1998 /* Second entry: empty metadata stream. */
1999 e.type = VtDataType;
2000 vtentrypack(&e, buf, 0);
2001 vtfilewrite(f, buf, VtEntrySize, VtEntrySize);
2003 /* Third entry: metadata stream with root directory. */
2004 memmove(e.score, metascore, VtScoreSize);
2005 e.size = bsize;
2006 vtentrypack(&e, buf, 0);
2007 vtfilewrite(f, buf, VtEntrySize, VtEntrySize*2);
2009 vtfileflush(f);
2010 vtfileunlock(f);
2012 /* Now open it as a vac fs. */
2013 fs->root = _vacfileroot(fs, f);
2014 if(fs->root == nil){
2015 werrstr("vacfileroot: %r");
2016 vacfsclose(fs);
2017 return nil;
2020 return fs;
2023 int
2024 vacfssync(VacFs *fs)
2026 uchar buf[1024];
2027 VtEntry e;
2028 VtFile *f;
2029 VtRoot root;
2031 /* Sync the entire vacfs to disk. */
2032 if(vacfileflush(fs->root, 1) < 0)
2033 return -1;
2034 if(vtfilelock(fs->root->up->msource, -1) < 0)
2035 return -1;
2036 if(vtfileflush(fs->root->up->msource) < 0){
2037 vtfileunlock(fs->root->up->msource);
2038 return -1;
2040 vtfileunlock(fs->root->up->msource);
2042 /* Prepare the dir stream for the root block. */
2043 if(getentry(fs->root->source, &e) < 0)
2044 return -1;
2045 vtentrypack(&e, buf, 0);
2046 if(getentry(fs->root->msource, &e) < 0)
2047 return -1;
2048 vtentrypack(&e, buf, 1);
2049 if(getentry(fs->root->up->msource, &e) < 0)
2050 return -1;
2051 vtentrypack(&e, buf, 2);
2053 f = vtfilecreateroot(fs->cache, fs->bsize, fs->bsize, VtDirType);
2054 vtfilelock(f, VtORDWR);
2055 if(vtfilewrite(f, buf, 3*VtEntrySize, 0) < 0
2056 || vtfileflush(f) < 0){
2057 vtfileunlock(f);
2058 vtfileclose(f);
2059 return -1;
2061 vtfileunlock(f);
2062 if(getentry(f, &e) < 0){
2063 vtfileclose(f);
2064 return -1;
2066 vtfileclose(f);
2068 /* Build a root block. */
2069 memset(&root, 0, sizeof root);
2070 strcpy(root.type, "vac");
2071 strcpy(root.name, fs->name);
2072 memmove(root.score, e.score, VtScoreSize);
2073 root.blocksize = fs->bsize;
2074 memmove(root.prev, fs->score, VtScoreSize);
2075 vtrootpack(&root, buf);
2076 if(vtwrite(fs->z, fs->score, VtRootType, buf, VtRootSize) < 0){
2077 werrstr("writing root: %r");
2078 return -1;
2080 if(vtsync(fs->z) < 0)
2081 return -1;
2082 return 0;
2085 int
2086 vacfiledsize(VacFile *f)
2088 VtEntry e;
2090 if(vacfilegetentries(f,&e,nil) < 0)
2091 return -1;
2092 return e.dsize;
2096 * Does block b of f have the same SHA1 hash as the n bytes at buf?
2098 int
2099 sha1matches(VacFile *f, ulong b, uchar *buf, int n)
2101 uchar fscore[VtScoreSize];
2102 uchar bufscore[VtScoreSize];
2104 if(vacfileblockscore(f, b, fscore) < 0)
2105 return 0;
2106 n = vtzerotruncate(VtDataType, buf, n);
2107 sha1(buf, n, bufscore, nil);
2108 if(memcmp(bufscore, fscore, VtScoreSize) == 0)
2109 return 1;
2110 return 0;