5e87a0744fc873a6ed6bd7dca1a8a6863b03e6cb
[dragonfly.git] / sbin / dump / traverse.c
1 /*-
2  * Copyright (c) 1980, 1988, 1991, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
20  *
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
31  * SUCH DAMAGE.
32  *
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.3 2003/08/08 04:18:37 dillon Exp $
36  */
37
38 #include <sys/param.h>
39 #include <sys/stat.h>
40 #ifdef sunos
41 #include <sys/vnode.h>
42
43 #include <ufs/fs.h>
44 #include <ufs/fsdir.h>
45 #include <ufs/inode.h>
46 #else
47 #include <vfs/ufs/dir.h>
48 #include <vfs/ufs/dinode.h>
49 #include <vfs/ufs/fs.h>
50 #endif
51
52 #include <protocols/dumprestore.h>
53
54 #include <ctype.h>
55 #include <stdio.h>
56 #ifdef __STDC__
57 #include <errno.h>
58 #include <string.h>
59 #include <unistd.h>
60 #endif
61
62 #include "dump.h"
63
64 #define HASDUMPEDFILE   0x1
65 #define HASSUBDIRS      0x2
66
67 #ifdef  FS_44INODEFMT
68 typedef quad_t fsizeT;
69 #else
70 typedef long fsizeT;
71 #endif
72
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));
78
79 /*
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.
85  */
86 long
87 blockest(dp)
88         register struct dinode *dp;
89 {
90         long blkest, sizeest;
91
92         /*
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
104          *      calculation).
105          */
106         blkest = howmany(dbtob(dp->di_blocks), TP_BSIZE);
107         sizeest = howmany(dp->di_size, TP_BSIZE);
108         if (blkest > sizeest)
109                 blkest = sizeest;
110         if (dp->di_size > sblock->fs_bsize * NDADDR) {
111                 /* calculate the number of indirect blocks on the dump tape */
112                 blkest +=
113                         howmany(sizeest - NDADDR * sblock->fs_bsize / TP_BSIZE,
114                         TP_NINDIR);
115         }
116         return (blkest + 1);
117 }
118
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))
122
123 /* The WANTTODUMP macro decides whether a file should be dumped. */
124 #ifdef UF_NODUMP
125 #define WANTTODUMP(dp) \
126         (CHANGEDSINCE(dp, spcl.c_ddate) && \
127          (nonodump || ((dp)->di_flags & UF_NODUMP) != UF_NODUMP))
128 #else
129 #define WANTTODUMP(dp) CHANGEDSINCE(dp, spcl.c_ddate)
130 #endif
131
132 /*
133  * Dump pass 1.
134  *
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.
138  */
139 int
140 mapfiles(maxino, tapesize)
141         ino_t maxino;
142         long *tapesize;
143 {
144         register int mode;
145         register ino_t ino;
146         register struct dinode *dp;
147         int anydirskipped = 0;
148
149         for (ino = ROOTINO; ino < maxino; ino++) {
150                 dp = getino(ino);
151                 if ((mode = (dp->di_mode & IFMT)) == 0)
152                         continue;
153                 /*
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()).
158                  */
159                 SETINO(ino, usedinomap);
160                 if (mode == IFDIR)
161                         SETINO(ino, dumpdirmap);
162                 if (WANTTODUMP(dp)) {
163                         SETINO(ino, dumpinomap);
164                         if (mode != IFREG && mode != IFDIR && mode != IFLNK)
165                                 *tapesize += 1;
166                         else
167                                 *tapesize += blockest(dp);
168                         continue;
169                 }
170                 if (mode == IFDIR) {
171                         if (!nonodump && (dp->di_flags & UF_NODUMP))
172                                 CLRINO(ino, usedinomap);
173                         anydirskipped = 1;
174                 }
175         }
176         /*
177          * Restore gets very upset if the root is not dumped,
178          * so ensure that it always is dumped.
179          */
180         SETINO(ROOTINO, dumpinomap);
181         return (anydirskipped);
182 }
183
184 /*
185  * Dump pass 2.
186  *
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.
195  */
196 int
197 mapdirs(maxino, tapesize)
198         ino_t maxino;
199         long *tapesize;
200 {
201         register struct dinode *dp;
202         register int i, isdir, nodump;
203         register char *map;
204         register ino_t ino;
205         struct dinode di;
206         long filesize;
207         int ret, change = 0;
208
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 */
212                         isdir = *map++;
213                 else
214                         isdir >>= 1;
215                 /*
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.
221                  */
222                 nodump = !nonodump && (TSTINO(ino, usedinomap) == 0);
223                 if ((isdir & 1) == 0 || (TSTINO(ino, dumpinomap) && !nodump))
224                         continue;
225                 dp = getino(ino);
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)
234                                 filesize = 0;
235                         else
236                                 filesize -= sblock->fs_bsize;
237                 }
238                 for (i = 0; filesize > 0 && i < NIADDR; i++) {
239                         if (di.di_ib[i] == 0)
240                                 continue;
241                         ret |= dirindir(ino, di.di_ib[i], i, &filesize,
242                             tapesize, nodump);
243                 }
244                 if (ret & HASDUMPEDFILE) {
245                         SETINO(ino, dumpinomap);
246                         *tapesize += blockest(dp);
247                         change = 1;
248                         continue;
249                 }
250                 if (nodump) {
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);
257                                 change = 1;
258                         }
259         }
260         return (change);
261 }
262
263 /*
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.
267  */
268 static int
269 dirindir(ino, blkno, ind_level, filesize, tapesize, nodump)
270         ino_t ino;
271         daddr_t blkno;
272         int ind_level;
273         long *filesize;
274         long *tapesize;
275         int nodump;
276 {
277         int ret = 0;
278         register int i;
279         daddr_t idblk[MAXNINDIR];
280
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++) {
284                         blkno = idblk[i];
285                         if (blkno != 0)
286                                 ret |= searchdir(ino, blkno, sblock->fs_bsize,
287                                         *filesize, tapesize, nodump);
288                         if (ret & HASDUMPEDFILE)
289                                 *filesize = 0;
290                         else
291                                 *filesize -= sblock->fs_bsize;
292                 }
293                 return (ret);
294         }
295         ind_level--;
296         for (i = 0; *filesize > 0 && i < NINDIR(sblock); i++) {
297                 blkno = idblk[i];
298                 if (blkno != 0)
299                         ret |= dirindir(ino, blkno, ind_level, filesize,
300                             tapesize, nodump);
301         }
302         return (ret);
303 }
304
305 /*
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.
309  */
310 static int
311 searchdir(ino, blkno, size, filesize, tapesize, nodump)
312         ino_t ino;
313         daddr_t blkno;
314         register long size;
315         long filesize;
316         long *tapesize;
317         int nodump;
318 {
319         register struct direct *dp;
320         register struct dinode *ip;
321         register long loc, ret = 0;
322         char dblk[MAXBSIZE];
323
324         bread(fsbtodb(sblock, blkno), dblk, (int)size);
325         if (filesize < size)
326                 size = filesize;
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);
331                         break;
332                 }
333                 loc += dp->d_reclen;
334                 if (dp->d_ino == 0)
335                         continue;
336                 if (dp->d_name[0] == '.') {
337                         if (dp->d_name[1] == '\0')
338                                 continue;
339                         if (dp->d_name[1] == '.' && dp->d_name[2] == '\0')
340                                 continue;
341                 }
342                 if (nodump) {
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);
348                         }
349                         /* Add back to dumpdirmap to propagate nodump. */
350                         if ((ip->di_mode & IFMT) == IFDIR) {
351                                 SETINO(dp->d_ino, dumpdirmap);
352                                 ret |= HASSUBDIRS;
353                         }
354                 } else {
355                         if (TSTINO(dp->d_ino, dumpinomap)) {
356                                 ret |= HASDUMPEDFILE;
357                                 if (ret & HASSUBDIRS)
358                                         break;
359                         }
360                         if (TSTINO(dp->d_ino, dumpdirmap)) {
361                                 ret |= HASSUBDIRS;
362                                 if (ret & HASDUMPEDFILE)
363                                         break;
364                         }
365                 }
366         }
367         return (ret);
368 }
369
370 /*
371  * Dump passes 3 and 4.
372  *
373  * Dump the contents of an inode to tape.
374  */
375 void
376 dumpino(dp, ino)
377         register struct dinode *dp;
378         ino_t ino;
379 {
380         int ind_level, cnt;
381         fsizeT size;
382         char buf[TP_BSIZE];
383
384         if (newtape) {
385                 newtape = 0;
386                 dumpmap(dumpinomap, TS_BITS, ino);
387         }
388         CLRINO(ino, dumpinomap);
389         spcl.c_dinode = *dp;
390         spcl.c_type = TS_INODE;
391         spcl.c_count = 0;
392         switch (dp->di_mode & S_IFMT) {
393
394         case 0:
395                 /*
396                  * Freed inode.
397                  */
398                 return;
399
400         case S_IFLNK:
401                 /*
402                  * Check for short symbolic link.
403                  */
404 #ifdef FS_44INODEFMT
405                 if (dp->di_size > 0 &&
406                     dp->di_size < sblock->fs_maxsymlinklen) {
407                         spcl.c_addr[0] = 1;
408                         spcl.c_count = 1;
409                         writeheader(ino);
410                         memmove(buf, dp->di_shortlink, (u_long)dp->di_size);
411                         buf[dp->di_size] = '\0';
412                         writerec(buf, 0);
413                         return;
414                 }
415 #endif
416                 /* fall through */
417
418         case S_IFDIR:
419         case S_IFREG:
420                 if (dp->di_size > 0)
421                         break;
422                 /* fall through */
423
424         case S_IFIFO:
425         case S_IFSOCK:
426         case S_IFCHR:
427         case S_IFBLK:
428                 writeheader(ino);
429                 return;
430
431         default:
432                 msg("Warning: undefined file type 0%o\n", dp->di_mode & IFMT);
433                 return;
434         }
435         if (dp->di_size > NDADDR * sblock->fs_bsize)
436                 cnt = NDADDR * sblock->fs_frag;
437         else
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)
441                 return;
442         for (ind_level = 0; ind_level < NIADDR; ind_level++) {
443                 dmpindir(ino, dp->di_ib[ind_level], ind_level, &size);
444                 if (size <= 0)
445                         return;
446         }
447 }
448
449 /*
450  * Read indirect blocks, and pass the data blocks to be dumped.
451  */
452 static void
453 dmpindir(ino, blk, ind_level, size)
454         ino_t ino;
455         daddr_t blk;
456         int ind_level;
457         fsizeT *size;
458 {
459         int i, cnt;
460         daddr_t idblk[MAXNINDIR];
461
462         if (blk != 0)
463                 bread(fsbtodb(sblock, blk), (char *)idblk, (int) sblock->fs_bsize);
464         else
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);
469                 else
470                         cnt = NINDIR(sblock) * sblock->fs_frag;
471                 *size -= NINDIR(sblock) * sblock->fs_bsize;
472                 blksout(&idblk[0], cnt, ino);
473                 return;
474         }
475         ind_level--;
476         for (i = 0; i < NINDIR(sblock); i++) {
477                 dmpindir(ino, idblk[i], ind_level, size);
478                 if (*size <= 0)
479                         return;
480         }
481 }
482
483 /*
484  * Collect up the data into tape record sized buffers and output them.
485  */
486 void
487 blksout(blkp, frags, ino)
488         daddr_t *blkp;
489         int frags;
490         ino_t ino;
491 {
492         register daddr_t *bp;
493         int i, j, count, blks, tbperdb;
494
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)
499                         count = blks;
500                 else
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;
505                         else
506                                 spcl.c_addr[j - i] = 0;
507                 spcl.c_count = count - i;
508                 writeheader(ino);
509                 bp = &blkp[i / tbperdb];
510                 for (j = i; j < count; j += tbperdb, bp++)
511                         if (*bp != 0) {
512                                 if (j + tbperdb <= count)
513                                         dumpblock(*bp, (int)sblock->fs_bsize);
514                                 else
515                                         dumpblock(*bp, (count - j) * TP_BSIZE);
516                         }
517                 spcl.c_type = TS_ADDR;
518         }
519 }
520
521 /*
522  * Dump a map to the tape.
523  */
524 void
525 dumpmap(map, type, ino)
526         char *map;
527         int type;
528         ino_t ino;
529 {
530         register int i;
531         char *cp;
532
533         spcl.c_type = type;
534         spcl.c_count = howmany(mapsize * sizeof(char), TP_BSIZE);
535         writeheader(ino);
536         for (i = 0, cp = map; i < spcl.c_count; i++, cp += TP_BSIZE)
537                 writerec(cp, 0);
538 }
539
540 /*
541  * Write a header record to the dump tape.
542  */
543 void
544 writeheader(ino)
545         ino_t ino;
546 {
547         register int32_t sum, cnt, *lp;
548
549         spcl.c_inumber = ino;
550         spcl.c_magic = NFS_MAGIC;
551         spcl.c_checksum = 0;
552         lp = (int32_t *)&spcl;
553         sum = 0;
554         cnt = sizeof(union u_spcl) / (4 * sizeof(int32_t));
555         while (--cnt >= 0) {
556                 sum += *lp++;
557                 sum += *lp++;
558                 sum += *lp++;
559                 sum += *lp++;
560         }
561         spcl.c_checksum = CHECKSUM - sum;
562         writerec((char *)&spcl, 1);
563 }
564
565 struct dinode *
566 getino(inum)
567         ino_t inum;
568 {
569         static daddr_t minino, maxino;
570         static struct dinode inoblock[MAXINOPB];
571
572         curino = inum;
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]);
580 }
581
582 /*
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.
587  */
588 int     breaderrors = 0;
589 #define BREADEMAX 32
590
591 void
592 bread(blkno, buf, size)
593         daddr_t blkno;
594         char *buf;
595         int size;
596 {
597         int cnt, i;
598
599 loop:
600         cnt = cread(diskfd, buf, size, ((off_t)blkno << dev_bshift));
601         if (cnt == size)
602                 return;
603         if (blkno + (size / dev_bsize) > fsbtodb(sblock, sblock->fs_size)) {
604                 /*
605                  * Trying to read the final fragment.
606                  *
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)
613                  */
614                 size -= dev_bsize;
615                 goto loop;
616         }
617         if (cnt == -1)
618                 msg("read error from %s: %s: [block %d]: count=%d\n",
619                         disk, strerror(errno), blkno, size);
620         else
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",
625                         BREADEMAX, disk);
626                 broadcast("DUMP IS AILING!\n");
627                 msg("This is an unrecoverable error.\n");
628                 if (!query("Do you want to attempt to continue?")){
629                         dumpabort(0);
630                         /*NOTREACHED*/
631                 } else
632                         breaderrors = 0;
633         }
634         /*
635          * Zero buffer, then try to read each sector of buffer separately,
636          * and bypass the cache.
637          */
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)
642                         continue;
643                 if (cnt == -1) {
644                         msg("read error from %s: %s: [sector %d]: count=%d\n",
645                                 disk, strerror(errno), blkno, dev_bsize);
646                         continue;
647                 }
648                 msg("short read error from %s: [sector %d]: count=%d, got=%d\n",
649                         disk, blkno, dev_bsize, cnt);
650         }
651 }