]>
Commit | Line | Data |
---|---|---|
3839e657 TT |
1 | /* |
2 | * mke2fs.c - Make a ext2fs filesystem. | |
3 | * | |
19c78dc0 TT |
4 | * Copyright (C) 1994, 1995, 1996, 1997 Theodore Ts'o. |
5 | * | |
6 | * %Begin-Header% | |
7 | * This file may be redistributed under the terms of the GNU Public | |
8 | * License. | |
9 | * %End-Header% | |
3839e657 TT |
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 | ||
a418d3ad | 18 | #include <stdio.h> |
3839e657 TT |
19 | #include <string.h> |
20 | #include <fcntl.h> | |
21 | #include <ctype.h> | |
3839e657 | 22 | #include <time.h> |
2740156b TT |
23 | #ifdef linux |
24 | #include <sys/utsname.h> | |
25 | #endif | |
a418d3ad | 26 | #ifdef HAVE_GETOPT_H |
3839e657 | 27 | #include <getopt.h> |
a418d3ad TT |
28 | #endif |
29 | #ifdef HAVE_UNISTD_H | |
3839e657 | 30 | #include <unistd.h> |
a418d3ad TT |
31 | #endif |
32 | #ifdef HAVE_STDLIB_H | |
3839e657 | 33 | #include <stdlib.h> |
a418d3ad TT |
34 | #endif |
35 | #ifdef HAVE_ERRNO_H | |
36 | #include <errno.h> | |
37 | #endif | |
38 | #ifdef HAVE_MNTENT_H | |
3839e657 | 39 | #include <mntent.h> |
a418d3ad | 40 | #endif |
3839e657 | 41 | #include <sys/ioctl.h> |
f3db3566 TT |
42 | #include <sys/types.h> |
43 | ||
f3db3566 | 44 | #include <linux/ext2_fs.h> |
74becf3c TT |
45 | #ifdef HAVE_LINUX_MAJOR_H |
46 | #include <linux/major.h> | |
e6597048 | 47 | #include <sys/stat.h> /* Only need sys/stat.h for major nr test */ |
74becf3c | 48 | #endif |
3839e657 TT |
49 | |
50 | #include "et/com_err.h" | |
1e3472c5 | 51 | #include "uuid/uuid.h" |
896938d5 | 52 | #include "e2p/e2p.h" |
3839e657 TT |
53 | #include "ext2fs/ext2fs.h" |
54 | #include "../version.h" | |
d9c56d3c | 55 | #include "nls-enable.h" |
3839e657 | 56 | |
bbfa3aa9 TT |
57 | /* Everything is STDC, these days */ |
58 | #define NOARGS void | |
59 | ||
3839e657 TT |
60 | #define STRIDE_LENGTH 8 |
61 | ||
6733c2fd | 62 | #ifndef __sparc__ |
1e3472c5 TT |
63 | #define ZAP_BOOTBLOCK |
64 | #endif | |
65 | ||
3839e657 | 66 | extern int isatty(int); |
f3db3566 | 67 | extern FILE *fpopen(const char *cmd, const char *mode); |
3839e657 TT |
68 | |
69 | const char * program_name = "mke2fs"; | |
70 | const char * device_name = NULL; | |
71 | ||
72 | /* Command line options */ | |
73 | int cflag = 0; | |
74 | int verbose = 0; | |
75 | int quiet = 0; | |
f3db3566 | 76 | int super_only = 0; |
74becf3c | 77 | int force = 0; |
50787ea2 | 78 | int noaction = 0; |
3839e657 | 79 | char *bad_blocks_filename = 0; |
a29f4d30 | 80 | __u32 fs_stride = 0; |
3839e657 TT |
81 | |
82 | struct ext2_super_block param; | |
1e3472c5 TT |
83 | char *creator_os = NULL; |
84 | char *volume_label = NULL; | |
85 | char *mount_dir = NULL; | |
3839e657 | 86 | |
bbfa3aa9 TT |
87 | static void usage(NOARGS), check_plausibility(NOARGS), check_mount(NOARGS); |
88 | ||
3839e657 TT |
89 | static void usage(NOARGS) |
90 | { | |
d9c56d3c | 91 | fprintf(stderr, _("Usage: %s [-c|-t|-l filename] [-b block-size] " |
1e3472c5 | 92 | "[-f fragment-size]\n\t[-i bytes-per-inode] " |
5515e6b4 TT |
93 | " [-N number-of-inodes]\n\t[-m reserved-blocks-percentage] " |
94 | "[-o creator-os] [-g blocks-per-group]\n\t[-L volume-label] " | |
18160d26 TT |
95 | "[-M last-mounted-directory] [-O feature[,...]]\n\t" |
96 | "[-r fs-revision] [-R raid_opts] [-s sparse-super-flag]\n\t" | |
d9c56d3c | 97 | "[-qvSV] device [blocks-count]\n"), |
3839e657 TT |
98 | program_name); |
99 | exit(1); | |
100 | } | |
101 | ||
6733c2fd | 102 | static int int_log2(int arg) |
3839e657 TT |
103 | { |
104 | int l = 0; | |
105 | ||
106 | arg >>= 1; | |
107 | while (arg) { | |
108 | l++; | |
109 | arg >>= 1; | |
110 | } | |
111 | return l; | |
112 | } | |
113 | ||
6733c2fd | 114 | static int int_log10(unsigned int arg) |
1dde43f0 TT |
115 | { |
116 | int l; | |
117 | ||
118 | for (l=0; arg ; l++) | |
119 | arg = arg / 10; | |
120 | return l; | |
121 | } | |
122 | ||
a789d840 TT |
123 | static void proceed_question(NOARGS) |
124 | { | |
d9c56d3c TT |
125 | int c; |
126 | char *short_yes = _("yY"); | |
127 | ||
a789d840 TT |
128 | fflush(stdout); |
129 | fflush(stderr); | |
d9c56d3c TT |
130 | printf(_("Proceed anyway? (y,n) ")); |
131 | c = getchar(); | |
132 | if (strchr(short_yes, (char) c)) | |
a789d840 TT |
133 | exit(1); |
134 | } | |
135 | ||
93ab9d78 TT |
136 | #ifndef SCSI_BLK_MAJOR |
137 | #define SCSI_BLK_MAJOR(M) ((M) == SCSI_DISK_MAJOR || (M) == SCSI_CDROM_MAJOR) | |
138 | #endif | |
139 | ||
74becf3c TT |
140 | static void check_plausibility(NOARGS) |
141 | { | |
142 | #ifdef HAVE_LINUX_MAJOR_H | |
b4ee1fb2 TT |
143 | #ifndef MAJOR |
144 | #define MAJOR(dev) ((dev)>>8) | |
145 | #define MINOR(dev) ((dev) & 0xff) | |
146 | #endif | |
147 | ||
74becf3c TT |
148 | int val; |
149 | struct stat s; | |
150 | ||
151 | val = stat(device_name, &s); | |
152 | ||
153 | if(val == -1) { | |
d9c56d3c | 154 | fprintf(stderr, _("Could not stat %s --- %s\n"), |
a789d840 | 155 | device_name, error_message(errno)); |
19c78dc0 | 156 | if (errno == ENOENT) |
d9c56d3c TT |
157 | fprintf(stderr, _("\nThe device apparently does " |
158 | "not exist; did you specify it correctly?\n")); | |
74becf3c TT |
159 | exit(1); |
160 | } | |
161 | if(!S_ISBLK(s.st_mode)) { | |
d9c56d3c | 162 | printf(_("%s is not a block special device.\n"), device_name); |
a789d840 | 163 | proceed_question(); |
74becf3c | 164 | return; |
a789d840 TT |
165 | } else if ((MAJOR(s.st_rdev) == HD_MAJOR && |
166 | MINOR(s.st_rdev)%64 == 0) || | |
1dde43f0 TT |
167 | (SCSI_BLK_MAJOR(MAJOR(s.st_rdev)) && |
168 | MINOR(s.st_rdev)%16 == 0)) { | |
d9c56d3c | 169 | printf(_("%s is entire device, not just one partition!\n"), |
74becf3c | 170 | device_name); |
a789d840 | 171 | proceed_question(); |
74becf3c TT |
172 | } |
173 | #endif | |
174 | } | |
175 | ||
3839e657 TT |
176 | static void check_mount(NOARGS) |
177 | { | |
a418d3ad TT |
178 | errcode_t retval; |
179 | int mount_flags; | |
3839e657 | 180 | |
a418d3ad TT |
181 | retval = ext2fs_check_if_mounted(device_name, &mount_flags); |
182 | if (retval) { | |
183 | com_err("ext2fs_check_if_mount", retval, | |
d9c56d3c | 184 | _("while determining whether %s is mounted."), |
a418d3ad | 185 | device_name); |
3839e657 | 186 | return; |
a418d3ad TT |
187 | } |
188 | if (!(mount_flags & EXT2_MF_MOUNTED)) | |
3839e657 | 189 | return; |
ca3c3285 | 190 | |
d9c56d3c | 191 | fprintf(stderr, _("%s is mounted; "), device_name); |
ca3c3285 | 192 | if (force) { |
d9c56d3c TT |
193 | fprintf(stderr, _("mke2fs forced anyway. " |
194 | "Hope /etc/mtab is incorrect.\n")); | |
ca3c3285 | 195 | } else { |
d9c56d3c | 196 | fprintf(stderr, _("will not make a filesystem here!\n")); |
ca3c3285 TT |
197 | exit(1); |
198 | } | |
3839e657 TT |
199 | } |
200 | ||
50787ea2 TT |
201 | /* |
202 | * This function sets the default parameters for a filesystem | |
203 | * | |
2740156b TT |
204 | * The type is specified by the user. The size is the maximum size |
205 | * (in megabytes) for which a set of parameters applies, with a size | |
206 | * of zero meaning that it is the default parameter for the type. | |
207 | * Note that order is important in the table below. | |
50787ea2 TT |
208 | */ |
209 | static char default_str[] = "default"; | |
210 | struct mke2fs_defaults { | |
4a600566 TT |
211 | const char *type; |
212 | int size; | |
213 | int blocksize; | |
214 | int inode_ratio; | |
50787ea2 TT |
215 | } settings[] = { |
216 | { default_str, 0, 4096, 8192 }, | |
217 | { default_str, 512, 1024, 4096 }, | |
218 | { default_str, 3, 1024, 8192 }, | |
219 | { "news", 0, 4096, 4096 }, | |
220 | { 0, 0, 0, 0}, | |
221 | }; | |
222 | ||
9094f284 | 223 | static void set_fs_defaults(char *fs_type, struct ext2fs_sb *super, |
50787ea2 TT |
224 | int blocksize, int *inode_ratio) |
225 | { | |
226 | int megs; | |
227 | int ratio = 0; | |
228 | struct mke2fs_defaults *p; | |
229 | ||
9094f284 | 230 | megs = (super->s_blocks_count * (EXT2_BLOCK_SIZE(super) / 1024) / |
50787ea2 TT |
231 | 1024); |
232 | if (inode_ratio) | |
233 | ratio = *inode_ratio; | |
234 | if (!fs_type) | |
235 | fs_type = default_str; | |
236 | for (p = settings; p->type; p++) { | |
237 | if ((strcmp(p->type, fs_type) != 0) && | |
238 | (strcmp(p->type, default_str) != 0)) | |
239 | continue; | |
240 | if ((p->size != 0) && | |
241 | (megs > p->size)) | |
242 | continue; | |
243 | if (ratio == 0) | |
244 | *inode_ratio = p->inode_ratio; | |
245 | if (blocksize == 0) { | |
9094f284 | 246 | super->s_log_frag_size = super->s_log_block_size = |
6733c2fd | 247 | int_log2(p->blocksize >> EXT2_MIN_BLOCK_LOG_SIZE); |
50787ea2 TT |
248 | } |
249 | } | |
250 | if (blocksize == 0) | |
9094f284 | 251 | super->s_blocks_count /= EXT2_BLOCK_SIZE(super) / 1024; |
50787ea2 TT |
252 | } |
253 | ||
3839e657 TT |
254 | /* |
255 | * Helper function for read_bb_file and test_disk | |
256 | */ | |
257 | static void invalid_block(ext2_filsys fs, blk_t blk) | |
258 | { | |
d9c56d3c | 259 | printf(_("Bad block %u out of range; ignored.\n"), blk); |
3839e657 TT |
260 | return; |
261 | } | |
262 | ||
263 | /* | |
264 | * Reads the bad blocks list from a file | |
265 | */ | |
266 | static void read_bb_file(ext2_filsys fs, badblocks_list *bb_list, | |
267 | const char *bad_blocks_file) | |
268 | { | |
269 | FILE *f; | |
270 | errcode_t retval; | |
271 | ||
272 | f = fopen(bad_blocks_file, "r"); | |
273 | if (!f) { | |
274 | com_err("read_bad_blocks_file", errno, | |
d9c56d3c | 275 | _("while trying to open %s"), bad_blocks_file); |
3839e657 TT |
276 | exit(1); |
277 | } | |
278 | retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block); | |
279 | fclose (f); | |
280 | if (retval) { | |
281 | com_err("ext2fs_read_bb_FILE", retval, | |
d9c56d3c | 282 | _("while reading in list of bad blocks from file")); |
3839e657 TT |
283 | exit(1); |
284 | } | |
285 | } | |
286 | ||
287 | /* | |
288 | * Runs the badblocks program to test the disk | |
289 | */ | |
290 | static void test_disk(ext2_filsys fs, badblocks_list *bb_list) | |
291 | { | |
292 | FILE *f; | |
293 | errcode_t retval; | |
294 | char buf[1024]; | |
295 | ||
f635d7f6 TT |
296 | sprintf(buf, "badblocks -b %d %s%s %d", fs->blocksize, |
297 | quiet ? "" : "-s ", fs->device_name, | |
3839e657 TT |
298 | fs->super->s_blocks_count); |
299 | if (verbose) | |
d9c56d3c | 300 | printf(_("Running command: %s\n"), buf); |
3839e657 TT |
301 | f = popen(buf, "r"); |
302 | if (!f) { | |
303 | com_err("popen", errno, | |
d9c56d3c | 304 | _("while trying run '%s'"), buf); |
3839e657 TT |
305 | exit(1); |
306 | } | |
307 | retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block); | |
f3db3566 | 308 | pclose(f); |
3839e657 TT |
309 | if (retval) { |
310 | com_err("ext2fs_read_bb_FILE", retval, | |
d9c56d3c | 311 | _("while processing list of bad blocks from program")); |
3839e657 TT |
312 | exit(1); |
313 | } | |
314 | } | |
315 | ||
316 | static void handle_bad_blocks(ext2_filsys fs, badblocks_list bb_list) | |
317 | { | |
f3db3566 | 318 | int i, j; |
3839e657 TT |
319 | int must_be_good; |
320 | blk_t blk; | |
321 | badblocks_iterate bb_iter; | |
322 | errcode_t retval; | |
f3db3566 TT |
323 | blk_t group_block; |
324 | int group; | |
325 | int group_bad; | |
3839e657 TT |
326 | |
327 | if (!bb_list) | |
328 | return; | |
329 | ||
330 | /* | |
331 | * The primary superblock and group descriptors *must* be | |
332 | * good; if not, abort. | |
333 | */ | |
334 | must_be_good = fs->super->s_first_data_block + 1 + fs->desc_blocks; | |
335 | for (i = fs->super->s_first_data_block; i <= must_be_good; i++) { | |
336 | if (badblocks_list_test(bb_list, i)) { | |
d9c56d3c TT |
337 | fprintf(stderr, _("Block %d in primary " |
338 | "superblock/group descriptor area bad.\n"), i); | |
339 | fprintf(stderr, _("Blocks %d through %d must be good " | |
340 | "in order to build a filesystem.\n"), | |
3839e657 | 341 | fs->super->s_first_data_block, must_be_good); |
d9c56d3c | 342 | fprintf(stderr, _("Aborting....\n")); |
3839e657 TT |
343 | exit(1); |
344 | } | |
345 | } | |
f3db3566 TT |
346 | |
347 | /* | |
348 | * See if any of the bad blocks are showing up in the backup | |
349 | * superblocks and/or group descriptors. If so, issue a | |
350 | * warning and adjust the block counts appropriately. | |
351 | */ | |
352 | group_block = fs->super->s_first_data_block + | |
353 | fs->super->s_blocks_per_group; | |
f3db3566 TT |
354 | |
355 | for (i = 1; i < fs->group_desc_count; i++) { | |
92bcc595 | 356 | group_bad = 0; |
f3db3566 TT |
357 | for (j=0; j < fs->desc_blocks+1; j++) { |
358 | if (badblocks_list_test(bb_list, group_block + | |
359 | j)) { | |
360 | if (!group_bad) | |
361 | fprintf(stderr, | |
d9c56d3c TT |
362 | _("Warning: the backup superblock/group descriptors at block %d contain\n" |
363 | " bad blocks.\n\n"), | |
f3db3566 TT |
364 | group_block); |
365 | group_bad++; | |
366 | group = ext2fs_group_of_blk(fs, group_block+j); | |
367 | fs->group_desc[group].bg_free_blocks_count++; | |
368 | fs->super->s_free_blocks_count++; | |
369 | } | |
370 | } | |
371 | group_block += fs->super->s_blocks_per_group; | |
372 | } | |
3839e657 TT |
373 | |
374 | /* | |
375 | * Mark all the bad blocks as used... | |
376 | */ | |
377 | retval = badblocks_list_iterate_begin(bb_list, &bb_iter); | |
378 | if (retval) { | |
379 | com_err("badblocks_list_iterate_begin", retval, | |
d9c56d3c | 380 | _("while marking bad blocks as used")); |
3839e657 TT |
381 | exit(1); |
382 | } | |
383 | while (badblocks_list_iterate(bb_iter, &blk)) | |
f3db3566 | 384 | ext2fs_mark_block_bitmap(fs->block_map, blk); |
3839e657 TT |
385 | badblocks_list_iterate_end(bb_iter); |
386 | } | |
387 | ||
19c78dc0 | 388 | static void write_inode_tables(ext2_filsys fs) |
3839e657 TT |
389 | { |
390 | errcode_t retval; | |
391 | blk_t blk; | |
19c78dc0 TT |
392 | int i, j, num, count; |
393 | char *buf; | |
1dde43f0 | 394 | char format[20], backup[80]; |
7d5633cf TT |
395 | int sync_kludge = 0; |
396 | char *mke2fs_sync; | |
397 | ||
398 | mke2fs_sync = getenv("MKE2FS_SYNC"); | |
399 | if (mke2fs_sync) | |
400 | sync_kludge = atoi(mke2fs_sync); | |
3839e657 TT |
401 | |
402 | buf = malloc(fs->blocksize * STRIDE_LENGTH); | |
403 | if (!buf) { | |
d9c56d3c TT |
404 | com_err("malloc", ENOMEM, |
405 | _("while allocating zeroizing buffer")); | |
3839e657 TT |
406 | exit(1); |
407 | } | |
408 | memset(buf, 0, fs->blocksize * STRIDE_LENGTH); | |
1dde43f0 TT |
409 | |
410 | /* | |
411 | * Figure out how many digits we need | |
412 | */ | |
6733c2fd | 413 | i = int_log10(fs->group_desc_count); |
1dde43f0 TT |
414 | sprintf(format, "%%%dd/%%%dld", i, i); |
415 | memset(backup, '\b', sizeof(backup)-1); | |
416 | backup[sizeof(backup)-1] = 0; | |
417 | if ((2*i)+1 < sizeof(backup)) | |
418 | backup[(2*i)+1] = 0; | |
419 | ||
3839e657 | 420 | if (!quiet) |
d9c56d3c | 421 | printf(_("Writing inode tables: ")); |
3839e657 TT |
422 | for (i = 0; i < fs->group_desc_count; i++) { |
423 | if (!quiet) | |
1dde43f0 | 424 | printf(format, i, fs->group_desc_count); |
3839e657 | 425 | |
19c78dc0 TT |
426 | blk = fs->group_desc[i].bg_inode_table; |
427 | num = fs->inode_blocks_per_group; | |
428 | ||
429 | for (j=0; j < num; j += STRIDE_LENGTH, blk += STRIDE_LENGTH) { | |
430 | if (num-j > STRIDE_LENGTH) | |
431 | count = STRIDE_LENGTH; | |
432 | else | |
433 | count = num - j; | |
434 | retval = io_channel_write_blk(fs->io, blk, count, buf); | |
435 | if (retval) | |
d9c56d3c TT |
436 | printf(_("Warning: could not write %d blocks " |
437 | "in inode table starting at %d: %s\n"), | |
19c78dc0 TT |
438 | count, blk, error_message(retval)); |
439 | } | |
3839e657 | 440 | if (!quiet) |
1dde43f0 | 441 | fputs(backup, stdout); |
7d5633cf TT |
442 | if (sync_kludge) { |
443 | if (sync_kludge == 1) | |
444 | sync(); | |
445 | else if ((i % sync_kludge) == 0) | |
446 | sync(); | |
447 | } | |
3839e657 | 448 | } |
19c78dc0 | 449 | free(buf); |
3839e657 | 450 | if (!quiet) |
d9c56d3c | 451 | fputs(_("done \n"), stdout); |
3839e657 TT |
452 | } |
453 | ||
454 | static void create_root_dir(ext2_filsys fs) | |
455 | { | |
456 | errcode_t retval; | |
f3db3566 | 457 | struct ext2_inode inode; |
3839e657 TT |
458 | |
459 | retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, 0); | |
460 | if (retval) { | |
d9c56d3c | 461 | com_err("ext2fs_mkdir", retval, _("while creating root dir")); |
3839e657 TT |
462 | exit(1); |
463 | } | |
f3db3566 TT |
464 | if (geteuid()) { |
465 | retval = ext2fs_read_inode(fs, EXT2_ROOT_INO, &inode); | |
466 | if (retval) { | |
467 | com_err("ext2fs_read_inode", retval, | |
d9c56d3c | 468 | _("while reading root inode")); |
f3db3566 TT |
469 | exit(1); |
470 | } | |
19c78dc0 TT |
471 | inode.i_uid = getuid(); |
472 | if (inode.i_uid) | |
473 | inode.i_gid = getgid(); | |
f3db3566 TT |
474 | retval = ext2fs_write_inode(fs, EXT2_ROOT_INO, &inode); |
475 | if (retval) { | |
476 | com_err("ext2fs_write_inode", retval, | |
d9c56d3c | 477 | _("while setting root inode ownership")); |
f3db3566 TT |
478 | exit(1); |
479 | } | |
480 | } | |
3839e657 TT |
481 | } |
482 | ||
483 | static void create_lost_and_found(ext2_filsys fs) | |
484 | { | |
485 | errcode_t retval; | |
486 | ino_t ino; | |
487 | const char *name = "lost+found"; | |
488 | int i; | |
50787ea2 | 489 | int lpf_size = 0; |
3839e657 TT |
490 | |
491 | retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, 0, name); | |
492 | if (retval) { | |
d9c56d3c TT |
493 | com_err("ext2fs_mkdir", retval, |
494 | _("while creating /lost+found")); | |
3839e657 TT |
495 | exit(1); |
496 | } | |
497 | ||
498 | retval = ext2fs_lookup(fs, EXT2_ROOT_INO, name, strlen(name), 0, &ino); | |
499 | if (retval) { | |
d9c56d3c TT |
500 | com_err("ext2_lookup", retval, |
501 | _("while looking up /lost+found")); | |
3839e657 TT |
502 | exit(1); |
503 | } | |
504 | ||
505 | for (i=1; i < EXT2_NDIR_BLOCKS; i++) { | |
50787ea2 TT |
506 | if ((lpf_size += fs->blocksize) >= 16*1024) |
507 | break; | |
3839e657 TT |
508 | retval = ext2fs_expand_dir(fs, ino); |
509 | if (retval) { | |
510 | com_err("ext2fs_expand_dir", retval, | |
d9c56d3c | 511 | _("while expanding /lost+found")); |
3839e657 TT |
512 | exit(1); |
513 | } | |
514 | } | |
515 | } | |
516 | ||
517 | static void create_bad_block_inode(ext2_filsys fs, badblocks_list bb_list) | |
518 | { | |
519 | errcode_t retval; | |
520 | ||
f3db3566 | 521 | ext2fs_mark_inode_bitmap(fs->inode_map, EXT2_BAD_INO); |
3839e657 TT |
522 | fs->group_desc[0].bg_free_inodes_count--; |
523 | fs->super->s_free_inodes_count--; | |
524 | retval = ext2fs_update_bb_inode(fs, bb_list); | |
525 | if (retval) { | |
526 | com_err("ext2fs_update_bb_inode", retval, | |
d9c56d3c | 527 | _("while setting bad block inode")); |
3839e657 TT |
528 | exit(1); |
529 | } | |
530 | ||
531 | } | |
532 | ||
533 | static void reserve_inodes(ext2_filsys fs) | |
534 | { | |
535 | ino_t i; | |
536 | int group; | |
537 | ||
7f88b043 | 538 | for (i = EXT2_ROOT_INO + 1; i < EXT2_FIRST_INODE(fs->super); i++) { |
f3db3566 | 539 | ext2fs_mark_inode_bitmap(fs->inode_map, i); |
3839e657 TT |
540 | group = ext2fs_group_of_ino(fs, i); |
541 | fs->group_desc[group].bg_free_inodes_count--; | |
542 | fs->super->s_free_inodes_count--; | |
543 | } | |
544 | ext2fs_mark_ib_dirty(fs); | |
545 | } | |
546 | ||
1e3472c5 | 547 | #ifdef ZAP_BOOTBLOCK |
f3db3566 TT |
548 | static void zap_bootblock(ext2_filsys fs) |
549 | { | |
550 | char buf[512]; | |
551 | int retval; | |
552 | ||
553 | memset(buf, 0, 512); | |
554 | ||
555 | retval = io_channel_write_blk(fs->io, 0, -512, buf); | |
556 | if (retval) | |
d9c56d3c | 557 | printf(_("Warning: could not erase block 0: %s\n"), |
f3db3566 TT |
558 | error_message(retval)); |
559 | } | |
1e3472c5 | 560 | #endif |
f3db3566 TT |
561 | |
562 | ||
3839e657 TT |
563 | static void show_stats(ext2_filsys fs) |
564 | { | |
1e3472c5 TT |
565 | struct ext2fs_sb *s = (struct ext2fs_sb *) fs->super; |
566 | char buf[80]; | |
3839e657 | 567 | blk_t group_block; |
1dde43f0 | 568 | int i, need, col_left; |
3839e657 TT |
569 | |
570 | if (param.s_blocks_count != s->s_blocks_count) | |
d9c56d3c | 571 | printf(_("warning: %d blocks unused.\n\n"), |
3839e657 | 572 | param.s_blocks_count - s->s_blocks_count); |
50787ea2 TT |
573 | |
574 | memset(buf, 0, sizeof(buf)); | |
575 | strncpy(buf, s->s_volume_name, sizeof(s->s_volume_name)); | |
d9c56d3c TT |
576 | printf(_("Filesystem label=%s\n"), buf); |
577 | printf(_("OS type: ")); | |
1e3472c5 TT |
578 | switch (fs->super->s_creator_os) { |
579 | case EXT2_OS_LINUX: printf ("Linux"); break; | |
ad6783df | 580 | case EXT2_OS_HURD: printf ("GNU/Hurd"); break; |
1e3472c5 | 581 | case EXT2_OS_MASIX: printf ("Masix"); break; |
d9c56d3c | 582 | default: printf (_("(unknown os)")); |
1e3472c5 | 583 | } |
50787ea2 | 584 | printf("\n"); |
d9c56d3c | 585 | printf(_("Block size=%u (log=%u)\n"), fs->blocksize, |
50787ea2 | 586 | s->s_log_block_size); |
d9c56d3c | 587 | printf(_("Fragment size=%u (log=%u)\n"), fs->fragsize, |
50787ea2 | 588 | s->s_log_frag_size); |
d9c56d3c | 589 | printf(_("%u inodes, %u blocks\n"), s->s_inodes_count, |
3839e657 | 590 | s->s_blocks_count); |
d9c56d3c | 591 | printf(_("%u blocks (%2.2f%%) reserved for the super user\n"), |
3839e657 TT |
592 | s->s_r_blocks_count, |
593 | 100.0 * s->s_r_blocks_count / s->s_blocks_count); | |
d9c56d3c TT |
594 | printf(_("First data block=%u\n"), s->s_first_data_block); |
595 | if (fs->group_desc_count > 1) | |
596 | printf(_("%lu block groups\n"), fs->group_desc_count); | |
597 | else | |
598 | printf(_("%lu block group\n"), fs->group_desc_count); | |
599 | printf(_("%u blocks per group, %u fragments per group\n"), | |
3839e657 | 600 | s->s_blocks_per_group, s->s_frags_per_group); |
d9c56d3c | 601 | printf(_("%u inodes per group\n"), s->s_inodes_per_group); |
3839e657 TT |
602 | |
603 | if (fs->group_desc_count == 1) { | |
604 | printf("\n"); | |
605 | return; | |
606 | } | |
607 | ||
d9c56d3c | 608 | printf(_("Superblock backups stored on blocks: ")); |
3839e657 TT |
609 | group_block = s->s_first_data_block; |
610 | col_left = 0; | |
611 | for (i = 1; i < fs->group_desc_count; i++) { | |
612 | group_block += s->s_blocks_per_group; | |
521e3685 TT |
613 | if (!ext2fs_bg_has_super(fs, i)) |
614 | continue; | |
7671433a TT |
615 | if (i != 1) |
616 | printf(", "); | |
6733c2fd | 617 | need = int_log10(group_block) + 2; |
1dde43f0 | 618 | if (need > col_left) { |
3839e657 | 619 | printf("\n\t"); |
1dde43f0 | 620 | col_left = 72; |
3839e657 | 621 | } |
1dde43f0 | 622 | col_left -= need; |
a418d3ad | 623 | printf("%u", group_block); |
3839e657 TT |
624 | } |
625 | printf("\n\n"); | |
626 | } | |
627 | ||
1e3472c5 TT |
628 | #ifndef HAVE_STRCASECMP |
629 | static int strcasecmp (char *s1, char *s2) | |
630 | { | |
631 | while (*s1 && *s2) { | |
632 | int ch1 = *s1++, ch2 = *s2++; | |
633 | if (isupper (ch1)) | |
634 | ch1 = tolower (ch1); | |
635 | if (isupper (ch2)) | |
636 | ch2 = tolower (ch2); | |
637 | if (ch1 != ch2) | |
638 | return ch1 - ch2; | |
639 | } | |
640 | return *s1 ? 1 : *s2 ? -1 : 0; | |
641 | } | |
642 | #endif | |
643 | ||
644 | /* | |
645 | * Set the S_CREATOR_OS field. Return true if OS is known, | |
646 | * otherwise, 0. | |
647 | */ | |
648 | static int set_os(struct ext2_super_block *sb, char *os) | |
649 | { | |
650 | if (isdigit (*os)) | |
651 | sb->s_creator_os = atoi (os); | |
652 | else if (strcasecmp(os, "linux") == 0) | |
653 | sb->s_creator_os = EXT2_OS_LINUX; | |
654 | else if (strcasecmp(os, "GNU") == 0 || strcasecmp(os, "hurd") == 0) | |
655 | sb->s_creator_os = EXT2_OS_HURD; | |
656 | else if (strcasecmp(os, "masix") == 0) | |
657 | sb->s_creator_os = EXT2_OS_MASIX; | |
658 | else | |
659 | return 0; | |
660 | return 1; | |
661 | } | |
662 | ||
a418d3ad TT |
663 | #define PATH_SET "PATH=/sbin" |
664 | ||
d163b094 | 665 | static void parse_raid_opts(const char *opts) |
a29f4d30 TT |
666 | { |
667 | char *buf, *token, *next, *p, *arg; | |
668 | int len; | |
669 | int raid_usage = 0; | |
670 | ||
671 | len = strlen(opts); | |
672 | buf = malloc(len+1); | |
673 | if (!buf) { | |
d9c56d3c TT |
674 | fprintf(stderr, _("Couldn't allocate memory to parse " |
675 | "raid options!\n")); | |
a29f4d30 TT |
676 | exit(1); |
677 | } | |
678 | strcpy(buf, opts); | |
679 | for (token = buf; token && *token; token = next) { | |
680 | p = strchr(token, ','); | |
681 | next = 0; | |
682 | if (p) { | |
683 | *p = 0; | |
684 | next = p+1; | |
685 | } | |
686 | arg = strchr(token, '='); | |
687 | if (arg) { | |
688 | *arg = 0; | |
689 | arg++; | |
690 | } | |
691 | if (strcmp(token, "stride") == 0) { | |
692 | if (!arg) { | |
693 | raid_usage++; | |
694 | continue; | |
695 | } | |
696 | fs_stride = strtoul(arg, &p, 0); | |
697 | if (*p || (fs_stride == 0)) { | |
d9c56d3c TT |
698 | fprintf(stderr, |
699 | _("Invalid stride parameter.\n")); | |
a29f4d30 TT |
700 | raid_usage++; |
701 | continue; | |
702 | } | |
703 | } else | |
704 | raid_usage++; | |
705 | } | |
706 | if (raid_usage) { | |
d9c56d3c | 707 | fprintf(stderr, _("\nBad raid options specified.\n\n" |
a29f4d30 TT |
708 | "Raid options are separated by commas, " |
709 | "and may take an argument which\n" | |
710 | "\tis set off by an equals ('=') sign.\n\n" | |
711 | "Valid raid options are:\n" | |
d9c56d3c | 712 | "\tstride=<stride length in blocks>\n\n")); |
a29f4d30 TT |
713 | exit(1); |
714 | } | |
715 | } | |
716 | ||
896938d5 TT |
717 | static __u32 ok_features[3] = { |
718 | 0, /* Compat */ | |
719 | EXT2_FEATURE_INCOMPAT_FILETYPE, /* Incompat */ | |
720 | EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER /* R/O compat */ | |
721 | }; | |
a29f4d30 TT |
722 | |
723 | ||
3839e657 TT |
724 | static void PRS(int argc, char *argv[]) |
725 | { | |
4a600566 TT |
726 | int c; |
727 | int size; | |
728 | char * tmp; | |
729 | blk_t max = 8192; | |
730 | int blocksize = 0; | |
731 | int inode_ratio = 0; | |
732 | int reserved_ratio = 5; | |
733 | ino_t num_inodes = 0; | |
a418d3ad | 734 | errcode_t retval; |
4a600566 TT |
735 | int sparse_option = 0; |
736 | char * oldpath = getenv("PATH"); | |
521e3685 | 737 | struct ext2fs_sb *param_ext2 = (struct ext2fs_sb *) ¶m; |
4a600566 TT |
738 | char * raid_opts = 0; |
739 | char * fs_type = 0; | |
740 | const char * feature_set = "filetype,sparse_super"; | |
741 | blk_t dev_size; | |
2740156b | 742 | #ifdef linux |
4a600566 | 743 | struct utsname ut; |
2740156b TT |
744 | |
745 | if (uname(&ut)) { | |
746 | perror("uname"); | |
747 | exit(1); | |
748 | } | |
896938d5 TT |
749 | if ((ut.release[0] == '1') || |
750 | (ut.release[0] == '2' && ut.release[1] == '.' && | |
751 | ut.release[2] < '2' && ut.release[3] == '.')) | |
752 | feature_set = 0; | |
2740156b | 753 | #endif |
3839e657 | 754 | /* Update our PATH to include /sbin */ |
a418d3ad TT |
755 | if (oldpath) { |
756 | char *newpath; | |
757 | ||
758 | newpath = malloc(sizeof (PATH_SET) + 1 + strlen (oldpath)); | |
759 | strcpy (newpath, PATH_SET); | |
760 | strcat (newpath, ":"); | |
761 | strcat (newpath, oldpath); | |
762 | putenv (newpath); | |
763 | } else | |
764 | putenv (PATH_SET); | |
3839e657 TT |
765 | |
766 | setbuf(stdout, NULL); | |
767 | setbuf(stderr, NULL); | |
768 | initialize_ext2_error_table(); | |
769 | memset(¶m, 0, sizeof(struct ext2_super_block)); | |
50787ea2 | 770 | param.s_rev_level = 1; /* Create revision 1 filesystems now */ |
3839e657 | 771 | |
d9c56d3c | 772 | fprintf (stderr, _("mke2fs %s, %s for EXT2 FS %s, %s\n"), |
3839e657 TT |
773 | E2FSPROGS_VERSION, E2FSPROGS_DATE, |
774 | EXT2FS_VERSION, EXT2FS_DATE); | |
775 | if (argc && *argv) | |
776 | program_name = *argv; | |
1e3472c5 | 777 | while ((c = getopt (argc, argv, |
896938d5 | 778 | "b:cf:g:i:l:m:no:qr:R:s:tvI:ST:FL:M:N:O:V")) != EOF) |
3839e657 TT |
779 | switch (c) { |
780 | case 'b': | |
50787ea2 TT |
781 | blocksize = strtoul(optarg, &tmp, 0); |
782 | if (blocksize < 1024 || blocksize > 4096 || *tmp) { | |
d9c56d3c TT |
783 | com_err(program_name, 0, |
784 | _("bad block size - %s"), optarg); | |
3839e657 TT |
785 | exit(1); |
786 | } | |
787 | param.s_log_block_size = | |
6733c2fd | 788 | int_log2(blocksize >> EXT2_MIN_BLOCK_LOG_SIZE); |
50787ea2 | 789 | max = blocksize * 8; |
3839e657 TT |
790 | break; |
791 | case 'c': | |
792 | case 't': /* Check for bad blocks */ | |
793 | cflag = 1; | |
794 | break; | |
795 | case 'f': | |
796 | size = strtoul(optarg, &tmp, 0); | |
797 | if (size < 1024 || size > 4096 || *tmp) { | |
d9c56d3c TT |
798 | com_err(program_name, 0, |
799 | _("bad fragment size - %s"), | |
3839e657 TT |
800 | optarg); |
801 | exit(1); | |
802 | } | |
803 | param.s_log_frag_size = | |
6733c2fd | 804 | int_log2(size >> EXT2_MIN_BLOCK_LOG_SIZE); |
d9c56d3c TT |
805 | printf(_("Warning: fragments not supported. " |
806 | "Ignoring -f option\n")); | |
3839e657 TT |
807 | break; |
808 | case 'g': | |
809 | param.s_blocks_per_group = strtoul(optarg, &tmp, 0); | |
f3db3566 TT |
810 | if (*tmp) { |
811 | com_err(program_name, 0, | |
d9c56d3c | 812 | _("Illegal number for blocks per group")); |
f3db3566 TT |
813 | exit(1); |
814 | } | |
f3db3566 TT |
815 | if ((param.s_blocks_per_group % 8) != 0) { |
816 | com_err(program_name, 0, | |
d9c56d3c | 817 | _("blocks per group must be multiple of 8")); |
3839e657 TT |
818 | exit(1); |
819 | } | |
820 | break; | |
821 | case 'i': | |
822 | inode_ratio = strtoul(optarg, &tmp, 0); | |
823 | if (inode_ratio < 1024 || inode_ratio > 256 * 1024 || | |
824 | *tmp) { | |
d9c56d3c TT |
825 | com_err(program_name, 0, |
826 | _("bad inode ratio - %s"), optarg); | |
3839e657 TT |
827 | exit(1); |
828 | } | |
829 | break; | |
830 | case 'l': | |
f3db3566 TT |
831 | bad_blocks_filename = malloc(strlen(optarg)+1); |
832 | if (!bad_blocks_filename) { | |
833 | com_err(program_name, ENOMEM, | |
d9c56d3c | 834 | _("in malloc for bad_blocks_filename")); |
f3db3566 TT |
835 | exit(1); |
836 | } | |
837 | strcpy(bad_blocks_filename, optarg); | |
3839e657 TT |
838 | break; |
839 | case 'm': | |
840 | reserved_ratio = strtoul(optarg, &tmp, 0); | |
841 | if (reserved_ratio > 50 || *tmp) { | |
842 | com_err(program_name, 0, | |
d9c56d3c | 843 | _("bad reserved blocks percent - %s"), |
3839e657 TT |
844 | optarg); |
845 | exit(1); | |
846 | } | |
847 | break; | |
50787ea2 TT |
848 | case 'n': |
849 | noaction++; | |
850 | break; | |
1e3472c5 TT |
851 | case 'o': |
852 | creator_os = optarg; | |
853 | break; | |
7f88b043 TT |
854 | case 'r': |
855 | param.s_rev_level = atoi(optarg); | |
856 | break; | |
521e3685 TT |
857 | case 's': |
858 | sparse_option = atoi(optarg); | |
859 | break; | |
7f88b043 TT |
860 | #ifdef EXT2_DYNAMIC_REV |
861 | case 'I': | |
862 | param.s_inode_size = atoi(optarg); | |
863 | break; | |
864 | #endif | |
5515e6b4 TT |
865 | case 'N': |
866 | num_inodes = atoi(optarg); | |
867 | break; | |
3839e657 TT |
868 | case 'v': |
869 | verbose = 1; | |
870 | break; | |
871 | case 'q': | |
872 | quiet = 1; | |
873 | break; | |
74becf3c TT |
874 | case 'F': |
875 | force = 1; | |
876 | break; | |
1e3472c5 TT |
877 | case 'L': |
878 | volume_label = optarg; | |
879 | break; | |
880 | case 'M': | |
881 | mount_dir = optarg; | |
882 | break; | |
896938d5 TT |
883 | case 'O': |
884 | feature_set = optarg; | |
885 | break; | |
a29f4d30 TT |
886 | case 'R': |
887 | raid_opts = optarg; | |
888 | break; | |
f3db3566 TT |
889 | case 'S': |
890 | super_only = 1; | |
891 | break; | |
50787ea2 TT |
892 | case 'T': |
893 | fs_type = optarg; | |
894 | break; | |
818180cd TT |
895 | case 'V': |
896 | /* Print version number and exit */ | |
d9c56d3c | 897 | fprintf(stderr, _("\tUsing %s\n"), |
818180cd TT |
898 | error_message(EXT2_ET_BASE)); |
899 | exit(0); | |
3839e657 TT |
900 | default: |
901 | usage(); | |
902 | } | |
903 | if (optind == argc) | |
904 | usage(); | |
905 | device_name = argv[optind]; | |
906 | optind++; | |
907 | if (optind < argc) { | |
908 | param.s_blocks_count = strtoul(argv[optind++], &tmp, 0); | |
909 | if (*tmp) { | |
d9c56d3c | 910 | com_err(program_name, 0, _("bad blocks count - %s"), |
3839e657 TT |
911 | argv[optind - 1]); |
912 | exit(1); | |
913 | } | |
914 | } | |
915 | if (optind < argc) | |
916 | usage(); | |
a418d3ad | 917 | |
a29f4d30 TT |
918 | if (raid_opts) |
919 | parse_raid_opts(raid_opts); | |
920 | ||
74becf3c TT |
921 | if (!force) |
922 | check_plausibility(); | |
a418d3ad TT |
923 | check_mount(); |
924 | ||
3839e657 TT |
925 | param.s_log_frag_size = param.s_log_block_size; |
926 | ||
50787ea2 TT |
927 | if (noaction && param.s_blocks_count) { |
928 | dev_size = param.s_blocks_count; | |
929 | retval = 0; | |
930 | } else | |
931 | retval = ext2fs_get_device_size(device_name, | |
932 | EXT2_BLOCK_SIZE(¶m), | |
933 | &dev_size); | |
a789d840 TT |
934 | if (retval && (retval != EXT2_ET_UNIMPLEMENTED)) { |
935 | com_err(program_name, retval, | |
d9c56d3c | 936 | _("while trying to determine filesystem size")); |
a789d840 TT |
937 | exit(1); |
938 | } | |
a418d3ad | 939 | if (!param.s_blocks_count) { |
a789d840 TT |
940 | if (retval == EXT2_ET_UNIMPLEMENTED) { |
941 | com_err(program_name, 0, | |
d9c56d3c | 942 | _("Couldn't determine device size; you " |
a789d840 | 943 | "must specify\nthe size of the " |
d9c56d3c | 944 | "filesystem\n")); |
a418d3ad | 945 | exit(1); |
a789d840 TT |
946 | } else |
947 | param.s_blocks_count = dev_size; | |
948 | } else if (!force && (param.s_blocks_count > dev_size)) { | |
949 | com_err(program_name, 0, | |
d9c56d3c | 950 | _("Filesystem larger than apparent filesystem size.")); |
a789d840 | 951 | proceed_question(); |
a418d3ad | 952 | } |
3839e657 | 953 | |
50787ea2 TT |
954 | set_fs_defaults(fs_type, param_ext2, blocksize, &inode_ratio); |
955 | ||
521e3685 TT |
956 | if (param.s_blocks_per_group) { |
957 | if (param.s_blocks_per_group < 256 || | |
958 | param.s_blocks_per_group > max || *tmp) { | |
959 | com_err(program_name, 0, | |
d9c56d3c | 960 | _("blocks per group count out of range")); |
521e3685 TT |
961 | exit(1); |
962 | } | |
963 | } | |
964 | ||
3839e657 TT |
965 | /* |
966 | * Calculate number of inodes based on the inode ratio | |
967 | */ | |
5515e6b4 | 968 | param.s_inodes_count = num_inodes ? num_inodes : |
e6597048 | 969 | ((__u64) param.s_blocks_count * EXT2_BLOCK_SIZE(¶m)) |
f3db3566 | 970 | / inode_ratio; |
3839e657 TT |
971 | |
972 | /* | |
973 | * Calculate number of blocks to reserve | |
974 | */ | |
975 | param.s_r_blocks_count = (param.s_blocks_count * reserved_ratio) / 100; | |
521e3685 | 976 | |
521e3685 | 977 | #ifdef EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER |
2740156b | 978 | if (sparse_option) |
521e3685 TT |
979 | param_ext2->s_feature_ro_compat |= |
980 | EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER; | |
981 | #endif | |
896938d5 TT |
982 | if (feature_set && !strncasecmp(feature_set, "none", 4)) |
983 | feature_set = 0; | |
984 | if (feature_set && e2p_edit_feature(feature_set, | |
985 | ¶m_ext2->s_feature_compat, | |
986 | ok_features)) { | |
d9c56d3c | 987 | fprintf(stderr, _("Invalid filesystem option set: %s\n"), |
896938d5 TT |
988 | feature_set); |
989 | exit(1); | |
990 | } | |
3839e657 TT |
991 | } |
992 | ||
993 | int main (int argc, char *argv[]) | |
994 | { | |
995 | errcode_t retval = 0; | |
996 | ext2_filsys fs; | |
997 | badblocks_list bb_list = 0; | |
1e3472c5 | 998 | struct ext2fs_sb *s; |
d9c56d3c TT |
999 | |
1000 | #ifdef ENABLE_NLS | |
1001 | setlocale(LC_MESSAGES, ""); | |
1002 | bindtextdomain(NLS_CAT_NAME, LOCALEDIR); | |
1003 | textdomain(NLS_CAT_NAME); | |
1004 | #endif | |
3839e657 TT |
1005 | PRS(argc, argv); |
1006 | ||
3839e657 TT |
1007 | /* |
1008 | * Initialize the superblock.... | |
1009 | */ | |
1010 | retval = ext2fs_initialize(device_name, 0, ¶m, | |
1011 | unix_io_manager, &fs); | |
1012 | if (retval) { | |
d9c56d3c | 1013 | com_err(device_name, retval, _("while setting up superblock")); |
3839e657 TT |
1014 | exit(1); |
1015 | } | |
1016 | ||
1e3472c5 TT |
1017 | /* |
1018 | * Generate a UUID for it... | |
1019 | */ | |
1020 | s = (struct ext2fs_sb *) fs->super; | |
1021 | uuid_generate(s->s_uuid); | |
1022 | ||
1023 | /* | |
1024 | * Override the creator OS, if applicable | |
1025 | */ | |
1026 | if (creator_os && !set_os(fs->super, creator_os)) { | |
d9c56d3c | 1027 | com_err (program_name, 0, _("unknown os - %s"), creator_os); |
1e3472c5 TT |
1028 | exit(1); |
1029 | } | |
1030 | ||
1031 | /* | |
1032 | * Set the volume label... | |
1033 | */ | |
1034 | if (volume_label) { | |
1035 | memset(s->s_volume_name, 0, sizeof(s->s_volume_name)); | |
1036 | strncpy(s->s_volume_name, volume_label, | |
1037 | sizeof(s->s_volume_name)); | |
1038 | } | |
1039 | ||
1040 | /* | |
1041 | * Set the last mount directory | |
1042 | */ | |
1043 | if (mount_dir) { | |
1044 | memset(s->s_last_mounted, 0, sizeof(s->s_last_mounted)); | |
1045 | strncpy(s->s_last_mounted, mount_dir, | |
1046 | sizeof(s->s_last_mounted)); | |
1047 | } | |
1048 | ||
50787ea2 | 1049 | if (!quiet || noaction) |
3839e657 TT |
1050 | show_stats(fs); |
1051 | ||
50787ea2 TT |
1052 | if (noaction) |
1053 | exit(0); | |
1054 | ||
3839e657 TT |
1055 | if (bad_blocks_filename) |
1056 | read_bb_file(fs, &bb_list, bad_blocks_filename); | |
1057 | if (cflag) | |
1058 | test_disk(fs, &bb_list); | |
1059 | ||
1060 | handle_bad_blocks(fs, bb_list); | |
a29f4d30 | 1061 | fs->stride = fs_stride; |
19c78dc0 TT |
1062 | retval = ext2fs_allocate_tables(fs); |
1063 | if (retval) { | |
1064 | com_err(program_name, retval, | |
d9c56d3c | 1065 | _("while trying to allocate filesystem tables")); |
19c78dc0 TT |
1066 | exit(1); |
1067 | } | |
f3db3566 TT |
1068 | if (super_only) { |
1069 | fs->super->s_state |= EXT2_ERROR_FS; | |
1070 | fs->flags &= ~(EXT2_FLAG_IB_DIRTY|EXT2_FLAG_BB_DIRTY); | |
1071 | } else { | |
19c78dc0 | 1072 | write_inode_tables(fs); |
f3db3566 TT |
1073 | create_root_dir(fs); |
1074 | create_lost_and_found(fs); | |
1075 | reserve_inodes(fs); | |
1076 | create_bad_block_inode(fs, bb_list); | |
1e3472c5 | 1077 | #ifdef ZAP_BOOTBLOCK |
f3db3566 | 1078 | zap_bootblock(fs); |
1e3472c5 | 1079 | #endif |
f3db3566 | 1080 | } |
3839e657 TT |
1081 | |
1082 | if (!quiet) | |
d9c56d3c TT |
1083 | printf(_("Writing superblocks and " |
1084 | "filesystem accounting information: ")); | |
5d45d803 TT |
1085 | retval = ext2fs_flush(fs); |
1086 | if (retval) { | |
d9c56d3c | 1087 | printf(_("\nWarning, had trouble writing out superblocks.")); |
5d45d803 | 1088 | } |
3839e657 | 1089 | if (!quiet) |
d9c56d3c | 1090 | printf(_("done\n")); |
5d45d803 | 1091 | ext2fs_close(fs); |
3839e657 TT |
1092 | return 0; |
1093 | } |