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

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

Compute the maximum size of each inode

  • Property mode set to 100644
File size: 18.6 KB
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;
[99f043e]448 unsigned long ind;
449 unsigned long ind2;
450 unsigned long zones;
[0b07acd]451 int rc;
[68ed0fb]452
[59e670e]453 if (sb->longnames)
454 sb->magic = sb->fs_version == 1 ? MFS_MAGIC_V1L : MFS_MAGIC_V2L;
[939b7d2]455
[e03a733]456 /*Compute the number of zones on disk*/
457
[59e670e]458 if (sb->fs_version == 1) {
459 /*Valid only for MFS V1*/
460 sb->n_zones = sb->dev_nblocks > UINT16_MAX ?
[e91b898]461 UINT16_MAX : sb->dev_nblocks;
[99f043e]462 ind = MFS_BLOCKSIZE / sizeof(uint16_t);
463 ind2 = ind * ind;
464 sb->max_file_size = (V1_NR_DIRECT_ZONES + ind + ind2) * MFS_BLOCKSIZE;
[59e670e]465 } else {
466 /*Valid for MFS V2/V3*/
[99f043e]467 size_t ptrsize;
468 if (sb->fs_version == 2)
469 ptrsize = sizeof(uint16_t);
470 else
471 ptrsize = sizeof(uint32_t);
472 ind = sb->block_size / ptrsize;
473 ind2 = ind * ind;
474 zones = V2_NR_DIRECT_ZONES + ind + ind2;
475 sb->max_file_size = zones * sb->block_size;
[59e670e]476 sb->n_zones = sb->dev_nblocks > UINT32_MAX ?
[e91b898]477 UINT32_MAX : sb->dev_nblocks;
[19a057f9]478
[59e670e]479 if (sb->fs_version == 3) {
[99f043e]480 if(INT32_MAX / sb->block_size < zones)
481 sb->max_file_size = INT32_MAX;
[59e670e]482 sb->ino_per_block = V3_INODES_PER_BLOCK(sb->block_size);
483 sb->n_zones /= (sb->block_size / MFS_MIN_BLOCKSIZE);
484 }
485 }
[19a057f9]486
[e03a733]487 /*Round up the number of inodes to fill block size*/
[59e670e]488 if (sb->n_inodes == 0)
489 inodes = sb->dev_nblocks / 3;
[c64506b]490 else
491 inodes = sb->n_inodes;
[68ed0fb]492
[59e670e]493 if (inodes % sb->ino_per_block)
494 inodes = ((inodes / sb->ino_per_block) + 1) * sb->ino_per_block;
[e91b898]495
[59e670e]496 if (sb->fs_version < 3)
497 sb->n_inodes = inodes > UINT16_MAX ? UINT16_MAX : inodes;
498 else
499 sb->n_inodes = inodes > UINT32_MAX ? UINT32_MAX : inodes;
[68ed0fb]500
501 /*Compute inode bitmap size in blocks*/
[59e670e]502 sb->ibmap_blocks = UPPER(sb->n_inodes, sb->block_size * 8);
[68ed0fb]503
[e03a733]504 /*Compute inode table size*/
[410a065]505 sb->itable_size = sb->n_inodes / sb->ino_per_block;
[e03a733]506
[340b5690]507 /*Compute zone bitmap size in blocks*/
508 sb->zbmap_blocks = UPPER(sb->n_zones, sb->block_size * 8);
509
[68ed0fb]510 /*Compute first data zone position*/
[92dd5c8]511 sb->first_data_zone = 2 + sb->itable_size +
[e91b898]512 sb->zbmap_blocks + sb->ibmap_blocks;
[ae1ae27]513
514 /*Set log2 of zone to block ratio to zero*/
[59e670e]515 sb->log2_zone_size = 0;
[ae1ae27]516
[0b07acd]517 /*Check for errors*/
518 if (sb->first_data_zone >= sb->n_zones) {
519 printf(NAME ": Error! Insufficient disk space");
520 return ENOMEM;
521 }
522
[68ed0fb]523 /*Superblock is now ready to be written on disk*/
[86c03ba]524 printf(NAME ": %d block size\n", sb->block_size);
[59e670e]525 printf(NAME ": %d inodes\n", (uint32_t) sb->n_inodes);
526 printf(NAME ": %d zones\n", (uint32_t) sb->n_zones);
[410a065]527 printf(NAME ": inode table blocks = %ld\n", sb->itable_size);
528 printf(NAME ": inode bitmap blocks = %ld\n", sb->ibmap_blocks);
529 printf(NAME ": zone bitmap blocks = %ld\n", sb->zbmap_blocks);
[59e670e]530 printf(NAME ": first data zone = %d\n", (uint32_t) sb->first_data_zone);
[99f043e]531 printf(NAME ": max file size = %u\n", sb->max_file_size);
[59e670e]532 printf(NAME ": long fnames = %s\n", sb->longnames ? "Yes" : "No");
[bc99ed6]533
[59e670e]534 if (sb->fs_version == 3)
[0b07acd]535 rc = write_superblock3(sb);
[59e670e]536 else
[0b07acd]537 rc = write_superblock(sb);
538
539 return rc;
[eee8007]540}
541
[2eaf655]542/**Write the V1/V2 superblock on disk.
543 *
544 * @param sbi Pointer to the superblock structure to write on disk.
545 *
546 * @return EOK on success or a negative error code.
547 */
[197b671]548static int write_superblock(const struct mfs_sb_info *sbi)
[eee8007]549{
[59e670e]550 struct mfs_superblock *sb;
[0b07acd]551 int rc;
[e03a733]552
[b438804]553 sb = malloc(MFS_SUPERBLOCK_SIZE);;
[e03a733]554
[0b07acd]555 if (!sb)
556 return ENOMEM;
557
[59e670e]558 sb->s_ninodes = (uint16_t) sbi->n_inodes;
559 sb->s_nzones = (uint16_t) sbi->n_zones;
560 sb->s_nzones2 = (uint32_t) sbi->n_zones;
561 sb->s_ibmap_blocks = (uint16_t) sbi->ibmap_blocks;
562 sb->s_zbmap_blocks = (uint16_t) sbi->zbmap_blocks;
563 sb->s_first_data_zone = (uint16_t) sbi->first_data_zone;
564 sb->s_log2_zone_size = sbi->log2_zone_size;
[99f043e]565 sb->s_max_file_size = sbi->max_file_size;
[59e670e]566 sb->s_magic = sbi->magic;
567 sb->s_state = MFS_VALID_FS;
[e03a733]568
[f66d903]569 rc = write_block(MFS_SUPERBLOCK, 1, sb);
[8e41994]570 free(sb);
[0b07acd]571
572 return rc;
[59e670e]573}
[e03a733]574
[2eaf655]575/**Write the V3s superblock on disk.
576 *
577 * @param sbi Pointer to the superblock structure to write on disk.
578 *
579 * @return EOK on success or a negative error code.
580 */
[197b671]581static int write_superblock3(const struct mfs_sb_info *sbi)
[59e670e]582{
583 struct mfs3_superblock *sb;
[0b07acd]584 int rc;
[59e670e]585
[b438804]586 sb = malloc(MFS_SUPERBLOCK_SIZE);
[59e670e]587
[0b07acd]588 if (!sb)
589 return ENOMEM;
590
[59e670e]591 sb->s_ninodes = (uint32_t) sbi->n_inodes;
592 sb->s_nzones = (uint32_t) sbi->n_zones;
593 sb->s_ibmap_blocks = (uint16_t) sbi->ibmap_blocks;
594 sb->s_zbmap_blocks = (uint16_t) sbi->zbmap_blocks;
595 sb->s_first_data_zone = (uint16_t) sbi->first_data_zone;
596 sb->s_log2_zone_size = sbi->log2_zone_size;
[99f043e]597 sb->s_max_file_size = sbi->max_file_size;
[59e670e]598 sb->s_magic = sbi->magic;
599 sb->s_block_size = sbi->block_size;
[e03a733]600 sb->s_disk_version = 3;
[bc99ed6]601
[f66d903]602 rc = block_write_direct(handle, MFS_SUPERBLOCK << 1, 1 << 1, sb);
[410a065]603 free(sb);
[0b07acd]604
605 return rc;
[eee8007]606}
607
[2eaf655]608/**Initialize the inode and block bitmaps on disk.
609 *
610 * @param sb Pointer to the superblock structure.
611 *
612 * @return EOK on success or a negative error code.
613 */
[197b671]614static int init_bitmaps(const struct mfs_sb_info *sb)
[e6aaa59]615{
[bb0db564]616 uint32_t *ibmap_buf, *zbmap_buf;
617 uint8_t *ibmap_buf8, *zbmap_buf8;
[f66d903]618 const unsigned int ibmap_nblocks = sb->ibmap_blocks;
619 const unsigned int zbmap_nblocks = sb->zbmap_blocks;
[e6aaa59]620 unsigned int i;
[0b07acd]621 int rc;
[e6aaa59]622
[b438804]623 ibmap_buf = malloc(ibmap_nblocks * sb->block_size);
624 zbmap_buf = malloc(zbmap_nblocks * sb->block_size);
[e6aaa59]625
[0b07acd]626 if (!ibmap_buf || !zbmap_buf)
627 return ENOMEM;
628
[59e670e]629 memset(ibmap_buf, 0xFF, ibmap_nblocks * sb->block_size);
630 memset(zbmap_buf, 0xFF, zbmap_nblocks * sb->block_size);
[e6aaa59]631
[e33100c]632 for (i = 2; i < sb->n_inodes + 1; ++i)
[bb0db564]633 mark_bmap(ibmap_buf, i, FREE);
[f5cbd4f]634
[03fa85c]635 for (i = sb->first_data_zone + 1; i < sb->n_zones; ++i)
[bb0db564]636 mark_bmap(zbmap_buf, i, FREE);
[e6aaa59]637
[bb0db564]638 ibmap_buf8 = (uint8_t *) ibmap_buf;
639 zbmap_buf8 = (uint8_t *) zbmap_buf;
640
[f66d903]641 int start_block = 2;
[8ceba1e]642
[7df022e5]643 for (i = 0; i < ibmap_nblocks; ++i) {
[f66d903]644 if ((rc = write_block(start_block + i,
645 1, (ibmap_buf8 + i * sb->block_size))) != EOK)
[7df022e5]646 return rc;
647 }
648
[f66d903]649 start_block = 2 + ibmap_nblocks;
[8ceba1e]650
[7df022e5]651 for (i = 0; i < zbmap_nblocks; ++i) {
[f66d903]652 if ((rc = write_block(start_block + i,
653 1, (zbmap_buf8 + i * sb->block_size))) != EOK)
[7df022e5]654 return rc;
655 }
656
[c4c7f5a]657 free(ibmap_buf);
658 free(zbmap_buf);
659
[0b07acd]660 return rc;
[e6aaa59]661}
662
[2eaf655]663/**Mark a bitmap entry as used or free.
664 *
665 * @param bmap 32-bit pointer to the bitmap in memory.
666 * @param idx The index in the bitmap of the bit to set at 1 or 0.
667 * @param v FREE to clear the bit, USED to set the bit.
668 */
[59e670e]669static void mark_bmap(uint32_t *bmap, int idx, int v)
[e6aaa59]670{
671 if (v == FREE)
[59e670e]672 bmap[idx / 32] &= ~(1 << (idx % 32));
[e6aaa59]673 else
[59e670e]674 bmap[idx / 32] |= 1 << (idx % 32);
[e6aaa59]675}
676
[2eaf655]677/**Write a block on disk.
678 *
679 * @param off 64-bit block offset on disk.
680 * @param size size of the block.
681 * @param data Pointer to the block content.
682 *
683 * @return EOK on success or a negative error number.
684 */
[f66d903]685static inline int write_block(aoff64_t off, size_t size, const void *data)
[b84175a]686{
[f66d903]687 if (shift == 3) {
688 int rc;
689 aoff64_t tmp_off = off << 1;
690 uint8_t *data_ptr = (uint8_t *) data;
691
692 rc = block_write_direct(handle, tmp_off << 2, size << 2, data_ptr);
693
694 if (rc != EOK)
695 return rc;
696
697 data_ptr += 2048;
698 tmp_off++;
699
700 return block_write_direct(handle, tmp_off << 2, size << 2, data_ptr);
701 }
702 return block_write_direct(handle, off << shift, size << shift, data);
[b84175a]703}
704
[954bf385]705static void help_cmd_mkminix(help_level_t level)
706{
707 if (level == HELP_SHORT) {
708 printf(NAME": tool to create new Minix file systems\n");
709 } else {
[e91b898]710 printf("Usage: [options] device\n"
711 "-1 Make a Minix version 1 filesystem\n"
712 "-2 Make a Minix version 2 filesystem\n"
713 "-b ## Specify the block size in bytes (V3 only),\n"
714 " valid block size values are 1024, 2048 and 4096 bytes per block\n"
715 "-i ## Specify the number of inodes for the filesystem\n"
716 "-l Use 30-char long filenames (V1/V2 only)\n");
[954bf385]717 }
718}
719
720static int num_of_set_bits(uint32_t n)
721{
722 n = n - ((n >> 1) & 0x55555555);
723 n = (n & 0x33333333) + ((n >> 2) & 0x33333333);
724 return (((n + (n >> 4)) & 0xF0F0F0F) * 0x1010101) >> 24;
725}
726
727
728/**
729 * @}
730 */
Note: See TracBrowser for help on using the repository browser.