Skip to content
Snippets Groups Projects
mkfs.c 5.64 KiB
Newer Older
rtm's avatar
rtm committed
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
rtm's avatar
rtm committed
#include <assert.h>

#define stat xv6_stat  // avoid clash with host struct stat
rtm's avatar
rtm committed
#include "types.h"
#include "fs.h"
#include "stat.h"
Frans Kaashoek's avatar
Frans Kaashoek committed
#include "param.h"
rtm's avatar
rtm committed

Frans Kaashoek's avatar
Frans Kaashoek committed
int nblocks = 985;
int nlog = LOGSIZE;
int ninodes = 200;
kaashoek's avatar
kaashoek committed
int size = 1024;
rtm's avatar
rtm committed

rtm's avatar
rtm committed
int fsfd;
rtm's avatar
rtm committed
struct superblock sb;
char zeroes[512];
uint freeblock;
kaashoek's avatar
kaashoek committed
uint usedblocks;
uint bitblocks;
rtm's avatar
rtm committed
uint freeinode = 1;
rtm's avatar
rtm committed

kaashoek's avatar
kaashoek committed
void balloc(int);
rsc's avatar
rsc committed
void wsect(uint, void*);
void winode(uint, struct dinode*);
void rinode(uint inum, struct dinode *ip);
rtm's avatar
rtm committed
void rsect(uint sec, void *buf);
rtm's avatar
rtm committed
uint ialloc(ushort type);
void iappend(uint inum, void *p, int n);
rtm's avatar
rtm committed

// convert to intel byte order
ushort
xshort(ushort x)
{
  ushort y;
Russ Cox's avatar
Russ Cox committed
  uchar *a = (uchar*)&y;
rtm's avatar
rtm committed
  a[0] = x;
  a[1] = x >> 8;
  return y;
}

uint
xint(uint x)
{
  uint y;
Russ Cox's avatar
Russ Cox committed
  uchar *a = (uchar*)&y;
rtm's avatar
rtm committed
  a[0] = x;
  a[1] = x >> 8;
  a[2] = x >> 16;
  a[3] = x >> 24;
  return y;
}

rsc's avatar
rsc committed
int
rtm's avatar
rtm committed
main(int argc, char *argv[])
{
rtm's avatar
rtm committed
  int i, cc, fd;
rsc's avatar
rsc committed
  uint rootino, inum, off;
rtm's avatar
rtm committed
  struct dirent de;
  char buf[512];
  struct dinode din;
rtm's avatar
rtm committed

rtm's avatar
rtm committed
  if(argc < 2){
    fprintf(stderr, "Usage: mkfs fs.img files...\n");
rtm's avatar
rtm committed
    exit(1);
  }

rtm's avatar
rtm committed
  assert((512 % sizeof(struct dinode)) == 0);
  assert((512 % sizeof(struct dirent)) == 0);
rtm's avatar
rtm committed

rtm's avatar
rtm committed
  fsfd = open(argv[1], O_RDWR|O_CREAT|O_TRUNC, 0666);
  if(fsfd < 0){
rtm's avatar
rtm committed
    perror(argv[1]);
    exit(1);
  }

kaashoek's avatar
kaashoek committed
  sb.size = xint(size);
  sb.nblocks = xint(nblocks); // so whole disk is size sectors
rtm's avatar
rtm committed
  sb.ninodes = xint(ninodes);
Frans Kaashoek's avatar
Frans Kaashoek committed
  sb.nlog = xint(nlog);
rtm's avatar
rtm committed

rsc's avatar
rsc committed
  bitblocks = size/(512*8) + 1;
kaashoek's avatar
kaashoek committed
  usedblocks = ninodes / IPB + 3 + bitblocks;
  freeblock = usedblocks;
rtm's avatar
rtm committed

Frans Kaashoek's avatar
Frans Kaashoek committed
  printf("used %d (bit %d ninode %zu) free %u log %u total %d\n", usedblocks,
         bitblocks, ninodes/IPB + 1, freeblock, nlog, nblocks+usedblocks+nlog);
kaashoek's avatar
kaashoek committed

Frans Kaashoek's avatar
Frans Kaashoek committed
  assert(nblocks + usedblocks + nlog == size);
kaashoek's avatar
kaashoek committed

Frans Kaashoek's avatar
Frans Kaashoek committed
  for(i = 0; i < nblocks + usedblocks + nlog; i++)
rtm's avatar
rtm committed
    wsect(i, zeroes);

  memset(buf, 0, sizeof(buf));
  memmove(buf, &sb, sizeof(sb));
  wsect(1, buf);
rtm's avatar
rtm committed

rtm's avatar
rtm committed
  rootino = ialloc(T_DIR);
rtm's avatar
rtm committed
  assert(rootino == ROOTINO);
rtm's avatar
rtm committed

  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++){
    assert(index(argv[i], '/') == 0);
rtm's avatar
rtm committed
    if((fd = open(argv[i], 0)) < 0){
      perror(argv[i]);
      exit(1);
    }
rsc's avatar
rsc committed
    
    // 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.
    if(argv[i][0] == '_')
      ++argv[i];
rtm's avatar
rtm committed

    inum = ialloc(T_FILE);

    bzero(&de, sizeof(de));
    de.inum = xshort(inum);
    strncpy(de.name, argv[i], DIRSIZ);
    iappend(rootino, &de, sizeof(de));
rtm's avatar
rtm committed
    while((cc = read(fd, buf, sizeof(buf))) > 0)
      iappend(inum, buf, cc);

    close(fd);
  }
