]>
Commit | Line | Data |
---|---|---|
6dbe3af9 | 1 | /* |
9e95aa12 KZ |
2 | * SPDX-License-Identifier: GPL-2.0-or-later |
3 | * | |
4 | * This program is free software; you can redistribute it and/or modify | |
5 | * it under the terms of the GNU General Public License as published by | |
6 | * the Free Software Foundation; either version 2 of the License, or | |
7 | * (at your option) any later version. | |
727b3c2f | 8 | * mkfs.minix.c - make a linux (minix) file-system. |
6dbe3af9 KZ |
9 | * |
10 | * (C) 1991 Linus Torvalds. This file may be redistributed as per | |
11 | * the Linux copyright. | |
9e95aa12 | 12 | * |
726f69e2 | 13 | * DD.MM.YY |
6dbe3af9 | 14 | * |
726f69e2 KZ |
15 | * 24.11.91 - Time began. Used the fsck sources to get started. |
16 | * | |
17 | * 25.11.91 - Corrected some bugs. Added support for ".badblocks" | |
6dbe3af9 KZ |
18 | * The algorithm for ".badblocks" is a bit weird, but |
19 | * it should work. Oh, well. | |
20 | * | |
726f69e2 KZ |
21 | * 25.01.92 - Added the -l option for getting the list of bad blocks |
22 | * out of a named file. (Dave Rivers, rivers@ponds.uucp) | |
6dbe3af9 | 23 | * |
726f69e2 | 24 | * 28.02.92 - Added %-information when using -c. |
6dbe3af9 | 25 | * |
726f69e2 | 26 | * 28.02.93 - Added support for other namelengths than the original |
6dbe3af9 KZ |
27 | * 14 characters so that I can test the new kernel routines.. |
28 | * | |
726f69e2 KZ |
29 | * 09.10.93 - Make exit status conform to that required by fsutil |
30 | * (Rik Faith, faith@cs.unc.edu) | |
6dbe3af9 | 31 | * |
726f69e2 KZ |
32 | * 31.10.93 - Added inode request feature, for backup floppies: use |
33 | * 32 inodes, for a news partition use more. | |
34 | * (Scott Heavner, sdh@po.cwru.edu) | |
6dbe3af9 | 35 | * |
726f69e2 KZ |
36 | * 03.01.94 - Added support for file system valid flag. |
37 | * (Dr. Wettstein, greg%wind.uucp@plains.nodak.edu) | |
fd6b7a7f | 38 | * |
66ee8158 KZ |
39 | * 30.10.94 - Added support for v2 filesystem |
40 | * (Andreas Schwab, schwab@issan.informatik.uni-dortmund.de) | |
2bb3aa36 | 41 | * |
726f69e2 KZ |
42 | * 09.11.94 - Added test to prevent overwrite of mounted fs adapted |
43 | * from Theodore Ts'o's (tytso@athena.mit.edu) mke2fs | |
44 | * program. (Daniel Quinlan, quinlan@yggdrasil.com) | |
45 | * | |
46 | * 03.20.95 - Clear first 512 bytes of filesystem to make certain that | |
47 | * the filesystem is not misidentified as a MS-DOS FAT filesystem. | |
48 | * (Daniel Quinlan, quinlan@yggdrasil.com) | |
6dbe3af9 | 49 | * |
fd6b7a7f KZ |
50 | * 02.07.96 - Added small patch from Russell King to make the program a |
51 | * good deal more portable (janl@math.uio.no) | |
52 | * | |
a2657ae3 DB |
53 | * 06.29.11 - Overall cleanups for util-linux and v3 support |
54 | * Davidlohr Bueso <dave@gnu.org> | |
55 | * | |
da41ff55 JH |
56 | * 06.20.15 - Do not infinite loop or crash on large devices |
57 | * Joshua Hudson <joshudson@gmail.com> | |
58 | * | |
6dbe3af9 KZ |
59 | */ |
60 | ||
61 | #include <stdio.h> | |
62 | #include <time.h> | |
63 | #include <unistd.h> | |
64 | #include <string.h> | |
65 | #include <signal.h> | |
66 | #include <fcntl.h> | |
67 | #include <ctype.h> | |
68 | #include <stdlib.h> | |
69 | #include <termios.h> | |
70 | #include <sys/stat.h> | |
7eda085c | 71 | #include <getopt.h> |
c8079044 | 72 | #include <err.h> |
6dbe3af9 | 73 | |
a28a3ad2 | 74 | #include "blkdev.h" |
7cc112d9 | 75 | #include "minix_programs.h" |
7eda085c | 76 | #include "nls.h" |
fc68cd49 | 77 | #include "pathnames.h" |
e98754d6 | 78 | #include "bitops.h" |
b8f040cd | 79 | #include "exitcodes.h" |
a2657ae3 | 80 | #include "strutils.h" |
e12c9866 | 81 | #include "all-io.h" |
45ca68ec | 82 | #include "closestream.h" |
c99128bc | 83 | #include "ismounted.h" |
778ca2a0 RM |
84 | |
85 | #define XALLOC_EXIT_CODE MKFS_EX_ERROR | |
cd16685e | 86 | #include "xalloc.h" |
7eda085c | 87 | |
6dbe3af9 KZ |
88 | #define MINIX_ROOT_INO 1 |
89 | #define MINIX_BAD_INO 2 | |
90 | ||
91 | #define TEST_BUFFER_BLOCKS 16 | |
92 | #define MAX_GOOD_BLOCKS 512 | |
93 | ||
c1d26b19 | 94 | #define MINIX_MAX_INODES 65535 |
c8079044 | 95 | |
2159dbf3 SK |
96 | #define DEFAULT_FS_VERSION 1 |
97 | ||
058cda4c | 98 | /* |
9e930041 | 99 | * Global variables used in minix_programs.h inline functions |
058cda4c | 100 | */ |
2159dbf3 | 101 | int fs_version = DEFAULT_FS_VERSION; |
058cda4c KZ |
102 | char *super_block_buffer; |
103 | ||
104 | static char *inode_buffer = NULL; | |
105 | ||
106 | #define Inode (((struct minix_inode *) inode_buffer) - 1) | |
107 | #define Inode2 (((struct minix2_inode *) inode_buffer) - 1) | |
108 | ||
77799d7c SK |
109 | struct fs_control { |
110 | char *device_name; /* device on a Minix file system is created */ | |
111 | int device_fd; /* open file descriptor of the device */ | |
e57cbfe0 | 112 | char *lockmode; /* as specified by --lock */ |
77799d7c SK |
113 | unsigned long long fs_blocks; /* device block count for the file system */ |
114 | int fs_used_blocks; /* used blocks on a device */ | |
115 | int fs_bad_blocks; /* number of bad blocks found from device */ | |
2159dbf3 | 116 | uint16_t fs_namelen; /* maximum length of filenames */ |
77799d7c SK |
117 | size_t fs_dirsize; /* maximum size of directory */ |
118 | unsigned long fs_inodes; /* number of inodes */ | |
119 | int fs_magic; /* file system magic number */ | |
120 | unsigned int | |
121 | check_blocks:1; /* check for bad blocks */ | |
122 | }; | |
6dbe3af9 | 123 | |
0dca7bd5 | 124 | static char root_block[MINIX_BLOCK_SIZE]; |
726f69e2 | 125 | static char boot_block_buffer[512]; |
6dbe3af9 | 126 | static unsigned short good_blocks_table[MAX_GOOD_BLOCKS]; |
0dca7bd5 KZ |
127 | |
128 | static char *inode_map; | |
129 | static char *zone_map; | |
6dbe3af9 | 130 | |
50758d0e | 131 | #define zone_in_use(x) (isset(zone_map,(x)-get_first_zone()+1) != 0) |
6dbe3af9 KZ |
132 | |
133 | #define mark_inode(x) (setbit(inode_map,(x))) | |
134 | #define unmark_inode(x) (clrbit(inode_map,(x))) | |
135 | ||
50758d0e DB |
136 | #define mark_zone(x) (setbit(zone_map,(x)-get_first_zone()+1)) |
137 | #define unmark_zone(x) (clrbit(zone_map,(x)-get_first_zone()+1)) | |
6dbe3af9 | 138 | |
6e1eda6f | 139 | static void __attribute__((__noreturn__)) usage(void) |
b59c5925 | 140 | { |
6e1eda6f | 141 | FILE *out = stdout; |
b59c5925 SK |
142 | fputs(USAGE_HEADER, out); |
143 | fprintf(out, _(" %s [options] /dev/name [blocks]\n"), program_invocation_short_name); | |
144 | fputs(USAGE_OPTIONS, out); | |
145 | fputs(_(" -1 use Minix version 1\n"), out); | |
146 | fputs(_(" -2, -v use Minix version 2\n"), out); | |
147 | fputs(_(" -3 use Minix version 3\n"), out); | |
148 | fputs(_(" -n, --namelength <num> maximum length of filenames\n"), out); | |
149 | fputs(_(" -i, --inodes <num> number of inodes for the filesystem\n"), out); | |
150 | fputs(_(" -c, --check check the device for bad blocks\n"), out); | |
151 | fputs(_(" -l, --badblocks <file> list of bad blocks from file\n"), out); | |
e57cbfe0 KZ |
152 | fprintf(out, _( |
153 | " --lock[=<mode>] use exclusive device lock (%s, %s or %s)\n"), "yes", "no", "nonblock"); | |
b59c5925 | 154 | fputs(USAGE_SEPARATOR, out); |
bad4c729 MY |
155 | fprintf(out, USAGE_HELP_OPTIONS(25)); |
156 | fprintf(out, USAGE_MAN_TAIL("mkfs.minix(8)")); | |
6e1eda6f | 157 | exit(MKFS_EX_OK); |
7eda085c | 158 | } |
6dbe3af9 | 159 | |
98f8b800 SK |
160 | #ifdef TEST_SCRIPT |
161 | static inline time_t mkfs_minix_time(time_t *t) | |
162 | { | |
163 | const char *str = getenv("MKFS_MINIX_TEST_SECOND_SINCE_EPOCH"); | |
c2496023 | 164 | uint64_t sec; |
98f8b800 | 165 | |
c2496023 | 166 | if (str && sscanf(str, "%"SCNd64, &sec) == 1) |
98f8b800 SK |
167 | return sec; |
168 | return time(t); | |
169 | } | |
170 | #else /* !TEST_SCRIPT */ | |
171 | # define mkfs_minix_time(x) time(x) | |
172 | #endif | |
173 | ||
a2657ae3 DB |
174 | static void super_set_state(void) |
175 | { | |
176 | switch (fs_version) { | |
3ca43386 DB |
177 | case 1: |
178 | case 2: | |
a2657ae3 DB |
179 | Super.s_state |= MINIX_VALID_FS; |
180 | Super.s_state &= ~MINIX_ERROR_FS; | |
181 | break; | |
75ab0b36 ML |
182 | default: /* v3 */ |
183 | break; | |
a2657ae3 DB |
184 | } |
185 | } | |
186 | ||
77799d7c | 187 | static void write_tables(const struct fs_control *ctl) { |
50758d0e DB |
188 | unsigned long imaps = get_nimaps(); |
189 | unsigned long zmaps = get_nzmaps(); | |
a180dc6c | 190 | size_t buffsz = get_inode_buffer_size(); |
50758d0e | 191 | |
6dbe3af9 | 192 | /* Mark the super block valid. */ |
a2657ae3 | 193 | super_set_state(); |
6dbe3af9 | 194 | |
77799d7c | 195 | if (lseek(ctl->device_fd, 0, SEEK_SET)) |
70b604b8 | 196 | err(MKFS_EX_ERROR, _("%s: seek to boot block failed " |
77799d7c SK |
197 | " in write_tables"), ctl->device_name); |
198 | if (write_all(ctl->device_fd, boot_block_buffer, 512)) | |
199 | err(MKFS_EX_ERROR, _("%s: unable to clear boot sector"), ctl->device_name); | |
200 | if (MINIX_BLOCK_SIZE != lseek(ctl->device_fd, MINIX_BLOCK_SIZE, SEEK_SET)) | |
201 | err(MKFS_EX_ERROR, _("%s: seek failed in write_tables"), ctl->device_name); | |
d9d88e31 | 202 | |
77799d7c SK |
203 | if (write_all(ctl->device_fd, super_block_buffer, MINIX_BLOCK_SIZE)) |
204 | err(MKFS_EX_ERROR, _("%s: unable to write super-block"), ctl->device_name); | |
d9d88e31 | 205 | |
77799d7c SK |
206 | if (write_all(ctl->device_fd, inode_map, imaps * MINIX_BLOCK_SIZE)) |
207 | err(MKFS_EX_ERROR, _("%s: unable to write inode map"), ctl->device_name); | |
d9d88e31 | 208 | |
77799d7c SK |
209 | if (write_all(ctl->device_fd, zone_map, zmaps * MINIX_BLOCK_SIZE)) |
210 | err(MKFS_EX_ERROR, _("%s: unable to write zone map"), ctl->device_name); | |
d9d88e31 | 211 | |
77799d7c SK |
212 | if (write_all(ctl->device_fd, inode_buffer, buffsz)) |
213 | err(MKFS_EX_ERROR, _("%s: unable to write inodes"), ctl->device_name); | |
6dbe3af9 KZ |
214 | } |
215 | ||
77799d7c SK |
216 | static void write_block(const struct fs_control *ctl, int blk, char * buffer) { |
217 | if (blk * MINIX_BLOCK_SIZE != lseek(ctl->device_fd, blk * MINIX_BLOCK_SIZE, SEEK_SET)) | |
218 | errx(MKFS_EX_ERROR, _("%s: seek failed in write_block"), ctl->device_name); | |
d9d88e31 | 219 | |
77799d7c SK |
220 | if (write_all(ctl->device_fd, buffer, MINIX_BLOCK_SIZE)) |
221 | errx(MKFS_EX_ERROR, _("%s: write failed in write_block"), ctl->device_name); | |
6dbe3af9 KZ |
222 | } |
223 | ||
77799d7c | 224 | static int get_free_block(struct fs_control *ctl) { |
d9d88e31 | 225 | unsigned int blk; |
50758d0e DB |
226 | unsigned int zones = get_nzones(); |
227 | unsigned int first_zone = get_first_zone(); | |
6dbe3af9 | 228 | |
77799d7c SK |
229 | if (ctl->fs_used_blocks + 1 >= MAX_GOOD_BLOCKS) |
230 | errx(MKFS_EX_ERROR, _("%s: too many bad blocks"), ctl->device_name); | |
231 | if (ctl->fs_used_blocks) | |
232 | blk = good_blocks_table[ctl->fs_used_blocks - 1] + 1; | |
6dbe3af9 | 233 | else |
50758d0e DB |
234 | blk = first_zone; |
235 | while (blk < zones && zone_in_use(blk)) | |
6dbe3af9 | 236 | blk++; |
50758d0e | 237 | if (blk >= zones) |
77799d7c SK |
238 | errx(MKFS_EX_ERROR, _("%s: not enough good blocks"), ctl->device_name); |
239 | good_blocks_table[ctl->fs_used_blocks] = blk; | |
240 | ctl->fs_used_blocks++; | |
6dbe3af9 KZ |
241 | return blk; |
242 | } | |
243 | ||
77799d7c | 244 | static void mark_good_blocks(const struct fs_control *ctl) { |
6dbe3af9 KZ |
245 | int blk; |
246 | ||
77799d7c | 247 | for (blk=0 ; blk < ctl->fs_used_blocks ; blk++) |
6dbe3af9 KZ |
248 | mark_zone(good_blocks_table[blk]); |
249 | } | |
250 | ||
d9d88e31 KZ |
251 | static inline int next(unsigned long zone) { |
252 | unsigned long zones = get_nzones(); | |
253 | unsigned long first_zone = get_first_zone(); | |
50758d0e | 254 | |
6dbe3af9 | 255 | if (!zone) |
50758d0e DB |
256 | zone = first_zone-1; |
257 | while (++zone < zones) | |
6dbe3af9 KZ |
258 | if (zone_in_use(zone)) |
259 | return zone; | |
260 | return 0; | |
261 | } | |
262 | ||
77799d7c | 263 | static void make_bad_inode_v1(struct fs_control *ctl) |
a2657ae3 | 264 | { |
6dbe3af9 KZ |
265 | struct minix_inode * inode = &Inode[MINIX_BAD_INO]; |
266 | int i,j,zone; | |
267 | int ind=0,dind=0; | |
63883af7 SK |
268 | unsigned short ind_block[MINIX_BLOCK_SIZE>>1]; |
269 | unsigned short dind_block[MINIX_BLOCK_SIZE>>1]; | |
6dbe3af9 KZ |
270 | |
271 | #define NEXT_BAD (zone = next(zone)) | |
272 | ||
77799d7c | 273 | if (!ctl->fs_bad_blocks) |
6dbe3af9 KZ |
274 | return; |
275 | mark_inode(MINIX_BAD_INO); | |
276 | inode->i_nlinks = 1; | |
98f8b800 | 277 | inode->i_time = mkfs_minix_time(NULL); |
6dbe3af9 | 278 | inode->i_mode = S_IFREG + 0000; |
77799d7c | 279 | inode->i_size = ctl->fs_bad_blocks * MINIX_BLOCK_SIZE; |
6dbe3af9 KZ |
280 | zone = next(0); |
281 | for (i=0 ; i<7 ; i++) { | |
282 | inode->i_zone[i] = zone; | |
283 | if (!NEXT_BAD) | |
284 | goto end_bad; | |
285 | } | |
77799d7c | 286 | inode->i_zone[7] = ind = get_free_block(ctl); |
63883af7 | 287 | memset(ind_block,0,MINIX_BLOCK_SIZE); |
6dbe3af9 KZ |
288 | for (i=0 ; i<512 ; i++) { |
289 | ind_block[i] = zone; | |
290 | if (!NEXT_BAD) | |
291 | goto end_bad; | |
292 | } | |
77799d7c | 293 | inode->i_zone[8] = dind = get_free_block(ctl); |
63883af7 | 294 | memset(dind_block,0,MINIX_BLOCK_SIZE); |
6dbe3af9 | 295 | for (i=0 ; i<512 ; i++) { |
77799d7c SK |
296 | write_block(ctl, ind,(char *) ind_block); |
297 | dind_block[i] = ind = get_free_block(ctl); | |
63883af7 | 298 | memset(ind_block,0,MINIX_BLOCK_SIZE); |
6dbe3af9 KZ |
299 | for (j=0 ; j<512 ; j++) { |
300 | ind_block[j] = zone; | |
301 | if (!NEXT_BAD) | |
302 | goto end_bad; | |
303 | } | |
304 | } | |
77799d7c | 305 | errx(MKFS_EX_ERROR, _("%s: too many bad blocks"), ctl->device_name); |
6dbe3af9 KZ |
306 | end_bad: |
307 | if (ind) | |
77799d7c | 308 | write_block(ctl, ind, (char *) ind_block); |
6dbe3af9 | 309 | if (dind) |
77799d7c | 310 | write_block(ctl, dind, (char *) dind_block); |
6dbe3af9 KZ |
311 | } |
312 | ||
77799d7c | 313 | static void make_bad_inode_v2_v3 (struct fs_control *ctl) |
a2657ae3 | 314 | { |
fd6b7a7f KZ |
315 | struct minix2_inode *inode = &Inode2[MINIX_BAD_INO]; |
316 | int i, j, zone; | |
317 | int ind = 0, dind = 0; | |
63883af7 SK |
318 | unsigned long ind_block[MINIX_BLOCK_SIZE >> 2]; |
319 | unsigned long dind_block[MINIX_BLOCK_SIZE >> 2]; | |
fd6b7a7f | 320 | |
77799d7c | 321 | if (!ctl->fs_bad_blocks) |
fd6b7a7f KZ |
322 | return; |
323 | mark_inode (MINIX_BAD_INO); | |
324 | inode->i_nlinks = 1; | |
98f8b800 | 325 | inode->i_atime = inode->i_mtime = inode->i_ctime = mkfs_minix_time(NULL); |
fd6b7a7f | 326 | inode->i_mode = S_IFREG + 0000; |
77799d7c | 327 | inode->i_size = ctl->fs_bad_blocks * MINIX_BLOCK_SIZE; |
fd6b7a7f KZ |
328 | zone = next (0); |
329 | for (i = 0; i < 7; i++) { | |
330 | inode->i_zone[i] = zone; | |
331 | if (!NEXT_BAD) | |
332 | goto end_bad; | |
333 | } | |
77799d7c | 334 | inode->i_zone[7] = ind = get_free_block (ctl); |
63883af7 | 335 | memset (ind_block, 0, MINIX_BLOCK_SIZE); |
fd6b7a7f KZ |
336 | for (i = 0; i < 256; i++) { |
337 | ind_block[i] = zone; | |
338 | if (!NEXT_BAD) | |
339 | goto end_bad; | |
340 | } | |
77799d7c | 341 | inode->i_zone[8] = dind = get_free_block (ctl); |
63883af7 | 342 | memset (dind_block, 0, MINIX_BLOCK_SIZE); |
fd6b7a7f | 343 | for (i = 0; i < 256; i++) { |
77799d7c SK |
344 | write_block (ctl, ind, (char *) ind_block); |
345 | dind_block[i] = ind = get_free_block (ctl); | |
63883af7 | 346 | memset (ind_block, 0, MINIX_BLOCK_SIZE); |
fd6b7a7f KZ |
347 | for (j = 0; j < 256; j++) { |
348 | ind_block[j] = zone; | |
349 | if (!NEXT_BAD) | |
350 | goto end_bad; | |
351 | } | |
352 | } | |
353 | /* Could make triple indirect block here */ | |
77799d7c | 354 | errx(MKFS_EX_ERROR, _("%s: too many bad blocks"), ctl->device_name); |
fd6b7a7f KZ |
355 | end_bad: |
356 | if (ind) | |
77799d7c | 357 | write_block (ctl, ind, (char *) ind_block); |
fd6b7a7f | 358 | if (dind) |
77799d7c | 359 | write_block (ctl, dind, (char *) dind_block); |
fd6b7a7f | 360 | } |
fd6b7a7f | 361 | |
77799d7c | 362 | static void make_bad_inode(struct fs_control *ctl) |
37ebb48b | 363 | { |
e9b0ac10 | 364 | if (fs_version < 2) { |
77799d7c | 365 | make_bad_inode_v1(ctl); |
e9b0ac10 SK |
366 | return; |
367 | } | |
77799d7c | 368 | make_bad_inode_v2_v3(ctl); |
37ebb48b DB |
369 | } |
370 | ||
77799d7c | 371 | static void make_root_inode_v1(struct fs_control *ctl) { |
6dbe3af9 KZ |
372 | struct minix_inode * inode = &Inode[MINIX_ROOT_INO]; |
373 | ||
374 | mark_inode(MINIX_ROOT_INO); | |
77799d7c | 375 | inode->i_zone[0] = get_free_block(ctl); |
6dbe3af9 | 376 | inode->i_nlinks = 2; |
98f8b800 | 377 | inode->i_time = mkfs_minix_time(NULL); |
77799d7c SK |
378 | if (ctl->fs_bad_blocks) |
379 | inode->i_size = 3 * ctl->fs_dirsize; | |
6dbe3af9 | 380 | else { |
7252874a | 381 | memset(&root_block[2 * ctl->fs_dirsize], 0, ctl->fs_dirsize); |
77799d7c | 382 | inode->i_size = 2 * ctl->fs_dirsize; |
6dbe3af9 KZ |
383 | } |
384 | inode->i_mode = S_IFDIR + 0755; | |
2b6fc908 KZ |
385 | inode->i_uid = getuid(); |
386 | if (inode->i_uid) | |
387 | inode->i_gid = getgid(); | |
77799d7c | 388 | write_block(ctl, inode->i_zone[0],root_block); |
6dbe3af9 KZ |
389 | } |
390 | ||
77799d7c | 391 | static void make_root_inode_v2_v3 (struct fs_control *ctl) { |
fd6b7a7f KZ |
392 | struct minix2_inode *inode = &Inode2[MINIX_ROOT_INO]; |
393 | ||
394 | mark_inode (MINIX_ROOT_INO); | |
77799d7c | 395 | inode->i_zone[0] = get_free_block (ctl); |
fd6b7a7f | 396 | inode->i_nlinks = 2; |
98f8b800 | 397 | inode->i_atime = inode->i_mtime = inode->i_ctime = mkfs_minix_time(NULL); |
a2657ae3 | 398 | |
77799d7c SK |
399 | if (ctl->fs_bad_blocks) |
400 | inode->i_size = 3 * ctl->fs_dirsize; | |
fd6b7a7f | 401 | else { |
7252874a | 402 | memset(&root_block[2 * ctl->fs_dirsize], 0, ctl->fs_dirsize); |
77799d7c | 403 | inode->i_size = 2 * ctl->fs_dirsize; |
fd6b7a7f | 404 | } |
a2657ae3 | 405 | |
fd6b7a7f | 406 | inode->i_mode = S_IFDIR + 0755; |
2b6fc908 KZ |
407 | inode->i_uid = getuid(); |
408 | if (inode->i_uid) | |
409 | inode->i_gid = getgid(); | |
77799d7c | 410 | write_block (ctl, inode->i_zone[0], root_block); |
fd6b7a7f | 411 | } |
fd6b7a7f | 412 | |
77799d7c | 413 | static void make_root_inode(struct fs_control *ctl) |
37ebb48b | 414 | { |
ed7f9c5d SK |
415 | char *tmp = root_block; |
416 | ||
417 | if (fs_version == 3) { | |
418 | *(uint32_t *) tmp = 1; | |
419 | strcpy(tmp + 4, "."); | |
420 | tmp += ctl->fs_dirsize; | |
421 | *(uint32_t *) tmp = 1; | |
422 | strcpy(tmp + 4, ".."); | |
423 | tmp += ctl->fs_dirsize; | |
424 | *(uint32_t *) tmp = 2; | |
425 | strcpy(tmp + 4, ".badblocks"); | |
426 | } else { | |
427 | *(uint16_t *) tmp = 1; | |
428 | strcpy(tmp + 2, "."); | |
429 | tmp += ctl->fs_dirsize; | |
430 | *(uint16_t *) tmp = 1; | |
431 | strcpy(tmp + 2, ".."); | |
432 | tmp += ctl->fs_dirsize; | |
433 | *(uint16_t *) tmp = 2; | |
434 | strcpy(tmp + 2, ".badblocks"); | |
435 | } | |
e9b0ac10 | 436 | if (fs_version < 2) { |
77799d7c | 437 | make_root_inode_v1(ctl); |
e9b0ac10 SK |
438 | return; |
439 | } | |
77799d7c | 440 | make_root_inode_v2_v3(ctl); |
37ebb48b DB |
441 | } |
442 | ||
77799d7c | 443 | static void super_set_nzones(const struct fs_control *ctl) |
7a173d7f DB |
444 | { |
445 | switch (fs_version) { | |
a2657ae3 | 446 | case 3: |
77799d7c | 447 | Super3.s_zones = ctl->fs_blocks; |
75ab0b36 | 448 | break; |
7a173d7f | 449 | case 2: |
77799d7c | 450 | Super.s_zones = ctl->fs_blocks; |
7a173d7f DB |
451 | break; |
452 | default: /* v1 */ | |
77799d7c | 453 | Super.s_nzones = ctl->fs_blocks; |
7a173d7f DB |
454 | break; |
455 | } | |
456 | } | |
457 | ||
458 | static void super_init_maxsize(void) | |
459 | { | |
460 | switch (fs_version) { | |
a2657ae3 DB |
461 | case 3: |
462 | Super3.s_max_size = 2147483647L; | |
463 | break; | |
7a173d7f DB |
464 | case 2: |
465 | Super.s_max_size = 0x7fffffff; | |
a2657ae3 | 466 | break; |
7a173d7f DB |
467 | default: /* v1 */ |
468 | Super.s_max_size = (7+512+512*512)*1024; | |
469 | break; | |
470 | } | |
471 | } | |
472 | ||
77799d7c | 473 | static void super_set_map_blocks(const struct fs_control *ctl, unsigned long inodes) |
a2657ae3 DB |
474 | { |
475 | switch (fs_version) { | |
476 | case 3: | |
477 | Super3.s_imap_blocks = UPPER(inodes + 1, BITS_PER_BLOCK); | |
77799d7c SK |
478 | Super3.s_zmap_blocks = UPPER(ctl->fs_blocks - (1 + get_nimaps() + inode_blocks()), |
479 | BITS_PER_BLOCK + 1); | |
a2657ae3 DB |
480 | Super3.s_firstdatazone = first_zone_data(); |
481 | break; | |
482 | default: | |
483 | Super.s_imap_blocks = UPPER(inodes + 1, BITS_PER_BLOCK); | |
77799d7c SK |
484 | Super.s_zmap_blocks = UPPER(ctl->fs_blocks - (1 + get_nimaps() + inode_blocks()), |
485 | BITS_PER_BLOCK + 1); | |
a2657ae3 DB |
486 | Super.s_firstdatazone = first_zone_data(); |
487 | break; | |
488 | } | |
489 | } | |
490 | ||
77799d7c | 491 | static void super_set_magic(const struct fs_control *ctl) |
a2657ae3 DB |
492 | { |
493 | switch (fs_version) { | |
494 | case 3: | |
77799d7c | 495 | Super3.s_magic = ctl->fs_magic; |
a2657ae3 DB |
496 | break; |
497 | default: | |
77799d7c | 498 | Super.s_magic = ctl->fs_magic; |
a2657ae3 DB |
499 | break; |
500 | } | |
501 | } | |
502 | ||
77799d7c | 503 | static void setup_tables(const struct fs_control *ctl) { |
d9d88e31 | 504 | unsigned long inodes, zmaps, imaps, zones, i; |
6dbe3af9 | 505 | |
cd16685e | 506 | super_block_buffer = xcalloc(1, MINIX_BLOCK_SIZE); |
dfea2569 | 507 | |
726f69e2 | 508 | memset(boot_block_buffer,0,512); |
77799d7c | 509 | super_set_magic(ctl); |
a2657ae3 DB |
510 | |
511 | if (fs_version == 3) { | |
512 | Super3.s_log_zone_size = 0; | |
a54d258f | 513 | Super3.s_blocksize = MINIX_BLOCK_SIZE; |
a2657ae3 DB |
514 | } |
515 | else { | |
516 | Super.s_log_zone_size = 0; | |
517 | } | |
7a173d7f DB |
518 | |
519 | super_init_maxsize(); | |
77799d7c | 520 | super_set_nzones(ctl); |
7a173d7f | 521 | zones = get_nzones(); |
eb63b9b8 | 522 | |
da41ff55 JH |
523 | /* some magic nrs: 1 inode / 3 blocks for smaller filesystems, |
524 | * for one inode / 16 blocks for large ones. mkfs will eventually | |
525 | * crab about too far when getting close to the maximum size. */ | |
77799d7c SK |
526 | if (ctl->fs_inodes == 0) |
527 | if (2048 * 1024 < ctl->fs_blocks) /* 2GB */ | |
528 | inodes = ctl->fs_blocks / 16; | |
529 | else if (512 * 1024 < ctl->fs_blocks) /* 0.5GB */ | |
530 | inodes = ctl->fs_blocks / 8; | |
da41ff55 | 531 | else |
77799d7c | 532 | inodes = ctl->fs_blocks / 3; |
6dbe3af9 | 533 | else |
77799d7c | 534 | inodes = ctl->fs_inodes; |
2b6fc908 | 535 | /* Round up inode count to fill block size */ |
a2657ae3 | 536 | if (fs_version == 2 || fs_version == 3) |
2b6fc908 KZ |
537 | inodes = ((inodes + MINIX2_INODES_PER_BLOCK - 1) & |
538 | ~(MINIX2_INODES_PER_BLOCK - 1)); | |
539 | else | |
2b6fc908 KZ |
540 | inodes = ((inodes + MINIX_INODES_PER_BLOCK - 1) & |
541 | ~(MINIX_INODES_PER_BLOCK - 1)); | |
de532609 | 542 | |
a2657ae3 DB |
543 | if (fs_version == 3) |
544 | Super3.s_ninodes = inodes; | |
de532609 | 545 | else { |
de532609 ML |
546 | if (inodes > MINIX_MAX_INODES) |
547 | inodes = MINIX_MAX_INODES; | |
b7b26945 | 548 | Super.s_ninodes = inodes; |
de532609 | 549 | } |
77799d7c | 550 | super_set_map_blocks(ctl, inodes); |
da41ff55 JH |
551 | if (MINIX_MAX_INODES < first_zone_data()) |
552 | errx(MKFS_EX_ERROR, | |
553 | _("First data block at %jd, which is too far (max %d).\n" | |
b59c5925 | 554 | "Try specifying fewer inodes by passing --inodes <num>"), |
fdbd7bb9 | 555 | (intmax_t)first_zone_data(), |
da41ff55 | 556 | MINIX_MAX_INODES); |
a2657ae3 DB |
557 | imaps = get_nimaps(); |
558 | zmaps = get_nzmaps(); | |
c129767e | 559 | |
cd16685e KZ |
560 | inode_map = xmalloc(imaps * MINIX_BLOCK_SIZE); |
561 | zone_map = xmalloc(zmaps * MINIX_BLOCK_SIZE); | |
63883af7 SK |
562 | memset(inode_map,0xff,imaps * MINIX_BLOCK_SIZE); |
563 | memset(zone_map,0xff,zmaps * MINIX_BLOCK_SIZE); | |
cd16685e | 564 | |
50758d0e | 565 | for (i = get_first_zone() ; i<zones ; i++) |
6dbe3af9 | 566 | unmark_zone(i); |
50758d0e | 567 | for (i = MINIX_ROOT_INO ; i<=inodes; i++) |
6dbe3af9 | 568 | unmark_inode(i); |
cd16685e KZ |
569 | |
570 | inode_buffer = xmalloc(get_inode_buffer_size()); | |
50758d0e | 571 | memset(inode_buffer,0, get_inode_buffer_size()); |
cd16685e | 572 | |
c2fcffd5 BS |
573 | printf(P_("%lu inode\n", "%lu inodes\n", inodes), inodes); |
574 | printf(P_("%lu block\n", "%lu blocks\n", zones), zones); | |
fdbd7bb9 RM |
575 | printf(_("Firstdatazone=%jd (%jd)\n"), |
576 | (intmax_t)get_first_zone(), (intmax_t)first_zone_data()); | |
b4ee2cac | 577 | printf(_("Zonesize=%zu\n"), (size_t) MINIX_BLOCK_SIZE << get_zone_size()); |
248b8101 | 578 | printf(_("Maxsize=%zu\n\n"),get_max_size()); |
6dbe3af9 KZ |
579 | } |
580 | ||
581 | /* | |
582 | * Perform a test of a block; return the number of | |
9e930041 | 583 | * blocks readable/writable. |
6dbe3af9 | 584 | */ |
77799d7c | 585 | static size_t do_check(const struct fs_control *ctl, char * buffer, int try, unsigned int current_block) { |
a180dc6c | 586 | ssize_t got; |
2bb3aa36 | 587 | |
6dbe3af9 | 588 | /* Seek to the correct loc. */ |
77799d7c | 589 | if (lseek(ctl->device_fd, current_block * MINIX_BLOCK_SIZE, SEEK_SET) != |
63883af7 | 590 | current_block * MINIX_BLOCK_SIZE ) |
70b604b8 | 591 | err(MKFS_EX_ERROR, _("%s: seek failed during testing of blocks"), |
77799d7c | 592 | ctl->device_name); |
6dbe3af9 KZ |
593 | |
594 | /* Try the read */ | |
77799d7c | 595 | got = read(ctl->device_fd, buffer, try * MINIX_BLOCK_SIZE); |
2bb3aa36 | 596 | if (got < 0) got = 0; |
63883af7 | 597 | if (got & (MINIX_BLOCK_SIZE - 1 )) { |
7eda085c | 598 | printf(_("Weird values in do_check: probably bugs\n")); |
6dbe3af9 | 599 | } |
63883af7 | 600 | got /= MINIX_BLOCK_SIZE; |
6dbe3af9 KZ |
601 | return got; |
602 | } | |
603 | ||
604 | static unsigned int currently_testing = 0; | |
605 | ||
d9d88e31 | 606 | static void alarm_intr(int alnum __attribute__ ((__unused__))) { |
50758d0e DB |
607 | unsigned long zones = get_nzones(); |
608 | ||
609 | if (currently_testing >= zones) | |
6dbe3af9 KZ |
610 | return; |
611 | signal(SIGALRM,alarm_intr); | |
612 | alarm(5); | |
613 | if (!currently_testing) | |
614 | return; | |
615 | printf("%d ...", currently_testing); | |
616 | fflush(stdout); | |
617 | } | |
618 | ||
77799d7c | 619 | static void check_blocks(struct fs_control *ctl) { |
a180dc6c | 620 | size_t try, got; |
63883af7 | 621 | static char buffer[MINIX_BLOCK_SIZE * TEST_BUFFER_BLOCKS]; |
50758d0e DB |
622 | unsigned long zones = get_nzones(); |
623 | unsigned long first_zone = get_first_zone(); | |
6dbe3af9 KZ |
624 | |
625 | currently_testing=0; | |
626 | signal(SIGALRM,alarm_intr); | |
627 | alarm(5); | |
50758d0e | 628 | while (currently_testing < zones) { |
77799d7c | 629 | if (lseek(ctl->device_fd, currently_testing * MINIX_BLOCK_SIZE,SEEK_SET) != |
63883af7 | 630 | currently_testing*MINIX_BLOCK_SIZE) |
70b604b8 | 631 | errx(MKFS_EX_ERROR, _("%s: seek failed in check_blocks"), |
77799d7c | 632 | ctl->device_name); |
6dbe3af9 | 633 | try = TEST_BUFFER_BLOCKS; |
50758d0e DB |
634 | if (currently_testing + try > zones) |
635 | try = zones-currently_testing; | |
77799d7c | 636 | got = do_check(ctl, buffer, try, currently_testing); |
6dbe3af9 KZ |
637 | currently_testing += got; |
638 | if (got == try) | |
639 | continue; | |
50758d0e | 640 | if (currently_testing < first_zone) |
70b604b8 | 641 | errx(MKFS_EX_ERROR, _("%s: bad blocks before data-area: " |
77799d7c | 642 | "cannot make fs"), ctl->device_name); |
6dbe3af9 | 643 | mark_zone(currently_testing); |
77799d7c | 644 | ctl->fs_bad_blocks++; |
6dbe3af9 KZ |
645 | currently_testing++; |
646 | } | |
77799d7c SK |
647 | if (ctl->fs_bad_blocks > 0) |
648 | printf(P_("%d bad block\n", "%d bad blocks\n", ctl->fs_bad_blocks), ctl->fs_bad_blocks); | |
6dbe3af9 KZ |
649 | } |
650 | ||
77799d7c | 651 | static void get_list_blocks(struct fs_control *ctl, char *filename) { |
e8f26419 KZ |
652 | FILE *listfile; |
653 | unsigned long blockno; | |
fd6b7a7f | 654 | |
e8f26419 KZ |
655 | listfile = fopen(filename,"r"); |
656 | if (listfile == NULL) | |
70b604b8 | 657 | err(MKFS_EX_ERROR, _("%s: can't open file of bad blocks"), |
77799d7c | 658 | ctl->device_name); |
e8f26419 KZ |
659 | |
660 | while (!feof(listfile)) { | |
33cac100 | 661 | if (fscanf(listfile,"%lu\n", &blockno) != 1) { |
77799d7c | 662 | printf(_("badblock number input error on line %d\n"), ctl->fs_bad_blocks + 1); |
70b604b8 | 663 | errx(MKFS_EX_ERROR, _("%s: cannot read badblocks file"), |
77799d7c | 664 | ctl->device_name); |
e30cb0ed | 665 | } |
e8f26419 | 666 | mark_zone(blockno); |
77799d7c | 667 | ctl->fs_bad_blocks++; |
e8f26419 KZ |
668 | } |
669 | fclose(listfile); | |
670 | ||
77799d7c SK |
671 | if (ctl->fs_bad_blocks > 0) |
672 | printf(P_("%d bad block\n", "%d bad blocks\n", ctl->fs_bad_blocks), ctl->fs_bad_blocks); | |
6dbe3af9 KZ |
673 | } |
674 | ||
2159dbf3 SK |
675 | static int find_super_magic(const struct fs_control *ctl) |
676 | { | |
677 | switch (fs_version) { | |
678 | case 1: | |
679 | if (ctl->fs_namelen == 14) | |
680 | return MINIX_SUPER_MAGIC; | |
74ce680a | 681 | return MINIX_SUPER_MAGIC2; |
2159dbf3 SK |
682 | case 2: |
683 | if (ctl->fs_namelen == 14) | |
684 | return MINIX2_SUPER_MAGIC; | |
74ce680a | 685 | return MINIX2_SUPER_MAGIC2; |
2159dbf3 SK |
686 | case 3: |
687 | return MINIX3_SUPER_MAGIC; | |
688 | default: | |
689 | abort(); | |
690 | } | |
691 | } | |
692 | ||
a232cfdc SK |
693 | static void determine_device_blocks(struct fs_control *ctl, const struct stat *statbuf) |
694 | { | |
4813a521 | 695 | unsigned long long dev_blocks = 0; |
a232cfdc SK |
696 | |
697 | if (S_ISBLK(statbuf->st_mode)) { | |
698 | int sectorsize; | |
699 | ||
700 | if (blkdev_get_sector_size(ctl->device_fd, §orsize) == -1) | |
701 | sectorsize = DEFAULT_SECTOR_SIZE; /* kernel < 2.3.3 */ | |
a232cfdc SK |
702 | if (MINIX_BLOCK_SIZE < sectorsize) |
703 | errx(MKFS_EX_ERROR, _("block size smaller than physical " | |
704 | "sector size of %s"), ctl->device_name); | |
705 | if (blkdev_get_size(ctl->device_fd, &dev_blocks) == -1) | |
706 | errx(MKFS_EX_ERROR, _("cannot determine size of %s"), ctl->device_name); | |
707 | dev_blocks /= MINIX_BLOCK_SIZE; | |
708 | } else if (!S_ISBLK(statbuf->st_mode)) | |
709 | dev_blocks = statbuf->st_size / MINIX_BLOCK_SIZE; | |
710 | if (!ctl->fs_blocks) | |
711 | ctl->fs_blocks = dev_blocks; | |
712 | else if (dev_blocks < ctl->fs_blocks) | |
713 | errx(MKFS_EX_ERROR, | |
714 | _("%s: requested blocks (%llu) exceeds available (%llu) blocks\n"), | |
715 | ctl->device_name, ctl->fs_blocks, dev_blocks); | |
716 | if (ctl->fs_blocks < 10) | |
717 | errx(MKFS_EX_ERROR, _("%s: number of blocks too small"), ctl->device_name); | |
718 | if (fs_version == 1 && ctl->fs_blocks > MINIX_MAX_INODES) | |
719 | ctl->fs_blocks = MINIX_MAX_INODES; | |
3e56e362 SK |
720 | if (ctl->fs_blocks > (4 + ((MINIX_MAX_INODES - 4) * BITS_PER_BLOCK))) |
721 | ctl->fs_blocks = 4 + ((MINIX_MAX_INODES - 4) * BITS_PER_BLOCK); /* Utter maximum: Clip. */ | |
a232cfdc SK |
722 | } |
723 | ||
2159dbf3 SK |
724 | static void check_user_instructions(struct fs_control *ctl) |
725 | { | |
726 | switch (fs_version) { | |
727 | case 1: | |
728 | case 2: | |
729 | if (ctl->fs_namelen == 14 || ctl->fs_namelen == 30) | |
730 | ctl->fs_dirsize = ctl->fs_namelen + 2; | |
731 | else | |
732 | errx(MKFS_EX_ERROR, _("unsupported name length: %d"), ctl->fs_namelen); | |
733 | break; | |
734 | case 3: | |
735 | if (ctl->fs_namelen == 60) | |
736 | ctl->fs_dirsize = ctl->fs_namelen + 4; | |
737 | else | |
738 | errx(MKFS_EX_ERROR, _("unsupported name length: %d"), ctl->fs_namelen); | |
739 | break; | |
740 | default: | |
741 | errx(MKFS_EX_ERROR, _("unsupported minix file system version: %d"), fs_version); | |
742 | } | |
743 | ctl->fs_magic = find_super_magic(ctl); | |
744 | } | |
745 | ||
77799d7c SK |
746 | int main(int argc, char ** argv) |
747 | { | |
748 | struct fs_control ctl = { | |
2159dbf3 | 749 | .fs_namelen = 30, /* keep in sync with DEFAULT_FS_VERSION */ |
77799d7c SK |
750 | 0 |
751 | }; | |
c8079044 | 752 | int i; |
c8079044 DB |
753 | struct stat statbuf; |
754 | char * listfile = NULL; | |
e57cbfe0 KZ |
755 | enum { |
756 | OPT_LOCK = CHAR_MAX + 1 | |
757 | }; | |
b59c5925 SK |
758 | static const struct option longopts[] = { |
759 | {"namelength", required_argument, NULL, 'n'}, | |
760 | {"inodes", required_argument, NULL, 'i'}, | |
761 | {"check", no_argument, NULL, 'c'}, | |
762 | {"badblocks", required_argument, NULL, 'l'}, | |
763 | {"version", no_argument, NULL, 'V'}, | |
764 | {"help", no_argument, NULL, 'h'}, | |
e57cbfe0 | 765 | {"lock",optional_argument, NULL, OPT_LOCK}, |
b59c5925 SK |
766 | {NULL, 0, NULL, 0} |
767 | }; | |
c8079044 DB |
768 | |
769 | setlocale(LC_ALL, ""); | |
770 | bindtextdomain(PACKAGE, LOCALEDIR); | |
771 | textdomain(PACKAGE); | |
2c308875 | 772 | close_stdout_atexit(); |
c8079044 | 773 | |
0b2b32e8 RM |
774 | strutils_set_exitcode(MKFS_EX_USAGE); |
775 | ||
b59c5925 | 776 | while ((i = getopt_long(argc, argv, "1v23n:i:cl:Vh", longopts, NULL)) != -1) |
c8079044 | 777 | switch (i) { |
b59c5925 SK |
778 | case '1': |
779 | fs_version = 1; | |
780 | break; | |
9e930041 | 781 | case 'v': /* kept for backwards compatibility */ |
b59c5925 | 782 | warnx(_("-v is ambiguous, use '-2' instead")); |
70e05135 | 783 | /* fallthrough */ |
b59c5925 SK |
784 | case '2': |
785 | fs_version = 2; | |
786 | break; | |
787 | case '3': | |
788 | fs_version = 3; | |
77799d7c | 789 | ctl.fs_namelen = 60; |
c8079044 | 790 | break; |
c8079044 | 791 | case 'n': |
2159dbf3 | 792 | ctl.fs_namelen = strtou16_or_err(optarg, |
f150ac37 | 793 | _("failed to parse maximum length of filenames")); |
c8079044 | 794 | break; |
b59c5925 | 795 | case 'i': |
77799d7c | 796 | ctl.fs_inodes = strtoul_or_err(optarg, |
b59c5925 | 797 | _("failed to parse number of inodes")); |
4bc4653e | 798 | break; |
b59c5925 | 799 | case 'c': |
77799d7c | 800 | ctl.check_blocks = 1; |
c8079044 | 801 | break; |
b59c5925 SK |
802 | case 'l': |
803 | listfile = optarg; | |
a2657ae3 | 804 | break; |
e57cbfe0 KZ |
805 | case OPT_LOCK: |
806 | ctl.lockmode = "1"; | |
807 | if (optarg) { | |
808 | if (*optarg == '=') | |
809 | optarg++; | |
810 | ctl.lockmode = optarg; | |
811 | } | |
812 | break; | |
b59c5925 | 813 | case 'V': |
2c308875 | 814 | print_version(MKFS_EX_OK); |
b59c5925 | 815 | case 'h': |
6e1eda6f | 816 | usage(); |
c8079044 | 817 | default: |
677ec86c | 818 | errtryhelp(MKFS_EX_USAGE); |
c8079044 DB |
819 | } |
820 | argc -= optind; | |
821 | argv += optind; | |
77799d7c SK |
822 | if (argc > 0) { |
823 | ctl.device_name = argv[0]; | |
c8079044 DB |
824 | argc--; |
825 | argv++; | |
826 | } | |
f150ac37 | 827 | if (argc > 0) |
77799d7c | 828 | ctl.fs_blocks = strtoul_or_err(argv[0], _("failed to parse number of blocks")); |
c8079044 | 829 | |
77799d7c | 830 | if (!ctl.device_name) { |
6e1eda6f RM |
831 | warnx(_("no device specified")); |
832 | errtryhelp(MKFS_EX_USAGE); | |
c8079044 | 833 | } |
2159dbf3 | 834 | check_user_instructions(&ctl); |
77799d7c | 835 | if (is_mounted(ctl.device_name)) |
c99128bc | 836 | errx(MKFS_EX_ERROR, _("%s is mounted; will not make a filesystem here!"), |
77799d7c | 837 | ctl.device_name); |
77799d7c SK |
838 | if (stat(ctl.device_name, &statbuf) < 0) |
839 | err(MKFS_EX_ERROR, _("stat of %s failed"), ctl.device_name); | |
8a4eb983 | 840 | ctl.device_fd = open_blkdev_or_file(&statbuf, ctl.device_name, O_RDWR); |
77799d7c SK |
841 | if (ctl.device_fd < 0) |
842 | err(MKFS_EX_ERROR, _("cannot open %s"), ctl.device_name); | |
e57cbfe0 KZ |
843 | if (blkdev_lock(ctl.device_fd, ctl.device_name, ctl.lockmode) != 0) |
844 | exit(MKFS_EX_ERROR); | |
a232cfdc | 845 | determine_device_blocks(&ctl, &statbuf); |
77799d7c SK |
846 | setup_tables(&ctl); |
847 | if (ctl.check_blocks) | |
848 | check_blocks(&ctl); | |
c8079044 | 849 | else if (listfile) |
77799d7c | 850 | get_list_blocks(&ctl, listfile); |
37ebb48b | 851 | |
77799d7c SK |
852 | make_root_inode(&ctl); |
853 | make_bad_inode(&ctl); | |
c8079044 | 854 | |
77799d7c SK |
855 | mark_good_blocks(&ctl); |
856 | write_tables(&ctl); | |
857 | if (close_fd(ctl.device_fd) != 0) | |
616fbb3a | 858 | err(MKFS_EX_ERROR, _("write failed")); |
c8079044 | 859 | |
65ca6f83 | 860 | return MKFS_EX_OK; |
6dbe3af9 | 861 | } |