xref: /csrg-svn/sbin/dump/traverse.c (revision 57723)
147082Smckusick /*-
247082Smckusick  * Copyright (c) 1980, 1988, 1991 The Regents of the University of California.
347082Smckusick  * All rights reserved.
447082Smckusick  *
547082Smckusick  * %sccs.include.redist.c%
622041Sdist  */
75329Smckusic 
822041Sdist #ifndef lint
9*57723Smckusick static char sccsid[] = "@(#)traverse.c	5.23 (Berkeley) 01/25/93";
1046584Storek #endif /* not lint */
1122041Sdist 
12*57723Smckusick #include <sys/param.h>
13*57723Smckusick #include <sys/time.h>
14*57723Smckusick #include <sys/stat.h>
1550499Smckusick #ifdef sunos
16*57723Smckusick #include <sys/vnode.h>
17*57723Smckusick 
1851605Sbostic #include <ufs/fs.h>
19*57723Smckusick #include <ufs/fsdir.h>
20*57723Smckusick #include <ufs/inode.h>
2150499Smckusick #else
2254070Smckusick #include <ufs/ffs/fs.h>
2351605Sbostic #include <ufs/ufs/dir.h>
2451605Sbostic #include <ufs/ufs/dinode.h>
25*57723Smckusick #endif
26*57723Smckusick 
2746795Sbostic #include <protocols/dumprestore.h>
28*57723Smckusick 
29*57723Smckusick #include <ctype.h>
30*57723Smckusick #include <stdio.h>
3146795Sbostic #ifdef __STDC__
32*57723Smckusick #include <string.h>
3346795Sbostic #include <unistd.h>
3446795Sbostic #endif
35*57723Smckusick 
361426Sroot #include "dump.h"
371426Sroot 
3846792Smckusick #define	HASDUMPEDFILE	0x1
3946792Smckusick #define	HASSUBDIRS	0x2
4046584Storek 
41*57723Smckusick static	int dirindir __P((ino_t ino, daddr_t blkno, int level, long *size));
42*57723Smckusick static	void dmpindir __P((ino_t ino, daddr_t blk, int level, quad_t *size));
43*57723Smckusick static	int searchdir __P((ino_t ino, daddr_t blkno, long size, long filesize));
44*57723Smckusick 
4546584Storek /*
4646584Storek  * This is an estimation of the number of TP_BSIZE blocks in the file.
4746584Storek  * It estimates the number of blocks in files with holes by assuming
4846584Storek  * that all of the blocks accounted for by di_blocks are data blocks
4946584Storek  * (when some of the blocks are usually used for indirect pointers);
5046584Storek  * hence the estimate may be high.
5146584Storek  */
5246792Smckusick long
5347058Smckusick blockest(dp)
5447058Smckusick 	register struct dinode *dp;
5546584Storek {
5646792Smckusick 	long blkest, sizeest;
5746584Storek 
5846584Storek 	/*
5947058Smckusick 	 * dp->di_size is the size of the file in bytes.
6047058Smckusick 	 * dp->di_blocks stores the number of sectors actually in the file.
6146584Storek 	 * If there are more sectors than the size would indicate, this just
6246584Storek 	 *	means that there are indirect blocks in the file or unused
6346584Storek 	 *	sectors in the last file block; we can safely ignore these
6446792Smckusick 	 *	(blkest = sizeest below).
6546584Storek 	 * If the file is bigger than the number of sectors would indicate,
6646584Storek 	 *	then the file has holes in it.	In this case we must use the
6746584Storek 	 *	block count to estimate the number of data blocks used, but
6846584Storek 	 *	we use the actual size for estimating the number of indirect
6946792Smckusick 	 *	dump blocks (sizeest vs. blkest in the indirect block
7046792Smckusick 	 *	calculation).
7146584Storek 	 */
7247058Smckusick 	blkest = howmany(dbtob(dp->di_blocks), TP_BSIZE);
7347058Smckusick 	sizeest = howmany(dp->di_size, TP_BSIZE);
7446792Smckusick 	if (blkest > sizeest)
7546792Smckusick 		blkest = sizeest;
7647058Smckusick 	if (dp->di_size > sblock->fs_bsize * NDADDR) {
7746584Storek 		/* calculate the number of indirect blocks on the dump tape */
7846792Smckusick 		blkest +=
7946792Smckusick 			howmany(sizeest - NDADDR * sblock->fs_bsize / TP_BSIZE,
8046584Storek 			TP_NINDIR);
8146584Storek 	}
8246792Smckusick 	return (blkest + 1);
8346584Storek }
8446584Storek 
8546792Smckusick /*
8646792Smckusick  * Dump pass 1.
8746792Smckusick  *
8846792Smckusick  * Walk the inode list for a filesystem to find all allocated inodes
8946792Smckusick  * that have been modified since the previous dump time. Also, find all
9046792Smckusick  * the directories in the filesystem.
9146792Smckusick  */
92*57723Smckusick int
9346792Smckusick mapfiles(maxino, tapesize)
9446792Smckusick 	ino_t maxino;
9546792Smckusick 	long *tapesize;
9646584Storek {
9746792Smckusick 	register int mode;
9846792Smckusick 	register ino_t ino;
9946792Smckusick 	register struct dinode *dp;
10046792Smckusick 	int anydirskipped = 0;
10146584Storek 
10257718Smckusick 	for (ino = 0; ino <= maxino; ino++) {
10346792Smckusick 		dp = getino(ino);
10446792Smckusick 		if ((mode = (dp->di_mode & IFMT)) == 0)
10546792Smckusick 			continue;
10646792Smckusick 		SETINO(ino, usedinomap);
10746792Smckusick 		if (mode == IFDIR)
10846792Smckusick 			SETINO(ino, dumpdirmap);
109*57723Smckusick 		if (
110*57723Smckusick #ifdef FS_44INODEFMT
111*57723Smckusick 		    (dp->di_mtime.ts_sec >= spcl.c_ddate ||
112*57723Smckusick 		     dp->di_ctime.ts_sec >= spcl.c_ddate)
11354070Smckusick 		    && (dp->di_flags & NODUMP) != NODUMP
114*57723Smckusick #else
115*57723Smckusick 		    dp->di_mtime >= spcl.c_ddate ||
116*57723Smckusick 		     dp->di_ctime >= spcl.c_ddate
11754070Smckusick #endif
11854070Smckusick 		    ) {
11946792Smckusick 			SETINO(ino, dumpinomap);
12046792Smckusick 			if (mode != IFREG && mode != IFDIR && mode != IFLNK) {
12146792Smckusick 				*tapesize += 1;
12246792Smckusick 				continue;
12346792Smckusick 			}
12446792Smckusick 			*tapesize += blockest(dp);
12546792Smckusick 			continue;
12646792Smckusick 		}
12746792Smckusick 		if (mode == IFDIR)
12846792Smckusick 			anydirskipped = 1;
12946792Smckusick 	}
13046792Smckusick 	/*
13146792Smckusick 	 * Restore gets very upset if the root is not dumped,
13246792Smckusick 	 * so ensure that it always is dumped.
13346792Smckusick 	 */
13451374Smckusick 	SETINO(ROOTINO, dumpinomap);
13546792Smckusick 	return (anydirskipped);
13646584Storek }
13746584Storek 
13846792Smckusick /*
13946792Smckusick  * Dump pass 2.
14046792Smckusick  *
14146792Smckusick  * Scan each directory on the filesystem to see if it has any modified
14246792Smckusick  * files in it. If it does, and has not already been added to the dump
14346792Smckusick  * list (because it was itself modified), then add it. If a directory
14446792Smckusick  * has not been modified itself, contains no modified files and has no
14546792Smckusick  * subdirectories, then it can be deleted from the dump list and from
14646792Smckusick  * the list of directories. By deleting it from the list of directories,
14746792Smckusick  * its parent may now qualify for the same treatment on this or a later
14846792Smckusick  * pass using this algorithm.
14946792Smckusick  */
150*57723Smckusick int
15146792Smckusick mapdirs(maxino, tapesize)
15246792Smckusick 	ino_t maxino;
15346792Smckusick 	long *tapesize;
15446792Smckusick {
15546792Smckusick 	register struct	dinode *dp;
15655390Smckusick 	register int i, isdir;
15725797Smckusick 	register char *map;
15846792Smckusick 	register ino_t ino;
15954070Smckusick 	long filesize;
16046792Smckusick 	int ret, change = 0;
1611426Sroot 
16246792Smckusick 	for (map = dumpdirmap, ino = 0; ino < maxino; ) {
16346584Storek 		if ((ino % NBBY) == 0)
16455390Smckusick 			isdir = *map++;
16546792Smckusick 		else
16655390Smckusick 			isdir >>= 1;
1674702Smckusic 		ino++;
16855390Smckusick 		if ((isdir & 1) == 0 || TSTINO(ino, dumpinomap))
16946792Smckusick 			continue;
17046792Smckusick 		dp = getino(ino);
17146792Smckusick 		filesize = dp->di_size;
17246792Smckusick 		for (ret = 0, i = 0; filesize > 0 && i < NDADDR; i++) {
17346792Smckusick 			if (dp->di_db[i] != 0)
17446792Smckusick 				ret |= searchdir(ino, dp->di_db[i],
17555390Smckusick 					(long)dblksize(sblock, dp, i),
17655390Smckusick 					filesize);
17746792Smckusick 			if (ret & HASDUMPEDFILE)
17846792Smckusick 				filesize = 0;
17946792Smckusick 			else
18046792Smckusick 				filesize -= sblock->fs_bsize;
18146792Smckusick 		}
18246792Smckusick 		for (i = 0; filesize > 0 && i < NIADDR; i++) {
18346792Smckusick 			if (dp->di_ib[i] == 0)
18446792Smckusick 				continue;
18546792Smckusick 			ret |= dirindir(ino, dp->di_ib[i], i, &filesize);
18646792Smckusick 		}
18746792Smckusick 		if (ret & HASDUMPEDFILE) {
18855390Smckusick 			SETINO(ino, dumpinomap);
18955390Smckusick 			*tapesize += blockest(dp);
19046792Smckusick 			change = 1;
19146792Smckusick 			continue;
19246792Smckusick 		}
19346792Smckusick 		if ((ret & HASSUBDIRS) == 0) {
19446792Smckusick 			if (!TSTINO(ino, dumpinomap)) {
19546792Smckusick 				CLRINO(ino, dumpdirmap);
19646792Smckusick 				change = 1;
19746792Smckusick 			}
19846792Smckusick 		}
1991426Sroot 	}
20046792Smckusick 	return (change);
2011426Sroot }
2021426Sroot 
20346792Smckusick /*
20446792Smckusick  * Read indirect blocks, and pass the data blocks to be searched
20546792Smckusick  * as directories. Quit as soon as any entry is found that will
20646792Smckusick  * require the directory to be dumped.
20746792Smckusick  */
208*57723Smckusick static int
20954070Smckusick dirindir(ino, blkno, ind_level, filesize)
21046792Smckusick 	ino_t ino;
21146792Smckusick 	daddr_t blkno;
21254070Smckusick 	int ind_level;
21354070Smckusick 	long *filesize;
2141426Sroot {
21546792Smckusick 	int ret = 0;
21646792Smckusick 	register int i;
21746792Smckusick 	daddr_t	idblk[MAXNINDIR];
2181426Sroot 
21954070Smckusick 	bread(fsbtodb(sblock, blkno), (char *)idblk, (int)sblock->fs_bsize);
22054070Smckusick 	if (ind_level <= 0) {
22146792Smckusick 		for (i = 0; *filesize > 0 && i < NINDIR(sblock); i++) {
22246792Smckusick 			blkno = idblk[i];
22346792Smckusick 			if (blkno != 0)
22446796Smckusick 				ret |= searchdir(ino, blkno, sblock->fs_bsize,
22554070Smckusick 					*filesize);
22646792Smckusick 			if (ret & HASDUMPEDFILE)
22746792Smckusick 				*filesize = 0;
22846792Smckusick 			else
22946792Smckusick 				*filesize -= sblock->fs_bsize;
2301426Sroot 		}
23146792Smckusick 		return (ret);
2325329Smckusic 	}
23354070Smckusick 	ind_level--;
23446792Smckusick 	for (i = 0; *filesize > 0 && i < NINDIR(sblock); i++) {
23546792Smckusick 		blkno = idblk[i];
23646792Smckusick 		if (blkno != 0)
23754070Smckusick 			ret |= dirindir(ino, blkno, ind_level, filesize);
2385329Smckusic 	}
23946792Smckusick 	return (ret);
2401426Sroot }
2411426Sroot 
24246792Smckusick /*
24346792Smckusick  * Scan a disk block containing directory information looking to see if
24446792Smckusick  * any of the entries are on the dump list and to see if the directory
24546792Smckusick  * contains any subdirectories.
24646792Smckusick  */
247*57723Smckusick static int
24846796Smckusick searchdir(ino, blkno, size, filesize)
24946792Smckusick 	ino_t ino;
25046792Smckusick 	daddr_t blkno;
25154070Smckusick 	register long size;
25254070Smckusick 	long filesize;
2535329Smckusic {
25446792Smckusick 	register struct direct *dp;
25555390Smckusick 	register long loc, ret = 0;
25646792Smckusick 	char dblk[MAXBSIZE];
2575329Smckusic 
25854070Smckusick 	bread(fsbtodb(sblock, blkno), dblk, (int)size);
25946796Smckusick 	if (filesize < size)
26046796Smckusick 		size = filesize;
26146792Smckusick 	for (loc = 0; loc < size; ) {
26246792Smckusick 		dp = (struct direct *)(dblk + loc);
26346792Smckusick 		if (dp->d_reclen == 0) {
26446792Smckusick 			msg("corrupted directory, inumber %d\n", ino);
26546792Smckusick 			break;
2665329Smckusic 		}
26746792Smckusick 		loc += dp->d_reclen;
26846792Smckusick 		if (dp->d_ino == 0)
26946792Smckusick 			continue;
27046792Smckusick 		if (dp->d_name[0] == '.') {
27146792Smckusick 			if (dp->d_name[1] == '\0')
27246792Smckusick 				continue;
27346792Smckusick 			if (dp->d_name[1] == '.' && dp->d_name[2] == '\0')
27446792Smckusick 				continue;
2755329Smckusic 		}
27655390Smckusick 		if (TSTINO(dp->d_ino, dumpinomap)) {
27755390Smckusick 			ret |= HASDUMPEDFILE;
27855390Smckusick 			if (ret & HASSUBDIRS)
27955390Smckusick 				break;
28055390Smckusick 		}
28155390Smckusick 		if (TSTINO(dp->d_ino, dumpdirmap)) {
28255390Smckusick 			ret |= HASSUBDIRS;
28355390Smckusick 			if (ret & HASDUMPEDFILE)
28455390Smckusick 				break;
28555390Smckusick 		}
2865329Smckusic 	}
28755390Smckusick 	return (ret);
2885329Smckusic }
2895329Smckusic 
29046792Smckusick /*
29146792Smckusick  * Dump passes 3 and 4.
29246792Smckusick  *
29346792Smckusick  * Dump the contents of an inode to tape.
29446792Smckusick  */
29546584Storek void
29647058Smckusick dumpino(dp, ino)
29747058Smckusick 	register struct dinode *dp;
29846792Smckusick 	ino_t ino;
29917234Smckusick {
30054596Smckusick 	int ind_level, cnt;
301*57723Smckusick 	quad_t size;
30254596Smckusick 	char buf[TP_BSIZE];
3031426Sroot 
30446792Smckusick 	if (newtape) {
3051426Sroot 		newtape = 0;
30646792Smckusick 		dumpmap(dumpinomap, TS_BITS, ino);
3071426Sroot 	}
30846792Smckusick 	CLRINO(ino, dumpinomap);
30947058Smckusick 	spcl.c_dinode = *dp;
3101426Sroot 	spcl.c_type = TS_INODE;
3111426Sroot 	spcl.c_count = 0;
312*57723Smckusick 	switch (dp->di_mode & S_IFMT) {
31354596Smckusick 
314*57723Smckusick 	case 0:
31554596Smckusick 		/*
31654596Smckusick 		 * Freed inode.
31754596Smckusick 		 */
31817234Smckusick 		return;
31954596Smckusick 
320*57723Smckusick 	case S_IFLNK:
32154596Smckusick 		/*
32254596Smckusick 		 * Check for short symbolic link.
32354596Smckusick 		 */
324*57723Smckusick #ifdef FS_44INODEFMT
32554596Smckusick 		if (dp->di_size > 0 &&
32654596Smckusick 		    dp->di_size < sblock->fs_maxsymlinklen) {
32754596Smckusick 			spcl.c_addr[0] = 1;
32854596Smckusick 			spcl.c_count = 1;
32954596Smckusick 			writeheader(ino);
33054596Smckusick 			bcopy((caddr_t)dp->di_shortlink, buf,
33154596Smckusick 			    (u_long)dp->di_size);
33254596Smckusick 			buf[dp->di_size] = '\0';
33354596Smckusick 			writerec(buf, 0);
33454596Smckusick 			return;
33554596Smckusick 		}
336*57723Smckusick #endif
33754596Smckusick 		/* fall through */
33854596Smckusick 
339*57723Smckusick 	case S_IFDIR:
340*57723Smckusick 	case S_IFREG:
34154596Smckusick 		if (dp->di_size > 0)
34254596Smckusick 			break;
34354596Smckusick 		/* fall through */
34454596Smckusick 
345*57723Smckusick 	case S_IFIFO:
346*57723Smckusick 	case S_IFSOCK:
347*57723Smckusick 	case S_IFCHR:
348*57723Smckusick 	case S_IFBLK:
34946792Smckusick 		writeheader(ino);
3501426Sroot 		return;
35154596Smckusick 
35254596Smckusick 	default:
35354596Smckusick 		msg("Warning: undefined file type 0%o\n", dp->di_mode & IFMT);
35454596Smckusick 		return;
3551426Sroot 	}
35647058Smckusick 	if (dp->di_size > NDADDR * sblock->fs_bsize)
35746792Smckusick 		cnt = NDADDR * sblock->fs_frag;
3584777Smckusic 	else
35947058Smckusick 		cnt = howmany(dp->di_size, sblock->fs_fsize);
36047058Smckusick 	blksout(&dp->di_db[0], cnt, ino);
36147058Smckusick 	if ((size = dp->di_size - NDADDR * sblock->fs_bsize) <= 0)
3624777Smckusic 		return;
36354070Smckusick 	for (ind_level = 0; ind_level < NIADDR; ind_level++) {
36454070Smckusick 		dmpindir(ino, dp->di_ib[ind_level], ind_level, &size);
3654777Smckusic 		if (size <= 0)
3664777Smckusic 			return;
3674777Smckusic 	}
3681426Sroot }
3691426Sroot 
37046792Smckusick /*
37146792Smckusick  * Read indirect blocks, and pass the data blocks to be dumped.
37246792Smckusick  */
373*57723Smckusick static void
37454070Smckusick dmpindir(ino, blk, ind_level, size)
37546792Smckusick 	ino_t ino;
3764777Smckusic 	daddr_t blk;
37754070Smckusick 	int ind_level;
378*57723Smckusick 	quad_t *size;
3791426Sroot {
3804777Smckusic 	int i, cnt;
3815329Smckusic 	daddr_t idblk[MAXNINDIR];
3821426Sroot 
3834777Smckusic 	if (blk != 0)
38454070Smckusick 		bread(fsbtodb(sblock, blk), (char *)idblk, (int) sblock->fs_bsize);
3854777Smckusic 	else
38654070Smckusick 		bzero((char *)idblk, (int)sblock->fs_bsize);
38754070Smckusick 	if (ind_level <= 0) {
3885329Smckusic 		if (*size < NINDIR(sblock) * sblock->fs_bsize)
3895329Smckusic 			cnt = howmany(*size, sblock->fs_fsize);
3904777Smckusic 		else
3915329Smckusic 			cnt = NINDIR(sblock) * sblock->fs_frag;
3925329Smckusic 		*size -= NINDIR(sblock) * sblock->fs_bsize;
39346792Smckusick 		blksout(&idblk[0], cnt, ino);
3944777Smckusic 		return;
3951426Sroot 	}
39654070Smckusick 	ind_level--;
3975329Smckusic 	for (i = 0; i < NINDIR(sblock); i++) {
39854070Smckusick 		dmpindir(ino, idblk[i], ind_level, size);
3994777Smckusic 		if (*size <= 0)
4004777Smckusic 			return;
4014777Smckusic 	}
4021426Sroot }
4031426Sroot 
40446792Smckusick /*
40546792Smckusick  * Collect up the data into tape record sized buffers and output them.
40646792Smckusick  */
40746584Storek void
40846792Smckusick blksout(blkp, frags, ino)
4094777Smckusic 	daddr_t *blkp;
4104777Smckusic 	int frags;
41146792Smckusick 	ino_t ino;
4124777Smckusic {
41346584Storek 	register daddr_t *bp;
4145329Smckusic 	int i, j, count, blks, tbperdb;
4154777Smckusic 
4169403Smckusick 	blks = howmany(frags * sblock->fs_fsize, TP_BSIZE);
41746584Storek 	tbperdb = sblock->fs_bsize >> tp_bshift;
4184777Smckusic 	for (i = 0; i < blks; i += TP_NINDIR) {
4194777Smckusic 		if (i + TP_NINDIR > blks)
4204777Smckusic 			count = blks;
4214777Smckusic 		else
4224777Smckusic 			count = i + TP_NINDIR;
4234777Smckusic 		for (j = i; j < count; j++)
4245329Smckusic 			if (blkp[j / tbperdb] != 0)
4254777Smckusic 				spcl.c_addr[j - i] = 1;
4264777Smckusic 			else
4274777Smckusic 				spcl.c_addr[j - i] = 0;
4284777Smckusic 		spcl.c_count = count - i;
42946792Smckusick 		writeheader(ino);
43046584Storek 		bp = &blkp[i / tbperdb];
43146584Storek 		for (j = i; j < count; j += tbperdb, bp++)
43246584Storek 			if (*bp != 0)
4335329Smckusic 				if (j + tbperdb <= count)
43454070Smckusick 					dumpblock(*bp, (int)sblock->fs_bsize);
4354777Smckusic 				else
43646792Smckusick 					dumpblock(*bp, (count - j) * TP_BSIZE);
4374777Smckusic 		spcl.c_type = TS_ADDR;
4384777Smckusic 	}
4394777Smckusic }
4404777Smckusic 
44146792Smckusick /*
44246792Smckusick  * Dump a map to the tape.
44346792Smckusick  */
44446584Storek void
44546792Smckusick dumpmap(map, type, ino)
4465329Smckusic 	char *map;
44746792Smckusick 	int type;
44846792Smckusick 	ino_t ino;
4491426Sroot {
45046792Smckusick 	register int i;
4511426Sroot 	char *cp;
4521426Sroot 
45346792Smckusick 	spcl.c_type = type;
45446792Smckusick 	spcl.c_count = howmany(mapsize * sizeof(char), TP_BSIZE);
45546792Smckusick 	writeheader(ino);
4565329Smckusic 	for (i = 0, cp = map; i < spcl.c_count; i++, cp += TP_BSIZE)
45754596Smckusick 		writerec(cp, 0);
4581426Sroot }
4591426Sroot 
46046792Smckusick /*
46146792Smckusick  * Write a header record to the dump tape.
46246792Smckusick  */
46346584Storek void
46446792Smckusick writeheader(ino)
46546792Smckusick 	ino_t ino;
4661426Sroot {
46746792Smckusick 	register long sum, cnt, *lp;
4681426Sroot 
4691426Sroot 	spcl.c_inumber = ino;
4708368Smckusick 	spcl.c_magic = NFS_MAGIC;
4711426Sroot 	spcl.c_checksum = 0;
47246792Smckusick 	lp = (long *)&spcl;
47346792Smckusick 	sum = 0;
47446792Smckusick 	cnt = sizeof(union u_spcl) / (4 * sizeof(long));
47546792Smckusick 	while (--cnt >= 0) {
47646792Smckusick 		sum += *lp++;
47746792Smckusick 		sum += *lp++;
47846792Smckusick 		sum += *lp++;
47946792Smckusick 		sum += *lp++;
48024168Smckusick 	}
48146792Smckusick 	spcl.c_checksum = CHECKSUM - sum;
48254596Smckusick 	writerec((char *)&spcl, 1);
4831426Sroot }
4841426Sroot 
4854702Smckusic struct dinode *
48646792Smckusick getino(inum)
48746792Smckusick 	ino_t inum;
4884702Smckusic {
4894702Smckusic 	static daddr_t minino, maxino;
49046792Smckusick 	static struct dinode inoblock[MAXINOPB];
4914702Smckusic 
49246792Smckusick 	curino = inum;
49346792Smckusick 	if (inum >= minino && inum < maxino)
49446792Smckusick 		return (&inoblock[inum - minino]);
49554070Smckusick 	bread(fsbtodb(sblock, itod(sblock, inum)), (char *)inoblock,
49654070Smckusick 	    (int)sblock->fs_bsize);
49746792Smckusick 	minino = inum - (inum % INOPB(sblock));
4985329Smckusic 	maxino = minino + INOPB(sblock);
49946792Smckusick 	return (&inoblock[inum - minino]);
5004702Smckusic }
5014702Smckusic 
50246792Smckusick /*
50346792Smckusick  * Read a chunk of data from the disk.
50446792Smckusick  * Try to recover from hard errors by reading in sector sized pieces.
50546792Smckusick  * Error recovery is attempted at most BREADEMAX times before seeking
50646792Smckusick  * consent from the operator to continue.
50746792Smckusick  */
5081426Sroot int	breaderrors = 0;
5091426Sroot #define	BREADEMAX 32
5101426Sroot 
51146584Storek void
51246792Smckusick bread(blkno, buf, size)
51346792Smckusick 	daddr_t blkno;
51446792Smckusick 	char *buf;
51546792Smckusick 	int size;
5161426Sroot {
51746792Smckusick 	int cnt, i;
51834359Skarels 	extern int errno;
5191426Sroot 
52015095Smckusick loop:
52154070Smckusick 	if ((int)lseek(diskfd, ((off_t)blkno << dev_bshift), 0) < 0)
5221426Sroot 		msg("bread: lseek fails\n");
52346792Smckusick 	if ((cnt = read(diskfd, buf, size)) == size)
52415095Smckusick 		return;
52546792Smckusick 	if (blkno + (size / dev_bsize) > fsbtodb(sblock, sblock->fs_size)) {
52615095Smckusick 		/*
52715095Smckusick 		 * Trying to read the final fragment.
52815095Smckusick 		 *
52915095Smckusick 		 * NB - dump only works in TP_BSIZE blocks, hence
53030560Smckusick 		 * rounds `dev_bsize' fragments up to TP_BSIZE pieces.
53115095Smckusick 		 * It should be smarter about not actually trying to
53215095Smckusick 		 * read more than it can get, but for the time being
53315095Smckusick 		 * we punt and scale back the read only when it gets
53415095Smckusick 		 * us into trouble. (mkm 9/25/83)
53515095Smckusick 		 */
53646792Smckusick 		size -= dev_bsize;
53715095Smckusick 		goto loop;
5381426Sroot 	}
53950905Smckusick 	if (cnt == -1)
54050905Smckusick 		msg("read error from %s: %s: [block %d]: count=%d\n",
54150905Smckusick 			disk, strerror(errno), blkno, size);
54250905Smckusick 	else
54350905Smckusick 		msg("short read error from %s: [block %d]: count=%d, got=%d\n",
54450905Smckusick 			disk, blkno, size, cnt);
54546584Storek 	if (++breaderrors > BREADEMAX) {
54615095Smckusick 		msg("More than %d block read errors from %d\n",
54715095Smckusick 			BREADEMAX, disk);
54815095Smckusick 		broadcast("DUMP IS AILING!\n");
54915095Smckusick 		msg("This is an unrecoverable error.\n");
55015095Smckusick 		if (!query("Do you want to attempt to continue?")){
55155288Sbostic 			dumpabort(0);
55215095Smckusick 			/*NOTREACHED*/
55315095Smckusick 		} else
55415095Smckusick 			breaderrors = 0;
55515095Smckusick 	}
55634359Skarels 	/*
55734359Skarels 	 * Zero buffer, then try to read each sector of buffer separately.
55834359Skarels 	 */
55946792Smckusick 	bzero(buf, size);
56046792Smckusick 	for (i = 0; i < size; i += dev_bsize, buf += dev_bsize, blkno++) {
56154070Smckusick 		if ((int)lseek(diskfd, ((off_t)blkno << dev_bshift), 0) < 0)
56234359Skarels 			msg("bread: lseek2 fails!\n");
56354070Smckusick 		if ((cnt = read(diskfd, buf, (int)dev_bsize)) == dev_bsize)
56450905Smckusick 			continue;
56550905Smckusick 		if (cnt == -1) {
56650905Smckusick 			msg("read error from %s: %s: [sector %d]: count=%d\n",
56750905Smckusick 				disk, strerror(errno), blkno, dev_bsize);
56850905Smckusick 			continue;
56950905Smckusick 		}
57050905Smckusick 		msg("short read error from %s: [sector %d]: count=%d, got=%d\n",
57150905Smckusick 			disk, blkno, dev_bsize, cnt);
57234359Skarels 	}
5731426Sroot }
574