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>
47 #include "transaction.h"
51 static u64 index_cnt = 2;
53 struct directory_name_entry {
57 struct list_head list;
60 static int make_root_dir(struct btrfs_root *root, int mixed)
62 struct btrfs_trans_handle *trans;
63 struct btrfs_key location;
69 trans = btrfs_start_transaction(root, 1);
70 bytes_used = btrfs_super_bytes_used(root->fs_info->super_copy);
72 root->fs_info->system_allocs = 1;
73 ret = btrfs_make_block_group(trans, root, bytes_used,
74 BTRFS_BLOCK_GROUP_SYSTEM,
75 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
76 0, BTRFS_MKFS_SYSTEM_GROUP_SIZE);
80 ret = btrfs_alloc_chunk(trans, root->fs_info->extent_root,
81 &chunk_start, &chunk_size,
82 BTRFS_BLOCK_GROUP_METADATA |
83 BTRFS_BLOCK_GROUP_DATA);
85 ret = btrfs_make_block_group(trans, root, 0,
86 BTRFS_BLOCK_GROUP_METADATA |
87 BTRFS_BLOCK_GROUP_DATA,
88 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
89 chunk_start, chunk_size);
91 printf("Created a data/metadata chunk of size %llu\n", chunk_size);
93 ret = btrfs_alloc_chunk(trans, root->fs_info->extent_root,
94 &chunk_start, &chunk_size,
95 BTRFS_BLOCK_GROUP_METADATA);
97 ret = btrfs_make_block_group(trans, root, 0,
98 BTRFS_BLOCK_GROUP_METADATA,
99 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
100 chunk_start, chunk_size);
104 root->fs_info->system_allocs = 0;
105 btrfs_commit_transaction(trans, root);
106 trans = btrfs_start_transaction(root, 1);
110 ret = btrfs_alloc_chunk(trans, root->fs_info->extent_root,
111 &chunk_start, &chunk_size,
112 BTRFS_BLOCK_GROUP_DATA);
114 ret = btrfs_make_block_group(trans, root, 0,
115 BTRFS_BLOCK_GROUP_DATA,
116 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
117 chunk_start, chunk_size);
121 ret = btrfs_make_root_dir(trans, root->fs_info->tree_root,
122 BTRFS_ROOT_TREE_DIR_OBJECTID);
125 ret = btrfs_make_root_dir(trans, root, BTRFS_FIRST_FREE_OBJECTID);
128 memcpy(&location, &root->fs_info->fs_root->root_key, sizeof(location));
129 location.offset = (u64)-1;
130 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
132 btrfs_super_root_dir(root->fs_info->super_copy),
133 &location, BTRFS_FT_DIR, 0);
137 ret = btrfs_insert_inode_ref(trans, root->fs_info->tree_root,
138 "default", 7, location.objectid,
139 BTRFS_ROOT_TREE_DIR_OBJECTID, 0);
143 btrfs_commit_transaction(trans, root);
148 static int recow_roots(struct btrfs_trans_handle *trans,
149 struct btrfs_root *root)
152 struct extent_buffer *tmp;
153 struct btrfs_fs_info *info = root->fs_info;
155 ret = __btrfs_cow_block(trans, info->fs_root, info->fs_root->node,
156 NULL, 0, &tmp, 0, 0);
158 free_extent_buffer(tmp);
160 ret = __btrfs_cow_block(trans, info->tree_root, info->tree_root->node,
161 NULL, 0, &tmp, 0, 0);
163 free_extent_buffer(tmp);
165 ret = __btrfs_cow_block(trans, info->extent_root,
166 info->extent_root->node, NULL, 0, &tmp, 0, 0);
168 free_extent_buffer(tmp);
170 ret = __btrfs_cow_block(trans, info->chunk_root, info->chunk_root->node,
171 NULL, 0, &tmp, 0, 0);
173 free_extent_buffer(tmp);
176 ret = __btrfs_cow_block(trans, info->dev_root, info->dev_root->node,
177 NULL, 0, &tmp, 0, 0);
179 free_extent_buffer(tmp);
181 ret = __btrfs_cow_block(trans, info->csum_root, info->csum_root->node,
182 NULL, 0, &tmp, 0, 0);
184 free_extent_buffer(tmp);
189 static int create_one_raid_group(struct btrfs_trans_handle *trans,
190 struct btrfs_root *root, u64 type)
196 ret = btrfs_alloc_chunk(trans, root->fs_info->extent_root,
197 &chunk_start, &chunk_size, type);
199 ret = btrfs_make_block_group(trans, root->fs_info->extent_root, 0,
200 type, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
201 chunk_start, chunk_size);
206 static int create_raid_groups(struct btrfs_trans_handle *trans,
207 struct btrfs_root *root, u64 data_profile,
208 int data_profile_opt, u64 metadata_profile,
209 int metadata_profile_opt, int mixed, int ssd)
211 u64 num_devices = btrfs_super_num_devices(root->fs_info->super_copy);
213 u64 devices_for_raid = num_devices;
217 * Set default profiles according to number of added devices.
218 * For mixed groups defaults are single/single.
220 if (!metadata_profile_opt && !mixed) {
221 if (num_devices == 1 && ssd)
222 printf("Detected a SSD, turning off metadata "
223 "duplication. Mkfs with -m dup if you want to "
224 "force metadata duplication.\n");
225 metadata_profile = (num_devices > 1) ?
226 BTRFS_BLOCK_GROUP_RAID1 : (ssd) ? 0: BTRFS_BLOCK_GROUP_DUP;
228 if (!data_profile_opt && !mixed) {
229 data_profile = (num_devices > 1) ?
230 BTRFS_BLOCK_GROUP_RAID0 : 0; /* raid0 or single */
233 if (devices_for_raid > 4)
234 devices_for_raid = 4;
236 switch (devices_for_raid) {
239 allowed |= BTRFS_BLOCK_GROUP_RAID10;
241 allowed |= BTRFS_BLOCK_GROUP_RAID6;
243 allowed |= BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1 |
244 BTRFS_BLOCK_GROUP_RAID5;
247 allowed |= BTRFS_BLOCK_GROUP_DUP;
250 if (metadata_profile & ~allowed) {
251 fprintf(stderr, "unable to create FS with metadata "
252 "profile %llu (have %llu devices)\n", metadata_profile,
256 if (data_profile & ~allowed) {
257 fprintf(stderr, "unable to create FS with data "
258 "profile %llu (have %llu devices)\n", data_profile,
263 /* allow dup'ed data chunks only in mixed mode */
264 if (!mixed && (data_profile & BTRFS_BLOCK_GROUP_DUP)) {
265 fprintf(stderr, "dup for data is allowed only in mixed mode\n");
269 if (allowed & metadata_profile) {
270 u64 meta_flags = BTRFS_BLOCK_GROUP_METADATA;
272 ret = create_one_raid_group(trans, root,
273 BTRFS_BLOCK_GROUP_SYSTEM |
274 (allowed & metadata_profile));
278 meta_flags |= BTRFS_BLOCK_GROUP_DATA;
280 ret = create_one_raid_group(trans, root, meta_flags |
281 (allowed & metadata_profile));
284 ret = recow_roots(trans, root);
287 if (!mixed && num_devices > 1 && (allowed & data_profile)) {
288 ret = create_one_raid_group(trans, root,
289 BTRFS_BLOCK_GROUP_DATA |
290 (allowed & data_profile));
296 static int create_data_reloc_tree(struct btrfs_trans_handle *trans,
297 struct btrfs_root *root)
299 struct btrfs_key location;
300 struct btrfs_root_item root_item;
301 struct extent_buffer *tmp;
302 u64 objectid = BTRFS_DATA_RELOC_TREE_OBJECTID;
305 ret = btrfs_copy_root(trans, root, root->node, &tmp, objectid);
308 memcpy(&root_item, &root->root_item, sizeof(root_item));
309 btrfs_set_root_bytenr(&root_item, tmp->start);
310 btrfs_set_root_level(&root_item, btrfs_header_level(tmp));
311 btrfs_set_root_generation(&root_item, trans->transid);
312 free_extent_buffer(tmp);
314 location.objectid = objectid;
315 location.type = BTRFS_ROOT_ITEM_KEY;
317 ret = btrfs_insert_root(trans, root->fs_info->tree_root,
318 &location, &root_item);
323 static void print_usage(void)
325 fprintf(stderr, "usage: mkfs.btrfs [options] dev [ dev ... ]\n");
326 fprintf(stderr, "options:\n");
327 fprintf(stderr, "\t -A --alloc-start the offset to start the FS\n");
328 fprintf(stderr, "\t -b --byte-count total number of bytes in the FS\n");
329 fprintf(stderr, "\t -d --data data profile, raid0, raid1, raid5, raid6, raid10, dup or single\n");
330 fprintf(stderr, "\t -l --leafsize size of btree leaves\n");
331 fprintf(stderr, "\t -L --label set a label\n");
332 fprintf(stderr, "\t -m --metadata metadata profile, values like data profile\n");
333 fprintf(stderr, "\t -M --mixed mix metadata and data together\n");
334 fprintf(stderr, "\t -n --nodesize size of btree nodes\n");
335 fprintf(stderr, "\t -s --sectorsize min block allocation\n");
336 fprintf(stderr, "\t -r --rootdir the source directory\n");
337 fprintf(stderr, "\t -K --nodiscard do not perform whole device TRIM\n");
338 fprintf(stderr, "\t -V --version print the mkfs.btrfs version and exit\n");
339 fprintf(stderr, "%s\n", BTRFS_BUILD_VERSION);
343 static void print_version(void)
345 fprintf(stderr, "mkfs.btrfs, part of %s\n", BTRFS_BUILD_VERSION);
349 static u64 parse_profile(char *s)
351 if (strcmp(s, "raid0") == 0) {
352 return BTRFS_BLOCK_GROUP_RAID0;
353 } else if (strcmp(s, "raid1") == 0) {
354 return BTRFS_BLOCK_GROUP_RAID1;
355 } else if (strcmp(s, "raid5") == 0) {
356 return BTRFS_BLOCK_GROUP_RAID5;
357 } else if (strcmp(s, "raid6") == 0) {
358 return BTRFS_BLOCK_GROUP_RAID6;
359 } else if (strcmp(s, "raid10") == 0) {
360 return BTRFS_BLOCK_GROUP_RAID10;
361 } else if (strcmp(s, "dup") == 0) {
362 return BTRFS_BLOCK_GROUP_DUP;
363 } else if (strcmp(s, "single") == 0) {
366 fprintf(stderr, "Unknown profile %s\n", s);
373 static char *parse_label(char *input)
375 int len = strlen(input);
377 if (len >= BTRFS_LABEL_SIZE) {
378 fprintf(stderr, "Label %s is too long (max %d)\n", input,
379 BTRFS_LABEL_SIZE - 1);
382 return strdup(input);
385 static struct option long_options[] = {
386 { "alloc-start", 1, NULL, 'A'},
387 { "byte-count", 1, NULL, 'b' },
388 { "leafsize", 1, NULL, 'l' },
389 { "label", 1, NULL, 'L'},
390 { "metadata", 1, NULL, 'm' },
391 { "mixed", 0, NULL, 'M' },
392 { "nodesize", 1, NULL, 'n' },
393 { "sectorsize", 1, NULL, 's' },
394 { "data", 1, NULL, 'd' },
395 { "version", 0, NULL, 'V' },
396 { "rootdir", 1, NULL, 'r' },
397 { "nodiscard", 0, NULL, 'K' },
401 static int add_directory_items(struct btrfs_trans_handle *trans,
402 struct btrfs_root *root, u64 objectid,
403 ino_t parent_inum, const char *name,
404 struct stat *st, int *dir_index_cnt)
408 struct btrfs_key location;
411 name_len = strlen(name);
413 location.objectid = objectid;
415 btrfs_set_key_type(&location, BTRFS_INODE_ITEM_KEY);
417 if (S_ISDIR(st->st_mode))
418 filetype = BTRFS_FT_DIR;
419 if (S_ISREG(st->st_mode))
420 filetype = BTRFS_FT_REG_FILE;
421 if (S_ISLNK(st->st_mode))
422 filetype = BTRFS_FT_SYMLINK;
424 ret = btrfs_insert_dir_item(trans, root, name, name_len,
425 parent_inum, &location,
426 filetype, index_cnt);
428 *dir_index_cnt = index_cnt;
434 static int fill_inode_item(struct btrfs_trans_handle *trans,
435 struct btrfs_root *root,
436 struct btrfs_inode_item *dst, struct stat *src)
439 u64 sectorsize = root->sectorsize;
442 * btrfs_inode_item has some reserved fields
443 * and represents on-disk inode entry, so
444 * zero everything to prevent information leak
446 memset(dst, 0, sizeof (*dst));
448 btrfs_set_stack_inode_generation(dst, trans->transid);
449 btrfs_set_stack_inode_size(dst, src->st_size);
450 btrfs_set_stack_inode_nbytes(dst, 0);
451 btrfs_set_stack_inode_block_group(dst, 0);
452 btrfs_set_stack_inode_nlink(dst, src->st_nlink);
453 btrfs_set_stack_inode_uid(dst, src->st_uid);
454 btrfs_set_stack_inode_gid(dst, src->st_gid);
455 btrfs_set_stack_inode_mode(dst, src->st_mode);
456 btrfs_set_stack_inode_rdev(dst, 0);
457 btrfs_set_stack_inode_flags(dst, 0);
458 btrfs_set_stack_timespec_sec(&dst->atime, src->st_atime);
459 btrfs_set_stack_timespec_nsec(&dst->atime, 0);
460 btrfs_set_stack_timespec_sec(&dst->ctime, src->st_ctime);
461 btrfs_set_stack_timespec_nsec(&dst->ctime, 0);
462 btrfs_set_stack_timespec_sec(&dst->mtime, src->st_mtime);
463 btrfs_set_stack_timespec_nsec(&dst->mtime, 0);
464 btrfs_set_stack_timespec_sec(&dst->otime, 0);
465 btrfs_set_stack_timespec_nsec(&dst->otime, 0);
467 if (S_ISDIR(src->st_mode)) {
468 btrfs_set_stack_inode_size(dst, 0);
469 btrfs_set_stack_inode_nlink(dst, 1);
471 if (S_ISREG(src->st_mode)) {
472 btrfs_set_stack_inode_size(dst, (u64)src->st_size);
473 if (src->st_size <= BTRFS_MAX_INLINE_DATA_SIZE(root))
474 btrfs_set_stack_inode_nbytes(dst, src->st_size);
476 blocks = src->st_size / sectorsize;
477 if (src->st_size % sectorsize)
479 blocks *= sectorsize;
480 btrfs_set_stack_inode_nbytes(dst, blocks);
483 if (S_ISLNK(src->st_mode))
484 btrfs_set_stack_inode_nbytes(dst, src->st_size + 1);
489 static int directory_select(const struct direct *entry)
491 if ((strncmp(entry->d_name, ".", entry->d_reclen) == 0) ||
492 (strncmp(entry->d_name, "..", entry->d_reclen) == 0))
498 static void free_namelist(struct direct **files, int count)
505 for (i = 0; i < count; ++i)
510 static u64 calculate_dir_inode_size(char *dirname)
513 struct direct **files, *cur_file;
514 u64 dir_inode_size = 0;
516 count = scandir(dirname, &files, directory_select, NULL);
518 for (i = 0; i < count; i++) {
520 dir_inode_size += strlen(cur_file->d_name);
523 free_namelist(files, count);
526 return dir_inode_size;
529 static int add_inode_items(struct btrfs_trans_handle *trans,
530 struct btrfs_root *root,
531 struct stat *st, char *name,
532 u64 self_objectid, ino_t parent_inum,
533 int dir_index_cnt, struct btrfs_inode_item *inode_ret)
536 struct btrfs_key inode_key;
537 struct btrfs_inode_item btrfs_inode;
542 name_len = strlen(name);
543 fill_inode_item(trans, root, &btrfs_inode, st);
544 objectid = self_objectid;
546 if (S_ISDIR(st->st_mode)) {
547 inode_size = calculate_dir_inode_size(name);
548 btrfs_set_stack_inode_size(&btrfs_inode, inode_size);
551 inode_key.objectid = objectid;
552 inode_key.offset = 0;
553 btrfs_set_key_type(&inode_key, BTRFS_INODE_ITEM_KEY);
555 ret = btrfs_insert_inode(trans, root, objectid, &btrfs_inode);
559 ret = btrfs_insert_inode_ref(trans, root, name, name_len,
560 objectid, parent_inum, dir_index_cnt);
564 *inode_ret = btrfs_inode;
569 static int add_xattr_item(struct btrfs_trans_handle *trans,
570 struct btrfs_root *root, u64 objectid,
571 const char *file_name)
575 char xattr_list[XATTR_LIST_MAX];
577 char cur_value[XATTR_SIZE_MAX];
578 char delimiter = '\0';
579 char *next_location = xattr_list;
581 ret = llistxattr(file_name, xattr_list, XATTR_LIST_MAX);
585 fprintf(stderr, "get a list of xattr failed for %s\n",
592 cur_name = strtok(xattr_list, &delimiter);
593 while (cur_name != NULL) {
594 cur_name_len = strlen(cur_name);
595 next_location += cur_name_len + 1;
597 ret = getxattr(file_name, cur_name, cur_value, XATTR_SIZE_MAX);
601 fprintf(stderr, "get a xattr value failed for %s attr %s\n",
602 file_name, cur_name);
606 ret = btrfs_insert_xattr_item(trans, root, cur_name,
607 cur_name_len, cur_value,
610 fprintf(stderr, "insert a xattr item failed for %s\n",
614 cur_name = strtok(next_location, &delimiter);
619 static int custom_alloc_extent(struct btrfs_root *root, u64 num_bytes,
620 u64 hint_byte, struct btrfs_key *ins)
624 u64 last = hint_byte;
627 struct btrfs_block_group_cache *cache;
630 ret = find_first_extent_bit(&root->fs_info->free_space_cache,
631 last, &start, &end, EXTENT_DIRTY);
633 if (wrapped++ == 0) {
641 start = max(last, start);
643 if (last - start < num_bytes)
646 last = start + num_bytes;
647 if (test_range_bit(&root->fs_info->pinned_extents,
648 start, last - 1, EXTENT_DIRTY, 0))
651 cache = btrfs_lookup_block_group(root->fs_info, start);
653 if (cache->flags & BTRFS_BLOCK_GROUP_SYSTEM ||
654 last > cache->key.objectid + cache->key.offset) {
655 last = cache->key.objectid + cache->key.offset;
659 if (cache->flags & (BTRFS_BLOCK_GROUP_SYSTEM |
660 BTRFS_BLOCK_GROUP_METADATA)) {
661 last = cache->key.objectid + cache->key.offset;
665 clear_extent_dirty(&root->fs_info->free_space_cache,
666 start, start + num_bytes - 1, 0);
668 ins->objectid = start;
669 ins->offset = num_bytes;
670 ins->type = BTRFS_EXTENT_ITEM_KEY;
674 fprintf(stderr, "not enough free space\n");
678 static int record_file_extent(struct btrfs_trans_handle *trans,
679 struct btrfs_root *root, u64 objectid,
680 struct btrfs_inode_item *inode,
681 u64 file_pos, u64 disk_bytenr,
685 struct btrfs_fs_info *info = root->fs_info;
686 struct btrfs_root *extent_root = info->extent_root;
687 struct extent_buffer *leaf;
688 struct btrfs_file_extent_item *fi;
689 struct btrfs_key ins_key;
690 struct btrfs_path path;
691 struct btrfs_extent_item *ei;
693 btrfs_init_path(&path);
695 ins_key.objectid = objectid;
697 btrfs_set_key_type(&ins_key, BTRFS_EXTENT_DATA_KEY);
698 ret = btrfs_insert_empty_item(trans, root, &path, &ins_key,
702 leaf = path.nodes[0];
703 fi = btrfs_item_ptr(leaf, path.slots[0],
704 struct btrfs_file_extent_item);
705 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
706 btrfs_set_file_extent_type(leaf, fi, BTRFS_FILE_EXTENT_REG);
707 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
708 btrfs_set_file_extent_disk_num_bytes(leaf, fi, num_bytes);
709 btrfs_set_file_extent_offset(leaf, fi, 0);
710 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
711 btrfs_set_file_extent_ram_bytes(leaf, fi, num_bytes);
712 btrfs_set_file_extent_compression(leaf, fi, 0);
713 btrfs_set_file_extent_encryption(leaf, fi, 0);
714 btrfs_set_file_extent_other_encoding(leaf, fi, 0);
715 btrfs_mark_buffer_dirty(leaf);
717 btrfs_release_path(root, &path);
719 ins_key.objectid = disk_bytenr;
720 ins_key.offset = num_bytes;
721 ins_key.type = BTRFS_EXTENT_ITEM_KEY;
723 ret = btrfs_insert_empty_item(trans, extent_root, &path,
724 &ins_key, sizeof(*ei));
726 leaf = path.nodes[0];
727 ei = btrfs_item_ptr(leaf, path.slots[0],
728 struct btrfs_extent_item);
730 btrfs_set_extent_refs(leaf, ei, 0);
731 btrfs_set_extent_generation(leaf, ei, trans->transid);
732 btrfs_set_extent_flags(leaf, ei, BTRFS_EXTENT_FLAG_DATA);
734 btrfs_mark_buffer_dirty(leaf);
735 ret = btrfs_update_block_group(trans, root, disk_bytenr,
739 } else if (ret != -EEXIST) {
743 ret = btrfs_inc_extent_ref(trans, root, disk_bytenr, num_bytes, 0,
744 root->root_key.objectid,
747 btrfs_release_path(root, &path);
751 static int add_symbolic_link(struct btrfs_trans_handle *trans,
752 struct btrfs_root *root,
753 u64 objectid, const char *path_name)
756 u64 sectorsize = root->sectorsize;
757 char *buf = malloc(sectorsize);
759 ret = readlink(path_name, buf, sectorsize);
761 fprintf(stderr, "readlink failed for %s\n", path_name);
764 if (ret >= sectorsize) {
765 fprintf(stderr, "symlink too long for %s", path_name);
770 buf[ret] = '\0'; /* readlink does not do it for us */
771 ret = btrfs_insert_inline_extent(trans, root, objectid, 0,
778 static int add_file_items(struct btrfs_trans_handle *trans,
779 struct btrfs_root *root,
780 struct btrfs_inode_item *btrfs_inode, u64 objectid,
781 ino_t parent_inum, struct stat *st,
782 const char *path_name, int out_fd)
788 struct btrfs_key key;
790 u32 sectorsize = root->sectorsize;
795 fd = open(path_name, O_RDONLY);
797 fprintf(stderr, "%s open failed\n", path_name);
801 blocks = st->st_size / sectorsize;
802 if (st->st_size % sectorsize)
805 if (st->st_size <= BTRFS_MAX_INLINE_DATA_SIZE(root)) {
806 buffer = malloc(st->st_size);
807 ret_read = pread64(fd, buffer, st->st_size, bytes_read);
808 if (ret_read == -1) {
809 fprintf(stderr, "%s read failed\n", path_name);
813 ret = btrfs_insert_inline_extent(trans, root, objectid, 0,
814 buffer, st->st_size);
818 ret = custom_alloc_extent(root, blocks * sectorsize, 0, &key);
822 first_block = key.objectid;
824 buffer = malloc(sectorsize);
827 memset(buffer, 0, sectorsize);
828 ret_read = pread64(fd, buffer, sectorsize, bytes_read);
829 if (ret_read == -1) {
830 fprintf(stderr, "%s read failed\n", path_name);
834 ret = pwrite64(out_fd, buffer, sectorsize,
835 first_block + bytes_read);
836 if (ret != sectorsize) {
837 fprintf(stderr, "output file write failed\n");
841 /* checksum for file data */
842 ret = btrfs_csum_file_block(trans, root->fs_info->csum_root,
843 first_block + (blocks * sectorsize),
844 first_block + bytes_read,
847 fprintf(stderr, "%s checksum failed\n", path_name);
851 bytes_read += ret_read;
853 } while (ret_read == sectorsize);
855 if (num_blocks > 0) {
856 ret = record_file_extent(trans, root, objectid, btrfs_inode,
857 first_block, first_block,
858 blocks * sectorsize);
870 static char *make_path(char *dir, char *name)
874 path = malloc(strlen(dir) + strlen(name) + 2);
878 if (dir[strlen(dir) - 1] != '/')
884 static int traverse_directory(struct btrfs_trans_handle *trans,
885 struct btrfs_root *root, char *dir_name,
886 struct directory_name_entry *dir_head, int out_fd)
890 struct btrfs_inode_item cur_inode;
891 struct btrfs_inode_item *inode_item;
892 int count, i, dir_index_cnt;
893 struct direct **files;
895 struct directory_name_entry *dir_entry, *parent_dir_entry;
896 struct direct *cur_file;
897 ino_t parent_inum, cur_inum;
898 ino_t highest_inum = 0;
899 char *parent_dir_name;
900 struct btrfs_path path;
901 struct extent_buffer *leaf;
902 struct btrfs_key root_dir_key;
903 u64 root_dir_inode_size = 0;
905 /* Add list for source directory */
906 dir_entry = malloc(sizeof(struct directory_name_entry));
907 dir_entry->dir_name = dir_name;
908 dir_entry->path = strdup(dir_name);
910 parent_inum = highest_inum + BTRFS_FIRST_FREE_OBJECTID;
911 dir_entry->inum = parent_inum;
912 list_add_tail(&dir_entry->list, &dir_head->list);
914 btrfs_init_path(&path);
916 root_dir_key.objectid = btrfs_root_dirid(&root->root_item);
917 root_dir_key.offset = 0;
918 btrfs_set_key_type(&root_dir_key, BTRFS_INODE_ITEM_KEY);
919 ret = btrfs_lookup_inode(trans, root, &path, &root_dir_key, 1);
921 fprintf(stderr, "root dir lookup error\n");
925 leaf = path.nodes[0];
926 inode_item = btrfs_item_ptr(leaf, path.slots[0],
927 struct btrfs_inode_item);
929 root_dir_inode_size = calculate_dir_inode_size(dir_name);
930 btrfs_set_inode_size(leaf, inode_item, root_dir_inode_size);
931 btrfs_mark_buffer_dirty(leaf);
933 btrfs_release_path(root, &path);
936 parent_dir_entry = list_entry(dir_head->list.next,
937 struct directory_name_entry,
939 list_del(&parent_dir_entry->list);
941 parent_inum = parent_dir_entry->inum;
942 parent_dir_name = parent_dir_entry->dir_name;
943 if (chdir(parent_dir_entry->path)) {
944 fprintf(stderr, "chdir error for %s\n",
949 count = scandir(parent_dir_entry->path, &files,
950 directory_select, NULL);
953 fprintf(stderr, "scandir for %s failed: %s\n",
954 parent_dir_name, strerror (errno));
958 for (i = 0; i < count; i++) {
961 if (lstat(cur_file->d_name, &st) == -1) {
962 fprintf(stderr, "lstat failed for file %s\n",
967 cur_inum = ++highest_inum + BTRFS_FIRST_FREE_OBJECTID;
968 ret = add_directory_items(trans, root,
969 cur_inum, parent_inum,
971 &st, &dir_index_cnt);
973 fprintf(stderr, "add_directory_items failed\n");
977 ret = add_inode_items(trans, root, &st,
978 cur_file->d_name, cur_inum,
979 parent_inum, dir_index_cnt,
982 fprintf(stderr, "add_inode_items failed\n");
986 ret = add_xattr_item(trans, root,
987 cur_inum, cur_file->d_name);
989 fprintf(stderr, "add_xattr_item failed\n");
994 if (S_ISDIR(st.st_mode)) {
995 dir_entry = malloc(sizeof(struct directory_name_entry));
996 dir_entry->dir_name = cur_file->d_name;
997 dir_entry->path = make_path(parent_dir_entry->path,
999 dir_entry->inum = cur_inum;
1000 list_add_tail(&dir_entry->list, &dir_head->list);
1001 } else if (S_ISREG(st.st_mode)) {
1002 ret = add_file_items(trans, root, &cur_inode,
1003 cur_inum, parent_inum, &st,
1004 cur_file->d_name, out_fd);
1006 fprintf(stderr, "add_file_items failed\n");
1009 } else if (S_ISLNK(st.st_mode)) {
1010 ret = add_symbolic_link(trans, root,
1011 cur_inum, cur_file->d_name);
1013 fprintf(stderr, "add_symbolic_link failed\n");
1019 free_namelist(files, count);
1020 free(parent_dir_entry->path);
1021 free(parent_dir_entry);
1025 } while (!list_empty(&dir_head->list));
1029 free_namelist(files, count);
1031 free(parent_dir_entry->path);
1032 free(parent_dir_entry);
1036 static int open_target(char *output_name)
1039 output_fd = open(output_name, O_CREAT | O_RDWR | O_TRUNC,
1040 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH);
1045 static int create_chunks(struct btrfs_trans_handle *trans,
1046 struct btrfs_root *root, u64 num_of_meta_chunks,
1051 u64 meta_type = BTRFS_BLOCK_GROUP_METADATA;
1052 u64 data_type = BTRFS_BLOCK_GROUP_DATA;
1053 u64 minimum_data_chunk_size = 8 * 1024 * 1024;
1057 for (i = 0; i < num_of_meta_chunks; i++) {
1058 ret = btrfs_alloc_chunk(trans, root->fs_info->extent_root,
1059 &chunk_start, &chunk_size, meta_type);
1061 ret = btrfs_make_block_group(trans, root->fs_info->extent_root, 0,
1062 meta_type, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
1063 chunk_start, chunk_size);
1065 set_extent_dirty(&root->fs_info->free_space_cache,
1066 chunk_start, chunk_start + chunk_size - 1, 0);
1069 if (size_of_data < minimum_data_chunk_size)
1070 size_of_data = minimum_data_chunk_size;
1071 ret = btrfs_alloc_data_chunk(trans, root->fs_info->extent_root,
1072 &chunk_start, size_of_data, data_type);
1074 ret = btrfs_make_block_group(trans, root->fs_info->extent_root, 0,
1075 data_type, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
1076 chunk_start, size_of_data);
1078 set_extent_dirty(&root->fs_info->free_space_cache,
1079 chunk_start, chunk_start + size_of_data - 1, 0);
1083 static int make_image(char *source_dir, struct btrfs_root *root, int out_fd)
1086 struct btrfs_trans_handle *trans;
1088 struct stat root_st;
1090 struct directory_name_entry dir_head;
1092 ret = lstat(source_dir, &root_st);
1094 fprintf(stderr, "unable to lstat the %s\n", source_dir);
1098 INIT_LIST_HEAD(&dir_head.list);
1100 trans = btrfs_start_transaction(root, 1);
1101 ret = traverse_directory(trans, root, source_dir, &dir_head, out_fd);
1103 fprintf(stderr, "unable to traverse_directory\n");
1106 btrfs_commit_transaction(trans, root);
1108 printf("Making image is completed.\n");
1111 fprintf(stderr, "Making image is aborted.\n");
1116 * This ignores symlinks with unreadable targets and subdirs that can't
1117 * be read. It's a best-effort to give a rough estimate of the size of
1118 * a subdir. It doesn't guarantee that prepopulating btrfs from this
1119 * tree won't still run out of space.
1121 * The rounding up to 4096 is questionable. Previous code used du -B 4096.
1123 static u64 global_total_size;
1124 static int ftw_add_entry_size(const char *fpath, const struct stat *st,
1127 if (type == FTW_F || type == FTW_D)
1128 global_total_size += round_up(st->st_size, 4096);
1133 static u64 size_sourcedir(char *dir_name, u64 sectorsize,
1134 u64 *num_of_meta_chunks_ret, u64 *size_of_data_ret)
1139 u64 default_chunk_size = 8 * 1024 * 1024; /* 8MB */
1140 u64 allocated_meta_size = 8 * 1024 * 1024; /* 8MB */
1141 u64 allocated_total_size = 20 * 1024 * 1024; /* 20MB */
1142 u64 num_of_meta_chunks = 0;
1143 u64 num_of_allocated_meta_chunks =
1144 allocated_meta_size / default_chunk_size;
1146 global_total_size = 0;
1147 ret = ftw(dir_name, ftw_add_entry_size, 10);
1148 dir_size = global_total_size;
1150 fprintf(stderr, "ftw subdir walk of '%s' failed: %s\n",
1151 dir_name, strerror(errno));
1155 num_of_meta_chunks = (dir_size / 2) / default_chunk_size;
1156 if (((dir_size / 2) % default_chunk_size) != 0)
1157 num_of_meta_chunks++;
1158 if (num_of_meta_chunks <= num_of_allocated_meta_chunks)
1159 num_of_meta_chunks = 0;
1161 num_of_meta_chunks -= num_of_allocated_meta_chunks;
1163 total_size = allocated_total_size + dir_size +
1164 (num_of_meta_chunks * default_chunk_size);
1166 *num_of_meta_chunks_ret = num_of_meta_chunks;
1171 static int zero_output_file(int out_fd, u64 size, u32 sectorsize)
1173 int len = sectorsize;
1174 int loop_num = size / sectorsize;
1176 char *buf = malloc(len);
1182 memset(buf, 0, len);
1183 for (i = 0; i < loop_num; i++) {
1184 written = pwrite64(out_fd, buf, len, location);
1187 location += sectorsize;
1193 static int check_leaf_or_node_size(u32 size, u32 sectorsize)
1195 if (size < sectorsize) {
1197 "Illegal leafsize (or nodesize) %u (smaller than %u)\n",
1200 } else if (size > BTRFS_MAX_METADATA_BLOCKSIZE) {
1202 "Illegal leafsize (or nodesize) %u (larger than %u)\n",
1203 size, BTRFS_MAX_METADATA_BLOCKSIZE);
1205 } else if (size & (sectorsize - 1)) {
1207 "Illegal leafsize (or nodesize) %u (not align to %u)\n",
1214 static int is_ssd(const char *file)
1219 char path[PATH_MAX];
1224 probe = blkid_new_probe_from_filename(file);
1229 * We want to use blkid_devno_to_wholedisk() but it's broken for some
1230 * reason on F17 at least so we'll do this trickery
1232 disk = blkid_probe_get_wholedisk_devno(probe);
1236 devname = blkid_devno_to_devname(disk);
1240 dev = strrchr(devname, '/');
1243 snprintf(path, PATH_MAX, "/sys/block/%s/queue/rotational", dev);
1246 blkid_free_probe(probe);
1248 fd = open(path, O_RDONLY);
1253 if (read(fd, &rotational, sizeof(char)) < sizeof(char)) {
1259 return !atoi((const char *)&rotational);
1263 * Check for existing filesystem or partition table on device.
1265 * 1 for existing fs or partition
1266 * 0 for nothing found
1267 * -1 for internal error
1274 blkid_probe pr = NULL;
1278 if (!device || !*device)
1281 ret = -1; /* will reset on success of all setup calls */
1283 pr = blkid_new_probe_from_filename(device);
1287 size = blkid_probe_get_size(pr);
1291 /* nothing to overwrite on a 0-length device */
1297 ret = blkid_probe_enable_partitions(pr, 1);
1301 ret = blkid_do_fullprobe(pr);
1306 * Blkid returns 1 for nothing found and 0 when it finds a signature,
1307 * but we want the exact opposite, so reverse the return value here.
1309 * In addition print some useful diagnostics about what actually is
1317 if (!blkid_probe_lookup_value(pr, "TYPE", &type, NULL)) {
1319 "%s appears to contain an existing "
1320 "filesystem (%s).\n", device, type);
1321 } else if (!blkid_probe_lookup_value(pr, "PTTYPE", &type, NULL)) {
1323 "%s appears to contain a partition "
1324 "table (%s).\n", device, type);
1327 "%s appears to contain something weird "
1328 "according to blkid\n", device);
1334 blkid_free_probe(pr);
1337 "probe of %s failed, cannot detect "
1338 "existing filesystem.\n", device);
1342 int main(int ac, char **av)
1345 struct btrfs_root *root;
1346 struct btrfs_trans_handle *trans;
1349 u64 block_count = 0;
1350 u64 dev_block_count = 0;
1352 u64 alloc_start = 0;
1353 u64 metadata_profile = 0;
1354 u64 data_profile = 0;
1355 u32 leafsize = sysconf(_SC_PAGESIZE);
1356 u32 sectorsize = 4096;
1357 u32 nodesize = leafsize;
1358 u32 stripesize = 4096;
1360 int option_index = 0;
1365 int data_profile_opt = 0;
1366 int metadata_profile_opt = 0;
1369 int force_overwrite = 0;
1371 char *source_dir = NULL;
1372 int source_dir_set = 0;
1373 u64 num_of_meta_chunks = 0;
1374 u64 size_of_data = 0;
1375 u64 source_dir_size = 0;
1377 struct btrfs_super_block *super;
1382 c = getopt_long(ac, av, "A:b:fl:n:s:m:d:L:r:VMK", long_options,
1388 alloc_start = parse_size(optarg);
1391 force_overwrite = 1;
1394 data_profile = parse_profile(optarg);
1395 data_profile_opt = 1;
1399 nodesize = parse_size(optarg);
1400 leafsize = parse_size(optarg);
1403 label = parse_label(optarg);
1406 metadata_profile = parse_profile(optarg);
1407 metadata_profile_opt = 1;
1413 sectorsize = parse_size(optarg);
1416 block_count = parse_size(optarg);
1417 if (block_count <= 1024*1024*1024) {
1418 printf("SMALL VOLUME: forcing mixed "
1419 "metadata/data groups\n");
1428 source_dir = optarg;
1438 sectorsize = max(sectorsize, (u32)sysconf(_SC_PAGESIZE));
1439 if (check_leaf_or_node_size(leafsize, sectorsize))
1441 if (check_leaf_or_node_size(nodesize, sectorsize))
1447 printf("\nWARNING! - %s IS EXPERIMENTAL\n", BTRFS_BUILD_VERSION);
1448 printf("WARNING! - see http://btrfs.wiki.kernel.org before using\n\n");
1450 if (source_dir == 0) {
1451 file = av[optind++];
1452 ret = is_swap_device(file);
1454 fprintf(stderr, "error checking %s status: %s\n", file,
1459 fprintf(stderr, "%s is a swap device\n", file);
1462 if (!force_overwrite) {
1463 if (check_overwrite(file)) {
1464 fprintf(stderr, "Use the -f option to force overwrite.\n");
1468 ret = check_mounted(file);
1470 fprintf(stderr, "error checking %s mount status\n", file);
1474 fprintf(stderr, "%s is mounted\n", file);
1478 /* check if the device is busy */
1479 fd = open(file, O_RDWR|O_EXCL);
1481 fprintf(stderr, "unable to open %s: %s\n", file,
1487 * open again without O_EXCL so that the problem should not
1488 * occur by the following processing.
1489 * (btrfs_register_one_device() fails if O_EXCL is on)
1491 fd = open(file, O_RDWR);
1493 fprintf(stderr, "unable to open %s: %s\n", file,
1498 ret = btrfs_prepare_device(fd, file, zero_end, &dev_block_count,
1499 block_count, &mixed, nodiscard);
1500 if (block_count && block_count > dev_block_count) {
1501 fprintf(stderr, "%s is smaller than requested size\n", file);
1506 file = av[optind++];
1507 fd = open_target(file);
1509 fprintf(stderr, "unable to open the %s\n", file);
1514 source_dir_size = size_sourcedir(source_dir, sectorsize,
1515 &num_of_meta_chunks, &size_of_data);
1516 if(block_count < source_dir_size)
1517 block_count = source_dir_size;
1518 ret = zero_output_file(fd, block_count, sectorsize);
1520 fprintf(stderr, "unable to zero the output file\n");
1523 /* our "device" is the new image file */
1524 dev_block_count = block_count;
1530 if (metadata_profile != data_profile) {
1531 fprintf(stderr, "With mixed block groups data and metadata "
1532 "profiles must be the same\n");
1537 blocks[0] = BTRFS_SUPER_INFO_OFFSET;
1538 for (i = 1; i < 7; i++) {
1539 blocks[i] = BTRFS_SUPER_INFO_OFFSET + 1024 * 1024 +
1543 ret = make_btrfs(fd, file, label, blocks, dev_block_count,
1545 sectorsize, stripesize);
1547 fprintf(stderr, "error during mkfs %d\n", ret);
1551 root = open_ctree(file, 0, O_RDWR);
1553 fprintf(stderr, "Open ctree failed\n");
1557 root->fs_info->alloc_start = alloc_start;
1559 ret = make_root_dir(root, mixed);
1561 fprintf(stderr, "failed to setup the root directory\n");
1565 trans = btrfs_start_transaction(root, 1);
1570 btrfs_register_one_device(file);
1574 int old_mixed = mixed;
1576 file = av[optind++];
1577 if (!force_overwrite) {
1578 if (check_overwrite(file)) {
1579 fprintf(stderr, "Use the -f option to force overwrite.\n");
1584 ret = is_swap_device(file);
1586 fprintf(stderr, "error checking %s status: %s\n", file,
1591 fprintf(stderr, "%s is a swap device\n", file);
1594 ret = check_mounted(file);
1596 fprintf(stderr, "error checking %s mount status\n",
1601 fprintf(stderr, "%s is mounted\n", file);
1604 /* check if the device is busy */
1605 fd = open(file, O_RDWR|O_EXCL);
1607 fprintf(stderr, "unable to open %s: %s\n", file,
1613 * open again without O_EXCL so that the problem should not
1614 * occur by the following processing.
1615 * (btrfs_register_one_device() fails if O_EXCL is on)
1617 fd = open(file, O_RDWR);
1619 fprintf(stderr, "unable to open %s: %s\n", file,
1623 ret = btrfs_device_already_in_root(root, fd,
1624 BTRFS_SUPER_INFO_OFFSET);
1626 fprintf(stderr, "skipping duplicate device %s in FS\n",
1631 ret = btrfs_prepare_device(fd, file, zero_end, &dev_block_count,
1632 block_count, &mixed, nodiscard);
1636 ret = btrfs_add_to_fsid(trans, root, fd, file, dev_block_count,
1637 sectorsize, sectorsize, sectorsize);
1639 btrfs_register_one_device(file);
1643 if (!source_dir_set) {
1644 ret = create_raid_groups(trans, root, data_profile,
1645 data_profile_opt, metadata_profile,
1646 metadata_profile_opt, mixed, ssd);
1650 ret = create_data_reloc_tree(trans, root);
1653 super = root->fs_info->super_copy;
1654 flags = btrfs_super_incompat_flags(super);
1655 flags |= BTRFS_FEATURE_INCOMPAT_EXTENDED_IREF;
1658 flags |= BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS;
1660 btrfs_set_super_incompat_flags(super, flags);
1662 if ((data_profile | metadata_profile) &
1663 (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6)) {
1664 struct btrfs_super_block *super = root->fs_info->super_copy;
1665 u64 flags = btrfs_super_incompat_flags(super);
1667 flags |= BTRFS_FEATURE_INCOMPAT_RAID56;
1668 btrfs_set_super_incompat_flags(super, flags);
1669 printf("Setting RAID5/6 feature flag\n");
1672 printf("fs created label %s on %s\n\tnodesize %u leafsize %u "
1673 "sectorsize %u size %s\n",
1674 label, first_file, nodesize, leafsize, sectorsize,
1675 pretty_buf = pretty_sizes(btrfs_super_total_bytes(root->fs_info->super_copy)));
1678 printf("%s\n", BTRFS_BUILD_VERSION);
1679 btrfs_commit_transaction(trans, root);
1681 if (source_dir_set) {
1682 trans = btrfs_start_transaction(root, 1);
1683 ret = create_chunks(trans, root,
1684 num_of_meta_chunks, size_of_data);
1686 btrfs_commit_transaction(trans, root);
1688 ret = make_image(source_dir, root, fd);
1692 ret = close_ctree(root);