source: mainline/uspace/app/mkminix/mkminix.c@ 9cfe0d5

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

fix bug when initializing bitmaps

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