Modify the subvol and snapshot creation ioctls to work anywhere in the tree
[btrfs-progs-unstable/devel.git] / convert.c
blob5a3c14133fe6b6e1cd3bce64ebdb66fbec8c04b6
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 index_cnt;
192 u64 parent;
193 int errcode;
196 static u8 filetype_conversion_table[EXT2_FT_MAX] = {
197 [EXT2_FT_UNKNOWN] = BTRFS_FT_UNKNOWN,
198 [EXT2_FT_REG_FILE] = BTRFS_FT_REG_FILE,
199 [EXT2_FT_DIR] = BTRFS_FT_DIR,
200 [EXT2_FT_CHRDEV] = BTRFS_FT_CHRDEV,
201 [EXT2_FT_BLKDEV] = BTRFS_FT_BLKDEV,
202 [EXT2_FT_FIFO] = BTRFS_FT_FIFO,
203 [EXT2_FT_SOCK] = BTRFS_FT_SOCK,
204 [EXT2_FT_SYMLINK] = BTRFS_FT_SYMLINK,
207 static int dir_iterate_proc(ext2_ino_t dir, int entry,
208 struct ext2_dir_entry *old,
209 int offset, int blocksize,
210 char *buf,void *priv_data)
212 int ret;
213 int file_type;
214 u64 objectid;
215 u64 inode_size;
216 char dotdot[] = "..";
217 struct btrfs_key location;
218 struct ext2_dir_entry_2 *dirent = (struct ext2_dir_entry_2 *)old;
219 struct dir_iterate_data *idata = (struct dir_iterate_data *)priv_data;
221 objectid = dirent->inode + INO_OFFSET;
222 if (!strncmp(dirent->name, dotdot, dirent->name_len)) {
223 if (dirent->name_len == 2) {
224 BUG_ON(idata->parent != 0);
225 idata->parent = objectid;
227 return 0;
229 if (dirent->inode < EXT2_GOOD_OLD_FIRST_INO)
230 return 0;
232 location.objectid = objectid;
233 location.offset = 0;
234 btrfs_set_key_type(&location, BTRFS_INODE_ITEM_KEY);
236 file_type = dirent->file_type;
237 BUG_ON(file_type > EXT2_FT_SYMLINK);
238 ret = btrfs_insert_dir_item(idata->trans, idata->root,
239 dirent->name, dirent->name_len,
240 idata->objectid, &location,
241 filetype_conversion_table[file_type]);
242 if (ret)
243 goto fail;
244 ret = btrfs_insert_inode_ref(idata->trans, idata->root,
245 dirent->name, dirent->name_len,
246 objectid, idata->objectid,
247 idata->index_cnt++);
248 if (ret)
249 goto fail;
250 inode_size = btrfs_stack_inode_size(idata->inode) +
251 dirent->name_len * 2;
252 btrfs_set_stack_inode_size(idata->inode, inode_size);
253 return 0;
254 fail:
255 idata->errcode = ret;
256 return BLOCK_ABORT;
259 static int create_dir_entries(struct btrfs_trans_handle *trans,
260 struct btrfs_root *root, u64 objectid,
261 struct btrfs_inode_item *btrfs_inode,
262 ext2_filsys ext2_fs, ext2_ino_t ext2_ino)
264 int ret;
265 errcode_t err;
266 struct dir_iterate_data data = {
267 .trans = trans,
268 .root = root,
269 .inode = btrfs_inode,
270 .objectid = objectid,
271 .index_cnt = 2,
272 .parent = 0,
273 .errcode = 0,
276 err = ext2fs_dir_iterate2(ext2_fs, ext2_ino, 0, NULL,
277 dir_iterate_proc, &data);
278 if (err)
279 goto error;
280 ret = data.errcode;
281 if (ret == 0 && data.parent == objectid) {
282 ret = btrfs_insert_inode_ref(trans, root, "..", 2,
283 objectid, objectid, 0);
285 return ret;
286 error:
287 fprintf(stderr, "ext2fs_dir_iterate2: %s\n", error_message(err));
288 return -1;
291 static int read_disk_extent(struct btrfs_root *root, u64 bytenr,
292 u32 num_bytes, char *buffer)
294 int ret;
295 struct btrfs_fs_devices *fs_devs = root->fs_info->fs_devices;
297 ret = pread(fs_devs->latest_bdev, buffer, num_bytes, bytenr);
298 if (ret != num_bytes)
299 goto fail;
300 ret = 0;
301 fail:
302 if (ret > 0)
303 ret = -1;
304 return ret;
307 * Record a file extent. Do all the required works, such as inserting
308 * file extent item, inserting extent item and backref item into extent
309 * tree and updating block accounting.
311 static int record_file_extent(struct btrfs_trans_handle *trans,
312 struct btrfs_root *root, u64 objectid,
313 struct btrfs_inode_item *inode,
314 u64 file_pos, u64 disk_bytenr,
315 u64 num_bytes, int checksum)
317 int ret;
318 struct btrfs_fs_info *info = root->fs_info;
319 struct btrfs_root *extent_root = info->extent_root;
320 struct extent_buffer *leaf;
321 struct btrfs_file_extent_item *fi;
322 struct btrfs_key ins_key;
323 struct btrfs_path path;
324 struct btrfs_extent_item extent_item;
325 u32 blocksize = root->sectorsize;
326 u64 nblocks;
327 u64 bytes_used;
329 if (disk_bytenr == 0) {
330 ret = btrfs_insert_file_extent(trans, root, objectid,
331 file_pos, disk_bytenr,
332 num_bytes, num_bytes);
333 return ret;
336 btrfs_init_path(&path);
338 if (checksum) {
339 u64 offset;
340 char *buffer;
342 ret = -ENOMEM;
343 buffer = malloc(blocksize);
344 if (!buffer)
345 goto fail;
346 for (offset = 0; offset < num_bytes; offset += blocksize) {
347 ret = read_disk_extent(root, disk_bytenr + offset,
348 blocksize, buffer);
349 if (ret)
350 break;
351 ret = btrfs_csum_file_block(trans, root, inode,
352 objectid, file_pos + offset,
353 buffer, blocksize);
354 if (ret)
355 break;
357 free(buffer);
358 if (ret)
359 goto fail;
362 ins_key.objectid = objectid;
363 ins_key.offset = file_pos;
364 btrfs_set_key_type(&ins_key, BTRFS_EXTENT_DATA_KEY);
365 ret = btrfs_insert_empty_item(trans, root, &path, &ins_key,
366 sizeof(*fi));
367 if (ret)
368 goto fail;
369 leaf = path.nodes[0];
370 fi = btrfs_item_ptr(leaf, path.slots[0],
371 struct btrfs_file_extent_item);
372 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
373 btrfs_set_file_extent_type(leaf, fi, BTRFS_FILE_EXTENT_REG);
374 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
375 btrfs_set_file_extent_disk_num_bytes(leaf, fi, num_bytes);
376 btrfs_set_file_extent_offset(leaf, fi, 0);
377 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
378 btrfs_mark_buffer_dirty(leaf);
380 nblocks = btrfs_stack_inode_nblocks(inode) + num_bytes / 512;
381 btrfs_set_stack_inode_nblocks(inode, nblocks);
383 bytes_used = btrfs_root_used(&root->root_item);
384 btrfs_set_root_used(&root->root_item, bytes_used + num_bytes);
386 ins_key.objectid = disk_bytenr;
387 ins_key.offset = num_bytes;
388 btrfs_set_key_type(&ins_key, BTRFS_EXTENT_ITEM_KEY);
389 btrfs_set_stack_extent_refs(&extent_item, 0);
390 ret = btrfs_insert_item(trans, extent_root, &ins_key,
391 &extent_item, sizeof(extent_item));
392 if (ret == 0) {
393 bytes_used = btrfs_super_bytes_used(&info->super_copy);
394 btrfs_set_super_bytes_used(&info->super_copy, bytes_used +
395 num_bytes);
396 } else if (ret != -EEXIST) {
397 goto fail;
399 btrfs_extent_post_op(trans, extent_root);
401 ret = btrfs_inc_extent_ref(trans, root, disk_bytenr, num_bytes,
402 leaf->start, root->root_key.objectid,
403 trans->transid, objectid, file_pos);
404 if (ret)
405 goto fail;
406 ret = 0;
407 fail:
408 btrfs_release_path(root, &path);
409 return ret;
412 static int record_file_blocks(struct btrfs_trans_handle *trans,
413 struct btrfs_root *root, u64 objectid,
414 struct btrfs_inode_item *inode,
415 u64 file_block, u64 disk_block,
416 u64 num_blocks, int checksum)
418 u64 file_pos = file_block * root->sectorsize;
419 u64 disk_bytenr = disk_block * root->sectorsize;
420 u64 num_bytes = num_blocks * root->sectorsize;
421 return record_file_extent(trans, root, objectid, inode, file_pos,
422 disk_bytenr, num_bytes, checksum);
425 struct blk_iterate_data {
426 struct btrfs_trans_handle *trans;
427 struct btrfs_root *root;
428 struct btrfs_inode_item *inode;
429 u64 objectid;
430 u64 first_block;
431 u64 disk_block;
432 u64 num_blocks;
433 u64 boundary;
434 int checksum;
435 int errcode;
438 static int block_iterate_proc(ext2_filsys ext2_fs,
439 u64 disk_block, u64 file_block,
440 struct blk_iterate_data *idata)
442 int ret;
443 struct btrfs_root *root = idata->root;
444 struct btrfs_trans_handle *trans = idata->trans;
445 struct btrfs_block_group_cache *cache;
446 u64 bytenr;
448 BUG_ON(disk_block == 0);
449 if ((idata->num_blocks > 0 && disk_block >= idata->boundary) ||
450 (file_block > idata->first_block + idata->num_blocks) ||
451 (disk_block != idata->disk_block + idata->num_blocks)) {
452 if (idata->num_blocks > 0) {
453 ret = record_file_blocks(trans, root, idata->objectid,
454 idata->inode, idata->first_block,
455 idata->disk_block, idata->num_blocks,
456 idata->checksum);
457 if (ret)
458 goto fail;
459 idata->first_block += idata->num_blocks;
460 idata->num_blocks = 0;
462 if (file_block > idata->first_block) {
463 ret = record_file_blocks(trans, root, idata->objectid,
464 idata->inode, idata->first_block,
465 0, file_block - idata->first_block,
466 idata->checksum);
467 if (ret)
468 goto fail;
471 bytenr = disk_block * root->sectorsize;
472 cache = btrfs_lookup_block_group(root->fs_info, bytenr);
473 BUG_ON(!cache);
474 bytenr = cache->key.objectid + cache->key.offset;
476 idata->first_block = file_block;
477 idata->disk_block = disk_block;
478 idata->boundary = bytenr / root->sectorsize;
480 idata->num_blocks++;
481 return 0;
482 fail:
483 idata->errcode = ret;
484 return BLOCK_ABORT;
487 static int __block_iterate_proc(ext2_filsys fs, blk_t *blocknr,
488 e2_blkcnt_t blockcnt, blk_t ref_block,
489 int ref_offset, void *priv_data)
491 struct blk_iterate_data *idata;
492 idata = (struct blk_iterate_data *)priv_data;
493 return block_iterate_proc(fs, *blocknr, blockcnt, idata);
497 * traverse file's data blocks, record these data blocks as file extents.
499 static int create_file_extents(struct btrfs_trans_handle *trans,
500 struct btrfs_root *root, u64 objectid,
501 struct btrfs_inode_item *btrfs_inode,
502 ext2_filsys ext2_fs, ext2_ino_t ext2_ino,
503 int datacsum, int packing)
505 int ret;
506 char *buffer = NULL;
507 errcode_t err;
508 u32 last_block;
509 u32 sectorsize = root->sectorsize;
510 u64 inode_size = btrfs_stack_inode_size(btrfs_inode);
511 struct blk_iterate_data data = {
512 .trans = trans,
513 .root = root,
514 .inode = btrfs_inode,
515 .objectid = objectid,
516 .first_block = 0,
517 .disk_block = 0,
518 .num_blocks = 0,
519 .boundary = (u64)-1,
520 .checksum = datacsum,
521 .errcode = 0,
523 err = ext2fs_block_iterate2(ext2_fs, ext2_ino, BLOCK_FLAG_DATA_ONLY,
524 NULL, __block_iterate_proc, &data);
525 if (err)
526 goto error;
527 ret = data.errcode;
528 if (ret)
529 goto fail;
530 if (packing && data.first_block == 0 && data.num_blocks > 0 &&
531 inode_size <= BTRFS_MAX_INLINE_DATA_SIZE(root)) {
532 u64 num_bytes = data.num_blocks * sectorsize;
533 u64 disk_bytenr = data.disk_block * sectorsize;
535 buffer = malloc(num_bytes);
536 if (!buffer)
537 return -ENOMEM;
538 ret = read_disk_extent(root, disk_bytenr, num_bytes, buffer);
539 if (ret)
540 goto fail;
541 if (num_bytes > inode_size)
542 num_bytes = inode_size;
543 ret = btrfs_insert_inline_extent(trans, root, objectid,
544 0, buffer, num_bytes);
545 if (ret)
546 goto fail;
547 } else if (data.num_blocks > 0) {
548 ret = record_file_blocks(trans, root, objectid, btrfs_inode,
549 data.first_block, data.disk_block,
550 data.num_blocks, data.checksum);
551 if (ret)
552 goto fail;
554 data.first_block += data.num_blocks;
555 last_block = (inode_size + sectorsize - 1) / sectorsize;
556 if (last_block > data.first_block) {
557 ret = record_file_blocks(trans, root, objectid, btrfs_inode,
558 data.first_block, 0, last_block -
559 data.first_block, data.checksum);
561 fail:
562 if (buffer)
563 free(buffer);
564 return ret;
565 error:
566 fprintf(stderr, "ext2fs_block_iterate2: %s\n", error_message(err));
567 return -1;
570 static int create_symbol_link(struct btrfs_trans_handle *trans,
571 struct btrfs_root *root, u64 objectid,
572 struct btrfs_inode_item *btrfs_inode,
573 ext2_filsys ext2_fs, ext2_ino_t ext2_ino,
574 struct ext2_inode *ext2_inode)
576 int ret;
577 char *pathname;
578 u64 inode_size = btrfs_stack_inode_size(btrfs_inode);
579 if (ext2fs_inode_data_blocks(ext2_fs, ext2_inode)) {
580 btrfs_set_stack_inode_size(btrfs_inode, inode_size + 1);
581 ret = create_file_extents(trans, root, objectid, btrfs_inode,
582 ext2_fs, ext2_ino, 1, 1);
583 btrfs_set_stack_inode_size(btrfs_inode, inode_size);
584 return ret;
587 pathname = (char *)&(ext2_inode->i_block[0]);
588 BUG_ON(pathname[inode_size] != 0);
589 ret = btrfs_insert_inline_extent(trans, root, objectid, 0,
590 pathname, inode_size + 1);
591 return ret;
595 * Following xattr/acl related codes are based on codes in
596 * fs/ext3/xattr.c and fs/ext3/acl.c
598 #define EXT2_XATTR_BHDR(ptr) ((struct ext2_ext_attr_header *)(ptr))
599 #define EXT2_XATTR_BFIRST(ptr) \
600 ((struct ext2_ext_attr_entry *)(EXT2_XATTR_BHDR(ptr) + 1))
601 #define EXT2_XATTR_IHDR(inode) \
602 ((struct ext2_ext_attr_header *) ((void *)(inode) + \
603 EXT2_GOOD_OLD_INODE_SIZE + (inode)->i_extra_isize))
604 #define EXT2_XATTR_IFIRST(inode) \
605 ((struct ext2_ext_attr_entry *) ((void *)EXT2_XATTR_IHDR(inode) + \
606 sizeof(EXT2_XATTR_IHDR(inode)->h_magic)))
608 static int ext2_xattr_check_names(struct ext2_ext_attr_entry *entry,
609 const void *end)
611 struct ext2_ext_attr_entry *next;
613 while (!EXT2_EXT_IS_LAST_ENTRY(entry)) {
614 next = EXT2_EXT_ATTR_NEXT(entry);
615 if ((void *)next >= end)
616 return -EIO;
617 entry = next;
619 return 0;
622 static int ext2_xattr_check_block(const char *buf, size_t size)
624 int error;
625 struct ext2_ext_attr_header *header = EXT2_XATTR_BHDR(buf);
627 if (header->h_magic != EXT2_EXT_ATTR_MAGIC ||
628 header->h_blocks != 1)
629 return -EIO;
630 error = ext2_xattr_check_names(EXT2_XATTR_BFIRST(buf), buf + size);
631 return error;
634 static int ext2_xattr_check_entry(struct ext2_ext_attr_entry *entry,
635 size_t size)
637 size_t value_size = entry->e_value_size;
639 if (entry->e_value_block != 0 || value_size > size ||
640 entry->e_value_offs + value_size > size)
641 return -EIO;
642 return 0;
645 #define EXT2_ACL_VERSION 0x0001
647 typedef struct {
648 __le16 e_tag;
649 __le16 e_perm;
650 __le32 e_id;
651 } ext2_acl_entry;
653 typedef struct {
654 __le16 e_tag;
655 __le16 e_perm;
656 } ext2_acl_entry_short;
658 typedef struct {
659 __le32 a_version;
660 } ext2_acl_header;
662 static inline int ext2_acl_count(size_t size)
664 ssize_t s;
665 size -= sizeof(ext2_acl_header);
666 s = size - 4 * sizeof(ext2_acl_entry_short);
667 if (s < 0) {
668 if (size % sizeof(ext2_acl_entry_short))
669 return -1;
670 return size / sizeof(ext2_acl_entry_short);
671 } else {
672 if (s % sizeof(ext2_acl_entry))
673 return -1;
674 return s / sizeof(ext2_acl_entry) + 4;
678 #define ACL_EA_VERSION 0x0002
680 typedef struct {
681 __le16 e_tag;
682 __le16 e_perm;
683 __le32 e_id;
684 } acl_ea_entry;
686 typedef struct {
687 __le32 a_version;
688 acl_ea_entry a_entries[0];
689 } acl_ea_header;
691 static inline size_t acl_ea_size(int count)
693 return sizeof(acl_ea_header) + count * sizeof(acl_ea_entry);
696 static int ext2_acl_to_xattr(void *dst, const void *src,
697 size_t dst_size, size_t src_size)
699 int i, count;
700 const void *end = src + src_size;
701 acl_ea_header *ext_acl = (acl_ea_header *)dst;
702 acl_ea_entry *dst_entry = ext_acl->a_entries;
703 ext2_acl_entry *src_entry;
705 if (src_size < sizeof(ext2_acl_header))
706 goto fail;
707 if (((ext2_acl_header *)src)->a_version !=
708 cpu_to_le32(EXT2_ACL_VERSION))
709 goto fail;
710 src += sizeof(ext2_acl_header);
711 count = ext2_acl_count(src_size);
712 if (count <= 0)
713 goto fail;
715 BUG_ON(dst_size < acl_ea_size(count));
716 ext_acl->a_version = cpu_to_le32(ACL_EA_VERSION);
717 for (i = 0; i < count; i++, dst_entry++) {
718 src_entry = (ext2_acl_entry *)src;
719 if (src + sizeof(ext2_acl_entry_short) > end)
720 goto fail;
721 dst_entry->e_tag = src_entry->e_tag;
722 dst_entry->e_perm = src_entry->e_perm;
723 switch (le16_to_cpu(src_entry->e_tag)) {
724 case ACL_USER_OBJ:
725 case ACL_GROUP_OBJ:
726 case ACL_MASK:
727 case ACL_OTHER:
728 src += sizeof(ext2_acl_entry_short);
729 dst_entry->e_id = cpu_to_le32(ACL_UNDEFINED_ID);
730 break;
731 case ACL_USER:
732 case ACL_GROUP:
733 src += sizeof(ext2_acl_entry);
734 if (src > end)
735 goto fail;
736 dst_entry->e_id = src_entry->e_id;
737 break;
738 default:
739 goto fail;
742 if (src != end)
743 goto fail;
744 return 0;
745 fail:
746 return -EINVAL;
749 static char *xattr_prefix_table[] = {
750 [1] = "user.",
751 [2] = "system.posix_acl_access",
752 [3] = "system.posix_acl_default",
753 [4] = "trusted.",
754 [6] = "security.",
757 static int copy_single_xattr(struct btrfs_trans_handle *trans,
758 struct btrfs_root *root, u64 objectid,
759 struct ext2_ext_attr_entry *entry,
760 const void *data, u32 datalen)
762 int ret = 0;
763 int name_len;
764 int name_index;
765 void *databuf = NULL;
766 char namebuf[XATTR_NAME_MAX + 1];
768 name_index = entry->e_name_index;
769 if (name_index >= ARRAY_SIZE(xattr_prefix_table) ||
770 xattr_prefix_table[name_index] == NULL)
771 return -EOPNOTSUPP;
772 name_len = strlen(xattr_prefix_table[name_index]) +
773 entry->e_name_len;
774 if (name_len >= sizeof(namebuf))
775 return -ERANGE;
777 if (name_index == 2 || name_index == 3) {
778 size_t bufsize = acl_ea_size(ext2_acl_count(datalen));
779 databuf = malloc(bufsize);
780 if (!databuf)
781 return -ENOMEM;
782 ret = ext2_acl_to_xattr(databuf, data, bufsize, datalen);
783 if (ret)
784 goto out;
785 data = databuf;
786 datalen = bufsize;
788 strcpy(namebuf, xattr_prefix_table[name_index]);
789 strncat(namebuf, EXT2_EXT_ATTR_NAME(entry), entry->e_name_len);
790 if (name_len + datalen > BTRFS_LEAF_DATA_SIZE(root) -
791 sizeof(struct btrfs_item) - sizeof(struct btrfs_dir_item)) {
792 fprintf(stderr, "skip large xattr on inode %Lu name %.*s\n",
793 objectid - INO_OFFSET, name_len, namebuf);
794 goto out;
796 ret = btrfs_insert_xattr_item(trans, root, namebuf, name_len,
797 data, datalen, objectid);
798 out:
799 if (databuf)
800 free(databuf);
801 return ret;
804 static int copy_extended_attrs(struct btrfs_trans_handle *trans,
805 struct btrfs_root *root, u64 objectid,
806 struct btrfs_inode_item *btrfs_inode,
807 ext2_filsys ext2_fs, ext2_ino_t ext2_ino)
809 int ret = 0;
810 int inline_ea = 0;
811 errcode_t err;
812 u32 datalen;
813 u32 block_size = ext2_fs->blocksize;
814 u32 inode_size = EXT2_INODE_SIZE(ext2_fs->super);
815 struct ext2_inode_large *ext2_inode;
816 struct ext2_ext_attr_entry *entry;
817 void *data;
818 char *buffer = NULL;
819 char inode_buf[EXT2_GOOD_OLD_INODE_SIZE];
821 if (inode_size <= EXT2_GOOD_OLD_INODE_SIZE) {
822 ext2_inode = (struct ext2_inode_large *)inode_buf;
823 } else {
824 ext2_inode = (struct ext2_inode_large *)malloc(inode_size);
825 if (!ext2_inode)
826 return -ENOMEM;
828 err = ext2fs_read_inode_full(ext2_fs, ext2_ino, (void *)ext2_inode,
829 inode_size);
830 if (err) {
831 fprintf(stderr, "ext2fs_read_inode_full: %s\n",
832 error_message(err));
833 ret = -1;
834 goto out;
837 if (ext2_ino > ext2_fs->super->s_first_ino &&
838 inode_size > EXT2_GOOD_OLD_INODE_SIZE) {
839 if (EXT2_GOOD_OLD_INODE_SIZE +
840 ext2_inode->i_extra_isize > inode_size) {
841 ret = -EIO;
842 goto out;
844 if (ext2_inode->i_extra_isize != 0 &&
845 EXT2_XATTR_IHDR(ext2_inode)->h_magic ==
846 EXT2_EXT_ATTR_MAGIC) {
847 inline_ea = 1;
850 if (inline_ea) {
851 int total;
852 void *end = (void *)ext2_inode + inode_size;
853 entry = EXT2_XATTR_IFIRST(ext2_inode);
854 total = end - (void *)entry;
855 ret = ext2_xattr_check_names(entry, end);
856 if (ret)
857 goto out;
858 while (!EXT2_EXT_IS_LAST_ENTRY(entry)) {
859 ret = ext2_xattr_check_entry(entry, total);
860 if (ret)
861 goto out;
862 data = (void *)EXT2_XATTR_IFIRST(ext2_inode) +
863 entry->e_value_offs;
864 datalen = entry->e_value_size;
865 ret = copy_single_xattr(trans, root, objectid,
866 entry, data, datalen);
867 if (ret)
868 goto out;
869 entry = EXT2_EXT_ATTR_NEXT(entry);
873 if (ext2_inode->i_file_acl == 0)
874 goto out;
876 buffer = malloc(block_size);
877 if (!buffer) {
878 ret = -ENOMEM;
879 goto out;
881 err = ext2fs_read_ext_attr(ext2_fs, ext2_inode->i_file_acl, buffer);
882 if (err) {
883 fprintf(stderr, "ext2fs_read_ext_attr: %s\n",
884 error_message(err));
885 ret = -1;
886 goto out;
888 ret = ext2_xattr_check_block(buffer, block_size);
889 if (ret)
890 goto out;
892 entry = EXT2_XATTR_BFIRST(buffer);
893 while (!EXT2_EXT_IS_LAST_ENTRY(entry)) {
894 ret = ext2_xattr_check_entry(entry, block_size);
895 if (ret)
896 goto out;
897 data = buffer + entry->e_value_offs;
898 datalen = entry->e_value_size;
899 ret = copy_single_xattr(trans, root, objectid,
900 entry, data, datalen);
901 if (ret)
902 goto out;
903 entry = EXT2_EXT_ATTR_NEXT(entry);
905 out:
906 if (buffer != NULL)
907 free(buffer);
908 if ((void *)ext2_inode != inode_buf)
909 free(ext2_inode);
910 return ret;
912 #define MINORBITS 20
913 #define MKDEV(ma, mi) (((ma) << MINORBITS) | (mi))
915 static inline dev_t old_decode_dev(u16 val)
917 return MKDEV((val >> 8) & 255, val & 255);
920 static inline dev_t new_decode_dev(u32 dev)
922 unsigned major = (dev & 0xfff00) >> 8;
923 unsigned minor = (dev & 0xff) | ((dev >> 12) & 0xfff00);
924 return MKDEV(major, minor);
927 static int copy_inode_item(struct btrfs_inode_item *dst,
928 struct ext2_inode *src)
930 btrfs_set_stack_inode_generation(dst, 1);
931 btrfs_set_stack_inode_size(dst, src->i_size);
932 btrfs_set_stack_inode_nblocks(dst, src->i_blocks);
933 btrfs_set_stack_inode_block_group(dst, 0);
934 btrfs_set_stack_inode_nblocks(dst, 0);
935 btrfs_set_stack_inode_nlink(dst, src->i_links_count);
936 btrfs_set_stack_inode_uid(dst, src->i_uid | (src->i_uid_high << 16));
937 btrfs_set_stack_inode_gid(dst, src->i_gid | (src->i_gid_high << 16));
938 btrfs_set_stack_inode_mode(dst, src->i_mode);
939 btrfs_set_stack_inode_rdev(dst, 0);
940 btrfs_set_stack_inode_flags(dst, 0);
941 btrfs_set_stack_inode_compat_flags(dst, 0);
942 btrfs_set_stack_timespec_sec(&dst->atime, src->i_atime);
943 btrfs_set_stack_timespec_nsec(&dst->atime, 0);
944 btrfs_set_stack_timespec_sec(&dst->ctime, src->i_ctime);
945 btrfs_set_stack_timespec_nsec(&dst->ctime, 0);
946 btrfs_set_stack_timespec_sec(&dst->mtime, src->i_mtime);
947 btrfs_set_stack_timespec_nsec(&dst->mtime, 0);
948 btrfs_set_stack_timespec_sec(&dst->otime, 0);
949 btrfs_set_stack_timespec_nsec(&dst->otime, 0);
951 if (S_ISDIR(src->i_mode)) {
952 btrfs_set_stack_inode_size(dst, 0);
953 btrfs_set_stack_inode_nlink(dst, 1);
955 if (S_ISREG(src->i_mode)) {
956 btrfs_set_stack_inode_size(dst, (u64)src->i_size_high << 32 |
957 (u64)src->i_size);
959 if (!S_ISREG(src->i_mode) && !S_ISDIR(src->i_mode) &&
960 !S_ISLNK(src->i_mode)) {
961 if (src->i_block[0]) {
962 btrfs_set_stack_inode_rdev(dst,
963 old_decode_dev(src->i_block[0]));
964 } else {
965 btrfs_set_stack_inode_rdev(dst,
966 new_decode_dev(src->i_block[1]));
969 return 0;
973 * copy a single inode. do all the required works, such as cloning
974 * inode item, creating file extents and creating directory entries.
976 static int copy_single_inode(struct btrfs_trans_handle *trans,
977 struct btrfs_root *root, u64 objectid,
978 ext2_filsys ext2_fs, ext2_ino_t ext2_ino,
979 struct ext2_inode *ext2_inode,
980 int datacsum, int packing, int noxattr)
982 int ret;
983 struct btrfs_key inode_key;
984 struct btrfs_inode_item btrfs_inode;
986 if (ext2_inode->i_links_count == 0)
987 return 0;
989 copy_inode_item(&btrfs_inode, ext2_inode);
990 if (!datacsum && S_ISREG(ext2_inode->i_mode)) {
991 u32 flags = btrfs_stack_inode_flags(&btrfs_inode) |
992 BTRFS_INODE_NODATASUM;
993 btrfs_set_stack_inode_flags(&btrfs_inode, flags);
996 switch (ext2_inode->i_mode & S_IFMT) {
997 case S_IFREG:
998 ret = create_file_extents(trans, root, objectid, &btrfs_inode,
999 ext2_fs, ext2_ino, datacsum, packing);
1000 break;
1001 case S_IFDIR:
1002 ret = create_dir_entries(trans, root, objectid, &btrfs_inode,
1003 ext2_fs, ext2_ino);
1004 break;
1005 case S_IFLNK:
1006 ret = create_symbol_link(trans, root, objectid, &btrfs_inode,
1007 ext2_fs, ext2_ino, ext2_inode);
1008 break;
1009 default:
1010 ret = 0;
1011 break;
1013 if (ret)
1014 return ret;
1016 if (!noxattr) {
1017 ret = copy_extended_attrs(trans, root, objectid, &btrfs_inode,
1018 ext2_fs, ext2_ino);
1019 if (ret)
1020 return ret;
1022 inode_key.objectid = objectid;
1023 inode_key.offset = 0;
1024 btrfs_set_key_type(&inode_key, BTRFS_INODE_ITEM_KEY);
1025 ret = btrfs_insert_inode(trans, root, objectid, &btrfs_inode);
1026 return ret;
1029 static int copy_disk_extent(struct btrfs_root *root, u64 dst_bytenr,
1030 u64 src_bytenr, u32 num_bytes)
1032 int ret;
1033 char *buffer;
1034 struct btrfs_fs_devices *fs_devs = root->fs_info->fs_devices;
1036 buffer = malloc(num_bytes);
1037 if (!buffer)
1038 return -ENOMEM;
1039 ret = pread(fs_devs->latest_bdev, buffer, num_bytes, src_bytenr);
1040 if (ret != num_bytes)
1041 goto fail;
1042 ret = pwrite(fs_devs->latest_bdev, buffer, num_bytes, dst_bytenr);
1043 if (ret != num_bytes)
1044 goto fail;
1045 ret = 0;
1046 fail:
1047 free(buffer);
1048 if (ret > 0)
1049 ret = -1;
1050 return ret;
1053 * scan ext2's inode bitmap and copy all used inode.
1055 static int copy_inodes(struct btrfs_root *root, ext2_filsys ext2_fs,
1056 int datacsum, int packing, int noxattr)
1058 int ret;
1059 errcode_t err;
1060 ext2_inode_scan ext2_scan;
1061 struct ext2_inode ext2_inode;
1062 ext2_ino_t ext2_ino;
1063 u64 objectid;
1064 struct btrfs_trans_handle *trans;
1066 trans = btrfs_start_transaction(root, 1);
1067 if (!trans)
1068 return -ENOMEM;
1069 err = ext2fs_open_inode_scan(ext2_fs, 0, &ext2_scan);
1070 if (err) {
1071 fprintf(stderr, "ext2fs_open_inode_scan: %s\n", error_message(err));
1072 return -1;
1074 while (!(err = ext2fs_get_next_inode(ext2_scan, &ext2_ino,
1075 &ext2_inode))) {
1076 /* no more inodes */
1077 if (ext2_ino == 0)
1078 break;
1079 /* skip special inode in ext2fs */
1080 if (ext2_ino < EXT2_GOOD_OLD_FIRST_INO &&
1081 ext2_ino != EXT2_ROOT_INO)
1082 continue;
1083 objectid = ext2_ino + INO_OFFSET;
1084 ret = copy_single_inode(trans, root,
1085 objectid, ext2_fs, ext2_ino,
1086 &ext2_inode, datacsum, packing,
1087 noxattr);
1088 if (ret)
1089 return ret;
1090 if (trans->blocks_used >= 4096) {
1091 ret = btrfs_commit_transaction(trans, root);
1092 BUG_ON(ret);
1093 trans = btrfs_start_transaction(root, 1);
1094 BUG_ON(!trans);
1097 if (err) {
1098 fprintf(stderr, "ext2fs_get_next_inode: %s\n", error_message(err));
1099 return -1;
1101 ret = btrfs_commit_transaction(trans, root);
1102 BUG_ON(ret);
1104 return ret;
1108 * Construct a range of ext2fs image file.
1109 * scan block allocation bitmap, find all blocks used by the ext2fs
1110 * in this range and create file extents that point to these blocks.
1112 * Note: Before calling the function, no file extent points to blocks
1113 * in this range
1115 static int create_image_file_range(struct btrfs_trans_handle *trans,
1116 struct btrfs_root *root, u64 objectid,
1117 struct btrfs_inode_item *inode,
1118 u64 start_byte, u64 end_byte,
1119 ext2_filsys ext2_fs)
1121 u32 blocksize = ext2_fs->blocksize;
1122 u32 block = start_byte / blocksize;
1123 u32 last_block = (end_byte + blocksize - 1) / blocksize;
1124 int ret = 0;
1125 struct blk_iterate_data data = {
1126 .trans = trans,
1127 .root = root,
1128 .inode = inode,
1129 .objectid = objectid,
1130 .first_block = block,
1131 .disk_block = 0,
1132 .num_blocks = 0,
1133 .boundary = (u64)-1,
1134 .checksum = 0,
1135 .errcode = 0,
1137 for (; start_byte < end_byte; block++, start_byte += blocksize) {
1138 if (!ext2fs_fast_test_block_bitmap(ext2_fs->block_map, block))
1139 continue;
1140 ret = block_iterate_proc(NULL, block, block, &data);
1141 if (ret & BLOCK_ABORT) {
1142 ret = data.errcode;
1143 goto fail;
1146 if (data.num_blocks > 0) {
1147 ret = record_file_blocks(trans, root, objectid, inode,
1148 data.first_block, data.disk_block,
1149 data.num_blocks, 0);
1150 if (ret)
1151 goto fail;
1152 data.first_block += data.num_blocks;
1154 if (last_block > data.first_block) {
1155 ret = record_file_blocks(trans, root, objectid, inode,
1156 data.first_block, 0, last_block -
1157 data.first_block, 0);
1158 if (ret)
1159 goto fail;
1161 fail:
1162 return ret;
1165 * Create the ext2fs image file.
1167 static int create_ext2_image(struct btrfs_root *root, ext2_filsys ext2_fs,
1168 const char *name)
1170 int ret;
1171 struct btrfs_key key;
1172 struct btrfs_key location;
1173 struct btrfs_path path;
1174 struct btrfs_inode_item btrfs_inode;
1175 struct btrfs_inode_item *inode_item;
1176 struct extent_buffer *leaf;
1177 struct btrfs_fs_info *fs_info = root->fs_info;
1178 struct btrfs_root *extent_root = fs_info->extent_root;
1179 struct btrfs_trans_handle *trans;
1180 struct btrfs_extent_ref *ref_item;
1181 u64 bytenr;
1182 u64 num_bytes;
1183 u64 ref_root;
1184 u64 ref_owner;
1185 u64 objectid;
1186 u64 last_byte;
1187 u64 first_free;
1188 u64 total_bytes;
1189 u32 sectorsize = root->sectorsize;
1190 int file_extent;
1192 total_bytes = btrfs_super_total_bytes(&fs_info->super_copy);
1193 first_free = BTRFS_SUPER_INFO_OFFSET + sectorsize * 2 - 1;
1194 first_free &= ~((u64)sectorsize - 1);
1196 memset(&btrfs_inode, 0, sizeof(btrfs_inode));
1197 btrfs_set_stack_inode_generation(&btrfs_inode, 1);
1198 btrfs_set_stack_inode_size(&btrfs_inode, total_bytes);
1199 btrfs_set_stack_inode_nlink(&btrfs_inode, 1);
1200 btrfs_set_stack_inode_nblocks(&btrfs_inode, 0);
1201 btrfs_set_stack_inode_mode(&btrfs_inode, S_IFREG | 0400);
1202 btrfs_set_stack_inode_flags(&btrfs_inode, BTRFS_INODE_NODATASUM |
1203 BTRFS_INODE_READONLY);
1204 btrfs_init_path(&path);
1205 trans = btrfs_start_transaction(root, 1);
1206 BUG_ON(!trans);
1208 objectid = btrfs_root_dirid(&root->root_item);
1209 ret = btrfs_find_free_objectid(trans, root, objectid, &objectid);
1210 if (ret)
1211 goto fail;
1214 * copy blocks covered by extent #0 to new positions. extent #0 is
1215 * special, we can't rely on relocate_extents_range to relocate it.
1217 for (last_byte = 0; last_byte < first_free; last_byte += sectorsize) {
1218 ret = custom_alloc_extent(root, sectorsize, 0, &key);
1219 if (ret)
1220 goto fail;
1221 ret = copy_disk_extent(root, key.objectid, last_byte,
1222 sectorsize);
1223 if (ret)
1224 goto fail;
1225 ret = record_file_extent(trans, root, objectid,
1226 &btrfs_inode, last_byte,
1227 key.objectid, sectorsize, 0);
1228 if (ret)
1229 goto fail;
1232 while(1) {
1233 key.objectid = last_byte;
1234 key.offset = 0;
1235 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
1236 ret = btrfs_search_slot(trans, fs_info->extent_root,
1237 &key, &path, 0, 0);
1238 if (ret < 0)
1239 goto fail;
1240 next:
1241 leaf = path.nodes[0];
1242 if (path.slots[0] >= btrfs_header_nritems(leaf)) {
1243 ret = btrfs_next_leaf(extent_root, &path);
1244 if (ret < 0)
1245 goto fail;
1246 if (ret > 0)
1247 break;
1248 leaf = path.nodes[0];
1250 btrfs_item_key_to_cpu(leaf, &key, path.slots[0]);
1251 if (last_byte > key.objectid ||
1252 key.type != BTRFS_EXTENT_ITEM_KEY) {
1253 path.slots[0]++;
1254 goto next;
1257 * Check backref to distinguish extent items for normal
1258 * files (files that correspond to files in Ext2fs) from
1259 * extent items for ctree blocks.
1261 bytenr = key.objectid;
1262 num_bytes = key.offset;
1263 file_extent = 0;
1264 while (1) {
1265 if (path.slots[0] >= btrfs_header_nritems(leaf)) {
1266 ret = btrfs_next_leaf(extent_root, &path);
1267 if (ret > 0)
1268 break;
1269 if (ret < 0)
1270 goto fail;
1271 leaf = path.nodes[0];
1273 btrfs_item_key_to_cpu(leaf, &key, path.slots[0]);
1274 if (key.objectid != bytenr)
1275 break;
1276 if (key.type != BTRFS_EXTENT_REF_KEY) {
1277 path.slots[0]++;
1278 continue;
1280 ref_item = btrfs_item_ptr(leaf, path.slots[0],
1281 struct btrfs_extent_ref);
1282 ref_root = btrfs_ref_root(leaf, ref_item);
1283 ref_owner = btrfs_ref_objectid(leaf, ref_item);
1284 if ((ref_root == BTRFS_FS_TREE_OBJECTID) &&
1285 (ref_owner >= BTRFS_FIRST_FREE_OBJECTID)) {
1286 file_extent = 1;
1287 break;
1289 path.slots[0]++;
1291 if (!file_extent)
1292 goto next;
1294 if (bytenr > last_byte) {
1295 ret = create_image_file_range(trans, root, objectid,
1296 &btrfs_inode, last_byte,
1297 bytenr, ext2_fs);
1298 if (ret)
1299 goto fail;
1301 ret = record_file_extent(trans, root, objectid, &btrfs_inode,
1302 bytenr, bytenr, num_bytes, 0);
1303 if (ret)
1304 goto fail;
1305 last_byte = bytenr + num_bytes;
1306 btrfs_release_path(extent_root, &path);
1308 if (trans->blocks_used >= 4096) {
1309 ret = btrfs_commit_transaction(trans, root);
1310 BUG_ON(ret);
1311 trans = btrfs_start_transaction(root, 1);
1312 BUG_ON(!trans);
1315 btrfs_release_path(root, &path);
1316 if (total_bytes > last_byte) {
1317 ret = create_image_file_range(trans, root, objectid,
1318 &btrfs_inode, last_byte,
1319 total_bytes, ext2_fs);
1320 if (ret)
1321 goto fail;
1324 ret = btrfs_insert_inode(trans, root, objectid, &btrfs_inode);
1325 if (ret)
1326 goto fail;
1328 location.objectid = objectid;
1329 location.offset = 0;
1330 btrfs_set_key_type(&location, BTRFS_INODE_ITEM_KEY);
1331 ret = btrfs_insert_dir_item(trans, root, name, strlen(name),
1332 btrfs_root_dirid(&root->root_item),
1333 &location, EXT2_FT_REG_FILE);
1334 if (ret)
1335 goto fail;
1336 ret = btrfs_insert_inode_ref(trans, root, name, strlen(name),
1337 objectid,
1338 btrfs_root_dirid(&root->root_item), 0);
1339 if (ret)
1340 goto fail;
1341 location.objectid = btrfs_root_dirid(&root->root_item);
1342 location.offset = 0;
1343 btrfs_set_key_type(&location, BTRFS_INODE_ITEM_KEY);
1344 ret = btrfs_lookup_inode(trans, root, &path, &location, 1);
1345 if (ret)
1346 goto fail;
1347 leaf = path.nodes[0];
1348 inode_item = btrfs_item_ptr(leaf, path.slots[0],
1349 struct btrfs_inode_item);
1350 btrfs_set_inode_size(leaf, inode_item, strlen(name) * 2 +
1351 btrfs_inode_size(leaf, inode_item));
1352 btrfs_mark_buffer_dirty(leaf);
1353 btrfs_release_path(root, &path);
1354 ret = btrfs_commit_transaction(trans, root);
1355 BUG_ON(ret);
1356 fail:
1357 btrfs_release_path(root, &path);
1358 return ret;
1361 struct btrfs_root *create_subvol(struct btrfs_root *root, const char *name)
1363 int ret;
1364 u64 objectid;
1365 struct btrfs_key location;
1366 struct btrfs_root_item root_item;
1367 struct btrfs_trans_handle *trans;
1368 struct btrfs_fs_info *fs_info = root->fs_info;
1369 struct btrfs_root *tree_root = fs_info->tree_root;
1370 struct btrfs_root *new_root;
1371 struct extent_buffer *tmp;
1373 trans = btrfs_start_transaction(root, 1);
1374 BUG_ON(!trans);
1376 objectid = btrfs_super_root_dir(&fs_info->super_copy);
1377 ret = btrfs_find_free_objectid(trans, root, objectid, &objectid);
1378 if (ret)
1379 goto fail;
1380 ret = btrfs_copy_root(trans, root, root->node, &tmp, objectid);
1381 if (ret)
1382 goto fail;
1383 memcpy(&root_item, &root->root_item, sizeof(root_item));
1384 btrfs_set_root_bytenr(&root_item, tmp->start);
1385 btrfs_set_root_level(&root_item, btrfs_header_level(tmp));
1386 free_extent_buffer(tmp);
1388 location.objectid = objectid;
1389 location.offset = 1;
1390 btrfs_set_key_type(&location, BTRFS_ROOT_ITEM_KEY);
1391 ret = btrfs_insert_root(trans, root->fs_info->tree_root,
1392 &location, &root_item);
1393 if (ret)
1394 goto fail;
1395 location.offset = (u64)-1;
1396 ret = btrfs_insert_dir_item(trans, tree_root, name, strlen(name),
1397 btrfs_super_root_dir(&fs_info->super_copy),
1398 &location, BTRFS_FT_DIR);
1399 if (ret)
1400 goto fail;
1401 ret = btrfs_insert_inode_ref(trans, tree_root, name, strlen(name),
1402 objectid,
1403 btrfs_super_root_dir(&fs_info->super_copy),
1405 if (ret)
1406 goto fail;
1407 ret = btrfs_commit_transaction(trans, root);
1408 BUG_ON(ret);
1409 new_root = btrfs_read_fs_root(fs_info, &location);
1410 if (!new_root || IS_ERR(new_root))
1411 goto fail;
1412 trans = btrfs_start_transaction(new_root, 1);
1413 BUG_ON(!trans);
1414 ret = btrfs_make_root_dir(trans, new_root, BTRFS_FIRST_FREE_OBJECTID);
1415 if (ret)
1416 goto fail;
1417 ret = btrfs_commit_transaction(trans, new_root);
1418 BUG_ON(ret);
1419 return new_root;
1420 fail:
1421 return NULL;
1424 * Fixup block accounting. The initial block accounting created by
1425 * make_block_groups isn't accuracy in this case.
1427 static int fixup_block_accounting(struct btrfs_trans_handle *trans,
1428 struct btrfs_root *root)
1430 int ret;
1431 int slot;
1432 u64 start = 0;
1433 u64 bytes_used = 0;
1434 struct btrfs_path path;
1435 struct btrfs_key key;
1436 struct extent_buffer *leaf;
1437 struct btrfs_block_group_cache *cache;
1438 struct btrfs_fs_info *fs_info = root->fs_info;
1440 while(1) {
1441 cache = btrfs_lookup_block_group(fs_info, start);
1442 if (!cache)
1443 break;
1444 start = cache->key.objectid + cache->key.offset;
1445 btrfs_set_block_group_used(&cache->item, 0);
1446 cache->space_info->bytes_used = 0;
1449 btrfs_init_path(&path);
1450 key.offset = 0;
1451 key.objectid = 0;
1452 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
1453 ret = btrfs_search_slot(trans, root->fs_info->extent_root,
1454 &key, &path, 0, 0);
1455 if (ret < 0)
1456 return ret;
1457 while(1) {
1458 leaf = path.nodes[0];
1459 slot = path.slots[0];
1460 if (slot >= btrfs_header_nritems(leaf)) {
1461 ret = btrfs_next_leaf(root, &path);
1462 if (ret < 0)
1463 return ret;
1464 if (ret > 0)
1465 break;
1466 leaf = path.nodes[0];
1467 slot = path.slots[0];
1469 btrfs_item_key_to_cpu(leaf, &key, slot);
1470 if (key.type == BTRFS_EXTENT_ITEM_KEY) {
1471 bytes_used += key.offset;
1472 ret = btrfs_update_block_group(trans, root,
1473 key.objectid, key.offset, 1, 0);
1474 BUG_ON(ret);
1476 path.slots[0]++;
1478 btrfs_set_super_bytes_used(&root->fs_info->super_copy, bytes_used);
1479 btrfs_release_path(root, &path);
1480 return 0;
1483 static int create_chunk_mapping(struct btrfs_trans_handle *trans,
1484 struct btrfs_root *root)
1486 struct btrfs_fs_info *info = root->fs_info;
1487 struct btrfs_root *chunk_root = info->chunk_root;
1488 struct btrfs_root *extent_root = info->extent_root;
1489 struct btrfs_device *device;
1490 struct btrfs_block_group_cache *cache;
1491 struct btrfs_dev_extent *extent;
1492 struct extent_buffer *leaf;
1493 struct btrfs_chunk chunk;
1494 struct btrfs_key key;
1495 struct btrfs_path path;
1496 u64 cur_start;
1497 u64 total_bytes;
1498 u64 chunk_objectid;
1499 int ret;
1501 btrfs_init_path(&path);
1503 total_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy);
1504 chunk_objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
1506 BUG_ON(list_empty(&info->fs_devices->devices));
1507 device = list_entry(info->fs_devices->devices.next,
1508 struct btrfs_device, dev_list);
1509 BUG_ON(device->devid != info->fs_devices->latest_devid);
1511 /* delete device extent created by make_btrfs */
1512 key.objectid = device->devid;
1513 key.offset = 0;
1514 key.type = BTRFS_DEV_EXTENT_KEY;
1515 ret = btrfs_search_slot(trans, device->dev_root, &key, &path, -1, 1);
1516 if (ret < 0)
1517 goto err;
1519 BUG_ON(ret > 0);
1520 ret = btrfs_del_item(trans, device->dev_root, &path);
1521 if (ret)
1522 goto err;
1523 btrfs_release_path(device->dev_root, &path);
1525 /* delete chunk item created by make_btrfs */
1526 key.objectid = chunk_objectid;
1527 key.offset = 0;
1528 key.type = BTRFS_CHUNK_ITEM_KEY;
1529 ret = btrfs_search_slot(trans, chunk_root, &key, &path, -1, 1);
1530 if (ret < 0)
1531 goto err;
1533 BUG_ON(ret > 0);
1534 ret = btrfs_del_item(trans, chunk_root, &path);
1535 if (ret)
1536 goto err;
1537 btrfs_release_path(chunk_root, &path);
1539 /* for each block group, create device extent and chunk item */
1540 cur_start = 0;
1541 while (cur_start < total_bytes) {
1542 cache = btrfs_lookup_block_group(root->fs_info, cur_start);
1543 BUG_ON(!cache);
1545 /* insert device extent */
1546 key.objectid = device->devid;
1547 key.offset = cache->key.objectid;
1548 key.type = BTRFS_DEV_EXTENT_KEY;
1549 ret = btrfs_insert_empty_item(trans, device->dev_root, &path,
1550 &key, sizeof(*extent));
1551 if (ret)
1552 goto err;
1554 leaf = path.nodes[0];
1555 extent = btrfs_item_ptr(leaf, path.slots[0],
1556 struct btrfs_dev_extent);
1558 btrfs_set_dev_extent_chunk_tree(leaf, extent,
1559 chunk_root->root_key.objectid);
1560 btrfs_set_dev_extent_chunk_objectid(leaf, extent,
1561 chunk_objectid);
1562 btrfs_set_dev_extent_chunk_offset(leaf, extent,
1563 cache->key.objectid);
1564 btrfs_set_dev_extent_length(leaf, extent, cache->key.offset);
1565 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
1566 (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent),
1567 BTRFS_UUID_SIZE);
1568 btrfs_mark_buffer_dirty(leaf);
1569 btrfs_release_path(device->dev_root, &path);
1571 /* insert chunk item */
1572 btrfs_set_stack_chunk_length(&chunk, cache->key.offset);
1573 btrfs_set_stack_chunk_owner(&chunk,
1574 extent_root->root_key.objectid);
1575 btrfs_set_stack_chunk_stripe_len(&chunk, 64 * 1024);
1576 btrfs_set_stack_chunk_type(&chunk, cache->flags);
1577 btrfs_set_stack_chunk_io_align(&chunk, device->io_align);
1578 btrfs_set_stack_chunk_io_width(&chunk, device->io_width);
1579 btrfs_set_stack_chunk_sector_size(&chunk, device->sector_size);
1580 btrfs_set_stack_chunk_num_stripes(&chunk, 1);
1581 btrfs_set_stack_chunk_sub_stripes(&chunk, 0);
1582 btrfs_set_stack_stripe_devid(&chunk.stripe, device->devid);
1583 btrfs_set_stack_stripe_offset(&chunk.stripe,
1584 cache->key.objectid);
1585 memcpy(&chunk.stripe.dev_uuid, device->uuid, BTRFS_UUID_SIZE);
1587 key.objectid = chunk_objectid;
1588 key.offset = cache->key.objectid;
1589 key.type = BTRFS_CHUNK_ITEM_KEY;
1591 ret = btrfs_insert_item(trans, chunk_root, &key, &chunk,
1592 btrfs_chunk_item_size(1));
1593 if (ret)
1594 goto err;
1596 cur_start = cache->key.objectid + cache->key.offset;
1599 device->bytes_used = total_bytes;
1600 ret = btrfs_update_device(trans, device);
1601 err:
1602 btrfs_release_path(device->dev_root, &path);
1603 return ret;
1606 static int init_btrfs(struct btrfs_root *root)
1608 int ret;
1609 struct btrfs_key location;
1610 struct btrfs_trans_handle *trans;
1611 struct btrfs_fs_info *fs_info = root->fs_info;
1613 trans = btrfs_start_transaction(root, 1);
1614 BUG_ON(!trans);
1615 ret = btrfs_make_block_groups(trans, root);
1616 if (ret)
1617 goto err;
1618 ret = fixup_block_accounting(trans, root);
1619 if (ret)
1620 goto err;
1621 ret = create_chunk_mapping(trans, root);
1622 if (ret)
1623 goto err;
1624 ret = btrfs_make_root_dir(trans, fs_info->tree_root,
1625 BTRFS_ROOT_TREE_DIR_OBJECTID);
1626 if (ret)
1627 goto err;
1628 memcpy(&location, &root->root_key, sizeof(location));
1629 location.offset = (u64)-1;
1630 ret = btrfs_insert_dir_item(trans, fs_info->tree_root, "default", 7,
1631 btrfs_super_root_dir(&fs_info->super_copy),
1632 &location, BTRFS_FT_DIR);
1633 if (ret)
1634 goto err;
1635 ret = btrfs_insert_inode_ref(trans, fs_info->tree_root, "default", 7,
1636 location.objectid,
1637 btrfs_super_root_dir(&fs_info->super_copy), 0);
1638 if (ret)
1639 goto err;
1640 btrfs_set_root_dirid(&fs_info->fs_root->root_item,
1641 BTRFS_FIRST_FREE_OBJECTID);
1642 ret = btrfs_commit_transaction(trans, root);
1643 BUG_ON(ret);
1644 err:
1645 return ret;
1649 * Migrate super block to it's default position and zero 0 ~ 16k
1651 static int migrate_super_block(int fd, u64 old_bytenr, u32 sectorsize)
1653 int ret;
1654 struct extent_buffer *buf;
1655 struct btrfs_super_block *super;
1656 u32 len;
1657 u32 bytenr;
1659 BUG_ON(sectorsize < sizeof(*super));
1660 buf = malloc(sizeof(*buf) + sectorsize);
1661 if (!buf)
1662 return -ENOMEM;
1664 buf->len = sectorsize;
1665 ret = pread(fd, buf->data, sectorsize, old_bytenr);
1666 if (ret != sectorsize)
1667 goto fail;
1669 super = (struct btrfs_super_block *)buf->data;
1670 BUG_ON(btrfs_super_bytenr(super) != old_bytenr);
1671 btrfs_set_super_bytenr(super, BTRFS_SUPER_INFO_OFFSET);
1673 csum_tree_block(NULL, buf, 0);
1674 ret = pwrite(fd, buf->data, sectorsize, BTRFS_SUPER_INFO_OFFSET);
1675 if (ret != sectorsize)
1676 goto fail;
1678 ret = fsync(fd);
1679 if (ret)
1680 goto fail;
1682 memset(buf->data, 0, sectorsize);
1683 for (bytenr = 0; bytenr < BTRFS_SUPER_INFO_OFFSET; ) {
1684 len = BTRFS_SUPER_INFO_OFFSET - bytenr;
1685 if (len > sectorsize)
1686 len = sectorsize;
1687 ret = pwrite(fd, buf->data, len, bytenr);
1688 if (ret != len) {
1689 fprintf(stderr, "unable to zero fill device\n");
1690 break;
1692 bytenr += len;
1694 ret = 0;
1695 fsync(fd);
1696 fail:
1697 free(buf);
1698 if (ret > 0)
1699 ret = -1;
1700 return ret;
1703 static int prepare_system_chunk_sb(struct btrfs_super_block *super)
1705 struct btrfs_chunk *chunk;
1706 struct btrfs_disk_key *key;
1707 u32 sectorsize = btrfs_super_sectorsize(super);
1709 key = (struct btrfs_disk_key *)(super->sys_chunk_array);
1710 chunk = (struct btrfs_chunk *)(super->sys_chunk_array +
1711 sizeof(struct btrfs_disk_key));
1713 btrfs_set_disk_key_objectid(key, BTRFS_FIRST_CHUNK_TREE_OBJECTID);
1714 btrfs_set_disk_key_type(key, BTRFS_CHUNK_ITEM_KEY);
1715 btrfs_set_disk_key_offset(key, 0);
1717 btrfs_set_stack_chunk_length(chunk, btrfs_super_total_bytes(super));
1718 btrfs_set_stack_chunk_owner(chunk, BTRFS_EXTENT_TREE_OBJECTID);
1719 btrfs_set_stack_chunk_stripe_len(chunk, 64 * 1024);
1720 btrfs_set_stack_chunk_type(chunk, BTRFS_BLOCK_GROUP_SYSTEM);
1721 btrfs_set_stack_chunk_io_align(chunk, sectorsize);
1722 btrfs_set_stack_chunk_io_width(chunk, sectorsize);
1723 btrfs_set_stack_chunk_sector_size(chunk, sectorsize);
1724 btrfs_set_stack_chunk_num_stripes(chunk, 1);
1725 btrfs_set_stack_chunk_sub_stripes(chunk, 0);
1726 chunk->stripe.devid = super->dev_item.devid;
1727 chunk->stripe.offset = cpu_to_le64(0);
1728 memcpy(chunk->stripe.dev_uuid, super->dev_item.uuid, BTRFS_UUID_SIZE);
1729 btrfs_set_super_sys_array_size(super, sizeof(*key) + sizeof(*chunk));
1730 return 0;
1733 static int prepare_system_chunk(int fd, u64 sb_bytenr, u32 sectorsize)
1735 int ret;
1736 struct extent_buffer *buf;
1737 struct btrfs_super_block *super;
1739 BUG_ON(sectorsize < sizeof(*super));
1740 buf = malloc(sizeof(*buf) + sectorsize);
1741 if (!buf)
1742 return -ENOMEM;
1744 buf->len = sectorsize;
1745 ret = pread(fd, buf->data, sectorsize, sb_bytenr);
1746 if (ret != sectorsize)
1747 goto fail;
1749 super = (struct btrfs_super_block *)buf->data;
1750 BUG_ON(btrfs_super_bytenr(super) != sb_bytenr);
1751 BUG_ON(btrfs_super_num_devices(super) != 1);
1753 ret = prepare_system_chunk_sb(super);
1754 if (ret)
1755 goto fail;
1757 csum_tree_block(NULL, buf, 0);
1758 ret = pwrite(fd, buf->data, sectorsize, sb_bytenr);
1759 if (ret != sectorsize)
1760 goto fail;
1762 ret = 0;
1763 fail:
1764 free(buf);
1765 if (ret > 0)
1766 ret = -1;
1767 return ret;
1770 static int relocate_one_reference(struct btrfs_trans_handle *trans,
1771 struct btrfs_root *root,
1772 u64 extent_start, u64 extent_size,
1773 u64 objectid, u64 offset,
1774 struct extent_io_tree *reloc_tree)
1776 struct extent_buffer *leaf;
1777 struct btrfs_file_extent_item *fi;
1778 struct btrfs_key key;
1779 struct btrfs_path path;
1780 struct btrfs_inode_item inode;
1781 struct blk_iterate_data data;
1782 u64 bytenr;
1783 u64 num_bytes;
1784 u64 cur_offset;
1785 u64 new_pos;
1786 u64 nblocks;
1787 u64 root_gen;
1788 u64 root_owner;
1789 u64 sector_end;
1790 u32 sectorsize = root->sectorsize;
1791 unsigned long ptr;
1792 int fd;
1793 int ret;
1795 btrfs_init_path(&path);
1796 ret = btrfs_lookup_file_extent(trans, root, &path,
1797 objectid, offset, -1);
1798 if (ret)
1799 goto fail;
1801 leaf = path.nodes[0];
1802 fi = btrfs_item_ptr(leaf, path.slots[0],
1803 struct btrfs_file_extent_item);
1804 /* the extent may be referenced by old snapshot */
1805 if (extent_start != btrfs_file_extent_disk_bytenr(leaf, fi) ||
1806 extent_size != btrfs_file_extent_disk_num_bytes(leaf, fi)) {
1807 ret = 1;
1808 goto fail;
1811 root_gen = btrfs_header_generation(leaf);
1812 root_owner = btrfs_header_owner(leaf);
1813 bytenr = extent_start + btrfs_file_extent_offset(leaf, fi);
1814 num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
1816 ret = btrfs_del_item(trans, root, &path);
1817 if (ret)
1818 goto fail;
1820 ret = btrfs_free_extent(trans, root, extent_start, extent_size,
1821 leaf->start, root_owner, root_gen, objectid,
1822 offset, 0);
1823 if (ret)
1824 goto fail;
1826 btrfs_release_path(root, &path);
1828 key.objectid = objectid;
1829 key.offset = 0;
1830 key.type = BTRFS_INODE_ITEM_KEY;
1831 ret = btrfs_lookup_inode(trans, root, &path, &key, 0);
1832 if (ret)
1833 goto fail;
1835 leaf = path.nodes[0];
1836 ptr = btrfs_item_ptr_offset(leaf, path.slots[0]);
1837 read_extent_buffer(leaf, &inode, ptr, sizeof(inode));
1838 btrfs_release_path(root, &path);
1840 BUG_ON(num_bytes & (sectorsize - 1));
1841 nblocks = btrfs_stack_inode_nblocks(&inode) - num_bytes / 512;
1842 btrfs_set_stack_inode_nblocks(&inode, nblocks);
1844 data = (struct blk_iterate_data) {
1845 .trans = trans,
1846 .root = root,
1847 .inode = &inode,
1848 .objectid = objectid,
1849 .first_block = offset / sectorsize,
1850 .disk_block = 0,
1851 .num_blocks = 0,
1852 .boundary = (u64)-1,
1853 .checksum = 0,
1854 .errcode = 0,
1857 cur_offset = offset;
1858 while (num_bytes > 0) {
1859 sector_end = bytenr + sectorsize - 1;
1860 if (test_range_bit(reloc_tree, bytenr, sector_end,
1861 EXTENT_LOCKED, 1)) {
1862 ret = get_state_private(reloc_tree, bytenr, &new_pos);
1863 BUG_ON(ret);
1864 } else {
1865 ret = custom_alloc_extent(root, sectorsize, 0, &key);
1866 if (ret)
1867 goto fail;
1868 new_pos = key.objectid;
1870 if (cur_offset == offset) {
1871 fd = root->fs_info->fs_devices->latest_bdev;
1872 readahead(fd, bytenr, num_bytes);
1874 ret = copy_disk_extent(root, new_pos, bytenr,
1875 sectorsize);
1876 if (ret)
1877 goto fail;
1878 ret = set_extent_bits(reloc_tree, bytenr, sector_end,
1879 EXTENT_LOCKED, GFP_NOFS);
1880 BUG_ON(ret);
1881 ret = set_state_private(reloc_tree, bytenr, new_pos);
1882 BUG_ON(ret);
1885 ret = block_iterate_proc(NULL, new_pos / sectorsize,
1886 cur_offset / sectorsize, &data);
1887 if (ret & BLOCK_ABORT) {
1888 ret = data.errcode;
1889 goto fail;
1892 cur_offset += sectorsize;
1893 bytenr += sectorsize;
1894 num_bytes -= sectorsize;
1897 if (data.num_blocks > 0) {
1898 ret = record_file_blocks(trans, root, objectid, &inode,
1899 data.first_block, data.disk_block,
1900 data.num_blocks, 0);
1901 if (ret)
1902 goto fail;
1905 key.objectid = objectid;
1906 key.offset = 0;
1907 key.type = BTRFS_INODE_ITEM_KEY;
1908 ret = btrfs_lookup_inode(trans, root, &path, &key, 1);
1909 if (ret)
1910 goto fail;
1912 leaf = path.nodes[0];
1913 ptr = btrfs_item_ptr_offset(leaf, path.slots[0]);
1914 write_extent_buffer(leaf, &inode, ptr, sizeof(inode));
1915 btrfs_mark_buffer_dirty(leaf);
1916 btrfs_release_path(root, &path);
1918 fail:
1919 btrfs_release_path(root, &path);
1920 return ret;
1923 static int relocate_extents_range(struct btrfs_root *fs_root,
1924 struct btrfs_root *ext2_root,
1925 u64 start_byte, u64 end_byte)
1927 struct btrfs_fs_info *info = fs_root->fs_info;
1928 struct btrfs_root *extent_root = info->extent_root;
1929 struct btrfs_root *cur_root;
1930 struct btrfs_trans_handle *trans;
1931 struct btrfs_extent_ref *ref_item;
1932 struct extent_buffer *leaf;
1933 struct btrfs_key key;
1934 struct btrfs_path path;
1935 struct extent_io_tree reloc_tree;
1936 u64 cur_byte;
1937 u64 num_bytes;
1938 u64 ref_root;
1939 u64 ref_owner;
1940 u64 ref_offset;
1941 u64 num_refs;
1942 int pass = 0;
1943 int ret;
1944 int found;
1946 btrfs_init_path(&path);
1947 extent_io_tree_init(&reloc_tree);
1948 again:
1949 cur_root = (pass % 2 == 0) ? ext2_root : fs_root;
1950 num_refs = 0;
1952 trans = btrfs_start_transaction(cur_root, 1);
1953 BUG_ON(!trans);
1955 cur_byte = start_byte;
1956 while (1) {
1957 key.objectid = cur_byte;
1958 key.offset = 0;
1959 key.type = BTRFS_EXTENT_ITEM_KEY;
1960 ret = btrfs_search_slot(trans, extent_root,
1961 &key, &path, 0, 0);
1962 if (ret < 0)
1963 goto fail;
1964 next:
1965 leaf = path.nodes[0];
1966 if (path.slots[0] >= btrfs_header_nritems(leaf)) {
1967 ret = btrfs_next_leaf(extent_root, &path);
1968 if (ret < 0)
1969 goto fail;
1970 if (ret > 0)
1971 break;
1972 leaf = path.nodes[0];
1975 btrfs_item_key_to_cpu(leaf, &key, path.slots[0]);
1976 if (key.objectid < cur_byte ||
1977 key.type != BTRFS_EXTENT_ITEM_KEY) {
1978 path.slots[0]++;
1979 goto next;
1981 if (key.objectid >= end_byte)
1982 break;
1984 cur_byte = key.objectid;
1985 num_bytes = key.offset;
1986 found = 0;
1987 while (1) {
1988 if (path.slots[0] >= btrfs_header_nritems(leaf)) {
1989 ret = btrfs_next_leaf(extent_root, &path);
1990 if (ret > 0)
1991 break;
1992 if (ret < 0)
1993 goto fail;
1994 leaf = path.nodes[0];
1997 btrfs_item_key_to_cpu(leaf, &key, path.slots[0]);
1998 if (key.objectid != cur_byte)
1999 break;
2000 if (key.type != BTRFS_EXTENT_REF_KEY) {
2001 path.slots[0]++;
2002 continue;
2004 ref_item = btrfs_item_ptr(leaf, path.slots[0],
2005 struct btrfs_extent_ref);
2006 ref_root = btrfs_ref_root(leaf, ref_item);
2007 ref_owner = btrfs_ref_objectid(leaf, ref_item);
2008 ref_offset = btrfs_ref_offset(leaf, ref_item);
2009 num_refs++;
2011 BUG_ON(ref_owner < BTRFS_FIRST_FREE_OBJECTID);
2012 if (ref_root == cur_root->root_key.objectid) {
2013 found = 1;
2014 break;
2016 path.slots[0]++;
2018 if (!found)
2019 goto next;
2021 ret = relocate_one_reference(trans, cur_root, cur_byte,
2022 num_bytes, ref_owner,
2023 ref_offset, &reloc_tree);
2024 if (ret < 0)
2025 goto fail;
2027 cur_byte += num_bytes;
2028 btrfs_release_path(extent_root, &path);
2030 if (trans->blocks_used >= 4096) {
2031 ret = btrfs_commit_transaction(trans, cur_root);
2032 BUG_ON(ret);
2033 trans = btrfs_start_transaction(cur_root, 1);
2034 BUG_ON(!trans);
2037 btrfs_release_path(cur_root, &path);
2039 ret = btrfs_commit_transaction(trans, cur_root);
2040 BUG_ON(ret);
2042 if (num_refs > 0 && pass++ < 4)
2043 goto again;
2045 ret = (num_refs > 0) ? -1 : 0;
2046 fail:
2047 btrfs_release_path(cur_root, &path);
2048 extent_io_tree_cleanup(&reloc_tree);
2049 return ret;
2053 * relocate data in system chunk
2055 static int cleanup_sys_chunk(struct btrfs_root *fs_root,
2056 struct btrfs_root *ext2_root)
2058 struct btrfs_block_group_cache *cache;
2059 int ret = 0;
2060 u64 offset = 0;
2061 u64 end_byte;
2063 while(1) {
2064 cache = btrfs_lookup_block_group(fs_root->fs_info, offset);
2065 if (!cache)
2066 break;
2068 end_byte = cache->key.objectid + cache->key.offset;
2069 if (cache->flags & BTRFS_BLOCK_GROUP_SYSTEM) {
2070 ret = relocate_extents_range(fs_root, ext2_root,
2071 cache->key.objectid,
2072 end_byte);
2073 if (ret)
2074 goto fail;
2076 offset = end_byte;
2078 fail:
2079 return ret;
2082 static int fixup_chunk_mapping(struct btrfs_root *root)
2084 struct btrfs_trans_handle *trans;
2085 struct btrfs_fs_info *info = root->fs_info;
2086 struct btrfs_root *chunk_root = info->chunk_root;
2087 struct extent_buffer *leaf;
2088 struct btrfs_key key;
2089 struct btrfs_path path;
2090 struct btrfs_chunk chunk;
2091 unsigned long ptr;
2092 u32 size;
2093 u64 type;
2094 int ret;
2096 btrfs_init_path(&path);
2098 trans = btrfs_start_transaction(root, 1);
2099 BUG_ON(!trans);
2102 * recow the whole chunk tree. this will move all chunk tree blocks
2103 * into system block group.
2105 memset(&key, 0, sizeof(key));
2106 while (1) {
2107 ret = btrfs_search_slot(trans, chunk_root, &key, &path, 0, 1);
2108 if (ret < 0)
2109 goto err;
2111 ret = btrfs_next_leaf(chunk_root, &path);
2112 if (ret < 0)
2113 goto err;
2114 if (ret > 0)
2115 break;
2117 btrfs_item_key_to_cpu(path.nodes[0], &key, path.slots[0]);
2118 btrfs_release_path(chunk_root, &path);
2120 btrfs_release_path(chunk_root, &path);
2122 /* fixup the system chunk array in super block */
2123 btrfs_set_super_sys_array_size(&info->super_copy, 0);
2125 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2126 key.offset = 0;
2127 key.type = BTRFS_CHUNK_ITEM_KEY;
2129 ret = btrfs_search_slot(trans, chunk_root, &key, &path, 0, 0);
2130 if (ret < 0)
2131 goto err;
2132 BUG_ON(ret != 0);
2133 while(1) {
2134 leaf = path.nodes[0];
2135 if (path.slots[0] >= btrfs_header_nritems(leaf)) {
2136 ret = btrfs_next_leaf(chunk_root, &path);
2137 if (ret < 0)
2138 goto err;
2139 if (ret > 0)
2140 break;
2141 leaf = path.nodes[0];
2143 btrfs_item_key_to_cpu(leaf, &key, path.slots[0]);
2144 if (key.type != BTRFS_CHUNK_ITEM_KEY)
2145 goto next;
2147 ptr = btrfs_item_ptr_offset(leaf, path.slots[0]);
2148 size = btrfs_item_size_nr(leaf, path.slots[0]);
2149 BUG_ON(size != sizeof(chunk));
2150 read_extent_buffer(leaf, &chunk, ptr, size);
2151 type = btrfs_stack_chunk_type(&chunk);
2153 if (!(type & BTRFS_BLOCK_GROUP_SYSTEM))
2154 goto next;
2156 ret = btrfs_add_system_chunk(trans, chunk_root, &key,
2157 &chunk, size);
2158 if (ret)
2159 goto err;
2160 next:
2161 path.slots[0]++;
2164 ret = btrfs_commit_transaction(trans, root);
2165 BUG_ON(ret);
2166 err:
2167 btrfs_release_path(chunk_root, &path);
2168 return ret;
2171 int do_convert(const char *devname, int datacsum, int packing, int noxattr)
2173 int i, fd, ret;
2174 u32 blocksize;
2175 u64 blocks[6];
2176 u64 total_bytes;
2177 u64 super_bytenr;
2178 ext2_filsys ext2_fs;
2179 struct btrfs_root *root;
2180 struct btrfs_root *ext2_root;
2182 ret = open_ext2fs(devname, &ext2_fs);
2183 if (ret) {
2184 fprintf(stderr, "unable to open the Ext2fs\n");
2185 goto fail;
2187 blocksize = ext2_fs->blocksize;
2188 total_bytes = (u64)ext2_fs->super->s_blocks_count * blocksize;
2189 if (blocksize < 4096) {
2190 fprintf(stderr, "block size is too small\n");
2191 goto fail;
2193 if (!(ext2_fs->super->s_feature_incompat &
2194 EXT2_FEATURE_INCOMPAT_FILETYPE)) {
2195 fprintf(stderr, "filetype feature is missing\n");
2196 goto fail;
2198 for (i = 0; i < 6; i++) {
2199 ret = ext2_alloc_block(ext2_fs, 0, blocks + i);
2200 if (ret) {
2201 fprintf(stderr, "not enough free space\n");
2202 goto fail;
2204 blocks[i] *= blocksize;
2206 super_bytenr = blocks[0];
2207 fd = open(devname, O_RDWR);
2208 if (fd < 0) {
2209 fprintf(stderr, "unable to open %s\n", devname);
2210 goto fail;
2212 ret = make_btrfs(fd, devname, ext2_fs->super->s_volume_name,
2213 blocks, total_bytes, blocksize, blocksize,
2214 blocksize, blocksize);
2215 if (ret) {
2216 fprintf(stderr, "unable to create initial ctree\n");
2217 goto fail;
2219 /* create a system chunk that maps the whole device */
2220 ret = prepare_system_chunk(fd, super_bytenr, blocksize);
2221 if (ret) {
2222 fprintf(stderr, "unable to update system chunk\n");
2223 goto fail;
2225 root = open_ctree_fd(fd, devname, super_bytenr, O_RDWR);
2226 if (!root) {
2227 fprintf(stderr, "unable to open ctree\n");
2228 goto fail;
2230 ret = cache_free_extents(root, ext2_fs);
2231 if (ret) {
2232 fprintf(stderr, "error during cache_free_extents %d\n", ret);
2233 goto fail;
2235 root->fs_info->extent_ops = &extent_ops;
2236 /* recover block allocation bitmap */
2237 for (i = 0; i < 6; i++) {
2238 blocks[i] /= blocksize;
2239 ext2_free_block(ext2_fs, blocks[i]);
2241 ret = init_btrfs(root);
2242 if (ret) {
2243 fprintf(stderr, "unable to setup the root tree\n");
2244 goto fail;
2246 ext2_root = create_subvol(root, "ext2_saved");
2247 if (!ext2_root) {
2248 fprintf(stderr, "unable to create subvol\n");
2249 goto fail;
2251 printf("creating btrfs metadata.\n");
2252 ret = copy_inodes(root, ext2_fs, datacsum, packing, noxattr);
2253 if (ret) {
2254 fprintf(stderr, "error during copy_inodes %d\n", ret);
2255 goto fail;
2257 printf("creating ext2fs image file.\n");
2258 ret = create_ext2_image(ext2_root, ext2_fs, "image");
2259 if (ret) {
2260 fprintf(stderr, "error during create_ext2_image %d\n", ret);
2261 goto fail;
2263 printf("cleaning up system chunk.\n");
2264 ret = cleanup_sys_chunk(root, ext2_root);
2265 if (ret) {
2266 fprintf(stderr, "error during cleanup_sys_chunk %d\n", ret);
2267 goto fail;
2269 btrfs_free_fs_root(ext2_root->fs_info, ext2_root);
2270 ret = close_ctree(root);
2271 if (ret) {
2272 fprintf(stderr, "error during close_ctree %d\n", ret);
2273 goto fail;
2275 close_ext2fs(ext2_fs);
2278 * If this step succeed, we get a mountable btrfs. Otherwise
2279 * the ext2fs is left unchanged.
2281 ret = migrate_super_block(fd, super_bytenr, blocksize);
2282 if (ret) {
2283 fprintf(stderr, "unable to migrate super block\n");
2284 goto fail;
2287 root = open_ctree_fd(fd, devname, 0, O_RDWR);
2288 if (!root) {
2289 fprintf(stderr, "unable to open ctree\n");
2290 goto fail;
2292 /* move chunk tree into system chunk. */
2293 ret = fixup_chunk_mapping(root);
2294 if (ret) {
2295 fprintf(stderr, "error during fixup_chunk_tree\n");
2296 goto fail;
2298 ret = close_ctree(root);
2299 close(fd);
2301 printf("conversion complete.\n");
2302 return 0;
2303 fail:
2304 fprintf(stderr, "conversion aborted.\n");
2305 return -1;
2308 static int may_rollback(struct btrfs_root *root)
2310 struct btrfs_fs_info *info = root->fs_info;
2311 struct btrfs_multi_bio *multi = NULL;
2312 u64 bytenr;
2313 u64 length;
2314 u64 physical;
2315 u64 total_bytes;
2316 int num_stripes;
2317 int ret;
2319 if (btrfs_super_num_devices(&info->super_copy) != 1)
2320 goto fail;
2322 bytenr = BTRFS_SUPER_INFO_OFFSET;
2323 total_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy);
2325 while (1) {
2326 ret = btrfs_map_block(&info->mapping_tree, WRITE, bytenr,
2327 &length, &multi, 0);
2328 if (ret)
2329 goto fail;
2331 num_stripes = multi->num_stripes;
2332 physical = multi->stripes[0].physical;
2333 kfree(multi);
2335 if (num_stripes != 1 || physical != bytenr)
2336 goto fail;
2338 bytenr += length;
2339 if (bytenr >= total_bytes)
2340 break;
2342 return 0;
2343 fail:
2344 return -1;
2347 int do_rollback(const char *devname, int force)
2349 int fd;
2350 int ret;
2351 struct btrfs_root *root;
2352 struct btrfs_root *ext2_root;
2353 struct btrfs_root *chunk_root;
2354 struct btrfs_dir_item *dir;
2355 struct btrfs_inode_item *inode;
2356 struct btrfs_file_extent_item *fi;
2357 struct btrfs_trans_handle *trans;
2358 struct extent_buffer *leaf;
2359 struct btrfs_block_group_cache *cache1;
2360 struct btrfs_block_group_cache *cache2;
2361 struct btrfs_key key;
2362 struct btrfs_path path;
2363 struct extent_io_tree io_tree;
2364 char *buf;
2365 char *name;
2366 u64 bytenr;
2367 u64 num_bytes;
2368 u64 root_dir;
2369 u64 objectid;
2370 u64 offset;
2371 u64 start;
2372 u64 end;
2373 u64 sb_bytenr;
2374 u64 first_free;
2375 u64 total_bytes;
2376 u32 sectorsize;
2378 extent_io_tree_init(&io_tree);
2380 fd = open(devname, O_RDWR);
2381 if (fd < 0) {
2382 fprintf(stderr, "unable to open %s\n", devname);
2383 goto fail;
2385 root = open_ctree_fd(fd, devname, 0, O_RDWR);
2386 if (!root) {
2387 fprintf(stderr, "unable to open ctree\n");
2388 goto fail;
2390 ret = may_rollback(root);
2391 if (ret < 0) {
2392 fprintf(stderr, "unable to do rollback\n");
2393 goto fail;
2396 sectorsize = root->sectorsize;
2397 buf = malloc(sectorsize);
2398 if (!buf) {
2399 fprintf(stderr, "unable to allocate memory\n");
2400 goto fail;
2403 btrfs_init_path(&path);
2404 name = "ext2_saved";
2405 root_dir = btrfs_super_root_dir(&root->fs_info->super_copy);
2406 dir = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root, &path,
2407 root_dir, name, strlen(name), 0);
2408 if (!dir || IS_ERR(dir)) {
2409 fprintf(stderr, "unable to find subvol %s\n", name);
2410 goto fail;
2412 leaf = path.nodes[0];
2413 btrfs_dir_item_key_to_cpu(leaf, dir, &key);
2414 btrfs_release_path(root->fs_info->tree_root, &path);
2416 ext2_root = btrfs_read_fs_root(root->fs_info, &key);
2417 if (!ext2_root || IS_ERR(ext2_root)) {
2418 fprintf(stderr, "unable to open subvol %s\n", name);
2419 goto fail;
2422 name = "image";
2423 root_dir = btrfs_root_dirid(&root->root_item);
2424 dir = btrfs_lookup_dir_item(NULL, ext2_root, &path,
2425 root_dir, name, strlen(name), 0);
2426 if (!dir || IS_ERR(dir)) {
2427 fprintf(stderr, "unable to find file %s\n", name);
2428 goto fail;
2430 leaf = path.nodes[0];
2431 btrfs_dir_item_key_to_cpu(leaf, dir, &key);
2432 btrfs_release_path(ext2_root, &path);
2434 objectid = key.objectid;
2436 ret = btrfs_lookup_inode(NULL, ext2_root, &path, &key, 0);
2437 if (ret) {
2438 fprintf(stderr, "unable to find inode item\n");
2439 goto fail;
2441 leaf = path.nodes[0];
2442 inode = btrfs_item_ptr(leaf, path.slots[0], struct btrfs_inode_item);
2443 total_bytes = btrfs_inode_size(leaf, inode);
2444 btrfs_release_path(ext2_root, &path);
2446 key.objectid = objectid;
2447 key.offset = 0;
2448 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
2449 ret = btrfs_search_slot(NULL, ext2_root, &key, &path, 0, 0);
2450 if (ret != 0) {
2451 fprintf(stderr, "unable to find first file extent\n");
2452 btrfs_release_path(ext2_root, &path);
2453 goto fail;
2456 /* build mapping tree for the relocated blocks */
2457 for (offset = 0; offset < total_bytes; ) {
2458 leaf = path.nodes[0];
2459 if (path.slots[0] >= btrfs_header_nritems(leaf)) {
2460 ret = btrfs_next_leaf(root, &path);
2461 if (ret != 0)
2462 break;
2463 continue;
2466 btrfs_item_key_to_cpu(leaf, &key, path.slots[0]);
2467 if (key.objectid != objectid || key.offset != offset ||
2468 btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
2469 break;
2471 fi = btrfs_item_ptr(leaf, path.slots[0],
2472 struct btrfs_file_extent_item);
2473 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG)
2474 break;
2476 bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
2477 /* skip holes and direct mapped extents */
2478 if (bytenr == 0 || bytenr == offset)
2479 goto next_extent;
2481 bytenr += btrfs_file_extent_offset(leaf, fi);
2482 num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
2484 cache1 = btrfs_lookup_block_group(root->fs_info, offset);
2485 cache2 = btrfs_lookup_block_group(root->fs_info,
2486 offset + num_bytes - 1);
2487 if (!cache1 || cache1 != cache2 ||
2488 !(cache1->flags & BTRFS_BLOCK_GROUP_SYSTEM))
2489 break;
2491 set_extent_bits(&io_tree, offset, offset + num_bytes - 1,
2492 EXTENT_LOCKED, GFP_NOFS);
2493 set_state_private(&io_tree, offset, bytenr);
2494 next_extent:
2495 offset += btrfs_file_extent_num_bytes(leaf, fi);
2496 path.slots[0]++;
2498 btrfs_release_path(ext2_root, &path);
2499 btrfs_free_fs_root(ext2_root->fs_info, ext2_root);
2501 if (offset < total_bytes) {
2502 fprintf(stderr, "unable to build extent mapping\n");
2503 goto fail;
2506 first_free = BTRFS_SUPER_INFO_OFFSET + 2 * sectorsize - 1;
2507 first_free &= ~((u64)sectorsize - 1);
2508 /* backup for extent #0 should exist */
2509 if(!test_range_bit(&io_tree, 0, first_free - 1, EXTENT_LOCKED, 1)) {
2510 fprintf(stderr, "no backup for the first extent\n");
2511 goto fail;
2513 /* force no allocation from system block group */
2514 root->fs_info->system_allocs = -1;
2515 trans = btrfs_start_transaction(root, 1);
2516 BUG_ON(!trans);
2518 * recow the whole chunk tree, this will remove all chunk tree blocks
2519 * from system block group
2521 chunk_root = root->fs_info->chunk_root;
2522 memset(&key, 0, sizeof(key));
2523 while (1) {
2524 ret = btrfs_search_slot(trans, chunk_root, &key, &path, 0, 1);
2525 if (ret < 0)
2526 break;
2528 ret = btrfs_next_leaf(chunk_root, &path);
2529 if (ret)
2530 break;
2532 btrfs_item_key_to_cpu(path.nodes[0], &key, path.slots[0]);
2533 btrfs_release_path(chunk_root, &path);
2535 btrfs_release_path(chunk_root, &path);
2537 offset = 0;
2538 num_bytes = 0;
2539 while(1) {
2540 cache1 = btrfs_lookup_block_group(root->fs_info, offset);
2541 if (!cache1)
2542 break;
2544 if (cache1->flags & BTRFS_BLOCK_GROUP_SYSTEM)
2545 num_bytes += btrfs_block_group_used(&cache1->item);
2547 offset = cache1->key.objectid + cache1->key.offset;
2549 /* only extent #0 left in system block group? */
2550 if (num_bytes > first_free) {
2551 fprintf(stderr, "unable to empty system block group\n");
2552 goto fail;
2554 /* create a system chunk that maps the whole device */
2555 ret = prepare_system_chunk_sb(&root->fs_info->super_copy);
2556 if (ret) {
2557 fprintf(stderr, "unable to update system chunk\n");
2558 goto fail;
2561 ret = btrfs_commit_transaction(trans, root);
2562 BUG_ON(ret);
2564 ret = close_ctree(root);
2565 if (ret) {
2566 fprintf(stderr, "error during close_ctree %d\n", ret);
2567 goto fail;
2570 sb_bytenr = (u64)-1;
2571 /* copy all relocated blocks back */
2572 while(1) {
2573 ret = find_first_extent_bit(&io_tree, 0, &start, &end,
2574 EXTENT_LOCKED);
2575 if (ret)
2576 break;
2578 ret = get_state_private(&io_tree, start, &bytenr);
2579 BUG_ON(ret);
2581 clear_extent_bits(&io_tree, start, end, EXTENT_LOCKED,
2582 GFP_NOFS);
2584 while (start <= end) {
2585 if (start == BTRFS_SUPER_INFO_OFFSET) {
2586 sb_bytenr = bytenr;
2587 goto next_sector;
2589 ret = pread(fd, buf, sectorsize, bytenr);
2590 if (ret < 0) {
2591 fprintf(stderr, "error during pread %d\n", ret);
2592 goto fail;
2594 BUG_ON(ret != sectorsize);
2595 ret = pwrite(fd, buf, sectorsize, start);
2596 if (ret < 0) {
2597 fprintf(stderr, "error during pwrite %d\n", ret);
2598 goto fail;
2600 BUG_ON(ret != sectorsize);
2601 next_sector:
2602 start += sectorsize;
2603 bytenr += sectorsize;
2607 ret = fsync(fd);
2608 if (ret) {
2609 fprintf(stderr, "error during fsync %d\n", ret);
2610 goto fail;
2613 * finally, overwrite btrfs super block.
2615 ret = pread(fd, buf, sectorsize, sb_bytenr);
2616 if (ret < 0) {
2617 fprintf(stderr, "error during pread %d\n", ret);
2618 goto fail;
2620 BUG_ON(ret != sectorsize);
2621 ret = pwrite(fd, buf, sectorsize, BTRFS_SUPER_INFO_OFFSET);
2622 if (ret < 0) {
2623 fprintf(stderr, "error during pwrite %d\n", ret);
2624 goto fail;
2626 BUG_ON(ret != sectorsize);
2627 ret = fsync(fd);
2628 if (ret) {
2629 fprintf(stderr, "error during fsync %d\n", ret);
2630 goto fail;
2633 close(fd);
2634 free(buf);
2635 extent_io_tree_cleanup(&io_tree);
2636 printf("rollback complete.\n");
2637 return 0;
2638 fail:
2639 fprintf(stderr, "rollback aborted.\n");
2640 return -1;
2643 static void print_usage(void)
2645 printf("usage: btrfs-convert [-d] [-i] [-n] [-r] device\n");
2646 printf("\t-d disable data checksum\n");
2647 printf("\t-i ignore xattrs and ACLs\n");
2648 printf("\t-n disable packing of small files\n");
2649 printf("\t-r roll back to ext2fs\n");
2652 int main(int argc, char *argv[])
2654 int ret;
2655 int packing = 1;
2656 int noxattr = 0;
2657 int datacsum = 1;
2658 int rollback = 0;
2659 char *file;
2660 while(1) {
2661 int c = getopt(argc, argv, "dinr");
2662 if (c < 0)
2663 break;
2664 switch(c) {
2665 case 'd':
2666 datacsum = 0;
2667 break;
2668 case 'i':
2669 noxattr = 1;
2670 break;
2671 case 'n':
2672 packing = 0;
2673 break;
2674 case 'r':
2675 rollback = 1;
2676 break;
2677 default:
2678 print_usage();
2679 return 1;
2682 argc = argc - optind;
2683 if (argc != 1) {
2684 print_usage();
2685 return 1;
2688 file = argv[optind];
2689 if (check_mounted(file)) {
2690 fprintf(stderr, "%s is mounted\n", file);
2691 return 1;
2694 if (rollback) {
2695 ret = do_rollback(file, 0);
2696 } else {
2697 ret = do_convert(file, datacsum, packing, noxattr);
2699 if (ret)
2700 return 1;
2701 return 0;