Home | History | Annotate | Line # | Download | only in dump
traverse.c revision 1.18
      1 /*	$NetBSD: traverse.c,v 1.18 1997/09/15 07:58:09 lukem Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1980, 1988, 1991, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by the University of
     18  *	California, Berkeley and its contributors.
     19  * 4. Neither the name of the University nor the names of its contributors
     20  *    may be used to endorse or promote products derived from this software
     21  *    without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  * SUCH DAMAGE.
     34  */
     35 
     36 #include <sys/cdefs.h>
     37 #ifndef lint
     38 #if 0
     39 static char sccsid[] = "@(#)traverse.c	8.2 (Berkeley) 9/23/93";
     40 #else
     41 __RCSID("$NetBSD: traverse.c,v 1.18 1997/09/15 07:58:09 lukem Exp $");
     42 #endif
     43 #endif /* not lint */
     44 
     45 #include <sys/param.h>
     46 #include <sys/time.h>
     47 #include <sys/stat.h>
     48 #ifdef sunos
     49 #include <sys/vnode.h>
     50 
     51 #include <ufs/fs.h>
     52 #include <ufs/fsdir.h>
     53 #include <ufs/inode.h>
     54 #else
     55 #include <ufs/ffs/fs.h>
     56 #include <ufs/ufs/dir.h>
     57 #include <ufs/ufs/dinode.h>
     58 #endif
     59 
     60 #include <protocols/dumprestore.h>
     61 
     62 #include <ctype.h>
     63 #include <errno.h>
     64 #include <fts.h>
     65 #include <stdio.h>
     66 #ifdef __STDC__
     67 #include <string.h>
     68 #include <unistd.h>
     69 #endif
     70 
     71 #include "dump.h"
     72 
     73 #define	HASDUMPEDFILE	0x1
     74 #define	HASSUBDIRS	0x2
     75 
     76 #ifdef	FS_44INODEFMT
     77 typedef	quad_t fsizeT;
     78 #else
     79 typedef	int32_t fsizeT;
     80 #endif
     81 
     82 static	int dirindir __P((ino_t ino, daddr_t blkno, int level, long *size));
     83 static	void dmpindir __P((ino_t ino, daddr_t blk, int level, fsizeT *size));
     84 static	int searchdir __P((ino_t ino, daddr_t blkno, long size, long filesize));
     85 
     86 /*
     87  * This is an estimation of the number of TP_BSIZE blocks in the file.
     88  * It estimates the number of blocks in files with holes by assuming
     89  * that all of the blocks accounted for by di_blocks are data blocks
     90  * (when some of the blocks are usually used for indirect pointers);
     91  * hence the estimate may be high.
     92  */
     93 long
     94 blockest(dp)
     95 	struct dinode *dp;
     96 {
     97 	long blkest, sizeest;
     98 
     99 	/*
    100 	 * dp->di_size is the size of the file in bytes.
    101 	 * dp->di_blocks stores the number of sectors actually in the file.
    102 	 * If there are more sectors than the size would indicate, this just
    103 	 *	means that there are indirect blocks in the file or unused
    104 	 *	sectors in the last file block; we can safely ignore these
    105 	 *	(blkest = sizeest below).
    106 	 * If the file is bigger than the number of sectors would indicate,
    107 	 *	then the file has holes in it.	In this case we must use the
    108 	 *	block count to estimate the number of data blocks used, but
    109 	 *	we use the actual size for estimating the number of indirect
    110 	 *	dump blocks (sizeest vs. blkest in the indirect block
    111 	 *	calculation).
    112 	 */
    113 	blkest = howmany(dbtob(dp->di_blocks), TP_BSIZE);
    114 	sizeest = howmany(dp->di_size, TP_BSIZE);
    115 	if (blkest > sizeest)
    116 		blkest = sizeest;
    117 	if (dp->di_size > sblock->fs_bsize * NDADDR) {
    118 		/* calculate the number of indirect blocks on the dump tape */
    119 		blkest +=
    120 			howmany(sizeest - NDADDR * sblock->fs_bsize / TP_BSIZE,
    121 			TP_NINDIR);
    122 	}
    123 	return (blkest + 1);
    124 }
    125 
    126 /* Auxiliary macro to pick up files changed since previous dump. */
    127 #define	CHANGEDSINCE(dp, t) \
    128 	((dp)->di_mtime >= (t) || (dp)->di_ctime >= (t))
    129 
    130 /* The WANTTODUMP macro decides whether a file should be dumped. */
    131 #ifdef UF_NODUMP
    132 #define	WANTTODUMP(dp) \
    133 	(CHANGEDSINCE(dp, spcl.c_ddate) && \
    134 	 (nonodump || ((dp)->di_flags & UF_NODUMP) != UF_NODUMP))
    135 #else
    136 #define	WANTTODUMP(dp) CHANGEDSINCE(dp, spcl.c_ddate)
    137 #endif
    138 
    139 /*
    140  * Determine if given inode should be dumped
    141  */
    142 void
    143 mapfileino(ino, tapesize, dirskipped)
    144 	ino_t ino;
    145 	long *tapesize;
    146 	int *dirskipped;
    147 {
    148 	int mode;
    149 	struct dinode *dp;
    150 
    151 	dp = getino(ino);
    152 	if ((mode = (dp->di_mode & IFMT)) == 0)
    153 		return;
    154 	SETINO(ino, usedinomap);
    155 	if (mode == IFDIR)
    156 		SETINO(ino, dumpdirmap);
    157 	if (WANTTODUMP(dp)) {
    158 		SETINO(ino, dumpinomap);
    159 		if (mode != IFREG && mode != IFDIR && mode != IFLNK)
    160 			*tapesize += 1;
    161 		else
    162 			*tapesize += blockest(dp);
    163 		return;
    164 	}
    165 	if (mode == IFDIR)
    166 		*dirskipped = 1;
    167 }
    168 
    169 /*
    170  * Dump pass 1.
    171  *
    172  * Walk the inode list for a filesystem to find all allocated inodes
    173  * that have been modified since the previous dump time. Also, find all
    174  * the directories in the filesystem.
    175  */
    176 int
    177 mapfiles(maxino, tapesize, disk, dirv)
    178 	ino_t maxino;
    179 	long *tapesize;
    180 	char *disk;
    181 	char * const *dirv;
    182 {
    183 	int anydirskipped = 0;
    184 
    185 	if (dirv != NULL) {
    186 		char	 curdir[MAXPATHLEN];
    187 		FTS	*dirh;
    188 		FTSENT	*entry;
    189 		int	 d;
    190 
    191 		if (getcwd(curdir, sizeof(curdir)) == NULL) {
    192 			msg("Can't determine cwd: %s\n", strerror(errno));
    193 			dumpabort(0);
    194 		}
    195 		if ((dirh = fts_open(dirv, FTS_PHYSICAL|FTS_SEEDOT|FTS_XDEV,
    196 		    		    NULL)) == NULL) {
    197 			msg("fts_open failed: %s\n", strerror(errno));
    198 			dumpabort(0);
    199 		}
    200 		while ((entry = fts_read(dirh)) != NULL) {
    201 			switch (entry->fts_info) {
    202 			case FTS_DNR:		/* an error */
    203 			case FTS_ERR:
    204 			case FTS_NS:
    205 				msg("Can't fts_read %s: %s\n", entry->fts_path,
    206 				    strerror(errno));
    207 			case FTS_DP:		/* already seen dir */
    208 				continue;
    209 			}
    210 			mapfileino(entry->fts_statp->st_ino, tapesize,
    211 			    &anydirskipped);
    212 		}
    213 		(void)fts_close(dirh);
    214 
    215 		/*
    216 		 * Add any parent directories
    217 		 */
    218 		for (d = 0 ; dirv[d] != NULL ; d++) {
    219 			char path[MAXPATHLEN];
    220 
    221 			if (dirv[d][0] != '/')
    222 				(void)snprintf(path, sizeof(path), "%s/%s",
    223 				    curdir, dirv[d]);
    224 			else
    225 				(void)snprintf(path, sizeof(path), "%s",
    226 				    dirv[d]);
    227 			while (strcmp(path, disk) != 0) {
    228 				char *p;
    229 				struct stat sb;
    230 
    231 				if (*path == '\0')
    232 					break;
    233 				if ((p = strrchr(path, '/')) == NULL)
    234 					break;
    235 				if (p == path)
    236 					break;
    237 				*p = '\0';
    238 				if (stat(path, &sb) == -1) {
    239 					msg("Can't stat %s: %s\n", path,
    240 					    strerror(errno));
    241 					break;
    242 				}
    243 				mapfileino(sb.st_ino, tapesize, &anydirskipped);
    244 			}
    245 		}
    246 
    247 		/*
    248 		 * Ensure that the root inode actually appears in the
    249 		 * file list for a subdir
    250 		 */
    251 		mapfileino(ROOTINO, tapesize, &anydirskipped);
    252 	} else {
    253 		ino_t ino;
    254 
    255 		for (ino = ROOTINO; ino < maxino; ino++) {
    256 			mapfileino(ino, tapesize, &anydirskipped);
    257 		}
    258 	}
    259 	/*
    260 	 * Restore gets very upset if the root is not dumped,
    261 	 * so ensure that it always is dumped.
    262 	 */
    263 	SETINO(ROOTINO, dumpinomap);
    264 	return (anydirskipped);
    265 }
    266 
    267 /*
    268  * Dump pass 2.
    269  *
    270  * Scan each directory on the filesystem to see if it has any modified
    271  * files in it. If it does, and has not already been added to the dump
    272  * list (because it was itself modified), then add it. If a directory
    273  * has not been modified itself, contains no modified files and has no
    274  * subdirectories, then it can be deleted from the dump list and from
    275  * the list of directories. By deleting it from the list of directories,
    276  * its parent may now qualify for the same treatment on this or a later
    277  * pass using this algorithm.
    278  */
    279 int
    280 mapdirs(maxino, tapesize)
    281 	ino_t maxino;
    282 	long *tapesize;
    283 {
    284 	struct	dinode *dp;
    285 	int i, isdir;
    286 	char *map;
    287 	ino_t ino;
    288 	long filesize;
    289 	int ret, change = 0;
    290 
    291 	isdir = 0;		/* XXX just to get gcc to shut up */
    292 	for (map = dumpdirmap, ino = 1; ino < maxino; ino++) {
    293 		if (((ino - 1) % NBBY) == 0)	/* map is offset by 1 */
    294 			isdir = *map++;
    295 		else
    296 			isdir >>= 1;
    297 		if ((isdir & 1) == 0 || TSTINO(ino, dumpinomap))
    298 			continue;
    299 		dp = getino(ino);
    300 		filesize = dp->di_size;
    301 		for (ret = 0, i = 0; filesize > 0 && i < NDADDR; i++) {
    302 			if (dp->di_db[i] != 0)
    303 				ret |= searchdir(ino, dp->di_db[i],
    304 					(long)dblksize(sblock, dp, i),
    305 					filesize);
    306 			if (ret & HASDUMPEDFILE)
    307 				filesize = 0;
    308 			else
    309 				filesize -= sblock->fs_bsize;
    310 		}
    311 		for (i = 0; filesize > 0 && i < NIADDR; i++) {
    312 			if (dp->di_ib[i] == 0)
    313 				continue;
    314 			ret |= dirindir(ino, dp->di_ib[i], i, &filesize);
    315 		}
    316 		if (ret & HASDUMPEDFILE) {
    317 			SETINO(ino, dumpinomap);
    318 			*tapesize += blockest(dp);
    319 			change = 1;
    320 			continue;
    321 		}
    322 		if ((ret & HASSUBDIRS) == 0) {
    323 			if (!TSTINO(ino, dumpinomap)) {
    324 				CLRINO(ino, dumpdirmap);
    325 				change = 1;
    326 			}
    327 		}
    328 	}
    329 	return (change);
    330 }
    331 
    332 /*
    333  * Read indirect blocks, and pass the data blocks to be searched
    334  * as directories. Quit as soon as any entry is found that will
    335  * require the directory to be dumped.
    336  */
    337 static int
    338 dirindir(ino, blkno, ind_level, filesize)
    339 	ino_t ino;
    340 	daddr_t blkno;
    341 	int ind_level;
    342 	long *filesize;
    343 {
    344 	int ret = 0;
    345 	int i;
    346 	daddr_t	idblk[MAXNINDIR];
    347 
    348 	bread(fsbtodb(sblock, blkno), (char *)idblk, (int)sblock->fs_bsize);
    349 	if (ind_level <= 0) {
    350 		for (i = 0; *filesize > 0 && i < NINDIR(sblock); i++) {
    351 			blkno = idblk[i];
    352 			if (blkno != 0)
    353 				ret |= searchdir(ino, blkno, sblock->fs_bsize,
    354 					*filesize);
    355 			if (ret & HASDUMPEDFILE)
    356 				*filesize = 0;
    357 			else
    358 				*filesize -= sblock->fs_bsize;
    359 		}
    360 		return (ret);
    361 	}
    362 	ind_level--;
    363 	for (i = 0; *filesize > 0 && i < NINDIR(sblock); i++) {
    364 		blkno = idblk[i];
    365 		if (blkno != 0)
    366 			ret |= dirindir(ino, blkno, ind_level, filesize);
    367 	}
    368 	return (ret);
    369 }
    370 
    371 /*
    372  * Scan a disk block containing directory information looking to see if
    373  * any of the entries are on the dump list and to see if the directory
    374  * contains any subdirectories.
    375  */
    376 static int
    377 searchdir(ino, blkno, size, filesize)
    378 	ino_t ino;
    379 	daddr_t blkno;
    380 	long size;
    381 	long filesize;
    382 {
    383 	struct direct *dp;
    384 	long loc, ret = 0;
    385 	char dblk[MAXBSIZE];
    386 
    387 	bread(fsbtodb(sblock, blkno), dblk, (int)size);
    388 	if (filesize < size)
    389 		size = filesize;
    390 	for (loc = 0; loc < size; ) {
    391 		dp = (struct direct *)(dblk + loc);
    392 		if (dp->d_reclen == 0) {
    393 			msg("corrupted directory, inumber %d\n", ino);
    394 			break;
    395 		}
    396 		loc += dp->d_reclen;
    397 		if (dp->d_ino == 0)
    398 			continue;
    399 		if (dp->d_name[0] == '.') {
    400 			if (dp->d_name[1] == '\0')
    401 				continue;
    402 			if (dp->d_name[1] == '.' && dp->d_name[2] == '\0')
    403 				continue;
    404 		}
    405 		if (TSTINO(dp->d_ino, dumpinomap)) {
    406 			ret |= HASDUMPEDFILE;
    407 			if (ret & HASSUBDIRS)
    408 				break;
    409 		}
    410 		if (TSTINO(dp->d_ino, dumpdirmap)) {
    411 			ret |= HASSUBDIRS;
    412 			if (ret & HASDUMPEDFILE)
    413 				break;
    414 		}
    415 	}
    416 	return (ret);
    417 }
    418 
    419 /*
    420  * Dump passes 3 and 4.
    421  *
    422  * Dump the contents of an inode to tape.
    423  */
    424 void
    425 dumpino(dp, ino)
    426 	struct dinode *dp;
    427 	ino_t ino;
    428 {
    429 	int ind_level, cnt;
    430 	fsizeT size;
    431 	char buf[TP_BSIZE];
    432 
    433 	if (newtape) {
    434 		newtape = 0;
    435 		dumpmap(dumpinomap, TS_BITS, ino);
    436 	}
    437 	CLRINO(ino, dumpinomap);
    438 	spcl.c_dinode = *dp;
    439 	spcl.c_type = TS_INODE;
    440 	spcl.c_count = 0;
    441 	switch (dp->di_mode & IFMT) {
    442 
    443 	case 0:
    444 		/*
    445 		 * Freed inode.
    446 		 */
    447 		return;
    448 
    449 	case IFLNK:
    450 		/*
    451 		 * Check for short symbolic link.
    452 		 */
    453 		if (dp->di_size > 0 &&
    454 #ifdef FS_44INODEFMT
    455 		    (dp->di_size < sblock->fs_maxsymlinklen ||
    456 		     (sblock->fs_maxsymlinklen == 0 && dp->di_blocks == 0))) {
    457 #else
    458 		    dp->di_blocks == 0) {
    459 #endif
    460 			spcl.c_addr[0] = 1;
    461 			spcl.c_count = 1;
    462 			writeheader(ino);
    463 			memcpy(buf, dp->di_shortlink, (u_long)dp->di_size);
    464 			buf[dp->di_size] = '\0';
    465 			writerec(buf, 0);
    466 			return;
    467 		}
    468 		/* fall through */
    469 
    470 	case IFDIR:
    471 	case IFREG:
    472 		if (dp->di_size > 0)
    473 			break;
    474 		/* fall through */
    475 
    476 	case IFIFO:
    477 	case IFSOCK:
    478 	case IFCHR:
    479 	case IFBLK:
    480 		writeheader(ino);
    481 		return;
    482 
    483 	default:
    484 		msg("Warning: undefined file type 0%o\n", dp->di_mode & IFMT);
    485 		return;
    486 	}
    487 	if (dp->di_size > NDADDR * sblock->fs_bsize)
    488 		cnt = NDADDR * sblock->fs_frag;
    489 	else
    490 		cnt = howmany(dp->di_size, sblock->fs_fsize);
    491 	blksout(&dp->di_db[0], cnt, ino);
    492 	if ((size = dp->di_size - NDADDR * sblock->fs_bsize) <= 0)
    493 		return;
    494 	for (ind_level = 0; ind_level < NIADDR; ind_level++) {
    495 		dmpindir(ino, dp->di_ib[ind_level], ind_level, &size);
    496 		if (size <= 0)
    497 			return;
    498 	}
    499 }
    500 
    501 /*
    502  * Read indirect blocks, and pass the data blocks to be dumped.
    503  */
    504 static void
    505 dmpindir(ino, blk, ind_level, size)
    506 	ino_t ino;
    507 	daddr_t blk;
    508 	int ind_level;
    509 	fsizeT *size;
    510 {
    511 	int i, cnt;
    512 	daddr_t idblk[MAXNINDIR];
    513 
    514 	if (blk != 0)
    515 		bread(fsbtodb(sblock, blk), (char *)idblk, (int) sblock->fs_bsize);
    516 	else
    517 		memset(idblk, 0, (int)sblock->fs_bsize);
    518 	if (ind_level <= 0) {
    519 		if (*size < NINDIR(sblock) * sblock->fs_bsize)
    520 			cnt = howmany(*size, sblock->fs_fsize);
    521 		else
    522 			cnt = NINDIR(sblock) * sblock->fs_frag;
    523 		*size -= NINDIR(sblock) * sblock->fs_bsize;
    524 		blksout(&idblk[0], cnt, ino);
    525 		return;
    526 	}
    527 	ind_level--;
    528 	for (i = 0; i < NINDIR(sblock); i++) {
    529 		dmpindir(ino, idblk[i], ind_level, size);
    530 		if (*size <= 0)
    531 			return;
    532 	}
    533 }
    534 
    535 /*
    536  * Collect up the data into tape record sized buffers and output them.
    537  */
    538 void
    539 blksout(blkp, frags, ino)
    540 	daddr_t *blkp;
    541 	int frags;
    542 	ino_t ino;
    543 {
    544 	daddr_t *bp;
    545 	int i, j, count, blks, tbperdb;
    546 
    547 	blks = howmany(frags * sblock->fs_fsize, TP_BSIZE);
    548 	tbperdb = sblock->fs_bsize >> tp_bshift;
    549 	for (i = 0; i < blks; i += TP_NINDIR) {
    550 		if (i + TP_NINDIR > blks)
    551 			count = blks;
    552 		else
    553 			count = i + TP_NINDIR;
    554 		for (j = i; j < count; j++)
    555 			if (blkp[j / tbperdb] != 0)
    556 				spcl.c_addr[j - i] = 1;
    557 			else
    558 				spcl.c_addr[j - i] = 0;
    559 		spcl.c_count = count - i;
    560 		writeheader(ino);
    561 		bp = &blkp[i / tbperdb];
    562 		for (j = i; j < count; j += tbperdb, bp++)
    563 			if (*bp != 0)
    564 				if (j + tbperdb <= count)
    565 					dumpblock(*bp, (int)sblock->fs_bsize);
    566 				else
    567 					dumpblock(*bp, (count - j) * TP_BSIZE);
    568 		spcl.c_type = TS_ADDR;
    569 	}
    570 }
    571 
    572 /*
    573  * Dump a map to the tape.
    574  */
    575 void
    576 dumpmap(map, type, ino)
    577 	char *map;
    578 	int type;
    579 	ino_t ino;
    580 {
    581 	int i;
    582 	char *cp;
    583 
    584 	spcl.c_type = type;
    585 	spcl.c_count = howmany(mapsize * sizeof(char), TP_BSIZE);
    586 	writeheader(ino);
    587 	for (i = 0, cp = map; i < spcl.c_count; i++, cp += TP_BSIZE)
    588 		writerec(cp, 0);
    589 }
    590 
    591 /*
    592  * Write a header record to the dump tape.
    593  */
    594 void
    595 writeheader(ino)
    596 	ino_t ino;
    597 {
    598 	int32_t sum, cnt, *lp;
    599 
    600 	spcl.c_inumber = ino;
    601 	spcl.c_magic = NFS_MAGIC;
    602 	spcl.c_checksum = 0;
    603 	lp = (int32_t *)&spcl;
    604 	sum = 0;
    605 	cnt = sizeof(union u_spcl) / (4 * sizeof(int32_t));
    606 	while (--cnt >= 0) {
    607 		sum += *lp++;
    608 		sum += *lp++;
    609 		sum += *lp++;
    610 		sum += *lp++;
    611 	}
    612 	spcl.c_checksum = CHECKSUM - sum;
    613 	writerec((char *)&spcl, 1);
    614 }
    615 
    616 struct dinode *
    617 getino(inum)
    618 	ino_t inum;
    619 {
    620 	static daddr_t minino, maxino;
    621 	static struct dinode inoblock[MAXINOPB];
    622 
    623 	curino = inum;
    624 	if (inum >= minino && inum < maxino)
    625 		return (&inoblock[inum - minino]);
    626 	bread(fsbtodb(sblock, ino_to_fsba(sblock, inum)), (char *)inoblock,
    627 	    (int)sblock->fs_bsize);
    628 	minino = inum - (inum % INOPB(sblock));
    629 	maxino = minino + INOPB(sblock);
    630 	return (&inoblock[inum - minino]);
    631 }
    632 
    633 /*
    634  * Read a chunk of data from the disk.
    635  * Try to recover from hard errors by reading in sector sized pieces.
    636  * Error recovery is attempted at most BREADEMAX times before seeking
    637  * consent from the operator to continue.
    638  */
    639 int	breaderrors = 0;
    640 #define	BREADEMAX 32
    641 
    642 void
    643 bread(blkno, buf, size)
    644 	daddr_t blkno;
    645 	char *buf;
    646 	int size;
    647 {
    648 	int cnt, i;
    649 	extern int errno;
    650 
    651 loop:
    652 	if (lseek(diskfd, ((off_t)blkno << dev_bshift), 0) < 0)
    653 		msg("bread: lseek fails\n");
    654 	if ((cnt = read(diskfd, buf, size)) == size)
    655 		return;
    656 	if (blkno + (size / dev_bsize) > fsbtodb(sblock, sblock->fs_size)) {
    657 		/*
    658 		 * Trying to read the final fragment.
    659 		 *
    660 		 * NB - dump only works in TP_BSIZE blocks, hence
    661 		 * rounds `dev_bsize' fragments up to TP_BSIZE pieces.
    662 		 * It should be smarter about not actually trying to
    663 		 * read more than it can get, but for the time being
    664 		 * we punt and scale back the read only when it gets
    665 		 * us into trouble. (mkm 9/25/83)
    666 		 */
    667 		size -= dev_bsize;
    668 		goto loop;
    669 	}
    670 	if (cnt == -1)
    671 		msg("read error from %s: %s: [block %d]: count=%d\n",
    672 			disk, strerror(errno), blkno, size);
    673 	else
    674 		msg("short read error from %s: [block %d]: count=%d, got=%d\n",
    675 			disk, blkno, size, cnt);
    676 	if (++breaderrors > BREADEMAX) {
    677 		msg("More than %d block read errors from %d\n",
    678 			BREADEMAX, disk);
    679 		broadcast("DUMP IS AILING!\n");
    680 		msg("This is an unrecoverable error.\n");
    681 		if (!query("Do you want to attempt to continue?")){
    682 			dumpabort(0);
    683 			/*NOTREACHED*/
    684 		} else
    685 			breaderrors = 0;
    686 	}
    687 	/*
    688 	 * Zero buffer, then try to read each sector of buffer separately.
    689 	 */
    690 	memset(buf, 0, size);
    691 	for (i = 0; i < size; i += dev_bsize, buf += dev_bsize, blkno++) {
    692 		if (lseek(diskfd, ((off_t)blkno << dev_bshift), 0) < 0)
    693 			msg("bread: lseek2 fails!\n");
    694 		if ((cnt = read(diskfd, buf, (int)dev_bsize)) == dev_bsize)
    695 			continue;
    696 		if (cnt == -1) {
    697 			msg("read error from %s: %s: [sector %d]: count=%d\n",
    698 				disk, strerror(errno), blkno, dev_bsize);
    699 			continue;
    700 		}
    701 		msg("short read error from %s: [sector %d]: count=%d, got=%d\n",
    702 			disk, blkno, dev_bsize, cnt);
    703 	}
    704 }
    705