source: mainline/uspace/app/mkminix/mkminix.c@ 4727a97a

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

Set the current time in root inode and finx inode table initialization

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