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