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