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
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.47 2002/06/10 14:05:00 stelian Exp $";
56 #include <sys/param.h>
59 #ifdef HAVE_EXT2FS_EXT2_FS_H
60 #include <ext2fs/ext2_fs.h>
62 #include <linux/ext2_fs.h>
64 #include <ext2fs/ext2fs.h>
65 #include <bsdcompat.h>
66 #include <compaterr.h>
69 #include <sys/vnode.h>
72 #include <ufs/fsdir.h>
73 #include <ufs/inode.h>
75 #include <ufs/ufs/dir.h>
76 #include <ufs/ufs/dinode.h>
77 #include <ufs/ffs/fs.h>
78 #endif /* __linux__ */
80 #include <protocols/dumprestore.h>
84 #define HASDUMPEDFILE 0x1
85 #define HASSUBDIRS 0x2
88 typedef u_quad_t fsizeT
;
91 typedef quad_t fsizeT
;
98 static int searchdir
__P((struct ext2_dir_entry
*dp
, int offset
,
99 int blocksize
, char *buf
, void *private));
101 static int dirindir
__P((dump_ino_t ino
, daddr_t blkno
, int level
, long *size
));
102 static void dmpindir
__P((dump_ino_t ino
, daddr_t blk
, int level
, fsizeT
*size
));
103 static int searchdir
__P((dump_ino_t ino
, daddr_t blkno
, long size
, long filesize
));
105 static void mapfileino
__P((dump_ino_t ino
, struct dinode
const *dp
, long *tapesize
, int *dirskipped
));
107 #ifdef HAVE_EXT2_JOURNAL_INUM
108 #define ext2_journal_ino(sb) (sb->s_journal_inum)
110 #define ext2_journal_ino(sb) (*((__u32 *)sb + 0x38))
112 #ifndef HAVE_EXT2_INO_T
113 typedef ino_t ext2_ino_t
;
116 #ifndef EXT3_FEATURE_COMPAT_HAS_JOURNAL
117 #define EXT3_FEATURE_COMPAT_HAS_JOURNAL 0x0004
119 #ifndef EXT2_FEATURE_INCOMPAT_FILETYPE
120 #define EXT2_FEATURE_INCOMPAT_FILETYPE 0x0002
122 #ifndef EXT3_FEATURE_INCOMPAT_RECOVER
123 #define EXT3_FEATURE_INCOMPAT_RECOVER 0x0004
125 #ifndef EXT3_FEATURE_INCOMPAT_JOURNAL_DEV
126 #define EXT3_FEATURE_INCOMPAT_JOURNAL_DEV 0x0008
129 #ifndef EXT2_LIB_FEATURE_INCOMPAT_SUPP
130 #define EXT2_LIB_FEATURE_INCOMPAT_SUPP (EXT3_FEATURE_INCOMPAT_RECOVER | \
131 EXT2_FEATURE_INCOMPAT_FILETYPE)
133 #ifndef EXT2_RESIZE_INO
134 #define EXT2_RESIZE_INO 7
137 int dump_fs_open(const char *disk
, ext2_filsys
*fs
)
141 retval
= ext2fs_open(disk
, EXT2_FLAG_FORCE
, 0, 0, unix_io_manager
, fs
);
143 struct ext2_super_block
*es
= (*fs
)->super
;
144 dump_ino_t journal_ino
= ext2_journal_ino(es
);
146 if (es
->s_feature_incompat
& EXT3_FEATURE_INCOMPAT_JOURNAL_DEV
){
147 msg("This is a journal, not a filesystem!\n");
148 retval
= EXT2_ET_UNSUPP_FEATURE
;
151 else if ((retval
= es
->s_feature_incompat
&
152 ~(EXT2_LIB_FEATURE_INCOMPAT_SUPP
|
153 EXT3_FEATURE_INCOMPAT_RECOVER
))) {
154 msg("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 do_exclude_ino(journal_ino
, "journal inode");
164 do_exclude_ino(EXT2_RESIZE_INO
, "resize inode");
171 * This is an estimation of the number of TP_BSIZE blocks in the file.
172 * It estimates the number of blocks in files with holes by assuming
173 * that all of the blocks accounted for by di_blocks are data blocks
174 * (when some of the blocks are usually used for indirect pointers);
175 * hence the estimate may be high.
178 blockest(struct dinode
const *dp
)
180 long blkest
, sizeest
;
184 * dp->di_size is the size of the file in bytes.
185 * dp->di_blocks stores the number of sectors actually in the file.
186 * If there are more sectors than the size would indicate, this just
187 * means that there are indirect blocks in the file or unused
188 * sectors in the last file block; we can safely ignore these
189 * (blkest = sizeest below).
190 * If the file is bigger than the number of sectors would indicate,
191 * then the file has holes in it. In this case we must use the
192 * block count to estimate the number of data blocks used, but
193 * we use the actual size for estimating the number of indirect
194 * dump blocks (sizeest vs. blkest in the indirect block
197 blkest
= howmany((u_quad_t
)dp
->di_blocks
*fs
->blocksize
, TP_BSIZE
);
198 i_size
= dp
->di_size
+ ((u_quad_t
) dp
->di_size_high
<< 32);
199 sizeest
= howmany(i_size
, TP_BSIZE
);
200 if (blkest
> sizeest
)
203 if (i_size
> fs
->blocksize
* NDADDR
) {
204 /* calculate the number of indirect blocks on the dump tape */
206 howmany(sizeest
- NDADDR
* fs
->blocksize
/ TP_BSIZE
,
207 NINDIR(sblock
) * EXT2_FRAGS_PER_BLOCK(fs
->super
));
210 if (i_size
> sblock
->fs_bsize
* NDADDR
) {
211 /* calculate the number of indirect blocks on the dump tape */
213 howmany(sizeest
- NDADDR
* sblock
->fs_bsize
/ TP_BSIZE
,
220 /* Auxiliary macro to pick up files changed since previous dump. */
221 #define CSINCE(dp, t) \
222 ((dp)->di_ctime >= (t))
223 #define MSINCE(dp, t) \
224 ((dp)->di_mtime >= (t))
225 #define CHANGEDSINCE(dp, t) \
226 (CSINCE(dp, t) || MSINCE(dp, t))
228 /* The NODUMP_FLAG macro tests if a file has the nodump flag. */
230 #define NODUMP_FLAG(dp) (!nonodump && (((dp)->di_flags & UF_NODUMP) == UF_NODUMP))
232 #define NODUMP_FLAG(dp) 0
235 /* The WANTTODUMP macro decides whether a file should be dumped. */
236 #define WANTTODUMP(dp, ino) \
237 (CHANGEDSINCE(dp, spcl.c_ddate) && \
238 (!NODUMP_FLAG(dp)) && \
242 * Determine if given inode should be dumped. "dp" must either point to a
243 * copy of the given inode, or be NULL (in which case it is fetched.)
246 mapfileino(dump_ino_t ino
, struct dinode
const *dp
, long *tapesize
, int *dirskipped
)
251 * Skip inode if we've already marked it for dumping
253 if (TSTINO(ino
, usedinomap
))
257 if ((mode
= (dp
->di_mode
& IFMT
)) == 0)
260 if (dp
->di_nlink
== 0 || dp
->di_dtime
!= 0)
264 * Put all dirs in dumpdirmap, inodes that are to be dumped in the
265 * used map. All inode but dirs who have the nodump attribute go
268 SETINO(ino
, usedinomap
);
271 SETINO(ino
, dumpdirmap
);
272 if (WANTTODUMP(dp
, ino
)) {
273 SETINO(ino
, dumpinomap
);
274 if (!MSINCE(dp
, spcl
.c_ddate
))
275 SETINO(ino
, metainomap
);
276 if (mode
!= IFREG
&& mode
!= IFDIR
&& mode
!= IFLNK
)
279 *tapesize
+= blockest(dp
);
283 if ( NODUMP_FLAG(dp
) || exclude_ino(ino
) )
284 CLRINO(ino
, usedinomap
);
292 * Walk the inode list for a filesystem to find all allocated inodes
293 * that have been modified since the previous dump time. Also, find all
294 * the directories in the filesystem.
298 mapfiles(dump_ino_t maxino
, long *tapesize
)
301 int anydirskipped
= 0;
302 ext2_inode_scan scan
;
304 struct ext2_inode inode
;
307 * We use libext2fs's inode scanning routines, which are particularly
308 * robust. (Note that getino cannot return an error.)
310 err
= ext2fs_open_inode_scan(fs
, 0, &scan
);
312 com_err(disk
, err
, "while opening inodes\n");
316 err
= ext2fs_get_next_inode(scan
, &ino
, &inode
);
317 if (err
== EXT2_ET_BAD_BLOCK_IN_INODE_TABLE
)
320 com_err(disk
, err
, "while scanning inode #%ld\n",
328 mapfileino(ino
, (struct dinode
const *)&inode
, tapesize
,
331 ext2fs_close_inode_scan(scan
);
334 * Restore gets very upset if the root is not dumped,
335 * so ensure that it always is dumped.
337 SETINO(ROOTINO
, dumpinomap
);
338 return (anydirskipped
);
342 mapfiles(dump_ino_t maxino
, long *tapesize
)
345 int anydirskipped
= 0;
347 for (ino
= ROOTINO
; ino
< maxino
; ino
++)
348 mapfileino(ino
, tapesize
, &anydirskipped
);
351 * Restore gets very upset if the root is not dumped,
352 * so ensure that it always is dumped.
354 SETINO(ROOTINO
, dumpinomap
);
355 return (anydirskipped
);
357 #endif /* __linux__ */
361 maponefile(dump_ino_t maxino
, long *tapesize
, char *directory
)
365 char dir_name
[MAXPATHLEN
];
366 int i
, anydirskipped
= 0;
369 * Mark every directory in the path as being dumped
371 for (i
= 0; i
< strlen (directory
); i
++) {
372 if (directory
[i
] == '/') {
373 strncpy (dir_name
, directory
, i
);
375 retval
= ext2fs_namei(fs
, ROOTINO
, ROOTINO
,
378 com_err(disk
, retval
,
379 "while translating %s", dir_name
);
382 mapfileino((dump_ino_t
) dir_ino
, 0,
383 tapesize
, &anydirskipped
);
387 * Mark the final directory
389 retval
= ext2fs_namei(fs
, ROOTINO
, ROOTINO
, directory
, &dir_ino
);
391 com_err(disk
, retval
, "while translating %s", directory
);
394 mapfileino((dump_ino_t
)dir_ino
, 0, tapesize
, &anydirskipped
);
396 mapfileino(ROOTINO
, 0, tapesize
, &anydirskipped
);
399 * Restore gets very upset if the root is not dumped,
400 * so ensure that it always is dumped.
402 SETINO(ROOTINO
, dumpdirmap
);
403 return anydirskipped
;
405 #endif /* __linux__ */
408 struct mapfile_context
{
414 mapfilesindir(struct ext2_dir_entry
*dirent
, int offset
, int blocksize
, char *buf
, void *private)
416 struct dinode
const *dp
;
419 struct mapfile_context
*mfc
;
423 mfc
= (struct mapfile_context
*)private;
424 dp
= getino(dirent
->inode
);
426 mapfileino(dirent
->inode
, dp
, mfc
->tapesize
, mfc
->anydirskipped
);
428 mode
= dp
->di_mode
& IFMT
;
429 if (mode
== IFDIR
&& dp
->di_nlink
!= 0 && dp
->di_dtime
== 0) {
430 if ((dirent
->name
[0] != '.' || ( dirent
->name_len
& 0xFF ) != 1) &&
431 (dirent
->name
[0] != '.' || dirent
->name
[1] != '.' ||
432 ( dirent
->name_len
& 0xFF ) != 2)) {
433 retval
= ext2fs_dir_iterate(fs
, ino
, 0, NULL
,
434 mapfilesindir
, private);
445 * Walk the inode list for a filesystem to find all allocated inodes
446 * that have been modified since the previous dump time. Also, find all
447 * the directories in the filesystem.
450 mapfilesfromdir(dump_ino_t maxino
, long *tapesize
, char *directory
)
453 struct mapfile_context mfc
;
455 char dir_name
[MAXPATHLEN
];
456 int i
, anydirskipped
= 0;
459 * Mark every directory in the path as being dumped
461 for (i
= 0; i
< strlen (directory
); i
++) {
462 if (directory
[i
] == '/') {
463 strncpy (dir_name
, directory
, i
);
465 retval
= ext2fs_namei(fs
, ROOTINO
, ROOTINO
, dir_name
,
468 com_err(disk
, retval
, "while translating %s",
472 mapfileino(dir_ino
, 0, tapesize
, &anydirskipped
);
476 * Mark the final directory
478 retval
= ext2fs_namei(fs
, ROOTINO
, ROOTINO
, directory
, &dir_ino
);
480 com_err(disk
, retval
, "while translating %s", directory
);
483 mapfileino(dir_ino
, 0, tapesize
, &anydirskipped
);
485 mfc
.tapesize
= tapesize
;
486 mfc
.anydirskipped
= &anydirskipped
;
487 retval
= ext2fs_dir_iterate(fs
, dir_ino
, 0, NULL
, mapfilesindir
,
491 com_err(disk
, retval
, "while mapping files in %s", directory
);
495 * Ensure that the root inode actually appears in the file list
498 mapfileino(ROOTINO
, 0, tapesize
, &anydirskipped
);
500 * Restore gets very upset if the root is not dumped,
501 * so ensure that it always is dumped.
503 SETINO(ROOTINO
, dumpinomap
);
504 return anydirskipped
;
509 struct mapdirs_context
{
519 * Scan each directory on the filesystem to see if it has any modified
520 * files in it. If it does, and has not already been added to the dump
521 * list (because it was itself modified), then add it. If a directory
522 * has not been modified itself, contains no modified files and has no
523 * subdirectories, then it can be deleted from the dump list and from
524 * the list of directories. By deleting it from the list of directories,
525 * its parent may now qualify for the same treatment on this or a later
526 * pass using this algorithm.
529 mapdirs(dump_ino_t maxino
, long *tapesize
)
539 struct mapdirs_context mdc
;
541 int ret
, change
= 0, nodump
;
543 isdir
= 0; /* XXX just to get gcc to shut up */
544 for (map
= dumpdirmap
, ino
= 1; ino
< maxino
; ino
++) {
545 if (((ino
- 1) % NBBY
) == 0) /* map is offset by 1 */
550 * If dir has been removed from the used map, it's either
551 * because it had the nodump flag, or it herited it from
552 * its parent. A directory can't be in dumpinomap if not
553 * in usedinomap, but we have to go through it anyway
554 * to propagate the nodump attribute.
556 nodump
= (TSTINO(ino
, usedinomap
) == 0);
557 if ((isdir
& 1) == 0 ||
558 (TSTINO(ino
, dumpinomap
) && nodump
== 0))
565 mdc
.tapesize
= tapesize
;
566 ext2fs_dir_iterate(fs
, ino
, 0, NULL
, searchdir
, (void *) &mdc
);
567 #else /* __linux__ */
568 filesize
= dp
->di_size
;
569 for (ret
= 0, i
= 0; filesize
> 0 && i
< NDADDR
; i
++) {
570 if (dp
->di_db
[i
] != 0)
571 ret
|= searchdir(ino
, dp
->di_db
[i
],
572 (long)dblksize(sblock
, dp
, i
),
574 if (ret
& HASDUMPEDFILE
)
577 filesize
-= sblock
->fs_bsize
;
579 for (i
= 0; filesize
> 0 && i
< NIADDR
; i
++) {
580 if (dp
->di_ib
[i
] == 0)
582 ret
|= dirindir(ino
, dp
->di_ib
[i
], i
, &filesize
);
584 #endif /* __linux__ */
585 if (ret
& HASDUMPEDFILE
) {
586 SETINO(ino
, dumpinomap
);
587 *tapesize
+= blockest(dp
);
592 if (ret
& HASSUBDIRS
)
593 change
= 1; /* subdirs have inherited nodump */
594 CLRINO(ino
, dumpdirmap
);
595 } else if ((ret
& HASSUBDIRS
) == 0) {
596 if (!TSTINO(ino
, dumpinomap
)) {
597 CLRINO(ino
, dumpdirmap
);
607 * Read indirect blocks, and pass the data blocks to be searched
608 * as directories. Quit as soon as any entry is found that will
609 * require the directory to be dumped.
612 dirindir(dump_ino_t ino
, daddr_t blkno
, int ind_level
, long *filesize
)
616 daddr_t idblk
[MAXNINDIR
];
618 bread(fsbtodb(sblock
, blkno
), (char *)idblk
, (int)sblock
->fs_bsize
);
619 if (ind_level
<= 0) {
620 for (i
= 0; *filesize
> 0 && i
< NINDIR(sblock
); i
++) {
623 ret
|= searchdir(ino
, blkno
, sblock
->fs_bsize
,
625 if (ret
& HASDUMPEDFILE
)
628 *filesize
-= sblock
->fs_bsize
;
633 for (i
= 0; *filesize
> 0 && i
< NINDIR(sblock
); i
++) {
636 ret
|= dirindir(ino
, blkno
, ind_level
, filesize
);
640 #endif /* !__linux__ */
643 * Scan a disk block containing directory information looking to see if
644 * any of the entries are on the dump list and to see if the directory
645 * contains any subdirectories.
649 searchdir(struct ext2_dir_entry
*dp
, int offset
, int blocksize
, char *buf
, void *private)
651 struct mapdirs_context
*mdc
;
656 mdc
= (struct mapdirs_context
*)private;
658 tapesize
= mdc
->tapesize
;
662 if (dp
->name
[0] == '.') {
663 if (( dp
->name_len
& 0xFF ) == 1)
665 if (dp
->name
[1] == '.' && ( dp
->name_len
& 0xFF ) == 2)
669 ip
= getino(dp
->inode
);
670 if (TSTINO(dp
->inode
, dumpinomap
)) {
671 CLRINO(dp
->inode
, dumpinomap
);
672 *tapesize
-= blockest(ip
);
674 /* Add dir back to the dir map and remove from
675 * usedinomap to propagate nodump */
676 if ((ip
->di_mode
& IFMT
) == IFDIR
) {
677 SETINO(dp
->inode
, dumpdirmap
);
678 CLRINO(dp
->inode
, usedinomap
);
682 if (TSTINO(dp
->inode
, dumpinomap
)) {
683 *ret
|= HASDUMPEDFILE
;
684 if (*ret
& HASSUBDIRS
)
687 if (TSTINO(dp
->inode
, dumpdirmap
)) {
689 if (*ret
& HASDUMPEDFILE
)
696 #else /* __linux__ */
699 searchdir(dump_ino_t ino
, daddr_t blkno
, long size
, long filesize
)
705 bread(fsbtodb(sblock
, blkno
), dblk
, (int)size
);
708 for (loc
= 0; loc
< size
; ) {
709 dp
= (struct direct
*)(dblk
+ loc
);
710 if (dp
->d_reclen
== 0) {
711 msg("corrupted directory, inumber %d\n", ino
);
717 if (dp
->d_name
[0] == '.') {
718 if (dp
->d_name
[1] == '\0')
720 if (dp
->d_name
[1] == '.' && dp
->d_name
[2] == '\0')
723 if (TSTINO(dp
->d_ino
, dumpinomap
)) {
724 ret
|= HASDUMPEDFILE
;
725 if (ret
& HASSUBDIRS
)
728 if (TSTINO(dp
->d_ino
, dumpdirmap
)) {
730 if (ret
& HASDUMPEDFILE
)
736 #endif /* __linux__ */
740 struct block_context
{
749 * Dump a block to the tape
752 dumponeblock(ext2_filsys fs
, blk_t
*blocknr
, e2_blkcnt_t blockcnt
,
753 blk_t ref_block
, int ref_offset
, void * private)
755 struct block_context
*p
;
758 if (blockcnt
< NDADDR
)
760 p
= (struct block_context
*)private;
761 for (i
= p
->next_block
; i
< blockcnt
; i
++) {
762 p
->buf
[p
->cnt
++] = 0;
763 if (p
->cnt
== p
->max
) {
764 blksout (p
->buf
, p
->cnt
, p
->ino
);
768 p
->buf
[p
->cnt
++] = *blocknr
;
769 if (p
->cnt
== p
->max
) {
770 blksout (p
->buf
, p
->cnt
, p
->ino
);
773 p
->next_block
= blockcnt
+ 1;
779 * Dump passes 3 and 4.
781 * Dump the contents of an inode to tape.
784 dumpino(struct dinode
*dp
, dump_ino_t ino
, int metaonly
)
787 fsizeT size
, remaining
;
789 struct old_bsd_inode obi
;
792 struct block_context bc
;
801 i_size
= dp
->di_size
+ ((u_quad_t
) dp
->di_size_high
<< 32);
805 dumpmap(dumpinomap
, TS_BITS
, ino
);
807 CLRINO(ino
, dumpinomap
);
809 memset(&obi
, 0, sizeof(obi
));
810 obi
.di_mode
= dp
->di_mode
;
811 obi
.di_uid
= dp
->di_uid
;
812 obi
.di_gid
= dp
->di_gid
;
813 obi
.di_qsize
.v
= i_size
;
814 obi
.di_atime
= dp
->di_atime
;
815 obi
.di_mtime
= dp
->di_mtime
;
816 obi
.di_ctime
= dp
->di_ctime
;
817 obi
.di_nlink
= dp
->di_nlink
;
818 obi
.di_blocks
= dp
->di_blocks
;
819 obi
.di_flags
= dp
->di_flags
;
820 obi
.di_gen
= dp
->di_gen
;
821 memmove(&obi
.di_db
, &dp
->di_db
, (NDADDR
+ NIADDR
) * sizeof(daddr_t
));
823 warn("ACLs in inode #%ld won't be dumped", (long)ino
);
824 memmove(&spcl
.c_dinode
, &obi
, sizeof(obi
));
825 #else /* __linux__ */
827 #endif /* __linux__ */
828 spcl
.c_type
= TS_INODE
;
831 if (metaonly
&& (dp
->di_mode
& S_IFMT
)) {
832 spcl
.c_flags
|= DR_METAONLY
;
835 spcl
.c_flags
&= ~DR_METAONLY
;
839 switch (dp
->di_mode
& S_IFMT
) {
849 msg("Warning: dumpino called on a directory (ino %d)\n", ino
);
855 * Check for short symbolic link.
859 i_size
< EXT2_N_BLOCKS
* sizeof (daddr_t
)) {
863 memmove(buf
, dp
->di_db
, (u_long
)dp
->di_size
);
864 buf
[dp
->di_size
] = '\0';
868 #endif /* __linux__ */
870 if (dp
->di_size
> 0 &&
871 dp
->di_size
< sblock
->fs_maxsymlinklen
) {
875 memmove(buf
, dp
->di_shortlink
, (u_long
)dp
->di_size
);
876 buf
[dp
->di_size
] = '\0';
899 msg("Warning: undefined file type 0%o\n", dp
->di_mode
& IFMT
);
902 if (i_size
> NDADDR
* sblock
->fs_bsize
)
904 cnt
= NDADDR
* EXT2_FRAGS_PER_BLOCK(fs
->super
);
906 cnt
= NDADDR
* sblock
->fs_frag
;
909 cnt
= howmany(i_size
, sblock
->fs_fsize
);
910 blksout(&dp
->di_db
[0], cnt
, ino
);
911 if ((quad_t
) (size
= i_size
- NDADDR
* sblock
->fs_bsize
) <= 0)
914 bc
.max
= NINDIR(sblock
) * EXT2_FRAGS_PER_BLOCK(fs
->super
);
915 bc
.buf
= (int *)malloc (bc
.max
* sizeof (int));
918 bc
.next_block
= NDADDR
;
920 ext2fs_block_iterate2(fs
, (ext2_ino_t
)ino
, 0, NULL
, dumponeblock
, (void *)&bc
);
921 /* deal with holes at the end of the inode */
922 if (i_size
> ((u_quad_t
)bc
.next_block
) * sblock
->fs_fsize
) {
923 remaining
= i_size
- ((u_quad_t
)bc
.next_block
) * sblock
->fs_fsize
;
924 for (i
= 0; i
< howmany(remaining
, sblock
->fs_fsize
); i
++) {
925 bc
.buf
[bc
.cnt
++] = 0;
926 if (bc
.cnt
== bc
.max
) {
927 blksout (bc
.buf
, bc
.cnt
, bc
.ino
);
933 blksout (bc
.buf
, bc
.cnt
, bc
.ino
);
937 for (ind_level
= 0; ind_level
< NIADDR
; ind_level
++) {
938 dmpindir(ino
, dp
->di_ib
[ind_level
], ind_level
, &size
);
947 struct convert_dir_context
{
955 * This function converts an ext2fs directory entry to the BSD format.
957 * Basically, it adds a null-character at the end of the name, recomputes the
958 * size of the entry, and creates it in a temporary buffer
961 convert_dir(struct ext2_dir_entry
*dirent
, int offset
, int blocksize
, char *buf
, void *private)
963 struct convert_dir_context
*p
;
964 struct olddirect
*dp
;
967 p
= (struct convert_dir_context
*)private;
969 reclen
= EXT2_DIR_REC_LEN((dirent
->name_len
& 0xFF) + 1);
970 if (((p
->offset
+ reclen
- 1) / p
->bs
) != (p
->offset
/ p
->bs
)) {
971 dp
= (struct olddirect
*)(p
->buf
+ p
->prev_offset
);
972 dp
->d_reclen
+= p
->bs
- (p
->offset
% p
->bs
);
973 p
->offset
+= p
->bs
- (p
->offset
% p
->bs
);
976 dp
= (struct olddirect
*)(p
->buf
+ p
->offset
);
977 dp
->d_ino
= dirent
->inode
;
978 dp
->d_reclen
= reclen
;
979 dp
->d_namlen
= dirent
->name_len
& 0xFF;
980 strncpy(dp
->d_name
, dirent
->name
, dp
->d_namlen
);
981 dp
->d_name
[dp
->d_namlen
] = '\0';
982 p
->prev_offset
= p
->offset
;
991 * Dumps a directory to tape after converting it to the BSD format
994 dumpdirino(struct dinode
*dp
, dump_ino_t ino
)
998 struct old_bsd_inode obi
;
999 struct convert_dir_context cdc
;
1001 struct ext2_dir_entry
*de
;
1006 dumpmap(dumpinomap
, TS_BITS
, ino
);
1008 CLRINO(ino
, dumpinomap
);
1011 * Convert the directory to the BSD format
1013 /* Allocate a buffer for the conversion (twice the size of the
1014 ext2fs directory to avoid problems ;-) */
1015 cdc
.buf
= (char *)malloc(dp
->di_size
* 2 * sizeof(char));
1016 if (cdc
.buf
== NULL
)
1017 err(1, "Cannot allocate buffer to convert directory #%lu\n",
1018 (unsigned long)ino
);
1020 cdc
.prev_offset
= 0;
1021 cdc
.bs
= MIN(DIRBLKSIZ
, TP_BSIZE
);
1022 /* Do the conversion */
1023 retval
= ext2fs_dir_iterate(fs
, (ext2_ino_t
)ino
, 0, NULL
, convert_dir
, (void *)&cdc
);
1025 com_err(disk
, retval
, "while converting directory #%ld\n", (long)ino
);
1028 /* Fix the last entry */
1029 if ((cdc
.offset
% cdc
.bs
) != 0) {
1030 de
= (struct ext2_dir_entry
*)(cdc
.buf
+ cdc
.prev_offset
);
1031 de
->rec_len
+= cdc
.bs
- (cdc
.offset
% cdc
.bs
);
1032 cdc
.offset
+= cdc
.bs
- (cdc
.offset
% cdc
.bs
);
1035 dir_size
= cdc
.offset
;
1038 memset(&obi
, 0, sizeof(obi
));
1039 obi
.di_mode
= dp
->di_mode
;
1040 obi
.di_uid
= dp
->di_uid
;
1041 obi
.di_gid
= dp
->di_gid
;
1042 obi
.di_qsize
.v
= dir_size
; /* (u_quad_t)dp->di_size; */
1043 obi
.di_atime
= dp
->di_atime
;
1044 obi
.di_mtime
= dp
->di_mtime
;
1045 obi
.di_ctime
= dp
->di_ctime
;
1046 obi
.di_nlink
= dp
->di_nlink
;
1047 obi
.di_blocks
= dp
->di_blocks
;
1048 obi
.di_flags
= dp
->di_flags
;
1049 obi
.di_gen
= dp
->di_gen
;
1050 memmove(&obi
.di_db
, &dp
->di_db
, (NDADDR
+ NIADDR
) * sizeof(daddr_t
));
1051 if (dp
->di_file_acl
)
1052 warn("ACLs in inode #%ld won't be dumped", (long)ino
);
1053 memmove(&spcl
.c_dinode
, &obi
, sizeof(obi
));
1054 #else /* __linux__ */
1055 spcl
.c_dinode
= *dp
;
1056 #endif /* __linux__ */
1057 spcl
.c_type
= TS_INODE
;
1059 switch (dp
->di_mode
& S_IFMT
) {
1070 msg("Warning: size of directory inode #%d is <= 0 (%d)!\n",
1075 msg("Warning: dumpdirino called with file type 0%o (inode #%d)\n",
1076 dp
->di_mode
& IFMT
, ino
);
1079 for (size
= 0; size
< dir_size
; size
+= TP_BSIZE
) {
1083 memmove(buf
, cdc
.buf
+ size
, TP_BSIZE
);
1085 spcl
.c_type
= TS_ADDR
;
1088 (void)free(cdc
.buf
);
1090 #endif /* __linux__ */
1094 * Read indirect blocks, and pass the data blocks to be dumped.
1097 dmpindir(dump_ino_t ino
, daddr_t blk
, int ind_level
, fsizeT
*size
)
1104 daddr_t idblk
[MAXNINDIR
];
1107 bread(fsbtodb(sblock
, blk
), (char *)idblk
, (int) sblock
->fs_bsize
);
1110 * My RedHat 4.0 system doesn't have these flags; I haven't
1111 * upgraded e2fsprogs yet
1113 #if defined(EXT2_FLAG_SWAP_BYTES)
1114 if ((fs
->flags
& EXT2_FLAG_SWAP_BYTES
) ||
1115 (fs
->flags
& EXT2_FLAG_SWAP_BYTES_READ
))
1118 max
= sblock
->fs_bsize
>> 2;
1119 swapme
= (blk_t
*) idblk
;
1120 for (i
= 0; i
< max
; i
++, swapme
++)
1121 *swapme
= ext2fs_swab32(*swapme
);
1123 #endif /* __linux__ */
1125 memset(idblk
, 0, (int)sblock
->fs_bsize
);
1126 if (ind_level
<= 0) {
1127 if (*size
< NINDIR(sblock
) * sblock
->fs_bsize
)
1128 cnt
= howmany(*size
, sblock
->fs_fsize
);
1131 cnt
= NINDIR(sblock
) * EXT2_FRAGS_PER_BLOCK(fs
->super
);
1133 cnt
= NINDIR(sblock
) * sblock
->fs_frag
;
1135 *size
-= NINDIR(sblock
) * sblock
->fs_bsize
;
1136 blksout(&idblk
[0], cnt
, ino
);
1140 for (i
= 0; i
< NINDIR(sblock
); i
++) {
1141 dmpindir(ino
, idblk
[i
], ind_level
, size
);
1149 * Collect up the data into tape record sized buffers and output them.
1152 blksout(daddr_t
*blkp
, int frags
, dump_ino_t ino
)
1155 int i
, j
, count
, blks
, tbperdb
;
1157 blks
= howmany(frags
* sblock
->fs_fsize
, TP_BSIZE
);
1158 tbperdb
= sblock
->fs_bsize
>> tp_bshift
;
1159 for (i
= 0; i
< blks
; i
+= TP_NINDIR
) {
1160 if (i
+ TP_NINDIR
> blks
)
1163 count
= i
+ TP_NINDIR
;
1164 for (j
= i
; j
< count
; j
++)
1165 if (blkp
[j
/ tbperdb
] != 0)
1166 spcl
.c_addr
[j
- i
] = 1;
1168 spcl
.c_addr
[j
- i
] = 0;
1169 spcl
.c_count
= count
- i
;
1171 bp
= &blkp
[i
/ tbperdb
];
1172 for (j
= i
; j
< count
; j
+= tbperdb
, bp
++) {
1174 if (j
+ tbperdb
<= count
)
1175 dumpblock(*bp
, (int)sblock
->fs_bsize
);
1177 dumpblock(*bp
, (count
- j
) * TP_BSIZE
);
1180 spcl
.c_type
= TS_ADDR
;
1185 * Dump a map to the tape.
1188 dumpmap(char *map
, int type
, dump_ino_t ino
)
1194 spcl
.c_count
= howmany(mapsize
* sizeof(char), TP_BSIZE
);
1196 for (i
= 0, cp
= map
; i
< spcl
.c_count
; i
++, cp
+= TP_BSIZE
)
1200 #if defined __linux__ && !defined(int32_t)
1201 #define int32_t __s32
1205 * Compute and fill in checksum information.
1208 mkchecksum(union u_spcl
*tmpspcl
)
1210 int32_t sum
, cnt
, *lp
;
1212 tmpspcl
->s_spcl
.c_checksum
= 0;
1213 lp
= (int32_t *)&tmpspcl
->s_spcl
;
1215 cnt
= sizeof(union u_spcl
) / (4 * sizeof(int32_t));
1216 while (--cnt
>= 0) {
1222 tmpspcl
->s_spcl
.c_checksum
= CHECKSUM
- sum
;
1226 * Write a header record to the dump tape.
1229 writeheader(dump_ino_t ino
)
1231 spcl
.c_inumber
= ino
;
1232 spcl
.c_magic
= NFS_MAGIC
;
1233 mkchecksum((union u_spcl
*)&spcl
);
1234 writerec((char *)&spcl
, 1);
1239 getino(dump_ino_t inum
)
1241 static struct dinode dinode
;
1245 err
= ext2fs_read_inode(fs
, (ext2_ino_t
)inum
, (struct ext2_inode
*) &dinode
);
1247 com_err(disk
, err
, "while reading inode #%ld\n", (long)inum
);
1252 #else /* __linux__ */
1254 getino(dump_ino_t inum
)
1256 static daddr_t minino
, maxino
;
1257 static struct dinode inoblock
[MAXINOPB
];
1260 if (inum
>= minino
&& inum
< maxino
)
1261 return (&inoblock
[inum
- minino
]);
1262 bread(fsbtodb(sblock
, ino_to_fsba(sblock
, inum
)), (char *)inoblock
,
1263 (int)sblock
->fs_bsize
);
1264 minino
= inum
- (inum
% INOPB(sblock
));
1265 maxino
= minino
+ INOPB(sblock
);
1266 return (&inoblock
[inum
- minino
]);
1268 #endif /* __linux__ */
1271 * Read a chunk of data from the disk.
1272 * Try to recover from hard errors by reading in sector sized pieces.
1273 * Error recovery is attempted at most breademax times before seeking
1274 * consent from the operator to continue.
1276 int breaderrors
= 0;
1279 bread(daddr_t blkno
, char *buf
, int size
)
1285 if (ext2fs_llseek(diskfd
, (((ext2_loff_t
)blkno
) << dev_bshift
), 0) !=
1286 (((ext2_loff_t
)blkno
) << dev_bshift
))
1288 if (lseek(diskfd
, ((off_t
)blkno
<< dev_bshift
), 0) !=
1289 ((off_t
)blkno
<< dev_bshift
))
1291 msg("bread: lseek fails\n");
1292 if ((cnt
= read(diskfd
, buf
, size
)) == size
)
1294 if (blkno
+ (size
/ dev_bsize
) > fsbtodb(sblock
, sblock
->fs_size
)) {
1296 * Trying to read the final fragment.
1298 * NB - dump only works in TP_BSIZE blocks, hence
1299 * rounds `dev_bsize' fragments up to TP_BSIZE pieces.
1300 * It should be smarter about not actually trying to
1301 * read more than it can get, but for the time being
1302 * we punt and scale back the read only when it gets
1303 * us into trouble. (mkm 9/25/83)
1309 msg("read error from %s: %s: [block %d, ext2blk %d]: count=%d\n",
1310 disk
, strerror(errno
), blkno
,
1311 dbtofsb(sblock
, blkno
), size
);
1313 msg("short read error from %s: [block %d, ext2blk %d]: count=%d, got=%d\n",
1314 disk
, blkno
, dbtofsb(sblock
, blkno
), size
, cnt
);
1315 if (++breaderrors
> breademax
) {
1316 msg("More than %d block read errors from %d\n",
1318 broadcast("DUMP IS AILING!\n");
1319 msg("This is an unrecoverable error.\n");
1320 if (!query("Do you want to attempt to continue?")){
1327 * Zero buffer, then try to read each sector of buffer separately.
1329 memset(buf
, 0, size
);
1330 for (i
= 0; i
< size
; i
+= dev_bsize
, buf
+= dev_bsize
, blkno
++) {
1332 if (ext2fs_llseek(diskfd
, (((ext2_loff_t
)blkno
) << dev_bshift
), 0) !=
1333 (((ext2_loff_t
)blkno
) << dev_bshift
))
1335 if (lseek(diskfd
, ((off_t
)blkno
<< dev_bshift
), 0) !=
1336 ((off_t
)blkno
<< dev_bshift
))
1338 msg("bread: lseek2 fails!\n");
1339 if ((cnt
= read(diskfd
, buf
, (int)dev_bsize
)) == dev_bsize
)
1342 msg("read error from %s: %s: [sector %d, ext2blk %d]: count=%d\n",
1343 disk
, strerror(errno
), blkno
,
1344 dbtofsb(sblock
, blkno
), dev_bsize
);
1347 msg("short read error from %s: [sector %d, ext2blk %d]: count=%d, got=%d\n",
1348 disk
, blkno
, dbtofsb(sblock
, blkno
), dev_bsize
, cnt
);