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 Library
12 * General Public License, version 2.
31 #include <sys/types.h>
37 #ifndef HAVE_TYPE_SSIZE_T
42 * This function returns 1 if the specified block is all zeros
44 static int check_zero_block(char *buf, int blocksize)
58 * Write the inode table out as a single block.
62 errcode_t ext2fs_image_inode_write(ext2_filsys fs, int fd, int flags)
64 unsigned int group, left, c, d;
71 buf = malloc(fs->blocksize * BUF_BLOCKS);
75 for (group = 0; group < fs->group_desc_count; group++) {
76 blk = ext2fs_inode_table_loc(fs, (unsigned)group);
78 retval = EXT2_ET_MISSING_INODE_TABLE;
81 left = fs->inode_blocks_per_group;
86 retval = io_channel_read_blk64(fs->io, blk, c, buf);
91 if (!(flags & IMAGER_FLAG_SPARSEWRITE)) {
95 /* Skip zero blocks */
96 if (check_zero_block(cp, fs->blocksize)) {
101 r = ext2fs_llseek(fd, fs->blocksize,
109 /* Find non-zero blocks */
110 for (d=1; d < c; d++) {
111 if (check_zero_block(cp + d*fs->blocksize, fs->blocksize))
115 actual = write(fd, cp, fs->blocksize * d);
120 if (actual != (ssize_t) (fs->blocksize * d)) {
121 retval = EXT2_ET_SHORT_WRITE;
126 cp += fs->blocksize * d;
139 * Read in the inode table and stuff it into place
141 errcode_t ext2fs_image_inode_read(ext2_filsys fs, int fd,
142 int flags EXT2FS_ATTR((unused)))
144 unsigned int group, c, left;
150 buf = malloc(fs->blocksize * BUF_BLOCKS);
154 for (group = 0; group < fs->group_desc_count; group++) {
155 blk = ext2fs_inode_table_loc(fs, (unsigned)group);
157 retval = EXT2_ET_MISSING_INODE_TABLE;
160 left = fs->inode_blocks_per_group;
165 actual = read(fd, buf, fs->blocksize * c);
170 if (actual != (ssize_t) (fs->blocksize * c)) {
171 retval = EXT2_ET_SHORT_READ;
174 retval = io_channel_write_blk64(fs->io, blk, c, buf);
182 retval = ext2fs_flush_icache(fs);
190 * Write out superblock and group descriptors
192 errcode_t ext2fs_image_super_write(ext2_filsys fs, int fd,
193 int flags EXT2FS_ATTR((unused)))
199 buf = malloc(fs->blocksize);
204 * Write out the superblock
206 memset(buf, 0, fs->blocksize);
207 memcpy(buf, fs->super, SUPERBLOCK_SIZE);
208 actual = write(fd, buf, fs->blocksize);
213 if (actual != (ssize_t) fs->blocksize) {
214 retval = EXT2_ET_SHORT_WRITE;
219 * Now write out the block group descriptors
221 cp = (char *) fs->group_desc;
222 actual = write(fd, cp, fs->blocksize * fs->desc_blocks);
227 if (actual != (ssize_t) (fs->blocksize * fs->desc_blocks)) {
228 retval = EXT2_ET_SHORT_WRITE;
240 * Read the superblock and group descriptors and overwrite them.
242 errcode_t ext2fs_image_super_read(ext2_filsys fs, int fd,
243 int flags EXT2FS_ATTR((unused)))
246 ssize_t actual, size;
249 size = fs->blocksize * (fs->group_desc_count + 1);
257 actual = read(fd, buf, size);
262 if (actual != size) {
263 retval = EXT2_ET_SHORT_READ;
268 * Now copy in the superblock and group descriptors
270 memcpy(fs->super, buf, SUPERBLOCK_SIZE);
272 memcpy(fs->group_desc, buf + fs->blocksize,
273 fs->blocksize * fs->group_desc_count);
283 * Write the block/inode bitmaps.
285 errcode_t ext2fs_image_bitmap_write(ext2_filsys fs, int fd, int flags)
287 ext2fs_generic_bitmap bmap;
291 __u64 itr, cnt, size, total_size;
294 if (flags & IMAGER_FLAG_INODEMAP) {
295 if (!fs->inode_map) {
296 retval = ext2fs_read_inode_bitmap(fs);
300 bmap = fs->inode_map;
302 cnt = EXT2_INODES_PER_GROUP(fs->super) * fs->group_desc_count;
303 size = (EXT2_INODES_PER_GROUP(fs->super) / 8);
305 if (!fs->block_map) {
306 retval = ext2fs_read_block_bitmap(fs);
310 bmap = fs->block_map;
311 itr = fs->super->s_first_data_block;
312 cnt = EXT2_GROUPS_TO_BLOCKS(fs->super, fs->group_desc_count);
313 size = EXT2_BLOCKS_PER_GROUP(fs->super) / 8;
315 total_size = size * fs->group_desc_count;
319 if (size > (cnt >> 3))
322 retval = ext2fs_get_generic_bmap_range(bmap, itr,
327 actual = write(fd, buf, size);
330 if (actual != (int) size)
331 return EXT2_ET_SHORT_READ;
337 size = total_size % fs->blocksize;
338 memset(buf, 0, sizeof(buf));
340 size = fs->blocksize - size;
343 if (c > (int) sizeof(buf))
345 actual = write(fd, buf, c);
348 if ((size_t) actual != c)
349 return EXT2_ET_SHORT_WRITE;
358 * Read the block/inode bitmaps.
360 errcode_t ext2fs_image_bitmap_read(ext2_filsys fs, int fd, int flags)
362 ext2fs_generic_bitmap bmap;
369 if (flags & IMAGER_FLAG_INODEMAP) {
370 if (!fs->inode_map) {
371 retval = ext2fs_read_inode_bitmap(fs);
375 bmap = fs->inode_map;
377 cnt = EXT2_INODES_PER_GROUP(fs->super) * fs->group_desc_count;
378 size = (EXT2_INODES_PER_GROUP(fs->super) / 8);
380 if (!fs->block_map) {
381 retval = ext2fs_read_block_bitmap(fs);
385 bmap = fs->block_map;
386 itr = fs->super->s_first_data_block;
387 cnt = EXT2_GROUPS_TO_BLOCKS(fs->super, fs->group_desc_count);
388 size = EXT2_BLOCKS_PER_GROUP(fs->super) / 8;
393 if (size > (cnt >> 3))
396 actual = read(fd, buf, size);
399 if (actual != (int) size)
400 return EXT2_ET_SHORT_READ;
402 retval = ext2fs_set_generic_bmap_range(bmap, itr,