Whamcloud - gitweb
e2fsck: allow to fix some superblock corruptions
[tools/e2fsprogs.git] / lib / ext2fs / imager.c
index 053917f..7fd06f7 100644 (file)
@@ -13,6 +13,7 @@
  * %End-Header%
  */
 
+#include "config.h"
 #include <stdio.h>
 #include <string.h>
 #if HAVE_UNISTD_H
@@ -62,9 +63,10 @@ errcode_t ext2fs_image_inode_write(ext2_filsys fs, int fd, int flags)
 {
        unsigned int    group, left, c, d;
        char            *buf, *cp;
-       blk_t           blk;
+       blk64_t         blk;
        ssize_t         actual;
        errcode_t       retval;
+       off_t           r;
 
        buf = malloc(fs->blocksize * BUF_BLOCKS);
        if (!buf)
@@ -96,7 +98,12 @@ errcode_t ext2fs_image_inode_write(ext2_filsys fs, int fd, int flags)
                                        blk++;
                                        left--;
                                        cp += fs->blocksize;
-                                       lseek(fd, fs->blocksize, SEEK_CUR);
+                                       r = ext2fs_llseek(fd, fs->blocksize,
+                                                         SEEK_CUR);
+                                       if (r < 0) {
+                                               retval = errno;
+                                               goto errout;
+                                       }
                                        continue;
                                }
                                /* Find non-zero blocks */
