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