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@cybercable.fr>, 1999-2000
9 * Copyright (c) 1980, 1988, 1991, 1993
10 * The Regents of the University of California. All rights reserved.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 * This product includes software developed by the University of
23 * California, Berkeley and its contributors.
24 * 4. Neither the name of the University nor the names of its contributors
25 * may be used to endorse or promote products derived from this software
26 * without specific prior written permission.
28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
42 static const char rcsid[] =
43 "$Id: traverse.c,v 1.18 2000/05/28 18:16:42 stelian Exp $";
46 #include <sys/param.h>
49 #include <linux/ext2_fs.h>
50 #include <bsdcompat.h>
51 #include <compaterr.h>
53 #define swab32(x) ext2fs_swab32(x)
57 #include <sys/vnode.h>
60 #include <ufs/fsdir.h>
61 #include <ufs/inode.h>
63 #include <ufs/ufs/dir.h>
64 #include <ufs/ufs/dinode.h>
65 #include <ufs/ffs/fs.h>
67 #endif /* __linux__ */
69 #include <protocols/dumprestore.h>
79 #include <ext2fs/ext2fs.h>
84 #define HASDUMPEDFILE 0x1
85 #define HASSUBDIRS 0x2
88 typedef quad_t fsizeT;
94 static int searchdir __P((struct ext2_dir_entry *dp, int offset,
95 int blocksize, char *buf, void *private));
97 static int dirindir __P((ino_t ino, daddr_t blkno, int level, long *size));
98 static void dmpindir __P((ino_t ino, daddr_t blk, int level, fsizeT *size));
99 static int searchdir __P((ino_t ino, daddr_t blkno, long size, long filesize));
101 static void mapfileino __P((ino_t ino, long *tapesize, int *dirskipped));
103 /* #define EXT3_FEATURE_INCOMPAT_RECOVER */
105 int dump_fs_open(const char *disk, ext2_filsys *fs)
110 #ifdef EXT3_FEATURE_INCOMPAT_RECOVER
111 retval = ext2fs_open(disk, EXT2_FLAG_FORCE, 0, 0, unix_io_manager, fs);
113 retval = ext2fs_open(disk, 0, 0, 0, unix_io_manager, fs);
115 #if defined(EXT2_LIB_FEATURE_COMPAT_SUPP) && defined(EXT2_LIB_FEATURE_INCOMPAT_SUPP) && defined(EXT2_LIB_FEATURE_RO_COMPAT_SUPP) && defined(EXT2_ET_UNSUPP_FEATURE) && defined(EXT2_ET_RO_UNSUPP_FEATURE)
117 s = (struct ext2fs_sb *) (*fs)->super;
118 if ((s->s_feature_compat & ~EXT2_LIB_FEATURE_COMPAT_SUPP) ||
119 #ifdef EXT3_FEATURE_INCOMPAT_RECOVER
120 (s->s_feature_incompat & ~(EXT3_FEATURE_INCOMPAT_RECOVER | EXT2_LIB_FEATURE_INCOMPAT_SUPP))) {
122 (s->s_feature_incompat & ~EXT2_LIB_FEATURE_INCOMPAT_SUPP)) {
124 retval = EXT2_ET_UNSUPP_FEATURE;
126 else if (s->s_feature_ro_compat & ~EXT2_LIB_FEATURE_RO_COMPAT_SUPP) {
127 retval = EXT2_ET_RO_UNSUPP_FEATURE;
130 #endif /* defined && defined && defined... */
135 * This is an estimation of the number of TP_BSIZE blocks in the file.
136 * It estimates the number of blocks in files with holes by assuming
137 * that all of the blocks accounted for by di_blocks are data blocks
138 * (when some of the blocks are usually used for indirect pointers);
139 * hence the estimate may be high.
142 blockest(struct dinode *dp)
144 long blkest, sizeest;
147 * dp->di_size is the size of the file in bytes.
148 * dp->di_blocks stores the number of sectors actually in the file.
149 * If there are more sectors than the size would indicate, this just
150 * means that there are indirect blocks in the file or unused
151 * sectors in the last file block; we can safely ignore these
152 * (blkest = sizeest below).
153 * If the file is bigger than the number of sectors would indicate,
154 * then the file has holes in it. In this case we must use the
155 * block count to estimate the number of data blocks used, but
156 * we use the actual size for estimating the number of indirect
157 * dump blocks (sizeest vs. blkest in the indirect block
160 blkest = howmany(dbtob(dp->di_blocks), TP_BSIZE);
161 sizeest = howmany(dp->di_size, TP_BSIZE);
162 if (blkest > sizeest)
165 if (dp->di_size > fs->blocksize * NDADDR) {
166 /* calculate the number of indirect blocks on the dump tape */
168 howmany(sizeest - NDADDR * fs->blocksize / TP_BSIZE,
169 NINDIR(sblock) * EXT2_FRAGS_PER_BLOCK(fs->super));
172 if (dp->di_size > sblock->fs_bsize * NDADDR) {
173 /* calculate the number of indirect blocks on the dump tape */
175 howmany(sizeest - NDADDR * sblock->fs_bsize / TP_BSIZE,
182 /* Auxiliary macro to pick up files changed since previous dump. */
183 #define CHANGEDSINCE(dp, t) \
184 ((dp)->di_mtime >= (t) || (dp)->di_ctime >= (t))
186 /* The WANTTODUMP macro decides whether a file should be dumped. */
188 #define WANTTODUMP(dp) \
189 (CHANGEDSINCE(dp, spcl.c_ddate) && \
190 (nonodump || ((dp)->di_flags & UF_NODUMP) != UF_NODUMP))
192 #define WANTTODUMP(dp) CHANGEDSINCE(dp, spcl.c_ddate)
195 extern ino_t iexclude_list[IEXCLUDE_MAXNUM]; /* the inode exclude list */
196 extern int iexclude_num; /* number of elements in the list */
199 * Determine if given inode should be dumped
202 mapfileino(ino_t ino, long *tapesize, int *dirskipped)
205 register struct dinode *dp;
208 * Skip inode if we've already marked it for dumping
210 if (TSTINO(ino, usedinomap))
213 if ((mode = (dp->di_mode & IFMT)) == 0)
216 if (dp->di_nlink == 0 || dp->di_dtime != 0)
220 * Put all dirs in dumpdirmap, inodes that are to be dumped in the
221 * used map. All inode but dirs who have the nodump attribute go
224 SETINO(ino, usedinomap);
227 if(iexclude_num) { /* if there are inodes in the exclude list */
228 int idx; /* then check this inode against it */
229 for (idx=0; idx<iexclude_num; idx++) {
230 if (ino == iexclude_list[idx]) {
231 msg("Excluding inode number %d\n", ino);
232 return; /* if in list then skip */
238 SETINO(ino, dumpdirmap);
239 if (WANTTODUMP(dp)) {
240 SETINO(ino, dumpinomap);
241 if (mode != IFREG && mode != IFDIR && mode != IFLNK)
244 *tapesize += blockest(dp);
249 if (!nonodump && (dp->di_flags & UF_NODUMP))
250 CLRINO(ino, usedinomap);
259 * Walk the inode list for a filesystem to find all allocated inodes
260 * that have been modified since the previous dump time. Also, find all
261 * the directories in the filesystem.
264 mapfiles(ino_t maxino, long *tapesize)
267 int anydirskipped = 0;
269 for (ino = ROOTINO; ino < maxino; ino++)
270 mapfileino(ino, tapesize, &anydirskipped);
273 * Restore gets very upset if the root is not dumped,
274 * so ensure that it always is dumped.
276 SETINO(ROOTINO, dumpinomap);
277 return (anydirskipped);
281 struct mapfile_context {
287 mapfilesindir(struct ext2_dir_entry *dirent, int offset, int blocksize, char *buf, void *private)
289 register struct dinode *dp;
292 struct mapfile_context *mfc;
296 mfc = (struct mapfile_context *)private;
298 mapfileino(dirent->inode, mfc->tapesize, mfc->anydirskipped);
300 dp = getino(dirent->inode);
301 mode = dp->di_mode & IFMT;
302 if (mode == IFDIR && dp->di_nlink != 0 && dp->di_dtime == 0) {
303 if ((dirent->name[0] != '.' || ( dirent->name_len & 0xFF ) != 1) &&
304 (dirent->name[0] != '.' || dirent->name[1] != '.' ||
305 ( dirent->name_len & 0xFF ) != 2)) {
306 retval = ext2fs_dir_iterate(fs, ino, 0, NULL,
307 mapfilesindir, private);
318 * Walk the inode list for a filesystem to find all allocated inodes
319 * that have been modified since the previous dump time. Also, find all
320 * the directories in the filesystem.
323 mapfilesfromdir(ino_t maxino, long *tapesize, char *directory)
326 struct mapfile_context mfc;
328 char dir_name [MAXPATHLEN];
329 int i, anydirskipped = 0;
332 * Mark every directory in the path as being dumped
334 for (i = 0; i < strlen (directory); i++) {
335 if (directory[i] == '/') {
336 strncpy (dir_name, directory, i);
338 retval = ext2fs_namei(fs, ROOTINO, ROOTINO, dir_name,
341 com_err(disk, retval, "while translating %s",
345 mapfileino(dir_ino, tapesize, &anydirskipped);
349 * Mark the final directory
351 retval = ext2fs_namei(fs, ROOTINO, ROOTINO, directory, &dir_ino);
353 com_err(disk, retval, "while translating %s", directory);
356 mapfileino(dir_ino, tapesize, &anydirskipped);
358 mfc.tapesize = tapesize;
359 mfc.anydirskipped = &anydirskipped;
360 retval = ext2fs_dir_iterate(fs, dir_ino, 0, NULL, mapfilesindir,
364 com_err(disk, retval, "while mapping files in %s", directory);
368 * Ensure that the root inode actually appears in the file list
371 mapfileino(ROOTINO, tapesize, &anydirskipped);
373 * Restore gets very upset if the root is not dumped,
374 * so ensure that it always is dumped.
376 SETINO(ROOTINO, dumpinomap);
377 return anydirskipped;
382 struct mapdirs_context {
392 * Scan each directory on the filesystem to see if it has any modified
393 * files in it. If it does, and has not already been added to the dump
394 * list (because it was itself modified), then add it. If a directory
395 * has not been modified itself, contains no modified files and has no
396 * subdirectories, then it can be deleted from the dump list and from
397 * the list of directories. By deleting it from the list of directories,
398 * its parent may now qualify for the same treatment on this or a later
399 * pass using this algorithm.
402 mapdirs(ino_t maxino, long *tapesize)
404 register struct dinode *dp;
412 struct mapdirs_context mdc;
414 int ret, change = 0, nodump;
416 isdir = 0; /* XXX just to get gcc to shut up */
417 for (map = dumpdirmap, ino = 1; ino < maxino; ino++) {
418 if (((ino - 1) % NBBY) == 0) /* map is offset by 1 */
423 * If dir has been removed from the used map, it's either
424 * because it had the nodump flag, or it herited it from
425 * its parent. A directory can't be in dumpinomap if not
426 * in usedinomap, but we have to go through it anyway
427 * to propagate the nodump attribute.
429 nodump = (TSTINO(ino, usedinomap) == 0);
430 if ((isdir & 1) == 0 ||
431 (TSTINO(ino, dumpinomap) && nodump == 0))
438 mdc.tapesize = tapesize;
439 ext2fs_dir_iterate(fs, ino, 0, NULL, searchdir, (void *) &mdc);
440 #else /* __linux__ */
441 filesize = dp->di_size;
442 for (ret = 0, i = 0; filesize > 0 && i < NDADDR; i++) {
443 if (dp->di_db[i] != 0)
444 ret |= searchdir(ino, dp->di_db[i],
445 (long)dblksize(sblock, dp, i),
447 if (ret & HASDUMPEDFILE)
450 filesize -= sblock->fs_bsize;
452 for (i = 0; filesize > 0 && i < NIADDR; i++) {
453 if (dp->di_ib[i] == 0)
455 ret |= dirindir(ino, dp->di_ib[i], i, &filesize);
457 #endif /* __linux__ */
458 if (ret & HASDUMPEDFILE) {
459 SETINO(ino, dumpinomap);
460 *tapesize += blockest(dp);
465 if (ret & HASSUBDIRS)
466 change = 1; /* subdirs have inherited nodump */
467 CLRINO(ino, dumpdirmap);
468 } else if ((ret & HASSUBDIRS) == 0) {
469 if (!TSTINO(ino, dumpinomap)) {
470 CLRINO(ino, dumpdirmap);
480 * Read indirect blocks, and pass the data blocks to be searched
481 * as directories. Quit as soon as any entry is found that will
482 * require the directory to be dumped.
485 dirindir(ino_t ino, daddr_t blkno, int ind_level, long *filesize)
489 daddr_t idblk[MAXNINDIR];
491 bread(fsbtodb(sblock, blkno), (char *)idblk, (int)sblock->fs_bsize);
492 if (ind_level <= 0) {
493 for (i = 0; *filesize > 0 && i < NINDIR(sblock); i++) {
496 ret |= searchdir(ino, blkno, sblock->fs_bsize,
498 if (ret & HASDUMPEDFILE)
501 *filesize -= sblock->fs_bsize;
506 for (i = 0; *filesize > 0 && i < NINDIR(sblock); i++) {
509 ret |= dirindir(ino, blkno, ind_level, filesize);
513 #endif /* !__linux__ */
516 * Scan a disk block containing directory information looking to see if
517 * any of the entries are on the dump list and to see if the directory
518 * contains any subdirectories.
522 searchdir(struct ext2_dir_entry *dp, int offset, int blocksize, char *buf, void *private)
524 struct mapdirs_context *mdc;
529 mdc = (struct mapdirs_context *)private;
531 tapesize = mdc->tapesize;
535 if (dp->name[0] == '.') {
536 if (( dp->name_len & 0xFF ) == 1)
538 if (dp->name[1] == '.' && ( dp->name_len & 0xFF ) == 2)
542 ip = getino(dp->inode);
543 if (TSTINO(dp->inode, dumpinomap)) {
544 CLRINO(dp->inode, dumpinomap);
545 CLRINO(dp->inode, usedinomap);
546 *tapesize -= blockest(ip);
548 /* Add dir back to the dir map, to propagate nodump */
549 if ((ip->di_mode & IFMT) == IFDIR) {
550 SETINO(dp->inode, dumpdirmap);
554 if (TSTINO(dp->inode, dumpinomap)) {
555 *ret |= HASDUMPEDFILE;
556 if (*ret & HASSUBDIRS)
559 if (TSTINO(dp->inode, dumpdirmap)) {
561 if (*ret & HASDUMPEDFILE)
568 #else /* __linux__ */
571 searchdir(ino_t ino, daddr_t blkno, long size, long filesize)
573 register struct direct *dp;
574 register long loc, ret = 0;
577 bread(fsbtodb(sblock, blkno), dblk, (int)size);
580 for (loc = 0; loc < size; ) {
581 dp = (struct direct *)(dblk + loc);
582 if (dp->d_reclen == 0) {
583 msg("corrupted directory, inumber %d\n", ino);
589 if (dp->d_name[0] == '.') {
590 if (dp->d_name[1] == '\0')
592 if (dp->d_name[1] == '.' && dp->d_name[2] == '\0')
595 if (TSTINO(dp->d_ino, dumpinomap)) {
596 ret |= HASDUMPEDFILE;
597 if (ret & HASSUBDIRS)
600 if (TSTINO(dp->d_ino, dumpdirmap)) {
602 if (ret & HASDUMPEDFILE)
608 #endif /* __linux__ */
612 struct block_context {
621 * Dump a block to the tape
624 dumponeblock(ext2_filsys fs, blk_t *blocknr, int blockcnt, void *private)
626 struct block_context *p;
629 if (blockcnt < NDADDR)
631 p = (struct block_context *)private;
632 for (i = p->next_block; i < blockcnt; i++) {
633 p->buf[p->cnt++] = 0;
634 if (p->cnt == p->max) {
635 blksout (p->buf, p->cnt, p->ino);
639 p->buf[p->cnt++] = *blocknr;
640 if (p->cnt == p->max) {
641 blksout (p->buf, p->cnt, p->ino);
644 p->next_block = blockcnt + 1;
650 * Dump passes 3 and 4.
652 * Dump the contents of an inode to tape.
655 dumpino(struct dinode *dp, ino_t ino)
660 struct old_bsd_inode obi;
662 struct block_context bc;
669 dumpmap(dumpinomap, TS_BITS, ino);
671 CLRINO(ino, dumpinomap);
673 memset(&obi, 0, sizeof(obi));
674 obi.di_mode = dp->di_mode;
675 obi.di_uid = dp->di_uid;
676 obi.di_gid = dp->di_gid;
677 obi.di_qsize.v = (u_quad_t)dp->di_size;
678 obi.di_atime = dp->di_atime;
679 obi.di_mtime = dp->di_mtime;
680 obi.di_ctime = dp->di_ctime;
681 obi.di_nlink = dp->di_nlink;
682 obi.di_blocks = dp->di_blocks;
683 obi.di_flags = dp->di_flags;
684 obi.di_gen = dp->di_gen;
685 memmove(&obi.di_db, &dp->di_db, (NDADDR + NIADDR) * sizeof(daddr_t));
686 if (dp->di_file_acl || dp->di_dir_acl)
687 warn("ACLs in inode #%ld won't be dumped", (long)ino);
688 memmove(&spcl.c_dinode, &obi, sizeof(obi));
689 #else /* __linux__ */
691 #endif /* __linux__ */
692 spcl.c_type = TS_INODE;
694 switch (dp->di_mode & S_IFMT) {
704 msg("Warning: dumpino called on a directory (ino %d)\n", ino);
710 * Check for short symbolic link.
713 if (dp->di_size > 0 &&
714 dp->di_size < EXT2_N_BLOCKS * sizeof (daddr_t)) {
718 memmove(buf, dp->di_db, (u_long)dp->di_size);
719 buf[dp->di_size] = '\0';
723 #endif /* __linux__ */
725 if (dp->di_size > 0 &&
726 dp->di_size < sblock->fs_maxsymlinklen) {
730 memmove(buf, dp->di_shortlink, (u_long)dp->di_size);
731 buf[dp->di_size] = '\0';
754 msg("Warning: undefined file type 0%o\n", dp->di_mode & IFMT);
757 if (dp->di_size > NDADDR * sblock->fs_bsize)
759 cnt = NDADDR * EXT2_FRAGS_PER_BLOCK(fs->super);
761 cnt = NDADDR * sblock->fs_frag;
764 cnt = howmany(dp->di_size, sblock->fs_fsize);
765 blksout(&dp->di_db[0], cnt, ino);
766 if ((size = dp->di_size - NDADDR * sblock->fs_bsize) <= 0)
769 bc.max = NINDIR(sblock) * EXT2_FRAGS_PER_BLOCK(fs->super);
770 bc.buf = (int *)malloc (bc.max * sizeof (int));
773 bc.next_block = NDADDR;
775 ext2fs_block_iterate (fs, ino, 0, NULL, dumponeblock, (void *)&bc);
777 blksout (bc.buf, bc.cnt, bc.ino);
781 for (ind_level = 0; ind_level < NIADDR; ind_level++) {
782 dmpindir(ino, dp->di_ib[ind_level], ind_level, &size);
791 struct convert_dir_context {
799 * This function converts an ext2fs directory entry to the BSD format.
801 * Basically, it adds a null-character at the end of the name, recomputes the
802 * size of the entry, and creates it in a temporary buffer
805 convert_dir(struct ext2_dir_entry *dirent, int offset, int blocksize, char *buf, void *private)
807 struct convert_dir_context *p;
808 struct olddirect *dp;
811 p = (struct convert_dir_context *)private;
813 reclen = EXT2_DIR_REC_LEN((dirent->name_len & 0xFF) + 1);
814 if (((p->offset + reclen - 1) / p->bs) != (p->offset / p->bs)) {
815 dp = (struct olddirect *)(p->buf + p->prev_offset);
816 dp->d_reclen += p->bs - (p->offset % p->bs);
817 p->offset += p->bs - (p->offset % p->bs);
820 dp = (struct olddirect *)(p->buf + p->offset);
821 dp->d_ino = dirent->inode;
822 dp->d_reclen = reclen;
823 dp->d_namlen = dirent->name_len & 0xFF;
824 strncpy(dp->d_name, dirent->name, dp->d_namlen);
825 dp->d_name[dp->d_namlen] = '\0';
826 p->prev_offset = p->offset;
835 * Dumps a directory to tape after converting it to the BSD format
838 dumpdirino(struct dinode *dp, ino_t ino)
842 struct old_bsd_inode obi;
843 struct convert_dir_context cdc;
845 struct ext2_dir_entry *de;
850 dumpmap(dumpinomap, TS_BITS, ino);
852 CLRINO(ino, dumpinomap);
855 * Convert the directory to the BSD format
857 /* Allocate a buffer for the conversion (twice the size of the
858 ext2fs directory to avoid problems ;-) */
859 cdc.buf = (char *)malloc(dp->di_size * 2 * sizeof(char));
861 err(1, "Cannot allocate buffer to convert directory #%lu\n",
865 cdc.bs = MIN(DIRBLKSIZ, TP_BSIZE);
866 /* Do the conversion */
867 retval = ext2fs_dir_iterate(fs, ino, 0, NULL, convert_dir, (void *)&cdc);
869 com_err(disk, retval, "while converting directory #%ld\n", (long)ino);
872 /* Fix the last entry */
873 if ((cdc.offset % cdc.bs) != 0) {
874 de = (struct ext2_dir_entry *)(cdc.buf + cdc.prev_offset);
875 de->rec_len += cdc.bs - (cdc.offset % cdc.bs);
876 cdc.offset += cdc.bs - (cdc.offset % cdc.bs);
879 dir_size = cdc.offset;
882 memset(&obi, 0, sizeof(obi));
883 obi.di_mode = dp->di_mode;
884 obi.di_uid = dp->di_uid;
885 obi.di_gid = dp->di_gid;
886 obi.di_qsize.v = dir_size; /* (u_quad_t)dp->di_size; */
887 obi.di_atime = dp->di_atime;
888 obi.di_mtime = dp->di_mtime;
889 obi.di_ctime = dp->di_ctime;
890 obi.di_nlink = dp->di_nlink;
891 obi.di_blocks = dp->di_blocks;
892 obi.di_flags = dp->di_flags;
893 obi.di_gen = dp->di_gen;
894 memmove(&obi.di_db, &dp->di_db, (NDADDR + NIADDR) * sizeof(daddr_t));
895 if (dp->di_file_acl || dp->di_dir_acl)
896 warn("ACLs in inode #%ld won't be dumped", (long)ino);
897 memmove(&spcl.c_dinode, &obi, sizeof(obi));
898 #else /* __linux__ */
900 #endif /* __linux__ */
901 spcl.c_type = TS_INODE;
903 switch (dp->di_mode & S_IFMT) {
914 msg("Warning: size of directory inode #%d is <= 0 (%d)!\n",
919 msg("Warning: dumpdirino called with file type 0%o (inode #%d)\n",
920 dp->di_mode & IFMT, ino);
923 for (size = 0; size < dir_size; size += TP_BSIZE) {
927 memmove(buf, cdc.buf + size, TP_BSIZE);
929 spcl.c_type = TS_ADDR;
934 #endif /* __linux__ */
938 * Read indirect blocks, and pass the data blocks to be dumped.
941 dmpindir(ino_t ino, daddr_t blk, int ind_level, fsizeT *size)
948 daddr_t idblk[MAXNINDIR];
951 bread(fsbtodb(sblock, blk), (char *)idblk, (int) sblock->fs_bsize);
954 * My RedHat 4.0 system doesn't have these flags; I haven't
955 * upgraded e2fsprogs yet
957 #if defined(EXT2_FLAG_SWAP_BYTES)
958 if ((fs->flags & EXT2_FLAG_SWAP_BYTES) ||
959 (fs->flags & EXT2_FLAG_SWAP_BYTES_READ)) {
961 max = sblock->fs_bsize >> 2;
962 swapme = (blk_t *) idblk;
963 for (i = 0; i < max; i++, swapme++)
964 *swapme = swab32(*swapme);
965 #if defined(EXT2_FLAG_SWAP_BYTES)
970 memset(idblk, 0, (int)sblock->fs_bsize);
971 if (ind_level <= 0) {
972 if (*size < NINDIR(sblock) * sblock->fs_bsize)
973 cnt = howmany(*size, sblock->fs_fsize);
976 cnt = NINDIR(sblock) * EXT2_FRAGS_PER_BLOCK(fs->super);
978 cnt = NINDIR(sblock) * sblock->fs_frag;
980 *size -= NINDIR(sblock) * sblock->fs_bsize;
981 blksout(&idblk[0], cnt, ino);
985 for (i = 0; i < NINDIR(sblock); i++) {
986 dmpindir(ino, idblk[i], ind_level, size);
994 * Collect up the data into tape record sized buffers and output them.
997 blksout(daddr_t *blkp, int frags, ino_t ino)
999 register daddr_t *bp;
1000 int i, j, count, blks, tbperdb;
1002 blks = howmany(frags * sblock->fs_fsize, TP_BSIZE);
1003 tbperdb = sblock->fs_bsize >> tp_bshift;
1004 for (i = 0; i < blks; i += TP_NINDIR) {
1005 if (i + TP_NINDIR > blks)
1008 count = i + TP_NINDIR;
1009 for (j = i; j < count; j++)
1010 if (blkp[j / tbperdb] != 0)
1011 spcl.c_addr[j - i] = 1;
1013 spcl.c_addr[j - i] = 0;
1014 spcl.c_count = count - i;
1016 bp = &blkp[i / tbperdb];
1017 for (j = i; j < count; j += tbperdb, bp++) {
1019 if (j + tbperdb <= count)
1020 dumpblock(*bp, (int)sblock->fs_bsize);
1022 dumpblock(*bp, (count - j) * TP_BSIZE);
1025 spcl.c_type = TS_ADDR;
1030 * Dump a map to the tape.
1033 dumpmap(char *map, int type, ino_t ino)
1039 spcl.c_count = howmany(mapsize * sizeof(char), TP_BSIZE);
1041 for (i = 0, cp = map; i < spcl.c_count; i++, cp += TP_BSIZE)
1046 * Write a header record to the dump tape.
1049 writeheader(ino_t ino)
1052 register __s32 sum, cnt, *lp;
1054 register int32_t sum, cnt, *lp;
1057 spcl.c_inumber = ino;
1058 spcl.c_magic = NFS_MAGIC;
1059 spcl.c_checksum = 0;
1061 lp = (__s32 *)&spcl;
1063 lp = (int32_t *)&spcl;
1067 cnt = sizeof(union u_spcl) / (4 * sizeof(__s32));
1069 cnt = sizeof(union u_spcl) / (4 * sizeof(int32_t));
1071 while (--cnt >= 0) {
1077 spcl.c_checksum = CHECKSUM - sum;
1078 writerec((char *)&spcl, 1);
1085 static struct dinode dinode;
1088 ext2fs_read_inode(fs, inum, (struct ext2_inode *) &dinode);
1091 #else /* __linux__ */
1095 static daddr_t minino, maxino;
1096 static struct dinode inoblock[MAXINOPB];
1099 if (inum >= minino && inum < maxino)
1100 return (&inoblock[inum - minino]);
1101 bread(fsbtodb(sblock, ino_to_fsba(sblock, inum)), (char *)inoblock,
1102 (int)sblock->fs_bsize);
1103 minino = inum - (inum % INOPB(sblock));
1104 maxino = minino + INOPB(sblock);
1105 return (&inoblock[inum - minino]);
1107 #endif /* __linux__ */
1110 * Read a chunk of data from the disk.
1111 * Try to recover from hard errors by reading in sector sized pieces.
1112 * Error recovery is attempted at most BREADEMAX times before seeking
1113 * consent from the operator to continue.
1115 int breaderrors = 0;
1116 #define BREADEMAX 32
1119 bread(daddr_t blkno, char *buf, int size)
1126 if (ext2fs_llseek(diskfd, (((ext2_loff_t)blkno) << dev_bshift), 0) !=
1127 (((ext2_loff_t)blkno) << dev_bshift))
1129 if (lseek(diskfd, ((off_t)blkno << dev_bshift), 0) !=
1130 ((off_t)blkno << dev_bshift))
1132 msg("bread: lseek fails\n");
1133 if ((cnt = read(diskfd, buf, size)) == size)
1135 if (blkno + (size / dev_bsize) > fsbtodb(sblock, sblock->fs_size)) {
1137 * Trying to read the final fragment.
1139 * NB - dump only works in TP_BSIZE blocks, hence
1140 * rounds `dev_bsize' fragments up to TP_BSIZE pieces.
1141 * It should be smarter about not actually trying to
1142 * read more than it can get, but for the time being
1143 * we punt and scale back the read only when it gets
1144 * us into trouble. (mkm 9/25/83)
1150 msg("read error from %s: %s: [block %d]: count=%d\n",
1151 disk, strerror(errno), blkno, size);
1153 msg("short read error from %s: [block %d]: count=%d, got=%d\n",
1154 disk, blkno, size, cnt);
1155 if (++breaderrors > BREADEMAX) {
1156 msg("More than %d block read errors from %d\n",
1158 broadcast("DUMP IS AILING!\n");
1159 msg("This is an unrecoverable error.\n");
1160 if (!query("Do you want to attempt to continue?")){
1167 * Zero buffer, then try to read each sector of buffer separately.
1169 memset(buf, 0, size);
1170 for (i = 0; i < size; i += dev_bsize, buf += dev_bsize, blkno++) {
1172 if (ext2fs_llseek(diskfd, (((ext2_loff_t)blkno) << dev_bshift), 0) !=
1173 (((ext2_loff_t)blkno) << dev_bshift))
1175 if (lseek(diskfd, ((off_t)blkno << dev_bshift), 0) !=
1176 ((off_t)blkno << dev_bshift))
1178 msg("bread: lseek2 fails!\n");
1179 if ((cnt = read(diskfd, buf, (int)dev_bsize)) == dev_bsize)
1182 msg("read error from %s: %s: [sector %d]: count=%d\n",
1183 disk, strerror(errno), blkno, dev_bsize);
1186 msg("short read error from %s: [sector %d]: count=%d, got=%d\n",
1187 disk, blkno, dev_bsize, cnt);