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.51 2002/09/02 12:20:26 stelian Exp $";
50 #include <sys/types.h>
57 #include <sys/param.h>
60 #ifdef HAVE_EXT2FS_EXT2_FS_H
61 #include <ext2fs/ext2_fs.h>
63 #include <linux/ext2_fs.h>
65 #include <ext2fs/ext2fs.h>
66 #include <bsdcompat.h>
67 #include <compaterr.h>
70 #include <sys/vnode.h>
73 #include <ufs/fsdir.h>
74 #include <ufs/inode.h>
76 #include <ufs/ufs/dir.h>
77 #include <ufs/ufs/dinode.h>
78 #include <ufs/ffs/fs.h>
79 #endif /* __linux__ */
81 #include <protocols/dumprestore.h>
85 #define HASDUMPEDFILE 0x1
86 #define HASSUBDIRS 0x2
89 typedef u_quad_t fsizeT
;
92 typedef quad_t fsizeT
;
99 static int searchdir
__P((struct ext2_dir_entry
*dp
, int offset
,
100 int blocksize
, char *buf
, void *private));
102 static int dirindir
__P((dump_ino_t ino
, daddr_t blkno
, int level
, long *size
));
103 static void dmpindir
__P((dump_ino_t ino
, daddr_t blk
, int level
, fsizeT
*size
));
104 static int searchdir
__P((dump_ino_t ino
, daddr_t blkno
, long size
, long filesize
));
106 static void mapfileino
__P((dump_ino_t ino
, struct dinode
const *dp
, long *tapesize
, int *dirskipped
));
108 #ifdef HAVE_EXT2_JOURNAL_INUM
109 #define ext2_journal_ino(sb) (sb->s_journal_inum)
111 #define ext2_journal_ino(sb) (*((u_int32_t *)sb + 0x38))
113 #ifndef HAVE_EXT2_INO_T
114 typedef ino_t ext2_ino_t
;
117 #ifndef EXT3_FEATURE_COMPAT_HAS_JOURNAL
118 #define EXT3_FEATURE_COMPAT_HAS_JOURNAL 0x0004
120 #ifndef EXT2_FEATURE_INCOMPAT_FILETYPE
121 #define EXT2_FEATURE_INCOMPAT_FILETYPE 0x0002
123 #ifndef EXT3_FEATURE_INCOMPAT_RECOVER
124 #define EXT3_FEATURE_INCOMPAT_RECOVER 0x0004
126 #ifndef EXT3_FEATURE_INCOMPAT_JOURNAL_DEV
127 #define EXT3_FEATURE_INCOMPAT_JOURNAL_DEV 0x0008
130 #ifndef EXT2_LIB_FEATURE_INCOMPAT_SUPP
131 #define EXT2_LIB_FEATURE_INCOMPAT_SUPP (EXT3_FEATURE_INCOMPAT_RECOVER | \
132 EXT2_FEATURE_INCOMPAT_FILETYPE)
134 #ifndef EXT2_RESIZE_INO
135 #define EXT2_RESIZE_INO 7
138 int dump_fs_open(const char *disk
, ext2_filsys
*fs
)
142 retval
= ext2fs_open(disk
, EXT2_FLAG_FORCE
, 0, 0, unix_io_manager
, fs
);
144 struct ext2_super_block
*es
= (*fs
)->super
;
145 dump_ino_t journal_ino
= ext2_journal_ino(es
);
147 if (es
->s_feature_incompat
& EXT3_FEATURE_INCOMPAT_JOURNAL_DEV
){
148 msg("This is a journal, not a filesystem!\n");
149 retval
= EXT2_ET_UNSUPP_FEATURE
;
152 else if ((retval
= es
->s_feature_incompat
&
153 ~(EXT2_LIB_FEATURE_INCOMPAT_SUPP
|
154 EXT3_FEATURE_INCOMPAT_RECOVER
))) {
155 msg("Unsupported feature(s) 0x%x in filesystem\n",
157 retval
= EXT2_ET_UNSUPP_FEATURE
;
161 if (es
->s_feature_compat
&
162 EXT3_FEATURE_COMPAT_HAS_JOURNAL
&&
164 do_exclude_ino(journal_ino
, "journal inode");
165 do_exclude_ino(EXT2_RESIZE_INO
, "resize inode");
172 * This is an estimation of the number of TP_BSIZE blocks in the file.
173 * It estimates the number of blocks in files with holes by assuming
174 * that all of the blocks accounted for by di_blocks are data blocks
175 * (when some of the blocks are usually used for indirect pointers);
176 * hence the estimate may be high.
179 blockest(struct dinode
const *dp
)
181 long blkest
, sizeest
;
185 * dp->di_size is the size of the file in bytes.
186 * dp->di_blocks stores the number of sectors actually in the file.
187 * If there are more sectors than the size would indicate, this just
188 * means that there are indirect blocks in the file or unused
189 * sectors in the last file block; we can safely ignore these
190 * (blkest = sizeest below).
191 * If the file is bigger than the number of sectors would indicate,
192 * then the file has holes in it. In this case we must use the
193 * block count to estimate the number of data blocks used, but
194 * we use the actual size for estimating the number of indirect
195 * dump blocks (sizeest vs. blkest in the indirect block
198 blkest
= howmany((u_quad_t
)dp
->di_blocks
* 512, TP_BSIZE
);
199 i_size
= dp
->di_size
+ ((u_quad_t
) dp
->di_size_high
<< 32);
200 sizeest
= howmany(i_size
, TP_BSIZE
);
201 if (blkest
> sizeest
)
204 if (i_size
> (u_quad_t
)fs
->blocksize
* NDADDR
) {
205 /* calculate the number of indirect blocks on the dump tape */
207 howmany(sizeest
- NDADDR
* fs
->blocksize
/ TP_BSIZE
,
208 NINDIR(sblock
) * EXT2_FRAGS_PER_BLOCK(fs
->super
));
211 if (i_size
> sblock
->fs_bsize
* NDADDR
) {
212 /* calculate the number of indirect blocks on the dump tape */
214 howmany(sizeest
- NDADDR
* sblock
->fs_bsize
/ TP_BSIZE
,
221 /* Auxiliary macro to pick up files changed since previous dump. */
222 #define CSINCE(dp, t) \
223 ((dp)->di_ctime >= (t))
224 #define MSINCE(dp, t) \
225 ((dp)->di_mtime >= (t))
226 #define CHANGEDSINCE(dp, t) \
227 (CSINCE(dp, t) || MSINCE(dp, t))
229 /* The NODUMP_FLAG macro tests if a file has the nodump flag. */
231 #define NODUMP_FLAG(dp) (!nonodump && (((dp)->di_flags & UF_NODUMP) == UF_NODUMP))
233 #define NODUMP_FLAG(dp) 0
236 /* The WANTTODUMP macro decides whether a file should be dumped. */
237 #define WANTTODUMP(dp, ino) \
238 (CHANGEDSINCE(dp, ((u_int32_t)spcl.c_ddate)) && \
239 (!NODUMP_FLAG(dp)) && \
243 * Determine if given inode should be dumped. "dp" must either point to a
244 * copy of the given inode, or be NULL (in which case it is fetched.)
247 mapfileino(dump_ino_t ino
, struct dinode
const *dp
, long *tapesize
, int *dirskipped
)
252 * Skip inode if we've already marked it for dumping
254 if (TSTINO(ino
, usedinomap
))
258 if ((mode
= (dp
->di_mode
& IFMT
)) == 0)
261 if (dp
->di_nlink
== 0 || dp
->di_dtime
!= 0)
265 * Put all dirs in dumpdirmap, inodes that are to be dumped in the
266 * used map. All inode but dirs who have the nodump attribute go
269 SETINO(ino
, usedinomap
);
272 SETINO(ino
, dumpdirmap
);
273 if (WANTTODUMP(dp
, ino
)) {
274 SETINO(ino
, dumpinomap
);
275 if (!MSINCE(dp
, (u_int32_t
)spcl
.c_ddate
))
276 SETINO(ino
, metainomap
);
277 if (mode
!= IFREG
&& mode
!= IFDIR
&& mode
!= IFLNK
)
280 *tapesize
+= blockest(dp
);
284 if ( NODUMP_FLAG(dp
) || exclude_ino(ino
) )
285 CLRINO(ino
, usedinomap
);
293 * Walk the inode list for a filesystem to find all allocated inodes
294 * that have been modified since the previous dump time. Also, find all
295 * the directories in the filesystem.
299 mapfiles(UNUSED(dump_ino_t maxino
), long *tapesize
)
302 int anydirskipped
= 0;
303 ext2_inode_scan scan
;
305 struct ext2_inode inode
;
308 * We use libext2fs's inode scanning routines, which are particularly
309 * robust. (Note that getino cannot return an error.)
311 err
= ext2fs_open_inode_scan(fs
, 0, &scan
);
313 com_err(disk
, err
, "while opening inodes\n");
317 err
= ext2fs_get_next_inode(scan
, &ino
, &inode
);
318 if (err
== EXT2_ET_BAD_BLOCK_IN_INODE_TABLE
)
321 com_err(disk
, err
, "while scanning inode #%ld\n",
329 mapfileino(ino
, (struct dinode
const *)&inode
, tapesize
,
332 ext2fs_close_inode_scan(scan
);
335 * Restore gets very upset if the root is not dumped,
336 * so ensure that it always is dumped.
338 SETINO(ROOTINO
, dumpinomap
);
339 return (anydirskipped
);
343 mapfiles(dump_ino_t maxino
, long *tapesize
)
346 int anydirskipped
= 0;
348 for (ino
= ROOTINO
; ino
< maxino
; ino
++)
349 mapfileino(ino
, tapesize
, &anydirskipped
);
352 * Restore gets very upset if the root is not dumped,
353 * so ensure that it always is dumped.
355 SETINO(ROOTINO
, dumpinomap
);
356 return (anydirskipped
);
358 #endif /* __linux__ */
362 maponefile(UNUSED(dump_ino_t maxino
), long *tapesize
, char *directory
)
366 char dir_name
[MAXPATHLEN
];
367 int i
, anydirskipped
= 0;
370 * Mark every directory in the path as being dumped
372 for (i
= 0; i
< (int)strlen (directory
); i
++) {
373 if (directory
[i
] == '/') {
374 strncpy (dir_name
, directory
, i
);
376 retval
= ext2fs_namei(fs
, ROOTINO
, ROOTINO
,
379 com_err(disk
, retval
,
380 "while translating %s", dir_name
);
383 mapfileino((dump_ino_t
) dir_ino
, 0,
384 tapesize
, &anydirskipped
);
388 * Mark the final directory
390 retval
= ext2fs_namei(fs
, ROOTINO
, ROOTINO
, directory
, &dir_ino
);
392 com_err(disk
, retval
, "while translating %s", directory
);
395 mapfileino((dump_ino_t
)dir_ino
, 0, tapesize
, &anydirskipped
);
397 mapfileino(ROOTINO
, 0, tapesize
, &anydirskipped
);
400 * Restore gets very upset if the root is not dumped,
401 * so ensure that it always is dumped.
403 SETINO(ROOTINO
, dumpdirmap
);
404 return anydirskipped
;
406 #endif /* __linux__ */
409 struct mapfile_context
{
415 mapfilesindir(struct ext2_dir_entry
*dirent
, UNUSED(int offset
),
416 UNUSED(int blocksize
), UNUSED(char *buf
), void *private)
418 struct dinode
const *dp
;
421 struct mapfile_context
*mfc
;
425 mfc
= (struct mapfile_context
*)private;
426 dp
= getino(dirent
->inode
);
428 mapfileino(dirent
->inode
, dp
, mfc
->tapesize
, mfc
->anydirskipped
);
430 mode
= dp
->di_mode
& IFMT
;
431 if (mode
== IFDIR
&& dp
->di_nlink
!= 0 && dp
->di_dtime
== 0) {
432 if ((dirent
->name
[0] != '.' || ( dirent
->name_len
& 0xFF ) != 1) &&
433 (dirent
->name
[0] != '.' || dirent
->name
[1] != '.' ||
434 ( dirent
->name_len
& 0xFF ) != 2)) {
435 retval
= ext2fs_dir_iterate(fs
, ino
, 0, NULL
,
436 mapfilesindir
, private);
447 * Walk the inode list for a filesystem to find all allocated inodes
448 * that have been modified since the previous dump time. Also, find all
449 * the directories in the filesystem.
452 mapfilesfromdir(UNUSED(dump_ino_t maxino
), long *tapesize
, char *directory
)
455 struct mapfile_context mfc
;
457 char dir_name
[MAXPATHLEN
];
458 int i
, anydirskipped
= 0;
461 * Mark every directory in the path as being dumped
463 for (i
= 0; i
< (int)strlen (directory
); i
++) {
464 if (directory
[i
] == '/') {
465 strncpy (dir_name
, directory
, i
);
467 retval
= ext2fs_namei(fs
, ROOTINO
, ROOTINO
, dir_name
,
470 com_err(disk
, retval
, "while translating %s",
474 mapfileino(dir_ino
, 0, tapesize
, &anydirskipped
);
478 * Mark the final directory
480 retval
= ext2fs_namei(fs
, ROOTINO
, ROOTINO
, directory
, &dir_ino
);
482 com_err(disk
, retval
, "while translating %s", directory
);
485 mapfileino(dir_ino
, 0, tapesize
, &anydirskipped
);
487 mfc
.tapesize
= tapesize
;
488 mfc
.anydirskipped
= &anydirskipped
;
489 retval
= ext2fs_dir_iterate(fs
, dir_ino
, 0, NULL
, mapfilesindir
,
493 com_err(disk
, retval
, "while mapping files in %s", directory
);
497 * Ensure that the root inode actually appears in the file list
500 mapfileino(ROOTINO
, 0, tapesize
, &anydirskipped
);
502 * Restore gets very upset if the root is not dumped,
503 * so ensure that it always is dumped.
505 SETINO(ROOTINO
, dumpinomap
);
506 return anydirskipped
;
511 struct mapdirs_context
{
521 * Scan each directory on the filesystem to see if it has any modified
522 * files in it. If it does, and has not already been added to the dump
523 * list (because it was itself modified), then add it. If a directory
524 * has not been modified itself, contains no modified files and has no
525 * subdirectories, then it can be deleted from the dump list and from
526 * the list of directories. By deleting it from the list of directories,
527 * its parent may now qualify for the same treatment on this or a later
528 * pass using this algorithm.
531 mapdirs(dump_ino_t maxino
, long *tapesize
)
541 struct mapdirs_context mdc
;
543 int ret
, change
= 0, nodump
;
545 isdir
= 0; /* XXX just to get gcc to shut up */
546 for (map
= dumpdirmap
, ino
= 1; ino
< maxino
; ino
++) {
547 if (((ino
- 1) % NBBY
) == 0) /* map is offset by 1 */
552 * If dir has been removed from the used map, it's either
553 * because it had the nodump flag, or it herited it from
554 * its parent. A directory can't be in dumpinomap if not
555 * in usedinomap, but we have to go through it anyway
556 * to propagate the nodump attribute.
558 nodump
= (TSTINO(ino
, usedinomap
) == 0);
559 if ((isdir
& 1) == 0 ||
560 (TSTINO(ino
, dumpinomap
) && nodump
== 0))
567 mdc
.tapesize
= tapesize
;
568 ext2fs_dir_iterate(fs
, ino
, 0, NULL
, searchdir
, (void *) &mdc
);
569 #else /* __linux__ */
570 filesize
= dp
->di_size
;
571 for (ret
= 0, i
= 0; filesize
> 0 && i
< NDADDR
; i
++) {
572 if (dp
->di_db
[i
] != 0)
573 ret
|= searchdir(ino
, dp
->di_db
[i
],
574 (long)dblksize(sblock
, dp
, i
),
576 if (ret
& HASDUMPEDFILE
)
579 filesize
-= sblock
->fs_bsize
;
581 for (i
= 0; filesize
> 0 && i
< NIADDR
; i
++) {
582 if (dp
->di_ib
[i
] == 0)
584 ret
|= dirindir(ino
, dp
->di_ib
[i
], i
, &filesize
);
586 #endif /* __linux__ */
587 if (ret
& HASDUMPEDFILE
) {
588 SETINO(ino
, dumpinomap
);
589 *tapesize
+= blockest(dp
);
594 if (ret
& HASSUBDIRS
)
595 change
= 1; /* subdirs have inherited nodump */
596 CLRINO(ino
, dumpdirmap
);
597 } else if ((ret
& HASSUBDIRS
) == 0) {
598 if (!TSTINO(ino
, dumpinomap
)) {
599 CLRINO(ino
, dumpdirmap
);
609 * Read indirect blocks, and pass the data blocks to be searched
610 * as directories. Quit as soon as any entry is found that will
611 * require the directory to be dumped.
614 dirindir(dump_ino_t ino
, daddr_t blkno
, int ind_level
, long *filesize
)
618 daddr_t idblk
[MAXNINDIR
];
620 bread(fsbtodb(sblock
, blkno
), (char *)idblk
, (int)sblock
->fs_bsize
);
621 if (ind_level
<= 0) {
622 for (i
= 0; *filesize
> 0 && i
< NINDIR(sblock
); i
++) {
625 ret
|= searchdir(ino
, blkno
, sblock
->fs_bsize
,
627 if (ret
& HASDUMPEDFILE
)
630 *filesize
-= sblock
->fs_bsize
;
635 for (i
= 0; *filesize
> 0 && i
< NINDIR(sblock
); i
++) {
638 ret
|= dirindir(ino
, blkno
, ind_level
, filesize
);
642 #endif /* !__linux__ */
645 * Scan a disk block containing directory information looking to see if
646 * any of the entries are on the dump list and to see if the directory
647 * contains any subdirectories.
651 searchdir(struct ext2_dir_entry
*dp
, UNUSED(int offset
),
652 UNUSED(int blocksize
), UNUSED(char *buf
), void *private)
654 struct mapdirs_context
*mdc
;
659 mdc
= (struct mapdirs_context
*)private;
661 tapesize
= mdc
->tapesize
;
665 if (dp
->name
[0] == '.') {
666 if (( dp
->name_len
& 0xFF ) == 1)
668 if (dp
->name
[1] == '.' && ( dp
->name_len
& 0xFF ) == 2)
672 ip
= getino(dp
->inode
);
673 if (TSTINO(dp
->inode
, dumpinomap
)) {
674 CLRINO(dp
->inode
, dumpinomap
);
675 *tapesize
-= blockest(ip
);
677 /* Add dir back to the dir map and remove from
678 * usedinomap to propagate nodump */
679 if ((ip
->di_mode
& IFMT
) == IFDIR
) {
680 SETINO(dp
->inode
, dumpdirmap
);
681 CLRINO(dp
->inode
, usedinomap
);
685 if (TSTINO(dp
->inode
, dumpinomap
)) {
686 *ret
|= HASDUMPEDFILE
;
687 if (*ret
& HASSUBDIRS
)
690 if (TSTINO(dp
->inode
, dumpdirmap
)) {
692 if (*ret
& HASDUMPEDFILE
)
699 #else /* __linux__ */
702 searchdir(dump_ino_t ino
, daddr_t blkno
, long size
, long filesize
)
708 bread(fsbtodb(sblock
, blkno
), dblk
, (int)size
);
711 for (loc
= 0; loc
< size
; ) {
712 dp
= (struct direct
*)(dblk
+ loc
);
713 if (dp
->d_reclen
== 0) {
714 msg("corrupted directory, inumber %d\n", ino
);
720 if (dp
->d_name
[0] == '.') {
721 if (dp
->d_name
[1] == '\0')
723 if (dp
->d_name
[1] == '.' && dp
->d_name
[2] == '\0')
726 if (TSTINO(dp
->d_ino
, dumpinomap
)) {
727 ret
|= HASDUMPEDFILE
;
728 if (ret
& HASSUBDIRS
)
731 if (TSTINO(dp
->d_ino
, dumpdirmap
)) {
733 if (ret
& HASDUMPEDFILE
)
739 #endif /* __linux__ */
743 struct block_context
{
752 * Dump a block to the tape
755 dumponeblock(UNUSED(ext2_filsys fs
), blk_t
*blocknr
, e2_blkcnt_t blockcnt
,
756 UNUSED(blk_t ref_block
), UNUSED(int ref_offset
), void * private)
758 struct block_context
*p
;
761 if (blockcnt
< NDADDR
)
763 p
= (struct block_context
*)private;
764 for (i
= p
->next_block
; i
< blockcnt
; i
++) {
765 p
->buf
[p
->cnt
++] = 0;
766 if (p
->cnt
== p
->max
) {
767 blksout (p
->buf
, p
->cnt
, p
->ino
);
771 p
->buf
[p
->cnt
++] = *blocknr
;
772 if (p
->cnt
== p
->max
) {
773 blksout (p
->buf
, p
->cnt
, p
->ino
);
776 p
->next_block
= blockcnt
+ 1;
782 * Dump passes 3 and 4.
784 * Dump the contents of an inode to tape.
787 dumpino(struct dinode
*dp
, dump_ino_t ino
, int metaonly
)
790 fsizeT size
, remaining
;
792 struct new_bsd_inode nbi
;
795 struct block_context bc
;
804 i_size
= dp
->di_size
+ ((u_quad_t
) dp
->di_size_high
<< 32);
808 dumpmap(dumpinomap
, TS_BITS
, ino
);
810 CLRINO(ino
, dumpinomap
);
812 memset(&nbi
, 0, sizeof(nbi
));
813 nbi
.di_mode
= dp
->di_mode
;
814 nbi
.di_nlink
= dp
->di_nlink
;
815 nbi
.di_ouid
= dp
->di_uid
;
816 nbi
.di_ogid
= dp
->di_gid
;
817 nbi
.di_size
= i_size
;
818 nbi
.di_atime
.tv_sec
= dp
->di_atime
;
819 nbi
.di_mtime
.tv_sec
= dp
->di_mtime
;
820 nbi
.di_ctime
.tv_sec
= dp
->di_ctime
;
821 memmove(&nbi
.di_db
, &dp
->di_db
, (NDADDR
+ NIADDR
) * sizeof(daddr_t
));
822 nbi
.di_flags
= dp
->di_flags
;
823 nbi
.di_blocks
= dp
->di_blocks
;
824 nbi
.di_gen
= dp
->di_gen
;
825 nbi
.di_uid
= (((int32_t)dp
->di_uidhigh
) << 16) | dp
->di_uid
;
826 nbi
.di_gid
= (((int32_t)dp
->di_gidhigh
) << 16) | dp
->di_gid
;
828 warn("ACLs in inode #%ld won't be dumped", (long)ino
);
829 memmove(&spcl
.c_dinode
, &nbi
, sizeof(nbi
));
830 #else /* __linux__ */
832 #endif /* __linux__ */
833 spcl
.c_type
= TS_INODE
;
836 if (metaonly
&& (dp
->di_mode
& S_IFMT
)) {
837 spcl
.c_flags
|= DR_METAONLY
;
840 spcl
.c_flags
&= ~DR_METAONLY
;
844 switch (dp
->di_mode
& S_IFMT
) {
854 msg("Warning: dumpino called on a directory (ino %d)\n", ino
);
860 * Check for short symbolic link.
864 i_size
< EXT2_N_BLOCKS
* sizeof (daddr_t
)) {
868 memmove(buf
, dp
->di_db
, (u_long
)dp
->di_size
);
869 buf
[dp
->di_size
] = '\0';
873 #endif /* __linux__ */
875 if (dp
->di_size
> 0 &&
876 dp
->di_size
< sblock
->fs_maxsymlinklen
) {
880 memmove(buf
, dp
->di_shortlink
, (u_long
)dp
->di_size
);
881 buf
[dp
->di_size
] = '\0';
904 msg("Warning: undefined file type 0%o\n", dp
->di_mode
& IFMT
);
907 if (i_size
> (u_quad_t
)NDADDR
* sblock
->fs_bsize
)
909 cnt
= NDADDR
* EXT2_FRAGS_PER_BLOCK(fs
->super
);
911 cnt
= NDADDR
* sblock
->fs_frag
;
914 cnt
= howmany(i_size
, sblock
->fs_fsize
);
915 blksout(&dp
->di_db
[0], cnt
, ino
);
916 if ((quad_t
) (size
= i_size
- NDADDR
* sblock
->fs_bsize
) <= 0)
919 bc
.max
= NINDIR(sblock
) * EXT2_FRAGS_PER_BLOCK(fs
->super
);
920 bc
.buf
= (int *)malloc (bc
.max
* sizeof (int));
923 bc
.next_block
= NDADDR
;
925 ext2fs_block_iterate2(fs
, (ext2_ino_t
)ino
, 0, NULL
, dumponeblock
, (void *)&bc
);
926 /* deal with holes at the end of the inode */
927 if (i_size
> ((u_quad_t
)bc
.next_block
) * sblock
->fs_fsize
) {
928 remaining
= i_size
- ((u_quad_t
)bc
.next_block
) * sblock
->fs_fsize
;
929 for (i
= 0; i
< (int)howmany(remaining
, sblock
->fs_fsize
); i
++) {
930 bc
.buf
[bc
.cnt
++] = 0;
931 if (bc
.cnt
== bc
.max
) {
932 blksout (bc
.buf
, bc
.cnt
, bc
.ino
);
938 blksout (bc
.buf
, bc
.cnt
, bc
.ino
);
942 for (ind_level
= 0; ind_level
< NIADDR
; ind_level
++) {
943 dmpindir(ino
, dp
->di_ib
[ind_level
], ind_level
, &size
);
952 struct convert_dir_context
{
960 * This function converts an ext2fs directory entry to the BSD format.
962 * Basically, it adds a null-character at the end of the name, recomputes the
963 * size of the entry, and creates it in a temporary buffer
966 convert_dir(struct ext2_dir_entry
*dirent
, UNUSED(int offset
),
967 UNUSED(int blocksize
), UNUSED(char *buf
), void *private)
969 struct convert_dir_context
*p
;
973 p
= (struct convert_dir_context
*)private;
975 reclen
= EXT2_DIR_REC_LEN((dirent
->name_len
& 0xFF) + 1);
976 if (((p
->offset
+ reclen
- 1) / p
->bs
) != (p
->offset
/ p
->bs
)) {
977 dp
= (struct direct
*)(p
->buf
+ p
->prev_offset
);
978 dp
->d_reclen
+= p
->bs
- (p
->offset
% p
->bs
);
979 p
->offset
+= p
->bs
- (p
->offset
% p
->bs
);
982 dp
= (struct direct
*)(p
->buf
+ p
->offset
);
983 dp
->d_ino
= dirent
->inode
;
984 dp
->d_reclen
= reclen
;
985 dp
->d_namlen
= dirent
->name_len
& 0xFF;
986 switch ((dirent
->name_len
& 0xFF00) >> 8) {
988 dp
->d_type
= DT_UNKNOWN
;
990 case EXT2_FT_REG_FILE
:
1000 dp
->d_type
= DT_BLK
;
1003 dp
->d_type
= DT_FIFO
;
1006 dp
->d_type
= DT_SOCK
;
1008 case EXT2_FT_SYMLINK
:
1009 dp
->d_type
= DT_LNK
;
1012 strncpy(dp
->d_name
, dirent
->name
, dp
->d_namlen
);
1013 dp
->d_name
[dp
->d_namlen
] = '\0';
1014 p
->prev_offset
= p
->offset
;
1015 p
->offset
+= reclen
;
1023 * Dumps a directory to tape after converting it to the BSD format
1026 dumpdirino(struct dinode
*dp
, dump_ino_t ino
)
1030 struct new_bsd_inode nbi
;
1031 struct convert_dir_context cdc
;
1033 struct ext2_dir_entry
*de
;
1038 dumpmap(dumpinomap
, TS_BITS
, ino
);
1040 CLRINO(ino
, dumpinomap
);
1043 * Convert the directory to the BSD format
1045 /* Allocate a buffer for the conversion (twice the size of the
1046 ext2fs directory to avoid problems ;-) */
1047 cdc
.buf
= (char *)malloc(dp
->di_size
* 2 * sizeof(char));
1048 if (cdc
.buf
== NULL
)
1049 err(1, "Cannot allocate buffer to convert directory #%lu\n",
1050 (unsigned long)ino
);
1052 cdc
.prev_offset
= 0;
1053 cdc
.bs
= MIN(DIRBLKSIZ
, TP_BSIZE
);
1054 /* Do the conversion */
1055 retval
= ext2fs_dir_iterate(fs
, (ext2_ino_t
)ino
, 0, NULL
, convert_dir
, (void *)&cdc
);
1057 com_err(disk
, retval
, "while converting directory #%ld\n", (long)ino
);
1060 /* Fix the last entry */
1061 if ((cdc
.offset
% cdc
.bs
) != 0) {
1062 de
= (struct ext2_dir_entry
*)(cdc
.buf
+ cdc
.prev_offset
);
1063 de
->rec_len
+= cdc
.bs
- (cdc
.offset
% cdc
.bs
);
1064 cdc
.offset
+= cdc
.bs
- (cdc
.offset
% cdc
.bs
);
1067 dir_size
= cdc
.offset
;
1070 memset(&nbi
, 0, sizeof(nbi
));
1071 nbi
.di_mode
= dp
->di_mode
;
1072 nbi
.di_nlink
= dp
->di_nlink
;
1073 nbi
.di_ouid
= dp
->di_uid
;
1074 nbi
.di_ogid
= dp
->di_gid
;
1075 nbi
.di_size
= dir_size
; /* (u_quad_t)dp->di_size; */
1076 nbi
.di_atime
.tv_sec
= dp
->di_atime
;
1077 nbi
.di_mtime
.tv_sec
= dp
->di_mtime
;
1078 nbi
.di_ctime
.tv_sec
= dp
->di_ctime
;
1079 memmove(&nbi
.di_db
, &dp
->di_db
, (NDADDR
+ NIADDR
) * sizeof(daddr_t
));
1080 nbi
.di_flags
= dp
->di_flags
;
1081 nbi
.di_blocks
= dp
->di_blocks
;
1082 nbi
.di_gen
= dp
->di_gen
;
1083 nbi
.di_uid
= (((int32_t)dp
->di_uidhigh
) << 16) | dp
->di_uid
;
1084 nbi
.di_gid
= (((int32_t)dp
->di_gidhigh
) << 16) | dp
->di_gid
;
1085 if (dp
->di_file_acl
)
1086 warn("ACLs in inode #%ld won't be dumped", (long)ino
);
1087 memmove(&spcl
.c_dinode
, &nbi
, sizeof(nbi
));
1088 #else /* __linux__ */
1089 spcl
.c_dinode
= *dp
;
1090 #endif /* __linux__ */
1091 spcl
.c_type
= TS_INODE
;
1093 switch (dp
->di_mode
& S_IFMT
) {
1104 msg("Warning: size of directory inode #%d is <= 0 (%d)!\n",
1109 msg("Warning: dumpdirino called with file type 0%o (inode #%d)\n",
1110 dp
->di_mode
& IFMT
, ino
);
1113 for (size
= 0; size
< dir_size
; size
+= TP_BSIZE
) {
1117 memmove(buf
, cdc
.buf
+ size
, TP_BSIZE
);
1119 spcl
.c_type
= TS_ADDR
;
1122 (void)free(cdc
.buf
);
1124 #endif /* __linux__ */
1128 * Read indirect blocks, and pass the data blocks to be dumped.
1131 dmpindir(dump_ino_t ino
, daddr_t blk
, int ind_level
, fsizeT
*size
)
1138 daddr_t idblk
[MAXNINDIR
];
1141 bread(fsbtodb(sblock
, blk
), (char *)idblk
, (int) sblock
->fs_bsize
);
1144 * My RedHat 4.0 system doesn't have these flags; I haven't
1145 * upgraded e2fsprogs yet
1147 #if defined(EXT2_FLAG_SWAP_BYTES)
1148 if ((fs
->flags
& EXT2_FLAG_SWAP_BYTES
) ||
1149 (fs
->flags
& EXT2_FLAG_SWAP_BYTES_READ
))
1152 max
= sblock
->fs_bsize
>> 2;
1153 swapme
= (blk_t
*) idblk
;
1154 for (i
= 0; i
< max
; i
++, swapme
++)
1155 *swapme
= ext2fs_swab32(*swapme
);
1157 #endif /* __linux__ */
1159 memset(idblk
, 0, (int)sblock
->fs_bsize
);
1160 if (ind_level
<= 0) {
1161 if (*size
< NINDIR(sblock
) * sblock
->fs_bsize
)
1162 cnt
= howmany(*size
, sblock
->fs_fsize
);
1165 cnt
= NINDIR(sblock
) * EXT2_FRAGS_PER_BLOCK(fs
->super
);
1167 cnt
= NINDIR(sblock
) * sblock
->fs_frag
;
1169 *size
-= NINDIR(sblock
) * sblock
->fs_bsize
;
1170 blksout(&idblk
[0], cnt
, ino
);
1174 for (i
= 0; i
< NINDIR(sblock
); i
++) {
1175 dmpindir(ino
, idblk
[i
], ind_level
, size
);
1183 * Collect up the data into tape record sized buffers and output them.
1186 blksout(daddr_t
*blkp
, int frags
, dump_ino_t ino
)
1189 int i
, j
, count
, blks
, tbperdb
;
1191 blks
= howmany(frags
* sblock
->fs_fsize
, TP_BSIZE
);
1192 tbperdb
= sblock
->fs_bsize
>> tp_bshift
;
1193 for (i
= 0; i
< blks
; i
+= TP_NINDIR
) {
1194 if (i
+ TP_NINDIR
> blks
)
1197 count
= i
+ TP_NINDIR
;
1198 for (j
= i
; j
< count
; j
++)
1199 if (blkp
[j
/ tbperdb
] != 0)
1200 spcl
.c_addr
[j
- i
] = 1;
1202 spcl
.c_addr
[j
- i
] = 0;
1203 spcl
.c_count
= count
- i
;
1205 bp
= &blkp
[i
/ tbperdb
];
1206 for (j
= i
; j
< count
; j
+= tbperdb
, bp
++) {
1208 if (j
+ tbperdb
<= count
)
1209 dumpblock(*bp
, (int)sblock
->fs_bsize
);
1211 dumpblock(*bp
, (count
- j
) * TP_BSIZE
);
1214 spcl
.c_type
= TS_ADDR
;
1219 * Dump a map to the tape.
1222 dumpmap(char *map
, int type
, dump_ino_t ino
)
1228 spcl
.c_count
= howmany(mapsize
* sizeof(char), TP_BSIZE
);
1230 for (i
= 0, cp
= map
; i
< spcl
.c_count
; i
++, cp
+= TP_BSIZE
)
1234 #if defined __linux__ && !defined(int32_t)
1235 #define int32_t __s32
1239 * Compute and fill in checksum information.
1242 mkchecksum(union u_spcl
*tmpspcl
)
1244 int32_t sum
, cnt
, *lp
;
1246 tmpspcl
->s_spcl
.c_checksum
= 0;
1247 lp
= (int32_t *)&tmpspcl
->s_spcl
;
1249 cnt
= sizeof(union u_spcl
) / (4 * sizeof(int32_t));
1250 while (--cnt
>= 0) {
1256 tmpspcl
->s_spcl
.c_checksum
= CHECKSUM
- sum
;
1260 * Write a header record to the dump tape.
1263 writeheader(dump_ino_t ino
)
1265 spcl
.c_inumber
= ino
;
1266 spcl
.c_magic
= NFS_MAGIC
;
1267 mkchecksum((union u_spcl
*)&spcl
);
1268 writerec((char *)&spcl
, 1);
1273 getino(dump_ino_t inum
)
1275 static struct dinode dinode
;
1279 err
= ext2fs_read_inode(fs
, (ext2_ino_t
)inum
, (struct ext2_inode
*) &dinode
);
1281 com_err(disk
, err
, "while reading inode #%ld\n", (long)inum
);
1286 #else /* __linux__ */
1288 getino(dump_ino_t inum
)
1290 static daddr_t minino
, maxino
;
1291 static struct dinode inoblock
[MAXINOPB
];
1294 if (inum
>= minino
&& inum
< maxino
)
1295 return (&inoblock
[inum
- minino
]);
1296 bread(fsbtodb(sblock
, ino_to_fsba(sblock
, inum
)), (char *)inoblock
,
1297 (int)sblock
->fs_bsize
);
1298 minino
= inum
- (inum
% INOPB(sblock
));
1299 maxino
= minino
+ INOPB(sblock
);
1300 return (&inoblock
[inum
- minino
]);
1302 #endif /* __linux__ */
1305 * Read a chunk of data from the disk.
1306 * Try to recover from hard errors by reading in sector sized pieces.
1307 * Error recovery is attempted at most breademax times before seeking
1308 * consent from the operator to continue.
1310 int breaderrors
= 0;
1313 bread(daddr_t blkno
, char *buf
, int size
)
1319 if (ext2fs_llseek(diskfd
, (((ext2_loff_t
)blkno
) << dev_bshift
), 0) !=
1320 (((ext2_loff_t
)blkno
) << dev_bshift
))
1322 if (lseek(diskfd
, ((off_t
)blkno
<< dev_bshift
), 0) !=
1323 ((off_t
)blkno
<< dev_bshift
))
1325 msg("bread: lseek fails\n");
1326 if ((cnt
= read(diskfd
, buf
, size
)) == size
)
1328 if (blkno
+ (size
/ dev_bsize
) > fsbtodb(sblock
, sblock
->fs_size
)) {
1330 * Trying to read the final fragment.
1332 * NB - dump only works in TP_BSIZE blocks, hence
1333 * rounds `dev_bsize' fragments up to TP_BSIZE pieces.
1334 * It should be smarter about not actually trying to
1335 * read more than it can get, but for the time being
1336 * we punt and scale back the read only when it gets
1337 * us into trouble. (mkm 9/25/83)
1343 msg("read error from %s: %s: [block %d, ext2blk %d]: count=%d\n",
1344 disk
, strerror(errno
), blkno
,
1345 dbtofsb(sblock
, blkno
), size
);
1347 msg("short read error from %s: [block %d, ext2blk %d]: count=%d, got=%d\n",
1348 disk
, blkno
, dbtofsb(sblock
, blkno
), size
, cnt
);
1349 if (++breaderrors
> breademax
) {
1350 msg("More than %d block read errors from %d\n",
1352 broadcast("DUMP IS AILING!\n");
1353 msg("This is an unrecoverable error.\n");
1354 if (!query("Do you want to attempt to continue?")){
1361 * Zero buffer, then try to read each sector of buffer separately.
1363 memset(buf
, 0, size
);
1364 for (i
= 0; i
< size
; i
+= dev_bsize
, buf
+= dev_bsize
, blkno
++) {
1366 if (ext2fs_llseek(diskfd
, (((ext2_loff_t
)blkno
) << dev_bshift
), 0) !=
1367 (((ext2_loff_t
)blkno
) << dev_bshift
))
1369 if (lseek(diskfd
, ((off_t
)blkno
<< dev_bshift
), 0) !=
1370 ((off_t
)blkno
<< dev_bshift
))
1372 msg("bread: lseek2 fails!\n");
1373 if ((cnt
= read(diskfd
, buf
, (int)dev_bsize
)) == dev_bsize
)
1376 msg("read error from %s: %s: [sector %d, ext2blk %d]: count=%d\n",
1377 disk
, strerror(errno
), blkno
,
1378 dbtofsb(sblock
, blkno
), dev_bsize
);
1381 msg("short read error from %s: [sector %d, ext2blk %d]: count=%d, got=%d\n",
1382 disk
, blkno
, dbtofsb(sblock
, blkno
), dev_bsize
, cnt
);