ChangeLog, Makefile.in, swapfs.c, unix.c:
  Makefile.in: Add message.c and swapfs.c to the list of source files to
  	build the make depend.
  swapfs.c, unix.c: Only support the -s and -S options to e2fsck if
  	ENABLE_SWAPFS is defined.
Many files:
  ext2fs.h, bitops.h, block.c, bmap.c, closefs.c, dirblock.c, inode.c,
  	native.c, openfs.c, rw_bitmaps.c, swapfs.c: Only include the
  	byte-swapping logic if ENABLE_SWAPFS is turned on or if we're on a
  	big-endian machine.
  initialize.c (ext2fs_initialize):Use WORDS_BIGENDIAN directly to set
  	EXT2_FLAG_SWAP_BYTES, instead of using ext2fs_native_flag.
  native.c (ext2fs_native_flag): Use WORDS_BIGENDIAN provided by
  	autoconf to determine whether or not return EXT2_FLAG_SWAP_BYTES.

diff --git a/e2fsck/ChangeLog b/e2fsck/ChangeLog
index 28cbf55..c10b5c0 100644
--- a/e2fsck/ChangeLog
+++ b/e2fsck/ChangeLog
@@ -1,3 +1,11 @@
+2001-06-11  Theodore Tso  <tytso@valinux.com>
+
+	* Makefile.in: Add message.c and swapfs.c to the list of source
+		files to build the make depend.
+
+	* swapfs.c, unix.c: Only support the -s and -S options to e2fsck
+		if ENABLE_SWAPFS is defined.
+
 2001-06-08  Theodore Tso  <tytso@valinux.com>
 
 	* recover.c, revoke.c: Synchronize with ext3 0.7a
diff --git a/e2fsck/Makefile.in b/e2fsck/Makefile.in
index a813e86..0b71fca 100644
--- a/e2fsck/Makefile.in
+++ b/e2fsck/Makefile.in
@@ -83,6 +83,8 @@
 	$(srcdir)/dirinfo.c \
 	$(srcdir)/ehandler.c \
 	$(srcdir)/problem.c \
+	$(srcdir)/message.c \
+	$(srcdir)/swapfs.c \
 	$(MTRACE_SRC)
 
 all:: profiled $(PROGS) e2fsck.static e2fsck.shared $(MANPAGES)
diff --git a/e2fsck/swapfs.c b/e2fsck/swapfs.c
index 620dacc..513859b 100644
--- a/e2fsck/swapfs.c
+++ b/e2fsck/swapfs.c
@@ -16,6 +16,8 @@
 #include <et/com_err.h>
 #include "e2fsck.h"
 
+#ifdef ENABLE_SWAPFS
+
 struct swap_block_struct {
 	ext2_ino_t	ino;
 	int		isdir;
@@ -228,4 +230,4 @@
 #endif
 }
 
-
+#endif
diff --git a/e2fsck/unix.c b/e2fsck/unix.c
index 049e273..f7ed56d 100644
--- a/e2fsck/unix.c
+++ b/e2fsck/unix.c
@@ -572,11 +572,20 @@
 		case 'N':
 			ctx->device_name = optarg;
 			break;
+#ifdef ENABLE_SWAPFS
 		case 's':
 			normalize_swapfs = 1;
 		case 'S':
 			swapfs = 1;
 			break;
+#else
+		case 's':
+		case 'S':
+			fprintf(stderr, _("Byte-swapping filesystems "
+					  "not compiled in this version "
+					  "of e2fsck\n"));
+			exit(1);
+#endif
 		default:
 			usage(ctx);
 		}
@@ -597,7 +606,7 @@
 				ctx->filesystem_name);
 			fatal_error(ctx, 0);
 		}
-		if ((retval == ext2fs_sync_device(fd, 1))) {
+		if ((retval = ext2fs_sync_device(fd, 1))) {
 			com_err("ext2fs_sync_device", retval,
 				_("while trying to flush %s"),
 				ctx->filesystem_name);
@@ -605,6 +614,7 @@
 		}
 		close(fd);
 	}
