txtfs

A textual disk filesystem for Plan 9
git clone git://git.stellar-nexus.ru/txtfs
Log | Files | Refs

commit 9809511e7ae6f2694e1647f78e8f199eecff5c6a
Author: Plat <plat@stellar-nexus.ru>
Date:   Sun, 20 Sep 2026 02:43:36 +0000

Initial commit

Diffstat:
Ablk.c | 325+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Ablk_test.c | 31+++++++++++++++++++++++++++++++
Adir.c | 541+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Aexample_dir | 43+++++++++++++++++++++++++++++++++++++++++++
Afrag.c | 10++++++++++
Afs.c | 47+++++++++++++++++++++++++++++++++++++++++++++++
Afs.h | 103+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asuper.c | 141+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
8 files changed, 1241 insertions(+), 0 deletions(-)

diff --git a/blk.c b/blk.c @@ -0,0 +1,325 @@ +#include <u.h> +#include <libc.h> + +#include "fs.h" + +int diskfd; + + +#ifdef DOUBLECHECK +int +blkread(u64int blkno, void *buf) +{ + vlong off = (vlong)blkno * BLKSZ; + + int s = pread(diskfd, buf, BLKSZ, off); + if(s != BLKSZ) + return -1; + char buf2[BLKSZ]; + s = pread(diskfd, buf2, BLKSZ, off); + if(s != BLKSZ) + return -1; + if(memcmp(buf, buf2, BLKSZ) != 0) { + werrstr("Block %ulld read failure", blkno); + return -1; + } + return 0; +} +#endif +#ifndef DOUBLECHECK +int +blkread(u64int blkno, void *buf) +{ + vlong off = (vlong)blkno * BLKSZ; + return pread(diskfd, buf, BLKSZ, off) == BLKSZ ? 0 : -1; +} +#endif + +#ifdef DOUBLECHECK +int +blkwrite(u64int blkno, void *buf) +{ + vlong off = (vlong)blkno * BLKSZ; + + int s = pwrite(diskfd, buf, BLKSZ, off); + if(s != BLKSZ) + return -1; + char buf2[BLKSZ]; + s = pread(diskfd, buf2, BLKSZ, off); + if(s != BLKSZ) + return -1; + if(memcmp(buf, buf2, BLKSZ) != 0) { + werrstr("Block %ulld write failure", blkno); + return -1; + } + return 0; +} +#endif +#ifndef DOUBLECHECK +int +blkwrite(u64int blkno, void *buf) +{ + vlong off = (vlong)blkno * BLKSZ; + return pwrite(diskfd, buf, BLKSZ, off) == BLKSZ ? 0 : -1; +} +#endif + +int +blkcopy(u64int from, u64int to, void *buf) +{ + vlong off = (vlong)from * BLKSZ; + if(pread(diskfd, buf, BLKSZ, off) != BLKSZ) + return -1; + off = (vlong)to * BLKSZ; + return pwrite(diskfd, buf, BLKSZ, off) == BLKSZ ? 0 : -1; +} + +int +balloc(u64int start, u64int end) +{ +#ifdef DEFENSIVE + if(start > end || end > super.nblocks) { + werrstr("Invalid block range"); + return -1; + } +#endif + + u32int spos = start % BLKSZ; + u32int epos = end % BLKSZ; + start = TBLKBLK + start / BLKSZ; + end = TBLKBLK + end / BLKSZ; + + char buf[BLKSZ]; + if(blkread(start, buf) < 0) { + werrstr("block table read failure"); + return -1; + } + + if(start == end) { + memset(buf + spos, 'U', epos + 1 - spos); + if(blkwrite(start, buf) < 0) { + werrstr("block table write failure"); + return -1; + } + return 0; + } + + memset(buf + spos, 'U', BLKSZ - spos); + if(blkwrite(start, buf) < 0) { + werrstr("block table write failure"); + return -1; + } + + memset(buf, 'U', BLKSZ); + for(; start + 1 <= end - 1; ++start) { + if(blkwrite(start, buf) < 0) { + werrstr("block table write failure"); + return -1; + } + } + + if(blkread(end, buf) < 0) { + werrstr("block table read failure"); + return -1; + } + memset(buf, 'U', epos + 1); + if(blkwrite(end, buf) < 0) { + werrstr("block table write failure"); + return -1; + } + + return 0; +} + +int +bfree(u64int start, u64int end) +{ +#ifdef DEFENSIVE + if(start > end || end > super.nblocks) { + werrstr("Invalid block range"); + return -1; + } +#endif + + u32int spos = start % BLKSZ; + u32int epos = end % BLKSZ; + start = TBLKBLK + start / BLKSZ; + end = TBLKBLK + end / BLKSZ; + + char buf[BLKSZ]; + if(blkread(start, buf) < 0) { + werrstr("block table read failure"); + return -1; + } + + if(start == end) { + memset(buf + spos, 'F', epos + 1 - spos); + if(blkwrite(start, buf) < 0) { + werrstr("block table write failure"); + return -1; + } + return 0; + } + + memset(buf + spos, 'F', BLKSZ - spos); + if(blkwrite(start, buf) < 0) { + werrstr("block table write failure"); + return -1; + } + + memset(buf, 'F', BLKSZ); + for(; start + 1 <= end - 1; ++start) { + if(blkwrite(start, buf) < 0) { + werrstr("block table write failure"); + return -1; + } + } + + if(blkread(end, buf) < 0) { + werrstr("block table read failure"); + return -1; + } + memset(buf, 'F', epos + 1); + if(blkwrite(end, buf) < 0) { + werrstr("block table write failure"); + return -1; + } + + return 0; +} + +int +bused(u64int start, u64int end) +{ +#ifdef DEFENSIVE + if(start > end || end >= super.nblocks) { + werrstr("Invalid block range"); + return -1; + } +#endif + + u32int spos = start % BLKSZ; + u32int epos = end % BLKSZ; + start = TBLKBLK + start / BLKSZ; + end = TBLKBLK + end / BLKSZ; + + char buf[BLKSZ]; + if(blkread(start, buf) < 0) { + werrstr("block table read failure"); + return -1; + } + if(start == end) { + for(u32int pos = spos; pos <= epos; ++pos) { +#ifdef DEFENSIVE + switch(buf[pos]) { + case 'F': + break; + case 'U': + return 1; + default: + werrstr("block table illegal value at pos %ud", pos); + return -1; + } +#endif +#ifndef DEFENSIVE + if(buf[pos] == 'U') + return 1; +#endif + } + return 0; + } + + for(u32int pos = spos; pos < BLKSZ; ++pos) { +#ifdef DEFENSIVE + switch(buf[pos]) { + case 'F': + break; + case 'U': + return 1; + default: + werrstr("block table illegal value"); + return -1; + } +#endif +#ifndef DEFENSIVE + if(buf[pos] == 'U') + return 1; +#endif + } + + for(; start + 1 <= end - 1; ++start) { + if(blkread(start, buf) < 0) { + werrstr("block table read failure"); + return -1; + } + for(u32int pos = 0; pos < BLKSZ; ++pos) { +#ifdef DEFENSIVE + switch(buf[pos]) { + case 'F': + break; + case 'U': + return 1; + default: + werrstr("block table illegal value"); + return -1; + } +#endif +#ifndef DEFENSIVE + if(buf[pos] == 'U') + return 1; +#endif + } + } + + if(blkread(end, buf) < 0) { + werrstr("block table read failure"); + return -1; + } + for(u32int pos = 0; pos <= epos; ++pos) { +#ifdef DEFENSIVE + switch(buf[pos]) { + case 'F': + break; + case 'U': + return 1; + default: + werrstr("block table illegal value"); + return -1; + } +#endif +#ifndef DEFENSIVE + if(buf[pos] == 'U') + return 1; +#endif + } + + return 0; +} + +/* caller does the hint */ +u64int +bfindfree(u64int n) +{ + u64int good, ogrover = rover; + + for(good = 0; rover < super.nblocks; ++rover) { + if(bused(rover, rover)) + good = 0; + else if(++good == n) + return rover - good + 1; + } + + for(rover = super.rootblk + ROOTSIZ; rover < ogrover; ++rover) { + if(bused(rover, rover)) + good = 0; + else if(++good == n) + return rover - good + 1; + } + + for(; !bused(rover, rover) && rover < super.nblocks; ++rover) { + if(++good == n) + return rover - good + 1; + + return 0; + +} diff --git a/blk_test.c b/blk_test.c @@ -0,0 +1,31 @@ +#include <u.h> +#include <libc.h> + +#include "fs.h" + + +void +main(int argc, char *argv[]) +{ + uchar buf[BLKSZ]; + + if(argc != 2) + sysfatal("usage: %s diskfile", argv[0]); + diskfd = open(argv[1], ORDWR); + if(diskfd < 0) + sysfatal("open: %r"); + + memset(buf, 'A', BLKSZ); + if(blkwrite(0, buf) < 0) + sysfatal("blkwrite: %r"); + + memset(buf, 0, BLKSZ); + if(blkread(0, buf) < 0) + sysfatal("blkread: %r"); + + if(buf[0] != 'A' || buf[BLKSZ-1] != 'A') + sysfatal("data mismatch"); + + print("blk I/O ok\n"); + exits(nil); +} diff --git a/dir.c b/dir.c @@ -0,0 +1,541 @@ +#include <u.h> +#include <libc.h> + +#include "fs.h" + +u64int /* this returns a byte position! */ +getdentry(u64int dirblk, u64int size, u64int block) +{ + u64int nblks, blk, pos; + char buf[BLKSZ]; + char target[BLOCKSIZ+1]; + char field[STRSIZ+1]; + + seprint(target, target + sizeof(target), "%ulld", block); + nblks = size / BLKSZ + (size % BLKSZ > 0); + + for(blk = dirblk; blk < dirblk + nblks; ++blk) { + if(blkread(blk, buf) < 0) { + werrstr("blkread: %r"); + return 0; + } + + for(pos = 0; pos + DENTRYSIZ <= BLKSZ; pos += DENTRYSIZ) { + if(buf[pos] == 'd' || buf[pos] == '#') + continue; + +#ifdef DEFENSIVE + if(buf[pos] != '+') { + werrstr("block %ulld: bad status '%c' at entry %ulld", + blk, buf[pos], (blk - dirblk) * (BLKSZ/DENTRYSIZ) + pos/DENTRYSIZ); + return 0; + } +#endif + + getfield(buf + pos, BLOCK, BLOCKSIZ, field); + if(strtoull(field, nil, 10) == block) { + return blk * BLKSZ + pos; + } + } + } + + return 0; /* not found */ +} + +void +getfield(char *buf, int offset, int size, char *out) +{ + int i; + + for(i = 0; i < size; ++i) + out[i] = buf[offset + i]; + out[size] = '\0'; + + for(i = size - 1; i >= 0 && out[i] == ' '; --i) + out[i] = '\0'; +} + +void +writefield(char *buf, int offset, int size, char *in) +{ + int i; + + for(i = 0; in[i] != '\0'; ++i) + buf[offset + i] = in[i]; + + for(; i < size; ++i) + buf[offset + i] = ' '; +} + +static int +isleap(u32int year) +{ + return year % 4 == 0 && (year % 100 != 0 || year % 400 == 0); +} + +static u64int +iso2unix(char *buf) +{ +#ifdef DEFENSIVE + if(buf[4] != '-' + || buf[7] != '-' + || buf[10] != 'T' + || buf[13] != ':' + || buf[16] != ':' + || buf[19] != 'Z') { + werrstr("wrong time format"); + return 0; + } +#endif + + u32int y, m, d, i; + u64int days = 0; + + y = strtoul(buf, nil, 10); + m = strtoul(buf + 5, nil, 10); + d = strtoul(buf + 8, nil, 10); + + for(i = 1970; i < y; ++i) + days += 365 + isleap(i); + + int mdays[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; + if(isleap(y)) + mdays[1] = 29; + + for(i = 0; i < m - 1; ++i) + days += mdays[i]; + + days += d - 1; + + return days * 86400 + + strtoul(buf + 11, nil, 10) * 3600 + + strtoul(buf + 14, nil, 10) * 60 + + strtoul(buf + 17, nil, 10); +} + +static char * +unix2iso(char *buf, u64int t) +{ + u32int y = 1970, m, d, h, min, s; + u64int days = t / 86400; + u32int secs = t % 86400; + int mdays[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; + + while(days >= 365 + isleap(y)) { + days -= 365 + isleap(y); + ++y; + } + + if(isleap(y)) + mdays[1] = 29; + + m = 0; + while(m < 12 && days >= mdays[m]) { + days -= mdays[m]; + ++m; + } + + d = days + 1; + h = secs / 3600; + min = (secs % 3600) / 60; + s = secs % 60; + + snprint(buf, 20, "%04ud-%02ud-%02udT%02ud:%02ud:%02udZ", y, m+1, d, h, min, s); + return buf; +} + +#ifdef DEFENSIVE +static ulong +rwx2num(char *buf) +{ + ulong p = 0; + + if(buf[0] == 'r') + p |= (1 << 8); + else if(buf[0] != '-') + goto err; + if(buf[1] == 'w') + p |= (1 << 7); + else if(buf[1] != '-') + goto err; + if(buf[2] == 'x') + p |= (1 << 6); + else if(buf[2] != 'x') + goto err; + + if(buf[3] == 'r') + p |= (1 << 5); + else if(buf[3] != '-') + goto err; + if(buf[4] == 'w') + p |= (1 << 4); + else if(buf[4] != '-') + goto err; + if(buf[5] == 'x') + p |= (1 << 3); + else if(buf[5] != '-') + goto err; + + if(buf[6] == 'r') + p |= (1 << 2); + else if(buf[6] != '-') + goto err; + if(buf[7] == 'w') + p |= (1 << 1); + else if(buf[7] != '-') + goto err; + if(buf[8] == 'x') + p |= (1 << 0); + else if(buf[8] != '-') + goto err; + + return p; + +err: + werrstr("incorrect permissions format"); + return 0; +} +#endif +#ifndef DEFENSIVE +static ulong +rwx2num(char *buf) +{ + ulong p = 0; + + if(buf[0] == 'r') + p |= (1 << 8); + if(buf[1] == 'w') + p |= (1 << 7); + if(buf[2] == 'x') + p |= (1 << 6); + + if(buf[3] == 'r') + p |= (1 << 5); + if(buf[4] == 'w') + p |= (1 << 4); + if(buf[5] == 'x') + p |= (1 << 3); + + if(buf[6] == 'r') + p |= (1 << 2); + if(buf[7] == 'w') + p |= (1 << 1); + if(buf[8] == 'x') + p |= (1 << 0); + + + return p; +} +#endif + +static char * +num2rwx(char *buf, ulong mode) +{ + buf[0] = (mode & (1 << 8)) ? 'r' : '-'; + buf[1] = (mode & (1 << 7)) ? 'w' : '-'; + buf[2] = (mode & (1 << 6)) ? 'x' : '-'; + + buf[3] = (mode & (1 << 5)) ? 'r' : '-'; + buf[4] = (mode & (1 << 4)) ? 'w' : '-'; + buf[5] = (mode & (1 << 3)) ? 'x' : '-'; + + buf[6] = (mode & (1 << 2)) ? 'r' : '-'; + buf[7] = (mode & (1 << 1)) ? 'w' : '-'; + buf[8] = (mode & (1 << 0)) ? 'x' : '-'; + + buf[9] = '\0'; + + return buf; +} + +Dir * +fsstat(u64int dentrypos) +{ + u64int pos; /* to avoid a conversion */ + u64int blk; + char buf[BLKSZ]; + char field[STRSIZ+1]; + + pos = dentrypos % BLKSZ; + blk = dentrypos / BLKSZ; + + if(blkread(blk, buf) < 0) { + werrstr("blkread: %r"); + return nil; + } + + Dir *out = calloc(1, sizeof(Dir)); + if(out == nil) { + werrstr("calloc: %r"); + return nil; + } + + /* Qid */ + getfield(buf + pos, BLOCK, BLOCKSIZ, field); + out->qid.path = strtoull(field, nil, 10); + + /* Type */ /* add fragmentation support here */ + getfield(buf + pos, TYPE, TYPESIZ, field); + if(strcmp(field, " DIR") == 0) { + out->qid.type = QTDIR; + out->mode = DMDIR; + } else if(strcmp(field, " APPD") == 0) { + out->qid.type = QTAPPEND; + out->mode = DMAPPEND; + } else if(strcmp(field, " EXCL") == 0) { + out->qid.type = QTEXCL; + out->mode = DMEXCL; + } else { + out->qid.type = QTFILE; + out->mode = 0; + } + + /* Perms */ + getfield(buf + pos, PERMS, PERMSSIZ, field); + out->mode |= rwx2num(field); + + /* Name */ + getfield(buf + pos, NAME, NAMESIZ, field); + out->name = strdup(field); + if(out->name == nil) + goto nomem; + + /* Size */ + getfield(buf + pos, SIZE, SIZESIZ, field); + out->length = strtoll(field, nil, 10); + + /* UID */ + getfield(buf + pos, UID, UIDSIZ, field); + out->uid = strdup(field); + if(out->uid == nil) + goto nomem; + + /* GID */ + getfield(buf + pos, GID, GIDSIZ, field); + out->gid = strdup(field); + if(out->gid == nil) + goto nomem; + + /* MUID */ + getfield(buf + pos, MUID, MUIDSIZ, field); + out->muid = strdup(field); + if(out->muid == nil) + goto nomem; + + /* Times */ + getfield(buf + pos, MTIME, MTIMESIZ, field); + out->mtime = iso2unix(field); +#ifdef DEFENSIVE + if(out->mtime == 0) { + werrstr("iso2unix: %r"); + goto err; + } +#endif + + getfield(buf + pos, ATIME, ATIMESIZ, field); + out->atime = iso2unix(field); +#ifdef DEFENSIVE + if(out->atime == 0) { + werrstr("iso2unix: %r"); + goto err; + } +#endif + + return out; + +nomem: + werrstr("malloc: %r"); +err: + free(out->name); + free(out->uid); + free(out->gid); + free(out->muid); + free(out); + return nil; +} + + +u64int +dir1up(u64int dirblk, u64int dirsize, + Qid *parent, u64int parentsize) +{ + u64int parentblk = parent->path; + u64int parentblks = parentsize / BLKSZ + (parentsize % BLKSZ > 0); +#ifdef DEFENSIVE + if(dirblk == super.rootblk || parentblk == SUPERBLK) { + werrstr("Cannot extend root directory"); + return 0; + /* stub - you should actually extend here */ + } +#endif + + u64int dirnblks = dirsize / BLKSZ + (dirsize % BLKSZ > 0); + + u64int nextblk = dirblk + dirnblks; + if(nextblk < super.nblocks && !bused(nextblk, nextblk)) { + if(balloc(nextblk, nextblk) < 0) { + werrstr("balloc: %r"); + return 0; + } + /* caller must fill the new block immediately and update size */ + + return dirblk; + } + + + + + + u64int newblk = bfindfree(dirnblks + 1, nextblk); + if(newblk == 0) { + werrstr("no space for %ulld contiguous blocks", dirnblks + 1); + return 0; + } + + if(balloc(newblk, newblk + dirnblks) < 0) { + werrstr("balloc: %r"); + return 0; + } + + char buf[BLKSZ]; + for(u64int i = 0; i < dirnblks; i++) { + if(blkcopy(dirblk + i, newblk + i, buf) < 0) { + werrstr("blkcopy: %r"); + if(bfree(newblk, newblk + dirnblks) < 0) + werrstr("blkcopy and bfree: %r"); + return 0; + } + } + + u64int pos = getdentry(parentblk, parentblks, dirblk); + if(pos == 0) { + werrstr("getdentry: %r"); + return 0; + } + u64int dblk = pos / BLKSZ; + pos = pos % BLKSZ; + if(blkread(dblk, buf) < 0) { + werrstr("blkread: %r"); + return 0; + } + char field[BLOCKSIZ + 1]; + seprint(field, field + sizeof(field), "%ulld", newblk); + writefield(buf + pos % BLKSZ, BLOCK, BLOCKSIZ, field); + if(blkwrite(dblk, buf) < 0) { + werrstr("blkwrite: %r"); + return 0; + } + INCR_VERS(parent->vers); + + if(bfree(dirblk, dirblk + dirnblks - 1) < 0) { + werrstr("bfree: %r"); + return 0; + } + + return newblk; +} + +int /* add a root case. also make it so that it is impossible to update the directory, but not the parent */ +initdentry(Qid *dir, u64int dirsize, + Qid *parent, u64int parentsize, + Dir *file) +{ + u64int dirblk = dir->path, parentblk = parent->path; + u64int blk, pos; + u64int nblks = dirsize / BLKSZ + (dirsize % BLKSZ > 0); + char buf[BLKSZ], field[STRSIZ+1]; + + for(blk = dirblk; blk < dirblk + nblks; ++blk) { + if(blkread(blk, buf) < 0) { + werrstr("blkread: %r"); + return -1; + } + + for(pos = 0; pos + DENTRYSIZ <= BLKSZ; pos += DENTRYSIZ) { + if(buf[pos] == 'd') + goto found; + } + } + + + dirblk = dir1up(dirblk, dirsize, parent, parentsize); + if(dirblk == 0) { + werrstr("dir1up: %r"); + return 0; + } + dir->path = dirblk; + ++nblks; + + blk = dirblk + nblks - 1; + if(blkread(blk, buf) < 0) { + werrstr("blkread: %r"); + return -1; + } + pos = 0; + +found: + buf[pos] = '+'; + + seprint(field, field + sizeof(field), "%ulld", file->qid.path); + writefield(buf + pos, BLOCK, BLOCKSIZ, field); + + writefield(buf + pos, NAME, NAMESIZ, file->name); + + if(file->qid.type & QTDIR) + writefield(buf + pos, TYPE, TYPESIZ, " DIR "); + else if(file->qid.type & QTAPPEND) + writefield(buf + pos, TYPE, TYPESIZ, " APPD "); + else if(file->qid.type & QTEXCL) + writefield(buf + pos, TYPE, TYPESIZ, " EXCL "); + else + writefield(buf + pos, TYPE, TYPESIZ, " FILE "); + + seprint(field, field + sizeof(field), "%lld", file->length); + writefield(buf + pos, SIZE, SIZESIZ, field); + + num2rwx(field, file->mode & 0777); + writefield(buf + pos, PERMS, PERMSSIZ, field); + + writefield(buf + pos, UID, UIDSIZ, file->uid); + writefield(buf + pos, GID, GIDSIZ, file->gid); + writefield(buf + pos, MUID, MUIDSIZ, file->muid); + + unix2iso(field, file->mtime); + writefield(buf + pos, MTIME, MTIMESIZ, field); + + unix2iso(field, file->atime); + writefield(buf + pos, ATIME, ATIMESIZ, field); + + + if(blkwrite(blk, buf) < 0) { + werrstr("blkwrite: %r"); + return -1; + } + INCR_VERS(dir->vers); + + pos = getdentry(parentblk, parentsize, dirblk); + if(pos == 0) { + werrstr("getdentry: %r"); + return -1; + } + blk = pos / BLKSZ + (pos % BLKSZ > 0); + pos = pos % BLKSZ; + if(blkread(blk, buf) < 0) { + werrstr("blkread: %r"); + return -1; + } + + /* read size and write it back??? you need to update it! although that should be done inside dir1up */ + getfield(buf + pos, SIZE, SIZESIZ, field); + u64int size = strtoull(field, nil, 10); + seprint(field, field + sizeof(field), "%ulld", size); + writefield(buf + pos, SIZE, SIZESIZ, field); + if(blkwrite(blk, buf) < 0) { + werrstr("blkwrite: %r"); + return -1; + } + INCR_VERS(parent->vers); + + + return 0; +} diff --git a/example_dir b/example_dir @@ -0,0 +1,43 @@ +0name000...<TAB>block000...<TAB>size000...<TAB>perms000<TAB>type<TAB>uid000...<TAB>gid000...<TAB>btime000...<TAB>mtime000...<TAB>ctime000... +#foobar ;12845 ;1300 --rw-r--r-- FILE glenda glenda 15085655 15085655 15085655 +64 20 20 7? 4 64 64 20 20 20 + + ;foobar ;FILE;1300 ;DAET--rw-r--r--;glenda ;glenda ;glenda ;13005655 ;13005655 ;13005655 ;12845 ;12059 ; + foobar FILE 1300 DAET--rw-r--r-- glenda glenda glenda 13005655 13005655 13005655 12845 12059 + + + + ++ +foobar +FILE +1300 +DAET--rw-r--r-- +glenda +glenda +glenda +13005655 +13005655 +13005655 +12845 +12059 +this is a file for my logs + +#From Rob: +The photo album below stinks! + ++ +photos +DIR +1300 +DAET--rw-r--r-- +glenda +glenda +glenda +13005655 +13005655 +13005655 +12845 +12059 +these are from the sea! +had a great time in there diff --git a/frag.c b/frag.c @@ -0,0 +1,10 @@ +#include <u.h> +#include <libc.h> + +#include "fs.h" + +int +isfragmented(u64int dentrypos) +{ + +} diff --git a/fs.c b/fs.c @@ -0,0 +1,47 @@ +#include <u.h> +#include <libc.h> +#include <fcall.h> +#include <thread.h> +#include <9p.h> + +int +ream(void) +{ + uchar buf[BLKSZ]; + uchar *p; + uchar *e; + e = buf + sizeof(buf); + + p = tbuf; + p = strecpy(p, e, MAGIC); + p = seprint(p, e, BLKSZ); + p = seprint(p, e, NAMELEN); + p = seprint(p, e, ROOTBLK); + + /* write the block table here */ + + return blkwrite(0, buf); + /* don't forget to expose superblock in the service + in the ream case in main */ +} + +int +fileread(u64int startblk, void *buf, long count, vlong offset) +{ + /* TODO: handle splits and locks */ + /* also don't forget that count needs to be MIN(filesize, count) + and account for block sizes with that too */ + + /* startblk += offset; may read unintended data + on negative offset */ + + uchar *p = (uchar *)buf; + for(startblk += offset; startblk < count; ++startblk) { + if(blkread(startblk, p) < 0) /* is the check worth it? + also, does it convert ? */ + /* <error out> here */; + /* bounds check here? */ + + p += BLKSZ; + } +} diff --git a/fs.h b/fs.h @@ -0,0 +1,103 @@ +#define DEFENSIVE +#define DOUBLECHECK + +#define MIN(x,y) ((x) < (y) ? (x) : (y)) +#define MAX(x,y) ((x) > (y) ? (x) : (y)) + +enum { + BLKSZ = 4096, + SUPERBLK = 0, + TBLKBLK = 1, + ROOTSIZ = 128, /* blocks */ +}; + +#define DENTRYSIZ 1024 +#define DEL 1 +enum { + STATUSSIZ = 1, + BLOCKSIZ = 20, + NAMESIZ = 64, + TYPESIZ = 5, + SIZESIZ = 20, + PERMSSIZ = 9, + UIDSIZ = 64, + GIDSIZ = 64, + MUIDSIZ = 64, + MTIMESIZ = 20, + ATIMESIZ = 20, +}; +#define STRSIZ 64 /* maximum of the above */ +enum { + BLOCK = STATUSSIZ + DEL, + NAME = BLOCK + BLOCKSIZ + DEL, + TYPE = NAME + NAMESIZ + DEL, + SIZE = TYPE + TYPESIZ + DEL, + PERMS = SIZE + SIZESIZ + DEL, + UID = PERMS + PERMSSIZ + DEL, + GID = UID + UIDSIZ + DEL, + MUID = GID + GIDSIZ + DEL, + MTIME = MUID + MUIDSIZ + DEL, + ATIME = MTIME + MTIMESIZ + DEL, + CUSTOM = ATIME + ATIMESIZ + DEL, +}; +#define CUSTOMSIZ (DENTRYSIZ - CUSTOM - DEL) +enum { + FRAGS = STATUSSIZ + DEL, + FRAGSIZ = STATUSSIZ + DEL + BLOCKSIZ + DEL + SIZESIZ + DEL, + MAXFRAGS = (DENTRYSIZ - FRAGS) / FRAGSIZ, +}; + + +#define MAXDEPTH 16 +#define SMALLGAP 16 /* the gap which the allocator skips */ +typedef struct Frag Frag; +struct Frag { + char status; // find a way to remove this + u64int block; + u64int size; +}; + +typedef struct File File; +struct File { + Dir *dir; + uint nfrags; // pad for cache..? + Frag *frags; +}; + +#define INCR_VERS(x) \ + do { \ + ++(x); \ + (x) = ((x) & 0x00FFFFFFU) | (super.epoch); \ + } while(0) + + +typedef struct Super Super; +struct Super { + u64int nblocks; + u64int rootblk; + u32int epoch; +}; + +extern Super super; +extern int diskfd; +extern u64int rover; + +int blkread(u64int, void *); +int blkwrite(u64int, void *); +int balloc(u64int, u64int); +int bfree(u64int, u64int); +int bused(u64int, u64int); +int ream(u64int); +int superread(void); + +int blkcopy(u64int, u64int, void *); +u64int bfindfree(u64int, u64int); + +void getfield(char *, int, int, char *); +void writefield(char *, int, int, char *); + +u64int getdentry(u64int, u64int, u64int); +Dir *fsstat(u64int); +int initdentry(Qid *, u64int, Qid *, u64int, Dir *); + + diff --git a/super.c b/super.c @@ -0,0 +1,141 @@ +#include <u.h> +#include <libc.h> + +#include "fs.h" + + +Super super; + +int +ream(u64int nblocks) +{ + char buf[BLKSZ], buf2[BLKSZ]; + char *p; + + u64int rootblk = TBLKBLK + nblocks/BLKSZ + (nblocks % BLKSZ > 0); + + memset(buf, 0, BLKSZ); + seprint(buf, buf+sizeof(buf), "%s %ulld %c\n", + "TXTF", nblocks, '0'); + + if(blkwrite(SUPERBLK, buf) < 0) + return -1; + if(blkread(SUPERBLK, buf2) < 0) + return -1; + if(memcmp(buf, buf2, BLKSZ) != 0) { + werrstr("Superblock write failure"); + return -1; + } + + /* filling 'U'sed here */ + memset(buf, 'U', BLKSZ); + u32int curblk = TBLKBLK; + u32int nublocks = rootblk - 1; + + while(curblk < TBLKBLK + nublocks/BLKSZ + ROOTSIZ) { + if(blkwrite(curblk, buf) < 0) + return -1; + if(blkread(curblk, buf2) < 0) + return -1; + if(memcmp(buf, buf2, BLKSZ) != 0) { + werrstr("Block bitmap write failure"); + return -1; + } + ++curblk; + } + + memset(buf, 'F', BLKSZ); + memset(buf, 'U', nublocks%BLKSZ); + if(blkwrite(curblk, buf) < 0) + return -1; + if(blkread(curblk, buf2) < 0) + return -1; + if(memcmp(buf, buf2, BLKSZ) != 0) { + werrstr("Block bitmap write failure"); + return -1; + } + ++curblk; + + /* filling 'F'ree here */ + memset(buf, 'F', BLKSZ); + while(curblk < TBLKBLK + nblocks/BLKSZ) { + if(blkwrite(curblk, buf) < 0) + return -1; + if(blkread(curblk, buf2) < 0) + return -1; + if(memcmp(buf, buf2, BLKSZ) != 0) { + werrstr("Block bitmap write failure"); + return -1; + } + ++curblk; + } + memset(buf, 0, BLKSZ); + memset(buf, 'F', nblocks%BLKSZ); + if(blkwrite(curblk, buf) < 0) + return -1; + if(blkread(curblk, buf2) < 0) + return -1; + if(memcmp(buf, buf2, BLKSZ) != 0) { + werrstr("Block bitmap write failure"); + return -1; + } + ++curblk; + + + Dir *empty = calloc(1, sizeof(Dir)); + if(empty == nil) { + werrstr("calloc: %r"); + return -1; + } + superread(); +/* + for(u64int end = curblk + ROOTSIZ; curblk < end; ++curblk) { + for(dentry = 0; dentry < BLKSZ/DENTRYSIZ; ++dentry) + //keep working here +;} +*/ + return 0; +} + +int +superread(void) +{ + char buf[BLKSZ]; + char *p; + char *sup_str[3]; + + if(blkread(SUPERBLK, buf) < 0) + return -1; + if(tokenize(buf, sup_str, 3) < 3) { + werrstr("corrupted superblock"); + return -1; + } + if(strcmp(sup_str[0], "TXTF") != 0) { + werrstr("bad magic"); + return -1; + } + + if((super.nblocks = strtoull(sup_str[1], nil, 10)) <= 0) { + werrstr("impossible number of blocks"); + return -1; + } + + super.rootblk = + TBLKBLK + + super.nblocks/BLKSZ + + (super.nblocks % BLKSZ > 0) + + 1; + + uchar epoch = strtoul(sup_str[2], nil, 10); + ++epoch; + seprint(buf, buf+sizeof(buf), "%s %ulld %uld\n", + "TXTF", super.nblocks, epoch); + if(blkwrite(SUPERBLK, buf) < 0) { + werrstr("Cannot write to superblock"); + return -1; + } + super.epoch = ((u32int)epoch) << 24; + + + return 0; +}