@@ -136,7 +143,7 @@ errcode_t ext2fs_image_inode_read(ext2_filsys fs, int fd,
 {
        unsigned int    group, c, left;
        char            *buf;
-       blk_t           blk;
+       blk64_t         blk;
        ssize_t         actual;
        errcode_t       retval;
 
@@ -188,6 +195,11 @@ errcode_t ext2fs_image_super_write(ext2_filsys fs, int fd,
        char            *buf, *cp;
        ssize_t         actual;
        errcode_t       retval;
+#ifdef WORDS_BIGENDIAN
+       unsigned int    groups_per_block;
+       struct          ext2_group_desc *gdp;
+       int             j;
+#endif
 
        buf = malloc(fs->blocksize);
        if (!buf)
@@ -197,7 +209,17 @@ errcode_t ext2fs_image_super_write(ext2_filsys fs, int fd,
         * Write out the superblock
         */
        memset(buf, 0, fs->blocksize);
+#ifdef WORDS_BIGENDIAN
+       /*
+        * We're writing out superblock so let's convert
+        * it to little endian and then back if needed
+        */
+       ext2fs_swap_super(fs->super);
        memcpy(buf, fs->super, SUPERBLOCK_SIZE);
+       ext2fs_swap_super(fs->super);
+#else
+       memcpy(buf, fs->super, SUPERBLOCK_SIZE);
+#endif
        actual = write(fd, buf, fs->blocksize);
        if (actual == -1) {
                retval = errno;
@@ -211,8 +233,34 @@ errcode_t ext2fs_image_super_write(ext2_filsys fs, int fd,
        /*
         * Now write out the block group descriptors
         */
+
        cp = (char *) fs->group_desc;
+
+#ifdef WORDS_BIGENDIAN
+       /*
+        * Convert group descriptors to little endian and back
+        * if needed
+        */
+       groups_per_block = EXT2_DESC_PER_BLOCK(fs->super);
+       gdp = (struct ext2_group_desc *) cp;
+       for (j=0; j < groups_per_block*fs->desc_blocks; j++) {
+               gdp = ext2fs_group_desc(fs, fs->group_desc, j);
+               ext2fs_swap_group_desc2(fs, gdp);
+       }
+#endif
+
        actual = write(fd, cp, fs->blocksize * fs->desc_blocks);
+
+
+#ifdef WORDS_BIGENDIAN
+       groups_per_block = EXT2_DESC_PER_BLOCK(fs->super);
+       gdp = (struct ext2_group_desc *) cp;
+       for (j=0; j < groups_per_block*fs->desc_blocks; j++) {
+               gdp = ext2fs_group_desc(fs, fs->group_desc, j);
+               ext2fs_swap_group_desc2(fs, gdp);
+       }
+#endif
+
        if (actual == -1) {
                retval = errno;
                goto errout;
@@ -278,10 +326,10 @@ errout:
 errcode_t ext2fs_image_bitmap_write(ext2_filsys fs, int fd, int flags)
 {
        ext2fs_generic_bitmap   bmap;
-       errcode_t               err, retval;
+       errcode_t               retval;
        ssize_t                 actual;
-       __u32                   itr, cnt, size;
-       int                     c, total_size;
+       size_t                  c;
+       __u64                   itr, cnt, size, total_size;
        char                    buf[1024];
 
        if (flags & IMAGER_FLAG_INODEMAP) {
@@ -291,7 +339,6 @@ errcode_t ext2fs_image_bitmap_write(ext2_filsys fs, int fd, int flags)
                                return retval;
                }
                bmap = fs->inode_map;
-               err = EXT2_ET_MAGIC_INODE_BITMAP;
                itr = 1;
                cnt = EXT2_INODES_PER_GROUP(fs->super) * fs->group_desc_count;
                size = (EXT2_INODES_PER_GROUP(fs->super) / 8);
@@ -302,10 +349,9 @@ errcode_t ext2fs_image_bitmap_write(ext2_filsys fs, int fd, int flags)
                                return retval;
                }
                bmap = fs->block_map;
-               err = EXT2_ET_MAGIC_BLOCK_BITMAP;
                itr = fs->super->s_first_data_block;
-               cnt = EXT2_BLOCKS_PER_GROUP(fs->super) * fs->group_desc_count;
-               size = EXT2_BLOCKS_PER_GROUP(fs->super) / 8;
+               cnt = EXT2_GROUPS_TO_CLUSTERS(fs->super, fs->group_desc_count);
+               size = EXT2_CLUSTERS_PER_GROUP(fs->super) / 8;
        }
        total_size = size * fs->group_desc_count;
 
@@ -338,9 +384,9 @@ errcode_t ext2fs_image_bitmap_write(ext2_filsys fs, int fd, int flags)
                        if (c > (int) sizeof(buf))
                                c = sizeof(buf);
                        actual = write(fd, buf, c);
-                       if (actual == -1)
+                       if (actual < 0)
                                return errno;
-                       if (actual != c)
+                       if ((size_t) actual != c)
                                return EXT2_ET_SHORT_WRITE;
                        size -= c;
                }
@@ -355,8 +401,8 @@ errcode_t ext2fs_image_bitmap_write(ext2_filsys fs, int fd, int flags)
 errcode_t ext2fs_image_bitmap_read(ext2_filsys fs, int fd, int flags)
 {
        ext2fs_generic_bitmap   bmap;
-       errcode_t               err, retval;
-       __u32                   itr, cnt;
+       errcode_t               retval;
+       __u64                   itr, cnt;
        char                    buf[1024];
        unsigned int            size;
        ssize_t                 actual;
@@ -368,7 +414,6 @@ errcode_t ext2fs_image_bitmap_read(ext2_filsys fs, int fd, int flags)
                                return retval;
                }
                bmap = fs->inode_map;
-               err = EXT2_ET_MAGIC_INODE_BITMAP;
                itr = 1;
                cnt = EXT2_INODES_PER_GROUP(fs->super) * fs->group_desc_count;
                size = (EXT2_INODES_PER_GROUP(fs->super) / 8);
@@ -379,9 +424,8 @@ errcode_t ext2fs_image_bitmap_read(ext2_filsys fs, int fd, int flags)
                                return retval;
                }
                bmap = fs->block_map;
-               err = EXT2_ET_MAGIC_BLOCK_BITMAP;
                itr = fs->super->s_first_data_block;
-               cnt = EXT2_BLOCKS_PER_GROUP(fs->super) * fs->group_desc_count;
+               cnt = EXT2_GROUPS_TO_BLOCKS(fs->super, fs->group_desc_count);
                size = EXT2_BLOCKS_PER_GROUP(fs->super) / 8;
        }