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