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 |
|
---|
57 | #define UPPER(n, size) (((n) / (size)) + (((n) % (size)) != 0))
|
---|
58 |
|
---|
59 | typedef enum {
|
---|
60 | HELP_SHORT,
|
---|
61 | HELP_LONG
|
---|
62 | } help_level_t;
|
---|
63 |
|
---|
64 | typedef struct mfs_params {
|
---|
65 | uint16_t fs_magic;
|
---|
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 |
|
---|
73 | static void help_cmd_mkminix(help_level_t level);
|
---|
74 | static int num_of_set_bits(uint32_t n);
|
---|
75 | static void setup_superblock(struct mfs_superblock *sb, mfs_params_t *opt);
|
---|
76 | static void setup_superblock_v3(struct mfs3_superblock *sb, mfs_params_t *opt);
|
---|
77 | static void setup_bitmaps(devmap_handle_t handle, uint32_t ninodes,
|
---|
78 | uint32_t nzones, int bsize);
|
---|
79 | static void mark_bmap(uint8_t *bmap, int idx, int v);
|
---|
80 |
|
---|
81 | static struct option const long_options[] = {
|
---|
82 | { "help", no_argument, 0, 'h' },
|
---|
83 | { "long-names", no_argument, 0, 'l' },
|
---|
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 |
|
---|
92 | int main (int argc, char **argv)
|
---|
93 | {
|
---|
94 | int rc, c, opt_ind;
|
---|
95 | char *device_name;
|
---|
96 | devmap_handle_t handle;
|
---|
97 |
|
---|
98 | struct mfs_superblock *sb;
|
---|
99 | struct mfs3_superblock *sb3;
|
---|
100 |
|
---|
101 | mfs_params_t opt;
|
---|
102 |
|
---|
103 | /*Default is MinixFS V3*/
|
---|
104 | opt.fs_magic = MFS_MAGIC_V3;
|
---|
105 |
|
---|
106 | /*Default block size is 4Kb*/
|
---|
107 | opt.block_size = MFS_MAX_BLOCKSIZE;
|
---|
108 | opt.n_inodes = 0;
|
---|
109 | opt.fs_longnames = false;
|
---|
110 |
|
---|
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;) {
|
---|
118 | c = getopt_long(argc, argv, "lh123b:i:", long_options, &opt_ind);
|
---|
119 | switch (c) {
|
---|
120 | case 'h':
|
---|
121 | help_cmd_mkminix(HELP_LONG);
|
---|
122 | exit(0);
|
---|
123 | case '1':
|
---|
124 | opt.fs_magic = MFS_MAGIC_V1;
|
---|
125 | opt.block_size = MFS_BLOCKSIZE;
|
---|
126 | break;
|
---|
127 | case '2':
|
---|
128 | opt.fs_magic = MFS_MAGIC_V2;
|
---|
129 | opt.block_size = MFS_BLOCKSIZE;
|
---|
130 | break;
|
---|
131 | case '3':
|
---|
132 | opt.fs_magic = MFS_MAGIC_V3;
|
---|
133 | break;
|
---|
134 | case 'b':
|
---|
135 | opt.block_size = (uint32_t) strtol(optarg, NULL, 10);
|
---|
136 | break;
|
---|
137 | case 'i':
|
---|
138 | opt.n_inodes = (unsigned long) strtol(optarg, NULL, 10);
|
---|
139 | break;
|
---|
140 | case 'l':
|
---|
141 | opt.fs_longnames = true;
|
---|
142 | break;
|
---|
143 | }
|
---|
144 | }
|
---|
145 |
|
---|
146 | if (opt.block_size < MFS_MIN_BLOCKSIZE ||
|
---|
147 | opt.block_size > MFS_MAX_BLOCKSIZE) {
|
---|
148 | printf(NAME ":Error! Invalid block size.\n");
|
---|
149 | exit(0);
|
---|
150 | } else if (num_of_set_bits(opt.block_size) != 1) {
|
---|
151 | /*Block size must be a power of 2.*/
|
---|
152 | printf(NAME ":Error! Invalid block size.\n");
|
---|
153 | exit(0);
|
---|
154 | } else if (opt.block_size > MFS_BLOCKSIZE &&
|
---|
155 | opt.fs_magic != MFS_MAGIC_V3) {
|
---|
156 | printf(NAME ":Error! Block size > 1024 is supported by V3 filesystem only.\n");
|
---|
157 | exit(0);
|
---|
158 | } else if (opt.fs_magic == MFS_MAGIC_V3 && opt.fs_longnames) {
|
---|
159 | printf(NAME ":Error! Long filenames are supported by V1/V2 filesystem only.\n");
|
---|
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 |
|
---|
178 | rc = block_init(handle, MFS_MIN_BLOCKSIZE);
|
---|
179 | if (rc != EOK) {
|
---|
180 | printf(NAME ": Error initializing libblock.\n");
|
---|
181 | return 2;
|
---|
182 | }
|
---|
183 |
|
---|
184 | rc = block_get_bsize(handle, &opt.devblock_size);
|
---|
185 | if (rc != EOK) {
|
---|
186 | printf(NAME ": Error determining device block size.\n");
|
---|
187 | return 2;
|
---|
188 | }
|
---|
189 |
|
---|
190 | rc = block_get_nblocks(handle, &opt.dev_nblocks);
|
---|
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",
|
---|
195 | opt.dev_nblocks);
|
---|
196 | }
|
---|
197 |
|
---|
198 | if (opt.devblock_size != 512) {
|
---|
199 | printf(NAME ": Error. Device block size is not 512 bytes.\n");
|
---|
200 | return 2;
|
---|
201 | }
|
---|
202 |
|
---|
203 | /*Minimum block size is 1 Kb*/
|
---|
204 | opt.dev_nblocks /= 2;
|
---|
205 |
|
---|
206 | printf(NAME ": Creating Minix file system on device\n");
|
---|
207 |
|
---|
208 | /*Setting up superblock*/
|
---|
209 |
|
---|
210 | if (opt.fs_magic == MFS_MAGIC_V3) {
|
---|
211 | sb3 = (struct mfs3_superblock *) malloc(sizeof(struct mfs3_superblock));
|
---|
212 | if (!sb3) {
|
---|
213 | printf(NAME ": Error, not enough memory");
|
---|
214 | return 2;
|
---|
215 | }
|
---|
216 | setup_superblock_v3(sb3, &opt);
|
---|
217 | block_write_direct(handle, MFS_SUPERBLOCK, 1, sb3);
|
---|
218 | setup_bitmaps(handle, sb3->s_ninodes,
|
---|
219 | sb3->s_nzones, sb3->s_block_size);
|
---|
220 | } else {
|
---|
221 | sb = (struct mfs_superblock *) malloc(sizeof(struct mfs_superblock));
|
---|
222 | if (!sb) {
|
---|
223 | printf(NAME ": Error, not enough memory");
|
---|
224 | return 2;
|
---|
225 | }
|
---|
226 | setup_superblock(sb, &opt);
|
---|
227 | block_write_direct(handle, MFS_SUPERBLOCK, 1, sb);
|
---|
228 | setup_bitmaps(handle, sb->s_ninodes, sb->s_nzones, MFS_BLOCKSIZE);
|
---|
229 | }
|
---|
230 |
|
---|
231 | return 0;
|
---|
232 | }
|
---|
233 |
|
---|
234 | static void setup_superblock(struct mfs_superblock *sb, mfs_params_t *opt)
|
---|
235 | {
|
---|
236 | int ino_per_block = 0;
|
---|
237 | int fs_version;
|
---|
238 | aoff64_t inodes;
|
---|
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)
|
---|
244 | opt->fs_magic = MFS_MAGIC_V1L;
|
---|
245 | } else {
|
---|
246 | fs_version = 2;
|
---|
247 | ino_per_block = V2_INODES_PER_BLOCK;
|
---|
248 | if (opt->fs_longnames)
|
---|
249 | opt->fs_magic = MFS_MAGIC_V2L;
|
---|
250 | }
|
---|
251 |
|
---|
252 | sb->s_magic = opt->fs_magic;
|
---|
253 |
|
---|
254 | /*Compute the number of zones on disk*/
|
---|
255 |
|
---|
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 |
|
---|
264 | /*Round up the number of inodes to fill block size*/
|
---|
265 | if (opt->n_inodes == 0)
|
---|
266 | inodes = opt->dev_nblocks / 3;
|
---|
267 | else
|
---|
268 | inodes = opt->n_inodes;
|
---|
269 |
|
---|
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;
|
---|
273 |
|
---|
274 | /*Compute inode bitmap size in blocks*/
|
---|
275 | sb->s_ibmap_blocks = UPPER(sb->s_ninodes, MFS_BLOCKSIZE * 8);
|
---|
276 |
|
---|
277 | /*Compute zone bitmap size in blocks*/
|
---|
278 | if (fs_version == 1)
|
---|
279 | sb->s_zbmap_blocks = UPPER(sb->s_nzones, MFS_BLOCKSIZE * 8);
|
---|
280 | else
|
---|
281 | sb->s_zbmap_blocks = UPPER(sb->s_nzones2, MFS_BLOCKSIZE * 8);
|
---|
282 |
|
---|
283 | /*Compute inode table size*/
|
---|
284 | unsigned long ninodes_blocks = sb->s_ninodes / ino_per_block;
|
---|
285 |
|
---|
286 | /*Compute first data zone position*/
|
---|
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*/
|
---|
291 | sb->s_log2_zone_size = 0;
|
---|
292 |
|
---|
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);
|
---|
296 | printf(NAME ": inode table blocks = %ld\n", ninodes_blocks);
|
---|
297 | printf(NAME ": first data zone = %d\n", sb->s_first_data_zone);
|
---|
298 | printf(NAME ": long fnames = %s\n", opt->fs_longnames ? "Yes" : "No");
|
---|
299 | }
|
---|
300 |
|
---|
301 | static void setup_superblock_v3(struct mfs3_superblock *sb, mfs_params_t *opt)
|
---|
302 | {
|
---|
303 | int ino_per_block;
|
---|
304 | int bs;
|
---|
305 | aoff64_t inodes;
|
---|
306 |
|
---|
307 | sb->s_magic = opt->fs_magic;
|
---|
308 | bs = opt->block_size;
|
---|
309 |
|
---|
310 | if (opt->n_inodes == 0)
|
---|
311 | inodes = opt->dev_nblocks / 3;
|
---|
312 | else
|
---|
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;
|
---|
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);
|
---|
350 | }
|
---|
351 |
|
---|
352 | static void setup_bitmaps(devmap_handle_t handle, uint32_t ninodes,
|
---|
353 | uint32_t nzones, int bsize)
|
---|
354 | {
|
---|
355 | uint8_t *ibmap_buf, *zbmap_buf;
|
---|
356 | int ibmap_nblocks = 1 + (ninodes / 8) / bsize;
|
---|
357 | int zbmap_nblocks = 1 + (nzones / 8) / bsize;
|
---|
358 | unsigned int i;
|
---|
359 |
|
---|
360 | ibmap_buf = (uint8_t *) malloc(ibmap_nblocks * bsize);
|
---|
361 | zbmap_buf = (uint8_t *) malloc(zbmap_nblocks * bsize);
|
---|
362 |
|
---|
363 | memset(ibmap_buf, 0xFF, ibmap_nblocks * bsize);
|
---|
364 | memset(zbmap_buf, 0xFF, zbmap_nblocks * bsize);
|
---|
365 |
|
---|
366 | for (i = 2; i < ninodes; ++i) {
|
---|
367 | mark_bmap(ibmap_buf, i, FREE);
|
---|
368 | mark_bmap(zbmap_buf, i, FREE);
|
---|
369 | }
|
---|
370 |
|
---|
371 | ibmap_nblocks *= bsize / MFS_BLOCKSIZE;
|
---|
372 | zbmap_nblocks *= bsize / MFS_BLOCKSIZE;
|
---|
373 |
|
---|
374 | block_write_direct(handle, 2, ibmap_nblocks, ibmap_buf);
|
---|
375 | block_write_direct(handle, 2 + ibmap_nblocks, zbmap_nblocks, zbmap_buf);
|
---|
376 | }
|
---|
377 |
|
---|
378 | static 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 |
|
---|
386 | static 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"
|
---|
396 | " valid block size values are 1024, 2048 and 4096 bytes per block\n"
|
---|
397 | "-i ## Specify the number of inodes for the filesystem\n"
|
---|
398 | "-l Use 30-char long filenames (V1/V2 only)\n");
|
---|
399 | }
|
---|
400 | }
|
---|
401 |
|
---|
402 | static 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 | */
|
---|