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
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 <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
59typedef enum {
60 HELP_SHORT,
61 HELP_LONG
62} help_level_t;
63
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;
74 unsigned long itable_size;
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};
83
84static void help_cmd_mkminix(help_level_t level);
85static int num_of_set_bits(uint32_t n);
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);
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);
93static void mark_bmap(uint32_t *bmap, int idx, int v);
94
95static struct option const long_options[] = {
96 { "help", no_argument, 0, 'h' },
97 { "long-names", no_argument, 0, 'l' },
98 { "block-size", required_argument, 0, 'b' },
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
111 struct mfs_sb_info sb;
112
113 /*Default is MinixFS V3*/
114 sb.magic = MFS_MAGIC_V3;
115
116 /*Default block size is 4Kb*/
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);
121
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;) {
129 c = getopt_long(argc, argv, "lh123b:i:", long_options, &opt_ind);
130 switch (c) {
131 case 'h':
132 help_cmd_mkminix(HELP_LONG);
133 exit(0);
134 case '1':
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;
139 break;
140 case '2':
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;
145 break;
146 case '3':
147 sb.magic = MFS_MAGIC_V3;
148 sb.fs_version = 3;
149 sb.block_size = MFS_MAX_BLOCKSIZE;
150 break;
151 case 'b':
152 sb.block_size = (uint32_t) strtol(optarg, NULL, 10);
153 break;
154 case 'i':
155 sb.n_inodes = (unsigned long) strtol(optarg, NULL, 10);
156 break;
157 case 'l':
158 sb.longnames = true;
159 break;
160 }
161 }
162
163 if (sb.block_size < MFS_MIN_BLOCKSIZE ||
164 sb.block_size > MFS_MAX_BLOCKSIZE) {
165 printf(NAME ":Error! Invalid block size.\n");
166 exit(0);
167 } else if (num_of_set_bits(sb.block_size) != 1) {
168 /*Block size must be a power of 2.*/
169 printf(NAME ":Error! Invalid block size.\n");
170 exit(0);
171 } else if (sb.block_size > MFS_BLOCKSIZE &&
172 sb.fs_version != 3) {
173 printf(NAME ":Error! Block size > 1024 is supported by V3 filesystem only.\n");
174 exit(0);
175 } else if (sb.fs_version == 3 && sb.longnames) {
176 printf(NAME ":Error! Long filenames are supported by V1/V2 filesystem only.\n");
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
189 rc = devmap_device_get_handle(device_name, &sb.handle, 0);
190 if (rc != EOK) {
191 printf(NAME ": Error resolving device `%s'.\n", device_name);
192 return 2;
193 }
194
195 rc = block_init(sb.handle, MFS_MIN_BLOCKSIZE);
196 if (rc != EOK) {
197 printf(NAME ": Error initializing libblock.\n");
198 return 2;
199 }
200
201 rc = block_get_bsize(sb.handle, &sb.devblock_size);
202 if (rc != EOK) {
203 printf(NAME ": Error determining device block size.\n");
204 return 2;
205 }
206
207 rc = block_get_nblocks(sb.handle, &sb.dev_nblocks);
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",
212 sb.dev_nblocks);
213 }
214
215 if (sb.devblock_size != 512) {
216 printf(NAME ": Error. Device block size is not 512 bytes.\n");
217 return 2;
218 }
219
220 /*Minimum block size is 1 Kb*/
221 sb.dev_nblocks /= 2;
222
223 printf(NAME ": Creating Minix file system on device\n");
224
225 /*Initialize superblock*/
226 init_superblock(&sb);
227
228 /*Initialize bitmaps*/
229 init_bitmaps(&sb);
230
231 /*Init inode table*/
232 init_inode_table(&sb);
233
234 return 0;
235}
236
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
306static void init_superblock(struct mfs_sb_info *sb)
307{
308 aoff64_t inodes;
309
310 if (sb->longnames)
311 sb->magic = sb->fs_version == 1 ? MFS_MAGIC_V1L : MFS_MAGIC_V2L;
312
313 /*Compute the number of zones on disk*/
314
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;
323
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 }
329
330 /*Round up the number of inodes to fill block size*/
331 if (sb->n_inodes == 0)
332 inodes = sb->dev_nblocks / 3;
333
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;
341
342 /*Compute inode bitmap size in blocks*/
343 sb->ibmap_blocks = UPPER(sb->n_inodes, sb->block_size * 8);
344
345 /*Compute zone bitmap size in blocks*/
346 sb->zbmap_blocks = UPPER(sb->n_zones, sb->block_size * 8);
347
348 /*Compute inode table size*/
349 sb->itable_size = sb->n_inodes / sb->ino_per_block;
350
351 /*Compute first data zone position*/
352 sb->first_data_zone = 2 + sb->itable_size +
353 sb->zbmap_blocks + sb->ibmap_blocks;
354
355 /*Set log2 of zone to block ratio to zero*/
356 sb->log2_zone_size = 0;
357
358 /*Superblock is now ready to be written on disk*/
359 printf(NAME ": %d inodes\n", (uint32_t) sb->n_inodes);
360 printf(NAME ": %d zones\n", (uint32_t) sb->n_zones);
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);
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");
366
367 if (sb->fs_version == 3)
368 write_superblock3(sb);
369 else
370 write_superblock(sb);
371}
372
373static void write_superblock(struct mfs_sb_info *sbi)
374{
375 struct mfs_superblock *sb;
376
377 sb = (struct mfs_superblock *) malloc(MFS_SUPERBLOCK_SIZE);;
378
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;
389
390 block_write_direct(sbi->handle, MFS_SUPERBLOCK, 1, sb);
391 free(sb);
392}
393
394static void write_superblock3(struct mfs_sb_info *sbi)
395{
396 struct mfs3_superblock *sb;
397
398 sb = (struct mfs3_superblock *) malloc(MFS_SUPERBLOCK_SIZE);
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;
409 sb->s_disk_version = 3;
410
411 block_write_direct(sbi->handle, MFS_SUPERBLOCK, 1, sb);
412 free(sb);
413}
414
415static void init_bitmaps(struct mfs_sb_info *sb)
416{
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;
420 unsigned int i;
421
422 ibmap_buf = (uint32_t *) malloc(ibmap_nblocks * sb->block_size);
423 zbmap_buf = (uint32_t *) malloc(zbmap_nblocks * sb->block_size);
424
425 memset(ibmap_buf, 0xFF, ibmap_nblocks * sb->block_size);
426 memset(zbmap_buf, 0xFF, zbmap_nblocks * sb->block_size);
427
428 for (i = 2; i < sb->n_inodes; ++i)
429 mark_bmap(ibmap_buf, i, FREE);
430
431 for (i = 2; i < sb->n_zones; ++i)
432 mark_bmap(zbmap_buf, i, FREE);
433
434 ibmap_nblocks *= sb->block_size / MFS_BLOCKSIZE;
435 zbmap_nblocks *= sb->block_size / MFS_BLOCKSIZE;
436
437 block_write_direct(sb->handle, 2, ibmap_nblocks, ibmap_buf);
438 block_write_direct(sb->handle, 2 + ibmap_nblocks, zbmap_nblocks, zbmap_buf);
439}
440
441static void mark_bmap(uint32_t *bmap, int idx, int v)
442{
443 if (v == FREE)
444 bmap[idx / 32] &= ~(1 << (idx % 32));
445 else
446 bmap[idx / 32] |= 1 << (idx % 32);
447}
448
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"
459 " valid block size values are 1024, 2048 and 4096 bytes per block\n"
460 "-i ## Specify the number of inodes for the filesystem\n"
461 "-l Use 30-char long filenames (V1/V2 only)\n");
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.