Whamcloud - gitweb
Explain in mke2fs and tune2fs man pages that shared journals are not supported
[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 [-rsI] device image_file\n"), 
51                 program_name);
52         exit (1);
53 }
54
55 static void write_header(int fd, struct ext2_image_hdr *hdr, int blocksize)
56 {
57         char *header_buf;
58         int actual;
59
60         header_buf = malloc(blocksize);
61         if (!header_buf) {
62                 fputs(_("Couldn't allocate header buffer\n"), stderr);
63                 exit(1);
64         }
65
66         if (lseek(fd, 0, SEEK_SET) < 0) {
67                 perror("lseek while writing header");
68                 exit(1);
69         }
70         memset(header_buf, 0, blocksize);
71         
72         if (hdr)
73                 memcpy(header_buf, hdr, sizeof(struct ext2_image_hdr));
74         
75         actual = write(fd, header_buf, blocksize);
76         if (actual < 0) {
77                 perror("write header");
78                 exit(1);
79         }
80         if (actual != blocksize) {
81                 fprintf(stderr, _("short write (only %d bytes) for "
82                                   "writing image header"), actual);
83                 exit(1);
84         }
85         free(header_buf);
86 }
87
88 static void write_image_file(ext2_filsys fs, int fd)
89 {
90         struct ext2_image_hdr   hdr;
91         struct stat             st;
92         errcode_t               retval;
93
94         write_header(fd, NULL, fs->blocksize);
95         memset(&hdr, 0, sizeof(struct ext2_image_hdr));
96
97         hdr.offset_super = lseek(fd, 0, SEEK_CUR);
98         retval = ext2fs_image_super_write(fs, fd, 0);
99         if (retval) {
100                 com_err(program_name, retval, _("while writing superblock"));
101                 exit(1);
102         }
103         
104         hdr.offset_inode = lseek(fd, 0, SEEK_CUR);
105         retval = ext2fs_image_inode_write(fs, fd,
106                                   (fd != 1) ? IMAGER_FLAG_SPARSEWRITE : 0);
107         if (retval) {
108                 com_err(program_name, retval, _("while writing inode table"));
109                 exit(1);
110         }
111         
112         hdr.offset_blockmap = lseek(fd, 0, SEEK_CUR);
113         retval = ext2fs_image_bitmap_write(fs, fd, 0);
114         if (retval) {
115                 com_err(program_name, retval, _("while writing block bitmap"));
116                 exit(1);
117         }
118
119         hdr.offset_inodemap = lseek(fd, 0, SEEK_CUR);
120         retval = ext2fs_image_bitmap_write(fs, fd, IMAGER_FLAG_INODEMAP);
121         if (retval) {
122                 com_err(program_name, retval, _("while writing inode bitmap"));
123                 exit(1);
124         }
125
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         strncat(hdr.fs_device_name, device_name, sizeof(hdr.fs_device_name));
130         hdr.fs_device_name[sizeof(hdr.fs_device_name) - 1] = 0;
131         hdr.fs_blocksize = fs->blocksize;
132         
133         if (stat(device_name, &st) == 0)
134                 hdr.fs_device = st.st_rdev;
135
136         if (fstat(fd, &st) == 0) {
137                 hdr.image_device = st.st_dev;
138                 hdr.image_inode = st.st_ino;
139         }
140         memcpy(hdr.fs_uuid, fs->super->s_uuid, sizeof(hdr.fs_uuid));
141
142         hdr.image_time = time(0);
143         write_header(fd, &hdr, fs->blocksize);
144 }
145
146 /*
147  * These set of functions are used to write a RAW image file.
148  */
149 ext2fs_block_bitmap meta_block_map;
150 ext2fs_block_bitmap scramble_block_map; /* Directory blocks to be scrambled */
151
152 struct process_block_struct {
153         ext2_ino_t      ino;
154         int             is_dir;
155 };
156
157 /*
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
161  * the inode again.
162  */
163 static ino_t stashed_ino = 0;
164 static struct ext2_inode *stashed_inode;
165
166 static errcode_t meta_get_blocks(ext2_filsys fs EXT2FS_ATTR((unused)), 
167                                  ext2_ino_t ino,
168                                  blk_t *blocks)
169 {
170         int     i;
171         
172         if ((ino != stashed_ino) || !stashed_inode)
173                 return EXT2_ET_CALLBACK_NOTHANDLED;
174
175         for (i=0; i < EXT2_N_BLOCKS; i++)
176                 blocks[i] = stashed_inode->i_block[i];
177         return 0;
178 }
179
180 static errcode_t meta_check_directory(ext2_filsys fs EXT2FS_ATTR((unused)), 
181                                       ext2_ino_t ino)
182 {
183         if ((ino != stashed_ino) || !stashed_inode)
184                 return EXT2_ET_CALLBACK_NOTHANDLED;
185
186         if (!LINUX_S_ISDIR(stashed_inode->i_mode))
187                 return EXT2_ET_NO_DIRECTORY;
188         return 0;
189 }
190
191 static errcode_t meta_read_inode(ext2_filsys fs EXT2FS_ATTR((unused)), 
192                                  ext2_ino_t ino,
193                                  struct ext2_inode *inode)
194 {
195         if ((ino != stashed_ino) || !stashed_inode)
196                 return EXT2_ET_CALLBACK_NOTHANDLED;
197         *inode = *stashed_inode;
198         return 0;
199 }
200
201 static void use_inode_shortcuts(ext2_filsys fs, int bool)
202 {
203         if (bool) {
204                 fs->get_blocks = meta_get_blocks;
205                 fs->check_directory = meta_check_directory;
206                 fs->read_inode = meta_read_inode;
207                 stashed_ino = 0;
208         } else {
209                 fs->get_blocks = 0;
210                 fs->check_directory = 0;
211                 fs->read_inode = 0;
212         }
213 }
214
215 static int process_dir_block(ext2_filsys fs EXT2FS_ATTR((unused)), 
216                              blk_t *block_nr,
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)))
221 {
222         struct process_block_struct *p;
223
224         p = (struct process_block_struct *) priv_data;
225
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);
229         return 0;
230 }
231
232 static int process_file_block(ext2_filsys fs EXT2FS_ATTR((unused)), 
233                               blk_t *block_nr,
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)))
238 {
239         if (blockcnt < 0) {
240                 ext2fs_mark_block_bitmap(meta_block_map, *block_nr);
241         }
242         return 0;
243 }
244
245 static void mark_table_blocks(ext2_filsys fs)
246 {
247         blk_t   block, b;
248         unsigned int    i,j;
249         
250         block = fs->super->s_first_data_block;
251         /*
252          * Mark primary superblock
253          */
254         ext2fs_mark_block_bitmap(meta_block_map, block);
255                         
256         /*
257          * Mark the primary superblock descriptors
258          */
259         for (j = 0; j < fs->desc_blocks; j++) {
260                 ext2fs_mark_block_bitmap(meta_block_map,
261                          ext2fs_descriptor_block_loc(fs, block, j));
262         }
263
264         for (i = 0; i < fs->group_desc_count; i++) {
265                 /*
266                  * Mark the blocks used for the inode table
267                  */
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;
271                              j++, b++)
272                                 ext2fs_mark_block_bitmap(meta_block_map, b);
273                 }
274                             
275                 /*
276                  * Mark block used for the block bitmap 
277                  */
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);
281                 }
282                 
283                 /*
284                  * Mark block used for the inode bitmap 
285                  */
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);
289                 }
290                 block += fs->super->s_blocks_per_group;
291         }
292 }
293
294 /*
295  * This function returns 1 if the specified block is all zeros
296  */
297 static int check_zero_block(char *buf, int blocksize)
298 {
299         char    *cp = buf;
300         int     left = blocksize;
301
302         while (left > 0) {
303                 if (*cp++)
304                         return 0;
305                 left--;
306         }
307         return 1;
308 }
309
310 static void write_block(int fd, char *buf, int sparse_offset,
311                         int blocksize, blk_t block)
312 {
313         int             count;
314         errcode_t       err;
315
316         if (sparse_offset) {
317 #ifdef HAVE_LSEEK64
318                 if (lseek64(fd, sparse_offset, SEEK_CUR) < 0)
319                         perror("lseek");
320 #else
321                 if (lseek(fd, sparse_offset, SEEK_CUR) < 0)
322                         perror("lseek");
323 #endif
324         }
325         if (blocksize) {
326                 count = write(fd, buf, blocksize);
327                 if (count != blocksize) {
328                         if (count == -1)
329                                 err = errno;
330                         else
331                                 err = 0;
332                         com_err(program_name, err, "error writing block %u",
333                                 block);
334                 }
335         }
336 }
337
338 int name_id[256];
339
340 static void scramble_dir_block(ext2_filsys fs, blk_t blk, char *buf)
341 {
342         char *p, *end, *cp;
343         struct ext2_dir_entry_2 *dirent;
344         int rec_len, id, len;
345
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);
353 #endif
354 #if 0
355                 printf("rec_len = %d, name_len = %d\n", rec_len, dirent->name_len);
356 #endif
357                 if (rec_len < 8 || (rec_len % 4) ||
358                     (p+rec_len > end)) {
359                         printf("Corrupt directory block %lu: "
360                                "bad rec_len (%d)\n", (unsigned long) blk, 
361                                rec_len);
362                         rec_len = end - p;
363 #ifdef EXT2FS_ENABLE_SWAPFS
364                         if (fs->flags & EXT2_FLAG_SWAP_BYTES) 
365                                 dirent->rec_len = ext2fs_swab16(rec_len);
366 #endif
367                         continue;
368                 }
369                 if (dirent->name_len + 8 > rec_len) {
370                         printf("Corrupt directory block %lu: "
371                                "bad name_len (%d)\n", (unsigned long) blk, 
372                                dirent->name_len);
373                         dirent->name_len = rec_len - 8;
374                         continue;
375                 }
376                 cp = p+8;
377                 len = rec_len - dirent->name_len - 8;
378                 if (len > 0)
379                         memset(cp+dirent->name_len, 0, len);
380                 if (dirent->name_len==1 && cp[0] == '.')
381                         continue;
382                 if (dirent->name_len==2 && cp[0] == '.' && cp[1] == '.')
383                         continue;
384
385                 memset(cp, 'A', dirent->name_len);
386                 len = dirent->name_len;
387                 id = name_id[len]++;
388                 while ((len > 0) && (id > 0)) {
389                         *cp += id % 26;
390                         id = id / 26;
391                         cp++;
392                         len--;
393                 }
394         }
395 }
396
397 static void output_meta_data_blocks(ext2_filsys fs, int fd)
398 {
399         errcode_t       retval;
400         blk_t           blk;
401         char            *buf, *zero_buf;
402         int             sparse = 0;
403
404         buf = malloc(fs->blocksize);
405         if (!buf) {
406                 com_err(program_name, ENOMEM, "while allocating buffer");
407                 exit(1);
408         }
409         zero_buf = malloc(fs->blocksize);
410         if (!zero_buf) {
411                 com_err(program_name, ENOMEM, "while allocating buffer");
412                 exit(1);
413         }
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);
419                         if (retval) {
420                                 com_err(program_name, retval,
421                                         "error reading block %d", blk);
422                         }
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))
427                                 goto sparse_write;
428                         write_block(fd, buf, sparse, fs->blocksize, blk);
429                         sparse = 0;
430                 } else {
431                 sparse_write:
432                         if (fd == 1) {
433                                 write_block(fd, zero_buf, 0,
434                                             fs->blocksize, blk);
435                                 continue;
436                         }
437                         sparse += fs->blocksize;
438                         if (sparse >= 1024*1024) {
439                                 write_block(fd, 0, sparse, 0, 0);
440                                 sparse = 0;
441                         }
442                 }
443         }
444         write_block(fd, zero_buf, sparse, 1, -1);
445 }
446
447 static void write_raw_image_file(ext2_filsys fs, int fd, int scramble_flag)
448 {
449         struct process_block_struct     pb;
450         struct ext2_inode               inode;
451         ext2_inode_scan                 scan;
452         ext2_ino_t                      ino;
453         errcode_t                       retval;
454         char *                          block_buf;
455         
456         retval = ext2fs_allocate_block_bitmap(fs, "in-use block map",
457                                               &meta_block_map);
458         if (retval) {
459                 com_err(program_name, retval, "while allocating block bitmap");
460                 exit(1);
461         }
462
463         if (scramble_flag) {
464                 retval = ext2fs_allocate_block_bitmap(fs, "scramble block map",
465                                                       &scramble_block_map);
466                 if (retval) {
467                         com_err(program_name, retval, 
468                                 "while allocating scramble block bitmap");
469                         exit(1);
470                 }
471         }
472         
473         mark_table_blocks(fs);
474
475         retval = ext2fs_open_inode_scan(fs, 0, &scan);
476         if (retval) {
477                 com_err(program_name, retval, _("while opening inode scan"));
478                 exit(1);
479         }
480
481         block_buf = malloc(fs->blocksize * 3);
482         if (!block_buf) {
483                 com_err(program_name, 0, "Can't allocate block buffer");
484                 exit(1);
485         }
486         
487         use_inode_shortcuts(fs, 1);
488         stashed_inode = &inode;
489         while (1) {
490                 retval = ext2fs_get_next_inode(scan, &ino, &inode);
491                 if (retval == EXT2_ET_BAD_BLOCK_IN_INODE_TABLE)
492                         continue;
493                 if (retval) {
494                         com_err(program_name, retval,
495                                 _("while getting next inode"));
496                         exit(1);
497                 }
498                 if (ino == 0)
499                         break;
500                 if (!inode.i_links_count)
501                         continue;
502                 if (inode.i_file_acl) {
503                         ext2fs_mark_block_bitmap(meta_block_map,
504                                                  inode.i_file_acl);
505                 }
506                 if (!ext2fs_inode_has_valid_blocks(&inode))
507                         continue;
508                 
509                 stashed_ino = ino;
510                 pb.ino = ino;
511                 pb.is_dir = LINUX_S_ISDIR(inode.i_mode);
512                 if (LINUX_S_ISDIR(inode.i_mode) ||
513                     (LINUX_S_ISLNK(inode.i_mode) &&
514                      ext2fs_inode_has_valid_blocks(&inode)) ||
515                     ino == fs->super->s_journal_inum) {
516                         retval = ext2fs_block_iterate2(fs, ino, 0, 
517                                        block_buf, process_dir_block, &pb);
518                         if (retval) {
519                                 com_err(program_name, retval,
520                                         "while iterating over inode %d", 
521                                         ino);
522                                 exit(1);
523                         }
524                 } else {
525                         if (inode.i_block[EXT2_IND_BLOCK] ||
526                             inode.i_block[EXT2_DIND_BLOCK] ||
527                             inode.i_block[EXT2_TIND_BLOCK]) {
528                                 retval = ext2fs_block_iterate2(fs,
529                                        ino, 0, block_buf,
530                                        process_file_block, &pb);
531                                 if (retval) {
532                                         com_err(program_name, retval,
533                                         "while iterating over %d", ino);
534                                         exit(1);
535                                 }
536                         }
537                 }
538         }
539         use_inode_shortcuts(fs, 0);
540         output_meta_data_blocks(fs, fd);
541 }
542
543 static void install_image(char *device, char *image_fn, int raw_flag)
544 {
545         errcode_t retval;
546         ext2_filsys fs;
547         int open_flag = EXT2_FLAG_IMAGE_FILE;
548         int fd = 0;
549         io_manager      io_ptr;
550         io_channel      io, image_io;
551
552         if (raw_flag) {
553                 com_err(program_name, 0, "Raw images cannot be installed");
554                 exit(1);
555         }
556         
557 #ifdef CONFIG_TESTIO_DEBUG
558         io_ptr = test_io_manager;
559         test_io_backing_manager = unix_io_manager;
560 #else
561         io_ptr = unix_io_manager;
562 #endif
563
564         retval = ext2fs_open (image_fn, open_flag, 0, 0,
565                               io_ptr, &fs);
566         if (retval) {
567                 com_err (program_name, retval, _("while trying to open %s"),
568                          image_fn);
569                 exit(1);
570         }
571
572         retval = ext2fs_read_bitmaps (fs);
573         if (retval) {
574                 com_err(program_name, retval, "error reading bitmaps");
575                 exit(1);
576         }
577
578
579         fd = open(image_fn, O_RDONLY);
580         if (fd < 0) {
581                 perror(image_fn);
582                 exit(1);
583         }
584
585         retval = io_ptr->open(device, IO_FLAG_RW, &io); 
586         if (retval) {
587                 com_err(device, 0, "while opening device file");
588                 exit(1);
589         }
590
591         image_io = fs->io;
592
593         ext2fs_rewrite_to_io(fs, io);
594
595         if (lseek(fd, fs->image_header->offset_inode, SEEK_SET) < 0) {
596                 perror("lseek");
597                 exit(1);
598         }
599
600         retval = ext2fs_image_inode_read(fs, fd, 0);
601         if (retval) {
602                 com_err(image_fn, 0, "while restoring the image table");
603                 exit(1);
604         }
605
606         ext2fs_close (fs);
607         exit (0);
608 }
609
610 int main (int argc, char ** argv)
611 {
612         int c;
613         errcode_t retval;
614         ext2_filsys fs;
615         char *image_fn;
616         int open_flag = 0;
617         int raw_flag = 0;
618         int install_flag = 0;
619         int scramble_flag = 0;
620         int fd = 0;
621
622 #ifdef ENABLE_NLS
623         setlocale(LC_MESSAGES, "");
624         setlocale(LC_CTYPE, "");
625         bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
626         textdomain(NLS_CAT_NAME);
627 #endif
628         fprintf (stderr, "e2image %s (%s)\n", E2FSPROGS_VERSION,
629                  E2FSPROGS_DATE);
630         if (argc && *argv)
631                 program_name = *argv;
632         initialize_ext2_error_table();
633         while ((c = getopt (argc, argv, "rsI")) != EOF)
634                 switch (c) {
635                 case 'r':
636                         raw_flag++;
637                         break;
638                 case 's':
639                         scramble_flag++;
640                         break;
641                 case 'I':
642                         install_flag++;
643                         break;
644                 default:
645                         usage();
646                 }
647         if (optind != argc - 2 )
648                 usage();
649         device_name = argv[optind];
650         image_fn = argv[optind+1];
651
652         if (install_flag) {
653                 install_image(device_name, image_fn, raw_flag);
654                 exit (0);
655         }
656
657         retval = ext2fs_open (device_name, open_flag, 0, 0,
658                               unix_io_manager, &fs);
659         if (retval) {
660                 com_err (program_name, retval, _("while trying to open %s"),
661                          device_name);
662                 fputs(_("Couldn't find valid filesystem superblock.\n"), stdout);
663                 exit(1);
664         }
665
666         if (strcmp(image_fn, "-") == 0)
667                 fd = 1;
668         else {
669 #ifdef HAVE_OPEN64
670                 fd = open64(image_fn, O_CREAT|O_TRUNC|O_WRONLY, 0600);
671 #else
672                 fd = open(image_fn, O_CREAT|O_TRUNC|O_WRONLY, 0600);
673 #endif
674                 if (fd < 0) {
675                         com_err(program_name, errno,
676                                 _("while trying to open %s"), argv[optind+1]);
677                         exit(1);
678                 }
679         }
680
681         if (raw_flag)
682                 write_raw_image_file(fs, fd, scramble_flag);
683         else
684                 write_image_file(fs, fd);
685
686         ext2fs_close (fs);
687         exit (0);
688 }