]> git.wh0rd.org Git - dump.git/blob - dump/traverse.c
SQLlite and QFA overhaul.
[dump.git] / dump / traverse.c
1 /*
2  *      Ported to Linux's Second Extended File System as part of the
3  *      dump and restore backup suit
4  *      Remy Card <card@Linux.EU.Org>, 1994-1997
5  *      Stelian Pop <stelian@popies.net>, 1999-2000
6  *      Stelian Pop <stelian@popies.net> - AlcĂ´ve <www.alcove.com>, 2000-2002
7  */
8
9 /*-
10  * Copyright (c) 1980, 1988, 1991, 1993
11  *      The Regents of the University of California.  All rights reserved.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  * 3. Neither the name of the University nor the names of its contributors
22  *    may be used to endorse or promote products derived from this software
23  *    without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35  * SUCH DAMAGE.
36  */
37
38 #ifndef lint
39 static const char rcsid[] =
40         "$Id: traverse.c,v 1.74 2011/06/10 13:41:41 stelian Exp $";
41 #endif /* not lint */
42
43 #include <config.h>
44 #include <ctype.h>
45 #include <stdio.h>
46 #include <sys/types.h>
47 #ifdef __STDC__
48 #include <string.h>
49 #include <unistd.h>
50 #endif
51 #include <errno.h>
52
53 #include <sys/param.h>
54 #include <sys/stat.h>
55 #ifdef  __linux__
56 #ifdef HAVE_EXT2FS_EXT2_FS_H
57 #include <ext2fs/ext2_fs.h>
58 #else
59 #include <linux/ext2_fs.h>
60 #endif
61 #include <ext2fs/ext2fs.h>
62 #include <bsdcompat.h>
63 #include <compaterr.h>
64 #include <stdlib.h>
65 #elif defined sunos
66 #include <sys/vnode.h>
67
68 #include <ufs/fs.h>
69 #include <ufs/fsdir.h>
70 #include <ufs/inode.h>
71 #else
72 #include <ufs/ufs/dir.h>
73 #include <ufs/ufs/dinode.h>
74 #include <ufs/ffs/fs.h>
75 #endif  /* __linux__ */
76
77 #include <protocols/dumprestore.h>
78
79 #include "dump.h"
80 #include "indexer.h"
81
82 #define HASDUMPEDFILE   0x1
83 #define HASSUBDIRS      0x2
84
85 #ifdef __linux__
86 typedef u_quad_t fsizeT;
87 #else
88 #ifdef  FS_44INODEFMT
89 typedef quad_t fsizeT;
90 #else
91 typedef long fsizeT;
92 #endif
93 #endif
94
95 #ifdef  __linux__
96 static  int searchdir __P((struct ext2_dir_entry *dp, int offset,
97                            int blocksize, char *buf, void *private));
98 #else
99 static  int dirindir __P((dump_ino_t ino, daddr_t blkno, int level, long *size));
100 static  void dmpindir __P((dump_ino_t ino, daddr_t blk, int level, fsizeT *size));
101 static  int searchdir __P((dump_ino_t ino, daddr_t blkno, long size, long filesize));
102 #endif
103 static  void mapfileino __P((dump_ino_t ino, struct dinode const *dp, long long *tapesize, int *dirskipped));
104 static void dump_xattr __P((dump_ino_t ino, struct dinode *dp));
105
106 #ifdef HAVE_EXT2_JOURNAL_INUM
107 #define ext2_journal_ino(sb) (sb->s_journal_inum)
108 #else
109 #define ext2_journal_ino(sb) (*((u_int32_t *)sb + 0x38))
110 #endif
111 #ifndef HAVE_EXT2_INO_T
112 typedef ino_t ext2_ino_t;
113 #endif
114
115 #ifndef EXT3_FEATURE_COMPAT_HAS_JOURNAL
116 #define EXT3_FEATURE_COMPAT_HAS_JOURNAL         0x0004
117 #endif
118 #ifndef EXT2_FEATURE_INCOMPAT_FILETYPE
119 #define EXT2_FEATURE_INCOMPAT_FILETYPE          0x0002
120 #endif
121 #ifndef EXT3_FEATURE_INCOMPAT_RECOVER
122 #define EXT3_FEATURE_INCOMPAT_RECOVER           0x0004
123 #endif
124 #ifndef EXT3_FEATURE_INCOMPAT_JOURNAL_DEV
125 #define EXT3_FEATURE_INCOMPAT_JOURNAL_DEV       0x0008
126 #endif
127
128 #ifndef EXT2_LIB_FEATURE_INCOMPAT_SUPP
129 #define EXT2_LIB_FEATURE_INCOMPAT_SUPP (EXT3_FEATURE_INCOMPAT_RECOVER | \
130                                         EXT2_FEATURE_INCOMPAT_FILETYPE)
131 #endif
132 #ifndef EXT2_RESIZE_INO
133 #define EXT2_RESIZE_INO                 7
134 #endif
135 #ifndef EXT2_FT_UNKNOWN
136 #define EXT2_FT_UNKNOWN         0
137 #define EXT2_FT_REG_FILE        1
138 #define EXT2_FT_DIR             2
139 #define EXT2_FT_CHRDEV          3
140 #define EXT2_FT_BLKDEV          4
141 #define EXT2_FT_FIFO            5
142 #define EXT2_FT_SOCK            6
143 #define EXT2_FT_SYMLINK         7
144 #define EXT2_FT_MAX             8
145 #endif
146
147 int dump_fs_open(const char *disk, ext2_filsys *fs)
148 {
149         int retval;
150
151         retval = ext2fs_open(disk, EXT2_FLAG_FORCE, 0, 0, unix_io_manager, fs);
152         if (!retval) {
153                 struct ext2_super_block *es = (*fs)->super;
154                 dump_ino_t journal_ino = ext2_journal_ino(es);
155
156                 if (es->s_feature_incompat & EXT3_FEATURE_INCOMPAT_JOURNAL_DEV){
157                         msg("This is a journal, not a filesystem!\n");
158                         retval = EXT2_ET_UNSUPP_FEATURE;
159                         ext2fs_close(*fs);
160                 }
161                 else if ((retval = es->s_feature_incompat &
162                                         ~(EXT2_LIB_FEATURE_INCOMPAT_SUPP |
163                                           EXT3_FEATURE_INCOMPAT_RECOVER))) {
164                         msg("Unsupported feature(s) 0x%x in filesystem\n",
165                                 retval);
166                         retval = EXT2_ET_UNSUPP_FEATURE;
167                         ext2fs_close(*fs);
168                 }
169                 else {
170                         if (es->s_feature_compat &
171                                 EXT3_FEATURE_COMPAT_HAS_JOURNAL && 
172                                 journal_ino)
173                                 do_exclude_ino(journal_ino, "journal inode");
174                         do_exclude_ino(EXT2_RESIZE_INO, "resize inode");
175                 }
176         }
177         return retval;
178 }
179
180 /*
181  * This is an estimation of the number of TP_BSIZE blocks in the file.
182  * It estimates the number of blocks in files with holes by assuming
183  * that all of the blocks accounted for by di_blocks are data blocks
184  * (when some of the blocks are usually used for indirect pointers);
185  * hence the estimate may be high.
186  */
187 long
188 blockest(struct dinode const *dp)
189 {
190         long blkest, sizeest;
191         u_quad_t i_size;
192
193         /*
194          * dp->di_size is the size of the file in bytes.
195          * dp->di_blocks stores the number of sectors actually in the file.
196          * If there are more sectors than the size would indicate, this just
197          *      means that there are indirect blocks in the file or unused
198          *      sectors in the last file block; we can safely ignore these
199          *      (blkest = sizeest below).
200          * If the file is bigger than the number of sectors would indicate,
201          *      then the file has holes in it.  In this case we must use the
202          *      block count to estimate the number of data blocks used, but
203          *      we use the actual size for estimating the number of indirect
204          *      dump blocks (sizeest vs. blkest in the indirect block
205          *      calculation).
206          */
207         blkest = howmany((u_quad_t)dp->di_blocks * 512, fs->blocksize) * (fs->blocksize / TP_BSIZE);
208         i_size = dp->di_size + ((u_quad_t) dp->di_size_high << 32);
209         sizeest = howmany(i_size, fs->blocksize) * (fs->blocksize / TP_BSIZE);
210         if (blkest > sizeest)
211                 blkest = sizeest;
212 #ifdef  __linux__
213         if ((dp->di_mode & IFMT) == IFDIR) {
214                 /*
215                  * for directories, assume only half of space is filled
216                  * with entries.  
217                  */
218                  blkest = blkest / 2;
219                  sizeest = sizeest / 2;
220         }
221         if (i_size > (u_quad_t)fs->blocksize * NDADDR) {
222                 /* calculate the number of indirect blocks on the dump tape */
223                 blkest +=
224                         howmany(sizeest - NDADDR * fs->blocksize / TP_BSIZE,
225                         TP_NINDIR);
226         }
227 #else
228         if (i_size > sblock->fs_bsize * NDADDR) {
229                 /* calculate the number of indirect blocks on the dump tape */
230                 blkest +=
231                         howmany(sizeest - NDADDR * sblock->fs_bsize / TP_BSIZE,
232                         TP_NINDIR);
233         }
234 #endif
235         return (blkest + 1);
236 }
237
238 /* Auxiliary macro to pick up files changed since previous dump. */
239 #define CSINCE(dp, t) \
240         ((dp)->di_ctime >= (t))
241 #define MSINCE(dp, t) \
242         ((dp)->di_mtime >= (t))
243 #define CHANGEDSINCE(dp, t) \
244         CSINCE(dp, t)
245
246 /* The NODUMP_FLAG macro tests if a file has the nodump flag. */
247 #ifdef UF_NODUMP
248 #define NODUMP_FLAG(dp) (!nonodump && (((dp)->di_flags & UF_NODUMP) == UF_NODUMP))
249 #else
250 #define NODUMP_FLAG(dp) 0
251 #endif
252
253 /* The WANTTODUMP macro decides whether a file should be dumped. */
254 #define WANTTODUMP(dp, ino) \
255         (CHANGEDSINCE(dp, ((u_int32_t)spcl.c_ddate)) && \
256          (!NODUMP_FLAG(dp)) && \
257          (!exclude_ino(ino)))
258
259 /*
260  * Determine if given inode should be dumped. "dp" must either point to a
261  * copy of the given inode, or be NULL (in which case it is fetched.)
262  */
263 static void
264 mapfileino(dump_ino_t ino, struct dinode const *dp, long long *tapesize, int *dirskipped)
265 {
266         int mode;
267
268         /*
269          * Skip inode if we've already marked it for dumping
270          */
271         if (TSTINO(ino, usedinomap))
272                 return;
273         if (!dp)
274                 dp = getino(ino);
275         if ((mode = (dp->di_mode & IFMT)) == 0)
276                 return;
277 #ifdef  __linux__
278         if (dp->di_nlink == 0 || dp->di_dtime != 0)
279                 return;
280 #endif
281         /*
282          * Put all dirs in dumpdirmap, inodes that are to be dumped in the
283          * used map. All inode but dirs who have the nodump attribute go
284          * to the usedinomap.
285          */
286         SETINO(ino, usedinomap);
287
288         if (NODUMP_FLAG(dp))
289                 do_exclude_ino(ino, "nodump attribute");
290
291         if (mode == IFDIR)
292                 SETINO(ino, dumpdirmap);
293         if (WANTTODUMP(dp, ino)) {
294                 SETINO(ino, dumpinomap);
295                 if (!MSINCE(dp, (u_int32_t)spcl.c_ddate))
296                         SETINO(ino, metainomap);
297                 if (mode != IFREG && mode != IFDIR && mode != IFLNK)
298                         *tapesize += 1;
299                 else
300                         *tapesize += blockest(dp);
301                 return;
302         }
303         if (mode == IFDIR) {
304                 if (exclude_ino(ino))
305                         CLRINO(ino, usedinomap);
306                 *dirskipped = 1;
307         }
308 }
309
310 /*
311  * Dump pass 1.
312  *
313  * Walk the inode list for a filesystem to find all allocated inodes
314  * that have been modified since the previous dump time. Also, find all
315  * the directories in the filesystem.
316  */
317 #ifdef __linux__
318 int
319 mapfiles(UNUSED(dump_ino_t maxino), long long *tapesize)
320 {
321         ext2_ino_t ino;
322         int anydirskipped = 0;
323         ext2_inode_scan scan;
324         errcode_t err;
325         struct ext2_inode inode;
326
327         /*
328          * We use libext2fs's inode scanning routines, which are particularly
329          * robust.  (Note that getino cannot return an error.)
330          */
331         err = ext2fs_open_inode_scan(fs, 0, &scan);
332         if (err) {
333                 com_err(disk, err, "while opening inodes\n");
334                 exit(X_ABORT);
335         }
336         for (;;) {
337                 err = ext2fs_get_next_inode(scan, &ino, &inode);
338                 if (err == EXT2_ET_BAD_BLOCK_IN_INODE_TABLE)
339                         continue;
340                 if (err) {
341                         com_err(disk, err, "while scanning inode #%ld\n",
342                                 (long)ino);
343                         exit(X_ABORT);
344                 }
345                 if (ino == 0)
346                         break;
347
348                 curino = ino;
349                 mapfileino(ino, (struct dinode const *)&inode, tapesize,
350                            &anydirskipped);
351         }
352         ext2fs_close_inode_scan(scan);
353
354         /*
355          * Restore gets very upset if the root is not dumped,
356          * so ensure that it always is dumped.
357          */
358         SETINO(ROOTINO, dumpinomap);
359         return (anydirskipped);
360 }
361 #else
362 int
363 mapfiles(dump_ino_t maxino, long long *tapesize)
364 {
365         dump_ino_t ino;
366         int anydirskipped = 0;
367
368         for (ino = ROOTINO; ino < maxino; ino++)
369                 mapfileino(ino, tapesize, &anydirskipped);
370
371         /*
372          * Restore gets very upset if the root is not dumped,
373          * so ensure that it always is dumped.
374          */
375         SETINO(ROOTINO, dumpinomap);
376         return (anydirskipped);
377 }
378 #endif /* __linux__ */
379
380 #ifdef __linux__
381 int
382 maponefile(UNUSED(dump_ino_t maxino), long long *tapesize, char *directory)
383 {
384         errcode_t retval;
385         ext2_ino_t dir_ino;
386         char dir_name [MAXPATHLEN];
387         int i, anydirskipped = 0;
388
389         /*
390          * Mark every directory in the path as being dumped
391          */
392         for (i = 0; i < (int)strlen (directory); i++) {
393                 if (directory[i] == '/') {
394                         strncpy (dir_name, directory, i);
395                         dir_name[i] = '\0';
396                         retval = ext2fs_namei(fs, ROOTINO, ROOTINO, 
397                                               dir_name, &dir_ino);
398                         if (retval) {
399                                 com_err(disk, retval, 
400                                         "while translating %s", dir_name);
401                                 exit(X_ABORT);
402                         }
403                         mapfileino((dump_ino_t) dir_ino, 0,
404                                    tapesize, &anydirskipped);
405                 }
406         }
407         /*
408          * Mark the final directory
409          */
410         retval = ext2fs_namei(fs, ROOTINO, ROOTINO, directory, &dir_ino);
411         if (retval) {
412                 com_err(disk, retval, "while translating %s", directory);
413                 exit(X_ABORT);
414         }
415         mapfileino((dump_ino_t)dir_ino, 0, tapesize, &anydirskipped);
416
417         mapfileino(ROOTINO, 0, tapesize, &anydirskipped);
418
419         /*
420          * Restore gets very upset if the root is not dumped,
421          * so ensure that it always is dumped.
422          */
423         SETINO(ROOTINO, dumpdirmap);
424         return anydirskipped;
425 }
426 #endif /* __linux__ */
427
428 #ifdef  __linux__
429 struct mapfile_context {
430         long long *tapesize;
431         int *anydirskipped;
432 };
433
434 static int
435 mapfilesindir(struct ext2_dir_entry *dirent, UNUSED(int offset), 
436               UNUSED(int blocksize), UNUSED(char *buf), void *private)
437 {
438         struct dinode const *dp;
439         int mode;
440         errcode_t retval;
441         struct mapfile_context *mfc;
442         ext2_ino_t ino;
443
444         ino = dirent->inode;
445         mfc = (struct mapfile_context *)private;
446         dp = getino(dirent->inode);
447
448         mapfileino(dirent->inode, dp, mfc->tapesize, mfc->anydirskipped);
449
450         mode = dp->di_mode & IFMT;
451         if (mode == IFDIR && dp->di_nlink != 0 && dp->di_dtime == 0) {
452                 if ((dirent->name[0] != '.' || ( dirent->name_len & 0xFF ) != 1) &&
453                     (dirent->name[0] != '.' || dirent->name[1] != '.' ||
454                      ( dirent->name_len & 0xFF ) != 2)) {
455                 retval = ext2fs_dir_iterate(fs, ino, 0, NULL,
456                                             mapfilesindir, private);
457                 if (retval)
458                         return retval;
459                 }
460         }
461         return 0;
462 }
463
464 /*
465  * Dump pass 1.
466  *
467  * Walk the inode list for a filesystem to find all allocated inodes
468  * that have been modified since the previous dump time. Also, find all
469  * the directories in the filesystem.
470  */
471 int
472 mapfilesfromdir(UNUSED(dump_ino_t maxino), long long *tapesize, char *directory)
473 {
474         errcode_t retval;
475         struct mapfile_context mfc;
476         ext2_ino_t dir_ino;
477         char dir_name [MAXPATHLEN];
478         int i, anydirskipped = 0;
479
480         /*
481          * Mark every directory in the path as being dumped
482          */
483         for (i = 0; i < (int)strlen (directory); i++) {
484                 if (directory[i] == '/') {
485                         strncpy (dir_name, directory, i);
486                         dir_name[i] = '\0';
487                         retval = ext2fs_namei(fs, ROOTINO, ROOTINO, dir_name,
488                                               &dir_ino);
489                         if (retval) {
490                                 com_err(disk, retval, "while translating %s",
491                                         dir_name);
492                                 exit(X_ABORT);
493                         }
494                         mapfileino(dir_ino, 0, tapesize, &anydirskipped);
495                 }
496         }
497         /*
498          * Mark the final directory
499          */
500         retval = ext2fs_namei(fs, ROOTINO, ROOTINO, directory, &dir_ino);
501         if (retval) {
502                 com_err(disk, retval, "while translating %s", directory);
503                 exit(X_ABORT);
504         }
505         mapfileino(dir_ino, 0, tapesize, &anydirskipped);
506
507         mfc.tapesize = tapesize;
508         mfc.anydirskipped = &anydirskipped;
509         retval = ext2fs_dir_iterate(fs, dir_ino, 0, NULL, mapfilesindir,
510                                     (void *)&mfc);
511
512         if (retval) {
513                 com_err(disk, retval, "while mapping files in %s", directory);
514                 exit(X_ABORT);
515         }
516         /*
517          * Ensure that the root inode actually appears in the file list
518          * for a subdir
519          */
520         mapfileino(ROOTINO, 0, tapesize, &anydirskipped);
521         /*
522          * Restore gets very upset if the root is not dumped,
523          * so ensure that it always is dumped.
524          */
525         SETINO(ROOTINO, dumpinomap);
526         return anydirskipped;
527 }
528 #endif
529
530 #ifdef __linux__
531 struct mapdirs_context {
532         int *ret;
533         int nodump;
534         long long *tapesize;
535 };
536 #endif
537
538 /*
539  * Dump pass 2.
540  *
541  * Scan each directory on the filesystem to see if it has any modified
542  * files in it. If it does, and has not already been added to the dump
543  * list (because it was itself modified), then add it. If a directory
544  * has not been modified itself, contains no modified files and has no
545  * subdirectories, then it can be deleted from the dump list and from
546  * the list of directories. By deleting it from the list of directories,
547  * its parent may now qualify for the same treatment on this or a later
548  * pass using this algorithm.
549  */
550 int
551 mapdirs(dump_ino_t maxino, long long *tapesize)
552 {
553         struct  dinode *dp;
554         int isdir;
555         char *map;
556         dump_ino_t ino;
557 #ifndef __linux__
558         int i;
559         long filesize;
560 #else
561         struct mapdirs_context mdc;
562 #endif
563         int ret, change = 0, nodump;
564
565         isdir = 0;              /* XXX just to get gcc to shut up */
566         for (map = dumpdirmap, ino = 1; ino < maxino; ino++) {
567                 if (((ino - 1) % NBBY) == 0)    /* map is offset by 1 */
568                         isdir = *map++;
569                 else
570                         isdir >>= 1;
571                 /*
572                  * If dir has been removed from the used map, it's either
573                  * because it had the nodump flag, or it herited it from
574                  * its parent. A directory can't be in dumpinomap if not
575                  * in usedinomap, but we have to go through it anyway 
576                  * to propagate the nodump attribute.
577                  */
578                 if ((isdir & 1) == 0)
579                         continue;
580                 nodump = (TSTINO(ino, usedinomap) == 0);
581                 if (TSTINO(ino, dumpinomap) && nodump == 0)
582                         continue;
583                 dp = getino(ino);
584 #ifdef  __linux__
585                 ret = 0;
586                 mdc.ret = &ret;
587                 mdc.nodump = nodump;
588                 mdc.tapesize = tapesize;
589                 ext2fs_dir_iterate(fs, ino, 0, NULL, searchdir, (void *) &mdc);
590 #else   /* __linux__ */
591                 filesize = dp->di_size;
592                 for (ret = 0, i = 0; filesize > 0 && i < NDADDR; i++) {
593                         if (dp->di_db[i] != 0)
594                                 ret |= searchdir(ino, dp->di_db[i],
595                                         (long)dblksize(sblock, dp, i),
596                                         filesize);
597                         if (ret & HASDUMPEDFILE)
598                                 filesize = 0;
599                         else
600                                 filesize -= sblock->fs_bsize;
601                 }
602                 for (i = 0; filesize > 0 && i < NIADDR; i++) {
603                         if (dp->di_ib[i] == 0)
604                                 continue;
605                         ret |= dirindir(ino, dp->di_ib[i], i, &filesize);
606                 }
607 #endif  /* __linux__ */
608                 if (ret & HASDUMPEDFILE) {
609                         SETINO(ino, dumpinomap);
610                         *tapesize += blockest(dp);
611                         change = 1;
612                         continue;
613                 }
614                 if (nodump) {
615                         if (ret & HASSUBDIRS)
616                                 change = 1; /* subdirs have inherited nodump */
617                         CLRINO(ino, dumpdirmap);
618                 } else if ((ret & HASSUBDIRS) == 0) {
619                         if (!TSTINO(ino, dumpinomap)) {
620                                 CLRINO(ino, dumpdirmap);
621                                 change = 1;
622                         }
623                 }
624         }
625         return (change);
626 }
627
628 #ifndef __linux__
629 /*
630  * Read indirect blocks, and pass the data blocks to be searched
631  * as directories. Quit as soon as any entry is found that will
632  * require the directory to be dumped.
633  */
634 static int
635 dirindir(dump_ino_t ino, daddr_t blkno, int ind_level, long *filesize)
636 {
637         int ret = 0;
638         int i;
639         daddr_t idblk[MAXNINDIR];
640
641         bread(fsbtodb(sblock, blkno), (char *)idblk, (int)sblock->fs_bsize);
642         if (ind_level <= 0) {
643                 for (i = 0; *filesize > 0 && i < NINDIR(sblock); i++) {
644                         blkno = idblk[i];
645                         if (blkno != 0)
646                                 ret |= searchdir(ino, blkno, sblock->fs_bsize,
647                                         *filesize);
648                         if (ret & HASDUMPEDFILE)
649                                 *filesize = 0;
650                         else
651                                 *filesize -= sblock->fs_bsize;
652                 }
653                 return (ret);
654         }
655         ind_level--;
656         for (i = 0; *filesize > 0 && i < NINDIR(sblock); i++) {
657                 blkno = idblk[i];
658                 if (blkno != 0)
659                         ret |= dirindir(ino, blkno, ind_level, filesize);
660         }
661         return (ret);
662 }
663 #endif  /* !__linux__ */
664
665 /*
666  * Scan a disk block containing directory information looking to see if
667  * any of the entries are on the dump list and to see if the directory
668  * contains any subdirectories.
669  */
670 #ifdef  __linux__
671 static  int
672 searchdir(struct ext2_dir_entry *dp, UNUSED(int offset), 
673           UNUSED(int blocksize), UNUSED(char *buf), void *private)
674 {
675         struct mapdirs_context *mdc;
676         int *ret;
677         long long *tapesize;
678         struct dinode *ip;
679
680         mdc = (struct mapdirs_context *)private;
681         ret = mdc->ret;
682         tapesize = mdc->tapesize;
683
684         if (dp->inode == 0)
685                 return 0;
686         if (dp->name[0] == '.') {
687                 if (( dp->name_len & 0xFF ) == 1)
688                         return 0;
689                 if (dp->name[1] == '.' && ( dp->name_len & 0xFF ) == 2)
690                         return 0;
691         }
692         if (mdc->nodump) {
693                 ip = getino(dp->inode);
694                 if (TSTINO(dp->inode, dumpinomap)) {
695                         CLRINO(dp->inode, dumpinomap);
696                         *tapesize -= blockest(ip);
697                 }
698                 /* Add dir back to the dir map and remove from
699                  * usedinomap to propagate nodump */
700                 if ((ip->di_mode & IFMT) == IFDIR) {
701                         SETINO(dp->inode, dumpdirmap);
702                         CLRINO(dp->inode, usedinomap);
703                         *ret |= HASSUBDIRS;
704                 }
705         } else {
706                 if (TSTINO(dp->inode, dumpinomap)) {
707                         *ret |= HASDUMPEDFILE;
708                         if (*ret & HASSUBDIRS)
709                                 return DIRENT_ABORT;
710                 }
711                 if (TSTINO(dp->inode, dumpdirmap)) {
712                         *ret |= HASSUBDIRS;
713                         if (*ret & HASDUMPEDFILE)
714                                 return DIRENT_ABORT;
715                 }
716         }
717         return 0;
718 }
719
720 #else   /* __linux__ */
721
722 static int
723 searchdir(dump_ino_t ino, daddr_t blkno, long size, long filesize)
724 {
725         struct direct *dp;
726         long loc, ret = 0;
727         char dblk[MAXBSIZE];
728
729         bread(fsbtodb(sblock, blkno), dblk, (int)size);
730         if (filesize < size)
731                 size = filesize;
732         for (loc = 0; loc < size; ) {
733                 dp = (struct direct *)(dblk + loc);
734                 if (dp->d_reclen == 0) {
735                         msg("corrupted directory, inumber %d\n", ino);
736                         break;
737                 }
738                 loc += dp->d_reclen;
739                 if (dp->d_ino == 0)
740                         continue;
741                 if (dp->d_name[0] == '.') {
742                         if (dp->d_name[1] == '\0')
743                                 continue;
744                         if (dp->d_name[1] == '.' && dp->d_name[2] == '\0')
745                                 continue;
746                 }
747                 if (TSTINO(dp->d_ino, dumpinomap)) {
748                         ret |= HASDUMPEDFILE;
749                         if (ret & HASSUBDIRS)
750                                 break;
751                 }
752                 if (TSTINO(dp->d_ino, dumpdirmap)) {
753                         ret |= HASSUBDIRS;
754                         if (ret & HASDUMPEDFILE)
755                                 break;
756                 }
757         }
758         return (ret);
759 }
760 #endif  /* __linux__ */
761
762 #ifdef  __linux__
763
764 struct block_context {
765         ext2_ino_t ino;
766         int     *buf;
767         int     cnt;
768         int     max;
769         int     next_block;
770 };
771
772 /*
773  * Dump a block to the tape
774  */
775 static int
776 dumponeblock(UNUSED(ext2_filsys fs), blk_t *blocknr, e2_blkcnt_t blockcnt,
777              UNUSED(blk_t ref_block), UNUSED(int ref_offset), void * private)
778 {
779         struct block_context *p;
780         e2_blkcnt_t i;
781
782         p = (struct block_context *)private;
783         for (i = p->next_block; i < blockcnt; i++) {
784                 p->buf[p->cnt++] = 0;
785                 if (p->cnt == p->max) {
786                         blksout (p->buf, p->cnt, p->ino);
787                         p->cnt = 0;
788                 }
789         }
790         p->buf[p->cnt++] = *blocknr;
791         if (p->cnt == p->max) {
792                 blksout (p->buf, p->cnt, p->ino);
793                 p->cnt = 0;
794         }
795         p->next_block = blockcnt + 1;
796         return 0;
797 }
798 #endif
799
800 static void
801 dump_xattr(dump_ino_t ino, struct dinode *dp) {
802
803         if (dp->di_extraisize != 0) {
804 #ifdef HAVE_EXT2FS_READ_INODE_FULL
805                 char inode[EXT2_INODE_SIZE(fs->super)];
806                 errcode_t err;
807                 u_int32_t *magic;
808
809                 memset(inode, 0, EXT2_INODE_SIZE(fs->super));
810                 err = ext2fs_read_inode_full(fs, (ext2_ino_t)ino,
811                                              (struct ext2_inode *) inode,
812                                              EXT2_INODE_SIZE(fs->super));
813                 if (err) {
814                         com_err(disk, err, "while reading inode #%ld\n", (long)ino);
815                         exit(X_ABORT);
816                 }
817
818                 magic = (void *)inode + EXT2_GOOD_OLD_INODE_SIZE + dp->di_extraisize;
819                 if (*magic == EXT2_XATTR_MAGIC) {
820                         char xattr[EXT2_INODE_SIZE(fs->super)];
821                         int i;
822                         char *cp;
823
824                         if (vflag)
825                                 msg("dumping EA (inode) in inode #%ld\n", (long)ino);
826                         memset(xattr, 0, EXT2_INODE_SIZE(fs->super));
827                         memcpy(xattr, (void *)magic,
828                                EXT2_INODE_SIZE(fs->super) - 
829                                (EXT2_GOOD_OLD_INODE_SIZE + dp->di_extraisize));
830                         magic = (u_int32_t *)xattr;
831                         *magic = EXT2_XATTR_MAGIC2;
832
833                         spcl.c_type = TS_INODE;
834                         spcl.c_dinode.di_size = EXT2_INODE_SIZE(fs->super);
835                         spcl.c_flags |= DR_EXTATTRIBUTES;
836                         spcl.c_extattributes = EXT_XATTR;
837                         spcl.c_count = howmany(EXT2_INODE_SIZE(fs->super), TP_BSIZE);
838                         for (i = 0; i < spcl.c_count; i++)
839                                 spcl.c_addr[i] = 1;
840                         writeheader(ino);
841                         for (i = 0, cp = xattr; i < spcl.c_count; i++, cp += TP_BSIZE)
842                                 writerec(cp, 0);
843                         spcl.c_flags &= ~DR_EXTATTRIBUTES;
844                         spcl.c_extattributes = 0;
845                 }
846 #endif
847         }
848
849         if (dp->di_file_acl) {
850
851                 if (vflag)
852                         msg("dumping EA (block) in inode #%ld\n", (long)ino);
853
854                 spcl.c_type = TS_INODE;
855                 spcl.c_dinode.di_size = sblock->fs_bsize;
856                 spcl.c_flags |= DR_EXTATTRIBUTES;
857                 spcl.c_extattributes = EXT_XATTR;
858                 blksout(&dp->di_file_acl, EXT2_FRAGS_PER_BLOCK(fs->super), ino);
859                 spcl.c_flags &= ~DR_EXTATTRIBUTES;
860                 spcl.c_extattributes = 0;
861         }
862 }
863
864 /*
865  * Dump passes 3 and 4.
866  *
867  * Dump the contents of an inode to tape.
868  */
869 void
870 dumpino(struct dinode *dp, dump_ino_t ino, int metaonly)
871 {
872         //unsigned long cnt;
873         //fsizeT size;
874         fsizeT remaining;
875         char buf[TP_BSIZE];
876         struct new_bsd_inode nbi;
877         int i;
878 #ifdef  __linux__
879         struct block_context bc;
880 #else
881         int ind_level;
882 #endif
883         u_quad_t i_size;
884         
885         if (metaonly)
886                 i_size = 0;
887         else 
888                 i_size = dp->di_size + ((u_quad_t) dp->di_size_high << 32);
889
890         if (newtape) {
891                 newtape = 0;
892                 dumpmap(dumpinomap, TS_BITS, ino);
893         }
894         CLRINO(ino, dumpinomap);
895 #ifdef  __linux__
896         memset(&nbi, 0, sizeof(nbi));
897         nbi.di_mode = dp->di_mode;
898         nbi.di_nlink = dp->di_nlink;
899         nbi.di_ouid = dp->di_uid;
900         nbi.di_ogid = dp->di_gid;
901         nbi.di_size = i_size;
902         nbi.di_atime.tv_sec = dp->di_atime;
903         nbi.di_mtime.tv_sec = dp->di_mtime;
904         nbi.di_ctime.tv_sec = dp->di_ctime;
905         memmove(&nbi.di_db, &dp->di_db, (NDADDR + NIADDR) * sizeof(daddr_t));
906         nbi.di_flags = dp->di_flags;
907         nbi.di_blocks = dp->di_blocks;
908         nbi.di_gen = dp->di_gen;
909         nbi.di_uid = (((int32_t)dp->di_uidhigh) << 16) | dp->di_uid;
910         nbi.di_gid = (((int32_t)dp->di_gidhigh) << 16) | dp->di_gid;
911         memmove(&spcl.c_dinode, &nbi, sizeof(nbi));
912 #else   /* __linux__ */
913         spcl.c_dinode = *dp;
914 #endif  /* __linux__ */
915         spcl.c_type = TS_INODE;
916         spcl.c_count = 0;
917
918         indexer->addInode(dp, ino, metaonly);
919
920         if (metaonly && (dp->di_mode & S_IFMT)) {
921                 spcl.c_flags |= DR_METAONLY;
922                 spcl.c_count = 0;
923                 writeheader(ino);
924                 spcl.c_flags &= ~DR_METAONLY;
925                 dump_xattr(ino, dp);
926                 return;
927         }
928
929         switch (dp->di_mode & S_IFMT) {
930
931         case 0:
932                 /*
933                  * Freed inode.
934                  */
935                 return;
936
937 #ifdef  __linux__
938         case S_IFDIR:
939                 msg("Warning: dumpino called on a directory (ino %d)\n", ino);
940                 return;
941 #endif
942
943         case S_IFLNK:
944                 /*
945                  * Check for short symbolic link.
946                  */
947 #ifdef  __linux__
948                 if (i_size > 0 &&
949                     i_size < EXT2_N_BLOCKS * sizeof (daddr_t)) {
950                         spcl.c_addr[0] = 1;
951                         spcl.c_count = 1;
952                         writeheader(ino);
953                         memmove(buf, dp->di_db, (u_long)dp->di_size);
954                         buf[dp->di_size] = '\0';
955                         writerec(buf, 0);
956                         dump_xattr(ino, dp);
957                         return;
958                 }
959 #endif  /* __linux__ */
960 #ifdef FS_44INODEFMT
961                 if (dp->di_size > 0 &&
962                     dp->di_size < sblock->fs_maxsymlinklen) {
963                         spcl.c_addr[0] = 1;
964                         spcl.c_count = 1;
965                         writeheader(ino);
966                         memmove(buf, dp->di_shortlink, (u_long)dp->di_size);
967                         buf[dp->di_size] = '\0';
968                         writerec(buf, 0);
969                         return;
970                 }
971 #endif
972                 /* fall through */
973
974 #ifndef __linux__
975         case S_IFDIR:
976 #endif
977         case S_IFREG:
978                 if (i_size)
979                         break;
980                 /* fall through */
981
982         case S_IFIFO:
983         case S_IFSOCK:
984         case S_IFCHR:
985         case S_IFBLK:
986                 writeheader(ino);
987                 dump_xattr(ino, dp);
988                 return;
989
990         default:
991                 msg("Warning: undefined file type 0%o\n", dp->di_mode & IFMT);
992                 return;
993         }
994 #ifdef  __linux__
995         bc.max = NINDIR(sblock) * EXT2_FRAGS_PER_BLOCK(fs->super);
996         bc.buf = (int *)malloc (bc.max * sizeof (int));
997         bc.cnt = 0;
998         bc.ino = ino;
999         bc.next_block = 0;
1000
1001         ext2fs_block_iterate2(fs, (ext2_ino_t)ino, BLOCK_FLAG_DATA_ONLY, NULL, dumponeblock, (void *)&bc);
1002         /* deal with holes at the end of the inode */
1003         if (i_size > ((u_quad_t)bc.next_block) * sblock->fs_fsize) {
1004                 remaining = i_size - ((u_quad_t)bc.next_block) * sblock->fs_fsize;
1005                 for (i = 0; i < (int)howmany(remaining, sblock->fs_fsize); i++) {
1006                         bc.buf[bc.cnt++] = 0;
1007                         if (bc.cnt == bc.max) {
1008                                 blksout (bc.buf, bc.cnt, bc.ino);
1009                                 bc.cnt = 0;
1010                         }
1011                 }
1012         }
1013         if (bc.cnt > 0) {
1014                 blksout (bc.buf, bc.cnt, bc.ino);
1015         }
1016         free(bc.buf);
1017         dump_xattr(ino, dp);
1018 #else
1019         if (i_size > (u_quad_t)NDADDR * sblock->fs_bsize)
1020                 cnt = NDADDR * sblock->fs_frag;
1021         else
1022                 cnt = howmany(i_size, sblock->fs_fsize);
1023         blksout(&dp->di_db[0], cnt, ino);
1024         if ((quad_t) (size = i_size - NDADDR * sblock->fs_bsize) <= 0) {
1025                 dump_xattr(ino, dp);
1026                 return;
1027         }
1028         for (ind_level = 0; ind_level < NIADDR; ind_level++) {
1029                 dmpindir(ino, dp->di_ib[ind_level], ind_level, &size);
1030                 if (size <= 0)
1031                         return;
1032         }
1033 #endif
1034 }
1035
1036 #ifdef  __linux__
1037
1038 struct convert_dir_context {
1039         char *buf;
1040         int prev_offset;
1041         int offset;
1042         int bs;
1043 };
1044
1045 /*
1046  * This function converts an ext2fs directory entry to the BSD format.
1047  *
1048  * Basically, it adds a null-character at the end of the name, recomputes the
1049  * size of the entry, and creates it in a temporary buffer
1050  */
1051 static int
1052 convert_dir(struct ext2_dir_entry *dirent, UNUSED(int offset), 
1053             UNUSED(int blocksize), UNUSED(char *buf), void *private)
1054 {
1055         struct convert_dir_context *p;
1056         struct direct *dp;
1057         int reclen;
1058
1059         /* do not save entries to excluded inodes */
1060         if (exclude_ino(dirent->inode))
1061                 return 0;
1062
1063         p = (struct convert_dir_context *)private;
1064
1065         reclen = EXT2_DIR_REC_LEN((dirent->name_len & 0xFF) + 1);
1066         if (((p->offset + reclen - 1) / p->bs) != (p->offset / p->bs)) {
1067                 dp = (struct direct *)(p->buf + p->prev_offset);
1068                 dp->d_reclen += p->bs - (p->offset % p->bs);
1069                 p->offset += p->bs - (p->offset % p->bs);
1070         }
1071
1072         dp = (struct direct *)(p->buf + p->offset);
1073         dp->d_ino = dirent->inode;
1074         dp->d_reclen = reclen;
1075         dp->d_namlen = dirent->name_len & 0xFF;
1076         switch ((dirent->name_len & 0xFF00) >> 8) {
1077         default:
1078                 dp->d_type = DT_UNKNOWN;
1079                 break;
1080         case EXT2_FT_REG_FILE:
1081                 dp->d_type = DT_REG;
1082                 break;
1083         case EXT2_FT_DIR:
1084                 dp->d_type = DT_DIR;
1085                 break;
1086         case EXT2_FT_CHRDEV:
1087                 dp->d_type = DT_CHR;
1088                 break;
1089         case EXT2_FT_BLKDEV:
1090                 dp->d_type = DT_BLK;
1091                 break;
1092         case EXT2_FT_FIFO:
1093                 dp->d_type = DT_FIFO;
1094                 break;
1095         case EXT2_FT_SOCK:
1096                 dp->d_type = DT_SOCK;
1097                 break;
1098         case EXT2_FT_SYMLINK:
1099                 dp->d_type = DT_LNK;
1100                 break;
1101         }
1102         strncpy(dp->d_name, dirent->name, dp->d_namlen);
1103         dp->d_name[dp->d_namlen] = '\0';
1104         p->prev_offset = p->offset;
1105         p->offset += reclen;
1106
1107         //indexer->addDirEntry(dirent, offset, blocksize, buf, private);
1108
1109         return 0;
1110 }
1111
1112 /*
1113  * Dump pass 3
1114  *
1115  * Dumps a directory to tape after converting it to the BSD format
1116  */
1117 void
1118 dumpdirino(struct dinode *dp, dump_ino_t ino)
1119 {
1120         fsizeT size;
1121         char buf[TP_BSIZE];
1122         struct new_bsd_inode nbi;
1123         struct convert_dir_context cdc;
1124         errcode_t retval;
1125         struct ext2_dir_entry *de;
1126         fsizeT dir_size;
1127
1128         if (newtape) {
1129                 newtape = 0;
1130                 dumpmap(dumpinomap, TS_BITS, ino);
1131         }
1132         CLRINO(ino, dumpinomap);
1133
1134         /*
1135          * Convert the directory to the BSD format
1136          */
1137         /* Allocate a buffer for the conversion (twice the size of the
1138            ext2fs directory to avoid problems ;-) */
1139         cdc.buf = (char *)malloc(dp->di_size * 2 * sizeof(char));
1140         if (cdc.buf == NULL)
1141                 err(1, "Cannot allocate buffer to convert directory #%lu\n",
1142                     (unsigned long)ino);
1143         cdc.offset = 0;
1144         cdc.prev_offset = 0;
1145         cdc.bs = MIN(DIRBLKSIZ, TP_BSIZE);
1146         /* Do the conversion */
1147         retval = ext2fs_dir_iterate(fs, (ext2_ino_t)ino, 0, NULL, convert_dir, (void *)&cdc);
1148         if (retval) {
1149                 com_err(disk, retval, "while converting directory #%ld\n", (long)ino);
1150                 exit(X_ABORT);
1151         }
1152         /* Fix the last entry */
1153         if ((cdc.offset % cdc.bs) != 0) {
1154                 de = (struct ext2_dir_entry *)(cdc.buf + cdc.prev_offset);
1155                 de->rec_len += cdc.bs - (cdc.offset % cdc.bs);
1156                 cdc.offset += cdc.bs - (cdc.offset % cdc.bs);
1157         }
1158
1159         dir_size = cdc.offset;
1160
1161 #ifdef  __linux__
1162         memset(&nbi, 0, sizeof(nbi));
1163         nbi.di_mode = dp->di_mode;
1164         nbi.di_nlink = dp->di_nlink;
1165         nbi.di_ouid = dp->di_uid;
1166         nbi.di_ogid = dp->di_gid;
1167         nbi.di_size = dir_size; /* (u_quad_t)dp->di_size; */
1168         nbi.di_atime.tv_sec = dp->di_atime;
1169         nbi.di_mtime.tv_sec = dp->di_mtime;
1170         nbi.di_ctime.tv_sec = dp->di_ctime;
1171         memmove(&nbi.di_db, &dp->di_db, (NDADDR + NIADDR) * sizeof(daddr_t));
1172         nbi.di_flags = dp->di_flags;
1173         nbi.di_blocks = dp->di_blocks;
1174         nbi.di_gen = dp->di_gen;
1175         nbi.di_uid = (((int32_t)dp->di_uidhigh) << 16) | dp->di_uid;
1176         nbi.di_gid = (((int32_t)dp->di_gidhigh) << 16) | dp->di_gid;
1177         memmove(&spcl.c_dinode, &nbi, sizeof(nbi));
1178 #else   /* __linux__ */
1179         spcl.c_dinode = *dp;
1180 #endif  /* __linux__ */
1181         spcl.c_type = TS_INODE;
1182         spcl.c_count = 0;
1183         switch (dp->di_mode & S_IFMT) {
1184
1185         case 0:
1186                 /*
1187                  * Freed inode.
1188                  */
1189                 return;
1190
1191         case S_IFDIR:
1192                 if (dir_size > 0)
1193                         break;
1194                 msg("Warning: size of directory inode #%d is <= 0 (%d)!\n",
1195                         ino, dir_size);
1196                 return;
1197
1198         default:
1199                 msg("Warning: dumpdirino called with file type 0%o (inode #%d)\n",
1200                         dp->di_mode & IFMT, ino);
1201                 return;
1202         }
1203         for (size = 0; size < dir_size; size += TP_BSIZE) {
1204                 spcl.c_addr[0] = 1;
1205                 spcl.c_count = 1;
1206                 writeheader(ino);
1207                 memmove(buf, cdc.buf + size, TP_BSIZE);
1208                 writerec(buf, 0);
1209                 spcl.c_type = TS_ADDR;
1210         }
1211
1212         indexer->addInode(dp, ino, 0);
1213
1214         (void)free(cdc.buf);
1215         dump_xattr(ino, dp);
1216 }
1217 #endif  /* __linux__ */
1218
1219 #ifndef __linux__
1220 /*
1221  * Read indirect blocks, and pass the data blocks to be dumped.
1222  */
1223 static void
1224 dmpindir(dump_ino_t ino, daddr_t blk, int ind_level, fsizeT *size)
1225 {
1226         int i, cnt;
1227 #ifdef __linux__
1228         int max;
1229         blk_t *swapme;
1230 #endif
1231         daddr_t idblk[MAXNINDIR];
1232
1233         if (blk != 0) {
1234                 bread(fsbtodb(sblock, blk), (char *)idblk, (int) sblock->fs_bsize);
1235 #ifdef __linux__
1236         /* 
1237          * My RedHat 4.0 system doesn't have these flags; I haven't
1238          * upgraded e2fsprogs yet
1239          */
1240 #if defined(EXT2_FLAG_SWAP_BYTES)
1241         if ((fs->flags & EXT2_FLAG_SWAP_BYTES) ||
1242             (fs->flags & EXT2_FLAG_SWAP_BYTES_READ))
1243 #endif
1244         {
1245                 max = sblock->fs_bsize >> 2;
1246                 swapme = (blk_t *) idblk;
1247                 for (i = 0; i < max; i++, swapme++)
1248                         *swapme = ext2fs_swab32(*swapme);
1249         }
1250 #endif /* __linux__ */
1251         else
1252                 memset(idblk, 0, (int)sblock->fs_bsize);
1253         if (ind_level <= 0) {
1254                 if (*size < NINDIR(sblock) * sblock->fs_bsize)
1255                         cnt = howmany(*size, sblock->fs_fsize);
1256                 else
1257 #ifdef  __linux__
1258                         cnt = NINDIR(sblock) * EXT2_FRAGS_PER_BLOCK(fs->super);
1259 #else
1260                         cnt = NINDIR(sblock) * sblock->fs_frag;
1261 #endif
1262                 *size -= NINDIR(sblock) * sblock->fs_bsize;
1263                 blksout(&idblk[0], cnt, ino);
1264                 return;
1265         }
1266         ind_level--;
1267         for (i = 0; i < NINDIR(sblock); i++) {
1268                 dmpindir(ino, idblk[i], ind_level, size);
1269                 if (*size <= 0)
1270                         return;
1271         }
1272 }
1273 #endif
1274
1275 /*
1276  * Collect up the data into tape record sized buffers and output them.
1277  */
1278 void
1279 blksout(blk_t *blkp, int frags, dump_ino_t ino)
1280 {
1281         blk_t *bp;
1282         int i, j, count, blks, tbperdb;
1283
1284         blks = howmany(frags * sblock->fs_fsize, TP_BSIZE);
1285         tbperdb = sblock->fs_bsize >> tp_bshift;
1286         for (i = 0; i < blks; i += TP_NINDIR) {
1287                 if (i + TP_NINDIR > blks)
1288                         count = blks;
1289                 else
1290                         count = i + TP_NINDIR;
1291                 for (j = i; j < count; j++)
1292                         if (blkp[j / tbperdb] != 0)
1293                                 spcl.c_addr[j - i] = 1;
1294                         else
1295                                 spcl.c_addr[j - i] = 0;
1296                 spcl.c_count = count - i;
1297                 writeheader(ino);
1298                 bp = &blkp[i / tbperdb];
1299                 for (j = i; j < count; j += tbperdb, bp++) {
1300                         if (*bp != 0) {
1301                                 if (j + tbperdb <= count)
1302                                         dumpblock(*bp, (int)sblock->fs_bsize);
1303                                 else
1304                                         dumpblock(*bp, (count - j) * TP_BSIZE);
1305                         }
1306                 }
1307                 spcl.c_type = TS_ADDR;
1308         }
1309 }
1310
1311 /*
1312  * Dump a map to the tape.
1313  */
1314 void
1315 dumpmap(char *map, int type, dump_ino_t ino)
1316 {
1317         int i;
1318         char *cp;
1319
1320         spcl.c_type = type;
1321         spcl.c_count = howmany(mapsize * sizeof(char), TP_BSIZE);
1322         spcl.c_dinode.di_size = mapsize;
1323         writeheader(ino);
1324         for (i = 0, cp = map; i < spcl.c_count; i++, cp += TP_BSIZE)
1325                 writerec(cp, 0);
1326 }
1327
1328 #if defined(__linux__) && !defined(int32_t)
1329 #define int32_t __s32
1330 #endif
1331
1332 /* 
1333  * Compute and fill in checksum information.
1334  */
1335 void
1336 mkchecksum(union u_spcl *tmpspcl) 
1337 {
1338         int32_t sum, cnt, *lp;
1339
1340         tmpspcl->s_spcl.c_checksum = 0;
1341         lp = (int32_t *)&tmpspcl->s_spcl;
1342         sum = 0;
1343         cnt = sizeof(union u_spcl) / (4 * sizeof(int32_t));
1344         while (--cnt >= 0) {
1345                 sum += *lp++;
1346                 sum += *lp++;
1347                 sum += *lp++;
1348                 sum += *lp++;
1349         }
1350         tmpspcl->s_spcl.c_checksum = CHECKSUM - sum;
1351 }
1352
1353 /*
1354  * Write a header record to the dump tape.
1355  */
1356 void
1357 writeheader(dump_ino_t ino)
1358 {
1359         char *state; /* need to have some place to put this! */
1360         spcl.c_inumber = ino;
1361         spcl.c_magic = NFS_MAGIC;
1362         mkchecksum((union u_spcl *)&spcl);
1363         writerec((char *)&spcl, 1);
1364 }
1365
1366 #ifdef  __linux__
1367 struct dinode *
1368 getino(dump_ino_t inum)
1369 {
1370         static struct dinode dinode;
1371         errcode_t err;
1372
1373         curino = inum;
1374 #ifdef HAVE_EXT2FS_READ_INODE_FULL
1375         err = ext2fs_read_inode_full(fs, (ext2_ino_t)inum, (struct ext2_inode *) &dinode, sizeof(struct dinode));
1376 #else
1377         err = ext2fs_read_inode(fs, (ext2_ino_t)inum, (struct ext2_inode *) &dinode);
1378 #endif
1379         if (err) {
1380                 com_err(disk, err, "while reading inode #%ld\n", (long)inum);
1381                 exit(X_ABORT);
1382         }
1383         return &dinode;
1384 }
1385 #else   /* __linux__ */
1386 struct dinode *
1387 getino(dump_ino_t inum)
1388 {
1389         static daddr_t minino, maxino;
1390         static struct dinode inoblock[MAXINOPB];
1391
1392         curino = inum;
1393         if (inum >= minino && inum < maxino)
1394                 return (&inoblock[inum - minino]);
1395         bread(fsbtodb(sblock, ino_to_fsba(sblock, inum)), (char *)inoblock,
1396             (int)sblock->fs_bsize);
1397         minino = inum - (inum % INOPB(sblock));
1398         maxino = minino + INOPB(sblock);
1399         return (&inoblock[inum - minino]);
1400 }
1401 #endif  /* __linux__ */
1402
1403 /*
1404  * Read a chunk of data from the disk.
1405  * Try to recover from hard errors by reading in sector sized pieces.
1406  * Error recovery is attempted at most BREADEMAX times before seeking
1407  * consent from the operator to continue.
1408  */
1409 int     breaderrors = 0;
1410
1411 void
1412 bread(ext2_loff_t blkno, char *buf, int size)
1413 {
1414         int cnt, i;
1415
1416 loop:
1417 #ifdef  __linux__
1418         if (ext2fs_llseek(diskfd, (blkno << dev_bshift), 0) !=
1419                         (blkno << dev_bshift))
1420 #else
1421         if (lseek(diskfd, ((off_t)blkno << dev_bshift), 0) !=
1422                                                 ((off_t)blkno << dev_bshift))
1423 #endif
1424                 msg("bread: lseek fails\n");
1425         if ((cnt = read(diskfd, buf, size)) == size)
1426                 return;
1427         if (blkno + (size / dev_bsize) > fsbtodb(sblock, sblock->fs_size)) {
1428                 /*
1429                  * Trying to read the final fragment.
1430                  *
1431                  * NB - dump only works in TP_BSIZE blocks, hence
1432                  * rounds `dev_bsize' fragments up to TP_BSIZE pieces.
1433                  * It should be smarter about not actually trying to
1434                  * read more than it can get, but for the time being
1435                  * we punt and scale back the read only when it gets
1436                  * us into trouble. (mkm 9/25/83)
1437                  */
1438                 size -= dev_bsize;
1439                 goto loop;
1440         }
1441         if (cnt == -1)
1442                 msg("read error from %s: %s: [block %d, ext2blk %d]: count=%d\n",
1443                         disk, strerror(errno), blkno, 
1444                         dbtofsb(sblock, blkno), size);
1445         else
1446                 msg("short read error from %s: [block %d, ext2blk %d]: count=%d, got=%d\n",
1447                         disk, blkno, dbtofsb(sblock, blkno), size, cnt);
1448         if (breademax && ++breaderrors > breademax) {
1449                 msg("More than %d block read errors from %d\n",
1450                         breademax, disk);
1451                 broadcast("DUMP IS AILING!\n");
1452                 msg("This is an unrecoverable error.\n");
1453                 if (!query("Do you want to attempt to continue?")){
1454                         dumpabort(0);
1455                         /*NOTREACHED*/
1456                 } else
1457                         breaderrors = 0;
1458         }
1459         /*
1460          * Zero buffer, then try to read each sector of buffer separately.
1461          */
1462         memset(buf, 0, size);
1463         for (i = 0; i < size; i += dev_bsize, buf += dev_bsize, blkno++) {
1464 #ifdef  __linux__
1465                 if (ext2fs_llseek(diskfd, (blkno << dev_bshift), 0) !=
1466                                 (blkno << dev_bshift))
1467 #else
1468                 if (lseek(diskfd, ((off_t)blkno << dev_bshift), 0) !=
1469                                                 ((off_t)blkno << dev_bshift))
1470 #endif
1471                         msg("bread: lseek2 fails!\n");
1472                 if ((cnt = read(diskfd, buf, (int)dev_bsize)) == dev_bsize)
1473                         continue;
1474                 if (cnt == -1) {
1475                         msg("read error from %s: %s: [sector %d, ext2blk %d]: count=%d\n",
1476                                 disk, strerror(errno), blkno, 
1477                                 dbtofsb(sblock, blkno), dev_bsize);
1478                         continue;
1479                 }
1480                 msg("short read error from %s: [sector %d, ext2blk %d]: count=%d, got=%d\n",
1481                         disk, blkno, dbtofsb(sblock, blkno), dev_bsize, cnt);
1482         }
1483 }