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 #define _XOPEN_SOURCE 500
22 #include "kerncompat.h"
24 #include <sys/ioctl.h>
25 #include <sys/mount.h>
29 #include <sys/types.h>
35 #include <uuid/uuid.h>
37 #include <sys/xattr.h>
38 #include <blkid/blkid.h>
43 #include "transaction.h"
47 static u64 index_cnt = 2;
49 #define DEFAULT_MKFS_FEATURES (BTRFS_FEATURE_INCOMPAT_EXTENDED_IREF)
51 #define DEFAULT_MKFS_LEAF_SIZE 16384
53 struct directory_name_entry {
57 struct list_head list;
60 static int make_root_dir(struct btrfs_root *root, int mixed)
62 struct btrfs_trans_handle *trans;
63 struct btrfs_key location;
69 trans = btrfs_start_transaction(root, 1);
70 bytes_used = btrfs_super_bytes_used(root->fs_info->super_copy);
72 root->fs_info->system_allocs = 1;
73 ret = btrfs_make_block_group(trans, root, bytes_used,
74 BTRFS_BLOCK_GROUP_SYSTEM,
75 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
76 0, BTRFS_MKFS_SYSTEM_GROUP_SIZE);
80 ret = btrfs_alloc_chunk(trans, root->fs_info->extent_root,
81 &chunk_start, &chunk_size,
82 BTRFS_BLOCK_GROUP_METADATA |
83 BTRFS_BLOCK_GROUP_DATA);
86 "no space to alloc data/metadata chunk\n");
90 ret = btrfs_make_block_group(trans, root, 0,
91 BTRFS_BLOCK_GROUP_METADATA |
92 BTRFS_BLOCK_GROUP_DATA,
93 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
94 chunk_start, chunk_size);
96 printf("Created a data/metadata chunk of size %llu\n", chunk_size);
98 ret = btrfs_alloc_chunk(trans, root->fs_info->extent_root,
99 &chunk_start, &chunk_size,
100 BTRFS_BLOCK_GROUP_METADATA);
101 if (ret == -ENOSPC) {
102 fprintf(stderr, "no space to alloc metadata chunk\n");
106 ret = btrfs_make_block_group(trans, root, 0,
107 BTRFS_BLOCK_GROUP_METADATA,
108 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
109 chunk_start, chunk_size);
113 root->fs_info->system_allocs = 0;
114 btrfs_commit_transaction(trans, root);
115 trans = btrfs_start_transaction(root, 1);
119 ret = btrfs_alloc_chunk(trans, root->fs_info->extent_root,
120 &chunk_start, &chunk_size,
121 BTRFS_BLOCK_GROUP_DATA);
122 if (ret == -ENOSPC) {
123 fprintf(stderr, "no space to alloc data chunk\n");
127 ret = btrfs_make_block_group(trans, root, 0,
128 BTRFS_BLOCK_GROUP_DATA,
129 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
130 chunk_start, chunk_size);
134 ret = btrfs_make_root_dir(trans, root->fs_info->tree_root,
135 BTRFS_ROOT_TREE_DIR_OBJECTID);
138 ret = btrfs_make_root_dir(trans, root, BTRFS_FIRST_FREE_OBJECTID);
141 memcpy(&location, &root->fs_info->fs_root->root_key, sizeof(location));
142 location.offset = (u64)-1;
143 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
145 btrfs_super_root_dir(root->fs_info->super_copy),
146 &location, BTRFS_FT_DIR, 0);
150 ret = btrfs_insert_inode_ref(trans, root->fs_info->tree_root,
151 "default", 7, location.objectid,
152 BTRFS_ROOT_TREE_DIR_OBJECTID, 0);
156 btrfs_commit_transaction(trans, root);
161 static void __recow_root(struct btrfs_trans_handle *trans,
162 struct btrfs_root *root)
165 struct extent_buffer *tmp;
167 if (trans->transid != btrfs_root_generation(&root->root_item)) {
168 extent_buffer_get(root->node);
169 ret = __btrfs_cow_block(trans, root, root->node,
170 NULL, 0, &tmp, 0, 0);
172 free_extent_buffer(tmp);
176 static void recow_roots(struct btrfs_trans_handle *trans,
177 struct btrfs_root *root)
179 struct btrfs_fs_info *info = root->fs_info;
181 __recow_root(trans, info->fs_root);
182 __recow_root(trans, info->tree_root);
183 __recow_root(trans, info->extent_root);
184 __recow_root(trans, info->chunk_root);
185 __recow_root(trans, info->dev_root);
186 __recow_root(trans, info->csum_root);
189 static int create_one_raid_group(struct btrfs_trans_handle *trans,
190 struct btrfs_root *root, u64 type)
196 ret = btrfs_alloc_chunk(trans, root->fs_info->extent_root,
197 &chunk_start, &chunk_size, type);
198 if (ret == -ENOSPC) {
199 fprintf(stderr, "not enough free space\n");
203 ret = btrfs_make_block_group(trans, root->fs_info->extent_root, 0,
204 type, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
205 chunk_start, chunk_size);
210 static int create_raid_groups(struct btrfs_trans_handle *trans,
211 struct btrfs_root *root, u64 data_profile,
212 int data_profile_opt, u64 metadata_profile,
213 int metadata_profile_opt, int mixed, int ssd)
215 u64 num_devices = btrfs_super_num_devices(root->fs_info->super_copy);
218 if (metadata_profile) {
219 u64 meta_flags = BTRFS_BLOCK_GROUP_METADATA;
221 ret = create_one_raid_group(trans, root,
222 BTRFS_BLOCK_GROUP_SYSTEM |
227 meta_flags |= BTRFS_BLOCK_GROUP_DATA;
229 ret = create_one_raid_group(trans, root, meta_flags |
234 if (!mixed && num_devices > 1 && data_profile) {
235 ret = create_one_raid_group(trans, root,
236 BTRFS_BLOCK_GROUP_DATA |
240 recow_roots(trans, root);
245 static int create_data_reloc_tree(struct btrfs_trans_handle *trans,
246 struct btrfs_root *root)
248 struct btrfs_key location;
249 struct btrfs_root_item root_item;
250 struct extent_buffer *tmp;
251 u64 objectid = BTRFS_DATA_RELOC_TREE_OBJECTID;
254 ret = btrfs_copy_root(trans, root, root->node, &tmp, objectid);
257 memcpy(&root_item, &root->root_item, sizeof(root_item));
258 btrfs_set_root_bytenr(&root_item, tmp->start);
259 btrfs_set_root_level(&root_item, btrfs_header_level(tmp));
260 btrfs_set_root_generation(&root_item, trans->transid);
261 free_extent_buffer(tmp);
263 location.objectid = objectid;
264 location.type = BTRFS_ROOT_ITEM_KEY;
266 ret = btrfs_insert_root(trans, root->fs_info->tree_root,
267 &location, &root_item);
273 static void print_usage(void) __attribute__((noreturn));
274 static void print_usage(void)
276 fprintf(stderr, "usage: mkfs.btrfs [options] dev [ dev ... ]\n");
277 fprintf(stderr, "options:\n");
278 fprintf(stderr, "\t -A --alloc-start the offset to start the FS\n");
279 fprintf(stderr, "\t -b --byte-count total number of bytes in the FS\n");
280 fprintf(stderr, "\t -d --data data profile, raid0, raid1, raid5, raid6, raid10, dup or single\n");
281 fprintf(stderr, "\t -f --force force overwrite of existing filesystem\n");
282 fprintf(stderr, "\t -l --leafsize size of btree leaves\n");
283 fprintf(stderr, "\t -L --label set a label\n");
284 fprintf(stderr, "\t -m --metadata metadata profile, values like data profile\n");
285 fprintf(stderr, "\t -M --mixed mix metadata and data together\n");
286 fprintf(stderr, "\t -n --nodesize size of btree nodes\n");
287 fprintf(stderr, "\t -s --sectorsize min block allocation (may not mountable by current kernel)\n");
288 fprintf(stderr, "\t -r --rootdir the source directory\n");
289 fprintf(stderr, "\t -K --nodiscard do not perform whole device TRIM\n");
290 fprintf(stderr, "\t -O --features comma separated list of filesystem features\n");
291 fprintf(stderr, "\t -V --version print the mkfs.btrfs version and exit\n");
292 fprintf(stderr, "%s\n", BTRFS_BUILD_VERSION);
296 static void print_version(void) __attribute__((noreturn));
297 static void print_version(void)
299 fprintf(stderr, "mkfs.btrfs, part of %s\n", BTRFS_BUILD_VERSION);
303 static u64 parse_profile(char *s)
305 if (strcmp(s, "raid0") == 0) {
306 return BTRFS_BLOCK_GROUP_RAID0;
307 } else if (strcmp(s, "raid1") == 0) {
308 return BTRFS_BLOCK_GROUP_RAID1;
309 } else if (strcmp(s, "raid5") == 0) {
310 return BTRFS_BLOCK_GROUP_RAID5;
311 } else if (strcmp(s, "raid6") == 0) {
312 return BTRFS_BLOCK_GROUP_RAID6;
313 } else if (strcmp(s, "raid10") == 0) {
314 return BTRFS_BLOCK_GROUP_RAID10;
315 } else if (strcmp(s, "dup") == 0) {
316 return BTRFS_BLOCK_GROUP_DUP;
317 } else if (strcmp(s, "single") == 0) {
320 fprintf(stderr, "Unknown profile %s\n", s);
327 static char *parse_label(char *input)
329 int len = strlen(input);
331 if (len >= BTRFS_LABEL_SIZE) {
332 fprintf(stderr, "Label %s is too long (max %d)\n", input,
333 BTRFS_LABEL_SIZE - 1);
336 return strdup(input);
339 static struct option long_options[] = {
340 { "alloc-start", 1, NULL, 'A'},
341 { "byte-count", 1, NULL, 'b' },
342 { "force", 0, NULL, 'f' },
343 { "leafsize", 1, NULL, 'l' },
344 { "label", 1, NULL, 'L'},
345 { "metadata", 1, NULL, 'm' },
346 { "mixed", 0, NULL, 'M' },
347 { "nodesize", 1, NULL, 'n' },
348 { "sectorsize", 1, NULL, 's' },
349 { "data", 1, NULL, 'd' },
350 { "version", 0, NULL, 'V' },
351 { "rootdir", 1, NULL, 'r' },
352 { "nodiscard", 0, NULL, 'K' },
353 { "features", 0, NULL, 'O' },
357 static int add_directory_items(struct btrfs_trans_handle *trans,
358 struct btrfs_root *root, u64 objectid,
359 ino_t parent_inum, const char *name,
360 struct stat *st, int *dir_index_cnt)
364 struct btrfs_key location;
367 name_len = strlen(name);
369 location.objectid = objectid;
371 btrfs_set_key_type(&location, BTRFS_INODE_ITEM_KEY);
373 if (S_ISDIR(st->st_mode))
374 filetype = BTRFS_FT_DIR;
375 if (S_ISREG(st->st_mode))
376 filetype = BTRFS_FT_REG_FILE;
377 if (S_ISLNK(st->st_mode))
378 filetype = BTRFS_FT_SYMLINK;
380 ret = btrfs_insert_dir_item(trans, root, name, name_len,
381 parent_inum, &location,
382 filetype, index_cnt);
385 ret = btrfs_insert_inode_ref(trans, root, name, name_len,
386 objectid, parent_inum, index_cnt);
387 *dir_index_cnt = index_cnt;
393 static int fill_inode_item(struct btrfs_trans_handle *trans,
394 struct btrfs_root *root,
395 struct btrfs_inode_item *dst, struct stat *src)
398 u64 sectorsize = root->sectorsize;
401 * btrfs_inode_item has some reserved fields
402 * and represents on-disk inode entry, so
403 * zero everything to prevent information leak
405 memset(dst, 0, sizeof (*dst));
407 btrfs_set_stack_inode_generation(dst, trans->transid);
408 btrfs_set_stack_inode_size(dst, src->st_size);
409 btrfs_set_stack_inode_nbytes(dst, 0);
410 btrfs_set_stack_inode_block_group(dst, 0);
411 btrfs_set_stack_inode_nlink(dst, src->st_nlink);
412 btrfs_set_stack_inode_uid(dst, src->st_uid);
413 btrfs_set_stack_inode_gid(dst, src->st_gid);
414 btrfs_set_stack_inode_mode(dst, src->st_mode);
415 btrfs_set_stack_inode_rdev(dst, 0);
416 btrfs_set_stack_inode_flags(dst, 0);
417 btrfs_set_stack_timespec_sec(&dst->atime, src->st_atime);
418 btrfs_set_stack_timespec_nsec(&dst->atime, 0);
419 btrfs_set_stack_timespec_sec(&dst->ctime, src->st_ctime);
420 btrfs_set_stack_timespec_nsec(&dst->ctime, 0);
421 btrfs_set_stack_timespec_sec(&dst->mtime, src->st_mtime);
422 btrfs_set_stack_timespec_nsec(&dst->mtime, 0);
423 btrfs_set_stack_timespec_sec(&dst->otime, 0);
424 btrfs_set_stack_timespec_nsec(&dst->otime, 0);
426 if (S_ISDIR(src->st_mode)) {
427 btrfs_set_stack_inode_size(dst, 0);
428 btrfs_set_stack_inode_nlink(dst, 1);
430 if (S_ISREG(src->st_mode)) {
431 btrfs_set_stack_inode_size(dst, (u64)src->st_size);
432 if (src->st_size <= BTRFS_MAX_INLINE_DATA_SIZE(root))
433 btrfs_set_stack_inode_nbytes(dst, src->st_size);
435 blocks = src->st_size / sectorsize;
436 if (src->st_size % sectorsize)
438 blocks *= sectorsize;
439 btrfs_set_stack_inode_nbytes(dst, blocks);
442 if (S_ISLNK(src->st_mode))
443 btrfs_set_stack_inode_nbytes(dst, src->st_size + 1);
448 static int directory_select(const struct direct *entry)
450 if ((strncmp(entry->d_name, ".", entry->d_reclen) == 0) ||
451 (strncmp(entry->d_name, "..", entry->d_reclen) == 0))
457 static void free_namelist(struct direct **files, int count)
464 for (i = 0; i < count; ++i)
469 static u64 calculate_dir_inode_size(char *dirname)
472 struct direct **files, *cur_file;
473 u64 dir_inode_size = 0;
475 count = scandir(dirname, &files, directory_select, NULL);
477 for (i = 0; i < count; i++) {
479 dir_inode_size += strlen(cur_file->d_name);
482 free_namelist(files, count);
485 return dir_inode_size;
488 static int add_inode_items(struct btrfs_trans_handle *trans,
489 struct btrfs_root *root,
490 struct stat *st, char *name,
491 u64 self_objectid, ino_t parent_inum,
492 int dir_index_cnt, struct btrfs_inode_item *inode_ret)
495 struct btrfs_key inode_key;
496 struct btrfs_inode_item btrfs_inode;
500 fill_inode_item(trans, root, &btrfs_inode, st);
501 objectid = self_objectid;
503 if (S_ISDIR(st->st_mode)) {
504 inode_size = calculate_dir_inode_size(name);
505 btrfs_set_stack_inode_size(&btrfs_inode, inode_size);
508 inode_key.objectid = objectid;
509 inode_key.offset = 0;
510 btrfs_set_key_type(&inode_key, BTRFS_INODE_ITEM_KEY);
512 ret = btrfs_insert_inode(trans, root, objectid, &btrfs_inode);
514 *inode_ret = btrfs_inode;
518 static int add_xattr_item(struct btrfs_trans_handle *trans,
519 struct btrfs_root *root, u64 objectid,
520 const char *file_name)
524 char xattr_list[XATTR_LIST_MAX];
526 char cur_value[XATTR_SIZE_MAX];
527 char delimiter = '\0';
528 char *next_location = xattr_list;
530 ret = llistxattr(file_name, xattr_list, XATTR_LIST_MAX);
534 fprintf(stderr, "get a list of xattr failed for %s\n",
541 cur_name = strtok(xattr_list, &delimiter);
542 while (cur_name != NULL) {
543 cur_name_len = strlen(cur_name);
544 next_location += cur_name_len + 1;
546 ret = getxattr(file_name, cur_name, cur_value, XATTR_SIZE_MAX);
550 fprintf(stderr, "get a xattr value failed for %s attr %s\n",
551 file_name, cur_name);
555 ret = btrfs_insert_xattr_item(trans, root, cur_name,
556 cur_name_len, cur_value,
559 fprintf(stderr, "insert a xattr item failed for %s\n",
563 cur_name = strtok(next_location, &delimiter);
569 static int add_symbolic_link(struct btrfs_trans_handle *trans,
570 struct btrfs_root *root,
571 u64 objectid, const char *path_name)
574 u64 sectorsize = root->sectorsize;
575 char *buf = malloc(sectorsize);
577 ret = readlink(path_name, buf, sectorsize);
579 fprintf(stderr, "readlink failed for %s\n", path_name);
582 if (ret >= sectorsize) {
583 fprintf(stderr, "symlink too long for %s", path_name);
588 buf[ret] = '\0'; /* readlink does not do it for us */
589 ret = btrfs_insert_inline_extent(trans, root, objectid, 0,
596 static int add_file_items(struct btrfs_trans_handle *trans,
597 struct btrfs_root *root,
598 struct btrfs_inode_item *btrfs_inode, u64 objectid,
599 ino_t parent_inum, struct stat *st,
600 const char *path_name, int out_fd)
605 struct btrfs_key key;
607 u32 sectorsize = root->sectorsize;
612 struct extent_buffer *eb = NULL;
615 if (st->st_size == 0)
618 fd = open(path_name, O_RDONLY);
620 fprintf(stderr, "%s open failed\n", path_name);
624 blocks = st->st_size / sectorsize;
625 if (st->st_size % sectorsize)
628 if (st->st_size <= BTRFS_MAX_INLINE_DATA_SIZE(root)) {
629 char *buffer = malloc(st->st_size);
630 ret_read = pread64(fd, buffer, st->st_size, bytes_read);
631 if (ret_read == -1) {
632 fprintf(stderr, "%s read failed\n", path_name);
637 ret = btrfs_insert_inline_extent(trans, root, objectid, 0,
638 buffer, st->st_size);
643 /* round up our st_size to the FS blocksize */
644 total_bytes = (u64)blocks * sectorsize;
647 * do our IO in extent buffers so it can work
648 * against any raid type
650 eb = malloc(sizeof(*eb) + sectorsize);
655 memset(eb, 0, sizeof(*eb) + sectorsize);
660 * keep our extent size at 1MB max, this makes it easier to work inside
661 * the tiny block groups created during mkfs
663 cur_bytes = min(total_bytes, 1024ULL * 1024);
664 ret = btrfs_reserve_extent(trans, root, cur_bytes, 0, 0, (u64)-1,
669 first_block = key.objectid;
672 while (bytes_read < cur_bytes) {
674 memset(eb->data, 0, sectorsize);
676 ret_read = pread64(fd, eb->data, sectorsize, file_pos + bytes_read);
677 if (ret_read == -1) {
678 fprintf(stderr, "%s read failed\n", path_name);
682 eb->start = first_block + bytes_read;
683 eb->len = sectorsize;
686 * we're doing the csum before we record the extent, but
689 ret = btrfs_csum_file_block(trans, root->fs_info->csum_root,
690 first_block + bytes_read + sectorsize,
691 first_block + bytes_read,
692 eb->data, sectorsize);
696 ret = write_and_map_eb(trans, root, eb);
698 fprintf(stderr, "output file write failed\n");
702 bytes_read += sectorsize;
706 ret = btrfs_record_file_extent(trans, root, objectid, btrfs_inode,
707 file_pos, first_block, cur_bytes);
713 file_pos += cur_bytes;
714 total_bytes -= cur_bytes;
725 static char *make_path(char *dir, char *name)
729 path = malloc(strlen(dir) + strlen(name) + 2);
733 if (dir[strlen(dir) - 1] != '/')
739 static int traverse_directory(struct btrfs_trans_handle *trans,
740 struct btrfs_root *root, char *dir_name,
741 struct directory_name_entry *dir_head, int out_fd)
745 struct btrfs_inode_item cur_inode;
746 struct btrfs_inode_item *inode_item;
747 int count, i, dir_index_cnt;
748 struct direct **files;
750 struct directory_name_entry *dir_entry, *parent_dir_entry;
751 struct direct *cur_file;
752 ino_t parent_inum, cur_inum;
753 ino_t highest_inum = 0;
754 char *parent_dir_name;
755 char real_path[PATH_MAX];
756 struct btrfs_path path;
757 struct extent_buffer *leaf;
758 struct btrfs_key root_dir_key;
759 u64 root_dir_inode_size = 0;
761 /* Add list for source directory */
762 dir_entry = malloc(sizeof(struct directory_name_entry));
763 dir_entry->dir_name = dir_name;
764 dir_entry->path = realpath(dir_name, real_path);
765 if (!dir_entry->path) {
766 fprintf(stderr, "get directory real path error\n");
771 parent_inum = highest_inum + BTRFS_FIRST_FREE_OBJECTID;
772 dir_entry->inum = parent_inum;
773 list_add_tail(&dir_entry->list, &dir_head->list);
775 btrfs_init_path(&path);
777 root_dir_key.objectid = btrfs_root_dirid(&root->root_item);
778 root_dir_key.offset = 0;
779 btrfs_set_key_type(&root_dir_key, BTRFS_INODE_ITEM_KEY);
780 ret = btrfs_lookup_inode(trans, root, &path, &root_dir_key, 1);
782 fprintf(stderr, "root dir lookup error\n");
786 leaf = path.nodes[0];
787 inode_item = btrfs_item_ptr(leaf, path.slots[0],
788 struct btrfs_inode_item);
790 root_dir_inode_size = calculate_dir_inode_size(dir_name);
791 btrfs_set_inode_size(leaf, inode_item, root_dir_inode_size);
792 btrfs_mark_buffer_dirty(leaf);
794 btrfs_release_path(&path);
797 parent_dir_entry = list_entry(dir_head->list.next,
798 struct directory_name_entry,
800 list_del(&parent_dir_entry->list);
802 parent_inum = parent_dir_entry->inum;
803 parent_dir_name = parent_dir_entry->dir_name;
804 if (chdir(parent_dir_entry->path)) {
805 fprintf(stderr, "chdir error for %s\n",
811 count = scandir(parent_dir_entry->path, &files,
812 directory_select, NULL);
815 fprintf(stderr, "scandir for %s failed: %s\n",
816 parent_dir_name, strerror (errno));
821 for (i = 0; i < count; i++) {
824 if (lstat(cur_file->d_name, &st) == -1) {
825 fprintf(stderr, "lstat failed for file %s\n",
831 cur_inum = st.st_ino;
832 ret = add_directory_items(trans, root,
833 cur_inum, parent_inum,
835 &st, &dir_index_cnt);
837 fprintf(stderr, "add_directory_items failed\n");
841 ret = add_inode_items(trans, root, &st,
842 cur_file->d_name, cur_inum,
843 parent_inum, dir_index_cnt,
845 if (ret == -EEXIST) {
846 BUG_ON(st.st_nlink <= 1);
850 fprintf(stderr, "add_inode_items failed\n");
854 ret = add_xattr_item(trans, root,
855 cur_inum, cur_file->d_name);
857 fprintf(stderr, "add_xattr_item failed\n");
862 if (S_ISDIR(st.st_mode)) {
863 dir_entry = malloc(sizeof(struct directory_name_entry));
864 dir_entry->dir_name = cur_file->d_name;
865 dir_entry->path = make_path(parent_dir_entry->path,
867 dir_entry->inum = cur_inum;
868 list_add_tail(&dir_entry->list, &dir_head->list);
869 } else if (S_ISREG(st.st_mode)) {
870 ret = add_file_items(trans, root, &cur_inode,
871 cur_inum, parent_inum, &st,
872 cur_file->d_name, out_fd);
874 fprintf(stderr, "add_file_items failed\n");
877 } else if (S_ISLNK(st.st_mode)) {
878 ret = add_symbolic_link(trans, root,
879 cur_inum, cur_file->d_name);
881 fprintf(stderr, "add_symbolic_link failed\n");
887 free_namelist(files, count);
888 free(parent_dir_entry);
892 } while (!list_empty(&dir_head->list));
897 free_namelist(files, count);
899 free(parent_dir_entry);
906 static int open_target(char *output_name)
909 output_fd = open(output_name, O_CREAT | O_RDWR | O_TRUNC,
910 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH);
915 static int create_chunks(struct btrfs_trans_handle *trans,
916 struct btrfs_root *root, u64 num_of_meta_chunks,
921 u64 meta_type = BTRFS_BLOCK_GROUP_METADATA;
922 u64 data_type = BTRFS_BLOCK_GROUP_DATA;
923 u64 minimum_data_chunk_size = 8 * 1024 * 1024;
927 for (i = 0; i < num_of_meta_chunks; i++) {
928 ret = btrfs_alloc_chunk(trans, root->fs_info->extent_root,
929 &chunk_start, &chunk_size, meta_type);
931 ret = btrfs_make_block_group(trans, root->fs_info->extent_root, 0,
932 meta_type, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
933 chunk_start, chunk_size);
935 set_extent_dirty(&root->fs_info->free_space_cache,
936 chunk_start, chunk_start + chunk_size - 1, 0);
939 if (size_of_data < minimum_data_chunk_size)
940 size_of_data = minimum_data_chunk_size;
942 ret = btrfs_alloc_data_chunk(trans, root->fs_info->extent_root,
943 &chunk_start, size_of_data, data_type);
945 ret = btrfs_make_block_group(trans, root->fs_info->extent_root, 0,
946 data_type, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
947 chunk_start, size_of_data);
949 set_extent_dirty(&root->fs_info->free_space_cache,
950 chunk_start, chunk_start + size_of_data - 1, 0);
954 static int make_image(char *source_dir, struct btrfs_root *root, int out_fd)
957 struct btrfs_trans_handle *trans;
961 struct directory_name_entry dir_head;
963 struct directory_name_entry *dir_entry = NULL;
965 ret = lstat(source_dir, &root_st);
967 fprintf(stderr, "unable to lstat the %s\n", source_dir);
971 INIT_LIST_HEAD(&dir_head.list);
973 trans = btrfs_start_transaction(root, 1);
974 ret = traverse_directory(trans, root, source_dir, &dir_head, out_fd);
976 fprintf(stderr, "unable to traverse_directory\n");
979 btrfs_commit_transaction(trans, root);
981 printf("Making image is completed.\n");
984 while (!list_empty(&dir_head.list)) {
985 dir_entry = list_entry(dir_head.list.next,
986 struct directory_name_entry, list);
987 list_del(&dir_entry->list);
991 fprintf(stderr, "Making image is aborted.\n");
996 * This ignores symlinks with unreadable targets and subdirs that can't
997 * be read. It's a best-effort to give a rough estimate of the size of
998 * a subdir. It doesn't guarantee that prepopulating btrfs from this
999 * tree won't still run out of space.
1001 * The rounding up to 4096 is questionable. Previous code used du -B 4096.
1003 static u64 global_total_size;
1004 static int ftw_add_entry_size(const char *fpath, const struct stat *st,
1007 if (type == FTW_F || type == FTW_D)
1008 global_total_size += round_up(st->st_size, 4096);
1013 static u64 size_sourcedir(char *dir_name, u64 sectorsize,
1014 u64 *num_of_meta_chunks_ret, u64 *size_of_data_ret)
1019 u64 default_chunk_size = 8 * 1024 * 1024; /* 8MB */
1020 u64 allocated_meta_size = 8 * 1024 * 1024; /* 8MB */
1021 u64 allocated_total_size = 20 * 1024 * 1024; /* 20MB */
1022 u64 num_of_meta_chunks = 0;
1023 u64 num_of_data_chunks = 0;
1024 u64 num_of_allocated_meta_chunks =
1025 allocated_meta_size / default_chunk_size;
1027 global_total_size = 0;
1028 ret = ftw(dir_name, ftw_add_entry_size, 10);
1029 dir_size = global_total_size;
1031 fprintf(stderr, "ftw subdir walk of '%s' failed: %s\n",
1032 dir_name, strerror(errno));
1036 num_of_data_chunks = (dir_size + default_chunk_size - 1) /
1039 num_of_meta_chunks = (dir_size / 2) / default_chunk_size;
1040 if (((dir_size / 2) % default_chunk_size) != 0)
1041 num_of_meta_chunks++;
1042 if (num_of_meta_chunks <= num_of_allocated_meta_chunks)
1043 num_of_meta_chunks = 0;
1045 num_of_meta_chunks -= num_of_allocated_meta_chunks;
1047 total_size = allocated_total_size +
1048 (num_of_data_chunks * default_chunk_size) +
1049 (num_of_meta_chunks * default_chunk_size);
1051 *num_of_meta_chunks_ret = num_of_meta_chunks;
1052 *size_of_data_ret = num_of_data_chunks * default_chunk_size;
1056 static int zero_output_file(int out_fd, u64 size, u32 sectorsize)
1058 int len = sectorsize;
1059 int loop_num = size / sectorsize;
1061 char *buf = malloc(len);
1067 memset(buf, 0, len);
1068 for (i = 0; i < loop_num; i++) {
1069 written = pwrite64(out_fd, buf, len, location);
1072 location += sectorsize;
1078 static int check_leaf_or_node_size(u32 size, u32 sectorsize)
1080 if (size < sectorsize) {
1082 "Illegal leafsize (or nodesize) %u (smaller than %u)\n",
1085 } else if (size > BTRFS_MAX_METADATA_BLOCKSIZE) {
1087 "Illegal leafsize (or nodesize) %u (larger than %u)\n",
1088 size, BTRFS_MAX_METADATA_BLOCKSIZE);
1090 } else if (size & (sectorsize - 1)) {
1092 "Illegal leafsize (or nodesize) %u (not align to %u)\n",
1099 static int is_ssd(const char *file)
1103 char sysfs_path[PATH_MAX];
1109 probe = blkid_new_probe_from_filename(file);
1113 /* Device number of this disk (possibly a partition) */
1114 devno = blkid_probe_get_devno(probe);
1116 blkid_free_probe(probe);
1120 /* Get whole disk name (not full path) for this devno */
1121 ret = blkid_devno_to_wholedisk(devno,
1122 wholedisk, sizeof(wholedisk), NULL);
1124 blkid_free_probe(probe);
1128 snprintf(sysfs_path, PATH_MAX, "/sys/block/%s/queue/rotational",
1131 blkid_free_probe(probe);
1133 fd = open(sysfs_path, O_RDONLY);
1138 if (read(fd, &rotational, sizeof(char)) < sizeof(char)) {
1144 return !atoi((const char *)&rotational);
1147 #define BTRFS_FEATURE_LIST_ALL (1ULL << 63)
1149 static const struct btrfs_fs_feature {
1153 } mkfs_features[] = {
1154 { "mixed-bg", BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS,
1155 "mixed data and metadata block groups" },
1156 { "extref", BTRFS_FEATURE_INCOMPAT_EXTENDED_IREF,
1157 "increased hardlink limit per file to 65536" },
1158 { "raid56", BTRFS_FEATURE_INCOMPAT_RAID56,
1159 "raid56 extended format" },
1160 { "skinny-metadata", BTRFS_FEATURE_INCOMPAT_SKINNY_METADATA,
1161 "reduced-size metadata extent refs" },
1162 { "no-holes", BTRFS_FEATURE_INCOMPAT_NO_HOLES,
1163 "no explicit hole extents for files" },
1164 /* Keep this one last */
1165 { "list-all", BTRFS_FEATURE_LIST_ALL, NULL }
1168 static void list_all_fs_features(void)
1172 fprintf(stderr, "Filesystem features available at mkfs time:\n");
1173 for (i = 0; i < ARRAY_SIZE(mkfs_features) - 1; i++) {
1174 char *is_default = "";
1176 if (mkfs_features[i].flag & DEFAULT_MKFS_FEATURES)
1177 is_default = ", default";
1178 fprintf(stderr, "%-20s- %s (0x%llx%s)\n",
1179 mkfs_features[i].name,
1180 mkfs_features[i].desc,
1181 mkfs_features[i].flag,
1186 static int parse_one_fs_feature(const char *name, u64 *flags)
1191 for (i = 0; i < ARRAY_SIZE(mkfs_features); i++) {
1192 if (name[0] == '^' &&
1193 !strcmp(mkfs_features[i].name, name + 1)) {
1194 *flags &= ~ mkfs_features[i].flag;
1196 } else if (!strcmp(mkfs_features[i].name, name)) {
1197 *flags |= mkfs_features[i].flag;
1205 static void process_fs_features(u64 flags)
1209 for (i = 0; i < ARRAY_SIZE(mkfs_features); i++) {
1210 if (flags & mkfs_features[i].flag) {
1211 printf("Turning ON incompat feature '%s': %s\n",
1212 mkfs_features[i].name,
1213 mkfs_features[i].desc);
1220 * Return NULL if all features were parsed fine, otherwise return the name of
1221 * the first unparsed.
1223 static char* parse_fs_features(char *namelist, u64 *flags)
1226 char *save_ptr = NULL; /* Satisfy static checkers */
1228 for (this_char = strtok_r(namelist, ",", &save_ptr);
1230 this_char = strtok_r(NULL, ",", &save_ptr)) {
1231 if (parse_one_fs_feature(this_char, flags))
1238 int main(int ac, char **av)
1241 struct btrfs_root *root;
1242 struct btrfs_trans_handle *trans;
1245 u64 block_count = 0;
1246 u64 dev_block_count = 0;
1248 u64 alloc_start = 0;
1249 u64 metadata_profile = 0;
1250 u64 data_profile = 0;
1251 u32 leafsize = max_t(u32, sysconf(_SC_PAGESIZE), DEFAULT_MKFS_LEAF_SIZE);
1252 u32 sectorsize = 4096;
1253 u32 nodesize = leafsize;
1254 u32 stripesize = 4096;
1256 int option_index = 0;
1261 int leaf_forced = 0;
1262 int data_profile_opt = 0;
1263 int metadata_profile_opt = 0;
1266 int force_overwrite = 0;
1268 char *source_dir = NULL;
1269 int source_dir_set = 0;
1270 u64 num_of_meta_chunks = 0;
1271 u64 size_of_data = 0;
1272 u64 source_dir_size = 0;
1276 u64 features = DEFAULT_MKFS_FEATURES;
1280 c = getopt_long(ac, av, "A:b:fl:n:s:m:d:L:O:r:VMK",
1281 long_options, &option_index);
1286 alloc_start = parse_size(optarg);
1289 force_overwrite = 1;
1292 data_profile = parse_profile(optarg);
1293 data_profile_opt = 1;
1297 nodesize = parse_size(optarg);
1298 leafsize = parse_size(optarg);
1302 label = parse_label(optarg);
1305 metadata_profile = parse_profile(optarg);
1306 metadata_profile_opt = 1;
1312 char *orig = strdup(optarg);
1315 tmp = parse_fs_features(tmp, &features);
1318 "Unrecognized filesystem feature '%s'\n",
1324 if (features & BTRFS_FEATURE_LIST_ALL) {
1325 list_all_fs_features();
1331 sectorsize = parse_size(optarg);
1334 block_count = parse_size(optarg);
1335 if (block_count <= 1024*1024*1024) {
1336 printf("SMALL VOLUME: forcing mixed "
1337 "metadata/data groups\n");
1346 source_dir = optarg;
1356 sectorsize = max(sectorsize, (u32)sysconf(_SC_PAGESIZE));
1357 if (check_leaf_or_node_size(leafsize, sectorsize))
1359 if (check_leaf_or_node_size(nodesize, sectorsize))
1361 saved_optind = optind;
1362 dev_cnt = ac - optind;
1366 if (source_dir_set && dev_cnt > 1) {
1368 "The -r option is limited to a single device\n");
1371 while (dev_cnt-- > 0) {
1372 file = av[optind++];
1373 if (is_block_device(file))
1374 if (test_dev_for_mkfs(file, force_overwrite, estr)) {
1375 fprintf(stderr, "Error: %s", estr);
1380 optind = saved_optind;
1381 dev_cnt = ac - optind;
1383 file = av[optind++];
1386 if (is_vol_small(file)) {
1387 printf("SMALL VOLUME: forcing mixed metadata/data groups\n");
1392 * Set default profiles according to number of added devices.
1393 * For mixed groups defaults are single/single.
1396 if (!metadata_profile_opt) {
1397 if (dev_cnt == 1 && ssd)
1398 printf("Detected a SSD, turning off metadata "
1399 "duplication. Mkfs with -m dup if you want to "
1400 "force metadata duplication.\n");
1402 metadata_profile = (dev_cnt > 1) ?
1403 BTRFS_BLOCK_GROUP_RAID1 : (ssd) ?
1404 0: BTRFS_BLOCK_GROUP_DUP;
1406 if (!data_profile_opt) {
1407 data_profile = (dev_cnt > 1) ?
1408 BTRFS_BLOCK_GROUP_RAID0 : 0; /* raid0 or single */
1411 u32 best_leafsize = max_t(u32, sysconf(_SC_PAGESIZE), sectorsize);
1413 if (metadata_profile_opt || data_profile_opt) {
1414 if (metadata_profile != data_profile) {
1416 "ERROR: With mixed block groups data and metadata profiles must be the same\n");
1422 leafsize = best_leafsize;
1423 nodesize = best_leafsize;
1424 if (check_leaf_or_node_size(leafsize, sectorsize))
1427 if (leafsize != sectorsize) {
1428 fprintf(stderr, "Error: mixed metadata/data block groups "
1429 "require metadata blocksizes equal to the sectorsize\n");
1434 ret = test_num_disk_vs_raid(metadata_profile, data_profile,
1435 dev_cnt, mixed, estr);
1437 fprintf(stderr, "Error: %s\n", estr);
1441 /* if we are here that means all devs are good to btrfsify */
1442 printf("\nWARNING! - %s IS EXPERIMENTAL\n", BTRFS_BUILD_VERSION);
1443 printf("WARNING! - see http://btrfs.wiki.kernel.org before using\n\n");
1447 if (!source_dir_set) {
1449 * open without O_EXCL so that the problem should not
1450 * occur by the following processing.
1451 * (btrfs_register_one_device() fails if O_EXCL is on)
1453 fd = open(file, O_RDWR);
1455 fprintf(stderr, "unable to open %s: %s\n", file,
1460 ret = btrfs_prepare_device(fd, file, zero_end, &dev_block_count,
1461 block_count, &mixed, discard);
1466 if (block_count && block_count > dev_block_count) {
1467 fprintf(stderr, "%s is smaller than requested size\n", file);
1471 fd = open_target(file);
1473 fprintf(stderr, "unable to open the %s\n", file);
1478 source_dir_size = size_sourcedir(source_dir, sectorsize,
1479 &num_of_meta_chunks, &size_of_data);
1480 if(block_count < source_dir_size)
1481 block_count = source_dir_size;
1482 ret = zero_output_file(fd, block_count, sectorsize);
1484 fprintf(stderr, "unable to zero the output file\n");
1487 /* our "device" is the new image file */
1488 dev_block_count = block_count;
1491 /* To create the first block group and chunk 0 in make_btrfs */
1492 if (dev_block_count < BTRFS_MKFS_SYSTEM_GROUP_SIZE) {
1493 fprintf(stderr, "device is too small to make filesystem\n");
1497 blocks[0] = BTRFS_SUPER_INFO_OFFSET;
1498 for (i = 1; i < 7; i++) {
1499 blocks[i] = BTRFS_SUPER_INFO_OFFSET + 1024 * 1024 +
1504 * FS features that can be set by other means than -O
1505 * just set the bit here
1508 features |= BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS;
1510 if ((data_profile | metadata_profile) &
1511 (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6)) {
1512 features |= BTRFS_FEATURE_INCOMPAT_RAID56;
1515 process_fs_features(features);
1517 ret = make_btrfs(fd, file, label, blocks, dev_block_count,
1519 sectorsize, stripesize, features);
1521 fprintf(stderr, "error during mkfs: %s\n", strerror(-ret));
1525 root = open_ctree(file, 0, OPEN_CTREE_WRITES);
1527 fprintf(stderr, "Open ctree failed\n");
1531 root->fs_info->alloc_start = alloc_start;
1533 ret = make_root_dir(root, mixed);
1535 fprintf(stderr, "failed to setup the root directory\n");
1539 trans = btrfs_start_transaction(root, 1);
1544 btrfs_register_one_device(file);
1546 while (dev_cnt-- > 0) {
1547 int old_mixed = mixed;
1549 file = av[optind++];
1552 * open without O_EXCL so that the problem should not
1553 * occur by the following processing.
1554 * (btrfs_register_one_device() fails if O_EXCL is on)
1556 fd = open(file, O_RDWR);
1558 fprintf(stderr, "unable to open %s: %s\n", file,
1562 ret = btrfs_device_already_in_root(root, fd,
1563 BTRFS_SUPER_INFO_OFFSET);
1565 fprintf(stderr, "skipping duplicate device %s in FS\n",
1570 ret = btrfs_prepare_device(fd, file, zero_end, &dev_block_count,
1571 block_count, &mixed, discard);
1578 ret = btrfs_add_to_fsid(trans, root, fd, file, dev_block_count,
1579 sectorsize, sectorsize, sectorsize);
1581 btrfs_register_one_device(file);
1585 if (!source_dir_set) {
1586 ret = create_raid_groups(trans, root, data_profile,
1587 data_profile_opt, metadata_profile,
1588 metadata_profile_opt, mixed, ssd);
1592 ret = create_data_reloc_tree(trans, root);
1595 printf("fs created label %s on %s\n\tnodesize %u leafsize %u "
1596 "sectorsize %u size %s\n",
1597 label, first_file, nodesize, leafsize, sectorsize,
1598 pretty_size(btrfs_super_total_bytes(root->fs_info->super_copy)));
1600 printf("%s\n", BTRFS_BUILD_VERSION);
1601 btrfs_commit_transaction(trans, root);
1603 if (source_dir_set) {
1604 trans = btrfs_start_transaction(root, 1);
1605 ret = create_chunks(trans, root,
1606 num_of_meta_chunks, size_of_data);
1608 btrfs_commit_transaction(trans, root);
1610 ret = make_image(source_dir, root, fd);
1614 ret = close_ctree(root);