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>
36 #include <sys/vnode.h>
39 #include <ufs/fsdir.h>
40 #include <ufs/inode.h>
42 #include <vfs/ufs/dir.h>
43 #include <vfs/ufs/dinode.h>
44 #include <vfs/ufs/fs.h>
47 #include <protocols/dumprestore.h>
57 #define HASDUMPEDFILE 0x1
58 #define HASSUBDIRS 0x2
61 typedef quad_t fsizeT;
66 static int dirindir(ufs1_ino_t, daddr_t, int, long *, long *, int);
67 static void dmpindir(ufs1_ino_t, daddr_t, int, fsizeT *);
68 static int searchdir(ufs1_ino_t, daddr_t, long, long, long *, int);
71 * This is an estimation of the number of TP_BSIZE blocks in the file.
72 * It estimates the number of blocks in files with holes by assuming
73 * that all of the blocks accounted for by di_blocks are data blocks
74 * (when some of the blocks are usually used for indirect pointers);
75 * hence the estimate may be high.
78 blockest(struct ufs1_dinode *dp)
83 * dp->di_size is the size of the file in bytes.
84 * dp->di_blocks stores the number of sectors actually in the file.
85 * If there are more sectors than the size would indicate, this just
86 * means that there are indirect blocks in the file or unused
87 * sectors in the last file block; we can safely ignore these
88 * (blkest = sizeest below).
89 * If the file is bigger than the number of sectors would indicate,
90 * then the file has holes in it. In this case we must use the
91 * block count to estimate the number of data blocks used, but
92 * we use the actual size for estimating the number of indirect
93 * dump blocks (sizeest vs. blkest in the indirect block
96 blkest = howmany(dbtob(dp->di_blocks), TP_BSIZE);
97 sizeest = howmany(dp->di_size, TP_BSIZE);
100 if (dp->di_size > (unsigned)sblock->fs_bsize * NDADDR) {
101 /* calculate the number of indirect blocks on the dump tape */
103 howmany(sizeest - NDADDR * sblock->fs_bsize / TP_BSIZE,
109 /* Auxiliary macro to pick up files changed since previous dump. */
110 #define CHANGEDSINCE(dp, t) \
111 ((dp)->di_mtime >= (t) || (dp)->di_ctime >= (t))
113 /* The WANTTODUMP macro decides whether a file should be dumped. */
115 #define WANTTODUMP(dp) \
116 (CHANGEDSINCE(dp, spcl.c_ddate) && \
117 (nonodump || ((dp)->di_flags & UF_NODUMP) != UF_NODUMP))
119 #define WANTTODUMP(dp) CHANGEDSINCE(dp, spcl.c_ddate)
125 * Walk the inode list for a filesystem to find all allocated inodes
126 * that have been modified since the previous dump time. Also, find all
127 * the directories in the filesystem.
130 mapfiles(ufs1_ino_t maxino, long *tape_size)
134 struct ufs1_dinode *dp;
135 int anydirskipped = 0;
137 for (ino = ROOTINO; ino < maxino; ino++) {
139 if ((mode = (dp->di_mode & IFMT)) == 0)
142 * Everything must go in usedinomap so that a check
143 * for "in dumpdirmap but not in usedinomap" to detect
144 * dirs with nodump set has a chance of succeeding
145 * (this is used in mapdirs()).
147 SETINO(ino, usedinomap);
149 SETINO(ino, dumpdirmap);
150 if (WANTTODUMP(dp)) {
151 SETINO(ino, dumpinomap);
152 if (mode != IFREG && mode != IFDIR && mode != IFLNK)
155 *tape_size += blockest(dp);
159 if (!nonodump && (dp->di_flags & UF_NODUMP))
160 CLRINO(ino, usedinomap);
165 * Restore gets very upset if the root is not dumped,
166 * so ensure that it always is dumped.
168 SETINO(ROOTINO, dumpinomap);
169 return (anydirskipped);
175 * Scan each directory on the filesystem to see if it has any modified
176 * files in it. If it does, and has not already been added to the dump
177 * list (because it was itself modified), then add it. If a directory
178 * has not been modified itself, contains no modified files and has no
179 * subdirectories, then it can be deleted from the dump list and from
180 * the list of directories. By deleting it from the list of directories,
181 * its parent may now qualify for the same treatment on this or a later
182 * pass using this algorithm.
185 mapdirs(ufs1_ino_t maxino, long *tape_size)
187 struct ufs1_dinode *dp;
192 struct ufs1_dinode di;
196 isdir = 0; /* XXX just to get gcc to shut up */
197 for (map = dumpdirmap, ino = 1; ino < maxino; ino++) {
198 if (((ino - 1) % NBBY) == 0) /* map is offset by 1 */
203 * If a directory has been removed from usedinomap, it
204 * either has the nodump flag set, or has inherited
205 * it. Although a directory can't be in dumpinomap if
206 * it isn't in usedinomap, we have to go through it to
207 * propagate the nodump flag.
209 nodump = !nonodump && (TSTINO(ino, usedinomap) == 0);
210 if ((isdir & 1) == 0 || (TSTINO(ino, dumpinomap) && !nodump))
213 di = *dp; /* inode buf may change in searchdir(). */
214 filesize = di.di_size;
215 for (ret = 0, i = 0; filesize > 0 && i < NDADDR; i++) {
216 if (di.di_db[i] != 0) {
217 ret |= searchdir(ino, di.di_db[i],
218 (long)dblksize(sblock, &di, i),
219 filesize, tape_size, nodump);
221 if (ret & HASDUMPEDFILE)
224 filesize -= sblock->fs_bsize;
226 for (i = 0; filesize > 0 && i < NIADDR; i++) {
227 if (di.di_ib[i] == 0)
229 ret |= dirindir(ino, di.di_ib[i], i, &filesize,
232 if (ret & HASDUMPEDFILE) {
233 SETINO(ino, dumpinomap);
234 *tape_size += blockest(&di);
239 if (ret & HASSUBDIRS)
240 change = 1; /* subdirs inherit nodump */
241 CLRINO(ino, dumpdirmap);
242 } else if ((ret & HASSUBDIRS) == 0)
243 if (!TSTINO(ino, dumpinomap)) {
244 CLRINO(ino, dumpdirmap);
252 * Read indirect blocks, and pass the data blocks to be searched
253 * as directories. Quit as soon as any entry is found that will
254 * require the directory to be dumped.
257 dirindir(ufs1_ino_t ino, daddr_t blkno, int ind_level, long *filesize,
258 long *tape_size, int nodump)
262 daddr_t idblk[MAXNINDIR];
264 bread(fsbtodb(sblock, blkno), (char *)idblk, (int)sblock->fs_bsize);
265 if (ind_level <= 0) {
266 for (i = 0; *filesize > 0 && i < NINDIR(sblock); i++) {
269 ret |= searchdir(ino, blkno, sblock->fs_bsize,
270 *filesize, tape_size, nodump);
272 if (ret & HASDUMPEDFILE)
275 *filesize -= sblock->fs_bsize;
280 for (i = 0; *filesize > 0 && i < NINDIR(sblock); i++) {
283 ret |= dirindir(ino, blkno, ind_level, filesize,
291 * Scan a disk block containing directory information looking to see if
292 * any of the entries are on the dump list and to see if the directory
293 * contains any subdirectories.
296 searchdir(ufs1_ino_t ino, daddr_t blkno, long size, long filesize,
297 long *tape_size, int nodump)
300 struct ufs1_dinode *ip;
304 bread(fsbtodb(sblock, blkno), dblk, (int)size);
307 for (loc = 0; loc < size; ) {
308 dp = (struct direct *)(dblk + loc);
309 if (dp->d_reclen == 0) {
310 msg("corrupted directory, inumber %d\n", ino);
316 if (dp->d_name[0] == '.') {
317 if (dp->d_name[1] == '\0')
319 if (dp->d_name[1] == '.' && dp->d_name[2] == '\0')
323 ip = getino(dp->d_ino);
324 if (TSTINO(dp->d_ino, dumpinomap)) {
325 CLRINO(dp->d_ino, dumpinomap);
326 *tape_size -= blockest(ip);
329 * Add back to dumpdirmap and remove from usedinomap
330 * to propagate nodump.
332 if ((ip->di_mode & IFMT) == IFDIR) {
333 SETINO(dp->d_ino, dumpdirmap);
334 CLRINO(dp->d_ino, usedinomap);
338 if (TSTINO(dp->d_ino, dumpinomap)) {
339 ret |= HASDUMPEDFILE;
340 if (ret & HASSUBDIRS)
343 if (TSTINO(dp->d_ino, dumpdirmap)) {
345 if (ret & HASDUMPEDFILE)
354 * Dump passes 3 and 4.
356 * Dump the contents of an inode to tape.
359 dumpino(struct ufs1_dinode *dp, ufs1_ino_t ino)
367 dumpmap(dumpinomap, TS_BITS, ino);
369 CLRINO(ino, dumpinomap);
371 spcl.c_type = TS_INODE;
373 switch (dp->di_mode & S_IFMT) {
383 * Check for short symbolic link.
386 if (dp->di_size > 0 &&
387 dp->di_size < (unsigned)sblock->fs_maxsymlinklen) {
391 memmove(buf, dp->di_shortlink, (u_long)dp->di_size);
392 buf[dp->di_size] = '\0';
413 msg("Warning: undefined file type 0%o\n", dp->di_mode & IFMT);
416 if (dp->di_size > NDADDR * (unsigned)sblock->fs_bsize)
417 cnt = NDADDR * sblock->fs_frag;
419 cnt = howmany(dp->di_size, sblock->fs_fsize);
420 blksout(&dp->di_db[0], cnt, ino);
421 if ((size = dp->di_size - NDADDR * sblock->fs_bsize) <= 0)
423 for (ind_level = 0; ind_level < NIADDR; ind_level++) {
424 dmpindir(ino, dp->di_ib[ind_level], ind_level, &size);
431 * Read indirect blocks, and pass the data blocks to be dumped.
434 dmpindir(ufs1_ino_t ino, daddr_t blk, int ind_level, fsizeT *size)
437 daddr_t idblk[MAXNINDIR];
440 bread(fsbtodb(sblock, blk), (char *)idblk, (int) sblock->fs_bsize);
442 memset(idblk, 0, (int)sblock->fs_bsize);
443 if (ind_level <= 0) {
444 if (*size < NINDIR(sblock) * sblock->fs_bsize)
445 cnt = howmany(*size, sblock->fs_fsize);
447 cnt = NINDIR(sblock) * sblock->fs_frag;
448 *size -= NINDIR(sblock) * sblock->fs_bsize;
449 blksout(&idblk[0], cnt, ino);
453 for (i = 0; i < NINDIR(sblock); i++) {
454 dmpindir(ino, idblk[i], ind_level, size);
461 * Collect up the data into tape record sized buffers and output them.
464 blksout(daddr_t *blkp, int frags, ufs1_ino_t ino)
467 int i, j, count, blks, tbperdb;
469 blks = howmany(frags * sblock->fs_fsize, TP_BSIZE);
470 tbperdb = sblock->fs_bsize >> tp_bshift;
471 for (i = 0; i < blks; i += TP_NINDIR) {
472 if (i + TP_NINDIR > blks)
475 count = i + TP_NINDIR;
476 for (j = i; j < count; j++)
477 if (blkp[j / tbperdb] != 0)
478 spcl.c_addr[j - i] = 1;
480 spcl.c_addr[j - i] = 0;
481 spcl.c_count = count - i;
483 bp = &blkp[i / tbperdb];
484 for (j = i; j < count; j += tbperdb, bp++)
486 if (j + tbperdb <= count)
487 dumpblock(*bp, (int)sblock->fs_bsize);
489 dumpblock(*bp, (count - j) * TP_BSIZE);
491 spcl.c_type = TS_ADDR;
496 * Dump a map to the tape.
499 dumpmap(const char *map, int type, ufs1_ino_t ino)
505 spcl.c_count = howmany(mapsize * sizeof(char), TP_BSIZE);
507 for (i = 0, cp = map; i < spcl.c_count; i++, cp += TP_BSIZE)
512 * Write a header record to the dump tape.
515 writeheader(ufs1_ino_t ino)
517 int32_t sum, cnt, *lp;
519 spcl.c_inumber = ino;
520 spcl.c_magic = NFS_MAGIC;
522 lp = (int32_t *)&spcl;
524 cnt = sizeof(union u_spcl) / (4 * sizeof(int32_t));
531 spcl.c_checksum = CHECKSUM - sum;
536 getino(ufs1_ino_t inum)
538 static daddr_t minino, maxino;
539 static struct ufs1_dinode inoblock[MAXINOPB];
542 if (inum >= (unsigned)minino && inum < (unsigned)maxino)
543 return (&inoblock[inum - minino]);
544 bread(fsbtodb(sblock, ino_to_fsba(sblock, inum)), (char *)inoblock,
545 (int)sblock->fs_bsize);
546 minino = inum - (inum % INOPB(sblock));
547 maxino = minino + INOPB(sblock);
548 return (&inoblock[inum - minino]);
552 * Read a chunk of data from the disk.
553 * Try to recover from hard errors by reading in sector sized pieces.
554 * Error recovery is attempted at most BREADEMAX times before seeking
555 * consent from the operator to continue.
561 bread(daddr_t blkno, char *buf, int size)
566 cnt = cread(diskfd, buf, size, ((off_t)blkno << dev_bshift));
569 if (blkno + (size / dev_bsize) > fsbtodb(sblock, sblock->fs_size)) {
571 * Trying to read the final fragment.
573 * NB - dump only works in TP_BSIZE blocks, hence
574 * rounds `dev_bsize' fragments up to TP_BSIZE pieces.
575 * It should be smarter about not actually trying to
576 * read more than it can get, but for the time being
577 * we punt and scale back the read only when it gets
578 * us into trouble. (mkm 9/25/83)
584 msg("read error from %s: %s: [block %d]: count=%d\n",
585 disk, strerror(errno), blkno, size);
587 msg("short read error from %s: [block %d]: count=%d, got=%d\n",
588 disk, blkno, size, cnt);
589 if (++breaderrors > BREADEMAX) {
590 msg("More than %d block read errors from %s\n",
592 broadcast("DUMP IS AILING!\n");
593 msg("This is an unrecoverable error.\n");
594 if (!query("Do you want to attempt to continue?")){
601 * Zero buffer, then try to read each sector of buffer separately,
602 * and bypass the cache.
604 memset(buf, 0, size);
605 for (i = 0; i < size; i += dev_bsize, buf += dev_bsize, blkno++) {
606 if ((cnt = pread(diskfd, buf, (int)dev_bsize,
607 ((off_t)blkno << dev_bshift))) == dev_bsize)
610 msg("read error from %s: %s: [sector %d]: count=%ld\n",
611 disk, strerror(errno), blkno, dev_bsize);
614 msg("short read error from %s: [sector %d]: count=%ld, got=%d\n",
615 disk, blkno, dev_bsize, cnt);