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