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