1 /*- 2 * Copyright (c) 1992 The Regents of the University of California. 3 * All rights reserved. 4 * 5 * %sccs.include.redist.c% 6 */ 7 8 #ifndef lint 9 static char sccsid[] = "@(#)library.c 5.5 (Berkeley) 09/03/92"; 10 #endif /* not lint */ 11 12 #include <sys/param.h> 13 #include <sys/time.h> 14 #include <sys/stat.h> 15 #include <sys/mount.h> 16 17 #include <ufs/ufs/dinode.h> 18 #include <ufs/lfs/lfs.h> 19 20 #include <fcntl.h> 21 #include <stdio.h> 22 #include <unistd.h> 23 #include <stdlib.h> 24 #include <string.h> 25 #include "clean.h" 26 27 void add_blocks __P((FS_INFO *, BLOCK_INFO *, int *, SEGSUM *, caddr_t, 28 daddr_t, daddr_t)); 29 void add_inodes __P((FS_INFO *, BLOCK_INFO *, int *, SEGSUM *, caddr_t, 30 daddr_t)); 31 int bi_compare __P((const void *, const void *)); 32 int bi_toss __P((const void *, const void *, const void *)); 33 void get_ifile __P((FS_INFO *)); 34 int get_superblock __P((FS_INFO *, struct lfs *)); 35 int pseg_valid __P((FS_INFO *, SEGSUM *)); 36 37 /* 38 * This function will get information on all mounted file systems 39 * of a given type. 40 */ 41 int 42 fs_getmntinfo(buf, type) 43 struct statfs **buf; 44 int type; 45 { 46 struct statfs *tstatfsp; 47 struct statfs *sbp; 48 int count, i, tcount; 49 50 tcount = getmntinfo(&tstatfsp, MNT_NOWAIT); 51 52 if (tcount < 0) { 53 err(0, "getmntinfo failed"); 54 return (-1); 55 } 56 57 for (count = 0, i = 0; i < tcount ; ++i) 58 if (tstatfsp[i].f_type == type) 59 ++count; 60 61 if (count) { 62 if (!(*buf = (struct statfs *) 63 malloc(count * sizeof(struct statfs)))) 64 err(1, "fs_getmntinfo: out of space"); 65 for (i = 0, sbp = *buf; i < tcount ; ++i) { 66 if (tstatfsp[i].f_type == type) { 67 *sbp = tstatfsp[i]; 68 ++sbp; 69 } 70 } 71 } 72 return (count); 73 } 74 75 /* 76 * Get all the information available on an LFS file system. 77 * Returns an array of FS_INFO structures, NULL on error. 78 */ 79 FS_INFO * 80 get_fs_info (lstatfsp, count) 81 struct statfs *lstatfsp; /* IN: array of statfs structs */ 82 int count; /* IN: number of file systems */ 83 { 84 FS_INFO *fp, *fsp; 85 int i; 86 87 fsp = (FS_INFO *)malloc(count * sizeof(FS_INFO)); 88 89 for (fp = fsp, i = 0; i < count; ++i, ++fp) { 90 fp->fi_statfsp = lstatfsp++; 91 if (get_superblock (fp, &fp->fi_lfs)) 92 err(1, "get_fs_info: get_superblock failed"); 93 fp->fi_daddr_shift = 94 fp->fi_lfs.lfs_bshift - fp->fi_lfs.lfs_fsbtodb; 95 get_ifile (fp); 96 } 97 return (fsp); 98 } 99 100 /* 101 * If we are reading the ifile then we need to refresh it. Even if 102 * we are mmapping it, it might have grown. Finally, we need to 103 * refresh the file system information (statfs) info. 104 */ 105 void 106 reread_fs_info(fsp, count) 107 FS_INFO *fsp; /* IN: array of fs_infos to free */ 108 int count; /* IN: number of file systems */ 109 { 110 int i; 111 112 for (i = 0; i < count; ++i, ++fsp) { 113 if (statfs(fsp->fi_statfsp->f_mntonname, fsp->fi_statfsp)) 114 err(0, "reread_fs_info: statfs failed"); 115 #ifdef MMAP_WORKS 116 if (munmap(fsp->fi_cip, fsp->fi_ifile_length) < 0) 117 err(0, "reread_fs_info: munmap failed"); 118 #else 119 free (fsp->fi_cip); 120 #endif /* MMAP_WORKS */ 121 get_ifile (fsp); 122 } 123 } 124 125 /* 126 * Gets the superblock from disk (possibly in face of errors) 127 */ 128 int 129 get_superblock (fsp, sbp) 130 FS_INFO *fsp; /* local file system info structure */ 131 struct lfs *sbp; 132 { 133 char mntfromname[MNAMELEN+1]; 134 int fid; 135 136 strcpy(mntfromname, "/dev/r"); 137 strcat(mntfromname, fsp->fi_statfsp->f_mntfromname+5); 138 139 if ((fid = open(mntfromname, O_RDONLY, (mode_t)0)) < 0) { 140 err(0, "get_superblock: bad open"); 141 return (-1); 142 } 143 144 get(fid, LFS_LABELPAD, sbp, sizeof(struct lfs)); 145 close (fid); 146 147 return (0); 148 } 149 150 /* 151 * This function will map the ifile into memory. It causes a 152 * fatal error on failure. 153 */ 154 void 155 get_ifile (fsp) 156 FS_INFO *fsp; 157 { 158 struct stat file_stat; 159 caddr_t ifp; 160 char *ifile_name; 161 int count, fid; 162 163 ifp = NULL; 164 ifile_name = malloc(strlen(fsp->fi_statfsp->f_mntonname) + 165 strlen(IFILE_NAME)+2); 166 strcat(strcat(strcpy(ifile_name, fsp->fi_statfsp->f_mntonname), "/"), 167 IFILE_NAME); 168 169 if ((fid = open(ifile_name, O_RDWR, (mode_t)0)) < 0) 170 err(1, "get_ifile: bad open"); 171 172 if (fstat (fid, &file_stat)) 173 err(1, "get_ifile: fstat failed"); 174 175 fsp->fi_ifile_length = file_stat.st_size; 176 177 /* get the ifile */ 178 #ifndef MMAP_WORKS 179 if (!(ifp = malloc ((size_t)fsp->fi_ifile_length))) 180 err (1, "get_ifile: malloc failed"); 181 redo_read: 182 count = read (fid, ifp, (size_t) fsp->fi_ifile_length); 183 184 if (count < 0) 185 err(1, "get_ifile: bad ifile read"); 186 else if (count < (int)fsp->fi_ifile_length) { 187 err(0, "get_ifile"); 188 if (lseek(fid, 0, SEEK_SET) < 0) 189 err(1, "get_ifile: bad ifile lseek"); 190 goto redo_read; 191 } 192 #else /* MMAP_WORKS */ 193 ifp = mmap ((caddr_t)0, (size_t) fsp->fi_ifile_length, PROT_READ|PROT_WRITE, 194 MAP_FILE|MAP_SHARED, fid, (off_t)0); 195 if (ifp < 0) 196 err(1, "get_ifile: mmap failed"); 197 #endif /* MMAP_WORKS */ 198 199 close (fid); 200 201 fsp->fi_cip = (CLEANERINFO *)ifp; 202 fsp->fi_segusep = (SEGUSE *)(ifp + CLEANSIZE(fsp)); 203 fsp->fi_ifilep = (IFILE *)((caddr_t)fsp->fi_segusep + SEGTABSIZE(fsp)); 204 205 /* 206 * The number of ifile entries is equal to the number of blocks 207 * blocks in the ifile minus the ones allocated to cleaner info 208 * and segment usage table multiplied by the number of ifile 209 * entries per page. 210 */ 211 fsp->fi_ifile_count = (fsp->fi_ifile_length >> fsp->fi_lfs.lfs_bshift - 212 fsp->fi_lfs.lfs_cleansz - fsp->fi_lfs.lfs_segtabsz) * 213 fsp->fi_lfs.lfs_ifpb; 214 215 free (ifile_name); 216 } 217 218 /* 219 * This function will scan a segment and return a list of 220 * <inode, blocknum> pairs which indicate which blocks were 221 * contained as live data within the segment when the segment 222 * summary was read (it may have "died" since then). Any given 223 * pair will be listed at most once. 224 */ 225 int 226 lfs_segmapv(fsp, seg, seg_buf, blocks, bcount) 227 FS_INFO *fsp; /* pointer to local file system information */ 228 int seg; /* the segment number */ 229 caddr_t seg_buf; /* the buffer containing the segment's data */ 230 BLOCK_INFO **blocks; /* OUT: array of block_info for live blocks */ 231 int *bcount; /* OUT: number of active blocks in segment */ 232 { 233 BLOCK_INFO *bip; 234 SEGSUM *sp; 235 SEGUSE *sup; 236 struct lfs *lfsp; 237 caddr_t s, segend; 238 daddr_t pseg_addr, seg_addr; 239 int nelem, nblocks; 240 time_t timestamp; 241 242 lfsp = &fsp->fi_lfs; 243 nelem = 2 * lfsp->lfs_ssize; 244 if (!(bip = malloc(nelem * sizeof(BLOCK_INFO)))) 245 goto err0; 246 247 sup = SEGUSE_ENTRY(lfsp, fsp->fi_segusep, seg); 248 s = seg_buf + (sup->su_flags & SEGUSE_SUPERBLOCK ? LFS_SBPAD : 0); 249 seg_addr = sntoda(lfsp, seg); 250 pseg_addr = seg_addr + (sup->su_flags & SEGUSE_SUPERBLOCK ? btodb(LFS_SBPAD) : 0); 251 #ifdef VERBOSE 252 printf("\tsegment buffer at: 0x%x\tseg_addr 0x%x\n", s, seg_addr); 253 #endif /* VERBOSE */ 254 255 *bcount = 0; 256 for (segend = seg_buf + seg_size(lfsp), timestamp = 0; s < segend; ) { 257 sp = (SEGSUM *)s; 258 #ifdef VERBOSE 259 printf("\tpartial at: 0x%x\n", pseg_addr); 260 print_SEGSUM(lfsp, sp); 261 fflush(stdout); 262 #endif /* VERBOSE */ 263 264 nblocks = pseg_valid(fsp, sp); 265 if (nblocks <= 0) 266 break; 267 268 /* Check if we have hit old data */ 269 if (timestamp > ((SEGSUM*)s)->ss_create) 270 break; 271 timestamp = ((SEGSUM*)s)->ss_create; 272 273 if (*bcount + nblocks + sp->ss_ninos > nelem) { 274 nelem = *bcount + nblocks + sp->ss_ninos; 275 bip = realloc (bip, nelem * sizeof(BLOCK_INFO)); 276 if (!bip) 277 goto err0; 278 } 279 add_blocks(fsp, bip, bcount, sp, seg_buf, seg_addr, pseg_addr); 280 add_inodes(fsp, bip, bcount, sp, seg_buf, seg_addr); 281 pseg_addr += fsbtodb(lfsp, nblocks) + 282 bytetoda(fsp, LFS_SUMMARY_SIZE); 283 s += (nblocks << lfsp->lfs_bshift) + LFS_SUMMARY_SIZE; 284 } 285 qsort(bip, *bcount, sizeof(BLOCK_INFO), bi_compare); 286 toss(bip, bcount, sizeof(BLOCK_INFO), bi_toss, NULL); 287 #ifdef VERBOSE 288 { 289 BLOCK_INFO *_bip; 290 int i; 291 292 printf("BLOCK INFOS\n"); 293 for (_bip = bip, i=0; i < *bcount; ++_bip, ++i) 294 PRINT_BINFO(_bip); 295 } 296 #endif 297 *blocks = bip; 298 return (0); 299 300 err0: *bcount = 0; 301 return (-1); 302 303 } 304 305 /* 306 * This will parse a partial segment and fill in BLOCK_INFO structures 307 * for each block described in the segment summary. It will not include 308 * blocks or inodes from files with new version numbers. 309 */ 310 void 311 add_blocks (fsp, bip, countp, sp, seg_buf, segaddr, psegaddr) 312 FS_INFO *fsp; /* pointer to super block */ 313 BLOCK_INFO *bip; /* Block info array */ 314 int *countp; /* IN/OUT: number of blocks in array */ 315 SEGSUM *sp; /* segment summmary pointer */ 316 caddr_t seg_buf; /* buffer containing segment */ 317 daddr_t segaddr; /* address of this segment */ 318 daddr_t psegaddr; /* address of this partial segment */ 319 { 320 IFILE *ifp; 321 FINFO *fip; 322 caddr_t bp; 323 daddr_t *dp, *iaddrp; 324 int db_per_block, i, j; 325 u_long page_size; 326 327 #ifdef VERBOSE 328 printf("FILE INFOS\n"); 329 #endif 330 db_per_block = fsbtodb(&fsp->fi_lfs, 1); 331 page_size = fsp->fi_lfs.lfs_bsize; 332 bp = seg_buf + datobyte(fsp, psegaddr - segaddr) + LFS_SUMMARY_SIZE; 333 bip += *countp; 334 psegaddr += bytetoda(fsp, LFS_SUMMARY_SIZE); 335 iaddrp = (daddr_t *)((caddr_t)sp + LFS_SUMMARY_SIZE); 336 --iaddrp; 337 for (fip = (FINFO *)(sp + 1), i = 0; i < sp->ss_nfinfo; 338 ++i, fip = (FINFO *)(&fip->fi_blocks[fip->fi_nblocks])) { 339 340 ifp = IFILE_ENTRY(&fsp->fi_lfs, fsp->fi_ifilep, fip->fi_ino); 341 PRINT_FINFO(fip, ifp); 342 if (ifp->if_version > fip->fi_version) 343 continue; 344 dp = &(fip->fi_blocks[0]); 345 for (j = 0; j < fip->fi_nblocks; j++, dp++) { 346 while (psegaddr == *iaddrp) { 347 psegaddr += db_per_block; 348 bp += page_size; 349 --iaddrp; 350 } 351 bip->bi_inode = fip->fi_ino; 352 bip->bi_lbn = *dp; 353 bip->bi_daddr = psegaddr; 354 bip->bi_segcreate = (time_t)(sp->ss_create); 355 bip->bi_bp = bp; 356 bip->bi_version = ifp->if_version; 357 psegaddr += db_per_block; 358 bp += page_size; 359 ++bip; 360 ++(*countp); 361 } 362 } 363 } 364 365 /* 366 * For a particular segment summary, reads the inode blocks and adds 367 * INODE_INFO structures to the array. Returns the number of inodes 368 * actually added. 369 */ 370 void 371 add_inodes (fsp, bip, countp, sp, seg_buf, seg_addr) 372 FS_INFO *fsp; /* pointer to super block */ 373 BLOCK_INFO *bip; /* block info array */ 374 int *countp; /* pointer to current number of inodes */ 375 SEGSUM *sp; /* segsum pointer */ 376 caddr_t seg_buf; /* the buffer containing the segment's data */ 377 daddr_t seg_addr; /* disk address of seg_buf */ 378 { 379 struct dinode *di; 380 struct lfs *lfsp; 381 IFILE *ifp; 382 BLOCK_INFO *bp; 383 daddr_t *daddrp; 384 ino_t inum; 385 int i; 386 387 if (sp->ss_ninos <= 0) 388 return; 389 390 bp = bip + *countp; 391 lfsp = &fsp->fi_lfs; 392 #ifdef VERBOSE 393 (void) printf("INODES:\n"); 394 #endif 395 daddrp = (daddr_t *)((caddr_t)sp + LFS_SUMMARY_SIZE); 396 for (i = 0; i < sp->ss_ninos; ++i) { 397 if (i % INOPB(lfsp) == 0) { 398 --daddrp; 399 di = (struct dinode *)(seg_buf + 400 ((*daddrp - seg_addr) << fsp->fi_daddr_shift)); 401 } else 402 ++di; 403 404 inum = di->di_inum; 405 bp->bi_lbn = LFS_UNUSED_LBN; 406 bp->bi_inode = inum; 407 bp->bi_daddr = *daddrp; 408 bp->bi_bp = di; 409 bp->bi_segcreate = sp->ss_create; 410 411 if (inum == LFS_IFILE_INUM) { 412 bp->bi_version = 1; /* Ifile version should be 1 */ 413 bp++; 414 ++(*countp); 415 PRINT_INODE(1, bp); 416 } else { 417 ifp = IFILE_ENTRY(lfsp, fsp->fi_ifilep, inum); 418 PRINT_INODE(ifp->if_daddr == *daddrp, bp); 419 bp->bi_version = ifp->if_version; 420 if (ifp->if_daddr == *daddrp) { 421 bp++; 422 ++(*countp); 423 } 424 } 425 } 426 } 427 428 /* 429 * Checks the summary checksum and the data checksum to determine if the 430 * segment is valid or not. Returns the size of the partial segment if it 431 * is valid, * and 0 otherwise. Use dump_summary to figure out size of the 432 * the partial as well as whether or not the checksum is valid. 433 */ 434 int 435 pseg_valid (fsp, ssp) 436 FS_INFO *fsp; /* pointer to file system info */ 437 SEGSUM *ssp; /* pointer to segment summary block */ 438 { 439 caddr_t p; 440 int i, nblocks; 441 u_long *datap; 442 443 if ((nblocks = dump_summary(&fsp->fi_lfs, ssp, 0, NULL)) <= 0) 444 return(0); 445 446 /* check data/inode block(s) checksum too */ 447 datap = (u_long *)malloc(nblocks * sizeof(u_long)); 448 p = (caddr_t)ssp + LFS_SUMMARY_SIZE; 449 for (i = 0; i < nblocks; ++i) { 450 datap[i] = *((u_long *)p); 451 p += fsp->fi_lfs.lfs_bsize; 452 } 453 if (cksum ((void *)datap, nblocks * sizeof(u_long)) != ssp->ss_datasum) 454 return (0); 455 456 return (nblocks); 457 } 458 459 460 /* #define MMAP_SEGMENT */ 461 /* 462 * read a segment into a memory buffer 463 */ 464 int 465 mmap_segment (fsp, segment, segbuf) 466 FS_INFO *fsp; /* file system information */ 467 int segment; /* segment number */ 468 caddr_t *segbuf; /* pointer to buffer area */ 469 { 470 struct lfs *lfsp; 471 int fid; /* fildes for file system device */ 472 daddr_t seg_daddr; /* base disk address of segment */ 473 off_t seg_byte; 474 size_t ssize; 475 char mntfromname[MNAMELEN+2]; 476 477 lfsp = &fsp->fi_lfs; 478 479 /* get the disk address of the beginning of the segment */ 480 seg_daddr = sntoda(lfsp, segment); 481 seg_byte = datobyte(fsp, seg_daddr); 482 ssize = seg_size(lfsp); 483 484 strcpy(mntfromname, "/dev/r"); 485 strcat(mntfromname, fsp->fi_statfsp->f_mntfromname+5); 486 487 if ((fid = open(mntfromname, O_RDONLY, (mode_t)0)) < 0) { 488 err(0, "mmap_segment: bad open"); 489 return (-1); 490 } 491 492 #ifdef MMAP_SEGMENT 493 *segbuf = mmap ((caddr_t)0, seg_size(lfsp), PROT_READ, 494 MAP_FILE, fid, seg_byte); 495 if (*(long *)segbuf < 0) { 496 err(0, "mmap_segment: mmap failed"); 497 return (NULL); 498 } 499 #else /* MMAP_SEGMENT */ 500 #ifdef VERBOSE 501 printf("mmap_segment\tseg_daddr: %lu\tseg_size: %lu\tseg_offset: %qu\n", 502 seg_daddr, ssize, seg_byte); 503 #endif 504 /* malloc the space for the buffer */ 505 *segbuf = malloc(ssize); 506 if (!*segbuf) { 507 err(0, "mmap_segment: malloc failed"); 508 return(NULL); 509 } 510 511 /* read the segment data into the buffer */ 512 if (lseek (fid, seg_byte, SEEK_SET) != seg_byte) { 513 err (0, "mmap_segment: bad lseek"); 514 free(*segbuf); 515 return (-1); 516 } 517 518 if (read (fid, *segbuf, ssize) != ssize) { 519 err (0, "mmap_segment: bad read"); 520 free(*segbuf); 521 return (-1); 522 } 523 #endif /* MMAP_SEGMENT */ 524 close (fid); 525 526 return (0); 527 } 528 529 void 530 munmap_segment (fsp, seg_buf) 531 FS_INFO *fsp; /* file system information */ 532 caddr_t seg_buf; /* pointer to buffer area */ 533 { 534 #ifdef MMAP_SEGMENT 535 munmap (seg_buf, seg_size(&fsp->fi_lfs)); 536 #else /* MMAP_SEGMENT */ 537 free (seg_buf); 538 #endif /* MMAP_SEGMENT */ 539 } 540 541 542 /* 543 * USEFUL DEBUGGING TOOLS: 544 */ 545 void 546 print_SEGSUM (lfsp, p) 547 struct lfs *lfsp; 548 SEGSUM *p; 549 { 550 if (p) 551 (void) dump_summary(lfsp, p, DUMP_ALL, NULL); 552 else printf("0x0"); 553 fflush(stdout); 554 } 555 556 int 557 bi_compare(a, b) 558 const void *a; 559 const void *b; 560 { 561 const BLOCK_INFO *ba, *bb; 562 int diff; 563 564 ba = a; 565 bb = b; 566 567 if (diff = (int)(ba->bi_inode - bb->bi_inode)) 568 return (diff); 569 if (diff = (int)(ba->bi_lbn - bb->bi_lbn)) { 570 if (ba->bi_lbn == LFS_UNUSED_LBN) 571 return(-1); 572 else if (bb->bi_lbn == LFS_UNUSED_LBN) 573 return(1); 574 else if (ba->bi_lbn < 0) 575 return(1); 576 else 577 return (diff); 578 } 579 if (diff = (int)(ba->bi_segcreate - bb->bi_segcreate)) 580 return (diff); 581 diff = (int)(ba->bi_daddr - bb->bi_daddr); 582 return (diff); 583 } 584 585 int 586 bi_toss(dummy, a, b) 587 const void *dummy; 588 const void *a; 589 const void *b; 590 { 591 const BLOCK_INFO *ba, *bb; 592 593 ba = a; 594 bb = b; 595 596 return(ba->bi_inode == bb->bi_inode && ba->bi_lbn == bb->bi_lbn); 597 } 598 599 void 600 toss(p, nump, size, dotoss, client) 601 void *p; 602 int *nump; 603 size_t size; 604 int (*dotoss) __P((const void *, const void *, const void *)); 605 void *client; 606 { 607 int i; 608 void *p1; 609 610 if (*nump == 0) 611 return; 612 613 for (i = *nump; --i > 0;) { 614 p1 = p + size; 615 if (dotoss(client, p, p1)) { 616 bcopy(p1, p, i * size); 617 --(*nump); 618 } else 619 p += size; 620 } 621 } 622