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