1 /* 2 * Copyright (c) 1998, 2010, Oracle and/or its affiliates. All rights reserved. 3 */ 4 5 /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */ 6 /* All Rights Reserved */ 7 8 /* 9 * Copyright (c) 1980, 1986, 1990 The Regents of the University of California. 10 * All rights reserved. 11 * 12 * Redistribution and use in source and binary forms are permitted 13 * provided that: (1) source distributions retain this entire copyright 14 * notice and comment, and (2) distributions including binaries display 15 * the following acknowledgement: ``This product includes software 16 * developed by the University of California, Berkeley and its contributors'' 17 * in the documentation or other materials provided with the distribution 18 * and in all advertising materials mentioning features or use of this 19 * software. Neither the name of the University nor the names of its 20 * contributors may be used to endorse or promote products derived 21 * from this software without specific prior written permission. 22 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 23 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 24 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 25 */ 26 27 #include <stdio.h> 28 #include <fcntl.h> 29 #include <errno.h> 30 #include <unistd.h> 31 #include <stdlib.h> 32 #include <stdarg.h> 33 #include <fcntl.h> 34 #include <string.h> 35 #include <strings.h> 36 #include <ctype.h> 37 #include <malloc.h> 38 #include <signal.h> 39 #include <sys/param.h> 40 #include <sys/types.h> 41 #include <sys/mntent.h> 42 #include <sys/filio.h> 43 #include <sys/vnode.h> 44 #include <sys/mnttab.h> 45 #include <sys/types.h> 46 #include <sys/stat.h> 47 #include <sys/vfstab.h> 48 #include <sys/sysmacros.h> 49 #include <sys/fs/udf_volume.h> 50 #include "fsck.h" 51 #include <sys/lockfs.h> 52 #include <locale.h> 53 54 extern int32_t verifytag(struct tag *, uint32_t, struct tag *, int); 55 extern char *tagerrs[]; 56 extern void maketag(struct tag *, struct tag *); 57 extern char *hasvfsopt(struct vfstab *, char *); 58 static struct bufarea *getdatablk(daddr_t, long); 59 static struct bufarea *getblk(struct bufarea *, daddr_t, long); 60 61 void flush(int32_t, struct bufarea *); 62 int32_t bread(int32_t, char *, daddr_t, long); 63 void bwrite(int, char *, daddr_t, long); 64 static int32_t getaline(FILE *, char *, int32_t); 65 void errexit(char *, ...) __NORETURN; 66 static long diskreads, totalreads; /* Disk cache statistics */ 67 offset_t llseek(); 68 extern unsigned int largefile_count; 69 70 /* 71 * An unexpected inconsistency occured. 72 * Die if preening, otherwise just print message and continue. 73 */ 74 /* VARARGS1 */ 75 void 76 pfatal(char *fmt, ...) 77 { 78 va_list args; 79 va_start(args, fmt); 80 if (preen) { 81 (void) printf("%s: ", devname); 82 (void) vprintf(fmt, args); 83 (void) printf("\n"); 84 (void) printf( 85 gettext("%s: UNEXPECTED INCONSISTENCY; RUN fsck " 86 "MANUALLY.\n"), devname); 87 va_end(args); 88 exit(36); 89 } 90 (void) vprintf(fmt, args); 91 va_end(args); 92 } 93 94 /* 95 * Pwarn just prints a message when not preening, 96 * or a warning (preceded by filename) when preening. 97 */ 98 /* VARARGS1 */ 99 void 100 pwarn(char *fmt, ...) 101 { 102 va_list args; 103 va_start(args, fmt); 104 if (preen) 105 (void) printf("%s: ", devname); 106 (void) vprintf(fmt, args); 107 va_end(args); 108 } 109 110 111 /* VARARGS1 */ 112 void 113 errexit(char *fmt, ...) 114 { 115 va_list args; 116 va_start(args, fmt); 117 (void) vprintf(fmt, args); 118 va_end(args); 119 exit(39); 120 } 121 122 void 123 markbusy(daddr_t block, long count) 124 { 125 register int i; 126 127 count = roundup(count, secsize) / secsize; 128 for (i = 0; i < count; i++, block++) { 129 if ((unsigned)block > part_len) { 130 pwarn(gettext("Block %lx out of range\n"), block); 131 break; 132 } 133 if (testbusy(block)) 134 pwarn(gettext("Dup block %lx\n"), block); 135 else { 136 n_blks++; 137 setbusy(block); 138 } 139 } 140 } 141 142 void 143 printfree() 144 { 145 int i, startfree, endfree; 146 147 startfree = -1; 148 for (i = 0; i < part_len; i++) { 149 if (!testbusy(i)) { 150 if (startfree <= 0) 151 startfree = i; 152 endfree = i; 153 } else if (startfree >= 0) { 154 (void) printf("free: %x-%x\n", startfree, endfree - 1); 155 startfree = -1; 156 } 157 } 158 if (startfree >= 0) { 159 (void) printf("free: %x-%x\n", startfree, endfree); 160 } 161 } 162 163 struct bufarea * 164 getfilentry(uint32_t block, int len) 165 { 166 struct bufarea *bp; 167 struct file_entry *fp; 168 int err; 169 170 if (len > fsbsize) { 171 (void) printf(gettext("File entry at %x is too long " 172 "(%d bytes)\n"), block, len); 173 len = fsbsize; 174 } 175 bp = getdatablk((daddr_t)(block + part_start), fsbsize); 176 if (bp->b_errs) { 177 bp->b_flags &= ~B_INUSE; 178 return (NULL); 179 } 180 /* LINTED */ 181 fp = (struct file_entry *)bp->b_un.b_buf; 182 err = verifytag(&fp->fe_tag, block, &fp->fe_tag, UD_FILE_ENTRY); 183 if (err) { 184 (void) printf(gettext("Tag error %s or bad file entry, " 185 "tag=%d\n"), tagerrs[err], fp->fe_tag.tag_id); 186 bp->b_flags &= ~B_INUSE; 187 return (NULL); 188 } 189 return (bp); 190 } 191 192 void 193 putfilentry(struct bufarea *bp) 194 { 195 struct file_entry *fp; 196 197 /* LINTED */ 198 fp = (struct file_entry *)bp->b_un.b_buf; 199 maketag(&fp->fe_tag, &fp->fe_tag); 200 } 201 202 203 int32_t 204 reply(char *question) 205 { 206 char line[80]; 207 208 if (preen) 209 pfatal(gettext("INTERNAL ERROR: GOT TO reply()")); 210 (void) printf("\n%s? ", question); 211 if (nflag || fswritefd < 0) { 212 (void) printf(gettext(" no\n\n")); 213 iscorrupt = 1; /* known to be corrupt */ 214 return (0); 215 } 216 if (yflag) { 217 (void) printf(gettext(" yes\n\n")); 218 return (1); 219 } 220 if (getaline(stdin, line, sizeof (line)) == EOF) 221 errexit("\n"); 222 (void) printf("\n"); 223 if (line[0] == 'y' || line[0] == 'Y') 224 return (1); 225 else { 226 iscorrupt = 1; /* known to be corrupt */ 227 return (0); 228 } 229 } 230 231 int32_t 232 getaline(FILE *fp, char *loc, int32_t maxlen) 233 { 234 int n; 235 register char *p, *lastloc; 236 237 p = loc; 238 lastloc = &p[maxlen-1]; 239 while ((n = getc(fp)) != '\n') { 240 if (n == EOF) 241 return (EOF); 242 if (!isspace(n) && p < lastloc) 243 *p++ = n; 244 } 245 *p = 0; 246 return (p - loc); 247 } 248 /* 249 * Malloc buffers and set up cache. 250 */ 251 void 252 bufinit() 253 { 254 register struct bufarea *bp; 255 long bufcnt, i; 256 char *bufp; 257 258 bufp = malloc((unsigned int)fsbsize); 259 if (bufp == 0) 260 errexit(gettext("cannot allocate buffer pool\n")); 261 bufhead.b_next = bufhead.b_prev = &bufhead; 262 bufcnt = MAXBUFSPACE / fsbsize; 263 if (bufcnt < MINBUFS) 264 bufcnt = MINBUFS; 265 for (i = 0; i < bufcnt; i++) { 266 bp = (struct bufarea *)malloc(sizeof (struct bufarea)); 267 bufp = malloc((unsigned int)fsbsize); 268 if (bp == NULL || bufp == NULL) { 269 if (i >= MINBUFS) 270 break; 271 errexit(gettext("cannot allocate buffer pool\n")); 272 } 273 bp->b_un.b_buf = bufp; 274 bp->b_prev = &bufhead; 275 bp->b_next = bufhead.b_next; 276 bufhead.b_next->b_prev = bp; 277 bufhead.b_next = bp; 278 initbarea(bp); 279 } 280 bufhead.b_size = i; /* save number of buffers */ 281 pbp = pdirbp = NULL; 282 } 283 284 /* 285 * Manage a cache of directory blocks. 286 */ 287 static struct bufarea * 288 getdatablk(daddr_t blkno, long size) 289 { 290 register struct bufarea *bp; 291 292 for (bp = bufhead.b_next; bp != &bufhead; bp = bp->b_next) 293 if (bp->b_bno == fsbtodb(blkno)) 294 goto foundit; 295 for (bp = bufhead.b_prev; bp != &bufhead; bp = bp->b_prev) 296 if ((bp->b_flags & B_INUSE) == 0) 297 break; 298 if (bp == &bufhead) 299 errexit(gettext("deadlocked buffer pool\n")); 300 (void) getblk(bp, blkno, size); 301 /* fall through */ 302 foundit: 303 totalreads++; 304 bp->b_prev->b_next = bp->b_next; 305 bp->b_next->b_prev = bp->b_prev; 306 bp->b_prev = &bufhead; 307 bp->b_next = bufhead.b_next; 308 bufhead.b_next->b_prev = bp; 309 bufhead.b_next = bp; 310 bp->b_flags |= B_INUSE; 311 return (bp); 312 } 313 314 static struct bufarea * 315 getblk(struct bufarea *bp, daddr_t blk, long size) 316 { 317 daddr_t dblk; 318 319 dblk = fsbtodb(blk); 320 if (bp->b_bno == dblk) 321 return (bp); 322 flush(fswritefd, bp); 323 diskreads++; 324 bp->b_errs = bread(fsreadfd, bp->b_un.b_buf, dblk, size); 325 bp->b_bno = dblk; 326 bp->b_size = size; 327 return (bp); 328 } 329 330 void 331 flush(int32_t fd, struct bufarea *bp) 332 { 333 if (!bp->b_dirty) 334 return; 335 if (bp->b_errs != 0) 336 pfatal(gettext("WRITING ZERO'ED BLOCK %d TO DISK\n"), 337 bp->b_bno); 338 bp->b_dirty = 0; 339 bp->b_errs = 0; 340 bwrite(fd, bp->b_un.b_buf, bp->b_bno, (long)bp->b_size); 341 } 342 343 static void 344 rwerror(char *mesg, daddr_t blk) 345 { 346 347 if (preen == 0) 348 (void) printf("\n"); 349 pfatal(gettext("CANNOT %s: BLK %ld"), mesg, blk); 350 if (reply(gettext("CONTINUE")) == 0) 351 errexit(gettext("Program terminated\n")); 352 } 353 354 void 355 ckfini() 356 { 357 struct bufarea *bp, *nbp; 358 int cnt = 0; 359 360 for (bp = bufhead.b_prev; bp && bp != &bufhead; bp = nbp) { 361 cnt++; 362 flush(fswritefd, bp); 363 nbp = bp->b_prev; 364 free(bp->b_un.b_buf); 365 free((char *)bp); 366 } 367 pbp = pdirbp = NULL; 368 if (bufhead.b_size != cnt) 369 errexit(gettext("Panic: lost %d buffers\n"), 370 bufhead.b_size - cnt); 371 if (debug) 372 (void) printf("cache missed %ld of %ld (%ld%%)\n", 373 diskreads, totalreads, 374 totalreads ? diskreads * 100 / totalreads : 0); 375 (void) close(fsreadfd); 376 (void) close(fswritefd); 377 } 378 379 int32_t 380 bread(int fd, char *buf, daddr_t blk, long size) 381 { 382 char *cp; 383 int i, errs; 384 offset_t offset = ldbtob(blk); 385 offset_t addr; 386 387 if (llseek(fd, offset, 0) < 0) 388 rwerror(gettext("SEEK"), blk); 389 else if (read(fd, buf, (int)size) == size) 390 return (0); 391 rwerror(gettext("READ"), blk); 392 if (llseek(fd, offset, 0) < 0) 393 rwerror(gettext("SEEK"), blk); 394 errs = 0; 395 bzero(buf, (int)size); 396 pwarn(gettext("THE FOLLOWING SECTORS COULD NOT BE READ:")); 397 for (cp = buf, i = 0; i < btodb(size); i++, cp += DEV_BSIZE) { 398 addr = ldbtob(blk + i); 399 if (llseek(fd, addr, SEEK_CUR) < 0 || 400 read(fd, cp, (int)secsize) < 0) { 401 (void) printf(" %ld", blk + i); 402 errs++; 403 } 404 } 405 (void) printf("\n"); 406 return (errs); 407 } 408 409 void 410 bwrite(int fd, char *buf, daddr_t blk, long size) 411 { 412 int i, n; 413 char *cp; 414 offset_t offset = ldbtob(blk); 415 offset_t addr; 416 417 if (fd < 0) 418 return; 419 if (llseek(fd, offset, 0) < 0) 420 rwerror(gettext("SEEK"), blk); 421 else if (write(fd, buf, (int)size) == size) { 422 fsmodified = 1; 423 return; 424 } 425 rwerror(gettext("WRITE"), blk); 426 if (llseek(fd, offset, 0) < 0) 427 rwerror(gettext("SEEK"), blk); 428 pwarn(gettext("THE FOLLOWING SECTORS COULD NOT BE WRITTEN:")); 429 for (cp = buf, i = 0; i < btodb(size); i++, cp += DEV_BSIZE) { 430 n = 0; 431 addr = ldbtob(blk + i); 432 if (llseek(fd, addr, SEEK_CUR) < 0 || 433 (n = write(fd, cp, DEV_BSIZE)) < 0) { 434 (void) printf(" %ld", blk + i); 435 } else if (n > 0) { 436 fsmodified = 1; 437 } 438 439 } 440 (void) printf("\n"); 441 } 442 443 void 444 catch() 445 { 446 ckfini(); 447 exit(37); 448 } 449 450 /* 451 * When preening, allow a single quit to signal 452 * a special exit after filesystem checks complete 453 * so that reboot sequence may be interrupted. 454 */ 455 void 456 catchquit() 457 { 458 extern int returntosingle; 459 460 (void) printf(gettext("returning to single-user after filesystem " 461 "check\n")); 462 returntosingle = 1; 463 (void) signal(SIGQUIT, SIG_DFL); 464 } 465 466 /* 467 * determine whether an inode should be fixed. 468 */ 469 /* ARGSUSED1 */ 470 int32_t 471 dofix(struct inodesc *idesc, char *msg) 472 { 473 474 switch (idesc->id_fix) { 475 476 case DONTKNOW: 477 pwarn(msg); 478 if (preen) { 479 (void) printf(gettext(" (SALVAGED)\n")); 480 idesc->id_fix = FIX; 481 return (ALTERED); 482 } 483 if (reply(gettext("SALVAGE")) == 0) { 484 idesc->id_fix = NOFIX; 485 return (0); 486 } 487 idesc->id_fix = FIX; 488 return (ALTERED); 489 490 case FIX: 491 return (ALTERED); 492 493 case NOFIX: 494 return (0); 495 496 default: 497 errexit(gettext("UNKNOWN INODESC FIX MODE %d\n"), 498 idesc->id_fix); 499 } 500 /* NOTREACHED */ 501 } 502 503 /* 504 * Check to see if unraw version of name is already mounted. 505 * Since we do not believe /etc/mnttab, we stat the mount point 506 * to see if it is really looks mounted. 507 */ 508 int 509 mounted(char *name) 510 { 511 int found = 0; 512 struct mnttab mnt; 513 FILE *mnttab; 514 struct stat device_stat, mount_stat; 515 char *blkname, *unrawname(); 516 int err; 517 518 mnttab = fopen(MNTTAB, "r"); 519 if (mnttab == NULL) { 520 (void) printf(gettext("can't open %s\n"), MNTTAB); 521 return (0); 522 } 523 blkname = unrawname(name); 524 while ((getmntent(mnttab, &mnt)) == 0) { 525 if (strcmp(mnt.mnt_fstype, MNTTYPE_UDFS) != 0) { 526 continue; 527 } 528 if (strcmp(blkname, mnt.mnt_special) == 0) { 529 err = stat(mnt.mnt_mountp, &mount_stat); 530 err |= stat(mnt.mnt_special, &device_stat); 531 if (err < 0) 532 continue; 533 if (device_stat.st_rdev == mount_stat.st_dev) { 534 (void) strncpy(mnt.mnt_mountp, mountpoint, 535 sizeof (mountpoint)); 536 if (hasmntopt(&mnt, MNTOPT_RO) != 0) 537 found = 2; /* mounted as RO */ 538 else 539 found = 1; /* mounted as R/W */ 540 } 541 break; 542 } 543 } 544 (void) fclose(mnttab); 545 return (found); 546 } 547 548 /* 549 * Check to see if name corresponds to an entry in vfstab, and that the entry 550 * does not have option ro. 551 */ 552 int 553 writable(char *name) 554 { 555 int rw = 1; 556 struct vfstab vfsbuf; 557 FILE *vfstab; 558 char *blkname, *unrawname(); 559 560 vfstab = fopen(VFSTAB, "r"); 561 if (vfstab == NULL) { 562 (void) printf(gettext("can't open %s\n"), VFSTAB); 563 return (1); 564 } 565 blkname = unrawname(name); 566 if ((getvfsspec(vfstab, &vfsbuf, blkname) == 0) && 567 (vfsbuf.vfs_fstype != NULL) && 568 (strcmp(vfsbuf.vfs_fstype, MNTTYPE_UDFS) == 0) && 569 (hasvfsopt(&vfsbuf, MNTOPT_RO))) { 570 rw = 0; 571 } 572 (void) fclose(vfstab); 573 return (rw); 574 } 575 576 /* 577 * print out clean info 578 */ 579 void 580 printclean() 581 { 582 char *s; 583 584 switch (lvintp->lvid_int_type) { 585 586 case LVI_CLOSE: 587 s = gettext("clean"); 588 break; 589 590 case LVI_OPEN: 591 s = gettext("active"); 592 break; 593 594 default: 595 s = gettext("unknown"); 596 } 597 598 if (preen) 599 pwarn(gettext("is %s.\n"), s); 600 else 601 (void) printf("** %s is %s.\n", devname, s); 602 } 603