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 \
50 | BTRFS_FEATURE_INCOMPAT_SKINNY_METADATA)
52 #define DEFAULT_MKFS_LEAF_SIZE 16384
54 struct directory_name_entry {
58 struct list_head list;
61 static int make_root_dir(struct btrfs_root *root, int mixed)
63 struct btrfs_trans_handle *trans;
64 struct btrfs_key location;
70 trans = btrfs_start_transaction(root, 1);
71 bytes_used = btrfs_super_bytes_used(root->fs_info->super_copy);
73 root->fs_info->system_allocs = 1;
74 ret = btrfs_make_block_group(trans, root, bytes_used,
75 BTRFS_BLOCK_GROUP_SYSTEM,
76 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
77 0, BTRFS_MKFS_SYSTEM_GROUP_SIZE);
81 ret = btrfs_alloc_chunk(trans, root->fs_info->extent_root,
82 &chunk_start, &chunk_size,
83 BTRFS_BLOCK_GROUP_METADATA |
84 BTRFS_BLOCK_GROUP_DATA);
87 "no space to alloc data/metadata chunk\n");
91 ret = btrfs_make_block_group(trans, root, 0,
92 BTRFS_BLOCK_GROUP_METADATA |
93 BTRFS_BLOCK_GROUP_DATA,
94 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
95 chunk_start, chunk_size);
97 printf("Created a data/metadata chunk of size %llu\n", chunk_size);
99 ret = btrfs_alloc_chunk(trans, root->fs_info->extent_root,
100 &chunk_start, &chunk_size,
101 BTRFS_BLOCK_GROUP_METADATA);
102 if (ret == -ENOSPC) {
103 fprintf(stderr, "no space to alloc metadata chunk\n");
107 ret = btrfs_make_block_group(trans, root, 0,
108 BTRFS_BLOCK_GROUP_METADATA,
109 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
110 chunk_start, chunk_size);
114 root->fs_info->system_allocs = 0;
115 btrfs_commit_transaction(trans, root);
116 trans = btrfs_start_transaction(root, 1);
120 ret = btrfs_alloc_chunk(trans, root->fs_info->extent_root,
121 &chunk_start, &chunk_size,
122 BTRFS_BLOCK_GROUP_DATA);
123 if (ret == -ENOSPC) {
124 fprintf(stderr, "no space to alloc data chunk\n");
128 ret = btrfs_make_block_group(trans, root, 0,
129 BTRFS_BLOCK_GROUP_DATA,
130 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
131 chunk_start, chunk_size);
135 ret = btrfs_make_root_dir(trans, root->fs_info->tree_root,
136 BTRFS_ROOT_TREE_DIR_OBJECTID);
139 ret = btrfs_make_root_dir(trans, root, BTRFS_FIRST_FREE_OBJECTID);
142 memcpy(&location, &root->fs_info->fs_root->root_key, sizeof(location));
143 location.offset = (u64)-1;
144 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
146 btrfs_super_root_dir(root->fs_info->super_copy),
147 &location, BTRFS_FT_DIR, 0);
151 ret = btrfs_insert_inode_ref(trans, root->fs_info->tree_root,
152 "default", 7, location.objectid,
153 BTRFS_ROOT_TREE_DIR_OBJECTID, 0);
157 btrfs_commit_transaction(trans, root);
162 static void __recow_root(struct btrfs_trans_handle *trans,
163 struct btrfs_root *root)
166 struct extent_buffer *tmp;
168 if (trans->transid != btrfs_root_generation(&root->root_item)) {
169 extent_buffer_get(root->node);
170 ret = __btrfs_cow_block(trans, root, root->node,
171 NULL, 0, &tmp, 0, 0);
173 free_extent_buffer(tmp);
177 static void recow_roots(struct btrfs_trans_handle *trans,
178 struct btrfs_root *root)
180 struct btrfs_fs_info *info = root->fs_info;
182 __recow_root(trans, info->fs_root);
183 __recow_root(trans, info->tree_root);
184 __recow_root(trans, info->extent_root);
185 __recow_root(trans, info->chunk_root);
186 __recow_root(trans, info->dev_root);
187 __recow_root(trans, info->csum_root);
190 static int create_one_raid_group(struct btrfs_trans_handle *trans,
191 struct btrfs_root *root, u64 type)
197 ret = btrfs_alloc_chunk(trans, root->fs_info->extent_root,
198 &chunk_start, &chunk_size, type);
199 if (ret == -ENOSPC) {
200 fprintf(stderr, "not enough free space\n");
204 ret = btrfs_make_block_group(trans, root->fs_info->extent_root, 0,
205 type, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
206 chunk_start, chunk_size);
211 static int create_raid_groups(struct btrfs_trans_handle *trans,
212 struct btrfs_root *root, u64 data_profile,
213 int data_profile_opt, u64 metadata_profile,
216 u64 num_devices = btrfs_super_num_devices(root->fs_info->super_copy);
219 if (metadata_profile) {
220 u64 meta_flags = BTRFS_BLOCK_GROUP_METADATA;
222 ret = create_one_raid_group(trans, root,
223 BTRFS_BLOCK_GROUP_SYSTEM |
228 meta_flags |= BTRFS_BLOCK_GROUP_DATA;
230 ret = create_one_raid_group(trans, root, meta_flags |
235 if (!mixed && num_devices > 1 && data_profile) {
236 ret = create_one_raid_group(trans, root,
237 BTRFS_BLOCK_GROUP_DATA |
241 recow_roots(trans, root);
246 static int create_data_reloc_tree(struct btrfs_trans_handle *trans,
247 struct btrfs_root *root)
249 struct btrfs_key location;
250 struct btrfs_root_item root_item;
251 struct extent_buffer *tmp;
252 u64 objectid = BTRFS_DATA_RELOC_TREE_OBJECTID;
255 ret = btrfs_copy_root(trans, root, root->node, &tmp, objectid);
258 memcpy(&root_item, &root->root_item, sizeof(root_item));
259 btrfs_set_root_bytenr(&root_item, tmp->start);
260 btrfs_set_root_level(&root_item, btrfs_header_level(tmp));
261 btrfs_set_root_generation(&root_item, trans->transid);
262 free_extent_buffer(tmp);
264 location.objectid = objectid;
265 location.type = BTRFS_ROOT_ITEM_KEY;
267 ret = btrfs_insert_root(trans, root->fs_info->tree_root,
268 &location, &root_item);
274 static void print_usage(void) __attribute__((noreturn));
275 static void print_usage(void)
277 fprintf(stderr, "usage: mkfs.btrfs [options] dev [ dev ... ]\n");
278 fprintf(stderr, "options:\n");
279 fprintf(stderr, "\t -A --alloc-start the offset to start the FS\n");
280 fprintf(stderr, "\t -b --byte-count total number of bytes in the FS\n");
281 fprintf(stderr, "\t -d --data data profile, raid0, raid1, raid5, raid6, raid10, dup or single\n");
282 fprintf(stderr, "\t -f --force force overwrite of existing filesystem\n");
283 fprintf(stderr, "\t -l --leafsize size of btree leaves\n");
284 fprintf(stderr, "\t -L --label set a label\n");
285 fprintf(stderr, "\t -m --metadata metadata profile, values like data profile\n");
286 fprintf(stderr, "\t -M --mixed mix metadata and data together\n");
287 fprintf(stderr, "\t -n --nodesize size of btree nodes\n");
288 fprintf(stderr, "\t -s --sectorsize min block allocation (may not mountable by current kernel)\n");
289 fprintf(stderr, "\t -r --rootdir the source directory\n");
290 fprintf(stderr, "\t -K --nodiscard do not perform whole device TRIM\n");
291 fprintf(stderr, "\t -O --features comma separated list of filesystem features\n");
292 fprintf(stderr, "\t -U --uuid specify the filesystem UUID\n");
293 fprintf(stderr, "\t -V --version print the mkfs.btrfs version and exit\n");
294 fprintf(stderr, "%s\n", BTRFS_BUILD_VERSION);
298 static void print_version(void) __attribute__((noreturn));
299 static void print_version(void)
301 fprintf(stderr, "mkfs.btrfs, part of %s\n", BTRFS_BUILD_VERSION);
305 static u64 parse_profile(char *s)
307 if (strcmp(s, "raid0") == 0) {
308 return BTRFS_BLOCK_GROUP_RAID0;
309 } else if (strcmp(s, "raid1") == 0) {
310 return BTRFS_BLOCK_GROUP_RAID1;
311 } else if (strcmp(s, "raid5") == 0) {
312 return BTRFS_BLOCK_GROUP_RAID5;
313 } else if (strcmp(s, "raid6") == 0) {
314 return BTRFS_BLOCK_GROUP_RAID6;
315 } else if (strcmp(s, "raid10") == 0) {
316 return BTRFS_BLOCK_GROUP_RAID10;
317 } else if (strcmp(s, "dup") == 0) {
318 return BTRFS_BLOCK_GROUP_DUP;
319 } else if (strcmp(s, "single") == 0) {
322 fprintf(stderr, "Unknown profile %s\n", s);
329 static char *parse_label(char *input)
331 int len = strlen(input);
333 if (len >= BTRFS_LABEL_SIZE) {
334 fprintf(stderr, "Label %s is too long (max %d)\n", input,
335 BTRFS_LABEL_SIZE - 1);
338 return strdup(input);
341 static struct option long_options[] = {
342 { "alloc-start", 1, NULL, 'A'},
343 { "byte-count", 1, NULL, 'b' },
344 { "force", 0, NULL, 'f' },
345 { "leafsize", 1, NULL, 'l' },
346 { "label", 1, NULL, 'L'},
347 { "metadata", 1, NULL, 'm' },
348 { "mixed", 0, NULL, 'M' },
349 { "nodesize", 1, NULL, 'n' },
350 { "sectorsize", 1, NULL, 's' },
351 { "data", 1, NULL, 'd' },
352 { "version", 0, NULL, 'V' },
353 { "rootdir", 1, NULL, 'r' },
354 { "nodiscard", 0, NULL, 'K' },
355 { "features", 1, NULL, 'O' },
356 { "uuid", required_argument, NULL, 'U' },
360 static int add_directory_items(struct btrfs_trans_handle *trans,
361 struct btrfs_root *root, u64 objectid,
362 ino_t parent_inum, const char *name,
363 struct stat *st, int *dir_index_cnt)
367 struct btrfs_key location;
370 name_len = strlen(name);
372 location.objectid = objectid;
374 btrfs_set_key_type(&location, BTRFS_INODE_ITEM_KEY);
376 if (S_ISDIR(st->st_mode))
377 filetype = BTRFS_FT_DIR;
378 if (S_ISREG(st->st_mode))
379 filetype = BTRFS_FT_REG_FILE;
380 if (S_ISLNK(st->st_mode))
381 filetype = BTRFS_FT_SYMLINK;
383 ret = btrfs_insert_dir_item(trans, root, name, name_len,
384 parent_inum, &location,
385 filetype, index_cnt);
388 ret = btrfs_insert_inode_ref(trans, root, name, name_len,
389 objectid, parent_inum, index_cnt);
390 *dir_index_cnt = index_cnt;
396 static int fill_inode_item(struct btrfs_trans_handle *trans,
397 struct btrfs_root *root,
398 struct btrfs_inode_item *dst, struct stat *src)
401 u64 sectorsize = root->sectorsize;
404 * btrfs_inode_item has some reserved fields
405 * and represents on-disk inode entry, so
406 * zero everything to prevent information leak
408 memset(dst, 0, sizeof (*dst));
410 btrfs_set_stack_inode_generation(dst, trans->transid);
411 btrfs_set_stack_inode_size(dst, src->st_size);
412 btrfs_set_stack_inode_nbytes(dst, 0);
413 btrfs_set_stack_inode_block_group(dst, 0);
414 btrfs_set_stack_inode_nlink(dst, src->st_nlink);
415 btrfs_set_stack_inode_uid(dst, src->st_uid);
416 btrfs_set_stack_inode_gid(dst, src->st_gid);
417 btrfs_set_stack_inode_mode(dst, src->st_mode);
418 btrfs_set_stack_inode_rdev(dst, 0);
419 btrfs_set_stack_inode_flags(dst, 0);
420 btrfs_set_stack_timespec_sec(&dst->atime, src->st_atime);
421 btrfs_set_stack_timespec_nsec(&dst->atime, 0);
422 btrfs_set_stack_timespec_sec(&dst->ctime, src->st_ctime);
423 btrfs_set_stack_timespec_nsec(&dst->ctime, 0);
424 btrfs_set_stack_timespec_sec(&dst->mtime, src->st_mtime);
425 btrfs_set_stack_timespec_nsec(&dst->mtime, 0);
426 btrfs_set_stack_timespec_sec(&dst->otime, 0);
427 btrfs_set_stack_timespec_nsec(&dst->otime, 0);
429 if (S_ISDIR(src->st_mode)) {
430 btrfs_set_stack_inode_size(dst, 0);
431 btrfs_set_stack_inode_nlink(dst, 1);
433 if (S_ISREG(src->st_mode)) {
434 btrfs_set_stack_inode_size(dst, (u64)src->st_size);
435 if (src->st_size <= BTRFS_MAX_INLINE_DATA_SIZE(root))
436 btrfs_set_stack_inode_nbytes(dst, src->st_size);
438 blocks = src->st_size / sectorsize;
439 if (src->st_size % sectorsize)
441 blocks *= sectorsize;
442 btrfs_set_stack_inode_nbytes(dst, blocks);
445 if (S_ISLNK(src->st_mode))
446 btrfs_set_stack_inode_nbytes(dst, src->st_size + 1);
451 static int directory_select(const struct direct *entry)
453 if ((strncmp(entry->d_name, ".", entry->d_reclen) == 0) ||
454 (strncmp(entry->d_name, "..", entry->d_reclen) == 0))
460 static void free_namelist(struct direct **files, int count)
467 for (i = 0; i < count; ++i)
472 static u64 calculate_dir_inode_size(char *dirname)
475 struct direct **files, *cur_file;
476 u64 dir_inode_size = 0;
478 count = scandir(dirname, &files, directory_select, NULL);
480 for (i = 0; i < count; i++) {
482 dir_inode_size += strlen(cur_file->d_name);
485 free_namelist(files, count);
488 return dir_inode_size;
491 static int add_inode_items(struct btrfs_trans_handle *trans,
492 struct btrfs_root *root,
493 struct stat *st, char *name,
494 u64 self_objectid, ino_t parent_inum,
495 int dir_index_cnt, struct btrfs_inode_item *inode_ret)
498 struct btrfs_key inode_key;
499 struct btrfs_inode_item btrfs_inode;
503 fill_inode_item(trans, root, &btrfs_inode, st);
504 objectid = self_objectid;
506 if (S_ISDIR(st->st_mode)) {
507 inode_size = calculate_dir_inode_size(name);
508 btrfs_set_stack_inode_size(&btrfs_inode, inode_size);
511 inode_key.objectid = objectid;
512 inode_key.offset = 0;
513 btrfs_set_key_type(&inode_key, BTRFS_INODE_ITEM_KEY);
515 ret = btrfs_insert_inode(trans, root, objectid, &btrfs_inode);
517 *inode_ret = btrfs_inode;
521 static int add_xattr_item(struct btrfs_trans_handle *trans,
522 struct btrfs_root *root, u64 objectid,
523 const char *file_name)
527 char xattr_list[XATTR_LIST_MAX];
529 char cur_value[XATTR_SIZE_MAX];
530 char delimiter = '\0';
531 char *next_location = xattr_list;
533 ret = llistxattr(file_name, xattr_list, XATTR_LIST_MAX);
537 fprintf(stderr, "get a list of xattr failed for %s\n",
544 cur_name = strtok(xattr_list, &delimiter);
545 while (cur_name != NULL) {
546 cur_name_len = strlen(cur_name);
547 next_location += cur_name_len + 1;
549 ret = getxattr(file_name, cur_name, cur_value, XATTR_SIZE_MAX);
553 fprintf(stderr, "get a xattr value failed for %s attr %s\n",
554 file_name, cur_name);
558 ret = btrfs_insert_xattr_item(trans, root, cur_name,
559 cur_name_len, cur_value,
562 fprintf(stderr, "insert a xattr item failed for %s\n",
566 cur_name = strtok(next_location, &delimiter);
572 static int add_symbolic_link(struct btrfs_trans_handle *trans,
573 struct btrfs_root *root,
574 u64 objectid, const char *path_name)
577 u64 sectorsize = root->sectorsize;
578 char *buf = malloc(sectorsize);
580 ret = readlink(path_name, buf, sectorsize);
582 fprintf(stderr, "readlink failed for %s\n", path_name);
585 if (ret >= sectorsize) {
586 fprintf(stderr, "symlink too long for %s", path_name);
591 buf[ret] = '\0'; /* readlink does not do it for us */
592 ret = btrfs_insert_inline_extent(trans, root, objectid, 0,
599 static int add_file_items(struct btrfs_trans_handle *trans,
600 struct btrfs_root *root,
601 struct btrfs_inode_item *btrfs_inode, u64 objectid,
602 ino_t parent_inum, struct stat *st,
603 const char *path_name, int out_fd)
608 struct btrfs_key key;
610 u32 sectorsize = root->sectorsize;
615 struct extent_buffer *eb = NULL;
618 if (st->st_size == 0)
621 fd = open(path_name, O_RDONLY);
623 fprintf(stderr, "%s open failed\n", path_name);
627 blocks = st->st_size / sectorsize;
628 if (st->st_size % sectorsize)
631 if (st->st_size <= BTRFS_MAX_INLINE_DATA_SIZE(root)) {
632 char *buffer = malloc(st->st_size);
633 ret_read = pread64(fd, buffer, st->st_size, bytes_read);
634 if (ret_read == -1) {
635 fprintf(stderr, "%s read failed\n", path_name);
640 ret = btrfs_insert_inline_extent(trans, root, objectid, 0,
641 buffer, st->st_size);
646 /* round up our st_size to the FS blocksize */
647 total_bytes = (u64)blocks * sectorsize;
650 * do our IO in extent buffers so it can work
651 * against any raid type
653 eb = malloc(sizeof(*eb) + sectorsize);
658 memset(eb, 0, sizeof(*eb) + sectorsize);
663 * keep our extent size at 1MB max, this makes it easier to work inside
664 * the tiny block groups created during mkfs
666 cur_bytes = min(total_bytes, 1024ULL * 1024);
667 ret = btrfs_reserve_extent(trans, root, cur_bytes, 0, 0, (u64)-1,
672 first_block = key.objectid;
675 while (bytes_read < cur_bytes) {
677 memset(eb->data, 0, sectorsize);
679 ret_read = pread64(fd, eb->data, sectorsize, file_pos + bytes_read);
680 if (ret_read == -1) {
681 fprintf(stderr, "%s read failed\n", path_name);
685 eb->start = first_block + bytes_read;
686 eb->len = sectorsize;
689 * we're doing the csum before we record the extent, but
692 ret = btrfs_csum_file_block(trans, root->fs_info->csum_root,
693 first_block + bytes_read + sectorsize,
694 first_block + bytes_read,
695 eb->data, sectorsize);
699 ret = write_and_map_eb(trans, root, eb);
701 fprintf(stderr, "output file write failed\n");
705 bytes_read += sectorsize;
709 ret = btrfs_record_file_extent(trans, root, objectid, btrfs_inode,
710 file_pos, first_block, cur_bytes);
716 file_pos += cur_bytes;
717 total_bytes -= cur_bytes;
728 static char *make_path(char *dir, char *name)
732 path = malloc(strlen(dir) + strlen(name) + 2);
736 if (dir[strlen(dir) - 1] != '/')
742 static int traverse_directory(struct btrfs_trans_handle *trans,
743 struct btrfs_root *root, char *dir_name,
744 struct directory_name_entry *dir_head, int out_fd)
748 struct btrfs_inode_item cur_inode;
749 struct btrfs_inode_item *inode_item;
750 int count, i, dir_index_cnt;
751 struct direct **files;
753 struct directory_name_entry *dir_entry, *parent_dir_entry;
754 struct direct *cur_file;
755 ino_t parent_inum, cur_inum;
756 ino_t highest_inum = 0;
757 char *parent_dir_name;
758 char real_path[PATH_MAX];
759 struct btrfs_path path;
760 struct extent_buffer *leaf;
761 struct btrfs_key root_dir_key;
762 u64 root_dir_inode_size = 0;
764 /* Add list for source directory */
765 dir_entry = malloc(sizeof(struct directory_name_entry));
766 dir_entry->dir_name = dir_name;
767 dir_entry->path = realpath(dir_name, real_path);
768 if (!dir_entry->path) {
769 fprintf(stderr, "get directory real path error\n");
774 parent_inum = highest_inum + BTRFS_FIRST_FREE_OBJECTID;
775 dir_entry->inum = parent_inum;
776 list_add_tail(&dir_entry->list, &dir_head->list);
778 btrfs_init_path(&path);
780 root_dir_key.objectid = btrfs_root_dirid(&root->root_item);
781 root_dir_key.offset = 0;
782 btrfs_set_key_type(&root_dir_key, BTRFS_INODE_ITEM_KEY);
783 ret = btrfs_lookup_inode(trans, root, &path, &root_dir_key, 1);
785 fprintf(stderr, "root dir lookup error\n");
789 leaf = path.nodes[0];
790 inode_item = btrfs_item_ptr(leaf, path.slots[0],
791 struct btrfs_inode_item);
793 root_dir_inode_size = calculate_dir_inode_size(dir_name);
794 btrfs_set_inode_size(leaf, inode_item, root_dir_inode_size);
795 btrfs_mark_buffer_dirty(leaf);
797 btrfs_release_path(&path);
800 parent_dir_entry = list_entry(dir_head->list.next,
801 struct directory_name_entry,
803 list_del(&parent_dir_entry->list);
805 parent_inum = parent_dir_entry->inum;
806 parent_dir_name = parent_dir_entry->dir_name;
807 if (chdir(parent_dir_entry->path)) {
808 fprintf(stderr, "chdir error for %s\n",
814 count = scandir(parent_dir_entry->path, &files,
815 directory_select, NULL);
818 fprintf(stderr, "scandir for %s failed: %s\n",
819 parent_dir_name, strerror (errno));
824 for (i = 0; i < count; i++) {
827 if (lstat(cur_file->d_name, &st) == -1) {
828 fprintf(stderr, "lstat failed for file %s\n",
834 cur_inum = st.st_ino;
835 ret = add_directory_items(trans, root,
836 cur_inum, parent_inum,
838 &st, &dir_index_cnt);
840 fprintf(stderr, "add_directory_items failed\n");
844 ret = add_inode_items(trans, root, &st,
845 cur_file->d_name, cur_inum,
846 parent_inum, dir_index_cnt,
848 if (ret == -EEXIST) {
849 BUG_ON(st.st_nlink <= 1);
853 fprintf(stderr, "add_inode_items failed\n");
857 ret = add_xattr_item(trans, root,
858 cur_inum, cur_file->d_name);
860 fprintf(stderr, "add_xattr_item failed\n");
865 if (S_ISDIR(st.st_mode)) {
866 dir_entry = malloc(sizeof(struct directory_name_entry));
867 dir_entry->dir_name = cur_file->d_name;
868 dir_entry->path = make_path(parent_dir_entry->path,
870 dir_entry->inum = cur_inum;
871 list_add_tail(&dir_entry->list, &dir_head->list);
872 } else if (S_ISREG(st.st_mode)) {
873 ret = add_file_items(trans, root, &cur_inode,
874 cur_inum, parent_inum, &st,
875 cur_file->d_name, out_fd);
877 fprintf(stderr, "add_file_items failed\n");
880 } else if (S_ISLNK(st.st_mode)) {
881 ret = add_symbolic_link(trans, root,
882 cur_inum, cur_file->d_name);
884 fprintf(stderr, "add_symbolic_link failed\n");
890 free_namelist(files, count);
891 free(parent_dir_entry);
895 } while (!list_empty(&dir_head->list));
900 free_namelist(files, count);
902 free(parent_dir_entry);
909 static int open_target(char *output_name)
912 output_fd = open(output_name, O_CREAT | O_RDWR | O_TRUNC,
913 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH);
918 static int create_chunks(struct btrfs_trans_handle *trans,
919 struct btrfs_root *root, u64 num_of_meta_chunks,
924 u64 meta_type = BTRFS_BLOCK_GROUP_METADATA;
925 u64 data_type = BTRFS_BLOCK_GROUP_DATA;
926 u64 minimum_data_chunk_size = 8 * 1024 * 1024;
930 for (i = 0; i < num_of_meta_chunks; i++) {
931 ret = btrfs_alloc_chunk(trans, root->fs_info->extent_root,
932 &chunk_start, &chunk_size, meta_type);
934 ret = btrfs_make_block_group(trans, root->fs_info->extent_root, 0,
935 meta_type, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
936 chunk_start, chunk_size);
938 set_extent_dirty(&root->fs_info->free_space_cache,
939 chunk_start, chunk_start + chunk_size - 1, 0);
942 if (size_of_data < minimum_data_chunk_size)
943 size_of_data = minimum_data_chunk_size;
945 ret = btrfs_alloc_data_chunk(trans, root->fs_info->extent_root,
946 &chunk_start, size_of_data, data_type);
948 ret = btrfs_make_block_group(trans, root->fs_info->extent_root, 0,
949 data_type, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
950 chunk_start, size_of_data);
952 set_extent_dirty(&root->fs_info->free_space_cache,
953 chunk_start, chunk_start + size_of_data - 1, 0);
957 static int make_image(char *source_dir, struct btrfs_root *root, int out_fd)
960 struct btrfs_trans_handle *trans;
964 struct directory_name_entry dir_head;
966 struct directory_name_entry *dir_entry = NULL;
968 ret = lstat(source_dir, &root_st);
970 fprintf(stderr, "unable to lstat the %s\n", source_dir);
974 INIT_LIST_HEAD(&dir_head.list);
976 trans = btrfs_start_transaction(root, 1);
977 ret = traverse_directory(trans, root, source_dir, &dir_head, out_fd);
979 fprintf(stderr, "unable to traverse_directory\n");
982 btrfs_commit_transaction(trans, root);
984 printf("Making image is completed.\n");
987 while (!list_empty(&dir_head.list)) {
988 dir_entry = list_entry(dir_head.list.next,
989 struct directory_name_entry, list);
990 list_del(&dir_entry->list);
994 fprintf(stderr, "Making image is aborted.\n");
999 * This ignores symlinks with unreadable targets and subdirs that can't
1000 * be read. It's a best-effort to give a rough estimate of the size of
1001 * a subdir. It doesn't guarantee that prepopulating btrfs from this
1002 * tree won't still run out of space.
1004 * The rounding up to 4096 is questionable. Previous code used du -B 4096.
1006 static u64 global_total_size;
1007 static int ftw_add_entry_size(const char *fpath, const struct stat *st,
1010 if (type == FTW_F || type == FTW_D)
1011 global_total_size += round_up(st->st_size, 4096);
1016 static u64 size_sourcedir(char *dir_name, u64 sectorsize,
1017 u64 *num_of_meta_chunks_ret, u64 *size_of_data_ret)
1022 u64 default_chunk_size = 8 * 1024 * 1024; /* 8MB */
1023 u64 allocated_meta_size = 8 * 1024 * 1024; /* 8MB */
1024 u64 allocated_total_size = 20 * 1024 * 1024; /* 20MB */
1025 u64 num_of_meta_chunks = 0;
1026 u64 num_of_data_chunks = 0;
1027 u64 num_of_allocated_meta_chunks =
1028 allocated_meta_size / default_chunk_size;
1030 global_total_size = 0;
1031 ret = ftw(dir_name, ftw_add_entry_size, 10);
1032 dir_size = global_total_size;
1034 fprintf(stderr, "ftw subdir walk of '%s' failed: %s\n",
1035 dir_name, strerror(errno));
1039 num_of_data_chunks = (dir_size + default_chunk_size - 1) /
1042 num_of_meta_chunks = (dir_size / 2) / default_chunk_size;
1043 if (((dir_size / 2) % default_chunk_size) != 0)
1044 num_of_meta_chunks++;
1045 if (num_of_meta_chunks <= num_of_allocated_meta_chunks)
1046 num_of_meta_chunks = 0;
1048 num_of_meta_chunks -= num_of_allocated_meta_chunks;
1050 total_size = allocated_total_size +
1051 (num_of_data_chunks * default_chunk_size) +
1052 (num_of_meta_chunks * default_chunk_size);
1054 *num_of_meta_chunks_ret = num_of_meta_chunks;
1055 *size_of_data_ret = num_of_data_chunks * default_chunk_size;
1059 static int zero_output_file(int out_fd, u64 size, u32 sectorsize)
1061 int len = sectorsize;
1062 int loop_num = size / sectorsize;
1064 char *buf = malloc(len);
1070 memset(buf, 0, len);
1071 for (i = 0; i < loop_num; i++) {
1072 written = pwrite64(out_fd, buf, len, location);
1075 location += sectorsize;
1081 static int check_leaf_or_node_size(u32 size, u32 sectorsize)
1083 if (size < sectorsize) {
1085 "Illegal leafsize (or nodesize) %u (smaller than %u)\n",
1088 } else if (size > BTRFS_MAX_METADATA_BLOCKSIZE) {
1090 "Illegal leafsize (or nodesize) %u (larger than %u)\n",
1091 size, BTRFS_MAX_METADATA_BLOCKSIZE);
1093 } else if (size & (sectorsize - 1)) {
1095 "Illegal leafsize (or nodesize) %u (not align to %u)\n",
1102 static int is_ssd(const char *file)
1106 char sysfs_path[PATH_MAX];
1112 probe = blkid_new_probe_from_filename(file);
1116 /* Device number of this disk (possibly a partition) */
1117 devno = blkid_probe_get_devno(probe);
1119 blkid_free_probe(probe);
1123 /* Get whole disk name (not full path) for this devno */
1124 ret = blkid_devno_to_wholedisk(devno,
1125 wholedisk, sizeof(wholedisk), NULL);
1127 blkid_free_probe(probe);
1131 snprintf(sysfs_path, PATH_MAX, "/sys/block/%s/queue/rotational",
1134 blkid_free_probe(probe);
1136 fd = open(sysfs_path, O_RDONLY);
1141 if (read(fd, &rotational, sizeof(char)) < sizeof(char)) {
1147 return !atoi((const char *)&rotational);
1150 #define BTRFS_FEATURE_LIST_ALL (1ULL << 63)
1152 static const struct btrfs_fs_feature {
1156 } mkfs_features[] = {
1157 { "mixed-bg", BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS,
1158 "mixed data and metadata block groups" },
1159 { "extref", BTRFS_FEATURE_INCOMPAT_EXTENDED_IREF,
1160 "increased hardlink limit per file to 65536" },
1161 { "raid56", BTRFS_FEATURE_INCOMPAT_RAID56,
1162 "raid56 extended format" },
1163 { "skinny-metadata", BTRFS_FEATURE_INCOMPAT_SKINNY_METADATA,
1164 "reduced-size metadata extent refs" },
1165 { "no-holes", BTRFS_FEATURE_INCOMPAT_NO_HOLES,
1166 "no explicit hole extents for files" },
1167 /* Keep this one last */
1168 { "list-all", BTRFS_FEATURE_LIST_ALL, NULL }
1171 static void list_all_fs_features(void)
1175 fprintf(stderr, "Filesystem features available at mkfs time:\n");
1176 for (i = 0; i < ARRAY_SIZE(mkfs_features) - 1; i++) {
1177 char *is_default = "";
1179 if (mkfs_features[i].flag & DEFAULT_MKFS_FEATURES)
1180 is_default = ", default";
1181 fprintf(stderr, "%-20s- %s (0x%llx%s)\n",
1182 mkfs_features[i].name,
1183 mkfs_features[i].desc,
1184 mkfs_features[i].flag,
1189 static int parse_one_fs_feature(const char *name, u64 *flags)
1194 for (i = 0; i < ARRAY_SIZE(mkfs_features); i++) {
1195 if (name[0] == '^' &&
1196 !strcmp(mkfs_features[i].name, name + 1)) {
1197 *flags &= ~ mkfs_features[i].flag;
1199 } else if (!strcmp(mkfs_features[i].name, name)) {
1200 *flags |= mkfs_features[i].flag;
1208 static void process_fs_features(u64 flags)
1212 for (i = 0; i < ARRAY_SIZE(mkfs_features); i++) {
1213 if (flags & mkfs_features[i].flag) {
1214 printf("Turning ON incompat feature '%s': %s\n",
1215 mkfs_features[i].name,
1216 mkfs_features[i].desc);
1223 * Return NULL if all features were parsed fine, otherwise return the name of
1224 * the first unparsed.
1226 static char* parse_fs_features(char *namelist, u64 *flags)
1229 char *save_ptr = NULL; /* Satisfy static checkers */
1231 for (this_char = strtok_r(namelist, ",", &save_ptr);
1233 this_char = strtok_r(NULL, ",", &save_ptr)) {
1234 if (parse_one_fs_feature(this_char, flags))
1241 int main(int ac, char **av)
1244 struct btrfs_root *root;
1245 struct btrfs_trans_handle *trans;
1248 u64 block_count = 0;
1249 u64 dev_block_count = 0;
1251 u64 alloc_start = 0;
1252 u64 metadata_profile = 0;
1253 u64 data_profile = 0;
1254 u32 leafsize = max_t(u32, sysconf(_SC_PAGESIZE), DEFAULT_MKFS_LEAF_SIZE);
1255 u32 sectorsize = 4096;
1256 u32 nodesize = leafsize;
1257 u32 stripesize = 4096;
1259 int option_index = 0;
1264 int leaf_forced = 0;
1265 int data_profile_opt = 0;
1266 int metadata_profile_opt = 0;
1269 int force_overwrite = 0;
1271 char *source_dir = NULL;
1272 int source_dir_set = 0;
1273 u64 num_of_meta_chunks = 0;
1274 u64 size_of_data = 0;
1275 u64 source_dir_size = 0;
1279 char *fs_uuid = NULL;
1280 u64 features = DEFAULT_MKFS_FEATURES;
1284 c = getopt_long(ac, av, "A:b:fl:n:s:m:d:L:O:r:U:VMK",
1285 long_options, &option_index);
1290 alloc_start = parse_size(optarg);
1293 force_overwrite = 1;
1296 data_profile = parse_profile(optarg);
1297 data_profile_opt = 1;
1301 nodesize = parse_size(optarg);
1302 leafsize = parse_size(optarg);
1306 label = parse_label(optarg);
1309 metadata_profile = parse_profile(optarg);
1310 metadata_profile_opt = 1;
1316 char *orig = strdup(optarg);
1319 tmp = parse_fs_features(tmp, &features);
1322 "Unrecognized filesystem feature '%s'\n",
1328 if (features & BTRFS_FEATURE_LIST_ALL) {
1329 list_all_fs_features();
1335 sectorsize = parse_size(optarg);
1338 block_count = parse_size(optarg);
1339 if (block_count <= BTRFS_MKFS_SMALL_VOLUME_SIZE) {
1341 "SMALL VOLUME: forcing mixed metadata/data groups\n");
1350 source_dir = optarg;
1363 sectorsize = max(sectorsize, (u32)sysconf(_SC_PAGESIZE));
1364 if (check_leaf_or_node_size(leafsize, sectorsize))
1366 if (check_leaf_or_node_size(nodesize, sectorsize))
1368 saved_optind = optind;
1369 dev_cnt = ac - optind;
1373 if (source_dir_set && dev_cnt > 1) {
1375 "The -r option is limited to a single device\n");
1382 if (uuid_parse(fs_uuid, dummy_uuid) != 0) {
1383 fprintf(stderr, "could not parse UUID: %s\n", fs_uuid);
1386 if (!test_uuid_unique(fs_uuid)) {
1387 fprintf(stderr, "non-unique UUID: %s\n", fs_uuid);
1392 while (dev_cnt-- > 0) {
1393 file = av[optind++];
1394 if (is_block_device(file))
1395 if (test_dev_for_mkfs(file, force_overwrite, estr)) {
1396 fprintf(stderr, "Error: %s", estr);
1401 optind = saved_optind;
1402 dev_cnt = ac - optind;
1404 file = av[optind++];
1407 if (is_vol_small(file)) {
1408 printf("SMALL VOLUME: forcing mixed metadata/data groups\n");
1413 * Set default profiles according to number of added devices.
1414 * For mixed groups defaults are single/single.
1417 if (!metadata_profile_opt) {
1418 if (dev_cnt == 1 && ssd)
1419 printf("Detected a SSD, turning off metadata "
1420 "duplication. Mkfs with -m dup if you want to "
1421 "force metadata duplication.\n");
1423 metadata_profile = (dev_cnt > 1) ?
1424 BTRFS_BLOCK_GROUP_RAID1 : (ssd) ?
1425 0: BTRFS_BLOCK_GROUP_DUP;
1427 if (!data_profile_opt) {
1428 data_profile = (dev_cnt > 1) ?
1429 BTRFS_BLOCK_GROUP_RAID0 : 0; /* raid0 or single */
1432 u32 best_leafsize = max_t(u32, sysconf(_SC_PAGESIZE), sectorsize);
1434 if (metadata_profile_opt || data_profile_opt) {
1435 if (metadata_profile != data_profile) {
1437 "ERROR: With mixed block groups data and metadata profiles must be the same\n");
1443 leafsize = best_leafsize;
1444 nodesize = best_leafsize;
1445 if (check_leaf_or_node_size(leafsize, sectorsize))
1448 if (leafsize != sectorsize) {
1449 fprintf(stderr, "Error: mixed metadata/data block groups "
1450 "require metadata blocksizes equal to the sectorsize\n");
1455 /* Check device/block_count after the leafsize is determined */
1456 if (block_count && block_count < btrfs_min_dev_size(leafsize)) {
1458 "Size '%llu' is too small to make a usable filesystem\n",
1461 "Minimum size for btrfs filesystem is %llu\n",
1462 btrfs_min_dev_size(leafsize));
1465 for (i = saved_optind; i < saved_optind + dev_cnt; i++) {
1469 ret = test_minimum_size(path, leafsize);
1471 fprintf(stderr, "Failed to check size for '%s': %s\n",
1472 path, strerror(-ret));
1477 "'%s' is too small to make a usable filesystem\n",
1480 "Minimum size for each btrfs device is %llu.\n",
1481 btrfs_min_dev_size(leafsize));
1485 ret = test_num_disk_vs_raid(metadata_profile, data_profile,
1486 dev_cnt, mixed, estr);
1488 fprintf(stderr, "Error: %s\n", estr);
1492 /* if we are here that means all devs are good to btrfsify */
1493 printf("%s\n", BTRFS_BUILD_VERSION);
1494 printf("See http://btrfs.wiki.kernel.org for more information.\n\n");
1498 if (!source_dir_set) {
1500 * open without O_EXCL so that the problem should not
1501 * occur by the following processing.
1502 * (btrfs_register_one_device() fails if O_EXCL is on)
1504 fd = open(file, O_RDWR);
1506 fprintf(stderr, "unable to open %s: %s\n", file,
1511 ret = btrfs_prepare_device(fd, file, zero_end, &dev_block_count,
1512 block_count, &mixed, discard);
1517 if (block_count && block_count > dev_block_count) {
1518 fprintf(stderr, "%s is smaller than requested size\n", file);
1522 fd = open_target(file);
1524 fprintf(stderr, "unable to open the %s\n", file);
1529 source_dir_size = size_sourcedir(source_dir, sectorsize,
1530 &num_of_meta_chunks, &size_of_data);
1531 if(block_count < source_dir_size)
1532 block_count = source_dir_size;
1533 ret = zero_output_file(fd, block_count, sectorsize);
1535 fprintf(stderr, "unable to zero the output file\n");
1538 /* our "device" is the new image file */
1539 dev_block_count = block_count;
1542 /* To create the first block group and chunk 0 in make_btrfs */
1543 if (dev_block_count < BTRFS_MKFS_SYSTEM_GROUP_SIZE) {
1544 fprintf(stderr, "device is too small to make filesystem\n");
1548 blocks[0] = BTRFS_SUPER_INFO_OFFSET;
1549 for (i = 1; i < 7; i++) {
1550 blocks[i] = BTRFS_SUPER_INFO_OFFSET + 1024 * 1024 +
1555 * FS features that can be set by other means than -O
1556 * just set the bit here
1559 features |= BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS;
1561 if ((data_profile | metadata_profile) &
1562 (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6)) {
1563 features |= BTRFS_FEATURE_INCOMPAT_RAID56;
1566 process_fs_features(features);
1568 ret = make_btrfs(fd, file, label, fs_uuid, blocks, dev_block_count,
1570 sectorsize, stripesize, features);
1572 fprintf(stderr, "error during mkfs: %s\n", strerror(-ret));
1576 root = open_ctree(file, 0, OPEN_CTREE_WRITES);
1578 fprintf(stderr, "Open ctree failed\n");
1582 root->fs_info->alloc_start = alloc_start;
1584 ret = make_root_dir(root, mixed);
1586 fprintf(stderr, "failed to setup the root directory\n");
1590 trans = btrfs_start_transaction(root, 1);
1592 btrfs_register_one_device(file);
1597 while (dev_cnt-- > 0) {
1598 int old_mixed = mixed;
1600 file = av[optind++];
1603 * open without O_EXCL so that the problem should not
1604 * occur by the following processing.
1605 * (btrfs_register_one_device() fails if O_EXCL is on)
1607 fd = open(file, O_RDWR);
1609 fprintf(stderr, "unable to open %s: %s\n", file,
1613 ret = btrfs_device_already_in_root(root, fd,
1614 BTRFS_SUPER_INFO_OFFSET);
1616 fprintf(stderr, "skipping duplicate device %s in FS\n",
1621 ret = btrfs_prepare_device(fd, file, zero_end, &dev_block_count,
1622 block_count, &mixed, discard);
1629 ret = btrfs_add_to_fsid(trans, root, fd, file, dev_block_count,
1630 sectorsize, sectorsize, sectorsize);
1632 btrfs_register_one_device(file);
1636 if (!source_dir_set) {
1637 ret = create_raid_groups(trans, root, data_profile,
1638 data_profile_opt, metadata_profile,
1643 ret = create_data_reloc_tree(trans, root);
1646 printf("fs created label %s on %s\n\tnodesize %u leafsize %u "
1647 "sectorsize %u size %s\n",
1648 label, first_file, nodesize, leafsize, sectorsize,
1649 pretty_size(btrfs_super_total_bytes(root->fs_info->super_copy)));
1651 btrfs_commit_transaction(trans, root);
1653 if (source_dir_set) {
1654 trans = btrfs_start_transaction(root, 1);
1655 ret = create_chunks(trans, root,
1656 num_of_meta_chunks, size_of_data);
1658 btrfs_commit_transaction(trans, root);
1660 ret = make_image(source_dir, root, fd);
1664 ret = close_ctree(root);