1 /* $NetBSD: utilities.c,v 1.18 2008/11/24 18:05:25 tsutsui Exp $ */ 2 3 /* 4 * Copyright (c) 1980, 1986, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. Neither the name of the University nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32 /* 33 * Copyright (c) 1997 Manuel Bouyer. 34 * 35 * Redistribution and use in source and binary forms, with or without 36 * modification, are permitted provided that the following conditions 37 * are met: 38 * 1. Redistributions of source code must retain the above copyright 39 * notice, this list of conditions and the following disclaimer. 40 * 2. Redistributions in binary form must reproduce the above copyright 41 * notice, this list of conditions and the following disclaimer in the 42 * documentation and/or other materials provided with the distribution. 43 * 3. All advertising materials mentioning features or use of this software 44 * must display the following acknowledgement: 45 * This product includes software developed by Manuel Bouyer. 46 * 4. The name of the author may not be used to endorse or promote products 47 * derived from this software without specific prior written permission. 48 * 49 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 50 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 51 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 52 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 53 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 54 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 55 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 56 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 57 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 58 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 59 */ 60 61 #include <sys/cdefs.h> 62 #ifndef lint 63 #if 0 64 static char sccsid[] = "@(#)utilities.c 8.1 (Berkeley) 6/5/93"; 65 #else 66 __RCSID("$NetBSD: utilities.c,v 1.18 2008/11/24 18:05:25 tsutsui Exp $"); 67 #endif 68 #endif /* not lint */ 69 70 #include <sys/param.h> 71 #include <sys/time.h> 72 #include <ufs/ext2fs/ext2fs_dinode.h> 73 #include <ufs/ext2fs/ext2fs_dir.h> 74 #include <ufs/ext2fs/ext2fs.h> 75 #include <ufs/ufs/dinode.h> /* for IFMT & friends */ 76 #include <stdio.h> 77 #include <stdlib.h> 78 #include <string.h> 79 #include <ctype.h> 80 #include <unistd.h> 81 #include <signal.h> 82 83 #include "fsutil.h" 84 #include "fsck.h" 85 #include "extern.h" 86 #include "exitvalues.h" 87 88 long diskreads, totalreads; /* Disk cache statistics */ 89 90 static void rwerror(const char *, daddr_t); 91 92 extern int returntosingle; 93 94 int 95 ftypeok(struct ext2fs_dinode *dp) 96 { 97 switch (fs2h16(dp->e2di_mode) & IFMT) { 98 99 case IFDIR: 100 case IFREG: 101 case IFBLK: 102 case IFCHR: 103 case IFLNK: 104 case IFSOCK: 105 case IFIFO: 106 return (1); 107 108 default: 109 if (debug) 110 printf("bad file type 0%o\n", fs2h16(dp->e2di_mode)); 111 return (0); 112 } 113 } 114 115 int 116 reply(const char *question) 117 { 118 int persevere; 119 char c; 120 121 if (preen) 122 pfatal("INTERNAL ERROR: GOT TO reply()"); 123 persevere = !strcmp(question, "CONTINUE"); 124 printf("\n"); 125 if (!persevere && (nflag || fswritefd < 0)) { 126 printf("%s? no\n\n", question); 127 return (0); 128 } 129 if (yflag || (persevere && nflag)) { 130 printf("%s? yes\n\n", question); 131 return (1); 132 } 133 do { 134 printf("%s? [yn] ", question); 135 (void) fflush(stdout); 136 c = getc(stdin); 137 while (c != '\n' && getc(stdin) != '\n') 138 if (feof(stdin)) 139 return (0); 140 } while (c != 'y' && c != 'Y' && c != 'n' && c != 'N'); 141 printf("\n"); 142 if (c == 'y' || c == 'Y') 143 return (1); 144 return (0); 145 } 146 147 /* 148 * Malloc buffers and set up cache. 149 */ 150 void 151 bufinit(void) 152 { 153 struct bufarea *bp; 154 long bufcnt, i; 155 char *bufp; 156 157 diskreads = totalreads = 0; 158 pbp = pdirbp = (struct bufarea *)0; 159 bufhead.b_next = bufhead.b_prev = &bufhead; 160 bufcnt = MAXBUFSPACE / sblock.e2fs_bsize; 161 if (bufcnt < MINBUFS) 162 bufcnt = MINBUFS; 163 for (i = 0; i < bufcnt; i++) { 164 bp = malloc(sizeof(struct bufarea)); 165 bufp = malloc((unsigned int)sblock.e2fs_bsize); 166 if (bp == NULL || bufp == NULL) { 167 if (i >= MINBUFS) 168 break; 169 errexit("cannot allocate buffer pool"); 170 } 171 bp->b_un.b_buf = bufp; 172 bp->b_prev = &bufhead; 173 bp->b_next = bufhead.b_next; 174 bufhead.b_next->b_prev = bp; 175 bufhead.b_next = bp; 176 initbarea(bp); 177 } 178 bufhead.b_size = i; /* save number of buffers */ 179 } 180 181 /* 182 * Manage a cache of directory blocks. 183 */ 184 struct bufarea * 185 getdatablk(daddr_t blkno, long size) 186 { 187 struct bufarea *bp; 188 189 for (bp = bufhead.b_next; bp != &bufhead; bp = bp->b_next) 190 if (bp->b_bno == fsbtodb(&sblock, blkno)) 191 goto foundit; 192 for (bp = bufhead.b_prev; bp != &bufhead; bp = bp->b_prev) 193 if ((bp->b_flags & B_INUSE) == 0) 194 break; 195 if (bp == &bufhead) 196 errexit("deadlocked buffer pool"); 197 getblk(bp, blkno, size); 198 diskreads++; 199 /* fall through */ 200 foundit: 201 totalreads++; 202 bp->b_prev->b_next = bp->b_next; 203 bp->b_next->b_prev = bp->b_prev; 204 bp->b_prev = &bufhead; 205 bp->b_next = bufhead.b_next; 206 bufhead.b_next->b_prev = bp; 207 bufhead.b_next = bp; 208 bp->b_flags |= B_INUSE; 209 return (bp); 210 } 211 212 void 213 getblk(struct bufarea *bp, daddr_t blk, long size) 214 { 215 daddr_t dblk; 216 217 dblk = fsbtodb(&sblock, blk); 218 if (bp->b_bno != dblk) { 219 flush(fswritefd, bp); 220 bp->b_errs = bread(fsreadfd, bp->b_un.b_buf, dblk, size); 221 bp->b_bno = dblk; 222 bp->b_size = size; 223 } 224 } 225 226 void 227 flush(int fd, struct bufarea *bp) 228 { 229 int i; 230 231 if (!bp->b_dirty) 232 return; 233 if (bp->b_errs != 0) 234 pfatal("WRITING %sZERO'ED BLOCK %lld TO DISK\n", 235 (bp->b_errs == bp->b_size / dev_bsize) ? "" : "PARTIALLY ", 236 (long long)bp->b_bno); 237 bp->b_dirty = 0; 238 bp->b_errs = 0; 239 bwrite(fd, bp->b_un.b_buf, bp->b_bno, (long)bp->b_size); 240 if (bp != &sblk) 241 return; 242 for (i = 0; i < sblock.e2fs_ngdb; i++) { 243 bwrite(fswritefd, (char *) 244 &sblock.e2fs_gd[i* sblock.e2fs_bsize / sizeof(struct ext2_gd)], 245 fsbtodb(&sblock, ((sblock.e2fs_bsize>1024)?0:1)+i+1), 246 sblock.e2fs_bsize); 247 } 248 } 249 250 static void 251 rwerror(const char *mesg, daddr_t blk) 252 { 253 254 if (preen == 0) 255 printf("\n"); 256 pfatal("CANNOT %s: BLK %lld", mesg, (long long)blk); 257 if (reply("CONTINUE") == 0) 258 errexit("Program terminated"); 259 } 260 261 void 262 ckfini(int markclean) 263 { 264 struct bufarea *bp, *nbp; 265 int cnt = 0; 266 267 if (fswritefd < 0) { 268 (void)close(fsreadfd); 269 return; 270 } 271 flush(fswritefd, &sblk); 272 if (havesb && sblk.b_bno != SBOFF / dev_bsize && 273 !preen && reply("UPDATE STANDARD SUPERBLOCKS")) { 274 sblk.b_bno = SBOFF / dev_bsize; 275 sbdirty(); 276 flush(fswritefd, &sblk); 277 copyback_sb(&asblk); 278 asblk.b_dirty = 1; 279 flush(fswritefd, &asblk); 280 } 281 for (bp = bufhead.b_prev; bp && bp != &bufhead; bp = nbp) { 282 cnt++; 283 flush(fswritefd, bp); 284 nbp = bp->b_prev; 285 free(bp->b_un.b_buf); 286 free(bp); 287 } 288 if (bufhead.b_size != cnt) 289 errexit("Panic: lost %d buffers", bufhead.b_size - cnt); 290 pbp = pdirbp = (struct bufarea *)0; 291 if (markclean && (sblock.e2fs.e2fs_state & E2FS_ISCLEAN) == 0) { 292 /* 293 * Mark the file system as clean, and sync the superblock. 294 */ 295 if (preen) 296 pwarn("MARKING FILE SYSTEM CLEAN\n"); 297 else if (!reply("MARK FILE SYSTEM CLEAN")) 298 markclean = 0; 299 if (markclean) { 300 sblock.e2fs.e2fs_state = E2FS_ISCLEAN; 301 sbdirty(); 302 flush(fswritefd, &sblk); 303 } 304 } 305 if (debug) 306 printf("cache missed %ld of %ld (%d%%)\n", diskreads, 307 totalreads, (int)(diskreads * 100 / totalreads)); 308 (void)close(fsreadfd); 309 (void)close(fswritefd); 310 } 311 312 int 313 bread(int fd, char *buf, daddr_t blk, long size) 314 { 315 char *cp; 316 int i, errs; 317 off_t offset; 318 319 offset = blk; 320 offset *= dev_bsize; 321 if (lseek(fd, offset, 0) < 0) 322 rwerror("SEEK", blk); 323 else if (read(fd, buf, (int)size) == size) 324 return (0); 325 rwerror("READ", blk); 326 if (lseek(fd, offset, 0) < 0) 327 rwerror("SEEK", blk); 328 errs = 0; 329 memset(buf, 0, (size_t)size); 330 printf("THE FOLLOWING DISK SECTORS COULD NOT BE READ:"); 331 for (cp = buf, i = 0; i < size; i += secsize, cp += secsize) { 332 if (read(fd, cp, (int)secsize) != secsize) { 333 (void)lseek(fd, offset + i + secsize, 0); 334 if (secsize != dev_bsize && dev_bsize != 1) 335 printf(" %lld (%lld),", 336 (long long)((blk*dev_bsize + i) / secsize), 337 (long long)(blk + i / dev_bsize)); 338 else 339 printf(" %lld,", (long long)(blk + 340 i / dev_bsize)); 341 errs++; 342 } 343 } 344 printf("\n"); 345 return (errs); 346 } 347 348 void 349 bwrite(int fd, char *buf, daddr_t blk, long size) 350 { 351 int i; 352 char *cp; 353 off_t offset; 354 355 if (fd < 0) 356 return; 357 offset = blk; 358 offset *= dev_bsize; 359 if (lseek(fd, offset, 0) < 0) 360 rwerror("SEEK", blk); 361 else if (write(fd, buf, (int)size) == size) { 362 fsmodified = 1; 363 return; 364 } 365 rwerror("WRITE", blk); 366 if (lseek(fd, offset, 0) < 0) 367 rwerror("SEEK", blk); 368 printf("THE FOLLOWING SECTORS COULD NOT BE WRITTEN:"); 369 for (cp = buf, i = 0; i < size; i += dev_bsize, cp += dev_bsize) 370 if (write(fd, cp, (int)dev_bsize) != dev_bsize) { 371 (void)lseek(fd, offset + i + dev_bsize, 0); 372 printf(" %lld,", (long long)(blk + i / dev_bsize)); 373 } 374 printf("\n"); 375 return; 376 } 377 378 /* 379 * allocate a data block 380 */ 381 int 382 allocblk(void) 383 { 384 int i; 385 386 for (i = 0; i < maxfsblock - 1; i++) { 387 if (testbmap(i)) 388 continue; 389 setbmap(i); 390 n_blks++; 391 return (i); 392 } 393 return (0); 394 } 395 396 /* 397 * Free a previously allocated block 398 */ 399 void 400 freeblk(daddr_t blkno) 401 { 402 struct inodesc idesc; 403 404 idesc.id_blkno = blkno; 405 idesc.id_numfrags = 1; 406 (void)pass4check(&idesc); 407 } 408 409 /* 410 * Find a pathname 411 */ 412 void 413 getpathname(char *namebuf, size_t namebuflen, ino_t curdir, ino_t ino) 414 { 415 int len; 416 char *cp; 417 struct inodesc idesc; 418 static int busy = 0; 419 420 if (curdir == ino && ino == EXT2_ROOTINO) { 421 (void)strlcpy(namebuf, "/", namebuflen); 422 return; 423 } 424 if (busy || 425 (statemap[curdir] != DSTATE && statemap[curdir] != DFOUND)) { 426 (void)strlcpy(namebuf, "?", namebuflen); 427 return; 428 } 429 busy = 1; 430 memset(&idesc, 0, sizeof(struct inodesc)); 431 idesc.id_type = DATA; 432 idesc.id_fix = IGNORE; 433 cp = &namebuf[MAXPATHLEN - 1]; 434 *cp = '\0'; 435 if (curdir != ino) { 436 idesc.id_parent = curdir; 437 goto namelookup; 438 } 439 while (ino != EXT2_ROOTINO) { 440 idesc.id_number = ino; 441 idesc.id_func = findino; 442 idesc.id_name = ".."; 443 if ((ckinode(ginode(ino), &idesc) & FOUND) == 0) 444 break; 445 namelookup: 446 idesc.id_number = idesc.id_parent; 447 idesc.id_parent = ino; 448 idesc.id_func = findname; 449 idesc.id_name = namebuf; 450 if ((ckinode(ginode(idesc.id_number), &idesc)&FOUND) == 0) 451 break; 452 len = strlen(namebuf); 453 cp -= len; 454 memcpy(cp, namebuf, (size_t)len); 455 *--cp = '/'; 456 if (cp < &namebuf[EXT2FS_MAXNAMLEN]) 457 break; 458 ino = idesc.id_number; 459 } 460 busy = 0; 461 if (ino != EXT2_ROOTINO) 462 *--cp = '?'; 463 memcpy(namebuf, cp, (size_t)(&namebuf[MAXPATHLEN] - cp)); 464 } 465 466 void 467 catch(int n) 468 { 469 ckfini(0); 470 exit(FSCK_EXIT_SIGNALLED); 471 } 472 473 /* 474 * When preening, allow a single quit to signal 475 * a special exit after filesystem checks complete 476 * so that reboot sequence may be interrupted. 477 */ 478 void 479 catchquit(int n) 480 { 481 printf("returning to single-user after filesystem check\n"); 482 returntosingle = 1; 483 (void)signal(SIGQUIT, SIG_DFL); 484 } 485 486 /* 487 * Ignore a single quit signal; wait and flush just in case. 488 * Used by child processes in preen. 489 */ 490 void 491 voidquit(int n) 492 { 493 494 sleep(1); 495 (void)signal(SIGQUIT, SIG_IGN); 496 (void)signal(SIGQUIT, SIG_DFL); 497 } 498 499 /* 500 * determine whether an inode should be fixed. 501 */ 502 int 503 dofix(struct inodesc *idesc, const char *msg) 504 { 505 506 switch (idesc->id_fix) { 507 508 case DONTKNOW: 509 if (idesc->id_type == DATA) 510 direrror(idesc->id_number, msg); 511 else 512 pwarn("%s", msg); 513 if (preen) { 514 printf(" (SALVAGED)\n"); 515 idesc->id_fix = FIX; 516 return (ALTERED); 517 } 518 if (reply("SALVAGE") == 0) { 519 idesc->id_fix = NOFIX; 520 return (0); 521 } 522 idesc->id_fix = FIX; 523 return (ALTERED); 524 525 case FIX: 526 return (ALTERED); 527 528 case NOFIX: 529 case IGNORE: 530 return (0); 531 532 default: 533 errexit("UNKNOWN INODESC FIX MODE %d", idesc->id_fix); 534 } 535 /* NOTREACHED */ 536 } 537