1 /* $OpenBSD: traverse.c,v 1.14 2003/07/28 06:13:26 tedu Exp $ */ 2 /* $NetBSD: traverse.c,v 1.17 1997/06/05 11:13:27 lukem Exp $ */ 3 4 /*- 5 * Copyright (c) 1980, 1988, 1991, 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. Neither the name of the University nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 */ 32 33 #ifndef lint 34 #if 0 35 static char sccsid[] = "@(#)traverse.c 8.2 (Berkeley) 9/23/93"; 36 #else 37 static const char rcsid[] = "$OpenBSD: traverse.c,v 1.14 2003/07/28 06:13:26 tedu Exp $"; 38 #endif 39 #endif /* not lint */ 40 41 #include <sys/param.h> 42 #include <sys/time.h> 43 #include <sys/stat.h> 44 #ifdef sunos 45 #include <sys/vnode.h> 46 47 #include <ufs/fs.h> 48 #include <ufs/fsdir.h> 49 #include <ufs/inode.h> 50 #else 51 #include <ufs/ffs/fs.h> 52 #include <ufs/ufs/dir.h> 53 #include <ufs/ufs/dinode.h> 54 #endif 55 56 #include <protocols/dumprestore.h> 57 58 #include <ctype.h> 59 #include <errno.h> 60 #include <fts.h> 61 #include <stdio.h> 62 #include <string.h> 63 #include <unistd.h> 64 65 #include "dump.h" 66 67 #define HASDUMPEDFILE 0x1 68 #define HASSUBDIRS 0x2 69 70 static int dirindir(ino_t ino, daddr_t blkno, int level, off_t *size); 71 static void dmpindir(ino_t ino, daddr_t blk, int level, off_t *size); 72 static int searchdir(ino_t ino, daddr_t blkno, long size, off_t filesize); 73 74 /* 75 * This is an estimation of the number of TP_BSIZE blocks in the file. 76 * It estimates the number of blocks in files with holes by assuming 77 * that all of the blocks accounted for by di_blocks are data blocks 78 * (when some of the blocks are usually used for indirect pointers); 79 * hence the estimate may be high. 80 */ 81 off_t 82 blockest(struct dinode *dp) 83 { 84 off_t blkest, sizeest; 85 86 /* 87 * dp->di_size is the size of the file in bytes. 88 * dp->di_blocks stores the number of sectors actually in the file. 89 * If there are more sectors than the size would indicate, this just 90 * means that there are indirect blocks in the file or unused 91 * sectors in the last file block; we can safely ignore these 92 * (blkest = sizeest below). 93 * If the file is bigger than the number of sectors would indicate, 94 * then the file has holes in it. In this case we must use the 95 * block count to estimate the number of data blocks used, but 96 * we use the actual size for estimating the number of indirect 97 * dump blocks (sizeest vs. blkest in the indirect block 98 * calculation). 99 */ 100 blkest = howmany(dbtob((off_t)dp->di_blocks), TP_BSIZE); 101 sizeest = howmany(dp->di_size, TP_BSIZE); 102 if (blkest > sizeest) 103 blkest = sizeest; 104 if (dp->di_size > sblock->fs_bsize * NDADDR) { 105 /* calculate the number of indirect blocks on the dump tape */ 106 blkest += 107 howmany(sizeest - NDADDR * sblock->fs_bsize / TP_BSIZE, 108 TP_NINDIR); 109 } 110 return (blkest + 1); 111 } 112 113 /* Auxiliary macro to pick up files changed since previous dump. */ 114 #define CHANGEDSINCE(dp, t) \ 115 ((dp)->di_mtime >= (t) || (dp)->di_ctime >= (t)) 116 117 /* The WANTTODUMP macro decides whether a file should be dumped. */ 118 #ifdef UF_NODUMP 119 #define WANTTODUMP(dp) \ 120 (CHANGEDSINCE(dp, spcl.c_ddate) && \ 121 (nonodump || ((dp)->di_flags & UF_NODUMP) != UF_NODUMP)) 122 #else 123 #define WANTTODUMP(dp) CHANGEDSINCE(dp, spcl.c_ddate) 124 #endif 125 126 /* 127 * Determine if given inode should be dumped 128 */ 129 void 130 mapfileino(ino_t ino, off_t *tapesize, int *dirskipped) 131 { 132 int mode; 133 struct dinode *dp; 134 135 dp = getino(ino); 136 if ((mode = (dp->di_mode & IFMT)) == 0) 137 return; 138 SETINO(ino, usedinomap); 139 if (mode == IFDIR) 140 SETINO(ino, dumpdirmap); 141 if (WANTTODUMP(dp)) { 142 SETINO(ino, dumpinomap); 143 if (mode != IFREG && mode != IFDIR && mode != IFLNK) 144 *tapesize += 1; 145 else 146 *tapesize += blockest(dp); 147 return; 148 } 149 if (mode == IFDIR) 150 *dirskipped = 1; 151 } 152 153 /* 154 * Dump pass 1. 155 * 156 * Walk the inode list for a filesystem to find all allocated inodes 157 * that have been modified since the previous dump time. Also, find all 158 * the directories in the filesystem. 159 */ 160 int 161 mapfiles(ino_t maxino, off_t *tapesize, char *disk, char * const *dirv) 162 { 163 int anydirskipped = 0; 164 165 if (dirv != NULL) { 166 char curdir[MAXPATHLEN]; 167 FTS *dirh; 168 FTSENT *entry; 169 int d; 170 171 if (getcwd(curdir, sizeof(curdir)) == NULL) { 172 msg("Can't determine cwd: %s\n", strerror(errno)); 173 dumpabort(0); 174 } 175 if ((dirh = fts_open(dirv, FTS_PHYSICAL|FTS_SEEDOT|FTS_XDEV, 176 NULL)) == NULL) { 177 msg("fts_open failed: %s\n", strerror(errno)); 178 dumpabort(0); 179 } 180 while ((entry = fts_read(dirh)) != NULL) { 181 switch (entry->fts_info) { 182 case FTS_DNR: /* an error */ 183 case FTS_ERR: 184 case FTS_NS: 185 msg("Can't fts_read %s: %s\n", entry->fts_path, 186 strerror(errno)); 187 case FTS_DP: /* already seen dir */ 188 continue; 189 } 190 mapfileino(entry->fts_statp->st_ino, tapesize, 191 &anydirskipped); 192 } 193 (void)fts_close(dirh); 194 195 /* 196 * Add any parent directories 197 */ 198 for (d = 0 ; dirv[d] != NULL ; d++) { 199 char path[MAXPATHLEN]; 200 201 if (dirv[d][0] != '/') 202 (void)snprintf(path, sizeof(path), "%s/%s", 203 curdir, dirv[d]); 204 else 205 (void)snprintf(path, sizeof(path), "%s", 206 dirv[d]); 207 while (strcmp(path, disk) != 0) { 208 char *p; 209 struct stat sb; 210 211 if (*path == '\0') 212 break; 213 if ((p = strrchr(path, '/')) == NULL) 214 break; 215 if (p == path) 216 break; 217 *p = '\0'; 218 if (stat(path, &sb) == -1) { 219 msg("Can't stat %s: %s\n", path, 220 strerror(errno)); 221 break; 222 } 223 mapfileino(sb.st_ino, tapesize, &anydirskipped); 224 } 225 } 226 227 /* 228 * Ensure that the root inode actually appears in the 229 * file list for a subdir 230 */ 231 mapfileino(ROOTINO, tapesize, &anydirskipped); 232 } else { 233 ino_t ino; 234 235 for (ino = ROOTINO; ino < maxino; ino++) { 236 mapfileino(ino, tapesize, &anydirskipped); 237 } 238 } 239 /* 240 * Restore gets very upset if the root is not dumped, 241 * so ensure that it always is dumped. 242 */ 243 SETINO(ROOTINO, dumpinomap); 244 return (anydirskipped); 245 } 246 247 /* 248 * Dump pass 2. 249 * 250 * Scan each directory on the filesystem to see if it has any modified 251 * files in it. If it does, and has not already been added to the dump 252 * list (because it was itself modified), then add it. If a directory 253 * has not been modified itself, contains no modified files and has no 254 * subdirectories, then it can be deleted from the dump list and from 255 * the list of directories. By deleting it from the list of directories, 256 * its parent may now qualify for the same treatment on this or a later 257 * pass using this algorithm. 258 */ 259 int 260 mapdirs(ino_t maxino, off_t *tapesize) 261 { 262 struct dinode *dp; 263 int i, isdir; 264 char *map; 265 ino_t ino; 266 off_t filesize; 267 int ret, change = 0; 268 269 isdir = 0; /* XXX just to get gcc to shut up */ 270 for (map = dumpdirmap, ino = 1; ino < maxino; ino++) { 271 if (((ino - 1) % NBBY) == 0) /* map is offset by 1 */ 272 isdir = *map++; 273 else 274 isdir >>= 1; 275 if ((isdir & 1) == 0 || TSTINO(ino, dumpinomap)) 276 continue; 277 dp = getino(ino); 278 filesize = dp->di_size; 279 for (ret = 0, i = 0; filesize > 0 && i < NDADDR; i++) { 280 if (dp->di_db[i] != 0) 281 ret |= searchdir(ino, dp->di_db[i], 282 dblksize(sblock, dp, i), filesize); 283 if (ret & HASDUMPEDFILE) 284 filesize = 0; 285 else 286 filesize -= sblock->fs_bsize; 287 } 288 for (i = 0; filesize > 0 && i < NIADDR; i++) { 289 if (dp->di_ib[i] == 0) 290 continue; 291 ret |= dirindir(ino, dp->di_ib[i], i, &filesize); 292 } 293 if (ret & HASDUMPEDFILE) { 294 SETINO(ino, dumpinomap); 295 *tapesize += blockest(dp); 296 change = 1; 297 continue; 298 } 299 if ((ret & HASSUBDIRS) == 0) { 300 if (!TSTINO(ino, dumpinomap)) { 301 CLRINO(ino, dumpdirmap); 302 change = 1; 303 } 304 } 305 } 306 return (change); 307 } 308 309 /* 310 * Read indirect blocks, and pass the data blocks to be searched 311 * as directories. Quit as soon as any entry is found that will 312 * require the directory to be dumped. 313 */ 314 static int 315 dirindir(ino_t ino, daddr_t blkno, int ind_level, off_t *filesize) 316 { 317 int ret = 0; 318 int i; 319 daddr_t idblk[MAXNINDIR]; 320 321 bread(fsbtodb(sblock, blkno), (char *)idblk, (int)sblock->fs_bsize); 322 if (ind_level <= 0) { 323 for (i = 0; *filesize > 0 && i < NINDIR(sblock); i++) { 324 blkno = idblk[i]; 325 if (blkno != 0) 326 ret |= searchdir(ino, blkno, sblock->fs_bsize, 327 *filesize); 328 if (ret & HASDUMPEDFILE) 329 *filesize = 0; 330 else 331 *filesize -= sblock->fs_bsize; 332 } 333 return (ret); 334 } 335 ind_level--; 336 for (i = 0; *filesize > 0 && i < NINDIR(sblock); i++) { 337 blkno = idblk[i]; 338 if (blkno != 0) 339 ret |= dirindir(ino, blkno, ind_level, filesize); 340 } 341 return (ret); 342 } 343 344 /* 345 * Scan a disk block containing directory information looking to see if 346 * any of the entries are on the dump list and to see if the directory 347 * contains any subdirectories. 348 */ 349 static int 350 searchdir(ino_t ino, daddr_t blkno, long size, off_t filesize) 351 { 352 struct direct *dp; 353 long loc; 354 char dblk[MAXBSIZE]; 355 int ret = 0; 356 357 bread(fsbtodb(sblock, blkno), dblk, (int)size); 358 if (filesize < size) 359 size = filesize; 360 for (loc = 0; loc < size; ) { 361 dp = (struct direct *)(dblk + loc); 362 if (dp->d_reclen == 0) { 363 msg("corrupted directory, inumber %d\n", ino); 364 break; 365 } 366 loc += dp->d_reclen; 367 if (dp->d_ino == 0) 368 continue; 369 if (dp->d_name[0] == '.') { 370 if (dp->d_name[1] == '\0') 371 continue; 372 if (dp->d_name[1] == '.' && dp->d_name[2] == '\0') 373 continue; 374 } 375 if (TSTINO(dp->d_ino, dumpinomap)) { 376 ret |= HASDUMPEDFILE; 377 if (ret & HASSUBDIRS) 378 break; 379 } 380 if (TSTINO(dp->d_ino, dumpdirmap)) { 381 ret |= HASSUBDIRS; 382 if (ret & HASDUMPEDFILE) 383 break; 384 } 385 } 386 return (ret); 387 } 388 389 /* 390 * Dump passes 3 and 4. 391 * 392 * Dump the contents of an inode to tape. 393 */ 394 void 395 dumpino(struct dinode *dp, ino_t ino) 396 { 397 int ind_level, cnt; 398 off_t size; 399 char buf[TP_BSIZE]; 400 401 if (newtape) { 402 newtape = 0; 403 dumpmap(dumpinomap, TS_BITS, ino); 404 } 405 CLRINO(ino, dumpinomap); 406 spcl.c_dinode = *dp; 407 spcl.c_type = TS_INODE; 408 spcl.c_count = 0; 409 switch (dp->di_mode & IFMT) { 410 411 case 0: 412 /* 413 * Freed inode. 414 */ 415 return; 416 417 case IFLNK: 418 /* 419 * Check for short symbolic link. 420 */ 421 if (dp->di_size > 0 && 422 #ifdef FS_44INODEFMT 423 (dp->di_size < sblock->fs_maxsymlinklen || 424 (sblock->fs_maxsymlinklen == 0 && dp->di_blocks == 0))) { 425 #else 426 dp->di_blocks == 0) { 427 #endif 428 spcl.c_addr[0] = 1; 429 spcl.c_count = 1; 430 writeheader(ino); 431 memcpy(buf, dp->di_shortlink, (u_long)dp->di_size); 432 buf[dp->di_size] = '\0'; 433 writerec(buf, 0); 434 return; 435 } 436 /* fall through */ 437 438 case IFDIR: 439 case IFREG: 440 if (dp->di_size > 0) 441 break; 442 /* fall through */ 443 444 case IFIFO: 445 case IFSOCK: 446 case IFCHR: 447 case IFBLK: 448 writeheader(ino); 449 return; 450 451 default: 452 msg("Warning: undefined file type 0%o\n", dp->di_mode & IFMT); 453 return; 454 } 455 if (dp->di_size > NDADDR * sblock->fs_bsize) 456 cnt = NDADDR * sblock->fs_frag; 457 else 458 cnt = howmany(dp->di_size, sblock->fs_fsize); 459 blksout(&dp->di_db[0], cnt, ino); 460 if ((size = dp->di_size - NDADDR * sblock->fs_bsize) <= 0) 461 return; 462 for (ind_level = 0; ind_level < NIADDR; ind_level++) { 463 dmpindir(ino, dp->di_ib[ind_level], ind_level, &size); 464 if (size <= 0) 465 return; 466 } 467 } 468 469 /* 470 * Read indirect blocks, and pass the data blocks to be dumped. 471 */ 472 static void 473 dmpindir(ino_t ino, daddr_t blk, int ind_level, off_t *size) 474 { 475 int i, cnt; 476 daddr_t idblk[MAXNINDIR]; 477 478 if (blk != 0) 479 bread(fsbtodb(sblock, blk), (char *)idblk, (int) sblock->fs_bsize); 480 else 481 memset(idblk, 0, (int)sblock->fs_bsize); 482 if (ind_level <= 0) { 483 if (*size < NINDIR(sblock) * sblock->fs_bsize) 484 cnt = howmany(*size, sblock->fs_fsize); 485 else 486 cnt = NINDIR(sblock) * sblock->fs_frag; 487 *size -= NINDIR(sblock) * sblock->fs_bsize; 488 blksout(&idblk[0], cnt, ino); 489 return; 490 } 491 ind_level--; 492 for (i = 0; i < NINDIR(sblock); i++) { 493 dmpindir(ino, idblk[i], ind_level, size); 494 if (*size <= 0) 495 return; 496 } 497 } 498 499 /* 500 * Collect up the data into tape record sized buffers and output them. 501 */ 502 void 503 blksout(daddr_t *blkp, int frags, ino_t ino) 504 { 505 daddr_t *bp; 506 int i, j, count, blks, tbperdb; 507 508 blks = howmany(frags * sblock->fs_fsize, TP_BSIZE); 509 tbperdb = sblock->fs_bsize >> tp_bshift; 510 for (i = 0; i < blks; i += TP_NINDIR) { 511 if (i + TP_NINDIR > blks) 512 count = blks; 513 else 514 count = i + TP_NINDIR; 515 for (j = i; j < count; j++) 516 if (blkp[j / tbperdb] != 0) 517 spcl.c_addr[j - i] = 1; 518 else 519 spcl.c_addr[j - i] = 0; 520 spcl.c_count = count - i; 521 writeheader(ino); 522 bp = &blkp[i / tbperdb]; 523 for (j = i; j < count; j += tbperdb, bp++) 524 if (*bp != 0) { 525 if (j + tbperdb <= count) 526 dumpblock(*bp, (int)sblock->fs_bsize); 527 else 528 dumpblock(*bp, (count - j) * TP_BSIZE); 529 } 530 spcl.c_type = TS_ADDR; 531 } 532 } 533 534 /* 535 * Dump a map to the tape. 536 */ 537 void 538 dumpmap(map, type, ino) 539 char *map; 540 int type; 541 ino_t ino; 542 { 543 int i; 544 char *cp; 545 546 spcl.c_type = type; 547 spcl.c_count = howmany(mapsize * sizeof(char), TP_BSIZE); 548 writeheader(ino); 549 for (i = 0, cp = map; i < spcl.c_count; i++, cp += TP_BSIZE) 550 writerec(cp, 0); 551 } 552 553 /* 554 * Write a header record to the dump tape. 555 */ 556 void 557 writeheader(ino) 558 ino_t ino; 559 { 560 int32_t sum, cnt, *lp; 561 562 spcl.c_inumber = ino; 563 spcl.c_magic = NFS_MAGIC; 564 spcl.c_checksum = 0; 565 lp = (int32_t *)&spcl; 566 sum = 0; 567 cnt = sizeof(union u_spcl) / (4 * sizeof(int32_t)); 568 while (--cnt >= 0) { 569 sum += *lp++; 570 sum += *lp++; 571 sum += *lp++; 572 sum += *lp++; 573 } 574 spcl.c_checksum = CHECKSUM - sum; 575 writerec((char *)&spcl, 1); 576 } 577 578 struct dinode * 579 getino(ino_t inum) 580 { 581 static daddr_t minino, maxino; 582 static struct dinode inoblock[MAXINOPB]; 583 584 curino = inum; 585 if (inum >= minino && inum < maxino) 586 return (&inoblock[inum - minino]); 587 bread(fsbtodb(sblock, ino_to_fsba(sblock, inum)), (char *)inoblock, 588 (int)sblock->fs_bsize); 589 minino = inum - (inum % INOPB(sblock)); 590 maxino = minino + INOPB(sblock); 591 return (&inoblock[inum - minino]); 592 } 593 594 /* 595 * Read a chunk of data from the disk. 596 * Try to recover from hard errors by reading in sector sized pieces. 597 * Error recovery is attempted at most BREADEMAX times before seeking 598 * consent from the operator to continue. 599 */ 600 int breaderrors = 0; 601 #define BREADEMAX 32 602 603 void 604 bread(daddr_t blkno, char *buf, int size) 605 { 606 int cnt, i; 607 608 loop: 609 if (lseek(diskfd, ((off_t)blkno << dev_bshift), 0) < 0) 610 msg("bread: lseek fails\n"); 611 if ((cnt = read(diskfd, buf, size)) == size) 612 return; 613 if (blkno + (size / dev_bsize) > fsbtodb(sblock, sblock->fs_size)) { 614 /* 615 * Trying to read the final fragment. 616 * 617 * NB - dump only works in TP_BSIZE blocks, hence 618 * rounds `dev_bsize' fragments up to TP_BSIZE pieces. 619 * It should be smarter about not actually trying to 620 * read more than it can get, but for the time being 621 * we punt and scale back the read only when it gets 622 * us into trouble. (mkm 9/25/83) 623 */ 624 size -= dev_bsize; 625 goto loop; 626 } 627 if (cnt == -1) 628 msg("read error from %s: %s: [block %d]: count=%d\n", 629 disk, strerror(errno), blkno, size); 630 else 631 msg("short read error from %s: [block %d]: count=%d, got=%d\n", 632 disk, blkno, size, cnt); 633 if (++breaderrors > BREADEMAX) { 634 msg("More than %d block read errors from %s\n", 635 BREADEMAX, disk); 636 broadcast("DUMP IS AILING!\n"); 637 msg("This is an unrecoverable error.\n"); 638 if (!query("Do you want to attempt to continue?")){ 639 dumpabort(0); 640 /*NOTREACHED*/ 641 } else 642 breaderrors = 0; 643 } 644 /* 645 * Zero buffer, then try to read each sector of buffer separately. 646 */ 647 memset(buf, 0, size); 648 for (i = 0; i < size; i += dev_bsize, buf += dev_bsize, blkno++) { 649 if (lseek(diskfd, ((off_t)blkno << dev_bshift), 0) < 0) 650 msg("bread: lseek2 fails!\n"); 651 if ((cnt = read(diskfd, buf, (int)dev_bsize)) == dev_bsize) 652 continue; 653 if (cnt == -1) { 654 msg("read error from %s: %s: [sector %d]: count=%d\n", 655 disk, strerror(errno), blkno, dev_bsize); 656 continue; 657 } 658 msg("short read error from %s: [sector %d]: count=%d, got=%d\n", 659 disk, blkno, dev_bsize, cnt); 660 } 661 } 662