btrfs-progs: enclose uuid tree compat code with ifdefs
[platform/upstream/btrfs-progs.git] / btrfs-convert.c
1 /*
2  * Copyright (C) 2007 Oracle.  All rights reserved.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public
6  * License v2 as published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public
14  * License along with this program; if not, write to the
15  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16  * Boston, MA 021110-1307, USA.
17  */
18
19 #define _XOPEN_SOURCE 600
20 #define _GNU_SOURCE 1
21
22 #include "kerncompat.h"
23
24 #include <sys/ioctl.h>
25 #include <sys/mount.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <sys/types.h>
29 #include <sys/stat.h>
30 #include <sys/acl.h>
31 #include <fcntl.h>
32 #include <unistd.h>
33 #include <uuid/uuid.h>
34
35 #include "ctree.h"
36 #include "disk-io.h"
37 #include "volumes.h"
38 #include "transaction.h"
39 #include "crc32c.h"
40 #include "utils.h"
41 #include <ext2fs/ext2_fs.h>
42 #include <ext2fs/ext2fs.h>
43 #include <ext2fs/ext2_ext_attr.h>
44
45 #define INO_OFFSET (BTRFS_FIRST_FREE_OBJECTID - EXT2_ROOT_INO)
46 #define EXT2_IMAGE_SUBVOL_OBJECTID BTRFS_FIRST_FREE_OBJECTID
47
48 /*
49  * Open Ext2fs in readonly mode, read block allocation bitmap and
50  * inode bitmap into memory.
51  */
52 static int open_ext2fs(const char *name, ext2_filsys *ret_fs)
53 {
54         errcode_t ret;
55         ext2_filsys ext2_fs;
56         ext2_ino_t ino;
57         ret = ext2fs_open(name, 0, 0, 0, unix_io_manager, &ext2_fs);
58         if (ret) {
59                 fprintf(stderr, "ext2fs_open: %s\n", error_message(ret));
60                 goto fail;
61         }
62         ret = ext2fs_read_inode_bitmap(ext2_fs);
63         if (ret) {
64                 fprintf(stderr, "ext2fs_read_inode_bitmap: %s\n",
65                         error_message(ret));
66                 goto fail;
67         }
68         ret = ext2fs_read_block_bitmap(ext2_fs);
69         if (ret) {
70                 fprintf(stderr, "ext2fs_read_block_bitmap: %s\n",
71                         error_message(ret));
72                 goto fail;
73         }
74         /*
75          * search each block group for a free inode. this set up
76          * uninit block/inode bitmaps appropriately.
77          */
78         ino = 1;
79         while (ino <= ext2_fs->super->s_inodes_count) {
80                 ext2_ino_t foo;
81                 ext2fs_new_inode(ext2_fs, ino, 0, NULL, &foo);
82                 ino += EXT2_INODES_PER_GROUP(ext2_fs->super);
83         }
84
85         *ret_fs = ext2_fs;
86         return 0;
87 fail:
88         return -1;
89 }
90
91 static int close_ext2fs(ext2_filsys fs)
92 {
93         ext2fs_close(fs);
94         return 0;
95 }
96
97 static int ext2_alloc_block(ext2_filsys fs, u64 goal, u64 *block_ret)
98 {
99         blk_t block;
100
101         if (!ext2fs_new_block(fs, goal, NULL, &block)) {
102                 ext2fs_fast_mark_block_bitmap(fs->block_map, block);
103                 *block_ret = block;
104                 return 0;
105         }
106         return -ENOSPC;
107 }
108
109 static int ext2_free_block(ext2_filsys fs, u64 block)
110 {
111         BUG_ON(block != (blk_t)block);
112         ext2fs_fast_unmark_block_bitmap(fs->block_map, block);
113         return 0;
114 }
115
116 static int cache_free_extents(struct btrfs_root *root, ext2_filsys ext2_fs)
117
118 {
119         int i, ret = 0;
120         blk_t block;
121         u64 bytenr;
122         u64 blocksize = ext2_fs->blocksize;
123
124         block = ext2_fs->super->s_first_data_block;
125         for (; block < ext2_fs->super->s_blocks_count; block++) {
126                 if (ext2fs_fast_test_block_bitmap(ext2_fs->block_map, block))
127                         continue;
128                 bytenr = block * blocksize;
129                 ret = set_extent_dirty(&root->fs_info->free_space_cache,
130                                        bytenr, bytenr + blocksize - 1, 0);
131                 BUG_ON(ret);
132         }
133
134         for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
135                 bytenr = btrfs_sb_offset(i);
136                 bytenr &= ~((u64)BTRFS_STRIPE_LEN - 1);
137                 if (bytenr >= blocksize * ext2_fs->super->s_blocks_count)
138                         break;
139                 clear_extent_dirty(&root->fs_info->free_space_cache, bytenr,
140                                    bytenr + BTRFS_STRIPE_LEN - 1, 0);
141         }
142
143         clear_extent_dirty(&root->fs_info->free_space_cache,
144                            0, BTRFS_SUPER_INFO_OFFSET - 1, 0);
145
146         return 0;
147 }
148
149 static int custom_alloc_extent(struct btrfs_root *root, u64 num_bytes,
150                                u64 hint_byte, struct btrfs_key *ins)
151 {
152         u64 start;
153         u64 end;
154         u64 last = hint_byte;
155         int ret;
156         int wrapped = 0;
157         struct btrfs_block_group_cache *cache;
158
159         while(1) {
160                 ret = find_first_extent_bit(&root->fs_info->free_space_cache,
161                                             last, &start, &end, EXTENT_DIRTY);
162                 if (ret) {
163                         if (wrapped++ == 0) {
164                                 last = 0;
165                                 continue;
166                         } else {
167                                 goto fail;
168                         }
169                 }
170
171                 start = max(last, start);
172                 last = end + 1;
173                 if (last - start < num_bytes)
174                         continue;
175
176                 last = start + num_bytes;
177                 if (test_range_bit(&root->fs_info->pinned_extents,
178                                    start, last - 1, EXTENT_DIRTY, 0))
179                         continue;
180
181                 cache = btrfs_lookup_block_group(root->fs_info, start);
182                 BUG_ON(!cache);
183                 if (cache->flags & BTRFS_BLOCK_GROUP_SYSTEM ||
184                     last > cache->key.objectid + cache->key.offset) {
185                         last = cache->key.objectid + cache->key.offset;
186                         continue;
187                 }
188
189                 clear_extent_dirty(&root->fs_info->free_space_cache,
190                                    start, start + num_bytes - 1, 0);
191
192                 ins->objectid = start;
193                 ins->offset = num_bytes;
194                 ins->type = BTRFS_EXTENT_ITEM_KEY;
195                 return 0;
196         }
197 fail:
198         fprintf(stderr, "not enough free space\n");
199         return -ENOSPC;
200 }
201
202 static int intersect_with_sb(u64 bytenr, u64 num_bytes)
203 {
204         int i;
205         u64 offset;
206
207         for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
208                 offset = btrfs_sb_offset(i);
209                 offset &= ~((u64)BTRFS_STRIPE_LEN - 1);
210
211                 if (bytenr < offset + BTRFS_STRIPE_LEN &&
212                     bytenr + num_bytes > offset)
213                         return 1;
214         }
215         return 0;
216 }
217
218 static int custom_free_extent(struct btrfs_root *root, u64 bytenr,
219                               u64 num_bytes)
220 {
221         return intersect_with_sb(bytenr, num_bytes);
222 }
223
224 static struct btrfs_extent_ops extent_ops = {
225         .alloc_extent = custom_alloc_extent,
226         .free_extent = custom_free_extent,
227 };
228
229 struct dir_iterate_data {
230         struct btrfs_trans_handle *trans;
231         struct btrfs_root *root;
232         struct btrfs_inode_item *inode;
233         u64 objectid;
234         u64 index_cnt;
235         u64 parent;
236         int errcode;
237 };
238
239 static u8 filetype_conversion_table[EXT2_FT_MAX] = {
240         [EXT2_FT_UNKNOWN]       = BTRFS_FT_UNKNOWN,
241         [EXT2_FT_REG_FILE]      = BTRFS_FT_REG_FILE,
242         [EXT2_FT_DIR]           = BTRFS_FT_DIR,
243         [EXT2_FT_CHRDEV]        = BTRFS_FT_CHRDEV,
244         [EXT2_FT_BLKDEV]        = BTRFS_FT_BLKDEV,
245         [EXT2_FT_FIFO]          = BTRFS_FT_FIFO,
246         [EXT2_FT_SOCK]          = BTRFS_FT_SOCK,
247         [EXT2_FT_SYMLINK]       = BTRFS_FT_SYMLINK,
248 };
249
250 static int dir_iterate_proc(ext2_ino_t dir, int entry,
251                             struct ext2_dir_entry *dirent,
252                             int offset, int blocksize,
253                             char *buf,void *priv_data)
254 {
255         int ret;
256         int file_type;
257         u64 objectid;
258         u64 inode_size;
259         char dotdot[] = "..";
260         struct btrfs_key location;
261         struct dir_iterate_data *idata = (struct dir_iterate_data *)priv_data;
262         int name_len;
263
264         name_len = dirent->name_len & 0xFF;
265
266         objectid = dirent->inode + INO_OFFSET;
267         if (!strncmp(dirent->name, dotdot, name_len)) {
268                 if (name_len == 2) {
269                         BUG_ON(idata->parent != 0);
270                         idata->parent = objectid;
271                 }
272                 return 0;
273         }
274         if (dirent->inode < EXT2_GOOD_OLD_FIRST_INO)
275                 return 0;
276
277         location.objectid = objectid;
278         location.offset = 0;
279         btrfs_set_key_type(&location, BTRFS_INODE_ITEM_KEY);
280
281         file_type = dirent->name_len >> 8;
282         BUG_ON(file_type > EXT2_FT_SYMLINK);
283         ret = btrfs_insert_dir_item(idata->trans, idata->root,
284                                     dirent->name, name_len,
285                                     idata->objectid, &location,
286                                     filetype_conversion_table[file_type],
287                                     idata->index_cnt);
288         if (ret)
289                 goto fail;
290         ret = btrfs_insert_inode_ref(idata->trans, idata->root,
291                                      dirent->name, name_len,
292                                      objectid, idata->objectid,
293                                      idata->index_cnt);
294         if (ret)
295                 goto fail;
296         idata->index_cnt++;
297         inode_size = btrfs_stack_inode_size(idata->inode) +
298                      name_len * 2;
299         btrfs_set_stack_inode_size(idata->inode, inode_size);
300         return 0;
301 fail:
302         idata->errcode = ret;
303         return BLOCK_ABORT;
304 }
305
306 static int create_dir_entries(struct btrfs_trans_handle *trans,
307                               struct btrfs_root *root, u64 objectid,
308                               struct btrfs_inode_item *btrfs_inode,
309                               ext2_filsys ext2_fs, ext2_ino_t ext2_ino)
310 {
311         int ret;
312         errcode_t err;
313         struct dir_iterate_data data = {
314                 .trans          = trans,
315                 .root           = root,
316                 .inode          = btrfs_inode,
317                 .objectid       = objectid,
318                 .index_cnt      = 2,
319                 .parent         = 0,
320                 .errcode        = 0,
321         };
322
323         err = ext2fs_dir_iterate2(ext2_fs, ext2_ino, 0, NULL,
324                                   dir_iterate_proc, &data);
325         if (err)
326                 goto error;
327         ret = data.errcode;
328         if (ret == 0 && data.parent == objectid) {
329                 ret = btrfs_insert_inode_ref(trans, root, "..", 2,
330                                              objectid, objectid, 0);
331         }
332         return ret;
333 error:
334         fprintf(stderr, "ext2fs_dir_iterate2: %s\n", error_message(err));
335         return -1;
336 }
337
338 static int read_disk_extent(struct btrfs_root *root, u64 bytenr,
339                             u32 num_bytes, char *buffer)
340 {
341         int ret;
342         struct btrfs_fs_devices *fs_devs = root->fs_info->fs_devices;
343
344         ret = pread(fs_devs->latest_bdev, buffer, num_bytes, bytenr);
345         if (ret != num_bytes)
346                 goto fail;
347         ret = 0;
348 fail:
349         if (ret > 0)
350                 ret = -1;
351         return ret;
352 }
353
354 static int csum_disk_extent(struct btrfs_trans_handle *trans,
355                             struct btrfs_root *root,
356                             u64 disk_bytenr, u64 num_bytes)
357 {
358         u32 blocksize = root->sectorsize;
359         u64 offset;
360         char *buffer;
361         int ret = 0;
362
363         buffer = malloc(blocksize);
364         if (!buffer)
365                 return -ENOMEM;
366         for (offset = 0; offset < num_bytes; offset += blocksize) {
367                 ret = read_disk_extent(root, disk_bytenr + offset,
368                                         blocksize, buffer);
369                 if (ret)
370                         break;
371                 ret = btrfs_csum_file_block(trans,
372                                             root->fs_info->csum_root,
373                                             disk_bytenr + num_bytes,
374                                             disk_bytenr + offset,
375                                             buffer, blocksize);
376                 if (ret)
377                         break;
378         }
379         free(buffer);
380         return ret;
381 }
382
383 static int record_file_blocks(struct btrfs_trans_handle *trans,
384                               struct btrfs_root *root, u64 objectid,
385                               struct btrfs_inode_item *inode,
386                               u64 file_block, u64 disk_block,
387                               u64 num_blocks, int checksum)
388 {
389         int ret;
390         u64 file_pos = file_block * root->sectorsize;
391         u64 disk_bytenr = disk_block * root->sectorsize;
392         u64 num_bytes = num_blocks * root->sectorsize;
393         ret = btrfs_record_file_extent(trans, root, objectid, inode, file_pos,
394                                         disk_bytenr, num_bytes);
395
396         if (ret || !checksum)
397                 return ret;
398
399         return csum_disk_extent(trans, root, disk_bytenr, num_bytes);
400 }
401
402 struct blk_iterate_data {
403         struct btrfs_trans_handle *trans;
404         struct btrfs_root *root;
405         struct btrfs_inode_item *inode;
406         u64 objectid;
407         u64 first_block;
408         u64 disk_block;
409         u64 num_blocks;
410         u64 boundary;
411         int checksum;
412         int errcode;
413 };
414
415 static int block_iterate_proc(ext2_filsys ext2_fs,
416                               u64 disk_block, u64 file_block,
417                               struct blk_iterate_data *idata)
418 {
419         int ret;
420         int sb_region;
421         int do_barrier;
422         struct btrfs_root *root = idata->root;
423         struct btrfs_trans_handle *trans = idata->trans;
424         struct btrfs_block_group_cache *cache;
425         u64 bytenr = disk_block * root->sectorsize;
426
427         sb_region = intersect_with_sb(bytenr, root->sectorsize);
428         do_barrier = sb_region || disk_block >= idata->boundary;
429         if ((idata->num_blocks > 0 && do_barrier) ||
430             (file_block > idata->first_block + idata->num_blocks) ||
431             (disk_block != idata->disk_block + idata->num_blocks)) {
432                 if (idata->num_blocks > 0) {
433                         ret = record_file_blocks(trans, root, idata->objectid,
434                                         idata->inode, idata->first_block,
435                                         idata->disk_block, idata->num_blocks,
436                                         idata->checksum);
437                         if (ret)
438                                 goto fail;
439                         idata->first_block += idata->num_blocks;
440                         idata->num_blocks = 0;
441                 }
442                 if (file_block > idata->first_block) {
443                         ret = record_file_blocks(trans, root, idata->objectid,
444                                         idata->inode, idata->first_block,
445                                         0, file_block - idata->first_block,
446                                         idata->checksum);
447                         if (ret)
448                                 goto fail;
449                 }
450
451                 if (sb_region) {
452                         bytenr += BTRFS_STRIPE_LEN - 1;
453                         bytenr &= ~((u64)BTRFS_STRIPE_LEN - 1);
454                 } else {
455                         cache = btrfs_lookup_block_group(root->fs_info, bytenr);
456                         BUG_ON(!cache);
457                         bytenr = cache->key.objectid + cache->key.offset;
458                 }
459
460                 idata->first_block = file_block;
461                 idata->disk_block = disk_block;
462                 idata->boundary = bytenr / root->sectorsize;
463         }
464         idata->num_blocks++;
465         return 0;
466 fail:
467         idata->errcode = ret;
468         return BLOCK_ABORT;
469 }
470
471 static int __block_iterate_proc(ext2_filsys fs, blk_t *blocknr,
472                                 e2_blkcnt_t blockcnt, blk_t ref_block,
473                                 int ref_offset, void *priv_data)
474 {
475         struct blk_iterate_data *idata;
476         idata = (struct blk_iterate_data *)priv_data;
477         return block_iterate_proc(fs, *blocknr, blockcnt, idata);
478 }
479
480 /*
481  * traverse file's data blocks, record these data blocks as file extents.
482  */
483 static int create_file_extents(struct btrfs_trans_handle *trans,
484                                struct btrfs_root *root, u64 objectid,
485                                struct btrfs_inode_item *btrfs_inode,
486                                ext2_filsys ext2_fs, ext2_ino_t ext2_ino,
487                                int datacsum, int packing)
488 {
489         int ret;
490         char *buffer = NULL;
491         errcode_t err;
492         u32 last_block;
493         u32 sectorsize = root->sectorsize;
494         u64 inode_size = btrfs_stack_inode_size(btrfs_inode);
495         struct blk_iterate_data data = {
496                 .trans          = trans,
497                 .root           = root,
498                 .inode          = btrfs_inode,
499                 .objectid       = objectid,
500                 .first_block    = 0,
501                 .disk_block     = 0,
502                 .num_blocks     = 0,
503                 .boundary       = (u64)-1,
504                 .checksum       = datacsum,
505                 .errcode        = 0,
506         };
507         err = ext2fs_block_iterate2(ext2_fs, ext2_ino, BLOCK_FLAG_DATA_ONLY,
508                                     NULL, __block_iterate_proc, &data);
509         if (err)
510                 goto error;
511         ret = data.errcode;
512         if (ret)
513                 goto fail;
514         if (packing && data.first_block == 0 && data.num_blocks > 0 &&
515             inode_size <= BTRFS_MAX_INLINE_DATA_SIZE(root)) {
516                 u64 num_bytes = data.num_blocks * sectorsize;
517                 u64 disk_bytenr = data.disk_block * sectorsize;
518                 u64 nbytes;
519
520                 buffer = malloc(num_bytes);
521                 if (!buffer)
522                         return -ENOMEM;
523                 ret = read_disk_extent(root, disk_bytenr, num_bytes, buffer);
524                 if (ret)
525                         goto fail;
526                 if (num_bytes > inode_size)
527                         num_bytes = inode_size;
528                 ret = btrfs_insert_inline_extent(trans, root, objectid,
529                                                  0, buffer, num_bytes);
530                 if (ret)
531                         goto fail;
532                 nbytes = btrfs_stack_inode_nbytes(btrfs_inode) + num_bytes;
533                 btrfs_set_stack_inode_nbytes(btrfs_inode, nbytes);
534         } else if (data.num_blocks > 0) {
535                 ret = record_file_blocks(trans, root, objectid, btrfs_inode,
536                                          data.first_block, data.disk_block,
537                                          data.num_blocks, data.checksum);
538                 if (ret)
539                         goto fail;
540         }
541         data.first_block += data.num_blocks;
542         last_block = (inode_size + sectorsize - 1) / sectorsize;
543         if (last_block > data.first_block) {
544                 ret = record_file_blocks(trans, root, objectid, btrfs_inode,
545                                          data.first_block, 0, last_block -
546                                          data.first_block, data.checksum);
547         }
548 fail:
549         free(buffer);
550         return ret;
551 error:
552         fprintf(stderr, "ext2fs_block_iterate2: %s\n", error_message(err));
553         return -1;
554 }
555
556 static int create_symbol_link(struct btrfs_trans_handle *trans,
557                               struct btrfs_root *root, u64 objectid,
558                               struct btrfs_inode_item *btrfs_inode,
559                               ext2_filsys ext2_fs, ext2_ino_t ext2_ino,
560                               struct ext2_inode *ext2_inode)
561 {
562         int ret;
563         char *pathname;
564         u64 inode_size = btrfs_stack_inode_size(btrfs_inode);
565         if (ext2fs_inode_data_blocks(ext2_fs, ext2_inode)) {
566                 btrfs_set_stack_inode_size(btrfs_inode, inode_size + 1);
567                 ret = create_file_extents(trans, root, objectid, btrfs_inode,
568                                           ext2_fs, ext2_ino, 1, 1);
569                 btrfs_set_stack_inode_size(btrfs_inode, inode_size);
570                 return ret;
571         }
572
573         pathname = (char *)&(ext2_inode->i_block[0]);
574         BUG_ON(pathname[inode_size] != 0);
575         ret = btrfs_insert_inline_extent(trans, root, objectid, 0,
576                                          pathname, inode_size + 1);
577         btrfs_set_stack_inode_nbytes(btrfs_inode, inode_size + 1);
578         return ret;
579 }
580
581 /*
582  * Following xattr/acl related codes are based on codes in
583  * fs/ext3/xattr.c and fs/ext3/acl.c
584  */
585 #define EXT2_XATTR_BHDR(ptr) ((struct ext2_ext_attr_header *)(ptr))
586 #define EXT2_XATTR_BFIRST(ptr) \
587         ((struct ext2_ext_attr_entry *)(EXT2_XATTR_BHDR(ptr) + 1))
588 #define EXT2_XATTR_IHDR(inode) \
589         ((struct ext2_ext_attr_header *) ((void *)(inode) + \
590                 EXT2_GOOD_OLD_INODE_SIZE + (inode)->i_extra_isize))
591 #define EXT2_XATTR_IFIRST(inode) \
592         ((struct ext2_ext_attr_entry *) ((void *)EXT2_XATTR_IHDR(inode) + \
593                 sizeof(EXT2_XATTR_IHDR(inode)->h_magic)))
594
595 static int ext2_xattr_check_names(struct ext2_ext_attr_entry *entry,
596                                   const void *end)
597 {
598         struct ext2_ext_attr_entry *next;
599
600         while (!EXT2_EXT_IS_LAST_ENTRY(entry)) {
601                 next = EXT2_EXT_ATTR_NEXT(entry);
602                 if ((void *)next >= end)
603                         return -EIO;
604                 entry = next;
605         }
606         return 0;
607 }
608
609 static int ext2_xattr_check_block(const char *buf, size_t size)
610 {
611         int error;
612         struct ext2_ext_attr_header *header = EXT2_XATTR_BHDR(buf);
613
614         if (header->h_magic != EXT2_EXT_ATTR_MAGIC ||
615             header->h_blocks != 1)
616                 return -EIO;
617         error = ext2_xattr_check_names(EXT2_XATTR_BFIRST(buf), buf + size);
618         return error;
619 }
620
621 static int ext2_xattr_check_entry(struct ext2_ext_attr_entry *entry,
622                                   size_t size)
623 {
624         size_t value_size = entry->e_value_size;
625
626         if (entry->e_value_block != 0 || value_size > size ||
627             entry->e_value_offs + value_size > size)
628                 return -EIO;
629         return 0;
630 }
631
632 #define EXT2_ACL_VERSION        0x0001
633
634 typedef struct {
635         __le16          e_tag;
636         __le16          e_perm;
637         __le32          e_id;
638 } ext2_acl_entry;
639
640 typedef struct {
641         __le16          e_tag;
642         __le16          e_perm;
643 } ext2_acl_entry_short;
644
645 typedef struct {
646         __le32          a_version;
647 } ext2_acl_header;
648
649 static inline int ext2_acl_count(size_t size)
650 {
651         ssize_t s;
652         size -= sizeof(ext2_acl_header);
653         s = size - 4 * sizeof(ext2_acl_entry_short);
654         if (s < 0) {
655                 if (size % sizeof(ext2_acl_entry_short))
656                         return -1;
657                 return size / sizeof(ext2_acl_entry_short);
658         } else {
659                 if (s % sizeof(ext2_acl_entry))
660                         return -1;
661                 return s / sizeof(ext2_acl_entry) + 4;
662         }
663 }
664
665 #define ACL_EA_VERSION          0x0002
666
667 typedef struct {
668         __le16          e_tag;
669         __le16          e_perm;
670         __le32          e_id;
671 } acl_ea_entry;
672
673 typedef struct {
674         __le32          a_version;
675         acl_ea_entry    a_entries[0];
676 } acl_ea_header;
677
678 static inline size_t acl_ea_size(int count)
679 {
680         return sizeof(acl_ea_header) + count * sizeof(acl_ea_entry);
681 }
682
683 static int ext2_acl_to_xattr(void *dst, const void *src,
684                              size_t dst_size, size_t src_size)
685 {
686         int i, count;
687         const void *end = src + src_size;
688         acl_ea_header *ext_acl = (acl_ea_header *)dst;
689         acl_ea_entry *dst_entry = ext_acl->a_entries;
690         ext2_acl_entry *src_entry;
691
692         if (src_size < sizeof(ext2_acl_header))
693                 goto fail;
694         if (((ext2_acl_header *)src)->a_version !=
695             cpu_to_le32(EXT2_ACL_VERSION))
696                 goto fail;
697         src += sizeof(ext2_acl_header);
698         count = ext2_acl_count(src_size);
699         if (count <= 0)
700                 goto fail;
701
702         BUG_ON(dst_size < acl_ea_size(count));
703         ext_acl->a_version = cpu_to_le32(ACL_EA_VERSION);
704         for (i = 0; i < count; i++, dst_entry++) {
705                 src_entry = (ext2_acl_entry *)src;
706                 if (src + sizeof(ext2_acl_entry_short) > end)
707                         goto fail;
708                 dst_entry->e_tag = src_entry->e_tag;
709                 dst_entry->e_perm = src_entry->e_perm;
710                 switch (le16_to_cpu(src_entry->e_tag)) {
711                 case ACL_USER_OBJ:
712                 case ACL_GROUP_OBJ:
713                 case ACL_MASK:
714                 case ACL_OTHER:
715                         src += sizeof(ext2_acl_entry_short);
716                         dst_entry->e_id = cpu_to_le32(ACL_UNDEFINED_ID);
717                         break;
718                 case ACL_USER:
719                 case ACL_GROUP:
720                         src += sizeof(ext2_acl_entry);
721                         if (src > end)
722                                 goto fail;
723                         dst_entry->e_id = src_entry->e_id;
724                         break;
725                 default:
726                         goto fail;
727                 }
728         }
729         if (src != end)
730                 goto fail;
731         return 0;
732 fail:
733         return -EINVAL;
734 }
735
736 static char *xattr_prefix_table[] = {
737         [1] =   "user.",
738         [2] =   "system.posix_acl_access",
739         [3] =   "system.posix_acl_default",
740         [4] =   "trusted.",
741         [6] =   "security.",
742 };
743
744 static int copy_single_xattr(struct btrfs_trans_handle *trans,
745                              struct btrfs_root *root, u64 objectid,
746                              struct ext2_ext_attr_entry *entry,
747                              const void *data, u32 datalen)
748 {
749         int ret = 0;
750         int name_len;
751         int name_index;
752         void *databuf = NULL;
753         char namebuf[XATTR_NAME_MAX + 1];
754
755         name_index = entry->e_name_index;
756         if (name_index >= ARRAY_SIZE(xattr_prefix_table) ||
757             xattr_prefix_table[name_index] == NULL)
758                 return -EOPNOTSUPP;
759         name_len = strlen(xattr_prefix_table[name_index]) +
760                    entry->e_name_len;
761         if (name_len >= sizeof(namebuf))
762                 return -ERANGE;
763
764         if (name_index == 2 || name_index == 3) {
765                 size_t bufsize = acl_ea_size(ext2_acl_count(datalen));
766                 databuf = malloc(bufsize);
767                 if (!databuf)
768                        return -ENOMEM;
769                 ret = ext2_acl_to_xattr(databuf, data, bufsize, datalen);
770                 if (ret)
771                         goto out;
772                 data = databuf;
773                 datalen = bufsize;
774         }
775         strncpy(namebuf, xattr_prefix_table[name_index], XATTR_NAME_MAX);
776         strncat(namebuf, EXT2_EXT_ATTR_NAME(entry), entry->e_name_len);
777         if (name_len + datalen > BTRFS_LEAF_DATA_SIZE(root) -
778             sizeof(struct btrfs_item) - sizeof(struct btrfs_dir_item)) {
779                 fprintf(stderr, "skip large xattr on inode %Lu name %.*s\n",
780                         objectid - INO_OFFSET, name_len, namebuf);
781                 goto out;
782         }
783         ret = btrfs_insert_xattr_item(trans, root, namebuf, name_len,
784                                       data, datalen, objectid);
785 out:
786         free(databuf);
787         return ret;
788 }
789
790 static int copy_extended_attrs(struct btrfs_trans_handle *trans,
791                                struct btrfs_root *root, u64 objectid,
792                                struct btrfs_inode_item *btrfs_inode,
793                                ext2_filsys ext2_fs, ext2_ino_t ext2_ino)
794 {
795         int ret = 0;
796         int inline_ea = 0;
797         errcode_t err;
798         u32 datalen;
799         u32 block_size = ext2_fs->blocksize;
800         u32 inode_size = EXT2_INODE_SIZE(ext2_fs->super);
801         struct ext2_inode_large *ext2_inode;
802         struct ext2_ext_attr_entry *entry;
803         void *data;
804         char *buffer = NULL;
805         char inode_buf[EXT2_GOOD_OLD_INODE_SIZE];
806
807         if (inode_size <= EXT2_GOOD_OLD_INODE_SIZE) {
808                 ext2_inode = (struct ext2_inode_large *)inode_buf;
809         } else {
810                 ext2_inode = (struct ext2_inode_large *)malloc(inode_size);
811                 if (!ext2_inode)
812                        return -ENOMEM;
813         }
814         err = ext2fs_read_inode_full(ext2_fs, ext2_ino, (void *)ext2_inode,
815                                      inode_size);
816         if (err) {
817                 fprintf(stderr, "ext2fs_read_inode_full: %s\n",
818                         error_message(err));
819                 ret = -1;
820                 goto out;
821         }
822
823         if (ext2_ino > ext2_fs->super->s_first_ino &&
824             inode_size > EXT2_GOOD_OLD_INODE_SIZE) {
825                 if (EXT2_GOOD_OLD_INODE_SIZE +
826                     ext2_inode->i_extra_isize > inode_size) {
827                         ret = -EIO;
828                         goto out;
829                 }
830                 if (ext2_inode->i_extra_isize != 0 &&
831                     EXT2_XATTR_IHDR(ext2_inode)->h_magic ==
832                     EXT2_EXT_ATTR_MAGIC) {
833                         inline_ea = 1;
834                 }
835         }
836         if (inline_ea) {
837                 int total;
838                 void *end = (void *)ext2_inode + inode_size;
839                 entry = EXT2_XATTR_IFIRST(ext2_inode);
840                 total = end - (void *)entry;
841                 ret = ext2_xattr_check_names(entry, end);
842                 if (ret)
843                         goto out;
844                 while (!EXT2_EXT_IS_LAST_ENTRY(entry)) {
845                         ret = ext2_xattr_check_entry(entry, total);
846                         if (ret)
847                                 goto out;
848                         data = (void *)EXT2_XATTR_IFIRST(ext2_inode) +
849                                 entry->e_value_offs;
850                         datalen = entry->e_value_size;
851                         ret = copy_single_xattr(trans, root, objectid,
852                                                 entry, data, datalen);
853                         if (ret)
854                                 goto out;
855                         entry = EXT2_EXT_ATTR_NEXT(entry);
856                 }
857         }
858
859         if (ext2_inode->i_file_acl == 0)
860                 goto out;
861
862         buffer = malloc(block_size);
863         if (!buffer) {
864                 ret = -ENOMEM;
865                 goto out;
866         }
867         err = ext2fs_read_ext_attr(ext2_fs, ext2_inode->i_file_acl, buffer);
868         if (err) {
869                 fprintf(stderr, "ext2fs_read_ext_attr: %s\n",
870                         error_message(err));
871                 ret = -1;
872                 goto out;
873         }
874         ret = ext2_xattr_check_block(buffer, block_size);
875         if (ret)
876                 goto out;
877
878         entry = EXT2_XATTR_BFIRST(buffer);
879         while (!EXT2_EXT_IS_LAST_ENTRY(entry)) {
880                 ret = ext2_xattr_check_entry(entry, block_size);
881                 if (ret)
882                         goto out;
883                 data = buffer + entry->e_value_offs;
884                 datalen = entry->e_value_size;
885                 ret = copy_single_xattr(trans, root, objectid,
886                                         entry, data, datalen);
887                 if (ret)
888                         goto out;
889                 entry = EXT2_EXT_ATTR_NEXT(entry);
890         }
891 out:
892         free(buffer);
893         if ((void *)ext2_inode != inode_buf)
894                 free(ext2_inode);
895         return ret;
896 }
897 #define MINORBITS       20
898 #define MKDEV(ma, mi)   (((ma) << MINORBITS) | (mi))
899
900 static inline dev_t old_decode_dev(u16 val)
901 {
902         return MKDEV((val >> 8) & 255, val & 255);
903 }
904
905 static inline dev_t new_decode_dev(u32 dev)
906 {
907         unsigned major = (dev & 0xfff00) >> 8;
908         unsigned minor = (dev & 0xff) | ((dev >> 12) & 0xfff00);
909         return MKDEV(major, minor);
910 }
911
912 static int copy_inode_item(struct btrfs_inode_item *dst,
913                            struct ext2_inode *src, u32 blocksize)
914 {
915         btrfs_set_stack_inode_generation(dst, 1);
916         btrfs_set_stack_inode_size(dst, src->i_size);
917         btrfs_set_stack_inode_nbytes(dst, 0);
918         btrfs_set_stack_inode_block_group(dst, 0);
919         btrfs_set_stack_inode_nlink(dst, src->i_links_count);
920         btrfs_set_stack_inode_uid(dst, src->i_uid | (src->i_uid_high << 16));
921         btrfs_set_stack_inode_gid(dst, src->i_gid | (src->i_gid_high << 16));
922         btrfs_set_stack_inode_mode(dst, src->i_mode);
923         btrfs_set_stack_inode_rdev(dst, 0);
924         btrfs_set_stack_inode_flags(dst, 0);
925         btrfs_set_stack_timespec_sec(&dst->atime, src->i_atime);
926         btrfs_set_stack_timespec_nsec(&dst->atime, 0);
927         btrfs_set_stack_timespec_sec(&dst->ctime, src->i_ctime);
928         btrfs_set_stack_timespec_nsec(&dst->ctime, 0);
929         btrfs_set_stack_timespec_sec(&dst->mtime, src->i_mtime);
930         btrfs_set_stack_timespec_nsec(&dst->mtime, 0);
931         btrfs_set_stack_timespec_sec(&dst->otime, 0);
932         btrfs_set_stack_timespec_nsec(&dst->otime, 0);
933
934         if (S_ISDIR(src->i_mode)) {
935                 btrfs_set_stack_inode_size(dst, 0);
936                 btrfs_set_stack_inode_nlink(dst, 1);
937         }
938         if (S_ISREG(src->i_mode)) {
939                 btrfs_set_stack_inode_size(dst, (u64)src->i_size_high << 32 |
940                                            (u64)src->i_size);
941         }
942         if (!S_ISREG(src->i_mode) && !S_ISDIR(src->i_mode) &&
943             !S_ISLNK(src->i_mode)) {
944                 if (src->i_block[0]) {
945                         btrfs_set_stack_inode_rdev(dst,
946                                 old_decode_dev(src->i_block[0]));
947                 } else {
948                         btrfs_set_stack_inode_rdev(dst,
949                                 new_decode_dev(src->i_block[1]));
950                 }
951         }
952         return 0;
953 }
954
955 /*
956  * copy a single inode. do all the required works, such as cloning
957  * inode item, creating file extents and creating directory entries.
958  */
959 static int copy_single_inode(struct btrfs_trans_handle *trans,
960                              struct btrfs_root *root, u64 objectid,
961                              ext2_filsys ext2_fs, ext2_ino_t ext2_ino,
962                              struct ext2_inode *ext2_inode,
963                              int datacsum, int packing, int noxattr)
964 {
965         int ret;
966         struct btrfs_key inode_key;
967         struct btrfs_inode_item btrfs_inode;
968
969         if (ext2_inode->i_links_count == 0)
970                 return 0;
971
972         copy_inode_item(&btrfs_inode, ext2_inode, ext2_fs->blocksize);
973         if (!datacsum && S_ISREG(ext2_inode->i_mode)) {
974                 u32 flags = btrfs_stack_inode_flags(&btrfs_inode) |
975                             BTRFS_INODE_NODATASUM;
976                 btrfs_set_stack_inode_flags(&btrfs_inode, flags);
977         }
978
979         switch (ext2_inode->i_mode & S_IFMT) {
980         case S_IFREG:
981                 ret = create_file_extents(trans, root, objectid, &btrfs_inode,
982                                         ext2_fs, ext2_ino, datacsum, packing);
983                 break;
984         case S_IFDIR:
985                 ret = create_dir_entries(trans, root, objectid, &btrfs_inode,
986                                          ext2_fs, ext2_ino);
987                 break;
988         case S_IFLNK:
989                 ret = create_symbol_link(trans, root, objectid, &btrfs_inode,
990                                          ext2_fs, ext2_ino, ext2_inode);
991                 break;
992         default:
993                 ret = 0;
994                 break;
995         }
996         if (ret)
997                 return ret;
998
999         if (!noxattr) {
1000                 ret = copy_extended_attrs(trans, root, objectid, &btrfs_inode,
1001                                           ext2_fs, ext2_ino);
1002                 if (ret)
1003                         return ret;
1004         }
1005         inode_key.objectid = objectid;
1006         inode_key.offset = 0;
1007         btrfs_set_key_type(&inode_key, BTRFS_INODE_ITEM_KEY);
1008         ret = btrfs_insert_inode(trans, root, objectid, &btrfs_inode);
1009         return ret;
1010 }
1011
1012 static int copy_disk_extent(struct btrfs_root *root, u64 dst_bytenr,
1013                             u64 src_bytenr, u32 num_bytes)
1014 {
1015         int ret;
1016         char *buffer;
1017         struct btrfs_fs_devices *fs_devs = root->fs_info->fs_devices;
1018
1019         buffer = malloc(num_bytes);
1020         if (!buffer)
1021                 return -ENOMEM;
1022         ret = pread(fs_devs->latest_bdev, buffer, num_bytes, src_bytenr);
1023         if (ret != num_bytes)
1024                 goto fail;
1025         ret = pwrite(fs_devs->latest_bdev, buffer, num_bytes, dst_bytenr);
1026         if (ret != num_bytes)
1027                 goto fail;
1028         ret = 0;
1029 fail:
1030         free(buffer);
1031         if (ret > 0)
1032                 ret = -1;
1033         return ret;
1034 }
1035 /*
1036  * scan ext2's inode bitmap and copy all used inodes.
1037  */
1038 static int copy_inodes(struct btrfs_root *root, ext2_filsys ext2_fs,
1039                        int datacsum, int packing, int noxattr)
1040 {
1041         int ret;
1042         errcode_t err;
1043         ext2_inode_scan ext2_scan;
1044         struct ext2_inode ext2_inode;
1045         ext2_ino_t ext2_ino;
1046         u64 objectid;
1047         struct btrfs_trans_handle *trans;
1048
1049         trans = btrfs_start_transaction(root, 1);
1050         if (!trans)
1051                 return -ENOMEM;
1052         err = ext2fs_open_inode_scan(ext2_fs, 0, &ext2_scan);
1053         if (err) {
1054                 fprintf(stderr, "ext2fs_open_inode_scan: %s\n", error_message(err));
1055                 return -1;
1056         }
1057         while (!(err = ext2fs_get_next_inode(ext2_scan, &ext2_ino,
1058                                              &ext2_inode))) {
1059                 /* no more inodes */
1060                 if (ext2_ino == 0)
1061                         break;
1062                 /* skip special inode in ext2fs */
1063                 if (ext2_ino < EXT2_GOOD_OLD_FIRST_INO &&
1064                     ext2_ino != EXT2_ROOT_INO)
1065                         continue;
1066                 objectid = ext2_ino + INO_OFFSET;
1067                 ret = copy_single_inode(trans, root,
1068                                         objectid, ext2_fs, ext2_ino,
1069                                         &ext2_inode, datacsum, packing,
1070                                         noxattr);
1071                 if (ret)
1072                         return ret;
1073                 if (trans->blocks_used >= 4096) {
1074                         ret = btrfs_commit_transaction(trans, root);
1075                         BUG_ON(ret);
1076                         trans = btrfs_start_transaction(root, 1);
1077                         BUG_ON(!trans);
1078                 }
1079         }
1080         if (err) {
1081                 fprintf(stderr, "ext2fs_get_next_inode: %s\n", error_message(err));
1082                 return -1;
1083         }
1084         ret = btrfs_commit_transaction(trans, root);
1085         BUG_ON(ret);
1086
1087         return ret;
1088 }
1089
1090 /*
1091  * Construct a range of ext2fs image file.
1092  * scan block allocation bitmap, find all blocks used by the ext2fs
1093  * in this range and create file extents that point to these blocks.
1094  *
1095  * Note: Before calling the function, no file extent points to blocks
1096  *       in this range
1097  */
1098 static int create_image_file_range(struct btrfs_trans_handle *trans,
1099                                    struct btrfs_root *root, u64 objectid,
1100                                    struct btrfs_inode_item *inode,
1101                                    u64 start_byte, u64 end_byte,
1102                                    ext2_filsys ext2_fs)
1103 {
1104         u32 blocksize = ext2_fs->blocksize;
1105         u32 block = start_byte / blocksize;
1106         u32 last_block = (end_byte + blocksize - 1) / blocksize;
1107         int ret = 0;
1108         struct blk_iterate_data data = {
1109                 .trans          = trans,
1110                 .root           = root,
1111                 .inode          = inode,
1112                 .objectid       = objectid,
1113                 .first_block    = block,
1114                 .disk_block     = 0,
1115                 .num_blocks     = 0,
1116                 .boundary       = (u64)-1,
1117                 .checksum       = 0,
1118                 .errcode        = 0,
1119         };
1120         for (; start_byte < end_byte; block++, start_byte += blocksize) {
1121                 if (!ext2fs_fast_test_block_bitmap(ext2_fs->block_map, block))
1122                         continue;
1123                 ret = block_iterate_proc(NULL, block, block, &data);
1124                 if (ret & BLOCK_ABORT) {
1125                         ret = data.errcode;
1126                         goto fail;
1127                 }
1128         }
1129         if (data.num_blocks > 0) {
1130                 ret = record_file_blocks(trans, root, objectid, inode,
1131                                          data.first_block, data.disk_block,
1132                                          data.num_blocks, 0);
1133                 if (ret)
1134                         goto fail;
1135                 data.first_block += data.num_blocks;
1136         }
1137         if (last_block > data.first_block) {
1138                 ret = record_file_blocks(trans, root, objectid, inode,
1139                                          data.first_block, 0, last_block -
1140                                          data.first_block, 0);
1141                 if (ret)
1142                         goto fail;
1143         }
1144 fail:
1145         return ret;
1146 }
1147 /*
1148  * Create the ext2fs image file.
1149  */
1150 static int create_ext2_image(struct btrfs_root *root, ext2_filsys ext2_fs,
1151                              const char *name)
1152 {
1153         int ret;
1154         struct btrfs_key key;
1155         struct btrfs_key location;
1156         struct btrfs_path path;
1157         struct btrfs_inode_item btrfs_inode;
1158         struct btrfs_inode_item *inode_item;
1159         struct extent_buffer *leaf;
1160         struct btrfs_fs_info *fs_info = root->fs_info;
1161         struct btrfs_root *extent_root = fs_info->extent_root;
1162         struct btrfs_trans_handle *trans;
1163         struct btrfs_extent_item *ei;
1164         struct btrfs_extent_inline_ref *iref;
1165         struct btrfs_extent_data_ref *dref;
1166         u64 bytenr;
1167         u64 num_bytes;
1168         u64 objectid;
1169         u64 last_byte;
1170         u64 first_free;
1171         u64 total_bytes;
1172         u32 sectorsize = root->sectorsize;
1173
1174         total_bytes = btrfs_super_total_bytes(fs_info->super_copy);
1175         first_free =  BTRFS_SUPER_INFO_OFFSET + sectorsize * 2 - 1;
1176         first_free &= ~((u64)sectorsize - 1);
1177
1178         memset(&btrfs_inode, 0, sizeof(btrfs_inode));
1179         btrfs_set_stack_inode_generation(&btrfs_inode, 1);
1180         btrfs_set_stack_inode_size(&btrfs_inode, total_bytes);
1181         btrfs_set_stack_inode_nlink(&btrfs_inode, 1);
1182         btrfs_set_stack_inode_nbytes(&btrfs_inode, 0);
1183         btrfs_set_stack_inode_mode(&btrfs_inode, S_IFREG | 0400);
1184         btrfs_set_stack_inode_flags(&btrfs_inode, BTRFS_INODE_NODATASUM |
1185                                     BTRFS_INODE_READONLY);
1186         btrfs_init_path(&path);
1187         trans = btrfs_start_transaction(root, 1);
1188         BUG_ON(!trans);
1189
1190         objectid = btrfs_root_dirid(&root->root_item);
1191         ret = btrfs_find_free_objectid(trans, root, objectid, &objectid);
1192         if (ret)
1193                 goto fail;
1194
1195         /*
1196          * copy blocks covered by extent #0 to new positions. extent #0 is
1197          * special, we can't rely on relocate_extents_range to relocate it.
1198          */
1199         for (last_byte = 0; last_byte < first_free; last_byte += sectorsize) {
1200                 ret = custom_alloc_extent(root, sectorsize, 0, &key);
1201                 if (ret)
1202                         goto fail;
1203                 ret = copy_disk_extent(root, key.objectid, last_byte,
1204                                        sectorsize);
1205                 if (ret)
1206                         goto fail;
1207                 ret = btrfs_record_file_extent(trans, root, objectid,
1208                                                &btrfs_inode, last_byte,
1209                                                key.objectid, sectorsize);
1210                 if (ret)
1211                         goto fail;
1212         }
1213
1214         while(1) {
1215                 key.objectid = last_byte;
1216                 key.offset = 0;
1217                 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
1218                 ret = btrfs_search_slot(trans, fs_info->extent_root,
1219                                         &key, &path, 0, 0);
1220                 if (ret < 0)
1221                         goto fail;
1222 next:
1223                 leaf = path.nodes[0];
1224                 if (path.slots[0] >= btrfs_header_nritems(leaf)) {
1225                         ret = btrfs_next_leaf(extent_root, &path);
1226                         if (ret < 0)
1227                                 goto fail;
1228                         if (ret > 0)
1229                                 break;
1230                         leaf = path.nodes[0];
1231                 }
1232                 btrfs_item_key_to_cpu(leaf, &key, path.slots[0]);
1233                 if (last_byte > key.objectid ||
1234                     key.type != BTRFS_EXTENT_ITEM_KEY) {
1235                         path.slots[0]++;
1236                         goto next;
1237                 }
1238
1239                 bytenr = key.objectid;
1240                 num_bytes = key.offset;
1241                 ei = btrfs_item_ptr(leaf, path.slots[0],
1242                                     struct btrfs_extent_item);
1243                 if (!(btrfs_extent_flags(leaf, ei) & BTRFS_EXTENT_FLAG_DATA)) {
1244                         path.slots[0]++;
1245                         goto next;
1246                 }
1247
1248                 BUG_ON(btrfs_item_size_nr(leaf, path.slots[0]) != sizeof(*ei) +
1249                        btrfs_extent_inline_ref_size(BTRFS_EXTENT_DATA_REF_KEY));
1250
1251                 iref = (struct btrfs_extent_inline_ref *)(ei + 1);
1252                 key.type = btrfs_extent_inline_ref_type(leaf, iref);
1253                 BUG_ON(key.type != BTRFS_EXTENT_DATA_REF_KEY);
1254                 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1255                 if (btrfs_extent_data_ref_root(leaf, dref) !=
1256                     BTRFS_FS_TREE_OBJECTID) {
1257                         path.slots[0]++;
1258                         goto next;
1259                 }
1260
1261                 if (bytenr > last_byte) {
1262                         ret = create_image_file_range(trans, root, objectid,
1263                                                       &btrfs_inode, last_byte,
1264                                                       bytenr, ext2_fs);
1265                         if (ret)
1266                                 goto fail;
1267                 }
1268                 ret = btrfs_record_file_extent(trans, root, objectid,
1269                                                &btrfs_inode, bytenr, bytenr,
1270                                                num_bytes);
1271                 if (ret)
1272                         goto fail;
1273                 last_byte = bytenr + num_bytes;
1274                 btrfs_release_path(&path);
1275
1276                 if (trans->blocks_used >= 4096) {
1277                         ret = btrfs_commit_transaction(trans, root);
1278                         BUG_ON(ret);
1279                         trans = btrfs_start_transaction(root, 1);
1280                         BUG_ON(!trans);
1281                 }
1282         }
1283         btrfs_release_path(&path);
1284         if (total_bytes > last_byte) {
1285                 ret = create_image_file_range(trans, root, objectid,
1286                                               &btrfs_inode, last_byte,
1287                                               total_bytes, ext2_fs);
1288                 if (ret)
1289                         goto fail;
1290         }
1291
1292         ret = btrfs_insert_inode(trans, root, objectid, &btrfs_inode);
1293         if (ret)
1294                 goto fail;
1295
1296         location.objectid = objectid;
1297         location.offset = 0;
1298         btrfs_set_key_type(&location, BTRFS_INODE_ITEM_KEY);
1299         ret = btrfs_insert_dir_item(trans, root, name, strlen(name),
1300                                     btrfs_root_dirid(&root->root_item),
1301                                     &location, EXT2_FT_REG_FILE, objectid);
1302         if (ret)
1303                 goto fail;
1304         ret = btrfs_insert_inode_ref(trans, root, name, strlen(name),
1305                                      objectid,
1306                                      btrfs_root_dirid(&root->root_item),
1307                                      objectid);
1308         if (ret)
1309                 goto fail;
1310         location.objectid = btrfs_root_dirid(&root->root_item);
1311         location.offset = 0;
1312         btrfs_set_key_type(&location, BTRFS_INODE_ITEM_KEY);
1313         ret = btrfs_lookup_inode(trans, root, &path, &location, 1);
1314         if (ret)
1315                 goto fail;
1316         leaf = path.nodes[0];
1317         inode_item = btrfs_item_ptr(leaf, path.slots[0],
1318                                     struct btrfs_inode_item);
1319         btrfs_set_inode_size(leaf, inode_item, strlen(name) * 2 +
1320                              btrfs_inode_size(leaf, inode_item));
1321         btrfs_mark_buffer_dirty(leaf);
1322         btrfs_release_path(&path);
1323         ret = btrfs_commit_transaction(trans, root);
1324         BUG_ON(ret);
1325 fail:
1326         btrfs_release_path(&path);
1327         return ret;
1328 }
1329
1330 static struct btrfs_root * link_subvol(struct btrfs_root *root,
1331                 const char *base, u64 root_objectid)
1332 {
1333         struct btrfs_trans_handle *trans;
1334         struct btrfs_fs_info *fs_info = root->fs_info;
1335         struct btrfs_root *tree_root = fs_info->tree_root;
1336         struct btrfs_root *new_root = NULL;
1337         struct btrfs_path *path;
1338         struct btrfs_inode_item *inode_item;
1339         struct extent_buffer *leaf;
1340         struct btrfs_key key;
1341         u64 dirid = btrfs_root_dirid(&root->root_item);
1342         u64 index = 2;
1343         char buf[BTRFS_NAME_LEN + 1]; /* for snprintf null */
1344         int len;
1345         int i;
1346         int ret;
1347
1348         len = strlen(base);
1349         if (len < 1 || len > BTRFS_NAME_LEN)
1350                 return NULL;
1351
1352         path = btrfs_alloc_path();
1353         BUG_ON(!path);
1354
1355         key.objectid = dirid;
1356         key.type = BTRFS_DIR_INDEX_KEY;
1357         key.offset = (u64)-1;
1358
1359         ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1360         BUG_ON(ret <= 0);
1361
1362         if (path->slots[0] > 0) {
1363                 path->slots[0]--;
1364                 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1365                 if (key.objectid == dirid && key.type == BTRFS_DIR_INDEX_KEY)
1366                         index = key.offset + 1;
1367         }
1368         btrfs_release_path(path);
1369
1370         trans = btrfs_start_transaction(root, 1);
1371         BUG_ON(!trans);
1372
1373         key.objectid = dirid;
1374         key.offset = 0;
1375         key.type =  BTRFS_INODE_ITEM_KEY;
1376
1377         ret = btrfs_lookup_inode(trans, root, path, &key, 1);
1378         BUG_ON(ret);
1379         leaf = path->nodes[0];
1380         inode_item = btrfs_item_ptr(leaf, path->slots[0],
1381                                     struct btrfs_inode_item);
1382
1383         key.objectid = root_objectid;
1384         key.offset = (u64)-1;
1385         key.type = BTRFS_ROOT_ITEM_KEY;
1386
1387         memcpy(buf, base, len);
1388         for (i = 0; i < 1024; i++) {
1389                 ret = btrfs_insert_dir_item(trans, root, buf, len,
1390                                             dirid, &key, BTRFS_FT_DIR, index);
1391                 if (ret != -EEXIST)
1392                         break;
1393                 len = snprintf(buf, ARRAY_SIZE(buf), "%s%d", base, i);
1394                 if (len < 1 || len > BTRFS_NAME_LEN) {
1395                         ret = -EINVAL;
1396                         break;
1397                 }
1398         }
1399         if (ret)
1400                 goto fail;
1401
1402         btrfs_set_inode_size(leaf, inode_item, len * 2 +
1403                              btrfs_inode_size(leaf, inode_item));
1404         btrfs_mark_buffer_dirty(leaf);
1405         btrfs_release_path(path);
1406
1407         /* add the backref first */
1408         ret = btrfs_add_root_ref(trans, tree_root, root_objectid,
1409                                  BTRFS_ROOT_BACKREF_KEY,
1410                                  root->root_key.objectid,
1411                                  dirid, index, buf, len);
1412         BUG_ON(ret);
1413
1414         /* now add the forward ref */
1415         ret = btrfs_add_root_ref(trans, tree_root, root->root_key.objectid,
1416                                  BTRFS_ROOT_REF_KEY, root_objectid,
1417                                  dirid, index, buf, len);
1418
1419         ret = btrfs_commit_transaction(trans, root);
1420         BUG_ON(ret);
1421
1422         new_root = btrfs_read_fs_root(fs_info, &key);
1423         if (IS_ERR(new_root))
1424                 new_root = NULL;
1425 fail:
1426         btrfs_free_path(path);
1427         return new_root;
1428 }
1429
1430 static int create_chunk_mapping(struct btrfs_trans_handle *trans,
1431                                 struct btrfs_root *root)
1432 {
1433         struct btrfs_fs_info *info = root->fs_info;
1434         struct btrfs_root *chunk_root = info->chunk_root;
1435         struct btrfs_root *extent_root = info->extent_root;
1436         struct btrfs_device *device;
1437         struct btrfs_block_group_cache *cache;
1438         struct btrfs_dev_extent *extent;
1439         struct extent_buffer *leaf;
1440         struct btrfs_chunk chunk;
1441         struct btrfs_key key;
1442         struct btrfs_path path;
1443         u64 cur_start;
1444         u64 total_bytes;
1445         u64 chunk_objectid;
1446         int ret;
1447
1448         btrfs_init_path(&path);
1449
1450         total_bytes = btrfs_super_total_bytes(root->fs_info->super_copy);
1451         chunk_objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
1452
1453         BUG_ON(list_empty(&info->fs_devices->devices));
1454         device = list_entry(info->fs_devices->devices.next,
1455                             struct btrfs_device, dev_list);
1456         BUG_ON(device->devid != info->fs_devices->latest_devid);
1457
1458         /* delete device extent created by make_btrfs */
1459         key.objectid = device->devid;
1460         key.offset = 0;
1461         key.type = BTRFS_DEV_EXTENT_KEY;
1462         ret = btrfs_search_slot(trans, device->dev_root, &key, &path, -1, 1);
1463         if (ret < 0)
1464                 goto err;
1465
1466         BUG_ON(ret > 0);
1467         ret = btrfs_del_item(trans, device->dev_root, &path);
1468         if (ret)
1469                 goto err;
1470         btrfs_release_path(&path);
1471
1472         /* delete chunk item created by make_btrfs */
1473         key.objectid = chunk_objectid;
1474         key.offset = 0;
1475         key.type = BTRFS_CHUNK_ITEM_KEY;
1476         ret = btrfs_search_slot(trans, chunk_root, &key, &path, -1, 1);
1477         if (ret < 0)
1478                 goto err;
1479
1480         BUG_ON(ret > 0);
1481         ret = btrfs_del_item(trans, chunk_root, &path);
1482         if (ret)
1483                 goto err;
1484         btrfs_release_path(&path);
1485
1486         /* for each block group, create device extent and chunk item */
1487         cur_start = 0;
1488         while (cur_start < total_bytes) {
1489                 cache = btrfs_lookup_block_group(root->fs_info, cur_start);
1490                 BUG_ON(!cache);
1491
1492                 /* insert device extent */
1493                 key.objectid = device->devid;
1494                 key.offset = cache->key.objectid;
1495                 key.type = BTRFS_DEV_EXTENT_KEY;
1496                 ret = btrfs_insert_empty_item(trans, device->dev_root, &path,
1497                                               &key, sizeof(*extent));
1498                 if (ret)
1499                         goto err;
1500
1501                 leaf = path.nodes[0];
1502                 extent = btrfs_item_ptr(leaf, path.slots[0],
1503                                         struct btrfs_dev_extent);
1504
1505                 btrfs_set_dev_extent_chunk_tree(leaf, extent,
1506                                                 chunk_root->root_key.objectid);
1507                 btrfs_set_dev_extent_chunk_objectid(leaf, extent,
1508                                                     chunk_objectid);
1509                 btrfs_set_dev_extent_chunk_offset(leaf, extent,
1510                                                   cache->key.objectid);
1511                 btrfs_set_dev_extent_length(leaf, extent, cache->key.offset);
1512                 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
1513                     (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent),
1514                     BTRFS_UUID_SIZE);
1515                 btrfs_mark_buffer_dirty(leaf);
1516                 btrfs_release_path(&path);
1517
1518                 /* insert chunk item */
1519                 btrfs_set_stack_chunk_length(&chunk, cache->key.offset);
1520                 btrfs_set_stack_chunk_owner(&chunk,
1521                                             extent_root->root_key.objectid);
1522                 btrfs_set_stack_chunk_stripe_len(&chunk, BTRFS_STRIPE_LEN);
1523                 btrfs_set_stack_chunk_type(&chunk, cache->flags);
1524                 btrfs_set_stack_chunk_io_align(&chunk, device->io_align);
1525                 btrfs_set_stack_chunk_io_width(&chunk, device->io_width);
1526                 btrfs_set_stack_chunk_sector_size(&chunk, device->sector_size);
1527                 btrfs_set_stack_chunk_num_stripes(&chunk, 1);
1528                 btrfs_set_stack_chunk_sub_stripes(&chunk, 0);
1529                 btrfs_set_stack_stripe_devid(&chunk.stripe, device->devid);
1530                 btrfs_set_stack_stripe_offset(&chunk.stripe,
1531                                               cache->key.objectid);
1532                 memcpy(&chunk.stripe.dev_uuid, device->uuid, BTRFS_UUID_SIZE);
1533
1534                 key.objectid = chunk_objectid;
1535                 key.offset = cache->key.objectid;
1536                 key.type = BTRFS_CHUNK_ITEM_KEY;
1537
1538                 ret = btrfs_insert_item(trans, chunk_root, &key, &chunk,
1539                                         btrfs_chunk_item_size(1));
1540                 if (ret)
1541                         goto err;
1542
1543                 cur_start = cache->key.objectid + cache->key.offset;
1544         }
1545
1546         device->bytes_used = total_bytes;
1547         ret = btrfs_update_device(trans, device);
1548 err:
1549         btrfs_release_path(&path);
1550         return ret;
1551 }
1552
1553 static int create_subvol(struct btrfs_trans_handle *trans,
1554                          struct btrfs_root *root, u64 root_objectid)
1555 {
1556         struct extent_buffer *tmp;
1557         struct btrfs_root *new_root;
1558         struct btrfs_key key;
1559         struct btrfs_root_item root_item;
1560         int ret;
1561
1562         ret = btrfs_copy_root(trans, root, root->node, &tmp,
1563                               root_objectid);
1564         BUG_ON(ret);
1565
1566         memcpy(&root_item, &root->root_item, sizeof(root_item));
1567         btrfs_set_root_bytenr(&root_item, tmp->start);
1568         btrfs_set_root_level(&root_item, btrfs_header_level(tmp));
1569         btrfs_set_root_generation(&root_item, trans->transid);
1570         free_extent_buffer(tmp);
1571
1572         key.objectid = root_objectid;
1573         key.type = BTRFS_ROOT_ITEM_KEY;
1574         key.offset = trans->transid;
1575         ret = btrfs_insert_root(trans, root->fs_info->tree_root,
1576                                 &key, &root_item);
1577
1578         key.offset = (u64)-1;
1579         new_root = btrfs_read_fs_root(root->fs_info, &key);
1580         BUG_ON(!new_root || IS_ERR(new_root));
1581
1582         ret = btrfs_make_root_dir(trans, new_root, BTRFS_FIRST_FREE_OBJECTID);
1583         BUG_ON(ret);
1584
1585         return 0;
1586 }
1587
1588 static int init_btrfs(struct btrfs_root *root)
1589 {
1590         int ret;
1591         struct btrfs_key location;
1592         struct btrfs_trans_handle *trans;
1593         struct btrfs_fs_info *fs_info = root->fs_info;
1594         struct extent_buffer *tmp;
1595
1596         trans = btrfs_start_transaction(root, 1);
1597         BUG_ON(!trans);
1598         ret = btrfs_make_block_groups(trans, root);
1599         if (ret)
1600                 goto err;
1601         ret = btrfs_fix_block_accounting(trans, root);
1602         if (ret)
1603                 goto err;
1604         ret = create_chunk_mapping(trans, root);
1605         if (ret)
1606                 goto err;
1607         ret = btrfs_make_root_dir(trans, fs_info->tree_root,
1608                                   BTRFS_ROOT_TREE_DIR_OBJECTID);
1609         if (ret)
1610                 goto err;
1611         memcpy(&location, &root->root_key, sizeof(location));
1612         location.offset = (u64)-1;
1613         ret = btrfs_insert_dir_item(trans, fs_info->tree_root, "default", 7,
1614                                 btrfs_super_root_dir(fs_info->super_copy),
1615                                 &location, BTRFS_FT_DIR, 0);
1616         if (ret)
1617                 goto err;
1618         ret = btrfs_insert_inode_ref(trans, fs_info->tree_root, "default", 7,
1619                                 location.objectid,
1620                                 btrfs_super_root_dir(fs_info->super_copy), 0);
1621         if (ret)
1622                 goto err;
1623         btrfs_set_root_dirid(&fs_info->fs_root->root_item,
1624                              BTRFS_FIRST_FREE_OBJECTID);
1625
1626         /* subvol for ext2 image file */
1627         ret = create_subvol(trans, root, EXT2_IMAGE_SUBVOL_OBJECTID);
1628         BUG_ON(ret);
1629         /* subvol for data relocation */
1630         ret = create_subvol(trans, root, BTRFS_DATA_RELOC_TREE_OBJECTID);
1631         BUG_ON(ret);
1632
1633         extent_buffer_get(fs_info->csum_root->node);
1634         ret = __btrfs_cow_block(trans, fs_info->csum_root,
1635                                 fs_info->csum_root->node, NULL, 0, &tmp, 0, 0);
1636         BUG_ON(ret);
1637         free_extent_buffer(tmp);
1638
1639         ret = btrfs_commit_transaction(trans, root);
1640         BUG_ON(ret);
1641 err:
1642         return ret;
1643 }
1644
1645 /*
1646  * Migrate super block to it's default position and zero 0 ~ 16k
1647  */
1648 static int migrate_super_block(int fd, u64 old_bytenr, u32 sectorsize)
1649 {
1650         int ret;
1651         struct extent_buffer *buf;
1652         struct btrfs_super_block *super;
1653         u32 len;
1654         u32 bytenr;
1655
1656         BUG_ON(sectorsize < sizeof(*super));
1657         buf = malloc(sizeof(*buf) + sectorsize);
1658         if (!buf)
1659                 return -ENOMEM;
1660
1661         buf->len = sectorsize;
1662         ret = pread(fd, buf->data, sectorsize, old_bytenr);
1663         if (ret != sectorsize)
1664                 goto fail;
1665
1666         super = (struct btrfs_super_block *)buf->data;
1667         BUG_ON(btrfs_super_bytenr(super) != old_bytenr);
1668         btrfs_set_super_bytenr(super, BTRFS_SUPER_INFO_OFFSET);
1669
1670         csum_tree_block_size(buf, BTRFS_CRC32_SIZE, 0);
1671         ret = pwrite(fd, buf->data, sectorsize, BTRFS_SUPER_INFO_OFFSET);
1672         if (ret != sectorsize)
1673                 goto fail;
1674
1675         ret = fsync(fd);
1676         if (ret)
1677                 goto fail;
1678
1679         memset(buf->data, 0, sectorsize);
1680         for (bytenr = 0; bytenr < BTRFS_SUPER_INFO_OFFSET; ) {
1681                 len = BTRFS_SUPER_INFO_OFFSET - bytenr;
1682                 if (len > sectorsize)
1683                         len = sectorsize;
1684                 ret = pwrite(fd, buf->data, len, bytenr);
1685                 if (ret != len) {
1686                         fprintf(stderr, "unable to zero fill device\n");
1687                         break;
1688                 }
1689                 bytenr += len;
1690         }
1691         ret = 0;
1692         fsync(fd);
1693 fail:
1694         free(buf);
1695         if (ret > 0)
1696                 ret = -1;
1697         return ret;
1698 }
1699
1700 static int prepare_system_chunk_sb(struct btrfs_super_block *super)
1701 {
1702         struct btrfs_chunk *chunk;
1703         struct btrfs_disk_key *key;
1704         u32 sectorsize = btrfs_super_sectorsize(super);
1705
1706         key = (struct btrfs_disk_key *)(super->sys_chunk_array);
1707         chunk = (struct btrfs_chunk *)(super->sys_chunk_array +
1708                                        sizeof(struct btrfs_disk_key));
1709
1710         btrfs_set_disk_key_objectid(key, BTRFS_FIRST_CHUNK_TREE_OBJECTID);
1711         btrfs_set_disk_key_type(key, BTRFS_CHUNK_ITEM_KEY);
1712         btrfs_set_disk_key_offset(key, 0);
1713
1714         btrfs_set_stack_chunk_length(chunk, btrfs_super_total_bytes(super));
1715         btrfs_set_stack_chunk_owner(chunk, BTRFS_EXTENT_TREE_OBJECTID);
1716         btrfs_set_stack_chunk_stripe_len(chunk, BTRFS_STRIPE_LEN);
1717         btrfs_set_stack_chunk_type(chunk, BTRFS_BLOCK_GROUP_SYSTEM);
1718         btrfs_set_stack_chunk_io_align(chunk, sectorsize);
1719         btrfs_set_stack_chunk_io_width(chunk, sectorsize);
1720         btrfs_set_stack_chunk_sector_size(chunk, sectorsize);
1721         btrfs_set_stack_chunk_num_stripes(chunk, 1);
1722         btrfs_set_stack_chunk_sub_stripes(chunk, 0);
1723         chunk->stripe.devid = super->dev_item.devid;
1724         btrfs_set_stack_stripe_offset(&chunk->stripe, 0);
1725         memcpy(chunk->stripe.dev_uuid, super->dev_item.uuid, BTRFS_UUID_SIZE);
1726         btrfs_set_super_sys_array_size(super, sizeof(*key) + sizeof(*chunk));
1727         return 0;
1728 }
1729
1730 static int prepare_system_chunk(int fd, u64 sb_bytenr, u32 sectorsize)
1731 {
1732         int ret;
1733         struct extent_buffer *buf;
1734         struct btrfs_super_block *super;
1735
1736         BUG_ON(sectorsize < sizeof(*super));
1737         buf = malloc(sizeof(*buf) + sectorsize);
1738         if (!buf)
1739                 return -ENOMEM;
1740
1741         buf->len = sectorsize;
1742         ret = pread(fd, buf->data, sectorsize, sb_bytenr);
1743         if (ret != sectorsize)
1744                 goto fail;
1745
1746         super = (struct btrfs_super_block *)buf->data;
1747         BUG_ON(btrfs_super_bytenr(super) != sb_bytenr);
1748         BUG_ON(btrfs_super_num_devices(super) != 1);
1749
1750         ret = prepare_system_chunk_sb(super);
1751         if (ret)
1752                 goto fail;
1753
1754         csum_tree_block_size(buf, BTRFS_CRC32_SIZE, 0);
1755         ret = pwrite(fd, buf->data, sectorsize, sb_bytenr);
1756         if (ret != sectorsize)
1757                 goto fail;
1758
1759         ret = 0;
1760 fail:
1761         free(buf);
1762         if (ret > 0)
1763                 ret = -1;
1764         return ret;
1765 }
1766
1767 static int relocate_one_reference(struct btrfs_trans_handle *trans,
1768                                   struct btrfs_root *root,
1769                                   u64 extent_start, u64 extent_size,
1770                                   struct btrfs_key *extent_key,
1771                                   struct extent_io_tree *reloc_tree)
1772 {
1773         struct extent_buffer *leaf;
1774         struct btrfs_file_extent_item *fi;
1775         struct btrfs_key key;
1776         struct btrfs_path path;
1777         struct btrfs_inode_item inode;
1778         struct blk_iterate_data data;
1779         u64 bytenr;
1780         u64 num_bytes;
1781         u64 cur_offset;
1782         u64 new_pos;
1783         u64 nbytes;
1784         u64 sector_end;
1785         u32 sectorsize = root->sectorsize;
1786         unsigned long ptr;
1787         int datacsum;
1788         int fd;
1789         int ret;
1790
1791         btrfs_init_path(&path);
1792         ret = btrfs_search_slot(trans, root, extent_key, &path, -1, 1);
1793         if (ret)
1794                 goto fail;
1795
1796         leaf = path.nodes[0];
1797         fi = btrfs_item_ptr(leaf, path.slots[0],
1798                             struct btrfs_file_extent_item);
1799         BUG_ON(btrfs_file_extent_offset(leaf, fi) > 0);
1800         if (extent_start != btrfs_file_extent_disk_bytenr(leaf, fi) ||
1801             extent_size != btrfs_file_extent_disk_num_bytes(leaf, fi)) {
1802                 ret = 1;
1803                 goto fail;
1804         }
1805
1806         bytenr = extent_start + btrfs_file_extent_offset(leaf, fi);
1807         num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
1808
1809         ret = btrfs_del_item(trans, root, &path);
1810         if (ret)
1811                 goto fail;
1812
1813         ret = btrfs_free_extent(trans, root, extent_start, extent_size, 0,
1814                                 root->root_key.objectid,
1815                                 extent_key->objectid, extent_key->offset);
1816         if (ret)
1817                 goto fail;
1818
1819         btrfs_release_path(&path);
1820
1821         key.objectid = extent_key->objectid;
1822         key.offset = 0;
1823         key.type =  BTRFS_INODE_ITEM_KEY;
1824         ret = btrfs_lookup_inode(trans, root, &path, &key, 0);
1825         if (ret)
1826                 goto fail;
1827
1828         leaf = path.nodes[0];
1829         ptr = btrfs_item_ptr_offset(leaf, path.slots[0]);
1830         read_extent_buffer(leaf, &inode, ptr, sizeof(inode));
1831         btrfs_release_path(&path);
1832
1833         BUG_ON(num_bytes & (sectorsize - 1));
1834         nbytes = btrfs_stack_inode_nbytes(&inode) - num_bytes;
1835         btrfs_set_stack_inode_nbytes(&inode, nbytes);
1836         datacsum = !(btrfs_stack_inode_flags(&inode) & BTRFS_INODE_NODATASUM);
1837
1838         data = (struct blk_iterate_data) {
1839                 .trans          = trans,
1840                 .root           = root,
1841                 .inode          = &inode,
1842                 .objectid       = extent_key->objectid,
1843                 .first_block    = extent_key->offset / sectorsize,
1844                 .disk_block     = 0,
1845                 .num_blocks     = 0,
1846                 .boundary       = (u64)-1,
1847                 .checksum       = datacsum,
1848                 .errcode        = 0,
1849         };
1850
1851         cur_offset = extent_key->offset;
1852         while (num_bytes > 0) {
1853                 sector_end = bytenr + sectorsize - 1;
1854                 if (test_range_bit(reloc_tree, bytenr, sector_end,
1855                                    EXTENT_LOCKED, 1)) {
1856                         ret = get_state_private(reloc_tree, bytenr, &new_pos);
1857                         BUG_ON(ret);
1858                 } else {
1859                         ret = custom_alloc_extent(root, sectorsize, 0, &key);
1860                         if (ret)
1861                                 goto fail;
1862                         new_pos = key.objectid;
1863
1864                         if (cur_offset == extent_key->offset) {
1865                                 fd = root->fs_info->fs_devices->latest_bdev;
1866                                 readahead(fd, bytenr, num_bytes);
1867                         }
1868                         ret = copy_disk_extent(root, new_pos, bytenr,
1869                                                sectorsize);
1870                         if (ret)
1871                                 goto fail;
1872                         ret = set_extent_bits(reloc_tree, bytenr, sector_end,
1873                                               EXTENT_LOCKED, GFP_NOFS);
1874                         BUG_ON(ret);
1875                         ret = set_state_private(reloc_tree, bytenr, new_pos);
1876                         BUG_ON(ret);
1877                 }
1878
1879                 ret = block_iterate_proc(NULL, new_pos / sectorsize,
1880                                          cur_offset / sectorsize, &data);
1881                 if (ret & BLOCK_ABORT) {
1882                         ret = data.errcode;
1883                         goto fail;
1884                 }
1885
1886                 cur_offset += sectorsize;
1887                 bytenr += sectorsize;
1888                 num_bytes -= sectorsize;
1889         }
1890
1891         if (data.num_blocks > 0) {
1892                 ret = record_file_blocks(trans, root,
1893                                          extent_key->objectid, &inode,
1894                                          data.first_block, data.disk_block,
1895                                          data.num_blocks, datacsum);
1896                 if (ret)
1897                         goto fail;
1898         }
1899
1900         key.objectid = extent_key->objectid;
1901         key.offset = 0;
1902         key.type =  BTRFS_INODE_ITEM_KEY;
1903         ret = btrfs_lookup_inode(trans, root, &path, &key, 1);
1904         if (ret)
1905                 goto fail;
1906
1907         leaf = path.nodes[0];
1908         ptr = btrfs_item_ptr_offset(leaf, path.slots[0]);
1909         write_extent_buffer(leaf, &inode, ptr, sizeof(inode));
1910         btrfs_mark_buffer_dirty(leaf);
1911         btrfs_release_path(&path);
1912
1913 fail:
1914         btrfs_release_path(&path);
1915         return ret;
1916 }
1917
1918 static int relocate_extents_range(struct btrfs_root *fs_root,
1919                                   struct btrfs_root *ext2_root,
1920                                   u64 start_byte, u64 end_byte)
1921 {
1922         struct btrfs_fs_info *info = fs_root->fs_info;
1923         struct btrfs_root *extent_root = info->extent_root;
1924         struct btrfs_root *cur_root = NULL;
1925         struct btrfs_trans_handle *trans;
1926         struct btrfs_extent_data_ref *dref;
1927         struct btrfs_extent_inline_ref *iref;
1928         struct btrfs_extent_item *ei;
1929         struct extent_buffer *leaf;
1930         struct btrfs_key key;
1931         struct btrfs_key extent_key;
1932         struct btrfs_path path;
1933         struct extent_io_tree reloc_tree;
1934         unsigned long ptr;
1935         unsigned long end;
1936         u64 cur_byte;
1937         u64 num_bytes;
1938         u64 ref_root;
1939         u64 num_extents;
1940         int pass = 0;
1941         int ret;
1942
1943         btrfs_init_path(&path);
1944         extent_io_tree_init(&reloc_tree);
1945
1946         key.objectid = start_byte;
1947         key.offset = 0;
1948         key.type = BTRFS_EXTENT_ITEM_KEY;
1949         ret = btrfs_search_slot(NULL, extent_root, &key, &path, 0, 0);
1950         if (ret < 0)
1951                 goto fail;
1952         if (ret > 0) {
1953                 ret = btrfs_previous_item(extent_root, &path, 0,
1954                                           BTRFS_EXTENT_ITEM_KEY);
1955                 if (ret < 0)
1956                         goto fail;
1957                 if (ret == 0) {
1958                         leaf = path.nodes[0];
1959                         btrfs_item_key_to_cpu(leaf, &key, path.slots[0]);
1960                         if (key.objectid + key.offset > start_byte)
1961                                 start_byte = key.objectid;
1962                 }
1963         }
1964         btrfs_release_path(&path);
1965 again:
1966         cur_root = (pass % 2 == 0) ? ext2_root : fs_root;
1967         num_extents = 0;
1968
1969         trans = btrfs_start_transaction(cur_root, 1);
1970         BUG_ON(!trans);
1971
1972         cur_byte = start_byte;
1973         while (1) {
1974                 key.objectid = cur_byte;
1975                 key.offset = 0;
1976                 key.type = BTRFS_EXTENT_ITEM_KEY;
1977                 ret = btrfs_search_slot(trans, extent_root,
1978                                         &key, &path, 0, 0);
1979                 if (ret < 0)
1980                         goto fail;
1981 next:
1982                 leaf = path.nodes[0];
1983                 if (path.slots[0] >= btrfs_header_nritems(leaf)) {
1984                         ret = btrfs_next_leaf(extent_root, &path);
1985                         if (ret < 0)
1986                                 goto fail;
1987                         if (ret > 0)
1988                                 break;
1989                         leaf = path.nodes[0];
1990                 }
1991
1992                 btrfs_item_key_to_cpu(leaf, &key, path.slots[0]);
1993                 if (key.objectid < cur_byte ||
1994                     key.type != BTRFS_EXTENT_ITEM_KEY) {
1995                         path.slots[0]++;
1996                         goto next;
1997                 }
1998                 if (key.objectid >= end_byte)
1999                         break;
2000
2001                 num_extents++;
2002
2003                 cur_byte = key.objectid;
2004                 num_bytes = key.offset;
2005                 ei = btrfs_item_ptr(leaf, path.slots[0],
2006                                     struct btrfs_extent_item);
2007                 BUG_ON(!(btrfs_extent_flags(leaf, ei) &
2008                          BTRFS_EXTENT_FLAG_DATA));
2009
2010                 ptr = btrfs_item_ptr_offset(leaf, path.slots[0]);
2011                 end = ptr + btrfs_item_size_nr(leaf, path.slots[0]);
2012
2013                 ptr += sizeof(struct btrfs_extent_item);
2014
2015                 while (ptr < end) {
2016                         iref = (struct btrfs_extent_inline_ref *)ptr;
2017                         key.type = btrfs_extent_inline_ref_type(leaf, iref);
2018                         BUG_ON(key.type != BTRFS_EXTENT_DATA_REF_KEY);
2019                         dref = (struct btrfs_extent_data_ref *)(&iref->offset);
2020                         ref_root = btrfs_extent_data_ref_root(leaf, dref);
2021                         extent_key.objectid =
2022                                 btrfs_extent_data_ref_objectid(leaf, dref);
2023                         extent_key.offset =
2024                                 btrfs_extent_data_ref_offset(leaf, dref);
2025                         extent_key.type = BTRFS_EXTENT_DATA_KEY;
2026                         BUG_ON(btrfs_extent_data_ref_count(leaf, dref) != 1);
2027
2028                         if (ref_root == cur_root->root_key.objectid)
2029                                 break;
2030
2031                         ptr += btrfs_extent_inline_ref_size(key.type);
2032                 }
2033
2034                 if (ptr >= end) {
2035                         path.slots[0]++;
2036                         goto next;
2037                 }
2038
2039                 ret = relocate_one_reference(trans, cur_root, cur_byte,
2040                                              num_bytes, &extent_key,
2041                                              &reloc_tree);
2042                 if (ret < 0)
2043                         goto fail;
2044
2045                 cur_byte += num_bytes;
2046                 btrfs_release_path(&path);
2047
2048                 if (trans->blocks_used >= 4096) {
2049                         ret = btrfs_commit_transaction(trans, cur_root);
2050                         BUG_ON(ret);
2051                         trans = btrfs_start_transaction(cur_root, 1);
2052                         BUG_ON(!trans);
2053                 }
2054         }
2055         btrfs_release_path(&path);
2056
2057         ret = btrfs_commit_transaction(trans, cur_root);
2058         BUG_ON(ret);
2059
2060         if (num_extents > 0 && pass++ < 16)
2061                 goto again;
2062
2063         ret = (num_extents > 0) ? -1 : 0;
2064 fail:
2065         btrfs_release_path(&path);
2066         extent_io_tree_cleanup(&reloc_tree);
2067         return ret;
2068 }
2069
2070 /*
2071  * relocate data in system chunk
2072  */
2073 static int cleanup_sys_chunk(struct btrfs_root *fs_root,
2074                              struct btrfs_root *ext2_root)
2075 {
2076         struct btrfs_block_group_cache *cache;
2077         int i, ret = 0;
2078         u64 offset = 0;
2079         u64 end_byte;
2080
2081         while(1) {
2082                 cache = btrfs_lookup_block_group(fs_root->fs_info, offset);
2083                 if (!cache)
2084                         break;
2085
2086                 end_byte = cache->key.objectid + cache->key.offset;
2087                 if (cache->flags & BTRFS_BLOCK_GROUP_SYSTEM) {
2088                         ret = relocate_extents_range(fs_root, ext2_root,
2089                                                      cache->key.objectid,
2090                                                      end_byte);
2091                         if (ret)
2092                                 goto fail;
2093                 }
2094                 offset = end_byte;
2095         }
2096         for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
2097                 offset = btrfs_sb_offset(i);
2098                 offset &= ~((u64)BTRFS_STRIPE_LEN - 1);
2099
2100                 ret = relocate_extents_range(fs_root, ext2_root,
2101                                              offset, offset + BTRFS_STRIPE_LEN);
2102                 if (ret)
2103                         goto fail;
2104         }
2105         ret = 0;
2106 fail:
2107         return ret;
2108 }
2109
2110 static int fixup_chunk_mapping(struct btrfs_root *root)
2111 {
2112         struct btrfs_trans_handle *trans;
2113         struct btrfs_fs_info *info = root->fs_info;
2114         struct btrfs_root *chunk_root = info->chunk_root;
2115         struct extent_buffer *leaf;
2116         struct btrfs_key key;
2117         struct btrfs_path path;
2118         struct btrfs_chunk chunk;
2119         unsigned long ptr;
2120         u32 size;
2121         u64 type;
2122         int ret;
2123
2124         btrfs_init_path(&path);
2125
2126         trans = btrfs_start_transaction(root, 1);
2127         BUG_ON(!trans);
2128
2129         /*
2130          * recow the whole chunk tree. this will move all chunk tree blocks
2131          * into system block group.
2132          */
2133         memset(&key, 0, sizeof(key));
2134         while (1) {
2135                 ret = btrfs_search_slot(trans, chunk_root, &key, &path, 0, 1);
2136                 if (ret < 0)
2137                         goto err;
2138
2139                 ret = btrfs_next_leaf(chunk_root, &path);
2140                 if (ret < 0)
2141                         goto err;
2142                 if (ret > 0)
2143                         break;
2144
2145                 btrfs_item_key_to_cpu(path.nodes[0], &key, path.slots[0]);
2146                 btrfs_release_path(&path);
2147         }
2148         btrfs_release_path(&path);
2149
2150         /* fixup the system chunk array in super block */
2151         btrfs_set_super_sys_array_size(info->super_copy, 0);
2152
2153         key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2154         key.offset = 0;
2155         key.type = BTRFS_CHUNK_ITEM_KEY;
2156
2157         ret = btrfs_search_slot(trans, chunk_root, &key, &path, 0, 0);
2158         if (ret < 0)
2159                 goto err;
2160         BUG_ON(ret != 0);
2161         while(1) {
2162                 leaf = path.nodes[0];
2163                 if (path.slots[0] >= btrfs_header_nritems(leaf)) {
2164                         ret = btrfs_next_leaf(chunk_root, &path);
2165                         if (ret < 0)
2166                                 goto err;
2167                         if (ret > 0)
2168                                 break;
2169                         leaf = path.nodes[0];
2170                 }
2171                 btrfs_item_key_to_cpu(leaf, &key, path.slots[0]);
2172                 if (key.type != BTRFS_CHUNK_ITEM_KEY)
2173                         goto next;
2174
2175                 ptr = btrfs_item_ptr_offset(leaf, path.slots[0]);
2176                 size = btrfs_item_size_nr(leaf, path.slots[0]);
2177                 BUG_ON(size != sizeof(chunk));
2178                 read_extent_buffer(leaf, &chunk, ptr, size);
2179                 type = btrfs_stack_chunk_type(&chunk);
2180
2181                 if (!(type & BTRFS_BLOCK_GROUP_SYSTEM))
2182                         goto next;
2183
2184                 ret = btrfs_add_system_chunk(trans, chunk_root, &key,
2185                                              &chunk, size);
2186                 if (ret)
2187                         goto err;
2188 next:
2189                 path.slots[0]++;
2190         }
2191
2192         ret = btrfs_commit_transaction(trans, root);
2193         BUG_ON(ret);
2194 err:
2195         btrfs_release_path(&path);
2196         return ret;
2197 }
2198
2199 static int do_convert(const char *devname, int datacsum, int packing,
2200                 int noxattr)
2201 {
2202         int i, ret;
2203         int fd = -1;
2204         u32 blocksize;
2205         u64 blocks[7];
2206         u64 total_bytes;
2207         u64 super_bytenr;
2208         ext2_filsys ext2_fs;
2209         struct btrfs_root *root;
2210         struct btrfs_root *ext2_root;
2211
2212         ret = open_ext2fs(devname, &ext2_fs);
2213         if (ret) {
2214                 fprintf(stderr, "unable to open the Ext2fs\n");
2215                 goto fail;
2216         }
2217         blocksize = ext2_fs->blocksize;
2218         total_bytes = (u64)ext2_fs->super->s_blocks_count * blocksize;
2219         if (blocksize < 4096) {
2220                 fprintf(stderr, "block size is too small\n");
2221                 goto fail;
2222         }
2223         if (!(ext2_fs->super->s_feature_incompat &
2224               EXT2_FEATURE_INCOMPAT_FILETYPE)) {
2225                 fprintf(stderr, "filetype feature is missing\n");
2226                 goto fail;
2227         }
2228         for (i = 0; i < 7; i++) {
2229                 ret = ext2_alloc_block(ext2_fs, 0, blocks + i);
2230                 if (ret) {
2231                         fprintf(stderr, "not enough free space\n");
2232                         goto fail;
2233                 }
2234                 blocks[i] *= blocksize;
2235         }
2236         super_bytenr = blocks[0];
2237         fd = open(devname, O_RDWR);
2238         if (fd < 0) {
2239                 fprintf(stderr, "unable to open %s\n", devname);
2240                 goto fail;
2241         }
2242         ret = make_btrfs(fd, devname, ext2_fs->super->s_volume_name,
2243                          blocks, total_bytes, blocksize, blocksize,
2244                          blocksize, blocksize, 0);
2245         if (ret) {
2246                 fprintf(stderr, "unable to create initial ctree: %s\n",
2247                         strerror(-ret));
2248                 goto fail;
2249         }
2250         /* create a system chunk that maps the whole device */
2251         ret = prepare_system_chunk(fd, super_bytenr, blocksize);
2252         if (ret) {
2253                 fprintf(stderr, "unable to update system chunk\n");
2254                 goto fail;
2255         }
2256         root = open_ctree_fd(fd, devname, super_bytenr, OPEN_CTREE_WRITES);
2257         if (!root) {
2258                 fprintf(stderr, "unable to open ctree\n");
2259                 goto fail;
2260         }
2261         ret = cache_free_extents(root, ext2_fs);
2262         if (ret) {
2263                 fprintf(stderr, "error during cache_free_extents %d\n", ret);
2264                 goto fail;
2265         }
2266         root->fs_info->extent_ops = &extent_ops;
2267         /* recover block allocation bitmap */
2268         for (i = 0; i < 7; i++) {
2269                 blocks[i] /= blocksize;
2270                 ext2_free_block(ext2_fs, blocks[i]);
2271         }
2272         ret = init_btrfs(root);
2273         if (ret) {
2274                 fprintf(stderr, "unable to setup the root tree\n");
2275                 goto fail;
2276         }
2277         printf("creating btrfs metadata.\n");
2278         ret = copy_inodes(root, ext2_fs, datacsum, packing, noxattr);
2279         if (ret) {
2280                 fprintf(stderr, "error during copy_inodes %d\n", ret);
2281                 goto fail;
2282         }
2283         printf("creating ext2fs image file.\n");
2284         ext2_root = link_subvol(root, "ext2_saved", EXT2_IMAGE_SUBVOL_OBJECTID);
2285         if (!ext2_root) {
2286                 fprintf(stderr, "unable to create subvol\n");
2287                 goto fail;
2288         }
2289         ret = create_ext2_image(ext2_root, ext2_fs, "image");
2290         if (ret) {
2291                 fprintf(stderr, "error during create_ext2_image %d\n", ret);
2292                 goto fail;
2293         }
2294         printf("cleaning up system chunk.\n");
2295         ret = cleanup_sys_chunk(root, ext2_root);
2296         if (ret) {
2297                 fprintf(stderr, "error during cleanup_sys_chunk %d\n", ret);
2298                 goto fail;
2299         }
2300         ret = close_ctree(root);
2301         if (ret) {
2302                 fprintf(stderr, "error during close_ctree %d\n", ret);
2303                 goto fail;
2304         }
2305         close_ext2fs(ext2_fs);
2306
2307         /*
2308          * If this step succeed, we get a mountable btrfs. Otherwise
2309          * the ext2fs is left unchanged.
2310          */
2311         ret = migrate_super_block(fd, super_bytenr, blocksize);
2312         if (ret) {
2313                 fprintf(stderr, "unable to migrate super block\n");
2314                 goto fail;
2315         }
2316
2317         root = open_ctree_fd(fd, devname, 0, OPEN_CTREE_WRITES);
2318         if (!root) {
2319                 fprintf(stderr, "unable to open ctree\n");
2320                 goto fail;
2321         }
2322         /* move chunk tree into system chunk. */
2323         ret = fixup_chunk_mapping(root);
2324         if (ret) {
2325                 fprintf(stderr, "error during fixup_chunk_tree\n");
2326                 goto fail;
2327         }
2328         ret = close_ctree(root);
2329         close(fd);
2330
2331         printf("conversion complete.\n");
2332         return 0;
2333 fail:
2334         if (fd != -1)
2335                 close(fd);
2336         fprintf(stderr, "conversion aborted.\n");
2337         return -1;
2338 }
2339
2340 static int may_rollback(struct btrfs_root *root)
2341 {
2342         struct btrfs_fs_info *info = root->fs_info;
2343         struct btrfs_multi_bio *multi = NULL;
2344         u64 bytenr;
2345         u64 length;
2346         u64 physical;
2347         u64 total_bytes;
2348         int num_stripes;
2349         int ret;
2350
2351         if (btrfs_super_num_devices(info->super_copy) != 1)
2352                 goto fail;
2353
2354         bytenr = BTRFS_SUPER_INFO_OFFSET;
2355         total_bytes = btrfs_super_total_bytes(root->fs_info->super_copy);
2356
2357         while (1) {
2358                 ret = btrfs_map_block(&info->mapping_tree, WRITE, bytenr,
2359                                       &length, &multi, 0, NULL);
2360                 if (ret)
2361                         goto fail;
2362
2363                 num_stripes = multi->num_stripes;
2364                 physical = multi->stripes[0].physical;
2365                 kfree(multi);
2366
2367                 if (num_stripes != 1 || physical != bytenr)
2368                         goto fail;
2369
2370                 bytenr += length;
2371                 if (bytenr >= total_bytes)
2372                         break;
2373         }
2374         return 0;
2375 fail:
2376         return -1;
2377 }
2378
2379 static int do_rollback(const char *devname, int force)
2380 {
2381         int fd = -1;
2382         int ret;
2383         int i;
2384         struct btrfs_root *root;
2385         struct btrfs_root *ext2_root;
2386         struct btrfs_root *chunk_root;
2387         struct btrfs_dir_item *dir;
2388         struct btrfs_inode_item *inode;
2389         struct btrfs_file_extent_item *fi;
2390         struct btrfs_trans_handle *trans;
2391         struct extent_buffer *leaf;
2392         struct btrfs_block_group_cache *cache1;
2393         struct btrfs_block_group_cache *cache2;
2394         struct btrfs_key key;
2395         struct btrfs_path path;
2396         struct extent_io_tree io_tree;
2397         char *buf = NULL;
2398         char *name;
2399         u64 bytenr;
2400         u64 num_bytes;
2401         u64 root_dir;
2402         u64 objectid;
2403         u64 offset;
2404         u64 start;
2405         u64 end;
2406         u64 sb_bytenr;
2407         u64 first_free;
2408         u64 total_bytes;
2409         u32 sectorsize;
2410
2411         extent_io_tree_init(&io_tree);
2412
2413         fd = open(devname, O_RDWR);
2414         if (fd < 0) {
2415                 fprintf(stderr, "unable to open %s\n", devname);
2416                 goto fail;
2417         }
2418         root = open_ctree_fd(fd, devname, 0, OPEN_CTREE_WRITES);
2419         if (!root) {
2420                 fprintf(stderr, "unable to open ctree\n");
2421                 goto fail;
2422         }
2423         ret = may_rollback(root);
2424         if (ret < 0) {
2425                 fprintf(stderr, "unable to do rollback\n");
2426                 goto fail;
2427         }
2428
2429         sectorsize = root->sectorsize;
2430         buf = malloc(sectorsize);
2431         if (!buf) {
2432                 fprintf(stderr, "unable to allocate memory\n");
2433                 goto fail;
2434         }
2435
2436         btrfs_init_path(&path);
2437
2438         key.objectid = EXT2_IMAGE_SUBVOL_OBJECTID;
2439         key.type = BTRFS_ROOT_ITEM_KEY;
2440         key.offset = (u64)-1;
2441         ext2_root = btrfs_read_fs_root(root->fs_info, &key);
2442         if (!ext2_root || IS_ERR(ext2_root)) {
2443                 fprintf(stderr, "unable to open subvol %llu\n",
2444                         key.objectid);
2445                 goto fail;
2446         }
2447
2448         name = "image";
2449         root_dir = btrfs_root_dirid(&root->root_item);
2450         dir = btrfs_lookup_dir_item(NULL, ext2_root, &path,
2451                                    root_dir, name, strlen(name), 0);
2452         if (!dir || IS_ERR(dir)) {
2453                 fprintf(stderr, "unable to find file %s\n", name);
2454                 goto fail;
2455         }
2456         leaf = path.nodes[0];
2457         btrfs_dir_item_key_to_cpu(leaf, dir, &key);
2458         btrfs_release_path(&path);
2459
2460         objectid = key.objectid;
2461
2462         ret = btrfs_lookup_inode(NULL, ext2_root, &path, &key, 0);
2463         if (ret) {
2464                 fprintf(stderr, "unable to find inode item\n");
2465                 goto fail;
2466         }
2467         leaf = path.nodes[0];
2468         inode = btrfs_item_ptr(leaf, path.slots[0], struct btrfs_inode_item);
2469         total_bytes = btrfs_inode_size(leaf, inode);
2470         btrfs_release_path(&path);
2471
2472         key.objectid = objectid;
2473         key.offset = 0;
2474         btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
2475         ret = btrfs_search_slot(NULL, ext2_root, &key, &path, 0, 0);
2476         if (ret != 0) {
2477                 fprintf(stderr, "unable to find first file extent\n");
2478                 btrfs_release_path(&path);
2479                 goto fail;
2480         }
2481
2482         /* build mapping tree for the relocated blocks */
2483         for (offset = 0; offset < total_bytes; ) {
2484                 leaf = path.nodes[0];
2485                 if (path.slots[0] >= btrfs_header_nritems(leaf)) {
2486                         ret = btrfs_next_leaf(root, &path);
2487                         if (ret != 0)
2488                                 break;  
2489                         continue;
2490                 }
2491
2492                 btrfs_item_key_to_cpu(leaf, &key, path.slots[0]);
2493                 if (key.objectid != objectid || key.offset != offset ||
2494                     btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
2495                         break;
2496
2497                 fi = btrfs_item_ptr(leaf, path.slots[0],
2498                                     struct btrfs_file_extent_item);
2499                 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG)
2500                         break;
2501                 if (btrfs_file_extent_compression(leaf, fi) ||
2502                     btrfs_file_extent_encryption(leaf, fi) ||
2503                     btrfs_file_extent_other_encoding(leaf, fi))
2504                         break;
2505
2506                 bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
2507                 /* skip holes and direct mapped extents */
2508                 if (bytenr == 0 || bytenr == offset)
2509                         goto next_extent;
2510
2511                 bytenr += btrfs_file_extent_offset(leaf, fi);
2512                 num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
2513
2514                 cache1 = btrfs_lookup_block_group(root->fs_info, offset);
2515                 cache2 =  btrfs_lookup_block_group(root->fs_info,
2516                                                    offset + num_bytes - 1);
2517                 if (!cache1 || cache1 != cache2 ||
2518                     (!(cache1->flags & BTRFS_BLOCK_GROUP_SYSTEM) &&
2519                      !intersect_with_sb(offset, num_bytes)))
2520                         break;
2521
2522                 set_extent_bits(&io_tree, offset, offset + num_bytes - 1,
2523                                 EXTENT_LOCKED, GFP_NOFS);
2524                 set_state_private(&io_tree, offset, bytenr);
2525 next_extent:
2526                 offset += btrfs_file_extent_num_bytes(leaf, fi);
2527                 path.slots[0]++;
2528         }
2529         btrfs_release_path(&path);
2530
2531         if (offset < total_bytes) {
2532                 fprintf(stderr, "unable to build extent mapping\n");
2533                 goto fail;
2534         }
2535
2536         first_free = BTRFS_SUPER_INFO_OFFSET + 2 * sectorsize - 1;
2537         first_free &= ~((u64)sectorsize - 1);
2538         /* backup for extent #0 should exist */
2539         if(!test_range_bit(&io_tree, 0, first_free - 1, EXTENT_LOCKED, 1)) {
2540                 fprintf(stderr, "no backup for the first extent\n");
2541                 goto fail;
2542         }
2543         /* force no allocation from system block group */
2544         root->fs_info->system_allocs = -1;
2545         trans = btrfs_start_transaction(root, 1);
2546         BUG_ON(!trans);
2547         /*
2548          * recow the whole chunk tree, this will remove all chunk tree blocks
2549          * from system block group
2550          */
2551         chunk_root = root->fs_info->chunk_root;
2552         memset(&key, 0, sizeof(key));
2553         while (1) {
2554                 ret = btrfs_search_slot(trans, chunk_root, &key, &path, 0, 1);
2555                 if (ret < 0)
2556                         break;
2557
2558                 ret = btrfs_next_leaf(chunk_root, &path);
2559                 if (ret)
2560                         break;
2561
2562                 btrfs_item_key_to_cpu(path.nodes[0], &key, path.slots[0]);
2563                 btrfs_release_path(&path);
2564         }
2565         btrfs_release_path(&path);
2566
2567         offset = 0;
2568         num_bytes = 0;
2569         while(1) {
2570                 cache1 = btrfs_lookup_block_group(root->fs_info, offset);
2571                 if (!cache1)
2572                         break;
2573
2574                 if (cache1->flags & BTRFS_BLOCK_GROUP_SYSTEM)
2575                         num_bytes += btrfs_block_group_used(&cache1->item);
2576
2577                 offset = cache1->key.objectid + cache1->key.offset;
2578         }
2579         /* only extent #0 left in system block group? */
2580         if (num_bytes > first_free) {
2581                 fprintf(stderr, "unable to empty system block group\n");
2582                 goto fail;
2583         }
2584         /* create a system chunk that maps the whole device */
2585         ret = prepare_system_chunk_sb(root->fs_info->super_copy);
2586         if (ret) {
2587                 fprintf(stderr, "unable to update system chunk\n");
2588                 goto fail;
2589         }
2590
2591         ret = btrfs_commit_transaction(trans, root);
2592         BUG_ON(ret);
2593
2594         ret = close_ctree(root);
2595         if (ret) {
2596                 fprintf(stderr, "error during close_ctree %d\n", ret);
2597                 goto fail;
2598         }
2599
2600         /* zero btrfs super block mirrors */
2601         memset(buf, 0, sectorsize);
2602         for (i = 1 ; i < BTRFS_SUPER_MIRROR_MAX; i++) {
2603                 bytenr = btrfs_sb_offset(i);
2604                 if (bytenr >= total_bytes)
2605                         break;
2606                 ret = pwrite(fd, buf, sectorsize, bytenr);
2607         }
2608
2609         sb_bytenr = (u64)-1;
2610         /* copy all relocated blocks back */
2611         while(1) {
2612                 ret = find_first_extent_bit(&io_tree, 0, &start, &end,
2613                                             EXTENT_LOCKED);
2614                 if (ret)
2615                         break;
2616
2617                 ret = get_state_private(&io_tree, start, &bytenr);
2618                 BUG_ON(ret);
2619
2620                 clear_extent_bits(&io_tree, start, end, EXTENT_LOCKED,
2621                                   GFP_NOFS);
2622
2623                 while (start <= end) {
2624                         if (start == BTRFS_SUPER_INFO_OFFSET) {
2625                                 sb_bytenr = bytenr;
2626                                 goto next_sector;
2627                         }
2628                         ret = pread(fd, buf, sectorsize, bytenr);
2629                         if (ret < 0) {
2630                                 fprintf(stderr, "error during pread %d\n", ret);
2631                                 goto fail;
2632                         }
2633                         BUG_ON(ret != sectorsize);
2634                         ret = pwrite(fd, buf, sectorsize, start);
2635                         if (ret < 0) {
2636                                 fprintf(stderr, "error during pwrite %d\n", ret);
2637                                 goto fail;
2638                         }
2639                         BUG_ON(ret != sectorsize);
2640 next_sector:
2641                         start += sectorsize;
2642                         bytenr += sectorsize;
2643                 }
2644         }
2645
2646         ret = fsync(fd);
2647         if (ret) {
2648                 fprintf(stderr, "error during fsync %d\n", ret);
2649                 goto fail;
2650         }
2651         /*
2652          * finally, overwrite btrfs super block.
2653          */
2654         ret = pread(fd, buf, sectorsize, sb_bytenr);
2655         if (ret < 0) {
2656                 fprintf(stderr, "error during pread %d\n", ret);
2657                 goto fail;
2658         }
2659         BUG_ON(ret != sectorsize);
2660         ret = pwrite(fd, buf, sectorsize, BTRFS_SUPER_INFO_OFFSET);
2661         if (ret < 0) {
2662                 fprintf(stderr, "error during pwrite %d\n", ret);
2663                 goto fail;
2664         }
2665         BUG_ON(ret != sectorsize);
2666         ret = fsync(fd);
2667         if (ret) {
2668                 fprintf(stderr, "error during fsync %d\n", ret);
2669                 goto fail;
2670         }
2671
2672         close(fd);
2673         free(buf);
2674         extent_io_tree_cleanup(&io_tree);
2675         printf("rollback complete.\n");
2676         return 0;
2677
2678 fail:
2679         if (fd != -1)
2680                 close(fd);
2681         free(buf);
2682         fprintf(stderr, "rollback aborted.\n");
2683         return -1;
2684 }
2685
2686 static void print_usage(void)
2687 {
2688         printf("usage: btrfs-convert [-d] [-i] [-n] [-r] device\n");
2689         printf("\t-d disable data checksum\n");
2690         printf("\t-i ignore xattrs and ACLs\n");
2691         printf("\t-n disable packing of small files\n");
2692         printf("\t-r roll back to ext2fs\n");
2693 }
2694
2695 int main(int argc, char *argv[])
2696 {
2697         int ret;
2698         int packing = 1;
2699         int noxattr = 0;
2700         int datacsum = 1;
2701         int rollback = 0;
2702         char *file;
2703         while(1) {
2704                 int c = getopt(argc, argv, "dinr");
2705                 if (c < 0)
2706                         break;
2707                 switch(c) {
2708                         case 'd':
2709                                 datacsum = 0;
2710                                 break;
2711                         case 'i':
2712                                 noxattr = 1;
2713                                 break;
2714                         case 'n':
2715                                 packing = 0;
2716                                 break;
2717                         case 'r':
2718                                 rollback = 1;
2719                                 break;
2720                         default:
2721                                 print_usage();
2722                                 return 1;
2723                 }
2724         }
2725         argc = argc - optind;
2726         if (argc != 1) {
2727                 print_usage();
2728                 return 1;
2729         }
2730
2731         file = argv[optind];
2732         if (check_mounted(file)) {
2733                 fprintf(stderr, "%s is mounted\n", file);
2734                 return 1;
2735         }
2736
2737         if (rollback) {
2738                 ret = do_rollback(file, 0);
2739         } else {
2740                 ret = do_convert(file, datacsum, packing, noxattr);
2741         }
2742         if (ret)
2743                 return 1;
2744         return 0;
2745 }