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