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