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. Neither the name of the University nor the names of its contributors 14 * may be used to endorse or promote products derived from this software 15 * without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 */ 29 30 #ifndef lint 31 #if 0 32 static char sccsid[] = "@(#)traverse.c 8.7 (Berkeley) 6/15/95"; 33 #endif 34 static const char rcsid[] = 35 "$FreeBSD$"; 36 #endif /* not lint */ 37 38 #include <sys/param.h> 39 #include <sys/stat.h> 40 41 #include <ufs/ufs/dir.h> 42 #include <ufs/ufs/dinode.h> 43 #include <ufs/ffs/fs.h> 44 45 #include <protocols/dumprestore.h> 46 47 #include <ctype.h> 48 #include <errno.h> 49 #include <inttypes.h> 50 #include <limits.h> 51 #include <stdio.h> 52 #include <stdlib.h> 53 #include <string.h> 54 #include <timeconv.h> 55 #include <unistd.h> 56 57 #include "dump.h" 58 59 union dinode { 60 struct ufs1_dinode dp1; 61 struct ufs2_dinode dp2; 62 }; 63 #define DIP(dp, field) \ 64 ((sblock->fs_magic == FS_UFS1_MAGIC) ? \ 65 (dp)->dp1.field : (dp)->dp2.field) 66 #define DIP_SET(dp, field, val) do {\ 67 if (sblock->fs_magic == FS_UFS1_MAGIC) \ 68 (dp)->dp1.field = (val); \ 69 else \ 70 (dp)->dp2.field = (val); \ 71 } while (0) 72 73 #define HASDUMPEDFILE 0x1 74 #define HASSUBDIRS 0x2 75 76 static int dirindir(ino_t ino, ufs2_daddr_t blkno, int level, long *size, 77 long *tapesize, int nodump, ino_t maxino); 78 static void dmpindir(union dinode *dp, ino_t ino, ufs2_daddr_t blk, int level, 79 off_t *size); 80 static void ufs1_blksout(ufs1_daddr_t *blkp, int frags, ino_t ino); 81 static void ufs2_blksout(union dinode *dp, ufs2_daddr_t *blkp, int frags, 82 ino_t ino, int last); 83 static int appendextdata(union dinode *dp); 84 static void writeextdata(union dinode *dp, ino_t ino, int added); 85 static int searchdir(ino_t ino, ufs2_daddr_t blkno, long size, long filesize, 86 long *tapesize, int nodump, ino_t maxino); 87 static long blockest(union dinode *dp); 88 89 /* 90 * This is an estimation of the number of TP_BSIZE blocks in the file. 91 * It estimates the number of blocks in files with holes by assuming 92 * that all of the blocks accounted for by di_blocks are data blocks 93 * (when some of the blocks are usually used for indirect pointers); 94 * hence the estimate may be high. 95 */ 96 static long 97 blockest(union dinode *dp) 98 { 99 long blkest, sizeest; 100 101 /* 102 * dp->di_size is the size of the file in bytes. 103 * dp->di_blocks stores the number of sectors actually in the file. 104 * If there are more sectors than the size would indicate, this just 105 * means that there are indirect blocks in the file or unused 106 * sectors in the last file block; we can safely ignore these 107 * (blkest = sizeest below). 108 * If the file is bigger than the number of sectors would indicate, 109 * then the file has holes in it. In this case we must use the 110 * block count to estimate the number of data blocks used, but 111 * we use the actual size for estimating the number of indirect 112 * dump blocks (sizeest vs. blkest in the indirect block 113 * calculation). 114 */ 115 if ((DIP(dp, di_flags) & SF_SNAPSHOT) != 0) 116 return (1); 117 blkest = howmany(dbtob(DIP(dp, di_blocks)), TP_BSIZE); 118 sizeest = howmany(DIP(dp, di_size), TP_BSIZE); 119 if (blkest > sizeest) 120 blkest = sizeest; 121 if (DIP(dp, di_size) > sblock->fs_bsize * UFS_NDADDR) { 122 /* calculate the number of indirect blocks on the dump tape */ 123 blkest += howmany(sizeest - 124 UFS_NDADDR * sblock->fs_bsize / TP_BSIZE, TP_NINDIR); 125 } 126 return (blkest + 1); 127 } 128 129 /* Auxiliary macro to pick up files changed since previous dump. */ 130 #define CHANGEDSINCE(dp, t) \ 131 (DIP(dp, di_mtime) >= (t) || DIP(dp, di_ctime) >= (t)) 132 133 /* The WANTTODUMP macro decides whether a file should be dumped. */ 134 #ifdef UF_NODUMP 135 #define WANTTODUMP(dp) \ 136 (CHANGEDSINCE(dp, spcl.c_ddate) && \ 137 (nonodump || (DIP(dp, di_flags) & UF_NODUMP) != UF_NODUMP)) 138 #else 139 #define WANTTODUMP(dp) CHANGEDSINCE(dp, spcl.c_ddate) 140 #endif 141 142 /* 143 * Dump pass 1. 144 * 145 * Walk the inode list for a file system to find all allocated inodes 146 * that have been modified since the previous dump time. Also, find all 147 * the directories in the file system. 148 */ 149 int 150 mapfiles(ino_t maxino, long *tapesize) 151 { 152 int i, cg, mode, inosused; 153 int anydirskipped = 0; 154 union dinode *dp; 155 struct cg *cgp; 156 ino_t ino; 157 u_char *cp; 158 159 if ((cgp = malloc(sblock->fs_cgsize)) == NULL) 160 quit("mapfiles: cannot allocate memory.\n"); 161 for (cg = 0; cg < sblock->fs_ncg; cg++) { 162 ino = cg * sblock->fs_ipg; 163 bread(fsbtodb(sblock, cgtod(sblock, cg)), (char *)cgp, 164 sblock->fs_cgsize); 165 if (sblock->fs_magic == FS_UFS2_MAGIC) 166 inosused = cgp->cg_initediblk; 167 else 168 inosused = sblock->fs_ipg; 169 /* 170 * If we are using soft updates, then we can trust the 171 * cylinder group inode allocation maps to tell us which 172 * inodes are allocated. We will scan the used inode map 173 * to find the inodes that are really in use, and then 174 * read only those inodes in from disk. 175 */ 176 if (sblock->fs_flags & FS_DOSOFTDEP) { 177 if (!cg_chkmagic(cgp)) 178 quit("mapfiles: cg %d: bad magic number\n", cg); 179 cp = &cg_inosused(cgp)[(inosused - 1) / CHAR_BIT]; 180 for ( ; inosused > 0; inosused -= CHAR_BIT, cp--) { 181 if (*cp == 0) 182 continue; 183 for (i = 1 << (CHAR_BIT - 1); i > 0; i >>= 1) { 184 if (*cp & i) 185 break; 186 inosused--; 187 } 188 break; 189 } 190 if (inosused <= 0) 191 continue; 192 } 193 for (i = 0; i < inosused; i++, ino++) { 194 if (ino < UFS_ROOTINO || 195 (dp = getino(ino, &mode)) == NULL || 196 (mode & IFMT) == 0) 197 continue; 198 if (ino >= maxino) { 199 msg("Skipping inode %ju >= maxino %ju\n", 200 (uintmax_t)ino, (uintmax_t)maxino); 201 continue; 202 } 203 /* 204 * Everything must go in usedinomap so that a check 205 * for "in dumpdirmap but not in usedinomap" to detect 206 * dirs with nodump set has a chance of succeeding 207 * (this is used in mapdirs()). 208 */ 209 SETINO(ino, usedinomap); 210 if (mode == IFDIR) 211 SETINO(ino, dumpdirmap); 212 if (WANTTODUMP(dp)) { 213 SETINO(ino, dumpinomap); 214 if (mode != IFREG && 215 mode != IFDIR && 216 mode != IFLNK) 217 *tapesize += 1; 218 else 219 *tapesize += blockest(dp); 220 continue; 221 } 222 if (mode == IFDIR) { 223 if (!nonodump && 224 (DIP(dp, di_flags) & UF_NODUMP)) 225 CLRINO(ino, usedinomap); 226 anydirskipped = 1; 227 } 228 } 229 } 230 /* 231 * Restore gets very upset if the root is not dumped, 232 * so ensure that it always is dumped. 233 */ 234 SETINO(UFS_ROOTINO, dumpinomap); 235 return (anydirskipped); 236 } 237 238 /* 239 * Dump pass 2. 240 * 241 * Scan each directory on the file system to see if it has any modified 242 * files in it. If it does, and has not already been added to the dump 243 * list (because it was itself modified), then add it. If a directory 244 * has not been modified itself, contains no modified files and has no 245 * subdirectories, then it can be deleted from the dump list and from 246 * the list of directories. By deleting it from the list of directories, 247 * its parent may now qualify for the same treatment on this or a later 248 * pass using this algorithm. 249 */ 250 int 251 mapdirs(ino_t maxino, long *tapesize) 252 { 253 union dinode *dp; 254 int i, isdir, nodump; 255 char *map; 256 ino_t ino; 257 union dinode di; 258 long filesize; 259 int ret, change = 0; 260 261 isdir = 0; /* XXX just to get gcc to shut up */ 262 for (map = dumpdirmap, ino = 1; ino < maxino; ino++) { 263 if (((ino - 1) % CHAR_BIT) == 0) /* map is offset by 1 */ 264 isdir = *map++; 265 else 266 isdir >>= 1; 267 /* 268 * If a directory has been removed from usedinomap, it 269 * either has the nodump flag set, or has inherited 270 * it. Although a directory can't be in dumpinomap if 271 * it isn't in usedinomap, we have to go through it to 272 * propagate the nodump flag. 273 */ 274 nodump = !nonodump && (TSTINO(ino, usedinomap) == 0); 275 if ((isdir & 1) == 0 || (TSTINO(ino, dumpinomap) && !nodump)) 276 continue; 277 dp = getino(ino, &i); 278 /* 279 * inode buf may change in searchdir(). 280 */ 281 if (sblock->fs_magic == FS_UFS1_MAGIC) 282 di.dp1 = dp->dp1; 283 else 284 di.dp2 = dp->dp2; 285 filesize = DIP(&di, di_size); 286 for (ret = 0, i = 0; filesize > 0 && i < UFS_NDADDR; i++) { 287 if (DIP(&di, di_db[i]) != 0) 288 ret |= searchdir(ino, DIP(&di, di_db[i]), 289 (long)sblksize(sblock, DIP(&di, di_size), 290 i), filesize, tapesize, nodump, maxino); 291 if (ret & HASDUMPEDFILE) 292 filesize = 0; 293 else 294 filesize -= sblock->fs_bsize; 295 } 296 for (i = 0; filesize > 0 && i < UFS_NIADDR; i++) { 297 if (DIP(&di, di_ib[i]) == 0) 298 continue; 299 ret |= dirindir(ino, DIP(&di, di_ib[i]), i, &filesize, 300 tapesize, nodump, maxino); 301 } 302 if (ret & HASDUMPEDFILE) { 303 SETINO(ino, dumpinomap); 304 *tapesize += blockest(&di); 305 change = 1; 306 continue; 307 } 308 if (nodump) { 309 if (ret & HASSUBDIRS) 310 change = 1; /* subdirs inherit nodump */ 311 CLRINO(ino, dumpdirmap); 312 } else if ((ret & HASSUBDIRS) == 0) 313 if (!TSTINO(ino, dumpinomap)) { 314 CLRINO(ino, dumpdirmap); 315 change = 1; 316 } 317 } 318 return (change); 319 } 320 321 /* 322 * Read indirect blocks, and pass the data blocks to be searched 323 * as directories. Quit as soon as any entry is found that will 324 * require the directory to be dumped. 325 */ 326 static int 327 dirindir( 328 ino_t ino, 329 ufs2_daddr_t blkno, 330 int ind_level, 331 long *filesize, 332 long *tapesize, 333 int nodump, 334 ino_t maxino) 335 { 336 union { 337 ufs1_daddr_t ufs1[MAXBSIZE / sizeof(ufs1_daddr_t)]; 338 ufs2_daddr_t ufs2[MAXBSIZE / sizeof(ufs2_daddr_t)]; 339 } idblk; 340 int ret = 0; 341 int i; 342 343 bread(fsbtodb(sblock, blkno), (char *)&idblk, (int)sblock->fs_bsize); 344 if (ind_level <= 0) { 345 for (i = 0; *filesize > 0 && i < NINDIR(sblock); i++) { 346 if (sblock->fs_magic == FS_UFS1_MAGIC) 347 blkno = idblk.ufs1[i]; 348 else 349 blkno = idblk.ufs2[i]; 350 if (blkno != 0) 351 ret |= searchdir(ino, blkno, sblock->fs_bsize, 352 *filesize, tapesize, nodump, maxino); 353 if (ret & HASDUMPEDFILE) 354 *filesize = 0; 355 else 356 *filesize -= sblock->fs_bsize; 357 } 358 return (ret); 359 } 360 ind_level--; 361 for (i = 0; *filesize > 0 && i < NINDIR(sblock); i++) { 362 if (sblock->fs_magic == FS_UFS1_MAGIC) 363 blkno = idblk.ufs1[i]; 364 else 365 blkno = idblk.ufs2[i]; 366 if (blkno != 0) 367 ret |= dirindir(ino, blkno, ind_level, filesize, 368 tapesize, nodump, maxino); 369 } 370 return (ret); 371 } 372 373 /* 374 * Scan a disk block containing directory information looking to see if 375 * any of the entries are on the dump list and to see if the directory 376 * contains any subdirectories. 377 */ 378 static int 379 searchdir( 380 ino_t ino, 381 ufs2_daddr_t blkno, 382 long size, 383 long filesize, 384 long *tapesize, 385 int nodump, 386 ino_t maxino) 387 { 388 int mode; 389 struct direct *dp; 390 union dinode *ip; 391 long loc, ret = 0; 392 static caddr_t dblk; 393 394 if (dblk == NULL && (dblk = malloc(sblock->fs_bsize)) == NULL) 395 quit("searchdir: cannot allocate indirect memory.\n"); 396 bread(fsbtodb(sblock, blkno), dblk, (int)size); 397 if (filesize < size) 398 size = filesize; 399 for (loc = 0; loc < size; ) { 400 dp = (struct direct *)(dblk + loc); 401 if (dp->d_reclen == 0) { 402 msg("corrupted directory, inumber %ju\n", 403 (uintmax_t)ino); 404 break; 405 } 406 loc += dp->d_reclen; 407 if (dp->d_ino == 0) 408 continue; 409 if (dp->d_ino >= maxino) { 410 msg("corrupted directory entry, d_ino %ju >= %ju\n", 411 (uintmax_t)dp->d_ino, (uintmax_t)maxino); 412 break; 413 } 414 if (dp->d_name[0] == '.') { 415 if (dp->d_name[1] == '\0') 416 continue; 417 if (dp->d_name[1] == '.' && dp->d_name[2] == '\0') 418 continue; 419 } 420 if (nodump) { 421 ip = getino(dp->d_ino, &mode); 422 if (TSTINO(dp->d_ino, dumpinomap)) { 423 CLRINO(dp->d_ino, dumpinomap); 424 *tapesize -= blockest(ip); 425 } 426 /* 427 * Add back to dumpdirmap and remove from usedinomap 428 * to propagate nodump. 429 */ 430 if (mode == IFDIR) { 431 SETINO(dp->d_ino, dumpdirmap); 432 CLRINO(dp->d_ino, usedinomap); 433 ret |= HASSUBDIRS; 434 } 435 } else { 436 if (TSTINO(dp->d_ino, dumpinomap)) { 437 ret |= HASDUMPEDFILE; 438 if (ret & HASSUBDIRS) 439 break; 440 } 441 if (TSTINO(dp->d_ino, dumpdirmap)) { 442 ret |= HASSUBDIRS; 443 if (ret & HASDUMPEDFILE) 444 break; 445 } 446 } 447 } 448 return (ret); 449 } 450 451 /* 452 * Dump passes 3 and 4. 453 * 454 * Dump the contents of an inode to tape. 455 */ 456 void 457 dumpino(union dinode *dp, ino_t ino) 458 { 459 int ind_level, cnt, last, added; 460 off_t size; 461 char buf[TP_BSIZE]; 462 463 if (newtape) { 464 newtape = 0; 465 dumpmap(dumpinomap, TS_BITS, ino); 466 } 467 CLRINO(ino, dumpinomap); 468 /* 469 * Zero out the size of a snapshot so that it will be dumped 470 * as a zero length file. 471 */ 472 if ((DIP(dp, di_flags) & SF_SNAPSHOT) != 0) { 473 DIP_SET(dp, di_size, 0); 474 DIP_SET(dp, di_flags, DIP(dp, di_flags) & ~SF_SNAPSHOT); 475 } 476 if (sblock->fs_magic == FS_UFS1_MAGIC) { 477 spcl.c_mode = dp->dp1.di_mode; 478 spcl.c_size = dp->dp1.di_size; 479 spcl.c_extsize = 0; 480 spcl.c_atime = _time32_to_time(dp->dp1.di_atime); 481 spcl.c_atimensec = dp->dp1.di_atimensec; 482 spcl.c_mtime = _time32_to_time(dp->dp1.di_mtime); 483 spcl.c_mtimensec = dp->dp1.di_mtimensec; 484 spcl.c_birthtime = 0; 485 spcl.c_birthtimensec = 0; 486 spcl.c_rdev = dp->dp1.di_rdev; 487 spcl.c_file_flags = dp->dp1.di_flags; 488 spcl.c_uid = dp->dp1.di_uid; 489 spcl.c_gid = dp->dp1.di_gid; 490 } else { 491 spcl.c_mode = dp->dp2.di_mode; 492 spcl.c_size = dp->dp2.di_size; 493 spcl.c_extsize = dp->dp2.di_extsize; 494 spcl.c_atime = _time64_to_time(dp->dp2.di_atime); 495 spcl.c_atimensec = dp->dp2.di_atimensec; 496 spcl.c_mtime = _time64_to_time(dp->dp2.di_mtime); 497 spcl.c_mtimensec = dp->dp2.di_mtimensec; 498 spcl.c_birthtime = _time64_to_time(dp->dp2.di_birthtime); 499 spcl.c_birthtimensec = dp->dp2.di_birthnsec; 500 spcl.c_rdev = dp->dp2.di_rdev; 501 spcl.c_file_flags = dp->dp2.di_flags; 502 spcl.c_uid = dp->dp2.di_uid; 503 spcl.c_gid = dp->dp2.di_gid; 504 } 505 spcl.c_type = TS_INODE; 506 spcl.c_count = 0; 507 switch (DIP(dp, di_mode) & S_IFMT) { 508 509 case 0: 510 /* 511 * Freed inode. 512 */ 513 return; 514 515 case S_IFLNK: 516 /* 517 * Check for short symbolic link. 518 */ 519 if (DIP(dp, di_size) > 0 && 520 DIP(dp, di_size) < sblock->fs_maxsymlinklen) { 521 spcl.c_addr[0] = 1; 522 spcl.c_count = 1; 523 added = appendextdata(dp); 524 writeheader(ino); 525 if (sblock->fs_magic == FS_UFS1_MAGIC) 526 memmove(buf, (caddr_t)dp->dp1.di_db, 527 (u_long)DIP(dp, di_size)); 528 else 529 memmove(buf, (caddr_t)dp->dp2.di_db, 530 (u_long)DIP(dp, di_size)); 531 buf[DIP(dp, di_size)] = '\0'; 532 writerec(buf, 0); 533 writeextdata(dp, ino, added); 534 return; 535 } 536 /* FALLTHROUGH */ 537 538 case S_IFDIR: 539 case S_IFREG: 540 if (DIP(dp, di_size) > 0) 541 break; 542 /* FALLTHROUGH */ 543 544 case S_IFIFO: 545 case S_IFSOCK: 546 case S_IFCHR: 547 case S_IFBLK: 548 added = appendextdata(dp); 549 writeheader(ino); 550 writeextdata(dp, ino, added); 551 return; 552 553 default: 554 msg("Warning: undefined file type 0%o\n", 555 DIP(dp, di_mode) & IFMT); 556 return; 557 } 558 if (DIP(dp, di_size) > UFS_NDADDR * sblock->fs_bsize) { 559 cnt = UFS_NDADDR * sblock->fs_frag; 560 last = 0; 561 } else { 562 cnt = howmany(DIP(dp, di_size), sblock->fs_fsize); 563 last = 1; 564 } 565 if (sblock->fs_magic == FS_UFS1_MAGIC) 566 ufs1_blksout(&dp->dp1.di_db[0], cnt, ino); 567 else 568 ufs2_blksout(dp, &dp->dp2.di_db[0], cnt, ino, last); 569 if ((size = DIP(dp, di_size) - UFS_NDADDR * sblock->fs_bsize) <= 0) 570 return; 571 for (ind_level = 0; ind_level < UFS_NIADDR; ind_level++) { 572 dmpindir(dp, ino, DIP(dp, di_ib[ind_level]), ind_level, &size); 573 if (size <= 0) 574 return; 575 } 576 } 577 578 /* 579 * Read indirect blocks, and pass the data blocks to be dumped. 580 */ 581 static void 582 dmpindir(union dinode *dp, ino_t ino, ufs2_daddr_t blk, int ind_level, 583 off_t *size) 584 { 585 union { 586 ufs1_daddr_t ufs1[MAXBSIZE / sizeof(ufs1_daddr_t)]; 587 ufs2_daddr_t ufs2[MAXBSIZE / sizeof(ufs2_daddr_t)]; 588 } idblk; 589 int i, cnt, last; 590 591 if (blk != 0) 592 bread(fsbtodb(sblock, blk), (char *)&idblk, 593 (int)sblock->fs_bsize); 594 else 595 memset(&idblk, 0, sblock->fs_bsize); 596 if (ind_level <= 0) { 597 if (*size > NINDIR(sblock) * sblock->fs_bsize) { 598 cnt = NINDIR(sblock) * sblock->fs_frag; 599 last = 0; 600 } else { 601 cnt = howmany(*size, sblock->fs_fsize); 602 last = 1; 603 } 604 *size -= NINDIR(sblock) * sblock->fs_bsize; 605 if (sblock->fs_magic == FS_UFS1_MAGIC) 606 ufs1_blksout(idblk.ufs1, cnt, ino); 607 else 608 ufs2_blksout(dp, idblk.ufs2, cnt, ino, last); 609 return; 610 } 611 ind_level--; 612 for (i = 0; i < NINDIR(sblock); i++) { 613 if (sblock->fs_magic == FS_UFS1_MAGIC) 614 dmpindir(dp, ino, idblk.ufs1[i], ind_level, size); 615 else 616 dmpindir(dp, ino, idblk.ufs2[i], ind_level, size); 617 if (*size <= 0) 618 return; 619 } 620 } 621 622 /* 623 * Collect up the data into tape record sized buffers and output them. 624 */ 625 static void 626 ufs1_blksout(ufs1_daddr_t *blkp, int frags, ino_t ino) 627 { 628 ufs1_daddr_t *bp; 629 int i, j, count, blks, tbperdb; 630 631 blks = howmany(frags * sblock->fs_fsize, TP_BSIZE); 632 tbperdb = sblock->fs_bsize >> tp_bshift; 633 for (i = 0; i < blks; i += TP_NINDIR) { 634 if (i + TP_NINDIR > blks) 635 count = blks; 636 else 637 count = i + TP_NINDIR; 638 for (j = i; j < count; j++) 639 if (blkp[j / tbperdb] != 0) 640 spcl.c_addr[j - i] = 1; 641 else 642 spcl.c_addr[j - i] = 0; 643 spcl.c_count = count - i; 644 writeheader(ino); 645 bp = &blkp[i / tbperdb]; 646 for (j = i; j < count; j += tbperdb, bp++) 647 if (*bp != 0) { 648 if (j + tbperdb <= count) 649 dumpblock(*bp, (int)sblock->fs_bsize); 650 else 651 dumpblock(*bp, (count - j) * TP_BSIZE); 652 } 653 spcl.c_type = TS_ADDR; 654 } 655 } 656 657 /* 658 * Collect up the data into tape record sized buffers and output them. 659 */ 660 static void 661 ufs2_blksout(union dinode *dp, ufs2_daddr_t *blkp, int frags, ino_t ino, 662 int last) 663 { 664 ufs2_daddr_t *bp; 665 int i, j, count, resid, blks, tbperdb, added; 666 static int writingextdata = 0; 667 668 /* 669 * Calculate the number of TP_BSIZE blocks to be dumped. 670 * For filesystems with a fragment size bigger than TP_BSIZE, 671 * only part of the final fragment may need to be dumped. 672 */ 673 blks = howmany(frags * sblock->fs_fsize, TP_BSIZE); 674 if (last) { 675 if (writingextdata) 676 resid = howmany(fragoff(sblock, spcl.c_extsize), 677 TP_BSIZE); 678 else 679 resid = howmany(fragoff(sblock, dp->dp2.di_size), 680 TP_BSIZE); 681 if (resid > 0) 682 blks -= howmany(sblock->fs_fsize, TP_BSIZE) - resid; 683 } 684 tbperdb = sblock->fs_bsize >> tp_bshift; 685 for (i = 0; i < blks; i += TP_NINDIR) { 686 if (i + TP_NINDIR > blks) 687 count = blks; 688 else 689 count = i + TP_NINDIR; 690 for (j = i; j < count; j++) 691 if (blkp[j / tbperdb] != 0) 692 spcl.c_addr[j - i] = 1; 693 else 694 spcl.c_addr[j - i] = 0; 695 spcl.c_count = count - i; 696 if (last && count == blks && !writingextdata) 697 added = appendextdata(dp); 698 writeheader(ino); 699 bp = &blkp[i / tbperdb]; 700 for (j = i; j < count; j += tbperdb, bp++) 701 if (*bp != 0) { 702 if (j + tbperdb <= count) 703 dumpblock(*bp, (int)sblock->fs_bsize); 704 else 705 dumpblock(*bp, (count - j) * TP_BSIZE); 706 } 707 spcl.c_type = TS_ADDR; 708 spcl.c_count = 0; 709 if (last && count == blks && !writingextdata) { 710 writingextdata = 1; 711 writeextdata(dp, ino, added); 712 writingextdata = 0; 713 } 714 } 715 } 716 717 /* 718 * If there is room in the current block for the extended attributes 719 * as well as the file data, update the header to reflect the added 720 * attribute data at the end. Attributes are placed at the end so that 721 * old versions of restore will correctly restore the file and simply 722 * discard the extra data at the end that it does not understand. 723 * The attribute data is dumped following the file data by the 724 * writeextdata() function (below). 725 */ 726 static int 727 appendextdata(union dinode *dp) 728 { 729 int i, blks, tbperdb; 730 731 /* 732 * If no extended attributes, there is nothing to do. 733 */ 734 if (spcl.c_extsize == 0) 735 return (0); 736 /* 737 * If there is not enough room at the end of this block 738 * to add the extended attributes, then rather than putting 739 * part of them here, we simply push them entirely into a 740 * new block rather than putting some here and some later. 741 */ 742 if (spcl.c_extsize > UFS_NXADDR * sblock->fs_bsize) 743 blks = howmany(UFS_NXADDR * sblock->fs_bsize, TP_BSIZE); 744 else 745 blks = howmany(spcl.c_extsize, TP_BSIZE); 746 if (spcl.c_count + blks > TP_NINDIR) 747 return (0); 748 /* 749 * Update the block map in the header to indicate the added 750 * extended attribute. They will be appended after the file 751 * data by the writeextdata() routine. 752 */ 753 tbperdb = sblock->fs_bsize >> tp_bshift; 754 for (i = 0; i < blks; i++) 755 if (&dp->dp2.di_extb[i / tbperdb] != 0) 756 spcl.c_addr[spcl.c_count + i] = 1; 757 else 758 spcl.c_addr[spcl.c_count + i] = 0; 759 spcl.c_count += blks; 760 return (blks); 761 } 762 763 /* 764 * Dump the extended attribute data. If there was room in the file 765 * header, then all we need to do is output the data blocks. If there 766 * was not room in the file header, then an additional TS_ADDR header 767 * is created to hold the attribute data. 768 */ 769 static void 770 writeextdata(union dinode *dp, ino_t ino, int added) 771 { 772 int i, frags, blks, tbperdb, last; 773 ufs2_daddr_t *bp; 774 off_t size; 775 776 /* 777 * If no extended attributes, there is nothing to do. 778 */ 779 if (spcl.c_extsize == 0) 780 return; 781 /* 782 * If there was no room in the file block for the attributes, 783 * dump them out in a new block, otherwise just dump the data. 784 */ 785 if (added == 0) { 786 if (spcl.c_extsize > UFS_NXADDR * sblock->fs_bsize) { 787 frags = UFS_NXADDR * sblock->fs_frag; 788 last = 0; 789 } else { 790 frags = howmany(spcl.c_extsize, sblock->fs_fsize); 791 last = 1; 792 } 793 ufs2_blksout(dp, &dp->dp2.di_extb[0], frags, ino, last); 794 } else { 795 if (spcl.c_extsize > UFS_NXADDR * sblock->fs_bsize) 796 blks = howmany(UFS_NXADDR * sblock->fs_bsize, TP_BSIZE); 797 else 798 blks = howmany(spcl.c_extsize, TP_BSIZE); 799 tbperdb = sblock->fs_bsize >> tp_bshift; 800 for (i = 0; i < blks; i += tbperdb) { 801 bp = &dp->dp2.di_extb[i / tbperdb]; 802 if (*bp != 0) { 803 if (i + tbperdb <= blks) 804 dumpblock(*bp, (int)sblock->fs_bsize); 805 else 806 dumpblock(*bp, (blks - i) * TP_BSIZE); 807 } 808 } 809 810 } 811 /* 812 * If an indirect block is added for extended attributes, then 813 * di_exti below should be changed to the structure element 814 * that references the extended attribute indirect block. This 815 * definition is here only to make it compile without complaint. 816 */ 817 #define di_exti di_spare[0] 818 /* 819 * If the extended attributes fall into an indirect block, 820 * dump it as well. 821 */ 822 if ((size = spcl.c_extsize - UFS_NXADDR * sblock->fs_bsize) > 0) 823 dmpindir(dp, ino, dp->dp2.di_exti, 0, &size); 824 } 825 826 /* 827 * Dump a map to the tape. 828 */ 829 void 830 dumpmap(char *map, int type, ino_t ino) 831 { 832 int i; 833 char *cp; 834 835 spcl.c_type = type; 836 spcl.c_count = howmany(mapsize * sizeof(char), TP_BSIZE); 837 writeheader(ino); 838 for (i = 0, cp = map; i < spcl.c_count; i++, cp += TP_BSIZE) 839 writerec(cp, 0); 840 } 841 842 /* 843 * Write a header record to the dump tape. 844 */ 845 void 846 writeheader(ino_t ino) 847 { 848 int32_t sum, cnt, *lp; 849 850 if (rsync_friendly >= 2) { 851 /* don't track changes to access time */ 852 spcl.c_atime = spcl.c_mtime; 853 spcl.c_atimensec = spcl.c_mtimensec; 854 } 855 spcl.c_inumber = ino; 856 spcl.c_magic = FS_UFS2_MAGIC; 857 spcl.c_checksum = 0; 858 lp = (int32_t *)&spcl; 859 sum = 0; 860 cnt = sizeof(union u_spcl) / (4 * sizeof(int32_t)); 861 while (--cnt >= 0) { 862 sum += *lp++; 863 sum += *lp++; 864 sum += *lp++; 865 sum += *lp++; 866 } 867 spcl.c_checksum = CHECKSUM - sum; 868 writerec((char *)&spcl, 1); 869 } 870 871 union dinode * 872 getino(ino_t inum, int *modep) 873 { 874 static ino_t minino, maxino; 875 static caddr_t inoblock; 876 struct ufs1_dinode *dp1; 877 struct ufs2_dinode *dp2; 878 879 if (inoblock == NULL && (inoblock = malloc(sblock->fs_bsize)) == NULL) 880 quit("cannot allocate inode memory.\n"); 881 curino = inum; 882 if (inum >= minino && inum < maxino) 883 goto gotit; 884 bread(fsbtodb(sblock, ino_to_fsba(sblock, inum)), inoblock, 885 (int)sblock->fs_bsize); 886 minino = inum - (inum % INOPB(sblock)); 887 maxino = minino + INOPB(sblock); 888 gotit: 889 if (sblock->fs_magic == FS_UFS1_MAGIC) { 890 dp1 = &((struct ufs1_dinode *)inoblock)[inum - minino]; 891 *modep = (dp1->di_mode & IFMT); 892 return ((union dinode *)dp1); 893 } 894 dp2 = &((struct ufs2_dinode *)inoblock)[inum - minino]; 895 *modep = (dp2->di_mode & IFMT); 896 return ((union dinode *)dp2); 897 } 898 899 /* 900 * Read a chunk of data from the disk. 901 * Try to recover from hard errors by reading in sector sized pieces. 902 * Error recovery is attempted at most BREADEMAX times before seeking 903 * consent from the operator to continue. 904 */ 905 int breaderrors = 0; 906 #define BREADEMAX 32 907 908 void 909 bread(ufs2_daddr_t blkno, char *buf, int size) 910 { 911 int secsize, bytes, resid, xfer, base, cnt, i; 912 static char *tmpbuf; 913 off_t offset; 914 915 loop: 916 offset = blkno << dev_bshift; 917 secsize = sblock->fs_fsize; 918 base = offset % secsize; 919 resid = size % secsize; 920 /* 921 * If the transfer request starts or ends on a non-sector 922 * boundary, we must read the entire sector and copy out 923 * just the part that we need. 924 */ 925 if (base == 0 && resid == 0) { 926 cnt = cread(diskfd, buf, size, offset); 927 if (cnt == size) 928 return; 929 } else { 930 if (tmpbuf == NULL && (tmpbuf = malloc(secsize)) == NULL) 931 quit("buffer malloc failed\n"); 932 xfer = 0; 933 bytes = size; 934 if (base != 0) { 935 cnt = cread(diskfd, tmpbuf, secsize, offset - base); 936 if (cnt != secsize) 937 goto bad; 938 xfer = MIN(secsize - base, size); 939 offset += xfer; 940 bytes -= xfer; 941 resid = bytes % secsize; 942 memcpy(buf, &tmpbuf[base], xfer); 943 } 944 if (bytes >= secsize) { 945 cnt = cread(diskfd, &buf[xfer], bytes - resid, offset); 946 if (cnt != bytes - resid) 947 goto bad; 948 xfer += cnt; 949 offset += cnt; 950 } 951 if (resid == 0) 952 return; 953 cnt = cread(diskfd, tmpbuf, secsize, offset); 954 if (cnt == secsize) { 955 memcpy(&buf[xfer], tmpbuf, resid); 956 return; 957 } 958 } 959 bad: 960 if (blkno + (size / dev_bsize) > fsbtodb(sblock, sblock->fs_size)) { 961 /* 962 * Trying to read the final fragment. 963 * 964 * NB - dump only works in TP_BSIZE blocks, hence 965 * rounds `dev_bsize' fragments up to TP_BSIZE pieces. 966 * It should be smarter about not actually trying to 967 * read more than it can get, but for the time being 968 * we punt and scale back the read only when it gets 969 * us into trouble. (mkm 9/25/83) 970 */ 971 size -= dev_bsize; 972 goto loop; 973 } 974 if (cnt == -1) 975 msg("read error from %s: %s: [block %jd]: count=%d\n", 976 disk, strerror(errno), (intmax_t)blkno, size); 977 else 978 msg("short read error from %s: [block %jd]: count=%d, got=%d\n", 979 disk, (intmax_t)blkno, size, cnt); 980 if (++breaderrors > BREADEMAX) { 981 msg("More than %d block read errors from %s\n", 982 BREADEMAX, disk); 983 broadcast("DUMP IS AILING!\n"); 984 msg("This is an unrecoverable error.\n"); 985 if (!query("Do you want to attempt to continue?")){ 986 dumpabort(0); 987 /*NOTREACHED*/ 988 } else 989 breaderrors = 0; 990 } 991 /* 992 * Zero buffer, then try to read each sector of buffer separately, 993 * and bypass the cache. 994 */ 995 memset(buf, 0, size); 996 for (i = 0; i < size; i += dev_bsize, buf += dev_bsize, blkno++) { 997 if ((cnt = pread(diskfd, buf, (int)dev_bsize, 998 ((off_t)blkno << dev_bshift))) == dev_bsize) 999 continue; 1000 if (cnt == -1) { 1001 msg("read error from %s: %s: [sector %jd]: count=%ld\n", 1002 disk, strerror(errno), (intmax_t)blkno, dev_bsize); 1003 continue; 1004 } 1005 msg("short read from %s: [sector %jd]: count=%ld, got=%d\n", 1006 disk, (intmax_t)blkno, dev_bsize, cnt); 1007 } 1008 } 1009