source: mainline/uspace/app/mkminix/mkminix.c@ 3c616f6

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

Fix directory entries structures

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