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,
63 u64 metadata_profile, int mixed,
64 struct mkfs_allocation *allocation)
66 struct btrfs_trans_handle *trans;
72 trans = btrfs_start_transaction(root, 1);
73 bytes_used = btrfs_super_bytes_used(root->fs_info->super_copy);
75 root->fs_info->system_allocs = 1;
76 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;
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);
91 "no space to alloc data/metadata chunk\n");
95 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);
102 allocation->mixed += chunk_size;
104 ret = btrfs_alloc_chunk(trans, root->fs_info->extent_root,
105 &chunk_start, &chunk_size,
106 BTRFS_BLOCK_GROUP_METADATA);
107 if (ret == -ENOSPC) {
108 fprintf(stderr, "no space to alloc metadata chunk\n");
112 ret = btrfs_make_block_group(trans, root, 0,
114 BTRFS_BLOCK_GROUP_METADATA,
115 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
116 chunk_start, chunk_size);
117 allocation->metadata += chunk_size;
121 root->fs_info->system_allocs = 0;
122 btrfs_commit_transaction(trans, root);
128 static int create_data_block_groups(struct btrfs_trans_handle *trans,
129 struct btrfs_root *root, u64 data_profile, int mixed,
130 struct mkfs_allocation *allocation)
137 ret = btrfs_alloc_chunk(trans, root->fs_info->extent_root,
138 &chunk_start, &chunk_size,
139 BTRFS_BLOCK_GROUP_DATA);
140 if (ret == -ENOSPC) {
141 fprintf(stderr, "no space to alloc data chunk\n");
145 ret = btrfs_make_block_group(trans, root, 0,
147 BTRFS_BLOCK_GROUP_DATA,
148 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
149 chunk_start, chunk_size);
150 allocation->data += chunk_size;
158 static int make_root_dir(struct btrfs_trans_handle *trans, struct btrfs_root *root,
159 int mixed, struct mkfs_allocation *allocation)
161 struct btrfs_key location;
164 ret = btrfs_make_root_dir(trans, root->fs_info->tree_root,
165 BTRFS_ROOT_TREE_DIR_OBJECTID);
168 ret = btrfs_make_root_dir(trans, root, BTRFS_FIRST_FREE_OBJECTID);
171 memcpy(&location, &root->fs_info->fs_root->root_key, sizeof(location));
172 location.offset = (u64)-1;
173 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
175 btrfs_super_root_dir(root->fs_info->super_copy),
176 &location, BTRFS_FT_DIR, 0);
180 ret = btrfs_insert_inode_ref(trans, root->fs_info->tree_root,
181 "default", 7, location.objectid,
182 BTRFS_ROOT_TREE_DIR_OBJECTID, 0);
190 static void __recow_root(struct btrfs_trans_handle *trans,
191 struct btrfs_root *root)
194 struct extent_buffer *tmp;
196 if (trans->transid != btrfs_root_generation(&root->root_item)) {
197 extent_buffer_get(root->node);
198 ret = __btrfs_cow_block(trans, root, root->node,
199 NULL, 0, &tmp, 0, 0);
201 free_extent_buffer(tmp);
205 static void recow_roots(struct btrfs_trans_handle *trans,
206 struct btrfs_root *root)
208 struct btrfs_fs_info *info = root->fs_info;
210 __recow_root(trans, info->fs_root);
211 __recow_root(trans, info->tree_root);
212 __recow_root(trans, info->extent_root);
213 __recow_root(trans, info->chunk_root);
214 __recow_root(trans, info->dev_root);
215 __recow_root(trans, info->csum_root);
218 static int create_one_raid_group(struct btrfs_trans_handle *trans,
219 struct btrfs_root *root, u64 type,
220 struct mkfs_allocation *allocation)
227 ret = btrfs_alloc_chunk(trans, root->fs_info->extent_root,
228 &chunk_start, &chunk_size, type);
229 if (ret == -ENOSPC) {
230 fprintf(stderr, "not enough free space\n");
234 ret = btrfs_make_block_group(trans, root->fs_info->extent_root, 0,
235 type, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
236 chunk_start, chunk_size);
237 if ((type & BTRFS_BLOCK_GROUP_TYPE_MASK) == BTRFS_BLOCK_GROUP_DATA)
238 allocation->data += chunk_size;
239 else if ((type & BTRFS_BLOCK_GROUP_TYPE_MASK) == BTRFS_BLOCK_GROUP_METADATA)
240 allocation->metadata += chunk_size;
241 else if ((type & BTRFS_BLOCK_GROUP_TYPE_MASK) == BTRFS_BLOCK_GROUP_SYSTEM)
242 allocation->system += chunk_size;
243 else if ((type & BTRFS_BLOCK_GROUP_TYPE_MASK) ==
244 (BTRFS_BLOCK_GROUP_METADATA|BTRFS_BLOCK_GROUP_DATA))
245 allocation->mixed += chunk_size;
253 static int create_raid_groups(struct btrfs_trans_handle *trans,
254 struct btrfs_root *root, u64 data_profile,
255 u64 metadata_profile, int mixed,
256 struct mkfs_allocation *allocation)
258 u64 num_devices = btrfs_super_num_devices(root->fs_info->super_copy);
261 if (metadata_profile) {
262 u64 meta_flags = BTRFS_BLOCK_GROUP_METADATA;
264 ret = create_one_raid_group(trans, root,
265 BTRFS_BLOCK_GROUP_SYSTEM |
266 metadata_profile, allocation);
270 meta_flags |= BTRFS_BLOCK_GROUP_DATA;
272 ret = create_one_raid_group(trans, root, meta_flags |
273 metadata_profile, allocation);
277 if (!mixed && num_devices > 1 && data_profile) {
278 ret = create_one_raid_group(trans, root,
279 BTRFS_BLOCK_GROUP_DATA |
280 data_profile, allocation);
283 recow_roots(trans, root);
288 static int create_data_reloc_tree(struct btrfs_trans_handle *trans,
289 struct btrfs_root *root)
291 struct btrfs_key location;
292 struct btrfs_root_item root_item;
293 struct extent_buffer *tmp;
294 u64 objectid = BTRFS_DATA_RELOC_TREE_OBJECTID;
297 ret = btrfs_copy_root(trans, root, root->node, &tmp, objectid);
300 memcpy(&root_item, &root->root_item, sizeof(root_item));
301 btrfs_set_root_bytenr(&root_item, tmp->start);
302 btrfs_set_root_level(&root_item, btrfs_header_level(tmp));
303 btrfs_set_root_generation(&root_item, trans->transid);
304 free_extent_buffer(tmp);
306 location.objectid = objectid;
307 location.type = BTRFS_ROOT_ITEM_KEY;
309 ret = btrfs_insert_root(trans, root->fs_info->tree_root,
310 &location, &root_item);
315 static void print_usage(int ret)
317 fprintf(stderr, "usage: mkfs.btrfs [options] dev [ dev ... ]\n");
318 fprintf(stderr, "options:\n");
319 fprintf(stderr, "\t-A|--alloc-start START the offset to start the FS\n");
320 fprintf(stderr, "\t-b|--byte-count SIZE total number of bytes in the FS\n");
321 fprintf(stderr, "\t-d|--data PROFILE data profile, raid0, raid1, raid5, raid6, raid10, dup or single\n");
322 fprintf(stderr, "\t-f|--force force overwrite of existing filesystem\n");
323 fprintf(stderr, "\t-l|--leafsize SIZE deprecated, alias for nodesize\n");
324 fprintf(stderr, "\t-L|--label LABEL set a label\n");
325 fprintf(stderr, "\t-m|--metadata PROFILE metadata profile, values like data profile\n");
326 fprintf(stderr, "\t-M|--mixed mix metadata and data together\n");
327 fprintf(stderr, "\t-n|--nodesize SIZE size of btree nodes\n");
328 fprintf(stderr, "\t-s|--sectorsize SIZE min block allocation (may not mountable by current kernel)\n");
329 fprintf(stderr, "\t-r|--rootdir DIR the source directory\n");
330 fprintf(stderr, "\t-K|--nodiscard do not perform whole device TRIM\n");
331 fprintf(stderr, "\t-O|--features LIST comma separated list of filesystem features, use '-O list-all' to list features\n");
332 fprintf(stderr, "\t-U|--uuid UUID specify the filesystem UUID\n");
333 fprintf(stderr, "\t-q|--quiet no messages except errors\n");
334 fprintf(stderr, "\t-V|--version print the mkfs.btrfs version and exit\n");
335 fprintf(stderr, "%s\n", PACKAGE_STRING);
339 static void print_version(void) __attribute__((noreturn));
340 static void print_version(void)
342 fprintf(stderr, "mkfs.btrfs, part of %s\n", PACKAGE_STRING);
346 static u64 parse_profile(char *s)
348 if (strcmp(s, "raid0") == 0) {
349 return BTRFS_BLOCK_GROUP_RAID0;
350 } else if (strcasecmp(s, "raid1") == 0) {
351 return BTRFS_BLOCK_GROUP_RAID1;
352 } else if (strcasecmp(s, "raid5") == 0) {
353 return BTRFS_BLOCK_GROUP_RAID5;
354 } else if (strcasecmp(s, "raid6") == 0) {
355 return BTRFS_BLOCK_GROUP_RAID6;
356 } else if (strcasecmp(s, "raid10") == 0) {
357 return BTRFS_BLOCK_GROUP_RAID10;
358 } else if (strcasecmp(s, "dup") == 0) {
359 return BTRFS_BLOCK_GROUP_DUP;
360 } else if (strcasecmp(s, "single") == 0) {
363 fprintf(stderr, "Unknown profile %s\n", s);
369 static char *parse_label(char *input)
371 int len = strlen(input);
373 if (len >= BTRFS_LABEL_SIZE) {
374 fprintf(stderr, "Label %s is too long (max %d)\n", input,
375 BTRFS_LABEL_SIZE - 1);
378 return strdup(input);
381 static int add_directory_items(struct btrfs_trans_handle *trans,
382 struct btrfs_root *root, u64 objectid,
383 ino_t parent_inum, const char *name,
384 struct stat *st, int *dir_index_cnt)
388 struct btrfs_key location;
391 name_len = strlen(name);
393 location.objectid = objectid;
395 btrfs_set_key_type(&location, BTRFS_INODE_ITEM_KEY);
397 if (S_ISDIR(st->st_mode))
398 filetype = BTRFS_FT_DIR;
399 if (S_ISREG(st->st_mode))
400 filetype = BTRFS_FT_REG_FILE;
401 if (S_ISLNK(st->st_mode))
402 filetype = BTRFS_FT_SYMLINK;
404 ret = btrfs_insert_dir_item(trans, root, name, name_len,
405 parent_inum, &location,
406 filetype, index_cnt);
409 ret = btrfs_insert_inode_ref(trans, root, name, name_len,
410 objectid, parent_inum, index_cnt);
411 *dir_index_cnt = index_cnt;
417 static int fill_inode_item(struct btrfs_trans_handle *trans,
418 struct btrfs_root *root,
419 struct btrfs_inode_item *dst, struct stat *src)
422 u64 sectorsize = root->sectorsize;
425 * btrfs_inode_item has some reserved fields
426 * and represents on-disk inode entry, so
427 * zero everything to prevent information leak
429 memset(dst, 0, sizeof (*dst));
431 btrfs_set_stack_inode_generation(dst, trans->transid);
432 btrfs_set_stack_inode_size(dst, src->st_size);
433 btrfs_set_stack_inode_nbytes(dst, 0);
434 btrfs_set_stack_inode_block_group(dst, 0);
435 btrfs_set_stack_inode_nlink(dst, src->st_nlink);
436 btrfs_set_stack_inode_uid(dst, src->st_uid);
437 btrfs_set_stack_inode_gid(dst, src->st_gid);
438 btrfs_set_stack_inode_mode(dst, src->st_mode);
439 btrfs_set_stack_inode_rdev(dst, 0);
440 btrfs_set_stack_inode_flags(dst, 0);
441 btrfs_set_stack_timespec_sec(&dst->atime, src->st_atime);
442 btrfs_set_stack_timespec_nsec(&dst->atime, 0);
443 btrfs_set_stack_timespec_sec(&dst->ctime, src->st_ctime);
444 btrfs_set_stack_timespec_nsec(&dst->ctime, 0);
445 btrfs_set_stack_timespec_sec(&dst->mtime, src->st_mtime);
446 btrfs_set_stack_timespec_nsec(&dst->mtime, 0);
447 btrfs_set_stack_timespec_sec(&dst->otime, 0);
448 btrfs_set_stack_timespec_nsec(&dst->otime, 0);
450 if (S_ISDIR(src->st_mode)) {
451 btrfs_set_stack_inode_size(dst, 0);
452 btrfs_set_stack_inode_nlink(dst, 1);
454 if (S_ISREG(src->st_mode)) {
455 btrfs_set_stack_inode_size(dst, (u64)src->st_size);
456 if (src->st_size <= BTRFS_MAX_INLINE_DATA_SIZE(root))
457 btrfs_set_stack_inode_nbytes(dst, src->st_size);
459 blocks = src->st_size / sectorsize;
460 if (src->st_size % sectorsize)
462 blocks *= sectorsize;
463 btrfs_set_stack_inode_nbytes(dst, blocks);
466 if (S_ISLNK(src->st_mode))
467 btrfs_set_stack_inode_nbytes(dst, src->st_size + 1);
472 static int directory_select(const struct direct *entry)
474 if ((strncmp(entry->d_name, ".", entry->d_reclen) == 0) ||
475 (strncmp(entry->d_name, "..", entry->d_reclen) == 0))
481 static void free_namelist(struct direct **files, int count)
488 for (i = 0; i < count; ++i)
493 static u64 calculate_dir_inode_size(char *dirname)
496 struct direct **files, *cur_file;
497 u64 dir_inode_size = 0;
499 count = scandir(dirname, &files, directory_select, NULL);
501 for (i = 0; i < count; i++) {
503 dir_inode_size += strlen(cur_file->d_name);
506 free_namelist(files, count);
509 return dir_inode_size;
512 static int add_inode_items(struct btrfs_trans_handle *trans,
513 struct btrfs_root *root,
514 struct stat *st, char *name,
515 u64 self_objectid, ino_t parent_inum,
516 int dir_index_cnt, struct btrfs_inode_item *inode_ret)
519 struct btrfs_key inode_key;
520 struct btrfs_inode_item btrfs_inode;
524 fill_inode_item(trans, root, &btrfs_inode, st);
525 objectid = self_objectid;
527 if (S_ISDIR(st->st_mode)) {
528 inode_size = calculate_dir_inode_size(name);
529 btrfs_set_stack_inode_size(&btrfs_inode, inode_size);
532 inode_key.objectid = objectid;
533 inode_key.offset = 0;
534 btrfs_set_key_type(&inode_key, BTRFS_INODE_ITEM_KEY);
536 ret = btrfs_insert_inode(trans, root, objectid, &btrfs_inode);
538 *inode_ret = btrfs_inode;
542 static int add_xattr_item(struct btrfs_trans_handle *trans,
543 struct btrfs_root *root, u64 objectid,
544 const char *file_name)
548 char xattr_list[XATTR_LIST_MAX];
550 char cur_value[XATTR_SIZE_MAX];
551 char delimiter = '\0';
552 char *next_location = xattr_list;
554 ret = llistxattr(file_name, xattr_list, XATTR_LIST_MAX);
558 fprintf(stderr, "get a list of xattr failed for %s\n",
565 cur_name = strtok(xattr_list, &delimiter);
566 while (cur_name != NULL) {
567 cur_name_len = strlen(cur_name);
568 next_location += cur_name_len + 1;
570 ret = getxattr(file_name, cur_name, cur_value, XATTR_SIZE_MAX);
574 fprintf(stderr, "get a xattr value failed for %s attr %s\n",
575 file_name, cur_name);
579 ret = btrfs_insert_xattr_item(trans, root, cur_name,
580 cur_name_len, cur_value,
583 fprintf(stderr, "insert a xattr item failed for %s\n",
587 cur_name = strtok(next_location, &delimiter);
593 static int add_symbolic_link(struct btrfs_trans_handle *trans,
594 struct btrfs_root *root,
595 u64 objectid, const char *path_name)
598 u64 sectorsize = root->sectorsize;
599 char *buf = malloc(sectorsize);
601 ret = readlink(path_name, buf, sectorsize);
603 fprintf(stderr, "readlink failed for %s\n", path_name);
606 if (ret >= sectorsize) {
607 fprintf(stderr, "symlink too long for %s", path_name);
612 buf[ret] = '\0'; /* readlink does not do it for us */
613 ret = btrfs_insert_inline_extent(trans, root, objectid, 0,
620 static int add_file_items(struct btrfs_trans_handle *trans,
621 struct btrfs_root *root,
622 struct btrfs_inode_item *btrfs_inode, u64 objectid,
623 ino_t parent_inum, struct stat *st,
624 const char *path_name, int out_fd)
629 struct btrfs_key key;
631 u32 sectorsize = root->sectorsize;
636 struct extent_buffer *eb = NULL;
639 if (st->st_size == 0)
642 fd = open(path_name, O_RDONLY);
644 fprintf(stderr, "%s open failed\n", path_name);
648 blocks = st->st_size / sectorsize;
649 if (st->st_size % sectorsize)
652 if (st->st_size <= BTRFS_MAX_INLINE_DATA_SIZE(root)) {
653 char *buffer = malloc(st->st_size);
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);
661 ret = btrfs_insert_inline_extent(trans, root, objectid, 0,
662 buffer, st->st_size);
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 = malloc(sizeof(*eb) + sectorsize);
679 memset(eb, 0, sizeof(*eb) + sectorsize);
684 * keep our extent size at 1MB max, this makes it easier to work inside
685 * the tiny block groups created during mkfs
687 cur_bytes = min(total_bytes, 1024ULL * 1024);
688 ret = btrfs_reserve_extent(trans, root, cur_bytes, 0, 0, (u64)-1,
693 first_block = key.objectid;
696 while (bytes_read < cur_bytes) {
698 memset(eb->data, 0, sectorsize);
700 ret_read = pread64(fd, eb->data, sectorsize, file_pos + bytes_read);
701 if (ret_read == -1) {
702 fprintf(stderr, "%s read failed\n", path_name);
706 eb->start = first_block + bytes_read;
707 eb->len = sectorsize;
710 * we're doing the csum before we record the extent, but
713 ret = btrfs_csum_file_block(trans, root->fs_info->csum_root,
714 first_block + bytes_read + sectorsize,
715 first_block + bytes_read,
716 eb->data, sectorsize);
720 ret = write_and_map_eb(trans, root, eb);
722 fprintf(stderr, "output file write failed\n");
726 bytes_read += sectorsize;
730 ret = btrfs_record_file_extent(trans, root, objectid, btrfs_inode,
731 file_pos, first_block, cur_bytes);
737 file_pos += cur_bytes;
738 total_bytes -= cur_bytes;
749 static char *make_path(char *dir, char *name)
753 path = malloc(strlen(dir) + strlen(name) + 2);
757 if (dir[strlen(dir) - 1] != '/')
763 static int traverse_directory(struct btrfs_trans_handle *trans,
764 struct btrfs_root *root, char *dir_name,
765 struct directory_name_entry *dir_head, int out_fd)
769 struct btrfs_inode_item cur_inode;
770 struct btrfs_inode_item *inode_item;
771 int count, i, dir_index_cnt;
772 struct direct **files;
774 struct directory_name_entry *dir_entry, *parent_dir_entry;
775 struct direct *cur_file;
776 ino_t parent_inum, cur_inum;
777 ino_t highest_inum = 0;
778 char *parent_dir_name;
779 char real_path[PATH_MAX];
780 struct btrfs_path path;
781 struct extent_buffer *leaf;
782 struct btrfs_key root_dir_key;
783 u64 root_dir_inode_size = 0;
785 /* Add list for source directory */
786 dir_entry = malloc(sizeof(struct directory_name_entry));
787 dir_entry->dir_name = dir_name;
788 dir_entry->path = realpath(dir_name, real_path);
789 if (!dir_entry->path) {
790 fprintf(stderr, "get directory real path error\n");
795 parent_inum = highest_inum + BTRFS_FIRST_FREE_OBJECTID;
796 dir_entry->inum = parent_inum;
797 list_add_tail(&dir_entry->list, &dir_head->list);
799 btrfs_init_path(&path);
801 root_dir_key.objectid = btrfs_root_dirid(&root->root_item);
802 root_dir_key.offset = 0;
803 btrfs_set_key_type(&root_dir_key, BTRFS_INODE_ITEM_KEY);
804 ret = btrfs_lookup_inode(trans, root, &path, &root_dir_key, 1);
806 fprintf(stderr, "root dir lookup error\n");
810 leaf = path.nodes[0];
811 inode_item = btrfs_item_ptr(leaf, path.slots[0],
812 struct btrfs_inode_item);
814 root_dir_inode_size = calculate_dir_inode_size(dir_name);
815 btrfs_set_inode_size(leaf, inode_item, root_dir_inode_size);
816 btrfs_mark_buffer_dirty(leaf);
818 btrfs_release_path(&path);
821 parent_dir_entry = list_entry(dir_head->list.next,
822 struct directory_name_entry,
824 list_del(&parent_dir_entry->list);
826 parent_inum = parent_dir_entry->inum;
827 parent_dir_name = parent_dir_entry->dir_name;
828 if (chdir(parent_dir_entry->path)) {
829 fprintf(stderr, "chdir error for %s\n",
835 count = scandir(parent_dir_entry->path, &files,
836 directory_select, NULL);
839 fprintf(stderr, "scandir for %s failed: %s\n",
840 parent_dir_name, strerror (errno));
845 for (i = 0; i < count; i++) {
848 if (lstat(cur_file->d_name, &st) == -1) {
849 fprintf(stderr, "lstat failed for file %s\n",
855 cur_inum = st.st_ino;
856 ret = add_directory_items(trans, root,
857 cur_inum, parent_inum,
859 &st, &dir_index_cnt);
861 fprintf(stderr, "add_directory_items failed\n");
865 ret = add_inode_items(trans, root, &st,
866 cur_file->d_name, cur_inum,
867 parent_inum, dir_index_cnt,
869 if (ret == -EEXIST) {
870 BUG_ON(st.st_nlink <= 1);
874 fprintf(stderr, "add_inode_items failed\n");
878 ret = add_xattr_item(trans, root,
879 cur_inum, cur_file->d_name);
881 fprintf(stderr, "add_xattr_item failed\n");
886 if (S_ISDIR(st.st_mode)) {
887 dir_entry = malloc(sizeof(struct directory_name_entry));
888 dir_entry->dir_name = cur_file->d_name;
889 dir_entry->path = make_path(parent_dir_entry->path,
891 dir_entry->inum = cur_inum;
892 list_add_tail(&dir_entry->list, &dir_head->list);
893 } else if (S_ISREG(st.st_mode)) {
894 ret = add_file_items(trans, root, &cur_inode,
895 cur_inum, parent_inum, &st,
896 cur_file->d_name, out_fd);
898 fprintf(stderr, "add_file_items failed\n");
901 } else if (S_ISLNK(st.st_mode)) {
902 ret = add_symbolic_link(trans, root,
903 cur_inum, cur_file->d_name);
905 fprintf(stderr, "add_symbolic_link failed\n");
911 free_namelist(files, count);
912 free(parent_dir_entry);
916 } while (!list_empty(&dir_head->list));
921 free_namelist(files, count);
923 free(parent_dir_entry);
930 static int open_target(char *output_name)
933 output_fd = open(output_name, O_CREAT | O_RDWR | O_TRUNC,
934 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH);
939 static int create_chunks(struct btrfs_trans_handle *trans,
940 struct btrfs_root *root, u64 num_of_meta_chunks,
942 struct mkfs_allocation *allocation)
946 u64 meta_type = BTRFS_BLOCK_GROUP_METADATA;
947 u64 data_type = BTRFS_BLOCK_GROUP_DATA;
948 u64 minimum_data_chunk_size = 8 * 1024 * 1024;
952 for (i = 0; i < num_of_meta_chunks; i++) {
953 ret = btrfs_alloc_chunk(trans, root->fs_info->extent_root,
954 &chunk_start, &chunk_size, meta_type);
956 ret = btrfs_make_block_group(trans, root->fs_info->extent_root, 0,
957 meta_type, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
958 chunk_start, chunk_size);
959 allocation->metadata += chunk_size;
961 set_extent_dirty(&root->fs_info->free_space_cache,
962 chunk_start, chunk_start + chunk_size - 1, 0);
965 if (size_of_data < minimum_data_chunk_size)
966 size_of_data = minimum_data_chunk_size;
968 ret = btrfs_alloc_data_chunk(trans, root->fs_info->extent_root,
969 &chunk_start, size_of_data, data_type);
971 ret = btrfs_make_block_group(trans, root->fs_info->extent_root, 0,
972 data_type, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
973 chunk_start, size_of_data);
974 allocation->data += size_of_data;
976 set_extent_dirty(&root->fs_info->free_space_cache,
977 chunk_start, chunk_start + size_of_data - 1, 0);
981 static int make_image(char *source_dir, struct btrfs_root *root, int out_fd)
984 struct btrfs_trans_handle *trans;
988 struct directory_name_entry dir_head;
990 struct directory_name_entry *dir_entry = NULL;
992 ret = lstat(source_dir, &root_st);
994 fprintf(stderr, "unable to lstat the %s\n", source_dir);
998 INIT_LIST_HEAD(&dir_head.list);
1000 trans = btrfs_start_transaction(root, 1);
1001 ret = traverse_directory(trans, root, source_dir, &dir_head, out_fd);
1003 fprintf(stderr, "unable to traverse_directory\n");
1006 btrfs_commit_transaction(trans, root);
1009 printf("Making image is completed.\n");
1012 while (!list_empty(&dir_head.list)) {
1013 dir_entry = list_entry(dir_head.list.next,
1014 struct directory_name_entry, list);
1015 list_del(&dir_entry->list);
1019 fprintf(stderr, "Making image is aborted.\n");
1024 * This ignores symlinks with unreadable targets and subdirs that can't
1025 * be read. It's a best-effort to give a rough estimate of the size of
1026 * a subdir. It doesn't guarantee that prepopulating btrfs from this
1027 * tree won't still run out of space.
1029 * The rounding up to 4096 is questionable. Previous code used du -B 4096.
1031 static u64 global_total_size;
1032 static int ftw_add_entry_size(const char *fpath, const struct stat *st,
1035 if (type == FTW_F || type == FTW_D)
1036 global_total_size += round_up(st->st_size, 4096);
1041 static u64 size_sourcedir(char *dir_name, u64 sectorsize,
1042 u64 *num_of_meta_chunks_ret, u64 *size_of_data_ret)
1047 u64 default_chunk_size = 8 * 1024 * 1024; /* 8MB */
1048 u64 allocated_meta_size = 8 * 1024 * 1024; /* 8MB */
1049 u64 allocated_total_size = 20 * 1024 * 1024; /* 20MB */
1050 u64 num_of_meta_chunks = 0;
1051 u64 num_of_data_chunks = 0;
1052 u64 num_of_allocated_meta_chunks =
1053 allocated_meta_size / default_chunk_size;
1055 global_total_size = 0;
1056 ret = ftw(dir_name, ftw_add_entry_size, 10);
1057 dir_size = global_total_size;
1059 fprintf(stderr, "ftw subdir walk of '%s' failed: %s\n",
1060 dir_name, strerror(errno));
1064 num_of_data_chunks = (dir_size + default_chunk_size - 1) /
1067 num_of_meta_chunks = (dir_size / 2) / default_chunk_size;
1068 if (((dir_size / 2) % default_chunk_size) != 0)
1069 num_of_meta_chunks++;
1070 if (num_of_meta_chunks <= num_of_allocated_meta_chunks)
1071 num_of_meta_chunks = 0;
1073 num_of_meta_chunks -= num_of_allocated_meta_chunks;
1075 total_size = allocated_total_size +
1076 (num_of_data_chunks * default_chunk_size) +
1077 (num_of_meta_chunks * default_chunk_size);
1079 *num_of_meta_chunks_ret = num_of_meta_chunks;
1080 *size_of_data_ret = num_of_data_chunks * default_chunk_size;
1084 static int zero_output_file(int out_fd, u64 size, u32 sectorsize)
1086 int len = sectorsize;
1087 int loop_num = size / sectorsize;
1089 char *buf = malloc(len);
1095 memset(buf, 0, len);
1096 for (i = 0; i < loop_num; i++) {
1097 written = pwrite64(out_fd, buf, len, location);
1100 location += sectorsize;
1106 static int is_ssd(const char *file)
1110 char sysfs_path[PATH_MAX];
1116 probe = blkid_new_probe_from_filename(file);
1120 /* Device number of this disk (possibly a partition) */
1121 devno = blkid_probe_get_devno(probe);
1123 blkid_free_probe(probe);
1127 /* Get whole disk name (not full path) for this devno */
1128 ret = blkid_devno_to_wholedisk(devno,
1129 wholedisk, sizeof(wholedisk), NULL);
1131 blkid_free_probe(probe);
1135 snprintf(sysfs_path, PATH_MAX, "/sys/block/%s/queue/rotational",
1138 blkid_free_probe(probe);
1140 fd = open(sysfs_path, O_RDONLY);
1145 if (read(fd, &rotational, sizeof(char)) < sizeof(char)) {
1151 return !atoi((const char *)&rotational);
1154 static void list_all_devices(struct btrfs_root *root)
1156 struct btrfs_fs_devices *fs_devices;
1157 struct btrfs_device *device;
1158 int number_of_devices = 0;
1159 u64 total_block_count = 0;
1161 fs_devices = root->fs_info->fs_devices;
1163 list_for_each_entry(device, &fs_devices->devices, dev_list)
1164 number_of_devices++;
1166 printf("Number of devices: %d\n", number_of_devices);
1167 /* printf("Total devices size: %10s\n", */
1168 /* pretty_size(total_block_count)); */
1169 printf("Devices:\n");
1170 printf(" ID SIZE PATH\n");
1171 list_for_each_entry_reverse(device, &fs_devices->devices, dev_list) {
1172 char dev_uuid[BTRFS_UUID_UNPARSED_SIZE];
1174 uuid_unparse(device->uuid, dev_uuid);
1175 printf(" %3llu %10s %s\n",
1177 pretty_size(device->total_bytes),
1179 total_block_count += device->total_bytes;
1185 int main(int ac, char **av)
1188 struct btrfs_root *root;
1189 struct btrfs_trans_handle *trans;
1191 u64 block_count = 0;
1192 u64 dev_block_count = 0;
1194 u64 alloc_start = 0;
1195 u64 metadata_profile = 0;
1196 u64 data_profile = 0;
1197 u64 default_metadata_profile = 0;
1198 u64 default_data_profile = 0;
1199 u32 nodesize = max_t(u32, sysconf(_SC_PAGESIZE),
1200 BTRFS_MKFS_DEFAULT_NODE_SIZE);
1201 u32 sectorsize = 4096;
1202 u32 stripesize = 4096;
1208 int nodesize_forced = 0;
1209 int data_profile_opt = 0;
1210 int metadata_profile_opt = 0;
1213 int force_overwrite = 0;
1214 char *source_dir = NULL;
1215 int source_dir_set = 0;
1216 u64 num_of_meta_chunks = 0;
1217 u64 size_of_data = 0;
1218 u64 source_dir_size = 0;
1221 char fs_uuid[BTRFS_UUID_UNPARSED_SIZE] = { 0 };
1222 u64 features = BTRFS_MKFS_DEFAULT_FEATURES;
1223 struct mkfs_allocation allocation = { 0 };
1224 struct btrfs_mkfs_config mkfs_cfg;
1228 static const struct option long_options[] = {
1229 { "alloc-start", required_argument, NULL, 'A'},
1230 { "byte-count", required_argument, NULL, 'b' },
1231 { "force", no_argument, NULL, 'f' },
1232 { "leafsize", required_argument, NULL, 'l' },
1233 { "label", required_argument, NULL, 'L'},
1234 { "metadata", required_argument, NULL, 'm' },
1235 { "mixed", no_argument, NULL, 'M' },
1236 { "nodesize", required_argument, NULL, 'n' },
1237 { "sectorsize", required_argument, NULL, 's' },
1238 { "data", required_argument, NULL, 'd' },
1239 { "version", no_argument, NULL, 'V' },
1240 { "rootdir", required_argument, NULL, 'r' },
1241 { "nodiscard", no_argument, NULL, 'K' },
1242 { "features", required_argument, NULL, 'O' },
1243 { "uuid", required_argument, NULL, 'U' },
1244 { "quiet", 0, NULL, 'q' },
1245 { "help", no_argument, NULL, GETOPT_VAL_HELP },
1249 c = getopt_long(ac, av, "A:b:fl:n:s:m:d:L:O:r:U:VMKq",
1250 long_options, NULL);
1255 alloc_start = parse_size(optarg);
1258 force_overwrite = 1;
1261 data_profile = parse_profile(optarg);
1262 data_profile_opt = 1;
1266 "WARNING: --leafsize is deprecated, use --nodesize\n");
1268 nodesize = parse_size(optarg);
1269 nodesize_forced = 1;
1272 label = parse_label(optarg);
1275 metadata_profile = parse_profile(optarg);
1276 metadata_profile_opt = 1;
1282 char *orig = strdup(optarg);
1285 tmp = btrfs_parse_fs_features(tmp, &features);
1288 "Unrecognized filesystem feature '%s'\n",
1294 if (features & BTRFS_FEATURE_LIST_ALL) {
1295 btrfs_list_all_fs_features(0);
1301 sectorsize = parse_size(optarg);
1304 block_count = parse_size(optarg);
1305 if (block_count <= BTRFS_MKFS_SMALL_VOLUME_SIZE)
1313 source_dir = optarg;
1317 strncpy(fs_uuid, optarg,
1318 BTRFS_UUID_UNPARSED_SIZE - 1);
1326 case GETOPT_VAL_HELP:
1328 print_usage(c != GETOPT_VAL_HELP);
1331 sectorsize = max(sectorsize, (u32)sysconf(_SC_PAGESIZE));
1332 if (btrfs_check_nodesize(nodesize, sectorsize))
1334 saved_optind = optind;
1335 dev_cnt = ac - optind;
1339 if (source_dir_set && dev_cnt > 1) {
1341 "The -r option is limited to a single device\n");
1348 if (uuid_parse(fs_uuid, dummy_uuid) != 0) {
1349 fprintf(stderr, "could not parse UUID: %s\n", fs_uuid);
1352 if (!test_uuid_unique(fs_uuid)) {
1353 fprintf(stderr, "non-unique UUID: %s\n", fs_uuid);
1358 while (dev_cnt-- > 0) {
1359 file = av[optind++];
1360 if (is_block_device(file))
1361 if (test_dev_for_mkfs(file, force_overwrite))
1365 optind = saved_optind;
1366 dev_cnt = ac - optind;
1368 file = av[optind++];
1371 if (is_vol_small(file) || mixed) {
1373 printf("SMALL VOLUME: forcing mixed metadata/data groups\n");
1378 * Set default profiles according to number of added devices.
1379 * For mixed groups defaults are single/single.
1382 if (!metadata_profile_opt) {
1383 if (dev_cnt == 1 && ssd && verbose)
1384 printf("Detected a SSD, turning off metadata "
1385 "duplication. Mkfs with -m dup if you want to "
1386 "force metadata duplication.\n");
1388 metadata_profile = (dev_cnt > 1) ?
1389 BTRFS_BLOCK_GROUP_RAID1 : (ssd) ?
1390 0: BTRFS_BLOCK_GROUP_DUP;
1392 if (!data_profile_opt) {
1393 data_profile = (dev_cnt > 1) ?
1394 BTRFS_BLOCK_GROUP_RAID0 : 0; /* raid0 or single */
1397 u32 best_nodesize = max_t(u32, sysconf(_SC_PAGESIZE), sectorsize);
1399 if (metadata_profile_opt || data_profile_opt) {
1400 if (metadata_profile != data_profile) {
1402 "ERROR: With mixed block groups data and metadata profiles must be the same\n");
1407 if (!nodesize_forced) {
1408 nodesize = best_nodesize;
1409 if (btrfs_check_nodesize(nodesize, sectorsize))
1412 if (nodesize != sectorsize) {
1413 fprintf(stderr, "Error: mixed metadata/data block groups "
1414 "require metadata blocksizes equal to the sectorsize\n");
1419 /* Check device/block_count after the nodesize is determined */
1420 if (block_count && block_count < btrfs_min_dev_size(nodesize)) {
1422 "Size '%llu' is too small to make a usable filesystem\n",
1425 "Minimum size for btrfs filesystem is %llu\n",
1426 btrfs_min_dev_size(nodesize));
1429 for (i = saved_optind; i < saved_optind + dev_cnt; i++) {
1433 ret = test_minimum_size(path, nodesize);
1435 fprintf(stderr, "Failed to check size for '%s': %s\n",
1436 path, strerror(-ret));
1441 "'%s' is too small to make a usable filesystem\n",
1444 "Minimum size for each btrfs device is %llu.\n",
1445 btrfs_min_dev_size(nodesize));
1449 ret = test_num_disk_vs_raid(metadata_profile, data_profile,
1454 /* if we are here that means all devs are good to btrfsify */
1456 printf("%s\n", PACKAGE_STRING);
1457 printf("See %s for more information.\n\n", PACKAGE_URL);
1462 if (!source_dir_set) {
1464 * open without O_EXCL so that the problem should not
1465 * occur by the following processing.
1466 * (btrfs_register_one_device() fails if O_EXCL is on)
1468 fd = open(file, O_RDWR);
1470 fprintf(stderr, "unable to open %s: %s\n", file,
1474 ret = btrfs_prepare_device(fd, file, zero_end, &dev_block_count,
1475 block_count, &mixed, discard);
1480 if (block_count && block_count > dev_block_count) {
1481 fprintf(stderr, "%s is smaller than requested size\n", file);
1485 fd = open_target(file);
1487 fprintf(stderr, "unable to open the %s\n", file);
1491 source_dir_size = size_sourcedir(source_dir, sectorsize,
1492 &num_of_meta_chunks, &size_of_data);
1493 if(block_count < source_dir_size)
1494 block_count = source_dir_size;
1495 ret = zero_output_file(fd, block_count, sectorsize);
1497 fprintf(stderr, "unable to zero the output file\n");
1500 /* our "device" is the new image file */
1501 dev_block_count = block_count;
1504 /* To create the first block group and chunk 0 in make_btrfs */
1505 if (dev_block_count < BTRFS_MKFS_SYSTEM_GROUP_SIZE) {
1506 fprintf(stderr, "device is too small to make filesystem\n");
1510 blocks[0] = BTRFS_SUPER_INFO_OFFSET;
1511 for (i = 1; i < 7; i++) {
1512 blocks[i] = BTRFS_SUPER_INFO_OFFSET + 1024 * 1024 +
1516 if (group_profile_max_safe_loss(metadata_profile) <
1517 group_profile_max_safe_loss(data_profile)){
1519 "WARNING: metatdata has lower redundancy than data!\n\n");
1523 * FS features that can be set by other means than -O
1524 * just set the bit here
1527 features |= BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS;
1529 if ((data_profile | metadata_profile) &
1530 (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6)) {
1531 features |= BTRFS_FEATURE_INCOMPAT_RAID56;
1534 mkfs_cfg.label = label;
1535 mkfs_cfg.fs_uuid = fs_uuid;
1536 memcpy(mkfs_cfg.blocks, blocks, sizeof(blocks));
1537 mkfs_cfg.num_bytes = dev_block_count;
1538 mkfs_cfg.nodesize = nodesize;
1539 mkfs_cfg.sectorsize = sectorsize;
1540 mkfs_cfg.stripesize = stripesize;
1541 mkfs_cfg.features = features;
1543 ret = make_btrfs(fd, &mkfs_cfg);
1545 fprintf(stderr, "error during mkfs: %s\n", strerror(-ret));
1549 root = open_ctree(file, 0, OPEN_CTREE_WRITES);
1551 fprintf(stderr, "Open ctree failed\n");
1555 root->fs_info->alloc_start = alloc_start;
1558 default_metadata_profile = metadata_profile;
1559 default_data_profile = data_profile;
1562 * Temporary groups to store new device entries
1564 default_metadata_profile = 0;
1565 default_data_profile = 0;
1568 ret = create_metadata_block_groups(root, default_metadata_profile,
1569 mixed, &allocation);
1571 fprintf(stderr, "failed to create default block groups\n");
1575 trans = btrfs_start_transaction(root, 1);
1578 ret = create_data_block_groups(trans, root, default_data_profile,
1579 mixed, &allocation);
1581 fprintf(stderr, "failed to create default data block groups\n");
1585 ret = make_root_dir(trans, root, mixed, &allocation);
1587 fprintf(stderr, "failed to setup the root directory\n");
1591 btrfs_commit_transaction(trans, root);
1593 trans = btrfs_start_transaction(root, 1);
1595 if (is_block_device(file))
1596 btrfs_register_one_device(file);
1601 while (dev_cnt-- > 0) {
1602 int old_mixed = mixed;
1604 file = av[optind++];
1607 * open without O_EXCL so that the problem should not
1608 * occur by the following processing.
1609 * (btrfs_register_one_device() fails if O_EXCL is on)
1611 fd = open(file, O_RDWR);
1613 fprintf(stderr, "unable to open %s: %s\n", file,
1617 ret = btrfs_device_already_in_root(root, fd,
1618 BTRFS_SUPER_INFO_OFFSET);
1620 fprintf(stderr, "skipping duplicate device %s in FS\n",
1625 ret = btrfs_prepare_device(fd, file, zero_end, &dev_block_count,
1626 block_count, &mixed, discard);
1633 ret = btrfs_add_to_fsid(trans, root, fd, file, dev_block_count,
1634 sectorsize, sectorsize, sectorsize);
1637 struct btrfs_device *device;
1639 device = container_of(root->fs_info->fs_devices->devices.next,
1640 struct btrfs_device, dev_list);
1641 printf("adding device %s id %llu\n", file,
1642 (unsigned long long)device->devid);
1645 if (is_block_device(file))
1646 btrfs_register_one_device(file);
1649 if (!source_dir_set) {
1650 ret = create_raid_groups(trans, root, data_profile,
1651 metadata_profile, mixed, &allocation);
1656 ret = create_data_reloc_tree(trans, root);
1659 btrfs_commit_transaction(trans, root);
1661 if (source_dir_set) {
1662 trans = btrfs_start_transaction(root, 1);
1663 ret = create_chunks(trans, root,
1664 num_of_meta_chunks, size_of_data,
1667 btrfs_commit_transaction(trans, root);
1669 ret = make_image(source_dir, root, fd);
1674 char features_buf[64];
1676 printf("Label: %s\n", label);
1677 printf("UUID: %s\n", fs_uuid);
1678 printf("Node size: %u\n", nodesize);
1679 printf("Sector size: %u\n", sectorsize);
1680 printf("Filesystem size: %s\n",
1681 pretty_size(btrfs_super_total_bytes(root->fs_info->super_copy)));
1682 printf("Block group profiles:\n");
1683 if (allocation.data)
1684 printf(" Data: %-8s %16s\n",
1685 btrfs_group_profile_str(data_profile),
1686 pretty_size(allocation.data));
1687 if (allocation.metadata)
1688 printf(" Metadata: %-8s %16s\n",
1689 btrfs_group_profile_str(metadata_profile),
1690 pretty_size(allocation.metadata));
1691 if (allocation.mixed)
1692 printf(" Data+Metadata: %-8s %16s\n",
1693 btrfs_group_profile_str(data_profile),
1694 pretty_size(allocation.mixed));
1695 printf(" System: %-8s %16s\n",
1696 btrfs_group_profile_str(metadata_profile),
1697 pretty_size(allocation.system));
1698 printf("SSD detected: %s\n", ssd ? "yes" : "no");
1699 btrfs_parse_features_to_string(features_buf, features);
1700 printf("Incompat features: %s", features_buf);
1703 list_all_devices(root);
1706 ret = close_ctree(root);