rtm's avatar
rtm committed

  // fix size of root inode dir
  rinode(rootino, &din);
rsc's avatar
rsc committed
  off = xint(din.size);
  off = ((off/BSIZE) + 1) * BSIZE;
  din.size = xint(off);
  winode(rootino, &din);

kaashoek's avatar
kaashoek committed
  balloc(usedblocks);

rtm's avatar
rtm committed
  exit(0);
}

void
wsect(uint sec, void *buf)
{
rtm's avatar
rtm committed
  if(lseek(fsfd, sec * 512L, 0) != sec * 512L){
rtm's avatar
rtm committed
    perror("lseek");
    exit(1);
  }
rtm's avatar
rtm committed
  if(write(fsfd, buf, 512) != 512){
rtm's avatar
rtm committed
    perror("write");
    exit(1);
  }
}

uint
i2b(uint inum)
{
  return (inum / IPB) + 2;
}

void
winode(uint inum, struct dinode *ip)
{
  char buf[512];
  uint bn;
  struct dinode *dip;

  bn = i2b(inum);
  rsect(bn, buf);
Russ Cox's avatar
Russ Cox committed
  dip = ((struct dinode*)buf) + (inum % IPB);
rtm's avatar
rtm committed
  *dip = *ip;
  wsect(bn, buf);
rtm's avatar
rtm committed
}

void
rinode(uint inum, struct dinode *ip)
{
  char buf[512];
  uint bn;
  struct dinode *dip;

  bn = i2b(inum);
  rsect(bn, buf);
Russ Cox's avatar
Russ Cox committed
  dip = ((struct dinode*)buf) + (inum % IPB);
rtm's avatar
rtm committed
  *ip = *dip;
rtm's avatar
rtm committed
}

void
rsect(uint sec, void *buf)
{
rtm's avatar
rtm committed
  if(lseek(fsfd, sec * 512L, 0) != sec * 512L){
rtm's avatar
rtm committed
    perror("lseek");
    exit(1);
  }
rtm's avatar
rtm committed
  if(read(fsfd, buf, 512) != 512){
rtm's avatar
rtm committed
    perror("read");
    exit(1);
  }
}
rtm's avatar
rtm committed

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;
}

kaashoek's avatar
kaashoek committed
void
balloc(int used)
{
  uchar buf[512];
  int i;

  printf("balloc: first %d blocks have been allocated\n", used);
Russ Cox's avatar
Russ Cox committed
  assert(used < 512*8);
kaashoek's avatar
kaashoek committed
  bzero(buf, 512);
Russ Cox's avatar
Russ Cox committed
  for(i = 0; i < used; i++){
kaashoek's avatar
kaashoek committed
    buf[i/8] = buf[i/8] | (0x1 << (i%8));
  }
  printf("balloc: write bitmap block at sector %zu\n", ninodes/IPB + 3);
kaashoek's avatar
kaashoek committed
  wsect(ninodes / IPB + 3, buf);
}

rtm's avatar
rtm committed
#define min(a, b) ((a) < (b) ? (a) : (b))

void
iappend(uint inum, void *xp, int n)
{
Russ Cox's avatar
Russ Cox committed
  char *p = (char*)xp;
rtm's avatar
rtm committed
  uint fbn, off, n1;
  struct dinode din;
  char buf[512];
kaashoek's avatar
kaashoek committed
  uint indirect[NINDIRECT];
  uint x;
rtm's avatar
rtm committed

  rinode(inum, &din);

  off = xint(din.size);
  while(n > 0){
    fbn = off / 512;
kaashoek's avatar
kaashoek committed
    assert(fbn < MAXFILE);
Russ Cox's avatar
Russ Cox committed
    if(fbn < NDIRECT){
      if(xint(din.addrs[fbn]) == 0){
        din.addrs[fbn] = xint(freeblock++);
        usedblocks++;
kaashoek's avatar
kaashoek committed
      }
      x = xint(din.addrs[fbn]);
    } else {
Russ Cox's avatar
Russ Cox committed
      if(xint(din.addrs[NDIRECT]) == 0){
rsc's avatar
rsc committed
        // printf("allocate indirect block\n");
        din.addrs[NDIRECT] = xint(freeblock++);
        usedblocks++;
rsc's avatar
rsc committed
      // printf("read indirect block\n");
Russ Cox's avatar
Russ Cox committed
      rsect(xint(din.addrs[NDIRECT]), (char*)indirect);
      if(indirect[fbn - NDIRECT] == 0){
        indirect[fbn - NDIRECT] = xint(freeblock++);
        usedblocks++;
Russ Cox's avatar
Russ Cox committed
        wsect(xint(din.addrs[NDIRECT]), (char*)indirect);
kaashoek's avatar
kaashoek committed
      }
      x = xint(indirect[fbn-NDIRECT]);
kaashoek's avatar
kaashoek committed
    }
rtm's avatar
rtm committed
    n1 = min(n, (fbn + 1) * 512 - off);
kaashoek's avatar
kaashoek committed
    rsect(x, buf);
rtm's avatar
rtm committed
    bcopy(p, buf + off - (fbn * 512), n1);
kaashoek's avatar
kaashoek committed
    wsect(x, buf);
rtm's avatar
rtm committed
    n -= n1;
    off += n1;
    p += n1;
  }
  din.size = xint(off);
  winode(inum, &din);
}