packaging: Enable LTO and set visibility to hidden
[platform/upstream/btrfs-progs.git] / mkfs / main.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/dir.h> included via androidcompat.h */
28 #include <fcntl.h>
29 #include <limits.h>
30 #include <unistd.h>
31 #include <getopt.h>
32 #include <uuid/uuid.h>
33 #include <ctype.h>
34 #include <blkid/blkid.h>
35 #include "ctree.h"
36 #include "disk-io.h"
37 #include "volumes.h"
38 #include "transaction.h"
39 #include "utils.h"
40 #include "list_sort.h"
41 #include "help.h"
42 #include "mkfs/common.h"
43 #include "mkfs/rootdir.h"
44 #include "fsfeatures.h"
45
46 static int verbose = 1;
47
48 struct mkfs_allocation {
49         u64 data;
50         u64 metadata;
51         u64 mixed;
52         u64 system;
53 };
54
55 static int create_metadata_block_groups(struct btrfs_root *root, int mixed,
56                                 struct mkfs_allocation *allocation)
57 {
58         struct btrfs_fs_info *fs_info = root->fs_info;
59         struct btrfs_trans_handle *trans;
60         u64 bytes_used;
61         u64 chunk_start = 0;
62         u64 chunk_size = 0;
63         int ret;
64
65         trans = btrfs_start_transaction(root, 1);
66         BUG_ON(IS_ERR(trans));
67         bytes_used = btrfs_super_bytes_used(fs_info->super_copy);
68
69         root->fs_info->system_allocs = 1;
70         /*
71          * First temporary system chunk must match the chunk layout
72          * created in make_btrfs().
73          */
74         ret = btrfs_make_block_group(trans, fs_info, bytes_used,
75                                      BTRFS_BLOCK_GROUP_SYSTEM,
76                                      BTRFS_BLOCK_RESERVED_1M_FOR_SUPER,
77                                      BTRFS_MKFS_SYSTEM_GROUP_SIZE);
78         allocation->system += BTRFS_MKFS_SYSTEM_GROUP_SIZE;
79         if (ret)
80                 return ret;
81
82         if (mixed) {
83                 ret = btrfs_alloc_chunk(trans, fs_info,
84                                         &chunk_start, &chunk_size,
85                                         BTRFS_BLOCK_GROUP_METADATA |
86                                         BTRFS_BLOCK_GROUP_DATA);
87                 if (ret == -ENOSPC) {
88                         error("no space to allocate data/metadata chunk");
89                         goto err;
90                 }
91                 if (ret)
92                         return ret;
93                 ret = btrfs_make_block_group(trans, fs_info, 0,
94                                              BTRFS_BLOCK_GROUP_METADATA |
95                                              BTRFS_BLOCK_GROUP_DATA,
96                                              chunk_start, chunk_size);
97                 if (ret)
98                         return ret;
99                 allocation->mixed += chunk_size;
100         } else {
101                 ret = btrfs_alloc_chunk(trans, fs_info,
102                                         &chunk_start, &chunk_size,
103                                         BTRFS_BLOCK_GROUP_METADATA);
104                 if (ret == -ENOSPC) {
105                         error("no space to allocate metadata chunk");
106                         goto err;
107                 }
108                 if (ret)
109                         return ret;
110                 ret = btrfs_make_block_group(trans, fs_info, 0,
111                                              BTRFS_BLOCK_GROUP_METADATA,
112                                              chunk_start, chunk_size);
113                 allocation->metadata += chunk_size;
114                 if (ret)
115                         return ret;
116         }
117
118         root->fs_info->system_allocs = 0;
119         ret = btrfs_commit_transaction(trans, root);
120
121 err:
122         return ret;
123 }
124
125 static int create_data_block_groups(struct btrfs_trans_handle *trans,
126                 struct btrfs_root *root, int mixed,
127                 struct mkfs_allocation *allocation)
128 {
129         struct btrfs_fs_info *fs_info = root->fs_info;
130         u64 chunk_start = 0;
131         u64 chunk_size = 0;
132         int ret = 0;
133
134         if (!mixed) {
135                 ret = btrfs_alloc_chunk(trans, fs_info,
136                                         &chunk_start, &chunk_size,
137                                         BTRFS_BLOCK_GROUP_DATA);
138                 if (ret == -ENOSPC) {
139                         error("no space to allocate data chunk");
140                         goto err;
141                 }
142                 if (ret)
143                         return ret;
144                 ret = btrfs_make_block_group(trans, fs_info, 0,
145                                              BTRFS_BLOCK_GROUP_DATA,
146                                              chunk_start, chunk_size);
147                 allocation->data += chunk_size;
148                 if (ret)
149                         return ret;
150         }
151
152 err:
153         return ret;
154 }
155
156 static int make_root_dir(struct btrfs_trans_handle *trans,
157                 struct btrfs_root *root)
158 {
159         struct btrfs_key location;
160         int ret;
161
162         ret = btrfs_make_root_dir(trans, root->fs_info->tree_root,
163                               BTRFS_ROOT_TREE_DIR_OBJECTID);
164         if (ret)
165                 goto err;
166         ret = btrfs_make_root_dir(trans, root, BTRFS_FIRST_FREE_OBJECTID);
167         if (ret)
168                 goto err;
169         memcpy(&location, &root->fs_info->fs_root->root_key, sizeof(location));
170         location.offset = (u64)-1;
171         ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
172                         "default", 7,
173                         btrfs_super_root_dir(root->fs_info->super_copy),
174                         &location, BTRFS_FT_DIR, 0);
175         if (ret)
176                 goto err;
177
178         ret = btrfs_insert_inode_ref(trans, root->fs_info->tree_root,
179                              "default", 7, location.objectid,
180                              BTRFS_ROOT_TREE_DIR_OBJECTID, 0);
181         if (ret)
182                 goto err;
183
184 err:
185         return ret;
186 }
187
188 static int __recow_root(struct btrfs_trans_handle *trans,
189                          struct btrfs_root *root)
190 {
191         struct extent_buffer *tmp;
192         int ret;
193
194         if (trans->transid != btrfs_root_generation(&root->root_item)) {
195                 extent_buffer_get(root->node);
196                 ret = __btrfs_cow_block(trans, root, root->node,
197                                         NULL, 0, &tmp, 0, 0);
198                 if (ret)
199                         return ret;
200                 free_extent_buffer(tmp);
201         }
202
203         return 0;
204 }
205
206 static int recow_roots(struct btrfs_trans_handle *trans,
207                        struct btrfs_root *root)
208 {
209         struct btrfs_fs_info *info = root->fs_info;
210         int ret;
211
212         ret = __recow_root(trans, info->fs_root);
213         if (ret)
214                 return ret;
215         ret = __recow_root(trans, info->tree_root);
216         if (ret)
217                 return ret;
218         ret = __recow_root(trans, info->extent_root);
219         if (ret)
220                 return ret;
221         ret = __recow_root(trans, info->chunk_root);
222         if (ret)
223                 return ret;
224         ret = __recow_root(trans, info->dev_root);
225         if (ret)
226                 return ret;
227         ret = __recow_root(trans, info->csum_root);
228         if (ret)
229                 return ret;
230
231         return 0;
232 }
233
234 static int create_one_raid_group(struct btrfs_trans_handle *trans,
235                               struct btrfs_root *root, u64 type,
236                               struct mkfs_allocation *allocation)
237
238 {
239         struct btrfs_fs_info *fs_info = root->fs_info;
240         u64 chunk_start;
241         u64 chunk_size;
242         int ret;
243
244         ret = btrfs_alloc_chunk(trans, fs_info,
245                                 &chunk_start, &chunk_size, type);
246         if (ret == -ENOSPC) {
247                 error("not enough free space to allocate chunk");
248                 exit(1);
249         }
250         if (ret)
251                 return ret;
252
253         ret = btrfs_make_block_group(trans, fs_info, 0,
254                                      type, chunk_start, chunk_size);
255
256         type &= BTRFS_BLOCK_GROUP_TYPE_MASK;
257         if (type == BTRFS_BLOCK_GROUP_DATA) {
258                 allocation->data += chunk_size;
259         } else if (type == BTRFS_BLOCK_GROUP_METADATA) {
260                 allocation->metadata += chunk_size;
261         } else if (type == BTRFS_BLOCK_GROUP_SYSTEM) {
262                 allocation->system += chunk_size;
263         } else if (type ==
264                         (BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_DATA)) {
265                 allocation->mixed += chunk_size;
266         } else {
267                 error("unrecognized profile type: 0x%llx",
268                                 (unsigned long long)type);
269                 ret = -EINVAL;
270         }
271
272         return ret;
273 }
274
275 static int create_raid_groups(struct btrfs_trans_handle *trans,
276                               struct btrfs_root *root, u64 data_profile,
277                               u64 metadata_profile, int mixed,
278                               struct mkfs_allocation *allocation)
279 {
280         int ret;
281
282         if (metadata_profile) {
283                 u64 meta_flags = BTRFS_BLOCK_GROUP_METADATA;
284
285                 ret = create_one_raid_group(trans, root,
286                                             BTRFS_BLOCK_GROUP_SYSTEM |
287                                             metadata_profile, allocation);
288                 if (ret)
289                         return ret;
290
291                 if (mixed)
292                         meta_flags |= BTRFS_BLOCK_GROUP_DATA;
293
294                 ret = create_one_raid_group(trans, root, meta_flags |
295                                             metadata_profile, allocation);
296                 if (ret)
297                         return ret;
298
299         }
300         if (!mixed && data_profile) {
301                 ret = create_one_raid_group(trans, root,
302                                             BTRFS_BLOCK_GROUP_DATA |
303                                             data_profile, allocation);
304                 if (ret)
305                         return ret;
306         }
307         ret = recow_roots(trans, root);
308
309         return ret;
310 }
311
312 static int create_tree(struct btrfs_trans_handle *trans,
313                         struct btrfs_root *root, u64 objectid)
314 {
315         struct btrfs_key location;
316         struct btrfs_root_item root_item;
317         struct extent_buffer *tmp;
318         u8 uuid[BTRFS_UUID_SIZE] = {0};
319         int ret;
320
321         ret = btrfs_copy_root(trans, root, root->node, &tmp, objectid);
322         if (ret)
323                 return ret;
324
325         memcpy(&root_item, &root->root_item, sizeof(root_item));
326         btrfs_set_root_bytenr(&root_item, tmp->start);
327         btrfs_set_root_level(&root_item, btrfs_header_level(tmp));
328         btrfs_set_root_generation(&root_item, trans->transid);
329         /* clear uuid and o/ctime of source tree */
330         memcpy(root_item.uuid, uuid, BTRFS_UUID_SIZE);
331         btrfs_set_stack_timespec_sec(&root_item.otime, 0);
332         btrfs_set_stack_timespec_sec(&root_item.ctime, 0);
333         free_extent_buffer(tmp);
334
335         location.objectid = objectid;
336         location.type = BTRFS_ROOT_ITEM_KEY;
337         location.offset = 0;
338         ret = btrfs_insert_root(trans, root->fs_info->tree_root,
339                                 &location, &root_item);
340
341         return ret;
342 }
343
344 static void print_usage(int ret)
345 {
346         printf("Usage: mkfs.btrfs [options] dev [ dev ... ]\n");
347         printf("Options:\n");
348         printf("  allocation profiles:\n");
349         printf("\t-d|--data PROFILE       data profile, raid0, raid1, raid5, raid6, raid10, dup or single\n");
350         printf("\t-m|--metadata PROFILE   metadata profile, values like for data profile\n");
351         printf("\t-M|--mixed              mix metadata and data together\n");
352         printf("  features:\n");
353         printf("\t-n|--nodesize SIZE      size of btree nodes\n");
354         printf("\t-s|--sectorsize SIZE    data block size (may not be mountable by current kernel)\n");
355         printf("\t-O|--features LIST      comma separated list of filesystem features (use '-O list-all' to list features)\n");
356         printf("\t-L|--label LABEL        set the filesystem label\n");
357         printf("\t-U|--uuid UUID          specify the filesystem UUID (must be unique)\n");
358         printf("  creation:\n");
359         printf("\t-b|--byte-count SIZE    set filesystem size to SIZE (on the first device)\n");
360         printf("\t-r|--rootdir DIR        copy files from DIR to the image root directory\n");
361         printf("\t--shrink                (with --rootdir) shrink the filled filesystem to minimal size\n");
362         printf("\t-K|--nodiscard          do not perform whole device TRIM\n");
363         printf("\t-f|--force              force overwrite of existing filesystem\n");
364         printf("  general:\n");
365         printf("\t-q|--quiet              no messages except errors\n");
366         printf("\t-V|--version            print the mkfs.btrfs version and exit\n");
367         printf("\t--help                  print this help and exit\n");
368         printf("  deprecated:\n");
369         printf("\t-A|--alloc-start START  the offset to start the filesystem\n");
370         printf("\t-l|--leafsize SIZE      deprecated, alias for nodesize\n");
371         exit(ret);
372 }
373
374 static u64 parse_profile(const char *s)
375 {
376         if (strcasecmp(s, "raid0") == 0) {
377                 return BTRFS_BLOCK_GROUP_RAID0;
378         } else if (strcasecmp(s, "raid1") == 0) {
379                 return BTRFS_BLOCK_GROUP_RAID1;
380         } else if (strcasecmp(s, "raid5") == 0) {
381                 return BTRFS_BLOCK_GROUP_RAID5;
382         } else if (strcasecmp(s, "raid6") == 0) {
383                 return BTRFS_BLOCK_GROUP_RAID6;
384         } else if (strcasecmp(s, "raid10") == 0) {
385                 return BTRFS_BLOCK_GROUP_RAID10;
386         } else if (strcasecmp(s, "dup") == 0) {
387                 return BTRFS_BLOCK_GROUP_DUP;
388         } else if (strcasecmp(s, "single") == 0) {
389                 return 0;
390         } else {
391                 error("unknown profile %s", s);
392                 exit(1);
393         }
394         /* not reached */
395         return 0;
396 }
397
398 static char *parse_label(const char *input)
399 {
400         int len = strlen(input);
401
402         if (len >= BTRFS_LABEL_SIZE) {
403                 error("label %s is too long (max %d)", input,
404                         BTRFS_LABEL_SIZE - 1);
405                 exit(1);
406         }
407         return strdup(input);
408 }
409
410 static int zero_output_file(int out_fd, u64 size)
411 {
412         int loop_num;
413         u64 location = 0;
414         char buf[SZ_4K];
415         int ret = 0, i;
416         ssize_t written;
417
418         memset(buf, 0, SZ_4K);
419
420         /* Only zero out the first 1M */
421         loop_num = SZ_1M / SZ_4K;
422         for (i = 0; i < loop_num; i++) {
423                 written = pwrite64(out_fd, buf, SZ_4K, location);
424                 if (written != SZ_4K)
425                         ret = -EIO;
426                 location += SZ_4K;
427         }
428
429         /* Then enlarge the file to size */
430         written = pwrite64(out_fd, buf, 1, size - 1);
431         if (written < 1)
432                 ret = -EIO;
433         return ret;
434 }
435
436 static int is_ssd(const char *file)
437 {
438         blkid_probe probe;
439         char wholedisk[PATH_MAX];
440         char sysfs_path[PATH_MAX];
441         dev_t devno;
442         int fd;
443         char rotational;
444         int ret;
445
446         probe = blkid_new_probe_from_filename(file);
447         if (!probe)
448                 return 0;
449
450         /* Device number of this disk (possibly a partition) */
451         devno = blkid_probe_get_devno(probe);
452         if (!devno) {
453                 blkid_free_probe(probe);
454                 return 0;
455         }
456
457         /* Get whole disk name (not full path) for this devno */
458         ret = blkid_devno_to_wholedisk(devno,
459                         wholedisk, sizeof(wholedisk), NULL);
460         if (ret) {
461                 blkid_free_probe(probe);
462                 return 0;
463         }
464
465         snprintf(sysfs_path, PATH_MAX, "/sys/block/%s/queue/rotational",
466                  wholedisk);
467
468         blkid_free_probe(probe);
469
470         fd = open(sysfs_path, O_RDONLY);
471         if (fd < 0) {
472                 return 0;
473         }
474
475         if (read(fd, &rotational, 1) < 1) {
476                 close(fd);
477                 return 0;
478         }
479         close(fd);
480
481         return rotational == '0';
482 }
483
484 static int _cmp_device_by_id(void *priv, struct list_head *a,
485                              struct list_head *b)
486 {
487         return list_entry(a, struct btrfs_device, dev_list)->devid -
488                list_entry(b, struct btrfs_device, dev_list)->devid;
489 }
490
491 static void list_all_devices(struct btrfs_root *root)
492 {
493         struct btrfs_fs_devices *fs_devices;
494         struct btrfs_device *device;
495         int number_of_devices = 0;
496         u64 total_block_count = 0;
497
498         fs_devices = root->fs_info->fs_devices;
499
500         list_for_each_entry(device, &fs_devices->devices, dev_list)
501                 number_of_devices++;
502
503         list_sort(NULL, &fs_devices->devices, _cmp_device_by_id);
504
505         printf("Number of devices:  %d\n", number_of_devices);
506         /* printf("Total devices size: %10s\n", */
507                 /* pretty_size(total_block_count)); */
508         printf("Devices:\n");
509         printf("   ID        SIZE  PATH\n");
510         list_for_each_entry(device, &fs_devices->devices, dev_list) {
511                 printf("  %3llu  %10s  %s\n",
512                         device->devid,
513                         pretty_size(device->total_bytes),
514                         device->name);
515                 total_block_count += device->total_bytes;
516         }
517
518         printf("\n");
519 }
520
521 static int is_temp_block_group(struct extent_buffer *node,
522                                struct btrfs_block_group_item *bgi,
523                                u64 data_profile, u64 meta_profile,
524                                u64 sys_profile)
525 {
526         u64 flag = btrfs_disk_block_group_flags(node, bgi);
527         u64 flag_type = flag & BTRFS_BLOCK_GROUP_TYPE_MASK;
528         u64 flag_profile = flag & BTRFS_BLOCK_GROUP_PROFILE_MASK;
529         u64 used = btrfs_disk_block_group_used(node, bgi);
530
531         /*
532          * Chunks meets all the following conditions is a temp chunk
533          * 1) Empty chunk
534          * Temp chunk is always empty.
535          *
536          * 2) profile mismatch with mkfs profile.
537          * Temp chunk is always in SINGLE
538          *
539          * 3) Size differs with mkfs_alloc
540          * Special case for SINGLE/SINGLE btrfs.
541          * In that case, temp data chunk and real data chunk are always empty.
542          * So we need to use mkfs_alloc to be sure which chunk is the newly
543          * allocated.
544          *
545          * Normally, new chunk size is equal to mkfs one (One chunk)
546          * If it has multiple chunks, we just refuse to delete any one.
547          * As they are all single, so no real problem will happen.
548          * So only use condition 1) and 2) to judge them.
549          */
550         if (used != 0)
551                 return 0;
552         switch (flag_type) {
553         case BTRFS_BLOCK_GROUP_DATA:
554         case BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA:
555                 data_profile &= BTRFS_BLOCK_GROUP_PROFILE_MASK;
556                 if (flag_profile != data_profile)
557                         return 1;
558                 break;
559         case BTRFS_BLOCK_GROUP_METADATA:
560                 meta_profile &= BTRFS_BLOCK_GROUP_PROFILE_MASK;
561                 if (flag_profile != meta_profile)
562                         return 1;
563                 break;
564         case BTRFS_BLOCK_GROUP_SYSTEM:
565                 sys_profile &= BTRFS_BLOCK_GROUP_PROFILE_MASK;
566                 if (flag_profile != sys_profile)
567                         return 1;
568                 break;
569         }
570         return 0;
571 }
572
573 /* Note: if current is a block group, it will skip it anyway */
574 static int next_block_group(struct btrfs_root *root,
575                             struct btrfs_path *path)
576 {
577         struct btrfs_key key;
578         int ret = 0;
579
580         while (1) {
581                 ret = btrfs_next_item(root, path);
582                 if (ret)
583                         goto out;
584
585                 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
586                 if (key.type == BTRFS_BLOCK_GROUP_ITEM_KEY)
587                         goto out;
588         }
589 out:
590         return ret;
591 }
592
593 /* This function will cleanup  */
594 static int cleanup_temp_chunks(struct btrfs_fs_info *fs_info,
595                                struct mkfs_allocation *alloc,
596                                u64 data_profile, u64 meta_profile,
597                                u64 sys_profile)
598 {
599         struct btrfs_trans_handle *trans = NULL;
600         struct btrfs_block_group_item *bgi;
601         struct btrfs_root *root = fs_info->extent_root;
602         struct btrfs_key key;
603         struct btrfs_key found_key;
604         struct btrfs_path path;
605         int ret = 0;
606
607         btrfs_init_path(&path);
608         trans = btrfs_start_transaction(root, 1);
609         BUG_ON(IS_ERR(trans));
610
611         key.objectid = 0;
612         key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
613         key.offset = 0;
614
615         while (1) {
616                 /*
617                  * as the rest of the loop may modify the tree, we need to
618                  * start a new search each time.
619                  */
620                 ret = btrfs_search_slot(trans, root, &key, &path, 0, 0);
621                 if (ret < 0)
622                         goto out;
623                 /* Don't pollute ret for >0 case */
624                 if (ret > 0)
625                         ret = 0;
626
627                 btrfs_item_key_to_cpu(path.nodes[0], &found_key,
628                                       path.slots[0]);
629                 if (found_key.objectid < key.objectid)
630                         goto out;
631                 if (found_key.type != BTRFS_BLOCK_GROUP_ITEM_KEY) {
632                         ret = next_block_group(root, &path);
633                         if (ret < 0)
634                                 goto out;
635                         if (ret > 0) {
636                                 ret = 0;
637                                 goto out;
638                         }
639                         btrfs_item_key_to_cpu(path.nodes[0], &found_key,
640                                               path.slots[0]);
641                 }
642
643                 bgi = btrfs_item_ptr(path.nodes[0], path.slots[0],
644                                      struct btrfs_block_group_item);
645                 if (is_temp_block_group(path.nodes[0], bgi,
646                                         data_profile, meta_profile,
647                                         sys_profile)) {
648                         u64 flags = btrfs_disk_block_group_flags(path.nodes[0],
649                                                              bgi);
650
651                         ret = btrfs_free_block_group(trans, fs_info,
652                                         found_key.objectid, found_key.offset);
653                         if (ret < 0)
654                                 goto out;
655
656                         if ((flags & BTRFS_BLOCK_GROUP_TYPE_MASK) ==
657                             BTRFS_BLOCK_GROUP_DATA)
658                                 alloc->data -= found_key.offset;
659                         else if ((flags & BTRFS_BLOCK_GROUP_TYPE_MASK) ==
660                                  BTRFS_BLOCK_GROUP_METADATA)
661                                 alloc->metadata -= found_key.offset;
662                         else if ((flags & BTRFS_BLOCK_GROUP_TYPE_MASK) ==
663                                  BTRFS_BLOCK_GROUP_SYSTEM)
664                                 alloc->system -= found_key.offset;
665                         else if ((flags & BTRFS_BLOCK_GROUP_TYPE_MASK) ==
666                                  (BTRFS_BLOCK_GROUP_METADATA |
667                                   BTRFS_BLOCK_GROUP_DATA))
668                                 alloc->mixed -= found_key.offset;
669                 }
670                 btrfs_release_path(&path);
671                 key.objectid = found_key.objectid + found_key.offset;
672         }
673 out:
674         if (trans)
675                 btrfs_commit_transaction(trans, root);
676         btrfs_release_path(&path);
677         return ret;
678 }
679
680 /*
681  * Just update chunk allocation info, since --rootdir may allocate new
682  * chunks which is not updated in @allocation structure.
683  */
684 static void update_chunk_allocation(struct btrfs_fs_info *fs_info,
685                                     struct mkfs_allocation *allocation)
686 {
687         struct btrfs_block_group_cache *bg_cache;
688         const u64 mixed_flag = BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA;
689         u64 search_start = 0;
690
691         allocation->mixed = 0;
692         allocation->data = 0;
693         allocation->metadata = 0;
694         allocation->system = 0;
695         while (1) {
696                 bg_cache = btrfs_lookup_first_block_group(fs_info,
697                                                           search_start);
698                 if (!bg_cache)
699                         break;
700                 if ((bg_cache->flags & mixed_flag) == mixed_flag)
701                         allocation->mixed += bg_cache->key.offset;
702                 else if (bg_cache->flags & BTRFS_BLOCK_GROUP_DATA)
703                         allocation->data += bg_cache->key.offset;
704                 else if (bg_cache->flags & BTRFS_BLOCK_GROUP_METADATA)
705                         allocation->metadata += bg_cache->key.offset;
706                 else
707                         allocation->system += bg_cache->key.offset;
708                 search_start = bg_cache->key.objectid + bg_cache->key.offset;
709         }
710 }
711
712 int main(int argc, char **argv)
713 {
714         char *file;
715         struct btrfs_root *root;
716         struct btrfs_fs_info *fs_info;
717         struct btrfs_trans_handle *trans;
718         char *label = NULL;
719         u64 block_count = 0;
720         u64 dev_block_count = 0;
721         u64 alloc_start = 0;
722         u64 metadata_profile = 0;
723         u64 data_profile = 0;
724         u32 nodesize = max_t(u32, sysconf(_SC_PAGESIZE),
725                         BTRFS_MKFS_DEFAULT_NODE_SIZE);
726         u32 sectorsize = 4096;
727         u32 stripesize = 4096;
728         int zero_end = 1;
729         int fd = -1;
730         int ret = 0;
731         int close_ret;
732         int i;
733         int mixed = 0;
734         int nodesize_forced = 0;
735         int data_profile_opt = 0;
736         int metadata_profile_opt = 0;
737         int discard = 1;
738         int ssd = 0;
739         int force_overwrite = 0;
740         char *source_dir = NULL;
741         bool source_dir_set = false;
742         bool shrink_rootdir = false;
743         u64 source_dir_size = 0;
744         u64 min_dev_size;
745         u64 shrink_size;
746         int dev_cnt = 0;
747         int saved_optind;
748         char fs_uuid[BTRFS_UUID_UNPARSED_SIZE] = { 0 };
749         u64 features = BTRFS_MKFS_DEFAULT_FEATURES;
750         struct mkfs_allocation allocation = { 0 };
751         struct btrfs_mkfs_config mkfs_cfg;
752
753         while(1) {
754                 int c;
755                 enum { GETOPT_VAL_SHRINK = 257 };
756                 static const struct option long_options[] = {
757                         { "alloc-start", required_argument, NULL, 'A'},
758                         { "byte-count", required_argument, NULL, 'b' },
759                         { "force", no_argument, NULL, 'f' },
760                         { "leafsize", required_argument, NULL, 'l' },
761                         { "label", required_argument, NULL, 'L'},
762                         { "metadata", required_argument, NULL, 'm' },
763                         { "mixed", no_argument, NULL, 'M' },
764                         { "nodesize", required_argument, NULL, 'n' },
765                         { "sectorsize", required_argument, NULL, 's' },
766                         { "data", required_argument, NULL, 'd' },
767                         { "version", no_argument, NULL, 'V' },
768                         { "rootdir", required_argument, NULL, 'r' },
769                         { "nodiscard", no_argument, NULL, 'K' },
770                         { "features", required_argument, NULL, 'O' },
771                         { "uuid", required_argument, NULL, 'U' },
772                         { "quiet", 0, NULL, 'q' },
773                         { "shrink", no_argument, NULL, GETOPT_VAL_SHRINK },
774                         { "help", no_argument, NULL, GETOPT_VAL_HELP },
775                         { NULL, 0, NULL, 0}
776                 };
777
778                 c = getopt_long(argc, argv, "A:b:fl:n:s:m:d:L:O:r:U:VMKq",
779                                 long_options, NULL);
780                 if (c < 0)
781                         break;
782                 switch(c) {
783                         case 'A':
784                                 alloc_start = parse_size(optarg);
785                                 break;
786                         case 'f':
787                                 force_overwrite = 1;
788                                 break;
789                         case 'd':
790                                 data_profile = parse_profile(optarg);
791                                 data_profile_opt = 1;
792                                 break;
793                         case 'l':
794                                 warning("--leafsize is deprecated, use --nodesize");
795                                 /* fall through */
796                         case 'n':
797                                 nodesize = parse_size(optarg);
798                                 nodesize_forced = 1;
799                                 break;
800                         case 'L':
801                                 label = parse_label(optarg);
802                                 break;
803                         case 'm':
804                                 metadata_profile = parse_profile(optarg);
805                                 metadata_profile_opt = 1;
806                                 break;
807                         case 'M':
808                                 mixed = 1;
809                                 break;
810                         case 'O': {
811                                 char *orig = strdup(optarg);
812                                 char *tmp = orig;
813
814                                 tmp = btrfs_parse_fs_features(tmp, &features);
815                                 if (tmp) {
816                                         error("unrecognized filesystem feature '%s'",
817                                                         tmp);
818                                         free(orig);
819                                         goto error;
820                                 }
821                                 free(orig);
822                                 if (features & BTRFS_FEATURE_LIST_ALL) {
823                                         btrfs_list_all_fs_features(0);
824                                         goto success;
825                                 }
826                                 break;
827                                 }
828                         case 's':
829                                 sectorsize = parse_size(optarg);
830                                 break;
831                         case 'b':
832                                 block_count = parse_size(optarg);
833                                 zero_end = 0;
834                                 break;
835                         case 'V':
836                                 printf("mkfs.btrfs, part of %s\n",
837                                                 PACKAGE_STRING);
838                                 goto success;
839                         case 'r':
840                                 source_dir = optarg;
841                                 source_dir_set = true;
842                                 break;
843                         case 'U':
844                                 strncpy(fs_uuid, optarg,
845                                         BTRFS_UUID_UNPARSED_SIZE - 1);
846                                 break;
847                         case 'K':
848                                 discard = 0;
849                                 break;
850                         case 'q':
851                                 verbose = 0;
852                                 break;
853                         case GETOPT_VAL_SHRINK:
854                                 shrink_rootdir = true;
855                                 break;
856                         case GETOPT_VAL_HELP:
857                         default:
858                                 print_usage(c != GETOPT_VAL_HELP);
859                 }
860         }
861
862         if (verbose) {
863                 printf("%s\n", PACKAGE_STRING);
864                 printf("See %s for more information.\n\n", PACKAGE_URL);
865         }
866
867         sectorsize = max(sectorsize, (u32)sysconf(_SC_PAGESIZE));
868         stripesize = sectorsize;
869         saved_optind = optind;
870         dev_cnt = argc - optind;
871         if (dev_cnt == 0)
872                 print_usage(1);
873
874         if (source_dir_set && dev_cnt > 1) {
875                 error("the option -r is limited to a single device");
876                 goto error;
877         }
878         if (shrink_rootdir && !source_dir_set) {
879                 error("the option --shrink must be used with --rootdir");
880                 goto error;
881         }
882
883         if (*fs_uuid) {
884                 uuid_t dummy_uuid;
885
886                 if (uuid_parse(fs_uuid, dummy_uuid) != 0) {
887                         error("could not parse UUID: %s", fs_uuid);
888                         goto error;
889                 }
890                 if (!test_uuid_unique(fs_uuid)) {
891                         error("non-unique UUID: %s", fs_uuid);
892                         goto error;
893                 }
894         }
895
896         while (dev_cnt-- > 0) {
897                 file = argv[optind++];
898                 if (source_dir_set && is_path_exist(file) == 0)
899                         ret = 0;
900                 else if (is_block_device(file) == 1)
901                         ret = test_dev_for_mkfs(file, force_overwrite);
902                 else
903                         ret = test_status_for_mkfs(file, force_overwrite);
904
905                 if (ret)
906                         goto error;
907         }
908
909         optind = saved_optind;
910         dev_cnt = argc - optind;
911
912         file = argv[optind++];
913         ssd = is_ssd(file);
914
915         /*
916         * Set default profiles according to number of added devices.
917         * For mixed groups defaults are single/single.
918         */
919         if (!mixed) {
920                 if (!metadata_profile_opt) {
921                         if (dev_cnt == 1 && ssd && verbose)
922                                 printf("Detected a SSD, turning off metadata "
923                                 "duplication.  Mkfs with -m dup if you want to "
924                                 "force metadata duplication.\n");
925
926                         metadata_profile = (dev_cnt > 1) ?
927                                         BTRFS_BLOCK_GROUP_RAID1 : (ssd) ?
928                                         0: BTRFS_BLOCK_GROUP_DUP;
929                 }
930                 if (!data_profile_opt) {
931                         data_profile = (dev_cnt > 1) ?
932                                 BTRFS_BLOCK_GROUP_RAID0 : 0; /* raid0 or single */
933                 }
934         } else {
935                 u32 best_nodesize = max_t(u32, sysconf(_SC_PAGESIZE), sectorsize);
936
937                 if (metadata_profile_opt || data_profile_opt) {
938                         if (metadata_profile != data_profile) {
939                                 error(
940         "with mixed block groups data and metadata profiles must be the same");
941                                 goto error;
942                         }
943                 }
944
945                 if (!nodesize_forced)
946                         nodesize = best_nodesize;
947         }
948
949         /*
950          * FS features that can be set by other means than -O
951          * just set the bit here
952          */
953         if (mixed)
954                 features |= BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS;
955
956         if ((data_profile | metadata_profile) &
957             (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6)) {
958                 features |= BTRFS_FEATURE_INCOMPAT_RAID56;
959         }
960
961         if (btrfs_check_nodesize(nodesize, sectorsize,
962                                  features))
963                 goto error;
964
965         if (sectorsize < sizeof(struct btrfs_super_block)) {
966                 error("sectorsize smaller than superblock: %u < %zu",
967                                 sectorsize, sizeof(struct btrfs_super_block));
968                 goto error;
969         }
970
971         min_dev_size = btrfs_min_dev_size(nodesize, mixed, metadata_profile,
972                                           data_profile);
973         /*
974          * Enlarge the destination file or create a new one, using the size
975          * calculated from source dir.
976          *
977          * This must be done before minimal device size checks.
978          */
979         if (source_dir_set) {
980                 int oflags = O_RDWR;
981                 struct stat statbuf;
982
983                 if (is_path_exist(file) == 0)
984                         oflags |= O_CREAT;
985
986                 fd = open(file, oflags, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP |
987                                          S_IROTH);
988                 if (fd < 0) {
989                         error("unable to open %s: %m", file);
990                         goto error;
991                 }
992
993                 ret = fstat(fd, &statbuf);
994                 if (ret < 0) {
995                         error("unable to stat %s: %m", file);
996                         ret = -errno;
997                         goto error;
998                 }
999
1000                 /*
1001                  * Block_count not specified, use file/device size first.
1002                  * Or we will always use source_dir_size calculated for mkfs.
1003                  */
1004                 if (!block_count)
1005                         block_count = btrfs_device_size(fd, &statbuf);
1006                 source_dir_size = btrfs_mkfs_size_dir(source_dir, sectorsize,
1007                                 min_dev_size, metadata_profile, data_profile);
1008                 if (block_count < source_dir_size)
1009                         block_count = source_dir_size;
1010                 ret = zero_output_file(fd, block_count);
1011                 if (ret) {
1012                         error("unable to zero the output file");
1013                         close(fd);
1014                         goto error;
1015                 }
1016                 /* our "device" is the new image file */
1017                 dev_block_count = block_count;
1018                 close(fd);
1019         }
1020         /* Check device/block_count after the nodesize is determined */
1021         if (block_count && block_count < min_dev_size) {
1022                 error("size %llu is too small to make a usable filesystem",
1023                         block_count);
1024                 error("minimum size for btrfs filesystem is %llu",
1025                         min_dev_size);
1026                 goto error;
1027         }
1028         for (i = saved_optind; i < saved_optind + dev_cnt; i++) {
1029                 char *path;
1030
1031                 path = argv[i];
1032                 ret = test_minimum_size(path, min_dev_size);
1033                 if (ret < 0) {
1034                         error("failed to check size for %s: %m", path);
1035                         goto error;
1036                 }
1037                 if (ret > 0) {
1038                         error("'%s' is too small to make a usable filesystem",
1039                                 path);
1040                         error("minimum size for each btrfs device is %llu",
1041                                 min_dev_size);
1042                         goto error;
1043                 }
1044         }
1045         ret = test_num_disk_vs_raid(metadata_profile, data_profile,
1046                         dev_cnt, mixed, ssd);
1047         if (ret)
1048                 goto error;
1049
1050         dev_cnt--;
1051
1052         /*
1053          * Open without O_EXCL so that the problem should not occur by the
1054          * following operation in kernel:
1055          * (btrfs_register_one_device() fails if O_EXCL is on)
1056          */
1057         fd = open(file, O_RDWR);
1058         if (fd < 0) {
1059                 error("unable to open %s: %m", file);
1060                 goto error;
1061         }
1062         ret = btrfs_prepare_device(fd, file, &dev_block_count, block_count,
1063                         (zero_end ? PREP_DEVICE_ZERO_END : 0) |
1064                         (discard ? PREP_DEVICE_DISCARD : 0) |
1065                         (verbose ? PREP_DEVICE_VERBOSE : 0));
1066         if (ret)
1067                 goto error;
1068         if (block_count && block_count > dev_block_count) {
1069                 error("%s is smaller than requested size, expected %llu, found %llu",
1070                       file, (unsigned long long)block_count,
1071                       (unsigned long long)dev_block_count);
1072                 goto error;
1073         }
1074
1075         /* To create the first block group and chunk 0 in make_btrfs */
1076         if (dev_block_count < BTRFS_MKFS_SYSTEM_GROUP_SIZE) {
1077                 error("device is too small to make filesystem, must be at least %llu",
1078                                 (unsigned long long)BTRFS_MKFS_SYSTEM_GROUP_SIZE);
1079                 goto error;
1080         }
1081
1082         if (group_profile_max_safe_loss(metadata_profile) <
1083                 group_profile_max_safe_loss(data_profile)){
1084                 warning("metadata has lower redundancy than data!\n");
1085         }
1086
1087         mkfs_cfg.label = label;
1088         memcpy(mkfs_cfg.fs_uuid, fs_uuid, sizeof(mkfs_cfg.fs_uuid));
1089         mkfs_cfg.num_bytes = dev_block_count;
1090         mkfs_cfg.nodesize = nodesize;
1091         mkfs_cfg.sectorsize = sectorsize;
1092         mkfs_cfg.stripesize = stripesize;
1093         mkfs_cfg.features = features;
1094
1095         ret = make_btrfs(fd, &mkfs_cfg);
1096         if (ret) {
1097                 error("error during mkfs: %s", strerror(-ret));
1098                 goto error;
1099         }
1100
1101         fs_info = open_ctree_fs_info(file, 0, 0, 0,
1102                         OPEN_CTREE_WRITES | OPEN_CTREE_TEMPORARY_SUPER);
1103         if (!fs_info) {
1104                 error("open ctree failed");
1105                 goto error;
1106         }
1107         close(fd);
1108         fd = -1;
1109         root = fs_info->fs_root;
1110         fs_info->alloc_start = alloc_start;
1111
1112         ret = create_metadata_block_groups(root, mixed, &allocation);
1113         if (ret) {
1114                 error("failed to create default block groups: %d", ret);
1115                 goto error;
1116         }
1117
1118         trans = btrfs_start_transaction(root, 1);
1119         if (IS_ERR(trans)) {
1120                 error("failed to start transaction");
1121                 goto error;
1122         }
1123
1124         ret = create_data_block_groups(trans, root, mixed, &allocation);
1125         if (ret) {
1126                 error("failed to create default data block groups: %d", ret);
1127                 goto error;
1128         }
1129
1130         ret = make_root_dir(trans, root);
1131         if (ret) {
1132                 error("failed to setup the root directory: %d", ret);
1133                 goto error;
1134         }
1135
1136         ret = btrfs_commit_transaction(trans, root);
1137         if (ret) {
1138                 error("unable to commit transaction: %d", ret);
1139                 goto out;
1140         }
1141
1142         trans = btrfs_start_transaction(root, 1);
1143         if (IS_ERR(trans)) {
1144                 error("failed to start transaction");
1145                 goto error;
1146         }
1147
1148         if (dev_cnt == 0)
1149                 goto raid_groups;
1150
1151         while (dev_cnt-- > 0) {
1152                 file = argv[optind++];
1153
1154                 /*
1155                  * open without O_EXCL so that the problem should not
1156                  * occur by the following processing.
1157                  * (btrfs_register_one_device() fails if O_EXCL is on)
1158                  */
1159                 fd = open(file, O_RDWR);
1160                 if (fd < 0) {
1161                         error("unable to open %s: %m", file);
1162                         goto error;
1163                 }
1164                 ret = btrfs_device_already_in_root(root, fd,
1165                                                    BTRFS_SUPER_INFO_OFFSET);
1166                 if (ret) {
1167                         error("skipping duplicate device %s in the filesystem",
1168                                 file);
1169                         close(fd);
1170                         continue;
1171                 }
1172                 ret = btrfs_prepare_device(fd, file, &dev_block_count,
1173                                 block_count,
1174                                 (verbose ? PREP_DEVICE_VERBOSE : 0) |
1175                                 (zero_end ? PREP_DEVICE_ZERO_END : 0) |
1176                                 (discard ? PREP_DEVICE_DISCARD : 0));
1177                 if (ret) {
1178                         goto error;
1179                 }
1180
1181                 ret = btrfs_add_to_fsid(trans, root, fd, file, dev_block_count,
1182                                         sectorsize, sectorsize, sectorsize);
1183                 if (ret) {
1184                         error("unable to add %s to filesystem: %d", file, ret);
1185                         goto out;
1186                 }
1187                 if (verbose >= 2) {
1188                         struct btrfs_device *device;
1189
1190                         device = container_of(fs_info->fs_devices->devices.next,
1191                                         struct btrfs_device, dev_list);
1192                         printf("adding device %s id %llu\n", file,
1193                                 (unsigned long long)device->devid);
1194                 }
1195         }
1196
1197 raid_groups:
1198         ret = create_raid_groups(trans, root, data_profile,
1199                          metadata_profile, mixed, &allocation);
1200         if (ret) {
1201                 error("unable to create raid groups: %d", ret);
1202                 goto out;
1203         }
1204
1205         ret = create_tree(trans, root, BTRFS_DATA_RELOC_TREE_OBJECTID);
1206         if (ret) {
1207                 error("unable to create data reloc tree: %d", ret);
1208                 goto out;
1209         }
1210
1211         ret = create_tree(trans, root, BTRFS_UUID_TREE_OBJECTID);
1212         if (ret)
1213                 warning(
1214         "unable to create uuid tree, will be created after mount: %d", ret);
1215
1216         ret = btrfs_commit_transaction(trans, root);
1217         if (ret) {
1218                 error("unable to commit transaction: %d", ret);
1219                 goto out;
1220         }
1221
1222         ret = cleanup_temp_chunks(fs_info, &allocation, data_profile,
1223                                   metadata_profile, metadata_profile);
1224         if (ret < 0) {
1225                 error("failed to cleanup temporary chunks: %d", ret);
1226                 goto out;
1227         }
1228
1229         if (source_dir_set) {
1230                 ret = btrfs_mkfs_fill_dir(source_dir, root, verbose);
1231                 if (ret) {
1232                         error("error wihle filling filesystem: %d", ret);
1233                         goto out;
1234                 }
1235                 if (shrink_rootdir) {
1236                         ret = btrfs_mkfs_shrink_fs(fs_info, &shrink_size,
1237                                                    shrink_rootdir);
1238                         if (ret < 0) {
1239                                 error("error while shrinking filesystem: %d",
1240                                         ret);
1241                                 goto out;
1242                         }
1243                 }
1244         }
1245
1246         if (verbose) {
1247                 char features_buf[64];
1248
1249                 update_chunk_allocation(fs_info, &allocation);
1250                 printf("Label:              %s\n", label);
1251                 printf("UUID:               %s\n", mkfs_cfg.fs_uuid);
1252                 printf("Node size:          %u\n", nodesize);
1253                 printf("Sector size:        %u\n", sectorsize);
1254                 printf("Filesystem size:    %s\n",
1255                         pretty_size(btrfs_super_total_bytes(fs_info->super_copy)));
1256                 printf("Block group profiles:\n");
1257                 if (allocation.data)
1258                         printf("  Data:             %-8s %16s\n",
1259                                 btrfs_group_profile_str(data_profile),
1260                                 pretty_size(allocation.data));
1261                 if (allocation.metadata)
1262                         printf("  Metadata:         %-8s %16s\n",
1263                                 btrfs_group_profile_str(metadata_profile),
1264                                 pretty_size(allocation.metadata));
1265                 if (allocation.mixed)
1266                         printf("  Data+Metadata:    %-8s %16s\n",
1267                                 btrfs_group_profile_str(data_profile),
1268                                 pretty_size(allocation.mixed));
1269                 printf("  System:           %-8s %16s\n",
1270                         btrfs_group_profile_str(metadata_profile),
1271                         pretty_size(allocation.system));
1272                 printf("SSD detected:       %s\n", ssd ? "yes" : "no");
1273                 btrfs_parse_features_to_string(features_buf, features);
1274                 printf("Incompat features:  %s", features_buf);
1275                 printf("\n");
1276
1277                 list_all_devices(root);
1278         }
1279
1280         /*
1281          * The filesystem is now fully set up, commit the remaining changes and
1282          * fix the signature as the last step before closing the devices.
1283          */
1284         fs_info->finalize_on_close = 1;
1285 out:
1286         close_ret = close_ctree(root);
1287
1288         if (!close_ret) {
1289                 optind = saved_optind;
1290                 dev_cnt = argc - optind;
1291                 while (dev_cnt-- > 0) {
1292                         file = argv[optind++];
1293                         if (is_block_device(file) == 1)
1294                                 btrfs_register_one_device(file);
1295                 }
1296         }
1297
1298         if (!ret && close_ret) {
1299                 ret = close_ret;
1300                 error("failed to close ctree, the filesystem may be inconsistent: %d",
1301                       ret);
1302         }
1303
1304         btrfs_close_all_devices();
1305         free(label);
1306
1307         return !!ret;
1308 error:
1309         if (fd > 0)
1310                 close(fd);
1311
1312         free(label);
1313         exit(1);
1314 success:
1315         exit(0);
1316 }