btrfs-progs: move transaction start/commit out of make_root_dir
[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 #include "kerncompat.h"
20
21 #include <sys/ioctl.h>
22 #include <sys/mount.h>
23 #include "ioctl.h"
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #include <sys/dir.h>
29 #include <fcntl.h>
30 #include <unistd.h>
31 #include <getopt.h>
32 #include <uuid/uuid.h>
33 #include <ctype.h>
34 #include <sys/xattr.h>
35 #include <limits.h>
36 #include <linux/limits.h>
37 #include <blkid/blkid.h>
38 #include <ftw.h>
39 #include "ctree.h"
40 #include "disk-io.h"
41 #include "volumes.h"
42 #include "transaction.h"
43 #include "utils.h"
44
45 static u64 index_cnt = 2;
46 static int verbose = 1;
47
48 struct directory_name_entry {
49         char *dir_name;
50         char *path;
51         ino_t inum;
52         struct list_head list;
53 };
54
55 struct mkfs_allocation {
56         u64 data;
57         u64 metadata;
58         u64 mixed;
59         u64 system;
60 };
61
62 static int create_metadata_block_groups(struct btrfs_root *root, int mixed,
63                                 struct mkfs_allocation *allocation)
64 {
65         struct btrfs_trans_handle *trans;
66         u64 bytes_used;
67         u64 chunk_start = 0;
68         u64 chunk_size = 0;
69         int ret;
70
71         trans = btrfs_start_transaction(root, 1);
72         bytes_used = btrfs_super_bytes_used(root->fs_info->super_copy);
73
74         root->fs_info->system_allocs = 1;
75         ret = btrfs_make_block_group(trans, root, bytes_used,
76                                      BTRFS_BLOCK_GROUP_SYSTEM,
77                                      BTRFS_FIRST_CHUNK_TREE_OBJECTID,
78                                      0, BTRFS_MKFS_SYSTEM_GROUP_SIZE);
79         allocation->system += BTRFS_MKFS_SYSTEM_GROUP_SIZE;
80         BUG_ON(ret);
81
82         if (mixed) {
83                 ret = btrfs_alloc_chunk(trans, root->fs_info->extent_root,
84                                         &chunk_start, &chunk_size,
85                                         BTRFS_BLOCK_GROUP_METADATA |
86                                         BTRFS_BLOCK_GROUP_DATA);
87                 if (ret == -ENOSPC) {
88                         fprintf(stderr,
89                                 "no space to alloc data/metadata chunk\n");
90                         goto err;
91                 }
92                 BUG_ON(ret);
93                 ret = btrfs_make_block_group(trans, root, 0,
94                                              BTRFS_BLOCK_GROUP_METADATA |
95                                              BTRFS_BLOCK_GROUP_DATA,
96                                              BTRFS_FIRST_CHUNK_TREE_OBJECTID,
97                                              chunk_start, chunk_size);
98                 BUG_ON(ret);
99                 allocation->mixed += chunk_size;
100         } else {
101                 ret = btrfs_alloc_chunk(trans, root->fs_info->extent_root,
102                                         &chunk_start, &chunk_size,
103                                         BTRFS_BLOCK_GROUP_METADATA);
104                 if (ret == -ENOSPC) {
105                         fprintf(stderr, "no space to alloc metadata chunk\n");
106                         goto err;
107                 }
108                 BUG_ON(ret);
109                 ret = btrfs_make_block_group(trans, root, 0,
110                                              BTRFS_BLOCK_GROUP_METADATA,
111                                              BTRFS_FIRST_CHUNK_TREE_OBJECTID,
112                                              chunk_start, chunk_size);
113                 allocation->metadata += chunk_size;
114                 BUG_ON(ret);
115         }
116
117         root->fs_info->system_allocs = 0;
118         btrfs_commit_transaction(trans, root);
119
120 err:
121         return ret;
122 }
123
124 static int make_root_dir(struct btrfs_trans_handle *trans, struct btrfs_root *root,
125                 int mixed, struct mkfs_allocation *allocation)
126 {
127         u64 chunk_start = 0;
128         u64 chunk_size = 0;
129         int ret;
130
131         if (!mixed) {
132                 ret = btrfs_alloc_chunk(trans, root->fs_info->extent_root,
133                                         &chunk_start, &chunk_size,
134                                         BTRFS_BLOCK_GROUP_DATA);
135                 if (ret == -ENOSPC) {
136                         fprintf(stderr, "no space to alloc data chunk\n");
137                         goto err;
138                 }
139                 BUG_ON(ret);
140                 ret = btrfs_make_block_group(trans, root, 0,
141                                              BTRFS_BLOCK_GROUP_DATA,
142                                              BTRFS_FIRST_CHUNK_TREE_OBJECTID,
143                                              chunk_start, chunk_size);
144                 allocation->data += chunk_size;
145                 BUG_ON(ret);
146         }
147
148         ret = btrfs_make_root_dir(trans, root->fs_info->tree_root,
149                               BTRFS_ROOT_TREE_DIR_OBJECTID);
150         if (ret)
151                 goto err;
152         ret = btrfs_make_root_dir(trans, root, BTRFS_FIRST_FREE_OBJECTID);
153         if (ret)
154                 goto err;
155         memcpy(&location, &root->fs_info->fs_root->root_key, sizeof(location));
156         location.offset = (u64)-1;
157         ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
158                         "default", 7,
159                         btrfs_super_root_dir(root->fs_info->super_copy),
160                         &location, BTRFS_FT_DIR, 0);
161         if (ret)
162                 goto err;
163
164         ret = btrfs_insert_inode_ref(trans, root->fs_info->tree_root,
165                              "default", 7, location.objectid,
166                              BTRFS_ROOT_TREE_DIR_OBJECTID, 0);
167         if (ret)
168                 goto err;
169
170 err:
171         return ret;
172 }
173
174 static void __recow_root(struct btrfs_trans_handle *trans,
175                          struct btrfs_root *root)
176 {
177         int ret;
178         struct extent_buffer *tmp;
179
180         if (trans->transid != btrfs_root_generation(&root->root_item)) {
181                 extent_buffer_get(root->node);
182                 ret = __btrfs_cow_block(trans, root, root->node,
183                                         NULL, 0, &tmp, 0, 0);
184                 BUG_ON(ret);
185                 free_extent_buffer(tmp);
186         }
187 }
188
189 static void recow_roots(struct btrfs_trans_handle *trans,
190                        struct btrfs_root *root)
191 {
192         struct btrfs_fs_info *info = root->fs_info;
193
194         __recow_root(trans, info->fs_root);
195         __recow_root(trans, info->tree_root);
196         __recow_root(trans, info->extent_root);
197         __recow_root(trans, info->chunk_root);
198         __recow_root(trans, info->dev_root);
199         __recow_root(trans, info->csum_root);
200 }
201
202 static int create_one_raid_group(struct btrfs_trans_handle *trans,
203                               struct btrfs_root *root, u64 type,
204                               struct mkfs_allocation *allocation)
205
206 {
207         u64 chunk_start;
208         u64 chunk_size;
209         int ret;
210
211         ret = btrfs_alloc_chunk(trans, root->fs_info->extent_root,
212                                 &chunk_start, &chunk_size, type);
213         if (ret == -ENOSPC) {
214                 fprintf(stderr, "not enough free space\n");
215                 exit(1);
216         }
217         BUG_ON(ret);
218         ret = btrfs_make_block_group(trans, root->fs_info->extent_root, 0,
219                                      type, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
220                                      chunk_start, chunk_size);
221         if ((type & BTRFS_BLOCK_GROUP_TYPE_MASK) == BTRFS_BLOCK_GROUP_DATA)
222                 allocation->data += chunk_size;
223         else if ((type & BTRFS_BLOCK_GROUP_TYPE_MASK) == BTRFS_BLOCK_GROUP_METADATA)
224                 allocation->metadata += chunk_size;
225         else if ((type & BTRFS_BLOCK_GROUP_TYPE_MASK) == BTRFS_BLOCK_GROUP_SYSTEM)
226                 allocation->system += chunk_size;
227         else if ((type & BTRFS_BLOCK_GROUP_TYPE_MASK) ==
228                         (BTRFS_BLOCK_GROUP_METADATA|BTRFS_BLOCK_GROUP_DATA))
229                 allocation->mixed += chunk_size;
230         else
231                 BUG_ON(1);
232
233         BUG_ON(ret);
234         return ret;
235 }
236
237 static int create_raid_groups(struct btrfs_trans_handle *trans,
238                               struct btrfs_root *root, u64 data_profile,
239                               int data_profile_opt, u64 metadata_profile,
240                               int mixed,
241                               struct mkfs_allocation *allocation)
242 {
243         u64 num_devices = btrfs_super_num_devices(root->fs_info->super_copy);
244         int ret;
245
246         if (metadata_profile) {
247                 u64 meta_flags = BTRFS_BLOCK_GROUP_METADATA;
248
249                 ret = create_one_raid_group(trans, root,
250                                             BTRFS_BLOCK_GROUP_SYSTEM |
251                                             metadata_profile, allocation);
252                 BUG_ON(ret);
253
254                 if (mixed)
255                         meta_flags |= BTRFS_BLOCK_GROUP_DATA;
256
257                 ret = create_one_raid_group(trans, root, meta_flags |
258                                             metadata_profile, allocation);
259                 BUG_ON(ret);
260
261         }
262         if (!mixed && num_devices > 1 && data_profile) {
263                 ret = create_one_raid_group(trans, root,
264                                             BTRFS_BLOCK_GROUP_DATA |
265                                             data_profile, allocation);
266                 BUG_ON(ret);
267         }
268         recow_roots(trans, root);
269
270         return 0;
271 }
272
273 static int create_data_reloc_tree(struct btrfs_trans_handle *trans,
274                                   struct btrfs_root *root)
275 {
276         struct btrfs_key location;
277         struct btrfs_root_item root_item;
278         struct extent_buffer *tmp;
279         u64 objectid = BTRFS_DATA_RELOC_TREE_OBJECTID;
280         int ret;
281
282         ret = btrfs_copy_root(trans, root, root->node, &tmp, objectid);
283         BUG_ON(ret);
284
285         memcpy(&root_item, &root->root_item, sizeof(root_item));
286         btrfs_set_root_bytenr(&root_item, tmp->start);
287         btrfs_set_root_level(&root_item, btrfs_header_level(tmp));
288         btrfs_set_root_generation(&root_item, trans->transid);
289         free_extent_buffer(tmp);
290
291         location.objectid = objectid;
292         location.type = BTRFS_ROOT_ITEM_KEY;
293         location.offset = 0;
294         ret = btrfs_insert_root(trans, root->fs_info->tree_root,
295                                 &location, &root_item);
296         BUG_ON(ret);
297         return 0;
298 }
299
300 static void print_usage(int ret)
301 {
302         fprintf(stderr, "usage: mkfs.btrfs [options] dev [ dev ... ]\n");
303         fprintf(stderr, "options:\n");
304         fprintf(stderr, "\t-A|--alloc-start START  the offset to start the FS\n");
305         fprintf(stderr, "\t-b|--byte-count SIZE    total number of bytes in the FS\n");
306         fprintf(stderr, "\t-d|--data PROFILE       data profile, raid0, raid1, raid5, raid6, raid10, dup or single\n");
307         fprintf(stderr, "\t-f|--force              force overwrite of existing filesystem\n");
308         fprintf(stderr, "\t-l|--leafsize SIZE      deprecated, alias for nodesize\n");
309         fprintf(stderr, "\t-L|--label LABEL        set a label\n");
310         fprintf(stderr, "\t-m|--metadata PROFILE   metadata profile, values like data profile\n");
311         fprintf(stderr, "\t-M|--mixed              mix metadata and data together\n");
312         fprintf(stderr, "\t-n|--nodesize SIZE      size of btree nodes\n");
313         fprintf(stderr, "\t-s|--sectorsize SIZE    min block allocation (may not mountable by current kernel)\n");
314         fprintf(stderr, "\t-r|--rootdir DIR        the source directory\n");
315         fprintf(stderr, "\t-K|--nodiscard          do not perform whole device TRIM\n");
316         fprintf(stderr, "\t-O|--features LIST      comma separated list of filesystem features, use '-O list-all' to list features\n");
317         fprintf(stderr, "\t-U|--uuid UUID          specify the filesystem UUID\n");
318         fprintf(stderr, "\t-q|--quiet              no messages except errors\n");
319         fprintf(stderr, "\t-V|--version            print the mkfs.btrfs version and exit\n");
320         fprintf(stderr, "%s\n", PACKAGE_STRING);
321         exit(ret);
322 }
323
324 static void print_version(void) __attribute__((noreturn));
325 static void print_version(void)
326 {
327         fprintf(stderr, "mkfs.btrfs, part of %s\n", PACKAGE_STRING);
328         exit(0);
329 }
330
331 static u64 parse_profile(char *s)
332 {
333         if (strcmp(s, "raid0") == 0) {
334                 return BTRFS_BLOCK_GROUP_RAID0;
335         } else if (strcasecmp(s, "raid1") == 0) {
336                 return BTRFS_BLOCK_GROUP_RAID1;
337         } else if (strcasecmp(s, "raid5") == 0) {
338                 return BTRFS_BLOCK_GROUP_RAID5;
339         } else if (strcasecmp(s, "raid6") == 0) {
340                 return BTRFS_BLOCK_GROUP_RAID6;
341         } else if (strcasecmp(s, "raid10") == 0) {
342                 return BTRFS_BLOCK_GROUP_RAID10;
343         } else if (strcasecmp(s, "dup") == 0) {
344                 return BTRFS_BLOCK_GROUP_DUP;
345         } else if (strcasecmp(s, "single") == 0) {
346                 return 0;
347         } else {
348                 fprintf(stderr, "Unknown profile %s\n", s);
349         }
350         /* not reached */
351         return 0;
352 }
353
354 static char *parse_label(char *input)
355 {
356         int len = strlen(input);
357
358         if (len >= BTRFS_LABEL_SIZE) {
359                 fprintf(stderr, "Label %s is too long (max %d)\n", input,
360                         BTRFS_LABEL_SIZE - 1);
361                 exit(1);
362         }
363         return strdup(input);
364 }
365
366 static int add_directory_items(struct btrfs_trans_handle *trans,
367                                struct btrfs_root *root, u64 objectid,
368                                ino_t parent_inum, const char *name,
369                                struct stat *st, int *dir_index_cnt)
370 {
371         int ret;
372         int name_len;
373         struct btrfs_key location;
374         u8 filetype = 0;
375
376         name_len = strlen(name);
377
378         location.objectid = objectid;
379         location.offset = 0;
380         btrfs_set_key_type(&location, BTRFS_INODE_ITEM_KEY);
381
382         if (S_ISDIR(st->st_mode))
383                 filetype = BTRFS_FT_DIR;
384         if (S_ISREG(st->st_mode))
385                 filetype = BTRFS_FT_REG_FILE;
386         if (S_ISLNK(st->st_mode))
387                 filetype = BTRFS_FT_SYMLINK;
388
389         ret = btrfs_insert_dir_item(trans, root, name, name_len,
390                                     parent_inum, &location,
391                                     filetype, index_cnt);
392         if (ret)
393                 return ret;
394         ret = btrfs_insert_inode_ref(trans, root, name, name_len,
395                                      objectid, parent_inum, index_cnt);
396         *dir_index_cnt = index_cnt;
397         index_cnt++;
398
399         return ret;
400 }
401
402 static int fill_inode_item(struct btrfs_trans_handle *trans,
403                            struct btrfs_root *root,
404                            struct btrfs_inode_item *dst, struct stat *src)
405 {
406         u64 blocks = 0;
407         u64 sectorsize = root->sectorsize;
408
409         /*
410          * btrfs_inode_item has some reserved fields
411          * and represents on-disk inode entry, so
412          * zero everything to prevent information leak
413          */
414         memset(dst, 0, sizeof (*dst));
415
416         btrfs_set_stack_inode_generation(dst, trans->transid);
417         btrfs_set_stack_inode_size(dst, src->st_size);
418         btrfs_set_stack_inode_nbytes(dst, 0);
419         btrfs_set_stack_inode_block_group(dst, 0);
420         btrfs_set_stack_inode_nlink(dst, src->st_nlink);
421         btrfs_set_stack_inode_uid(dst, src->st_uid);
422         btrfs_set_stack_inode_gid(dst, src->st_gid);
423         btrfs_set_stack_inode_mode(dst, src->st_mode);
424         btrfs_set_stack_inode_rdev(dst, 0);
425         btrfs_set_stack_inode_flags(dst, 0);
426         btrfs_set_stack_timespec_sec(&dst->atime, src->st_atime);
427         btrfs_set_stack_timespec_nsec(&dst->atime, 0);
428         btrfs_set_stack_timespec_sec(&dst->ctime, src->st_ctime);
429         btrfs_set_stack_timespec_nsec(&dst->ctime, 0);
430         btrfs_set_stack_timespec_sec(&dst->mtime, src->st_mtime);
431         btrfs_set_stack_timespec_nsec(&dst->mtime, 0);
432         btrfs_set_stack_timespec_sec(&dst->otime, 0);
433         btrfs_set_stack_timespec_nsec(&dst->otime, 0);
434
435         if (S_ISDIR(src->st_mode)) {
436                 btrfs_set_stack_inode_size(dst, 0);
437                 btrfs_set_stack_inode_nlink(dst, 1);
438         }
439         if (S_ISREG(src->st_mode)) {
440                 btrfs_set_stack_inode_size(dst, (u64)src->st_size);
441                 if (src->st_size <= BTRFS_MAX_INLINE_DATA_SIZE(root))
442                         btrfs_set_stack_inode_nbytes(dst, src->st_size);
443                 else {
444                         blocks = src->st_size / sectorsize;
445                         if (src->st_size % sectorsize)
446                                 blocks += 1;
447                         blocks *= sectorsize;
448                         btrfs_set_stack_inode_nbytes(dst, blocks);
449                 }
450         }
451         if (S_ISLNK(src->st_mode))
452                 btrfs_set_stack_inode_nbytes(dst, src->st_size + 1);
453
454         return 0;
455 }
456
457 static int directory_select(const struct direct *entry)
458 {
459         if ((strncmp(entry->d_name, ".", entry->d_reclen) == 0) ||
460                 (strncmp(entry->d_name, "..", entry->d_reclen) == 0))
461                 return 0;
462         else
463                 return 1;
464 }
465
466 static void free_namelist(struct direct **files, int count)
467 {
468         int i;
469
470         if (count < 0)
471                 return;
472
473         for (i = 0; i < count; ++i)
474                 free(files[i]);
475         free(files);
476 }
477
478 static u64 calculate_dir_inode_size(char *dirname)
479 {
480         int count, i;
481         struct direct **files, *cur_file;
482         u64 dir_inode_size = 0;
483
484         count = scandir(dirname, &files, directory_select, NULL);
485
486         for (i = 0; i < count; i++) {
487                 cur_file = files[i];
488                 dir_inode_size += strlen(cur_file->d_name);
489         }
490
491         free_namelist(files, count);
492
493         dir_inode_size *= 2;
494         return dir_inode_size;
495 }
496
497 static int add_inode_items(struct btrfs_trans_handle *trans,
498                            struct btrfs_root *root,
499                            struct stat *st, char *name,
500                            u64 self_objectid, ino_t parent_inum,
501                            int dir_index_cnt, struct btrfs_inode_item *inode_ret)
502 {
503         int ret;
504         struct btrfs_key inode_key;
505         struct btrfs_inode_item btrfs_inode;
506         u64 objectid;
507         u64 inode_size = 0;
508
509         fill_inode_item(trans, root, &btrfs_inode, st);
510         objectid = self_objectid;
511
512         if (S_ISDIR(st->st_mode)) {
513                 inode_size = calculate_dir_inode_size(name);
514                 btrfs_set_stack_inode_size(&btrfs_inode, inode_size);
515         }
516
517         inode_key.objectid = objectid;
518         inode_key.offset = 0;
519         btrfs_set_key_type(&inode_key, BTRFS_INODE_ITEM_KEY);
520
521         ret = btrfs_insert_inode(trans, root, objectid, &btrfs_inode);
522
523         *inode_ret = btrfs_inode;
524         return ret;
525 }
526
527 static int add_xattr_item(struct btrfs_trans_handle *trans,
528                           struct btrfs_root *root, u64 objectid,
529                           const char *file_name)
530 {
531         int ret;
532         int cur_name_len;
533         char xattr_list[XATTR_LIST_MAX];
534         char *cur_name;
535         char cur_value[XATTR_SIZE_MAX];
536         char delimiter = '\0';
537         char *next_location = xattr_list;
538
539         ret = llistxattr(file_name, xattr_list, XATTR_LIST_MAX);
540         if (ret < 0) {
541                 if(errno == ENOTSUP)
542                         return 0;
543                 fprintf(stderr, "get a list of xattr failed for %s\n",
544                         file_name);
545                 return ret;
546         }
547         if (ret == 0)
548                 return ret;
549
550         cur_name = strtok(xattr_list, &delimiter);
551         while (cur_name != NULL) {
552                 cur_name_len = strlen(cur_name);
553                 next_location += cur_name_len + 1;
554
555                 ret = getxattr(file_name, cur_name, cur_value, XATTR_SIZE_MAX);
556                 if (ret < 0) {
557                         if(errno == ENOTSUP)
558                                 return 0;
559                         fprintf(stderr, "get a xattr value failed for %s attr %s\n",
560                                 file_name, cur_name);
561                         return ret;
562                 }
563
564                 ret = btrfs_insert_xattr_item(trans, root, cur_name,
565                                               cur_name_len, cur_value,
566                                               ret, objectid);
567                 if (ret) {
568                         fprintf(stderr, "insert a xattr item failed for %s\n",
569                                 file_name);
570                 }
571
572                 cur_name = strtok(next_location, &delimiter);
573         }
574
575         return ret;
576 }
577
578 static int add_symbolic_link(struct btrfs_trans_handle *trans,
579                              struct btrfs_root *root,
580                              u64 objectid, const char *path_name)
581 {
582         int ret;
583         u64 sectorsize = root->sectorsize;
584         char *buf = malloc(sectorsize);
585
586         ret = readlink(path_name, buf, sectorsize);
587         if (ret <= 0) {
588                 fprintf(stderr, "readlink failed for %s\n", path_name);
589                 goto fail;
590         }
591         if (ret >= sectorsize) {
592                 fprintf(stderr, "symlink too long for %s", path_name);
593                 ret = -1;
594                 goto fail;
595         }
596
597         buf[ret] = '\0'; /* readlink does not do it for us */
598         ret = btrfs_insert_inline_extent(trans, root, objectid, 0,
599                                          buf, ret + 1);
600 fail:
601         free(buf);
602         return ret;
603 }
604
605 static int add_file_items(struct btrfs_trans_handle *trans,
606                           struct btrfs_root *root,
607                           struct btrfs_inode_item *btrfs_inode, u64 objectid,
608                           ino_t parent_inum, struct stat *st,
609                           const char *path_name, int out_fd)
610 {
611         int ret = -1;
612         ssize_t ret_read;
613         u64 bytes_read = 0;
614         struct btrfs_key key;
615         int blocks;
616         u32 sectorsize = root->sectorsize;
617         u64 first_block = 0;
618         u64 file_pos = 0;
619         u64 cur_bytes;
620         u64 total_bytes;
621         struct extent_buffer *eb = NULL;
622         int fd;
623
624         if (st->st_size == 0)
625                 return 0;
626
627         fd = open(path_name, O_RDONLY);
628         if (fd == -1) {
629                 fprintf(stderr, "%s open failed\n", path_name);
630                 return ret;
631         }
632
633         blocks = st->st_size / sectorsize;
634         if (st->st_size % sectorsize)
635                 blocks += 1;
636
637         if (st->st_size <= BTRFS_MAX_INLINE_DATA_SIZE(root)) {
638                 char *buffer = malloc(st->st_size);
639                 ret_read = pread64(fd, buffer, st->st_size, bytes_read);
640                 if (ret_read == -1) {
641                         fprintf(stderr, "%s read failed\n", path_name);
642                         free(buffer);
643                         goto end;
644                 }
645
646                 ret = btrfs_insert_inline_extent(trans, root, objectid, 0,
647                                                  buffer, st->st_size);
648                 free(buffer);
649                 goto end;
650         }
651
652         /* round up our st_size to the FS blocksize */
653         total_bytes = (u64)blocks * sectorsize;
654
655         /*
656          * do our IO in extent buffers so it can work
657          * against any raid type
658          */
659         eb = malloc(sizeof(*eb) + sectorsize);
660         if (!eb) {
661                 ret = -ENOMEM;
662                 goto end;
663         }
664         memset(eb, 0, sizeof(*eb) + sectorsize);
665
666 again:
667
668         /*
669          * keep our extent size at 1MB max, this makes it easier to work inside
670          * the tiny block groups created during mkfs
671          */
672         cur_bytes = min(total_bytes, 1024ULL * 1024);
673         ret = btrfs_reserve_extent(trans, root, cur_bytes, 0, 0, (u64)-1,
674                                    &key, 1);
675         if (ret)
676                 goto end;
677
678         first_block = key.objectid;
679         bytes_read = 0;
680
681         while (bytes_read < cur_bytes) {
682
683                 memset(eb->data, 0, sectorsize);
684
685                 ret_read = pread64(fd, eb->data, sectorsize, file_pos + bytes_read);
686                 if (ret_read == -1) {
687                         fprintf(stderr, "%s read failed\n", path_name);
688                         goto end;
689                 }
690
691                 eb->start = first_block + bytes_read;
692                 eb->len = sectorsize;
693
694                 /*
695                  * we're doing the csum before we record the extent, but
696                  * that's ok
697                  */
698                 ret = btrfs_csum_file_block(trans, root->fs_info->csum_root,
699                                             first_block + bytes_read + sectorsize,
700                                             first_block + bytes_read,
701                                             eb->data, sectorsize);
702                 if (ret)
703                         goto end;
704
705                 ret = write_and_map_eb(trans, root, eb);
706                 if (ret) {
707                         fprintf(stderr, "output file write failed\n");
708                         goto end;
709                 }
710
711                 bytes_read += sectorsize;
712         }
713
714         if (bytes_read) {
715                 ret = btrfs_record_file_extent(trans, root, objectid, btrfs_inode,
716                                                file_pos, first_block, cur_bytes);
717                 if (ret)
718                         goto end;
719
720         }
721
722         file_pos += cur_bytes;
723         total_bytes -= cur_bytes;
724
725         if (total_bytes)
726                 goto again;
727
728 end:
729         free(eb);
730         close(fd);
731         return ret;
732 }
733
734 static char *make_path(char *dir, char *name)
735 {
736         char *path;
737
738         path = malloc(strlen(dir) + strlen(name) + 2);
739         if (!path)
740                 return NULL;
741         strcpy(path, dir);
742         if (dir[strlen(dir) - 1] != '/')
743                 strcat(path, "/");
744         strcat(path, name);
745         return path;
746 }
747
748 static int traverse_directory(struct btrfs_trans_handle *trans,
749                               struct btrfs_root *root, char *dir_name,
750                               struct directory_name_entry *dir_head, int out_fd)
751 {
752         int ret = 0;
753
754         struct btrfs_inode_item cur_inode;
755         struct btrfs_inode_item *inode_item;
756         int count, i, dir_index_cnt;
757         struct direct **files;
758         struct stat st;
759         struct directory_name_entry *dir_entry, *parent_dir_entry;
760         struct direct *cur_file;
761         ino_t parent_inum, cur_inum;
762         ino_t highest_inum = 0;
763         char *parent_dir_name;
764         char real_path[PATH_MAX];
765         struct btrfs_path path;
766         struct extent_buffer *leaf;
767         struct btrfs_key root_dir_key;
768         u64 root_dir_inode_size = 0;
769
770         /* Add list for source directory */
771         dir_entry = malloc(sizeof(struct directory_name_entry));
772         dir_entry->dir_name = dir_name;
773         dir_entry->path = realpath(dir_name, real_path);
774         if (!dir_entry->path) {
775                 fprintf(stderr, "get directory real path error\n");
776                 ret = -1;
777                 goto fail_no_dir;
778         }
779
780         parent_inum = highest_inum + BTRFS_FIRST_FREE_OBJECTID;
781         dir_entry->inum = parent_inum;
782         list_add_tail(&dir_entry->list, &dir_head->list);
783
784         btrfs_init_path(&path);
785
786         root_dir_key.objectid = btrfs_root_dirid(&root->root_item);
787         root_dir_key.offset = 0;
788         btrfs_set_key_type(&root_dir_key, BTRFS_INODE_ITEM_KEY);
789         ret = btrfs_lookup_inode(trans, root, &path, &root_dir_key, 1);
790         if (ret) {
791                 fprintf(stderr, "root dir lookup error\n");
792                 goto fail_no_dir;
793         }
794
795         leaf = path.nodes[0];
796         inode_item = btrfs_item_ptr(leaf, path.slots[0],
797                                     struct btrfs_inode_item);
798
799         root_dir_inode_size = calculate_dir_inode_size(dir_name);
800         btrfs_set_inode_size(leaf, inode_item, root_dir_inode_size);
801         btrfs_mark_buffer_dirty(leaf);
802
803         btrfs_release_path(&path);
804
805         do {
806                 parent_dir_entry = list_entry(dir_head->list.next,
807                                               struct directory_name_entry,
808                                               list);
809                 list_del(&parent_dir_entry->list);
810
811                 parent_inum = parent_dir_entry->inum;
812                 parent_dir_name = parent_dir_entry->dir_name;
813                 if (chdir(parent_dir_entry->path)) {
814                         fprintf(stderr, "chdir error for %s\n",
815                                 parent_dir_name);
816                         ret = -1;
817                         goto fail_no_files;
818                 }
819
820                 count = scandir(parent_dir_entry->path, &files,
821                                 directory_select, NULL);
822                 if (count == -1)
823                 {
824                         fprintf(stderr, "scandir for %s failed: %s\n",
825                                 parent_dir_name, strerror (errno));
826                         ret = -1;
827                         goto fail;
828                 }
829
830                 for (i = 0; i < count; i++) {
831                         cur_file = files[i];
832
833                         if (lstat(cur_file->d_name, &st) == -1) {
834                                 fprintf(stderr, "lstat failed for file %s\n",
835                                         cur_file->d_name);
836                                 ret = -1;
837                                 goto fail;
838                         }
839
840                         cur_inum = st.st_ino;
841                         ret = add_directory_items(trans, root,
842                                                   cur_inum, parent_inum,
843                                                   cur_file->d_name,
844                                                   &st, &dir_index_cnt);
845                         if (ret) {
846                                 fprintf(stderr, "add_directory_items failed\n");
847                                 goto fail;
848                         }
849
850                         ret = add_inode_items(trans, root, &st,
851                                               cur_file->d_name, cur_inum,
852                                               parent_inum, dir_index_cnt,
853                                               &cur_inode);
854                         if (ret == -EEXIST) {
855                                 BUG_ON(st.st_nlink <= 1);
856                                 continue;
857                         }
858                         if (ret) {
859                                 fprintf(stderr, "add_inode_items failed\n");
860                                 goto fail;
861                         }
862
863                         ret = add_xattr_item(trans, root,
864                                              cur_inum, cur_file->d_name);
865                         if (ret) {
866                                 fprintf(stderr, "add_xattr_item failed\n");
867                                 if(ret != -ENOTSUP)
868                                         goto fail;
869                         }
870
871                         if (S_ISDIR(st.st_mode)) {
872                                 dir_entry = malloc(sizeof(struct directory_name_entry));
873                                 dir_entry->dir_name = cur_file->d_name;
874                                 dir_entry->path = make_path(parent_dir_entry->path,
875                                                             cur_file->d_name);
876                                 dir_entry->inum = cur_inum;
877                                 list_add_tail(&dir_entry->list, &dir_head->list);
878                         } else if (S_ISREG(st.st_mode)) {
879                                 ret = add_file_items(trans, root, &cur_inode,
880                                                      cur_inum, parent_inum, &st,
881                                                      cur_file->d_name, out_fd);
882                                 if (ret) {
883                                         fprintf(stderr, "add_file_items failed\n");
884                                         goto fail;
885                                 }
886                         } else if (S_ISLNK(st.st_mode)) {
887                                 ret = add_symbolic_link(trans, root,
888                                                         cur_inum, cur_file->d_name);
889                                 if (ret) {
890                                         fprintf(stderr, "add_symbolic_link failed\n");
891                                         goto fail;
892                                 }
893                         }
894                 }
895
896                 free_namelist(files, count);
897                 free(parent_dir_entry);
898
899                 index_cnt = 2;
900
901         } while (!list_empty(&dir_head->list));
902
903 out:
904         return !!ret;
905 fail:
906         free_namelist(files, count);
907 fail_no_files:
908         free(parent_dir_entry);
909         goto out;
910 fail_no_dir:
911         free(dir_entry);
912         goto out;
913 }
914
915 static int open_target(char *output_name)
916 {
917         int output_fd;
918         output_fd = open(output_name, O_CREAT | O_RDWR | O_TRUNC,
919                          S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH);
920
921         return output_fd;
922 }
923
924 static int create_chunks(struct btrfs_trans_handle *trans,
925                          struct btrfs_root *root, u64 num_of_meta_chunks,
926                          u64 size_of_data,
927                          struct mkfs_allocation *allocation)
928 {
929         u64 chunk_start;
930         u64 chunk_size;
931         u64 meta_type = BTRFS_BLOCK_GROUP_METADATA;
932         u64 data_type = BTRFS_BLOCK_GROUP_DATA;
933         u64 minimum_data_chunk_size = 8 * 1024 * 1024;
934         u64 i;
935         int ret;
936
937         for (i = 0; i < num_of_meta_chunks; i++) {
938                 ret = btrfs_alloc_chunk(trans, root->fs_info->extent_root,
939                                         &chunk_start, &chunk_size, meta_type);
940                 BUG_ON(ret);
941                 ret = btrfs_make_block_group(trans, root->fs_info->extent_root, 0,
942                                              meta_type, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
943                                              chunk_start, chunk_size);
944                 allocation->metadata += chunk_size;
945                 BUG_ON(ret);
946                 set_extent_dirty(&root->fs_info->free_space_cache,
947                                  chunk_start, chunk_start + chunk_size - 1, 0);
948         }
949
950         if (size_of_data < minimum_data_chunk_size)
951                 size_of_data = minimum_data_chunk_size;
952
953         ret = btrfs_alloc_data_chunk(trans, root->fs_info->extent_root,
954                                      &chunk_start, size_of_data, data_type);
955         BUG_ON(ret);
956         ret = btrfs_make_block_group(trans, root->fs_info->extent_root, 0,
957                                      data_type, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
958                                      chunk_start, size_of_data);
959         allocation->data += size_of_data;
960         BUG_ON(ret);
961         set_extent_dirty(&root->fs_info->free_space_cache,
962                          chunk_start, chunk_start + size_of_data - 1, 0);
963         return ret;
964 }
965
966 static int make_image(char *source_dir, struct btrfs_root *root, int out_fd)
967 {
968         int ret;
969         struct btrfs_trans_handle *trans;
970
971         struct stat root_st;
972
973         struct directory_name_entry dir_head;
974
975         struct directory_name_entry *dir_entry = NULL;
976
977         ret = lstat(source_dir, &root_st);
978         if (ret) {
979                 fprintf(stderr, "unable to lstat the %s\n", source_dir);
980                 goto out;
981         }
982
983         INIT_LIST_HEAD(&dir_head.list);
984
985         trans = btrfs_start_transaction(root, 1);
986         ret = traverse_directory(trans, root, source_dir, &dir_head, out_fd);
987         if (ret) {
988                 fprintf(stderr, "unable to traverse_directory\n");
989                 goto fail;
990         }
991         btrfs_commit_transaction(trans, root);
992
993         if (verbose)
994                 printf("Making image is completed.\n");
995         return 0;
996 fail:
997         while (!list_empty(&dir_head.list)) {
998                 dir_entry = list_entry(dir_head.list.next,
999                                        struct directory_name_entry, list);
1000                 list_del(&dir_entry->list);
1001                 free(dir_entry);
1002         }
1003 out:
1004         fprintf(stderr, "Making image is aborted.\n");
1005         return -1;
1006 }
1007
1008 /*
1009  * This ignores symlinks with unreadable targets and subdirs that can't
1010  * be read.  It's a best-effort to give a rough estimate of the size of
1011  * a subdir.  It doesn't guarantee that prepopulating btrfs from this
1012  * tree won't still run out of space. 
1013  *
1014  * The rounding up to 4096 is questionable.  Previous code used du -B 4096.
1015  */
1016 static u64 global_total_size;
1017 static int ftw_add_entry_size(const char *fpath, const struct stat *st,
1018                               int type)
1019 {
1020         if (type == FTW_F || type == FTW_D)
1021                 global_total_size += round_up(st->st_size, 4096);
1022
1023         return 0;
1024 }
1025
1026 static u64 size_sourcedir(char *dir_name, u64 sectorsize,
1027                           u64 *num_of_meta_chunks_ret, u64 *size_of_data_ret)
1028 {
1029         u64 dir_size = 0;
1030         u64 total_size = 0;
1031         int ret;
1032         u64 default_chunk_size = 8 * 1024 * 1024;       /* 8MB */
1033         u64 allocated_meta_size = 8 * 1024 * 1024;      /* 8MB */
1034         u64 allocated_total_size = 20 * 1024 * 1024;    /* 20MB */
1035         u64 num_of_meta_chunks = 0;
1036         u64 num_of_data_chunks = 0;
1037         u64 num_of_allocated_meta_chunks =
1038                         allocated_meta_size / default_chunk_size;
1039
1040         global_total_size = 0;
1041         ret = ftw(dir_name, ftw_add_entry_size, 10);
1042         dir_size = global_total_size;
1043         if (ret < 0) {
1044                 fprintf(stderr, "ftw subdir walk of '%s' failed: %s\n",
1045                         dir_name, strerror(errno));
1046                 exit(1);
1047         }
1048
1049         num_of_data_chunks = (dir_size + default_chunk_size - 1) /
1050                 default_chunk_size;
1051
1052         num_of_meta_chunks = (dir_size / 2) / default_chunk_size;
1053         if (((dir_size / 2) % default_chunk_size) != 0)
1054                 num_of_meta_chunks++;
1055         if (num_of_meta_chunks <= num_of_allocated_meta_chunks)
1056                 num_of_meta_chunks = 0;
1057         else
1058                 num_of_meta_chunks -= num_of_allocated_meta_chunks;
1059
1060         total_size = allocated_total_size +
1061                      (num_of_data_chunks * default_chunk_size) +
1062                      (num_of_meta_chunks * default_chunk_size);
1063
1064         *num_of_meta_chunks_ret = num_of_meta_chunks;
1065         *size_of_data_ret = num_of_data_chunks * default_chunk_size;
1066         return total_size;
1067 }
1068
1069 static int zero_output_file(int out_fd, u64 size, u32 sectorsize)
1070 {
1071         int len = sectorsize;
1072         int loop_num = size / sectorsize;
1073         u64 location = 0;
1074         char *buf = malloc(len);
1075         int ret = 0, i;
1076         ssize_t written;
1077
1078         if (!buf)
1079                 return -ENOMEM;
1080         memset(buf, 0, len);
1081         for (i = 0; i < loop_num; i++) {
1082                 written = pwrite64(out_fd, buf, len, location);
1083                 if (written != len)
1084                         ret = -EIO;
1085                 location += sectorsize;
1086         }
1087         free(buf);
1088         return ret;
1089 }
1090
1091 static int is_ssd(const char *file)
1092 {
1093         blkid_probe probe;
1094         char wholedisk[32];
1095         char sysfs_path[PATH_MAX];
1096         dev_t devno;
1097         int fd;
1098         char rotational;
1099         int ret;
1100
1101         probe = blkid_new_probe_from_filename(file);
1102         if (!probe)
1103                 return 0;
1104
1105         /* Device number of this disk (possibly a partition) */
1106         devno = blkid_probe_get_devno(probe);
1107         if (!devno) {
1108                 blkid_free_probe(probe);
1109                 return 0;
1110         }
1111
1112         /* Get whole disk name (not full path) for this devno */
1113         ret = blkid_devno_to_wholedisk(devno,
1114                         wholedisk, sizeof(wholedisk), NULL);
1115         if (ret) {
1116                 blkid_free_probe(probe);
1117                 return 0;
1118         }
1119
1120         snprintf(sysfs_path, PATH_MAX, "/sys/block/%s/queue/rotational",
1121                  wholedisk);
1122
1123         blkid_free_probe(probe);
1124
1125         fd = open(sysfs_path, O_RDONLY);
1126         if (fd < 0) {
1127                 return 0;
1128         }
1129
1130         if (read(fd, &rotational, sizeof(char)) < sizeof(char)) {
1131                 close(fd);
1132                 return 0;
1133         }
1134         close(fd);
1135
1136         return !atoi((const char *)&rotational);
1137 }
1138
1139 static void list_all_devices(struct btrfs_root *root)
1140 {
1141         struct btrfs_fs_devices *fs_devices;
1142         struct btrfs_device *device;
1143         int number_of_devices = 0;
1144         u64 total_block_count = 0;
1145
1146         fs_devices = root->fs_info->fs_devices;
1147
1148         list_for_each_entry(device, &fs_devices->devices, dev_list)
1149                 number_of_devices++;
1150
1151         printf("Number of devices:  %d\n", number_of_devices);
1152         /* printf("Total devices size: %10s\n", */
1153                 /* pretty_size(total_block_count)); */
1154         printf("Devices:\n");
1155         printf("   ID        SIZE  PATH\n");
1156         list_for_each_entry_reverse(device, &fs_devices->devices, dev_list) {
1157                 char dev_uuid[BTRFS_UUID_UNPARSED_SIZE];
1158
1159                 uuid_unparse(device->uuid, dev_uuid);
1160                 printf("  %3llu  %10s  %s\n",
1161                         device->devid,
1162                         pretty_size(device->total_bytes),
1163                         device->name);
1164                 total_block_count += device->total_bytes;
1165         }
1166
1167         printf("\n");
1168 }
1169
1170 int main(int ac, char **av)
1171 {
1172         char *file;
1173         struct btrfs_root *root;
1174         struct btrfs_trans_handle *trans;
1175         char *label = NULL;
1176         u64 block_count = 0;
1177         u64 dev_block_count = 0;
1178         u64 blocks[7];
1179         u64 alloc_start = 0;
1180         u64 metadata_profile = 0;
1181         u64 data_profile = 0;
1182         u32 nodesize = max_t(u32, sysconf(_SC_PAGESIZE),
1183                         BTRFS_MKFS_DEFAULT_NODE_SIZE);
1184         u32 sectorsize = 4096;
1185         u32 stripesize = 4096;
1186         int zero_end = 1;
1187         int fd;
1188         int ret;
1189         int i;
1190         int mixed = 0;
1191         int nodesize_forced = 0;
1192         int data_profile_opt = 0;
1193         int metadata_profile_opt = 0;
1194         int discard = 1;
1195         int ssd = 0;
1196         int force_overwrite = 0;
1197         char *source_dir = NULL;
1198         int source_dir_set = 0;
1199         u64 num_of_meta_chunks = 0;
1200         u64 size_of_data = 0;
1201         u64 source_dir_size = 0;
1202         int dev_cnt = 0;
1203         int saved_optind;
1204         char fs_uuid[BTRFS_UUID_UNPARSED_SIZE] = { 0 };
1205         u64 features = BTRFS_MKFS_DEFAULT_FEATURES;
1206         struct mkfs_allocation allocation = { 0 };
1207         struct btrfs_mkfs_config mkfs_cfg;
1208
1209         while(1) {
1210                 int c;
1211                 static const struct option long_options[] = {
1212                         { "alloc-start", required_argument, NULL, 'A'},
1213                         { "byte-count", required_argument, NULL, 'b' },
1214                         { "force", no_argument, NULL, 'f' },
1215                         { "leafsize", required_argument, NULL, 'l' },
1216                         { "label", required_argument, NULL, 'L'},
1217                         { "metadata", required_argument, NULL, 'm' },
1218                         { "mixed", no_argument, NULL, 'M' },
1219                         { "nodesize", required_argument, NULL, 'n' },
1220                         { "sectorsize", required_argument, NULL, 's' },
1221                         { "data", required_argument, NULL, 'd' },
1222                         { "version", no_argument, NULL, 'V' },
1223                         { "rootdir", required_argument, NULL, 'r' },
1224                         { "nodiscard", no_argument, NULL, 'K' },
1225                         { "features", required_argument, NULL, 'O' },
1226                         { "uuid", required_argument, NULL, 'U' },
1227                         { "quiet", 0, NULL, 'q' },
1228                         { "help", no_argument, NULL, GETOPT_VAL_HELP },
1229                         { NULL, 0, NULL, 0}
1230                 };
1231
1232                 c = getopt_long(ac, av, "A:b:fl:n:s:m:d:L:O:r:U:VMKq",
1233                                 long_options, NULL);
1234                 if (c < 0)
1235                         break;
1236                 switch(c) {
1237                         case 'A':
1238                                 alloc_start = parse_size(optarg);
1239                                 break;
1240                         case 'f':
1241                                 force_overwrite = 1;
1242                                 break;
1243                         case 'd':
1244                                 data_profile = parse_profile(optarg);
1245                                 data_profile_opt = 1;
1246                                 break;
1247                         case 'l':
1248                                 fprintf(stderr,
1249                         "WARNING: --leafsize is deprecated, use --nodesize\n");
1250                         case 'n':
1251                                 nodesize = parse_size(optarg);
1252                                 nodesize_forced = 1;
1253                                 break;
1254                         case 'L':
1255                                 label = parse_label(optarg);
1256                                 break;
1257                         case 'm':
1258                                 metadata_profile = parse_profile(optarg);
1259                                 metadata_profile_opt = 1;
1260                                 break;
1261                         case 'M':
1262                                 mixed = 1;
1263                                 break;
1264                         case 'O': {
1265                                 char *orig = strdup(optarg);
1266                                 char *tmp = orig;
1267
1268                                 tmp = btrfs_parse_fs_features(tmp, &features);
1269                                 if (tmp) {
1270                                         fprintf(stderr,
1271                                                 "Unrecognized filesystem feature '%s'\n",
1272                                                         tmp);
1273                                         free(orig);
1274                                         exit(1);
1275                                 }
1276                                 free(orig);
1277                                 if (features & BTRFS_FEATURE_LIST_ALL) {
1278                                         btrfs_list_all_fs_features(0);
1279                                         exit(0);
1280                                 }
1281                                 break;
1282                                 }
1283                         case 's':
1284                                 sectorsize = parse_size(optarg);
1285                                 break;
1286                         case 'b':
1287                                 block_count = parse_size(optarg);
1288                                 if (block_count <= BTRFS_MKFS_SMALL_VOLUME_SIZE)
1289                                         mixed = 1;
1290                                 zero_end = 0;
1291                                 break;
1292                         case 'V':
1293                                 print_version();
1294                                 break;
1295                         case 'r':
1296                                 source_dir = optarg;
1297                                 source_dir_set = 1;
1298                                 break;
1299                         case 'U':
1300                                 strncpy(fs_uuid, optarg,
1301                                         BTRFS_UUID_UNPARSED_SIZE - 1);
1302                                 break;
1303                         case 'K':
1304                                 discard = 0;
1305                                 break;
1306                         case 'q':
1307                                 verbose = 0;
1308                                 break;
1309                         case GETOPT_VAL_HELP:
1310                         default:
1311                                 print_usage(c != GETOPT_VAL_HELP);
1312                 }
1313         }
1314         sectorsize = max(sectorsize, (u32)sysconf(_SC_PAGESIZE));
1315         if (btrfs_check_nodesize(nodesize, sectorsize))
1316                 exit(1);
1317         saved_optind = optind;
1318         dev_cnt = ac - optind;
1319         if (dev_cnt == 0)
1320                 print_usage(1);
1321
1322         if (source_dir_set && dev_cnt > 1) {
1323                 fprintf(stderr,
1324                         "The -r option is limited to a single device\n");
1325                 exit(1);
1326         }
1327
1328         if (*fs_uuid) {
1329                 uuid_t dummy_uuid;
1330
1331                 if (uuid_parse(fs_uuid, dummy_uuid) != 0) {
1332                         fprintf(stderr, "could not parse UUID: %s\n", fs_uuid);
1333                         exit(1);
1334                 }
1335                 if (!test_uuid_unique(fs_uuid)) {
1336                         fprintf(stderr, "non-unique UUID: %s\n", fs_uuid);
1337                         exit(1);
1338                 }
1339         }
1340         
1341         while (dev_cnt-- > 0) {
1342                 file = av[optind++];
1343                 if (is_block_device(file))
1344                         if (test_dev_for_mkfs(file, force_overwrite))
1345                                 exit(1);
1346         }
1347
1348         optind = saved_optind;
1349         dev_cnt = ac - optind;
1350
1351         file = av[optind++];
1352         ssd = is_ssd(file);
1353
1354         if (is_vol_small(file) || mixed) {
1355                 if (verbose)
1356                         printf("SMALL VOLUME: forcing mixed metadata/data groups\n");
1357                 mixed = 1;
1358         }
1359
1360         /*
1361         * Set default profiles according to number of added devices.
1362         * For mixed groups defaults are single/single.
1363         */
1364         if (!mixed) {
1365                 if (!metadata_profile_opt) {
1366                         if (dev_cnt == 1 && ssd && verbose)
1367                                 printf("Detected a SSD, turning off metadata "
1368                                 "duplication.  Mkfs with -m dup if you want to "
1369                                 "force metadata duplication.\n");
1370
1371                         metadata_profile = (dev_cnt > 1) ?
1372                                         BTRFS_BLOCK_GROUP_RAID1 : (ssd) ?
1373                                         0: BTRFS_BLOCK_GROUP_DUP;
1374                 }
1375                 if (!data_profile_opt) {
1376                         data_profile = (dev_cnt > 1) ?
1377                                 BTRFS_BLOCK_GROUP_RAID0 : 0; /* raid0 or single */
1378                 }
1379         } else {
1380                 u32 best_nodesize = max_t(u32, sysconf(_SC_PAGESIZE), sectorsize);
1381
1382                 if (metadata_profile_opt || data_profile_opt) {
1383                         if (metadata_profile != data_profile) {
1384                                 fprintf(stderr,
1385         "ERROR: With mixed block groups data and metadata profiles must be the same\n");
1386                                 exit(1);
1387                         }
1388                 }
1389
1390                 if (!nodesize_forced) {
1391                         nodesize = best_nodesize;
1392                         if (btrfs_check_nodesize(nodesize, sectorsize))
1393                                 exit(1);
1394                 }
1395                 if (nodesize != sectorsize) {
1396                         fprintf(stderr, "Error: mixed metadata/data block groups "
1397                                 "require metadata blocksizes equal to the sectorsize\n");
1398                         exit(1);
1399                 }
1400         }
1401
1402         /* Check device/block_count after the nodesize is determined */
1403         if (block_count && block_count < btrfs_min_dev_size(nodesize)) {
1404                 fprintf(stderr,
1405                         "Size '%llu' is too small to make a usable filesystem\n",
1406                         block_count);
1407                 fprintf(stderr,
1408                         "Minimum size for btrfs filesystem is %llu\n",
1409                         btrfs_min_dev_size(nodesize));
1410                 exit(1);
1411         }
1412         for (i = saved_optind; i < saved_optind + dev_cnt; i++) {
1413                 char *path;
1414
1415                 path = av[i];
1416                 ret = test_minimum_size(path, nodesize);
1417                 if (ret < 0) {
1418                         fprintf(stderr, "Failed to check size for '%s': %s\n",
1419                                 path, strerror(-ret));
1420                         exit (1);
1421                 }
1422                 if (ret > 0) {
1423                         fprintf(stderr,
1424                                 "'%s' is too small to make a usable filesystem\n",
1425                                 path);
1426                         fprintf(stderr,
1427                                 "Minimum size for each btrfs device is %llu.\n",
1428                                 btrfs_min_dev_size(nodesize));
1429                         exit(1);
1430                 }
1431         }
1432         ret = test_num_disk_vs_raid(metadata_profile, data_profile,
1433                         dev_cnt, mixed);
1434         if (ret)
1435                 exit(1);
1436
1437         /* if we are here that means all devs are good to btrfsify */
1438         if (verbose) {
1439                 printf("%s\n", PACKAGE_STRING);
1440                 printf("See %s for more information.\n\n", PACKAGE_URL);
1441         }
1442
1443         dev_cnt--;
1444
1445         if (!source_dir_set) {
1446                 /*
1447                  * open without O_EXCL so that the problem should not
1448                  * occur by the following processing.
1449                  * (btrfs_register_one_device() fails if O_EXCL is on)
1450                  */
1451                 fd = open(file, O_RDWR);
1452                 if (fd < 0) {
1453                         fprintf(stderr, "unable to open %s: %s\n", file,
1454                                 strerror(errno));
1455                         exit(1);
1456                 }
1457                 ret = btrfs_prepare_device(fd, file, zero_end, &dev_block_count,
1458                                            block_count, &mixed, discard);
1459                 if (ret) {
1460                         close(fd);
1461                         exit(1);
1462                 }
1463                 if (block_count && block_count > dev_block_count) {
1464                         fprintf(stderr, "%s is smaller than requested size\n", file);
1465                         exit(1);
1466                 }
1467         } else {
1468                 fd = open_target(file);
1469                 if (fd < 0) {
1470                         fprintf(stderr, "unable to open the %s\n", file);
1471                         exit(1);
1472                 }
1473
1474                 source_dir_size = size_sourcedir(source_dir, sectorsize,
1475                                              &num_of_meta_chunks, &size_of_data);
1476                 if(block_count < source_dir_size)
1477                         block_count = source_dir_size;
1478                 ret = zero_output_file(fd, block_count, sectorsize);
1479                 if (ret) {
1480                         fprintf(stderr, "unable to zero the output file\n");
1481                         exit(1);
1482                 }
1483                 /* our "device" is the new image file */
1484                 dev_block_count = block_count;
1485         }
1486
1487         /* To create the first block group and chunk 0 in make_btrfs */
1488         if (dev_block_count < BTRFS_MKFS_SYSTEM_GROUP_SIZE) {
1489                 fprintf(stderr, "device is too small to make filesystem\n");
1490                 exit(1);
1491         }
1492
1493         blocks[0] = BTRFS_SUPER_INFO_OFFSET;
1494         for (i = 1; i < 7; i++) {
1495                 blocks[i] = BTRFS_SUPER_INFO_OFFSET + 1024 * 1024 +
1496                         nodesize * i;
1497         }
1498
1499         if (group_profile_max_safe_loss(metadata_profile) <
1500                 group_profile_max_safe_loss(data_profile)){
1501                 fprintf(stderr,
1502                         "WARNING: metatdata has lower redundancy than data!\n\n");
1503         }
1504
1505         /*
1506          * FS features that can be set by other means than -O
1507          * just set the bit here
1508          */
1509         if (mixed)
1510                 features |= BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS;
1511
1512         if ((data_profile | metadata_profile) &
1513             (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6)) {
1514                 features |= BTRFS_FEATURE_INCOMPAT_RAID56;
1515         }
1516
1517         mkfs_cfg.label = label;
1518         mkfs_cfg.fs_uuid = fs_uuid;
1519         memcpy(mkfs_cfg.blocks, blocks, sizeof(blocks));
1520         mkfs_cfg.num_bytes = dev_block_count;
1521         mkfs_cfg.nodesize = nodesize;
1522         mkfs_cfg.sectorsize = sectorsize;
1523         mkfs_cfg.stripesize = stripesize;
1524         mkfs_cfg.features = features;
1525
1526         ret = make_btrfs(fd, &mkfs_cfg);
1527         if (ret) {
1528                 fprintf(stderr, "error during mkfs: %s\n", strerror(-ret));
1529                 exit(1);
1530         }
1531
1532         root = open_ctree(file, 0, OPEN_CTREE_WRITES);
1533         if (!root) {
1534                 fprintf(stderr, "Open ctree failed\n");
1535                 close(fd);
1536                 exit(1);
1537         }
1538         root->fs_info->alloc_start = alloc_start;
1539
1540         ret = create_metadata_block_groups(root, mixed, &allocation);
1541         if (ret) {
1542                 fprintf(stderr, "failed to create default block groups\n");
1543                 exit(1);
1544         }
1545
1546         trans = btrfs_start_transaction(root, 1);
1547         BUG_ON(!trans);
1548
1549         ret = make_root_dir(trans, root, mixed, &allocation);
1550         if (ret) {
1551                 fprintf(stderr, "failed to setup the root directory\n");
1552                 exit(1);
1553         }
1554
1555         btrfs_commit_transaction(trans, root);
1556
1557         trans = btrfs_start_transaction(root, 1);
1558
1559         if (is_block_device(file))
1560                 btrfs_register_one_device(file);
1561
1562         if (dev_cnt == 0)
1563                 goto raid_groups;
1564
1565         while (dev_cnt-- > 0) {
1566                 int old_mixed = mixed;
1567
1568                 file = av[optind++];
1569
1570                 /*
1571                  * open without O_EXCL so that the problem should not
1572                  * occur by the following processing.
1573                  * (btrfs_register_one_device() fails if O_EXCL is on)
1574                  */
1575                 fd = open(file, O_RDWR);
1576                 if (fd < 0) {
1577                         fprintf(stderr, "unable to open %s: %s\n", file,
1578                                 strerror(errno));
1579                         exit(1);
1580                 }
1581                 ret = btrfs_device_already_in_root(root, fd,
1582                                                    BTRFS_SUPER_INFO_OFFSET);
1583                 if (ret) {
1584                         fprintf(stderr, "skipping duplicate device %s in FS\n",
1585                                 file);
1586                         close(fd);
1587                         continue;
1588                 }
1589                 ret = btrfs_prepare_device(fd, file, zero_end, &dev_block_count,
1590                                            block_count, &mixed, discard);
1591                 if (ret) {
1592                         close(fd);
1593                         exit(1);
1594                 }
1595                 mixed = old_mixed;
1596
1597                 ret = btrfs_add_to_fsid(trans, root, fd, file, dev_block_count,
1598                                         sectorsize, sectorsize, sectorsize);
1599                 BUG_ON(ret);
1600                 if (verbose >= 2) {
1601                         struct btrfs_device *device;
1602
1603                         device = container_of(root->fs_info->fs_devices->devices.next,
1604                                         struct btrfs_device, dev_list);
1605                         printf("adding device %s id %llu\n", file,
1606                                 (unsigned long long)device->devid);
1607                 }
1608
1609                 if (is_block_device(file))
1610                         btrfs_register_one_device(file);
1611         }
1612
1613 raid_groups:
1614         if (!source_dir_set) {
1615                 ret = create_raid_groups(trans, root, data_profile,
1616                                  data_profile_opt, metadata_profile,
1617                                  mixed, &allocation);
1618                 BUG_ON(ret);
1619         }
1620
1621         ret = create_data_reloc_tree(trans, root);
1622         BUG_ON(ret);
1623
1624         btrfs_commit_transaction(trans, root);
1625
1626         if (source_dir_set) {
1627                 trans = btrfs_start_transaction(root, 1);
1628                 ret = create_chunks(trans, root,
1629                                     num_of_meta_chunks, size_of_data,
1630                                     &allocation);
1631                 BUG_ON(ret);
1632                 btrfs_commit_transaction(trans, root);
1633
1634                 ret = make_image(source_dir, root, fd);
1635                 BUG_ON(ret);
1636         }
1637
1638         if (verbose) {
1639                 char features_buf[64];
1640
1641                 printf("Label:              %s\n", label);
1642                 printf("UUID:               %s\n", fs_uuid);
1643                 printf("Node size:          %u\n", nodesize);
1644                 printf("Sector size:        %u\n", sectorsize);
1645                 printf("Filesystem size:    %s\n",
1646                         pretty_size(btrfs_super_total_bytes(root->fs_info->super_copy)));
1647                 printf("Block group profiles:\n");
1648                 if (allocation.data)
1649                         printf("  Data:             %-8s %16s\n",
1650                                 btrfs_group_profile_str(data_profile),
1651                                 pretty_size(allocation.data));
1652                 if (allocation.metadata)
1653                         printf("  Metadata:         %-8s %16s\n",
1654                                 btrfs_group_profile_str(metadata_profile),
1655                                 pretty_size(allocation.metadata));
1656                 if (allocation.mixed)
1657                         printf("  Data+Metadata:    %-8s %16s\n",
1658                                 btrfs_group_profile_str(data_profile),
1659                                 pretty_size(allocation.mixed));
1660                 printf("  System:           %-8s %16s\n",
1661                         btrfs_group_profile_str(metadata_profile),
1662                         pretty_size(allocation.system));
1663                 printf("SSD detected:       %s\n", ssd ? "yes" : "no");
1664                 btrfs_parse_features_to_string(features_buf, features);
1665                 printf("Incompat features:  %s", features_buf);
1666                 printf("\n");
1667
1668                 list_all_devices(root);
1669         }
1670
1671         ret = close_ctree(root);
1672         BUG_ON(ret);
1673         free(label);
1674         return 0;
1675 }