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