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