xref: /csrg-svn/sbin/dump/traverse.c (revision 69902)
147082Smckusick /*-
261484Sbostic  * Copyright (c) 1980, 1988, 1991, 1993
361484Sbostic  *	The Regents of the University of California.  All rights reserved.
447082Smckusick  *
547082Smckusick  * %sccs.include.redist.c%
622041Sdist  */
75329Smckusic 
822041Sdist #ifndef lint
9*69902Smckusick static char sccsid[] = "@(#)traverse.c	8.7 (Berkeley) 06/15/95";
1046584Storek #endif /* not lint */
1122041Sdist 
1257723Smckusick #include <sys/param.h>
1357723Smckusick #include <sys/time.h>
1457723Smckusick #include <sys/stat.h>
1550499Smckusick #ifdef sunos
1657723Smckusick #include <sys/vnode.h>
1757723Smckusick 
1851605Sbostic #include <ufs/fs.h>
1957723Smckusick #include <ufs/fsdir.h>
2057723Smckusick #include <ufs/inode.h>
2150499Smckusick #else
2251605Sbostic #include <ufs/ufs/dir.h>
2351605Sbostic #include <ufs/ufs/dinode.h>
2469158Smckusick #include <ufs/ffs/fs.h>
2557723Smckusick #endif
2657723Smckusick 
2746795Sbostic #include <protocols/dumprestore.h>
2857723Smckusick 
2957723Smckusick #include <ctype.h>
3057723Smckusick #include <stdio.h>
3146795Sbostic #ifdef __STDC__
3257723Smckusick #include <string.h>
3346795Sbostic #include <unistd.h>
3446795Sbostic #endif
3557723Smckusick 
361426Sroot #include "dump.h"
371426Sroot 
3846792Smckusick #define	HASDUMPEDFILE	0x1
3946792Smckusick #define	HASSUBDIRS	0x2
4046584Storek 
4159925Storek #ifdef	FS_44INODEFMT
4259925Storek typedef	quad_t fsizeT;
4359925Storek #else
4459925Storek typedef	long fsizeT;
4559925Storek #endif
4659925Storek 
4757723Smckusick static	int dirindir __P((ino_t ino, daddr_t blkno, int level, long *size));
4859925Storek static	void dmpindir __P((ino_t ino, daddr_t blk, int level, fsizeT *size));
4957723Smckusick static	int searchdir __P((ino_t ino, daddr_t blkno, long size, long filesize));
5057723Smckusick 
5146584Storek /*
5246584Storek  * This is an estimation of the number of TP_BSIZE blocks in the file.
5346584Storek  * It estimates the number of blocks in files with holes by assuming
5446584Storek  * that all of the blocks accounted for by di_blocks are data blocks
5546584Storek  * (when some of the blocks are usually used for indirect pointers);
5646584Storek  * hence the estimate may be high.
5746584Storek  */
5846792Smckusick long
blockest(dp)5947058Smckusick blockest(dp)
6047058Smckusick 	register struct dinode *dp;
6146584Storek {
6246792Smckusick 	long blkest, sizeest;
6346584Storek 
6446584Storek 	/*
6547058Smckusick 	 * dp->di_size is the size of the file in bytes.
6647058Smckusick 	 * dp->di_blocks stores the number of sectors actually in the file.
6746584Storek 	 * If there are more sectors than the size would indicate, this just
6846584Storek 	 *	means that there are indirect blocks in the file or unused
6946584Storek 	 *	sectors in the last file block; we can safely ignore these
7046792Smckusick 	 *	(blkest = sizeest below).
7146584Storek 	 * If the file is bigger than the number of sectors would indicate,
7246584Storek 	 *	then the file has holes in it.	In this case we must use the
7346584Storek 	 *	block count to estimate the number of data blocks used, but
7446584Storek 	 *	we use the actual size for estimating the number of indirect
7546792Smckusick 	 *	dump blocks (sizeest vs. blkest in the indirect block
7646792Smckusick 	 *	calculation).
7746584Storek 	 */
7847058Smckusick 	blkest = howmany(dbtob(dp->di_blocks), TP_BSIZE);
7947058Smckusick 	sizeest = howmany(dp->di_size, TP_BSIZE);
8046792Smckusick 	if (blkest > sizeest)
8146792Smckusick 		blkest = sizeest;
8247058Smckusick 	if (dp->di_size > sblock->fs_bsize * NDADDR) {
8346584Storek 		/* calculate the number of indirect blocks on the dump tape */
8446792Smckusick 		blkest +=
8546792Smckusick 			howmany(sizeest - NDADDR * sblock->fs_bsize / TP_BSIZE,
8646584Storek 			TP_NINDIR);
8746584Storek 	}
8846792Smckusick 	return (blkest + 1);
8946584Storek }
9046584Storek 
9159925Storek /* Auxiliary macro to pick up files changed since previous dump. */
9259925Storek #define	CHANGEDSINCE(dp, t) \
9359925Storek 	((dp)->di_mtime >= (t) || (dp)->di_ctime >= (t))
9459925Storek 
9559925Storek /* The WANTTODUMP macro decides whether a file should be dumped. */
9659925Storek #ifdef UF_NODUMP
9759925Storek #define	WANTTODUMP(dp) \
9859925Storek 	(CHANGEDSINCE(dp, spcl.c_ddate) && \
9959925Storek 	 (nonodump || ((dp)->di_flags & UF_NODUMP) != UF_NODUMP))
10059925Storek #else
10159925Storek #define	WANTTODUMP(dp) CHANGEDSINCE(dp, spcl.c_ddate)
10259925Storek #endif
10359925Storek 
10446792Smckusick /*
10546792Smckusick  * Dump pass 1.
10646792Smckusick  *
10746792Smckusick  * Walk the inode list for a filesystem to find all allocated inodes
10846792Smckusick  * that have been modified since the previous dump time. Also, find all
10946792Smckusick  * the directories in the filesystem.
11046792Smckusick  */
11157723Smckusick int
mapfiles(maxino,tapesize)11246792Smckusick mapfiles(maxino, tapesize)
11346792Smckusick 	ino_t maxino;
11446792Smckusick 	long *tapesize;
11546584Storek {
11646792Smckusick 	register int mode;
11746792Smckusick 	register ino_t ino;
11846792Smckusick 	register struct dinode *dp;
11946792Smckusick 	int anydirskipped = 0;
12046584Storek 
12159925Storek 	for (ino = ROOTINO; ino < maxino; ino++) {
12246792Smckusick 		dp = getino(ino);
12346792Smckusick 		if ((mode = (dp->di_mode & IFMT)) == 0)
12446792Smckusick 			continue;
12546792Smckusick 		SETINO(ino, usedinomap);
12646792Smckusick 		if (mode == IFDIR)
12746792Smckusick 			SETINO(ino, dumpdirmap);
12859925Storek 		if (WANTTODUMP(dp)) {
12946792Smckusick 			SETINO(ino, dumpinomap);
13059925Storek 			if (mode != IFREG && mode != IFDIR && mode != IFLNK)
13146792Smckusick 				*tapesize += 1;
13259925Storek 			else
13359925Storek 				*tapesize += blockest(dp);
13446792Smckusick 			continue;
13546792Smckusick 		}
13646792Smckusick 		if (mode == IFDIR)
13746792Smckusick 			anydirskipped = 1;
13846792Smckusick 	}
13946792Smckusick 	/*
14046792Smckusick 	 * Restore gets very upset if the root is not dumped,
14146792Smckusick 	 * so ensure that it always is dumped.
14246792Smckusick 	 */
14351374Smckusick 	SETINO(ROOTINO, dumpinomap);
14446792Smckusick 	return (anydirskipped);
14546584Storek }
14646584Storek 
14746792Smckusick /*
14846792Smckusick  * Dump pass 2.
14946792Smckusick  *
15046792Smckusick  * Scan each directory on the filesystem to see if it has any modified
15146792Smckusick  * files in it. If it does, and has not already been added to the dump
15246792Smckusick  * list (because it was itself modified), then add it. If a directory
15346792Smckusick  * has not been modified itself, contains no modified files and has no
15446792Smckusick  * subdirectories, then it can be deleted from the dump list and from
15546792Smckusick  * the list of directories. By deleting it from the list of directories,
15646792Smckusick  * its parent may now qualify for the same treatment on this or a later
15746792Smckusick  * pass using this algorithm.
15846792Smckusick  */
15957723Smckusick int
mapdirs(maxino,tapesize)16046792Smckusick mapdirs(maxino, tapesize)
16146792Smckusick 	ino_t maxino;
16246792Smckusick 	long *tapesize;
16346792Smckusick {
16446792Smckusick 	register struct	dinode *dp;
16555390Smckusick 	register int i, isdir;
16625797Smckusick 	register char *map;
16746792Smckusick 	register ino_t ino;
16854070Smckusick 	long filesize;
16946792Smckusick 	int ret, change = 0;
1701426Sroot 
17159925Storek 	isdir = 0;		/* XXX just to get gcc to shut up */
17259925Storek 	for (map = dumpdirmap, ino = 1; ino < maxino; ino++) {
17359925Storek 		if (((ino - 1) % NBBY) == 0)	/* map is offset by 1 */
17455390Smckusick 			isdir = *map++;
17546792Smckusick 		else
17655390Smckusick 			isdir >>= 1;
17755390Smckusick 		if ((isdir & 1) == 0 || TSTINO(ino, dumpinomap))
17846792Smckusick 			continue;
17946792Smckusick 		dp = getino(ino);
18046792Smckusick 		filesize = dp->di_size;
18146792Smckusick 		for (ret = 0, i = 0; filesize > 0 && i < NDADDR; i++) {
18246792Smckusick 			if (dp->di_db[i] != 0)
18346792Smckusick 				ret |= searchdir(ino, dp->di_db[i],
18455390Smckusick 					(long)dblksize(sblock, dp, i),
18555390Smckusick 					filesize);
18646792Smckusick 			if (ret & HASDUMPEDFILE)
18746792Smckusick 				filesize = 0;
18846792Smckusick 			else
18946792Smckusick 				filesize -= sblock->fs_bsize;
19046792Smckusick 		}
19146792Smckusick 		for (i = 0; filesize > 0 && i < NIADDR; i++) {
19246792Smckusick 			if (dp->di_ib[i] == 0)
19346792Smckusick 				continue;
19446792Smckusick 			ret |= dirindir(ino, dp->di_ib[i], i, &filesize);
19546792Smckusick 		}
19646792Smckusick 		if (ret & HASDUMPEDFILE) {
19755390Smckusick 			SETINO(ino, dumpinomap);
19855390Smckusick 			*tapesize += blockest(dp);
19946792Smckusick 			change = 1;
20046792Smckusick 			continue;
20146792Smckusick 		}
20246792Smckusick 		if ((ret & HASSUBDIRS) == 0) {
20346792Smckusick 			if (!TSTINO(ino, dumpinomap)) {
20446792Smckusick 				CLRINO(ino, dumpdirmap);
20546792Smckusick 				change = 1;
20646792Smckusick 			}
20746792Smckusick 		}
2081426Sroot 	}
20946792Smckusick 	return (change);
2101426Sroot }
2111426Sroot 
21246792Smckusick /*
21346792Smckusick  * Read indirect blocks, and pass the data blocks to be searched
21446792Smckusick  * as directories. Quit as soon as any entry is found that will
21546792Smckusick  * require the directory to be dumped.
21646792Smckusick  */
21757723Smckusick static int
dirindir(ino,blkno,ind_level,filesize)21854070Smckusick dirindir(ino, blkno, ind_level, filesize)
21946792Smckusick 	ino_t ino;
22046792Smckusick 	daddr_t blkno;
22154070Smckusick 	int ind_level;
22254070Smckusick 	long *filesize;
2231426Sroot {
22446792Smckusick 	int ret = 0;
22546792Smckusick 	register int i;
22646792Smckusick 	daddr_t	idblk[MAXNINDIR];
2271426Sroot 
22854070Smckusick 	bread(fsbtodb(sblock, blkno), (char *)idblk, (int)sblock->fs_bsize);
22954070Smckusick 	if (ind_level <= 0) {
23046792Smckusick 		for (i = 0; *filesize > 0 && i < NINDIR(sblock); i++) {
23146792Smckusick 			blkno = idblk[i];
23246792Smckusick 			if (blkno != 0)
23346796Smckusick 				ret |= searchdir(ino, blkno, sblock->fs_bsize,
23454070Smckusick 					*filesize);
23546792Smckusick 			if (ret & HASDUMPEDFILE)
23646792Smckusick 				*filesize = 0;
23746792Smckusick 			else
23846792Smckusick 				*filesize -= sblock->fs_bsize;
2391426Sroot 		}
24046792Smckusick 		return (ret);
2415329Smckusic 	}
24254070Smckusick 	ind_level--;
24346792Smckusick 	for (i = 0; *filesize > 0 && i < NINDIR(sblock); i++) {
24446792Smckusick 		blkno = idblk[i];
24546792Smckusick 		if (blkno != 0)
24654070Smckusick 			ret |= dirindir(ino, blkno, ind_level, filesize);
2475329Smckusic 	}
24846792Smckusick 	return (ret);
2491426Sroot }
2501426Sroot 
25146792Smckusick /*
25246792Smckusick  * Scan a disk block containing directory information looking to see if
25346792Smckusick  * any of the entries are on the dump list and to see if the directory
25446792Smckusick  * contains any subdirectories.
25546792Smckusick  */
25657723Smckusick static int
searchdir(ino,blkno,size,filesize)25746796Smckusick searchdir(ino, blkno, size, filesize)
25846792Smckusick 	ino_t ino;
25946792Smckusick 	daddr_t blkno;
26054070Smckusick 	register long size;
26154070Smckusick 	long filesize;
2625329Smckusic {
26346792Smckusick 	register struct direct *dp;
26455390Smckusick 	register long loc, ret = 0;
26546792Smckusick 	char dblk[MAXBSIZE];
2665329Smckusic 
26754070Smckusick 	bread(fsbtodb(sblock, blkno), dblk, (int)size);
26846796Smckusick 	if (filesize < size)
26946796Smckusick 		size = filesize;
27046792Smckusick 	for (loc = 0; loc < size; ) {
27146792Smckusick 		dp = (struct direct *)(dblk + loc);
27246792Smckusick 		if (dp->d_reclen == 0) {
27346792Smckusick 			msg("corrupted directory, inumber %d\n", ino);
27446792Smckusick 			break;
2755329Smckusic 		}
27646792Smckusick 		loc += dp->d_reclen;
27746792Smckusick 		if (dp->d_ino == 0)
27846792Smckusick 			continue;
27946792Smckusick 		if (dp->d_name[0] == '.') {
28046792Smckusick 			if (dp->d_name[1] == '\0')
28146792Smckusick 				continue;
28246792Smckusick 			if (dp->d_name[1] == '.' && dp->d_name[2] == '\0')
28346792Smckusick 				continue;
2845329Smckusic 		}
28555390Smckusick 		if (TSTINO(dp->d_ino, dumpinomap)) {
28655390Smckusick 			ret |= HASDUMPEDFILE;
28755390Smckusick 			if (ret & HASSUBDIRS)
28855390Smckusick 				break;
28955390Smckusick 		}
29055390Smckusick 		if (TSTINO(dp->d_ino, dumpdirmap)) {
29155390Smckusick 			ret |= HASSUBDIRS;
29255390Smckusick 			if (ret & HASDUMPEDFILE)
29355390Smckusick 				break;
29455390Smckusick 		}
2955329Smckusic 	}
29655390Smckusick 	return (ret);
2975329Smckusic }
2985329Smckusic 
29946792Smckusick /*
30046792Smckusick  * Dump passes 3 and 4.
30146792Smckusick  *
30246792Smckusick  * Dump the contents of an inode to tape.
30346792Smckusick  */
30446584Storek void
dumpino(dp,ino)30547058Smckusick dumpino(dp, ino)
30647058Smckusick 	register struct dinode *dp;
30746792Smckusick 	ino_t ino;
30817234Smckusick {
30954596Smckusick 	int ind_level, cnt;
31059925Storek 	fsizeT size;
31154596Smckusick 	char buf[TP_BSIZE];
3121426Sroot 
31346792Smckusick 	if (newtape) {
3141426Sroot 		newtape = 0;
31546792Smckusick 		dumpmap(dumpinomap, TS_BITS, ino);
3161426Sroot 	}
31746792Smckusick 	CLRINO(ino, dumpinomap);
31847058Smckusick 	spcl.c_dinode = *dp;
3191426Sroot 	spcl.c_type = TS_INODE;
3201426Sroot 	spcl.c_count = 0;
32157723Smckusick 	switch (dp->di_mode & S_IFMT) {
32254596Smckusick 
32357723Smckusick 	case 0:
32454596Smckusick 		/*
32554596Smckusick 		 * Freed inode.
32654596Smckusick 		 */
32717234Smckusick 		return;
32854596Smckusick 
32957723Smckusick 	case S_IFLNK:
33054596Smckusick 		/*
33154596Smckusick 		 * Check for short symbolic link.
33254596Smckusick 		 */
33357723Smckusick #ifdef FS_44INODEFMT
33454596Smckusick 		if (dp->di_size > 0 &&
33554596Smckusick 		    dp->di_size < sblock->fs_maxsymlinklen) {
33654596Smckusick 			spcl.c_addr[0] = 1;
33754596Smckusick 			spcl.c_count = 1;
33854596Smckusick 			writeheader(ino);
33968991Sbostic 			memmove(buf, dp->di_shortlink, (u_long)dp->di_size);
34054596Smckusick 			buf[dp->di_size] = '\0';
34154596Smckusick 			writerec(buf, 0);
34254596Smckusick 			return;
34354596Smckusick 		}
34457723Smckusick #endif
34554596Smckusick 		/* fall through */
34654596Smckusick 
34757723Smckusick 	case S_IFDIR:
34857723Smckusick 	case S_IFREG:
34954596Smckusick 		if (dp->di_size > 0)
35054596Smckusick 			break;
35154596Smckusick 		/* fall through */
35254596Smckusick 
35357723Smckusick 	case S_IFIFO:
35457723Smckusick 	case S_IFSOCK:
35557723Smckusick 	case S_IFCHR:
35657723Smckusick 	case S_IFBLK:
35746792Smckusick 		writeheader(ino);
3581426Sroot 		return;
35954596Smckusick 
36054596Smckusick 	default:
36154596Smckusick 		msg("Warning: undefined file type 0%o\n", dp->di_mode & IFMT);
36254596Smckusick 		return;
3631426Sroot 	}
36447058Smckusick 	if (dp->di_size > NDADDR * sblock->fs_bsize)
36546792Smckusick 		cnt = NDADDR * sblock->fs_frag;
3664777Smckusic 	else
36747058Smckusick 		cnt = howmany(dp->di_size, sblock->fs_fsize);
36847058Smckusick 	blksout(&dp->di_db[0], cnt, ino);
36947058Smckusick 	if ((size = dp->di_size - NDADDR * sblock->fs_bsize) <= 0)
3704777Smckusic 		return;
37154070Smckusick 	for (ind_level = 0; ind_level < NIADDR; ind_level++) {
37254070Smckusick 		dmpindir(ino, dp->di_ib[ind_level], ind_level, &size);
3734777Smckusic 		if (size <= 0)
3744777Smckusic 			return;
3754777Smckusic 	}
3761426Sroot }
3771426Sroot 
37846792Smckusick /*
37946792Smckusick  * Read indirect blocks, and pass the data blocks to be dumped.
38046792Smckusick  */
38157723Smckusick static void
dmpindir(ino,blk,ind_level,size)38254070Smckusick dmpindir(ino, blk, ind_level, size)
38346792Smckusick 	ino_t ino;
3844777Smckusic 	daddr_t blk;
38554070Smckusick 	int ind_level;
38659925Storek 	fsizeT *size;
3871426Sroot {
3884777Smckusic 	int i, cnt;
3895329Smckusic 	daddr_t idblk[MAXNINDIR];
3901426Sroot 
3914777Smckusic 	if (blk != 0)
39254070Smckusick 		bread(fsbtodb(sblock, blk), (char *)idblk, (int) sblock->fs_bsize);
3934777Smckusic 	else
39468991Sbostic 		memset(idblk, 0, (int)sblock->fs_bsize);
39554070Smckusick 	if (ind_level <= 0) {
3965329Smckusic 		if (*size < NINDIR(sblock) * sblock->fs_bsize)
3975329Smckusic 			cnt = howmany(*size, sblock->fs_fsize);
3984777Smckusic 		else
3995329Smckusic 			cnt = NINDIR(sblock) * sblock->fs_frag;
4005329Smckusic 		*size -= NINDIR(sblock) * sblock->fs_bsize;
40146792Smckusick 		blksout(&idblk[0], cnt, ino);
4024777Smckusic 		return;
4031426Sroot 	}
40454070Smckusick 	ind_level--;
4055329Smckusic 	for (i = 0; i < NINDIR(sblock); i++) {
40654070Smckusick 		dmpindir(ino, idblk[i], ind_level, size);
4074777Smckusic 		if (*size <= 0)
4084777Smckusic 			return;
4094777Smckusic 	}
4101426Sroot }
4111426Sroot 
41246792Smckusick /*
41346792Smckusick  * Collect up the data into tape record sized buffers and output them.
41446792Smckusick  */
41546584Storek void
blksout(blkp,frags,ino)41646792Smckusick blksout(blkp, frags, ino)
4174777Smckusic 	daddr_t *blkp;
4184777Smckusic 	int frags;
41946792Smckusick 	ino_t ino;
4204777Smckusic {
42146584Storek 	register daddr_t *bp;
4225329Smckusic 	int i, j, count, blks, tbperdb;
4234777Smckusic 
4249403Smckusick 	blks = howmany(frags * sblock->fs_fsize, TP_BSIZE);
42546584Storek 	tbperdb = sblock->fs_bsize >> tp_bshift;
4264777Smckusic 	for (i = 0; i < blks; i += TP_NINDIR) {
4274777Smckusic 		if (i + TP_NINDIR > blks)
4284777Smckusic 			count = blks;
4294777Smckusic 		else
4304777Smckusic 			count = i + TP_NINDIR;
4314777Smckusic 		for (j = i; j < count; j++)
4325329Smckusic 			if (blkp[j / tbperdb] != 0)
4334777Smckusic 				spcl.c_addr[j - i] = 1;
4344777Smckusic 			else
4354777Smckusic 				spcl.c_addr[j - i] = 0;
4364777Smckusic 		spcl.c_count = count - i;
43746792Smckusick 		writeheader(ino);
43846584Storek 		bp = &blkp[i / tbperdb];
43946584Storek 		for (j = i; j < count; j += tbperdb, bp++)
44046584Storek 			if (*bp != 0)
4415329Smckusic 				if (j + tbperdb <= count)
44254070Smckusick 					dumpblock(*bp, (int)sblock->fs_bsize);
4434777Smckusic 				else
44446792Smckusick 					dumpblock(*bp, (count - j) * TP_BSIZE);
4454777Smckusic 		spcl.c_type = TS_ADDR;
4464777Smckusic 	}
4474777Smckusic }
4484777Smckusic 
44946792Smckusick /*
45046792Smckusick  * Dump a map to the tape.
45146792Smckusick  */
45246584Storek void
dumpmap(map,type,ino)45346792Smckusick dumpmap(map, type, ino)
4545329Smckusic 	char *map;
45546792Smckusick 	int type;
45646792Smckusick 	ino_t ino;
4571426Sroot {
45846792Smckusick 	register int i;
4591426Sroot 	char *cp;
4601426Sroot 
46146792Smckusick 	spcl.c_type = type;
46246792Smckusick 	spcl.c_count = howmany(mapsize * sizeof(char), TP_BSIZE);
46346792Smckusick 	writeheader(ino);
4645329Smckusic 	for (i = 0, cp = map; i < spcl.c_count; i++, cp += TP_BSIZE)
46554596Smckusick 		writerec(cp, 0);
4661426Sroot }
4671426Sroot 
46846792Smckusick /*
46946792Smckusick  * Write a header record to the dump tape.
47046792Smckusick  */
47146584Storek void
writeheader(ino)47246792Smckusick writeheader(ino)
47346792Smckusick 	ino_t ino;
4741426Sroot {
47546792Smckusick 	register long sum, cnt, *lp;
4761426Sroot 
4771426Sroot 	spcl.c_inumber = ino;
4788368Smckusick 	spcl.c_magic = NFS_MAGIC;
4791426Sroot 	spcl.c_checksum = 0;
48046792Smckusick 	lp = (long *)&spcl;
48146792Smckusick 	sum = 0;
48246792Smckusick 	cnt = sizeof(union u_spcl) / (4 * sizeof(long));
48346792Smckusick 	while (--cnt >= 0) {
48446792Smckusick 		sum += *lp++;
48546792Smckusick 		sum += *lp++;
48646792Smckusick 		sum += *lp++;
48746792Smckusick 		sum += *lp++;
48824168Smckusick 	}
48946792Smckusick 	spcl.c_checksum = CHECKSUM - sum;
49054596Smckusick 	writerec((char *)&spcl, 1);
4911426Sroot }
4921426Sroot 
4934702Smckusic struct dinode *
getino(inum)49446792Smckusick getino(inum)
49546792Smckusick 	ino_t inum;
4964702Smckusic {
4974702Smckusic 	static daddr_t minino, maxino;
49846792Smckusick 	static struct dinode inoblock[MAXINOPB];
4994702Smckusic 
50046792Smckusick 	curino = inum;
50146792Smckusick 	if (inum >= minino && inum < maxino)
50246792Smckusick 		return (&inoblock[inum - minino]);
50364639Sbostic 	bread(fsbtodb(sblock, ino_to_fsba(sblock, inum)), (char *)inoblock,
50454070Smckusick 	    (int)sblock->fs_bsize);
50546792Smckusick 	minino = inum - (inum % INOPB(sblock));
5065329Smckusic 	maxino = minino + INOPB(sblock);
50746792Smckusick 	return (&inoblock[inum - minino]);
5084702Smckusic }
5094702Smckusic 
51046792Smckusick /*
51146792Smckusick  * Read a chunk of data from the disk.
51246792Smckusick  * Try to recover from hard errors by reading in sector sized pieces.
51346792Smckusick  * Error recovery is attempted at most BREADEMAX times before seeking
51446792Smckusick  * consent from the operator to continue.
51546792Smckusick  */
5161426Sroot int	breaderrors = 0;
5171426Sroot #define	BREADEMAX 32
5181426Sroot 
51946584Storek void
bread(blkno,buf,size)52046792Smckusick bread(blkno, buf, size)
52146792Smckusick 	daddr_t blkno;
52246792Smckusick 	char *buf;
52346792Smckusick 	int size;
5241426Sroot {
52546792Smckusick 	int cnt, i;
52634359Skarels 	extern int errno;
5271426Sroot 
52815095Smckusick loop:
529*69902Smckusick 	if ((int)lseek(diskfd, ((off_t)blkno << dev_bshift), 0) == -1)
5301426Sroot 		msg("bread: lseek fails\n");
53146792Smckusick 	if ((cnt = read(diskfd, buf, size)) == size)
53215095Smckusick 		return;
53346792Smckusick 	if (blkno + (size / dev_bsize) > fsbtodb(sblock, sblock->fs_size)) {
53415095Smckusick 		/*
53515095Smckusick 		 * Trying to read the final fragment.
53615095Smckusick 		 *
53715095Smckusick 		 * NB - dump only works in TP_BSIZE blocks, hence
53830560Smckusick 		 * rounds `dev_bsize' fragments up to TP_BSIZE pieces.
53915095Smckusick 		 * It should be smarter about not actually trying to
54015095Smckusick 		 * read more than it can get, but for the time being
54115095Smckusick 		 * we punt and scale back the read only when it gets
54215095Smckusick 		 * us into trouble. (mkm 9/25/83)
54315095Smckusick 		 */
54446792Smckusick 		size -= dev_bsize;
54515095Smckusick 		goto loop;
5461426Sroot 	}
54750905Smckusick 	if (cnt == -1)
54850905Smckusick 		msg("read error from %s: %s: [block %d]: count=%d\n",
54950905Smckusick 			disk, strerror(errno), blkno, size);
55050905Smckusick 	else
55150905Smckusick 		msg("short read error from %s: [block %d]: count=%d, got=%d\n",
55250905Smckusick 			disk, blkno, size, cnt);
55346584Storek 	if (++breaderrors > BREADEMAX) {
55415095Smckusick 		msg("More than %d block read errors from %d\n",
55515095Smckusick 			BREADEMAX, disk);
55615095Smckusick 		broadcast("DUMP IS AILING!\n");
55715095Smckusick 		msg("This is an unrecoverable error.\n");
55815095Smckusick 		if (!query("Do you want to attempt to continue?")){
55955288Sbostic 			dumpabort(0);
56015095Smckusick 			/*NOTREACHED*/
56115095Smckusick 		} else
56215095Smckusick 			breaderrors = 0;
56315095Smckusick 	}
56434359Skarels 	/*
56534359Skarels 	 * Zero buffer, then try to read each sector of buffer separately.
56634359Skarels 	 */
56768991Sbostic 	memset(buf, 0, size);
56846792Smckusick 	for (i = 0; i < size; i += dev_bsize, buf += dev_bsize, blkno++) {
569*69902Smckusick 		if ((int)lseek(diskfd, ((off_t)blkno << dev_bshift), 0) == -1)
57034359Skarels 			msg("bread: lseek2 fails!\n");
57154070Smckusick 		if ((cnt = read(diskfd, buf, (int)dev_bsize)) == dev_bsize)
57250905Smckusick 			continue;
57350905Smckusick 		if (cnt == -1) {
57450905Smckusick 			msg("read error from %s: %s: [sector %d]: count=%d\n",
57550905Smckusick 				disk, strerror(errno), blkno, dev_bsize);
57650905Smckusick 			continue;
57750905Smckusick 		}
57850905Smckusick 		msg("short read error from %s: [sector %d]: count=%d, got=%d\n",
57950905Smckusick 			disk, blkno, dev_bsize, cnt);
58034359Skarels 	}
5811426Sroot }
582