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.3 (Berkeley) 08/21/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 psegaddr += db_per_block; 357 bp += page_size; 358 ++bip; 359 ++(*countp); 360 } 361 } 362 } 363 364 /* 365 * For a particular segment summary, reads the inode blocks and adds 366 * INODE_INFO structures to the array. Returns the number of inodes 367 * actually added. 368 */ 369 void 370 add_inodes (fsp, bip, countp, sp, seg_buf, seg_addr) 371 FS_INFO *fsp; /* pointer to super block */ 372 BLOCK_INFO *bip; /* block info array */ 373 int *countp; /* pointer to current number of inodes */ 374 SEGSUM *sp; /* segsum pointer */ 375 caddr_t seg_buf; /* the buffer containing the segment's data */ 376 daddr_t seg_addr; /* disk address of seg_buf */ 377 { 378 struct dinode *di; 379 struct lfs *lfsp; 380 IFILE *ifp; 381 BLOCK_INFO *bp; 382 daddr_t *daddrp; 383 ino_t inum; 384 int i; 385 386 if (sp->ss_ninos <= 0) 387 return; 388 389 bp = bip + *countp; 390 lfsp = &fsp->fi_lfs; 391 #ifdef VERBOSE 392 (void) printf("INODES:\n"); 393 #endif 394 daddrp = (daddr_t *)((caddr_t)sp + LFS_SUMMARY_SIZE); 395 for (i = 0; i < sp->ss_ninos; ++i) { 396 if (i % INOPB(lfsp) == 0) { 397 --daddrp; 398 di = (struct dinode *)(seg_buf + 399 ((*daddrp - seg_addr) << fsp->fi_daddr_shift)); 400 } else 401 ++di; 402 403 inum = di->di_inum; 404 bp->bi_lbn = LFS_UNUSED_LBN; 405 bp->bi_inode = inum; 406 bp->bi_daddr = *daddrp; 407 bp->bi_bp = di; 408 bp->bi_segcreate = sp->ss_create; 409 410 ifp = IFILE_ENTRY(lfsp, fsp->fi_ifilep, inum); 411 PRINT_INODE(ifp->if_daddr == *daddrp, ip); 412 if (ifp->if_daddr == *daddrp) { 413 bp++; 414 ++(*countp); 415 } 416 } 417 } 418 419 /* 420 * Checks the summary checksum and the data checksum to determine if the 421 * segment is valid or not. Returns the size of the partial segment if it 422 * is valid, * and 0 otherwise. Use dump_summary to figure out size of the 423 * the partial as well as whether or not the checksum is valid. 424 */ 425 int 426 pseg_valid (fsp, ssp) 427 FS_INFO *fsp; /* pointer to file system info */ 428 SEGSUM *ssp; /* pointer to segment summary block */ 429 { 430 caddr_t p; 431 int i, nblocks; 432 u_long *datap; 433 434 if ((nblocks = dump_summary(&fsp->fi_lfs, ssp, 0, NULL)) <= 0) 435 return(0); 436 437 /* check data/inode block(s) checksum too */ 438 datap = (u_long *)malloc(nblocks * sizeof(u_long)); 439 p = (caddr_t)ssp + LFS_SUMMARY_SIZE; 440 for (i = 0; i < nblocks; ++i) { 441 datap[i] = *((u_long *)p); 442 p += fsp->fi_lfs.lfs_bsize; 443 } 444 if (cksum ((void *)datap, nblocks * sizeof(u_long)) != ssp->ss_datasum) 445 return (0); 446 447 return (nblocks); 448 } 449 450 451 /* #define MMAP_SEGMENT */ 452 /* 453 * read a segment into a memory buffer 454 */ 455 int 456 mmap_segment (fsp, segment, segbuf) 457 FS_INFO *fsp; /* file system information */ 458 int segment; /* segment number */ 459 caddr_t *segbuf; /* pointer to buffer area */ 460 { 461 struct lfs *lfsp; 462 int fid; /* fildes for file system device */ 463 daddr_t seg_daddr; /* base disk address of segment */ 464 off_t seg_byte; 465 size_t ssize; 466 char mntfromname[MNAMELEN+2]; 467 468 lfsp = &fsp->fi_lfs; 469 470 /* get the disk address of the beginning of the segment */ 471 seg_daddr = sntoda(lfsp, segment); 472 seg_byte = datobyte(fsp, seg_daddr); 473 ssize = seg_size(lfsp); 474 475 strcpy(mntfromname, "/dev/r"); 476 strcat(mntfromname, fsp->fi_statfsp->f_mntfromname+5); 477 478 if ((fid = open(mntfromname, O_RDONLY, (mode_t)0)) < 0) { 479 err(0, "mmap_segment: bad open"); 480 return (-1); 481 } 482 483 #ifdef MMAP_SEGMENT 484 *segbuf = mmap ((caddr_t)0, seg_size(lfsp), PROT_READ, 485 MAP_FILE, fid, seg_byte); 486 if (*(long *)segbuf < 0) { 487 err(0, "mmap_segment: mmap failed"); 488 return (NULL); 489 } 490 #else /* MMAP_SEGMENT */ 491 #ifdef VERBOSE 492 printf("mmap_segment\tseg_daddr: %lu\tseg_size: %lu\tseg_offset: %qu\n", 493 seg_daddr, ssize, seg_byte); 494 #endif 495 /* malloc the space for the buffer */ 496 *segbuf = malloc(ssize); 497 if (!*segbuf) { 498 err(0, "mmap_segment: malloc failed"); 499 return(NULL); 500 } 501 502 /* read the segment data into the buffer */ 503 if (lseek (fid, seg_byte, SEEK_SET) != seg_byte) { 504 err (0, "mmap_segment: bad lseek"); 505 free(*segbuf); 506 return (-1); 507 } 508 509 if (read (fid, *segbuf, ssize) != ssize) { 510 err (0, "mmap_segment: bad read"); 511 free(*segbuf); 512 return (-1); 513 } 514 #endif /* MMAP_SEGMENT */ 515 close (fid); 516 517 return (0); 518 } 519 520 void 521 munmap_segment (fsp, seg_buf) 522 FS_INFO *fsp; /* file system information */ 523 caddr_t seg_buf; /* pointer to buffer area */ 524 { 525 #ifdef MMAP_SEGMENT 526 munmap (seg_buf, seg_size(&fsp->fi_lfs)); 527 #else /* MMAP_SEGMENT */ 528 free (seg_buf); 529 #endif /* MMAP_SEGMENT */ 530 } 531 532 533 /* 534 * USEFUL DEBUGGING TOOLS: 535 */ 536 void 537 print_SEGSUM (lfsp, p) 538 struct lfs *lfsp; 539 SEGSUM *p; 540 { 541 if (p) 542 (void) dump_summary(lfsp, p, DUMP_ALL, NULL); 543 else printf("0x0"); 544 fflush(stdout); 545 } 546 547 int 548 bi_compare(a, b) 549 const void *a; 550 const void *b; 551 { 552 const BLOCK_INFO *ba, *bb; 553 int diff; 554 555 ba = a; 556 bb = b; 557 558 if (diff = (int)(ba->bi_inode - bb->bi_inode)) 559 return (diff); 560 if (diff = (int)(ba->bi_lbn - bb->bi_lbn)) { 561 if (ba->bi_lbn == LFS_UNUSED_LBN) 562 return(-1); 563 else if (bb->bi_lbn == LFS_UNUSED_LBN) 564 return(1); 565 else if (ba->bi_lbn < 0) 566 return(1); 567 else 568 return (diff); 569 } 570 if (diff = (int)(ba->bi_segcreate - bb->bi_segcreate)) 571 return (diff); 572 diff = (int)(ba->bi_daddr - bb->bi_daddr); 573 return (diff); 574 } 575 576 int 577 bi_toss(dummy, a, b) 578 const void *dummy; 579 const void *a; 580 const void *b; 581 { 582 const BLOCK_INFO *ba, *bb; 583 584 ba = a; 585 bb = b; 586 587 return(ba->bi_inode == bb->bi_inode && ba->bi_lbn == bb->bi_lbn); 588 } 589 590 void 591 toss(p, nump, size, dotoss, client) 592 void *p; 593 int *nump; 594 size_t size; 595 int (*dotoss) __P((const void *, const void *, const void *)); 596 void *client; 597 { 598 int i; 599 void *p1; 600 601 if (*nump == 0) 602 return; 603 604 for (i = *nump; --i > 0;) { 605 p1 = p + size; 606 if (dotoss(client, p, p1)) { 607 bcopy(p1, p, i * size); 608 --(*nump); 609 } else 610 p += size; 611 } 612 } 613