Home | History | Annotate | Line # | Download | only in dump
traverse.c revision 1.42
      1 /*	$NetBSD: traverse.c,v 1.42 2003/08/07 10:04:14 agc 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.42 2003/08/07 10:04:14 agc 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(dbtob((u_int64_t)DIP(dp, blocks)), TP_BSIZE);
     95 	sizeest = howmany(DIP(dp, size), TP_BSIZE);
     96 	if (blkest > sizeest)
     97 		blkest = sizeest;
     98 	if (DIP(dp, size) > ufsib->ufs_bsize * NDADDR) {
     99 		/* calculate the number of indirect blocks on the dump tape */
    100 		blkest +=
    101 			howmany(sizeest - NDADDR * ufsib->ufs_bsize / TP_BSIZE,
    102 			TP_NINDIR);
    103 	}
    104 	return (blkest + 1);
    105 }
    106 
    107 /* Auxiliary macro to pick up files changed since previous dump. */
    108 #define	CHANGEDSINCE(dp, t) \
    109 	(DIP((dp), mtime) >= (t) || DIP((dp), ctime) >= (t))
    110 
    111 /* The WANTTODUMP macro decides whether a file should be dumped. */
    112 #ifdef UF_NODUMP
    113 #define	WANTTODUMP(dp) \
    114 	(CHANGEDSINCE(dp, iswap64(spcl.c_ddate)) && \
    115 	 (nonodump || (DIP((dp), flags) & UF_NODUMP) != UF_NODUMP))
    116 #else
    117 #define	WANTTODUMP(dp) CHANGEDSINCE(dp, iswap64(spcl.c_ddate))
    118 #endif
    119 
    120 /*
    121  * Determine if given inode should be dumped
    122  */
    123 void
    124 mapfileino(ino_t ino, u_int64_t *tape_size, int *dirskipped)
    125 {
    126 	int mode;
    127 	union dinode *dp;
    128 
    129 	/*
    130 	 * Skip inode if we've already marked it for dumping
    131 	 */
    132 	if (TSTINO(ino, usedinomap))
    133 		return;
    134 	dp = getino(ino);
    135 	if ((mode = (DIP(dp, mode) & IFMT)) == 0)
    136 		return;
    137 	/*
    138 	 * Put all dirs in dumpdirmap, inodes that are to be dumped in the
    139 	 * used map. All inode but dirs who have the nodump attribute go
    140 	 * to the usedinomap.
    141 	 */
    142 	SETINO(ino, usedinomap);
    143 	if (mode == IFDIR)
    144 		SETINO(ino, dumpdirmap);
    145 	if (WANTTODUMP(dp)) {
    146 		SETINO(ino, dumpinomap);
    147 		if (mode != IFREG && mode != IFDIR && mode != IFLNK)
    148 			*tape_size += 1;
    149 		else
    150 			*tape_size += blockest(dp);
    151 		return;
    152 	}
    153 	if (mode == IFDIR) {
    154 #ifdef UF_NODUMP
    155 		if (!nonodump && (DIP(dp, flags) & UF_NODUMP))
    156 			CLRINO(ino, usedinomap);
    157 #endif
    158 		*dirskipped = 1;
    159 	}
    160 }
    161 
    162 /*
    163  * Dump pass 1.
    164  *
    165  * Walk the inode list for a file system to find all allocated inodes
    166  * that have been modified since the previous dump time. Also, find all
    167  * the directories in the file system.
    168  * disk may be NULL if dirv is NULL.
    169  */
    170 int
    171 mapfiles(ino_t maxino, u_int64_t *tape_size, char *diskname, char * const *dirv)
    172 {
    173 	int anydirskipped = 0;
    174 
    175 	if (dirv != NULL) {
    176 		char	 curdir[MAXPATHLEN];
    177 		FTS	*dirh;
    178 		FTSENT	*entry;
    179 		int	 d;
    180 
    181 		if (getcwd(curdir, sizeof(curdir)) == NULL) {
    182 			msg("Can't determine cwd: %s\n", strerror(errno));
    183 			dumpabort(0);
    184 		}
    185 		if ((dirh = fts_open(dirv, FTS_PHYSICAL|FTS_SEEDOT|FTS_XDEV,
    186 		    		    NULL)) == NULL) {
    187 			msg("fts_open failed: %s\n", strerror(errno));
    188 			dumpabort(0);
    189 		}
    190 		while ((entry = fts_read(dirh)) != NULL) {
    191 			switch (entry->fts_info) {
    192 			case FTS_DNR:		/* an error */
    193 			case FTS_ERR:
    194 			case FTS_NS:
    195 				msg("Can't fts_read %s: %s\n", entry->fts_path,
    196 				    strerror(errno));
    197 			case FTS_DP:		/* already seen dir */
    198 				continue;
    199 			}
    200 			mapfileino(entry->fts_statp->st_ino, tape_size,
    201 			    &anydirskipped);
    202 		}
    203 		(void)fts_close(dirh);
    204 
    205 		/*
    206 		 * Add any parent directories
    207 		 */
    208 		for (d = 0 ; dirv[d] != NULL ; d++) {
    209 			char path[MAXPATHLEN];
    210 
    211 			if (dirv[d][0] != '/')
    212 				(void)snprintf(path, sizeof(path), "%s/%s",
    213 				    curdir, dirv[d]);
    214 			else
    215 				(void)snprintf(path, sizeof(path), "%s",
    216 				    dirv[d]);
    217 			while (strcmp(path, diskname) != 0) {
    218 				char *p;
    219 				struct stat sb;
    220 
    221 				if (*path == '\0')
    222 					break;
    223 				if ((p = strrchr(path, '/')) == NULL)
    224 					break;
    225 				if (p == path)
    226 					break;
    227 				*p = '\0';
    228 				if (stat(path, &sb) == -1) {
    229 					msg("Can't stat %s: %s\n", path,
    230 					    strerror(errno));
    231 					break;
    232 				}
    233 				mapfileino(sb.st_ino, tape_size, &anydirskipped);
    234 			}
    235 		}
    236 
    237 		/*
    238 		 * Ensure that the root inode actually appears in the
    239 		 * file list for a subdir
    240 		 */
    241 		mapfileino(ROOTINO, tape_size, &anydirskipped);
    242 	} else {
    243 		fs_mapinodes(maxino, tape_size, &anydirskipped);
    244 	}
    245 	/*
    246 	 * Restore gets very upset if the root is not dumped,
    247 	 * so ensure that it always is dumped.
    248 	 */
    249 	SETINO(ROOTINO, dumpinomap);
    250 	return (anydirskipped);
    251 }
    252 
    253 /*
    254  * Dump pass 2.
    255  *
    256  * Scan each directory on the file system to see if it has any modified
    257  * files in it. If it does, and has not already been added to the dump
    258  * list (because it was itself modified), then add it. If a directory
    259  * has not been modified itself, contains no modified files and has no
    260  * subdirectories, then it can be deleted from the dump list and from
    261  * the list of directories. By deleting it from the list of directories,
    262  * its parent may now qualify for the same treatment on this or a later
    263  * pass using this algorithm.
    264  */
    265 int
    266 mapdirs(ino_t maxino, u_int64_t *tape_size)
    267 {
    268 	union dinode *dp, di;
    269 	int i, isdir, nodump;
    270 	char *map;
    271 	ino_t ino;
    272 	off_t filesize;
    273 	int ret, change = 0;
    274 	daddr_t blk;
    275 
    276 	isdir = 0;		/* XXX just to get gcc to shut up */
    277 	for (map = dumpdirmap, ino = 1; ino < maxino; ino++) {
    278 		if (((ino - 1) % NBBY) == 0)	/* map is offset by 1 */
    279 			isdir = *map++;
    280 		else
    281 			isdir >>= 1;
    282 		/*
    283 		 * If dir has been removed from the used map, it's either
    284 		 * because it had the nodump flag, or it inherited it from
    285 		 * its parent. A directory can't be in dumpinomap if
    286 		 * not in usedinomap, but we have to go throuh it anyway
    287 		 * to propagate the nodump attribute.
    288 		 */
    289 		nodump = (TSTINO(ino, usedinomap) == 0);
    290 		if ((isdir & 1) == 0 ||
    291 		    (TSTINO(ino, dumpinomap) && nodump == 0))
    292 			continue;
    293 
    294 		dp = getino(ino);
    295 		/*
    296 		 * inode buf may be changed in searchdir().
    297 		 */
    298 		if (is_ufs2)
    299 			di.dp2 = dp->dp2;
    300 		else
    301 			di.dp1 = dp->dp1;
    302 		filesize = DIP(&di, size);
    303 		for (ret = 0, i = 0; filesize > 0 && i < NDADDR; i++) {
    304 			if (is_ufs2)
    305 				blk = iswap64(di.dp2.di_db[i]);
    306 			else
    307 				blk = iswap32(di.dp1.di_db[i]);
    308 			if (blk != 0)
    309 				ret |= searchdir(ino, blk,
    310 					(long)ufs_dblksize(ufsib, &di, i),
    311 					filesize, tape_size, nodump);
    312 			if (ret & HASDUMPEDFILE)
    313 				filesize = 0;
    314 			else
    315 				filesize -= ufsib->ufs_bsize;
    316 		}
    317 		for (i = 0; filesize > 0 && i < NIADDR; i++) {
    318 			if (is_ufs2)
    319 				blk = iswap64(di.dp2.di_ib[i]);
    320 			else
    321 				blk = iswap32(di.dp1.di_ib[i]);
    322 			if (blk == 0)
    323 				continue;
    324 			ret |= dirindir(ino, blk, i, &filesize,
    325 			    tape_size, nodump);
    326 		}
    327 		if (ret & HASDUMPEDFILE) {
    328 			SETINO(ino, dumpinomap);
    329 			*tape_size += blockest(&di);
    330 			change = 1;
    331 			continue;
    332 		}
    333 		if (nodump) {
    334 			if (ret & HASSUBDIRS)
    335 				change = 1; /* subdirs have inherited nodump */
    336 			CLRINO(ino, dumpdirmap);
    337 		} else if ((ret & HASSUBDIRS) == 0) {
    338 			if (!TSTINO(ino, dumpinomap)) {
    339 				CLRINO(ino, dumpdirmap);
    340 				change = 1;
    341 			}
    342 		}
    343 	}
    344 	return (change);
    345 }
    346 
    347 /*
    348  * Read indirect blocks, and pass the data blocks to be searched
    349  * as directories. Quit as soon as any entry is found that will
    350  * require the directory to be dumped.
    351  */
    352 static int
    353 dirindir(ino_t ino, daddr_t blkno, int ind_level, off_t *filesize,
    354 	u_int64_t *tape_size, int nodump)
    355 {
    356 	int ret = 0;
    357 	int i;
    358 	union {
    359 		int64_t i64[MAXBSIZE / sizeof (int64_t)];
    360 		int32_t i32[MAXBSIZE / sizeof (int32_t)];
    361 	} idblk;
    362 
    363 	bread(fsatoda(ufsib, blkno), (char *)&idblk, (int)ufsib->ufs_bsize);
    364 	if (ind_level <= 0) {
    365 		for (i = 0; *filesize > 0 && i < ufsib->ufs_nindir; i++) {
    366 			if (is_ufs2)
    367 				blkno = iswap64(idblk.i64[i]);
    368 			else
    369 				blkno = iswap32(idblk.i32[i]);
    370 			if (blkno != 0)
    371 				ret |= searchdir(ino, blkno,
    372 				    ufsib->ufs_bsize, *filesize,
    373 				    tape_size, nodump);
    374 			if (ret & HASDUMPEDFILE)
    375 				*filesize = 0;
    376 			else
    377 				*filesize -= ufsib->ufs_bsize;
    378 		}
    379 		return (ret);
    380 	}
    381 	ind_level--;
    382 	for (i = 0; *filesize > 0 && i < ufsib->ufs_nindir; i++) {
    383 		if (is_ufs2)
    384 			blkno = iswap64(idblk.i64[i]);
    385 		else
    386 			blkno = iswap32(idblk.i32[i]);
    387 		if (blkno != 0)
    388 			ret |= dirindir(ino, blkno, ind_level, filesize,
    389 			    tape_size, nodump);
    390 	}
    391 	return (ret);
    392 }
    393 
    394 /*
    395  * Scan a disk block containing directory information looking to see if
    396  * any of the entries are on the dump list and to see if the directory
    397  * contains any subdirectories.
    398  */
    399 static int
    400 searchdir(ino_t dino, daddr_t blkno, long size, off_t filesize,
    401 	u_int64_t *tape_size, int nodump)
    402 {
    403 	struct direct *dp;
    404 	union dinode *ip;
    405 	long loc, ret = 0;
    406 	char *dblk;
    407 	ino_t ino;
    408 
    409 	dblk = malloc(size);
    410 	if (dblk == NULL)
    411 		quit("searchdir: cannot allocate directory memory.\n");
    412 	bread(fsatoda(ufsib, blkno), dblk, (int)size);
    413 	if (filesize < size)
    414 		size = filesize;
    415 	for (loc = 0; loc < size; ) {
    416 		dp = (struct direct *)(dblk + loc);
    417 		if (dp->d_reclen == 0) {
    418 			msg("corrupted directory, inumber %d\n", dino);
    419 			break;
    420 		}
    421 		loc += iswap16(dp->d_reclen);
    422 		if (dp->d_ino == 0)
    423 			continue;
    424 		if (dp->d_name[0] == '.') {
    425 			if (dp->d_name[1] == '\0')
    426 				continue;
    427 			if (dp->d_name[1] == '.' && dp->d_name[2] == '\0')
    428 				continue;
    429 		}
    430 		ino = iswap32(dp->d_ino);
    431 		if (nodump) {
    432 			ip = getino(ino);
    433 			if (TSTINO(ino, dumpinomap)) {
    434 				CLRINO(ino, dumpinomap);
    435 				*tape_size -= blockest(ip);
    436 			}
    437 			/*
    438 			 * Add back to dumpdirmap and remove from usedinomap
    439 			 * to propagate nodump.
    440 			 */
    441 			if ((DIP(ip, mode) & IFMT) == IFDIR) {
    442 				SETINO(ino, dumpdirmap);
    443 				CLRINO(ino, usedinomap);
    444 				ret |= HASSUBDIRS;
    445 			}
    446 		} else {
    447 			if (TSTINO(ino, dumpinomap)) {
    448 				ret |= HASDUMPEDFILE;
    449 				if (ret & HASSUBDIRS)
    450 					break;
    451 			}
    452 			if (TSTINO(ino, dumpdirmap)) {
    453 				ret |= HASSUBDIRS;
    454 				if (ret & HASDUMPEDFILE)
    455 					break;
    456 			}
    457 		}
    458 	}
    459 	free(dblk);
    460 	return (ret);
    461 }
    462 
    463 /*
    464  * Dump passes 3 and 4.
    465  *
    466  * Dump the contents of an inode to tape.
    467  */
    468 void
    469 dumpino(union dinode *dp, ino_t ino)
    470 {
    471 	int ind_level, cnt;
    472 	off_t size;
    473 	char buf[TP_BSIZE];
    474 	daddr_t blk;
    475 	void *shortlink;
    476 
    477 	if (newtape) {
    478 		newtape = 0;
    479 		dumpmap(dumpinomap, TS_BITS, ino);
    480 	}
    481 	CLRINO(ino, dumpinomap);
    482 	if (!is_ufs2) {
    483 		if (needswap)
    484 			ffs_dinode1_swap(&dp->dp1, &spcl.c_dinode);
    485 		else
    486 			spcl.c_dinode = dp->dp1;
    487 	} else {
    488 		if (needswap)
    489 			ffs_dinode2_swap(&dp->dp2, &dp->dp2);
    490 		spcl.c_mode = dp->dp2.di_mode;
    491 		spcl.c_size = dp->dp2.di_size;
    492 		spcl.c_atime = dp->dp2.di_atime;
    493 		spcl.c_atimensec = dp->dp2.di_atimensec;
    494 		spcl.c_mtime = dp->dp2.di_mtime;
    495 		spcl.c_mtimensec = dp->dp2.di_mtimensec;
    496 		spcl.c_birthtime = dp->dp2.di_birthtime;
    497 		spcl.c_birthtimensec = dp->dp2.di_birthnsec;
    498 		spcl.c_rdev = dp->dp2.di_rdev;
    499 		spcl.c_file_flags = dp->dp2.di_flags;
    500 		spcl.c_uid = dp->dp2.di_uid;
    501 		spcl.c_gid = dp->dp2.di_gid;
    502 	}
    503 	spcl.c_type = iswap32(TS_INODE);
    504 	spcl.c_count = 0;
    505 	switch (DIP(dp, mode) & IFMT) {
    506 
    507 	case 0:
    508 		/*
    509 		 * Freed inode.
    510 		 */
    511 		return;
    512 
    513 	case IFLNK:
    514 		/*
    515 		 * Check for short symbolic link.
    516 		 */
    517 		if (DIP(dp, size) > 0 &&
    518 #ifdef FS_44INODEFMT
    519 		    (DIP(dp, size) < ufsib->ufs_maxsymlinklen ||
    520 		     (ufsib->ufs_maxsymlinklen == 0 && DIP(dp, blocks) == 0))
    521 #else
    522 		    DIP(dp, blocks) == 0
    523 #endif
    524 			) {
    525 			spcl.c_addr[0] = 1;
    526 			spcl.c_count = iswap32(1);
    527 			writeheader(ino);
    528 			if (is_ufs2)
    529 				shortlink = dp->dp2.di_db;
    530 			else
    531 				shortlink = dp->dp1.di_db;
    532 			memmove(buf, shortlink, DIP(dp, size));
    533 			buf[DIP(dp, size)] = '\0';
    534 			writerec(buf, 0);
    535 			return;
    536 		}
    537 		/* fall through */
    538 
    539 	case IFDIR:
    540 	case IFREG:
    541 		if (DIP(dp, size) > 0)
    542 			break;
    543 		/* fall through */
    544 
    545 	case IFIFO:
    546 	case IFSOCK:
    547 	case IFCHR:
    548 	case IFBLK:
    549 		writeheader(ino);
    550 		return;
    551 
    552 	default:
    553 		msg("Warning: undefined file type 0%o\n", DIP(dp, mode) & IFMT);
    554 		return;
    555 	}
    556 	if (DIP(dp, size) > NDADDR * ufsib->ufs_bsize)
    557 		cnt = NDADDR * ufsib->ufs_frag;
    558 	else
    559 		cnt = howmany(DIP(dp, size), ufsib->ufs_fsize);
    560 	if (is_ufs2)
    561 		blksout64(&dp->dp2.di_db[0], cnt, ino);
    562 	else
    563 		blksout32(&dp->dp1.di_db[0], cnt, ino);
    564 
    565 	if ((size = DIP(dp, size) - NDADDR * ufsib->ufs_bsize) <= 0)
    566 		return;
    567 	for (ind_level = 0; ind_level < NIADDR; ind_level++) {
    568 		if (is_ufs2)
    569 			blk = iswap64(dp->dp2.di_ib[ind_level]);
    570 		else
    571 			blk = iswap32(dp->dp1.di_ib[ind_level]);
    572 		dmpindir(ino, blk, 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(ino_t ino, daddr_t blk, int ind_level, off_t *size)
    583 {
    584 	int i, cnt;
    585 	union {
    586 		int32_t i32[MAXBSIZE / sizeof (int32_t)];
    587 		int64_t i64[MAXBSIZE / sizeof (int64_t)];
    588 	} idblk;
    589 	daddr_t iblk;
    590 
    591 
    592 	if (blk != 0)
    593 		bread(fsatoda(ufsib, blk), (char *)&idblk,
    594 			(int) ufsib->ufs_bsize);
    595 	else
    596 		memset(&idblk, 0, (int)ufsib->ufs_bsize);
    597 	if (ind_level <= 0) {
    598 		if (*size < ufsib->ufs_nindir * ufsib->ufs_bsize)
    599 			cnt = howmany(*size, ufsib->ufs_fsize);
    600 		else
    601 			cnt = ufsib->ufs_nindir * ufsib->ufs_frag;
    602 		*size -= ufsib->ufs_nindir * ufsib->ufs_bsize;
    603 		if (is_ufs2)
    604 			blksout64(&idblk.i64[0], cnt, ino);
    605 		else
    606 			blksout32(&idblk.i32[0], cnt, ino);
    607 		return;
    608 	}
    609 	ind_level--;
    610 	for (i = 0; i < ufsib->ufs_nindir; i++) {
    611 		if (is_ufs2)
    612 			iblk = iswap64(idblk.i64[i]);
    613 		else
    614 			iblk = iswap32(idblk.i32[i]);
    615 		dmpindir(ino, iblk, ind_level, size);
    616 		if (*size <= 0)
    617 			return;
    618 	}
    619 }
    620 
    621 /*
    622  * Collect up the data into tape record sized buffers and output them.
    623  */
    624 void
    625 blksout32(int32_t *blkp, int frags, ino_t ino)
    626 {
    627 	int32_t *bp;
    628 	int i, j, count, blks, tbperdb;
    629 
    630 	blks = howmany(frags * ufsib->ufs_fsize, TP_BSIZE);
    631 	tbperdb = ufsib->ufs_bsize >> tp_bshift;
    632 	for (i = 0; i < blks; i += TP_NINDIR) {
    633 		if (i + TP_NINDIR > blks)
    634 			count = blks;
    635 		else
    636 			count = i + TP_NINDIR;
    637 		for (j = i; j < count; j++)
    638 			if (blkp[j / tbperdb] != 0)
    639 				spcl.c_addr[j - i] = 1;
    640 			else
    641 				spcl.c_addr[j - i] = 0;
    642 		spcl.c_count = iswap32(count - i);
    643 		writeheader(ino);
    644 		bp = &blkp[i / tbperdb];
    645 		for (j = i; j < count; j += tbperdb, bp++)
    646 			if (*bp != 0) {
    647 				if (j + tbperdb <= count)
    648 					dumpblock(iswap32(*bp), (int)ufsib->ufs_bsize);
    649 				else
    650 					dumpblock(iswap32(*bp), (count - j) * TP_BSIZE);
    651 			}
    652 		spcl.c_type = iswap32(TS_ADDR);
    653 	}
    654 }
    655 
    656 void
    657 blksout64(int64_t *blkp, int frags, ino_t ino)
    658 {
    659 	int64_t *bp;
    660 	int i, j, count, blks, tbperdb;
    661 
    662 	blks = howmany(frags * ufsib->ufs_fsize, TP_BSIZE);
    663 	tbperdb = ufsib->ufs_bsize >> tp_bshift;
    664 	for (i = 0; i < blks; i += TP_NINDIR) {
    665 		if (i + TP_NINDIR > blks)
    666 			count = blks;
    667 		else
    668 			count = i + TP_NINDIR;
    669 		for (j = i; j < count; j++)
    670 			if (blkp[j / tbperdb] != 0)
    671 				spcl.c_addr[j - i] = 1;
    672 			else
    673 				spcl.c_addr[j - i] = 0;
    674 		spcl.c_count = iswap32(count - i);
    675 		writeheader(ino);
    676 		bp = &blkp[i / tbperdb];
    677 		for (j = i; j < count; j += tbperdb, bp++)
    678 			if (*bp != 0) {
    679 				if (j + tbperdb <= count)
    680 					dumpblock(iswap64(*bp), (int)ufsib->ufs_bsize);
    681 				else
    682 					dumpblock(iswap64(*bp), (count - j) * TP_BSIZE);
    683 			}
    684 		spcl.c_type = iswap32(TS_ADDR);
    685 	}
    686 }
    687 
    688 /*
    689  * Dump a map to the tape.
    690  */
    691 void
    692 dumpmap(char *map, int type, ino_t ino)
    693 {
    694 	int i;
    695 	char *cp;
    696 
    697 	spcl.c_type = iswap32(type);
    698 	spcl.c_count = iswap32(howmany(mapsize * sizeof(char), TP_BSIZE));
    699 	writeheader(ino);
    700 	for (i = 0, cp = map; i < iswap32(spcl.c_count); i++, cp += TP_BSIZE)
    701 		writerec(cp, 0);
    702 }
    703 
    704 /*
    705  * Write a header record to the dump tape.
    706  */
    707 void
    708 writeheader(ino_t ino)
    709 {
    710 	int32_t sum, cnt, *lp;
    711 
    712 	spcl.c_inumber = iswap32(ino);
    713 	if (is_ufs2)
    714 		spcl.c_magic = iswap32(FS_UFS2_MAGIC);
    715 	else {
    716 		spcl.c_magic = iswap32(NFS_MAGIC);
    717 		spcl.c_old_date = iswap32(iswap64(spcl.c_date));
    718 		spcl.c_old_ddate = iswap32(iswap64(spcl.c_ddate));
    719 		spcl.c_old_tapea = iswap32(iswap64(spcl.c_tapea));
    720 		spcl.c_old_firstrec = iswap32(iswap64(spcl.c_firstrec));
    721 	}
    722 	spcl.c_checksum = 0;
    723 	lp = (int32_t *)&spcl;
    724 	sum = 0;
    725 	cnt = sizeof(union u_spcl) / (4 * sizeof(int32_t));
    726 	while (--cnt >= 0) {
    727 		sum += iswap32(*lp++);
    728 		sum += iswap32(*lp++);
    729 		sum += iswap32(*lp++);
    730 		sum += iswap32(*lp++);
    731 	}
    732 	spcl.c_checksum = iswap32(CHECKSUM - sum);
    733 	writerec((char *)&spcl, 1);
    734 }
    735