minix檔案系統是怎麼格式化的?

先上個程式碼:

int main(int argc, char ** argv){ struct fs_control ctl = { 。fs_namelen = 30, /* keep in sync with DEFAULT_FS_VERSION */ 0 }; int i; struct stat statbuf; char * listfile = NULL; enum { OPT_LOCK = CHAR_MAX + 1 }; static const struct option longopts[] = { {“namelength”, required_argument, NULL, ‘n’}, {“inodes”, required_argument, NULL, ‘i’}, {“check”, no_argument, NULL, ‘c’}, {“badblocks”, required_argument, NULL, ‘l’}, {“version”, no_argument, NULL, ‘V’}, {“help”, no_argument, NULL, ‘h’}, {“lock”,optional_argument, NULL, OPT_LOCK}, {NULL, 0, NULL, 0} }; setlocale(LC_ALL, “”); bindtextdomain(PACKAGE, LOCALEDIR); textdomain(PACKAGE); close_stdout_atexit(); strutils_set_exitcode(MKFS_EX_USAGE); while ((i = getopt_long(argc, argv, “1v23n:i:cl:Vh”, longopts, NULL)) != -1) switch (i) { case ‘1’: fs_version = 1; break; case ‘v’: /* kept for backwards compatibility */ warnx(_(“-v is ambiguous, use ‘-2’ instead”)); /* fallthrough */ case ‘2’: fs_version = 2; break; case ‘3’: fs_version = 3; ctl。fs_namelen = 60; break; case ‘n’: ctl。fs_namelen = strtou16_or_err(optarg, _(“failed to parse maximum length of filenames”)); break; case ‘i’: ctl。fs_inodes = strtoul_or_err(optarg, _(“failed to parse number of inodes”)); break; case ‘c’: ctl。check_blocks = 1; break; case ‘l’: listfile = optarg; break; case OPT_LOCK: ctl。lockmode = “1”; if (optarg) { if (*optarg == ‘=’) optarg++; ctl。lockmode = optarg; } break; case ‘V’: print_version(MKFS_EX_OK); case ‘h’: usage(); default: errtryhelp(MKFS_EX_USAGE); } argc -= optind; argv += optind; if (argc > 0) { ctl。device_name = argv[0]; argc——; argv++; } if (argc > 0) ctl。fs_blocks = strtoul_or_err(argv[0], _(“failed to parse number of blocks”)); if (!ctl。device_name) { warnx(_(“no device specified”)); errtryhelp(MKFS_EX_USAGE); } check_user_instructions(&ctl); if (is_mounted(ctl。device_name)) errx(MKFS_EX_ERROR, _(“%s is mounted; will not make a filesystem here!”), ctl。device_name); if (stat(ctl。device_name, &statbuf) < 0) err(MKFS_EX_ERROR, _(“stat of %s failed”), ctl。device_name); ctl。device_fd = open_blkdev_or_file(&statbuf, ctl。device_name, O_RDWR); if (ctl。device_fd < 0) err(MKFS_EX_ERROR, _(“cannot open %s”), ctl。device_name); if (blkdev_lock(ctl。device_fd, ctl。device_name, ctl。lockmode) != 0) exit(MKFS_EX_ERROR); determine_device_blocks(&ctl, &statbuf); setup_tables(&ctl); if (ctl。check_blocks) check_blocks(&ctl); else if (listfile) get_list_blocks(&ctl, listfile); make_root_inode(&ctl); make_bad_inode(&ctl); mark_good_blocks(&ctl); write_tables(&ctl); if (close_fd(ctl。device_fd) != 0) err(MKFS_EX_ERROR, _(“write failed”)); return MKFS_EX_OK;}

1。setlocale(): 設定或讀取地域化資訊

2。bindtextdomain() 用來設定文字域目錄。

3。getopt_long迴圈部分。

獲取主函式傳參的功能。 根據引數部署相應的功能。

目前主要看格式化分割槽功能。

4。ctl。fs_blocks = strtoul_or_err(argv[0], _(“failed to parse number of blocks”));

分開引數中的字元部分, 和數字部分 , 並返回數字部分給ctl。fs_blocks。

沒有傳參的話,就返回0。

5。check_user_instructions(&ctl);

static void check_user_instructions(struct fs_control *ctl){ switch (fs_version) { case 1: case 2: if (ctl->fs_namelen == 14 || ctl->fs_namelen == 30) ctl->fs_dirsize = ctl->fs_namelen + 2; else errx(MKFS_EX_ERROR, _(“unsupported name length: %d”), ctl->fs_namelen); break; case 3: if (ctl->fs_namelen == 60) ctl->fs_dirsize = ctl->fs_namelen + 4; else errx(MKFS_EX_ERROR, _(“unsupported name length: %d”), ctl->fs_namelen); break; default: errx(MKFS_EX_ERROR, _(“unsupported minix file system version: %d”), fs_version); } ctl->fs_magic = find_super_magic(ctl);}

fs_namelen :在初始化時為30,如果沒有傳參改變它的值,那它還是30, 則進入到case2:中,fs_dirsize = 32;

然後透過ctl->fs_magic = find_super_magic(ctl);返回minix v1版本檔案系統魔數。

6。is_mounted(ctl。device_name)

透過is_mounted判斷,要格式化的該分割槽或者檔案是否已經被掛載。

