unstack, sort: cleanup and improvement
[minix.git] / commands / fsck.mfs / fsck.c
blob63513eb25551898bb5e8ff0c736770d19fb11960
1 /* Hacks for version 1.6 */
3 #define INODES_PER_BLOCK V2_INODES_PER_BLOCK(block_size)
4 #define INODE_SIZE ((int) V2_INODE_SIZE)
5 #define WORDS_PER_BLOCK (block_size / (int) sizeof(bitchunk_t))
6 #define MAX_ZONES (V2_NR_DZONES+V2_INDIRECTS(block_size)+(long)V2_INDIRECTS(block_size)*V2_INDIRECTS(block_size))
7 #define NR_DZONE_NUM V2_NR_DZONES
8 #define NR_INDIRECTS V2_INDIRECTS(block_size)
9 #define NR_ZONE_NUMS V2_NR_TZONES
10 #define ZONE_NUM_SIZE V2_ZONE_NUM_SIZE
11 #define bit_nr bit_t
12 #define block_nr block_t
13 #define d_inode d2_inode
14 #define d_inum mfs_d_ino
15 #define dir_struct struct direct
16 #define i_mode d2_mode
17 #define i_nlinks d2_nlinks
18 #define i_size d2_size
19 #define i_zone d2_zone
20 #define zone_nr zone_t
22 /* fsck - file system checker Author: Robbert van Renesse */
24 /* Modified by Norbert Schlenker
25 * Removed vestiges of standalone/DOS versions:
26 * - various unused variables and buffers removed
27 * - now uses library functions rather than private internal routines
28 * - bytewise structure copies replaced by structure assignment
29 * - fixed one bug with 14 character file names
30 * - other small tweaks for speed
32 * Modified by Lars Fredriksen at the request of Andy Tanenbaum, 90-03-10.
33 * Removed -m option, by which fsck could be told to make a file
34 * system on a 360K floppy. The code had limited utility, was buggy,
35 * and failed due to a bug in the ACK C compiler. Use mkfs instead!
38 #include <sys/types.h>
39 #include <ctype.h>
40 #include <errno.h>
41 #include <fcntl.h>
42 #include <limits.h>
43 #include <stdlib.h>
44 #include <string.h>
45 #include <unistd.h>
46 #include <minix/config.h>
47 #include <minix/const.h>
48 #include <minix/type.h>
49 #include <minix/u64.h>
50 #include "mfs/const.h"
51 #include "mfs/inode.h"
52 #include "mfs/type.h"
53 #include "mfs/mfsdir.h"
54 #include <minix/fslib.h>
55 #include <stdio.h>
56 #include <sys/stat.h>
57 #include <a.out.h>
58 #include <dirent.h>
60 #include "exitvalues.h"
62 #undef N_DATA
64 unsigned int fs_version = 2, block_size = 0;
66 #define BITSHIFT 5 /* = log2(#bits(int)) */
68 #define MAXPRINT 80 /* max. number of error lines in chkmap */
69 #define CINDIR 128 /* number of indirect zno's read at a time */
70 #define CDIRECT 1 /* number of dir entries read at a time */
72 /* Macros for handling bitmaps. Now bit_t is long, these are bulky and the
73 * type demotions produce a lot of lint. The explicit demotion in POWEROFBIT
74 * is for efficiency and assumes 2's complement ints. Lint should be clever
75 * enough not to warn about it since BITMASK is small, but isn't. (It would
76 * be easier to get right if bit_t was was unsigned (long) since then there
77 * would be no danger from wierd sign representations. Lint doesn't know
78 * we only use non-negative bit numbers.) There will usually be an implicit
79 * demotion when WORDOFBIT is used as an array index. This should be safe
80 * since memory for bitmaps will run out first.
82 #define BITMASK ((1 << BITSHIFT) - 1)
83 #define WORDOFBIT(b) ((b) >> BITSHIFT)
84 #define POWEROFBIT(b) (1 << ((int) (b) & BITMASK))
85 #define setbit(w, b) (w[WORDOFBIT(b)] |= POWEROFBIT(b))
86 #define clrbit(w, b) (w[WORDOFBIT(b)] &= ~POWEROFBIT(b))
87 #define bitset(w, b) (w[WORDOFBIT(b)] & POWEROFBIT(b))
89 #define ZONE_CT 360 /* default zones (when making file system) */
90 #define INODE_CT 95 /* default inodes (when making file system) */
92 #include "mfs/super.h"
93 static struct super_block sb;
95 #define STICKY_BIT 01000 /* not defined anywhere else */
97 /* Ztob gives the block address of a zone
98 * btoa64 gives the byte address of a block
100 #define ztob(z) ((block_nr) (z) << sb.s_log_zone_size)
101 #define btoa64(b) (mul64u(b, block_size))
102 #define SCALE ((int) ztob(1)) /* # blocks in a zone */
103 #define FIRST ((zone_nr) sb.s_firstdatazone) /* as the name says */
105 /* # blocks of each type */
106 #define N_IMAP (sb.s_imap_blocks)
107 #define N_ZMAP (sb.s_zmap_blocks)
108 #define N_ILIST ((sb.s_ninodes+INODES_PER_BLOCK-1) / INODES_PER_BLOCK)
109 #define N_DATA (sb.s_zones - FIRST)
111 /* Block address of each type */
112 #define OFFSET_SUPER_BLOCK SUPER_BLOCK_BYTES
113 #define BLK_IMAP 2
114 #define BLK_ZMAP (BLK_IMAP + N_IMAP)
115 #define BLK_ILIST (BLK_ZMAP + N_ZMAP)
116 #define BLK_FIRST ztob(FIRST)
117 #define ZONE_SIZE ((int) ztob(block_size))
118 #define NLEVEL (NR_ZONE_NUMS - NR_DZONE_NUM + 1)
120 /* Byte address of a zone */
121 #define INDCHUNK ((int) (CINDIR * ZONE_NUM_SIZE))
122 #define DIRCHUNK ((int) (CDIRECT * DIR_ENTRY_SIZE))
124 char *prog, *fsck_device; /* program name (fsck), device name */
125 int firstcnterr; /* is this the first inode ref cnt error? */
126 bitchunk_t *imap, *spec_imap; /* inode bit maps */
127 bitchunk_t *zmap, *spec_zmap; /* zone bit maps */
128 bitchunk_t *dirmap; /* directory (inode) bit map */
129 char *rwbuf; /* one block buffer cache */
130 block_nr thisblk; /* block in buffer cache */
131 char *nullbuf; /* null buffer */
132 nlink_t *count; /* inode count */
133 int changed; /* has the diskette been written to? */
134 struct stack {
135 dir_struct *st_dir;
136 struct stack *st_next;
137 char st_presence;
138 } *ftop;
140 int dev; /* file descriptor of the device */
142 #define DOT 1
143 #define DOTDOT 2
145 /* Counters for each type of inode/zone. */
146 int nfreeinode, nregular, ndirectory, nblkspec, ncharspec, nbadinode;
147 int nsock, npipe, nsyml, ztype[NLEVEL];
148 long nfreezone;
150 int repair, notrepaired = 0, automatic, listing, listsuper; /* flags */
151 int preen = 0, markdirty = 0;
152 int firstlist; /* has the listing header been printed? */
153 unsigned part_offset; /* sector offset for this partition */
154 char answer[] = "Answer questions with y or n. Then hit RETURN";
156 int main(int argc, char **argv);
157 void initvars(void);
158 void fatal(char *s);
159 int eoln(int c);
160 int yes(char *question);
161 int atoo(char *s);
162 int input(char *buf, int size);
163 char *alloc(unsigned nelem, unsigned elsize);
164 void printname(char *s);
165 void printrec(struct stack *sp);
166 void printpath(int mode, int nlcr);
167 void devopen(void);
168 void devclose(void);
169 void devio(block_nr bno, int dir);
170 void devread(long block, long offset, char *buf, int size);
171 void devwrite(long block, long offset, char *buf, int size);
172 void pr(char *fmt, int cnt, char *s, char *p);
173 void lpr(char *fmt, long cnt, char *s, char *p);
174 bit_nr getnumber(char *s);
175 char **getlist(char ***argv, char *type);
176 void lsuper(void);
177 #define SUPER_GET 0
178 #define SUPER_PUT 1
179 void rw_super(int mode);
180 void chksuper(void);
181 void lsi(char **clist);
182 bitchunk_t *allocbitmap(int nblk);
183 void loadbitmap(bitchunk_t *bitmap, block_nr bno, int nblk);
184 void dumpbitmap(bitchunk_t *bitmap, block_nr bno, int nblk);
185 void fillbitmap(bitchunk_t *bitmap, bit_nr lwb, bit_nr upb, char
186 **list);
187 void freebitmap(bitchunk_t *p);
188 void getbitmaps(void);
189 void putbitmaps(void);
190 void chkword(unsigned w1, unsigned w2, bit_nr bit, char *type, int *n,
191 int *report, bit_t);
192 void chkmap(bitchunk_t *cmap, bitchunk_t *dmap, bit_nr bit, block_nr
193 blkno, int nblk, char *type);
194 void chkilist(void);
195 void getcount(void);
196 void counterror(ino_t ino);
197 void chkcount(void);
198 void freecount(void);
199 void printperm(mode_t mode, int shift, int special, int overlay);
200 void list(ino_t ino, d_inode *ip);
201 int Remove(dir_struct *dp);
202 void make_printable_name(char *dst, char *src, int n);
203 int chkdots(ino_t ino, off_t pos, dir_struct *dp, ino_t exp);
204 int chkname(ino_t ino, dir_struct *dp);
205 int chkentry(ino_t ino, off_t pos, dir_struct *dp);
206 int chkdirzone(ino_t ino, d_inode *ip, off_t pos, zone_nr zno);
207 int chksymlinkzone(ino_t ino, d_inode *ip, off_t pos, zone_nr zno);
208 void errzone(char *mess, zone_nr zno, int level, off_t pos);
209 int markzone(zone_nr zno, int level, off_t pos);
210 int chkindzone(ino_t ino, d_inode *ip, off_t *pos, zone_nr zno, int
211 level);
212 off_t jump(int level);
213 int zonechk(ino_t ino, d_inode *ip, off_t *pos, zone_nr zno, int level);
214 int chkzones(ino_t ino, d_inode *ip, off_t *pos, zone_nr *zlist, int
215 len, int level);
216 int chkfile(ino_t ino, d_inode *ip);
217 int chkdirectory(ino_t ino, d_inode *ip);
218 int chklink(ino_t ino, d_inode *ip);
219 int chkspecial(ino_t ino, d_inode *ip);
220 int chkmode(ino_t ino, d_inode *ip);
221 int chkinode(ino_t ino, d_inode *ip);
222 int descendtree(dir_struct *dp);
223 void chktree(void);
224 void printtotal(void);
225 void chkdev(char *f, char **clist, char **ilist, char **zlist);
227 /* Initialize the variables used by this program. */
228 void initvars()
230 register level;
232 nregular = ndirectory = nblkspec = ncharspec =
233 nbadinode = nsock = npipe = nsyml = 0;
234 for (level = 0; level < NLEVEL; level++) ztype[level] = 0;
235 changed = 0;
236 thisblk = NO_BLOCK;
237 firstlist = 1;
238 firstcnterr = 1;
241 /* Print the string `s' and exit. */
242 void fatal(s)
243 char *s;
245 printf("%s\nfatal\n", s);
246 exit(FSCK_EXIT_CHECK_FAILED);
249 /* Test for end of line. */
250 int eoln(c)
251 int c;
253 return(c == EOF || c == '\n' || c == '\r');
256 /* Ask a question and get the answer unless automatic is set. */
257 int yes(question)
258 char *question;
260 register int c, answerchar;
261 static int note = 0;
262 int yes;
264 if (!repair) {
265 printf("\n");
266 return(0);
268 printf("%s? ", question);
269 if(!note) { printf("(y=yes, n=no, q=quit, A=for yes to all) "); note = 1; }
270 if (automatic) {
271 printf("yes\n");
272 return(1);
274 fflush(stdout);
275 if ((c = answerchar = getchar()) == 'q' || c == 'Q') exit(FSCK_EXIT_CHECK_FAILED);
276 if(c == 'A') { automatic = 1; c = 'y'; }
277 while (!eoln(c)) c = getchar();
278 yes = !(answerchar == 'n' || answerchar == 'N');
279 if(!yes) notrepaired = 1;
280 return yes;
283 /* Convert string to integer. Representation is octal. */
284 int atoo(s)
285 register char *s;
287 register int n = 0;
289 while ('0' <= *s && *s < '8') {
290 n <<= 3;
291 n += *s++ - '0';
293 return n;
296 /* If repairing the file system, print a prompt and get a string from user. */
297 int input(buf, size)
298 char *buf;
299 int size;
301 register char *p = buf;
303 printf("\n");
304 if (repair) {
305 printf("--> ");
306 fflush(stdout);
307 while (--size) {
308 *p = getchar();
309 if (eoln(*p)) {
310 *p = 0;
311 return(p > buf);
313 p++;
315 *p = 0;
316 while (!eoln(getchar()));
317 return(1);
319 return(0);
322 /* Allocate some memory and zero it. */
323 char *alloc(nelem, elsize)
324 unsigned nelem, elsize;
326 char *p;
328 if ((p = (char *)malloc((size_t)nelem * elsize)) == 0) {
329 fprintf(stderr, "Tried to allocate %dkB\n",
330 nelem*elsize/1024);
331 fatal("out of memory");
333 memset((void *) p, 0, (size_t)nelem * elsize);
334 return(p);
337 /* Print the name in a directory entry. */
338 void printname(s)
339 char *s;
341 register n = MFS_NAME_MAX;
342 int c;
344 do {
345 if ((c = *s) == 0) break;
346 if (!isprint(c)) c = '?';
347 putchar(c);
348 s++;
349 } while (--n);
352 /* Print the pathname given by a linked list pointed to by `sp'. The
353 * names are in reverse order.
355 void printrec(struct stack *sp)
357 if (sp->st_next != 0) {
358 printrec(sp->st_next);
359 putchar('/');
360 printname(sp->st_dir->mfs_d_name);
364 /* Print the current pathname. */
365 void printpath(mode, nlcr)
366 int mode;
367 int nlcr;
369 if (ftop->st_next == 0)
370 putchar('/');
371 else
372 printrec(ftop);
373 switch (mode) {
374 case 1:
375 printf(" (ino = %u, ", ftop->st_dir->d_inum);
376 break;
377 case 2:
378 printf(" (ino = %u)", ftop->st_dir->d_inum);
379 break;
381 if (nlcr) printf("\n");
384 /* Open the device. */
385 void devopen()
387 if ((dev = open(fsck_device,
388 (repair || markdirty) ? O_RDWR : O_RDONLY)) < 0) {
389 perror(fsck_device);
390 fatal("couldn't open device to fsck");
394 /* Close the device. */
395 void devclose()
397 if (close(dev) != 0) {
398 perror("close");
399 fatal("");
403 /* Read or write a block. */
404 void devio(bno, dir)
405 block_nr bno;
406 int dir;
408 int r;
410 if(!block_size) fatal("devio() with unknown block size");
411 if (dir == READING && bno == thisblk) return;
412 thisblk = bno;
414 #if 0
415 printf("%s at block %5d\n", dir == READING ? "reading " : "writing", bno);
416 #endif
417 r= lseek64(dev, btoa64(bno), SEEK_SET, NULL);
418 if (r != 0)
419 fatal("lseek64 failed");
420 if (dir == READING) {
421 if (read(dev, rwbuf, block_size) == block_size)
422 return;
423 } else {
424 if (write(dev, rwbuf, block_size) == block_size)
425 return;
428 printf("%s: can't %s block %ld (error = 0x%x)\n", prog,
429 dir == READING ? "read" : "write", (long) bno, errno);
430 if (dir == READING) {
431 printf("Continuing with a zero-filled block.\n");
432 memset(rwbuf, 0, block_size);
433 return;
435 fatal("");
438 /* Read `size' bytes from the disk starting at block 'block' and
439 * byte `offset'.
441 void devread(block, offset, buf, size)
442 long block;
443 long offset;
444 char *buf;
445 int size;
447 if(!block_size) fatal("devread() with unknown block size");
448 if (offset >= block_size)
450 block += offset/block_size;
451 offset %= block_size;
453 devio(block, READING);
454 memmove(buf, &rwbuf[offset], size);
457 /* Write `size' bytes to the disk starting at block 'block' and
458 * byte `offset'.
460 void devwrite(block, offset, buf, size)
461 long block;
462 long offset;
463 char *buf;
464 int size;
466 if(!block_size) fatal("devwrite() with unknown block size");
467 if (!repair) fatal("internal error (devwrite)");
468 if (offset >= block_size)
470 block += offset/block_size;
471 offset %= block_size;
473 if (size != block_size) devio(block, READING);
474 memmove(&rwbuf[offset], buf, size);
475 devio(block, WRITING);
476 changed = 1;
479 /* Print a string with either a singular or a plural pronoun. */
480 void pr(fmt, cnt, s, p)
481 char *fmt, *s, *p;
482 int cnt;
484 printf(fmt, cnt, cnt == 1 ? s : p);
487 /* Same as above, but with a long argument */
488 void lpr(fmt, cnt, s, p)
489 char *fmt, *s, *p;
490 long cnt;
492 printf(fmt, cnt, cnt == 1 ? s : p);
495 /* Convert string to number. */
496 bit_nr getnumber(s)
497 register char *s;
499 register bit_nr n = 0;
501 if (s == NULL)
502 return NO_BIT;
503 while (isdigit(*s))
504 n = (n << 1) + (n << 3) + *s++ - '0';
505 return (*s == '\0') ? n : NO_BIT;
508 /* See if the list pointed to by `argv' contains numbers. */
509 char **getlist(argv, type)
510 char ***argv, *type;
512 register char **list = *argv;
513 register empty = 1;
515 while (getnumber(**argv) != NO_BIT) {
516 (*argv)++;
517 empty = 0;
519 if (empty) {
520 printf("warning: no %s numbers given\n", type);
521 return(NULL);
523 return(list);
526 /* Make a listing of the super block. If `repair' is set, ask the user
527 * for changes.
529 void lsuper()
531 char buf[80];
533 do {
534 /* Most of the following atol's enrage lint, for good reason. */
535 printf("ninodes = %u", sb.s_ninodes);
536 if (input(buf, 80)) sb.s_ninodes = atol(buf);
537 printf("nzones = %ld", sb.s_zones);
538 if (input(buf, 80)) sb.s_zones = atol(buf);
539 printf("imap_blocks = %u", sb.s_imap_blocks);
540 if (input(buf, 80)) sb.s_imap_blocks = atol(buf);
541 printf("zmap_blocks = %u", sb.s_zmap_blocks);
542 if (input(buf, 80)) sb.s_zmap_blocks = atol(buf);
543 printf("firstdatazone = %u", sb.s_firstdatazone_old);
544 if (input(buf, 80)) sb.s_firstdatazone_old = atol(buf);
545 printf("log_zone_size = %u", sb.s_log_zone_size);
546 if (input(buf, 80)) sb.s_log_zone_size = atol(buf);
547 printf("maxsize = %ld", sb.s_max_size);
548 if (input(buf, 80)) sb.s_max_size = atol(buf);
549 printf("block size = %ld", sb.s_block_size);
550 if (input(buf, 80)) sb.s_block_size = atol(buf);
551 if (yes("ok now")) {
552 devwrite(0, OFFSET_SUPER_BLOCK, (char *) &sb, sizeof(sb));
553 return;
555 printf("flags = ");
556 if(sb.s_flags & MFSFLAG_CLEAN) printf("CLEAN "); else printf("DIRTY ");
557 printf("\n");
558 } while (yes("Do you want to try again"));
559 if (repair) exit(FSCK_EXIT_OK);
562 /* Get the super block from either disk or user. Do some initial checks. */
563 void rw_super(int put)
565 if(lseek(dev, OFFSET_SUPER_BLOCK, SEEK_SET) < 0) {
566 perror("lseek");
567 fatal("couldn't seek to super block.");
569 if(put == SUPER_PUT) {
570 if(write(dev, &sb, sizeof(sb)) != sizeof(sb)) {
571 fatal("couldn't write super block.");
573 return;
575 if(read(dev, &sb, sizeof(sb)) != sizeof(sb)) {
576 fatal("couldn't read super block.");
578 if (listsuper) lsuper();
579 if (sb.s_magic == SUPER_MAGIC) fatal("Cannot handle V1 file systems");
580 if (sb.s_magic == SUPER_V2) {
581 fs_version = 2;
582 block_size = /* STATIC_BLOCK_SIZE */ 8192;
583 } else if(sb.s_magic == SUPER_V3) {
584 fs_version = 3;
585 block_size = sb.s_block_size;
586 } else {
587 fatal("bad magic number in super block");
589 if (sb.s_ninodes <= 0) fatal("no inodes");
590 if (sb.s_zones <= 0) fatal("no zones");
591 if (sb.s_imap_blocks <= 0) fatal("no imap");
592 if (sb.s_zmap_blocks <= 0) fatal("no zmap");
593 if (sb.s_firstdatazone != 0 && sb.s_firstdatazone <= 4)
594 fatal("first data zone too small");
595 if (sb.s_log_zone_size < 0) fatal("zone size < block size");
596 if (sb.s_max_size <= 0) {
597 printf("warning: invalid max file size %ld\n", sb.s_max_size);
598 sb.s_max_size = LONG_MAX;
602 /* Check the super block for reasonable contents. */
603 void chksuper()
605 register n;
606 register off_t maxsize;
608 n = bitmapsize((bit_t) sb.s_ninodes + 1, block_size);
609 if (sb.s_magic != SUPER_V2 && sb.s_magic != SUPER_V3)
610 fatal("bad magic number in super block");
611 if (sb.s_imap_blocks < n) {
612 printf("need %d bocks for inode bitmap; only have %d\n",
613 n, sb.s_imap_blocks);
614 fatal("too few imap blocks");
616 if (sb.s_imap_blocks != n) {
617 pr("warning: expected %d imap_block%s", n, "", "s");
618 printf(" instead of %d\n", sb.s_imap_blocks);
620 n = bitmapsize((bit_t) sb.s_zones, block_size);
621 if (sb.s_zmap_blocks < n) fatal("too few zmap blocks");
622 if (sb.s_zmap_blocks != n) {
623 pr("warning: expected %d zmap_block%s", n, "", "s");
624 printf(" instead of %d\n", sb.s_zmap_blocks);
626 if (sb.s_log_zone_size >= 8 * sizeof(block_nr))
627 fatal("log_zone_size too large");
628 if (sb.s_log_zone_size > 8) printf("warning: large log_zone_size (%d)\n",
629 sb.s_log_zone_size);
630 sb.s_firstdatazone = (BLK_ILIST + N_ILIST + SCALE - 1) >> sb.s_log_zone_size;
631 if (sb.s_firstdatazone_old != 0) {
632 if (sb.s_firstdatazone_old >= sb.s_zones)
633 fatal("first data zone too large");
634 if (sb.s_firstdatazone_old < sb.s_firstdatazone)
635 fatal("first data zone too small");
636 if (sb.s_firstdatazone_old != sb.s_firstdatazone) {
637 printf("warning: expected first data zone to be %u ",
638 sb.s_firstdatazone);
639 printf("instead of %u\n", sb.s_firstdatazone_old);
640 sb.s_firstdatazone = sb.s_firstdatazone_old;
643 maxsize = MAX_FILE_POS;
644 if (((maxsize - 1) >> sb.s_log_zone_size) / block_size >= MAX_ZONES)
645 maxsize = ((long) MAX_ZONES * block_size) << sb.s_log_zone_size;
646 if(maxsize <= 0)
647 maxsize = LONG_MAX;
648 if (sb.s_max_size != maxsize) {
649 printf("warning: expected max size to be %ld ", maxsize);
650 printf("instead of %ld\n", sb.s_max_size);
653 if(sb.s_flags & MFSFLAG_MANDATORY_MASK) {
654 fatal("unsupported feature bits - newer fsck needed");
658 int inoblock(int inn)
660 return div64u(mul64u(inn - 1, INODE_SIZE), block_size) + BLK_ILIST;
663 int inooff(int inn)
665 return rem64u(mul64u(inn - 1, INODE_SIZE), block_size);
668 /* Make a listing of the inodes given by `clist'. If `repair' is set, ask
669 * the user for changes.
671 void lsi(clist)
672 char **clist;
674 register bit_nr bit;
675 register ino_t ino;
676 d_inode inode, *ip = &inode;
677 char buf[80];
679 if (clist == 0) return;
680 while ((bit = getnumber(*clist++)) != NO_BIT) {
681 setbit(spec_imap, bit);
682 ino = bit;
683 do {
684 devread(inoblock(ino), inooff(ino), (char *) ip, INODE_SIZE);
685 printf("inode %u:\n", ino);
686 printf(" mode = %6o", ip->i_mode);
687 if (input(buf, 80)) ip->i_mode = atoo(buf);
688 printf(" nlinks = %6u", ip->i_nlinks);
689 if (input(buf, 80)) ip->i_nlinks = atol(buf);
690 printf(" size = %6ld", ip->i_size);
691 if (input(buf, 80)) ip->i_size = atol(buf);
692 if (yes("Write this back")) {
693 devwrite(inoblock(ino), inooff(ino), (char *) ip,
694 INODE_SIZE);
695 break;
697 } while (yes("Do you want to change it again"));
701 /* Allocate `nblk' blocks worth of bitmap. */
702 bitchunk_t *allocbitmap(nblk)
703 int nblk;
705 register bitchunk_t *bitmap;
707 bitmap = (bitchunk_t *) alloc((unsigned) nblk, block_size);
708 *bitmap |= 1;
709 return(bitmap);
712 /* Load the bitmap starting at block `bno' from disk. */
713 void loadbitmap(bitmap, bno, nblk)
714 bitchunk_t *bitmap;
715 block_nr bno;
716 int nblk;
718 register i;
719 register bitchunk_t *p;
721 p = bitmap;
722 for (i = 0; i < nblk; i++, bno++, p += WORDS_PER_BLOCK)
723 devread(bno, 0, (char *) p, block_size);
724 *bitmap |= 1;
727 /* Write the bitmap starting at block `bno' to disk. */
728 void dumpbitmap(bitmap, bno, nblk)
729 bitchunk_t *bitmap;
730 block_nr bno;
731 int nblk;
733 register i;
734 register bitchunk_t *p = bitmap;
736 for (i = 0; i < nblk; i++, bno++, p += WORDS_PER_BLOCK)
737 devwrite(bno, 0, (char *) p, block_size);
740 /* Set the bits given by `list' in the bitmap. */
741 void fillbitmap(bitmap, lwb, upb, list)
742 bitchunk_t *bitmap;
743 bit_nr lwb, upb;
744 char **list;
746 register bit_nr bit;
748 if (list == 0) return;
749 while ((bit = getnumber(*list++)) != NO_BIT)
750 if (bit < lwb || bit >= upb) {
751 if (bitmap == spec_imap)
752 printf("inode number %ld ", bit);
753 else
754 printf("zone number %ld ", bit);
755 printf("out of range (ignored)\n");
756 } else
757 setbit(bitmap, bit - lwb + 1);
760 /* Deallocate the bitmap `p'. */
761 void freebitmap(p)
762 bitchunk_t *p;
764 free((char *) p);
767 /* Get all the bitmaps used by this program. */
768 void getbitmaps()
770 imap = allocbitmap(N_IMAP);
771 zmap = allocbitmap(N_ZMAP);
772 spec_imap = allocbitmap(N_IMAP);
773 spec_zmap = allocbitmap(N_ZMAP);
774 dirmap = allocbitmap(N_IMAP);
777 /* Release all the space taken by the bitmaps. */
778 void putbitmaps()
780 freebitmap(imap);
781 freebitmap(zmap);
782 freebitmap(spec_imap);
783 freebitmap(spec_zmap);
784 freebitmap(dirmap);
787 /* `w1' and `w2' are differing words from two bitmaps that should be
788 * identical. Print what's the matter with them.
790 void chkword(w1, w2, bit, type, n, report, phys)
791 unsigned w1, w2;
792 char *type;
793 bit_nr bit;
794 int *n, *report;
795 bit_nr phys;
797 for (; (w1 | w2); w1 >>= 1, w2 >>= 1, bit++, phys++)
798 if ((w1 ^ w2) & 1 && ++(*n) % MAXPRINT == 0 && *report &&
799 (!repair || automatic || yes("stop this listing")))
800 *report = 0;
801 else if (*report)
802 if ((w1 & 1) && !(w2 & 1))
803 printf("%s %ld is missing\n", type, bit);
804 else if (!(w1 & 1) && (w2 & 1))
805 printf("%s %ld is not free\n", type, bit);
808 /* Check if the given (correct) bitmap is identical with the one that is
809 * on the disk. If not, ask if the disk should be repaired.
811 void chkmap(cmap, dmap, bit, blkno, nblk, type)
812 bitchunk_t *cmap, *dmap;
813 bit_nr bit;
814 block_nr blkno;
815 int nblk;
816 char *type;
818 register bitchunk_t *p = dmap, *q = cmap;
819 int report = 1, nerr = 0;
820 int w = nblk * WORDS_PER_BLOCK;
821 bit_nr phys = 0;
823 printf("Checking %s map. ", type);
824 if(!preen) printf("\n");
825 fflush(stdout);
826 loadbitmap(dmap, blkno, nblk);
827 do {
828 if (*p != *q) chkword(*p, *q, bit, type, &nerr, &report, phys);
829 p++;
830 q++;
831 bit += 8 * sizeof(bitchunk_t);
832 phys += 8 * sizeof(bitchunk_t);
833 } while (--w > 0);
835 if ((!repair || automatic) && !report) printf("etc. ");
836 if (nerr > MAXPRINT || nerr > 10) printf("%d errors found. ", nerr);
837 if (nerr != 0 && yes("install a new map")) dumpbitmap(cmap, blkno, nblk);
838 if (nerr > 0) printf("\n");
841 /* See if the inodes that aren't allocated are cleared. */
842 void chkilist()
844 register ino_t ino = 1;
845 mode_t mode;
847 printf("Checking inode list. ");
848 if(!preen) printf("\n");
849 fflush(stdout);
851 if (!bitset(imap, (bit_nr) ino)) {
852 devread(inoblock(ino), inooff(ino), (char *) &mode,
853 sizeof(mode));
854 if (mode != I_NOT_ALLOC) {
855 printf("mode inode %u not cleared", ino);
856 if (yes(". clear")) devwrite(inoblock(ino),
857 inooff(ino), nullbuf, INODE_SIZE);
860 while (++ino <= sb.s_ninodes && ino != 0);
861 if(!preen) printf("\n");
864 /* Allocate an array to maintain the inode reference counts in. */
865 void getcount()
867 count = (nlink_t *) alloc((unsigned) (sb.s_ninodes + 1), sizeof(nlink_t));
870 /* The reference count for inode `ino' is wrong. Ask if it should be adjusted. */
871 void counterror(ino_t ino)
873 d_inode inode;
875 if (firstcnterr) {
876 printf("INODE NLINK COUNT\n");
877 firstcnterr = 0;
879 devread(inoblock(ino), inooff(ino), (char *) &inode, INODE_SIZE);
880 count[ino] += inode.i_nlinks; /* it was already subtracted; add it back */
881 printf("%5u %5u %5u", ino, (unsigned) inode.i_nlinks, count[ino]);
882 if (yes(" adjust")) {
883 if ((inode.i_nlinks = count[ino]) == 0) {
884 fatal("internal error (counterror)");
885 inode.i_mode = I_NOT_ALLOC;
886 clrbit(imap, (bit_nr) ino);
888 devwrite(inoblock(ino), inooff(ino), (char *) &inode, INODE_SIZE);
892 /* Check if the reference count of the inodes are correct. The array `count'
893 * is maintained as follows: an entry indexed by the inode number is
894 * incremented each time a link is found; when the inode is read the link
895 * count in there is substracted from the corresponding entry in `count'.
896 * Thus, when the whole file system has been traversed, all the entries
897 * should be zero.
899 void chkcount()
901 register ino_t ino;
903 for (ino = 1; ino <= sb.s_ninodes && ino != 0; ino++)
904 if (count[ino] != 0) counterror(ino);
905 if (!firstcnterr) printf("\n");
908 /* Deallocate the `count' array. */
909 void freecount()
911 free((char *) count);
914 /* Print the inode permission bits given by mode and shift. */
915 void printperm(mode_t mode, int shift, int special, int overlay)
917 if (mode >> shift & R_BIT)
918 putchar('r');
919 else
920 putchar('-');
921 if (mode >> shift & W_BIT)
922 putchar('w');
923 else
924 putchar('-');
925 if (mode & special)
926 putchar(overlay);
927 else
928 if (mode >> shift & X_BIT)
929 putchar('x');
930 else
931 putchar('-');
934 /* List the given inode. */
935 void list(ino_t ino, d_inode *ip)
937 if (firstlist) {
938 firstlist = 0;
939 printf(" inode permission link size name\n");
941 printf("%6u ", ino);
942 switch (ip->i_mode & I_TYPE) {
943 case I_REGULAR: putchar('-'); break;
944 case I_DIRECTORY: putchar('d'); break;
945 case I_CHAR_SPECIAL: putchar('c'); break;
946 case I_BLOCK_SPECIAL: putchar('b'); break;
947 case I_NAMED_PIPE: putchar('p'); break;
948 case I_UNIX_SOCKET: putchar('s'); break;
949 #ifdef I_SYMBOLIC_LINK
950 case I_SYMBOLIC_LINK: putchar('l'); break;
951 #endif
952 default: putchar('?');
954 printperm(ip->i_mode, 6, I_SET_UID_BIT, 's');
955 printperm(ip->i_mode, 3, I_SET_GID_BIT, 's');
956 printperm(ip->i_mode, 0, STICKY_BIT, 't');
957 printf(" %3u ", ip->i_nlinks);
958 switch (ip->i_mode & I_TYPE) {
959 case I_CHAR_SPECIAL:
960 case I_BLOCK_SPECIAL:
961 printf(" %2x,%2x ", (dev_t) ip->i_zone[0] >> MAJOR & 0xFF,
962 (dev_t) ip->i_zone[0] >> MINOR & 0xFF);
963 break;
964 default: printf("%7ld ", ip->i_size);
966 printpath(0, 1);
969 /* Remove an entry from a directory if ok with the user.
970 * Don't name the function remove() - that is owned by ANSI, and chaos results
971 * when it is a macro.
973 int Remove(dir_struct *dp)
975 setbit(spec_imap, (bit_nr) dp->d_inum);
976 if (yes(". remove entry")) {
977 count[dp->d_inum]--;
978 memset((void *) dp, 0, sizeof(dir_struct));
979 return(1);
981 return(0);
984 /* Convert string so that embedded control characters are printable. */
985 void make_printable_name(dst, src, n)
986 register char *dst;
987 register char *src;
988 register int n;
990 register int c;
992 while (--n >= 0 && (c = *src++) != '\0') {
993 if (isprint(c) && c != '\\')
994 *dst++ = c;
995 else {
996 *dst++ = '\\';
997 switch (c) {
998 case '\\':
999 *dst++ = '\\'; break;
1000 case '\b':
1001 *dst++ = 'b'; break;
1002 case '\f':
1003 *dst++ = 'f'; break;
1004 case '\n':
1005 *dst++ = 'n'; break;
1006 case '\r':
1007 *dst++ = 'r'; break;
1008 case '\t':
1009 *dst++ = 't'; break;
1010 default:
1011 *dst++ = '0' + ((c >> 6) & 03);
1012 *dst++ = '0' + ((c >> 3) & 07);
1013 *dst++ = '0' + (c & 07);
1017 *dst = '\0';
1020 /* See if the `.' or `..' entry is as expected. */
1021 int chkdots(ino_t ino, off_t pos, dir_struct *dp, ino_t exp)
1023 char printable_name[4 * MFS_NAME_MAX + 1];
1025 if (dp->d_inum != exp) {
1026 make_printable_name(printable_name, dp->mfs_d_name, sizeof(dp->mfs_d_name));
1027 printf("bad %s in ", printable_name);
1028 printpath(1, 0);
1029 printf("%s is linked to %u ", printable_name, dp->d_inum);
1030 printf("instead of %u)", exp);
1031 setbit(spec_imap, (bit_nr) ino);
1032 setbit(spec_imap, (bit_nr) dp->d_inum);
1033 setbit(spec_imap, (bit_nr) exp);
1034 if (yes(". repair")) {
1035 count[dp->d_inum]--;
1036 dp->d_inum = exp;
1037 count[exp]++;
1038 return(0);
1040 } else if (pos != (dp->mfs_d_name[1] ? DIR_ENTRY_SIZE : 0)) {
1041 make_printable_name(printable_name, dp->mfs_d_name, sizeof(dp->mfs_d_name));
1042 printf("warning: %s has offset %ld in ", printable_name, pos);
1043 printpath(1, 0);
1044 printf("%s is linked to %u)\n", printable_name, dp->d_inum);
1045 setbit(spec_imap, (bit_nr) ino);
1046 setbit(spec_imap, (bit_nr) dp->d_inum);
1047 setbit(spec_imap, (bit_nr) exp);
1049 return(1);
1052 /* Check the name in a directory entry. */
1053 int chkname(ino_t ino, dir_struct *dp)
1055 register n = MFS_NAME_MAX + 1;
1056 register char *p = dp->mfs_d_name;
1058 if (*p == '\0') {
1059 printf("null name found in ");
1060 printpath(0, 0);
1061 setbit(spec_imap, (bit_nr) ino);
1062 if (Remove(dp)) return(0);
1064 while (*p != '\0' && --n != 0)
1065 if (*p++ == '/') {
1066 printf("found a '/' in entry of directory ");
1067 printpath(1, 0);
1068 setbit(spec_imap, (bit_nr) ino);
1069 printf("entry = '");
1070 printname(dp->mfs_d_name);
1071 printf("')");
1072 if (Remove(dp)) return(0);
1073 break;
1075 return(1);
1078 /* Check a directory entry. Here the routine `descendtree' is called
1079 * recursively to check the file or directory pointed to by the entry.
1081 int chkentry(ino_t ino, off_t pos, dir_struct *dp)
1083 if (dp->d_inum < ROOT_INODE || dp->d_inum > sb.s_ninodes) {
1084 printf("bad inode found in directory ");
1085 printpath(1, 0);
1086 printf("ino found = %u, ", dp->d_inum);
1087 printf("name = '");
1088 printname(dp->mfs_d_name);
1089 printf("')");
1090 if (yes(". remove entry")) {
1091 memset((void *) dp, 0, sizeof(dir_struct));
1092 return(0);
1094 return(1);
1096 if ((unsigned) count[dp->d_inum] == SHRT_MAX) {
1097 printf("too many links to ino %u\n", dp->d_inum);
1098 printf("discovered at entry '");
1099 printname(dp->mfs_d_name);
1100 printf("' in directory ");
1101 printpath(0, 1);
1102 if (Remove(dp)) return(0);
1104 count[dp->d_inum]++;
1105 if (strcmp(dp->mfs_d_name, ".") == 0) {
1106 ftop->st_presence |= DOT;
1107 return(chkdots(ino, pos, dp, ino));
1109 if (strcmp(dp->mfs_d_name, "..") == 0) {
1110 ftop->st_presence |= DOTDOT;
1111 return(chkdots(ino, pos, dp, ino == ROOT_INODE ? ino :
1112 ftop->st_next->st_dir->d_inum));
1114 if (!chkname(ino, dp)) return(0);
1115 if (bitset(dirmap, (bit_nr) dp->d_inum)) {
1116 printf("link to directory discovered in ");
1117 printpath(1, 0);
1118 printf("name = '");
1119 printname(dp->mfs_d_name);
1120 printf("', dir ino = %u)", dp->d_inum);
1121 return !Remove(dp);
1123 return(descendtree(dp));
1126 /* Check a zone of a directory by checking all the entries in the zone.
1127 * The zone is split up into chunks to not allocate too much stack.
1129 int chkdirzone(ino_t ino, d_inode *ip, off_t pos, zone_nr zno)
1131 dir_struct dirblk[CDIRECT];
1132 register dir_struct *dp;
1133 register n, dirty;
1134 long block= ztob(zno);
1135 register long offset = 0;
1136 register off_t size = 0;
1137 n = SCALE * (NR_DIR_ENTRIES(block_size) / CDIRECT);
1139 do {
1140 devread(block, offset, (char *) dirblk, DIRCHUNK);
1141 dirty = 0;
1142 for (dp = dirblk; dp < &dirblk[CDIRECT]; dp++) {
1143 if (dp->d_inum != NO_ENTRY && !chkentry(ino, pos, dp))
1144 dirty = 1;
1145 pos += DIR_ENTRY_SIZE;
1146 if (dp->d_inum != NO_ENTRY) size = pos;
1148 if (dirty) devwrite(block, offset, (char *) dirblk, DIRCHUNK);
1149 offset += DIRCHUNK;
1150 n--;
1151 } while (n > 0);
1153 if (size > ip->i_size) {
1154 printf("size not updated of directory ");
1155 printpath(2, 0);
1156 if (yes(". extend")) {
1157 setbit(spec_imap, (bit_nr) ino);
1158 ip->i_size = size;
1159 devwrite(inoblock(ino), inooff(ino), (char *) ip, INODE_SIZE);
1162 return(1);
1166 int chksymlinkzone(ino_t ino, d_inode *ip, off_t pos, zone_nr zno)
1168 long block;
1169 size_t len;
1170 char target[PATH_MAX+1];
1172 if (ip->i_size > PATH_MAX)
1173 fatal("chksymlinkzone: fsck program inconsistency\n");
1174 block= ztob(zno);
1175 devread(block, 0, target, ip->i_size);
1176 target[ip->i_size]= '\0';
1177 len= strlen(target);
1178 if (len != ip->i_size)
1180 printf("bad size in symbolic link (%d instead of %d) ",
1181 ip->i_size, len);
1182 printpath(2, 0);
1183 if (yes(". update")) {
1184 setbit(spec_imap, (bit_nr) ino);
1185 ip->i_size = len;
1186 devwrite(inoblock(ino), inooff(ino),
1187 (char *) ip, INODE_SIZE);
1190 return 1;
1193 /* There is something wrong with the given zone. Print some details. */
1194 void errzone(mess, zno, level, pos)
1195 char *mess;
1196 zone_nr zno;
1197 int level;
1198 off_t pos;
1200 printf("%s zone in ", mess);
1201 printpath(1, 0);
1202 printf("zno = %ld, type = ", zno);
1203 switch (level) {
1204 case 0: printf("DATA"); break;
1205 case 1: printf("SINGLE INDIRECT"); break;
1206 case 2: printf("DOUBLE INDIRECT"); break;
1207 default: printf("VERY INDIRECT");
1209 printf(", pos = %ld)\n", pos);
1212 /* Found the given zone in the given inode. Check it, and if ok, mark it
1213 * in the zone bitmap.
1215 int markzone(zno, level, pos)
1216 zone_nr zno;
1217 int level;
1218 off_t pos;
1220 register bit_nr bit = (bit_nr) zno - FIRST + 1;
1222 ztype[level]++;
1223 if (zno < FIRST || zno >= sb.s_zones) {
1224 errzone("out-of-range", zno, level, pos);
1225 return(0);
1227 if (bitset(zmap, bit)) {
1228 setbit(spec_zmap, bit);
1229 errzone("duplicate", zno, level, pos);
1230 return(0);
1232 nfreezone--;
1233 if (bitset(spec_zmap, bit)) errzone("found", zno, level, pos);
1234 setbit(zmap, bit);
1235 return(1);
1238 /* Check an indirect zone by checking all of its entries.
1239 * The zone is split up into chunks to not allocate too much stack.
1241 int chkindzone(ino_t ino, d_inode *ip, off_t *pos, zone_nr zno, int level)
1243 zone_nr indirect[CINDIR];
1244 register n = NR_INDIRECTS / CINDIR;
1245 long block= ztob(zno);
1246 register long offset = 0;
1248 do {
1249 devread(block, offset, (char *) indirect, INDCHUNK);
1250 if (!chkzones(ino, ip, pos, indirect, CINDIR, level - 1)) return(0);
1251 offset += INDCHUNK;
1252 } while (--n && *pos < ip->i_size);
1253 return(1);
1256 /* Return the size of a gap in the file, represented by a null zone number
1257 * at some level of indirection.
1259 off_t jump(level)
1260 int level;
1262 off_t power = ZONE_SIZE;
1264 if (level != 0) do
1265 power *= NR_INDIRECTS;
1266 while (--level);
1267 return(power);
1270 /* Check a zone, which may be either a normal data zone, a directory zone,
1271 * or an indirect zone.
1273 int zonechk(ino_t ino, d_inode *ip, off_t *pos, zone_nr zno, int level)
1275 if (level == 0) {
1276 if ((ip->i_mode & I_TYPE) == I_DIRECTORY &&
1277 !chkdirzone(ino, ip, *pos, zno))
1278 return(0);
1279 if ((ip->i_mode & I_TYPE) == I_SYMBOLIC_LINK &&
1280 !chksymlinkzone(ino, ip, *pos, zno))
1281 return(0);
1282 *pos += ZONE_SIZE;
1283 return(1);
1284 } else
1285 return chkindzone(ino, ip, pos, zno, level);
1288 /* Check a list of zones given by `zlist'. */
1289 int chkzones(ino_t ino, d_inode *ip, off_t *pos, zone_nr *zlist,
1290 int len, int level)
1292 register ok = 1, i;
1294 /* The check on the position in the next loop is commented out, since FS
1295 * now requires valid zone numbers in each level that is necessary and FS
1296 * always deleted all the zones in the double indirect block.
1298 for (i = 0; i < len /* && *pos < ip->i_size */ ; i++)
1299 if (zlist[i] == NO_ZONE)
1300 *pos += jump(level);
1301 else if (!markzone(zlist[i], level, *pos)) {
1302 *pos += jump(level);
1303 ok = 0;
1304 } else if (!zonechk(ino, ip, pos, zlist[i], level))
1305 ok = 0;
1306 return(ok);
1309 /* Check a file or a directory. */
1310 int chkfile(ino_t ino, d_inode *ip)
1312 register ok, i, level;
1313 off_t pos = 0;
1315 ok = chkzones(ino, ip, &pos, &ip->i_zone[0], NR_DZONE_NUM, 0);
1316 for (i = NR_DZONE_NUM, level = 1; i < NR_ZONE_NUMS; i++, level++)
1317 ok &= chkzones(ino, ip, &pos, &ip->i_zone[i], 1, level);
1318 return(ok);
1321 /* Check a directory by checking the contents. Check if . and .. are present. */
1322 int chkdirectory(ino_t ino, d_inode *ip)
1324 register ok;
1326 setbit(dirmap, (bit_nr) ino);
1327 ok = chkfile(ino, ip);
1328 if (!(ftop->st_presence & DOT)) {
1329 printf(". missing in ");
1330 printpath(2, 1);
1331 ok = 0;
1333 if (!(ftop->st_presence & DOTDOT)) {
1334 printf(".. missing in ");
1335 printpath(2, 1);
1336 ok = 0;
1338 return(ok);
1341 #ifdef I_SYMBOLIC_LINK
1343 /* Check the validity of a symbolic link. */
1344 int chklink(ino_t ino, d_inode *ip)
1346 int ok;
1348 ok = chkfile(ino, ip);
1349 if (ip->i_size <= 0 || ip->i_size > block_size) {
1350 if (ip->i_size == 0)
1351 printf("empty symbolic link ");
1352 else
1353 printf("symbolic link too large (size %ld) ", ip->i_size);
1354 printpath(2, 1);
1355 ok = 0;
1357 return(ok);
1360 #endif
1362 /* Check the validity of a special file. */
1363 int chkspecial(ino_t ino, d_inode *ip)
1365 int i, ok;
1367 ok = 1;
1368 if ((dev_t) ip->i_zone[0] == NO_DEV) {
1369 printf("illegal device number %ld for special file ", ip->i_zone[0]);
1370 printpath(2, 1);
1371 ok = 0;
1374 /* FS will not use the remaining "zone numbers" but 1.6.11++ will panic if
1375 * they are nonzero, since this should not happen.
1377 for (i = 1; i < NR_ZONE_NUMS; i++)
1378 if (ip->i_zone[i] != NO_ZONE) {
1379 printf("nonzero zone number %ld for special file ",
1380 ip->i_zone[i]);
1381 printpath(2, 1);
1382 ok = 0;
1384 return(ok);
1387 /* Check the mode and contents of an inode. */
1388 int chkmode(ino_t ino, d_inode *ip)
1390 switch (ip->i_mode & I_TYPE) {
1391 case I_REGULAR:
1392 nregular++;
1393 return chkfile(ino, ip);
1394 case I_DIRECTORY:
1395 ndirectory++;
1396 return chkdirectory(ino, ip);
1397 case I_BLOCK_SPECIAL:
1398 nblkspec++;
1399 return chkspecial(ino, ip);
1400 case I_CHAR_SPECIAL:
1401 ncharspec++;
1402 return chkspecial(ino, ip);
1403 case I_NAMED_PIPE:
1404 npipe++;
1405 return chkfile(ino, ip);
1406 case I_UNIX_SOCKET:
1407 nsock++;
1408 return chkfile(ino, ip);
1409 #ifdef I_SYMBOLIC_LINK
1410 case I_SYMBOLIC_LINK:
1411 nsyml++;
1412 return chklink(ino, ip);
1413 #endif
1414 default:
1415 nbadinode++;
1416 printf("bad mode of ");
1417 printpath(1, 0);
1418 printf("mode = %o)", ip->i_mode);
1419 return(0);
1423 /* Check an inode. */
1424 int chkinode(ino_t ino, d_inode *ip)
1426 if (ino == ROOT_INODE && (ip->i_mode & I_TYPE) != I_DIRECTORY) {
1427 printf("root inode is not a directory ");
1428 printf("(ino = %u, mode = %o)\n", ino, ip->i_mode);
1429 fatal("");
1431 if (ip->i_nlinks == 0) {
1432 printf("link count zero of ");
1433 printpath(2, 0);
1434 return(0);
1436 nfreeinode--;
1437 setbit(imap, (bit_nr) ino);
1438 if ((unsigned) ip->i_nlinks > SHRT_MAX) {
1439 printf("link count too big in ");
1440 printpath(1, 0);
1441 printf("cnt = %u)\n", (unsigned) ip->i_nlinks);
1442 count[ino] -= SHRT_MAX;
1443 setbit(spec_imap, (bit_nr) ino);
1444 } else {
1445 count[ino] -= (unsigned) ip->i_nlinks;
1447 return chkmode(ino, ip);
1450 /* Check the directory entry pointed to by dp, by checking the inode. */
1451 int descendtree(dp)
1452 dir_struct *dp;
1454 d_inode inode;
1455 register ino_t ino = dp->d_inum;
1456 register visited;
1457 struct stack stk;
1459 stk.st_dir = dp;
1460 stk.st_next = ftop;
1461 ftop = &stk;
1462 if (bitset(spec_imap, (bit_nr) ino)) {
1463 printf("found inode %u: ", ino);
1464 printpath(0, 1);
1466 visited = bitset(imap, (bit_nr) ino);
1467 if (!visited || listing) {
1468 devread(inoblock(ino), inooff(ino), (char *) &inode, INODE_SIZE);
1469 if (listing) list(ino, &inode);
1470 if (!visited && !chkinode(ino, &inode)) {
1471 setbit(spec_imap, (bit_nr) ino);
1472 if (yes("remove")) {
1473 count[ino] += inode.i_nlinks - 1;
1474 clrbit(imap, (bit_nr) ino);
1475 devwrite(inoblock(ino), inooff(ino),
1476 nullbuf, INODE_SIZE);
1477 memset((void *) dp, 0, sizeof(dir_struct));
1478 ftop = ftop->st_next;
1479 return(0);
1483 ftop = ftop->st_next;
1484 return(1);
1487 /* Check the file system tree. */
1488 void chktree()
1490 dir_struct dir;
1492 nfreeinode = sb.s_ninodes;
1493 nfreezone = N_DATA;
1494 dir.d_inum = ROOT_INODE;
1495 dir.mfs_d_name[0] = 0;
1496 if (!descendtree(&dir)) fatal("bad root inode");
1497 putchar('\n');
1500 /* Print the totals of all the objects found. */
1501 void printtotal()
1503 if(preen) {
1504 printf("%d files, %d directories, %d free inodes, %d free zones\n",
1505 nregular, ndirectory, nfreeinode, nfreezone);
1506 return;
1509 printf("blocksize = %5d ", block_size);
1510 printf("zonesize = %5d\n", ZONE_SIZE);
1511 printf("\n");
1512 pr("%8u Regular file%s\n", nregular, "", "s");
1513 pr("%8u Director%s\n", ndirectory, "y", "ies");
1514 pr("%8u Block special file%s\n", nblkspec, "", "s");
1515 pr("%8u Character special file%s\n", ncharspec, "", "s");
1516 if (nbadinode != 0) pr("%6u Bad inode%s\n", nbadinode, "", "s");
1517 pr("%8u Free inode%s\n", nfreeinode, "", "s");
1518 pr("%8u Named pipe%s\n", npipe, "", "s");
1519 pr("%8u Unix socket%s\n", nsock, "", "s");
1520 pr("%8u Symbolic link%s\n", nsyml, "", "s");
1521 /* Don't print some fields.
1522 printf("\n");
1523 pr("%8u Data zone%s\n", ztype[0], "", "s");
1524 pr("%8u Single indirect zone%s\n", ztype[1], "", "s");
1525 pr("%8u Double indirect zone%s\n", ztype[2], "", "s");
1527 lpr("%8ld Free zone%s\n", nfreezone, "", "s");
1530 /* Check the device which name is given by `f'. The inodes listed by `clist'
1531 * should be listed separately, and the inodes listed by `ilist' and the zones
1532 * listed by `zlist' should be watched for while checking the file system.
1535 void chkdev(f, clist, ilist, zlist)
1536 char *f, **clist, **ilist, **zlist;
1538 if (automatic) repair = 1;
1539 fsck_device = f;
1540 initvars();
1542 devopen();
1544 rw_super(SUPER_GET);
1546 if(block_size < _MIN_BLOCK_SIZE)
1547 fatal("funny block size");
1549 if(!(rwbuf = malloc(block_size))) fatal("couldn't allocate fs buf (1)");
1550 if(!(nullbuf = malloc(block_size))) fatal("couldn't allocate fs buf (2)");
1551 memset(nullbuf, 0, block_size);
1553 chksuper();
1555 if(markdirty) {
1556 if(sb.s_flags & MFSFLAG_CLEAN) {
1557 sb.s_flags &= ~MFSFLAG_CLEAN;
1558 rw_super(SUPER_PUT);
1559 printf("\n----- FILE SYSTEM MARKED DIRTY -----\n\n");
1560 } else {
1561 printf("Filesystem is already dirty.\n");
1565 /* If preening, skip fsck if clean flag is on. */
1566 if(preen) {
1567 if(sb.s_flags & MFSFLAG_CLEAN) {
1568 printf("%s: clean\n", f);
1569 return;
1571 printf("%s: dirty, performing fsck\n", f);
1574 lsi(clist);
1576 getbitmaps();
1578 fillbitmap(spec_imap, (bit_nr) 1, (bit_nr) sb.s_ninodes + 1, ilist);
1579 fillbitmap(spec_zmap, (bit_nr) FIRST, (bit_nr) sb.s_zones, zlist);
1581 getcount();
1582 chktree();
1583 chkmap(zmap, spec_zmap, (bit_nr) FIRST - 1, BLK_ZMAP, N_ZMAP, "zone");
1584 chkcount();
1585 chkmap(imap, spec_imap, (bit_nr) 0, BLK_IMAP, N_IMAP, "inode");
1586 chkilist();
1587 if(preen) printf("\n");
1588 printtotal();
1590 putbitmaps();
1591 freecount();
1593 if (changed) printf("\n----- FILE SYSTEM HAS BEEN MODIFIED -----\n\n");
1595 /* If we were told to repair the FS, and the user never stopped us from
1596 * doing it, and the FS wasn't marked clean, we can mark the FS as clean.
1597 * If we were stopped from repairing, tell user about it.
1599 if(repair && !(sb.s_flags & MFSFLAG_CLEAN)) {
1600 if(notrepaired) {
1601 printf("\n----- FILE SYSTEM STILL DIRTY -----\n\n");
1602 } else {
1603 sync(); /* update FS on disk before clean flag */
1604 sb.s_flags |= MFSFLAG_CLEAN;
1605 rw_super(SUPER_PUT);
1606 printf("\n----- FILE SYSTEM MARKED CLEAN -----\n\n");
1610 devclose();
1613 int main(argc, argv)
1614 int argc;
1615 char **argv;
1617 register char **clist = 0, **ilist = 0, **zlist = 0;
1618 int badflag = 0;
1620 register devgiven = 0;
1621 register char *arg;
1623 if ((1 << BITSHIFT) != 8 * sizeof(bitchunk_t)) {
1624 printf("Fsck was compiled with the wrong BITSHIFT!\n");
1625 exit(FSCK_EXIT_CHECK_FAILED);
1628 sync();
1629 prog = *argv++;
1630 while ((arg = *argv++) != 0)
1631 if (arg[0] == '-' && arg[1] != 0 && arg[2] == 0) switch (arg[1]) {
1632 case 'd': markdirty = 1; break;
1633 case 'p': preen = repair = automatic = 1; break;
1634 case 'y':
1635 case 'a': automatic ^= 1; break;
1636 case 'c':
1637 clist = getlist(&argv, "inode");
1638 break;
1639 case 'i':
1640 ilist = getlist(&argv, "inode");
1641 break;
1642 case 'z':
1643 zlist = getlist(&argv, "zone");
1644 break;
1645 case 'r': repair ^= 1; break;
1646 case 'l': listing ^= 1; break;
1647 case 's': listsuper ^= 1; break;
1648 case 'f': break;
1649 default:
1650 printf("%s: unknown flag '%s'\n", prog, arg);
1651 badflag = 1;
1653 else {
1654 chkdev(arg, clist, ilist, zlist);
1655 clist = 0;
1656 ilist = 0;
1657 zlist = 0;
1658 devgiven = 1;
1660 if (!devgiven || badflag) {
1661 printf("Usage: fsck [-dyfpacilrsz] file\n");
1662 exit(FSCK_EXIT_USAGE);
1664 return(0);