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"
21 #include <sys/ioctl.h>
22 #include <sys/mount.h>
26 #include <sys/types.h>
32 #include <uuid/uuid.h>
34 #include <sys/xattr.h>
36 #include <linux/limits.h>
37 #include <blkid/blkid.h>
42 #include "transaction.h"
45 static u64 index_cnt = 2;
46 static int verbose = 1;
48 struct directory_name_entry {
52 struct list_head list;
55 struct mkfs_allocation {
62 static int create_metadata_block_groups(struct btrfs_root *root, int mixed,
63 struct mkfs_allocation *allocation)
65 struct btrfs_trans_handle *trans;
71 trans = btrfs_start_transaction(root, 1);
72 bytes_used = btrfs_super_bytes_used(root->fs_info->super_copy);
74 root->fs_info->system_allocs = 1;
75 ret = btrfs_make_block_group(trans, root, bytes_used,
76 BTRFS_BLOCK_GROUP_SYSTEM,
77 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
78 0, BTRFS_MKFS_SYSTEM_GROUP_SIZE);
79 allocation->system += BTRFS_MKFS_SYSTEM_GROUP_SIZE;
83 ret = btrfs_alloc_chunk(trans, root->fs_info->extent_root,
84 &chunk_start, &chunk_size,
85 BTRFS_BLOCK_GROUP_METADATA |
86 BTRFS_BLOCK_GROUP_DATA);
89 "no space to alloc data/metadata chunk\n");
93 ret = btrfs_make_block_group(trans, root, 0,
94 BTRFS_BLOCK_GROUP_METADATA |
95 BTRFS_BLOCK_GROUP_DATA,
96 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
97 chunk_start, chunk_size);
99 allocation->mixed += chunk_size;
101 ret = btrfs_alloc_chunk(trans, root->fs_info->extent_root,
102 &chunk_start, &chunk_size,
103 BTRFS_BLOCK_GROUP_METADATA);
104 if (ret == -ENOSPC) {
105 fprintf(stderr, "no space to alloc metadata chunk\n");
109 ret = btrfs_make_block_group(trans, root, 0,
110 BTRFS_BLOCK_GROUP_METADATA,
111 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
112 chunk_start, chunk_size);
113 allocation->metadata += chunk_size;
117 root->fs_info->system_allocs = 0;
118 btrfs_commit_transaction(trans, root);
124 static int make_root_dir(struct btrfs_trans_handle *trans, struct btrfs_root *root,
125 int mixed, struct mkfs_allocation *allocation)
132 ret = btrfs_alloc_chunk(trans, root->fs_info->extent_root,
133 &chunk_start, &chunk_size,
134 BTRFS_BLOCK_GROUP_DATA);
135 if (ret == -ENOSPC) {
136 fprintf(stderr, "no space to alloc data chunk\n");
140 ret = btrfs_make_block_group(trans, root, 0,
141 BTRFS_BLOCK_GROUP_DATA,
142 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
143 chunk_start, chunk_size);
144 allocation->data += chunk_size;
148 ret = btrfs_make_root_dir(trans, root->fs_info->tree_root,
149 BTRFS_ROOT_TREE_DIR_OBJECTID);
152 ret = btrfs_make_root_dir(trans, root, BTRFS_FIRST_FREE_OBJECTID);
155 memcpy(&location, &root->fs_info->fs_root->root_key, sizeof(location));
156 location.offset = (u64)-1;
157 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
159 btrfs_super_root_dir(root->fs_info->super_copy),
160 &location, BTRFS_FT_DIR, 0);
164 ret = btrfs_insert_inode_ref(trans, root->fs_info->tree_root,
165 "default", 7, location.objectid,
166 BTRFS_ROOT_TREE_DIR_OBJECTID, 0);
174 static void __recow_root(struct btrfs_trans_handle *trans,
175 struct btrfs_root *root)
178 struct extent_buffer *tmp;
180 if (trans->transid != btrfs_root_generation(&root->root_item)) {
181 extent_buffer_get(root->node);
182 ret = __btrfs_cow_block(trans, root, root->node,
183 NULL, 0, &tmp, 0, 0);
185 free_extent_buffer(tmp);
189 static void recow_roots(struct btrfs_trans_handle *trans,
190 struct btrfs_root *root)
192 struct btrfs_fs_info *info = root->fs_info;
194 __recow_root(trans, info->fs_root);
195 __recow_root(trans, info->tree_root);
196 __recow_root(trans, info->extent_root);
197 __recow_root(trans, info->chunk_root);
198 __recow_root(trans, info->dev_root);
199 __recow_root(trans, info->csum_root);
202 static int create_one_raid_group(struct btrfs_trans_handle *trans,
203 struct btrfs_root *root, u64 type,
204 struct mkfs_allocation *allocation)
211 ret = btrfs_alloc_chunk(trans, root->fs_info->extent_root,
212 &chunk_start, &chunk_size, type);
213 if (ret == -ENOSPC) {
214 fprintf(stderr, "not enough free space\n");
218 ret = btrfs_make_block_group(trans, root->fs_info->extent_root, 0,
219 type, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
220 chunk_start, chunk_size);
221 if ((type & BTRFS_BLOCK_GROUP_TYPE_MASK) == BTRFS_BLOCK_GROUP_DATA)
222 allocation->data += chunk_size;
223 else if ((type & BTRFS_BLOCK_GROUP_TYPE_MASK) == BTRFS_BLOCK_GROUP_METADATA)
224 allocation->metadata += chunk_size;
225 else if ((type & BTRFS_BLOCK_GROUP_TYPE_MASK) == BTRFS_BLOCK_GROUP_SYSTEM)
226 allocation->system += chunk_size;
227 else if ((type & BTRFS_BLOCK_GROUP_TYPE_MASK) ==
228 (BTRFS_BLOCK_GROUP_METADATA|BTRFS_BLOCK_GROUP_DATA))
229 allocation->mixed += chunk_size;
237 static int create_raid_groups(struct btrfs_trans_handle *trans,
238 struct btrfs_root *root, u64 data_profile,
239 int data_profile_opt, u64 metadata_profile,
241 struct mkfs_allocation *allocation)
243 u64 num_devices = btrfs_super_num_devices(root->fs_info->super_copy);
246 if (metadata_profile) {
247 u64 meta_flags = BTRFS_BLOCK_GROUP_METADATA;
249 ret = create_one_raid_group(trans, root,
250 BTRFS_BLOCK_GROUP_SYSTEM |
251 metadata_profile, allocation);
255 meta_flags |= BTRFS_BLOCK_GROUP_DATA;
257 ret = create_one_raid_group(trans, root, meta_flags |
258 metadata_profile, allocation);
262 if (!mixed && num_devices > 1 && data_profile) {
263 ret = create_one_raid_group(trans, root,
264 BTRFS_BLOCK_GROUP_DATA |
265 data_profile, allocation);
268 recow_roots(trans, root);
273 static int create_data_reloc_tree(struct btrfs_trans_handle *trans,
274 struct btrfs_root *root)
276 struct btrfs_key location;
277 struct btrfs_root_item root_item;
278 struct extent_buffer *tmp;
279 u64 objectid = BTRFS_DATA_RELOC_TREE_OBJECTID;
282 ret = btrfs_copy_root(trans, root, root->node, &tmp, objectid);
285 memcpy(&root_item, &root->root_item, sizeof(root_item));
286 btrfs_set_root_bytenr(&root_item, tmp->start);
287 btrfs_set_root_level(&root_item, btrfs_header_level(tmp));
288 btrfs_set_root_generation(&root_item, trans->transid);
289 free_extent_buffer(tmp);
291 location.objectid = objectid;
292 location.type = BTRFS_ROOT_ITEM_KEY;
294 ret = btrfs_insert_root(trans, root->fs_info->tree_root,
295 &location, &root_item);
300 static void print_usage(int ret)
302 fprintf(stderr, "usage: mkfs.btrfs [options] dev [ dev ... ]\n");
303 fprintf(stderr, "options:\n");
304 fprintf(stderr, "\t-A|--alloc-start START the offset to start the FS\n");
305 fprintf(stderr, "\t-b|--byte-count SIZE total number of bytes in the FS\n");
306 fprintf(stderr, "\t-d|--data PROFILE data profile, raid0, raid1, raid5, raid6, raid10, dup or single\n");
307 fprintf(stderr, "\t-f|--force force overwrite of existing filesystem\n");
308 fprintf(stderr, "\t-l|--leafsize SIZE deprecated, alias for nodesize\n");
309 fprintf(stderr, "\t-L|--label LABEL set a label\n");
310 fprintf(stderr, "\t-m|--metadata PROFILE metadata profile, values like data profile\n");
311 fprintf(stderr, "\t-M|--mixed mix metadata and data together\n");
312 fprintf(stderr, "\t-n|--nodesize SIZE size of btree nodes\n");
313 fprintf(stderr, "\t-s|--sectorsize SIZE min block allocation (may not mountable by current kernel)\n");
314 fprintf(stderr, "\t-r|--rootdir DIR the source directory\n");
315 fprintf(stderr, "\t-K|--nodiscard do not perform whole device TRIM\n");
316 fprintf(stderr, "\t-O|--features LIST comma separated list of filesystem features, use '-O list-all' to list features\n");
317 fprintf(stderr, "\t-U|--uuid UUID specify the filesystem UUID\n");
318 fprintf(stderr, "\t-q|--quiet no messages except errors\n");
319 fprintf(stderr, "\t-V|--version print the mkfs.btrfs version and exit\n");
320 fprintf(stderr, "%s\n", PACKAGE_STRING);
324 static void print_version(void) __attribute__((noreturn));
325 static void print_version(void)
327 fprintf(stderr, "mkfs.btrfs, part of %s\n", PACKAGE_STRING);
331 static u64 parse_profile(char *s)
333 if (strcmp(s, "raid0") == 0) {
334 return BTRFS_BLOCK_GROUP_RAID0;
335 } else if (strcasecmp(s, "raid1") == 0) {
336 return BTRFS_BLOCK_GROUP_RAID1;
337 } else if (strcasecmp(s, "raid5") == 0) {
338 return BTRFS_BLOCK_GROUP_RAID5;
339 } else if (strcasecmp(s, "raid6") == 0) {
340 return BTRFS_BLOCK_GROUP_RAID6;
341 } else if (strcasecmp(s, "raid10") == 0) {
342 return BTRFS_BLOCK_GROUP_RAID10;
343 } else if (strcasecmp(s, "dup") == 0) {
344 return BTRFS_BLOCK_GROUP_DUP;
345 } else if (strcasecmp(s, "single") == 0) {
348 fprintf(stderr, "Unknown profile %s\n", s);
354 static char *parse_label(char *input)
356 int len = strlen(input);
358 if (len >= BTRFS_LABEL_SIZE) {
359 fprintf(stderr, "Label %s is too long (max %d)\n", input,
360 BTRFS_LABEL_SIZE - 1);
363 return strdup(input);
366 static int add_directory_items(struct btrfs_trans_handle *trans,
367 struct btrfs_root *root, u64 objectid,
368 ino_t parent_inum, const char *name,
369 struct stat *st, int *dir_index_cnt)
373 struct btrfs_key location;
376 name_len = strlen(name);
378 location.objectid = objectid;
380 btrfs_set_key_type(&location, BTRFS_INODE_ITEM_KEY);
382 if (S_ISDIR(st->st_mode))
383 filetype = BTRFS_FT_DIR;
384 if (S_ISREG(st->st_mode))
385 filetype = BTRFS_FT_REG_FILE;
386 if (S_ISLNK(st->st_mode))
387 filetype = BTRFS_FT_SYMLINK;
389 ret = btrfs_insert_dir_item(trans, root, name, name_len,
390 parent_inum, &location,
391 filetype, index_cnt);
394 ret = btrfs_insert_inode_ref(trans, root, name, name_len,
395 objectid, parent_inum, index_cnt);
396 *dir_index_cnt = index_cnt;
402 static int fill_inode_item(struct btrfs_trans_handle *trans,
403 struct btrfs_root *root,
404 struct btrfs_inode_item *dst, struct stat *src)
407 u64 sectorsize = root->sectorsize;
410 * btrfs_inode_item has some reserved fields
411 * and represents on-disk inode entry, so
412 * zero everything to prevent information leak
414 memset(dst, 0, sizeof (*dst));
416 btrfs_set_stack_inode_generation(dst, trans->transid);
417 btrfs_set_stack_inode_size(dst, src->st_size);
418 btrfs_set_stack_inode_nbytes(dst, 0);
419 btrfs_set_stack_inode_block_group(dst, 0);
420 btrfs_set_stack_inode_nlink(dst, src->st_nlink);
421 btrfs_set_stack_inode_uid(dst, src->st_uid);
422 btrfs_set_stack_inode_gid(dst, src->st_gid);
423 btrfs_set_stack_inode_mode(dst, src->st_mode);
424 btrfs_set_stack_inode_rdev(dst, 0);
425 btrfs_set_stack_inode_flags(dst, 0);
426 btrfs_set_stack_timespec_sec(&dst->atime, src->st_atime);
427 btrfs_set_stack_timespec_nsec(&dst->atime, 0);
428 btrfs_set_stack_timespec_sec(&dst->ctime, src->st_ctime);
429 btrfs_set_stack_timespec_nsec(&dst->ctime, 0);
430 btrfs_set_stack_timespec_sec(&dst->mtime, src->st_mtime);
431 btrfs_set_stack_timespec_nsec(&dst->mtime, 0);
432 btrfs_set_stack_timespec_sec(&dst->otime, 0);
433 btrfs_set_stack_timespec_nsec(&dst->otime, 0);
435 if (S_ISDIR(src->st_mode)) {
436 btrfs_set_stack_inode_size(dst, 0);
437 btrfs_set_stack_inode_nlink(dst, 1);
439 if (S_ISREG(src->st_mode)) {
440 btrfs_set_stack_inode_size(dst, (u64)src->st_size);
441 if (src->st_size <= BTRFS_MAX_INLINE_DATA_SIZE(root))
442 btrfs_set_stack_inode_nbytes(dst, src->st_size);
444 blocks = src->st_size / sectorsize;
445 if (src->st_size % sectorsize)
447 blocks *= sectorsize;
448 btrfs_set_stack_inode_nbytes(dst, blocks);
451 if (S_ISLNK(src->st_mode))
452 btrfs_set_stack_inode_nbytes(dst, src->st_size + 1);
457 static int directory_select(const struct direct *entry)
459 if ((strncmp(entry->d_name, ".", entry->d_reclen) == 0) ||
460 (strncmp(entry->d_name, "..", entry->d_reclen) == 0))
466 static void free_namelist(struct direct **files, int count)
473 for (i = 0; i < count; ++i)
478 static u64 calculate_dir_inode_size(char *dirname)
481 struct direct **files, *cur_file;
482 u64 dir_inode_size = 0;
484 count = scandir(dirname, &files, directory_select, NULL);
486 for (i = 0; i < count; i++) {
488 dir_inode_size += strlen(cur_file->d_name);
491 free_namelist(files, count);
494 return dir_inode_size;
497 static int add_inode_items(struct btrfs_trans_handle *trans,
498 struct btrfs_root *root,
499 struct stat *st, char *name,
500 u64 self_objectid, ino_t parent_inum,
501 int dir_index_cnt, struct btrfs_inode_item *inode_ret)
504 struct btrfs_key inode_key;
505 struct btrfs_inode_item btrfs_inode;
509 fill_inode_item(trans, root, &btrfs_inode, st);
510 objectid = self_objectid;
512 if (S_ISDIR(st->st_mode)) {
513 inode_size = calculate_dir_inode_size(name);
514 btrfs_set_stack_inode_size(&btrfs_inode, inode_size);
517 inode_key.objectid = objectid;
518 inode_key.offset = 0;
519 btrfs_set_key_type(&inode_key, BTRFS_INODE_ITEM_KEY);
521 ret = btrfs_insert_inode(trans, root, objectid, &btrfs_inode);
523 *inode_ret = btrfs_inode;
527 static int add_xattr_item(struct btrfs_trans_handle *trans,
528 struct btrfs_root *root, u64 objectid,
529 const char *file_name)
533 char xattr_list[XATTR_LIST_MAX];
535 char cur_value[XATTR_SIZE_MAX];
536 char delimiter = '\0';
537 char *next_location = xattr_list;
539 ret = llistxattr(file_name, xattr_list, XATTR_LIST_MAX);
543 fprintf(stderr, "get a list of xattr failed for %s\n",
550 cur_name = strtok(xattr_list, &delimiter);
551 while (cur_name != NULL) {
552 cur_name_len = strlen(cur_name);
553 next_location += cur_name_len + 1;
555 ret = getxattr(file_name, cur_name, cur_value, XATTR_SIZE_MAX);
559 fprintf(stderr, "get a xattr value failed for %s attr %s\n",
560 file_name, cur_name);
564 ret = btrfs_insert_xattr_item(trans, root, cur_name,
565 cur_name_len, cur_value,
568 fprintf(stderr, "insert a xattr item failed for %s\n",
572 cur_name = strtok(next_location, &delimiter);
578 static int add_symbolic_link(struct btrfs_trans_handle *trans,
579 struct btrfs_root *root,
580 u64 objectid, const char *path_name)
583 u64 sectorsize = root->sectorsize;
584 char *buf = malloc(sectorsize);
586 ret = readlink(path_name, buf, sectorsize);
588 fprintf(stderr, "readlink failed for %s\n", path_name);
591 if (ret >= sectorsize) {
592 fprintf(stderr, "symlink too long for %s", path_name);
597 buf[ret] = '\0'; /* readlink does not do it for us */
598 ret = btrfs_insert_inline_extent(trans, root, objectid, 0,
605 static int add_file_items(struct btrfs_trans_handle *trans,
606 struct btrfs_root *root,
607 struct btrfs_inode_item *btrfs_inode, u64 objectid,
608 ino_t parent_inum, struct stat *st,
609 const char *path_name, int out_fd)
614 struct btrfs_key key;
616 u32 sectorsize = root->sectorsize;
621 struct extent_buffer *eb = NULL;
624 if (st->st_size == 0)
627 fd = open(path_name, O_RDONLY);
629 fprintf(stderr, "%s open failed\n", path_name);
633 blocks = st->st_size / sectorsize;
634 if (st->st_size % sectorsize)
637 if (st->st_size <= BTRFS_MAX_INLINE_DATA_SIZE(root)) {
638 char *buffer = malloc(st->st_size);
639 ret_read = pread64(fd, buffer, st->st_size, bytes_read);
640 if (ret_read == -1) {
641 fprintf(stderr, "%s read failed\n", path_name);
646 ret = btrfs_insert_inline_extent(trans, root, objectid, 0,
647 buffer, st->st_size);
652 /* round up our st_size to the FS blocksize */
653 total_bytes = (u64)blocks * sectorsize;
656 * do our IO in extent buffers so it can work
657 * against any raid type
659 eb = malloc(sizeof(*eb) + sectorsize);
664 memset(eb, 0, sizeof(*eb) + sectorsize);
669 * keep our extent size at 1MB max, this makes it easier to work inside
670 * the tiny block groups created during mkfs
672 cur_bytes = min(total_bytes, 1024ULL * 1024);
673 ret = btrfs_reserve_extent(trans, root, cur_bytes, 0, 0, (u64)-1,
678 first_block = key.objectid;
681 while (bytes_read < cur_bytes) {
683 memset(eb->data, 0, sectorsize);
685 ret_read = pread64(fd, eb->data, sectorsize, file_pos + bytes_read);
686 if (ret_read == -1) {
687 fprintf(stderr, "%s read failed\n", path_name);
691 eb->start = first_block + bytes_read;
692 eb->len = sectorsize;
695 * we're doing the csum before we record the extent, but
698 ret = btrfs_csum_file_block(trans, root->fs_info->csum_root,
699 first_block + bytes_read + sectorsize,
700 first_block + bytes_read,
701 eb->data, sectorsize);
705 ret = write_and_map_eb(trans, root, eb);
707 fprintf(stderr, "output file write failed\n");
711 bytes_read += sectorsize;
715 ret = btrfs_record_file_extent(trans, root, objectid, btrfs_inode,
716 file_pos, first_block, cur_bytes);
722 file_pos += cur_bytes;
723 total_bytes -= cur_bytes;
734 static char *make_path(char *dir, char *name)
738 path = malloc(strlen(dir) + strlen(name) + 2);
742 if (dir[strlen(dir) - 1] != '/')
748 static int traverse_directory(struct btrfs_trans_handle *trans,
749 struct btrfs_root *root, char *dir_name,
750 struct directory_name_entry *dir_head, int out_fd)
754 struct btrfs_inode_item cur_inode;
755 struct btrfs_inode_item *inode_item;
756 int count, i, dir_index_cnt;
757 struct direct **files;
759 struct directory_name_entry *dir_entry, *parent_dir_entry;
760 struct direct *cur_file;
761 ino_t parent_inum, cur_inum;
762 ino_t highest_inum = 0;
763 char *parent_dir_name;
764 char real_path[PATH_MAX];
765 struct btrfs_path path;
766 struct extent_buffer *leaf;
767 struct btrfs_key root_dir_key;
768 u64 root_dir_inode_size = 0;
770 /* Add list for source directory */
771 dir_entry = malloc(sizeof(struct directory_name_entry));
772 dir_entry->dir_name = dir_name;
773 dir_entry->path = realpath(dir_name, real_path);
774 if (!dir_entry->path) {
775 fprintf(stderr, "get directory real path error\n");
780 parent_inum = highest_inum + BTRFS_FIRST_FREE_OBJECTID;
781 dir_entry->inum = parent_inum;
782 list_add_tail(&dir_entry->list, &dir_head->list);
784 btrfs_init_path(&path);
786 root_dir_key.objectid = btrfs_root_dirid(&root->root_item);
787 root_dir_key.offset = 0;
788 btrfs_set_key_type(&root_dir_key, BTRFS_INODE_ITEM_KEY);
789 ret = btrfs_lookup_inode(trans, root, &path, &root_dir_key, 1);
791 fprintf(stderr, "root dir lookup error\n");
795 leaf = path.nodes[0];
796 inode_item = btrfs_item_ptr(leaf, path.slots[0],
797 struct btrfs_inode_item);
799 root_dir_inode_size = calculate_dir_inode_size(dir_name);
800 btrfs_set_inode_size(leaf, inode_item, root_dir_inode_size);
801 btrfs_mark_buffer_dirty(leaf);
803 btrfs_release_path(&path);
806 parent_dir_entry = list_entry(dir_head->list.next,
807 struct directory_name_entry,
809 list_del(&parent_dir_entry->list);
811 parent_inum = parent_dir_entry->inum;
812 parent_dir_name = parent_dir_entry->dir_name;
813 if (chdir(parent_dir_entry->path)) {
814 fprintf(stderr, "chdir error for %s\n",
820 count = scandir(parent_dir_entry->path, &files,
821 directory_select, NULL);
824 fprintf(stderr, "scandir for %s failed: %s\n",
825 parent_dir_name, strerror (errno));
830 for (i = 0; i < count; i++) {
833 if (lstat(cur_file->d_name, &st) == -1) {
834 fprintf(stderr, "lstat failed for file %s\n",
840 cur_inum = st.st_ino;
841 ret = add_directory_items(trans, root,
842 cur_inum, parent_inum,
844 &st, &dir_index_cnt);
846 fprintf(stderr, "add_directory_items failed\n");
850 ret = add_inode_items(trans, root, &st,
851 cur_file->d_name, cur_inum,
852 parent_inum, dir_index_cnt,
854 if (ret == -EEXIST) {
855 BUG_ON(st.st_nlink <= 1);
859 fprintf(stderr, "add_inode_items failed\n");
863 ret = add_xattr_item(trans, root,
864 cur_inum, cur_file->d_name);
866 fprintf(stderr, "add_xattr_item failed\n");
871 if (S_ISDIR(st.st_mode)) {
872 dir_entry = malloc(sizeof(struct directory_name_entry));
873 dir_entry->dir_name = cur_file->d_name;
874 dir_entry->path = make_path(parent_dir_entry->path,
876 dir_entry->inum = cur_inum;
877 list_add_tail(&dir_entry->list, &dir_head->list);
878 } else if (S_ISREG(st.st_mode)) {
879 ret = add_file_items(trans, root, &cur_inode,
880 cur_inum, parent_inum, &st,
881 cur_file->d_name, out_fd);
883 fprintf(stderr, "add_file_items failed\n");
886 } else if (S_ISLNK(st.st_mode)) {
887 ret = add_symbolic_link(trans, root,
888 cur_inum, cur_file->d_name);
890 fprintf(stderr, "add_symbolic_link failed\n");
896 free_namelist(files, count);
897 free(parent_dir_entry);
901 } while (!list_empty(&dir_head->list));
906 free_namelist(files, count);
908 free(parent_dir_entry);
915 static int open_target(char *output_name)
918 output_fd = open(output_name, O_CREAT | O_RDWR | O_TRUNC,
919 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH);
924 static int create_chunks(struct btrfs_trans_handle *trans,
925 struct btrfs_root *root, u64 num_of_meta_chunks,
927 struct mkfs_allocation *allocation)
931 u64 meta_type = BTRFS_BLOCK_GROUP_METADATA;
932 u64 data_type = BTRFS_BLOCK_GROUP_DATA;
933 u64 minimum_data_chunk_size = 8 * 1024 * 1024;
937 for (i = 0; i < num_of_meta_chunks; i++) {
938 ret = btrfs_alloc_chunk(trans, root->fs_info->extent_root,
939 &chunk_start, &chunk_size, meta_type);
941 ret = btrfs_make_block_group(trans, root->fs_info->extent_root, 0,
942 meta_type, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
943 chunk_start, chunk_size);
944 allocation->metadata += chunk_size;
946 set_extent_dirty(&root->fs_info->free_space_cache,
947 chunk_start, chunk_start + chunk_size - 1, 0);
950 if (size_of_data < minimum_data_chunk_size)
951 size_of_data = minimum_data_chunk_size;
953 ret = btrfs_alloc_data_chunk(trans, root->fs_info->extent_root,
954 &chunk_start, size_of_data, data_type);
956 ret = btrfs_make_block_group(trans, root->fs_info->extent_root, 0,
957 data_type, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
958 chunk_start, size_of_data);
959 allocation->data += size_of_data;
961 set_extent_dirty(&root->fs_info->free_space_cache,
962 chunk_start, chunk_start + size_of_data - 1, 0);
966 static int make_image(char *source_dir, struct btrfs_root *root, int out_fd)
969 struct btrfs_trans_handle *trans;
973 struct directory_name_entry dir_head;
975 struct directory_name_entry *dir_entry = NULL;
977 ret = lstat(source_dir, &root_st);
979 fprintf(stderr, "unable to lstat the %s\n", source_dir);
983 INIT_LIST_HEAD(&dir_head.list);
985 trans = btrfs_start_transaction(root, 1);
986 ret = traverse_directory(trans, root, source_dir, &dir_head, out_fd);
988 fprintf(stderr, "unable to traverse_directory\n");
991 btrfs_commit_transaction(trans, root);
994 printf("Making image is completed.\n");
997 while (!list_empty(&dir_head.list)) {
998 dir_entry = list_entry(dir_head.list.next,
999 struct directory_name_entry, list);
1000 list_del(&dir_entry->list);
1004 fprintf(stderr, "Making image is aborted.\n");
1009 * This ignores symlinks with unreadable targets and subdirs that can't
1010 * be read. It's a best-effort to give a rough estimate of the size of
1011 * a subdir. It doesn't guarantee that prepopulating btrfs from this
1012 * tree won't still run out of space.
1014 * The rounding up to 4096 is questionable. Previous code used du -B 4096.
1016 static u64 global_total_size;
1017 static int ftw_add_entry_size(const char *fpath, const struct stat *st,
1020 if (type == FTW_F || type == FTW_D)
1021 global_total_size += round_up(st->st_size, 4096);
1026 static u64 size_sourcedir(char *dir_name, u64 sectorsize,
1027 u64 *num_of_meta_chunks_ret, u64 *size_of_data_ret)
1032 u64 default_chunk_size = 8 * 1024 * 1024; /* 8MB */
1033 u64 allocated_meta_size = 8 * 1024 * 1024; /* 8MB */
1034 u64 allocated_total_size = 20 * 1024 * 1024; /* 20MB */
1035 u64 num_of_meta_chunks = 0;
1036 u64 num_of_data_chunks = 0;
1037 u64 num_of_allocated_meta_chunks =
1038 allocated_meta_size / default_chunk_size;
1040 global_total_size = 0;
1041 ret = ftw(dir_name, ftw_add_entry_size, 10);
1042 dir_size = global_total_size;
1044 fprintf(stderr, "ftw subdir walk of '%s' failed: %s\n",
1045 dir_name, strerror(errno));
1049 num_of_data_chunks = (dir_size + default_chunk_size - 1) /
1052 num_of_meta_chunks = (dir_size / 2) / default_chunk_size;
1053 if (((dir_size / 2) % default_chunk_size) != 0)
1054 num_of_meta_chunks++;
1055 if (num_of_meta_chunks <= num_of_allocated_meta_chunks)
1056 num_of_meta_chunks = 0;
1058 num_of_meta_chunks -= num_of_allocated_meta_chunks;
1060 total_size = allocated_total_size +
1061 (num_of_data_chunks * default_chunk_size) +
1062 (num_of_meta_chunks * default_chunk_size);
1064 *num_of_meta_chunks_ret = num_of_meta_chunks;
1065 *size_of_data_ret = num_of_data_chunks * default_chunk_size;
1069 static int zero_output_file(int out_fd, u64 size, u32 sectorsize)
1071 int len = sectorsize;
1072 int loop_num = size / sectorsize;
1074 char *buf = malloc(len);
1080 memset(buf, 0, len);
1081 for (i = 0; i < loop_num; i++) {
1082 written = pwrite64(out_fd, buf, len, location);
1085 location += sectorsize;
1091 static int is_ssd(const char *file)
1095 char sysfs_path[PATH_MAX];
1101 probe = blkid_new_probe_from_filename(file);
1105 /* Device number of this disk (possibly a partition) */
1106 devno = blkid_probe_get_devno(probe);
1108 blkid_free_probe(probe);
1112 /* Get whole disk name (not full path) for this devno */
1113 ret = blkid_devno_to_wholedisk(devno,
1114 wholedisk, sizeof(wholedisk), NULL);
1116 blkid_free_probe(probe);
1120 snprintf(sysfs_path, PATH_MAX, "/sys/block/%s/queue/rotational",
1123 blkid_free_probe(probe);
1125 fd = open(sysfs_path, O_RDONLY);
1130 if (read(fd, &rotational, sizeof(char)) < sizeof(char)) {
1136 return !atoi((const char *)&rotational);
1139 static void list_all_devices(struct btrfs_root *root)
1141 struct btrfs_fs_devices *fs_devices;
1142 struct btrfs_device *device;
1143 int number_of_devices = 0;
1144 u64 total_block_count = 0;
1146 fs_devices = root->fs_info->fs_devices;
1148 list_for_each_entry(device, &fs_devices->devices, dev_list)
1149 number_of_devices++;
1151 printf("Number of devices: %d\n", number_of_devices);
1152 /* printf("Total devices size: %10s\n", */
1153 /* pretty_size(total_block_count)); */
1154 printf("Devices:\n");
1155 printf(" ID SIZE PATH\n");
1156 list_for_each_entry_reverse(device, &fs_devices->devices, dev_list) {
1157 char dev_uuid[BTRFS_UUID_UNPARSED_SIZE];
1159 uuid_unparse(device->uuid, dev_uuid);
1160 printf(" %3llu %10s %s\n",
1162 pretty_size(device->total_bytes),
1164 total_block_count += device->total_bytes;
1170 int main(int ac, char **av)
1173 struct btrfs_root *root;
1174 struct btrfs_trans_handle *trans;
1176 u64 block_count = 0;
1177 u64 dev_block_count = 0;
1179 u64 alloc_start = 0;
1180 u64 metadata_profile = 0;
1181 u64 data_profile = 0;
1182 u32 nodesize = max_t(u32, sysconf(_SC_PAGESIZE),
1183 BTRFS_MKFS_DEFAULT_NODE_SIZE);
1184 u32 sectorsize = 4096;
1185 u32 stripesize = 4096;
1191 int nodesize_forced = 0;
1192 int data_profile_opt = 0;
1193 int metadata_profile_opt = 0;
1196 int force_overwrite = 0;
1197 char *source_dir = NULL;
1198 int source_dir_set = 0;
1199 u64 num_of_meta_chunks = 0;
1200 u64 size_of_data = 0;
1201 u64 source_dir_size = 0;
1204 char fs_uuid[BTRFS_UUID_UNPARSED_SIZE] = { 0 };
1205 u64 features = BTRFS_MKFS_DEFAULT_FEATURES;
1206 struct mkfs_allocation allocation = { 0 };
1207 struct btrfs_mkfs_config mkfs_cfg;
1211 static const struct option long_options[] = {
1212 { "alloc-start", required_argument, NULL, 'A'},
1213 { "byte-count", required_argument, NULL, 'b' },
1214 { "force", no_argument, NULL, 'f' },
1215 { "leafsize", required_argument, NULL, 'l' },
1216 { "label", required_argument, NULL, 'L'},
1217 { "metadata", required_argument, NULL, 'm' },
1218 { "mixed", no_argument, NULL, 'M' },
1219 { "nodesize", required_argument, NULL, 'n' },
1220 { "sectorsize", required_argument, NULL, 's' },
1221 { "data", required_argument, NULL, 'd' },
1222 { "version", no_argument, NULL, 'V' },
1223 { "rootdir", required_argument, NULL, 'r' },
1224 { "nodiscard", no_argument, NULL, 'K' },
1225 { "features", required_argument, NULL, 'O' },
1226 { "uuid", required_argument, NULL, 'U' },
1227 { "quiet", 0, NULL, 'q' },
1228 { "help", no_argument, NULL, GETOPT_VAL_HELP },
1232 c = getopt_long(ac, av, "A:b:fl:n:s:m:d:L:O:r:U:VMKq",
1233 long_options, NULL);
1238 alloc_start = parse_size(optarg);
1241 force_overwrite = 1;
1244 data_profile = parse_profile(optarg);
1245 data_profile_opt = 1;
1249 "WARNING: --leafsize is deprecated, use --nodesize\n");
1251 nodesize = parse_size(optarg);
1252 nodesize_forced = 1;
1255 label = parse_label(optarg);
1258 metadata_profile = parse_profile(optarg);
1259 metadata_profile_opt = 1;
1265 char *orig = strdup(optarg);
1268 tmp = btrfs_parse_fs_features(tmp, &features);
1271 "Unrecognized filesystem feature '%s'\n",
1277 if (features & BTRFS_FEATURE_LIST_ALL) {
1278 btrfs_list_all_fs_features(0);
1284 sectorsize = parse_size(optarg);
1287 block_count = parse_size(optarg);
1288 if (block_count <= BTRFS_MKFS_SMALL_VOLUME_SIZE)
1296 source_dir = optarg;
1300 strncpy(fs_uuid, optarg,
1301 BTRFS_UUID_UNPARSED_SIZE - 1);
1309 case GETOPT_VAL_HELP:
1311 print_usage(c != GETOPT_VAL_HELP);
1314 sectorsize = max(sectorsize, (u32)sysconf(_SC_PAGESIZE));
1315 if (btrfs_check_nodesize(nodesize, sectorsize))
1317 saved_optind = optind;
1318 dev_cnt = ac - optind;
1322 if (source_dir_set && dev_cnt > 1) {
1324 "The -r option is limited to a single device\n");
1331 if (uuid_parse(fs_uuid, dummy_uuid) != 0) {
1332 fprintf(stderr, "could not parse UUID: %s\n", fs_uuid);
1335 if (!test_uuid_unique(fs_uuid)) {
1336 fprintf(stderr, "non-unique UUID: %s\n", fs_uuid);
1341 while (dev_cnt-- > 0) {
1342 file = av[optind++];
1343 if (is_block_device(file))
1344 if (test_dev_for_mkfs(file, force_overwrite))
1348 optind = saved_optind;
1349 dev_cnt = ac - optind;
1351 file = av[optind++];
1354 if (is_vol_small(file) || mixed) {
1356 printf("SMALL VOLUME: forcing mixed metadata/data groups\n");
1361 * Set default profiles according to number of added devices.
1362 * For mixed groups defaults are single/single.
1365 if (!metadata_profile_opt) {
1366 if (dev_cnt == 1 && ssd && verbose)
1367 printf("Detected a SSD, turning off metadata "
1368 "duplication. Mkfs with -m dup if you want to "
1369 "force metadata duplication.\n");
1371 metadata_profile = (dev_cnt > 1) ?
1372 BTRFS_BLOCK_GROUP_RAID1 : (ssd) ?
1373 0: BTRFS_BLOCK_GROUP_DUP;
1375 if (!data_profile_opt) {
1376 data_profile = (dev_cnt > 1) ?
1377 BTRFS_BLOCK_GROUP_RAID0 : 0; /* raid0 or single */
1380 u32 best_nodesize = max_t(u32, sysconf(_SC_PAGESIZE), sectorsize);
1382 if (metadata_profile_opt || data_profile_opt) {
1383 if (metadata_profile != data_profile) {
1385 "ERROR: With mixed block groups data and metadata profiles must be the same\n");
1390 if (!nodesize_forced) {
1391 nodesize = best_nodesize;
1392 if (btrfs_check_nodesize(nodesize, sectorsize))
1395 if (nodesize != sectorsize) {
1396 fprintf(stderr, "Error: mixed metadata/data block groups "
1397 "require metadata blocksizes equal to the sectorsize\n");
1402 /* Check device/block_count after the nodesize is determined */
1403 if (block_count && block_count < btrfs_min_dev_size(nodesize)) {
1405 "Size '%llu' is too small to make a usable filesystem\n",
1408 "Minimum size for btrfs filesystem is %llu\n",
1409 btrfs_min_dev_size(nodesize));
1412 for (i = saved_optind; i < saved_optind + dev_cnt; i++) {
1416 ret = test_minimum_size(path, nodesize);
1418 fprintf(stderr, "Failed to check size for '%s': %s\n",
1419 path, strerror(-ret));
1424 "'%s' is too small to make a usable filesystem\n",
1427 "Minimum size for each btrfs device is %llu.\n",
1428 btrfs_min_dev_size(nodesize));
1432 ret = test_num_disk_vs_raid(metadata_profile, data_profile,
1437 /* if we are here that means all devs are good to btrfsify */
1439 printf("%s\n", PACKAGE_STRING);
1440 printf("See %s for more information.\n\n", PACKAGE_URL);
1445 if (!source_dir_set) {
1447 * open without O_EXCL so that the problem should not
1448 * occur by the following processing.
1449 * (btrfs_register_one_device() fails if O_EXCL is on)
1451 fd = open(file, O_RDWR);
1453 fprintf(stderr, "unable to open %s: %s\n", file,
1457 ret = btrfs_prepare_device(fd, file, zero_end, &dev_block_count,
1458 block_count, &mixed, discard);
1463 if (block_count && block_count > dev_block_count) {
1464 fprintf(stderr, "%s is smaller than requested size\n", file);
1468 fd = open_target(file);
1470 fprintf(stderr, "unable to open the %s\n", file);
1474 source_dir_size = size_sourcedir(source_dir, sectorsize,
1475 &num_of_meta_chunks, &size_of_data);
1476 if(block_count < source_dir_size)
1477 block_count = source_dir_size;
1478 ret = zero_output_file(fd, block_count, sectorsize);
1480 fprintf(stderr, "unable to zero the output file\n");
1483 /* our "device" is the new image file */
1484 dev_block_count = block_count;
1487 /* To create the first block group and chunk 0 in make_btrfs */
1488 if (dev_block_count < BTRFS_MKFS_SYSTEM_GROUP_SIZE) {
1489 fprintf(stderr, "device is too small to make filesystem\n");
1493 blocks[0] = BTRFS_SUPER_INFO_OFFSET;
1494 for (i = 1; i < 7; i++) {
1495 blocks[i] = BTRFS_SUPER_INFO_OFFSET + 1024 * 1024 +
1499 if (group_profile_max_safe_loss(metadata_profile) <
1500 group_profile_max_safe_loss(data_profile)){
1502 "WARNING: metatdata has lower redundancy than data!\n\n");
1506 * FS features that can be set by other means than -O
1507 * just set the bit here
1510 features |= BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS;
1512 if ((data_profile | metadata_profile) &
1513 (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6)) {
1514 features |= BTRFS_FEATURE_INCOMPAT_RAID56;
1517 mkfs_cfg.label = label;
1518 mkfs_cfg.fs_uuid = fs_uuid;
1519 memcpy(mkfs_cfg.blocks, blocks, sizeof(blocks));
1520 mkfs_cfg.num_bytes = dev_block_count;
1521 mkfs_cfg.nodesize = nodesize;
1522 mkfs_cfg.sectorsize = sectorsize;
1523 mkfs_cfg.stripesize = stripesize;
1524 mkfs_cfg.features = features;
1526 ret = make_btrfs(fd, &mkfs_cfg);
1528 fprintf(stderr, "error during mkfs: %s\n", strerror(-ret));
1532 root = open_ctree(file, 0, OPEN_CTREE_WRITES);
1534 fprintf(stderr, "Open ctree failed\n");
1538 root->fs_info->alloc_start = alloc_start;
1540 ret = create_metadata_block_groups(root, mixed, &allocation);
1542 fprintf(stderr, "failed to create default block groups\n");
1546 trans = btrfs_start_transaction(root, 1);
1549 ret = make_root_dir(trans, root, mixed, &allocation);
1551 fprintf(stderr, "failed to setup the root directory\n");
1555 btrfs_commit_transaction(trans, root);
1557 trans = btrfs_start_transaction(root, 1);
1559 if (is_block_device(file))
1560 btrfs_register_one_device(file);
1565 while (dev_cnt-- > 0) {
1566 int old_mixed = mixed;
1568 file = av[optind++];
1571 * open without O_EXCL so that the problem should not
1572 * occur by the following processing.
1573 * (btrfs_register_one_device() fails if O_EXCL is on)
1575 fd = open(file, O_RDWR);
1577 fprintf(stderr, "unable to open %s: %s\n", file,
1581 ret = btrfs_device_already_in_root(root, fd,
1582 BTRFS_SUPER_INFO_OFFSET);
1584 fprintf(stderr, "skipping duplicate device %s in FS\n",
1589 ret = btrfs_prepare_device(fd, file, zero_end, &dev_block_count,
1590 block_count, &mixed, discard);
1597 ret = btrfs_add_to_fsid(trans, root, fd, file, dev_block_count,
1598 sectorsize, sectorsize, sectorsize);
1601 struct btrfs_device *device;
1603 device = container_of(root->fs_info->fs_devices->devices.next,
1604 struct btrfs_device, dev_list);
1605 printf("adding device %s id %llu\n", file,
1606 (unsigned long long)device->devid);
1609 if (is_block_device(file))
1610 btrfs_register_one_device(file);
1614 if (!source_dir_set) {
1615 ret = create_raid_groups(trans, root, data_profile,
1616 data_profile_opt, metadata_profile,
1617 mixed, &allocation);
1621 ret = create_data_reloc_tree(trans, root);
1624 btrfs_commit_transaction(trans, root);
1626 if (source_dir_set) {
1627 trans = btrfs_start_transaction(root, 1);
1628 ret = create_chunks(trans, root,
1629 num_of_meta_chunks, size_of_data,
1632 btrfs_commit_transaction(trans, root);
1634 ret = make_image(source_dir, root, fd);
1639 char features_buf[64];
1641 printf("Label: %s\n", label);
1642 printf("UUID: %s\n", fs_uuid);
1643 printf("Node size: %u\n", nodesize);
1644 printf("Sector size: %u\n", sectorsize);
1645 printf("Filesystem size: %s\n",
1646 pretty_size(btrfs_super_total_bytes(root->fs_info->super_copy)));
1647 printf("Block group profiles:\n");
1648 if (allocation.data)
1649 printf(" Data: %-8s %16s\n",
1650 btrfs_group_profile_str(data_profile),
1651 pretty_size(allocation.data));
1652 if (allocation.metadata)
1653 printf(" Metadata: %-8s %16s\n",
1654 btrfs_group_profile_str(metadata_profile),
1655 pretty_size(allocation.metadata));
1656 if (allocation.mixed)
1657 printf(" Data+Metadata: %-8s %16s\n",
1658 btrfs_group_profile_str(data_profile),
1659 pretty_size(allocation.mixed));
1660 printf(" System: %-8s %16s\n",
1661 btrfs_group_profile_str(metadata_profile),
1662 pretty_size(allocation.system));
1663 printf("SSD detected: %s\n", ssd ? "yes" : "no");
1664 btrfs_parse_features_to_string(features_buf, features);
1665 printf("Incompat features: %s", features_buf);
1668 list_all_devices(root);
1671 ret = close_ctree(root);