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