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