ref: 73ecfeda6df68dc89a93e17799cad0de3d786497
dir: /ream.c/
#include <u.h> #include <libc.h> #include <fcall.h> #include <avl.h> #include "dat.h" #include "fns.h" enum { Qmainroot, Qadmroot, Qadmuser, Nreamqid, }; static void fillxdir(Xdir *d, vlong qid, char *name, int type, int mode) { memset(d, 0, sizeof(Xdir)); d->qid = (Qid){qid, 0, type}; d->mode = mode; d->atime = 0; d->mtime = 0; d->length = 0; d->name = name; d->uid = -1; d->gid = -1; d->muid = 0; } static void initadm(Blk *r, Blk *u, int nu) { char *p, kbuf[Keymax], vbuf[Inlmax]; Kvp kv; Xdir d; /* nb: values must be inserted in key order */ kv.k = kbuf; kv.nk = Offksz; kv.v = vbuf; kv.nv = Ptrsz; kbuf[0] = Kdat; PACK64(kbuf+1, (uvlong)Qadmuser); PACK64(kbuf+9, 0ULL); packbp(kv.v, kv.nv, &u->bp); setval(r, &kv); fillxdir(&d, Qadmuser, "users", QTFILE, 0664); d.length = nu; dir2kv(Qadmroot, &d, &kv, vbuf, sizeof(vbuf)); setval(r, &kv); fillxdir(&d, Qadmroot, "", QTDIR, DMDIR|0775); dir2kv(-1, &d, &kv, vbuf, sizeof(vbuf)); setval(r, &kv); p = packsuper(kbuf, sizeof(kbuf), 0); kv.k = kbuf; kv.nk = p - kbuf; p = packdkey(vbuf, sizeof(vbuf), -1, ""); kv.v = vbuf; kv.nv = p - vbuf; setval(r, &kv); } static void initroot(Blk *r) { char *p, kbuf[Keymax], vbuf[Inlmax]; Kvp kv; Xdir d; /* nb: values must be inserted in key order */ fillxdir(&d, Qmainroot, "", QTDIR, DMDIR|0775); dir2kv(-1, &d, &kv, vbuf, sizeof(vbuf)); setval(r, &kv); p = packsuper(kbuf, sizeof(kbuf), 0); kv.k = kbuf; kv.nk = p - kbuf; p = packdkey(vbuf, sizeof(vbuf), -1, ""); kv.v = vbuf; kv.nv = p - vbuf; setval(r, &kv); } static void initsnap(Blk *s, Blk *r, Blk *a) { char *p, *e, buf[Kvmax]; Tree t; Kvp kv; lbl2kv("adm", 1, Lmut, &kv, buf, sizeof(buf)); setval(s, &kv); lbl2kv("empty", 0, 0, &kv, buf, sizeof(buf)); setval(s, &kv); lbl2kv("main", 2, Lmut, &kv, buf, sizeof(buf)); setval(s, &kv); p = buf; e = p + sizeof(buf); /* empty */ kv.k = p; p = packsnap(buf, e - p, 0); kv.nk = p - kv.k; kv.v = p; memset(&t, 0, sizeof(Tree)); t.flag = 0; t.nref = 2; t.nlbl = 1; t.ht = 1; t.gen = fs->nextgen++; t.pred = 0; t.succ = 2; t.bp = r->bp; p = packtree(p, e - p, &t); kv.nv = p - kv.v; setval(s, &kv); p = buf; e = p + sizeof(buf); /* adm */ kv.k = p; p = packsnap(p, e - p, 1); kv.nk = p - kv.k; kv.v = p; memset(&t, 0, sizeof(Tree)); t.nref = 0; t.nlbl = 1; t.ht = 1; t.gen = fs->nextgen++; t.pred = 0; t.succ = -1; t.bp = a->bp; p = packtree(p, e - p, &t); kv.nv = p - kv.v; setval(s, &kv); p = buf; e = p + sizeof(buf); /* main */ kv.k = p; p = packsnap(buf, e - p, 2); kv.nk = p - kv.k; kv.v = p; memset(&t, 0, sizeof(Tree)); t.nref = 0; t.nlbl = 1; t.ht = 1; t.gen = fs->nextgen++; t.pred = 0; t.succ = -1; t.bp = r->bp; p = packtree(p, e - p, &t); kv.nv = p - kv.v; setval(s, &kv); } static void initarena(Arena *a, vlong start, vlong asz) { vlong addr, bo, bh; char *p; Blk *b, *hd, *tl; b = cachepluck(); if(start == 512*MiB){ start += Blksz; asz -= Blksz; } addr = start+Blksz; /* leave room for arena hdr */ a->loghd.addr = -1; a->loghd.hash = -1; a->loghd.gen = -1; memset(b->buf, 0, sizeof(b->buf)); b->type = Tlog; b->bp.addr = addr+Blksz; if(b->bp.addr == 512*MiB){ b->bp.addr += Blksz; asz -= Blksz; } b->logsz = 0; b->logp = (Bptr){-1, -1, -1}; b->data = b->buf + Loghdsz; setflag(b, Bdirty); p = b->buf + Loghdsz; b->logp = (Bptr){-1, -1, -1}; PACK64(p, addr|LogFree); p += 8; /* addr */ PACK64(p, asz-Blksz); p += 8; /* len */ PACK64(p, b->bp.addr|LogAlloc); p += 8; /* addr */ PACK64(p, Blksz); p += 8; /* len */ /* backup sb */ if(start <= 512*MiB && start+asz > 512*MiB){ PACK64(p, (512*MiB)|LogAlloc1); p += 8; } PACK64(p, (uvlong)LogSync); p += 8; /* barrier */ b->logsz = p - b->data; finalize(b); syncblk(b); dropblk(b); bh = b->bp.hash; bo = b->bp.addr; a->loghd.addr = bo; a->loghd.hash = bh; a->loghd.gen = -1; a->size = asz; a->used = Blksz; hd = cachepluck(); tl = cachepluck(); memset(hd->buf, 0, sizeof(hd->buf)); hd->type = Tarena; hd->bp.addr = start; hd->data = hd->buf+2; finalize(hd); memset(tl->buf, 0, sizeof(tl->buf)); tl->type = Tarena; tl->bp.addr = start+asz; tl->data = tl->buf+2; finalize(tl); packarena(hd->data, Arenasz, a); packarena(tl->data, Arenasz, a); finalize(hd); finalize(tl); syncblk(hd); syncblk(tl); a->hd = hd; a->tl = tl; } void reamfs(char *dev) { Blk *sb0, *sb1, *tb, *mb, *ab, *ub; vlong sz, asz, off; Mount *mnt, *adm; Arena *a; char *utab; Dir *d; int i; if(waserror()) sysfatal("ream %s: %s\n", dev, errmsg()); if((fs->fd = open(dev, ORDWR)) == -1) sysfatal("open %s: %r", dev); if((d = dirfstat(fs->fd)) == nil) sysfatal("ream: %r"); sz = d->length; free(d); if(sz < 512*MiB+Blksz) sysfatal("ream: disk too small"); mnt = emalloc(sizeof(Mount), 1); mnt->root = mallocz(sizeof(Tree), 1); adm = mallocz(sizeof(Mount), 1); adm->root = mallocz(sizeof(Tree), 1); sz = sz - sz%Blksz - 2*Blksz; fs->narena = (sz + 64ULL*GiB - 1) / (64ULL*GiB); if(fs->narena < 8) fs->narena = 8; if(fs->narena >= 128) fs->narena = 128; fs->arenas = emalloc(fs->narena*sizeof(Arena), 1); off = Blksz; asz = sz/fs->narena; asz = asz - (asz % Blksz) - Blksz; fs->arenasz = asz; sb0 = cachepluck(); sb1 = cachepluck(); sb0->bp = (Bptr){0, -1, -1}; sb1->bp = (Bptr){512*MiB, -1, -1}; fs->arenabp = emalloc(fs->narena * sizeof(Bptr), 1); for(i = 0; i < fs->narena; i++){ a = &fs->arenas[i]; print("\tarena %d: %lld blocks at %llx\n", i, asz/Blksz, off); initarena(a, off, asz); fs->arenabp[i] = a->hd->bp; off += asz+Blksz; } for(i = 0; i < fs->narena; i++){ a = &fs->arenas[i]; loadarena(a, a->hd->bp, asz); loadlog(a, a->loghd); } if((mb = newblk(mnt->root, Tleaf, 0)) == nil) sysfatal("ream: allocate root: %r"); holdblk(mb); initroot(mb); finalize(mb); syncblk(mb); mnt->root->ht = 1; mnt->root->bp = mb->bp; if((ab = newblk(adm->root, Tleaf, 0)) == nil) sysfatal("ream: allocate root: %r"); if((ub = newblk(adm->root, Tdat, 0)) == nil) sysfatal("ream: allocate root: %r"); holdblk(ab); holdblk(ub); utab = smprint( "-1:adm::%s\n" "0:none::\n" "1:%s:%s:\n", reamuser, reamuser, reamuser); memcpy(ub->data, utab, strlen(utab)); finalize(ub); syncblk(ub); initadm(ab, ub, strlen(utab)); finalize(ab); syncblk(ab); adm->root->ht = 1; adm->root->bp = ab->bp; /* * Now that we have a completely empty fs, give it * a single snap block that the tree will insert * into, and take a snapshot as the initial state. */ if((tb = newblk(mnt->root, Tleaf, 0)) == nil) sysfatal("ream: allocate snaps: %r"); holdblk(tb); initsnap(tb, mb, ab); finalize(tb); syncblk(tb); fs->snap.bp = tb->bp; fs->snap.ht = 1; fs->snapdl.hd.addr = -1; fs->snapdl.hd.hash = -1; fs->snapdl.tl.addr = -1; fs->snapdl.tl.hash = -1; fs->nextqid = Nreamqid; dropblk(mb); dropblk(ab); dropblk(ub); dropblk(tb); fs->nextqid = Nreamqid; /* * We need to write back all of the arenas * with the updated free lists */ for(i = 0; i < fs->narena; i++){ a = &fs->arenas[i]; finalize(a->logtl); syncblk(a->logtl); packarena(a->hd->data, Blksz, a); finalize(a->hd); syncblk(a->hd); packarena(a->tl->data, Blksz, a); finalize(a->tl); syncblk(a->tl); fs->arenabp[i] = a->hd->bp; dropblk(a->hd); dropblk(a->tl); } dropblk(mb); dropblk(ab); dropblk(ub); dropblk(tb); /* * Finally, write back the superblock and backup * superblock. */ packsb(sb0->buf, Blksz, fs); packsb(sb1->buf, Blksz, fs); finalize(sb0); finalize(sb1); syncblk(sb0); syncblk(sb1); dropblk(sb0); dropblk(sb1); free(mnt); poperror(); } void growfs(char *dev) { vlong sz, off; int i, narena; Bptr bp; Arena *a; Dir *d; if(waserror()) sysfatal("grow %s: %s\n", dev, errmsg()); if((fs->fd = open(dev, ORDWR)) == -1) sysfatal("open %s: %r", dev); if((d = dirfstat(fs->fd)) == nil) sysfatal("ream: %r"); sz = d->length; free(d); bp = (Bptr){0, -1, -1}; if((fs->sb0 = getblk(bp, GBnochk)) == nil) sysfatal("superblock: %r\n"); if(unpacksb(fs, fs->sb0->buf, Blksz) == nil) sysfatal("superblock: %r"); if((fs->arenas = calloc(fs->narena, sizeof(Arena))) == nil) sysfatal("malloc: %r"); for(i = 0; i < fs->narena; i++){ a = &fs->arenas[i]; loadarena(a, fs->arenabp[i], fs->arenasz); } narena = sz/fs->arenasz; off = fs->arenasz * fs->narena; if(narena <= fs->narena) sysfatal("disk too small for more arenas"); if((fs->arenas = realloc(fs->arenas, narena*sizeof(Arena))) == nil) error(Enomem); if((fs->arenabp = realloc(fs->arenas, narena*sizeof(Bptr))) == nil) error(Enomem); for(i = fs->narena; i < narena; i++){ a = &fs->arenas[i]; print("\tadding %d: %lld blocks at %llx\n", i, fs->arenasz/Blksz, off); initarena(&fs->arenas[i], off, fs->arenasz); loadarena(a, fs->arenabp[i], fs->arenasz); fs->arenabp[i] = a->hd->bp; off += fs->arenasz; } fs->narena = narena; for(i = 0; i < narena; i++){ a = &fs->arenas[i]; packarena(a->hd->data, Blksz, a); packarena(a->tl->data, Blksz, a); finalize(a->hd); finalize(a->tl); syncblk(a->hd); syncblk(a->tl); } packsb(fs->sb0->buf, Blksz, fs); packsb(fs->sb1->buf, Blksz, fs); finalize(fs->sb0); finalize(fs->sb1); syncblk(fs->sb0); syncblk(fs->sb1); poperror(); }