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
23 #include <sys/ioctl.h>
24 #include <sys/mount.h>
30 #include <sys/types.h>
36 #include <uuid/uuid.h>
39 #include <attr/xattr.h>
40 #include "kerncompat.h"
44 #include "transaction.h"
48 static u64 index_cnt = 2;
50 struct directory_name_entry {
54 struct list_head list;
57 static u64 parse_size(char *s)
63 if (!isdigit(s[len - 1])) {
64 c = tolower(s[len - 1]);
75 fprintf(stderr, "Unknown size descriptor %c\n", c);
80 return atol(s) * mult;
83 static int make_root_dir(struct btrfs_root *root, int mixed)
85 struct btrfs_trans_handle *trans;
86 struct btrfs_key location;
92 trans = btrfs_start_transaction(root, 1);
93 bytes_used = btrfs_super_bytes_used(&root->fs_info->super_copy);
95 root->fs_info->system_allocs = 1;
96 ret = btrfs_make_block_group(trans, root, bytes_used,
97 BTRFS_BLOCK_GROUP_SYSTEM,
98 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
99 0, BTRFS_MKFS_SYSTEM_GROUP_SIZE);
103 ret = btrfs_alloc_chunk(trans, root->fs_info->extent_root,
104 &chunk_start, &chunk_size,
105 BTRFS_BLOCK_GROUP_METADATA |
106 BTRFS_BLOCK_GROUP_DATA);
108 ret = btrfs_make_block_group(trans, root, 0,
109 BTRFS_BLOCK_GROUP_METADATA |
110 BTRFS_BLOCK_GROUP_DATA,
111 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
112 chunk_start, chunk_size);
114 printf("Created a data/metadata chunk of size %llu\n", chunk_size);
116 ret = btrfs_alloc_chunk(trans, root->fs_info->extent_root,
117 &chunk_start, &chunk_size,
118 BTRFS_BLOCK_GROUP_METADATA);
120 ret = btrfs_make_block_group(trans, root, 0,
121 BTRFS_BLOCK_GROUP_METADATA,
122 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
123 chunk_start, chunk_size);
127 root->fs_info->system_allocs = 0;
128 btrfs_commit_transaction(trans, root);
129 trans = btrfs_start_transaction(root, 1);
133 ret = btrfs_alloc_chunk(trans, root->fs_info->extent_root,
134 &chunk_start, &chunk_size,
135 BTRFS_BLOCK_GROUP_DATA);
137 ret = btrfs_make_block_group(trans, root, 0,
138 BTRFS_BLOCK_GROUP_DATA,
139 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
140 chunk_start, chunk_size);
144 ret = btrfs_make_root_dir(trans, root->fs_info->tree_root,
145 BTRFS_ROOT_TREE_DIR_OBJECTID);
148 ret = btrfs_make_root_dir(trans, root, BTRFS_FIRST_FREE_OBJECTID);
151 memcpy(&location, &root->fs_info->fs_root->root_key, sizeof(location));
152 location.offset = (u64)-1;
153 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
155 btrfs_super_root_dir(&root->fs_info->super_copy),
156 &location, BTRFS_FT_DIR, 0);
160 ret = btrfs_insert_inode_ref(trans, root->fs_info->tree_root,
161 "default", 7, location.objectid,
162 BTRFS_ROOT_TREE_DIR_OBJECTID, 0);
166 btrfs_commit_transaction(trans, root);
171 static int recow_roots(struct btrfs_trans_handle *trans,
172 struct btrfs_root *root)
175 struct extent_buffer *tmp;
176 struct btrfs_fs_info *info = root->fs_info;
178 ret = __btrfs_cow_block(trans, info->fs_root, info->fs_root->node,
179 NULL, 0, &tmp, 0, 0);
181 free_extent_buffer(tmp);
183 ret = __btrfs_cow_block(trans, info->tree_root, info->tree_root->node,
184 NULL, 0, &tmp, 0, 0);
186 free_extent_buffer(tmp);
188 ret = __btrfs_cow_block(trans, info->extent_root,
189 info->extent_root->node, NULL, 0, &tmp, 0, 0);
191 free_extent_buffer(tmp);
193 ret = __btrfs_cow_block(trans, info->chunk_root, info->chunk_root->node,
194 NULL, 0, &tmp, 0, 0);
196 free_extent_buffer(tmp);
199 ret = __btrfs_cow_block(trans, info->dev_root, info->dev_root->node,
200 NULL, 0, &tmp, 0, 0);
202 free_extent_buffer(tmp);
204 ret = __btrfs_cow_block(trans, info->csum_root, info->csum_root->node,
205 NULL, 0, &tmp, 0, 0);
207 free_extent_buffer(tmp);
212 static int create_one_raid_group(struct btrfs_trans_handle *trans,
213 struct btrfs_root *root, u64 type)
219 ret = btrfs_alloc_chunk(trans, root->fs_info->extent_root,
220 &chunk_start, &chunk_size, type);
222 ret = btrfs_make_block_group(trans, root->fs_info->extent_root, 0,
223 type, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
224 chunk_start, chunk_size);
229 static int create_raid_groups(struct btrfs_trans_handle *trans,
230 struct btrfs_root *root, u64 data_profile,
231 u64 metadata_profile, int mixed)
233 u64 num_devices = btrfs_super_num_devices(&root->fs_info->super_copy);
237 if (num_devices == 1)
238 allowed = BTRFS_BLOCK_GROUP_DUP;
239 else if (num_devices >= 4) {
240 allowed = BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1 |
241 BTRFS_BLOCK_GROUP_RAID10;
243 allowed = BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1;
245 if (allowed & metadata_profile) {
246 u64 meta_flags = BTRFS_BLOCK_GROUP_METADATA;
248 ret = create_one_raid_group(trans, root,
249 BTRFS_BLOCK_GROUP_SYSTEM |
250 (allowed & metadata_profile));
254 meta_flags |= BTRFS_BLOCK_GROUP_DATA;
256 ret = create_one_raid_group(trans, root, meta_flags |
257 (allowed & metadata_profile));
260 ret = recow_roots(trans, root);
263 if (!mixed && num_devices > 1 && (allowed & data_profile)) {
264 ret = create_one_raid_group(trans, root,
265 BTRFS_BLOCK_GROUP_DATA |
266 (allowed & data_profile));
272 static int create_data_reloc_tree(struct btrfs_trans_handle *trans,
273 struct btrfs_root *root)
275 struct btrfs_key location;
276 struct btrfs_root_item root_item;
277 struct extent_buffer *tmp;
278 u64 objectid = BTRFS_DATA_RELOC_TREE_OBJECTID;
281 ret = btrfs_copy_root(trans, root, root->node, &tmp, objectid);
284 memcpy(&root_item, &root->root_item, sizeof(root_item));
285 btrfs_set_root_bytenr(&root_item, tmp->start);
286 btrfs_set_root_level(&root_item, btrfs_header_level(tmp));
287 btrfs_set_root_generation(&root_item, trans->transid);
288 free_extent_buffer(tmp);
290 location.objectid = objectid;
291 location.type = BTRFS_ROOT_ITEM_KEY;
293 ret = btrfs_insert_root(trans, root->fs_info->tree_root,
294 &location, &root_item);
299 static void print_usage(void)
301 fprintf(stderr, "usage: mkfs.btrfs [options] dev [ dev ... ]\n");
302 fprintf(stderr, "options:\n");
303 fprintf(stderr, "\t -A --alloc-start the offset to start the FS\n");
304 fprintf(stderr, "\t -b --byte-count total number of bytes in the FS\n");
305 fprintf(stderr, "\t -d --data data profile, raid0, raid1, raid10 or single\n");
306 fprintf(stderr, "\t -l --leafsize size of btree leaves\n");
307 fprintf(stderr, "\t -L --label set a label\n");
308 fprintf(stderr, "\t -m --metadata metadata profile, values like data profile\n");
309 fprintf(stderr, "\t -M --mixed mix metadata and data together\n");
310 fprintf(stderr, "\t -n --nodesize size of btree nodes\n");
311 fprintf(stderr, "\t -s --sectorsize min block allocation\n");
312 fprintf(stderr, "\t -r --rootdir the source directory\n");
313 fprintf(stderr, "%s\n", BTRFS_BUILD_VERSION);
317 static void print_version(void)
319 fprintf(stderr, "mkfs.btrfs, part of %s\n", BTRFS_BUILD_VERSION);
323 static u64 parse_profile(char *s)
325 if (strcmp(s, "raid0") == 0) {
326 return BTRFS_BLOCK_GROUP_RAID0;
327 } else if (strcmp(s, "raid1") == 0) {
328 return BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_DUP;
329 } else if (strcmp(s, "raid10") == 0) {
330 return BTRFS_BLOCK_GROUP_RAID10 | BTRFS_BLOCK_GROUP_DUP;
331 } else if (strcmp(s, "single") == 0) {
334 fprintf(stderr, "Unknown option %s\n", s);
340 static char *parse_label(char *input)
343 int len = strlen(input);
345 if (len > BTRFS_LABEL_SIZE) {
346 fprintf(stderr, "Label %s is too long (max %d)\n", input,
350 for (i = 0; i < len; i++) {
351 if (input[i] == '/' || input[i] == '\\') {
352 fprintf(stderr, "invalid label %s\n", input);
356 return strdup(input);
359 static struct option long_options[] = {
360 { "alloc-start", 1, NULL, 'A'},
361 { "byte-count", 1, NULL, 'b' },
362 { "leafsize", 1, NULL, 'l' },
363 { "label", 1, NULL, 'L'},
364 { "metadata", 1, NULL, 'm' },
365 { "mixed", 0, NULL, 'M' },
366 { "nodesize", 1, NULL, 'n' },
367 { "sectorsize", 1, NULL, 's' },
368 { "data", 1, NULL, 'd' },
369 { "version", 0, NULL, 'V' },
370 { "rootdir", 1, NULL, 'r' },
374 static int add_directory_items(struct btrfs_trans_handle *trans,
375 struct btrfs_root *root, u64 objectid,
376 ino_t parent_inum, const char *name,
377 struct stat *st, int *dir_index_cnt)
381 struct btrfs_key location;
384 name_len = strlen(name);
386 location.objectid = objectid;
388 btrfs_set_key_type(&location, BTRFS_INODE_ITEM_KEY);
390 if (S_ISDIR(st->st_mode))
391 filetype = BTRFS_FT_DIR;
392 if (S_ISREG(st->st_mode))
393 filetype = BTRFS_FT_REG_FILE;
394 if (S_ISLNK(st->st_mode))
395 filetype = BTRFS_FT_SYMLINK;
397 ret = btrfs_insert_dir_item(trans, root, name, name_len,
398 parent_inum, &location,
399 filetype, index_cnt);
401 *dir_index_cnt = index_cnt;
407 static int fill_inode_item(struct btrfs_trans_handle *trans,
408 struct btrfs_root *root,
409 struct btrfs_inode_item *dst, struct stat *src)
412 u64 sectorsize = root->sectorsize;
415 * btrfs_inode_item has some reserved fields
416 * and represents on-disk inode entry, so
417 * zero everything to prevent information leak
419 memset(dst, 0, sizeof (*dst));
421 btrfs_set_stack_inode_generation(dst, trans->transid);
422 btrfs_set_stack_inode_size(dst, src->st_size);
423 btrfs_set_stack_inode_nbytes(dst, 0);
424 btrfs_set_stack_inode_block_group(dst, 0);
425 btrfs_set_stack_inode_nlink(dst, src->st_nlink);
426 btrfs_set_stack_inode_uid(dst, src->st_uid);
427 btrfs_set_stack_inode_gid(dst, src->st_gid);
428 btrfs_set_stack_inode_mode(dst, src->st_mode);
429 btrfs_set_stack_inode_rdev(dst, 0);
430 btrfs_set_stack_inode_flags(dst, 0);
431 btrfs_set_stack_timespec_sec(&dst->atime, src->st_atime);
432 btrfs_set_stack_timespec_nsec(&dst->atime, 0);
433 btrfs_set_stack_timespec_sec(&dst->ctime, src->st_ctime);
434 btrfs_set_stack_timespec_nsec(&dst->ctime, 0);
435 btrfs_set_stack_timespec_sec(&dst->mtime, src->st_mtime);
436 btrfs_set_stack_timespec_nsec(&dst->mtime, 0);
437 btrfs_set_stack_timespec_sec(&dst->otime, 0);
438 btrfs_set_stack_timespec_nsec(&dst->otime, 0);
440 if (S_ISDIR(src->st_mode)) {
441 btrfs_set_stack_inode_size(dst, 0);
442 btrfs_set_stack_inode_nlink(dst, 1);
444 if (S_ISREG(src->st_mode)) {
445 btrfs_set_stack_inode_size(dst, (u64)src->st_size);
446 if (src->st_size <= BTRFS_MAX_INLINE_DATA_SIZE(root))
447 btrfs_set_stack_inode_nbytes(dst, src->st_size);
449 blocks = src->st_size / sectorsize;
450 if (src->st_size % sectorsize)
452 blocks *= sectorsize;
453 btrfs_set_stack_inode_nbytes(dst, blocks);
456 if (S_ISLNK(src->st_mode))
457 btrfs_set_stack_inode_nbytes(dst, src->st_size + 1);
462 static int directory_select(const struct direct *entry)
464 if ((strncmp(entry->d_name, ".", entry->d_reclen) == 0) ||
465 (strncmp(entry->d_name, "..", entry->d_reclen) == 0))
471 static u64 calculate_dir_inode_size(char *dirname)
474 struct direct **files, *cur_file;
475 u64 dir_inode_size = 0;
477 count = scandir(dirname, &files, directory_select, NULL);
479 for (i = 0; i < count; i++) {
481 dir_inode_size += strlen(cur_file->d_name);
485 return dir_inode_size;
488 static int add_inode_items(struct btrfs_trans_handle *trans,
489 struct btrfs_root *root,
490 struct stat *st, char *name,
491 u64 self_objectid, ino_t parent_inum,
492 int dir_index_cnt, struct btrfs_inode_item *inode_ret)
495 struct btrfs_key inode_key;
496 struct btrfs_inode_item btrfs_inode;
501 name_len = strlen(name);
502 fill_inode_item(trans, root, &btrfs_inode, st);
503 objectid = self_objectid;
505 if (S_ISDIR(st->st_mode)) {
506 inode_size = calculate_dir_inode_size(name);
507 btrfs_set_stack_inode_size(&btrfs_inode, inode_size);
510 inode_key.objectid = objectid;
511 inode_key.offset = 0;
512 btrfs_set_key_type(&inode_key, BTRFS_INODE_ITEM_KEY);
514 ret = btrfs_insert_inode(trans, root, objectid, &btrfs_inode);
518 ret = btrfs_insert_inode_ref(trans, root, name, name_len,
519 objectid, parent_inum, dir_index_cnt);
523 *inode_ret = btrfs_inode;
528 static int add_xattr_item(struct btrfs_trans_handle *trans,
529 struct btrfs_root *root, u64 objectid,
530 const char *file_name)
534 char xattr_list[XATTR_LIST_MAX];
536 char cur_value[XATTR_SIZE_MAX];
537 char delimiter = '\0';
538 char *next_location = xattr_list;
540 ret = llistxattr(file_name, xattr_list, XATTR_LIST_MAX);
542 fprintf(stderr, "get a list of xattr failed for %s\n",
549 cur_name = strtok(xattr_list, &delimiter);
550 while (cur_name != NULL) {
551 cur_name_len = strlen(cur_name);
552 next_location += cur_name_len + 1;
554 ret = getxattr(file_name, cur_name, cur_value, XATTR_SIZE_MAX);
556 fprintf(stderr, "get a xattr value failed for %s\n",
560 ret = btrfs_insert_xattr_item(trans, root, cur_name,
561 cur_name_len, cur_value,
564 fprintf(stderr, "insert a xattr item failed for %s\n",
568 cur_name = strtok(next_location, &delimiter);
574 static int custom_alloc_extent(struct btrfs_root *root, u64 num_bytes,
575 u64 hint_byte, struct btrfs_key *ins)
579 u64 last = hint_byte;
582 struct btrfs_block_group_cache *cache;
585 ret = find_first_extent_bit(&root->fs_info->free_space_cache,
586 last, &start, &end, EXTENT_DIRTY);
588 if (wrapped++ == 0) {
596 start = max(last, start);
598 if (last - start < num_bytes)
601 last = start + num_bytes;
602 if (test_range_bit(&root->fs_info->pinned_extents,
603 start, last - 1, EXTENT_DIRTY, 0))
606 cache = btrfs_lookup_block_group(root->fs_info, start);
608 if (cache->flags & BTRFS_BLOCK_GROUP_SYSTEM ||
609 last > cache->key.objectid + cache->key.offset) {
610 last = cache->key.objectid + cache->key.offset;
614 if (cache->flags & (BTRFS_BLOCK_GROUP_SYSTEM |
615 BTRFS_BLOCK_GROUP_METADATA)) {
616 last = cache->key.objectid + cache->key.offset;
620 clear_extent_dirty(&root->fs_info->free_space_cache,
621 start, start + num_bytes - 1, 0);
623 ins->objectid = start;
624 ins->offset = num_bytes;
625 ins->type = BTRFS_EXTENT_ITEM_KEY;
629 fprintf(stderr, "not enough free space\n");
633 static int record_file_extent(struct btrfs_trans_handle *trans,
634 struct btrfs_root *root, u64 objectid,
635 struct btrfs_inode_item *inode,
636 u64 file_pos, u64 disk_bytenr,
640 struct btrfs_fs_info *info = root->fs_info;
641 struct btrfs_root *extent_root = info->extent_root;
642 struct extent_buffer *leaf;
643 struct btrfs_file_extent_item *fi;
644 struct btrfs_key ins_key;
645 struct btrfs_path path;
646 struct btrfs_extent_item *ei;
648 btrfs_init_path(&path);
650 ins_key.objectid = objectid;
652 btrfs_set_key_type(&ins_key, BTRFS_EXTENT_DATA_KEY);
653 ret = btrfs_insert_empty_item(trans, root, &path, &ins_key,
657 leaf = path.nodes[0];
658 fi = btrfs_item_ptr(leaf, path.slots[0],
659 struct btrfs_file_extent_item);
660 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
661 btrfs_set_file_extent_type(leaf, fi, BTRFS_FILE_EXTENT_REG);
662 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
663 btrfs_set_file_extent_disk_num_bytes(leaf, fi, num_bytes);
664 btrfs_set_file_extent_offset(leaf, fi, 0);
665 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
666 btrfs_set_file_extent_ram_bytes(leaf, fi, num_bytes);
667 btrfs_set_file_extent_compression(leaf, fi, 0);
668 btrfs_set_file_extent_encryption(leaf, fi, 0);
669 btrfs_set_file_extent_other_encoding(leaf, fi, 0);
670 btrfs_mark_buffer_dirty(leaf);
672 btrfs_release_path(root, &path);
674 ins_key.objectid = disk_bytenr;
675 ins_key.offset = num_bytes;
676 ins_key.type = BTRFS_EXTENT_ITEM_KEY;
678 ret = btrfs_insert_empty_item(trans, extent_root, &path,
679 &ins_key, sizeof(*ei));
681 leaf = path.nodes[0];
682 ei = btrfs_item_ptr(leaf, path.slots[0],
683 struct btrfs_extent_item);
685 btrfs_set_extent_refs(leaf, ei, 0);
686 btrfs_set_extent_generation(leaf, ei, trans->transid);
687 btrfs_set_extent_flags(leaf, ei, BTRFS_EXTENT_FLAG_DATA);
689 btrfs_mark_buffer_dirty(leaf);
690 ret = btrfs_update_block_group(trans, root, disk_bytenr,
694 } else if (ret != -EEXIST) {
698 ret = btrfs_inc_extent_ref(trans, root, disk_bytenr, num_bytes, 0,
699 root->root_key.objectid,
702 btrfs_release_path(root, &path);
706 static int add_symbolic_link(struct btrfs_trans_handle *trans,
707 struct btrfs_root *root,
708 u64 objectid, const char *path_name)
711 u64 sectorsize = root->sectorsize;
712 char *buf = malloc(sectorsize);
714 ret = readlink(path_name, buf, sectorsize);
716 fprintf(stderr, "readlink failed for %s\n", path_name);
719 if (ret >= sectorsize) {
720 fprintf(stderr, "symlink too long for %s", path_name);
725 buf[ret] = '\0'; /* readlink does not do it for us */
726 ret = btrfs_insert_inline_extent(trans, root, objectid, 0,
733 static int add_file_items(struct btrfs_trans_handle *trans,
734 struct btrfs_root *root,
735 struct btrfs_inode_item *btrfs_inode, u64 objectid,
736 ino_t parent_inum, struct stat *st,
737 const char *path_name, int out_fd)
743 struct btrfs_key key;
745 u32 sectorsize = root->sectorsize;
750 fd = open(path_name, O_RDONLY);
752 fprintf(stderr, "%s open failed\n", path_name);
756 blocks = st->st_size / sectorsize;
757 if (st->st_size % sectorsize)
760 if (st->st_size <= BTRFS_MAX_INLINE_DATA_SIZE(root)) {
761 buffer = malloc(st->st_size);
762 ret_read = pread64(fd, buffer, st->st_size, bytes_read);
763 if (ret_read == -1) {
764 fprintf(stderr, "%s read failed\n", path_name);
768 ret = btrfs_insert_inline_extent(trans, root, objectid, 0,
769 buffer, st->st_size);
773 ret = custom_alloc_extent(root, blocks * sectorsize, 0, &key);
777 first_block = key.objectid;
779 buffer = malloc(sectorsize);
782 memset(buffer, 0, sectorsize);
783 ret_read = pread64(fd, buffer, sectorsize, bytes_read);
784 if (ret_read == -1) {
785 fprintf(stderr, "%s read failed\n", path_name);
789 ret = pwrite64(out_fd, buffer, sectorsize,
790 first_block + bytes_read);
791 if (ret != sectorsize) {
792 fprintf(stderr, "output file write failed\n");
796 /* checksum for file data */
797 ret = btrfs_csum_file_block(trans, root->fs_info->csum_root,
798 first_block + (blocks * sectorsize),
799 first_block + bytes_read,
802 fprintf(stderr, "%s checksum failed\n", path_name);
806 bytes_read += ret_read;
808 } while (ret_read == sectorsize);
810 if (num_blocks > 0) {
811 ret = record_file_extent(trans, root, objectid, btrfs_inode,
812 first_block, first_block,
813 blocks * sectorsize);
825 static char *make_path(char *dir, char *name)
829 path = malloc(strlen(dir) + strlen(name) + 2);
833 if (dir[strlen(dir) - 1] != '/')
839 static int traverse_directory(struct btrfs_trans_handle *trans,
840 struct btrfs_root *root, char *dir_name,
841 struct directory_name_entry *dir_head, int out_fd)
845 struct btrfs_inode_item cur_inode;
846 struct btrfs_inode_item *inode_item;
847 int count, i, dir_index_cnt;
848 struct direct **files;
850 struct directory_name_entry *dir_entry, *parent_dir_entry;
851 struct direct *cur_file;
852 ino_t parent_inum, cur_inum;
853 ino_t highest_inum = 0;
854 char *parent_dir_name;
855 struct btrfs_path path;
856 struct extent_buffer *leaf;
857 struct btrfs_key root_dir_key;
858 u64 root_dir_inode_size = 0;
860 /* Add list for source directory */
861 dir_entry = malloc(sizeof(struct directory_name_entry));
862 dir_entry->dir_name = dir_name;
863 dir_entry->path = malloc(strlen(dir_name) + 1);
864 strcpy(dir_entry->path, dir_name);
866 parent_inum = highest_inum + BTRFS_FIRST_FREE_OBJECTID;
867 dir_entry->inum = parent_inum;
868 list_add_tail(&dir_entry->list, &dir_head->list);
870 btrfs_init_path(&path);
872 root_dir_key.objectid = btrfs_root_dirid(&root->root_item);
873 root_dir_key.offset = 0;
874 btrfs_set_key_type(&root_dir_key, BTRFS_INODE_ITEM_KEY);
875 ret = btrfs_lookup_inode(trans, root, &path, &root_dir_key, 1);
877 fprintf(stderr, "root dir lookup error\n");
881 leaf = path.nodes[0];
882 inode_item = btrfs_item_ptr(leaf, path.slots[0],
883 struct btrfs_inode_item);
885 root_dir_inode_size = calculate_dir_inode_size(dir_name);
886 btrfs_set_inode_size(leaf, inode_item, root_dir_inode_size);
887 btrfs_mark_buffer_dirty(leaf);
889 btrfs_release_path(root, &path);
892 parent_dir_entry = list_entry(dir_head->list.next,
893 struct directory_name_entry,
895 list_del(&parent_dir_entry->list);
897 parent_inum = parent_dir_entry->inum;
898 parent_dir_name = parent_dir_entry->dir_name;
899 if (chdir(parent_dir_entry->path)) {
900 fprintf(stderr, "chdir error for %s\n",
905 count = scandir(parent_dir_entry->path, &files,
906 directory_select, NULL);
909 fprintf(stderr, "scandir for %s failed: %s\n",
910 parent_dir_name, strerror (errno));
914 for (i = 0; i < count; i++) {
917 if (lstat(cur_file->d_name, &st) == -1) {
918 fprintf(stderr, "lstat failed for file %s\n",
923 cur_inum = ++highest_inum + BTRFS_FIRST_FREE_OBJECTID;
924 ret = add_directory_items(trans, root,
925 cur_inum, parent_inum,
927 &st, &dir_index_cnt);
929 fprintf(stderr, "add_directory_items failed\n");
933 ret = add_inode_items(trans, root, &st,
934 cur_file->d_name, cur_inum,
935 parent_inum, dir_index_cnt,
938 fprintf(stderr, "add_inode_items failed\n");
942 ret = add_xattr_item(trans, root,
943 cur_inum, cur_file->d_name);
945 fprintf(stderr, "add_xattr_item failed\n");
949 if (S_ISDIR(st.st_mode)) {
950 dir_entry = malloc(sizeof(struct directory_name_entry));
951 dir_entry->dir_name = cur_file->d_name;
952 dir_entry->path = make_path(parent_dir_entry->path,
954 dir_entry->inum = cur_inum;
955 list_add_tail(&dir_entry->list, &dir_head->list);
956 } else if (S_ISREG(st.st_mode)) {
957 ret = add_file_items(trans, root, &cur_inode,
958 cur_inum, parent_inum, &st,
959 cur_file->d_name, out_fd);
961 fprintf(stderr, "add_file_items failed\n");
964 } else if (S_ISLNK(st.st_mode)) {
965 ret = add_symbolic_link(trans, root,
966 cur_inum, cur_file->d_name);
968 fprintf(stderr, "add_symbolic_link failed\n");
974 free(parent_dir_entry->path);
975 free(parent_dir_entry);
979 } while (!list_empty(&dir_head->list));
983 free(parent_dir_entry->path);
984 free(parent_dir_entry);
988 static int open_target(char *output_name)
991 output_fd = open(output_name, O_CREAT | O_RDWR | O_TRUNC,
992 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH);
997 static int create_chunks(struct btrfs_trans_handle *trans,
998 struct btrfs_root *root, u64 num_of_meta_chunks,
1003 u64 meta_type = BTRFS_BLOCK_GROUP_METADATA;
1004 u64 data_type = BTRFS_BLOCK_GROUP_DATA;
1005 u64 minimum_data_chunk_size = 64 * 1024 * 1024;
1009 for (i = 0; i < num_of_meta_chunks; i++) {
1010 ret = btrfs_alloc_chunk(trans, root->fs_info->extent_root,
1011 &chunk_start, &chunk_size, meta_type);
1013 ret = btrfs_make_block_group(trans, root->fs_info->extent_root, 0,
1014 meta_type, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
1015 chunk_start, chunk_size);
1017 set_extent_dirty(&root->fs_info->free_space_cache,
1018 chunk_start, chunk_start + chunk_size - 1, 0);
1021 if (size_of_data < minimum_data_chunk_size)
1022 size_of_data = minimum_data_chunk_size;
1023 ret = btrfs_alloc_data_chunk(trans, root->fs_info->extent_root,
1024 &chunk_start, size_of_data, data_type);
1026 ret = btrfs_make_block_group(trans, root->fs_info->extent_root, 0,
1027 data_type, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
1028 chunk_start, size_of_data);
1030 set_extent_dirty(&root->fs_info->free_space_cache,
1031 chunk_start, chunk_start + size_of_data - 1, 0);
1035 static int make_image(char *source_dir, struct btrfs_root *root, int out_fd)
1038 struct btrfs_trans_handle *trans;
1040 struct stat root_st;
1043 struct directory_name_entry dir_head;
1045 ret = lstat(source_dir, &root_st);
1047 fprintf(stderr, "unable to lstat the %s\n", source_dir);
1051 root_len = strlen(source_dir);
1053 INIT_LIST_HEAD(&dir_head.list);
1055 trans = btrfs_start_transaction(root, 1);
1056 ret = traverse_directory(trans, root, source_dir, &dir_head, out_fd);
1058 fprintf(stderr, "unable to traverse_directory\n");
1061 btrfs_commit_transaction(trans, root);
1063 printf("Making image is completed.\n");
1066 fprintf(stderr, "Making image is aborted.\n");
1070 static u64 size_sourcedir(char *dir_name, u64 sectorsize,
1071 u64 *num_of_meta_chunks_ret, u64 *size_of_data_ret)
1078 char *file_name = "temp_file";
1080 u64 minimum_data_size = 256 * 1024 * 1024; /* 256MB */
1081 u64 default_chunk_size = 8 * 1024 * 1024; /* 8MB */
1082 u64 allocated_meta_size = 8 * 1024 * 1024; /* 8MB */
1083 u64 allocated_total_size = 20 * 1024 * 1024; /* 20MB */
1084 u64 num_of_meta_chunks = 0;
1085 u64 num_of_allocated_meta_chunks =
1086 allocated_meta_size / default_chunk_size;
1088 ret = sprintf(command, "du -B 4096 -s ");
1090 fprintf(stderr, "error executing sprintf for du command\n");
1093 strcat(command, dir_name);
1094 strcat(command, " > ");
1095 strcat(command, file_name);
1096 ret = system(command);
1098 file = fopen(file_name, "r");
1099 ret = fscanf(file, "%lld %s\n", &dir_size, path);
1103 dir_size *= sectorsize;
1104 *size_of_data_ret = dir_size;
1106 num_of_meta_chunks = (dir_size / 2) / default_chunk_size;
1107 if (((dir_size / 2) % default_chunk_size) != 0)
1108 num_of_meta_chunks++;
1109 if (num_of_meta_chunks <= num_of_allocated_meta_chunks)
1110 num_of_meta_chunks = 0;
1112 num_of_meta_chunks -= num_of_allocated_meta_chunks;
1114 total_size = allocated_total_size + dir_size +
1115 (num_of_meta_chunks * default_chunk_size);
1117 *num_of_meta_chunks_ret = num_of_meta_chunks;
1119 if (total_size < minimum_data_size)
1120 total_size = minimum_data_size;
1125 static int zero_output_file(int out_fd, u64 size, u32 sectorsize)
1127 int len = sectorsize;
1128 int loop_num = size / sectorsize;
1130 char *buf = malloc(len);
1136 memset(buf, 0, len);
1137 for (i = 0; i < loop_num; i++) {
1138 written = pwrite64(out_fd, buf, len, location);
1141 location += sectorsize;
1147 int main(int ac, char **av)
1150 struct btrfs_root *root;
1151 struct btrfs_trans_handle *trans;
1154 u64 block_count = 0;
1155 u64 dev_block_count = 0;
1157 u64 alloc_start = 0;
1158 u64 metadata_profile = BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_DUP;
1159 u64 data_profile = BTRFS_BLOCK_GROUP_RAID0;
1160 u32 leafsize = getpagesize();
1161 u32 sectorsize = 4096;
1162 u32 nodesize = leafsize;
1163 u32 stripesize = 4096;
1165 int option_index = 0;
1170 int data_profile_opt = 0;
1171 int metadata_profile_opt = 0;
1173 char *source_dir = NULL;
1174 int source_dir_set = 0;
1175 char *output = "output.img";
1176 u64 num_of_meta_chunks = 0;
1177 u64 size_of_data = 0;
1182 c = getopt_long(ac, av, "A:b:l:n:s:m:d:L:r:VM", long_options,
1188 alloc_start = parse_size(optarg);
1191 data_profile = parse_profile(optarg);
1192 data_profile_opt = 1;
1195 leafsize = parse_size(optarg);
1198 label = parse_label(optarg);
1201 metadata_profile = parse_profile(optarg);
1202 metadata_profile_opt = 1;
1208 nodesize = parse_size(optarg);
1211 sectorsize = parse_size(optarg);
1214 block_count = parse_size(optarg);
1215 if (block_count <= 1024*1024*1024) {
1216 printf("SMALL VOLUME: forcing mixed "
1217 "metadata/data groups\n");
1226 source_dir = optarg;
1233 sectorsize = max(sectorsize, (u32)getpagesize());
1234 if (leafsize < sectorsize || (leafsize & (sectorsize - 1))) {
1235 fprintf(stderr, "Illegal leafsize %u\n", leafsize);
1238 if (nodesize < sectorsize || (nodesize & (sectorsize - 1))) {
1239 fprintf(stderr, "Illegal nodesize %u\n", nodesize);
1248 printf("\nWARNING! - %s IS EXPERIMENTAL\n", BTRFS_BUILD_VERSION);
1249 printf("WARNING! - see http://btrfs.wiki.kernel.org before using\n\n");
1251 if (source_dir == 0) {
1252 file = av[optind++];
1253 ret = check_mounted(file);
1255 fprintf(stderr, "error checking %s mount status\n", file);
1259 fprintf(stderr, "%s is mounted\n", file);
1263 fd = open(file, O_RDWR);
1265 fprintf(stderr, "unable to open %s\n", file);
1269 ret = btrfs_prepare_device(fd, file, zero_end, &dev_block_count, &mixed);
1270 if (block_count == 0)
1271 block_count = dev_block_count;
1274 fd = open_target(output);
1276 fprintf(stderr, "unable to open the %s\n", file);
1282 block_count = size_sourcedir(source_dir, sectorsize,
1283 &num_of_meta_chunks, &size_of_data);
1284 ret = zero_output_file(fd, block_count, sectorsize);
1286 fprintf(stderr, "unable to zero the output file\n");
1291 if (!metadata_profile_opt)
1292 metadata_profile = 0;
1293 if (!data_profile_opt)
1296 if (metadata_profile != data_profile) {
1297 fprintf(stderr, "With mixed block groups data and metadata "
1298 "profiles must be the same\n");
1303 blocks[0] = BTRFS_SUPER_INFO_OFFSET;
1304 for (i = 1; i < 7; i++) {
1305 blocks[i] = BTRFS_SUPER_INFO_OFFSET + 1024 * 1024 +
1309 ret = make_btrfs(fd, file, label, blocks, block_count,
1311 sectorsize, stripesize);
1313 fprintf(stderr, "error during mkfs %d\n", ret);
1316 root = open_ctree(file, 0, O_RDWR);
1317 root->fs_info->alloc_start = alloc_start;
1319 ret = make_root_dir(root, mixed);
1321 fprintf(stderr, "failed to setup the root directory\n");
1325 trans = btrfs_start_transaction(root, 1);
1330 btrfs_register_one_device(file);
1332 fprintf(stderr, "ctree init failed\n");
1338 int old_mixed = mixed;
1340 file = av[optind++];
1341 ret = check_mounted(file);
1343 fprintf(stderr, "error checking %s mount status\n",
1348 fprintf(stderr, "%s is mounted\n", file);
1351 fd = open(file, O_RDWR);
1353 fprintf(stderr, "unable to open %s\n", file);
1356 ret = btrfs_device_already_in_root(root, fd,
1357 BTRFS_SUPER_INFO_OFFSET);
1359 fprintf(stderr, "skipping duplicate device %s in FS\n",
1364 ret = btrfs_prepare_device(fd, file, zero_end,
1365 &dev_block_count, &mixed);
1369 ret = btrfs_add_to_fsid(trans, root, fd, file, dev_block_count,
1370 sectorsize, sectorsize, sectorsize);
1372 btrfs_register_one_device(file);
1376 if (!source_dir_set) {
1377 ret = create_raid_groups(trans, root, data_profile,
1378 metadata_profile, mixed);
1382 ret = create_data_reloc_tree(trans, root);
1386 struct btrfs_super_block *super = &root->fs_info->super_copy;
1387 u64 flags = btrfs_super_incompat_flags(super);
1389 flags |= BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS;
1390 btrfs_set_super_incompat_flags(super, flags);
1393 printf("fs created label %s on %s\n\tnodesize %u leafsize %u "
1394 "sectorsize %u size %s\n",
1395 label, first_file, nodesize, leafsize, sectorsize,
1396 pretty_buf = pretty_sizes(btrfs_super_total_bytes(&root->fs_info->super_copy)));
1399 printf("%s\n", BTRFS_BUILD_VERSION);
1400 btrfs_commit_transaction(trans, root);
1402 if (source_dir_set) {
1403 trans = btrfs_start_transaction(root, 1);
1404 ret = create_chunks(trans, root,
1405 num_of_meta_chunks, size_of_data);
1407 btrfs_commit_transaction(trans, root);
1409 ret = make_image(source_dir, root, fd);
1413 ret = close_ctree(root);