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

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

Fix possible memory leak in mkmfs.

  • Property mode set to 100644
File size: 18.9 KB
RevLine 
[954bf385]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/**
[7a46bfe]34 * @file mkmfs.c
[954bf385]35 * @brief Tool for creating new Minix file systems.
36 *
37 */
38
39#include <stdio.h>
40#include <stdlib.h>
[f73b291]41#include <block.h>
[954bf385]42#include <errno.h>
43#include <inttypes.h>
44#include <getopt.h>
45#include <mem.h>
[fd282ad]46#include <str.h>
[4727a97a]47#include <time.h>
[86d0b4b3]48#include <minix.h>
[954bf385]49
[7a46bfe]50#define NAME "mkmfs"
[954bf385]51
[e6aaa59]52#define FREE 0
53#define USED 1
54
[8367d1d]55#define UPPER(n, size) (((n) / (size)) + (((n) % (size)) != 0))
56#define NEXT_DENTRY(p, dirsize) (p += (dirsize))
[ae1ae27]57
[954bf385]58typedef enum {
59 HELP_SHORT,
60 HELP_LONG
61} help_level_t;
62
[8367d1d]63/* Generic MFS superblock */
[59e670e]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;
[410a065]71 unsigned long itable_size;
[59e670e]72 int log2_zone_size;
73 int ino_per_block;
[fd282ad]74 int dirsize;
[59e670e]75 uint32_t max_file_size;
76 uint16_t magic;
77 uint32_t block_size;
78 int fs_version;
79 bool longnames;
80};
[19a057f9]81
[7a46bfe]82static void help_cmd_mkmfs(help_level_t level);
[5726a1a3]83static bool is_power_of_two(uint32_t n);
[0b07acd]84static int init_superblock(struct mfs_sb_info *sb);
[197b671]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);
[bed0356]90static int make_root_ino2(const struct mfs_sb_info *sb);
[59e670e]91static void mark_bmap(uint32_t *bmap, int idx, int v);
[197b671]92static int insert_dentries(const struct mfs_sb_info *sb);
93
[f66d903]94static inline int write_block(aoff64_t off, size_t size, const void *data);
[b84175a]95
[03bc76a]96static service_id_t service_id;
[f66d903]97static int shift;
[954bf385]98
99static struct option const long_options[] = {
[e91b898]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 }
[954bf385]107};
108
109int main (int argc, char **argv)
110{
111 int rc, c, opt_ind;
112 char *device_name;
[16cc9a6]113 size_t devblock_size;
[954bf385]114
[59e670e]115 struct mfs_sb_info sb;
[954bf385]116
[8367d1d]117 /* Default is MinixFS V3 */
[59e670e]118 sb.magic = MFS_MAGIC_V3;
[6cda025]119 sb.fs_version = 3;
[eee8007]120
[8367d1d]121 /* Default block size is 4Kb */
[59e670e]122 sb.block_size = MFS_MAX_BLOCKSIZE;
[c4c7f5a]123 sb.dirsize = MFS3_DIRSIZE;
[59e670e]124 sb.n_inodes = 0;
125 sb.longnames = false;
126 sb.ino_per_block = V3_INODES_PER_BLOCK(MFS_MAX_BLOCKSIZE);
[eaf9794]127
[954bf385]128 if (argc == 1) {
[7a46bfe]129 help_cmd_mkmfs(HELP_SHORT);
130 printf("Incorrect number of arguments, try `mkmfs --help'\n");
[954bf385]131 exit(0);
132 }
133
134 for (c = 0, optind = 0, opt_ind = 0; c != -1;) {
[8367d1d]135 c = getopt_long(argc, argv, "lh12b:i:",
136 long_options, &opt_ind);
[954bf385]137 switch (c) {
138 case 'h':
[7a46bfe]139 help_cmd_mkmfs(HELP_LONG);
[954bf385]140 exit(0);
141 case '1':
[59e670e]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;
[fd282ad]146 sb.dirsize = MFS_DIRSIZE;
[954bf385]147 break;
148 case '2':
[59e670e]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;
[fd282ad]153 sb.dirsize = MFS_DIRSIZE;
[954bf385]154 break;
155 case 'b':
[59e670e]156 sb.block_size = (uint32_t) strtol(optarg, NULL, 10);
[954bf385]157 break;
158 case 'i':
[c64506b]159 sb.n_inodes = (uint64_t) strtol(optarg, NULL, 10);
[eee8007]160 break;
161 case 'l':
[59e670e]162 sb.longnames = true;
[fd282ad]163 sb.dirsize = MFSL_DIRSIZE;
[954bf385]164 break;
165 }
166 }
167
[59e670e]168 if (sb.block_size < MFS_MIN_BLOCKSIZE ||
[8367d1d]169 sb.block_size > MFS_MAX_BLOCKSIZE) {
[954bf385]170 printf(NAME ":Error! Invalid block size.\n");
171 exit(0);
[5726a1a3]172 } else if (!is_power_of_two(sb.block_size)) {
[8367d1d]173 /* Block size must be a power of 2. */
[954bf385]174 printf(NAME ":Error! Invalid block size.\n");
175 exit(0);
[59e670e]176 } else if (sb.block_size > MFS_BLOCKSIZE &&
[8367d1d]177 sb.fs_version != 3) {
178 printf(NAME ":Error! Block size > 1024 is "
179 "supported by V3 filesystem only.\n");
[eee8007]180 exit(0);
[59e670e]181 } else if (sb.fs_version == 3 && sb.longnames) {
[8367d1d]182 printf(NAME ":Error! Long filenames are supported "
183 "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
[fc22069]209 rc = block_init(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) {
[8367d1d]223 printf(NAME ": Warning, failed to obtain "
224 "block device size.\n");
[954bf385]225 } else {
226 printf(NAME ": Block device has %" PRIuOFF64 " blocks.\n",
[8367d1d]227 sb.dev_nblocks);
[954bf385]228 }
229
[197b671]230 if (devblock_size != 512) {
[954bf385]231 printf(NAME ": Error. Device block size is not 512 bytes.\n");
232 return 2;
233 }
234
[8367d1d]235 /* Minimum block size is 1 Kb */
[59e670e]236 sb.dev_nblocks /= 2;
[68ed0fb]237
238 printf(NAME ": Creating Minix file system on device\n");
[5726a1a3]239 printf(NAME ": Writing superblock\n");
[954bf385]240
[8367d1d]241 /* Initialize superblock */
[0b07acd]242 if (init_superblock(&sb) != EOK) {
243 printf(NAME ": Error. Superblock initialization failed\n");
244 return 2;
245 }
[eaf9794]246
[5726a1a3]247 printf(NAME ": Initializing bitmaps\n");
248
[8367d1d]249 /* Initialize bitmaps */
[0b07acd]250 if (init_bitmaps(&sb) != EOK) {
251 printf(NAME ": Error. Bitmaps initialization failed\n");
252 return 2;
253 }
[eee8007]254
[5726a1a3]255 printf(NAME ": Initializing the inode table\n");
256
[8367d1d]257 /* Init inode table */
[0b07acd]258 if (init_inode_table(&sb) != EOK) {
259 printf(NAME ": Error. Inode table initialization failed\n");
260 return 2;
261 }
[410a065]262
[5726a1a3]263 printf(NAME ": Creating the root directory inode\n");
264
[8367d1d]265 /* Make the root inode */
[bed0356]266 if (sb.fs_version == 1)
[0b07acd]267 rc = make_root_ino(&sb);
[bed0356]268 else
269 rc = make_root_ino2(&sb);
[0b07acd]270
271 if (rc != EOK) {
272 printf(NAME ": Error. Root inode initialization failed\n");
273 return 2;
274 }
[f0ceb1d]275
[8367d1d]276 /* Insert directory entries . and .. */
[0b07acd]277 if (insert_dentries(&sb) != EOK) {
278 printf(NAME ": Error. Root directory initialization failed\n");
279 return 2;
280 }
[fd282ad]281
[03bc76a]282 block_fini(service_id);
283
[954bf385]284 return 0;
285}
286
[2eaf655]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 */
[197b671]293static int insert_dentries(const struct mfs_sb_info *sb)
[fd282ad]294{
295 void *root_block;
[fcce9e1]296 uint8_t *dentry_ptr;
[0b07acd]297 int rc;
[f66d903]298 const long root_dblock = sb->first_data_zone;
[fd282ad]299
[b438804]300 root_block = malloc(sb->block_size);
[f66d903]301 memset(root_block, 0x00, sb->block_size);
[0b07acd]302
303 if (!root_block)
304 return ENOMEM;
[fcce9e1]305
306 dentry_ptr = root_block;
[e91b898]307
[fd282ad]308 if (sb->fs_version != 3) {
[8367d1d]309 /* Directory entries for V1/V2 filesystem */
[fd282ad]310 struct mfs_dentry *dentry = root_block;
311
312 dentry->d_inum = MFS_ROOT_INO;
[14c29ba]313 memcpy(dentry->d_name, ".\0", 2);
[fd282ad]314
[fcce9e1]315 dentry = (struct mfs_dentry *) NEXT_DENTRY(dentry_ptr,
[8367d1d]316 sb->dirsize);
[fd282ad]317
318 dentry->d_inum = MFS_ROOT_INO;
[14c29ba]319 memcpy(dentry->d_name, "..\0", 3);
[fd282ad]320 } else {
[8367d1d]321 /* Directory entries for V3 filesystem */
[fd282ad]322 struct mfs3_dentry *dentry = root_block;
323
324 dentry->d_inum = MFS_ROOT_INO;
[14c29ba]325 memcpy(dentry->d_name, ".\0", 2);
[fd282ad]326
[fcce9e1]327 dentry = (struct mfs3_dentry *) NEXT_DENTRY(dentry_ptr,
[8367d1d]328 sb->dirsize);
[fd282ad]329
330 dentry->d_inum = MFS_ROOT_INO;
[14c29ba]331 memcpy(dentry->d_name, "..\0", 3);
[fd282ad]332 }
333
[f66d903]334 rc = write_block(root_dblock, 1, root_block);
[fd282ad]335
336 free(root_block);
[0b07acd]337 return rc;
[fd282ad]338}
339
[2eaf655]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 */
[197b671]346static int init_inode_table(const struct mfs_sb_info *sb)
[410a065]347{
348 unsigned int i;
349 uint8_t *itable_buf;
[0464967]350 int rc = EOK;
[340b5690]351
[f66d903]352 long itable_off = sb->zbmap_blocks + sb->ibmap_blocks + 2;
353 unsigned long itable_size = sb->itable_size;
[340b5690]354
[f66d903]355 itable_buf = malloc(sb->block_size);
[0b07acd]356
357 if (!itable_buf)
358 return ENOMEM;
359
[f66d903]360 memset(itable_buf, 0x00, sb->block_size);
[410a065]361
[8ceba1e]362 for (i = 0; i < itable_size; ++i, ++itable_off) {
[f66d903]363 rc = write_block(itable_off, 1, itable_buf);
[0b07acd]364
365 if (rc != EOK)
366 break;
367 }
[410a065]368
369 free(itable_buf);
[0b07acd]370 return rc;
[410a065]371}
372
[2eaf655]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 */
[197b671]379static int make_root_ino(const struct mfs_sb_info *sb)
[410a065]380{
381 struct mfs_inode *ino_buf;
[0b07acd]382 int rc;
[410a065]383
[f66d903]384 const long itable_off = sb->zbmap_blocks + sb->ibmap_blocks + 2;
[340b5690]385
[4727a97a]386 const time_t sec = time(NULL);
387
[b438804]388 ino_buf = malloc(MFS_BLOCKSIZE);
[0b07acd]389
390 if (!ino_buf)
391 return ENOMEM;
392
[4727a97a]393 memset(ino_buf, 0x00, MFS_BLOCKSIZE);
[410a065]394
[e33100c]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 :
[8367d1d]399 MFS_DIRSIZE) * 2;
[e33100c]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;
[410a065]403
[f66d903]404 rc = write_block(itable_off, 1, ino_buf);
[410a065]405
406 free(ino_buf);
[0b07acd]407 return rc;
[410a065]408}
409
[2eaf655]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 */
[bed0356]416static int make_root_ino2(const struct mfs_sb_info *sb)
[410a065]417{
418 struct mfs2_inode *ino_buf;
[0b07acd]419 int rc;
[410a065]420
[8367d1d]421 /* Compute offset of the first inode table block */
[f66d903]422 const long itable_off = sb->zbmap_blocks + sb->ibmap_blocks + 2;
[340b5690]423
[4727a97a]424 const time_t sec = time(NULL);
425
[b438804]426 ino_buf = malloc(sb->block_size);
[0b07acd]427
428 if (!ino_buf)
429 return ENOMEM;
430
[f66d903]431 memset(ino_buf, 0x00, sb->block_size);
[410a065]432
[e33100c]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;
[410a065]442
[f66d903]443 rc = write_block(itable_off, 1, ino_buf);
[410a065]444
445 free(ino_buf);
[0b07acd]446 return rc;
[410a065]447}
448
[2eaf655]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 */
[0b07acd]455static int init_superblock(struct mfs_sb_info *sb)
[eee8007]456{
[e2267e82]457 aoff64_t inodes;
[99f043e]458 unsigned long ind;
459 unsigned long ind2;
460 unsigned long zones;
[0b07acd]461 int rc;
[68ed0fb]462
[59e670e]463 if (sb->longnames)
[8367d1d]464 sb->magic = sb->fs_version == 1 ? MFS_MAGIC_V1L :
465 MFS_MAGIC_V2L;
[939b7d2]466
[8367d1d]467 /* Compute the number of zones on disk */
[e03a733]468
[59e670e]469 if (sb->fs_version == 1) {
[8367d1d]470 /* Valid only for MFS V1 */
[59e670e]471 sb->n_zones = sb->dev_nblocks > UINT16_MAX ?
[8367d1d]472 UINT16_MAX : sb->dev_nblocks;
[99f043e]473 ind = MFS_BLOCKSIZE / sizeof(uint16_t);
474 ind2 = ind * ind;
[8367d1d]475 sb->max_file_size = (V1_NR_DIRECT_ZONES + ind + ind2) *
476 MFS_BLOCKSIZE;
[59e670e]477 } else {
478 /*Valid for MFS V2/V3*/
[99f043e]479 size_t ptrsize;
480 if (sb->fs_version == 2)
481 ptrsize = sizeof(uint16_t);
482 else
483 ptrsize = sizeof(uint32_t);
[8367d1d]484
[99f043e]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;
[59e670e]489 sb->n_zones = sb->dev_nblocks > UINT32_MAX ?
[8367d1d]490 UINT32_MAX : sb->dev_nblocks;
[19a057f9]491
[59e670e]492 if (sb->fs_version == 3) {
[99f043e]493 if(INT32_MAX / sb->block_size < zones)
494 sb->max_file_size = INT32_MAX;
[59e670e]495 sb->ino_per_block = V3_INODES_PER_BLOCK(sb->block_size);
496 sb->n_zones /= (sb->block_size / MFS_MIN_BLOCKSIZE);
497 }
498 }
[19a057f9]499
[8367d1d]500 /* Round up the number of inodes to fill block size */
[59e670e]501 if (sb->n_inodes == 0)
502 inodes = sb->dev_nblocks / 3;
[c64506b]503 else
504 inodes = sb->n_inodes;
[68ed0fb]505
[59e670e]506 if (inodes % sb->ino_per_block)
[8367d1d]507 inodes = ((inodes / sb->ino_per_block) + 1) *
508 sb->ino_per_block;
[e91b898]509
[59e670e]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;
[68ed0fb]514
[8367d1d]515 /* Compute inode bitmap size in blocks */
[59e670e]516 sb->ibmap_blocks = UPPER(sb->n_inodes, sb->block_size * 8);
[68ed0fb]517
[8367d1d]518 /* Compute inode table size */
[410a065]519 sb->itable_size = sb->n_inodes / sb->ino_per_block;
[e03a733]520
[8367d1d]521 /* Compute zone bitmap size in blocks */
[340b5690]522 sb->zbmap_blocks = UPPER(sb->n_zones, sb->block_size * 8);
523
[8367d1d]524 /* Compute first data zone position */
[92dd5c8]525 sb->first_data_zone = 2 + sb->itable_size +
[8367d1d]526 sb->zbmap_blocks + sb->ibmap_blocks;
[ae1ae27]527
[8367d1d]528 /* Set log2 of zone to block ratio to zero */
[59e670e]529 sb->log2_zone_size = 0;
[ae1ae27]530
[8367d1d]531 /* Check for errors */
[0b07acd]532 if (sb->first_data_zone >= sb->n_zones) {
533 printf(NAME ": Error! Insufficient disk space");
534 return ENOMEM;
535 }
536
[8367d1d]537 /* Superblock is now ready to be written on disk */
[86c03ba]538 printf(NAME ": %d block size\n", sb->block_size);
[59e670e]539 printf(NAME ": %d inodes\n", (uint32_t) sb->n_inodes);
540 printf(NAME ": %d zones\n", (uint32_t) sb->n_zones);
[410a065]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);
[8367d1d]544 printf(NAME ": first data zone = %d\n", (uint32_t)sb->first_data_zone);
[99f043e]545 printf(NAME ": max file size = %u\n", sb->max_file_size);
[59e670e]546 printf(NAME ": long fnames = %s\n", sb->longnames ? "Yes" : "No");
[bc99ed6a]547
[59e670e]548 if (sb->fs_version == 3)
[0b07acd]549 rc = write_superblock3(sb);
[59e670e]550 else
[0b07acd]551 rc = write_superblock(sb);
552
553 return rc;
[eee8007]554}
555
[2eaf655]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 */
[197b671]562static int write_superblock(const struct mfs_sb_info *sbi)
[eee8007]563{
[59e670e]564 struct mfs_superblock *sb;
[0b07acd]565 int rc;
[e03a733]566
[b438804]567 sb = malloc(MFS_SUPERBLOCK_SIZE);;
[e03a733]568
[0b07acd]569 if (!sb)
570 return ENOMEM;
571
[59e670e]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;
[99f043e]579 sb->s_max_file_size = sbi->max_file_size;
[59e670e]580 sb->s_magic = sbi->magic;
581 sb->s_state = MFS_VALID_FS;
[e03a733]582
[f66d903]583 rc = write_block(MFS_SUPERBLOCK, 1, sb);
[8e41994]584 free(sb);
[0b07acd]585
586 return rc;
[59e670e]587}
[e03a733]588
[2eaf655]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 */
[197b671]595static int write_superblock3(const struct mfs_sb_info *sbi)
[59e670e]596{
597 struct mfs3_superblock *sb;
[0b07acd]598 int rc;
[59e670e]599
[b438804]600 sb = malloc(MFS_SUPERBLOCK_SIZE);
[59e670e]601
[0b07acd]602 if (!sb)
603 return ENOMEM;
604
[59e670e]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;
[99f043e]611 sb->s_max_file_size = sbi->max_file_size;
[59e670e]612 sb->s_magic = sbi->magic;
613 sb->s_block_size = sbi->block_size;
[e03a733]614 sb->s_disk_version = 3;
[bc99ed6a]615
[03bc76a]616 rc = block_write_direct(service_id, MFS_SUPERBLOCK << 1, 1 << 1, sb);
[410a065]617 free(sb);
[0b07acd]618
619 return rc;
[eee8007]620}
621
[2eaf655]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 */
[197b671]628static int init_bitmaps(const struct mfs_sb_info *sb)
[e6aaa59]629{
[bb0db564]630 uint32_t *ibmap_buf, *zbmap_buf;
631 uint8_t *ibmap_buf8, *zbmap_buf8;
[f66d903]632 const unsigned int ibmap_nblocks = sb->ibmap_blocks;
633 const unsigned int zbmap_nblocks = sb->zbmap_blocks;
[e6aaa59]634 unsigned int i;
[0464967]635 int rc = EOK;
[e6aaa59]636
[b438804]637 ibmap_buf = malloc(ibmap_nblocks * sb->block_size);
638 zbmap_buf = malloc(zbmap_nblocks * sb->block_size);
[e6aaa59]639
[88ab6e9]640 if (!ibmap_buf || !zbmap_buf) {
641 rc = ENOMEM;
642 goto exit;
643 }
[0b07acd]644
[59e670e]645 memset(ibmap_buf, 0xFF, ibmap_nblocks * sb->block_size);
646 memset(zbmap_buf, 0xFF, zbmap_nblocks * sb->block_size);
[e6aaa59]647
[e33100c]648 for (i = 2; i < sb->n_inodes + 1; ++i)
[bb0db564]649 mark_bmap(ibmap_buf, i, FREE);
[f5cbd4f]650
[0395a7b]651 for (i = 2; i < sb->n_zones - sb->first_data_zone; ++i)
[bb0db564]652 mark_bmap(zbmap_buf, i, FREE);
[e6aaa59]653
[bb0db564]654 ibmap_buf8 = (uint8_t *) ibmap_buf;
655 zbmap_buf8 = (uint8_t *) zbmap_buf;
656
[f66d903]657 int start_block = 2;
[8ceba1e]658
[7df022e5]659 for (i = 0; i < ibmap_nblocks; ++i) {
[f66d903]660 if ((rc = write_block(start_block + i,
[8367d1d]661 1, (ibmap_buf8 + i * sb->block_size))) != EOK)
[04efacc]662 goto exit;
[7df022e5]663 }
664
[f66d903]665 start_block = 2 + ibmap_nblocks;
[8ceba1e]666
[7df022e5]667 for (i = 0; i < zbmap_nblocks; ++i) {
[f66d903]668 if ((rc = write_block(start_block + i,
[8367d1d]669 1, (zbmap_buf8 + i * sb->block_size))) != EOK)
[04efacc]670 goto exit;
[7df022e5]671 }
672
[88ab6e9]673exit:
[c4c7f5a]674 free(ibmap_buf);
675 free(zbmap_buf);
676
[0b07acd]677 return rc;
[e6aaa59]678}
679
[2eaf655]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 */
[59e670e]686static void mark_bmap(uint32_t *bmap, int idx, int v)
[e6aaa59]687{
688 if (v == FREE)
[59e670e]689 bmap[idx / 32] &= ~(1 << (idx % 32));
[e6aaa59]690 else
[59e670e]691 bmap[idx / 32] |= 1 << (idx % 32);
[e6aaa59]692}
693
[2eaf655]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 */
[f66d903]702static inline int write_block(aoff64_t off, size_t size, const void *data)
[b84175a]703{
[f66d903]704 if (shift == 3) {
705 int rc;
706 aoff64_t tmp_off = off << 1;
707 uint8_t *data_ptr = (uint8_t *) data;
708
[8367d1d]709 rc = block_write_direct(service_id, tmp_off << 2,
710 size << 2, data_ptr);
[f66d903]711
712 if (rc != EOK)
713 return rc;
714
715 data_ptr += 2048;
716 tmp_off++;
717
[8367d1d]718 return block_write_direct(service_id, tmp_off << 2,
719 size << 2, data_ptr);
[f66d903]720 }
[8367d1d]721 return block_write_direct(service_id, off << shift,
722 size << shift, data);
[b84175a]723}
724
[7a46bfe]725static void help_cmd_mkmfs(help_level_t level)
[954bf385]726{
727 if (level == HELP_SHORT) {
728 printf(NAME": tool to create new Minix file systems\n");
729 } else {
[e91b898]730 printf("Usage: [options] device\n"
[8367d1d]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");
[954bf385]739 }
740}
741
[8367d1d]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 */
[5726a1a3]748static bool is_power_of_two(uint32_t n)
[954bf385]749{
[5726a1a3]750 if (n == 0)
751 return false;
752
753 return (n & (n - 1)) == 0;
[954bf385]754}
755
756
757/**
758 * @}
759 */
Note: See TracBrowser for help on using the repository browser.