2006-07-21 13:18:04 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <fcntl.h>
|
2006-07-27 21:10:00 +00:00
|
|
|
#include <assert.h>
|
2011-01-10 15:43:40 +00:00
|
|
|
|
|
|
|
#define stat xv6_stat // avoid clash with host struct stat
|
2019-06-11 13:57:14 +00:00
|
|
|
#include "kernel/types.h"
|
|
|
|
#include "kernel/fs.h"
|
|
|
|
#include "kernel/stat.h"
|
|
|
|
#include "kernel/param.h"
|
2006-07-21 13:18:04 +00:00
|
|
|
|
2015-08-13 02:11:39 +00:00
|
|
|
#ifndef static_assert
|
2012-09-11 01:58:18 +00:00
|
|
|
#define static_assert(a, b) do { switch (0) case 0: case (a): ; } while (0)
|
2015-08-13 02:11:39 +00:00
|
|
|
#endif
|
2012-09-07 21:39:04 +00:00
|
|
|
|
2015-04-03 12:22:02 +00:00
|
|
|
#define NINODES 200
|
|
|
|
|
|
|
|
// Disk layout:
|
2015-06-27 16:39:13 +00:00
|
|
|
// [ boot block | sb block | log | inode blocks | free bit map | data blocks ]
|
2015-04-03 12:22:02 +00:00
|
|
|
|
2015-04-10 11:15:06 +00:00
|
|
|
int nbitmap = FSSIZE/(BSIZE*8) + 1;
|
2015-04-03 12:22:02 +00:00
|
|
|
int ninodeblocks = NINODES / IPB + 1;
|
2016-08-25 13:13:00 +00:00
|
|
|
int nlog = LOGSIZE;
|
2015-06-27 16:39:13 +00:00
|
|
|
int nmeta; // Number of meta blocks (boot, sb, nlog, inode, bitmap)
|
2015-04-03 12:22:02 +00:00
|
|
|
int nblocks; // Number of data blocks
|
2006-07-21 13:18:04 +00:00
|
|
|
|
2006-07-27 21:10:00 +00:00
|
|
|
int fsfd;
|
2006-07-21 13:18:04 +00:00
|
|
|
struct superblock sb;
|
2015-04-03 12:22:02 +00:00
|
|
|
char zeroes[BSIZE];
|
2006-07-27 21:10:00 +00:00
|
|
|
uint freeinode = 1;
|
2015-04-03 12:22:02 +00:00
|
|
|
uint freeblock;
|
|
|
|
|
2006-07-21 13:18:04 +00:00
|
|
|
|
2006-08-09 01:09:36 +00:00
|
|
|
void balloc(int);
|
2006-09-06 17:27:19 +00:00
|
|
|
void wsect(uint, void*);
|
|
|
|
void winode(uint, struct dinode*);
|
2006-08-14 14:13:52 +00:00
|
|
|
void rinode(uint inum, struct dinode *ip);
|
2006-07-21 13:18:04 +00:00
|
|
|
void rsect(uint sec, void *buf);
|
2006-07-27 21:10:00 +00:00
|
|
|
uint ialloc(ushort type);
|
|
|
|
void iappend(uint inum, void *p, int n);
|
2020-10-21 07:03:17 +00:00
|
|
|
void die(const char *);
|
2006-07-21 13:18:04 +00:00
|
|
|
|
|
|
|
// convert to intel byte order
|
|
|
|
ushort
|
|
|
|
xshort(ushort x)
|
|
|
|
{
|
|
|
|
ushort y;
|
2011-01-11 18:01:13 +00:00
|
|
|
uchar *a = (uchar*)&y;
|
2006-07-21 13:18:04 +00:00
|
|
|
a[0] = x;
|
|
|
|
a[1] = x >> 8;
|
|
|
|
return y;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint
|
|
|
|
xint(uint x)
|
|
|
|
{
|
|
|
|
uint y;
|
2011-01-11 18:01:13 +00:00
|
|
|
uchar *a = (uchar*)&y;
|
2006-07-21 13:18:04 +00:00
|
|
|
a[0] = x;
|
|
|
|
a[1] = x >> 8;
|
|
|
|
a[2] = x >> 16;
|
|
|
|
a[3] = x >> 24;
|
|
|
|
return y;
|
|
|
|
}
|
|
|
|
|
2006-09-07 13:07:39 +00:00
|
|
|
int
|
2006-07-21 13:18:04 +00:00
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
2006-07-27 21:10:00 +00:00
|
|
|
int i, cc, fd;
|
2007-08-27 23:26:33 +00:00
|
|
|
uint rootino, inum, off;
|
2006-07-27 21:10:00 +00:00
|
|
|
struct dirent de;
|
2015-04-03 12:22:02 +00:00
|
|
|
char buf[BSIZE];
|
2006-08-14 14:13:52 +00:00
|
|
|
struct dinode din;
|
2006-07-21 13:18:04 +00:00
|
|
|
|
2012-09-11 01:58:18 +00:00
|
|
|
|
|
|
|
static_assert(sizeof(int) == 4, "Integers must be 4 bytes!");
|
|
|
|
|
2006-07-27 21:10:00 +00:00
|
|
|
if(argc < 2){
|
|
|
|
fprintf(stderr, "Usage: mkfs fs.img files...\n");
|
2006-07-21 13:18:04 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2015-04-03 12:22:02 +00:00
|
|
|
assert((BSIZE % sizeof(struct dinode)) == 0);
|
|
|
|
assert((BSIZE % sizeof(struct dirent)) == 0);
|
2006-07-21 13:18:04 +00:00
|
|
|
|
2006-07-27 21:10:00 +00:00
|
|
|
fsfd = open(argv[1], O_RDWR|O_CREAT|O_TRUNC, 0666);
|
2020-10-21 07:03:17 +00:00
|
|
|
if(fsfd < 0)
|
|
|
|
die(argv[1]);
|
2006-07-21 13:18:04 +00:00
|
|
|
|
2015-06-27 16:39:13 +00:00
|
|
|
// 1 fs block = 1 disk sector
|
|
|
|
nmeta = 2 + nlog + ninodeblocks + nbitmap;
|
|
|
|
nblocks = FSSIZE - nmeta;
|
2015-04-03 12:22:02 +00:00
|
|
|
|
2019-06-04 09:57:47 +00:00
|
|
|
sb.magic = FSMAGIC;
|
2015-04-10 11:15:06 +00:00
|
|
|
sb.size = xint(FSSIZE);
|
2015-06-27 16:39:13 +00:00
|
|
|
sb.nblocks = xint(nblocks);
|
2015-04-03 12:22:02 +00:00
|
|
|
sb.ninodes = xint(NINODES);
|
2011-07-28 00:35:46 +00:00
|
|
|
sb.nlog = xint(nlog);
|
2015-06-27 16:39:13 +00:00
|
|
|
sb.logstart = xint(2);
|
|
|
|
sb.inodestart = xint(2+nlog);
|
|
|
|
sb.bmapstart = xint(2+nlog+ninodeblocks);
|
2006-07-21 13:18:04 +00:00
|
|
|
|
2015-06-27 16:39:13 +00:00
|
|
|
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);
|
2006-07-21 13:18:04 +00:00
|
|
|
|
2015-04-03 12:22:02 +00:00
|
|
|
freeblock = nmeta; // the first free block that we can allocate
|
2006-08-09 01:09:36 +00:00
|
|
|
|
2015-04-10 11:15:06 +00:00
|
|
|
for(i = 0; i < FSSIZE; i++)
|
2006-07-21 13:18:04 +00:00
|
|
|
wsect(i, zeroes);
|
|
|
|
|
2011-01-02 22:59:57 +00:00
|
|
|
memset(buf, 0, sizeof(buf));
|
|
|
|
memmove(buf, &sb, sizeof(sb));
|
|
|
|
wsect(1, buf);
|
2006-07-21 13:18:04 +00:00
|
|
|
|
2006-07-27 21:10:00 +00:00
|
|
|
rootino = ialloc(T_DIR);
|
2008-10-08 18:57:13 +00:00
|
|
|
assert(rootino == ROOTINO);
|
2006-07-27 21:10:00 +00:00
|
|
|
|
|
|
|
bzero(&de, sizeof(de));
|
|
|
|
de.inum = xshort(rootino);
|
|
|
|
strcpy(de.name, ".");
|
|
|
|
iappend(rootino, &de, sizeof(de));
|
|
|
|
|
|
|
|
bzero(&de, sizeof(de));
|
|
|
|
de.inum = xshort(rootino);
|
|
|
|
strcpy(de.name, "..");
|
|
|
|
iappend(rootino, &de, sizeof(de));
|
|
|
|
|
|
|
|
for(i = 2; i < argc; i++){
|
2019-06-11 13:57:14 +00:00
|
|
|
// 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);
|
2006-08-24 02:44:41 +00:00
|
|
|
|
2020-10-21 07:03:17 +00:00
|
|
|
if((fd = open(argv[i], 0)) < 0)
|
|
|
|
die(argv[i]);
|
2016-08-25 13:13:00 +00:00
|
|
|
|
2006-09-07 13:07:39 +00:00
|
|
|
// Skip leading _ in name when writing to file system.
|
|
|
|
// The binaries are named _rm, _cat, etc. to keep the
|
|
|
|
// build operating system from trying to execute them
|
|
|
|
// in place of system binaries like rm and cat.
|
2019-06-11 13:57:14 +00:00
|
|
|
if(shortname[0] == '_')
|
|
|
|
shortname += 1;
|
2006-07-27 21:10:00 +00:00
|
|
|
|
|
|
|
inum = ialloc(T_FILE);
|
|
|
|
|
|
|
|
bzero(&de, sizeof(de));
|
|
|
|
de.inum = xshort(inum);
|
2019-06-11 13:57:14 +00:00
|
|
|
strncpy(de.name, shortname, DIRSIZ);
|
2006-07-27 21:10:00 +00:00
|
|
|
iappend(rootino, &de, sizeof(de));
|
2006-09-06 17:27:19 +00:00
|
|
|
|
2006-07-27 21:10:00 +00:00
|
|
|
while((cc = read(fd, buf, sizeof(buf))) > 0)
|
|
|
|
iappend(inum, buf, cc);
|
|
|
|
|
|
|
|
close(fd);
|
|
|
|
}
|
2006-07-21 13:18:04 +00:00
|
|
|
|
2006-08-14 14:13:52 +00:00
|
|
|
// fix size of root inode dir
|
|
|
|
rinode(rootino, &din);
|
2006-09-06 17:27:19 +00:00
|
|
|
off = xint(din.size);
|
2006-08-14 14:13:52 +00:00
|
|
|
off = ((off/BSIZE) + 1) * BSIZE;
|
|
|
|
din.size = xint(off);
|
|
|
|
winode(rootino, &din);
|
|
|
|
|
2015-04-03 12:22:02 +00:00
|
|
|
balloc(freeblock);
|
2006-08-09 01:09:36 +00:00
|
|
|
|
2006-07-21 13:18:04 +00:00
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
wsect(uint sec, void *buf)
|
|
|
|
{
|
2020-10-21 07:03:17 +00:00
|
|
|
if(lseek(fsfd, sec * BSIZE, 0) != sec * BSIZE)
|
|
|
|
die("lseek");
|
|
|
|
if(write(fsfd, buf, BSIZE) != BSIZE)
|
|
|
|
die("write");
|
2006-07-21 13:18:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
winode(uint inum, struct dinode *ip)
|
|
|
|
{
|
2015-04-03 12:22:02 +00:00
|
|
|
char buf[BSIZE];
|
2006-07-21 13:18:04 +00:00
|
|
|
uint bn;
|
|
|
|
struct dinode *dip;
|
|
|
|
|
2015-06-27 16:39:13 +00:00
|
|
|
bn = IBLOCK(inum, sb);
|
2006-07-21 13:18:04 +00:00
|
|
|
rsect(bn, buf);
|
2011-01-11 18:01:13 +00:00
|
|
|
dip = ((struct dinode*)buf) + (inum % IPB);
|
2006-07-21 13:18:04 +00:00
|
|
|
*dip = *ip;
|
|
|
|
wsect(bn, buf);
|
2006-07-27 21:10:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
rinode(uint inum, struct dinode *ip)
|
|
|
|
{
|
2015-04-03 12:22:02 +00:00
|
|
|
char buf[BSIZE];
|
2006-07-27 21:10:00 +00:00
|
|
|
uint bn;
|
|
|
|
struct dinode *dip;
|
|
|
|
|
2015-06-27 16:39:13 +00:00
|
|
|
bn = IBLOCK(inum, sb);
|
2006-07-27 21:10:00 +00:00
|
|
|
rsect(bn, buf);
|
2011-01-11 18:01:13 +00:00
|
|
|
dip = ((struct dinode*)buf) + (inum % IPB);
|
2006-07-27 21:10:00 +00:00
|
|
|
*ip = *dip;
|
2006-07-21 13:18:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
rsect(uint sec, void *buf)
|
|
|
|
{
|
2020-10-21 07:03:17 +00:00
|
|
|
if(lseek(fsfd, sec * BSIZE, 0) != sec * BSIZE)
|
|
|
|
die("lseek");
|
|
|
|
if(read(fsfd, buf, BSIZE) != BSIZE)
|
|
|
|
die("read");
|
2006-07-21 13:18:04 +00:00
|
|
|
}
|
2006-07-27 21:10:00 +00:00
|
|
|
|
|
|
|
uint
|
|
|
|
ialloc(ushort type)
|
|
|
|
{
|
|
|
|
uint inum = freeinode++;
|
|
|
|
struct dinode din;
|
|
|
|
|
|
|
|
bzero(&din, sizeof(din));
|
|
|
|
din.type = xshort(type);
|
|
|
|
din.nlink = xshort(1);
|
|
|
|
din.size = xint(0);
|
|
|
|
winode(inum, &din);
|
|
|
|
return inum;
|
|
|
|
}
|
|
|
|
|
2006-08-09 01:09:36 +00:00
|
|
|
void
|
|
|
|
balloc(int used)
|
|
|
|
{
|
2015-04-03 12:22:02 +00:00
|
|
|
uchar buf[BSIZE];
|
2006-08-09 01:09:36 +00:00
|
|
|
int i;
|
|
|
|
|
|
|
|
printf("balloc: first %d blocks have been allocated\n", used);
|
2015-04-03 12:22:02 +00:00
|
|
|
assert(used < BSIZE*8);
|
|
|
|
bzero(buf, BSIZE);
|
2011-01-11 18:01:13 +00:00
|
|
|
for(i = 0; i < used; i++){
|
2006-08-09 01:09:36 +00:00
|
|
|
buf[i/8] = buf[i/8] | (0x1 << (i%8));
|
|
|
|
}
|
2015-06-27 16:39:13 +00:00
|
|
|
printf("balloc: write bitmap block at sector %d\n", sb.bmapstart);
|
|
|
|
wsect(sb.bmapstart, buf);
|
2006-08-09 01:09:36 +00:00
|
|
|
}
|
|
|
|
|
2006-07-27 21:10:00 +00:00
|
|
|
#define min(a, b) ((a) < (b) ? (a) : (b))
|
|
|
|
|
|
|
|
void
|
|
|
|
iappend(uint inum, void *xp, int n)
|
|
|
|
{
|
2011-01-11 18:01:13 +00:00
|
|
|
char *p = (char*)xp;
|
2006-07-27 21:10:00 +00:00
|
|
|
uint fbn, off, n1;
|
|
|
|
struct dinode din;
|
2015-04-03 12:22:02 +00:00
|
|
|
char buf[BSIZE];
|
2006-08-24 02:44:41 +00:00
|
|
|
uint indirect[NINDIRECT];
|
|
|
|
uint x;
|
2006-07-27 21:10:00 +00:00
|
|
|
|
|
|
|
rinode(inum, &din);
|
|
|
|
off = xint(din.size);
|
2015-06-27 16:39:13 +00:00
|
|
|
// printf("append inum %d at off %d sz %d\n", inum, off, n);
|
2006-07-27 21:10:00 +00:00
|
|
|
while(n > 0){
|
2015-04-03 12:22:02 +00:00
|
|
|
fbn = off / BSIZE;
|
2006-08-24 02:44:41 +00:00
|
|
|
assert(fbn < MAXFILE);
|
2011-01-11 18:01:13 +00:00
|
|
|
if(fbn < NDIRECT){
|
|
|
|
if(xint(din.addrs[fbn]) == 0){
|
2006-09-06 17:04:06 +00:00
|
|
|
din.addrs[fbn] = xint(freeblock++);
|
2006-08-24 02:44:41 +00:00
|
|
|
}
|
|
|
|
x = xint(din.addrs[fbn]);
|
|
|
|
} else {
|
2011-01-11 18:01:13 +00:00
|
|
|
if(xint(din.addrs[NDIRECT]) == 0){
|
2009-05-31 00:59:37 +00:00
|
|
|
din.addrs[NDIRECT] = xint(freeblock++);
|
2006-08-24 02:44:41 +00:00
|
|
|
}
|
2011-01-11 18:01:13 +00:00
|
|
|
rsect(xint(din.addrs[NDIRECT]), (char*)indirect);
|
|
|
|
if(indirect[fbn - NDIRECT] == 0){
|
2006-09-06 17:04:06 +00:00
|
|
|
indirect[fbn - NDIRECT] = xint(freeblock++);
|
2011-01-11 18:01:13 +00:00
|
|
|
wsect(xint(din.addrs[NDIRECT]), (char*)indirect);
|
2006-08-24 02:44:41 +00:00
|
|
|
}
|
|
|
|
x = xint(indirect[fbn-NDIRECT]);
|
2006-08-09 01:09:36 +00:00
|
|
|
}
|
2015-04-03 12:22:02 +00:00
|
|
|
n1 = min(n, (fbn + 1) * BSIZE - off);
|
2006-08-24 02:44:41 +00:00
|
|
|
rsect(x, buf);
|
2015-04-03 12:22:02 +00:00
|
|
|
bcopy(p, buf + off - (fbn * BSIZE), n1);
|
2006-08-24 02:44:41 +00:00
|
|
|
wsect(x, buf);
|
2006-07-27 21:10:00 +00:00
|
|
|
n -= n1;
|
|
|
|
off += n1;
|
|
|
|
p += n1;
|
|
|
|
}
|
|
|
|
din.size = xint(off);
|
|
|
|
winode(inum, &din);
|
|
|
|
}
|
2020-10-21 07:03:17 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
die(const char *s)
|
|
|
|
{
|
|
|
|
perror(s);
|
|
|
|
exit(1);
|
|
|
|
}
|