Whamcloud - gitweb
Merge branch 'maint' into next
[tools/e2fsprogs.git] / lib / ext2fs / imager.c
index ac8309c..f8d67d8 100644 (file)
@@ -5,14 +5,15 @@
  * Copyright (C) 2000 Theodore Ts'o.
  *
  * Note: this uses the POSIX IO interfaces, unlike most of the other
- * functions in this library.  So sue me.  
+ * functions in this library.  So sue me.
  *
  * %Begin-Header%
- * This file may be redistributed under the terms of the GNU Public
- * License.
+ * This file may be redistributed under the terms of the GNU Library
+ * General Public License, version 2.
  * %End-Header%
  */
 
+#include "config.h"
 #include <stdio.h>
 #include <string.h>
 #if HAVE_UNISTD_H
@@ -60,18 +61,20 @@ static int check_zero_block(char *buf, int blocksize)
 
 errcode_t ext2fs_image_inode_write(ext2_filsys fs, int fd, int flags)
 {
-       unsigned int    group, left, c, d;
+       dgrp_t          group;
+       ssize_t         left, c, d;
        char            *buf, *cp;
-       blk_t           blk;
+       blk64_t         blk;
        ssize_t         actual;
        errcode_t       retval;
+       loff_t          r;
 
        buf = malloc(fs->blocksize * BUF_BLOCKS);
        if (!buf)
                return ENOMEM;
-       
+
        for (group = 0; group < fs->group_desc_count; group++) {
-               blk = fs->group_desc[(unsigned)group].bg_inode_table;
+               blk = ext2fs_inode_table_loc(fs, group);
                if (!blk) {
                        retval = EXT2_ET_MISSING_INODE_TABLE;
                        goto errout;
@@ -81,7 +84,7 @@ errcode_t ext2fs_image_inode_write(ext2_filsys fs, int fd, int flags)
                        c = BUF_BLOCKS;
                        if (c > left)
                                c = left;
-                       retval = io_channel_read_blk(fs->io, blk, c, buf);
+                       retval = io_channel_read_blk64(fs->io, blk, c, buf);
                        if (retval)
                                goto errout;
                        cp = buf;
@@ -96,27 +99,34 @@ 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 */
-                               for (d=1; d < c; d++) {
-                                       if (check_zero_block(cp + d*fs->blocksize, fs->blocksize))
+                               for (d = 1; d < c; d++) {
+                                       if (check_zero_block(cp +
+                                                            d * fs->blocksize,
+                                                            fs->blocksize))
                                                break;
                                }
                        skip_sparse:
-                               actual = write(fd, cp, fs->blocksize * d);
+                               actual = write(fd, cp, d * fs->blocksize);
                                if (actual == -1) {
                                        retval = errno;
                                        goto errout;
                                }
-                               if (actual != (ssize_t) (fs->blocksize * d)) {
+                               if (actual != d * fs->blocksize) {
                                        retval = EXT2_ET_SHORT_WRITE;
                                        goto errout;
                                }
                                blk += d;
                                left -= d;
-                               cp += fs->blocksize * d;
+                               cp += d * fs->blocksize;
                                c -= d;
                        }
                }
@@ -131,21 +141,22 @@ errout:
 /*
  * Read in the inode table and stuff it into place
  */
-errcode_t ext2fs_image_inode_read(ext2_filsys fs, int fd, 
+errcode_t ext2fs_image_inode_read(ext2_filsys fs, int fd,
                                  int flags EXT2FS_ATTR((unused)))
 {
-       unsigned int    group, c, left;
+       dgrp_t          group;
+       ssize_t         c, left;
        char            *buf;
-       blk_t           blk;
+       blk64_t         blk;
        ssize_t         actual;
        errcode_t       retval;
 
        buf = malloc(fs->blocksize * BUF_BLOCKS);
        if (!buf)
                return ENOMEM;
-       
+
        for (group = 0; group < fs->group_desc_count; group++) {
-               blk = fs->group_desc[(unsigned)group].bg_inode_table;
+               blk = ext2fs_inode_table_loc(fs, group);
                if (!blk) {
                        retval = EXT2_ET_MISSING_INODE_TABLE;
                        goto errout;
@@ -160,14 +171,14 @@ errcode_t ext2fs_image_inode_read(ext2_filsys fs, int fd,
                                retval = errno;
                                goto errout;
                        }
-                       if (actual != (ssize_t) (fs->blocksize * c)) {
+                       if (actual != fs->blocksize * c) {
                                retval = EXT2_ET_SHORT_READ;
                                goto errout;
                        }
-                       retval = io_channel_write_blk(fs->io, blk, c, buf);
+                       retval = io_channel_write_blk64(fs->io, blk, c, buf);
                        if (retval)
                                goto errout;
-                       
+
                        blk += c;
                        left -= c;
                }
@@ -182,12 +193,20 @@ errout:
 /*
  * Write out superblock and group descriptors
  */
-errcode_t ext2fs_image_super_write(ext2_filsys fs, int fd, 
+errcode_t ext2fs_image_super_write(ext2_filsys fs, int fd,
                                   int flags EXT2FS_ATTR((unused)))
 {
        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
+
+       if (fs->group_desc == NULL)
+               return EXT2_ET_NO_GDESC;
 
        buf = malloc(fs->blocksize);
        if (!buf)
@@ -197,7 +216,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,17 +240,43 @@ errcode_t ext2fs_image_super_write(ext2_filsys fs, int fd,
        /*
         * Now write out the block group descriptors
         */
+
        cp = (char *) fs->group_desc;
-       actual = write(fd, cp, fs->blocksize * fs->desc_blocks);
+
+#ifdef WORDS_BIGENDIAN
+       /*
+        * Convert group descriptors to little endian and back
+        * if needed
+        */
+       groups_per_block = EXT2_DESC_PER_BLOCK(fs->super);
+       for (j=0; j < groups_per_block*fs->desc_blocks; j++) {
+               gdp = ext2fs_group_desc(fs, fs->group_desc, j);
+               if (gdp)
+                       ext2fs_swap_group_desc2(fs, gdp);
+       }
+#endif
+
+       actual = write(fd, cp, (ssize_t)fs->blocksize * fs->desc_blocks);
+
+
+#ifdef WORDS_BIGENDIAN
+       groups_per_block = EXT2_DESC_PER_BLOCK(fs->super);
+       for (j=0; j < groups_per_block*fs->desc_blocks; j++) {
+               gdp = ext2fs_group_desc(fs, fs->group_desc, j);
+               if (gdp)
+                       ext2fs_swap_group_desc2(fs, gdp);
+       }
+#endif
+
        if (actual == -1) {
                retval = errno;
                goto errout;
        }
-       if (actual != (ssize_t) (fs->blocksize * fs->desc_blocks)) {
+       if (actual != (ssize_t)fs->blocksize * fs->desc_blocks) {
                retval = EXT2_ET_SHORT_WRITE;
                goto errout;
        }
-       
+
        retval = 0;
 
 errout:
@@ -232,14 +287,14 @@ errout:
 /*
  * Read the superblock and group descriptors and overwrite them.
  */
-errcode_t ext2fs_image_super_read(ext2_filsys fs, int fd, 
+errcode_t ext2fs_image_super_read(ext2_filsys fs, int fd,
                                  int flags EXT2FS_ATTR((unused)))
 {
        char            *buf;
        ssize_t         actual, size;
        errcode_t       retval;
 
-       size = fs->blocksize * (fs->group_desc_count + 1);
+       size = (ssize_t)fs->blocksize * (fs->group_desc_count + 1);
        buf = malloc(size);
        if (!buf)
                return ENOMEM;
@@ -263,7 +318,7 @@ errcode_t ext2fs_image_super_read(ext2_filsys fs, int fd,
        memcpy(fs->super, buf, SUPERBLOCK_SIZE);
 
        memcpy(fs->group_desc, buf + fs->blocksize,
-              fs->blocksize * fs->group_desc_count);
+              (ssize_t)fs->blocksize * fs->group_desc_count);
 
        retval = 0;
 
@@ -277,11 +332,12 @@ errout:
  */
 errcode_t ext2fs_image_bitmap_write(ext2_filsys fs, int fd, int flags)
 {
-       char            *ptr;
-       int             c, size;
-       char            zero_buf[1024];
-       ssize_t         actual;
-       errcode_t       retval;
+       ext2fs_generic_bitmap   bmap;
+       errcode_t               retval;
+       ssize_t                 actual;
+       size_t                  c;
+       __u64                   itr, cnt, size, total_size;
+       char                    buf[1024];
 
        if (flags & IMAGER_FLAG_INODEMAP) {
                if (!fs->inode_map) {
@@ -289,7 +345,10 @@ errcode_t ext2fs_image_bitmap_write(ext2_filsys fs, int fd, int flags)
                        if (retval)
                                return retval;
                }
-               ptr = fs->inode_map->bitmap;
+               bmap = fs->inode_map;
+               itr = 1;
+               cnt = (__u64)EXT2_INODES_PER_GROUP(fs->super) *
+                       fs->group_desc_count;
                size = (EXT2_INODES_PER_GROUP(fs->super) / 8);
        } else {
                if (!fs->block_map) {
@@ -297,43 +356,50 @@ errcode_t ext2fs_image_bitmap_write(ext2_filsys fs, int fd, int flags)
                        if (retval)
                                return retval;
                }
-               ptr = fs->block_map->bitmap;
-               size = EXT2_BLOCKS_PER_GROUP(fs->super) / 8;
+               bmap = fs->block_map;
+               itr = fs->super->s_first_data_block;
+               cnt = EXT2_GROUPS_TO_CLUSTERS(fs->super, fs->group_desc_count);
+               size = EXT2_CLUSTERS_PER_GROUP(fs->super) / 8;
        }
-       size = size * fs->group_desc_count;
-
-       actual = write(fd, ptr, size);
-       if (actual == -1) {
-               retval = errno;
-               goto errout;
+       total_size = size * fs->group_desc_count;
+
+       while (cnt > 0) {
+               size = sizeof(buf);
+               if (size > (cnt >> 3))
+                       size = (cnt >> 3);
+
+               retval = ext2fs_get_generic_bmap_range(bmap, itr,
+                                                      size << 3, buf);
+               if (retval)
+                       return retval;
+
+               actual = write(fd, buf, size);
+               if (actual == -1)
+                       return errno;
+               if (actual != (int) size)
+                       return EXT2_ET_SHORT_READ;
+
+               itr += size << 3;
+               cnt -= size << 3;
        }
-       if (actual != size) {
-               retval = EXT2_ET_SHORT_WRITE;
-               goto errout;
-       }
-       size = size % fs->blocksize;
-       memset(zero_buf, 0, sizeof(zero_buf));
+
+       size = total_size % fs->blocksize;
+       memset(buf, 0, sizeof(buf));
        if (size) {
                size = fs->blocksize - size;
                while (size) {
                        c = size;
-                       if (c > (int) sizeof(zero_buf))
-                               c = sizeof(zero_buf);
-                       actual = write(fd, zero_buf, c);
-                       if (actual == -1) {
-                               retval = errno;
-                               goto errout;
-                       }
-                       if (actual != c) {
-                               retval = EXT2_ET_SHORT_WRITE;
-                               goto errout;
-                       }
+                       if (c > (int) sizeof(buf))
+                               c = sizeof(buf);
+                       actual = write(fd, buf, c);
+                       if (actual < 0)
+                               return errno;
+                       if ((size_t) actual != c)
+                               return EXT2_ET_SHORT_WRITE;
                        size -= c;
                }
        }
-       retval = 0;
-errout:
-       return (retval);
+       return 0;
 }
 
 
@@ -342,10 +408,12 @@ errout:
  */
 errcode_t ext2fs_image_bitmap_read(ext2_filsys fs, int fd, int flags)
 {
-       char            *ptr, *buf = 0;
-       int             size;
-       ssize_t         actual;
-       errcode_t       retval;
+       ext2fs_generic_bitmap   bmap;
+       errcode_t               retval;
+       __u64                   itr, cnt;
+       char                    buf[1024];
+       unsigned int            size;
+       ssize_t                 actual;
 
        if (flags & IMAGER_FLAG_INODEMAP) {
                if (!fs->inode_map) {
@@ -353,7 +421,10 @@ errcode_t ext2fs_image_bitmap_read(ext2_filsys fs, int fd, int flags)
                        if (retval)
                                return retval;
                }
-               ptr = fs->inode_map->bitmap;
+               bmap = fs->inode_map;
+               itr = 1;
+               cnt = (__u64)EXT2_INODES_PER_GROUP(fs->super) *
+                       fs->group_desc_count;
                size = (EXT2_INODES_PER_GROUP(fs->super) / 8);
        } else {
                if (!fs->block_map) {
@@ -361,29 +432,30 @@ errcode_t ext2fs_image_bitmap_read(ext2_filsys fs, int fd, int flags)
                        if (retval)
                                return retval;
                }
-               ptr = fs->block_map->bitmap;
+               bmap = fs->block_map;
+               itr = fs->super->s_first_data_block;
+               cnt = EXT2_GROUPS_TO_BLOCKS(fs->super, fs->group_desc_count);
                size = EXT2_BLOCKS_PER_GROUP(fs->super) / 8;
        }
-       size = size * fs->group_desc_count;
 
-       buf = malloc(size);
-       if (!buf)
-               return ENOMEM;
+       while (cnt > 0) {
+               size = sizeof(buf);
+               if (size > (cnt >> 3))
+                       size = (cnt >> 3);
 
-       actual = read(fd, buf, size);
-       if (actual == -1) {
-               retval = errno;
-               goto errout;
-       }
-       if (actual != size) {
-               retval = EXT2_ET_SHORT_WRITE;
-               goto errout;
+               actual = read(fd, buf, size);
+               if (actual == -1)
+                       return errno;
+               if (actual != (int) size)
+                       return EXT2_ET_SHORT_READ;
+
+               retval = ext2fs_set_generic_bmap_range(bmap, itr,
+                                                      size << 3, buf);
+               if (retval)
+                       return retval;
+
+               itr += size << 3;
+               cnt -= size << 3;
        }
-       memcpy(ptr, buf, size);
-       
-       retval = 0;
-errout:
-       if (buf)
-               free(buf);
-       return (retval);
+       return 0;
 }