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