root/mkfs/mkfs.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. xshort
  2. xint
  3. main
  4. wsect
  5. winode
  6. rinode
  7. rsect
  8. ialloc
  9. balloc
  10. iappend
  11. die

   1 #include <stdio.h>
   2 #include <unistd.h>
   3 #include <stdlib.h>
   4 #include <string.h>
   5 #include <fcntl.h>
   6 #include <assert.h>
   7 
   8 #define stat xv6_stat  // avoid clash with host struct stat
   9 #include "kernel/types.h"
  10 #include "kernel/fs.h"
  11 #include "kernel/stat.h"
  12 #include "kernel/param.h"
  13 
  14 #ifndef static_assert
  15 #define static_assert(a, b) do { switch (0) case 0: case (a): ; } while (0)
  16 #endif
  17 
  18 #define NINODES 200
  19 
  20 // Disk layout:
  21 // [ boot block | sb block | log | inode blocks | free bit map | data blocks ]
  22 
  23 int nbitmap = FSSIZE/(BSIZE*8) + 1;
  24 int ninodeblocks = NINODES / IPB + 1;
  25 int nlog = LOGSIZE;
  26 int nmeta;    // Number of meta blocks (boot, sb, nlog, inode, bitmap)
  27 int nblocks;  // Number of data blocks
  28 
  29 int fsfd;
  30 struct superblock sb;
  31 char zeroes[BSIZE];
  32 uint freeinode = 1;
  33 uint freeblock;
  34 
  35 
  36 void balloc(int);
  37 void wsect(uint, void*);
  38 void winode(uint, struct dinode*);
  39 void rinode(uint inum, struct dinode *ip);
  40 void rsect(uint sec, void *buf);
  41 uint ialloc(ushort type);
  42 void iappend(uint inum, void *p, int n);
  43 void die(const char *);
  44 
  45 // convert to riscv byte order
  46 ushort
  47 xshort(ushort x)
  48 {
  49   ushort y;
  50   uchar *a = (uchar*)&y;
  51   a[0] = x;
  52   a[1] = x >> 8;
  53   return y;
  54 }
  55 
  56 uint
  57 xint(uint x)
  58 {
  59   uint y;
  60   uchar *a = (uchar*)&y;
  61   a[0] = x;
  62   a[1] = x >> 8;
  63   a[2] = x >> 16;
  64   a[3] = x >> 24;
  65   return y;
  66 }
  67 
  68 int
  69 main(int argc, char *argv[])
  70 {
  71   int i, cc, fd;
  72   uint rootino, inum, off;
  73   struct dirent de;
  74   char buf[BSIZE];
  75   struct dinode din;
  76 
  77 
  78   static_assert(sizeof(int) == 4, "Integers must be 4 bytes!");
  79 
  80   if(argc < 2){
  81     fprintf(stderr, "Usage: mkfs fs.img files...\n");
  82     exit(1);
  83   }
  84 
  85   assert((BSIZE % sizeof(struct dinode)) == 0);
  86   assert((BSIZE % sizeof(struct dirent)) == 0);
  87 
  88   fsfd = open(argv[1], O_RDWR|O_CREAT|O_TRUNC, 0666);
  89   if(fsfd < 0)
  90     die(argv[1]);
  91 
  92   // 1 fs block = 1 disk sector
  93   nmeta = 2 + nlog + ninodeblocks + nbitmap;
  94   nblocks = FSSIZE - nmeta;
  95 
  96   sb.magic = FSMAGIC;
  97   sb.size = xint(FSSIZE);
  98   sb.nblocks = xint(nblocks);
  99   sb.ninodes = xint(NINODES);
 100   sb.nlog = xint(nlog);
 101   sb.logstart = xint(2);
 102   sb.inodestart = xint(2+nlog);
 103   sb.bmapstart = xint(2+nlog+ninodeblocks);
 104 
 105   printf("nmeta %d (boot, super, log blocks %u inode blocks %u, bitmap blocks %u) blocks %d total %d\n",
 106          nmeta, nlog, ninodeblocks, nbitmap, nblocks, FSSIZE);
 107 
 108   freeblock = nmeta;     // the first free block that we can allocate
 109 
 110   for(i = 0; i < FSSIZE; i++)
 111     wsect(i, zeroes);
 112 
 113   memset(buf, 0, sizeof(buf));
 114   memmove(buf, &sb, sizeof(sb));
 115   wsect(1, buf);
 116 
 117   rootino = ialloc(T_DIR);
 118   assert(rootino == ROOTINO);
 119 
 120   bzero(&de, sizeof(de));
 121   de.inum = xshort(rootino);
 122   strcpy(de.name, ".");
 123   iappend(rootino, &de, sizeof(de));
 124 
 125   bzero(&de, sizeof(de));
 126   de.inum = xshort(rootino);
 127   strcpy(de.name, "..");
 128   iappend(rootino, &de, sizeof(de));
 129 
 130   for(i = 2; i < argc; i++){
 131     // get rid of "user/"
 132     char *shortname;
 133     if(strncmp(argv[i], "user/", 5) == 0)
 134       shortname = argv[i] + 5;
 135     else
 136       shortname = argv[i];
 137     
 138     assert(index(shortname, '/') == 0);
 139 
 140     if((fd = open(argv[i], 0)) < 0)
 141       die(argv[i]);
 142 
 143     // Skip leading _ in name when writing to file system.
 144     // The binaries are named _rm, _cat, etc. to keep the
 145     // build operating system from trying to execute them
 146     // in place of system binaries like rm and cat.
 147     if(shortname[0] == '_')
 148       shortname += 1;
 149 
 150     inum = ialloc(T_FILE);
 151 
 152     bzero(&de, sizeof(de));
 153     de.inum = xshort(inum);
 154     strncpy(de.name, shortname, DIRSIZ);
 155     iappend(rootino, &de, sizeof(de));
 156 
 157     while((cc = read(fd, buf, sizeof(buf))) > 0)
 158       iappend(inum, buf, cc);
 159 
 160     close(fd);
 161   }
 162 
 163   // fix size of root inode dir
 164   rinode(rootino, &din);
 165   off = xint(din.size);
 166   off = ((off/BSIZE) + 1) * BSIZE;
 167   din.size = xint(off);
 168   winode(rootino, &din);
 169 
 170   balloc(freeblock);
 171 
 172   exit(0);
 173 }
 174 
 175 void
 176 wsect(uint sec, void *buf)
 177 {
 178   if(lseek(fsfd, sec * BSIZE, 0) != sec * BSIZE)
 179     die("lseek");
 180   if(write(fsfd, buf, BSIZE) != BSIZE)
 181     die("write");
 182 }
 183 
 184 void
 185 winode(uint inum, struct dinode *ip)
 186 {
 187   char buf[BSIZE];
 188   uint bn;
 189   struct dinode *dip;
 190 
 191   bn = IBLOCK(inum, sb);
 192   rsect(bn, buf);
 193   dip = ((struct dinode*)buf) + (inum % IPB);
 194   *dip = *ip;
 195   wsect(bn, buf);
 196 }
 197 
 198 void
 199 rinode(uint inum, struct dinode *ip)
 200 {
 201   char buf[BSIZE];
 202   uint bn;
 203   struct dinode *dip;
 204 
 205   bn = IBLOCK(inum, sb);
 206   rsect(bn, buf);
 207   dip = ((struct dinode*)buf) + (inum % IPB);
 208   *ip = *dip;
 209 }
 210 
 211 void
 212 rsect(uint sec, void *buf)
 213 {
 214   if(lseek(fsfd, sec * BSIZE, 0) != sec * BSIZE)
 215     die("lseek");
 216   if(read(fsfd, buf, BSIZE) != BSIZE)
 217     die("read");
 218 }
 219 
 220 uint
 221 ialloc(ushort type)
 222 {
 223   uint inum = freeinode++;
 224   struct dinode din;
 225 
 226   bzero(&din, sizeof(din));
 227   din.type = xshort(type);
 228   din.nlink = xshort(1);
 229   din.size = xint(0);
 230   winode(inum, &din);
 231   return inum;
 232 }
 233 
 234 void
 235 balloc(int used)
 236 {
 237   uchar buf[BSIZE];
 238   int i;
 239 
 240   printf("balloc: first %d blocks have been allocated\n", used);
 241   assert(used < BSIZE*8);
 242   bzero(buf, BSIZE);
 243   for(i = 0; i < used; i++){
 244     buf[i/8] = buf[i/8] | (0x1 << (i%8));
 245   }
 246   printf("balloc: write bitmap block at sector %d\n", sb.bmapstart);
 247   wsect(sb.bmapstart, buf);
 248 }
 249 
 250 #define min(a, b) ((a) < (b) ? (a) : (b))
 251 
 252 void
 253 iappend(uint inum, void *xp, int n)
 254 {
 255   char *p = (char*)xp;
 256   uint fbn, off, n1;
 257   struct dinode din;
 258   char buf[BSIZE];
 259   uint indirect[NINDIRECT];
 260   uint x;
 261 
 262   rinode(inum, &din);
 263   off = xint(din.size);
 264   // printf("append inum %d at off %d sz %d\n", inum, off, n);
 265   while(n > 0){
 266     fbn = off / BSIZE;
 267     assert(fbn < MAXFILE);
 268     if(fbn < NDIRECT){
 269       if(xint(din.addrs[fbn]) == 0){
 270         din.addrs[fbn] = xint(freeblock++);
 271       }
 272       x = xint(din.addrs[fbn]);
 273     } else {
 274       if(xint(din.addrs[NDIRECT]) == 0){
 275         din.addrs[NDIRECT] = xint(freeblock++);
 276       }
 277       rsect(xint(din.addrs[NDIRECT]), (char*)indirect);
 278       if(indirect[fbn - NDIRECT] == 0){
 279         indirect[fbn - NDIRECT] = xint(freeblock++);
 280         wsect(xint(din.addrs[NDIRECT]), (char*)indirect);
 281       }
 282       x = xint(indirect[fbn-NDIRECT]);
 283     }
 284     n1 = min(n, (fbn + 1) * BSIZE - off);
 285     rsect(x, buf);
 286     bcopy(p, buf + off - (fbn * BSIZE), n1);
 287     wsect(x, buf);
 288     n -= n1;
 289     off += n1;
 290     p += n1;
 291   }
 292   din.size = xint(off);
 293   winode(inum, &din);
 294 }
 295 
 296 void
 297 die(const char *s)
 298 {
 299   perror(s);
 300   exit(1);
 301 }

/* [<][>][^][v][top][bottom][index][help] */