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 return EXT2_ET_MISSING_INODE_TABLE;
77 left = fs->inode_blocks_per_group;
82 retval = io_channel_read_blk(fs->io, blk, c, buf);
87 if (!(flags & IMAGER_FLAG_SPARSEWRITE)) {
91 /* Skip zero blocks */
92 if (check_zero_block(cp, fs->blocksize)) {
97 lseek(fd, fs->blocksize, SEEK_CUR);
100 /* Find non-zero blocks */
101 for (d=1; d < c; d++) {
102 if (check_zero_block(cp + d*fs->blocksize, fs->blocksize))
106 actual = write(fd, cp, fs->blocksize * d);
111 if (actual != (ssize_t) (fs->blocksize * d)) {
112 retval = EXT2_ET_SHORT_WRITE;
117 cp += fs->blocksize * d;
130 * Read in the inode table and stuff it into place
132 errcode_t ext2fs_image_inode_read(ext2_filsys fs, int fd,
133 int flags EXT2FS_ATTR((unused)))
135 unsigned int group, c, left;
141 buf = malloc(fs->blocksize * BUF_BLOCKS);
145 for (group = 0; group < fs->group_desc_count; group++) {
146 blk = fs->group_desc[(unsigned)group].bg_inode_table;
148 retval = EXT2_ET_MISSING_INODE_TABLE;
151 left = fs->inode_blocks_per_group;
156 actual = read(fd, buf, fs->blocksize * c);
161 if (actual != (ssize_t) (fs->blocksize * c)) {
162 retval = EXT2_ET_SHORT_READ;
165 retval = io_channel_write_blk(fs->io, blk, c, buf);
173 retval = ext2fs_flush_icache(fs);
181 * Write out superblock and group descriptors
183 errcode_t ext2fs_image_super_write(ext2_filsys fs, int fd,
184 int flags EXT2FS_ATTR((unused)))
190 buf = malloc(fs->blocksize);
195 * Write out the superblock
197 memset(buf, 0, fs->blocksize);
198 memcpy(buf, fs->super, SUPERBLOCK_SIZE);
199 actual = write(fd, buf, fs->blocksize);
204 if (actual != (ssize_t) fs->blocksize) {
205 retval = EXT2_ET_SHORT_WRITE;
210 * Now write out the block group descriptors
212 cp = (char *) fs->group_desc;
213 actual = write(fd, cp, fs->blocksize * fs->desc_blocks);
218 if (actual != (ssize_t) (fs->blocksize * fs->desc_blocks)) {
219 retval = EXT2_ET_SHORT_WRITE;
231 * Read the superblock and group descriptors and overwrite them.
233 errcode_t ext2fs_image_super_read(ext2_filsys fs, int fd,
234 int flags EXT2FS_ATTR((unused)))
237 ssize_t actual, size;
240 size = fs->blocksize * (fs->group_desc_count + 1);
248 actual = read(fd, buf, size);
253 if (actual != size) {
254 retval = EXT2_ET_SHORT_READ;
259 * Now copy in the superblock and group descriptors
261 memcpy(fs->super, buf, SUPERBLOCK_SIZE);
263 memcpy(fs->group_desc, buf + fs->blocksize,
264 fs->blocksize * fs->group_desc_count);
274 * Write the block/inode bitmaps.
276 errcode_t ext2fs_image_bitmap_write(ext2_filsys fs, int fd, int flags)
284 if (flags & IMAGER_FLAG_INODEMAP) {
285 if (!fs->inode_map) {
286 retval = ext2fs_read_inode_bitmap(fs);
290 ptr = fs->inode_map->bitmap;
291 size = (EXT2_INODES_PER_GROUP(fs->super) / 8);
293 if (!fs->block_map) {
294 retval = ext2fs_read_block_bitmap(fs);
298 ptr = fs->block_map->bitmap;
299 size = EXT2_BLOCKS_PER_GROUP(fs->super) / 8;
301 size = size * fs->group_desc_count;
303 actual = write(fd, ptr, size);
308 if (actual != size) {
309 retval = EXT2_ET_SHORT_WRITE;
312 size = size % fs->blocksize;
313 memset(zero_buf, 0, sizeof(zero_buf));
315 size = fs->blocksize - size;
318 if (c > (int) sizeof(zero_buf))
319 c = sizeof(zero_buf);
320 actual = write(fd, zero_buf, c);
326 retval = EXT2_ET_SHORT_WRITE;
339 * Read the block/inode bitmaps.
341 errcode_t ext2fs_image_bitmap_read(ext2_filsys fs, int fd, int flags)
348 if (flags & IMAGER_FLAG_INODEMAP) {
349 if (!fs->inode_map) {
350 retval = ext2fs_read_inode_bitmap(fs);
354 ptr = fs->inode_map->bitmap;
355 size = (EXT2_INODES_PER_GROUP(fs->super) / 8);
357 if (!fs->block_map) {
358 retval = ext2fs_read_block_bitmap(fs);
362 ptr = fs->block_map->bitmap;
363 size = EXT2_BLOCKS_PER_GROUP(fs->super) / 8;
365 size = size * fs->group_desc_count;
371 actual = read(fd, buf, size);
376 if (actual != size) {
377 retval = EXT2_ET_SHORT_WRITE;
380 memcpy(ptr, buf, size);