+#ifdef ENABLE_SWAPFS
 	if (swapfs) {
 		if (cflag || bad_blocks_file) {
 			fprintf(stderr, _("Incompatible options not "
@@ -612,6 +622,7 @@
 			exit(FSCK_USAGE);
 		}
 	}
+#endif
 #ifdef HAVE_SIGNAL_H
 	/*
 	 * Set up signal action
@@ -880,7 +891,7 @@
 		test_disk(ctx);
 	if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
 		fatal_error(ctx, 0);
-
+#ifdef ENABLE_SWAPFS
 	if (normalize_swapfs) {
 		if ((fs->flags & EXT2_FLAG_SWAP_BYTES) ==
 		    ext2fs_native_flag()) {
@@ -894,6 +905,7 @@
 		if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
 			fatal_error(ctx, 0);
 	}
+#endif
 
 	/*
 	 * Mark the system as valid, 'til proven otherwise
diff --git a/lib/ext2fs/ChangeLog b/lib/ext2fs/ChangeLog
index 4ff3df4..f26f76b 100644
--- a/lib/ext2fs/ChangeLog
+++ b/lib/ext2fs/ChangeLog
@@ -1,3 +1,18 @@
+2001-06-11  Theodore Tso  <tytso@valinux.com>
+
+	* ext2fs.h, bitops.h, block.c, bmap.c, closefs.c, dirblock.c,
+		inode.c, native.c, openfs.c, rw_bitmaps.c, swapfs.c: Only
+		include the byte-swapping logic if ENABLE_SWAPFS is turned
+		on or if we're on a big-endian machine.
+	
+	* initialize.c (ext2fs_initialize):Use WORDS_BIGENDIAN directly to
+		set EXT2_FLAG_SWAP_BYTES, instead of using
+		ext2fs_native_flag.
+	
+	* native.c (ext2fs_native_flag): Use WORDS_BIGENDIAN provided by
+		autoconf to determine whether or not return
+		EXT2_FLAG_SWAP_BYTES.
+
 2001-06-10  Theodore Tso  <tytso@valinux.com>
 
 	* Makefile.in: Remove the dependence on the libe2p library.
diff --git a/lib/ext2fs/bitops.h b/lib/ext2fs/bitops.h
index f91f564..f4b8e26 100644
--- a/lib/ext2fs/bitops.h
+++ b/lib/ext2fs/bitops.h
@@ -156,6 +156,7 @@
 	return oldbit;
 }
 
+#ifdef EXT2FS_ENABLE_SWAPFS
 _INLINE_ __u32 ext2fs_swab32(__u32 val)
 {
 #ifdef EXT2FS_REQUIRE_486
@@ -177,6 +178,7 @@
 		:  "0" (val)); \
 		return val;
 }
+#endif
 
 #undef EXT2FS_ADDR
 
@@ -337,7 +339,7 @@
 
 #endif /* __sparc__ */
 
-#ifndef _EXT2_HAVE_ASM_SWAB_
+#if !defined(_EXT2_HAVE_ASM_SWAB_) && defined(ENABLE_SWAPFS)
 
 _INLINE_ __u16 ext2fs_swab16(__u16 val)
 {
diff --git a/lib/ext2fs/block.c b/lib/ext2fs/block.c
index 1abbf38..a0bf738 100644
--- a/lib/ext2fs/block.c
+++ b/lib/ext2fs/block.c
@@ -69,12 +69,14 @@
 		ret |= BLOCK_ERROR;
 		return ret;
 	}
+#ifdef EXT2FS_ENABLE_SWAPFS
 	if (ctx->fs->flags & (EXT2_FLAG_SWAP_BYTES |
 			      EXT2_FLAG_SWAP_BYTES_READ)) {
 		block_nr = (blk_t *) ctx->ind_buf;
 		for (i = 0; i < limit; i++, block_nr++)
 			*block_nr = ext2fs_swab32(*block_nr);
 	}
+#endif
 	block_nr = (blk_t *) ctx->ind_buf;
 	offset = 0;
 	if (ctx->flags & BLOCK_FLAG_APPEND) {
@@ -106,12 +108,14 @@
 	}
 	if (!(ctx->fs->flags & EXT2_FLAG_IMAGE_FILE) &&
 	    (changed & BLOCK_CHANGED)) {
+#ifdef EXT2FS_ENABLE_SWAPFS
 		if (ctx->fs->flags & (EXT2_FLAG_SWAP_BYTES |
 				      EXT2_FLAG_SWAP_BYTES_WRITE)) {
 			block_nr = (blk_t *) ctx->ind_buf;
 			for (i = 0; i < limit; i++, block_nr++)
 				*block_nr = ext2fs_swab32(*block_nr);
 		}
+#endif
 		ctx->errcode = io_channel_write_blk(ctx->fs->io, *ind_block,
 						    1, ctx->ind_buf);
 		if (ctx->errcode)
@@ -159,12 +163,14 @@
 		ret |= BLOCK_ERROR;
 		return ret;
 	}
+#ifdef EXT2FS_ENABLE_SWAPFS
 	if (ctx->fs->flags & (EXT2_FLAG_SWAP_BYTES |
 			      EXT2_FLAG_SWAP_BYTES_READ)) {
 		block_nr = (blk_t *) ctx->dind_buf;
 		for (i = 0; i < limit; i++, block_nr++)
 			*block_nr = ext2fs_swab32(*block_nr);
 	}
+#endif
 	block_nr = (blk_t *) ctx->dind_buf;
 	offset = 0;
 	if (ctx->flags & BLOCK_FLAG_APPEND) {
@@ -198,12 +204,14 @@
 	}
 	if (!(ctx->fs->flags & EXT2_FLAG_IMAGE_FILE) &&
 	    (changed & BLOCK_CHANGED)) {
+#ifdef EXT2FS_ENABLE_SWAPFS
 		if (ctx->fs->flags & (EXT2_FLAG_SWAP_BYTES |
 				      EXT2_FLAG_SWAP_BYTES_WRITE)) {
 			block_nr = (blk_t *) ctx->dind_buf;
 			for (i = 0; i < limit; i++, block_nr++)
 				*block_nr = ext2fs_swab32(*block_nr);
 		}
+#endif
 		ctx->errcode = io_channel_write_blk(ctx->fs->io, *dind_block,
 						    1, ctx->dind_buf);
 		if (ctx->errcode)
@@ -251,12 +259,14 @@
 		ret |= BLOCK_ERROR;
 		return ret;
 	}
+#ifdef EXT2FS_ENABLE_SWAPFS
 	if (ctx->fs->flags & (EXT2_FLAG_SWAP_BYTES |
 			      EXT2_FLAG_SWAP_BYTES_READ)) {
 		block_nr = (blk_t *) ctx->tind_buf;
 		for (i = 0; i < limit; i++, block_nr++)
 			*block_nr = ext2fs_swab32(*block_nr);
 	}
+#endif
 	block_nr = (blk_t *) ctx->tind_buf;
 	offset = 0;
 	if (ctx->flags & BLOCK_FLAG_APPEND) {
@@ -290,12 +300,14 @@
 	}
 	if (!(ctx->fs->flags & EXT2_FLAG_IMAGE_FILE) &&
 	    (changed & BLOCK_CHANGED)) {
+#ifdef EXT2FS_ENABLE_SWAPFS
 		if (ctx->fs->flags & (EXT2_FLAG_SWAP_BYTES |
 				      EXT2_FLAG_SWAP_BYTES_WRITE)) {
 			block_nr = (blk_t *) ctx->tind_buf;
 			for (i = 0; i < limit; i++, block_nr++)
 				*block_nr = ext2fs_swab32(*block_nr);
 		}
+#endif
 		ctx->errcode = io_channel_write_blk(ctx->fs->io, *tind_block,
 						    1, ctx->tind_buf);
 		if (ctx->errcode)
diff --git a/lib/ext2fs/bmap.c b/lib/ext2fs/bmap.c
index 6d73328..25ba695 100644
--- a/lib/ext2fs/bmap.c
+++ b/lib/ext2fs/bmap.c
@@ -51,9 +51,11 @@
 
 	b = ((blk_t *) block_buf)[nr];
 
+#ifdef EXT2FS_ENABLE_SWAPFS
 	if ((fs->flags & EXT2_FLAG_SWAP_BYTES) ||
 	    (fs->flags & EXT2_FLAG_SWAP_BYTES_READ))
 		b = ext2fs_swab32(b);
+#endif
 
 	if (!b && (flags & BMAP_ALLOC)) {
 		b = nr ? ((blk_t *) block_buf)[nr-1] : 0;
@@ -62,10 +64,12 @@
 		if (retval)
 			return retval;
 
+#ifdef EXT2FS_ENABLE_SWAPFS
 		if ((fs->flags & EXT2_FLAG_SWAP_BYTES) ||
 		    (fs->flags & EXT2_FLAG_SWAP_BYTES_WRITE))
 			((blk_t *) block_buf)[nr] = ext2fs_swab32(b);
 		else
+#endif
 			((blk_t *) block_buf)[nr] = b;
 
 		retval = io_channel_write_blk(fs->io, ind, 1, block_buf);
diff --git a/lib/ext2fs/closefs.c b/lib/ext2fs/closefs.c
index 91a342a..69a769f 100644
--- a/lib/ext2fs/closefs.c
+++ b/lib/ext2fs/closefs.c
@@ -129,6 +129,7 @@
 
 	fs->super->s_wtime = time(NULL);
 	fs->super->s_block_group_nr = 0;
+#ifdef EXT2FS_ENABLE_SWAPFS
 	if (fs->flags & EXT2_FLAG_SWAP_BYTES) {
 		retval = EXT2_ET_NO_MEMORY;
 		retval = ext2fs_get_mem(SUPERBLOCK_SIZE,
@@ -157,6 +158,10 @@
 		super_shadow = fs->super;
 		group_shadow = fs->group_desc;
 	}
+#else
+	super_shadow = fs->super;
+	group_shadow = fs->group_desc;
+#endif
 	
 	/*
 	 * Write out master superblock.  This has to be done
@@ -183,10 +188,12 @@
 	 * we exit.)
 	 */
 	fs->super->s_state &= ~EXT2_VALID_FS;
+#ifdef EXT2FS_ENABLE_SWAPFS
 	if (fs->flags & EXT2_FLAG_SWAP_BYTES) {
 		*super_shadow = *fs->super;
 		ext2fs_swap_super(super_shadow);
 	}
+#endif
 
 	/*
 	 * Write out the master group descriptors, and the backup
@@ -202,9 +209,11 @@
 		sgrp = i;
 		if (sgrp > ((1 << 16) - 1))
 			sgrp = (1 << 16) - 1;
+#ifdef EXT2FS_ENABLE_SWAPFS
 		if (fs->flags & EXT2_FLAG_SWAP_BYTES)
 			super_shadow->s_block_group_nr = ext2fs_swab16(sgrp);
 		else
+#endif
 			fs->super->s_block_group_nr = sgrp;
 
 		if (i !=0 ) {
diff --git a/lib/ext2fs/dirblock.c b/lib/ext2fs/dirblock.c
index d1d8ca8..846aebe 100644
--- a/lib/ext2fs/dirblock.c
+++ b/lib/ext2fs/dirblock.c
@@ -31,17 +31,21 @@
  	retval = io_channel_read_blk(fs->io, block, 1, buf);
 	if (retval)
 		return retval;
+#ifdef EXT2FS_ENABLE_SWAPFS
 	do_swap = (fs->flags & (EXT2_FLAG_SWAP_BYTES|
 				EXT2_FLAG_SWAP_BYTES_READ)) != 0;
+#endif
 	p = (char *) buf;
 	end = (char *) buf + fs->blocksize;
 	while (p < end-8) {
 		dirent = (struct ext2_dir_entry *) p;
+#ifdef EXT2FS_ENABLE_SWAPFS
 		if (do_swap) {
 			dirent->inode = ext2fs_swab32(dirent->inode);
 			dirent->rec_len = ext2fs_swab16(dirent->rec_len);
 			dirent->name_len = ext2fs_swab16(dirent->name_len);
 		}
+#endif
 		rec_len = dirent->rec_len;
 		if ((rec_len < 8) || (rec_len % 4)) {
 			rec_len = 8;
@@ -63,6 +67,7 @@
 	char		*buf = 0;
 	struct ext2_dir_entry *dirent;
 
+#ifdef EXT2FS_ENABLE_SWAPFS
 	if ((fs->flags & EXT2_FLAG_SWAP_BYTES) ||
 	    (fs->flags & EXT2_FLAG_SWAP_BYTES_WRITE)) {
 		retval = ext2fs_get_mem(fs->blocksize, (void **) &buf);
@@ -85,6 +90,7 @@
 			dirent->name_len = ext2fs_swab16(dirent->name_len);
 		}
 	} else
+#endif
 		write_buf = (char *) inbuf;
  	retval = io_channel_write_blk(fs->io, block, 1, write_buf);
 errout:
diff --git a/lib/ext2fs/ext2fs.h b/lib/ext2fs/ext2fs.h
index d0ac235..f89ccc7 100644
--- a/lib/ext2fs/ext2fs.h
+++ b/lib/ext2fs/ext2fs.h
@@ -24,6 +24,15 @@
 #endif
 
 /*
+ * Build in support for byte-swapping filesystems if we the feature
+ * has been configured or if we're being built on a CPU architecture
+ * with a non-native byte order.
+ */
+#if defined(ENABLE_SWAPFS) || defined(WORDS_BIGENDIAN)
+#define EXT2FS_ENABLE_SWAPFS
+#endif
+
+/*
  * Where the master copy of the superblock is located, and how big
  * superblocks are supposed to be.  We define SUPERBLOCK_SIZE because
  * the size of the superblock structure is not necessarily trustworthy
diff --git a/lib/ext2fs/initialize.c b/lib/ext2fs/initialize.c
index 8022aee..821036e 100644
--- a/lib/ext2fs/initialize.c
+++ b/lib/ext2fs/initialize.c
@@ -77,7 +77,10 @@
 	
 	memset(fs, 0, sizeof(struct struct_ext2_filsys));
 	fs->magic = EXT2_ET_MAGIC_EXT2FS_FILSYS;
-	fs->flags = flags | EXT2_FLAG_RW | ext2fs_native_flag();
+	fs->flags = flags | EXT2_FLAG_RW;
+#ifdef WORDS_BIGENDIAN
+	fs->flags |= EXT2_FLAG_SWAP_BYTES;
+#endif
 	retval = manager->open(name, IO_FLAG_RW, &fs->io);
 	if (retval)
 		goto cleanup;
diff --git a/lib/ext2fs/inode.c b/lib/ext2fs/inode.c
index 4d4911a..5b4dd2b 100644
--- a/lib/ext2fs/inode.c
+++ b/lib/ext2fs/inode.c
@@ -445,21 +445,25 @@
 		scan->ptr += scan->inode_size - extra_bytes;
 		scan->bytes_left -= scan->inode_size - extra_bytes;
 
+#ifdef EXT2FS_ENABLE_SWAPFS
 		if ((scan->fs->flags & EXT2_FLAG_SWAP_BYTES) ||
 		    (scan->fs->flags & EXT2_FLAG_SWAP_BYTES_READ))
 			ext2fs_swap_inode(scan->fs, inode,
 				 (struct ext2_inode *) scan->temp_buffer, 0);
 		else
+#endif
 			*inode = *((struct ext2_inode *) scan->temp_buffer);
 		if (scan->scan_flags & EXT2_SF_BAD_EXTRA_BYTES)
 			retval = EXT2_ET_BAD_BLOCK_IN_INODE_TABLE;
 		scan->scan_flags &= ~EXT2_SF_BAD_EXTRA_BYTES;
 	} else {
+#ifdef EXT2FS_ENABLE_SWAPFS
 		if ((scan->fs->flags & EXT2_FLAG_SWAP_BYTES) ||
 		    (scan->fs->flags & EXT2_FLAG_SWAP_BYTES_READ))
 			ext2fs_swap_inode(scan->fs, inode,
 				 (struct ext2_inode *) scan->ptr, 0);
 		else
+#endif
 			*inode = *((struct ext2_inode *) scan->ptr);
 		scan->ptr += scan->inode_size;
 		scan->bytes_left -= scan->inode_size;
@@ -556,9 +560,11 @@
 	} else
 		memcpy((char *) inode, ptr, length);
 	
+#ifdef EXT2FS_ENABLE_SWAPFS
 	if ((fs->flags & EXT2_FLAG_SWAP_BYTES) ||
 	    (fs->flags & EXT2_FLAG_SWAP_BYTES_READ))
 		ext2fs_swap_inode(fs, inode, inode, 0);
+#endif
 
 	/* Update the inode cache */
 	fs->icache->cache_last = (fs->icache->cache_last + 1) %
@@ -607,10 +613,12 @@
 	if ((ino == 0) || (ino > fs->super->s_inodes_count))
 		return EXT2_ET_BAD_INODE_NUM;
 
+#ifdef EXT2FS_ENABLE_SWAPFS
 	if ((fs->flags & EXT2_FLAG_SWAP_BYTES) ||
 	    (fs->flags & EXT2_FLAG_SWAP_BYTES_WRITE))
 		ext2fs_swap_inode(fs, &temp_inode, inode, 1);
 	else
+#endif
 		memcpy(&temp_inode, inode, sizeof(struct ext2_inode));
 	
 	group = (ino - 1) / EXT2_INODES_PER_GROUP(fs->super);
diff --git a/lib/ext2fs/native.c b/lib/ext2fs/native.c
index 6315a91..85d0989 100644
--- a/lib/ext2fs/native.c
+++ b/lib/ext2fs/native.c
@@ -14,19 +14,13 @@
 #include "ext2_fs.h"
 #include "ext2fs.h"
 
-static int i386_byteorder(void)
-{
-	int one = 1;
-	char *cp = (char *) &one;
-
-	return (*cp == 1);
-}
-
 int ext2fs_native_flag(void)
 {
-	if (i386_byteorder())
-		return 0;
+#ifdef WORDS_BIGENDIAN
 	return EXT2_FLAG_SWAP_BYTES;
+#else
+	return 0;
+#endif
 }
 
 	
diff --git a/lib/ext2fs/openfs.c b/lib/ext2fs/openfs.c
index 2abbb2a..96ddf49 100644
--- a/lib/ext2fs/openfs.c
+++ b/lib/ext2fs/openfs.c
@@ -119,12 +119,14 @@
 	if (fs->orig_super)
 		memcpy(fs->orig_super, fs->super, SUPERBLOCK_SIZE);
 
+#ifdef EXT2FS_ENABLE_SWAPFS
 	if ((fs->super->s_magic == ext2fs_swab16(EXT2_SUPER_MAGIC)) ||
 	    (fs->flags & EXT2_FLAG_SWAP_BYTES)) {
 		fs->flags |= EXT2_FLAG_SWAP_BYTES;
 
 		ext2fs_swap_super(fs->super);
 	}
+#endif
 	
 	if (fs->super->s_magic != EXT2_SUPER_MAGIC) {
 		retval = EXT2_ET_BAD_MAGIC;
@@ -216,6 +218,7 @@
 		if (retval)
 			goto cleanup;
 		group_block++;
+#ifdef EXT2FS_ENABLE_SWAPFS
 		if (fs->flags & EXT2_FLAG_SWAP_BYTES) {
 			gdp = (struct ext2_group_desc *) dest;
 			groups_per_block = fs->blocksize /
@@ -223,6 +226,7 @@
 			for (j=0; j < groups_per_block; j++)
 				ext2fs_swap_group_desc(gdp++);
 		}
+#endif
 		dest += fs->blocksize;
 	}
 
diff --git a/lib/ext2fs/rw_bitmaps.c b/lib/ext2fs/rw_bitmaps.c
index 559f4a6..89557db 100644
--- a/lib/ext2fs/rw_bitmaps.c
+++ b/lib/ext2fs/rw_bitmaps.c
@@ -27,7 +27,7 @@
 #include "ext2fs.h"
 #include "e2image.h"
 
-#ifdef __powerpc__
+#if defined(__powerpc__) && defined(EXT2FS_ENABLE_SWAPFS)
 /*
  * On the PowerPC, the big-endian variant of the ext2 filesystem
  * has its bitmaps stored as 32-bit words with bit 0 as the LSB
diff --git a/lib/ext2fs/swapfs.c b/lib/ext2fs/swapfs.c
index f26f7cd..f50a795 100644
--- a/lib/ext2fs/swapfs.c
+++ b/lib/ext2fs/swapfs.c
@@ -18,6 +18,7 @@
 #include "ext2_fs.h"
 #include "ext2fs.h"
 
+#ifdef EXT2FS_ENABLE_SWAPFS
 void ext2fs_swap_super(struct ext2_super_block * sb)
 {
 	sb->s_inodes_count = ext2fs_swab32(sb->s_inodes_count);
@@ -141,4 +142,4 @@
 		break;
 	}
 }
-	
+#endif