Complete reformat

This commit is contained in:
Imbus 2024-06-15 16:55:06 +02:00
parent b1a34398e9
commit d6793bf093
60 changed files with 1952 additions and 1995 deletions

View file

@ -5,14 +5,19 @@
#include <fcntl.h>
#include <assert.h>
#define stat xv6_stat // avoid clash with host struct stat
#define stat xv6_stat // avoid clash with host struct stat
#include "kernel/types.h"
#include "kernel/fs.h"
#include "kernel/stat.h"
#include "kernel/param.h"
#ifndef static_assert
#define static_assert(a, b) do { switch (0) case 0: case (a): ; } while (0)
#define static_assert(a, b) \
do { \
switch(0) \
case 0: \
case(a):; \
} while(0)
#endif
#define NINODES 200
@ -20,22 +25,21 @@
// Disk layout:
// [ boot block | sb block | log | inode blocks | free bit map | data blocks ]
int nbitmap = FSSIZE/(BSIZE*8) + 1;
int nbitmap = FSSIZE / (BSIZE * 8) + 1;
int ninodeblocks = NINODES / IPB + 1;
int nlog = LOGSIZE;
int nmeta; // Number of meta blocks (boot, sb, nlog, inode, bitmap)
int nblocks; // Number of data blocks
int nmeta; // Number of meta blocks (boot, sb, nlog, inode, bitmap)
int nblocks; // Number of data blocks
int fsfd;
int fsfd;
struct superblock sb;
char zeroes[BSIZE];
uint freeinode = 1;
uint freeblock;
char zeroes[BSIZE];
uint freeinode = 1;
uint freeblock;
void balloc(int);
void wsect(uint, void*);
void winode(uint, struct dinode*);
void wsect(uint, void *);
void winode(uint, struct dinode *);
void rinode(uint inum, struct dinode *ip);
void rsect(uint sec, void *buf);
uint ialloc(ushort type);
@ -47,7 +51,7 @@ ushort
xshort(ushort x)
{
ushort y;
u8 *a = (u8*)&y;
u8 *a = (u8 *)&y;
a[0] = x;
a[1] = x >> 8;
return y;
@ -57,7 +61,7 @@ uint
xint(uint x)
{
uint y;
u8 *a = (u8*)&y;
u8 *a = (u8 *)&y;
a[0] = x;
a[1] = x >> 8;
a[2] = x >> 16;
@ -68,16 +72,15 @@ xint(uint x)
int
main(int argc, char *argv[])
{
int i, cc, fd;
uint rootino, inum, off;
int i, cc, fd;
uint rootino, inum, off;
struct dirent de;
char buf[BSIZE];
char buf[BSIZE];
struct dinode din;
static_assert(sizeof(int) == 4, "Integers must be 4 bytes!");
if(argc < 2){
if(argc < 2) {
fprintf(stderr, "Usage: mkfs fs.img files...\n");
exit(1);
}
@ -85,7 +88,7 @@ main(int argc, char *argv[])
assert((BSIZE % sizeof(struct dinode)) == 0);
assert((BSIZE % sizeof(struct dirent)) == 0);
fsfd = open(argv[1], O_RDWR|O_CREAT|O_TRUNC, 0666);
fsfd = open(argv[1], O_RDWR | O_CREAT | O_TRUNC, 0666);
if(fsfd < 0)
die(argv[1]);
@ -99,13 +102,13 @@ main(int argc, char *argv[])
sb.ninodes = xint(NINODES);
sb.nlog = xint(nlog);
sb.logstart = xint(2);
sb.inodestart = xint(2+nlog);
sb.bmapstart = xint(2+nlog+ninodeblocks);
sb.inodestart = xint(2 + nlog);
sb.bmapstart = xint(2 + nlog + ninodeblocks);
printf("nmeta %d (boot, super, log blocks %u inode blocks %u, bitmap blocks %u) blocks %d total %d\n",
nmeta, nlog, ninodeblocks, nbitmap, nblocks, FSSIZE);
printf("nmeta %d (boot, super, log blocks %u inode blocks %u, bitmap blocks %u) blocks %d total %d\n", nmeta, nlog,
ninodeblocks, nbitmap, nblocks, FSSIZE);
freeblock = nmeta; // the first free block that we can allocate
freeblock = nmeta; // the first free block that we can allocate
for(i = 0; i < FSSIZE; i++)
wsect(i, zeroes);
@ -127,14 +130,14 @@ main(int argc, char *argv[])
strcpy(de.name, "..");
iappend(rootino, &de, sizeof(de));
for(i = 2; i < argc; i++){
for(i = 2; i < argc; i++) {
// get rid of "user/"
char *shortname;
if(strncmp(argv[i], "user/", 5) == 0)
shortname = argv[i] + 5;
else
shortname = argv[i];
assert(index(shortname, '/') == 0);
if((fd = open(argv[i], 0)) < 0)
@ -163,7 +166,7 @@ main(int argc, char *argv[])
// fix size of root inode dir
rinode(rootino, &din);
off = xint(din.size);
off = ((off/BSIZE) + 1) * BSIZE;
off = ((off / BSIZE) + 1) * BSIZE;
din.size = xint(off);
winode(rootino, &din);
@ -184,13 +187,13 @@ wsect(uint sec, void *buf)
void
winode(uint inum, struct dinode *ip)
{
char buf[BSIZE];
uint bn;
char buf[BSIZE];
uint bn;
struct dinode *dip;
bn = IBLOCK(inum, sb);
rsect(bn, buf);
dip = ((struct dinode*)buf) + (inum % IPB);
dip = ((struct dinode *)buf) + (inum % IPB);
*dip = *ip;
wsect(bn, buf);
}
@ -198,13 +201,13 @@ winode(uint inum, struct dinode *ip)
void
rinode(uint inum, struct dinode *ip)
{
char buf[BSIZE];
uint bn;
char buf[BSIZE];
uint bn;
struct dinode *dip;
bn = IBLOCK(inum, sb);
rsect(bn, buf);
dip = ((struct dinode*)buf) + (inum % IPB);
dip = ((struct dinode *)buf) + (inum % IPB);
*ip = *dip;
}
@ -220,7 +223,7 @@ rsect(uint sec, void *buf)
uint
ialloc(ushort type)
{
uint inum = freeinode++;
uint inum = freeinode++;
struct dinode din;
bzero(&din, sizeof(din));
@ -234,14 +237,14 @@ ialloc(ushort type)
void
balloc(int used)
{
u8 buf[BSIZE];
u8 buf[BSIZE];
int i;
printf("balloc: first %d blocks have been allocated\n", used);
assert(used < BSIZE*8);
assert(used < BSIZE * 8);
bzero(buf, BSIZE);
for(i = 0; i < used; i++){
buf[i/8] = buf[i/8] | (0x1 << (i%8));
for(i = 0; i < used; i++) {
buf[i / 8] = buf[i / 8] | (0x1 << (i % 8));
}
printf("balloc: write bitmap block at sector %d\n", sb.bmapstart);
wsect(sb.bmapstart, buf);
@ -252,34 +255,34 @@ balloc(int used)
void
iappend(uint inum, void *xp, int n)
{
char *p = (char*)xp;
uint fbn, off, n1;
char *p = (char *)xp;
uint fbn, off, n1;
struct dinode din;
char buf[BSIZE];
uint indirect[NINDIRECT];
uint x;
char buf[BSIZE];
uint indirect[NINDIRECT];
uint x;
rinode(inum, &din);
off = xint(din.size);
// printf("append inum %d at off %d sz %d\n", inum, off, n);
while(n > 0){
while(n > 0) {
fbn = off / BSIZE;
assert(fbn < MAXFILE);
if(fbn < NDIRECT){
if(xint(din.addrs[fbn]) == 0){
if(fbn < NDIRECT) {
if(xint(din.addrs[fbn]) == 0) {
din.addrs[fbn] = xint(freeblock++);
}
x = xint(din.addrs[fbn]);
} else {
if(xint(din.addrs[NDIRECT]) == 0){
if(xint(din.addrs[NDIRECT]) == 0) {
din.addrs[NDIRECT] = xint(freeblock++);
}
rsect(xint(din.addrs[NDIRECT]), (char*)indirect);
if(indirect[fbn - NDIRECT] == 0){
rsect(xint(din.addrs[NDIRECT]), (char *)indirect);
if(indirect[fbn - NDIRECT] == 0) {
indirect[fbn - NDIRECT] = xint(freeblock++);
wsect(xint(din.addrs[NDIRECT]), (char*)indirect);
wsect(xint(din.addrs[NDIRECT]), (char *)indirect);
}
x = xint(indirect[fbn-NDIRECT]);
x = xint(indirect[fbn - NDIRECT]);
}
n1 = min(n, (fbn + 1) * BSIZE - off);
rsect(x, buf);