Whamcloud - gitweb
dupfs.c (ext2fs_dup_handle): Make sure the new filesystem handle
[tools/e2fsprogs.git] / misc / e2image.c
1 /*
2  * e2image.c --- Program which writes an image file backing up
3  * critical metadata for the filesystem.
4  *
5  * Copyright 2000, 2001 by Theodore Ts'o.
6  *
7  * %Begin-Header%
8  * This file may be redistributed under the terms of the GNU Public
9  * License.
10  * %End-Header%
11  */
12
13 #define _LARGEFILE_SOURCE
14 #define _LARGEFILE64_SOURCE
15
16 #include <fcntl.h>
17 #include <grp.h>
18 #ifdef HAVE_GETOPT_H
19 #include <getopt.h>
20 #else
21 extern char *optarg;
22 extern int optind;
23 #endif
24 #include <pwd.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <time.h>
29 #include <unistd.h>
30 #include <fcntl.h>
31 #include <errno.h>
32 #include <sys/stat.h>
33 #include <sys/types.h>
34
35 #include "ext2fs/ext2_fs.h"
36 #include "ext2fs/ext2fs.h"
37 #include "et/com_err.h"
38 #include "uuid/uuid.h"
39 #include "e2p/e2p.h"
40 #include "ext2fs/e2image.h"
41
42 #include "../version.h"
43 #include "nls-enable.h"
44
45 const char * program_name = "e2image";
46 char * device_name = NULL;
47
48 static void usage(void)
49 {
50         fprintf(stderr, _("Usage: %s [-r] device image_file\n"), program_name);
51         exit (1);
52 }
53
54 static void write_header(int fd, struct ext2_image_hdr *hdr, int blocksize)
55 {
56         char *header_buf;
57         int actual;
58
59         header_buf = malloc(blocksize);
60         if (!header_buf) {
61                 fputs(_("Couldn't allocate header buffer\n"), stderr);
62                 exit(1);
63         }
64
65         if (lseek(fd, 0, SEEK_SET) < 0) {
66                 perror("lseek while writing header");
67                 exit(1);
68         }
69         memset(header_buf, 0, blocksize);
70         
71         if (hdr)
72                 memcpy(header_buf, hdr, sizeof(struct ext2_image_hdr));
73         
74         actual = write(fd, header_buf, blocksize);
75         if (actual < 0) {
76                 perror("write header");
77                 exit(1);
78         }
79         if (actual != blocksize) {
80                 fprintf(stderr, _("short write (only %d bytes) for "
81                                   "writing image header"), actual);
82                 exit(1);
83         }
84         free(header_buf);
85 }
86
87 static void write_image_file(ext2_filsys fs, int fd)
88 {
89         struct ext2_image_hdr   hdr;
90         struct stat             st;
91         errcode_t               retval;
92
93         write_header(fd, NULL, fs->blocksize);
94         memset(&hdr, 0, sizeof(struct ext2_image_hdr));
95
96         hdr.offset_super = lseek(fd, 0, SEEK_CUR);
97         retval = ext2fs_image_super_write(fs, fd, 0);
98         if (retval) {
99                 com_err(program_name, retval, _("while writing superblock"));
100                 exit(1);
101         }
102         
103         hdr.offset_inode = lseek(fd, 0, SEEK_CUR);
104         retval = ext2fs_image_inode_write(fs, fd,
105                                   (fd != 1) ? IMAGER_FLAG_SPARSEWRITE : 0);
106         if (retval) {
107                 com_err(program_name, retval, _("while writing inode table"));
108                 exit(1);
109         }
110         
111         hdr.offset_blockmap = lseek(fd, 0, SEEK_CUR);
112         retval = ext2fs_image_bitmap_write(fs, fd, 0);
113         if (retval) {
114                 com_err(program_name, retval, _("while writing block bitmap"));
115                 exit(1);
116         }
117
118         hdr.offset_inodemap = lseek(fd, 0, SEEK_CUR);
119         retval = ext2fs_image_bitmap_write(fs, fd, IMAGER_FLAG_INODEMAP);
120         if (retval) {
121                 com_err(program_name, retval, _("while writing inode bitmap"));
122                 exit(1);
123         }
124
125         hdr.magic_number = EXT2_ET_MAGIC_E2IMAGE;
126         strcpy(hdr.magic_descriptor, "Ext2 Image 1.0");
127         gethostname(hdr.fs_hostname, sizeof(hdr.fs_hostname));
128         strncat(hdr.fs_device_name, device_name, sizeof(hdr.fs_device_name));
129         hdr.fs_device_name[sizeof(hdr.fs_device_name) - 1] = 0;
130         hdr.fs_blocksize = fs->blocksize;
131         
132         if (stat(device_name, &st) == 0)
133                 hdr.fs_device = st.st_rdev;
134
135         if (fstat(fd, &st) == 0) {
136                 hdr.image_device = st.st_dev;
137                 hdr.image_inode = st.st_ino;
138         }
139         memcpy(hdr.fs_uuid, fs->super->s_uuid, sizeof(hdr.fs_uuid));
140
141         hdr.image_time = time(0);
142         write_header(fd, &hdr, fs->blocksize);
143 }
144
145 /*
146  * These set of functions are used to write a RAW image file.
147  */
148 ext2fs_block_bitmap meta_block_map;
149
150 struct process_block_struct {
151         ext2_ino_t      ino;
152 };
153
154 /*
155  * These subroutines short circuits ext2fs_get_blocks and
156  * ext2fs_check_directory; we use them since we already have the inode
157  * structure, so there's no point in letting the ext2fs library read
158  * the inode again.
159  */
160 static ino_t stashed_ino = 0;
161 static struct ext2_inode *stashed_inode;
162
163 static errcode_t meta_get_blocks(ext2_filsys fs EXT2FS_ATTR((unused)), 
164                                  ext2_ino_t ino,
165                                  blk_t *blocks)
166 {
167         int     i;
168         
169         if ((ino != stashed_ino) || !stashed_inode)
170                 return EXT2_ET_CALLBACK_NOTHANDLED;
171
172         for (i=0; i < EXT2_N_BLOCKS; i++)
173                 blocks[i] = stashed_inode->i_block[i];
174         return 0;
175 }
176
177 static errcode_t meta_check_directory(ext2_filsys fs EXT2FS_ATTR((unused)), 
178                                       ext2_ino_t ino)
179 {
180         if ((ino != stashed_ino) || !stashed_inode)
181                 return EXT2_ET_CALLBACK_NOTHANDLED;
182
183         if (!LINUX_S_ISDIR(stashed_inode->i_mode))
184                 return EXT2_ET_NO_DIRECTORY;
185         return 0;
186 }
187
188 static errcode_t meta_read_inode(ext2_filsys fs EXT2FS_ATTR((unused)), 
189                                  ext2_ino_t ino,
190                                  struct ext2_inode *inode)
191 {
192         if ((ino != stashed_ino) || !stashed_inode)
193                 return EXT2_ET_CALLBACK_NOTHANDLED;
194         *inode = *stashed_inode;
195         return 0;
196 }
197
198 static void use_inode_shortcuts(ext2_filsys fs, int bool)
199 {
200         if (bool) {
201                 fs->get_blocks = meta_get_blocks;
202                 fs->check_directory = meta_check_directory;
203                 fs->read_inode = meta_read_inode;
204                 stashed_ino = 0;
205         } else {
206                 fs->get_blocks = 0;
207                 fs->check_directory = 0;
208                 fs->read_inode = 0;
209         }
210 }
211
212 static int process_dir_block(ext2_filsys fs EXT2FS_ATTR((unused)), 
213                              blk_t *block_nr,
214                              e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)), 
215                              blk_t ref_block EXT2FS_ATTR((unused)),
216                              int ref_offset EXT2FS_ATTR((unused)), 
217                              void *priv_data EXT2FS_ATTR((unused)))
218 {
219         ext2fs_mark_block_bitmap(meta_block_map, *block_nr);
220         return 0;
221 }
222
223 static int process_file_block(ext2_filsys fs EXT2FS_ATTR((unused)), 
224                               blk_t *block_nr,
225                               e2_blkcnt_t blockcnt, 
226                               blk_t ref_block EXT2FS_ATTR((unused)),
227                               int ref_offset EXT2FS_ATTR((unused)), 
228                               void *priv_data EXT2FS_ATTR((unused)))
229 {
230         if (blockcnt < 0) {
231                 ext2fs_mark_block_bitmap(meta_block_map, *block_nr);
232         }
233         return 0;
234 }
235
236 static void mark_table_blocks(ext2_filsys fs)
237 {
238         blk_t   block, b;
239         unsigned int    i,j;
240         
241         block = fs->super->s_first_data_block;
242         /*
243          * Mark primary superblock
244          */
245         ext2fs_mark_block_bitmap(meta_block_map, block);
246                         
247         /*
248          * Mark the primary superblock descriptors
249          */
250         for (j = 0; j < fs->desc_blocks; j++) {
251                 ext2fs_mark_block_bitmap(meta_block_map,
252                          ext2fs_descriptor_block_loc(fs, block, j));
253         }
254
255         for (i = 0; i < fs->group_desc_count; i++) {
256                 /*
257                  * Mark the blocks used for the inode table
258                  */
259                 if (fs->group_desc[i].bg_inode_table) {
260                         for (j = 0, b = fs->group_desc[i].bg_inode_table;
261                              j < (unsigned) fs->inode_blocks_per_group;
262                              j++, b++)
263                                 ext2fs_mark_block_bitmap(meta_block_map, b);
264                 }
265                             
266                 /*
267                  * Mark block used for the block bitmap 
268                  */
269                 if (fs->group_desc[i].bg_block_bitmap) {
270                         ext2fs_mark_block_bitmap(meta_block_map,
271                                      fs->group_desc[i].bg_block_bitmap);
272                 }
273                 
274                 /*
275                  * Mark block used for the inode bitmap 
276                  */
277                 if (fs->group_desc[i].bg_inode_bitmap) {
278                         ext2fs_mark_block_bitmap(meta_block_map,
279                                  fs->group_desc[i].bg_inode_bitmap);
280                 }
281                 block += fs->super->s_blocks_per_group;
282         }
283 }
284
285 /*
286  * This function returns 1 if the specified block is all zeros
287  */
288 static int check_zero_block(char *buf, int blocksize)
289 {
290         char    *cp = buf;
291         int     left = blocksize;
292
293         while (left > 0) {
294                 if (*cp++)
295                         return 0;
296                 left--;
297         }
298         return 1;
299 }
300
301 static void write_block(int fd, char *buf, int sparse_offset,
302                         int blocksize, blk_t block)
303 {
304         int             count;
305         errcode_t       err;
306
307         if (sparse_offset) {
308 #ifdef HAVE_LSEEK64
309                 if (lseek64(fd, sparse_offset, SEEK_CUR) < 0)
310                         perror("lseek");
311 #else
312                 if (lseek(fd, sparse_offset, SEEK_CUR) < 0)
313                         perror("lseek");
314 #endif
315         }
316         if (blocksize) {
317                 count = write(fd, buf, blocksize);
318                 if (count != blocksize) {
319                         if (count == -1)
320                                 err = errno;
321                         else
322                                 err = 0;
323                         com_err(program_name, err, "error writing block %d", 
324                                 block);
325                 }
326         }
327 }
328
329 static void output_meta_data_blocks(ext2_filsys fs, int fd)
330 {
331         errcode_t       retval;
332         blk_t           blk;
333         char            buf[8192], zero_buf[8192];
334         int             sparse = 0;
335
336         memset(zero_buf, 0, sizeof(zero_buf));
337         for (blk = 0; blk < fs->super->s_blocks_count; blk++) {
338                 if ((blk >= fs->super->s_first_data_block) &&
339                     ext2fs_test_block_bitmap(meta_block_map, blk)) {
340                         retval = io_channel_read_blk(fs->io, blk, 1, buf);
341                         if (retval) {
342                                 com_err(program_name, retval,
343                                         "error reading block %d", blk);
344                         }
345                         if ((fd != 1) && check_zero_block(buf, fs->blocksize))
346                                 goto sparse_write;
347                         write_block(fd, buf, sparse, fs->blocksize, blk);
348                         sparse = 0;
349                 } else {
350                 sparse_write:
351                         if (fd == 1) {
352                                 write_block(fd, zero_buf, 0,
353                                             fs->blocksize, blk);
354                                 continue;
355                         }
356                         sparse += fs->blocksize;
357                         if (sparse >= 1024*1024) {
358                                 write_block(fd, 0, sparse, 0, 0);
359                                 sparse = 0;
360                         }
361                 }
362         }
363         write_block(fd, zero_buf, sparse, 1, -1);
364 }
365
366 static void write_raw_image_file(ext2_filsys fs, int fd)
367 {
368         struct process_block_struct     pb;
369         struct ext2_inode               inode;
370         ext2_inode_scan                 scan;
371         ext2_ino_t                      ino;
372         errcode_t                       retval;
373         char *                          block_buf;
374         
375         retval = ext2fs_allocate_block_bitmap(fs, "in-use block map",
376                                               &meta_block_map);
377         if (retval) {
378                 com_err(program_name, retval, "while allocating block bitmap");
379                 exit(1);
380         }
381         
382         mark_table_blocks(fs);
383
384         retval = ext2fs_open_inode_scan(fs, 0, &scan);
385         if (retval) {
386                 com_err(program_name, retval, _("while opening inode scan"));
387                 exit(1);
388         }
389
390         block_buf = malloc(fs->blocksize * 3);
391         if (!block_buf) {
392                 com_err(program_name, 0, "Can't allocate block buffer");
393                 exit(1);
394         }
395         
396         use_inode_shortcuts(fs, 1);
397         stashed_inode = &inode;
398         while (1) {
399                 retval = ext2fs_get_next_inode(scan, &ino, &inode);
400                 if (retval == EXT2_ET_BAD_BLOCK_IN_INODE_TABLE)
401                         continue;
402                 if (retval) {
403                         com_err(program_name, retval,
404                                 _("while getting next inode"));
405                         exit(1);
406                 }
407                 if (ino == 0)
408                         break;
409                 if (!inode.i_links_count)
410                         continue;
411                 if (inode.i_file_acl) {
412                         ext2fs_mark_block_bitmap(meta_block_map,
413                                                  inode.i_file_acl);
414                 }
415                 if (!ext2fs_inode_has_valid_blocks(&inode))
416                         continue;
417                 
418                 stashed_ino = ino;
419                 if (LINUX_S_ISDIR(inode.i_mode) ||
420                     (LINUX_S_ISLNK(inode.i_mode) &&
421                      ext2fs_inode_has_valid_blocks(&inode)) ||
422                     ino == fs->super->s_journal_inum) {
423                         retval = ext2fs_block_iterate2(fs, ino, 0, 
424                                        block_buf, process_dir_block, &pb);
425                         if (retval) {
426                                 com_err(program_name, retval,
427                                         "while iterating over inode %d", 
428                                         ino);
429                                 exit(1);
430                         }
431                 } else {
432                         if (inode.i_block[EXT2_IND_BLOCK] ||
433                             inode.i_block[EXT2_DIND_BLOCK] ||
434                             inode.i_block[EXT2_TIND_BLOCK]) {
435                                 retval = ext2fs_block_iterate2(fs,
436                                        ino, 0, block_buf,
437                                        process_file_block, &pb);
438                                 if (retval) {
439                                         com_err(program_name, retval,
440                                         "while iterating over %d", ino);
441                                         exit(1);
442                                 }
443                         }
444                 }
445         }
446         use_inode_shortcuts(fs, 0);
447         output_meta_data_blocks(fs, fd);
448 }
449
450 static void install_image(char *device, char *image_fn, int raw_flag)
451 {
452         errcode_t retval;
453         ext2_filsys fs;
454         int open_flag = EXT2_FLAG_IMAGE_FILE;
455         int fd = 0;
456         io_manager      io_ptr;
457         io_channel      io, image_io;
458
459         if (raw_flag) {
460                 com_err(program_name, 0, "Raw images cannot be installed");
461                 exit(1);
462         }
463         
464 #ifdef CONFIG_TESTIO_DEBUG
465         io_ptr = test_io_manager;
466         test_io_backing_manager = unix_io_manager;
467 #else
468         io_ptr = unix_io_manager;
469 #endif
470
471         retval = ext2fs_open (image_fn, open_flag, 0, 0,
472                               io_ptr, &fs);
473         if (retval) {
474                 com_err (program_name, retval, _("while trying to open %s"),
475                          image_fn);
476                 exit(1);
477         }
478
479         retval = ext2fs_read_bitmaps (fs);
480         if (retval) {
481                 com_err(program_name, retval, "error reading bitmaps");
482                 exit(1);
483         }
484
485
486         fd = open(image_fn, O_RDONLY);
487         if (fd < 0) {
488                 perror(image_fn);
489                 exit(1);
490         }
491
492         retval = io_ptr->open(device, IO_FLAG_RW, &io); 
493         if (retval) {
494                 com_err(device, 0, "while opening device file");
495                 exit(1);
496         }
497
498         image_io = fs->io;
499
500         ext2fs_rewrite_to_io(fs, io);
501
502         if (lseek(fd, fs->image_header->offset_inode, SEEK_SET) < 0) {
503                 perror("lseek");
504                 exit(1);
505         }
506
507         retval = ext2fs_image_inode_read(fs, fd, 0);
508         if (retval) {
509                 com_err(image_fn, 0, "while restoring the image table");
510                 exit(1);
511         }
512
513         ext2fs_close (fs);
514         exit (0);
515 }
516
517 int main (int argc, char ** argv)
518 {
519         int c;
520         errcode_t retval;
521         ext2_filsys fs;
522         char *image_fn;
523         int open_flag = 0;
524         int raw_flag = 0;
525         int install_flag = 0;
526         int fd = 0;
527
528 #ifdef ENABLE_NLS
529         setlocale(LC_MESSAGES, "");
530         setlocale(LC_CTYPE, "");
531         bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
532         textdomain(NLS_CAT_NAME);
533 #endif
534         fprintf (stderr, "e2image %s (%s)\n", E2FSPROGS_VERSION,
535                  E2FSPROGS_DATE);
536         if (argc && *argv)
537                 program_name = *argv;
538         initialize_ext2_error_table();
539         while ((c = getopt (argc, argv, "rI")) != EOF)
540                 switch (c) {
541                 case 'r':
542                         raw_flag++;
543                         break;
544                 case 'I':
545                         install_flag++;
546                         break;
547                 default:
548                         usage();
549                 }
550         if (optind != argc - 2 )
551                 usage();
552         device_name = argv[optind];
553         image_fn = argv[optind+1];
554
555         if (install_flag) {
556                 install_image(device_name, image_fn, raw_flag);
557                 exit (0);
558         }
559
560         retval = ext2fs_open (device_name, open_flag, 0, 0,
561                               unix_io_manager, &fs);
562         if (retval) {
563                 com_err (program_name, retval, _("while trying to open %s"),
564                          device_name);
565                 fputs(_("Couldn't find valid filesystem superblock.\n"), stdout);
566                 exit(1);
567         }
568
569         if (strcmp(image_fn, "-") == 0)
570                 fd = 1;
571         else {
572 #ifdef HAVE_OPEN64
573                 fd = open64(image_fn, O_CREAT|O_TRUNC|O_WRONLY, 0600);
574 #else
575                 fd = open(image_fn, O_CREAT|O_TRUNC|O_WRONLY, 0600);
576 #endif
577                 if (fd < 0) {
578                         com_err(program_name, errno,
579                                 _("while trying to open %s"), argv[optind+1]);
580                         exit(1);
581                 }
582         }
583
584         if (raw_flag)
585                 write_raw_image_file(fs, fd);
586         else
587                 write_image_file(fs, fd);
588
589         ext2fs_close (fs);
590         exit (0);
591 }