~ubuntu-branches/ubuntu/trusty/util-linux/trusty-proposed

« back to all changes in this revision

Viewing changes to disk-utils/fsck.minix.c

  • Committer: Package Import Robot
  • Author(s): LaMont Jones
  • Date: 2011-11-03 15:38:23 UTC
  • mto: (4.5.5 sid) (1.6.4)
  • mto: This revision was merged to the branch mainline in revision 85.
  • Revision ID: package-import@ubuntu.com-20111103153823-10sx16jprzxlhkqf
ImportĀ upstreamĀ versionĀ 2.20.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
48
48
 * 01.07.96  - Fixed the v2 fs stuff to use the right #defines and such
49
49
 *             for modern libcs (janl@math.uio.no, Nicolai Langfeldt)
50
50
 *
51
 
 * 02.07.96  - Added C bit fiddling routines from rmk@ecs.soton.ac.uk 
 
51
 * 02.07.96  - Added C bit fiddling routines from rmk@ecs.soton.ac.uk
52
52
 *             (Russell King).  He made them for ARM.  It would seem
53
53
 *             that the ARM is powerful enough to do this in C whereas
54
54
 *             i386 and m64k must use assembly to get it fast >:-)
56
56
 *             (janl@math.uio.no, Nicolai Langfeldt)
57
57
 *
58
58
 * 04.11.96  - Added minor fixes from Andreas Schwab to avoid compiler
59
 
 *             warnings.  Added mc68k bitops from 
 
59
 *             warnings.  Added mc68k bitops from
60
60
 *             Joerg Dorchain <dorchain@mpi-sb.mpg.de>.
61
61
 *
62
62
 * 06.11.96  - Added v2 code submitted by Joerg Dorchain, but written by
86
86
 *      -f force filesystem check even if filesystem marked as valid
87
87
 *
88
88
 * The device may be a block device or a image of one, but this isn't
89
 
 * enforced (but it's not much fun on a character device :-). 
 
89
 * enforced (but it's not much fun on a character device :-).
90
90
 */
91
91
 
92
92
#include <stdio.h>
102
102
#include <sys/stat.h>
103
103
#include <signal.h>
104
104
 
105
 
#include "minix.h"
 
105
#include "minix_programs.h"
106
106
#include "nls.h"
107
107
#include "pathnames.h"
108
108
#include "bitops.h"
 
109
#include "ismounted.h"
 
110
#include "writeall.h"
109
111
 
110
112
#define ROOT_INO 1
111
113
 
112
 
#define UPPER(size,n) ((size+((n)-1))/(n))
113
 
#define INODE_SIZE (sizeof(struct minix_inode))
114
 
#define INODE_SIZE2 (sizeof(struct minix2_inode))
115
 
#define INODE_BLOCKS UPPER(INODES, (version2 ? MINIX2_INODES_PER_BLOCK \
116
 
                                    : MINIX_INODES_PER_BLOCK))
117
 
#define INODE_BUFFER_SIZE (INODE_BLOCKS * BLOCK_SIZE)
118
 
 
119
 
#define BITS_PER_BLOCK (BLOCK_SIZE<<3)
120
 
 
121
 
static char * program_name = "fsck.minix";
122
 
static char * device_name = NULL;
 
114
/*
 
115
 * Global variables used in minix_programs.h inline fuctions
 
116
 */
 
117
int fs_version = 1;
 
118
char *super_block_buffer;
 
119
 
 
120
 
 
121
static char *inode_buffer;
 
122
 
 
123
#define Inode (((struct minix_inode *) inode_buffer) - 1)
 
124
#define Inode2 (((struct minix2_inode *) inode_buffer) - 1)
 
125
 
 
126
static char *program_name = "fsck.minix";
 
127
static char *device_name;
123
128
static int IN;
124
 
static int repair=0, automatic=0, verbose=0, list=0, show=0, warn_mode=0, 
125
 
        force=0;
126
 
static int directory=0, regular=0, blockdev=0, chardev=0, links=0,
127
 
                symlinks=0, total=0;
 
129
static int repair, automatic, verbose, list, show, warn_mode, force;
 
130
static int directory, regular, blockdev, chardev, links, symlinks, total;
128
131
 
129
 
static int changed = 0; /* flags if the filesystem has been changed */
130
 
static int errors_uncorrected = 0; /* flag if some error was not corrected */
131
 
static int dirsize = 16;
132
 
static int namelen = 14;
133
 
static int version2 = 0;
 
132
static int changed; /* flags if the filesystem has been changed */
 
133
static int errors_uncorrected; /* flag if some error was not corrected */
 
134
static size_t dirsize = 16;
 
135
static size_t namelen = 14;
134
136
static struct termios termios;
135
 
static volatile sig_atomic_t termios_set = 0;
 
137
static volatile sig_atomic_t termios_set;
136
138
 
137
139
/* File-name data */
138
140
#define MAX_DEPTH 50
139
 
static int name_depth = 0;
140
 
static char name_list[MAX_DEPTH][NAME_MAX+1];
 
141
static int name_depth;
 
142
static char name_list[MAX_DEPTH][MINIX_NAME_MAX + 1];
 
143
 
141
144
/* Copy of the previous, just for error reporting - see get_current_name */
142
145
/* This is a waste of 12kB or so. */
143
 
static char current_name[MAX_DEPTH*(NAME_MAX+1)+1];
144
 
 
145
 
static char * inode_buffer = NULL;
146
 
#define Inode (((struct minix_inode *) inode_buffer)-1)
147
 
#define Inode2 (((struct minix2_inode *) inode_buffer)-1)
148
 
 
149
 
