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 <attr/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);
384 *dir_index_cnt = index_cnt;
390 static int fill_inode_item(struct btrfs_trans_handle *trans,
391 struct btrfs_root *root,
392 struct btrfs_inode_item *dst, struct stat *src)
395 u64 sectorsize = root->sectorsize;
398 * btrfs_inode_item has some reserved fields
399 * and represents on-disk inode entry, so
400 * zero everything to prevent information leak
402 memset(dst, 0, sizeof (*dst));
404 btrfs_set_stack_inode_generation(dst, trans->transid);
405 btrfs_set_stack_inode_size(dst, src->st_size);
406 btrfs_set_stack_inode_nbytes(dst, 0);
407 btrfs_set_stack_inode_block_group(dst, 0);
408 btrfs_set_stack_inode_nlink(dst, src->st_nlink);
409 btrfs_set_stack_inode_uid(dst, src->st_uid);
410 btrfs_set_stack_inode_gid(dst, src->st_gid);
411 btrfs_set_stack_inode_mode(dst, src->st_mode);
412 btrfs_set_stack_inode_rdev(dst, 0);
413 btrfs_set_stack_inode_flags(dst, 0);
414 btrfs_set_stack_timespec_sec(&dst->atime, src->st_atime);
415 btrfs_set_stack_timespec_nsec(&dst->atime, 0);
416 btrfs_set_stack_timespec_sec(&dst->ctime, src->st_ctime);
417 btrfs_set_stack_timespec_nsec(&dst->ctime, 0);
418 btrfs_set_stack_timespec_sec(&dst->mtime, src->st_mtime);
419 btrfs_set_stack_timespec_nsec(&dst->mtime, 0);
420 btrfs_set_stack_timespec_sec(&dst->otime, 0);
421 btrfs_set_stack_timespec_nsec(&dst->otime, 0);
423 if (S_ISDIR(src->st_mode)) {
424 btrfs_set_stack_inode_size(dst, 0);
425 btrfs_set_stack_inode_nlink(dst, 1);
427 if (S_ISREG(src->st_mode)) {
428 btrfs_set_stack_inode_size(dst, (u64)src->st_size);
429 if (src->st_size <= BTRFS_MAX_INLINE_DATA_SIZE(root))
430 btrfs_set_stack_inode_nbytes(dst, src->st_size);
432 blocks = src->st_size / sectorsize;
433 if (src->st_size % sectorsize)
435 blocks *= sectorsize;
436 btrfs_set_stack_inode_nbytes(dst, blocks);
439 if (S_ISLNK(src->st_mode))
440 btrfs_set_stack_inode_nbytes(dst, src->st_size + 1);
445 static int directory_select(const struct direct *entry)
447 if ((strncmp(entry->d_name, ".", entry->d_reclen) == 0) ||
448 (strncmp(entry->d_name, "..", entry->d_reclen) == 0))
454 static void free_namelist(struct direct **files, int count)
461 for (i = 0; i < count; ++i)
466 static u64 calculate_dir_inode_size(char *dirname)
469 struct direct **files, *cur_file;
470 u64 dir_inode_size = 0;
472 count = scandir(dirname, &files, directory_select, NULL);
474 for (i = 0; i < count; i++) {
476 dir_inode_size += strlen(cur_file->d_name);
479 free_namelist(files, count);
482 return dir_inode_size;
485 static int add_inode_items(struct btrfs_trans_handle *trans,
486 struct btrfs_root *root,
487 struct stat *st, char *name,
488 u64 self_objectid, ino_t parent_inum,
489 int dir_index_cnt, struct btrfs_inode_item *inode_ret)
492 struct btrfs_key inode_key;
493 struct btrfs_inode_item btrfs_inode;
498 name_len = strlen(name);
499 fill_inode_item(trans, root, &btrfs_inode, st);
500 objectid = self_objectid;
502 if (S_ISDIR(st->st_mode)) {
503 inode_size = calculate_dir_inode_size(name);
504 btrfs_set_stack_inode_size(&btrfs_inode, inode_size);
507 inode_key.objectid = objectid;
508 inode_key.offset = 0;
509 btrfs_set_key_type(&inode_key, BTRFS_INODE_ITEM_KEY);
511 ret = btrfs_insert_inode(trans, root, objectid, &btrfs_inode);
515 ret = btrfs_insert_inode_ref(trans, root, name, name_len,
516 objectid, parent_inum, dir_index_cnt);
520 *inode_ret = btrfs_inode;
525 static int add_xattr_item(struct btrfs_trans_handle *trans,
526 struct btrfs_root *root, u64 objectid,
527 const char *file_name)
531 char xattr_list[XATTR_LIST_MAX];
533 char cur_value[XATTR_SIZE_MAX];
534 char delimiter = '\0';
535 char *next_location = xattr_list;
537 ret = llistxattr(file_name, xattr_list, XATTR_LIST_MAX);
541 fprintf(stderr, "get a list of xattr failed for %s\n",
548 cur_name = strtok(xattr_list, &delimiter);
549 while (cur_name != NULL) {
550 cur_name_len = strlen(cur_name);
551 next_location += cur_name_len + 1;
553 ret = getxattr(file_name, cur_name, cur_value, XATTR_SIZE_MAX);
557 fprintf(stderr, "get a xattr value failed for %s attr %s\n",
558 file_name, cur_name);
562 ret = btrfs_insert_xattr_item(trans, root, cur_name,
563 cur_name_len, cur_value,
566 fprintf(stderr, "insert a xattr item failed for %s\n",
570 cur_name = strtok(next_location, &delimiter);
576 static int add_symbolic_link(struct btrfs_trans_handle *trans,
577 struct btrfs_root *root,
578 u64 objectid, const char *path_name)
581 u64 sectorsize = root->sectorsize;
582 char *buf = malloc(sectorsize);
584 ret = readlink(path_name, buf, sectorsize);
586 fprintf(stderr, "readlink failed for %s\n", path_name);
589 if (ret >= sectorsize) {
590 fprintf(stderr, "symlink too long for %s", path_name);
595 buf[ret] = '\0'; /* readlink does not do it for us */
596 ret = btrfs_insert_inline_extent(trans, root, objectid, 0,
603 static int add_file_items(struct btrfs_trans_handle *trans,
604 struct btrfs_root *root,
605 struct btrfs_inode_item *btrfs_inode, u64 objectid,
606 ino_t parent_inum, struct stat *st,
607 const char *path_name, int out_fd)
612 struct btrfs_key key;
614 u32 sectorsize = root->sectorsize;
619 struct extent_buffer *eb = NULL;
622 fd = open(path_name, O_RDONLY);
624 fprintf(stderr, "%s open failed\n", path_name);
628 blocks = st->st_size / sectorsize;
629 if (st->st_size % sectorsize)
632 if (st->st_size <= BTRFS_MAX_INLINE_DATA_SIZE(root)) {
633 char *buffer = malloc(st->st_size);
634 ret_read = pread64(fd, buffer, st->st_size, bytes_read);
635 if (ret_read == -1) {
636 fprintf(stderr, "%s read failed\n", path_name);
641 ret = btrfs_insert_inline_extent(trans, root, objectid, 0,
642 buffer, st->st_size);
647 /* round up our st_size to the FS blocksize */
648 total_bytes = (u64)blocks * sectorsize;
651 * do our IO in extent buffers so it can work
652 * against any raid type
654 eb = malloc(sizeof(*eb) + sectorsize);
659 memset(eb, 0, sizeof(*eb) + sectorsize);
664 * keep our extent size at 1MB max, this makes it easier to work inside
665 * the tiny block groups created during mkfs
667 cur_bytes = min(total_bytes, 1024ULL * 1024);
668 ret = btrfs_reserve_extent(trans, root, cur_bytes, 0, 0, (u64)-1,
673 first_block = key.objectid;
676 while (bytes_read < cur_bytes) {
678 memset(eb->data, 0, sectorsize);
680 ret_read = pread64(fd, eb->data, sectorsize, file_pos + bytes_read);
681 if (ret_read == -1) {
682 fprintf(stderr, "%s read failed\n", path_name);
686 eb->start = first_block + bytes_read;
687 eb->len = sectorsize;
690 * we're doing the csum before we record the extent, but
693 ret = btrfs_csum_file_block(trans, root->fs_info->csum_root,
694 first_block + bytes_read + sectorsize,
695 first_block + bytes_read,
696 eb->data, sectorsize);
700 ret = write_and_map_eb(trans, root, eb);
702 fprintf(stderr, "output file write failed\n");
706 bytes_read += sectorsize;
710 ret = btrfs_record_file_extent(trans, root, objectid, btrfs_inode,
711 file_pos, first_block, cur_bytes);
717 file_pos += cur_bytes;
718 total_bytes -= cur_bytes;
730 static char *make_path(char *dir, char *name)
734 path = malloc(strlen(dir) + strlen(name) + 2);
738 if (dir[strlen(dir) - 1] != '/')
744 static int traverse_directory(struct btrfs_trans_handle *trans,
745 struct btrfs_root *root, char *dir_name,
746 struct directory_name_entry *dir_head, int out_fd)
750 struct btrfs_inode_item cur_inode;
751 struct btrfs_inode_item *inode_item;
752 int count, i, dir_index_cnt;
753 struct direct **files;
755 struct directory_name_entry *dir_entry, *parent_dir_entry;
756 struct direct *cur_file;
757 ino_t parent_inum, cur_inum;
758 ino_t highest_inum = 0;
759 char *parent_dir_name;
760 struct btrfs_path path;
761 struct extent_buffer *leaf;
762 struct btrfs_key root_dir_key;
763 u64 root_dir_inode_size = 0;
765 /* Add list for source directory */
766 dir_entry = malloc(sizeof(struct directory_name_entry));
767 dir_entry->dir_name = dir_name;
768 dir_entry->path = strdup(dir_name);
770 parent_inum = highest_inum + BTRFS_FIRST_FREE_OBJECTID;
771 dir_entry->inum = parent_inum;
772 list_add_tail(&dir_entry->list, &dir_head->list);
774 btrfs_init_path(&path);
776 root_dir_key.objectid = btrfs_root_dirid(&root->root_item);
777 root_dir_key.offset = 0;
778 btrfs_set_key_type(&root_dir_key, BTRFS_INODE_ITEM_KEY);
779 ret = btrfs_lookup_inode(trans, root, &path, &root_dir_key, 1);
781 fprintf(stderr, "root dir lookup error\n");
785 leaf = path.nodes[0];
786 inode_item = btrfs_item_ptr(leaf, path.slots[0],
787 struct btrfs_inode_item);
789 root_dir_inode_size = calculate_dir_inode_size(dir_name);
790 btrfs_set_inode_size(leaf, inode_item, root_dir_inode_size);
791 btrfs_mark_buffer_dirty(leaf);
793 btrfs_release_path(&path);
796 parent_dir_entry = list_entry(dir_head->list.next,
797 struct directory_name_entry,
799 list_del(&parent_dir_entry->list);
801 parent_inum = parent_dir_entry->inum;
802 parent_dir_name = parent_dir_entry->dir_name;
803 if (chdir(parent_dir_entry->path)) {
804 fprintf(stderr, "chdir error for %s\n",
809 count = scandir(parent_dir_entry->path, &files,
810 directory_select, NULL);
813 fprintf(stderr, "scandir for %s failed: %s\n",
814 parent_dir_name, strerror (errno));
818 for (i = 0; i < count; i++) {
821 if (lstat(cur_file->d_name, &st) == -1) {
822 fprintf(stderr, "lstat failed for file %s\n",
827 cur_inum = ++highest_inum + BTRFS_FIRST_FREE_OBJECTID;
828 ret = add_directory_items(trans, root,
829 cur_inum, parent_inum,
831 &st, &dir_index_cnt);
833 fprintf(stderr, "add_directory_items failed\n");
837 ret = add_inode_items(trans, root, &st,
838 cur_file->d_name, cur_inum,
839 parent_inum, dir_index_cnt,
842 fprintf(stderr, "add_inode_items failed\n");
846 ret = add_xattr_item(trans, root,
847 cur_inum, cur_file->d_name);
849 fprintf(stderr, "add_xattr_item failed\n");
854 if (S_ISDIR(st.st_mode)) {
855 dir_entry = malloc(sizeof(struct directory_name_entry));
856 dir_entry->dir_name = cur_file->d_name;
857 dir_entry->path = make_path(parent_dir_entry->path,
859 dir_entry->inum = cur_inum;
860 list_add_tail(&dir_entry->list, &dir_head->list);
861 } else if (S_ISREG(st.st_mode)) {
862 ret = add_file_items(trans, root, &cur_inode,
863 cur_inum, parent_inum, &st,
864 cur_file->d_name, out_fd);
866 fprintf(stderr, "add_file_items failed\n");
869 } else if (S_ISLNK(st.st_mode)) {
870 ret = add_symbolic_link(trans, root,
871 cur_inum, cur_file->d_name);
873 fprintf(stderr, "add_symbolic_link failed\n");
879 free_namelist(files, count);
880 free(parent_dir_entry->path);
881 free(parent_dir_entry);
885 } while (!list_empty(&dir_head->list));
889 free_namelist(files, count);
891 free(parent_dir_entry->path);
892 free(parent_dir_entry);
896 static int open_target(char *output_name)
899 output_fd = open(output_name, O_CREAT | O_RDWR | O_TRUNC,
900 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH);
905 static int create_chunks(struct btrfs_trans_handle *trans,
906 struct btrfs_root *root, u64 num_of_meta_chunks,
911 u64 meta_type = BTRFS_BLOCK_GROUP_METADATA;
912 u64 data_type = BTRFS_BLOCK_GROUP_DATA;
913 u64 minimum_data_chunk_size = 8 * 1024 * 1024;
917 for (i = 0; i < num_of_meta_chunks; i++) {
918 ret = btrfs_alloc_chunk(trans, root->fs_info->extent_root,
919 &chunk_start, &chunk_size, meta_type);
921 ret = btrfs_make_block_group(trans, root->fs_info->extent_root, 0,
922 meta_type, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
923 chunk_start, chunk_size);
925 set_extent_dirty(&root->fs_info->free_space_cache,
926 chunk_start, chunk_start + chunk_size - 1, 0);
929 if (size_of_data < minimum_data_chunk_size)
930 size_of_data = minimum_data_chunk_size;
932 ret = btrfs_alloc_data_chunk(trans, root->fs_info->extent_root,
933 &chunk_start, size_of_data, data_type);
935 ret = btrfs_make_block_group(trans, root->fs_info->extent_root, 0,
936 data_type, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
937 chunk_start, size_of_data);
939 set_extent_dirty(&root->fs_info->free_space_cache,
940 chunk_start, chunk_start + size_of_data - 1, 0);
944 static int make_image(char *source_dir, struct btrfs_root *root, int out_fd)
947 struct btrfs_trans_handle *trans;
951 struct directory_name_entry dir_head;
953 struct directory_name_entry *dir_entry = NULL;
955 ret = lstat(source_dir, &root_st);
957 fprintf(stderr, "unable to lstat the %s\n", source_dir);
961 INIT_LIST_HEAD(&dir_head.list);
963 trans = btrfs_start_transaction(root, 1);
964 ret = traverse_directory(trans, root, source_dir, &dir_head, out_fd);
966 fprintf(stderr, "unable to traverse_directory\n");
969 btrfs_commit_transaction(trans, root);
971 printf("Making image is completed.\n");
974 while (!list_empty(&dir_head.list)) {
975 dir_entry = list_entry(dir_head.list.next,
976 struct directory_name_entry, list);
977 list_del(&dir_entry->list);
980 fprintf(stderr, "Making image is aborted.\n");
985 * This ignores symlinks with unreadable targets and subdirs that can't
986 * be read. It's a best-effort to give a rough estimate of the size of
987 * a subdir. It doesn't guarantee that prepopulating btrfs from this
988 * tree won't still run out of space.
990 * The rounding up to 4096 is questionable. Previous code used du -B 4096.
992 static u64 global_total_size;
993 static int ftw_add_entry_size(const char *fpath, const struct stat *st,
996 if (type == FTW_F || type == FTW_D)
997 global_total_size += round_up(st->st_size, 4096);
1002 static u64 size_sourcedir(char *dir_name, u64 sectorsize,
1003 u64 *num_of_meta_chunks_ret, u64 *size_of_data_ret)
1008 u64 default_chunk_size = 8 * 1024 * 1024; /* 8MB */
1009 u64 allocated_meta_size = 8 * 1024 * 1024; /* 8MB */
1010 u64 allocated_total_size = 20 * 1024 * 1024; /* 20MB */
1011 u64 num_of_meta_chunks = 0;
1012 u64 num_of_data_chunks = 0;
1013 u64 num_of_allocated_meta_chunks =
1014 allocated_meta_size / default_chunk_size;
1016 global_total_size = 0;
1017 ret = ftw(dir_name, ftw_add_entry_size, 10);
1018 dir_size = global_total_size;
1020 fprintf(stderr, "ftw subdir walk of '%s' failed: %s\n",
1021 dir_name, strerror(errno));
1025 num_of_data_chunks = (dir_size + default_chunk_size - 1) /
1028 num_of_meta_chunks = (dir_size / 2) / default_chunk_size;
1029 if (((dir_size / 2) % default_chunk_size) != 0)
1030 num_of_meta_chunks++;
1031 if (num_of_meta_chunks <= num_of_allocated_meta_chunks)
1032 num_of_meta_chunks = 0;
1034 num_of_meta_chunks -= num_of_allocated_meta_chunks;
1036 total_size = allocated_total_size +
1037 (num_of_data_chunks * default_chunk_size) +
1038 (num_of_meta_chunks * default_chunk_size);
1040 *num_of_meta_chunks_ret = num_of_meta_chunks;
1041 *size_of_data_ret = num_of_data_chunks * default_chunk_size;
1045 static int zero_output_file(int out_fd, u64 size, u32 sectorsize)
1047 int len = sectorsize;
1048 int loop_num = size / sectorsize;
1050 char *buf = malloc(len);
1056 memset(buf, 0, len);
1057 for (i = 0; i < loop_num; i++) {
1058 written = pwrite64(out_fd, buf, len, location);
1061 location += sectorsize;
1067 static int check_leaf_or_node_size(u32 size, u32 sectorsize)
1069 if (size < sectorsize) {
1071 "Illegal leafsize (or nodesize) %u (smaller than %u)\n",
1074 } else if (size > BTRFS_MAX_METADATA_BLOCKSIZE) {
1076 "Illegal leafsize (or nodesize) %u (larger than %u)\n",
1077 size, BTRFS_MAX_METADATA_BLOCKSIZE);
1079 } else if (size & (sectorsize - 1)) {
1081 "Illegal leafsize (or nodesize) %u (not align to %u)\n",
1088 static int is_ssd(const char *file)
1092 char sysfs_path[PATH_MAX];
1098 probe = blkid_new_probe_from_filename(file);
1102 /* Device number of this disk (possibly a partition) */
1103 devno = blkid_probe_get_devno(probe);
1105 blkid_free_probe(probe);
1109 /* Get whole disk name (not full path) for this devno */
1110 ret = blkid_devno_to_wholedisk(devno,
1111 wholedisk, sizeof(wholedisk), NULL);
1113 blkid_free_probe(probe);
1117 snprintf(sysfs_path, PATH_MAX, "/sys/block/%s/queue/rotational",
1120 blkid_free_probe(probe);
1122 fd = open(sysfs_path, O_RDONLY);
1127 if (read(fd, &rotational, sizeof(char)) < sizeof(char)) {
1133 return !atoi((const char *)&rotational);
1136 #define BTRFS_FEATURE_LIST_ALL (1ULL << 63)
1138 static const struct btrfs_fs_feature {
1142 } mkfs_features[] = {
1143 { "mixed-bg", BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS,
1144 "mixed data and metadata block groups" },
1145 { "extref", BTRFS_FEATURE_INCOMPAT_EXTENDED_IREF,
1146 "increased hardlink limit per file to 65536" },
1147 { "raid56", BTRFS_FEATURE_INCOMPAT_RAID56,
1148 "raid56 extended format" },
1149 { "skinny-metadata", BTRFS_FEATURE_INCOMPAT_SKINNY_METADATA,
1150 "reduced-size metadata extent refs" },
1151 /* Keep this one last */
1152 { "list-all", BTRFS_FEATURE_LIST_ALL, NULL }
1155 static void list_all_fs_features(void)
1159 fprintf(stderr, "Filesystem features available at mkfs time:\n");
1160 for (i = 0; i < ARRAY_SIZE(mkfs_features) - 1; i++) {
1161 char *is_default = "";
1163 if (mkfs_features[i].flag & DEFAULT_MKFS_FEATURES)
1164 is_default = ", default";
1165 fprintf(stderr, "%-20s- %s (0x%llx%s)\n",
1166 mkfs_features[i].name,
1167 mkfs_features[i].desc,
1168 mkfs_features[i].flag,
1173 static int parse_one_fs_feature(const char *name, u64 *flags)
1178 for (i = 0; i < ARRAY_SIZE(mkfs_features); i++) {
1179 if (name[0] == '^' &&
1180 !strcmp(mkfs_features[i].name, name + 1)) {
1181 *flags &= ~ mkfs_features[i].flag;
1183 } else if (!strcmp(mkfs_features[i].name, name)) {
1184 *flags |= mkfs_features[i].flag;
1192 static void process_fs_features(u64 flags)
1196 for (i = 0; i < ARRAY_SIZE(mkfs_features); i++) {
1197 if (flags & mkfs_features[i].flag) {
1198 printf("Turning ON incompat feature '%s': %s\n",
1199 mkfs_features[i].name,
1200 mkfs_features[i].desc);
1207 * Return NULL if all features were parsed fine, otherwise return the name of
1208 * the first unparsed.
1210 static char* parse_fs_features(char *namelist, u64 *flags)
1213 char *save_ptr = NULL; /* Satisfy static checkers */
1215 for (this_char = strtok_r(namelist, ",", &save_ptr);
1217 this_char = strtok_r(NULL, ",", &save_ptr)) {
1218 if (parse_one_fs_feature(this_char, flags))
1225 int main(int ac, char **av)
1228 struct btrfs_root *root;
1229 struct btrfs_trans_handle *trans;
1232 u64 block_count = 0;
1233 u64 dev_block_count = 0;
1235 u64 alloc_start = 0;
1236 u64 metadata_profile = 0;
1237 u64 data_profile = 0;
1238 u32 leafsize = max_t(u32, sysconf(_SC_PAGESIZE), DEFAULT_MKFS_LEAF_SIZE);
1239 u32 sectorsize = 4096;
1240 u32 nodesize = leafsize;
1241 u32 stripesize = 4096;
1243 int option_index = 0;
1248 int leaf_forced = 0;
1249 int data_profile_opt = 0;
1250 int metadata_profile_opt = 0;
1253 int force_overwrite = 0;
1255 char *source_dir = NULL;
1256 int source_dir_set = 0;
1257 u64 num_of_meta_chunks = 0;
1258 u64 size_of_data = 0;
1259 u64 source_dir_size = 0;
1263 u64 features = DEFAULT_MKFS_FEATURES;
1267 c = getopt_long(ac, av, "A:b:fl:n:s:m:d:L:O:r:VMK",
1268 long_options, &option_index);
1273 alloc_start = parse_size(optarg);
1276 force_overwrite = 1;
1279 data_profile = parse_profile(optarg);
1280 data_profile_opt = 1;
1284 nodesize = parse_size(optarg);
1285 leafsize = parse_size(optarg);
1289 label = parse_label(optarg);
1292 metadata_profile = parse_profile(optarg);
1293 metadata_profile_opt = 1;
1299 char *orig = strdup(optarg);
1302 tmp = parse_fs_features(tmp, &features);
1305 "Unrecognized filesystem feature '%s'\n",
1311 if (features & BTRFS_FEATURE_LIST_ALL) {
1312 list_all_fs_features();
1318 sectorsize = parse_size(optarg);
1321 block_count = parse_size(optarg);
1322 if (block_count <= 1024*1024*1024) {
1323 printf("SMALL VOLUME: forcing mixed "
1324 "metadata/data groups\n");
1333 source_dir = optarg;
1343 sectorsize = max(sectorsize, (u32)sysconf(_SC_PAGESIZE));
1344 if (check_leaf_or_node_size(leafsize, sectorsize))
1346 if (check_leaf_or_node_size(nodesize, sectorsize))
1348 saved_optind = optind;
1349 dev_cnt = ac - optind;
1353 if (source_dir_set && dev_cnt > 1) {
1355 "The -r option is limited to a single device\n");
1358 while (dev_cnt-- > 0) {
1359 file = av[optind++];
1360 if (is_block_device(file))
1361 if (test_dev_for_mkfs(file, force_overwrite, estr)) {
1362 fprintf(stderr, "Error: %s", estr);
1367 optind = saved_optind;
1368 dev_cnt = ac - optind;
1370 file = av[optind++];
1373 if (is_vol_small(file)) {
1374 printf("SMALL VOLUME: forcing mixed metadata/data groups\n");
1379 * Set default profiles according to number of added devices.
1380 * For mixed groups defaults are single/single.
1383 if (!metadata_profile_opt) {
1384 if (dev_cnt == 1 && ssd)
1385 printf("Detected a SSD, turning off metadata "
1386 "duplication. Mkfs with -m dup if you want to "
1387 "force metadata duplication.\n");
1389 metadata_profile = (dev_cnt > 1) ?
1390 BTRFS_BLOCK_GROUP_RAID1 : (ssd) ?
1391 0: BTRFS_BLOCK_GROUP_DUP;
1393 if (!data_profile_opt) {
1394 data_profile = (dev_cnt > 1) ?
1395 BTRFS_BLOCK_GROUP_RAID0 : 0; /* raid0 or single */
1398 u32 best_leafsize = max_t(u32, sysconf(_SC_PAGESIZE), sectorsize);
1400 if (metadata_profile_opt || data_profile_opt) {
1401 if (metadata_profile != data_profile) {
1403 "ERROR: With mixed block groups data and metadata profiles must be the same\n");
1409 leafsize = best_leafsize;
1410 nodesize = best_leafsize;
1411 if (check_leaf_or_node_size(leafsize, sectorsize))
1414 if (leafsize != sectorsize) {
1415 fprintf(stderr, "Error: mixed metadata/data block groups "
1416 "require metadata blocksizes equal to the sectorsize\n");
1421 ret = test_num_disk_vs_raid(metadata_profile, data_profile,
1422 dev_cnt, mixed, estr);
1424 fprintf(stderr, "Error: %s\n", estr);
1428 /* if we are here that means all devs are good to btrfsify */
1429 printf("\nWARNING! - %s IS EXPERIMENTAL\n", BTRFS_BUILD_VERSION);
1430 printf("WARNING! - see http://btrfs.wiki.kernel.org before using\n\n");
1434 if (!source_dir_set) {
1436 * open without O_EXCL so that the problem should not
1437 * occur by the following processing.
1438 * (btrfs_register_one_device() fails if O_EXCL is on)
1440 fd = open(file, O_RDWR);
1442 fprintf(stderr, "unable to open %s: %s\n", file,
1447 ret = btrfs_prepare_device(fd, file, zero_end, &dev_block_count,
1448 block_count, &mixed, discard);
1449 if (block_count && block_count > dev_block_count) {
1450 fprintf(stderr, "%s is smaller than requested size\n", file);
1454 fd = open_target(file);
1456 fprintf(stderr, "unable to open the %s\n", file);
1461 source_dir_size = size_sourcedir(source_dir, sectorsize,
1462 &num_of_meta_chunks, &size_of_data);
1463 if(block_count < source_dir_size)
1464 block_count = source_dir_size;
1465 ret = zero_output_file(fd, block_count, sectorsize);
1467 fprintf(stderr, "unable to zero the output file\n");
1470 /* our "device" is the new image file */
1471 dev_block_count = block_count;
1474 /* To create the first block group and chunk 0 in make_btrfs */
1475 if (dev_block_count < BTRFS_MKFS_SYSTEM_GROUP_SIZE) {
1476 fprintf(stderr, "device is too small to make filesystem\n");
1480 blocks[0] = BTRFS_SUPER_INFO_OFFSET;
1481 for (i = 1; i < 7; i++) {
1482 blocks[i] = BTRFS_SUPER_INFO_OFFSET + 1024 * 1024 +
1487 * FS features that can be set by other means than -O
1488 * just set the bit here
1491 features |= BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS;
1493 if ((data_profile | metadata_profile) &
1494 (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6)) {
1495 features |= BTRFS_FEATURE_INCOMPAT_RAID56;
1498 process_fs_features(features);
1500 ret = make_btrfs(fd, file, label, blocks, dev_block_count,
1502 sectorsize, stripesize, features);
1504 fprintf(stderr, "error during mkfs: %s\n", strerror(-ret));
1508 root = open_ctree(file, 0, OPEN_CTREE_WRITES);
1510 fprintf(stderr, "Open ctree failed\n");
1514 root->fs_info->alloc_start = alloc_start;
1516 ret = make_root_dir(root, mixed);
1518 fprintf(stderr, "failed to setup the root directory\n");
1522 trans = btrfs_start_transaction(root, 1);
1527 btrfs_register_one_device(file);
1530 while (dev_cnt-- > 0) {
1531 int old_mixed = mixed;
1533 file = av[optind++];
1536 * open without O_EXCL so that the problem should not
1537 * occur by the following processing.
1538 * (btrfs_register_one_device() fails if O_EXCL is on)
1540 fd = open(file, O_RDWR);
1542 fprintf(stderr, "unable to open %s: %s\n", file,
1546 ret = btrfs_device_already_in_root(root, fd,
1547 BTRFS_SUPER_INFO_OFFSET);
1549 fprintf(stderr, "skipping duplicate device %s in FS\n",
1554 ret = btrfs_prepare_device(fd, file, zero_end, &dev_block_count,
1555 block_count, &mixed, discard);
1559 ret = btrfs_add_to_fsid(trans, root, fd, file, dev_block_count,
1560 sectorsize, sectorsize, sectorsize);
1562 btrfs_register_one_device(file);
1566 if (!source_dir_set) {
1567 ret = create_raid_groups(trans, root, data_profile,
1568 data_profile_opt, metadata_profile,
1569 metadata_profile_opt, mixed, ssd);
1573 ret = create_data_reloc_tree(trans, root);
1576 printf("fs created label %s on %s\n\tnodesize %u leafsize %u "
1577 "sectorsize %u size %s\n",
1578 label, first_file, nodesize, leafsize, sectorsize,
1579 pretty_size(btrfs_super_total_bytes(root->fs_info->super_copy)));
1581 printf("%s\n", BTRFS_BUILD_VERSION);
1582 btrfs_commit_transaction(trans, root);
1584 if (source_dir_set) {
1585 trans = btrfs_start_transaction(root, 1);
1586 ret = create_chunks(trans, root,
1587 num_of_meta_chunks, size_of_data);
1589 btrfs_commit_transaction(trans, root);
1591 ret = make_image(source_dir, root, fd);
1595 ret = close_ctree(root);