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