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