traverse.c revision 97956
1177633Sdfr/*-
2177633Sdfr * Copyright (c) 1980, 1988, 1991, 1993
3177633Sdfr *	The Regents of the University of California.  All rights reserved.
4177633Sdfr *
5177633Sdfr * Redistribution and use in source and binary forms, with or without
6177633Sdfr * modification, are permitted provided that the following conditions
7177633Sdfr * are met:
8177633Sdfr * 1. Redistributions of source code must retain the above copyright
9177633Sdfr *    notice, this list of conditions and the following disclaimer.
10177633Sdfr * 2. Redistributions in binary form must reproduce the above copyright
11177633Sdfr *    notice, this list of conditions and the following disclaimer in the
12177633Sdfr *    documentation and/or other materials provided with the distribution.
13177633Sdfr * 3. All advertising materials mentioning features or use of this software
14177633Sdfr *    must display the following acknowledgement:
15177633Sdfr *	This product includes software developed by the University of
16177633Sdfr *	California, Berkeley and its contributors.
17177633Sdfr * 4. Neither the name of the University nor the names of its contributors
18177633Sdfr *    may be used to endorse or promote products derived from this software
19177633Sdfr *    without specific prior written permission.
20177633Sdfr *
21177633Sdfr * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22177633Sdfr * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23177633Sdfr * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24177633Sdfr * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25177633Sdfr * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26177633Sdfr * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27177633Sdfr * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28177633Sdfr * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29177633Sdfr * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30177633Sdfr * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31177633Sdfr * SUCH DAMAGE.
32177633Sdfr */
33177633Sdfr
34177633Sdfr#ifndef lint
35177633Sdfr#if 0
36177633Sdfrstatic char sccsid[] = "@(#)traverse.c	8.7 (Berkeley) 6/15/95";
37177633Sdfr#endif
38177633Sdfrstatic const char rcsid[] =
39177633Sdfr  "$FreeBSD: head/sbin/dump/traverse.c 97956 2002-06-06 19:59:46Z phk $";
40177685Sdfr#endif /* not lint */
41177685Sdfr
42177633Sdfr#include <sys/param.h>
43177633Sdfr#include <sys/stat.h>
44177633Sdfr
45177633Sdfr#include <ufs/ufs/dir.h>
46177633Sdfr#include <ufs/ufs/dinode.h>
47177633Sdfr#include <ufs/ffs/fs.h>
48177633Sdfr
49177633Sdfr#include <protocols/dumprestore.h>
50177633Sdfr
51177633Sdfr#include <ctype.h>
52177633Sdfr#include <stdio.h>
53177633Sdfr#include <errno.h>
54177633Sdfr#include <string.h>
55177633Sdfr#include <unistd.h>
56177633Sdfr
57177633Sdfr#include "dump.h"
58177633Sdfr
59177633Sdfr#define	HASDUMPEDFILE	0x1
60177633Sdfr#define	HASSUBDIRS	0x2
61177633Sdfr
62177633Sdfr#ifdef	FS_44INODEFMT
63177633Sdfrtypedef	quad_t fsizeT;
64177633Sdfr#else
65177633Sdfrtypedef	long fsizeT;
66177633Sdfr#endif
67177633Sdfr
68177633Sdfrstatic	int dirindir(ino_t ino, ufs_daddr_t blkno, int level, long *size,
69177633Sdfr    long *tapesize, int nodump);
70177633Sdfrstatic	void dmpindir(ino_t ino, ufs_daddr_t blk, int level, fsizeT *size);
71177633Sdfrstatic	int searchdir(ino_t ino, ufs_daddr_t blkno, long size, long filesize,
72177633Sdfr    long *tapesize, int nodump);
73177633Sdfr
74177633Sdfr/*
75177633Sdfr * This is an estimation of the number of TP_BSIZE blocks in the file.
76177633Sdfr * It estimates the number of blocks in files with holes by assuming
77177633Sdfr * that all of the blocks accounted for by di_blocks are data blocks
78177633Sdfr * (when some of the blocks are usually used for indirect pointers);
79177633Sdfr * hence the estimate may be high.
80177633Sdfr */
81177633Sdfrlong
82177633Sdfrblockest(struct dinode *dp)
83177633Sdfr{
84177633Sdfr	long blkest, sizeest;
85177633Sdfr
86177633Sdfr	/*
87177633Sdfr	 * dp->di_size is the size of the file in bytes.
88177633Sdfr	 * dp->di_blocks stores the number of sectors actually in the file.
89177633Sdfr	 * If there are more sectors than the size would indicate, this just
90177633Sdfr	 *	means that there are indirect blocks in the file or unused
91177633Sdfr	 *	sectors in the last file block; we can safely ignore these
92177633Sdfr	 *	(blkest = sizeest below).
93177633Sdfr	 * If the file is bigger than the number of sectors would indicate,
94177633Sdfr	 *	then the file has holes in it.	In this case we must use the
95177633Sdfr	 *	block count to estimate the number of data blocks used, but
96177633Sdfr	 *	we use the actual size for estimating the number of indirect
97177633Sdfr	 *	dump blocks (sizeest vs. blkest in the indirect block
98177633Sdfr	 *	calculation).
99177633Sdfr	 */
100177633Sdfr	blkest = howmany(dbtob(dp->di_blocks), TP_BSIZE);
101177633Sdfr	sizeest = howmany(dp->di_size, TP_BSIZE);
102177633Sdfr	if (blkest > sizeest)
103177633Sdfr		blkest = sizeest;
104177633Sdfr	if (dp->di_size > sblock->fs_bsize * NDADDR) {
105177633Sdfr		/* calculate the number of indirect blocks on the dump tape */
106177633Sdfr		blkest +=
107177633Sdfr			howmany(sizeest - NDADDR * sblock->fs_bsize / TP_BSIZE,
108177633Sdfr			TP_NINDIR);
109177633Sdfr	}
110177633Sdfr	return (blkest + 1);
111177633Sdfr}
112177633Sdfr
113177633Sdfr/* Auxiliary macro to pick up files changed since previous dump. */
114177633Sdfr#define	CHANGEDSINCE(dp, t) \
115177633Sdfr	((dp)->di_mtime >= (t) || (dp)->di_ctime >= (t))
116177633Sdfr
117177633Sdfr/* The WANTTODUMP macro decides whether a file should be dumped. */
118177633Sdfr#ifdef UF_NODUMP
119177633Sdfr#define	WANTTODUMP(dp) \
120177633Sdfr	(CHANGEDSINCE(dp, spcl.c_ddate) && \
121177633Sdfr	 (nonodump || ((dp)->di_flags & UF_NODUMP) != UF_NODUMP))
122177633Sdfr#else
123177633Sdfr#define	WANTTODUMP(dp) CHANGEDSINCE(dp, spcl.c_ddate)
124177633Sdfr#endif
125177633Sdfr
126177633Sdfr/*
127177633Sdfr * Dump pass 1.
128177633Sdfr *
129177633Sdfr * Walk the inode list for a filesystem to find all allocated inodes
130177633Sdfr * that have been modified since the previous dump time. Also, find all
131177633Sdfr * the directories in the filesystem.
132177633Sdfr */
133177633Sdfrint
134177633Sdfrmapfiles(ino_t maxino, long *tapesize)
135177633Sdfr{
136177633Sdfr	int mode;
137177633Sdfr	ino_t ino;
138177633Sdfr	struct dinode *dp;
139177633Sdfr	int anydirskipped = 0;
140177633Sdfr
141177633Sdfr	for (ino = ROOTINO; ino < maxino; ino++) {
142177633Sdfr		dp = getino(ino);
143177633Sdfr		if ((mode = (dp->di_mode & IFMT)) == 0)
144177633Sdfr			continue;
145177633Sdfr		/*
146177633Sdfr		 * Everything must go in usedinomap so that a check
147177633Sdfr		 * for "in dumpdirmap but not in usedinomap" to detect
148177633Sdfr		 * dirs with nodump set has a chance of succeeding
149177633Sdfr		 * (this is used in mapdirs()).
150177633Sdfr		 */
151177633Sdfr		SETINO(ino, usedinomap);
152177633Sdfr		if (mode == IFDIR)
153177633Sdfr			SETINO(ino, dumpdirmap);
154177633Sdfr		if (WANTTODUMP(dp)) {
155177633Sdfr			SETINO(ino, dumpinomap);
156177633Sdfr			if (mode != IFREG && mode != IFDIR && mode != IFLNK)
157177633Sdfr				*tapesize += 1;
158177633Sdfr			else
159177633Sdfr				*tapesize += blockest(dp);
160177633Sdfr			continue;
161177633Sdfr		}
162177633Sdfr		if (mode == IFDIR) {
163177633Sdfr			if (!nonodump && (dp->di_flags & UF_NODUMP))
164177633Sdfr				CLRINO(ino, usedinomap);
165177633Sdfr			anydirskipped = 1;
166177633Sdfr		}
167177633Sdfr	}
168177633Sdfr	/*
169177633Sdfr	 * Restore gets very upset if the root is not dumped,
170177633Sdfr	 * so ensure that it always is dumped.
171177633Sdfr	 */
172177633Sdfr	SETINO(ROOTINO, dumpinomap);
173177633Sdfr	return (anydirskipped);
174177633Sdfr}
175177633Sdfr
176177633Sdfr/*
177177633Sdfr * Dump pass 2.
178177633Sdfr *
179177633Sdfr * Scan each directory on the filesystem to see if it has any modified
180177633Sdfr * files in it. If it does, and has not already been added to the dump
181177633Sdfr * list (because it was itself modified), then add it. If a directory
182177633Sdfr * has not been modified itself, contains no modified files and has no
183177633Sdfr * subdirectories, then it can be deleted from the dump list and from
184177633Sdfr * the list of directories. By deleting it from the list of directories,
185177633Sdfr * its parent may now qualify for the same treatment on this or a later
186177633Sdfr * pass using this algorithm.
187177633Sdfr */
188177633Sdfrint
189177633Sdfrmapdirs(ino_t maxino, long *tapesize)
190177633Sdfr{
191177633Sdfr	struct	dinode *dp;
192177633Sdfr	int i, isdir, nodump;
193177633Sdfr	char *map;
194177633Sdfr	ino_t ino;
195177633Sdfr	struct dinode di;
196177633Sdfr	long filesize;
197177633Sdfr	int ret, change = 0;
198177633Sdfr
199177633Sdfr	isdir = 0;		/* XXX just to get gcc to shut up */
200177633Sdfr	for (map = dumpdirmap, ino = 1; ino < maxino; ino++) {
201177633Sdfr		if (((ino - 1) % NBBY) == 0)	/* map is offset by 1 */
202177633Sdfr			isdir = *map++;
203177633Sdfr		else
204177633Sdfr			isdir >>= 1;
205177633Sdfr		/*
206177633Sdfr		 * If a directory has been removed from usedinomap, it
207177633Sdfr		 * either has the nodump flag set, or has inherited
208177633Sdfr		 * it.  Although a directory can't be in dumpinomap if
209177633Sdfr		 * it isn't in usedinomap, we have to go through it to
210177633Sdfr		 * propagate the nodump flag.
211177633Sdfr		 */
212177633Sdfr		nodump = !nonodump && (TSTINO(ino, usedinomap) == 0);
213177633Sdfr		if ((isdir & 1) == 0 || (TSTINO(ino, dumpinomap) && !nodump))
214177633Sdfr			continue;
215177633Sdfr		dp = getino(ino);
216177633Sdfr		di = *dp;	/* inode buf may change in searchdir(). */
217177633Sdfr		filesize = di.di_size;
218177633Sdfr		for (ret = 0, i = 0; filesize > 0 && i < NDADDR; i++) {
219177633Sdfr			if (di.di_db[i] != 0)
220177633Sdfr				ret |= searchdir(ino, di.di_db[i],
221177633Sdfr					(long)dblksize(sblock, dp, i),
222177633Sdfr					filesize, tapesize, nodump);
223177633Sdfr			if (ret & HASDUMPEDFILE)
224177633Sdfr				filesize = 0;
225177633Sdfr			else
226177633Sdfr				filesize -= sblock->fs_bsize;
227177633Sdfr		}
228177633Sdfr		for (i = 0; filesize > 0 && i < NIADDR; i++) {
229177633Sdfr			if (di.di_ib[i] == 0)
230177633Sdfr				continue;
231177633Sdfr			ret |= dirindir(ino, di.di_ib[i], i, &filesize,
232177633Sdfr			    tapesize, nodump);
233177633Sdfr		}
234177633Sdfr		if (ret & HASDUMPEDFILE) {
235177633Sdfr			SETINO(ino, dumpinomap);
236177633Sdfr			*tapesize += blockest(dp);
237177633Sdfr			change = 1;
238177633Sdfr			continue;
239177633Sdfr		}
240177633Sdfr		if (nodump) {
241177633Sdfr			if (ret & HASSUBDIRS)
242180025Sdfr				change = 1;	/* subdirs inherit nodump */
243180025Sdfr			CLRINO(ino, dumpdirmap);
244177633Sdfr		} else if ((ret & HASSUBDIRS) == 0)
245177633Sdfr			if (!TSTINO(ino, dumpinomap)) {
246177633Sdfr				CLRINO(ino, dumpdirmap);
247177633Sdfr				change = 1;
248177633Sdfr			}
249177633Sdfr	}
250177633Sdfr	return (change);
251177633Sdfr}
252180025Sdfr
253180025Sdfr/*
254180025Sdfr * Read indirect blocks, and pass the data blocks to be searched
255180025Sdfr * as directories. Quit as soon as any entry is found that will
256177633Sdfr * require the directory to be dumped.
257177633Sdfr */
258177633Sdfrstatic int
259177633Sdfrdirindir(ino_t ino, ufs_daddr_t blkno, int ind_level, long *filesize,
260177633Sdfr  long *tapesize, int nodump)
261177633Sdfr{
262177633Sdfr	int ret = 0;
263177633Sdfr	int i;
264177633Sdfr	ufs_daddr_t	idblk[MAXNINDIR];
265177633Sdfr
266177633Sdfr	bread(fsbtodb(sblock, blkno), (char *)idblk, (int)sblock->fs_bsize);
267177633Sdfr	if (ind_level <= 0) {
268177633Sdfr		for (i = 0; *filesize > 0 && i < NINDIR(sblock); i++) {
269177633Sdfr			blkno = idblk[i];
270177633Sdfr			if (blkno != 0)
271177633Sdfr				ret |= searchdir(ino, blkno, sblock->fs_bsize,
272177633Sdfr					*filesize, tapesize, nodump);
273177633Sdfr			if (ret & HASDUMPEDFILE)
274177633Sdfr				*filesize = 0;
275177633Sdfr			else
276177633Sdfr				*filesize -= sblock->fs_bsize;
277180025Sdfr		}
278180025Sdfr		return (ret);
279177633Sdfr	}
280177633Sdfr	ind_level--;
281177633Sdfr	for (i = 0; *filesize > 0 && i < NINDIR(sblock); i++) {
282180025Sdfr		blkno = idblk[i];
283180025Sdfr		if (blkno != 0)
284180025Sdfr			ret |= dirindir(ino, blkno, ind_level, filesize,
285180025Sdfr			    tapesize, nodump);
286177633Sdfr	}
287177633Sdfr	return (ret);
288177633Sdfr}
289177633Sdfr
290177633Sdfr/*
291177633Sdfr * Scan a disk block containing directory information looking to see if
292177633Sdfr * any of the entries are on the dump list and to see if the directory
293177633Sdfr * contains any subdirectories.
294177633Sdfr */
295177633Sdfrstatic int
296177633Sdfrsearchdir(ino_t ino, ufs_daddr_t blkno, long size, long filesize,
297177633Sdfr  long *tapesize, int nodump)
298177633Sdfr{
299177633Sdfr	struct direct *dp;
300177633Sdfr	struct dinode *ip;
301177633Sdfr	long loc, ret = 0;
302177633Sdfr	char dblk[MAXBSIZE];
303177633Sdfr
304177633Sdfr	bread(fsbtodb(sblock, blkno), dblk, (int)size);
305180025Sdfr	if (filesize < size)
306180025Sdfr		size = filesize;
307177633Sdfr	for (loc = 0; loc < size; ) {
308177633Sdfr		dp = (struct direct *)(dblk + loc);
309177633Sdfr		if (dp->d_reclen == 0) {
310180025Sdfr			msg("corrupted directory, inumber %d\n", ino);
311180025Sdfr			break;
312180025Sdfr		}
313180025Sdfr		loc += dp->d_reclen;
314177633Sdfr		if (dp->d_ino == 0)
315177633Sdfr			continue;
316177633Sdfr		if (dp->d_name[0] == '.') {
317177633Sdfr			if (dp->d_name[1] == '\0')
318177633Sdfr				continue;
319177633Sdfr			if (dp->d_name[1] == '.' && dp->d_name[2] == '\0')
320177633Sdfr				continue;
321177633Sdfr		}
322177633Sdfr		if (nodump) {
323177633Sdfr			ip = getino(dp->d_ino);
324177633Sdfr			if (TSTINO(dp->d_ino, dumpinomap)) {
325177633Sdfr				CLRINO(dp->d_ino, dumpinomap);
326177633Sdfr				*tapesize -= blockest(ip);
327177633Sdfr			}
328177633Sdfr			/*
329177633Sdfr			 * Add back to dumpdirmap and remove from usedinomap
330177633Sdfr			 * to propagate nodump.
331180025Sdfr			 */
332180025Sdfr			if ((ip->di_mode & IFMT) == IFDIR) {
333177633Sdfr				SETINO(dp->d_ino, dumpdirmap);
334177633Sdfr				CLRINO(dp->d_ino, usedinomap);
335177633Sdfr				ret |= HASSUBDIRS;
336180025Sdfr			}
337180025Sdfr		} else {
338180025Sdfr			if (TSTINO(dp->d_ino, dumpinomap)) {
339180025Sdfr				ret |= HASDUMPEDFILE;
340177633Sdfr				if (ret & HASSUBDIRS)
341177633Sdfr					break;
342177633Sdfr			}
343177633Sdfr			if (TSTINO(dp->d_ino, dumpdirmap)) {
344177633Sdfr				ret |= HASSUBDIRS;
345177633Sdfr				if (ret & HASDUMPEDFILE)
346177633Sdfr					break;
347177633Sdfr			}
348177633Sdfr		}
349177633Sdfr	}
350177633Sdfr	return (ret);
351177633Sdfr}
352177633Sdfr
353177633Sdfr/*
354177633Sdfr * Dump passes 3 and 4.
355177633Sdfr *
356177633Sdfr * Dump the contents of an inode to tape.
357177633Sdfr */
358180025Sdfrvoid
359180025Sdfrdumpino(struct dinode *dp, ino_t ino)
360177633Sdfr{
361177633Sdfr	int ind_level, cnt;
362177633Sdfr	fsizeT size;
363180025Sdfr	char buf[TP_BSIZE];
364180025Sdfr
365180025Sdfr	if (newtape) {
366180025Sdfr		newtape = 0;
367177633Sdfr		dumpmap(dumpinomap, TS_BITS, ino);
368177633Sdfr	}
369177633Sdfr	CLRINO(ino, dumpinomap);
370177633Sdfr	spcl.c_dinode = *dp;
371177633Sdfr	spcl.c_type = TS_INODE;
372177633Sdfr	spcl.c_count = 0;
373177633Sdfr	switch (dp->di_mode & S_IFMT) {
374177633Sdfr
375177633Sdfr	case 0:
376177633Sdfr		/*
377177633Sdfr		 * Freed inode.
378177633Sdfr		 */
379177633Sdfr		return;
380177633Sdfr
381177633Sdfr	case S_IFLNK:
382177633Sdfr		/*
383177633Sdfr		 * Check for short symbolic link.
384177633Sdfr		 */
385177633Sdfr#ifdef FS_44INODEFMT
386177633Sdfr		if (dp->di_size > 0 &&
387177633Sdfr		    dp->di_size < sblock->fs_maxsymlinklen) {
388177633Sdfr			spcl.c_addr[0] = 1;
389177633Sdfr			spcl.c_count = 1;
390177633Sdfr			writeheader(ino);
391177633Sdfr			memmove(buf, dp->di_shortlink, (u_long)dp->di_size);
392177633Sdfr			buf[dp->di_size] = '\0';
393177633Sdfr			writerec(buf, 0);
394177633Sdfr			return;
395177633Sdfr		}
396177633Sdfr#endif
397177633Sdfr		/* fall through */
398177633Sdfr
399177633Sdfr	case S_IFDIR:
400177633Sdfr	case S_IFREG:
401177633Sdfr		if (dp->di_size > 0)
402177633Sdfr			break;
403177633Sdfr		/* fall through */
404177633Sdfr
405177633Sdfr	case S_IFIFO:
406177633Sdfr	case S_IFSOCK:
407177633Sdfr	case S_IFCHR:
408177633Sdfr	case S_IFBLK:
409177633Sdfr		writeheader(ino);
410177633Sdfr		return;
411177633Sdfr
412177633Sdfr	default:
413177633Sdfr		msg("Warning: undefined file type 0%o\n", dp->di_mode & IFMT);
414177633Sdfr		return;
415177633Sdfr	}
416177633Sdfr	if (dp->di_size > NDADDR * sblock->fs_bsize)
417177633Sdfr		cnt = NDADDR * sblock->fs_frag;
418177633Sdfr	else
419177633Sdfr		cnt = howmany(dp->di_size, sblock->fs_fsize);
420177633Sdfr	blksout(&dp->di_db[0], cnt, ino);
421177633Sdfr	if ((size = dp->di_size - NDADDR * sblock->fs_bsize) <= 0)
422177633Sdfr		return;
423177633Sdfr	for (ind_level = 0; ind_level < NIADDR; ind_level++) {
424177633Sdfr		dmpindir(ino, dp->di_ib[ind_level], ind_level, &size);
425177633Sdfr		if (size <= 0)
426177633Sdfr			return;
427177633Sdfr	}
428177633Sdfr}
429177633Sdfr
430177633Sdfr/*
431177633Sdfr * Read indirect blocks, and pass the data blocks to be dumped.
432177633Sdfr */
433177633Sdfrstatic void
434177633Sdfrdmpindir(ino_t ino, ufs_daddr_t blk, int ind_level, fsizeT *size)
435177633Sdfr{
436177633Sdfr	int i, cnt;
437177633Sdfr	ufs_daddr_t idblk[MAXNINDIR];
438177633Sdfr
439177633Sdfr	if (blk != 0)
440177633Sdfr		bread(fsbtodb(sblock, blk), (char *)idblk, (int) sblock->fs_bsize);
441177633Sdfr	else
442177633Sdfr		memset(idblk, 0, (int)sblock->fs_bsize);
443177633Sdfr	if (ind_level <= 0) {
444177633Sdfr		if (*size < NINDIR(sblock) * sblock->fs_bsize)
445177633Sdfr			cnt = howmany(*size, sblock->fs_fsize);
446177633Sdfr		else
447177633Sdfr			cnt = NINDIR(sblock) * sblock->fs_frag;
448177633Sdfr		*size -= NINDIR(sblock) * sblock->fs_bsize;
449177633Sdfr		blksout(&idblk[0], cnt, ino);
450177633Sdfr		return;
451177633Sdfr	}
452177633Sdfr	ind_level--;
453177633Sdfr	for (i = 0; i < NINDIR(sblock); i++) {
454177633Sdfr		dmpindir(ino, idblk[i], ind_level, size);
455177633Sdfr		if (*size <= 0)
456177633Sdfr			return;
457177633Sdfr	}
458177633Sdfr}
459177633Sdfr
460177633Sdfr/*
461177633Sdfr * Collect up the data into tape record sized buffers and output them.
462177633Sdfr */
463177633Sdfrvoid
464177633Sdfrblksout(ufs_daddr_t *blkp, int frags, ino_t ino)
465177633Sdfr{
466177633Sdfr	ufs_daddr_t *bp;
467177633Sdfr	int i, j, count, blks, tbperdb;
468177633Sdfr
469177633Sdfr	blks = howmany(frags * sblock->fs_fsize, TP_BSIZE);
470177633Sdfr	tbperdb = sblock->fs_bsize >> tp_bshift;
471177633Sdfr	for (i = 0; i < blks; i += TP_NINDIR) {
472177633Sdfr		if (i + TP_NINDIR > blks)
473177633Sdfr			count = blks;
474177633Sdfr		else
475177633Sdfr			count = i + TP_NINDIR;
476177633Sdfr		for (j = i; j < count; j++)
477177633Sdfr			if (blkp[j / tbperdb] != 0)
478177633Sdfr				spcl.c_addr[j - i] = 1;
479177633Sdfr			else
480177633Sdfr				spcl.c_addr[j - i] = 0;
481177633Sdfr		spcl.c_count = count - i;
482177633Sdfr		writeheader(ino);
483177633Sdfr		bp = &blkp[i / tbperdb];
484177633Sdfr		for (j = i; j < count; j += tbperdb, bp++)
485177633Sdfr			if (*bp != 0) {
486177633Sdfr				if (j + tbperdb <= count)
487177633Sdfr					dumpblock(*bp, (int)sblock->fs_bsize);
488177633Sdfr				else
489177633Sdfr					dumpblock(*bp, (count - j) * TP_BSIZE);
490177633Sdfr			}
491177633Sdfr		spcl.c_type = TS_ADDR;
492177633Sdfr	}
493177633Sdfr}
494177633Sdfr
495177633Sdfr/*
496177633Sdfr * Dump a map to the tape.
497177633Sdfr */
498177633Sdfrvoid
499177633Sdfrdumpmap(char *map, int type, ino_t ino)
500177633Sdfr{
501177633Sdfr	int i;
502177633Sdfr	char *cp;
503177633Sdfr
504177633Sdfr	spcl.c_type = type;
505177633Sdfr	spcl.c_count = howmany(mapsize * sizeof(char), TP_BSIZE);
506177633Sdfr	writeheader(ino);
507177633Sdfr	for (i = 0, cp = map; i < spcl.c_count; i++, cp += TP_BSIZE)
508177633Sdfr		writerec(cp, 0);
509177633Sdfr}
510177633Sdfr
511177633Sdfr/*
512177633Sdfr * Write a header record to the dump tape.
513177633Sdfr */
514177633Sdfrvoid
515177633Sdfrwriteheader(ino_t ino)
516180025Sdfr{
517177633Sdfr	int32_t sum, cnt, *lp;
518177633Sdfr
519177633Sdfr	spcl.c_inumber = ino;
520177633Sdfr	spcl.c_magic = NFS_MAGIC;
521177633Sdfr	spcl.c_checksum = 0;
522177633Sdfr	lp = (int32_t *)&spcl;
523177633Sdfr	sum = 0;
524180025Sdfr	cnt = sizeof(union u_spcl) / (4 * sizeof(int32_t));
525177633Sdfr	while (--cnt >= 0) {
526177633Sdfr		sum += *lp++;
527177633Sdfr		sum += *lp++;
528177633Sdfr		sum += *lp++;
529177633Sdfr		sum += *lp++;
530177633Sdfr	}
531177633Sdfr	spcl.c_checksum = CHECKSUM - sum;
532180025Sdfr	writerec((char *)&spcl, 1);
533177633Sdfr}
534177633Sdfr
535177633Sdfrstruct dinode *
536177633Sdfrgetino(ino_t inum)
537177633Sdfr{
538177633Sdfr	static ufs_daddr_t minino, maxino;
539177633Sdfr	static struct dinode inoblock[MAXINOPB];
540180025Sdfr
541177633Sdfr	curino = inum;
542177633Sdfr	if (inum >= minino && inum < maxino)
543177633Sdfr		return (&inoblock[inum - minino]);
544177633Sdfr	bread(fsbtodb(sblock, ino_to_fsba(sblock, inum)), (char *)inoblock,
545177633Sdfr	    (int)sblock->fs_bsize);
546177633Sdfr	minino = inum - (inum % INOPB(sblock));
547177633Sdfr	maxino = minino + INOPB(sblock);
548180025Sdfr	return (&inoblock[inum - minino]);
549177633Sdfr}
550177633Sdfr
551177633Sdfr/*
552177633Sdfr * Read a chunk of data from the disk.
553177633Sdfr * Try to recover from hard errors by reading in sector sized pieces.
554177633Sdfr * Error recovery is attempted at most BREADEMAX times before seeking
555177633Sdfr * consent from the operator to continue.
556177633Sdfr */
557177633Sdfrint	breaderrors = 0;
558177633Sdfr#define	BREADEMAX 32
559180025Sdfr
560180025Sdfrvoid
561180025Sdfrbread(ufs_daddr_t blkno, char *buf, int size)
562180025Sdfr{
563180025Sdfr	int cnt, i;
564180025Sdfr
565177633Sdfrloop:
566177633Sdfr	if ((cnt = pread(diskfd, buf, size, ((off_t)blkno << dev_bshift))) ==
567177633Sdfr						size)
568177633Sdfr		return;
569177633Sdfr	if (blkno + (size / dev_bsize) > fsbtodb(sblock, sblock->fs_size)) {
570177633Sdfr		/*
571177633Sdfr		 * Trying to read the final fragment.
572177633Sdfr		 *
573177633Sdfr		 * NB - dump only works in TP_BSIZE blocks, hence
574177633Sdfr		 * rounds `dev_bsize' fragments up to TP_BSIZE pieces.
575177633Sdfr		 * It should be smarter about not actually trying to
576177633Sdfr		 * read more than it can get, but for the time being
577180025Sdfr		 * we punt and scale back the read only when it gets
578180025Sdfr		 * us into trouble. (mkm 9/25/83)
579180025Sdfr		 */
580180025Sdfr		size -= dev_bsize;
581180025Sdfr		goto loop;
582180025Sdfr	}
583177633Sdfr	if (cnt == -1)
584177633Sdfr		msg("read error from %s: %s: [block %d]: count=%d\n",
585177633Sdfr			disk, strerror(errno), blkno, size);
586177633Sdfr	else
587177633Sdfr		msg("short read error from %s: [block %d]: count=%d, got=%d\n",
588177633Sdfr			disk, blkno, size, cnt);
589177633Sdfr	if (++breaderrors > BREADEMAX) {
590177633Sdfr		msg("More than %d block read errors from %d\n",
591177633Sdfr			BREADEMAX, disk);
592177633Sdfr		broadcast("DUMP IS AILING!\n");
593177633Sdfr		msg("This is an unrecoverable error.\n");
594177633Sdfr		if (!query("Do you want to attempt to continue?")){
595180025Sdfr			dumpabort(0);
596180025Sdfr			/*NOTREACHED*/
597180025Sdfr		} else
598180025Sdfr			breaderrors = 0;
599180025Sdfr	}
600180025Sdfr	/*
601177633Sdfr	 * Zero buffer, then try to read each sector of buffer separately.
602177633Sdfr	 */
603177633Sdfr	memset(buf, 0, size);
604177633Sdfr	for (i = 0; i < size; i += dev_bsize, buf += dev_bsize, blkno++) {
605177633Sdfr		if ((cnt = pread(diskfd, buf, (int)dev_bsize,
606177633Sdfr		    ((off_t)blkno << dev_bshift))) == dev_bsize)
607177633Sdfr			continue;
608177633Sdfr		if (cnt == -1) {
609177633Sdfr			msg("read error from %s: %s: [sector %d]: count=%d\n",
610177633Sdfr				disk, strerror(errno), blkno, dev_bsize);
611177633Sdfr			continue;
612177633Sdfr		}
613180025Sdfr		msg("short read error from %s: [sector %d]: count=%d, got=%d\n",
614182153Sdfr			disk, blkno, dev_bsize, cnt);
615180025Sdfr	}
616180025Sdfr}
617180025Sdfr