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