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 #define _LARGEFILE_SOURCE
14 #define _LARGEFILE64_SOURCE
33 #include <sys/types.h>
35 #include "ext2fs/ext2_fs.h"
36 #include "ext2fs/ext2fs.h"
37 #include "et/com_err.h"
38 #include "uuid/uuid.h"
40 #include "ext2fs/e2image.h"
42 #include "../version.h"
43 #include "nls-enable.h"
45 const char * program_name = "e2image";
46 char * device_name = NULL;
48 static void usage(void)
50 fprintf(stderr, _("Usage: %s [-rsI] device image_file\n"),
55 static void write_header(int fd, struct ext2_image_hdr *hdr, int blocksize)
60 header_buf = malloc(blocksize);
62 fputs(_("Couldn't allocate header buffer\n"), stderr);
66 if (lseek(fd, 0, SEEK_SET) < 0) {
67 perror("lseek while writing header");
70 memset(header_buf, 0, blocksize);
73 memcpy(header_buf, hdr, sizeof(struct ext2_image_hdr));
75 actual = write(fd, header_buf, blocksize);
77 perror("write header");
80 if (actual != blocksize) {
81 fprintf(stderr, _("short write (only %d bytes) for "
82 "writing image header"), actual);
88 static void write_image_file(ext2_filsys fs, int fd)
90 struct ext2_image_hdr hdr;
94 write_header(fd, NULL, fs->blocksize);
95 memset(&hdr, 0, sizeof(struct ext2_image_hdr));
97 hdr.offset_super = lseek(fd, 0, SEEK_CUR);
98 retval = ext2fs_image_super_write(fs, fd, 0);
100 com_err(program_name, retval, _("while writing superblock"));
104 hdr.offset_inode = lseek(fd, 0, SEEK_CUR);
105 retval = ext2fs_image_inode_write(fs, fd,
106 (fd != 1) ? IMAGER_FLAG_SPARSEWRITE : 0);
108 com_err(program_name, retval, _("while writing inode table"));
112 hdr.offset_blockmap = lseek(fd, 0, SEEK_CUR);
113 retval = ext2fs_image_bitmap_write(fs, fd, 0);
115 com_err(program_name, retval, _("while writing block bitmap"));
119 hdr.offset_inodemap = lseek(fd, 0, SEEK_CUR);
120 retval = ext2fs_image_bitmap_write(fs, fd, IMAGER_FLAG_INODEMAP);
122 com_err(program_name, retval, _("while writing inode bitmap"));
126 hdr.magic_number = EXT2_ET_MAGIC_E2IMAGE;
127 strcpy(hdr.magic_descriptor, "Ext2 Image 1.0");
128 gethostname(hdr.fs_hostname, sizeof(hdr.fs_hostname));
129 strncpy(hdr.fs_device_name, device_name, sizeof(hdr.fs_device_name)-1);
130 hdr.fs_device_name[sizeof(hdr.fs_device_name) - 1] = 0;
131 hdr.fs_blocksize = fs->blocksize;
133 if (stat(device_name, &st) == 0)
134 hdr.fs_device = st.st_rdev;
136 if (fstat(fd, &st) == 0) {
137 hdr.image_device = st.st_dev;
138 hdr.image_inode = st.st_ino;
140 memcpy(hdr.fs_uuid, fs->super->s_uuid, sizeof(hdr.fs_uuid));
142 hdr.image_time = time(0);
143 write_header(fd, &hdr, fs->blocksize);
147 * These set of functions are used to write a RAW image file.
149 ext2fs_block_bitmap meta_block_map;
150 ext2fs_block_bitmap scramble_block_map; /* Directory blocks to be scrambled */
152 struct process_block_struct {
158 * These subroutines short circuits ext2fs_get_blocks and
159 * ext2fs_check_directory; we use them since we already have the inode
160 * structure, so there's no point in letting the ext2fs library read
163 static ino_t stashed_ino = 0;
164 static struct ext2_inode *stashed_inode;
166 static errcode_t meta_get_blocks(ext2_filsys fs EXT2FS_ATTR((unused)),
172 if ((ino != stashed_ino) || !stashed_inode)
173 return EXT2_ET_CALLBACK_NOTHANDLED;
175 for (i=0; i < EXT2_N_BLOCKS; i++)
176 blocks[i] = stashed_inode->i_block[i];
180 static errcode_t meta_check_directory(ext2_filsys fs EXT2FS_ATTR((unused)),
183 if ((ino != stashed_ino) || !stashed_inode)
184 return EXT2_ET_CALLBACK_NOTHANDLED;
186 if (!LINUX_S_ISDIR(stashed_inode->i_mode))
187 return EXT2_ET_NO_DIRECTORY;
191 static errcode_t meta_read_inode(ext2_filsys fs EXT2FS_ATTR((unused)),
193 struct ext2_inode *inode)
195 if ((ino != stashed_ino) || !stashed_inode)
196 return EXT2_ET_CALLBACK_NOTHANDLED;
197 *inode = *stashed_inode;
201 static void use_inode_shortcuts(ext2_filsys fs, int bool)
204 fs->get_blocks = meta_get_blocks;
205 fs->check_directory = meta_check_directory;
206 fs->read_inode = meta_read_inode;
210 fs->check_directory = 0;
215 static int process_dir_block(ext2_filsys fs EXT2FS_ATTR((unused)),
217 e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
218 blk_t ref_block EXT2FS_ATTR((unused)),
219 int ref_offset EXT2FS_ATTR((unused)),
220 void *priv_data EXT2FS_ATTR((unused)))
222 struct process_block_struct *p;
224 p = (struct process_block_struct *) priv_data;
226 ext2fs_mark_block_bitmap(meta_block_map, *block_nr);
227 if (scramble_block_map && p->is_dir && blockcnt >= 0)
228 ext2fs_mark_block_bitmap(scramble_block_map, *block_nr);
232 static int process_file_block(ext2_filsys fs EXT2FS_ATTR((unused)),
234 e2_blkcnt_t blockcnt,
235 blk_t ref_block EXT2FS_ATTR((unused)),
236 int ref_offset EXT2FS_ATTR((unused)),
237 void *priv_data EXT2FS_ATTR((unused)))
240 ext2fs_mark_block_bitmap(meta_block_map, *block_nr);
245 static void mark_table_blocks(ext2_filsys fs)
247 blk_t first_block, b;
250 first_block = fs->super->s_first_data_block;
252 * Mark primary superblock
254 ext2fs_mark_block_bitmap(meta_block_map, first_block);
257 * Mark the primary superblock descriptors
259 for (j = 0; j < fs->desc_blocks; j++) {
260 ext2fs_mark_block_bitmap(meta_block_map,
261 ext2fs_descriptor_block_loc(fs, first_block, j));
264 for (i = 0; i < fs->group_desc_count; i++) {
266 * Mark the blocks used for the inode table
268 if (fs->group_desc[i].bg_inode_table) {
269 for (j = 0, b = fs->group_desc[i].bg_inode_table;
270 j < (unsigned) fs->inode_blocks_per_group;
272 ext2fs_mark_block_bitmap(meta_block_map, b);
276 * Mark block used for the block bitmap
278 if (fs->group_desc[i].bg_block_bitmap) {
279 ext2fs_mark_block_bitmap(meta_block_map,
280 fs->group_desc[i].bg_block_bitmap);
284 * Mark block used for the inode bitmap
286 if (fs->group_desc[i].bg_inode_bitmap) {
287 ext2fs_mark_block_bitmap(meta_block_map,
288 fs->group_desc[i].bg_inode_bitmap);
294 * This function returns 1 if the specified block is all zeros
296 static int check_zero_block(char *buf, int blocksize)
299 int left = blocksize;
309 static void write_block(int fd, char *buf, int sparse_offset,
310 int blocksize, blk_t block)
317 if (lseek64(fd, sparse_offset, SEEK_CUR) < 0)
320 if (lseek(fd, sparse_offset, SEEK_CUR) < 0)
325 count = write(fd, buf, blocksize);
326 if (count != blocksize) {
331 com_err(program_name, err, "error writing block %u",
340 static void scramble_dir_block(ext2_filsys fs, blk_t blk, char *buf)
343 struct ext2_dir_entry_2 *dirent;
344 int rec_len, id, len;
346 end = buf + fs->blocksize;
347 for (p = buf; p < end-8; p += rec_len) {
348 dirent = (struct ext2_dir_entry_2 *) p;
349 rec_len = dirent->rec_len;
350 #ifdef EXT2FS_ENABLE_SWAPFS
351 if (fs->flags & EXT2_FLAG_SWAP_BYTES)
352 rec_len = ext2fs_swab16(rec_len);
355 printf("rec_len = %d, name_len = %d\n", rec_len, dirent->name_len);
357 if (rec_len < 8 || (rec_len % 4) ||
359 printf("Corrupt directory block %lu: "
360 "bad rec_len (%d)\n", (unsigned long) blk,
363 #ifdef EXT2FS_ENABLE_SWAPFS
364 if (fs->flags & EXT2_FLAG_SWAP_BYTES)
365 dirent->rec_len = ext2fs_swab16(rec_len);
369 if (dirent->name_len + 8 > rec_len) {
370 printf("Corrupt directory block %lu: "
371 "bad name_len (%d)\n", (unsigned long) blk,
373 dirent->name_len = rec_len - 8;
377 len = rec_len - dirent->name_len - 8;
379 memset(cp+dirent->name_len, 0, len);
380 if (dirent->name_len==1 && cp[0] == '.')
382 if (dirent->name_len==2 && cp[0] == '.' && cp[1] == '.')
385 memset(cp, 'A', dirent->name_len);
386 len = dirent->name_len;
388 while ((len > 0) && (id > 0)) {
397 static void output_meta_data_blocks(ext2_filsys fs, int fd)
401 char *buf, *zero_buf;
404 buf = malloc(fs->blocksize);
406 com_err(program_name, ENOMEM, "while allocating buffer");
409 zero_buf = malloc(fs->blocksize);
411 com_err(program_name, ENOMEM, "while allocating buffer");
414 memset(zero_buf, 0, fs->blocksize);
415 for (blk = 0; blk < fs->super->s_blocks_count; blk++) {
416 if ((blk >= fs->super->s_first_data_block) &&
417 ext2fs_test_block_bitmap(meta_block_map, blk)) {
418 retval = io_channel_read_blk(fs->io, blk, 1, buf);
420 com_err(program_name, retval,
421 "error reading block %u", blk);
423 if (scramble_block_map &&
424 ext2fs_test_block_bitmap(scramble_block_map, blk))
425 scramble_dir_block(fs, blk, buf);
426 if ((fd != 1) && check_zero_block(buf, fs->blocksize))
428 write_block(fd, buf, sparse, fs->blocksize, blk);
433 write_block(fd, zero_buf, 0,
437 sparse += fs->blocksize;
438 if (sparse >= 1024*1024) {
439 write_block(fd, 0, sparse, 0, 0);
445 write_block(fd, zero_buf, sparse-1, 1, -1);
450 static void write_raw_image_file(ext2_filsys fs, int fd, int scramble_flag)
452 struct process_block_struct pb;
453 struct ext2_inode inode;
454 ext2_inode_scan scan;
459 retval = ext2fs_allocate_block_bitmap(fs, "in-use block map",
462 com_err(program_name, retval, "while allocating block bitmap");
467 retval = ext2fs_allocate_block_bitmap(fs, "scramble block map",
468 &scramble_block_map);
470 com_err(program_name, retval,
471 "while allocating scramble block bitmap");
476 mark_table_blocks(fs);
478 retval = ext2fs_open_inode_scan(fs, 0, &scan);
480 com_err(program_name, retval, _("while opening inode scan"));
484 block_buf = malloc(fs->blocksize * 3);
486 com_err(program_name, 0, "Can't allocate block buffer");
490 use_inode_shortcuts(fs, 1);
491 stashed_inode = &inode;
493 retval = ext2fs_get_next_inode(scan, &ino, &inode);
494 if (retval == EXT2_ET_BAD_BLOCK_IN_INODE_TABLE)
497 com_err(program_name, retval,
498 _("while getting next inode"));
503 if (!inode.i_links_count)
505 if (inode.i_file_acl) {
506 ext2fs_mark_block_bitmap(meta_block_map,
509 if (!ext2fs_inode_has_valid_blocks(&inode))
514 pb.is_dir = LINUX_S_ISDIR(inode.i_mode);
515 if (LINUX_S_ISDIR(inode.i_mode) ||
516 (LINUX_S_ISLNK(inode.i_mode) &&
517 ext2fs_inode_has_valid_blocks(&inode)) ||
518 ino == fs->super->s_journal_inum) {
519 retval = ext2fs_block_iterate2(fs, ino, 0,
520 block_buf, process_dir_block, &pb);
522 com_err(program_name, retval,
523 "while iterating over inode %u",
528 if (inode.i_block[EXT2_IND_BLOCK] ||
529 inode.i_block[EXT2_DIND_BLOCK] ||
530 inode.i_block[EXT2_TIND_BLOCK]) {
531 retval = ext2fs_block_iterate2(fs,
533 process_file_block, &pb);
535 com_err(program_name, retval,
536 "while iterating over inode %u", ino);
542 use_inode_shortcuts(fs, 0);
543 output_meta_data_blocks(fs, fd);
547 static void install_image(char *device, char *image_fn, int raw_flag)
551 int open_flag = EXT2_FLAG_IMAGE_FILE;
554 io_channel io, image_io;
557 com_err(program_name, 0, "Raw images cannot be installed");
561 #ifdef CONFIG_TESTIO_DEBUG
562 io_ptr = test_io_manager;
563 test_io_backing_manager = unix_io_manager;
565 io_ptr = unix_io_manager;
568 retval = ext2fs_open (image_fn, open_flag, 0, 0,
571 com_err (program_name, retval, _("while trying to open %s"),
576 retval = ext2fs_read_bitmaps (fs);
578 com_err(program_name, retval, "error reading bitmaps");
583 fd = open64(image_fn, O_RDONLY);
585 fd = open(image_fn, O_RDONLY);
592 retval = io_ptr->open(device, IO_FLAG_RW, &io);
594 com_err(device, 0, "while opening device file");
600 ext2fs_rewrite_to_io(fs, io);
602 if (lseek(fd, fs->image_header->offset_inode, SEEK_SET) < 0) {
607 retval = ext2fs_image_inode_read(fs, fd, 0);
609 com_err(image_fn, 0, "while restoring the image table");
617 int main (int argc, char ** argv)
625 int install_flag = 0;
626 int scramble_flag = 0;
630 setlocale(LC_MESSAGES, "");
631 setlocale(LC_CTYPE, "");
632 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
633 textdomain(NLS_CAT_NAME);
635 fprintf (stderr, "e2image %s (%s)\n", E2FSPROGS_VERSION,
638 program_name = *argv;
639 add_error_table(&et_ext2_error_table);
640 while ((c = getopt (argc, argv, "rsI")) != EOF)
654 if (optind != argc - 2 )
656 device_name = argv[optind];
657 image_fn = argv[optind+1];
660 install_image(device_name, image_fn, raw_flag);
664 retval = ext2fs_open (device_name, open_flag, 0, 0,
665 unix_io_manager, &fs);
667 com_err (program_name, retval, _("while trying to open %s"),
669 fputs(_("Couldn't find valid filesystem superblock.\n"), stdout);
673 if (strcmp(image_fn, "-") == 0)
677 fd = open64(image_fn, O_CREAT|O_TRUNC|O_WRONLY, 0600);
679 fd = open(image_fn, O_CREAT|O_TRUNC|O_WRONLY, 0600);
682 com_err(program_name, errno,
683 _("while trying to open %s"), argv[optind+1]);
689 write_raw_image_file(fs, fd, scramble_flag);
691 write_image_file(fs, fd);
694 remove_error_table(&et_ext2_error_table);