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