static char *super_block_buffer;
150
 
#define Super (*(struct minix_super_block *)super_block_buffer)
151
 
#define INODES ((unsigned long)Super.s_ninodes)
152
 
#define ZONES ((unsigned long)(version2 ? Super.s_zones : Super.s_nzones))
153
 
#define IMAPS ((unsigned long)Super.s_imap_blocks)
154
 
#define ZMAPS ((unsigned long)Super.s_zmap_blocks)
155
 
#define FIRSTZONE ((unsigned long)Super.s_firstdatazone)
156
 
#define ZONESIZE ((unsigned long)Super.s_log_zone_size)
157
 
#define MAXSIZE ((unsigned long)Super.s_max_size)
 
146
static char current_name[MAX_DEPTH * (MINIX_NAME_MAX + 1) + 1];
 
147
 
158
148
#define MAGIC (Super.s_magic)
159
 
#define NORM_FIRSTZONE (2+IMAPS+ZMAPS+INODE_BLOCKS)
 
149
 
 
150
static unsigned char *inode_count = NULL;
 
151
static unsigned char *zone_count = NULL;
 
152
 
 
153
static void recursive_check(unsigned int ino);
 
154
static void recursive_check2(unsigned int ino);
160
155
 
161
156
static char *inode_map;
162
157
static char *zone_map;
163
158
 
164
 
static unsigned char * inode_count = NULL;
165
 
static unsigned char * zone_count = NULL;
166
 
 
167
 
static void recursive_check(unsigned int ino);
168
 
static void recursive_check2(unsigned int ino);
169
 
 
170
159
#define inode_in_use(x) (isset(inode_map,(x)) != 0)
171
 
#define zone_in_use(x) (isset(zone_map,(x)-FIRSTZONE+1) != 0)
 
160
#define zone_in_use(x) (isset(zone_map,(x)-get_first_zone()+1) != 0)
172
161
 
173
162
#define mark_inode(x) (setbit(inode_map,(x)),changed=1)
174
163
#define unmark_inode(x) (clrbit(inode_map,(x)),changed=1)
175
164
 
176
 
#define mark_zone(x) (setbit(zone_map,(x)-FIRSTZONE+1),changed=1)
177
 
#define unmark_zone(x) (clrbit(zone_map,(x)-FIRSTZONE+1),changed=1)
 
165
#define mark_zone(x) (setbit(zone_map,(x)-get_first_zone()+1),changed=1)
 
166
#define unmark_zone(x) (clrbit(zone_map,(x)-get_first_zone()+1),changed=1)
178
167
 
