1 /* $NetBSD: utilities.c,v 1.4 1998/03/30 02:07:59 mrg 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.4 1998/03/30 02:07:59 mrg 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 int 67 ftypeok(dp) 68 struct ext2fs_dinode *dp; 69 { 70 switch (fs2h16(dp->e2di_mode) & IFMT) { 71 72 case IFDIR: 73 case IFREG: 74 case IFBLK: 75 case IFCHR: 76 case IFLNK: 77 case IFSOCK: 78 case IFIFO: 79 return (1); 80 81 default: 82 if (debug) 83 printf("bad file type 0%o\n", fs2h16(dp->e2di_mode)); 84 return (0); 85 } 86 } 87 88 int 89 reply(question) 90 char *question; 91 { 92 int persevere; 93 char c; 94 95 if (preen) 96 pfatal("INTERNAL ERROR: GOT TO reply()"); 97 persevere = !strcmp(question, "CONTINUE"); 98 printf("\n"); 99 if (!persevere && (nflag || fswritefd < 0)) { 100 printf("%s? no\n\n", question); 101 return (0); 102 } 103 if (yflag || (persevere && nflag)) { 104 printf("%s? yes\n\n", question); 105 return (1); 106 } 107 do { 108 printf("%s? [yn] ", question); 109 (void) fflush(stdout); 110 c = getc(stdin); 111 while (c != '\n' && getc(stdin) != '\n') 112 if (feof(stdin)) 113 return (0); 114 } while (c != 'y' && c != 'Y' && c != 'n' && c != 'N'); 115 printf("\n"); 116 if (c == 'y' || c == 'Y') 117 return (1); 118 return (0); 119 } 120 121 /* 122 * Malloc buffers and set up cache. 123 */ 124 void 125 bufinit() 126 { 127 struct bufarea *bp; 128 long bufcnt, i; 129 char *bufp; 130 131 diskreads = totalreads = 0; 132 pbp = pdirbp = (struct bufarea *)0; 133 bufhead.b_next = bufhead.b_prev = &bufhead; 134 bufcnt = MAXBUFSPACE / sblock.e2fs_bsize; 135 if (bufcnt < MINBUFS) 136 bufcnt = MINBUFS; 137 for (i = 0; i < bufcnt; i++) { 138 bp = (struct bufarea *)malloc(sizeof(struct bufarea)); 139 bufp = malloc((unsigned int)sblock.e2fs_bsize); 140 if (bp == NULL || bufp == NULL) { 141 if (i >= MINBUFS) 142 break; 143 errexit("cannot allocate buffer pool\n"); 144 } 145 bp->b_un.b_buf = bufp; 146 bp->b_prev = &bufhead; 147 bp->b_next = bufhead.b_next; 148 bufhead.b_next->b_prev = bp; 149 bufhead.b_next = bp; 150 initbarea(bp); 151 } 152 bufhead.b_size = i; /* save number of buffers */ 153 } 154 155 /* 156 * Manage a cache of directory blocks. 157 */ 158 struct bufarea * 159 getdatablk(blkno, size) 160 daddr_t blkno; 161 long size; 162 { 163 struct bufarea *bp; 164 165 for (bp = bufhead.b_next; bp != &bufhead; bp = bp->b_next) 166 if (bp->b_bno == fsbtodb(&sblock, blkno)) 167 goto foundit; 168 for (bp = bufhead.b_prev; bp != &bufhead; bp = bp->b_prev) 169 if ((bp->b_flags & B_INUSE) == 0) 170 break; 171 if (bp == &bufhead) 172 errexit("deadlocked buffer pool\n"); 173 getblk(bp, blkno, size); 174 diskreads++; 175 /* fall through */ 176 foundit: 177 totalreads++; 178 bp->b_prev->b_next = bp->b_next; 179 bp->b_next->b_prev = bp->b_prev; 180 bp->b_prev = &bufhead; 181 bp->b_next = bufhead.b_next; 182 bufhead.b_next->b_prev = bp; 183 bufhead.b_next = bp; 184 bp->b_flags |= B_INUSE; 185 return (bp); 186 } 187 188 void 189 getblk(bp, blk, size) 190 struct bufarea *bp; 191 daddr_t blk; 192 long size; 193 { 194 daddr_t dblk; 195 196 dblk = fsbtodb(&sblock, blk); 197 if (bp->b_bno != dblk) { 198 flush(fswritefd, bp); 199 bp->b_errs = bread(fsreadfd, bp->b_un.b_buf, dblk, size); 200 bp->b_bno = dblk; 201 bp->b_size = size; 202 } 203 } 204 205 void 206 flush(fd, bp) 207 int fd; 208 struct bufarea *bp; 209 { 210 int i; 211 212 if (!bp->b_dirty) 213 return; 214 if (bp->b_errs != 0) 215 pfatal("WRITING %sZERO'ED BLOCK %d TO DISK\n", 216 (bp->b_errs == bp->b_size / dev_bsize) ? "" : "PARTIALLY ", 217 bp->b_bno); 218 bp->b_dirty = 0; 219 bp->b_errs = 0; 220 bwrite(fd, bp->b_un.b_buf, bp->b_bno, (long)bp->b_size); 221 if (bp != &sblk) 222 return; 223 for (i = 0; i < sblock.e2fs_ngdb; i++) { 224 bwrite(fswritefd, (char *) 225 &sblock.e2fs_gd[i* sblock.e2fs_bsize / sizeof(struct ext2_gd)], 226 fsbtodb(&sblock, ((sblock.e2fs_bsize>1024)?0:1)+i+1), 227 sblock.e2fs_bsize); 228 } 229 } 230 231 static void 232 rwerror(mesg, blk) 233 char *mesg; 234 daddr_t blk; 235 { 236 237 if (preen == 0) 238 printf("\n"); 239 pfatal("CANNOT %s: BLK %d", mesg, blk); 240 if (reply("CONTINUE") == 0) 241 errexit("Program terminated\n"); 242 } 243 244 void 245 ckfini(markclean) 246 int markclean; 247 { 248 struct bufarea *bp, *nbp; 249 int cnt = 0; 250 251 if (fswritefd < 0) { 252 (void)close(fsreadfd); 253 return; 254 } 255 flush(fswritefd, &sblk); 256 if (havesb && sblk.b_bno != SBOFF / dev_bsize && 257 !preen && reply("UPDATE STANDARD SUPERBLOCKS")) { 258 sblk.b_bno = SBOFF / dev_bsize; 259 sbdirty(); 260 flush(fswritefd, &sblk); 261 copyback_sb(&asblk); 262 asblk.b_dirty = 1; 263 flush(fswritefd, &asblk); 264 } 265 for (bp = bufhead.b_prev; bp && bp != &bufhead; bp = nbp) { 266 cnt++; 267 flush(fswritefd, bp); 268 nbp = bp->b_prev; 269 free(bp->b_un.b_buf); 270 free((char *)bp); 271 } 272 if (bufhead.b_size != cnt) 273 errexit("Panic: lost %d buffers\n", bufhead.b_size - cnt); 274 pbp = pdirbp = (struct bufarea *)0; 275 if (markclean && (sblock.e2fs.e2fs_state & E2FS_ISCLEAN) == 0) { 276 /* 277 * Mark the file system as clean, and sync the superblock. 278 */ 279 if (preen) 280 pwarn("MARKING FILE SYSTEM CLEAN\n"); 281 else if (!reply("MARK FILE SYSTEM CLEAN")) 282 markclean = 0; 283 if (markclean) { 284 sblock.e2fs.e2fs_state = E2FS_ISCLEAN; 285 sbdirty(); 286 flush(fswritefd, &sblk); 287 } 288 } 289 if (debug) 290 printf("cache missed %ld of %ld (%d%%)\n", diskreads, 291 totalreads, (int)(diskreads * 100 / totalreads)); 292 (void)close(fsreadfd); 293 (void)close(fswritefd); 294 } 295 296 int 297 bread(fd, buf, blk, size) 298 int fd; 299 char *buf; 300 daddr_t blk; 301 long size; 302 { 303 char *cp; 304 int i, errs; 305 off_t offset; 306 307 offset = blk; 308 offset *= dev_bsize; 309 if (lseek(fd, offset, 0) < 0) 310 rwerror("SEEK", blk); 311 else if (read(fd, buf, (int)size) == size) 312 return (0); 313 rwerror("READ", blk); 314 if (lseek(fd, offset, 0) < 0) 315 rwerror("SEEK", blk); 316 errs = 0; 317 memset(buf, 0, (size_t)size); 318 printf("THE FOLLOWING DISK SECTORS COULD NOT BE READ:"); 319 for (cp = buf, i = 0; i < size; i += secsize, cp += secsize) { 320 if (read(fd, cp, (int)secsize) != secsize) { 321 (void)lseek(fd, offset + i + secsize, 0); 322 if (secsize != dev_bsize && dev_bsize != 1) 323 printf(" %ld (%ld),", 324 (blk * dev_bsize + i) / secsize, 325 blk + i / dev_bsize); 326 else 327 printf(" %ld,", blk + i / dev_bsize); 328 errs++; 329 } 330 } 331 printf("\n"); 332 return (errs); 333 } 334 335 void 336 bwrite(fd, buf, blk, size) 337 int fd; 338 char *buf; 339 daddr_t blk; 340 long size; 341 { 342 int i; 343 char *cp; 344 off_t offset; 345 346 if (fd < 0) 347 return; 348 offset = blk; 349 offset *= dev_bsize; 350 if (lseek(fd, offset, 0) < 0) 351 rwerror("SEEK", blk); 352 else if (write(fd, buf, (int)size) == size) { 353 fsmodified = 1; 354 return; 355 } 356 rwerror("WRITE", blk); 357 if (lseek(fd, offset, 0) < 0) 358 rwerror("SEEK", blk); 359 printf("THE FOLLOWING SECTORS COULD NOT BE WRITTEN:"); 360 for (cp = buf, i = 0; i < size; i += dev_bsize, cp += dev_bsize) 361 if (write(fd, cp, (int)dev_bsize) != dev_bsize) { 362 (void)lseek(fd, offset + i + dev_bsize, 0); 363 printf(" %ld,", blk + i / dev_bsize); 364 } 365 printf("\n"); 366 return; 367 } 368 369 /* 370 * allocate a data block 371 */ 372 int 373 allocblk() 374 { 375 int i; 376 377 for (i = 0; i < maxfsblock - 1; i++) { 378 if (testbmap(i)) 379 continue; 380 setbmap(i); 381 n_blks ++; 382 return (i); 383 } 384 return (0); 385 } 386 387 /* 388 * Free a previously allocated block 389 */ 390 void 391 freeblk(blkno) 392 daddr_t blkno; 393 { 394 struct inodesc idesc; 395 396 idesc.id_blkno = blkno; 397 idesc.id_numfrags = 1; 398 (void)pass4check(&idesc); 399 } 400 401 /* 402 * Find a pathname 403 */ 404 void 405 getpathname(namebuf, curdir, ino) 406 char *namebuf; 407 ino_t curdir, ino; 408 { 409 int len; 410 char *cp; 411 struct inodesc idesc; 412 static int busy = 0; 413 414 if (curdir == ino && ino == EXT2_ROOTINO) { 415 (void)strcpy(namebuf, "/"); 416 return; 417 } 418 if (busy || 419 (statemap[curdir] != DSTATE && statemap[curdir] != DFOUND)) { 420 (void)strcpy(namebuf, "?"); 421 return; 422 } 423 busy = 1; 424 memset(&idesc, 0, sizeof(struct inodesc)); 425 idesc.id_type = DATA; 426 idesc.id_fix = IGNORE; 427 cp = &namebuf[MAXPATHLEN - 1]; 428 *cp = '\0'; 429 if (curdir != ino) { 430 idesc.id_parent = curdir; 431 goto namelookup; 432 } 433 while (ino != EXT2_ROOTINO) { 434 idesc.id_number = ino; 435 idesc.id_func = findino; 436 idesc.id_name = ".."; 437 if ((ckinode(ginode(ino), &idesc) & FOUND) == 0) 438 break; 439 namelookup: 440 idesc.id_number = idesc.id_parent; 441 idesc.id_parent = ino; 442 idesc.id_func = findname; 443 idesc.id_name = namebuf; 444 if ((ckinode(ginode(idesc.id_number), &idesc)&FOUND) == 0) 445 break; 446 len = strlen(namebuf); 447 cp -= len; 448 memcpy(cp, namebuf, (size_t)len); 449 *--cp = '/'; 450 if (cp < &namebuf[EXT2FS_MAXNAMLEN]) 451 break; 452 ino = idesc.id_number; 453 } 454 busy = 0; 455 if (ino != EXT2_ROOTINO) 456 *--cp = '?'; 457 memcpy(namebuf, cp, (size_t)(&namebuf[MAXPATHLEN] - cp)); 458 } 459 460 void 461 catch(n) 462 int n; 463 { 464 ckfini(0); 465 exit(12); 466 } 467 468 /* 469 * When preening, allow a single quit to signal 470 * a special exit after filesystem checks complete 471 * so that reboot sequence may be interrupted. 472 */ 473 void 474 catchquit(n) 475 int n; 476 { 477 extern int returntosingle; 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(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