Btrfs: fix raid10 reading math
[platform/upstream/btrfs-progs.git] / mkfs.c
1 /*
2  * Copyright (C) 2007 Oracle.  All rights reserved.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public
6  * License v2 as published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public
14  * License along with this program; if not, write to the
15  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16  * Boston, MA 021110-1307, USA.
17  */
18
19 #define _XOPEN_SOURCE 500
20 #define _GNU_SOURCE
21
22 #ifndef __CHECKER__
23 #include <sys/ioctl.h>
24 #include <sys/mount.h>
25 #include "ioctl.h"
26 #endif
27
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"
47
48 static u64 index_cnt = 2;
49
50 struct directory_name_entry {
51         char *dir_name;
52         char *path;
53         ino_t inum;
54         struct list_head list;
55 };
56
57 static u64 parse_size(char *s)
58 {
59         int len = strlen(s);
60         char c;
61         u64 mult = 1;
62
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);
77                 }
78                 s[len - 1] = '\0';
79         }
80         return atol(s) * mult;
81 }
82
83 static int make_root_dir(struct btrfs_root *root, int mixed)
84 {
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;
91
92         trans = btrfs_start_transaction(root, 1);
93         bytes_used = btrfs_super_bytes_used(&root->fs_info->super_copy);
94
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);
101
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);
125         }
126
127         root->fs_info->system_allocs = 0;
128         btrfs_commit_transaction(trans, root);
129         trans = btrfs_start_transaction(root, 1);
130         BUG_ON(!trans);
131
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);
142         }
143
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;
159
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;
165
166         btrfs_commit_transaction(trans, root);
167 err:
168         return ret;
169 }
170
171 static int recow_roots(struct btrfs_trans_handle *trans,
172                        struct btrfs_root *root)
173 {
174         int ret;
175         struct extent_buffer *tmp;
176         struct btrfs_fs_info *info = root->fs_info;
177
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);
182
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);
187
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);
192
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);
197
198
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);
203
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);
208
209         return 0;
210 }
211
212 static int create_one_raid_group(struct btrfs_trans_handle *trans,
213                               struct btrfs_root *root, u64 type)
214 {
215         u64 chunk_start;
216         u64 chunk_size;
217         int ret;
218
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;
227 }
228
229 static int create_raid_groups(struct btrfs_trans_handle *trans,
230                               struct btrfs_root *root, u64 data_profile,
231                               u64 metadata_profile, int mixed)
232 {
233         u64 num_devices = btrfs_super_num_devices(&root->fs_info->super_copy);
234         u64 allowed;
235         int ret;
236
237         if (num_devices == 1)
238                 allowed = BTRFS_BLOCK_GROUP_DUP;
239         else if (num_devices >= 4) {
240                 allowed = BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1 |
241                         BTRFS_BLOCK_GROUP_RAID10;
242         } else
243                 allowed = BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1;
244
245         if (allowed & metadata_profile) {
246                 u64 meta_flags = BTRFS_BLOCK_GROUP_METADATA;
247
248                 ret = create_one_raid_group(trans, root,
249                                             BTRFS_BLOCK_GROUP_SYSTEM |
250                                             (allowed & metadata_profile));
251                 BUG_ON(ret);
252
253                 if (mixed)
254                         meta_flags |= BTRFS_BLOCK_GROUP_DATA;
255
256                 ret = create_one_raid_group(trans, root, meta_flags |
257                                             (allowed & metadata_profile));
258                 BUG_ON(ret);
259
260                 ret = recow_roots(trans, root);
261                 BUG_ON(ret);
262         }
263         if (!mixed && num_devices > 1 && (allowed & data_profile)) {
264                 ret = create_one_raid_group(trans, root,
265                                             BTRFS_BLOCK_GROUP_DATA |
266                                             (allowed & data_profile));
267                 BUG_ON(ret);
268         }
269         return 0;
270 }
271
272 static int create_data_reloc_tree(struct btrfs_trans_handle *trans,
273                                   struct btrfs_root *root)
274 {
275         struct btrfs_key location;
276         struct btrfs_root_item root_item;
277         struct extent_buffer *tmp;
278         u64 objectid = BTRFS_DATA_RELOC_TREE_OBJECTID;
279         int ret;
280
281         ret = btrfs_copy_root(trans, root, root->node, &tmp, objectid);
282         BUG_ON(ret);
283
284         memcpy(&root_item, &root->root_item, sizeof(root_item));
285         btrfs_set_root_bytenr(&root_item, tmp->start);
286         btrfs_set_root_level(&root_item, btrfs_header_level(tmp));
287         btrfs_set_root_generation(&root_item, trans->transid);
288         free_extent_buffer(tmp);
289
290         location.objectid = objectid;
291         location.type = BTRFS_ROOT_ITEM_KEY;
292         location.offset = 0;
293         ret = btrfs_insert_root(trans, root->fs_info->tree_root,
294                                 &location, &root_item);
295         BUG_ON(ret);
296         return 0;
297 }
298
299 static void print_usage(void)
300 {
301         fprintf(stderr, "usage: mkfs.btrfs [options] dev [ dev ... ]\n");
302         fprintf(stderr, "options:\n");
303         fprintf(stderr, "\t -A --alloc-start the offset to start the FS\n");
304         fprintf(stderr, "\t -b --byte-count total number of bytes in the FS\n");
305         fprintf(stderr, "\t -d --data data profile, raid0, raid1, raid10 or single\n");
306         fprintf(stderr, "\t -l --leafsize size of btree leaves\n");
307         fprintf(stderr, "\t -L --label set a label\n");
308         fprintf(stderr, "\t -m --metadata metadata profile, values like data profile\n");
309         fprintf(stderr, "\t -M --mixed mix metadata and data together\n");
310         fprintf(stderr, "\t -n --nodesize size of btree nodes\n");
311         fprintf(stderr, "\t -s --sectorsize min block allocation\n");
312         fprintf(stderr, "\t -r --rootdir the source directory\n");
313         fprintf(stderr, "%s\n", BTRFS_BUILD_VERSION);
314         exit(1);
315 }
316
317 static void print_version(void)
318 {
319         fprintf(stderr, "mkfs.btrfs, part of %s\n", BTRFS_BUILD_VERSION);
320         exit(0);
321 }
322
323 static u64 parse_profile(char *s)
324 {
325         if (strcmp(s, "raid0") == 0) {
326                 return BTRFS_BLOCK_GROUP_RAID0;
327         } else if (strcmp(s, "raid1") == 0) {
328                 return BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_DUP;
329         } else if (strcmp(s, "raid10") == 0) {
330                 return BTRFS_BLOCK_GROUP_RAID10 | BTRFS_BLOCK_GROUP_DUP;
331         } else if (strcmp(s, "single") == 0) {
332                 return 0;
333         } else {
334                 fprintf(stderr, "Unknown option %s\n", s);
335                 print_usage();
336         }
337         return 0;
338 }
339
340 static char *parse_label(char *input)
341 {
342         int i;
343         int len = strlen(input);
344
345         if (len >= BTRFS_LABEL_SIZE) {
346                 fprintf(stderr, "Label %s is too long (max %d)\n", input,
347                         BTRFS_LABEL_SIZE - 1);
348                 exit(1);
349         }
350         for (i = 0; i < len; i++) {
351                 if (input[i] == '/' || input[i] == '\\') {
352                         fprintf(stderr, "invalid label %s\n", input);
353                         exit(1);
354                 }
355         }
356         return strdup(input);
357 }
358
359 static struct option long_options[] = {
360         { "alloc-start", 1, NULL, 'A'},
361         { "byte-count", 1, NULL, 'b' },
362         { "leafsize", 1, NULL, 'l' },
363         { "label", 1, NULL, 'L'},
364         { "metadata", 1, NULL, 'm' },
365         { "mixed", 0, NULL, 'M' },
366         { "nodesize", 1, NULL, 'n' },
367         { "sectorsize", 1, NULL, 's' },
368         { "data", 1, NULL, 'd' },
369         { "version", 0, NULL, 'V' },
370         { "rootdir", 1, NULL, 'r' },
371         { 0, 0, 0, 0}
372 };
373
374 static int add_directory_items(struct btrfs_trans_handle *trans,
375                                struct btrfs_root *root, u64 objectid,
376                                ino_t parent_inum, const char *name,
377                                struct stat *st, int *dir_index_cnt)
378 {
379         int ret;
380         int name_len;
381         struct btrfs_key location;
382         u8 filetype = 0;
383
384         name_len = strlen(name);
385
386         location.objectid = objectid;
387         location.offset = 0;
388         btrfs_set_key_type(&location, BTRFS_INODE_ITEM_KEY);
389
390         if (S_ISDIR(st->st_mode))
391                 filetype = BTRFS_FT_DIR;
392         if (S_ISREG(st->st_mode))
393                 filetype = BTRFS_FT_REG_FILE;
394         if (S_ISLNK(st->st_mode))
395                 filetype = BTRFS_FT_SYMLINK;
396
397         ret = btrfs_insert_dir_item(trans, root, name, name_len,
398                                     parent_inum, &location,
399                                     filetype, index_cnt);
400
401         *dir_index_cnt = index_cnt;
402         index_cnt++;
403
404         return ret;
405 }
406
407 static int fill_inode_item(struct btrfs_trans_handle *trans,
408                            struct btrfs_root *root,
409                            struct btrfs_inode_item *dst, struct stat *src)
410 {
411         u64 blocks = 0;
412         u64 sectorsize = root->sectorsize;
413
414         /*
415          * btrfs_inode_item has some reserved fields
416          * and represents on-disk inode entry, so
417          * zero everything to prevent information leak
418          */
419         memset(dst, 0, sizeof (*dst));
420
421         btrfs_set_stack_inode_generation(dst, trans->transid);
422         btrfs_set_stack_inode_size(dst, src->st_size);
423         btrfs_set_stack_inode_nbytes(dst, 0);
424         btrfs_set_stack_inode_block_group(dst, 0);
425         btrfs_set_stack_inode_nlink(dst, src->st_nlink);
426         btrfs_set_stack_inode_uid(dst, src->st_uid);
427         btrfs_set_stack_inode_gid(dst, src->st_gid);
428         btrfs_set_stack_inode_mode(dst, src->st_mode);
429         btrfs_set_stack_inode_rdev(dst, 0);
430         btrfs_set_stack_inode_flags(dst, 0);
431         btrfs_set_stack_timespec_sec(&dst->atime, src->st_atime);
432         btrfs_set_stack_timespec_nsec(&dst->atime, 0);
433         btrfs_set_stack_timespec_sec(&dst->ctime, src->st_ctime);
434         btrfs_set_stack_timespec_nsec(&dst->ctime, 0);
435         btrfs_set_stack_timespec_sec(&dst->mtime, src->st_mtime);
436         btrfs_set_stack_timespec_nsec(&dst->mtime, 0);
437         btrfs_set_stack_timespec_sec(&dst->otime, 0);
438         btrfs_set_stack_timespec_nsec(&dst->otime, 0);
439
440         if (S_ISDIR(src->st_mode)) {
441                 btrfs_set_stack_inode_size(dst, 0);
442                 btrfs_set_stack_inode_nlink(dst, 1);
443         }
444         if (S_ISREG(src->st_mode)) {
445                 btrfs_set_stack_inode_size(dst, (u64)src->st_size);
446                 if (src->st_size <= BTRFS_MAX_INLINE_DATA_SIZE(root))
447                         btrfs_set_stack_inode_nbytes(dst, src->st_size);
448                 else {
449                         blocks = src->st_size / sectorsize;
450                         if (src->st_size % sectorsize)
451                                 blocks += 1;
452                         blocks *= sectorsize;
453                         btrfs_set_stack_inode_nbytes(dst, blocks);
454                 }
455         }
456         if (S_ISLNK(src->st_mode))
457                 btrfs_set_stack_inode_nbytes(dst, src->st_size + 1);
458
459         return 0;
460 }
461
462 static int directory_select(const struct direct *entry)
463 {
464         if ((strncmp(entry->d_name, ".", entry->d_reclen) == 0) ||
465                 (strncmp(entry->d_name, "..", entry->d_reclen) == 0))
466                 return 0;
467         else
468                 return 1;
469 }
470
471 static void free_namelist(struct direct **files, int count)
472 {
473         int i;
474
475         if (count < 0)
476                 return;
477
478         for (i = 0; i < count; ++i)
479                 free(files[i]);
480         free(files);
481 }
482
483 static u64 calculate_dir_inode_size(char *dirname)
484 {
485         int count, i;
486         struct direct **files, *cur_file;
487         u64 dir_inode_size = 0;
488
489         count = scandir(dirname, &files, directory_select, NULL);
490
491         for (i = 0; i < count; i++) {
492                 cur_file = files[i];
493                 dir_inode_size += strlen(cur_file->d_name);
494         }
495
496         free_namelist(files, count);
497
498         dir_inode_size *= 2;
499         return dir_inode_size;
500 }
501
502 static int add_inode_items(struct btrfs_trans_handle *trans,
503                            struct btrfs_root *root,
504                            struct stat *st, char *name,
505                            u64 self_objectid, ino_t parent_inum,
506                            int dir_index_cnt, struct btrfs_inode_item *inode_ret)
507 {
508         int ret;
509         struct btrfs_key inode_key;
510         struct btrfs_inode_item btrfs_inode;
511         u64 objectid;
512         u64 inode_size = 0;
513         int name_len;
514
515         name_len = strlen(name);
516         fill_inode_item(trans, root, &btrfs_inode, st);
517         objectid = self_objectid;
518
519         if (S_ISDIR(st->st_mode)) {
520                 inode_size = calculate_dir_inode_size(name);
521                 btrfs_set_stack_inode_size(&btrfs_inode, inode_size);
522         }
523
524         inode_key.objectid = objectid;
525         inode_key.offset = 0;
526         btrfs_set_key_type(&inode_key, BTRFS_INODE_ITEM_KEY);
527
528         ret = btrfs_insert_inode(trans, root, objectid, &btrfs_inode);
529         if (ret)
530                 goto fail;
531
532         ret = btrfs_insert_inode_ref(trans, root, name, name_len,
533                                      objectid, parent_inum, dir_index_cnt);
534         if (ret)
535                 goto fail;
536
537         *inode_ret = btrfs_inode;
538 fail:
539         return ret;
540 }
541
542 static int add_xattr_item(struct btrfs_trans_handle *trans,
543                           struct btrfs_root *root, u64 objectid,
544                           const char *file_name)
545 {
546         int ret;
547         int cur_name_len;
548         char xattr_list[XATTR_LIST_MAX];
549         char *cur_name;
550         char cur_value[XATTR_SIZE_MAX];
551         char delimiter = '\0';
552         char *next_location = xattr_list;
553
554         ret = llistxattr(file_name, xattr_list, XATTR_LIST_MAX);
555         if (ret < 0) {
556                 if(errno == ENOTSUP)
557                         return 0;
558                 fprintf(stderr, "get a list of xattr failed for %s\n",
559                         file_name);
560                 return ret;
561         }
562         if (ret == 0)
563                 return ret;
564
565         cur_name = strtok(xattr_list, &delimiter);
566         while (cur_name != NULL) {
567                 cur_name_len = strlen(cur_name);
568                 next_location += cur_name_len + 1;
569
570                 ret = getxattr(file_name, cur_name, cur_value, XATTR_SIZE_MAX);
571                 if (ret < 0) {
572                         if(errno == ENOTSUP)
573                                 return 0;
574                         fprintf(stderr, "get a xattr value failed for %s attr %s\n",
575                                 file_name, cur_name);
576                         return ret;
577                 }
578
579                 ret = btrfs_insert_xattr_item(trans, root, cur_name,
580                                               cur_name_len, cur_value,
581                                               ret, objectid);
582                 if (ret) {
583                         fprintf(stderr, "insert a xattr item failed for %s\n",
584                                 file_name);
585                 }
586
587                 cur_name = strtok(next_location, &delimiter);
588         }
589
590         return ret;
591 }
592 static int custom_alloc_extent(struct btrfs_root *root, u64 num_bytes,
593                                u64 hint_byte, struct btrfs_key *ins)
594 {
595         u64 start;
596         u64 end;
597         u64 last = hint_byte;
598         int ret;
599         int wrapped = 0;
600         struct btrfs_block_group_cache *cache;
601
602         while (1) {
603                 ret = find_first_extent_bit(&root->fs_info->free_space_cache,
604                                             last, &start, &end, EXTENT_DIRTY);
605                 if (ret) {
606                         if (wrapped++ == 0) {
607                                 last = 0;
608                                 continue;
609                         } else {
610                                 goto fail;
611                         }
612                 }
613
614                 start = max(last, start);
615                 last = end + 1;
616                 if (last - start < num_bytes)
617                         continue;
618
619                 last = start + num_bytes;
620                 if (test_range_bit(&root->fs_info->pinned_extents,
621                                    start, last - 1, EXTENT_DIRTY, 0))
622                         continue;
623
624                 cache = btrfs_lookup_block_group(root->fs_info, start);
625                 BUG_ON(!cache);
626                 if (cache->flags & BTRFS_BLOCK_GROUP_SYSTEM ||
627                     last > cache->key.objectid + cache->key.offset) {
628                         last = cache->key.objectid + cache->key.offset;
629                         continue;
630                 }
631
632                 if (cache->flags & (BTRFS_BLOCK_GROUP_SYSTEM |
633                             BTRFS_BLOCK_GROUP_METADATA)) {
634                         last = cache->key.objectid + cache->key.offset;
635                         continue;
636                 }
637
638                 clear_extent_dirty(&root->fs_info->free_space_cache,
639                                    start, start + num_bytes - 1, 0);
640
641                 ins->objectid = start;
642                 ins->offset = num_bytes;
643                 ins->type = BTRFS_EXTENT_ITEM_KEY;
644                 return 0;
645         }
646 fail:
647         fprintf(stderr, "not enough free space\n");
648         return -ENOSPC;
649 }
650
651 static int record_file_extent(struct btrfs_trans_handle *trans,
652                               struct btrfs_root *root, u64 objectid,
653                               struct btrfs_inode_item *inode,
654                               u64 file_pos, u64 disk_bytenr,
655                               u64 num_bytes)
656 {
657         int ret;
658         struct btrfs_fs_info *info = root->fs_info;
659         struct btrfs_root *extent_root = info->extent_root;
660         struct extent_buffer *leaf;
661         struct btrfs_file_extent_item *fi;
662         struct btrfs_key ins_key;
663         struct btrfs_path path;
664         struct btrfs_extent_item *ei;
665
666         btrfs_init_path(&path);
667
668         ins_key.objectid = objectid;
669         ins_key.offset = 0;
670         btrfs_set_key_type(&ins_key, BTRFS_EXTENT_DATA_KEY);
671         ret = btrfs_insert_empty_item(trans, root, &path, &ins_key,
672                                       sizeof(*fi));
673         if (ret)
674                 goto fail;
675         leaf = path.nodes[0];
676         fi = btrfs_item_ptr(leaf, path.slots[0],
677                             struct btrfs_file_extent_item);
678         btrfs_set_file_extent_generation(leaf, fi, trans->transid);
679         btrfs_set_file_extent_type(leaf, fi, BTRFS_FILE_EXTENT_REG);
680         btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
681         btrfs_set_file_extent_disk_num_bytes(leaf, fi, num_bytes);
682         btrfs_set_file_extent_offset(leaf, fi, 0);
683         btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
684         btrfs_set_file_extent_ram_bytes(leaf, fi, num_bytes);
685         btrfs_set_file_extent_compression(leaf, fi, 0);
686         btrfs_set_file_extent_encryption(leaf, fi, 0);
687         btrfs_set_file_extent_other_encoding(leaf, fi, 0);
688         btrfs_mark_buffer_dirty(leaf);
689
690         btrfs_release_path(root, &path);
691
692         ins_key.objectid = disk_bytenr;
693         ins_key.offset = num_bytes;
694         ins_key.type = BTRFS_EXTENT_ITEM_KEY;
695
696         ret = btrfs_insert_empty_item(trans, extent_root, &path,
697                                 &ins_key, sizeof(*ei));
698         if (ret == 0) {
699                 leaf = path.nodes[0];
700                 ei = btrfs_item_ptr(leaf, path.slots[0],
701                                     struct btrfs_extent_item);
702
703                 btrfs_set_extent_refs(leaf, ei, 0);
704                 btrfs_set_extent_generation(leaf, ei, trans->transid);
705                 btrfs_set_extent_flags(leaf, ei, BTRFS_EXTENT_FLAG_DATA);
706
707                 btrfs_mark_buffer_dirty(leaf);
708                 ret = btrfs_update_block_group(trans, root, disk_bytenr,
709                                                num_bytes, 1, 0);
710                 if (ret)
711                         goto fail;
712         } else if (ret != -EEXIST) {
713                 goto fail;
714         }
715
716         ret = btrfs_inc_extent_ref(trans, root, disk_bytenr, num_bytes, 0,
717                                    root->root_key.objectid,
718                                    objectid, 0);
719 fail:
720         btrfs_release_path(root, &path);
721         return ret;
722 }
723
724 static int add_symbolic_link(struct btrfs_trans_handle *trans,
725                              struct btrfs_root *root,
726                              u64 objectid, const char *path_name)
727 {
728         int ret;
729         u64 sectorsize = root->sectorsize;
730         char *buf = malloc(sectorsize);
731
732         ret = readlink(path_name, buf, sectorsize);
733         if (ret <= 0) {
734                 fprintf(stderr, "readlink failed for %s\n", path_name);
735                 goto fail;
736         }
737         if (ret >= sectorsize) {
738                 fprintf(stderr, "symlink too long for %s", path_name);
739                 ret = -1;
740                 goto fail;
741         }
742
743         buf[ret] = '\0'; /* readlink does not do it for us */
744         ret = btrfs_insert_inline_extent(trans, root, objectid, 0,
745                                          buf, ret + 1);
746 fail:
747         free(buf);
748         return ret;
749 }
750
751 static int add_file_items(struct btrfs_trans_handle *trans,
752                           struct btrfs_root *root,
753                           struct btrfs_inode_item *btrfs_inode, u64 objectid,
754                           ino_t parent_inum, struct stat *st,
755                           const char *path_name, int out_fd)
756 {
757         int ret = -1;
758         ssize_t ret_read;
759         u64 bytes_read = 0;
760         char *buffer = NULL;
761         struct btrfs_key key;
762         int blocks;
763         u32 sectorsize = root->sectorsize;
764         u64 first_block = 0;
765         u64 num_blocks = 0;
766         int fd;
767
768         fd = open(path_name, O_RDONLY);
769         if (fd == -1) {
770                 fprintf(stderr, "%s open failed\n", path_name);
771                 goto end;
772         }
773
774         blocks = st->st_size / sectorsize;
775         if (st->st_size % sectorsize)
776                 blocks += 1;
777
778         if (st->st_size <= BTRFS_MAX_INLINE_DATA_SIZE(root)) {
779                 buffer = malloc(st->st_size);
780                 ret_read = pread64(fd, buffer, st->st_size, bytes_read);
781                 if (ret_read == -1) {
782                         fprintf(stderr, "%s read failed\n", path_name);
783                         goto end;
784                 }
785
786                 ret = btrfs_insert_inline_extent(trans, root, objectid, 0,
787                                                  buffer, st->st_size);
788                 goto end;
789         }
790
791         ret = custom_alloc_extent(root, blocks * sectorsize, 0, &key);
792         if (ret)
793                 goto end;
794
795         first_block = key.objectid;
796         bytes_read = 0;
797         buffer = malloc(sectorsize);
798
799         do {
800                 memset(buffer, 0, sectorsize);
801                 ret_read = pread64(fd, buffer, sectorsize, bytes_read);
802                 if (ret_read == -1) {
803                         fprintf(stderr, "%s read failed\n", path_name);
804                         goto end;
805                 }
806
807                 ret = pwrite64(out_fd, buffer, sectorsize,
808                                first_block + bytes_read);
809                 if (ret != sectorsize) {
810                         fprintf(stderr, "output file write failed\n");
811                         goto end;
812                 }
813
814                 /* checksum for file data */
815                 ret = btrfs_csum_file_block(trans, root->fs_info->csum_root,
816                                 first_block + (blocks * sectorsize),
817                                 first_block + bytes_read,
818                                 buffer, sectorsize);
819                 if (ret) {
820                         fprintf(stderr, "%s checksum failed\n", path_name);
821                         goto end;
822                 }
823
824                 bytes_read += ret_read;
825                 num_blocks++;
826         } while (ret_read == sectorsize);
827
828         if (num_blocks > 0) {
829                 ret = record_file_extent(trans, root, objectid, btrfs_inode,
830                                          first_block, first_block,
831                                          blocks * sectorsize);
832                 if (ret)
833                         goto end;
834         }
835
836 end:
837         if (buffer)
838                 free(buffer);
839         close(fd);
840         return ret;
841 }
842
843 static char *make_path(char *dir, char *name)
844 {
845         char *path;
846
847         path = malloc(strlen(dir) + strlen(name) + 2);
848         if (!path)
849                 return NULL;
850         strcpy(path, dir);
851         if (dir[strlen(dir) - 1] != '/')
852                 strcat(path, "/");
853         strcat(path, name);
854         return path;
855 }
856
857 static int traverse_directory(struct btrfs_trans_handle *trans,
858                               struct btrfs_root *root, char *dir_name,
859                               struct directory_name_entry *dir_head, int out_fd)
860 {
861         int ret = 0;
862
863         struct btrfs_inode_item cur_inode;
864         struct btrfs_inode_item *inode_item;
865         int count, i, dir_index_cnt;
866         struct direct **files;
867         struct stat st;
868         struct directory_name_entry *dir_entry, *parent_dir_entry;
869         struct direct *cur_file;
870         ino_t parent_inum, cur_inum;
871         ino_t highest_inum = 0;
872         char *parent_dir_name;
873         struct btrfs_path path;
874         struct extent_buffer *leaf;
875         struct btrfs_key root_dir_key;
876         u64 root_dir_inode_size = 0;
877
878         /* Add list for source directory */
879         dir_entry = malloc(sizeof(struct directory_name_entry));
880         dir_entry->dir_name = dir_name;
881         dir_entry->path = malloc(strlen(dir_name) + 1);
882         strcpy(dir_entry->path, dir_name);
883
884         parent_inum = highest_inum + BTRFS_FIRST_FREE_OBJECTID;
885         dir_entry->inum = parent_inum;
886         list_add_tail(&dir_entry->list, &dir_head->list);
887
888         btrfs_init_path(&path);
889
890         root_dir_key.objectid = btrfs_root_dirid(&root->root_item);
891         root_dir_key.offset = 0;
892         btrfs_set_key_type(&root_dir_key, BTRFS_INODE_ITEM_KEY);
893         ret = btrfs_lookup_inode(trans, root, &path, &root_dir_key, 1);
894         if (ret) {
895                 fprintf(stderr, "root dir lookup error\n");
896                 return -1;
897         }
898
899         leaf = path.nodes[0];
900         inode_item = btrfs_item_ptr(leaf, path.slots[0],
901                                     struct btrfs_inode_item);
902
903         root_dir_inode_size = calculate_dir_inode_size(dir_name);
904         btrfs_set_inode_size(leaf, inode_item, root_dir_inode_size);
905         btrfs_mark_buffer_dirty(leaf);
906
907         btrfs_release_path(root, &path);
908
909         do {
910                 parent_dir_entry = list_entry(dir_head->list.next,
911                                               struct directory_name_entry,
912                                               list);
913                 list_del(&parent_dir_entry->list);
914
915                 parent_inum = parent_dir_entry->inum;
916                 parent_dir_name = parent_dir_entry->dir_name;
917                 if (chdir(parent_dir_entry->path)) {
918                         fprintf(stderr, "chdir error for %s\n",
919                                 parent_dir_name);
920                         goto fail_no_files;
921                 }
922
923                 count = scandir(parent_dir_entry->path, &files,
924                                 directory_select, NULL);
925                 if (count == -1)
926                 {
927                         fprintf(stderr, "scandir for %s failed: %s\n",
928                                 parent_dir_name, strerror (errno));
929                         goto fail;
930                 }
931
932                 for (i = 0; i < count; i++) {
933                         cur_file = files[i];
934
935                         if (lstat(cur_file->d_name, &st) == -1) {
936                                 fprintf(stderr, "lstat failed for file %s\n",
937                                         cur_file->d_name);
938                                 goto fail;
939                         }
940
941                         cur_inum = ++highest_inum + BTRFS_FIRST_FREE_OBJECTID;
942                         ret = add_directory_items(trans, root,
943                                                   cur_inum, parent_inum,
944                                                   cur_file->d_name,
945                                                   &st, &dir_index_cnt);
946                         if (ret) {
947                                 fprintf(stderr, "add_directory_items failed\n");
948                                 goto fail;
949                         }
950
951                         ret = add_inode_items(trans, root, &st,
952                                               cur_file->d_name, cur_inum,
953                                               parent_inum, dir_index_cnt,
954                                               &cur_inode);
955                         if (ret) {
956                                 fprintf(stderr, "add_inode_items failed\n");
957                                 goto fail;
958                         }
959
960                         ret = add_xattr_item(trans, root,
961                                              cur_inum, cur_file->d_name);
962                         if (ret) {
963                                 fprintf(stderr, "add_xattr_item failed\n");
964                                 if(ret != -ENOTSUP)
965                                         goto fail;
966                         }
967
968                         if (S_ISDIR(st.st_mode)) {
969                                 dir_entry = malloc(sizeof(struct directory_name_entry));
970                                 dir_entry->dir_name = cur_file->d_name;
971                                 dir_entry->path = make_path(parent_dir_entry->path,
972                                                             cur_file->d_name);
973                                 dir_entry->inum = cur_inum;
974                                 list_add_tail(&dir_entry->list, &dir_head->list);
975                         } else if (S_ISREG(st.st_mode)) {
976                                 ret = add_file_items(trans, root, &cur_inode,
977                                                      cur_inum, parent_inum, &st,
978                                                      cur_file->d_name, out_fd);
979                                 if (ret) {
980                                         fprintf(stderr, "add_file_items failed\n");
981                                         goto fail;
982                                 }
983                         } else if (S_ISLNK(st.st_mode)) {
984                                 ret = add_symbolic_link(trans, root,
985                                                         cur_inum, cur_file->d_name);
986                                 if (ret) {
987                                         fprintf(stderr, "add_symbolic_link failed\n");
988                                         goto fail;
989                                 }
990                         }
991                 }
992
993                 free_namelist(files, count);
994                 free(parent_dir_entry->path);
995                 free(parent_dir_entry);
996
997                 index_cnt = 2;
998
999         } while (!list_empty(&dir_head->list));
1000
1001         return 0;
1002 fail:
1003         free_namelist(files, count);
1004 fail_no_files:
1005         free(parent_dir_entry->path);
1006         free(parent_dir_entry);
1007         return -1;
1008 }
1009
1010 static int open_target(char *output_name)
1011 {
1012         int output_fd;
1013         output_fd = open(output_name, O_CREAT | O_RDWR | O_TRUNC,
1014                          S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH);
1015
1016         return output_fd;
1017 }
1018
1019 static int create_chunks(struct btrfs_trans_handle *trans,
1020                          struct btrfs_root *root, u64 num_of_meta_chunks,
1021                          u64 size_of_data)
1022 {
1023         u64 chunk_start;
1024         u64 chunk_size;
1025         u64 meta_type = BTRFS_BLOCK_GROUP_METADATA;
1026         u64 data_type = BTRFS_BLOCK_GROUP_DATA;
1027         u64 minimum_data_chunk_size = 8 * 1024 * 1024;
1028         u64 i;
1029         int ret;
1030
1031         for (i = 0; i < num_of_meta_chunks; i++) {
1032                 ret = btrfs_alloc_chunk(trans, root->fs_info->extent_root,
1033                                         &chunk_start, &chunk_size, meta_type);
1034                 BUG_ON(ret);
1035                 ret = btrfs_make_block_group(trans, root->fs_info->extent_root, 0,
1036                                              meta_type, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
1037                                              chunk_start, chunk_size);
1038                 BUG_ON(ret);
1039                 set_extent_dirty(&root->fs_info->free_space_cache,
1040                                  chunk_start, chunk_start + chunk_size - 1, 0);
1041         }
1042
1043         if (size_of_data < minimum_data_chunk_size)
1044                 size_of_data = minimum_data_chunk_size;
1045         ret = btrfs_alloc_data_chunk(trans, root->fs_info->extent_root,
1046                                      &chunk_start, size_of_data, data_type);
1047         BUG_ON(ret);
1048         ret = btrfs_make_block_group(trans, root->fs_info->extent_root, 0,
1049                                      data_type, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
1050                                      chunk_start, size_of_data);
1051         BUG_ON(ret);
1052         set_extent_dirty(&root->fs_info->free_space_cache,
1053                          chunk_start, chunk_start + size_of_data - 1, 0);
1054         return ret;
1055 }
1056
1057 static int make_image(char *source_dir, struct btrfs_root *root, int out_fd)
1058 {
1059         int ret;
1060         struct btrfs_trans_handle *trans;
1061
1062         struct stat root_st;
1063
1064         struct directory_name_entry dir_head;
1065
1066         ret = lstat(source_dir, &root_st);
1067         if (ret) {
1068                 fprintf(stderr, "unable to lstat the %s\n", source_dir);
1069                 goto fail;
1070         }
1071
1072         INIT_LIST_HEAD(&dir_head.list);
1073
1074         trans = btrfs_start_transaction(root, 1);
1075         ret = traverse_directory(trans, root, source_dir, &dir_head, out_fd);
1076         if (ret) {
1077                 fprintf(stderr, "unable to traverse_directory\n");
1078                 goto fail;
1079         }
1080         btrfs_commit_transaction(trans, root);
1081
1082         printf("Making image is completed.\n");
1083         return 0;
1084 fail:
1085         fprintf(stderr, "Making image is aborted.\n");
1086         return -1;
1087 }
1088
1089 static u64 size_sourcedir(char *dir_name, u64 sectorsize,
1090                           u64 *num_of_meta_chunks_ret, u64 *size_of_data_ret)
1091 {
1092         u64 dir_size = 0;
1093         u64 total_size = 0;
1094         int ret;
1095         char command[1024];
1096         char path[512];
1097         char *file_name = "temp_file";
1098         FILE *file;
1099         u64 default_chunk_size = 8 * 1024 * 1024;       /* 8MB */
1100         u64 allocated_meta_size = 8 * 1024 * 1024;      /* 8MB */
1101         u64 allocated_total_size = 20 * 1024 * 1024;    /* 20MB */
1102         u64 num_of_meta_chunks = 0;
1103         u64 num_of_allocated_meta_chunks =
1104                         allocated_meta_size / default_chunk_size;
1105
1106         ret = sprintf(command, "du -B 4096 -s ");
1107         if (ret < 0) {
1108                 fprintf(stderr, "error executing sprintf for du command\n");
1109                 return -1;
1110         }
1111         strcat(command, dir_name);
1112         strcat(command, " > ");
1113         strcat(command, file_name);
1114         ret = system(command);
1115
1116         file = fopen(file_name, "r");
1117         ret = fscanf(file, "%lld %s\n", &dir_size, path);
1118         fclose(file);
1119         remove(file_name);
1120
1121         dir_size *= sectorsize;
1122         *size_of_data_ret = dir_size;
1123
1124         num_of_meta_chunks = (dir_size / 2) / default_chunk_size;
1125         if (((dir_size / 2) % default_chunk_size) != 0)
1126                 num_of_meta_chunks++;
1127         if (num_of_meta_chunks <= num_of_allocated_meta_chunks)
1128                 num_of_meta_chunks = 0;
1129         else
1130                 num_of_meta_chunks -= num_of_allocated_meta_chunks;
1131
1132         total_size = allocated_total_size + dir_size +
1133                      (num_of_meta_chunks * default_chunk_size);
1134
1135         *num_of_meta_chunks_ret = num_of_meta_chunks;
1136
1137         return total_size;
1138 }
1139
1140 static int zero_output_file(int out_fd, u64 size, u32 sectorsize)
1141 {
1142         int len = sectorsize;
1143         int loop_num = size / sectorsize;
1144         u64 location = 0;
1145         char *buf = malloc(len);
1146         int ret = 0, i;
1147         ssize_t written;
1148
1149         if (!buf)
1150                 return -ENOMEM;
1151         memset(buf, 0, len);
1152         for (i = 0; i < loop_num; i++) {
1153                 written = pwrite64(out_fd, buf, len, location);
1154                 if (written != len)
1155                         ret = -EIO;
1156                 location += sectorsize;
1157         }
1158         free(buf);
1159         return ret;
1160 }
1161
1162 int main(int ac, char **av)
1163 {
1164         char *file;
1165         struct btrfs_root *root;
1166         struct btrfs_trans_handle *trans;
1167         char *label = NULL;
1168         char *first_file;
1169         u64 block_count = 0;
1170         u64 dev_block_count = 0;
1171         u64 blocks[7];
1172         u64 alloc_start = 0;
1173         u64 metadata_profile = BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_DUP;
1174         u64 data_profile = BTRFS_BLOCK_GROUP_RAID0;
1175         u32 leafsize = getpagesize();
1176         u32 sectorsize = 4096;
1177         u32 nodesize = leafsize;
1178         u32 stripesize = 4096;
1179         int zero_end = 1;
1180         int option_index = 0;
1181         int fd;
1182         int ret;
1183         int i;
1184         int mixed = 0;
1185         int data_profile_opt = 0;
1186         int metadata_profile_opt = 0;
1187
1188         char *source_dir = NULL;
1189         int source_dir_set = 0;
1190         u64 num_of_meta_chunks = 0;
1191         u64 size_of_data = 0;
1192         u64 source_dir_size = 0;
1193         char *pretty_buf;
1194
1195         while(1) {
1196                 int c;
1197                 c = getopt_long(ac, av, "A:b:l:n:s:m:d:L:r:VM", long_options,
1198                                 &option_index);
1199                 if (c < 0)
1200                         break;
1201                 switch(c) {
1202                         case 'A':
1203                                 alloc_start = parse_size(optarg);
1204                                 break;
1205                         case 'd':
1206                                 data_profile = parse_profile(optarg);
1207                                 data_profile_opt = 1;
1208                                 break;
1209                         case 'l':
1210                                 leafsize = parse_size(optarg);
1211                                 break;
1212                         case 'L':
1213                                 label = parse_label(optarg);
1214                                 break;
1215                         case 'm':
1216                                 metadata_profile = parse_profile(optarg);
1217                                 metadata_profile_opt = 1;
1218                                 break;
1219                         case 'M':
1220                                 mixed = 1;
1221                                 break;
1222                         case 'n':
1223                                 nodesize = parse_size(optarg);
1224                                 break;
1225                         case 's':
1226                                 sectorsize = parse_size(optarg);
1227                                 break;
1228                         case 'b':
1229                                 block_count = parse_size(optarg);
1230                                 if (block_count <= 1024*1024*1024) {
1231                                         printf("SMALL VOLUME: forcing mixed "
1232                                                "metadata/data groups\n");
1233                                         mixed = 1;
1234                                 }
1235                                 zero_end = 0;
1236                                 break;
1237                         case 'V':
1238                                 print_version();
1239                                 break;
1240                         case 'r':
1241                                 source_dir = optarg;
1242                                 source_dir_set = 1;
1243                                 break;
1244                         default:
1245                                 print_usage();
1246                 }
1247         }
1248         sectorsize = max(sectorsize, (u32)getpagesize());
1249         if (leafsize < sectorsize || (leafsize & (sectorsize - 1))) {
1250                 fprintf(stderr, "Illegal leafsize %u\n", leafsize);
1251                 exit(1);
1252         }
1253         if (nodesize < sectorsize || (nodesize & (sectorsize - 1))) {
1254                 fprintf(stderr, "Illegal nodesize %u\n", nodesize);
1255                 exit(1);
1256         }
1257         ac = ac - optind;
1258         if (ac == 0)
1259                 print_usage();
1260
1261         printf("\nWARNING! - %s IS EXPERIMENTAL\n", BTRFS_BUILD_VERSION);
1262         printf("WARNING! - see http://btrfs.wiki.kernel.org before using\n\n");
1263
1264         if (source_dir == 0) {
1265                 file = av[optind++];
1266                 ret = check_mounted(file);
1267                 if (ret < 0) {
1268                         fprintf(stderr, "error checking %s mount status\n", file);
1269                         exit(1);
1270                 }
1271                 if (ret == 1) {
1272                         fprintf(stderr, "%s is mounted\n", file);
1273                         exit(1);
1274                 }
1275                 ac--;
1276                 fd = open(file, O_RDWR);
1277                 if (fd < 0) {
1278                         fprintf(stderr, "unable to open %s\n", file);
1279                         exit(1);
1280                 }
1281                 first_file = file;
1282                 ret = btrfs_prepare_device(fd, file, zero_end, &dev_block_count, &mixed);
1283                 if (block_count == 0)
1284                         block_count = dev_block_count;
1285         } else {
1286                 ac = 0;
1287                 file = av[optind++];
1288                 fd = open_target(file);
1289                 if (fd < 0) {
1290                         fprintf(stderr, "unable to open the %s\n", file);
1291                         exit(1);
1292                 }
1293
1294                 first_file = file;
1295                 source_dir_size = size_sourcedir(source_dir, sectorsize,
1296                                              &num_of_meta_chunks, &size_of_data);
1297                 if(block_count < source_dir_size)
1298                         block_count = source_dir_size;
1299                 ret = zero_output_file(fd, block_count, sectorsize);
1300                 if (ret) {
1301                         fprintf(stderr, "unable to zero the output file\n");
1302                         exit(1);
1303                 }
1304         }
1305         if (mixed) {
1306                 if (!metadata_profile_opt)
1307                         metadata_profile = 0;
1308                 if (!data_profile_opt)
1309                         data_profile = 0;
1310
1311                 if (metadata_profile != data_profile) {
1312                         fprintf(stderr, "With mixed block groups data and metadata "
1313                                 "profiles must be the same\n");
1314                         exit(1);
1315                 }
1316         }
1317
1318         blocks[0] = BTRFS_SUPER_INFO_OFFSET;
1319         for (i = 1; i < 7; i++) {
1320                 blocks[i] = BTRFS_SUPER_INFO_OFFSET + 1024 * 1024 +
1321                         leafsize * i;
1322         }
1323
1324         ret = make_btrfs(fd, file, label, blocks, block_count,
1325                          nodesize, leafsize,
1326                          sectorsize, stripesize);
1327         if (ret) {
1328                 fprintf(stderr, "error during mkfs %d\n", ret);
1329                 exit(1);
1330         }
1331         root = open_ctree(file, 0, O_RDWR);
1332         root->fs_info->alloc_start = alloc_start;
1333
1334         ret = make_root_dir(root, mixed);
1335         if (ret) {
1336                 fprintf(stderr, "failed to setup the root directory\n");
1337                 exit(1);
1338         }
1339
1340         trans = btrfs_start_transaction(root, 1);
1341
1342         if (ac == 0)
1343                 goto raid_groups;
1344
1345         btrfs_register_one_device(file);
1346         if (!root) {
1347                 fprintf(stderr, "ctree init failed\n");
1348                 return -1;
1349         }
1350
1351         zero_end = 1;
1352         while(ac-- > 0) {
1353                 int old_mixed = mixed;
1354
1355                 file = av[optind++];
1356                 ret = check_mounted(file);
1357                 if (ret < 0) {
1358                         fprintf(stderr, "error checking %s mount status\n",
1359                                 file);
1360                         exit(1);
1361                 }
1362                 if (ret == 1) {
1363                         fprintf(stderr, "%s is mounted\n", file);
1364                         exit(1);
1365                 }
1366                 fd = open(file, O_RDWR);
1367                 if (fd < 0) {
1368                         fprintf(stderr, "unable to open %s\n", file);
1369                         exit(1);
1370                 }
1371                 ret = btrfs_device_already_in_root(root, fd,
1372                                                    BTRFS_SUPER_INFO_OFFSET);
1373                 if (ret) {
1374                         fprintf(stderr, "skipping duplicate device %s in FS\n",
1375                                 file);
1376                         close(fd);
1377                         continue;
1378                 }
1379                 ret = btrfs_prepare_device(fd, file, zero_end,
1380                                            &dev_block_count, &mixed);
1381                 mixed = old_mixed;
1382                 BUG_ON(ret);
1383
1384                 ret = btrfs_add_to_fsid(trans, root, fd, file, dev_block_count,
1385                                         sectorsize, sectorsize, sectorsize);
1386                 BUG_ON(ret);
1387                 btrfs_register_one_device(file);
1388         }
1389
1390 raid_groups:
1391         if (!source_dir_set) {
1392                 ret = create_raid_groups(trans, root, data_profile,
1393                                  metadata_profile, mixed);
1394                 BUG_ON(ret);
1395         }
1396
1397         ret = create_data_reloc_tree(trans, root);
1398         BUG_ON(ret);
1399
1400         if (mixed) {
1401                 struct btrfs_super_block *super = &root->fs_info->super_copy;
1402                 u64 flags = btrfs_super_incompat_flags(super);
1403
1404                 flags |= BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS;
1405                 btrfs_set_super_incompat_flags(super, flags);
1406         }
1407
1408         printf("fs created label %s on %s\n\tnodesize %u leafsize %u "
1409             "sectorsize %u size %s\n",
1410             label, first_file, nodesize, leafsize, sectorsize,
1411             pretty_buf = pretty_sizes(btrfs_super_total_bytes(&root->fs_info->super_copy)));
1412         free(pretty_buf);
1413
1414         printf("%s\n", BTRFS_BUILD_VERSION);
1415         btrfs_commit_transaction(trans, root);
1416
1417         if (source_dir_set) {
1418                 trans = btrfs_start_transaction(root, 1);
1419                 ret = create_chunks(trans, root,
1420                                     num_of_meta_chunks, size_of_data);
1421                 BUG_ON(ret);
1422                 btrfs_commit_transaction(trans, root);
1423
1424                 ret = make_image(source_dir, root, fd);
1425                 BUG_ON(ret);
1426         }
1427
1428         ret = close_ctree(root);
1429         BUG_ON(ret);
1430
1431         free(label);
1432         return 0;
1433 }
1434