xref: /csrg-svn/sbin/dump/traverse.c (revision 68991)
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*68991Sbostic static char sccsid[] = "@(#)traverse.c	8.4 (Berkeley) 04/28/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
2254070Smckusick #include <ufs/ffs/fs.h>
2351605Sbostic #include <ufs/ufs/dir.h>
2451605Sbostic #include <ufs/ufs/dinode.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
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 #ifdef FS_44INODEFMT
9359925Storek #define	CHANGEDSINCE(dp, t) \
9459925Storek 	((dp)->di_mtime.ts_sec >= (t) || (dp)->di_ctime.ts_sec >= (t))
9559925Storek #else
9659925Storek #define	CHANGEDSINCE(dp, t) \
9759925Storek 	((dp)->di_mtime >= (t) || (dp)->di_ctime >= (t))
9859925Storek #endif
9959925Storek 
10059925Storek /* The WANTTODUMP macro decides whether a file should be dumped. */
10159925Storek #ifdef UF_NODUMP
10259925Storek #define	WANTTODUMP(dp) \
10359925Storek 	(CHANGEDSINCE(dp, spcl.c_ddate) && \
10459925Storek 	 (nonodump || ((dp)->di_flags & UF_NODUMP) != UF_NODUMP))
10559925Storek #else
10659925Storek #define	WANTTODUMP(dp) CHANGEDSINCE(dp, spcl.c_ddate)
10759925Storek #endif
10859925Storek 
10946792Smckusick /*
11046792Smckusick  * Dump pass 1.
11146792Smckusick  *
11246792Smckusick  * Walk the inode list for a filesystem to find all allocated inodes
11346792Smckusick  * that have been modified since the previous dump time. Also, find all
11446792Smckusick  * the directories in the filesystem.
11546792Smckusick  */
11657723Smckusick int
11746792Smckusick mapfiles(maxino, tapesize)
11846792Smckusick 	ino_t maxino;
11946792Smckusick 	long *tapesize;
12046584Storek {
12146792Smckusick 	register int mode;
12246792Smckusick 	register ino_t ino;
12346792Smckusick 	register struct dinode *dp;
12446792Smckusick 	int anydirskipped = 0;
12546584Storek 
12659925Storek 	for (ino = ROOTINO; ino < maxino; ino++) {
12746792Smckusick 		dp = getino(ino);
12846792Smckusick 		if ((mode = (dp->di_mode & IFMT)) == 0)
12946792Smckusick 			continue;
13046792Smckusick 		SETINO(ino, usedinomap);
13146792Smckusick 		if (mode == IFDIR)
13246792Smckusick 			SETINO(ino, dumpdirmap);
13359925Storek 		if (WANTTODUMP(dp)) {
13446792Smckusick 			SETINO(ino, dumpinomap);
13559925Storek 			if (mode != IFREG && mode != IFDIR && mode != IFLNK)
13646792Smckusick 				*tapesize += 1;
13759925Storek 			else
13859925Storek 				*tapesize += blockest(dp);
13946792Smckusick 			continue;
14046792Smckusick 		}
14146792Smckusick 		if (mode == IFDIR)
14246792Smckusick 			anydirskipped = 1;
14346792Smckusick 	}
14446792Smckusick 	/*
14546792Smckusick 	 * Restore gets very upset if the root is not dumped,
14646792Smckusick 	 * so ensure that it always is dumped.
14746792Smckusick 	 */
14851374Smckusick 	SETINO(ROOTINO, dumpinomap);
14946792Smckusick 	return (anydirskipped);
15046584Storek }
15146584Storek 
15246792Smckusick /*
15346792Smckusick  * Dump pass 2.
15446792Smckusick  *
15546792Smckusick  * Scan each directory on the filesystem to see if it has any modified
15646792Smckusick  * files in it. If it does, and has not already been added to the dump
15746792Smckusick  * list (because it was itself modified), then add it. If a directory
15846792Smckusick  * has not been modified itself, contains no modified files and has no
15946792Smckusick  * subdirectories, then it can be deleted from the dump list and from
16046792Smckusick  * the list of directories. By deleting it from the list of directories,
16146792Smckusick  * its parent may now qualify for the same treatment on this or a later
16246792Smckusick  * pass using this algorithm.
16346792Smckusick  */
16457723Smckusick int
16546792Smckusick mapdirs(maxino, tapesize)
16646792Smckusick 	ino_t maxino;
16746792Smckusick 	long *tapesize;
16846792Smckusick {
16946792Smckusick 	register struct	dinode *dp;
17055390Smckusick 	register int i, isdir;
17125797Smckusick 	register char *map;
17246792Smckusick 	register ino_t ino;
17354070Smckusick 	long filesize;
17446792Smckusick 	int ret, change = 0;
1751426Sroot 
17659925Storek 	isdir = 0;		/* XXX just to get gcc to shut up */
17759925Storek 	for (map = dumpdirmap, ino = 1; ino < maxino; ino++) {
17859925Storek 		if (((ino - 1) % NBBY) == 0)	/* map is offset by 1 */
17955390Smckusick 			isdir = *map++;
18046792Smckusick 		else
18155390Smckusick 			isdir >>= 1;
18255390Smckusick 		if ((isdir & 1) == 0 || TSTINO(ino, dumpinomap))
18346792Smckusick 			continue;
18446792Smckusick 		dp = getino(ino);
18546792Smckusick 		filesize = dp->di_size;
18646792Smckusick 		for (ret = 0, i = 0; filesize > 0 && i < NDADDR; i++) {
18746792Smckusick 			if (dp->di_db[i] != 0)
18846792Smckusick 				ret |= searchdir(ino, dp->di_db[i],
18955390Smckusick 					(long)dblksize(sblock, dp, i),
19055390Smckusick 					filesize);
19146792Smckusick 			if (ret & HASDUMPEDFILE)
19246792Smckusick 				filesize = 0;
19346792Smckusick 			else
19446792Smckusick 				filesize -= sblock->fs_bsize;
19546792Smckusick 		}
19646792Smckusick 		for (i = 0; filesize > 0 && i < NIADDR; i++) {
19746792Smckusick 			if (dp->di_ib[i] == 0)
19846792Smckusick 				continue;
19946792Smckusick 			ret |= dirindir(ino, dp->di_ib[i], i, &filesize);
20046792Smckusick 		}
20146792Smckusick 		if (ret & HASDUMPEDFILE) {
20255390Smckusick 			SETINO(ino, dumpinomap);
20355390Smckusick 			*tapesize += blockest(dp);
20446792Smckusick 			change = 1;
20546792Smckusick 			continue;
20646792Smckusick 		}
20746792Smckusick 		if ((ret & HASSUBDIRS) == 0) {
20846792Smckusick 			if (!TSTINO(ino, dumpinomap)) {
20946792Smckusick 				CLRINO(ino, dumpdirmap);
21046792Smckusick 				change = 1;
21146792Smckusick 			}
21246792Smckusick 		}
2131426Sroot 	}
21446792Smckusick 	return (change);
2151426Sroot }
2161426Sroot 
21746792Smckusick /*
21846792Smckusick  * Read indirect blocks, and pass the data blocks to be searched
21946792Smckusick  * as directories. Quit as soon as any entry is found that will
22046792Smckusick  * require the directory to be dumped.
22146792Smckusick  */
22257723Smckusick static int
22354070Smckusick dirindir(ino, blkno, ind_level, filesize)
22446792Smckusick 	ino_t ino;
22546792Smckusick 	daddr_t blkno;
22654070Smckusick 	int ind_level;
22754070Smckusick 	long *filesize;
2281426Sroot {
22946792Smckusick 	int ret = 0;
23046792Smckusick 	register int i;
23146792Smckusick 	daddr_t	idblk[MAXNINDIR];
2321426Sroot 
23354070Smckusick 	bread(fsbtodb(sblock, blkno), (char *)idblk, (int)sblock->fs_bsize);
23454070Smckusick 	if (ind_level <= 0) {
23546792Smckusick 		for (i = 0; *filesize > 0 && i < NINDIR(sblock); i++) {
23646792Smckusick 			blkno = idblk[i];
23746792Smckusick 			if (blkno != 0)
23846796Smckusick 				ret |= searchdir(ino, blkno, sblock->fs_bsize,
23954070Smckusick 					*filesize);
24046792Smckusick 			if (ret & HASDUMPEDFILE)
24146792Smckusick 				*filesize = 0;
24246792Smckusick 			else
24346792Smckusick 				*filesize -= sblock->fs_bsize;
2441426Sroot 		}
24546792Smckusick 		return (ret);
2465329Smckusic 	}
24754070Smckusick 	ind_level--;
24846792Smckusick 	for (i = 0; *filesize > 0 && i < NINDIR(sblock); i++) {
24946792Smckusick 		blkno = idblk[i];
25046792Smckusick 		if (blkno != 0)
25154070Smckusick 			ret |= dirindir(ino, blkno, ind_level, filesize);
2525329Smckusic 	}
25346792Smckusick 	return (ret);
2541426Sroot }
2551426Sroot 
25646792Smckusick /*
25746792Smckusick  * Scan a disk block containing directory information looking to see if
25846792Smckusick  * any of the entries are on the dump list and to see if the directory
25946792Smckusick  * contains any subdirectories.
26046792Smckusick  */
26157723Smckusick static int
26246796Smckusick searchdir(ino, blkno, size, filesize)
26346792Smckusick 	ino_t ino;
26446792Smckusick 	daddr_t blkno;
26554070Smckusick 	register long size;
26654070Smckusick 	long filesize;
2675329Smckusic {
26846792Smckusick 	register struct direct *dp;
26955390Smckusick 	register long loc, ret = 0;
27046792Smckusick 	char dblk[MAXBSIZE];
2715329Smckusic 
27254070Smckusick 	bread(fsbtodb(sblock, blkno), dblk, (int)size);
27346796Smckusick 	if (filesize < size)
27446796Smckusick 		size = filesize;
27546792Smckusick 	for (loc = 0; loc < size; ) {
27646792Smckusick 		dp = (struct direct *)(dblk + loc);
27746792Smckusick 		if (dp->d_reclen == 0) {
27846792Smckusick 			msg("corrupted directory, inumber %d\n", ino);
27946792Smckusick 			break;
2805329Smckusic 		}
28146792Smckusick 		loc += dp->d_reclen;
28246792Smckusick 		if (dp->d_ino == 0)
28346792Smckusick 			continue;
28446792Smckusick 		if (dp->d_name[0] == '.') {
28546792Smckusick 			if (dp->d_name[1] == '\0')
28646792Smckusick 				continue;
28746792Smckusick 			if (dp->d_name[1] == '.' && dp->d_name[2] == '\0')
28846792Smckusick 				continue;
2895329Smckusic 		}
29055390Smckusick 		if (TSTINO(dp->d_ino, dumpinomap)) {
29155390Smckusick 			ret |= HASDUMPEDFILE;
29255390Smckusick 			if (ret & HASSUBDIRS)
29355390Smckusick 				break;
29455390Smckusick 		}
29555390Smckusick 		if (TSTINO(dp->d_ino, dumpdirmap)) {
29655390Smckusick 			ret |= HASSUBDIRS;
29755390Smckusick 			if (ret & HASDUMPEDFILE)
29855390Smckusick 				break;
29955390Smckusick 		}
3005329Smckusic 	}
30155390Smckusick 	return (ret);
3025329Smckusic }
3035329Smckusic 
30446792Smckusick /*
30546792Smckusick  * Dump passes 3 and 4.
30646792Smckusick  *
30746792Smckusick  * Dump the contents of an inode to tape.
30846792Smckusick  */
30946584Storek void
31047058Smckusick dumpino(dp, ino)
31147058Smckusick 	register struct dinode *dp;
31246792Smckusick 	ino_t ino;
31317234Smckusick {
31454596Smckusick 	int ind_level, cnt;
31559925Storek 	fsizeT size;
31654596Smckusick 	char buf[TP_BSIZE];
3171426Sroot 
31846792Smckusick 	if (newtape) {
3191426Sroot 		newtape = 0;
32046792Smckusick 		dumpmap(dumpinomap, TS_BITS, ino);
3211426Sroot 	}
32246792Smckusick 	CLRINO(ino, dumpinomap);
32347058Smckusick 	spcl.c_dinode = *dp;
3241426Sroot 	spcl.c_type = TS_INODE;
3251426Sroot 	spcl.c_count = 0;
32657723Smckusick 	switch (dp->di_mode & S_IFMT) {
32754596Smckusick 
32857723Smckusick 	case 0:
32954596Smckusick 		/*
33054596Smckusick 		 * Freed inode.
33154596Smckusick 		 */
33217234Smckusick 		return;
33354596Smckusick 
33457723Smckusick 	case S_IFLNK:
33554596Smckusick 		/*
33654596Smckusick 		 * Check for short symbolic link.
33754596Smckusick 		 */
33857723Smckusick #ifdef FS_44INODEFMT
33954596Smckusick 		if (dp->di_size > 0 &&
34054596Smckusick 		    dp->di_size < sblock->fs_maxsymlinklen) {
34154596Smckusick 			spcl.c_addr[0] = 1;
34254596Smckusick 			spcl.c_count = 1;
34354596Smckusick 			writeheader(ino);
344*68991Sbostic 			memmove(buf, dp->di_shortlink, (u_long)dp->di_size);
34554596Smckusick 			buf[dp->di_size] = '\0';
34654596Smckusick 			writerec(buf, 0);
34754596Smckusick 			return;
34854596Smckusick 		}
34957723Smckusick #endif
35054596Smckusick 		/* fall through */
35154596Smckusick 
35257723Smckusick 	case S_IFDIR:
35357723Smckusick 	case S_IFREG:
35454596Smckusick 		if (dp->di_size > 0)
35554596Smckusick 			break;
35654596Smckusick 		/* fall through */
35754596Smckusick 
35857723Smckusick 	case S_IFIFO:
35957723Smckusick 	case S_IFSOCK:
36057723Smckusick 	case S_IFCHR:
36157723Smckusick 	case S_IFBLK:
36246792Smckusick 		writeheader(ino);
3631426Sroot 		return;
36454596Smckusick 
36554596Smckusick 	default:
36654596Smckusick 		msg("Warning: undefined file type 0%o\n", dp->di_mode & IFMT);
36754596Smckusick 		return;
3681426Sroot 	}
36947058Smckusick 	if (dp->di_size > NDADDR * sblock->fs_bsize)
37046792Smckusick 		cnt = NDADDR * sblock->fs_frag;
3714777Smckusic 	else
37247058Smckusick 		cnt = howmany(dp->di_size, sblock->fs_fsize);
37347058Smckusick 	blksout(&dp->di_db[0], cnt, ino);
37447058Smckusick 	if ((size = dp->di_size - NDADDR * sblock->fs_bsize) <= 0)
3754777Smckusic 		return;
37654070Smckusick 	for (ind_level = 0; ind_level < NIADDR; ind_level++) {
37754070Smckusick 		dmpindir(ino, dp->di_ib[ind_level], ind_level, &size);
3784777Smckusic 		if (size <= 0)
3794777Smckusic 			return;
3804777Smckusic 	}
3811426Sroot }
3821426Sroot 
38346792Smckusick /*
38446792Smckusick  * Read indirect blocks, and pass the data blocks to be dumped.
38546792Smckusick  */
38657723Smckusick static void
38754070Smckusick dmpindir(ino, blk, ind_level, size)
38846792Smckusick 	ino_t ino;
3894777Smckusic 	daddr_t blk;
39054070Smckusick 	int ind_level;
39159925Storek 	fsizeT *size;
3921426Sroot {
3934777Smckusic 	int i, cnt;
3945329Smckusic 	daddr_t idblk[MAXNINDIR];
3951426Sroot 
3964777Smckusic 	if (blk != 0)
39754070Smckusick 		bread(fsbtodb(sblock, blk), (char *)idblk, (int) sblock->fs_bsize);
3984777Smckusic 	else
399*68991Sbostic 		memset(idblk, 0, (int)sblock->fs_bsize);
40054070Smckusick 	if (ind_level <= 0) {
4015329Smckusic 		if (*size < NINDIR(sblock) * sblock->fs_bsize)
4025329Smckusic 			cnt = howmany(*size, sblock->fs_fsize);
4034777Smckusic 		else
4045329Smckusic 			cnt = NINDIR(sblock) * sblock->fs_frag;
4055329Smckusic 		*size -= NINDIR(sblock) * sblock->fs_bsize;
40646792Smckusick 		blksout(&idblk[0], cnt, ino);
4074777Smckusic 		return;
4081426Sroot 	}
40954070Smckusick 	ind_level--;
4105329Smckusic 	for (i = 0; i < NINDIR(sblock); i++) {
41154070Smckusick 		dmpindir(ino, idblk[i], ind_level, size);
4124777Smckusic 		if (*size <= 0)
4134777Smckusic 			return;
4144777Smckusic 	}
4151426Sroot }
4161426Sroot 
41746792Smckusick /*
41846792Smckusick  * Collect up the data into tape record sized buffers and output them.
41946792Smckusick  */
42046584Storek void
42146792Smckusick blksout(blkp, frags, ino)
4224777Smckusic 	daddr_t *blkp;
4234777Smckusic 	int frags;
42446792Smckusick 	ino_t ino;
4254777Smckusic {
42646584Storek 	register daddr_t *bp;
4275329Smckusic 	int i, j, count, blks, tbperdb;
4284777Smckusic 
4299403Smckusick 	blks = howmany(frags * sblock->fs_fsize, TP_BSIZE);
43046584Storek 	tbperdb = sblock->fs_bsize >> tp_bshift;
4314777Smckusic 	for (i = 0; i < blks; i += TP_NINDIR) {
4324777Smckusic 		if (i + TP_NINDIR > blks)
4334777Smckusic 			count = blks;
4344777Smckusic 		else
4354777Smckusic 			count = i + TP_NINDIR;
4364777Smckusic 		for (j = i; j < count; j++)
4375329Smckusic 			if (blkp[j / tbperdb] != 0)
4384777Smckusic 				spcl.c_addr[j - i] = 1;
4394777Smckusic 			else
4404777Smckusic 				spcl.c_addr[j - i] = 0;
4414777Smckusic 		spcl.c_count = count - i;
44246792Smckusick 		writeheader(ino);
44346584Storek 		bp = &blkp[i / tbperdb];
44446584Storek 		for (j = i; j < count; j += tbperdb, bp++)
44546584Storek 			if (*bp != 0)
4465329Smckusic 				if (j + tbperdb <= count)
44754070Smckusick 					dumpblock(*bp, (int)sblock->fs_bsize);
4484777Smckusic 				else
44946792Smckusick 					dumpblock(*bp, (count - j) * TP_BSIZE);
4504777Smckusic 		spcl.c_type = TS_ADDR;
4514777Smckusic 	}
4524777Smckusic }
4534777Smckusic 
45446792Smckusick /*
45546792Smckusick  * Dump a map to the tape.
45646792Smckusick  */
45746584Storek void
45846792Smckusick dumpmap(map, type, ino)
4595329Smckusic 	char *map;
46046792Smckusick 	int type;
46146792Smckusick 	ino_t ino;
4621426Sroot {
46346792Smckusick 	register int i;
4641426Sroot 	char *cp;
4651426Sroot 
46646792Smckusick 	spcl.c_type = type;
46746792Smckusick 	spcl.c_count = howmany(mapsize * sizeof(char), TP_BSIZE);
46846792Smckusick 	writeheader(ino);
4695329Smckusic 	for (i = 0, cp = map; i < spcl.c_count; i++, cp += TP_BSIZE)
47054596Smckusick 		writerec(cp, 0);
4711426Sroot }
4721426Sroot 
47346792Smckusick /*
47446792Smckusick  * Write a header record to the dump tape.
47546792Smckusick  */
47646584Storek void
47746792Smckusick writeheader(ino)
47846792Smckusick 	ino_t ino;
4791426Sroot {
48046792Smckusick 	register long sum, cnt, *lp;
4811426Sroot 
4821426Sroot 	spcl.c_inumber = ino;
4838368Smckusick 	spcl.c_magic = NFS_MAGIC;
4841426Sroot 	spcl.c_checksum = 0;
48546792Smckusick 	lp = (long *)&spcl;
48646792Smckusick 	sum = 0;
48746792Smckusick 	cnt = sizeof(union u_spcl) / (4 * sizeof(long));
48846792Smckusick 	while (--cnt >= 0) {
48946792Smckusick 		sum += *lp++;
49046792Smckusick 		sum += *lp++;
49146792Smckusick 		sum += *lp++;
49246792Smckusick 		sum += *lp++;
49324168Smckusick 	}
49446792Smckusick 	spcl.c_checksum = CHECKSUM - sum;
49554596Smckusick 	writerec((char *)&spcl, 1);
4961426Sroot }
4971426Sroot 
4984702Smckusic struct dinode *
49946792Smckusick getino(inum)
50046792Smckusick 	ino_t inum;
5014702Smckusic {
5024702Smckusic 	static daddr_t minino, maxino;
50346792Smckusick 	static struct dinode inoblock[MAXINOPB];
5044702Smckusic 
50546792Smckusick 	curino = inum;
50646792Smckusick 	if (inum >= minino && inum < maxino)
50746792Smckusick 		return (&inoblock[inum - minino]);
50864639Sbostic 	bread(fsbtodb(sblock, ino_to_fsba(sblock, inum)), (char *)inoblock,
50954070Smckusick 	    (int)sblock->fs_bsize);
51046792Smckusick 	minino = inum - (inum % INOPB(sblock));
5115329Smckusic 	maxino = minino + INOPB(sblock);
51246792Smckusick 	return (&inoblock[inum - minino]);
5134702Smckusic }
5144702Smckusic 
51546792Smckusick /*
51646792Smckusick  * Read a chunk of data from the disk.
51746792Smckusick  * Try to recover from hard errors by reading in sector sized pieces.
51846792Smckusick  * Error recovery is attempted at most BREADEMAX times before seeking
51946792Smckusick  * consent from the operator to continue.
52046792Smckusick  */
5211426Sroot int	breaderrors = 0;
5221426Sroot #define	BREADEMAX 32
5231426Sroot 
52446584Storek void
52546792Smckusick bread(blkno, buf, size)
52646792Smckusick 	daddr_t blkno;
52746792Smckusick 	char *buf;
52846792Smckusick 	int size;
5291426Sroot {
53046792Smckusick 	int cnt, i;
53134359Skarels 	extern int errno;
5321426Sroot 
53315095Smckusick loop:
53468607Smckusick 	if (lseek(diskfd, ((off_t)blkno << dev_bshift), 0) < 0)
5351426Sroot 		msg("bread: lseek fails\n");
53646792Smckusick 	if ((cnt = read(diskfd, buf, size)) == size)
53715095Smckusick 		return;
53846792Smckusick 	if (blkno + (size / dev_bsize) > fsbtodb(sblock, sblock->fs_size)) {
53915095Smckusick 		/*
54015095Smckusick 		 * Trying to read the final fragment.
54115095Smckusick 		 *
54215095Smckusick 		 * NB - dump only works in TP_BSIZE blocks, hence
54330560Smckusick 		 * rounds `dev_bsize' fragments up to TP_BSIZE pieces.
54415095Smckusick 		 * It should be smarter about not actually trying to
54515095Smckusick 		 * read more than it can get, but for the time being
54615095Smckusick 		 * we punt and scale back the read only when it gets
54715095Smckusick 		 * us into trouble. (mkm 9/25/83)
54815095Smckusick 		 */
54946792Smckusick 		size -= dev_bsize;
55015095Smckusick 		goto loop;
5511426Sroot 	}
55250905Smckusick 	if (cnt == -1)
55350905Smckusick 		msg("read error from %s: %s: [block %d]: count=%d\n",
55450905Smckusick 			disk, strerror(errno), blkno, size);
55550905Smckusick 	else
55650905Smckusick 		msg("short read error from %s: [block %d]: count=%d, got=%d\n",
55750905Smckusick 			disk, blkno, size, cnt);
55846584Storek 	if (++breaderrors > BREADEMAX) {
55915095Smckusick 		msg("More than %d block read errors from %d\n",
56015095Smckusick 			BREADEMAX, disk);
56115095Smckusick 		broadcast("DUMP IS AILING!\n");
56215095Smckusick 		msg("This is an unrecoverable error.\n");
56315095Smckusick 		if (!query("Do you want to attempt to continue?")){
56455288Sbostic 			dumpabort(0);
56515095Smckusick 			/*NOTREACHED*/
56615095Smckusick 		} else
56715095Smckusick 			breaderrors = 0;
56815095Smckusick 	}
56934359Skarels 	/*
57034359Skarels 	 * Zero buffer, then try to read each sector of buffer separately.
57134359Skarels 	 */
572*68991Sbostic 	memset(buf, 0, size);
57346792Smckusick 	for (i = 0; i < size; i += dev_bsize, buf += dev_bsize, blkno++) {
57468607Smckusick 		if (lseek(diskfd, ((off_t)blkno << dev_bshift), 0) < 0)
57534359Skarels 			msg("bread: lseek2 fails!\n");
57654070Smckusick 		if ((cnt = read(diskfd, buf, (int)dev_bsize)) == dev_bsize)
57750905Smckusick 			continue;
57850905Smckusick 		if (cnt == -1) {
57950905Smckusick 			msg("read error from %s: %s: [sector %d]: count=%d\n",
58050905Smckusick 				disk, strerror(errno), blkno, dev_bsize);
58150905Smckusick 			continue;
58250905Smckusick 		}
58350905Smckusick 		msg("short read error from %s: [sector %d]: count=%d, got=%d\n",
58450905Smckusick 			disk, blkno, dev_bsize, cnt);
58534359Skarels 	}
5861426Sroot }
587