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