2 * Copyright (c) 1980, 1988, 1991, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * @(#)traverse.c 8.7 (Berkeley) 6/15/95
30 * $FreeBSD: src/sbin/dump/traverse.c,v 1.10.2.6 2003/04/14 20:10:35 johan Exp $
33 #include <sys/param.h>
35 #include <vfs/ufs/dir.h>
36 #include <vfs/ufs/dinode.h>
37 #include <vfs/ufs/fs.h>
39 #include <protocols/dumprestore.h>
49 #define HASDUMPEDFILE 0x1
50 #define HASSUBDIRS 0x2
53 typedef quad_t fsizeT
;
58 static int dirindir(ufs1_ino_t
, daddr_t
, int, long *, long *, int);
59 static void dmpindir(ufs1_ino_t
, daddr_t
, int, fsizeT
*);
60 static int searchdir(ufs1_ino_t
, daddr_t
, long, long, long *, int);
63 * This is an estimation of the number of TP_BSIZE blocks in the file.
64 * It estimates the number of blocks in files with holes by assuming
65 * that all of the blocks accounted for by di_blocks are data blocks
66 * (when some of the blocks are usually used for indirect pointers);
67 * hence the estimate may be high.
70 blockest(struct ufs1_dinode
*dp
)
75 * dp->di_size is the size of the file in bytes.
76 * dp->di_blocks stores the number of sectors actually in the file.
77 * If there are more sectors than the size would indicate, this just
78 * means that there are indirect blocks in the file or unused
79 * sectors in the last file block; we can safely ignore these
80 * (blkest = sizeest below).
81 * If the file is bigger than the number of sectors would indicate,
82 * then the file has holes in it. In this case we must use the
83 * block count to estimate the number of data blocks used, but
84 * we use the actual size for estimating the number of indirect
85 * dump blocks (sizeest vs. blkest in the indirect block
88 blkest
= howmany(dbtob(dp
->di_blocks
), TP_BSIZE
);
89 sizeest
= howmany(dp
->di_size
, TP_BSIZE
);
92 if (dp
->di_size
> (unsigned)sblock
->fs_bsize
* UFS_NDADDR
) {
93 /* calculate the number of indirect blocks on the dump tape */
95 howmany(sizeest
- UFS_NDADDR
* sblock
->fs_bsize
/ TP_BSIZE
,
101 /* Auxiliary macro to pick up files changed since previous dump. */
102 #define CHANGEDSINCE(dp, t) \
103 ((dp)->di_mtime >= (t) || (dp)->di_ctime >= (t))
105 /* The WANTTODUMP macro decides whether a file should be dumped. */
107 #define WANTTODUMP(dp) \
108 (CHANGEDSINCE(dp, spcl.c_ddate) && \
109 (nonodump || ((dp)->di_flags & UF_NODUMP) != UF_NODUMP))
111 #define WANTTODUMP(dp) CHANGEDSINCE(dp, spcl.c_ddate)
117 * Walk the inode list for a filesystem to find all allocated inodes
118 * that have been modified since the previous dump time. Also, find all
119 * the directories in the filesystem.
122 mapfiles(ufs1_ino_t maxino
, long *tape_size
)
126 struct ufs1_dinode
*dp
;
127 int anydirskipped
= 0;
129 for (ino
= UFS_ROOTINO
; ino
< maxino
; ino
++) {
131 if ((mode
= (dp
->di_mode
& IFMT
)) == 0)
134 * Everything must go in usedinomap so that a check
135 * for "in dumpdirmap but not in usedinomap" to detect
136 * dirs with nodump set has a chance of succeeding
137 * (this is used in mapdirs()).
139 SETINO(ino
, usedinomap
);
141 SETINO(ino
, dumpdirmap
);
142 if (WANTTODUMP(dp
)) {
143 SETINO(ino
, dumpinomap
);
144 if (mode
!= IFREG
&& mode
!= IFDIR
&& mode
!= IFLNK
)
147 *tape_size
+= blockest(dp
);
151 if (!nonodump
&& (dp
->di_flags
& UF_NODUMP
))
152 CLRINO(ino
, usedinomap
);
157 * Restore gets very upset if the root is not dumped,
158 * so ensure that it always is dumped.
160 SETINO(UFS_ROOTINO
, dumpinomap
);
161 return (anydirskipped
);
167 * Scan each directory on the filesystem to see if it has any modified
168 * files in it. If it does, and has not already been added to the dump
169 * list (because it was itself modified), then add it. If a directory
170 * has not been modified itself, contains no modified files and has no
171 * subdirectories, then it can be deleted from the dump list and from
172 * the list of directories. By deleting it from the list of directories,
173 * its parent may now qualify for the same treatment on this or a later
174 * pass using this algorithm.
177 mapdirs(ufs1_ino_t maxino
, long *tape_size
)
179 struct ufs1_dinode
*dp
;
184 struct ufs1_dinode di
;
188 isdir
= 0; /* XXX just to get gcc to shut up */
189 for (map
= dumpdirmap
, ino
= 1; ino
< maxino
; ino
++) {
190 if (((ino
- 1) % NBBY
) == 0) /* map is offset by 1 */
195 * If a directory has been removed from usedinomap, it
196 * either has the nodump flag set, or has inherited
197 * it. Although a directory can't be in dumpinomap if
198 * it isn't in usedinomap, we have to go through it to
199 * propagate the nodump flag.
201 nodump
= !nonodump
&& (TSTINO(ino
, usedinomap
) == 0);
202 if ((isdir
& 1) == 0 || (TSTINO(ino
, dumpinomap
) && !nodump
))
205 di
= *dp
; /* inode buf may change in searchdir(). */
206 filesize
= di
.di_size
;
207 for (ret
= 0, i
= 0; filesize
> 0 && i
< UFS_NDADDR
; i
++) {
208 if (di
.di_db
[i
] != 0) {
209 ret
|= searchdir(ino
, di
.di_db
[i
],
210 (long)dblksize(sblock
, &di
, i
),
211 filesize
, tape_size
, nodump
);
213 if (ret
& HASDUMPEDFILE
)
216 filesize
-= sblock
->fs_bsize
;
218 for (i
= 0; filesize
> 0 && i
< UFS_NIADDR
; i
++) {
219 if (di
.di_ib
[i
] == 0)
221 ret
|= dirindir(ino
, di
.di_ib
[i
], i
, &filesize
,
224 if (ret
& HASDUMPEDFILE
) {
225 SETINO(ino
, dumpinomap
);
226 *tape_size
+= blockest(&di
);
231 if (ret
& HASSUBDIRS
)
232 change
= 1; /* subdirs inherit nodump */
233 CLRINO(ino
, dumpdirmap
);
234 } else if ((ret
& HASSUBDIRS
) == 0)
235 if (!TSTINO(ino
, dumpinomap
)) {
236 CLRINO(ino
, dumpdirmap
);
244 * Read indirect blocks, and pass the data blocks to be searched
245 * as directories. Quit as soon as any entry is found that will
246 * require the directory to be dumped.
249 dirindir(ufs1_ino_t ino
, daddr_t blkno
, int ind_level
, long *filesize
,
250 long *tape_size
, int nodump
)
254 daddr_t idblk
[MAXNINDIR
];
256 bread(fsbtodb(sblock
, blkno
), (char *)idblk
, (int)sblock
->fs_bsize
);
257 if (ind_level
<= 0) {
258 for (i
= 0; *filesize
> 0 && i
< NINDIR(sblock
); i
++) {
261 ret
|= searchdir(ino
, blkno
, sblock
->fs_bsize
,
262 *filesize
, tape_size
, nodump
);
264 if (ret
& HASDUMPEDFILE
)
267 *filesize
-= sblock
->fs_bsize
;
272 for (i
= 0; *filesize
> 0 && i
< NINDIR(sblock
); i
++) {
275 ret
|= dirindir(ino
, blkno
, ind_level
, filesize
,
283 * Scan a disk block containing directory information looking to see if
284 * any of the entries are on the dump list and to see if the directory
285 * contains any subdirectories.
288 searchdir(ufs1_ino_t ino
, daddr_t blkno
, long size
, long filesize
,
289 long *tape_size
, int nodump
)
292 struct ufs1_dinode
*ip
;
296 bread(fsbtodb(sblock
, blkno
), dblk
, (int)size
);
299 for (loc
= 0; loc
< size
; ) {
300 dp
= (struct direct
*)(dblk
+ loc
);
301 if (dp
->d_reclen
== 0) {
302 msg("corrupted directory, inumber %d\n", ino
);
308 if (dp
->d_name
[0] == '.') {
309 if (dp
->d_name
[1] == '\0')
311 if (dp
->d_name
[1] == '.' && dp
->d_name
[2] == '\0')
315 ip
= getino(dp
->d_ino
);
316 if (TSTINO(dp
->d_ino
, dumpinomap
)) {
317 CLRINO(dp
->d_ino
, dumpinomap
);
318 *tape_size
-= blockest(ip
);
321 * Add back to dumpdirmap and remove from usedinomap
322 * to propagate nodump.
324 if ((ip
->di_mode
& IFMT
) == IFDIR
) {
325 SETINO(dp
->d_ino
, dumpdirmap
);
326 CLRINO(dp
->d_ino
, usedinomap
);
330 if (TSTINO(dp
->d_ino
, dumpinomap
)) {
331 ret
|= HASDUMPEDFILE
;
332 if (ret
& HASSUBDIRS
)
335 if (TSTINO(dp
->d_ino
, dumpdirmap
)) {
337 if (ret
& HASDUMPEDFILE
)
346 * Dump passes 3 and 4.
348 * Dump the contents of an inode to tape.
351 dumpino(struct ufs1_dinode
*dp
, ufs1_ino_t ino
)
359 dumpmap(dumpinomap
, TS_BITS
, ino
);
361 CLRINO(ino
, dumpinomap
);
363 spcl
.c_type
= TS_INODE
;
365 switch (dp
->di_mode
& S_IFMT
) {
375 * Check for short symbolic link.
378 if (dp
->di_size
> 0 &&
379 dp
->di_size
< (unsigned)sblock
->fs_maxsymlinklen
) {
383 memmove(buf
, dp
->di_shortlink
, (u_long
)dp
->di_size
);
384 buf
[dp
->di_size
] = '\0';
405 msg("Warning: undefined file type 0%o\n", dp
->di_mode
& IFMT
);
408 if (dp
->di_size
> UFS_NDADDR
* (unsigned)sblock
->fs_bsize
)
409 cnt
= UFS_NDADDR
* sblock
->fs_frag
;
411 cnt
= howmany(dp
->di_size
, sblock
->fs_fsize
);
412 blksout(&dp
->di_db
[0], cnt
, ino
);
413 if ((size
= dp
->di_size
- UFS_NDADDR
* sblock
->fs_bsize
) <= 0)
415 for (ind_level
= 0; ind_level
< UFS_NIADDR
; ind_level
++) {
416 dmpindir(ino
, dp
->di_ib
[ind_level
], ind_level
, &size
);
423 * Read indirect blocks, and pass the data blocks to be dumped.
426 dmpindir(ufs1_ino_t ino
, daddr_t blk
, int ind_level
, fsizeT
*size
)
429 daddr_t idblk
[MAXNINDIR
];
432 bread(fsbtodb(sblock
, blk
), (char *)idblk
, (int) sblock
->fs_bsize
);
434 memset(idblk
, 0, (int)sblock
->fs_bsize
);
435 if (ind_level
<= 0) {
436 if (*size
< NINDIR(sblock
) * sblock
->fs_bsize
)
437 cnt
= howmany(*size
, sblock
->fs_fsize
);
439 cnt
= NINDIR(sblock
) * sblock
->fs_frag
;
440 *size
-= NINDIR(sblock
) * sblock
->fs_bsize
;
441 blksout(&idblk
[0], cnt
, ino
);
445 for (i
= 0; i
< NINDIR(sblock
); i
++) {
446 dmpindir(ino
, idblk
[i
], ind_level
, size
);
453 * Collect up the data into tape record sized buffers and output them.
456 blksout(daddr_t
*blkp
, int frags
, ufs1_ino_t ino
)
459 int i
, j
, count
, blks
, tbperdb
;
461 blks
= howmany(frags
* sblock
->fs_fsize
, TP_BSIZE
);
462 tbperdb
= sblock
->fs_bsize
>> tp_bshift
;
463 for (i
= 0; i
< blks
; i
+= TP_NINDIR
) {
464 if (i
+ TP_NINDIR
> blks
)
467 count
= i
+ TP_NINDIR
;
468 for (j
= i
; j
< count
; j
++)
469 if (blkp
[j
/ tbperdb
] != 0)
470 spcl
.c_addr
[j
- i
] = 1;
472 spcl
.c_addr
[j
- i
] = 0;
473 spcl
.c_count
= count
- i
;
475 bp
= &blkp
[i
/ tbperdb
];
476 for (j
= i
; j
< count
; j
+= tbperdb
, bp
++)
478 if (j
+ tbperdb
<= count
)
479 dumpblock(*bp
, (int)sblock
->fs_bsize
);
481 dumpblock(*bp
, (count
- j
) * TP_BSIZE
);
483 spcl
.c_type
= TS_ADDR
;
488 * Dump a map to the tape.
491 dumpmap(const char *map
, int type
, ufs1_ino_t ino
)
497 spcl
.c_count
= howmany(mapsize
* sizeof(char), TP_BSIZE
);
499 for (i
= 0, cp
= map
; i
< spcl
.c_count
; i
++, cp
+= TP_BSIZE
)
504 * Write a header record to the dump tape.
507 writeheader(ufs1_ino_t ino
)
509 int32_t sum
, cnt
, *lp
;
511 spcl
.c_inumber
= ino
;
512 spcl
.c_magic
= NFS_MAGIC
;
514 lp
= (int32_t *)&spcl
;
516 cnt
= sizeof(union u_spcl
) / (4 * sizeof(int32_t));
523 spcl
.c_checksum
= CHECKSUM
- sum
;
528 getino(ufs1_ino_t inum
)
530 static daddr_t minino
, maxino
;
531 static struct ufs1_dinode inoblock
[MAXINOPB
];
534 if (inum
>= (unsigned)minino
&& inum
< (unsigned)maxino
)
535 return (&inoblock
[inum
- minino
]);
536 bread(fsbtodb(sblock
, ino_to_fsba(sblock
, inum
)), (char *)inoblock
,
537 (int)sblock
->fs_bsize
);
538 minino
= inum
- (inum
% INOPB(sblock
));
539 maxino
= minino
+ INOPB(sblock
);
540 return (&inoblock
[inum
- minino
]);
544 * Read a chunk of data from the disk.
545 * Try to recover from hard errors by reading in sector sized pieces.
546 * Error recovery is attempted at most BREADEMAX times before seeking
547 * consent from the operator to continue.
553 bread(daddr_t blkno
, char *buf
, int size
)
558 cnt
= cread(diskfd
, buf
, size
, ((off_t
)blkno
<< dev_bshift
));
561 if (blkno
+ (size
/ dev_bsize
) > fsbtodb(sblock
, sblock
->fs_size
)) {
563 * Trying to read the final fragment.
565 * NB - dump only works in TP_BSIZE blocks, hence
566 * rounds `dev_bsize' fragments up to TP_BSIZE pieces.
567 * It should be smarter about not actually trying to
568 * read more than it can get, but for the time being
569 * we punt and scale back the read only when it gets
570 * us into trouble. (mkm 9/25/83)
576 msg("read error from %s: %s: [block %d]: count=%d\n",
577 disk
, strerror(errno
), blkno
, size
);
579 msg("short read error from %s: [block %d]: count=%d, got=%d\n",
580 disk
, blkno
, size
, cnt
);
581 if (++breaderrors
> BREADEMAX
) {
582 msg("More than %d block read errors from %s\n",
584 broadcast("DUMP IS AILING!\n");
585 msg("This is an unrecoverable error.\n");
586 if (!query("Do you want to attempt to continue?")){
593 * Zero buffer, then try to read each sector of buffer separately,
594 * and bypass the cache.
596 memset(buf
, 0, size
);
597 for (i
= 0; i
< size
; i
+= dev_bsize
, buf
+= dev_bsize
, blkno
++) {
598 if ((cnt
= pread(diskfd
, buf
, (int)dev_bsize
,
599 ((off_t
)blkno
<< dev_bshift
))) == dev_bsize
)
602 msg("read error from %s: %s: [sector %d]: count=%ld\n",
603 disk
, strerror(errno
), blkno
, dev_bsize
);
606 msg("short read error from %s: [sector %d]: count=%ld, got=%d\n",
607 disk
, blkno
, dev_bsize
, cnt
);