Add a readonly flag open_ctree to force RO opens
[btrfs-progs-unstable.git] / convert.c
blob219f849bc37a993755209bd9e65670c79571fc3c
1 /*
2 * Copyright (C) 2007 Oracle. All rights reserved.
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.
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.
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.
19 #define _XOPEN_SOURCE 600
20 #define _GNU_SOURCE 1
21 #ifndef __CHECKER__
22 #include <sys/ioctl.h>
23 #include <sys/mount.h>
24 #endif
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <sys/types.h>
28 #include <sys/stat.h>
29 #include <sys/acl.h>
30 #include <fcntl.h>
31 #include <unistd.h>
32 #include <uuid/uuid.h>
33 #include <linux/fs.h>
34 #include "kerncompat.h"
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 #define INO_OFFSET (BTRFS_FIRST_FREE_OBJECTID - EXT2_ROOT_INO)
46 * Open Ext2fs in readonly mode, read block allocation bitmap and
47 * inode bitmap into memory.
49 static int open_ext2fs(const char *name, ext2_filsys *ret_fs)
51 errcode_t ret;
52 ext2_filsys ext2_fs;
53 ret = ext2fs_open(name, 0, 0, 0, unix_io_manager, &ext2_fs);
54 if (ret) {
55 fprintf(stderr, "ext2fs_open: %s\n", error_message(ret));
56 goto fail;
58 ret = ext2fs_read_inode_bitmap(ext2_fs);
59 if (ret) {
60 fprintf(stderr, "ext2fs_read_inode_bitmap: %s\n",
61 error_message(ret));
62 goto fail;
64 ret = ext2fs_read_block_bitmap(ext2_fs);
65 if (ret) {
66 fprintf(stderr, "ext2fs_read_block_bitmap: %s\n",
67 error_message(ret));
68 goto fail;
70 *ret_fs = ext2_fs;
71 return 0;
72 fail:
73 return -1;
76 static int close_ext2fs(ext2_filsys fs)
78 ext2fs_close(fs);
79 return 0;
82 static int ext2_alloc_block(ext2_filsys fs, u64 goal, u64 *block_ret)
84 blk_t block;
86 if (!ext2fs_new_block(fs, goal, NULL, &block)) {
87 ext2fs_fast_mark_block_bitmap(fs->block_map, block);
88 *block_ret = block;
89 return 0;
91 return -ENOSPC;
94 static int ext2_free_block(ext2_filsys fs, u64 block)
96 BUG_ON(block != (blk_t)block);
97 ext2fs_fast_unmark_block_bitmap(fs->block_map, block);
98 return 0;
101 static int cache_free_extents(struct btrfs_root *root, ext2_filsys ext2_fs)
104 int ret = 0;
105 blk_t block;
106 u64 bytenr;
107 u64 blocksize = ext2_fs->blocksize;
109 block = ext2_fs->super->s_first_data_block;
110 for (; block < ext2_fs->super->s_blocks_count; block++) {
111 if (ext2fs_fast_test_block_bitmap(ext2_fs->block_map, block))
112 continue;
113 bytenr = block * blocksize;
114 ret = set_extent_dirty(&root->fs_info->free_space_cache,
115 bytenr, bytenr + blocksize - 1, 0);
116 if (ret)
117 break;
119 return ret;
122 static int custom_alloc_extent(struct btrfs_root *root, u64 num_bytes,
123 u64 hint_byte, struct btrfs_key *ins)
125 u64 start;
126 u64 end;
127 u64 last = hint_byte;
128 int ret;
129 int wrapped = 0;
130 struct btrfs_block_group_cache *cache;
132 while(1) {
133 ret = find_first_extent_bit(&root->fs_info->free_space_cache,
134 last, &start, &end, EXTENT_DIRTY);
135 if (ret) {
136 if (wrapped++ == 0) {
137 last = 0;
138 continue;
139 } else {
140 goto fail;
144 start = max(last, start);
145 last = end + 1;
146 if (last - start < num_bytes)
147 continue;
149 last = start + num_bytes;
150 if (test_range_bit(&root->fs_info->pinned_extents,
151 start, last - 1, EXTENT_DIRTY, 0))
152 continue;
154 cache = btrfs_lookup_block_group(root->fs_info, start);
155 BUG_ON(!cache);
156 if (cache->flags & BTRFS_BLOCK_GROUP_SYSTEM ||
157 last > cache->key.objectid + cache->key.offset) {
158 last = cache->key.objectid + cache->key.offset;
159 continue;
162 clear_extent_dirty(&root->fs_info->free_space_cache,
163 start, start + num_bytes - 1, 0);
165 ins->objectid = start;
166 ins->offset = num_bytes;
167 ins->type = BTRFS_EXTENT_ITEM_KEY;
168 return 0;
170 fail:
171 fprintf(stderr, "not enough free space\n");
172 return -ENOSPC;
175 static int custom_free_extent(struct btrfs_root *root, u64 bytenr,
176 u64 num_bytes)
178 return 0;
181 struct btrfs_extent_ops extent_ops = {
182 .alloc_extent = custom_alloc_extent,
183 .free_extent = custom_free_extent,
186 struct dir_iterate_data {
187 struct btrfs_trans_handle *trans;
188 struct btrfs_root *root;
189 struct btrfs_inode_item *inode;
190 u64 objectid;
191 u64 parent;
192 int errcode;
195 static u8 filetype_conversion_table[EXT2_FT_MAX] = {
196 [EXT2_FT_UNKNOWN] = BTRFS_FT_UNKNOWN,
197 [EXT2_FT_REG_FILE] = BTRFS_FT_REG_FILE,
198 [EXT2_FT_DIR] = BTRFS_FT_DIR,
199 [EXT2_FT_CHRDEV] = BTRFS_FT_CHRDEV,
200 [EXT2_FT_BLKDEV] = BTRFS_FT_BLKDEV,
201 [EXT2_FT_FIFO] = BTRFS_FT_FIFO,
202 [EXT2_FT_SOCK] = BTRFS_FT_SOCK,
203 [EXT2_FT_SYMLINK] = BTRFS_FT_SYMLINK,
206 static int dir_iterate_proc(ext2_ino_t dir, int entry,
207 struct ext2_dir_entry *old,
208 int offset, int blocksize,
209 char *buf,void *priv_data)
211 int ret;
212 int file_type;
213 u64 objectid;
214 u64 inode_size;
215 char dotdot[] = "..";
216 struct btrfs_key location;
217 struct ext2_dir_entry_2 *dirent = (struct ext2_dir_entry_2 *)old;
218 struct dir_iterate_data *idata = (struct dir_iterate_data *)priv_data;
220 objectid = dirent->inode + INO_OFFSET;
221 if (!strncmp(dirent->name, dotdot, dirent->name_len)) {
222 if (dirent->name_len == 2) {
223 BUG_ON(idata->parent != 0);
224 idata->parent = objectid;
226 return 0;
228 if (dirent->inode < EXT2_GOOD_OLD_FIRST_INO)
229 return 0;
231 location.objectid = objectid;
232 location.offset = 0;
233 btrfs_set_key_type(&location, BTRFS_INODE_ITEM_KEY);
235 file_type = dirent->file_type;
236 BUG_ON(file_type > EXT2_FT_SYMLINK);
237 ret = btrfs_insert_dir_item(idata->trans, idata->root,
238 dirent->name, dirent->name_len,
239 idata->objectid, &location,
240 filetype_conversion_table[file_type]);
241 if (ret)
242 goto fail;
243 ret = btrfs_insert_inode_ref(idata->trans, idata->root,
244 dirent->name, dirent->name_len,
245 objectid, idata->objectid);
246 if (ret)
247 goto fail;
248 inode_size = btrfs_stack_inode_size(idata->inode) +
249 dirent->name_len * 2;
250 btrfs_set_stack_inode_size(idata->inode, inode_size);
251 return 0;
252 fail:
253 idata->errcode = ret;
254 return BLOCK_ABORT;
257 static int create_dir_entries(struct btrfs_trans_handle *trans,
258 struct btrfs_root *root, u64 objectid,
259 struct btrfs_inode_item *btrfs_inode,
260 ext2_filsys ext2_fs, ext2_ino_t ext2_ino)
262 int ret;
263 errcode_t err;
264 struct dir_iterate_data data = {
265 .trans = trans,
266 .root = root,
267 .inode = btrfs_inode,
268 .objectid = objectid,
269 .parent = 0,
270 .errcode = 0,
273 err = ext2fs_dir_iterate2(ext2_fs, ext2_ino, 0, NULL,
274 dir_iterate_proc, &data);
275 if (err)
276 goto error;
277 ret = data.errcode;
278 if (ret == 0 && data.parent == objectid) {
279 ret = btrfs_insert_inode_ref(trans, root, "..", 2,
280 objectid, objectid);
282 return ret;
283 error:
284 fprintf(stderr, "ext2fs_dir_iterate2: %s\n", error_message(err));
285 return -1;
288 static int read_disk_extent(struct btrfs_root *root, u64 bytenr,
289 u32 num_bytes, char *buffer)
291 int ret;
292 struct btrfs_fs_devices *fs_devs = root->fs_info->fs_devices;
294 ret = pread(fs_devs->latest_bdev, buffer, num_bytes, bytenr);
295 if (ret != num_bytes)
296 goto fail;
297 ret = 0;
298 fail:
299 if (ret > 0)
300 ret = -1;
301 return ret;
304 * Record a file extent. Do all the required works, such as inserting
305 * file extent item, inserting extent item and backref item into extent
306 * tree and updating block accounting.
308 static int record_file_extent(struct btrfs_trans_handle *trans,
309 struct btrfs_root *root, u64 objectid,
310 struct btrfs_inode_item *inode,
311 u64 file_pos, u64 disk_bytenr,
312 u64 num_bytes, int checksum)
314 int ret;
315 struct btrfs_fs_info *info = root->fs_info;
316 struct btrfs_root *extent_root = info->extent_root;
317 struct btrfs_key ins_key;
318 struct btrfs_path path;
319 struct btrfs_extent_item extent_item;
320 u32 blocksize = root->sectorsize;
321 u64 nblocks;
322 u64 bytes_used;
324 ret = btrfs_insert_file_extent(trans, root, objectid, file_pos,
325 disk_bytenr, num_bytes, num_bytes);
326 if (ret || disk_bytenr == 0)
327 return ret;
329 nblocks = btrfs_stack_inode_nblocks(inode) + num_bytes / 512;
330 btrfs_set_stack_inode_nblocks(inode, nblocks);
331 if (checksum) {
332 u64 offset;
333 char *buffer;
335 ret = -ENOMEM;
336 buffer = malloc(blocksize);
337 if (!buffer)
338 goto fail;
339 for (offset = 0; offset < num_bytes; offset += blocksize) {
340 ret = read_disk_extent(root, disk_bytenr + offset,
341 blocksize, buffer);
342 if (ret)
343 break;
344 ret = btrfs_csum_file_block(trans, root, inode,
345 objectid, file_pos + offset,
346 buffer, blocksize);
347 if (ret)
348 break;
350 free(buffer);
351 if (ret)
352 goto fail;
355 bytes_used = btrfs_root_used(&root->root_item);
356 btrfs_set_root_used(&root->root_item, bytes_used + num_bytes);
357 ins_key.objectid = disk_bytenr;
358 ins_key.offset = num_bytes;
359 btrfs_set_key_type(&ins_key, BTRFS_EXTENT_ITEM_KEY);
360 btrfs_set_stack_extent_refs(&extent_item, 1);
361 ret = btrfs_insert_item(trans, extent_root, &ins_key,
362 &extent_item, sizeof(extent_item));
363 if (ret == 0) {
364 bytes_used = btrfs_super_bytes_used(&info->super_copy);
365 btrfs_set_super_bytes_used(&info->super_copy, bytes_used +
366 num_bytes);
367 btrfs_init_path(&path);
368 ret = btrfs_insert_extent_backref(trans, extent_root, &path,
369 disk_bytenr, root->root_key.objectid,
370 trans->transid, objectid, file_pos);
371 if (ret)
372 goto fail;
373 ret = btrfs_update_block_group(trans, root, disk_bytenr,
374 num_bytes, 1, 0);
375 } else if (ret == -EEXIST) {
376 ret = btrfs_inc_extent_ref(trans, root, disk_bytenr, num_bytes,
377 root->root_key.objectid,
378 trans->transid, objectid, file_pos);
380 if (ret)
381 goto fail;
382 btrfs_extent_post_op(trans, extent_root);
383 return 0;
384 fail:
385 return ret;
388 static int record_file_blocks(struct btrfs_trans_handle *trans,
389 struct btrfs_root *root, u64 objectid,
390 struct btrfs_inode_item *inode,
391 u64 file_block, u64 disk_block,
392 u64 num_blocks, int checksum)
394 u64 file_pos = file_block * root->sectorsize;
395 u64 disk_bytenr = disk_block * root->sectorsize;
396 u64 num_bytes = num_blocks * root->sectorsize;
397 return record_file_extent(trans, root, objectid, inode, file_pos,
398 disk_bytenr, num_bytes, checksum);
401 struct blk_iterate_data {
402 struct btrfs_trans_handle *trans;
403 struct btrfs_root *root;
404 struct btrfs_inode_item *inode;
405 u64 objectid;
406 u64 first_block;
407 u64 disk_block;
408 u64 num_blocks;
409 u64 boundary;
410 int checksum;
411 int errcode;
414 static int block_iterate_proc(ext2_filsys ext2_fs,
415 u64 disk_block, u64 file_block,
416 struct blk_iterate_data *idata)
418 int ret;
419 struct btrfs_root *root = idata->root;
420 struct btrfs_trans_handle *trans = idata->trans;
421 struct btrfs_block_group_cache *cache;
422 u64 bytenr;
424 BUG_ON(disk_block == 0);
425 if ((idata->num_blocks > 0 && disk_block >= idata->boundary) ||
426 (file_block > idata->first_block + idata->num_blocks) ||
427 (disk_block != idata->disk_block + idata->num_blocks)) {
428 if (idata->num_blocks > 0) {
429 ret = record_file_blocks(trans, root, idata->objectid,
430 idata->inode, idata->first_block,
431 idata->disk_block, idata->num_blocks,
432 idata->checksum);
433 if (ret)
434 goto fail;
435 idata->first_block += idata->num_blocks;
436 idata->num_blocks = 0;
438 if (file_block > idata->first_block) {
439 ret = record_file_blocks(trans, root, idata->objectid,
440 idata->inode, idata->first_block,
441 0, file_block - idata->first_block,
442 idata->checksum);
443 if (ret)
444 goto fail;
447 bytenr = disk_block * root->sectorsize;
448 cache = btrfs_lookup_block_group(root->fs_info, bytenr);
449 BUG_ON(!cache);
450 bytenr = cache->key.objectid + cache->key.offset;
452 idata->first_block = file_block;
453 idata->disk_block = disk_block;
454 idata->boundary = bytenr / root->sectorsize;
456 idata->num_blocks++;
457 return 0;
458 fail:
459 idata->errcode = ret;
460 return BLOCK_ABORT;
463 static int __block_iterate_proc(ext2_filsys fs, blk_t *blocknr,
464 e2_blkcnt_t blockcnt, blk_t ref_block,
465 int ref_offset, void *priv_data)
467 struct blk_iterate_data *idata;
468 idata = (struct blk_iterate_data *)priv_data;
469 return block_iterate_proc(fs, *blocknr, blockcnt, idata);
473 * traverse file's data blocks, record these data blocks as file extents.
475 static int create_file_extents(struct btrfs_trans_handle *trans,
476 struct btrfs_root *root, u64 objectid,
477 struct btrfs_inode_item *btrfs_inode,
478 ext2_filsys ext2_fs, ext2_ino_t ext2_ino,
479 int datacsum, int packing)
481 int ret;
482 char *buffer = NULL;
483 errcode_t err;
484 u32 last_block;
485 u32 sectorsize = root->sectorsize;
486 u64 inode_size = btrfs_stack_inode_size(btrfs_inode);
487 struct blk_iterate_data data = {
488 .trans = trans,
489 .root = root,
490 .inode = btrfs_inode,
491 .objectid = objectid,
492 .first_block = 0,
493 .disk_block = 0,
494 .num_blocks = 0,
495 .boundary = (u64)-1,
496 .checksum = datacsum,
497 .errcode = 0,
499 err = ext2fs_block_iterate2(ext2_fs, ext2_ino, BLOCK_FLAG_DATA_ONLY,
500 NULL, __block_iterate_proc, &data);
501 if (err)
502 goto error;
503 ret = data.errcode;
504 if (ret)
505 goto fail;
506 if (packing && data.first_block == 0 && data.num_blocks > 0 &&
507 inode_size <= BTRFS_MAX_INLINE_DATA_SIZE(root)) {
508 u64 num_bytes = data.num_blocks * sectorsize;
509 u64 disk_bytenr = data.disk_block * sectorsize;
511 buffer = malloc(num_bytes);
512 if (!buffer)
513 return -ENOMEM;
514 ret = read_disk_extent(root, disk_bytenr, num_bytes, buffer);
515 if (ret)
516 goto fail;
517 if (num_bytes > inode_size)
518 num_bytes = inode_size;
519 ret = btrfs_insert_inline_extent(trans, root, objectid,
520 0, buffer, num_bytes);
521 if (ret)
522 goto fail;
523 } else if (data.num_blocks > 0) {
524 ret = record_file_blocks(trans, root, objectid, btrfs_inode,
525 data.first_block, data.disk_block,
526 data.num_blocks, data.checksum);
527 if (ret)
528 goto fail;
530 data.first_block += data.num_blocks;
531 last_block = (inode_size + sectorsize - 1) / sectorsize;
532 if (last_block > data.first_block) {
533 ret = record_file_blocks(trans, root, objectid, btrfs_inode,
534 data.first_block, 0, last_block -
535 data.first_block, data.checksum);
537 fail:
538 if (buffer)
539 free(buffer);
540 return ret;
541 error:
542 fprintf(stderr, "ext2fs_block_iterate2: %s\n", error_message(err));
543 return -1;
546 static int create_symbol_link(struct btrfs_trans_handle *trans,
547 struct btrfs_root *root, u64 objectid,
548 struct btrfs_inode_item *btrfs_inode,
549 ext2_filsys ext2_fs, ext2_ino_t ext2_ino,
550 struct ext2_inode *ext2_inode)
552 int ret;
553 char *pathname;
554 u64 inode_size = btrfs_stack_inode_size(btrfs_inode);
555 if (ext2fs_inode_data_blocks(ext2_fs, ext2_inode)) {
556 btrfs_set_stack_inode_size(btrfs_inode, inode_size + 1);
557 ret = create_file_extents(trans, root, objectid, btrfs_inode,
558 ext2_fs, ext2_ino, 1, 1);
559 btrfs_set_stack_inode_size(btrfs_inode, inode_size);
560 return ret;
563 pathname = (char *)&(ext2_inode->i_block[0]);
564 BUG_ON(pathname[inode_size] != 0);
565 ret = btrfs_insert_inline_extent(trans, root, objectid, 0,
566 pathname, inode_size + 1);
567 return ret;
571 * Following xattr/acl related codes are based on codes in
572 * fs/ext3/xattr.c and fs/ext3/acl.c
574 #define EXT2_XATTR_BHDR(ptr) ((struct ext2_ext_attr_header *)(ptr))
575 #define EXT2_XATTR_BFIRST(ptr) \
576 ((struct ext2_ext_attr_entry *)(EXT2_XATTR_BHDR(ptr) + 1))
577 #define EXT2_XATTR_IHDR(inode) \
578 ((struct ext2_ext_attr_header *) ((void *)(inode) + \
579 EXT2_GOOD_OLD_INODE_SIZE + (inode)->i_extra_isize))
580 #define EXT2_XATTR_IFIRST(inode) \
581 ((struct ext2_ext_attr_entry *) ((void *)EXT2_XATTR_IHDR(inode) + \
582 sizeof(EXT2_XATTR_IHDR(inode)->h_magic)))
584 static int ext2_xattr_check_names(struct ext2_ext_attr_entry *entry,
585 const void *end)
587 struct ext2_ext_attr_entry *next;
589 while (!EXT2_EXT_IS_LAST_ENTRY(entry)) {
590 next = EXT2_EXT_ATTR_NEXT(entry);
591 if ((void *)next >= end)
592 return -EIO;
593 entry = next;
595 return 0;
598 static int ext2_xattr_check_block(const char *buf, size_t size)
600 int error;
601 struct ext2_ext_attr_header *header = EXT2_XATTR_BHDR(buf);
603 if (header->h_magic != EXT2_EXT_ATTR_MAGIC ||
604 header->h_blocks != 1)
605 return -EIO;
606 error = ext2_xattr_check_names(EXT2_XATTR_BFIRST(buf), buf + size);
607 return error;
610 static int ext2_xattr_check_entry(struct ext2_ext_attr_entry *entry,
611 size_t size)
613 size_t value_size = entry->e_value_size;
615 if (entry->e_value_block != 0 || value_size > size ||
616 entry->e_value_offs + value_size > size)
617 return -EIO;
618 return 0;
621 #define EXT2_ACL_VERSION 0x0001
623 typedef struct {
624 __le16 e_tag;
625 __le16 e_perm;
626 __le32 e_id;
627 } ext2_acl_entry;
629 typedef struct {
630 __le16 e_tag;
631 __le16 e_perm;
632 } ext2_acl_entry_short;
634 typedef struct {
635 __le32 a_version;
636 } ext2_acl_header;
638 static inline int ext2_acl_count(size_t size)
640 ssize_t s;
641 size -= sizeof(ext2_acl_header);
642 s = size - 4 * sizeof(ext2_acl_entry_short);
643 if (s < 0) {
644 if (size % sizeof(ext2_acl_entry_short))
645 return -1;
646 return size / sizeof(ext2_acl_entry_short);
647 } else {
648 if (s % sizeof(ext2_acl_entry))
649 return -1;
650 return s / sizeof(ext2_acl_entry) + 4;
654 #define ACL_EA_VERSION 0x0002
656 typedef struct {
657 __le16 e_tag;
658 __le16 e_perm;
659 __le32 e_id;
660 } acl_ea_entry;
662 typedef struct {
663 __le32 a_version;
664 acl_ea_entry a_entries[0];
665 } acl_ea_header;
667 static inline size_t acl_ea_size(int count)
669 return sizeof(acl_ea_header) + count * sizeof(acl_ea_entry);
672 static int ext2_acl_to_xattr(void *dst, const void *src,
673 size_t dst_size, size_t src_size)
675 int i, count;
676 const void *end = src + src_size;
677 acl_ea_header *ext_acl = (acl_ea_header *)dst;
678 acl_ea_entry *dst_entry = ext_acl->a_entries;
679 ext2_acl_entry *src_entry;
681 if (src_size < sizeof(ext2_acl_header))
682 goto fail;
683 if (((ext2_acl_header *)src)->a_version !=
684 cpu_to_le32(EXT2_ACL_VERSION))
685 goto fail;
686 src += sizeof(ext2_acl_header);
687 count = ext2_acl_count(src_size);
688 if (count <= 0)
689 goto fail;
691 BUG_ON(dst_size < acl_ea_size(count));
692 ext_acl->a_version = cpu_to_le32(ACL_EA_VERSION);
693 for (i = 0; i < count; i++, dst_entry++) {
694 src_entry = (ext2_acl_entry *)src;
695 if (src + sizeof(ext2_acl_entry_short) > end)
696 goto fail;
697 dst_entry->e_tag = src_entry->e_tag;
698 dst_entry->e_perm = src_entry->e_perm;
699 switch (le16_to_cpu(src_entry->e_tag)) {
700 case ACL_USER_OBJ:
701 case ACL_GROUP_OBJ:
702 case ACL_MASK:
703 case ACL_OTHER:
704 src += sizeof(ext2_acl_entry_short);
705 dst_entry->e_id = cpu_to_le32(ACL_UNDEFINED_ID);
706 break;
707 case ACL_USER:
708 case ACL_GROUP:
709 src += sizeof(ext2_acl_entry);
710 if (src > end)
711 goto fail;
712 dst_entry->e_id = src_entry->e_id;
713 break;
714 default:
715 goto fail;
718 if (src != end)
719 goto fail;
720 return 0;
721 fail:
722 return -EINVAL;
725 static char *xattr_prefix_table[] = {
726 [1] = "user.",
727 [2] = "system.posix_acl_access",
728 [3] = "system.posix_acl_default",
729 [4] = "trusted.",
730 [6] = "security.",
733 static int copy_single_xattr(struct btrfs_trans_handle *trans,
734 struct btrfs_root *root, u64 objectid,
735 struct ext2_ext_attr_entry *entry,
736 const void *data, u32 datalen)
738 int ret = 0;
739 int name_len;
740 int name_index;
741 void *databuf = NULL;
742 char namebuf[XATTR_NAME_MAX + 1];
744 name_index = entry->e_name_index;
745 if (name_index >= ARRAY_SIZE(xattr_prefix_table) ||
746 xattr_prefix_table[name_index] == NULL)
747 return -EOPNOTSUPP;
748 name_len = strlen(xattr_prefix_table[name_index]) +
749 entry->e_name_len;
750 if (name_len >= sizeof(namebuf))
751 return -ERANGE;
753 if (name_index == 2 || name_index == 3) {
754 size_t bufsize = acl_ea_size(ext2_acl_count(datalen));
755 databuf = malloc(bufsize);
756 if (!databuf)
757 return -ENOMEM;
758 ret = ext2_acl_to_xattr(databuf, data, bufsize, datalen);
759 if (ret)
760 goto out;
761 data = databuf;
762 datalen = bufsize;
764 strcpy(namebuf, xattr_prefix_table[name_index]);
765 strncat(namebuf, EXT2_EXT_ATTR_NAME(entry), entry->e_name_len);
766 if (name_len + datalen > BTRFS_LEAF_DATA_SIZE(root) -
767 sizeof(struct btrfs_item) - sizeof(struct btrfs_dir_item)) {
768 fprintf(stderr, "skip large xattr on inode %Lu name %.*s\n",
769 objectid - INO_OFFSET, name_len, namebuf);
770 goto out;
772 ret = btrfs_insert_xattr_item(trans, root, namebuf, name_len,
773 data, datalen, objectid);
774 out:
775 if (databuf)
776 free(databuf);
777 return ret;
780 static int copy_extended_attrs(struct btrfs_trans_handle *trans,
781 struct btrfs_root *root, u64 objectid,
782 struct btrfs_inode_item *btrfs_inode,
783 ext2_filsys ext2_fs, ext2_ino_t ext2_ino)
785 int ret = 0;
786 int inline_ea = 0;
787 errcode_t err;
788 u32 datalen;
789 u32 block_size = ext2_fs->blocksize;
790 u32 inode_size = EXT2_INODE_SIZE(ext2_fs->super);
791 struct ext2_inode_large *ext2_inode;
792 struct ext2_ext_attr_entry *entry;
793 void *data;
794 char *buffer = NULL;
795 char inode_buf[EXT2_GOOD_OLD_INODE_SIZE];
797 if (inode_size <= EXT2_GOOD_OLD_INODE_SIZE) {
798 ext2_inode = (struct ext2_inode_large *)inode_buf;
799 } else {
800 ext2_inode = (struct ext2_inode_large *)malloc(inode_size);
801 if (!ext2_inode)
802 return -ENOMEM;
804 err = ext2fs_read_inode_full(ext2_fs, ext2_ino, (void *)ext2_inode,
805 inode_size);
806 if (err) {
807 fprintf(stderr, "ext2fs_read_inode_full: %s\n",
808 error_message(err));
809 ret = -1;
810 goto out;
813 if (ext2_ino > ext2_fs->super->s_first_ino &&
814 inode_size > EXT2_GOOD_OLD_INODE_SIZE) {
815 if (EXT2_GOOD_OLD_INODE_SIZE +
816 ext2_inode->i_extra_isize > inode_size) {
817 ret = -EIO;
818 goto out;
820 if (ext2_inode->i_extra_isize != 0 &&
821 EXT2_XATTR_IHDR(ext2_inode)->h_magic ==
822 EXT2_EXT_ATTR_MAGIC) {
823 inline_ea = 1;
826 if (inline_ea) {
827 int total;
828 void *end = (void *)ext2_inode + inode_size;
829 entry = EXT2_XATTR_IFIRST(ext2_inode);
830 total = end - (void *)entry;
831 ret = ext2_xattr_check_names(entry, end);
832 if (ret)
833 goto out;
834 while (!EXT2_EXT_IS_LAST_ENTRY(entry)) {
835 ret = ext2_xattr_check_entry(entry, total);
836 if (ret)
837 goto out;
838 data = (void *)EXT2_XATTR_IFIRST(ext2_inode) +
839 entry->e_value_offs;
840 datalen = entry->e_value_size;
841 ret = copy_single_xattr(trans, root, objectid,
842 entry, data, datalen);
843 if (ret)
844 goto out;
845 entry = EXT2_EXT_ATTR_NEXT(entry);
849 if (ext2_inode->i_file_acl == 0)
850 goto out;
852 buffer = malloc(block_size);
853 if (!buffer) {
854 ret = -ENOMEM;
855 goto out;
857 err = ext2fs_read_ext_attr(ext2_fs, ext2_inode->i_file_acl, buffer);
858 if (err) {
859 fprintf(stderr, "ext2fs_read_ext_attr: %s\n",
860 error_message(err));
861 ret = -1;
862 goto out;
864 ret = ext2_xattr_check_block(buffer, block_size);
865 if (ret)
866 goto out;
868 entry = EXT2_XATTR_BFIRST(buffer);
869 while (!EXT2_EXT_IS_LAST_ENTRY(entry)) {
870 ret = ext2_xattr_check_entry(entry, block_size);
871 if (ret)
872 goto out;
873 data = buffer + entry->e_value_offs;
874 datalen = entry->e_value_size;
875 ret = copy_single_xattr(trans, root, objectid,
876 entry, data, datalen);
877 if (ret)
878 goto out;
879 entry = EXT2_EXT_ATTR_NEXT(entry);
881 out:
882 if (buffer != NULL)
883 free(buffer);
884 if ((void *)ext2_inode != inode_buf)
885 free(ext2_inode);
886 return ret;
888 #define MINORBITS 20
889 #define MKDEV(ma, mi) (((ma) << MINORBITS) | (mi))
891 static inline dev_t old_decode_dev(u16 val)
893 return MKDEV((val >> 8) & 255, val & 255);
896 static inline dev_t new_decode_dev(u32 dev)
898 unsigned major = (dev & 0xfff00) >> 8;
899 unsigned minor = (dev & 0xff) | ((dev >> 12) & 0xfff00);
900 return MKDEV(major, minor);
903 static int copy_inode_item(struct btrfs_inode_item *dst,
904 struct ext2_inode *src)
906 btrfs_set_stack_inode_generation(dst, 1);
907 btrfs_set_stack_inode_size(dst, src->i_size);
908 btrfs_set_stack_inode_nblocks(dst, src->i_blocks);
909 btrfs_set_stack_inode_block_group(dst, 0);
910 btrfs_set_stack_inode_nblocks(dst, 0);
911 btrfs_set_stack_inode_nlink(dst, src->i_links_count);
912 btrfs_set_stack_inode_uid(dst, src->i_uid | (src->i_uid_high << 16));
913 btrfs_set_stack_inode_gid(dst, src->i_gid | (src->i_gid_high << 16));
914 btrfs_set_stack_inode_mode(dst, src->i_mode);
915 btrfs_set_stack_inode_rdev(dst, 0);
916 btrfs_set_stack_inode_flags(dst, 0);
917 btrfs_set_stack_inode_compat_flags(dst, 0);
918 btrfs_set_stack_timespec_sec(&dst->atime, src->i_atime);
919 btrfs_set_stack_timespec_nsec(&dst->atime, 0);
920 btrfs_set_stack_timespec_sec(&dst->ctime, src->i_ctime);
921 btrfs_set_stack_timespec_nsec(&dst->ctime, 0);
922 btrfs_set_stack_timespec_sec(&dst->mtime, src->i_mtime);
923 btrfs_set_stack_timespec_nsec(&dst->mtime, 0);
924 btrfs_set_stack_timespec_sec(&dst->otime, 0);
925 btrfs_set_stack_timespec_nsec(&dst->otime, 0);
927 if (S_ISDIR(src->i_mode)) {
928 btrfs_set_stack_inode_size(dst, 0);
929 btrfs_set_stack_inode_nlink(dst, 1);
931 if (S_ISREG(src->i_mode)) {
932 btrfs_set_stack_inode_size(dst, (u64)src->i_size_high << 32 |
933 (u64)src->i_size);
935 if (!S_ISREG(src->i_mode) && !S_ISDIR(src->i_mode) &&
936 !S_ISLNK(src->i_mode)) {
937 if (src->i_block[0]) {
938 btrfs_set_stack_inode_rdev(dst,
939 old_decode_dev(src->i_block[0]));
940 } else {
941 btrfs_set_stack_inode_rdev(dst,
942 new_decode_dev(src->i_block[1]));
945 return 0;
949 * copy a single inode. do all the required works, such as cloning
950 * inode item, creating file extents and creating directory entries.
952 static int copy_single_inode(struct btrfs_trans_handle *trans,
953 struct btrfs_root *root, u64 objectid,
954 ext2_filsys ext2_fs, ext2_ino_t ext2_ino,
955 int datacsum, int packing, int noxattr)
957 int ret;
958 errcode_t err;
959 struct ext2_inode ext2_inode;
960 struct btrfs_key inode_key;
961 struct btrfs_inode_item btrfs_inode;
963 err = ext2fs_read_inode(ext2_fs, ext2_ino, &ext2_inode);
964 if (err)
965 goto error;
967 if (!ext2_inode.i_links_count &&
968 (!ext2_inode.i_mode || ext2_inode.i_dtime)) {
969 printf("skip inode %u\n", ext2_ino);
970 return 0;
972 copy_inode_item(&btrfs_inode, &ext2_inode);
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);
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;
996 if (ret)
997 return ret;
999 if (!noxattr) {
1000 ret = copy_extended_attrs(trans, root, objectid, &btrfs_inode,
1001 ext2_fs, ext2_ino);
1002 if (ret)
1003 return ret;
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 error:
1011 fprintf(stderr, "ext2fs_read_inode: %s\n", error_message(err));
1012 return -1;
1015 static int copy_disk_extent(struct btrfs_root *root, u64 dst_bytenr,
1016 u64 src_bytenr, u32 num_bytes)
1018 int ret;
1019 char *buffer;
1020 struct btrfs_fs_devices *fs_devs = root->fs_info->fs_devices;
1022 buffer = malloc(num_bytes);
1023 if (!buffer)
1024 return -ENOMEM;
1025 ret = pread(fs_devs->latest_bdev, buffer, num_bytes, src_bytenr);
1026 if (ret != num_bytes)
1027 goto fail;
1028 ret = pwrite(fs_devs->latest_bdev, buffer, num_bytes, dst_bytenr);
1029 if (ret != num_bytes)
1030 goto fail;
1031 ret = 0;
1032 fail:
1033 free(buffer);
1034 if (ret > 0)
1035 ret = -1;
1036 return ret;
1039 * scan ext2's inode bitmap and copy all used inode.
1041 static int copy_inodes(struct btrfs_root *root, ext2_filsys ext2_fs,
1042 int datacsum, int packing, int noxattr)
1044 int ret;
1045 ext2_ino_t ext2_ino;
1046 u64 objectid;
1047 struct btrfs_trans_handle *trans;
1049 trans = btrfs_start_transaction(root, 1);
1050 if (!trans)
1051 return -ENOMEM;
1052 ext2_ino = ext2_fs->inode_map->start;
1053 for (; ext2_ino <= ext2_fs->inode_map->end; ext2_ino++) {
1054 if (ext2fs_fast_test_inode_bitmap(ext2_fs->inode_map,
1055 ext2_ino)) {
1056 /* skip special inode in ext2fs */
1057 if (ext2_ino < EXT2_GOOD_OLD_FIRST_INO &&
1058 ext2_ino != EXT2_ROOT_INO)
1059 continue;
1060 objectid = ext2_ino + INO_OFFSET;
1061 ret = copy_single_inode(trans, root,
1062 objectid, ext2_fs, ext2_ino,
1063 datacsum, packing, noxattr);
1064 if (ret)
1065 return ret;
1067 if (trans->blocks_used >= 4096) {
1068 ret = btrfs_commit_transaction(trans, root);
1069 BUG_ON(ret);
1070 trans = btrfs_start_transaction(root, 1);
1071 BUG_ON(!trans);
1074 ret = btrfs_commit_transaction(trans, root);
1075 BUG_ON(ret);
1077 return ret;
1081 * Construct a range of ext2fs image file.
1082 * scan block allocation bitmap, find all blocks used by the ext2fs
1083 * in this range and create file extents that point to these blocks.
1085 * Note: Before calling the function, no file extent points to blocks
1086 * in this range
1088 static int create_image_file_range(struct btrfs_trans_handle *trans,
1089 struct btrfs_root *root, u64 objectid,
1090 struct btrfs_inode_item *inode,
1091 u64 start_byte, u64 end_byte,
1092 ext2_filsys ext2_fs)
1094 u32 blocksize = ext2_fs->blocksize;
1095 u32 block = start_byte / blocksize;
1096 u32 last_block = (end_byte + blocksize - 1) / blocksize;
1097 int ret;
1098 struct blk_iterate_data data = {
1099 .trans = trans,
1100 .root = root,
1101 .inode = inode,
1102 .objectid = objectid,
1103 .first_block = block,
1104 .disk_block = 0,
1105 .num_blocks = 0,
1106 .boundary = (u64)-1,
1107 .checksum = 0,
1108 .errcode = 0,
1110 for (; start_byte < end_byte; block++, start_byte += blocksize) {
1111 if (!ext2fs_fast_test_block_bitmap(ext2_fs->block_map, block))
1112 continue;
1113 ret = block_iterate_proc(NULL, block, block, &data);
1114 if (ret & BLOCK_ABORT) {
1115 ret = data.errcode;
1116 goto fail;
1119 if (data.num_blocks > 0) {
1120 ret = record_file_blocks(trans, root, objectid, inode,
1121 data.first_block, data.disk_block,
1122 data.num_blocks, 0);
1123 if (ret)
1124 goto fail;
1125 data.first_block += data.num_blocks;
1127 if (last_block > data.first_block) {
1128 ret = record_file_blocks(trans, root, objectid, inode,
1129 data.first_block, 0, last_block -
1130 data.first_block, 0);
1131 if (ret)
1132 goto fail;
1134 fail:
1135 return ret;
1138 * Create the ext2fs image file.
1140 static int create_ext2_image(struct btrfs_root *root, ext2_filsys ext2_fs,
1141 const char *name)
1143 int ret;
1144 struct btrfs_key key;
1145 struct btrfs_key location;
1146 struct btrfs_path path;
1147 struct btrfs_inode_item btrfs_inode;
1148 struct btrfs_inode_item *inode_item;
1149 struct extent_buffer *leaf;
1150 struct btrfs_fs_info *fs_info = root->fs_info;
1151 struct btrfs_root *extent_root = fs_info->extent_root;
1152 struct btrfs_trans_handle *trans;
1153 struct btrfs_extent_ref *ref_item;
1154 u64 bytenr;
1155 u64 num_bytes;
1156 u64 ref_root;
1157 u64 ref_owner;
1158 u64 objectid;
1159 u64 last_byte;
1160 u64 first_free;
1161 u64 total_bytes;
1162 u32 sectorsize = root->sectorsize;
1163 int file_extent;
1165 total_bytes = btrfs_super_total_bytes(&fs_info->super_copy);
1166 first_free = BTRFS_SUPER_INFO_OFFSET + sectorsize * 2 - 1;
1167 first_free &= ~((u64)sectorsize - 1);
1169 memset(&btrfs_inode, 0, sizeof(btrfs_inode));
1170 btrfs_set_stack_inode_generation(&btrfs_inode, 1);
1171 btrfs_set_stack_inode_size(&btrfs_inode, total_bytes);
1172 btrfs_set_stack_inode_nlink(&btrfs_inode, 1);
1173 btrfs_set_stack_inode_nblocks(&btrfs_inode, 0);
1174 btrfs_set_stack_inode_mode(&btrfs_inode, S_IFREG | 0400);
1175 btrfs_set_stack_inode_flags(&btrfs_inode, BTRFS_INODE_NODATASUM |
1176 BTRFS_INODE_READONLY);
1177 btrfs_init_path(&path);
1178 trans = btrfs_start_transaction(root, 1);
1179 BUG_ON(!trans);
1181 objectid = btrfs_root_dirid(&root->root_item);
1182 ret = btrfs_find_free_objectid(trans, root, objectid, &objectid);
1183 if (ret)
1184 goto fail;
1187 * copy blocks covered by extent #0 to new positions. extent #0 is
1188 * special, we can't rely on relocate_extents_range to relocate it.
1190 for (last_byte = 0; last_byte < first_free; last_byte += sectorsize) {
1191 ret = custom_alloc_extent(root, sectorsize, 0, &key);
1192 if (ret)
1193 goto fail;
1194 ret = copy_disk_extent(root, key.objectid, last_byte,
1195 sectorsize);
1196 if (ret)
1197 goto fail;
1198 ret = record_file_extent(trans, root, objectid,
1199 &btrfs_inode, last_byte,
1200 key.objectid, sectorsize, 0);
1201 if (ret)
1202 goto fail;
1205 while(1) {
1206 key.objectid = last_byte;
1207 key.offset = 0;
1208 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
1209 ret = btrfs_search_slot(trans, fs_info->extent_root,
1210 &key, &path, 0, 0);
1211 if (ret < 0)
1212 goto fail;
1213 next:
1214 leaf = path.nodes[0];
1215 if (path.slots[0] >= btrfs_header_nritems(leaf)) {
1216 ret = btrfs_next_leaf(extent_root, &path);
1217 if (ret < 0)
1218 goto fail;
1219 if (ret > 0)
1220 break;
1221 leaf = path.nodes[0];
1223 btrfs_item_key_to_cpu(leaf, &key, path.slots[0]);
1224 if (last_byte > key.objectid ||
1225 key.type != BTRFS_EXTENT_ITEM_KEY) {
1226 path.slots[0]++;
1227 goto next;
1230 * Check backref to distinguish extent items for normal
1231 * files (files that correspond to files in Ext2fs) from
1232 * extent items for ctree blocks.
1234 bytenr = key.objectid;
1235 num_bytes = key.offset;
1236 file_extent = 0;
1237 while (1) {
1238 if (path.slots[0] >= btrfs_header_nritems(leaf)) {
1239 ret = btrfs_next_leaf(extent_root, &path);
1240 if (ret > 0)
1241 break;
1242 if (ret < 0)
1243 goto fail;
1244 leaf = path.nodes[0];
1246 btrfs_item_key_to_cpu(leaf, &key, path.slots[0]);
1247 if (key.objectid != bytenr)
1248 break;
1249 if (key.type != BTRFS_EXTENT_REF_KEY) {
1250 path.slots[0]++;
1251 continue;
1253 ref_item = btrfs_item_ptr(leaf, path.slots[0],
1254 struct btrfs_extent_ref);
1255 ref_root = btrfs_ref_root(leaf, ref_item);
1256 ref_owner = btrfs_ref_objectid(leaf, ref_item);
1257 if ((ref_root == BTRFS_FS_TREE_OBJECTID) &&
1258 (ref_owner >= BTRFS_FIRST_FREE_OBJECTID)) {
1259 file_extent = 1;
1260 break;
1262 path.slots[0]++;
1264 if (!file_extent)
1265 goto next;
1267 if (bytenr > last_byte) {
1268 ret = create_image_file_range(trans, root, objectid,
1269 &btrfs_inode, last_byte,
1270 bytenr, ext2_fs);
1271 if (ret)
1272 goto fail;
1274 ret = record_file_extent(trans, root, objectid, &btrfs_inode,
1275 bytenr, bytenr, num_bytes, 0);
1276 if (ret)
1277 goto fail;
1278 last_byte = bytenr + num_bytes;
1279 btrfs_release_path(extent_root, &path);
1281 if (trans->blocks_used >= 4096) {
1282 ret = btrfs_commit_transaction(trans, root);
1283 BUG_ON(ret);
1284 trans = btrfs_start_transaction(root, 1);
1285 BUG_ON(!trans);
1288 btrfs_release_path(root, &path);
1289 if (total_bytes > last_byte) {
1290 ret = create_image_file_range(trans, root, objectid,
1291 &btrfs_inode, last_byte,
1292 total_bytes, ext2_fs);
1293 if (ret)
1294 goto fail;
1297 ret = btrfs_insert_inode(trans, root, objectid, &btrfs_inode);
1298 if (ret)
1299 goto fail;
1301 location.objectid = objectid;
1302 location.offset = 0;
1303 btrfs_set_key_type(&location, BTRFS_INODE_ITEM_KEY);
1304 ret = btrfs_insert_dir_item(trans, root, name, strlen(name),
1305 btrfs_root_dirid(&root->root_item),
1306 &location, EXT2_FT_REG_FILE);
1307 if (ret)
1308 goto fail;
1309 ret = btrfs_insert_inode_ref(trans, root, name, strlen(name),
1310 objectid,
1311 btrfs_root_dirid(&root->root_item));
1312 if (ret)
1313 goto fail;
1314 location.objectid = btrfs_root_dirid(&root->root_item);
1315 location.offset = 0;
1316 btrfs_set_key_type(&location, BTRFS_INODE_ITEM_KEY);
1317 ret = btrfs_lookup_inode(trans, root, &path, &location, 1);
1318 if (ret)
1319 goto fail;
1320 leaf = path.nodes[0];
1321 inode_item = btrfs_item_ptr(leaf, path.slots[0],
1322 struct btrfs_inode_item);
1323 btrfs_set_inode_size(leaf, inode_item, strlen(name) * 2 +
1324 btrfs_inode_size(leaf, inode_item));
1325 btrfs_mark_buffer_dirty(leaf);
1326 btrfs_release_path(root, &path);
1327 ret = btrfs_commit_transaction(trans, root);
1328 BUG_ON(ret);
1329 fail:
1330 btrfs_release_path(root, &path);
1331 return ret;
1334 struct btrfs_root *create_subvol(struct btrfs_root *root, const char *name)
1336 int ret;
1337 u64 objectid;
1338 struct btrfs_key location;
1339 struct btrfs_root_item root_item;
1340 struct btrfs_trans_handle *trans;
1341 struct btrfs_fs_info *fs_info = root->fs_info;
1342 struct btrfs_root *tree_root = fs_info->tree_root;
1343 struct btrfs_root *new_root;
1344 struct extent_buffer *tmp;
1346 trans = btrfs_start_transaction(root, 1);
1347 BUG_ON(!trans);
1349 objectid = btrfs_super_root_dir(&fs_info->super_copy);
1350 ret = btrfs_find_free_objectid(trans, root, objectid, &objectid);
1351 if (ret)
1352 goto fail;
1353 ret = btrfs_copy_root(trans, root, root->node, &tmp, objectid);
1354 if (ret)
1355 goto fail;
1356 memcpy(&root_item, &root->root_item, sizeof(root_item));
1357 btrfs_set_root_bytenr(&root_item, tmp->start);
1358 btrfs_set_root_level(&root_item, btrfs_header_level(tmp));
1359 free_extent_buffer(tmp);
1361 location.objectid = objectid;
1362 location.offset = 1;
1363 btrfs_set_key_type(&location, BTRFS_ROOT_ITEM_KEY);
1364 ret = btrfs_insert_root(trans, root->fs_info->tree_root,
1365 &location, &root_item);
1366 if (ret)
1367 goto fail;
1368 location.offset = (u64)-1;
1369 ret = btrfs_insert_dir_item(trans, tree_root, name, strlen(name),
1370 btrfs_super_root_dir(&fs_info->super_copy),
1371 &location, BTRFS_FT_DIR);
1372 if (ret)
1373 goto fail;
1374 ret = btrfs_insert_inode_ref(trans, tree_root, name, strlen(name),
1375 objectid,
1376 btrfs_super_root_dir(&fs_info->super_copy));
1377 if (ret)
1378 goto fail;
1379 ret = btrfs_commit_transaction(trans, root);
1380 BUG_ON(ret);
1381 new_root = btrfs_read_fs_root(fs_info, &location);
1382 if (!new_root || IS_ERR(new_root))
1383 goto fail;
1384 trans = btrfs_start_transaction(new_root, 1);
1385 BUG_ON(!trans);
1386 ret = btrfs_make_root_dir(trans, new_root, BTRFS_FIRST_FREE_OBJECTID);
1387 if (ret)
1388 goto fail;
1389 ret = btrfs_commit_transaction(trans, new_root);
1390 BUG_ON(ret);
1391 return new_root;
1392 fail:
1393 return NULL;
1396 * Fixup block accounting. The initial block accounting created by
1397 * make_block_groups isn't accuracy in this case.
1399 static int fixup_block_accounting(struct btrfs_trans_handle *trans,
1400 struct btrfs_root *root)
1402 int ret;
1403 int slot;
1404 u64 start = 0;
1405 u64 bytes_used = 0;
1406 struct btrfs_path path;
1407 struct btrfs_key key;
1408 struct extent_buffer *leaf;
1409 struct btrfs_block_group_cache *cache;
1410 struct btrfs_fs_info *fs_info = root->fs_info;
1412 while(1) {
1413 cache = btrfs_lookup_block_group(fs_info, start);
1414 if (!cache)
1415 break;
1416 start = cache->key.objectid + cache->key.offset;
1417 btrfs_set_block_group_used(&cache->item, 0);
1418 cache->space_info->bytes_used = 0;
1421 btrfs_init_path(&path);
1422 key.offset = 0;
1423 key.objectid = 0;
1424 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
1425 ret = btrfs_search_slot(trans, root->fs_info->extent_root,
1426 &key, &path, 0, 0);
1427 if (ret < 0)
1428 return ret;
1429 while(1) {
1430 leaf = path.nodes[0];
1431 slot = path.slots[0];
1432 if (slot >= btrfs_header_nritems(leaf)) {
1433 ret = btrfs_next_leaf(root, &path);
1434 if (ret < 0)
1435 return ret;
1436 if (ret > 0)
1437 break;
1438 leaf = path.nodes[0];
1439 slot = path.slots[0];
1441 btrfs_item_key_to_cpu(leaf, &key, slot);
1442 if (key.type == BTRFS_EXTENT_ITEM_KEY) {
1443 bytes_used += key.offset;
1444 ret = btrfs_update_block_group(trans, root,
1445 key.objectid, key.offset, 1, 0);
1446 BUG_ON(ret);
1448 path.slots[0]++;
1450 btrfs_set_super_bytes_used(&root->fs_info->super_copy, bytes_used);
1451 btrfs_release_path(root, &path);
1452 return 0;
1455 static int create_chunk_mapping(struct btrfs_trans_handle *trans,
1456 struct btrfs_root *root)
1458 struct btrfs_fs_info *info = root->fs_info;
1459 struct btrfs_root *chunk_root = info->chunk_root;
1460 struct btrfs_root *extent_root = info->extent_root;
1461 struct btrfs_device *device;
1462 struct btrfs_block_group_cache *cache;
1463 struct btrfs_dev_extent *extent;
1464 struct extent_buffer *leaf;
1465 struct btrfs_chunk chunk;
1466 struct btrfs_key key;
1467 struct btrfs_path path;
1468 u64 cur_start;
1469 u64 total_bytes;
1470 u64 chunk_objectid;
1471 int ret;
1473 btrfs_init_path(&path);
1475 total_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy);
1476 chunk_objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
1478 BUG_ON(list_empty(&info->fs_devices->devices));
1479 device = list_entry(info->fs_devices->devices.next,
1480 struct btrfs_device, dev_list);
1481 BUG_ON(device->devid != info->fs_devices->latest_devid);
1483 /* delete device extent created by make_btrfs */
1484 key.objectid = device->devid;
1485 key.offset = 0;
1486 key.type = BTRFS_DEV_EXTENT_KEY;
1487 ret = btrfs_search_slot(trans, device->dev_root, &key, &path, -1, 1);
1488 if (ret < 0)
1489 goto err;
1491 BUG_ON(ret > 0);
1492 ret = btrfs_del_item(trans, device->dev_root, &path);
1493 if (ret)
1494 goto err;
1495 btrfs_release_path(device->dev_root, &path);
1497 /* delete chunk item created by make_btrfs */
1498 key.objectid = chunk_objectid;
1499 key.offset = 0;
1500 key.type = BTRFS_CHUNK_ITEM_KEY;
1501 ret = btrfs_search_slot(trans, chunk_root, &key, &path, -1, 1);
1502 if (ret < 0)
1503 goto err;
1505 BUG_ON(ret > 0);
1506 ret = btrfs_del_item(trans, chunk_root, &path);
1507 if (ret)
1508 goto err;
1509 btrfs_release_path(chunk_root, &path);
1511 /* for each block group, create device extent and chunk item */
1512 cur_start = 0;
1513 while (cur_start < total_bytes) {
1514 cache = btrfs_lookup_block_group(root->fs_info, cur_start);
1515 BUG_ON(!cache);
1517 /* insert device extent */
1518 key.objectid = device->devid;
1519 key.offset = cache->key.objectid;
1520 key.type = BTRFS_DEV_EXTENT_KEY;
1521 ret = btrfs_insert_empty_item(trans, device->dev_root, &path,
1522 &key, sizeof(*extent));
1523 if (ret)
1524 goto err;
1526 leaf = path.nodes[0];
1527 extent = btrfs_item_ptr(leaf, path.slots[0],
1528 struct btrfs_dev_extent);
1530 btrfs_set_dev_extent_chunk_tree(leaf, extent,
1531 chunk_root->root_key.objectid);
1532 btrfs_set_dev_extent_chunk_objectid(leaf, extent,
1533 chunk_objectid);
1534 btrfs_set_dev_extent_chunk_offset(leaf, extent,
1535 cache->key.objectid);
1536 btrfs_set_dev_extent_length(leaf, extent, cache->key.offset);
1537 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
1538 (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent),
1539 BTRFS_UUID_SIZE);
1540 btrfs_mark_buffer_dirty(leaf);
1541 btrfs_release_path(device->dev_root, &path);
1543 /* insert chunk item */
1544 btrfs_set_stack_chunk_length(&chunk, cache->key.offset);
1545 btrfs_set_stack_chunk_owner(&chunk,
1546 extent_root->root_key.objectid);
1547 btrfs_set_stack_chunk_stripe_len(&chunk, 64 * 1024);
1548 btrfs_set_stack_chunk_type(&chunk, cache->flags);
1549 btrfs_set_stack_chunk_io_align(&chunk, device->io_align);
1550 btrfs_set_stack_chunk_io_width(&chunk, device->io_width);
1551 btrfs_set_stack_chunk_sector_size(&chunk, device->sector_size);
1552 btrfs_set_stack_chunk_num_stripes(&chunk, 1);
1553 btrfs_set_stack_chunk_sub_stripes(&chunk, 0);
1554 btrfs_set_stack_stripe_devid(&chunk.stripe, device->devid);
1555 btrfs_set_stack_stripe_offset(&chunk.stripe,
1556 cache->key.objectid);
1557 memcpy(&chunk.stripe.dev_uuid, device->uuid, BTRFS_UUID_SIZE);
1559 key.objectid = chunk_objectid;
1560 key.offset = cache->key.objectid;
1561 key.type = BTRFS_CHUNK_ITEM_KEY;
1563 ret = btrfs_insert_item(trans, chunk_root, &key, &chunk,
1564 btrfs_chunk_item_size(1));
1565 if (ret)
1566 goto err;
1568 cur_start = cache->key.objectid + cache->key.offset;
1571 device->bytes_used = total_bytes;
1572 ret = btrfs_update_device(trans, device);
1573 err:
1574 btrfs_release_path(device->dev_root, &path);
1575 return ret;
1578 static int init_btrfs(struct btrfs_root *root)
1580 int ret;
1581 struct btrfs_key location;
1582 struct btrfs_trans_handle *trans;
1583 struct btrfs_fs_info *fs_info = root->fs_info;
1585 trans = btrfs_start_transaction(root, 1);
1586 BUG_ON(!trans);
1587 ret = btrfs_make_block_groups(trans, root);
1588 if (ret)
1589 goto err;
1590 ret = fixup_block_accounting(trans, root);
1591 if (ret)
1592 goto err;
1593 ret = create_chunk_mapping(trans, root);
1594 if (ret)
1595 goto err;
1596 ret = btrfs_make_root_dir(trans, fs_info->tree_root,
1597 BTRFS_ROOT_TREE_DIR_OBJECTID);
1598 if (ret)
1599 goto err;
1600 memcpy(&location, &root->root_key, sizeof(location));
1601 location.offset = (u64)-1;
1602 ret = btrfs_insert_dir_item(trans, fs_info->tree_root, "default", 7,
1603 btrfs_super_root_dir(&fs_info->super_copy),
1604 &location, BTRFS_FT_DIR);
1605 if (ret)
1606 goto err;
1607 ret = btrfs_insert_inode_ref(trans, fs_info->tree_root, "default", 7,
1608 location.objectid,
1609 btrfs_super_root_dir(&fs_info->super_copy));
1610 if (ret)
1611 goto err;
1612 btrfs_set_root_dirid(&fs_info->fs_root->root_item,
1613 BTRFS_FIRST_FREE_OBJECTID);
1614 ret = btrfs_commit_transaction(trans, root);
1615 BUG_ON(ret);
1616 err:
1617 return ret;
1621 * Migrate super block to it's default position and zero 0 ~ 16k
1623 static int migrate_super_block(int fd, u64 old_bytenr, u32 sectorsize)
1625 int ret;
1626 struct extent_buffer *buf;
1627 struct btrfs_super_block *super;
1628 u32 len;
1629 u32 bytenr;
1631 BUG_ON(sectorsize < sizeof(*super));
1632 buf = malloc(sizeof(*buf) + sectorsize);
1633 if (!buf)
1634 return -ENOMEM;
1636 buf->len = sectorsize;
1637 ret = pread(fd, buf->data, sectorsize, old_bytenr);
1638 if (ret != sectorsize)
1639 goto fail;
1641 super = (struct btrfs_super_block *)buf->data;
1642 BUG_ON(btrfs_super_bytenr(super) != old_bytenr);
1643 btrfs_set_super_bytenr(super, BTRFS_SUPER_INFO_OFFSET);
1645 csum_tree_block(NULL, buf, 0);
1646 ret = pwrite(fd, buf->data, sectorsize, BTRFS_SUPER_INFO_OFFSET);
1647 if (ret != sectorsize)
1648 goto fail;
1650 ret = fsync(fd);
1651 if (ret)
1652 goto fail;
1654 memset(buf->data, 0, sectorsize);
1655 for (bytenr = 0; bytenr < BTRFS_SUPER_INFO_OFFSET; ) {
1656 len = BTRFS_SUPER_INFO_OFFSET - bytenr;
1657 if (len > sectorsize)
1658 len = sectorsize;
1659 ret = pwrite(fd, buf->data, len, bytenr);
1660 if (ret != len) {
1661 fprintf(stderr, "unable to zero fill device\n");
1662 break;
1664 bytenr += len;
1666 ret = 0;
1667 fsync(fd);
1668 fail:
1669 free(buf);
1670 if (ret > 0)
1671 ret = -1;
1672 return ret;
1675 static int prepare_system_chunk_sb(struct btrfs_super_block *super)
1677 struct btrfs_chunk *chunk;
1678 struct btrfs_disk_key *key;
1679 u32 sectorsize = btrfs_super_sectorsize(super);
1681 key = (struct btrfs_disk_key *)(super->sys_chunk_array);
1682 chunk = (struct btrfs_chunk *)(super->sys_chunk_array +
1683 sizeof(struct btrfs_disk_key));
1685 btrfs_set_disk_key_objectid(key, BTRFS_FIRST_CHUNK_TREE_OBJECTID);
1686 btrfs_set_disk_key_type(key, BTRFS_CHUNK_ITEM_KEY);
1687 btrfs_set_disk_key_offset(key, 0);
1689 btrfs_set_stack_chunk_length(chunk, btrfs_super_total_bytes(super));
1690 btrfs_set_stack_chunk_owner(chunk, BTRFS_EXTENT_TREE_OBJECTID);
1691 btrfs_set_stack_chunk_stripe_len(chunk, 64 * 1024);
1692 btrfs_set_stack_chunk_type(chunk, BTRFS_BLOCK_GROUP_SYSTEM);
1693 btrfs_set_stack_chunk_io_align(chunk, sectorsize);
1694 btrfs_set_stack_chunk_io_width(chunk, sectorsize);
1695 btrfs_set_stack_chunk_sector_size(chunk, sectorsize);
1696 btrfs_set_stack_chunk_num_stripes(chunk, 1);
1697 btrfs_set_stack_chunk_sub_stripes(chunk, 0);
1698 chunk->stripe.devid = super->dev_item.devid;
1699 chunk->stripe.offset = cpu_to_le64(0);
1700 memcpy(chunk->stripe.dev_uuid, super->dev_item.uuid, BTRFS_UUID_SIZE);
1701 btrfs_set_super_sys_array_size(super, sizeof(*key) + sizeof(*chunk));
1702 return 0;
1705 static int prepare_system_chunk(int fd, u64 sb_bytenr, u32 sectorsize)
1707 int ret;
1708 struct extent_buffer *buf;
1709 struct btrfs_super_block *super;
1711 BUG_ON(sectorsize < sizeof(*super));
1712 buf = malloc(sizeof(*buf) + sectorsize);
1713 if (!buf)
1714 return -ENOMEM;
1716 buf->len = sectorsize;
1717 ret = pread(fd, buf->data, sectorsize, sb_bytenr);
1718 if (ret != sectorsize)
1719 goto fail;
1721 super = (struct btrfs_super_block *)buf->data;
1722 BUG_ON(btrfs_super_bytenr(super) != sb_bytenr);
1723 BUG_ON(btrfs_super_num_devices(super) != 1);
1725 ret = prepare_system_chunk_sb(super);
1726 if (ret)
1727 goto fail;
1729 csum_tree_block(NULL, buf, 0);
1730 ret = pwrite(fd, buf->data, sectorsize, sb_bytenr);
1731 if (ret != sectorsize)
1732 goto fail;
1734 ret = 0;
1735 fail:
1736 free(buf);
1737 if (ret > 0)
1738 ret = -1;
1739 return ret;
1742 static int relocate_one_reference(struct btrfs_trans_handle *trans,
1743 struct btrfs_root *root,
1744 u64 extent_start, u64 extent_size,
1745 u64 objectid, u64 offset,
1746 struct extent_io_tree *reloc_tree)
1748 struct extent_buffer *leaf;
1749 struct btrfs_file_extent_item *fi;
1750 struct btrfs_key key;
1751 struct btrfs_path path;
1752 struct btrfs_inode_item inode;
1753 struct blk_iterate_data data;
1754 u64 bytenr;
1755 u64 num_bytes;
1756 u64 cur_offset;
1757 u64 new_pos;
1758 u64 nblocks;
1759 u64 root_gen;
1760 u64 root_owner;
1761 u64 sector_end;
1762 u32 sectorsize = root->sectorsize;
1763 unsigned long ptr;
1764 int fd;
1765 int ret;
1767 btrfs_init_path(&path);
1768 ret = btrfs_lookup_file_extent(trans, root, &path,
1769 objectid, offset, -1);
1770 if (ret)
1771 goto fail;
1773 leaf = path.nodes[0];
1774 fi = btrfs_item_ptr(leaf, path.slots[0],
1775 struct btrfs_file_extent_item);
1776 /* the extent may be referenced by old snapshot */
1777 if (extent_start != btrfs_file_extent_disk_bytenr(leaf, fi) ||
1778 extent_size != btrfs_file_extent_disk_num_bytes(leaf, fi)) {
1779 ret = 1;
1780 goto fail;
1783 root_gen = btrfs_header_generation(leaf);
1784 root_owner = btrfs_header_owner(leaf);
1785 bytenr = extent_start + btrfs_file_extent_offset(leaf, fi);
1786 num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
1788 ret = btrfs_del_item(trans, root, &path);
1789 if (ret)
1790 goto fail;
1791 btrfs_release_path(root, &path);
1793 key.objectid = objectid;
1794 key.offset = 0;
1795 key.type = BTRFS_INODE_ITEM_KEY;
1796 ret = btrfs_lookup_inode(trans, root, &path, &key, 0);
1797 if (ret)
1798 goto fail;
1800 leaf = path.nodes[0];
1801 ptr = btrfs_item_ptr_offset(leaf, path.slots[0]);
1802 read_extent_buffer(leaf, &inode, ptr, sizeof(inode));
1803 btrfs_release_path(root, &path);
1805 BUG_ON(num_bytes & (sectorsize - 1));
1806 nblocks = btrfs_stack_inode_nblocks(&inode) - num_bytes / 512;
1807 btrfs_set_stack_inode_nblocks(&inode, nblocks);
1809 data = (struct blk_iterate_data) {
1810 .trans = trans,
1811 .root = root,
1812 .inode = &inode,
1813 .objectid = objectid,
1814 .first_block = offset / sectorsize,
1815 .disk_block = 0,
1816 .num_blocks = 0,
1817 .boundary = (u64)-1,
1818 .checksum = 0,
1819 .errcode = 0,
1822 cur_offset = offset;
1823 while (num_bytes > 0) {
1824 sector_end = bytenr + sectorsize - 1;
1825 if (test_range_bit(reloc_tree, bytenr, sector_end,
1826 EXTENT_LOCKED, 1)) {
1827 ret = get_state_private(reloc_tree, bytenr, &new_pos);
1828 BUG_ON(ret);
1829 } else {
1830 ret = custom_alloc_extent(root, sectorsize, 0, &key);
1831 if (ret)
1832 goto fail;
1833 new_pos = key.objectid;
1835 if (cur_offset == offset) {
1836 fd = root->fs_info->fs_devices->latest_bdev;
1837 readahead(fd, bytenr, num_bytes);
1839 ret = copy_disk_extent(root, new_pos, bytenr,
1840 sectorsize);
1841 if (ret)
1842 goto fail;
1843 ret = set_extent_bits(reloc_tree, bytenr, sector_end,
1844 EXTENT_LOCKED, GFP_NOFS);
1845 BUG_ON(ret);
1846 ret = set_state_private(reloc_tree, bytenr, new_pos);
1847 BUG_ON(ret);
1850 ret = block_iterate_proc(NULL, new_pos / sectorsize,
1851 cur_offset / sectorsize, &data);
1852 if (ret & BLOCK_ABORT) {
1853 ret = data.errcode;
1854 goto fail;
1857 cur_offset += sectorsize;
1858 bytenr += sectorsize;
1859 num_bytes -= sectorsize;
1862 if (data.num_blocks > 0) {
1863 ret = record_file_blocks(trans, root, objectid, &inode,
1864 data.first_block, data.disk_block,
1865 data.num_blocks, 0);
1866 if (ret)
1867 goto fail;
1870 key.objectid = objectid;
1871 key.offset = 0;
1872 key.type = BTRFS_INODE_ITEM_KEY;
1873 ret = btrfs_lookup_inode(trans, root, &path, &key, 1);
1874 if (ret)
1875 goto fail;
1877 leaf = path.nodes[0];
1878 ptr = btrfs_item_ptr_offset(leaf, path.slots[0]);
1879 write_extent_buffer(leaf, &inode, ptr, sizeof(inode));
1880 btrfs_mark_buffer_dirty(leaf);
1881 btrfs_release_path(root, &path);
1883 ret = btrfs_free_extent(trans, root, extent_start, extent_size,
1884 root_owner, root_gen, objectid, offset, 0);
1885 fail:
1886 btrfs_release_path(root, &path);
1887 return ret;
1890 static int relocate_extents_range(struct btrfs_root *fs_root,
1891 struct btrfs_root *ext2_root,
1892 u64 start_byte, u64 end_byte)
1894 struct btrfs_fs_info *info = fs_root->fs_info;
1895 struct btrfs_root *extent_root = info->extent_root;
1896 struct btrfs_root *cur_root;
1897 struct btrfs_trans_handle *trans;
1898 struct btrfs_extent_ref *ref_item;
1899 struct extent_buffer *leaf;
1900 struct btrfs_key key;
1901 struct btrfs_path path;
1902 struct extent_io_tree reloc_tree;
1903 u64 cur_byte;
1904 u64 num_bytes;
1905 u64 ref_root;
1906 u64 ref_owner;
1907 u64 ref_offset;
1908 u64 num_refs;
1909 int pass = 0;
1910 int ret;
1911 int found;
1913 btrfs_init_path(&path);
1914 extent_io_tree_init(&reloc_tree);
1915 again:
1916 cur_root = (pass % 2 == 0) ? ext2_root : fs_root;
1917 num_refs = 0;
1919 trans = btrfs_start_transaction(cur_root, 1);
1920 BUG_ON(!trans);
1922 cur_byte = start_byte;
1923 while (1) {
1924 key.objectid = cur_byte;
1925 key.offset = 0;
1926 key.type = BTRFS_EXTENT_ITEM_KEY;
1927 ret = btrfs_search_slot(trans, extent_root,
1928 &key, &path, 0, 0);
1929 if (ret < 0)
1930 goto fail;
1931 next:
1932 leaf = path.nodes[0];
1933 if (path.slots[0] >= btrfs_header_nritems(leaf)) {
1934 ret = btrfs_next_leaf(extent_root, &path);
1935 if (ret < 0)
1936 goto fail;
1937 if (ret > 0)
1938 break;
1939 leaf = path.nodes[0];
1942 btrfs_item_key_to_cpu(leaf, &key, path.slots[0]);
1943 if (key.objectid < cur_byte ||
1944 key.type != BTRFS_EXTENT_ITEM_KEY) {
1945 path.slots[0]++;
1946 goto next;
1948 if (key.objectid >= end_byte)
1949 break;
1951 cur_byte = key.objectid;
1952 num_bytes = key.offset;
1953 found = 0;
1954 while (1) {
1955 if (path.slots[0] >= btrfs_header_nritems(leaf)) {
1956 ret = btrfs_next_leaf(extent_root, &path);
1957 if (ret > 0)
1958 break;
1959 if (ret < 0)
1960 goto fail;
1961 leaf = path.nodes[0];
1964 btrfs_item_key_to_cpu(leaf, &key, path.slots[0]);
1965 if (key.objectid != cur_byte)
1966 break;
1967 if (key.type != BTRFS_EXTENT_REF_KEY) {
1968 path.slots[0]++;
1969 continue;
1971 ref_item = btrfs_item_ptr(leaf, path.slots[0],
1972 struct btrfs_extent_ref);
1973 ref_root = btrfs_ref_root(leaf, ref_item);
1974 ref_owner = btrfs_ref_objectid(leaf, ref_item);
1975 ref_offset = btrfs_ref_offset(leaf, ref_item);
1976 num_refs++;
1978 BUG_ON(ref_owner < BTRFS_FIRST_FREE_OBJECTID);
1979 if (ref_root == cur_root->root_key.objectid) {
1980 found = 1;
1981 break;
1983 path.slots[0]++;
1985 if (!found)
1986 goto next;
1988 ret = relocate_one_reference(trans, cur_root, cur_byte,
1989 num_bytes, ref_owner,
1990 ref_offset, &reloc_tree);
1991 if (ret < 0)
1992 goto fail;
1994 cur_byte += num_bytes;
1995 btrfs_release_path(extent_root, &path);
1997 if (trans->blocks_used >= 4096) {
1998 ret = btrfs_commit_transaction(trans, cur_root);
1999 BUG_ON(ret);
2000 trans = btrfs_start_transaction(cur_root, 1);
2001 BUG_ON(!trans);
2004 btrfs_release_path(cur_root, &path);
2006 ret = btrfs_commit_transaction(trans, cur_root);
2007 BUG_ON(ret);
2009 if (num_refs > 0 && pass++ < 4)
2010 goto again;
2012 ret = (num_refs > 0) ? -1 : 0;
2013 fail:
2014 btrfs_release_path(cur_root, &path);
2015 extent_io_tree_cleanup(&reloc_tree);
2016 return ret;
2020 * relocate data in system chunk
2022 static int cleanup_sys_chunk(struct btrfs_root *fs_root,
2023 struct btrfs_root *ext2_root)
2025 struct btrfs_block_group_cache *cache;
2026 int ret = 0;
2027 u64 offset = 0;
2028 u64 end_byte;
2030 while(1) {
2031 cache = btrfs_lookup_block_group(fs_root->fs_info, offset);
2032 if (!cache)
2033 break;
2035 end_byte = cache->key.objectid + cache->key.offset;
2036 if (cache->flags & BTRFS_BLOCK_GROUP_SYSTEM) {
2037 ret = relocate_extents_range(fs_root, ext2_root,
2038 cache->key.objectid,
2039 end_byte);
2040 if (ret)
2041 goto fail;
2043 offset = end_byte;
2045 fail:
2046 return ret;
2049 static int fixup_chunk_mapping(struct btrfs_root *root)
2051 struct btrfs_trans_handle *trans;
2052 struct btrfs_fs_info *info = root->fs_info;
2053 struct btrfs_root *chunk_root = info->chunk_root;
2054 struct extent_buffer *leaf;
2055 struct btrfs_key key;
2056 struct btrfs_path path;
2057 struct btrfs_chunk chunk;
2058 unsigned long ptr;
2059 u32 size;
2060 u64 type;
2061 int ret;
2063 btrfs_init_path(&path);
2065 trans = btrfs_start_transaction(root, 1);
2066 BUG_ON(!trans);
2069 * recow the whole chunk tree. this will move all chunk tree blocks
2070 * into system block group.
2072 memset(&key, 0, sizeof(key));
2073 while (1) {
2074 ret = btrfs_search_slot(trans, chunk_root, &key, &path, 0, 1);
2075 if (ret < 0)
2076 goto err;
2078 ret = btrfs_next_leaf(chunk_root, &path);
2079 if (ret < 0)
2080 goto err;
2081 if (ret > 0)
2082 break;
2084 btrfs_item_key_to_cpu(path.nodes[0], &key, path.slots[0]);
2085 btrfs_release_path(chunk_root, &path);
2087 btrfs_release_path(chunk_root, &path);
2089 /* fixup the system chunk array in super block */
2090 btrfs_set_super_sys_array_size(&info->super_copy, 0);
2092 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2093 key.offset = 0;
2094 key.type = BTRFS_CHUNK_ITEM_KEY;
2096 ret = btrfs_search_slot(trans, chunk_root, &key, &path, 0, 0);
2097 if (ret < 0)
2098 goto err;
2099 BUG_ON(ret != 0);
2100 while(1) {
2101 leaf = path.nodes[0];
2102 if (path.slots[0] >= btrfs_header_nritems(leaf)) {
2103 ret = btrfs_next_leaf(chunk_root, &path);
2104 if (ret < 0)
2105 goto err;
2106 if (ret > 0)
2107 break;
2108 leaf = path.nodes[0];
2110 btrfs_item_key_to_cpu(leaf, &key, path.slots[0]);
2111 if (key.type != BTRFS_CHUNK_ITEM_KEY)
2112 goto next;
2114 ptr = btrfs_item_ptr_offset(leaf, path.slots[0]);
2115 size = btrfs_item_size_nr(leaf, path.slots[0]);
2116 BUG_ON(size != sizeof(chunk));
2117 read_extent_buffer(leaf, &chunk, ptr, size);
2118 type = btrfs_stack_chunk_type(&chunk);
2120 if (!(type & BTRFS_BLOCK_GROUP_SYSTEM))
2121 goto next;
2123 ret = btrfs_add_system_chunk(trans, chunk_root, &key,
2124 &chunk, size);
2125 if (ret)
2126 goto err;
2127 next:
2128 path.slots[0]++;
2131 ret = btrfs_commit_transaction(trans, root);
2132 BUG_ON(ret);
2133 err:
2134 btrfs_release_path(chunk_root, &path);
2135 return ret;
2138 int do_convert(const char *devname, int datacsum, int packing, int noxattr)
2140 int i, fd, ret;
2141 u32 blocksize;
2142 u64 blocks[6];
2143 u64 total_bytes;
2144 u64 super_bytenr;
2145 ext2_filsys ext2_fs;
2146 struct btrfs_root *root;
2147 struct btrfs_root *ext2_root;
2149 ret = open_ext2fs(devname, &ext2_fs);
2150 if (ret) {
2151 fprintf(stderr, "unable to open the Ext2fs\n");
2152 goto fail;
2154 blocksize = ext2_fs->blocksize;
2155 total_bytes = (u64)ext2_fs->super->s_blocks_count * blocksize;
2156 if (blocksize < 4096) {
2157 fprintf(stderr, "block size is too small\n");
2158 goto fail;
2160 if (!(ext2_fs->super->s_feature_incompat &
2161 EXT2_FEATURE_INCOMPAT_FILETYPE)) {
2162 fprintf(stderr, "filetype feature is missing\n");
2163 goto fail;
2165 for (i = 0; i < 6; i++) {
2166 ret = ext2_alloc_block(ext2_fs, 0, blocks + i);
2167 if (ret) {
2168 fprintf(stderr, "not enough free space\n");
2169 goto fail;
2171 blocks[i] *= blocksize;
2173 super_bytenr = blocks[0];
2174 fd = open(devname, O_RDWR);
2175 if (fd < 0) {
2176 fprintf(stderr, "unable to open %s\n", devname);
2177 goto fail;
2179 ret = make_btrfs(fd, devname, ext2_fs->super->s_volume_name,
2180 blocks, total_bytes, blocksize, blocksize,
2181 blocksize, blocksize);
2182 if (ret) {
2183 fprintf(stderr, "unable to create initial ctree\n");
2184 goto fail;
2186 /* create a system chunk that maps the whole device */
2187 ret = prepare_system_chunk(fd, super_bytenr, blocksize);
2188 if (ret) {
2189 fprintf(stderr, "unable to update system chunk\n");
2190 goto fail;
2192 root = open_ctree_fd(fd, devname, super_bytenr, O_RDWR);
2193 if (!root) {
2194 fprintf(stderr, "unable to open ctree\n");
2195 goto fail;
2197 ret = cache_free_extents(root, ext2_fs);
2198 if (ret) {
2199 fprintf(stderr, "error during cache_free_extents %d\n", ret);
2200 goto fail;
2202 root->fs_info->extent_ops = &extent_ops;
2203 /* recover block allocation bitmap */
2204 for (i = 0; i < 6; i++) {
2205 blocks[i] /= blocksize;
2206 ext2_free_block(ext2_fs, blocks[i]);
2208 ret = init_btrfs(root);
2209 if (ret) {
2210 fprintf(stderr, "unable to setup the root tree\n");
2211 goto fail;
2213 ext2_root = create_subvol(root, "ext2_saved");
2214 if (!ext2_root) {
2215 fprintf(stderr, "unable to create subvol\n");
2216 goto fail;
2218 printf("creating btrfs metadata.\n");
2219 ret = copy_inodes(root, ext2_fs, datacsum, packing, noxattr);
2220 if (ret) {
2221 fprintf(stderr, "error during copy_inodes %d\n", ret);
2222 goto fail;
2224 printf("creating ext2fs image file.\n");
2225 ret = create_ext2_image(ext2_root, ext2_fs, "image");
2226 if (ret) {
2227 fprintf(stderr, "error during create_ext2_image %d\n", ret);
2228 goto fail;
2230 printf("cleaning up system chunk.\n");
2231 ret = cleanup_sys_chunk(root, ext2_root);
2232 if (ret) {
2233 fprintf(stderr, "error during cleanup_sys_chunk %d\n", ret);
2234 goto fail;
2236 btrfs_free_fs_root(ext2_root->fs_info, ext2_root);
2237 ret = close_ctree(root);
2238 if (ret) {
2239 fprintf(stderr, "error during close_ctree %d\n", ret);
2240 goto fail;
2242 close_ext2fs(ext2_fs);
2245 * If this step succeed, we get a mountable btrfs. Otherwise
2246 * the ext2fs is left unchanged.
2248 ret = migrate_super_block(fd, super_bytenr, blocksize);
2249 if (ret) {
2250 fprintf(stderr, "unable to migrate super block\n");
2251 goto fail;
2254 root = open_ctree_fd(fd, devname, 0, O_RDWR);
2255 if (!root) {
2256 fprintf(stderr, "unable to open ctree\n");
2257 goto fail;
2259 /* move chunk tree into system chunk. */
2260 ret = fixup_chunk_mapping(root);
2261 if (ret) {
2262 fprintf(stderr, "error during fixup_chunk_tree\n");
2263 goto fail;
2265 ret = close_ctree(root);
2266 close(fd);
2268 printf("conversion complete.\n");
2269 return 0;
2270 fail:
2271 fprintf(stderr, "conversion aborted.\n");
2272 return -1;
2275 static int may_rollback(struct btrfs_root *root)
2277 struct btrfs_fs_info *info = root->fs_info;
2278 struct btrfs_multi_bio *multi = NULL;
2279 u64 bytenr;
2280 u64 length;
2281 u64 physical;
2282 u64 total_bytes;
2283 int num_stripes;
2284 int ret;
2286 if (btrfs_super_num_devices(&info->super_copy) != 1)
2287 goto fail;
2289 bytenr = BTRFS_SUPER_INFO_OFFSET;
2290 total_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy);
2292 while (1) {
2293 ret = btrfs_map_block(&info->mapping_tree, WRITE, bytenr,
2294 &length, &multi, 0);
2295 if (ret)
2296 goto fail;
2298 num_stripes = multi->num_stripes;
2299 physical = multi->stripes[0].physical;
2300 kfree(multi);
2302 if (num_stripes != 1 || physical != bytenr)
2303 goto fail;
2305 bytenr += length;
2306 if (bytenr >= total_bytes)
2307 break;
2309 return 0;
2310 fail:
2311 return -1;
2314 int do_rollback(const char *devname, int force)
2316 int fd;
2317 int ret;
2318 struct btrfs_root *root;
2319 struct btrfs_root *ext2_root;
2320 struct btrfs_root *chunk_root;
2321 struct btrfs_dir_item *dir;
2322 struct btrfs_inode_item *inode;
2323 struct btrfs_file_extent_item *fi;
2324 struct btrfs_trans_handle *trans;
2325 struct extent_buffer *leaf;
2326 struct btrfs_block_group_cache *cache1;
2327 struct btrfs_block_group_cache *cache2;
2328 struct btrfs_key key;
2329 struct btrfs_path path;
2330 struct extent_io_tree io_tree;
2331 char *buf;
2332 char *name;
2333 u64 bytenr;
2334 u64 num_bytes;
2335 u64 root_dir;
2336 u64 objectid;
2337 u64 offset;
2338 u64 start;
2339 u64 end;
2340 u64 sb_bytenr;
2341 u64 first_free;
2342 u64 total_bytes;
2343 u32 sectorsize;
2345 extent_io_tree_init(&io_tree);
2347 fd = open(devname, O_RDWR);
2348 if (fd < 0) {
2349 fprintf(stderr, "unable to open %s\n", devname);
2350 goto fail;
2352 root = open_ctree_fd(fd, devname, 0, O_RDWR);
2353 if (!root) {
2354 fprintf(stderr, "unable to open ctree\n");
2355 goto fail;
2357 ret = may_rollback(root);
2358 if (ret < 0) {
2359 fprintf(stderr, "unable to do rollback\n");
2360 goto fail;
2363 sectorsize = root->sectorsize;
2364 buf = malloc(sectorsize);
2365 if (!buf) {
2366 fprintf(stderr, "unable to allocate memory\n");
2367 goto fail;
2370 btrfs_init_path(&path);
2371 name = "ext2_saved";
2372 root_dir = btrfs_super_root_dir(&root->fs_info->super_copy);
2373 dir = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root, &path,
2374 root_dir, name, strlen(name), 0);
2375 if (!dir || IS_ERR(dir)) {
2376 fprintf(stderr, "unable to find subvol %s\n", name);
2377 goto fail;
2379 leaf = path.nodes[0];
2380 btrfs_dir_item_key_to_cpu(leaf, dir, &key);
2381 btrfs_release_path(root->fs_info->tree_root, &path);
2383 ext2_root = btrfs_read_fs_root(root->fs_info, &key);
2384 if (!ext2_root || IS_ERR(ext2_root)) {
2385 fprintf(stderr, "unable to open subvol %s\n", name);
2386 goto fail;
2389 name = "image";
2390 root_dir = btrfs_root_dirid(&root->root_item);
2391 dir = btrfs_lookup_dir_item(NULL, ext2_root, &path,
2392 root_dir, name, strlen(name), 0);
2393 if (!dir || IS_ERR(dir)) {
2394 fprintf(stderr, "unable to find file %s\n", name);
2395 goto fail;
2397 leaf = path.nodes[0];
2398 btrfs_dir_item_key_to_cpu(leaf, dir, &key);
2399 btrfs_release_path(ext2_root, &path);
2401 objectid = key.objectid;
2403 ret = btrfs_lookup_inode(NULL, ext2_root, &path, &key, 0);
2404 if (ret) {
2405 fprintf(stderr, "unable to find inode item\n");
2406 goto fail;
2408 leaf = path.nodes[0];
2409 inode = btrfs_item_ptr(leaf, path.slots[0], struct btrfs_inode_item);
2410 total_bytes = btrfs_inode_size(leaf, inode);
2411 btrfs_release_path(ext2_root, &path);
2413 key.objectid = objectid;
2414 key.offset = 0;
2415 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
2416 ret = btrfs_search_slot(NULL, ext2_root, &key, &path, 0, 0);
2417 if (ret != 0) {
2418 fprintf(stderr, "unable to find first file extent\n");
2419 btrfs_release_path(ext2_root, &path);
2420 goto fail;
2423 /* build mapping tree for the relocated blocks */
2424 for (offset = 0; offset < total_bytes; ) {
2425 leaf = path.nodes[0];
2426 if (path.slots[0] >= btrfs_header_nritems(leaf)) {
2427 ret = btrfs_next_leaf(root, &path);
2428 if (ret != 0)
2429 break;
2430 continue;
2433 btrfs_item_key_to_cpu(leaf, &key, path.slots[0]);
2434 if (key.objectid != objectid || key.offset != offset ||
2435 btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
2436 break;
2438 fi = btrfs_item_ptr(leaf, path.slots[0],
2439 struct btrfs_file_extent_item);
2440 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG)
2441 break;
2443 bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
2444 /* skip holes and direct mapped extents */
2445 if (bytenr == 0 || bytenr == offset)
2446 goto next_extent;
2448 bytenr += btrfs_file_extent_offset(leaf, fi);
2449 num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
2451 cache1 = btrfs_lookup_block_group(root->fs_info, offset);
2452 cache2 = btrfs_lookup_block_group(root->fs_info,
2453 offset + num_bytes - 1);
2454 if (!cache1 || cache1 != cache2 ||
2455 !(cache1->flags & BTRFS_BLOCK_GROUP_SYSTEM))
2456 break;
2458 set_extent_bits(&io_tree, offset, offset + num_bytes - 1,
2459 EXTENT_LOCKED, GFP_NOFS);
2460 set_state_private(&io_tree, offset, bytenr);
2461 next_extent:
2462 offset += btrfs_file_extent_num_bytes(leaf, fi);
2463 path.slots[0]++;
2465 btrfs_release_path(ext2_root, &path);
2466 btrfs_free_fs_root(ext2_root->fs_info, ext2_root);
2468 if (offset < total_bytes) {
2469 fprintf(stderr, "unable to build extent mapping\n");
2470 goto fail;
2473 first_free = BTRFS_SUPER_INFO_OFFSET + 2 * sectorsize - 1;
2474 first_free &= ~((u64)sectorsize - 1);
2475 /* backup for extent #0 should exist */
2476 if(!test_range_bit(&io_tree, 0, first_free - 1, EXTENT_LOCKED, 1)) {
2477 fprintf(stderr, "no backup for the first extent\n");
2478 goto fail;
2480 /* force no allocation from system block group */
2481 root->fs_info->system_allocs = -1;
2482 trans = btrfs_start_transaction(root, 1);
2483 BUG_ON(!trans);
2485 * recow the whole chunk tree, this will remove all chunk tree blocks
2486 * from system block group
2488 chunk_root = root->fs_info->chunk_root;
2489 memset(&key, 0, sizeof(key));
2490 while (1) {
2491 ret = btrfs_search_slot(trans, chunk_root, &key, &path, 0, 1);
2492 if (ret < 0)
2493 break;
2495 ret = btrfs_next_leaf(chunk_root, &path);
2496 if (ret)
2497 break;
2499 btrfs_item_key_to_cpu(path.nodes[0], &key, path.slots[0]);
2500 btrfs_release_path(chunk_root, &path);
2502 btrfs_release_path(chunk_root, &path);
2504 offset = 0;
2505 num_bytes = 0;
2506 while(1) {
2507 cache1 = btrfs_lookup_block_group(root->fs_info, offset);
2508 if (!cache1)
2509 break;
2511 if (cache1->flags & BTRFS_BLOCK_GROUP_SYSTEM)
2512 num_bytes += btrfs_block_group_used(&cache1->item);
2514 offset = cache1->key.objectid + cache1->key.offset;
2516 /* only extent #0 left in system block group? */
2517 if (num_bytes > first_free) {
2518 fprintf(stderr, "unable to empty system block group\n");
2519 goto fail;
2521 /* create a system chunk that maps the whole device */
2522 ret = prepare_system_chunk_sb(&root->fs_info->super_copy);
2523 if (ret) {
2524 fprintf(stderr, "unable to update system chunk\n");
2525 goto fail;
2528 ret = btrfs_commit_transaction(trans, root);
2529 BUG_ON(ret);
2531 ret = close_ctree(root);
2532 if (ret) {
2533 fprintf(stderr, "error during close_ctree %d\n", ret);
2534 goto fail;
2537 sb_bytenr = (u64)-1;
2538 /* copy all relocated blocks back */
2539 while(1) {
2540 ret = find_first_extent_bit(&io_tree, 0, &start, &end,
2541 EXTENT_LOCKED);
2542 if (ret)
2543 break;
2545 ret = get_state_private(&io_tree, start, &bytenr);
2546 BUG_ON(ret);
2548 clear_extent_bits(&io_tree, start, end, EXTENT_LOCKED,
2549 GFP_NOFS);
2551 while (start <= end) {
2552 if (start == BTRFS_SUPER_INFO_OFFSET) {
2553 sb_bytenr = bytenr;
2554 goto next_sector;
2556 ret = pread(fd, buf, sectorsize, bytenr);
2557 if (ret < 0) {
2558 fprintf(stderr, "error during pread %d\n", ret);
2559 goto fail;
2561 BUG_ON(ret != sectorsize);
2562 ret = pwrite(fd, buf, sectorsize, start);
2563 if (ret < 0) {
2564 fprintf(stderr, "error during pwrite %d\n", ret);
2565 goto fail;
2567 BUG_ON(ret != sectorsize);
2568 next_sector:
2569 start += sectorsize;
2570 bytenr += sectorsize;
2574 ret = fsync(fd);
2575 if (ret) {
2576 fprintf(stderr, "error during fsync %d\n", ret);
2577 goto fail;
2580 * finally, overwrite btrfs super block.
2582 ret = pread(fd, buf, sectorsize, sb_bytenr);
2583 if (ret < 0) {
2584 fprintf(stderr, "error during pread %d\n", ret);
2585 goto fail;
2587 BUG_ON(ret != sectorsize);
2588 ret = pwrite(fd, buf, sectorsize, BTRFS_SUPER_INFO_OFFSET);
2589 if (ret < 0) {
2590 fprintf(stderr, "error during pwrite %d\n", ret);
2591 goto fail;
2593 BUG_ON(ret != sectorsize);
2594 ret = fsync(fd);
2595 if (ret) {
2596 fprintf(stderr, "error during fsync %d\n", ret);
2597 goto fail;
2600 close(fd);
2601 free(buf);
2602 extent_io_tree_cleanup(&io_tree);
2603 printf("rollback complete.\n");
2604 return 0;
2605 fail:
2606 fprintf(stderr, "rollback aborted.\n");
2607 return -1;
2610 static void print_usage(void)
2612 printf("usage: btrfs-convert [-d] [-i] [-n] [-r] device\n");
2613 printf("\t-d disable data checksum\n");
2614 printf("\t-i ignore xattrs and ACLs\n");
2615 printf("\t-n disable packing of small files\n");
2616 printf("\t-r roll back to ext2fs\n");
2619 int main(int argc, char *argv[])
2621 int ret;
2622 int packing = 1;
2623 int noxattr = 0;
2624 int datacsum = 1;
2625 int rollback = 0;
2626 char *file;
2627 while(1) {
2628 int c = getopt(argc, argv, "dinr");
2629 if (c < 0)
2630 break;
2631 switch(c) {
2632 case 'd':
2633 datacsum = 0;
2634 break;
2635 case 'i':
2636 noxattr = 1;
2637 break;
2638 case 'n':
2639 packing = 0;
2640 break;
2641 case 'r':
2642 rollback = 1;
2643 break;
2644 default:
2645 print_usage();
2646 return 1;
2649 argc = argc - optind;
2650 if (argc != 1) {
2651 print_usage();
2652 return 1;
2655 file = argv[optind];
2656 if (check_mounted(file)) {
2657 fprintf(stderr, "%s is mounted\n", file);
2658 return 1;
2661 if (rollback) {
2662 ret = do_rollback(file, 0);
2663 } else {
2664 ret = do_convert(file, datacsum, packing, noxattr);
2666 if (ret)
2667 return 1;
2668 return 0;