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