-
Notifications
You must be signed in to change notification settings - Fork 2
/
convert.c
2815 lines (2530 loc) · 72 KB
/
convert.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2007 Oracle. All rights reserved.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public
* License v2 as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 021110-1307, USA.
*/
#define _XOPEN_SOURCE 600
#define _GNU_SOURCE 1
#include "kerncompat.h"
#ifndef __CHECKER__
#include <sys/ioctl.h>
#include <sys/mount.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/acl.h>
#include <fcntl.h>
#include <unistd.h>
#include <uuid/uuid.h>
#include <linux/fs.h>
#include "ctree.h"
#include "disk-io.h"
#include "volumes.h"
#include "transaction.h"
#include "crc32c.h"
#include "utils.h"
#include <ext2fs/ext2_fs.h>
#include <ext2fs/ext2fs.h>
#include <ext2fs/ext2_ext_attr.h>
#define INO_OFFSET (BTRFS_FIRST_FREE_OBJECTID - EXT2_ROOT_INO)
#define STRIPE_LEN (64 * 1024)
#define EXT2_IMAGE_SUBVOL_OBJECTID BTRFS_FIRST_FREE_OBJECTID
/*
* Open Ext2fs in readonly mode, read block allocation bitmap and
* inode bitmap into memory.
*/
static int open_ext2fs(const char *name, ext2_filsys *ret_fs)
{
errcode_t ret;
ext2_filsys ext2_fs;
ext2_ino_t ino;
ret = ext2fs_open(name, 0, 0, 0, unix_io_manager, &ext2_fs);
if (ret) {
fprintf(stderr, "ext2fs_open: %s\n", error_message(ret));
goto fail;
}
ret = ext2fs_read_inode_bitmap(ext2_fs);
if (ret) {
fprintf(stderr, "ext2fs_read_inode_bitmap: %s\n",
error_message(ret));
goto fail;
}
ret = ext2fs_read_block_bitmap(ext2_fs);
if (ret) {
fprintf(stderr, "ext2fs_read_block_bitmap: %s\n",
error_message(ret));
goto fail;
}
/*
* search each block group for a free inode. this set up
* uninit block/inode bitmaps appropriately.
*/
ino = 1;
while (ino <= ext2_fs->super->s_inodes_count) {
ext2_ino_t foo;
ext2fs_new_inode(ext2_fs, ino, 0, NULL, &foo);
ino += EXT2_INODES_PER_GROUP(ext2_fs->super);
}
*ret_fs = ext2_fs;
return 0;
fail:
return -1;
}
static int close_ext2fs(ext2_filsys fs)
{
ext2fs_close(fs);
return 0;
}
static int ext2_alloc_block(ext2_filsys fs, u64 goal, u64 *block_ret)
{
blk_t block;
if (!ext2fs_new_block(fs, goal, NULL, &block)) {
ext2fs_fast_mark_block_bitmap(fs->block_map, block);
*block_ret = block;
return 0;
}
return -ENOSPC;
}
static int ext2_free_block(ext2_filsys fs, u64 block)
{
BUG_ON(block != (blk_t)block);
ext2fs_fast_unmark_block_bitmap(fs->block_map, block);
return 0;
}
static int cache_free_extents(struct btrfs_root *root, ext2_filsys ext2_fs)
{
int i, ret = 0;
blk_t block;
u64 bytenr;
u64 blocksize = ext2_fs->blocksize;
block = ext2_fs->super->s_first_data_block;
for (; block < ext2_fs->super->s_blocks_count; block++) {
if (ext2fs_fast_test_block_bitmap(ext2_fs->block_map, block))
continue;
bytenr = block * blocksize;
ret = set_extent_dirty(&root->fs_info->free_space_cache,
bytenr, bytenr + blocksize - 1, 0);
BUG_ON(ret);
}
for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
bytenr = btrfs_sb_offset(i);
bytenr &= ~((u64)STRIPE_LEN - 1);
if (bytenr >= blocksize * ext2_fs->super->s_blocks_count)
break;
clear_extent_dirty(&root->fs_info->free_space_cache, bytenr,
bytenr + STRIPE_LEN - 1, 0);
}
clear_extent_dirty(&root->fs_info->free_space_cache,
0, BTRFS_SUPER_INFO_OFFSET - 1, 0);
return 0;
}
static int custom_alloc_extent(struct btrfs_root *root, u64 num_bytes,
u64 hint_byte, struct btrfs_key *ins)
{
u64 start;
u64 end;
u64 last = hint_byte;
int ret;
int wrapped = 0;
struct btrfs_block_group_cache *cache;
while(1) {
ret = find_first_extent_bit(&root->fs_info->free_space_cache,
last, &start, &end, EXTENT_DIRTY);
if (ret) {
if (wrapped++ == 0) {
last = 0;
continue;
} else {
goto fail;
}
}
start = max(last, start);
last = end + 1;
if (last - start < num_bytes)
continue;
last = start + num_bytes;
if (test_range_bit(&root->fs_info->pinned_extents,
start, last - 1, EXTENT_DIRTY, 0))
continue;
cache = btrfs_lookup_block_group(root->fs_info, start);
BUG_ON(!cache);
if (cache->flags & BTRFS_BLOCK_GROUP_SYSTEM ||
last > cache->key.objectid + cache->key.offset) {
last = cache->key.objectid + cache->key.offset;
continue;
}
clear_extent_dirty(&root->fs_info->free_space_cache,
start, start + num_bytes - 1, 0);
ins->objectid = start;
ins->offset = num_bytes;
ins->type = BTRFS_EXTENT_ITEM_KEY;
return 0;
}
fail:
fprintf(stderr, "not enough free space\n");
return -ENOSPC;
}
static int intersect_with_sb(u64 bytenr, u64 num_bytes)
{
int i;
u64 offset;
for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
offset = btrfs_sb_offset(i);
offset &= ~((u64)STRIPE_LEN - 1);
if (bytenr < offset + STRIPE_LEN &&
bytenr + num_bytes > offset)
return 1;
}
return 0;
}
static int custom_free_extent(struct btrfs_root *root, u64 bytenr,
u64 num_bytes)
{
return intersect_with_sb(bytenr, num_bytes);
}
struct btrfs_extent_ops extent_ops = {
.alloc_extent = custom_alloc_extent,
.free_extent = custom_free_extent,
};
struct dir_iterate_data {
struct btrfs_trans_handle *trans;
struct btrfs_root *root;
struct btrfs_inode_item *inode;
u64 objectid;
u64 index_cnt;
u64 parent;
int errcode;
};
static u8 filetype_conversion_table[EXT2_FT_MAX] = {
[EXT2_FT_UNKNOWN] = BTRFS_FT_UNKNOWN,
[EXT2_FT_REG_FILE] = BTRFS_FT_REG_FILE,
[EXT2_FT_DIR] = BTRFS_FT_DIR,
[EXT2_FT_CHRDEV] = BTRFS_FT_CHRDEV,
[EXT2_FT_BLKDEV] = BTRFS_FT_BLKDEV,
[EXT2_FT_FIFO] = BTRFS_FT_FIFO,
[EXT2_FT_SOCK] = BTRFS_FT_SOCK,
[EXT2_FT_SYMLINK] = BTRFS_FT_SYMLINK,
};
static int dir_iterate_proc(ext2_ino_t dir, int entry,
struct ext2_dir_entry *old,
int offset, int blocksize,
char *buf,void *priv_data)
{
int ret;
int file_type;
u64 objectid;
u64 inode_size;
char dotdot[] = "..";
struct btrfs_key location;
struct ext2_dir_entry_2 *dirent = (struct ext2_dir_entry_2 *)old;
struct dir_iterate_data *idata = (struct dir_iterate_data *)priv_data;
objectid = dirent->inode + INO_OFFSET;
if (!strncmp(dirent->name, dotdot, dirent->name_len)) {
if (dirent->name_len == 2) {
BUG_ON(idata->parent != 0);
idata->parent = objectid;
}
return 0;
}
if (dirent->inode < EXT2_GOOD_OLD_FIRST_INO)
return 0;
location.objectid = objectid;
location.offset = 0;
btrfs_set_key_type(&location, BTRFS_INODE_ITEM_KEY);
file_type = dirent->file_type;
BUG_ON(file_type > EXT2_FT_SYMLINK);
ret = btrfs_insert_dir_item(idata->trans, idata->root,
dirent->name, dirent->name_len,
idata->objectid, &location,
filetype_conversion_table[file_type],
idata->index_cnt);
if (ret)
goto fail;
ret = btrfs_insert_inode_ref(idata->trans, idata->root,
dirent->name, dirent->name_len,
objectid, idata->objectid,
idata->index_cnt);
if (ret)
goto fail;
idata->index_cnt++;
inode_size = btrfs_stack_inode_size(idata->inode) +
dirent->name_len * 2;
btrfs_set_stack_inode_size(idata->inode, inode_size);
return 0;
fail:
idata->errcode = ret;
return BLOCK_ABORT;
}
static int create_dir_entries(struct btrfs_trans_handle *trans,
struct btrfs_root *root, u64 objectid,
struct btrfs_inode_item *btrfs_inode,
ext2_filsys ext2_fs, ext2_ino_t ext2_ino)
{
int ret;
errcode_t err;
struct dir_iterate_data data = {
.trans = trans,
.root = root,
.inode = btrfs_inode,
.objectid = objectid,
.index_cnt = 2,
.parent = 0,
.errcode = 0,
};
err = ext2fs_dir_iterate2(ext2_fs, ext2_ino, 0, NULL,
dir_iterate_proc, &data);
if (err)
goto error;
ret = data.errcode;
if (ret == 0 && data.parent == objectid) {
ret = btrfs_insert_inode_ref(trans, root, "..", 2,
objectid, objectid, 0);
}
return ret;
error:
fprintf(stderr, "ext2fs_dir_iterate2: %s\n", error_message(err));
return -1;
}
static int read_disk_extent(struct btrfs_root *root, u64 bytenr,
u32 num_bytes, char *buffer)
{
int ret;
struct btrfs_fs_devices *fs_devs = root->fs_info->fs_devices;
ret = pread(fs_devs->latest_bdev, buffer, num_bytes, bytenr);
if (ret != num_bytes)
goto fail;
ret = 0;
fail:
if (ret > 0)
ret = -1;
return ret;
}
/*
* Record a file extent. Do all the required works, such as inserting
* file extent item, inserting extent item and backref item into extent
* tree and updating block accounting.
*/
static int record_file_extent(struct btrfs_trans_handle *trans,
struct btrfs_root *root, u64 objectid,
struct btrfs_inode_item *inode,
u64 file_pos, u64 disk_bytenr,
u64 num_bytes, int checksum)
{
int ret;
struct btrfs_fs_info *info = root->fs_info;
struct btrfs_root *extent_root = info->extent_root;
struct extent_buffer *leaf;
struct btrfs_file_extent_item *fi;
struct btrfs_key ins_key;
struct btrfs_path path;
struct btrfs_extent_item *ei;
u32 blocksize = root->sectorsize;
u64 nbytes;
if (disk_bytenr == 0) {
ret = btrfs_insert_file_extent(trans, root, objectid,
file_pos, disk_bytenr,
num_bytes, num_bytes);
return ret;
}
btrfs_init_path(&path);
if (checksum) {
u64 offset;
char *buffer;
ret = -ENOMEM;
buffer = malloc(blocksize);
if (!buffer)
goto fail;
for (offset = 0; offset < num_bytes; offset += blocksize) {
ret = read_disk_extent(root, disk_bytenr + offset,
blocksize, buffer);
if (ret)
break;
ret = btrfs_csum_file_block(trans,
root->fs_info->csum_root,
disk_bytenr + num_bytes,
disk_bytenr + offset,
buffer, blocksize);
if (ret)
break;
}
free(buffer);
if (ret)
goto fail;
}
ins_key.objectid = objectid;
ins_key.offset = file_pos;
btrfs_set_key_type(&ins_key, BTRFS_EXTENT_DATA_KEY);
ret = btrfs_insert_empty_item(trans, root, &path, &ins_key,
sizeof(*fi));
if (ret)
goto fail;
leaf = path.nodes[0];
fi = btrfs_item_ptr(leaf, path.slots[0],
struct btrfs_file_extent_item);
btrfs_set_file_extent_generation(leaf, fi, trans->transid);
btrfs_set_file_extent_type(leaf, fi, BTRFS_FILE_EXTENT_REG);
btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
btrfs_set_file_extent_disk_num_bytes(leaf, fi, num_bytes);
btrfs_set_file_extent_offset(leaf, fi, 0);
btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
btrfs_set_file_extent_ram_bytes(leaf, fi, num_bytes);
btrfs_set_file_extent_compression(leaf, fi, 0);
btrfs_set_file_extent_encryption(leaf, fi, 0);
btrfs_set_file_extent_other_encoding(leaf, fi, 0);
btrfs_mark_buffer_dirty(leaf);
nbytes = btrfs_stack_inode_nbytes(inode) + num_bytes;
btrfs_set_stack_inode_nbytes(inode, nbytes);
btrfs_release_path(root, &path);
ins_key.objectid = disk_bytenr;
ins_key.offset = num_bytes;
ins_key.type = BTRFS_EXTENT_ITEM_KEY;
ret = btrfs_insert_empty_item(trans, extent_root, &path,
&ins_key, sizeof(*ei));
if (ret == 0) {
leaf = path.nodes[0];
ei = btrfs_item_ptr(leaf, path.slots[0],
struct btrfs_extent_item);
btrfs_set_extent_refs(leaf, ei, 0);
btrfs_set_extent_generation(leaf, ei, 0);
btrfs_set_extent_flags(leaf, ei, BTRFS_EXTENT_FLAG_DATA);
btrfs_mark_buffer_dirty(leaf);
ret = btrfs_update_block_group(trans, root, disk_bytenr,
num_bytes, 1, 0);
if (ret)
goto fail;
} else if (ret != -EEXIST) {
goto fail;
}
btrfs_extent_post_op(trans, extent_root);
ret = btrfs_inc_extent_ref(trans, root, disk_bytenr, num_bytes, 0,
root->root_key.objectid,
objectid, file_pos);
if (ret)
goto fail;
ret = 0;
fail:
btrfs_release_path(root, &path);
return ret;
}
static int record_file_blocks(struct btrfs_trans_handle *trans,
struct btrfs_root *root, u64 objectid,
struct btrfs_inode_item *inode,
u64 file_block, u64 disk_block,
u64 num_blocks, int checksum)
{
u64 file_pos = file_block * root->sectorsize;
u64 disk_bytenr = disk_block * root->sectorsize;
u64 num_bytes = num_blocks * root->sectorsize;
return record_file_extent(trans, root, objectid, inode, file_pos,
disk_bytenr, num_bytes, checksum);
}
struct blk_iterate_data {
struct btrfs_trans_handle *trans;
struct btrfs_root *root;
struct btrfs_inode_item *inode;
u64 objectid;
u64 first_block;
u64 disk_block;
u64 num_blocks;
u64 boundary;
int checksum;
int errcode;
};
static int block_iterate_proc(ext2_filsys ext2_fs,
u64 disk_block, u64 file_block,
struct blk_iterate_data *idata)
{
int ret;
int sb_region;
int do_barrier;
struct btrfs_root *root = idata->root;
struct btrfs_trans_handle *trans = idata->trans;
struct btrfs_block_group_cache *cache;
u64 bytenr = disk_block * root->sectorsize;
sb_region = intersect_with_sb(bytenr, root->sectorsize);
do_barrier = sb_region || disk_block >= idata->boundary;
if ((idata->num_blocks > 0 && do_barrier) ||
(file_block > idata->first_block + idata->num_blocks) ||
(disk_block != idata->disk_block + idata->num_blocks)) {
if (idata->num_blocks > 0) {
ret = record_file_blocks(trans, root, idata->objectid,
idata->inode, idata->first_block,
idata->disk_block, idata->num_blocks,
idata->checksum);
if (ret)
goto fail;
idata->first_block += idata->num_blocks;
idata->num_blocks = 0;
}
if (file_block > idata->first_block) {
ret = record_file_blocks(trans, root, idata->objectid,
idata->inode, idata->first_block,
0, file_block - idata->first_block,
idata->checksum);
if (ret)
goto fail;
}
if (sb_region) {
bytenr += STRIPE_LEN - 1;
bytenr &= ~((u64)STRIPE_LEN - 1);
} else {
cache = btrfs_lookup_block_group(root->fs_info, bytenr);
BUG_ON(!cache);
bytenr = cache->key.objectid + cache->key.offset;
}
idata->first_block = file_block;
idata->disk_block = disk_block;
idata->boundary = bytenr / root->sectorsize;
}
idata->num_blocks++;
return 0;
fail:
idata->errcode = ret;
return BLOCK_ABORT;
}
static int __block_iterate_proc(ext2_filsys fs, blk_t *blocknr,
e2_blkcnt_t blockcnt, blk_t ref_block,
int ref_offset, void *priv_data)
{
struct blk_iterate_data *idata;
idata = (struct blk_iterate_data *)priv_data;
return block_iterate_proc(fs, *blocknr, blockcnt, idata);
}
/*
* traverse file's data blocks, record these data blocks as file extents.
*/
static int create_file_extents(struct btrfs_trans_handle *trans,
struct btrfs_root *root, u64 objectid,
struct btrfs_inode_item *btrfs_inode,
ext2_filsys ext2_fs, ext2_ino_t ext2_ino,
int datacsum, int packing)
{
int ret;
char *buffer = NULL;
errcode_t err;
u32 last_block;
u32 sectorsize = root->sectorsize;
u64 inode_size = btrfs_stack_inode_size(btrfs_inode);
struct blk_iterate_data data = {
.trans = trans,
.root = root,
.inode = btrfs_inode,
.objectid = objectid,
.first_block = 0,
.disk_block = 0,
.num_blocks = 0,
.boundary = (u64)-1,
.checksum = datacsum,
.errcode = 0,
};
err = ext2fs_block_iterate2(ext2_fs, ext2_ino, BLOCK_FLAG_DATA_ONLY,
NULL, __block_iterate_proc, &data);
if (err)
goto error;
ret = data.errcode;
if (ret)
goto fail;
if (packing && data.first_block == 0 && data.num_blocks > 0 &&
inode_size <= BTRFS_MAX_INLINE_DATA_SIZE(root)) {
u64 num_bytes = data.num_blocks * sectorsize;
u64 disk_bytenr = data.disk_block * sectorsize;
u64 nbytes;
buffer = malloc(num_bytes);
if (!buffer)
return -ENOMEM;
ret = read_disk_extent(root, disk_bytenr, num_bytes, buffer);
if (ret)
goto fail;
if (num_bytes > inode_size)
num_bytes = inode_size;
ret = btrfs_insert_inline_extent(trans, root, objectid,
0, buffer, num_bytes);
if (ret)
goto fail;
nbytes = btrfs_stack_inode_nbytes(btrfs_inode) + num_bytes;
btrfs_set_stack_inode_nbytes(btrfs_inode, nbytes);
} else if (data.num_blocks > 0) {
ret = record_file_blocks(trans, root, objectid, btrfs_inode,
data.first_block, data.disk_block,
data.num_blocks, data.checksum);
if (ret)
goto fail;
}
data.first_block += data.num_blocks;
last_block = (inode_size + sectorsize - 1) / sectorsize;
if (last_block > data.first_block) {
ret = record_file_blocks(trans, root, objectid, btrfs_inode,
data.first_block, 0, last_block -
data.first_block, data.checksum);
}
fail:
if (buffer)
free(buffer);
return ret;
error:
fprintf(stderr, "ext2fs_block_iterate2: %s\n", error_message(err));
return -1;
}
static int create_symbol_link(struct btrfs_trans_handle *trans,
struct btrfs_root *root, u64 objectid,
struct btrfs_inode_item *btrfs_inode,
ext2_filsys ext2_fs, ext2_ino_t ext2_ino,
struct ext2_inode *ext2_inode)
{
int ret;
char *pathname;
u64 inode_size = btrfs_stack_inode_size(btrfs_inode);
if (ext2fs_inode_data_blocks(ext2_fs, ext2_inode)) {
btrfs_set_stack_inode_size(btrfs_inode, inode_size + 1);
ret = create_file_extents(trans, root, objectid, btrfs_inode,
ext2_fs, ext2_ino, 1, 1);
btrfs_set_stack_inode_size(btrfs_inode, inode_size);
return ret;
}
pathname = (char *)&(ext2_inode->i_block[0]);
BUG_ON(pathname[inode_size] != 0);
ret = btrfs_insert_inline_extent(trans, root, objectid, 0,
pathname, inode_size + 1);
btrfs_set_stack_inode_nbytes(btrfs_inode, inode_size + 1);
return ret;
}
/*
* Following xattr/acl related codes are based on codes in
* fs/ext3/xattr.c and fs/ext3/acl.c
*/
#define EXT2_XATTR_BHDR(ptr) ((struct ext2_ext_attr_header *)(ptr))
#define EXT2_XATTR_BFIRST(ptr) \
((struct ext2_ext_attr_entry *)(EXT2_XATTR_BHDR(ptr) + 1))
#define EXT2_XATTR_IHDR(inode) \
((struct ext2_ext_attr_header *) ((void *)(inode) + \
EXT2_GOOD_OLD_INODE_SIZE + (inode)->i_extra_isize))
#define EXT2_XATTR_IFIRST(inode) \
((struct ext2_ext_attr_entry *) ((void *)EXT2_XATTR_IHDR(inode) + \
sizeof(EXT2_XATTR_IHDR(inode)->h_magic)))
static int ext2_xattr_check_names(struct ext2_ext_attr_entry *entry,
const void *end)
{
struct ext2_ext_attr_entry *next;
while (!EXT2_EXT_IS_LAST_ENTRY(entry)) {
next = EXT2_EXT_ATTR_NEXT(entry);
if ((void *)next >= end)
return -EIO;
entry = next;
}
return 0;
}
static int ext2_xattr_check_block(const char *buf, size_t size)
{
int error;
struct ext2_ext_attr_header *header = EXT2_XATTR_BHDR(buf);
if (header->h_magic != EXT2_EXT_ATTR_MAGIC ||
header->h_blocks != 1)
return -EIO;
error = ext2_xattr_check_names(EXT2_XATTR_BFIRST(buf), buf + size);
return error;
}
static int ext2_xattr_check_entry(struct ext2_ext_attr_entry *entry,
size_t size)
{
size_t value_size = entry->e_value_size;
if (entry->e_value_block != 0 || value_size > size ||
entry->e_value_offs + value_size > size)
return -EIO;
return 0;
}
#define EXT2_ACL_VERSION 0x0001
typedef struct {
__le16 e_tag;
__le16 e_perm;
__le32 e_id;
} ext2_acl_entry;
typedef struct {
__le16 e_tag;
__le16 e_perm;
} ext2_acl_entry_short;
typedef struct {
__le32 a_version;
} ext2_acl_header;
static inline int ext2_acl_count(size_t size)
{
ssize_t s;
size -= sizeof(ext2_acl_header);
s = size - 4 * sizeof(ext2_acl_entry_short);
if (s < 0) {
if (size % sizeof(ext2_acl_entry_short))
return -1;
return size / sizeof(ext2_acl_entry_short);
} else {
if (s % sizeof(ext2_acl_entry))
return -1;
return s / sizeof(ext2_acl_entry) + 4;
}
}
#define ACL_EA_VERSION 0x0002
typedef struct {
__le16 e_tag;
__le16 e_perm;
__le32 e_id;
} acl_ea_entry;
typedef struct {
__le32 a_version;
acl_ea_entry a_entries[0];
} acl_ea_header;
static inline size_t acl_ea_size(int count)
{
return sizeof(acl_ea_header) + count * sizeof(acl_ea_entry);
}
static int ext2_acl_to_xattr(void *dst, const void *src,
size_t dst_size, size_t src_size)
{
int i, count;
const void *end = src + src_size;
acl_ea_header *ext_acl = (acl_ea_header *)dst;
acl_ea_entry *dst_entry = ext_acl->a_entries;
ext2_acl_entry *src_entry;
if (src_size < sizeof(ext2_acl_header))
goto fail;
if (((ext2_acl_header *)src)->a_version !=
cpu_to_le32(EXT2_ACL_VERSION))
goto fail;
src += sizeof(ext2_acl_header);
count = ext2_acl_count(src_size);
if (count <= 0)
goto fail;
BUG_ON(dst_size < acl_ea_size(count));
ext_acl->a_version = cpu_to_le32(ACL_EA_VERSION);
for (i = 0; i < count; i++, dst_entry++) {
src_entry = (ext2_acl_entry *)src;
if (src + sizeof(ext2_acl_entry_short) > end)
goto fail;
dst_entry->e_tag = src_entry->e_tag;
dst_entry->e_perm = src_entry->e_perm;
switch (le16_to_cpu(src_entry->e_tag)) {
case ACL_USER_OBJ:
case ACL_GROUP_OBJ:
case ACL_MASK:
case ACL_OTHER:
src += sizeof(ext2_acl_entry_short);
dst_entry->e_id = cpu_to_le32(ACL_UNDEFINED_ID);
break;
case ACL_USER:
case ACL_GROUP:
src += sizeof(ext2_acl_entry);
if (src > end)
goto fail;
dst_entry->e_id = src_entry->e_id;
break;
default:
goto fail;
}
}
if (src != end)
goto fail;
return 0;
fail:
return -EINVAL;
}
static char *xattr_prefix_table[] = {
[1] = "user.",
[2] = "system.posix_acl_access",
[3] = "system.posix_acl_default",
[4] = "trusted.",
[6] = "security.",
};
static int copy_single_xattr(struct btrfs_trans_handle *trans,
struct btrfs_root *root, u64 objectid,
struct ext2_ext_attr_entry *entry,
const void *data, u32 datalen)
{
int ret = 0;
int name_len;
int name_index;
void *databuf = NULL;
char namebuf[XATTR_NAME_MAX + 1];
name_index = entry->e_name_index;
if (name_index >= ARRAY_SIZE(xattr_prefix_table) ||
xattr_prefix_table[name_index] == NULL)
return -EOPNOTSUPP;
name_len = strlen(xattr_prefix_table[name_index]) +
entry->e_name_len;
if (name_len >= sizeof(namebuf))
return -ERANGE;
if (name_index == 2 || name_index == 3) {
size_t bufsize = acl_ea_size(ext2_acl_count(datalen));
databuf = malloc(bufsize);
if (!databuf)
return -ENOMEM;
ret = ext2_acl_to_xattr(databuf, data, bufsize, datalen);
if (ret)
goto out;
data = databuf;
datalen = bufsize;
}
strncpy(namebuf, xattr_prefix_table[name_index], XATTR_NAME_MAX);
strncat(namebuf, EXT2_EXT_ATTR_NAME(entry), entry->e_name_len);
if (name_len + datalen > BTRFS_LEAF_DATA_SIZE(root) -
sizeof(struct btrfs_item) - sizeof(struct btrfs_dir_item)) {
fprintf(stderr, "skip large xattr on inode %Lu name %.*s\n",
objectid - INO_OFFSET, name_len, namebuf);
goto out;
}
ret = btrfs_insert_xattr_item(trans, root, namebuf, name_len,
data, datalen, objectid);
out:
if (databuf)
free(databuf);
return ret;
}
static int copy_extended_attrs(struct btrfs_trans_handle *trans,
struct btrfs_root *root, u64 objectid,
struct btrfs_inode_item *btrfs_inode,
ext2_filsys ext2_fs, ext2_ino_t ext2_ino)
{
int ret = 0;
int inline_ea = 0;
errcode_t err;
u32 datalen;
u32 block_size = ext2_fs->blocksize;
u32 inode_size = EXT2_INODE_SIZE(ext2_fs->super);
struct ext2_inode_large *ext2_inode;
struct ext2_ext_attr_entry *entry;
void *data;
char *buffer = NULL;
char inode_buf[EXT2_GOOD_OLD_INODE_SIZE];
if (inode_size <= EXT2_GOOD_OLD_INODE_SIZE) {
ext2_inode = (struct ext2_inode_large *)inode_buf;
} else {
ext2_inode = (struct ext2_inode_large *)malloc(inode_size);
if (!ext2_inode)
return -ENOMEM;
}
err = ext2fs_read_inode_full(ext2_fs, ext2_ino, (void *)ext2_inode,
inode_size);
if (err) {
fprintf(stderr, "ext2fs_read_inode_full: %s\n",
error_message(err));
ret = -1;
goto out;
}
if (ext2_ino > ext2_fs->super->s_first_ino &&
inode_size > EXT2_GOOD_OLD_INODE_SIZE) {
if (EXT2_GOOD_OLD_INODE_SIZE +
ext2_inode->i_extra_isize > inode_size) {
ret = -EIO;
goto out;
}
if (ext2_inode->i_extra_isize != 0 &&
EXT2_XATTR_IHDR(ext2_inode)->h_magic ==
EXT2_EXT_ATTR_MAGIC) {
inline_ea = 1;
}
}
if (inline_ea) {
int total;
void *end = (void *)ext2_inode + inode_size;
entry = EXT2_XATTR_IFIRST(ext2_inode);
total = end - (void *)entry;
ret = ext2_xattr_check_names(entry, end);
if (ret)
goto out;
while (!EXT2_EXT_IS_LAST_ENTRY(entry)) {
ret = ext2_xattr_check_entry(entry, total);
if (ret)
goto out;
data = (void *)EXT2_XATTR_IFIRST(ext2_inode) +
entry->e_value_offs;
datalen = entry->e_value_size;
ret = copy_single_xattr(trans, root, objectid,
entry, data, datalen);
if (ret)
goto out;
entry = EXT2_EXT_ATTR_NEXT(entry);
}
}
if (ext2_inode->i_file_acl == 0)
goto out;
buffer = malloc(block_size);
if (!buffer) {
ret = -ENOMEM;
goto out;
}
err = ext2fs_read_ext_attr(ext2_fs, ext2_inode->i_file_acl, buffer);
if (err) {
fprintf(stderr, "ext2fs_read_ext_attr: %s\n",
error_message(err));
ret = -1;
goto out;
}
ret = ext2_xattr_check_block(buffer, block_size);
if (ret)
goto out;
entry = EXT2_XATTR_BFIRST(buffer);
while (!EXT2_EXT_IS_LAST_ENTRY(entry)) {
ret = ext2_xattr_check_entry(entry, block_size);
if (ret)
goto out;
data = buffer + entry->e_value_offs;
datalen = entry->e_value_size;
ret = copy_single_xattr(trans, root, objectid,
entry, data, datalen);
if (ret)
goto out;
entry = EXT2_EXT_ATTR_NEXT(entry);
}
out:
if (buffer != NULL)
free(buffer);
if ((void *)ext2_inode != inode_buf)
free(ext2_inode);
return ret;
}
#define MINORBITS 20
#define MKDEV(ma, mi) (((ma) << MINORBITS) | (mi))
static inline dev_t old_decode_dev(u16 val)
{
return MKDEV((val >> 8) & 255, val & 255);
}
static inline dev_t new_decode_dev(u32 dev)
{
unsigned major = (dev & 0xfff00) >> 8;
unsigned minor = (dev & 0xff) | ((dev >> 12) & 0xfff00);
return MKDEV(major, minor);
}