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