1 /* $NetBSD: traverse.c,v 1.47 2006/06/24 05:28:54 perseant Exp $ */
4 * Copyright (c) 1980, 1988, 1991, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/cdefs.h>
35 static char sccsid
[] = "@(#)traverse.c 8.7 (Berkeley) 6/15/95";
37 __RCSID("$NetBSD: traverse.c,v 1.47 2006/06/24 05:28:54 perseant Exp $");
41 #include <sys/param.h>
44 #include <ufs/ufs/dir.h>
45 #include <ufs/ufs/dinode.h>
46 #include <ufs/ffs/fs.h>
47 #include <ufs/ffs/ffs_extern.h>
49 #include <protocols/dumprestore.h>
61 #define HASDUMPEDFILE 0x1
62 #define HASSUBDIRS 0x2
64 static int dirindir(ino_t
, daddr_t
, int, off_t
*, u_int64_t
*, int);
65 static void dmpindir(ino_t
, daddr_t
, int, off_t
*);
66 static int searchdir(ino_t
, daddr_t
, long, off_t
, u_int64_t
*, int);
69 * This is an estimation of the number of TP_BSIZE blocks in the file.
70 * It estimates the number of blocks in files with holes by assuming
71 * that all of the blocks accounted for by di_blocks are data blocks
72 * (when some of the blocks are usually used for indirect pointers);
73 * hence the estimate may be high.
76 blockest(union dinode
*dp
)
78 int64_t blkest
, sizeest
;
81 * dp->di_size is the size of the file in bytes.
82 * dp->di_blocks stores the number of sectors actually in the file.
83 * If there are more sectors than the size would indicate, this just
84 * means that there are indirect blocks in the file or unused
85 * sectors in the last file block; we can safely ignore these
86 * (blkest = sizeest below).
87 * If the file is bigger than the number of sectors would indicate,
88 * then the file has holes in it. In this case we must use the
89 * block count to estimate the number of data blocks used, but
90 * we use the actual size for estimating the number of indirect
91 * dump blocks (sizeest vs. blkest in the indirect block
94 if (DIP(dp
, flags
) & SF_SNAPSHOT
)
96 blkest
= howmany(ufs_fragroundup(ufsib
,
97 dbtob((u_int64_t
)DIP(dp
, blocks
))), TP_BSIZE
);
98 sizeest
= howmany(ufs_fragroundup(ufsib
, DIP(dp
, size
)), TP_BSIZE
);
101 if (DIP(dp
, size
) > ufsib
->ufs_bsize
* NDADDR
) {
102 /* calculate the number of indirect blocks on the dump tape */
104 howmany(sizeest
- NDADDR
* ufsib
->ufs_bsize
/ TP_BSIZE
,
110 /* Auxiliary macro to pick up files changed since previous dump. */
111 #define CHANGEDSINCE(dp, t) \
112 (DIP((dp), mtime) >= (t) || DIP((dp), ctime) >= (t))
114 /* The WANTTODUMP macro decides whether a file should be dumped. */
116 #define WANTTODUMP(dp) \
117 (CHANGEDSINCE(dp, iswap64(spcl.c_ddate)) && \
118 (nonodump || (DIP((dp), flags) & UF_NODUMP) != UF_NODUMP))
120 #define WANTTODUMP(dp) CHANGEDSINCE(dp, iswap64(spcl.c_ddate))
124 * Determine if given inode should be dumped
127 mapfileino(ino_t ino
, u_int64_t
*tape_size
, int *dirskipped
)
133 * Skip inode if we've already marked it for dumping
135 if (TSTINO(ino
, usedinomap
))
138 if (dp
== NULL
|| (mode
= (DIP(dp
, mode
) & IFMT
)) == 0)
141 * Skip WAPBL log file inodes.
143 if (DIP(dp
, flags
) & SF_LOG
)
146 * Put all dirs in dumpdirmap, inodes that are to be dumped in the
147 * used map. All inode but dirs who have the nodump attribute go
150 SETINO(ino
, usedinomap
);
152 SETINO(ino
, dumpdirmap
);
153 if (WANTTODUMP(dp
)) {
154 SETINO(ino
, dumpinomap
);
155 if (mode
!= IFREG
&& mode
!= IFDIR
&& mode
!= IFLNK
)
158 *tape_size
+= blockest(dp
);
163 if (!nonodump
&& (DIP(dp
, flags
) & UF_NODUMP
))
164 CLRINO(ino
, usedinomap
);
173 * Walk the inode list for a file system to find all allocated inodes
174 * that have been modified since the previous dump time. Also, find all
175 * the directories in the file system.
176 * disk may be NULL if dirv is NULL.
179 mapfiles(ino_t maxino
, u_int64_t
*tape_size
, char *diskname
, char * const *dirv
)
181 int anydirskipped
= 0;
184 char curdir
[MAXPATHLEN
];
189 if (getcwd(curdir
, sizeof(curdir
)) == NULL
) {
190 msg("Can't determine cwd: %s\n", strerror(errno
));
193 if ((dirh
= fts_open(dirv
, FTS_PHYSICAL
|FTS_SEEDOT
|FTS_XDEV
,
195 msg("fts_open failed: %s\n", strerror(errno
));
198 while ((entry
= fts_read(dirh
)) != NULL
) {
199 switch (entry
->fts_info
) {
200 case FTS_DNR
: /* an error */
203 msg("Can't fts_read %s: %s\n", entry
->fts_path
,
205 case FTS_DP
: /* already seen dir */
208 mapfileino(entry
->fts_statp
->st_ino
, tape_size
,
211 (void)fts_close(dirh
);
214 * Add any parent directories
216 for (d
= 0 ; dirv
[d
] != NULL
; d
++) {
217 char path
[MAXPATHLEN
];
219 if (dirv
[d
][0] != '/')
220 (void)snprintf(path
, sizeof(path
), "%s/%s",
223 (void)snprintf(path
, sizeof(path
), "%s",
225 while (strcmp(path
, diskname
) != 0) {
231 if ((p
= strrchr(path
, '/')) == NULL
)
236 if (stat(path
, &sb
) == -1) {
237 msg("Can't stat %s: %s\n", path
,
241 mapfileino(sb
.st_ino
, tape_size
, &anydirskipped
);
246 * Ensure that the root inode actually appears in the
247 * file list for a subdir
249 mapfileino(ROOTINO
, tape_size
, &anydirskipped
);
251 fs_mapinodes(maxino
, tape_size
, &anydirskipped
);
254 * Restore gets very upset if the root is not dumped,
255 * so ensure that it always is dumped.
257 SETINO(ROOTINO
, dumpinomap
);
258 return (anydirskipped
);
264 * Scan each directory on the file system to see if it has any modified
265 * files in it. If it does, and has not already been added to the dump
266 * list (because it was itself modified), then add it. If a directory
267 * has not been modified itself, contains no modified files and has no
268 * subdirectories, then it can be deleted from the dump list and from
269 * the list of directories. By deleting it from the list of directories,
270 * its parent may now qualify for the same treatment on this or a later
271 * pass using this algorithm.
274 mapdirs(ino_t maxino
, u_int64_t
*tape_size
)
276 union dinode
*dp
, di
;
277 int i
, isdir
, nodump
;
284 isdir
= 0; /* XXX just to get gcc to shut up */
285 for (map
= dumpdirmap
, ino
= 1; ino
< maxino
; ino
++) {
286 if (((ino
- 1) % NBBY
) == 0) /* map is offset by 1 */
291 * If dir has been removed from the used map, it's either
292 * because it had the nodump flag, or it inherited it from
293 * its parent. A directory can't be in dumpinomap if
294 * not in usedinomap, but we have to go throuh it anyway
295 * to propagate the nodump attribute.
297 nodump
= (TSTINO(ino
, usedinomap
) == 0);
298 if ((isdir
& 1) == 0 ||
299 (TSTINO(ino
, dumpinomap
) && nodump
== 0))
304 * inode buf may be changed in searchdir().
310 filesize
= DIP(&di
, size
);
311 for (ret
= 0, i
= 0; filesize
> 0 && i
< NDADDR
; i
++) {
313 blk
= iswap64(di
.dp2
.di_db
[i
]);
315 blk
= iswap32(di
.dp1
.di_db
[i
]);
317 ret
|= searchdir(ino
, blk
,
318 (long)ufs_dblksize(ufsib
, &di
, i
),
319 filesize
, tape_size
, nodump
);
320 if (ret
& HASDUMPEDFILE
)
323 filesize
-= ufsib
->ufs_bsize
;
325 for (i
= 0; filesize
> 0 && i
< NIADDR
; i
++) {
327 blk
= iswap64(di
.dp2
.di_ib
[i
]);
329 blk
= iswap32(di
.dp1
.di_ib
[i
]);
332 ret
|= dirindir(ino
, blk
, i
, &filesize
,
335 if (ret
& HASDUMPEDFILE
) {
336 SETINO(ino
, dumpinomap
);
337 *tape_size
+= blockest(&di
);
342 if (ret
& HASSUBDIRS
)
343 change
= 1; /* subdirs have inherited nodump */
344 CLRINO(ino
, dumpdirmap
);
345 } else if ((ret
& HASSUBDIRS
) == 0) {
346 if (!TSTINO(ino
, dumpinomap
)) {
347 CLRINO(ino
, dumpdirmap
);
356 * Read indirect blocks, and pass the data blocks to be searched
357 * as directories. Quit as soon as any entry is found that will
358 * require the directory to be dumped.
361 dirindir(ino_t ino
, daddr_t blkno
, int ind_level
, off_t
*filesize
,
362 u_int64_t
*tape_size
, int nodump
)
367 int64_t i64
[MAXBSIZE
/ sizeof (int64_t)];
368 int32_t i32
[MAXBSIZE
/ sizeof (int32_t)];
371 bread(fsatoda(ufsib
, blkno
), (char *)&idblk
, (int)ufsib
->ufs_bsize
);
372 if (ind_level
<= 0) {
373 for (i
= 0; *filesize
> 0 && i
< ufsib
->ufs_nindir
; i
++) {
375 blkno
= iswap64(idblk
.i64
[i
]);
377 blkno
= iswap32(idblk
.i32
[i
]);
379 ret
|= searchdir(ino
, blkno
,
380 ufsib
->ufs_bsize
, *filesize
,
382 if (ret
& HASDUMPEDFILE
)
385 *filesize
-= ufsib
->ufs_bsize
;
390 for (i
= 0; *filesize
> 0 && i
< ufsib
->ufs_nindir
; i
++) {
392 blkno
= iswap64(idblk
.i64
[i
]);
394 blkno
= iswap32(idblk
.i32
[i
]);
396 ret
|= dirindir(ino
, blkno
, ind_level
, filesize
,
403 * Scan a disk block containing directory information looking to see if
404 * any of the entries are on the dump list and to see if the directory
405 * contains any subdirectories.
408 searchdir(ino_t dino
, daddr_t blkno
, long size
, off_t filesize
,
409 u_int64_t
*tape_size
, int nodump
)
419 quit("searchdir: cannot allocate directory memory.\n");
420 bread(fsatoda(ufsib
, blkno
), dblk
, (int)size
);
423 for (loc
= 0; loc
< size
; ) {
424 dp
= (struct direct
*)(dblk
+ loc
);
425 if (dp
->d_reclen
== 0) {
426 msg("corrupted directory, inumber %llu\n",
427 (unsigned long long)dino
);
430 loc
+= iswap16(dp
->d_reclen
);
433 if (dp
->d_name
[0] == '.') {
434 if (dp
->d_name
[1] == '\0')
436 if (dp
->d_name
[1] == '.' && dp
->d_name
[2] == '\0')
439 ino
= iswap32(dp
->d_ino
);
442 if (TSTINO(ino
, dumpinomap
)) {
443 CLRINO(ino
, dumpinomap
);
444 *tape_size
-= blockest(ip
);
447 * Add back to dumpdirmap and remove from usedinomap
448 * to propagate nodump.
450 if ((DIP(ip
, mode
) & IFMT
) == IFDIR
) {
451 SETINO(ino
, dumpdirmap
);
452 CLRINO(ino
, usedinomap
);
456 if (TSTINO(ino
, dumpinomap
)) {
457 ret
|= HASDUMPEDFILE
;
458 if (ret
& HASSUBDIRS
)
461 if (TSTINO(ino
, dumpdirmap
)) {
463 if (ret
& HASDUMPEDFILE
)
473 * Dump passes 3 and 4.
475 * Dump the contents of an inode to tape.
478 dumpino(union dinode
*dp
, ino_t ino
)
488 dumpmap(dumpinomap
, TS_BITS
, ino
);
490 CLRINO(ino
, dumpinomap
);
492 * Zero out the size of a snapshot so that it will be dumped
493 * as a zero length file.
495 if (DIP(dp
, flags
) & SF_SNAPSHOT
) {
496 DIP_SET(dp
, size
, 0);
497 DIP_SET(dp
, flags
, DIP(dp
, flags
) & ~SF_SNAPSHOT
);
501 ffs_dinode1_swap(&dp
->dp1
, &spcl
.c_dinode
);
503 spcl
.c_dinode
= dp
->dp1
;
506 ffs_dinode2_swap(&dp
->dp2
, &dp
->dp2
);
507 spcl
.c_mode
= dp
->dp2
.di_mode
;
508 spcl
.c_size
= dp
->dp2
.di_size
;
509 spcl
.c_atime
= dp
->dp2
.di_atime
;
510 spcl
.c_atimensec
= dp
->dp2
.di_atimensec
;
511 spcl
.c_mtime
= dp
->dp2
.di_mtime
;
512 spcl
.c_mtimensec
= dp
->dp2
.di_mtimensec
;
513 spcl
.c_birthtime
= dp
->dp2
.di_birthtime
;
514 spcl
.c_birthtimensec
= dp
->dp2
.di_birthnsec
;
515 spcl
.c_rdev
= dp
->dp2
.di_rdev
;
516 spcl
.c_file_flags
= dp
->dp2
.di_flags
;
517 spcl
.c_uid
= dp
->dp2
.di_uid
;
518 spcl
.c_gid
= dp
->dp2
.di_gid
;
520 spcl
.c_type
= iswap32(TS_INODE
);
522 switch (DIP(dp
, mode
) & IFMT
) {
532 * Check for short symbolic link.
534 if (DIP(dp
, size
) > 0 &&
536 (DIP(dp
, size
) < ufsib
->ufs_maxsymlinklen
||
537 (ufsib
->ufs_maxsymlinklen
== 0 && DIP(dp
, blocks
) == 0))
543 spcl
.c_count
= iswap32(1);
546 shortlink
= dp
->dp2
.di_db
;
548 shortlink
= dp
->dp1
.di_db
;
549 memmove(buf
, shortlink
, DIP(dp
, size
));
550 buf
[DIP(dp
, size
)] = '\0';
558 if (DIP(dp
, size
) > 0)
570 msg("Warning: undefined file type 0%o\n", DIP(dp
, mode
) & IFMT
);
573 if (DIP(dp
, size
) > NDADDR
* ufsib
->ufs_bsize
)
574 cnt
= NDADDR
* ufsib
->ufs_frag
;
576 cnt
= howmany(DIP(dp
, size
), ufsib
->ufs_fsize
);
578 blksout64(&dp
->dp2
.di_db
[0], cnt
, ino
);
580 blksout32(&dp
->dp1
.di_db
[0], cnt
, ino
);
582 if ((size
= DIP(dp
, size
) - NDADDR
* ufsib
->ufs_bsize
) <= 0)
584 for (ind_level
= 0; ind_level
< NIADDR
; ind_level
++) {
586 blk
= iswap64(dp
->dp2
.di_ib
[ind_level
]);
588 blk
= iswap32(dp
->dp1
.di_ib
[ind_level
]);
589 dmpindir(ino
, blk
, ind_level
, &size
);
596 * Read indirect blocks, and pass the data blocks to be dumped.
599 dmpindir(ino_t ino
, daddr_t blk
, int ind_level
, off_t
*size
)
603 int32_t i32
[MAXBSIZE
/ sizeof (int32_t)];
604 int64_t i64
[MAXBSIZE
/ sizeof (int64_t)];
610 bread(fsatoda(ufsib
, blk
), (char *)&idblk
,
611 (int) ufsib
->ufs_bsize
);
613 memset(&idblk
, 0, (int)ufsib
->ufs_bsize
);
614 if (ind_level
<= 0) {
615 if (*size
< ufsib
->ufs_nindir
* ufsib
->ufs_bsize
)
616 cnt
= howmany(*size
, ufsib
->ufs_fsize
);
618 cnt
= ufsib
->ufs_nindir
* ufsib
->ufs_frag
;
619 *size
-= ufsib
->ufs_nindir
* ufsib
->ufs_bsize
;
621 blksout64(&idblk
.i64
[0], cnt
, ino
);
623 blksout32(&idblk
.i32
[0], cnt
, ino
);
627 for (i
= 0; i
< ufsib
->ufs_nindir
; i
++) {
629 iblk
= iswap64(idblk
.i64
[i
]);
631 iblk
= iswap32(idblk
.i32
[i
]);
632 dmpindir(ino
, iblk
, ind_level
, size
);
639 * Collect up the data into tape record sized buffers and output them.
642 blksout32(int32_t *blkp
, int frags
, ino_t ino
)
645 int i
, j
, count
, blks
, tbperdb
;
647 blks
= howmany(frags
* ufsib
->ufs_fsize
, TP_BSIZE
);
648 tbperdb
= ufsib
->ufs_bsize
>> tp_bshift
;
649 for (i
= 0; i
< blks
; i
+= TP_NINDIR
) {
650 if (i
+ TP_NINDIR
> blks
)
653 count
= i
+ TP_NINDIR
;
654 for (j
= i
; j
< count
; j
++)
655 if (blkp
[j
/ tbperdb
] != 0)
656 spcl
.c_addr
[j
- i
] = 1;
658 spcl
.c_addr
[j
- i
] = 0;
659 spcl
.c_count
= iswap32(count
- i
);
661 bp
= &blkp
[i
/ tbperdb
];
662 for (j
= i
; j
< count
; j
+= tbperdb
, bp
++)
664 if (j
+ tbperdb
<= count
)
665 dumpblock(iswap32(*bp
), (int)ufsib
->ufs_bsize
);
667 dumpblock(iswap32(*bp
), (count
- j
) * TP_BSIZE
);
669 spcl
.c_type
= iswap32(TS_ADDR
);
674 blksout64(int64_t *blkp
, int frags
, ino_t ino
)
677 int i
, j
, count
, blks
, tbperdb
;
679 blks
= howmany(frags
* ufsib
->ufs_fsize
, TP_BSIZE
);
680 tbperdb
= ufsib
->ufs_bsize
>> tp_bshift
;
681 for (i
= 0; i
< blks
; i
+= TP_NINDIR
) {
682 if (i
+ TP_NINDIR
> blks
)
685 count
= i
+ TP_NINDIR
;
686 for (j
= i
; j
< count
; j
++)
687 if (blkp
[j
/ tbperdb
] != 0)
688 spcl
.c_addr
[j
- i
] = 1;
690 spcl
.c_addr
[j
- i
] = 0;
691 spcl
.c_count
= iswap32(count
- i
);
693 bp
= &blkp
[i
/ tbperdb
];
694 for (j
= i
; j
< count
; j
+= tbperdb
, bp
++)
696 if (j
+ tbperdb
<= count
)
697 dumpblock(iswap64(*bp
), (int)ufsib
->ufs_bsize
);
699 dumpblock(iswap64(*bp
), (count
- j
) * TP_BSIZE
);
701 spcl
.c_type
= iswap32(TS_ADDR
);
706 * Dump a map to the tape.
709 dumpmap(char *map
, int type
, ino_t ino
)
714 spcl
.c_type
= iswap32(type
);
715 spcl
.c_count
= iswap32(howmany(mapsize
* sizeof(char), TP_BSIZE
));
717 for (i
= 0, cp
= map
; i
< iswap32(spcl
.c_count
); i
++, cp
+= TP_BSIZE
)
722 * Write a header record to the dump tape.
725 writeheader(ino_t ino
)
727 int32_t sum
, cnt
, *lp
;
729 spcl
.c_inumber
= iswap32(ino
);
731 spcl
.c_magic
= iswap32(FS_UFS2_MAGIC
);
733 spcl
.c_magic
= iswap32(NFS_MAGIC
);
734 spcl
.c_old_date
= iswap32(iswap64(spcl
.c_date
));
735 spcl
.c_old_ddate
= iswap32(iswap64(spcl
.c_ddate
));
736 spcl
.c_old_tapea
= iswap32(iswap64(spcl
.c_tapea
));
737 spcl
.c_old_firstrec
= iswap32(iswap64(spcl
.c_firstrec
));
740 lp
= (int32_t *)&spcl
;
742 cnt
= sizeof(union u_spcl
) / (4 * sizeof(int32_t));
744 sum
+= iswap32(*lp
++);
745 sum
+= iswap32(*lp
++);
746 sum
+= iswap32(*lp
++);
747 sum
+= iswap32(*lp
++);
749 spcl
.c_checksum
= iswap32(CHECKSUM
- sum
);
750 writerec((char *)&spcl
, 1);