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. Neither the name of the University nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 static const char rcsid[] =
40 "$Id: traverse.c,v 1.66 2005/05/02 15:10:46 stelian Exp $";
46 #include <sys/types.h>
53 #include <sys/param.h>
56 #ifdef HAVE_EXT2FS_EXT2_FS_H
57 #include <ext2fs/ext2_fs.h>
59 #include <linux/ext2_fs.h>
61 #include <ext2fs/ext2fs.h>
62 #include <bsdcompat.h>
63 #include <compaterr.h>
66 #include <sys/vnode.h>
69 #include <ufs/fsdir.h>
70 #include <ufs/inode.h>
72 #include <ufs/ufs/dir.h>
73 #include <ufs/ufs/dinode.h>
74 #include <ufs/ffs/fs.h>
75 #endif /* __linux__ */
77 #include <protocols/dumprestore.h>
81 #define HASDUMPEDFILE 0x1
82 #define HASSUBDIRS 0x2
85 typedef u_quad_t fsizeT;
88 typedef quad_t fsizeT;
95 static int searchdir __P((struct ext2_dir_entry *dp, int offset,
96 int blocksize, char *buf, void *private));
98 static int dirindir __P((dump_ino_t ino, daddr_t blkno, int level, long *size));
99 static void dmpindir __P((dump_ino_t ino, daddr_t blk, int level, fsizeT *size));
100 static int searchdir __P((dump_ino_t ino, daddr_t blkno, long size, long filesize));
102 static void mapfileino __P((dump_ino_t ino, struct dinode const *dp, long *tapesize, int *dirskipped));
103 static void dump_xattr __P((dump_ino_t ino, struct dinode *dp));
105 #ifdef HAVE_EXT2_JOURNAL_INUM
106 #define ext2_journal_ino(sb) (sb->s_journal_inum)
108 #define ext2_journal_ino(sb) (*((u_int32_t *)sb + 0x38))
110 #ifndef HAVE_EXT2_INO_T
111 typedef ino_t ext2_ino_t;
114 #ifndef EXT3_FEATURE_COMPAT_HAS_JOURNAL
115 #define EXT3_FEATURE_COMPAT_HAS_JOURNAL 0x0004
117 #ifndef EXT2_FEATURE_INCOMPAT_FILETYPE
118 #define EXT2_FEATURE_INCOMPAT_FILETYPE 0x0002
120 #ifndef EXT3_FEATURE_INCOMPAT_RECOVER
121 #define EXT3_FEATURE_INCOMPAT_RECOVER 0x0004
123 #ifndef EXT3_FEATURE_INCOMPAT_JOURNAL_DEV
124 #define EXT3_FEATURE_INCOMPAT_JOURNAL_DEV 0x0008
127 #ifndef EXT2_LIB_FEATURE_INCOMPAT_SUPP
128 #define EXT2_LIB_FEATURE_INCOMPAT_SUPP (EXT3_FEATURE_INCOMPAT_RECOVER | \
129 EXT2_FEATURE_INCOMPAT_FILETYPE)
131 #ifndef EXT2_RESIZE_INO
132 #define EXT2_RESIZE_INO 7
134 #ifndef EXT2_FT_UNKNOWN
135 #define EXT2_FT_UNKNOWN 0
136 #define EXT2_FT_REG_FILE 1
137 #define EXT2_FT_DIR 2
138 #define EXT2_FT_CHRDEV 3
139 #define EXT2_FT_BLKDEV 4
140 #define EXT2_FT_FIFO 5
141 #define EXT2_FT_SOCK 6
142 #define EXT2_FT_SYMLINK 7
143 #define EXT2_FT_MAX 8
146 int dump_fs_open(const char *disk, ext2_filsys *fs)
150 retval = ext2fs_open(disk, EXT2_FLAG_FORCE, 0, 0, unix_io_manager, fs);
152 struct ext2_super_block *es = (*fs)->super;
153 dump_ino_t journal_ino = ext2_journal_ino(es);
155 if (es->s_feature_incompat & EXT3_FEATURE_INCOMPAT_JOURNAL_DEV){
156 msg("This is a journal, not a filesystem!\n");
157 retval = EXT2_ET_UNSUPP_FEATURE;
160 else if ((retval = es->s_feature_incompat &
161 ~(EXT2_LIB_FEATURE_INCOMPAT_SUPP |
162 EXT3_FEATURE_INCOMPAT_RECOVER))) {
163 msg("Unsupported feature(s) 0x%x in filesystem\n",
165 retval = EXT2_ET_UNSUPP_FEATURE;
169 if (es->s_feature_compat &
170 EXT3_FEATURE_COMPAT_HAS_JOURNAL &&
172 do_exclude_ino(journal_ino, "journal inode");
173 do_exclude_ino(EXT2_RESIZE_INO, "resize inode");
180 * This is an estimation of the number of TP_BSIZE blocks in the file.
181 * It estimates the number of blocks in files with holes by assuming
182 * that all of the blocks accounted for by di_blocks are data blocks
183 * (when some of the blocks are usually used for indirect pointers);
184 * hence the estimate may be high.
187 blockest(struct dinode const *dp)
189 long blkest, sizeest;
193 * dp->di_size is the size of the file in bytes.
194 * dp->di_blocks stores the number of sectors actually in the file.
195 * If there are more sectors than the size would indicate, this just
196 * means that there are indirect blocks in the file or unused
197 * sectors in the last file block; we can safely ignore these
198 * (blkest = sizeest below).
199 * If the file is bigger than the number of sectors would indicate,
200 * then the file has holes in it. In this case we must use the
201 * block count to estimate the number of data blocks used, but
202 * we use the actual size for estimating the number of indirect
203 * dump blocks (sizeest vs. blkest in the indirect block
206 blkest = howmany((u_quad_t)dp->di_blocks * 512, fs->blocksize) * (fs->blocksize / TP_BSIZE);
207 i_size = dp->di_size + ((u_quad_t) dp->di_size_high << 32);
208 sizeest = howmany(i_size, fs->blocksize) * (fs->blocksize / TP_BSIZE);
209 if (blkest > sizeest)
212 if ((dp->di_mode & IFMT) == IFDIR) {
214 * for directories, assume only half of space is filled
218 sizeest = sizeest / 2;
220 if (i_size > (u_quad_t)fs->blocksize * NDADDR) {
221 /* calculate the number of indirect blocks on the dump tape */
223 howmany(sizeest - NDADDR * fs->blocksize / TP_BSIZE,
227 if (i_size > sblock->fs_bsize * NDADDR) {
228 /* calculate the number of indirect blocks on the dump tape */
230 howmany(sizeest - NDADDR * sblock->fs_bsize / TP_BSIZE,
237 /* Auxiliary macro to pick up files changed since previous dump. */
238 #define CSINCE(dp, t) \
239 ((dp)->di_ctime >= (t))
240 #define MSINCE(dp, t) \
241 ((dp)->di_mtime >= (t))
242 #define CHANGEDSINCE(dp, t) \
243 (CSINCE(dp, t) || MSINCE(dp, t))
245 /* The NODUMP_FLAG macro tests if a file has the nodump flag. */
247 #define NODUMP_FLAG(dp) (!nonodump && (((dp)->di_flags & UF_NODUMP) == UF_NODUMP))
249 #define NODUMP_FLAG(dp) 0
252 /* The WANTTODUMP macro decides whether a file should be dumped. */
253 #define WANTTODUMP(dp, ino) \
254 (CHANGEDSINCE(dp, ((u_int32_t)spcl.c_ddate)) && \
255 (!NODUMP_FLAG(dp)) && \
259 * Determine if given inode should be dumped. "dp" must either point to a
260 * copy of the given inode, or be NULL (in which case it is fetched.)
263 mapfileino(dump_ino_t ino, struct dinode const *dp, long *tapesize, int *dirskipped)
268 * Skip inode if we've already marked it for dumping
270 if (TSTINO(ino, usedinomap))
274 if ((mode = (dp->di_mode & IFMT)) == 0)
277 if (dp->di_nlink == 0 || dp->di_dtime != 0)
281 * Put all dirs in dumpdirmap, inodes that are to be dumped in the
282 * used map. All inode but dirs who have the nodump attribute go
285 SETINO(ino, usedinomap);
288 do_exclude_ino(ino, "nodump attribute");
291 SETINO(ino, dumpdirmap);
292 if (WANTTODUMP(dp, ino)) {
293 SETINO(ino, dumpinomap);
294 if (!MSINCE(dp, (u_int32_t)spcl.c_ddate))
295 SETINO(ino, metainomap);
296 if (mode != IFREG && mode != IFDIR && mode != IFLNK)
299 *tapesize += blockest(dp);
303 if (exclude_ino(ino))
304 CLRINO(ino, usedinomap);
312 * Walk the inode list for a filesystem to find all allocated inodes
313 * that have been modified since the previous dump time. Also, find all
314 * the directories in the filesystem.
318 mapfiles(UNUSED(dump_ino_t maxino), long *tapesize)
321 int anydirskipped = 0;
322 ext2_inode_scan scan;
324 struct ext2_inode inode;
327 * We use libext2fs's inode scanning routines, which are particularly
328 * robust. (Note that getino cannot return an error.)
330 err = ext2fs_open_inode_scan(fs, 0, &scan);
332 com_err(disk, err, "while opening inodes\n");
336 err = ext2fs_get_next_inode(scan, &ino, &inode);
337 if (err == EXT2_ET_BAD_BLOCK_IN_INODE_TABLE)
340 com_err(disk, err, "while scanning inode #%ld\n",
348 mapfileino(ino, (struct dinode const *)&inode, tapesize,
351 ext2fs_close_inode_scan(scan);
354 * Restore gets very upset if the root is not dumped,
355 * so ensure that it always is dumped.
357 SETINO(ROOTINO, dumpinomap);
358 return (anydirskipped);
362 mapfiles(dump_ino_t maxino, long *tapesize)
365 int anydirskipped = 0;
367 for (ino = ROOTINO; ino < maxino; ino++)
368 mapfileino(ino, tapesize, &anydirskipped);
371 * Restore gets very upset if the root is not dumped,
372 * so ensure that it always is dumped.
374 SETINO(ROOTINO, dumpinomap);
375 return (anydirskipped);
377 #endif /* __linux__ */
381 maponefile(UNUSED(dump_ino_t maxino), long *tapesize, char *directory)
385 char dir_name [MAXPATHLEN];
386 int i, anydirskipped = 0;
389 * Mark every directory in the path as being dumped
391 for (i = 0; i < (int)strlen (directory); i++) {
392 if (directory[i] == '/') {
393 strncpy (dir_name, directory, i);
395 retval = ext2fs_namei(fs, ROOTINO, ROOTINO,
398 com_err(disk, retval,
399 "while translating %s", dir_name);
402 mapfileino((dump_ino_t) dir_ino, 0,
403 tapesize, &anydirskipped);
407 * Mark the final directory
409 retval = ext2fs_namei(fs, ROOTINO, ROOTINO, directory, &dir_ino);
411 com_err(disk, retval, "while translating %s", directory);
414 mapfileino((dump_ino_t)dir_ino, 0, tapesize, &anydirskipped);
416 mapfileino(ROOTINO, 0, tapesize, &anydirskipped);
419 * Restore gets very upset if the root is not dumped,
420 * so ensure that it always is dumped.
422 SETINO(ROOTINO, dumpdirmap);
423 return anydirskipped;
425 #endif /* __linux__ */
428 struct mapfile_context {
434 mapfilesindir(struct ext2_dir_entry *dirent, UNUSED(int offset),
435 UNUSED(int blocksize), UNUSED(char *buf), void *private)
437 struct dinode const *dp;
440 struct mapfile_context *mfc;
444 mfc = (struct mapfile_context *)private;
445 dp = getino(dirent->inode);
447 mapfileino(dirent->inode, dp, mfc->tapesize, mfc->anydirskipped);
449 mode = dp->di_mode & IFMT;
450 if (mode == IFDIR && dp->di_nlink != 0 && dp->di_dtime == 0) {
451 if ((dirent->name[0] != '.' || ( dirent->name_len & 0xFF ) != 1) &&
452 (dirent->name[0] != '.' || dirent->name[1] != '.' ||
453 ( dirent->name_len & 0xFF ) != 2)) {
454 retval = ext2fs_dir_iterate(fs, ino, 0, NULL,
455 mapfilesindir, private);
466 * Walk the inode list for a filesystem to find all allocated inodes
467 * that have been modified since the previous dump time. Also, find all
468 * the directories in the filesystem.
471 mapfilesfromdir(UNUSED(dump_ino_t maxino), long *tapesize, char *directory)
474 struct mapfile_context mfc;
476 char dir_name [MAXPATHLEN];
477 int i, anydirskipped = 0;
480 * Mark every directory in the path as being dumped
482 for (i = 0; i < (int)strlen (directory); i++) {
483 if (directory[i] == '/') {
484 strncpy (dir_name, directory, i);
486 retval = ext2fs_namei(fs, ROOTINO, ROOTINO, dir_name,
489 com_err(disk, retval, "while translating %s",
493 mapfileino(dir_ino, 0, tapesize, &anydirskipped);
497 * Mark the final directory
499 retval = ext2fs_namei(fs, ROOTINO, ROOTINO, directory, &dir_ino);
501 com_err(disk, retval, "while translating %s", directory);
504 mapfileino(dir_ino, 0, tapesize, &anydirskipped);
506 mfc.tapesize = tapesize;
507 mfc.anydirskipped = &anydirskipped;
508 retval = ext2fs_dir_iterate(fs, dir_ino, 0, NULL, mapfilesindir,
512 com_err(disk, retval, "while mapping files in %s", directory);
516 * Ensure that the root inode actually appears in the file list
519 mapfileino(ROOTINO, 0, tapesize, &anydirskipped);
521 * Restore gets very upset if the root is not dumped,
522 * so ensure that it always is dumped.
524 SETINO(ROOTINO, dumpinomap);
525 return anydirskipped;
530 struct mapdirs_context {
540 * Scan each directory on the filesystem to see if it has any modified
541 * files in it. If it does, and has not already been added to the dump
542 * list (because it was itself modified), then add it. If a directory
543 * has not been modified itself, contains no modified files and has no
544 * subdirectories, then it can be deleted from the dump list and from
545 * the list of directories. By deleting it from the list of directories,
546 * its parent may now qualify for the same treatment on this or a later
547 * pass using this algorithm.
550 mapdirs(dump_ino_t maxino, long *tapesize)
560 struct mapdirs_context mdc;
562 int ret, change = 0, nodump;
564 isdir = 0; /* XXX just to get gcc to shut up */
565 for (map = dumpdirmap, ino = 1; ino < maxino; ino++) {
566 if (((ino - 1) % NBBY) == 0) /* map is offset by 1 */
571 * If dir has been removed from the used map, it's either
572 * because it had the nodump flag, or it herited it from
573 * its parent. A directory can't be in dumpinomap if not
574 * in usedinomap, but we have to go through it anyway
575 * to propagate the nodump attribute.
577 nodump = (TSTINO(ino, usedinomap) == 0);
578 if ((isdir & 1) == 0 ||
579 (TSTINO(ino, dumpinomap) && nodump == 0))
586 mdc.tapesize = tapesize;
587 ext2fs_dir_iterate(fs, ino, 0, NULL, searchdir, (void *) &mdc);
588 #else /* __linux__ */
589 filesize = dp->di_size;
590 for (ret = 0, i = 0; filesize > 0 && i < NDADDR; i++) {
591 if (dp->di_db[i] != 0)
592 ret |= searchdir(ino, dp->di_db[i],
593 (long)dblksize(sblock, dp, i),
595 if (ret & HASDUMPEDFILE)
598 filesize -= sblock->fs_bsize;
600 for (i = 0; filesize > 0 && i < NIADDR; i++) {
601 if (dp->di_ib[i] == 0)
603 ret |= dirindir(ino, dp->di_ib[i], i, &filesize);
605 #endif /* __linux__ */
606 if (ret & HASDUMPEDFILE) {
607 SETINO(ino, dumpinomap);
608 *tapesize += blockest(dp);
613 if (ret & HASSUBDIRS)
614 change = 1; /* subdirs have inherited nodump */
615 CLRINO(ino, dumpdirmap);
616 } else if ((ret & HASSUBDIRS) == 0) {
617 if (!TSTINO(ino, dumpinomap)) {
618 CLRINO(ino, dumpdirmap);
628 * Read indirect blocks, and pass the data blocks to be searched
629 * as directories. Quit as soon as any entry is found that will
630 * require the directory to be dumped.
633 dirindir(dump_ino_t ino, daddr_t blkno, int ind_level, long *filesize)
637 daddr_t idblk[MAXNINDIR];
639 bread(fsbtodb(sblock, blkno), (char *)idblk, (int)sblock->fs_bsize);
640 if (ind_level <= 0) {
641 for (i = 0; *filesize > 0 && i < NINDIR(sblock); i++) {
644 ret |= searchdir(ino, blkno, sblock->fs_bsize,
646 if (ret & HASDUMPEDFILE)
649 *filesize -= sblock->fs_bsize;
654 for (i = 0; *filesize > 0 && i < NINDIR(sblock); i++) {
657 ret |= dirindir(ino, blkno, ind_level, filesize);
661 #endif /* !__linux__ */
664 * Scan a disk block containing directory information looking to see if
665 * any of the entries are on the dump list and to see if the directory
666 * contains any subdirectories.
670 searchdir(struct ext2_dir_entry *dp, UNUSED(int offset),
671 UNUSED(int blocksize), UNUSED(char *buf), void *private)
673 struct mapdirs_context *mdc;
678 mdc = (struct mapdirs_context *)private;
680 tapesize = mdc->tapesize;
684 if (dp->name[0] == '.') {
685 if (( dp->name_len & 0xFF ) == 1)
687 if (dp->name[1] == '.' && ( dp->name_len & 0xFF ) == 2)
691 ip = getino(dp->inode);
692 if (TSTINO(dp->inode, dumpinomap)) {
693 CLRINO(dp->inode, dumpinomap);
694 *tapesize -= blockest(ip);
696 /* Add dir back to the dir map and remove from
697 * usedinomap to propagate nodump */
698 if ((ip->di_mode & IFMT) == IFDIR) {
699 SETINO(dp->inode, dumpdirmap);
700 CLRINO(dp->inode, usedinomap);
704 if (TSTINO(dp->inode, dumpinomap)) {
705 *ret |= HASDUMPEDFILE;
706 if (*ret & HASSUBDIRS)
709 if (TSTINO(dp->inode, dumpdirmap)) {
711 if (*ret & HASDUMPEDFILE)
718 #else /* __linux__ */
721 searchdir(dump_ino_t ino, daddr_t blkno, long size, long filesize)
727 bread(fsbtodb(sblock, blkno), dblk, (int)size);
730 for (loc = 0; loc < size; ) {
731 dp = (struct direct *)(dblk + loc);
732 if (dp->d_reclen == 0) {
733 msg("corrupted directory, inumber %d\n", ino);
739 if (dp->d_name[0] == '.') {
740 if (dp->d_name[1] == '\0')
742 if (dp->d_name[1] == '.' && dp->d_name[2] == '\0')
745 if (TSTINO(dp->d_ino, dumpinomap)) {
746 ret |= HASDUMPEDFILE;
747 if (ret & HASSUBDIRS)
750 if (TSTINO(dp->d_ino, dumpdirmap)) {
752 if (ret & HASDUMPEDFILE)
758 #endif /* __linux__ */
762 struct block_context {
771 * Dump a block to the tape
774 dumponeblock(UNUSED(ext2_filsys fs), blk_t *blocknr, e2_blkcnt_t blockcnt,
775 UNUSED(blk_t ref_block), UNUSED(int ref_offset), void * private)
777 struct block_context *p;
780 if (blockcnt < NDADDR)
782 p = (struct block_context *)private;
783 for (i = p->next_block; i < blockcnt; i++) {
784 p->buf[p->cnt++] = 0;
785 if (p->cnt == p->max) {
786 blksout (p->buf, p->cnt, p->ino);
790 p->buf[p->cnt++] = *blocknr;
791 if (p->cnt == p->max) {
792 blksout (p->buf, p->cnt, p->ino);
795 p->next_block = blockcnt + 1;
801 dump_xattr(dump_ino_t ino, struct dinode *dp) {
803 if (dp->di_extraisize != 0) {
804 #ifdef HAVE_EXT2FS_READ_INODE_FULL
805 char inode[EXT2_INODE_SIZE(fs->super)];
809 memset(inode, 0, EXT2_INODE_SIZE(fs->super));
810 err = ext2fs_read_inode_full(fs, (ext2_ino_t)ino,
811 (struct ext2_inode *) inode,
812 EXT2_INODE_SIZE(fs->super));
814 com_err(disk, err, "while reading inode #%ld\n", (long)ino);
818 magic = (void *)inode + EXT2_GOOD_OLD_INODE_SIZE + dp->di_extraisize;
819 if (*magic == EXT2_XATTR_MAGIC) {
820 char xattr[EXT2_INODE_SIZE(fs->super)];
825 msg("dumping EA (inode) in inode #%ld\n", (long)ino);
826 memset(xattr, 0, EXT2_INODE_SIZE(fs->super));
827 memcpy(xattr, (void *)magic,
828 EXT2_INODE_SIZE(fs->super) -
829 (EXT2_GOOD_OLD_INODE_SIZE + dp->di_extraisize));
830 magic = (u_int32_t *)xattr;
831 *magic = EXT2_XATTR_MAGIC2;
833 spcl.c_type = TS_INODE;
834 spcl.c_dinode.di_size = EXT2_INODE_SIZE(fs->super);
835 spcl.c_flags |= DR_EXTATTRIBUTES;
836 spcl.c_extattributes = EXT_XATTR;
837 spcl.c_count = howmany(EXT2_INODE_SIZE(fs->super), TP_BSIZE);
839 for (i = 0, cp = xattr; i < spcl.c_count; i++, cp += TP_BSIZE)
841 spcl.c_flags &= ~DR_EXTATTRIBUTES;
842 spcl.c_extattributes = 0;
847 if (dp->di_file_acl) {
850 msg("dumping EA (block) in inode #%ld\n", (long)ino);
852 spcl.c_type = TS_INODE;
853 spcl.c_dinode.di_size = sblock->fs_bsize;
854 spcl.c_flags |= DR_EXTATTRIBUTES;
855 spcl.c_extattributes = EXT_XATTR;
856 blksout(&dp->di_file_acl, EXT2_FRAGS_PER_BLOCK(fs->super), ino);
857 spcl.c_flags &= ~DR_EXTATTRIBUTES;
858 spcl.c_extattributes = 0;
863 * Dump passes 3 and 4.
865 * Dump the contents of an inode to tape.
868 dumpino(struct dinode *dp, dump_ino_t ino, int metaonly)
871 fsizeT size, remaining;
873 struct new_bsd_inode nbi;
876 struct block_context bc;
885 i_size = dp->di_size + ((u_quad_t) dp->di_size_high << 32);
889 dumpmap(dumpinomap, TS_BITS, ino);
891 CLRINO(ino, dumpinomap);
893 memset(&nbi, 0, sizeof(nbi));
894 nbi.di_mode = dp->di_mode;
895 nbi.di_nlink = dp->di_nlink;
896 nbi.di_ouid = dp->di_uid;
897 nbi.di_ogid = dp->di_gid;
898 nbi.di_size = i_size;
899 nbi.di_atime.tv_sec = dp->di_atime;
900 nbi.di_mtime.tv_sec = dp->di_mtime;
901 nbi.di_ctime.tv_sec = dp->di_ctime;
902 memmove(&nbi.di_db, &dp->di_db, (NDADDR + NIADDR) * sizeof(daddr_t));
903 nbi.di_flags = dp->di_flags;
904 nbi.di_blocks = dp->di_blocks;
905 nbi.di_gen = dp->di_gen;
906 nbi.di_uid = (((int32_t)dp->di_uidhigh) << 16) | dp->di_uid;
907 nbi.di_gid = (((int32_t)dp->di_gidhigh) << 16) | dp->di_gid;
908 memmove(&spcl.c_dinode, &nbi, sizeof(nbi));
909 #else /* __linux__ */
911 #endif /* __linux__ */
912 spcl.c_type = TS_INODE;
915 if (metaonly && (dp->di_mode & S_IFMT)) {
916 spcl.c_flags |= DR_METAONLY;
919 spcl.c_flags &= ~DR_METAONLY;
924 switch (dp->di_mode & S_IFMT) {
934 msg("Warning: dumpino called on a directory (ino %d)\n", ino);
940 * Check for short symbolic link.
944 i_size < EXT2_N_BLOCKS * sizeof (daddr_t)) {
948 memmove(buf, dp->di_db, (u_long)dp->di_size);
949 buf[dp->di_size] = '\0';
954 #endif /* __linux__ */
956 if (dp->di_size > 0 &&
957 dp->di_size < sblock->fs_maxsymlinklen) {
961 memmove(buf, dp->di_shortlink, (u_long)dp->di_size);
962 buf[dp->di_size] = '\0';
986 msg("Warning: undefined file type 0%o\n", dp->di_mode & IFMT);
989 if (i_size > (u_quad_t)NDADDR * sblock->fs_bsize)
991 cnt = NDADDR * EXT2_FRAGS_PER_BLOCK(fs->super);
993 cnt = NDADDR * sblock->fs_frag;
996 cnt = howmany(i_size, sblock->fs_fsize);
997 blksout(&dp->di_db[0], cnt, ino);
998 if ((quad_t) (size = i_size - NDADDR * sblock->fs_bsize) <= 0) {
1003 bc.max = NINDIR(sblock) * EXT2_FRAGS_PER_BLOCK(fs->super);
1004 bc.buf = (int *)malloc (bc.max * sizeof (int));
1007 bc.next_block = NDADDR;
1009 ext2fs_block_iterate2(fs, (ext2_ino_t)ino, 0, NULL, dumponeblock, (void *)&bc);
1010 /* deal with holes at the end of the inode */
1011 if (i_size > ((u_quad_t)bc.next_block) * sblock->fs_fsize) {
1012 remaining = i_size - ((u_quad_t)bc.next_block) * sblock->fs_fsize;
1013 for (i = 0; i < (int)howmany(remaining, sblock->fs_fsize); i++) {
1014 bc.buf[bc.cnt++] = 0;
1015 if (bc.cnt == bc.max) {
1016 blksout (bc.buf, bc.cnt, bc.ino);
1022 blksout (bc.buf, bc.cnt, bc.ino);
1025 dump_xattr(ino, dp);
1027 for (ind_level = 0; ind_level < NIADDR; ind_level++) {
1028 dmpindir(ino, dp->di_ib[ind_level], ind_level, &size);
1037 struct convert_dir_context {
1045 * This function converts an ext2fs directory entry to the BSD format.
1047 * Basically, it adds a null-character at the end of the name, recomputes the
1048 * size of the entry, and creates it in a temporary buffer
1051 convert_dir(struct ext2_dir_entry *dirent, UNUSED(int offset),
1052 UNUSED(int blocksize), UNUSED(char *buf), void *private)
1054 struct convert_dir_context *p;
1058 /* do not save entries to excluded inodes */
1059 if (exclude_ino(dirent->inode))
1062 p = (struct convert_dir_context *)private;
1064 reclen = EXT2_DIR_REC_LEN((dirent->name_len & 0xFF) + 1);
1065 if (((p->offset + reclen - 1) / p->bs) != (p->offset / p->bs)) {
1066 dp = (struct direct *)(p->buf + p->prev_offset);
1067 dp->d_reclen += p->bs - (p->offset % p->bs);
1068 p->offset += p->bs - (p->offset % p->bs);
1071 dp = (struct direct *)(p->buf + p->offset);
1072 dp->d_ino = dirent->inode;
1073 dp->d_reclen = reclen;
1074 dp->d_namlen = dirent->name_len & 0xFF;
1075 switch ((dirent->name_len & 0xFF00) >> 8) {
1077 dp->d_type = DT_UNKNOWN;
1079 case EXT2_FT_REG_FILE:
1080 dp->d_type = DT_REG;
1083 dp->d_type = DT_DIR;
1085 case EXT2_FT_CHRDEV:
1086 dp->d_type = DT_CHR;
1088 case EXT2_FT_BLKDEV:
1089 dp->d_type = DT_BLK;
1092 dp->d_type = DT_FIFO;
1095 dp->d_type = DT_SOCK;
1097 case EXT2_FT_SYMLINK:
1098 dp->d_type = DT_LNK;
1101 strncpy(dp->d_name, dirent->name, dp->d_namlen);
1102 dp->d_name[dp->d_namlen] = '\0';
1103 p->prev_offset = p->offset;
1104 p->offset += reclen;
1112 * Dumps a directory to tape after converting it to the BSD format
1115 dumpdirino(struct dinode *dp, dump_ino_t ino)
1119 struct new_bsd_inode nbi;
1120 struct convert_dir_context cdc;
1122 struct ext2_dir_entry *de;
1127 dumpmap(dumpinomap, TS_BITS, ino);
1129 CLRINO(ino, dumpinomap);
1132 * Convert the directory to the BSD format
1134 /* Allocate a buffer for the conversion (twice the size of the
1135 ext2fs directory to avoid problems ;-) */
1136 cdc.buf = (char *)malloc(dp->di_size * 2 * sizeof(char));
1137 if (cdc.buf == NULL)
1138 err(1, "Cannot allocate buffer to convert directory #%lu\n",
1139 (unsigned long)ino);
1141 cdc.prev_offset = 0;
1142 cdc.bs = MIN(DIRBLKSIZ, TP_BSIZE);
1143 /* Do the conversion */
1144 retval = ext2fs_dir_iterate(fs, (ext2_ino_t)ino, 0, NULL, convert_dir, (void *)&cdc);
1146 com_err(disk, retval, "while converting directory #%ld\n", (long)ino);
1149 /* Fix the last entry */
1150 if ((cdc.offset % cdc.bs) != 0) {
1151 de = (struct ext2_dir_entry *)(cdc.buf + cdc.prev_offset);
1152 de->rec_len += cdc.bs - (cdc.offset % cdc.bs);
1153 cdc.offset += cdc.bs - (cdc.offset % cdc.bs);
1156 dir_size = cdc.offset;
1159 memset(&nbi, 0, sizeof(nbi));
1160 nbi.di_mode = dp->di_mode;
1161 nbi.di_nlink = dp->di_nlink;
1162 nbi.di_ouid = dp->di_uid;
1163 nbi.di_ogid = dp->di_gid;
1164 nbi.di_size = dir_size; /* (u_quad_t)dp->di_size; */
1165 nbi.di_atime.tv_sec = dp->di_atime;
1166 nbi.di_mtime.tv_sec = dp->di_mtime;
1167 nbi.di_ctime.tv_sec = dp->di_ctime;
1168 memmove(&nbi.di_db, &dp->di_db, (NDADDR + NIADDR) * sizeof(daddr_t));
1169 nbi.di_flags = dp->di_flags;
1170 nbi.di_blocks = dp->di_blocks;
1171 nbi.di_gen = dp->di_gen;
1172 nbi.di_uid = (((int32_t)dp->di_uidhigh) << 16) | dp->di_uid;
1173 nbi.di_gid = (((int32_t)dp->di_gidhigh) << 16) | dp->di_gid;
1174 memmove(&spcl.c_dinode, &nbi, sizeof(nbi));
1175 #else /* __linux__ */
1176 spcl.c_dinode = *dp;
1177 #endif /* __linux__ */
1178 spcl.c_type = TS_INODE;
1180 switch (dp->di_mode & S_IFMT) {
1191 msg("Warning: size of directory inode #%d is <= 0 (%d)!\n",
1196 msg("Warning: dumpdirino called with file type 0%o (inode #%d)\n",
1197 dp->di_mode & IFMT, ino);
1200 for (size = 0; size < dir_size; size += TP_BSIZE) {
1204 memmove(buf, cdc.buf + size, TP_BSIZE);
1206 spcl.c_type = TS_ADDR;
1209 (void)free(cdc.buf);
1210 dump_xattr(ino, dp);
1212 #endif /* __linux__ */
1216 * Read indirect blocks, and pass the data blocks to be dumped.
1219 dmpindir(dump_ino_t ino, daddr_t blk, int ind_level, fsizeT *size)
1226 daddr_t idblk[MAXNINDIR];
1229 bread(fsbtodb(sblock, blk), (char *)idblk, (int) sblock->fs_bsize);
1232 * My RedHat 4.0 system doesn't have these flags; I haven't
1233 * upgraded e2fsprogs yet
1235 #if defined(EXT2_FLAG_SWAP_BYTES)
1236 if ((fs->flags & EXT2_FLAG_SWAP_BYTES) ||
1237 (fs->flags & EXT2_FLAG_SWAP_BYTES_READ))
1240 max = sblock->fs_bsize >> 2;
1241 swapme = (blk_t *) idblk;
1242 for (i = 0; i < max; i++, swapme++)
1243 *swapme = ext2fs_swab32(*swapme);
1245 #endif /* __linux__ */
1247 memset(idblk, 0, (int)sblock->fs_bsize);
1248 if (ind_level <= 0) {
1249 if (*size < NINDIR(sblock) * sblock->fs_bsize)
1250 cnt = howmany(*size, sblock->fs_fsize);
1253 cnt = NINDIR(sblock) * EXT2_FRAGS_PER_BLOCK(fs->super);
1255 cnt = NINDIR(sblock) * sblock->fs_frag;
1257 *size -= NINDIR(sblock) * sblock->fs_bsize;
1258 blksout(&idblk[0], cnt, ino);
1262 for (i = 0; i < NINDIR(sblock); i++) {
1263 dmpindir(ino, idblk[i], ind_level, size);
1271 * Collect up the data into tape record sized buffers and output them.
1274 blksout(blk_t *blkp, int frags, dump_ino_t ino)
1277 int i, j, count, blks, tbperdb;
1279 blks = howmany(frags * sblock->fs_fsize, TP_BSIZE);
1280 tbperdb = sblock->fs_bsize >> tp_bshift;
1281 for (i = 0; i < blks; i += TP_NINDIR) {
1282 if (i + TP_NINDIR > blks)
1285 count = i + TP_NINDIR;
1286 for (j = i; j < count; j++)
1287 if (blkp[j / tbperdb] != 0)
1288 spcl.c_addr[j - i] = 1;
1290 spcl.c_addr[j - i] = 0;
1291 spcl.c_count = count - i;
1293 bp = &blkp[i / tbperdb];
1294 for (j = i; j < count; j += tbperdb, bp++) {
1296 if (j + tbperdb <= count)
1297 dumpblock(*bp, (int)sblock->fs_bsize);
1299 dumpblock(*bp, (count - j) * TP_BSIZE);
1302 spcl.c_type = TS_ADDR;
1307 * Dump a map to the tape.
1310 dumpmap(char *map, int type, dump_ino_t ino)
1316 spcl.c_count = howmany(mapsize * sizeof(char), TP_BSIZE);
1317 spcl.c_dinode.di_size = mapsize;
1319 for (i = 0, cp = map; i < spcl.c_count; i++, cp += TP_BSIZE)
1323 #if defined(__linux__) && !defined(int32_t)
1324 #define int32_t __s32
1328 * Compute and fill in checksum information.
1331 mkchecksum(union u_spcl *tmpspcl)
1333 int32_t sum, cnt, *lp;
1335 tmpspcl->s_spcl.c_checksum = 0;
1336 lp = (int32_t *)&tmpspcl->s_spcl;
1338 cnt = sizeof(union u_spcl) / (4 * sizeof(int32_t));
1339 while (--cnt >= 0) {
1345 tmpspcl->s_spcl.c_checksum = CHECKSUM - sum;
1349 * Write a header record to the dump tape.
1352 writeheader(dump_ino_t ino)
1354 spcl.c_inumber = ino;
1355 spcl.c_magic = NFS_MAGIC;
1356 mkchecksum((union u_spcl *)&spcl);
1357 writerec((char *)&spcl, 1);
1362 getino(dump_ino_t inum)
1364 static struct dinode dinode;
1368 #ifdef HAVE_EXT2FS_READ_INODE_FULL
1369 err = ext2fs_read_inode_full(fs, (ext2_ino_t)inum, (struct ext2_inode *) &dinode, sizeof(struct dinode));
1371 err = ext2fs_read_inode(fs, (ext2_ino_t)inum, (struct ext2_inode *) &dinode);
1374 com_err(disk, err, "while reading inode #%ld\n", (long)inum);
1379 #else /* __linux__ */
1381 getino(dump_ino_t inum)
1383 static daddr_t minino, maxino;
1384 static struct dinode inoblock[MAXINOPB];
1387 if (inum >= minino && inum < maxino)
1388 return (&inoblock[inum - minino]);
1389 bread(fsbtodb(sblock, ino_to_fsba(sblock, inum)), (char *)inoblock,
1390 (int)sblock->fs_bsize);
1391 minino = inum - (inum % INOPB(sblock));
1392 maxino = minino + INOPB(sblock);
1393 return (&inoblock[inum - minino]);
1395 #endif /* __linux__ */
1398 * Read a chunk of data from the disk.
1399 * Try to recover from hard errors by reading in sector sized pieces.
1400 * Error recovery is attempted at most BREADEMAX times before seeking
1401 * consent from the operator to continue.
1403 int breaderrors = 0;
1406 bread(ext2_loff_t blkno, char *buf, int size)
1412 if (ext2fs_llseek(diskfd, (blkno << dev_bshift), 0) !=
1413 (blkno << dev_bshift))
1415 if (lseek(diskfd, ((off_t)blkno << dev_bshift), 0) !=
1416 ((off_t)blkno << dev_bshift))
1418 msg("bread: lseek fails\n");
1419 if ((cnt = read(diskfd, buf, size)) == size)
1421 if (blkno + (size / dev_bsize) > fsbtodb(sblock, sblock->fs_size)) {
1423 * Trying to read the final fragment.
1425 * NB - dump only works in TP_BSIZE blocks, hence
1426 * rounds `dev_bsize' fragments up to TP_BSIZE pieces.
1427 * It should be smarter about not actually trying to
1428 * read more than it can get, but for the time being
1429 * we punt and scale back the read only when it gets
1430 * us into trouble. (mkm 9/25/83)
1436 msg("read error from %s: %s: [block %d, ext2blk %d]: count=%d\n",
1437 disk, strerror(errno), blkno,
1438 dbtofsb(sblock, blkno), size);
1440 msg("short read error from %s: [block %d, ext2blk %d]: count=%d, got=%d\n",
1441 disk, blkno, dbtofsb(sblock, blkno), size, cnt);
1442 if (breademax && ++breaderrors > breademax) {
1443 msg("More than %d block read errors from %d\n",
1445 broadcast("DUMP IS AILING!\n");
1446 msg("This is an unrecoverable error.\n");
1447 if (!query("Do you want to attempt to continue?")){
1454 * Zero buffer, then try to read each sector of buffer separately.
1456 memset(buf, 0, size);
1457 for (i = 0; i < size; i += dev_bsize, buf += dev_bsize, blkno++) {
1459 if (ext2fs_llseek(diskfd, (blkno << dev_bshift), 0) !=
1460 (blkno << dev_bshift))
1462 if (lseek(diskfd, ((off_t)blkno << dev_bshift), 0) !=
1463 ((off_t)blkno << dev_bshift))
1465 msg("bread: lseek2 fails!\n");
1466 if ((cnt = read(diskfd, buf, (int)dev_bsize)) == dev_bsize)
1469 msg("read error from %s: %s: [sector %d, ext2blk %d]: count=%d\n",
1470 disk, strerror(errno), blkno,
1471 dbtofsb(sblock, blkno), dev_bsize);
1474 msg("short read error from %s: [sector %d, ext2blk %d]: count=%d, got=%d\n",
1475 disk, blkno, dbtofsb(sblock, blkno), dev_bsize, cnt);