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