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