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.37 2001/07/19 09:49:35 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 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 CHANGEDSINCE(dp, t) \
222 ((dp)->di_mtime >= (t) || (dp)->di_ctime >= (t))
224 /* The NODUMP_FLAG macro tests if a file has the nodump flag. */
226 #define NODUMP_FLAG(dp) (!nonodump && (((dp)->di_flags & UF_NODUMP) == UF_NODUMP))
228 #define NODUMP_FLAG(dp) 0
231 /* The WANTTODUMP macro decides whether a file should be dumped. */
232 #define WANTTODUMP(dp, ino) \
233 (CHANGEDSINCE(dp, spcl.c_ddate) && \
234 (!NODUMP_FLAG(dp)) && \
238 * Determine if given inode should be dumped. "dp" must either point to a
239 * copy of the given inode, or be NULL (in which case it is fetched.)
242 mapfileino(dump_ino_t ino
, struct dinode
const *dp
, long *tapesize
, int *dirskipped
)
247 * Skip inode if we've already marked it for dumping
249 if (TSTINO(ino
, usedinomap
))
253 if ((mode
= (dp
->di_mode
& IFMT
)) == 0)
256 if (dp
->di_nlink
== 0 || dp
->di_dtime
!= 0)
260 * Put all dirs in dumpdirmap, inodes that are to be dumped in the
261 * used map. All inode but dirs who have the nodump attribute go
264 SETINO(ino
, usedinomap
);
267 SETINO(ino
, dumpdirmap
);
268 if (WANTTODUMP(dp
, ino
)) {
269 SETINO(ino
, dumpinomap
);
270 if (mode
!= IFREG
&& mode
!= IFDIR
&& mode
!= IFLNK
)
273 *tapesize
+= blockest(dp
);
277 if ( NODUMP_FLAG(dp
) || exclude_ino(ino
) )
278 CLRINO(ino
, usedinomap
);
286 * Walk the inode list for a filesystem to find all allocated inodes
287 * that have been modified since the previous dump time. Also, find all
288 * the directories in the filesystem.
292 mapfiles(dump_ino_t maxino
, long *tapesize
)
295 int anydirskipped
= 0;
296 ext2_inode_scan scan
;
298 struct ext2_inode inode
;
301 * We use libext2fs's inode scanning routines, which are particularly
302 * robust. (Note that getino cannot return an error.)
304 err
= ext2fs_open_inode_scan(fs
, 0, &scan
);
306 com_err(disk
, err
, "while opening inodes\n");
310 err
= ext2fs_get_next_inode(scan
, &ino
, &inode
);
311 if (err
== EXT2_ET_BAD_BLOCK_IN_INODE_TABLE
)
314 com_err(disk
, err
, "while scanning inode #%ld\n",
322 mapfileino(ino
, (struct dinode
const *)&inode
, tapesize
,
325 ext2fs_close_inode_scan(scan
);
328 * Restore gets very upset if the root is not dumped,
329 * so ensure that it always is dumped.
331 SETINO(ROOTINO
, dumpinomap
);
332 return (anydirskipped
);
336 mapfiles(dump_ino_t maxino
, long *tapesize
)
338 register dump_ino_t ino
;
339 int anydirskipped
= 0;
341 for (ino
= ROOTINO
; ino
< maxino
; ino
++)
342 mapfileino(ino
, tapesize
, &anydirskipped
);
345 * Restore gets very upset if the root is not dumped,
346 * so ensure that it always is dumped.
348 SETINO(ROOTINO
, dumpinomap
);
349 return (anydirskipped
);
351 #endif /* __linux__ */
355 maponefile(dump_ino_t maxino
, long *tapesize
, char *directory
)
359 char dir_name
[MAXPATHLEN
];
360 int i
, anydirskipped
= 0;
363 * Mark every directory in the path as being dumped
365 for (i
= 0; i
< strlen (directory
); i
++) {
366 if (directory
[i
] == '/') {
367 strncpy (dir_name
, directory
, i
);
369 retval
= ext2fs_namei(fs
, ROOTINO
, ROOTINO
,
372 com_err(disk
, retval
,
373 "while translating %s", dir_name
);
376 mapfileino((dump_ino_t
) dir_ino
, 0,
377 tapesize
, &anydirskipped
);
381 * Mark the final directory
383 retval
= ext2fs_namei(fs
, ROOTINO
, ROOTINO
, directory
, &dir_ino
);
385 com_err(disk
, retval
, "while translating %s", directory
);
388 mapfileino((dump_ino_t
)dir_ino
, 0, tapesize
, &anydirskipped
);
390 mapfileino(ROOTINO
, 0, tapesize
, &anydirskipped
);
393 * Restore gets very upset if the root is not dumped,
394 * so ensure that it always is dumped.
396 SETINO(ROOTINO
, dumpdirmap
);
397 return anydirskipped
;
399 #endif /* __linux__ */
402 struct mapfile_context
{
408 mapfilesindir(struct ext2_dir_entry
*dirent
, int offset
, int blocksize
, char *buf
, void *private)
410 register struct dinode
const *dp
;
413 struct mapfile_context
*mfc
;
417 mfc
= (struct mapfile_context
*)private;
418 dp
= getino(dirent
->inode
);
420 mapfileino(dirent
->inode
, dp
, mfc
->tapesize
, mfc
->anydirskipped
);
422 mode
= dp
->di_mode
& IFMT
;
423 if (mode
== IFDIR
&& dp
->di_nlink
!= 0 && dp
->di_dtime
== 0) {
424 if ((dirent
->name
[0] != '.' || ( dirent
->name_len
& 0xFF ) != 1) &&
425 (dirent
->name
[0] != '.' || dirent
->name
[1] != '.' ||
426 ( dirent
->name_len
& 0xFF ) != 2)) {
427 retval
= ext2fs_dir_iterate(fs
, ino
, 0, NULL
,
428 mapfilesindir
, private);
439 * Walk the inode list for a filesystem to find all allocated inodes
440 * that have been modified since the previous dump time. Also, find all
441 * the directories in the filesystem.
444 mapfilesfromdir(dump_ino_t maxino
, long *tapesize
, char *directory
)
447 struct mapfile_context mfc
;
449 char dir_name
[MAXPATHLEN
];
450 int i
, anydirskipped
= 0;
453 * Mark every directory in the path as being dumped
455 for (i
= 0; i
< strlen (directory
); i
++) {
456 if (directory
[i
] == '/') {
457 strncpy (dir_name
, directory
, i
);
459 retval
= ext2fs_namei(fs
, ROOTINO
, ROOTINO
, dir_name
,
462 com_err(disk
, retval
, "while translating %s",
466 mapfileino(dir_ino
, 0, tapesize
, &anydirskipped
);
470 * Mark the final directory
472 retval
= ext2fs_namei(fs
, ROOTINO
, ROOTINO
, directory
, &dir_ino
);
474 com_err(disk
, retval
, "while translating %s", directory
);
477 mapfileino(dir_ino
, 0, tapesize
, &anydirskipped
);
479 mfc
.tapesize
= tapesize
;
480 mfc
.anydirskipped
= &anydirskipped
;
481 retval
= ext2fs_dir_iterate(fs
, dir_ino
, 0, NULL
, mapfilesindir
,
485 com_err(disk
, retval
, "while mapping files in %s", directory
);
489 * Ensure that the root inode actually appears in the file list
492 mapfileino(ROOTINO
, 0, tapesize
, &anydirskipped
);
494 * Restore gets very upset if the root is not dumped,
495 * so ensure that it always is dumped.
497 SETINO(ROOTINO
, dumpinomap
);
498 return anydirskipped
;
503 struct mapdirs_context
{
513 * Scan each directory on the filesystem to see if it has any modified
514 * files in it. If it does, and has not already been added to the dump
515 * list (because it was itself modified), then add it. If a directory
516 * has not been modified itself, contains no modified files and has no
517 * subdirectories, then it can be deleted from the dump list and from
518 * the list of directories. By deleting it from the list of directories,
519 * its parent may now qualify for the same treatment on this or a later
520 * pass using this algorithm.
523 mapdirs(dump_ino_t maxino
, long *tapesize
)
525 register struct dinode
*dp
;
528 register dump_ino_t ino
;
533 struct mapdirs_context mdc
;
535 int ret
, change
= 0, nodump
;
537 isdir
= 0; /* XXX just to get gcc to shut up */
538 for (map
= dumpdirmap
, ino
= 1; ino
< maxino
; ino
++) {
539 if (((ino
- 1) % NBBY
) == 0) /* map is offset by 1 */
544 * If dir has been removed from the used map, it's either
545 * because it had the nodump flag, or it herited it from
546 * its parent. A directory can't be in dumpinomap if not
547 * in usedinomap, but we have to go through it anyway
548 * to propagate the nodump attribute.
550 nodump
= (TSTINO(ino
, usedinomap
) == 0);
551 if ((isdir
& 1) == 0 ||
552 (TSTINO(ino
, dumpinomap
) && nodump
== 0))
559 mdc
.tapesize
= tapesize
;
560 ext2fs_dir_iterate(fs
, ino
, 0, NULL
, searchdir
, (void *) &mdc
);
561 #else /* __linux__ */
562 filesize
= dp
->di_size
;
563 for (ret
= 0, i
= 0; filesize
> 0 && i
< NDADDR
; i
++) {
564 if (dp
->di_db
[i
] != 0)
565 ret
|= searchdir(ino
, dp
->di_db
[i
],
566 (long)dblksize(sblock
, dp
, i
),
568 if (ret
& HASDUMPEDFILE
)
571 filesize
-= sblock
->fs_bsize
;
573 for (i
= 0; filesize
> 0 && i
< NIADDR
; i
++) {
574 if (dp
->di_ib
[i
] == 0)
576 ret
|= dirindir(ino
, dp
->di_ib
[i
], i
, &filesize
);
578 #endif /* __linux__ */
579 if (ret
& HASDUMPEDFILE
) {
580 SETINO(ino
, dumpinomap
);
581 *tapesize
+= blockest(dp
);
586 if (ret
& HASSUBDIRS
)
587 change
= 1; /* subdirs have inherited nodump */
588 CLRINO(ino
, dumpdirmap
);
589 } else if ((ret
& HASSUBDIRS
) == 0) {
590 if (!TSTINO(ino
, dumpinomap
)) {
591 CLRINO(ino
, dumpdirmap
);
601 * Read indirect blocks, and pass the data blocks to be searched
602 * as directories. Quit as soon as any entry is found that will
603 * require the directory to be dumped.
606 dirindir(dump_ino_t ino
, daddr_t blkno
, int ind_level
, long *filesize
)
610 daddr_t idblk
[MAXNINDIR
];
612 bread(fsbtodb(sblock
, blkno
), (char *)idblk
, (int)sblock
->fs_bsize
);
613 if (ind_level
<= 0) {
614 for (i
= 0; *filesize
> 0 && i
< NINDIR(sblock
); i
++) {
617 ret
|= searchdir(ino
, blkno
, sblock
->fs_bsize
,
619 if (ret
& HASDUMPEDFILE
)
622 *filesize
-= sblock
->fs_bsize
;
627 for (i
= 0; *filesize
> 0 && i
< NINDIR(sblock
); i
++) {
630 ret
|= dirindir(ino
, blkno
, ind_level
, filesize
);
634 #endif /* !__linux__ */
637 * Scan a disk block containing directory information looking to see if
638 * any of the entries are on the dump list and to see if the directory
639 * contains any subdirectories.
643 searchdir(struct ext2_dir_entry
*dp
, int offset
, int blocksize
, char *buf
, void *private)
645 struct mapdirs_context
*mdc
;
650 mdc
= (struct mapdirs_context
*)private;
652 tapesize
= mdc
->tapesize
;
656 if (dp
->name
[0] == '.') {
657 if (( dp
->name_len
& 0xFF ) == 1)
659 if (dp
->name
[1] == '.' && ( dp
->name_len
& 0xFF ) == 2)
663 ip
= getino(dp
->inode
);
664 if (TSTINO(dp
->inode
, dumpinomap
)) {
665 CLRINO(dp
->inode
, dumpinomap
);
666 CLRINO(dp
->inode
, usedinomap
);
667 *tapesize
-= blockest(ip
);
669 /* Add dir back to the dir map, to propagate nodump */
670 if ((ip
->di_mode
& IFMT
) == IFDIR
) {
671 SETINO(dp
->inode
, dumpdirmap
);
675 if (TSTINO(dp
->inode
, dumpinomap
)) {
676 *ret
|= HASDUMPEDFILE
;
677 if (*ret
& HASSUBDIRS
)
680 if (TSTINO(dp
->inode
, dumpdirmap
)) {
682 if (*ret
& HASDUMPEDFILE
)
689 #else /* __linux__ */
692 searchdir(dump_ino_t ino
, daddr_t blkno
, long size
, long filesize
)
694 register struct direct
*dp
;
695 register long loc
, ret
= 0;
698 bread(fsbtodb(sblock
, blkno
), dblk
, (int)size
);
701 for (loc
= 0; loc
< size
; ) {
702 dp
= (struct direct
*)(dblk
+ loc
);
703 if (dp
->d_reclen
== 0) {
704 msg("corrupted directory, inumber %d\n", ino
);
710 if (dp
->d_name
[0] == '.') {
711 if (dp
->d_name
[1] == '\0')
713 if (dp
->d_name
[1] == '.' && dp
->d_name
[2] == '\0')
716 if (TSTINO(dp
->d_ino
, dumpinomap
)) {
717 ret
|= HASDUMPEDFILE
;
718 if (ret
& HASSUBDIRS
)
721 if (TSTINO(dp
->d_ino
, dumpdirmap
)) {
723 if (ret
& HASDUMPEDFILE
)
729 #endif /* __linux__ */
733 struct block_context
{
742 * Dump a block to the tape
745 dumponeblock(ext2_filsys fs
, blk_t
*blocknr
, e2_blkcnt_t blockcnt
,
746 blk_t ref_block
, int ref_offset
, void * private)
748 struct block_context
*p
;
751 if (blockcnt
< NDADDR
)
753 p
= (struct block_context
*)private;
754 for (i
= p
->next_block
; i
< blockcnt
; i
++) {
755 p
->buf
[p
->cnt
++] = 0;
756 if (p
->cnt
== p
->max
) {
757 blksout (p
->buf
, p
->cnt
, p
->ino
);
761 p
->buf
[p
->cnt
++] = *blocknr
;
762 if (p
->cnt
== p
->max
) {
763 blksout (p
->buf
, p
->cnt
, p
->ino
);
766 p
->next_block
= blockcnt
+ 1;
772 * Dump passes 3 and 4.
774 * Dump the contents of an inode to tape.
777 dumpino(struct dinode
*dp
, dump_ino_t ino
)
780 fsizeT size
, remaining
;
782 struct old_bsd_inode obi
;
785 struct block_context bc
;
789 u_quad_t i_size
= dp
->di_size
+ ((u_quad_t
) dp
->di_size_high
<< 32);
793 dumpmap(dumpinomap
, TS_BITS
, ino
);
795 CLRINO(ino
, dumpinomap
);
797 memset(&obi
, 0, sizeof(obi
));
798 obi
.di_mode
= dp
->di_mode
;
799 obi
.di_uid
= dp
->di_uid
;
800 obi
.di_gid
= dp
->di_gid
;
801 obi
.di_qsize
.v
= i_size
;
802 obi
.di_atime
= dp
->di_atime
;
803 obi
.di_mtime
= dp
->di_mtime
;
804 obi
.di_ctime
= dp
->di_ctime
;
805 obi
.di_nlink
= dp
->di_nlink
;
806 obi
.di_blocks
= dp
->di_blocks
;
807 obi
.di_flags
= dp
->di_flags
;
808 obi
.di_gen
= dp
->di_gen
;
809 memmove(&obi
.di_db
, &dp
->di_db
, (NDADDR
+ NIADDR
) * sizeof(daddr_t
));
811 warn("ACLs in inode #%ld won't be dumped", (long)ino
);
812 memmove(&spcl
.c_dinode
, &obi
, sizeof(obi
));
813 #else /* __linux__ */
815 #endif /* __linux__ */
816 spcl
.c_type
= TS_INODE
;
818 switch (dp
->di_mode
& S_IFMT
) {
828 msg("Warning: dumpino called on a directory (ino %d)\n", ino
);
834 * Check for short symbolic link.
838 i_size
< EXT2_N_BLOCKS
* sizeof (daddr_t
)) {
842 memmove(buf
, dp
->di_db
, (u_long
)dp
->di_size
);
843 buf
[dp
->di_size
] = '\0';
847 #endif /* __linux__ */
849 if (dp
->di_size
> 0 &&
850 dp
->di_size
< sblock
->fs_maxsymlinklen
) {
854 memmove(buf
, dp
->di_shortlink
, (u_long
)dp
->di_size
);
855 buf
[dp
->di_size
] = '\0';
878 msg("Warning: undefined file type 0%o\n", dp
->di_mode
& IFMT
);
881 if (i_size
> NDADDR
* sblock
->fs_bsize
)
883 cnt
= NDADDR
* EXT2_FRAGS_PER_BLOCK(fs
->super
);
885 cnt
= NDADDR
* sblock
->fs_frag
;
888 cnt
= howmany(i_size
, sblock
->fs_fsize
);
889 blksout(&dp
->di_db
[0], cnt
, ino
);
890 if ((quad_t
) (size
= i_size
- NDADDR
* sblock
->fs_bsize
) <= 0)
893 bc
.max
= NINDIR(sblock
) * EXT2_FRAGS_PER_BLOCK(fs
->super
);
894 bc
.buf
= (int *)malloc (bc
.max
* sizeof (int));
897 bc
.next_block
= NDADDR
;
899 ext2fs_block_iterate2(fs
, (ext2_ino_t
)ino
, 0, NULL
, dumponeblock
, (void *)&bc
);
900 /* deal with holes at the end of the inode */
901 if (i_size
> bc
.next_block
*sblock
->fs_fsize
) {
902 remaining
= i_size
- bc
.next_block
*sblock
->fs_fsize
;
903 for (i
= 0; i
< howmany(remaining
, sblock
->fs_fsize
); i
++) {
904 bc
.buf
[bc
.cnt
++] = 0;
905 if (bc
.cnt
== bc
.max
) {
906 blksout (bc
.buf
, bc
.cnt
, bc
.ino
);
912 blksout (bc
.buf
, bc
.cnt
, bc
.ino
);
916 for (ind_level
= 0; ind_level
< NIADDR
; ind_level
++) {
917 dmpindir(ino
, dp
->di_ib
[ind_level
], ind_level
, &size
);
926 struct convert_dir_context
{
934 * This function converts an ext2fs directory entry to the BSD format.
936 * Basically, it adds a null-character at the end of the name, recomputes the
937 * size of the entry, and creates it in a temporary buffer
940 convert_dir(struct ext2_dir_entry
*dirent
, int offset
, int blocksize
, char *buf
, void *private)
942 struct convert_dir_context
*p
;
943 struct olddirect
*dp
;
946 p
= (struct convert_dir_context
*)private;
948 reclen
= EXT2_DIR_REC_LEN((dirent
->name_len
& 0xFF) + 1);
949 if (((p
->offset
+ reclen
- 1) / p
->bs
) != (p
->offset
/ p
->bs
)) {
950 dp
= (struct olddirect
*)(p
->buf
+ p
->prev_offset
);
951 dp
->d_reclen
+= p
->bs
- (p
->offset
% p
->bs
);
952 p
->offset
+= p
->bs
- (p
->offset
% p
->bs
);
955 dp
= (struct olddirect
*)(p
->buf
+ p
->offset
);
956 dp
->d_ino
= dirent
->inode
;
957 dp
->d_reclen
= reclen
;
958 dp
->d_namlen
= dirent
->name_len
& 0xFF;
959 strncpy(dp
->d_name
, dirent
->name
, dp
->d_namlen
);
960 dp
->d_name
[dp
->d_namlen
] = '\0';
961 p
->prev_offset
= p
->offset
;
970 * Dumps a directory to tape after converting it to the BSD format
973 dumpdirino(struct dinode
*dp
, dump_ino_t ino
)
977 struct old_bsd_inode obi
;
978 struct convert_dir_context cdc
;
980 struct ext2_dir_entry
*de
;
985 dumpmap(dumpinomap
, TS_BITS
, ino
);
987 CLRINO(ino
, dumpinomap
);
990 * Convert the directory to the BSD format
992 /* Allocate a buffer for the conversion (twice the size of the
993 ext2fs directory to avoid problems ;-) */
994 cdc
.buf
= (char *)malloc(dp
->di_size
* 2 * sizeof(char));
996 err(1, "Cannot allocate buffer to convert directory #%lu\n",
1000 cdc
.bs
= MIN(DIRBLKSIZ
, TP_BSIZE
);
1001 /* Do the conversion */
1002 retval
= ext2fs_dir_iterate(fs
, (ext2_ino_t
)ino
, 0, NULL
, convert_dir
, (void *)&cdc
);
1004 com_err(disk
, retval
, "while converting directory #%ld\n", (long)ino
);
1007 /* Fix the last entry */
1008 if ((cdc
.offset
% cdc
.bs
) != 0) {
1009 de
= (struct ext2_dir_entry
*)(cdc
.buf
+ cdc
.prev_offset
);
1010 de
->rec_len
+= cdc
.bs
- (cdc
.offset
% cdc
.bs
);
1011 cdc
.offset
+= cdc
.bs
- (cdc
.offset
% cdc
.bs
);
1014 dir_size
= cdc
.offset
;
1017 memset(&obi
, 0, sizeof(obi
));
1018 obi
.di_mode
= dp
->di_mode
;
1019 obi
.di_uid
= dp
->di_uid
;
1020 obi
.di_gid
= dp
->di_gid
;
1021 obi
.di_qsize
.v
= dir_size
; /* (u_quad_t)dp->di_size; */
1022 obi
.di_atime
= dp
->di_atime
;
1023 obi
.di_mtime
= dp
->di_mtime
;
1024 obi
.di_ctime
= dp
->di_ctime
;
1025 obi
.di_nlink
= dp
->di_nlink
;
1026 obi
.di_blocks
= dp
->di_blocks
;
1027 obi
.di_flags
= dp
->di_flags
;
1028 obi
.di_gen
= dp
->di_gen
;
1029 memmove(&obi
.di_db
, &dp
->di_db
, (NDADDR
+ NIADDR
) * sizeof(daddr_t
));
1030 if (dp
->di_file_acl
)
1031 warn("ACLs in inode #%ld won't be dumped", (long)ino
);
1032 memmove(&spcl
.c_dinode
, &obi
, sizeof(obi
));
1033 #else /* __linux__ */
1034 spcl
.c_dinode
= *dp
;
1035 #endif /* __linux__ */
1036 spcl
.c_type
= TS_INODE
;
1038 switch (dp
->di_mode
& S_IFMT
) {
1049 msg("Warning: size of directory inode #%d is <= 0 (%d)!\n",
1054 msg("Warning: dumpdirino called with file type 0%o (inode #%d)\n",
1055 dp
->di_mode
& IFMT
, ino
);
1058 for (size
= 0; size
< dir_size
; size
+= TP_BSIZE
) {
1062 memmove(buf
, cdc
.buf
+ size
, TP_BSIZE
);
1064 spcl
.c_type
= TS_ADDR
;
1067 (void)free(cdc
.buf
);
1069 #endif /* __linux__ */
1073 * Read indirect blocks, and pass the data blocks to be dumped.
1076 dmpindir(dump_ino_t ino
, daddr_t blk
, int ind_level
, fsizeT
*size
)
1083 daddr_t idblk
[MAXNINDIR
];
1086 bread(fsbtodb(sblock
, blk
), (char *)idblk
, (int) sblock
->fs_bsize
);
1089 * My RedHat 4.0 system doesn't have these flags; I haven't
1090 * upgraded e2fsprogs yet
1092 #if defined(EXT2_FLAG_SWAP_BYTES)
1093 if ((fs
->flags
& EXT2_FLAG_SWAP_BYTES
) ||
1094 (fs
->flags
& EXT2_FLAG_SWAP_BYTES_READ
))
1097 max
= sblock
->fs_bsize
>> 2;
1098 swapme
= (blk_t
*) idblk
;
1099 for (i
= 0; i
< max
; i
++, swapme
++)
1100 *swapme
= ext2fs_swab32(*swapme
);
1102 #endif /* __linux__ */
1104 memset(idblk
, 0, (int)sblock
->fs_bsize
);
1105 if (ind_level
<= 0) {
1106 if (*size
< NINDIR(sblock
) * sblock
->fs_bsize
)
1107 cnt
= howmany(*size
, sblock
->fs_fsize
);
1110 cnt
= NINDIR(sblock
) * EXT2_FRAGS_PER_BLOCK(fs
->super
);
1112 cnt
= NINDIR(sblock
) * sblock
->fs_frag
;
1114 *size
-= NINDIR(sblock
) * sblock
->fs_bsize
;
1115 blksout(&idblk
[0], cnt
, ino
);
1119 for (i
= 0; i
< NINDIR(sblock
); i
++) {
1120 dmpindir(ino
, idblk
[i
], ind_level
, size
);
1128 * Collect up the data into tape record sized buffers and output them.
1131 blksout(daddr_t
*blkp
, int frags
, dump_ino_t ino
)
1133 register daddr_t
*bp
;
1134 int i
, j
, count
, blks
, tbperdb
;
1136 blks
= howmany(frags
* sblock
->fs_fsize
, TP_BSIZE
);
1137 tbperdb
= sblock
->fs_bsize
>> tp_bshift
;
1138 for (i
= 0; i
< blks
; i
+= TP_NINDIR
) {
1139 if (i
+ TP_NINDIR
> blks
)
1142 count
= i
+ TP_NINDIR
;
1143 for (j
= i
; j
< count
; j
++)
1144 if (blkp
[j
/ tbperdb
] != 0)
1145 spcl
.c_addr
[j
- i
] = 1;
1147 spcl
.c_addr
[j
- i
] = 0;
1148 spcl
.c_count
= count
- i
;
1150 bp
= &blkp
[i
/ tbperdb
];
1151 for (j
= i
; j
< count
; j
+= tbperdb
, bp
++) {
1153 if (j
+ tbperdb
<= count
)
1154 dumpblock(*bp
, (int)sblock
->fs_bsize
);
1156 dumpblock(*bp
, (count
- j
) * TP_BSIZE
);
1159 spcl
.c_type
= TS_ADDR
;
1164 * Dump a map to the tape.
1167 dumpmap(char *map
, int type
, dump_ino_t ino
)
1173 spcl
.c_count
= howmany(mapsize
* sizeof(char), TP_BSIZE
);
1175 for (i
= 0, cp
= map
; i
< spcl
.c_count
; i
++, cp
+= TP_BSIZE
)
1180 * Write a header record to the dump tape.
1182 #if defined __linux__ && !defined(int32_t)
1183 #define int32_t __s32
1186 writeheader(dump_ino_t ino
)
1188 register int32_t sum
, cnt
, *lp
;
1190 spcl
.c_inumber
= ino
;
1191 spcl
.c_magic
= NFS_MAGIC
;
1192 spcl
.c_checksum
= 0;
1193 lp
= (int32_t *)&spcl
;
1195 cnt
= sizeof(union u_spcl
) / (4 * sizeof(int32_t));
1196 while (--cnt
>= 0) {
1202 spcl
.c_checksum
= CHECKSUM
- sum
;
1203 writerec((char *)&spcl
, 1);
1208 getino(dump_ino_t inum
)
1210 static struct dinode dinode
;
1214 err
= ext2fs_read_inode(fs
, (ext2_ino_t
)inum
, (struct ext2_inode
*) &dinode
);
1216 com_err(disk
, err
, "while reading inode #%ld\n", (long)inum
);
1221 #else /* __linux__ */
1223 getino(dump_ino_t inum
)
1225 static daddr_t minino
, maxino
;
1226 static struct dinode inoblock
[MAXINOPB
];
1229 if (inum
>= minino
&& inum
< maxino
)
1230 return (&inoblock
[inum
- minino
]);
1231 bread(fsbtodb(sblock
, ino_to_fsba(sblock
, inum
)), (char *)inoblock
,
1232 (int)sblock
->fs_bsize
);
1233 minino
= inum
- (inum
% INOPB(sblock
));
1234 maxino
= minino
+ INOPB(sblock
);
1235 return (&inoblock
[inum
- minino
]);
1237 #endif /* __linux__ */
1240 * Read a chunk of data from the disk.
1241 * Try to recover from hard errors by reading in sector sized pieces.
1242 * Error recovery is attempted at most BREADEMAX times before seeking
1243 * consent from the operator to continue.
1245 int breaderrors
= 0;
1246 #define BREADEMAX 32
1249 bread(daddr_t blkno
, char *buf
, int size
)
1256 if (ext2fs_llseek(diskfd
, (((ext2_loff_t
)blkno
) << dev_bshift
), 0) !=
1257 (((ext2_loff_t
)blkno
) << dev_bshift
))
1259 if (lseek(diskfd
, ((off_t
)blkno
<< dev_bshift
), 0) !=
1260 ((off_t
)blkno
<< dev_bshift
))
1262 msg("bread: lseek fails\n");
1263 if ((cnt
= read(diskfd
, buf
, size
)) == size
)
1265 if (blkno
+ (size
/ dev_bsize
) > fsbtodb(sblock
, sblock
->fs_size
)) {
1267 * Trying to read the final fragment.
1269 * NB - dump only works in TP_BSIZE blocks, hence
1270 * rounds `dev_bsize' fragments up to TP_BSIZE pieces.
1271 * It should be smarter about not actually trying to
1272 * read more than it can get, but for the time being
1273 * we punt and scale back the read only when it gets
1274 * us into trouble. (mkm 9/25/83)
1280 msg("read error from %s: %s: [block %d]: count=%d\n",
1281 disk
, strerror(errno
), blkno
, size
);
1283 msg("short read error from %s: [block %d]: count=%d, got=%d\n",
1284 disk
, blkno
, size
, cnt
);
1285 if (++breaderrors
> BREADEMAX
) {
1286 msg("More than %d block read errors from %d\n",
1288 broadcast("DUMP IS AILING!\n");
1289 msg("This is an unrecoverable error.\n");
1290 if (!query("Do you want to attempt to continue?")){
1297 * Zero buffer, then try to read each sector of buffer separately.
1299 memset(buf
, 0, size
);
1300 for (i
= 0; i
< size
; i
+= dev_bsize
, buf
+= dev_bsize
, blkno
++) {
1302 if (ext2fs_llseek(diskfd
, (((ext2_loff_t
)blkno
) << dev_bshift
), 0) !=
1303 (((ext2_loff_t
)blkno
) << dev_bshift
))
1305 if (lseek(diskfd
, ((off_t
)blkno
<< dev_bshift
), 0) !=
1306 ((off_t
)blkno
<< dev_bshift
))
1308 msg("bread: lseek2 fails!\n");
1309 if ((cnt
= read(diskfd
, buf
, (int)dev_bsize
)) == dev_bsize
)
1312 msg("read error from %s: %s: [sector %d]: count=%d\n",
1313 disk
, strerror(errno
), blkno
, dev_bsize
);
1316 msg("short read error from %s: [sector %d]: count=%d, got=%d\n",
1317 disk
, blkno
, dev_bsize
, cnt
);