source: mainline/uspace/app/mkminix/mkminix.c@ 99f043e

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

Compute the maximum size of each inode

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