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