import Base import ../src/lib/u64.bend as U import ../src/vmm/abi.bend as V import ./hvf.bend as H import ../src/fs/quota.bend as Qt import ../src/fs/contain.bend as Ct import ../src/fs/listing.bend as Ls import ../src/fs/entry.bend as En import ../src/host/fdcache.bend as Fc import ../src/fs/readonly.bend as Ro import ../src/fs/meta.bend as Mt # Shared directories: V.FsOp on the host (host/hvf.c, "Shared directories"). def Fs.w(+ws: +List, i: Nat) -> U32: Maybe.default(&2, U32, List.get(&2, U32, ws, i), 0) def Fs.w64(+ws: +List, i: Nat) -> U.U64: +i = i U.U64{Fs.w(ws, i), Fs.w(ws, 1n+i)} def Fs.chars(bs: +List) -> String: match bs: case Nil{}: "" case Con{b, rest}: SCon{Char.from_u32(b), Fs.chars(rest)} # Entries: (ino hi, ino lo, type, name length, name bytes)*. def Fs.entries(fuel: Nat, ws: +List) -> +List: match fuel: case 0n: [] case 1n+f: match ws: case Nil{}: [] case Con{+h, rest}: +rest = rest +len = U32.to_nat(Fs.w(rest, 2n)) name = Fs.chars(List.take(&2, U32, List.drop(&2, U32, rest, 3n), len)) V.FsEntry{name, U.U64{h, Fs.w(rest, 0n)}, Fs.w(rest, 1n)} <> Fs.entries(f, List.drop(&2, U32, rest, Nat.add(3n, len))) def Fs.attr(+ws: +List) -> V.FsAttr: V.FsAttr{Fs.w(ws, 1n), Fs.w(ws, 2n), Fs.w(ws, 3n), Fs.w(ws, 4n), Fs.w64(ws, 5n), Fs.w64(ws, 7n), Fs.w64(ws, 9n), Fs.w64(ws, 11n), Fs.w64(ws, 13n), Fs.w64(ws, 15n)} def Fs.ename(e: V.FsEntry) -> String: match e: case V.FsEntry{n, i, k}: n def Fs.decode.go(+tag: U32, +ws: +List) -> V.FsResult: +rest = List.drop(&2, U32, ws, 1n) Bool.pick(V.FsResult, U32.is_eq(tag, 0), V.FsOk{}, Bool.pick(V.FsResult, U32.is_eq(tag, 2), V.FsGotAttr{Fs.attr(ws)}, Bool.pick(V.FsResult, U32.is_eq(tag, 3), V.FsBytes{rest}, Bool.pick(V.FsResult, U32.is_eq(tag, 4), V.FsCount{Fs.w(ws, 1n)}, Bool.pick(V.FsResult, U32.is_eq(tag, 5), V.FsEntries{Fs.entries(List.length(&2, U32, rest), rest)}, Bool.pick(V.FsResult, U32.is_eq(tag, 6), V.FsLink{Fs.chars(rest)}, Bool.pick(V.FsResult, U32.is_eq(tag, 7), V.FsStatfsOk{Fs.w(ws, 1n), Fs.w64(ws, 2n), Fs.w64(ws, 4n), Fs.w64(ws, 6n), Fs.w64(ws, 8n), Fs.w64(ws, 10n)}, V.FsErr{Fs.w(ws, 1n)}))))))) # (tags 8 and 9 are 3 and 4 when the host opened a file for the operation) def Fs.tag(+t: U32) -> U32: Bool.pick(U32, U32.is_eq(t, 8), 3, Bool.pick(U32, U32.is_eq(t, 9), 4, t)) def Fs.decode(+ws: +List) -> V.FsResult: Fs.decode.go(Fs.tag(Fs.w(ws, 0n)), ws) # A file the host opened for an operation takes the slot Bend picks # (src/host/fdcache.bend: the least recently used). def Fs.placed(opened: Bool, +ws: +List) -> IO(V.FsResult): match opened: case False{}: IO.pure(V.FsResult, Fs.decode(ws)) case True{}: do IO: +st : +List <- H.Hvf.fdc_stamps() _ : U32 <- H.Hvf.fdc_put(Fc.Fdc.victim(st)) return Fs.decode(ws) # A host operation on a share: its paths' names vetted first # (src/fs/contain.bend: no empty part, no "." or ".."; the host walks them # without following a symlink). def Fs.run(sh: U32, +op: U32, p: String, p2: String, a: U.U64, b: U32, bytes: +List) -> IO(V.FsResult): match a: case U.U64{ah, al}: do IO: +ws : +List <- H.Hvf.fs(sh, (op .|. Bool.pick(U32, Fc.Fdc.invalidates(op), 256, 0) : U32), p, p2, ah, al, b, bytes) +t : U32 = Fs.w(ws, 0n) Fs.placed(U32.is_eq(t, 8) || U32.is_eq(t, 9), ws) def Fs.call(+sh: U32, +op: U32, +p: String, +p2: String, +a: U.U64, +b: U32, +bytes: +List) -> IO(V.FsResult): Bool.pick(IO(V.FsResult), Ct.Share.rel_ok(p) && (U32.is_ne(op, 7) || Ct.Share.rel_ok(p2)), Fs.run(sh, op, p, p2, a, b, bytes), IO.pure(V.FsResult, V.FsErr{Ct.Share.einval()})) # The page of a listing the guest asked for (src/fs/entry.bend: which # entries, with what type; src/fs/listing.bend: in what order). def Fs.paged(r: V.FsResult, +from: U32, +max: U32) -> V.FsResult: match r: case V.FsEntries{es}: V.FsEntries{Ls.Listing.page(En.Entry.view(es), from, max)} case _: r def Host.fs.do(sh: U32, op: V.FsOp) -> IO(V.FsResult): match op: case V.FsStat{p}: Fs.call(sh, 0, p, "", U.U64.zero(), 0, []) case V.FsList{p, from, max}: IO.bind(V.FsResult, V.FsResult, Fs.call(sh, 1, p, "", U.U64.zero(), 0, []), r => IO.pure(V.FsResult, Fs.paged(r, from, max))) case V.FsRead{p, off, len}: Fs.call(sh, 2, p, "", off, len, []) case V.FsWrite{p, off, bs}: Fs.call(sh, 3, p, "", off, 0, bs) case V.FsCreate{p, mode}: Fs.call(sh, 4, p, "", U.U64.zero(), En.Entry.perm(mode), []) case V.FsMkdir{p, mode}: Fs.call(sh, 5, p, "", U.U64.zero(), En.Entry.perm(mode), []) case V.FsRemove{p, dir}: Fs.call(sh, 6, p, "", U.U64.zero(), Bool.to_u32(dir), []) case V.FsRename{from, to}: Fs.call(sh, 7, from, to, U.U64.zero(), 0, []) case V.FsTruncate{p, size}: Fs.call(sh, 8, p, "", size, 0, []) case V.FsChmod{p, mode}: Fs.call(sh, 9, p, "", U.U64.zero(), En.Entry.perm(mode), []) case V.FsSymlink{p, target}: Fs.call(sh, 10, p, target, U.U64.zero(), 0, []) case V.FsReadlink{p}: Fs.call(sh, 11, p, "", U.U64.zero(), 0, []) case V.FsStatfs{}: Fs.call(sh, 12, "", "", U.U64.zero(), 0, []) case V.FsReadRam{p, off, iovs}: Fs.call(sh, 13, p, "", off, 0, H.Host.iovs(iovs)) case V.FsWriteRam{p, off, iovs}: Fs.call(sh, 14, p, "", off, 0, H.Host.iovs(iovs)) case V.FsXattr{p, n}: IO.pure(V.FsResult, V.FsErr{45}) # EOPNOTSUPP (macOS 45; Linux 95): a plain share serves no extended attributes # A write past the share's quota (src/fs/quota.bend) fails with ENOSPC # before it happens. def Host.fs.quota(ok: Bool, sh: U32, op: V.FsOp) -> IO(V.FsResult): match ok: case True{}: Host.fs.do(sh, op) case False{}: IO.pure(V.FsResult, V.FsErr{Qt.Quota.enospc()}) def Host.fs.grows(none: Bool, +growth: U.U64, +sh: U32, op: V.FsOp) -> IO(V.FsResult): match none: case True{}: Host.fs.do(sh, op) case False{}: do IO: +ws : +List <- H.Hvf.share_usage(sh) Host.fs.quota(Qt.Quota.allows(Fs.w64(ws, 0n), growth, Fs.w64(ws, 2n)), sh, op) def Host.fs.rw(+sh: U32, +op: V.FsOp) -> IO(V.FsResult): +g = Qt.Quota.growth(op) Host.fs.grows(U.U64.is_zero(g), g, sh, op) # A share marked meta (src/fs/meta.bend): every guest path goes to its host # path through the directories' records, loaded from their .bend-meta the # first time (Hvf.meta_load), and attributes, listings and extended # attributes come from the records. It is read-only: Host.fs refused every # change before this. # A sidecar's text: read in 64 KiB pieces until one comes short. def Host.meta.bytes(r: V.FsResult) -> +List: match r: case V.FsBytes{bs}: bs case _: [] def Host.meta.read(fuel: Nat, +idx: U32, +path: String, +off: U.U64, +acc: +List) -> IO(+List): match fuel: case 0n: IO.pure(+List, acc) case 1n+f: do IO<+List>: r : V.FsResult <- Fs.call(idx, 2, path, "", off, 65536, []) +bs : +List = Host.meta.bytes(r) +n : U32 = U32.from_nat(List.length(&2, U32, bs)) Bool.pick(IO(+List), U32.is_lt(n, 65536), IO.pure(+List, List.append(&2, U32, acc, bs)), Host.meta.read(f, idx, path, U.U64.add_u32(off, n), List.append(&2, U32, acc, bs))) # Loads the table of the guest directory gdir, whose host path is hostdir. def Host.meta.load(+sh: U32, +gdir: String, +hostdir: String) -> IO(U32): do IO: bs : +List <- Host.meta.read(256n, Ro.Ro.index(sh), Mt.Meta.join(hostdir, Mt.Meta.file()), U.U64.zero(), []) H.Hvf.meta_load(sh, gdir, Mt.Meta.table(Mt.Meta.chars(bs))) # Loads the table a resolve asked for; True when it did (ask again). def Host.meta.load_need(+sh: U32, +p: String, r: Mt.Found) -> IO(Bool): match r: case Mt.NeedTable{k, hostdir}: do IO: _ : U32 <- Host.meta.load(sh, Mt.Meta.prefix(p, k), hostdir) return True{} case _: IO.pure(Bool, False{}) # A guest path's host path and record line (Missing: no such name); each # directory along it loaded first (fuel: the path's depth, and some). def Host.meta.resolve(fuel: Nat, +sh: U32, +p: String) -> IO(Mt.Found): match fuel: case 0n: IO.pure(Mt.Found, Mt.Missing{}) case 1n+f: do IO: +ws : +List <- H.Hvf.meta_resolve(sh, p) +r : Mt.Found = Mt.Meta.found(ws) +again : Bool <- Host.meta.load_need(sh, p, r) Bool.pick(IO(Mt.Found), again, Host.meta.resolve(f, sh, p), IO.pure(Mt.Found, r)) def Host.meta.depth(+p: String) -> Nat: Nat.add(4n, List.length(&2, String, String.split(p, '/'))) def Host.meta.stat.rec(m: Maybe<&2, Mt.Rec>, a: V.FsAttr, +p: String) -> V.FsResult: match m: case None{}: V.FsGotAttr{a} case Some{rec}: Mt.Meta.attr(rec, a, p) # The attributes of what a resolved path names. def Host.meta.stat.got(r: V.FsResult, line: Maybe<&2, String>, +p: String) -> V.FsResult: match r: case V.FsGotAttr{a}: match line: case None{}: V.FsGotAttr{a} case Some{l}: Host.meta.stat.rec(Mt.Meta.line(l), a, p) case _: r def Host.meta.xattr.val(m: Maybe<&2, +List>) -> V.FsResult: match m: case None{}: V.FsErr{61} case Some{v}: V.FsBytes{v} def Host.meta.xattr.rec(m: Maybe<&2, Mt.Rec>, +name: String) -> V.FsResult: match m: case None{}: V.FsErr{61} case Some{rec}: Host.meta.xattr.val(Mt.Meta.xattr(rec, name)) def Host.meta.xattr(line: Maybe<&2, String>, +name: String) -> V.FsResult: match line: case None{}: V.FsErr{61} case Some{l}: Host.meta.xattr.rec(Mt.Meta.line(l), name) # A directory's listing: its records, or the host's when it has none. def Host.meta.list.got(es: Maybe<&2, List<&2, V.FsEntry>>, +idx: U32, +host: String, +from: U32, +max: U32) -> IO(V.FsResult): match es: case None{}: Host.fs.do(idx, V.FsList{host, from, max}) case Some{l}: IO.pure(V.FsResult, Fs.paged(V.FsEntries{l}, from, max)) def Host.meta.list(+sh: U32, +p: String, +host: String, +from: U32, +max: U32) -> IO(V.FsResult): do IO: _ : Mt.Found <- Host.meta.resolve(Host.meta.depth(p), sh, Mt.Meta.join(p, ".")) +ws : +List <- H.Hvf.meta_list(sh, p) Host.meta.list.got(Mt.Meta.entries(p, ws), Ro.Ro.index(sh), host, from, max) def Host.meta.on(+idx: U32, +sh: U32, op: V.FsOp, +host: String, line: Maybe<&2, String>) -> IO(V.FsResult): match op: case V.FsStat{p}: IO.bind(V.FsResult, V.FsResult, Host.fs.do(idx, V.FsStat{host}), a => IO.pure(V.FsResult, Host.meta.stat.got(a, line, p))) case V.FsList{p, from, max}: Host.meta.list(sh, p, host, from, max) case V.FsRead{p, off, len}: Host.fs.do(idx, V.FsRead{host, off, len}) case V.FsReadRam{p, off, iovs}: Host.fs.do(idx, V.FsReadRam{host, off, iovs}) case V.FsReadlink{p}: Host.fs.do(idx, V.FsReadlink{host}) case V.FsXattr{p, name}: IO.pure(V.FsResult, Host.meta.xattr(line, name)) case V.FsStatfs{}: Host.fs.do(idx, V.FsStatfs{}) case _: IO.pure(V.FsResult, V.FsErr{Ro.Ro.erofs()}) # One operation, once its path resolved. def Host.meta.op(+sh: U32, op: V.FsOp, r: Mt.Found) -> IO(V.FsResult): match r: case Mt.Missing{}: IO.pure(V.FsResult, V.FsErr{2}) case Mt.NeedTable{k, h}: IO.pure(V.FsResult, V.FsErr{5}) case Mt.Resolved{+host, line}: Host.meta.on(Ro.Ro.index(sh), sh, op, host, line) def Host.meta.path(op: V.FsOp) -> String: match op: case V.FsStat{p}: p case V.FsList{p, f, m}: p case V.FsRead{p, o, l}: p case V.FsReadRam{p, o, i}: p case V.FsReadlink{p}: p case V.FsXattr{p, n}: p case _: "" def Host.meta(+sh: U32, +op: V.FsOp) -> IO(V.FsResult): +p : String = Host.meta.path(op) IO.bind(Mt.Found, V.FsResult, Host.meta.resolve(Host.meta.depth(p), sh, p), r => Host.meta.op(sh, op, r)) # A share's operation: on a read-only share (src/fs/readonly.bend) every # change is refused with EROFS before the host sees it; the host gets the # share's index. def Host.fs(+sh: U32, +op: V.FsOp) -> IO(V.FsResult): Bool.pick(IO(V.FsResult), Ro.Ro.refuses(sh, op), IO.pure(V.FsResult, V.FsErr{Ro.Ro.erofs()}), Bool.pick(IO(V.FsResult), Mt.Meta.is_meta(sh), Host.meta(sh, op), Host.fs.rw(Ro.Ro.index(sh), op)))