7。stat(ctl。device_name, &statbuf)

透過stat函式進行獲取該裝置或者檔案的屬性資訊,並放入到statbuf中。

8。ctl。device_fd = open_blkdev_or_file(&statbuf, ctl。device_name, O_RDWR);

開啟該裝置,或者檔案, 並返回檔案描述符。

9。blkdev_lock(ctl。device_fd, ctl。device_name, ctl。lockmode)

對該開啟的裝置進行加鎖,防止其他程序訪問。

10。determine_device_blocks(&ctl, &statbuf);

獲取該裝置的儲存大小,並計算有多少fs_block,並賦值給ctl。fs_blocks。

static void determine_device_blocks(struct fs_control *ctl, const struct stat *statbuf){ unsigned long long dev_blocks = 0; if (S_ISBLK(statbuf->st_mode)) { //判斷該檔案是否為一個塊裝置 int sectorsize; if (blkdev_get_sector_size(ctl->device_fd, §orsize) == -1)//得到扇區大小 sectorsize = DEFAULT_SECTOR_SIZE; /* kernel < 2。3。3 */ if (MINIX_BLOCK_SIZE < sectorsize) //1024 errx(MKFS_EX_ERROR, _(“block size smaller than physical ” “sector size of %s”), ctl->device_name); if (blkdev_get_size(ctl->device_fd, &dev_blocks) == -1) errx(MKFS_EX_ERROR, _(“cannot determine size of %s”), ctl->device_name); dev_blocks /= MINIX_BLOCK_SIZE; } else if (!S_ISBLK(statbuf->st_mode)) dev_blocks = statbuf->st_size / MINIX_BLOCK_SIZE; //不是塊裝置時獲取檔案大小, 塊總數 = 總位元組數 / 1024 一個塊的大小。 if (!ctl->fs_blocks) ctl->fs_blocks = dev_blocks; else if (dev_blocks < ctl->fs_blocks) errx(MKFS_EX_ERROR, _(“%s: requested blocks (%llu) exceeds available (%llu) blocks\n”), ctl->device_name, ctl->fs_blocks, dev_blocks); if (ctl->fs_blocks < 10) errx(MKFS_EX_ERROR, _(“%s: number of blocks too small”), ctl->device_name); if (fs_version == 1 && ctl->fs_blocks > MINIX_MAX_INODES) ctl->fs_blocks = MINIX_MAX_INODES; if (ctl->fs_blocks > (4 + ((MINIX_MAX_INODES - 4) * BITS_PER_BLOCK))) ctl->fs_blocks = 4 + ((MINIX_MAX_INODES - 4) * BITS_PER_BLOCK); /* Utter maximum: Clip。 */}

11。

setup_tables(&ctl);

設定要寫入的超級塊的結構體。

設定超級塊的位表。

程式碼如下:

static void setup_tables(const struct fs_control *ctl) { unsigned long inodes, zmaps, imaps, zones, i; super_block_buffer = xcalloc(1, MINIX_BLOCK_SIZE); memset(boot_block_buffer,0,512); super_set_magic(ctl); if (fs_version == 3) { Super3。s_log_zone_size = 0; Super3。s_blocksize = MINIX_BLOCK_SIZE; } else { Super。s_log_zone_size = 0; } super_init_maxsize(); super_set_nzones(ctl); zones = get_nzones(); /* some magic nrs: 1 inode / 3 blocks for smaller filesystems, * for one inode / 16 blocks for large ones。 mkfs will eventually * crab about too far when getting close to the maximum size。 */ if (ctl->fs_inodes == 0) if (2048 * 1024 < ctl->fs_blocks) /* 2GB */ inodes = ctl->fs_blocks / 16; else if (512 * 1024 < ctl->fs_blocks) /* 0。5GB */ inodes = ctl->fs_blocks / 8; else inodes = ctl->fs_blocks / 3; else inodes = ctl->fs_inodes; /* Round up inode count to fill block size */ if (fs_version == 2 || fs_version == 3) inodes = ((inodes + MINIX2_INODES_PER_BLOCK - 1) & ~(MINIX2_INODES_PER_BLOCK - 1)); else inodes = ((inodes + MINIX_INODES_PER_BLOCK - 1) & ~(MINIX_INODES_PER_BLOCK - 1)); if (fs_version == 3) Super3。s_ninodes = inodes; else { if (inodes > MINIX_MAX_INODES) inodes = MINIX_MAX_INODES; Super。s_ninodes = inodes; } super_set_map_blocks(ctl, inodes); if (MINIX_MAX_INODES < first_zone_data()) errx(MKFS_EX_ERROR, _(“First data block at %jd, which is too far (max %d)。\n” “Try specifying fewer inodes by passing ——inodes ”), (intmax_t)first_zone_data(), MINIX_MAX_INODES); imaps = get_nimaps(); zmaps = get_nzmaps(); inode_map = xmalloc(imaps * MINIX_BLOCK_SIZE); zone_map = xmalloc(zmaps * MINIX_BLOCK_SIZE); memset(inode_map,0xff,imaps * MINIX_BLOCK_SIZE); memset(zone_map,0xff,zmaps * MINIX_BLOCK_SIZE); for (i = get_first_zone() ; i

12。

make_root_inode(&ctl); make_bad_inode(&ctl); mark_good_blocks(&ctl);