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