source: mainline/uspace/app/mkmfs/mkmfs.c@ 0464967

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 0464967 was 0464967, checked in by jzr <zarevucky.jiri@…>, 8 years ago

Fix gcc warnings when building with -O1 or -Og flags.
(Thanks Ondřej Hlavatý!)

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