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
Line 
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>
50#include <minix.h>
51
52#define NAME "mkminix"
53
54#define FREE 0
55#define USED 1
56
57typedef enum {
58 HELP_SHORT,
59 HELP_LONG
60} help_level_t;
61
62typedef struct mfs_params {
63 uint16_t fs_magic;
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
71static void help_cmd_mkminix(help_level_t level);
72static int num_of_set_bits(uint32_t n);
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);
75static void setup_bitmaps(devmap_handle_t handle, uint32_t ninodes,
76 uint32_t nzones, int bsize);
77static void mark_bmap(uint8_t *bmap, int idx, int v);
78
79static struct option const long_options[] = {
80 { "help", no_argument, 0, 'h' },
81 { "long-names", no_argument, 0, 'l' },
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;
95
96 struct mfs_superblock *sb;
97 struct mfs3_superblock *sb3;
98
99 mfs_params_t opt;
100
101 /*Default is MinixFS V3*/
102 opt.fs_magic = MFS_MAGIC_V3;
103
104 /*Default block size is 4Kb*/
105 opt.block_size = MFS_MAX_BLOCKSIZE;
106 opt.n_inodes = 0;
107 opt.fs_longnames = false;
108
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;) {
116 c = getopt_long(argc, argv, "lh123b:i:", long_options, &opt_ind);
117 switch (c) {
118 case 'h':
119 help_cmd_mkminix(HELP_LONG);
120 exit(0);
121 case '1':
122 opt.fs_magic = MFS_MAGIC_V1;
123 opt.block_size = MFS_BLOCKSIZE;
124 break;
125 case '2':
126 opt.fs_magic = MFS_MAGIC_V2;
127 opt.block_size = MFS_BLOCKSIZE;
128 break;
129 case '3':
130 opt.fs_magic = MFS_MAGIC_V3;
131 break;
132 case 'b':
133 opt.block_size = (uint32_t) strtol(optarg, NULL, 10);
134 break;
135 case 'i':
136 opt.n_inodes = (unsigned long) strtol(optarg, NULL, 10);
137 break;
138 case 'l':
139 opt.fs_longnames = true;
140 break;
141 }
142 }
143
144 if (opt.block_size < MFS_MIN_BLOCKSIZE ||
145 opt.block_size > MFS_MAX_BLOCKSIZE) {
146 printf(NAME ":Error! Invalid block size.\n");
147 exit(0);
148 } else if (num_of_set_bits(opt.block_size) != 1) {
149 /*Block size must be a power of 2.*/
150 printf(NAME ":Error! Invalid block size.\n");
151 exit(0);
152 } else if (opt.block_size > MFS_BLOCKSIZE &&
153 opt.fs_magic != MFS_MAGIC_V3) {
154 printf(NAME ":Error! Block size > 1024 is supported by V3 filesystem only.\n");
155 exit(0);
156 } else if (opt.fs_magic == MFS_MAGIC_V3 && opt.fs_longnames) {
157 printf(NAME ":Error! Long filenames are supported by V1/V2 filesystem only.\n");
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
176 rc = block_init(handle, MFS_MIN_BLOCKSIZE);
177 if (rc != EOK) {
178 printf(NAME ": Error initializing libblock.\n");
179 return 2;
180 }
181
182 rc = block_get_bsize(handle, &opt.devblock_size);
183 if (rc != EOK) {
184 printf(NAME ": Error determining device block size.\n");
185 return 2;
186 }
187
188 rc = block_get_nblocks(handle, &opt.dev_nblocks);
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",
193 opt.dev_nblocks);
194 }
195
196 if (opt.devblock_size != 512) {
197 printf(NAME ": Error. Device block size is not 512 bytes.\n");
198 return 2;
199 }
200
201 /*Minimum block size is 1 Kb*/
202 opt.dev_nblocks /= 2;
203
204 printf(NAME ": Creating Minix file system on device\n");
205
206 /*Setting up superblock*/
207
208 if (opt.fs_magic == MFS_MAGIC_V3) {
209 sb3 = (struct mfs3_superblock *) malloc(sizeof(struct mfs3_superblock));
210 if (!sb3) {
211 printf(NAME ": Error, not enough memory");
212 return 2;
213 }
214 setup_superblock_v3(sb3, &opt);
215 setup_bitmaps(handle, sb3->s_ninodes,
216 sb3->s_total_zones, sb3->s_block_size);
217 } else {
218 sb = (struct mfs_superblock *) malloc(sizeof(struct mfs_superblock));
219 if (!sb) {
220 printf(NAME ": Error, not enough memory");
221 return 2;
222 }
223 setup_superblock(sb, &opt);
224 block_write_direct(handle, MFS_SUPERBLOCK, 1, sb);
225 setup_bitmaps(handle, sb->s_ninodes, sb->s_nzones, MFS_BLOCKSIZE);
226 }
227
228 return 0;
229}
230
231static void setup_superblock(struct mfs_superblock *sb, mfs_params_t *opt)
232{
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)
241 opt->fs_magic = MFS_MAGIC_V1L;
242 } else {
243 fs_version = 2;
244 ino_per_block = V1_INODES_PER_BLOCK;
245 if (opt->fs_longnames)
246 opt->fs_magic = MFS_MAGIC_V2L;
247 }
248
249 sb->s_magic = opt->fs_magic;
250
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
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*/
270 sb->s_ibmap_blocks = 1 + (sb->s_ninodes / (MFS_BLOCKSIZE * 8));
271
272 /*Compute zone bitmap size in blocks*/
273 if (fs_version == 1)
274 sb->s_zbmap_blocks = 1 + (sb->s_nzones / (MFS_BLOCKSIZE * 8));
275 else
276 sb->s_zbmap_blocks = 1 + (sb->s_nzones2 / (MFS_BLOCKSIZE * 8));
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;
284 sb->s_log2_zone_size = 0;
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);
289}
290
291static void setup_superblock_v3(struct mfs3_superblock *sb, mfs_params_t *opt)
292{
293}
294
295static void setup_bitmaps(devmap_handle_t handle, uint32_t ninodes,
296 uint32_t nzones, int bsize)
297{
298 uint8_t *ibmap_buf, *zbmap_buf;
299 int ibmap_nblocks = 1 + (ninodes / 8) / bsize;
300 int zbmap_nblocks = 1 + (nzones / 8) / bsize;
301 unsigned int i;
302
303 ibmap_buf = (uint8_t *) malloc(ibmap_nblocks * bsize);
304 zbmap_buf = (uint8_t *) malloc(zbmap_nblocks * bsize);
305
306 memset(ibmap_buf, 0xFF, ibmap_nblocks * bsize);
307 memset(zbmap_buf, 0xFF, zbmap_nblocks * bsize);
308
309 for (i = 2; i < ninodes; ++i) {
310 mark_bmap(ibmap_buf, i, FREE);
311 mark_bmap(zbmap_buf, i, FREE);
312 }
313
314 ibmap_nblocks *= bsize / MFS_BLOCKSIZE;
315 zbmap_nblocks *= bsize / MFS_BLOCKSIZE;
316
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
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"
339 " valid block size values are 1024, 2048 and 4096 bytes per block\n"
340 "-i ## Specify the number of inodes for the filesystem\n"
341 "-l Use 30-char long filenames (V1/V2 only)\n");
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.