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