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