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