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