source: mainline/uspace/app/mkminix/mkminix.c@ e91b898

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

cstyle

  • Property mode set to 100644
File size: 18.0 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[] = {
[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;
[197b671]116 aoff64_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) {
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;) {
[2cada66]138 c = getopt_long(argc, argv, "lh12b:i:", long_options, &opt_ind);
[954bf385]139 switch (c) {
140 case 'h':
141 help_cmd_mkminix(HELP_LONG);
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) {
199 help_cmd_mkminix(HELP_LONG);
200 exit(0);
201 }
202
[197b671]203 rc = devmap_device_get_handle(device_name, &handle, 0);
[954bf385]204 if (rc != EOK) {
205 printf(NAME ": Error resolving device `%s'.\n", device_name);
206 return 2;
207 }
208
[1affcdf3]209 rc = block_init(EXCHANGE_SERIALIZE, handle, 2048);
[954bf385]210 if (rc != EOK) {
211 printf(NAME ": Error initializing libblock.\n");
212 return 2;
213 }
214
[197b671]215 rc = block_get_bsize(handle, &devblock_size);
[954bf385]216 if (rc != EOK) {
217 printf(NAME ": Error determining device block size.\n");
218 return 2;
219 }
220
[197b671]221 rc = block_get_nblocks(handle, &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
[954bf385]274 return 0;
275}
276
[2eaf655]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 */
[197b671]283static int insert_dentries(const struct mfs_sb_info *sb)
[fd282ad]284{
285 void *root_block;
[fcce9e1]286 uint8_t *dentry_ptr;
[0b07acd]287 int rc;
[f66d903]288 const long root_dblock = sb->first_data_zone;
[fd282ad]289
[b438804]290 root_block = malloc(sb->block_size);
[f66d903]291 memset(root_block, 0x00, sb->block_size);
[0b07acd]292
293 if (!root_block)
294 return ENOMEM;
[fcce9e1]295
296 dentry_ptr = root_block;
[e91b898]297
[fd282ad]298 if (sb->fs_version != 3) {
[f0ceb1d]299 /*Directory entries for V1/V2 filesystem*/
[fd282ad]300 struct mfs_dentry *dentry = root_block;
301
302 dentry->d_inum = MFS_ROOT_INO;
[14c29ba]303 memcpy(dentry->d_name, ".\0", 2);
[fd282ad]304
[fcce9e1]305 dentry = (struct mfs_dentry *) NEXT_DENTRY(dentry_ptr,
[e91b898]306 sb->dirsize);
[fd282ad]307
308 dentry->d_inum = MFS_ROOT_INO;
[14c29ba]309 memcpy(dentry->d_name, "..\0", 3);
[fd282ad]310 } else {
[f0ceb1d]311 /*Directory entries for V3 filesystem*/
[fd282ad]312 struct mfs3_dentry *dentry = root_block;
313
314 dentry->d_inum = MFS_ROOT_INO;
[14c29ba]315 memcpy(dentry->d_name, ".\0", 2);
[fd282ad]316
[fcce9e1]317 dentry = (struct mfs3_dentry *) NEXT_DENTRY(dentry_ptr,
[e91b898]318 sb->dirsize);
[fd282ad]319
320 dentry->d_inum = MFS_ROOT_INO;
[14c29ba]321 memcpy(dentry->d_name, "..\0", 3);
[fd282ad]322 }
323
[f66d903]324 rc = write_block(root_dblock, 1, root_block);
[fd282ad]325
326 free(root_block);
[0b07acd]327 return rc;
[fd282ad]328}
329
[2eaf655]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 */
[197b671]336static int init_inode_table(const struct mfs_sb_info *sb)
[410a065]337{
338 unsigned int i;
339 uint8_t *itable_buf;
[0b07acd]340 int rc;
[340b5690]341
[f66d903]342 long itable_off = sb->zbmap_blocks + sb->ibmap_blocks + 2;
343 unsigned long itable_size = sb->itable_size;
[340b5690]344
[f66d903]345 itable_buf = malloc(sb->block_size);
[0b07acd]346
347 if (!itable_buf)
348 return ENOMEM;
349
[f66d903]350 memset(itable_buf, 0x00, sb->block_size);
[410a065]351
[8ceba1e]352 for (i = 0; i < itable_size; ++i, ++itable_off) {
[f66d903]353 rc = write_block(itable_off, 1, itable_buf);
[0b07acd]354
355 if (rc != EOK)
356 break;
357 }
[410a065]358
359 free(itable_buf);
[0b07acd]360 return rc;
[410a065]361}
362
[2eaf655]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 */
[197b671]369static int make_root_ino(const struct mfs_sb_info *sb)
[410a065]370{
371 struct mfs_inode *ino_buf;
[0b07acd]372 int rc;
[410a065]373
[f66d903]374 const long itable_off = sb->zbmap_blocks + sb->ibmap_blocks + 2;
[340b5690]375
[4727a97a]376 const time_t sec = time(NULL);
377
[b438804]378 ino_buf = malloc(MFS_BLOCKSIZE);
[0b07acd]379
380 if (!ino_buf)
381 return ENOMEM;
382
[4727a97a]383 memset(ino_buf, 0x00, MFS_BLOCKSIZE);
[410a065]384
[e33100c]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 :
[e91b898]389 MFS_DIRSIZE) * 2;
[e33100c]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;
[410a065]393
[f66d903]394 rc = write_block(itable_off, 1, ino_buf);
[410a065]395
396 free(ino_buf);
[0b07acd]397 return rc;
[410a065]398}
399
[2eaf655]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 */
[bed0356]406static int make_root_ino2(const struct mfs_sb_info *sb)
[410a065]407{
408 struct mfs2_inode *ino_buf;
[0b07acd]409 int rc;
[410a065]410
[c4c7f5a]411 /*Compute offset of the first inode table block*/
[f66d903]412 const long itable_off = sb->zbmap_blocks + sb->ibmap_blocks + 2;
[340b5690]413
[4727a97a]414 const time_t sec = time(NULL);
415
[b438804]416 ino_buf = malloc(sb->block_size);
[0b07acd]417
418 if (!ino_buf)
419 return ENOMEM;
420
[f66d903]421 memset(ino_buf, 0x00, sb->block_size);
[410a065]422
[e33100c]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;
[410a065]432
[f66d903]433 rc = write_block(itable_off, 1, ino_buf);
[410a065]434
435 free(ino_buf);
[0b07acd]436 return rc;
[410a065]437}
438
[2eaf655]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 */
[0b07acd]445static int init_superblock(struct mfs_sb_info *sb)
[eee8007]446{
[e2267e82]447 aoff64_t inodes;
[0b07acd]448 int rc;
[68ed0fb]449
[59e670e]450 if (sb->longnames)
451 sb->magic = sb->fs_version == 1 ? MFS_MAGIC_V1L : MFS_MAGIC_V2L;
[939b7d2]452
[e03a733]453 /*Compute the number of zones on disk*/
454
[59e670e]455 if (sb->fs_version == 1) {
456 /*Valid only for MFS V1*/
457 sb->n_zones = sb->dev_nblocks > UINT16_MAX ?
[e91b898]458 UINT16_MAX : sb->dev_nblocks;
[59e670e]459 } else {
460 /*Valid for MFS V2/V3*/
461 sb->n_zones = sb->dev_nblocks > UINT32_MAX ?
[e91b898]462 UINT32_MAX : sb->dev_nblocks;
[19a057f9]463
[59e670e]464 if (sb->fs_version == 3) {
465 sb->ino_per_block = V3_INODES_PER_BLOCK(sb->block_size);
466 sb->n_zones /= (sb->block_size / MFS_MIN_BLOCKSIZE);
467 }
468 }
[19a057f9]469
[e03a733]470 /*Round up the number of inodes to fill block size*/
[59e670e]471 if (sb->n_inodes == 0)
472 inodes = sb->dev_nblocks / 3;
[c64506b]473 else
474 inodes = sb->n_inodes;
[68ed0fb]475
[59e670e]476 if (inodes % sb->ino_per_block)
477 inodes = ((inodes / sb->ino_per_block) + 1) * sb->ino_per_block;
[e91b898]478
[59e670e]479 if (sb->fs_version < 3)
480 sb->n_inodes = inodes > UINT16_MAX ? UINT16_MAX : inodes;
481 else
482 sb->n_inodes = inodes > UINT32_MAX ? UINT32_MAX : inodes;
[68ed0fb]483
484 /*Compute inode bitmap size in blocks*/
[59e670e]485 sb->ibmap_blocks = UPPER(sb->n_inodes, sb->block_size * 8);
[68ed0fb]486
[e03a733]487 /*Compute inode table size*/
[410a065]488 sb->itable_size = sb->n_inodes / sb->ino_per_block;
[e03a733]489
[340b5690]490 /*Compute zone bitmap size in blocks*/
491 sb->zbmap_blocks = UPPER(sb->n_zones, sb->block_size * 8);
492
[68ed0fb]493 /*Compute first data zone position*/
[92dd5c8]494 sb->first_data_zone = 2 + sb->itable_size +
[e91b898]495 sb->zbmap_blocks + sb->ibmap_blocks;
[ae1ae27]496
497 /*Set log2 of zone to block ratio to zero*/
[59e670e]498 sb->log2_zone_size = 0;
[ae1ae27]499
[0b07acd]500 /*Check for errors*/
501 if (sb->first_data_zone >= sb->n_zones) {
502 printf(NAME ": Error! Insufficient disk space");
503 return ENOMEM;
504 }
505
[68ed0fb]506 /*Superblock is now ready to be written on disk*/
[86c03ba]507 printf(NAME ": %d block size\n", sb->block_size);
[59e670e]508 printf(NAME ": %d inodes\n", (uint32_t) sb->n_inodes);
509 printf(NAME ": %d zones\n", (uint32_t) sb->n_zones);
[410a065]510 printf(NAME ": inode table blocks = %ld\n", sb->itable_size);
511 printf(NAME ": inode bitmap blocks = %ld\n", sb->ibmap_blocks);
512 printf(NAME ": zone bitmap blocks = %ld\n", sb->zbmap_blocks);
[59e670e]513 printf(NAME ": first data zone = %d\n", (uint32_t) sb->first_data_zone);
514 printf(NAME ": long fnames = %s\n", sb->longnames ? "Yes" : "No");
[bc99ed6a]515
[59e670e]516 if (sb->fs_version == 3)
[0b07acd]517 rc = write_superblock3(sb);
[59e670e]518 else
[0b07acd]519 rc = write_superblock(sb);
520
521 return rc;
[eee8007]522}
523
[2eaf655]524/**Write the V1/V2 superblock on disk.
525 *
526 * @param sbi Pointer to the superblock structure to write on disk.
527 *
528 * @return EOK on success or a negative error code.
529 */
[197b671]530static int write_superblock(const struct mfs_sb_info *sbi)
[eee8007]531{
[59e670e]532 struct mfs_superblock *sb;
[0b07acd]533 int rc;
[e03a733]534
[b438804]535 sb = malloc(MFS_SUPERBLOCK_SIZE);;
[e03a733]536
[0b07acd]537 if (!sb)
538 return ENOMEM;
539
[59e670e]540 sb->s_ninodes = (uint16_t) sbi->n_inodes;
541 sb->s_nzones = (uint16_t) sbi->n_zones;
542 sb->s_nzones2 = (uint32_t) sbi->n_zones;
543 sb->s_ibmap_blocks = (uint16_t) sbi->ibmap_blocks;
544 sb->s_zbmap_blocks = (uint16_t) sbi->zbmap_blocks;
545 sb->s_first_data_zone = (uint16_t) sbi->first_data_zone;
546 sb->s_log2_zone_size = sbi->log2_zone_size;
547 sb->s_max_file_size = UINT32_MAX;
548 sb->s_magic = sbi->magic;
549 sb->s_state = MFS_VALID_FS;
[e03a733]550
[f66d903]551 rc = write_block(MFS_SUPERBLOCK, 1, sb);
[8e41994]552 free(sb);
[0b07acd]553
554 return rc;
[59e670e]555}
[e03a733]556
[2eaf655]557/**Write the V3s superblock on disk.
558 *
559 * @param sbi Pointer to the superblock structure to write on disk.
560 *
561 * @return EOK on success or a negative error code.
562 */
[197b671]563static int write_superblock3(const struct mfs_sb_info *sbi)
[59e670e]564{
565 struct mfs3_superblock *sb;
[0b07acd]566 int rc;
[59e670e]567
[b438804]568 sb = malloc(MFS_SUPERBLOCK_SIZE);
[59e670e]569
[0b07acd]570 if (!sb)
571 return ENOMEM;
572
[59e670e]573 sb->s_ninodes = (uint32_t) sbi->n_inodes;
574 sb->s_nzones = (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 = UINT32_MAX;
580 sb->s_magic = sbi->magic;
581 sb->s_block_size = sbi->block_size;
[e03a733]582 sb->s_disk_version = 3;
[bc99ed6a]583
[f66d903]584 rc = block_write_direct(handle, MFS_SUPERBLOCK << 1, 1 << 1, sb);
[410a065]585 free(sb);
[0b07acd]586
587 return rc;
[eee8007]588}
589
[2eaf655]590/**Initialize the inode and block bitmaps on disk.
591 *
592 * @param sb Pointer to the superblock structure.
593 *
594 * @return EOK on success or a negative error code.
595 */
[197b671]596static int init_bitmaps(const struct mfs_sb_info *sb)
[e6aaa59]597{
[bb0db564]598 uint32_t *ibmap_buf, *zbmap_buf;
599 uint8_t *ibmap_buf8, *zbmap_buf8;
[f66d903]600 const unsigned int ibmap_nblocks = sb->ibmap_blocks;
601 const unsigned int zbmap_nblocks = sb->zbmap_blocks;
[e6aaa59]602 unsigned int i;
[0b07acd]603 int rc;
[e6aaa59]604
[b438804]605 ibmap_buf = malloc(ibmap_nblocks * sb->block_size);
606 zbmap_buf = malloc(zbmap_nblocks * sb->block_size);
[e6aaa59]607
[0b07acd]608 if (!ibmap_buf || !zbmap_buf)
609 return ENOMEM;
610
[59e670e]611 memset(ibmap_buf, 0xFF, ibmap_nblocks * sb->block_size);
612 memset(zbmap_buf, 0xFF, zbmap_nblocks * sb->block_size);
[e6aaa59]613
[e33100c]614 for (i = 2; i < sb->n_inodes + 1; ++i)
[bb0db564]615 mark_bmap(ibmap_buf, i, FREE);
[f5cbd4f]616
[03fa85c]617 for (i = sb->first_data_zone + 1; i < sb->n_zones; ++i)
[bb0db564]618 mark_bmap(zbmap_buf, i, FREE);
[e6aaa59]619
[bb0db564]620 ibmap_buf8 = (uint8_t *) ibmap_buf;
621 zbmap_buf8 = (uint8_t *) zbmap_buf;
622
[f66d903]623 int start_block = 2;
[8ceba1e]624
[7df022e5]625 for (i = 0; i < ibmap_nblocks; ++i) {
[f66d903]626 if ((rc = write_block(start_block + i,
627 1, (ibmap_buf8 + i * sb->block_size))) != EOK)
[7df022e5]628 return rc;
629 }
630
[f66d903]631 start_block = 2 + ibmap_nblocks;
[8ceba1e]632
[7df022e5]633 for (i = 0; i < zbmap_nblocks; ++i) {
[f66d903]634 if ((rc = write_block(start_block + i,
635 1, (zbmap_buf8 + i * sb->block_size))) != EOK)
[7df022e5]636 return rc;
637 }
638
[c4c7f5a]639 free(ibmap_buf);
640 free(zbmap_buf);
641
[0b07acd]642 return rc;
[e6aaa59]643}
644
[2eaf655]645/**Mark a bitmap entry as used or free.
646 *
647 * @param bmap 32-bit pointer to the bitmap in memory.
648 * @param idx The index in the bitmap of the bit to set at 1 or 0.
649 * @param v FREE to clear the bit, USED to set the bit.
650 */
[59e670e]651static void mark_bmap(uint32_t *bmap, int idx, int v)
[e6aaa59]652{
653 if (v == FREE)
[59e670e]654 bmap[idx / 32] &= ~(1 << (idx % 32));
[e6aaa59]655 else
[59e670e]656 bmap[idx / 32] |= 1 << (idx % 32);
[e6aaa59]657}
658
[2eaf655]659/**Write a block on disk.
660 *
661 * @param off 64-bit block offset on disk.
662 * @param size size of the block.
663 * @param data Pointer to the block content.
664 *
665 * @return EOK on success or a negative error number.
666 */
[f66d903]667static inline int write_block(aoff64_t off, size_t size, const void *data)
[b84175a]668{
[f66d903]669 if (shift == 3) {
670 int rc;
671 aoff64_t tmp_off = off << 1;
672 uint8_t *data_ptr = (uint8_t *) data;
673
674 rc = block_write_direct(handle, tmp_off << 2, size << 2, data_ptr);
675
676 if (rc != EOK)
677 return rc;
678
679 data_ptr += 2048;
680 tmp_off++;
681
682 return block_write_direct(handle, tmp_off << 2, size << 2, data_ptr);
683 }
684 return block_write_direct(handle, off << shift, size << shift, data);
[b84175a]685}
686
[954bf385]687static void help_cmd_mkminix(help_level_t level)
688{
689 if (level == HELP_SHORT) {
690 printf(NAME": tool to create new Minix file systems\n");
691 } else {
[e91b898]692 printf("Usage: [options] device\n"
693 "-1 Make a Minix version 1 filesystem\n"
694 "-2 Make a Minix version 2 filesystem\n"
695 "-b ## Specify the block size in bytes (V3 only),\n"
696 " valid block size values are 1024, 2048 and 4096 bytes per block\n"
697 "-i ## Specify the number of inodes for the filesystem\n"
698 "-l Use 30-char long filenames (V1/V2 only)\n");
[954bf385]699 }
700}
701
702static int num_of_set_bits(uint32_t n)
703{
704 n = n - ((n >> 1) & 0x55555555);
705 n = (n & 0x33333333) + ((n >> 2) & 0x33333333);
706 return (((n + (n >> 4)) & 0xF0F0F0F) * 0x1010101) >> 24;
707}
708
709
710/**
711 * @}
712 */
Note: See TracBrowser for help on using the repository browser.