2 * image.c --- writes out the critical parts of the filesystem as a
5 * Copyright (C) 2000 Theodore Ts'o.
7 * Note: this uses the POSIX IO interfaces, unlike most of the other
8 * functions in this library. So sue me.
11 * This file may be redistributed under the terms of the GNU Public
30 #include <sys/types.h>
36 #ifndef HAVE_TYPE_SSIZE_T
41 * This function returns 1 if the specified block is all zeros
43 static int check_zero_block(char *buf, int blocksize)
57 * Write the inode table out as a single block.
61 errcode_t ext2fs_image_inode_write(ext2_filsys fs, int fd, int flags)
63 unsigned int group, left, c, d;
69 buf = malloc(fs->blocksize * BUF_BLOCKS);
73 for (group = 0; group < fs->group_desc_count; group++) {
74 blk = fs->group_desc[(unsigned)group].bg_inode_table;
76 retval = EXT2_ET_MISSING_INODE_TABLE;
79 left = fs->inode_blocks_per_group;
84 retval = io_channel_read_blk(fs->io, blk, c, buf);
89 if (!(flags & IMAGER_FLAG_SPARSEWRITE)) {
93 /* Skip zero blocks */
94 if (check_zero_block(cp, fs->blocksize)) {
99 lseek(fd, fs->blocksize, SEEK_CUR);
102 /* Find non-zero blocks */
103 for (d=1; d < c; d++) {
104 if (check_zero_block(cp + d*fs->blocksize, fs->blocksize))
108 actual = write(fd, cp, fs->blocksize * d);
113 if (actual != (ssize_t) (fs->blocksize * d)) {
114 retval = EXT2_ET_SHORT_WRITE;
119 cp += fs->blocksize * d;
132 * Read in the inode table and stuff it into place
134 errcode_t ext2fs_image_inode_read(ext2_filsys fs, int fd,
135 int flags EXT2FS_ATTR((unused)))
137 unsigned int group, c, left;
143 buf = malloc(fs->blocksize * BUF_BLOCKS);
147 for (group = 0; group < fs->group_desc_count; group++) {
148 blk = fs->group_desc[(unsigned)group].bg_inode_table;
150 retval = EXT2_ET_MISSING_INODE_TABLE;
153 left = fs->inode_blocks_per_group;
158 actual = read(fd, buf, fs->blocksize * c);
163 if (actual != (ssize_t) (fs->blocksize * c)) {
164 retval = EXT2_ET_SHORT_READ;
167 retval = io_channel_write_blk(fs->io, blk, c, buf);
175 retval = ext2fs_flush_icache(fs);
183 * Write out superblock and group descriptors
185 errcode_t ext2fs_image_super_write(ext2_filsys fs, int fd,
186 int flags EXT2FS_ATTR((unused)))
192 buf = malloc(fs->blocksize);
197 * Write out the superblock
199 memset(buf, 0, fs->blocksize);
200 memcpy(buf, fs->super, SUPERBLOCK_SIZE);
201 actual = write(fd, buf, fs->blocksize);
206 if (actual != (ssize_t) fs->blocksize) {
207 retval = EXT2_ET_SHORT_WRITE;
212 * Now write out the block group descriptors
214 cp = (char *) fs->group_desc;
215 actual = write(fd, cp, fs->blocksize * fs->desc_blocks);
220 if (actual != (ssize_t) (fs->blocksize * fs->desc_blocks)) {
221 retval = EXT2_ET_SHORT_WRITE;
233 * Read the superblock and group descriptors and overwrite them.
235 errcode_t ext2fs_image_super_read(ext2_filsys fs, int fd,
236 int flags EXT2FS_ATTR((unused)))
239 ssize_t actual, size;
242 size = fs->blocksize * (fs->group_desc_count + 1);
250 actual = read(fd, buf, size);
255 if (actual != size) {
256 retval = EXT2_ET_SHORT_READ;
261 * Now copy in the superblock and group descriptors
263 memcpy(fs->super, buf, SUPERBLOCK_SIZE);
265 memcpy(fs->group_desc, buf + fs->blocksize,
266 fs->blocksize * fs->group_desc_count);
276 * Write the block/inode bitmaps.
278 errcode_t ext2fs_image_bitmap_write(ext2_filsys fs, int fd, int flags)
286 if (flags & IMAGER_FLAG_INODEMAP) {
287 if (!fs->inode_map) {
288 retval = ext2fs_read_inode_bitmap(fs);
292 ptr = fs->inode_map->bitmap;
293 size = (EXT2_INODES_PER_GROUP(fs->super) / 8);
295 if (!fs->block_map) {
296 retval = ext2fs_read_block_bitmap(fs);
300 ptr = fs->block_map->bitmap;
301 size = EXT2_BLOCKS_PER_GROUP(fs->super) / 8;
303 size = size * fs->group_desc_count;
305 actual = write(fd, ptr, size);
310 if (actual != size) {
311 retval = EXT2_ET_SHORT_WRITE;
314 size = size % fs->blocksize;
315 memset(zero_buf, 0, sizeof(zero_buf));
317 size = fs->blocksize - size;
320 if (c > (int) sizeof(zero_buf))
321 c = sizeof(zero_buf);
322 actual = write(fd, zero_buf, c);
328 retval = EXT2_ET_SHORT_WRITE;
341 * Read the block/inode bitmaps.
343 errcode_t ext2fs_image_bitmap_read(ext2_filsys fs, int fd, int flags)
350 if (flags & IMAGER_FLAG_INODEMAP) {
351 if (!fs->inode_map) {
352 retval = ext2fs_read_inode_bitmap(fs);
356 ptr = fs->inode_map->bitmap;
357 size = (EXT2_INODES_PER_GROUP(fs->super) / 8);
359 if (!fs->block_map) {
360 retval = ext2fs_read_block_bitmap(fs);
364 ptr = fs->block_map->bitmap;
365 size = EXT2_BLOCKS_PER_GROUP(fs->super) / 8;
367 size = size * fs->group_desc_count;
373 actual = read(fd, buf, size);
378 if (actual != size) {
379 retval = EXT2_ET_SHORT_WRITE;
382 memcpy(ptr, buf, size);