source: mainline/uspace/app/mkminix/mkminix.c@ 1affcdf3

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

Merge mainline changes

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