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