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 void __recow_root(struct btrfs_trans_handle *trans,
149 struct btrfs_root *root)
152 struct extent_buffer *tmp;
154 if (trans->transid != btrfs_root_generation(&root->root_item)) {
155 ret = __btrfs_cow_block(trans, root, root->node,
156 NULL, 0, &tmp, 0, 0);
158 free_extent_buffer(tmp);
162 static void recow_roots(struct btrfs_trans_handle *trans,
163 struct btrfs_root *root)
165 struct btrfs_fs_info *info = root->fs_info;
167 __recow_root(trans, info->fs_root);
168 __recow_root(trans, info->tree_root);
169 __recow_root(trans, info->extent_root);
170 __recow_root(trans, info->chunk_root);
171 __recow_root(trans, info->dev_root);
172 __recow_root(trans, info->csum_root);
175 static int create_one_raid_group(struct btrfs_trans_handle *trans,
176 struct btrfs_root *root, u64 type)
182 ret = btrfs_alloc_chunk(trans, root->fs_info->extent_root,
183 &chunk_start, &chunk_size, type);
185 ret = btrfs_make_block_group(trans, root->fs_info->extent_root, 0,
186 type, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
187 chunk_start, chunk_size);
192 static int create_raid_groups(struct btrfs_trans_handle *trans,
193 struct btrfs_root *root, u64 data_profile,
194 int data_profile_opt, u64 metadata_profile,
195 int metadata_profile_opt, int mixed, int ssd)
197 u64 num_devices = btrfs_super_num_devices(root->fs_info->super_copy);
199 u64 devices_for_raid = num_devices;
203 * Set default profiles according to number of added devices.
204 * For mixed groups defaults are single/single.
206 if (!metadata_profile_opt && !mixed) {
207 if (num_devices == 1 && ssd)
208 printf("Detected a SSD, turning off metadata "
209 "duplication. Mkfs with -m dup if you want to "
210 "force metadata duplication.\n");
211 metadata_profile = (num_devices > 1) ?
212 BTRFS_BLOCK_GROUP_RAID1 : (ssd) ? 0: BTRFS_BLOCK_GROUP_DUP;
214 if (!data_profile_opt && !mixed) {
215 data_profile = (num_devices > 1) ?
216 BTRFS_BLOCK_GROUP_RAID0 : 0; /* raid0 or single */
219 if (devices_for_raid > 4)
220 devices_for_raid = 4;
222 switch (devices_for_raid) {
225 allowed |= BTRFS_BLOCK_GROUP_RAID10;
227 allowed |= BTRFS_BLOCK_GROUP_RAID6;
229 allowed |= BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1 |
230 BTRFS_BLOCK_GROUP_RAID5;
233 allowed |= BTRFS_BLOCK_GROUP_DUP;
236 if (metadata_profile & ~allowed) {
237 fprintf(stderr, "unable to create FS with metadata "
238 "profile %llu (have %llu devices)\n", metadata_profile,
242 if (data_profile & ~allowed) {
243 fprintf(stderr, "unable to create FS with data "
244 "profile %llu (have %llu devices)\n", data_profile,
249 /* allow dup'ed data chunks only in mixed mode */
250 if (!mixed && (data_profile & BTRFS_BLOCK_GROUP_DUP)) {
251 fprintf(stderr, "dup for data is allowed only in mixed mode\n");
255 if (allowed & metadata_profile) {
256 u64 meta_flags = BTRFS_BLOCK_GROUP_METADATA;
258 ret = create_one_raid_group(trans, root,
259 BTRFS_BLOCK_GROUP_SYSTEM |
260 (allowed & metadata_profile));
264 meta_flags |= BTRFS_BLOCK_GROUP_DATA;
266 ret = create_one_raid_group(trans, root, meta_flags |
267 (allowed & metadata_profile));
271 if (!mixed && num_devices > 1 && (allowed & data_profile)) {
272 ret = create_one_raid_group(trans, root,
273 BTRFS_BLOCK_GROUP_DATA |
274 (allowed & data_profile));
277 recow_roots(trans, root);
282 static int create_data_reloc_tree(struct btrfs_trans_handle *trans,
283 struct btrfs_root *root)
285 struct btrfs_key location;
286 struct btrfs_root_item root_item;
287 struct extent_buffer *tmp;
288 u64 objectid = BTRFS_DATA_RELOC_TREE_OBJECTID;
291 ret = btrfs_copy_root(trans, root, root->node, &tmp, objectid);
294 memcpy(&root_item, &root->root_item, sizeof(root_item));
295 btrfs_set_root_bytenr(&root_item, tmp->start);
296 btrfs_set_root_level(&root_item, btrfs_header_level(tmp));
297 btrfs_set_root_generation(&root_item, trans->transid);
298 free_extent_buffer(tmp);
300 location.objectid = objectid;
301 location.type = BTRFS_ROOT_ITEM_KEY;
303 ret = btrfs_insert_root(trans, root->fs_info->tree_root,
304 &location, &root_item);
309 static void print_usage(void)
311 fprintf(stderr, "usage: mkfs.btrfs [options] dev [ dev ... ]\n");
312 fprintf(stderr, "options:\n");
313 fprintf(stderr, "\t -A --alloc-start the offset to start the FS\n");
314 fprintf(stderr, "\t -b --byte-count total number of bytes in the FS\n");
315 fprintf(stderr, "\t -d --data data profile, raid0, raid1, raid5, raid6, raid10, dup or single\n");
316 fprintf(stderr, "\t -f --force force overwrite of existing filesystem\n");
317 fprintf(stderr, "\t -l --leafsize size of btree leaves\n");
318 fprintf(stderr, "\t -L --label set a label\n");
319 fprintf(stderr, "\t -m --metadata metadata profile, values like data profile\n");
320 fprintf(stderr, "\t -M --mixed mix metadata and data together\n");
321 fprintf(stderr, "\t -n --nodesize size of btree nodes\n");
322 fprintf(stderr, "\t -s --sectorsize min block allocation\n");
323 fprintf(stderr, "\t -r --rootdir the source directory\n");
324 fprintf(stderr, "\t -K --nodiscard do not perform whole device TRIM\n");
325 fprintf(stderr, "\t -V --version print the mkfs.btrfs version and exit\n");
326 fprintf(stderr, "%s\n", BTRFS_BUILD_VERSION);
330 static void print_version(void)
332 fprintf(stderr, "mkfs.btrfs, part of %s\n", BTRFS_BUILD_VERSION);
336 static u64 parse_profile(char *s)
338 if (strcmp(s, "raid0") == 0) {
339 return BTRFS_BLOCK_GROUP_RAID0;
340 } else if (strcmp(s, "raid1") == 0) {
341 return BTRFS_BLOCK_GROUP_RAID1;
342 } else if (strcmp(s, "raid5") == 0) {
343 return BTRFS_BLOCK_GROUP_RAID5;
344 } else if (strcmp(s, "raid6") == 0) {
345 return BTRFS_BLOCK_GROUP_RAID6;
346 } else if (strcmp(s, "raid10") == 0) {
347 return BTRFS_BLOCK_GROUP_RAID10;
348 } else if (strcmp(s, "dup") == 0) {
349 return BTRFS_BLOCK_GROUP_DUP;
350 } else if (strcmp(s, "single") == 0) {
353 fprintf(stderr, "Unknown profile %s\n", s);
360 static char *parse_label(char *input)
362 int len = strlen(input);
364 if (len >= BTRFS_LABEL_SIZE) {
365 fprintf(stderr, "Label %s is too long (max %d)\n", input,
366 BTRFS_LABEL_SIZE - 1);
369 return strdup(input);
372 static struct option long_options[] = {
373 { "alloc-start", 1, NULL, 'A'},
374 { "byte-count", 1, NULL, 'b' },
375 { "force", 0, NULL, 'f' },
376 { "leafsize", 1, NULL, 'l' },
377 { "label", 1, NULL, 'L'},
378 { "metadata", 1, NULL, 'm' },
379 { "mixed", 0, NULL, 'M' },
380 { "nodesize", 1, NULL, 'n' },
381 { "sectorsize", 1, NULL, 's' },
382 { "data", 1, NULL, 'd' },
383 { "version", 0, NULL, 'V' },
384 { "rootdir", 1, NULL, 'r' },
385 { "nodiscard", 0, NULL, 'K' },
389 static int add_directory_items(struct btrfs_trans_handle *trans,
390 struct btrfs_root *root, u64 objectid,
391 ino_t parent_inum, const char *name,
392 struct stat *st, int *dir_index_cnt)
396 struct btrfs_key location;
399 name_len = strlen(name);
401 location.objectid = objectid;
403 btrfs_set_key_type(&location, BTRFS_INODE_ITEM_KEY);
405 if (S_ISDIR(st->st_mode))
406 filetype = BTRFS_FT_DIR;
407 if (S_ISREG(st->st_mode))
408 filetype = BTRFS_FT_REG_FILE;
409 if (S_ISLNK(st->st_mode))
410 filetype = BTRFS_FT_SYMLINK;
412 ret = btrfs_insert_dir_item(trans, root, name, name_len,
413 parent_inum, &location,
414 filetype, index_cnt);
416 *dir_index_cnt = index_cnt;
422 static int fill_inode_item(struct btrfs_trans_handle *trans,
423 struct btrfs_root *root,
424 struct btrfs_inode_item *dst, struct stat *src)
427 u64 sectorsize = root->sectorsize;
430 * btrfs_inode_item has some reserved fields
431 * and represents on-disk inode entry, so
432 * zero everything to prevent information leak
434 memset(dst, 0, sizeof (*dst));
436 btrfs_set_stack_inode_generation(dst, trans->transid);
437 btrfs_set_stack_inode_size(dst, src->st_size);
438 btrfs_set_stack_inode_nbytes(dst, 0);
439 btrfs_set_stack_inode_block_group(dst, 0);
440 btrfs_set_stack_inode_nlink(dst, src->st_nlink);
441 btrfs_set_stack_inode_uid(dst, src->st_uid);
442 btrfs_set_stack_inode_gid(dst, src->st_gid);
443 btrfs_set_stack_inode_mode(dst, src->st_mode);
444 btrfs_set_stack_inode_rdev(dst, 0);
445 btrfs_set_stack_inode_flags(dst, 0);
446 btrfs_set_stack_timespec_sec(&dst->atime, src->st_atime);
447 btrfs_set_stack_timespec_nsec(&dst->atime, 0);
448 btrfs_set_stack_timespec_sec(&dst->ctime, src->st_ctime);
449 btrfs_set_stack_timespec_nsec(&dst->ctime, 0);
450 btrfs_set_stack_timespec_sec(&dst->mtime, src->st_mtime);
451 btrfs_set_stack_timespec_nsec(&dst->mtime, 0);
452 btrfs_set_stack_timespec_sec(&dst->otime, 0);
453 btrfs_set_stack_timespec_nsec(&dst->otime, 0);
455 if (S_ISDIR(src->st_mode)) {
456 btrfs_set_stack_inode_size(dst, 0);
457 btrfs_set_stack_inode_nlink(dst, 1);
459 if (S_ISREG(src->st_mode)) {
460 btrfs_set_stack_inode_size(dst, (u64)src->st_size);
461 if (src->st_size <= BTRFS_MAX_INLINE_DATA_SIZE(root))
462 btrfs_set_stack_inode_nbytes(dst, src->st_size);
464 blocks = src->st_size / sectorsize;
465 if (src->st_size % sectorsize)
467 blocks *= sectorsize;
468 btrfs_set_stack_inode_nbytes(dst, blocks);
471 if (S_ISLNK(src->st_mode))
472 btrfs_set_stack_inode_nbytes(dst, src->st_size + 1);
477 static int directory_select(const struct direct *entry)
479 if ((strncmp(entry->d_name, ".", entry->d_reclen) == 0) ||
480 (strncmp(entry->d_name, "..", entry->d_reclen) == 0))
486 static void free_namelist(struct direct **files, int count)
493 for (i = 0; i < count; ++i)
498 static u64 calculate_dir_inode_size(char *dirname)
501 struct direct **files, *cur_file;
502 u64 dir_inode_size = 0;
504 count = scandir(dirname, &files, directory_select, NULL);
506 for (i = 0; i < count; i++) {
508 dir_inode_size += strlen(cur_file->d_name);
511 free_namelist(files, count);
514 return dir_inode_size;
517 static int add_inode_items(struct btrfs_trans_handle *trans,
518 struct btrfs_root *root,
519 struct stat *st, char *name,
520 u64 self_objectid, ino_t parent_inum,
521 int dir_index_cnt, struct btrfs_inode_item *inode_ret)
524 struct btrfs_key inode_key;
525 struct btrfs_inode_item btrfs_inode;
530 name_len = strlen(name);
531 fill_inode_item(trans, root, &btrfs_inode, st);
532 objectid = self_objectid;
534 if (S_ISDIR(st->st_mode)) {
535 inode_size = calculate_dir_inode_size(name);
536 btrfs_set_stack_inode_size(&btrfs_inode, inode_size);
539 inode_key.objectid = objectid;
540 inode_key.offset = 0;
541 btrfs_set_key_type(&inode_key, BTRFS_INODE_ITEM_KEY);
543 ret = btrfs_insert_inode(trans, root, objectid, &btrfs_inode);
547 ret = btrfs_insert_inode_ref(trans, root, name, name_len,
548 objectid, parent_inum, dir_index_cnt);
552 *inode_ret = btrfs_inode;
557 static int add_xattr_item(struct btrfs_trans_handle *trans,
558 struct btrfs_root *root, u64 objectid,
559 const char *file_name)
563 char xattr_list[XATTR_LIST_MAX];
565 char cur_value[XATTR_SIZE_MAX];
566 char delimiter = '\0';
567 char *next_location = xattr_list;
569 ret = llistxattr(file_name, xattr_list, XATTR_LIST_MAX);
573 fprintf(stderr, "get a list of xattr failed for %s\n",
580 cur_name = strtok(xattr_list, &delimiter);
581 while (cur_name != NULL) {
582 cur_name_len = strlen(cur_name);
583 next_location += cur_name_len + 1;
585 ret = getxattr(file_name, cur_name, cur_value, XATTR_SIZE_MAX);
589 fprintf(stderr, "get a xattr value failed for %s attr %s\n",
590 file_name, cur_name);
594 ret = btrfs_insert_xattr_item(trans, root, cur_name,
595 cur_name_len, cur_value,
598 fprintf(stderr, "insert a xattr item failed for %s\n",
602 cur_name = strtok(next_location, &delimiter);
607 static int custom_alloc_extent(struct btrfs_root *root, u64 num_bytes,
608 u64 hint_byte, struct btrfs_key *ins)
612 u64 last = hint_byte;
615 struct btrfs_block_group_cache *cache;
618 ret = find_first_extent_bit(&root->fs_info->free_space_cache,
619 last, &start, &end, EXTENT_DIRTY);
621 if (wrapped++ == 0) {
629 start = max(last, start);
631 if (last - start < num_bytes)
634 last = start + num_bytes;
635 if (test_range_bit(&root->fs_info->pinned_extents,
636 start, last - 1, EXTENT_DIRTY, 0))
639 cache = btrfs_lookup_block_group(root->fs_info, start);
641 if (cache->flags & BTRFS_BLOCK_GROUP_SYSTEM ||
642 last > cache->key.objectid + cache->key.offset) {
643 last = cache->key.objectid + cache->key.offset;
647 if (cache->flags & (BTRFS_BLOCK_GROUP_SYSTEM |
648 BTRFS_BLOCK_GROUP_METADATA)) {
649 last = cache->key.objectid + cache->key.offset;
653 clear_extent_dirty(&root->fs_info->free_space_cache,
654 start, start + num_bytes - 1, 0);
656 ins->objectid = start;
657 ins->offset = num_bytes;
658 ins->type = BTRFS_EXTENT_ITEM_KEY;
662 fprintf(stderr, "not enough free space\n");
666 static int record_file_extent(struct btrfs_trans_handle *trans,
667 struct btrfs_root *root, u64 objectid,
668 struct btrfs_inode_item *inode,
669 u64 file_pos, u64 disk_bytenr,
673 struct btrfs_fs_info *info = root->fs_info;
674 struct btrfs_root *extent_root = info->extent_root;
675 struct extent_buffer *leaf;
676 struct btrfs_file_extent_item *fi;
677 struct btrfs_key ins_key;
678 struct btrfs_path path;
679 struct btrfs_extent_item *ei;
681 btrfs_init_path(&path);
683 ins_key.objectid = objectid;
685 btrfs_set_key_type(&ins_key, BTRFS_EXTENT_DATA_KEY);
686 ret = btrfs_insert_empty_item(trans, root, &path, &ins_key,
690 leaf = path.nodes[0];
691 fi = btrfs_item_ptr(leaf, path.slots[0],
692 struct btrfs_file_extent_item);
693 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
694 btrfs_set_file_extent_type(leaf, fi, BTRFS_FILE_EXTENT_REG);
695 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
696 btrfs_set_file_extent_disk_num_bytes(leaf, fi, num_bytes);
697 btrfs_set_file_extent_offset(leaf, fi, 0);
698 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
699 btrfs_set_file_extent_ram_bytes(leaf, fi, num_bytes);
700 btrfs_set_file_extent_compression(leaf, fi, 0);
701 btrfs_set_file_extent_encryption(leaf, fi, 0);
702 btrfs_set_file_extent_other_encoding(leaf, fi, 0);
703 btrfs_mark_buffer_dirty(leaf);
705 btrfs_release_path(root, &path);
707 ins_key.objectid = disk_bytenr;
708 ins_key.offset = num_bytes;
709 ins_key.type = BTRFS_EXTENT_ITEM_KEY;
711 ret = btrfs_insert_empty_item(trans, extent_root, &path,
712 &ins_key, sizeof(*ei));
714 leaf = path.nodes[0];
715 ei = btrfs_item_ptr(leaf, path.slots[0],
716 struct btrfs_extent_item);
718 btrfs_set_extent_refs(leaf, ei, 0);
719 btrfs_set_extent_generation(leaf, ei, trans->transid);
720 btrfs_set_extent_flags(leaf, ei, BTRFS_EXTENT_FLAG_DATA);
722 btrfs_mark_buffer_dirty(leaf);
723 ret = btrfs_update_block_group(trans, root, disk_bytenr,
727 } else if (ret != -EEXIST) {
731 ret = btrfs_inc_extent_ref(trans, root, disk_bytenr, num_bytes, 0,
732 root->root_key.objectid,
735 btrfs_release_path(root, &path);
739 static int add_symbolic_link(struct btrfs_trans_handle *trans,
740 struct btrfs_root *root,
741 u64 objectid, const char *path_name)
744 u64 sectorsize = root->sectorsize;
745 char *buf = malloc(sectorsize);
747 ret = readlink(path_name, buf, sectorsize);
749 fprintf(stderr, "readlink failed for %s\n", path_name);
752 if (ret >= sectorsize) {
753 fprintf(stderr, "symlink too long for %s", path_name);
758 buf[ret] = '\0'; /* readlink does not do it for us */
759 ret = btrfs_insert_inline_extent(trans, root, objectid, 0,
766 static int add_file_items(struct btrfs_trans_handle *trans,
767 struct btrfs_root *root,
768 struct btrfs_inode_item *btrfs_inode, u64 objectid,
769 ino_t parent_inum, struct stat *st,
770 const char *path_name, int out_fd)
776 struct btrfs_key key;
778 u32 sectorsize = root->sectorsize;
783 fd = open(path_name, O_RDONLY);
785 fprintf(stderr, "%s open failed\n", path_name);
789 blocks = st->st_size / sectorsize;
790 if (st->st_size % sectorsize)
793 if (st->st_size <= BTRFS_MAX_INLINE_DATA_SIZE(root)) {
794 buffer = malloc(st->st_size);
795 ret_read = pread64(fd, buffer, st->st_size, bytes_read);
796 if (ret_read == -1) {
797 fprintf(stderr, "%s read failed\n", path_name);
801 ret = btrfs_insert_inline_extent(trans, root, objectid, 0,
802 buffer, st->st_size);
806 ret = custom_alloc_extent(root, blocks * sectorsize, 0, &key);
810 first_block = key.objectid;
812 buffer = malloc(sectorsize);
815 memset(buffer, 0, sectorsize);
816 ret_read = pread64(fd, buffer, sectorsize, bytes_read);
817 if (ret_read == -1) {
818 fprintf(stderr, "%s read failed\n", path_name);
822 ret = pwrite64(out_fd, buffer, sectorsize,
823 first_block + bytes_read);
824 if (ret != sectorsize) {
825 fprintf(stderr, "output file write failed\n");
829 /* checksum for file data */
830 ret = btrfs_csum_file_block(trans, root->fs_info->csum_root,
831 first_block + (blocks * sectorsize),
832 first_block + bytes_read,
835 fprintf(stderr, "%s checksum failed\n", path_name);
839 bytes_read += ret_read;
841 } while (ret_read == sectorsize);
843 if (num_blocks > 0) {
844 ret = record_file_extent(trans, root, objectid, btrfs_inode,
845 first_block, first_block,
846 blocks * sectorsize);
858 static char *make_path(char *dir, char *name)
862 path = malloc(strlen(dir) + strlen(name) + 2);
866 if (dir[strlen(dir) - 1] != '/')
872 static int traverse_directory(struct btrfs_trans_handle *trans,
873 struct btrfs_root *root, char *dir_name,
874 struct directory_name_entry *dir_head, int out_fd)
878 struct btrfs_inode_item cur_inode;
879 struct btrfs_inode_item *inode_item;
880 int count, i, dir_index_cnt;
881 struct direct **files;
883 struct directory_name_entry *dir_entry, *parent_dir_entry;
884 struct direct *cur_file;
885 ino_t parent_inum, cur_inum;
886 ino_t highest_inum = 0;
887 char *parent_dir_name;
888 struct btrfs_path path;
889 struct extent_buffer *leaf;
890 struct btrfs_key root_dir_key;
891 u64 root_dir_inode_size = 0;
893 /* Add list for source directory */
894 dir_entry = malloc(sizeof(struct directory_name_entry));
895 dir_entry->dir_name = dir_name;
896 dir_entry->path = strdup(dir_name);
898 parent_inum = highest_inum + BTRFS_FIRST_FREE_OBJECTID;
899 dir_entry->inum = parent_inum;
900 list_add_tail(&dir_entry->list, &dir_head->list);
902 btrfs_init_path(&path);
904 root_dir_key.objectid = btrfs_root_dirid(&root->root_item);
905 root_dir_key.offset = 0;
906 btrfs_set_key_type(&root_dir_key, BTRFS_INODE_ITEM_KEY);
907 ret = btrfs_lookup_inode(trans, root, &path, &root_dir_key, 1);
909 fprintf(stderr, "root dir lookup error\n");
913 leaf = path.nodes[0];
914 inode_item = btrfs_item_ptr(leaf, path.slots[0],
915 struct btrfs_inode_item);
917 root_dir_inode_size = calculate_dir_inode_size(dir_name);
918 btrfs_set_inode_size(leaf, inode_item, root_dir_inode_size);
919 btrfs_mark_buffer_dirty(leaf);
921 btrfs_release_path(root, &path);
924 parent_dir_entry = list_entry(dir_head->list.next,
925 struct directory_name_entry,
927 list_del(&parent_dir_entry->list);
929 parent_inum = parent_dir_entry->inum;
930 parent_dir_name = parent_dir_entry->dir_name;
931 if (chdir(parent_dir_entry->path)) {
932 fprintf(stderr, "chdir error for %s\n",
937 count = scandir(parent_dir_entry->path, &files,
938 directory_select, NULL);
941 fprintf(stderr, "scandir for %s failed: %s\n",
942 parent_dir_name, strerror (errno));
946 for (i = 0; i < count; i++) {
949 if (lstat(cur_file->d_name, &st) == -1) {
950 fprintf(stderr, "lstat failed for file %s\n",
955 cur_inum = ++highest_inum + BTRFS_FIRST_FREE_OBJECTID;
956 ret = add_directory_items(trans, root,
957 cur_inum, parent_inum,
959 &st, &dir_index_cnt);
961 fprintf(stderr, "add_directory_items failed\n");
965 ret = add_inode_items(trans, root, &st,
966 cur_file->d_name, cur_inum,
967 parent_inum, dir_index_cnt,
970 fprintf(stderr, "add_inode_items failed\n");
974 ret = add_xattr_item(trans, root,
975 cur_inum, cur_file->d_name);
977 fprintf(stderr, "add_xattr_item failed\n");
982 if (S_ISDIR(st.st_mode)) {
983 dir_entry = malloc(sizeof(struct directory_name_entry));
984 dir_entry->dir_name = cur_file->d_name;
985 dir_entry->path = make_path(parent_dir_entry->path,
987 dir_entry->inum = cur_inum;
988 list_add_tail(&dir_entry->list, &dir_head->list);
989 } else if (S_ISREG(st.st_mode)) {
990 ret = add_file_items(trans, root, &cur_inode,
991 cur_inum, parent_inum, &st,
992 cur_file->d_name, out_fd);
994 fprintf(stderr, "add_file_items failed\n");
997 } else if (S_ISLNK(st.st_mode)) {
998 ret = add_symbolic_link(trans, root,
999 cur_inum, cur_file->d_name);
1001 fprintf(stderr, "add_symbolic_link failed\n");
1007 free_namelist(files, count);
1008 free(parent_dir_entry->path);
1009 free(parent_dir_entry);
1013 } while (!list_empty(&dir_head->list));
1017 free_namelist(files, count);
1019 free(parent_dir_entry->path);
1020 free(parent_dir_entry);
1024 static int open_target(char *output_name)
1027 output_fd = open(output_name, O_CREAT | O_RDWR | O_TRUNC,
1028 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH);
1033 static int create_chunks(struct btrfs_trans_handle *trans,
1034 struct btrfs_root *root, u64 num_of_meta_chunks,
1039 u64 meta_type = BTRFS_BLOCK_GROUP_METADATA;
1040 u64 data_type = BTRFS_BLOCK_GROUP_DATA;
1041 u64 minimum_data_chunk_size = 8 * 1024 * 1024;
1045 for (i = 0; i < num_of_meta_chunks; i++) {
1046 ret = btrfs_alloc_chunk(trans, root->fs_info->extent_root,
1047 &chunk_start, &chunk_size, meta_type);
1049 ret = btrfs_make_block_group(trans, root->fs_info->extent_root, 0,
1050 meta_type, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
1051 chunk_start, chunk_size);
1053 set_extent_dirty(&root->fs_info->free_space_cache,
1054 chunk_start, chunk_start + chunk_size - 1, 0);
1057 if (size_of_data < minimum_data_chunk_size)
1058 size_of_data = minimum_data_chunk_size;
1059 ret = btrfs_alloc_data_chunk(trans, root->fs_info->extent_root,
1060 &chunk_start, size_of_data, data_type);
1062 ret = btrfs_make_block_group(trans, root->fs_info->extent_root, 0,
1063 data_type, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
1064 chunk_start, size_of_data);
1066 set_extent_dirty(&root->fs_info->free_space_cache,
1067 chunk_start, chunk_start + size_of_data - 1, 0);
1071 static int make_image(char *source_dir, struct btrfs_root *root, int out_fd)
1074 struct btrfs_trans_handle *trans;
1076 struct stat root_st;
1078 struct directory_name_entry dir_head;
1080 ret = lstat(source_dir, &root_st);
1082 fprintf(stderr, "unable to lstat the %s\n", source_dir);
1086 INIT_LIST_HEAD(&dir_head.list);
1088 trans = btrfs_start_transaction(root, 1);
1089 ret = traverse_directory(trans, root, source_dir, &dir_head, out_fd);
1091 fprintf(stderr, "unable to traverse_directory\n");
1094 btrfs_commit_transaction(trans, root);
1096 printf("Making image is completed.\n");
1099 fprintf(stderr, "Making image is aborted.\n");
1104 * This ignores symlinks with unreadable targets and subdirs that can't
1105 * be read. It's a best-effort to give a rough estimate of the size of
1106 * a subdir. It doesn't guarantee that prepopulating btrfs from this
1107 * tree won't still run out of space.
1109 * The rounding up to 4096 is questionable. Previous code used du -B 4096.
1111 static u64 global_total_size;
1112 static int ftw_add_entry_size(const char *fpath, const struct stat *st,
1115 if (type == FTW_F || type == FTW_D)
1116 global_total_size += round_up(st->st_size, 4096);
1121 static u64 size_sourcedir(char *dir_name, u64 sectorsize,
1122 u64 *num_of_meta_chunks_ret, u64 *size_of_data_ret)
1127 u64 default_chunk_size = 8 * 1024 * 1024; /* 8MB */
1128 u64 allocated_meta_size = 8 * 1024 * 1024; /* 8MB */
1129 u64 allocated_total_size = 20 * 1024 * 1024; /* 20MB */
1130 u64 num_of_meta_chunks = 0;
1131 u64 num_of_allocated_meta_chunks =
1132 allocated_meta_size / default_chunk_size;
1134 global_total_size = 0;
1135 ret = ftw(dir_name, ftw_add_entry_size, 10);
1136 dir_size = global_total_size;
1138 fprintf(stderr, "ftw subdir walk of '%s' failed: %s\n",
1139 dir_name, strerror(errno));
1143 num_of_meta_chunks = (dir_size / 2) / default_chunk_size;
1144 if (((dir_size / 2) % default_chunk_size) != 0)
1145 num_of_meta_chunks++;
1146 if (num_of_meta_chunks <= num_of_allocated_meta_chunks)
1147 num_of_meta_chunks = 0;
1149 num_of_meta_chunks -= num_of_allocated_meta_chunks;
1151 total_size = allocated_total_size + dir_size +
1152 (num_of_meta_chunks * default_chunk_size);
1154 *num_of_meta_chunks_ret = num_of_meta_chunks;
1159 static int zero_output_file(int out_fd, u64 size, u32 sectorsize)
1161 int len = sectorsize;
1162 int loop_num = size / sectorsize;
1164 char *buf = malloc(len);
1170 memset(buf, 0, len);
1171 for (i = 0; i < loop_num; i++) {
1172 written = pwrite64(out_fd, buf, len, location);
1175 location += sectorsize;
1181 static int check_leaf_or_node_size(u32 size, u32 sectorsize)
1183 if (size < sectorsize) {
1185 "Illegal leafsize (or nodesize) %u (smaller than %u)\n",
1188 } else if (size > BTRFS_MAX_METADATA_BLOCKSIZE) {
1190 "Illegal leafsize (or nodesize) %u (larger than %u)\n",
1191 size, BTRFS_MAX_METADATA_BLOCKSIZE);
1193 } else if (size & (sectorsize - 1)) {
1195 "Illegal leafsize (or nodesize) %u (not align to %u)\n",
1202 static int is_ssd(const char *file)
1206 char sysfs_path[PATH_MAX];
1211 probe = blkid_new_probe_from_filename(file);
1215 /* Device number of this disk (possibly a partition) */
1216 devno = blkid_probe_get_devno(probe);
1220 /* Get whole disk name (not full path) for this devno */
1221 blkid_devno_to_wholedisk(devno, wholedisk, sizeof(wholedisk), NULL);
1223 snprintf(sysfs_path, PATH_MAX, "/sys/block/%s/queue/rotational",
1226 blkid_free_probe(probe);
1228 fd = open(sysfs_path, O_RDONLY);
1233 if (read(fd, &rotational, sizeof(char)) < sizeof(char)) {
1239 return !atoi((const char *)&rotational);
1242 int main(int ac, char **av)
1245 struct btrfs_root *root;
1246 struct btrfs_trans_handle *trans;
1249 u64 block_count = 0;
1250 u64 dev_block_count = 0;
1252 u64 alloc_start = 0;
1253 u64 metadata_profile = 0;
1254 u64 data_profile = 0;
1255 u32 leafsize = sysconf(_SC_PAGESIZE);
1256 u32 sectorsize = 4096;
1257 u32 nodesize = leafsize;
1258 u32 stripesize = 4096;
1260 int option_index = 0;
1265 int data_profile_opt = 0;
1266 int metadata_profile_opt = 0;
1269 int force_overwrite = 0;
1271 char *source_dir = NULL;
1272 int source_dir_set = 0;
1273 u64 num_of_meta_chunks = 0;
1274 u64 size_of_data = 0;
1275 u64 source_dir_size = 0;
1277 struct btrfs_super_block *super;
1285 c = getopt_long(ac, av, "A:b:fl:n:s:m:d:L:r:VMK",
1286 long_options, &option_index);
1291 alloc_start = parse_size(optarg);
1294 force_overwrite = 1;
1297 data_profile = parse_profile(optarg);
1298 data_profile_opt = 1;
1302 nodesize = parse_size(optarg);
1303 leafsize = parse_size(optarg);
1306 label = parse_label(optarg);
1309 metadata_profile = parse_profile(optarg);
1310 metadata_profile_opt = 1;
1316 sectorsize = parse_size(optarg);
1319 block_count = parse_size(optarg);
1320 if (block_count <= 1024*1024*1024) {
1321 printf("SMALL VOLUME: forcing mixed "
1322 "metadata/data groups\n");
1331 source_dir = optarg;
1341 sectorsize = max(sectorsize, (u32)sysconf(_SC_PAGESIZE));
1342 if (check_leaf_or_node_size(leafsize, sectorsize))
1344 if (check_leaf_or_node_size(nodesize, sectorsize))
1346 saved_optind = optind;
1347 dev_cnt = ac - optind;
1351 if (source_dir_set && dev_cnt > 1) {
1353 "The -r option is limited to a single device\n");
1356 while (dev_cnt-- > 0) {
1357 file = av[optind++];
1358 if (is_block_device(file))
1359 if (test_dev_for_mkfs(file, force_overwrite, estr)) {
1360 fprintf(stderr, "Error: %s", estr);
1365 /* if we are here that means all devs are good to btrfsify */
1366 optind = saved_optind;
1367 dev_cnt = ac - optind;
1369 printf("\nWARNING! - %s IS EXPERIMENTAL\n", BTRFS_BUILD_VERSION);
1370 printf("WARNING! - see http://btrfs.wiki.kernel.org before using\n\n");
1372 file = av[optind++];
1375 if (!source_dir_set) {
1377 * open without O_EXCL so that the problem should not
1378 * occur by the following processing.
1379 * (btrfs_register_one_device() fails if O_EXCL is on)
1381 fd = open(file, O_RDWR);
1383 fprintf(stderr, "unable to open %s: %s\n", file,
1388 ret = btrfs_prepare_device(fd, file, zero_end, &dev_block_count,
1389 block_count, &mixed, nodiscard);
1390 if (block_count && block_count > dev_block_count) {
1391 fprintf(stderr, "%s is smaller than requested size\n", file);
1395 fd = open_target(file);
1397 fprintf(stderr, "unable to open the %s\n", file);
1402 source_dir_size = size_sourcedir(source_dir, sectorsize,
1403 &num_of_meta_chunks, &size_of_data);
1404 if(block_count < source_dir_size)
1405 block_count = source_dir_size;
1406 ret = zero_output_file(fd, block_count, sectorsize);
1408 fprintf(stderr, "unable to zero the output file\n");
1411 /* our "device" is the new image file */
1412 dev_block_count = block_count;
1418 if (metadata_profile != data_profile) {
1419 fprintf(stderr, "With mixed block groups data and metadata "
1420 "profiles must be the same\n");
1425 blocks[0] = BTRFS_SUPER_INFO_OFFSET;
1426 for (i = 1; i < 7; i++) {
1427 blocks[i] = BTRFS_SUPER_INFO_OFFSET + 1024 * 1024 +
1431 ret = make_btrfs(fd, file, label, blocks, dev_block_count,
1433 sectorsize, stripesize);
1435 fprintf(stderr, "error during mkfs %d\n", ret);
1439 root = open_ctree(file, 0, O_RDWR);
1441 fprintf(stderr, "Open ctree failed\n");
1445 root->fs_info->alloc_start = alloc_start;
1447 ret = make_root_dir(root, mixed);
1449 fprintf(stderr, "failed to setup the root directory\n");
1453 trans = btrfs_start_transaction(root, 1);
1458 btrfs_register_one_device(file);
1461 while (dev_cnt-- > 0) {
1462 int old_mixed = mixed;
1464 file = av[optind++];
1467 * open without O_EXCL so that the problem should not
1468 * occur by the following processing.
1469 * (btrfs_register_one_device() fails if O_EXCL is on)
1471 fd = open(file, O_RDWR);
1473 fprintf(stderr, "unable to open %s: %s\n", file,
1477 ret = btrfs_device_already_in_root(root, fd,
1478 BTRFS_SUPER_INFO_OFFSET);
1480 fprintf(stderr, "skipping duplicate device %s in FS\n",
1485 ret = btrfs_prepare_device(fd, file, zero_end, &dev_block_count,
1486 block_count, &mixed, nodiscard);
1490 ret = btrfs_add_to_fsid(trans, root, fd, file, dev_block_count,
1491 sectorsize, sectorsize, sectorsize);
1493 btrfs_register_one_device(file);
1497 if (!source_dir_set) {
1498 ret = create_raid_groups(trans, root, data_profile,
1499 data_profile_opt, metadata_profile,
1500 metadata_profile_opt, mixed, ssd);
1504 ret = create_data_reloc_tree(trans, root);
1507 super = root->fs_info->super_copy;
1508 flags = btrfs_super_incompat_flags(super);
1511 flags |= BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS;
1513 btrfs_set_super_incompat_flags(super, flags);
1515 if ((data_profile | metadata_profile) &
1516 (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6)) {
1517 struct btrfs_super_block *super = root->fs_info->super_copy;
1518 u64 flags = btrfs_super_incompat_flags(super);
1520 flags |= BTRFS_FEATURE_INCOMPAT_RAID56;
1521 btrfs_set_super_incompat_flags(super, flags);
1522 printf("Setting RAID5/6 feature flag\n");
1525 printf("fs created label %s on %s\n\tnodesize %u leafsize %u "
1526 "sectorsize %u size %s\n",
1527 label, first_file, nodesize, leafsize, sectorsize,
1528 pretty_buf = pretty_sizes(btrfs_super_total_bytes(root->fs_info->super_copy)));
1531 printf("%s\n", BTRFS_BUILD_VERSION);
1532 btrfs_commit_transaction(trans, root);
1534 if (source_dir_set) {
1535 trans = btrfs_start_transaction(root, 1);
1536 ret = create_chunks(trans, root,
1537 num_of_meta_chunks, size_of_data);
1539 btrfs_commit_transaction(trans, root);
1541 ret = make_image(source_dir, root, fd);
1545 ret = close_ctree(root);