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