commit 3025d1cec268938d097a6379e1b960cecfd2e4a7
parent 30f9bc6b3a71e9fad70d35c42787aa9acb90ced3
Author: Plat <plat@stellar-nexus.ru>
Date: Fri, 25 Sep 2026 16:26:42 +0000
Expanded fragmentation support
Diffstat:
| M | frag.c | | | 174 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---------------- |
1 file changed, 140 insertions(+), 34 deletions(-)
diff --git a/frag.c b/frag.c
@@ -3,40 +3,6 @@
#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 = inodepos % BLKSZ + nfrag * FRAGSIZ;
- blk = inodepos / 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 */
@@ -109,3 +75,143 @@ addfrag(u64int inodepos, Frag *frag)
return out;
}
+
+
+/* hack */
+static int
+char2num(char c)
+{
+ switch(c) {
+ case '0': return 0;
+ case '1': return 1;
+ case '2': return 2;
+ case '3': return 3;
+ case '4': return 4;
+ case '5': return 5;
+ case '6': return 6;
+ case '7': return 7;
+ case '8': return 8;
+ case '9': return 9;
+ case 'A': case 'a': return 10;
+ case 'B': case 'b': return 11;
+ case 'C': case 'c': return 12;
+ case 'D': case 'd': return 13;
+ case 'E': case 'e': return 14;
+ case 'F': case 'f': return 15;
+ case 'G': case 'g': return 16;
+ case 'H': case 'h': return 17;
+ case 'I': case 'i': return 18;
+ case 'J': case 'j': return 19;
+ case 'K': case 'k': return 20;
+ case 'L': case 'l': return 21;
+ case 'M': case 'm': return 22;
+ case 'N': case 'n': return 23;
+ default: return -1;
+ }
+}
+
+uint
+countfrags(u64int inodepos, uint nfrag)
+{
+ u64int pos; /* to avoid a conversion */
+ u64int blk;
+ u64int count;
+ u64int inodepos2;
+ char buf[BLKSZ];
+ char field[STRSIZ+1];
+ char c;
+ uint out;
+
+ pos = inodepos % BLKSZ + nfrag * FRAGSIZ;
+ blk = inodepos / BLKSZ;
+
+ if(blkread(blk, buf) < 0) {
+ werrstr("blkread: %r");
+ return 0;
+ }
+
+ if(buf[pos] != 'f')
+ return 1;
+
+ getfield(buf + pos, BLOCK, BLOCKSIZ, field);
+ inodepos2 = strtoull(field, nil, 10);
+
+ getfield(buf + pos, SIZE, SIZESIZ, field);
+
+ for(count = 0; count != 20 && c = field[count] != ' '; ++count) {
+ out += countfrags(inodepos2, char2num(c));
+ }
+ return out;
+}
+
+static Frag *
+fragstat(u64int inodepos, uint nfrag)
+{
+ u64int pos; /* to avoid a conversion */
+ u64int blk;
+ char buf[BLKSZ];
+ char field[STRSIZ+1];
+
+ pos = inodepos % BLKSZ + nfrag * FRAGSIZ;
+ blk = inodepos / 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;
+ }
+
+ if(buf[pos] == 'f')
+ return out;
+
+ /* 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
+buildfragarr(u64int inodepos, uint nfrag, Frag *frags, uint *fragidx)
+{
+ u64int pos; /* to avoid a conversion */
+ u64int blk;
+ u64int count;
+ u64int inodepos2;
+ char buf[BLKSZ];
+ char field[STRSIZ+1];
+ char c;
+ uint out;
+
+ pos = inodepos % BLKSZ + nfrag * FRAGSIZ;
+ blk = inodepos / BLKSZ;
+
+ if(blkread(blk, buf) < 0) {
+ werrstr("blkread: %r");
+ return 1;
+ }
+
+ if(buf[pos] != 'f') {
+ frags[(*fragidx)++] = fragstat(inodepos, nfrag);
+ return 0;
+ }
+
+ getfield(buf + pos, BLOCK, BLOCKSIZ, field);
+ inodepos2 = strtoull(field, nil, 10);
+
+ getfield(buf + pos, SIZE, SIZESIZ, field);
+
+ for(count = 0; count != 20 && c = field[count] != ' '; ++count) {
+ out += countfrags(inodepos2, char2num(c), frags, fragidx);
+ }
+ return out;
+}