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