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.2 2003/06/17 04:27:32 dillon Exp $
38 #include <sys/param.h>
41 #include <sys/vnode.h>
44 #include <ufs/fsdir.h>
45 #include <ufs/inode.h>
47 #include <ufs/ufs/dir.h>
48 #include <ufs/ufs/dinode.h>
49 #include <ufs/ffs/fs.h>
52 #include <protocols/dumprestore.h>
64 #define HASDUMPEDFILE 0x1
65 #define HASSUBDIRS 0x2
68 typedef quad_t fsizeT;
73 static int dirindir __P((ino_t ino, daddr_t blkno, int level, long *size,
74 long *tapesize, int nodump));
75 static void dmpindir __P((ino_t ino, daddr_t blk, int level, fsizeT *size));
76 static int searchdir __P((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.
88 register struct dinode *dp;
93 * dp->di_size is the size of the file in bytes.
94 * dp->di_blocks stores the number of sectors actually in the file.
95 * If there are more sectors than the size would indicate, this just
96 * means that there are indirect blocks in the file or unused
97 * sectors in the last file block; we can safely ignore these
98 * (blkest = sizeest below).
99 * If the file is bigger than the number of sectors would indicate,
100 * then the file has holes in it. In this case we must use the
101 * block count to estimate the number of data blocks used, but
102 * we use the actual size for estimating the number of indirect
103 * dump blocks (sizeest vs. blkest in the indirect block
106 blkest = howmany(dbtob(dp->di_blocks), TP_BSIZE);
107 sizeest = howmany(dp->di_size, TP_BSIZE);
108 if (blkest > sizeest)
110 if (dp->di_size > sblock->fs_bsize * NDADDR) {
111 /* calculate the number of indirect blocks on the dump tape */
113 howmany(sizeest - NDADDR * sblock->fs_bsize / TP_BSIZE,
119 /* Auxiliary macro to pick up files changed since previous dump. */
120 #define CHANGEDSINCE(dp, t) \
121 ((dp)->di_mtime >= (t) || (dp)->di_ctime >= (t))
123 /* The WANTTODUMP macro decides whether a file should be dumped. */
125 #define WANTTODUMP(dp) \
126 (CHANGEDSINCE(dp, spcl.c_ddate) && \
127 (nonodump || ((dp)->di_flags & UF_NODUMP) != UF_NODUMP))
129 #define WANTTODUMP(dp) CHANGEDSINCE(dp, spcl.c_ddate)
135 * Walk the inode list for a filesystem to find all allocated inodes
136 * that have been modified since the previous dump time. Also, find all
137 * the directories in the filesystem.
140 mapfiles(maxino, tapesize)
146 register struct dinode *dp;
147 int anydirskipped = 0;
149 for (ino = ROOTINO; ino < maxino; ino++) {
151 if ((mode = (dp->di_mode & IFMT)) == 0)
154 * Everything must go in usedinomap so that a check
155 * for "in dumpdirmap but not in usedinomap" to detect
156 * dirs with nodump set has a chance of succeeding
157 * (this is used in mapdirs()).
159 SETINO(ino, usedinomap);
161 SETINO(ino, dumpdirmap);
162 if (WANTTODUMP(dp)) {
163 SETINO(ino, dumpinomap);
164 if (mode != IFREG && mode != IFDIR && mode != IFLNK)
167 *tapesize += blockest(dp);
171 if (!nonodump && (dp->di_flags & UF_NODUMP))
172 CLRINO(ino, usedinomap);
177 * Restore gets very upset if the root is not dumped,
178 * so ensure that it always is dumped.
180 SETINO(ROOTINO, dumpinomap);
181 return (anydirskipped);
187 * Scan each directory on the filesystem to see if it has any modified
188 * files in it. If it does, and has not already been added to the dump
189 * list (because it was itself modified), then add it. If a directory
190 * has not been modified itself, contains no modified files and has no
191 * subdirectories, then it can be deleted from the dump list and from
192 * the list of directories. By deleting it from the list of directories,
193 * its parent may now qualify for the same treatment on this or a later
194 * pass using this algorithm.
197 mapdirs(maxino, tapesize)
201 register struct dinode *dp;
202 register int i, isdir, nodump;
209 isdir = 0; /* XXX just to get gcc to shut up */
210 for (map = dumpdirmap, ino = 1; ino < maxino; ino++) {
211 if (((ino - 1) % NBBY) == 0) /* map is offset by 1 */
216 * If a directory has been removed from usedinomap, it
217 * either has the nodump flag set, or has inherited
218 * it. Although a directory can't be in dumpinomap if
219 * it isn't in usedinomap, we have to go through it to
220 * propagate the nodump flag.
222 nodump = !nonodump && (TSTINO(ino, usedinomap) == 0);
223 if ((isdir & 1) == 0 || (TSTINO(ino, dumpinomap) && !nodump))
226 di = *dp; /* inode buf may change in searchdir(). */
227 filesize = di.di_size;
228 for (ret = 0, i = 0; filesize > 0 && i < NDADDR; i++) {
229 if (di.di_db[i] != 0)
230 ret |= searchdir(ino, di.di_db[i],
231 (long)dblksize(sblock, dp, i),
232 filesize, tapesize, nodump);
233 if (ret & HASDUMPEDFILE)
236 filesize -= sblock->fs_bsize;
238 for (i = 0; filesize > 0 && i < NIADDR; i++) {
239 if (di.di_ib[i] == 0)
241 ret |= dirindir(ino, di.di_ib[i], i, &filesize,
244 if (ret & HASDUMPEDFILE) {
245 SETINO(ino, dumpinomap);
246 *tapesize += blockest(dp);
251 if (ret & HASSUBDIRS)
252 change = 1; /* subdirs inherit nodump */
253 CLRINO(ino, dumpdirmap);
254 } else if ((ret & HASSUBDIRS) == 0)
255 if (!TSTINO(ino, dumpinomap)) {
256 CLRINO(ino, dumpdirmap);
264 * Read indirect blocks, and pass the data blocks to be searched
265 * as directories. Quit as soon as any entry is found that will
266 * require the directory to be dumped.
269 dirindir(ino, blkno, ind_level, filesize, tapesize, nodump)
279 daddr_t idblk[MAXNINDIR];
281 bread(fsbtodb(sblock, blkno), (char *)idblk, (int)sblock->fs_bsize);
282 if (ind_level <= 0) {
283 for (i = 0; *filesize > 0 && i < NINDIR(sblock); i++) {
286 ret |= searchdir(ino, blkno, sblock->fs_bsize,
287 *filesize, tapesize, nodump);
288 if (ret & HASDUMPEDFILE)
291 *filesize -= sblock->fs_bsize;
296 for (i = 0; *filesize > 0 && i < NINDIR(sblock); i++) {
299 ret |= dirindir(ino, blkno, ind_level, filesize,
306 * Scan a disk block containing directory information looking to see if
307 * any of the entries are on the dump list and to see if the directory
308 * contains any subdirectories.
311 searchdir(ino, blkno, size, filesize, tapesize, nodump)
319 register struct direct *dp;
320 register struct dinode *ip;
321 register long loc, ret = 0;
324 bread(fsbtodb(sblock, blkno), dblk, (int)size);
327 for (loc = 0; loc < size; ) {
328 dp = (struct direct *)(dblk + loc);
329 if (dp->d_reclen == 0) {
330 msg("corrupted directory, inumber %d\n", ino);
336 if (dp->d_name[0] == '.') {
337 if (dp->d_name[1] == '\0')
339 if (dp->d_name[1] == '.' && dp->d_name[2] == '\0')
343 ip = getino(dp->d_ino);
344 if (TSTINO(dp->d_ino, dumpinomap)) {
345 CLRINO(dp->d_ino, dumpinomap);
346 CLRINO(dp->d_ino, usedinomap);
347 *tapesize -= blockest(ip);
349 /* Add back to dumpdirmap to propagate nodump. */
350 if ((ip->di_mode & IFMT) == IFDIR) {
351 SETINO(dp->d_ino, dumpdirmap);
355 if (TSTINO(dp->d_ino, dumpinomap)) {
356 ret |= HASDUMPEDFILE;
357 if (ret & HASSUBDIRS)
360 if (TSTINO(dp->d_ino, dumpdirmap)) {
362 if (ret & HASDUMPEDFILE)
371 * Dump passes 3 and 4.
373 * Dump the contents of an inode to tape.
377 register struct dinode *dp;
386 dumpmap(dumpinomap, TS_BITS, ino);
388 CLRINO(ino, dumpinomap);
390 spcl.c_type = TS_INODE;
392 switch (dp->di_mode & S_IFMT) {
402 * Check for short symbolic link.
405 if (dp->di_size > 0 &&
406 dp->di_size < sblock->fs_maxsymlinklen) {
410 memmove(buf, dp->di_shortlink, (u_long)dp->di_size);
411 buf[dp->di_size] = '\0';
432 msg("Warning: undefined file type 0%o\n", dp->di_mode & IFMT);
435 if (dp->di_size > NDADDR * sblock->fs_bsize)
436 cnt = NDADDR * sblock->fs_frag;
438 cnt = howmany(dp->di_size, sblock->fs_fsize);
439 blksout(&dp->di_db[0], cnt, ino);
440 if ((size = dp->di_size - NDADDR * sblock->fs_bsize) <= 0)
442 for (ind_level = 0; ind_level < NIADDR; ind_level++) {
443 dmpindir(ino, dp->di_ib[ind_level], ind_level, &size);
450 * Read indirect blocks, and pass the data blocks to be dumped.
453 dmpindir(ino, blk, ind_level, size)
460 daddr_t idblk[MAXNINDIR];
463 bread(fsbtodb(sblock, blk), (char *)idblk, (int) sblock->fs_bsize);
465 memset(idblk, 0, (int)sblock->fs_bsize);
466 if (ind_level <= 0) {
467 if (*size < NINDIR(sblock) * sblock->fs_bsize)
468 cnt = howmany(*size, sblock->fs_fsize);
470 cnt = NINDIR(sblock) * sblock->fs_frag;
471 *size -= NINDIR(sblock) * sblock->fs_bsize;
472 blksout(&idblk[0], cnt, ino);
476 for (i = 0; i < NINDIR(sblock); i++) {
477 dmpindir(ino, idblk[i], ind_level, size);
484 * Collect up the data into tape record sized buffers and output them.
487 blksout(blkp, frags, ino)
492 register daddr_t *bp;
493 int i, j, count, blks, tbperdb;
495 blks = howmany(frags * sblock->fs_fsize, TP_BSIZE);
496 tbperdb = sblock->fs_bsize >> tp_bshift;
497 for (i = 0; i < blks; i += TP_NINDIR) {
498 if (i + TP_NINDIR > blks)
501 count = i + TP_NINDIR;
502 for (j = i; j < count; j++)
503 if (blkp[j / tbperdb] != 0)
504 spcl.c_addr[j - i] = 1;
506 spcl.c_addr[j - i] = 0;
507 spcl.c_count = count - i;
509 bp = &blkp[i / tbperdb];
510 for (j = i; j < count; j += tbperdb, bp++)
512 if (j + tbperdb <= count)
513 dumpblock(*bp, (int)sblock->fs_bsize);
515 dumpblock(*bp, (count - j) * TP_BSIZE);
517 spcl.c_type = TS_ADDR;
522 * Dump a map to the tape.
525 dumpmap(map, type, ino)
534 spcl.c_count = howmany(mapsize * sizeof(char), TP_BSIZE);
536 for (i = 0, cp = map; i < spcl.c_count; i++, cp += TP_BSIZE)
541 * Write a header record to the dump tape.
547 register int32_t sum, cnt, *lp;
549 spcl.c_inumber = ino;
550 spcl.c_magic = NFS_MAGIC;
552 lp = (int32_t *)&spcl;
554 cnt = sizeof(union u_spcl) / (4 * sizeof(int32_t));
561 spcl.c_checksum = CHECKSUM - sum;
562 writerec((char *)&spcl, 1);
569 static daddr_t minino, maxino;
570 static struct dinode inoblock[MAXINOPB];
573 if (inum >= minino && inum < maxino)
574 return (&inoblock[inum - minino]);
575 bread(fsbtodb(sblock, ino_to_fsba(sblock, inum)), (char *)inoblock,
576 (int)sblock->fs_bsize);
577 minino = inum - (inum % INOPB(sblock));
578 maxino = minino + INOPB(sblock);
579 return (&inoblock[inum - minino]);
583 * Read a chunk of data from the disk.
584 * Try to recover from hard errors by reading in sector sized pieces.
585 * Error recovery is attempted at most BREADEMAX times before seeking
586 * consent from the operator to continue.
592 bread(blkno, buf, size)
600 cnt = cread(diskfd, buf, size, ((off_t)blkno << dev_bshift));
603 if (blkno + (size / dev_bsize) > fsbtodb(sblock, sblock->fs_size)) {
605 * Trying to read the final fragment.
607 * NB - dump only works in TP_BSIZE blocks, hence
608 * rounds `dev_bsize' fragments up to TP_BSIZE pieces.
609 * It should be smarter about not actually trying to
610 * read more than it can get, but for the time being
611 * we punt and scale back the read only when it gets
612 * us into trouble. (mkm 9/25/83)
618 msg("read error from %s: %s: [block %d]: count=%d\n",
619 disk, strerror(errno), blkno, size);
621 msg("short read error from %s: [block %d]: count=%d, got=%d\n",
622 disk, blkno, size, cnt);
623 if (++breaderrors > BREADEMAX) {
624 msg("More than %d block read errors from %s\n",
626 broadcast("DUMP IS AILING!\n");
627 msg("This is an unrecoverable error.\n");
628 if (!query("Do you want to attempt to continue?")){
635 * Zero buffer, then try to read each sector of buffer separately,
636 * and bypass the cache.
638 memset(buf, 0, size);
639 for (i = 0; i < size; i += dev_bsize, buf += dev_bsize, blkno++) {
640 if ((cnt = pread(diskfd, buf, (int)dev_bsize,
641 ((off_t)blkno << dev_bshift))) == dev_bsize)
644 msg("read error from %s: %s: [sector %d]: count=%d\n",
645 disk, strerror(errno), blkno, dev_bsize);
648 msg("short read error from %s: [sector %d]: count=%d, got=%d\n",
649 disk, blkno, dev_bsize, cnt);