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