diff options
author | Erik Andersen <andersen@codepoet.org> | 2000-02-08 19:58:47 +0000 |
---|---|---|
committer | Erik Andersen <andersen@codepoet.org> | 2000-02-08 19:58:47 +0000 |
commit | e49d5ecbbe51718fa925b6890a735e5937cc2aa2 (patch) | |
tree | c90bda10731ad9333ce3b404f993354c9fc104b8 /util-linux/mkfs_minix.c | |
parent | c0bf817bbc5c7867fbe8fb76d5c39f8ee802692f (diff) | |
download | busybox-w32-e49d5ecbbe51718fa925b6890a735e5937cc2aa2.tar.gz busybox-w32-e49d5ecbbe51718fa925b6890a735e5937cc2aa2.tar.bz2 busybox-w32-e49d5ecbbe51718fa925b6890a735e5937cc2aa2.zip |
Some formatting updates (ran the code through indent)
-Erik
Diffstat (limited to 'util-linux/mkfs_minix.c')
-rw-r--r-- | util-linux/mkfs_minix.c | 499 |
1 files changed, 257 insertions, 242 deletions
diff --git a/util-linux/mkfs_minix.c b/util-linux/mkfs_minix.c index b90d3a700..4435cb64a 100644 --- a/util-linux/mkfs_minix.c +++ b/util-linux/mkfs_minix.c | |||
@@ -1,3 +1,4 @@ | |||
1 | /* vi: set sw=4 ts=4: */ | ||
1 | /* | 2 | /* |
2 | * mkfs.c - make a linux (minix) file-system. | 3 | * mkfs.c - make a linux (minix) file-system. |
3 | * | 4 | * |
@@ -107,27 +108,30 @@ | |||
107 | 108 | ||
108 | #define BITS_PER_BLOCK (BLOCK_SIZE<<3) | 109 | #define BITS_PER_BLOCK (BLOCK_SIZE<<3) |
109 | 110 | ||
110 | static char * program_name = "mkfs"; | 111 | static char *program_name = "mkfs"; |
111 | static char * device_name = NULL; | 112 | static char *device_name = NULL; |
112 | static int DEV = -1; | 113 | static int DEV = -1; |
113 | static long BLOCKS = 0; | 114 | static long BLOCKS = 0; |
114 | static int check = 0; | 115 | static int check = 0; |
115 | static int badblocks = 0; | 116 | static int badblocks = 0; |
116 | static int namelen = 30; /* default (changed to 30, per Linus's | 117 | static int namelen = 30; /* default (changed to 30, per Linus's |
117 | suggestion, Sun Nov 21 08:05:07 1993) */ | 118 | |
119 | suggestion, Sun Nov 21 08:05:07 1993) */ | ||
118 | static int dirsize = 32; | 120 | static int dirsize = 32; |
119 | static int magic = MINIX_SUPER_MAGIC2; | 121 | static int magic = MINIX_SUPER_MAGIC2; |
120 | static int version2 = 0; | 122 | static int version2 = 0; |
121 | 123 | ||
122 | static char root_block[BLOCK_SIZE] = "\0"; | 124 | static char root_block[BLOCK_SIZE] = "\0"; |
123 | 125 | ||
124 | static char * inode_buffer = NULL; | 126 | static char *inode_buffer = NULL; |
127 | |||
125 | #define Inode (((struct minix_inode *) inode_buffer)-1) | 128 | #define Inode (((struct minix_inode *) inode_buffer)-1) |
126 | #ifdef HAVE_MINIX2 | 129 | #ifdef HAVE_MINIX2 |
127 | #define Inode2 (((struct minix2_inode *) inode_buffer)-1) | 130 | #define Inode2 (((struct minix2_inode *) inode_buffer)-1) |
128 | #endif | 131 | #endif |
129 | static char super_block_buffer[BLOCK_SIZE]; | 132 | static char super_block_buffer[BLOCK_SIZE]; |
130 | static char boot_block_buffer[512]; | 133 | static char boot_block_buffer[512]; |
134 | |||
131 | #define Super (*(struct minix_super_block *)super_block_buffer) | 135 | #define Super (*(struct minix_super_block *)super_block_buffer) |
132 | #define INODES ((unsigned long)Super.s_ninodes) | 136 | #define INODES ((unsigned long)Super.s_ninodes) |
133 | #ifdef HAVE_MINIX2 | 137 | #ifdef HAVE_MINIX2 |
@@ -164,21 +168,28 @@ static unsigned long req_nr_inodes = 0; | |||
164 | * to compile this under minix, volatile gives a warning, as | 168 | * to compile this under minix, volatile gives a warning, as |
165 | * exit() isn't defined as volatile under minix. | 169 | * exit() isn't defined as volatile under minix. |
166 | */ | 170 | */ |
167 | static volatile void die(char *str) { | 171 | static volatile void die(char *str) |
172 | { | ||
168 | fprintf(stderr, "%s: %s\n", program_name, str); | 173 | fprintf(stderr, "%s: %s\n", program_name, str); |
169 | exit(8); | 174 | exit(8); |
170 | } | 175 | } |
171 | 176 | ||
172 | static volatile void show_usage() | 177 | static volatile void show_usage() |
173 | { | 178 | { |
174 | fprintf(stderr, "BusyBox v%s (%s) multi-call binary -- GPL2\n\n", BB_VER, BB_BT); | 179 | fprintf(stderr, "BusyBox v%s (%s) multi-call binary -- GPL2\n\n", |
175 | fprintf(stderr, "Usage: %s [-c | -l filename] [-nXX] [-iXX] /dev/name [blocks]\n\n", program_name); | 180 | BB_VER, BB_BT); |
181 | fprintf(stderr, | ||
182 | "Usage: %s [-c | -l filename] [-nXX] [-iXX] /dev/name [blocks]\n\n", | ||
183 | program_name); | ||
176 | fprintf(stderr, "Make a MINIX filesystem.\n\n"); | 184 | fprintf(stderr, "Make a MINIX filesystem.\n\n"); |
177 | fprintf(stderr, "OPTIONS:\n"); | 185 | fprintf(stderr, "OPTIONS:\n"); |
178 | fprintf(stderr, "\t-c\t\tCheck the device for bad blocks\n"); | 186 | fprintf(stderr, "\t-c\t\tCheck the device for bad blocks\n"); |
179 | fprintf(stderr, "\t-n [14|30]\tSpecify the maximum length of filenames\n"); | 187 | fprintf(stderr, |
180 | fprintf(stderr, "\t-i\t\tSpecify the number of inodes for the filesystem\n"); | 188 | "\t-n [14|30]\tSpecify the maximum length of filenames\n"); |
181 | fprintf(stderr, "\t-l FILENAME\tRead the bad blocks list from FILENAME\n"); | 189 | fprintf(stderr, |
190 | "\t-i\t\tSpecify the number of inodes for the filesystem\n"); | ||
191 | fprintf(stderr, | ||
192 | "\t-l FILENAME\tRead the bad blocks list from FILENAME\n"); | ||
182 | fprintf(stderr, "\t-v\t\tMake a Minix version 2 filesystem\n\n"); | 193 | fprintf(stderr, "\t-v\t\tMake a Minix version 2 filesystem\n\n"); |
183 | exit(16); | 194 | exit(16); |
184 | } | 195 | } |
@@ -190,56 +201,55 @@ static volatile void show_usage() | |||
190 | */ | 201 | */ |
191 | static void check_mount(void) | 202 | static void check_mount(void) |
192 | { | 203 | { |
193 | FILE * f; | 204 | FILE *f; |
194 | struct mntent * mnt; | 205 | struct mntent *mnt; |
195 | 206 | ||
196 | if ((f = setmntent (MOUNTED, "r")) == NULL) | 207 | if ((f = setmntent(MOUNTED, "r")) == NULL) |
197 | return; | 208 | return; |
198 | while ((mnt = getmntent (f)) != NULL) | 209 | while ((mnt = getmntent(f)) != NULL) |
199 | if (strcmp (device_name, mnt->mnt_fsname) == 0) | 210 | if (strcmp(device_name, mnt->mnt_fsname) == 0) |
200 | break; | 211 | break; |
201 | endmntent (f); | 212 | endmntent(f); |
202 | if (!mnt) | 213 | if (!mnt) |
203 | return; | 214 | return; |
204 | 215 | ||
205 | die("%s is mounted; will not make a filesystem here!"); | 216 | die("%s is mounted; will not make a filesystem here!"); |
206 | } | 217 | } |
207 | 218 | ||
208 | static long valid_offset (int fd, int offset) | 219 | static long valid_offset(int fd, int offset) |
209 | { | 220 | { |
210 | char ch; | 221 | char ch; |
211 | 222 | ||
212 | if (lseek (fd, offset, 0) < 0) | 223 | if (lseek(fd, offset, 0) < 0) |
213 | return 0; | 224 | return 0; |
214 | if (read (fd, &ch, 1) < 1) | 225 | if (read(fd, &ch, 1) < 1) |
215 | return 0; | 226 | return 0; |
216 | return 1; | 227 | return 1; |
217 | } | 228 | } |
218 | 229 | ||
219 | static int count_blocks (int fd) | 230 | static int count_blocks(int fd) |
220 | { | 231 | { |
221 | int high, low; | 232 | int high, low; |
222 | 233 | ||
223 | low = 0; | 234 | low = 0; |
224 | for (high = 1; valid_offset (fd, high); high *= 2) | 235 | for (high = 1; valid_offset(fd, high); high *= 2) |
225 | low = high; | 236 | low = high; |
226 | while (low < high - 1) | 237 | while (low < high - 1) { |
227 | { | ||
228 | const int mid = (low + high) / 2; | 238 | const int mid = (low + high) / 2; |
229 | 239 | ||
230 | if (valid_offset (fd, mid)) | 240 | if (valid_offset(fd, mid)) |
231 | low = mid; | 241 | low = mid; |
232 | else | 242 | else |
233 | high = mid; | 243 | high = mid; |
234 | } | 244 | } |
235 | valid_offset (fd, 0); | 245 | valid_offset(fd, 0); |
236 | return (low + 1); | 246 | return (low + 1); |
237 | } | 247 | } |
238 | 248 | ||
239 | static int get_size(const char *file) | 249 | static int get_size(const char *file) |
240 | { | 250 | { |
241 | int fd; | 251 | int fd; |
242 | long size; | 252 | long size; |
243 | 253 | ||
244 | fd = open(file, O_RDWR); | 254 | fd = open(file, O_RDWR); |
245 | if (fd < 0) { | 255 | if (fd < 0) { |
@@ -250,7 +260,7 @@ static int get_size(const char *file) | |||
250 | close(fd); | 260 | close(fd); |
251 | return (size * 512); | 261 | return (size * 512); |
252 | } | 262 | } |
253 | 263 | ||
254 | size = count_blocks(fd); | 264 | size = count_blocks(fd); |
255 | close(fd); | 265 | close(fd); |
256 | return size; | 266 | return size; |
@@ -270,18 +280,18 @@ static void write_tables(void) | |||
270 | die("seek failed in write_tables"); | 280 | die("seek failed in write_tables"); |
271 | if (BLOCK_SIZE != write(DEV, super_block_buffer, BLOCK_SIZE)) | 281 | if (BLOCK_SIZE != write(DEV, super_block_buffer, BLOCK_SIZE)) |
272 | die("unable to write super-block"); | 282 | die("unable to write super-block"); |
273 | if (IMAPS*BLOCK_SIZE != write(DEV,inode_map,IMAPS*BLOCK_SIZE)) | 283 | if (IMAPS * BLOCK_SIZE != write(DEV, inode_map, IMAPS * BLOCK_SIZE)) |
274 | die("unable to write inode map"); | 284 | die("unable to write inode map"); |
275 | if (ZMAPS*BLOCK_SIZE != write(DEV,zone_map,ZMAPS*BLOCK_SIZE)) | 285 | if (ZMAPS * BLOCK_SIZE != write(DEV, zone_map, ZMAPS * BLOCK_SIZE)) |
276 | die("unable to write zone map"); | 286 | die("unable to write zone map"); |
277 | if (INODE_BUFFER_SIZE != write(DEV,inode_buffer,INODE_BUFFER_SIZE)) | 287 | if (INODE_BUFFER_SIZE != write(DEV, inode_buffer, INODE_BUFFER_SIZE)) |
278 | die("unable to write inodes"); | 288 | die("unable to write inodes"); |
279 | 289 | ||
280 | } | 290 | } |
281 | 291 | ||
282 | static void write_block(int blk, char * buffer) | 292 | static void write_block(int blk, char *buffer) |
283 | { | 293 | { |
284 | if (blk*BLOCK_SIZE != lseek(DEV, blk*BLOCK_SIZE, SEEK_SET)) | 294 | if (blk * BLOCK_SIZE != lseek(DEV, blk * BLOCK_SIZE, SEEK_SET)) |
285 | die("seek failed in write_block"); | 295 | die("seek failed in write_block"); |
286 | if (BLOCK_SIZE != write(DEV, buffer, BLOCK_SIZE)) | 296 | if (BLOCK_SIZE != write(DEV, buffer, BLOCK_SIZE)) |
287 | die("write failed in write_block"); | 297 | die("write failed in write_block"); |
@@ -291,10 +301,10 @@ static int get_free_block(void) | |||
291 | { | 301 | { |
292 | int blk; | 302 | int blk; |
293 | 303 | ||
294 | if (used_good_blocks+1 >= MAX_GOOD_BLOCKS) | 304 | if (used_good_blocks + 1 >= MAX_GOOD_BLOCKS) |
295 | die("too many bad blocks"); | 305 | die("too many bad blocks"); |
296 | if (used_good_blocks) | 306 | if (used_good_blocks) |
297 | blk = good_blocks_table[used_good_blocks-1]+1; | 307 | blk = good_blocks_table[used_good_blocks - 1] + 1; |
298 | else | 308 | else |
299 | blk = FIRSTZONE; | 309 | blk = FIRSTZONE; |
300 | while (blk < ZONES && zone_in_use(blk)) | 310 | while (blk < ZONES && zone_in_use(blk)) |
@@ -310,14 +320,14 @@ static void mark_good_blocks(void) | |||
310 | { | 320 | { |
311 | int blk; | 321 | int blk; |
312 | 322 | ||
313 | for (blk=0 ; blk < used_good_blocks ; blk++) | 323 | for (blk = 0; blk < used_good_blocks; blk++) |
314 | mark_zone(good_blocks_table[blk]); | 324 | mark_zone(good_blocks_table[blk]); |
315 | } | 325 | } |
316 | 326 | ||
317 | inline int next(int zone) | 327 | inline int next(int zone) |
318 | { | 328 | { |
319 | if (!zone) | 329 | if (!zone) |
320 | zone = FIRSTZONE-1; | 330 | zone = FIRSTZONE - 1; |
321 | while (++zone < ZONES) | 331 | while (++zone < ZONES) |
322 | if (zone_in_use(zone)) | 332 | if (zone_in_use(zone)) |
323 | return zone; | 333 | return zone; |
@@ -326,11 +336,11 @@ inline int next(int zone) | |||
326 | 336 | ||
327 | static void make_bad_inode(void) | 337 | static void make_bad_inode(void) |
328 | { | 338 | { |
329 | struct minix_inode * inode = &Inode[MINIX_BAD_INO]; | 339 | struct minix_inode *inode = &Inode[MINIX_BAD_INO]; |
330 | int i,j,zone; | 340 | int i, j, zone; |
331 | int ind=0,dind=0; | 341 | int ind = 0, dind = 0; |
332 | unsigned short ind_block[BLOCK_SIZE>>1]; | 342 | unsigned short ind_block[BLOCK_SIZE >> 1]; |
333 | unsigned short dind_block[BLOCK_SIZE>>1]; | 343 | unsigned short dind_block[BLOCK_SIZE >> 1]; |
334 | 344 | ||
335 | #define NEXT_BAD (zone = next(zone)) | 345 | #define NEXT_BAD (zone = next(zone)) |
336 | 346 | ||
@@ -340,34 +350,34 @@ static void make_bad_inode(void) | |||
340 | inode->i_nlinks = 1; | 350 | inode->i_nlinks = 1; |
341 | inode->i_time = time(NULL); | 351 | inode->i_time = time(NULL); |
342 | inode->i_mode = S_IFREG + 0000; | 352 | inode->i_mode = S_IFREG + 0000; |
343 | inode->i_size = badblocks*BLOCK_SIZE; | 353 | inode->i_size = badblocks * BLOCK_SIZE; |
344 | zone = next(0); | 354 | zone = next(0); |
345 | for (i=0 ; i<7 ; i++) { | 355 | for (i = 0; i < 7; i++) { |
346 | inode->i_zone[i] = zone; | 356 | inode->i_zone[i] = zone; |
347 | if (!NEXT_BAD) | 357 | if (!NEXT_BAD) |
348 | goto end_bad; | 358 | goto end_bad; |
349 | } | 359 | } |
350 | inode->i_zone[7] = ind = get_free_block(); | 360 | inode->i_zone[7] = ind = get_free_block(); |
351 | memset(ind_block,0,BLOCK_SIZE); | 361 | memset(ind_block, 0, BLOCK_SIZE); |
352 | for (i=0 ; i<512 ; i++) { | 362 | for (i = 0; i < 512; i++) { |
353 | ind_block[i] = zone; | 363 | ind_block[i] = zone; |
354 | if (!NEXT_BAD) | 364 | if (!NEXT_BAD) |
355 | goto end_bad; | 365 | goto end_bad; |
356 | } | 366 | } |
357 | inode->i_zone[8] = dind = get_free_block(); | 367 | inode->i_zone[8] = dind = get_free_block(); |
358 | memset(dind_block,0,BLOCK_SIZE); | 368 | memset(dind_block, 0, BLOCK_SIZE); |
359 | for (i=0 ; i<512 ; i++) { | 369 | for (i = 0; i < 512; i++) { |
360 | write_block(ind,(char *) ind_block); | 370 | write_block(ind, (char *) ind_block); |
361 | dind_block[i] = ind = get_free_block(); | 371 | dind_block[i] = ind = get_free_block(); |
362 | memset(ind_block,0,BLOCK_SIZE); | 372 | memset(ind_block, 0, BLOCK_SIZE); |
363 | for (j=0 ; j<512 ; j++) { | 373 | for (j = 0; j < 512; j++) { |
364 | ind_block[j] = zone; | 374 | ind_block[j] = zone; |
365 | if (!NEXT_BAD) | 375 | if (!NEXT_BAD) |
366 | goto end_bad; | 376 | goto end_bad; |
367 | } | 377 | } |
368 | } | 378 | } |
369 | die("too many bad blocks"); | 379 | die("too many bad blocks"); |
370 | end_bad: | 380 | end_bad: |
371 | if (ind) | 381 | if (ind) |
372 | write_block(ind, (char *) ind_block); | 382 | write_block(ind, (char *) ind_block); |
373 | if (dind) | 383 | if (dind) |
@@ -375,8 +385,7 @@ end_bad: | |||
375 | } | 385 | } |
376 | 386 | ||
377 | #ifdef HAVE_MINIX2 | 387 | #ifdef HAVE_MINIX2 |
378 | static void | 388 | static void make_bad_inode2(void) |
379 | make_bad_inode2 (void) | ||
380 | { | 389 | { |
381 | struct minix2_inode *inode = &Inode2[MINIX_BAD_INO]; | 390 | struct minix2_inode *inode = &Inode2[MINIX_BAD_INO]; |
382 | int i, j, zone; | 391 | int i, j, zone; |
@@ -386,30 +395,30 @@ make_bad_inode2 (void) | |||
386 | 395 | ||
387 | if (!badblocks) | 396 | if (!badblocks) |
388 | return; | 397 | return; |
389 | mark_inode (MINIX_BAD_INO); | 398 | mark_inode(MINIX_BAD_INO); |
390 | inode->i_nlinks = 1; | 399 | inode->i_nlinks = 1; |
391 | inode->i_atime = inode->i_mtime = inode->i_ctime = time (NULL); | 400 | inode->i_atime = inode->i_mtime = inode->i_ctime = time(NULL); |
392 | inode->i_mode = S_IFREG + 0000; | 401 | inode->i_mode = S_IFREG + 0000; |
393 | inode->i_size = badblocks * BLOCK_SIZE; | 402 | inode->i_size = badblocks * BLOCK_SIZE; |
394 | zone = next (0); | 403 | zone = next(0); |
395 | for (i = 0; i < 7; i++) { | 404 | for (i = 0; i < 7; i++) { |
396 | inode->i_zone[i] = zone; | 405 | inode->i_zone[i] = zone; |
397 | if (!NEXT_BAD) | 406 | if (!NEXT_BAD) |
398 | goto end_bad; | 407 | goto end_bad; |
399 | } | 408 | } |
400 | inode->i_zone[7] = ind = get_free_block (); | 409 | inode->i_zone[7] = ind = get_free_block(); |
401 | memset (ind_block, 0, BLOCK_SIZE); | 410 | memset(ind_block, 0, BLOCK_SIZE); |
402 | for (i = 0; i < 256; i++) { | 411 | for (i = 0; i < 256; i++) { |
403 | ind_block[i] = zone; | 412 | ind_block[i] = zone; |
404 | if (!NEXT_BAD) | 413 | if (!NEXT_BAD) |
405 | goto end_bad; | 414 | goto end_bad; |
406 | } | 415 | } |
407 | inode->i_zone[8] = dind = get_free_block (); | 416 | inode->i_zone[8] = dind = get_free_block(); |
408 | memset (dind_block, 0, BLOCK_SIZE); | 417 | memset(dind_block, 0, BLOCK_SIZE); |
409 | for (i = 0; i < 256; i++) { | 418 | for (i = 0; i < 256; i++) { |
410 | write_block (ind, (char *) ind_block); | 419 | write_block(ind, (char *) ind_block); |
411 | dind_block[i] = ind = get_free_block (); | 420 | dind_block[i] = ind = get_free_block(); |
412 | memset (ind_block, 0, BLOCK_SIZE); | 421 | memset(ind_block, 0, BLOCK_SIZE); |
413 | for (j = 0; j < 256; j++) { | 422 | for (j = 0; j < 256; j++) { |
414 | ind_block[j] = zone; | 423 | ind_block[j] = zone; |
415 | if (!NEXT_BAD) | 424 | if (!NEXT_BAD) |
@@ -417,47 +426,46 @@ make_bad_inode2 (void) | |||
417 | } | 426 | } |
418 | } | 427 | } |
419 | /* Could make triple indirect block here */ | 428 | /* Could make triple indirect block here */ |
420 | die ("too many bad blocks"); | 429 | die("too many bad blocks"); |
421 | end_bad: | 430 | end_bad: |
422 | if (ind) | 431 | if (ind) |
423 | write_block (ind, (char *) ind_block); | 432 | write_block(ind, (char *) ind_block); |
424 | if (dind) | 433 | if (dind) |
425 | write_block (dind, (char *) dind_block); | 434 | write_block(dind, (char *) dind_block); |
426 | } | 435 | } |
427 | #endif | 436 | #endif |
428 | 437 | ||
429 | static void make_root_inode(void) | 438 | static void make_root_inode(void) |
430 | { | 439 | { |
431 | struct minix_inode * inode = &Inode[MINIX_ROOT_INO]; | 440 | struct minix_inode *inode = &Inode[MINIX_ROOT_INO]; |
432 | 441 | ||
433 | mark_inode(MINIX_ROOT_INO); | 442 | mark_inode(MINIX_ROOT_INO); |
434 | inode->i_zone[0] = get_free_block(); | 443 | inode->i_zone[0] = get_free_block(); |
435 | inode->i_nlinks = 2; | 444 | inode->i_nlinks = 2; |
436 | inode->i_time = time(NULL); | 445 | inode->i_time = time(NULL); |
437 | if (badblocks) | 446 | if (badblocks) |
438 | inode->i_size = 3*dirsize; | 447 | inode->i_size = 3 * dirsize; |
439 | else { | 448 | else { |
440 | root_block[2*dirsize] = '\0'; | 449 | root_block[2 * dirsize] = '\0'; |
441 | root_block[2*dirsize+1] = '\0'; | 450 | root_block[2 * dirsize + 1] = '\0'; |
442 | inode->i_size = 2*dirsize; | 451 | inode->i_size = 2 * dirsize; |
443 | } | 452 | } |
444 | inode->i_mode = S_IFDIR + 0755; | 453 | inode->i_mode = S_IFDIR + 0755; |
445 | inode->i_uid = getuid(); | 454 | inode->i_uid = getuid(); |
446 | if (inode->i_uid) | 455 | if (inode->i_uid) |
447 | inode->i_gid = getgid(); | 456 | inode->i_gid = getgid(); |
448 | write_block(inode->i_zone[0],root_block); | 457 | write_block(inode->i_zone[0], root_block); |
449 | } | 458 | } |
450 | 459 | ||
451 | #ifdef HAVE_MINIX2 | 460 | #ifdef HAVE_MINIX2 |
452 | static void | 461 | static void make_root_inode2(void) |
453 | make_root_inode2 (void) | ||
454 | { | 462 | { |
455 | struct minix2_inode *inode = &Inode2[MINIX_ROOT_INO]; | 463 | struct minix2_inode *inode = &Inode2[MINIX_ROOT_INO]; |
456 | 464 | ||
457 | mark_inode (MINIX_ROOT_INO); | 465 | mark_inode(MINIX_ROOT_INO); |
458 | inode->i_zone[0] = get_free_block (); | 466 | inode->i_zone[0] = get_free_block(); |
459 | inode->i_nlinks = 2; | 467 | inode->i_nlinks = 2; |
460 | inode->i_atime = inode->i_mtime = inode->i_ctime = time (NULL); | 468 | inode->i_atime = inode->i_mtime = inode->i_ctime = time(NULL); |
461 | if (badblocks) | 469 | if (badblocks) |
462 | inode->i_size = 3 * dirsize; | 470 | inode->i_size = 3 * dirsize; |
463 | else { | 471 | else { |
@@ -469,7 +477,7 @@ make_root_inode2 (void) | |||
469 | inode->i_uid = getuid(); | 477 | inode->i_uid = getuid(); |
470 | if (inode->i_uid) | 478 | if (inode->i_uid) |
471 | inode->i_gid = getgid(); | 479 | inode->i_gid = getgid(); |
472 | write_block (inode->i_zone[0], root_block); | 480 | write_block(inode->i_zone[0], root_block); |
473 | } | 481 | } |
474 | #endif | 482 | #endif |
475 | 483 | ||
@@ -478,34 +486,38 @@ static void setup_tables(void) | |||
478 | int i; | 486 | int i; |
479 | unsigned long inodes; | 487 | unsigned long inodes; |
480 | 488 | ||
481 | memset(super_block_buffer,0,BLOCK_SIZE); | 489 | memset(super_block_buffer, 0, BLOCK_SIZE); |
482 | memset(boot_block_buffer,0,512); | 490 | memset(boot_block_buffer, 0, 512); |
483 | MAGIC = magic; | 491 | MAGIC = magic; |
484 | ZONESIZE = 0; | 492 | ZONESIZE = 0; |
485 | MAXSIZE = version2 ? 0x7fffffff : (7+512+512*512)*1024; | 493 | MAXSIZE = version2 ? 0x7fffffff : (7 + 512 + 512 * 512) * 1024; |
486 | ZONES = BLOCKS; | 494 | ZONES = BLOCKS; |
487 | /* some magic nrs: 1 inode / 3 blocks */ | 495 | /* some magic nrs: 1 inode / 3 blocks */ |
488 | if ( req_nr_inodes == 0 ) | 496 | if (req_nr_inodes == 0) |
489 | inodes = BLOCKS/3; | 497 | inodes = BLOCKS / 3; |
490 | else | 498 | else |
491 | inodes = req_nr_inodes; | 499 | inodes = req_nr_inodes; |
492 | /* Round up inode count to fill block size */ | 500 | /* Round up inode count to fill block size */ |
493 | #ifdef HAVE_MINIX2 | 501 | #ifdef HAVE_MINIX2 |
494 | if (version2) | 502 | if (version2) |
495 | inodes = ((inodes + MINIX2_INODES_PER_BLOCK - 1) & | 503 | inodes = ((inodes + MINIX2_INODES_PER_BLOCK - 1) & |
496 | ~(MINIX2_INODES_PER_BLOCK - 1)); | 504 | ~(MINIX2_INODES_PER_BLOCK - 1)); |
497 | else | 505 | else |
498 | #endif | 506 | #endif |
499 | inodes = ((inodes + MINIX_INODES_PER_BLOCK - 1) & | 507 | inodes = ((inodes + MINIX_INODES_PER_BLOCK - 1) & |
500 | ~(MINIX_INODES_PER_BLOCK - 1)); | 508 | ~(MINIX_INODES_PER_BLOCK - 1)); |
501 | if (inodes > 65535) | 509 | if (inodes > 65535) |
502 | inodes = 65535; | 510 | inodes = 65535; |
503 | INODES = inodes; | 511 | INODES = inodes; |
504 | IMAPS = UPPER(INODES + 1,BITS_PER_BLOCK); | 512 | IMAPS = UPPER(INODES + 1, BITS_PER_BLOCK); |
505 | ZMAPS = 0; | 513 | ZMAPS = 0; |
506 | i=0; | 514 | i = 0; |
507 | while (ZMAPS != UPPER(BLOCKS - (2+IMAPS+ZMAPS+INODE_BLOCKS) + 1,BITS_PER_BLOCK) && i<1000) { | 515 | while (ZMAPS != |
508 | ZMAPS = UPPER(BLOCKS - (2+IMAPS+ZMAPS+INODE_BLOCKS) + 1,BITS_PER_BLOCK); | 516 | UPPER(BLOCKS - (2 + IMAPS + ZMAPS + INODE_BLOCKS) + 1, |
517 | BITS_PER_BLOCK) && i < 1000) { | ||
518 | ZMAPS = | ||
519 | UPPER(BLOCKS - (2 + IMAPS + ZMAPS + INODE_BLOCKS) + 1, | ||
520 | BITS_PER_BLOCK); | ||
509 | i++; | 521 | i++; |
510 | } | 522 | } |
511 | /* Real bad hack but overwise mkfs.minix can be thrown | 523 | /* Real bad hack but overwise mkfs.minix can be thrown |
@@ -514,50 +526,51 @@ static void setup_tables(void) | |||
514 | * dd if=/dev/zero of=test.fs count=10 bs=1024 | 526 | * dd if=/dev/zero of=test.fs count=10 bs=1024 |
515 | * /sbin/mkfs.minix -i 200 test.fs | 527 | * /sbin/mkfs.minix -i 200 test.fs |
516 | * */ | 528 | * */ |
517 | if (i>=999) { | 529 | if (i >= 999) { |
518 | die ("unable to allocate buffers for maps"); | 530 | die("unable to allocate buffers for maps"); |
519 | } | 531 | } |
520 | FIRSTZONE = NORM_FIRSTZONE; | 532 | FIRSTZONE = NORM_FIRSTZONE; |
521 | inode_map = malloc(IMAPS * BLOCK_SIZE); | 533 | inode_map = malloc(IMAPS * BLOCK_SIZE); |
522 | zone_map = malloc(ZMAPS * BLOCK_SIZE); | 534 | zone_map = malloc(ZMAPS * BLOCK_SIZE); |
523 | if (!inode_map || !zone_map) | 535 | if (!inode_map || !zone_map) |
524 | die("unable to allocate buffers for maps"); | 536 | die("unable to allocate buffers for maps"); |
525 | memset(inode_map,0xff,IMAPS * BLOCK_SIZE); | 537 | memset(inode_map, 0xff, IMAPS * BLOCK_SIZE); |
526 | memset(zone_map,0xff,ZMAPS * BLOCK_SIZE); | 538 | memset(zone_map, 0xff, ZMAPS * BLOCK_SIZE); |
527 | for (i = FIRSTZONE ; i<ZONES ; i++) | 539 | for (i = FIRSTZONE; i < ZONES; i++) |
528 | unmark_zone(i); | 540 | unmark_zone(i); |
529 | for (i = MINIX_ROOT_INO ; i<=INODES ; i++) | 541 | for (i = MINIX_ROOT_INO; i <= INODES; i++) |
530 | unmark_inode(i); | 542 | unmark_inode(i); |
531 | inode_buffer = malloc(INODE_BUFFER_SIZE); | 543 | inode_buffer = malloc(INODE_BUFFER_SIZE); |
532 | if (!inode_buffer) | 544 | if (!inode_buffer) |
533 | die("unable to allocate buffer for inodes"); | 545 | die("unable to allocate buffer for inodes"); |
534 | memset(inode_buffer,0,INODE_BUFFER_SIZE); | 546 | memset(inode_buffer, 0, INODE_BUFFER_SIZE); |
535 | printf("%ld inodes\n",INODES); | 547 | printf("%ld inodes\n", INODES); |
536 | printf("%ld blocks\n",ZONES); | 548 | printf("%ld blocks\n", ZONES); |
537 | printf("Firstdatazone=%ld (%ld)\n",FIRSTZONE,NORM_FIRSTZONE); | 549 | printf("Firstdatazone=%ld (%ld)\n", FIRSTZONE, NORM_FIRSTZONE); |
538 | printf("Zonesize=%d\n",BLOCK_SIZE<<ZONESIZE); | 550 | printf("Zonesize=%d\n", BLOCK_SIZE << ZONESIZE); |
539 | printf("Maxsize=%ld\n\n",MAXSIZE); | 551 | printf("Maxsize=%ld\n\n", MAXSIZE); |
540 | } | 552 | } |
541 | 553 | ||
542 | /* | 554 | /* |
543 | * Perform a test of a block; return the number of | 555 | * Perform a test of a block; return the number of |
544 | * blocks readable/writeable. | 556 | * blocks readable/writeable. |
545 | */ | 557 | */ |
546 | long do_check(char * buffer, int try, unsigned int current_block) | 558 | long do_check(char *buffer, int try, unsigned int current_block) |
547 | { | 559 | { |
548 | long got; | 560 | long got; |
549 | 561 | ||
550 | /* Seek to the correct loc. */ | 562 | /* Seek to the correct loc. */ |
551 | if (lseek(DEV, current_block * BLOCK_SIZE, SEEK_SET) != | 563 | if (lseek(DEV, current_block * BLOCK_SIZE, SEEK_SET) != |
552 | current_block * BLOCK_SIZE ) { | 564 | current_block * BLOCK_SIZE) { |
553 | die("seek failed during testing of blocks"); | 565 | die("seek failed during testing of blocks"); |
554 | } | 566 | } |
555 | 567 | ||
556 | 568 | ||
557 | /* Try the read */ | 569 | /* Try the read */ |
558 | got = read(DEV, buffer, try * BLOCK_SIZE); | 570 | got = read(DEV, buffer, try * BLOCK_SIZE); |
559 | if (got < 0) got = 0; | 571 | if (got < 0) |
560 | if (got & (BLOCK_SIZE - 1 )) { | 572 | got = 0; |
573 | if (got & (BLOCK_SIZE - 1)) { | ||
561 | printf("Weird values in do_check: probably bugs\n"); | 574 | printf("Weird values in do_check: probably bugs\n"); |
562 | } | 575 | } |
563 | got /= BLOCK_SIZE; | 576 | got /= BLOCK_SIZE; |
@@ -570,7 +583,7 @@ static void alarm_intr(int alnum) | |||
570 | { | 583 | { |
571 | if (currently_testing >= ZONES) | 584 | if (currently_testing >= ZONES) |
572 | return; | 585 | return; |
573 | signal(SIGALRM,alarm_intr); | 586 | signal(SIGALRM, alarm_intr); |
574 | alarm(5); | 587 | alarm(5); |
575 | if (!currently_testing) | 588 | if (!currently_testing) |
576 | return; | 589 | return; |
@@ -580,19 +593,19 @@ static void alarm_intr(int alnum) | |||
580 | 593 | ||
581 | static void check_blocks(void) | 594 | static void check_blocks(void) |
582 | { | 595 | { |
583 | int try,got; | 596 | int try, got; |
584 | static char buffer[BLOCK_SIZE * TEST_BUFFER_BLOCKS]; | 597 | static char buffer[BLOCK_SIZE * TEST_BUFFER_BLOCKS]; |
585 | 598 | ||
586 | currently_testing=0; | 599 | currently_testing = 0; |
587 | signal(SIGALRM,alarm_intr); | 600 | signal(SIGALRM, alarm_intr); |
588 | alarm(5); | 601 | alarm(5); |
589 | while (currently_testing < ZONES) { | 602 | while (currently_testing < ZONES) { |
590 | if (lseek(DEV,currently_testing*BLOCK_SIZE,SEEK_SET) != | 603 | if (lseek(DEV, currently_testing * BLOCK_SIZE, SEEK_SET) != |
591 | currently_testing*BLOCK_SIZE) | 604 | currently_testing * BLOCK_SIZE) |
592 | die("seek failed in check_blocks"); | 605 | die("seek failed in check_blocks"); |
593 | try = TEST_BUFFER_BLOCKS; | 606 | try = TEST_BUFFER_BLOCKS; |
594 | if (currently_testing + try > ZONES) | 607 | if (currently_testing + try > ZONES) |
595 | try = ZONES-currently_testing; | 608 | try = ZONES - currently_testing; |
596 | got = do_check(buffer, try, currently_testing); | 609 | got = do_check(buffer, try, currently_testing); |
597 | currently_testing += got; | 610 | currently_testing += got; |
598 | if (got == try) | 611 | if (got == try) |
@@ -613,139 +626,141 @@ static void get_list_blocks(filename) | |||
613 | char *filename; | 626 | char *filename; |
614 | 627 | ||
615 | { | 628 | { |
616 | FILE *listfile; | 629 | FILE *listfile; |
617 | unsigned long blockno; | 630 | unsigned long blockno; |
618 | 631 | ||
619 | listfile=fopen(filename,"r"); | 632 | listfile = fopen(filename, "r"); |
620 | if(listfile == (FILE *)NULL) { | 633 | if (listfile == (FILE *) NULL) { |
621 | die("can't open file of bad blocks"); | 634 | die("can't open file of bad blocks"); |
622 | } | 635 | } |
623 | while(!feof(listfile)) { | 636 | while (!feof(listfile)) { |
624 | fscanf(listfile,"%ld\n", &blockno); | 637 | fscanf(listfile, "%ld\n", &blockno); |
625 | mark_zone(blockno); | 638 | mark_zone(blockno); |
626 | badblocks++; | 639 | badblocks++; |
627 | } | 640 | } |
628 | if(badblocks > 1) | 641 | if (badblocks > 1) |
629 | printf("%d bad blocks\n", badblocks); | 642 | printf("%d bad blocks\n", badblocks); |
630 | else if (badblocks == 1) | 643 | else if (badblocks == 1) |
631 | printf("one bad block\n"); | 644 | printf("one bad block\n"); |
632 | } | 645 | } |
633 | 646 | ||
634 | extern int | 647 | extern int mkfs_minix_main(int argc, char **argv) |
635 | mkfs_minix_main(int argc, char ** argv) | ||
636 | { | 648 | { |
637 | int i; | 649 | int i; |
638 | char * tmp; | 650 | char *tmp; |
639 | struct stat statbuf; | 651 | struct stat statbuf; |
640 | char * listfile = NULL; | 652 | char *listfile = NULL; |
641 | 653 | ||
642 | if (argc && *argv) | 654 | if (argc && *argv) |
643 | program_name = *argv; | 655 | program_name = *argv; |
644 | if (INODE_SIZE * MINIX_INODES_PER_BLOCK != BLOCK_SIZE) | 656 | if (INODE_SIZE * MINIX_INODES_PER_BLOCK != BLOCK_SIZE) |
645 | die("bad inode size"); | 657 | die("bad inode size"); |
646 | #ifdef HAVE_MINIX2 | 658 | #ifdef HAVE_MINIX2 |
647 | if (INODE_SIZE2 * MINIX2_INODES_PER_BLOCK != BLOCK_SIZE) | 659 | if (INODE_SIZE2 * MINIX2_INODES_PER_BLOCK != BLOCK_SIZE) |
648 | die("bad inode size"); | 660 | die("bad inode size"); |
649 | #endif | 661 | #endif |
650 | opterr = 0; | 662 | opterr = 0; |
651 | while ((i = getopt(argc, argv, "ci:l:n:v")) != EOF) | 663 | while ((i = getopt(argc, argv, "ci:l:n:v")) != EOF) |
652 | switch (i) { | 664 | switch (i) { |
653 | case 'c': | 665 | case 'c': |
654 | check=1; break; | 666 | check = 1; |
655 | case 'i': | 667 | break; |
656 | req_nr_inodes = (unsigned long) atol(optarg); | 668 | case 'i': |
657 | break; | 669 | req_nr_inodes = (unsigned long) atol(optarg); |
658 | case 'l': | 670 | break; |
659 | listfile = optarg; break; | 671 | case 'l': |
660 | case 'n': | 672 | listfile = optarg; |
661 | i = strtoul(optarg,&tmp,0); | 673 | break; |
662 | if (*tmp) | 674 | case 'n': |
663 | show_usage(); | 675 | i = strtoul(optarg, &tmp, 0); |
664 | if (i == 14) | 676 | if (*tmp) |
665 | magic = MINIX_SUPER_MAGIC; | 677 | show_usage(); |
666 | else if (i == 30) | 678 | if (i == 14) |
667 | magic = MINIX_SUPER_MAGIC2; | 679 | magic = MINIX_SUPER_MAGIC; |
668 | else | 680 | else if (i == 30) |
669 | show_usage(); | 681 | magic = MINIX_SUPER_MAGIC2; |
670 | namelen = i; | 682 | else |
671 | dirsize = i+2; | 683 | show_usage(); |
672 | break; | 684 | namelen = i; |
673 | case 'v': | 685 | dirsize = i + 2; |
686 | break; | ||
687 | case 'v': | ||
674 | #ifdef HAVE_MINIX2 | 688 | #ifdef HAVE_MINIX2 |
675 | version2 = 1; | 689 | version2 = 1; |
676 | #else | 690 | #else |
677 | fprintf(stderr,"%s: not compiled with minix v2 support\n",program_name,device_name); | 691 | fprintf(stderr, "%s: not compiled with minix v2 support\n", |
678 | exit(-1); | 692 | program_name, device_name); |
693 | exit(-1); | ||
679 | #endif | 694 | #endif |
680 | break; | 695 | break; |
681 | default: | 696 | default: |
682 | show_usage(); | 697 | show_usage(); |
683 | } | 698 | } |
684 | argc -= optind; | 699 | argc -= optind; |
685 | argv += optind; | 700 | argv += optind; |
686 | if (argc > 0 && !device_name) { | 701 | if (argc > 0 && !device_name) { |
687 | device_name = argv[0]; | 702 | device_name = argv[0]; |
688 | argc--; | 703 | argc--; |
689 | argv++; | 704 | argv++; |
690 | } | 705 | } |
691 | if (argc > 0) { | 706 | if (argc > 0) { |
692 | BLOCKS = strtol(argv[0],&tmp,0); | 707 | BLOCKS = strtol(argv[0], &tmp, 0); |
693 | if (*tmp) { | 708 | if (*tmp) { |
694 | printf("strtol error: number of blocks not specified"); | 709 | printf("strtol error: number of blocks not specified"); |
695 | show_usage(); | 710 | show_usage(); |
696 | } | 711 | } |
697 | } | 712 | } |
698 | 713 | ||
699 | if (device_name && !BLOCKS) | 714 | if (device_name && !BLOCKS) |
700 | BLOCKS = get_size (device_name) / 1024; | 715 | BLOCKS = get_size(device_name) / 1024; |
701 | if (!device_name || BLOCKS<10) { | 716 | if (!device_name || BLOCKS < 10) { |
702 | show_usage(); | 717 | show_usage(); |
703 | } | 718 | } |
704 | #ifdef HAVE_MINIX2 | 719 | #ifdef HAVE_MINIX2 |
705 | if (version2) { | 720 | if (version2) { |
706 | if (namelen == 14) | 721 | if (namelen == 14) |
707 | magic = MINIX2_SUPER_MAGIC; | 722 | magic = MINIX2_SUPER_MAGIC; |
708 | else | 723 | else |
709 | magic = MINIX2_SUPER_MAGIC2; | 724 | magic = MINIX2_SUPER_MAGIC2; |
710 | } else | 725 | } else |
711 | #endif | 726 | #endif |
712 | if (BLOCKS > 65535) | 727 | if (BLOCKS > 65535) |
713 | BLOCKS = 65535; | 728 | BLOCKS = 65535; |
714 | check_mount(); /* is it already mounted? */ | 729 | check_mount(); /* is it already mounted? */ |
715 | tmp = root_block; | 730 | tmp = root_block; |
716 | *(short *)tmp = 1; | 731 | *(short *) tmp = 1; |
717 | strcpy(tmp+2,"."); | 732 | strcpy(tmp + 2, "."); |
718 | tmp += dirsize; | 733 | tmp += dirsize; |
719 | *(short *)tmp = 1; | 734 | *(short *) tmp = 1; |
720 | strcpy(tmp+2,".."); | 735 | strcpy(tmp + 2, ".."); |
721 | tmp += dirsize; | 736 | tmp += dirsize; |
722 | *(short *)tmp = 2; | 737 | *(short *) tmp = 2; |
723 | strcpy(tmp+2,".badblocks"); | 738 | strcpy(tmp + 2, ".badblocks"); |
724 | DEV = open(device_name,O_RDWR ); | 739 | DEV = open(device_name, O_RDWR); |
725 | if (DEV<0) | 740 | if (DEV < 0) |
726 | die("unable to open %s"); | 741 | die("unable to open %s"); |
727 | if (fstat(DEV,&statbuf)<0) | 742 | if (fstat(DEV, &statbuf) < 0) |
728 | die("unable to stat %s"); | 743 | die("unable to stat %s"); |
729 | if (!S_ISBLK(statbuf.st_mode)) | 744 | if (!S_ISBLK(statbuf.st_mode)) |
730 | check=0; | 745 | check = 0; |
731 | else if (statbuf.st_rdev == 0x0300 || statbuf.st_rdev == 0x0340) | 746 | else if (statbuf.st_rdev == 0x0300 || statbuf.st_rdev == 0x0340) |
732 | die("will not try to make filesystem on '%s'"); | 747 | die("will not try to make filesystem on '%s'"); |
733 | setup_tables(); | 748 | setup_tables(); |
734 | if (check) | 749 | if (check) |
735 | check_blocks(); | 750 | check_blocks(); |
736 | else if (listfile) | 751 | else if (listfile) |
737 | get_list_blocks(listfile); | 752 | get_list_blocks(listfile); |
738 | #ifdef HAVE_MINIX2 | 753 | #ifdef HAVE_MINIX2 |
739 | if (version2) { | 754 | if (version2) { |
740 | make_root_inode2 (); | 755 | make_root_inode2(); |
741 | make_bad_inode2 (); | 756 | make_bad_inode2(); |
742 | } else | 757 | } else |
743 | #endif | 758 | #endif |
744 | { | 759 | { |
745 | make_root_inode(); | 760 | make_root_inode(); |
746 | make_bad_inode(); | 761 | make_bad_inode(); |
747 | } | 762 | } |
748 | mark_good_blocks(); | 763 | mark_good_blocks(); |
749 | write_tables(); | 764 | write_tables(); |
750 | return 0; | 765 | return 0; |
751 | } | 766 | } |