btrfs-progs: convert: move create_image
[btrfs-progs-unstable/devel.git] / mkfs.c
blobb33e3688060700a6ddee721495e3aa621631ad2a
1 /*
2 * Copyright (C) 2007 Oracle. All rights reserved.
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.
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.
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.
19 #include "kerncompat.h"
20 #include "androidcompat.h"
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/types.h>
28 #include <sys/stat.h>
29 /* #include <sys/dir.h> included via androidcompat.h */
30 #include <fcntl.h>
31 #include <unistd.h>
32 #include <getopt.h>
33 #include <uuid/uuid.h>
34 #include <ctype.h>
35 #include <sys/xattr.h>
36 #include <limits.h>
37 #include <linux/limits.h>
38 #include <blkid/blkid.h>
39 #include <ftw.h>
40 #include "ctree.h"
41 #include "disk-io.h"
42 #include "volumes.h"
43 #include "transaction.h"
44 #include "utils.h"
45 #include "list_sort.h"
47 static u64 index_cnt = 2;
48 static int verbose = 1;
50 struct directory_name_entry {
51 char *dir_name;
52 char *path;
53 ino_t inum;
54 struct list_head list;
57 struct mkfs_allocation {
58 u64 data;
59 u64 metadata;
60 u64 mixed;
61 u64 system;
64 static int create_metadata_block_groups(struct btrfs_root *root, int mixed,
65 struct mkfs_allocation *allocation)
67 struct btrfs_trans_handle *trans;
68 u64 bytes_used;
69 u64 chunk_start = 0;
70 u64 chunk_size = 0;
71 int ret;
73 trans = btrfs_start_transaction(root, 1);
74 bytes_used = btrfs_super_bytes_used(root->fs_info->super_copy);
76 root->fs_info->system_allocs = 1;
77 ret = btrfs_make_block_group(trans, root, bytes_used,
78 BTRFS_BLOCK_GROUP_SYSTEM,
79 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
80 0, BTRFS_MKFS_SYSTEM_GROUP_SIZE);
81 allocation->system += BTRFS_MKFS_SYSTEM_GROUP_SIZE;
82 if (ret)
83 return ret;
85 if (mixed) {
86 ret = btrfs_alloc_chunk(trans, root->fs_info->extent_root,
87 &chunk_start, &chunk_size,
88 BTRFS_BLOCK_GROUP_METADATA |
89 BTRFS_BLOCK_GROUP_DATA);
90 if (ret == -ENOSPC) {
91 error("no space to allocate data/metadata chunk");
92 goto err;
94 if (ret)
95 return ret;
96 ret = btrfs_make_block_group(trans, root, 0,
97 BTRFS_BLOCK_GROUP_METADATA |
98 BTRFS_BLOCK_GROUP_DATA,
99 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
100 chunk_start, chunk_size);
101 if (ret)
102 return ret;
103 allocation->mixed += chunk_size;
104 } else {
105 ret = btrfs_alloc_chunk(trans, root->fs_info->extent_root,
106 &chunk_start, &chunk_size,
107 BTRFS_BLOCK_GROUP_METADATA);
108 if (ret == -ENOSPC) {
109 error("no space to allocate metadata chunk");
110 goto err;
112 if (ret)
113 return ret;
114 ret = btrfs_make_block_group(trans, root, 0,
115 BTRFS_BLOCK_GROUP_METADATA,
116 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
117 chunk_start, chunk_size);
118 allocation->metadata += chunk_size;
119 if (ret)
120 return ret;
123 root->fs_info->system_allocs = 0;
124 ret = btrfs_commit_transaction(trans, root);
126 err:
127 return ret;
130 static int create_data_block_groups(struct btrfs_trans_handle *trans,
131 struct btrfs_root *root, int mixed,
132 struct mkfs_allocation *allocation)
134 u64 chunk_start = 0;
135 u64 chunk_size = 0;
136 int ret = 0;
138 if (!mixed) {
139 ret = btrfs_alloc_chunk(trans, root->fs_info->extent_root,
140 &chunk_start, &chunk_size,
141 BTRFS_BLOCK_GROUP_DATA);
142 if (ret == -ENOSPC) {
143 error("no space to allocate data chunk");
144 goto err;
146 if (ret)
147 return ret;
148 ret = btrfs_make_block_group(trans, root, 0,
149 BTRFS_BLOCK_GROUP_DATA,
150 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
151 chunk_start, chunk_size);
152 allocation->data += chunk_size;
153 if (ret)
154 return ret;
157 err:
158 return ret;
161 static int make_root_dir(struct btrfs_trans_handle *trans, struct btrfs_root *root,
162 struct mkfs_allocation *allocation)
164 struct btrfs_key location;
165 int ret;
167 ret = btrfs_make_root_dir(trans, root->fs_info->tree_root,
168 BTRFS_ROOT_TREE_DIR_OBJECTID);
169 if (ret)
170 goto err;
171 ret = btrfs_make_root_dir(trans, root, BTRFS_FIRST_FREE_OBJECTID);
172 if (ret)
173 goto err;
174 memcpy(&location, &root->fs_info->fs_root->root_key, sizeof(location));
175 location.offset = (u64)-1;
176 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
177 "default", 7,
178 btrfs_super_root_dir(root->fs_info->super_copy),
179 &location, BTRFS_FT_DIR, 0);
180 if (ret)
181 goto err;
183 ret = btrfs_insert_inode_ref(trans, root->fs_info->tree_root,
184 "default", 7, location.objectid,
185 BTRFS_ROOT_TREE_DIR_OBJECTID, 0);
186 if (ret)
187 goto err;
189 err:
190 return ret;
193 static int __recow_root(struct btrfs_trans_handle *trans,
194 struct btrfs_root *root)
196 struct extent_buffer *tmp;
197 int ret;
199 if (trans->transid != btrfs_root_generation(&root->root_item)) {
200 extent_buffer_get(root->node);
201 ret = __btrfs_cow_block(trans, root, root->node,
202 NULL, 0, &tmp, 0, 0);
203 if (ret)
204 return ret;
205 free_extent_buffer(tmp);
208 return 0;
211 static int recow_roots(struct btrfs_trans_handle *trans,
212 struct btrfs_root *root)
214 struct btrfs_fs_info *info = root->fs_info;
215 int ret;
217 ret = __recow_root(trans, info->fs_root);
218 if (ret)
219 return ret;
220 ret = __recow_root(trans, info->tree_root);
221 if (ret)
222 return ret;
223 ret = __recow_root(trans, info->extent_root);
224 if (ret)
225 return ret;
226 ret = __recow_root(trans, info->chunk_root);
227 if (ret)
228 return ret;
229 ret = __recow_root(trans, info->dev_root);
230 if (ret)
231 return ret;
232 ret = __recow_root(trans, info->csum_root);
233 if (ret)
234 return ret;
236 return 0;
239 static int create_one_raid_group(struct btrfs_trans_handle *trans,
240 struct btrfs_root *root, u64 type,
241 struct mkfs_allocation *allocation)
244 u64 chunk_start;
245 u64 chunk_size;
246 int ret;
248 ret = btrfs_alloc_chunk(trans, root->fs_info->extent_root,
249 &chunk_start, &chunk_size, type);
250 if (ret == -ENOSPC) {
251 error("not enough free space to allocate chunk");
252 exit(1);
254 if (ret)
255 return ret;
257 ret = btrfs_make_block_group(trans, root->fs_info->extent_root, 0,
258 type, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
259 chunk_start, chunk_size);
261 type &= BTRFS_BLOCK_GROUP_TYPE_MASK;
262 if (type == BTRFS_BLOCK_GROUP_DATA) {
263 allocation->data += chunk_size;
264 } else if (type == BTRFS_BLOCK_GROUP_METADATA) {
265 allocation->metadata += chunk_size;
266 } else if (type == BTRFS_BLOCK_GROUP_SYSTEM) {
267 allocation->system += chunk_size;
268 } else if (type ==
269 (BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_DATA)) {
270 allocation->mixed += chunk_size;
271 } else {
272 error("unrecognized profile type: 0x%llx",
273 (unsigned long long)type);
274 ret = -EINVAL;
277 return ret;
280 static int create_raid_groups(struct btrfs_trans_handle *trans,
281 struct btrfs_root *root, u64 data_profile,
282 u64 metadata_profile, int mixed,
283 struct mkfs_allocation *allocation)
285 int ret;
287 if (metadata_profile) {
288 u64 meta_flags = BTRFS_BLOCK_GROUP_METADATA;
290 ret = create_one_raid_group(trans, root,
291 BTRFS_BLOCK_GROUP_SYSTEM |
292 metadata_profile, allocation);
293 if (ret)
294 return ret;
296 if (mixed)
297 meta_flags |= BTRFS_BLOCK_GROUP_DATA;
299 ret = create_one_raid_group(trans, root, meta_flags |
300 metadata_profile, allocation);
301 if (ret)
302 return ret;
305 if (!mixed && data_profile) {
306 ret = create_one_raid_group(trans, root,
307 BTRFS_BLOCK_GROUP_DATA |
308 data_profile, allocation);
309 if (ret)
310 return ret;
312 ret = recow_roots(trans, root);
314 return ret;
317 static int create_data_reloc_tree(struct btrfs_trans_handle *trans,
318 struct btrfs_root *root)
320 struct btrfs_key location;
321 struct btrfs_root_item root_item;
322 struct extent_buffer *tmp;
323 u64 objectid = BTRFS_DATA_RELOC_TREE_OBJECTID;
324 int ret;
326 ret = btrfs_copy_root(trans, root, root->node, &tmp, objectid);
327 if (ret)
328 return ret;
330 memcpy(&root_item, &root->root_item, sizeof(root_item));
331 btrfs_set_root_bytenr(&root_item, tmp->start);
332 btrfs_set_root_level(&root_item, btrfs_header_level(tmp));
333 btrfs_set_root_generation(&root_item, trans->transid);
334 free_extent_buffer(tmp);
336 location.objectid = objectid;
337 location.type = BTRFS_ROOT_ITEM_KEY;
338 location.offset = 0;
339 ret = btrfs_insert_root(trans, root->fs_info->tree_root,
340 &location, &root_item);
342 return ret;
345 static void print_usage(int ret)
347 printf("usage: mkfs.btrfs [options] dev [ dev ... ]\n");
348 printf("options:\n");
349 printf("\t-A|--alloc-start START the offset to start the FS\n");
350 printf("\t-b|--byte-count SIZE total number of bytes in the FS\n");
351 printf("\t-d|--data PROFILE data profile, raid0, raid1, raid5, raid6, raid10, dup or single\n");
352 printf("\t-f|--force force overwrite of existing filesystem\n");
353 printf("\t-l|--leafsize SIZE deprecated, alias for nodesize\n");
354 printf("\t-L|--label LABEL set a label\n");
355 printf("\t-m|--metadata PROFILE metadata profile, values like data profile\n");
356 printf("\t-M|--mixed mix metadata and data together\n");
357 printf("\t-n|--nodesize SIZE size of btree nodes\n");
358 printf("\t-s|--sectorsize SIZE min block allocation (may not mountable by current kernel)\n");
359 printf("\t-r|--rootdir DIR the source directory\n");
360 printf("\t-K|--nodiscard do not perform whole device TRIM\n");
361 printf("\t-O|--features LIST comma separated list of filesystem features, use '-O list-all' to list features\n");
362 printf("\t-U|--uuid UUID specify the filesystem UUID\n");
363 printf("\t-q|--quiet no messages except errors\n");
364 printf("\t-V|--version print the mkfs.btrfs version and exit\n");
365 exit(ret);
368 static u64 parse_profile(char *s)
370 if (strcasecmp(s, "raid0") == 0) {
371 return BTRFS_BLOCK_GROUP_RAID0;
372 } else if (strcasecmp(s, "raid1") == 0) {
373 return BTRFS_BLOCK_GROUP_RAID1;
374 } else if (strcasecmp(s, "raid5") == 0) {
375 return BTRFS_BLOCK_GROUP_RAID5;
376 } else if (strcasecmp(s, "raid6") == 0) {
377 return BTRFS_BLOCK_GROUP_RAID6;
378 } else if (strcasecmp(s, "raid10") == 0) {
379 return BTRFS_BLOCK_GROUP_RAID10;
380 } else if (strcasecmp(s, "dup") == 0) {
381 return BTRFS_BLOCK_GROUP_DUP;
382 } else if (strcasecmp(s, "single") == 0) {
383 return 0;
384 } else {
385 error("unknown profile %s", s);
386 exit(1);
388 /* not reached */
389 return 0;
392 static char *parse_label(char *input)
394 int len = strlen(input);
396 if (len >= BTRFS_LABEL_SIZE) {
397 error("label %s is too long (max %d)", input,
398 BTRFS_LABEL_SIZE - 1);
399 exit(1);
401 return strdup(input);
404 static int add_directory_items(struct btrfs_trans_handle *trans,
405 struct btrfs_root *root, u64 objectid,
406 ino_t parent_inum, const char *name,
407 struct stat *st, int *dir_index_cnt)
409 int ret;
410 int name_len;
411 struct btrfs_key location;
412 u8 filetype = 0;
414 name_len = strlen(name);
416 location.objectid = objectid;
417 location.offset = 0;
418 btrfs_set_key_type(&location, BTRFS_INODE_ITEM_KEY);
420 if (S_ISDIR(st->st_mode))
421 filetype = BTRFS_FT_DIR;
422 if (S_ISREG(st->st_mode))
423 filetype = BTRFS_FT_REG_FILE;
424 if (S_ISLNK(st->st_mode))
425 filetype = BTRFS_FT_SYMLINK;
427 ret = btrfs_insert_dir_item(trans, root, name, name_len,
428 parent_inum, &location,
429 filetype, index_cnt);
430 if (ret)
431 return ret;
432 ret = btrfs_insert_inode_ref(trans, root, name, name_len,
433 objectid, parent_inum, index_cnt);
434 *dir_index_cnt = index_cnt;
435 index_cnt++;
437 return ret;
440 static int fill_inode_item(struct btrfs_trans_handle *trans,
441 struct btrfs_root *root,
442 struct btrfs_inode_item *dst, struct stat *src)
444 u64 blocks = 0;
445 u64 sectorsize = root->sectorsize;
448 * btrfs_inode_item has some reserved fields
449 * and represents on-disk inode entry, so
450 * zero everything to prevent information leak
452 memset(dst, 0, sizeof (*dst));
454 btrfs_set_stack_inode_generation(dst, trans->transid);
455 btrfs_set_stack_inode_size(dst, src->st_size);
456 btrfs_set_stack_inode_nbytes(dst, 0);
457 btrfs_set_stack_inode_block_group(dst, 0);
458 btrfs_set_stack_inode_nlink(dst, src->st_nlink);
459 btrfs_set_stack_inode_uid(dst, src->st_uid);
460 btrfs_set_stack_inode_gid(dst, src->st_gid);
461 btrfs_set_stack_inode_mode(dst, src->st_mode);
462 btrfs_set_stack_inode_rdev(dst, 0);
463 btrfs_set_stack_inode_flags(dst, 0);
464 btrfs_set_stack_timespec_sec(&dst->atime, src->st_atime);
465 btrfs_set_stack_timespec_nsec(&dst->atime, 0);
466 btrfs_set_stack_timespec_sec(&dst->ctime, src->st_ctime);
467 btrfs_set_stack_timespec_nsec(&dst->ctime, 0);
468 btrfs_set_stack_timespec_sec(&dst->mtime, src->st_mtime);
469 btrfs_set_stack_timespec_nsec(&dst->mtime, 0);
470 btrfs_set_stack_timespec_sec(&dst->otime, 0);
471 btrfs_set_stack_timespec_nsec(&dst->otime, 0);
473 if (S_ISDIR(src->st_mode)) {
474 btrfs_set_stack_inode_size(dst, 0);
475 btrfs_set_stack_inode_nlink(dst, 1);
477 if (S_ISREG(src->st_mode)) {
478 btrfs_set_stack_inode_size(dst, (u64)src->st_size);
479 if (src->st_size <= BTRFS_MAX_INLINE_DATA_SIZE(root))
480 btrfs_set_stack_inode_nbytes(dst, src->st_size);
481 else {
482 blocks = src->st_size / sectorsize;
483 if (src->st_size % sectorsize)
484 blocks += 1;
485 blocks *= sectorsize;
486 btrfs_set_stack_inode_nbytes(dst, blocks);
489 if (S_ISLNK(src->st_mode))
490 btrfs_set_stack_inode_nbytes(dst, src->st_size + 1);
492 return 0;
495 static int directory_select(const struct direct *entry)
497 if ((strncmp(entry->d_name, ".", entry->d_reclen) == 0) ||
498 (strncmp(entry->d_name, "..", entry->d_reclen) == 0))
499 return 0;
500 else
501 return 1;
504 static void free_namelist(struct direct **files, int count)
506 int i;
508 if (count < 0)
509 return;
511 for (i = 0; i < count; ++i)
512 free(files[i]);
513 free(files);
516 static u64 calculate_dir_inode_size(char *dirname)
518 int count, i;
519 struct direct **files, *cur_file;
520 u64 dir_inode_size = 0;
522 count = scandir(dirname, &files, directory_select, NULL);
524 for (i = 0; i < count; i++) {
525 cur_file = files[i];
526 dir_inode_size += strlen(cur_file->d_name);
529 free_namelist(files, count);
531 dir_inode_size *= 2;
532 return dir_inode_size;
535 static int add_inode_items(struct btrfs_trans_handle *trans,
536 struct btrfs_root *root,
537 struct stat *st, char *name,
538 u64 self_objectid, ino_t parent_inum,
539 int dir_index_cnt, struct btrfs_inode_item *inode_ret)
541 int ret;
542 struct btrfs_key inode_key;
543 struct btrfs_inode_item btrfs_inode;
544 u64 objectid;
545 u64 inode_size = 0;
547 fill_inode_item(trans, root, &btrfs_inode, st);
548 objectid = self_objectid;
550 if (S_ISDIR(st->st_mode)) {
551 inode_size = calculate_dir_inode_size(name);
552 btrfs_set_stack_inode_size(&btrfs_inode, inode_size);
555 inode_key.objectid = objectid;
556 inode_key.offset = 0;
557 btrfs_set_key_type(&inode_key, BTRFS_INODE_ITEM_KEY);
559 ret = btrfs_insert_inode(trans, root, objectid, &btrfs_inode);
561 *inode_ret = btrfs_inode;
562 return ret;
565 static int add_xattr_item(struct btrfs_trans_handle *trans,
566 struct btrfs_root *root, u64 objectid,
567 const char *file_name)
569 int ret;
570 int cur_name_len;
571 char xattr_list[XATTR_LIST_MAX];
572 char *cur_name;
573 char cur_value[XATTR_SIZE_MAX];
574 char delimiter = '\0';
575 char *next_location = xattr_list;
577 ret = llistxattr(file_name, xattr_list, XATTR_LIST_MAX);
578 if (ret < 0) {
579 if(errno == ENOTSUP)
580 return 0;
581 error("getting a list of xattr failed for %s: %s", file_name,
582 strerror(errno));
583 return ret;
585 if (ret == 0)
586 return ret;
588 cur_name = strtok(xattr_list, &delimiter);
589 while (cur_name != NULL) {
590 cur_name_len = strlen(cur_name);
591 next_location += cur_name_len + 1;
593 ret = getxattr(file_name, cur_name, cur_value, XATTR_SIZE_MAX);
594 if (ret < 0) {
595 if(errno == ENOTSUP)
596 return 0;
597 error("gettig a xattr value failed for %s attr %s: %s",
598 file_name, cur_name, strerror(errno));
599 return ret;
602 ret = btrfs_insert_xattr_item(trans, root, cur_name,
603 cur_name_len, cur_value,
604 ret, objectid);
605 if (ret) {
606 error("inserting a xattr item failed for %s: %s",
607 file_name, strerror(-ret));
610 cur_name = strtok(next_location, &delimiter);
613 return ret;
616 static int add_symbolic_link(struct btrfs_trans_handle *trans,
617 struct btrfs_root *root,
618 u64 objectid, const char *path_name)
620 int ret;
621 char buf[PATH_MAX];
623 ret = readlink(path_name, buf, sizeof(buf));
624 if (ret <= 0) {
625 error("readlink failed for %s: %s", path_name, strerror(errno));
626 goto fail;
628 if (ret >= sizeof(buf)) {
629 error("symlink too long for %s", path_name);
630 ret = -1;
631 goto fail;
634 buf[ret] = '\0'; /* readlink does not do it for us */
635 ret = btrfs_insert_inline_extent(trans, root, objectid, 0,
636 buf, ret + 1);
637 fail:
638 return ret;
641 static int add_file_items(struct btrfs_trans_handle *trans,
642 struct btrfs_root *root,
643 struct btrfs_inode_item *btrfs_inode, u64 objectid,
644 ino_t parent_inum, struct stat *st,
645 const char *path_name, int out_fd)
647 int ret = -1;
648 ssize_t ret_read;
649 u64 bytes_read = 0;
650 struct btrfs_key key;
651 int blocks;
652 u32 sectorsize = root->sectorsize;
653 u64 first_block = 0;
654 u64 file_pos = 0;
655 u64 cur_bytes;
656 u64 total_bytes;
657 struct extent_buffer *eb = NULL;
658 int fd;
660 if (st->st_size == 0)
661 return 0;
663 fd = open(path_name, O_RDONLY);
664 if (fd == -1) {
665 error("cannot open %s: %s", path_name, strerror(errno));
666 return ret;
669 blocks = st->st_size / sectorsize;
670 if (st->st_size % sectorsize)
671 blocks += 1;
673 if (st->st_size <= BTRFS_MAX_INLINE_DATA_SIZE(root)) {
674 char *buffer = malloc(st->st_size);
676 if (!buffer) {
677 ret = -ENOMEM;
678 goto end;
681 ret_read = pread64(fd, buffer, st->st_size, bytes_read);
682 if (ret_read == -1) {
683 error("cannot read %s at offset %llu length %llu: %s",
684 path_name, (unsigned long long)bytes_read,
685 (unsigned long long)st->st_size,
686 strerror(errno));
687 free(buffer);
688 goto end;
691 ret = btrfs_insert_inline_extent(trans, root, objectid, 0,
692 buffer, st->st_size);
693 free(buffer);
694 goto end;
697 /* round up our st_size to the FS blocksize */
698 total_bytes = (u64)blocks * sectorsize;
701 * do our IO in extent buffers so it can work
702 * against any raid type
704 eb = calloc(1, sizeof(*eb) + sectorsize);
705 if (!eb) {
706 ret = -ENOMEM;
707 goto end;
710 again:
713 * keep our extent size at 1MB max, this makes it easier to work inside
714 * the tiny block groups created during mkfs
716 cur_bytes = min(total_bytes, 1024ULL * 1024);
717 ret = btrfs_reserve_extent(trans, root, cur_bytes, 0, 0, (u64)-1,
718 &key, 1);
719 if (ret)
720 goto end;
722 first_block = key.objectid;
723 bytes_read = 0;
725 while (bytes_read < cur_bytes) {
727 memset(eb->data, 0, sectorsize);
729 ret_read = pread64(fd, eb->data, sectorsize, file_pos + bytes_read);
730 if (ret_read == -1) {
731 error("cannot read %s at offset %llu length %llu: %s",
732 path_name,
733 (unsigned long long)file_pos + bytes_read,
734 (unsigned long long)sectorsize,
735 strerror(errno));
736 goto end;
739 eb->start = first_block + bytes_read;
740 eb->len = sectorsize;
743 * we're doing the csum before we record the extent, but
744 * that's ok
746 ret = btrfs_csum_file_block(trans, root->fs_info->csum_root,
747 first_block + bytes_read + sectorsize,
748 first_block + bytes_read,
749 eb->data, sectorsize);
750 if (ret)
751 goto end;
753 ret = write_and_map_eb(trans, root, eb);
754 if (ret) {
755 error("failed to write %s", path_name);
756 goto end;
759 bytes_read += sectorsize;
762 if (bytes_read) {
763 ret = btrfs_record_file_extent(trans, root, objectid, btrfs_inode,
764 file_pos, first_block, cur_bytes);
765 if (ret)
766 goto end;
770 file_pos += cur_bytes;
771 total_bytes -= cur_bytes;
773 if (total_bytes)
774 goto again;
776 end:
777 free(eb);
778 close(fd);
779 return ret;
782 static char *make_path(char *dir, char *name)
784 char *path;
786 path = malloc(strlen(dir) + strlen(name) + 2);
787 if (!path)
788 return NULL;
789 strcpy(path, dir);
790 if (dir[strlen(dir) - 1] != '/')
791 strcat(path, "/");
792 strcat(path, name);
793 return path;
796 static int traverse_directory(struct btrfs_trans_handle *trans,
797 struct btrfs_root *root, char *dir_name,
798 struct directory_name_entry *dir_head, int out_fd)
800 int ret = 0;
802 struct btrfs_inode_item cur_inode;
803 struct btrfs_inode_item *inode_item;
804 int count, i, dir_index_cnt;
805 struct direct **files;
806 struct stat st;
807 struct directory_name_entry *dir_entry, *parent_dir_entry;
808 struct direct *cur_file;
809 ino_t parent_inum, cur_inum;
810 ino_t highest_inum = 0;
811 char *parent_dir_name;
812 char real_path[PATH_MAX];
813 struct btrfs_path path;
814 struct extent_buffer *leaf;
815 struct btrfs_key root_dir_key;
816 u64 root_dir_inode_size = 0;
818 /* Add list for source directory */
819 dir_entry = malloc(sizeof(struct directory_name_entry));
820 if (!dir_entry)
821 return -ENOMEM;
822 dir_entry->dir_name = dir_name;
823 dir_entry->path = realpath(dir_name, real_path);
824 if (!dir_entry->path) {
825 error("realpath failed for %s: %s", dir_name, strerror(errno));
826 ret = -1;
827 goto fail_no_dir;
830 parent_inum = highest_inum + BTRFS_FIRST_FREE_OBJECTID;
831 dir_entry->inum = parent_inum;
832 list_add_tail(&dir_entry->list, &dir_head->list);
834 btrfs_init_path(&path);
836 root_dir_key.objectid = btrfs_root_dirid(&root->root_item);
837 root_dir_key.offset = 0;
838 btrfs_set_key_type(&root_dir_key, BTRFS_INODE_ITEM_KEY);
839 ret = btrfs_lookup_inode(trans, root, &path, &root_dir_key, 1);
840 if (ret) {
841 error("failed to lookup root dir: %d", ret);
842 goto fail_no_dir;
845 leaf = path.nodes[0];
846 inode_item = btrfs_item_ptr(leaf, path.slots[0],
847 struct btrfs_inode_item);
849 root_dir_inode_size = calculate_dir_inode_size(dir_name);
850 btrfs_set_inode_size(leaf, inode_item, root_dir_inode_size);
851 btrfs_mark_buffer_dirty(leaf);
853 btrfs_release_path(&path);
855 do {
856 parent_dir_entry = list_entry(dir_head->list.next,
857 struct directory_name_entry,
858 list);
859 list_del(&parent_dir_entry->list);
861 parent_inum = parent_dir_entry->inum;
862 parent_dir_name = parent_dir_entry->dir_name;
863 if (chdir(parent_dir_entry->path)) {
864 error("chdir failed for %s: %s",
865 parent_dir_name, strerror(errno));
866 ret = -1;
867 goto fail_no_files;
870 count = scandir(parent_dir_entry->path, &files,
871 directory_select, NULL);
872 if (count == -1)
874 error("scandir failed for %s: %s",
875 parent_dir_name, strerror (errno));
876 ret = -1;
877 goto fail;
880 for (i = 0; i < count; i++) {
881 cur_file = files[i];
883 if (lstat(cur_file->d_name, &st) == -1) {
884 error("lstat failed for %s: %s",
885 cur_file->d_name, strerror(errno));
886 ret = -1;
887 goto fail;
890 cur_inum = st.st_ino;
891 ret = add_directory_items(trans, root,
892 cur_inum, parent_inum,
893 cur_file->d_name,
894 &st, &dir_index_cnt);
895 if (ret) {
896 error("unable to add directory items for %s: %d",
897 cur_file->d_name, ret);
898 goto fail;
901 ret = add_inode_items(trans, root, &st,
902 cur_file->d_name, cur_inum,
903 parent_inum, dir_index_cnt,
904 &cur_inode);
905 if (ret == -EEXIST) {
906 if (st.st_nlink <= 1) {
907 error(
908 "item %s already exists but has wrong st_nlink %ld <= 1",
909 cur_file->d_name, st.st_nlink);
910 goto fail;
912 continue;
914 if (ret) {
915 error("unable to add inode items for %s: %d",
916 cur_file->d_name, ret);
917 goto fail;
920 ret = add_xattr_item(trans, root,
921 cur_inum, cur_file->d_name);
922 if (ret) {
923 error("unable to add xattr items for %s: %d",
924 cur_file->d_name, ret);
925 if(ret != -ENOTSUP)
926 goto fail;
929 if (S_ISDIR(st.st_mode)) {
930 dir_entry = malloc(sizeof(struct directory_name_entry));
931 if (!dir_entry) {
932 ret = -ENOMEM;
933 goto fail;
935 dir_entry->dir_name = cur_file->d_name;
936 dir_entry->path = make_path(parent_dir_entry->path,
937 cur_file->d_name);
938 dir_entry->inum = cur_inum;
939 list_add_tail(&dir_entry->list, &dir_head->list);
940 } else if (S_ISREG(st.st_mode)) {
941 ret = add_file_items(trans, root, &cur_inode,
942 cur_inum, parent_inum, &st,
943 cur_file->d_name, out_fd);
944 if (ret) {
945 error("unable to add file items for %s: %d",
946 cur_file->d_name, ret);
947 goto fail;
949 } else if (S_ISLNK(st.st_mode)) {
950 ret = add_symbolic_link(trans, root,
951 cur_inum, cur_file->d_name);
952 if (ret) {
953 error("unable to add symlink for %s: %d",
954 cur_file->d_name, ret);
955 goto fail;
960 free_namelist(files, count);
961 free(parent_dir_entry);
963 index_cnt = 2;
965 } while (!list_empty(&dir_head->list));
967 out:
968 return !!ret;
969 fail:
970 free_namelist(files, count);
971 fail_no_files:
972 free(parent_dir_entry);
973 goto out;
974 fail_no_dir:
975 free(dir_entry);
976 goto out;
979 static int open_target(char *output_name)
981 int output_fd;
982 output_fd = open(output_name, O_CREAT | O_RDWR,
983 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH);
985 return output_fd;
988 static int create_chunks(struct btrfs_trans_handle *trans,
989 struct btrfs_root *root, u64 num_of_meta_chunks,
990 u64 size_of_data,
991 struct mkfs_allocation *allocation)
993 u64 chunk_start;
994 u64 chunk_size;
995 u64 meta_type = BTRFS_BLOCK_GROUP_METADATA;
996 u64 data_type = BTRFS_BLOCK_GROUP_DATA;
997 u64 minimum_data_chunk_size = 8 * 1024 * 1024;
998 u64 i;
999 int ret;
1001 for (i = 0; i < num_of_meta_chunks; i++) {
1002 ret = btrfs_alloc_chunk(trans, root->fs_info->extent_root,
1003 &chunk_start, &chunk_size, meta_type);
1004 if (ret)
1005 return ret;
1006 ret = btrfs_make_block_group(trans, root->fs_info->extent_root, 0,
1007 meta_type, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
1008 chunk_start, chunk_size);
1009 allocation->metadata += chunk_size;
1010 if (ret)
1011 return ret;
1012 set_extent_dirty(&root->fs_info->free_space_cache,
1013 chunk_start, chunk_start + chunk_size - 1, 0);
1016 if (size_of_data < minimum_data_chunk_size)
1017 size_of_data = minimum_data_chunk_size;
1019 ret = btrfs_alloc_data_chunk(trans, root->fs_info->extent_root,
1020 &chunk_start, size_of_data, data_type, 0);
1021 if (ret)
1022 return ret;
1023 ret = btrfs_make_block_group(trans, root->fs_info->extent_root, 0,
1024 data_type, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
1025 chunk_start, size_of_data);
1026 allocation->data += size_of_data;
1027 if (ret)
1028 return ret;
1029 set_extent_dirty(&root->fs_info->free_space_cache,
1030 chunk_start, chunk_start + size_of_data - 1, 0);
1031 return ret;
1034 static int make_image(char *source_dir, struct btrfs_root *root, int out_fd)
1036 int ret;
1037 struct btrfs_trans_handle *trans;
1038 struct stat root_st;
1039 struct directory_name_entry dir_head;
1040 struct directory_name_entry *dir_entry = NULL;
1042 ret = lstat(source_dir, &root_st);
1043 if (ret) {
1044 error("unable to lstat %s: %s", source_dir, strerror(errno));
1045 ret = -errno;
1046 goto out;
1049 INIT_LIST_HEAD(&dir_head.list);
1051 trans = btrfs_start_transaction(root, 1);
1052 ret = traverse_directory(trans, root, source_dir, &dir_head, out_fd);
1053 if (ret) {
1054 error("unable to traverse directory %s: %d", source_dir, ret);
1055 goto fail;
1057 ret = btrfs_commit_transaction(trans, root);
1058 if (ret) {
1059 error("transaction commit failed: %d", ret);
1060 goto out;
1063 if (verbose)
1064 printf("Making image is completed.\n");
1065 return 0;
1066 fail:
1067 while (!list_empty(&dir_head.list)) {
1068 dir_entry = list_entry(dir_head.list.next,
1069 struct directory_name_entry, list);
1070 list_del(&dir_entry->list);
1071 free(dir_entry);
1073 out:
1074 return ret;
1078 * This ignores symlinks with unreadable targets and subdirs that can't
1079 * be read. It's a best-effort to give a rough estimate of the size of
1080 * a subdir. It doesn't guarantee that prepopulating btrfs from this
1081 * tree won't still run out of space.
1083 static u64 global_total_size;
1084 static u64 fs_block_size;
1085 static int ftw_add_entry_size(const char *fpath, const struct stat *st,
1086 int type)
1088 if (type == FTW_F || type == FTW_D)
1089 global_total_size += round_up(st->st_size, fs_block_size);
1091 return 0;
1094 static u64 size_sourcedir(char *dir_name, u64 sectorsize,
1095 u64 *num_of_meta_chunks_ret, u64 *size_of_data_ret)
1097 u64 dir_size = 0;
1098 u64 total_size = 0;
1099 int ret;
1100 u64 default_chunk_size = 8 * 1024 * 1024; /* 8MB */
1101 u64 allocated_meta_size = 8 * 1024 * 1024; /* 8MB */
1102 u64 allocated_total_size = 20 * 1024 * 1024; /* 20MB */
1103 u64 num_of_meta_chunks = 0;
1104 u64 num_of_data_chunks = 0;
1105 u64 num_of_allocated_meta_chunks =
1106 allocated_meta_size / default_chunk_size;
1108 global_total_size = 0;
1109 fs_block_size = sectorsize;
1110 ret = ftw(dir_name, ftw_add_entry_size, 10);
1111 dir_size = global_total_size;
1112 if (ret < 0) {
1113 error("ftw subdir walk of %s failed: %s", dir_name,
1114 strerror(errno));
1115 exit(1);
1118 num_of_data_chunks = (dir_size + default_chunk_size - 1) /
1119 default_chunk_size;
1121 num_of_meta_chunks = (dir_size / 2) / default_chunk_size;
1122 if (((dir_size / 2) % default_chunk_size) != 0)
1123 num_of_meta_chunks++;
1124 if (num_of_meta_chunks <= num_of_allocated_meta_chunks)
1125 num_of_meta_chunks = 0;
1126 else
1127 num_of_meta_chunks -= num_of_allocated_meta_chunks;
1129 total_size = allocated_total_size +
1130 (num_of_data_chunks * default_chunk_size) +
1131 (num_of_meta_chunks * default_chunk_size);
1133 *num_of_meta_chunks_ret = num_of_meta_chunks;
1134 *size_of_data_ret = num_of_data_chunks * default_chunk_size;
1135 return total_size;
1138 static int zero_output_file(int out_fd, u64 size)
1140 int loop_num;
1141 u64 location = 0;
1142 char buf[4096];
1143 int ret = 0, i;
1144 ssize_t written;
1146 memset(buf, 0, 4096);
1147 loop_num = size / 4096;
1148 for (i = 0; i < loop_num; i++) {
1149 written = pwrite64(out_fd, buf, 4096, location);
1150 if (written != 4096)
1151 ret = -EIO;
1152 location += 4096;
1154 return ret;
1157 static int is_ssd(const char *file)
1159 blkid_probe probe;
1160 char wholedisk[PATH_MAX];
1161 char sysfs_path[PATH_MAX];
1162 dev_t devno;
1163 int fd;
1164 char rotational;
1165 int ret;
1167 probe = blkid_new_probe_from_filename(file);
1168 if (!probe)
1169 return 0;
1171 /* Device number of this disk (possibly a partition) */
1172 devno = blkid_probe_get_devno(probe);
1173 if (!devno) {
1174 blkid_free_probe(probe);
1175 return 0;
1178 /* Get whole disk name (not full path) for this devno */
1179 ret = blkid_devno_to_wholedisk(devno,
1180 wholedisk, sizeof(wholedisk), NULL);
1181 if (ret) {
1182 blkid_free_probe(probe);
1183 return 0;
1186 snprintf(sysfs_path, PATH_MAX, "/sys/block/%s/queue/rotational",
1187 wholedisk);
1189 blkid_free_probe(probe);
1191 fd = open(sysfs_path, O_RDONLY);
1192 if (fd < 0) {
1193 return 0;
1196 if (read(fd, &rotational, sizeof(char)) < sizeof(char)) {
1197 close(fd);
1198 return 0;
1200 close(fd);
1202 return !atoi((const char *)&rotational);
1205 static int _cmp_device_by_id(void *priv, struct list_head *a,
1206 struct list_head *b)
1208 return list_entry(a, struct btrfs_device, dev_list)->devid -
1209 list_entry(b, struct btrfs_device, dev_list)->devid;
1212 static void list_all_devices(struct btrfs_root *root)
1214 struct btrfs_fs_devices *fs_devices;
1215 struct btrfs_device *device;
1216 int number_of_devices = 0;
1217 u64 total_block_count = 0;
1219 fs_devices = root->fs_info->fs_devices;
1221 list_for_each_entry(device, &fs_devices->devices, dev_list)
1222 number_of_devices++;
1224 list_sort(NULL, &fs_devices->devices, _cmp_device_by_id);
1226 printf("Number of devices: %d\n", number_of_devices);
1227 /* printf("Total devices size: %10s\n", */
1228 /* pretty_size(total_block_count)); */
1229 printf("Devices:\n");
1230 printf(" ID SIZE PATH\n");
1231 list_for_each_entry(device, &fs_devices->devices, dev_list) {
1232 printf(" %3llu %10s %s\n",
1233 device->devid,
1234 pretty_size(device->total_bytes),
1235 device->name);
1236 total_block_count += device->total_bytes;
1239 printf("\n");
1242 static int is_temp_block_group(struct extent_buffer *node,
1243 struct btrfs_block_group_item *bgi,
1244 u64 data_profile, u64 meta_profile,
1245 u64 sys_profile)
1247 u64 flag = btrfs_disk_block_group_flags(node, bgi);
1248 u64 flag_type = flag & BTRFS_BLOCK_GROUP_TYPE_MASK;
1249 u64 flag_profile = flag & BTRFS_BLOCK_GROUP_PROFILE_MASK;
1250 u64 used = btrfs_disk_block_group_used(node, bgi);
1253 * Chunks meets all the following conditions is a temp chunk
1254 * 1) Empty chunk
1255 * Temp chunk is always empty.
1257 * 2) profile mismatch with mkfs profile.
1258 * Temp chunk is always in SINGLE
1260 * 3) Size differs with mkfs_alloc
1261 * Special case for SINGLE/SINGLE btrfs.
1262 * In that case, temp data chunk and real data chunk are always empty.
1263 * So we need to use mkfs_alloc to be sure which chunk is the newly
1264 * allocated.
1266 * Normally, new chunk size is equal to mkfs one (One chunk)
1267 * If it has multiple chunks, we just refuse to delete any one.
1268 * As they are all single, so no real problem will happen.
1269 * So only use condition 1) and 2) to judge them.
1271 if (used != 0)
1272 return 0;
1273 switch (flag_type) {
1274 case BTRFS_BLOCK_GROUP_DATA:
1275 case BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA:
1276 data_profile &= BTRFS_BLOCK_GROUP_PROFILE_MASK;
1277 if (flag_profile != data_profile)
1278 return 1;
1279 break;
1280 case BTRFS_BLOCK_GROUP_METADATA:
1281 meta_profile &= BTRFS_BLOCK_GROUP_PROFILE_MASK;
1282 if (flag_profile != meta_profile)
1283 return 1;
1284 break;
1285 case BTRFS_BLOCK_GROUP_SYSTEM:
1286 sys_profile &= BTRFS_BLOCK_GROUP_PROFILE_MASK;
1287 if (flag_profile != sys_profile)
1288 return 1;
1289 break;
1291 return 0;
1294 /* Note: if current is a block group, it will skip it anyway */
1295 static int next_block_group(struct btrfs_root *root,
1296 struct btrfs_path *path)
1298 struct btrfs_key key;
1299 int ret = 0;
1301 while (1) {
1302 ret = btrfs_next_item(root, path);
1303 if (ret)
1304 goto out;
1306 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1307 if (key.type == BTRFS_BLOCK_GROUP_ITEM_KEY)
1308 goto out;
1310 out:
1311 return ret;
1314 /* This function will cleanup */
1315 static int cleanup_temp_chunks(struct btrfs_fs_info *fs_info,
1316 struct mkfs_allocation *alloc,
1317 u64 data_profile, u64 meta_profile,
1318 u64 sys_profile)
1320 struct btrfs_trans_handle *trans = NULL;
1321 struct btrfs_block_group_item *bgi;
1322 struct btrfs_root *root = fs_info->extent_root;
1323 struct btrfs_key key;
1324 struct btrfs_key found_key;
1325 struct btrfs_path *path;
1326 int ret = 0;
1328 path = btrfs_alloc_path();
1329 if (!path) {
1330 ret = -ENOMEM;
1331 goto out;
1334 trans = btrfs_start_transaction(root, 1);
1336 key.objectid = 0;
1337 key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
1338 key.offset = 0;
1340 while (1) {
1342 * as the rest of the loop may modify the tree, we need to
1343 * start a new search each time.
1345 ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
1346 if (ret < 0)
1347 goto out;
1349 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1350 path->slots[0]);
1351 if (found_key.objectid < key.objectid)
1352 goto out;
1353 if (found_key.type != BTRFS_BLOCK_GROUP_ITEM_KEY) {
1354 ret = next_block_group(root, path);
1355 if (ret < 0)
1356 goto out;
1357 if (ret > 0) {
1358 ret = 0;
1359 goto out;
1361 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1362 path->slots[0]);
1365 bgi = btrfs_item_ptr(path->nodes[0], path->slots[0],
1366 struct btrfs_block_group_item);
1367 if (is_temp_block_group(path->nodes[0], bgi,
1368 data_profile, meta_profile,
1369 sys_profile)) {
1370 u64 flags = btrfs_disk_block_group_flags(path->nodes[0],
1371 bgi);
1373 ret = btrfs_free_block_group(trans, fs_info,
1374 found_key.objectid, found_key.offset);
1375 if (ret < 0)
1376 goto out;
1378 if ((flags & BTRFS_BLOCK_GROUP_TYPE_MASK) ==
1379 BTRFS_BLOCK_GROUP_DATA)
1380 alloc->data -= found_key.offset;
1381 else if ((flags & BTRFS_BLOCK_GROUP_TYPE_MASK) ==
1382 BTRFS_BLOCK_GROUP_METADATA)
1383 alloc->metadata -= found_key.offset;
1384 else if ((flags & BTRFS_BLOCK_GROUP_TYPE_MASK) ==
1385 BTRFS_BLOCK_GROUP_SYSTEM)
1386 alloc->system -= found_key.offset;
1387 else if ((flags & BTRFS_BLOCK_GROUP_TYPE_MASK) ==
1388 (BTRFS_BLOCK_GROUP_METADATA |
1389 BTRFS_BLOCK_GROUP_DATA))
1390 alloc->mixed -= found_key.offset;
1392 btrfs_release_path(path);
1393 key.objectid = found_key.objectid + found_key.offset;
1395 out:
1396 if (trans)
1397 btrfs_commit_transaction(trans, root);
1398 btrfs_free_path(path);
1399 return ret;
1402 int main(int argc, char **argv)
1404 char *file;
1405 struct btrfs_root *root;
1406 struct btrfs_fs_info *fs_info;
1407 struct btrfs_trans_handle *trans;
1408 char *label = NULL;
1409 u64 block_count = 0;
1410 u64 dev_block_count = 0;
1411 u64 blocks[7];
1412 u64 alloc_start = 0;
1413 u64 metadata_profile = 0;
1414 u64 data_profile = 0;
1415 u32 nodesize = max_t(u32, sysconf(_SC_PAGESIZE),
1416 BTRFS_MKFS_DEFAULT_NODE_SIZE);
1417 u32 sectorsize = 4096;
1418 u32 stripesize = 4096;
1419 int zero_end = 1;
1420 int fd;
1421 int ret;
1422 int i;
1423 int mixed = 0;
1424 int nodesize_forced = 0;
1425 int data_profile_opt = 0;
1426 int metadata_profile_opt = 0;
1427 int discard = 1;
1428 int ssd = 0;
1429 int force_overwrite = 0;
1430 char *source_dir = NULL;
1431 int source_dir_set = 0;
1432 u64 num_of_meta_chunks = 0;
1433 u64 size_of_data = 0;
1434 u64 source_dir_size = 0;
1435 int dev_cnt = 0;
1436 int saved_optind;
1437 char fs_uuid[BTRFS_UUID_UNPARSED_SIZE] = { 0 };
1438 u64 features = BTRFS_MKFS_DEFAULT_FEATURES;
1439 struct mkfs_allocation allocation = { 0 };
1440 struct btrfs_mkfs_config mkfs_cfg;
1442 while(1) {
1443 int c;
1444 static const struct option long_options[] = {
1445 { "alloc-start", required_argument, NULL, 'A'},
1446 { "byte-count", required_argument, NULL, 'b' },
1447 { "force", no_argument, NULL, 'f' },
1448 { "leafsize", required_argument, NULL, 'l' },
1449 { "label", required_argument, NULL, 'L'},
1450 { "metadata", required_argument, NULL, 'm' },
1451 { "mixed", no_argument, NULL, 'M' },
1452 { "nodesize", required_argument, NULL, 'n' },
1453 { "sectorsize", required_argument, NULL, 's' },
1454 { "data", required_argument, NULL, 'd' },
1455 { "version", no_argument, NULL, 'V' },
1456 { "rootdir", required_argument, NULL, 'r' },
1457 { "nodiscard", no_argument, NULL, 'K' },
1458 { "features", required_argument, NULL, 'O' },
1459 { "uuid", required_argument, NULL, 'U' },
1460 { "quiet", 0, NULL, 'q' },
1461 { "help", no_argument, NULL, GETOPT_VAL_HELP },
1462 { NULL, 0, NULL, 0}
1465 c = getopt_long(argc, argv, "A:b:fl:n:s:m:d:L:O:r:U:VMKq",
1466 long_options, NULL);
1467 if (c < 0)
1468 break;
1469 switch(c) {
1470 case 'A':
1471 alloc_start = parse_size(optarg);
1472 break;
1473 case 'f':
1474 force_overwrite = 1;
1475 break;
1476 case 'd':
1477 data_profile = parse_profile(optarg);
1478 data_profile_opt = 1;
1479 break;
1480 case 'l':
1481 warning("--leafsize is deprecated, use --nodesize");
1482 case 'n':
1483 nodesize = parse_size(optarg);
1484 nodesize_forced = 1;
1485 break;
1486 case 'L':
1487 label = parse_label(optarg);
1488 break;
1489 case 'm':
1490 metadata_profile = parse_profile(optarg);
1491 metadata_profile_opt = 1;
1492 break;
1493 case 'M':
1494 mixed = 1;
1495 break;
1496 case 'O': {
1497 char *orig = strdup(optarg);
1498 char *tmp = orig;
1500 tmp = btrfs_parse_fs_features(tmp, &features);
1501 if (tmp) {
1502 error("unrecognized filesystem feature '%s'",
1503 tmp);
1504 free(orig);
1505 exit(1);
1507 free(orig);
1508 if (features & BTRFS_FEATURE_LIST_ALL) {
1509 btrfs_list_all_fs_features(0);
1510 exit(0);
1512 break;
1514 case 's':
1515 sectorsize = parse_size(optarg);
1516 break;
1517 case 'b':
1518 block_count = parse_size(optarg);
1519 zero_end = 0;
1520 break;
1521 case 'V':
1522 printf("mkfs.btrfs, part of %s\n",
1523 PACKAGE_STRING);
1524 exit(0);
1525 break;
1526 case 'r':
1527 source_dir = optarg;
1528 source_dir_set = 1;
1529 break;
1530 case 'U':
1531 strncpy(fs_uuid, optarg,
1532 BTRFS_UUID_UNPARSED_SIZE - 1);
1533 break;
1534 case 'K':
1535 discard = 0;
1536 break;
1537 case 'q':
1538 verbose = 0;
1539 break;
1540 case GETOPT_VAL_HELP:
1541 default:
1542 print_usage(c != GETOPT_VAL_HELP);
1546 if (verbose) {
1547 printf("%s\n", PACKAGE_STRING);
1548 printf("See %s for more information.\n\n", PACKAGE_URL);
1551 sectorsize = max(sectorsize, (u32)sysconf(_SC_PAGESIZE));
1552 stripesize = sectorsize;
1553 saved_optind = optind;
1554 dev_cnt = argc - optind;
1555 if (dev_cnt == 0)
1556 print_usage(1);
1558 if (source_dir_set && dev_cnt > 1) {
1559 error("the option -r is limited to a single device");
1560 exit(1);
1563 if (*fs_uuid) {
1564 uuid_t dummy_uuid;
1566 if (uuid_parse(fs_uuid, dummy_uuid) != 0) {
1567 error("could not parse UUID: %s", fs_uuid);
1568 exit(1);
1570 if (!test_uuid_unique(fs_uuid)) {
1571 error("non-unique UUID: %s", fs_uuid);
1572 exit(1);
1576 while (dev_cnt-- > 0) {
1577 file = argv[optind++];
1578 if (is_block_device(file) == 1)
1579 if (test_dev_for_mkfs(file, force_overwrite))
1580 exit(1);
1583 optind = saved_optind;
1584 dev_cnt = argc - optind;
1586 file = argv[optind++];
1587 ssd = is_ssd(file);
1590 * Set default profiles according to number of added devices.
1591 * For mixed groups defaults are single/single.
1593 if (!mixed) {
1594 if (!metadata_profile_opt) {
1595 if (dev_cnt == 1 && ssd && verbose)
1596 printf("Detected a SSD, turning off metadata "
1597 "duplication. Mkfs with -m dup if you want to "
1598 "force metadata duplication.\n");
1600 metadata_profile = (dev_cnt > 1) ?
1601 BTRFS_BLOCK_GROUP_RAID1 : (ssd) ?
1602 0: BTRFS_BLOCK_GROUP_DUP;
1604 if (!data_profile_opt) {
1605 data_profile = (dev_cnt > 1) ?
1606 BTRFS_BLOCK_GROUP_RAID0 : 0; /* raid0 or single */
1608 } else {
1609 u32 best_nodesize = max_t(u32, sysconf(_SC_PAGESIZE), sectorsize);
1611 if (metadata_profile_opt || data_profile_opt) {
1612 if (metadata_profile != data_profile) {
1613 error(
1614 "with mixed block groups data and metadata profiles must be the same");
1615 exit(1);
1619 if (!nodesize_forced)
1620 nodesize = best_nodesize;
1624 * FS features that can be set by other means than -O
1625 * just set the bit here
1627 if (mixed)
1628 features |= BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS;
1630 if ((data_profile | metadata_profile) &
1631 (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6)) {
1632 features |= BTRFS_FEATURE_INCOMPAT_RAID56;
1635 if (btrfs_check_nodesize(nodesize, sectorsize,
1636 features))
1637 exit(1);
1639 /* Check device/block_count after the nodesize is determined */
1640 if (block_count && block_count < btrfs_min_dev_size(nodesize)) {
1641 error("size %llu is too small to make a usable filesystem",
1642 block_count);
1643 error("minimum size for btrfs filesystem is %llu",
1644 btrfs_min_dev_size(nodesize));
1645 exit(1);
1647 for (i = saved_optind; i < saved_optind + dev_cnt; i++) {
1648 char *path;
1650 path = argv[i];
1651 ret = test_minimum_size(path, nodesize);
1652 if (ret < 0) {
1653 error("failed to check size for %s: %s",
1654 path, strerror(-ret));
1655 exit (1);
1657 if (ret > 0) {
1658 error("'%s' is too small to make a usable filesystem",
1659 path);
1660 error("minimum size for each btrfs device is %llu",
1661 btrfs_min_dev_size(nodesize));
1662 exit(1);
1665 ret = test_num_disk_vs_raid(metadata_profile, data_profile,
1666 dev_cnt, mixed, ssd);
1667 if (ret)
1668 exit(1);
1670 dev_cnt--;
1672 if (!source_dir_set) {
1674 * open without O_EXCL so that the problem should not
1675 * occur by the following processing.
1676 * (btrfs_register_one_device() fails if O_EXCL is on)
1678 fd = open(file, O_RDWR);
1679 if (fd < 0) {
1680 error("unable to open %s: %s", file, strerror(errno));
1681 exit(1);
1683 ret = btrfs_prepare_device(fd, file, &dev_block_count,
1684 block_count,
1685 (zero_end ? PREP_DEVICE_ZERO_END : 0) |
1686 (discard ? PREP_DEVICE_DISCARD : 0) |
1687 (verbose ? PREP_DEVICE_VERBOSE : 0));
1688 if (ret) {
1689 close(fd);
1690 exit(1);
1692 if (block_count && block_count > dev_block_count) {
1693 error("%s is smaller than requested size, expected %llu, found %llu",
1694 file,
1695 (unsigned long long)block_count,
1696 (unsigned long long)dev_block_count);
1697 exit(1);
1699 } else {
1700 fd = open_target(file);
1701 if (fd < 0) {
1702 error("unable to open %s: %s", file, strerror(errno));
1703 exit(1);
1706 source_dir_size = size_sourcedir(source_dir, sectorsize,
1707 &num_of_meta_chunks, &size_of_data);
1708 if(block_count < source_dir_size)
1709 block_count = source_dir_size;
1710 ret = zero_output_file(fd, block_count);
1711 if (ret) {
1712 error("unable to zero the output file");
1713 exit(1);
1715 /* our "device" is the new image file */
1716 dev_block_count = block_count;
1719 /* To create the first block group and chunk 0 in make_btrfs */
1720 if (dev_block_count < BTRFS_MKFS_SYSTEM_GROUP_SIZE) {
1721 error("device is too small to make filesystem, must be at least %llu",
1722 (unsigned long long)BTRFS_MKFS_SYSTEM_GROUP_SIZE);
1723 exit(1);
1726 blocks[0] = BTRFS_SUPER_INFO_OFFSET;
1727 for (i = 1; i < 7; i++) {
1728 blocks[i] = BTRFS_SUPER_INFO_OFFSET + 1024 * 1024 +
1729 nodesize * i;
1732 if (group_profile_max_safe_loss(metadata_profile) <
1733 group_profile_max_safe_loss(data_profile)){
1734 warning("metadata has lower redundancy than data!\n");
1737 mkfs_cfg.label = label;
1738 mkfs_cfg.fs_uuid = fs_uuid;
1739 memcpy(mkfs_cfg.blocks, blocks, sizeof(blocks));
1740 mkfs_cfg.num_bytes = dev_block_count;
1741 mkfs_cfg.nodesize = nodesize;
1742 mkfs_cfg.sectorsize = sectorsize;
1743 mkfs_cfg.stripesize = stripesize;
1744 mkfs_cfg.features = features;
1746 ret = make_btrfs(fd, &mkfs_cfg, NULL);
1747 if (ret) {
1748 error("error during mkfs: %s", strerror(-ret));
1749 exit(1);
1752 fs_info = open_ctree_fs_info(file, 0, 0, 0,
1753 OPEN_CTREE_WRITES | OPEN_CTREE_FS_PARTIAL);
1754 if (!fs_info) {
1755 error("open ctree failed");
1756 close(fd);
1757 exit(1);
1759 root = fs_info->fs_root;
1760 fs_info->alloc_start = alloc_start;
1762 ret = create_metadata_block_groups(root, mixed, &allocation);
1763 if (ret) {
1764 error("failed to create default block groups: %d", ret);
1765 exit(1);
1768 trans = btrfs_start_transaction(root, 1);
1769 if (!trans) {
1770 error("failed to start transaction");
1771 exit(1);
1774 ret = create_data_block_groups(trans, root, mixed, &allocation);
1775 if (ret) {
1776 error("failed to create default data block groups: %d", ret);
1777 exit(1);
1780 ret = make_root_dir(trans, root, &allocation);
1781 if (ret) {
1782 error("failed to setup the root directory: %d", ret);
1783 exit(1);
1786 ret = btrfs_commit_transaction(trans, root);
1787 if (ret) {
1788 error("unable to commit transaction: %d", ret);
1789 goto out;
1792 trans = btrfs_start_transaction(root, 1);
1793 if (!trans) {
1794 error("failed to start transaction");
1795 exit(1);
1798 if (dev_cnt == 0)
1799 goto raid_groups;
1801 while (dev_cnt-- > 0) {
1802 file = argv[optind++];
1805 * open without O_EXCL so that the problem should not
1806 * occur by the following processing.
1807 * (btrfs_register_one_device() fails if O_EXCL is on)
1809 fd = open(file, O_RDWR);
1810 if (fd < 0) {
1811 error("unable to open %s: %s", file, strerror(errno));
1812 exit(1);
1814 ret = btrfs_device_already_in_root(root, fd,
1815 BTRFS_SUPER_INFO_OFFSET);
1816 if (ret) {
1817 error("skipping duplicate device %s in the filesystem",
1818 file);
1819 close(fd);
1820 continue;
1822 ret = btrfs_prepare_device(fd, file, &dev_block_count,
1823 block_count,
1824 (verbose ? PREP_DEVICE_VERBOSE : 0) |
1825 (zero_end ? PREP_DEVICE_ZERO_END : 0) |
1826 (discard ? PREP_DEVICE_DISCARD : 0));
1827 if (ret) {
1828 close(fd);
1829 exit(1);
1832 ret = btrfs_add_to_fsid(trans, root, fd, file, dev_block_count,
1833 sectorsize, sectorsize, sectorsize);
1834 if (ret) {
1835 error("unable to add %s to filesystem: %d", file, ret);
1836 goto out;
1838 if (verbose >= 2) {
1839 struct btrfs_device *device;
1841 device = container_of(fs_info->fs_devices->devices.next,
1842 struct btrfs_device, dev_list);
1843 printf("adding device %s id %llu\n", file,
1844 (unsigned long long)device->devid);
1848 raid_groups:
1849 if (!source_dir_set) {
1850 ret = create_raid_groups(trans, root, data_profile,
1851 metadata_profile, mixed, &allocation);
1852 if (ret) {
1853 error("unable to create raid groups: %d", ret);
1854 goto out;
1858 ret = create_data_reloc_tree(trans, root);
1859 if (ret) {
1860 error("unable to create data reloc tree: %d", ret);
1861 goto out;
1864 ret = btrfs_commit_transaction(trans, root);
1865 if (ret) {
1866 error("unable to commit transaction: %d", ret);
1867 goto out;
1870 if (source_dir_set) {
1871 trans = btrfs_start_transaction(root, 1);
1872 ret = create_chunks(trans, root,
1873 num_of_meta_chunks, size_of_data,
1874 &allocation);
1875 if (ret) {
1876 error("unable to create chunks: %d", ret);
1877 goto out;
1879 ret = btrfs_commit_transaction(trans, root);
1880 if (ret) {
1881 error("transaction commit failed: %d", ret);
1882 goto out;
1885 ret = make_image(source_dir, root, fd);
1886 if (ret) {
1887 error("error wihle filling filesystem: %d", ret);
1888 goto out;
1891 ret = cleanup_temp_chunks(fs_info, &allocation, data_profile,
1892 metadata_profile, metadata_profile);
1893 if (ret < 0) {
1894 error("failed to cleanup temporary chunks: %d", ret);
1895 goto out;
1898 if (verbose) {
1899 char features_buf[64];
1901 printf("Label: %s\n", label);
1902 printf("UUID: %s\n", fs_uuid);
1903 printf("Node size: %u\n", nodesize);
1904 printf("Sector size: %u\n", sectorsize);
1905 printf("Filesystem size: %s\n",
1906 pretty_size(btrfs_super_total_bytes(fs_info->super_copy)));
1907 printf("Block group profiles:\n");
1908 if (allocation.data)
1909 printf(" Data: %-8s %16s\n",
1910 btrfs_group_profile_str(data_profile),
1911 pretty_size(allocation.data));
1912 if (allocation.metadata)
1913 printf(" Metadata: %-8s %16s\n",
1914 btrfs_group_profile_str(metadata_profile),
1915 pretty_size(allocation.metadata));
1916 if (allocation.mixed)
1917 printf(" Data+Metadata: %-8s %16s\n",
1918 btrfs_group_profile_str(data_profile),
1919 pretty_size(allocation.mixed));
1920 printf(" System: %-8s %16s\n",
1921 btrfs_group_profile_str(metadata_profile),
1922 pretty_size(allocation.system));
1923 printf("SSD detected: %s\n", ssd ? "yes" : "no");
1924 btrfs_parse_features_to_string(features_buf, features);
1925 printf("Incompat features: %s", features_buf);
1926 printf("\n");
1928 list_all_devices(root);
1932 * The filesystem is now fully set up, commit the remaining changes and
1933 * fix the signature as the last step before closing the devices.
1935 fs_info->finalize_on_close = 1;
1936 out:
1937 ret = close_ctree(root);
1939 if (!ret) {
1940 optind = saved_optind;
1941 dev_cnt = argc - optind;
1942 while (dev_cnt-- > 0) {
1943 file = argv[optind++];
1944 if (is_block_device(file) == 1)
1945 btrfs_register_one_device(file);
1949 btrfs_close_all_devices();
1950 free(label);
1952 return !!ret;