xref: /freebsd/sbin/dump/traverse.c (revision 5956d97f4b3204318ceb6aa9c77bd0bc6ea87a41)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
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 #ifndef lint
33 #if 0
34 static char sccsid[] = "@(#)traverse.c	8.7 (Berkeley) 6/15/95";
35 #endif
36 static const char rcsid[] =
37   "$FreeBSD$";
38 #endif /* not lint */
39 
40 #include <sys/param.h>
41 #include <sys/stat.h>
42 
43 #include <ufs/ufs/dir.h>
44 #include <ufs/ufs/dinode.h>
45 #include <ufs/ffs/fs.h>
46 
47 #include <protocols/dumprestore.h>
48 
49 #include <assert.h>
50 #include <ctype.h>
51 #include <errno.h>
52 #include <inttypes.h>
53 #include <limits.h>
54 #include <stdio.h>
55 #include <stdlib.h>
56 #include <string.h>
57 #include <timeconv.h>
58 #include <unistd.h>
59 
60 #include "dump.h"
61 
62 union dinode {
63 	struct ufs1_dinode dp1;
64 	struct ufs2_dinode dp2;
65 };
66 #define	DIP(dp, field) \
67 	((sblock->fs_magic == FS_UFS1_MAGIC) ? \
68 	(dp)->dp1.field : (dp)->dp2.field)
69 #define DIP_SET(dp, field, val) do {\
70 	if (sblock->fs_magic == FS_UFS1_MAGIC) \
71 		(dp)->dp1.field = (val); \
72 	else \
73 		(dp)->dp2.field = (val); \
74 	} while (0)
75 
76 #define	HASDUMPEDFILE	0x1
77 #define	HASSUBDIRS	0x2
78 
79 static	int dirindir(ino_t ino, ufs2_daddr_t blkno, int level, long *size,
80     long *tapesize, int nodump, ino_t maxino);
81 static	void dmpindir(union dinode *dp, ino_t ino, ufs2_daddr_t blk, int level,
82     off_t *size);
83 static	void ufs1_blksout(ufs1_daddr_t *blkp, int frags, ino_t ino);
84 static	void ufs2_blksout(union dinode *dp, ufs2_daddr_t *blkp, int frags,
85     ino_t ino, int last);
86 static	int appendextdata(union dinode *dp);
87 static	void writeextdata(union dinode *dp, ino_t ino, int added);
88 static	int searchdir(ino_t ino, ufs2_daddr_t blkno, long size, long filesize,
89     long *tapesize, int nodump, ino_t maxino);
90 static	long blockest(union dinode *dp);
91 
92 /*
93  * This is an estimation of the number of TP_BSIZE blocks in the file.
94  * It estimates the number of blocks in files with holes by assuming
95  * that all of the blocks accounted for by di_blocks are data blocks
96  * (when some of the blocks are usually used for indirect pointers);
97  * hence the estimate may be high.
98  */
99 static long
100 blockest(union dinode *dp)
101 {
102 	long blkest, sizeest;
103 
104 	/*
105 	 * dp->di_size is the size of the file in bytes.
106 	 * dp->di_blocks stores the number of sectors actually in the file.
107 	 * If there are more sectors than the size would indicate, this just
108 	 *	means that there are indirect blocks in the file or unused
109 	 *	sectors in the last file block; we can safely ignore these
110 	 *	(blkest = sizeest below).
111 	 * If the file is bigger than the number of sectors would indicate,
112 	 *	then the file has holes in it.	In this case we must use the
113 	 *	block count to estimate the number of data blocks used, but
114 	 *	we use the actual size for estimating the number of indirect
115 	 *	dump blocks (sizeest vs. blkest in the indirect block
116 	 *	calculation).
117 	 */
118 	if ((DIP(dp, di_flags) & SF_SNAPSHOT) != 0)
119 		return (1);
120 	blkest = howmany(dbtob(DIP(dp, di_blocks)), TP_BSIZE);
121 	sizeest = howmany(DIP(dp, di_size), TP_BSIZE);
122 	if (blkest > sizeest)
123 		blkest = sizeest;
124 	if (DIP(dp, di_size) > sblock->fs_bsize * UFS_NDADDR) {
125 		/* calculate the number of indirect blocks on the dump tape */
126 		blkest += howmany(sizeest -
127 		    UFS_NDADDR * sblock->fs_bsize / TP_BSIZE, TP_NINDIR);
128 	}
129 	return (blkest + 1);
130 }
131 
132 /* Auxiliary macro to pick up files changed since previous dump. */
133 #define	CHANGEDSINCE(dp, t) \
134 	(DIP(dp, di_mtime) >= (t) || DIP(dp, di_ctime) >= (t))
135 
136 /* The WANTTODUMP macro decides whether a file should be dumped. */
137 #ifdef UF_NODUMP
138 #define	WANTTODUMP(dp) \
139 	(CHANGEDSINCE(dp, spcl.c_ddate) && \
140 	 (nonodump || (DIP(dp, di_flags) & UF_NODUMP) != UF_NODUMP))
141 #else
142 #define	WANTTODUMP(dp) CHANGEDSINCE(dp, spcl.c_ddate)
143 #endif
144 
145 /*
146  * Dump pass 1.
147  *
148  * Walk the inode list for a file system to find all allocated inodes
149  * that have been modified since the previous dump time. Also, find all
150  * the directories in the file system.
151  */
152 int
153 mapfiles(ino_t maxino, long *tapesize)
154 {
155 	int i, cg, mode, inosused;
156 	int anydirskipped = 0;
157 	union dinode *dp;
158 	struct cg *cgp;
159 	ino_t ino;
160 	u_char *cp;
161 
162 	if ((cgp = malloc(sblock->fs_cgsize)) == NULL)
163 		quit("mapfiles: cannot allocate memory.\n");
164 	for (cg = 0; cg < sblock->fs_ncg; cg++) {
165 		ino = cg * sblock->fs_ipg;
166 		blkread(fsbtodb(sblock, cgtod(sblock, cg)), (char *)cgp,
167 		    sblock->fs_cgsize);
168 		if (sblock->fs_magic == FS_UFS2_MAGIC)
169 			inosused = cgp->cg_initediblk;
170 		else
171 			inosused = sblock->fs_ipg;
172 		/*
173 		 * If we are using soft updates, then we can trust the
174 		 * cylinder group inode allocation maps to tell us which
175 		 * inodes are allocated. We will scan the used inode map
176 		 * to find the inodes that are really in use, and then
177 		 * read only those inodes in from disk.
178 		 */
179 		if (sblock->fs_flags & FS_DOSOFTDEP) {
180 			if (!cg_chkmagic(cgp))
181 				quit("mapfiles: cg %d: bad magic number\n", cg);
182 			cp = &cg_inosused(cgp)[(inosused - 1) / CHAR_BIT];
183 			for ( ; inosused > 0; inosused -= CHAR_BIT, cp--) {
184 				if (*cp == 0)
185 					continue;
186 				for (i = 1 << (CHAR_BIT - 1); i > 0; i >>= 1) {
187 					if (*cp & i)
188 						break;
189 					inosused--;
190 				}
191 				break;
192 			}
193 			if (inosused <= 0)
194 				continue;
195 		}
196 		for (i = 0; i < inosused; i++, ino++) {
197 			if (ino < UFS_ROOTINO ||
198 			    (dp = getino(ino, &mode)) == NULL ||
199 			    (mode & IFMT) == 0)
200 				continue;
201 			if (ino >= maxino) {
202 				msg("Skipping inode %ju >= maxino %ju\n",
203 				    (uintmax_t)ino, (uintmax_t)maxino);
204 				continue;
205 			}
206 			/*
207 			 * Everything must go in usedinomap so that a check
208 			 * for "in dumpdirmap but not in usedinomap" to detect
209 			 * dirs with nodump set has a chance of succeeding
210 			 * (this is used in mapdirs()).
211 			 */
212 			SETINO(ino, usedinomap);
213 			if (mode == IFDIR)
214 				SETINO(ino, dumpdirmap);
215 			if (WANTTODUMP(dp)) {
216 				SETINO(ino, dumpinomap);
217 				if (mode != IFREG &&
218 				    mode != IFDIR &&
219 				    mode != IFLNK)
220 					*tapesize += 1;
221 				else
222 					*tapesize += blockest(dp);
223 				continue;
224 			}
225 			if (mode == IFDIR) {
226 				if (!nonodump &&
227 				    (DIP(dp, di_flags) & UF_NODUMP))
228 					CLRINO(ino, usedinomap);
229 				anydirskipped = 1;
230 			}
231 		}
232 	}
233 	/*
234 	 * Restore gets very upset if the root is not dumped,
235 	 * so ensure that it always is dumped.
236 	 */
237 	SETINO(UFS_ROOTINO, dumpinomap);
238 	return (anydirskipped);
239 }
240 
241 /*
242  * Dump pass 2.
243  *
244  * Scan each directory on the file system to see if it has any modified
245  * files in it. If it does, and has not already been added to the dump
246  * list (because it was itself modified), then add it. If a directory
247  * has not been modified itself, contains no modified files and has no
248  * subdirectories, then it can be deleted from the dump list and from
249  * the list of directories. By deleting it from the list of directories,
250  * its parent may now qualify for the same treatment on this or a later
251  * pass using this algorithm.
252  */
253 int
254 mapdirs(ino_t maxino, long *tapesize)
255 {
256 	union dinode *dp;
257 	int i, isdir, nodump;
258 	char *map;
259 	ino_t ino;
260 	union dinode di;
261 	long filesize;
262 	int ret, change = 0;
263 
264 	isdir = 0;		/* XXX just to get gcc to shut up */
265 	for (map = dumpdirmap, ino = 1; ino < maxino; ino++) {
266 		if (((ino - 1) % CHAR_BIT) == 0)	/* map is offset by 1 */
267 			isdir = *map++;
268 		else
269 			isdir >>= 1;
270 		/*
271 		 * If a directory has been removed from usedinomap, it
272 		 * either has the nodump flag set, or has inherited
273 		 * it.  Although a directory can't be in dumpinomap if
274 		 * it isn't in usedinomap, we have to go through it to
275 		 * propagate the nodump flag.
276 		 */
277 		nodump = !nonodump && (TSTINO(ino, usedinomap) == 0);
278 		if ((isdir & 1) == 0 || (TSTINO(ino, dumpinomap) && !nodump))
279 			continue;
280 		dp = getino(ino, &i);
281 		/*
282 		 * inode buf may change in searchdir().
283 		 */
284 		if (sblock->fs_magic == FS_UFS1_MAGIC)
285 			di.dp1 = dp->dp1;
286 		else
287 			di.dp2 = dp->dp2;
288 		filesize = DIP(&di, di_size);
289 		for (ret = 0, i = 0; filesize > 0 && i < UFS_NDADDR; i++) {
290 			if (DIP(&di, di_db[i]) != 0)
291 				ret |= searchdir(ino, DIP(&di, di_db[i]),
292 				    (long)sblksize(sblock, DIP(&di, di_size),
293 				    i), filesize, tapesize, nodump, maxino);
294 			if (ret & HASDUMPEDFILE)
295 				filesize = 0;
296 			else
297 				filesize -= sblock->fs_bsize;
298 		}
299 		for (i = 0; filesize > 0 && i < UFS_NIADDR; i++) {
300 			if (DIP(&di, di_ib[i]) == 0)
301 				continue;
302 			ret |= dirindir(ino, DIP(&di, di_ib[i]), i, &filesize,
303 			    tapesize, nodump, maxino);
304 		}
305 		if (ret & HASDUMPEDFILE) {
306 			SETINO(ino, dumpinomap);
307 			*tapesize += blockest(&di);
308 			change = 1;
309 			continue;
310 		}
311 		if (nodump) {
312 			if (ret & HASSUBDIRS)
313 				change = 1;	/* subdirs inherit nodump */
314 			CLRINO(ino, dumpdirmap);
315 		} else if ((ret & HASSUBDIRS) == 0)
316 			if (!TSTINO(ino, dumpinomap)) {
317 				CLRINO(ino, dumpdirmap);
318 				change = 1;
319 			}
320 	}
321 	return (change);
322 }
323 
324 /*
325  * Read indirect blocks, and pass the data blocks to be searched
326  * as directories. Quit as soon as any entry is found that will
327  * require the directory to be dumped.
328  */
329 static int
330 dirindir(
331 	ino_t ino,
332 	ufs2_daddr_t blkno,
333 	int ind_level,
334 	long *filesize,
335 	long *tapesize,
336 	int nodump,
337 	ino_t maxino)
338 {
339 	union {
340 		ufs1_daddr_t ufs1[MAXBSIZE / sizeof(ufs1_daddr_t)];
341 		ufs2_daddr_t ufs2[MAXBSIZE / sizeof(ufs2_daddr_t)];
342 	} idblk;
343 	int ret = 0;
344 	int i;
345 
346 	blkread(fsbtodb(sblock, blkno), (char *)&idblk, (int)sblock->fs_bsize);
347 	if (ind_level <= 0) {
348 		for (i = 0; *filesize > 0 && i < NINDIR(sblock); i++) {
349 			if (sblock->fs_magic == FS_UFS1_MAGIC)
350 				blkno = idblk.ufs1[i];
351 			else
352 				blkno = idblk.ufs2[i];
353 			if (blkno != 0)
354 				ret |= searchdir(ino, blkno, sblock->fs_bsize,
355 					*filesize, tapesize, nodump, maxino);
356 			if (ret & HASDUMPEDFILE)
357 				*filesize = 0;
358 			else
359 				*filesize -= sblock->fs_bsize;
360 		}
361 		return (ret);
362 	}
363 	ind_level--;
364 	for (i = 0; *filesize > 0 && i < NINDIR(sblock); i++) {
365 		if (sblock->fs_magic == FS_UFS1_MAGIC)
366 			blkno = idblk.ufs1[i];
367 		else
368 			blkno = idblk.ufs2[i];
369 		if (blkno != 0)
370 			ret |= dirindir(ino, blkno, ind_level, filesize,
371 			    tapesize, nodump, maxino);
372 	}
373 	return (ret);
374 }
375 
376 /*
377  * Scan a disk block containing directory information looking to see if
378  * any of the entries are on the dump list and to see if the directory
379  * contains any subdirectories.
380  */
381 static int
382 searchdir(
383 	ino_t ino,
384 	ufs2_daddr_t blkno,
385 	long size,
386 	long filesize,
387 	long *tapesize,
388 	int nodump,
389 	ino_t maxino)
390 {
391 	int mode;
392 	struct direct *dp;
393 	union dinode *ip;
394 	long loc, ret = 0;
395 	static caddr_t dblk;
396 
397 	if (dblk == NULL && (dblk = malloc(sblock->fs_bsize)) == NULL)
398 		quit("searchdir: cannot allocate indirect memory.\n");
399 	blkread(fsbtodb(sblock, blkno), dblk, (int)size);
400 	if (filesize < size)
401 		size = filesize;
402 	for (loc = 0; loc < size; ) {
403 		dp = (struct direct *)(dblk + loc);
404 		if (dp->d_reclen == 0) {
405 			msg("corrupted directory, inumber %ju\n",
406 			    (uintmax_t)ino);
407 			break;
408 		}
409 		loc += dp->d_reclen;
410 		if (dp->d_ino == 0)
411 			continue;
412 		if (dp->d_ino >= maxino) {
413 			msg("corrupted directory entry, d_ino %ju >= %ju\n",
414 			    (uintmax_t)dp->d_ino, (uintmax_t)maxino);
415 			break;
416 		}
417 		if (dp->d_name[0] == '.') {
418 			if (dp->d_name[1] == '\0')
419 				continue;
420 			if (dp->d_name[1] == '.' && dp->d_name[2] == '\0')
421 				continue;
422 		}
423 		if (nodump) {
424 			ip = getino(dp->d_ino, &mode);
425 			if (TSTINO(dp->d_ino, dumpinomap)) {
426 				CLRINO(dp->d_ino, dumpinomap);
427 				*tapesize -= blockest(ip);
428 			}
429 			/*
430 			 * Add back to dumpdirmap and remove from usedinomap
431 			 * to propagate nodump.
432 			 */
433 			if (mode == IFDIR) {
434 				SETINO(dp->d_ino, dumpdirmap);
435 				CLRINO(dp->d_ino, usedinomap);
436 				ret |= HASSUBDIRS;
437 			}
438 		} else {
439 			if (TSTINO(dp->d_ino, dumpinomap)) {
440 				ret |= HASDUMPEDFILE;
441 				if (ret & HASSUBDIRS)
442 					break;
443 			}
444 			if (TSTINO(dp->d_ino, dumpdirmap)) {
445 				ret |= HASSUBDIRS;
446 				if (ret & HASDUMPEDFILE)
447 					break;
448 			}
449 		}
450 	}
451 	return (ret);
452 }
453 
454 /*
455  * Dump passes 3 and 4.
456  *
457  * Dump the contents of an inode to tape.
458  */
459 void
460 dumpino(union dinode *dp, ino_t ino)
461 {
462 	int ind_level, cnt, last, added;
463 	off_t size;
464 	char buf[TP_BSIZE];
465 
466 	if (newtape) {
467 		newtape = 0;
468 		dumpmap(dumpinomap, TS_BITS, ino);
469 	}
470 	CLRINO(ino, dumpinomap);
471 	/*
472 	 * Zero out the size of a snapshot so that it will be dumped
473 	 * as a zero length file.
474 	 */
475 	if ((DIP(dp, di_flags) & SF_SNAPSHOT) != 0) {
476 		DIP_SET(dp, di_size, 0);
477 		DIP_SET(dp, di_flags, DIP(dp, di_flags) & ~SF_SNAPSHOT);
478 	}
479 	if (sblock->fs_magic == FS_UFS1_MAGIC) {
480 		spcl.c_mode = dp->dp1.di_mode;
481 		spcl.c_size = dp->dp1.di_size;
482 		spcl.c_extsize = 0;
483 		spcl.c_atime = _time32_to_time(dp->dp1.di_atime);
484 		spcl.c_atimensec = dp->dp1.di_atimensec;
485 		spcl.c_mtime = _time32_to_time(dp->dp1.di_mtime);
486 		spcl.c_mtimensec = dp->dp1.di_mtimensec;
487 		spcl.c_birthtime = 0;
488 		spcl.c_birthtimensec = 0;
489 		spcl.c_rdev = dp->dp1.di_rdev;
490 		spcl.c_file_flags = dp->dp1.di_flags;
491 		spcl.c_uid = dp->dp1.di_uid;
492 		spcl.c_gid = dp->dp1.di_gid;
493 	} else {
494 		spcl.c_mode = dp->dp2.di_mode;
495 		spcl.c_size = dp->dp2.di_size;
496 		spcl.c_extsize = dp->dp2.di_extsize;
497 		spcl.c_atime = _time64_to_time(dp->dp2.di_atime);
498 		spcl.c_atimensec = dp->dp2.di_atimensec;
499 		spcl.c_mtime = _time64_to_time(dp->dp2.di_mtime);
500 		spcl.c_mtimensec = dp->dp2.di_mtimensec;
501 		spcl.c_birthtime = _time64_to_time(dp->dp2.di_birthtime);
502 		spcl.c_birthtimensec = dp->dp2.di_birthnsec;
503 		spcl.c_rdev = dp->dp2.di_rdev;
504 		spcl.c_file_flags = dp->dp2.di_flags;
505 		spcl.c_uid = dp->dp2.di_uid;
506 		spcl.c_gid = dp->dp2.di_gid;
507 	}
508 	spcl.c_type = TS_INODE;
509 	spcl.c_count = 0;
510 	switch (DIP(dp, di_mode) & S_IFMT) {
511 
512 	case 0:
513 		/*
514 		 * Freed inode.
515 		 */
516 		return;
517 
518 	case S_IFLNK:
519 		/*
520 		 * Check for short symbolic link.
521 		 */
522 		if (DIP(dp, di_size) > 0 &&
523 		    DIP(dp, di_size) < sblock->fs_maxsymlinklen) {
524 			spcl.c_addr[0] = 1;
525 			spcl.c_count = 1;
526 			added = appendextdata(dp);
527 			writeheader(ino);
528 			memmove(buf, DIP(dp, di_shortlink),
529 			    (u_long)DIP(dp, di_size));
530 			buf[DIP(dp, di_size)] = '\0';
531 			writerec(buf, 0);
532 			writeextdata(dp, ino, added);
533 			return;
534 		}
535 		/* FALLTHROUGH */
536 
537 	case S_IFDIR:
538 	case S_IFREG:
539 		if (DIP(dp, di_size) > 0)
540 			break;
541 		/* FALLTHROUGH */
542 
543 	case S_IFIFO:
544 	case S_IFSOCK:
545 	case S_IFCHR:
546 	case S_IFBLK:
547 		added = appendextdata(dp);
548 		writeheader(ino);
549 		writeextdata(dp, ino, added);
550 		return;
551 
552 	default:
553 		msg("Warning: undefined file type 0%o\n",
554 		    DIP(dp, di_mode) & IFMT);
555 		return;
556 	}
557 	if (DIP(dp, di_size) > UFS_NDADDR * sblock->fs_bsize) {
558 		cnt = UFS_NDADDR * sblock->fs_frag;
559 		last = 0;
560 	} else {
561 		cnt = howmany(DIP(dp, di_size), sblock->fs_fsize);
562 		last = 1;
563 	}
564 	if (sblock->fs_magic == FS_UFS1_MAGIC)
565 		ufs1_blksout(&dp->dp1.di_db[0], cnt, ino);
566 	else
567 		ufs2_blksout(dp, &dp->dp2.di_db[0], cnt, ino, last);
568 	if ((size = DIP(dp, di_size) - UFS_NDADDR * sblock->fs_bsize) <= 0)
569 		return;
570 	for (ind_level = 0; ind_level < UFS_NIADDR; ind_level++) {
571 		dmpindir(dp, ino, DIP(dp, di_ib[ind_level]), ind_level, &size);
572 		if (size <= 0)
573 			return;
574 	}
575 }
576 
577 /*
578  * Read indirect blocks, and pass the data blocks to be dumped.
579  */
580 static void
581 dmpindir(union dinode *dp, ino_t ino, ufs2_daddr_t blk, int ind_level,
582 	off_t *size)
583 {
584 	union {
585 		ufs1_daddr_t ufs1[MAXBSIZE / sizeof(ufs1_daddr_t)];
586 		ufs2_daddr_t ufs2[MAXBSIZE / sizeof(ufs2_daddr_t)];
587 	} idblk;
588 	int i, cnt, last;
589 
590 	if (blk != 0)
591 		blkread(fsbtodb(sblock, blk), (char *)&idblk,
592 		    (int)sblock->fs_bsize);
593 	else
594 		memset(&idblk, 0, sblock->fs_bsize);
595 	if (ind_level <= 0) {
596 		if (*size > NINDIR(sblock) * sblock->fs_bsize) {
597 			cnt = NINDIR(sblock) * sblock->fs_frag;
598 			last = 0;
599 		} else {
600 			cnt = howmany(*size, sblock->fs_fsize);
601 			last = 1;
602 		}
603 		*size -= NINDIR(sblock) * sblock->fs_bsize;
604 		if (sblock->fs_magic == FS_UFS1_MAGIC)
605 			ufs1_blksout(idblk.ufs1, cnt, ino);
606 		else
607 			ufs2_blksout(dp, idblk.ufs2, cnt, ino, last);
608 		return;
609 	}
610 	ind_level--;
611 	for (i = 0; i < NINDIR(sblock); i++) {
612 		if (sblock->fs_magic == FS_UFS1_MAGIC)
613 			dmpindir(dp, ino, idblk.ufs1[i], ind_level, size);
614 		else
615 			dmpindir(dp, ino, idblk.ufs2[i], 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 static void
625 ufs1_blksout(ufs1_daddr_t *blkp, int frags, ino_t ino)
626 {
627 	ufs1_daddr_t *bp;
628 	int i, j, count, blks, tbperdb;
629 
630 	blks = howmany(frags * sblock->fs_fsize, TP_BSIZE);
631 	tbperdb = sblock->fs_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 		assert(count <= TP_NINDIR + i);
638 		for (j = i; j < count; j++)
639 			if (blkp[j / tbperdb] != 0)
640 				spcl.c_addr[j - i] = 1;
641 			else
642 				spcl.c_addr[j - i] = 0;
643 		spcl.c_count = count - i;
644 		writeheader(ino);
645 		bp = &blkp[i / tbperdb];
646 		for (j = i; j < count; j += tbperdb, bp++)
647 			if (*bp != 0) {
648 				if (j + tbperdb <= count)
649 					dumpblock(*bp, (int)sblock->fs_bsize);
650 				else
651 					dumpblock(*bp, (count - j) * TP_BSIZE);
652 			}
653 		spcl.c_type = TS_ADDR;
654 	}
655 }
656 
657 /*
658  * Collect up the data into tape record sized buffers and output them.
659  */
660 static void
661 ufs2_blksout(union dinode *dp, ufs2_daddr_t *blkp, int frags, ino_t ino,
662 	int last)
663 {
664 	ufs2_daddr_t *bp;
665 	int i, j, count, resid, blks, tbperdb, added;
666 	static int writingextdata = 0;
667 
668 	/*
669 	 * Calculate the number of TP_BSIZE blocks to be dumped.
670 	 * For filesystems with a fragment size bigger than TP_BSIZE,
671 	 * only part of the final fragment may need to be dumped.
672 	 */
673 	blks = howmany(frags * sblock->fs_fsize, TP_BSIZE);
674 	if (last) {
675 		if (writingextdata)
676 			resid = howmany(fragoff(sblock, spcl.c_extsize),
677 			    TP_BSIZE);
678 		else
679 			resid = howmany(fragoff(sblock, dp->dp2.di_size),
680 			    TP_BSIZE);
681 		if (resid > 0)
682 			blks -= howmany(sblock->fs_fsize, TP_BSIZE) - resid;
683 	}
684 	tbperdb = sblock->fs_bsize >> tp_bshift;
685 	for (i = 0; i < blks; i += TP_NINDIR) {
686 		if (i + TP_NINDIR > blks)
687 			count = blks;
688 		else
689 			count = i + TP_NINDIR;
690 		assert(count <= TP_NINDIR + i);
691 		for (j = i; j < count; j++)
692 			if (blkp[j / tbperdb] != 0)
693 				spcl.c_addr[j - i] = 1;
694 			else
695 				spcl.c_addr[j - i] = 0;
696 		spcl.c_count = count - i;
697 		if (last && count == blks && !writingextdata)
698 			added = appendextdata(dp);
699 		writeheader(ino);
700 		bp = &blkp[i / tbperdb];
701 		for (j = i; j < count; j += tbperdb, bp++)
702 			if (*bp != 0) {
703 				if (j + tbperdb <= count)
704 					dumpblock(*bp, (int)sblock->fs_bsize);
705 				else
706 					dumpblock(*bp, (count - j) * TP_BSIZE);
707 			}
708 		spcl.c_type = TS_ADDR;
709 		spcl.c_count = 0;
710 		if (last && count == blks && !writingextdata) {
711 			writingextdata = 1;
712 			writeextdata(dp, ino, added);
713 			writingextdata = 0;
714 		}
715 	}
716 }
717 
718 /*
719  * If there is room in the current block for the extended attributes
720  * as well as the file data, update the header to reflect the added
721  * attribute data at the end. Attributes are placed at the end so that
722  * old versions of restore will correctly restore the file and simply
723  * discard the extra data at the end that it does not understand.
724  * The attribute data is dumped following the file data by the
725  * writeextdata() function (below).
726  */
727 static int
728 appendextdata(union dinode *dp)
729 {
730 	int i, blks, tbperdb;
731 
732 	/*
733 	 * If no extended attributes, there is nothing to do.
734 	 */
735 	if (spcl.c_extsize == 0)
736 		return (0);
737 	/*
738 	 * If there is not enough room at the end of this block
739 	 * to add the extended attributes, then rather than putting
740 	 * part of them here, we simply push them entirely into a
741 	 * new block rather than putting some here and some later.
742 	 */
743 	if (spcl.c_extsize > UFS_NXADDR * sblock->fs_bsize)
744 		blks = howmany(UFS_NXADDR * sblock->fs_bsize, TP_BSIZE);
745 	else
746 		blks = howmany(spcl.c_extsize, TP_BSIZE);
747 	if (spcl.c_count + blks > TP_NINDIR)
748 		return (0);
749 	/*
750 	 * Update the block map in the header to indicate the added
751 	 * extended attribute. They will be appended after the file
752 	 * data by the writeextdata() routine.
753 	 */
754 	tbperdb = sblock->fs_bsize >> tp_bshift;
755 	assert(spcl.c_count + blks <= TP_NINDIR);
756 	for (i = 0; i < blks; i++)
757 		if (&dp->dp2.di_extb[i / tbperdb] != 0)
758 				spcl.c_addr[spcl.c_count + i] = 1;
759 			else
760 				spcl.c_addr[spcl.c_count + i] = 0;
761 	spcl.c_count += blks;
762 	return (blks);
763 }
764 
765 /*
766  * Dump the extended attribute data. If there was room in the file
767  * header, then all we need to do is output the data blocks. If there
768  * was not room in the file header, then an additional TS_ADDR header
769  * is created to hold the attribute data.
770  */
771 static void
772 writeextdata(union dinode *dp, ino_t ino, int added)
773 {
774 	int i, frags, blks, tbperdb, last;
775 	ufs2_daddr_t *bp;
776 	off_t size;
777 
778 	/*
779 	 * If no extended attributes, there is nothing to do.
780 	 */
781 	if (spcl.c_extsize == 0)
782 		return;
783 	/*
784 	 * If there was no room in the file block for the attributes,
785 	 * dump them out in a new block, otherwise just dump the data.
786 	 */
787 	if (added == 0) {
788 		if (spcl.c_extsize > UFS_NXADDR * sblock->fs_bsize) {
789 			frags = UFS_NXADDR * sblock->fs_frag;
790 			last = 0;
791 		} else {
792 			frags = howmany(spcl.c_extsize, sblock->fs_fsize);
793 			last = 1;
794 		}
795 		ufs2_blksout(dp, &dp->dp2.di_extb[0], frags, ino, last);
796 	} else {
797 		if (spcl.c_extsize > UFS_NXADDR * sblock->fs_bsize)
798 			blks = howmany(UFS_NXADDR * sblock->fs_bsize, TP_BSIZE);
799 		else
800 			blks = howmany(spcl.c_extsize, TP_BSIZE);
801 		tbperdb = sblock->fs_bsize >> tp_bshift;
802 		for (i = 0; i < blks; i += tbperdb) {
803 			bp = &dp->dp2.di_extb[i / tbperdb];
804 			if (*bp != 0) {
805 				if (i + tbperdb <= blks)
806 					dumpblock(*bp, (int)sblock->fs_bsize);
807 				else
808 					dumpblock(*bp, (blks - i) * TP_BSIZE);
809 			}
810 		}
811 
812 	}
813 	/*
814 	 * If an indirect block is added for extended attributes, then
815 	 * di_exti below should be changed to the structure element
816 	 * that references the extended attribute indirect block. This
817 	 * definition is here only to make it compile without complaint.
818 	 */
819 #define di_exti di_spare[0]
820 	/*
821 	 * If the extended attributes fall into an indirect block,
822 	 * dump it as well.
823 	 */
824 	if ((size = spcl.c_extsize - UFS_NXADDR * sblock->fs_bsize) > 0)
825 		dmpindir(dp, ino, dp->dp2.di_exti, 0, &size);
826 }
827 
828 /*
829  * Dump a map to the tape.
830  */
831 void
832 dumpmap(char *map, int type, ino_t ino)
833 {
834 	int i;
835 	char *cp;
836 
837 	spcl.c_type = type;
838 	spcl.c_count = howmany(mapsize * sizeof(char), TP_BSIZE);
839 	writeheader(ino);
840 	for (i = 0, cp = map; i < spcl.c_count; i++, cp += TP_BSIZE)
841 		writerec(cp, 0);
842 }
843 
844 /*
845  * Write a header record to the dump tape.
846  */
847 void
848 writeheader(ino_t ino)
849 {
850 	int32_t sum, cnt, *lp;
851 
852 	if (rsync_friendly >= 2) {
853 		/* don't track changes to access time */
854 		spcl.c_atime = spcl.c_mtime;
855 		spcl.c_atimensec = spcl.c_mtimensec;
856 	}
857 	spcl.c_inumber = ino;
858 	spcl.c_magic = FS_UFS2_MAGIC;
859 	spcl.c_checksum = 0;
860 	lp = (int32_t *)&spcl;
861 	sum = 0;
862 	cnt = sizeof(union u_spcl) / (4 * sizeof(int32_t));
863 	while (--cnt >= 0) {
864 		sum += *lp++;
865 		sum += *lp++;
866 		sum += *lp++;
867 		sum += *lp++;
868 	}
869 	spcl.c_checksum = CHECKSUM - sum;
870 	writerec((char *)&spcl, 1);
871 }
872 
873 union dinode *
874 getino(ino_t inum, int *modep)
875 {
876 	static ino_t minino, maxino;
877 	static caddr_t inoblock;
878 	struct ufs1_dinode *dp1;
879 	struct ufs2_dinode *dp2;
880 
881 	if (inoblock == NULL && (inoblock = malloc(sblock->fs_bsize)) == NULL)
882 		quit("cannot allocate inode memory.\n");
883 	curino = inum;
884 	if (inum >= minino && inum < maxino)
885 		goto gotit;
886 	blkread(fsbtodb(sblock, ino_to_fsba(sblock, inum)), inoblock,
887 	    (int)sblock->fs_bsize);
888 	minino = inum - (inum % INOPB(sblock));
889 	maxino = minino + INOPB(sblock);
890 gotit:
891 	if (sblock->fs_magic == FS_UFS1_MAGIC) {
892 		dp1 = &((struct ufs1_dinode *)inoblock)[inum - minino];
893 		*modep = (dp1->di_mode & IFMT);
894 		return ((union dinode *)dp1);
895 	}
896 	dp2 = &((struct ufs2_dinode *)inoblock)[inum - minino];
897 	*modep = (dp2->di_mode & IFMT);
898 	return ((union dinode *)dp2);
899 }
900 
901 /*
902  * Read a chunk of data from the disk.
903  * Try to recover from hard errors by reading in sector sized pieces.
904  * Error recovery is attempted at most BREADEMAX times before seeking
905  * consent from the operator to continue.
906  */
907 int	breaderrors = 0;
908 #define	BREADEMAX 32
909 
910 void
911 blkread(ufs2_daddr_t blkno, char *buf, int size)
912 {
913 	int secsize, bytes, resid, xfer, base, cnt, i;
914 	static char *tmpbuf;
915 	off_t offset;
916 
917 loop:
918 	offset = blkno << dev_bshift;
919 	secsize = sblock->fs_fsize;
920 	base = offset % secsize;
921 	resid = size % secsize;
922 	/*
923 	 * If the transfer request starts or ends on a non-sector
924 	 * boundary, we must read the entire sector and copy out
925 	 * just the part that we need.
926 	 */
927 	if (base == 0 && resid == 0) {
928 		cnt = cread(diskfd, buf, size, offset);
929 		if (cnt == size)
930 			return;
931 	} else {
932 		if (tmpbuf == NULL && (tmpbuf = malloc(secsize)) == NULL)
933 			quit("buffer malloc failed\n");
934 		xfer = 0;
935 		bytes = size;
936 		if (base != 0) {
937 			cnt = cread(diskfd, tmpbuf, secsize, offset - base);
938 			if (cnt != secsize)
939 				goto bad;
940 			xfer = MIN(secsize - base, size);
941 			offset += xfer;
942 			bytes -= xfer;
943 			resid = bytes % secsize;
944 			memcpy(buf, &tmpbuf[base], xfer);
945 		}
946 		if (bytes >= secsize) {
947 			cnt = cread(diskfd, &buf[xfer], bytes - resid, offset);
948 			if (cnt != bytes - resid)
949 				goto bad;
950 			xfer += cnt;
951 			offset += cnt;
952 		}
953 		if (resid == 0)
954 			return;
955 		cnt = cread(diskfd, tmpbuf, secsize, offset);
956 		if (cnt == secsize) {
957 			memcpy(&buf[xfer], tmpbuf, resid);
958 			return;
959 		}
960 	}
961 bad:
962 	if (blkno + (size / dev_bsize) > fsbtodb(sblock, sblock->fs_size)) {
963 		/*
964 		 * Trying to read the final fragment.
965 		 *
966 		 * NB - dump only works in TP_BSIZE blocks, hence
967 		 * rounds `dev_bsize' fragments up to TP_BSIZE pieces.
968 		 * It should be smarter about not actually trying to
969 		 * read more than it can get, but for the time being
970 		 * we punt and scale back the read only when it gets
971 		 * us into trouble. (mkm 9/25/83)
972 		 */
973 		size -= dev_bsize;
974 		goto loop;
975 	}
976 	if (cnt == -1)
977 		msg("read error from %s: %s: [block %jd]: count=%d\n",
978 			disk, strerror(errno), (intmax_t)blkno, size);
979 	else
980 		msg("short read error from %s: [block %jd]: count=%d, got=%d\n",
981 			disk, (intmax_t)blkno, size, cnt);
982 	if (++breaderrors > BREADEMAX) {
983 		msg("More than %d block read errors from %s\n",
984 			BREADEMAX, disk);
985 		broadcast("DUMP IS AILING!\n");
986 		msg("This is an unrecoverable error.\n");
987 		if (!query("Do you want to attempt to continue?")){
988 			dumpabort(0);
989 			/*NOTREACHED*/
990 		} else
991 			breaderrors = 0;
992 	}
993 	/*
994 	 * Zero buffer, then try to read each sector of buffer separately,
995 	 * and bypass the cache.
996 	 */
997 	memset(buf, 0, size);
998 	for (i = 0; i < size; i += dev_bsize, buf += dev_bsize, blkno++) {
999 		if ((cnt = pread(diskfd, buf, (int)dev_bsize,
1000 		    ((off_t)blkno << dev_bshift))) == dev_bsize)
1001 			continue;
1002 		if (cnt == -1) {
1003 			msg("read error from %s: %s: [sector %jd]: count=%ld\n",
1004 			    disk, strerror(errno), (intmax_t)blkno, dev_bsize);
1005 			continue;
1006 		}
1007 		msg("short read from %s: [sector %jd]: count=%ld, got=%d\n",
1008 		    disk, (intmax_t)blkno, dev_bsize, cnt);
1009 	}
1010 }
1011