source: mainline/uspace/app/mkminix/mkminix.c@ fcce9e1

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since fcce9e1 was fcce9e1, checked in by Maurizio Lombardi <m.lombardi85@…>, 14 years ago

fix directory entries initialization

  • Property mode set to 100644
File size: 16.5 KB
RevLine 
[954bf385]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 <getopt.h>
48#include <mem.h>
[fd282ad]49#include <str.h>
[4727a97a]50#include <time.h>
[86d0b4b3]51#include <minix.h>
[954bf385]52
53#define NAME "mkminix"
54
[e6aaa59]55#define FREE 0
56#define USED 1
57
[8ceba1e]58#define UPPER(n, size) (((n) / (size)) + (((n) % (size)) != 0))
[fcce9e1]59#define NEXT_DENTRY(p, dirsize) (p += (dirsize))
[ae1ae27]60
[954bf385]61typedef enum {
62 HELP_SHORT,
63 HELP_LONG
64} help_level_t;
65
[59e670e]66/*Generic MFS superblock*/
67struct mfs_sb_info {
68 uint64_t n_inodes;
69 uint64_t n_zones;
70 aoff64_t dev_nblocks;
71 unsigned long ibmap_blocks;
72 unsigned long zbmap_blocks;
73 unsigned long first_data_zone;
[410a065]74 unsigned long itable_size;
[59e670e]75 int log2_zone_size;
76 int ino_per_block;
[fd282ad]77 int dirsize;
[59e670e]78 uint32_t max_file_size;
79 uint16_t magic;
80 uint32_t block_size;
81 int fs_version;
82 bool longnames;
83};
[19a057f9]84
[eee8007]85static void help_cmd_mkminix(help_level_t level);
86static int num_of_set_bits(uint32_t n);
[0b07acd]87static int init_superblock(struct mfs_sb_info *sb);
[197b671]88static int write_superblock(const struct mfs_sb_info *sbi);
89static int write_superblock3(const struct mfs_sb_info *sbi);
90static int init_bitmaps(const struct mfs_sb_info *sb);
91static int init_inode_table(const struct mfs_sb_info *sb);
92static int make_root_ino(const struct mfs_sb_info *sb);
93static int make_root_ino3(const struct mfs_sb_info *sb);
[59e670e]94static void mark_bmap(uint32_t *bmap, int idx, int v);
[197b671]95static int insert_dentries(const struct mfs_sb_info *sb);
96
[f66d903]97static inline int write_block(aoff64_t off, size_t size, const void *data);
[b84175a]98
[197b671]99static devmap_handle_t handle;
[f66d903]100static int shift;
[954bf385]101
102static struct option const long_options[] = {
103 { "help", no_argument, 0, 'h' },
[eee8007]104 { "long-names", no_argument, 0, 'l' },
[3c616f6]105 { "block-size", required_argument, 0, 'b' },
[954bf385]106 { "inodes", required_argument, 0, 'i' },
107 { NULL, no_argument, 0, '1' },
108 { NULL, no_argument, 0, '2' },
109 { NULL, no_argument, 0, '3' },
110 { 0, 0, 0, 0 }
111};
112
113int main (int argc, char **argv)
114{
115 int rc, c, opt_ind;
116 char *device_name;
[197b671]117 aoff64_t devblock_size;
[954bf385]118
[59e670e]119 struct mfs_sb_info sb;
[954bf385]120
[eee8007]121 /*Default is MinixFS V3*/
[59e670e]122 sb.magic = MFS_MAGIC_V3;
[6cda025]123 sb.fs_version = 3;
[eee8007]124
125 /*Default block size is 4Kb*/
[59e670e]126 sb.block_size = MFS_MAX_BLOCKSIZE;
[c4c7f5a]127 sb.dirsize = MFS3_DIRSIZE;
[59e670e]128 sb.n_inodes = 0;
129 sb.longnames = false;
130 sb.ino_per_block = V3_INODES_PER_BLOCK(MFS_MAX_BLOCKSIZE);
[eaf9794]131
[954bf385]132 if (argc == 1) {
133 help_cmd_mkminix(HELP_SHORT);
134 printf("Incorrect number of arguments, try `mkminix --help'\n");
135 exit(0);
136 }
137
138 for (c = 0, optind = 0, opt_ind = 0; c != -1;) {
[e6aaa59]139 c = getopt_long(argc, argv, "lh123b:i:", long_options, &opt_ind);
[954bf385]140 switch (c) {
141 case 'h':
142 help_cmd_mkminix(HELP_LONG);
143 exit(0);
144 case '1':
[59e670e]145 sb.magic = MFS_MAGIC_V1;
146 sb.block_size = MFS_BLOCKSIZE;
147 sb.fs_version = 1;
148 sb.ino_per_block = V1_INODES_PER_BLOCK;
[fd282ad]149 sb.dirsize = MFS_DIRSIZE;
[954bf385]150 break;
151 case '2':
[59e670e]152 sb.magic = MFS_MAGIC_V2;
153 sb.block_size = MFS_BLOCKSIZE;
154 sb.fs_version = 2;
155 sb.ino_per_block = V2_INODES_PER_BLOCK;
[fd282ad]156 sb.dirsize = MFS_DIRSIZE;
[954bf385]157 break;
158 case '3':
[59e670e]159 sb.magic = MFS_MAGIC_V3;
160 sb.fs_version = 3;
161 sb.block_size = MFS_MAX_BLOCKSIZE;
[fd282ad]162 sb.dirsize = MFS3_DIRSIZE;
[fcce9e1]163 sb.ino_per_block = V3_INODES_PER_BLOCK(sb.block_size);
[954bf385]164 break;
165 case 'b':
[59e670e]166 sb.block_size = (uint32_t) strtol(optarg, NULL, 10);
[954bf385]167 break;
168 case 'i':
[c64506b]169 sb.n_inodes = (uint64_t) strtol(optarg, NULL, 10);
[eee8007]170 break;
171 case 'l':
[59e670e]172 sb.longnames = true;
[fd282ad]173 sb.dirsize = MFSL_DIRSIZE;
[954bf385]174 break;
175 }
176 }
177
[59e670e]178 if (sb.block_size < MFS_MIN_BLOCKSIZE ||
179 sb.block_size > MFS_MAX_BLOCKSIZE) {
[954bf385]180 printf(NAME ":Error! Invalid block size.\n");
181 exit(0);
[59e670e]182 } else if (num_of_set_bits(sb.block_size) != 1) {
[eaf9794]183 /*Block size must be a power of 2.*/
[954bf385]184 printf(NAME ":Error! Invalid block size.\n");
185 exit(0);
[59e670e]186 } else if (sb.block_size > MFS_BLOCKSIZE &&
187 sb.fs_version != 3) {
[eee8007]188 printf(NAME ":Error! Block size > 1024 is supported by V3 filesystem only.\n");
189 exit(0);
[59e670e]190 } else if (sb.fs_version == 3 && sb.longnames) {
[eee8007]191 printf(NAME ":Error! Long filenames are supported by V1/V2 filesystem only.\n");
[954bf385]192 exit(0);
193 }
194
[f66d903]195 if (sb.block_size == MFS_MIN_BLOCKSIZE)
196 shift = 1;
197 else if (sb.block_size == MFS_MAX_BLOCKSIZE)
198 shift = 3;
199 else
200 shift = 2;
201
[954bf385]202 argv += optind;
203
204 device_name = argv[0];
205
206 if (!device_name) {
207 help_cmd_mkminix(HELP_LONG);
208 exit(0);
209 }
210
[197b671]211 rc = devmap_device_get_handle(device_name, &handle, 0);
[954bf385]212 if (rc != EOK) {
213 printf(NAME ": Error resolving device `%s'.\n", device_name);
214 return 2;
215 }
216
[f66d903]217 rc = block_init(handle, 2048);
[954bf385]218 if (rc != EOK) {
219 printf(NAME ": Error initializing libblock.\n");
220 return 2;
221 }
222
[197b671]223 rc = block_get_bsize(handle, &devblock_size);
[954bf385]224 if (rc != EOK) {
225 printf(NAME ": Error determining device block size.\n");
226 return 2;
227 }
228
[197b671]229 rc = block_get_nblocks(handle, &sb.dev_nblocks);
[954bf385]230 if (rc != EOK) {
231 printf(NAME ": Warning, failed to obtain block device size.\n");
232 } else {
233 printf(NAME ": Block device has %" PRIuOFF64 " blocks.\n",
[59e670e]234 sb.dev_nblocks);
[954bf385]235 }
236
[197b671]237 if (devblock_size != 512) {
[954bf385]238 printf(NAME ": Error. Device block size is not 512 bytes.\n");
239 return 2;
240 }
241
[68ed0fb]242 /*Minimum block size is 1 Kb*/
[59e670e]243 sb.dev_nblocks /= 2;
[68ed0fb]244
245 printf(NAME ": Creating Minix file system on device\n");
[954bf385]246
[59e670e]247 /*Initialize superblock*/
[0b07acd]248 if (init_superblock(&sb) != EOK) {
249 printf(NAME ": Error. Superblock initialization failed\n");
250 return 2;
251 }
[eaf9794]252
[59e670e]253 /*Initialize bitmaps*/
[0b07acd]254 if (init_bitmaps(&sb) != EOK) {
255 printf(NAME ": Error. Bitmaps initialization failed\n");
256 return 2;
257 }
[eee8007]258
[410a065]259 /*Init inode table*/
[0b07acd]260 if (init_inode_table(&sb) != EOK) {
261 printf(NAME ": Error. Inode table initialization failed\n");
262 return 2;
263 }
[410a065]264
[f0ceb1d]265 /*Make the root inode*/
266 if (sb.fs_version == 3)
[0b07acd]267 rc = make_root_ino3(&sb);
[f0ceb1d]268 else
[0b07acd]269 rc = make_root_ino(&sb);
270
271 if (rc != EOK) {
272 printf(NAME ": Error. Root inode initialization failed\n");
273 return 2;
274 }
[f0ceb1d]275
[fd282ad]276 /*Insert directory entries . and ..*/
[0b07acd]277 if (insert_dentries(&sb) != EOK) {
278 printf(NAME ": Error. Root directory initialization failed\n");
279 return 2;
280 }
[fd282ad]281
[954bf385]282 return 0;
283}
284
[197b671]285static int insert_dentries(const struct mfs_sb_info *sb)
[fd282ad]286{
287 void *root_block;
[fcce9e1]288 uint8_t *dentry_ptr;
[0b07acd]289 int rc;
[f66d903]290 const long root_dblock = sb->first_data_zone;
[fd282ad]291
[f66d903]292 root_block = (void *) malloc(sb->block_size);
293 memset(root_block, 0x00, sb->block_size);
[0b07acd]294
295 if (!root_block)
296 return ENOMEM;
[fcce9e1]297
298 dentry_ptr = root_block;
[fd282ad]299
300 if (sb->fs_version != 3) {
[f0ceb1d]301 /*Directory entries for V1/V2 filesystem*/
[fd282ad]302 struct mfs_dentry *dentry = root_block;
303
304 dentry->d_inum = MFS_ROOT_INO;
[14c29ba]305 memcpy(dentry->d_name, ".\0", 2);
[fd282ad]306
[fcce9e1]307 dentry = (struct mfs_dentry *) NEXT_DENTRY(dentry_ptr,
308 sb->dirsize);
[fd282ad]309
310 dentry->d_inum = MFS_ROOT_INO;
[14c29ba]311 memcpy(dentry->d_name, "..\0", 3);
[fd282ad]312 } else {
[f0ceb1d]313 /*Directory entries for V3 filesystem*/
[fd282ad]314 struct mfs3_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 mfs3_dentry *) NEXT_DENTRY(dentry_ptr,
320 sb->dirsize);
[fd282ad]321
322 dentry->d_inum = MFS_ROOT_INO;
[14c29ba]323 memcpy(dentry->d_name, "..\0", 3);
[fd282ad]324 }
325
[f66d903]326 rc = write_block(root_dblock, 1, root_block);
[fd282ad]327
328 free(root_block);
[0b07acd]329 return rc;
[fd282ad]330}
331
[197b671]332static int init_inode_table(const struct mfs_sb_info *sb)
[410a065]333{
334 unsigned int i;
335 uint8_t *itable_buf;
[0b07acd]336 int rc;
[340b5690]337
[f66d903]338 long itable_off = sb->zbmap_blocks + sb->ibmap_blocks + 2;
339 unsigned long itable_size = sb->itable_size;
[340b5690]340
[f66d903]341 itable_buf = malloc(sb->block_size);
[0b07acd]342
343 if (!itable_buf)
344 return ENOMEM;
345
[f66d903]346 memset(itable_buf, 0x00, sb->block_size);
[410a065]347
[8ceba1e]348 for (i = 0; i < itable_size; ++i, ++itable_off) {
[f66d903]349 rc = write_block(itable_off, 1, itable_buf);
[0b07acd]350
351 if (rc != EOK)
352 break;
353 }
[410a065]354
355 free(itable_buf);
[0b07acd]356 return rc;
[410a065]357}
358
[197b671]359static int make_root_ino(const struct mfs_sb_info *sb)
[410a065]360{
361 struct mfs_inode *ino_buf;
[0b07acd]362 int rc;
[410a065]363
[f66d903]364 const long itable_off = sb->zbmap_blocks + sb->ibmap_blocks + 2;
[340b5690]365
[4727a97a]366 const time_t sec = time(NULL);
367
368 ino_buf = (struct mfs_inode *) malloc(MFS_BLOCKSIZE);
[0b07acd]369
370 if (!ino_buf)
371 return ENOMEM;
372
[4727a97a]373 memset(ino_buf, 0x00, MFS_BLOCKSIZE);
[410a065]374
375 ino_buf[MFS_ROOT_INO].i_mode = S_IFDIR;
376 ino_buf[MFS_ROOT_INO].i_uid = 0;
377 ino_buf[MFS_ROOT_INO].i_gid = 0;
378 ino_buf[MFS_ROOT_INO].i_size = (sb->longnames ? MFSL_DIRSIZE : MFS_DIRSIZE) * 2;
[4727a97a]379 ino_buf[MFS_ROOT_INO].i_mtime = sec;
[410a065]380 ino_buf[MFS_ROOT_INO].i_nlinks = 2;
381 ino_buf[MFS_ROOT_INO].i_dzone[0] = sb->first_data_zone;
382
[f66d903]383 rc = write_block(itable_off, 1, ino_buf);
[410a065]384
385 free(ino_buf);
[0b07acd]386 return rc;
[410a065]387}
388
[197b671]389static int make_root_ino3(const struct mfs_sb_info *sb)
[410a065]390{
391 struct mfs2_inode *ino_buf;
[0b07acd]392 int rc;
[410a065]393
[c4c7f5a]394 /*Compute offset of the first inode table block*/
[f66d903]395 const long itable_off = sb->zbmap_blocks + sb->ibmap_blocks + 2;
[340b5690]396
[4727a97a]397 const time_t sec = time(NULL);
398
[f66d903]399 ino_buf = (struct mfs2_inode *) malloc(sb->block_size);
[0b07acd]400
401 if (!ino_buf)
402 return ENOMEM;
403
[f66d903]404 memset(ino_buf, 0x00, sb->block_size);
[410a065]405
406 ino_buf[MFS_ROOT_INO].i_mode = S_IFDIR;
407 ino_buf[MFS_ROOT_INO].i_uid = 0;
408 ino_buf[MFS_ROOT_INO].i_gid = 0;
409 ino_buf[MFS_ROOT_INO].i_size = MFS3_DIRSIZE * 2;
[4727a97a]410 ino_buf[MFS_ROOT_INO].i_mtime = sec;
411 ino_buf[MFS_ROOT_INO].i_atime = sec;
412 ino_buf[MFS_ROOT_INO].i_ctime = sec;
[410a065]413 ino_buf[MFS_ROOT_INO].i_nlinks = 2;
414 ino_buf[MFS_ROOT_INO].i_dzone[0] = sb->first_data_zone;
415
[f66d903]416 rc = write_block(itable_off, 1, ino_buf);
[410a065]417
418 free(ino_buf);
[0b07acd]419 return rc;
[410a065]420}
421
[0b07acd]422static int init_superblock(struct mfs_sb_info *sb)
[eee8007]423{
[e2267e82]424 aoff64_t inodes;
[0b07acd]425 int rc;
[68ed0fb]426
[59e670e]427 if (sb->longnames)
428 sb->magic = sb->fs_version == 1 ? MFS_MAGIC_V1L : MFS_MAGIC_V2L;
[939b7d2]429
[e03a733]430 /*Compute the number of zones on disk*/
431
[59e670e]432 if (sb->fs_version == 1) {
433 /*Valid only for MFS V1*/
434 sb->n_zones = sb->dev_nblocks > UINT16_MAX ?
435 UINT16_MAX : sb->dev_nblocks;
436 } else {
437 /*Valid for MFS V2/V3*/
438 sb->n_zones = sb->dev_nblocks > UINT32_MAX ?
439 UINT32_MAX : sb->dev_nblocks;
[19a057f9]440
[59e670e]441 if (sb->fs_version == 3) {
442 sb->ino_per_block = V3_INODES_PER_BLOCK(sb->block_size);
443 sb->n_zones /= (sb->block_size / MFS_MIN_BLOCKSIZE);
444 }
445 }
[19a057f9]446
[e03a733]447 /*Round up the number of inodes to fill block size*/
[59e670e]448 if (sb->n_inodes == 0)
449 inodes = sb->dev_nblocks / 3;
[c64506b]450 else
451 inodes = sb->n_inodes;
[68ed0fb]452
[59e670e]453 if (inodes % sb->ino_per_block)
454 inodes = ((inodes / sb->ino_per_block) + 1) * sb->ino_per_block;
455
456 if (sb->fs_version < 3)
457 sb->n_inodes = inodes > UINT16_MAX ? UINT16_MAX : inodes;
458 else
459 sb->n_inodes = inodes > UINT32_MAX ? UINT32_MAX : inodes;
[68ed0fb]460
461 /*Compute inode bitmap size in blocks*/
[59e670e]462 sb->ibmap_blocks = UPPER(sb->n_inodes, sb->block_size * 8);
[68ed0fb]463
[e03a733]464 /*Compute inode table size*/
[410a065]465 sb->itable_size = sb->n_inodes / sb->ino_per_block;
[e03a733]466
[340b5690]467 /*Compute zone bitmap size in blocks*/
468 sb->zbmap_blocks = UPPER(sb->n_zones, sb->block_size * 8);
469
[68ed0fb]470 /*Compute first data zone position*/
[92dd5c8]471 sb->first_data_zone = 2 + sb->itable_size +
[59e670e]472 sb->zbmap_blocks + sb->ibmap_blocks;
[ae1ae27]473
474 /*Set log2 of zone to block ratio to zero*/
[59e670e]475 sb->log2_zone_size = 0;
[ae1ae27]476
[0b07acd]477 /*Check for errors*/
478 if (sb->first_data_zone >= sb->n_zones) {
479 printf(NAME ": Error! Insufficient disk space");
480 return ENOMEM;
481 }
482
[68ed0fb]483 /*Superblock is now ready to be written on disk*/
[59e670e]484 printf(NAME ": %d inodes\n", (uint32_t) sb->n_inodes);
485 printf(NAME ": %d zones\n", (uint32_t) sb->n_zones);
[410a065]486 printf(NAME ": inode table blocks = %ld\n", sb->itable_size);
487 printf(NAME ": inode bitmap blocks = %ld\n", sb->ibmap_blocks);
488 printf(NAME ": zone bitmap blocks = %ld\n", sb->zbmap_blocks);
[59e670e]489 printf(NAME ": first data zone = %d\n", (uint32_t) sb->first_data_zone);
490 printf(NAME ": long fnames = %s\n", sb->longnames ? "Yes" : "No");
[bc99ed6]491
[59e670e]492 if (sb->fs_version == 3)
[0b07acd]493 rc = write_superblock3(sb);
[59e670e]494 else
[0b07acd]495 rc = write_superblock(sb);
496
497 return rc;
[eee8007]498}
499
[197b671]500static int write_superblock(const struct mfs_sb_info *sbi)
[eee8007]501{
[59e670e]502 struct mfs_superblock *sb;
[0b07acd]503 int rc;
[e03a733]504
[8e41994]505 sb = (struct mfs_superblock *) malloc(MFS_SUPERBLOCK_SIZE);;
[e03a733]506
[0b07acd]507 if (!sb)
508 return ENOMEM;
509
[59e670e]510 sb->s_ninodes = (uint16_t) sbi->n_inodes;
511 sb->s_nzones = (uint16_t) sbi->n_zones;
512 sb->s_nzones2 = (uint32_t) sbi->n_zones;
513 sb->s_ibmap_blocks = (uint16_t) sbi->ibmap_blocks;
514 sb->s_zbmap_blocks = (uint16_t) sbi->zbmap_blocks;
515 sb->s_first_data_zone = (uint16_t) sbi->first_data_zone;
516 sb->s_log2_zone_size = sbi->log2_zone_size;
517 sb->s_max_file_size = UINT32_MAX;
518 sb->s_magic = sbi->magic;
519 sb->s_state = MFS_VALID_FS;
[e03a733]520
[f66d903]521 rc = write_block(MFS_SUPERBLOCK, 1, sb);
[8e41994]522 free(sb);
[0b07acd]523
524 return rc;
[59e670e]525}
[e03a733]526
[197b671]527static int write_superblock3(const struct mfs_sb_info *sbi)
[59e670e]528{
529 struct mfs3_superblock *sb;
[0b07acd]530 int rc;
[59e670e]531
[410a065]532 sb = (struct mfs3_superblock *) malloc(MFS_SUPERBLOCK_SIZE);
[59e670e]533
[0b07acd]534 if (!sb)
535 return ENOMEM;
536
[59e670e]537 sb->s_ninodes = (uint32_t) sbi->n_inodes;
538 sb->s_nzones = (uint32_t) sbi->n_zones;
539 sb->s_ibmap_blocks = (uint16_t) sbi->ibmap_blocks;
540 sb->s_zbmap_blocks = (uint16_t) sbi->zbmap_blocks;
541 sb->s_first_data_zone = (uint16_t) sbi->first_data_zone;
542 sb->s_log2_zone_size = sbi->log2_zone_size;
543 sb->s_max_file_size = UINT32_MAX;
544 sb->s_magic = sbi->magic;
545 sb->s_block_size = sbi->block_size;
[e03a733]546 sb->s_disk_version = 3;
[bc99ed6]547
[f66d903]548 rc = block_write_direct(handle, MFS_SUPERBLOCK << 1, 1 << 1, sb);
[410a065]549 free(sb);
[0b07acd]550
551 return rc;
[eee8007]552}
553
[197b671]554static int init_bitmaps(const struct mfs_sb_info *sb)
[e6aaa59]555{
[bb0db564]556 uint32_t *ibmap_buf, *zbmap_buf;
557 uint8_t *ibmap_buf8, *zbmap_buf8;
[f66d903]558 const unsigned int ibmap_nblocks = sb->ibmap_blocks;
559 const unsigned int zbmap_nblocks = sb->zbmap_blocks;
[e6aaa59]560 unsigned int i;
[0b07acd]561 int rc;
[e6aaa59]562
[bb0db564]563 ibmap_buf = (uint32_t *) malloc(ibmap_nblocks * sb->block_size);
564 zbmap_buf = (uint32_t *) malloc(zbmap_nblocks * sb->block_size);
[e6aaa59]565
[0b07acd]566 if (!ibmap_buf || !zbmap_buf)
567 return ENOMEM;
568
[59e670e]569 memset(ibmap_buf, 0xFF, ibmap_nblocks * sb->block_size);
570 memset(zbmap_buf, 0xFF, zbmap_nblocks * sb->block_size);
[e6aaa59]571
[59e670e]572 for (i = 2; i < sb->n_inodes; ++i)
[bb0db564]573 mark_bmap(ibmap_buf, i, FREE);
[f5cbd4f]574
[03fa85c]575 for (i = sb->first_data_zone + 1; i < sb->n_zones; ++i)
[bb0db564]576 mark_bmap(zbmap_buf, i, FREE);
[e6aaa59]577
[bb0db564]578 /*Convert to 1K block offsets*/
[9cfe0d5]579
[bb0db564]580 ibmap_buf8 = (uint8_t *) ibmap_buf;
581 zbmap_buf8 = (uint8_t *) zbmap_buf;
582
[f66d903]583 int start_block = 2;
[8ceba1e]584
[7df022e5]585 for (i = 0; i < ibmap_nblocks; ++i) {
[f66d903]586 if ((rc = write_block(start_block + i,
587 1, (ibmap_buf8 + i * sb->block_size))) != EOK)
[7df022e5]588 return rc;
589 }
590
[f66d903]591 start_block = 2 + ibmap_nblocks;
[8ceba1e]592
[7df022e5]593 for (i = 0; i < zbmap_nblocks; ++i) {
[f66d903]594 if ((rc = write_block(start_block + i,
595 1, (zbmap_buf8 + i * sb->block_size))) != EOK)
[7df022e5]596 return rc;
597 }
598
[c4c7f5a]599 free(ibmap_buf);
600 free(zbmap_buf);
601
[0b07acd]602 return rc;
[e6aaa59]603}
604
[59e670e]605static void mark_bmap(uint32_t *bmap, int idx, int v)
[e6aaa59]606{
607 if (v == FREE)
[59e670e]608 bmap[idx / 32] &= ~(1 << (idx % 32));
[e6aaa59]609 else
[59e670e]610 bmap[idx / 32] |= 1 << (idx % 32);
[e6aaa59]611}
612
[f66d903]613static inline int write_block(aoff64_t off, size_t size, const void *data)
[b84175a]614{
[f66d903]615 if (shift == 3) {
616 int rc;
617 aoff64_t tmp_off = off << 1;
618 uint8_t *data_ptr = (uint8_t *) data;
619
620 rc = block_write_direct(handle, tmp_off << 2, size << 2, data_ptr);
621
622 if (rc != EOK)
623 return rc;
624
625 data_ptr += 2048;
626 tmp_off++;
627
628 return block_write_direct(handle, tmp_off << 2, size << 2, data_ptr);
629 }
630 return block_write_direct(handle, off << shift, size << shift, data);
[b84175a]631}
632
[954bf385]633static void help_cmd_mkminix(help_level_t level)
634{
635 if (level == HELP_SHORT) {
636 printf(NAME": tool to create new Minix file systems\n");
637 } else {
638 printf("Usage: [options] device\n"
639 "-1 Make a Minix version 1 filesystem\n"
640 "-2 Make a Minix version 2 filesystem\n"
641 "-3 Make a Minix version 3 filesystem\n"
642 "-b ## Specify the block size in bytes (V3 only),\n"
[eaf9794]643 " valid block size values are 1024, 2048 and 4096 bytes per block\n"
[eee8007]644 "-i ## Specify the number of inodes for the filesystem\n"
[82650385]645 "-l Use 30-char long filenames (V1/V2 only)\n");
[954bf385]646 }
647}
648
649static int num_of_set_bits(uint32_t n)
650{
651 n = n - ((n >> 1) & 0x55555555);
652 n = (n & 0x33333333) + ((n >> 2) & 0x33333333);
653 return (((n + (n >> 4)) & 0xF0F0F0F) * 0x1010101) >> 24;
654}
655
656
657/**
658 * @}
659 */
Note: See TracBrowser for help on using the repository browser.