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