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 (1LL << 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 ] device image-file\n"),
109 fprintf(stderr
, _(" %s -I device image-file\n"), program_name
);
110 fprintf(stderr
, _(" %s -ra [ -cfnp ] [ -o src_offset ] "
111 "[ -O dest_offset ] src_fs [ dest_fs ]\n"),
116 static ext2_loff_t
seek_relative(int fd
, int offset
)
118 ext2_loff_t ret
= ext2fs_llseek(fd
, offset
, SEEK_CUR
);
120 perror("seek_relative");
126 static ext2_loff_t
seek_set(int fd
, ext2_loff_t offset
)
128 ext2_loff_t ret
= ext2fs_llseek(fd
, offset
, SEEK_SET
);
137 * Returns true if the block we are about to write is identical to
138 * what is already on the disk.
140 static int check_block(int fd
, void *buf
, void *cbuf
, int blocksize
)
143 int count
= blocksize
, ret
;
149 ret
= read(fd
, cp
, count
);
151 perror("check_block");
157 ret
= memcmp(buf
, cbuf
, blocksize
);
158 seek_relative(fd
, -blocksize
);
159 return (ret
== 0) ? 1 : 0;
162 static void generic_write(int fd
, void *buf
, int blocksize
, blk64_t block
)
164 int count
, free_buf
= 0;
172 err
= ext2fs_get_arrayzero(1, blocksize
, &buf
);
174 com_err(program_name
, err
, "%s",
175 _("while allocating buffer"));
180 printf(_("Writing block %llu\n"), (unsigned long long) block
);
182 seek_relative(fd
, blocksize
);
183 goto free_and_return
;
185 count
= write(fd
, buf
, blocksize
);
186 if (count
!= blocksize
) {
193 com_err(program_name
, err
,
194 _("error writing block %llu"), block
);
196 com_err(program_name
, err
, "%s",
197 _("error in generic_write()"));
203 ext2fs_free_mem(&buf
);
206 static void write_header(int fd
, void *hdr
, int hdr_size
, int wrt_size
)
212 if (hdr_size
> wrt_size
) {
213 fprintf(stderr
, "%s",
214 _("Error: header size is bigger than wrt_size\n"));
217 ret
= ext2fs_get_mem(wrt_size
, &header_buf
);
219 fputs(_("Couldn't allocate header buffer\n"), stderr
);
224 memset(header_buf
, 0, wrt_size
);
227 memcpy(header_buf
, hdr
, hdr_size
);
229 generic_write(fd
, header_buf
, wrt_size
, NO_BLK
);
231 ext2fs_free_mem(&header_buf
);
234 static void write_image_file(ext2_filsys fs
, int fd
)
236 struct ext2_image_hdr hdr
;
240 write_header(fd
, NULL
, sizeof(struct ext2_image_hdr
), fs
->blocksize
);
241 memset(&hdr
, 0, sizeof(struct ext2_image_hdr
));
243 hdr
.offset_super
= seek_relative(fd
, 0);
244 retval
= ext2fs_image_super_write(fs
, fd
, 0);
246 com_err(program_name
, retval
, "%s",
247 _("while writing superblock"));
251 hdr
.offset_inode
= seek_relative(fd
, 0);
252 retval
= ext2fs_image_inode_write(fs
, fd
,
253 (fd
!= 1) ? IMAGER_FLAG_SPARSEWRITE
: 0);
255 com_err(program_name
, retval
, "%s",
256 _("while writing inode table"));
260 hdr
.offset_blockmap
= seek_relative(fd
, 0);
261 retval
= ext2fs_image_bitmap_write(fs
, fd
, 0);
263 com_err(program_name
, retval
, "%s",
264 _("while writing block bitmap"));
268 hdr
.offset_inodemap
= seek_relative(fd
, 0);
269 retval
= ext2fs_image_bitmap_write(fs
, fd
, IMAGER_FLAG_INODEMAP
);
271 com_err(program_name
, retval
, "%s",
272 _("while writing inode bitmap"));
276 hdr
.magic_number
= EXT2_ET_MAGIC_E2IMAGE
;
277 strcpy(hdr
.magic_descriptor
, "Ext2 Image 1.0");
278 gethostname(hdr
.fs_hostname
, sizeof(hdr
.fs_hostname
));
279 strncpy(hdr
.fs_device_name
, device_name
, sizeof(hdr
.fs_device_name
)-1);
280 hdr
.fs_device_name
[sizeof(hdr
.fs_device_name
) - 1] = 0;
281 hdr
.fs_blocksize
= fs
->blocksize
;
283 if (stat(device_name
, &st
) == 0)
284 hdr
.fs_device
= st
.st_rdev
;
286 if (fstat(fd
, &st
) == 0) {
287 hdr
.image_device
= st
.st_dev
;
288 hdr
.image_inode
= st
.st_ino
;
290 memcpy(hdr
.fs_uuid
, fs
->super
->s_uuid
, sizeof(hdr
.fs_uuid
));
292 hdr
.image_time
= time(0);
293 write_header(fd
, &hdr
, sizeof(struct ext2_image_hdr
), fs
->blocksize
);
297 * These set of functions are used to write a RAW image file.
299 static ext2fs_block_bitmap meta_block_map
;
300 static ext2fs_block_bitmap scramble_block_map
; /* Directory blocks to be scrambled */
301 static blk64_t meta_blocks_count
;
303 struct process_block_struct
{
309 * These subroutines short circuits ext2fs_get_blocks and
310 * ext2fs_check_directory; we use them since we already have the inode
311 * structure, so there's no point in letting the ext2fs library read
314 static ino_t stashed_ino
= 0;
315 static struct ext2_inode
*stashed_inode
;
317 static errcode_t
meta_get_blocks(ext2_filsys fs
EXT2FS_ATTR((unused
)),
323 if ((ino
!= stashed_ino
) || !stashed_inode
)
324 return EXT2_ET_CALLBACK_NOTHANDLED
;
326 for (i
=0; i
< EXT2_N_BLOCKS
; i
++)
327 blocks
[i
] = stashed_inode
->i_block
[i
];
331 static errcode_t
meta_check_directory(ext2_filsys fs
EXT2FS_ATTR((unused
)),
334 if ((ino
!= stashed_ino
) || !stashed_inode
)
335 return EXT2_ET_CALLBACK_NOTHANDLED
;
337 if (!LINUX_S_ISDIR(stashed_inode
->i_mode
))
338 return EXT2_ET_NO_DIRECTORY
;
342 static errcode_t
meta_read_inode(ext2_filsys fs
EXT2FS_ATTR((unused
)),
344 struct ext2_inode
*inode
)
346 if ((ino
!= stashed_ino
) || !stashed_inode
)
347 return EXT2_ET_CALLBACK_NOTHANDLED
;
348 *inode
= *stashed_inode
;
352 static void use_inode_shortcuts(ext2_filsys fs
, int use_shortcuts
)
355 fs
->get_blocks
= meta_get_blocks
;
356 fs
->check_directory
= meta_check_directory
;
357 fs
->read_inode
= meta_read_inode
;
361 fs
->check_directory
= 0;
366 static int process_dir_block(ext2_filsys fs
EXT2FS_ATTR((unused
)),
368 e2_blkcnt_t blockcnt
EXT2FS_ATTR((unused
)),
369 blk64_t ref_block
EXT2FS_ATTR((unused
)),
370 int ref_offset
EXT2FS_ATTR((unused
)),
371 void *priv_data
EXT2FS_ATTR((unused
)))
373 struct process_block_struct
*p
;
375 p
= (struct process_block_struct
*) priv_data
;
377 ext2fs_mark_block_bitmap2(meta_block_map
, *block_nr
);
379 if (scramble_block_map
&& p
->is_dir
&& blockcnt
>= 0)
380 ext2fs_mark_block_bitmap2(scramble_block_map
, *block_nr
);
384 static int process_file_block(ext2_filsys fs
EXT2FS_ATTR((unused
)),
386 e2_blkcnt_t blockcnt
,
387 blk64_t ref_block
EXT2FS_ATTR((unused
)),
388 int ref_offset
EXT2FS_ATTR((unused
)),
389 void *priv_data
EXT2FS_ATTR((unused
)))
391 if (blockcnt
< 0 || all_data
) {
392 ext2fs_mark_block_bitmap2(meta_block_map
, *block_nr
);
398 static void mark_table_blocks(ext2_filsys fs
)
400 blk64_t first_block
, b
;
403 first_block
= fs
->super
->s_first_data_block
;
405 * Mark primary superblock
407 ext2fs_mark_block_bitmap2(meta_block_map
, first_block
);
411 * Mark the primary superblock descriptors
413 for (j
= 0; j
< fs
->desc_blocks
; j
++) {
414 ext2fs_mark_block_bitmap2(meta_block_map
,
415 ext2fs_descriptor_block_loc2(fs
, first_block
, j
));
417 meta_blocks_count
+= fs
->desc_blocks
;
419 for (i
= 0; i
< fs
->group_desc_count
; i
++) {
421 * Mark the blocks used for the inode table
423 if ((output_is_blk
||
424 !ext2fs_bg_flags_test(fs
, i
, EXT2_BG_INODE_UNINIT
)) &&
425 ext2fs_inode_table_loc(fs
, i
)) {
426 unsigned int end
= (unsigned) fs
->inode_blocks_per_group
;
427 /* skip unused blocks */
428 if (!output_is_blk
&& ext2fs_has_group_desc_csum(fs
))
429 end
-= (ext2fs_bg_itable_unused(fs
, i
) /
430 EXT2_INODES_PER_BLOCK(fs
->super
));
431 for (j
= 0, b
= ext2fs_inode_table_loc(fs
, i
);
434 ext2fs_mark_block_bitmap2(meta_block_map
, b
);
440 * Mark block used for the block bitmap
442 if (!ext2fs_bg_flags_test(fs
, i
, EXT2_BG_BLOCK_UNINIT
) &&
443 ext2fs_block_bitmap_loc(fs
, i
)) {
444 ext2fs_mark_block_bitmap2(meta_block_map
,
445 ext2fs_block_bitmap_loc(fs
, i
));
450 * Mark block used for the inode bitmap
452 if (!ext2fs_bg_flags_test(fs
, i
, EXT2_BG_INODE_UNINIT
) &&
453 ext2fs_inode_bitmap_loc(fs
, i
)) {
454 ext2fs_mark_block_bitmap2(meta_block_map
,
455 ext2fs_inode_bitmap_loc(fs
, i
));
462 * This function returns 1 if the specified block is all zeros
464 static int check_zero_block(char *buf
, int blocksize
)
467 int left
= blocksize
;
479 static int name_id
[256];
481 #define EXT4_MAX_REC_LEN ((1<<16)-1)
483 static void scramble_dir_block(ext2_filsys fs
, blk64_t blk
, char *buf
)
486 struct ext2_dir_entry_2
*dirent
;
487 unsigned int rec_len
;
490 end
= buf
+ fs
->blocksize
;
491 for (p
= buf
; p
< end
-8; p
+= rec_len
) {
492 dirent
= (struct ext2_dir_entry_2
*) p
;
493 rec_len
= dirent
->rec_len
;
494 #ifdef WORDS_BIGENDIAN
495 rec_len
= ext2fs_swab16(rec_len
);
497 if (rec_len
== EXT4_MAX_REC_LEN
|| rec_len
== 0)
498 rec_len
= fs
->blocksize
;
500 rec_len
= (rec_len
& 65532) | ((rec_len
& 3) << 16);
502 printf("rec_len = %d, name_len = %d\n", rec_len
, dirent
->name_len
);
504 if (rec_len
< 8 || (rec_len
% 4) ||
506 printf(_("Corrupt directory block %llu: "
507 "bad rec_len (%d)\n"),
508 (unsigned long long) blk
, rec_len
);
510 (void) ext2fs_set_rec_len(fs
, rec_len
,
511 (struct ext2_dir_entry
*) dirent
);
512 #ifdef WORDS_BIGENDIAN
513 dirent
->rec_len
= ext2fs_swab16(dirent
->rec_len
);
517 if (dirent
->name_len
+ 8U > rec_len
) {
518 printf(_("Corrupt directory block %llu: "
519 "bad name_len (%d)\n"),
520 (unsigned long long) blk
, dirent
->name_len
);
521 dirent
->name_len
= rec_len
- 8;
525 len
= rec_len
- dirent
->name_len
- 8;
527 memset(cp
+dirent
->name_len
, 0, len
);
528 if (dirent
->name_len
==1 && cp
[0] == '.')
530 if (dirent
->name_len
==2 && cp
[0] == '.' && cp
[1] == '.')
533 memset(cp
, 'A', dirent
->name_len
);
534 len
= dirent
->name_len
;
536 while ((len
> 0) && (id
> 0)) {
545 static char got_sigint
;
547 static void sigint_handler(int unused
EXT2FS_ATTR((unused
)))
550 signal (SIGINT
, SIG_DFL
);
553 #define calc_percent(a, b) ((int) ((100.0 * (((float) (a)) / \
554 ((float) (b)))) + 0.5))
555 #define calc_rate(t, b, d) (((float)(t) / ((1024 * 1024) / (b))) / (d))
557 static int print_progress(blk64_t num
, blk64_t total
)
559 return fprintf(stderr
, _("%llu / %llu blocks (%d%%)"), num
, total
,
560 calc_percent(num
, total
));
563 static void output_meta_data_blocks(ext2_filsys fs
, int fd
, int flags
)
567 char *buf
, *zero_buf
;
570 blk64_t distance
= 0;
571 blk64_t end
= ext2fs_blocks_count(fs
->super
);
572 time_t last_update
= 0;
573 time_t start_time
= 0;
574 blk64_t total_written
= 0;
577 retval
= ext2fs_get_mem(fs
->blocksize
, &buf
);
579 com_err(program_name
, retval
, "%s",
580 _("while allocating buffer"));
583 retval
= ext2fs_get_memzero(fs
->blocksize
, &zero_buf
);
585 com_err(program_name
, retval
, "%s",
586 _("while allocating buffer"));
590 fprintf(stderr
, "%s", _("Copying "));
591 bscount
= print_progress(total_written
, meta_blocks_count
);
593 last_update
= time(NULL
);
594 start_time
= time(NULL
);
596 /* when doing an in place move to the right, you can't start
597 at the beginning or you will overwrite data, so instead
598 divide the fs up into distance size chunks and write them
600 if (move_mode
&& dest_offset
> source_offset
) {
601 distance
= (dest_offset
- source_offset
) / fs
->blocksize
;
602 if (distance
< ext2fs_blocks_count(fs
->super
))
603 start
= ext2fs_blocks_count(fs
->super
) - distance
;
606 signal (SIGINT
, sigint_handler
);
609 seek_set(fd
, (start
* fs
->blocksize
) + dest_offset
);
610 for (blk
= start
; blk
< end
; blk
++) {
613 /* moving to the right */
614 if (distance
>= ext2fs_blocks_count(fs
->super
)||
615 start
== ext2fs_blocks_count(fs
->super
) -
617 kill(getpid(), SIGINT
);
619 /* moving to the left */
620 if (blk
< (source_offset
- dest_offset
) /
622 kill(getpid(), SIGINT
);
626 fprintf(stderr
, "%s",
627 _("Stopping now will destroy the filesystem, "
628 "interrupt again if you are sure\n"));
630 fprintf(stderr
, "%s", _("Copying "));
631 bscount
= print_progress(total_written
,
638 if (show_progress
&& last_update
!= time(NULL
)) {
640 last_update
= time(NULL
);
643 bscount
= print_progress(total_written
,
645 duration
= time(NULL
) - start_time
;
646 if (duration
> 5 && total_written
) {
647 time_t est
= (duration
* meta_blocks_count
/
648 total_written
) - duration
;
650 strftime(buff
, 30, "%T", gmtime(&est
));
653 _(" %s remaining at %.2f MB/s"),
654 buff
, calc_rate(total_written
,
660 if ((blk
>= fs
->super
->s_first_data_block
) &&
661 ext2fs_test_block_bitmap2(meta_block_map
, blk
)) {
662 retval
= io_channel_read_blk64(fs
->io
, blk
, 1, buf
);
664 com_err(program_name
, retval
,
665 _("error reading block %llu"), blk
);
668 if (scramble_block_map
&&
669 ext2fs_test_block_bitmap2(scramble_block_map
, blk
))
670 scramble_dir_block(fs
, blk
, buf
);
671 if ((flags
& E2IMAGE_CHECK_ZERO_FLAG
) &&
672 check_zero_block(buf
, fs
->blocksize
))
675 seek_relative(fd
, sparse
);
677 if (check_block(fd
, buf
, check_buf
, fs
->blocksize
)) {
678 seek_relative(fd
, fs
->blocksize
);
681 generic_write(fd
, buf
, fs
->blocksize
, blk
);
686 generic_write(fd
, zero_buf
,
690 sparse
+= fs
->blocksize
;
691 if (sparse
> 1024*1024) {
692 seek_relative(fd
, 1024*1024);
697 if (distance
&& start
) {
698 if (start
< distance
) {
704 if (end
< distance
) {
705 /* past overlap, do rest in one go */
713 signal (SIGINT
, SIG_DFL
);
715 time_t duration
= time(NULL
) - start_time
;
718 strftime(buff
, 30, "%T", gmtime(&duration
));
719 fprintf(stderr
, _("Copied %llu / %llu blocks (%d%%) in %s "),
720 total_written
, meta_blocks_count
,
721 calc_percent(total_written
, meta_blocks_count
), buff
);
723 fprintf(stderr
, _("at %.2f MB/s"),
724 calc_rate(total_written
, fs
->blocksize
, duration
));
725 fputs(" \n", stderr
);
727 #ifdef HAVE_FTRUNCATE64
731 offset
= seek_set(fd
,
732 fs
->blocksize
* ext2fs_blocks_count(fs
->super
) + dest_offset
);
734 offset
= seek_relative(fd
, sparse
);
736 if (ftruncate64(fd
, offset
) < 0) {
737 seek_relative(fd
, -1);
738 generic_write(fd
, zero_buf
, 1, NO_BLK
);
742 if (sparse
&& !distance
) {
743 seek_relative(fd
, sparse
-1);
744 generic_write(fd
, zero_buf
, 1, NO_BLK
);
747 ext2fs_free_mem(&zero_buf
);
748 ext2fs_free_mem(&buf
);
751 static void init_l1_table(struct ext2_qcow2_image
*image
)
756 ret
= ext2fs_get_arrayzero(image
->l1_size
, sizeof(__u64
), &l1_table
);
758 com_err(program_name
, ret
, "%s",
759 _("while allocating l1 table"));
763 image
->l1_table
= l1_table
;
766 static void init_l2_cache(struct ext2_qcow2_image
*image
)
768 unsigned int count
, i
;
769 struct ext2_qcow2_l2_cache
*cache
;
770 struct ext2_qcow2_l2_table
*table
;
773 ret
= ext2fs_get_arrayzero(1, sizeof(struct ext2_qcow2_l2_cache
),
778 count
= (image
->l1_size
> L2_CACHE_PREALLOC
) ? L2_CACHE_PREALLOC
:
781 cache
->count
= count
;
783 cache
->next_offset
= image
->l2_offset
;
785 for (i
= 0; i
< count
; i
++) {
786 ret
= ext2fs_get_arrayzero(1,
787 sizeof(struct ext2_qcow2_l2_table
), &table
);
791 ret
= ext2fs_get_arrayzero(image
->l2_size
,
792 sizeof(__u64
), &table
->data
);
796 table
->next
= cache
->free_head
;
797 cache
->free_head
= table
;
800 image
->l2_cache
= cache
;
804 com_err(program_name
, ret
, "%s", _("while allocating l2 cache"));
808 static void put_l2_cache(struct ext2_qcow2_image
*image
)
810 struct ext2_qcow2_l2_cache
*cache
= image
->l2_cache
;
811 struct ext2_qcow2_l2_table
*tmp
, *table
;
816 table
= cache
->free_head
;
817 cache
->free_head
= NULL
;
822 ext2fs_free_mem(&tmp
->data
);
823 ext2fs_free_mem(&tmp
);
826 if (cache
->free
!= cache
->count
) {
827 fprintf(stderr
, "%s", _("Warning: There are still tables in "
828 "the cache while putting the cache, "
829 "data will be lost so the image may "
831 table
= cache
->used_head
;
832 cache
->used_head
= NULL
;
836 ext2fs_free_mem(&cache
);
839 static int init_refcount(struct ext2_qcow2_image
*img
, blk64_t table_offset
)
841 struct ext2_qcow2_refcount
*ref
;
842 blk64_t table_clusters
;
845 ref
= &(img
->refcount
);
848 * One refcount block addresses 2048 clusters, one refcount table
849 * addresses cluster/sizeof(__u64) refcount blocks, and we need
850 * to address meta_blocks_count clusters + qcow2 metadata clusters
853 table_clusters
= meta_blocks_count
+ (table_offset
>>
855 table_clusters
>>= (img
->cluster_bits
+ 6 - 1);
856 table_clusters
= (table_clusters
== 0) ? 1 : table_clusters
;
858 ref
->refcount_table_offset
= table_offset
;
859 ref
->refcount_table_clusters
= table_clusters
;
860 ref
->refcount_table_index
= 0;
861 ref
->refcount_block_index
= 0;
863 /* Allocate refcount table */
864 ret
= ext2fs_get_arrayzero(ref
->refcount_table_clusters
,
865 img
->cluster_size
, &ref
->refcount_table
);
869 /* Allocate refcount block */
870 ret
= ext2fs_get_arrayzero(1, img
->cluster_size
, &ref
->refcount_block
);
872 ext2fs_free_mem(&ref
->refcount_table
);
877 static int initialize_qcow2_image(int fd
, ext2_filsys fs
,
878 struct ext2_qcow2_image
*image
)
880 struct ext2_qcow2_hdr
*header
;
881 blk64_t total_size
, offset
;
882 int shift
, l2_bits
, header_size
, l1_size
, ret
;
883 int cluster_bits
= get_bits_from_size(fs
->blocksize
);
884 struct ext2_super_block
*sb
= fs
->super
;
886 /* Allocate header */
887 ret
= ext2fs_get_memzero(sizeof(struct ext2_qcow2_hdr
), &header
);
891 total_size
= ext2fs_blocks_count(sb
) << cluster_bits
;
892 image
->cluster_size
= fs
->blocksize
;
893 image
->l2_size
= 1 << (cluster_bits
- 3);
894 image
->cluster_bits
= cluster_bits
;
897 header
->magic
= ext2fs_cpu_to_be32(QCOW_MAGIC
);
898 header
->version
= ext2fs_cpu_to_be32(QCOW_VERSION
);
899 header
->size
= ext2fs_cpu_to_be64(total_size
);
900 header
->cluster_bits
= ext2fs_cpu_to_be32(cluster_bits
);
902 header_size
= (sizeof(struct ext2_qcow2_hdr
) + 7) & ~7;
903 offset
= align_offset(header_size
, image
->cluster_size
);
905 header
->l1_table_offset
= ext2fs_cpu_to_be64(offset
);
906 image
->l1_offset
= offset
;
908 l2_bits
= cluster_bits
- 3;
909 shift
= cluster_bits
+ l2_bits
;
910 l1_size
= ((total_size
+ (1LL << shift
) - 1) >> shift
);
911 header
->l1_size
= ext2fs_cpu_to_be32(l1_size
);
912 image
->l1_size
= l1_size
;
914 /* Make space for L1 table */
915 offset
+= align_offset(l1_size
* sizeof(blk64_t
), image
->cluster_size
);
917 /* Initialize refcounting */
918 ret
= init_refcount(image
, offset
);
920 ext2fs_free_mem(&header
);
923 header
->refcount_table_offset
= ext2fs_cpu_to_be64(offset
);
924 header
->refcount_table_clusters
=
925 ext2fs_cpu_to_be32(image
->refcount
.refcount_table_clusters
);
926 offset
+= image
->cluster_size
;
927 offset
+= image
->refcount
.refcount_table_clusters
<<
930 /* Make space for L2 tables */
931 image
->l2_offset
= offset
;
932 offset
+= image
->cluster_size
;
934 /* Make space for first refcount block */
935 image
->refcount
.refcount_block_offset
= offset
;
938 /* Initialize l1 and l2 tables */
939 init_l1_table(image
);
940 init_l2_cache(image
);
945 static void free_qcow2_image(struct ext2_qcow2_image
*img
)
951 ext2fs_free_mem(&img
->hdr
);
954 ext2fs_free_mem(&img
->l1_table
);
956 if (img
->refcount
.refcount_table
)
957 ext2fs_free_mem(&img
->refcount
.refcount_table
);
958 if (img
->refcount
.refcount_block
)
959 ext2fs_free_mem(&img
->refcount
.refcount_block
);
963 ext2fs_free_mem(&img
);
967 * Put table from used list (used_head) into free list (free_head).
968 * l2_table is used to return pointer to the next used table (used_head).
970 static void put_used_table(struct ext2_qcow2_image
*img
,
971 struct ext2_qcow2_l2_table
**l2_table
)
973 struct ext2_qcow2_l2_cache
*cache
= img
->l2_cache
;
974 struct ext2_qcow2_l2_table
*table
;
976 table
= cache
->used_head
;
977 cache
->used_head
= table
->next
;
981 cache
->used_tail
= NULL
;
983 /* Clean the table for case we will need to use it again */
984 memset(table
->data
, 0, img
->cluster_size
);
985 table
->next
= cache
->free_head
;
986 cache
->free_head
= table
;
990 *l2_table
= cache
->used_head
;
993 static void flush_l2_cache(struct ext2_qcow2_image
*image
)
997 struct ext2_qcow2_l2_cache
*cache
= image
->l2_cache
;
998 struct ext2_qcow2_l2_table
*table
= cache
->used_head
;
1001 /* Store current position */
1002 offset
= seek_relative(fd
, 0);
1005 while (cache
->free
< cache
->count
) {
1006 if (seek
!= table
->offset
) {
1007 seek_set(fd
, table
->offset
);
1008 seek
= table
->offset
;
1011 generic_write(fd
, (char *)table
->data
, image
->cluster_size
,
1013 put_used_table(image
, &table
);
1014 seek
+= image
->cluster_size
;
1017 /* Restore previous position */
1018 seek_set(fd
, offset
);
1022 * Get first free table (from free_head) and put it into tail of used list
1024 * l2_table is used to return pointer to moved table.
1025 * Returns 1 if the cache is full, 0 otherwise.
1027 static void get_free_table(struct ext2_qcow2_image
*image
,
1028 struct ext2_qcow2_l2_table
**l2_table
)
1030 struct ext2_qcow2_l2_table
*table
;
1031 struct ext2_qcow2_l2_cache
*cache
= image
->l2_cache
;
1033 if (0 == cache
->free
)
1034 flush_l2_cache(image
);
1036 table
= cache
->free_head
;
1038 cache
->free_head
= table
->next
;
1040 if (cache
->used_tail
)
1041 cache
->used_tail
->next
= table
;
1043 /* First item in the used list */
1044 cache
->used_head
= table
;
1046 cache
->used_tail
= table
;
1052 static int add_l2_item(struct ext2_qcow2_image
*img
, blk64_t blk
,
1053 blk64_t data
, blk64_t next
)
1055 struct ext2_qcow2_l2_cache
*cache
= img
->l2_cache
;
1056 struct ext2_qcow2_l2_table
*table
= cache
->used_tail
;
1057 blk64_t l1_index
= blk
/ img
->l2_size
;
1058 blk64_t l2_index
= blk
& (img
->l2_size
- 1);
1062 * Need to create new table if it does not exist,
1065 if (!table
|| (table
->l1_index
!= l1_index
)) {
1066 get_free_table(img
, &table
);
1067 table
->l1_index
= l1_index
;
1068 table
->offset
= cache
->next_offset
;
1069 cache
->next_offset
= next
;
1070 img
->l1_table
[l1_index
] =
1071 ext2fs_cpu_to_be64(table
->offset
| QCOW_OFLAG_COPIED
);
1075 table
->data
[l2_index
] = ext2fs_cpu_to_be64(data
| QCOW_OFLAG_COPIED
);
1079 static int update_refcount(int fd
, struct ext2_qcow2_image
*img
,
1080 blk64_t offset
, blk64_t rfblk_pos
)
1082 struct ext2_qcow2_refcount
*ref
;
1086 ref
= &(img
->refcount
);
1087 table_index
= offset
>> (2 * img
->cluster_bits
- 1);
1090 * Need to create new refcount block when the offset addresses
1091 * another item in the refcount table
1093 if (table_index
!= ref
->refcount_table_index
) {
1095 seek_set(fd
, ref
->refcount_block_offset
);
1097 generic_write(fd
, (char *)ref
->refcount_block
,
1098 img
->cluster_size
, NO_BLK
);
1099 memset(ref
->refcount_block
, 0, img
->cluster_size
);
1101 ref
->refcount_table
[ref
->refcount_table_index
] =
1102 ext2fs_cpu_to_be64(ref
->refcount_block_offset
);
1103 ref
->refcount_block_offset
= rfblk_pos
;
1104 ref
->refcount_block_index
= 0;
1105 ref
->refcount_table_index
= table_index
;
1110 * We are relying on the fact that we are creating the qcow2
1111 * image sequentially, hence we will always allocate refcount
1112 * block items sequentially.
1114 ref
->refcount_block
[ref
->refcount_block_index
] = ext2fs_cpu_to_be16(1);
1115 ref
->refcount_block_index
++;
1119 static int sync_refcount(int fd
, struct ext2_qcow2_image
*img
)
1121 struct ext2_qcow2_refcount
*ref
;
1123 ref
= &(img
->refcount
);
1125 ref
->refcount_table
[ref
->refcount_table_index
] =
1126 ext2fs_cpu_to_be64(ref
->refcount_block_offset
);
1127 seek_set(fd
, ref
->refcount_table_offset
);
1128 generic_write(fd
, (char *)ref
->refcount_table
,
1129 ref
->refcount_table_clusters
<< img
->cluster_bits
, NO_BLK
);
1131 seek_set(fd
, ref
->refcount_block_offset
);
1132 generic_write(fd
, (char *)ref
->refcount_block
, img
->cluster_size
,
1137 static void output_qcow2_meta_data_blocks(ext2_filsys fs
, int fd
)
1140 blk64_t blk
, offset
, size
, end
;
1142 struct ext2_qcow2_image
*img
;
1143 unsigned int header_size
;
1145 /* allocate struct ext2_qcow2_image */
1146 retval
= ext2fs_get_mem(sizeof(struct ext2_qcow2_image
), &img
);
1148 com_err(program_name
, retval
, "%s",
1149 _("while allocating ext2_qcow2_image"));
1153 retval
= initialize_qcow2_image(fd
, fs
, img
);
1155 com_err(program_name
, retval
, "%s",
1156 _("while initializing ext2_qcow2_image"));
1159 header_size
= align_offset(sizeof(struct ext2_qcow2_hdr
),
1161 write_header(fd
, img
->hdr
, sizeof(struct ext2_qcow2_hdr
), header_size
);
1163 /* Refcount all qcow2 related metadata up to refcount_block_offset */
1164 end
= img
->refcount
.refcount_block_offset
;
1166 blk
= end
+ img
->cluster_size
;
1167 for (offset
= 0; offset
<= end
; offset
+= img
->cluster_size
) {
1168 if (update_refcount(fd
, img
, offset
, blk
)) {
1169 blk
+= img
->cluster_size
;
1171 * If we create new refcount block, we need to refcount
1174 end
+= img
->cluster_size
;
1177 seek_set(fd
, offset
);
1179 retval
= ext2fs_get_mem(fs
->blocksize
, &buf
);
1181 com_err(program_name
, retval
, "%s",
1182 _("while allocating buffer"));
1185 /* Write qcow2 data blocks */
1186 for (blk
= 0; blk
< ext2fs_blocks_count(fs
->super
); blk
++) {
1187 if ((blk
>= fs
->super
->s_first_data_block
) &&
1188 ext2fs_test_block_bitmap2(meta_block_map
, blk
)) {
1189 retval
= io_channel_read_blk64(fs
->io
, blk
, 1, buf
);
1191 com_err(program_name
, retval
,
1192 _("error reading block %llu"), blk
);
1195 if (scramble_block_map
&&
1196 ext2fs_test_block_bitmap2(scramble_block_map
, blk
))
1197 scramble_dir_block(fs
, blk
, buf
);
1198 if (check_zero_block(buf
, fs
->blocksize
))
1201 if (update_refcount(fd
, img
, offset
, offset
)) {
1202 /* Make space for another refcount block */
1203 offset
+= img
->cluster_size
;
1204 seek_set(fd
, offset
);
1206 * We have created the new refcount block, this
1207 * means that we need to refcount it as well.
1208 * So the previous update_refcount refcounted
1209 * the block itself and now we are going to
1210 * create refcount for data. New refcount
1211 * block should not be created!
1213 if (update_refcount(fd
, img
, offset
, offset
)) {
1214 fprintf(stderr
, "%s",
1215 _("Programming error: multiple "
1216 "sequential refcount blocks "
1222 generic_write(fd
, buf
, fs
->blocksize
, blk
);
1224 if (add_l2_item(img
, blk
, offset
,
1225 offset
+ img
->cluster_size
)) {
1226 offset
+= img
->cluster_size
;
1227 if (update_refcount(fd
, img
, offset
,
1228 offset
+ img
->cluster_size
)) {
1229 offset
+= img
->cluster_size
;
1230 if (update_refcount(fd
, img
, offset
,
1232 fprintf(stderr
, "%s",
1233 _("Programming error: multiple sequential refcount "
1234 "blocks created!\n"));
1238 offset
+= img
->cluster_size
;
1239 seek_set(fd
, offset
);
1243 offset
+= img
->cluster_size
;
1246 update_refcount(fd
, img
, offset
, offset
);
1247 flush_l2_cache(img
);
1248 sync_refcount(fd
, img
);
1251 seek_set(fd
, img
->l1_offset
);
1252 size
= img
->l1_size
* sizeof(__u64
);
1253 generic_write(fd
, (char *)img
->l1_table
, size
, NO_BLK
);
1255 ext2fs_free_mem(&buf
);
1256 free_qcow2_image(img
);
1259 static void write_raw_image_file(ext2_filsys fs
, int fd
, int type
, int flags
)
1261 struct process_block_struct pb
;
1262 struct ext2_inode inode
;
1263 ext2_inode_scan scan
;
1268 meta_blocks_count
= 0;
1269 retval
= ext2fs_allocate_block_bitmap(fs
, _("in-use block map"),
1272 com_err(program_name
, retval
, "%s",
1273 _("while allocating block bitmap"));
1277 if (flags
& E2IMAGE_SCRAMBLE_FLAG
) {
1278 retval
= ext2fs_allocate_block_bitmap(fs
, "scramble block map",
1279 &scramble_block_map
);
1281 com_err(program_name
, retval
, "%s",
1282 _("while allocating scramble block bitmap"));
1287 mark_table_blocks(fs
);
1289 fprintf(stderr
, "%s", _("Scanning inodes...\n"));
1291 retval
= ext2fs_open_inode_scan(fs
, 0, &scan
);
1293 com_err(program_name
, retval
, "%s",
1294 _("while opening inode scan"));
1298 retval
= ext2fs_get_mem(fs
->blocksize
* 3, &block_buf
);
1300 com_err(program_name
, 0, "%s",
1301 _("Can't allocate block buffer"));
1305 use_inode_shortcuts(fs
, 1);
1306 stashed_inode
= &inode
;
1308 retval
= ext2fs_get_next_inode(scan
, &ino
, &inode
);
1309 if (retval
== EXT2_ET_BAD_BLOCK_IN_INODE_TABLE
)
1312 com_err(program_name
, retval
, "%s",
1313 _("while getting next inode"));
1318 if (!inode
.i_links_count
)
1320 if (ext2fs_file_acl_block(fs
, &inode
)) {
1321 ext2fs_mark_block_bitmap2(meta_block_map
,
1322 ext2fs_file_acl_block(fs
, &inode
));
1323 meta_blocks_count
++;
1325 if (!ext2fs_inode_has_valid_blocks2(fs
, &inode
))
1330 pb
.is_dir
= LINUX_S_ISDIR(inode
.i_mode
);
1331 if (LINUX_S_ISDIR(inode
.i_mode
) ||
1332 (LINUX_S_ISLNK(inode
.i_mode
) &&
1333 ext2fs_inode_has_valid_blocks2(fs
, &inode
)) ||
1334 ino
== fs
->super
->s_journal_inum
) {
1335 retval
= ext2fs_block_iterate3(fs
, ino
,
1336 BLOCK_FLAG_READ_ONLY
, block_buf
,
1337 process_dir_block
, &pb
);
1339 com_err(program_name
, retval
,
1340 _("while iterating over inode %u"),
1345 if ((inode
.i_flags
& EXT4_EXTENTS_FL
) ||
1346 inode
.i_block
[EXT2_IND_BLOCK
] ||
1347 inode
.i_block
[EXT2_DIND_BLOCK
] ||
1348 inode
.i_block
[EXT2_TIND_BLOCK
] || all_data
) {
1349 retval
= ext2fs_block_iterate3(fs
,
1350 ino
, BLOCK_FLAG_READ_ONLY
, block_buf
,
1351 process_file_block
, &pb
);
1353 com_err(program_name
, retval
,
1354 _("while iterating over inode %u"), ino
);
1360 use_inode_shortcuts(fs
, 0);
1362 if (type
& E2IMAGE_QCOW2
)
1363 output_qcow2_meta_data_blocks(fs
, fd
);
1365 output_meta_data_blocks(fs
, fd
, flags
);
1367 ext2fs_free_mem(&block_buf
);
1368 ext2fs_close_inode_scan(scan
);
1369 ext2fs_free_block_bitmap(meta_block_map
);
1370 if (type
& E2IMAGE_SCRAMBLE_FLAG
)
1371 ext2fs_free_block_bitmap(scramble_block_map
);
1374 static void install_image(char *device
, char *image_fn
, int type
)
1378 int open_flag
= EXT2_FLAG_IMAGE_FILE
| EXT2_FLAG_64BITS
|
1379 EXT2_FLAG_IGNORE_CSUM_ERRORS
;
1385 com_err(program_name
, 0, "%s",
1386 _("Raw and qcow2 images cannot be installed"));
1390 #ifdef CONFIG_TESTIO_DEBUG
1391 if (getenv("TEST_IO_FLAGS") || getenv("TEST_IO_BLOCK")) {
1392 io_ptr
= test_io_manager
;
1393 test_io_backing_manager
= unix_io_manager
;
1396 io_ptr
= unix_io_manager
;
1398 retval
= ext2fs_open (image_fn
, open_flag
, 0, 0,
1401 com_err(program_name
, retval
, _("while trying to open %s"),
1406 retval
= ext2fs_read_bitmaps (fs
);
1408 com_err(program_name
, retval
, "%s", _("error reading bitmaps"));
1412 fd
= ext2fs_open_file(image_fn
, O_RDONLY
, 0);
1418 retval
= io_ptr
->open(device
, IO_FLAG_RW
, &io
);
1420 com_err(device
, 0, "%s", _("while opening device file"));
1424 ext2fs_rewrite_to_io(fs
, io
);
1426 seek_set(fd
, fs
->image_header
->offset_inode
);
1428 retval
= ext2fs_image_inode_read(fs
, fd
, 0);
1430 com_err(image_fn
, 0, "%s",
1431 _("while restoring the image table"));
1436 ext2fs_close_free(&fs
);
1439 static struct ext2_qcow2_hdr
*check_qcow2_image(int *fd
, char *name
)
1442 *fd
= ext2fs_open_file(name
, O_RDONLY
, 0600);
1446 return qcow2_read_header(*fd
);
1449 int main (int argc
, char ** argv
)
1454 char *image_fn
, offset_opt
[64];
1455 struct ext2_qcow2_hdr
*header
= NULL
;
1456 int open_flag
= EXT2_FLAG_64BITS
| EXT2_FLAG_IGNORE_CSUM_ERRORS
;
1459 int mount_flags
= 0;
1463 int ignore_rw_mount
= 0;
1468 setlocale(LC_MESSAGES
, "");
1469 setlocale(LC_CTYPE
, "");
1470 bindtextdomain(NLS_CAT_NAME
, LOCALEDIR
);
1471 textdomain(NLS_CAT_NAME
);
1472 set_com_err_gettext(gettext
);
1474 fprintf (stderr
, "e2image %s (%s)\n", E2FSPROGS_VERSION
,
1477 program_name
= *argv
;
1478 add_error_table(&et_ext2_error_table
);
1479 while ((c
= getopt(argc
, argv
, "nrsIQafo:O:pc")) != EOF
)
1482 flags
|= E2IMAGE_INSTALL_FLAG
;
1487 img_type
|= E2IMAGE_QCOW2
;
1492 img_type
|= E2IMAGE_RAW
;
1495 flags
|= E2IMAGE_SCRAMBLE_FLAG
;
1501 ignore_rw_mount
= 1;
1507 source_offset
= strtoull(optarg
, NULL
, 0);
1510 dest_offset
= strtoull(optarg
, NULL
, 0);
1521 if (optind
== argc
- 1 &&
1522 (source_offset
|| dest_offset
))
1524 else if (optind
!= argc
- 2 )
1527 if (all_data
&& !img_type
) {
1528 com_err(program_name
, 0, "%s", _("-a option can only be used "
1529 "with raw or QCOW2 images."));
1532 if ((source_offset
|| dest_offset
) && img_type
!= E2IMAGE_RAW
) {
1533 com_err(program_name
, 0, "%s",
1534 _("Offsets are only allowed with raw images."));
1537 if (move_mode
&& img_type
!= E2IMAGE_RAW
) {
1538 com_err(program_name
, 0, "%s",
1539 _("Move mode is only allowed with raw images."));
1542 if (move_mode
&& !all_data
) {
1543 com_err(program_name
, 0, "%s",
1544 _("Move mode requires all data mode."));
1547 device_name
= argv
[optind
];
1549 image_fn
= device_name
;
1550 else image_fn
= argv
[optind
+1];
1552 retval
= ext2fs_check_if_mounted(device_name
, &mount_flags
);
1554 com_err(program_name
, retval
, "%s", _("checking if mounted"));
1558 if (img_type
&& !ignore_rw_mount
&&
1559 (mount_flags
& EXT2_MF_MOUNTED
) &&
1560 !(mount_flags
& EXT2_MF_READONLY
)) {
1561 fprintf(stderr
, "%s", _("\nRunning e2image on a R/W mounted "
1562 "filesystem can result in an\n"
1563 "inconsistent image which will not be useful "
1564 "for debugging purposes.\n"
1565 "Use -f option if you really want to do that.\n"));
1569 if (flags
& E2IMAGE_INSTALL_FLAG
) {
1570 install_image(device_name
, image_fn
, img_type
);
1574 if (img_type
& E2IMAGE_RAW
) {
1575 header
= check_qcow2_image(&qcow2_fd
, device_name
);
1577 flags
|= E2IMAGE_IS_QCOW2_FLAG
;
1581 sprintf(offset_opt
, "offset=%llu", source_offset
);
1582 retval
= ext2fs_open2(device_name
, offset_opt
, open_flag
, 0, 0,
1583 unix_io_manager
, &fs
);
1585 com_err (program_name
, retval
, _("while trying to open %s"),
1587 fputs(_("Couldn't find valid filesystem superblock.\n"), stdout
);
1588 if (retval
== EXT2_ET_BAD_MAGIC
)
1589 check_plausibility(device_name
, CHECK_FS_EXIST
, NULL
);
1594 if (strcmp(image_fn
, "-") == 0)
1597 int o_flags
= O_CREAT
|O_RDWR
;
1599 if (img_type
!= E2IMAGE_RAW
)
1601 if (access(image_fn
, F_OK
) != 0)
1602 flags
|= E2IMAGE_CHECK_ZERO_FLAG
;
1603 fd
= ext2fs_open_file(image_fn
, o_flags
, 0600);
1605 com_err(program_name
, errno
,
1606 _("while trying to open %s"), image_fn
);
1611 seek_set(fd
, dest_offset
);
1613 if ((img_type
& E2IMAGE_QCOW2
) && (fd
== 1)) {
1614 com_err(program_name
, 0, "%s",
1615 _("QCOW2 image can not be written to the stdout!\n"));
1619 if (fstat(fd
, &st
)) {
1620 com_err(program_name
, 0, "%s",
1621 _("Can not stat output\n"));
1624 if (ext2fsP_is_disk_device(st
.st_mode
))
1627 if (flags
& E2IMAGE_IS_QCOW2_FLAG
) {
1628 ret
= qcow2_write_raw_image(qcow2_fd
, fd
, header
);
1630 if (ret
== -QCOW_COMPRESSED
)
1631 fprintf(stderr
, _("Image (%s) is compressed\n"),
1633 if (ret
== -QCOW_ENCRYPTED
)
1634 fprintf(stderr
, _("Image (%s) is encrypted\n"),
1636 com_err(program_name
, ret
,
1637 _("while trying to convert qcow2 image"
1638 " (%s) into raw image (%s)"),
1639 device_name
, image_fn
);
1645 if (img_type
!= E2IMAGE_RAW
) {
1646 fprintf(stderr
, "%s", _("The -c option only supported "
1651 fprintf(stderr
, "%s", _("The -c option not supported "
1652 "when writing to stdout\n"));
1655 retval
= ext2fs_get_mem(fs
->blocksize
, &check_buf
);
1657 com_err(program_name
, retval
, "%s",
1658 _("while allocating check_buf"));
1662 if (show_progress
&& (img_type
!= E2IMAGE_RAW
)) {
1663 fprintf(stderr
, "%s",
1664 _("The -p option only supported in raw mode\n"));
1668 write_raw_image_file(fs
, fd
, img_type
, flags
);
1670 write_image_file(fs
, fd
);
1672 ext2fs_close_free(&fs
);
1674 printf(_("%d blocks already contained the data to be copied\n"),
1682 remove_error_table(&et_ext2_error_table
);