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