traverse.c revision 1.43 1 /* $NetBSD: traverse.c,v 1.43 2004/03/24 12:28:51 hannken 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. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32 #include <sys/cdefs.h>
33 #ifndef lint
34 #if 0
35 static char sccsid[] = "@(#)traverse.c 8.7 (Berkeley) 6/15/95";
36 #else
37 __RCSID("$NetBSD: traverse.c,v 1.43 2004/03/24 12:28:51 hannken Exp $");
38 #endif
39 #endif /* not lint */
40
41 #include <sys/param.h>
42 #include <sys/time.h>
43 #include <sys/stat.h>
44 #include <ufs/ufs/dir.h>
45 #include <ufs/ufs/dinode.h>
46 #include <ufs/ffs/fs.h>
47 #include <ufs/ffs/ffs_extern.h>
48
49 #include <protocols/dumprestore.h>
50
51 #include <ctype.h>
52 #include <errno.h>
53 #include <fts.h>
54 #include <stdio.h>
55 #include <stdlib.h>
56 #include <string.h>
57 #include <unistd.h>
58
59 #include "dump.h"
60
61 #define HASDUMPEDFILE 0x1
62 #define HASSUBDIRS 0x2
63
64 static int dirindir(ino_t, daddr_t, int, off_t *, u_int64_t *, int);
65 static void dmpindir(ino_t, daddr_t, int, off_t *);
66 static int searchdir(ino_t, daddr_t, long, off_t, u_int64_t *, int);
67
68 /*
69 * This is an estimation of the number of TP_BSIZE blocks in the file.
70 * It estimates the number of blocks in files with holes by assuming
71 * that all of the blocks accounted for by di_blocks are data blocks
72 * (when some of the blocks are usually used for indirect pointers);
73 * hence the estimate may be high.
74 */
75 int64_t
76 blockest(union dinode *dp)
77 {
78 int64_t blkest, sizeest;
79
80 /*
81 * dp->di_size is the size of the file in bytes.
82 * dp->di_blocks stores the number of sectors actually in the file.
83 * If there are more sectors than the size would indicate, this just
84 * means that there are indirect blocks in the file or unused
85 * sectors in the last file block; we can safely ignore these
86 * (blkest = sizeest below).
87 * If the file is bigger than the number of sectors would indicate,
88 * then the file has holes in it. In this case we must use the
89 * block count to estimate the number of data blocks used, but
90 * we use the actual size for estimating the number of indirect
91 * dump blocks (sizeest vs. blkest in the indirect block
92 * calculation).
93 */
94 blkest = howmany(ufs_fragroundup(ufsib,
95 dbtob((u_int64_t)DIP(dp, blocks))), TP_BSIZE);
96 sizeest = howmany(ufs_fragroundup(ufsib, DIP(dp, size)), TP_BSIZE);
97 if (blkest > sizeest)
98 blkest = sizeest;
99 if (DIP(dp, size) > ufsib->ufs_bsize * NDADDR) {
100 /* calculate the number of indirect blocks on the dump tape */
101 blkest +=
102 howmany(sizeest - NDADDR * ufsib->ufs_bsize / TP_BSIZE,
103 TP_NINDIR);
104 }
105 return (blkest + 1);
106 }
107
108 /* Auxiliary macro to pick up files changed since previous dump. */
109 #define CHANGEDSINCE(dp, t) \
110 (DIP((dp), mtime) >= (t) || DIP((dp), ctime) >= (t))
111
112 /* The WANTTODUMP macro decides whether a file should be dumped. */
113 #ifdef UF_NODUMP
114 #define WANTTODUMP(dp) \
115 (CHANGEDSINCE(dp, iswap64(spcl.c_ddate)) && \
116 (nonodump || (DIP((dp), flags) & UF_NODUMP) != UF_NODUMP))
117 #else
118 #define WANTTODUMP(dp) CHANGEDSINCE(dp, iswap64(spcl.c_ddate))
119 #endif
120
121 /*
122 * Determine if given inode should be dumped
123 */
124 void
125 mapfileino(ino_t ino, u_int64_t *tape_size, int *dirskipped)
126 {
127 int mode;
128 union dinode *dp;
129
130 /*
131 * Skip inode if we've already marked it for dumping
132 */
133 if (TSTINO(ino, usedinomap))
134 return;
135 dp = getino(ino);
136 if ((mode = (DIP(dp, mode) & IFMT)) == 0)
137 return;
138 /*
139 * Put all dirs in dumpdirmap, inodes that are to be dumped in the
140 * used map. All inode but dirs who have the nodump attribute go
141 * to the usedinomap.
142 */
143 SETINO(ino, usedinomap);
144 if (mode == IFDIR)
145 SETINO(ino, dumpdirmap);
146 if (WANTTODUMP(dp)) {
147 SETINO(ino, dumpinomap);
148 if (mode != IFREG && mode != IFDIR && mode != IFLNK)
149 *tape_size += 1;
150 else
151 *tape_size += blockest(dp);
152 return;
153 }
154 if (mode == IFDIR) {
155 #ifdef UF_NODUMP
156 if (!nonodump && (DIP(dp, flags) & UF_NODUMP))
157 CLRINO(ino, usedinomap);
158 #endif
159 *dirskipped = 1;
160 }
161 }
162
163 /*
164 * Dump pass 1.
165 *
166 * Walk the inode list for a file system to find all allocated inodes
167 * that have been modified since the previous dump time. Also, find all
168 * the directories in the file system.
169 * disk may be NULL if dirv is NULL.
170 */
171 int
172 mapfiles(ino_t maxino, u_int64_t *tape_size, char *diskname, char * const *dirv)
173 {
174 int anydirskipped = 0;
175
176 if (dirv != NULL) {
177 char curdir[MAXPATHLEN];
178 FTS *dirh;
179 FTSENT *entry;
180 int d;
181
182 if (getcwd(curdir, sizeof(curdir)) == NULL) {
183 msg("Can't determine cwd: %s\n", strerror(errno));
184 dumpabort(0);
185 }
186 if ((dirh = fts_open(dirv, FTS_PHYSICAL|FTS_SEEDOT|FTS_XDEV,
187 NULL)) == NULL) {
188 msg("fts_open failed: %s\n", strerror(errno));
189 dumpabort(0);
190 }
191 while ((entry = fts_read(dirh)) != NULL) {
192 switch (entry->fts_info) {
193 case FTS_DNR: /* an error */
194 case FTS_ERR:
195 case FTS_NS:
196 msg("Can't fts_read %s: %s\n", entry->fts_path,
197 strerror(errno));
198 case FTS_DP: /* already seen dir */
199 continue;
200 }
201 mapfileino(entry->fts_statp->st_ino, tape_size,
202 &anydirskipped);
203 }
204 (void)fts_close(dirh);
205
206 /*
207 * Add any parent directories
208 */
209 for (d = 0 ; dirv[d] != NULL ; d++) {
210 char path[MAXPATHLEN];
211
212 if (dirv[d][0] != '/')
213 (void)snprintf(path, sizeof(path), "%s/%s",
214 curdir, dirv[d]);
215 else
216 (void)snprintf(path, sizeof(path), "%s",
217 dirv[d]);
218 while (strcmp(path, diskname) != 0) {
219 char *p;
220 struct stat sb;
221
222 if (*path == '\0')
223 break;
224 if ((p = strrchr(path, '/')) == NULL)
225 break;
226 if (p == path)
227 break;
228 *p = '\0';
229 if (stat(path, &sb) == -1) {
230 msg("Can't stat %s: %s\n", path,
231 strerror(errno));
232 break;
233 }
234 mapfileino(sb.st_ino, tape_size, &anydirskipped);
235 }
236 }
237
238 /*
239 * Ensure that the root inode actually appears in the
240 * file list for a subdir
241 */
242 mapfileino(ROOTINO, tape_size, &anydirskipped);
243 } else {
244 fs_mapinodes(maxino, tape_size, &anydirskipped);
245 }
246 /*
247 * Restore gets very upset if the root is not dumped,
248 * so ensure that it always is dumped.
249 */
250 SETINO(ROOTINO, dumpinomap);
251 return (anydirskipped);
252 }
253
254 /*
255 * Dump pass 2.
256 *
257 * Scan each directory on the file system to see if it has any modified
258 * files in it. If it does, and has not already been added to the dump
259 * list (because it was itself modified), then add it. If a directory
260 * has not been modified itself, contains no modified files and has no
261 * subdirectories, then it can be deleted from the dump list and from
262 * the list of directories. By deleting it from the list of directories,
263 * its parent may now qualify for the same treatment on this or a later
264 * pass using this algorithm.
265 */
266 int
267 mapdirs(ino_t maxino, u_int64_t *tape_size)
268 {
269 union dinode *dp, di;
270 int i, isdir, nodump;
271 char *map;
272 ino_t ino;
273 off_t filesize;
274 int ret, change = 0;
275 daddr_t blk;
276
277 isdir = 0; /* XXX just to get gcc to shut up */
278 for (map = dumpdirmap, ino = 1; ino < maxino; ino++) {
279 if (((ino - 1) % NBBY) == 0) /* map is offset by 1 */
280 isdir = *map++;
281 else
282 isdir >>= 1;
283 /*
284 * If dir has been removed from the used map, it's either
285 * because it had the nodump flag, or it inherited it from
286 * its parent. A directory can't be in dumpinomap if
287 * not in usedinomap, but we have to go throuh it anyway
288 * to propagate the nodump attribute.
289 */
290 nodump = (TSTINO(ino, usedinomap) == 0);
291 if ((isdir & 1) == 0 ||
292 (TSTINO(ino, dumpinomap) && nodump == 0))
293 continue;
294
295 dp = getino(ino);
296 /*
297 * inode buf may be changed in searchdir().
298 */
299 if (is_ufs2)
300 di.dp2 = dp->dp2;
301 else
302 di.dp1 = dp->dp1;
303 filesize = DIP(&di, size);
304 for (ret = 0, i = 0; filesize > 0 && i < NDADDR; i++) {
305 if (is_ufs2)
306 blk = iswap64(di.dp2.di_db[i]);
307 else
308 blk = iswap32(di.dp1.di_db[i]);
309 if (blk != 0)
310 ret |= searchdir(ino, blk,
311 (long)ufs_dblksize(ufsib, &di, i),
312 filesize, tape_size, nodump);
313 if (ret & HASDUMPEDFILE)
314 filesize = 0;
315 else
316 filesize -= ufsib->ufs_bsize;
317 }
318 for (i = 0; filesize > 0 && i < NIADDR; i++) {
319 if (is_ufs2)
320 blk = iswap64(di.dp2.di_ib[i]);
321 else
322 blk = iswap32(di.dp1.di_ib[i]);
323 if (blk == 0)
324 continue;
325 ret |= dirindir(ino, blk, i, &filesize,
326 tape_size, nodump);
327 }
328 if (ret & HASDUMPEDFILE) {
329 SETINO(ino, dumpinomap);
330 *tape_size += blockest(&di);
331 change = 1;
332 continue;
333 }
334 if (nodump) {
335 if (ret & HASSUBDIRS)
336 change = 1; /* subdirs have inherited nodump */
337 CLRINO(ino, dumpdirmap);
338 } else if ((ret & HASSUBDIRS) == 0) {
339 if (!TSTINO(ino, dumpinomap)) {
340 CLRINO(ino, dumpdirmap);
341 change = 1;
342 }
343 }
344 }
345 return (change);
346 }
347
348 /*
349 * Read indirect blocks, and pass the data blocks to be searched
350 * as directories. Quit as soon as any entry is found that will
351 * require the directory to be dumped.
352 */
353 static int
354 dirindir(ino_t ino, daddr_t blkno, int ind_level, off_t *filesize,
355 u_int64_t *tape_size, int nodump)
356 {
357 int ret = 0;
358 int i;
359 union {
360 int64_t i64[MAXBSIZE / sizeof (int64_t)];
361 int32_t i32[MAXBSIZE / sizeof (int32_t)];
362 } idblk;
363
364 bread(fsatoda(ufsib, blkno), (char *)&idblk, (int)ufsib->ufs_bsize);
365 if (ind_level <= 0) {
366 for (i = 0; *filesize > 0 && i < ufsib->ufs_nindir; i++) {
367 if (is_ufs2)
368 blkno = iswap64(idblk.i64[i]);
369 else
370 blkno = iswap32(idblk.i32[i]);
371 if (blkno != 0)
372 ret |= searchdir(ino, blkno,
373 ufsib->ufs_bsize, *filesize,
374 tape_size, nodump);
375 if (ret & HASDUMPEDFILE)
376 *filesize = 0;
377 else
378 *filesize -= ufsib->ufs_bsize;
379 }
380 return (ret);
381 }
382 ind_level--;
383 for (i = 0; *filesize > 0 && i < ufsib->ufs_nindir; i++) {
384 if (is_ufs2)
385 blkno = iswap64(idblk.i64[i]);
386 else
387 blkno = iswap32(idblk.i32[i]);
388 if (blkno != 0)
389 ret |= dirindir(ino, blkno, ind_level, filesize,
390 tape_size, nodump);
391 }
392 return (ret);
393 }
394
395 /*
396 * Scan a disk block containing directory information looking to see if
397 * any of the entries are on the dump list and to see if the directory
398 * contains any subdirectories.
399 */
400 static int
401 searchdir(ino_t dino, daddr_t blkno, long size, off_t filesize,
402 u_int64_t *tape_size, int nodump)
403 {
404 struct direct *dp;
405 union dinode *ip;
406 long loc, ret = 0;
407 char *dblk;
408 ino_t ino;
409
410 dblk = malloc(size);
411 if (dblk == NULL)
412 quit("searchdir: cannot allocate directory memory.\n");
413 bread(fsatoda(ufsib, blkno), dblk, (int)size);
414 if (filesize < size)
415 size = filesize;
416 for (loc = 0; loc < size; ) {
417 dp = (struct direct *)(dblk + loc);
418 if (dp->d_reclen == 0) {
419 msg("corrupted directory, inumber %d\n", dino);
420 break;
421 }
422 loc += iswap16(dp->d_reclen);
423 if (dp->d_ino == 0)
424 continue;
425 if (dp->d_name[0] == '.') {
426 if (dp->d_name[1] == '\0')
427 continue;
428 if (dp->d_name[1] == '.' && dp->d_name[2] == '\0')
429 continue;
430 }
431 ino = iswap32(dp->d_ino);
432 if (nodump) {
433 ip = getino(ino);
434 if (TSTINO(ino, dumpinomap)) {
435 CLRINO(ino, dumpinomap);
436 *tape_size -= blockest(ip);
437 }
438 /*
439 * Add back to dumpdirmap and remove from usedinomap
440 * to propagate nodump.
441 */
442 if ((DIP(ip, mode) & IFMT) == IFDIR) {
443 SETINO(ino, dumpdirmap);
444 CLRINO(ino, usedinomap);
445 ret |= HASSUBDIRS;
446 }
447 } else {
448 if (TSTINO(ino, dumpinomap)) {
449 ret |= HASDUMPEDFILE;
450 if (ret & HASSUBDIRS)
451 break;
452 }
453 if (TSTINO(ino, dumpdirmap)) {
454 ret |= HASSUBDIRS;
455 if (ret & HASDUMPEDFILE)
456 break;
457 }
458 }
459 }
460 free(dblk);
461 return (ret);
462 }
463
464 /*
465 * Dump passes 3 and 4.
466 *
467 * Dump the contents of an inode to tape.
468 */
469 void
470 dumpino(union dinode *dp, ino_t ino)
471 {
472 int ind_level, cnt;
473 off_t size;
474 char buf[TP_BSIZE];
475 daddr_t blk;
476 void *shortlink;
477
478 if (newtape) {
479 newtape = 0;
480 dumpmap(dumpinomap, TS_BITS, ino);
481 }
482 CLRINO(ino, dumpinomap);
483 if (!is_ufs2) {
484 if (needswap)
485 ffs_dinode1_swap(&dp->dp1, &spcl.c_dinode);
486 else
487 spcl.c_dinode = dp->dp1;
488 } else {
489 if (needswap)
490 ffs_dinode2_swap(&dp->dp2, &dp->dp2);
491 spcl.c_mode = dp->dp2.di_mode;
492 spcl.c_size = dp->dp2.di_size;
493 spcl.c_atime = dp->dp2.di_atime;
494 spcl.c_atimensec = dp->dp2.di_atimensec;
495 spcl.c_mtime = dp->dp2.di_mtime;
496 spcl.c_mtimensec = dp->dp2.di_mtimensec;
497 spcl.c_birthtime = dp->dp2.di_birthtime;
498 spcl.c_birthtimensec = dp->dp2.di_birthnsec;
499 spcl.c_rdev = dp->dp2.di_rdev;
500 spcl.c_file_flags = dp->dp2.di_flags;
501 spcl.c_uid = dp->dp2.di_uid;
502 spcl.c_gid = dp->dp2.di_gid;
503 }
504 spcl.c_type = iswap32(TS_INODE);
505 spcl.c_count = 0;
506 switch (DIP(dp, mode) & IFMT) {
507
508 case 0:
509 /*
510 * Freed inode.
511 */
512 return;
513
514 case IFLNK:
515 /*
516 * Check for short symbolic link.
517 */
518 if (DIP(dp, size) > 0 &&
519 #ifdef FS_44INODEFMT
520 (DIP(dp, size) < ufsib->ufs_maxsymlinklen ||
521 (ufsib->ufs_maxsymlinklen == 0 && DIP(dp, blocks) == 0))
522 #else
523 DIP(dp, blocks) == 0
524 #endif
525 ) {
526 spcl.c_addr[0] = 1;
527 spcl.c_count = iswap32(1);
528 writeheader(ino);
529 if (is_ufs2)
530 shortlink = dp->dp2.di_db;
531 else
532 shortlink = dp->dp1.di_db;
533 memmove(buf, shortlink, DIP(dp, size));
534 buf[DIP(dp, size)] = '\0';
535 writerec(buf, 0);
536 return;
537 }
538 /* fall through */
539
540 case IFDIR:
541 case IFREG:
542 if (DIP(dp, size) > 0)
543 break;
544 /* fall through */
545
546 case IFIFO:
547 case IFSOCK:
548 case IFCHR:
549 case IFBLK:
550 writeheader(ino);
551 return;
552
553 default:
554 msg("Warning: undefined file type 0%o\n", DIP(dp, mode) & IFMT);
555 return;
556 }
557 if (DIP(dp, size) > NDADDR * ufsib->ufs_bsize)
558 cnt = NDADDR * ufsib->ufs_frag;
559 else
560 cnt = howmany(DIP(dp, size), ufsib->ufs_fsize);
561 if (is_ufs2)
562 blksout64(&dp->dp2.di_db[0], cnt, ino);
563 else
564 blksout32(&dp->dp1.di_db[0], cnt, ino);
565
566 if ((size = DIP(dp, size) - NDADDR * ufsib->ufs_bsize) <= 0)
567 return;
568 for (ind_level = 0; ind_level < NIADDR; ind_level++) {
569 if (is_ufs2)
570 blk = iswap64(dp->dp2.di_ib[ind_level]);
571 else
572 blk = iswap32(dp->dp1.di_ib[ind_level]);
573 dmpindir(ino, blk, ind_level, &size);
574 if (size <= 0)
575 return;
576 }
577 }
578
579 /*
580 * Read indirect blocks, and pass the data blocks to be dumped.
581 */
582 static void
583 dmpindir(ino_t ino, daddr_t blk, int ind_level, off_t *size)
584 {
585 int i, cnt;
586 union {
587 int32_t i32[MAXBSIZE / sizeof (int32_t)];
588 int64_t i64[MAXBSIZE / sizeof (int64_t)];
589 } idblk;
590 daddr_t iblk;
591
592
593 if (blk != 0)
594 bread(fsatoda(ufsib, blk), (char *)&idblk,
595 (int) ufsib->ufs_bsize);
596 else
597 memset(&idblk, 0, (int)ufsib->ufs_bsize);
598 if (ind_level <= 0) {
599 if (*size < ufsib->ufs_nindir * ufsib->ufs_bsize)
600 cnt = howmany(*size, ufsib->ufs_fsize);
601 else
602 cnt = ufsib->ufs_nindir * ufsib->ufs_frag;
603 *size -= ufsib->ufs_nindir * ufsib->ufs_bsize;
604 if (is_ufs2)
605 blksout64(&idblk.i64[0], cnt, ino);
606 else
607 blksout32(&idblk.i32[0], cnt, ino);
608 return;
609 }
610 ind_level--;
611 for (i = 0; i < ufsib->ufs_nindir; i++) {
612 if (is_ufs2)
613 iblk = iswap64(idblk.i64[i]);
614 else
615 iblk = iswap32(idblk.i32[i]);
616 dmpindir(ino, iblk, 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 void
626 blksout32(int32_t *blkp, int frags, ino_t ino)
627 {
628 int32_t *bp;
629 int i, j, count, blks, tbperdb;
630
631 blks = howmany(frags * ufsib->ufs_fsize, TP_BSIZE);
632 tbperdb = ufsib->ufs_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 = iswap32(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(iswap32(*bp), (int)ufsib->ufs_bsize);
650 else
651 dumpblock(iswap32(*bp), (count - j) * TP_BSIZE);
652 }
653 spcl.c_type = iswap32(TS_ADDR);
654 }
655 }
656
657 void
658 blksout64(int64_t *blkp, int frags, ino_t ino)
659 {
660 int64_t *bp;
661 int i, j, count, blks, tbperdb;
662
663 blks = howmany(frags * ufsib->ufs_fsize, TP_BSIZE);
664 tbperdb = ufsib->ufs_bsize >> tp_bshift;
665 for (i = 0; i < blks; i += TP_NINDIR) {
666 if (i + TP_NINDIR > blks)
667 count = blks;
668 else
669 count = i + TP_NINDIR;
670 for (j = i; j < count; j++)
671 if (blkp[j / tbperdb] != 0)
672 spcl.c_addr[j - i] = 1;
673 else
674 spcl.c_addr[j - i] = 0;
675 spcl.c_count = iswap32(count - i);
676 writeheader(ino);
677 bp = &blkp[i / tbperdb];
678 for (j = i; j < count; j += tbperdb, bp++)
679 if (*bp != 0) {
680 if (j + tbperdb <= count)
681 dumpblock(iswap64(*bp), (int)ufsib->ufs_bsize);
682 else
683 dumpblock(iswap64(*bp), (count - j) * TP_BSIZE);
684 }
685 spcl.c_type = iswap32(TS_ADDR);
686 }
687 }
688
689 /*
690 * Dump a map to the tape.
691 */
692 void
693 dumpmap(char *map, int type, ino_t ino)
694 {
695 int i;
696 char *cp;
697
698 spcl.c_type = iswap32(type);
699 spcl.c_count = iswap32(howmany(mapsize * sizeof(char), TP_BSIZE));
700 writeheader(ino);
701 for (i = 0, cp = map; i < iswap32(spcl.c_count); i++, cp += TP_BSIZE)
702 writerec(cp, 0);
703 }
704
705 /*
706 * Write a header record to the dump tape.
707 */
708 void
709 writeheader(ino_t ino)
710 {
711 int32_t sum, cnt, *lp;
712
713 spcl.c_inumber = iswap32(ino);
714 if (is_ufs2)
715 spcl.c_magic = iswap32(FS_UFS2_MAGIC);
716 else {
717 spcl.c_magic = iswap32(NFS_MAGIC);
718 spcl.c_old_date = iswap32(iswap64(spcl.c_date));
719 spcl.c_old_ddate = iswap32(iswap64(spcl.c_ddate));
720 spcl.c_old_tapea = iswap32(iswap64(spcl.c_tapea));
721 spcl.c_old_firstrec = iswap32(iswap64(spcl.c_firstrec));
722 }
723 spcl.c_checksum = 0;
724 lp = (int32_t *)&spcl;
725 sum = 0;
726 cnt = sizeof(union u_spcl) / (4 * sizeof(int32_t));
727 while (--cnt >= 0) {
728 sum += iswap32(*lp++);
729 sum += iswap32(*lp++);
730 sum += iswap32(*lp++);
731 sum += iswap32(*lp++);
732 }
733 spcl.c_checksum = iswap32(CHECKSUM - sum);
734 writerec((char *)&spcl, 1);
735 }
736