102
102
#include <sys/stat.h>
103
103
#include <signal.h>
105
#include "minix_programs.h"
107
107
#include "pathnames.h"
108
108
#include "bitops.h"
109
#include "ismounted.h"
110
#include "writeall.h"
110
112
#define ROOT_INO 1
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)
119
#define BITS_PER_BLOCK (BLOCK_SIZE<<3)
121
static char * program_name = "fsck.minix";
122
static char * device_name = NULL;
115
* Global variables used in minix_programs.h inline fuctions
118
char *super_block_buffer;
121
static char *inode_buffer;
123
#define Inode (((struct minix_inode *) inode_buffer) - 1)
124
#define Inode2 (((struct minix2_inode *) inode_buffer) - 1)
126
static char *program_name = "fsck.minix";
127
static char *device_name;
124
static int repair=0, automatic=0, verbose=0, list=0, show=0, warn_mode=0,
126
static int directory=0, regular=0, blockdev=0, chardev=0, links=0,
129
static int repair, automatic, verbose, list, show, warn_mode, force;
130
static int directory, regular, blockdev, chardev, links, symlinks, total;
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;
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];
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];
145
static char * inode_buffer = NULL;
146
#define Inode (((struct minix_inode *) inode_buffer)-1)
147
#define Inode2 (((struct minix2_inode *) inode_buffer)-1)
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];
158
148
#define MAGIC (Super.s_magic)
159
#define NORM_FIRSTZONE (2+IMAPS+ZMAPS+INODE_BLOCKS)
150
static unsigned char *inode_count = NULL;
151
static unsigned char *zone_count = NULL;
153
static void recursive_check(unsigned int ino);
154
static void recursive_check2(unsigned int ino);
161
156
static char *inode_map;
162
157
static char *zone_map;
164
static unsigned char * inode_count = NULL;
165
static unsigned char * zone_count = NULL;
167
static void recursive_check(unsigned int ino);
168
static void recursive_check2(unsigned int ino);
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)
173
162
#define mark_inode(x) (setbit(inode_map,(x)),changed=1)
174
163
#define unmark_inode(x) (clrbit(inode_map,(x)),changed=1)
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)
391
361
read_block(unsigned int nr, char * addr) {
393
memset(addr,0,BLOCK_SIZE);
363
memset(addr,0,MINIX_BLOCK_SIZE);
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"),
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"),
406
memset(addr,0,BLOCK_SIZE);
376
memset(addr,0,MINIX_BLOCK_SIZE);
407
377
errors_uncorrected = 1;
415
385
write_block(unsigned int nr, char * addr) {
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;
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"),
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();
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))
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))
534
if (write_all(IN, inode_buffer, buffsz))
560
535
die(_("Unable to write inodes"));
564
539
get_dirsize (void) {
566
char blk[BLOCK_SIZE];
541
char blk[MINIX_BLOCK_SIZE];
570
545
block = Inode2[ROOT_INO].i_zone[0];
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) {
550
for (size = 16; size < MINIX_BLOCK_SIZE; size <<= 1) {
551
if (strcmp(blk + size + 2, "..") == 0) {
577
553
namelen = size - 2;
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"));
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"));
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) {
599
575
} else if (MAGIC == MINIX_SUPER_MAGIC2) {
603
579
} else if (MAGIC == MINIX2_SUPER_MAGIC) {
607
583
} else if (MAGIC == MINIX2_SUPER_MAGIC2) {
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"));
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();
608
inode_map = malloc(imaps * MINIX_BLOCK_SIZE);
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);
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);
639
624
die(_("Unable to allocate buffer for zone count"));
640
if (IMAPS*BLOCK_SIZE != read(IN,inode_map,IMAPS*BLOCK_SIZE))
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))
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))
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;
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);
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;
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);
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);
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;
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);
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);
1151
1138
if (!inode_count[i]) {
1152
1139
if (!inode_in_use(i))
1154
printf(_("Inode %d not used, marked used in the bitmap."),i);
1141
printf(_("Inode %lu not used, marked used in the bitmap."),
1155
1143
if (ask(_("Clear"),1))
1156
1144
unmark_inode(i);
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."),
1162
1150
if (ask(_("Set"),1))
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];
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])
1177
1165
if (!zone_count[i]) {
1178
1166
if (bad_zone(i))
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);
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]);
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]);
1195
1183
check_counts2 (void) {
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;
1206
1194
if (!inode_count[i]) {
1207
1195
if (!inode_in_use (i))
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);
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);
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];
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])
1231
1219
if (!zone_count[i]) {
1232
1220
if (bad_zone (i))
1234
printf (_("Zone %d: marked in use, no file uses it."),
1222
printf (_("Zone %lu: marked in use, no file uses it."),
1236
1224
if (ask (_("Unmark"), 1))
1237
1225
unmark_zone (i);
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]);
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]);
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();
1358
unsigned long i, free;
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))
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))
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"