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