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 <pop@cybercable.fr>, 1999
10 * Copyright (c) 1980, 1988, 1991, 1993
11 * The Regents of the University of California. All rights reserved.
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
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.
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
41 * $Id: traverse.c,v 1.5 1999/10/11 13:31:11 stelian Exp $
44 #include <sys/param.h>
47 #include <linux/ext2_fs.h>
48 #include <bsdcompat.h>
49 #include <compaterr.h>
51 #define swab32(x) ext2fs_swab32(x)
55 #include <sys/vnode.h>
58 #include <ufs/fsdir.h>
59 #include <ufs/inode.h>
61 #include <ufs/ufs/dir.h>
62 #include <ufs/ufs/dinode.h>
63 #include <ufs/ffs/fs.h>
65 #endif /* __linux__ */
67 #include <protocols/dumprestore.h>
77 #include <ext2fs/ext2fs.h>
82 #define HASDUMPEDFILE 0x1
83 #define HASSUBDIRS 0x2
86 typedef quad_t fsizeT
;
92 static int searchdir
__P((struct ext2_dir_entry
*dp
, int offset
,
93 int blocksize
, char *buf
, void *private));
94 loff_t
llseek (int fd
, loff_t offset
, int origin
);
96 static int dirindir
__P((ino_t ino
, daddr_t blkno
, int level
, long *size
));
97 static void dmpindir
__P((ino_t ino
, daddr_t blk
, int level
, fsizeT
*size
));
98 static int searchdir
__P((ino_t ino
, daddr_t blkno
, long size
, long filesize
));
102 * This is an estimation of the number of TP_BSIZE blocks in the file.
103 * It estimates the number of blocks in files with holes by assuming
104 * that all of the blocks accounted for by di_blocks are data blocks
105 * (when some of the blocks are usually used for indirect pointers);
106 * hence the estimate may be high.
109 blockest(struct dinode
*dp
)
111 long blkest
, sizeest
;
114 * dp->di_size is the size of the file in bytes.
115 * dp->di_blocks stores the number of sectors actually in the file.
116 * If there are more sectors than the size would indicate, this just
117 * means that there are indirect blocks in the file or unused
118 * sectors in the last file block; we can safely ignore these
119 * (blkest = sizeest below).
120 * If the file is bigger than the number of sectors would indicate,
121 * then the file has holes in it. In this case we must use the
122 * block count to estimate the number of data blocks used, but
123 * we use the actual size for estimating the number of indirect
124 * dump blocks (sizeest vs. blkest in the indirect block
127 blkest
= howmany(dbtob(dp
->di_blocks
), TP_BSIZE
);
128 sizeest
= howmany(dp
->di_size
, TP_BSIZE
);
129 if (blkest
> sizeest
)
132 if (dp
->di_size
> fs
->blocksize
* NDADDR
) {
133 /* calculate the number of indirect blocks on the dump tape */
135 howmany(sizeest
- NDADDR
* fs
->blocksize
/ TP_BSIZE
,
139 if (dp
->di_size
> sblock
->fs_bsize
* NDADDR
) {
140 /* calculate the number of indirect blocks on the dump tape */
142 howmany(sizeest
- NDADDR
* sblock
->fs_bsize
/ TP_BSIZE
,
149 /* Auxiliary macro to pick up files changed since previous dump. */
150 #define CHANGEDSINCE(dp, t) \
151 ((dp)->di_mtime >= (t) || (dp)->di_ctime >= (t))
153 /* The WANTTODUMP macro decides whether a file should be dumped. */
155 #define WANTTODUMP(dp) \
156 (CHANGEDSINCE(dp, spcl.c_ddate) && \
157 (nonodump || ((dp)->di_flags & UF_NODUMP) != UF_NODUMP))
159 #define WANTTODUMP(dp) CHANGEDSINCE(dp, spcl.c_ddate)
165 * Walk the inode list for a filesystem to find all allocated inodes
166 * that have been modified since the previous dump time. Also, find all
167 * the directories in the filesystem.
170 mapfiles(ino_t maxino
, long *tapesize
)
174 register struct dinode
*dp
;
175 int anydirskipped
= 0;
177 for (ino
= ROOTINO
; ino
< maxino
; ino
++) {
179 if ((mode
= (dp
->di_mode
& IFMT
)) == 0)
182 if (dp
->di_nlink
== 0 || dp
->di_dtime
!= 0)
185 SETINO(ino
, usedinomap
);
187 SETINO(ino
, dumpdirmap
);
188 if (WANTTODUMP(dp
)) {
189 SETINO(ino
, dumpinomap
);
190 if (mode
!= IFREG
&& mode
!= IFDIR
&& mode
!= IFLNK
)
193 *tapesize
+= blockest(dp
);
200 * Restore gets very upset if the root is not dumped,
201 * so ensure that it always is dumped.
203 SETINO(ROOTINO
, dumpinomap
);
204 return (anydirskipped
);
208 struct mapfile_context
{
214 mapfilesindir(struct ext2_dir_entry
*dirent
, int offset
, int blocksize
, char *buf
, void *private)
216 register struct dinode
*dp
;
220 struct mapfile_context
*mfc
;
223 mfc
= (struct mapfile_context
*)private;
225 if ((mode
= (dp
->di_mode
& IFMT
)) != 0 &&
226 dp
->di_nlink
!= 0 && dp
->di_dtime
== 0) {
227 SETINO(ino
, usedinomap
);
229 SETINO(ino
, dumpdirmap
);
230 if (WANTTODUMP(dp
)) {
231 SETINO(ino
, dumpinomap
);
232 if (mode
!= IFREG
&& mode
!= IFDIR
&& mode
!= IFLNK
)
235 *mfc
->tapesize
+= blockest(dp
);
238 mfc
->anydirskipped
= 1;
239 if ((dirent
->name
[0] != '.' || dirent
->name_len
!= 1) &&
240 (dirent
->name
[0] != '.' || dirent
->name
[1] != '.' ||
241 dirent
->name_len
!= 2)) {
242 retval
= ext2fs_dir_iterate(fs
, ino
, 0, NULL
,
243 mapfilesindir
, private);
255 * Walk the inode list for a filesystem to find all allocated inodes
256 * that have been modified since the previous dump time. Also, find all
257 * the directories in the filesystem.
260 mapfilesfromdir(ino_t maxino
, long *tapesize
, char *directory
)
263 struct mapfile_context mfc
;
265 char dir_name
[MAXPATHLEN
];
269 * Mark every directory in the path as being dumped
271 for (i
= 0; i
< strlen (directory
); i
++) {
272 if (directory
[i
] == '/') {
273 strncpy (dir_name
, directory
, i
);
275 retval
= ext2fs_namei(fs
, ROOTINO
, ROOTINO
, dir_name
,
278 com_err(disk
, retval
, "while translating %s",
282 /* SETINO(dir_ino, dumpinomap); */
283 SETINO(dir_ino
, dumpdirmap
);
287 * Mark the final directory
289 retval
= ext2fs_namei(fs
, ROOTINO
, ROOTINO
, directory
, &dir_ino
);
291 com_err(disk
, retval
, "while translating %s", directory
);
294 /* SETINO(dir_ino, dumpinomap); */
295 SETINO(dir_ino
, dumpdirmap
);
297 mfc
.tapesize
= tapesize
;
298 mfc
.anydirskipped
= 0;
299 retval
= ext2fs_dir_iterate(fs
, dir_ino
, 0, NULL
, mapfilesindir
,
303 com_err(disk
, retval
, "while mapping files in %s", directory
);
307 * Restore gets very upset if the root is not dumped,
308 * so ensure that it always is dumped.
310 /* SETINO(ROOTINO, dumpinomap); */
311 SETINO(ROOTINO
, dumpdirmap
);
312 return (mfc
.anydirskipped
);
319 * Scan each directory on the filesystem to see if it has any modified
320 * files in it. If it does, and has not already been added to the dump
321 * list (because it was itself modified), then add it. If a directory
322 * has not been modified itself, contains no modified files and has no
323 * subdirectories, then it can be deleted from the dump list and from
324 * the list of directories. By deleting it from the list of directories,
325 * its parent may now qualify for the same treatment on this or a later
326 * pass using this algorithm.
329 mapdirs(ino_t maxino
, long *tapesize
)
331 register struct dinode
*dp
;
341 isdir
= 0; /* XXX just to get gcc to shut up */
342 for (map
= dumpdirmap
, ino
= 1; ino
< maxino
; ino
++) {
343 if (((ino
- 1) % NBBY
) == 0) /* map is offset by 1 */
347 if ((isdir
& 1) == 0 || TSTINO(ino
, dumpinomap
))
352 ext2fs_dir_iterate(fs
, ino
, 0, NULL
, searchdir
, (void *) &ret
);
353 #else /* __linux__ */
354 filesize
= dp
->di_size
;
355 for (ret
= 0, i
= 0; filesize
> 0 && i
< NDADDR
; i
++) {
356 if (dp
->di_db
[i
] != 0)
357 ret
|= searchdir(ino
, dp
->di_db
[i
],
358 (long)dblksize(sblock
, dp
, i
),
360 if (ret
& HASDUMPEDFILE
)
363 filesize
-= sblock
->fs_bsize
;
365 for (i
= 0; filesize
> 0 && i
< NIADDR
; i
++) {
366 if (dp
->di_ib
[i
] == 0)
368 ret
|= dirindir(ino
, dp
->di_ib
[i
], i
, &filesize
);
370 #endif /* __linux__ */
371 if (ret
& HASDUMPEDFILE
) {
372 SETINO(ino
, dumpinomap
);
373 *tapesize
+= blockest(dp
);
377 if ((ret
& HASSUBDIRS
) == 0) {
378 if (!TSTINO(ino
, dumpinomap
)) {
379 CLRINO(ino
, dumpdirmap
);
389 * Read indirect blocks, and pass the data blocks to be searched
390 * as directories. Quit as soon as any entry is found that will
391 * require the directory to be dumped.
394 dirindir(ino_t ino
, daddr_t blkno
, int ind_level
, long *filesize
)
398 daddr_t idblk
[MAXNINDIR
];
400 bread(fsbtodb(sblock
, blkno
), (char *)idblk
, (int)sblock
->fs_bsize
);
401 if (ind_level
<= 0) {
402 for (i
= 0; *filesize
> 0 && i
< NINDIR(sblock
); i
++) {
405 ret
|= searchdir(ino
, blkno
, sblock
->fs_bsize
,
407 if (ret
& HASDUMPEDFILE
)
410 *filesize
-= sblock
->fs_bsize
;
415 for (i
= 0; *filesize
> 0 && i
< NINDIR(sblock
); i
++) {
418 ret
|= dirindir(ino
, blkno
, ind_level
, filesize
);
422 #endif /* !__linux__ */
425 * Scan a disk block containing directory information looking to see if
426 * any of the entries are on the dump list and to see if the directory
427 * contains any subdirectories.
431 searchdir(struct ext2_dir_entry
*dp
, int offset
, int blocksize
, char *buf
, void *private)
433 int *ret
= (int *) private;
437 if (dp
->name
[0] == '.') {
438 if (dp
->name_len
== 1)
440 if (dp
->name
[1] == '.' && dp
->name_len
== 2)
443 if (TSTINO(dp
->inode
, dumpinomap
)) {
444 *ret
|= HASDUMPEDFILE
;
445 if (*ret
& HASSUBDIRS
)
448 if (TSTINO(dp
->inode
, dumpdirmap
)) {
450 if (*ret
& HASDUMPEDFILE
)
456 #else /* __linux__ */
459 searchdir(ino_t ino
, daddr_t blkno
, long size
, long filesize
)
461 register struct direct
*dp
;
462 register long loc
, ret
= 0;
465 bread(fsbtodb(sblock
, blkno
), dblk
, (int)size
);
468 for (loc
= 0; loc
< size
; ) {
469 dp
= (struct direct
*)(dblk
+ loc
);
470 if (dp
->d_reclen
== 0) {
471 msg("corrupted directory, inumber %d\n", ino
);
477 if (dp
->d_name
[0] == '.') {
478 if (dp
->d_name
[1] == '\0')
480 if (dp
->d_name
[1] == '.' && dp
->d_name
[2] == '\0')
483 if (TSTINO(dp
->d_ino
, dumpinomap
)) {
484 ret
|= HASDUMPEDFILE
;
485 if (ret
& HASSUBDIRS
)
488 if (TSTINO(dp
->d_ino
, dumpdirmap
)) {
490 if (ret
& HASDUMPEDFILE
)
496 #endif /* __linux__ */
500 struct block_context
{
509 * Dump a block to the tape
512 dumponeblock(ext2_filsys fs
, blk_t
*blocknr
, int blockcnt
, void *private)
514 struct block_context
*p
;
517 if (blockcnt
< NDADDR
)
519 p
= (struct block_context
*)private;
520 for (i
= p
->next_block
; i
< blockcnt
; i
++) {
521 p
->buf
[p
->cnt
++] = 0;
522 if (p
->cnt
== p
->max
) {
523 blksout (p
->buf
, p
->cnt
, p
->ino
);
527 p
->buf
[p
->cnt
++] = *blocknr
;
528 if (p
->cnt
== p
->max
) {
529 blksout (p
->buf
, p
->cnt
, p
->ino
);
532 p
->next_block
= blockcnt
+ 1;
538 * Dump passes 3 and 4.
540 * Dump the contents of an inode to tape.
543 dumpino(struct dinode
*dp
, ino_t ino
)
548 struct old_bsd_inode obi
;
550 struct block_context bc
;
557 dumpmap(dumpinomap
, TS_BITS
, ino
);
559 CLRINO(ino
, dumpinomap
);
561 memset(&obi
, 0, sizeof(obi
));
562 obi
.di_mode
= dp
->di_mode
;
563 obi
.di_uid
= dp
->di_uid
;
564 obi
.di_gid
= dp
->di_gid
;
565 obi
.di_qsize
.v
= (u_quad_t
)dp
->di_size
;
566 obi
.di_atime
= dp
->di_atime
;
567 obi
.di_mtime
= dp
->di_mtime
;
568 obi
.di_ctime
= dp
->di_ctime
;
569 obi
.di_nlink
= dp
->di_nlink
;
570 obi
.di_blocks
= dp
->di_blocks
;
571 obi
.di_flags
= dp
->di_flags
;
572 obi
.di_gen
= dp
->di_gen
;
573 memmove(&obi
.di_db
, &dp
->di_db
, (NDADDR
+ NIADDR
) * sizeof(daddr_t
));
574 if (dp
->di_file_acl
|| dp
->di_dir_acl
)
575 warn("ACLs in inode #%ld won't be dumped", ino
);
576 memmove(&spcl
.c_dinode
, &obi
, sizeof(obi
));
577 #else /* __linux__ */
579 #endif /* __linux__ */
580 spcl
.c_type
= TS_INODE
;
582 switch (dp
->di_mode
& S_IFMT
) {
592 msg("Warning: dumpino called on a directory (ino %d)\n", ino
);
598 * Check for short symbolic link.
601 if (dp
->di_size
> 0 &&
602 dp
->di_size
< EXT2_N_BLOCKS
* sizeof (daddr_t
)) {
606 memmove(buf
, dp
->di_db
, (u_long
)dp
->di_size
);
607 buf
[dp
->di_size
] = '\0';
611 #endif /* __linux__ */
613 if (dp
->di_size
> 0 &&
614 dp
->di_size
< sblock
->fs_maxsymlinklen
) {
618 memmove(buf
, dp
->di_shortlink
, (u_long
)dp
->di_size
);
619 buf
[dp
->di_size
] = '\0';
642 msg("Warning: undefined file type 0%o\n", dp
->di_mode
& IFMT
);
645 if (dp
->di_size
> NDADDR
* sblock
->fs_bsize
)
647 cnt
= NDADDR
* EXT2_FRAGS_PER_BLOCK(fs
->super
);
649 cnt
= NDADDR
* sblock
->fs_frag
;
652 cnt
= howmany(dp
->di_size
, sblock
->fs_fsize
);
653 blksout(&dp
->di_db
[0], cnt
, ino
);
654 if ((size
= dp
->di_size
- NDADDR
* sblock
->fs_bsize
) <= 0)
657 bc
.max
= NINDIR(sblock
) * EXT2_FRAGS_PER_BLOCK(fs
->super
);
658 bc
.buf
= (int *)malloc (bc
.max
* sizeof (int));
661 bc
.next_block
= NDADDR
;
663 ext2fs_block_iterate (fs
, ino
, 0, NULL
, dumponeblock
, (void *)&bc
);
665 blksout (bc
.buf
, bc
.cnt
, bc
.ino
);
668 for (ind_level
= 0; ind_level
< NIADDR
; ind_level
++) {
669 dmpindir(ino
, dp
->di_ib
[ind_level
], ind_level
, &size
);
678 struct convert_dir_context
{
686 * This function converts an ext2fs directory entry to the BSD format.
688 * Basically, it adds a null-character at the end of the name, recomputes the
689 * size of the entry, and creates it in a temporary buffer
692 convert_dir(struct ext2_dir_entry
*dirent
, int offset
, int blocksize
, char *buf
, void *private)
694 struct convert_dir_context
*p
;
698 p
= (struct convert_dir_context
*)private;
700 reclen
= EXT2_DIR_REC_LEN(dirent
->name_len
+ 1);
701 if (((p
->offset
+ reclen
- 1) / p
->bs
) != (p
->offset
/ p
->bs
)) {
702 dp
= (struct direct
*)(p
->buf
+ p
->prev_offset
);
703 dp
->d_reclen
+= p
->bs
- (p
->offset
% p
->bs
);
704 p
->offset
+= p
->bs
- (p
->offset
% p
->bs
);
707 dp
= (struct direct
*)(p
->buf
+ p
->offset
);
708 dp
->d_ino
= dirent
->inode
;
709 dp
->d_reclen
= reclen
;
711 dp
->d_namlen
= dirent
->name_len
;
712 strncpy(dp
->d_name
, dirent
->name
, dirent
->name_len
);
713 dp
->d_name
[dp
->d_namlen
] = '\0';
714 p
->prev_offset
= p
->offset
;
723 * Dumps a directory to tape after converting it to the BSD format
726 dumpdirino(struct dinode
*dp
, ino_t ino
)
730 struct old_bsd_inode obi
;
731 struct convert_dir_context cdc
;
733 struct ext2_dir_entry
*de
;
738 dumpmap(dumpinomap
, TS_BITS
, ino
);
740 CLRINO(ino
, dumpinomap
);
743 * Convert the directory to the BSD format
745 /* Allocate a buffer for the conversion (twice the size of the
746 ext2fs directory to avoid problems ;-) */
747 cdc
.buf
= (char *)malloc(dp
->di_size
* 2 * sizeof(char));
749 err(1, "Cannot allocate buffer to convert directory #%lu\n",
753 cdc
.bs
= MIN(DIRBLKSIZ
, TP_BSIZE
);
754 /* Do the conversion */
755 retval
= ext2fs_dir_iterate(fs
, ino
, 0, NULL
, convert_dir
, (void *)&cdc
);
757 com_err(disk
, retval
, "while converting directory #%ld\n", ino
);
760 /* Fix the last entry */
761 if ((cdc
.offset
% cdc
.bs
) != 0) {
762 de
= (struct ext2_dir_entry
*)(cdc
.buf
+ cdc
.prev_offset
);
763 de
->rec_len
+= cdc
.bs
- (cdc
.offset
% cdc
.bs
);
764 cdc
.offset
+= cdc
.bs
- (cdc
.offset
% cdc
.bs
);
767 dir_size
= cdc
.offset
;
770 memset(&obi
, 0, sizeof(obi
));
771 obi
.di_mode
= dp
->di_mode
;
772 obi
.di_uid
= dp
->di_uid
;
773 obi
.di_gid
= dp
->di_gid
;
774 obi
.di_qsize
.v
= dir_size
; /* (u_quad_t)dp->di_size; */
775 obi
.di_atime
= dp
->di_atime
;
776 obi
.di_mtime
= dp
->di_mtime
;
777 obi
.di_ctime
= dp
->di_ctime
;
778 obi
.di_nlink
= dp
->di_nlink
;
779 obi
.di_blocks
= dp
->di_blocks
;
780 obi
.di_flags
= dp
->di_flags
;
781 obi
.di_gen
= dp
->di_gen
;
782 memmove(&obi
.di_db
, &dp
->di_db
, (NDADDR
+ NIADDR
) * sizeof(daddr_t
));
783 if (dp
->di_file_acl
|| dp
->di_dir_acl
)
784 warn("ACLs in inode #%ld won't be dumped", ino
);
785 memmove(&spcl
.c_dinode
, &obi
, sizeof(obi
));
786 #else /* __linux__ */
788 #endif /* __linux__ */
789 spcl
.c_type
= TS_INODE
;
791 switch (dp
->di_mode
& S_IFMT
) {
802 msg("Warning: size of directory inode #%d is <= 0 (%d)!\n",
807 msg("Warning: dumpdirino called with file type 0%o (inode #%d)\n",
808 dp
->di_mode
& IFMT
, ino
);
811 for (size
= 0; size
< dir_size
; size
+= TP_BSIZE
) {
815 memmove(buf
, cdc
.buf
+ size
, TP_BSIZE
);
817 spcl
.c_type
= TS_ADDR
;
822 #endif /* __linux__ */
826 * Read indirect blocks, and pass the data blocks to be dumped.
829 dmpindir(ino_t ino
, daddr_t blk
, int ind_level
, fsizeT
*size
)
836 daddr_t idblk
[MAXNINDIR
];
839 bread(fsbtodb(sblock
, blk
), (char *)idblk
, (int) sblock
->fs_bsize
);
842 * My RedHat 4.0 system doesn't have these flags; I haven't
843 * upgraded e2fsprogs yet
845 #if defined(EXT2_FLAG_SWAP_BYTES)
846 if ((fs
->flags
& EXT2_FLAG_SWAP_BYTES
) ||
847 (fs
->flags
& EXT2_FLAG_SWAP_BYTES_READ
)) {
849 max
= sblock
->fs_bsize
>> 2;
850 swapme
= (blk_t
*) idblk
;
851 for (i
= 0; i
< max
; i
++, swapme
++)
852 *swapme
= swab32(*swapme
);
853 #if defined(EXT2_FLAG_SWAP_BYTES)
858 memset(idblk
, 0, (int)sblock
->fs_bsize
);
859 if (ind_level
<= 0) {
860 if (*size
< NINDIR(sblock
) * sblock
->fs_bsize
)
861 cnt
= howmany(*size
, sblock
->fs_fsize
);
864 cnt
= NINDIR(sblock
) * EXT2_FRAGS_PER_BLOCK(fs
->super
);
866 cnt
= NINDIR(sblock
) * sblock
->fs_frag
;
868 *size
-= NINDIR(sblock
) * sblock
->fs_bsize
;
869 blksout(&idblk
[0], cnt
, ino
);
873 for (i
= 0; i
< NINDIR(sblock
); i
++) {
874 dmpindir(ino
, idblk
[i
], ind_level
, size
);
882 * Collect up the data into tape record sized buffers and output them.
885 blksout(daddr_t
*blkp
, int frags
, ino_t ino
)
887 register daddr_t
*bp
;
888 int i
, j
, count
, blks
, tbperdb
;
890 blks
= howmany(frags
* sblock
->fs_fsize
, TP_BSIZE
);
891 tbperdb
= sblock
->fs_bsize
>> tp_bshift
;
892 for (i
= 0; i
< blks
; i
+= TP_NINDIR
) {
893 if (i
+ TP_NINDIR
> blks
)
896 count
= i
+ TP_NINDIR
;
897 for (j
= i
; j
< count
; j
++)
898 if (blkp
[j
/ tbperdb
] != 0)
899 spcl
.c_addr
[j
- i
] = 1;
901 spcl
.c_addr
[j
- i
] = 0;
902 spcl
.c_count
= count
- i
;
904 bp
= &blkp
[i
/ tbperdb
];
905 for (j
= i
; j
< count
; j
+= tbperdb
, bp
++) {
907 if (j
+ tbperdb
<= count
)
908 dumpblock(*bp
, (int)sblock
->fs_bsize
);
910 dumpblock(*bp
, (count
- j
) * TP_BSIZE
);
913 spcl
.c_type
= TS_ADDR
;
918 * Dump a map to the tape.
921 dumpmap(char *map
, int type
, ino_t ino
)
927 spcl
.c_count
= howmany(mapsize
* sizeof(char), TP_BSIZE
);
929 for (i
= 0, cp
= map
; i
< spcl
.c_count
; i
++, cp
+= TP_BSIZE
)
934 * Write a header record to the dump tape.
937 writeheader(ino_t ino
)
940 register __s32 sum
, cnt
, *lp
;
942 register int32_t sum
, cnt
, *lp
;
945 spcl
.c_inumber
= ino
;
946 spcl
.c_magic
= NFS_MAGIC
;
951 lp
= (int32_t *)&spcl
;
955 cnt
= sizeof(union u_spcl
) / (4 * sizeof(__s32
));
957 cnt
= sizeof(union u_spcl
) / (4 * sizeof(int32_t));
965 spcl
.c_checksum
= CHECKSUM
- sum
;
966 writerec((char *)&spcl
, 1);
973 static struct dinode dinode
;
976 ext2fs_read_inode(fs
, inum
, (struct ext2_inode
*) &dinode
);
979 #else /* __linux__ */
983 static daddr_t minino
, maxino
;
984 static struct dinode inoblock
[MAXINOPB
];
987 if (inum
>= minino
&& inum
< maxino
)
988 return (&inoblock
[inum
- minino
]);
989 bread(fsbtodb(sblock
, ino_to_fsba(sblock
, inum
)), (char *)inoblock
,
990 (int)sblock
->fs_bsize
);
991 minino
= inum
- (inum
% INOPB(sblock
));
992 maxino
= minino
+ INOPB(sblock
);
993 return (&inoblock
[inum
- minino
]);
995 #endif /* __linux__ */
998 * Read a chunk of data from the disk.
999 * Try to recover from hard errors by reading in sector sized pieces.
1000 * Error recovery is attempted at most BREADEMAX times before seeking
1001 * consent from the operator to continue.
1003 int breaderrors
= 0;
1004 #define BREADEMAX 32
1007 bread(daddr_t blkno
, char *buf
, int size
)
1014 if (llseek(diskfd
, ((ext2_loff_t
)blkno
<< dev_bshift
), 0) !=
1015 ((ext2_loff_t
)blkno
<< dev_bshift
))
1017 if (lseek(diskfd
, ((off_t
)blkno
<< dev_bshift
), 0) !=
1018 ((off_t
)blkno
<< dev_bshift
))
1020 msg("bread: lseek fails\n");
1021 if ((cnt
= read(diskfd
, buf
, size
)) == size
)
1023 if (blkno
+ (size
/ dev_bsize
) > fsbtodb(sblock
, sblock
->fs_size
)) {
1025 * Trying to read the final fragment.
1027 * NB - dump only works in TP_BSIZE blocks, hence
1028 * rounds `dev_bsize' fragments up to TP_BSIZE pieces.
1029 * It should be smarter about not actually trying to
1030 * read more than it can get, but for the time being
1031 * we punt and scale back the read only when it gets
1032 * us into trouble. (mkm 9/25/83)
1038 msg("read error from %s: %s: [block %d]: count=%d\n",
1039 disk
, strerror(errno
), blkno
, size
);
1041 msg("short read error from %s: [block %d]: count=%d, got=%d\n",
1042 disk
, blkno
, size
, cnt
);
1043 if (++breaderrors
> BREADEMAX
) {
1044 msg("More than %d block read errors from %d\n",
1046 broadcast("DUMP IS AILING!\n");
1047 msg("This is an unrecoverable error.\n");
1048 if (!query("Do you want to attempt to continue?")){
1055 * Zero buffer, then try to read each sector of buffer separately.
1057 memset(buf
, 0, size
);
1058 for (i
= 0; i
< size
; i
+= dev_bsize
, buf
+= dev_bsize
, blkno
++) {
1060 if (llseek(diskfd
, ((ext2_loff_t
)blkno
<< dev_bshift
), 0) !=
1061 ((ext2_loff_t
)blkno
<< dev_bshift
))
1063 if (lseek(diskfd
, ((off_t
)blkno
<< dev_bshift
), 0) !=
1064 ((off_t
)blkno
<< dev_bshift
))
1066 msg("bread: lseek2 fails!\n");
1067 if ((cnt
= read(diskfd
, buf
, (int)dev_bsize
)) == dev_bsize
)
1070 msg("read error from %s: %s: [sector %d]: count=%d\n",
1071 disk
, strerror(errno
), blkno
, dev_bsize
);
1074 msg("short read error from %s: [sector %d]: count=%d, got=%d\n",
1075 disk
, blkno
, dev_bsize
, cnt
);