]> git.ipfire.org Git - thirdparty/e2fsprogs.git/blob - misc/e2image.c
pass1.c (process_block): Change the limit of directory size from
[thirdparty/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 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 int main (int argc, char ** argv)
451 {
452 int c;
453 errcode_t retval;
454 ext2_filsys fs;
455 char *outfn;
456 int open_flag = 0;
457 int raw_flag = 0;
458 int fd = 0;
459
460 #ifdef ENABLE_NLS
461 setlocale(LC_MESSAGES, "");
462 setlocale(LC_CTYPE, "");
463 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
464 textdomain(NLS_CAT_NAME);
465 #endif
466 fprintf (stderr, "e2image %s (%s)\n", E2FSPROGS_VERSION,
467 E2FSPROGS_DATE);
468 if (argc && *argv)
469 program_name = *argv;
470 initialize_ext2_error_table();
471 while ((c = getopt (argc, argv, "r")) != EOF)
472 switch (c) {
473 case 'r':
474 raw_flag++;
475 break;
476 default:
477 usage();
478 }
479 if (optind != argc - 2 )
480 usage();
481 device_name = argv[optind];
482 outfn = argv[optind+1];
483 retval = ext2fs_open (device_name, open_flag, 0, 0,
484 unix_io_manager, &fs);
485 if (retval) {
486 com_err (program_name, retval, _("while trying to open %s"),
487 device_name);
488 fputs(_("Couldn't find valid filesystem superblock.\n"), stdout);
489 exit(1);
490 }
491
492 if (strcmp(outfn, "-") == 0)
493 fd = 1;
494 else {
495 #ifdef HAVE_OPEN64
496 fd = open64(outfn, O_CREAT|O_TRUNC|O_WRONLY, 0600);
497 #else
498 fd = open(outfn, O_CREAT|O_TRUNC|O_WRONLY, 0600);
499 #endif
500 if (fd < 0) {
501 com_err(program_name, errno,
502 _("while trying to open %s"), argv[optind+1]);
503 exit(1);
504 }
505 }
506
507 if (raw_flag)
508 write_raw_image_file(fs, fd);
509 else
510 write_image_file(fs, fd);
511
512 ext2fs_close (fs);
513 exit (0);
514 }