1139823Simp/*-
21541Srgrimes * Copyright (c) 1980, 1988, 1991, 1993
31541Srgrimes *	The Regents of the University of California.  All rights reserved.
41541Srgrimes *
51541Srgrimes * Redistribution and use in source and binary forms, with or without
61541Srgrimes * modification, are permitted provided that the following conditions
71541Srgrimes * are met:
81541Srgrimes * 1. Redistributions of source code must retain the above copyright
91541Srgrimes *    notice, this list of conditions and the following disclaimer.
101541Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111541Srgrimes *    notice, this list of conditions and the following disclaimer in the
121541Srgrimes *    documentation and/or other materials provided with the distribution.
131541Srgrimes * 4. Neither the name of the University nor the names of its contributors
141541Srgrimes *    may be used to endorse or promote products derived from this software
151541Srgrimes *    without specific prior written permission.
161541Srgrimes *
171541Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
181541Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
191541Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
201541Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
211541Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
221541Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
231541Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
241541Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
251541Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
261541Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
271541Srgrimes * SUCH DAMAGE.
281541Srgrimes */
298152Spst
3050477Speter#ifndef lint
311541Srgrimes#if 0
321541Srgrimesstatic char sccsid[] = "@(#)traverse.c	8.7 (Berkeley) 6/15/95";
338152Spst#endif
348152Spststatic const char rcsid[] =
351541Srgrimes  "$FreeBSD$";
36110527Shsu#endif /* not lint */
37110527Shsu
38110527Shsu#include <sys/param.h>
39185747Skmacy#include <sys/stat.h>
40110527Shsu
41108250Shsu#include <ufs/ufs/dir.h>
4230354Sphk#include <ufs/ufs/dinode.h>
4330354Sphk#include <ufs/ffs/fs.h>
4430354Sphk
4530354Sphk#include <protocols/dumprestore.h>
461541Srgrimes
471541Srgrimes#include <ctype.h>
481541Srgrimes#include <errno.h>
491541Srgrimes#include <inttypes.h>
501541Srgrimes#include <limits.h>
511541Srgrimes#include <stdio.h>
5259529Swollman#include <stdlib.h>
5359529Swollman#include <string.h>
541541Srgrimes#include <timeconv.h>
551541Srgrimes#include <unistd.h>
561541Srgrimes
571541Srgrimes#include "dump.h"
581541Srgrimes
591541Srgrimesunion dinode {
601541Srgrimes	struct ufs1_dinode dp1;
618152Spst	struct ufs2_dinode dp2;
621541Srgrimes};
631541Srgrimes#define	DIP(dp, field) \
641541Srgrimes	((sblock->fs_magic == FS_UFS1_MAGIC) ? \
651541Srgrimes	(dp)->dp1.field : (dp)->dp2.field)
661541Srgrimes#define DIP_SET(dp, field, val) do {\
671541Srgrimes	if (sblock->fs_magic == FS_UFS1_MAGIC) \
681541Srgrimes		(dp)->dp1.field = (val); \
698152Spst	else \
701541Srgrimes		(dp)->dp2.field = (val); \
711541Srgrimes	} while (0)
721541Srgrimes
731541Srgrimes#define	HASDUMPEDFILE	0x1
741541Srgrimes#define	HASSUBDIRS	0x2
751541Srgrimes
761541Srgrimesstatic	int dirindir(ino_t ino, ufs2_daddr_t blkno, int level, long *size,
771541Srgrimes    long *tapesize, int nodump, ino_t maxino);
7859529Swollmanstatic	void dmpindir(union dinode *dp, ino_t ino, ufs2_daddr_t blk, int level,
7959529Swollman    off_t *size);
8059529Swollmanstatic	void ufs1_blksout(ufs1_daddr_t *blkp, int frags, ino_t ino);
8159529Swollmanstatic	void ufs2_blksout(union dinode *dp, ufs2_daddr_t *blkp, int frags,
8259529Swollman    ino_t ino, int last);
8359529Swollmanstatic	int appendextdata(union dinode *dp);
841541Srgrimesstatic	void writeextdata(union dinode *dp, ino_t ino, int added);
851541Srgrimesstatic	int searchdir(ino_t ino, ufs2_daddr_t blkno, long size, long filesize,
861541Srgrimes    long *tapesize, int nodump, ino_t maxino);
871541Srgrimesstatic	long blockest(union dinode *dp);
881541Srgrimes
8929189Sbde/*
9059529Swollman * This is an estimation of the number of TP_BSIZE blocks in the file.
911541Srgrimes * It estimates the number of blocks in files with holes by assuming
921541Srgrimes * that all of the blocks accounted for by di_blocks are data blocks
931541Srgrimes * (when some of the blocks are usually used for indirect pointers);
948152Spst * hence the estimate may be high.
958152Spst */
968152Spststatic long
978152Spstblockest(union dinode *dp)
981541Srgrimes{
9929189Sbde	long blkest, sizeest;
1001541Srgrimes
10159529Swollman	/*
10259529Swollman	 * dp->di_size is the size of the file in bytes.
1038152Spst	 * dp->di_blocks stores the number of sectors actually in the file.
10492725Salfred	 * If there are more sectors than the size would indicate, this just
1054469Sbde	 *	means that there are indirect blocks in the file or unused
1061541Srgrimes	 *	sectors in the last file block; we can safely ignore these
1071541Srgrimes	 *	(blkest = sizeest below).
108225698Skmacy	 * If the file is bigger than the number of sectors would indicate,
109225698Skmacy	 *	then the file has holes in it.	In this case we must use the
1101541Srgrimes	 *	block count to estimate the number of data blocks used, but
1111541Srgrimes	 *	we use the actual size for estimating the number of indirect
1121541Srgrimes	 *	dump blocks (sizeest vs. blkest in the indirect block
11392725Salfred	 *	calculation).
11492725Salfred	 */
1151541Srgrimes	if ((DIP(dp, di_flags) & SF_SNAPSHOT) != 0)
11692725Salfred		return (1);
11792725Salfred	blkest = howmany(dbtob(DIP(dp, di_blocks)), TP_BSIZE);
1181541Srgrimes	sizeest = howmany(DIP(dp, di_size), TP_BSIZE);
11992725Salfred	if (blkest > sizeest)
1201541Srgrimes		blkest = sizeest;
12192725Salfred	if (DIP(dp, di_size) > sblock->fs_bsize * NDADDR) {
122265707Smelifaro		/* calculate the number of indirect blocks on the dump tape */
12392725Salfred		blkest +=
124265707Smelifaro			howmany(sizeest - NDADDR * sblock->fs_bsize / TP_BSIZE,
12592725Salfred			TP_NINDIR);
1261541Srgrimes	}
12792725Salfred	return (blkest + 1);
1281541Srgrimes}
12992725Salfred
1307197Swollman/* Auxiliary macro to pick up files changed since previous dump. */
13192725Salfred#define	CHANGEDSINCE(dp, t) \
13292725Salfred	(DIP(dp, di_mtime) >= (t) || DIP(dp, di_ctime) >= (t))
1334073Swollman
13492725Salfred/* The WANTTODUMP macro decides whether a file should be dumped. */
1351541Srgrimes#ifdef UF_NODUMP
136110527Shsu#define	WANTTODUMP(dp) \
137185747Skmacy	(CHANGEDSINCE(dp, spcl.c_ddate) && \
138110527Shsu	 (nonodump || (DIP(dp, di_flags) & UF_NODUMP) != UF_NODUMP))
139257389Smelifaro#else
1401541Srgrimes#define	WANTTODUMP(dp) CHANGEDSINCE(dp, spcl.c_ddate)
1411541Srgrimes#endif
14255205Speter
1431541Srgrimes/*
144119135Ssam * Dump pass 1.
1451541Srgrimes *
1461541Srgrimes * Walk the inode list for a file system to find all allocated inodes
147108107Sbmilekic * that have been modified since the previous dump time. Also, find all
148119135Ssam * the directories in the file system.
1491541Srgrimes */
150108250Shsuint
151110527Shsumapfiles(ino_t maxino, long *tapesize)
152185747Skmacy{
153185747Skmacy	int i, cg, mode, inosused;
154185747Skmacy	int anydirskipped = 0;
155185747Skmacy	union dinode *dp;
156185747Skmacy	struct cg *cgp;
157185747Skmacy	ino_t ino;
158185747Skmacy	u_char *cp;
159185747Skmacy
160185747Skmacy	if ((cgp = malloc(sblock->fs_cgsize)) == NULL)
161185747Skmacy		quit("mapfiles: cannot allocate memory.\n");
162185747Skmacy	for (cg = 0; cg < sblock->fs_ncg; cg++) {
16355205Speter		ino = cg * sblock->fs_ipg;
1641541Srgrimes		bread(fsbtodb(sblock, cgtod(sblock, cg)), (char *)cgp,
165200537Sluigi		    sblock->fs_cgsize);
16692725Salfred		if (sblock->fs_magic == FS_UFS2_MAGIC)
167204808Sbz			inosused = cgp->cg_initediblk;
16892725Salfred		else
1691541Srgrimes			inosused = sblock->fs_ipg;
17092725Salfred		/*
171257389Smelifaro		 * If we are using soft updates, then we can trust the
17292725Salfred		 * cylinder group inode allocation maps to tell us which
17392725Salfred		 * inodes are allocated. We will scan the used inode map
17492725Salfred		 * to find the inodes that are really in use, and then
17592725Salfred		 * read only those inodes in from disk.
17692725Salfred		 */
17792725Salfred		if (sblock->fs_flags & FS_DOSOFTDEP) {
1781541Srgrimes			if (!cg_chkmagic(cgp))
1798152Spst				quit("mapfiles: cg %d: bad magic number\n", cg);
180			cp = &cg_inosused(cgp)[(inosused - 1) / CHAR_BIT];
181			for ( ; inosused > 0; inosused -= CHAR_BIT, cp--) {
182				if (*cp == 0)
183					continue;
184				for (i = 1 << (CHAR_BIT - 1); i > 0; i >>= 1) {
185					if (*cp & i)
186						break;
187					inosused--;
188				}
189				break;
190			}
191			if (inosused <= 0)
192				continue;
193		}
194		for (i = 0; i < inosused; i++, ino++) {
195			if (ino < ROOTINO ||
196			    (dp = getino(ino, &mode)) == NULL ||
197			    (mode & IFMT) == 0)
198				continue;
199			if (ino >= maxino) {
200				msg("Skipping inode %d >= maxino %d\n",
201				    ino, maxino);
202				continue;
203			}
204			/*
205			 * Everything must go in usedinomap so that a check
206			 * for "in dumpdirmap but not in usedinomap" to detect
207			 * dirs with nodump set has a chance of succeeding
208			 * (this is used in mapdirs()).
209			 */
210			SETINO(ino, usedinomap);
211			if (mode == IFDIR)
212				SETINO(ino, dumpdirmap);
213			if (WANTTODUMP(dp)) {
214				SETINO(ino, dumpinomap);
215				if (mode != IFREG &&
216				    mode != IFDIR &&
217				    mode != IFLNK)
218					*tapesize += 1;
219				else
220					*tapesize += blockest(dp);
221				continue;
222			}
223			if (mode == IFDIR) {
224				if (!nonodump &&
225				    (DIP(dp, di_flags) & UF_NODUMP))
226					CLRINO(ino, usedinomap);
227				anydirskipped = 1;
228			}
229		}
230	}
231	/*
232	 * Restore gets very upset if the root is not dumped,
233	 * so ensure that it always is dumped.
234	 */
235	SETINO(ROOTINO, dumpinomap);
236	return (anydirskipped);
237}
238
239/*
240 * Dump pass 2.
241 *
242 * Scan each directory on the file system to see if it has any modified
243 * files in it. If it does, and has not already been added to the dump
244 * list (because it was itself modified), then add it. If a directory
245 * has not been modified itself, contains no modified files and has no
246 * subdirectories, then it can be deleted from the dump list and from
247 * the list of directories. By deleting it from the list of directories,
248 * its parent may now qualify for the same treatment on this or a later
249 * pass using this algorithm.
250 */
251int
252mapdirs(ino_t maxino, long *tapesize)
253{
254	union dinode *dp;
255	int i, isdir, nodump;
256	char *map;
257	ino_t ino;
258	union dinode di;
259	long filesize;
260	int ret, change = 0;
261
262	isdir = 0;		/* XXX just to get gcc to shut up */
263	for (map = dumpdirmap, ino = 1; ino < maxino; ino++) {
264		if (((ino - 1) % CHAR_BIT) == 0)	/* map is offset by 1 */
265			isdir = *map++;
266		else
267			isdir >>= 1;
268		/*
269		 * If a directory has been removed from usedinomap, it
270		 * either has the nodump flag set, or has inherited
271		 * it.  Although a directory can't be in dumpinomap if
272		 * it isn't in usedinomap, we have to go through it to
273		 * propagate the nodump flag.
274		 */
275		nodump = !nonodump && (TSTINO(ino, usedinomap) == 0);
276		if ((isdir & 1) == 0 || (TSTINO(ino, dumpinomap) && !nodump))
277			continue;
278		dp = getino(ino, &i);
279		/*
280		 * inode buf may change in searchdir().
281		 */
282		if (sblock->fs_magic == FS_UFS1_MAGIC)
283			di.dp1 = dp->dp1;
284		else
285			di.dp2 = dp->dp2;
286		filesize = DIP(&di, di_size);
287		for (ret = 0, i = 0; filesize > 0 && i < NDADDR; i++) {
288			if (DIP(&di, di_db[i]) != 0)
289				ret |= searchdir(ino, DIP(&di, di_db[i]),
290				    (long)sblksize(sblock, DIP(&di, di_size),
291				    i), filesize, tapesize, nodump, maxino);
292			if (ret & HASDUMPEDFILE)
293				filesize = 0;
294			else
295				filesize -= sblock->fs_bsize;
296		}
297		for (i = 0; filesize > 0 && i < NIADDR; i++) {
298			if (DIP(&di, di_ib[i]) == 0)
299				continue;
300			ret |= dirindir(ino, DIP(&di, di_ib[i]), i, &filesize,
301			    tapesize, nodump, maxino);
302		}
303		if (ret & HASDUMPEDFILE) {
304			SETINO(ino, dumpinomap);
305			*tapesize += blockest(&di);
306			change = 1;
307			continue;
308		}
309		if (nodump) {
310			if (ret & HASSUBDIRS)
311				change = 1;	/* subdirs inherit nodump */
312			CLRINO(ino, dumpdirmap);
313		} else if ((ret & HASSUBDIRS) == 0)
314			if (!TSTINO(ino, dumpinomap)) {
315				CLRINO(ino, dumpdirmap);
316				change = 1;
317			}
318	}
319	return (change);
320}
321
322/*
323 * Read indirect blocks, and pass the data blocks to be searched
324 * as directories. Quit as soon as any entry is found that will
325 * require the directory to be dumped.
326 */
327static int
328dirindir(
329	ino_t ino,
330	ufs2_daddr_t blkno,
331	int ind_level,
332	long *filesize,
333	long *tapesize,
334	int nodump,
335	ino_t maxino)
336{
337	union {
338		ufs1_daddr_t ufs1[MAXBSIZE / sizeof(ufs1_daddr_t)];
339		ufs2_daddr_t ufs2[MAXBSIZE / sizeof(ufs2_daddr_t)];
340	} idblk;
341	int ret = 0;
342	int i;
343
344	bread(fsbtodb(sblock, blkno), (char *)&idblk, (int)sblock->fs_bsize);
345	if (ind_level <= 0) {
346		for (i = 0; *filesize > 0 && i < NINDIR(sblock); i++) {
347			if (sblock->fs_magic == FS_UFS1_MAGIC)
348				blkno = idblk.ufs1[i];
349			else
350				blkno = idblk.ufs2[i];
351			if (blkno != 0)
352				ret |= searchdir(ino, blkno, sblock->fs_bsize,
353					*filesize, tapesize, nodump, maxino);
354			if (ret & HASDUMPEDFILE)
355				*filesize = 0;
356			else
357				*filesize -= sblock->fs_bsize;
358		}
359		return (ret);
360	}
361	ind_level--;
362	for (i = 0; *filesize > 0 && i < NINDIR(sblock); i++) {
363		if (sblock->fs_magic == FS_UFS1_MAGIC)
364			blkno = idblk.ufs1[i];
365		else
366			blkno = idblk.ufs2[i];
367		if (blkno != 0)
368			ret |= dirindir(ino, blkno, ind_level, filesize,
369			    tapesize, nodump, maxino);
370	}
371	return (ret);
372}
373
374/*
375 * Scan a disk block containing directory information looking to see if
376 * any of the entries are on the dump list and to see if the directory
377 * contains any subdirectories.
378 */
379static int
380searchdir(
381	ino_t ino,
382	ufs2_daddr_t blkno,
383	long size,
384	long filesize,
385	long *tapesize,
386	int nodump,
387	ino_t maxino)
388{
389	int mode;
390	struct direct *dp;
391	union dinode *ip;
392	long loc, ret = 0;
393	static caddr_t dblk;
394
395	if (dblk == NULL && (dblk = malloc(sblock->fs_bsize)) == NULL)
396		quit("searchdir: cannot allocate indirect memory.\n");
397	bread(fsbtodb(sblock, blkno), dblk, (int)size);
398	if (filesize < size)
399		size = filesize;
400	for (loc = 0; loc < size; ) {
401		dp = (struct direct *)(dblk + loc);
402		if (dp->d_reclen == 0) {
403			msg("corrupted directory, inumber %d\n", ino);
404			break;
405		}
406		loc += dp->d_reclen;
407		if (dp->d_ino == 0)
408			continue;
409		if (dp->d_ino >= maxino) {
410			msg("corrupted directory entry, d_ino %d >= %d\n",
411			    dp->d_ino, maxino);
412			break;
413		}
414		if (dp->d_name[0] == '.') {
415			if (dp->d_name[1] == '\0')
416				continue;
417			if (dp->d_name[1] == '.' && dp->d_name[2] == '\0')
418				continue;
419		}
420		if (nodump) {
421			ip = getino(dp->d_ino, &mode);
422			if (TSTINO(dp->d_ino, dumpinomap)) {
423				CLRINO(dp->d_ino, dumpinomap);
424				*tapesize -= blockest(ip);
425			}
426			/*
427			 * Add back to dumpdirmap and remove from usedinomap
428			 * to propagate nodump.
429			 */
430			if (mode == IFDIR) {
431				SETINO(dp->d_ino, dumpdirmap);
432				CLRINO(dp->d_ino, usedinomap);
433				ret |= HASSUBDIRS;
434			}
435		} else {
436			if (TSTINO(dp->d_ino, dumpinomap)) {
437				ret |= HASDUMPEDFILE;
438				if (ret & HASSUBDIRS)
439					break;
440			}
441			if (TSTINO(dp->d_ino, dumpdirmap)) {
442				ret |= HASSUBDIRS;
443				if (ret & HASDUMPEDFILE)
444					break;
445			}
446		}
447	}
448	return (ret);
449}
450
451/*
452 * Dump passes 3 and 4.
453 *
454 * Dump the contents of an inode to tape.
455 */
456void
457dumpino(union dinode *dp, ino_t ino)
458{
459	int ind_level, cnt, last, added;
460	off_t size;
461	char buf[TP_BSIZE];
462
463	if (newtape) {
464		newtape = 0;
465		dumpmap(dumpinomap, TS_BITS, ino);
466	}
467	CLRINO(ino, dumpinomap);
468	/*
469	 * Zero out the size of a snapshot so that it will be dumped
470	 * as a zero length file.
471	 */
472	if ((DIP(dp, di_flags) & SF_SNAPSHOT) != 0) {
473		DIP_SET(dp, di_size, 0);
474		DIP_SET(dp, di_flags, DIP(dp, di_flags) & ~SF_SNAPSHOT);
475	}
476	if (sblock->fs_magic == FS_UFS1_MAGIC) {
477		spcl.c_mode = dp->dp1.di_mode;
478		spcl.c_size = dp->dp1.di_size;
479		spcl.c_extsize = 0;
480		spcl.c_atime = _time32_to_time(dp->dp1.di_atime);
481		spcl.c_atimensec = dp->dp1.di_atimensec;
482		spcl.c_mtime = _time32_to_time(dp->dp1.di_mtime);
483		spcl.c_mtimensec = dp->dp1.di_mtimensec;
484		spcl.c_birthtime = 0;
485		spcl.c_birthtimensec = 0;
486		spcl.c_rdev = dp->dp1.di_rdev;
487		spcl.c_file_flags = dp->dp1.di_flags;
488		spcl.c_uid = dp->dp1.di_uid;
489		spcl.c_gid = dp->dp1.di_gid;
490	} else {
491		spcl.c_mode = dp->dp2.di_mode;
492		spcl.c_size = dp->dp2.di_size;
493		spcl.c_extsize = dp->dp2.di_extsize;
494		spcl.c_atime = _time64_to_time(dp->dp2.di_atime);
495		spcl.c_atimensec = dp->dp2.di_atimensec;
496		spcl.c_mtime = _time64_to_time(dp->dp2.di_mtime);
497		spcl.c_mtimensec = dp->dp2.di_mtimensec;
498		spcl.c_birthtime = _time64_to_time(dp->dp2.di_birthtime);
499		spcl.c_birthtimensec = dp->dp2.di_birthnsec;
500		spcl.c_rdev = dp->dp2.di_rdev;
501		spcl.c_file_flags = dp->dp2.di_flags;
502		spcl.c_uid = dp->dp2.di_uid;
503		spcl.c_gid = dp->dp2.di_gid;
504	}
505	spcl.c_type = TS_INODE;
506	spcl.c_count = 0;
507	switch (DIP(dp, di_mode) & S_IFMT) {
508
509	case 0:
510		/*
511		 * Freed inode.
512		 */
513		return;
514
515	case S_IFLNK:
516		/*
517		 * Check for short symbolic link.
518		 */
519		if (DIP(dp, di_size) > 0 &&
520		    DIP(dp, di_size) < sblock->fs_maxsymlinklen) {
521			spcl.c_addr[0] = 1;
522			spcl.c_count = 1;
523			added = appendextdata(dp);
524			writeheader(ino);
525			if (sblock->fs_magic == FS_UFS1_MAGIC)
526				memmove(buf, (caddr_t)dp->dp1.di_db,
527				    (u_long)DIP(dp, di_size));
528			else
529				memmove(buf, (caddr_t)dp->dp2.di_db,
530				    (u_long)DIP(dp, di_size));
531			buf[DIP(dp, di_size)] = '\0';
532			writerec(buf, 0);
533			writeextdata(dp, ino, added);
534			return;
535		}
536		/* FALLTHROUGH */
537
538	case S_IFDIR:
539	case S_IFREG:
540		if (DIP(dp, di_size) > 0)
541			break;
542		/* FALLTHROUGH */
543
544	case S_IFIFO:
545	case S_IFSOCK:
546	case S_IFCHR:
547	case S_IFBLK:
548		added = appendextdata(dp);
549		writeheader(ino);
550		writeextdata(dp, ino, added);
551		return;
552
553	default:
554		msg("Warning: undefined file type 0%o\n",
555		    DIP(dp, di_mode) & IFMT);
556		return;
557	}
558	if (DIP(dp, di_size) > NDADDR * sblock->fs_bsize) {
559		cnt = NDADDR * sblock->fs_frag;
560		last = 0;
561	} else {
562		cnt = howmany(DIP(dp, di_size), sblock->fs_fsize);
563		last = 1;
564	}
565	if (sblock->fs_magic == FS_UFS1_MAGIC)
566		ufs1_blksout(&dp->dp1.di_db[0], cnt, ino);
567	else
568		ufs2_blksout(dp, &dp->dp2.di_db[0], cnt, ino, last);
569	if ((size = DIP(dp, di_size) - NDADDR * sblock->fs_bsize) <= 0)
570		return;
571	for (ind_level = 0; ind_level < NIADDR; ind_level++) {
572		dmpindir(dp, ino, DIP(dp, di_ib[ind_level]), ind_level, &size);
573		if (size <= 0)
574			return;
575	}
576}
577
578/*
579 * Read indirect blocks, and pass the data blocks to be dumped.
580 */
581static void
582dmpindir(union dinode *dp, ino_t ino, ufs2_daddr_t blk, int ind_level,
583	off_t *size)
584{
585	union {
586		ufs1_daddr_t ufs1[MAXBSIZE / sizeof(ufs1_daddr_t)];
587		ufs2_daddr_t ufs2[MAXBSIZE / sizeof(ufs2_daddr_t)];
588	} idblk;
589	int i, cnt, last;
590
591	if (blk != 0)
592		bread(fsbtodb(sblock, blk), (char *)&idblk,
593		    (int)sblock->fs_bsize);
594	else
595		memset(&idblk, 0, sblock->fs_bsize);
596	if (ind_level <= 0) {
597		if (*size > NINDIR(sblock) * sblock->fs_bsize) {
598			cnt = NINDIR(sblock) * sblock->fs_frag;
599			last = 0;
600		} else {
601			cnt = howmany(*size, sblock->fs_fsize);
602			last = 1;
603		}
604		*size -= NINDIR(sblock) * sblock->fs_bsize;
605		if (sblock->fs_magic == FS_UFS1_MAGIC)
606			ufs1_blksout(idblk.ufs1, cnt, ino);
607		else
608			ufs2_blksout(dp, idblk.ufs2, cnt, ino, last);
609		return;
610	}
611	ind_level--;
612	for (i = 0; i < NINDIR(sblock); i++) {
613		if (sblock->fs_magic == FS_UFS1_MAGIC)
614			dmpindir(dp, ino, idblk.ufs1[i], ind_level, size);
615		else
616			dmpindir(dp, ino, idblk.ufs2[i], ind_level, size);
617		if (*size <= 0)
618			return;
619	}
620}
621
622/*
623 * Collect up the data into tape record sized buffers and output them.
624 */
625static void
626ufs1_blksout(ufs1_daddr_t *blkp, int frags, ino_t ino)
627{
628	ufs1_daddr_t *bp;
629	int i, j, count, blks, tbperdb;
630
631	blks = howmany(frags * sblock->fs_fsize, TP_BSIZE);
632	tbperdb = sblock->fs_bsize >> tp_bshift;
633	for (i = 0; i < blks; i += TP_NINDIR) {
634		if (i + TP_NINDIR > blks)
635			count = blks;
636		else
637			count = i + TP_NINDIR;
638		for (j = i; j < count; j++)
639			if (blkp[j / tbperdb] != 0)
640				spcl.c_addr[j - i] = 1;
641			else
642				spcl.c_addr[j - i] = 0;
643		spcl.c_count = count - i;
644		writeheader(ino);
645		bp = &blkp[i / tbperdb];
646		for (j = i; j < count; j += tbperdb, bp++)
647			if (*bp != 0) {
648				if (j + tbperdb <= count)
649					dumpblock(*bp, (int)sblock->fs_bsize);
650				else
651					dumpblock(*bp, (count - j) * TP_BSIZE);
652			}
653		spcl.c_type = TS_ADDR;
654	}
655}
656
657/*
658 * Collect up the data into tape record sized buffers and output them.
659 */
660static void
661ufs2_blksout(union dinode *dp, ufs2_daddr_t *blkp, int frags, ino_t ino,
662	int last)
663{
664	ufs2_daddr_t *bp;
665	int i, j, count, resid, blks, tbperdb, added;
666	static int writingextdata = 0;
667
668	/*
669	 * Calculate the number of TP_BSIZE blocks to be dumped.
670	 * For filesystems with a fragment size bigger than TP_BSIZE,
671	 * only part of the final fragment may need to be dumped.
672	 */
673	blks = howmany(frags * sblock->fs_fsize, TP_BSIZE);
674	if (last) {
675		resid = howmany(fragoff(sblock, dp->dp2.di_size), TP_BSIZE);
676		if (resid > 0)
677			blks -= howmany(sblock->fs_fsize, TP_BSIZE) - resid;
678	}
679	tbperdb = sblock->fs_bsize >> tp_bshift;
680	for (i = 0; i < blks; i += TP_NINDIR) {
681		if (i + TP_NINDIR > blks)
682			count = blks;
683		else
684			count = i + TP_NINDIR;
685		for (j = i; j < count; j++)
686			if (blkp[j / tbperdb] != 0)
687				spcl.c_addr[j - i] = 1;
688			else
689				spcl.c_addr[j - i] = 0;
690		spcl.c_count = count - i;
691		if (last && count == blks && !writingextdata)
692			added = appendextdata(dp);
693		writeheader(ino);
694		bp = &blkp[i / tbperdb];
695		for (j = i; j < count; j += tbperdb, bp++)
696			if (*bp != 0) {
697				if (j + tbperdb <= count)
698					dumpblock(*bp, (int)sblock->fs_bsize);
699				else
700					dumpblock(*bp, (count - j) * TP_BSIZE);
701			}
702		spcl.c_type = TS_ADDR;
703		spcl.c_count = 0;
704		if (last && count == blks && !writingextdata) {
705			writingextdata = 1;
706			writeextdata(dp, ino, added);
707			writingextdata = 0;
708		}
709	}
710}
711
712/*
713 * If there is room in the current block for the extended attributes
714 * as well as the file data, update the header to reflect the added
715 * attribute data at the end. Attributes are placed at the end so that
716 * old versions of restore will correctly restore the file and simply
717 * discard the extra data at the end that it does not understand.
718 * The attribute data is dumped following the file data by the
719 * writeextdata() function (below).
720 */
721static int
722appendextdata(union dinode *dp)
723{
724	int i, blks, tbperdb;
725
726	/*
727	 * If no extended attributes, there is nothing to do.
728	 */
729	if (spcl.c_extsize == 0)
730		return (0);
731	/*
732	 * If there is not enough room at the end of this block
733	 * to add the extended attributes, then rather than putting
734	 * part of them here, we simply push them entirely into a
735	 * new block rather than putting some here and some later.
736	 */
737	if (spcl.c_extsize > NXADDR * sblock->fs_bsize)
738		blks = howmany(NXADDR * sblock->fs_bsize, TP_BSIZE);
739	else
740		blks = howmany(spcl.c_extsize, TP_BSIZE);
741	if (spcl.c_count + blks > TP_NINDIR)
742		return (0);
743	/*
744	 * Update the block map in the header to indicate the added
745	 * extended attribute. They will be appended after the file
746	 * data by the writeextdata() routine.
747	 */
748	tbperdb = sblock->fs_bsize >> tp_bshift;
749	for (i = 0; i < blks; i++)
750		if (&dp->dp2.di_extb[i / tbperdb] != 0)
751				spcl.c_addr[spcl.c_count + i] = 1;
752			else
753				spcl.c_addr[spcl.c_count + i] = 0;
754	spcl.c_count += blks;
755	return (blks);
756}
757
758/*
759 * Dump the extended attribute data. If there was room in the file
760 * header, then all we need to do is output the data blocks. If there
761 * was not room in the file header, then an additional TS_ADDR header
762 * is created to hold the attribute data.
763 */
764static void
765writeextdata(union dinode *dp, ino_t ino, int added)
766{
767	int i, frags, blks, tbperdb, last;
768	ufs2_daddr_t *bp;
769	off_t size;
770
771	/*
772	 * If no extended attributes, there is nothing to do.
773	 */
774	if (spcl.c_extsize == 0)
775		return;
776	/*
777	 * If there was no room in the file block for the attributes,
778	 * dump them out in a new block, otherwise just dump the data.
779	 */
780	if (added == 0) {
781		if (spcl.c_extsize > NXADDR * sblock->fs_bsize) {
782			frags = NXADDR * sblock->fs_frag;
783			last = 0;
784		} else {
785			frags = howmany(spcl.c_extsize, sblock->fs_fsize);
786			last = 1;
787		}
788		ufs2_blksout(dp, &dp->dp2.di_extb[0], frags, ino, last);
789	} else {
790		if (spcl.c_extsize > NXADDR * sblock->fs_bsize)
791			blks = howmany(NXADDR * sblock->fs_bsize, TP_BSIZE);
792		else
793			blks = howmany(spcl.c_extsize, TP_BSIZE);
794		tbperdb = sblock->fs_bsize >> tp_bshift;
795		for (i = 0; i < blks; i += tbperdb) {
796			bp = &dp->dp2.di_extb[i / tbperdb];
797			if (*bp != 0) {
798				if (i + tbperdb <= blks)
799					dumpblock(*bp, (int)sblock->fs_bsize);
800				else
801					dumpblock(*bp, (blks - i) * TP_BSIZE);
802			}
803		}
804
805	}
806	/*
807	 * If an indirect block is added for extended attributes, then
808	 * di_exti below should be changed to the structure element
809	 * that references the extended attribute indirect block. This
810	 * definition is here only to make it compile without complaint.
811	 */
812#define di_exti di_spare[0]
813	/*
814	 * If the extended attributes fall into an indirect block,
815	 * dump it as well.
816	 */
817	if ((size = spcl.c_extsize - NXADDR * sblock->fs_bsize) > 0)
818		dmpindir(dp, ino, dp->dp2.di_exti, 0, &size);
819}
820
821/*
822 * Dump a map to the tape.
823 */
824void
825dumpmap(char *map, int type, ino_t ino)
826{
827	int i;
828	char *cp;
829
830	spcl.c_type = type;
831	spcl.c_count = howmany(mapsize * sizeof(char), TP_BSIZE);
832	writeheader(ino);
833	for (i = 0, cp = map; i < spcl.c_count; i++, cp += TP_BSIZE)
834		writerec(cp, 0);
835}
836
837/*
838 * Write a header record to the dump tape.
839 */
840void
841writeheader(ino_t ino)
842{
843	int32_t sum, cnt, *lp;
844
845	if (rsync_friendly >= 2) {
846		/* don't track changes to access time */
847		spcl.c_atime = spcl.c_mtime;
848		spcl.c_atimensec = spcl.c_mtimensec;
849	}
850	spcl.c_inumber = ino;
851	spcl.c_magic = FS_UFS2_MAGIC;
852	spcl.c_checksum = 0;
853	lp = (int32_t *)&spcl;
854	sum = 0;
855	cnt = sizeof(union u_spcl) / (4 * sizeof(int32_t));
856	while (--cnt >= 0) {
857		sum += *lp++;
858		sum += *lp++;
859		sum += *lp++;
860		sum += *lp++;
861	}
862	spcl.c_checksum = CHECKSUM - sum;
863	writerec((char *)&spcl, 1);
864}
865
866union dinode *
867getino(ino_t inum, int *modep)
868{
869	static ino_t minino, maxino;
870	static caddr_t inoblock;
871	struct ufs1_dinode *dp1;
872	struct ufs2_dinode *dp2;
873
874	if (inoblock == NULL && (inoblock = malloc(sblock->fs_bsize)) == NULL)
875		quit("cannot allocate inode memory.\n");
876	curino = inum;
877	if (inum >= minino && inum < maxino)
878		goto gotit;
879	bread(fsbtodb(sblock, ino_to_fsba(sblock, inum)), inoblock,
880	    (int)sblock->fs_bsize);
881	minino = inum - (inum % INOPB(sblock));
882	maxino = minino + INOPB(sblock);
883gotit:
884	if (sblock->fs_magic == FS_UFS1_MAGIC) {
885		dp1 = &((struct ufs1_dinode *)inoblock)[inum - minino];
886		*modep = (dp1->di_mode & IFMT);
887		return ((union dinode *)dp1);
888	}
889	dp2 = &((struct ufs2_dinode *)inoblock)[inum - minino];
890	*modep = (dp2->di_mode & IFMT);
891	return ((union dinode *)dp2);
892}
893
894/*
895 * Read a chunk of data from the disk.
896 * Try to recover from hard errors by reading in sector sized pieces.
897 * Error recovery is attempted at most BREADEMAX times before seeking
898 * consent from the operator to continue.
899 */
900int	breaderrors = 0;
901#define	BREADEMAX 32
902
903void
904bread(ufs2_daddr_t blkno, char *buf, int size)
905{
906	int secsize, bytes, resid, xfer, base, cnt, i;
907	static char *tmpbuf;
908	off_t offset;
909
910loop:
911	offset = blkno << dev_bshift;
912	secsize = sblock->fs_fsize;
913	base = offset % secsize;
914	resid = size % secsize;
915	/*
916	 * If the transfer request starts or ends on a non-sector
917	 * boundary, we must read the entire sector and copy out
918	 * just the part that we need.
919	 */
920	if (base == 0 && resid == 0) {
921		cnt = cread(diskfd, buf, size, offset);
922		if (cnt == size)
923			return;
924	} else {
925		if (tmpbuf == NULL && (tmpbuf = malloc(secsize)) == 0)
926			quit("buffer malloc failed\n");
927		xfer = 0;
928		bytes = size;
929		if (base != 0) {
930			cnt = cread(diskfd, tmpbuf, secsize, offset - base);
931			if (cnt != secsize)
932				goto bad;
933			xfer = MIN(secsize - base, size);
934			offset += xfer;
935			bytes -= xfer;
936			resid = bytes % secsize;
937			memcpy(buf, &tmpbuf[base], xfer);
938		}
939		if (bytes >= secsize) {
940			cnt = cread(diskfd, &buf[xfer], bytes - resid, offset);
941			if (cnt != bytes - resid)
942				goto bad;
943			xfer += cnt;
944			offset += cnt;
945		}
946		if (resid == 0)
947			return;
948		cnt = cread(diskfd, tmpbuf, secsize, offset);
949		if (cnt == secsize) {
950			memcpy(&buf[xfer], tmpbuf, resid);
951			return;
952		}
953	}
954bad:
955	if (blkno + (size / dev_bsize) > fsbtodb(sblock, sblock->fs_size)) {
956		/*
957		 * Trying to read the final fragment.
958		 *
959		 * NB - dump only works in TP_BSIZE blocks, hence
960		 * rounds `dev_bsize' fragments up to TP_BSIZE pieces.
961		 * It should be smarter about not actually trying to
962		 * read more than it can get, but for the time being
963		 * we punt and scale back the read only when it gets
964		 * us into trouble. (mkm 9/25/83)
965		 */
966		size -= dev_bsize;
967		goto loop;
968	}
969	if (cnt == -1)
970		msg("read error from %s: %s: [block %jd]: count=%d\n",
971			disk, strerror(errno), (intmax_t)blkno, size);
972	else
973		msg("short read error from %s: [block %jd]: count=%d, got=%d\n",
974			disk, (intmax_t)blkno, size, cnt);
975	if (++breaderrors > BREADEMAX) {
976		msg("More than %d block read errors from %s\n",
977			BREADEMAX, disk);
978		broadcast("DUMP IS AILING!\n");
979		msg("This is an unrecoverable error.\n");
980		if (!query("Do you want to attempt to continue?")){
981			dumpabort(0);
982			/*NOTREACHED*/
983		} else
984			breaderrors = 0;
985	}
986	/*
987	 * Zero buffer, then try to read each sector of buffer separately,
988	 * and bypass the cache.
989	 */
990	memset(buf, 0, size);
991	for (i = 0; i < size; i += dev_bsize, buf += dev_bsize, blkno++) {
992		if ((cnt = pread(diskfd, buf, (int)dev_bsize,
993		    ((off_t)blkno << dev_bshift))) == dev_bsize)
994			continue;
995		if (cnt == -1) {
996			msg("read error from %s: %s: [sector %jd]: count=%ld\n",
997			    disk, strerror(errno), (intmax_t)blkno, dev_bsize);
998			continue;
999		}
1000		msg("short read from %s: [sector %jd]: count=%ld, got=%d\n",
1001		    disk, (intmax_t)blkno, dev_bsize, cnt);
1002	}
1003}
1004