minix文件系统是怎么格式化的?

minix文件系统是怎么格式化的?ctl.fs_blocks = strtoul_or_err;通过stat函数进行获取该设备或者文件的属性信息,并放入到statbuf中。

欢迎大家来到IT世界,在知识的湖畔探索吧!

先上个代码:

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; }

欢迎大家来到IT世界,在知识的湖畔探索吧!

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);

欢迎大家来到IT世界,在知识的湖畔探索吧!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);
设置要写入的超级块的结构体。
设置超级块的位表。
代码如下:

欢迎大家来到IT世界,在知识的湖畔探索吧!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 <num>"), (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<zones ; i++) unmark_zone(i); for (i = MINIX_ROOT_INO ; i<=inodes; i++) unmark_inode(i); inode_buffer = xmalloc(get_inode_buffer_size()); memset(inode_buffer,0, get_inode_buffer_size()); printf(P_("%lu inode\n", "%lu inodes\n", inodes), inodes); printf(P_("%lu block\n", "%lu blocks\n", zones), zones); printf(_("Firstdatazone=%jd (%jd)\n"), (intmax_t)get_first_zone(), (intmax_t)first_zone_data()); printf(_("Zonesize=%zu\n"), (size_t) MINIX_BLOCK_SIZE << get_zone_size()); printf(_("Maxsize=%zu\n\n"),get_max_size()); } 

12.

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

免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://itzsg.com/65446.html

(0)

相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

联系我们YX

mu99908888

在线咨询: 微信交谈

邮件:itzsgw@126.com

工作时间:时刻准备着!

关注微信