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