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