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>
|
---|
49 | #include <str.h>
|
---|
50 | #include <time.h>
|
---|
51 | #include <minix.h>
|
---|
52 |
|
---|
53 | #define NAME "mkminix"
|
---|
54 |
|
---|
55 | #define FREE 0
|
---|
56 | #define USED 1
|
---|
57 |
|
---|
58 | #define UPPER(n, size) (((n) / (size)) + (((n) % (size)) != 0))
|
---|
59 | #define NEXT_DENTRY(p, dirsize) (p += (dirsize))
|
---|
60 |
|
---|
61 | typedef enum {
|
---|
62 | HELP_SHORT,
|
---|
63 | HELP_LONG
|
---|
64 | } help_level_t;
|
---|
65 |
|
---|
66 | /*Generic MFS superblock*/
|
---|
67 | struct 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;
|
---|
74 | unsigned long itable_size;
|
---|
75 | int log2_zone_size;
|
---|
76 | int ino_per_block;
|
---|
77 | int dirsize;
|
---|
78 | uint32_t max_file_size;
|
---|
79 | uint16_t magic;
|
---|
80 | uint32_t block_size;
|
---|
81 | int fs_version;
|
---|
82 | bool longnames;
|
---|
83 | };
|
---|
84 |
|
---|
85 | static void help_cmd_mkminix(help_level_t level);
|
---|
86 | static int num_of_set_bits(uint32_t n);
|
---|
87 | static int init_superblock(struct mfs_sb_info *sb);
|
---|
88 | static int write_superblock(const struct mfs_sb_info *sbi);
|
---|
89 | static int write_superblock3(const struct mfs_sb_info *sbi);
|
---|
90 | static int init_bitmaps(const struct mfs_sb_info *sb);
|
---|
91 | static int init_inode_table(const struct mfs_sb_info *sb);
|
---|
92 | static int make_root_ino(const struct mfs_sb_info *sb);
|
---|
93 | static int make_root_ino3(const struct mfs_sb_info *sb);
|
---|
94 | static void mark_bmap(uint32_t *bmap, int idx, int v);
|
---|
95 | static int insert_dentries(const struct mfs_sb_info *sb);
|
---|
96 |
|
---|
97 | static inline int write_block(aoff64_t off, size_t size, const void *data);
|
---|
98 |
|
---|
99 | static devmap_handle_t handle;
|
---|
100 | static int shift;
|
---|
101 |
|
---|
102 | static struct option const long_options[] = {
|
---|
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 | { NULL, no_argument, 0, '3' },
|
---|
110 | { 0, 0, 0, 0 }
|
---|
111 | };
|
---|
112 |
|
---|
113 | int main (int argc, char **argv)
|
---|
114 | {
|
---|
115 | int rc, c, opt_ind;
|
---|
116 | char *device_name;
|
---|
117 | aoff64_t devblock_size;
|
---|
118 |
|
---|
119 | struct mfs_sb_info sb;
|
---|
120 |
|
---|
121 | /*Default is MinixFS V3*/
|
---|
122 | sb.magic = MFS_MAGIC_V3;
|
---|
123 | sb.fs_version = 3;
|
---|
124 |
|
---|
125 | /*Default block size is 4Kb*/
|
---|
126 | sb.block_size = MFS_MAX_BLOCKSIZE;
|
---|
127 | sb.dirsize = MFS3_DIRSIZE;
|
---|
128 | sb.n_inodes = 0;
|
---|
129 | sb.longnames = false;
|
---|
130 | sb.ino_per_block = V3_INODES_PER_BLOCK(MFS_MAX_BLOCKSIZE);
|
---|
131 |
|
---|
132 | if (argc == 1) {
|
---|
133 | help_cmd_mkminix(HELP_SHORT);
|
---|
134 | printf("Incorrect number of arguments, try `mkminix --help'\n");
|
---|
135 | exit(0);
|
---|
136 | }
|
---|
137 |
|
---|
138 | for (c = 0, optind = 0, opt_ind = 0; c != -1;) {
|
---|
139 | c = getopt_long(argc, argv, "lh123b:i:", long_options, &opt_ind);
|
---|
140 | switch (c) {
|
---|
141 | case 'h':
|
---|
142 | help_cmd_mkminix(HELP_LONG);
|
---|
143 | exit(0);
|
---|
144 | case '1':
|
---|
145 | sb.magic = MFS_MAGIC_V1;
|
---|
146 | sb.block_size = MFS_BLOCKSIZE;
|
---|
147 | sb.fs_version = 1;
|
---|
148 | sb.ino_per_block = V1_INODES_PER_BLOCK;
|
---|
149 | sb.dirsize = MFS_DIRSIZE;
|
---|
150 | break;
|
---|
151 | case '2':
|
---|
152 | sb.magic = MFS_MAGIC_V2;
|
---|
153 | sb.block_size = MFS_BLOCKSIZE;
|
---|
154 | sb.fs_version = 2;
|
---|
155 | sb.ino_per_block = V2_INODES_PER_BLOCK;
|
---|
156 | sb.dirsize = MFS_DIRSIZE;
|
---|
157 | break;
|
---|
158 | case '3':
|
---|
159 | sb.magic = MFS_MAGIC_V3;
|
---|
160 | sb.fs_version = 3;
|
---|
161 | sb.block_size = MFS_MAX_BLOCKSIZE;
|
---|
162 | sb.dirsize = MFS3_DIRSIZE;
|
---|
163 | sb.ino_per_block = V3_INODES_PER_BLOCK(sb.block_size);
|
---|
164 | break;
|
---|
165 | case 'b':
|
---|
166 | sb.block_size = (uint32_t) strtol(optarg, NULL, 10);
|
---|
167 | break;
|
---|
168 | case 'i':
|
---|
169 | sb.n_inodes = (uint64_t) strtol(optarg, NULL, 10);
|
---|
170 | break;
|
---|
171 | case 'l':
|
---|
172 | sb.longnames = true;
|
---|
173 | sb.dirsize = MFSL_DIRSIZE;
|
---|
174 | break;
|
---|
175 | }
|
---|
176 | }
|
---|
177 |
|
---|
178 | if (sb.block_size < MFS_MIN_BLOCKSIZE ||
|
---|
179 | sb.block_size > MFS_MAX_BLOCKSIZE) {
|
---|
180 | printf(NAME ":Error! Invalid block size.\n");
|
---|
181 | exit(0);
|
---|
182 | } else if (num_of_set_bits(sb.block_size) != 1) {
|
---|
183 | /*Block size must be a power of 2.*/
|
---|
184 | printf(NAME ":Error! Invalid block size.\n");
|
---|
185 | exit(0);
|
---|
186 | } else if (sb.block_size > MFS_BLOCKSIZE &&
|
---|
187 | sb.fs_version != 3) {
|
---|
188 | printf(NAME ":Error! Block size > 1024 is supported by V3 filesystem only.\n");
|
---|
189 | exit(0);
|
---|
190 | } else if (sb.fs_version == 3 && sb.longnames) {
|
---|
191 | printf(NAME ":Error! Long filenames are supported by V1/V2 filesystem only.\n");
|
---|
192 | exit(0);
|
---|
193 | }
|
---|
194 |
|
---|
195 | if (sb.block_size == MFS_MIN_BLOCKSIZE)
|
---|
196 | shift = 1;
|
---|
197 | else if (sb.block_size == MFS_MAX_BLOCKSIZE)
|
---|
198 | shift = 3;
|
---|
199 | else
|
---|
200 | shift = 2;
|
---|
201 |
|
---|
202 | argv += optind;
|
---|
203 |
|
---|
204 | device_name = argv[0];
|
---|
205 |
|
---|
206 | if (!device_name) {
|
---|
207 | help_cmd_mkminix(HELP_LONG);
|
---|
208 | exit(0);
|
---|
209 | }
|
---|
210 |
|
---|
211 | rc = devmap_device_get_handle(device_name, &handle, 0);
|
---|
212 | if (rc != EOK) {
|
---|
213 | printf(NAME ": Error resolving device `%s'.\n", device_name);
|
---|
214 | return 2;
|
---|
215 | }
|
---|
216 |
|
---|
217 | rc = block_init(handle, 2048);
|
---|
218 | if (rc != EOK) {
|
---|
219 | printf(NAME ": Error initializing libblock.\n");
|
---|
220 | return 2;
|
---|
221 | }
|
---|
222 |
|
---|
223 | rc = block_get_bsize(handle, &devblock_size);
|
---|
224 | if (rc != EOK) {
|
---|
225 | printf(NAME ": Error determining device block size.\n");
|
---|
226 | return 2;
|
---|
227 | }
|
---|
228 |
|
---|
229 | rc = block_get_nblocks(handle, &sb.dev_nblocks);
|
---|
230 | if (rc != EOK) {
|
---|
231 | printf(NAME ": Warning, failed to obtain block device size.\n");
|
---|
232 | } else {
|
---|
233 | printf(NAME ": Block device has %" PRIuOFF64 " blocks.\n",
|
---|
234 | sb.dev_nblocks);
|
---|
235 | }
|
---|
236 |
|
---|
237 | if (devblock_size != 512) {
|
---|
238 | printf(NAME ": Error. Device block size is not 512 bytes.\n");
|
---|
239 | return 2;
|
---|
240 | }
|
---|
241 |
|
---|
242 | /*Minimum block size is 1 Kb*/
|
---|
243 | sb.dev_nblocks /= 2;
|
---|
244 |
|
---|
245 | printf(NAME ": Creating Minix file system on device\n");
|
---|
246 |
|
---|
247 | /*Initialize superblock*/
|
---|
248 | if (init_superblock(&sb) != EOK) {
|
---|
249 | printf(NAME ": Error. Superblock initialization failed\n");
|
---|
250 | return 2;
|
---|
251 | }
|
---|
252 |
|
---|
253 | /*Initialize bitmaps*/
|
---|
254 | if (init_bitmaps(&sb) != EOK) {
|
---|
255 | printf(NAME ": Error. Bitmaps initialization failed\n");
|
---|
256 | return 2;
|
---|
257 | }
|
---|
258 |
|
---|
259 | /*Init inode table*/
|
---|
260 | if (init_inode_table(&sb) != EOK) {
|
---|
261 | printf(NAME ": Error. Inode table initialization failed\n");
|
---|
262 | return 2;
|
---|
263 | }
|
---|
264 |
|
---|
265 | /*Make the root inode*/
|
---|
266 | if (sb.fs_version == 3)
|
---|
267 | rc = make_root_ino3(&sb);
|
---|
268 | else
|
---|
269 | rc = make_root_ino(&sb);
|
---|
270 |
|
---|
271 | if (rc != EOK) {
|
---|
272 | printf(NAME ": Error. Root inode initialization failed\n");
|
---|
273 | return 2;
|
---|
274 | }
|
---|
275 |
|
---|
276 | /*Insert directory entries . and ..*/
|
---|
277 | if (insert_dentries(&sb) != EOK) {
|
---|
278 | printf(NAME ": Error. Root directory initialization failed\n");
|
---|
279 | return 2;
|
---|
280 | }
|
---|
281 |
|
---|
282 | return 0;
|
---|
283 | }
|
---|
284 |
|
---|
285 | static int insert_dentries(const struct mfs_sb_info *sb)
|
---|
286 | {
|
---|
287 | void *root_block;
|
---|
288 | uint8_t *dentry_ptr;
|
---|
289 | int rc;
|
---|
290 | const long root_dblock = sb->first_data_zone;
|
---|
291 |
|
---|
292 | root_block = (void *) malloc(sb->block_size);
|
---|
293 | memset(root_block, 0x00, sb->block_size);
|
---|
294 |
|
---|
295 | if (!root_block)
|
---|
296 | return ENOMEM;
|
---|
297 |
|
---|
298 | dentry_ptr = root_block;
|
---|
299 |
|
---|
300 | if (sb->fs_version != 3) {
|
---|
301 | /*Directory entries for V1/V2 filesystem*/
|
---|
302 | struct mfs_dentry *dentry = root_block;
|
---|
303 |
|
---|
304 | dentry->d_inum = MFS_ROOT_INO;
|
---|
305 | memcpy(dentry->d_name, ".\0", 2);
|
---|
306 |
|
---|
307 | dentry = (struct mfs_dentry *) NEXT_DENTRY(dentry_ptr,
|
---|
308 | sb->dirsize);
|
---|
309 |
|
---|
310 | dentry->d_inum = MFS_ROOT_INO;
|
---|
311 | memcpy(dentry->d_name, "..\0", 3);
|
---|
312 | } else {
|
---|
313 | /*Directory entries for V3 filesystem*/
|
---|
314 | struct mfs3_dentry *dentry = root_block;
|
---|
315 |
|
---|
316 | dentry->d_inum = MFS_ROOT_INO;
|
---|
317 | memcpy(dentry->d_name, ".\0", 2);
|
---|
318 |
|
---|
319 | dentry = (struct mfs3_dentry *) NEXT_DENTRY(dentry_ptr,
|
---|
320 | sb->dirsize);
|
---|
321 |
|
---|
322 | dentry->d_inum = MFS_ROOT_INO;
|
---|
323 | memcpy(dentry->d_name, "..\0", 3);
|
---|
324 | }
|
---|
325 |
|
---|
326 | rc = write_block(root_dblock, 1, root_block);
|
---|
327 |
|
---|
328 | free(root_block);
|
---|
329 | return rc;
|
---|
330 | }
|
---|
331 |
|
---|
332 | static int init_inode_table(const struct mfs_sb_info *sb)
|
---|
333 | {
|
---|
334 | unsigned int i;
|
---|
335 | uint8_t *itable_buf;
|
---|
336 | int rc;
|
---|
337 |
|
---|
338 | long itable_off = sb->zbmap_blocks + sb->ibmap_blocks + 2;
|
---|
339 | unsigned long itable_size = sb->itable_size;
|
---|
340 |
|
---|
341 | itable_buf = malloc(sb->block_size);
|
---|
342 |
|
---|
343 | if (!itable_buf)
|
---|
344 | return ENOMEM;
|
---|
345 |
|
---|
346 | memset(itable_buf, 0x00, sb->block_size);
|
---|
347 |
|
---|
348 | for (i = 0; i < itable_size; ++i, ++itable_off) {
|
---|
349 | rc = write_block(itable_off, 1, itable_buf);
|
---|
350 |
|
---|
351 | if (rc != EOK)
|
---|
352 | break;
|
---|
353 | }
|
---|
354 |
|
---|
355 | free(itable_buf);
|
---|
356 | return rc;
|
---|
357 | }
|
---|
358 |
|
---|
359 | static int make_root_ino(const struct mfs_sb_info *sb)
|
---|
360 | {
|
---|
361 | struct mfs_inode *ino_buf;
|
---|
362 | int rc;
|
---|
363 |
|
---|
364 | const long itable_off = sb->zbmap_blocks + sb->ibmap_blocks + 2;
|
---|
365 |
|
---|
366 | const time_t sec = time(NULL);
|
---|
367 |
|
---|
368 | ino_buf = (struct mfs_inode *) malloc(MFS_BLOCKSIZE);
|
---|
369 |
|
---|
370 | if (!ino_buf)
|
---|
371 | return ENOMEM;
|
---|
372 |
|
---|
373 | memset(ino_buf, 0x00, MFS_BLOCKSIZE);
|
---|
374 |
|
---|
375 | ino_buf[MFS_ROOT_INO].i_mode = S_IFDIR;
|
---|
376 | ino_buf[MFS_ROOT_INO].i_uid = 0;
|
---|
377 | ino_buf[MFS_ROOT_INO].i_gid = 0;
|
---|
378 | ino_buf[MFS_ROOT_INO].i_size = (sb->longnames ? MFSL_DIRSIZE : MFS_DIRSIZE) * 2;
|
---|
379 | ino_buf[MFS_ROOT_INO].i_mtime = sec;
|
---|
380 | ino_buf[MFS_ROOT_INO].i_nlinks = 2;
|
---|
381 | ino_buf[MFS_ROOT_INO].i_dzone[0] = sb->first_data_zone;
|
---|
382 |
|
---|
383 | rc = write_block(itable_off, 1, ino_buf);
|
---|
384 |
|
---|
385 | free(ino_buf);
|
---|
386 | return rc;
|
---|
387 | }
|
---|
388 |
|
---|
389 | static int make_root_ino3(const struct mfs_sb_info *sb)
|
---|
390 | {
|
---|
391 | struct mfs2_inode *ino_buf;
|
---|
392 | int rc;
|
---|
393 |
|
---|
394 | /*Compute offset of the first inode table block*/
|
---|
395 | const long itable_off = sb->zbmap_blocks + sb->ibmap_blocks + 2;
|
---|
396 |
|
---|
397 | const time_t sec = time(NULL);
|
---|
398 |
|
---|
399 | ino_buf = (struct mfs2_inode *) malloc(sb->block_size);
|
---|
400 |
|
---|
401 | if (!ino_buf)
|
---|
402 | return ENOMEM;
|
---|
403 |
|
---|
404 | memset(ino_buf, 0x00, sb->block_size);
|
---|
405 |
|
---|
406 | ino_buf[MFS_ROOT_INO].i_mode = S_IFDIR;
|
---|
407 | ino_buf[MFS_ROOT_INO].i_uid = 0;
|
---|
408 | ino_buf[MFS_ROOT_INO].i_gid = 0;
|
---|
409 | ino_buf[MFS_ROOT_INO].i_size = MFS3_DIRSIZE * 2;
|
---|
410 | ino_buf[MFS_ROOT_INO].i_mtime = sec;
|
---|
411 | ino_buf[MFS_ROOT_INO].i_atime = sec;
|
---|
412 | ino_buf[MFS_ROOT_INO].i_ctime = sec;
|
---|
413 | ino_buf[MFS_ROOT_INO].i_nlinks = 2;
|
---|
414 | ino_buf[MFS_ROOT_INO].i_dzone[0] = sb->first_data_zone;
|
---|
415 |
|
---|
416 | rc = write_block(itable_off, 1, ino_buf);
|
---|
417 |
|
---|
418 | free(ino_buf);
|
---|
419 | return rc;
|
---|
420 | }
|
---|
421 |
|
---|
422 | static int init_superblock(struct mfs_sb_info *sb)
|
---|
423 | {
|
---|
424 | aoff64_t inodes;
|
---|
425 | int rc;
|
---|
426 |
|
---|
427 | if (sb->longnames)
|
---|
428 | sb->magic = sb->fs_version == 1 ? MFS_MAGIC_V1L : MFS_MAGIC_V2L;
|
---|
429 |
|
---|
430 | /*Compute the number of zones on disk*/
|
---|
431 |
|
---|
432 | if (sb->fs_version == 1) {
|
---|
433 | /*Valid only for MFS V1*/
|
---|
434 | sb->n_zones = sb->dev_nblocks > UINT16_MAX ?
|
---|
435 | UINT16_MAX : sb->dev_nblocks;
|
---|
436 | } else {
|
---|
437 | /*Valid for MFS V2/V3*/
|
---|
438 | sb->n_zones = sb->dev_nblocks > UINT32_MAX ?
|
---|
439 | UINT32_MAX : sb->dev_nblocks;
|
---|
440 |
|
---|
441 | if (sb->fs_version == 3) {
|
---|
442 | sb->ino_per_block = V3_INODES_PER_BLOCK(sb->block_size);
|
---|
443 | sb->n_zones /= (sb->block_size / MFS_MIN_BLOCKSIZE);
|
---|
444 | }
|
---|
445 | }
|
---|
446 |
|
---|
447 | /*Round up the number of inodes to fill block size*/
|
---|
448 | if (sb->n_inodes == 0)
|
---|
449 | inodes = sb->dev_nblocks / 3;
|
---|
450 | else
|
---|
451 | inodes = sb->n_inodes;
|
---|
452 |
|
---|
453 | if (inodes % sb->ino_per_block)
|
---|
454 | inodes = ((inodes / sb->ino_per_block) + 1) * sb->ino_per_block;
|
---|
455 |
|
---|
456 | if (sb->fs_version < 3)
|
---|
457 | sb->n_inodes = inodes > UINT16_MAX ? UINT16_MAX : inodes;
|
---|
458 | else
|
---|
459 | sb->n_inodes = inodes > UINT32_MAX ? UINT32_MAX : inodes;
|
---|
460 |
|
---|
461 | /*Compute inode bitmap size in blocks*/
|
---|
462 | sb->ibmap_blocks = UPPER(sb->n_inodes, sb->block_size * 8);
|
---|
463 |
|
---|
464 | /*Compute inode table size*/
|
---|
465 | sb->itable_size = sb->n_inodes / sb->ino_per_block;
|
---|
466 |
|
---|
467 | /*Compute zone bitmap size in blocks*/
|
---|
468 | sb->zbmap_blocks = UPPER(sb->n_zones, sb->block_size * 8);
|
---|
469 |
|
---|
470 | /*Compute first data zone position*/
|
---|
471 | sb->first_data_zone = 2 + sb->itable_size +
|
---|
472 | sb->zbmap_blocks + sb->ibmap_blocks;
|
---|
473 |
|
---|
474 | /*Set log2 of zone to block ratio to zero*/
|
---|
475 | sb->log2_zone_size = 0;
|
---|
476 |
|
---|
477 | /*Check for errors*/
|
---|
478 | if (sb->first_data_zone >= sb->n_zones) {
|
---|
479 | printf(NAME ": Error! Insufficient disk space");
|
---|
480 | return ENOMEM;
|
---|
481 | }
|
---|
482 |
|
---|
483 | /*Superblock is now ready to be written on disk*/
|
---|
484 | printf(NAME ": %d inodes\n", (uint32_t) sb->n_inodes);
|
---|
485 | printf(NAME ": %d zones\n", (uint32_t) sb->n_zones);
|
---|
486 | printf(NAME ": inode table blocks = %ld\n", sb->itable_size);
|
---|
487 | printf(NAME ": inode bitmap blocks = %ld\n", sb->ibmap_blocks);
|
---|
488 | printf(NAME ": zone bitmap blocks = %ld\n", sb->zbmap_blocks);
|
---|
489 | printf(NAME ": first data zone = %d\n", (uint32_t) sb->first_data_zone);
|
---|
490 | printf(NAME ": long fnames = %s\n", sb->longnames ? "Yes" : "No");
|
---|
491 |
|
---|
492 | if (sb->fs_version == 3)
|
---|
493 | rc = write_superblock3(sb);
|
---|
494 | else
|
---|
495 | rc = write_superblock(sb);
|
---|
496 |
|
---|
497 | return rc;
|
---|
498 | }
|
---|
499 |
|
---|
500 | static int write_superblock(const struct mfs_sb_info *sbi)
|
---|
501 | {
|
---|
502 | struct mfs_superblock *sb;
|
---|
503 | int rc;
|
---|
504 |
|
---|
505 | sb = (struct mfs_superblock *) malloc(MFS_SUPERBLOCK_SIZE);;
|
---|
506 |
|
---|
507 | if (!sb)
|
---|
508 | return ENOMEM;
|
---|
509 |
|
---|
510 | sb->s_ninodes = (uint16_t) sbi->n_inodes;
|
---|
511 | sb->s_nzones = (uint16_t) sbi->n_zones;
|
---|
512 | sb->s_nzones2 = (uint32_t) sbi->n_zones;
|
---|
513 | sb->s_ibmap_blocks = (uint16_t) sbi->ibmap_blocks;
|
---|
514 | sb->s_zbmap_blocks = (uint16_t) sbi->zbmap_blocks;
|
---|
515 | sb->s_first_data_zone = (uint16_t) sbi->first_data_zone;
|
---|
516 | sb->s_log2_zone_size = sbi->log2_zone_size;
|
---|
517 | sb->s_max_file_size = UINT32_MAX;
|
---|
518 | sb->s_magic = sbi->magic;
|
---|
519 | sb->s_state = MFS_VALID_FS;
|
---|
520 |
|
---|
521 | rc = write_block(MFS_SUPERBLOCK, 1, sb);
|
---|
522 | free(sb);
|
---|
523 |
|
---|
524 | return rc;
|
---|
525 | }
|
---|
526 |
|
---|
527 | static int write_superblock3(const struct mfs_sb_info *sbi)
|
---|
528 | {
|
---|
529 | struct mfs3_superblock *sb;
|
---|
530 | int rc;
|
---|
531 |
|
---|
532 | sb = (struct mfs3_superblock *) malloc(MFS_SUPERBLOCK_SIZE);
|
---|
533 |
|
---|
534 | if (!sb)
|
---|
535 | return ENOMEM;
|
---|
536 |
|
---|
537 | sb->s_ninodes = (uint32_t) sbi->n_inodes;
|
---|
538 | sb->s_nzones = (uint32_t) sbi->n_zones;
|
---|
539 | sb->s_ibmap_blocks = (uint16_t) sbi->ibmap_blocks;
|
---|
540 | sb->s_zbmap_blocks = (uint16_t) sbi->zbmap_blocks;
|
---|
541 | sb->s_first_data_zone = (uint16_t) sbi->first_data_zone;
|
---|
542 | sb->s_log2_zone_size = sbi->log2_zone_size;
|
---|
543 | sb->s_max_file_size = UINT32_MAX;
|
---|
544 | sb->s_magic = sbi->magic;
|
---|
545 | sb->s_block_size = sbi->block_size;
|
---|
546 | sb->s_disk_version = 3;
|
---|
547 |
|
---|
548 | rc = block_write_direct(handle, MFS_SUPERBLOCK << 1, 1 << 1, sb);
|
---|
549 | free(sb);
|
---|
550 |
|
---|
551 | return rc;
|
---|
552 | }
|
---|
553 |
|
---|
554 | static int init_bitmaps(const struct mfs_sb_info *sb)
|
---|
555 | {
|
---|
556 | uint32_t *ibmap_buf, *zbmap_buf;
|
---|
557 | uint8_t *ibmap_buf8, *zbmap_buf8;
|
---|
558 | const unsigned int ibmap_nblocks = sb->ibmap_blocks;
|
---|
559 | const unsigned int zbmap_nblocks = sb->zbmap_blocks;
|
---|
560 | unsigned int i;
|
---|
561 | int rc;
|
---|
562 |
|
---|
563 | ibmap_buf = (uint32_t *) malloc(ibmap_nblocks * sb->block_size);
|
---|
564 | zbmap_buf = (uint32_t *) malloc(zbmap_nblocks * sb->block_size);
|
---|
565 |
|
---|
566 | if (!ibmap_buf || !zbmap_buf)
|
---|
567 | return ENOMEM;
|
---|
568 |
|
---|
569 | memset(ibmap_buf, 0xFF, ibmap_nblocks * sb->block_size);
|
---|
570 | memset(zbmap_buf, 0xFF, zbmap_nblocks * sb->block_size);
|
---|
571 |
|
---|
572 | for (i = 2; i < sb->n_inodes; ++i)
|
---|
573 | mark_bmap(ibmap_buf, i, FREE);
|
---|
574 |
|
---|
575 | for (i = sb->first_data_zone + 1; i < sb->n_zones; ++i)
|
---|
576 | mark_bmap(zbmap_buf, i, FREE);
|
---|
577 |
|
---|
578 | /*Convert to 1K block offsets*/
|
---|
579 |
|
---|
580 | ibmap_buf8 = (uint8_t *) ibmap_buf;
|
---|
581 | zbmap_buf8 = (uint8_t *) zbmap_buf;
|
---|
582 |
|
---|
583 | int start_block = 2;
|
---|
584 |
|
---|
585 | for (i = 0; i < ibmap_nblocks; ++i) {
|
---|
586 | if ((rc = write_block(start_block + i,
|
---|
587 | 1, (ibmap_buf8 + i * sb->block_size))) != EOK)
|
---|
588 | return rc;
|
---|
589 | }
|
---|
590 |
|
---|
591 | start_block = 2 + ibmap_nblocks;
|
---|
592 |
|
---|
593 | for (i = 0; i < zbmap_nblocks; ++i) {
|
---|
594 | if ((rc = write_block(start_block + i,
|
---|
595 | 1, (zbmap_buf8 + i * sb->block_size))) != EOK)
|
---|
596 | return rc;
|
---|
597 | }
|
---|
598 |
|
---|
599 | free(ibmap_buf);
|
---|
600 | free(zbmap_buf);
|
---|
601 |
|
---|
602 | return rc;
|
---|
603 | }
|
---|
604 |
|
---|
605 | static void mark_bmap(uint32_t *bmap, int idx, int v)
|
---|
606 | {
|
---|
607 | if (v == FREE)
|
---|
608 | bmap[idx / 32] &= ~(1 << (idx % 32));
|
---|
609 | else
|
---|
610 | bmap[idx / 32] |= 1 << (idx % 32);
|
---|
611 | }
|
---|
612 |
|
---|
613 | static inline int write_block(aoff64_t off, size_t size, const void *data)
|
---|
614 | {
|
---|
615 | if (shift == 3) {
|
---|
616 | int rc;
|
---|
617 | aoff64_t tmp_off = off << 1;
|
---|
618 | uint8_t *data_ptr = (uint8_t *) data;
|
---|
619 |
|
---|
620 | rc = block_write_direct(handle, tmp_off << 2, size << 2, data_ptr);
|
---|
621 |
|
---|
622 | if (rc != EOK)
|
---|
623 | return rc;
|
---|
624 |
|
---|
625 | data_ptr += 2048;
|
---|
626 | tmp_off++;
|
---|
627 |
|
---|
628 | return block_write_direct(handle, tmp_off << 2, size << 2, data_ptr);
|
---|
629 | }
|
---|
630 | return block_write_direct(handle, off << shift, size << shift, data);
|
---|
631 | }
|
---|
632 |
|
---|
633 | static void help_cmd_mkminix(help_level_t level)
|
---|
634 | {
|
---|
635 | if (level == HELP_SHORT) {
|
---|
636 | printf(NAME": tool to create new Minix file systems\n");
|
---|
637 | } else {
|
---|
638 | printf("Usage: [options] device\n"
|
---|
639 | "-1 Make a Minix version 1 filesystem\n"
|
---|
640 | "-2 Make a Minix version 2 filesystem\n"
|
---|
641 | "-3 Make a Minix version 3 filesystem\n"
|
---|
642 | "-b ## Specify the block size in bytes (V3 only),\n"
|
---|
643 | " valid block size values are 1024, 2048 and 4096 bytes per block\n"
|
---|
644 | "-i ## Specify the number of inodes for the filesystem\n"
|
---|
645 | "-l Use 30-char long filenames (V1/V2 only)\n");
|
---|
646 | }
|
---|
647 | }
|
---|
648 |
|
---|
649 | static int num_of_set_bits(uint32_t n)
|
---|
650 | {
|
---|
651 | n = n - ((n >> 1) & 0x55555555);
|
---|
652 | n = (n & 0x33333333) + ((n >> 2) & 0x33333333);
|
---|
653 | return (((n + (n >> 4)) & 0xF0F0F0F) * 0x1010101) >> 24;
|
---|
654 | }
|
---|
655 |
|
---|
656 |
|
---|
657 | /**
|
---|
658 | * @}
|
---|
659 | */
|
---|