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

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

code cleanup

  • Property mode set to 100644
File size: 11.4 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 <str.h>
48#include <getopt.h>
49#include <mem.h>
[86d0b4b3]50#include <minix.h>
[954bf385]51
52#define NAME "mkminix"
53
[e6aaa59]54#define FREE 0
55#define USED 1
56
[ae1ae27]57#define UPPER(n, size) (((n) / (size)) + (((n) % (size)) != 0))
58
[954bf385]59typedef enum {
60 HELP_SHORT,
61 HELP_LONG
62} help_level_t;
63
[19a057f9]64typedef struct mfs_params {
[939b7d2]65 uint16_t fs_magic;
[19a057f9]66 uint32_t block_size;
67 size_t devblock_size;
68 unsigned long n_inodes;
69 aoff64_t dev_nblocks;
70 bool fs_longnames;
71} mfs_params_t;
72
[eee8007]73static void help_cmd_mkminix(help_level_t level);
74static int num_of_set_bits(uint32_t n);
[4cee3944]75static void setup_superblock(struct mfs_superblock *sb, mfs_params_t *opt);
76static void setup_superblock_v3(struct mfs3_superblock *sb, mfs_params_t *opt);
[9cfe0d5]77static void setup_bitmaps(devmap_handle_t handle, uint32_t ninodes,
78 uint32_t nzones, int bsize);
[e6aaa59]79static void mark_bmap(uint8_t *bmap, int idx, int v);
[954bf385]80
81static struct option const long_options[] = {
82 { "help", no_argument, 0, 'h' },
[eee8007]83 { "long-names", no_argument, 0, 'l' },
[954bf385]84 { "blocks", required_argument, 0, 'b' },
85 { "inodes", required_argument, 0, 'i' },
86 { NULL, no_argument, 0, '1' },
87 { NULL, no_argument, 0, '2' },
88 { NULL, no_argument, 0, '3' },
89 { 0, 0, 0, 0 }
90};
91
92int main (int argc, char **argv)
93{
94 int rc, c, opt_ind;
95 char *device_name;
96 devmap_handle_t handle;
[eee8007]97
[19a057f9]98 struct mfs_superblock *sb;
99 struct mfs3_superblock *sb3;
[954bf385]100
[eee8007]101 mfs_params_t opt;
[954bf385]102
[eee8007]103 /*Default is MinixFS V3*/
[939b7d2]104 opt.fs_magic = MFS_MAGIC_V3;
[eee8007]105
106 /*Default block size is 4Kb*/
[68ed0fb]107 opt.block_size = MFS_MAX_BLOCKSIZE;
[eee8007]108 opt.n_inodes = 0;
109 opt.fs_longnames = false;
[eaf9794]110
[954bf385]111 if (argc == 1) {
112 help_cmd_mkminix(HELP_SHORT);
113 printf("Incorrect number of arguments, try `mkminix --help'\n");
114 exit(0);
115 }
116
117 for (c = 0, optind = 0, opt_ind = 0; c != -1;) {
[e6aaa59]118 c = getopt_long(argc, argv, "lh123b:i:", long_options, &opt_ind);
[954bf385]119 switch (c) {
120 case 'h':
121 help_cmd_mkminix(HELP_LONG);
122 exit(0);
123 case '1':
[939b7d2]124 opt.fs_magic = MFS_MAGIC_V1;
[68ed0fb]125 opt.block_size = MFS_BLOCKSIZE;
[954bf385]126 break;
127 case '2':
[939b7d2]128 opt.fs_magic = MFS_MAGIC_V2;
[68ed0fb]129 opt.block_size = MFS_BLOCKSIZE;
[954bf385]130 break;
131 case '3':
[939b7d2]132 opt.fs_magic = MFS_MAGIC_V3;
[954bf385]133 break;
134 case 'b':
[eee8007]135 opt.block_size = (uint32_t) strtol(optarg, NULL, 10);
[954bf385]136 break;
137 case 'i':
[eee8007]138 opt.n_inodes = (unsigned long) strtol(optarg, NULL, 10);
139 break;
140 case 'l':
141 opt.fs_longnames = true;
[954bf385]142 break;
143 }
144 }
145
[68ed0fb]146 if (opt.block_size < MFS_MIN_BLOCKSIZE ||
147 opt.block_size > MFS_MAX_BLOCKSIZE) {
[954bf385]148 printf(NAME ":Error! Invalid block size.\n");
149 exit(0);
[eee8007]150 } else if (num_of_set_bits(opt.block_size) != 1) {
[eaf9794]151 /*Block size must be a power of 2.*/
[954bf385]152 printf(NAME ":Error! Invalid block size.\n");
153 exit(0);
[68ed0fb]154 } else if (opt.block_size > MFS_BLOCKSIZE &&
[939b7d2]155 opt.fs_magic != MFS_MAGIC_V3) {
[eee8007]156 printf(NAME ":Error! Block size > 1024 is supported by V3 filesystem only.\n");
157 exit(0);
[939b7d2]158 } else if (opt.fs_magic == MFS_MAGIC_V3 && opt.fs_longnames) {
[eee8007]159 printf(NAME ":Error! Long filenames are supported by V1/V2 filesystem only.\n");
[954bf385]160 exit(0);
161 }
162
163 argv += optind;
164
165 device_name = argv[0];
166
167 if (!device_name) {
168 help_cmd_mkminix(HELP_LONG);
169 exit(0);
170 }
171
172 rc = devmap_device_get_handle(device_name, &handle, 0);
173 if (rc != EOK) {
174 printf(NAME ": Error resolving device `%s'.\n", device_name);
175 return 2;
176 }
177
[68ed0fb]178 rc = block_init(handle, MFS_MIN_BLOCKSIZE);
[954bf385]179 if (rc != EOK) {
180 printf(NAME ": Error initializing libblock.\n");
181 return 2;
182 }
183
[eee8007]184 rc = block_get_bsize(handle, &opt.devblock_size);
[954bf385]185 if (rc != EOK) {
186 printf(NAME ": Error determining device block size.\n");
187 return 2;
188 }
189
[eee8007]190 rc = block_get_nblocks(handle, &opt.dev_nblocks);
[954bf385]191 if (rc != EOK) {
192 printf(NAME ": Warning, failed to obtain block device size.\n");
193 } else {
194 printf(NAME ": Block device has %" PRIuOFF64 " blocks.\n",
[eee8007]195 opt.dev_nblocks);
[954bf385]196 }
197
[eee8007]198 if (opt.devblock_size != 512) {
[954bf385]199 printf(NAME ": Error. Device block size is not 512 bytes.\n");
200 return 2;
201 }
202
[68ed0fb]203 /*Minimum block size is 1 Kb*/
204 opt.dev_nblocks /= 2;
205
206 printf(NAME ": Creating Minix file system on device\n");
[954bf385]207
[4cee3944]208 /*Setting up superblock*/
[eaf9794]209
[939b7d2]210 if (opt.fs_magic == MFS_MAGIC_V3) {
[19a057f9]211 sb3 = (struct mfs3_superblock *) malloc(sizeof(struct mfs3_superblock));
212 if (!sb3) {
213 printf(NAME ": Error, not enough memory");
214 return 2;
215 }
[4cee3944]216 setup_superblock_v3(sb3, &opt);
[e03a733]217 block_write_direct(handle, MFS_SUPERBLOCK, 1, sb3);
[9cfe0d5]218 setup_bitmaps(handle, sb3->s_ninodes,
[e03a733]219 sb3->s_nzones, sb3->s_block_size);
[eee8007]220 } else {
[19a057f9]221 sb = (struct mfs_superblock *) malloc(sizeof(struct mfs_superblock));
222 if (!sb) {
223 printf(NAME ": Error, not enough memory");
224 return 2;
225 }
[4cee3944]226 setup_superblock(sb, &opt);
[68ed0fb]227 block_write_direct(handle, MFS_SUPERBLOCK, 1, sb);
[9cfe0d5]228 setup_bitmaps(handle, sb->s_ninodes, sb->s_nzones, MFS_BLOCKSIZE);
[eee8007]229 }
230
[954bf385]231 return 0;
232}
233
[4cee3944]234static void setup_superblock(struct mfs_superblock *sb, mfs_params_t *opt)
[eee8007]235{
[68ed0fb]236 int ino_per_block = 0;
237 int fs_version;
[e2267e82]238 aoff64_t inodes;
[68ed0fb]239
240 if (opt->fs_magic == MFS_MAGIC_V1) {
241 fs_version = 1;
242 ino_per_block = V1_INODES_PER_BLOCK;
243 if (opt->fs_longnames)
[939b7d2]244 opt->fs_magic = MFS_MAGIC_V1L;
[68ed0fb]245 } else {
246 fs_version = 2;
[e03a733]247 ino_per_block = V2_INODES_PER_BLOCK;
[68ed0fb]248 if (opt->fs_longnames)
[939b7d2]249 opt->fs_magic = MFS_MAGIC_V2L;
[19a057f9]250 }
251
[939b7d2]252 sb->s_magic = opt->fs_magic;
253
[e03a733]254 /*Compute the number of zones on disk*/
255
[19a057f9]256 /*Valid only for MFS V1*/
257 sb->s_nzones = opt->dev_nblocks > UINT16_MAX ?
258 UINT16_MAX : opt->dev_nblocks;
259
260 /*Valid only for MFS V2*/
261 sb->s_nzones2 = opt->dev_nblocks > UINT32_MAX ?
262 UINT32_MAX : opt->dev_nblocks;
263
[e03a733]264 /*Round up the number of inodes to fill block size*/
[68ed0fb]265 if (opt->n_inodes == 0)
[e2267e82]266 inodes = opt->dev_nblocks / 3;
[68ed0fb]267 else
[e2267e82]268 inodes = opt->n_inodes;
[68ed0fb]269
[e2267e82]270 if (inodes % ino_per_block)
271 inodes = ((inodes / ino_per_block) + 1) * ino_per_block;
272 sb->s_ninodes = inodes > UINT16_MAX ? UINT16_MAX : inodes;
[68ed0fb]273
274 /*Compute inode bitmap size in blocks*/
[ae1ae27]275 sb->s_ibmap_blocks = UPPER(sb->s_ninodes, MFS_BLOCKSIZE * 8);
[68ed0fb]276
277 /*Compute zone bitmap size in blocks*/
278 if (fs_version == 1)
[ae1ae27]279 sb->s_zbmap_blocks = UPPER(sb->s_nzones, MFS_BLOCKSIZE * 8);
[68ed0fb]280 else
[ae1ae27]281 sb->s_zbmap_blocks = UPPER(sb->s_nzones2, MFS_BLOCKSIZE * 8);
[68ed0fb]282
[e03a733]283 /*Compute inode table size*/
284 unsigned long ninodes_blocks = sb->s_ninodes / ino_per_block;
285
[68ed0fb]286 /*Compute first data zone position*/
[ae1ae27]287 sb->s_first_data_zone = 2 + ninodes_blocks +
288 sb->s_zbmap_blocks + sb->s_ibmap_blocks;
289
290 /*Set log2 of zone to block ratio to zero*/
[19a057f9]291 sb->s_log2_zone_size = 0;
[ae1ae27]292
[68ed0fb]293 /*Superblock is now ready to be written on disk*/
294 printf(NAME ": %d inodes\n", sb->s_ninodes);
295 printf(NAME ": %d zones\n", sb->s_nzones2);
[ae1ae27]296 printf(NAME ": inode table blocks = %ld\n", ninodes_blocks);
297 printf(NAME ": first data zone = %d\n", sb->s_first_data_zone);
[e03a733]298 printf(NAME ": long fnames = %s\n", opt->fs_longnames ? "Yes" : "No");
[eee8007]299}
300
[4cee3944]301static void setup_superblock_v3(struct mfs3_superblock *sb, mfs_params_t *opt)
[eee8007]302{
[e03a733]303 int ino_per_block;
304 int bs;
[e2267e82]305 aoff64_t inodes;
[e03a733]306
307 sb->s_magic = opt->fs_magic;
308 bs = opt->block_size;
309
310 if (opt->n_inodes == 0)
[e2267e82]311 inodes = opt->dev_nblocks / 3;
[e03a733]312 else
[e2267e82]313 inodes = opt->n_inodes;
314
315 /*Round up the number of inodes to fill block size*/
316 ino_per_block = V3_INODES_PER_BLOCK(bs);
317 if (inodes % ino_per_block)
318 inodes = ((inodes / ino_per_block) + 1) * ino_per_block;
319 sb->s_ninodes = inodes > UINT32_MAX ? UINT32_MAX : inodes;
[e03a733]320
321 /*Compute the number of zones on disk*/
322 sb->s_nzones = opt->dev_nblocks > UINT32_MAX ?
323 UINT32_MAX : opt->dev_nblocks;
324 sb->s_nzones /= (bs / MFS_MIN_BLOCKSIZE);
325
326 /*Compute inode bitmap size in blocks*/
327 sb->s_ibmap_blocks = UPPER(sb->s_ninodes, bs * 8);
328
329 /*Compute zone bitmap size in blocks*/
330 sb->s_zbmap_blocks = UPPER(sb->s_nzones, bs * 8);
331
332 /*Compute inode table size*/
333 unsigned long ninodes_blocks = sb->s_ninodes / ino_per_block;
334
335 /*Compute first data zone position*/
336 sb->s_first_data_zone = 2 + ninodes_blocks +
337 sb->s_zbmap_blocks + sb->s_ibmap_blocks;
338
339 /*Set log2 of zone to block ratio to zero*/
340 sb->s_log2_zone_size = 0;
341 sb->s_disk_version = 3;
342 sb->s_block_size = bs;
343
344 /*Superblock is now ready to be written on disk*/
345 printf(NAME ": %d inodes\n", sb->s_ninodes);
346 printf(NAME ": %d zones\n", sb->s_nzones);
347 printf(NAME ": block size = %d\n", sb->s_block_size);
348 printf(NAME ": inode table blocks = %ld\n", ninodes_blocks);
349 printf(NAME ": first data zone = %d\n", sb->s_first_data_zone);
[eee8007]350}
351
[9cfe0d5]352static void setup_bitmaps(devmap_handle_t handle, uint32_t ninodes,
353 uint32_t nzones, int bsize)
[e6aaa59]354{
355 uint8_t *ibmap_buf, *zbmap_buf;
[9cfe0d5]356 int ibmap_nblocks = 1 + (ninodes / 8) / bsize;
357 int zbmap_nblocks = 1 + (nzones / 8) / bsize;
[e6aaa59]358 unsigned int i;
359
[9cfe0d5]360 ibmap_buf = (uint8_t *) malloc(ibmap_nblocks * bsize);
361 zbmap_buf = (uint8_t *) malloc(zbmap_nblocks * bsize);
[e6aaa59]362
[9cfe0d5]363 memset(ibmap_buf, 0xFF, ibmap_nblocks * bsize);
364 memset(zbmap_buf, 0xFF, zbmap_nblocks * bsize);
[e6aaa59]365
366 for (i = 2; i < ninodes; ++i) {
367 mark_bmap(ibmap_buf, i, FREE);
368 mark_bmap(zbmap_buf, i, FREE);
369 }
370
[9cfe0d5]371 ibmap_nblocks *= bsize / MFS_BLOCKSIZE;
372 zbmap_nblocks *= bsize / MFS_BLOCKSIZE;
373
[e6aaa59]374 block_write_direct(handle, 2, ibmap_nblocks, ibmap_buf);
375 block_write_direct(handle, 2 + ibmap_nblocks, zbmap_nblocks, zbmap_buf);
376}
377
378static void mark_bmap(uint8_t *bmap, int idx, int v)
379{
380 if (v == FREE)
381 bmap[idx / 8] &= ~(1 << (idx % 8));
382 else
383 bmap[idx / 8] |= 1 << (idx % 8);
384}
385
[954bf385]386static void help_cmd_mkminix(help_level_t level)
387{
388 if (level == HELP_SHORT) {
389 printf(NAME": tool to create new Minix file systems\n");
390 } else {
391 printf("Usage: [options] device\n"
392 "-1 Make a Minix version 1 filesystem\n"
393 "-2 Make a Minix version 2 filesystem\n"
394 "-3 Make a Minix version 3 filesystem\n"
395 "-b ## Specify the block size in bytes (V3 only),\n"
[eaf9794]396 " valid block size values are 1024, 2048 and 4096 bytes per block\n"
[eee8007]397 "-i ## Specify the number of inodes for the filesystem\n"
[82650385]398 "-l Use 30-char long filenames (V1/V2 only)\n");
[954bf385]399 }
400}
401
402static int num_of_set_bits(uint32_t n)
403{
404 n = n - ((n >> 1) & 0x55555555);
405 n = (n & 0x33333333) + ((n >> 2) & 0x33333333);
406 return (((n + (n >> 4)) & 0xF0F0F0F) * 0x1010101) >> 24;
407}
408
409
410/**
411 * @}
412 */
Note: See TracBrowser for help on using the repository browser.