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