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