Btrfsck: add the ability to prune corrupt extent allocation tree blocks
[btrfs-progs-unstable/devel.git] / mkfs.c
blobd3f45bdfc9b8c8e117ae1a6f132c65a81cc8a615
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 500
20 #define _GNU_SOURCE
22 #ifndef __CHECKER__
23 #include <sys/ioctl.h>
24 #include <sys/mount.h>
25 #include "ioctl.h"
26 #endif
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <sys/types.h>
31 #include <sys/stat.h>
32 #include <sys/dir.h>
33 #include <fcntl.h>
34 #include <unistd.h>
35 #include <getopt.h>
36 #include <uuid/uuid.h>
37 #include <linux/fs.h>
38 #include <ctype.h>
39 #include <attr/xattr.h>
40 #include "kerncompat.h"
41 #include "ctree.h"
42 #include "disk-io.h"
43 #include "volumes.h"
44 #include "transaction.h"
45 #include "utils.h"
46 #include "version.h"
48 static u64 index_cnt = 2;
50 struct directory_name_entry {
51 char *dir_name;
52 char *path;
53 ino_t inum;
54 struct list_head list;
57 static u64 parse_size(char *s)
59 int len = strlen(s);
60 char c;
61 u64 mult = 1;
63 if (!isdigit(s[len - 1])) {
64 c = tolower(s[len - 1]);
65 switch (c) {
66 case 'g':
67 mult *= 1024;
68 case 'm':
69 mult *= 1024;
70 case 'k':
71 mult *= 1024;
72 case 'b':
73 break;
74 default:
75 fprintf(stderr, "Unknown size descriptor %c\n", c);
76 exit(1);
78 s[len - 1] = '\0';
80 return atol(s) * mult;
83 static int make_root_dir(struct btrfs_root *root, int mixed)
85 struct btrfs_trans_handle *trans;
86 struct btrfs_key location;
87 u64 bytes_used;
88 u64 chunk_start = 0;
89 u64 chunk_size = 0;
90 int ret;
92 trans = btrfs_start_transaction(root, 1);
93 bytes_used = btrfs_super_bytes_used(&root->fs_info->super_copy);
95 root->fs_info->system_allocs = 1;
96 ret = btrfs_make_block_group(trans, root, bytes_used,
97 BTRFS_BLOCK_GROUP_SYSTEM,
98 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
99 0, BTRFS_MKFS_SYSTEM_GROUP_SIZE);
100 BUG_ON(ret);
102 if (mixed) {
103 ret = btrfs_alloc_chunk(trans, root->fs_info->extent_root,
104 &chunk_start, &chunk_size,
105 BTRFS_BLOCK_GROUP_METADATA |
106 BTRFS_BLOCK_GROUP_DATA);
107 BUG_ON(ret);
108 ret = btrfs_make_block_group(trans, root, 0,
109 BTRFS_BLOCK_GROUP_METADATA |
110 BTRFS_BLOCK_GROUP_DATA,
111 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
112 chunk_start, chunk_size);
113 BUG_ON(ret);
114 printf("Created a data/metadata chunk of size %llu\n", chunk_size);
115 } else {
116 ret = btrfs_alloc_chunk(trans, root->fs_info->extent_root,
117 &chunk_start, &chunk_size,
118 BTRFS_BLOCK_GROUP_METADATA);
119 BUG_ON(ret);
120 ret = btrfs_make_block_group(trans, root, 0,
121 BTRFS_BLOCK_GROUP_METADATA,
122 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
123 chunk_start, chunk_size);
124 BUG_ON(ret);
127 root->fs_info->system_allocs = 0;
128 btrfs_commit_transaction(trans, root);
129 trans = btrfs_start_transaction(root, 1);
130 BUG_ON(!trans);
132 if (!mixed) {
133 ret = btrfs_alloc_chunk(trans, root->fs_info->extent_root,
134 &chunk_start, &chunk_size,
135 BTRFS_BLOCK_GROUP_DATA);
136 BUG_ON(ret);
137 ret = btrfs_make_block_group(trans, root, 0,
138 BTRFS_BLOCK_GROUP_DATA,
139 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
140 chunk_start, chunk_size);
141 BUG_ON(ret);
144 ret = btrfs_make_root_dir(trans, root->fs_info->tree_root,
145 BTRFS_ROOT_TREE_DIR_OBJECTID);
146 if (ret)
147 goto err;
148 ret = btrfs_make_root_dir(trans, root, BTRFS_FIRST_FREE_OBJECTID);
149 if (ret)
150 goto err;
151 memcpy(&location, &root->fs_info->fs_root->root_key, sizeof(location));
152 location.offset = (u64)-1;
153 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
154 "default", 7,
155 btrfs_super_root_dir(&root->fs_info->super_copy),
156 &location, BTRFS_FT_DIR, 0);
157 if (ret)
158 goto err;
160 ret = btrfs_insert_inode_ref(trans, root->fs_info->tree_root,
161 "default", 7, location.objectid,
162 BTRFS_ROOT_TREE_DIR_OBJECTID, 0);
163 if (ret)
164 goto err;
166 btrfs_commit_transaction(trans, root);
167 err:
168 return ret;
171 static int recow_roots(struct btrfs_trans_handle *trans,
172 struct btrfs_root *root)
174 int ret;
175 struct extent_buffer *tmp;
176 struct btrfs_fs_info *info = root->fs_info;
178 ret = __btrfs_cow_block(trans, info->fs_root, info->fs_root->node,
179 NULL, 0, &tmp, 0, 0);
180 BUG_ON(ret);
181 free_extent_buffer(tmp);
183 ret = __btrfs_cow_block(trans, info->tree_root, info->tree_root->node,
184 NULL, 0, &tmp, 0, 0);
185 BUG_ON(ret);
186 free_extent_buffer(tmp);
188 ret = __btrfs_cow_block(trans, info->extent_root,
189 info->extent_root->node, NULL, 0, &tmp, 0, 0);
190 BUG_ON(ret);
191 free_extent_buffer(tmp);
193 ret = __btrfs_cow_block(trans, info->chunk_root, info->chunk_root->node,
194 NULL, 0, &tmp, 0, 0);
195 BUG_ON(ret);
196 free_extent_buffer(tmp);
199 ret = __btrfs_cow_block(trans, info->dev_root, info->dev_root->node,
200 NULL, 0, &tmp, 0, 0);
201 BUG_ON(ret);
202 free_extent_buffer(tmp);
204 ret = __btrfs_cow_block(trans, info->csum_root, info->csum_root->node,
205 NULL, 0, &tmp, 0, 0);
206 BUG_ON(ret);
207 free_extent_buffer(tmp);
209 return 0;
212 static int create_one_raid_group(struct btrfs_trans_handle *trans,
213 struct btrfs_root *root, u64 type)
215 u64 chunk_start;
216 u64 chunk_size;
217 int ret;
219 ret = btrfs_alloc_chunk(trans, root->fs_info->extent_root,
220 &chunk_start, &chunk_size, type);
221 BUG_ON(ret);
222 ret = btrfs_make_block_group(trans, root->fs_info->extent_root, 0,
223 type, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
224 chunk_start, chunk_size);
225 BUG_ON(ret);
226 return ret;
229 static int create_raid_groups(struct btrfs_trans_handle *trans,
230 struct btrfs_root *root, u64 data_profile,
231 int data_profile_opt, u64 metadata_profile,
232 int metadata_profile_opt, int mixed)
234 u64 num_devices = btrfs_super_num_devices(&root->fs_info->super_copy);
235 u64 allowed;
236 int ret;
239 * Set default profiles according to number of added devices.
240 * For mixed groups defaults are single/single.
242 if (!metadata_profile_opt && !mixed) {
243 metadata_profile = (num_devices > 1) ?
244 BTRFS_BLOCK_GROUP_RAID1 : BTRFS_BLOCK_GROUP_DUP;
246 if (!data_profile_opt && !mixed) {
247 data_profile = (num_devices > 1) ?
248 BTRFS_BLOCK_GROUP_RAID0 : 0; /* raid0 or single */
251 if (num_devices == 1)
252 allowed = BTRFS_BLOCK_GROUP_DUP;
253 else if (num_devices >= 4) {
254 allowed = BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1 |
255 BTRFS_BLOCK_GROUP_RAID10;
256 } else
257 allowed = BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1;
259 if (metadata_profile & ~allowed) {
260 fprintf(stderr, "unable to create FS with metadata "
261 "profile %llu (%llu devices)\n", metadata_profile,
262 num_devices);
263 exit(1);
265 if (data_profile & ~allowed) {
266 fprintf(stderr, "unable to create FS with data "
267 "profile %llu (%llu devices)\n", data_profile,
268 num_devices);
269 exit(1);
272 if (allowed & metadata_profile) {
273 u64 meta_flags = BTRFS_BLOCK_GROUP_METADATA;
275 ret = create_one_raid_group(trans, root,
276 BTRFS_BLOCK_GROUP_SYSTEM |
277 (allowed & metadata_profile));
278 BUG_ON(ret);
280 if (mixed)
281 meta_flags |= BTRFS_BLOCK_GROUP_DATA;
283 ret = create_one_raid_group(trans, root, meta_flags |
284 (allowed & metadata_profile));
285 BUG_ON(ret);
287 ret = recow_roots(trans, root);
288 BUG_ON(ret);
290 if (!mixed && num_devices > 1 && (allowed & data_profile)) {
291 ret = create_one_raid_group(trans, root,
292 BTRFS_BLOCK_GROUP_DATA |
293 (allowed & data_profile));
294 BUG_ON(ret);
296 return 0;
299 static int create_data_reloc_tree(struct btrfs_trans_handle *trans,
300 struct btrfs_root *root)
302 struct btrfs_key location;
303 struct btrfs_root_item root_item;
304 struct extent_buffer *tmp;
305 u64 objectid = BTRFS_DATA_RELOC_TREE_OBJECTID;
306 int ret;
308 ret = btrfs_copy_root(trans, root, root->node, &tmp, objectid);
309 BUG_ON(ret);
311 memcpy(&root_item, &root->root_item, sizeof(root_item));
312 btrfs_set_root_bytenr(&root_item, tmp->start);
313 btrfs_set_root_level(&root_item, btrfs_header_level(tmp));
314 btrfs_set_root_generation(&root_item, trans->transid);
315 free_extent_buffer(tmp);
317 location.objectid = objectid;
318 location.type = BTRFS_ROOT_ITEM_KEY;
319 location.offset = 0;
320 ret = btrfs_insert_root(trans, root->fs_info->tree_root,
321 &location, &root_item);
322 BUG_ON(ret);
323 return 0;
326 static void print_usage(void)
328 fprintf(stderr, "usage: mkfs.btrfs [options] dev [ dev ... ]\n");
329 fprintf(stderr, "options:\n");
330 fprintf(stderr, "\t -A --alloc-start the offset to start the FS\n");
331 fprintf(stderr, "\t -b --byte-count total number of bytes in the FS\n");
332 fprintf(stderr, "\t -d --data data profile, raid0, raid1, raid10 or single\n");
333 fprintf(stderr, "\t -l --leafsize size of btree leaves\n");
334 fprintf(stderr, "\t -L --label set a label\n");
335 fprintf(stderr, "\t -m --metadata metadata profile, values like data profile\n");
336 fprintf(stderr, "\t -M --mixed mix metadata and data together\n");
337 fprintf(stderr, "\t -n --nodesize size of btree nodes\n");
338 fprintf(stderr, "\t -s --sectorsize min block allocation\n");
339 fprintf(stderr, "\t -r --rootdir the source directory\n");
340 fprintf(stderr, "%s\n", BTRFS_BUILD_VERSION);
341 exit(1);
344 static void print_version(void)
346 fprintf(stderr, "mkfs.btrfs, part of %s\n", BTRFS_BUILD_VERSION);
347 exit(0);
350 static u64 parse_profile(char *s)
352 if (strcmp(s, "raid0") == 0) {
353 return BTRFS_BLOCK_GROUP_RAID0;
354 } else if (strcmp(s, "raid1") == 0) {
355 return BTRFS_BLOCK_GROUP_RAID1;
356 } else if (strcmp(s, "raid10") == 0) {
357 return BTRFS_BLOCK_GROUP_RAID10;
358 } else if (strcmp(s, "single") == 0) {
359 return 0;
360 } else {
361 fprintf(stderr, "Unknown option %s\n", s);
362 print_usage();
364 /* not reached */
365 return 0;
368 static char *parse_label(char *input)
370 int i;
371 int len = strlen(input);
373 if (len >= BTRFS_LABEL_SIZE) {
374 fprintf(stderr, "Label %s is too long (max %d)\n", input,
375 BTRFS_LABEL_SIZE - 1);
376 exit(1);
378 for (i = 0; i < len; i++) {
379 if (input[i] == '/' || input[i] == '\\') {
380 fprintf(stderr, "invalid label %s\n", input);
381 exit(1);
384 return strdup(input);
387 static struct option long_options[] = {
388 { "alloc-start", 1, NULL, 'A'},
389 { "byte-count", 1, NULL, 'b' },
390 { "leafsize", 1, NULL, 'l' },
391 { "label", 1, NULL, 'L'},
392 { "metadata", 1, NULL, 'm' },
393 { "mixed", 0, NULL, 'M' },
394 { "nodesize", 1, NULL, 'n' },
395 { "sectorsize", 1, NULL, 's' },
396 { "data", 1, NULL, 'd' },
397 { "version", 0, NULL, 'V' },
398 { "rootdir", 1, NULL, 'r' },
399 { 0, 0, 0, 0}
402 static int add_directory_items(struct btrfs_trans_handle *trans,
403 struct btrfs_root *root, u64 objectid,
404 ino_t parent_inum, const char *name,
405 struct stat *st, int *dir_index_cnt)
407 int ret;
408 int name_len;
409 struct btrfs_key location;
410 u8 filetype = 0;
412 name_len = strlen(name);
414 location.objectid = objectid;
415 location.offset = 0;
416 btrfs_set_key_type(&location, BTRFS_INODE_ITEM_KEY);
418 if (S_ISDIR(st->st_mode))
419 filetype = BTRFS_FT_DIR;
420 if (S_ISREG(st->st_mode))
421 filetype = BTRFS_FT_REG_FILE;
422 if (S_ISLNK(st->st_mode))
423 filetype = BTRFS_FT_SYMLINK;
425 ret = btrfs_insert_dir_item(trans, root, name, name_len,
426 parent_inum, &location,
427 filetype, index_cnt);
429 *dir_index_cnt = index_cnt;
430 index_cnt++;
432 return ret;
435 static int fill_inode_item(struct btrfs_trans_handle *trans,
436 struct btrfs_root *root,
437 struct btrfs_inode_item *dst, struct stat *src)
439 u64 blocks = 0;
440 u64 sectorsize = root->sectorsize;
443 * btrfs_inode_item has some reserved fields
444 * and represents on-disk inode entry, so
445 * zero everything to prevent information leak
447 memset(dst, 0, sizeof (*dst));
449 btrfs_set_stack_inode_generation(dst, trans->transid);
450 btrfs_set_stack_inode_size(dst, src->st_size);
451 btrfs_set_stack_inode_nbytes(dst, 0);
452 btrfs_set_stack_inode_block_group(dst, 0);
453 btrfs_set_stack_inode_nlink(dst, src->st_nlink);
454 btrfs_set_stack_inode_uid(dst, src->st_uid);
455 btrfs_set_stack_inode_gid(dst, src->st_gid);
456 btrfs_set_stack_inode_mode(dst, src->st_mode);
457 btrfs_set_stack_inode_rdev(dst, 0);
458 btrfs_set_stack_inode_flags(dst, 0);
459 btrfs_set_stack_timespec_sec(&dst->atime, src->st_atime);
460 btrfs_set_stack_timespec_nsec(&dst->atime, 0);
461 btrfs_set_stack_timespec_sec(&dst->ctime, src->st_ctime);
462 btrfs_set_stack_timespec_nsec(&dst->ctime, 0);
463 btrfs_set_stack_timespec_sec(&dst->mtime, src->st_mtime);
464 btrfs_set_stack_timespec_nsec(&dst->mtime, 0);
465 btrfs_set_stack_timespec_sec(&dst->otime, 0);
466 btrfs_set_stack_timespec_nsec(&dst->otime, 0);
468 if (S_ISDIR(src->st_mode)) {
469 btrfs_set_stack_inode_size(dst, 0);
470 btrfs_set_stack_inode_nlink(dst, 1);
472 if (S_ISREG(src->st_mode)) {
473 btrfs_set_stack_inode_size(dst, (u64)src->st_size);
474 if (src->st_size <= BTRFS_MAX_INLINE_DATA_SIZE(root))
475 btrfs_set_stack_inode_nbytes(dst, src->st_size);
476 else {
477 blocks = src->st_size / sectorsize;
478 if (src->st_size % sectorsize)
479 blocks += 1;
480 blocks *= sectorsize;
481 btrfs_set_stack_inode_nbytes(dst, blocks);
484 if (S_ISLNK(src->st_mode))
485 btrfs_set_stack_inode_nbytes(dst, src->st_size + 1);
487 return 0;
490 static int directory_select(const struct direct *entry)
492 if ((strncmp(entry->d_name, ".", entry->d_reclen) == 0) ||
493 (strncmp(entry->d_name, "..", entry->d_reclen) == 0))
494 return 0;
495 else
496 return 1;
499 static void free_namelist(struct direct **files, int count)
501 int i;
503 if (count < 0)
504 return;
506 for (i = 0; i < count; ++i)
507 free(files[i]);
508 free(files);
511 static u64 calculate_dir_inode_size(char *dirname)
513 int count, i;
514 struct direct **files, *cur_file;
515 u64 dir_inode_size = 0;
517 count = scandir(dirname, &files, directory_select, NULL);
519 for (i = 0; i < count; i++) {
520 cur_file = files[i];
521 dir_inode_size += strlen(cur_file->d_name);
524 free_namelist(files, count);
526 dir_inode_size *= 2;
527 return dir_inode_size;
530 static int add_inode_items(struct btrfs_trans_handle *trans,
531 struct btrfs_root *root,
532 struct stat *st, char *name,
533 u64 self_objectid, ino_t parent_inum,
534 int dir_index_cnt, struct btrfs_inode_item *inode_ret)
536 int ret;
537 struct btrfs_key inode_key;
538 struct btrfs_inode_item btrfs_inode;
539 u64 objectid;
540 u64 inode_size = 0;
541 int name_len;
543 name_len = strlen(name);
544 fill_inode_item(trans, root, &btrfs_inode, st);
545 objectid = self_objectid;
547 if (S_ISDIR(st->st_mode)) {
548 inode_size = calculate_dir_inode_size(name);
549 btrfs_set_stack_inode_size(&btrfs_inode, inode_size);
552 inode_key.objectid = objectid;
553 inode_key.offset = 0;
554 btrfs_set_key_type(&inode_key, BTRFS_INODE_ITEM_KEY);
556 ret = btrfs_insert_inode(trans, root, objectid, &btrfs_inode);
557 if (ret)
558 goto fail;
560 ret = btrfs_insert_inode_ref(trans, root, name, name_len,
561 objectid, parent_inum, dir_index_cnt);
562 if (ret)
563 goto fail;
565 *inode_ret = btrfs_inode;
566 fail:
567 return ret;
570 static int add_xattr_item(struct btrfs_trans_handle *trans,
571 struct btrfs_root *root, u64 objectid,
572 const char *file_name)
574 int ret;
575 int cur_name_len;
576 char xattr_list[XATTR_LIST_MAX];
577 char *cur_name;
578 char cur_value[XATTR_SIZE_MAX];
579 char delimiter = '\0';
580 char *next_location = xattr_list;
582 ret = llistxattr(file_name, xattr_list, XATTR_LIST_MAX);
583 if (ret < 0) {
584 if(errno == ENOTSUP)
585 return 0;
586 fprintf(stderr, "get a list of xattr failed for %s\n",
587 file_name);
588 return ret;
590 if (ret == 0)
591 return ret;
593 cur_name = strtok(xattr_list, &delimiter);
594 while (cur_name != NULL) {
595 cur_name_len = strlen(cur_name);
596 next_location += cur_name_len + 1;
598 ret = getxattr(file_name, cur_name, cur_value, XATTR_SIZE_MAX);
599 if (ret < 0) {
600 if(errno == ENOTSUP)
601 return 0;
602 fprintf(stderr, "get a xattr value failed for %s attr %s\n",
603 file_name, cur_name);
604 return ret;
607 ret = btrfs_insert_xattr_item(trans, root, cur_name,
608 cur_name_len, cur_value,
609 ret, objectid);
610 if (ret) {
611 fprintf(stderr, "insert a xattr item failed for %s\n",
612 file_name);
615 cur_name = strtok(next_location, &delimiter);
618 return ret;
620 static int custom_alloc_extent(struct btrfs_root *root, u64 num_bytes,
621 u64 hint_byte, struct btrfs_key *ins)
623 u64 start;
624 u64 end;
625 u64 last = hint_byte;
626 int ret;
627 int wrapped = 0;
628 struct btrfs_block_group_cache *cache;
630 while (1) {
631 ret = find_first_extent_bit(&root->fs_info->free_space_cache,
632 last, &start, &end, EXTENT_DIRTY);
633 if (ret) {
634 if (wrapped++ == 0) {
635 last = 0;
636 continue;
637 } else {
638 goto fail;
642 start = max(last, start);
643 last = end + 1;
644 if (last - start < num_bytes)
645 continue;
647 last = start + num_bytes;
648 if (test_range_bit(&root->fs_info->pinned_extents,
649 start, last - 1, EXTENT_DIRTY, 0))
650 continue;
652 cache = btrfs_lookup_block_group(root->fs_info, start);
653 BUG_ON(!cache);
654 if (cache->flags & BTRFS_BLOCK_GROUP_SYSTEM ||
655 last > cache->key.objectid + cache->key.offset) {
656 last = cache->key.objectid + cache->key.offset;
657 continue;
660 if (cache->flags & (BTRFS_BLOCK_GROUP_SYSTEM |
661 BTRFS_BLOCK_GROUP_METADATA)) {
662 last = cache->key.objectid + cache->key.offset;
663 continue;
666 clear_extent_dirty(&root->fs_info->free_space_cache,
667 start, start + num_bytes - 1, 0);
669 ins->objectid = start;
670 ins->offset = num_bytes;
671 ins->type = BTRFS_EXTENT_ITEM_KEY;
672 return 0;
674 fail:
675 fprintf(stderr, "not enough free space\n");
676 return -ENOSPC;
679 static int record_file_extent(struct btrfs_trans_handle *trans,
680 struct btrfs_root *root, u64 objectid,
681 struct btrfs_inode_item *inode,
682 u64 file_pos, u64 disk_bytenr,
683 u64 num_bytes)
685 int ret;
686 struct btrfs_fs_info *info = root->fs_info;
687 struct btrfs_root *extent_root = info->extent_root;
688 struct extent_buffer *leaf;
689 struct btrfs_file_extent_item *fi;
690 struct btrfs_key ins_key;
691 struct btrfs_path path;
692 struct btrfs_extent_item *ei;
694 btrfs_init_path(&path);
696 ins_key.objectid = objectid;
697 ins_key.offset = 0;
698 btrfs_set_key_type(&ins_key, BTRFS_EXTENT_DATA_KEY);
699 ret = btrfs_insert_empty_item(trans, root, &path, &ins_key,
700 sizeof(*fi));
701 if (ret)
702 goto fail;
703 leaf = path.nodes[0];
704 fi = btrfs_item_ptr(leaf, path.slots[0],
705 struct btrfs_file_extent_item);
706 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
707 btrfs_set_file_extent_type(leaf, fi, BTRFS_FILE_EXTENT_REG);
708 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
709 btrfs_set_file_extent_disk_num_bytes(leaf, fi, num_bytes);
710 btrfs_set_file_extent_offset(leaf, fi, 0);
711 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
712 btrfs_set_file_extent_ram_bytes(leaf, fi, num_bytes);
713 btrfs_set_file_extent_compression(leaf, fi, 0);
714 btrfs_set_file_extent_encryption(leaf, fi, 0);
715 btrfs_set_file_extent_other_encoding(leaf, fi, 0);
716 btrfs_mark_buffer_dirty(leaf);
718 btrfs_release_path(root, &path);
720 ins_key.objectid = disk_bytenr;
721 ins_key.offset = num_bytes;
722 ins_key.type = BTRFS_EXTENT_ITEM_KEY;
724 ret = btrfs_insert_empty_item(trans, extent_root, &path,
725 &ins_key, sizeof(*ei));
726 if (ret == 0) {
727 leaf = path.nodes[0];
728 ei = btrfs_item_ptr(leaf, path.slots[0],
729 struct btrfs_extent_item);
731 btrfs_set_extent_refs(leaf, ei, 0);
732 btrfs_set_extent_generation(leaf, ei, trans->transid);
733 btrfs_set_extent_flags(leaf, ei, BTRFS_EXTENT_FLAG_DATA);
735 btrfs_mark_buffer_dirty(leaf);
736 ret = btrfs_update_block_group(trans, root, disk_bytenr,
737 num_bytes, 1, 0);
738 if (ret)
739 goto fail;
740 } else if (ret != -EEXIST) {
741 goto fail;
744 ret = btrfs_inc_extent_ref(trans, root, disk_bytenr, num_bytes, 0,
745 root->root_key.objectid,
746 objectid, 0);
747 fail:
748 btrfs_release_path(root, &path);
749 return ret;
752 static int add_symbolic_link(struct btrfs_trans_handle *trans,
753 struct btrfs_root *root,
754 u64 objectid, const char *path_name)
756 int ret;
757 u64 sectorsize = root->sectorsize;
758 char *buf = malloc(sectorsize);
760 ret = readlink(path_name, buf, sectorsize);
761 if (ret <= 0) {
762 fprintf(stderr, "readlink failed for %s\n", path_name);
763 goto fail;
765 if (ret >= sectorsize) {
766 fprintf(stderr, "symlink too long for %s", path_name);
767 ret = -1;
768 goto fail;
771 buf[ret] = '\0'; /* readlink does not do it for us */
772 ret = btrfs_insert_inline_extent(trans, root, objectid, 0,
773 buf, ret + 1);
774 fail:
775 free(buf);
776 return ret;
779 static int add_file_items(struct btrfs_trans_handle *trans,
780 struct btrfs_root *root,
781 struct btrfs_inode_item *btrfs_inode, u64 objectid,
782 ino_t parent_inum, struct stat *st,
783 const char *path_name, int out_fd)
785 int ret = -1;
786 ssize_t ret_read;
787 u64 bytes_read = 0;
788 char *buffer = NULL;
789 struct btrfs_key key;
790 int blocks;
791 u32 sectorsize = root->sectorsize;
792 u64 first_block = 0;
793 u64 num_blocks = 0;
794 int fd;
796 fd = open(path_name, O_RDONLY);
797 if (fd == -1) {
798 fprintf(stderr, "%s open failed\n", path_name);
799 goto end;
802 blocks = st->st_size / sectorsize;
803 if (st->st_size % sectorsize)
804 blocks += 1;
806 if (st->st_size <= BTRFS_MAX_INLINE_DATA_SIZE(root)) {
807 buffer = malloc(st->st_size);
808 ret_read = pread64(fd, buffer, st->st_size, bytes_read);
809 if (ret_read == -1) {
810 fprintf(stderr, "%s read failed\n", path_name);
811 goto end;
814 ret = btrfs_insert_inline_extent(trans, root, objectid, 0,
815 buffer, st->st_size);
816 goto end;
819 ret = custom_alloc_extent(root, blocks * sectorsize, 0, &key);
820 if (ret)
821 goto end;
823 first_block = key.objectid;
824 bytes_read = 0;
825 buffer = malloc(sectorsize);
827 do {
828 memset(buffer, 0, sectorsize);
829 ret_read = pread64(fd, buffer, sectorsize, bytes_read);
830 if (ret_read == -1) {
831 fprintf(stderr, "%s read failed\n", path_name);
832 goto end;
835 ret = pwrite64(out_fd, buffer, sectorsize,
836 first_block + bytes_read);
837 if (ret != sectorsize) {
838 fprintf(stderr, "output file write failed\n");
839 goto end;
842 /* checksum for file data */
843 ret = btrfs_csum_file_block(trans, root->fs_info->csum_root,
844 first_block + (blocks * sectorsize),
845 first_block + bytes_read,
846 buffer, sectorsize);
847 if (ret) {
848 fprintf(stderr, "%s checksum failed\n", path_name);
849 goto end;
852 bytes_read += ret_read;
853 num_blocks++;
854 } while (ret_read == sectorsize);
856 if (num_blocks > 0) {
857 ret = record_file_extent(trans, root, objectid, btrfs_inode,
858 first_block, first_block,
859 blocks * sectorsize);
860 if (ret)
861 goto end;
864 end:
865 if (buffer)
866 free(buffer);
867 close(fd);
868 return ret;
871 static char *make_path(char *dir, char *name)
873 char *path;
875 path = malloc(strlen(dir) + strlen(name) + 2);
876 if (!path)
877 return NULL;
878 strcpy(path, dir);
879 if (dir[strlen(dir) - 1] != '/')
880 strcat(path, "/");
881 strcat(path, name);
882 return path;
885 static int traverse_directory(struct btrfs_trans_handle *trans,
886 struct btrfs_root *root, char *dir_name,
887 struct directory_name_entry *dir_head, int out_fd)
889 int ret = 0;
891 struct btrfs_inode_item cur_inode;
892 struct btrfs_inode_item *inode_item;
893 int count, i, dir_index_cnt;
894 struct direct **files;
895 struct stat st;
896 struct directory_name_entry *dir_entry, *parent_dir_entry;
897 struct direct *cur_file;
898 ino_t parent_inum, cur_inum;
899 ino_t highest_inum = 0;
900 char *parent_dir_name;
901 struct btrfs_path path;
902 struct extent_buffer *leaf;
903 struct btrfs_key root_dir_key;
904 u64 root_dir_inode_size = 0;
906 /* Add list for source directory */
907 dir_entry = malloc(sizeof(struct directory_name_entry));
908 dir_entry->dir_name = dir_name;
909 dir_entry->path = malloc(strlen(dir_name) + 1);
910 strcpy(dir_entry->path, dir_name);
912 parent_inum = highest_inum + BTRFS_FIRST_FREE_OBJECTID;
913 dir_entry->inum = parent_inum;
914 list_add_tail(&dir_entry->list, &dir_head->list);
916 btrfs_init_path(&path);
918 root_dir_key.objectid = btrfs_root_dirid(&root->root_item);
919 root_dir_key.offset = 0;
920 btrfs_set_key_type(&root_dir_key, BTRFS_INODE_ITEM_KEY);
921 ret = btrfs_lookup_inode(trans, root, &path, &root_dir_key, 1);
922 if (ret) {
923 fprintf(stderr, "root dir lookup error\n");
924 return -1;
927 leaf = path.nodes[0];
928 inode_item = btrfs_item_ptr(leaf, path.slots[0],
929 struct btrfs_inode_item);
931 root_dir_inode_size = calculate_dir_inode_size(dir_name);
932 btrfs_set_inode_size(leaf, inode_item, root_dir_inode_size);
933 btrfs_mark_buffer_dirty(leaf);
935 btrfs_release_path(root, &path);
937 do {
938 parent_dir_entry = list_entry(dir_head->list.next,
939 struct directory_name_entry,
940 list);
941 list_del(&parent_dir_entry->list);
943 parent_inum = parent_dir_entry->inum;
944 parent_dir_name = parent_dir_entry->dir_name;
945 if (chdir(parent_dir_entry->path)) {
946 fprintf(stderr, "chdir error for %s\n",
947 parent_dir_name);
948 goto fail_no_files;
951 count = scandir(parent_dir_entry->path, &files,
952 directory_select, NULL);
953 if (count == -1)
955 fprintf(stderr, "scandir for %s failed: %s\n",
956 parent_dir_name, strerror (errno));
957 goto fail;
960 for (i = 0; i < count; i++) {
961 cur_file = files[i];
963 if (lstat(cur_file->d_name, &st) == -1) {
964 fprintf(stderr, "lstat failed for file %s\n",
965 cur_file->d_name);
966 goto fail;
969 cur_inum = ++highest_inum + BTRFS_FIRST_FREE_OBJECTID;
970 ret = add_directory_items(trans, root,
971 cur_inum, parent_inum,
972 cur_file->d_name,
973 &st, &dir_index_cnt);
974 if (ret) {
975 fprintf(stderr, "add_directory_items failed\n");
976 goto fail;
979 ret = add_inode_items(trans, root, &st,
980 cur_file->d_name, cur_inum,
981 parent_inum, dir_index_cnt,
982 &cur_inode);
983 if (ret) {
984 fprintf(stderr, "add_inode_items failed\n");
985 goto fail;
988 ret = add_xattr_item(trans, root,
989 cur_inum, cur_file->d_name);
990 if (ret) {
991 fprintf(stderr, "add_xattr_item failed\n");
992 if(ret != -ENOTSUP)
993 goto fail;
996 if (S_ISDIR(st.st_mode)) {
997 dir_entry = malloc(sizeof(struct directory_name_entry));
998 dir_entry->dir_name = cur_file->d_name;
999 dir_entry->path = make_path(parent_dir_entry->path,
1000 cur_file->d_name);
1001 dir_entry->inum = cur_inum;
1002 list_add_tail(&dir_entry->list, &dir_head->list);
1003 } else if (S_ISREG(st.st_mode)) {
1004 ret = add_file_items(trans, root, &cur_inode,
1005 cur_inum, parent_inum, &st,
1006 cur_file->d_name, out_fd);
1007 if (ret) {
1008 fprintf(stderr, "add_file_items failed\n");
1009 goto fail;
1011 } else if (S_ISLNK(st.st_mode)) {
1012 ret = add_symbolic_link(trans, root,
1013 cur_inum, cur_file->d_name);
1014 if (ret) {
1015 fprintf(stderr, "add_symbolic_link failed\n");
1016 goto fail;
1021 free_namelist(files, count);
1022 free(parent_dir_entry->path);
1023 free(parent_dir_entry);
1025 index_cnt = 2;
1027 } while (!list_empty(&dir_head->list));
1029 return 0;
1030 fail:
1031 free_namelist(files, count);
1032 fail_no_files:
1033 free(parent_dir_entry->path);
1034 free(parent_dir_entry);
1035 return -1;
1038 static int open_target(char *output_name)
1040 int output_fd;
1041 output_fd = open(output_name, O_CREAT | O_RDWR | O_TRUNC,
1042 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH);
1044 return output_fd;
1047 static int create_chunks(struct btrfs_trans_handle *trans,
1048 struct btrfs_root *root, u64 num_of_meta_chunks,
1049 u64 size_of_data)
1051 u64 chunk_start;
1052 u64 chunk_size;
1053 u64 meta_type = BTRFS_BLOCK_GROUP_METADATA;
1054 u64 data_type = BTRFS_BLOCK_GROUP_DATA;
1055 u64 minimum_data_chunk_size = 8 * 1024 * 1024;
1056 u64 i;
1057 int ret;
1059 for (i = 0; i < num_of_meta_chunks; i++) {
1060 ret = btrfs_alloc_chunk(trans, root->fs_info->extent_root,
1061 &chunk_start, &chunk_size, meta_type);
1062 BUG_ON(ret);
1063 ret = btrfs_make_block_group(trans, root->fs_info->extent_root, 0,
1064 meta_type, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
1065 chunk_start, chunk_size);
1066 BUG_ON(ret);
1067 set_extent_dirty(&root->fs_info->free_space_cache,
1068 chunk_start, chunk_start + chunk_size - 1, 0);
1071 if (size_of_data < minimum_data_chunk_size)
1072 size_of_data = minimum_data_chunk_size;
1073 ret = btrfs_alloc_data_chunk(trans, root->fs_info->extent_root,
1074 &chunk_start, size_of_data, data_type);
1075 BUG_ON(ret);
1076 ret = btrfs_make_block_group(trans, root->fs_info->extent_root, 0,
1077 data_type, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
1078 chunk_start, size_of_data);
1079 BUG_ON(ret);
1080 set_extent_dirty(&root->fs_info->free_space_cache,
1081 chunk_start, chunk_start + size_of_data - 1, 0);
1082 return ret;
1085 static int make_image(char *source_dir, struct btrfs_root *root, int out_fd)
1087 int ret;
1088 struct btrfs_trans_handle *trans;
1090 struct stat root_st;
1092 struct directory_name_entry dir_head;
1094 ret = lstat(source_dir, &root_st);
1095 if (ret) {
1096 fprintf(stderr, "unable to lstat the %s\n", source_dir);
1097 goto fail;
1100 INIT_LIST_HEAD(&dir_head.list);
1102 trans = btrfs_start_transaction(root, 1);
1103 ret = traverse_directory(trans, root, source_dir, &dir_head, out_fd);
1104 if (ret) {
1105 fprintf(stderr, "unable to traverse_directory\n");
1106 goto fail;
1108 btrfs_commit_transaction(trans, root);
1110 printf("Making image is completed.\n");
1111 return 0;
1112 fail:
1113 fprintf(stderr, "Making image is aborted.\n");
1114 return -1;
1117 static u64 size_sourcedir(char *dir_name, u64 sectorsize,
1118 u64 *num_of_meta_chunks_ret, u64 *size_of_data_ret)
1120 u64 dir_size = 0;
1121 u64 total_size = 0;
1122 int ret;
1123 char command[1024];
1124 char path[512];
1125 char *file_name = "temp_file";
1126 FILE *file;
1127 u64 default_chunk_size = 8 * 1024 * 1024; /* 8MB */
1128 u64 allocated_meta_size = 8 * 1024 * 1024; /* 8MB */
1129 u64 allocated_total_size = 20 * 1024 * 1024; /* 20MB */
1130 u64 num_of_meta_chunks = 0;
1131 u64 num_of_allocated_meta_chunks =
1132 allocated_meta_size / default_chunk_size;
1134 ret = sprintf(command, "du -B 4096 -s ");
1135 if (ret < 0) {
1136 fprintf(stderr, "error executing sprintf for du command\n");
1137 return -1;
1139 strcat(command, dir_name);
1140 strcat(command, " > ");
1141 strcat(command, file_name);
1142 ret = system(command);
1144 file = fopen(file_name, "r");
1145 ret = fscanf(file, "%lld %s\n", &dir_size, path);
1146 fclose(file);
1147 remove(file_name);
1149 dir_size *= sectorsize;
1150 *size_of_data_ret = dir_size;
1152 num_of_meta_chunks = (dir_size / 2) / default_chunk_size;
1153 if (((dir_size / 2) % default_chunk_size) != 0)
1154 num_of_meta_chunks++;
1155 if (num_of_meta_chunks <= num_of_allocated_meta_chunks)
1156 num_of_meta_chunks = 0;
1157 else
1158 num_of_meta_chunks -= num_of_allocated_meta_chunks;
1160 total_size = allocated_total_size + dir_size +
1161 (num_of_meta_chunks * default_chunk_size);
1163 *num_of_meta_chunks_ret = num_of_meta_chunks;
1165 return total_size;
1168 static int zero_output_file(int out_fd, u64 size, u32 sectorsize)
1170 int len = sectorsize;
1171 int loop_num = size / sectorsize;
1172 u64 location = 0;
1173 char *buf = malloc(len);
1174 int ret = 0, i;
1175 ssize_t written;
1177 if (!buf)
1178 return -ENOMEM;
1179 memset(buf, 0, len);
1180 for (i = 0; i < loop_num; i++) {
1181 written = pwrite64(out_fd, buf, len, location);
1182 if (written != len)
1183 ret = -EIO;
1184 location += sectorsize;
1186 free(buf);
1187 return ret;
1190 int main(int ac, char **av)
1192 char *file;
1193 struct btrfs_root *root;
1194 struct btrfs_trans_handle *trans;
1195 char *label = NULL;
1196 char *first_file;
1197 u64 block_count = 0;
1198 u64 dev_block_count = 0;
1199 u64 blocks[7];
1200 u64 alloc_start = 0;
1201 u64 metadata_profile = 0;
1202 u64 data_profile = 0;
1203 u32 leafsize = getpagesize();
1204 u32 sectorsize = 4096;
1205 u32 nodesize = leafsize;
1206 u32 stripesize = 4096;
1207 int zero_end = 1;
1208 int option_index = 0;
1209 int fd;
1210 int ret;
1211 int i;
1212 int mixed = 0;
1213 int data_profile_opt = 0;
1214 int metadata_profile_opt = 0;
1216 char *source_dir = NULL;
1217 int source_dir_set = 0;
1218 u64 num_of_meta_chunks = 0;
1219 u64 size_of_data = 0;
1220 u64 source_dir_size = 0;
1221 char *pretty_buf;
1223 while(1) {
1224 int c;
1225 c = getopt_long(ac, av, "A:b:l:n:s:m:d:L:r:VM", long_options,
1226 &option_index);
1227 if (c < 0)
1228 break;
1229 switch(c) {
1230 case 'A':
1231 alloc_start = parse_size(optarg);
1232 break;
1233 case 'd':
1234 data_profile = parse_profile(optarg);
1235 data_profile_opt = 1;
1236 break;
1237 case 'l':
1238 leafsize = parse_size(optarg);
1239 break;
1240 case 'L':
1241 label = parse_label(optarg);
1242 break;
1243 case 'm':
1244 metadata_profile = parse_profile(optarg);
1245 metadata_profile_opt = 1;
1246 break;
1247 case 'M':
1248 mixed = 1;
1249 break;
1250 case 'n':
1251 nodesize = parse_size(optarg);
1252 break;
1253 case 's':
1254 sectorsize = parse_size(optarg);
1255 break;
1256 case 'b':
1257 block_count = parse_size(optarg);
1258 if (block_count <= 1024*1024*1024) {
1259 printf("SMALL VOLUME: forcing mixed "
1260 "metadata/data groups\n");
1261 mixed = 1;
1263 zero_end = 0;
1264 break;
1265 case 'V':
1266 print_version();
1267 break;
1268 case 'r':
1269 source_dir = optarg;
1270 source_dir_set = 1;
1271 break;
1272 default:
1273 print_usage();
1276 sectorsize = max(sectorsize, (u32)getpagesize());
1277 if (leafsize < sectorsize || (leafsize & (sectorsize - 1))) {
1278 fprintf(stderr, "Illegal leafsize %u\n", leafsize);
1279 exit(1);
1281 if (nodesize < sectorsize || (nodesize & (sectorsize - 1))) {
1282 fprintf(stderr, "Illegal nodesize %u\n", nodesize);
1283 exit(1);
1285 ac = ac - optind;
1286 if (ac == 0)
1287 print_usage();
1289 printf("\nWARNING! - %s IS EXPERIMENTAL\n", BTRFS_BUILD_VERSION);
1290 printf("WARNING! - see http://btrfs.wiki.kernel.org before using\n\n");
1292 if (source_dir == 0) {
1293 file = av[optind++];
1294 ret = check_mounted(file);
1295 if (ret < 0) {
1296 fprintf(stderr, "error checking %s mount status\n", file);
1297 exit(1);
1299 if (ret == 1) {
1300 fprintf(stderr, "%s is mounted\n", file);
1301 exit(1);
1303 ac--;
1304 fd = open(file, O_RDWR);
1305 if (fd < 0) {
1306 fprintf(stderr, "unable to open %s\n", file);
1307 exit(1);
1309 first_file = file;
1310 ret = btrfs_prepare_device(fd, file, zero_end, &dev_block_count, &mixed);
1311 if (block_count == 0)
1312 block_count = dev_block_count;
1313 } else {
1314 ac = 0;
1315 file = av[optind++];
1316 fd = open_target(file);
1317 if (fd < 0) {
1318 fprintf(stderr, "unable to open the %s\n", file);
1319 exit(1);
1322 first_file = file;
1323 source_dir_size = size_sourcedir(source_dir, sectorsize,
1324 &num_of_meta_chunks, &size_of_data);
1325 if(block_count < source_dir_size)
1326 block_count = source_dir_size;
1327 ret = zero_output_file(fd, block_count, sectorsize);
1328 if (ret) {
1329 fprintf(stderr, "unable to zero the output file\n");
1330 exit(1);
1333 if (mixed) {
1334 if (metadata_profile != data_profile) {
1335 fprintf(stderr, "With mixed block groups data and metadata "
1336 "profiles must be the same\n");
1337 exit(1);
1341 blocks[0] = BTRFS_SUPER_INFO_OFFSET;
1342 for (i = 1; i < 7; i++) {
1343 blocks[i] = BTRFS_SUPER_INFO_OFFSET + 1024 * 1024 +
1344 leafsize * i;
1347 ret = make_btrfs(fd, file, label, blocks, block_count,
1348 nodesize, leafsize,
1349 sectorsize, stripesize);
1350 if (ret) {
1351 fprintf(stderr, "error during mkfs %d\n", ret);
1352 exit(1);
1355 root = open_ctree(file, 0, O_RDWR);
1356 if (!root) {
1357 fprintf(stderr, "ctree init failed\n");
1358 exit(1);
1360 root->fs_info->alloc_start = alloc_start;
1362 ret = make_root_dir(root, mixed);
1363 if (ret) {
1364 fprintf(stderr, "failed to setup the root directory\n");
1365 exit(1);
1368 trans = btrfs_start_transaction(root, 1);
1370 if (ac == 0)
1371 goto raid_groups;
1373 btrfs_register_one_device(file);
1375 zero_end = 1;
1376 while(ac-- > 0) {
1377 int old_mixed = mixed;
1379 file = av[optind++];
1380 ret = check_mounted(file);
1381 if (ret < 0) {
1382 fprintf(stderr, "error checking %s mount status\n",
1383 file);
1384 exit(1);
1386 if (ret == 1) {
1387 fprintf(stderr, "%s is mounted\n", file);
1388 exit(1);
1390 fd = open(file, O_RDWR);
1391 if (fd < 0) {
1392 fprintf(stderr, "unable to open %s\n", file);
1393 exit(1);
1395 ret = btrfs_device_already_in_root(root, fd,
1396 BTRFS_SUPER_INFO_OFFSET);
1397 if (ret) {
1398 fprintf(stderr, "skipping duplicate device %s in FS\n",
1399 file);
1400 close(fd);
1401 continue;
1403 ret = btrfs_prepare_device(fd, file, zero_end,
1404 &dev_block_count, &mixed);
1405 mixed = old_mixed;
1406 BUG_ON(ret);
1408 ret = btrfs_add_to_fsid(trans, root, fd, file, dev_block_count,
1409 sectorsize, sectorsize, sectorsize);
1410 BUG_ON(ret);
1411 btrfs_register_one_device(file);
1414 raid_groups:
1415 if (!source_dir_set) {
1416 ret = create_raid_groups(trans, root, data_profile,
1417 data_profile_opt, metadata_profile,
1418 metadata_profile_opt, mixed);
1419 BUG_ON(ret);
1422 ret = create_data_reloc_tree(trans, root);
1423 BUG_ON(ret);
1425 if (mixed) {
1426 struct btrfs_super_block *super = &root->fs_info->super_copy;
1427 u64 flags = btrfs_super_incompat_flags(super);
1429 flags |= BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS;
1430 btrfs_set_super_incompat_flags(super, flags);
1433 printf("fs created label %s on %s\n\tnodesize %u leafsize %u "
1434 "sectorsize %u size %s\n",
1435 label, first_file, nodesize, leafsize, sectorsize,
1436 pretty_buf = pretty_sizes(btrfs_super_total_bytes(&root->fs_info->super_copy)));
1437 free(pretty_buf);
1439 printf("%s\n", BTRFS_BUILD_VERSION);
1440 btrfs_commit_transaction(trans, root);
1442 if (source_dir_set) {
1443 trans = btrfs_start_transaction(root, 1);
1444 ret = create_chunks(trans, root,
1445 num_of_meta_chunks, size_of_data);
1446 BUG_ON(ret);
1447 btrfs_commit_transaction(trans, root);
1449 ret = make_image(source_dir, root, fd);
1450 BUG_ON(ret);
1453 ret = close_ctree(root);
1454 BUG_ON(ret);
1456 free(label);
1457 return 0;