179
168
static void
180
169
reset(void) {
294
283
 * 1994 Theodore Ts'o.  Also licensed under GPL.
295
284
 */
296
285
static void
297
 
check_mount(void) {
298
 
        FILE * f;
299
 
        struct mntent * mnt;
 
286
check_mount(void)
 
287
{
300
288
        int cont;
301
 
        int fd;
302
 
 
303
 
        if ((f = setmntent (_PATH_MOUNTED, "r")) == NULL)
304
 
                return;
305
 
        while ((mnt = getmntent (f)) != NULL)
306
 
                if (strcmp (device_name, mnt->mnt_fsname) == 0)
307
 
                        break;
308
 
        endmntent (f);
309
 
        if (!mnt)
310
 
                return;
311
 
 
312
 
        /*
313
 
         * If the root is mounted read-only, then /etc/mtab is
314
 
         * probably not correct; so we won't issue a warning based on
315
 
         * it.
316
 
         */
317
 
        fd = open(_PATH_MOUNTED, O_RDWR);
318
 
        if (fd < 0 && errno == EROFS)
319
 
                return;
320
 
        else
321
 
                close(fd);
322
 
        
 
289
 
 
290
        if (!is_mounted(device_name))
 
291
                return;
 
292
 
323
293
        printf (_("%s is mounted.        "), device_name);
324
294
        if (isatty(0) && isatty(1))
325
295
                cont = ask(_("Do you really want to continue"), 0);
343
313
        if (!*nr)
344
314
                return 0;
345
315
 
346
 
        if (*nr < FIRSTZONE) {
 
316
        if (*nr < get_first_zone()) {
347
317
                get_current_name();
348
318
                printf(_("Zone nr < FIRSTZONE in file `%s'."),
349
319
                       current_name);
350
 
        } else if (*nr >= ZONES) {
 
320
        } else if (*nr >= get_nzones()) {
351
321
                get_current_name();
352
322
                printf(_("Zone nr >= ZONES in file `%s'."),
353
323
                       current_name);
366
336
        if (!*nr)
367
337
                return 0;
368
338
 
369
 
        if (*nr < FIRSTZONE) {
 
339
        if (*nr < get_first_zone()) {
370
340
                get_current_name();
371
341
                printf (_("Zone nr < FIRSTZONE in file `%s'."),
372
342
                        current_name);
373
 
        } else if (*nr >= ZONES) {
 
343
        } else if (*nr >= get_nzones()) {
374
344
                get_current_name();
375
345
                printf (_("Zone nr >= ZONES in file `%s'."),
376
346
                        current_name);
390
360
static void
391
361
read_block(unsigned int nr, char * addr) {
392
362
        if (!nr) {
393
 
                memset(addr,0,BLOCK_SIZE);
 
363
                memset(addr,0,MINIX_BLOCK_SIZE);
394
364
                return;
395
365
        }
396
 
        if (BLOCK_SIZE*nr != lseek(IN, BLOCK_SIZE*nr, SEEK_SET)) {
 
366
        if (MINIX_BLOCK_SIZE*nr != lseek(IN, MINIX_BLOCK_SIZE*nr, SEEK_SET)) {
397
367
                get_current_name();
398
368
                printf(_("Read error: unable to seek to block in file '%s'\n"),
399
369
                       current_name);
400
 
                memset(addr,0,BLOCK_SIZE);
 
370
                memset(addr,0,MINIX_BLOCK_SIZE);
401
371
                errors_uncorrected = 1;
402
 
        } else if (BLOCK_SIZE != read(IN, addr, BLOCK_SIZE)) {
 
372
        } else if (MINIX_BLOCK_SIZE != read(IN, addr, MINIX_BLOCK_SIZE)) {
403
373
                get_current_name();
404
374
                printf(_("Read error: bad block in file '%s'\n"),
405
375
                       current_name);
406
 
                memset(addr,0,BLOCK_SIZE);
 
376
                memset(addr,0,MINIX_BLOCK_SIZE);
407
377
                errors_uncorrected = 1;
408
378
        }
409
379
}
415
385
write_block(unsigned int nr, char * addr) {
416
386
        if (!nr)
417
387
                return;
418
 
        if (nr < FIRSTZONE || nr >= ZONES) {
 
388
        if (nr < get_first_zone() || nr >= get_nzones()) {
419
389
                printf(_("Internal error: trying to write bad block\n"
420
390
                "Write request ignored\n"));
421
391
                errors_uncorrected = 1;
422
392
                return;
423
393
        }
424
 
        if (BLOCK_SIZE*nr != lseek(IN, BLOCK_SIZE*nr, SEEK_SET))
 
394
        if (MINIX_BLOCK_SIZE*nr != lseek(IN, MINIX_BLOCK_SIZE*nr, SEEK_SET))
425
395
                die(_("seek failed in write_block"));
426
 
        if (BLOCK_SIZE != write(IN, addr, BLOCK_SIZE)) {
 
396
        if (MINIX_BLOCK_SIZE != write(IN, addr, MINIX_BLOCK_SIZE)) {
427
397
                get_current_name();
428
398
                printf(_("Write error: bad block in file '%s'\n"),
429
399
                       current_name);
438
408
 */
439
409
static int
440
410
map_block(struct minix_inode * inode, unsigned int blknr) {
441
 
        unsigned short ind[BLOCK_SIZE>>1];
442
 
        unsigned short dind[BLOCK_SIZE>>1];
 
411
        unsigned short ind[MINIX_BLOCK_SIZE>>1];
 
412
        unsigned short dind[MINIX_BLOCK_SIZE>>1];
443
413
        int blk_chg, block, result;
444
414
 
445
415
        if (blknr<7)
472
442
 
473
443
static int
474
444
map_block2 (struct minix2_inode *inode, unsigned int blknr) {
475
 
        unsigned int ind[BLOCK_SIZE >> 2];
476
 
        unsigned int dind[BLOCK_SIZE >> 2];
477
 
        unsigned int tind[BLOCK_SIZE >> 2];
 
445
        unsigned int ind[MINIX_BLOCK_SIZE >> 2];
 
446
        unsigned int dind[MINIX_BLOCK_SIZE >> 2];
 
447
        unsigned int tind[MINIX_BLOCK_SIZE >> 2];
478
448
        int blk_chg, block, result;
479
449
 
480
450
        if (blknr < 7)
490
460
                return result;
491
461
        }
492
462
        blknr -= 256;
493
 
        if (blknr >= 256 * 256) {
 
463
        if (blknr < 256 * 256) {
494
464
                block = check_zone_nr2 (inode->i_zone + 8, &changed);
495
465
                read_block (block, (char *) dind);
496
466
                blk_chg = 0;
539
509
                Super.s_state |= MINIX_ERROR_FS;
540
510
        else
541
511
                Super.s_state &= ~MINIX_ERROR_FS;
542
 
        
543
 
        if (BLOCK_SIZE != lseek(IN, BLOCK_SIZE, SEEK_SET))
 
512
 
 
513
        if (MINIX_BLOCK_SIZE != lseek(IN, MINIX_BLOCK_SIZE, SEEK_SET))
544
514
                die(_("seek failed in write_super_block"));
545
 
        if (BLOCK_SIZE != write(IN, super_block_buffer, BLOCK_SIZE))
 
515
        if (MINIX_BLOCK_SIZE != write(IN, super_block_buffer, MINIX_BLOCK_SIZE))
546
516
                die(_("unable to write super-block"));
547
517
 
548
518
        return;
551
521
static void
552
522
write_tables(void) {
553
523
        write_super_block();
 
524
        unsigned long buffsz = get_inode_buffer_size();
 
525
        unsigned long imaps = get_nimaps();
 
526
        unsigned long zmaps = get_nzmaps();
554
527
 
555
 
        if (IMAPS*BLOCK_SIZE != write(IN,inode_map,IMAPS*BLOCK_SIZE))
 
528
        if (write_all(IN, inode_map, imaps * MINIX_BLOCK_SIZE))
556
529
                die(_("Unable to write inode map"));
557
 
        if (ZMAPS*BLOCK_SIZE != write(IN,zone_map,ZMAPS*BLOCK_SIZE))
 
530
 
 
531
        if (write_all(IN, zone_map, zmaps * MINIX_BLOCK_SIZE))
558
532
                die(_("Unable to write zone map"));
559
 
        if (INODE_BUFFER_SIZE != write(IN,inode_buffer,INODE_BUFFER_SIZE))
 
533
 
 
534
        if (write_all(IN, inode_buffer, buffsz))
560
535
                die(_("Unable to write inodes"));
561
536
}
562
537
 
563
538
static void
564
539
get_dirsize (void) {
565
540
        int block;
566
 
        char blk[BLOCK_SIZE];
567
 
        int size;
 
541
        char blk[MINIX_BLOCK_SIZE];
 
542
        size_t size;
568
543
 
569
 
        if (version2)
 
544
        if (fs_version == 2)
570
545
                block = Inode2[ROOT_INO].i_zone[0];
571
546
        else
572
547
                block = Inode[ROOT_INO].i_zone[0];
573
548
        read_block (block, blk);
574
 
        for (size = 16; size < BLOCK_SIZE; size <<= 1) {
575
 
                if (strcmp (blk + size + 2, "..") == 0) {
 
549
 
 
550
        for (size = 16; size < MINIX_BLOCK_SIZE; size <<= 1) {
 
551
                if (strcmp(blk + size + 2, "..") == 0) {
576
552
                        dirsize = size;
577
553
                        namelen = size - 2;
578
554
                        return;
583
559
 
584
560
static void
585
561
read_superblock(void) {
586
 
        if (BLOCK_SIZE != lseek(IN, BLOCK_SIZE, SEEK_SET))
 
562
        if (MINIX_BLOCK_SIZE != lseek(IN, MINIX_BLOCK_SIZE, SEEK_SET))
587
563
                die(_("seek failed"));
588
564
 
589
 
        super_block_buffer = calloc(1, BLOCK_SIZE);
 
565
        super_block_buffer = calloc(1, MINIX_BLOCK_SIZE);
590
566
        if (!super_block_buffer)
591
567
                die(_("unable to alloc buffer for superblock"));
592
568
 
593
 
        if (BLOCK_SIZE != read(IN, super_block_buffer, BLOCK_SIZE))
 
569
        if (MINIX_BLOCK_SIZE != read(IN, super_block_buffer, MINIX_BLOCK_SIZE))
594
570
                die(_("unable to read super block"));
595
571
        if (MAGIC == MINIX_SUPER_MAGIC) {
596
572
                namelen = 14;
597
573
                dirsize = 16;
598
 
                version2 = 0;
 
574
                fs_version = 1;
599
575
        } else if (MAGIC == MINIX_SUPER_MAGIC2) {
600
576
                namelen = 30;
601
577
                dirsize = 32;
602
 
                version2 = 0;
 
578
                fs_version = 1;
603
579
        } else if (MAGIC == MINIX2_SUPER_MAGIC) {
604
580
                namelen = 14;
605
581
                dirsize = 16;
606
 
                version2 = 1;
 
582
                fs_version = 2;
607
583
        } else if (MAGIC == MINIX2_SUPER_MAGIC2) {
608
584
                namelen = 30;
609
585
                dirsize = 32;
610
 
                version2 = 1;
 
586
                fs_version = 2;
611
587
        } else
612
588
                die(_("bad magic number in super-block"));
613
 
        if (ZONESIZE != 0 || BLOCK_SIZE != 1024)
 
589
        if (get_zone_size() != 0 || MINIX_BLOCK_SIZE != 1024)
614
590
                die(_("Only 1k blocks/zones supported"));
615
 
        if (IMAPS * BLOCK_SIZE * 8 < INODES + 1)
 
591
        if (get_nimaps() * MINIX_BLOCK_SIZE * 8 < get_ninodes() + 1)
616
592
                die(_("bad s_imap_blocks field in super-block"));
617
 
        if (ZMAPS * BLOCK_SIZE * 8 < ZONES - FIRSTZONE + 1)
 
593
        if (get_nzmaps() * MINIX_BLOCK_SIZE * 8 < get_nzones() - get_first_zone() + 1)
618
594
                die(_("bad s_zmap_blocks field in super-block"));
619
595
}
620
596
 
621
597
static void
622
598
read_tables(void) {
623
 
        inode_map = malloc(IMAPS * BLOCK_SIZE);
 
599
        unsigned long inodes = get_ninodes();
 
600
        unsigned long buffsz = get_inode_buffer_size();
 
601
        unsigned long norm_first_zone = first_zone_data();
 
602
        unsigned long first_zone = get_first_zone();
 
603
        unsigned long zones = get_nzones();
 
604
        unsigned long imaps = get_nimaps();
 
605
        unsigned long zmaps = get_nzmaps();
 
606
        ssize_t rc;
 
607
 
 
608
        inode_map = malloc(imaps * MINIX_BLOCK_SIZE);
624
609
        if (!inode_map)
625
610
                die(_("Unable to allocate buffer for inode map"));
626
 
        zone_map = malloc(ZMAPS * BLOCK_SIZE);
 
611
        zone_map = malloc(zmaps * MINIX_BLOCK_SIZE);
627
612
        if (!inode_map)
628
613
                die(_("Unable to allocate buffer for zone map"));
629
614
        memset(inode_map,0,sizeof(inode_map));
630
615
        memset(zone_map,0,sizeof(zone_map));
631
 
        inode_buffer = malloc(INODE_BUFFER_SIZE);
 
616
        inode_buffer = malloc(buffsz);
632
617
        if (!inode_buffer)
633
618
                die(_("Unable to allocate buffer for inodes"));
634
 
        inode_count = malloc(INODES + 1);
 
619
        inode_count = malloc(inodes + 1);
635
620
        if (!inode_count)
636
621
                die(_("Unable to allocate buffer for inode count"));
637
 
        zone_count = malloc(ZONES);
 
622
        zone_count = malloc(zones);
638
623
        if (!zone_count)
639
624
                die(_("Unable to allocate buffer for zone count"));
640
 
        if (IMAPS*BLOCK_SIZE != read(IN,inode_map,IMAPS*BLOCK_SIZE))
 
625
 
 
626
        rc = read(IN, inode_map, imaps * MINIX_BLOCK_SIZE);
 
627
        if (rc < 0 || imaps * MINIX_BLOCK_SIZE != (size_t) rc)
641
628
                die(_("Unable to read inode map"));
642
 
        if (ZMAPS*BLOCK_SIZE != read(IN,zone_map,ZMAPS*BLOCK_SIZE))
 
629
 
 
630
        rc = read(IN, zone_map, zmaps * MINIX_BLOCK_SIZE);
 
631
        if (rc < 0 || zmaps * MINIX_BLOCK_SIZE != (size_t) rc)
643
632
                die(_("Unable to read zone map"));
644
 
        if (INODE_BUFFER_SIZE != read(IN,inode_buffer,INODE_BUFFER_SIZE))
 
633
 
 
634
        rc = read(IN,inode_buffer, buffsz);
 
635
        if (rc < 0 || buffsz != (size_t) rc)
645
636
                die(_("Unable to read inodes"));
646
 
        if (NORM_FIRSTZONE != FIRSTZONE) {
 
637
        if (norm_first_zone != first_zone) {
647
638
                printf(_("Warning: Firstzone != Norm_firstzone\n"));
648
639
                errors_uncorrected = 1;
649
640
        }
650
641
        get_dirsize ();
651
642
        if (show) {
652
 
                printf(_("%ld inodes\n"),INODES);
653
 
                printf(_("%ld blocks\n"),ZONES);
654
 
                printf(_("Firstdatazone=%ld (%ld)\n"),FIRSTZONE,NORM_FIRSTZONE);
655
 
                printf(_("Zonesize=%d\n"),BLOCK_SIZE<<ZONESIZE);
656
 
                printf(_("Maxsize=%ld\n"),MAXSIZE);
 
643
                printf(_("%ld inodes\n"), inodes);
 
644
                printf(_("%ld blocks\n"), zones);
 
645
                printf(_("Firstdatazone=%ld (%ld)\n"), first_zone, norm_first_zone);
 
646
                printf(_("Zonesize=%d\n"),MINIX_BLOCK_SIZE<<get_zone_size());
 
647
                printf(_("Maxsize=%ld\n"), get_max_size());
657
648
                printf(_("Filesystem state=%d\n"), Super.s_state);
658
 
                printf(_("namelen=%d\n\n"),namelen);
 
649
                printf(_("namelen=%zd\n\n"),namelen);
659
650
        }
660
651
}
661
652
 
663
654
get_inode(unsigned int nr) {
664
655
        struct minix_inode * inode;
665
656
 
666
 
        if (!nr || nr > INODES)
 
657
        if (!nr || nr > get_ninodes())
667
658
                return NULL;
668
659
        total++;
669
660
        inode = Inode + nr;
714
705
get_inode2 (unsigned int nr) {
715
706
        struct minix2_inode *inode;
716
707
 
717
 
        if (!nr || nr > INODES)
 
708
        if (!nr || nr > get_ninodes())
718
709
                return NULL;
719
710
        total++;
720
711
        inode = Inode2 + nr;
776
767
 
777
768
static int
778
769
add_zone(unsigned short * znr, int * corrected) {
779
 
        int result;
780
770
        int block;
781
771
 
782
 
        result = 0;
783
772
        block = check_zone_nr(znr, corrected);
784
773
        if (!block)
785
774
                return 0;
809
798
 
810
799
static int
811
800
add_zone2 (unsigned int *znr, int *corrected) {
812
 
        int result;
813
801
        int block;
814
802
 
815
 
        result = 0;
816
803
        block = check_zone_nr2 (znr, corrected);
817
804
        if (!block)
818
805
                return 0;
842
829
 
843
830
static void
844
831
add_zone_ind(unsigned short * znr, int * corrected) {
845
 
        static char blk[BLOCK_SIZE];
 
832
        static char blk[MINIX_BLOCK_SIZE];
846
833
        int i, chg_blk=0;
847
834
        int block;
848
835
 
850
837
        if (!block)
851
838
                return;
852
839
        read_block(block, blk);
853
 
        for (i=0 ; i < (BLOCK_SIZE>>1) ; i++)
 
840
        for (i=0 ; i < (MINIX_BLOCK_SIZE>>1) ; i++)
854
841
                add_zone(i + (unsigned short *) blk, &chg_blk);
855
842
        if (chg_blk)
856
843
                write_block(block, blk);
858
845
 
859
846
static void
860
847
add_zone_ind2 (unsigned int *znr, int *corrected) {
861
 
        static char blk[BLOCK_SIZE];
 
848
        static char blk[MINIX_BLOCK_SIZE];
862
849
        int i, chg_blk = 0;
863
850
        int block;
864
851
 
866
853
        if (!block)
867
854
                return;
868
855
        read_block (block, blk);
869
 
        for (i = 0; i < BLOCK_SIZE >> 2; i++)
 
856
        for (i = 0; i < MINIX_BLOCK_SIZE >> 2; i++)
870
857
                add_zone2 (i + (unsigned int *) blk, &chg_blk);
871
858
        if (chg_blk)
872
859
                write_block (block, blk);
874
861
 
875
862
static void
876
863
add_zone_dind(unsigned short * znr, int * corrected) {
877
 
        static char blk[BLOCK_SIZE];
 
864
        static char blk[MINIX_BLOCK_SIZE];
878
865
        int i, blk_chg=0;
879
866
        int block;
880
867
 
882
869
        if (!block)
883
870
                return;
884
871
        read_block(block, blk);
885
 
        for (i=0 ; i < (BLOCK_SIZE>>1) ; i++)
 
872
        for (i=0 ; i < (MINIX_BLOCK_SIZE>>1) ; i++)
886
873
                add_zone_ind(i + (unsigned short *) blk, &blk_chg);
887
874
        if (blk_chg)
888
875
                write_block(block, blk);
890
877
 
891
878
static void
892
879
add_zone_dind2 (unsigned int *znr, int *corrected) {
893
 
        static char blk[BLOCK_SIZE];
 
880
        static char blk[MINIX_BLOCK_SIZE];
894
881
        int i, blk_chg = 0;
895
882
        int block;
896
883
 
898
885
        if (!block)
899
886
                return;
900
887
        read_block (block, blk);
901
 
        for (i = 0; i < BLOCK_SIZE >> 2; i++)
 
888
        for (i = 0; i < MINIX_BLOCK_SIZE >> 2; i++)
902
889
                add_zone_ind2 (i + (unsigned int *) blk, &blk_chg);
903
890
        if (blk_chg)
904
891
                write_block (block, blk);
906
893
 
907
894
static void
908
895
add_zone_tind2 (unsigned int *znr, int *corrected) {
909
 
        static char blk[BLOCK_SIZE];
 
896
        static char blk[MINIX_BLOCK_SIZE];
910
897
        int i, blk_chg = 0;
911
898
        int block;
912
899
 
914
901
        if (!block)
915
902
                return;
916
903
        read_block (block, blk);
917
 
        for (i = 0; i < BLOCK_SIZE >> 2; i++)
 
904
        for (i = 0; i < MINIX_BLOCK_SIZE >> 2; i++)
918
905
                add_zone_dind2 (i + (unsigned int *) blk, &blk_chg);
919
906
        if (blk_chg)
920
907
                write_block (block, blk);
922
909
 
923
910
static void
924
911
check_zones(unsigned int i) {
925
 
        struct minix_inode * inode;
 
912
        struct minix_inode *inode;
926
913
 
927
 
        if (!i || i > INODES)
 
914
        if (!i || i > get_ninodes())
928
915
                return;
929
916
        if (inode_count[i] > 1) /* have we counted this file already? */
930
917
                return;
942
929
check_zones2 (unsigned int i) {
943
930
        struct minix2_inode *inode;
944
931
 
945
 
        if (!i || i > INODES)
 
932
        if (!i || i > get_ninodes())
946
933
                return;
947
934
        if (inode_count[i] > 1) /* have we counted this file already? */
948
935
                return;
959
946
 
960
947
static void
961
948
check_file(struct minix_inode * dir, unsigned int offset) {
962
 
        static char blk[BLOCK_SIZE];
 
949
        static char blk[MINIX_BLOCK_SIZE];
963
950
        struct minix_inode * inode;
964
 
        int ino;
 
951
        unsigned int ino;
965
952
        char * name;
966
953
        int block;
967
954
 
968
 
        block = map_block(dir,offset/BLOCK_SIZE);
 
955
        block = map_block(dir,offset/MINIX_BLOCK_SIZE);
969
956
        read_block(block, blk);
970
 
        name = blk + (offset % BLOCK_SIZE) + 2;
 
957
        name = blk + (offset % MINIX_BLOCK_SIZE) + 2;
971
958
        ino = * (unsigned short *) (name-2);
972
 
        if (ino > INODES) {
 
959
        if (ino > get_ninodes()) {
973
960
                get_current_name();
974
961
                printf(_("The directory '%s' contains a bad inode number "
975
962
                         "for file '%.*s'."),
976
 
                       current_name, namelen, name);
 
963
                       current_name, (int) namelen, name);
977
964
                if (ask(_(" Remove"),1)) {
978
965
                        *(unsigned short *)(name-2) = 0;
979
966
                        write_block(block, blk);
1026
1013
 
1027
1014
static void
1028
1015
check_file2 (struct minix2_inode *dir, unsigned int offset) {
1029
 
        static char blk[BLOCK_SIZE];
 
1016
        static char blk[MINIX_BLOCK_SIZE];
1030
1017
        struct minix2_inode *inode;
1031
 
        int ino;
 
1018
        unsigned long ino;
1032
1019
        char *name;
1033
1020
        int block;
1034
1021
 
1035
 
        block = map_block2 (dir, offset / BLOCK_SIZE);
 
1022
        block = map_block2 (dir, offset / MINIX_BLOCK_SIZE);
1036
1023
        read_block (block, blk);
1037
 
        name = blk + (offset % BLOCK_SIZE) + 2;
 
1024
        name = blk + (offset % MINIX_BLOCK_SIZE) + 2;
1038
1025
        ino = *(unsigned short *) (name - 2);
1039
 
        if (ino > INODES) {
 
1026
        if (ino > get_ninodes()) {
1040
1027
                get_current_name();
1041
1028
                printf(_("The directory '%s' contains a bad inode number "
1042
1029
                         "for file '%.*s'."),
1043
 
                          current_name, namelen, name);
 
1030
                          current_name, (int) namelen, name);
1044
1031
                if (ask (_(" Remove"), 1)) {
1045
1032
                        *(unsigned short *) (name - 2) = 0;
1046
1033
                        write_block (block, blk);
1075
1062
        name_depth++;
1076
1063
        if (list) {
1077
1064
                if (verbose)
1078
 
                        printf ("%6d %07o %3d ", ino, inode->i_mode,
 
1065
                        printf ("%6zd %07o %3d ", ino, inode->i_mode,
1079
1066
                                inode->i_nlinks);
1080
1067
                get_current_name ();
1081
1068
                printf("%s", current_name);
1131
1118
bad_zone(int i) {
1132
1119
        char buffer[1024];
1133
1120
 
1134
 
        if (BLOCK_SIZE*i != lseek(IN, BLOCK_SIZE*i, SEEK_SET))
 
1121
        if (MINIX_BLOCK_SIZE*i != lseek(IN, MINIX_BLOCK_SIZE*i, SEEK_SET))
1135
1122
                die(_("seek failed in bad_zone"));
1136
 
        return (BLOCK_SIZE != read(IN, buffer, BLOCK_SIZE));
 
1123
        return (MINIX_BLOCK_SIZE != read(IN, buffer, MINIX_BLOCK_SIZE));
1137
1124
}
1138
1125
 
1139
1126
static void
1140
1127
check_counts(void) {
1141
 
        int i;
 
1128
        unsigned long i;
1142
1129
 
1143
 
        for (i=1 ; i <= INODES ; i++) {
 
1130
        for (i=1 ; i <= get_ninodes(); i++) {
1144
1131
                if (!inode_in_use(i) && Inode[i].i_mode && warn_mode) {
1145
 
                        printf(_("Inode %d mode not cleared."),i);
 
1132
                        printf(_("Inode %lu mode not cleared."), i);
1146
1133
                        if (ask(_("Clear"),1)) {
1147
1134
                                Inode[i].i_mode = 0;
1148
1135
                                changed = 1;
1151
1138
                if (!inode_count[i]) {
1152
1139
                        if (!inode_in_use(i))
1153
1140
                                continue;
1154
 
                        printf(_("Inode %d not used, marked used in the bitmap."),i);
 
1141
                        printf(_("Inode %lu not used, marked used in the bitmap."),
 
1142
                                i);
1155
1143
                        if (ask(_("Clear"),1))
1156
1144
                                unmark_inode(i);
1157
1145
                        continue;
1158
1146
                }
1159
1147
                if (!inode_in_use(i)) {
1160
 
                        printf(_("Inode %d used, marked unused in the bitmap."),
 
1148
                        printf(_("Inode %lu used, marked unused in the bitmap."),
1161
1149
                                i);
1162
1150
                        if (ask(_("Set"),1))
1163
1151
                                mark_inode(i);
1164
1152
                }
1165
1153
                if (Inode[i].i_nlinks != inode_count[i]) {
1166
 
                        printf(_("Inode %d (mode = %07o), i_nlinks=%d, counted=%d."),
 
1154
                        printf(_("Inode %lu (mode = %07o), i_nlinks=%d, counted=%d."),
1167
1155
                                i,Inode[i].i_mode,Inode[i].i_nlinks,inode_count[i]);
1168
1156
                        if (ask(_("Set i_nlinks to count"),1)) {
1169
1157
                                Inode[i].i_nlinks=inode_count[i];
1171
1159
                        }
1172
1160
                }
1173
1161
        }
1174
 
        for (i=FIRSTZONE ; i < ZONES ; i++) {
 
1162
        for (i=get_first_zone() ; i < get_nzones() ; i++) {
1175
1163
                if (zone_in_use(i) == zone_count[i])
1176
1164
                        continue;
1177
1165
                if (!zone_count[i]) {
1178
1166
                        if (bad_zone(i))
1179
1167
                                continue;
1180
 
                        printf(_("Zone %d: marked in use, no file uses it."),i);
 
1168
                        printf(_("Zone %lu: marked in use, no file uses it."),i);
1181
1169
                        if (ask(_("Unmark"),1))
1182
1170
                                unmark_zone(i);
1183
1171
                        continue;
1184
1172
                }
1185
1173
                if (zone_in_use(i))
1186
 
                        printf(_("Zone %d: in use, counted=%d\n"),
 
1174
                        printf(_("Zone %lu: in use, counted=%d\n"),
1187
1175
                               i, zone_count[i]);
1188
1176
                else
1189
 
                        printf(_("Zone %d: not in use, counted=%d\n"),
 
1177
                        printf(_("Zone %lu: not in use, counted=%d\n"),
1190
1178
                               i, zone_count[i]);
1191
1179
        }
1192
1180
}
1193
1181
 
1194
1182
static void
1195
1183
check_counts2 (void) {
1196
 
        int i;
 
1184
        unsigned long i;
1197
1185
 
1198
 
        for (i = 1; i <= INODES; i++) {
 
1186
        for (i = 1; i <= get_ninodes(); i++) {
1199
1187
                if (!inode_in_use (i) && Inode2[i].i_mode && warn_mode) {
1200
 
                        printf (_("Inode %d mode not cleared."), i);
 
1188
                        printf (_("Inode %lu mode not cleared."), i);
1201
1189
                        if (ask (_("Clear"), 1)) {
1202
1190
                                Inode2[i].i_mode = 0;
1203
1191
                                changed = 1;
1206
1194
                if (!inode_count[i]) {
1207
1195
                        if (!inode_in_use (i))
1208
1196
                                continue;
1209
 
                        printf (_("Inode %d not used, marked used in the bitmap."), i);
 
1197
                        printf (_("Inode %lu not used, marked used in the bitmap."), i);
1210
1198
                        if (ask (_("Clear"), 1))
1211
1199
                                unmark_inode (i);
1212
1200
                        continue;
1213
1201
                }
1214
1202
                if (!inode_in_use (i)) {
1215
 
                        printf (_("Inode %d used, marked unused in the bitmap."), i);
 
1203
                        printf (_("Inode %lu used, marked unused in the bitmap."), i);
1216
1204
                        if (ask (_("Set"), 1))
1217
1205
                                mark_inode (i);
1218
1206
                }
1219
1207
                if (Inode2[i].i_nlinks != inode_count[i]) {
1220
 
                        printf (_("Inode %d (mode = %07o), i_nlinks=%d, counted=%d."),
 
1208
                        printf (_("Inode %lu (mode = %07o), i_nlinks=%d, counted=%d."),
1221
1209
                                i, Inode2[i].i_mode, Inode2[i].i_nlinks, inode_count[i]);
1222
1210
                        if (ask (_("Set i_nlinks to count"), 1)) {
1223
1211
                                Inode2[i].i_nlinks = inode_count[i];
1225
1213
                        }
1226
1214
                }
1227
1215
        }
1228
 
        for (i = FIRSTZONE; i < ZONES; i++) {
 
1216
        for (i = get_first_zone(); i < get_nzones(); i++) {
1229
1217
                if (zone_in_use (i) == zone_count[i])
1230
1218
                        continue;
1231
1219
                if (!zone_count[i]) {
1232
1220
                        if (bad_zone (i))
1233
1221
                                continue;
1234
 
                        printf (_("Zone %d: marked in use, no file uses it."),
 
1222
                        printf (_("Zone %lu: marked in use, no file uses it."),
1235
1223
                                i);
1236
1224
                        if (ask (_("Unmark"), 1))
1237
1225
                                unmark_zone (i);
1238
1226
                        continue;
1239
1227
                }
1240
1228
                if (zone_in_use (i))
1241
 
                        printf (_("Zone %d: in use, counted=%d\n"),
 
1229
                        printf (_("Zone %lu: in use, counted=%d\n"),
1242
1230
                                i, zone_count[i]);
1243
1231
                else
1244
 
                        printf (_("Zone %d: not in use, counted=%d\n"),
 
1232
                        printf (_("Zone %lu: not in use, counted=%d\n"),
1245
1233
                                i, zone_count[i]);
1246
1234
        }
1247
1235
}
1248
1236
 
1249
1237
static void
1250
1238
check(void) {
1251
 
        memset(inode_count,0,(INODES + 1) * sizeof(*inode_count));
1252
 
        memset(zone_count,0,ZONES*sizeof(*zone_count));
 
1239
        memset(inode_count,0,(get_ninodes() + 1) * sizeof(*inode_count));
 
1240
        memset(zone_count,0, get_nzones()*sizeof(*zone_count));
1253
1241
        check_zones(ROOT_INO);
1254
1242
        recursive_check(ROOT_INO);
1255
1243
        check_counts();
1257
1245
 
1258
1246
static void
1259
1247
check2 (void) {
1260
 
        memset (inode_count, 0, (INODES + 1) * sizeof (*inode_count));
1261
 
        memset (zone_count, 0, ZONES * sizeof (*zone_count));
 
1248
        memset (inode_count, 0, (get_ninodes() + 1) * sizeof (*inode_count));
 
1249
        memset (zone_count, 0, get_nzones() * sizeof (*zone_count));
1262
1250
        check_zones2 (ROOT_INO);
1263
1251
        recursive_check2 (ROOT_INO);
1264
1252
        check_counts2 ();
1285
1273
                exit(0);
1286
1274
        }
1287
1275
 
1288
 
        if (INODE_SIZE * MINIX_INODES_PER_BLOCK != BLOCK_SIZE)
 
1276
        if (INODE_SIZE * MINIX_INODES_PER_BLOCK != MINIX_BLOCK_SIZE)
1289
1277
                die(_("bad inode size"));
1290
 
        if (INODE_SIZE2 * MINIX2_INODES_PER_BLOCK != BLOCK_SIZE)
 
1278
        if (INODE2_SIZE * MINIX2_INODES_PER_BLOCK != MINIX_BLOCK_SIZE)
1291
1279
                die(_("bad v2 inode size"));
1292
1280
 
1293
1281
        while (argc-- > 1) {
1359
1347
                termios_set = 1;
1360
1348
        }
1361
1349
 
1362
 
        if (version2) {
 
1350
        if (fs_version == 2) {
1363
1351
                check_root2 ();
1364
1352
                check2 ();
1365
1353
        } else {
1367
1355
                check();
1368
1356
        }
1369
1357
        if (verbose) {
1370
 
                int i, free;
 
1358
                unsigned long i, free;
1371
1359
 
1372
 
                for (i=1,free=0 ; i <= INODES ; i++)
 
1360
                for (i=1,free=0 ; i <= get_ninodes() ; i++)
1373
1361
                        if (!inode_in_use(i))
1374
1362
                                free++;
1375
 
                printf(_("\n%6ld inodes used (%ld%%)\n"),(INODES-free),
1376
 
                        100*(INODES-free)/INODES);
1377
 
                for (i=FIRSTZONE,free=0 ; i < ZONES ; i++)
 
1363
                printf(_("\n%6ld inodes used (%ld%%)\n"),(get_ninodes()-free),
 
1364
                       100*(get_ninodes()-free)/get_ninodes());
 
1365
                for (i=get_first_zone(),free=0 ; i < get_nzones(); i++)
1378
1366
                        if (!zone_in_use(i))
1379
1367
                                free++;
1380
 
                printf(_("%6ld zones used (%ld%%)\n"),(ZONES-free),
1381
 
                        100*(ZONES-free)/ZONES);
 
1368
                printf(_("%6ld zones used (%ld%%)\n"),(get_nzones()-free),
 
1369
                       100*(get_nzones()-free)/get_nzones());
1382
1370
                printf(_("\n%6d regular files\n"
1383
1371
                "%6d directories\n"
1384
1372
                "%6d character device files\n"