dump(8): Remove some old code.
[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. 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.
16  *
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
27  * SUCH DAMAGE.
28  *
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 $
31  */
32
33 #include <sys/param.h>
34 #include <sys/stat.h>
35 #include <vfs/ufs/dir.h>
36 #include <vfs/ufs/dinode.h>
37 #include <vfs/ufs/fs.h>
38
39 #include <protocols/dumprestore.h>
40
41 #include <ctype.h>
42 #include <stdio.h>
43 #include <errno.h>
44 #include <string.h>
45 #include <unistd.h>
46
47 #include "dump.h"
48
49 #define HASDUMPEDFILE   0x1
50 #define HASSUBDIRS      0x2
51
52 #ifdef  FS_44INODEFMT
53 typedef quad_t fsizeT;
54 #else
55 typedef long fsizeT;
56 #endif
57
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);
61
62 /*
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.
68  */
69 long
70 blockest(struct ufs1_dinode *dp)
71 {
72         long blkest, sizeest;
73
74         /*
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
86          *      calculation).
87          */
88         blkest = howmany(dbtob(dp->di_blocks), TP_BSIZE);
89         sizeest = howmany(dp->di_size, TP_BSIZE);
90         if (blkest > sizeest)
91                 blkest = sizeest;
92         if (dp->di_size > (unsigned)sblock->fs_bsize * NDADDR) {
93                 /* calculate the number of indirect blocks on the dump tape */
94                 blkest +=
95                         howmany(sizeest - NDADDR * sblock->fs_bsize / TP_BSIZE,
96                         TP_NINDIR);
97         }
98         return (blkest + 1);
99 }
100
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))
104
105 /* The WANTTODUMP macro decides whether a file should be dumped. */
106 #ifdef UF_NODUMP
107 #define WANTTODUMP(dp) \
108         (CHANGEDSINCE(dp, spcl.c_ddate) && \
109          (nonodump || ((dp)->di_flags & UF_NODUMP) != UF_NODUMP))
110 #else
111 #define WANTTODUMP(dp) CHANGEDSINCE(dp, spcl.c_ddate)
112 #endif
113
114 /*
115  * Dump pass 1.
116  *
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.
120  */
121 int
122 mapfiles(ufs1_ino_t maxino, long *tape_size)
123 {
124         int mode;
125         ufs1_ino_t ino;
126         struct ufs1_dinode *dp;
127         int anydirskipped = 0;
128
129         for (ino = ROOTINO; ino < maxino; ino++) {
130                 dp = getino(ino);
131                 if ((mode = (dp->di_mode & IFMT)) == 0)
132                         continue;
133                 /*
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()).
138                  */
139                 SETINO(ino, usedinomap);
140                 if (mode == IFDIR)
141                         SETINO(ino, dumpdirmap);
142                 if (WANTTODUMP(dp)) {
143                         SETINO(ino, dumpinomap);
144                         if (mode != IFREG && mode != IFDIR && mode != IFLNK)
145                                 *tape_size += 1;
146                         else
147                                 *tape_size += blockest(dp);
148                         continue;
149                 }
150                 if (mode == IFDIR) {
151                         if (!nonodump && (dp->di_flags & UF_NODUMP))
152                                 CLRINO(ino, usedinomap);
153                         anydirskipped = 1;
154                 }
155         }
156         /*
157          * Restore gets very upset if the root is not dumped,
158          * so ensure that it always is dumped.
159          */
160         SETINO(ROOTINO, dumpinomap);
161         return (anydirskipped);
162 }
163
164 /*
165  * Dump pass 2.
166  *
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.
175  */
176 int
177 mapdirs(ufs1_ino_t maxino, long *tape_size)
178 {
179         struct  ufs1_dinode *dp;
180         int isdir, nodump;
181         unsigned int i;
182         char *map;
183         ufs1_ino_t ino;
184         struct ufs1_dinode di;
185         long filesize;
186         int ret, change = 0;
187
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 */
191                         isdir = *map++;
192                 else
193                         isdir >>= 1;
194                 /*
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.
200                  */
201                 nodump = !nonodump && (TSTINO(ino, usedinomap) == 0);
202                 if ((isdir & 1) == 0 || (TSTINO(ino, dumpinomap) && !nodump))
203                         continue;
204                 dp = getino(ino);
205                 di = *dp;       /* inode buf may change in searchdir(). */
206                 filesize = di.di_size;
207                 for (ret = 0, i = 0; filesize > 0 && i < 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);
212                         }
213                         if (ret & HASDUMPEDFILE)
214                                 filesize = 0;
215                         else
216                                 filesize -= sblock->fs_bsize;
217                 }
218                 for (i = 0; filesize > 0 && i < NIADDR; i++) {
219                         if (di.di_ib[i] == 0)
220                                 continue;
221                         ret |= dirindir(ino, di.di_ib[i], i, &filesize,
222                             tape_size, nodump);
223                 }
224                 if (ret & HASDUMPEDFILE) {
225                         SETINO(ino, dumpinomap);
226                         *tape_size += blockest(&di);
227                         change = 1;
228                         continue;
229                 }
230                 if (nodump) {
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);
237                                 change = 1;
238                         }
239         }
240         return (change);
241 }
242
243 /*
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.
247  */
248 static int
249 dirindir(ufs1_ino_t ino, daddr_t blkno, int ind_level, long *filesize,
250          long *tape_size, int nodump)
251 {
252         int ret = 0;
253         int i;
254         daddr_t idblk[MAXNINDIR];
255
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++) {
259                         blkno = idblk[i];
260                         if (blkno != 0) {
261                                 ret |= searchdir(ino, blkno, sblock->fs_bsize,
262                                         *filesize, tape_size, nodump);
263                         }
264                         if (ret & HASDUMPEDFILE)
265                                 *filesize = 0;
266                         else
267                                 *filesize -= sblock->fs_bsize;
268                 }
269                 return (ret);
270         }
271         ind_level--;
272         for (i = 0; *filesize > 0 && i < NINDIR(sblock); i++) {
273                 blkno = idblk[i];
274                 if (blkno != 0) {
275                         ret |= dirindir(ino, blkno, ind_level, filesize,
276                             tape_size, nodump);
277                 }
278         }
279         return (ret);
280 }
281
282 /*
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.
286  */
287 static int
288 searchdir(ufs1_ino_t ino, daddr_t blkno, long size, long filesize,
289           long *tape_size, int nodump)
290 {
291         struct direct *dp;
292         struct ufs1_dinode *ip;
293         long loc, ret = 0;
294         char dblk[MAXBSIZE];
295
296         bread(fsbtodb(sblock, blkno), dblk, (int)size);
297         if (filesize < size)
298                 size = filesize;
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);
303                         break;
304                 }
305                 loc += dp->d_reclen;
306                 if (dp->d_ino == 0)
307                         continue;
308                 if (dp->d_name[0] == '.') {
309                         if (dp->d_name[1] == '\0')
310                                 continue;
311                         if (dp->d_name[1] == '.' && dp->d_name[2] == '\0')
312                                 continue;
313                 }
314                 if (nodump) {
315                         ip = getino(dp->d_ino);
316                         if (TSTINO(dp->d_ino, dumpinomap)) {
317                                 CLRINO(dp->d_ino, dumpinomap);
318                                 *tape_size -= blockest(ip);
319                         }
320                         /*
321                          * Add back to dumpdirmap and remove from usedinomap
322                          * to propagate nodump.
323                          */
324                         if ((ip->di_mode & IFMT) == IFDIR) {
325                                 SETINO(dp->d_ino, dumpdirmap);
326                                 CLRINO(dp->d_ino, usedinomap);
327                                 ret |= HASSUBDIRS;
328                         }
329                 } else {
330                         if (TSTINO(dp->d_ino, dumpinomap)) {
331                                 ret |= HASDUMPEDFILE;
332                                 if (ret & HASSUBDIRS)
333                                         break;
334                         }
335                         if (TSTINO(dp->d_ino, dumpdirmap)) {
336                                 ret |= HASSUBDIRS;
337                                 if (ret & HASDUMPEDFILE)
338                                         break;
339                         }
340                 }
341         }
342         return (ret);
343 }
344
345 /*
346  * Dump passes 3 and 4.
347  *
348  * Dump the contents of an inode to tape.
349  */
350 void
351 dumpino(struct ufs1_dinode *dp, ufs1_ino_t ino)
352 {
353         int ind_level, cnt;
354         fsizeT size;
355         char buf[TP_BSIZE];
356
357         if (newtape) {
358                 newtape = 0;
359                 dumpmap(dumpinomap, TS_BITS, ino);
360         }
361         CLRINO(ino, dumpinomap);
362         spcl.c_dinode = *dp;
363         spcl.c_type = TS_INODE;
364         spcl.c_count = 0;
365         switch (dp->di_mode & S_IFMT) {
366
367         case 0:
368                 /*
369                  * Freed inode.
370                  */
371                 return;
372
373         case S_IFLNK:
374                 /*
375                  * Check for short symbolic link.
376                  */
377 #ifdef FS_44INODEFMT
378                 if (dp->di_size > 0 &&
379                     dp->di_size < (unsigned)sblock->fs_maxsymlinklen) {
380                         spcl.c_addr[0] = 1;
381                         spcl.c_count = 1;
382                         writeheader(ino);
383                         memmove(buf, dp->di_shortlink, (u_long)dp->di_size);
384                         buf[dp->di_size] = '\0';
385                         writerec(buf, 0);
386                         return;
387                 }
388 #endif
389                 /* fall through */
390
391         case S_IFDIR:
392         case S_IFREG:
393                 if (dp->di_size > 0)
394                         break;
395                 /* fall through */
396
397         case S_IFIFO:
398         case S_IFSOCK:
399         case S_IFCHR:
400         case S_IFBLK:
401                 writeheader(ino);
402                 return;
403
404         default:
405                 msg("Warning: undefined file type 0%o\n", dp->di_mode & IFMT);
406                 return;
407         }
408         if (dp->di_size > NDADDR * (unsigned)sblock->fs_bsize)
409                 cnt = NDADDR * sblock->fs_frag;
410         else
411                 cnt = howmany(dp->di_size, sblock->fs_fsize);
412         blksout(&dp->di_db[0], cnt, ino);
413         if ((size = dp->di_size - NDADDR * sblock->fs_bsize) <= 0)
414                 return;
415         for (ind_level = 0; ind_level < NIADDR; ind_level++) {
416                 dmpindir(ino, dp->di_ib[ind_level], ind_level, &size);
417                 if (size <= 0)
418                         return;
419         }
420 }
421
422 /*
423  * Read indirect blocks, and pass the data blocks to be dumped.
424  */
425 static void
426 dmpindir(ufs1_ino_t ino, daddr_t blk, int ind_level, fsizeT *size)
427 {
428         int i, cnt;
429         daddr_t idblk[MAXNINDIR];
430
431         if (blk != 0)
432                 bread(fsbtodb(sblock, blk), (char *)idblk, (int) sblock->fs_bsize);
433         else
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);
438                 else
439                         cnt = NINDIR(sblock) * sblock->fs_frag;
440                 *size -= NINDIR(sblock) * sblock->fs_bsize;
441                 blksout(&idblk[0], cnt, ino);
442                 return;
443         }
444         ind_level--;
445         for (i = 0; i < NINDIR(sblock); i++) {
446                 dmpindir(ino, idblk[i], ind_level, size);
447                 if (*size <= 0)
448                         return;
449         }
450 }
451
452 /*
453  * Collect up the data into tape record sized buffers and output them.
454  */
455 void
456 blksout(daddr_t *blkp, int frags, ufs1_ino_t ino)
457 {
458         daddr_t *bp;
459         int i, j, count, blks, tbperdb;
460
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)
465                         count = blks;
466                 else
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;
471                         else
472                                 spcl.c_addr[j - i] = 0;
473                 spcl.c_count = count - i;
474                 writeheader(ino);
475                 bp = &blkp[i / tbperdb];
476                 for (j = i; j < count; j += tbperdb, bp++)
477                         if (*bp != 0) {
478                                 if (j + tbperdb <= count)
479                                         dumpblock(*bp, (int)sblock->fs_bsize);
480                                 else
481                                         dumpblock(*bp, (count - j) * TP_BSIZE);
482                         }
483                 spcl.c_type = TS_ADDR;
484         }
485 }
486
487 /*
488  * Dump a map to the tape.
489  */
490 void
491 dumpmap(const char *map, int type, ufs1_ino_t ino)
492 {
493         int i;
494         const char *cp;
495
496         spcl.c_type = type;
497         spcl.c_count = howmany(mapsize * sizeof(char), TP_BSIZE);
498         writeheader(ino);
499         for (i = 0, cp = map; i < spcl.c_count; i++, cp += TP_BSIZE)
500                 writerec(cp, 0);
501 }
502
503 /*
504  * Write a header record to the dump tape.
505  */
506 void
507 writeheader(ufs1_ino_t ino)
508 {
509         int32_t sum, cnt, *lp;
510
511         spcl.c_inumber = ino;
512         spcl.c_magic = NFS_MAGIC;
513         spcl.c_checksum = 0;
514         lp = (int32_t *)&spcl;
515         sum = 0;
516         cnt = sizeof(union u_spcl) / (4 * sizeof(int32_t));
517         while (--cnt >= 0) {
518                 sum += *lp++;
519                 sum += *lp++;
520                 sum += *lp++;
521                 sum += *lp++;
522         }
523         spcl.c_checksum = CHECKSUM - sum;
524         writerec(&spcl, 1);
525 }
526
527 struct ufs1_dinode *
528 getino(ufs1_ino_t inum)
529 {
530         static daddr_t minino, maxino;
531         static struct ufs1_dinode inoblock[MAXINOPB];
532
533         curino = inum;
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]);
541 }
542
543 /*
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.
548  */
549 int     breaderrors = 0;
550 #define BREADEMAX 32
551
552 void
553 bread(daddr_t blkno, char *buf, int size)
554 {
555         int cnt, i;
556
557 loop:
558         cnt = cread(diskfd, buf, size, ((off_t)blkno << dev_bshift));
559         if (cnt == size)
560                 return;
561         if (blkno + (size / dev_bsize) > fsbtodb(sblock, sblock->fs_size)) {
562                 /*
563                  * Trying to read the final fragment.
564                  *
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)
571                  */
572                 size -= dev_bsize;
573                 goto loop;
574         }
575         if (cnt == -1)
576                 msg("read error from %s: %s: [block %d]: count=%d\n",
577                         disk, strerror(errno), blkno, size);
578         else
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",
583                         BREADEMAX, disk);
584                 broadcast("DUMP IS AILING!\n");
585                 msg("This is an unrecoverable error.\n");
586                 if (!query("Do you want to attempt to continue?")){
587                         dumpabort(0);
588                         /*NOTREACHED*/
589                 } else
590                         breaderrors = 0;
591         }
592         /*
593          * Zero buffer, then try to read each sector of buffer separately,
594          * and bypass the cache.
595          */
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)
600                         continue;
601                 if (cnt == -1) {
602                         msg("read error from %s: %s: [sector %d]: count=%ld\n",
603                                 disk, strerror(errno), blkno, dev_bsize);
604                         continue;
605                 }
606                 msg("short read error from %s: [sector %d]: count=%ld, got=%d\n",
607                         disk, blkno, dev_bsize, cnt);
608         }
609 }