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