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 struct directory_name_entry *dir_entry = NULL;
1025 ret = lstat(source_dir, &root_st);
1027 fprintf(stderr, "unable to lstat the %s\n", source_dir);
1031 INIT_LIST_HEAD(&dir_head.list);
1033 trans = btrfs_start_transaction(root, 1);
1034 ret = traverse_directory(trans, root, source_dir, &dir_head, out_fd);
1036 fprintf(stderr, "unable to traverse_directory\n");
1039 btrfs_commit_transaction(trans, root);
1041 printf("Making image is completed.\n");
1044 while (!list_empty(&dir_head.list)) {
1045 dir_entry = list_entry(dir_head.list.next,
1046 struct directory_name_entry, list);
1047 list_del(&dir_entry->list);
1050 fprintf(stderr, "Making image is aborted.\n");
1055 * This ignores symlinks with unreadable targets and subdirs that can't
1056 * be read. It's a best-effort to give a rough estimate of the size of
1057 * a subdir. It doesn't guarantee that prepopulating btrfs from this
1058 * tree won't still run out of space.
1060 * The rounding up to 4096 is questionable. Previous code used du -B 4096.
1062 static u64 global_total_size;
1063 static int ftw_add_entry_size(const char *fpath, const struct stat *st,
1066 if (type == FTW_F || type == FTW_D)
1067 global_total_size += round_up(st->st_size, 4096);
1072 static u64 size_sourcedir(char *dir_name, u64 sectorsize,
1073 u64 *num_of_meta_chunks_ret, u64 *size_of_data_ret)
1078 u64 default_chunk_size = 8 * 1024 * 1024; /* 8MB */
1079 u64 allocated_meta_size = 8 * 1024 * 1024; /* 8MB */
1080 u64 allocated_total_size = 20 * 1024 * 1024; /* 20MB */
1081 u64 num_of_meta_chunks = 0;
1082 u64 num_of_allocated_meta_chunks =
1083 allocated_meta_size / default_chunk_size;
1085 global_total_size = 0;
1086 ret = ftw(dir_name, ftw_add_entry_size, 10);
1087 dir_size = global_total_size;
1089 fprintf(stderr, "ftw subdir walk of '%s' failed: %s\n",
1090 dir_name, strerror(errno));
1094 num_of_meta_chunks = (dir_size / 2) / default_chunk_size;
1095 if (((dir_size / 2) % default_chunk_size) != 0)
1096 num_of_meta_chunks++;
1097 if (num_of_meta_chunks <= num_of_allocated_meta_chunks)
1098 num_of_meta_chunks = 0;
1100 num_of_meta_chunks -= num_of_allocated_meta_chunks;
1102 total_size = allocated_total_size + dir_size +
1103 (num_of_meta_chunks * default_chunk_size);
1105 *num_of_meta_chunks_ret = num_of_meta_chunks;
1110 static int zero_output_file(int out_fd, u64 size, u32 sectorsize)
1112 int len = sectorsize;
1113 int loop_num = size / sectorsize;
1115 char *buf = malloc(len);
1121 memset(buf, 0, len);
1122 for (i = 0; i < loop_num; i++) {
1123 written = pwrite64(out_fd, buf, len, location);
1126 location += sectorsize;
1132 static int check_leaf_or_node_size(u32 size, u32 sectorsize)
1134 if (size < sectorsize) {
1136 "Illegal leafsize (or nodesize) %u (smaller than %u)\n",
1139 } else if (size > BTRFS_MAX_METADATA_BLOCKSIZE) {
1141 "Illegal leafsize (or nodesize) %u (larger than %u)\n",
1142 size, BTRFS_MAX_METADATA_BLOCKSIZE);
1144 } else if (size & (sectorsize - 1)) {
1146 "Illegal leafsize (or nodesize) %u (not align to %u)\n",
1153 static int is_ssd(const char *file)
1157 char sysfs_path[PATH_MAX];
1163 probe = blkid_new_probe_from_filename(file);
1167 /* Device number of this disk (possibly a partition) */
1168 devno = blkid_probe_get_devno(probe);
1170 blkid_free_probe(probe);
1174 /* Get whole disk name (not full path) for this devno */
1175 ret = blkid_devno_to_wholedisk(devno,
1176 wholedisk, sizeof(wholedisk), NULL);
1178 blkid_free_probe(probe);
1182 snprintf(sysfs_path, PATH_MAX, "/sys/block/%s/queue/rotational",
1185 blkid_free_probe(probe);
1187 fd = open(sysfs_path, O_RDONLY);
1192 if (read(fd, &rotational, sizeof(char)) < sizeof(char)) {
1198 return !atoi((const char *)&rotational);
1201 #define BTRFS_FEATURE_LIST_ALL (1ULL << 63)
1203 static const struct btrfs_fs_feature {
1207 } mkfs_features[] = {
1208 { "mixed-bg", BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS,
1209 "mixed data and metadata block groups" },
1210 { "extref", BTRFS_FEATURE_INCOMPAT_EXTENDED_IREF,
1211 "increased hardlink limit per file to 65536" },
1212 { "raid56", BTRFS_FEATURE_INCOMPAT_RAID56,
1213 "raid56 extended format" },
1214 { "skinny-metadata", BTRFS_FEATURE_INCOMPAT_SKINNY_METADATA,
1215 "reduced-size metadata extent refs" },
1216 /* Keep this one last */
1217 { "list-all", BTRFS_FEATURE_LIST_ALL, NULL }
1220 static void list_all_fs_features(void)
1224 fprintf(stderr, "Filesystem features available at mkfs time:\n");
1225 for (i = 0; i < ARRAY_SIZE(mkfs_features) - 1; i++) {
1226 fprintf(stderr, "%-20s- %s (0x%llx)\n",
1227 mkfs_features[i].name,
1228 mkfs_features[i].desc,
1229 mkfs_features[i].flag);
1233 static int parse_one_fs_feature(const char *name, u64 *flags)
1238 for (i = 0; i < ARRAY_SIZE(mkfs_features); i++) {
1239 if (!strcmp(mkfs_features[i].name, name)) {
1240 *flags |= mkfs_features[i].flag;
1248 static void process_fs_features(u64 flags)
1252 for (i = 0; i < ARRAY_SIZE(mkfs_features); i++) {
1253 if (flags & mkfs_features[i].flag) {
1255 "Turning ON incompat feature '%s': %s\n",
1256 mkfs_features[i].name,
1257 mkfs_features[i].desc);
1264 * Return NULL if all features were parsed fine, otherwise return the name of
1265 * the first unparsed.
1267 static char* parse_fs_features(char *namelist, u64 *flags)
1270 char *save_ptr = NULL; /* Satisfy static checkers */
1272 for (this_char = strtok_r(namelist, ",", &save_ptr);
1274 this_char = strtok_r(NULL, ",", &save_ptr)) {
1275 if (parse_one_fs_feature(this_char, flags))
1282 int main(int ac, char **av)
1285 struct btrfs_root *root;
1286 struct btrfs_trans_handle *trans;
1289 u64 block_count = 0;
1290 u64 dev_block_count = 0;
1292 u64 alloc_start = 0;
1293 u64 metadata_profile = 0;
1294 u64 data_profile = 0;
1295 u32 leafsize = sysconf(_SC_PAGESIZE);
1296 u32 sectorsize = 4096;
1297 u32 nodesize = leafsize;
1298 u32 stripesize = 4096;
1300 int option_index = 0;
1305 int data_profile_opt = 0;
1306 int metadata_profile_opt = 0;
1309 int force_overwrite = 0;
1311 char *source_dir = NULL;
1312 int source_dir_set = 0;
1313 u64 num_of_meta_chunks = 0;
1314 u64 size_of_data = 0;
1315 u64 source_dir_size = 0;
1323 c = getopt_long(ac, av, "A:b:fl:n:s:m:d:L:O:r:VMK",
1324 long_options, &option_index);
1329 alloc_start = parse_size(optarg);
1332 force_overwrite = 1;
1335 data_profile = parse_profile(optarg);
1336 data_profile_opt = 1;
1340 nodesize = parse_size(optarg);
1341 leafsize = parse_size(optarg);
1344 label = parse_label(optarg);
1347 metadata_profile = parse_profile(optarg);
1348 metadata_profile_opt = 1;
1354 char *orig = strdup(optarg);
1357 tmp = parse_fs_features(tmp, &features);
1360 "Unrecognized filesystem feature '%s'\n",
1366 if (features & BTRFS_FEATURE_LIST_ALL) {
1367 list_all_fs_features();
1373 sectorsize = parse_size(optarg);
1376 block_count = parse_size(optarg);
1377 if (block_count <= 1024*1024*1024) {
1378 printf("SMALL VOLUME: forcing mixed "
1379 "metadata/data groups\n");
1388 source_dir = optarg;
1398 sectorsize = max(sectorsize, (u32)sysconf(_SC_PAGESIZE));
1399 if (check_leaf_or_node_size(leafsize, sectorsize))
1401 if (check_leaf_or_node_size(nodesize, sectorsize))
1403 saved_optind = optind;
1404 dev_cnt = ac - optind;
1408 if (source_dir_set && dev_cnt > 1) {
1410 "The -r option is limited to a single device\n");
1413 while (dev_cnt-- > 0) {
1414 file = av[optind++];
1415 if (is_block_device(file))
1416 if (test_dev_for_mkfs(file, force_overwrite, estr)) {
1417 fprintf(stderr, "Error: %s", estr);
1422 optind = saved_optind;
1423 dev_cnt = ac - optind;
1425 file = av[optind++];
1428 if (is_vol_small(file)) {
1429 printf("SMALL VOLUME: forcing mixed metadata/data groups\n");
1431 if (metadata_profile != data_profile) {
1432 if (metadata_profile_opt || data_profile_opt) {
1434 "With mixed block groups data and metadata profiles must be the same\n");
1440 * Set default profiles according to number of added devices.
1441 * For mixed groups defaults are single/single.
1444 if (!metadata_profile_opt) {
1445 if (dev_cnt == 1 && ssd)
1446 printf("Detected a SSD, turning off metadata "
1447 "duplication. Mkfs with -m dup if you want to "
1448 "force metadata duplication.\n");
1450 metadata_profile = (dev_cnt > 1) ?
1451 BTRFS_BLOCK_GROUP_RAID1 : (ssd) ?
1452 0: BTRFS_BLOCK_GROUP_DUP;
1454 if (!data_profile_opt) {
1455 data_profile = (dev_cnt > 1) ?
1456 BTRFS_BLOCK_GROUP_RAID0 : 0; /* raid0 or single */
1459 metadata_profile = 0;
1463 ret = test_num_disk_vs_raid(metadata_profile, data_profile,
1464 dev_cnt, mixed, estr);
1466 fprintf(stderr, "Error: %s\n", estr);
1470 /* if we are here that means all devs are good to btrfsify */
1471 printf("\nWARNING! - %s IS EXPERIMENTAL\n", BTRFS_BUILD_VERSION);
1472 printf("WARNING! - see http://btrfs.wiki.kernel.org before using\n\n");
1476 if (!source_dir_set) {
1478 * open without O_EXCL so that the problem should not
1479 * occur by the following processing.
1480 * (btrfs_register_one_device() fails if O_EXCL is on)
1482 fd = open(file, O_RDWR);
1484 fprintf(stderr, "unable to open %s: %s\n", file,
1489 ret = btrfs_prepare_device(fd, file, zero_end, &dev_block_count,
1490 block_count, &mixed, nodiscard);
1491 if (block_count && block_count > dev_block_count) {
1492 fprintf(stderr, "%s is smaller than requested size\n", file);
1496 fd = open_target(file);
1498 fprintf(stderr, "unable to open the %s\n", file);
1503 source_dir_size = size_sourcedir(source_dir, sectorsize,
1504 &num_of_meta_chunks, &size_of_data);
1505 if(block_count < source_dir_size)
1506 block_count = source_dir_size;
1507 ret = zero_output_file(fd, block_count, sectorsize);
1509 fprintf(stderr, "unable to zero the output file\n");
1512 /* our "device" is the new image file */
1513 dev_block_count = block_count;
1516 /* To create the first block group and chunk 0 in make_btrfs */
1517 if (dev_block_count < BTRFS_MKFS_SYSTEM_GROUP_SIZE) {
1518 fprintf(stderr, "device is too small to make filesystem\n");
1522 blocks[0] = BTRFS_SUPER_INFO_OFFSET;
1523 for (i = 1; i < 7; i++) {
1524 blocks[i] = BTRFS_SUPER_INFO_OFFSET + 1024 * 1024 +
1529 * FS features that can be set by other means than -O
1530 * just set the bit here
1533 features |= BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS;
1535 if ((data_profile | metadata_profile) &
1536 (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6)) {
1537 features |= BTRFS_FEATURE_INCOMPAT_RAID56;
1540 process_fs_features(features);
1542 ret = make_btrfs(fd, file, label, blocks, dev_block_count,
1544 sectorsize, stripesize, features);
1546 fprintf(stderr, "error during mkfs: %s\n", strerror(-ret));
1550 root = open_ctree(file, 0, O_RDWR);
1552 fprintf(stderr, "Open ctree failed\n");
1556 root->fs_info->alloc_start = alloc_start;
1558 ret = make_root_dir(root, mixed);
1560 fprintf(stderr, "failed to setup the root directory\n");
1564 trans = btrfs_start_transaction(root, 1);
1569 btrfs_register_one_device(file);
1572 while (dev_cnt-- > 0) {
1573 int old_mixed = mixed;
1575 file = av[optind++];
1578 * open without O_EXCL so that the problem should not
1579 * occur by the following processing.
1580 * (btrfs_register_one_device() fails if O_EXCL is on)
1582 fd = open(file, O_RDWR);
1584 fprintf(stderr, "unable to open %s: %s\n", file,
1588 ret = btrfs_device_already_in_root(root, fd,
1589 BTRFS_SUPER_INFO_OFFSET);
1591 fprintf(stderr, "skipping duplicate device %s in FS\n",
1596 ret = btrfs_prepare_device(fd, file, zero_end, &dev_block_count,
1597 block_count, &mixed, nodiscard);
1601 ret = btrfs_add_to_fsid(trans, root, fd, file, dev_block_count,
1602 sectorsize, sectorsize, sectorsize);
1604 btrfs_register_one_device(file);
1608 if (!source_dir_set) {
1609 ret = create_raid_groups(trans, root, data_profile,
1610 data_profile_opt, metadata_profile,
1611 metadata_profile_opt, mixed, ssd);
1615 ret = create_data_reloc_tree(trans, root);
1618 printf("fs created label %s on %s\n\tnodesize %u leafsize %u "
1619 "sectorsize %u size %s\n",
1620 label, first_file, nodesize, leafsize, sectorsize,
1621 pretty_size(btrfs_super_total_bytes(root->fs_info->super_copy)));
1623 printf("%s\n", BTRFS_BUILD_VERSION);
1624 btrfs_commit_transaction(trans, root);
1626 if (source_dir_set) {
1627 trans = btrfs_start_transaction(root, 1);
1628 ret = create_chunks(trans, root,
1629 num_of_meta_chunks, size_of_data);
1631 btrfs_commit_transaction(trans, root);
1633 ret = make_image(source_dir, root, fd);
1637 ret = close_ctree(root);