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