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