1 #define _LARGEFILE64_SOURCE
2
3 #define LOG_TAG "f2fs_sparseblock"
4
5 #include "f2fs_sparseblock.h"
6
7 #include <errno.h>
8 #include <f2fs_fs.h>
9 #include <fcntl.h>
10 #include <linux/types.h>
11 #include <malloc.h>
12 #include <string.h>
13 #include <sys/stat.h>
14 #include <sys/types.h>
15 #include <unistd.h>
16
17 #include <log/log.h>
18
19 #define D_DISP_u32(ptr, member) \
20 do { \
21 SLOGD("%-30s" "\t\t[0x%#08x : %u]\n", \
22 #member, le32_to_cpu((ptr)->member), le32_to_cpu((ptr)->member) ); \
23 } while (0);
24
25 #define D_DISP_u64(ptr, member) \
26 do { \
27 SLOGD("%-30s" "\t\t[0x%#016llx : %llu]\n", \
28 #member, le64_to_cpu((ptr)->member), le64_to_cpu((ptr)->member) ); \
29 } while (0);
30
31 #define segno_in_journal(jnl, i) ((jnl)->sit_j.entries[i].segno)
32
33 #define sit_in_journal(jnl, i) ((jnl)->sit_j.entries[i].se)
34
dbg_print_raw_sb_info(struct f2fs_super_block * sb)35 static void dbg_print_raw_sb_info(struct f2fs_super_block *sb)
36 {
37 SLOGD("\n");
38 SLOGD("+--------------------------------------------------------+\n");
39 SLOGD("| Super block |\n");
40 SLOGD("+--------------------------------------------------------+\n");
41
42 D_DISP_u32(sb, magic);
43 D_DISP_u32(sb, major_ver);
44 D_DISP_u32(sb, minor_ver);
45 D_DISP_u32(sb, log_sectorsize);
46 D_DISP_u32(sb, log_sectors_per_block);
47
48 D_DISP_u32(sb, log_blocksize);
49 D_DISP_u32(sb, log_blocks_per_seg);
50 D_DISP_u32(sb, segs_per_sec);
51 D_DISP_u32(sb, secs_per_zone);
52 D_DISP_u32(sb, checksum_offset);
53 D_DISP_u64(sb, block_count);
54
55 D_DISP_u32(sb, section_count);
56 D_DISP_u32(sb, segment_count);
57 D_DISP_u32(sb, segment_count_ckpt);
58 D_DISP_u32(sb, segment_count_sit);
59 D_DISP_u32(sb, segment_count_nat);
60
61 D_DISP_u32(sb, segment_count_ssa);
62 D_DISP_u32(sb, segment_count_main);
63 D_DISP_u32(sb, segment0_blkaddr);
64
65 D_DISP_u32(sb, cp_blkaddr);
66 D_DISP_u32(sb, sit_blkaddr);
67 D_DISP_u32(sb, nat_blkaddr);
68 D_DISP_u32(sb, ssa_blkaddr);
69 D_DISP_u32(sb, main_blkaddr);
70
71 D_DISP_u32(sb, root_ino);
72 D_DISP_u32(sb, node_ino);
73 D_DISP_u32(sb, meta_ino);
74 D_DISP_u32(sb, cp_payload);
75 SLOGD("\n");
76 }
dbg_print_raw_ckpt_struct(struct f2fs_checkpoint * cp)77 static void dbg_print_raw_ckpt_struct(struct f2fs_checkpoint *cp)
78 {
79 SLOGD("\n");
80 SLOGD("+--------------------------------------------------------+\n");
81 SLOGD("| Checkpoint |\n");
82 SLOGD("+--------------------------------------------------------+\n");
83
84 D_DISP_u64(cp, checkpoint_ver);
85 D_DISP_u64(cp, user_block_count);
86 D_DISP_u64(cp, valid_block_count);
87 D_DISP_u32(cp, rsvd_segment_count);
88 D_DISP_u32(cp, overprov_segment_count);
89 D_DISP_u32(cp, free_segment_count);
90
91 D_DISP_u32(cp, alloc_type[CURSEG_HOT_NODE]);
92 D_DISP_u32(cp, alloc_type[CURSEG_WARM_NODE]);
93 D_DISP_u32(cp, alloc_type[CURSEG_COLD_NODE]);
94 D_DISP_u32(cp, cur_node_segno[0]);
95 D_DISP_u32(cp, cur_node_segno[1]);
96 D_DISP_u32(cp, cur_node_segno[2]);
97
98 D_DISP_u32(cp, cur_node_blkoff[0]);
99 D_DISP_u32(cp, cur_node_blkoff[1]);
100 D_DISP_u32(cp, cur_node_blkoff[2]);
101
102
103 D_DISP_u32(cp, alloc_type[CURSEG_HOT_DATA]);
104 D_DISP_u32(cp, alloc_type[CURSEG_WARM_DATA]);
105 D_DISP_u32(cp, alloc_type[CURSEG_COLD_DATA]);
106 D_DISP_u32(cp, cur_data_segno[0]);
107 D_DISP_u32(cp, cur_data_segno[1]);
108 D_DISP_u32(cp, cur_data_segno[2]);
109
110 D_DISP_u32(cp, cur_data_blkoff[0]);
111 D_DISP_u32(cp, cur_data_blkoff[1]);
112 D_DISP_u32(cp, cur_data_blkoff[2]);
113
114 D_DISP_u32(cp, ckpt_flags);
115 D_DISP_u32(cp, cp_pack_total_block_count);
116 D_DISP_u32(cp, cp_pack_start_sum);
117 D_DISP_u32(cp, valid_node_count);
118 D_DISP_u32(cp, valid_inode_count);
119 D_DISP_u32(cp, next_free_nid);
120 D_DISP_u32(cp, sit_ver_bitmap_bytesize);
121 D_DISP_u32(cp, nat_ver_bitmap_bytesize);
122 D_DISP_u32(cp, checksum_offset);
123 D_DISP_u64(cp, elapsed_time);
124
125 D_DISP_u32(cp, sit_nat_version_bitmap[0]);
126 SLOGD("\n\n");
127 }
128
dbg_print_info_struct(struct f2fs_info * info)129 static void dbg_print_info_struct(struct f2fs_info *info)
130 {
131 SLOGD("\n");
132 SLOGD("+--------------------------------------------------------+\n");
133 SLOGD("| F2FS_INFO |\n");
134 SLOGD("+--------------------------------------------------------+\n");
135 SLOGD("blocks_per_segment: %" PRIu64, info->blocks_per_segment);
136 SLOGD("block_size: %d", info->block_size);
137 SLOGD("sit_bmp loc: %p", info->sit_bmp);
138 SLOGD("sit_bmp_size: %d", info->sit_bmp_size);
139 SLOGD("blocks_per_sit: %" PRIu64, info->blocks_per_sit);
140 SLOGD("sit_blocks loc: %p", info->sit_blocks);
141 SLOGD("sit_sums loc: %p", info->sit_sums);
142 SLOGD("sit_sums num: %d", le16_to_cpu(info->sit_sums->journal.n_sits));
143 unsigned int i;
144 for(i = 0; i < (le16_to_cpu(info->sit_sums->journal.n_sits)); i++) {
145 SLOGD("entry %d in journal entries is for segment %d", i,
146 le32_to_cpu(segno_in_journal(&info->sit_sums->journal, i)));
147 }
148
149 SLOGD("cp_blkaddr: %" PRIu64, info->cp_blkaddr);
150 SLOGD("cp_valid_cp_blkaddr: %" PRIu64, info->cp_valid_cp_blkaddr);
151 SLOGD("sit_blkaddr: %" PRIu64, info->sit_blkaddr);
152 SLOGD("nat_blkaddr: %" PRIu64, info->nat_blkaddr);
153 SLOGD("ssa_blkaddr: %" PRIu64, info->ssa_blkaddr);
154 SLOGD("main_blkaddr: %" PRIu64, info->main_blkaddr);
155 SLOGD("total_user_used: %" PRIu64, info->total_user_used);
156 SLOGD("total_blocks: %" PRIu64, info->total_blocks);
157 SLOGD("\n\n");
158 }
159
160
161 /* read blocks */
read_structure(int fd,unsigned long long start,void * buf,ssize_t len)162 static int read_structure(int fd, unsigned long long start, void *buf, ssize_t len)
163 {
164 off64_t ret;
165
166 ret = lseek64(fd, start, SEEK_SET);
167 if (ret < 0) {
168 SLOGE("failed to seek\n");
169 return ret;
170 }
171
172 ret = read(fd, buf, len);
173 if (ret < 0) {
174 SLOGE("failed to read\n");
175 return ret;
176 }
177 if (ret != len) {
178 SLOGE("failed to read all\n");
179 return -1;
180 }
181 return 0;
182 }
183
read_structure_blk(int fd,unsigned long long start_blk,void * buf,size_t len)184 static int read_structure_blk(int fd, unsigned long long start_blk, void *buf, size_t len)
185 {
186 return read_structure(fd, F2FS_BLKSIZE*start_blk, buf, F2FS_BLKSIZE * len);
187 }
188
read_f2fs_sb(int fd,struct f2fs_super_block * sb)189 static int read_f2fs_sb(int fd, struct f2fs_super_block *sb)
190 {
191 int rc;
192 rc = read_structure(fd, F2FS_SUPER_OFFSET, sb, sizeof(*sb));
193 if (le32_to_cpu(sb->magic) != F2FS_SUPER_MAGIC) {
194 SLOGE("Not a valid F2FS super block. Magic:%#08x != %#08x",
195 le32_to_cpu(sb->magic), F2FS_SUPER_MAGIC);
196 return -1;
197 }
198 return 0;
199 }
200
get_f2fs_filesystem_size_sec(char * dev)201 unsigned int get_f2fs_filesystem_size_sec(char *dev)
202 {
203 int fd;
204 if ((fd = open(dev, O_RDONLY)) < 0) {
205 SLOGE("Cannot open device to get filesystem size ");
206 return 0;
207 }
208 struct f2fs_super_block sb;
209 if(read_f2fs_sb(fd, &sb))
210 return 0;
211 return (unsigned int)(le64_to_cpu(sb.block_count)*F2FS_BLKSIZE/DEFAULT_SECTOR_SIZE);
212 }
213
validate_checkpoint(block_t cp_addr,unsigned long long * version,int fd)214 static struct f2fs_checkpoint *validate_checkpoint(block_t cp_addr,
215 unsigned long long *version, int fd)
216 {
217 unsigned char *cp_block_1, *cp_block_2;
218 struct f2fs_checkpoint *cp_block;
219 uint64_t cp1_version = 0, cp2_version = 0;
220
221 cp_block_1 = malloc(F2FS_BLKSIZE);
222 if (!cp_block_1)
223 return NULL;
224
225 /* Read the 1st cp block in this CP pack */
226 if (read_structure_blk(fd, cp_addr, cp_block_1, 1))
227 goto invalid_cp1;
228
229 /* get the version number */
230 cp_block = (struct f2fs_checkpoint *)cp_block_1;
231
232 cp1_version = le64_to_cpu(cp_block->checkpoint_ver);
233
234 cp_block_2 = malloc(F2FS_BLKSIZE);
235 if (!cp_block_2) {
236 goto invalid_cp1;
237 }
238 /* Read the 2nd cp block in this CP pack */
239 cp_addr += le32_to_cpu(cp_block->cp_pack_total_block_count) - 1;
240 if (read_structure_blk(fd, cp_addr, cp_block_2, 1)) {
241 goto invalid_cp2;
242 }
243
244 cp_block = (struct f2fs_checkpoint *)cp_block_2;
245
246 cp2_version = le64_to_cpu(cp_block->checkpoint_ver);
247
248 if (cp2_version == cp1_version) {
249 *version = cp2_version;
250 free(cp_block_2);
251 return (struct f2fs_checkpoint *)cp_block_1;
252 }
253
254 /* There must be something wrong with this checkpoint */
255 invalid_cp2:
256 free(cp_block_2);
257 invalid_cp1:
258 free(cp_block_1);
259 return NULL;
260 }
261
get_valid_checkpoint_info(int fd,struct f2fs_super_block * sb,struct f2fs_checkpoint ** cp,struct f2fs_info * info)262 int get_valid_checkpoint_info(int fd, struct f2fs_super_block *sb, struct f2fs_checkpoint **cp, struct f2fs_info *info)
263 {
264 struct f2fs_checkpoint *cp1, *cp2, *cur_cp;
265 unsigned long blk_size;
266 unsigned long long cp1_version = 0, cp2_version = 0;
267 unsigned long long cp1_start_blk_no;
268 unsigned long long cp2_start_blk_no;
269
270 blk_size = 1U << le32_to_cpu(sb->log_blocksize);
271
272 /*
273 * Find valid cp by reading both packs and finding most recent one.
274 */
275 cp1_start_blk_no = le32_to_cpu(sb->cp_blkaddr);
276 cp1 = validate_checkpoint(cp1_start_blk_no, &cp1_version, fd);
277
278 /* The second checkpoint pack should start at the next segment */
279 cp2_start_blk_no = cp1_start_blk_no + (1 << le32_to_cpu(sb->log_blocks_per_seg));
280 cp2 = validate_checkpoint(cp2_start_blk_no, &cp2_version, fd);
281
282 if (cp1 && cp2) {
283 if (ver_after(cp2_version, cp1_version)) {
284 cur_cp = cp2;
285 info->cp_valid_cp_blkaddr = cp2_start_blk_no;
286 free(cp1);
287 } else {
288 cur_cp = cp1;
289 info->cp_valid_cp_blkaddr = cp1_start_blk_no;
290 free(cp2);
291 }
292 } else if (cp1) {
293 cur_cp = cp1;
294 info->cp_valid_cp_blkaddr = cp1_start_blk_no;
295 } else if (cp2) {
296 cur_cp = cp2;
297 info->cp_valid_cp_blkaddr = cp2_start_blk_no;
298 } else {
299 goto fail_no_cp;
300 }
301
302 *cp = cur_cp;
303
304 return 0;
305
306 fail_no_cp:
307 SLOGE("Valid Checkpoint not found!!");
308 return -EINVAL;
309 }
310
gather_sit_info(int fd,struct f2fs_info * info)311 static int gather_sit_info(int fd, struct f2fs_info *info)
312 {
313 uint64_t num_segments = (info->total_blocks - info->main_blkaddr
314 + info->blocks_per_segment - 1) / info->blocks_per_segment;
315 uint64_t num_sit_blocks = (num_segments + SIT_ENTRY_PER_BLOCK - 1) / SIT_ENTRY_PER_BLOCK;
316 uint64_t sit_block;
317
318 info->sit_blocks = malloc(num_sit_blocks * sizeof(struct f2fs_sit_block));
319 if (!info->sit_blocks)
320 return -1;
321
322 for(sit_block = 0; sit_block<num_sit_blocks; sit_block++) {
323 off64_t address = info->sit_blkaddr + sit_block;
324
325 if (f2fs_test_bit(sit_block, info->sit_bmp))
326 address += info->blocks_per_sit;
327
328 SLOGD("Reading cache block starting at block %"PRIu64, address);
329 if (read_structure(fd, address * F2FS_BLKSIZE, &info->sit_blocks[sit_block], sizeof(struct f2fs_sit_block))) {
330 SLOGE("Could not read sit block at block %"PRIu64, address);
331 free(info->sit_blocks);
332 info->sit_blocks = NULL;
333 return -1;
334 }
335 }
336 return 0;
337 }
338
is_set_ckpt_flags(struct f2fs_checkpoint * cp,unsigned int f)339 static inline int is_set_ckpt_flags(struct f2fs_checkpoint *cp, unsigned int f)
340 {
341 unsigned int ckpt_flags = le32_to_cpu(cp->ckpt_flags);
342 return !!(ckpt_flags & f);
343 }
344
sum_blk_addr(struct f2fs_checkpoint * cp,struct f2fs_info * info,int base,int type)345 static inline uint64_t sum_blk_addr(struct f2fs_checkpoint *cp, struct f2fs_info *info, int base, int type)
346 {
347 return info->cp_valid_cp_blkaddr + le32_to_cpu(cp->cp_pack_total_block_count)
348 - (base + 1) + type;
349 }
350
get_sit_summary(int fd,struct f2fs_info * info,struct f2fs_checkpoint * cp)351 static int get_sit_summary(int fd, struct f2fs_info *info, struct f2fs_checkpoint *cp)
352 {
353 char buffer[F2FS_BLKSIZE];
354
355 info->sit_sums = calloc(1, sizeof(struct f2fs_summary_block));
356 if (!info->sit_sums)
357 return -1;
358
359 /* CURSEG_COLD_DATA where the journaled SIT entries are. */
360 if (is_set_ckpt_flags(cp, CP_COMPACT_SUM_FLAG)) {
361 if (read_structure_blk(fd, info->cp_valid_cp_blkaddr + le32_to_cpu(cp->cp_pack_start_sum), buffer, 1))
362 return -1;
363 memcpy(&info->sit_sums->journal.n_sits, &buffer[SUM_JOURNAL_SIZE], SUM_JOURNAL_SIZE);
364 } else {
365 uint64_t blk_addr;
366 if (is_set_ckpt_flags(cp, CP_UMOUNT_FLAG))
367 blk_addr = sum_blk_addr(cp, info, NR_CURSEG_TYPE, CURSEG_COLD_DATA);
368 else
369 blk_addr = sum_blk_addr(cp, info, NR_CURSEG_DATA_TYPE, CURSEG_COLD_DATA);
370
371 if (read_structure_blk(fd, blk_addr, buffer, 1))
372 return -1;
373
374 memcpy(info->sit_sums, buffer, sizeof(struct f2fs_summary_block));
375 }
376 return 0;
377 }
378
generate_f2fs_info(int fd)379 struct f2fs_info *generate_f2fs_info(int fd)
380 {
381 struct f2fs_super_block *sb = NULL;
382 struct f2fs_checkpoint *cp = NULL;
383 struct f2fs_info *info;
384
385 info = calloc(1, sizeof(*info));
386 if (!info) {
387 SLOGE("Out of memory!");
388 return NULL;
389 }
390
391 sb = malloc(sizeof(*sb));
392 if(!sb) {
393 SLOGE("Out of memory!");
394 free(info);
395 return NULL;
396 }
397 if (read_f2fs_sb(fd, sb)) {
398 SLOGE("Failed to read superblock");
399 free(info);
400 free(sb);
401 return NULL;
402 }
403 dbg_print_raw_sb_info(sb);
404
405 info->cp_blkaddr = le32_to_cpu(sb->cp_blkaddr);
406 info->sit_blkaddr = le32_to_cpu(sb->sit_blkaddr);
407 info->nat_blkaddr = le32_to_cpu(sb->nat_blkaddr);
408 info->ssa_blkaddr = le32_to_cpu(sb->ssa_blkaddr);
409 info->main_blkaddr = le32_to_cpu(sb->main_blkaddr);
410 info->block_size = F2FS_BLKSIZE;
411 info->total_blocks = sb->block_count;
412 info->blocks_per_sit = (le32_to_cpu(sb->segment_count_sit) >> 1) << le32_to_cpu(sb->log_blocks_per_seg);
413 info->blocks_per_segment = 1U << le32_to_cpu(sb->log_blocks_per_seg);
414
415 if (get_valid_checkpoint_info(fd, sb, &cp, info))
416 goto error;
417 dbg_print_raw_ckpt_struct(cp);
418
419 info->total_user_used = le32_to_cpu(cp->valid_block_count);
420
421 u32 bmp_size = le32_to_cpu(cp->sit_ver_bitmap_bytesize);
422
423 /* get sit validity bitmap */
424 info->sit_bmp = malloc(bmp_size);
425 if(!info->sit_bmp) {
426 SLOGE("Out of memory!");
427 goto error;
428 }
429
430 info->sit_bmp_size = bmp_size;
431 if (read_structure(fd, info->cp_valid_cp_blkaddr * F2FS_BLKSIZE
432 + offsetof(struct f2fs_checkpoint, sit_nat_version_bitmap),
433 info->sit_bmp, bmp_size)) {
434 SLOGE("Error getting SIT validity bitmap");
435 goto error;
436 }
437
438 if (gather_sit_info(fd , info)) {
439 SLOGE("Error getting SIT information");
440 goto error;
441 }
442 if (get_sit_summary(fd, info, cp)) {
443 SLOGE("Error getting SIT entries in summary area");
444 goto error;
445 }
446 dbg_print_info_struct(info);
447 return info;
448 error:
449 free(sb);
450 free(cp);
451 free_f2fs_info(info);
452 return NULL;
453 }
454
free_f2fs_info(struct f2fs_info * info)455 void free_f2fs_info(struct f2fs_info *info)
456 {
457 if (info) {
458 free(info->sit_blocks);
459 info->sit_blocks = NULL;
460
461 free(info->sit_bmp);
462 info->sit_bmp = NULL;
463
464 free(info->sit_sums);
465 info->sit_sums = NULL;
466 }
467 free(info);
468 }
469
get_num_blocks_used(struct f2fs_info * info)470 uint64_t get_num_blocks_used(struct f2fs_info *info)
471 {
472 return info->main_blkaddr + info->total_user_used;
473 }
474
f2fs_test_bit(unsigned int nr,const char * p)475 int f2fs_test_bit(unsigned int nr, const char *p)
476 {
477 int mask;
478 char *addr = (char *)p;
479
480 addr += (nr >> 3);
481 mask = 1 << (7 - (nr & 0x07));
482 return (mask & *addr) != 0;
483 }
484
run_on_used_blocks(uint64_t startblock,struct f2fs_info * info,int (* func)(uint64_t pos,void * data),void * data)485 int run_on_used_blocks(uint64_t startblock, struct f2fs_info *info, int (*func)(uint64_t pos, void *data), void *data) {
486 struct f2fs_sit_entry * sit_entry;
487 uint64_t sit_block_num_cur = 0, segnum = 0, block_offset;
488 uint64_t block;
489 unsigned int used, found, i;
490
491 block = startblock;
492 while (block < info->total_blocks) {
493 /* TODO: Save only relevant portions of metadata */
494 if (block < info->main_blkaddr) {
495 if (func(block, data)) {
496 SLOGI("func error");
497 return -1;
498 }
499 } else {
500 /* Main Section */
501 segnum = (block - info->main_blkaddr)/info->blocks_per_segment;
502
503 /* check the SIT entries in the journal */
504 found = 0;
505 for(i = 0; i < le16_to_cpu(info->sit_sums->journal.n_sits); i++) {
506 if (le32_to_cpu(segno_in_journal(&info->sit_sums->journal, i)) == segnum) {
507 sit_entry = &sit_in_journal(&info->sit_sums->journal, i);
508 found = 1;
509 break;
510 }
511 }
512
513 /* get SIT entry from SIT section */
514 if (!found) {
515 sit_block_num_cur = segnum / SIT_ENTRY_PER_BLOCK;
516 sit_entry = &info->sit_blocks[sit_block_num_cur].entries[segnum % SIT_ENTRY_PER_BLOCK];
517 }
518
519 block_offset = (block - info->main_blkaddr) % info->blocks_per_segment;
520
521 if (block_offset == 0 && GET_SIT_VBLOCKS(sit_entry) == 0) {
522 block += info->blocks_per_segment;
523 continue;
524 }
525
526 used = f2fs_test_bit(block_offset, (char *)sit_entry->valid_map);
527 if(used)
528 if (func(block, data))
529 return -1;
530 }
531
532 block++;
533 }
534 return 0;
535 }
536
537 struct privdata
538 {
539 int count;
540 int infd;
541 int outfd;
542 char* buf;
543 char *zbuf;
544 int done;
545 struct f2fs_info *info;
546 };
547
548
549 /*
550 * This is a simple test program. It performs a block to block copy of a
551 * filesystem, replacing blocks identified as unused with 0's.
552 */
553
copy_used(uint64_t pos,void * data)554 int copy_used(uint64_t pos, void *data)
555 {
556 struct privdata *d = data;
557 char *buf;
558 int pdone = (pos * 100) / d->info->total_blocks;
559 if (pdone > d->done) {
560 d->done = pdone;
561 printf("Done with %d percent\n", d->done);
562 }
563
564 d->count++;
565 buf = d->buf;
566 if(read_structure_blk(d->infd, (unsigned long long)pos, d->buf, 1)) {
567 printf("Error reading!!!\n");
568 return -1;
569 }
570
571 off64_t ret;
572 ret = lseek64(d->outfd, pos * F2FS_BLKSIZE, SEEK_SET);
573 if (ret < 0) {
574 SLOGE("failed to seek\n");
575 return ret;
576 }
577
578 ret = write(d->outfd, d->buf, F2FS_BLKSIZE);
579 if (ret < 0) {
580 SLOGE("failed to write\n");
581 return ret;
582 }
583 if (ret != F2FS_BLKSIZE) {
584 SLOGE("failed to read all\n");
585 return -1;
586 }
587 return 0;
588 }
589
main(int argc,char ** argv)590 int main(int argc, char **argv)
591 {
592 if (argc != 3)
593 printf("Usage: %s fs_file_in fs_file_out\n", argv[0]);
594 char *in = argv[1];
595 char *out = argv[2];
596 int infd, outfd;
597
598 if ((infd = open(in, O_RDONLY)) < 0) {
599 SLOGE("Cannot open device");
600 return 0;
601 }
602 if ((outfd = open(out, O_WRONLY|O_CREAT, S_IRUSR | S_IWUSR)) < 0) {
603 SLOGE("Cannot open output");
604 return 0;
605 }
606
607 struct privdata d;
608 d.infd = infd;
609 d.outfd = outfd;
610 d.count = 0;
611 struct f2fs_info *info = generate_f2fs_info(infd);
612 if (!info) {
613 printf("Failed to generate info!");
614 return -1;
615 }
616 char *buf = malloc(F2FS_BLKSIZE);
617 char *zbuf = calloc(1, F2FS_BLKSIZE);
618 d.buf = buf;
619 d.zbuf = zbuf;
620 d.done = 0;
621 d.info = info;
622 int expected_count = get_num_blocks_used(info);
623 run_on_used_blocks(0, info, ©_used, &d);
624 printf("Copied %d blocks. Expected to copy %d\n", d.count, expected_count);
625 ftruncate64(outfd, info->total_blocks * F2FS_BLKSIZE);
626 free_f2fs_info(info);
627 free(buf);
628 free(zbuf);
629 close(infd);
630 close(outfd);
631 return 0;
632 }
633