2 * e2image.c --- Program which writes an image file backing up
3 * critical metadata for the filesystem.
5 * Copyright 2000, 2001 by Theodore Ts'o.
8 * This file may be redistributed under the terms of the GNU Public
13 #ifndef _LARGEFILE_SOURCE
14 #define _LARGEFILE_SOURCE
16 #ifndef _LARGEFILE64_SOURCE
17 #define _LARGEFILE64_SOURCE
40 #include <sys/types.h>
44 #include "ext2fs/ext2_fs.h"
45 #include "ext2fs/ext2fs.h"
46 #include "ext2fs/ext2fsP.h"
47 #include "et/com_err.h"
48 #include "uuid/uuid.h"
50 #include "ext2fs/e2image.h"
51 #include "ext2fs/qcow2.h"
53 #include "support/nls-enable.h"
54 #include "support/plausible.h"
55 #include "../version.h"
57 #define QCOW_OFLAG_COPIED (1ULL << 63)
58 #define NO_BLK ((blk64_t) -1)
62 #define E2IMAGE_QCOW2 2
65 #define E2IMAGE_INSTALL_FLAG 1
66 #define E2IMAGE_SCRAMBLE_FLAG 2
67 #define E2IMAGE_IS_QCOW2_FLAG 4
68 #define E2IMAGE_CHECK_ZERO_FLAG 8
70 static const char * program_name = "e2image";
71 static char * device_name = NULL;
73 static char output_is_blk;
75 /* writing to blk device: don't skip zeroed blocks */
76 static blk64_t source_offset, dest_offset;
77 static char move_mode;
78 static char show_progress;
79 static char *check_buf;
80 static int skipped_blocks;
82 static blk64_t align_offset(blk64_t offset, unsigned int n)
84 return (offset + n - 1) & ~((blk64_t) n - 1);
87 static int get_bits_from_size(size_t size)
95 /* Not a power of two */
105 static void usage(void)
107 fprintf(stderr, _("Usage: %s [ -r|-Q ] [ -f ] [ -b superblock ] [ -B blocksize ] "
108 "device image-file\n"),
110 fprintf(stderr, _(" %s -I device image-file\n"), program_name);
111 fprintf(stderr, _(" %s -ra [ -cfnp ] [ -o src_offset ] "
112 "[ -O dest_offset ] src_fs [ dest_fs ]\n"),
117 static ext2_loff_t seek_relative(int fd, int offset)
119 ext2_loff_t ret = ext2fs_llseek(fd, offset, SEEK_CUR);
121 perror("seek_relative");
127 static ext2_loff_t seek_set(int fd, ext2_loff_t offset)
129 ext2_loff_t ret = ext2fs_llseek(fd, offset, SEEK_SET);
138 * Returns true if the block we are about to write is identical to
139 * what is already on the disk.
141 static int check_block(int fd, void *buf, void *cbuf, int blocksize)
144 int count = blocksize, ret;
150 ret = read(fd, cp, count);
152 perror("check_block");
158 ret = memcmp(buf, cbuf, blocksize);
159 seek_relative(fd, -blocksize);
160 return (ret == 0) ? 1 : 0;
163 static void generic_write(int fd, void *buf, int blocksize, blk64_t block)
165 int count, free_buf = 0;
173 err = ext2fs_get_arrayzero(1, blocksize, &buf);
175 com_err(program_name, err, "%s",
176 _("while allocating buffer"));
181 printf(_("Writing block %llu\n"), (unsigned long long) block);
183 seek_relative(fd, blocksize);
184 goto free_and_return;
186 count = write(fd, buf, blocksize);
187 if (count != blocksize) {
194 com_err(program_name, err,
195 _("error writing block %llu"),
196 (unsigned long long) block);
198 com_err(program_name, err, "%s",
199 _("error in generic_write()"));
205 ext2fs_free_mem(&buf);
208 static void write_header(int fd, void *hdr, int hdr_size, int wrt_size)
214 if (hdr_size > wrt_size) {
215 fprintf(stderr, "%s",
216 _("Error: header size is bigger than wrt_size\n"));
219 ret = ext2fs_get_mem(wrt_size, &header_buf);
221 fputs(_("Couldn't allocate header buffer\n"), stderr);
226 memset(header_buf, 0, wrt_size);
229 memcpy(header_buf, hdr, hdr_size);
231 generic_write(fd, header_buf, wrt_size, NO_BLK);
233 ext2fs_free_mem(&header_buf);
236 static void write_image_file(ext2_filsys fs, int fd)
238 struct ext2_image_hdr hdr;
242 write_header(fd, NULL, sizeof(struct ext2_image_hdr), fs->blocksize);
243 memset(&hdr, 0, sizeof(struct ext2_image_hdr));
245 hdr.offset_super = ext2fs_cpu_to_le32(seek_relative(fd, 0));
246 retval = ext2fs_image_super_write(fs, fd, 0);
248 com_err(program_name, retval, "%s",
249 _("while writing superblock"));
253 hdr.offset_inode = ext2fs_cpu_to_le32(seek_relative(fd, 0));
254 retval = ext2fs_image_inode_write(fs, fd,
255 (fd != 1) ? IMAGER_FLAG_SPARSEWRITE : 0);
257 com_err(program_name, retval, "%s",
258 _("while writing inode table"));
262 hdr.offset_blockmap = ext2fs_cpu_to_le32(seek_relative(fd, 0));
263 retval = ext2fs_image_bitmap_write(fs, fd, 0);
265 com_err(program_name, retval, "%s",
266 _("while writing block bitmap"));
270 hdr.offset_inodemap = ext2fs_cpu_to_le32(seek_relative(fd, 0));
271 retval = ext2fs_image_bitmap_write(fs, fd, IMAGER_FLAG_INODEMAP);
273 com_err(program_name, retval, "%s",
274 _("while writing inode bitmap"));
278 hdr.magic_number = ext2fs_cpu_to_le32(EXT2_ET_MAGIC_E2IMAGE);
279 strcpy(hdr.magic_descriptor, "Ext2 Image 1.0");
280 gethostname(hdr.fs_hostname, sizeof(hdr.fs_hostname));
281 strncpy(hdr.fs_device_name, device_name, sizeof(hdr.fs_device_name)-1);
282 hdr.fs_device_name[sizeof(hdr.fs_device_name) - 1] = 0;
283 hdr.fs_blocksize = ext2fs_cpu_to_le32(fs->blocksize);
285 if (stat(device_name, &st) == 0)
286 hdr.fs_device = ext2fs_cpu_to_le32(st.st_rdev);
288 if (fstat(fd, &st) == 0) {
289 hdr.image_device = ext2fs_cpu_to_le32(st.st_dev);
290 hdr.image_inode = ext2fs_cpu_to_le32(st.st_ino);
292 memcpy(hdr.fs_uuid, fs->super->s_uuid, sizeof(hdr.fs_uuid));
294 hdr.image_time = ext2fs_cpu_to_le32(time(0));
295 write_header(fd, &hdr, sizeof(struct ext2_image_hdr), fs->blocksize);
299 * These set of functions are used to write a RAW image file.
301 static ext2fs_block_bitmap meta_block_map;
302 static ext2fs_block_bitmap scramble_block_map; /* Directory blocks to be scrambled */
303 static blk64_t meta_blocks_count;
305 struct process_block_struct {
311 * These subroutines short circuits ext2fs_get_blocks and
312 * ext2fs_check_directory; we use them since we already have the inode
313 * structure, so there's no point in letting the ext2fs library read
316 static ext2_ino_t stashed_ino = 0;
317 static struct ext2_inode *stashed_inode;
319 static errcode_t meta_get_blocks(ext2_filsys fs EXT2FS_ATTR((unused)),
325 if ((ino != stashed_ino) || !stashed_inode)
326 return EXT2_ET_CALLBACK_NOTHANDLED;
328 for (i=0; i < EXT2_N_BLOCKS; i++)
329 blocks[i] = stashed_inode->i_block[i];
333 static errcode_t meta_check_directory(ext2_filsys fs EXT2FS_ATTR((unused)),
336 if ((ino != stashed_ino) || !stashed_inode)
337 return EXT2_ET_CALLBACK_NOTHANDLED;
339 if (!LINUX_S_ISDIR(stashed_inode->i_mode))
340 return EXT2_ET_NO_DIRECTORY;
344 static errcode_t meta_read_inode(ext2_filsys fs EXT2FS_ATTR((unused)),
346 struct ext2_inode *inode)
348 if ((ino != stashed_ino) || !stashed_inode)
349 return EXT2_ET_CALLBACK_NOTHANDLED;
350 *inode = *stashed_inode;
354 static void use_inode_shortcuts(ext2_filsys fs, int use_shortcuts)
357 fs->get_blocks = meta_get_blocks;
358 fs->check_directory = meta_check_directory;
359 fs->read_inode = meta_read_inode;
363 fs->check_directory = 0;
368 static int process_dir_block(ext2_filsys fs EXT2FS_ATTR((unused)),
370 e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
371 blk64_t ref_block EXT2FS_ATTR((unused)),
372 int ref_offset EXT2FS_ATTR((unused)),
373 void *priv_data EXT2FS_ATTR((unused)))
375 struct process_block_struct *p;
377 p = (struct process_block_struct *) priv_data;
379 ext2fs_mark_block_bitmap2(meta_block_map, *block_nr);
381 if (scramble_block_map && p->is_dir && blockcnt >= 0)
382 ext2fs_mark_block_bitmap2(scramble_block_map, *block_nr);
386 static int process_file_block(ext2_filsys fs EXT2FS_ATTR((unused)),
388 e2_blkcnt_t blockcnt,
389 blk64_t ref_block EXT2FS_ATTR((unused)),
390 int ref_offset EXT2FS_ATTR((unused)),
391 void *priv_data EXT2FS_ATTR((unused)))
393 if (blockcnt < 0 || all_data) {
394 ext2fs_mark_block_bitmap2(meta_block_map, *block_nr);
400 static void mark_table_blocks(ext2_filsys fs)
402 blk64_t first_block, b;
405 first_block = fs->super->s_first_data_block;
407 * Mark primary superblock
409 ext2fs_mark_block_bitmap2(meta_block_map, first_block);
413 * Mark the primary superblock descriptors
415 for (j = 0; j < fs->desc_blocks; j++) {
416 ext2fs_mark_block_bitmap2(meta_block_map,
417 ext2fs_descriptor_block_loc2(fs, first_block, j));
419 meta_blocks_count += fs->desc_blocks;
424 if (fs->super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_MMP) {
425 ext2fs_mark_block_bitmap2(meta_block_map, fs->super->s_mmp_block);
429 for (i = 0; i < fs->group_desc_count; i++) {
431 * Mark the blocks used for the inode table
433 if ((output_is_blk ||
434 !ext2fs_bg_flags_test(fs, i, EXT2_BG_INODE_UNINIT)) &&
435 ext2fs_inode_table_loc(fs, i)) {
436 unsigned int end = (unsigned) fs->inode_blocks_per_group;
437 /* skip unused blocks */
438 if (!output_is_blk && ext2fs_has_group_desc_csum(fs))
439 end -= (ext2fs_bg_itable_unused(fs, i) /
440 EXT2_INODES_PER_BLOCK(fs->super));
441 for (j = 0, b = ext2fs_inode_table_loc(fs, i);
444 ext2fs_mark_block_bitmap2(meta_block_map, b);
450 * Mark block used for the block bitmap
452 if (!ext2fs_bg_flags_test(fs, i, EXT2_BG_BLOCK_UNINIT) &&
453 ext2fs_block_bitmap_loc(fs, i)) {
454 ext2fs_mark_block_bitmap2(meta_block_map,
455 ext2fs_block_bitmap_loc(fs, i));
460 * Mark block used for the inode bitmap
462 if (!ext2fs_bg_flags_test(fs, i, EXT2_BG_INODE_UNINIT) &&
463 ext2fs_inode_bitmap_loc(fs, i)) {
464 ext2fs_mark_block_bitmap2(meta_block_map,
465 ext2fs_inode_bitmap_loc(fs, i));
472 * This function returns 1 if the specified block is all zeros
474 static int check_zero_block(char *buf, int blocksize)
477 int left = blocksize;
489 static int name_id[256];
491 #define EXT4_MAX_REC_LEN ((1<<16)-1)
493 static void scramble_dir_block(ext2_filsys fs, blk64_t blk, char *buf)
496 struct ext2_dir_entry_2 *dirent;
497 unsigned int rec_len;
500 end = buf + fs->blocksize;
501 for (p = buf; p < end-8; p += rec_len) {
502 dirent = (struct ext2_dir_entry_2 *) p;
503 rec_len = dirent->rec_len;
504 #ifdef WORDS_BIGENDIAN
505 rec_len = ext2fs_swab16(rec_len);
507 if (rec_len == EXT4_MAX_REC_LEN || rec_len == 0)
508 rec_len = fs->blocksize;
510 rec_len = (rec_len & 65532) | ((rec_len & 3) << 16);
512 printf("rec_len = %d, name_len = %d\n", rec_len, dirent->name_len);
514 if (rec_len < 8 || (rec_len % 4) ||
516 printf(_("Corrupt directory block %llu: "
517 "bad rec_len (%d)\n"),
518 (unsigned long long) blk, rec_len);
520 (void) ext2fs_set_rec_len(fs, rec_len,
521 (struct ext2_dir_entry *) dirent);
522 #ifdef WORDS_BIGENDIAN
523 dirent->rec_len = ext2fs_swab16(dirent->rec_len);
527 if (dirent->name_len + 8U > rec_len) {
528 printf(_("Corrupt directory block %llu: "
529 "bad name_len (%d)\n"),
530 (unsigned long long) blk, dirent->name_len);
531 dirent->name_len = rec_len - 8;
535 len = rec_len - dirent->name_len - 8;
537 memset(cp+dirent->name_len, 0, len);
538 if (dirent->name_len==1 && cp[0] == '.')
540 if (dirent->name_len==2 && cp[0] == '.' && cp[1] == '.')
543 memset(cp, 'A', dirent->name_len);
544 len = dirent->name_len;
546 while ((len > 0) && (id > 0)) {
555 static char got_sigint;
557 static void sigint_handler(int unused EXT2FS_ATTR((unused)))
560 signal (SIGINT, SIG_DFL);
563 #define calc_percent(a, b) ((int) ((100.0 * (((float) (a)) / \
564 ((float) (b)))) + 0.5))
565 #define calc_rate(t, b, d) (((float)(t) / ((float)(1024 * 1024) / (b))) / (d))
567 static int print_progress(blk64_t num, blk64_t total)
569 return fprintf(stderr, _("%llu / %llu blocks (%d%%)"),
570 (unsigned long long) num,
571 (unsigned long long) total,
572 calc_percent(num, total));
575 static void output_meta_data_blocks(ext2_filsys fs, int fd, int flags)
579 char *buf, *zero_buf;
582 blk64_t distance = 0;
583 blk64_t end = ext2fs_blocks_count(fs->super);
584 time_t last_update = 0;
585 time_t start_time = 0;
586 blk64_t total_written = 0;
589 retval = ext2fs_get_mem(fs->blocksize, &buf);
591 com_err(program_name, retval, "%s",
592 _("while allocating buffer"));
595 retval = ext2fs_get_memzero(fs->blocksize, &zero_buf);
597 com_err(program_name, retval, "%s",
598 _("while allocating buffer"));
602 fprintf(stderr, "%s", _("Copying "));
603 bscount = print_progress(total_written, meta_blocks_count);
605 last_update = time(NULL);
606 start_time = time(NULL);
608 /* when doing an in place move to the right, you can't start
609 at the beginning or you will overwrite data, so instead
610 divide the fs up into distance size chunks and write them
612 if (move_mode && dest_offset > source_offset) {
613 distance = (dest_offset - source_offset) / fs->blocksize;
614 if (distance < ext2fs_blocks_count(fs->super))
615 start = ext2fs_blocks_count(fs->super) - distance;
618 signal (SIGINT, sigint_handler);
621 seek_set(fd, (start * fs->blocksize) + dest_offset);
622 for (blk = start; blk < end; blk++) {
625 /* moving to the right */
626 if (distance >= ext2fs_blocks_count(fs->super)||
627 start == ext2fs_blocks_count(fs->super) -
629 kill(getpid(), SIGINT);
631 /* moving to the left */
632 if (blk < (source_offset - dest_offset) /
634 kill(getpid(), SIGINT);
638 fprintf(stderr, "%s",
639 _("Stopping now will destroy the filesystem, "
640 "interrupt again if you are sure\n"));
642 fprintf(stderr, "%s", _("Copying "));
643 bscount = print_progress(total_written,
650 if (show_progress && last_update != time(NULL)) {
652 last_update = time(NULL);
655 bscount = print_progress(total_written,
657 duration = time(NULL) - start_time;
658 if (duration > 5 && total_written) {
659 time_t est = (duration * meta_blocks_count /
660 total_written) - duration;
662 strftime(buff, 30, "%T", gmtime(&est));
665 _(" %s remaining at %.2f MB/s"),
666 buff, calc_rate(total_written,
672 if ((blk >= fs->super->s_first_data_block) &&
673 ext2fs_test_block_bitmap2(meta_block_map, blk)) {
674 retval = io_channel_read_blk64(fs->io, blk, 1, buf);
676 com_err(program_name, retval,
677 _("error reading block %llu"),
678 (unsigned long long) blk);
681 if (scramble_block_map &&
682 ext2fs_test_block_bitmap2(scramble_block_map, blk))
683 scramble_dir_block(fs, blk, buf);
684 if ((flags & E2IMAGE_CHECK_ZERO_FLAG) &&
685 check_zero_block(buf, fs->blocksize))
688 seek_relative(fd, sparse);
690 if (check_block(fd, buf, check_buf, fs->blocksize)) {
691 seek_relative(fd, fs->blocksize);
694 generic_write(fd, buf, fs->blocksize, blk);
699 generic_write(fd, zero_buf,
703 sparse += fs->blocksize;
704 if (sparse > 1024*1024) {
705 seek_relative(fd, 1024*1024);
710 if (distance && start) {
711 if (start < distance) {
717 if (end < distance) {
718 /* past overlap, do rest in one go */
726 signal (SIGINT, SIG_DFL);
728 time_t duration = time(NULL) - start_time;
731 strftime(buff, 30, "%T", gmtime(&duration));
732 fprintf(stderr, _("Copied %llu / %llu blocks (%d%%) in %s "),
733 (unsigned long long) total_written,
734 (unsigned long long) meta_blocks_count,
735 calc_percent(total_written, meta_blocks_count), buff);
737 fprintf(stderr, _("at %.2f MB/s"),
738 calc_rate(total_written, fs->blocksize, duration));
739 fputs(" \n", stderr);
741 #ifdef HAVE_FTRUNCATE64
745 offset = seek_set(fd,
746 fs->blocksize * ext2fs_blocks_count(fs->super) + dest_offset);
748 offset = seek_relative(fd, sparse);
750 if (ftruncate64(fd, offset) < 0) {
751 seek_relative(fd, -1);
752 generic_write(fd, zero_buf, 1, NO_BLK);
756 if (sparse && !distance) {
757 seek_relative(fd, sparse-1);
758 generic_write(fd, zero_buf, 1, NO_BLK);
761 ext2fs_free_mem(&zero_buf);
762 ext2fs_free_mem(&buf);
765 static void init_l1_table(struct ext2_qcow2_image *image)
770 ret = ext2fs_get_arrayzero(image->l1_size, sizeof(__u64), &l1_table);
772 com_err(program_name, ret, "%s",
773 _("while allocating l1 table"));
777 image->l1_table = l1_table;
780 static void init_l2_cache(struct ext2_qcow2_image *image)
782 unsigned int count, i;
783 struct ext2_qcow2_l2_cache *cache;
784 struct ext2_qcow2_l2_table *table;
787 ret = ext2fs_get_arrayzero(1, sizeof(struct ext2_qcow2_l2_cache),
792 count = (image->l1_size > L2_CACHE_PREALLOC) ? L2_CACHE_PREALLOC :
795 cache->count = count;
797 cache->next_offset = image->l2_offset;
799 for (i = 0; i < count; i++) {
800 ret = ext2fs_get_arrayzero(1,
801 sizeof(struct ext2_qcow2_l2_table), &table);
805 ret = ext2fs_get_arrayzero(image->l2_size,
806 sizeof(__u64), &table->data);
810 table->next = cache->free_head;
811 cache->free_head = table;
814 image->l2_cache = cache;
818 com_err(program_name, ret, "%s", _("while allocating l2 cache"));
822 static void put_l2_cache(struct ext2_qcow2_image *image)
824 struct ext2_qcow2_l2_cache *cache = image->l2_cache;
825 struct ext2_qcow2_l2_table *tmp, *table;
830 table = cache->free_head;
831 cache->free_head = NULL;
836 ext2fs_free_mem(&tmp->data);
837 ext2fs_free_mem(&tmp);
840 if (cache->free != cache->count) {
841 fprintf(stderr, "%s", _("Warning: There are still tables in "
842 "the cache while putting the cache, "
843 "data will be lost so the image may "
845 table = cache->used_head;
846 cache->used_head = NULL;
850 ext2fs_free_mem(&cache);
853 static int init_refcount(struct ext2_qcow2_image *img, blk64_t table_offset)
855 struct ext2_qcow2_refcount *ref;
856 blk64_t table_clusters;
859 ref = &(img->refcount);
862 * One refcount block addresses 2048 clusters, one refcount table
863 * addresses cluster/sizeof(__u64) refcount blocks, and we need
864 * to address meta_blocks_count clusters + qcow2 metadata clusters
867 table_clusters = meta_blocks_count + (table_offset >>
869 table_clusters >>= (img->cluster_bits + 6 - 1);
870 table_clusters = (table_clusters == 0) ? 1 : table_clusters;
872 ref->refcount_table_offset = table_offset;
873 ref->refcount_table_clusters = table_clusters;
874 ref->refcount_table_index = 0;
875 ref->refcount_block_index = 0;
877 /* Allocate refcount table */
878 ret = ext2fs_get_arrayzero(ref->refcount_table_clusters,
879 img->cluster_size, &ref->refcount_table);
883 /* Allocate refcount block */
884 ret = ext2fs_get_arrayzero(1, img->cluster_size, &ref->refcount_block);
886 ext2fs_free_mem(&ref->refcount_table);
891 static errcode_t initialize_qcow2_image(int fd, ext2_filsys fs,
892 struct ext2_qcow2_image *image)
894 struct ext2_qcow2_hdr *header;
895 blk64_t total_size, offset;
896 int shift, l2_bits, header_size, l1_size, ret;
897 int cluster_bits = get_bits_from_size(fs->blocksize);
898 struct ext2_super_block *sb = fs->super;
900 if (fs->blocksize < 1024)
901 return EINVAL; /* Can never happen, but just in case... */
903 /* Allocate header */
904 ret = ext2fs_get_memzero(sizeof(struct ext2_qcow2_hdr), &header);
908 total_size = ext2fs_blocks_count(sb) << cluster_bits;
909 image->cluster_size = fs->blocksize;
910 image->l2_size = 1 << (cluster_bits - 3);
911 image->cluster_bits = cluster_bits;
914 header->magic = ext2fs_cpu_to_be32(QCOW_MAGIC);
915 header->version = ext2fs_cpu_to_be32(QCOW_VERSION);
916 header->size = ext2fs_cpu_to_be64(total_size);
917 header->cluster_bits = ext2fs_cpu_to_be32(cluster_bits);
919 header_size = (sizeof(struct ext2_qcow2_hdr) + 7) & ~7;
920 offset = align_offset(header_size, image->cluster_size);
922 header->l1_table_offset = ext2fs_cpu_to_be64(offset);
923 image->l1_offset = offset;
925 l2_bits = cluster_bits - 3;
926 shift = cluster_bits + l2_bits;
927 l1_size = ((total_size + (1LL << shift) - 1) >> shift);
928 header->l1_size = ext2fs_cpu_to_be32(l1_size);
929 image->l1_size = l1_size;
931 /* Make space for L1 table */
932 offset += align_offset(l1_size * sizeof(blk64_t), image->cluster_size);
934 /* Initialize refcounting */
935 ret = init_refcount(image, offset);
937 ext2fs_free_mem(&header);
940 header->refcount_table_offset = ext2fs_cpu_to_be64(offset);
941 header->refcount_table_clusters =
942 ext2fs_cpu_to_be32(image->refcount.refcount_table_clusters);
943 offset += image->cluster_size;
944 offset += image->refcount.refcount_table_clusters <<
947 /* Make space for L2 tables */
948 image->l2_offset = offset;
949 offset += image->cluster_size;
951 /* Make space for first refcount block */
952 image->refcount.refcount_block_offset = offset;
955 /* Initialize l1 and l2 tables */
956 init_l1_table(image);
957 init_l2_cache(image);
962 static void free_qcow2_image(struct ext2_qcow2_image *img)
968 ext2fs_free_mem(&img->hdr);
971 ext2fs_free_mem(&img->l1_table);
973 if (img->refcount.refcount_table)
974 ext2fs_free_mem(&img->refcount.refcount_table);
975 if (img->refcount.refcount_block)
976 ext2fs_free_mem(&img->refcount.refcount_block);
980 ext2fs_free_mem(&img);
984 * Put table from used list (used_head) into free list (free_head).
985 * l2_table is used to return pointer to the next used table (used_head).
987 static void put_used_table(struct ext2_qcow2_image *img,
988 struct ext2_qcow2_l2_table **l2_table)
990 struct ext2_qcow2_l2_cache *cache = img->l2_cache;
991 struct ext2_qcow2_l2_table *table;
993 table = cache->used_head;
994 cache->used_head = table->next;
998 cache->used_tail = NULL;
1000 /* Clean the table for case we will need to use it again */
1001 memset(table->data, 0, img->cluster_size);
1002 table->next = cache->free_head;
1003 cache->free_head = table;
1007 *l2_table = cache->used_head;
1010 static void flush_l2_cache(struct ext2_qcow2_image *image)
1014 struct ext2_qcow2_l2_cache *cache = image->l2_cache;
1015 struct ext2_qcow2_l2_table *table = cache->used_head;
1018 /* Store current position */
1019 offset = seek_relative(fd, 0);
1022 while (cache->free < cache->count) {
1023 if (seek != table->offset) {
1024 seek_set(fd, table->offset);
1025 seek = table->offset;
1028 generic_write(fd, (char *)table->data, image->cluster_size,
1030 put_used_table(image, &table);
1031 seek += image->cluster_size;
1034 /* Restore previous position */
1035 seek_set(fd, offset);
1039 * Get first free table (from free_head) and put it into tail of used list
1041 * l2_table is used to return pointer to moved table.
1042 * Returns 1 if the cache is full, 0 otherwise.
1044 static void get_free_table(struct ext2_qcow2_image *image,
1045 struct ext2_qcow2_l2_table **l2_table)
1047 struct ext2_qcow2_l2_table *table;
1048 struct ext2_qcow2_l2_cache *cache = image->l2_cache;
1050 if (0 == cache->free)
1051 flush_l2_cache(image);
1053 table = cache->free_head;
1055 cache->free_head = table->next;
1057 if (cache->used_tail)
1058 cache->used_tail->next = table;
1060 /* First item in the used list */
1061 cache->used_head = table;
1063 cache->used_tail = table;
1069 static int add_l2_item(struct ext2_qcow2_image *img, blk64_t blk,
1070 blk64_t data, blk64_t next)
1072 struct ext2_qcow2_l2_cache *cache = img->l2_cache;
1073 struct ext2_qcow2_l2_table *table = cache->used_tail;
1074 blk64_t l1_index = blk / img->l2_size;
1075 blk64_t l2_index = blk & (img->l2_size - 1);
1079 * Need to create new table if it does not exist,
1082 if (!table || (table->l1_index != l1_index)) {
1083 get_free_table(img, &table);
1084 table->l1_index = l1_index;
1085 table->offset = cache->next_offset;
1086 cache->next_offset = next;
1087 img->l1_table[l1_index] =
1088 ext2fs_cpu_to_be64(table->offset | QCOW_OFLAG_COPIED);
1092 table->data[l2_index] = ext2fs_cpu_to_be64(data | QCOW_OFLAG_COPIED);
1096 static int update_refcount(int fd, struct ext2_qcow2_image *img,
1097 blk64_t offset, blk64_t rfblk_pos)
1099 struct ext2_qcow2_refcount *ref;
1103 ref = &(img->refcount);
1104 table_index = offset >> (2 * img->cluster_bits - 1);
1107 * Need to create new refcount block when the offset addresses
1108 * another item in the refcount table
1110 if (table_index != ref->refcount_table_index) {
1112 seek_set(fd, ref->refcount_block_offset);
1114 generic_write(fd, (char *)ref->refcount_block,
1115 img->cluster_size, NO_BLK);
1116 memset(ref->refcount_block, 0, img->cluster_size);
1118 ref->refcount_table[ref->refcount_table_index] =
1119 ext2fs_cpu_to_be64(ref->refcount_block_offset);
1120 ref->refcount_block_offset = rfblk_pos;
1121 ref->refcount_block_index = 0;
1122 ref->refcount_table_index = table_index;
1127 * We are relying on the fact that we are creating the qcow2
1128 * image sequentially, hence we will always allocate refcount
1129 * block items sequentially.
1131 ref->refcount_block[ref->refcount_block_index] = ext2fs_cpu_to_be16(1);
1132 ref->refcount_block_index++;
1136 static int sync_refcount(int fd, struct ext2_qcow2_image *img)
1138 struct ext2_qcow2_refcount *ref;
1140 ref = &(img->refcount);
1142 ref->refcount_table[ref->refcount_table_index] =
1143 ext2fs_cpu_to_be64(ref->refcount_block_offset);
1144 seek_set(fd, ref->refcount_table_offset);
1145 generic_write(fd, (char *)ref->refcount_table,
1146 ref->refcount_table_clusters << img->cluster_bits, NO_BLK);
1148 seek_set(fd, ref->refcount_block_offset);
1149 generic_write(fd, (char *)ref->refcount_block, img->cluster_size,
1154 static void output_qcow2_meta_data_blocks(ext2_filsys fs, int fd)
1157 blk64_t blk, offset, size, end;
1159 struct ext2_qcow2_image *img;
1160 unsigned int header_size;
1162 /* allocate struct ext2_qcow2_image */
1163 retval = ext2fs_get_mem(sizeof(struct ext2_qcow2_image), &img);
1165 com_err(program_name, retval, "%s",
1166 _("while allocating ext2_qcow2_image"));
1170 retval = initialize_qcow2_image(fd, fs, img);
1172 com_err(program_name, retval, "%s",
1173 _("while initializing ext2_qcow2_image"));
1176 header_size = align_offset(sizeof(struct ext2_qcow2_hdr),
1178 write_header(fd, img->hdr, sizeof(struct ext2_qcow2_hdr), header_size);
1180 /* Refcount all qcow2 related metadata up to refcount_block_offset */
1181 end = img->refcount.refcount_block_offset;
1183 blk = end + img->cluster_size;
1184 for (offset = 0; offset <= end; offset += img->cluster_size) {
1185 if (update_refcount(fd, img, offset, blk)) {
1186 blk += img->cluster_size;
1188 * If we create new refcount block, we need to refcount
1191 end += img->cluster_size;
1194 seek_set(fd, offset);
1196 retval = ext2fs_get_mem(fs->blocksize, &buf);
1198 com_err(program_name, retval, "%s",
1199 _("while allocating buffer"));
1202 /* Write qcow2 data blocks */
1203 for (blk = 0; blk < ext2fs_blocks_count(fs->super); blk++) {
1204 if ((blk >= fs->super->s_first_data_block) &&
1205 ext2fs_test_block_bitmap2(meta_block_map, blk)) {
1206 retval = io_channel_read_blk64(fs->io, blk, 1, buf);
1208 com_err(program_name, retval,
1209 _("error reading block %llu"),
1210 (unsigned long long) blk);
1213 if (scramble_block_map &&
1214 ext2fs_test_block_bitmap2(scramble_block_map, blk))
1215 scramble_dir_block(fs, blk, buf);
1216 if (check_zero_block(buf, fs->blocksize))
1219 if (update_refcount(fd, img, offset, offset)) {
1220 /* Make space for another refcount block */
1221 offset += img->cluster_size;
1222 seek_set(fd, offset);
1224 * We have created the new refcount block, this
1225 * means that we need to refcount it as well.
1226 * So the previous update_refcount refcounted
1227 * the block itself and now we are going to
1228 * create refcount for data. New refcount
1229 * block should not be created!
1231 if (update_refcount(fd, img, offset, offset)) {
1232 fprintf(stderr, "%s",
1233 _("Programming error: multiple "
1234 "sequential refcount blocks "
1240 generic_write(fd, buf, fs->blocksize, blk);
1242 if (add_l2_item(img, blk, offset,
1243 offset + img->cluster_size)) {
1244 offset += img->cluster_size;
1245 if (update_refcount(fd, img, offset,
1246 offset + img->cluster_size)) {
1247 offset += img->cluster_size;
1248 if (update_refcount(fd, img, offset,
1250 fprintf(stderr, "%s",
1251 _("Programming error: multiple sequential refcount "
1252 "blocks created!\n"));
1256 offset += img->cluster_size;
1257 seek_set(fd, offset);
1261 offset += img->cluster_size;
1264 update_refcount(fd, img, offset, offset);
1265 flush_l2_cache(img);
1266 sync_refcount(fd, img);
1269 seek_set(fd, img->l1_offset);
1270 size = img->l1_size * sizeof(__u64);
1271 generic_write(fd, (char *)img->l1_table, size, NO_BLK);
1273 ext2fs_free_mem(&buf);
1274 free_qcow2_image(img);
1277 static void write_raw_image_file(ext2_filsys fs, int fd, int type, int flags,
1280 struct process_block_struct pb;
1281 struct ext2_inode inode;
1282 ext2_inode_scan scan;
1287 meta_blocks_count = 0;
1288 retval = ext2fs_allocate_block_bitmap(fs, _("in-use block map"),
1291 com_err(program_name, retval, "%s",
1292 _("while allocating block bitmap"));
1296 if (flags & E2IMAGE_SCRAMBLE_FLAG) {
1297 retval = ext2fs_allocate_block_bitmap(fs, "scramble block map",
1298 &scramble_block_map);
1300 com_err(program_name, retval, "%s",
1301 _("while allocating scramble block bitmap"));
1309 ext2fs_mark_block_bitmap2(meta_block_map, superblock);
1310 meta_blocks_count++;
1313 * Mark the backup superblock descriptors
1315 for (j = 0; j < fs->desc_blocks; j++) {
1316 ext2fs_mark_block_bitmap2(meta_block_map,
1317 ext2fs_descriptor_block_loc2(fs, superblock, j));
1319 meta_blocks_count += fs->desc_blocks;
1322 mark_table_blocks(fs);
1324 fprintf(stderr, "%s", _("Scanning inodes...\n"));
1326 retval = ext2fs_open_inode_scan(fs, 0, &scan);
1328 com_err(program_name, retval, "%s",
1329 _("while opening inode scan"));
1333 retval = ext2fs_get_mem(fs->blocksize * 3, &block_buf);
1335 com_err(program_name, 0, "%s",
1336 _("Can't allocate block buffer"));
1340 use_inode_shortcuts(fs, 1);
1341 stashed_inode = &inode;
1343 retval = ext2fs_get_next_inode(scan, &ino, &inode);
1344 if (retval == EXT2_ET_BAD_BLOCK_IN_INODE_TABLE)
1347 com_err(program_name, retval, "%s",
1348 _("while getting next inode"));
1353 if (!inode.i_links_count)
1355 if (ext2fs_file_acl_block(fs, &inode)) {
1356 ext2fs_mark_block_bitmap2(meta_block_map,
1357 ext2fs_file_acl_block(fs, &inode));
1358 meta_blocks_count++;
1360 if (!ext2fs_inode_has_valid_blocks2(fs, &inode))
1365 pb.is_dir = LINUX_S_ISDIR(inode.i_mode);
1366 if (LINUX_S_ISDIR(inode.i_mode) ||
1367 (LINUX_S_ISLNK(inode.i_mode) &&
1368 ext2fs_inode_has_valid_blocks2(fs, &inode)) ||
1369 ino == fs->super->s_journal_inum) {
1370 retval = ext2fs_block_iterate3(fs, ino,
1371 BLOCK_FLAG_READ_ONLY, block_buf,
1372 process_dir_block, &pb);
1374 com_err(program_name, retval,
1375 _("while iterating over inode %u"),
1380 if ((inode.i_flags & EXT4_EXTENTS_FL) ||
1381 inode.i_block[EXT2_IND_BLOCK] ||
1382 inode.i_block[EXT2_DIND_BLOCK] ||
1383 inode.i_block[EXT2_TIND_BLOCK] || all_data) {
1384 retval = ext2fs_block_iterate3(fs,
1385 ino, BLOCK_FLAG_READ_ONLY, block_buf,
1386 process_file_block, &pb);
1388 com_err(program_name, retval,
1389 _("while iterating over inode %u"), ino);
1395 use_inode_shortcuts(fs, 0);
1397 if (type & E2IMAGE_QCOW2)
1398 output_qcow2_meta_data_blocks(fs, fd);
1400 output_meta_data_blocks(fs, fd, flags);
1402 ext2fs_free_mem(&block_buf);
1403 ext2fs_close_inode_scan(scan);
1404 ext2fs_free_block_bitmap(meta_block_map);
1405 if (type & E2IMAGE_SCRAMBLE_FLAG)
1406 ext2fs_free_block_bitmap(scramble_block_map);
1409 static void install_image(char *device, char *image_fn, int type)
1413 int open_flag = EXT2_FLAG_IMAGE_FILE | EXT2_FLAG_64BITS |
1414 EXT2_FLAG_IGNORE_CSUM_ERRORS;
1420 com_err(program_name, 0, "%s",
1421 _("Raw and qcow2 images cannot be installed"));
1425 #ifdef CONFIG_TESTIO_DEBUG
1426 if (getenv("TEST_IO_FLAGS") || getenv("TEST_IO_BLOCK")) {
1427 io_ptr = test_io_manager;
1428 test_io_backing_manager = unix_io_manager;
1431 io_ptr = unix_io_manager;
1433 retval = ext2fs_open (image_fn, open_flag, 0, 0,
1436 com_err(program_name, retval, _("while trying to open %s"),
1441 retval = ext2fs_read_bitmaps (fs);
1443 com_err(program_name, retval, "%s", _("error reading bitmaps"));
1447 fd = ext2fs_open_file(image_fn, O_RDONLY, 0);
1453 retval = io_ptr->open(device, IO_FLAG_RW, &io);
1455 com_err(device, 0, "%s", _("while opening device file"));
1459 ext2fs_rewrite_to_io(fs, io);
1461 seek_set(fd, ext2fs_le32_to_cpu(fs->image_header->offset_inode));
1463 retval = ext2fs_image_inode_read(fs, fd, 0);
1465 com_err(image_fn, 0, "%s",
1466 _("while restoring the image table"));
1471 ext2fs_close_free(&fs);
1474 static struct ext2_qcow2_hdr *check_qcow2_image(int *fd, char *name)
1477 *fd = ext2fs_open_file(name, O_RDONLY, 0600);
1481 return qcow2_read_header(*fd);
1484 int main (int argc, char ** argv)
1489 char *image_fn, offset_opt[64];
1490 struct ext2_qcow2_hdr *header = NULL;
1491 int open_flag = EXT2_FLAG_64BITS | EXT2_FLAG_THREADS |
1492 EXT2_FLAG_IGNORE_CSUM_ERRORS;
1495 int mount_flags = 0;
1499 int ignore_rw_mount = 0;
1502 blk64_t superblock = 0;
1506 setlocale(LC_MESSAGES, "");
1507 setlocale(LC_CTYPE, "");
1508 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
1509 textdomain(NLS_CAT_NAME);
1510 set_com_err_gettext(gettext);
1512 fprintf (stderr, "e2image %s (%s)\n", E2FSPROGS_VERSION,
1515 program_name = *argv;
1516 add_error_table(&et_ext2_error_table);
1517 while ((c = getopt(argc, argv, "b:B:nrsIQafo:O:pc")) != EOF)
1520 superblock = strtoull(optarg, NULL, 0);
1523 blocksize = strtoul(optarg, NULL, 0);
1526 flags |= E2IMAGE_INSTALL_FLAG;
1531 img_type |= E2IMAGE_QCOW2;
1536 img_type |= E2IMAGE_RAW;
1539 flags |= E2IMAGE_SCRAMBLE_FLAG;
1545 ignore_rw_mount = 1;
1551 source_offset = strtoull(optarg, NULL, 0);
1554 dest_offset = strtoull(optarg, NULL, 0);
1565 if (optind == argc - 1 &&
1566 (source_offset || dest_offset))
1568 else if (optind != argc - 2 )
1571 if (all_data && !img_type) {
1572 com_err(program_name, 0, "%s", _("-a option can only be used "
1573 "with raw or QCOW2 images."));
1576 if (superblock && !img_type) {
1577 com_err(program_name, 0, "%s", _("-b option can only be used "
1578 "with raw or QCOW2 images."));
1581 if ((source_offset || dest_offset) && img_type != E2IMAGE_RAW) {
1582 com_err(program_name, 0, "%s",
1583 _("Offsets are only allowed with raw images."));
1586 if (move_mode && img_type != E2IMAGE_RAW) {
1587 com_err(program_name, 0, "%s",
1588 _("Move mode is only allowed with raw images."));
1591 if (move_mode && !all_data) {
1592 com_err(program_name, 0, "%s",
1593 _("Move mode requires all data mode."));
1596 device_name = argv[optind];
1598 image_fn = device_name;
1599 else image_fn = argv[optind+1];
1601 retval = ext2fs_check_if_mounted(device_name, &mount_flags);
1603 com_err(program_name, retval, "%s", _("checking if mounted"));
1607 if (img_type && !ignore_rw_mount &&
1608 (mount_flags & EXT2_MF_MOUNTED) &&
1609 !(mount_flags & EXT2_MF_READONLY)) {
1610 fprintf(stderr, "%s", _("\nRunning e2image on a R/W mounted "
1611 "filesystem can result in an\n"
1612 "inconsistent image which will not be useful "
1613 "for debugging purposes.\n"
1614 "Use -f option if you really want to do that.\n"));
1618 if (flags & E2IMAGE_INSTALL_FLAG) {
1619 install_image(device_name, image_fn, img_type);
1623 if (img_type & E2IMAGE_RAW) {
1624 header = check_qcow2_image(&qcow2_fd, device_name);
1626 flags |= E2IMAGE_IS_QCOW2_FLAG;
1630 sprintf(offset_opt, "offset=%llu", (unsigned long long) source_offset);
1631 retval = ext2fs_open2(device_name, offset_opt, open_flag,
1632 superblock, blocksize, unix_io_manager, &fs);
1634 com_err (program_name, retval, _("while trying to open %s"),
1636 fputs(_("Couldn't find valid filesystem superblock.\n"), stdout);
1637 if (retval == EXT2_ET_BAD_MAGIC)
1638 check_plausibility(device_name, CHECK_FS_EXIST, NULL);
1643 if (strcmp(image_fn, "-") == 0)
1646 int o_flags = O_CREAT|O_RDWR;
1648 if (img_type != E2IMAGE_RAW)
1650 if (access(image_fn, F_OK) != 0)
1651 flags |= E2IMAGE_CHECK_ZERO_FLAG;
1652 fd = ext2fs_open_file(image_fn, o_flags, 0600);
1654 com_err(program_name, errno,
1655 _("while trying to open %s"), image_fn);
1660 seek_set(fd, dest_offset);
1662 if ((img_type & E2IMAGE_QCOW2) && (fd == 1)) {
1663 com_err(program_name, 0, "%s",
1664 _("QCOW2 image can not be written to the stdout!\n"));
1668 if (fstat(fd, &st)) {
1669 com_err(program_name, 0, "%s",
1670 _("Can not stat output\n"));
1673 if (ext2fsP_is_disk_device(st.st_mode))
1676 if (flags & E2IMAGE_IS_QCOW2_FLAG) {
1677 ret = qcow2_write_raw_image(qcow2_fd, fd, header);
1679 if (ret == -QCOW_COMPRESSED)
1680 fprintf(stderr, _("Image (%s) is compressed\n"),
1682 else if (ret == -QCOW_ENCRYPTED)
1683 fprintf(stderr, _("Image (%s) is encrypted\n"),
1685 else if (ret == -QCOW_CORRUPTED)
1686 fprintf(stderr, _("Image (%s) is corrupted\n"),
1689 com_err(program_name, ret,
1690 _("while trying to convert qcow2 image"
1691 " (%s) into raw image (%s)"),
1692 image_fn, device_name);
1699 if (img_type != E2IMAGE_RAW) {
1700 fprintf(stderr, "%s", _("The -c option only supported "
1705 fprintf(stderr, "%s", _("The -c option not supported "
1706 "when writing to stdout\n"));
1709 retval = ext2fs_get_mem(fs->blocksize, &check_buf);
1711 com_err(program_name, retval, "%s",
1712 _("while allocating check_buf"));
1716 if (show_progress && (img_type != E2IMAGE_RAW)) {
1717 fprintf(stderr, "%s",
1718 _("The -p option only supported in raw mode\n"));
1722 write_raw_image_file(fs, fd, img_type, flags, superblock);
1724 write_image_file(fs, fd);
1726 ext2fs_close_free(&fs);
1728 printf(_("%d blocks already contained the data to be copied\n"),
1736 remove_error_table(&et_ext2_error_table);