📄 mkfs.c
字号:
{ ROOTINO, sizeof(struct direct), DT_DIR, 2, ".." }, { 0, DIRBLKSIZ, 0, 0, 0 },};struct odirect olost_found_dir[] = { { LOSTFOUNDINO, sizeof(struct direct), 1, "." }, { ROOTINO, sizeof(struct direct), 2, ".." }, { 0, DIRBLKSIZ, 0, 0 },};#endifchar buf[MAXBSIZE];fsinit(utime) time_t utime;{ int i; /* * initialize the node */ node.di_atime.ts_sec = utime; node.di_mtime.ts_sec = utime; node.di_ctime.ts_sec = utime;#ifdef LOSTDIR /* * create the lost+found directory */ if (Oflag) { (void)makedir((struct direct *)olost_found_dir, 2); for (i = DIRBLKSIZ; i < sblock.fs_bsize; i += DIRBLKSIZ) bcopy(&olost_found_dir[2], &buf[i], DIRSIZ(0, &olost_found_dir[2])); } else { (void)makedir(lost_found_dir, 2); for (i = DIRBLKSIZ; i < sblock.fs_bsize; i += DIRBLKSIZ) bcopy(&lost_found_dir[2], &buf[i], DIRSIZ(0, &lost_found_dir[2])); } node.di_mode = IFDIR | UMASK; node.di_nlink = 2; node.di_size = sblock.fs_bsize; node.di_db[0] = alloc(node.di_size, node.di_mode); node.di_blocks = btodb(fragroundup(&sblock, node.di_size)); wtfs(fsbtodb(&sblock, node.di_db[0]), node.di_size, buf); iput(&node, LOSTFOUNDINO);#endif /* * create the root directory */ if (mfs) node.di_mode = IFDIR | 01777; else node.di_mode = IFDIR | UMASK; node.di_nlink = PREDEFDIR; if (Oflag) node.di_size = makedir((struct direct *)oroot_dir, PREDEFDIR); else node.di_size = makedir(root_dir, PREDEFDIR); node.di_db[0] = alloc(sblock.fs_fsize, node.di_mode); node.di_blocks = btodb(fragroundup(&sblock, node.di_size)); wtfs(fsbtodb(&sblock, node.di_db[0]), sblock.fs_fsize, buf); iput(&node, ROOTINO);}/* * construct a set of directory entries in "buf". * return size of directory. */makedir(protodir, entries) register struct direct *protodir; int entries;{ char *cp; int i, spcleft; spcleft = DIRBLKSIZ; for (cp = buf, i = 0; i < entries - 1; i++) { protodir[i].d_reclen = DIRSIZ(0, &protodir[i]); bcopy(&protodir[i], cp, protodir[i].d_reclen); cp += protodir[i].d_reclen; spcleft -= protodir[i].d_reclen; } protodir[i].d_reclen = spcleft; bcopy(&protodir[i], cp, DIRSIZ(0, &protodir[i])); return (DIRBLKSIZ);}/* * allocate a block or frag */daddr_talloc(size, mode) int size; int mode;{ int i, frag; daddr_t d, blkno; rdfs(fsbtodb(&sblock, cgtod(&sblock, 0)), sblock.fs_cgsize, (char *)&acg); if (acg.cg_magic != CG_MAGIC) { printf("cg 0: bad magic number\n"); return (0); } if (acg.cg_cs.cs_nbfree == 0) { printf("first cylinder group ran out of space\n"); return (0); } for (d = 0; d < acg.cg_ndblk; d += sblock.fs_frag) if (isblock(&sblock, cg_blksfree(&acg), d / sblock.fs_frag)) goto goth; printf("internal error: can't find block in cyl 0\n"); return (0);goth: blkno = fragstoblks(&sblock, d); clrblock(&sblock, cg_blksfree(&acg), blkno); clrbit(cg_clustersfree(&acg), blkno); acg.cg_cs.cs_nbfree--; sblock.fs_cstotal.cs_nbfree--; fscs[0].cs_nbfree--; if (mode & IFDIR) { acg.cg_cs.cs_ndir++; sblock.fs_cstotal.cs_ndir++; fscs[0].cs_ndir++; } cg_blktot(&acg)[cbtocylno(&sblock, d)]--; cg_blks(&sblock, &acg, cbtocylno(&sblock, d))[cbtorpos(&sblock, d)]--; if (size != sblock.fs_bsize) { frag = howmany(size, sblock.fs_fsize); fscs[0].cs_nffree += sblock.fs_frag - frag; sblock.fs_cstotal.cs_nffree += sblock.fs_frag - frag; acg.cg_cs.cs_nffree += sblock.fs_frag - frag; acg.cg_frsum[sblock.fs_frag - frag]++; for (i = frag; i < sblock.fs_frag; i++) setbit(cg_blksfree(&acg), d + i); } wtfs(fsbtodb(&sblock, cgtod(&sblock, 0)), sblock.fs_cgsize, (char *)&acg); return (d);}/* * Allocate an inode on the disk */iput(ip, ino) register struct dinode *ip; register ino_t ino;{ struct dinode buf[MAXINOPB]; daddr_t d; int c; c = ino_to_cg(&sblock, ino); rdfs(fsbtodb(&sblock, cgtod(&sblock, 0)), sblock.fs_cgsize, (char *)&acg); if (acg.cg_magic != CG_MAGIC) { printf("cg 0: bad magic number\n"); exit(31); } acg.cg_cs.cs_nifree--; setbit(cg_inosused(&acg), ino); wtfs(fsbtodb(&sblock, cgtod(&sblock, 0)), sblock.fs_cgsize, (char *)&acg); sblock.fs_cstotal.cs_nifree--; fscs[0].cs_nifree--; if (ino >= sblock.fs_ipg * sblock.fs_ncg) { printf("fsinit: inode value out of range (%d).\n", ino); exit(32); } d = fsbtodb(&sblock, ino_to_fsba(&sblock, ino)); rdfs(d, sblock.fs_bsize, buf); buf[ino_to_fsbo(&sblock, ino)] = *ip; wtfs(d, sblock.fs_bsize, buf);}/* * Notify parent process that the filesystem has created itself successfully. */voidstarted(){ exit(0);}/* * Replace libc function with one suited to our needs. */caddr_tmalloc(size) register u_long size;{ char *base, *i; static u_long pgsz; struct rlimit rlp; if (pgsz == 0) { base = sbrk(0); pgsz = getpagesize() - 1; i = (char *)((u_long)(base + pgsz) &~ pgsz); base = sbrk(i - base); if (getrlimit(RLIMIT_DATA, &rlp) < 0) perror("getrlimit"); rlp.rlim_cur = rlp.rlim_max; if (setrlimit(RLIMIT_DATA, &rlp) < 0) perror("setrlimit"); memleft = rlp.rlim_max - (u_long)base; } size = (size + pgsz) &~ pgsz; if (size > memleft) size = memleft; memleft -= size; if (size == 0) return (0); return ((caddr_t)sbrk(size));}/* * Replace libc function with one suited to our needs. */caddr_trealloc(ptr, size) char *ptr; u_long size;{ void *p; if ((p = malloc(size)) == NULL) return (NULL); bcopy(ptr, p, size); free(ptr); return (p);}/* * Replace libc function with one suited to our needs. */char *calloc(size, numelm) u_long size, numelm;{ caddr_t base; size *= numelm; base = malloc(size); bzero(base, size); return (base);}/* * Replace libc function with one suited to our needs. */free(ptr) char *ptr;{ /* do not worry about it for now */}/* * read a block from the file system */rdfs(bno, size, bf) daddr_t bno; int size; char *bf;{ int n; if (mfs) { bcopy(membase + bno * sectorsize, bf, size); return; } if (lseek(fsi, (off_t)bno * sectorsize, 0) < 0) { printf("seek error: %ld\n", bno); perror("rdfs"); exit(33); } n = read(fsi, bf, size); if (n != size) { printf("read error: %ld\n", bno); perror("rdfs"); exit(34); }}/* * write a block to the file system */wtfs(bno, size, bf) daddr_t bno; int size; char *bf;{ int n; if (mfs) { bcopy(bf, membase + bno * sectorsize, size); return; } if (Nflag) return; if (lseek(fso, (off_t)bno * sectorsize, SEEK_SET) < 0) { printf("seek error: %ld\n", bno); perror("wtfs"); exit(35); } n = write(fso, bf, size); if (n != size) { printf("write error: %ld\n", bno); perror("wtfs"); exit(36); }}/* * check if a block is available */isblock(fs, cp, h) struct fs *fs; unsigned char *cp; int h;{ unsigned char mask; switch (fs->fs_frag) { case 8: return (cp[h] == 0xff); case 4: mask = 0x0f << ((h & 0x1) << 2); return ((cp[h >> 1] & mask) == mask); case 2: mask = 0x03 << ((h & 0x3) << 1); return ((cp[h >> 2] & mask) == mask); case 1: mask = 0x01 << (h & 0x7); return ((cp[h >> 3] & mask) == mask); default:#ifdef STANDALONE printf("isblock bad fs_frag %d\n", fs->fs_frag);#else fprintf(stderr, "isblock bad fs_frag %d\n", fs->fs_frag);#endif return (0); }}/* * take a block out of the map */clrblock(fs, cp, h) struct fs *fs; unsigned char *cp; int h;{ switch ((fs)->fs_frag) { case 8: cp[h] = 0; return; case 4: cp[h >> 1] &= ~(0x0f << ((h & 0x1) << 2)); return; case 2: cp[h >> 2] &= ~(0x03 << ((h & 0x3) << 1)); return; case 1: cp[h >> 3] &= ~(0x01 << (h & 0x7)); return; default:#ifdef STANDALONE printf("clrblock bad fs_frag %d\n", fs->fs_frag);#else fprintf(stderr, "clrblock bad fs_frag %d\n", fs->fs_frag);#endif return; }}/* * put a block into the map */setblock(fs, cp, h) struct fs *fs; unsigned char *cp; int h;{ switch (fs->fs_frag) { case 8: cp[h] = 0xff; return; case 4: cp[h >> 1] |= (0x0f << ((h & 0x1) << 2)); return; case 2: cp[h >> 2] |= (0x03 << ((h & 0x3) << 1)); return; case 1: cp[h >> 3] |= (0x01 << (h & 0x7)); return; default:#ifdef STANDALONE printf("setblock bad fs_frag %d\n", fs->fs_frag);#else fprintf(stderr, "setblock bad fs_frag %d\n", fs->fs_frag);#endif return; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -