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;
47 #define DEFAULT_MKFS_FEATURES (BTRFS_FEATURE_INCOMPAT_EXTENDED_IREF \
48 | BTRFS_FEATURE_INCOMPAT_SKINNY_METADATA)
50 struct directory_name_entry {
54 struct list_head list;
57 static int make_root_dir(struct btrfs_root *root, int mixed)
59 struct btrfs_trans_handle *trans;
60 struct btrfs_key location;
66 trans = btrfs_start_transaction(root, 1);
67 bytes_used = btrfs_super_bytes_used(root->fs_info->super_copy);
69 root->fs_info->system_allocs = 1;
70 ret = btrfs_make_block_group(trans, root, bytes_used,
71 BTRFS_BLOCK_GROUP_SYSTEM,
72 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
73 0, BTRFS_MKFS_SYSTEM_GROUP_SIZE);
77 ret = btrfs_alloc_chunk(trans, root->fs_info->extent_root,
78 &chunk_start, &chunk_size,
79 BTRFS_BLOCK_GROUP_METADATA |
80 BTRFS_BLOCK_GROUP_DATA);
83 "no space to alloc data/metadata chunk\n");
87 ret = btrfs_make_block_group(trans, root, 0,
88 BTRFS_BLOCK_GROUP_METADATA |
89 BTRFS_BLOCK_GROUP_DATA,
90 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
91 chunk_start, chunk_size);
93 printf("Created a data/metadata chunk of size %llu\n", chunk_size);
95 ret = btrfs_alloc_chunk(trans, root->fs_info->extent_root,
96 &chunk_start, &chunk_size,
97 BTRFS_BLOCK_GROUP_METADATA);
99 fprintf(stderr, "no space to alloc metadata chunk\n");
103 ret = btrfs_make_block_group(trans, root, 0,
104 BTRFS_BLOCK_GROUP_METADATA,
105 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
106 chunk_start, chunk_size);
110 root->fs_info->system_allocs = 0;
111 btrfs_commit_transaction(trans, root);
112 trans = btrfs_start_transaction(root, 1);
116 ret = btrfs_alloc_chunk(trans, root->fs_info->extent_root,
117 &chunk_start, &chunk_size,
118 BTRFS_BLOCK_GROUP_DATA);
119 if (ret == -ENOSPC) {
120 fprintf(stderr, "no space to alloc data chunk\n");
124 ret = btrfs_make_block_group(trans, root, 0,
125 BTRFS_BLOCK_GROUP_DATA,
126 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
127 chunk_start, chunk_size);
131 ret = btrfs_make_root_dir(trans, root->fs_info->tree_root,
132 BTRFS_ROOT_TREE_DIR_OBJECTID);
135 ret = btrfs_make_root_dir(trans, root, BTRFS_FIRST_FREE_OBJECTID);
138 memcpy(&location, &root->fs_info->fs_root->root_key, sizeof(location));
139 location.offset = (u64)-1;
140 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
142 btrfs_super_root_dir(root->fs_info->super_copy),
143 &location, BTRFS_FT_DIR, 0);
147 ret = btrfs_insert_inode_ref(trans, root->fs_info->tree_root,
148 "default", 7, location.objectid,
149 BTRFS_ROOT_TREE_DIR_OBJECTID, 0);
153 btrfs_commit_transaction(trans, root);
158 static void __recow_root(struct btrfs_trans_handle *trans,
159 struct btrfs_root *root)
162 struct extent_buffer *tmp;
164 if (trans->transid != btrfs_root_generation(&root->root_item)) {
165 extent_buffer_get(root->node);
166 ret = __btrfs_cow_block(trans, root, root->node,
167 NULL, 0, &tmp, 0, 0);
169 free_extent_buffer(tmp);
173 static void recow_roots(struct btrfs_trans_handle *trans,
174 struct btrfs_root *root)
176 struct btrfs_fs_info *info = root->fs_info;
178 __recow_root(trans, info->fs_root);
179 __recow_root(trans, info->tree_root);
180 __recow_root(trans, info->extent_root);
181 __recow_root(trans, info->chunk_root);
182 __recow_root(trans, info->dev_root);
183 __recow_root(trans, info->csum_root);
186 static int create_one_raid_group(struct btrfs_trans_handle *trans,
187 struct btrfs_root *root, u64 type)
193 ret = btrfs_alloc_chunk(trans, root->fs_info->extent_root,
194 &chunk_start, &chunk_size, type);
195 if (ret == -ENOSPC) {
196 fprintf(stderr, "not enough free space\n");
200 ret = btrfs_make_block_group(trans, root->fs_info->extent_root, 0,
201 type, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
202 chunk_start, chunk_size);
207 static int create_raid_groups(struct btrfs_trans_handle *trans,
208 struct btrfs_root *root, u64 data_profile,
209 int data_profile_opt, u64 metadata_profile,
212 u64 num_devices = btrfs_super_num_devices(root->fs_info->super_copy);
215 if (metadata_profile) {
216 u64 meta_flags = BTRFS_BLOCK_GROUP_METADATA;
218 ret = create_one_raid_group(trans, root,
219 BTRFS_BLOCK_GROUP_SYSTEM |
224 meta_flags |= BTRFS_BLOCK_GROUP_DATA;
226 ret = create_one_raid_group(trans, root, meta_flags |
231 if (!mixed && num_devices > 1 && data_profile) {
232 ret = create_one_raid_group(trans, root,
233 BTRFS_BLOCK_GROUP_DATA |
237 recow_roots(trans, root);
242 static int create_data_reloc_tree(struct btrfs_trans_handle *trans,
243 struct btrfs_root *root)
245 struct btrfs_key location;
246 struct btrfs_root_item root_item;
247 struct extent_buffer *tmp;
248 u64 objectid = BTRFS_DATA_RELOC_TREE_OBJECTID;
251 ret = btrfs_copy_root(trans, root, root->node, &tmp, objectid);
254 memcpy(&root_item, &root->root_item, sizeof(root_item));
255 btrfs_set_root_bytenr(&root_item, tmp->start);
256 btrfs_set_root_level(&root_item, btrfs_header_level(tmp));
257 btrfs_set_root_generation(&root_item, trans->transid);
258 free_extent_buffer(tmp);
260 location.objectid = objectid;
261 location.type = BTRFS_ROOT_ITEM_KEY;
263 ret = btrfs_insert_root(trans, root->fs_info->tree_root,
264 &location, &root_item);
270 static void print_usage(void) __attribute__((noreturn));
271 static void print_usage(void)
273 fprintf(stderr, "usage: mkfs.btrfs [options] dev [ dev ... ]\n");
274 fprintf(stderr, "options:\n");
275 fprintf(stderr, "\t-A|--alloc-start START the offset to start the FS\n");
276 fprintf(stderr, "\t-b|--byte-count SIZE total number of bytes in the FS\n");
277 fprintf(stderr, "\t-d|--data PROFILE data profile, raid0, raid1, raid5, raid6, raid10, dup or single\n");
278 fprintf(stderr, "\t-f|--force force overwrite of existing filesystem\n");
279 fprintf(stderr, "\t-l|--leafsize SIZE deprecated, alias for nodesize\n");
280 fprintf(stderr, "\t-L|--label LABEL set a label\n");
281 fprintf(stderr, "\t-m|--metadata PROFILE metadata profile, values like data profile\n");
282 fprintf(stderr, "\t-M|--mixed mix metadata and data together\n");
283 fprintf(stderr, "\t-n|--nodesize SIZE size of btree nodes\n");
284 fprintf(stderr, "\t-s|--sectorsize SIZE min block allocation (may not mountable by current kernel)\n");
285 fprintf(stderr, "\t-r|--rootdir DIR the source directory\n");
286 fprintf(stderr, "\t-K|--nodiscard do not perform whole device TRIM\n");
287 fprintf(stderr, "\t-O|--features LIST comma separated list of filesystem features\n");
288 fprintf(stderr, "\t-U|--uuid UUID specify the filesystem UUID\n");
289 fprintf(stderr, "\t-V|--version print the mkfs.btrfs version and exit\n");
290 fprintf(stderr, "%s\n", PACKAGE_STRING);
294 static void print_version(void) __attribute__((noreturn));
295 static void print_version(void)
297 fprintf(stderr, "mkfs.btrfs, part of %s\n", PACKAGE_STRING);
301 static u64 parse_profile(char *s)
303 if (strcmp(s, "raid0") == 0) {
304 return BTRFS_BLOCK_GROUP_RAID0;
305 } else if (strcmp(s, "raid1") == 0) {
306 return BTRFS_BLOCK_GROUP_RAID1;
307 } else if (strcmp(s, "raid5") == 0) {
308 return BTRFS_BLOCK_GROUP_RAID5;
309 } else if (strcmp(s, "raid6") == 0) {
310 return BTRFS_BLOCK_GROUP_RAID6;
311 } else if (strcmp(s, "raid10") == 0) {
312 return BTRFS_BLOCK_GROUP_RAID10;
313 } else if (strcmp(s, "dup") == 0) {
314 return BTRFS_BLOCK_GROUP_DUP;
315 } else if (strcmp(s, "single") == 0) {
318 fprintf(stderr, "Unknown profile %s\n", s);
325 static char *parse_label(char *input)
327 int len = strlen(input);
329 if (len >= BTRFS_LABEL_SIZE) {
330 fprintf(stderr, "Label %s is too long (max %d)\n", input,
331 BTRFS_LABEL_SIZE - 1);
334 return strdup(input);
337 static int add_directory_items(struct btrfs_trans_handle *trans,
338 struct btrfs_root *root, u64 objectid,
339 ino_t parent_inum, const char *name,
340 struct stat *st, int *dir_index_cnt)
344 struct btrfs_key location;
347 name_len = strlen(name);
349 location.objectid = objectid;
351 btrfs_set_key_type(&location, BTRFS_INODE_ITEM_KEY);
353 if (S_ISDIR(st->st_mode))
354 filetype = BTRFS_FT_DIR;
355 if (S_ISREG(st->st_mode))
356 filetype = BTRFS_FT_REG_FILE;
357 if (S_ISLNK(st->st_mode))
358 filetype = BTRFS_FT_SYMLINK;
360 ret = btrfs_insert_dir_item(trans, root, name, name_len,
361 parent_inum, &location,
362 filetype, index_cnt);
365 ret = btrfs_insert_inode_ref(trans, root, name, name_len,
366 objectid, parent_inum, index_cnt);
367 *dir_index_cnt = index_cnt;
373 static int fill_inode_item(struct btrfs_trans_handle *trans,
374 struct btrfs_root *root,
375 struct btrfs_inode_item *dst, struct stat *src)
378 u64 sectorsize = root->sectorsize;
381 * btrfs_inode_item has some reserved fields
382 * and represents on-disk inode entry, so
383 * zero everything to prevent information leak
385 memset(dst, 0, sizeof (*dst));
387 btrfs_set_stack_inode_generation(dst, trans->transid);
388 btrfs_set_stack_inode_size(dst, src->st_size);
389 btrfs_set_stack_inode_nbytes(dst, 0);
390 btrfs_set_stack_inode_block_group(dst, 0);
391 btrfs_set_stack_inode_nlink(dst, src->st_nlink);
392 btrfs_set_stack_inode_uid(dst, src->st_uid);
393 btrfs_set_stack_inode_gid(dst, src->st_gid);
394 btrfs_set_stack_inode_mode(dst, src->st_mode);
395 btrfs_set_stack_inode_rdev(dst, 0);
396 btrfs_set_stack_inode_flags(dst, 0);
397 btrfs_set_stack_timespec_sec(&dst->atime, src->st_atime);
398 btrfs_set_stack_timespec_nsec(&dst->atime, 0);
399 btrfs_set_stack_timespec_sec(&dst->ctime, src->st_ctime);
400 btrfs_set_stack_timespec_nsec(&dst->ctime, 0);
401 btrfs_set_stack_timespec_sec(&dst->mtime, src->st_mtime);
402 btrfs_set_stack_timespec_nsec(&dst->mtime, 0);
403 btrfs_set_stack_timespec_sec(&dst->otime, 0);
404 btrfs_set_stack_timespec_nsec(&dst->otime, 0);
406 if (S_ISDIR(src->st_mode)) {
407 btrfs_set_stack_inode_size(dst, 0);
408 btrfs_set_stack_inode_nlink(dst, 1);
410 if (S_ISREG(src->st_mode)) {
411 btrfs_set_stack_inode_size(dst, (u64)src->st_size);
412 if (src->st_size <= BTRFS_MAX_INLINE_DATA_SIZE(root))
413 btrfs_set_stack_inode_nbytes(dst, src->st_size);
415 blocks = src->st_size / sectorsize;
416 if (src->st_size % sectorsize)
418 blocks *= sectorsize;
419 btrfs_set_stack_inode_nbytes(dst, blocks);
422 if (S_ISLNK(src->st_mode))
423 btrfs_set_stack_inode_nbytes(dst, src->st_size + 1);
428 static int directory_select(const struct direct *entry)
430 if ((strncmp(entry->d_name, ".", entry->d_reclen) == 0) ||
431 (strncmp(entry->d_name, "..", entry->d_reclen) == 0))
437 static void free_namelist(struct direct **files, int count)
444 for (i = 0; i < count; ++i)
449 static u64 calculate_dir_inode_size(char *dirname)
452 struct direct **files, *cur_file;
453 u64 dir_inode_size = 0;
455 count = scandir(dirname, &files, directory_select, NULL);
457 for (i = 0; i < count; i++) {
459 dir_inode_size += strlen(cur_file->d_name);
462 free_namelist(files, count);
465 return dir_inode_size;
468 static int add_inode_items(struct btrfs_trans_handle *trans,
469 struct btrfs_root *root,
470 struct stat *st, char *name,
471 u64 self_objectid, ino_t parent_inum,
472 int dir_index_cnt, struct btrfs_inode_item *inode_ret)
475 struct btrfs_key inode_key;
476 struct btrfs_inode_item btrfs_inode;
480 fill_inode_item(trans, root, &btrfs_inode, st);
481 objectid = self_objectid;
483 if (S_ISDIR(st->st_mode)) {
484 inode_size = calculate_dir_inode_size(name);
485 btrfs_set_stack_inode_size(&btrfs_inode, inode_size);
488 inode_key.objectid = objectid;
489 inode_key.offset = 0;
490 btrfs_set_key_type(&inode_key, BTRFS_INODE_ITEM_KEY);
492 ret = btrfs_insert_inode(trans, root, objectid, &btrfs_inode);
494 *inode_ret = btrfs_inode;
498 static int add_xattr_item(struct btrfs_trans_handle *trans,
499 struct btrfs_root *root, u64 objectid,
500 const char *file_name)
504 char xattr_list[XATTR_LIST_MAX];
506 char cur_value[XATTR_SIZE_MAX];
507 char delimiter = '\0';
508 char *next_location = xattr_list;
510 ret = llistxattr(file_name, xattr_list, XATTR_LIST_MAX);
514 fprintf(stderr, "get a list of xattr failed for %s\n",
521 cur_name = strtok(xattr_list, &delimiter);
522 while (cur_name != NULL) {
523 cur_name_len = strlen(cur_name);
524 next_location += cur_name_len + 1;
526 ret = getxattr(file_name, cur_name, cur_value, XATTR_SIZE_MAX);
530 fprintf(stderr, "get a xattr value failed for %s attr %s\n",
531 file_name, cur_name);
535 ret = btrfs_insert_xattr_item(trans, root, cur_name,
536 cur_name_len, cur_value,
539 fprintf(stderr, "insert a xattr item failed for %s\n",
543 cur_name = strtok(next_location, &delimiter);
549 static int add_symbolic_link(struct btrfs_trans_handle *trans,
550 struct btrfs_root *root,
551 u64 objectid, const char *path_name)
554 u64 sectorsize = root->sectorsize;
555 char *buf = malloc(sectorsize);
557 ret = readlink(path_name, buf, sectorsize);
559 fprintf(stderr, "readlink failed for %s\n", path_name);
562 if (ret >= sectorsize) {
563 fprintf(stderr, "symlink too long for %s", path_name);
568 buf[ret] = '\0'; /* readlink does not do it for us */
569 ret = btrfs_insert_inline_extent(trans, root, objectid, 0,
576 static int add_file_items(struct btrfs_trans_handle *trans,
577 struct btrfs_root *root,
578 struct btrfs_inode_item *btrfs_inode, u64 objectid,
579 ino_t parent_inum, struct stat *st,
580 const char *path_name, int out_fd)
585 struct btrfs_key key;
587 u32 sectorsize = root->sectorsize;
592 struct extent_buffer *eb = NULL;
595 if (st->st_size == 0)
598 fd = open(path_name, O_RDONLY);
600 fprintf(stderr, "%s open failed\n", path_name);
604 blocks = st->st_size / sectorsize;
605 if (st->st_size % sectorsize)
608 if (st->st_size <= BTRFS_MAX_INLINE_DATA_SIZE(root)) {
609 char *buffer = malloc(st->st_size);
610 ret_read = pread64(fd, buffer, st->st_size, bytes_read);
611 if (ret_read == -1) {
612 fprintf(stderr, "%s read failed\n", path_name);
617 ret = btrfs_insert_inline_extent(trans, root, objectid, 0,
618 buffer, st->st_size);
623 /* round up our st_size to the FS blocksize */
624 total_bytes = (u64)blocks * sectorsize;
627 * do our IO in extent buffers so it can work
628 * against any raid type
630 eb = malloc(sizeof(*eb) + sectorsize);
635 memset(eb, 0, sizeof(*eb) + sectorsize);
640 * keep our extent size at 1MB max, this makes it easier to work inside
641 * the tiny block groups created during mkfs
643 cur_bytes = min(total_bytes, 1024ULL * 1024);
644 ret = btrfs_reserve_extent(trans, root, cur_bytes, 0, 0, (u64)-1,
649 first_block = key.objectid;
652 while (bytes_read < cur_bytes) {
654 memset(eb->data, 0, sectorsize);
656 ret_read = pread64(fd, eb->data, sectorsize, file_pos + bytes_read);
657 if (ret_read == -1) {
658 fprintf(stderr, "%s read failed\n", path_name);
662 eb->start = first_block + bytes_read;
663 eb->len = sectorsize;
666 * we're doing the csum before we record the extent, but
669 ret = btrfs_csum_file_block(trans, root->fs_info->csum_root,
670 first_block + bytes_read + sectorsize,
671 first_block + bytes_read,
672 eb->data, sectorsize);
676 ret = write_and_map_eb(trans, root, eb);
678 fprintf(stderr, "output file write failed\n");
682 bytes_read += sectorsize;
686 ret = btrfs_record_file_extent(trans, root, objectid, btrfs_inode,
687 file_pos, first_block, cur_bytes);
693 file_pos += cur_bytes;
694 total_bytes -= cur_bytes;
705 static char *make_path(char *dir, char *name)
709 path = malloc(strlen(dir) + strlen(name) + 2);
713 if (dir[strlen(dir) - 1] != '/')
719 static int traverse_directory(struct btrfs_trans_handle *trans,
720 struct btrfs_root *root, char *dir_name,
721 struct directory_name_entry *dir_head, int out_fd)
725 struct btrfs_inode_item cur_inode;
726 struct btrfs_inode_item *inode_item;
727 int count, i, dir_index_cnt;
728 struct direct **files;
730 struct directory_name_entry *dir_entry, *parent_dir_entry;
731 struct direct *cur_file;
732 ino_t parent_inum, cur_inum;
733 ino_t highest_inum = 0;
734 char *parent_dir_name;
735 char real_path[PATH_MAX];
736 struct btrfs_path path;
737 struct extent_buffer *leaf;
738 struct btrfs_key root_dir_key;
739 u64 root_dir_inode_size = 0;
741 /* Add list for source directory */
742 dir_entry = malloc(sizeof(struct directory_name_entry));
743 dir_entry->dir_name = dir_name;
744 dir_entry->path = realpath(dir_name, real_path);
745 if (!dir_entry->path) {
746 fprintf(stderr, "get directory real path error\n");
751 parent_inum = highest_inum + BTRFS_FIRST_FREE_OBJECTID;
752 dir_entry->inum = parent_inum;
753 list_add_tail(&dir_entry->list, &dir_head->list);
755 btrfs_init_path(&path);
757 root_dir_key.objectid = btrfs_root_dirid(&root->root_item);
758 root_dir_key.offset = 0;
759 btrfs_set_key_type(&root_dir_key, BTRFS_INODE_ITEM_KEY);
760 ret = btrfs_lookup_inode(trans, root, &path, &root_dir_key, 1);
762 fprintf(stderr, "root dir lookup error\n");
766 leaf = path.nodes[0];
767 inode_item = btrfs_item_ptr(leaf, path.slots[0],
768 struct btrfs_inode_item);
770 root_dir_inode_size = calculate_dir_inode_size(dir_name);
771 btrfs_set_inode_size(leaf, inode_item, root_dir_inode_size);
772 btrfs_mark_buffer_dirty(leaf);
774 btrfs_release_path(&path);
777 parent_dir_entry = list_entry(dir_head->list.next,
778 struct directory_name_entry,
780 list_del(&parent_dir_entry->list);
782 parent_inum = parent_dir_entry->inum;
783 parent_dir_name = parent_dir_entry->dir_name;
784 if (chdir(parent_dir_entry->path)) {
785 fprintf(stderr, "chdir error for %s\n",
791 count = scandir(parent_dir_entry->path, &files,
792 directory_select, NULL);
795 fprintf(stderr, "scandir for %s failed: %s\n",
796 parent_dir_name, strerror (errno));
801 for (i = 0; i < count; i++) {
804 if (lstat(cur_file->d_name, &st) == -1) {
805 fprintf(stderr, "lstat failed for file %s\n",
811 cur_inum = st.st_ino;
812 ret = add_directory_items(trans, root,
813 cur_inum, parent_inum,
815 &st, &dir_index_cnt);
817 fprintf(stderr, "add_directory_items failed\n");
821 ret = add_inode_items(trans, root, &st,
822 cur_file->d_name, cur_inum,
823 parent_inum, dir_index_cnt,
825 if (ret == -EEXIST) {
826 BUG_ON(st.st_nlink <= 1);
830 fprintf(stderr, "add_inode_items failed\n");
834 ret = add_xattr_item(trans, root,
835 cur_inum, cur_file->d_name);
837 fprintf(stderr, "add_xattr_item failed\n");
842 if (S_ISDIR(st.st_mode)) {
843 dir_entry = malloc(sizeof(struct directory_name_entry));
844 dir_entry->dir_name = cur_file->d_name;
845 dir_entry->path = make_path(parent_dir_entry->path,
847 dir_entry->inum = cur_inum;
848 list_add_tail(&dir_entry->list, &dir_head->list);
849 } else if (S_ISREG(st.st_mode)) {
850 ret = add_file_items(trans, root, &cur_inode,
851 cur_inum, parent_inum, &st,
852 cur_file->d_name, out_fd);
854 fprintf(stderr, "add_file_items failed\n");
857 } else if (S_ISLNK(st.st_mode)) {
858 ret = add_symbolic_link(trans, root,
859 cur_inum, cur_file->d_name);
861 fprintf(stderr, "add_symbolic_link failed\n");
867 free_namelist(files, count);
868 free(parent_dir_entry);
872 } while (!list_empty(&dir_head->list));
877 free_namelist(files, count);
879 free(parent_dir_entry);
886 static int open_target(char *output_name)
889 output_fd = open(output_name, O_CREAT | O_RDWR | O_TRUNC,
890 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH);
895 static int create_chunks(struct btrfs_trans_handle *trans,
896 struct btrfs_root *root, u64 num_of_meta_chunks,
901 u64 meta_type = BTRFS_BLOCK_GROUP_METADATA;
902 u64 data_type = BTRFS_BLOCK_GROUP_DATA;
903 u64 minimum_data_chunk_size = 8 * 1024 * 1024;
907 for (i = 0; i < num_of_meta_chunks; i++) {
908 ret = btrfs_alloc_chunk(trans, root->fs_info->extent_root,
909 &chunk_start, &chunk_size, meta_type);
911 ret = btrfs_make_block_group(trans, root->fs_info->extent_root, 0,
912 meta_type, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
913 chunk_start, chunk_size);
915 set_extent_dirty(&root->fs_info->free_space_cache,
916 chunk_start, chunk_start + chunk_size - 1, 0);
919 if (size_of_data < minimum_data_chunk_size)
920 size_of_data = minimum_data_chunk_size;
922 ret = btrfs_alloc_data_chunk(trans, root->fs_info->extent_root,
923 &chunk_start, size_of_data, data_type);
925 ret = btrfs_make_block_group(trans, root->fs_info->extent_root, 0,
926 data_type, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
927 chunk_start, size_of_data);
929 set_extent_dirty(&root->fs_info->free_space_cache,
930 chunk_start, chunk_start + size_of_data - 1, 0);
934 static int make_image(char *source_dir, struct btrfs_root *root, int out_fd)
937 struct btrfs_trans_handle *trans;
941 struct directory_name_entry dir_head;
943 struct directory_name_entry *dir_entry = NULL;
945 ret = lstat(source_dir, &root_st);
947 fprintf(stderr, "unable to lstat the %s\n", source_dir);
951 INIT_LIST_HEAD(&dir_head.list);
953 trans = btrfs_start_transaction(root, 1);
954 ret = traverse_directory(trans, root, source_dir, &dir_head, out_fd);
956 fprintf(stderr, "unable to traverse_directory\n");
959 btrfs_commit_transaction(trans, root);
961 printf("Making image is completed.\n");
964 while (!list_empty(&dir_head.list)) {
965 dir_entry = list_entry(dir_head.list.next,
966 struct directory_name_entry, list);
967 list_del(&dir_entry->list);
971 fprintf(stderr, "Making image is aborted.\n");
976 * This ignores symlinks with unreadable targets and subdirs that can't
977 * be read. It's a best-effort to give a rough estimate of the size of
978 * a subdir. It doesn't guarantee that prepopulating btrfs from this
979 * tree won't still run out of space.
981 * The rounding up to 4096 is questionable. Previous code used du -B 4096.
983 static u64 global_total_size;
984 static int ftw_add_entry_size(const char *fpath, const struct stat *st,
987 if (type == FTW_F || type == FTW_D)
988 global_total_size += round_up(st->st_size, 4096);
993 static u64 size_sourcedir(char *dir_name, u64 sectorsize,
994 u64 *num_of_meta_chunks_ret, u64 *size_of_data_ret)
999 u64 default_chunk_size = 8 * 1024 * 1024; /* 8MB */
1000 u64 allocated_meta_size = 8 * 1024 * 1024; /* 8MB */
1001 u64 allocated_total_size = 20 * 1024 * 1024; /* 20MB */
1002 u64 num_of_meta_chunks = 0;
1003 u64 num_of_data_chunks = 0;
1004 u64 num_of_allocated_meta_chunks =
1005 allocated_meta_size / default_chunk_size;
1007 global_total_size = 0;
1008 ret = ftw(dir_name, ftw_add_entry_size, 10);
1009 dir_size = global_total_size;
1011 fprintf(stderr, "ftw subdir walk of '%s' failed: %s\n",
1012 dir_name, strerror(errno));
1016 num_of_data_chunks = (dir_size + default_chunk_size - 1) /
1019 num_of_meta_chunks = (dir_size / 2) / default_chunk_size;
1020 if (((dir_size / 2) % default_chunk_size) != 0)
1021 num_of_meta_chunks++;
1022 if (num_of_meta_chunks <= num_of_allocated_meta_chunks)
1023 num_of_meta_chunks = 0;
1025 num_of_meta_chunks -= num_of_allocated_meta_chunks;
1027 total_size = allocated_total_size +
1028 (num_of_data_chunks * default_chunk_size) +
1029 (num_of_meta_chunks * default_chunk_size);
1031 *num_of_meta_chunks_ret = num_of_meta_chunks;
1032 *size_of_data_ret = num_of_data_chunks * default_chunk_size;
1036 static int zero_output_file(int out_fd, u64 size, u32 sectorsize)
1038 int len = sectorsize;
1039 int loop_num = size / sectorsize;
1041 char *buf = malloc(len);
1047 memset(buf, 0, len);
1048 for (i = 0; i < loop_num; i++) {
1049 written = pwrite64(out_fd, buf, len, location);
1052 location += sectorsize;
1058 static int is_ssd(const char *file)
1062 char sysfs_path[PATH_MAX];
1068 probe = blkid_new_probe_from_filename(file);
1072 /* Device number of this disk (possibly a partition) */
1073 devno = blkid_probe_get_devno(probe);
1075 blkid_free_probe(probe);
1079 /* Get whole disk name (not full path) for this devno */
1080 ret = blkid_devno_to_wholedisk(devno,
1081 wholedisk, sizeof(wholedisk), NULL);
1083 blkid_free_probe(probe);
1087 snprintf(sysfs_path, PATH_MAX, "/sys/block/%s/queue/rotational",
1090 blkid_free_probe(probe);
1092 fd = open(sysfs_path, O_RDONLY);
1097 if (read(fd, &rotational, sizeof(char)) < sizeof(char)) {
1103 return !atoi((const char *)&rotational);
1106 #define BTRFS_FEATURE_LIST_ALL (1ULL << 63)
1108 static const struct btrfs_fs_feature {
1112 } mkfs_features[] = {
1113 { "mixed-bg", BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS,
1114 "mixed data and metadata block groups" },
1115 { "extref", BTRFS_FEATURE_INCOMPAT_EXTENDED_IREF,
1116 "increased hardlink limit per file to 65536" },
1117 { "raid56", BTRFS_FEATURE_INCOMPAT_RAID56,
1118 "raid56 extended format" },
1119 { "skinny-metadata", BTRFS_FEATURE_INCOMPAT_SKINNY_METADATA,
1120 "reduced-size metadata extent refs" },
1121 { "no-holes", BTRFS_FEATURE_INCOMPAT_NO_HOLES,
1122 "no explicit hole extents for files" },
1123 /* Keep this one last */
1124 { "list-all", BTRFS_FEATURE_LIST_ALL, NULL }
1127 static void list_all_fs_features(void)
1131 fprintf(stderr, "Filesystem features available at mkfs time:\n");
1132 for (i = 0; i < ARRAY_SIZE(mkfs_features) - 1; i++) {
1133 char *is_default = "";
1135 if (mkfs_features[i].flag & DEFAULT_MKFS_FEATURES)
1136 is_default = ", default";
1137 fprintf(stderr, "%-20s- %s (0x%llx%s)\n",
1138 mkfs_features[i].name,
1139 mkfs_features[i].desc,
1140 mkfs_features[i].flag,
1145 static int parse_one_fs_feature(const char *name, u64 *flags)
1150 for (i = 0; i < ARRAY_SIZE(mkfs_features); i++) {
1151 if (name[0] == '^' &&
1152 !strcmp(mkfs_features[i].name, name + 1)) {
1153 *flags &= ~ mkfs_features[i].flag;
1155 } else if (!strcmp(mkfs_features[i].name, name)) {
1156 *flags |= mkfs_features[i].flag;
1164 static void process_fs_features(u64 flags)
1168 for (i = 0; i < ARRAY_SIZE(mkfs_features); i++) {
1169 if (flags & mkfs_features[i].flag) {
1170 printf("Turning ON incompat feature '%s': %s\n",
1171 mkfs_features[i].name,
1172 mkfs_features[i].desc);
1179 * Return NULL if all features were parsed fine, otherwise return the name of
1180 * the first unparsed.
1182 static char* parse_fs_features(char *namelist, u64 *flags)
1185 char *save_ptr = NULL; /* Satisfy static checkers */
1187 for (this_char = strtok_r(namelist, ",", &save_ptr);
1189 this_char = strtok_r(NULL, ",", &save_ptr)) {
1190 if (parse_one_fs_feature(this_char, flags))
1197 int main(int ac, char **av)
1200 struct btrfs_root *root;
1201 struct btrfs_trans_handle *trans;
1204 u64 block_count = 0;
1205 u64 dev_block_count = 0;
1207 u64 alloc_start = 0;
1208 u64 metadata_profile = 0;
1209 u64 data_profile = 0;
1210 u32 nodesize = max_t(u32, sysconf(_SC_PAGESIZE),
1211 BTRFS_MKFS_DEFAULT_NODE_SIZE);
1212 u32 sectorsize = 4096;
1213 u32 stripesize = 4096;
1219 int nodesize_forced = 0;
1220 int data_profile_opt = 0;
1221 int metadata_profile_opt = 0;
1224 int force_overwrite = 0;
1226 char *source_dir = NULL;
1227 int source_dir_set = 0;
1228 u64 num_of_meta_chunks = 0;
1229 u64 size_of_data = 0;
1230 u64 source_dir_size = 0;
1234 char *fs_uuid = NULL;
1235 u64 features = DEFAULT_MKFS_FEATURES;
1239 int option_index = 0;
1240 static const struct option long_options[] = {
1241 { "alloc-start", 1, NULL, 'A'},
1242 { "byte-count", 1, NULL, 'b' },
1243 { "force", 0, NULL, 'f' },
1244 { "leafsize", 1, NULL, 'l' },
1245 { "label", 1, NULL, 'L'},
1246 { "metadata", 1, NULL, 'm' },
1247 { "mixed", 0, NULL, 'M' },
1248 { "nodesize", 1, NULL, 'n' },
1249 { "sectorsize", 1, NULL, 's' },
1250 { "data", 1, NULL, 'd' },
1251 { "version", 0, NULL, 'V' },
1252 { "rootdir", 1, NULL, 'r' },
1253 { "nodiscard", 0, NULL, 'K' },
1254 { "features", 1, NULL, 'O' },
1255 { "uuid", required_argument, NULL, 'U' },
1259 c = getopt_long(ac, av, "A:b:fl:n:s:m:d:L:O:r:U:VMK",
1260 long_options, &option_index);
1265 alloc_start = parse_size(optarg);
1268 force_overwrite = 1;
1271 data_profile = parse_profile(optarg);
1272 data_profile_opt = 1;
1276 "WARNING: --leafsize is deprecated, use --nodesize\n");
1278 nodesize = parse_size(optarg);
1279 nodesize_forced = 1;
1282 label = parse_label(optarg);
1285 metadata_profile = parse_profile(optarg);
1286 metadata_profile_opt = 1;
1292 char *orig = strdup(optarg);
1295 tmp = parse_fs_features(tmp, &features);
1298 "Unrecognized filesystem feature '%s'\n",
1304 if (features & BTRFS_FEATURE_LIST_ALL) {
1305 list_all_fs_features();
1311 sectorsize = parse_size(optarg);
1314 block_count = parse_size(optarg);
1315 if (block_count <= BTRFS_MKFS_SMALL_VOLUME_SIZE) {
1317 "SMALL VOLUME: forcing mixed metadata/data groups\n");
1326 source_dir = optarg;
1339 sectorsize = max(sectorsize, (u32)sysconf(_SC_PAGESIZE));
1340 if (btrfs_check_nodesize(nodesize, sectorsize))
1342 saved_optind = optind;
1343 dev_cnt = ac - optind;
1347 if (source_dir_set && dev_cnt > 1) {
1349 "The -r option is limited to a single device\n");
1356 if (uuid_parse(fs_uuid, dummy_uuid) != 0) {
1357 fprintf(stderr, "could not parse UUID: %s\n", fs_uuid);
1360 if (!test_uuid_unique(fs_uuid)) {
1361 fprintf(stderr, "non-unique UUID: %s\n", fs_uuid);
1366 while (dev_cnt-- > 0) {
1367 file = av[optind++];
1368 if (is_block_device(file))
1369 if (test_dev_for_mkfs(file, force_overwrite, estr)) {
1370 fprintf(stderr, "Error: %s", estr);
1375 optind = saved_optind;
1376 dev_cnt = ac - optind;
1378 file = av[optind++];
1381 if (is_vol_small(file)) {
1382 printf("SMALL VOLUME: forcing mixed metadata/data groups\n");
1387 * Set default profiles according to number of added devices.
1388 * For mixed groups defaults are single/single.
1391 if (!metadata_profile_opt) {
1392 if (dev_cnt == 1 && ssd)
1393 printf("Detected a SSD, turning off metadata "
1394 "duplication. Mkfs with -m dup if you want to "
1395 "force metadata duplication.\n");
1397 metadata_profile = (dev_cnt > 1) ?
1398 BTRFS_BLOCK_GROUP_RAID1 : (ssd) ?
1399 0: BTRFS_BLOCK_GROUP_DUP;
1401 if (!data_profile_opt) {
1402 data_profile = (dev_cnt > 1) ?
1403 BTRFS_BLOCK_GROUP_RAID0 : 0; /* raid0 or single */
1406 u32 best_nodesize = max_t(u32, sysconf(_SC_PAGESIZE), sectorsize);
1408 if (metadata_profile_opt || data_profile_opt) {
1409 if (metadata_profile != data_profile) {
1411 "ERROR: With mixed block groups data and metadata profiles must be the same\n");
1416 if (!nodesize_forced) {
1417 nodesize = best_nodesize;
1418 if (btrfs_check_nodesize(nodesize, sectorsize))
1421 if (nodesize != sectorsize) {
1422 fprintf(stderr, "Error: mixed metadata/data block groups "
1423 "require metadata blocksizes equal to the sectorsize\n");
1428 /* Check device/block_count after the nodesize is determined */
1429 if (block_count && block_count < btrfs_min_dev_size(nodesize)) {
1431 "Size '%llu' is too small to make a usable filesystem\n",
1434 "Minimum size for btrfs filesystem is %llu\n",
1435 btrfs_min_dev_size(nodesize));
1438 for (i = saved_optind; i < saved_optind + dev_cnt; i++) {
1442 ret = test_minimum_size(path, nodesize);
1444 fprintf(stderr, "Failed to check size for '%s': %s\n",
1445 path, strerror(-ret));
1450 "'%s' is too small to make a usable filesystem\n",
1453 "Minimum size for each btrfs device is %llu.\n",
1454 btrfs_min_dev_size(nodesize));
1458 ret = test_num_disk_vs_raid(metadata_profile, data_profile,
1459 dev_cnt, mixed, estr);
1461 fprintf(stderr, "Error: %s\n", estr);
1465 /* if we are here that means all devs are good to btrfsify */
1466 printf("%s\n", PACKAGE_STRING);
1467 printf("See %s for more information.\n\n", PACKAGE_URL);
1471 if (!source_dir_set) {
1473 * open without O_EXCL so that the problem should not
1474 * occur by the following processing.
1475 * (btrfs_register_one_device() fails if O_EXCL is on)
1477 fd = open(file, O_RDWR);
1479 fprintf(stderr, "unable to open %s: %s\n", file,
1484 ret = btrfs_prepare_device(fd, file, zero_end, &dev_block_count,
1485 block_count, &mixed, discard);
1490 if (block_count && block_count > dev_block_count) {
1491 fprintf(stderr, "%s is smaller than requested size\n", file);
1495 fd = open_target(file);
1497 fprintf(stderr, "unable to open the %s\n", file);
1502 source_dir_size = size_sourcedir(source_dir, sectorsize,
1503 &num_of_meta_chunks, &size_of_data);
1504 if(block_count < source_dir_size)
1505 block_count = source_dir_size;
1506 ret = zero_output_file(fd, block_count, sectorsize);
1508 fprintf(stderr, "unable to zero the output file\n");
1511 /* our "device" is the new image file */
1512 dev_block_count = block_count;
1515 /* To create the first block group and chunk 0 in make_btrfs */
1516 if (dev_block_count < BTRFS_MKFS_SYSTEM_GROUP_SIZE) {
1517 fprintf(stderr, "device is too small to make filesystem\n");
1521 blocks[0] = BTRFS_SUPER_INFO_OFFSET;
1522 for (i = 1; i < 7; i++) {
1523 blocks[i] = BTRFS_SUPER_INFO_OFFSET + 1024 * 1024 +
1528 * FS features that can be set by other means than -O
1529 * just set the bit here
1532 features |= BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS;
1534 if ((data_profile | metadata_profile) &
1535 (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6)) {
1536 features |= BTRFS_FEATURE_INCOMPAT_RAID56;
1539 process_fs_features(features);
1541 ret = make_btrfs(fd, file, label, fs_uuid, blocks, dev_block_count,
1542 nodesize, sectorsize, stripesize, features);
1544 fprintf(stderr, "error during mkfs: %s\n", strerror(-ret));
1548 root = open_ctree(file, 0, OPEN_CTREE_WRITES);
1550 fprintf(stderr, "Open ctree failed\n");
1554 root->fs_info->alloc_start = alloc_start;
1556 ret = make_root_dir(root, mixed);
1558 fprintf(stderr, "failed to setup the root directory\n");
1562 trans = btrfs_start_transaction(root, 1);
1564 btrfs_register_one_device(file);
1569 while (dev_cnt-- > 0) {
1570 int old_mixed = mixed;
1572 file = av[optind++];
1575 * open without O_EXCL so that the problem should not
1576 * occur by the following processing.
1577 * (btrfs_register_one_device() fails if O_EXCL is on)
1579 fd = open(file, O_RDWR);
1581 fprintf(stderr, "unable to open %s: %s\n", file,
1585 ret = btrfs_device_already_in_root(root, fd,
1586 BTRFS_SUPER_INFO_OFFSET);
1588 fprintf(stderr, "skipping duplicate device %s in FS\n",
1593 ret = btrfs_prepare_device(fd, file, zero_end, &dev_block_count,
1594 block_count, &mixed, discard);
1601 ret = btrfs_add_to_fsid(trans, root, fd, file, dev_block_count,
1602 sectorsize, sectorsize, sectorsize);
1604 btrfs_register_one_device(file);
1608 if (!source_dir_set) {
1609 ret = create_raid_groups(trans, root, data_profile,
1610 data_profile_opt, metadata_profile,
1615 ret = create_data_reloc_tree(trans, root);
1618 printf("fs created label %s on %s\n\tnodesize %u leafsize %u "
1619 "sectorsize %u size %s\n",
1620 label, first_file, nodesize, nodesize, sectorsize,
1621 pretty_size(btrfs_super_total_bytes(root->fs_info->super_copy)));
1623 btrfs_commit_transaction(trans, root);
1625 if (source_dir_set) {
1626 trans = btrfs_start_transaction(root, 1);
1627 ret = create_chunks(trans, root,
1628 num_of_meta_chunks, size_of_data);
1630 btrfs_commit_transaction(trans, root);
1632 ret = make_image(source_dir, root, fd);
1636 ret = close_ctree(root);