source: mainline/uspace/app/mkmfs/mkmfs.c@ 32ec5671

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 32ec5671 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
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/**
[7a46bfe]35 * @file mkmfs.c
[954bf385]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
[7a46bfe]53#define NAME "mkmfs"
[954bf385]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
[7a46bfe]85static void help_cmd_mkmfs(help_level_t level);
[eee8007]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
[03bc76a]99static service_id_t service_id;
[f66d903]100static int shift;
[954bf385]101
102static struct option const long_options[] = {
[e91b898]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 }
[954bf385]110};
111
112int main (int argc, char **argv)
113{
114 int rc, c, opt_ind;
115 char *device_name;
[16cc9a6]116 size_t devblock_size;
[954bf385]117
[59e670e]118 struct mfs_sb_info sb;
[954bf385]119
[eee8007]120 /*Default is MinixFS V3*/
[59e670e]121 sb.magic = MFS_MAGIC_V3;
[6cda025]122 sb.fs_version = 3;
[eee8007]123
124 /*Default block size is 4Kb*/
[59e670e]125 sb.block_size = MFS_MAX_BLOCKSIZE;
[c4c7f5a]126 sb.dirsize = MFS3_DIRSIZE;
[59e670e]127 sb.n_inodes = 0;
128 sb.longnames = false;
129 sb.ino_per_block = V3_INODES_PER_BLOCK(MFS_MAX_BLOCKSIZE);
[eaf9794]130
[954bf385]131 if (argc == 1) {
[7a46bfe]132 help_cmd_mkmfs(HELP_SHORT);
133 printf("Incorrect number of arguments, try `mkmfs --help'\n");
[954bf385]134 exit(0);
135 }
136
137 for (c = 0, optind = 0, opt_ind = 0; c != -1;) {
[2cada66]138 c = getopt_long(argc, argv, "lh12b:i:", long_options, &opt_ind);
[954bf385]139 switch (c) {
140 case 'h':
[7a46bfe]141 help_cmd_mkmfs(HELP_LONG);
[954bf385]142 exit(0);
143 case '1':
[59e670e]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;
[fd282ad]148 sb.dirsize = MFS_DIRSIZE;
[954bf385]149 break;
150 case '2':
[59e670e]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;
[fd282ad]155 sb.dirsize = MFS_DIRSIZE;
[954bf385]156 break;
157 case 'b':
[59e670e]158 sb.block_size = (uint32_t) strtol(optarg, NULL, 10);
[954bf385]159 break;
160 case 'i':
[c64506b]161 sb.n_inodes = (uint64_t) strtol(optarg, NULL, 10);
[eee8007]162 break;
163 case 'l':
[59e670e]164 sb.longnames = true;
[fd282ad]165 sb.dirsize = MFSL_DIRSIZE;
[954bf385]166 break;
167 }
168 }
169
[59e670e]170 if (sb.block_size < MFS_MIN_BLOCKSIZE ||
[e91b898]171 sb.block_size > MFS_MAX_BLOCKSIZE) {
[954bf385]172 printf(NAME ":Error! Invalid block size.\n");
173 exit(0);
[59e670e]174 } else if (num_of_set_bits(sb.block_size) != 1) {
[eaf9794]175 /*Block size must be a power of 2.*/
[954bf385]176 printf(NAME ":Error! Invalid block size.\n");
177 exit(0);
[59e670e]178 } else if (sb.block_size > MFS_BLOCKSIZE &&
179 sb.fs_version != 3) {
[eee8007]180 printf(NAME ":Error! Block size > 1024 is supported by V3 filesystem only.\n");
181 exit(0);
[59e670e]182 } else if (sb.fs_version == 3 && sb.longnames) {
[eee8007]183 printf(NAME ":Error! Long filenames are supported by V1/V2 filesystem only.\n");
[954bf385]184 exit(0);
185 }
186
[f66d903]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
[954bf385]194 argv += optind;
195
196 device_name = argv[0];
197
198 if (!device_name) {
[7a46bfe]199 help_cmd_mkmfs(HELP_LONG);
[954bf385]200 exit(0);
201 }
202
[03bc76a]203 rc = loc_service_get_id(device_name, &service_id, 0);
[954bf385]204 if (rc != EOK) {
205 printf(NAME ": Error resolving device `%s'.\n", device_name);
206 return 2;
207 }
208
[03bc76a]209 rc = block_init(EXCHANGE_SERIALIZE, service_id, 2048);
[954bf385]210 if (rc != EOK) {
211 printf(NAME ": Error initializing libblock.\n");
212 return 2;
213 }
214
[03bc76a]215 rc = block_get_bsize(service_id, &devblock_size);
[954bf385]216 if (rc != EOK) {
217 printf(NAME ": Error determining device block size.\n");
218 return 2;
219 }
220
[03bc76a]221 rc = block_get_nblocks(service_id, &sb.dev_nblocks);
[954bf385]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",
[e91b898]226 sb.dev_nblocks);
[954bf385]227 }
228
[197b671]229 if (devblock_size != 512) {
[954bf385]230 printf(NAME ": Error. Device block size is not 512 bytes.\n");
231 return 2;
232 }
233
[68ed0fb]234 /*Minimum block size is 1 Kb*/
[59e670e]235 sb.dev_nblocks /= 2;
[68ed0fb]236
237 printf(NAME ": Creating Minix file system on device\n");
[954bf385]238
[59e670e]239 /*Initialize superblock*/
[0b07acd]240 if (init_superblock(&sb) != EOK) {
241 printf(NAME ": Error. Superblock initialization failed\n");
242 return 2;
243 }
[eaf9794]244
[59e670e]245 /*Initialize bitmaps*/
[0b07acd]246 if (init_bitmaps(&sb) != EOK) {
247 printf(NAME ": Error. Bitmaps initialization failed\n");
248 return 2;
249 }
[eee8007]250
[410a065]251 /*Init inode table*/
[0b07acd]252 if (init_inode_table(&sb) != EOK) {
253 printf(NAME ": Error. Inode table initialization failed\n");
254 return 2;
255 }
[410a065]256
[f0ceb1d]257 /*Make the root inode*/
[bed0356]258 if (sb.fs_version == 1)
[0b07acd]259 rc = make_root_ino(&sb);
[bed0356]260 else
261 rc = make_root_ino2(&sb);
[0b07acd]262
263 if (rc != EOK) {
264 printf(NAME ": Error. Root inode initialization failed\n");
265 return 2;
266 }
[f0ceb1d]267
[fd282ad]268 /*Insert directory entries . and ..*/
[0b07acd]269 if (insert_dentries(&sb) != EOK) {
270 printf(NAME ": Error. Root directory initialization failed\n");
271 return 2;
272 }
[fd282ad]273
[03bc76a]274 block_fini(service_id);
275
[954bf385]276 return 0;
277}
278
[2eaf655]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 */
[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
[b438804]292 root_block = malloc(sb->block_size);
[f66d903]293 memset(root_block, 0x00, sb->block_size);
[0b07acd]294
295 if (!root_block)
296 return ENOMEM;
[fcce9e1]297
298 dentry_ptr = root_block;
[e91b898]299
[fd282ad]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,
[e91b898]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,
[e91b898]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
[2eaf655]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 */
[197b671]338static int init_inode_table(const struct mfs_sb_info *sb)
[410a065]339{
340 unsigned int i;
341 uint8_t *itable_buf;
[0b07acd]342 int rc;
[340b5690]343
[f66d903]344 long itable_off = sb->zbmap_blocks + sb->ibmap_blocks + 2;
345 unsigned long itable_size = sb->itable_size;
[340b5690]346
[f66d903]347 itable_buf = malloc(sb->block_size);
[0b07acd]348
349 if (!itable_buf)
350 return ENOMEM;
351
[f66d903]352 memset(itable_buf, 0x00, sb->block_size);
[410a065]353
[8ceba1e]354 for (i = 0; i < itable_size; ++i, ++itable_off) {
[f66d903]355 rc = write_block(itable_off, 1, itable_buf);
[0b07acd]356
357 if (rc != EOK)
358 break;
359 }
[410a065]360
361 free(itable_buf);
[0b07acd]362 return rc;
[410a065]363}
364
[2eaf655]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 */
[197b671]371static int make_root_ino(const struct mfs_sb_info *sb)
[410a065]372{
373 struct mfs_inode *ino_buf;
[0b07acd]374 int rc;
[410a065]375
[f66d903]376 const long itable_off = sb->zbmap_blocks + sb->ibmap_blocks + 2;
[340b5690]377
[4727a97a]378 const time_t sec = time(NULL);
379
[b438804]380 ino_buf = malloc(MFS_BLOCKSIZE);
[0b07acd]381
382 if (!ino_buf)
383 return ENOMEM;
384
[4727a97a]385 memset(ino_buf, 0x00, MFS_BLOCKSIZE);
[410a065]386
[e33100c]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 :
[e91b898]391 MFS_DIRSIZE) * 2;
[e33100c]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;
[410a065]395
[f66d903]396 rc = write_block(itable_off, 1, ino_buf);
[410a065]397
398 free(ino_buf);
[0b07acd]399 return rc;
[410a065]400}
401
[2eaf655]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 */
[bed0356]408static int make_root_ino2(const struct mfs_sb_info *sb)
[410a065]409{
410 struct mfs2_inode *ino_buf;
[0b07acd]411 int rc;
[410a065]412
[c4c7f5a]413 /*Compute offset of the first inode table block*/
[f66d903]414 const long itable_off = sb->zbmap_blocks + sb->ibmap_blocks + 2;
[340b5690]415
[4727a97a]416 const time_t sec = time(NULL);
417
[b438804]418 ino_buf = malloc(sb->block_size);
[0b07acd]419
420 if (!ino_buf)
421 return ENOMEM;
422
[f66d903]423 memset(ino_buf, 0x00, sb->block_size);
[410a065]424
[e33100c]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;
[410a065]434
[f66d903]435 rc = write_block(itable_off, 1, ino_buf);
[410a065]436
437 free(ino_buf);
[0b07acd]438 return rc;
[410a065]439}
440
[2eaf655]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 */
[0b07acd]447static int init_superblock(struct mfs_sb_info *sb)
[eee8007]448{
[e2267e82]449 aoff64_t inodes;
[99f043e]450 unsigned long ind;
451 unsigned long ind2;
452 unsigned long zones;
[0b07acd]453 int rc;
[68ed0fb]454
[59e670e]455 if (sb->longnames)
456 sb->magic = sb->fs_version == 1 ? MFS_MAGIC_V1L : MFS_MAGIC_V2L;
[939b7d2]457
[e03a733]458 /*Compute the number of zones on disk*/
459
[59e670e]460 if (sb->fs_version == 1) {
461 /*Valid only for MFS V1*/
462 sb->n_zones = sb->dev_nblocks > UINT16_MAX ?
[e91b898]463 UINT16_MAX : sb->dev_nblocks;
[99f043e]464 ind = MFS_BLOCKSIZE / sizeof(uint16_t);
465 ind2 = ind * ind;
466 sb->max_file_size = (V1_NR_DIRECT_ZONES + ind + ind2) * MFS_BLOCKSIZE;
[59e670e]467 } else {
468 /*Valid for MFS V2/V3*/
[99f043e]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;
[59e670e]478 sb->n_zones = sb->dev_nblocks > UINT32_MAX ?
[e91b898]479 UINT32_MAX : sb->dev_nblocks;
[19a057f9]480
[59e670e]481 if (sb->fs_version == 3) {
[99f043e]482 if(INT32_MAX / sb->block_size < zones)
483 sb->max_file_size = INT32_MAX;
[59e670e]484 sb->ino_per_block = V3_INODES_PER_BLOCK(sb->block_size);
485 sb->n_zones /= (sb->block_size / MFS_MIN_BLOCKSIZE);
486 }
487 }
[19a057f9]488
[e03a733]489 /*Round up the number of inodes to fill block size*/
[59e670e]490 if (sb->n_inodes == 0)
491 inodes = sb->dev_nblocks / 3;
[c64506b]492 else
493 inodes = sb->n_inodes;
[68ed0fb]494
[59e670e]495 if (inodes % sb->ino_per_block)
496 inodes = ((inodes / sb->ino_per_block) + 1) * sb->ino_per_block;
[e91b898]497
[59e670e]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;
[68ed0fb]502
503 /*Compute inode bitmap size in blocks*/
[59e670e]504 sb->ibmap_blocks = UPPER(sb->n_inodes, sb->block_size * 8);
[68ed0fb]505
[e03a733]506 /*Compute inode table size*/
[410a065]507 sb->itable_size = sb->n_inodes / sb->ino_per_block;
[e03a733]508
[340b5690]509 /*Compute zone bitmap size in blocks*/
510 sb->zbmap_blocks = UPPER(sb->n_zones, sb->block_size * 8);
511
[68ed0fb]512 /*Compute first data zone position*/
[92dd5c8]513 sb->first_data_zone = 2 + sb->itable_size +
[e91b898]514 sb->zbmap_blocks + sb->ibmap_blocks;
[ae1ae27]515
516 /*Set log2 of zone to block ratio to zero*/
[59e670e]517 sb->log2_zone_size = 0;
[ae1ae27]518
[0b07acd]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
[68ed0fb]525 /*Superblock is now ready to be written on disk*/
[86c03ba]526 printf(NAME ": %d block size\n", sb->block_size);
[59e670e]527 printf(NAME ": %d inodes\n", (uint32_t) sb->n_inodes);
528 printf(NAME ": %d zones\n", (uint32_t) sb->n_zones);
[410a065]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);
[59e670e]532 printf(NAME ": first data zone = %d\n", (uint32_t) sb->first_data_zone);
[99f043e]533 printf(NAME ": max file size = %u\n", sb->max_file_size);
[59e670e]534 printf(NAME ": long fnames = %s\n", sb->longnames ? "Yes" : "No");
[bc99ed6a]535
[59e670e]536 if (sb->fs_version == 3)
[0b07acd]537 rc = write_superblock3(sb);
[59e670e]538 else
[0b07acd]539 rc = write_superblock(sb);
540
541 return rc;
[eee8007]542}
543
[2eaf655]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 */
[197b671]550static int write_superblock(const struct mfs_sb_info *sbi)
[eee8007]551{
[59e670e]552 struct mfs_superblock *sb;
[0b07acd]553 int rc;
[e03a733]554
[b438804]555 sb = malloc(MFS_SUPERBLOCK_SIZE);;
[e03a733]556
[0b07acd]557 if (!sb)
558 return ENOMEM;
559
[59e670e]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;
[99f043e]567 sb->s_max_file_size = sbi->max_file_size;
[59e670e]568 sb->s_magic = sbi->magic;
569 sb->s_state = MFS_VALID_FS;
[e03a733]570
[f66d903]571 rc = write_block(MFS_SUPERBLOCK, 1, sb);
[8e41994]572 free(sb);
[0b07acd]573
574 return rc;
[59e670e]575}
[e03a733]576
[2eaf655]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 */
[197b671]583static int write_superblock3(const struct mfs_sb_info *sbi)
[59e670e]584{
585 struct mfs3_superblock *sb;
[0b07acd]586 int rc;
[59e670e]587
[b438804]588 sb = malloc(MFS_SUPERBLOCK_SIZE);
[59e670e]589
[0b07acd]590 if (!sb)
591 return ENOMEM;
592
[59e670e]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;
[99f043e]599 sb->s_max_file_size = sbi->max_file_size;
[59e670e]600 sb->s_magic = sbi->magic;
601 sb->s_block_size = sbi->block_size;
[e03a733]602 sb->s_disk_version = 3;
[bc99ed6a]603
[03bc76a]604 rc = block_write_direct(service_id, MFS_SUPERBLOCK << 1, 1 << 1, sb);
[410a065]605 free(sb);
[0b07acd]606
607 return rc;
[eee8007]608}
609
[2eaf655]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 */
[197b671]616static int init_bitmaps(const struct mfs_sb_info *sb)
[e6aaa59]617{
[bb0db564]618 uint32_t *ibmap_buf, *zbmap_buf;
619 uint8_t *ibmap_buf8, *zbmap_buf8;
[f66d903]620 const unsigned int ibmap_nblocks = sb->ibmap_blocks;
621 const unsigned int zbmap_nblocks = sb->zbmap_blocks;
[e6aaa59]622 unsigned int i;
[0b07acd]623 int rc;
[e6aaa59]624
[b438804]625 ibmap_buf = malloc(ibmap_nblocks * sb->block_size);
626 zbmap_buf = malloc(zbmap_nblocks * sb->block_size);
[e6aaa59]627
[0b07acd]628 if (!ibmap_buf || !zbmap_buf)
629 return ENOMEM;
630
[59e670e]631 memset(ibmap_buf, 0xFF, ibmap_nblocks * sb->block_size);
632 memset(zbmap_buf, 0xFF, zbmap_nblocks * sb->block_size);
[e6aaa59]633
[e33100c]634 for (i = 2; i < sb->n_inodes + 1; ++i)
[bb0db564]635 mark_bmap(ibmap_buf, i, FREE);
[f5cbd4f]636
[03fa85c]637 for (i = sb->first_data_zone + 1; i < sb->n_zones; ++i)
[bb0db564]638 mark_bmap(zbmap_buf, i, FREE);
[e6aaa59]639
[bb0db564]640 ibmap_buf8 = (uint8_t *) ibmap_buf;
641 zbmap_buf8 = (uint8_t *) zbmap_buf;
642
[f66d903]643 int start_block = 2;
[8ceba1e]644
[7df022e5]645 for (i = 0; i < ibmap_nblocks; ++i) {
[f66d903]646 if ((rc = write_block(start_block + i,
647 1, (ibmap_buf8 + i * sb->block_size))) != EOK)
[7df022e5]648 return rc;
649 }
650
[f66d903]651 start_block = 2 + ibmap_nblocks;
[8ceba1e]652
[7df022e5]653 for (i = 0; i < zbmap_nblocks; ++i) {
[f66d903]654 if ((rc = write_block(start_block + i,
655 1, (zbmap_buf8 + i * sb->block_size))) != EOK)
[7df022e5]656 return rc;
657 }
658
[c4c7f5a]659 free(ibmap_buf);
660 free(zbmap_buf);
661
[0b07acd]662 return rc;
[e6aaa59]663}
664
[2eaf655]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 */
[59e670e]671static void mark_bmap(uint32_t *bmap, int idx, int v)
[e6aaa59]672{
673 if (v == FREE)
[59e670e]674 bmap[idx / 32] &= ~(1 << (idx % 32));
[e6aaa59]675 else
[59e670e]676 bmap[idx / 32] |= 1 << (idx % 32);
[e6aaa59]677}
678
[2eaf655]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 */
[f66d903]687static inline int write_block(aoff64_t off, size_t size, const void *data)
[b84175a]688{
[f66d903]689 if (shift == 3) {
690 int rc;
691 aoff64_t tmp_off = off << 1;
692 uint8_t *data_ptr = (uint8_t *) data;
693
[03bc76a]694 rc = block_write_direct(service_id, tmp_off << 2, size << 2, data_ptr);
[f66d903]695
696 if (rc != EOK)
697 return rc;
698
699 data_ptr += 2048;
700 tmp_off++;
701
[03bc76a]702 return block_write_direct(service_id, tmp_off << 2, size << 2, data_ptr);
[f66d903]703 }
[03bc76a]704 return block_write_direct(service_id, off << shift, size << shift, data);
[b84175a]705}
706
[7a46bfe]707static void help_cmd_mkmfs(help_level_t level)
[954bf385]708{
709 if (level == HELP_SHORT) {
710 printf(NAME": tool to create new Minix file systems\n");
711 } else {
[e91b898]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");
[954bf385]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.