📄 mkfs_minix.c
字号:
for (j = 0; j < 512; j++) { ind_block[j] = zone; if (!NEXT_BAD) goto end_bad; } } error_msg_and_die("too many bad blocks"); end_bad: if (ind) write_block(ind, (char *) ind_block); if (dind) write_block(dind, (char *) dind_block);}#ifdef BB_FEATURE_MINIX2static void make_bad_inode2(void){ struct minix2_inode *inode = &Inode2[MINIX_BAD_INO]; int i, j, zone; int ind = 0, dind = 0; unsigned long ind_block[BLOCK_SIZE >> 2]; unsigned long dind_block[BLOCK_SIZE >> 2]; if (!badblocks) return; mark_inode(MINIX_BAD_INO); inode->i_nlinks = 1; inode->i_atime = inode->i_mtime = inode->i_ctime = time(NULL); inode->i_mode = S_IFREG + 0000; inode->i_size = badblocks * BLOCK_SIZE; zone = next(0); for (i = 0; i < 7; i++) { inode->i_zone[i] = zone; if (!NEXT_BAD) goto end_bad; } inode->i_zone[7] = ind = get_free_block(); memset(ind_block, 0, BLOCK_SIZE); for (i = 0; i < 256; i++) { ind_block[i] = zone; if (!NEXT_BAD) goto end_bad; } inode->i_zone[8] = dind = get_free_block(); memset(dind_block, 0, BLOCK_SIZE); for (i = 0; i < 256; i++) { write_block(ind, (char *) ind_block); dind_block[i] = ind = get_free_block(); memset(ind_block, 0, BLOCK_SIZE); for (j = 0; j < 256; j++) { ind_block[j] = zone; if (!NEXT_BAD) goto end_bad; } } /* Could make triple indirect block here */ error_msg_and_die("too many bad blocks"); end_bad: if (ind) write_block(ind, (char *) ind_block); if (dind) write_block(dind, (char *) dind_block);}#endifstatic void make_root_inode(void){ struct minix_inode *inode = &Inode[MINIX_ROOT_INO]; mark_inode(MINIX_ROOT_INO); inode->i_zone[0] = get_free_block(); inode->i_nlinks = 2; inode->i_time = time(NULL); if (badblocks) inode->i_size = 3 * dirsize; else { root_block[2 * dirsize] = '\0'; root_block[2 * dirsize + 1] = '\0'; inode->i_size = 2 * dirsize; } inode->i_mode = S_IFDIR + 0755; inode->i_uid = getuid(); if (inode->i_uid) inode->i_gid = getgid(); write_block(inode->i_zone[0], root_block);}#ifdef BB_FEATURE_MINIX2static void make_root_inode2(void){ struct minix2_inode *inode = &Inode2[MINIX_ROOT_INO]; mark_inode(MINIX_ROOT_INO); inode->i_zone[0] = get_free_block(); inode->i_nlinks = 2; inode->i_atime = inode->i_mtime = inode->i_ctime = time(NULL); if (badblocks) inode->i_size = 3 * dirsize; else { root_block[2 * dirsize] = '\0'; root_block[2 * dirsize + 1] = '\0'; inode->i_size = 2 * dirsize; } inode->i_mode = S_IFDIR + 0755; inode->i_uid = getuid(); if (inode->i_uid) inode->i_gid = getgid(); write_block(inode->i_zone[0], root_block);}#endifstatic void setup_tables(void){ int i; unsigned long inodes; memset(super_block_buffer, 0, BLOCK_SIZE); memset(boot_block_buffer, 0, 512); MAGIC = magic; ZONESIZE = 0; MAXSIZE = version2 ? 0x7fffffff : (7 + 512 + 512 * 512) * 1024; ZONES = BLOCKS;/* some magic nrs: 1 inode / 3 blocks */ if (req_nr_inodes == 0) inodes = BLOCKS / 3; else inodes = req_nr_inodes; /* Round up inode count to fill block size */#ifdef BB_FEATURE_MINIX2 if (version2) inodes = ((inodes + MINIX2_INODES_PER_BLOCK - 1) & ~(MINIX2_INODES_PER_BLOCK - 1)); else#endif inodes = ((inodes + MINIX_INODES_PER_BLOCK - 1) & ~(MINIX_INODES_PER_BLOCK - 1)); if (inodes > 65535) inodes = 65535; INODES = inodes; IMAPS = UPPER(INODES + 1, BITS_PER_BLOCK); ZMAPS = 0; i = 0; while (ZMAPS != UPPER(BLOCKS - (2 + IMAPS + ZMAPS + INODE_BLOCKS) + 1, BITS_PER_BLOCK) && i < 1000) { ZMAPS = UPPER(BLOCKS - (2 + IMAPS + ZMAPS + INODE_BLOCKS) + 1, BITS_PER_BLOCK); i++; } /* Real bad hack but overwise mkfs.minix can be thrown * in infinite loop... * try: * dd if=/dev/zero of=test.fs count=10 bs=1024 * /sbin/mkfs.minix -i 200 test.fs * */ if (i >= 999) { error_msg_and_die("unable to allocate buffers for maps"); } FIRSTZONE = NORM_FIRSTZONE; inode_map = xmalloc(IMAPS * BLOCK_SIZE); zone_map = xmalloc(ZMAPS * BLOCK_SIZE); memset(inode_map, 0xff, IMAPS * BLOCK_SIZE); memset(zone_map, 0xff, ZMAPS * BLOCK_SIZE); for (i = FIRSTZONE; i < ZONES; i++) unmark_zone(i); for (i = MINIX_ROOT_INO; i <= INODES; i++) unmark_inode(i); inode_buffer = xmalloc(INODE_BUFFER_SIZE); memset(inode_buffer, 0, INODE_BUFFER_SIZE); printf("%ld inodes\n", INODES); printf("%ld blocks\n", ZONES); printf("Firstdatazone=%ld (%ld)\n", FIRSTZONE, NORM_FIRSTZONE); printf("Zonesize=%d\n", BLOCK_SIZE << ZONESIZE); printf("Maxsize=%ld\n\n", MAXSIZE);}/* * Perform a test of a block; return the number of * blocks readable/writeable. */static long do_check(char *buffer, int try, unsigned int current_block){ long got; /* Seek to the correct loc. */ if (lseek(DEV, current_block * BLOCK_SIZE, SEEK_SET) != current_block * BLOCK_SIZE) { error_msg_and_die("seek failed during testing of blocks"); } /* Try the read */ got = read(DEV, buffer, try * BLOCK_SIZE); if (got < 0) got = 0; if (got & (BLOCK_SIZE - 1)) { printf("Weird values in do_check: probably bugs\n"); } got /= BLOCK_SIZE; return got;}static unsigned int currently_testing = 0;static void alarm_intr(int alnum){ if (currently_testing >= ZONES) return; signal(SIGALRM, alarm_intr); alarm(5); if (!currently_testing) return; printf("%d ...", currently_testing); fflush(stdout);}static void check_blocks(void){ int try, got; static char buffer[BLOCK_SIZE * TEST_BUFFER_BLOCKS]; currently_testing = 0; signal(SIGALRM, alarm_intr); alarm(5); while (currently_testing < ZONES) { if (lseek(DEV, currently_testing * BLOCK_SIZE, SEEK_SET) != currently_testing * BLOCK_SIZE) error_msg_and_die("seek failed in check_blocks"); try = TEST_BUFFER_BLOCKS; if (currently_testing + try > ZONES) try = ZONES - currently_testing; got = do_check(buffer, try, currently_testing); currently_testing += got; if (got == try) continue; if (currently_testing < FIRSTZONE) error_msg_and_die("bad blocks before data-area: cannot make fs"); mark_zone(currently_testing); badblocks++; currently_testing++; } if (badblocks > 1) printf("%d bad blocks\n", badblocks); else if (badblocks == 1) printf("one bad block\n");}static void get_list_blocks(filename)char *filename;{ FILE *listfile; unsigned long blockno; listfile = xfopen(filename, "r"); while (!feof(listfile)) { fscanf(listfile, "%ld\n", &blockno); mark_zone(blockno); badblocks++; } if (badblocks > 1) printf("%d bad blocks\n", badblocks); else if (badblocks == 1) printf("one bad block\n");}extern int mkfs_minix_main(int argc, char **argv){ int i=1; char *tmp; struct stat statbuf; char *listfile = NULL; int stopIt=FALSE; if (INODE_SIZE * MINIX_INODES_PER_BLOCK != BLOCK_SIZE) error_msg_and_die("bad inode size");#ifdef BB_FEATURE_MINIX2 if (INODE_SIZE2 * MINIX2_INODES_PER_BLOCK != BLOCK_SIZE) error_msg_and_die("bad inode size");#endif /* Parse options */ argv++; while (--argc >= 0 && *argv && **argv) { if (**argv == '-') { stopIt=FALSE; while (i > 0 && *++(*argv) && stopIt==FALSE) { switch (**argv) { case 'c': check = 1; break; case 'i': { char *cp=NULL; if (*(*argv+1) != 0) { cp = ++(*argv); } else { if (--argc == 0) { goto goodbye; } cp = *(++argv); } req_nr_inodes = strtoul(cp, &tmp, 0); if (*tmp) show_usage(); stopIt=TRUE; break; } case 'l': if (--argc == 0) { goto goodbye; } listfile = *(++argv); break; case 'n': { char *cp=NULL; if (*(*argv+1) != 0) { cp = ++(*argv); } else { if (--argc == 0) { goto goodbye; } cp = *(++argv); } i = strtoul(cp, &tmp, 0); if (*tmp) show_usage(); if (i == 14) magic = MINIX_SUPER_MAGIC; else if (i == 30) magic = MINIX_SUPER_MAGIC2; else show_usage(); namelen = i; dirsize = i + 2; stopIt=TRUE; break; } case 'v':#ifdef BB_FEATURE_MINIX2 version2 = 1;#else error_msg("%s: not compiled with minix v2 support", device_name); exit(-1);#endif break; case '-': case 'h': default:goodbye: show_usage(); } } } else { if (device_name == NULL) device_name = *argv; else if (BLOCKS == 0) BLOCKS = strtol(*argv, &tmp, 0); else { goto goodbye; } } argv++; } if (device_name && !BLOCKS) BLOCKS = get_size(device_name) / 1024; if (!device_name || BLOCKS < 10) { show_usage(); }#ifdef BB_FEATURE_MINIX2 if (version2) { if (namelen == 14) magic = MINIX2_SUPER_MAGIC; else magic = MINIX2_SUPER_MAGIC2; } else#endif if (BLOCKS > 65535) BLOCKS = 65535; check_mount(); /* is it already mounted? */ tmp = root_block; *(short *) tmp = 1; strcpy(tmp + 2, "."); tmp += dirsize; *(short *) tmp = 1; strcpy(tmp + 2, ".."); tmp += dirsize; *(short *) tmp = 2; strcpy(tmp + 2, ".badblocks"); DEV = open(device_name, O_RDWR); if (DEV < 0) error_msg_and_die("unable to open %s", device_name); if (fstat(DEV, &statbuf) < 0) error_msg_and_die("unable to stat %s", device_name); if (!S_ISBLK(statbuf.st_mode)) check = 0; else if (statbuf.st_rdev == 0x0300 || statbuf.st_rdev == 0x0340) error_msg_and_die("will not try to make filesystem on '%s'", device_name); setup_tables(); if (check) check_blocks(); else if (listfile) get_list_blocks(listfile);#ifdef BB_FEATURE_MINIX2 if (version2) { make_root_inode2(); make_bad_inode2(); } else#endif { make_root_inode(); make_bad_inode(); } mark_good_blocks(); write_tables(); return( 0);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -