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