Home | History | Annotate | Line # | Download | only in dump
traverse.c revision 1.46
      1 /*	$NetBSD: traverse.c,v 1.46 2006/04/21 15:00:49 skrll 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.46 2006/04/21 15:00:49 skrll 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 %llu\n",
    422 			    (unsigned long long)dino);
    423 			break;
    424 		}
    425 		loc += iswap16(dp->d_reclen);
    426 		if (dp->d_ino == 0)
    427 			continue;
    428 		if (dp->d_name[0] == '.') {
    429 			if (dp->d_name[1] == '\0')
    430 				continue;
    431 			if (dp->d_name[1] == '.' && dp->d_name[2] == '\0')
    432 				continue;
    433 		}
    434 		ino = iswap32(dp->d_ino);
    435 		if (nodump) {
    436 			ip = getino(ino);
    437 			if (TSTINO(ino, dumpinomap)) {
    438 				CLRINO(ino, dumpinomap);
    439 				*tape_size -= blockest(ip);
    440 			}
    441 			/*
    442 			 * Add back to dumpdirmap and remove from usedinomap
    443 			 * to propagate nodump.
    444 			 */
    445 			if ((DIP(ip, mode) & IFMT) == IFDIR) {
    446 				SETINO(ino, dumpdirmap);
    447 				CLRINO(ino, usedinomap);
    448 				ret |= HASSUBDIRS;
    449 			}
    450 		} else {
    451 			if (TSTINO(ino, dumpinomap)) {
    452 				ret |= HASDUMPEDFILE;
    453 				if (ret & HASSUBDIRS)
    454 					break;
    455 			}
    456 			if (TSTINO(ino, dumpdirmap)) {
    457 				ret |= HASSUBDIRS;
    458 				if (ret & HASDUMPEDFILE)
    459 					break;
    460 			}
    461 		}
    462 	}
    463 	free(dblk);
    464 	return (ret);
    465 }
    466 
    467 /*
    468  * Dump passes 3 and 4.
    469  *
    470  * Dump the contents of an inode to tape.
    471  */
    472 void
    473 dumpino(union dinode *dp, ino_t ino)
    474 {
    475 	int ind_level, cnt;
    476 	off_t size;
    477 	char buf[TP_BSIZE];
    478 	daddr_t blk;
    479 	void *shortlink;
    480 
    481 	if (newtape) {
    482 		newtape = 0;
    483 		dumpmap(dumpinomap, TS_BITS, ino);
    484 	}
    485 	CLRINO(ino, dumpinomap);
    486 	/*
    487 	 * Zero out the size of a snapshot so that it will be dumped
    488 	 * as a zero length file.
    489 	 */
    490 	if (DIP(dp, flags) & SF_SNAPSHOT) {
    491 		DIP_SET(dp, size, 0);
    492 		DIP_SET(dp, flags, DIP(dp, flags) & ~SF_SNAPSHOT);
    493 	}
    494 	if (!is_ufs2) {
    495 		if (needswap)
    496 			ffs_dinode1_swap(&dp->dp1, &spcl.c_dinode);
    497 		else
    498 			spcl.c_dinode = dp->dp1;
    499 	} else {
    500 		if (needswap)
    501 			ffs_dinode2_swap(&dp->dp2, &dp->dp2);
    502 		spcl.c_mode = dp->dp2.di_mode;
    503 		spcl.c_size = dp->dp2.di_size;
    504 		spcl.c_atime = dp->dp2.di_atime;
    505 		spcl.c_atimensec = dp->dp2.di_atimensec;
    506 		spcl.c_mtime = dp->dp2.di_mtime;
    507 		spcl.c_mtimensec = dp->dp2.di_mtimensec;
    508 		spcl.c_birthtime = dp->dp2.di_birthtime;
    509 		spcl.c_birthtimensec = dp->dp2.di_birthnsec;
    510 		spcl.c_rdev = dp->dp2.di_rdev;
    511 		spcl.c_file_flags = dp->dp2.di_flags;
    512 		spcl.c_uid = dp->dp2.di_uid;
    513 		spcl.c_gid = dp->dp2.di_gid;
    514 	}
    515 	spcl.c_type = iswap32(TS_INODE);
    516 	spcl.c_count = 0;
    517 	switch (DIP(dp, mode) & IFMT) {
    518 
    519 	case 0:
    520 		/*
    521 		 * Freed inode.
    522 		 */
    523 		return;
    524 
    525 	case IFLNK:
    526 		/*
    527 		 * Check for short symbolic link.
    528 		 */
    529 		if (DIP(dp, size) > 0 &&
    530 #ifdef FS_44INODEFMT
    531 		    (DIP(dp, size) < ufsib->ufs_maxsymlinklen ||
    532 		     (ufsib->ufs_maxsymlinklen == 0 && DIP(dp, blocks) == 0))
    533 #else
    534 		    DIP(dp, blocks) == 0
    535 #endif
    536 			) {
    537 			spcl.c_addr[0] = 1;
    538 			spcl.c_count = iswap32(1);
    539 			writeheader(ino);
    540 			if (is_ufs2)
    541 				shortlink = dp->dp2.di_db;
    542 			else
    543 				shortlink = dp->dp1.di_db;
    544 			memmove(buf, shortlink, DIP(dp, size));
    545 			buf[DIP(dp, size)] = '\0';
    546 			writerec(buf, 0);
    547 			return;
    548 		}
    549 		/* fall through */
    550 
    551 	case IFDIR:
    552 	case IFREG:
    553 		if (DIP(dp, size) > 0)
    554 			break;
    555 		/* fall through */
    556 
    557 	case IFIFO:
    558 	case IFSOCK:
    559 	case IFCHR:
    560 	case IFBLK:
    561 		writeheader(ino);
    562 		return;
    563 
    564 	default:
    565 		msg("Warning: undefined file type 0%o\n", DIP(dp, mode) & IFMT);
    566 		return;
    567 	}
    568 	if (DIP(dp, size) > NDADDR * ufsib->ufs_bsize)
    569 		cnt = NDADDR * ufsib->ufs_frag;
    570 	else
    571 		cnt = howmany(DIP(dp, size), ufsib->ufs_fsize);
    572 	if (is_ufs2)
    573 		blksout64(&dp->dp2.di_db[0], cnt, ino);
    574 	else
    575 		blksout32(&dp->dp1.di_db[0], cnt, ino);
    576 
    577 	if ((size = DIP(dp, size) - NDADDR * ufsib->ufs_bsize) <= 0)
    578 		return;
    579 	for (ind_level = 0; ind_level < NIADDR; ind_level++) {
    580 		if (is_ufs2)
    581 			blk = iswap64(dp->dp2.di_ib[ind_level]);
    582 		else
    583 			blk = iswap32(dp->dp1.di_ib[ind_level]);
    584 		dmpindir(ino, blk, ind_level, &size);
    585 		if (size <= 0)
    586 			return;
    587 	}
    588 }
    589 
    590 /*
    591  * Read indirect blocks, and pass the data blocks to be dumped.
    592  */
    593 static void
    594 dmpindir(ino_t ino, daddr_t blk, int ind_level, off_t *size)
    595 {
    596 	int i, cnt;
    597 	union {
    598 		int32_t i32[MAXBSIZE / sizeof (int32_t)];
    599 		int64_t i64[MAXBSIZE / sizeof (int64_t)];
    600 	} idblk;
    601 	daddr_t iblk;
    602 
    603 
    604 	if (blk != 0)
    605 		bread(fsatoda(ufsib, blk), (char *)&idblk,
    606 			(int) ufsib->ufs_bsize);
    607 	else
    608 		memset(&idblk, 0, (int)ufsib->ufs_bsize);
    609 	if (ind_level <= 0) {
    610 		if (*size < ufsib->ufs_nindir * ufsib->ufs_bsize)
    611 			cnt = howmany(*size, ufsib->ufs_fsize);
    612 		else
    613 			cnt = ufsib->ufs_nindir * ufsib->ufs_frag;
    614 		*size -= ufsib->ufs_nindir * ufsib->ufs_bsize;
    615 		if (is_ufs2)
    616 			blksout64(&idblk.i64[0], cnt, ino);
    617 		else
    618 			blksout32(&idblk.i32[0], cnt, ino);
    619 		return;
    620 	}
    621 	ind_level--;
    622 	for (i = 0; i < ufsib->ufs_nindir; i++) {
    623 		if (is_ufs2)
    624 			iblk = iswap64(idblk.i64[i]);
    625 		else
    626 			iblk = iswap32(idblk.i32[i]);
    627 		dmpindir(ino, iblk, ind_level, size);
    628 		if (*size <= 0)
    629 			return;
    630 	}
    631 }
    632 
    633 /*
    634  * Collect up the data into tape record sized buffers and output them.
    635  */
    636 void
    637 blksout32(int32_t *blkp, int frags, ino_t ino)
    638 {
    639 	int32_t *bp;
    640 	int i, j, count, blks, tbperdb;
    641 
    642 	blks = howmany(frags * ufsib->ufs_fsize, TP_BSIZE);
    643 	tbperdb = ufsib->ufs_bsize >> tp_bshift;
    644 	for (i = 0; i < blks; i += TP_NINDIR) {
    645 		if (i + TP_NINDIR > blks)
    646 			count = blks;
    647 		else
    648 			count = i + TP_NINDIR;
    649 		for (j = i; j < count; j++)
    650 			if (blkp[j / tbperdb] != 0)
    651 				spcl.c_addr[j - i] = 1;
    652 			else
    653 				spcl.c_addr[j - i] = 0;
    654 		spcl.c_count = iswap32(count - i);
    655 		writeheader(ino);
    656 		bp = &blkp[i / tbperdb];
    657 		for (j = i; j < count; j += tbperdb, bp++)
    658 			if (*bp != 0) {
    659 				if (j + tbperdb <= count)
    660 					dumpblock(iswap32(*bp), (int)ufsib->ufs_bsize);
    661 				else
    662 					dumpblock(iswap32(*bp), (count - j) * TP_BSIZE);
    663 			}
    664 		spcl.c_type = iswap32(TS_ADDR);
    665 	}
    666 }
    667 
    668 void
    669 blksout64(int64_t *blkp, int frags, ino_t ino)
    670 {
    671 	int64_t *bp;
    672 	int i, j, count, blks, tbperdb;
    673 
    674 	blks = howmany(frags * ufsib->ufs_fsize, TP_BSIZE);
    675 	tbperdb = ufsib->ufs_bsize >> tp_bshift;
    676 	for (i = 0; i < blks; i += TP_NINDIR) {
    677 		if (i + TP_NINDIR > blks)
    678 			count = blks;
    679 		else
    680 			count = i + TP_NINDIR;
    681 		for (j = i; j < count; j++)
    682 			if (blkp[j / tbperdb] != 0)
    683 				spcl.c_addr[j - i] = 1;
    684 			else
    685 				spcl.c_addr[j - i] = 0;
    686 		spcl.c_count = iswap32(count - i);
    687 		writeheader(ino);
    688 		bp = &blkp[i / tbperdb];
    689 		for (j = i; j < count; j += tbperdb, bp++)
    690 			if (*bp != 0) {
    691 				if (j + tbperdb <= count)
    692 					dumpblock(iswap64(*bp), (int)ufsib->ufs_bsize);
    693 				else
    694 					dumpblock(iswap64(*bp), (count - j) * TP_BSIZE);
    695 			}
    696 		spcl.c_type = iswap32(TS_ADDR);
    697 	}
    698 }
    699 
    700 /*
    701  * Dump a map to the tape.
    702  */
    703 void
    704 dumpmap(char *map, int type, ino_t ino)
    705 {
    706 	int i;
    707 	char *cp;
    708 
    709 	spcl.c_type = iswap32(type);
    710 	spcl.c_count = iswap32(howmany(mapsize * sizeof(char), TP_BSIZE));
    711 	writeheader(ino);
    712 	for (i = 0, cp = map; i < iswap32(spcl.c_count); i++, cp += TP_BSIZE)
    713 		writerec(cp, 0);
    714 }
    715 
    716 /*
    717  * Write a header record to the dump tape.
    718  */
    719 void
    720 writeheader(ino_t ino)
    721 {
    722 	int32_t sum, cnt, *lp;
    723 
    724 	spcl.c_inumber = iswap32(ino);
    725 	if (is_ufs2)
    726 		spcl.c_magic = iswap32(FS_UFS2_MAGIC);
    727 	else {
    728 		spcl.c_magic = iswap32(NFS_MAGIC);
    729 		spcl.c_old_date = iswap32(iswap64(spcl.c_date));
    730 		spcl.c_old_ddate = iswap32(iswap64(spcl.c_ddate));
    731 		spcl.c_old_tapea = iswap32(iswap64(spcl.c_tapea));
    732 		spcl.c_old_firstrec = iswap32(iswap64(spcl.c_firstrec));
    733 	}
    734 	spcl.c_checksum = 0;
    735 	lp = (int32_t *)&spcl;
    736 	sum = 0;
    737 	cnt = sizeof(union u_spcl) / (4 * sizeof(int32_t));
    738 	while (--cnt >= 0) {
    739 		sum += iswap32(*lp++);
    740 		sum += iswap32(*lp++);
    741 		sum += iswap32(*lp++);
    742 		sum += iswap32(*lp++);
    743 	}
    744 	spcl.c_checksum = iswap32(CHECKSUM - sum);
    745 	writerec((char *)&spcl, 1);
    746 }
    747