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 struct directory_name_entry {
53 struct list_head list;
56 static int make_root_dir(struct btrfs_root *root, int mixed)
58 struct btrfs_trans_handle *trans;
59 struct btrfs_key location;
65 trans = btrfs_start_transaction(root, 1);
66 bytes_used = btrfs_super_bytes_used(root->fs_info->super_copy);
68 root->fs_info->system_allocs = 1;
69 ret = btrfs_make_block_group(trans, root, bytes_used,
70 BTRFS_BLOCK_GROUP_SYSTEM,
71 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
72 0, BTRFS_MKFS_SYSTEM_GROUP_SIZE);
76 ret = btrfs_alloc_chunk(trans, root->fs_info->extent_root,
77 &chunk_start, &chunk_size,
78 BTRFS_BLOCK_GROUP_METADATA |
79 BTRFS_BLOCK_GROUP_DATA);
81 ret = btrfs_make_block_group(trans, root, 0,
82 BTRFS_BLOCK_GROUP_METADATA |
83 BTRFS_BLOCK_GROUP_DATA,
84 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
85 chunk_start, chunk_size);
87 printf("Created a data/metadata chunk of size %llu\n", chunk_size);
89 ret = btrfs_alloc_chunk(trans, root->fs_info->extent_root,
90 &chunk_start, &chunk_size,
91 BTRFS_BLOCK_GROUP_METADATA);
93 ret = btrfs_make_block_group(trans, root, 0,
94 BTRFS_BLOCK_GROUP_METADATA,
95 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
96 chunk_start, chunk_size);
100 root->fs_info->system_allocs = 0;
101 btrfs_commit_transaction(trans, root);
102 trans = btrfs_start_transaction(root, 1);
106 ret = btrfs_alloc_chunk(trans, root->fs_info->extent_root,
107 &chunk_start, &chunk_size,
108 BTRFS_BLOCK_GROUP_DATA);
110 ret = btrfs_make_block_group(trans, root, 0,
111 BTRFS_BLOCK_GROUP_DATA,
112 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
113 chunk_start, chunk_size);
117 ret = btrfs_make_root_dir(trans, root->fs_info->tree_root,
118 BTRFS_ROOT_TREE_DIR_OBJECTID);
121 ret = btrfs_make_root_dir(trans, root, BTRFS_FIRST_FREE_OBJECTID);
124 memcpy(&location, &root->fs_info->fs_root->root_key, sizeof(location));
125 location.offset = (u64)-1;
126 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
128 btrfs_super_root_dir(root->fs_info->super_copy),
129 &location, BTRFS_FT_DIR, 0);
133 ret = btrfs_insert_inode_ref(trans, root->fs_info->tree_root,
134 "default", 7, location.objectid,
135 BTRFS_ROOT_TREE_DIR_OBJECTID, 0);
139 btrfs_commit_transaction(trans, root);
144 static void __recow_root(struct btrfs_trans_handle *trans,
145 struct btrfs_root *root)
148 struct extent_buffer *tmp;
150 if (trans->transid != btrfs_root_generation(&root->root_item)) {
151 ret = __btrfs_cow_block(trans, root, root->node,
152 NULL, 0, &tmp, 0, 0);
154 free_extent_buffer(tmp);
158 static void recow_roots(struct btrfs_trans_handle *trans,
159 struct btrfs_root *root)
161 struct btrfs_fs_info *info = root->fs_info;
163 __recow_root(trans, info->fs_root);
164 __recow_root(trans, info->tree_root);
165 __recow_root(trans, info->extent_root);
166 __recow_root(trans, info->chunk_root);
167 __recow_root(trans, info->dev_root);
168 __recow_root(trans, info->csum_root);
171 static int create_one_raid_group(struct btrfs_trans_handle *trans,
172 struct btrfs_root *root, u64 type)
178 ret = btrfs_alloc_chunk(trans, root->fs_info->extent_root,
179 &chunk_start, &chunk_size, type);
181 ret = btrfs_make_block_group(trans, root->fs_info->extent_root, 0,
182 type, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
183 chunk_start, chunk_size);
188 static int create_raid_groups(struct btrfs_trans_handle *trans,
189 struct btrfs_root *root, u64 data_profile,
190 int data_profile_opt, u64 metadata_profile,
191 int metadata_profile_opt, int mixed, int ssd)
193 u64 num_devices = btrfs_super_num_devices(root->fs_info->super_copy);
196 if (metadata_profile) {
197 u64 meta_flags = BTRFS_BLOCK_GROUP_METADATA;
199 ret = create_one_raid_group(trans, root,
200 BTRFS_BLOCK_GROUP_SYSTEM |
205 meta_flags |= BTRFS_BLOCK_GROUP_DATA;
207 ret = create_one_raid_group(trans, root, meta_flags |
212 if (!mixed && num_devices > 1 && data_profile) {
213 ret = create_one_raid_group(trans, root,
214 BTRFS_BLOCK_GROUP_DATA |
218 recow_roots(trans, root);
223 static int create_data_reloc_tree(struct btrfs_trans_handle *trans,
224 struct btrfs_root *root)
226 struct btrfs_key location;
227 struct btrfs_root_item root_item;
228 struct extent_buffer *tmp;
229 u64 objectid = BTRFS_DATA_RELOC_TREE_OBJECTID;
232 ret = btrfs_copy_root(trans, root, root->node, &tmp, objectid);
235 memcpy(&root_item, &root->root_item, sizeof(root_item));
236 btrfs_set_root_bytenr(&root_item, tmp->start);
237 btrfs_set_root_level(&root_item, btrfs_header_level(tmp));
238 btrfs_set_root_generation(&root_item, trans->transid);
239 free_extent_buffer(tmp);
241 location.objectid = objectid;
242 location.type = BTRFS_ROOT_ITEM_KEY;
244 ret = btrfs_insert_root(trans, root->fs_info->tree_root,
245 &location, &root_item);
250 static void print_usage(void)
252 fprintf(stderr, "usage: mkfs.btrfs [options] dev [ dev ... ]\n");
253 fprintf(stderr, "options:\n");
254 fprintf(stderr, "\t -A --alloc-start the offset to start the FS\n");
255 fprintf(stderr, "\t -b --byte-count total number of bytes in the FS\n");
256 fprintf(stderr, "\t -d --data data profile, raid0, raid1, raid5, raid6, raid10, dup or single\n");
257 fprintf(stderr, "\t -f --force force overwrite of existing filesystem\n");
258 fprintf(stderr, "\t -l --leafsize size of btree leaves\n");
259 fprintf(stderr, "\t -L --label set a label\n");
260 fprintf(stderr, "\t -m --metadata metadata profile, values like data profile\n");
261 fprintf(stderr, "\t -M --mixed mix metadata and data together\n");
262 fprintf(stderr, "\t -n --nodesize size of btree nodes\n");
263 fprintf(stderr, "\t -s --sectorsize min block allocation (may not mountable by current kernel)\n");
264 fprintf(stderr, "\t -r --rootdir the source directory\n");
265 fprintf(stderr, "\t -K --nodiscard do not perform whole device TRIM\n");
266 fprintf(stderr, "\t -O --features comma separated list of filesystem features\n");
267 fprintf(stderr, "\t -V --version print the mkfs.btrfs version and exit\n");
268 fprintf(stderr, "%s\n", BTRFS_BUILD_VERSION);
272 static void print_version(void)
274 fprintf(stderr, "mkfs.btrfs, part of %s\n", BTRFS_BUILD_VERSION);
278 static u64 parse_profile(char *s)
280 if (strcmp(s, "raid0") == 0) {
281 return BTRFS_BLOCK_GROUP_RAID0;
282 } else if (strcmp(s, "raid1") == 0) {
283 return BTRFS_BLOCK_GROUP_RAID1;
284 } else if (strcmp(s, "raid5") == 0) {
285 return BTRFS_BLOCK_GROUP_RAID5;
286 } else if (strcmp(s, "raid6") == 0) {
287 return BTRFS_BLOCK_GROUP_RAID6;
288 } else if (strcmp(s, "raid10") == 0) {
289 return BTRFS_BLOCK_GROUP_RAID10;
290 } else if (strcmp(s, "dup") == 0) {
291 return BTRFS_BLOCK_GROUP_DUP;
292 } else if (strcmp(s, "single") == 0) {
295 fprintf(stderr, "Unknown profile %s\n", s);
302 static char *parse_label(char *input)
304 int len = strlen(input);
306 if (len >= BTRFS_LABEL_SIZE) {
307 fprintf(stderr, "Label %s is too long (max %d)\n", input,
308 BTRFS_LABEL_SIZE - 1);
311 return strdup(input);
314 static struct option long_options[] = {
315 { "alloc-start", 1, NULL, 'A'},
316 { "byte-count", 1, NULL, 'b' },
317 { "force", 0, NULL, 'f' },
318 { "leafsize", 1, NULL, 'l' },
319 { "label", 1, NULL, 'L'},
320 { "metadata", 1, NULL, 'm' },
321 { "mixed", 0, NULL, 'M' },
322 { "nodesize", 1, NULL, 'n' },
323 { "sectorsize", 1, NULL, 's' },
324 { "data", 1, NULL, 'd' },
325 { "version", 0, NULL, 'V' },
326 { "rootdir", 1, NULL, 'r' },
327 { "nodiscard", 0, NULL, 'K' },
328 { "features", 0, NULL, 'O' },
332 static int add_directory_items(struct btrfs_trans_handle *trans,
333 struct btrfs_root *root, u64 objectid,
334 ino_t parent_inum, const char *name,
335 struct stat *st, int *dir_index_cnt)
339 struct btrfs_key location;
342 name_len = strlen(name);
344 location.objectid = objectid;
346 btrfs_set_key_type(&location, BTRFS_INODE_ITEM_KEY);
348 if (S_ISDIR(st->st_mode))
349 filetype = BTRFS_FT_DIR;
350 if (S_ISREG(st->st_mode))
351 filetype = BTRFS_FT_REG_FILE;
352 if (S_ISLNK(st->st_mode))
353 filetype = BTRFS_FT_SYMLINK;
355 ret = btrfs_insert_dir_item(trans, root, name, name_len,
356 parent_inum, &location,
357 filetype, index_cnt);
359 *dir_index_cnt = index_cnt;
365 static int fill_inode_item(struct btrfs_trans_handle *trans,
366 struct btrfs_root *root,
367 struct btrfs_inode_item *dst, struct stat *src)
370 u64 sectorsize = root->sectorsize;
373 * btrfs_inode_item has some reserved fields
374 * and represents on-disk inode entry, so
375 * zero everything to prevent information leak
377 memset(dst, 0, sizeof (*dst));
379 btrfs_set_stack_inode_generation(dst, trans->transid);
380 btrfs_set_stack_inode_size(dst, src->st_size);
381 btrfs_set_stack_inode_nbytes(dst, 0);
382 btrfs_set_stack_inode_block_group(dst, 0);
383 btrfs_set_stack_inode_nlink(dst, src->st_nlink);
384 btrfs_set_stack_inode_uid(dst, src->st_uid);
385 btrfs_set_stack_inode_gid(dst, src->st_gid);
386 btrfs_set_stack_inode_mode(dst, src->st_mode);
387 btrfs_set_stack_inode_rdev(dst, 0);
388 btrfs_set_stack_inode_flags(dst, 0);
389 btrfs_set_stack_timespec_sec(&dst->atime, src->st_atime);
390 btrfs_set_stack_timespec_nsec(&dst->atime, 0);
391 btrfs_set_stack_timespec_sec(&dst->ctime, src->st_ctime);
392 btrfs_set_stack_timespec_nsec(&dst->ctime, 0);
393 btrfs_set_stack_timespec_sec(&dst->mtime, src->st_mtime);
394 btrfs_set_stack_timespec_nsec(&dst->mtime, 0);
395 btrfs_set_stack_timespec_sec(&dst->otime, 0);
396 btrfs_set_stack_timespec_nsec(&dst->otime, 0);
398 if (S_ISDIR(src->st_mode)) {
399 btrfs_set_stack_inode_size(dst, 0);
400 btrfs_set_stack_inode_nlink(dst, 1);
402 if (S_ISREG(src->st_mode)) {
403 btrfs_set_stack_inode_size(dst, (u64)src->st_size);
404 if (src->st_size <= BTRFS_MAX_INLINE_DATA_SIZE(root))
405 btrfs_set_stack_inode_nbytes(dst, src->st_size);
407 blocks = src->st_size / sectorsize;
408 if (src->st_size % sectorsize)
410 blocks *= sectorsize;
411 btrfs_set_stack_inode_nbytes(dst, blocks);
414 if (S_ISLNK(src->st_mode))
415 btrfs_set_stack_inode_nbytes(dst, src->st_size + 1);
420 static int directory_select(const struct direct *entry)
422 if ((strncmp(entry->d_name, ".", entry->d_reclen) == 0) ||
423 (strncmp(entry->d_name, "..", entry->d_reclen) == 0))
429 static void free_namelist(struct direct **files, int count)
436 for (i = 0; i < count; ++i)
441 static u64 calculate_dir_inode_size(char *dirname)
444 struct direct **files, *cur_file;
445 u64 dir_inode_size = 0;
447 count = scandir(dirname, &files, directory_select, NULL);
449 for (i = 0; i < count; i++) {
451 dir_inode_size += strlen(cur_file->d_name);
454 free_namelist(files, count);
457 return dir_inode_size;
460 static int add_inode_items(struct btrfs_trans_handle *trans,
461 struct btrfs_root *root,
462 struct stat *st, char *name,
463 u64 self_objectid, ino_t parent_inum,
464 int dir_index_cnt, struct btrfs_inode_item *inode_ret)
467 struct btrfs_key inode_key;
468 struct btrfs_inode_item btrfs_inode;
473 name_len = strlen(name);
474 fill_inode_item(trans, root, &btrfs_inode, st);
475 objectid = self_objectid;
477 if (S_ISDIR(st->st_mode)) {
478 inode_size = calculate_dir_inode_size(name);
479 btrfs_set_stack_inode_size(&btrfs_inode, inode_size);
482 inode_key.objectid = objectid;
483 inode_key.offset = 0;
484 btrfs_set_key_type(&inode_key, BTRFS_INODE_ITEM_KEY);
486 ret = btrfs_insert_inode(trans, root, objectid, &btrfs_inode);
490 ret = btrfs_insert_inode_ref(trans, root, name, name_len,
491 objectid, parent_inum, dir_index_cnt);
495 *inode_ret = btrfs_inode;
500 static int add_xattr_item(struct btrfs_trans_handle *trans,
501 struct btrfs_root *root, u64 objectid,
502 const char *file_name)
506 char xattr_list[XATTR_LIST_MAX];
508 char cur_value[XATTR_SIZE_MAX];
509 char delimiter = '\0';
510 char *next_location = xattr_list;
512 ret = llistxattr(file_name, xattr_list, XATTR_LIST_MAX);
516 fprintf(stderr, "get a list of xattr failed for %s\n",
523 cur_name = strtok(xattr_list, &delimiter);
524 while (cur_name != NULL) {
525 cur_name_len = strlen(cur_name);
526 next_location += cur_name_len + 1;
528 ret = getxattr(file_name, cur_name, cur_value, XATTR_SIZE_MAX);
532 fprintf(stderr, "get a xattr value failed for %s attr %s\n",
533 file_name, cur_name);
537 ret = btrfs_insert_xattr_item(trans, root, cur_name,
538 cur_name_len, cur_value,
541 fprintf(stderr, "insert a xattr item failed for %s\n",
545 cur_name = strtok(next_location, &delimiter);
550 static int custom_alloc_extent(struct btrfs_root *root, u64 num_bytes,
551 u64 hint_byte, struct btrfs_key *ins)
555 u64 last = hint_byte;
558 struct btrfs_block_group_cache *cache;
561 ret = find_first_extent_bit(&root->fs_info->free_space_cache,
562 last, &start, &end, EXTENT_DIRTY);
564 if (wrapped++ == 0) {
572 start = max(last, start);
574 if (last - start < num_bytes)
577 last = start + num_bytes;
578 if (test_range_bit(&root->fs_info->pinned_extents,
579 start, last - 1, EXTENT_DIRTY, 0))
582 cache = btrfs_lookup_block_group(root->fs_info, start);
584 if (cache->flags & BTRFS_BLOCK_GROUP_SYSTEM ||
585 last > cache->key.objectid + cache->key.offset) {
586 last = cache->key.objectid + cache->key.offset;
590 if (cache->flags & (BTRFS_BLOCK_GROUP_SYSTEM |
591 BTRFS_BLOCK_GROUP_METADATA)) {
592 last = cache->key.objectid + cache->key.offset;
596 clear_extent_dirty(&root->fs_info->free_space_cache,
597 start, start + num_bytes - 1, 0);
599 ins->objectid = start;
600 ins->offset = num_bytes;
601 ins->type = BTRFS_EXTENT_ITEM_KEY;
605 fprintf(stderr, "not enough free space\n");
609 static int record_file_extent(struct btrfs_trans_handle *trans,
610 struct btrfs_root *root, u64 objectid,
611 struct btrfs_inode_item *inode,
612 u64 file_pos, u64 disk_bytenr,
616 struct btrfs_fs_info *info = root->fs_info;
617 struct btrfs_root *extent_root = info->extent_root;
618 struct extent_buffer *leaf;
619 struct btrfs_file_extent_item *fi;
620 struct btrfs_key ins_key;
621 struct btrfs_path path;
622 struct btrfs_extent_item *ei;
624 btrfs_init_path(&path);
626 ins_key.objectid = objectid;
628 btrfs_set_key_type(&ins_key, BTRFS_EXTENT_DATA_KEY);
629 ret = btrfs_insert_empty_item(trans, root, &path, &ins_key,
633 leaf = path.nodes[0];
634 fi = btrfs_item_ptr(leaf, path.slots[0],
635 struct btrfs_file_extent_item);
636 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
637 btrfs_set_file_extent_type(leaf, fi, BTRFS_FILE_EXTENT_REG);
638 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
639 btrfs_set_file_extent_disk_num_bytes(leaf, fi, num_bytes);
640 btrfs_set_file_extent_offset(leaf, fi, 0);
641 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
642 btrfs_set_file_extent_ram_bytes(leaf, fi, num_bytes);
643 btrfs_set_file_extent_compression(leaf, fi, 0);
644 btrfs_set_file_extent_encryption(leaf, fi, 0);
645 btrfs_set_file_extent_other_encoding(leaf, fi, 0);
646 btrfs_mark_buffer_dirty(leaf);
648 btrfs_release_path(&path);
650 ins_key.objectid = disk_bytenr;
651 ins_key.offset = num_bytes;
652 ins_key.type = BTRFS_EXTENT_ITEM_KEY;
654 ret = btrfs_insert_empty_item(trans, extent_root, &path,
655 &ins_key, sizeof(*ei));
657 leaf = path.nodes[0];
658 ei = btrfs_item_ptr(leaf, path.slots[0],
659 struct btrfs_extent_item);
661 btrfs_set_extent_refs(leaf, ei, 0);
662 btrfs_set_extent_generation(leaf, ei, trans->transid);
663 btrfs_set_extent_flags(leaf, ei, BTRFS_EXTENT_FLAG_DATA);
665 btrfs_mark_buffer_dirty(leaf);
666 ret = btrfs_update_block_group(trans, root, disk_bytenr,
670 } else if (ret != -EEXIST) {
674 ret = btrfs_inc_extent_ref(trans, root, disk_bytenr, num_bytes, 0,
675 root->root_key.objectid,
678 btrfs_release_path(&path);
682 static int add_symbolic_link(struct btrfs_trans_handle *trans,
683 struct btrfs_root *root,
684 u64 objectid, const char *path_name)
687 u64 sectorsize = root->sectorsize;
688 char *buf = malloc(sectorsize);
690 ret = readlink(path_name, buf, sectorsize);
692 fprintf(stderr, "readlink failed for %s\n", path_name);
695 if (ret >= sectorsize) {
696 fprintf(stderr, "symlink too long for %s", path_name);
701 buf[ret] = '\0'; /* readlink does not do it for us */
702 ret = btrfs_insert_inline_extent(trans, root, objectid, 0,
709 static int add_file_items(struct btrfs_trans_handle *trans,
710 struct btrfs_root *root,
711 struct btrfs_inode_item *btrfs_inode, u64 objectid,
712 ino_t parent_inum, struct stat *st,
713 const char *path_name, int out_fd)
719 struct btrfs_key key;
721 u32 sectorsize = root->sectorsize;
726 fd = open(path_name, O_RDONLY);
728 fprintf(stderr, "%s open failed\n", path_name);
732 blocks = st->st_size / sectorsize;
733 if (st->st_size % sectorsize)
736 if (st->st_size <= BTRFS_MAX_INLINE_DATA_SIZE(root)) {
737 buffer = malloc(st->st_size);
738 ret_read = pread64(fd, buffer, st->st_size, bytes_read);
739 if (ret_read == -1) {
740 fprintf(stderr, "%s read failed\n", path_name);
744 ret = btrfs_insert_inline_extent(trans, root, objectid, 0,
745 buffer, st->st_size);
749 ret = custom_alloc_extent(root, blocks * sectorsize, 0, &key);
753 first_block = key.objectid;
755 buffer = malloc(sectorsize);
758 memset(buffer, 0, sectorsize);
759 ret_read = pread64(fd, buffer, sectorsize, bytes_read);
760 if (ret_read == -1) {
761 fprintf(stderr, "%s read failed\n", path_name);
765 ret = pwrite64(out_fd, buffer, sectorsize,
766 first_block + bytes_read);
767 if (ret != sectorsize) {
768 fprintf(stderr, "output file write failed\n");
772 /* checksum for file data */
773 ret = btrfs_csum_file_block(trans, root->fs_info->csum_root,
774 first_block + (blocks * sectorsize),
775 first_block + bytes_read,
778 fprintf(stderr, "%s checksum failed\n", path_name);
782 bytes_read += ret_read;
784 } while (ret_read == sectorsize);
786 if (num_blocks > 0) {
787 ret = record_file_extent(trans, root, objectid, btrfs_inode,
788 first_block, first_block,
789 blocks * sectorsize);
801 static char *make_path(char *dir, char *name)
805 path = malloc(strlen(dir) + strlen(name) + 2);
809 if (dir[strlen(dir) - 1] != '/')
815 static int traverse_directory(struct btrfs_trans_handle *trans,
816 struct btrfs_root *root, char *dir_name,
817 struct directory_name_entry *dir_head, int out_fd)
821 struct btrfs_inode_item cur_inode;
822 struct btrfs_inode_item *inode_item;
823 int count, i, dir_index_cnt;
824 struct direct **files;
826 struct directory_name_entry *dir_entry, *parent_dir_entry;
827 struct direct *cur_file;
828 ino_t parent_inum, cur_inum;
829 ino_t highest_inum = 0;
830 char *parent_dir_name;
831 struct btrfs_path path;
832 struct extent_buffer *leaf;
833 struct btrfs_key root_dir_key;
834 u64 root_dir_inode_size = 0;
836 /* Add list for source directory */
837 dir_entry = malloc(sizeof(struct directory_name_entry));
838 dir_entry->dir_name = dir_name;
839 dir_entry->path = strdup(dir_name);
841 parent_inum = highest_inum + BTRFS_FIRST_FREE_OBJECTID;
842 dir_entry->inum = parent_inum;
843 list_add_tail(&dir_entry->list, &dir_head->list);
845 btrfs_init_path(&path);
847 root_dir_key.objectid = btrfs_root_dirid(&root->root_item);
848 root_dir_key.offset = 0;
849 btrfs_set_key_type(&root_dir_key, BTRFS_INODE_ITEM_KEY);
850 ret = btrfs_lookup_inode(trans, root, &path, &root_dir_key, 1);
852 fprintf(stderr, "root dir lookup error\n");
856 leaf = path.nodes[0];
857 inode_item = btrfs_item_ptr(leaf, path.slots[0],
858 struct btrfs_inode_item);
860 root_dir_inode_size = calculate_dir_inode_size(dir_name);
861 btrfs_set_inode_size(leaf, inode_item, root_dir_inode_size);
862 btrfs_mark_buffer_dirty(leaf);
864 btrfs_release_path(&path);
867 parent_dir_entry = list_entry(dir_head->list.next,
868 struct directory_name_entry,
870 list_del(&parent_dir_entry->list);
872 parent_inum = parent_dir_entry->inum;
873 parent_dir_name = parent_dir_entry->dir_name;
874 if (chdir(parent_dir_entry->path)) {
875 fprintf(stderr, "chdir error for %s\n",
880 count = scandir(parent_dir_entry->path, &files,
881 directory_select, NULL);
884 fprintf(stderr, "scandir for %s failed: %s\n",
885 parent_dir_name, strerror (errno));
889 for (i = 0; i < count; i++) {
892 if (lstat(cur_file->d_name, &st) == -1) {
893 fprintf(stderr, "lstat failed for file %s\n",
898 cur_inum = ++highest_inum + BTRFS_FIRST_FREE_OBJECTID;
899 ret = add_directory_items(trans, root,
900 cur_inum, parent_inum,
902 &st, &dir_index_cnt);
904 fprintf(stderr, "add_directory_items failed\n");
908 ret = add_inode_items(trans, root, &st,
909 cur_file->d_name, cur_inum,
910 parent_inum, dir_index_cnt,
913 fprintf(stderr, "add_inode_items failed\n");
917 ret = add_xattr_item(trans, root,
918 cur_inum, cur_file->d_name);
920 fprintf(stderr, "add_xattr_item failed\n");
925 if (S_ISDIR(st.st_mode)) {
926 dir_entry = malloc(sizeof(struct directory_name_entry));
927 dir_entry->dir_name = cur_file->d_name;
928 dir_entry->path = make_path(parent_dir_entry->path,
930 dir_entry->inum = cur_inum;
931 list_add_tail(&dir_entry->list, &dir_head->list);
932 } else if (S_ISREG(st.st_mode)) {
933 ret = add_file_items(trans, root, &cur_inode,
934 cur_inum, parent_inum, &st,
935 cur_file->d_name, out_fd);
937 fprintf(stderr, "add_file_items failed\n");
940 } else if (S_ISLNK(st.st_mode)) {
941 ret = add_symbolic_link(trans, root,
942 cur_inum, cur_file->d_name);
944 fprintf(stderr, "add_symbolic_link failed\n");
950 free_namelist(files, count);
951 free(parent_dir_entry->path);
952 free(parent_dir_entry);
956 } while (!list_empty(&dir_head->list));
960 free_namelist(files, count);
962 free(parent_dir_entry->path);
963 free(parent_dir_entry);
967 static int open_target(char *output_name)
970 output_fd = open(output_name, O_CREAT | O_RDWR | O_TRUNC,
971 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH);
976 static int create_chunks(struct btrfs_trans_handle *trans,
977 struct btrfs_root *root, u64 num_of_meta_chunks,
982 u64 meta_type = BTRFS_BLOCK_GROUP_METADATA;
983 u64 data_type = BTRFS_BLOCK_GROUP_DATA;
984 u64 minimum_data_chunk_size = 8 * 1024 * 1024;
988 for (i = 0; i < num_of_meta_chunks; i++) {
989 ret = btrfs_alloc_chunk(trans, root->fs_info->extent_root,
990 &chunk_start, &chunk_size, meta_type);
992 ret = btrfs_make_block_group(trans, root->fs_info->extent_root, 0,
993 meta_type, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
994 chunk_start, chunk_size);
996 set_extent_dirty(&root->fs_info->free_space_cache,
997 chunk_start, chunk_start + chunk_size - 1, 0);
1000 if (size_of_data < minimum_data_chunk_size)
1001 size_of_data = minimum_data_chunk_size;
1002 ret = btrfs_alloc_data_chunk(trans, root->fs_info->extent_root,
1003 &chunk_start, size_of_data, data_type);
1005 ret = btrfs_make_block_group(trans, root->fs_info->extent_root, 0,
1006 data_type, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
1007 chunk_start, size_of_data);
1009 set_extent_dirty(&root->fs_info->free_space_cache,
1010 chunk_start, chunk_start + size_of_data - 1, 0);
1014 static int make_image(char *source_dir, struct btrfs_root *root, int out_fd)
1017 struct btrfs_trans_handle *trans;
1019 struct stat root_st;
1021 struct directory_name_entry dir_head;
1023 ret = lstat(source_dir, &root_st);
1025 fprintf(stderr, "unable to lstat the %s\n", source_dir);
1029 INIT_LIST_HEAD(&dir_head.list);
1031 trans = btrfs_start_transaction(root, 1);
1032 ret = traverse_directory(trans, root, source_dir, &dir_head, out_fd);
1034 fprintf(stderr, "unable to traverse_directory\n");
1037 btrfs_commit_transaction(trans, root);
1039 printf("Making image is completed.\n");
1042 fprintf(stderr, "Making image is aborted.\n");
1047 * This ignores symlinks with unreadable targets and subdirs that can't
1048 * be read. It's a best-effort to give a rough estimate of the size of
1049 * a subdir. It doesn't guarantee that prepopulating btrfs from this
1050 * tree won't still run out of space.
1052 * The rounding up to 4096 is questionable. Previous code used du -B 4096.
1054 static u64 global_total_size;
1055 static int ftw_add_entry_size(const char *fpath, const struct stat *st,
1058 if (type == FTW_F || type == FTW_D)
1059 global_total_size += round_up(st->st_size, 4096);
1064 static u64 size_sourcedir(char *dir_name, u64 sectorsize,
1065 u64 *num_of_meta_chunks_ret, u64 *size_of_data_ret)
1070 u64 default_chunk_size = 8 * 1024 * 1024; /* 8MB */
1071 u64 allocated_meta_size = 8 * 1024 * 1024; /* 8MB */
1072 u64 allocated_total_size = 20 * 1024 * 1024; /* 20MB */
1073 u64 num_of_meta_chunks = 0;
1074 u64 num_of_allocated_meta_chunks =
1075 allocated_meta_size / default_chunk_size;
1077 global_total_size = 0;
1078 ret = ftw(dir_name, ftw_add_entry_size, 10);
1079 dir_size = global_total_size;
1081 fprintf(stderr, "ftw subdir walk of '%s' failed: %s\n",
1082 dir_name, strerror(errno));
1086 num_of_meta_chunks = (dir_size / 2) / default_chunk_size;
1087 if (((dir_size / 2) % default_chunk_size) != 0)
1088 num_of_meta_chunks++;
1089 if (num_of_meta_chunks <= num_of_allocated_meta_chunks)
1090 num_of_meta_chunks = 0;
1092 num_of_meta_chunks -= num_of_allocated_meta_chunks;
1094 total_size = allocated_total_size + dir_size +
1095 (num_of_meta_chunks * default_chunk_size);
1097 *num_of_meta_chunks_ret = num_of_meta_chunks;
1102 static int zero_output_file(int out_fd, u64 size, u32 sectorsize)
1104 int len = sectorsize;
1105 int loop_num = size / sectorsize;
1107 char *buf = malloc(len);
1113 memset(buf, 0, len);
1114 for (i = 0; i < loop_num; i++) {
1115 written = pwrite64(out_fd, buf, len, location);
1118 location += sectorsize;
1124 static int check_leaf_or_node_size(u32 size, u32 sectorsize)
1126 if (size < sectorsize) {
1128 "Illegal leafsize (or nodesize) %u (smaller than %u)\n",
1131 } else if (size > BTRFS_MAX_METADATA_BLOCKSIZE) {
1133 "Illegal leafsize (or nodesize) %u (larger than %u)\n",
1134 size, BTRFS_MAX_METADATA_BLOCKSIZE);
1136 } else if (size & (sectorsize - 1)) {
1138 "Illegal leafsize (or nodesize) %u (not align to %u)\n",
1145 static int is_ssd(const char *file)
1149 char sysfs_path[PATH_MAX];
1154 probe = blkid_new_probe_from_filename(file);
1158 /* Device number of this disk (possibly a partition) */
1159 devno = blkid_probe_get_devno(probe);
1163 /* Get whole disk name (not full path) for this devno */
1164 blkid_devno_to_wholedisk(devno, wholedisk, sizeof(wholedisk), NULL);
1166 snprintf(sysfs_path, PATH_MAX, "/sys/block/%s/queue/rotational",
1169 blkid_free_probe(probe);
1171 fd = open(sysfs_path, O_RDONLY);
1176 if (read(fd, &rotational, sizeof(char)) < sizeof(char)) {
1182 return !atoi((const char *)&rotational);
1185 #define BTRFS_FEATURE_LIST_ALL (1ULL << 63)
1187 static const struct btrfs_fs_feature {
1191 } mkfs_features[] = {
1192 { "mixed-bg", BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS,
1193 "mixed data and metadata block groups" },
1194 { "extref", BTRFS_FEATURE_INCOMPAT_EXTENDED_IREF,
1195 "increased hardlink limit per file to 65536" },
1196 { "raid56", BTRFS_FEATURE_INCOMPAT_RAID56,
1197 "raid56 extended format" },
1198 { "skinny-metadata", BTRFS_FEATURE_INCOMPAT_SKINNY_METADATA,
1199 "reduced-size metadata extent refs" },
1200 /* Keep this one last */
1201 { "list-all", BTRFS_FEATURE_LIST_ALL, NULL }
1204 static void list_all_fs_features(void)
1208 fprintf(stderr, "Filesystem features available at mkfs time:\n");
1209 for (i = 0; i < ARRAY_SIZE(mkfs_features) - 1; i++) {
1210 fprintf(stderr, "%-20s- %s (0x%llx)\n",
1211 mkfs_features[i].name,
1212 mkfs_features[i].desc,
1213 mkfs_features[i].flag);
1217 static int parse_one_fs_feature(const char *name, u64 *flags)
1222 for (i = 0; i < ARRAY_SIZE(mkfs_features); i++) {
1223 if (!strcmp(mkfs_features[i].name, name)) {
1224 *flags |= mkfs_features[i].flag;
1232 static void process_fs_features(u64 flags)
1236 for (i = 0; i < ARRAY_SIZE(mkfs_features); i++) {
1237 if (flags & mkfs_features[i].flag) {
1239 "Turning ON incompat feature '%s': %s\n",
1240 mkfs_features[i].name,
1241 mkfs_features[i].desc);
1248 * Return NULL if all features were parsed fine, otherwise return the name of
1249 * the first unparsed.
1251 static char* parse_fs_features(char *namelist, u64 *flags)
1254 char *save_ptr = NULL; /* Satisfy static checkers */
1256 for (this_char = strtok_r(namelist, ",", &save_ptr);
1258 this_char = strtok_r(NULL, ",", &save_ptr)) {
1259 if (parse_one_fs_feature(this_char, flags))
1266 int main(int ac, char **av)
1269 struct btrfs_root *root;
1270 struct btrfs_trans_handle *trans;
1273 u64 block_count = 0;
1274 u64 dev_block_count = 0;
1276 u64 alloc_start = 0;
1277 u64 metadata_profile = 0;
1278 u64 data_profile = 0;
1279 u32 leafsize = sysconf(_SC_PAGESIZE);
1280 u32 sectorsize = 4096;
1281 u32 nodesize = leafsize;
1282 u32 stripesize = 4096;
1284 int option_index = 0;
1289 int data_profile_opt = 0;
1290 int metadata_profile_opt = 0;
1293 int force_overwrite = 0;
1295 char *source_dir = NULL;
1296 int source_dir_set = 0;
1297 u64 num_of_meta_chunks = 0;
1298 u64 size_of_data = 0;
1299 u64 source_dir_size = 0;
1307 c = getopt_long(ac, av, "A:b:fl:n:s:m:d:L:O:r:VMK",
1308 long_options, &option_index);
1313 alloc_start = parse_size(optarg);
1316 force_overwrite = 1;
1319 data_profile = parse_profile(optarg);
1320 data_profile_opt = 1;
1324 nodesize = parse_size(optarg);
1325 leafsize = parse_size(optarg);
1328 label = parse_label(optarg);
1331 metadata_profile = parse_profile(optarg);
1332 metadata_profile_opt = 1;
1338 char *orig = strdup(optarg);
1341 tmp = parse_fs_features(tmp, &features);
1344 "Unrecognized filesystem feature '%s'\n",
1350 if (features & BTRFS_FEATURE_LIST_ALL) {
1351 list_all_fs_features();
1357 sectorsize = parse_size(optarg);
1360 block_count = parse_size(optarg);
1361 if (block_count <= 1024*1024*1024) {
1362 printf("SMALL VOLUME: forcing mixed "
1363 "metadata/data groups\n");
1372 source_dir = optarg;
1382 sectorsize = max(sectorsize, (u32)sysconf(_SC_PAGESIZE));
1383 if (check_leaf_or_node_size(leafsize, sectorsize))
1385 if (check_leaf_or_node_size(nodesize, sectorsize))
1387 saved_optind = optind;
1388 dev_cnt = ac - optind;
1392 if (source_dir_set && dev_cnt > 1) {
1394 "The -r option is limited to a single device\n");
1397 while (dev_cnt-- > 0) {
1398 file = av[optind++];
1399 if (is_block_device(file))
1400 if (test_dev_for_mkfs(file, force_overwrite, estr)) {
1401 fprintf(stderr, "Error: %s", estr);
1406 optind = saved_optind;
1407 dev_cnt = ac - optind;
1409 file = av[optind++];
1412 if (is_vol_small(file)) {
1413 printf("SMALL VOLUME: forcing mixed metadata/data groups\n");
1415 if (metadata_profile != data_profile) {
1416 if (metadata_profile_opt || data_profile_opt) {
1418 "With mixed block groups data and metadata profiles must be the same\n");
1424 * Set default profiles according to number of added devices.
1425 * For mixed groups defaults are single/single.
1428 if (!metadata_profile_opt) {
1429 if (dev_cnt == 1 && ssd)
1430 printf("Detected a SSD, turning off metadata "
1431 "duplication. Mkfs with -m dup if you want to "
1432 "force metadata duplication.\n");
1434 metadata_profile = (dev_cnt > 1) ?
1435 BTRFS_BLOCK_GROUP_RAID1 : (ssd) ?
1436 0: BTRFS_BLOCK_GROUP_DUP;
1438 if (!data_profile_opt) {
1439 data_profile = (dev_cnt > 1) ?
1440 BTRFS_BLOCK_GROUP_RAID0 : 0; /* raid0 or single */
1443 metadata_profile = 0;
1447 ret = test_num_disk_vs_raid(metadata_profile, data_profile,
1448 dev_cnt, mixed, estr);
1450 fprintf(stderr, "Error: %s\n", estr);
1454 /* if we are here that means all devs are good to btrfsify */
1455 printf("\nWARNING! - %s IS EXPERIMENTAL\n", BTRFS_BUILD_VERSION);
1456 printf("WARNING! - see http://btrfs.wiki.kernel.org before using\n\n");
1460 if (!source_dir_set) {
1462 * open without O_EXCL so that the problem should not
1463 * occur by the following processing.
1464 * (btrfs_register_one_device() fails if O_EXCL is on)
1466 fd = open(file, O_RDWR);
1468 fprintf(stderr, "unable to open %s: %s\n", file,
1473 ret = btrfs_prepare_device(fd, file, zero_end, &dev_block_count,
1474 block_count, &mixed, nodiscard);
1475 if (block_count && block_count > dev_block_count) {
1476 fprintf(stderr, "%s is smaller than requested size\n", file);
1480 fd = open_target(file);
1482 fprintf(stderr, "unable to open the %s\n", file);
1487 source_dir_size = size_sourcedir(source_dir, sectorsize,
1488 &num_of_meta_chunks, &size_of_data);
1489 if(block_count < source_dir_size)
1490 block_count = source_dir_size;
1491 ret = zero_output_file(fd, block_count, sectorsize);
1493 fprintf(stderr, "unable to zero the output file\n");
1496 /* our "device" is the new image file */
1497 dev_block_count = block_count;
1501 blocks[0] = BTRFS_SUPER_INFO_OFFSET;
1502 for (i = 1; i < 7; i++) {
1503 blocks[i] = BTRFS_SUPER_INFO_OFFSET + 1024 * 1024 +
1508 * FS features that can be set by other means than -O
1509 * just set the bit here
1512 features |= BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS;
1514 if ((data_profile | metadata_profile) &
1515 (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6)) {
1516 features |= BTRFS_FEATURE_INCOMPAT_RAID56;
1519 process_fs_features(features);
1521 ret = make_btrfs(fd, file, label, blocks, dev_block_count,
1523 sectorsize, stripesize, features);
1525 fprintf(stderr, "error during mkfs: %s\n", strerror(-ret));
1529 root = open_ctree(file, 0, O_RDWR);
1531 fprintf(stderr, "Open ctree failed\n");
1535 root->fs_info->alloc_start = alloc_start;
1537 ret = make_root_dir(root, mixed);
1539 fprintf(stderr, "failed to setup the root directory\n");
1543 trans = btrfs_start_transaction(root, 1);
1548 btrfs_register_one_device(file);
1551 while (dev_cnt-- > 0) {
1552 int old_mixed = mixed;
1554 file = av[optind++];
1557 * open without O_EXCL so that the problem should not
1558 * occur by the following processing.
1559 * (btrfs_register_one_device() fails if O_EXCL is on)
1561 fd = open(file, O_RDWR);
1563 fprintf(stderr, "unable to open %s: %s\n", file,
1567 ret = btrfs_device_already_in_root(root, fd,
1568 BTRFS_SUPER_INFO_OFFSET);
1570 fprintf(stderr, "skipping duplicate device %s in FS\n",
1575 ret = btrfs_prepare_device(fd, file, zero_end, &dev_block_count,
1576 block_count, &mixed, nodiscard);
1580 ret = btrfs_add_to_fsid(trans, root, fd, file, dev_block_count,
1581 sectorsize, sectorsize, sectorsize);
1583 btrfs_register_one_device(file);
1587 if (!source_dir_set) {
1588 ret = create_raid_groups(trans, root, data_profile,
1589 data_profile_opt, metadata_profile,
1590 metadata_profile_opt, mixed, ssd);
1594 ret = create_data_reloc_tree(trans, root);
1597 printf("fs created label %s on %s\n\tnodesize %u leafsize %u "
1598 "sectorsize %u size %s\n",
1599 label, first_file, nodesize, leafsize, sectorsize,
1600 pretty_size(btrfs_super_total_bytes(root->fs_info->super_copy)));
1602 printf("%s\n", BTRFS_BUILD_VERSION);
1603 btrfs_commit_transaction(trans, root);
1605 if (source_dir_set) {
1606 trans = btrfs_start_transaction(root, 1);
1607 ret = create_chunks(trans, root,
1608 num_of_meta_chunks, size_of_data);
1610 btrfs_commit_transaction(trans, root);
1612 ret = make_image(source_dir, root, fd);
1616 ret = close_ctree(root);