source: mainline/uspace/app/mkmfs/mkmfs.c@ 2f910b7

serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 2f910b7 was d1582b50, checked in by Jiri Svoboda <jiri@…>, 5 years ago

Fix spacing in single-line comments using latest ccheck

This found incorrectly formatted section comments (with blocks of
asterisks or dashes). I strongly believe against using section comments
but I am not simply removing them since that would probably be
controversial.

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