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. 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.
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
33 * @(#)traverse.c 8.7 (Berkeley) 6/15/95
34 * $FreeBSD: src/sbin/dump/traverse.c,v 1.10.2.6 2003/04/14 20:10:35 johan Exp $
35 * $DragonFly: src/sbin/dump/traverse.c,v 1.8 2004/12/27 22:36:37 liamfoy Exp $
38 #include <sys/param.h>
41 #include <sys/vnode.h>
44 #include <ufs/fsdir.h>
45 #include <ufs/inode.h>
47 #include <vfs/ufs/dir.h>
48 #include <vfs/ufs/dinode.h>
49 #include <vfs/ufs/fs.h>
52 #include <protocols/dumprestore.h>
64 #define HASDUMPEDFILE 0x1
65 #define HASSUBDIRS 0x2
68 typedef quad_t fsizeT;
73 static int dirindir(ino_t ino, daddr_t blkno, int level, long *size,
74 long *tapesize, int nodump);
75 static void dmpindir(ino_t ino, daddr_t blk, int level, fsizeT *size);
76 static int searchdir(ino_t ino, daddr_t blkno, long size, long filesize,
77 long *tapesize, int nodump);
80 * This is an estimation of the number of TP_BSIZE blocks in the file.
81 * It estimates the number of blocks in files with holes by assuming
82 * that all of the blocks accounted for by di_blocks are data blocks
83 * (when some of the blocks are usually used for indirect pointers);
84 * hence the estimate may be high.
87 blockest(struct dinode *dp)
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
105 blkest = howmany(dbtob(dp->di_blocks), TP_BSIZE);
106 sizeest = howmany(dp->di_size, TP_BSIZE);
107 if (blkest > sizeest)
109 if (dp->di_size > sblock->fs_bsize * NDADDR) {
110 /* calculate the number of indirect blocks on the dump tape */
112 howmany(sizeest - NDADDR * sblock->fs_bsize / TP_BSIZE,
118 /* Auxiliary macro to pick up files changed since previous dump. */
119 #define CHANGEDSINCE(dp, t) \
120 ((dp)->di_mtime >= (t) || (dp)->di_ctime >= (t))
122 /* The WANTTODUMP macro decides whether a file should be dumped. */
124 #define WANTTODUMP(dp) \
125 (CHANGEDSINCE(dp, spcl.c_ddate) && \
126 (nonodump || ((dp)->di_flags & UF_NODUMP) != UF_NODUMP))
128 #define WANTTODUMP(dp) CHANGEDSINCE(dp, spcl.c_ddate)
134 * Walk the inode list for a filesystem to find all allocated inodes
135 * that have been modified since the previous dump time. Also, find all
136 * the directories in the filesystem.
139 mapfiles(ino_t maxino, long *tapesize)
144 int anydirskipped = 0;
146 for (ino = ROOTINO; ino < maxino; ino++) {
148 if ((mode = (dp->di_mode & IFMT)) == 0)
151 * Everything must go in usedinomap so that a check
152 * for "in dumpdirmap but not in usedinomap" to detect
153 * dirs with nodump set has a chance of succeeding
154 * (this is used in mapdirs()).
156 SETINO(ino, usedinomap);
158 SETINO(ino, dumpdirmap);
159 if (WANTTODUMP(dp)) {
160 SETINO(ino, dumpinomap);
161 if (mode != IFREG && mode != IFDIR && mode != IFLNK)
164 *tapesize += blockest(dp);
168 if (!nonodump && (dp->di_flags & UF_NODUMP))
169 CLRINO(ino, usedinomap);
174 * Restore gets very upset if the root is not dumped,
175 * so ensure that it always is dumped.
177 SETINO(ROOTINO, dumpinomap);
178 return (anydirskipped);
184 * Scan each directory on the filesystem to see if it has any modified
185 * files in it. If it does, and has not already been added to the dump
186 * list (because it was itself modified), then add it. If a directory
187 * has not been modified itself, contains no modified files and has no
188 * subdirectories, then it can be deleted from the dump list and from
189 * the list of directories. By deleting it from the list of directories,
190 * its parent may now qualify for the same treatment on this or a later
191 * pass using this algorithm.
194 mapdirs(ino_t maxino, long *tapesize)
197 int i, isdir, nodump;
204 isdir = 0; /* XXX just to get gcc to shut up */
205 for (map = dumpdirmap, ino = 1; ino < maxino; ino++) {
206 if (((ino - 1) % NBBY) == 0) /* map is offset by 1 */
211 * If a directory has been removed from usedinomap, it
212 * either has the nodump flag set, or has inherited
213 * it. Although a directory can't be in dumpinomap if
214 * it isn't in usedinomap, we have to go through it to
215 * propagate the nodump flag.
217 nodump = !nonodump && (TSTINO(ino, usedinomap) == 0);
218 if ((isdir & 1) == 0 || (TSTINO(ino, dumpinomap) && !nodump))
221 di = *dp; /* inode buf may change in searchdir(). */
222 filesize = di.di_size;
223 for (ret = 0, i = 0; filesize > 0 && i < NDADDR; i++) {
224 if (di.di_db[i] != 0)
225 ret |= searchdir(ino, di.di_db[i],
226 (long)dblksize(sblock, dp, i),
227 filesize, tapesize, nodump);
228 if (ret & HASDUMPEDFILE)
231 filesize -= sblock->fs_bsize;
233 for (i = 0; filesize > 0 && i < NIADDR; i++) {
234 if (di.di_ib[i] == 0)
236 ret |= dirindir(ino, di.di_ib[i], i, &filesize,
239 if (ret & HASDUMPEDFILE) {
240 SETINO(ino, dumpinomap);
241 *tapesize += blockest(dp);
246 if (ret & HASSUBDIRS)
247 change = 1; /* subdirs inherit nodump */
248 CLRINO(ino, dumpdirmap);
249 } else if ((ret & HASSUBDIRS) == 0)
250 if (!TSTINO(ino, dumpinomap)) {
251 CLRINO(ino, dumpdirmap);
259 * Read indirect blocks, and pass the data blocks to be searched
260 * as directories. Quit as soon as any entry is found that will
261 * require the directory to be dumped.
264 dirindir(ino_t ino, daddr_t blkno, int ind_level, long *filesize,
265 long *tapesize, int nodump)
269 daddr_t idblk[MAXNINDIR];
271 bread(fsbtodb(sblock, blkno), (char *)idblk, (int)sblock->fs_bsize);
272 if (ind_level <= 0) {
273 for (i = 0; *filesize > 0 && i < NINDIR(sblock); i++) {
276 ret |= searchdir(ino, blkno, sblock->fs_bsize,
277 *filesize, tapesize, nodump);
278 if (ret & HASDUMPEDFILE)
281 *filesize -= sblock->fs_bsize;
286 for (i = 0; *filesize > 0 && i < NINDIR(sblock); i++) {
289 ret |= dirindir(ino, blkno, ind_level, filesize,
296 * Scan a disk block containing directory information looking to see if
297 * any of the entries are on the dump list and to see if the directory
298 * contains any subdirectories.
301 searchdir(ino_t ino, daddr_t blkno, long size, long filesize,
302 long *tapesize, int nodump)
309 bread(fsbtodb(sblock, blkno), dblk, (int)size);
312 for (loc = 0; loc < size; ) {
313 dp = (struct direct *)(dblk + loc);
314 if (dp->d_reclen == 0) {
315 msg("corrupted directory, inumber %d\n", ino);
321 if (dp->d_name[0] == '.') {
322 if (dp->d_name[1] == '\0')
324 if (dp->d_name[1] == '.' && dp->d_name[2] == '\0')
328 ip = getino(dp->d_ino);
329 if (TSTINO(dp->d_ino, dumpinomap)) {
330 CLRINO(dp->d_ino, dumpinomap);
331 CLRINO(dp->d_ino, usedinomap);
332 *tapesize -= blockest(ip);
334 /* Add back to dumpdirmap to propagate nodump. */
335 if ((ip->di_mode & IFMT) == IFDIR) {
336 SETINO(dp->d_ino, dumpdirmap);
340 if (TSTINO(dp->d_ino, dumpinomap)) {
341 ret |= HASDUMPEDFILE;
342 if (ret & HASSUBDIRS)
345 if (TSTINO(dp->d_ino, dumpdirmap)) {
347 if (ret & HASDUMPEDFILE)
356 * Dump passes 3 and 4.
358 * Dump the contents of an inode to tape.
361 dumpino(struct dinode *dp, ino_t ino)
369 dumpmap(dumpinomap, TS_BITS, ino);
371 CLRINO(ino, dumpinomap);
373 spcl.c_type = TS_INODE;
375 switch (dp->di_mode & S_IFMT) {
385 * Check for short symbolic link.
388 if (dp->di_size > 0 &&
389 dp->di_size < sblock->fs_maxsymlinklen) {
393 memmove(buf, dp->di_shortlink, (u_long)dp->di_size);
394 buf[dp->di_size] = '\0';
415 msg("Warning: undefined file type 0%o\n", dp->di_mode & IFMT);
418 if (dp->di_size > NDADDR * sblock->fs_bsize)
419 cnt = NDADDR * sblock->fs_frag;
421 cnt = howmany(dp->di_size, sblock->fs_fsize);
422 blksout(&dp->di_db[0], cnt, ino);
423 if ((size = dp->di_size - NDADDR * sblock->fs_bsize) <= 0)
425 for (ind_level = 0; ind_level < NIADDR; ind_level++) {
426 dmpindir(ino, dp->di_ib[ind_level], ind_level, &size);
433 * Read indirect blocks, and pass the data blocks to be dumped.
436 dmpindir(ino_t ino, daddr_t blk, int ind_level, fsizeT *size)
439 daddr_t idblk[MAXNINDIR];
442 bread(fsbtodb(sblock, blk), (char *)idblk, (int) sblock->fs_bsize);
444 memset(idblk, 0, (int)sblock->fs_bsize);
445 if (ind_level <= 0) {
446 if (*size < NINDIR(sblock) * sblock->fs_bsize)
447 cnt = howmany(*size, sblock->fs_fsize);
449 cnt = NINDIR(sblock) * sblock->fs_frag;
450 *size -= NINDIR(sblock) * sblock->fs_bsize;
451 blksout(&idblk[0], cnt, ino);
455 for (i = 0; i < NINDIR(sblock); i++) {
456 dmpindir(ino, idblk[i], ind_level, size);
463 * Collect up the data into tape record sized buffers and output them.
466 blksout(daddr_t *blkp, int frags, ino_t ino)
469 int i, j, count, blks, tbperdb;
471 blks = howmany(frags * sblock->fs_fsize, TP_BSIZE);
472 tbperdb = sblock->fs_bsize >> tp_bshift;
473 for (i = 0; i < blks; i += TP_NINDIR) {
474 if (i + TP_NINDIR > blks)
477 count = i + TP_NINDIR;
478 for (j = i; j < count; j++)
479 if (blkp[j / tbperdb] != 0)
480 spcl.c_addr[j - i] = 1;
482 spcl.c_addr[j - i] = 0;
483 spcl.c_count = count - i;
485 bp = &blkp[i / tbperdb];
486 for (j = i; j < count; j += tbperdb, bp++)
488 if (j + tbperdb <= count)
489 dumpblock(*bp, (int)sblock->fs_bsize);
491 dumpblock(*bp, (count - j) * TP_BSIZE);
493 spcl.c_type = TS_ADDR;
498 * Dump a map to the tape.
501 dumpmap(char *map, int type, ino_t ino)
507 spcl.c_count = howmany(mapsize * sizeof(char), TP_BSIZE);
509 for (i = 0, cp = map; i < spcl.c_count; i++, cp += TP_BSIZE)
514 * Write a header record to the dump tape.
517 writeheader(ino_t ino)
519 int32_t sum, cnt, *lp;
521 spcl.c_inumber = ino;
522 spcl.c_magic = NFS_MAGIC;
524 lp = (int32_t *)&spcl;
526 cnt = sizeof(union u_spcl) / (4 * sizeof(int32_t));
533 spcl.c_checksum = CHECKSUM - sum;
534 writerec((char *)&spcl, 1);
540 static daddr_t minino, maxino;
541 static struct dinode inoblock[MAXINOPB];
544 if (inum >= minino && inum < maxino)
545 return (&inoblock[inum - minino]);
546 bread(fsbtodb(sblock, ino_to_fsba(sblock, inum)), (char *)inoblock,
547 (int)sblock->fs_bsize);
548 minino = inum - (inum % INOPB(sblock));
549 maxino = minino + INOPB(sblock);
550 return (&inoblock[inum - minino]);
554 * Read a chunk of data from the disk.
555 * Try to recover from hard errors by reading in sector sized pieces.
556 * Error recovery is attempted at most BREADEMAX times before seeking
557 * consent from the operator to continue.
563 bread(daddr_t blkno, char *buf, int size)
568 cnt = cread(diskfd, buf, size, ((off_t)blkno << dev_bshift));
571 if (blkno + (size / dev_bsize) > fsbtodb(sblock, sblock->fs_size)) {
573 * Trying to read the final fragment.
575 * NB - dump only works in TP_BSIZE blocks, hence
576 * rounds `dev_bsize' fragments up to TP_BSIZE pieces.
577 * It should be smarter about not actually trying to
578 * read more than it can get, but for the time being
579 * we punt and scale back the read only when it gets
580 * us into trouble. (mkm 9/25/83)
586 msg("read error from %s: %s: [block %d]: count=%d\n",
587 disk, strerror(errno), blkno, size);
589 msg("short read error from %s: [block %d]: count=%d, got=%d\n",
590 disk, blkno, size, cnt);
591 if (++breaderrors > BREADEMAX) {
592 msg("More than %d block read errors from %s\n",
594 broadcast("DUMP IS AILING!\n");
595 msg("This is an unrecoverable error.\n");
596 if (!query("Do you want to attempt to continue?")){
603 * Zero buffer, then try to read each sector of buffer separately,
604 * and bypass the cache.
606 memset(buf, 0, size);
607 for (i = 0; i < size; i += dev_bsize, buf += dev_bsize, blkno++) {
608 if ((cnt = pread(diskfd, buf, (int)dev_bsize,
609 ((off_t)blkno << dev_bshift))) == dev_bsize)
612 msg("read error from %s: %s: [sector %d]: count=%ld\n",
613 disk, strerror(errno), blkno, dev_bsize);
616 msg("short read error from %s: [sector %d]: count=%ld, got=%d\n",
617 disk, blkno, dev_bsize, cnt);