diff options
Diffstat (limited to 'e2fsprogs/mke2fs.c')
-rw-r--r-- | e2fsprogs/mke2fs.c | 1336 |
1 files changed, 1336 insertions, 0 deletions
diff --git a/e2fsprogs/mke2fs.c b/e2fsprogs/mke2fs.c new file mode 100644 index 000000000..f25ecfb6c --- /dev/null +++ b/e2fsprogs/mke2fs.c | |||
@@ -0,0 +1,1336 @@ | |||
1 | /* vi: set sw=4 ts=4: */ | ||
2 | /* | ||
3 | * mke2fs.c - Make a ext2fs filesystem. | ||
4 | * | ||
5 | * Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, | ||
6 | * 2003, 2004, 2005 by Theodore Ts'o. | ||
7 | * | ||
8 | * This file may be redistributed under the terms of the GNU Public | ||
9 | * License. | ||
10 | */ | ||
11 | |||
12 | /* Usage: mke2fs [options] device | ||
13 | * | ||
14 | * The device may be a block device or a image of one, but this isn't | ||
15 | * enforced (but it's not much fun on a character device :-). | ||
16 | */ | ||
17 | |||
18 | #include <stdio.h> | ||
19 | #include <string.h> | ||
20 | #include <fcntl.h> | ||
21 | #include <ctype.h> | ||
22 | #include <time.h> | ||
23 | #include <getopt.h> | ||
24 | #include <unistd.h> | ||
25 | #include <stdlib.h> | ||
26 | #include <errno.h> | ||
27 | #include <mntent.h> | ||
28 | #include <sys/ioctl.h> | ||
29 | #include <sys/types.h> | ||
30 | |||
31 | #include "e2fsbb.h" | ||
32 | #include "ext2fs/ext2_fs.h" | ||
33 | #include "uuid/uuid.h" | ||
34 | #include "e2p/e2p.h" | ||
35 | #include "ext2fs/ext2fs.h" | ||
36 | #include "util.h" | ||
37 | |||
38 | #define STRIDE_LENGTH 8 | ||
39 | |||
40 | #ifndef __sparc__ | ||
41 | #define ZAP_BOOTBLOCK | ||
42 | #endif | ||
43 | |||
44 | static const char * device_name; | ||
45 | |||
46 | /* Command line options */ | ||
47 | static int cflag; | ||
48 | static int quiet; | ||
49 | static int super_only; | ||
50 | static int force; | ||
51 | static int noaction; | ||
52 | static int journal_size; | ||
53 | static int journal_flags; | ||
54 | static const char *bad_blocks_filename; | ||
55 | static __u32 fs_stride; | ||
56 | |||
57 | static struct ext2_super_block param; | ||
58 | static char *creator_os; | ||
59 | static char *volume_label; | ||
60 | static char *mount_dir; | ||
61 | static char *journal_device = NULL; | ||
62 | static int sync_kludge; /* Set using the MKE2FS_SYNC env. option */ | ||
63 | |||
64 | static int sys_page_size = 4096; | ||
65 | static int linux_version_code = 0; | ||
66 | |||
67 | static int int_log2(int arg) | ||
68 | { | ||
69 | int l = 0; | ||
70 | |||
71 | arg >>= 1; | ||
72 | while (arg) { | ||
73 | l++; | ||
74 | arg >>= 1; | ||
75 | } | ||
76 | return l; | ||
77 | } | ||
78 | |||
79 | static int int_log10(unsigned int arg) | ||
80 | { | ||
81 | int l; | ||
82 | |||
83 | for (l=0; arg ; l++) | ||
84 | arg = arg / 10; | ||
85 | return l; | ||
86 | } | ||
87 | |||
88 | /* | ||
89 | * This function sets the default parameters for a filesystem | ||
90 | * | ||
91 | * The type is specified by the user. The size is the maximum size | ||
92 | * (in megabytes) for which a set of parameters applies, with a size | ||
93 | * of zero meaning that it is the default parameter for the type. | ||
94 | * Note that order is important in the table below. | ||
95 | */ | ||
96 | #define DEF_MAX_BLOCKSIZE -1 | ||
97 | static const char default_str[] = "default"; | ||
98 | struct mke2fs_defaults { | ||
99 | const char *type; | ||
100 | int size; | ||
101 | int blocksize; | ||
102 | int inode_ratio; | ||
103 | }; | ||
104 | |||
105 | static const struct mke2fs_defaults settings[] = { | ||
106 | { default_str, 0, 4096, 8192 }, | ||
107 | { default_str, 512, 1024, 4096 }, | ||
108 | { default_str, 3, 1024, 8192 }, | ||
109 | { "journal", 0, 4096, 8192 }, | ||
110 | { "news", 0, 4096, 4096 }, | ||
111 | { "largefile", 0, 4096, 1024 * 1024 }, | ||
112 | { "largefile4", 0, 4096, 4096 * 1024 }, | ||
113 | { 0, 0, 0, 0}, | ||
114 | }; | ||
115 | |||
116 | static void set_fs_defaults(const char *fs_type, | ||
117 | struct ext2_super_block *super, | ||
118 | int blocksize, int sector_size, | ||
119 | int *inode_ratio) | ||
120 | { | ||
121 | int megs; | ||
122 | int ratio = 0; | ||
123 | const struct mke2fs_defaults *p; | ||
124 | int use_bsize = 1024; | ||
125 | |||
126 | megs = super->s_blocks_count * (EXT2_BLOCK_SIZE(super) / 1024) / 1024; | ||
127 | if (inode_ratio) | ||
128 | ratio = *inode_ratio; | ||
129 | if (!fs_type) | ||
130 | fs_type = default_str; | ||
131 | for (p = settings; p->type; p++) { | ||
132 | if ((strcmp(p->type, fs_type) != 0) && | ||
133 | (strcmp(p->type, default_str) != 0)) | ||
134 | continue; | ||
135 | if ((p->size != 0) && (megs > p->size)) | ||
136 | continue; | ||
137 | if (ratio == 0) | ||
138 | *inode_ratio = p->inode_ratio < blocksize ? | ||
139 | blocksize : p->inode_ratio; | ||
140 | use_bsize = p->blocksize; | ||
141 | } | ||
142 | if (blocksize <= 0) { | ||
143 | if (use_bsize == DEF_MAX_BLOCKSIZE) { | ||
144 | use_bsize = sys_page_size; | ||
145 | if ((linux_version_code < (2*65536 + 6*256)) && | ||
146 | (use_bsize > 4096)) | ||
147 | use_bsize = 4096; | ||
148 | } | ||
149 | if (sector_size && use_bsize < sector_size) | ||
150 | use_bsize = sector_size; | ||
151 | if ((blocksize < 0) && (use_bsize < (-blocksize))) | ||
152 | use_bsize = -blocksize; | ||
153 | blocksize = use_bsize; | ||
154 | super->s_blocks_count /= blocksize / 1024; | ||
155 | } | ||
156 | super->s_log_frag_size = super->s_log_block_size = | ||
157 | int_log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE); | ||
158 | } | ||
159 | |||
160 | |||
161 | /* | ||
162 | * Helper function for read_bb_file and test_disk | ||
163 | */ | ||
164 | static void invalid_block(ext2_filsys fs EXT2FS_ATTR((unused)), blk_t blk) | ||
165 | { | ||
166 | bb_error_msg("Bad block %u out of range; ignored", blk); | ||
167 | return; | ||
168 | } | ||
169 | |||
170 | /* | ||
171 | * Busybox stuff | ||
172 | */ | ||
173 | static void mke2fs_error_msg_and_die(int retval, const char *fmt, ...)__attribute__ ((format (printf, 2, 3))); | ||
174 | static void mke2fs_error_msg_and_die(int retval, const char *fmt, ...) | ||
175 | { | ||
176 | va_list ap; | ||
177 | |||
178 | if (retval) { | ||
179 | va_start(ap, fmt); | ||
180 | fprintf(stderr,"\nCould not "); | ||
181 | vfprintf(stderr, fmt, ap); | ||
182 | fprintf(stderr, "\n"); | ||
183 | va_end(ap); | ||
184 | exit(EXIT_FAILURE); | ||
185 | } | ||
186 | } | ||
187 | |||
188 | static void mke2fs_verbose(const char *fmt, ...)__attribute__ ((format (printf, 1, 2))); | ||
189 | static void mke2fs_verbose(const char *fmt, ...) | ||
190 | { | ||
191 | va_list ap; | ||
192 | |||
193 | if (!quiet) { | ||
194 | va_start(ap, fmt); | ||
195 | vfprintf(stdout, fmt, ap); | ||
196 | fflush(stdout); | ||
197 | va_end(ap); | ||
198 | } | ||
199 | } | ||
200 | |||
201 | static void mke2fs_verbose_done(void) | ||
202 | { | ||
203 | mke2fs_verbose("done\n"); | ||
204 | } | ||
205 | |||
206 | static void mke2fs_warning_msg(int retval, char *fmt, ... )__attribute__ ((format (printf, 2, 3))); | ||
207 | static void mke2fs_warning_msg(int retval, char *fmt, ... ) | ||
208 | { | ||
209 | va_list ap; | ||
210 | |||
211 | if (retval) { | ||
212 | va_start(ap, fmt); | ||
213 | fprintf(stderr,"\nWarning: "); | ||
214 | vfprintf(stderr, fmt, ap); | ||
215 | fprintf(stderr, "\n"); | ||
216 | va_end(ap); | ||
217 | } | ||
218 | } | ||
219 | |||
220 | /* | ||
221 | * Reads the bad blocks list from a file | ||
222 | */ | ||
223 | static void read_bb_file(ext2_filsys fs, badblocks_list *bb_list, | ||
224 | const char *bad_blocks_file) | ||
225 | { | ||
226 | FILE *f; | ||
227 | errcode_t retval; | ||
228 | |||
229 | f = xfopen(bad_blocks_file, "r"); | ||
230 | retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block); | ||
231 | fclose (f); | ||
232 | mke2fs_error_msg_and_die(retval, "read bad blocks from list"); | ||
233 | } | ||
234 | |||
235 | /* | ||
236 | * Runs the badblocks program to test the disk | ||
237 | */ | ||
238 | static void test_disk(ext2_filsys fs, badblocks_list *bb_list) | ||
239 | { | ||
240 | FILE *f; | ||
241 | errcode_t retval; | ||
242 | char buf[1024]; | ||
243 | |||
244 | sprintf(buf, "badblocks -b %d %s%s%s %d", fs->blocksize, | ||
245 | quiet ? "" : "-s ", (cflag > 1) ? "-w " : "", | ||
246 | fs->device_name, fs->super->s_blocks_count); | ||
247 | mke2fs_verbose("Running command: %s\n", buf); | ||
248 | f = popen(buf, "r"); | ||
249 | if (!f) { | ||
250 | bb_perror_msg_and_die("cannot run '%s'", buf); | ||
251 | } | ||
252 | retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block); | ||
253 | pclose(f); | ||
254 | mke2fs_error_msg_and_die(retval, "read bad blocks from program"); | ||
255 | } | ||
256 | |||
257 | static void handle_bad_blocks(ext2_filsys fs, badblocks_list bb_list) | ||
258 | { | ||
259 | dgrp_t i; | ||
260 | blk_t j; | ||
261 | unsigned must_be_good; | ||
262 | blk_t blk; | ||
263 | badblocks_iterate bb_iter; | ||
264 | errcode_t retval; | ||
265 | blk_t group_block; | ||
266 | int group; | ||
267 | int group_bad; | ||
268 | |||
269 | if (!bb_list) | ||
270 | return; | ||
271 | |||
272 | /* | ||
273 | * The primary superblock and group descriptors *must* be | ||
274 | * good; if not, abort. | ||
275 | */ | ||
276 | must_be_good = fs->super->s_first_data_block + 1 + fs->desc_blocks; | ||
277 | for (i = fs->super->s_first_data_block; i <= must_be_good; i++) { | ||
278 | if (ext2fs_badblocks_list_test(bb_list, i)) { | ||
279 | bb_error_msg_and_die( | ||
280 | "Block %d in primary superblock/group descriptor area bad\n" | ||
281 | "Blocks %d through %d must be good in order to build a filesystem\n" | ||
282 | "Aborting ...", i, fs->super->s_first_data_block, must_be_good); | ||
283 | } | ||
284 | } | ||
285 | |||
286 | /* | ||
287 | * See if any of the bad blocks are showing up in the backup | ||
288 | * superblocks and/or group descriptors. If so, issue a | ||
289 | * warning and adjust the block counts appropriately. | ||
290 | */ | ||
291 | group_block = fs->super->s_first_data_block + | ||
292 | fs->super->s_blocks_per_group; | ||
293 | |||
294 | for (i = 1; i < fs->group_desc_count; i++) { | ||
295 | group_bad = 0; | ||
296 | for (j=0; j < fs->desc_blocks+1; j++) { | ||
297 | if (ext2fs_badblocks_list_test(bb_list, | ||
298 | group_block + j)) { | ||
299 | mke2fs_warning_msg(!group_bad, | ||
300 | "the backup superblock/group descriptors at block %d contain\n" | ||
301 | "bad blocks\n", group_block); | ||
302 | group_bad++; | ||
303 | group = ext2fs_group_of_blk(fs, group_block+j); | ||
304 | fs->group_desc[group].bg_free_blocks_count++; | ||
305 | fs->super->s_free_blocks_count++; | ||
306 | } | ||
307 | } | ||
308 | group_block += fs->super->s_blocks_per_group; | ||
309 | } | ||
310 | |||
311 | /* | ||
312 | * Mark all the bad blocks as used... | ||
313 | */ | ||
314 | retval = ext2fs_badblocks_list_iterate_begin(bb_list, &bb_iter); | ||
315 | mke2fs_error_msg_and_die(retval, "mark bad blocks as used"); | ||
316 | |||
317 | while (ext2fs_badblocks_list_iterate(bb_iter, &blk)) | ||
318 | ext2fs_mark_block_bitmap(fs->block_map, blk); | ||
319 | ext2fs_badblocks_list_iterate_end(bb_iter); | ||
320 | } | ||
321 | |||
322 | /* | ||
323 | * These functions implement a generalized progress meter. | ||
324 | */ | ||
325 | struct progress_struct { | ||
326 | char format[20]; | ||
327 | char backup[80]; | ||
328 | __u32 max; | ||
329 | int skip_progress; | ||
330 | }; | ||
331 | |||
332 | static void progress_init(struct progress_struct *progress, | ||
333 | const char *label,__u32 max) | ||
334 | { | ||
335 | int i; | ||
336 | |||
337 | memset(progress, 0, sizeof(struct progress_struct)); | ||
338 | if (quiet) | ||
339 | return; | ||
340 | |||
341 | /* | ||
342 | * Figure out how many digits we need | ||
343 | */ | ||
344 | i = int_log10(max); | ||
345 | sprintf(progress->format, "%%%dd/%%%dld", i, i); | ||
346 | memset(progress->backup, '\b', sizeof(progress->backup)-1); | ||
347 | progress->backup[sizeof(progress->backup)-1] = 0; | ||
348 | if ((2*i)+1 < (int) sizeof(progress->backup)) | ||
349 | progress->backup[(2*i)+1] = 0; | ||
350 | progress->max = max; | ||
351 | |||
352 | progress->skip_progress = 0; | ||
353 | if (getenv("MKE2FS_SKIP_PROGRESS")) | ||
354 | progress->skip_progress++; | ||
355 | |||
356 | fputs(label, stdout); | ||
357 | fflush(stdout); | ||
358 | } | ||
359 | |||
360 | static void progress_update(struct progress_struct *progress, __u32 val) | ||
361 | { | ||
362 | if ((progress->format[0] == 0) || progress->skip_progress) | ||
363 | return; | ||
364 | printf(progress->format, val, progress->max); | ||
365 | fputs(progress->backup, stdout); | ||
366 | } | ||
367 | |||
368 | static void progress_close(struct progress_struct *progress) | ||
369 | { | ||
370 | if (progress->format[0] == 0) | ||
371 | return; | ||
372 | printf("%-28s\n", "done"); | ||
373 | } | ||
374 | |||
375 | |||
376 | /* | ||
377 | * Helper function which zeros out _num_ blocks starting at _blk_. In | ||
378 | * case of an error, the details of the error is returned via _ret_blk_ | ||
379 | * and _ret_count_ if they are non-NULL pointers. Returns 0 on | ||
380 | * success, and an error code on an error. | ||
381 | * | ||
382 | * As a special case, if the first argument is NULL, then it will | ||
383 | * attempt to free the static zeroizing buffer. (This is to keep | ||
384 | * programs that check for memory leaks happy.) | ||
385 | */ | ||
386 | static errcode_t zero_blocks(ext2_filsys fs, blk_t blk, int num, | ||
387 | struct progress_struct *progress, | ||
388 | blk_t *ret_blk, int *ret_count) | ||
389 | { | ||
390 | int j, count, next_update, next_update_incr; | ||
391 | static char *buf; | ||
392 | errcode_t retval; | ||
393 | |||
394 | /* If fs is null, clean up the static buffer and return */ | ||
395 | if (!fs) { | ||
396 | if (buf) { | ||
397 | free(buf); | ||
398 | buf = 0; | ||
399 | } | ||
400 | return 0; | ||
401 | } | ||
402 | /* Allocate the zeroizing buffer if necessary */ | ||
403 | if (!buf) { | ||
404 | buf = xzalloc(fs->blocksize * STRIDE_LENGTH); | ||
405 | } | ||
406 | /* OK, do the write loop */ | ||
407 | next_update = 0; | ||
408 | next_update_incr = num / 100; | ||
409 | if (next_update_incr < 1) | ||
410 | next_update_incr = 1; | ||
411 | for (j=0; j < num; j += STRIDE_LENGTH, blk += STRIDE_LENGTH) { | ||
412 | count = num - j; | ||
413 | if (count > STRIDE_LENGTH) | ||
414 | count = STRIDE_LENGTH; | ||
415 | retval = io_channel_write_blk(fs->io, blk, count, buf); | ||
416 | if (retval) { | ||
417 | if (ret_count) | ||
418 | *ret_count = count; | ||
419 | if (ret_blk) | ||
420 | *ret_blk = blk; | ||
421 | return retval; | ||
422 | } | ||
423 | if (progress && j > next_update) { | ||
424 | next_update += num / 100; | ||
425 | progress_update(progress, blk); | ||
426 | } | ||
427 | } | ||
428 | return 0; | ||
429 | } | ||
430 | |||
431 | static void write_inode_tables(ext2_filsys fs) | ||
432 | { | ||
433 | errcode_t retval; | ||
434 | blk_t blk; | ||
435 | dgrp_t i; | ||
436 | int num; | ||
437 | struct progress_struct progress; | ||
438 | |||
439 | if (quiet) | ||
440 | memset(&progress, 0, sizeof(progress)); | ||
441 | else | ||
442 | progress_init(&progress, "Writing inode tables: ", | ||
443 | fs->group_desc_count); | ||
444 | |||
445 | for (i = 0; i < fs->group_desc_count; i++) { | ||
446 | progress_update(&progress, i); | ||
447 | |||
448 | blk = fs->group_desc[i].bg_inode_table; | ||
449 | num = fs->inode_blocks_per_group; | ||
450 | |||
451 | retval = zero_blocks(fs, blk, num, 0, &blk, &num); | ||
452 | mke2fs_error_msg_and_die(retval, | ||
453 | "write %d blocks in inode table starting at %d.", | ||
454 | num, blk); | ||
455 | if (sync_kludge) { | ||
456 | if (sync_kludge == 1) | ||
457 | sync(); | ||
458 | else if ((i % sync_kludge) == 0) | ||
459 | sync(); | ||
460 | } | ||
461 | } | ||
462 | zero_blocks(0, 0, 0, 0, 0, 0); | ||
463 | progress_close(&progress); | ||
464 | } | ||
465 | |||
466 | static void create_root_dir(ext2_filsys fs) | ||
467 | { | ||
468 | errcode_t retval; | ||
469 | struct ext2_inode inode; | ||
470 | |||
471 | retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, 0); | ||
472 | mke2fs_error_msg_and_die(retval, "create root dir"); | ||
473 | if (geteuid()) { | ||
474 | retval = ext2fs_read_inode(fs, EXT2_ROOT_INO, &inode); | ||
475 | mke2fs_error_msg_and_die(retval, "read root inode"); | ||
476 | inode.i_uid = getuid(); | ||
477 | if (inode.i_uid) | ||
478 | inode.i_gid = getgid(); | ||
479 | retval = ext2fs_write_new_inode(fs, EXT2_ROOT_INO, &inode); | ||
480 | mke2fs_error_msg_and_die(retval, "set root inode ownership"); | ||
481 | } | ||
482 | } | ||
483 | |||
484 | static void create_lost_and_found(ext2_filsys fs) | ||
485 | { | ||
486 | errcode_t retval; | ||
487 | ext2_ino_t ino; | ||
488 | const char *name = "lost+found"; | ||
489 | int i = 1; | ||
490 | char *msg = "create"; | ||
491 | int lpf_size = 0; | ||
492 | |||
493 | fs->umask = 077; | ||
494 | retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, 0, name); | ||
495 | if (retval) { | ||
496 | goto CREATE_LOST_AND_FOUND_ERROR; | ||
497 | } | ||
498 | |||
499 | retval = ext2fs_lookup(fs, EXT2_ROOT_INO, name, strlen(name), 0, &ino); | ||
500 | if (retval) { | ||
501 | msg = "lookup"; | ||
502 | goto CREATE_LOST_AND_FOUND_ERROR; | ||
503 | } | ||
504 | |||
505 | for (; i < EXT2_NDIR_BLOCKS; i++) { | ||
506 | if ((lpf_size += fs->blocksize) >= 16*1024) | ||
507 | break; | ||
508 | retval = ext2fs_expand_dir(fs, ino); | ||
509 | msg = "expand"; | ||
510 | CREATE_LOST_AND_FOUND_ERROR: | ||
511 | mke2fs_error_msg_and_die(retval, "%s %s", msg, name); | ||
512 | } | ||
513 | } | ||
514 | |||
515 | static void create_bad_block_inode(ext2_filsys fs, badblocks_list bb_list) | ||
516 | { | ||
517 | errcode_t retval; | ||
518 | |||
519 | ext2fs_mark_inode_bitmap(fs->inode_map, EXT2_BAD_INO); | ||
520 | fs->group_desc[0].bg_free_inodes_count--; | ||
521 | fs->super->s_free_inodes_count--; | ||
522 | retval = ext2fs_update_bb_inode(fs, bb_list); | ||
523 | mke2fs_error_msg_and_die(retval, "set bad block inode"); | ||
524 | } | ||
525 | |||
526 | static void reserve_inodes(ext2_filsys fs) | ||
527 | { | ||
528 | ext2_ino_t i; | ||
529 | int group; | ||
530 | |||
531 | for (i = EXT2_ROOT_INO + 1; i < EXT2_FIRST_INODE(fs->super); i++) { | ||
532 | ext2fs_mark_inode_bitmap(fs->inode_map, i); | ||
533 | group = ext2fs_group_of_ino(fs, i); | ||
534 | fs->group_desc[group].bg_free_inodes_count--; | ||
535 | fs->super->s_free_inodes_count--; | ||
536 | } | ||
537 | ext2fs_mark_ib_dirty(fs); | ||
538 | } | ||
539 | |||
540 | #define BSD_DISKMAGIC (0x82564557UL) /* The disk magic number */ | ||
541 | #define BSD_MAGICDISK (0x57455682UL) /* The disk magic number reversed */ | ||
542 | #define BSD_LABEL_OFFSET 64 | ||
543 | |||
544 | static void zap_sector(ext2_filsys fs, int sect, int nsect) | ||
545 | { | ||
546 | char *buf; | ||
547 | char *fmt = "could not %s %d"; | ||
548 | int retval; | ||
549 | unsigned int *magic; | ||
550 | |||
551 | buf = xmalloc(512*nsect); | ||
552 | |||
553 | if (sect == 0) { | ||
554 | /* Check for a BSD disklabel, and don't erase it if so */ | ||
555 | retval = io_channel_read_blk(fs->io, 0, -512, buf); | ||
556 | if (retval) | ||
557 | mke2fs_warning_msg(retval, fmt, "read block", 0); | ||
558 | else { | ||
559 | magic = (unsigned int *) (buf + BSD_LABEL_OFFSET); | ||
560 | if ((*magic == BSD_DISKMAGIC) || | ||
561 | (*magic == BSD_MAGICDISK)) | ||
562 | return; | ||
563 | } | ||
564 | } | ||
565 | |||
566 | memset(buf, 0, 512*nsect); | ||
567 | io_channel_set_blksize(fs->io, 512); | ||
568 | retval = io_channel_write_blk(fs->io, sect, -512*nsect, buf); | ||
569 | io_channel_set_blksize(fs->io, fs->blocksize); | ||
570 | free(buf); | ||
571 | mke2fs_warning_msg(retval, fmt, "erase sector", sect); | ||
572 | } | ||
573 | |||
574 | static void create_journal_dev(ext2_filsys fs) | ||
575 | { | ||
576 | struct progress_struct progress; | ||
577 | errcode_t retval; | ||
578 | char *buf; | ||
579 | char *fmt = "%s journal superblock"; | ||
580 | blk_t blk; | ||
581 | int count; | ||
582 | |||
583 | retval = ext2fs_create_journal_superblock(fs, | ||
584 | fs->super->s_blocks_count, 0, &buf); | ||
585 | mke2fs_error_msg_and_die(retval, fmt, "init"); | ||
586 | if (quiet) | ||
587 | memset(&progress, 0, sizeof(progress)); | ||
588 | else | ||
589 | progress_init(&progress, "Zeroing journal device: ", | ||
590 | fs->super->s_blocks_count); | ||
591 | |||
592 | retval = zero_blocks(fs, 0, fs->super->s_blocks_count, | ||
593 | &progress, &blk, &count); | ||
594 | mke2fs_error_msg_and_die(retval, "zero journal device (block %u, count %d)", | ||
595 | blk, count); | ||
596 | zero_blocks(0, 0, 0, 0, 0, 0); | ||
597 | |||
598 | retval = io_channel_write_blk(fs->io, | ||
599 | fs->super->s_first_data_block+1, | ||
600 | 1, buf); | ||
601 | mke2fs_error_msg_and_die(retval, fmt, "write"); | ||
602 | progress_close(&progress); | ||
603 | } | ||
604 | |||
605 | static void show_stats(ext2_filsys fs) | ||
606 | { | ||
607 | struct ext2_super_block *s = fs->super; | ||
608 | char *os; | ||
609 | blk_t group_block; | ||
610 | dgrp_t i; | ||
611 | int need, col_left; | ||
612 | |||
613 | mke2fs_warning_msg((param.s_blocks_count != s->s_blocks_count), | ||
614 | "%d blocks unused\n", param.s_blocks_count - s->s_blocks_count); | ||
615 | os = e2p_os2string(fs->super->s_creator_os); | ||
616 | printf( "Filesystem label=%.*s\n" | ||
617 | "OS type: %s\n" | ||
618 | "Block size=%u (log=%u)\n" | ||
619 | "Fragment size=%u (log=%u)\n" | ||
620 | "%u inodes, %u blocks\n" | ||
621 | "%u blocks (%2.2f%%) reserved for the super user\n" | ||
622 | "First data block=%u\n", | ||
623 | (int) sizeof(s->s_volume_name), | ||
624 | s->s_volume_name, | ||
625 | os, | ||
626 | fs->blocksize, s->s_log_block_size, | ||
627 | fs->fragsize, s->s_log_frag_size, | ||
628 | s->s_inodes_count, s->s_blocks_count, | ||
629 | s->s_r_blocks_count, 100.0 * s->s_r_blocks_count / s->s_blocks_count, | ||
630 | s->s_first_data_block); | ||
631 | free(os); | ||
632 | if (s->s_reserved_gdt_blocks) { | ||
633 | printf("Maximum filesystem blocks=%lu\n", | ||
634 | (s->s_reserved_gdt_blocks + fs->desc_blocks) * | ||
635 | (fs->blocksize / sizeof(struct ext2_group_desc)) * | ||
636 | s->s_blocks_per_group); | ||
637 | } | ||
638 | printf( "%u block group%s\n" | ||
639 | "%u blocks per group, %u fragments per group\n" | ||
640 | "%u inodes per group\n", | ||
641 | fs->group_desc_count, (fs->group_desc_count > 1) ? "s" : "", | ||
642 | s->s_blocks_per_group, s->s_frags_per_group, | ||
643 | s->s_inodes_per_group); | ||
644 | if (fs->group_desc_count == 1) { | ||
645 | puts(""); | ||
646 | return; | ||
647 | } | ||
648 | |||
649 | printf("Superblock backups stored on blocks: "); | ||
650 | group_block = s->s_first_data_block; | ||
651 | col_left = 0; | ||
652 | for (i = 1; i < fs->group_desc_count; i++) { | ||
653 | group_block += s->s_blocks_per_group; | ||
654 | if (!ext2fs_bg_has_super(fs, i)) | ||
655 | continue; | ||
656 | if (i != 1) | ||
657 | printf(", "); | ||
658 | need = int_log10(group_block) + 2; | ||
659 | if (need > col_left) { | ||
660 | printf("\n\t"); | ||
661 | col_left = 72; | ||
662 | } | ||
663 | col_left -= need; | ||
664 | printf("%u", group_block); | ||
665 | } | ||
666 | puts("\n"); | ||
667 | } | ||
668 | |||
669 | /* | ||
670 | * Set the S_CREATOR_OS field. Return true if OS is known, | ||
671 | * otherwise, 0. | ||
672 | */ | ||
673 | static int set_os(struct ext2_super_block *sb, char *os) | ||
674 | { | ||
675 | if (isdigit (*os)) { | ||
676 | sb->s_creator_os = atoi(os); | ||
677 | return 1; | ||
678 | } | ||
679 | |||
680 | if((sb->s_creator_os = e2p_string2os(os)) >= 0) { | ||
681 | return 1; | ||
682 | } else if (!strcasecmp("GNU", os)) { | ||
683 | sb->s_creator_os = EXT2_OS_HURD; | ||
684 | return 1; | ||
685 | } | ||
686 | return 0; | ||
687 | } | ||
688 | |||
689 | static void parse_extended_opts(struct ext2_super_block *sb_param, | ||
690 | const char *opts) | ||
691 | { | ||
692 | char *buf, *token, *next, *p, *arg; | ||
693 | int r_usage = 0; | ||
694 | |||
695 | buf = xstrdup(opts); | ||
696 | for (token = buf; token && *token; token = next) { | ||
697 | p = strchr(token, ','); | ||
698 | next = 0; | ||
699 | if (p) { | ||
700 | *p = 0; | ||
701 | next = p+1; | ||
702 | } | ||
703 | arg = strchr(token, '='); | ||
704 | if (arg) { | ||
705 | *arg = 0; | ||
706 | arg++; | ||
707 | } | ||
708 | if (strcmp(token, "stride") == 0) { | ||
709 | if (!arg) { | ||
710 | r_usage++; | ||
711 | continue; | ||
712 | } | ||
713 | fs_stride = strtoul(arg, &p, 0); | ||
714 | if (*p || (fs_stride == 0)) { | ||
715 | bb_error_msg("Invalid stride parameter: %s", arg); | ||
716 | r_usage++; | ||
717 | continue; | ||
718 | } | ||
719 | } else if (!strcmp(token, "resize")) { | ||
720 | unsigned long resize, bpg, rsv_groups; | ||
721 | unsigned long group_desc_count, desc_blocks; | ||
722 | unsigned int gdpb, blocksize; | ||
723 | int rsv_gdb; | ||
724 | |||
725 | if (!arg) { | ||
726 | r_usage++; | ||
727 | continue; | ||
728 | } | ||
729 | |||
730 | resize = parse_num_blocks(arg, | ||
731 | sb_param->s_log_block_size); | ||
732 | |||
733 | if (resize == 0) { | ||
734 | bb_error_msg("Invalid resize parameter: %s", arg); | ||
735 | r_usage++; | ||
736 | continue; | ||
737 | } | ||
738 | if (resize <= sb_param->s_blocks_count) { | ||
739 | bb_error_msg("The resize maximum must be greater " | ||
740 | "than the filesystem size"); | ||
741 | r_usage++; | ||
742 | continue; | ||
743 | } | ||
744 | |||
745 | blocksize = EXT2_BLOCK_SIZE(sb_param); | ||
746 | bpg = sb_param->s_blocks_per_group; | ||
747 | if (!bpg) | ||
748 | bpg = blocksize * 8; | ||
749 | gdpb = blocksize / sizeof(struct ext2_group_desc); | ||
750 | group_desc_count = (sb_param->s_blocks_count + | ||
751 | bpg - 1) / bpg; | ||
752 | desc_blocks = (group_desc_count + | ||
753 | gdpb - 1) / gdpb; | ||
754 | rsv_groups = (resize + bpg - 1) / bpg; | ||
755 | rsv_gdb = (rsv_groups + gdpb - 1) / gdpb - | ||
756 | desc_blocks; | ||
757 | if (rsv_gdb > EXT2_ADDR_PER_BLOCK(sb_param)) | ||
758 | rsv_gdb = EXT2_ADDR_PER_BLOCK(sb_param); | ||
759 | |||
760 | if (rsv_gdb > 0) { | ||
761 | sb_param->s_feature_compat |= | ||
762 | EXT2_FEATURE_COMPAT_RESIZE_INODE; | ||
763 | |||
764 | sb_param->s_reserved_gdt_blocks = rsv_gdb; | ||
765 | } | ||
766 | } else | ||
767 | r_usage++; | ||
768 | } | ||
769 | if (r_usage) { | ||
770 | bb_error_msg_and_die( | ||
771 | "\nBad options specified.\n\n" | ||
772 | "Extended options are separated by commas, " | ||
773 | "and may take an argument which\n" | ||
774 | "\tis set off by an equals ('=') sign.\n\n" | ||
775 | "Valid extended options are:\n" | ||
776 | "\tstride=<stride length in blocks>\n" | ||
777 | "\tresize=<resize maximum size in blocks>\n"); | ||
778 | } | ||
779 | } | ||
780 | |||
781 | static __u32 ok_features[3] = { | ||
782 | EXT3_FEATURE_COMPAT_HAS_JOURNAL | | ||
783 | EXT2_FEATURE_COMPAT_RESIZE_INODE | | ||
784 | EXT2_FEATURE_COMPAT_DIR_INDEX, /* Compat */ | ||
785 | EXT2_FEATURE_INCOMPAT_FILETYPE| /* Incompat */ | ||
786 | EXT3_FEATURE_INCOMPAT_JOURNAL_DEV| | ||
787 | EXT2_FEATURE_INCOMPAT_META_BG, | ||
788 | EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER /* R/O compat */ | ||
789 | }; | ||
790 | |||
791 | static int PRS(int argc, char *argv[]) | ||
792 | { | ||
793 | int c; | ||
794 | int size; | ||
795 | char * tmp; | ||
796 | int blocksize = 0; | ||
797 | int inode_ratio = 0; | ||
798 | int inode_size = 0; | ||
799 | int reserved_ratio = 5; | ||
800 | int sector_size = 0; | ||
801 | int show_version_only = 0; | ||
802 | ext2_ino_t num_inodes = 0; | ||
803 | errcode_t retval; | ||
804 | char * extended_opts = 0; | ||
805 | const char * fs_type = 0; | ||
806 | blk_t dev_size; | ||
807 | long sysval; | ||
808 | |||
809 | /* Update our PATH to include /sbin */ | ||
810 | e2fs_set_sbin_path(); | ||
811 | |||
812 | tmp = getenv("MKE2FS_SYNC"); | ||
813 | if (tmp) | ||
814 | sync_kludge = atoi(tmp); | ||
815 | |||
816 | /* Determine the system page size if possible */ | ||
817 | #if (!defined(_SC_PAGESIZE) && defined(_SC_PAGE_SIZE)) | ||
818 | #define _SC_PAGESIZE _SC_PAGE_SIZE | ||
819 | #endif | ||
820 | #ifdef _SC_PAGESIZE | ||
821 | sysval = sysconf(_SC_PAGESIZE); | ||
822 | if (sysval > 0) | ||
823 | sys_page_size = sysval; | ||
824 | #endif /* _SC_PAGESIZE */ | ||
825 | |||
826 | setbuf(stdout, NULL); | ||
827 | setbuf(stderr, NULL); | ||
828 | memset(¶m, 0, sizeof(struct ext2_super_block)); | ||
829 | param.s_rev_level = 1; /* Create revision 1 filesystems now */ | ||
830 | param.s_feature_incompat |= EXT2_FEATURE_INCOMPAT_FILETYPE; | ||
831 | param.s_feature_ro_compat |= EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER; | ||
832 | |||
833 | #ifdef __linux__ | ||
834 | linux_version_code = get_linux_version_code(); | ||
835 | if (linux_version_code && linux_version_code < KERNEL_VERSION(2,2,0)) { | ||
836 | param.s_rev_level = 0; | ||
837 | param.s_feature_incompat = 0; | ||
838 | param.s_feature_compat = 0; | ||
839 | param.s_feature_ro_compat = 0; | ||
840 | } | ||
841 | #endif | ||
842 | |||
843 | /* If called as mkfs.ext3, create a journal inode */ | ||
844 | if (last_char_is(applet_name, '3')) | ||
845 | journal_size = -1; | ||
846 | |||
847 | while ((c = getopt (argc, argv, | ||
848 | "b:cE:f:g:i:jl:m:no:qr:R:s:tvI:J:ST:FL:M:N:O:V")) != EOF) { | ||
849 | switch (c) { | ||
850 | case 'b': | ||
851 | blocksize = xatou_range(optarg, EXT2_MIN_BLOCK_SIZE, EXT2_MAX_BLOCK_SIZE); | ||
852 | mke2fs_warning_msg((blocksize > 4096), | ||
853 | "blocksize %d not usable on most systems", | ||
854 | blocksize); | ||
855 | param.s_log_block_size = | ||
856 | int_log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE); | ||
857 | break; | ||
858 | case 'c': /* Check for bad blocks */ | ||
859 | case 't': /* deprecated */ | ||
860 | cflag++; | ||
861 | break; | ||
862 | case 'f': | ||
863 | size = xatou_range(optarg, EXT2_MIN_BLOCK_SIZE, EXT2_MAX_BLOCK_SIZE); | ||
864 | param.s_log_frag_size = | ||
865 | int_log2(size >> EXT2_MIN_BLOCK_LOG_SIZE); | ||
866 | mke2fs_warning_msg(1, "fragments not supported. Ignoring -f option"); | ||
867 | break; | ||
868 | case 'g': | ||
869 | param.s_blocks_per_group = xatou32(optarg); | ||
870 | if ((param.s_blocks_per_group % 8) != 0) { | ||
871 | bb_error_msg_and_die("blocks per group must be multiple of 8"); | ||
872 | } | ||
873 | break; | ||
874 | case 'i': | ||
875 | /* Huh? is "* 1024" correct? */ | ||
876 | inode_ratio = xatou_range(optarg, EXT2_MIN_BLOCK_SIZE, EXT2_MAX_BLOCK_SIZE * 1024); | ||
877 | break; | ||
878 | case 'J': | ||
879 | parse_journal_opts(&journal_device, &journal_flags, &journal_size, optarg); | ||
880 | break; | ||
881 | case 'j': | ||
882 | param.s_feature_compat |= | ||
883 | EXT3_FEATURE_COMPAT_HAS_JOURNAL; | ||
884 | if (!journal_size) | ||
885 | journal_size = -1; | ||
886 | break; | ||
887 | case 'l': | ||
888 | bad_blocks_filename = optarg; | ||
889 | break; | ||
890 | case 'm': | ||
891 | reserved_ratio = xatou_range(optarg, 0, 50); | ||
892 | break; | ||
893 | case 'n': | ||
894 | noaction++; | ||
895 | break; | ||
896 | case 'o': | ||
897 | creator_os = optarg; | ||
898 | break; | ||
899 | case 'r': | ||
900 | param.s_rev_level = xatoi_u(optarg); | ||
901 | if (param.s_rev_level == EXT2_GOOD_OLD_REV) { | ||
902 | param.s_feature_incompat = 0; | ||
903 | param.s_feature_compat = 0; | ||
904 | param.s_feature_ro_compat = 0; | ||
905 | } | ||
906 | break; | ||
907 | case 's': /* deprecated */ | ||
908 | if (xatou(optarg)) | ||
909 | param.s_feature_ro_compat |= | ||
910 | EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER; | ||
911 | else | ||
912 | param.s_feature_ro_compat &= | ||
913 | ~EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER; | ||
914 | break; | ||
915 | #ifdef EXT2_DYNAMIC_REV | ||
916 | case 'I': | ||
917 | inode_size = xatoi_u(optarg); | ||
918 | break; | ||
919 | #endif | ||
920 | case 'N': | ||
921 | num_inodes = xatoi_u(optarg); | ||
922 | break; | ||
923 | case 'v': | ||
924 | quiet = 0; | ||
925 | break; | ||
926 | case 'q': | ||
927 | quiet = 1; | ||
928 | break; | ||
929 | case 'F': | ||
930 | force = 1; | ||
931 | break; | ||
932 | case 'L': | ||
933 | volume_label = optarg; | ||
934 | break; | ||
935 | case 'M': | ||
936 | mount_dir = optarg; | ||
937 | break; | ||
938 | case 'O': | ||
939 | if (!strcmp(optarg, "none")) { | ||
940 | param.s_feature_compat = 0; | ||
941 | param.s_feature_incompat = 0; | ||
942 | param.s_feature_ro_compat = 0; | ||
943 | break; | ||
944 | } | ||
945 | if (e2p_edit_feature(optarg, | ||
946 | ¶m.s_feature_compat, | ||
947 | ok_features)) { | ||
948 | bb_error_msg_and_die("Invalid filesystem option set: %s", optarg); | ||
949 | } | ||
950 | break; | ||
951 | case 'E': | ||
952 | case 'R': | ||
953 | extended_opts = optarg; | ||
954 | break; | ||
955 | case 'S': | ||
956 | super_only = 1; | ||
957 | break; | ||
958 | case 'T': | ||
959 | fs_type = optarg; | ||
960 | break; | ||
961 | case 'V': | ||
962 | /* Print version number and exit */ | ||
963 | show_version_only = 1; | ||
964 | quiet = 0; | ||
965 | break; | ||
966 | default: | ||
967 | bb_show_usage(); | ||
968 | } | ||
969 | } | ||
970 | if ((optind == argc) /*&& !show_version_only*/) | ||
971 | bb_show_usage(); | ||
972 | device_name = argv[optind++]; | ||
973 | |||
974 | mke2fs_verbose("mke2fs %s (%s)\n", E2FSPROGS_VERSION, E2FSPROGS_DATE); | ||
975 | |||
976 | if (show_version_only) { | ||
977 | return 0; | ||
978 | } | ||
979 | |||
980 | /* | ||
981 | * If there's no blocksize specified and there is a journal | ||
982 | * device, use it to figure out the blocksize | ||
983 | */ | ||
984 | if (blocksize <= 0 && journal_device) { | ||
985 | ext2_filsys jfs; | ||
986 | io_manager io_ptr; | ||
987 | |||
988 | #ifdef CONFIG_TESTIO_DEBUG | ||
989 | io_ptr = test_io_manager; | ||
990 | test_io_backing_manager = unix_io_manager; | ||
991 | #else | ||
992 | io_ptr = unix_io_manager; | ||
993 | #endif | ||
994 | retval = ext2fs_open(journal_device, | ||
995 | EXT2_FLAG_JOURNAL_DEV_OK, 0, | ||
996 | 0, io_ptr, &jfs); | ||
997 | mke2fs_error_msg_and_die(retval, "open journal device %s", journal_device); | ||
998 | if ((blocksize < 0) && (jfs->blocksize < (unsigned) (-blocksize))) { | ||
999 | bb_error_msg_and_die( | ||
1000 | "Journal dev blocksize (%d) smaller than " | ||
1001 | "minimum blocksize %d\n", jfs->blocksize, | ||
1002 | -blocksize); | ||
1003 | } | ||
1004 | blocksize = jfs->blocksize; | ||
1005 | param.s_log_block_size = | ||
1006 | int_log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE); | ||
1007 | ext2fs_close(jfs); | ||
1008 | } | ||
1009 | |||
1010 | if (blocksize > sys_page_size) { | ||
1011 | mke2fs_warning_msg(1, "%d-byte blocks too big for system (max %d)", | ||
1012 | blocksize, sys_page_size); | ||
1013 | if (!force) { | ||
1014 | proceed_question(); | ||
1015 | } | ||
1016 | bb_error_msg("Forced to continue"); | ||
1017 | } | ||
1018 | mke2fs_warning_msg(((blocksize > 4096) && | ||
1019 | (param.s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL)), | ||
1020 | "some 2.4 kernels do not support " | ||
1021 | "blocksizes greater than 4096 using ext3.\n" | ||
1022 | "Use -b 4096 if this is an issue for you\n"); | ||
1023 | |||
1024 | if (optind < argc) { | ||
1025 | param.s_blocks_count = parse_num_blocks(argv[optind++], | ||
1026 | param.s_log_block_size); | ||
1027 | mke2fs_error_msg_and_die(!param.s_blocks_count, "invalid blocks count - %s", argv[optind - 1]); | ||
1028 | } | ||
1029 | if (optind < argc) | ||
1030 | bb_show_usage(); | ||
1031 | |||
1032 | if (param.s_feature_incompat & EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) { | ||
1033 | if (!fs_type) | ||
1034 | fs_type = "journal"; | ||
1035 | reserved_ratio = 0; | ||
1036 | param.s_feature_incompat = EXT3_FEATURE_INCOMPAT_JOURNAL_DEV; | ||
1037 | param.s_feature_compat = 0; | ||
1038 | param.s_feature_ro_compat = 0; | ||
1039 | } | ||
1040 | if (param.s_rev_level == EXT2_GOOD_OLD_REV && | ||
1041 | (param.s_feature_compat || param.s_feature_ro_compat || | ||
1042 | param.s_feature_incompat)) | ||
1043 | param.s_rev_level = 1; /* Create a revision 1 filesystem */ | ||
1044 | |||
1045 | check_plausibility(device_name , force); | ||
1046 | check_mount(device_name, force, "filesystem"); | ||
1047 | |||
1048 | param.s_log_frag_size = param.s_log_block_size; | ||
1049 | |||
1050 | if (noaction && param.s_blocks_count) { | ||
1051 | dev_size = param.s_blocks_count; | ||
1052 | retval = 0; | ||
1053 | } else { | ||
1054 | retry: | ||
1055 | retval = ext2fs_get_device_size(device_name, | ||
1056 | EXT2_BLOCK_SIZE(¶m), | ||
1057 | &dev_size); | ||
1058 | if ((retval == EFBIG) && | ||
1059 | (blocksize == 0) && | ||
1060 | (param.s_log_block_size == 0)) { | ||
1061 | param.s_log_block_size = 2; | ||
1062 | blocksize = 4096; | ||
1063 | goto retry; | ||
1064 | } | ||
1065 | } | ||
1066 | |||
1067 | mke2fs_error_msg_and_die((retval && (retval != EXT2_ET_UNIMPLEMENTED)),"determine filesystem size"); | ||
1068 | |||
1069 | if (!param.s_blocks_count) { | ||
1070 | if (retval == EXT2_ET_UNIMPLEMENTED) { | ||
1071 | mke2fs_error_msg_and_die(1, | ||
1072 | "determine device size; you " | ||
1073 | "must specify\nthe size of the " | ||
1074 | "filesystem"); | ||
1075 | } else { | ||
1076 | if (dev_size == 0) { | ||
1077 | bb_error_msg_and_die( | ||
1078 | "Device size reported to be zero. " | ||
1079 | "Invalid partition specified, or\n\t" | ||
1080 | "partition table wasn't reread " | ||
1081 | "after running fdisk, due to\n\t" | ||
1082 | "a modified partition being busy " | ||
1083 | "and in use. You may need to reboot\n\t" | ||
1084 | "to re-read your partition table.\n" | ||
1085 | ); | ||
1086 | } | ||
1087 | param.s_blocks_count = dev_size; | ||
1088 | if (sys_page_size > EXT2_BLOCK_SIZE(¶m)) | ||
1089 | param.s_blocks_count &= ~((sys_page_size / | ||
1090 | EXT2_BLOCK_SIZE(¶m))-1); | ||
1091 | } | ||
1092 | |||
1093 | } else if (!force && (param.s_blocks_count > dev_size)) { | ||
1094 | bb_error_msg("Filesystem larger than apparent device size"); | ||
1095 | proceed_question(); | ||
1096 | } | ||
1097 | |||
1098 | /* | ||
1099 | * If the user asked for HAS_JOURNAL, then make sure a journal | ||
1100 | * gets created. | ||
1101 | */ | ||
1102 | if ((param.s_feature_compat & EXT3_FEATURE_COMPAT_HAS_JOURNAL) && | ||
1103 | !journal_size) | ||
1104 | journal_size = -1; | ||
1105 | |||
1106 | /* Set first meta blockgroup via an environment variable */ | ||
1107 | /* (this is mostly for debugging purposes) */ | ||
1108 | if ((param.s_feature_incompat & EXT2_FEATURE_INCOMPAT_META_BG) && | ||
1109 | ((tmp = getenv("MKE2FS_FIRST_META_BG")))) | ||
1110 | param.s_first_meta_bg = atoi(tmp); | ||
1111 | |||
1112 | /* Get the hardware sector size, if available */ | ||
1113 | retval = ext2fs_get_device_sectsize(device_name, §or_size); | ||
1114 | mke2fs_error_msg_and_die(retval, "determine hardware sector size"); | ||
1115 | |||
1116 | if ((tmp = getenv("MKE2FS_DEVICE_SECTSIZE")) != NULL) | ||
1117 | sector_size = atoi(tmp); | ||
1118 | |||
1119 | set_fs_defaults(fs_type, ¶m, blocksize, sector_size, &inode_ratio); | ||
1120 | blocksize = EXT2_BLOCK_SIZE(¶m); | ||
1121 | |||
1122 | if (extended_opts) | ||
1123 | parse_extended_opts(¶m, extended_opts); | ||
1124 | |||
1125 | /* Since sparse_super is the default, we would only have a problem | ||
1126 | * here if it was explicitly disabled. | ||
1127 | */ | ||
1128 | if ((param.s_feature_compat & EXT2_FEATURE_COMPAT_RESIZE_INODE) && | ||
1129 | !(param.s_feature_ro_compat&EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER)) { | ||
1130 | bb_error_msg_and_die("reserved online resize blocks not supported " | ||
1131 | "on non-sparse filesystem"); | ||
1132 | } | ||
1133 | |||
1134 | if (param.s_blocks_per_group) { | ||
1135 | if (param.s_blocks_per_group < 256 || | ||
1136 | param.s_blocks_per_group > 8 * (unsigned) blocksize) { | ||
1137 | bb_error_msg_and_die("blocks per group count out of range"); | ||
1138 | } | ||
1139 | } | ||
1140 | |||
1141 | if (!force && param.s_blocks_count >= (1 << 31)) { | ||
1142 | bb_error_msg_and_die("Filesystem too large. No more than 2**31-1 blocks\n" | ||
1143 | "\t (8TB using a blocksize of 4k) are currently supported."); | ||
1144 | } | ||
1145 | |||
1146 | if (inode_size) { | ||
1147 | if (inode_size < EXT2_GOOD_OLD_INODE_SIZE || | ||
1148 | inode_size > EXT2_BLOCK_SIZE(¶m) || | ||
1149 | inode_size & (inode_size - 1)) { | ||
1150 | bb_error_msg_and_die("invalid inode size %d (min %d/max %d)", | ||
1151 | inode_size, EXT2_GOOD_OLD_INODE_SIZE, | ||
1152 | blocksize); | ||
1153 | } | ||
1154 | mke2fs_warning_msg((inode_size != EXT2_GOOD_OLD_INODE_SIZE), | ||
1155 | "%d-byte inodes not usable on most systems", | ||
1156 | inode_size); | ||
1157 | param.s_inode_size = inode_size; | ||
1158 | } | ||
1159 | |||
1160 | /* | ||
1161 | * Calculate number of inodes based on the inode ratio | ||
1162 | */ | ||
1163 | param.s_inodes_count = num_inodes ? num_inodes : | ||
1164 | ((__u64) param.s_blocks_count * blocksize) | ||
1165 | / inode_ratio; | ||
1166 | |||
1167 | /* | ||
1168 | * Calculate number of blocks to reserve | ||
1169 | */ | ||
1170 | param.s_r_blocks_count = (param.s_blocks_count * reserved_ratio) / 100; | ||
1171 | return 1; | ||
1172 | } | ||
1173 | |||
1174 | static void mke2fs_clean_up(void) | ||
1175 | { | ||
1176 | if (ENABLE_FEATURE_CLEAN_UP && journal_device) free(journal_device); | ||
1177 | } | ||
1178 | |||
1179 | int mke2fs_main (int argc, char *argv[]) | ||
1180 | { | ||
1181 | errcode_t retval; | ||
1182 | ext2_filsys fs; | ||
1183 | badblocks_list bb_list = 0; | ||
1184 | unsigned int i; | ||
1185 | int val; | ||
1186 | io_manager io_ptr; | ||
1187 | |||
1188 | if (ENABLE_FEATURE_CLEAN_UP) | ||
1189 | atexit(mke2fs_clean_up); | ||
1190 | if(!PRS(argc, argv)) | ||
1191 | return 0; | ||
1192 | |||
1193 | #ifdef CONFIG_TESTIO_DEBUG | ||
1194 | io_ptr = test_io_manager; | ||
1195 | test_io_backing_manager = unix_io_manager; | ||
1196 | #else | ||
1197 | io_ptr = unix_io_manager; | ||
1198 | #endif | ||
1199 | |||
1200 | /* | ||
1201 | * Initialize the superblock.... | ||
1202 | */ | ||
1203 | retval = ext2fs_initialize(device_name, 0, ¶m, | ||
1204 | io_ptr, &fs); | ||
1205 | mke2fs_error_msg_and_die(retval, "set up superblock"); | ||
1206 | |||
1207 | /* | ||
1208 | * Wipe out the old on-disk superblock | ||
1209 | */ | ||
1210 | if (!noaction) | ||
1211 | zap_sector(fs, 2, 6); | ||
1212 | |||
1213 | /* | ||
1214 | * Generate a UUID for it... | ||
1215 | */ | ||
1216 | uuid_generate(fs->super->s_uuid); | ||
1217 | |||
1218 | /* | ||
1219 | * Initialize the directory index variables | ||
1220 | */ | ||
1221 | fs->super->s_def_hash_version = EXT2_HASH_TEA; | ||
1222 | uuid_generate((unsigned char *) fs->super->s_hash_seed); | ||
1223 | |||
1224 | /* | ||
1225 | * Add "jitter" to the superblock's check interval so that we | ||
1226 | * don't check all the filesystems at the same time. We use a | ||
1227 | * kludgy hack of using the UUID to derive a random jitter value. | ||
1228 | */ | ||
1229 | for (i = 0, val = 0 ; i < sizeof(fs->super->s_uuid); i++) | ||
1230 | val += fs->super->s_uuid[i]; | ||
1231 | fs->super->s_max_mnt_count += val % EXT2_DFL_MAX_MNT_COUNT; | ||
1232 | |||
1233 | /* | ||
1234 | * Override the creator OS, if applicable | ||
1235 | */ | ||
1236 | if (creator_os && !set_os(fs->super, creator_os)) { | ||
1237 | bb_error_msg_and_die("unknown os - %s", creator_os); | ||
1238 | } | ||
1239 | |||
1240 | /* | ||
1241 | * For the Hurd, we will turn off filetype since it doesn't | ||
1242 | * support it. | ||
1243 | */ | ||
1244 | if (fs->super->s_creator_os == EXT2_OS_HURD) | ||
1245 | fs->super->s_feature_incompat &= | ||
1246 | ~EXT2_FEATURE_INCOMPAT_FILETYPE; | ||
1247 | |||
1248 | /* | ||
1249 | * Set the volume label... | ||
1250 | */ | ||
1251 | if (volume_label) { | ||
1252 | snprintf(fs->super->s_volume_name, sizeof(fs->super->s_volume_name), "%s", volume_label); | ||
1253 | } | ||
1254 | |||
1255 | /* | ||
1256 | * Set the last mount directory | ||
1257 | */ | ||
1258 | if (mount_dir) { | ||
1259 | snprintf(fs->super->s_last_mounted, sizeof(fs->super->s_last_mounted), "%s", mount_dir); | ||
1260 | } | ||
1261 | |||
1262 | if (!quiet || noaction) | ||
1263 | show_stats(fs); | ||
1264 | |||
1265 | if (noaction) | ||
1266 | return 0; | ||
1267 | |||
1268 | if (fs->super->s_feature_incompat & | ||
1269 | EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) { | ||
1270 | create_journal_dev(fs); | ||
1271 | return (ext2fs_close(fs) ? 1 : 0); | ||
1272 | } | ||
1273 | |||
1274 | if (bad_blocks_filename) | ||
1275 | read_bb_file(fs, &bb_list, bad_blocks_filename); | ||
1276 | if (cflag) | ||
1277 | test_disk(fs, &bb_list); | ||
1278 | |||
1279 | handle_bad_blocks(fs, bb_list); | ||
1280 | fs->stride = fs_stride; | ||
1281 | retval = ext2fs_allocate_tables(fs); | ||
1282 | mke2fs_error_msg_and_die(retval, "allocate filesystem tables"); | ||
1283 | if (super_only) { | ||
1284 | fs->super->s_state |= EXT2_ERROR_FS; | ||
1285 | fs->flags &= ~(EXT2_FLAG_IB_DIRTY|EXT2_FLAG_BB_DIRTY); | ||
1286 | } else { | ||
1287 | /* rsv must be a power of two (64kB is MD RAID sb alignment) */ | ||
1288 | unsigned int rsv = 65536 / fs->blocksize; | ||
1289 | unsigned long blocks = fs->super->s_blocks_count; | ||
1290 | unsigned long start; | ||
1291 | blk_t ret_blk; | ||
1292 | |||
1293 | #ifdef ZAP_BOOTBLOCK | ||
1294 | zap_sector(fs, 0, 2); | ||
1295 | #endif | ||
1296 | |||
1297 | /* | ||
1298 | * Wipe out any old MD RAID (or other) metadata at the end | ||
1299 | * of the device. This will also verify that the device is | ||
1300 | * as large as we think. Be careful with very small devices. | ||
1301 | */ | ||
1302 | start = (blocks & ~(rsv - 1)); | ||
1303 | if (start > rsv) | ||
1304 | start -= rsv; | ||
1305 | if (start > 0) | ||
1306 | retval = zero_blocks(fs, start, blocks - start, | ||
1307 | NULL, &ret_blk, NULL); | ||
1308 | |||
1309 | mke2fs_warning_msg(retval, "cannot zero block %u at end of filesystem", ret_blk); | ||
1310 | write_inode_tables(fs); | ||
1311 | create_root_dir(fs); | ||
1312 | create_lost_and_found(fs); | ||
1313 | reserve_inodes(fs); | ||
1314 | create_bad_block_inode(fs, bb_list); | ||
1315 | if (fs->super->s_feature_compat & | ||
1316 | EXT2_FEATURE_COMPAT_RESIZE_INODE) { | ||
1317 | retval = ext2fs_create_resize_inode(fs); | ||
1318 | mke2fs_error_msg_and_die(retval, "reserve blocks for online resize"); | ||
1319 | } | ||
1320 | } | ||
1321 | |||
1322 | if (journal_device) { | ||
1323 | make_journal_device(journal_device, fs, quiet, force); | ||
1324 | } else if (journal_size) { | ||
1325 | make_journal_blocks(fs, journal_size, journal_flags, quiet); | ||
1326 | } | ||
1327 | |||
1328 | mke2fs_verbose("Writing superblocks and filesystem accounting information: "); | ||
1329 | retval = ext2fs_flush(fs); | ||
1330 | mke2fs_warning_msg(retval, "had trouble writing out superblocks"); | ||
1331 | mke2fs_verbose_done(); | ||
1332 | if (!quiet && !getenv("MKE2FS_SKIP_CHECK_MSG")) | ||
1333 | print_check_message(fs); | ||
1334 | val = ext2fs_close(fs); | ||
1335 | return (retval || val) ? 1 : 0; | ||
1336 | } | ||