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