btrfs-progs: Doc: Fix format error in btrfs-send
[btrfs-progs-unstable/devel.git] / mkfs.c
blobf30f05742c2ad8e38c62801d841397761549ca64
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 BUG_ON(ret);
84 if (mixed) {
85 ret = btrfs_alloc_chunk(trans, root->fs_info->extent_root,
86 &chunk_start, &chunk_size,
87 BTRFS_BLOCK_GROUP_METADATA |
88 BTRFS_BLOCK_GROUP_DATA);
89 if (ret == -ENOSPC) {
90 fprintf(stderr,
91 "no space to allocate data/metadata chunk\n");
92 goto err;
94 BUG_ON(ret);
95 ret = btrfs_make_block_group(trans, root, 0,
96 BTRFS_BLOCK_GROUP_METADATA |
97 BTRFS_BLOCK_GROUP_DATA,
98 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
99 chunk_start, chunk_size);
100 BUG_ON(ret);
101 allocation->mixed += chunk_size;
102 } else {
103 ret = btrfs_alloc_chunk(trans, root->fs_info->extent_root,
104 &chunk_start, &chunk_size,
105 BTRFS_BLOCK_GROUP_METADATA);
106 if (ret == -ENOSPC) {
107 fprintf(stderr, "no space to allocate metadata chunk\n");
108 goto err;
110 BUG_ON(ret);
111 ret = btrfs_make_block_group(trans, root, 0,
112 BTRFS_BLOCK_GROUP_METADATA,
113 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
114 chunk_start, chunk_size);
115 allocation->metadata += chunk_size;
116 BUG_ON(ret);
119 root->fs_info->system_allocs = 0;
120 btrfs_commit_transaction(trans, root);
122 err:
123 return ret;
126 static int create_data_block_groups(struct btrfs_trans_handle *trans,
127 struct btrfs_root *root, int mixed,
128 struct mkfs_allocation *allocation)
130 u64 chunk_start = 0;
131 u64 chunk_size = 0;
132 int ret = 0;
134 if (!mixed) {
135 ret = btrfs_alloc_chunk(trans, root->fs_info->extent_root,
136 &chunk_start, &chunk_size,
137 BTRFS_BLOCK_GROUP_DATA);
138 if (ret == -ENOSPC) {
139 fprintf(stderr, "no space to allocate data chunk\n");
140 goto err;
142 BUG_ON(ret);
143 ret = btrfs_make_block_group(trans, root, 0,
144 BTRFS_BLOCK_GROUP_DATA,
145 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
146 chunk_start, chunk_size);
147 allocation->data += chunk_size;
148 BUG_ON(ret);
151 err:
152 return ret;
155 static int make_root_dir(struct btrfs_trans_handle *trans, struct btrfs_root *root,
156 struct mkfs_allocation *allocation)
158 struct btrfs_key location;
159 int ret;
161 ret = btrfs_make_root_dir(trans, root->fs_info->tree_root,
162 BTRFS_ROOT_TREE_DIR_OBJECTID);
163 if (ret)
164 goto err;
165 ret = btrfs_make_root_dir(trans, root, BTRFS_FIRST_FREE_OBJECTID);
166 if (ret)
167 goto err;
168 memcpy(&location, &root->fs_info->fs_root->root_key, sizeof(location));
169 location.offset = (u64)-1;
170 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
171 "default", 7,
172 btrfs_super_root_dir(root->fs_info->super_copy),
173 &location, BTRFS_FT_DIR, 0);
174 if (ret)
175 goto err;
177 ret = btrfs_insert_inode_ref(trans, root->fs_info->tree_root,
178 "default", 7, location.objectid,
179 BTRFS_ROOT_TREE_DIR_OBJECTID, 0);
180 if (ret)
181 goto err;
183 err:
184 return ret;
187 static void __recow_root(struct btrfs_trans_handle *trans,
188 struct btrfs_root *root)
190 int ret;
191 struct extent_buffer *tmp;
193 if (trans->transid != btrfs_root_generation(&root->root_item)) {
194 extent_buffer_get(root->node);
195 ret = __btrfs_cow_block(trans, root, root->node,
196 NULL, 0, &tmp, 0, 0);
197 BUG_ON(ret);
198 free_extent_buffer(tmp);
202 static void recow_roots(struct btrfs_trans_handle *trans,
203 struct btrfs_root *root)
205 struct btrfs_fs_info *info = root->fs_info;
207 __recow_root(trans, info->fs_root);
208 __recow_root(trans, info->tree_root);
209 __recow_root(trans, info->extent_root);
210 __recow_root(trans, info->chunk_root);
211 __recow_root(trans, info->dev_root);
212 __recow_root(trans, info->csum_root);
215 static int create_one_raid_group(struct btrfs_trans_handle *trans,
216 struct btrfs_root *root, u64 type,
217 struct mkfs_allocation *allocation)
220 u64 chunk_start;
221 u64 chunk_size;
222 int ret;
224 ret = btrfs_alloc_chunk(trans, root->fs_info->extent_root,
225 &chunk_start, &chunk_size, type);
226 if (ret == -ENOSPC) {
227 fprintf(stderr, "not enough free space\n");
228 exit(1);
230 BUG_ON(ret);
231 ret = btrfs_make_block_group(trans, root->fs_info->extent_root, 0,
232 type, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
233 chunk_start, chunk_size);
234 if ((type & BTRFS_BLOCK_GROUP_TYPE_MASK) == BTRFS_BLOCK_GROUP_DATA)
235 allocation->data += chunk_size;
236 else if ((type & BTRFS_BLOCK_GROUP_TYPE_MASK) == BTRFS_BLOCK_GROUP_METADATA)
237 allocation->metadata += chunk_size;
238 else if ((type & BTRFS_BLOCK_GROUP_TYPE_MASK) == BTRFS_BLOCK_GROUP_SYSTEM)
239 allocation->system += chunk_size;
240 else if ((type & BTRFS_BLOCK_GROUP_TYPE_MASK) ==
241 (BTRFS_BLOCK_GROUP_METADATA|BTRFS_BLOCK_GROUP_DATA))
242 allocation->mixed += chunk_size;
243 else
244 BUG_ON(1);
246 BUG_ON(ret);
247 return ret;
250 static int create_raid_groups(struct btrfs_trans_handle *trans,
251 struct btrfs_root *root, u64 data_profile,
252 u64 metadata_profile, int mixed,
253 struct mkfs_allocation *allocation)
255 int ret;
257 if (metadata_profile) {
258 u64 meta_flags = BTRFS_BLOCK_GROUP_METADATA;
260 ret = create_one_raid_group(trans, root,
261 BTRFS_BLOCK_GROUP_SYSTEM |
262 metadata_profile, allocation);
263 BUG_ON(ret);
265 if (mixed)
266 meta_flags |= BTRFS_BLOCK_GROUP_DATA;
268 ret = create_one_raid_group(trans, root, meta_flags |
269 metadata_profile, allocation);
270 BUG_ON(ret);
273 if (!mixed && data_profile) {
274 ret = create_one_raid_group(trans, root,
275 BTRFS_BLOCK_GROUP_DATA |
276 data_profile, allocation);
277 BUG_ON(ret);
279 recow_roots(trans, root);
281 return 0;
284 static int create_data_reloc_tree(struct btrfs_trans_handle *trans,
285 struct btrfs_root *root)
287 struct btrfs_key location;
288 struct btrfs_root_item root_item;
289 struct extent_buffer *tmp;
290 u64 objectid = BTRFS_DATA_RELOC_TREE_OBJECTID;
291 int ret;
293 ret = btrfs_copy_root(trans, root, root->node, &tmp, objectid);
294 BUG_ON(ret);
296 memcpy(&root_item, &root->root_item, sizeof(root_item));
297 btrfs_set_root_bytenr(&root_item, tmp->start);
298 btrfs_set_root_level(&root_item, btrfs_header_level(tmp));
299 btrfs_set_root_generation(&root_item, trans->transid);
300 free_extent_buffer(tmp);
302 location.objectid = objectid;
303 location.type = BTRFS_ROOT_ITEM_KEY;
304 location.offset = 0;
305 ret = btrfs_insert_root(trans, root->fs_info->tree_root,
306 &location, &root_item);
307 BUG_ON(ret);
308 return 0;
311 static void print_usage(int ret)
313 fprintf(stderr, "usage: mkfs.btrfs [options] dev [ dev ... ]\n");
314 fprintf(stderr, "options:\n");
315 fprintf(stderr, "\t-A|--alloc-start START the offset to start the FS\n");
316 fprintf(stderr, "\t-b|--byte-count SIZE total number of bytes in the FS\n");
317 fprintf(stderr, "\t-d|--data PROFILE data profile, raid0, raid1, raid5, raid6, raid10, dup or single\n");
318 fprintf(stderr, "\t-f|--force force overwrite of existing filesystem\n");
319 fprintf(stderr, "\t-l|--leafsize SIZE deprecated, alias for nodesize\n");
320 fprintf(stderr, "\t-L|--label LABEL set a label\n");
321 fprintf(stderr, "\t-m|--metadata PROFILE metadata profile, values like data profile\n");
322 fprintf(stderr, "\t-M|--mixed mix metadata and data together\n");
323 fprintf(stderr, "\t-n|--nodesize SIZE size of btree nodes\n");
324 fprintf(stderr, "\t-s|--sectorsize SIZE min block allocation (may not mountable by current kernel)\n");
325 fprintf(stderr, "\t-r|--rootdir DIR the source directory\n");
326 fprintf(stderr, "\t-K|--nodiscard do not perform whole device TRIM\n");
327 fprintf(stderr, "\t-O|--features LIST comma separated list of filesystem features, use '-O list-all' to list features\n");
328 fprintf(stderr, "\t-U|--uuid UUID specify the filesystem UUID\n");
329 fprintf(stderr, "\t-q|--quiet no messages except errors\n");
330 fprintf(stderr, "\t-V|--version print the mkfs.btrfs version and exit\n");
331 exit(ret);
334 static void print_version(void) __attribute__((noreturn));
335 static void print_version(void)
337 fprintf(stderr, "mkfs.btrfs, part of %s\n", PACKAGE_STRING);
338 exit(0);
341 static u64 parse_profile(char *s)
343 if (strcasecmp(s, "raid0") == 0) {
344 return BTRFS_BLOCK_GROUP_RAID0;
345 } else if (strcasecmp(s, "raid1") == 0) {
346 return BTRFS_BLOCK_GROUP_RAID1;
347 } else if (strcasecmp(s, "raid5") == 0) {
348 return BTRFS_BLOCK_GROUP_RAID5;
349 } else if (strcasecmp(s, "raid6") == 0) {
350 return BTRFS_BLOCK_GROUP_RAID6;
351 } else if (strcasecmp(s, "raid10") == 0) {
352 return BTRFS_BLOCK_GROUP_RAID10;
353 } else if (strcasecmp(s, "dup") == 0) {
354 return BTRFS_BLOCK_GROUP_DUP;
355 } else if (strcasecmp(s, "single") == 0) {
356 return 0;
357 } else {
358 fprintf(stderr, "Unknown profile %s\n", s);
359 exit(1);
361 /* not reached */
362 return 0;
365 static char *parse_label(char *input)
367 int len = strlen(input);
369 if (len >= BTRFS_LABEL_SIZE) {
370 fprintf(stderr, "Label %s is too long (max %d)\n", input,
371 BTRFS_LABEL_SIZE - 1);
372 exit(1);
374 return strdup(input);
377 static int add_directory_items(struct btrfs_trans_handle *trans,
378 struct btrfs_root *root, u64 objectid,
379 ino_t parent_inum, const char *name,
380 struct stat *st, int *dir_index_cnt)
382 int ret;
383 int name_len;
384 struct btrfs_key location;
385 u8 filetype = 0;
387 name_len = strlen(name);
389 location.objectid = objectid;
390 location.offset = 0;
391 btrfs_set_key_type(&location, BTRFS_INODE_ITEM_KEY);
393 if (S_ISDIR(st->st_mode))
394 filetype = BTRFS_FT_DIR;
395 if (S_ISREG(st->st_mode))
396 filetype = BTRFS_FT_REG_FILE;
397 if (S_ISLNK(st->st_mode))
398 filetype = BTRFS_FT_SYMLINK;
400 ret = btrfs_insert_dir_item(trans, root, name, name_len,
401 parent_inum, &location,
402 filetype, index_cnt);
403 if (ret)
404 return ret;
405 ret = btrfs_insert_inode_ref(trans, root, name, name_len,
406 objectid, parent_inum, index_cnt);
407 *dir_index_cnt = index_cnt;
408 index_cnt++;
410 return ret;
413 static int fill_inode_item(struct btrfs_trans_handle *trans,
414 struct btrfs_root *root,
415 struct btrfs_inode_item *dst, struct stat *src)
417 u64 blocks = 0;
418 u64 sectorsize = root->sectorsize;
421 * btrfs_inode_item has some reserved fields
422 * and represents on-disk inode entry, so
423 * zero everything to prevent information leak
425 memset(dst, 0, sizeof (*dst));
427 btrfs_set_stack_inode_generation(dst, trans->transid);
428 btrfs_set_stack_inode_size(dst, src->st_size);
429 btrfs_set_stack_inode_nbytes(dst, 0);
430 btrfs_set_stack_inode_block_group(dst, 0);
431 btrfs_set_stack_inode_nlink(dst, src->st_nlink);
432 btrfs_set_stack_inode_uid(dst, src->st_uid);
433 btrfs_set_stack_inode_gid(dst, src->st_gid);
434 btrfs_set_stack_inode_mode(dst, src->st_mode);
435 btrfs_set_stack_inode_rdev(dst, 0);
436 btrfs_set_stack_inode_flags(dst, 0);
437 btrfs_set_stack_timespec_sec(&dst->atime, src->st_atime);
438 btrfs_set_stack_timespec_nsec(&dst->atime, 0);
439 btrfs_set_stack_timespec_sec(&dst->ctime, src->st_ctime);
440 btrfs_set_stack_timespec_nsec(&dst->ctime, 0);
441 btrfs_set_stack_timespec_sec(&dst->mtime, src->st_mtime);
442 btrfs_set_stack_timespec_nsec(&dst->mtime, 0);
443 btrfs_set_stack_timespec_sec(&dst->otime, 0);
444 btrfs_set_stack_timespec_nsec(&dst->otime, 0);
446 if (S_ISDIR(src->st_mode)) {
447 btrfs_set_stack_inode_size(dst, 0);
448 btrfs_set_stack_inode_nlink(dst, 1);
450 if (S_ISREG(src->st_mode)) {
451 btrfs_set_stack_inode_size(dst, (u64)src->st_size);
452 if (src->st_size <= BTRFS_MAX_INLINE_DATA_SIZE(root))
453 btrfs_set_stack_inode_nbytes(dst, src->st_size);
454 else {
455 blocks = src->st_size / sectorsize;
456 if (src->st_size % sectorsize)
457 blocks += 1;
458 blocks *= sectorsize;
459 btrfs_set_stack_inode_nbytes(dst, blocks);
462 if (S_ISLNK(src->st_mode))
463 btrfs_set_stack_inode_nbytes(dst, src->st_size + 1);
465 return 0;
468 static int directory_select(const struct direct *entry)
470 if ((strncmp(entry->d_name, ".", entry->d_reclen) == 0) ||
471 (strncmp(entry->d_name, "..", entry->d_reclen) == 0))
472 return 0;
473 else
474 return 1;
477 static void free_namelist(struct direct **files, int count)
479 int i;
481 if (count < 0)
482 return;
484 for (i = 0; i < count; ++i)
485 free(files[i]);
486 free(files);
489 static u64 calculate_dir_inode_size(char *dirname)
491 int count, i;
492 struct direct **files, *cur_file;
493 u64 dir_inode_size = 0;
495 count = scandir(dirname, &files, directory_select, NULL);
497 for (i = 0; i < count; i++) {
498 cur_file = files[i];
499 dir_inode_size += strlen(cur_file->d_name);
502 free_namelist(files, count);
504 dir_inode_size *= 2;
505 return dir_inode_size;
508 static int add_inode_items(struct btrfs_trans_handle *trans,
509 struct btrfs_root *root,
510 struct stat *st, char *name,
511 u64 self_objectid, ino_t parent_inum,
512 int dir_index_cnt, struct btrfs_inode_item *inode_ret)
514 int ret;
515 struct btrfs_key inode_key;
516 struct btrfs_inode_item btrfs_inode;
517 u64 objectid;
518 u64 inode_size = 0;
520 fill_inode_item(trans, root, &btrfs_inode, st);
521 objectid = self_objectid;
523 if (S_ISDIR(st->st_mode)) {
524 inode_size = calculate_dir_inode_size(name);
525 btrfs_set_stack_inode_size(&btrfs_inode, inode_size);
528 inode_key.objectid = objectid;
529 inode_key.offset = 0;
530 btrfs_set_key_type(&inode_key, BTRFS_INODE_ITEM_KEY);
532 ret = btrfs_insert_inode(trans, root, objectid, &btrfs_inode);
534 *inode_ret = btrfs_inode;
535 return ret;
538 static int add_xattr_item(struct btrfs_trans_handle *trans,
539 struct btrfs_root *root, u64 objectid,
540 const char *file_name)
542 int ret;
543 int cur_name_len;
544 char xattr_list[XATTR_LIST_MAX];
545 char *cur_name;
546 char cur_value[XATTR_SIZE_MAX];
547 char delimiter = '\0';
548 char *next_location = xattr_list;
550 ret = llistxattr(file_name, xattr_list, XATTR_LIST_MAX);
551 if (ret < 0) {
552 if(errno == ENOTSUP)
553 return 0;
554 fprintf(stderr, "get a list of xattr failed for %s\n",
555 file_name);
556 return ret;
558 if (ret == 0)
559 return ret;
561 cur_name = strtok(xattr_list, &delimiter);
562 while (cur_name != NULL) {
563 cur_name_len = strlen(cur_name);
564 next_location += cur_name_len + 1;
566 ret = getxattr(file_name, cur_name, cur_value, XATTR_SIZE_MAX);
567 if (ret < 0) {
568 if(errno == ENOTSUP)
569 return 0;
570 fprintf(stderr, "get a xattr value failed for %s attr %s\n",
571 file_name, cur_name);
572 return ret;
575 ret = btrfs_insert_xattr_item(trans, root, cur_name,
576 cur_name_len, cur_value,
577 ret, objectid);
578 if (ret) {
579 fprintf(stderr, "insert a xattr item failed for %s\n",
580 file_name);
583 cur_name = strtok(next_location, &delimiter);
586 return ret;
589 static int add_symbolic_link(struct btrfs_trans_handle *trans,
590 struct btrfs_root *root,
591 u64 objectid, const char *path_name)
593 int ret;
594 char buf[PATH_MAX];
596 ret = readlink(path_name, buf, sizeof(buf));
597 if (ret <= 0) {
598 fprintf(stderr, "readlink failed for %s\n", path_name);
599 goto fail;
601 if (ret >= sizeof(buf)) {
602 fprintf(stderr, "symlink too long for %s\n", path_name);
603 ret = -1;
604 goto fail;
607 buf[ret] = '\0'; /* readlink does not do it for us */
608 ret = btrfs_insert_inline_extent(trans, root, objectid, 0,
609 buf, ret + 1);
610 fail:
611 return ret;
614 static int add_file_items(struct btrfs_trans_handle *trans,
615 struct btrfs_root *root,
616 struct btrfs_inode_item *btrfs_inode, u64 objectid,
617 ino_t parent_inum, struct stat *st,
618 const char *path_name, int out_fd)
620 int ret = -1;
621 ssize_t ret_read;
622 u64 bytes_read = 0;
623 struct btrfs_key key;
624 int blocks;
625 u32 sectorsize = root->sectorsize;
626 u64 first_block = 0;
627 u64 file_pos = 0;
628 u64 cur_bytes;
629 u64 total_bytes;
630 struct extent_buffer *eb = NULL;
631 int fd;
633 if (st->st_size == 0)
634 return 0;
636 fd = open(path_name, O_RDONLY);
637 if (fd == -1) {
638 fprintf(stderr, "%s open failed\n", path_name);
639 return ret;
642 blocks = st->st_size / sectorsize;
643 if (st->st_size % sectorsize)
644 blocks += 1;
646 if (st->st_size <= BTRFS_MAX_INLINE_DATA_SIZE(root)) {
647 char *buffer = malloc(st->st_size);
649 if (!buffer) {
650 ret = -ENOMEM;
651 goto end;
654 ret_read = pread64(fd, buffer, st->st_size, bytes_read);
655 if (ret_read == -1) {
656 fprintf(stderr, "%s read failed\n", path_name);
657 free(buffer);
658 goto end;
661 ret = btrfs_insert_inline_extent(trans, root, objectid, 0,
662 buffer, st->st_size);
663 free(buffer);
664 goto end;
667 /* round up our st_size to the FS blocksize */
668 total_bytes = (u64)blocks * sectorsize;
671 * do our IO in extent buffers so it can work
672 * against any raid type
674 eb = calloc(1, sizeof(*eb) + sectorsize);
675 if (!eb) {
676 ret = -ENOMEM;
677 goto end;
680 again:
683 * keep our extent size at 1MB max, this makes it easier to work inside
684 * the tiny block groups created during mkfs
686 cur_bytes = min(total_bytes, 1024ULL * 1024);
687 ret = btrfs_reserve_extent(trans, root, cur_bytes, 0, 0, (u64)-1,
688 &key, 1);
689 if (ret)
690 goto end;
692 first_block = key.objectid;
693 bytes_read = 0;
695 while (bytes_read < cur_bytes) {
697 memset(eb->data, 0, sectorsize);
699 ret_read = pread64(fd, eb->data, sectorsize, file_pos + bytes_read);
700 if (ret_read == -1) {
701 fprintf(stderr, "%s read failed\n", path_name);
702 goto end;
705 eb->start = first_block + bytes_read;
706 eb->len = sectorsize;
709 * we're doing the csum before we record the extent, but
710 * that's ok
712 ret = btrfs_csum_file_block(trans, root->fs_info->csum_root,
713 first_block + bytes_read + sectorsize,
714 first_block + bytes_read,
715 eb->data, sectorsize);
716 if (ret)
717 goto end;
719 ret = write_and_map_eb(trans, root, eb);
720 if (ret) {
721 fprintf(stderr, "output file write failed\n");
722 goto end;
725 bytes_read += sectorsize;
728 if (bytes_read) {
729 ret = btrfs_record_file_extent(trans, root, objectid, btrfs_inode,
730 file_pos, first_block, cur_bytes);
731 if (ret)
732 goto end;
736 file_pos += cur_bytes;
737 total_bytes -= cur_bytes;
739 if (total_bytes)
740 goto again;
742 end:
743 free(eb);
744 close(fd);
745 return ret;
748 static char *make_path(char *dir, char *name)
750 char *path;
752 path = malloc(strlen(dir) + strlen(name) + 2);
753 if (!path)
754 return NULL;
755 strcpy(path, dir);
756 if (dir[strlen(dir) - 1] != '/')
757 strcat(path, "/");
758 strcat(path, name);
759 return path;
762 static int traverse_directory(struct btrfs_trans_handle *trans,
763 struct btrfs_root *root, char *dir_name,
764 struct directory_name_entry *dir_head, int out_fd)
766 int ret = 0;
768 struct btrfs_inode_item cur_inode;
769 struct btrfs_inode_item *inode_item;
770 int count, i, dir_index_cnt;
771 struct direct **files;
772 struct stat st;
773 struct directory_name_entry *dir_entry, *parent_dir_entry;
774 struct direct *cur_file;
775 ino_t parent_inum, cur_inum;
776 ino_t highest_inum = 0;
777 char *parent_dir_name;
778 char real_path[PATH_MAX];
779 struct btrfs_path path;
780 struct extent_buffer *leaf;
781 struct btrfs_key root_dir_key;
782 u64 root_dir_inode_size = 0;
784 /* Add list for source directory */
785 dir_entry = malloc(sizeof(struct directory_name_entry));
786 if (!dir_entry)
787 return -ENOMEM;
788 dir_entry->dir_name = dir_name;
789 dir_entry->path = realpath(dir_name, real_path);
790 if (!dir_entry->path) {
791 fprintf(stderr, "get directory real path error\n");
792 ret = -1;
793 goto fail_no_dir;
796 parent_inum = highest_inum + BTRFS_FIRST_FREE_OBJECTID;
797 dir_entry->inum = parent_inum;
798 list_add_tail(&dir_entry->list, &dir_head->list);
800 btrfs_init_path(&path);
802 root_dir_key.objectid = btrfs_root_dirid(&root->root_item);
803 root_dir_key.offset = 0;
804 btrfs_set_key_type(&root_dir_key, BTRFS_INODE_ITEM_KEY);
805 ret = btrfs_lookup_inode(trans, root, &path, &root_dir_key, 1);
806 if (ret) {
807 fprintf(stderr, "root dir lookup error\n");
808 goto fail_no_dir;
811 leaf = path.nodes[0];
812 inode_item = btrfs_item_ptr(leaf, path.slots[0],
813 struct btrfs_inode_item);
815 root_dir_inode_size = calculate_dir_inode_size(dir_name);
816 btrfs_set_inode_size(leaf, inode_item, root_dir_inode_size);
817 btrfs_mark_buffer_dirty(leaf);
819 btrfs_release_path(&path);
821 do {
822 parent_dir_entry = list_entry(dir_head->list.next,
823 struct directory_name_entry,
824 list);
825 list_del(&parent_dir_entry->list);
827 parent_inum = parent_dir_entry->inum;
828 parent_dir_name = parent_dir_entry->dir_name;
829 if (chdir(parent_dir_entry->path)) {
830 fprintf(stderr, "chdir error for %s\n",
831 parent_dir_name);
832 ret = -1;
833 goto fail_no_files;
836 count = scandir(parent_dir_entry->path, &files,
837 directory_select, NULL);
838 if (count == -1)
840 fprintf(stderr, "scandir for %s failed: %s\n",
841 parent_dir_name, strerror (errno));
842 ret = -1;
843 goto fail;
846 for (i = 0; i < count; i++) {
847 cur_file = files[i];
849 if (lstat(cur_file->d_name, &st) == -1) {
850 fprintf(stderr, "lstat failed for file %s\n",
851 cur_file->d_name);
852 ret = -1;
853 goto fail;
856 cur_inum = st.st_ino;
857 ret = add_directory_items(trans, root,
858 cur_inum, parent_inum,
859 cur_file->d_name,
860 &st, &dir_index_cnt);
861 if (ret) {
862 fprintf(stderr, "add_directory_items failed\n");
863 goto fail;
866 ret = add_inode_items(trans, root, &st,
867 cur_file->d_name, cur_inum,
868 parent_inum, dir_index_cnt,
869 &cur_inode);
870 if (ret == -EEXIST) {
871 BUG_ON(st.st_nlink <= 1);
872 continue;
874 if (ret) {
875 fprintf(stderr, "add_inode_items failed\n");
876 goto fail;
879 ret = add_xattr_item(trans, root,
880 cur_inum, cur_file->d_name);
881 if (ret) {
882 fprintf(stderr, "add_xattr_item failed\n");
883 if(ret != -ENOTSUP)
884 goto fail;
887 if (S_ISDIR(st.st_mode)) {
888 dir_entry = malloc(sizeof(struct directory_name_entry));
889 if (!dir_entry) {
890 ret = -ENOMEM;
891 goto fail;
893 dir_entry->dir_name = cur_file->d_name;
894 dir_entry->path = make_path(parent_dir_entry->path,
895 cur_file->d_name);
896 dir_entry->inum = cur_inum;
897 list_add_tail(&dir_entry->list, &dir_head->list);
898 } else if (S_ISREG(st.st_mode)) {
899 ret = add_file_items(trans, root, &cur_inode,
900 cur_inum, parent_inum, &st,
901 cur_file->d_name, out_fd);
902 if (ret) {
903 fprintf(stderr, "add_file_items failed\n");
904 goto fail;
906 } else if (S_ISLNK(st.st_mode)) {
907 ret = add_symbolic_link(trans, root,
908 cur_inum, cur_file->d_name);
909 if (ret) {
910 fprintf(stderr, "add_symbolic_link failed\n");
911 goto fail;
916 free_namelist(files, count);
917 free(parent_dir_entry);
919 index_cnt = 2;
921 } while (!list_empty(&dir_head->list));
923 out:
924 return !!ret;
925 fail:
926 free_namelist(files, count);
927 fail_no_files:
928 free(parent_dir_entry);
929 goto out;
930 fail_no_dir:
931 free(dir_entry);
932 goto out;
935 static int open_target(char *output_name)
937 int output_fd;
938 output_fd = open(output_name, O_CREAT | O_RDWR,
939 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH);
941 return output_fd;
944 static int create_chunks(struct btrfs_trans_handle *trans,
945 struct btrfs_root *root, u64 num_of_meta_chunks,
946 u64 size_of_data,
947 struct mkfs_allocation *allocation)
949 u64 chunk_start;
950 u64 chunk_size;
951 u64 meta_type = BTRFS_BLOCK_GROUP_METADATA;
952 u64 data_type = BTRFS_BLOCK_GROUP_DATA;
953 u64 minimum_data_chunk_size = 8 * 1024 * 1024;
954 u64 i;
955 int ret;
957 for (i = 0; i < num_of_meta_chunks; i++) {
958 ret = btrfs_alloc_chunk(trans, root->fs_info->extent_root,
959 &chunk_start, &chunk_size, meta_type);
960 BUG_ON(ret);
961 ret = btrfs_make_block_group(trans, root->fs_info->extent_root, 0,
962 meta_type, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
963 chunk_start, chunk_size);
964 allocation->metadata += chunk_size;
965 BUG_ON(ret);
966 set_extent_dirty(&root->fs_info->free_space_cache,
967 chunk_start, chunk_start + chunk_size - 1, 0);
970 if (size_of_data < minimum_data_chunk_size)
971 size_of_data = minimum_data_chunk_size;
973 ret = btrfs_alloc_data_chunk(trans, root->fs_info->extent_root,
974 &chunk_start, size_of_data, data_type, 0);
975 BUG_ON(ret);
976 ret = btrfs_make_block_group(trans, root->fs_info->extent_root, 0,
977 data_type, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
978 chunk_start, size_of_data);
979 allocation->data += size_of_data;
980 BUG_ON(ret);
981 set_extent_dirty(&root->fs_info->free_space_cache,
982 chunk_start, chunk_start + size_of_data - 1, 0);
983 return ret;
986 static int make_image(char *source_dir, struct btrfs_root *root, int out_fd)
988 int ret;
989 struct btrfs_trans_handle *trans;
991 struct stat root_st;
993 struct directory_name_entry dir_head;
995 struct directory_name_entry *dir_entry = NULL;
997 ret = lstat(source_dir, &root_st);
998 if (ret) {
999 fprintf(stderr, "unable to lstat the %s\n", source_dir);
1000 goto out;
1003 INIT_LIST_HEAD(&dir_head.list);
1005 trans = btrfs_start_transaction(root, 1);
1006 ret = traverse_directory(trans, root, source_dir, &dir_head, out_fd);
1007 if (ret) {
1008 fprintf(stderr, "unable to traverse_directory\n");
1009 goto fail;
1011 btrfs_commit_transaction(trans, root);
1013 if (verbose)
1014 printf("Making image is completed.\n");
1015 return 0;
1016 fail:
1017 while (!list_empty(&dir_head.list)) {
1018 dir_entry = list_entry(dir_head.list.next,
1019 struct directory_name_entry, list);
1020 list_del(&dir_entry->list);
1021 free(dir_entry);
1023 out:
1024 fprintf(stderr, "Making image is aborted.\n");
1025 return -1;
1029 * This ignores symlinks with unreadable targets and subdirs that can't
1030 * be read. It's a best-effort to give a rough estimate of the size of
1031 * a subdir. It doesn't guarantee that prepopulating btrfs from this
1032 * tree won't still run out of space.
1034 static u64 global_total_size;
1035 static u64 fs_block_size;
1036 static int ftw_add_entry_size(const char *fpath, const struct stat *st,
1037 int type)
1039 if (type == FTW_F || type == FTW_D)
1040 global_total_size += round_up(st->st_size, fs_block_size);
1042 return 0;
1045 static u64 size_sourcedir(char *dir_name, u64 sectorsize,
1046 u64 *num_of_meta_chunks_ret, u64 *size_of_data_ret)
1048 u64 dir_size = 0;
1049 u64 total_size = 0;
1050 int ret;
1051 u64 default_chunk_size = 8 * 1024 * 1024; /* 8MB */
1052 u64 allocated_meta_size = 8 * 1024 * 1024; /* 8MB */
1053 u64 allocated_total_size = 20 * 1024 * 1024; /* 20MB */
1054 u64 num_of_meta_chunks = 0;
1055 u64 num_of_data_chunks = 0;
1056 u64 num_of_allocated_meta_chunks =
1057 allocated_meta_size / default_chunk_size;
1059 global_total_size = 0;
1060 fs_block_size = sectorsize;
1061 ret = ftw(dir_name, ftw_add_entry_size, 10);
1062 dir_size = global_total_size;
1063 if (ret < 0) {
1064 fprintf(stderr, "ftw subdir walk of '%s' failed: %s\n",
1065 dir_name, strerror(errno));
1066 exit(1);
1069 num_of_data_chunks = (dir_size + default_chunk_size - 1) /
1070 default_chunk_size;
1072 num_of_meta_chunks = (dir_size / 2) / default_chunk_size;
1073 if (((dir_size / 2) % default_chunk_size) != 0)
1074 num_of_meta_chunks++;
1075 if (num_of_meta_chunks <= num_of_allocated_meta_chunks)
1076 num_of_meta_chunks = 0;
1077 else
1078 num_of_meta_chunks -= num_of_allocated_meta_chunks;
1080 total_size = allocated_total_size +
1081 (num_of_data_chunks * default_chunk_size) +
1082 (num_of_meta_chunks * default_chunk_size);
1084 *num_of_meta_chunks_ret = num_of_meta_chunks;
1085 *size_of_data_ret = num_of_data_chunks * default_chunk_size;
1086 return total_size;
1089 static int zero_output_file(int out_fd, u64 size)
1091 int loop_num;
1092 u64 location = 0;
1093 char buf[4096];
1094 int ret = 0, i;
1095 ssize_t written;
1097 memset(buf, 0, 4096);
1098 loop_num = size / 4096;
1099 for (i = 0; i < loop_num; i++) {
1100 written = pwrite64(out_fd, buf, 4096, location);
1101 if (written != 4096)
1102 ret = -EIO;
1103 location += 4096;
1105 return ret;
1108 static int is_ssd(const char *file)
1110 blkid_probe probe;
1111 char wholedisk[PATH_MAX];
1112 char sysfs_path[PATH_MAX];
1113 dev_t devno;
1114 int fd;
1115 char rotational;
1116 int ret;
1118 probe = blkid_new_probe_from_filename(file);
1119 if (!probe)
1120 return 0;
1122 /* Device number of this disk (possibly a partition) */
1123 devno = blkid_probe_get_devno(probe);
1124 if (!devno) {
1125 blkid_free_probe(probe);
1126 return 0;
1129 /* Get whole disk name (not full path) for this devno */
1130 ret = blkid_devno_to_wholedisk(devno,
1131 wholedisk, sizeof(wholedisk), NULL);
1132 if (ret) {
1133 blkid_free_probe(probe);
1134 return 0;
1137 snprintf(sysfs_path, PATH_MAX, "/sys/block/%s/queue/rotational",
1138 wholedisk);
1140 blkid_free_probe(probe);
1142 fd = open(sysfs_path, O_RDONLY);
1143 if (fd < 0) {
1144 return 0;
1147 if (read(fd, &rotational, sizeof(char)) < sizeof(char)) {
1148 close(fd);
1149 return 0;
1151 close(fd);
1153 return !atoi((const char *)&rotational);
1156 static int _cmp_device_by_id(void *priv, struct list_head *a,
1157 struct list_head *b)
1159 return list_entry(a, struct btrfs_device, dev_list)->devid -
1160 list_entry(b, struct btrfs_device, dev_list)->devid;
1163 static void list_all_devices(struct btrfs_root *root)
1165 struct btrfs_fs_devices *fs_devices;
1166 struct btrfs_device *device;
1167 int number_of_devices = 0;
1168 u64 total_block_count = 0;
1170 fs_devices = root->fs_info->fs_devices;
1172 list_for_each_entry(device, &fs_devices->devices, dev_list)
1173 number_of_devices++;
1175 list_sort(NULL, &fs_devices->devices, _cmp_device_by_id);
1177 printf("Number of devices: %d\n", number_of_devices);
1178 /* printf("Total devices size: %10s\n", */
1179 /* pretty_size(total_block_count)); */
1180 printf("Devices:\n");
1181 printf(" ID SIZE PATH\n");
1182 list_for_each_entry(device, &fs_devices->devices, dev_list) {
1183 printf(" %3llu %10s %s\n",
1184 device->devid,
1185 pretty_size(device->total_bytes),
1186 device->name);
1187 total_block_count += device->total_bytes;
1190 printf("\n");
1193 static int is_temp_block_group(struct extent_buffer *node,
1194 struct btrfs_block_group_item *bgi,
1195 u64 data_profile, u64 meta_profile,
1196 u64 sys_profile)
1198 u64 flag = btrfs_disk_block_group_flags(node, bgi);
1199 u64 flag_type = flag & BTRFS_BLOCK_GROUP_TYPE_MASK;
1200 u64 flag_profile = flag & BTRFS_BLOCK_GROUP_PROFILE_MASK;
1201 u64 used = btrfs_disk_block_group_used(node, bgi);
1204 * Chunks meets all the following conditions is a temp chunk
1205 * 1) Empty chunk
1206 * Temp chunk is always empty.
1208 * 2) profile mismatch with mkfs profile.
1209 * Temp chunk is always in SINGLE
1211 * 3) Size differs with mkfs_alloc
1212 * Special case for SINGLE/SINGLE btrfs.
1213 * In that case, temp data chunk and real data chunk are always empty.
1214 * So we need to use mkfs_alloc to be sure which chunk is the newly
1215 * allocated.
1217 * Normally, new chunk size is equal to mkfs one (One chunk)
1218 * If it has multiple chunks, we just refuse to delete any one.
1219 * As they are all single, so no real problem will happen.
1220 * So only use condition 1) and 2) to judge them.
1222 if (used != 0)
1223 return 0;
1224 switch (flag_type) {
1225 case BTRFS_BLOCK_GROUP_DATA:
1226 case BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA:
1227 data_profile &= BTRFS_BLOCK_GROUP_PROFILE_MASK;
1228 if (flag_profile != data_profile)
1229 return 1;
1230 break;
1231 case BTRFS_BLOCK_GROUP_METADATA:
1232 meta_profile &= BTRFS_BLOCK_GROUP_PROFILE_MASK;
1233 if (flag_profile != meta_profile)
1234 return 1;
1235 break;
1236 case BTRFS_BLOCK_GROUP_SYSTEM:
1237 sys_profile &= BTRFS_BLOCK_GROUP_PROFILE_MASK;
1238 if (flag_profile != sys_profile)
1239 return 1;
1240 break;
1242 return 0;
1245 /* Note: if current is a block group, it will skip it anyway */
1246 static int next_block_group(struct btrfs_root *root,
1247 struct btrfs_path *path)
1249 struct btrfs_key key;
1250 int ret = 0;
1252 while (1) {
1253 ret = btrfs_next_item(root, path);
1254 if (ret)
1255 goto out;
1257 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1258 if (key.type == BTRFS_BLOCK_GROUP_ITEM_KEY)
1259 goto out;
1261 out:
1262 return ret;
1265 /* This function will cleanup */
1266 static int cleanup_temp_chunks(struct btrfs_fs_info *fs_info,
1267 struct mkfs_allocation *alloc,
1268 u64 data_profile, u64 meta_profile,
1269 u64 sys_profile)
1271 struct btrfs_trans_handle *trans = NULL;
1272 struct btrfs_block_group_item *bgi;
1273 struct btrfs_root *root = fs_info->extent_root;
1274 struct btrfs_key key;
1275 struct btrfs_key found_key;
1276 struct btrfs_path *path;
1277 int ret = 0;
1279 path = btrfs_alloc_path();
1280 if (!path) {
1281 ret = -ENOMEM;
1282 goto out;
1285 trans = btrfs_start_transaction(root, 1);
1287 key.objectid = 0;
1288 key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
1289 key.offset = 0;
1291 while (1) {
1293 * as the rest of the loop may modify the tree, we need to
1294 * start a new search each time.
1296 ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
1297 if (ret < 0)
1298 goto out;
1300 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1301 path->slots[0]);
1302 if (found_key.objectid < key.objectid)
1303 goto out;
1304 if (found_key.type != BTRFS_BLOCK_GROUP_ITEM_KEY) {
1305 ret = next_block_group(root, path);
1306 if (ret < 0)
1307 goto out;
1308 if (ret > 0) {
1309 ret = 0;
1310 goto out;
1312 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1313 path->slots[0]);
1316 bgi = btrfs_item_ptr(path->nodes[0], path->slots[0],
1317 struct btrfs_block_group_item);
1318 if (is_temp_block_group(path->nodes[0], bgi,
1319 data_profile, meta_profile,
1320 sys_profile)) {
1321 u64 flags = btrfs_disk_block_group_flags(path->nodes[0],
1322 bgi);
1324 ret = btrfs_free_block_group(trans, fs_info,
1325 found_key.objectid, found_key.offset);
1326 if (ret < 0)
1327 goto out;
1329 if ((flags & BTRFS_BLOCK_GROUP_TYPE_MASK) ==
1330 BTRFS_BLOCK_GROUP_DATA)
1331 alloc->data -= found_key.offset;
1332 else if ((flags & BTRFS_BLOCK_GROUP_TYPE_MASK) ==
1333 BTRFS_BLOCK_GROUP_METADATA)
1334 alloc->metadata -= found_key.offset;
1335 else if ((flags & BTRFS_BLOCK_GROUP_TYPE_MASK) ==
1336 BTRFS_BLOCK_GROUP_SYSTEM)
1337 alloc->system -= found_key.offset;
1338 else if ((flags & BTRFS_BLOCK_GROUP_TYPE_MASK) ==
1339 (BTRFS_BLOCK_GROUP_METADATA |
1340 BTRFS_BLOCK_GROUP_DATA))
1341 alloc->mixed -= found_key.offset;
1343 btrfs_release_path(path);
1344 key.objectid = found_key.objectid + found_key.offset;
1346 out:
1347 if (trans)
1348 btrfs_commit_transaction(trans, root);
1349 btrfs_free_path(path);
1350 return ret;
1353 int main(int argc, char **argv)
1355 char *file;
1356 struct btrfs_root *root;
1357 struct btrfs_trans_handle *trans;
1358 char *label = NULL;
1359 u64 block_count = 0;
1360 u64 dev_block_count = 0;
1361 u64 blocks[7];
1362 u64 alloc_start = 0;
1363 u64 metadata_profile = 0;
1364 u64 data_profile = 0;
1365 u32 nodesize = max_t(u32, sysconf(_SC_PAGESIZE),
1366 BTRFS_MKFS_DEFAULT_NODE_SIZE);
1367 u32 sectorsize = 4096;
1368 u32 stripesize = 4096;
1369 int zero_end = 1;
1370 int fd;
1371 int ret;
1372 int i;
1373 int mixed = 0;
1374 int nodesize_forced = 0;
1375 int data_profile_opt = 0;
1376 int metadata_profile_opt = 0;
1377 int discard = 1;
1378 int ssd = 0;
1379 int force_overwrite = 0;
1380 char *source_dir = NULL;
1381 int source_dir_set = 0;
1382 u64 num_of_meta_chunks = 0;
1383 u64 size_of_data = 0;
1384 u64 source_dir_size = 0;
1385 int dev_cnt = 0;
1386 int saved_optind;
1387 char fs_uuid[BTRFS_UUID_UNPARSED_SIZE] = { 0 };
1388 u64 features = BTRFS_MKFS_DEFAULT_FEATURES;
1389 struct mkfs_allocation allocation = { 0 };
1390 struct btrfs_mkfs_config mkfs_cfg;
1392 while(1) {
1393 int c;
1394 static const struct option long_options[] = {
1395 { "alloc-start", required_argument, NULL, 'A'},
1396 { "byte-count", required_argument, NULL, 'b' },
1397 { "force", no_argument, NULL, 'f' },
1398 { "leafsize", required_argument, NULL, 'l' },
1399 { "label", required_argument, NULL, 'L'},
1400 { "metadata", required_argument, NULL, 'm' },
1401 { "mixed", no_argument, NULL, 'M' },
1402 { "nodesize", required_argument, NULL, 'n' },
1403 { "sectorsize", required_argument, NULL, 's' },
1404 { "data", required_argument, NULL, 'd' },
1405 { "version", no_argument, NULL, 'V' },
1406 { "rootdir", required_argument, NULL, 'r' },
1407 { "nodiscard", no_argument, NULL, 'K' },
1408 { "features", required_argument, NULL, 'O' },
1409 { "uuid", required_argument, NULL, 'U' },
1410 { "quiet", 0, NULL, 'q' },
1411 { "help", no_argument, NULL, GETOPT_VAL_HELP },
1412 { NULL, 0, NULL, 0}
1415 c = getopt_long(argc, argv, "A:b:fl:n:s:m:d:L:O:r:U:VMKq",
1416 long_options, NULL);
1417 if (c < 0)
1418 break;
1419 switch(c) {
1420 case 'A':
1421 alloc_start = parse_size(optarg);
1422 break;
1423 case 'f':
1424 force_overwrite = 1;
1425 break;
1426 case 'd':
1427 data_profile = parse_profile(optarg);
1428 data_profile_opt = 1;
1429 break;
1430 case 'l':
1431 fprintf(stderr,
1432 "WARNING: --leafsize is deprecated, use --nodesize\n");
1433 case 'n':
1434 nodesize = parse_size(optarg);
1435 nodesize_forced = 1;
1436 break;
1437 case 'L':
1438 label = parse_label(optarg);
1439 break;
1440 case 'm':
1441 metadata_profile = parse_profile(optarg);
1442 metadata_profile_opt = 1;
1443 break;
1444 case 'M':
1445 mixed = 1;
1446 break;
1447 case 'O': {
1448 char *orig = strdup(optarg);
1449 char *tmp = orig;
1451 tmp = btrfs_parse_fs_features(tmp, &features);
1452 if (tmp) {
1453 fprintf(stderr,
1454 "Unrecognized filesystem feature '%s'\n",
1455 tmp);
1456 free(orig);
1457 exit(1);
1459 free(orig);
1460 if (features & BTRFS_FEATURE_LIST_ALL) {
1461 btrfs_list_all_fs_features(0);
1462 exit(0);
1464 break;
1466 case 's':
1467 sectorsize = parse_size(optarg);
1468 break;
1469 case 'b':
1470 block_count = parse_size(optarg);
1471 zero_end = 0;
1472 break;
1473 case 'V':
1474 print_version();
1475 break;
1476 case 'r':
1477 source_dir = optarg;
1478 source_dir_set = 1;
1479 break;
1480 case 'U':
1481 strncpy(fs_uuid, optarg,
1482 BTRFS_UUID_UNPARSED_SIZE - 1);
1483 break;
1484 case 'K':
1485 discard = 0;
1486 break;
1487 case 'q':
1488 verbose = 0;
1489 break;
1490 case GETOPT_VAL_HELP:
1491 default:
1492 print_usage(c != GETOPT_VAL_HELP);
1496 if (verbose) {
1497 printf("%s\n", PACKAGE_STRING);
1498 printf("See %s for more information.\n\n", PACKAGE_URL);
1501 sectorsize = max(sectorsize, (u32)sysconf(_SC_PAGESIZE));
1502 stripesize = sectorsize;
1503 saved_optind = optind;
1504 dev_cnt = argc - optind;
1505 if (dev_cnt == 0)
1506 print_usage(1);
1508 if (source_dir_set && dev_cnt > 1) {
1509 fprintf(stderr,
1510 "The -r option is limited to a single device\n");
1511 exit(1);
1514 if (*fs_uuid) {
1515 uuid_t dummy_uuid;
1517 if (uuid_parse(fs_uuid, dummy_uuid) != 0) {
1518 fprintf(stderr, "could not parse UUID: %s\n", fs_uuid);
1519 exit(1);
1521 if (!test_uuid_unique(fs_uuid)) {
1522 fprintf(stderr, "non-unique UUID: %s\n", fs_uuid);
1523 exit(1);
1527 while (dev_cnt-- > 0) {
1528 file = argv[optind++];
1529 if (is_block_device(file) == 1)
1530 if (test_dev_for_mkfs(file, force_overwrite))
1531 exit(1);
1534 optind = saved_optind;
1535 dev_cnt = argc - optind;
1537 file = argv[optind++];
1538 ssd = is_ssd(file);
1541 * Set default profiles according to number of added devices.
1542 * For mixed groups defaults are single/single.
1544 if (!mixed) {
1545 if (!metadata_profile_opt) {
1546 if (dev_cnt == 1 && ssd && verbose)
1547 printf("Detected a SSD, turning off metadata "
1548 "duplication. Mkfs with -m dup if you want to "
1549 "force metadata duplication.\n");
1551 metadata_profile = (dev_cnt > 1) ?
1552 BTRFS_BLOCK_GROUP_RAID1 : (ssd) ?
1553 0: BTRFS_BLOCK_GROUP_DUP;
1555 if (!data_profile_opt) {
1556 data_profile = (dev_cnt > 1) ?
1557 BTRFS_BLOCK_GROUP_RAID0 : 0; /* raid0 or single */
1559 } else {
1560 u32 best_nodesize = max_t(u32, sysconf(_SC_PAGESIZE), sectorsize);
1562 if (metadata_profile_opt || data_profile_opt) {
1563 if (metadata_profile != data_profile) {
1564 fprintf(stderr,
1565 "ERROR: With mixed block groups data and metadata profiles must be the same\n");
1566 exit(1);
1570 if (!nodesize_forced)
1571 nodesize = best_nodesize;
1575 * FS features that can be set by other means than -O
1576 * just set the bit here
1578 if (mixed)
1579 features |= BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS;
1581 if ((data_profile | metadata_profile) &
1582 (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6)) {
1583 features |= BTRFS_FEATURE_INCOMPAT_RAID56;
1586 if (btrfs_check_nodesize(nodesize, sectorsize,
1587 features))
1588 exit(1);
1590 /* Check device/block_count after the nodesize is determined */
1591 if (block_count && block_count < btrfs_min_dev_size(nodesize)) {
1592 fprintf(stderr,
1593 "Size '%llu' is too small to make a usable filesystem\n",
1594 block_count);
1595 fprintf(stderr,
1596 "Minimum size for btrfs filesystem is %llu\n",
1597 btrfs_min_dev_size(nodesize));
1598 exit(1);
1600 for (i = saved_optind; i < saved_optind + dev_cnt; i++) {
1601 char *path;
1603 path = argv[i];
1604 ret = test_minimum_size(path, nodesize);
1605 if (ret < 0) {
1606 fprintf(stderr, "Failed to check size for '%s': %s\n",
1607 path, strerror(-ret));
1608 exit (1);
1610 if (ret > 0) {
1611 fprintf(stderr,
1612 "'%s' is too small to make a usable filesystem\n",
1613 path);
1614 fprintf(stderr,
1615 "Minimum size for each btrfs device is %llu.\n",
1616 btrfs_min_dev_size(nodesize));
1617 exit(1);
1620 ret = test_num_disk_vs_raid(metadata_profile, data_profile,
1621 dev_cnt, mixed, ssd);
1622 if (ret)
1623 exit(1);
1625 dev_cnt--;
1627 if (!source_dir_set) {
1629 * open without O_EXCL so that the problem should not
1630 * occur by the following processing.
1631 * (btrfs_register_one_device() fails if O_EXCL is on)
1633 fd = open(file, O_RDWR);
1634 if (fd < 0) {
1635 fprintf(stderr, "unable to open %s: %s\n", file,
1636 strerror(errno));
1637 exit(1);
1639 ret = btrfs_prepare_device(fd, file, &dev_block_count,
1640 block_count,
1641 (zero_end ? PREP_DEVICE_ZERO_END : 0) |
1642 (discard ? PREP_DEVICE_DISCARD : 0) |
1643 (verbose ? PREP_DEVICE_VERBOSE : 0));
1644 if (ret) {
1645 close(fd);
1646 exit(1);
1648 if (block_count && block_count > dev_block_count) {
1649 fprintf(stderr, "%s is smaller than requested size\n", file);
1650 exit(1);
1652 } else {
1653 fd = open_target(file);
1654 if (fd < 0) {
1655 fprintf(stderr, "unable to open the %s\n", file);
1656 exit(1);
1659 source_dir_size = size_sourcedir(source_dir, sectorsize,
1660 &num_of_meta_chunks, &size_of_data);
1661 if(block_count < source_dir_size)
1662 block_count = source_dir_size;
1663 ret = zero_output_file(fd, block_count);
1664 if (ret) {
1665 fprintf(stderr, "unable to zero the output file\n");
1666 exit(1);
1668 /* our "device" is the new image file */
1669 dev_block_count = block_count;
1672 /* To create the first block group and chunk 0 in make_btrfs */
1673 if (dev_block_count < BTRFS_MKFS_SYSTEM_GROUP_SIZE) {
1674 fprintf(stderr, "device is too small to make filesystem\n");
1675 exit(1);
1678 blocks[0] = BTRFS_SUPER_INFO_OFFSET;
1679 for (i = 1; i < 7; i++) {
1680 blocks[i] = BTRFS_SUPER_INFO_OFFSET + 1024 * 1024 +
1681 nodesize * i;
1684 if (group_profile_max_safe_loss(metadata_profile) <
1685 group_profile_max_safe_loss(data_profile)){
1686 fprintf(stderr,
1687 "WARNING: metadata has lower redundancy than data!\n\n");
1690 mkfs_cfg.label = label;
1691 mkfs_cfg.fs_uuid = fs_uuid;
1692 memcpy(mkfs_cfg.blocks, blocks, sizeof(blocks));
1693 mkfs_cfg.num_bytes = dev_block_count;
1694 mkfs_cfg.nodesize = nodesize;
1695 mkfs_cfg.sectorsize = sectorsize;
1696 mkfs_cfg.stripesize = stripesize;
1697 mkfs_cfg.features = features;
1699 ret = make_btrfs(fd, &mkfs_cfg, NULL);
1700 if (ret) {
1701 fprintf(stderr, "error during mkfs: %s\n", strerror(-ret));
1702 exit(1);
1705 root = open_ctree(file, 0, OPEN_CTREE_WRITES);
1706 if (!root) {
1707 fprintf(stderr, "Open ctree failed\n");
1708 close(fd);
1709 exit(1);
1711 root->fs_info->alloc_start = alloc_start;
1713 ret = create_metadata_block_groups(root, mixed, &allocation);
1714 if (ret) {
1715 fprintf(stderr, "failed to create default block groups\n");
1716 exit(1);
1719 trans = btrfs_start_transaction(root, 1);
1720 if (!trans) {
1721 fprintf(stderr, "failed to start transaction\n");
1722 exit(1);
1725 ret = create_data_block_groups(trans, root, mixed, &allocation);
1726 if (ret) {
1727 fprintf(stderr, "failed to create default data block groups\n");
1728 exit(1);
1731 ret = make_root_dir(trans, root, &allocation);
1732 if (ret) {
1733 fprintf(stderr, "failed to setup the root directory\n");
1734 exit(1);
1737 btrfs_commit_transaction(trans, root);
1739 trans = btrfs_start_transaction(root, 1);
1740 if (!trans) {
1741 fprintf(stderr, "failed to start transaction\n");
1742 exit(1);
1745 if (is_block_device(file) == 1)
1746 btrfs_register_one_device(file);
1748 if (dev_cnt == 0)
1749 goto raid_groups;
1751 while (dev_cnt-- > 0) {
1752 file = argv[optind++];
1755 * open without O_EXCL so that the problem should not
1756 * occur by the following processing.
1757 * (btrfs_register_one_device() fails if O_EXCL is on)
1759 fd = open(file, O_RDWR);
1760 if (fd < 0) {
1761 fprintf(stderr, "unable to open %s: %s\n", file,
1762 strerror(errno));
1763 exit(1);
1765 ret = btrfs_device_already_in_root(root, fd,
1766 BTRFS_SUPER_INFO_OFFSET);
1767 if (ret) {
1768 fprintf(stderr, "skipping duplicate device %s in FS\n",
1769 file);
1770 close(fd);
1771 continue;
1773 ret = btrfs_prepare_device(fd, file, &dev_block_count,
1774 block_count,
1775 (verbose ? PREP_DEVICE_VERBOSE : 0) |
1776 (zero_end ? PREP_DEVICE_ZERO_END : 0) |
1777 (discard ? PREP_DEVICE_DISCARD : 0));
1778 if (ret) {
1779 close(fd);
1780 exit(1);
1783 ret = btrfs_add_to_fsid(trans, root, fd, file, dev_block_count,
1784 sectorsize, sectorsize, sectorsize);
1785 BUG_ON(ret);
1786 if (verbose >= 2) {
1787 struct btrfs_device *device;
1789 device = container_of(root->fs_info->fs_devices->devices.next,
1790 struct btrfs_device, dev_list);
1791 printf("adding device %s id %llu\n", file,
1792 (unsigned long long)device->devid);
1795 if (is_block_device(file) == 1)
1796 btrfs_register_one_device(file);
1799 raid_groups:
1800 if (!source_dir_set) {
1801 ret = create_raid_groups(trans, root, data_profile,
1802 metadata_profile, mixed, &allocation);
1803 BUG_ON(ret);
1806 ret = create_data_reloc_tree(trans, root);
1807 BUG_ON(ret);
1809 btrfs_commit_transaction(trans, root);
1811 if (source_dir_set) {
1812 trans = btrfs_start_transaction(root, 1);
1813 ret = create_chunks(trans, root,
1814 num_of_meta_chunks, size_of_data,
1815 &allocation);
1816 BUG_ON(ret);
1817 btrfs_commit_transaction(trans, root);
1819 ret = make_image(source_dir, root, fd);
1820 BUG_ON(ret);
1822 ret = cleanup_temp_chunks(root->fs_info, &allocation, data_profile,
1823 metadata_profile, metadata_profile);
1824 if (ret < 0) {
1825 fprintf(stderr, "Failed to cleanup temporary chunks\n");
1826 goto out;
1829 if (verbose) {
1830 char features_buf[64];
1832 printf("Label: %s\n", label);
1833 printf("UUID: %s\n", fs_uuid);
1834 printf("Node size: %u\n", nodesize);
1835 printf("Sector size: %u\n", sectorsize);
1836 printf("Filesystem size: %s\n",
1837 pretty_size(btrfs_super_total_bytes(root->fs_info->super_copy)));
1838 printf("Block group profiles:\n");
1839 if (allocation.data)
1840 printf(" Data: %-8s %16s\n",
1841 btrfs_group_profile_str(data_profile),
1842 pretty_size(allocation.data));
1843 if (allocation.metadata)
1844 printf(" Metadata: %-8s %16s\n",
1845 btrfs_group_profile_str(metadata_profile),
1846 pretty_size(allocation.metadata));
1847 if (allocation.mixed)
1848 printf(" Data+Metadata: %-8s %16s\n",
1849 btrfs_group_profile_str(data_profile),
1850 pretty_size(allocation.mixed));
1851 printf(" System: %-8s %16s\n",
1852 btrfs_group_profile_str(metadata_profile),
1853 pretty_size(allocation.system));
1854 printf("SSD detected: %s\n", ssd ? "yes" : "no");
1855 btrfs_parse_features_to_string(features_buf, features);
1856 printf("Incompat features: %s", features_buf);
1857 printf("\n");
1859 list_all_devices(root);
1862 out:
1863 ret = close_ctree(root);
1864 BUG_ON(ret);
1865 btrfs_close_all_devices();
1866 free(label);
1867 return 0;