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