Annotation of researchv8dc/cmd/mkbitfs.c, revision 1.1.1.1

1.1       root        1: #include "stdio.h"
                      2: #include "sys/param.h"
                      3: #include "sys/stat.h"
                      4: #include "sys/filsys.h"
                      5: #include "sys/dir.h"
                      6: #include "sys/ino.h"
                      7: #include "sys/inode.h"
                      8: 
                      9: #define ICOUNT BUFSIZE/sizeof(struct dinode)
                     10: struct filsys sb;
                     11: struct dinode ib[ICOUNT];
                     12: struct direct db[BUFSIZE/sizeof(struct direct)];
                     13: struct stat statbuf;
                     14: char buf[BUFSIZE];
                     15: long where[NADDR];
                     16: 
                     17: main(argc, argv)
                     18: char **argv;
                     19: {
                     20:        register int i, j;
                     21:        daddr_t size, isize;
                     22:        register daddr_t bno, bfree;
                     23:        int fd;
                     24:        long atol();
                     25:        off_t lseek();
                     26: 
                     27:        if(argc != 3) {
                     28:                fprintf(stderr, "%s: bit-dev no-of-4k-blocks\n", argv[0]);
                     29:                exit(1);
                     30:        }
                     31:        fd = open(argv[1], 2);
                     32:        if(fd < 0) {
                     33:                perror(argv[1]);
                     34:                exit(1);
                     35:        }
                     36:        if(fstat(fd, &statbuf) < 0) {
                     37:                perror(argv[1]);
                     38:                exit(1);
                     39:        }
                     40:        if(!BITFS(statbuf.st_rdev)) {
                     41:                fprintf(stderr, "%s device %d, 0%o can't have a 4k system\n",
                     42:                        argv[1], major(statbuf.st_rdev), minor(statbuf.st_rdev));
                     43:                exit(1);
                     44:        }
                     45:        size = atoi(argv[2]);
                     46:        if(size <= 3) {
                     47:                fprintf(stderr, "size %ld too small\n", size);
                     48:                exit(1);
                     49:        }
                     50:        if((i = lseek(fd, (size -1) * BUFSIZE, 0)) < 0 ||
                     51:                (j = read(fd, buf, BUFSIZE)) != BUFSIZE) {
                     52:                fprintf(stderr, "size %ld too large (lseek %d, read %d)\n", size,
                     53:                        i, j);
                     54:                exit(1);
                     55:        }
                     56:        isize = (size - 2)/(1 + ICOUNT);        /* DOUBTFUL */
                     57:        if(isize * ICOUNT > 65536)
                     58:                isize = 65536/ICOUNT;   /* 65535 is largest short */
                     59:        fprintf(stderr, "%ld 4k blocks, %ld blocks of inodes, %d inodes\n",
                     60:                size, isize - 2, ICOUNT * (isize - 2));
                     61:        for(bno = 2; bno < isize; bno++) {      /* zero out all inodes */
                     62:                lseek(fd, (off_t)(bno * BUFSIZE), 0);
                     63:                if (write(fd, (char *)ib, BUFSIZE) != BUFSIZE) {
                     64:                        perror("inode write");
                     65:                        exit(1);
                     66:                }
                     67:        }
                     68:        where[0] = isize;
                     69:        /* next block has the root directory */
                     70:        db[0].d_ino = ROOTINO;
                     71:        db[0].d_name[0] = '.';
                     72:        db[1].d_ino = ROOTINO;
                     73:        db[1].d_name[1] = db[1].d_name[0] = '.';
                     74:        if (write(fd, (char *)db, BUFSIZE) != BUFSIZE) {
                     75:                perror("root dir write");
                     76:                exit(1);
                     77:        }
                     78:        /* now for its inode */
                     79:        ib[ROOTINO-1].di_mode = IFDIR | IREAD | IWRITE | IEXEC;
                     80:        ib[ROOTINO-1].di_mode |= (IREAD | IEXEC | IWRITE) >> 3;
                     81:        ib[ROOTINO-1].di_mode |= (IREAD | IEXEC | IWRITE) >> 6;
                     82:        ib[ROOTINO-1].di_nlink = 2;
                     83:        ib[ROOTINO-1].di_uid = ib[ROOTINO-1].di_gid = 0;
                     84:        ib[ROOTINO-1].di_size = 2*sizeof(struct direct);
                     85:        ltol3(ib[ROOTINO-1].di_addr, where, 1);
                     86:        ib[ROOTINO-1].di_atime = ib[ROOTINO-1].di_mtime =
                     87:                ib[ROOTINO-1].di_ctime = time(0);
                     88:        lseek(fd, (off_t)(2*BUFSIZE), 0);
                     89:        if (write(fd, (char *)ib, BUFSIZE) != BUFSIZE) {
                     90:                perror("root inode write");
                     91:                exit(1);
                     92:        }
                     93:        /* and now the super block */
                     94:        sb.s_isize = isize;
                     95:        sb.s_fsize = size;
                     96:        sb.s_time = ib[ROOTINO-1].di_atime;
                     97:        sb.s_tfree = size - isize - 1;
                     98:        sb.s_tinode = (isize - 2) * ICOUNT - 1;
                     99:        sb.s_valid = 1;
                    100:        for(i = 0; i < BITMAP; i++)
                    101:                sb.s_bfree[i] = 0;
                    102:        for(bno = isize + 1; bno < size; bno++) {
                    103:                bfree = bno - isize;
                    104:                sb.s_bfree[bfree>>5] |= (1 << (bfree & 31));
                    105:        }
                    106:        if((++bfree >> 5) >= BITMAP) {
                    107:                fprintf(stderr, "free map won't fit, blk = %ld, wd = %ld\n",
                    108:                        bno, bfree);
                    109:                exit(1);
                    110:        }
                    111:        lseek(fd, (off_t)BUFSIZE, 0);
                    112:        if (write(fd, (char *)&sb, BUFSIZE) != BUFSIZE) {
                    113:                perror("superblock write");
                    114:                exit(1);
                    115:        }
                    116:        exit(0);
                    117: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.