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"
25 #include <sys/ioctl.h>
26 #include <sys/mount.h>
32 #include <sys/types.h>
38 #include <uuid/uuid.h>
41 #include <attr/xattr.h>
42 #include <blkid/blkid.h>
46 #include "transaction.h"
50 static u64 index_cnt = 2;
52 struct directory_name_entry {
56 struct list_head list;
59 static int make_root_dir(struct btrfs_root *root, int mixed)
61 struct btrfs_trans_handle *trans;
62 struct btrfs_key location;
68 trans = btrfs_start_transaction(root, 1);
69 bytes_used = btrfs_super_bytes_used(&root->fs_info->super_copy);
71 root->fs_info->system_allocs = 1;
72 ret = btrfs_make_block_group(trans, root, bytes_used,
73 BTRFS_BLOCK_GROUP_SYSTEM,
74 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
75 0, BTRFS_MKFS_SYSTEM_GROUP_SIZE);
79 ret = btrfs_alloc_chunk(trans, root->fs_info->extent_root,
80 &chunk_start, &chunk_size,
81 BTRFS_BLOCK_GROUP_METADATA |
82 BTRFS_BLOCK_GROUP_DATA);
84 ret = btrfs_make_block_group(trans, root, 0,
85 BTRFS_BLOCK_GROUP_METADATA |
86 BTRFS_BLOCK_GROUP_DATA,
87 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
88 chunk_start, chunk_size);
90 printf("Created a data/metadata chunk of size %llu\n", chunk_size);
92 ret = btrfs_alloc_chunk(trans, root->fs_info->extent_root,
93 &chunk_start, &chunk_size,
94 BTRFS_BLOCK_GROUP_METADATA);
96 ret = btrfs_make_block_group(trans, root, 0,
97 BTRFS_BLOCK_GROUP_METADATA,
98 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
99 chunk_start, chunk_size);
103 root->fs_info->system_allocs = 0;
104 btrfs_commit_transaction(trans, root);
105 trans = btrfs_start_transaction(root, 1);
109 ret = btrfs_alloc_chunk(trans, root->fs_info->extent_root,
110 &chunk_start, &chunk_size,
111 BTRFS_BLOCK_GROUP_DATA);
113 ret = btrfs_make_block_group(trans, root, 0,
114 BTRFS_BLOCK_GROUP_DATA,
115 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
116 chunk_start, chunk_size);
120 ret = btrfs_make_root_dir(trans, root->fs_info->tree_root,
121 BTRFS_ROOT_TREE_DIR_OBJECTID);
124 ret = btrfs_make_root_dir(trans, root, BTRFS_FIRST_FREE_OBJECTID);
127 memcpy(&location, &root->fs_info->fs_root->root_key, sizeof(location));
128 location.offset = (u64)-1;
129 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
131 btrfs_super_root_dir(&root->fs_info->super_copy),
132 &location, BTRFS_FT_DIR, 0);
136 ret = btrfs_insert_inode_ref(trans, root->fs_info->tree_root,
137 "default", 7, location.objectid,
138 BTRFS_ROOT_TREE_DIR_OBJECTID, 0);
142 btrfs_commit_transaction(trans, root);
147 static int recow_roots(struct btrfs_trans_handle *trans,
148 struct btrfs_root *root)
151 struct extent_buffer *tmp;
152 struct btrfs_fs_info *info = root->fs_info;
154 ret = __btrfs_cow_block(trans, info->fs_root, info->fs_root->node,
155 NULL, 0, &tmp, 0, 0);
157 free_extent_buffer(tmp);
159 ret = __btrfs_cow_block(trans, info->tree_root, info->tree_root->node,
160 NULL, 0, &tmp, 0, 0);
162 free_extent_buffer(tmp);
164 ret = __btrfs_cow_block(trans, info->extent_root,
165 info->extent_root->node, NULL, 0, &tmp, 0, 0);
167 free_extent_buffer(tmp);
169 ret = __btrfs_cow_block(trans, info->chunk_root, info->chunk_root->node,
170 NULL, 0, &tmp, 0, 0);
172 free_extent_buffer(tmp);
175 ret = __btrfs_cow_block(trans, info->dev_root, info->dev_root->node,
176 NULL, 0, &tmp, 0, 0);
178 free_extent_buffer(tmp);
180 ret = __btrfs_cow_block(trans, info->csum_root, info->csum_root->node,
181 NULL, 0, &tmp, 0, 0);
183 free_extent_buffer(tmp);
188 static int create_one_raid_group(struct btrfs_trans_handle *trans,
189 struct btrfs_root *root, u64 type)
195 ret = btrfs_alloc_chunk(trans, root->fs_info->extent_root,
196 &chunk_start, &chunk_size, type);
198 ret = btrfs_make_block_group(trans, root->fs_info->extent_root, 0,
199 type, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
200 chunk_start, chunk_size);
205 static int create_raid_groups(struct btrfs_trans_handle *trans,
206 struct btrfs_root *root, u64 data_profile,
207 int data_profile_opt, u64 metadata_profile,
208 int metadata_profile_opt, int mixed, int ssd)
210 u64 num_devices = btrfs_super_num_devices(&root->fs_info->super_copy);
212 u64 devices_for_raid = num_devices;
216 * Set default profiles according to number of added devices.
217 * For mixed groups defaults are single/single.
219 if (!metadata_profile_opt && !mixed) {
220 if (num_devices == 1 && ssd)
221 printf("Detected a SSD, turning off metadata "
222 "duplication. Mkfs with -m dup if you want to "
223 "force metadata duplication.\n");
224 metadata_profile = (num_devices > 1) ?
225 BTRFS_BLOCK_GROUP_RAID1 : (ssd) ? 0: BTRFS_BLOCK_GROUP_DUP;
227 if (!data_profile_opt && !mixed) {
228 data_profile = (num_devices > 1) ?
229 BTRFS_BLOCK_GROUP_RAID0 : 0; /* raid0 or single */
232 if (devices_for_raid > 4)
233 devices_for_raid = 4;
235 switch (devices_for_raid) {
238 allowed |= BTRFS_BLOCK_GROUP_RAID10;
240 allowed |= BTRFS_BLOCK_GROUP_RAID6;
242 allowed |= BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1 |
243 BTRFS_BLOCK_GROUP_RAID5;
246 allowed |= BTRFS_BLOCK_GROUP_DUP;
249 if (metadata_profile & ~allowed) {
250 fprintf(stderr, "unable to create FS with metadata "
251 "profile %llu (have %llu devices)\n", metadata_profile,
255 if (data_profile & ~allowed) {
256 fprintf(stderr, "unable to create FS with data "
257 "profile %llu (have %llu devices)\n", data_profile,
262 /* allow dup'ed data chunks only in mixed mode */
263 if (!mixed && (data_profile & BTRFS_BLOCK_GROUP_DUP)) {
264 fprintf(stderr, "dup for data is allowed only in mixed mode\n");
268 if (allowed & metadata_profile) {
269 u64 meta_flags = BTRFS_BLOCK_GROUP_METADATA;
271 ret = create_one_raid_group(trans, root,
272 BTRFS_BLOCK_GROUP_SYSTEM |
273 (allowed & metadata_profile));
277 meta_flags |= BTRFS_BLOCK_GROUP_DATA;
279 ret = create_one_raid_group(trans, root, meta_flags |
280 (allowed & metadata_profile));
283 ret = recow_roots(trans, root);
286 if (!mixed && num_devices > 1 && (allowed & data_profile)) {
287 ret = create_one_raid_group(trans, root,
288 BTRFS_BLOCK_GROUP_DATA |
289 (allowed & data_profile));
295 static int create_data_reloc_tree(struct btrfs_trans_handle *trans,
296 struct btrfs_root *root)
298 struct btrfs_key location;
299 struct btrfs_root_item root_item;
300 struct extent_buffer *tmp;
301 u64 objectid = BTRFS_DATA_RELOC_TREE_OBJECTID;
304 ret = btrfs_copy_root(trans, root, root->node, &tmp, objectid);
307 memcpy(&root_item, &root->root_item, sizeof(root_item));
308 btrfs_set_root_bytenr(&root_item, tmp->start);
309 btrfs_set_root_level(&root_item, btrfs_header_level(tmp));
310 btrfs_set_root_generation(&root_item, trans->transid);
311 free_extent_buffer(tmp);
313 location.objectid = objectid;
314 location.type = BTRFS_ROOT_ITEM_KEY;
316 ret = btrfs_insert_root(trans, root->fs_info->tree_root,
317 &location, &root_item);
322 static void print_usage(void)
324 fprintf(stderr, "usage: mkfs.btrfs [options] dev [ dev ... ]\n");
325 fprintf(stderr, "options:\n");
326 fprintf(stderr, "\t -A --alloc-start the offset to start the FS\n");
327 fprintf(stderr, "\t -b --byte-count total number of bytes in the FS\n");
328 fprintf(stderr, "\t -d --data data profile, raid0, raid1, raid10, dup or single\n");
329 fprintf(stderr, "\t -l --leafsize size of btree leaves\n");
330 fprintf(stderr, "\t -L --label set a label\n");
331 fprintf(stderr, "\t -m --metadata metadata profile, values like data profile\n");
332 fprintf(stderr, "\t -M --mixed mix metadata and data together\n");
333 fprintf(stderr, "\t -n --nodesize size of btree nodes\n");
334 fprintf(stderr, "\t -s --sectorsize min block allocation\n");
335 fprintf(stderr, "\t -r --rootdir the source directory\n");
336 fprintf(stderr, "\t -K --nodiscard do not perform whole device TRIM\n");
337 fprintf(stderr, "\t -V --version print the mkfs.btrfs version and exit\n");
338 fprintf(stderr, "%s\n", BTRFS_BUILD_VERSION);
342 static void print_version(void)
344 fprintf(stderr, "mkfs.btrfs, part of %s\n", BTRFS_BUILD_VERSION);
348 static u64 parse_profile(char *s)
350 if (strcmp(s, "raid0") == 0) {
351 return BTRFS_BLOCK_GROUP_RAID0;
352 } else if (strcmp(s, "raid1") == 0) {
353 return BTRFS_BLOCK_GROUP_RAID1;
354 } else if (strcmp(s, "raid5") == 0) {
355 return BTRFS_BLOCK_GROUP_RAID5;
356 } else if (strcmp(s, "raid6") == 0) {
357 return BTRFS_BLOCK_GROUP_RAID6;
358 } else if (strcmp(s, "raid10") == 0) {
359 return BTRFS_BLOCK_GROUP_RAID10;
360 } else if (strcmp(s, "dup") == 0) {
361 return BTRFS_BLOCK_GROUP_DUP;
362 } else if (strcmp(s, "single") == 0) {
365 fprintf(stderr, "Unknown profile %s\n", s);
372 static char *parse_label(char *input)
374 int len = strlen(input);
376 if (len >= BTRFS_LABEL_SIZE) {
377 fprintf(stderr, "Label %s is too long (max %d)\n", input,
378 BTRFS_LABEL_SIZE - 1);
381 return strdup(input);
384 static struct option long_options[] = {
385 { "alloc-start", 1, NULL, 'A'},
386 { "byte-count", 1, NULL, 'b' },
387 { "leafsize", 1, NULL, 'l' },
388 { "label", 1, NULL, 'L'},
389 { "metadata", 1, NULL, 'm' },
390 { "mixed", 0, NULL, 'M' },
391 { "nodesize", 1, NULL, 'n' },
392 { "sectorsize", 1, NULL, 's' },
393 { "data", 1, NULL, 'd' },
394 { "version", 0, NULL, 'V' },
395 { "rootdir", 1, NULL, 'r' },
396 { "nodiscard", 0, NULL, 'K' },
400 static int add_directory_items(struct btrfs_trans_handle *trans,
401 struct btrfs_root *root, u64 objectid,
402 ino_t parent_inum, const char *name,
403 struct stat *st, int *dir_index_cnt)
407 struct btrfs_key location;
410 name_len = strlen(name);
412 location.objectid = objectid;
414 btrfs_set_key_type(&location, BTRFS_INODE_ITEM_KEY);
416 if (S_ISDIR(st->st_mode))
417 filetype = BTRFS_FT_DIR;
418 if (S_ISREG(st->st_mode))
419 filetype = BTRFS_FT_REG_FILE;
420 if (S_ISLNK(st->st_mode))
421 filetype = BTRFS_FT_SYMLINK;
423 ret = btrfs_insert_dir_item(trans, root, name, name_len,
424 parent_inum, &location,
425 filetype, index_cnt);
427 *dir_index_cnt = index_cnt;
433 static int fill_inode_item(struct btrfs_trans_handle *trans,
434 struct btrfs_root *root,
435 struct btrfs_inode_item *dst, struct stat *src)
438 u64 sectorsize = root->sectorsize;
441 * btrfs_inode_item has some reserved fields
442 * and represents on-disk inode entry, so
443 * zero everything to prevent information leak
445 memset(dst, 0, sizeof (*dst));
447 btrfs_set_stack_inode_generation(dst, trans->transid);
448 btrfs_set_stack_inode_size(dst, src->st_size);
449 btrfs_set_stack_inode_nbytes(dst, 0);
450 btrfs_set_stack_inode_block_group(dst, 0);
451 btrfs_set_stack_inode_nlink(dst, src->st_nlink);
452 btrfs_set_stack_inode_uid(dst, src->st_uid);
453 btrfs_set_stack_inode_gid(dst, src->st_gid);
454 btrfs_set_stack_inode_mode(dst, src->st_mode);
455 btrfs_set_stack_inode_rdev(dst, 0);
456 btrfs_set_stack_inode_flags(dst, 0);
457 btrfs_set_stack_timespec_sec(&dst->atime, src->st_atime);
458 btrfs_set_stack_timespec_nsec(&dst->atime, 0);
459 btrfs_set_stack_timespec_sec(&dst->ctime, src->st_ctime);
460 btrfs_set_stack_timespec_nsec(&dst->ctime, 0);
461 btrfs_set_stack_timespec_sec(&dst->mtime, src->st_mtime);
462 btrfs_set_stack_timespec_nsec(&dst->mtime, 0);
463 btrfs_set_stack_timespec_sec(&dst->otime, 0);
464 btrfs_set_stack_timespec_nsec(&dst->otime, 0);
466 if (S_ISDIR(src->st_mode)) {
467 btrfs_set_stack_inode_size(dst, 0);
468 btrfs_set_stack_inode_nlink(dst, 1);
470 if (S_ISREG(src->st_mode)) {
471 btrfs_set_stack_inode_size(dst, (u64)src->st_size);
472 if (src->st_size <= BTRFS_MAX_INLINE_DATA_SIZE(root))
473 btrfs_set_stack_inode_nbytes(dst, src->st_size);
475 blocks = src->st_size / sectorsize;
476 if (src->st_size % sectorsize)
478 blocks *= sectorsize;
479 btrfs_set_stack_inode_nbytes(dst, blocks);
482 if (S_ISLNK(src->st_mode))
483 btrfs_set_stack_inode_nbytes(dst, src->st_size + 1);
488 static int directory_select(const struct direct *entry)
490 if ((strncmp(entry->d_name, ".", entry->d_reclen) == 0) ||
491 (strncmp(entry->d_name, "..", entry->d_reclen) == 0))
497 static void free_namelist(struct direct **files, int count)
504 for (i = 0; i < count; ++i)
509 static u64 calculate_dir_inode_size(char *dirname)
512 struct direct **files, *cur_file;
513 u64 dir_inode_size = 0;
515 count = scandir(dirname, &files, directory_select, NULL);
517 for (i = 0; i < count; i++) {
519 dir_inode_size += strlen(cur_file->d_name);
522 free_namelist(files, count);
525 return dir_inode_size;
528 static int add_inode_items(struct btrfs_trans_handle *trans,
529 struct btrfs_root *root,
530 struct stat *st, char *name,
531 u64 self_objectid, ino_t parent_inum,
532 int dir_index_cnt, struct btrfs_inode_item *inode_ret)
535 struct btrfs_key inode_key;
536 struct btrfs_inode_item btrfs_inode;
541 name_len = strlen(name);
542 fill_inode_item(trans, root, &btrfs_inode, st);
543 objectid = self_objectid;
545 if (S_ISDIR(st->st_mode)) {
546 inode_size = calculate_dir_inode_size(name);
547 btrfs_set_stack_inode_size(&btrfs_inode, inode_size);
550 inode_key.objectid = objectid;
551 inode_key.offset = 0;
552 btrfs_set_key_type(&inode_key, BTRFS_INODE_ITEM_KEY);
554 ret = btrfs_insert_inode(trans, root, objectid, &btrfs_inode);
558 ret = btrfs_insert_inode_ref(trans, root, name, name_len,
559 objectid, parent_inum, dir_index_cnt);
563 *inode_ret = btrfs_inode;
568 static int add_xattr_item(struct btrfs_trans_handle *trans,
569 struct btrfs_root *root, u64 objectid,
570 const char *file_name)
574 char xattr_list[XATTR_LIST_MAX];
576 char cur_value[XATTR_SIZE_MAX];
577 char delimiter = '\0';
578 char *next_location = xattr_list;
580 ret = llistxattr(file_name, xattr_list, XATTR_LIST_MAX);
584 fprintf(stderr, "get a list of xattr failed for %s\n",
591 cur_name = strtok(xattr_list, &delimiter);
592 while (cur_name != NULL) {
593 cur_name_len = strlen(cur_name);
594 next_location += cur_name_len + 1;
596 ret = getxattr(file_name, cur_name, cur_value, XATTR_SIZE_MAX);
600 fprintf(stderr, "get a xattr value failed for %s attr %s\n",
601 file_name, cur_name);
605 ret = btrfs_insert_xattr_item(trans, root, cur_name,
606 cur_name_len, cur_value,
609 fprintf(stderr, "insert a xattr item failed for %s\n",
613 cur_name = strtok(next_location, &delimiter);
618 static int custom_alloc_extent(struct btrfs_root *root, u64 num_bytes,
619 u64 hint_byte, struct btrfs_key *ins)
623 u64 last = hint_byte;
626 struct btrfs_block_group_cache *cache;
629 ret = find_first_extent_bit(&root->fs_info->free_space_cache,
630 last, &start, &end, EXTENT_DIRTY);
632 if (wrapped++ == 0) {
640 start = max(last, start);
642 if (last - start < num_bytes)
645 last = start + num_bytes;
646 if (test_range_bit(&root->fs_info->pinned_extents,
647 start, last - 1, EXTENT_DIRTY, 0))
650 cache = btrfs_lookup_block_group(root->fs_info, start);
652 if (cache->flags & BTRFS_BLOCK_GROUP_SYSTEM ||
653 last > cache->key.objectid + cache->key.offset) {
654 last = cache->key.objectid + cache->key.offset;
658 if (cache->flags & (BTRFS_BLOCK_GROUP_SYSTEM |
659 BTRFS_BLOCK_GROUP_METADATA)) {
660 last = cache->key.objectid + cache->key.offset;
664 clear_extent_dirty(&root->fs_info->free_space_cache,
665 start, start + num_bytes - 1, 0);
667 ins->objectid = start;
668 ins->offset = num_bytes;
669 ins->type = BTRFS_EXTENT_ITEM_KEY;
673 fprintf(stderr, "not enough free space\n");
677 static int record_file_extent(struct btrfs_trans_handle *trans,
678 struct btrfs_root *root, u64 objectid,
679 struct btrfs_inode_item *inode,
680 u64 file_pos, u64 disk_bytenr,
684 struct btrfs_fs_info *info = root->fs_info;
685 struct btrfs_root *extent_root = info->extent_root;
686 struct extent_buffer *leaf;
687 struct btrfs_file_extent_item *fi;
688 struct btrfs_key ins_key;
689 struct btrfs_path path;
690 struct btrfs_extent_item *ei;
692 btrfs_init_path(&path);
694 ins_key.objectid = objectid;
696 btrfs_set_key_type(&ins_key, BTRFS_EXTENT_DATA_KEY);
697 ret = btrfs_insert_empty_item(trans, root, &path, &ins_key,
701 leaf = path.nodes[0];
702 fi = btrfs_item_ptr(leaf, path.slots[0],
703 struct btrfs_file_extent_item);
704 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
705 btrfs_set_file_extent_type(leaf, fi, BTRFS_FILE_EXTENT_REG);
706 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
707 btrfs_set_file_extent_disk_num_bytes(leaf, fi, num_bytes);
708 btrfs_set_file_extent_offset(leaf, fi, 0);
709 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
710 btrfs_set_file_extent_ram_bytes(leaf, fi, num_bytes);
711 btrfs_set_file_extent_compression(leaf, fi, 0);
712 btrfs_set_file_extent_encryption(leaf, fi, 0);
713 btrfs_set_file_extent_other_encoding(leaf, fi, 0);
714 btrfs_mark_buffer_dirty(leaf);
716 btrfs_release_path(root, &path);
718 ins_key.objectid = disk_bytenr;
719 ins_key.offset = num_bytes;
720 ins_key.type = BTRFS_EXTENT_ITEM_KEY;
722 ret = btrfs_insert_empty_item(trans, extent_root, &path,
723 &ins_key, sizeof(*ei));
725 leaf = path.nodes[0];
726 ei = btrfs_item_ptr(leaf, path.slots[0],
727 struct btrfs_extent_item);
729 btrfs_set_extent_refs(leaf, ei, 0);
730 btrfs_set_extent_generation(leaf, ei, trans->transid);
731 btrfs_set_extent_flags(leaf, ei, BTRFS_EXTENT_FLAG_DATA);
733 btrfs_mark_buffer_dirty(leaf);
734 ret = btrfs_update_block_group(trans, root, disk_bytenr,
738 } else if (ret != -EEXIST) {
742 ret = btrfs_inc_extent_ref(trans, root, disk_bytenr, num_bytes, 0,
743 root->root_key.objectid,
746 btrfs_release_path(root, &path);
750 static int add_symbolic_link(struct btrfs_trans_handle *trans,
751 struct btrfs_root *root,
752 u64 objectid, const char *path_name)
755 u64 sectorsize = root->sectorsize;
756 char *buf = malloc(sectorsize);
758 ret = readlink(path_name, buf, sectorsize);
760 fprintf(stderr, "readlink failed for %s\n", path_name);
763 if (ret >= sectorsize) {
764 fprintf(stderr, "symlink too long for %s", path_name);
769 buf[ret] = '\0'; /* readlink does not do it for us */
770 ret = btrfs_insert_inline_extent(trans, root, objectid, 0,
777 static int add_file_items(struct btrfs_trans_handle *trans,
778 struct btrfs_root *root,
779 struct btrfs_inode_item *btrfs_inode, u64 objectid,
780 ino_t parent_inum, struct stat *st,
781 const char *path_name, int out_fd)
787 struct btrfs_key key;
789 u32 sectorsize = root->sectorsize;
794 fd = open(path_name, O_RDONLY);
796 fprintf(stderr, "%s open failed\n", path_name);
800 blocks = st->st_size / sectorsize;
801 if (st->st_size % sectorsize)
804 if (st->st_size <= BTRFS_MAX_INLINE_DATA_SIZE(root)) {
805 buffer = malloc(st->st_size);
806 ret_read = pread64(fd, buffer, st->st_size, bytes_read);
807 if (ret_read == -1) {
808 fprintf(stderr, "%s read failed\n", path_name);
812 ret = btrfs_insert_inline_extent(trans, root, objectid, 0,
813 buffer, st->st_size);
817 ret = custom_alloc_extent(root, blocks * sectorsize, 0, &key);
821 first_block = key.objectid;
823 buffer = malloc(sectorsize);
826 memset(buffer, 0, sectorsize);
827 ret_read = pread64(fd, buffer, sectorsize, bytes_read);
828 if (ret_read == -1) {
829 fprintf(stderr, "%s read failed\n", path_name);
833 ret = pwrite64(out_fd, buffer, sectorsize,
834 first_block + bytes_read);
835 if (ret != sectorsize) {
836 fprintf(stderr, "output file write failed\n");
840 /* checksum for file data */
841 ret = btrfs_csum_file_block(trans, root->fs_info->csum_root,
842 first_block + (blocks * sectorsize),
843 first_block + bytes_read,
846 fprintf(stderr, "%s checksum failed\n", path_name);
850 bytes_read += ret_read;
852 } while (ret_read == sectorsize);
854 if (num_blocks > 0) {
855 ret = record_file_extent(trans, root, objectid, btrfs_inode,
856 first_block, first_block,
857 blocks * sectorsize);
869 static char *make_path(char *dir, char *name)
873 path = malloc(strlen(dir) + strlen(name) + 2);
877 if (dir[strlen(dir) - 1] != '/')
883 static int traverse_directory(struct btrfs_trans_handle *trans,
884 struct btrfs_root *root, char *dir_name,
885 struct directory_name_entry *dir_head, int out_fd)
889 struct btrfs_inode_item cur_inode;
890 struct btrfs_inode_item *inode_item;
891 int count, i, dir_index_cnt;
892 struct direct **files;
894 struct directory_name_entry *dir_entry, *parent_dir_entry;
895 struct direct *cur_file;
896 ino_t parent_inum, cur_inum;
897 ino_t highest_inum = 0;
898 char *parent_dir_name;
899 struct btrfs_path path;
900 struct extent_buffer *leaf;
901 struct btrfs_key root_dir_key;
902 u64 root_dir_inode_size = 0;
904 /* Add list for source directory */
905 dir_entry = malloc(sizeof(struct directory_name_entry));
906 dir_entry->dir_name = dir_name;
907 dir_entry->path = strdup(dir_name);
909 parent_inum = highest_inum + BTRFS_FIRST_FREE_OBJECTID;
910 dir_entry->inum = parent_inum;
911 list_add_tail(&dir_entry->list, &dir_head->list);
913 btrfs_init_path(&path);
915 root_dir_key.objectid = btrfs_root_dirid(&root->root_item);
916 root_dir_key.offset = 0;
917 btrfs_set_key_type(&root_dir_key, BTRFS_INODE_ITEM_KEY);
918 ret = btrfs_lookup_inode(trans, root, &path, &root_dir_key, 1);
920 fprintf(stderr, "root dir lookup error\n");
924 leaf = path.nodes[0];
925 inode_item = btrfs_item_ptr(leaf, path.slots[0],
926 struct btrfs_inode_item);
928 root_dir_inode_size = calculate_dir_inode_size(dir_name);
929 btrfs_set_inode_size(leaf, inode_item, root_dir_inode_size);
930 btrfs_mark_buffer_dirty(leaf);
932 btrfs_release_path(root, &path);
935 parent_dir_entry = list_entry(dir_head->list.next,
936 struct directory_name_entry,
938 list_del(&parent_dir_entry->list);
940 parent_inum = parent_dir_entry->inum;
941 parent_dir_name = parent_dir_entry->dir_name;
942 if (chdir(parent_dir_entry->path)) {
943 fprintf(stderr, "chdir error for %s\n",
948 count = scandir(parent_dir_entry->path, &files,
949 directory_select, NULL);
952 fprintf(stderr, "scandir for %s failed: %s\n",
953 parent_dir_name, strerror (errno));
957 for (i = 0; i < count; i++) {
960 if (lstat(cur_file->d_name, &st) == -1) {
961 fprintf(stderr, "lstat failed for file %s\n",
966 cur_inum = ++highest_inum + BTRFS_FIRST_FREE_OBJECTID;
967 ret = add_directory_items(trans, root,
968 cur_inum, parent_inum,
970 &st, &dir_index_cnt);
972 fprintf(stderr, "add_directory_items failed\n");
976 ret = add_inode_items(trans, root, &st,
977 cur_file->d_name, cur_inum,
978 parent_inum, dir_index_cnt,
981 fprintf(stderr, "add_inode_items failed\n");
985 ret = add_xattr_item(trans, root,
986 cur_inum, cur_file->d_name);
988 fprintf(stderr, "add_xattr_item failed\n");
993 if (S_ISDIR(st.st_mode)) {
994 dir_entry = malloc(sizeof(struct directory_name_entry));
995 dir_entry->dir_name = cur_file->d_name;
996 dir_entry->path = make_path(parent_dir_entry->path,
998 dir_entry->inum = cur_inum;
999 list_add_tail(&dir_entry->list, &dir_head->list);
1000 } else if (S_ISREG(st.st_mode)) {
1001 ret = add_file_items(trans, root, &cur_inode,
1002 cur_inum, parent_inum, &st,
1003 cur_file->d_name, out_fd);
1005 fprintf(stderr, "add_file_items failed\n");
1008 } else if (S_ISLNK(st.st_mode)) {
1009 ret = add_symbolic_link(trans, root,
1010 cur_inum, cur_file->d_name);
1012 fprintf(stderr, "add_symbolic_link failed\n");
1018 free_namelist(files, count);
1019 free(parent_dir_entry->path);
1020 free(parent_dir_entry);
1024 } while (!list_empty(&dir_head->list));
1028 free_namelist(files, count);
1030 free(parent_dir_entry->path);
1031 free(parent_dir_entry);
1035 static int open_target(char *output_name)
1038 output_fd = open(output_name, O_CREAT | O_RDWR | O_TRUNC,
1039 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH);
1044 static int create_chunks(struct btrfs_trans_handle *trans,
1045 struct btrfs_root *root, u64 num_of_meta_chunks,
1050 u64 meta_type = BTRFS_BLOCK_GROUP_METADATA;
1051 u64 data_type = BTRFS_BLOCK_GROUP_DATA;
1052 u64 minimum_data_chunk_size = 8 * 1024 * 1024;
1056 for (i = 0; i < num_of_meta_chunks; i++) {
1057 ret = btrfs_alloc_chunk(trans, root->fs_info->extent_root,
1058 &chunk_start, &chunk_size, meta_type);
1060 ret = btrfs_make_block_group(trans, root->fs_info->extent_root, 0,
1061 meta_type, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
1062 chunk_start, chunk_size);
1064 set_extent_dirty(&root->fs_info->free_space_cache,
1065 chunk_start, chunk_start + chunk_size - 1, 0);
1068 if (size_of_data < minimum_data_chunk_size)
1069 size_of_data = minimum_data_chunk_size;
1070 ret = btrfs_alloc_data_chunk(trans, root->fs_info->extent_root,
1071 &chunk_start, size_of_data, data_type);
1073 ret = btrfs_make_block_group(trans, root->fs_info->extent_root, 0,
1074 data_type, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
1075 chunk_start, size_of_data);
1077 set_extent_dirty(&root->fs_info->free_space_cache,
1078 chunk_start, chunk_start + size_of_data - 1, 0);
1082 static int make_image(char *source_dir, struct btrfs_root *root, int out_fd)
1085 struct btrfs_trans_handle *trans;
1087 struct stat root_st;
1089 struct directory_name_entry dir_head;
1091 ret = lstat(source_dir, &root_st);
1093 fprintf(stderr, "unable to lstat the %s\n", source_dir);
1097 INIT_LIST_HEAD(&dir_head.list);
1099 trans = btrfs_start_transaction(root, 1);
1100 ret = traverse_directory(trans, root, source_dir, &dir_head, out_fd);
1102 fprintf(stderr, "unable to traverse_directory\n");
1105 btrfs_commit_transaction(trans, root);
1107 printf("Making image is completed.\n");
1110 fprintf(stderr, "Making image is aborted.\n");
1114 static u64 size_sourcedir(char *dir_name, u64 sectorsize,
1115 u64 *num_of_meta_chunks_ret, u64 *size_of_data_ret)
1122 char *file_name = "temp_file";
1124 u64 default_chunk_size = 8 * 1024 * 1024; /* 8MB */
1125 u64 allocated_meta_size = 8 * 1024 * 1024; /* 8MB */
1126 u64 allocated_total_size = 20 * 1024 * 1024; /* 20MB */
1127 u64 num_of_meta_chunks = 0;
1128 u64 num_of_allocated_meta_chunks =
1129 allocated_meta_size / default_chunk_size;
1131 ret = sprintf(command, "du -B 4096 -s ");
1133 fprintf(stderr, "error executing sprintf for du command\n");
1136 strcat(command, dir_name);
1137 strcat(command, " > ");
1138 strcat(command, file_name);
1139 ret = system(command);
1141 file = fopen(file_name, "r");
1142 ret = fscanf(file, "%lld %s\n", &dir_size, path);
1146 dir_size *= sectorsize;
1147 *size_of_data_ret = dir_size;
1149 num_of_meta_chunks = (dir_size / 2) / default_chunk_size;
1150 if (((dir_size / 2) % default_chunk_size) != 0)
1151 num_of_meta_chunks++;
1152 if (num_of_meta_chunks <= num_of_allocated_meta_chunks)
1153 num_of_meta_chunks = 0;
1155 num_of_meta_chunks -= num_of_allocated_meta_chunks;
1157 total_size = allocated_total_size + dir_size +
1158 (num_of_meta_chunks * default_chunk_size);
1160 *num_of_meta_chunks_ret = num_of_meta_chunks;
1165 static int zero_output_file(int out_fd, u64 size, u32 sectorsize)
1167 int len = sectorsize;
1168 int loop_num = size / sectorsize;
1170 char *buf = malloc(len);
1176 memset(buf, 0, len);
1177 for (i = 0; i < loop_num; i++) {
1178 written = pwrite64(out_fd, buf, len, location);
1181 location += sectorsize;
1187 static int check_leaf_or_node_size(u32 size, u32 sectorsize)
1189 if (size < sectorsize) {
1191 "Illegal leafsize (or nodesize) %u (smaller than %u)\n",
1194 } else if (size > BTRFS_MAX_METADATA_BLOCKSIZE) {
1196 "Illegal leafsize (or nodesize) %u (larger than %u)\n",
1197 size, BTRFS_MAX_METADATA_BLOCKSIZE);
1199 } else if (size & (sectorsize - 1)) {
1201 "Illegal leafsize (or nodesize) %u (not align to %u)\n",
1208 static int is_ssd(const char *file)
1213 char path[PATH_MAX];
1218 probe = blkid_new_probe_from_filename(file);
1223 * We want to use blkid_devno_to_wholedisk() but it's broken for some
1224 * reason on F17 at least so we'll do this trickery
1226 disk = blkid_probe_get_wholedisk_devno(probe);
1230 devname = blkid_devno_to_devname(disk);
1234 dev = strrchr(devname, '/');
1237 snprintf(path, PATH_MAX, "/sys/block/%s/queue/rotational", dev);
1240 blkid_free_probe(probe);
1242 fd = open(path, O_RDONLY);
1247 if (read(fd, &rotational, sizeof(char)) < sizeof(char)) {
1253 return !atoi((const char *)&rotational);
1256 int main(int ac, char **av)
1259 struct btrfs_root *root;
1260 struct btrfs_trans_handle *trans;
1263 u64 block_count = 0;
1264 u64 dev_block_count = 0;
1266 u64 alloc_start = 0;
1267 u64 metadata_profile = 0;
1268 u64 data_profile = 0;
1269 u32 leafsize = sysconf(_SC_PAGESIZE);
1270 u32 sectorsize = 4096;
1271 u32 nodesize = leafsize;
1272 u32 stripesize = 4096;
1274 int option_index = 0;
1279 int data_profile_opt = 0;
1280 int metadata_profile_opt = 0;
1284 char *source_dir = NULL;
1285 int source_dir_set = 0;
1286 u64 num_of_meta_chunks = 0;
1287 u64 size_of_data = 0;
1288 u64 source_dir_size = 0;
1290 struct btrfs_super_block *super;
1295 c = getopt_long(ac, av, "A:b:l:n:s:m:d:L:r:VMK", long_options,
1301 alloc_start = parse_size(optarg);
1304 data_profile = parse_profile(optarg);
1305 data_profile_opt = 1;
1309 nodesize = parse_size(optarg);
1310 leafsize = parse_size(optarg);
1313 label = parse_label(optarg);
1316 metadata_profile = parse_profile(optarg);
1317 metadata_profile_opt = 1;
1323 sectorsize = parse_size(optarg);
1326 block_count = parse_size(optarg);
1327 if (block_count <= 1024*1024*1024) {
1328 printf("SMALL VOLUME: forcing mixed "
1329 "metadata/data groups\n");
1338 source_dir = optarg;
1348 sectorsize = max(sectorsize, (u32)sysconf(_SC_PAGESIZE));
1349 if (check_leaf_or_node_size(leafsize, sectorsize))
1351 if (check_leaf_or_node_size(nodesize, sectorsize))
1357 printf("\nWARNING! - %s IS EXPERIMENTAL\n", BTRFS_BUILD_VERSION);
1358 printf("WARNING! - see http://btrfs.wiki.kernel.org before using\n\n");
1360 if (source_dir == 0) {
1361 file = av[optind++];
1362 ret = check_mounted(file);
1364 fprintf(stderr, "error checking %s mount status\n", file);
1368 fprintf(stderr, "%s is mounted\n", file);
1372 fd = open(file, O_RDWR);
1374 fprintf(stderr, "unable to open %s\n", file);
1378 ret = btrfs_prepare_device(fd, file, zero_end, &dev_block_count,
1379 block_count, &mixed, nodiscard);
1380 if (block_count && block_count > dev_block_count) {
1381 fprintf(stderr, "%s is smaller than requested size\n", file);
1386 file = av[optind++];
1387 fd = open_target(file);
1389 fprintf(stderr, "unable to open the %s\n", file);
1394 source_dir_size = size_sourcedir(source_dir, sectorsize,
1395 &num_of_meta_chunks, &size_of_data);
1396 if(block_count < source_dir_size)
1397 block_count = source_dir_size;
1398 ret = zero_output_file(fd, block_count, sectorsize);
1400 fprintf(stderr, "unable to zero the output file\n");
1403 /* our "device" is the new image file */
1404 dev_block_count = block_count;
1410 if (metadata_profile != data_profile) {
1411 fprintf(stderr, "With mixed block groups data and metadata "
1412 "profiles must be the same\n");
1417 blocks[0] = BTRFS_SUPER_INFO_OFFSET;
1418 for (i = 1; i < 7; i++) {
1419 blocks[i] = BTRFS_SUPER_INFO_OFFSET + 1024 * 1024 +
1423 ret = make_btrfs(fd, file, label, blocks, dev_block_count,
1425 sectorsize, stripesize);
1427 fprintf(stderr, "error during mkfs %d\n", ret);
1431 root = open_ctree(file, 0, O_RDWR);
1433 fprintf(stderr, "Open ctree failed\n");
1437 root->fs_info->alloc_start = alloc_start;
1439 ret = make_root_dir(root, mixed);
1441 fprintf(stderr, "failed to setup the root directory\n");
1445 trans = btrfs_start_transaction(root, 1);
1450 btrfs_register_one_device(file);
1454 int old_mixed = mixed;
1456 file = av[optind++];
1457 ret = check_mounted(file);
1459 fprintf(stderr, "error checking %s mount status\n",
1464 fprintf(stderr, "%s is mounted\n", file);
1467 fd = open(file, O_RDWR);
1469 fprintf(stderr, "unable to open %s\n", file);
1472 ret = btrfs_device_already_in_root(root, fd,
1473 BTRFS_SUPER_INFO_OFFSET);
1475 fprintf(stderr, "skipping duplicate device %s in FS\n",
1480 ret = btrfs_prepare_device(fd, file, zero_end, &dev_block_count,
1481 block_count, &mixed, nodiscard);
1485 ret = btrfs_add_to_fsid(trans, root, fd, file, dev_block_count,
1486 sectorsize, sectorsize, sectorsize);
1488 btrfs_register_one_device(file);
1492 if (!source_dir_set) {
1493 ret = create_raid_groups(trans, root, data_profile,
1494 data_profile_opt, metadata_profile,
1495 metadata_profile_opt, mixed, ssd);
1499 ret = create_data_reloc_tree(trans, root);
1502 super = &root->fs_info->super_copy;
1503 flags = btrfs_super_incompat_flags(super);
1504 flags |= BTRFS_FEATURE_INCOMPAT_EXTENDED_IREF;
1507 flags |= BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS;
1509 btrfs_set_super_incompat_flags(super, flags);
1511 if ((data_profile | metadata_profile) &
1512 (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6)) {
1513 struct btrfs_super_block *super = &root->fs_info->super_copy;
1514 u64 flags = btrfs_super_incompat_flags(super);
1516 flags |= BTRFS_FEATURE_INCOMPAT_RAID56;
1517 btrfs_set_super_incompat_flags(super, flags);
1518 printf("Setting RAID5/6 feature flag\n");
1521 printf("fs created label %s on %s\n\tnodesize %u leafsize %u "
1522 "sectorsize %u size %s\n",
1523 label, first_file, nodesize, leafsize, sectorsize,
1524 pretty_buf = pretty_sizes(btrfs_super_total_bytes(&root->fs_info->super_copy)));
1527 printf("%s\n", BTRFS_BUILD_VERSION);
1528 btrfs_commit_transaction(trans, root);
1530 if (source_dir_set) {
1531 trans = btrfs_start_transaction(root, 1);
1532 ret = create_chunks(trans, root,
1533 num_of_meta_chunks, size_of_data);
1535 btrfs_commit_transaction(trans, root);
1537 ret = make_image(source_dir, root, fd);
1541 ret = close_ctree(root);