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