txtfs

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

commit 247bf998767f37d6ec43fa83f61e6c7e364a6fd7
parent 9809511e7ae6f2694e1647f78e8f199eecff5c6a
Author: Plat <plat@stellar-nexus.ru>
Date:   Sun, 20 Sep 2026 04:42:53 +0000

Added frag.c

Diffstat:
Mdir.c | 25+++++++++++++------------
Mfrag.c | 96+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--
Mfs.h | 13++++++-------
3 files changed, 113 insertions(+), 21 deletions(-)

diff --git a/dir.c b/dir.c @@ -21,11 +21,11 @@ getdentry(u64int dirblk, u64int size, u64int block) } for(pos = 0; pos + DENTRYSIZ <= BLKSZ; pos += DENTRYSIZ) { - if(buf[pos] == 'd' || buf[pos] == '#') + if(buf[pos] == 'd' || buf[pos] == '#' || buf[pos] == 'i') continue; #ifdef DEFENSIVE - if(buf[pos] != '+') { + if(buf[pos] != '+' || buf[pos] != 'f') { werrstr("block %ulld: bad status '%c' at entry %ulld", blk, buf[pos], (blk - dirblk) * (BLKSZ/DENTRYSIZ) + pos/DENTRYSIZ); return 0; @@ -247,9 +247,10 @@ num2rwx(char *buf, ulong mode) return buf; } - + + Dir * -fsstat(u64int dentrypos) +dirstat(u64int dentrypos) { u64int pos; /* to avoid a conversion */ u64int blk; @@ -274,15 +275,15 @@ fsstat(u64int dentrypos) getfield(buf + pos, BLOCK, BLOCKSIZ, field); out->qid.path = strtoull(field, nil, 10); - /* Type */ /* add fragmentation support here */ + /* Type */ getfield(buf + pos, TYPE, TYPESIZ, field); - if(strcmp(field, " DIR") == 0) { + if(strcmp(field, "DIR") == 0) { out->qid.type = QTDIR; out->mode = DMDIR; - } else if(strcmp(field, " APPD") == 0) { + } else if(strcmp(field, "APPD") == 0) { out->qid.type = QTAPPEND; out->mode = DMAPPEND; - } else if(strcmp(field, " EXCL") == 0) { + } else if(strcmp(field, "EXCL") == 0) { out->qid.type = QTEXCL; out->mode = DMEXCL; } else { @@ -482,13 +483,13 @@ found: writefield(buf + pos, NAME, NAMESIZ, file->name); if(file->qid.type & QTDIR) - writefield(buf + pos, TYPE, TYPESIZ, " DIR "); + writefield(buf + pos, TYPE, TYPESIZ, "DIR "); else if(file->qid.type & QTAPPEND) - writefield(buf + pos, TYPE, TYPESIZ, " APPD "); + writefield(buf + pos, TYPE, TYPESIZ, "APPD"); else if(file->qid.type & QTEXCL) - writefield(buf + pos, TYPE, TYPESIZ, " EXCL "); + writefield(buf + pos, TYPE, TYPESIZ, "EXCL"); else - writefield(buf + pos, TYPE, TYPESIZ, " FILE "); + writefield(buf + pos, TYPE, TYPESIZ, "FILE"); seprint(field, field + sizeof(field), "%lld", file->length); writefield(buf + pos, SIZE, SIZESIZ, field); diff --git a/frag.c b/frag.c @@ -3,8 +3,100 @@ #include "fs.h" +Frag * +fragstat(u64int inodepos, int nfrag) +{ + u64int pos; /* to avoid a conversion */ + u64int blk; + char buf[BLKSZ]; + char field[STRSIZ+1]; + + pos = dentrypos % BLKSZ + nfrag * FRAGSIZ; + blk = dentrypos / BLKSZ; + + if(blkread(blk, buf) < 0) { + werrstr("blkread: %r"); + return nil; + } + + Frag *out = calloc(1, sizeof(Frag)); + if(out == nil) { + werrstr("calloc: %r"); + return nil; + } + + + /* Block */ + getfield(buf + pos, BLOCK, BLOCKSIZ, field); + out->block = strtoull(field, nil, 10); + + /* Size */ + getfield(buf + pos, SIZE, SIZESIZ, field); + out->size = strtoull(field, nil, 10); + + return out; +} + +int +initinode(u64int inodepos) + u64int pos; /* to avoid a conversion */ + u64int blk; + char buf[BLKSZ]; + char field[FRAGSIZ+1]; + + pos = dentrypos % BLKSZ; + blk = dentrypos / BLKSZ; + + if(blkread(blk, buf) < 0) { + werrstr("blkread: %r"); + return nil; + } + + buf[pos] = 'i'; + buf[pos+1] = '\n'; + + field[0] = 'd'; + for(int i = 1; i < FRAGSIZ-1; ++i) + field[i] = ' '; + field[FRAGSIZ-1] = '\n'; + field[FRAGSIZ] = '\0'; + + for(int i = 0; i <= MAXFRAGS; ++i) + writefield(buf + pos + FRAGSIZ*i, FRAGS, FRAGSIZ, field); + + if(blkwrite(blk, buf) < 0) { + werrstr("blkwrite: %r"); + return -1; + } + + return 0; +} + int -isfragmented(u64int dentrypos) +addfrag(u64int inodepos, Frag *frag) { - + u64int pos; /* to avoid a conversion */ + u64int blk; + char buf[BLKSZ]; + char field[FRAGSIZ+1]; + + pos = dentrypos % BLKSZ; + blk = dentrypos / BLKSZ; + + if(blkread(blk, buf) < 0) { + werrstr("blkread: %r"); + return nil; + } + + + char out; + for(out = 0; out < MAXFRAGS; ++out) { + if(buf[pos+FRAGS+FRAGSIZ*out] == 'd') + break; + } + + if(buf[pos+FRAGS+FRAGSIZ*out] != 'd') + return -1; + + return out; } diff --git a/fs.h b/fs.h @@ -16,9 +16,9 @@ enum { enum { STATUSSIZ = 1, BLOCKSIZ = 20, - NAMESIZ = 64, - TYPESIZ = 5, SIZESIZ = 20, + NAMESIZ = 64, + TYPESIZ = 4, PERMSSIZ = 9, UIDSIZ = 64, GIDSIZ = 64, @@ -29,10 +29,10 @@ enum { #define STRSIZ 64 /* maximum of the above */ enum { BLOCK = STATUSSIZ + DEL, - NAME = BLOCK + BLOCKSIZ + DEL, + SIZE = BLOCK + BLOCKSIZ + DEL, + NAME = SIZE + SIZESIZ + DEL, TYPE = NAME + NAMESIZ + DEL, - SIZE = TYPE + TYPESIZ + DEL, - PERMS = SIZE + SIZESIZ + DEL, + PERMS = TYPE + TYPESIZ + DEL, UID = PERMS + PERMSSIZ + DEL, GID = UID + UIDSIZ + DEL, MUID = GID + GIDSIZ + DEL, @@ -52,7 +52,6 @@ enum { #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; }; @@ -97,7 +96,7 @@ void getfield(char *, int, int, char *); void writefield(char *, int, int, char *); u64int getdentry(u64int, u64int, u64int); -Dir *fsstat(u64int); +Dir *dirstat(u64int); int initdentry(Qid *, u64int, Qid *, u64int, Dir *);