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.33 2001/04/11 13:42:52 stelian Exp $";
55 #include <sys/param.h>
58 #include <linux/ext2_fs.h>
59 #include <ext2fs/ext2fs.h>
60 #include <bsdcompat.h>
61 #include <compaterr.h>
64 #include <sys/vnode.h>
67 #include <ufs/fsdir.h>
68 #include <ufs/inode.h>
70 #include <ufs/ufs/dir.h>
71 #include <ufs/ufs/dinode.h>
72 #include <ufs/ffs/fs.h>
73 #endif /* __linux__ */
75 #include <protocols/dumprestore.h>
79 #define HASDUMPEDFILE 0x1
80 #define HASSUBDIRS 0x2
83 typedef u_quad_t fsizeT;
86 typedef quad_t fsizeT;
93 static int searchdir __P((struct ext2_dir_entry *dp, int offset,
94 int blocksize, char *buf, void *private));
96 static int dirindir __P((dump_ino_t ino, daddr_t blkno, int level, long *size));
97 static void dmpindir __P((dump_ino_t ino, daddr_t blk, int level, fsizeT *size));
98 static int searchdir __P((dump_ino_t ino, daddr_t blkno, long size, long filesize));
100 static void mapfileino __P((dump_ino_t ino, struct dinode const *dp, long *tapesize, int *dirskipped));
101 static int exclude_ino __P((dump_ino_t ino));
102 extern dump_ino_t iexclude_list[IEXCLUDE_MAXNUM]; /* the inode exclude list */
103 extern int iexclude_num; /* number of elements in list */
105 #ifdef HAVE_EXT2_JOURNAL_INUM
106 #define ext2_journal_ino(sb) (sb->s_journal_inum)
108 #define ext2_journal_ino(sb) (*((__u32 *)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
135 int dump_fs_open(const char *disk, ext2_filsys *fs)
139 retval = ext2fs_open(disk, EXT2_FLAG_FORCE, 0, 0, unix_io_manager, fs);
141 struct ext2_super_block *es = (*fs)->super;
142 dump_ino_t journal_ino = ext2_journal_ino(es);
144 if (es->s_feature_incompat & EXT3_FEATURE_INCOMPAT_JOURNAL_DEV){
145 fprintf(stderr, "This an journal, not a filesystem!\n");
146 retval = EXT2_ET_UNSUPP_FEATURE;
149 else if ((retval = es->s_feature_incompat &
150 ~(EXT2_LIB_FEATURE_INCOMPAT_SUPP |
151 EXT3_FEATURE_INCOMPAT_RECOVER))) {
153 "Unsupported feature(s) 0x%x in filesystem\n",
155 retval = EXT2_ET_UNSUPP_FEATURE;
159 if (es->s_feature_compat &
160 EXT3_FEATURE_COMPAT_HAS_JOURNAL &&
161 journal_ino && !exclude_ino(journal_ino)) {
162 iexclude_list[iexclude_num++] = journal_ino;
163 msg("Exclude ext3 journal inode %u\n",
166 if (!exclude_ino(EXT2_RESIZE_INO))
167 iexclude_list[iexclude_num++] = EXT2_RESIZE_INO;
174 * This is an estimation of the number of TP_BSIZE blocks in the file.
175 * It estimates the number of blocks in files with holes by assuming
176 * that all of the blocks accounted for by di_blocks are data blocks
177 * (when some of the blocks are usually used for indirect pointers);
178 * hence the estimate may be high.
181 blockest(struct dinode const *dp)
183 long blkest, sizeest;
187 * dp->di_size is the size of the file in bytes.
188 * dp->di_blocks stores the number of sectors actually in the file.
189 * If there are more sectors than the size would indicate, this just
190 * means that there are indirect blocks in the file or unused
191 * sectors in the last file block; we can safely ignore these
192 * (blkest = sizeest below).
193 * If the file is bigger than the number of sectors would indicate,
194 * then the file has holes in it. In this case we must use the
195 * block count to estimate the number of data blocks used, but
196 * we use the actual size for estimating the number of indirect
197 * dump blocks (sizeest vs. blkest in the indirect block
200 blkest = howmany((u_quad_t)dp->di_blocks*fs->blocksize, TP_BSIZE);
201 i_size = dp->di_size + ((u_quad_t) dp->di_size_high << 32);
202 sizeest = howmany(i_size, TP_BSIZE);
203 if (blkest > sizeest)
206 if (i_size > fs->blocksize * NDADDR) {
207 /* calculate the number of indirect blocks on the dump tape */
209 howmany(sizeest - NDADDR * fs->blocksize / TP_BSIZE,
210 NINDIR(sblock) * EXT2_FRAGS_PER_BLOCK(fs->super));
213 if (i_size > sblock->fs_bsize * NDADDR) {
214 /* calculate the number of indirect blocks on the dump tape */
216 howmany(sizeest - NDADDR * sblock->fs_bsize / TP_BSIZE,
224 * This tests whether an inode is in the exclude list
227 exclude_ino(dump_ino_t ino)
230 if (iexclude_num) { /* if there are inodes in the exclude list */
231 int idx; /* then check this inode against it */
232 for (idx = 0; idx < iexclude_num; idx++)
233 if (ino == iexclude_list[idx])
239 /* Auxiliary macro to pick up files changed since previous dump. */
240 #define CHANGEDSINCE(dp, t) \
241 ((dp)->di_mtime >= (t) || (dp)->di_ctime >= (t))
243 /* The NODUMP_FLAG macro tests if a file has the nodump flag. */
245 #define NODUMP_FLAG(dp) (!nonodump && (((dp)->di_flags & UF_NODUMP) == UF_NODUMP))
247 #define NODUMP_FLAG(dp) 0
250 /* The WANTTODUMP macro decides whether a file should be dumped. */
251 #define WANTTODUMP(dp, ino) \
252 (CHANGEDSINCE(dp, spcl.c_ddate) && \
253 (!NODUMP_FLAG(dp)) && \
257 * Determine if given inode should be dumped. "dp" must either point to a
258 * copy of the given inode, or be NULL (in which case it is fetched.)
261 mapfileino(dump_ino_t ino, struct dinode const *dp, long *tapesize, int *dirskipped)
266 * Skip inode if we've already marked it for dumping
268 if (TSTINO(ino, usedinomap))
272 if ((mode = (dp->di_mode & IFMT)) == 0)
275 if (dp->di_nlink == 0 || dp->di_dtime != 0)
279 * Put all dirs in dumpdirmap, inodes that are to be dumped in the
280 * used map. All inode but dirs who have the nodump attribute go
283 SETINO(ino, usedinomap);
286 SETINO(ino, dumpdirmap);
287 if (WANTTODUMP(dp, ino)) {
288 SETINO(ino, dumpinomap);
289 if (mode != IFREG && mode != IFDIR && mode != IFLNK)
292 *tapesize += blockest(dp);
296 if ( NODUMP_FLAG(dp) || exclude_ino(ino) )
297 CLRINO(ino, usedinomap);
305 * Walk the inode list for a filesystem to find all allocated inodes
306 * that have been modified since the previous dump time. Also, find all
307 * the directories in the filesystem.
311 mapfiles(dump_ino_t maxino, long *tapesize)
314 int anydirskipped = 0;
315 ext2_inode_scan scan;
317 struct ext2_inode inode;
320 * We use libext2fs's inode scanning routines, which are particularly
321 * robust. (Note that getino cannot return an error.)
323 err = ext2fs_open_inode_scan(fs, 0, &scan);
325 com_err(disk, err, "while opening inodes\n");
329 err = ext2fs_get_next_inode(scan, &ino, &inode);
330 if (err == EXT2_ET_BAD_BLOCK_IN_INODE_TABLE)
333 com_err(disk, err, "while scanning inode #%ld\n",
341 mapfileino(ino, (struct dinode const *)&inode, tapesize,
344 ext2fs_close_inode_scan(scan);
347 * Restore gets very upset if the root is not dumped,
348 * so ensure that it always is dumped.
350 SETINO(ROOTINO, dumpinomap);
351 return (anydirskipped);
355 mapfiles(dump_ino_t maxino, long *tapesize)
357 register dump_ino_t ino;
358 int anydirskipped = 0;
360 for (ino = ROOTINO; ino < maxino; ino++)
361 mapfileino(ino, tapesize, &anydirskipped);
364 * Restore gets very upset if the root is not dumped,
365 * so ensure that it always is dumped.
367 SETINO(ROOTINO, dumpinomap);
368 return (anydirskipped);
370 #endif /* __linux__ */
374 maponefile(dump_ino_t maxino, long *tapesize, char *directory)
378 char dir_name [MAXPATHLEN];
379 int i, anydirskipped = 0;
382 * Mark every directory in the path as being dumped
384 for (i = 0; i < strlen (directory); i++) {
385 if (directory[i] == '/') {
386 strncpy (dir_name, directory, i);
388 retval = ext2fs_namei(fs, ROOTINO, ROOTINO,
391 com_err(disk, retval,
392 "while translating %s", dir_name);
395 mapfileino((dump_ino_t) dir_ino, 0,
396 tapesize, &anydirskipped);
400 * Mark the final directory
402 retval = ext2fs_namei(fs, ROOTINO, ROOTINO, directory, &dir_ino);
404 com_err(disk, retval, "while translating %s", directory);
407 mapfileino((dump_ino_t)dir_ino, 0, tapesize, &anydirskipped);
409 mapfileino(ROOTINO, 0, tapesize, &anydirskipped);
412 * Restore gets very upset if the root is not dumped,
413 * so ensure that it always is dumped.
415 SETINO(ROOTINO, dumpdirmap);
416 return anydirskipped;
418 #endif /* __linux__ */
421 struct mapfile_context {
427 mapfilesindir(struct ext2_dir_entry *dirent, int offset, int blocksize, char *buf, void *private)
429 register struct dinode const *dp;
432 struct mapfile_context *mfc;
436 mfc = (struct mapfile_context *)private;
437 dp = getino(dirent->inode);
439 mapfileino(dirent->inode, dp, mfc->tapesize, mfc->anydirskipped);
441 mode = dp->di_mode & IFMT;
442 if (mode == IFDIR && dp->di_nlink != 0 && dp->di_dtime == 0) {
443 if ((dirent->name[0] != '.' || ( dirent->name_len & 0xFF ) != 1) &&
444 (dirent->name[0] != '.' || dirent->name[1] != '.' ||
445 ( dirent->name_len & 0xFF ) != 2)) {
446 retval = ext2fs_dir_iterate(fs, ino, 0, NULL,
447 mapfilesindir, private);
458 * Walk the inode list for a filesystem to find all allocated inodes
459 * that have been modified since the previous dump time. Also, find all
460 * the directories in the filesystem.
463 mapfilesfromdir(dump_ino_t maxino, long *tapesize, char *directory)
466 struct mapfile_context mfc;
468 char dir_name [MAXPATHLEN];
469 int i, anydirskipped = 0;
472 * Mark every directory in the path as being dumped
474 for (i = 0; i < strlen (directory); i++) {
475 if (directory[i] == '/') {
476 strncpy (dir_name, directory, i);
478 retval = ext2fs_namei(fs, ROOTINO, ROOTINO, dir_name,
481 com_err(disk, retval, "while translating %s",
485 mapfileino(dir_ino, 0, tapesize, &anydirskipped);
489 * Mark the final directory
491 retval = ext2fs_namei(fs, ROOTINO, ROOTINO, directory, &dir_ino);
493 com_err(disk, retval, "while translating %s", directory);
496 mapfileino(dir_ino, 0, tapesize, &anydirskipped);
498 mfc.tapesize = tapesize;
499 mfc.anydirskipped = &anydirskipped;
500 retval = ext2fs_dir_iterate(fs, dir_ino, 0, NULL, mapfilesindir,
504 com_err(disk, retval, "while mapping files in %s", directory);
508 * Ensure that the root inode actually appears in the file list
511 mapfileino(ROOTINO, 0, tapesize, &anydirskipped);
513 * Restore gets very upset if the root is not dumped,
514 * so ensure that it always is dumped.
516 SETINO(ROOTINO, dumpinomap);
517 return anydirskipped;
522 struct mapdirs_context {
532 * Scan each directory on the filesystem to see if it has any modified
533 * files in it. If it does, and has not already been added to the dump
534 * list (because it was itself modified), then add it. If a directory
535 * has not been modified itself, contains no modified files and has no
536 * subdirectories, then it can be deleted from the dump list and from
537 * the list of directories. By deleting it from the list of directories,
538 * its parent may now qualify for the same treatment on this or a later
539 * pass using this algorithm.
542 mapdirs(dump_ino_t maxino, long *tapesize)
544 register struct dinode *dp;
547 register dump_ino_t ino;
552 struct mapdirs_context mdc;
554 int ret, change = 0, nodump;
556 isdir = 0; /* XXX just to get gcc to shut up */
557 for (map = dumpdirmap, ino = 1; ino < maxino; ino++) {
558 if (((ino - 1) % NBBY) == 0) /* map is offset by 1 */
563 * If dir has been removed from the used map, it's either
564 * because it had the nodump flag, or it herited it from
565 * its parent. A directory can't be in dumpinomap if not
566 * in usedinomap, but we have to go through it anyway
567 * to propagate the nodump attribute.
569 nodump = (TSTINO(ino, usedinomap) == 0);
570 if ((isdir & 1) == 0 ||
571 (TSTINO(ino, dumpinomap) && nodump == 0))
578 mdc.tapesize = tapesize;
579 ext2fs_dir_iterate(fs, ino, 0, NULL, searchdir, (void *) &mdc);
580 #else /* __linux__ */
581 filesize = dp->di_size;
582 for (ret = 0, i = 0; filesize > 0 && i < NDADDR; i++) {
583 if (dp->di_db[i] != 0)
584 ret |= searchdir(ino, dp->di_db[i],
585 (long)dblksize(sblock, dp, i),
587 if (ret & HASDUMPEDFILE)
590 filesize -= sblock->fs_bsize;
592 for (i = 0; filesize > 0 && i < NIADDR; i++) {
593 if (dp->di_ib[i] == 0)
595 ret |= dirindir(ino, dp->di_ib[i], i, &filesize);
597 #endif /* __linux__ */
598 if (ret & HASDUMPEDFILE) {
599 SETINO(ino, dumpinomap);
600 *tapesize += blockest(dp);
605 if (ret & HASSUBDIRS)
606 change = 1; /* subdirs have inherited nodump */
607 CLRINO(ino, dumpdirmap);
608 } else if ((ret & HASSUBDIRS) == 0) {
609 if (!TSTINO(ino, dumpinomap)) {
610 CLRINO(ino, dumpdirmap);
620 * Read indirect blocks, and pass the data blocks to be searched
621 * as directories. Quit as soon as any entry is found that will
622 * require the directory to be dumped.
625 dirindir(dump_ino_t ino, daddr_t blkno, int ind_level, long *filesize)
629 daddr_t idblk[MAXNINDIR];
631 bread(fsbtodb(sblock, blkno), (char *)idblk, (int)sblock->fs_bsize);
632 if (ind_level <= 0) {
633 for (i = 0; *filesize > 0 && i < NINDIR(sblock); i++) {
636 ret |= searchdir(ino, blkno, sblock->fs_bsize,
638 if (ret & HASDUMPEDFILE)
641 *filesize -= sblock->fs_bsize;
646 for (i = 0; *filesize > 0 && i < NINDIR(sblock); i++) {
649 ret |= dirindir(ino, blkno, ind_level, filesize);
653 #endif /* !__linux__ */
656 * Scan a disk block containing directory information looking to see if
657 * any of the entries are on the dump list and to see if the directory
658 * contains any subdirectories.
662 searchdir(struct ext2_dir_entry *dp, int offset, int blocksize, char *buf, void *private)
664 struct mapdirs_context *mdc;
669 mdc = (struct mapdirs_context *)private;
671 tapesize = mdc->tapesize;
675 if (dp->name[0] == '.') {
676 if (( dp->name_len & 0xFF ) == 1)
678 if (dp->name[1] == '.' && ( dp->name_len & 0xFF ) == 2)
682 ip = getino(dp->inode);
683 if (TSTINO(dp->inode, dumpinomap)) {
684 CLRINO(dp->inode, dumpinomap);
685 CLRINO(dp->inode, usedinomap);
686 *tapesize -= blockest(ip);
688 /* Add dir back to the dir map, to propagate nodump */
689 if ((ip->di_mode & IFMT) == IFDIR) {
690 SETINO(dp->inode, dumpdirmap);
694 if (TSTINO(dp->inode, dumpinomap)) {
695 *ret |= HASDUMPEDFILE;
696 if (*ret & HASSUBDIRS)
699 if (TSTINO(dp->inode, dumpdirmap)) {
701 if (*ret & HASDUMPEDFILE)
708 #else /* __linux__ */
711 searchdir(dump_ino_t ino, daddr_t blkno, long size, long filesize)
713 register struct direct *dp;
714 register long loc, ret = 0;
717 bread(fsbtodb(sblock, blkno), dblk, (int)size);
720 for (loc = 0; loc < size; ) {
721 dp = (struct direct *)(dblk + loc);
722 if (dp->d_reclen == 0) {
723 msg("corrupted directory, inumber %d\n", ino);
729 if (dp->d_name[0] == '.') {
730 if (dp->d_name[1] == '\0')
732 if (dp->d_name[1] == '.' && dp->d_name[2] == '\0')
735 if (TSTINO(dp->d_ino, dumpinomap)) {
736 ret |= HASDUMPEDFILE;
737 if (ret & HASSUBDIRS)
740 if (TSTINO(dp->d_ino, dumpdirmap)) {
742 if (ret & HASDUMPEDFILE)
748 #endif /* __linux__ */
752 struct block_context {
761 * Dump a block to the tape
764 dumponeblock(ext2_filsys fs, blk_t *blocknr, e2_blkcnt_t blockcnt,
765 blk_t ref_block, int ref_offset, void * private)
767 struct block_context *p;
770 if (blockcnt < NDADDR)
772 p = (struct block_context *)private;
773 for (i = p->next_block; i < blockcnt; i++) {
774 p->buf[p->cnt++] = 0;
775 if (p->cnt == p->max) {
776 blksout (p->buf, p->cnt, p->ino);
780 p->buf[p->cnt++] = *blocknr;
781 if (p->cnt == p->max) {
782 blksout (p->buf, p->cnt, p->ino);
785 p->next_block = blockcnt + 1;
791 * Dump passes 3 and 4.
793 * Dump the contents of an inode to tape.
796 dumpino(struct dinode *dp, dump_ino_t ino)
799 fsizeT size, remaining;
801 struct old_bsd_inode obi;
804 struct block_context bc;
808 u_quad_t i_size = dp->di_size + ((u_quad_t) dp->di_size_high << 32);
812 dumpmap(dumpinomap, TS_BITS, ino);
814 CLRINO(ino, dumpinomap);
816 memset(&obi, 0, sizeof(obi));
817 obi.di_mode = dp->di_mode;
818 obi.di_uid = dp->di_uid;
819 obi.di_gid = dp->di_gid;
820 obi.di_qsize.v = i_size;
821 obi.di_atime = dp->di_atime;
822 obi.di_mtime = dp->di_mtime;
823 obi.di_ctime = dp->di_ctime;
824 obi.di_nlink = dp->di_nlink;
825 obi.di_blocks = dp->di_blocks;
826 obi.di_flags = dp->di_flags;
827 obi.di_gen = dp->di_gen;
828 memmove(&obi.di_db, &dp->di_db, (NDADDR + NIADDR) * sizeof(daddr_t));
830 warn("ACLs in inode #%ld won't be dumped", (long)ino);
831 memmove(&spcl.c_dinode, &obi, sizeof(obi));
832 #else /* __linux__ */
834 #endif /* __linux__ */
835 spcl.c_type = TS_INODE;
837 switch (dp->di_mode & S_IFMT) {
847 msg("Warning: dumpino called on a directory (ino %d)\n", ino);
853 * Check for short symbolic link.
857 i_size < EXT2_N_BLOCKS * sizeof (daddr_t)) {
861 memmove(buf, dp->di_db, (u_long)dp->di_size);
862 buf[dp->di_size] = '\0';
866 #endif /* __linux__ */
868 if (dp->di_size > 0 &&
869 dp->di_size < sblock->fs_maxsymlinklen) {
873 memmove(buf, dp->di_shortlink, (u_long)dp->di_size);
874 buf[dp->di_size] = '\0';
897 msg("Warning: undefined file type 0%o\n", dp->di_mode & IFMT);
900 if (i_size > NDADDR * sblock->fs_bsize)
902 cnt = NDADDR * EXT2_FRAGS_PER_BLOCK(fs->super);
904 cnt = NDADDR * sblock->fs_frag;
907 cnt = howmany(i_size, sblock->fs_fsize);
908 blksout(&dp->di_db[0], cnt, ino);
909 if ((quad_t) (size = i_size - NDADDR * sblock->fs_bsize) <= 0)
912 bc.max = NINDIR(sblock) * EXT2_FRAGS_PER_BLOCK(fs->super);
913 bc.buf = (int *)malloc (bc.max * sizeof (int));
916 bc.next_block = NDADDR;
918 ext2fs_block_iterate2(fs, (ext2_ino_t)ino, 0, NULL, dumponeblock, (void *)&bc);
919 /* deal with holes at the end of the inode */
920 remaining = i_size - bc.next_block*sblock->fs_fsize;
922 for (i = 0; i < howmany(remaining, sblock->fs_fsize); i++) {
923 bc.buf[bc.cnt++] = 0;
924 if (bc.cnt == bc.max) {
925 blksout (bc.buf, bc.cnt, bc.ino);
930 blksout (bc.buf, bc.cnt, bc.ino);
934 for (ind_level = 0; ind_level < NIADDR; ind_level++) {
935 dmpindir(ino, dp->di_ib[ind_level], ind_level, &size);
944 struct convert_dir_context {
952 * This function converts an ext2fs directory entry to the BSD format.
954 * Basically, it adds a null-character at the end of the name, recomputes the
955 * size of the entry, and creates it in a temporary buffer
958 convert_dir(struct ext2_dir_entry *dirent, int offset, int blocksize, char *buf, void *private)
960 struct convert_dir_context *p;
961 struct olddirect *dp;
964 p = (struct convert_dir_context *)private;
966 reclen = EXT2_DIR_REC_LEN((dirent->name_len & 0xFF) + 1);
967 if (((p->offset + reclen - 1) / p->bs) != (p->offset / p->bs)) {
968 dp = (struct olddirect *)(p->buf + p->prev_offset);
969 dp->d_reclen += p->bs - (p->offset % p->bs);
970 p->offset += p->bs - (p->offset % p->bs);
973 dp = (struct olddirect *)(p->buf + p->offset);
974 dp->d_ino = dirent->inode;
975 dp->d_reclen = reclen;
976 dp->d_namlen = dirent->name_len & 0xFF;
977 strncpy(dp->d_name, dirent->name, dp->d_namlen);
978 dp->d_name[dp->d_namlen] = '\0';
979 p->prev_offset = p->offset;
988 * Dumps a directory to tape after converting it to the BSD format
991 dumpdirino(struct dinode *dp, dump_ino_t ino)
995 struct old_bsd_inode obi;
996 struct convert_dir_context cdc;
998 struct ext2_dir_entry *de;
1003 dumpmap(dumpinomap, TS_BITS, ino);
1005 CLRINO(ino, dumpinomap);
1008 * Convert the directory to the BSD format
1010 /* Allocate a buffer for the conversion (twice the size of the
1011 ext2fs directory to avoid problems ;-) */
1012 cdc.buf = (char *)malloc(dp->di_size * 2 * sizeof(char));
1013 if (cdc.buf == NULL)
1014 err(1, "Cannot allocate buffer to convert directory #%lu\n",
1015 (unsigned long)ino);
1017 cdc.prev_offset = 0;
1018 cdc.bs = MIN(DIRBLKSIZ, TP_BSIZE);
1019 /* Do the conversion */
1020 retval = ext2fs_dir_iterate(fs, (ext2_ino_t)ino, 0, NULL, convert_dir, (void *)&cdc);
1022 com_err(disk, retval, "while converting directory #%ld\n", (long)ino);
1025 /* Fix the last entry */
1026 if ((cdc.offset % cdc.bs) != 0) {
1027 de = (struct ext2_dir_entry *)(cdc.buf + cdc.prev_offset);
1028 de->rec_len += cdc.bs - (cdc.offset % cdc.bs);
1029 cdc.offset += cdc.bs - (cdc.offset % cdc.bs);
1032 dir_size = cdc.offset;
1035 memset(&obi, 0, sizeof(obi));
1036 obi.di_mode = dp->di_mode;
1037 obi.di_uid = dp->di_uid;
1038 obi.di_gid = dp->di_gid;
1039 obi.di_qsize.v = dir_size; /* (u_quad_t)dp->di_size; */
1040 obi.di_atime = dp->di_atime;
1041 obi.di_mtime = dp->di_mtime;
1042 obi.di_ctime = dp->di_ctime;
1043 obi.di_nlink = dp->di_nlink;
1044 obi.di_blocks = dp->di_blocks;
1045 obi.di_flags = dp->di_flags;
1046 obi.di_gen = dp->di_gen;
1047 memmove(&obi.di_db, &dp->di_db, (NDADDR + NIADDR) * sizeof(daddr_t));
1048 if (dp->di_file_acl)
1049 warn("ACLs in inode #%ld won't be dumped", (long)ino);
1050 memmove(&spcl.c_dinode, &obi, sizeof(obi));
1051 #else /* __linux__ */
1052 spcl.c_dinode = *dp;
1053 #endif /* __linux__ */
1054 spcl.c_type = TS_INODE;
1056 switch (dp->di_mode & S_IFMT) {
1067 msg("Warning: size of directory inode #%d is <= 0 (%d)!\n",
1072 msg("Warning: dumpdirino called with file type 0%o (inode #%d)\n",
1073 dp->di_mode & IFMT, ino);
1076 for (size = 0; size < dir_size; size += TP_BSIZE) {
1080 memmove(buf, cdc.buf + size, TP_BSIZE);
1082 spcl.c_type = TS_ADDR;
1085 (void)free(cdc.buf);
1087 #endif /* __linux__ */
1091 * Read indirect blocks, and pass the data blocks to be dumped.
1094 dmpindir(dump_ino_t ino, daddr_t blk, int ind_level, fsizeT *size)
1101 daddr_t idblk[MAXNINDIR];
1104 bread(fsbtodb(sblock, blk), (char *)idblk, (int) sblock->fs_bsize);
1107 * My RedHat 4.0 system doesn't have these flags; I haven't
1108 * upgraded e2fsprogs yet
1110 #if defined(EXT2_FLAG_SWAP_BYTES)
1111 if ((fs->flags & EXT2_FLAG_SWAP_BYTES) ||
1112 (fs->flags & EXT2_FLAG_SWAP_BYTES_READ))
1115 max = sblock->fs_bsize >> 2;
1116 swapme = (blk_t *) idblk;
1117 for (i = 0; i < max; i++, swapme++)
1118 *swapme = ext2fs_swab32(*swapme);
1120 #endif /* __linux__ */
1122 memset(idblk, 0, (int)sblock->fs_bsize);
1123 if (ind_level <= 0) {
1124 if (*size < NINDIR(sblock) * sblock->fs_bsize)
1125 cnt = howmany(*size, sblock->fs_fsize);
1128 cnt = NINDIR(sblock) * EXT2_FRAGS_PER_BLOCK(fs->super);
1130 cnt = NINDIR(sblock) * sblock->fs_frag;
1132 *size -= NINDIR(sblock) * sblock->fs_bsize;
1133 blksout(&idblk[0], cnt, ino);
1137 for (i = 0; i < NINDIR(sblock); i++) {
1138 dmpindir(ino, idblk[i], ind_level, size);
1146 * Collect up the data into tape record sized buffers and output them.
1149 blksout(daddr_t *blkp, int frags, dump_ino_t ino)
1151 register daddr_t *bp;
1152 int i, j, count, blks, tbperdb;
1154 blks = howmany(frags * sblock->fs_fsize, TP_BSIZE);
1155 tbperdb = sblock->fs_bsize >> tp_bshift;
1156 for (i = 0; i < blks; i += TP_NINDIR) {
1157 if (i + TP_NINDIR > blks)
1160 count = i + TP_NINDIR;
1161 for (j = i; j < count; j++)
1162 if (blkp[j / tbperdb] != 0)
1163 spcl.c_addr[j - i] = 1;
1165 spcl.c_addr[j - i] = 0;
1166 spcl.c_count = count - i;
1168 bp = &blkp[i / tbperdb];
1169 for (j = i; j < count; j += tbperdb, bp++) {
1171 if (j + tbperdb <= count)
1172 dumpblock(*bp, (int)sblock->fs_bsize);
1174 dumpblock(*bp, (count - j) * TP_BSIZE);
1177 spcl.c_type = TS_ADDR;
1182 * Dump a map to the tape.
1185 dumpmap(char *map, int type, dump_ino_t ino)
1191 spcl.c_count = howmany(mapsize * sizeof(char), TP_BSIZE);
1193 for (i = 0, cp = map; i < spcl.c_count; i++, cp += TP_BSIZE)
1198 * Write a header record to the dump tape.
1200 #if defined __linux__ && !defined(int32_t)
1201 #define int32_t __s32
1204 writeheader(dump_ino_t ino)
1206 register int32_t sum, cnt, *lp;
1208 spcl.c_inumber = ino;
1209 spcl.c_magic = NFS_MAGIC;
1210 spcl.c_checksum = 0;
1211 lp = (int32_t *)&spcl;
1213 cnt = sizeof(union u_spcl) / (4 * sizeof(int32_t));
1214 while (--cnt >= 0) {
1220 spcl.c_checksum = CHECKSUM - sum;
1221 writerec((char *)&spcl, 1);
1226 getino(dump_ino_t inum)
1228 static struct dinode dinode;
1232 err = ext2fs_read_inode(fs, (ext2_ino_t)inum, (struct ext2_inode *) &dinode);
1234 com_err(disk, err, "while reading inode #%ld\n", (long)inum);
1239 #else /* __linux__ */
1241 getino(dump_ino_t inum)
1243 static daddr_t minino, maxino;
1244 static struct dinode inoblock[MAXINOPB];
1247 if (inum >= minino && inum < maxino)
1248 return (&inoblock[inum - minino]);
1249 bread(fsbtodb(sblock, ino_to_fsba(sblock, inum)), (char *)inoblock,
1250 (int)sblock->fs_bsize);
1251 minino = inum - (inum % INOPB(sblock));
1252 maxino = minino + INOPB(sblock);
1253 return (&inoblock[inum - minino]);
1255 #endif /* __linux__ */
1258 * Read a chunk of data from the disk.
1259 * Try to recover from hard errors by reading in sector sized pieces.
1260 * Error recovery is attempted at most BREADEMAX times before seeking
1261 * consent from the operator to continue.
1263 int breaderrors = 0;
1264 #define BREADEMAX 32
1267 bread(daddr_t blkno, char *buf, int size)
1274 if (ext2fs_llseek(diskfd, (((ext2_loff_t)blkno) << dev_bshift), 0) !=
1275 (((ext2_loff_t)blkno) << dev_bshift))
1277 if (lseek(diskfd, ((off_t)blkno << dev_bshift), 0) !=
1278 ((off_t)blkno << dev_bshift))
1280 msg("bread: lseek fails\n");
1281 if ((cnt = read(diskfd, buf, size)) == size)
1283 if (blkno + (size / dev_bsize) > fsbtodb(sblock, sblock->fs_size)) {
1285 * Trying to read the final fragment.
1287 * NB - dump only works in TP_BSIZE blocks, hence
1288 * rounds `dev_bsize' fragments up to TP_BSIZE pieces.
1289 * It should be smarter about not actually trying to
1290 * read more than it can get, but for the time being
1291 * we punt and scale back the read only when it gets
1292 * us into trouble. (mkm 9/25/83)
1298 msg("read error from %s: %s: [block %d]: count=%d\n",
1299 disk, strerror(errno), blkno, size);
1301 msg("short read error from %s: [block %d]: count=%d, got=%d\n",
1302 disk, blkno, size, cnt);
1303 if (++breaderrors > BREADEMAX) {
1304 msg("More than %d block read errors from %d\n",
1306 broadcast("DUMP IS AILING!\n");
1307 msg("This is an unrecoverable error.\n");
1308 if (!query("Do you want to attempt to continue?")){
1315 * Zero buffer, then try to read each sector of buffer separately.
1317 memset(buf, 0, size);
1318 for (i = 0; i < size; i += dev_bsize, buf += dev_bsize, blkno++) {
1320 if (ext2fs_llseek(diskfd, (((ext2_loff_t)blkno) << dev_bshift), 0) !=
1321 (((ext2_loff_t)blkno) << dev_bshift))
1323 if (lseek(diskfd, ((off_t)blkno << dev_bshift), 0) !=
1324 ((off_t)blkno << dev_bshift))
1326 msg("bread: lseek2 fails!\n");
1327 if ((cnt = read(diskfd, buf, (int)dev_bsize)) == dev_bsize)
1330 msg("read error from %s: %s: [sector %d]: count=%d\n",
1331 disk, strerror(errno), blkno, dev_bsize);
1334 msg("short read error from %s: [sector %d]: count=%d, got=%d\n",
1335 disk, blkno, dev_bsize, cnt);