| 1 | /*
|
|---|
| 2 | * Copyright (c) 2010 Jiri Svoboda
|
|---|
| 3 | * Copyright (c) 2011 Maurizio Lombardi
|
|---|
| 4 | * All rights reserved.
|
|---|
| 5 | *
|
|---|
| 6 | * Redistribution and use in source and binary forms, with or without
|
|---|
| 7 | * modification, are permitted provided that the following conditions
|
|---|
| 8 | * are met:
|
|---|
| 9 | *
|
|---|
| 10 | * - Redistributions of source code must retain the above copyright
|
|---|
| 11 | * notice, this list of conditions and the following disclaimer.
|
|---|
| 12 | * - Redistributions in binary form must reproduce the above copyright
|
|---|
| 13 | * notice, this list of conditions and the following disclaimer in the
|
|---|
| 14 | * documentation and/or other materials provided with the distribution.
|
|---|
| 15 | * - The name of the author may not be used to endorse or promote products
|
|---|
| 16 | * derived from this software without specific prior written permission.
|
|---|
| 17 | *
|
|---|
| 18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|---|
| 19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|---|
| 20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|---|
| 21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|---|
| 22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|---|
| 23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|---|
| 24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|---|
| 25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|---|
| 26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|---|
| 27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|---|
| 28 | */
|
|---|
| 29 |
|
|---|
| 30 | /** @addtogroup fs
|
|---|
| 31 | * @{
|
|---|
| 32 | */
|
|---|
| 33 |
|
|---|
| 34 | /**
|
|---|
| 35 | * @file mkminix.c
|
|---|
| 36 | * @brief Tool for creating new Minix file systems.
|
|---|
| 37 | *
|
|---|
| 38 | */
|
|---|
| 39 |
|
|---|
| 40 | #include <stdio.h>
|
|---|
| 41 | #include <stdlib.h>
|
|---|
| 42 | #include <libblock.h>
|
|---|
| 43 | #include <unistd.h>
|
|---|
| 44 | #include <errno.h>
|
|---|
| 45 | #include <sys/typefmt.h>
|
|---|
| 46 | #include <inttypes.h>
|
|---|
| 47 | #include <str.h>
|
|---|
| 48 | #include <getopt.h>
|
|---|
| 49 | #include <mem.h>
|
|---|
| 50 | #include <str.h>
|
|---|
| 51 | #include <minix.h>
|
|---|
| 52 |
|
|---|
| 53 | #define NAME "mkminix"
|
|---|
| 54 |
|
|---|
| 55 | #define FREE 0
|
|---|
| 56 | #define USED 1
|
|---|
| 57 |
|
|---|
| 58 | #define UPPER(n, size) (((n) / (size)) + (((n) % (size)) != 0))
|
|---|
| 59 | #define NEXT_DENTRY(p, dirsize) (p += dirsize)
|
|---|
| 60 |
|
|---|
| 61 | typedef enum {
|
|---|
| 62 | HELP_SHORT,
|
|---|
| 63 | HELP_LONG
|
|---|
| 64 | } help_level_t;
|
|---|
| 65 |
|
|---|
| 66 | /*Generic MFS superblock*/
|
|---|
| 67 | struct mfs_sb_info {
|
|---|
| 68 | devmap_handle_t handle;
|
|---|
| 69 | uint64_t n_inodes;
|
|---|
| 70 | uint64_t n_zones;
|
|---|
| 71 | aoff64_t dev_nblocks;
|
|---|
| 72 | aoff64_t devblock_size;
|
|---|
| 73 | unsigned long ibmap_blocks;
|
|---|
| 74 | unsigned long zbmap_blocks;
|
|---|
| 75 | unsigned long first_data_zone;
|
|---|
| 76 | unsigned long itable_size;
|
|---|
| 77 | int log2_zone_size;
|
|---|
| 78 | int ino_per_block;
|
|---|
| 79 | int dirsize;
|
|---|
| 80 | uint32_t max_file_size;
|
|---|
| 81 | uint16_t magic;
|
|---|
| 82 | uint32_t block_size;
|
|---|
| 83 | int fs_version;
|
|---|
| 84 | bool longnames;
|
|---|
| 85 | };
|
|---|
| 86 |
|
|---|
| 87 | static void help_cmd_mkminix(help_level_t level);
|
|---|
| 88 | static int num_of_set_bits(uint32_t n);
|
|---|
| 89 | static void init_superblock(struct mfs_sb_info *sb);
|
|---|
| 90 | static void write_superblock(struct mfs_sb_info *sbi);
|
|---|
| 91 | static void write_superblock3(struct mfs_sb_info *sbi);
|
|---|
| 92 | static void init_bitmaps(struct mfs_sb_info *sb);
|
|---|
| 93 | static void init_inode_table(struct mfs_sb_info *sb);
|
|---|
| 94 | static void make_root_ino(struct mfs_sb_info *sb);
|
|---|
| 95 | static void make_root_ino3(struct mfs_sb_info *sb);
|
|---|
| 96 | static void mark_bmap(uint32_t *bmap, int idx, int v);
|
|---|
| 97 | static void insert_dentries(struct mfs_sb_info *sb);
|
|---|
| 98 |
|
|---|
| 99 | static struct option const long_options[] = {
|
|---|
| 100 | { "help", no_argument, 0, 'h' },
|
|---|
| 101 | { "long-names", no_argument, 0, 'l' },
|
|---|
| 102 | { "block-size", required_argument, 0, 'b' },
|
|---|
| 103 | { "inodes", required_argument, 0, 'i' },
|
|---|
| 104 | { NULL, no_argument, 0, '1' },
|
|---|
| 105 | { NULL, no_argument, 0, '2' },
|
|---|
| 106 | { NULL, no_argument, 0, '3' },
|
|---|
| 107 | { 0, 0, 0, 0 }
|
|---|
| 108 | };
|
|---|
| 109 |
|
|---|
| 110 | int main (int argc, char **argv)
|
|---|
| 111 | {
|
|---|
| 112 | int rc, c, opt_ind;
|
|---|
| 113 | char *device_name;
|
|---|
| 114 |
|
|---|
| 115 | struct mfs_sb_info sb;
|
|---|
| 116 |
|
|---|
| 117 | /*Default is MinixFS V3*/
|
|---|
| 118 | sb.magic = MFS_MAGIC_V3;
|
|---|
| 119 |
|
|---|
| 120 | /*Default block size is 4Kb*/
|
|---|
| 121 | sb.block_size = MFS_MAX_BLOCKSIZE;
|
|---|
| 122 | sb.n_inodes = 0;
|
|---|
| 123 | sb.longnames = false;
|
|---|
| 124 | sb.ino_per_block = V3_INODES_PER_BLOCK(MFS_MAX_BLOCKSIZE);
|
|---|
| 125 |
|
|---|
| 126 | if (argc == 1) {
|
|---|
| 127 | help_cmd_mkminix(HELP_SHORT);
|
|---|
| 128 | printf("Incorrect number of arguments, try `mkminix --help'\n");
|
|---|
| 129 | exit(0);
|
|---|
| 130 | }
|
|---|
| 131 |
|
|---|
| 132 | for (c = 0, optind = 0, opt_ind = 0; c != -1;) {
|
|---|
| 133 | c = getopt_long(argc, argv, "lh123b:i:", long_options, &opt_ind);
|
|---|
| 134 | switch (c) {
|
|---|
| 135 | case 'h':
|
|---|
| 136 | help_cmd_mkminix(HELP_LONG);
|
|---|
| 137 | exit(0);
|
|---|
| 138 | case '1':
|
|---|
| 139 | sb.magic = MFS_MAGIC_V1;
|
|---|
| 140 | sb.block_size = MFS_BLOCKSIZE;
|
|---|
| 141 | sb.fs_version = 1;
|
|---|
| 142 | sb.ino_per_block = V1_INODES_PER_BLOCK;
|
|---|
| 143 | sb.dirsize = MFS_DIRSIZE;
|
|---|
| 144 | break;
|
|---|
| 145 | case '2':
|
|---|
| 146 | sb.magic = MFS_MAGIC_V2;
|
|---|
| 147 | sb.block_size = MFS_BLOCKSIZE;
|
|---|
| 148 | sb.fs_version = 2;
|
|---|
| 149 | sb.ino_per_block = V2_INODES_PER_BLOCK;
|
|---|
| 150 | sb.dirsize = MFS_DIRSIZE;
|
|---|
| 151 | break;
|
|---|
| 152 | case '3':
|
|---|
| 153 | sb.magic = MFS_MAGIC_V3;
|
|---|
| 154 | sb.fs_version = 3;
|
|---|
| 155 | sb.block_size = MFS_MAX_BLOCKSIZE;
|
|---|
| 156 | sb.dirsize = MFS3_DIRSIZE;
|
|---|
| 157 | break;
|
|---|
| 158 | case 'b':
|
|---|
| 159 | sb.block_size = (uint32_t) strtol(optarg, NULL, 10);
|
|---|
| 160 | break;
|
|---|
| 161 | case 'i':
|
|---|
| 162 | sb.n_inodes = (uint64_t) strtol(optarg, NULL, 10);
|
|---|
| 163 | break;
|
|---|
| 164 | case 'l':
|
|---|
| 165 | sb.longnames = true;
|
|---|
| 166 | sb.dirsize = MFSL_DIRSIZE;
|
|---|
| 167 | break;
|
|---|
| 168 | }
|
|---|
| 169 | }
|
|---|
| 170 |
|
|---|
| 171 | if (sb.block_size < MFS_MIN_BLOCKSIZE ||
|
|---|
| 172 | sb.block_size > MFS_MAX_BLOCKSIZE) {
|
|---|
| 173 | printf(NAME ":Error! Invalid block size.\n");
|
|---|
| 174 | exit(0);
|
|---|
| 175 | } else if (num_of_set_bits(sb.block_size) != 1) {
|
|---|
| 176 | /*Block size must be a power of 2.*/
|
|---|
| 177 | printf(NAME ":Error! Invalid block size.\n");
|
|---|
| 178 | exit(0);
|
|---|
| 179 | } else if (sb.block_size > MFS_BLOCKSIZE &&
|
|---|
| 180 | sb.fs_version != 3) {
|
|---|
| 181 | printf(NAME ":Error! Block size > 1024 is supported by V3 filesystem only.\n");
|
|---|
| 182 | exit(0);
|
|---|
| 183 | } else if (sb.fs_version == 3 && sb.longnames) {
|
|---|
| 184 | printf(NAME ":Error! Long filenames are supported by V1/V2 filesystem only.\n");
|
|---|
| 185 | exit(0);
|
|---|
| 186 | }
|
|---|
| 187 |
|
|---|
| 188 | argv += optind;
|
|---|
| 189 |
|
|---|
| 190 | device_name = argv[0];
|
|---|
| 191 |
|
|---|
| 192 | if (!device_name) {
|
|---|
| 193 | help_cmd_mkminix(HELP_LONG);
|
|---|
| 194 | exit(0);
|
|---|
| 195 | }
|
|---|
| 196 |
|
|---|
| 197 | rc = devmap_device_get_handle(device_name, &sb.handle, 0);
|
|---|
| 198 | if (rc != EOK) {
|
|---|
| 199 | printf(NAME ": Error resolving device `%s'.\n", device_name);
|
|---|
| 200 | return 2;
|
|---|
| 201 | }
|
|---|
| 202 |
|
|---|
| 203 | rc = block_init(sb.handle, MFS_MIN_BLOCKSIZE);
|
|---|
| 204 | if (rc != EOK) {
|
|---|
| 205 | printf(NAME ": Error initializing libblock.\n");
|
|---|
| 206 | return 2;
|
|---|
| 207 | }
|
|---|
| 208 |
|
|---|
| 209 | rc = block_get_bsize(sb.handle, &sb.devblock_size);
|
|---|
| 210 | if (rc != EOK) {
|
|---|
| 211 | printf(NAME ": Error determining device block size.\n");
|
|---|
| 212 | return 2;
|
|---|
| 213 | }
|
|---|
| 214 |
|
|---|
| 215 | rc = block_get_nblocks(sb.handle, &sb.dev_nblocks);
|
|---|
| 216 | if (rc != EOK) {
|
|---|
| 217 | printf(NAME ": Warning, failed to obtain block device size.\n");
|
|---|
| 218 | } else {
|
|---|
| 219 | printf(NAME ": Block device has %" PRIuOFF64 " blocks.\n",
|
|---|
| 220 | sb.dev_nblocks);
|
|---|
| 221 | }
|
|---|
| 222 |
|
|---|
| 223 | if (sb.devblock_size != 512) {
|
|---|
| 224 | printf(NAME ": Error. Device block size is not 512 bytes.\n");
|
|---|
| 225 | return 2;
|
|---|
| 226 | }
|
|---|
| 227 |
|
|---|
| 228 | /*Minimum block size is 1 Kb*/
|
|---|
| 229 | sb.dev_nblocks /= 2;
|
|---|
| 230 |
|
|---|
| 231 | printf(NAME ": Creating Minix file system on device\n");
|
|---|
| 232 |
|
|---|
| 233 | /*Initialize superblock*/
|
|---|
| 234 | init_superblock(&sb);
|
|---|
| 235 |
|
|---|
| 236 | /*Initialize bitmaps*/
|
|---|
| 237 | init_bitmaps(&sb);
|
|---|
| 238 |
|
|---|
| 239 | /*Init inode table*/
|
|---|
| 240 | init_inode_table(&sb);
|
|---|
| 241 |
|
|---|
| 242 | /*Insert directory entries . and ..*/
|
|---|
| 243 | insert_dentries(&sb);
|
|---|
| 244 |
|
|---|
| 245 | return 0;
|
|---|
| 246 | }
|
|---|
| 247 |
|
|---|
| 248 | static void insert_dentries(struct mfs_sb_info *sb)
|
|---|
| 249 | {
|
|---|
| 250 | void *root_block;
|
|---|
| 251 | const long root_dblock = sb->first_data_zone * (sb->block_size / MFS_MIN_BLOCKSIZE);
|
|---|
| 252 |
|
|---|
| 253 | root_block = (void *) malloc(MFS_MIN_BLOCKSIZE);
|
|---|
| 254 |
|
|---|
| 255 | if (sb->fs_version != 3) {
|
|---|
| 256 | /*Directories entry for V1/V2 filesystem*/
|
|---|
| 257 | struct mfs_dentry *dentry = root_block;
|
|---|
| 258 |
|
|---|
| 259 | dentry->d_inum = MFS_ROOT_INO;
|
|---|
| 260 | str_cpy(dentry->d_name, 1, ".");
|
|---|
| 261 |
|
|---|
| 262 | NEXT_DENTRY(dentry, sb->dirsize);
|
|---|
| 263 |
|
|---|
| 264 | dentry->d_inum = MFS_ROOT_INO;
|
|---|
| 265 | str_cpy(dentry->d_name, 2, "..");
|
|---|
| 266 | } else {
|
|---|
| 267 | /*Directories entry for V3 filesystem*/
|
|---|
| 268 | struct mfs3_dentry *dentry = root_block;
|
|---|
| 269 |
|
|---|
| 270 | dentry->d_inum = MFS_ROOT_INO;
|
|---|
| 271 | str_cpy(dentry->d_name, 1, ".");
|
|---|
| 272 |
|
|---|
| 273 | NEXT_DENTRY(dentry, sb->dirsize);
|
|---|
| 274 |
|
|---|
| 275 | dentry->d_inum = MFS_ROOT_INO;
|
|---|
| 276 | str_cpy(dentry->d_name, 2, "..");
|
|---|
| 277 | }
|
|---|
| 278 |
|
|---|
| 279 | block_write_direct(sb->handle, root_dblock, 1, root_block);
|
|---|
| 280 |
|
|---|
| 281 | free(root_block);
|
|---|
| 282 | }
|
|---|
| 283 |
|
|---|
| 284 | static void init_inode_table(struct mfs_sb_info *sb)
|
|---|
| 285 | {
|
|---|
| 286 | unsigned int i;
|
|---|
| 287 | uint8_t *itable_buf;
|
|---|
| 288 | long itable_pos = 2 + sb->zbmap_blocks + sb->ibmap_blocks;
|
|---|
| 289 |
|
|---|
| 290 | itable_buf = malloc(sb->block_size);
|
|---|
| 291 | memset(itable_buf, 0x00, sb->block_size);
|
|---|
| 292 |
|
|---|
| 293 | const int chunks = sb->block_size / MFS_BLOCKSIZE;
|
|---|
| 294 |
|
|---|
| 295 | for (i = 0; i < sb->itable_size; ++i, ++itable_pos)
|
|---|
| 296 | block_write_direct(sb->handle, itable_pos, chunks, itable_buf);
|
|---|
| 297 |
|
|---|
| 298 | free(itable_buf);
|
|---|
| 299 |
|
|---|
| 300 | /*Make the root inode*/
|
|---|
| 301 | if (sb->fs_version != 3)
|
|---|
| 302 | make_root_ino(sb);
|
|---|
| 303 | else
|
|---|
| 304 | make_root_ino3(sb);
|
|---|
| 305 | }
|
|---|
| 306 |
|
|---|
| 307 | static void make_root_ino(struct mfs_sb_info *sb)
|
|---|
| 308 | {
|
|---|
| 309 | struct mfs_inode *ino_buf;
|
|---|
| 310 | const size_t bufsize = MFS_BLOCKSIZE;
|
|---|
| 311 | const long itable_pos = 2 + sb->zbmap_blocks + sb->ibmap_blocks;
|
|---|
| 312 |
|
|---|
| 313 | ino_buf = (struct mfs_inode *) malloc(bufsize);
|
|---|
| 314 | memset(ino_buf, 0x00, bufsize);
|
|---|
| 315 |
|
|---|
| 316 | ino_buf[MFS_ROOT_INO].i_mode = S_IFDIR;
|
|---|
| 317 | ino_buf[MFS_ROOT_INO].i_uid = 0;
|
|---|
| 318 | ino_buf[MFS_ROOT_INO].i_gid = 0;
|
|---|
| 319 | ino_buf[MFS_ROOT_INO].i_size = (sb->longnames ? MFSL_DIRSIZE : MFS_DIRSIZE) * 2;
|
|---|
| 320 | ino_buf[MFS_ROOT_INO].i_mtime = 0;
|
|---|
| 321 | ino_buf[MFS_ROOT_INO].i_nlinks = 2;
|
|---|
| 322 | ino_buf[MFS_ROOT_INO].i_dzone[0] = sb->first_data_zone;
|
|---|
| 323 |
|
|---|
| 324 | block_write_direct(sb->handle, itable_pos, 1, ino_buf);
|
|---|
| 325 |
|
|---|
| 326 | free(ino_buf);
|
|---|
| 327 | }
|
|---|
| 328 |
|
|---|
| 329 | static void make_root_ino3(struct mfs_sb_info *sb)
|
|---|
| 330 | {
|
|---|
| 331 | struct mfs2_inode *ino_buf;
|
|---|
| 332 | const size_t bufsize = MFS_MIN_BLOCKSIZE;
|
|---|
| 333 | const long itable_pos = 2 + sb->zbmap_blocks + sb->ibmap_blocks;
|
|---|
| 334 |
|
|---|
| 335 | ino_buf = (struct mfs2_inode *) malloc(bufsize);
|
|---|
| 336 | memset(ino_buf, 0x00, bufsize);
|
|---|
| 337 |
|
|---|
| 338 | ino_buf[MFS_ROOT_INO].i_mode = S_IFDIR;
|
|---|
| 339 | ino_buf[MFS_ROOT_INO].i_uid = 0;
|
|---|
| 340 | ino_buf[MFS_ROOT_INO].i_gid = 0;
|
|---|
| 341 | ino_buf[MFS_ROOT_INO].i_size = MFS3_DIRSIZE * 2;
|
|---|
| 342 | ino_buf[MFS_ROOT_INO].i_mtime = 0;
|
|---|
| 343 | ino_buf[MFS_ROOT_INO].i_atime = 0;
|
|---|
| 344 | ino_buf[MFS_ROOT_INO].i_ctime = 0;
|
|---|
| 345 | ino_buf[MFS_ROOT_INO].i_nlinks = 2;
|
|---|
| 346 | ino_buf[MFS_ROOT_INO].i_dzone[0] = sb->first_data_zone;
|
|---|
| 347 |
|
|---|
| 348 | block_write_direct(sb->handle, itable_pos * (sb->block_size / MFS_MIN_BLOCKSIZE), 1, ino_buf);
|
|---|
| 349 |
|
|---|
| 350 | free(ino_buf);
|
|---|
| 351 | }
|
|---|
| 352 |
|
|---|
| 353 | static void init_superblock(struct mfs_sb_info *sb)
|
|---|
| 354 | {
|
|---|
| 355 | aoff64_t inodes;
|
|---|
| 356 |
|
|---|
| 357 | if (sb->longnames)
|
|---|
| 358 | sb->magic = sb->fs_version == 1 ? MFS_MAGIC_V1L : MFS_MAGIC_V2L;
|
|---|
| 359 |
|
|---|
| 360 | /*Compute the number of zones on disk*/
|
|---|
| 361 |
|
|---|
| 362 | if (sb->fs_version == 1) {
|
|---|
| 363 | /*Valid only for MFS V1*/
|
|---|
| 364 | sb->n_zones = sb->dev_nblocks > UINT16_MAX ?
|
|---|
| 365 | UINT16_MAX : sb->dev_nblocks;
|
|---|
| 366 | } else {
|
|---|
| 367 | /*Valid for MFS V2/V3*/
|
|---|
| 368 | sb->n_zones = sb->dev_nblocks > UINT32_MAX ?
|
|---|
| 369 | UINT32_MAX : sb->dev_nblocks;
|
|---|
| 370 |
|
|---|
| 371 | if (sb->fs_version == 3) {
|
|---|
| 372 | sb->ino_per_block = V3_INODES_PER_BLOCK(sb->block_size);
|
|---|
| 373 | sb->n_zones /= (sb->block_size / MFS_MIN_BLOCKSIZE);
|
|---|
| 374 | }
|
|---|
| 375 | }
|
|---|
| 376 |
|
|---|
| 377 | /*Round up the number of inodes to fill block size*/
|
|---|
| 378 | if (sb->n_inodes == 0)
|
|---|
| 379 | inodes = sb->dev_nblocks / 3;
|
|---|
| 380 | else
|
|---|
| 381 | inodes = sb->n_inodes;
|
|---|
| 382 |
|
|---|
| 383 | if (inodes % sb->ino_per_block)
|
|---|
| 384 | inodes = ((inodes / sb->ino_per_block) + 1) * sb->ino_per_block;
|
|---|
| 385 |
|
|---|
| 386 | if (sb->fs_version < 3)
|
|---|
| 387 | sb->n_inodes = inodes > UINT16_MAX ? UINT16_MAX : inodes;
|
|---|
| 388 | else
|
|---|
| 389 | sb->n_inodes = inodes > UINT32_MAX ? UINT32_MAX : inodes;
|
|---|
| 390 |
|
|---|
| 391 | /*Compute inode bitmap size in blocks*/
|
|---|
| 392 | sb->ibmap_blocks = UPPER(sb->n_inodes, sb->block_size * 8);
|
|---|
| 393 |
|
|---|
| 394 | /*Compute zone bitmap size in blocks*/
|
|---|
| 395 | sb->zbmap_blocks = UPPER(sb->n_zones, sb->block_size * 8);
|
|---|
| 396 |
|
|---|
| 397 | /*Compute inode table size*/
|
|---|
| 398 | sb->itable_size = sb->n_inodes / sb->ino_per_block;
|
|---|
| 399 |
|
|---|
| 400 | /*Compute first data zone position*/
|
|---|
| 401 | sb->first_data_zone = 2 + sb->itable_size +
|
|---|
| 402 | sb->zbmap_blocks + sb->ibmap_blocks;
|
|---|
| 403 |
|
|---|
| 404 | /*Set log2 of zone to block ratio to zero*/
|
|---|
| 405 | sb->log2_zone_size = 0;
|
|---|
| 406 |
|
|---|
| 407 | /*Superblock is now ready to be written on disk*/
|
|---|
| 408 | printf(NAME ": %d inodes\n", (uint32_t) sb->n_inodes);
|
|---|
| 409 | printf(NAME ": %d zones\n", (uint32_t) sb->n_zones);
|
|---|
| 410 | printf(NAME ": inode table blocks = %ld\n", sb->itable_size);
|
|---|
| 411 | printf(NAME ": inode bitmap blocks = %ld\n", sb->ibmap_blocks);
|
|---|
| 412 | printf(NAME ": zone bitmap blocks = %ld\n", sb->zbmap_blocks);
|
|---|
| 413 | printf(NAME ": first data zone = %d\n", (uint32_t) sb->first_data_zone);
|
|---|
| 414 | printf(NAME ": long fnames = %s\n", sb->longnames ? "Yes" : "No");
|
|---|
| 415 |
|
|---|
| 416 | if (sb->fs_version == 3)
|
|---|
| 417 | write_superblock3(sb);
|
|---|
| 418 | else
|
|---|
| 419 | write_superblock(sb);
|
|---|
| 420 | }
|
|---|
| 421 |
|
|---|
| 422 | static void write_superblock(struct mfs_sb_info *sbi)
|
|---|
| 423 | {
|
|---|
| 424 | struct mfs_superblock *sb;
|
|---|
| 425 |
|
|---|
| 426 | sb = (struct mfs_superblock *) malloc(MFS_SUPERBLOCK_SIZE);;
|
|---|
| 427 |
|
|---|
| 428 | sb->s_ninodes = (uint16_t) sbi->n_inodes;
|
|---|
| 429 | sb->s_nzones = (uint16_t) sbi->n_zones;
|
|---|
| 430 | sb->s_nzones2 = (uint32_t) sbi->n_zones;
|
|---|
| 431 | sb->s_ibmap_blocks = (uint16_t) sbi->ibmap_blocks;
|
|---|
| 432 | sb->s_zbmap_blocks = (uint16_t) sbi->zbmap_blocks;
|
|---|
| 433 | sb->s_first_data_zone = (uint16_t) sbi->first_data_zone;
|
|---|
| 434 | sb->s_log2_zone_size = sbi->log2_zone_size;
|
|---|
| 435 | sb->s_max_file_size = UINT32_MAX;
|
|---|
| 436 | sb->s_magic = sbi->magic;
|
|---|
| 437 | sb->s_state = MFS_VALID_FS;
|
|---|
| 438 |
|
|---|
| 439 | block_write_direct(sbi->handle, MFS_SUPERBLOCK, 1, sb);
|
|---|
| 440 | free(sb);
|
|---|
| 441 | }
|
|---|
| 442 |
|
|---|
| 443 | static void write_superblock3(struct mfs_sb_info *sbi)
|
|---|
| 444 | {
|
|---|
| 445 | struct mfs3_superblock *sb;
|
|---|
| 446 |
|
|---|
| 447 | sb = (struct mfs3_superblock *) malloc(MFS_SUPERBLOCK_SIZE);
|
|---|
| 448 |
|
|---|
| 449 | sb->s_ninodes = (uint32_t) sbi->n_inodes;
|
|---|
| 450 | sb->s_nzones = (uint32_t) sbi->n_zones;
|
|---|
| 451 | sb->s_ibmap_blocks = (uint16_t) sbi->ibmap_blocks;
|
|---|
| 452 | sb->s_zbmap_blocks = (uint16_t) sbi->zbmap_blocks;
|
|---|
| 453 | sb->s_first_data_zone = (uint16_t) sbi->first_data_zone;
|
|---|
| 454 | sb->s_log2_zone_size = sbi->log2_zone_size;
|
|---|
| 455 | sb->s_max_file_size = UINT32_MAX;
|
|---|
| 456 | sb->s_magic = sbi->magic;
|
|---|
| 457 | sb->s_block_size = sbi->block_size;
|
|---|
| 458 | sb->s_disk_version = 3;
|
|---|
| 459 |
|
|---|
| 460 | block_write_direct(sbi->handle, MFS_SUPERBLOCK, 1, sb);
|
|---|
| 461 | free(sb);
|
|---|
| 462 | }
|
|---|
| 463 |
|
|---|
| 464 | static void init_bitmaps(struct mfs_sb_info *sb)
|
|---|
| 465 | {
|
|---|
| 466 | uint32_t *ibmap_buf, *zbmap_buf;
|
|---|
| 467 | int ibmap_nblocks = 1 + (sb->n_inodes / 8) / sb->block_size;
|
|---|
| 468 | int zbmap_nblocks = 1 + (sb->n_zones / 8) / sb->block_size;
|
|---|
| 469 | unsigned int i;
|
|---|
| 470 |
|
|---|
| 471 | ibmap_buf = (uint32_t *) malloc(ibmap_nblocks * sb->block_size);
|
|---|
| 472 | zbmap_buf = (uint32_t *) malloc(zbmap_nblocks * sb->block_size);
|
|---|
| 473 |
|
|---|
| 474 | memset(ibmap_buf, 0xFF, ibmap_nblocks * sb->block_size);
|
|---|
| 475 | memset(zbmap_buf, 0xFF, zbmap_nblocks * sb->block_size);
|
|---|
| 476 |
|
|---|
| 477 | for (i = 2; i < sb->n_inodes; ++i)
|
|---|
| 478 | mark_bmap(ibmap_buf, i, FREE);
|
|---|
| 479 |
|
|---|
| 480 | for (i = 2; i < sb->n_zones; ++i)
|
|---|
| 481 | mark_bmap(zbmap_buf, i, FREE);
|
|---|
| 482 |
|
|---|
| 483 | ibmap_nblocks *= sb->block_size / MFS_BLOCKSIZE;
|
|---|
| 484 | zbmap_nblocks *= sb->block_size / MFS_BLOCKSIZE;
|
|---|
| 485 |
|
|---|
| 486 | block_write_direct(sb->handle, 2, ibmap_nblocks, ibmap_buf);
|
|---|
| 487 | block_write_direct(sb->handle, 2 + ibmap_nblocks, zbmap_nblocks, zbmap_buf);
|
|---|
| 488 |
|
|---|
| 489 | free(ibmap_buf);
|
|---|
| 490 | free(zbmap_buf);
|
|---|
| 491 | }
|
|---|
| 492 |
|
|---|
| 493 | static void mark_bmap(uint32_t *bmap, int idx, int v)
|
|---|
| 494 | {
|
|---|
| 495 | if (v == FREE)
|
|---|
| 496 | bmap[idx / 32] &= ~(1 << (idx % 32));
|
|---|
| 497 | else
|
|---|
| 498 | bmap[idx / 32] |= 1 << (idx % 32);
|
|---|
| 499 | }
|
|---|
| 500 |
|
|---|
| 501 | static void help_cmd_mkminix(help_level_t level)
|
|---|
| 502 | {
|
|---|
| 503 | if (level == HELP_SHORT) {
|
|---|
| 504 | printf(NAME": tool to create new Minix file systems\n");
|
|---|
| 505 | } else {
|
|---|
| 506 | printf("Usage: [options] device\n"
|
|---|
| 507 | "-1 Make a Minix version 1 filesystem\n"
|
|---|
| 508 | "-2 Make a Minix version 2 filesystem\n"
|
|---|
| 509 | "-3 Make a Minix version 3 filesystem\n"
|
|---|
| 510 | "-b ## Specify the block size in bytes (V3 only),\n"
|
|---|
| 511 | " valid block size values are 1024, 2048 and 4096 bytes per block\n"
|
|---|
| 512 | "-i ## Specify the number of inodes for the filesystem\n"
|
|---|
| 513 | "-l Use 30-char long filenames (V1/V2 only)\n");
|
|---|
| 514 | }
|
|---|
| 515 | }
|
|---|
| 516 |
|
|---|
| 517 | static int num_of_set_bits(uint32_t n)
|
|---|
| 518 | {
|
|---|
| 519 | n = n - ((n >> 1) & 0x55555555);
|
|---|
| 520 | n = (n & 0x33333333) + ((n >> 2) & 0x33333333);
|
|---|
| 521 | return (((n + (n >> 4)) & 0xF0F0F0F) * 0x1010101) >> 24;
|
|---|
| 522 | }
|
|---|
| 523 |
|
|---|
| 524 |
|
|---|
| 525 | /**
|
|---|
| 526 | * @}
|
|---|
| 527 | */
|
|---|