traverse.c revision 1.29 1 /* $NetBSD: traverse.c,v 1.29 2001/05/27 14:17:57 lukem 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 #include <sys/cdefs.h>
37 #ifndef lint
38 #if 0
39 static char sccsid[] = "@(#)traverse.c 8.7 (Berkeley) 6/15/95";
40 #else
41 __RCSID("$NetBSD: traverse.c,v 1.29 2001/05/27 14:17:57 lukem Exp $");
42 #endif
43 #endif /* not lint */
44
45 #include <sys/param.h>
46 #include <sys/time.h>
47 #include <sys/stat.h>
48 #ifdef sunos
49 #include <sys/vnode.h>
50
51 #include <ufs/fs.h>
52 #include <ufs/fsdir.h>
53 #include <ufs/inode.h>
54 #else
55 #include <ufs/ufs/dir.h>
56 #include <ufs/ufs/dinode.h>
57 #include <ufs/ffs/fs.h>
58 #include <ufs/ffs/ffs_extern.h>
59 #endif
60
61 #include <protocols/dumprestore.h>
62
63 #include <ctype.h>
64 #include <errno.h>
65 #include <fts.h>
66 #include <stdio.h>
67 #ifdef __STDC__
68 #include <string.h>
69 #include <unistd.h>
70 #endif
71
72 #include "dump.h"
73
74 #define HASDUMPEDFILE 0x1
75 #define HASSUBDIRS 0x2
76
77 #ifdef FS_44INODEFMT
78 typedef quad_t fsizeT;
79 #else
80 typedef int32_t fsizeT;
81 #endif
82
83 static int dirindir(ino_t, daddr_t, int, long *, long *, int);
84 static void dmpindir(ino_t, daddr_t, int, fsizeT *);
85 static int searchdir(ino_t, daddr_t, long, long, long *, int);
86
87 /*
88 * This is an estimation of the number of TP_BSIZE blocks in the file.
89 * It estimates the number of blocks in files with holes by assuming
90 * that all of the blocks accounted for by di_blocks are data blocks
91 * (when some of the blocks are usually used for indirect pointers);
92 * hence the estimate may be high.
93 */
94 long
95 blockest(struct dinode *dp)
96 {
97 long blkest, sizeest;
98
99 /*
100 * dp->di_size is the size of the file in bytes.
101 * dp->di_blocks stores the number of sectors actually in the file.
102 * If there are more sectors than the size would indicate, this just
103 * means that there are indirect blocks in the file or unused
104 * sectors in the last file block; we can safely ignore these
105 * (blkest = sizeest below).
106 * If the file is bigger than the number of sectors would indicate,
107 * then the file has holes in it. In this case we must use the
108 * block count to estimate the number of data blocks used, but
109 * we use the actual size for estimating the number of indirect
110 * dump blocks (sizeest vs. blkest in the indirect block
111 * calculation).
112 */
113 blkest = howmany(dbtob((u_int64_t)dp->di_blocks), TP_BSIZE);
114 sizeest = howmany(dp->di_size, TP_BSIZE);
115 if (blkest > sizeest)
116 blkest = sizeest;
117 if (dp->di_size > ufsib->ufs_bsize * NDADDR) {
118 /* calculate the number of indirect blocks on the dump tape */
119 blkest +=
120 howmany(sizeest - NDADDR * ufsib->ufs_bsize / TP_BSIZE,
121 TP_NINDIR);
122 }
123 return (blkest + 1);
124 }
125
126 /* Auxiliary macro to pick up files changed since previous dump. */
127 #define CHANGEDSINCE(dp, t) \
128 ((dp)->di_mtime >= (t) || (dp)->di_ctime >= (t))
129
130 /* The WANTTODUMP macro decides whether a file should be dumped. */
131 #ifdef UF_NODUMP
132 #define WANTTODUMP(dp) \
133 (CHANGEDSINCE(dp, iswap32(spcl.c_ddate)) && \
134 (nonodump || ((dp)->di_flags & UF_NODUMP) != UF_NODUMP))
135 #else
136 #define WANTTODUMP(dp) CHANGEDSINCE(dp, iswap32(spcl.c_ddate))
137 #endif
138
139 /*
140 * Determine if given inode should be dumped
141 */
142 void
143 mapfileino(ino_t ino, long *tapesize, int *dirskipped)
144 {
145 int mode;
146 struct dinode *dp;
147
148 /*
149 * Skip inode if we've already marked it for dumping
150 */
151 if (TSTINO(ino, usedinomap))
152 return;
153 dp = getino(ino);
154 if ((mode = (dp->di_mode & IFMT)) == 0)
155 return;
156 /*
157 * Put all dirs in dumpdirmap, inodes that are to be dumped in the
158 * used map. All inode but dirs who have the nodump attribute go
159 * to the usedinomap.
160 */
161 SETINO(ino, usedinomap);
162 if (mode == IFDIR)
163 SETINO(ino, dumpdirmap);
164 if (WANTTODUMP(dp)) {
165 SETINO(ino, dumpinomap);
166 if (mode != IFREG && mode != IFDIR && mode != IFLNK)
167 *tapesize += 1;
168 else
169 *tapesize += blockest(dp);
170 return;
171 }
172 if (mode == IFDIR) {
173 #ifdef UF_NODUMP
174 if (!nonodump && (dp->di_flags & UF_NODUMP))
175 CLRINO(ino, usedinomap);
176 #endif
177 *dirskipped = 1;
178 }
179 }
180
181 /*
182 * Dump pass 1.
183 *
184 * Walk the inode list for a filesystem to find all allocated inodes
185 * that have been modified since the previous dump time. Also, find all
186 * the directories in the filesystem.
187 */
188 int
189 mapfiles(ino_t maxino, long *tapesize, char *disk, char * const *dirv)
190 {
191 int anydirskipped = 0;
192
193 if (dirv != NULL) {
194 char curdir[MAXPATHLEN];
195 FTS *dirh;
196 FTSENT *entry;
197 int d;
198
199 if (getcwd(curdir, sizeof(curdir)) == NULL) {
200 msg("Can't determine cwd: %s\n", strerror(errno));
201 dumpabort(0);
202 }
203 if ((dirh = fts_open(dirv, FTS_PHYSICAL|FTS_SEEDOT|FTS_XDEV,
204 NULL)) == NULL) {
205 msg("fts_open failed: %s\n", strerror(errno));
206 dumpabort(0);
207 }
208 while ((entry = fts_read(dirh)) != NULL) {
209 switch (entry->fts_info) {
210 case FTS_DNR: /* an error */
211 case FTS_ERR:
212 case FTS_NS:
213 msg("Can't fts_read %s: %s\n", entry->fts_path,
214 strerror(errno));
215 case FTS_DP: /* already seen dir */
216 continue;
217 }
218 mapfileino(entry->fts_statp->st_ino, tapesize,
219 &anydirskipped);
220 }
221 (void)fts_close(dirh);
222
223 /*
224 * Add any parent directories
225 */
226 for (d = 0 ; dirv[d] != NULL ; d++) {
227 char path[MAXPATHLEN];
228
229 if (dirv[d][0] != '/')
230 (void)snprintf(path, sizeof(path), "%s/%s",
231 curdir, dirv[d]);
232 else
233 (void)snprintf(path, sizeof(path), "%s",
234 dirv[d]);
235 while (strcmp(path, disk) != 0) {
236 char *p;
237 struct stat sb;
238
239 if (*path == '\0')
240 break;
241 if ((p = strrchr(path, '/')) == NULL)
242 break;
243 if (p == path)
244 break;
245 *p = '\0';
246 if (stat(path, &sb) == -1) {
247 msg("Can't stat %s: %s\n", path,
248 strerror(errno));
249 break;
250 }
251 mapfileino(sb.st_ino, tapesize, &anydirskipped);
252 }
253 }
254
255 /*
256 * Ensure that the root inode actually appears in the
257 * file list for a subdir
258 */
259 mapfileino(ROOTINO, tapesize, &anydirskipped);
260 } else {
261 ino_t ino;
262
263 for (ino = ROOTINO; ino < maxino; ino++) {
264 mapfileino(ino, tapesize, &anydirskipped);
265 }
266 }
267 /*
268 * Restore gets very upset if the root is not dumped,
269 * so ensure that it always is dumped.
270 */
271 SETINO(ROOTINO, dumpinomap);
272 return (anydirskipped);
273 }
274
275 /*
276 * Dump pass 2.
277 *
278 * Scan each directory on the filesystem to see if it has any modified
279 * files in it. If it does, and has not already been added to the dump
280 * list (because it was itself modified), then add it. If a directory
281 * has not been modified itself, contains no modified files and has no
282 * subdirectories, then it can be deleted from the dump list and from
283 * the list of directories. By deleting it from the list of directories,
284 * its parent may now qualify for the same treatment on this or a later
285 * pass using this algorithm.
286 */
287 int
288 mapdirs(ino_t maxino, long *tapesize)
289 {
290 struct dinode *dp, di;
291 int i, isdir, nodump;
292 char *map;
293 ino_t ino;
294 long filesize;
295 int ret, change = 0;
296
297 isdir = 0; /* XXX just to get gcc to shut up */
298 for (map = dumpdirmap, ino = 1; ino < maxino; ino++) {
299 if (((ino - 1) % NBBY) == 0) /* map is offset by 1 */
300 isdir = *map++;
301 else
302 isdir >>= 1;
303 /*
304 * If dir has been removed from the used map, it's either
305 * because it had the nodump flag, or it herited it from
306 * its parent. A directory can't be in dumpinomap if
307 * not in usedinomap, but we have to go throuh it anyway
308 * to propagate the nodump attribute.
309 */
310 nodump = (TSTINO(ino, usedinomap) == 0);
311 if ((isdir & 1) == 0 ||
312 (TSTINO(ino, dumpinomap) && nodump == 0))
313 continue;
314
315 dp = getino(ino);
316 di = *dp; /* inode buf may be changed in searchdir */
317 filesize = di.di_size;
318 for (ret = 0, i = 0; filesize > 0 && i < NDADDR; i++) {
319 if (di.di_db[i] != 0)
320 ret |= searchdir(ino, iswap32(di.di_db[i]),
321 (long)ufs_dblksize(ufsib, &di, i),
322 filesize, tapesize, nodump);
323 if (ret & HASDUMPEDFILE)
324 filesize = 0;
325 else
326 filesize -= ufsib->ufs_bsize;
327 }
328 for (i = 0; filesize > 0 && i < NIADDR; i++) {
329 if (di.di_ib[i] == 0)
330 continue;
331 ret |= dirindir(ino, di.di_ib[i], i, &filesize,
332 tapesize, nodump);
333 }
334 if (ret & HASDUMPEDFILE) {
335 SETINO(ino, dumpinomap);
336 *tapesize += blockest(&di);
337 change = 1;
338 continue;
339 }
340 if (nodump) {
341 if (ret & HASSUBDIRS)
342 change = 1; /* subdirs have inherited nodump */
343 CLRINO(ino, dumpdirmap);
344 } else if ((ret & HASSUBDIRS) == 0) {
345 if (!TSTINO(ino, dumpinomap)) {
346 CLRINO(ino, dumpdirmap);
347 change = 1;
348 }
349 }
350 }
351 return (change);
352 }
353
354 /*
355 * Read indirect blocks, and pass the data blocks to be searched
356 * as directories. Quit as soon as any entry is found that will
357 * require the directory to be dumped.
358 */
359 static int
360 dirindir(ino_t ino, daddr_t blkno, int ind_level, long *filesize,
361 long *tapesize, int nodump)
362 {
363 int ret = 0;
364 int i;
365 daddr_t idblk[MAXNINDIR];
366
367 bread(fsatoda(ufsib, iswap32(blkno)), (char *)idblk,
368 (int)ufsib->ufs_bsize);
369 if (ind_level <= 0) {
370 for (i = 0; *filesize > 0 && i < ufsib->ufs_nindir; i++) {
371 blkno = idblk[i];
372 if (blkno != 0)
373 ret |= searchdir(ino, iswap32(blkno),
374 ufsib->ufs_bsize, *filesize,
375 tapesize, nodump);
376 if (ret & HASDUMPEDFILE)
377 *filesize = 0;
378 else
379 *filesize -= ufsib->ufs_bsize;
380 }
381 return (ret);
382 }
383 ind_level--;
384 for (i = 0; *filesize > 0 && i < ufsib->ufs_nindir; i++) {
385 blkno = idblk[i];
386 if (blkno != 0)
387 ret |= dirindir(ino, blkno, ind_level, filesize,
388 tapesize, nodump);
389 }
390 return (ret);
391 }
392
393 /*
394 * Scan a disk block containing directory information looking to see if
395 * any of the entries are on the dump list and to see if the directory
396 * contains any subdirectories.
397 */
398 static int
399 searchdir(ino_t dino, daddr_t blkno, long size, long filesize,
400 long *tapesize, int nodump)
401 {
402 struct direct *dp;
403 struct dinode *ip;
404 long loc, ret = 0;
405 char dblk[MAXBSIZE];
406 ino_t ino;
407
408 bread(fsatoda(ufsib, blkno), dblk, (int)size);
409 if (filesize < size)
410 size = filesize;
411 for (loc = 0; loc < size; ) {
412 dp = (struct direct *)(dblk + loc);
413 if (dp->d_reclen == 0) {
414 msg("corrupted directory, inumber %d\n", dino);
415 break;
416 }
417 loc += iswap16(dp->d_reclen);
418 if (dp->d_ino == 0)
419 continue;
420 if (dp->d_name[0] == '.') {
421 if (dp->d_name[1] == '\0')
422 continue;
423 if (dp->d_name[1] == '.' && dp->d_name[2] == '\0')
424 continue;
425 }
426 ino = iswap32(dp->d_ino);
427 if (nodump) {
428 ip = getino(ino);
429 if (TSTINO(ino, dumpinomap)) {
430 CLRINO(ino, dumpinomap);
431 CLRINO(ino, usedinomap);
432 *tapesize -= blockest(ip);
433 }
434 /* Add dir back to the dir map, to propagate nodump */
435 if ((ip->di_mode & IFMT) == IFDIR) {
436 SETINO(ino, dumpdirmap);
437 ret |= HASSUBDIRS;
438 }
439 } else {
440 if (TSTINO(ino, dumpinomap)) {
441 ret |= HASDUMPEDFILE;
442 if (ret & HASSUBDIRS)
443 break;
444 }
445 if (TSTINO(ino, dumpdirmap)) {
446 ret |= HASSUBDIRS;
447 if (ret & HASDUMPEDFILE)
448 break;
449 }
450 }
451 }
452 return (ret);
453 }
454
455 /*
456 * Dump passes 3 and 4.
457 *
458 * Dump the contents of an inode to tape.
459 */
460 void
461 dumpino(struct dinode *dp, ino_t ino)
462 {
463 int ind_level, cnt;
464 fsizeT size;
465 char buf[TP_BSIZE];
466
467 if (newtape) {
468 newtape = 0;
469 dumpmap(dumpinomap, TS_BITS, ino);
470 }
471 CLRINO(ino, dumpinomap);
472 if (needswap)
473 ffs_dinode_swap(dp, &spcl.c_dinode);
474 else
475 spcl.c_dinode = *dp;
476 spcl.c_type = iswap32(TS_INODE);
477 spcl.c_count = 0;
478 switch (dp->di_mode & IFMT) {
479
480 case 0:
481 /*
482 * Freed inode.
483 */
484 return;
485
486 case IFLNK:
487 /*
488 * Check for short symbolic link.
489 */
490 if (dp->di_size > 0 &&
491 #ifdef FS_44INODEFMT
492 (dp->di_size < ufsib->ufs_maxsymlinklen ||
493 (ufsib->ufs_maxsymlinklen == 0 && dp->di_blocks == 0))
494 #else
495 dp->di_blocks == 0
496 #endif
497 ) {
498 spcl.c_addr[0] = 1;
499 spcl.c_count = iswap32(1);
500 writeheader(ino);
501 memmove(buf, dp->di_shortlink, (u_long)dp->di_size);
502 buf[dp->di_size] = '\0';
503 writerec(buf, 0);
504 return;
505 }
506 /* fall through */
507
508 case IFDIR:
509 case IFREG:
510 if (dp->di_size > 0)
511 break;
512 /* fall through */
513
514 case IFIFO:
515 case IFSOCK:
516 case IFCHR:
517 case IFBLK:
518 writeheader(ino);
519 return;
520
521 default:
522 msg("Warning: undefined file type 0%o\n", dp->di_mode & IFMT);
523 return;
524 }
525 if (dp->di_size > NDADDR * ufsib->ufs_bsize)
526 cnt = NDADDR * ufsib->ufs_frag;
527 else
528 cnt = howmany(dp->di_size, ufsib->ufs_fsize);
529 blksout(&dp->di_db[0], cnt, ino);
530 if ((size = dp->di_size - NDADDR * ufsib->ufs_bsize) <= 0)
531 return;
532 for (ind_level = 0; ind_level < NIADDR; ind_level++) {
533 dmpindir(ino, dp->di_ib[ind_level], ind_level, &size);
534 if (size <= 0)
535 return;
536 }
537 }
538
539 /*
540 * Read indirect blocks, and pass the data blocks to be dumped.
541 */
542 static void
543 dmpindir(ino_t ino, daddr_t blk, int ind_level, fsizeT *size)
544 {
545 int i, cnt;
546 daddr_t idblk[MAXNINDIR];
547
548 if (blk != 0)
549 bread(fsatoda(ufsib, iswap32(blk)), (char *)idblk,
550 (int) ufsib->ufs_bsize);
551 else
552 memset(idblk, 0, (int)ufsib->ufs_bsize);
553 if (ind_level <= 0) {
554 if (*size < ufsib->ufs_nindir * ufsib->ufs_bsize)
555 cnt = howmany(*size, ufsib->ufs_fsize);
556 else
557 cnt = ufsib->ufs_nindir * ufsib->ufs_frag;
558 *size -= ufsib->ufs_nindir * ufsib->ufs_bsize;
559 blksout(&idblk[0], cnt, ino);
560 return;
561 }
562 ind_level--;
563 for (i = 0; i < ufsib->ufs_nindir; i++) {
564 dmpindir(ino, idblk[i], ind_level, size);
565 if (*size <= 0)
566 return;
567 }
568 }
569
570 /*
571 * Collect up the data into tape record sized buffers and output them.
572 */
573 void
574 blksout(daddr_t *blkp, int frags, ino_t ino)
575 {
576 daddr_t *bp;
577 int i, j, count, blks, tbperdb;
578
579 blks = howmany(frags * ufsib->ufs_fsize, TP_BSIZE);
580 tbperdb = ufsib->ufs_bsize >> tp_bshift;
581 for (i = 0; i < blks; i += TP_NINDIR) {
582 if (i + TP_NINDIR > blks)
583 count = blks;
584 else
585 count = i + TP_NINDIR;
586 for (j = i; j < count; j++)
587 if (blkp[j / tbperdb] != 0)
588 spcl.c_addr[j - i] = 1;
589 else
590 spcl.c_addr[j - i] = 0;
591 spcl.c_count = iswap32(count - i);
592 writeheader(ino);
593 bp = &blkp[i / tbperdb];
594 for (j = i; j < count; j += tbperdb, bp++)
595 if (*bp != 0) {
596 if (j + tbperdb <= count)
597 dumpblock(iswap32(*bp), (int)ufsib->ufs_bsize);
598 else
599 dumpblock(iswap32(*bp), (count - j) * TP_BSIZE);
600 }
601 spcl.c_type = iswap32(TS_ADDR);
602 }
603 }
604
605 /*
606 * Dump a map to the tape.
607 */
608 void
609 dumpmap(char *map, int type, ino_t ino)
610 {
611 int i;
612 char *cp;
613
614 spcl.c_type = iswap32(type);
615 spcl.c_count = iswap32(howmany(mapsize * sizeof(char), TP_BSIZE));
616 writeheader(ino);
617 for (i = 0, cp = map; i < iswap32(spcl.c_count); i++, cp += TP_BSIZE)
618 writerec(cp, 0);
619 }
620
621 /*
622 * Write a header record to the dump tape.
623 */
624 void
625 writeheader(ino_t ino)
626 {
627 int32_t sum, cnt, *lp;
628
629 spcl.c_inumber = iswap32(ino);
630 spcl.c_magic = iswap32(NFS_MAGIC);
631 spcl.c_checksum = 0;
632 lp = (int32_t *)&spcl;
633 sum = 0;
634 cnt = sizeof(union u_spcl) / (4 * sizeof(int32_t));
635 while (--cnt >= 0) {
636 sum += iswap32(*lp++);
637 sum += iswap32(*lp++);
638 sum += iswap32(*lp++);
639 sum += iswap32(*lp++);
640 }
641 spcl.c_checksum = iswap32(CHECKSUM - sum);
642 writerec((char *)&spcl, 1);
643 }
644