Whamcloud - gitweb
libext2fs: don't use O_DIRECT for files on tmpfs
[tools/e2fsprogs.git] / lib / ext2fs / imager.c
index c0c10f0..b3ede9a 100644 (file)
@@ -8,11 +8,12 @@
  * 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;
                        }
                }
@@ -134,9 +144,10 @@ errout:
 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;
 
@@ -145,7 +156,7 @@ errcode_t ext2fs_image_inode_read(ext2_filsys fs, int fd,
                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,11 +171,11 @@ 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;
 
@@ -188,6 +199,14 @@ 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
+
+       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,13 +240,39 @@ 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);
+       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, (ssize_t)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;
        }
-       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;
        }
@@ -239,7 +294,7 @@ errcode_t ext2fs_image_super_read(ext2_filsys fs, int fd,
        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;
 
@@ -278,10 +333,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,9 +346,9 @@ 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;
+               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) {
@@ -302,10 +357,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 +392,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 +409,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,9 +422,9 @@ 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;
+               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) {
@@ -379,9 +433,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;
        }