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@noos.fr>, 1999-2000
6 * Stelian Pop <pop@noos.fr> - AlcĂ´ve <www.alcove.fr>, 2000
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
43 static const char rcsid
[] =
44 "$Id: traverse.c,v 1.36 2001/07/19 09:03:44 stelian Exp $";
55 #include <sys/param.h>
58 #ifdef HAVE_EXT2FS_EXT2_FS_H
59 #include <ext2fs/ext2_fs.h>
61 #include <linux/ext2_fs.h>
63 #include <ext2fs/ext2fs.h>
64 #include <bsdcompat.h>
65 #include <compaterr.h>
68 #include <sys/vnode.h>
71 #include <ufs/fsdir.h>
72 #include <ufs/inode.h>
74 #include <ufs/ufs/dir.h>
75 #include <ufs/ufs/dinode.h>
76 #include <ufs/ffs/fs.h>
77 #endif /* __linux__ */
79 #include <protocols/dumprestore.h>
83 #define HASDUMPEDFILE 0x1
84 #define HASSUBDIRS 0x2
87 typedef u_quad_t fsizeT
;
90 typedef quad_t fsizeT
;
97 static int searchdir
__P((struct ext2_dir_entry
*dp
, int offset
,
98 int blocksize
, char *buf
, void *private));
100 static int dirindir
__P((dump_ino_t ino
, daddr_t blkno
, int level
, long *size
));
101 static void dmpindir
__P((dump_ino_t ino
, daddr_t blk
, int level
, fsizeT
*size
));
102 static int searchdir
__P((dump_ino_t ino
, daddr_t blkno
, long size
, long filesize
));
104 static void mapfileino
__P((dump_ino_t ino
, struct dinode
const *dp
, long *tapesize
, int *dirskipped
));
106 #ifdef HAVE_EXT2_JOURNAL_INUM
107 #define ext2_journal_ino(sb) (sb->s_journal_inum)
109 #define ext2_journal_ino(sb) (*((__u32 *)sb + 0x38))
111 #ifndef HAVE_EXT2_INO_T
112 typedef ino_t ext2_ino_t
;
115 #ifndef EXT3_FEATURE_COMPAT_HAS_JOURNAL
116 #define EXT3_FEATURE_COMPAT_HAS_JOURNAL 0x0004
118 #ifndef EXT2_FEATURE_INCOMPAT_FILETYPE
119 #define EXT2_FEATURE_INCOMPAT_FILETYPE 0x0002
121 #ifndef EXT3_FEATURE_INCOMPAT_RECOVER
122 #define EXT3_FEATURE_INCOMPAT_RECOVER 0x0004
124 #ifndef EXT3_FEATURE_INCOMPAT_JOURNAL_DEV
125 #define EXT3_FEATURE_INCOMPAT_JOURNAL_DEV 0x0008
128 #ifndef EXT2_LIB_FEATURE_INCOMPAT_SUPP
129 #define EXT2_LIB_FEATURE_INCOMPAT_SUPP (EXT3_FEATURE_INCOMPAT_RECOVER | \
130 EXT2_FEATURE_INCOMPAT_FILETYPE)
132 #ifndef EXT2_RESIZE_INO
133 #define EXT2_RESIZE_INO 7
136 int dump_fs_open(const char *disk
, ext2_filsys
*fs
)
140 retval
= ext2fs_open(disk
, EXT2_FLAG_FORCE
, 0, 0, unix_io_manager
, fs
);
142 struct ext2_super_block
*es
= (*fs
)->super
;
143 dump_ino_t journal_ino
= ext2_journal_ino(es
);
145 if (es
->s_feature_incompat
& EXT3_FEATURE_INCOMPAT_JOURNAL_DEV
){
146 fprintf(stderr
, "This an journal, not a filesystem!\n");
147 retval
= EXT2_ET_UNSUPP_FEATURE
;
150 else if ((retval
= es
->s_feature_incompat
&
151 ~(EXT2_LIB_FEATURE_INCOMPAT_SUPP
|
152 EXT3_FEATURE_INCOMPAT_RECOVER
))) {
154 "Unsupported feature(s) 0x%x in filesystem\n",
156 retval
= EXT2_ET_UNSUPP_FEATURE
;
160 if (es
->s_feature_compat
&
161 EXT3_FEATURE_COMPAT_HAS_JOURNAL
&&
163 msg("Exclude journal inode %u\n",
165 do_exclude_ino(journal_ino
);
167 do_exclude_ino(EXT2_RESIZE_INO
);
174 * This is an estimation of the number of TP_BSIZE blocks in the file.
175 * It estimates the number of blocks in files with holes by assuming
176 * that all of the blocks accounted for by di_blocks are data blocks
177 * (when some of the blocks are usually used for indirect pointers);
178 * hence the estimate may be high.
181 blockest(struct dinode
const *dp
)
183 long blkest
, sizeest
;
187 * dp->di_size is the size of the file in bytes.
188 * dp->di_blocks stores the number of sectors actually in the file.
189 * If there are more sectors than the size would indicate, this just
190 * means that there are indirect blocks in the file or unused
191 * sectors in the last file block; we can safely ignore these
192 * (blkest = sizeest below).
193 * If the file is bigger than the number of sectors would indicate,
194 * then the file has holes in it. In this case we must use the
195 * block count to estimate the number of data blocks used, but
196 * we use the actual size for estimating the number of indirect
197 * dump blocks (sizeest vs. blkest in the indirect block
200 blkest
= howmany((u_quad_t
)dp
->di_blocks
*fs
->blocksize
, TP_BSIZE
);
201 i_size
= dp
->di_size
+ ((u_quad_t
) dp
->di_size_high
<< 32);
202 sizeest
= howmany(i_size
, TP_BSIZE
);
203 if (blkest
> sizeest
)
206 if (i_size
> fs
->blocksize
* NDADDR
) {
207 /* calculate the number of indirect blocks on the dump tape */
209 howmany(sizeest
- NDADDR
* fs
->blocksize
/ TP_BSIZE
,
210 NINDIR(sblock
) * EXT2_FRAGS_PER_BLOCK(fs
->super
));
213 if (i_size
> sblock
->fs_bsize
* NDADDR
) {
214 /* calculate the number of indirect blocks on the dump tape */
216 howmany(sizeest
- NDADDR
* sblock
->fs_bsize
/ TP_BSIZE
,
223 /* Auxiliary macro to pick up files changed since previous dump. */
224 #define CHANGEDSINCE(dp, t) \
225 ((dp)->di_mtime >= (t) || (dp)->di_ctime >= (t))
227 /* The NODUMP_FLAG macro tests if a file has the nodump flag. */
229 #define NODUMP_FLAG(dp) (!nonodump && (((dp)->di_flags & UF_NODUMP) == UF_NODUMP))
231 #define NODUMP_FLAG(dp) 0
234 /* The WANTTODUMP macro decides whether a file should be dumped. */
235 #define WANTTODUMP(dp, ino) \
236 (CHANGEDSINCE(dp, spcl.c_ddate) && \
237 (!NODUMP_FLAG(dp)) && \
241 * Determine if given inode should be dumped. "dp" must either point to a
242 * copy of the given inode, or be NULL (in which case it is fetched.)
245 mapfileino(dump_ino_t ino
, struct dinode
const *dp
, long *tapesize
, int *dirskipped
)
250 * Skip inode if we've already marked it for dumping
252 if (TSTINO(ino
, usedinomap
))
256 if ((mode
= (dp
->di_mode
& IFMT
)) == 0)
259 if (dp
->di_nlink
== 0 || dp
->di_dtime
!= 0)
263 * Put all dirs in dumpdirmap, inodes that are to be dumped in the
264 * used map. All inode but dirs who have the nodump attribute go
267 SETINO(ino
, usedinomap
);
270 SETINO(ino
, dumpdirmap
);
271 if (WANTTODUMP(dp
, ino
)) {
272 SETINO(ino
, dumpinomap
);
273 if (mode
!= IFREG
&& mode
!= IFDIR
&& mode
!= IFLNK
)
276 *tapesize
+= blockest(dp
);
280 if ( NODUMP_FLAG(dp
) || exclude_ino(ino
) )
281 CLRINO(ino
, usedinomap
);
289 * Walk the inode list for a filesystem to find all allocated inodes
290 * that have been modified since the previous dump time. Also, find all
291 * the directories in the filesystem.
295 mapfiles(dump_ino_t maxino
, long *tapesize
)
298 int anydirskipped
= 0;
299 ext2_inode_scan scan
;
301 struct ext2_inode inode
;
304 * We use libext2fs's inode scanning routines, which are particularly
305 * robust. (Note that getino cannot return an error.)
307 err
= ext2fs_open_inode_scan(fs
, 0, &scan
);
309 com_err(disk
, err
, "while opening inodes\n");
313 err
= ext2fs_get_next_inode(scan
, &ino
, &inode
);
314 if (err
== EXT2_ET_BAD_BLOCK_IN_INODE_TABLE
)
317 com_err(disk
, err
, "while scanning inode #%ld\n",
325 mapfileino(ino
, (struct dinode
const *)&inode
, tapesize
,
328 ext2fs_close_inode_scan(scan
);
331 * Restore gets very upset if the root is not dumped,
332 * so ensure that it always is dumped.
334 SETINO(ROOTINO
, dumpinomap
);
335 return (anydirskipped
);
339 mapfiles(dump_ino_t maxino
, long *tapesize
)
341 register dump_ino_t ino
;
342 int anydirskipped
= 0;
344 for (ino
= ROOTINO
; ino
< maxino
; ino
++)
345 mapfileino(ino
, tapesize
, &anydirskipped
);
348 * Restore gets very upset if the root is not dumped,
349 * so ensure that it always is dumped.
351 SETINO(ROOTINO
, dumpinomap
);
352 return (anydirskipped
);
354 #endif /* __linux__ */
358 maponefile(dump_ino_t maxino
, long *tapesize
, char *directory
)
362 char dir_name
[MAXPATHLEN
];
363 int i
, anydirskipped
= 0;
366 * Mark every directory in the path as being dumped
368 for (i
= 0; i
< strlen (directory
); i
++) {
369 if (directory
[i
] == '/') {
370 strncpy (dir_name
, directory
, i
);
372 retval
= ext2fs_namei(fs
, ROOTINO
, ROOTINO
,
375 com_err(disk
, retval
,
376 "while translating %s", dir_name
);
379 mapfileino((dump_ino_t
) dir_ino
, 0,
380 tapesize
, &anydirskipped
);
384 * Mark the final directory
386 retval
= ext2fs_namei(fs
, ROOTINO
, ROOTINO
, directory
, &dir_ino
);
388 com_err(disk
, retval
, "while translating %s", directory
);
391 mapfileino((dump_ino_t
)dir_ino
, 0, tapesize
, &anydirskipped
);
393 mapfileino(ROOTINO
, 0, tapesize
, &anydirskipped
);
396 * Restore gets very upset if the root is not dumped,
397 * so ensure that it always is dumped.
399 SETINO(ROOTINO
, dumpdirmap
);
400 return anydirskipped
;
402 #endif /* __linux__ */
405 struct mapfile_context
{
411 mapfilesindir(struct ext2_dir_entry
*dirent
, int offset
, int blocksize
, char *buf
, void *private)
413 register struct dinode
const *dp
;
416 struct mapfile_context
*mfc
;
420 mfc
= (struct mapfile_context
*)private;
421 dp
= getino(dirent
->inode
);
423 mapfileino(dirent
->inode
, dp
, mfc
->tapesize
, mfc
->anydirskipped
);
425 mode
= dp
->di_mode
& IFMT
;
426 if (mode
== IFDIR
&& dp
->di_nlink
!= 0 && dp
->di_dtime
== 0) {
427 if ((dirent
->name
[0] != '.' || ( dirent
->name_len
& 0xFF ) != 1) &&
428 (dirent
->name
[0] != '.' || dirent
->name
[1] != '.' ||
429 ( dirent
->name_len
& 0xFF ) != 2)) {
430 retval
= ext2fs_dir_iterate(fs
, ino
, 0, NULL
,
431 mapfilesindir
, private);
442 * Walk the inode list for a filesystem to find all allocated inodes
443 * that have been modified since the previous dump time. Also, find all
444 * the directories in the filesystem.
447 mapfilesfromdir(dump_ino_t maxino
, long *tapesize
, char *directory
)
450 struct mapfile_context mfc
;
452 char dir_name
[MAXPATHLEN
];
453 int i
, anydirskipped
= 0;
456 * Mark every directory in the path as being dumped
458 for (i
= 0; i
< strlen (directory
); i
++) {
459 if (directory
[i
] == '/') {
460 strncpy (dir_name
, directory
, i
);
462 retval
= ext2fs_namei(fs
, ROOTINO
, ROOTINO
, dir_name
,
465 com_err(disk
, retval
, "while translating %s",
469 mapfileino(dir_ino
, 0, tapesize
, &anydirskipped
);
473 * Mark the final directory
475 retval
= ext2fs_namei(fs
, ROOTINO
, ROOTINO
, directory
, &dir_ino
);
477 com_err(disk
, retval
, "while translating %s", directory
);
480 mapfileino(dir_ino
, 0, tapesize
, &anydirskipped
);
482 mfc
.tapesize
= tapesize
;
483 mfc
.anydirskipped
= &anydirskipped
;
484 retval
= ext2fs_dir_iterate(fs
, dir_ino
, 0, NULL
, mapfilesindir
,
488 com_err(disk
, retval
, "while mapping files in %s", directory
);
492 * Ensure that the root inode actually appears in the file list
495 mapfileino(ROOTINO
, 0, tapesize
, &anydirskipped
);
497 * Restore gets very upset if the root is not dumped,
498 * so ensure that it always is dumped.
500 SETINO(ROOTINO
, dumpinomap
);
501 return anydirskipped
;
506 struct mapdirs_context
{
516 * Scan each directory on the filesystem to see if it has any modified
517 * files in it. If it does, and has not already been added to the dump
518 * list (because it was itself modified), then add it. If a directory
519 * has not been modified itself, contains no modified files and has no
520 * subdirectories, then it can be deleted from the dump list and from
521 * the list of directories. By deleting it from the list of directories,
522 * its parent may now qualify for the same treatment on this or a later
523 * pass using this algorithm.
526 mapdirs(dump_ino_t maxino
, long *tapesize
)
528 register struct dinode
*dp
;
531 register dump_ino_t ino
;
536 struct mapdirs_context mdc
;
538 int ret
, change
= 0, nodump
;
540 isdir
= 0; /* XXX just to get gcc to shut up */
541 for (map
= dumpdirmap
, ino
= 1; ino
< maxino
; ino
++) {
542 if (((ino
- 1) % NBBY
) == 0) /* map is offset by 1 */
547 * If dir has been removed from the used map, it's either
548 * because it had the nodump flag, or it herited it from
549 * its parent. A directory can't be in dumpinomap if not
550 * in usedinomap, but we have to go through it anyway
551 * to propagate the nodump attribute.
553 nodump
= (TSTINO(ino
, usedinomap
) == 0);
554 if ((isdir
& 1) == 0 ||
555 (TSTINO(ino
, dumpinomap
) && nodump
== 0))
562 mdc
.tapesize
= tapesize
;
563 ext2fs_dir_iterate(fs
, ino
, 0, NULL
, searchdir
, (void *) &mdc
);
564 #else /* __linux__ */
565 filesize
= dp
->di_size
;
566 for (ret
= 0, i
= 0; filesize
> 0 && i
< NDADDR
; i
++) {
567 if (dp
->di_db
[i
] != 0)
568 ret
|= searchdir(ino
, dp
->di_db
[i
],
569 (long)dblksize(sblock
, dp
, i
),
571 if (ret
& HASDUMPEDFILE
)
574 filesize
-= sblock
->fs_bsize
;
576 for (i
= 0; filesize
> 0 && i
< NIADDR
; i
++) {
577 if (dp
->di_ib
[i
] == 0)
579 ret
|= dirindir(ino
, dp
->di_ib
[i
], i
, &filesize
);
581 #endif /* __linux__ */
582 if (ret
& HASDUMPEDFILE
) {
583 SETINO(ino
, dumpinomap
);
584 *tapesize
+= blockest(dp
);
589 if (ret
& HASSUBDIRS
)
590 change
= 1; /* subdirs have inherited nodump */
591 CLRINO(ino
, dumpdirmap
);
592 } else if ((ret
& HASSUBDIRS
) == 0) {
593 if (!TSTINO(ino
, dumpinomap
)) {
594 CLRINO(ino
, dumpdirmap
);
604 * Read indirect blocks, and pass the data blocks to be searched
605 * as directories. Quit as soon as any entry is found that will
606 * require the directory to be dumped.
609 dirindir(dump_ino_t ino
, daddr_t blkno
, int ind_level
, long *filesize
)
613 daddr_t idblk
[MAXNINDIR
];
615 bread(fsbtodb(sblock
, blkno
), (char *)idblk
, (int)sblock
->fs_bsize
);
616 if (ind_level
<= 0) {
617 for (i
= 0; *filesize
> 0 && i
< NINDIR(sblock
); i
++) {
620 ret
|= searchdir(ino
, blkno
, sblock
->fs_bsize
,
622 if (ret
& HASDUMPEDFILE
)
625 *filesize
-= sblock
->fs_bsize
;
630 for (i
= 0; *filesize
> 0 && i
< NINDIR(sblock
); i
++) {
633 ret
|= dirindir(ino
, blkno
, ind_level
, filesize
);
637 #endif /* !__linux__ */
640 * Scan a disk block containing directory information looking to see if
641 * any of the entries are on the dump list and to see if the directory
642 * contains any subdirectories.
646 searchdir(struct ext2_dir_entry
*dp
, int offset
, int blocksize
, char *buf
, void *private)
648 struct mapdirs_context
*mdc
;
653 mdc
= (struct mapdirs_context
*)private;
655 tapesize
= mdc
->tapesize
;
659 if (dp
->name
[0] == '.') {
660 if (( dp
->name_len
& 0xFF ) == 1)
662 if (dp
->name
[1] == '.' && ( dp
->name_len
& 0xFF ) == 2)
666 ip
= getino(dp
->inode
);
667 if (TSTINO(dp
->inode
, dumpinomap
)) {
668 CLRINO(dp
->inode
, dumpinomap
);
669 CLRINO(dp
->inode
, usedinomap
);
670 *tapesize
-= blockest(ip
);
672 /* Add dir back to the dir map, to propagate nodump */
673 if ((ip
->di_mode
& IFMT
) == IFDIR
) {
674 SETINO(dp
->inode
, dumpdirmap
);
678 if (TSTINO(dp
->inode
, dumpinomap
)) {
679 *ret
|= HASDUMPEDFILE
;
680 if (*ret
& HASSUBDIRS
)
683 if (TSTINO(dp
->inode
, dumpdirmap
)) {
685 if (*ret
& HASDUMPEDFILE
)
692 #else /* __linux__ */
695 searchdir(dump_ino_t ino
, daddr_t blkno
, long size
, long filesize
)
697 register struct direct
*dp
;
698 register long loc
, ret
= 0;
701 bread(fsbtodb(sblock
, blkno
), dblk
, (int)size
);
704 for (loc
= 0; loc
< size
; ) {
705 dp
= (struct direct
*)(dblk
+ loc
);
706 if (dp
->d_reclen
== 0) {
707 msg("corrupted directory, inumber %d\n", ino
);
713 if (dp
->d_name
[0] == '.') {
714 if (dp
->d_name
[1] == '\0')
716 if (dp
->d_name
[1] == '.' && dp
->d_name
[2] == '\0')
719 if (TSTINO(dp
->d_ino
, dumpinomap
)) {
720 ret
|= HASDUMPEDFILE
;
721 if (ret
& HASSUBDIRS
)
724 if (TSTINO(dp
->d_ino
, dumpdirmap
)) {
726 if (ret
& HASDUMPEDFILE
)
732 #endif /* __linux__ */
736 struct block_context
{
745 * Dump a block to the tape
748 dumponeblock(ext2_filsys fs
, blk_t
*blocknr
, e2_blkcnt_t blockcnt
,
749 blk_t ref_block
, int ref_offset
, void * private)
751 struct block_context
*p
;
754 if (blockcnt
< NDADDR
)
756 p
= (struct block_context
*)private;
757 for (i
= p
->next_block
; i
< blockcnt
; i
++) {
758 p
->buf
[p
->cnt
++] = 0;
759 if (p
->cnt
== p
->max
) {
760 blksout (p
->buf
, p
->cnt
, p
->ino
);
764 p
->buf
[p
->cnt
++] = *blocknr
;
765 if (p
->cnt
== p
->max
) {
766 blksout (p
->buf
, p
->cnt
, p
->ino
);
769 p
->next_block
= blockcnt
+ 1;
775 * Dump passes 3 and 4.
777 * Dump the contents of an inode to tape.
780 dumpino(struct dinode
*dp
, dump_ino_t ino
)
783 fsizeT size
, remaining
;
785 struct old_bsd_inode obi
;
788 struct block_context bc
;
792 u_quad_t i_size
= dp
->di_size
+ ((u_quad_t
) dp
->di_size_high
<< 32);
796 dumpmap(dumpinomap
, TS_BITS
, ino
);
798 CLRINO(ino
, dumpinomap
);
800 memset(&obi
, 0, sizeof(obi
));
801 obi
.di_mode
= dp
->di_mode
;
802 obi
.di_uid
= dp
->di_uid
;
803 obi
.di_gid
= dp
->di_gid
;
804 obi
.di_qsize
.v
= i_size
;
805 obi
.di_atime
= dp
->di_atime
;
806 obi
.di_mtime
= dp
->di_mtime
;
807 obi
.di_ctime
= dp
->di_ctime
;
808 obi
.di_nlink
= dp
->di_nlink
;
809 obi
.di_blocks
= dp
->di_blocks
;
810 obi
.di_flags
= dp
->di_flags
;
811 obi
.di_gen
= dp
->di_gen
;
812 memmove(&obi
.di_db
, &dp
->di_db
, (NDADDR
+ NIADDR
) * sizeof(daddr_t
));
814 warn("ACLs in inode #%ld won't be dumped", (long)ino
);
815 memmove(&spcl
.c_dinode
, &obi
, sizeof(obi
));
816 #else /* __linux__ */
818 #endif /* __linux__ */
819 spcl
.c_type
= TS_INODE
;
821 switch (dp
->di_mode
& S_IFMT
) {
831 msg("Warning: dumpino called on a directory (ino %d)\n", ino
);
837 * Check for short symbolic link.
841 i_size
< EXT2_N_BLOCKS
* sizeof (daddr_t
)) {
845 memmove(buf
, dp
->di_db
, (u_long
)dp
->di_size
);
846 buf
[dp
->di_size
] = '\0';
850 #endif /* __linux__ */
852 if (dp
->di_size
> 0 &&
853 dp
->di_size
< sblock
->fs_maxsymlinklen
) {
857 memmove(buf
, dp
->di_shortlink
, (u_long
)dp
->di_size
);
858 buf
[dp
->di_size
] = '\0';
881 msg("Warning: undefined file type 0%o\n", dp
->di_mode
& IFMT
);
884 if (i_size
> NDADDR
* sblock
->fs_bsize
)
886 cnt
= NDADDR
* EXT2_FRAGS_PER_BLOCK(fs
->super
);
888 cnt
= NDADDR
* sblock
->fs_frag
;
891 cnt
= howmany(i_size
, sblock
->fs_fsize
);
892 blksout(&dp
->di_db
[0], cnt
, ino
);
893 if ((quad_t
) (size
= i_size
- NDADDR
* sblock
->fs_bsize
) <= 0)
896 bc
.max
= NINDIR(sblock
) * EXT2_FRAGS_PER_BLOCK(fs
->super
);
897 bc
.buf
= (int *)malloc (bc
.max
* sizeof (int));
900 bc
.next_block
= NDADDR
;
902 ext2fs_block_iterate2(fs
, (ext2_ino_t
)ino
, 0, NULL
, dumponeblock
, (void *)&bc
);
903 /* deal with holes at the end of the inode */
904 if (i_size
> bc
.next_block
*sblock
->fs_fsize
) {
905 remaining
= i_size
- bc
.next_block
*sblock
->fs_fsize
;
906 for (i
= 0; i
< howmany(remaining
, sblock
->fs_fsize
); i
++) {
907 bc
.buf
[bc
.cnt
++] = 0;
908 if (bc
.cnt
== bc
.max
) {
909 blksout (bc
.buf
, bc
.cnt
, bc
.ino
);
915 blksout (bc
.buf
, bc
.cnt
, bc
.ino
);
919 for (ind_level
= 0; ind_level
< NIADDR
; ind_level
++) {
920 dmpindir(ino
, dp
->di_ib
[ind_level
], ind_level
, &size
);
929 struct convert_dir_context
{
937 * This function converts an ext2fs directory entry to the BSD format.
939 * Basically, it adds a null-character at the end of the name, recomputes the
940 * size of the entry, and creates it in a temporary buffer
943 convert_dir(struct ext2_dir_entry
*dirent
, int offset
, int blocksize
, char *buf
, void *private)
945 struct convert_dir_context
*p
;
946 struct olddirect
*dp
;
949 p
= (struct convert_dir_context
*)private;
951 reclen
= EXT2_DIR_REC_LEN((dirent
->name_len
& 0xFF) + 1);
952 if (((p
->offset
+ reclen
- 1) / p
->bs
) != (p
->offset
/ p
->bs
)) {
953 dp
= (struct olddirect
*)(p
->buf
+ p
->prev_offset
);
954 dp
->d_reclen
+= p
->bs
- (p
->offset
% p
->bs
);
955 p
->offset
+= p
->bs
- (p
->offset
% p
->bs
);
958 dp
= (struct olddirect
*)(p
->buf
+ p
->offset
);
959 dp
->d_ino
= dirent
->inode
;
960 dp
->d_reclen
= reclen
;
961 dp
->d_namlen
= dirent
->name_len
& 0xFF;
962 strncpy(dp
->d_name
, dirent
->name
, dp
->d_namlen
);
963 dp
->d_name
[dp
->d_namlen
] = '\0';
964 p
->prev_offset
= p
->offset
;
973 * Dumps a directory to tape after converting it to the BSD format
976 dumpdirino(struct dinode
*dp
, dump_ino_t ino
)
980 struct old_bsd_inode obi
;
981 struct convert_dir_context cdc
;
983 struct ext2_dir_entry
*de
;
988 dumpmap(dumpinomap
, TS_BITS
, ino
);
990 CLRINO(ino
, dumpinomap
);
993 * Convert the directory to the BSD format
995 /* Allocate a buffer for the conversion (twice the size of the
996 ext2fs directory to avoid problems ;-) */
997 cdc
.buf
= (char *)malloc(dp
->di_size
* 2 * sizeof(char));
999 err(1, "Cannot allocate buffer to convert directory #%lu\n",
1000 (unsigned long)ino
);
1002 cdc
.prev_offset
= 0;
1003 cdc
.bs
= MIN(DIRBLKSIZ
, TP_BSIZE
);
1004 /* Do the conversion */
1005 retval
= ext2fs_dir_iterate(fs
, (ext2_ino_t
)ino
, 0, NULL
, convert_dir
, (void *)&cdc
);
1007 com_err(disk
, retval
, "while converting directory #%ld\n", (long)ino
);
1010 /* Fix the last entry */
1011 if ((cdc
.offset
% cdc
.bs
) != 0) {
1012 de
= (struct ext2_dir_entry
*)(cdc
.buf
+ cdc
.prev_offset
);
1013 de
->rec_len
+= cdc
.bs
- (cdc
.offset
% cdc
.bs
);
1014 cdc
.offset
+= cdc
.bs
- (cdc
.offset
% cdc
.bs
);
1017 dir_size
= cdc
.offset
;
1020 memset(&obi
, 0, sizeof(obi
));
1021 obi
.di_mode
= dp
->di_mode
;
1022 obi
.di_uid
= dp
->di_uid
;
1023 obi
.di_gid
= dp
->di_gid
;
1024 obi
.di_qsize
.v
= dir_size
; /* (u_quad_t)dp->di_size; */
1025 obi
.di_atime
= dp
->di_atime
;
1026 obi
.di_mtime
= dp
->di_mtime
;
1027 obi
.di_ctime
= dp
->di_ctime
;
1028 obi
.di_nlink
= dp
->di_nlink
;
1029 obi
.di_blocks
= dp
->di_blocks
;
1030 obi
.di_flags
= dp
->di_flags
;
1031 obi
.di_gen
= dp
->di_gen
;
1032 memmove(&obi
.di_db
, &dp
->di_db
, (NDADDR
+ NIADDR
) * sizeof(daddr_t
));
1033 if (dp
->di_file_acl
)
1034 warn("ACLs in inode #%ld won't be dumped", (long)ino
);
1035 memmove(&spcl
.c_dinode
, &obi
, sizeof(obi
));
1036 #else /* __linux__ */
1037 spcl
.c_dinode
= *dp
;
1038 #endif /* __linux__ */
1039 spcl
.c_type
= TS_INODE
;
1041 switch (dp
->di_mode
& S_IFMT
) {
1052 msg("Warning: size of directory inode #%d is <= 0 (%d)!\n",
1057 msg("Warning: dumpdirino called with file type 0%o (inode #%d)\n",
1058 dp
->di_mode
& IFMT
, ino
);
1061 for (size
= 0; size
< dir_size
; size
+= TP_BSIZE
) {
1065 memmove(buf
, cdc
.buf
+ size
, TP_BSIZE
);
1067 spcl
.c_type
= TS_ADDR
;
1070 (void)free(cdc
.buf
);
1072 #endif /* __linux__ */
1076 * Read indirect blocks, and pass the data blocks to be dumped.
1079 dmpindir(dump_ino_t ino
, daddr_t blk
, int ind_level
, fsizeT
*size
)
1086 daddr_t idblk
[MAXNINDIR
];
1089 bread(fsbtodb(sblock
, blk
), (char *)idblk
, (int) sblock
->fs_bsize
);
1092 * My RedHat 4.0 system doesn't have these flags; I haven't
1093 * upgraded e2fsprogs yet
1095 #if defined(EXT2_FLAG_SWAP_BYTES)
1096 if ((fs
->flags
& EXT2_FLAG_SWAP_BYTES
) ||
1097 (fs
->flags
& EXT2_FLAG_SWAP_BYTES_READ
))
1100 max
= sblock
->fs_bsize
>> 2;
1101 swapme
= (blk_t
*) idblk
;
1102 for (i
= 0; i
< max
; i
++, swapme
++)
1103 *swapme
= ext2fs_swab32(*swapme
);
1105 #endif /* __linux__ */
1107 memset(idblk
, 0, (int)sblock
->fs_bsize
);
1108 if (ind_level
<= 0) {
1109 if (*size
< NINDIR(sblock
) * sblock
->fs_bsize
)
1110 cnt
= howmany(*size
, sblock
->fs_fsize
);
1113 cnt
= NINDIR(sblock
) * EXT2_FRAGS_PER_BLOCK(fs
->super
);
1115 cnt
= NINDIR(sblock
) * sblock
->fs_frag
;
1117 *size
-= NINDIR(sblock
) * sblock
->fs_bsize
;
1118 blksout(&idblk
[0], cnt
, ino
);
1122 for (i
= 0; i
< NINDIR(sblock
); i
++) {
1123 dmpindir(ino
, idblk
[i
], ind_level
, size
);
1131 * Collect up the data into tape record sized buffers and output them.
1134 blksout(daddr_t
*blkp
, int frags
, dump_ino_t ino
)
1136 register daddr_t
*bp
;
1137 int i
, j
, count
, blks
, tbperdb
;
1139 blks
= howmany(frags
* sblock
->fs_fsize
, TP_BSIZE
);
1140 tbperdb
= sblock
->fs_bsize
>> tp_bshift
;
1141 for (i
= 0; i
< blks
; i
+= TP_NINDIR
) {
1142 if (i
+ TP_NINDIR
> blks
)
1145 count
= i
+ TP_NINDIR
;
1146 for (j
= i
; j
< count
; j
++)
1147 if (blkp
[j
/ tbperdb
] != 0)
1148 spcl
.c_addr
[j
- i
] = 1;
1150 spcl
.c_addr
[j
- i
] = 0;
1151 spcl
.c_count
= count
- i
;
1153 bp
= &blkp
[i
/ tbperdb
];
1154 for (j
= i
; j
< count
; j
+= tbperdb
, bp
++) {
1156 if (j
+ tbperdb
<= count
)
1157 dumpblock(*bp
, (int)sblock
->fs_bsize
);
1159 dumpblock(*bp
, (count
- j
) * TP_BSIZE
);
1162 spcl
.c_type
= TS_ADDR
;
1167 * Dump a map to the tape.
1170 dumpmap(char *map
, int type
, dump_ino_t ino
)
1176 spcl
.c_count
= howmany(mapsize
* sizeof(char), TP_BSIZE
);
1178 for (i
= 0, cp
= map
; i
< spcl
.c_count
; i
++, cp
+= TP_BSIZE
)
1183 * Write a header record to the dump tape.
1185 #if defined __linux__ && !defined(int32_t)
1186 #define int32_t __s32
1189 writeheader(dump_ino_t ino
)
1191 register int32_t sum
, cnt
, *lp
;
1193 spcl
.c_inumber
= ino
;
1194 spcl
.c_magic
= NFS_MAGIC
;
1195 spcl
.c_checksum
= 0;
1196 lp
= (int32_t *)&spcl
;
1198 cnt
= sizeof(union u_spcl
) / (4 * sizeof(int32_t));
1199 while (--cnt
>= 0) {
1205 spcl
.c_checksum
= CHECKSUM
- sum
;
1206 writerec((char *)&spcl
, 1);
1211 getino(dump_ino_t inum
)
1213 static struct dinode dinode
;
1217 err
= ext2fs_read_inode(fs
, (ext2_ino_t
)inum
, (struct ext2_inode
*) &dinode
);
1219 com_err(disk
, err
, "while reading inode #%ld\n", (long)inum
);
1224 #else /* __linux__ */
1226 getino(dump_ino_t inum
)
1228 static daddr_t minino
, maxino
;
1229 static struct dinode inoblock
[MAXINOPB
];
1232 if (inum
>= minino
&& inum
< maxino
)
1233 return (&inoblock
[inum
- minino
]);
1234 bread(fsbtodb(sblock
, ino_to_fsba(sblock
, inum
)), (char *)inoblock
,
1235 (int)sblock
->fs_bsize
);
1236 minino
= inum
- (inum
% INOPB(sblock
));
1237 maxino
= minino
+ INOPB(sblock
);
1238 return (&inoblock
[inum
- minino
]);
1240 #endif /* __linux__ */
1243 * Read a chunk of data from the disk.
1244 * Try to recover from hard errors by reading in sector sized pieces.
1245 * Error recovery is attempted at most BREADEMAX times before seeking
1246 * consent from the operator to continue.
1248 int breaderrors
= 0;
1249 #define BREADEMAX 32
1252 bread(daddr_t blkno
, char *buf
, int size
)
1259 if (ext2fs_llseek(diskfd
, (((ext2_loff_t
)blkno
) << dev_bshift
), 0) !=
1260 (((ext2_loff_t
)blkno
) << dev_bshift
))
1262 if (lseek(diskfd
, ((off_t
)blkno
<< dev_bshift
), 0) !=
1263 ((off_t
)blkno
<< dev_bshift
))
1265 msg("bread: lseek fails\n");
1266 if ((cnt
= read(diskfd
, buf
, size
)) == size
)
1268 if (blkno
+ (size
/ dev_bsize
) > fsbtodb(sblock
, sblock
->fs_size
)) {
1270 * Trying to read the final fragment.
1272 * NB - dump only works in TP_BSIZE blocks, hence
1273 * rounds `dev_bsize' fragments up to TP_BSIZE pieces.
1274 * It should be smarter about not actually trying to
1275 * read more than it can get, but for the time being
1276 * we punt and scale back the read only when it gets
1277 * us into trouble. (mkm 9/25/83)
1283 msg("read error from %s: %s: [block %d]: count=%d\n",
1284 disk
, strerror(errno
), blkno
, size
);
1286 msg("short read error from %s: [block %d]: count=%d, got=%d\n",
1287 disk
, blkno
, size
, cnt
);
1288 if (++breaderrors
> BREADEMAX
) {
1289 msg("More than %d block read errors from %d\n",
1291 broadcast("DUMP IS AILING!\n");
1292 msg("This is an unrecoverable error.\n");
1293 if (!query("Do you want to attempt to continue?")){
1300 * Zero buffer, then try to read each sector of buffer separately.
1302 memset(buf
, 0, size
);
1303 for (i
= 0; i
< size
; i
+= dev_bsize
, buf
+= dev_bsize
, blkno
++) {
1305 if (ext2fs_llseek(diskfd
, (((ext2_loff_t
)blkno
) << dev_bshift
), 0) !=
1306 (((ext2_loff_t
)blkno
) << dev_bshift
))
1308 if (lseek(diskfd
, ((off_t
)blkno
<< dev_bshift
), 0) !=
1309 ((off_t
)blkno
<< dev_bshift
))
1311 msg("bread: lseek2 fails!\n");
1312 if ((cnt
= read(diskfd
, buf
, (int)dev_bsize
)) == dev_bsize
)
1315 msg("read error from %s: %s: [sector %d]: count=%d\n",
1316 disk
, strerror(errno
), blkno
, dev_bsize
);
1319 msg("short read error from %s: [sector %d]: count=%d, got=%d\n",
1320 disk
, blkno
, dev_bsize
, cnt
);