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