xref: /csrg-svn/sbin/dump/traverse.c (revision 55390)
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*55390Smckusick static char sccsid[] = "@(#)traverse.c	5.21 (Berkeley) 07/19/92";
1046584Storek #endif /* not lint */
1122041Sdist 
1250499Smckusick #ifdef sunos
1350499Smckusick #include <stdio.h>
1450499Smckusick #include <ctype.h>
1546795Sbostic #include <sys/param.h>
1651605Sbostic #include <ufs/fs.h>
1750499Smckusick #else
1850499Smckusick #include <sys/param.h>
1954070Smckusick #include <ufs/ffs/fs.h>
2054070Smckusick #endif
2153657Smckusick #include <sys/time.h>
2254070Smckusick #include <sys/stat.h>
2351605Sbostic #include <ufs/ufs/dir.h>
2451605Sbostic #include <ufs/ufs/dinode.h>
2546795Sbostic #include <protocols/dumprestore.h>
2646795Sbostic #ifdef __STDC__
2746795Sbostic #include <unistd.h>
2846795Sbostic #include <string.h>
2946795Sbostic #endif
301426Sroot #include "dump.h"
311426Sroot 
3246792Smckusick void	dmpindir();
3346792Smckusick #define	HASDUMPEDFILE	0x1
3446792Smckusick #define	HASSUBDIRS	0x2
3546584Storek 
3646584Storek /*
3746584Storek  * This is an estimation of the number of TP_BSIZE blocks in the file.
3846584Storek  * It estimates the number of blocks in files with holes by assuming
3946584Storek  * that all of the blocks accounted for by di_blocks are data blocks
4046584Storek  * (when some of the blocks are usually used for indirect pointers);
4146584Storek  * hence the estimate may be high.
4246584Storek  */
4346792Smckusick long
4447058Smckusick blockest(dp)
4547058Smckusick 	register struct dinode *dp;
4646584Storek {
4746792Smckusick 	long blkest, sizeest;
4846584Storek 
4946584Storek 	/*
5047058Smckusick 	 * dp->di_size is the size of the file in bytes.
5147058Smckusick 	 * dp->di_blocks stores the number of sectors actually in the file.
5246584Storek 	 * If there are more sectors than the size would indicate, this just
5346584Storek 	 *	means that there are indirect blocks in the file or unused
5446584Storek 	 *	sectors in the last file block; we can safely ignore these
5546792Smckusick 	 *	(blkest = sizeest below).
5646584Storek 	 * If the file is bigger than the number of sectors would indicate,
5746584Storek 	 *	then the file has holes in it.	In this case we must use the
5846584Storek 	 *	block count to estimate the number of data blocks used, but
5946584Storek 	 *	we use the actual size for estimating the number of indirect
6046792Smckusick 	 *	dump blocks (sizeest vs. blkest in the indirect block
6146792Smckusick 	 *	calculation).
6246584Storek 	 */
6347058Smckusick 	blkest = howmany(dbtob(dp->di_blocks), TP_BSIZE);
6447058Smckusick 	sizeest = howmany(dp->di_size, TP_BSIZE);
6546792Smckusick 	if (blkest > sizeest)
6646792Smckusick 		blkest = sizeest;
6747058Smckusick 	if (dp->di_size > sblock->fs_bsize * NDADDR) {
6846584Storek 		/* calculate the number of indirect blocks on the dump tape */
6946792Smckusick 		blkest +=
7046792Smckusick 			howmany(sizeest - NDADDR * sblock->fs_bsize / TP_BSIZE,
7146584Storek 			TP_NINDIR);
7246584Storek 	}
7346792Smckusick 	return (blkest + 1);
7446584Storek }
7546584Storek 
7646792Smckusick /*
7746792Smckusick  * Dump pass 1.
7846792Smckusick  *
7946792Smckusick  * Walk the inode list for a filesystem to find all allocated inodes
8046792Smckusick  * that have been modified since the previous dump time. Also, find all
8146792Smckusick  * the directories in the filesystem.
8246792Smckusick  */
8346792Smckusick mapfiles(maxino, tapesize)
8446792Smckusick 	ino_t maxino;
8546792Smckusick 	long *tapesize;
8646584Storek {
8746792Smckusick 	register int mode;
8846792Smckusick 	register ino_t ino;
8946792Smckusick 	register struct dinode *dp;
9046792Smckusick 	int anydirskipped = 0;
9146584Storek 
9246792Smckusick 	for (ino = 0; ino < maxino; ino++) {
9346792Smckusick 		dp = getino(ino);
9446792Smckusick 		if ((mode = (dp->di_mode & IFMT)) == 0)
9546792Smckusick 			continue;
9646792Smckusick 		SETINO(ino, usedinomap);
9746792Smckusick 		if (mode == IFDIR)
9846792Smckusick 			SETINO(ino, dumpdirmap);
9954070Smckusick 		if ((dp->di_mtime.ts_sec >= spcl.c_ddate ||
10054070Smckusick 		    dp->di_ctime.ts_sec >= spcl.c_ddate)
10154070Smckusick #ifndef sunos
10254070Smckusick 		    && (dp->di_flags & NODUMP) != NODUMP
10354070Smckusick #endif
10454070Smckusick 		    ) {
10546792Smckusick 			SETINO(ino, dumpinomap);
10646792Smckusick 			if (mode != IFREG && mode != IFDIR && mode != IFLNK) {
10746792Smckusick 				*tapesize += 1;
10846792Smckusick 				continue;
10946792Smckusick 			}
11046792Smckusick 			*tapesize += blockest(dp);
11146792Smckusick 			continue;
11246792Smckusick 		}
11346792Smckusick 		if (mode == IFDIR)
11446792Smckusick 			anydirskipped = 1;
11546792Smckusick 	}
11646792Smckusick 	/*
11746792Smckusick 	 * Restore gets very upset if the root is not dumped,
11846792Smckusick 	 * so ensure that it always is dumped.
11946792Smckusick 	 */
12051374Smckusick 	SETINO(ROOTINO, dumpinomap);
12146792Smckusick 	return (anydirskipped);
12246584Storek }
12346584Storek 
12446792Smckusick /*
12546792Smckusick  * Dump pass 2.
12646792Smckusick  *
12746792Smckusick  * Scan each directory on the filesystem to see if it has any modified
12846792Smckusick  * files in it. If it does, and has not already been added to the dump
12946792Smckusick  * list (because it was itself modified), then add it. If a directory
13046792Smckusick  * has not been modified itself, contains no modified files and has no
13146792Smckusick  * subdirectories, then it can be deleted from the dump list and from
13246792Smckusick  * the list of directories. By deleting it from the list of directories,
13346792Smckusick  * its parent may now qualify for the same treatment on this or a later
13446792Smckusick  * pass using this algorithm.
13546792Smckusick  */
13646792Smckusick mapdirs(maxino, tapesize)
13746792Smckusick 	ino_t maxino;
13846792Smckusick 	long *tapesize;
13946792Smckusick {
14046792Smckusick 	register struct	dinode *dp;
141*55390Smckusick 	register int i, isdir;
14225797Smckusick 	register char *map;
14346792Smckusick 	register ino_t ino;
14454070Smckusick 	long filesize;
14546792Smckusick 	int ret, change = 0;
1461426Sroot 
14746792Smckusick 	for (map = dumpdirmap, ino = 0; ino < maxino; ) {
14846584Storek 		if ((ino % NBBY) == 0)
149*55390Smckusick 			isdir = *map++;
15046792Smckusick 		else
151*55390Smckusick 			isdir >>= 1;
1524702Smckusic 		ino++;
153*55390Smckusick 		if ((isdir & 1) == 0 || TSTINO(ino, dumpinomap))
15446792Smckusick 			continue;
15546792Smckusick 		dp = getino(ino);
15646792Smckusick 		filesize = dp->di_size;
15746792Smckusick 		for (ret = 0, i = 0; filesize > 0 && i < NDADDR; i++) {
15846792Smckusick 			if (dp->di_db[i] != 0)
15946792Smckusick 				ret |= searchdir(ino, dp->di_db[i],
160*55390Smckusick 					(long)dblksize(sblock, dp, i),
161*55390Smckusick 					filesize);
16246792Smckusick 			if (ret & HASDUMPEDFILE)
16346792Smckusick 				filesize = 0;
16446792Smckusick 			else
16546792Smckusick 				filesize -= sblock->fs_bsize;
16646792Smckusick 		}
16746792Smckusick 		for (i = 0; filesize > 0 && i < NIADDR; i++) {
16846792Smckusick 			if (dp->di_ib[i] == 0)
16946792Smckusick 				continue;
17046792Smckusick 			ret |= dirindir(ino, dp->di_ib[i], i, &filesize);
17146792Smckusick 		}
17246792Smckusick 		if (ret & HASDUMPEDFILE) {
173*55390Smckusick 			SETINO(ino, dumpinomap);
174*55390Smckusick 			*tapesize += blockest(dp);
17546792Smckusick 			change = 1;
17646792Smckusick 			continue;
17746792Smckusick 		}
17846792Smckusick 		if ((ret & HASSUBDIRS) == 0) {
17946792Smckusick 			if (!TSTINO(ino, dumpinomap)) {
18046792Smckusick 				CLRINO(ino, dumpdirmap);
18146792Smckusick 				change = 1;
18246792Smckusick 			}
18346792Smckusick 		}
1841426Sroot 	}
18546792Smckusick 	return (change);
1861426Sroot }
1871426Sroot 
18846792Smckusick /*
18946792Smckusick  * Read indirect blocks, and pass the data blocks to be searched
19046792Smckusick  * as directories. Quit as soon as any entry is found that will
19146792Smckusick  * require the directory to be dumped.
19246792Smckusick  */
19354070Smckusick dirindir(ino, blkno, ind_level, filesize)
19446792Smckusick 	ino_t ino;
19546792Smckusick 	daddr_t blkno;
19654070Smckusick 	int ind_level;
19754070Smckusick 	long *filesize;
1981426Sroot {
19946792Smckusick 	int ret = 0;
20046792Smckusick 	register int i;
20146792Smckusick 	daddr_t	idblk[MAXNINDIR];
2021426Sroot 
20354070Smckusick 	bread(fsbtodb(sblock, blkno), (char *)idblk, (int)sblock->fs_bsize);
20454070Smckusick 	if (ind_level <= 0) {
20546792Smckusick 		for (i = 0; *filesize > 0 && i < NINDIR(sblock); i++) {
20646792Smckusick 			blkno = idblk[i];
20746792Smckusick 			if (blkno != 0)
20846796Smckusick 				ret |= searchdir(ino, blkno, sblock->fs_bsize,
20954070Smckusick 					*filesize);
21046792Smckusick 			if (ret & HASDUMPEDFILE)
21146792Smckusick 				*filesize = 0;
21246792Smckusick 			else
21346792Smckusick 				*filesize -= sblock->fs_bsize;
2141426Sroot 		}
21546792Smckusick 		return (ret);
2165329Smckusic 	}
21754070Smckusick 	ind_level--;
21846792Smckusick 	for (i = 0; *filesize > 0 && i < NINDIR(sblock); i++) {
21946792Smckusick 		blkno = idblk[i];
22046792Smckusick 		if (blkno != 0)
22154070Smckusick 			ret |= dirindir(ino, blkno, ind_level, filesize);
2225329Smckusic 	}
22346792Smckusick 	return (ret);
2241426Sroot }
2251426Sroot 
22646792Smckusick /*
22746792Smckusick  * Scan a disk block containing directory information looking to see if
22846792Smckusick  * any of the entries are on the dump list and to see if the directory
22946792Smckusick  * contains any subdirectories.
23046792Smckusick  */
23146796Smckusick searchdir(ino, blkno, size, filesize)
23246792Smckusick 	ino_t ino;
23346792Smckusick 	daddr_t blkno;
23454070Smckusick 	register long size;
23554070Smckusick 	long filesize;
2365329Smckusic {
23746792Smckusick 	register struct direct *dp;
238*55390Smckusick 	register long loc, ret = 0;
23946792Smckusick 	char dblk[MAXBSIZE];
2405329Smckusic 
24154070Smckusick 	bread(fsbtodb(sblock, blkno), dblk, (int)size);
24246796Smckusick 	if (filesize < size)
24346796Smckusick 		size = filesize;
24446792Smckusick 	for (loc = 0; loc < size; ) {
24546792Smckusick 		dp = (struct direct *)(dblk + loc);
24646792Smckusick 		if (dp->d_reclen == 0) {
24746792Smckusick 			msg("corrupted directory, inumber %d\n", ino);
24846792Smckusick 			break;
2495329Smckusic 		}
25046792Smckusick 		loc += dp->d_reclen;
25146792Smckusick 		if (dp->d_ino == 0)
25246792Smckusick 			continue;
25346792Smckusick 		if (dp->d_name[0] == '.') {
25446792Smckusick 			if (dp->d_name[1] == '\0')
25546792Smckusick 				continue;
25646792Smckusick 			if (dp->d_name[1] == '.' && dp->d_name[2] == '\0')
25746792Smckusick 				continue;
2585329Smckusic 		}
259*55390Smckusick 		if (TSTINO(dp->d_ino, dumpinomap)) {
260*55390Smckusick 			ret |= HASDUMPEDFILE;
261*55390Smckusick 			if (ret & HASSUBDIRS)
262*55390Smckusick 				break;
263*55390Smckusick 		}
264*55390Smckusick 		if (TSTINO(dp->d_ino, dumpdirmap)) {
265*55390Smckusick 			ret |= HASSUBDIRS;
266*55390Smckusick 			if (ret & HASDUMPEDFILE)
267*55390Smckusick 				break;
268*55390Smckusick 		}
2695329Smckusic 	}
270*55390Smckusick 	return (ret);
2715329Smckusic }
2725329Smckusic 
27346792Smckusick /*
27446792Smckusick  * Dump passes 3 and 4.
27546792Smckusick  *
27646792Smckusick  * Dump the contents of an inode to tape.
27746792Smckusick  */
27846584Storek void
27947058Smckusick dumpino(dp, ino)
28047058Smckusick 	register struct dinode *dp;
28146792Smckusick 	ino_t ino;
28217234Smckusick {
28354596Smckusick 	int ind_level, cnt;
2844777Smckusic 	long size;
28554596Smckusick 	char buf[TP_BSIZE];
2861426Sroot 
28746792Smckusick 	if (newtape) {
2881426Sroot 		newtape = 0;
28946792Smckusick 		dumpmap(dumpinomap, TS_BITS, ino);
2901426Sroot 	}
29146792Smckusick 	CLRINO(ino, dumpinomap);
29247058Smckusick 	spcl.c_dinode = *dp;
2931426Sroot 	spcl.c_type = TS_INODE;
2941426Sroot 	spcl.c_count = 0;
29554596Smckusick 	switch (IFTODT(dp->di_mode)) {
29654596Smckusick 
29754596Smckusick 	case DT_UNKNOWN:
29854596Smckusick 		/*
29954596Smckusick 		 * Freed inode.
30054596Smckusick 		 */
30117234Smckusick 		return;
30254596Smckusick 
30354596Smckusick 	case DT_LNK:
30454596Smckusick 		/*
30554596Smckusick 		 * Check for short symbolic link.
30654596Smckusick 		 */
30754596Smckusick 		if (dp->di_size > 0 &&
30854596Smckusick 		    dp->di_size < sblock->fs_maxsymlinklen) {
30954596Smckusick 			spcl.c_addr[0] = 1;
31054596Smckusick 			spcl.c_count = 1;
31154596Smckusick 			writeheader(ino);
31254596Smckusick 			bcopy((caddr_t)dp->di_shortlink, buf,
31354596Smckusick 			    (u_long)dp->di_size);
31454596Smckusick 			buf[dp->di_size] = '\0';
31554596Smckusick 			writerec(buf, 0);
31654596Smckusick 			return;
31754596Smckusick 		}
31854596Smckusick 		/* fall through */
31954596Smckusick 
32054596Smckusick 	case DT_DIR:
32154596Smckusick 	case DT_REG:
32254596Smckusick 		if (dp->di_size > 0)
32354596Smckusick 			break;
32454596Smckusick 		/* fall through */
32554596Smckusick 
32654596Smckusick 	case DT_FIFO:
32754596Smckusick 	case DT_SOCK:
32854596Smckusick 	case DT_CHR:
32954596Smckusick 	case DT_BLK:
33046792Smckusick 		writeheader(ino);
3311426Sroot 		return;
33254596Smckusick 
33354596Smckusick 	default:
33454596Smckusick 		msg("Warning: undefined file type 0%o\n", dp->di_mode & IFMT);
33554596Smckusick 		return;
3361426Sroot 	}
33747058Smckusick 	if (dp->di_size > NDADDR * sblock->fs_bsize)
33846792Smckusick 		cnt = NDADDR * sblock->fs_frag;
3394777Smckusic 	else
34047058Smckusick 		cnt = howmany(dp->di_size, sblock->fs_fsize);
34147058Smckusick 	blksout(&dp->di_db[0], cnt, ino);
34247058Smckusick 	if ((size = dp->di_size - NDADDR * sblock->fs_bsize) <= 0)
3434777Smckusic 		return;
34454070Smckusick 	for (ind_level = 0; ind_level < NIADDR; ind_level++) {
34554070Smckusick 		dmpindir(ino, dp->di_ib[ind_level], ind_level, &size);
3464777Smckusic 		if (size <= 0)
3474777Smckusic 			return;
3484777Smckusic 	}
3491426Sroot }
3501426Sroot 
35146792Smckusick /*
35246792Smckusick  * Read indirect blocks, and pass the data blocks to be dumped.
35346792Smckusick  */
35446584Storek void
35554070Smckusick dmpindir(ino, blk, ind_level, size)
35646792Smckusick 	ino_t ino;
3574777Smckusic 	daddr_t blk;
35854070Smckusick 	int ind_level;
3594777Smckusic 	long *size;
3601426Sroot {
3614777Smckusic 	int i, cnt;
3625329Smckusic 	daddr_t idblk[MAXNINDIR];
3631426Sroot 
3644777Smckusic 	if (blk != 0)
36554070Smckusick 		bread(fsbtodb(sblock, blk), (char *)idblk, (int) sblock->fs_bsize);
3664777Smckusic 	else
36754070Smckusick 		bzero((char *)idblk, (int)sblock->fs_bsize);
36854070Smckusick 	if (ind_level <= 0) {
3695329Smckusic 		if (*size < NINDIR(sblock) * sblock->fs_bsize)
3705329Smckusic 			cnt = howmany(*size, sblock->fs_fsize);
3714777Smckusic 		else
3725329Smckusic 			cnt = NINDIR(sblock) * sblock->fs_frag;
3735329Smckusic 		*size -= NINDIR(sblock) * sblock->fs_bsize;
37446792Smckusick 		blksout(&idblk[0], cnt, ino);
3754777Smckusic 		return;
3761426Sroot 	}
37754070Smckusick 	ind_level--;
3785329Smckusic 	for (i = 0; i < NINDIR(sblock); i++) {
37954070Smckusick 		dmpindir(ino, idblk[i], ind_level, size);
3804777Smckusic 		if (*size <= 0)
3814777Smckusic 			return;
3824777Smckusic 	}
3831426Sroot }
3841426Sroot 
38546792Smckusick /*
38646792Smckusick  * Collect up the data into tape record sized buffers and output them.
38746792Smckusick  */
38846584Storek void
38946792Smckusick blksout(blkp, frags, ino)
3904777Smckusic 	daddr_t *blkp;
3914777Smckusic 	int frags;
39246792Smckusick 	ino_t ino;
3934777Smckusic {
39446584Storek 	register daddr_t *bp;
3955329Smckusic 	int i, j, count, blks, tbperdb;
3964777Smckusic 
3979403Smckusick 	blks = howmany(frags * sblock->fs_fsize, TP_BSIZE);
39846584Storek 	tbperdb = sblock->fs_bsize >> tp_bshift;
3994777Smckusic 	for (i = 0; i < blks; i += TP_NINDIR) {
4004777Smckusic 		if (i + TP_NINDIR > blks)
4014777Smckusic 			count = blks;
4024777Smckusic 		else
4034777Smckusic 			count = i + TP_NINDIR;
4044777Smckusic 		for (j = i; j < count; j++)
4055329Smckusic 			if (blkp[j / tbperdb] != 0)
4064777Smckusic 				spcl.c_addr[j - i] = 1;
4074777Smckusic 			else
4084777Smckusic 				spcl.c_addr[j - i] = 0;
4094777Smckusic 		spcl.c_count = count - i;
41046792Smckusick 		writeheader(ino);
41146584Storek 		bp = &blkp[i / tbperdb];
41246584Storek 		for (j = i; j < count; j += tbperdb, bp++)
41346584Storek 			if (*bp != 0)
4145329Smckusic 				if (j + tbperdb <= count)
41554070Smckusick 					dumpblock(*bp, (int)sblock->fs_bsize);
4164777Smckusic 				else
41746792Smckusick 					dumpblock(*bp, (count - j) * TP_BSIZE);
4184777Smckusic 		spcl.c_type = TS_ADDR;
4194777Smckusic 	}
4204777Smckusic }
4214777Smckusic 
42246792Smckusick /*
42346792Smckusick  * Dump a map to the tape.
42446792Smckusick  */
42546584Storek void
42646792Smckusick dumpmap(map, type, ino)
4275329Smckusic 	char *map;
42846792Smckusick 	int type;
42946792Smckusick 	ino_t ino;
4301426Sroot {
43146792Smckusick 	register int i;
4321426Sroot 	char *cp;
4331426Sroot 
43446792Smckusick 	spcl.c_type = type;
43546792Smckusick 	spcl.c_count = howmany(mapsize * sizeof(char), TP_BSIZE);
43646792Smckusick 	writeheader(ino);
4375329Smckusic 	for (i = 0, cp = map; i < spcl.c_count; i++, cp += TP_BSIZE)
43854596Smckusick 		writerec(cp, 0);
4391426Sroot }
4401426Sroot 
44146792Smckusick /*
44246792Smckusick  * Write a header record to the dump tape.
44346792Smckusick  */
44446584Storek void
44546792Smckusick writeheader(ino)
44646792Smckusick 	ino_t ino;
4471426Sroot {
44846792Smckusick 	register long sum, cnt, *lp;
4491426Sroot 
4501426Sroot 	spcl.c_inumber = ino;
4518368Smckusick 	spcl.c_magic = NFS_MAGIC;
4521426Sroot 	spcl.c_checksum = 0;
45346792Smckusick 	lp = (long *)&spcl;
45446792Smckusick 	sum = 0;
45546792Smckusick 	cnt = sizeof(union u_spcl) / (4 * sizeof(long));
45646792Smckusick 	while (--cnt >= 0) {
45746792Smckusick 		sum += *lp++;
45846792Smckusick 		sum += *lp++;
45946792Smckusick 		sum += *lp++;
46046792Smckusick 		sum += *lp++;
46124168Smckusick 	}
46246792Smckusick 	spcl.c_checksum = CHECKSUM - sum;
46354596Smckusick 	writerec((char *)&spcl, 1);
4641426Sroot }
4651426Sroot 
4664702Smckusic struct dinode *
46746792Smckusick getino(inum)
46846792Smckusick 	ino_t inum;
4694702Smckusic {
4704702Smckusic 	static daddr_t minino, maxino;
47146792Smckusick 	static struct dinode inoblock[MAXINOPB];
4724702Smckusic 
47346792Smckusick 	curino = inum;
47446792Smckusick 	if (inum >= minino && inum < maxino)
47546792Smckusick 		return (&inoblock[inum - minino]);
47654070Smckusick 	bread(fsbtodb(sblock, itod(sblock, inum)), (char *)inoblock,
47754070Smckusick 	    (int)sblock->fs_bsize);
47846792Smckusick 	minino = inum - (inum % INOPB(sblock));
4795329Smckusic 	maxino = minino + INOPB(sblock);
48046792Smckusick 	return (&inoblock[inum - minino]);
4814702Smckusic }
4824702Smckusic 
48346792Smckusick /*
48446792Smckusick  * Read a chunk of data from the disk.
48546792Smckusick  * Try to recover from hard errors by reading in sector sized pieces.
48646792Smckusick  * Error recovery is attempted at most BREADEMAX times before seeking
48746792Smckusick  * consent from the operator to continue.
48846792Smckusick  */
4891426Sroot int	breaderrors = 0;
4901426Sroot #define	BREADEMAX 32
4911426Sroot 
49246584Storek void
49346792Smckusick bread(blkno, buf, size)
49446792Smckusick 	daddr_t blkno;
49546792Smckusick 	char *buf;
49646792Smckusick 	int size;
4971426Sroot {
49846792Smckusick 	int cnt, i;
49934359Skarels 	extern int errno;
5001426Sroot 
50115095Smckusick loop:
50254070Smckusick 	if ((int)lseek(diskfd, ((off_t)blkno << dev_bshift), 0) < 0)
5031426Sroot 		msg("bread: lseek fails\n");
50446792Smckusick 	if ((cnt = read(diskfd, buf, size)) == size)
50515095Smckusick 		return;
50646792Smckusick 	if (blkno + (size / dev_bsize) > fsbtodb(sblock, sblock->fs_size)) {
50715095Smckusick 		/*
50815095Smckusick 		 * Trying to read the final fragment.
50915095Smckusick 		 *
51015095Smckusick 		 * NB - dump only works in TP_BSIZE blocks, hence
51130560Smckusick 		 * rounds `dev_bsize' fragments up to TP_BSIZE pieces.
51215095Smckusick 		 * It should be smarter about not actually trying to
51315095Smckusick 		 * read more than it can get, but for the time being
51415095Smckusick 		 * we punt and scale back the read only when it gets
51515095Smckusick 		 * us into trouble. (mkm 9/25/83)
51615095Smckusick 		 */
51746792Smckusick 		size -= dev_bsize;
51815095Smckusick 		goto loop;
5191426Sroot 	}
52050905Smckusick 	if (cnt == -1)
52150905Smckusick 		msg("read error from %s: %s: [block %d]: count=%d\n",
52250905Smckusick 			disk, strerror(errno), blkno, size);
52350905Smckusick 	else
52450905Smckusick 		msg("short read error from %s: [block %d]: count=%d, got=%d\n",
52550905Smckusick 			disk, blkno, size, cnt);
52646584Storek 	if (++breaderrors > BREADEMAX) {
52715095Smckusick 		msg("More than %d block read errors from %d\n",
52815095Smckusick 			BREADEMAX, disk);
52915095Smckusick 		broadcast("DUMP IS AILING!\n");
53015095Smckusick 		msg("This is an unrecoverable error.\n");
53115095Smckusick 		if (!query("Do you want to attempt to continue?")){
53255288Sbostic 			dumpabort(0);
53315095Smckusick 			/*NOTREACHED*/
53415095Smckusick 		} else
53515095Smckusick 			breaderrors = 0;
53615095Smckusick 	}
53734359Skarels 	/*
53834359Skarels 	 * Zero buffer, then try to read each sector of buffer separately.
53934359Skarels 	 */
54046792Smckusick 	bzero(buf, size);
54146792Smckusick 	for (i = 0; i < size; i += dev_bsize, buf += dev_bsize, blkno++) {
54254070Smckusick 		if ((int)lseek(diskfd, ((off_t)blkno << dev_bshift), 0) < 0)
54334359Skarels 			msg("bread: lseek2 fails!\n");
54454070Smckusick 		if ((cnt = read(diskfd, buf, (int)dev_bsize)) == dev_bsize)
54550905Smckusick 			continue;
54650905Smckusick 		if (cnt == -1) {
54750905Smckusick 			msg("read error from %s: %s: [sector %d]: count=%d\n",
54850905Smckusick 				disk, strerror(errno), blkno, dev_bsize);
54950905Smckusick 			continue;
55050905Smckusick 		}
55150905Smckusick 		msg("short read error from %s: [sector %d]: count=%d, got=%d\n",
55250905Smckusick 			disk, blkno, dev_bsize, cnt);
55334359Skarels 	}
5541426Sroot }
555