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