[954bf385] | 1 | /*
|
---|
| 2 | * Copyright (c) 2011 Maurizio Lombardi
|
---|
| 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
| 29 | /** @addtogroup fs
|
---|
| 30 | * @{
|
---|
| 31 | */
|
---|
| 32 |
|
---|
| 33 | /**
|
---|
[7a46bfe] | 34 | * @file mkmfs.c
|
---|
[954bf385] | 35 | * @brief Tool for creating new Minix file systems.
|
---|
| 36 | *
|
---|
| 37 | */
|
---|
| 38 |
|
---|
| 39 | #include <stdio.h>
|
---|
| 40 | #include <stdlib.h>
|
---|
[f73b291] | 41 | #include <block.h>
|
---|
[954bf385] | 42 | #include <errno.h>
|
---|
| 43 | #include <inttypes.h>
|
---|
| 44 | #include <getopt.h>
|
---|
| 45 | #include <mem.h>
|
---|
[fd282ad] | 46 | #include <str.h>
|
---|
[4727a97a] | 47 | #include <time.h>
|
---|
[86d0b4b3] | 48 | #include <minix.h>
|
---|
[954bf385] | 49 |
|
---|
[7a46bfe] | 50 | #define NAME "mkmfs"
|
---|
[954bf385] | 51 |
|
---|
[e6aaa59] | 52 | #define FREE 0
|
---|
| 53 | #define USED 1
|
---|
| 54 |
|
---|
[8367d1d] | 55 | #define UPPER(n, size) (((n) / (size)) + (((n) % (size)) != 0))
|
---|
| 56 | #define NEXT_DENTRY(p, dirsize) (p += (dirsize))
|
---|
[ae1ae27] | 57 |
|
---|
[954bf385] | 58 | typedef enum {
|
---|
| 59 | HELP_SHORT,
|
---|
| 60 | HELP_LONG
|
---|
| 61 | } help_level_t;
|
---|
| 62 |
|
---|
[8367d1d] | 63 | /* Generic MFS superblock */
|
---|
[59e670e] | 64 | struct mfs_sb_info {
|
---|
| 65 | uint64_t n_inodes;
|
---|
| 66 | uint64_t n_zones;
|
---|
| 67 | aoff64_t dev_nblocks;
|
---|
| 68 | unsigned long ibmap_blocks;
|
---|
| 69 | unsigned long zbmap_blocks;
|
---|
| 70 | unsigned long first_data_zone;
|
---|
[410a065] | 71 | unsigned long itable_size;
|
---|
[59e670e] | 72 | int log2_zone_size;
|
---|
| 73 | int ino_per_block;
|
---|
[fd282ad] | 74 | int dirsize;
|
---|
[59e670e] | 75 | uint32_t max_file_size;
|
---|
| 76 | uint16_t magic;
|
---|
| 77 | uint32_t block_size;
|
---|
| 78 | int fs_version;
|
---|
| 79 | bool longnames;
|
---|
| 80 | };
|
---|
[19a057f9] | 81 |
|
---|
[7a46bfe] | 82 | static void help_cmd_mkmfs(help_level_t level);
|
---|
[5726a1a3] | 83 | static bool is_power_of_two(uint32_t n);
|
---|
[b7fd2a0] | 84 | static errno_t init_superblock(struct mfs_sb_info *sb);
|
---|
| 85 | static errno_t write_superblock(const struct mfs_sb_info *sbi);
|
---|
| 86 | static errno_t write_superblock3(const struct mfs_sb_info *sbi);
|
---|
| 87 | static errno_t init_bitmaps(const struct mfs_sb_info *sb);
|
---|
| 88 | static errno_t init_inode_table(const struct mfs_sb_info *sb);
|
---|
| 89 | static errno_t make_root_ino(const struct mfs_sb_info *sb);
|
---|
| 90 | static errno_t make_root_ino2(const struct mfs_sb_info *sb);
|
---|
[59e670e] | 91 | static void mark_bmap(uint32_t *bmap, int idx, int v);
|
---|
[b7fd2a0] | 92 | static errno_t insert_dentries(const struct mfs_sb_info *sb);
|
---|
[197b671] | 93 |
|
---|
[b7fd2a0] | 94 | static inline errno_t write_block(aoff64_t off, size_t size, const void *data);
|
---|
[b84175a] | 95 |
|
---|
[03bc76a] | 96 | static service_id_t service_id;
|
---|
[f66d903] | 97 | static int shift;
|
---|
[954bf385] | 98 |
|
---|
| 99 | static struct option const long_options[] = {
|
---|
[e91b898] | 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 | { 0, 0, 0, 0 }
|
---|
[954bf385] | 107 | };
|
---|
| 108 |
|
---|
| 109 | int main (int argc, char **argv)
|
---|
| 110 | {
|
---|
[b7fd2a0] | 111 | errno_t rc;
|
---|
[d5c1051] | 112 | int c, opt_ind;
|
---|
[954bf385] | 113 | char *device_name;
|
---|
[16cc9a6] | 114 | size_t devblock_size;
|
---|
[954bf385] | 115 |
|
---|
[59e670e] | 116 | struct mfs_sb_info sb;
|
---|
[954bf385] | 117 |
|
---|
[8367d1d] | 118 | /* Default is MinixFS V3 */
|
---|
[59e670e] | 119 | sb.magic = MFS_MAGIC_V3;
|
---|
[6cda025] | 120 | sb.fs_version = 3;
|
---|
[eee8007] | 121 |
|
---|
[8367d1d] | 122 | /* Default block size is 4Kb */
|
---|
[59e670e] | 123 | sb.block_size = MFS_MAX_BLOCKSIZE;
|
---|
[c4c7f5a] | 124 | sb.dirsize = MFS3_DIRSIZE;
|
---|
[59e670e] | 125 | sb.n_inodes = 0;
|
---|
| 126 | sb.longnames = false;
|
---|
| 127 | sb.ino_per_block = V3_INODES_PER_BLOCK(MFS_MAX_BLOCKSIZE);
|
---|
[eaf9794] | 128 |
|
---|
[954bf385] | 129 | if (argc == 1) {
|
---|
[7a46bfe] | 130 | help_cmd_mkmfs(HELP_SHORT);
|
---|
| 131 | printf("Incorrect number of arguments, try `mkmfs --help'\n");
|
---|
[954bf385] | 132 | exit(0);
|
---|
| 133 | }
|
---|
| 134 |
|
---|
| 135 | for (c = 0, optind = 0, opt_ind = 0; c != -1;) {
|
---|
[8367d1d] | 136 | c = getopt_long(argc, argv, "lh12b:i:",
|
---|
| 137 | long_options, &opt_ind);
|
---|
[954bf385] | 138 | switch (c) {
|
---|
| 139 | case 'h':
|
---|
[7a46bfe] | 140 | help_cmd_mkmfs(HELP_LONG);
|
---|
[954bf385] | 141 | exit(0);
|
---|
| 142 | case '1':
|
---|
[59e670e] | 143 | sb.magic = MFS_MAGIC_V1;
|
---|
| 144 | sb.block_size = MFS_BLOCKSIZE;
|
---|
| 145 | sb.fs_version = 1;
|
---|
| 146 | sb.ino_per_block = V1_INODES_PER_BLOCK;
|
---|
[fd282ad] | 147 | sb.dirsize = MFS_DIRSIZE;
|
---|
[954bf385] | 148 | break;
|
---|
| 149 | case '2':
|
---|
[59e670e] | 150 | sb.magic = MFS_MAGIC_V2;
|
---|
| 151 | sb.block_size = MFS_BLOCKSIZE;
|
---|
| 152 | sb.fs_version = 2;
|
---|
| 153 | sb.ino_per_block = V2_INODES_PER_BLOCK;
|
---|
[fd282ad] | 154 | sb.dirsize = MFS_DIRSIZE;
|
---|
[954bf385] | 155 | break;
|
---|
| 156 | case 'b':
|
---|
[59e670e] | 157 | sb.block_size = (uint32_t) strtol(optarg, NULL, 10);
|
---|
[954bf385] | 158 | break;
|
---|
| 159 | case 'i':
|
---|
[c64506b] | 160 | sb.n_inodes = (uint64_t) strtol(optarg, NULL, 10);
|
---|
[eee8007] | 161 | break;
|
---|
| 162 | case 'l':
|
---|
[59e670e] | 163 | sb.longnames = true;
|
---|
[fd282ad] | 164 | sb.dirsize = MFSL_DIRSIZE;
|
---|
[954bf385] | 165 | break;
|
---|
| 166 | }
|
---|
| 167 | }
|
---|
| 168 |
|
---|
[1b20da0] | 169 | if (sb.block_size < MFS_MIN_BLOCKSIZE ||
|
---|
[8367d1d] | 170 | sb.block_size > MFS_MAX_BLOCKSIZE) {
|
---|
[954bf385] | 171 | printf(NAME ":Error! Invalid block size.\n");
|
---|
| 172 | exit(0);
|
---|
[5726a1a3] | 173 | } else if (!is_power_of_two(sb.block_size)) {
|
---|
[8367d1d] | 174 | /* Block size must be a power of 2. */
|
---|
[954bf385] | 175 | printf(NAME ":Error! Invalid block size.\n");
|
---|
| 176 | exit(0);
|
---|
[1b20da0] | 177 | } else if (sb.block_size > MFS_BLOCKSIZE &&
|
---|
[8367d1d] | 178 | sb.fs_version != 3) {
|
---|
| 179 | printf(NAME ":Error! Block size > 1024 is "
|
---|
| 180 | "supported by V3 filesystem only.\n");
|
---|
[eee8007] | 181 | exit(0);
|
---|
[59e670e] | 182 | } else if (sb.fs_version == 3 && sb.longnames) {
|
---|
[8367d1d] | 183 | printf(NAME ":Error! Long filenames are supported "
|
---|
| 184 | "by V1/V2 filesystem only.\n");
|
---|
[954bf385] | 185 | exit(0);
|
---|
| 186 | }
|
---|
| 187 |
|
---|
[f66d903] | 188 | if (sb.block_size == MFS_MIN_BLOCKSIZE)
|
---|
| 189 | shift = 1;
|
---|
| 190 | else if (sb.block_size == MFS_MAX_BLOCKSIZE)
|
---|
| 191 | shift = 3;
|
---|
| 192 | else
|
---|
| 193 | shift = 2;
|
---|
| 194 |
|
---|
[954bf385] | 195 | argv += optind;
|
---|
| 196 |
|
---|
| 197 | device_name = argv[0];
|
---|
| 198 |
|
---|
| 199 | if (!device_name) {
|
---|
[7a46bfe] | 200 | help_cmd_mkmfs(HELP_LONG);
|
---|
[954bf385] | 201 | exit(0);
|
---|
| 202 | }
|
---|
| 203 |
|
---|
[03bc76a] | 204 | rc = loc_service_get_id(device_name, &service_id, 0);
|
---|
[954bf385] | 205 | if (rc != EOK) {
|
---|
| 206 | printf(NAME ": Error resolving device `%s'.\n", device_name);
|
---|
| 207 | return 2;
|
---|
| 208 | }
|
---|
| 209 |
|
---|
[fc22069] | 210 | rc = block_init(service_id, 2048);
|
---|
[954bf385] | 211 | if (rc != EOK) {
|
---|
| 212 | printf(NAME ": Error initializing libblock.\n");
|
---|
| 213 | return 2;
|
---|
| 214 | }
|
---|
| 215 |
|
---|
[03bc76a] | 216 | rc = block_get_bsize(service_id, &devblock_size);
|
---|
[954bf385] | 217 | if (rc != EOK) {
|
---|
| 218 | printf(NAME ": Error determining device block size.\n");
|
---|
| 219 | return 2;
|
---|
| 220 | }
|
---|
| 221 |
|
---|
[03bc76a] | 222 | rc = block_get_nblocks(service_id, &sb.dev_nblocks);
|
---|
[954bf385] | 223 | if (rc != EOK) {
|
---|
[8367d1d] | 224 | printf(NAME ": Warning, failed to obtain "
|
---|
| 225 | "block device size.\n");
|
---|
[954bf385] | 226 | } else {
|
---|
| 227 | printf(NAME ": Block device has %" PRIuOFF64 " blocks.\n",
|
---|
[8367d1d] | 228 | sb.dev_nblocks);
|
---|
[954bf385] | 229 | }
|
---|
| 230 |
|
---|
[197b671] | 231 | if (devblock_size != 512) {
|
---|
[954bf385] | 232 | printf(NAME ": Error. Device block size is not 512 bytes.\n");
|
---|
| 233 | return 2;
|
---|
| 234 | }
|
---|
| 235 |
|
---|
[8367d1d] | 236 | /* Minimum block size is 1 Kb */
|
---|
[59e670e] | 237 | sb.dev_nblocks /= 2;
|
---|
[68ed0fb] | 238 |
|
---|
| 239 | printf(NAME ": Creating Minix file system on device\n");
|
---|
[5726a1a3] | 240 | printf(NAME ": Writing superblock\n");
|
---|
[954bf385] | 241 |
|
---|
[8367d1d] | 242 | /* Initialize superblock */
|
---|
[0b07acd] | 243 | if (init_superblock(&sb) != EOK) {
|
---|
| 244 | printf(NAME ": Error. Superblock initialization failed\n");
|
---|
| 245 | return 2;
|
---|
| 246 | }
|
---|
[eaf9794] | 247 |
|
---|
[5726a1a3] | 248 | printf(NAME ": Initializing bitmaps\n");
|
---|
| 249 |
|
---|
[8367d1d] | 250 | /* Initialize bitmaps */
|
---|
[0b07acd] | 251 | if (init_bitmaps(&sb) != EOK) {
|
---|
| 252 | printf(NAME ": Error. Bitmaps initialization failed\n");
|
---|
| 253 | return 2;
|
---|
| 254 | }
|
---|
[eee8007] | 255 |
|
---|
[5726a1a3] | 256 | printf(NAME ": Initializing the inode table\n");
|
---|
| 257 |
|
---|
[8367d1d] | 258 | /* Init inode table */
|
---|
[0b07acd] | 259 | if (init_inode_table(&sb) != EOK) {
|
---|
| 260 | printf(NAME ": Error. Inode table initialization failed\n");
|
---|
| 261 | return 2;
|
---|
| 262 | }
|
---|
[410a065] | 263 |
|
---|
[5726a1a3] | 264 | printf(NAME ": Creating the root directory inode\n");
|
---|
| 265 |
|
---|
[8367d1d] | 266 | /* Make the root inode */
|
---|
[bed0356] | 267 | if (sb.fs_version == 1)
|
---|
[0b07acd] | 268 | rc = make_root_ino(&sb);
|
---|
[bed0356] | 269 | else
|
---|
| 270 | rc = make_root_ino2(&sb);
|
---|
[0b07acd] | 271 |
|
---|
| 272 | if (rc != EOK) {
|
---|
| 273 | printf(NAME ": Error. Root inode initialization failed\n");
|
---|
| 274 | return 2;
|
---|
| 275 | }
|
---|
[f0ceb1d] | 276 |
|
---|
[8367d1d] | 277 | /* Insert directory entries . and .. */
|
---|
[0b07acd] | 278 | if (insert_dentries(&sb) != EOK) {
|
---|
| 279 | printf(NAME ": Error. Root directory initialization failed\n");
|
---|
| 280 | return 2;
|
---|
| 281 | }
|
---|
[fd282ad] | 282 |
|
---|
[03bc76a] | 283 | block_fini(service_id);
|
---|
| 284 |
|
---|
[954bf385] | 285 | return 0;
|
---|
| 286 | }
|
---|
| 287 |
|
---|
[2eaf655] | 288 | /**Inserts the '.' and '..' directory entries in the root directory.
|
---|
| 289 | *
|
---|
| 290 | * @param sb Pointer to the superblock structure.
|
---|
| 291 | *
|
---|
[cde999a] | 292 | * @return EOK on success or an error code.
|
---|
[2eaf655] | 293 | */
|
---|
[b7fd2a0] | 294 | static errno_t insert_dentries(const struct mfs_sb_info *sb)
|
---|
[fd282ad] | 295 | {
|
---|
| 296 | void *root_block;
|
---|
[fcce9e1] | 297 | uint8_t *dentry_ptr;
|
---|
[b7fd2a0] | 298 | errno_t rc;
|
---|
[f66d903] | 299 | const long root_dblock = sb->first_data_zone;
|
---|
[fd282ad] | 300 |
|
---|
[b438804] | 301 | root_block = malloc(sb->block_size);
|
---|
[f66d903] | 302 | memset(root_block, 0x00, sb->block_size);
|
---|
[0b07acd] | 303 |
|
---|
| 304 | if (!root_block)
|
---|
| 305 | return ENOMEM;
|
---|
[fcce9e1] | 306 |
|
---|
| 307 | dentry_ptr = root_block;
|
---|
[e91b898] | 308 |
|
---|
[fd282ad] | 309 | if (sb->fs_version != 3) {
|
---|
[8367d1d] | 310 | /* Directory entries for V1/V2 filesystem */
|
---|
[fd282ad] | 311 | struct mfs_dentry *dentry = root_block;
|
---|
| 312 |
|
---|
| 313 | dentry->d_inum = MFS_ROOT_INO;
|
---|
[14c29ba] | 314 | memcpy(dentry->d_name, ".\0", 2);
|
---|
[fd282ad] | 315 |
|
---|
[fcce9e1] | 316 | dentry = (struct mfs_dentry *) NEXT_DENTRY(dentry_ptr,
|
---|
[8367d1d] | 317 | sb->dirsize);
|
---|
[fd282ad] | 318 |
|
---|
| 319 | dentry->d_inum = MFS_ROOT_INO;
|
---|
[14c29ba] | 320 | memcpy(dentry->d_name, "..\0", 3);
|
---|
[fd282ad] | 321 | } else {
|
---|
[8367d1d] | 322 | /* Directory entries for V3 filesystem */
|
---|
[fd282ad] | 323 | struct mfs3_dentry *dentry = root_block;
|
---|
| 324 |
|
---|
| 325 | dentry->d_inum = MFS_ROOT_INO;
|
---|
[14c29ba] | 326 | memcpy(dentry->d_name, ".\0", 2);
|
---|
[fd282ad] | 327 |
|
---|
[fcce9e1] | 328 | dentry = (struct mfs3_dentry *) NEXT_DENTRY(dentry_ptr,
|
---|
[8367d1d] | 329 | sb->dirsize);
|
---|
[fd282ad] | 330 |
|
---|
| 331 | dentry->d_inum = MFS_ROOT_INO;
|
---|
[14c29ba] | 332 | memcpy(dentry->d_name, "..\0", 3);
|
---|
[fd282ad] | 333 | }
|
---|
| 334 |
|
---|
[f66d903] | 335 | rc = write_block(root_dblock, 1, root_block);
|
---|
[fd282ad] | 336 |
|
---|
| 337 | free(root_block);
|
---|
[0b07acd] | 338 | return rc;
|
---|
[fd282ad] | 339 | }
|
---|
| 340 |
|
---|
[2eaf655] | 341 | /**Initialize the inode table.
|
---|
| 342 | *
|
---|
| 343 | * @param sb Pointer to the superblock structure.
|
---|
| 344 | *
|
---|
[cde999a] | 345 | * @return EOK on success or an error code.
|
---|
[2eaf655] | 346 | */
|
---|
[b7fd2a0] | 347 | static errno_t init_inode_table(const struct mfs_sb_info *sb)
|
---|
[410a065] | 348 | {
|
---|
| 349 | unsigned int i;
|
---|
| 350 | uint8_t *itable_buf;
|
---|
[b7fd2a0] | 351 | errno_t rc = EOK;
|
---|
[340b5690] | 352 |
|
---|
[f66d903] | 353 | long itable_off = sb->zbmap_blocks + sb->ibmap_blocks + 2;
|
---|
| 354 | unsigned long itable_size = sb->itable_size;
|
---|
[340b5690] | 355 |
|
---|
[f66d903] | 356 | itable_buf = malloc(sb->block_size);
|
---|
[0b07acd] | 357 |
|
---|
| 358 | if (!itable_buf)
|
---|
| 359 | return ENOMEM;
|
---|
| 360 |
|
---|
[f66d903] | 361 | memset(itable_buf, 0x00, sb->block_size);
|
---|
[410a065] | 362 |
|
---|
[8ceba1e] | 363 | for (i = 0; i < itable_size; ++i, ++itable_off) {
|
---|
[f66d903] | 364 | rc = write_block(itable_off, 1, itable_buf);
|
---|
[0b07acd] | 365 |
|
---|
| 366 | if (rc != EOK)
|
---|
| 367 | break;
|
---|
| 368 | }
|
---|
[410a065] | 369 |
|
---|
| 370 | free(itable_buf);
|
---|
[0b07acd] | 371 | return rc;
|
---|
[410a065] | 372 | }
|
---|
| 373 |
|
---|
[2eaf655] | 374 | /**Initialize a V1 root inode.
|
---|
| 375 | *
|
---|
| 376 | * @param sb Ponter to the superblock structure.
|
---|
| 377 | *
|
---|
[cde999a] | 378 | * @return EOK on success or an error code.
|
---|
[2eaf655] | 379 | */
|
---|
[b7fd2a0] | 380 | static errno_t make_root_ino(const struct mfs_sb_info *sb)
|
---|
[410a065] | 381 | {
|
---|
| 382 | struct mfs_inode *ino_buf;
|
---|
[b7fd2a0] | 383 | errno_t rc;
|
---|
[410a065] | 384 |
|
---|
[f66d903] | 385 | const long itable_off = sb->zbmap_blocks + sb->ibmap_blocks + 2;
|
---|
[340b5690] | 386 |
|
---|
[4727a97a] | 387 | const time_t sec = time(NULL);
|
---|
| 388 |
|
---|
[b438804] | 389 | ino_buf = malloc(MFS_BLOCKSIZE);
|
---|
[0b07acd] | 390 |
|
---|
| 391 | if (!ino_buf)
|
---|
| 392 | return ENOMEM;
|
---|
| 393 |
|
---|
[4727a97a] | 394 | memset(ino_buf, 0x00, MFS_BLOCKSIZE);
|
---|
[410a065] | 395 |
|
---|
[e33100c] | 396 | ino_buf[MFS_ROOT_INO - 1].i_mode = S_IFDIR;
|
---|
| 397 | ino_buf[MFS_ROOT_INO - 1].i_uid = 0;
|
---|
| 398 | ino_buf[MFS_ROOT_INO - 1].i_gid = 0;
|
---|
| 399 | ino_buf[MFS_ROOT_INO - 1].i_size = (sb->longnames ? MFSL_DIRSIZE :
|
---|
[8367d1d] | 400 | MFS_DIRSIZE) * 2;
|
---|
[e33100c] | 401 | ino_buf[MFS_ROOT_INO - 1].i_mtime = sec;
|
---|
| 402 | ino_buf[MFS_ROOT_INO - 1].i_nlinks = 2;
|
---|
| 403 | ino_buf[MFS_ROOT_INO - 1].i_dzone[0] = sb->first_data_zone;
|
---|
[410a065] | 404 |
|
---|
[f66d903] | 405 | rc = write_block(itable_off, 1, ino_buf);
|
---|
[410a065] | 406 |
|
---|
| 407 | free(ino_buf);
|
---|
[0b07acd] | 408 | return rc;
|
---|
[410a065] | 409 | }
|
---|
| 410 |
|
---|
[2eaf655] | 411 | /**Initialize a Minix V2 root inode on disk, also valid for V3 filesystem.
|
---|
| 412 | *
|
---|
| 413 | * @param sb Pointer to the superblock structure.
|
---|
| 414 | *
|
---|
[cde999a] | 415 | * @return EOK on success or an error code.
|
---|
[2eaf655] | 416 | */
|
---|
[b7fd2a0] | 417 | static errno_t make_root_ino2(const struct mfs_sb_info *sb)
|
---|
[410a065] | 418 | {
|
---|
| 419 | struct mfs2_inode *ino_buf;
|
---|
[b7fd2a0] | 420 | errno_t rc;
|
---|
[410a065] | 421 |
|
---|
[8367d1d] | 422 | /* Compute offset of the first inode table block */
|
---|
[f66d903] | 423 | const long itable_off = sb->zbmap_blocks + sb->ibmap_blocks + 2;
|
---|
[340b5690] | 424 |
|
---|
[4727a97a] | 425 | const time_t sec = time(NULL);
|
---|
| 426 |
|
---|
[b438804] | 427 | ino_buf = malloc(sb->block_size);
|
---|
[0b07acd] | 428 |
|
---|
| 429 | if (!ino_buf)
|
---|
| 430 | return ENOMEM;
|
---|
| 431 |
|
---|
[f66d903] | 432 | memset(ino_buf, 0x00, sb->block_size);
|
---|
[410a065] | 433 |
|
---|
[e33100c] | 434 | ino_buf[MFS_ROOT_INO - 1].i_mode = S_IFDIR;
|
---|
| 435 | ino_buf[MFS_ROOT_INO - 1].i_uid = 0;
|
---|
| 436 | ino_buf[MFS_ROOT_INO - 1].i_gid = 0;
|
---|
| 437 | ino_buf[MFS_ROOT_INO - 1].i_size = MFS3_DIRSIZE * 2;
|
---|
| 438 | ino_buf[MFS_ROOT_INO - 1].i_mtime = sec;
|
---|
| 439 | ino_buf[MFS_ROOT_INO - 1].i_atime = sec;
|
---|
| 440 | ino_buf[MFS_ROOT_INO - 1].i_ctime = sec;
|
---|
| 441 | ino_buf[MFS_ROOT_INO - 1].i_nlinks = 2;
|
---|
| 442 | ino_buf[MFS_ROOT_INO - 1].i_dzone[0] = sb->first_data_zone;
|
---|
[410a065] | 443 |
|
---|
[f66d903] | 444 | rc = write_block(itable_off, 1, ino_buf);
|
---|
[410a065] | 445 |
|
---|
| 446 | free(ino_buf);
|
---|
[0b07acd] | 447 | return rc;
|
---|
[410a065] | 448 | }
|
---|
| 449 |
|
---|
[2eaf655] | 450 | /**Initialize the superblock structure on disk.
|
---|
| 451 | *
|
---|
| 452 | * @param sb Pointer to the superblock structure.
|
---|
| 453 | *
|
---|
[cde999a] | 454 | * @return EOK on success or an error code.
|
---|
[2eaf655] | 455 | */
|
---|
[b7fd2a0] | 456 | static errno_t init_superblock(struct mfs_sb_info *sb)
|
---|
[eee8007] | 457 | {
|
---|
[e2267e82] | 458 | aoff64_t inodes;
|
---|
[99f043e] | 459 | unsigned long ind;
|
---|
| 460 | unsigned long ind2;
|
---|
| 461 | unsigned long zones;
|
---|
[b7fd2a0] | 462 | errno_t rc;
|
---|
[68ed0fb] | 463 |
|
---|
[59e670e] | 464 | if (sb->longnames)
|
---|
[8367d1d] | 465 | sb->magic = sb->fs_version == 1 ? MFS_MAGIC_V1L :
|
---|
| 466 | MFS_MAGIC_V2L;
|
---|
[939b7d2] | 467 |
|
---|
[8367d1d] | 468 | /* Compute the number of zones on disk */
|
---|
[e03a733] | 469 |
|
---|
[59e670e] | 470 | if (sb->fs_version == 1) {
|
---|
[8367d1d] | 471 | /* Valid only for MFS V1 */
|
---|
[1b20da0] | 472 | sb->n_zones = sb->dev_nblocks > UINT16_MAX ?
|
---|
[8367d1d] | 473 | UINT16_MAX : sb->dev_nblocks;
|
---|
[99f043e] | 474 | ind = MFS_BLOCKSIZE / sizeof(uint16_t);
|
---|
| 475 | ind2 = ind * ind;
|
---|
[8367d1d] | 476 | sb->max_file_size = (V1_NR_DIRECT_ZONES + ind + ind2) *
|
---|
| 477 | MFS_BLOCKSIZE;
|
---|
[59e670e] | 478 | } else {
|
---|
| 479 | /*Valid for MFS V2/V3*/
|
---|
[99f043e] | 480 | size_t ptrsize;
|
---|
| 481 | if (sb->fs_version == 2)
|
---|
| 482 | ptrsize = sizeof(uint16_t);
|
---|
| 483 | else
|
---|
| 484 | ptrsize = sizeof(uint32_t);
|
---|
[8367d1d] | 485 |
|
---|
[99f043e] | 486 | ind = sb->block_size / ptrsize;
|
---|
| 487 | ind2 = ind * ind;
|
---|
| 488 | zones = V2_NR_DIRECT_ZONES + ind + ind2;
|
---|
| 489 | sb->max_file_size = zones * sb->block_size;
|
---|
[59e670e] | 490 | sb->n_zones = sb->dev_nblocks > UINT32_MAX ?
|
---|
[8367d1d] | 491 | UINT32_MAX : sb->dev_nblocks;
|
---|
[19a057f9] | 492 |
|
---|
[59e670e] | 493 | if (sb->fs_version == 3) {
|
---|
[99f043e] | 494 | if(INT32_MAX / sb->block_size < zones)
|
---|
| 495 | sb->max_file_size = INT32_MAX;
|
---|
[59e670e] | 496 | sb->ino_per_block = V3_INODES_PER_BLOCK(sb->block_size);
|
---|
| 497 | sb->n_zones /= (sb->block_size / MFS_MIN_BLOCKSIZE);
|
---|
| 498 | }
|
---|
| 499 | }
|
---|
[19a057f9] | 500 |
|
---|
[8367d1d] | 501 | /* Round up the number of inodes to fill block size */
|
---|
[59e670e] | 502 | if (sb->n_inodes == 0)
|
---|
| 503 | inodes = sb->dev_nblocks / 3;
|
---|
[c64506b] | 504 | else
|
---|
| 505 | inodes = sb->n_inodes;
|
---|
[68ed0fb] | 506 |
|
---|
[59e670e] | 507 | if (inodes % sb->ino_per_block)
|
---|
[8367d1d] | 508 | inodes = ((inodes / sb->ino_per_block) + 1) *
|
---|
| 509 | sb->ino_per_block;
|
---|
[e91b898] | 510 |
|
---|
[59e670e] | 511 | if (sb->fs_version < 3)
|
---|
| 512 | sb->n_inodes = inodes > UINT16_MAX ? UINT16_MAX : inodes;
|
---|
| 513 | else
|
---|
| 514 | sb->n_inodes = inodes > UINT32_MAX ? UINT32_MAX : inodes;
|
---|
[68ed0fb] | 515 |
|
---|
[8367d1d] | 516 | /* Compute inode bitmap size in blocks */
|
---|
[59e670e] | 517 | sb->ibmap_blocks = UPPER(sb->n_inodes, sb->block_size * 8);
|
---|
[68ed0fb] | 518 |
|
---|
[8367d1d] | 519 | /* Compute inode table size */
|
---|
[410a065] | 520 | sb->itable_size = sb->n_inodes / sb->ino_per_block;
|
---|
[e03a733] | 521 |
|
---|
[8367d1d] | 522 | /* Compute zone bitmap size in blocks */
|
---|
[340b5690] | 523 | sb->zbmap_blocks = UPPER(sb->n_zones, sb->block_size * 8);
|
---|
| 524 |
|
---|
[8367d1d] | 525 | /* Compute first data zone position */
|
---|
[1b20da0] | 526 | sb->first_data_zone = 2 + sb->itable_size +
|
---|
[8367d1d] | 527 | sb->zbmap_blocks + sb->ibmap_blocks;
|
---|
[ae1ae27] | 528 |
|
---|
[8367d1d] | 529 | /* Set log2 of zone to block ratio to zero */
|
---|
[59e670e] | 530 | sb->log2_zone_size = 0;
|
---|
[ae1ae27] | 531 |
|
---|
[8367d1d] | 532 | /* Check for errors */
|
---|
[0b07acd] | 533 | if (sb->first_data_zone >= sb->n_zones) {
|
---|
| 534 | printf(NAME ": Error! Insufficient disk space");
|
---|
| 535 | return ENOMEM;
|
---|
| 536 | }
|
---|
| 537 |
|
---|
[8367d1d] | 538 | /* Superblock is now ready to be written on disk */
|
---|
[86c03ba] | 539 | printf(NAME ": %d block size\n", sb->block_size);
|
---|
[59e670e] | 540 | printf(NAME ": %d inodes\n", (uint32_t) sb->n_inodes);
|
---|
| 541 | printf(NAME ": %d zones\n", (uint32_t) sb->n_zones);
|
---|
[410a065] | 542 | printf(NAME ": inode table blocks = %ld\n", sb->itable_size);
|
---|
| 543 | printf(NAME ": inode bitmap blocks = %ld\n", sb->ibmap_blocks);
|
---|
| 544 | printf(NAME ": zone bitmap blocks = %ld\n", sb->zbmap_blocks);
|
---|
[8367d1d] | 545 | printf(NAME ": first data zone = %d\n", (uint32_t)sb->first_data_zone);
|
---|
[99f043e] | 546 | printf(NAME ": max file size = %u\n", sb->max_file_size);
|
---|
[59e670e] | 547 | printf(NAME ": long fnames = %s\n", sb->longnames ? "Yes" : "No");
|
---|
[bc99ed6] | 548 |
|
---|
[59e670e] | 549 | if (sb->fs_version == 3)
|
---|
[0b07acd] | 550 | rc = write_superblock3(sb);
|
---|
[59e670e] | 551 | else
|
---|
[0b07acd] | 552 | rc = write_superblock(sb);
|
---|
| 553 |
|
---|
| 554 | return rc;
|
---|
[eee8007] | 555 | }
|
---|
| 556 |
|
---|
[2eaf655] | 557 | /**Write the V1/V2 superblock on disk.
|
---|
| 558 | *
|
---|
| 559 | * @param sbi Pointer to the superblock structure to write on disk.
|
---|
| 560 | *
|
---|
[cde999a] | 561 | * @return EOK on success or an error code.
|
---|
[2eaf655] | 562 | */
|
---|
[b7fd2a0] | 563 | static errno_t write_superblock(const struct mfs_sb_info *sbi)
|
---|
[eee8007] | 564 | {
|
---|
[59e670e] | 565 | struct mfs_superblock *sb;
|
---|
[b7fd2a0] | 566 | errno_t rc;
|
---|
[e03a733] | 567 |
|
---|
[b438804] | 568 | sb = malloc(MFS_SUPERBLOCK_SIZE);;
|
---|
[e03a733] | 569 |
|
---|
[0b07acd] | 570 | if (!sb)
|
---|
| 571 | return ENOMEM;
|
---|
| 572 |
|
---|
[59e670e] | 573 | sb->s_ninodes = (uint16_t) sbi->n_inodes;
|
---|
| 574 | sb->s_nzones = (uint16_t) sbi->n_zones;
|
---|
| 575 | sb->s_nzones2 = (uint32_t) sbi->n_zones;
|
---|
| 576 | sb->s_ibmap_blocks = (uint16_t) sbi->ibmap_blocks;
|
---|
| 577 | sb->s_zbmap_blocks = (uint16_t) sbi->zbmap_blocks;
|
---|
| 578 | sb->s_first_data_zone = (uint16_t) sbi->first_data_zone;
|
---|
| 579 | sb->s_log2_zone_size = sbi->log2_zone_size;
|
---|
[99f043e] | 580 | sb->s_max_file_size = sbi->max_file_size;
|
---|
[59e670e] | 581 | sb->s_magic = sbi->magic;
|
---|
| 582 | sb->s_state = MFS_VALID_FS;
|
---|
[e03a733] | 583 |
|
---|
[f66d903] | 584 | rc = write_block(MFS_SUPERBLOCK, 1, sb);
|
---|
[8e41994] | 585 | free(sb);
|
---|
[0b07acd] | 586 |
|
---|
| 587 | return rc;
|
---|
[59e670e] | 588 | }
|
---|
[e03a733] | 589 |
|
---|
[2eaf655] | 590 | /**Write the V3s superblock on disk.
|
---|
| 591 | *
|
---|
| 592 | * @param sbi Pointer to the superblock structure to write on disk.
|
---|
| 593 | *
|
---|
[cde999a] | 594 | * @return EOK on success or an error code.
|
---|
[2eaf655] | 595 | */
|
---|
[b7fd2a0] | 596 | static errno_t write_superblock3(const struct mfs_sb_info *sbi)
|
---|
[59e670e] | 597 | {
|
---|
| 598 | struct mfs3_superblock *sb;
|
---|
[b7fd2a0] | 599 | errno_t rc;
|
---|
[59e670e] | 600 |
|
---|
[b438804] | 601 | sb = malloc(MFS_SUPERBLOCK_SIZE);
|
---|
[59e670e] | 602 |
|
---|
[0b07acd] | 603 | if (!sb)
|
---|
| 604 | return ENOMEM;
|
---|
| 605 |
|
---|
[59e670e] | 606 | sb->s_ninodes = (uint32_t) sbi->n_inodes;
|
---|
| 607 | sb->s_nzones = (uint32_t) sbi->n_zones;
|
---|
| 608 | sb->s_ibmap_blocks = (uint16_t) sbi->ibmap_blocks;
|
---|
| 609 | sb->s_zbmap_blocks = (uint16_t) sbi->zbmap_blocks;
|
---|
| 610 | sb->s_first_data_zone = (uint16_t) sbi->first_data_zone;
|
---|
| 611 | sb->s_log2_zone_size = sbi->log2_zone_size;
|
---|
[99f043e] | 612 | sb->s_max_file_size = sbi->max_file_size;
|
---|
[59e670e] | 613 | sb->s_magic = sbi->magic;
|
---|
| 614 | sb->s_block_size = sbi->block_size;
|
---|
[e03a733] | 615 | sb->s_disk_version = 3;
|
---|
[bc99ed6] | 616 |
|
---|
[03bc76a] | 617 | rc = block_write_direct(service_id, MFS_SUPERBLOCK << 1, 1 << 1, sb);
|
---|
[410a065] | 618 | free(sb);
|
---|
[0b07acd] | 619 |
|
---|
| 620 | return rc;
|
---|
[eee8007] | 621 | }
|
---|
| 622 |
|
---|
[2eaf655] | 623 | /**Initialize the inode and block bitmaps on disk.
|
---|
| 624 | *
|
---|
| 625 | * @param sb Pointer to the superblock structure.
|
---|
| 626 | *
|
---|
[cde999a] | 627 | * @return EOK on success or an error code.
|
---|
[2eaf655] | 628 | */
|
---|
[b7fd2a0] | 629 | static errno_t init_bitmaps(const struct mfs_sb_info *sb)
|
---|
[e6aaa59] | 630 | {
|
---|
[bb0db564] | 631 | uint32_t *ibmap_buf, *zbmap_buf;
|
---|
| 632 | uint8_t *ibmap_buf8, *zbmap_buf8;
|
---|
[f66d903] | 633 | const unsigned int ibmap_nblocks = sb->ibmap_blocks;
|
---|
| 634 | const unsigned int zbmap_nblocks = sb->zbmap_blocks;
|
---|
[e6aaa59] | 635 | unsigned int i;
|
---|
[b7fd2a0] | 636 | errno_t rc = EOK;
|
---|
[e6aaa59] | 637 |
|
---|
[b438804] | 638 | ibmap_buf = malloc(ibmap_nblocks * sb->block_size);
|
---|
| 639 | zbmap_buf = malloc(zbmap_nblocks * sb->block_size);
|
---|
[e6aaa59] | 640 |
|
---|
[88ab6e9] | 641 | if (!ibmap_buf || !zbmap_buf) {
|
---|
| 642 | rc = ENOMEM;
|
---|
| 643 | goto exit;
|
---|
| 644 | }
|
---|
[0b07acd] | 645 |
|
---|
[59e670e] | 646 | memset(ibmap_buf, 0xFF, ibmap_nblocks * sb->block_size);
|
---|
| 647 | memset(zbmap_buf, 0xFF, zbmap_nblocks * sb->block_size);
|
---|
[e6aaa59] | 648 |
|
---|
[e33100c] | 649 | for (i = 2; i < sb->n_inodes + 1; ++i)
|
---|
[bb0db564] | 650 | mark_bmap(ibmap_buf, i, FREE);
|
---|
[f5cbd4f] | 651 |
|
---|
[0395a7b] | 652 | for (i = 2; i < sb->n_zones - sb->first_data_zone; ++i)
|
---|
[bb0db564] | 653 | mark_bmap(zbmap_buf, i, FREE);
|
---|
[e6aaa59] | 654 |
|
---|
[bb0db564] | 655 | ibmap_buf8 = (uint8_t *) ibmap_buf;
|
---|
| 656 | zbmap_buf8 = (uint8_t *) zbmap_buf;
|
---|
| 657 |
|
---|
[f66d903] | 658 | int start_block = 2;
|
---|
[8ceba1e] | 659 |
|
---|
[7df022e5] | 660 | for (i = 0; i < ibmap_nblocks; ++i) {
|
---|
[f66d903] | 661 | if ((rc = write_block(start_block + i,
|
---|
[8367d1d] | 662 | 1, (ibmap_buf8 + i * sb->block_size))) != EOK)
|
---|
[04efacc] | 663 | goto exit;
|
---|
[7df022e5] | 664 | }
|
---|
| 665 |
|
---|
[f66d903] | 666 | start_block = 2 + ibmap_nblocks;
|
---|
[8ceba1e] | 667 |
|
---|
[7df022e5] | 668 | for (i = 0; i < zbmap_nblocks; ++i) {
|
---|
[f66d903] | 669 | if ((rc = write_block(start_block + i,
|
---|
[8367d1d] | 670 | 1, (zbmap_buf8 + i * sb->block_size))) != EOK)
|
---|
[04efacc] | 671 | goto exit;
|
---|
[7df022e5] | 672 | }
|
---|
| 673 |
|
---|
[88ab6e9] | 674 | exit:
|
---|
[c4c7f5a] | 675 | free(ibmap_buf);
|
---|
| 676 | free(zbmap_buf);
|
---|
| 677 |
|
---|
[0b07acd] | 678 | return rc;
|
---|
[e6aaa59] | 679 | }
|
---|
| 680 |
|
---|
[2eaf655] | 681 | /**Mark a bitmap entry as used or free.
|
---|
| 682 | *
|
---|
| 683 | * @param bmap 32-bit pointer to the bitmap in memory.
|
---|
| 684 | * @param idx The index in the bitmap of the bit to set at 1 or 0.
|
---|
| 685 | * @param v FREE to clear the bit, USED to set the bit.
|
---|
| 686 | */
|
---|
[59e670e] | 687 | static void mark_bmap(uint32_t *bmap, int idx, int v)
|
---|
[e6aaa59] | 688 | {
|
---|
| 689 | if (v == FREE)
|
---|
[59e670e] | 690 | bmap[idx / 32] &= ~(1 << (idx % 32));
|
---|
[e6aaa59] | 691 | else
|
---|
[59e670e] | 692 | bmap[idx / 32] |= 1 << (idx % 32);
|
---|
[e6aaa59] | 693 | }
|
---|
| 694 |
|
---|
[2eaf655] | 695 | /**Write a block on disk.
|
---|
| 696 | *
|
---|
| 697 | * @param off 64-bit block offset on disk.
|
---|
| 698 | * @param size size of the block.
|
---|
| 699 | * @param data Pointer to the block content.
|
---|
| 700 | *
|
---|
[cde999a] | 701 | * @return EOK on success or a error number.
|
---|
[2eaf655] | 702 | */
|
---|
[b7fd2a0] | 703 | static inline errno_t write_block(aoff64_t off, size_t size, const void *data)
|
---|
[b84175a] | 704 | {
|
---|
[f66d903] | 705 | if (shift == 3) {
|
---|
[b7fd2a0] | 706 | errno_t rc;
|
---|
[f66d903] | 707 | aoff64_t tmp_off = off << 1;
|
---|
| 708 | uint8_t *data_ptr = (uint8_t *) data;
|
---|
| 709 |
|
---|
[8367d1d] | 710 | rc = block_write_direct(service_id, tmp_off << 2,
|
---|
| 711 | size << 2, data_ptr);
|
---|
[f66d903] | 712 |
|
---|
| 713 | if (rc != EOK)
|
---|
| 714 | return rc;
|
---|
| 715 |
|
---|
| 716 | data_ptr += 2048;
|
---|
| 717 | tmp_off++;
|
---|
| 718 |
|
---|
[8367d1d] | 719 | return block_write_direct(service_id, tmp_off << 2,
|
---|
| 720 | size << 2, data_ptr);
|
---|
[f66d903] | 721 | }
|
---|
[8367d1d] | 722 | return block_write_direct(service_id, off << shift,
|
---|
| 723 | size << shift, data);
|
---|
[b84175a] | 724 | }
|
---|
| 725 |
|
---|
[7a46bfe] | 726 | static void help_cmd_mkmfs(help_level_t level)
|
---|
[954bf385] | 727 | {
|
---|
| 728 | if (level == HELP_SHORT) {
|
---|
| 729 | printf(NAME": tool to create new Minix file systems\n");
|
---|
| 730 | } else {
|
---|
[e91b898] | 731 | printf("Usage: [options] device\n"
|
---|
[8367d1d] | 732 | "-1 Make a Minix version 1 filesystem\n"
|
---|
| 733 | "-2 Make a Minix version 2 filesystem\n"
|
---|
| 734 | "-b ## Specify the block size in bytes (V3 only),\n"
|
---|
| 735 | " valid block size values are 1024, 2048 and"
|
---|
| 736 | " 4096 bytes per block\n"
|
---|
| 737 | "-i ## Specify the number of inodes"
|
---|
| 738 | " for the filesystem\n"
|
---|
| 739 | "-l Use 30-char long filenames (V1/V2 only)\n");
|
---|
[954bf385] | 740 | }
|
---|
| 741 | }
|
---|
| 742 |
|
---|
[8367d1d] | 743 | /** Check if a given number is a power of two.
|
---|
| 744 | *
|
---|
| 745 | * @param n The number to check.
|
---|
| 746 | *
|
---|
| 747 | * @return true if it is a power of two, false otherwise.
|
---|
| 748 | */
|
---|
[5726a1a3] | 749 | static bool is_power_of_two(uint32_t n)
|
---|
[954bf385] | 750 | {
|
---|
[5726a1a3] | 751 | if (n == 0)
|
---|
| 752 | return false;
|
---|
| 753 |
|
---|
| 754 | return (n & (n - 1)) == 0;
|
---|
[954bf385] | 755 | }
|
---|
| 756 |
|
---|
| 757 |
|
---|
| 758 | /**
|
---|
| 759 | * @}
|
---|
| 760 | */
|
---|