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