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

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

merge mainline changes

  • 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 mkmfs.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 "mkmfs"
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_mkmfs(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 service_id_t service_id;
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 size_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_mkmfs(HELP_SHORT);
133 printf("Incorrect number of arguments, try `mkmfs --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_mkmfs(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_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(EXCHANGE_SERIALIZE, 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 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 block_fini(service_id);
275
276 return 0;
277}
278
279/**Inserts the '.' and '..' directory entries in the root directory.
280 *
281 * @param sb Pointer to the superblock structure.
282 *
283 * @return EOK on success or a negative error code.
284 */
285static int insert_dentries(const struct mfs_sb_info *sb)
286{
287 void *root_block;
288 uint8_t *dentry_ptr;
289 int rc;
290 const long root_dblock = sb->first_data_zone;
291
292 root_block = malloc(sb->block_size);
293 memset(root_block, 0x00, sb->block_size);
294
295 if (!root_block)
296 return ENOMEM;
297
298 dentry_ptr = root_block;
299
300 if (sb->fs_version != 3) {
301 /*Directory entries for V1/V2 filesystem*/
302 struct mfs_dentry *dentry = root_block;
303
304 dentry->d_inum = MFS_ROOT_INO;
305 memcpy(dentry->d_name, ".\0", 2);
306
307 dentry = (struct mfs_dentry *) NEXT_DENTRY(dentry_ptr,
308 sb->dirsize);
309
310 dentry->d_inum = MFS_ROOT_INO;
311 memcpy(dentry->d_name, "..\0", 3);
312 } else {
313 /*Directory entries for V3 filesystem*/
314 struct mfs3_dentry *dentry = root_block;
315
316 dentry->d_inum = MFS_ROOT_INO;
317 memcpy(dentry->d_name, ".\0", 2);
318
319 dentry = (struct mfs3_dentry *) NEXT_DENTRY(dentry_ptr,
320 sb->dirsize);
321
322 dentry->d_inum = MFS_ROOT_INO;
323 memcpy(dentry->d_name, "..\0", 3);
324 }
325
326 rc = write_block(root_dblock, 1, root_block);
327
328 free(root_block);
329 return rc;
330}
331
332/**Initialize the inode table.
333 *
334 * @param sb Pointer to the superblock structure.
335 *
336 * @return EOK on success or a negative error code.
337 */
338static int init_inode_table(const struct mfs_sb_info *sb)
339{
340 unsigned int i;
341 uint8_t *itable_buf;
342 int rc;
343
344 long itable_off = sb->zbmap_blocks + sb->ibmap_blocks + 2;
345 unsigned long itable_size = sb->itable_size;
346
347 itable_buf = malloc(sb->block_size);
348
349 if (!itable_buf)
350 return ENOMEM;
351
352 memset(itable_buf, 0x00, sb->block_size);
353
354 for (i = 0; i < itable_size; ++i, ++itable_off) {
355 rc = write_block(itable_off, 1, itable_buf);
356
357 if (rc != EOK)
358 break;
359 }
360
361 free(itable_buf);
362 return rc;
363}
364
365/**Initialize a V1 root inode.
366 *
367 * @param sb Ponter to the superblock structure.
368 *
369 * @return EOK on success or a negative error code.
370 */
371static int make_root_ino(const struct mfs_sb_info *sb)
372{
373 struct mfs_inode *ino_buf;
374 int rc;
375
376 const long itable_off = sb->zbmap_blocks + sb->ibmap_blocks + 2;
377
378 const time_t sec = time(NULL);
379
380 ino_buf = malloc(MFS_BLOCKSIZE);
381
382 if (!ino_buf)
383 return ENOMEM;
384
385 memset(ino_buf, 0x00, MFS_BLOCKSIZE);
386
387 ino_buf[MFS_ROOT_INO - 1].i_mode = S_IFDIR;
388 ino_buf[MFS_ROOT_INO - 1].i_uid = 0;
389 ino_buf[MFS_ROOT_INO - 1].i_gid = 0;
390 ino_buf[MFS_ROOT_INO - 1].i_size = (sb->longnames ? MFSL_DIRSIZE :
391 MFS_DIRSIZE) * 2;
392 ino_buf[MFS_ROOT_INO - 1].i_mtime = sec;
393 ino_buf[MFS_ROOT_INO - 1].i_nlinks = 2;
394 ino_buf[MFS_ROOT_INO - 1].i_dzone[0] = sb->first_data_zone;
395
396 rc = write_block(itable_off, 1, ino_buf);
397
398 free(ino_buf);
399 return rc;
400}
401
402/**Initialize a Minix V2 root inode on disk, also valid for V3 filesystem.
403 *
404 * @param sb Pointer to the superblock structure.
405 *
406 * @return EOK on success or a negative error code.
407 */
408static int make_root_ino2(const struct mfs_sb_info *sb)
409{
410 struct mfs2_inode *ino_buf;
411 int rc;
412
413 /*Compute offset of the first inode table block*/
414 const long itable_off = sb->zbmap_blocks + sb->ibmap_blocks + 2;
415
416 const time_t sec = time(NULL);
417
418 ino_buf = malloc(sb->block_size);
419
420 if (!ino_buf)
421 return ENOMEM;
422
423 memset(ino_buf, 0x00, sb->block_size);
424
425 ino_buf[MFS_ROOT_INO - 1].i_mode = S_IFDIR;
426 ino_buf[MFS_ROOT_INO - 1].i_uid = 0;
427 ino_buf[MFS_ROOT_INO - 1].i_gid = 0;
428 ino_buf[MFS_ROOT_INO - 1].i_size = MFS3_DIRSIZE * 2;
429 ino_buf[MFS_ROOT_INO - 1].i_mtime = sec;
430 ino_buf[MFS_ROOT_INO - 1].i_atime = sec;
431 ino_buf[MFS_ROOT_INO - 1].i_ctime = sec;
432 ino_buf[MFS_ROOT_INO - 1].i_nlinks = 2;
433 ino_buf[MFS_ROOT_INO - 1].i_dzone[0] = sb->first_data_zone;
434
435 rc = write_block(itable_off, 1, ino_buf);
436
437 free(ino_buf);
438 return rc;
439}
440
441/**Initialize the superblock structure on disk.
442 *
443 * @param sb Pointer to the superblock structure.
444 *
445 * @return EOK on success or a negative error code.
446 */
447static int init_superblock(struct mfs_sb_info *sb)
448{
449 aoff64_t inodes;
450 unsigned long ind;
451 unsigned long ind2;
452 unsigned long zones;
453 int rc;
454
455 if (sb->longnames)
456 sb->magic = sb->fs_version == 1 ? MFS_MAGIC_V1L : MFS_MAGIC_V2L;
457
458 /*Compute the number of zones on disk*/
459
460 if (sb->fs_version == 1) {
461 /*Valid only for MFS V1*/
462 sb->n_zones = sb->dev_nblocks > UINT16_MAX ?
463 UINT16_MAX : sb->dev_nblocks;
464 ind = MFS_BLOCKSIZE / sizeof(uint16_t);
465 ind2 = ind * ind;
466 sb->max_file_size = (V1_NR_DIRECT_ZONES + ind + ind2) * MFS_BLOCKSIZE;
467 } else {
468 /*Valid for MFS V2/V3*/
469 size_t ptrsize;
470 if (sb->fs_version == 2)
471 ptrsize = sizeof(uint16_t);
472 else
473 ptrsize = sizeof(uint32_t);
474 ind = sb->block_size / ptrsize;
475 ind2 = ind * ind;
476 zones = V2_NR_DIRECT_ZONES + ind + ind2;
477 sb->max_file_size = zones * sb->block_size;
478 sb->n_zones = sb->dev_nblocks > UINT32_MAX ?
479 UINT32_MAX : sb->dev_nblocks;
480
481 if (sb->fs_version == 3) {
482 if(INT32_MAX / sb->block_size < zones)
483 sb->max_file_size = INT32_MAX;
484 sb->ino_per_block = V3_INODES_PER_BLOCK(sb->block_size);
485 sb->n_zones /= (sb->block_size / MFS_MIN_BLOCKSIZE);
486 }
487 }
488
489 /*Round up the number of inodes to fill block size*/
490 if (sb->n_inodes == 0)
491 inodes = sb->dev_nblocks / 3;
492 else
493 inodes = sb->n_inodes;
494
495 if (inodes % sb->ino_per_block)
496 inodes = ((inodes / sb->ino_per_block) + 1) * sb->ino_per_block;
497
498 if (sb->fs_version < 3)
499 sb->n_inodes = inodes > UINT16_MAX ? UINT16_MAX : inodes;
500 else
501 sb->n_inodes = inodes > UINT32_MAX ? UINT32_MAX : inodes;
502
503 /*Compute inode bitmap size in blocks*/
504 sb->ibmap_blocks = UPPER(sb->n_inodes, sb->block_size * 8);
505
506 /*Compute inode table size*/
507 sb->itable_size = sb->n_inodes / sb->ino_per_block;
508
509 /*Compute zone bitmap size in blocks*/
510 sb->zbmap_blocks = UPPER(sb->n_zones, sb->block_size * 8);
511
512 /*Compute first data zone position*/
513 sb->first_data_zone = 2 + sb->itable_size +
514 sb->zbmap_blocks + sb->ibmap_blocks;
515
516 /*Set log2 of zone to block ratio to zero*/
517 sb->log2_zone_size = 0;
518
519 /*Check for errors*/
520 if (sb->first_data_zone >= sb->n_zones) {
521 printf(NAME ": Error! Insufficient disk space");
522 return ENOMEM;
523 }
524
525 /*Superblock is now ready to be written on disk*/
526 printf(NAME ": %d block size\n", sb->block_size);
527 printf(NAME ": %d inodes\n", (uint32_t) sb->n_inodes);
528 printf(NAME ": %d zones\n", (uint32_t) sb->n_zones);
529 printf(NAME ": inode table blocks = %ld\n", sb->itable_size);
530 printf(NAME ": inode bitmap blocks = %ld\n", sb->ibmap_blocks);
531 printf(NAME ": zone bitmap blocks = %ld\n", sb->zbmap_blocks);
532 printf(NAME ": first data zone = %d\n", (uint32_t) sb->first_data_zone);
533 printf(NAME ": max file size = %u\n", sb->max_file_size);
534 printf(NAME ": long fnames = %s\n", sb->longnames ? "Yes" : "No");
535
536 if (sb->fs_version == 3)
537 rc = write_superblock3(sb);
538 else
539 rc = write_superblock(sb);
540
541 return rc;
542}
543
544/**Write the V1/V2 superblock on disk.
545 *
546 * @param sbi Pointer to the superblock structure to write on disk.
547 *
548 * @return EOK on success or a negative error code.
549 */
550static int write_superblock(const struct mfs_sb_info *sbi)
551{
552 struct mfs_superblock *sb;
553 int rc;
554
555 sb = malloc(MFS_SUPERBLOCK_SIZE);;
556
557 if (!sb)
558 return ENOMEM;
559
560 sb->s_ninodes = (uint16_t) sbi->n_inodes;
561 sb->s_nzones = (uint16_t) sbi->n_zones;
562 sb->s_nzones2 = (uint32_t) sbi->n_zones;
563 sb->s_ibmap_blocks = (uint16_t) sbi->ibmap_blocks;
564 sb->s_zbmap_blocks = (uint16_t) sbi->zbmap_blocks;
565 sb->s_first_data_zone = (uint16_t) sbi->first_data_zone;
566 sb->s_log2_zone_size = sbi->log2_zone_size;
567 sb->s_max_file_size = sbi->max_file_size;
568 sb->s_magic = sbi->magic;
569 sb->s_state = MFS_VALID_FS;
570
571 rc = write_block(MFS_SUPERBLOCK, 1, sb);
572 free(sb);
573
574 return rc;
575}
576
577/**Write the V3s superblock on disk.
578 *
579 * @param sbi Pointer to the superblock structure to write on disk.
580 *
581 * @return EOK on success or a negative error code.
582 */
583static int write_superblock3(const struct mfs_sb_info *sbi)
584{
585 struct mfs3_superblock *sb;
586 int rc;
587
588 sb = malloc(MFS_SUPERBLOCK_SIZE);
589
590 if (!sb)
591 return ENOMEM;
592
593 sb->s_ninodes = (uint32_t) sbi->n_inodes;
594 sb->s_nzones = (uint32_t) sbi->n_zones;
595 sb->s_ibmap_blocks = (uint16_t) sbi->ibmap_blocks;
596 sb->s_zbmap_blocks = (uint16_t) sbi->zbmap_blocks;
597 sb->s_first_data_zone = (uint16_t) sbi->first_data_zone;
598 sb->s_log2_zone_size = sbi->log2_zone_size;
599 sb->s_max_file_size = sbi->max_file_size;
600 sb->s_magic = sbi->magic;
601 sb->s_block_size = sbi->block_size;
602 sb->s_disk_version = 3;
603
604 rc = block_write_direct(service_id, MFS_SUPERBLOCK << 1, 1 << 1, sb);
605 free(sb);
606
607 return rc;
608}
609
610/**Initialize the inode and block bitmaps on disk.
611 *
612 * @param sb Pointer to the superblock structure.
613 *
614 * @return EOK on success or a negative error code.
615 */
616static int init_bitmaps(const struct mfs_sb_info *sb)
617{
618 uint32_t *ibmap_buf, *zbmap_buf;
619 uint8_t *ibmap_buf8, *zbmap_buf8;
620 const unsigned int ibmap_nblocks = sb->ibmap_blocks;
621 const unsigned int zbmap_nblocks = sb->zbmap_blocks;
622 unsigned int i;
623 int rc;
624
625 ibmap_buf = malloc(ibmap_nblocks * sb->block_size);
626 zbmap_buf = malloc(zbmap_nblocks * sb->block_size);
627
628 if (!ibmap_buf || !zbmap_buf)
629 return ENOMEM;
630
631 memset(ibmap_buf, 0xFF, ibmap_nblocks * sb->block_size);
632 memset(zbmap_buf, 0xFF, zbmap_nblocks * sb->block_size);
633
634 for (i = 2; i < sb->n_inodes + 1; ++i)
635 mark_bmap(ibmap_buf, i, FREE);
636
637 for (i = sb->first_data_zone + 1; i < sb->n_zones; ++i)
638 mark_bmap(zbmap_buf, i, FREE);
639
640 ibmap_buf8 = (uint8_t *) ibmap_buf;
641 zbmap_buf8 = (uint8_t *) zbmap_buf;
642
643 int start_block = 2;
644
645 for (i = 0; i < ibmap_nblocks; ++i) {
646 if ((rc = write_block(start_block + i,
647 1, (ibmap_buf8 + i * sb->block_size))) != EOK)
648 return rc;
649 }
650
651 start_block = 2 + ibmap_nblocks;
652
653 for (i = 0; i < zbmap_nblocks; ++i) {
654 if ((rc = write_block(start_block + i,
655 1, (zbmap_buf8 + i * sb->block_size))) != EOK)
656 return rc;
657 }
658
659 free(ibmap_buf);
660 free(zbmap_buf);
661
662 return rc;
663}
664
665/**Mark a bitmap entry as used or free.
666 *
667 * @param bmap 32-bit pointer to the bitmap in memory.
668 * @param idx The index in the bitmap of the bit to set at 1 or 0.
669 * @param v FREE to clear the bit, USED to set the bit.
670 */
671static void mark_bmap(uint32_t *bmap, int idx, int v)
672{
673 if (v == FREE)
674 bmap[idx / 32] &= ~(1 << (idx % 32));
675 else
676 bmap[idx / 32] |= 1 << (idx % 32);
677}
678
679/**Write a block on disk.
680 *
681 * @param off 64-bit block offset on disk.
682 * @param size size of the block.
683 * @param data Pointer to the block content.
684 *
685 * @return EOK on success or a negative error number.
686 */
687static inline int write_block(aoff64_t off, size_t size, const void *data)
688{
689 if (shift == 3) {
690 int rc;
691 aoff64_t tmp_off = off << 1;
692 uint8_t *data_ptr = (uint8_t *) data;
693
694 rc = block_write_direct(service_id, tmp_off << 2, size << 2, data_ptr);
695
696 if (rc != EOK)
697 return rc;
698
699 data_ptr += 2048;
700 tmp_off++;
701
702 return block_write_direct(service_id, tmp_off << 2, size << 2, data_ptr);
703 }
704 return block_write_direct(service_id, off << shift, size << shift, data);
705}
706
707static void help_cmd_mkmfs(help_level_t level)
708{
709 if (level == HELP_SHORT) {
710 printf(NAME": tool to create new Minix file systems\n");
711 } else {
712 printf("Usage: [options] device\n"
713 "-1 Make a Minix version 1 filesystem\n"
714 "-2 Make a Minix version 2 filesystem\n"
715 "-b ## Specify the block size in bytes (V3 only),\n"
716 " valid block size values are 1024, 2048 and 4096 bytes per block\n"
717 "-i ## Specify the number of inodes for the filesystem\n"
718 "-l Use 30-char long filenames (V1/V2 only)\n");
719 }
720}
721
722static int num_of_set_bits(uint32_t n)
723{
724 n = n - ((n >> 1) & 0x55555555);
725 n = (n & 0x33333333) + ((n >> 2) & 0x33333333);
726 return (((n + (n >> 4)) & 0xF0F0F0F) * 0x1010101) >> 24;
727}
728
729
730/**
731 * @}
732 */
Note: See TracBrowser for help on using the repository browser.