2 * Copyright (C) 2007 Oracle. All rights reserved.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
19 #define _XOPEN_SOURCE 500
22 #include "kerncompat.h"
24 #include <sys/ioctl.h>
25 #include <sys/mount.h>
29 #include <sys/types.h>
35 #include <uuid/uuid.h>
37 #include <attr/xattr.h>
38 #include <blkid/blkid.h>
43 #include "transaction.h"
47 static u64 index_cnt = 2;
49 struct directory_name_entry {
53 struct list_head list;
56 static int make_root_dir(struct btrfs_root *root, int mixed)
58 struct btrfs_trans_handle *trans;
59 struct btrfs_key location;
65 trans = btrfs_start_transaction(root, 1);
66 bytes_used = btrfs_super_bytes_used(root->fs_info->super_copy);
68 root->fs_info->system_allocs = 1;
69 ret = btrfs_make_block_group(trans, root, bytes_used,
70 BTRFS_BLOCK_GROUP_SYSTEM,
71 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
72 0, BTRFS_MKFS_SYSTEM_GROUP_SIZE);
76 ret = btrfs_alloc_chunk(trans, root->fs_info->extent_root,
77 &chunk_start, &chunk_size,
78 BTRFS_BLOCK_GROUP_METADATA |
79 BTRFS_BLOCK_GROUP_DATA);
81 ret = btrfs_make_block_group(trans, root, 0,
82 BTRFS_BLOCK_GROUP_METADATA |
83 BTRFS_BLOCK_GROUP_DATA,
84 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
85 chunk_start, chunk_size);
87 printf("Created a data/metadata chunk of size %llu\n", chunk_size);
89 ret = btrfs_alloc_chunk(trans, root->fs_info->extent_root,
90 &chunk_start, &chunk_size,
91 BTRFS_BLOCK_GROUP_METADATA);
93 ret = btrfs_make_block_group(trans, root, 0,
94 BTRFS_BLOCK_GROUP_METADATA,
95 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
96 chunk_start, chunk_size);
100 root->fs_info->system_allocs = 0;
101 btrfs_commit_transaction(trans, root);
102 trans = btrfs_start_transaction(root, 1);
106 ret = btrfs_alloc_chunk(trans, root->fs_info->extent_root,
107 &chunk_start, &chunk_size,
108 BTRFS_BLOCK_GROUP_DATA);
110 ret = btrfs_make_block_group(trans, root, 0,
111 BTRFS_BLOCK_GROUP_DATA,
112 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
113 chunk_start, chunk_size);
117 ret = btrfs_make_root_dir(trans, root->fs_info->tree_root,
118 BTRFS_ROOT_TREE_DIR_OBJECTID);
121 ret = btrfs_make_root_dir(trans, root, BTRFS_FIRST_FREE_OBJECTID);
124 memcpy(&location, &root->fs_info->fs_root->root_key, sizeof(location));
125 location.offset = (u64)-1;
126 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
128 btrfs_super_root_dir(root->fs_info->super_copy),
129 &location, BTRFS_FT_DIR, 0);
133 ret = btrfs_insert_inode_ref(trans, root->fs_info->tree_root,
134 "default", 7, location.objectid,
135 BTRFS_ROOT_TREE_DIR_OBJECTID, 0);
139 btrfs_commit_transaction(trans, root);
144 static void __recow_root(struct btrfs_trans_handle *trans,
145 struct btrfs_root *root)
148 struct extent_buffer *tmp;
150 if (trans->transid != btrfs_root_generation(&root->root_item)) {
151 ret = __btrfs_cow_block(trans, root, root->node,
152 NULL, 0, &tmp, 0, 0);
154 free_extent_buffer(tmp);
158 static void recow_roots(struct btrfs_trans_handle *trans,
159 struct btrfs_root *root)
161 struct btrfs_fs_info *info = root->fs_info;
163 __recow_root(trans, info->fs_root);
164 __recow_root(trans, info->tree_root);
165 __recow_root(trans, info->extent_root);
166 __recow_root(trans, info->chunk_root);
167 __recow_root(trans, info->dev_root);
168 __recow_root(trans, info->csum_root);
171 static int create_one_raid_group(struct btrfs_trans_handle *trans,
172 struct btrfs_root *root, u64 type)
178 ret = btrfs_alloc_chunk(trans, root->fs_info->extent_root,
179 &chunk_start, &chunk_size, type);
181 ret = btrfs_make_block_group(trans, root->fs_info->extent_root, 0,
182 type, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
183 chunk_start, chunk_size);
188 static int create_raid_groups(struct btrfs_trans_handle *trans,
189 struct btrfs_root *root, u64 data_profile,
190 int data_profile_opt, u64 metadata_profile,
191 int metadata_profile_opt, int mixed, int ssd)
193 u64 num_devices = btrfs_super_num_devices(root->fs_info->super_copy);
195 u64 devices_for_raid = num_devices;
199 * Set default profiles according to number of added devices.
200 * For mixed groups defaults are single/single.
202 if (!metadata_profile_opt && !mixed) {
203 if (num_devices == 1 && ssd)
204 printf("Detected a SSD, turning off metadata "
205 "duplication. Mkfs with -m dup if you want to "
206 "force metadata duplication.\n");
207 metadata_profile = (num_devices > 1) ?
208 BTRFS_BLOCK_GROUP_RAID1 : (ssd) ? 0: BTRFS_BLOCK_GROUP_DUP;
210 if (!data_profile_opt && !mixed) {
211 data_profile = (num_devices > 1) ?
212 BTRFS_BLOCK_GROUP_RAID0 : 0; /* raid0 or single */
215 if (devices_for_raid > 4)
216 devices_for_raid = 4;
218 switch (devices_for_raid) {
221 allowed |= BTRFS_BLOCK_GROUP_RAID10;
223 allowed |= BTRFS_BLOCK_GROUP_RAID6;
225 allowed |= BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1 |
226 BTRFS_BLOCK_GROUP_RAID5;
229 allowed |= BTRFS_BLOCK_GROUP_DUP;
232 if (metadata_profile & ~allowed) {
233 fprintf(stderr, "unable to create FS with metadata "
234 "profile %llu (have %llu devices)\n", metadata_profile,
238 if (data_profile & ~allowed) {
239 fprintf(stderr, "unable to create FS with data "
240 "profile %llu (have %llu devices)\n", data_profile,
245 /* allow dup'ed data chunks only in mixed mode */
246 if (!mixed && (data_profile & BTRFS_BLOCK_GROUP_DUP)) {
247 fprintf(stderr, "dup for data is allowed only in mixed mode\n");
251 if (allowed & metadata_profile) {
252 u64 meta_flags = BTRFS_BLOCK_GROUP_METADATA;
254 ret = create_one_raid_group(trans, root,
255 BTRFS_BLOCK_GROUP_SYSTEM |
256 (allowed & metadata_profile));
260 meta_flags |= BTRFS_BLOCK_GROUP_DATA;
262 ret = create_one_raid_group(trans, root, meta_flags |
263 (allowed & metadata_profile));
267 if (!mixed && num_devices > 1 && (allowed & data_profile)) {
268 ret = create_one_raid_group(trans, root,
269 BTRFS_BLOCK_GROUP_DATA |
270 (allowed & data_profile));
273 recow_roots(trans, root);
278 static int create_data_reloc_tree(struct btrfs_trans_handle *trans,
279 struct btrfs_root *root)
281 struct btrfs_key location;
282 struct btrfs_root_item root_item;
283 struct extent_buffer *tmp;
284 u64 objectid = BTRFS_DATA_RELOC_TREE_OBJECTID;
287 ret = btrfs_copy_root(trans, root, root->node, &tmp, objectid);
290 memcpy(&root_item, &root->root_item, sizeof(root_item));
291 btrfs_set_root_bytenr(&root_item, tmp->start);
292 btrfs_set_root_level(&root_item, btrfs_header_level(tmp));
293 btrfs_set_root_generation(&root_item, trans->transid);
294 free_extent_buffer(tmp);
296 location.objectid = objectid;
297 location.type = BTRFS_ROOT_ITEM_KEY;
299 ret = btrfs_insert_root(trans, root->fs_info->tree_root,
300 &location, &root_item);
305 static void print_usage(void)
307 fprintf(stderr, "usage: mkfs.btrfs [options] dev [ dev ... ]\n");
308 fprintf(stderr, "options:\n");
309 fprintf(stderr, "\t -A --alloc-start the offset to start the FS\n");
310 fprintf(stderr, "\t -b --byte-count total number of bytes in the FS\n");
311 fprintf(stderr, "\t -d --data data profile, raid0, raid1, raid5, raid6, raid10, dup or single\n");
312 fprintf(stderr, "\t -f --force force overwrite of existing filesystem\n");
313 fprintf(stderr, "\t -l --leafsize size of btree leaves\n");
314 fprintf(stderr, "\t -L --label set a label\n");
315 fprintf(stderr, "\t -m --metadata metadata profile, values like data profile\n");
316 fprintf(stderr, "\t -M --mixed mix metadata and data together\n");
317 fprintf(stderr, "\t -n --nodesize size of btree nodes\n");
318 fprintf(stderr, "\t -s --sectorsize min block allocation (may not mountable by current kernel)\n");
319 fprintf(stderr, "\t -r --rootdir the source directory\n");
320 fprintf(stderr, "\t -K --nodiscard do not perform whole device TRIM\n");
321 fprintf(stderr, "\t -O --features comma separated list of filesystem features\n");
322 fprintf(stderr, "\t -V --version print the mkfs.btrfs version and exit\n");
323 fprintf(stderr, "%s\n", BTRFS_BUILD_VERSION);
327 static void print_version(void)
329 fprintf(stderr, "mkfs.btrfs, part of %s\n", BTRFS_BUILD_VERSION);
333 static u64 parse_profile(char *s)
335 if (strcmp(s, "raid0") == 0) {
336 return BTRFS_BLOCK_GROUP_RAID0;
337 } else if (strcmp(s, "raid1") == 0) {
338 return BTRFS_BLOCK_GROUP_RAID1;
339 } else if (strcmp(s, "raid5") == 0) {
340 return BTRFS_BLOCK_GROUP_RAID5;
341 } else if (strcmp(s, "raid6") == 0) {
342 return BTRFS_BLOCK_GROUP_RAID6;
343 } else if (strcmp(s, "raid10") == 0) {
344 return BTRFS_BLOCK_GROUP_RAID10;
345 } else if (strcmp(s, "dup") == 0) {
346 return BTRFS_BLOCK_GROUP_DUP;
347 } else if (strcmp(s, "single") == 0) {
350 fprintf(stderr, "Unknown profile %s\n", s);
357 static char *parse_label(char *input)
359 int len = strlen(input);
361 if (len >= BTRFS_LABEL_SIZE) {
362 fprintf(stderr, "Label %s is too long (max %d)\n", input,
363 BTRFS_LABEL_SIZE - 1);
366 return strdup(input);
369 static struct option long_options[] = {
370 { "alloc-start", 1, NULL, 'A'},
371 { "byte-count", 1, NULL, 'b' },
372 { "force", 0, NULL, 'f' },
373 { "leafsize", 1, NULL, 'l' },
374 { "label", 1, NULL, 'L'},
375 { "metadata", 1, NULL, 'm' },
376 { "mixed", 0, NULL, 'M' },
377 { "nodesize", 1, NULL, 'n' },
378 { "sectorsize", 1, NULL, 's' },
379 { "data", 1, NULL, 'd' },
380 { "version", 0, NULL, 'V' },
381 { "rootdir", 1, NULL, 'r' },
382 { "nodiscard", 0, NULL, 'K' },
383 { "features", 0, NULL, 'O' },
387 static int add_directory_items(struct btrfs_trans_handle *trans,
388 struct btrfs_root *root, u64 objectid,
389 ino_t parent_inum, const char *name,
390 struct stat *st, int *dir_index_cnt)
394 struct btrfs_key location;
397 name_len = strlen(name);
399 location.objectid = objectid;
401 btrfs_set_key_type(&location, BTRFS_INODE_ITEM_KEY);
403 if (S_ISDIR(st->st_mode))
404 filetype = BTRFS_FT_DIR;
405 if (S_ISREG(st->st_mode))
406 filetype = BTRFS_FT_REG_FILE;
407 if (S_ISLNK(st->st_mode))
408 filetype = BTRFS_FT_SYMLINK;
410 ret = btrfs_insert_dir_item(trans, root, name, name_len,
411 parent_inum, &location,
412 filetype, index_cnt);
414 *dir_index_cnt = index_cnt;
420 static int fill_inode_item(struct btrfs_trans_handle *trans,
421 struct btrfs_root *root,
422 struct btrfs_inode_item *dst, struct stat *src)
425 u64 sectorsize = root->sectorsize;
428 * btrfs_inode_item has some reserved fields
429 * and represents on-disk inode entry, so
430 * zero everything to prevent information leak
432 memset(dst, 0, sizeof (*dst));
434 btrfs_set_stack_inode_generation(dst, trans->transid);
435 btrfs_set_stack_inode_size(dst, src->st_size);
436 btrfs_set_stack_inode_nbytes(dst, 0);
437 btrfs_set_stack_inode_block_group(dst, 0);
438 btrfs_set_stack_inode_nlink(dst, src->st_nlink);
439 btrfs_set_stack_inode_uid(dst, src->st_uid);
440 btrfs_set_stack_inode_gid(dst, src->st_gid);
441 btrfs_set_stack_inode_mode(dst, src->st_mode);
442 btrfs_set_stack_inode_rdev(dst, 0);
443 btrfs_set_stack_inode_flags(dst, 0);
444 btrfs_set_stack_timespec_sec(&dst->atime, src->st_atime);
445 btrfs_set_stack_timespec_nsec(&dst->atime, 0);
446 btrfs_set_stack_timespec_sec(&dst->ctime, src->st_ctime);
447 btrfs_set_stack_timespec_nsec(&dst->ctime, 0);
448 btrfs_set_stack_timespec_sec(&dst->mtime, src->st_mtime);
449 btrfs_set_stack_timespec_nsec(&dst->mtime, 0);
450 btrfs_set_stack_timespec_sec(&dst->otime, 0);
451 btrfs_set_stack_timespec_nsec(&dst->otime, 0);
453 if (S_ISDIR(src->st_mode)) {
454 btrfs_set_stack_inode_size(dst, 0);
455 btrfs_set_stack_inode_nlink(dst, 1);
457 if (S_ISREG(src->st_mode)) {
458 btrfs_set_stack_inode_size(dst, (u64)src->st_size);
459 if (src->st_size <= BTRFS_MAX_INLINE_DATA_SIZE(root))
460 btrfs_set_stack_inode_nbytes(dst, src->st_size);
462 blocks = src->st_size / sectorsize;
463 if (src->st_size % sectorsize)
465 blocks *= sectorsize;
466 btrfs_set_stack_inode_nbytes(dst, blocks);
469 if (S_ISLNK(src->st_mode))
470 btrfs_set_stack_inode_nbytes(dst, src->st_size + 1);
475 static int directory_select(const struct direct *entry)
477 if ((strncmp(entry->d_name, ".", entry->d_reclen) == 0) ||
478 (strncmp(entry->d_name, "..", entry->d_reclen) == 0))
484 static void free_namelist(struct direct **files, int count)
491 for (i = 0; i < count; ++i)
496 static u64 calculate_dir_inode_size(char *dirname)
499 struct direct **files, *cur_file;
500 u64 dir_inode_size = 0;
502 count = scandir(dirname, &files, directory_select, NULL);
504 for (i = 0; i < count; i++) {
506 dir_inode_size += strlen(cur_file->d_name);
509 free_namelist(files, count);
512 return dir_inode_size;
515 static int add_inode_items(struct btrfs_trans_handle *trans,
516 struct btrfs_root *root,
517 struct stat *st, char *name,
518 u64 self_objectid, ino_t parent_inum,
519 int dir_index_cnt, struct btrfs_inode_item *inode_ret)
522 struct btrfs_key inode_key;
523 struct btrfs_inode_item btrfs_inode;
528 name_len = strlen(name);
529 fill_inode_item(trans, root, &btrfs_inode, st);
530 objectid = self_objectid;
532 if (S_ISDIR(st->st_mode)) {
533 inode_size = calculate_dir_inode_size(name);
534 btrfs_set_stack_inode_size(&btrfs_inode, inode_size);
537 inode_key.objectid = objectid;
538 inode_key.offset = 0;
539 btrfs_set_key_type(&inode_key, BTRFS_INODE_ITEM_KEY);
541 ret = btrfs_insert_inode(trans, root, objectid, &btrfs_inode);
545 ret = btrfs_insert_inode_ref(trans, root, name, name_len,
546 objectid, parent_inum, dir_index_cnt);
550 *inode_ret = btrfs_inode;
555 static int add_xattr_item(struct btrfs_trans_handle *trans,
556 struct btrfs_root *root, u64 objectid,
557 const char *file_name)
561 char xattr_list[XATTR_LIST_MAX];
563 char cur_value[XATTR_SIZE_MAX];
564 char delimiter = '\0';
565 char *next_location = xattr_list;
567 ret = llistxattr(file_name, xattr_list, XATTR_LIST_MAX);
571 fprintf(stderr, "get a list of xattr failed for %s\n",
578 cur_name = strtok(xattr_list, &delimiter);
579 while (cur_name != NULL) {
580 cur_name_len = strlen(cur_name);
581 next_location += cur_name_len + 1;
583 ret = getxattr(file_name, cur_name, cur_value, XATTR_SIZE_MAX);
587 fprintf(stderr, "get a xattr value failed for %s attr %s\n",
588 file_name, cur_name);
592 ret = btrfs_insert_xattr_item(trans, root, cur_name,
593 cur_name_len, cur_value,
596 fprintf(stderr, "insert a xattr item failed for %s\n",
600 cur_name = strtok(next_location, &delimiter);
605 static int custom_alloc_extent(struct btrfs_root *root, u64 num_bytes,
606 u64 hint_byte, struct btrfs_key *ins)
610 u64 last = hint_byte;
613 struct btrfs_block_group_cache *cache;
616 ret = find_first_extent_bit(&root->fs_info->free_space_cache,
617 last, &start, &end, EXTENT_DIRTY);
619 if (wrapped++ == 0) {
627 start = max(last, start);
629 if (last - start < num_bytes)
632 last = start + num_bytes;
633 if (test_range_bit(&root->fs_info->pinned_extents,
634 start, last - 1, EXTENT_DIRTY, 0))
637 cache = btrfs_lookup_block_group(root->fs_info, start);
639 if (cache->flags & BTRFS_BLOCK_GROUP_SYSTEM ||
640 last > cache->key.objectid + cache->key.offset) {
641 last = cache->key.objectid + cache->key.offset;
645 if (cache->flags & (BTRFS_BLOCK_GROUP_SYSTEM |
646 BTRFS_BLOCK_GROUP_METADATA)) {
647 last = cache->key.objectid + cache->key.offset;
651 clear_extent_dirty(&root->fs_info->free_space_cache,
652 start, start + num_bytes - 1, 0);
654 ins->objectid = start;
655 ins->offset = num_bytes;
656 ins->type = BTRFS_EXTENT_ITEM_KEY;
660 fprintf(stderr, "not enough free space\n");
664 static int record_file_extent(struct btrfs_trans_handle *trans,
665 struct btrfs_root *root, u64 objectid,
666 struct btrfs_inode_item *inode,
667 u64 file_pos, u64 disk_bytenr,
671 struct btrfs_fs_info *info = root->fs_info;
672 struct btrfs_root *extent_root = info->extent_root;
673 struct extent_buffer *leaf;
674 struct btrfs_file_extent_item *fi;
675 struct btrfs_key ins_key;
676 struct btrfs_path path;
677 struct btrfs_extent_item *ei;
679 btrfs_init_path(&path);
681 ins_key.objectid = objectid;
683 btrfs_set_key_type(&ins_key, BTRFS_EXTENT_DATA_KEY);
684 ret = btrfs_insert_empty_item(trans, root, &path, &ins_key,
688 leaf = path.nodes[0];
689 fi = btrfs_item_ptr(leaf, path.slots[0],
690 struct btrfs_file_extent_item);
691 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
692 btrfs_set_file_extent_type(leaf, fi, BTRFS_FILE_EXTENT_REG);
693 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
694 btrfs_set_file_extent_disk_num_bytes(leaf, fi, num_bytes);
695 btrfs_set_file_extent_offset(leaf, fi, 0);
696 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
697 btrfs_set_file_extent_ram_bytes(leaf, fi, num_bytes);
698 btrfs_set_file_extent_compression(leaf, fi, 0);
699 btrfs_set_file_extent_encryption(leaf, fi, 0);
700 btrfs_set_file_extent_other_encoding(leaf, fi, 0);
701 btrfs_mark_buffer_dirty(leaf);
703 btrfs_release_path(&path);
705 ins_key.objectid = disk_bytenr;
706 ins_key.offset = num_bytes;
707 ins_key.type = BTRFS_EXTENT_ITEM_KEY;
709 ret = btrfs_insert_empty_item(trans, extent_root, &path,
710 &ins_key, sizeof(*ei));
712 leaf = path.nodes[0];
713 ei = btrfs_item_ptr(leaf, path.slots[0],
714 struct btrfs_extent_item);
716 btrfs_set_extent_refs(leaf, ei, 0);
717 btrfs_set_extent_generation(leaf, ei, trans->transid);
718 btrfs_set_extent_flags(leaf, ei, BTRFS_EXTENT_FLAG_DATA);
720 btrfs_mark_buffer_dirty(leaf);
721 ret = btrfs_update_block_group(trans, root, disk_bytenr,
725 } else if (ret != -EEXIST) {
729 ret = btrfs_inc_extent_ref(trans, root, disk_bytenr, num_bytes, 0,
730 root->root_key.objectid,
733 btrfs_release_path(&path);
737 static int add_symbolic_link(struct btrfs_trans_handle *trans,
738 struct btrfs_root *root,
739 u64 objectid, const char *path_name)
742 u64 sectorsize = root->sectorsize;
743 char *buf = malloc(sectorsize);
745 ret = readlink(path_name, buf, sectorsize);
747 fprintf(stderr, "readlink failed for %s\n", path_name);
750 if (ret >= sectorsize) {
751 fprintf(stderr, "symlink too long for %s", path_name);
756 buf[ret] = '\0'; /* readlink does not do it for us */
757 ret = btrfs_insert_inline_extent(trans, root, objectid, 0,
764 static int add_file_items(struct btrfs_trans_handle *trans,
765 struct btrfs_root *root,
766 struct btrfs_inode_item *btrfs_inode, u64 objectid,
767 ino_t parent_inum, struct stat *st,
768 const char *path_name, int out_fd)
774 struct btrfs_key key;
776 u32 sectorsize = root->sectorsize;
781 fd = open(path_name, O_RDONLY);
783 fprintf(stderr, "%s open failed\n", path_name);
787 blocks = st->st_size / sectorsize;
788 if (st->st_size % sectorsize)
791 if (st->st_size <= BTRFS_MAX_INLINE_DATA_SIZE(root)) {
792 buffer = malloc(st->st_size);
793 ret_read = pread64(fd, buffer, st->st_size, bytes_read);
794 if (ret_read == -1) {
795 fprintf(stderr, "%s read failed\n", path_name);
799 ret = btrfs_insert_inline_extent(trans, root, objectid, 0,
800 buffer, st->st_size);
804 ret = custom_alloc_extent(root, blocks * sectorsize, 0, &key);
808 first_block = key.objectid;
810 buffer = malloc(sectorsize);
813 memset(buffer, 0, sectorsize);
814 ret_read = pread64(fd, buffer, sectorsize, bytes_read);
815 if (ret_read == -1) {
816 fprintf(stderr, "%s read failed\n", path_name);
820 ret = pwrite64(out_fd, buffer, sectorsize,
821 first_block + bytes_read);
822 if (ret != sectorsize) {
823 fprintf(stderr, "output file write failed\n");
827 /* checksum for file data */
828 ret = btrfs_csum_file_block(trans, root->fs_info->csum_root,
829 first_block + (blocks * sectorsize),
830 first_block + bytes_read,
833 fprintf(stderr, "%s checksum failed\n", path_name);
837 bytes_read += ret_read;
839 } while (ret_read == sectorsize);
841 if (num_blocks > 0) {
842 ret = record_file_extent(trans, root, objectid, btrfs_inode,
843 first_block, first_block,
844 blocks * sectorsize);
856 static char *make_path(char *dir, char *name)
860 path = malloc(strlen(dir) + strlen(name) + 2);
864 if (dir[strlen(dir) - 1] != '/')
870 static int traverse_directory(struct btrfs_trans_handle *trans,
871 struct btrfs_root *root, char *dir_name,
872 struct directory_name_entry *dir_head, int out_fd)
876 struct btrfs_inode_item cur_inode;
877 struct btrfs_inode_item *inode_item;
878 int count, i, dir_index_cnt;
879 struct direct **files;
881 struct directory_name_entry *dir_entry, *parent_dir_entry;
882 struct direct *cur_file;
883 ino_t parent_inum, cur_inum;
884 ino_t highest_inum = 0;
885 char *parent_dir_name;
886 struct btrfs_path path;
887 struct extent_buffer *leaf;
888 struct btrfs_key root_dir_key;
889 u64 root_dir_inode_size = 0;
891 /* Add list for source directory */
892 dir_entry = malloc(sizeof(struct directory_name_entry));
893 dir_entry->dir_name = dir_name;
894 dir_entry->path = strdup(dir_name);
896 parent_inum = highest_inum + BTRFS_FIRST_FREE_OBJECTID;
897 dir_entry->inum = parent_inum;
898 list_add_tail(&dir_entry->list, &dir_head->list);
900 btrfs_init_path(&path);
902 root_dir_key.objectid = btrfs_root_dirid(&root->root_item);
903 root_dir_key.offset = 0;
904 btrfs_set_key_type(&root_dir_key, BTRFS_INODE_ITEM_KEY);
905 ret = btrfs_lookup_inode(trans, root, &path, &root_dir_key, 1);
907 fprintf(stderr, "root dir lookup error\n");
911 leaf = path.nodes[0];
912 inode_item = btrfs_item_ptr(leaf, path.slots[0],
913 struct btrfs_inode_item);
915 root_dir_inode_size = calculate_dir_inode_size(dir_name);
916 btrfs_set_inode_size(leaf, inode_item, root_dir_inode_size);
917 btrfs_mark_buffer_dirty(leaf);
919 btrfs_release_path(&path);
922 parent_dir_entry = list_entry(dir_head->list.next,
923 struct directory_name_entry,
925 list_del(&parent_dir_entry->list);
927 parent_inum = parent_dir_entry->inum;
928 parent_dir_name = parent_dir_entry->dir_name;
929 if (chdir(parent_dir_entry->path)) {
930 fprintf(stderr, "chdir error for %s\n",
935 count = scandir(parent_dir_entry->path, &files,
936 directory_select, NULL);
939 fprintf(stderr, "scandir for %s failed: %s\n",
940 parent_dir_name, strerror (errno));
944 for (i = 0; i < count; i++) {
947 if (lstat(cur_file->d_name, &st) == -1) {
948 fprintf(stderr, "lstat failed for file %s\n",
953 cur_inum = ++highest_inum + BTRFS_FIRST_FREE_OBJECTID;
954 ret = add_directory_items(trans, root,
955 cur_inum, parent_inum,
957 &st, &dir_index_cnt);
959 fprintf(stderr, "add_directory_items failed\n");
963 ret = add_inode_items(trans, root, &st,
964 cur_file->d_name, cur_inum,
965 parent_inum, dir_index_cnt,
968 fprintf(stderr, "add_inode_items failed\n");
972 ret = add_xattr_item(trans, root,
973 cur_inum, cur_file->d_name);
975 fprintf(stderr, "add_xattr_item failed\n");
980 if (S_ISDIR(st.st_mode)) {
981 dir_entry = malloc(sizeof(struct directory_name_entry));
982 dir_entry->dir_name = cur_file->d_name;
983 dir_entry->path = make_path(parent_dir_entry->path,
985 dir_entry->inum = cur_inum;
986 list_add_tail(&dir_entry->list, &dir_head->list);
987 } else if (S_ISREG(st.st_mode)) {
988 ret = add_file_items(trans, root, &cur_inode,
989 cur_inum, parent_inum, &st,
990 cur_file->d_name, out_fd);
992 fprintf(stderr, "add_file_items failed\n");
995 } else if (S_ISLNK(st.st_mode)) {
996 ret = add_symbolic_link(trans, root,
997 cur_inum, cur_file->d_name);
999 fprintf(stderr, "add_symbolic_link failed\n");
1005 free_namelist(files, count);
1006 free(parent_dir_entry->path);
1007 free(parent_dir_entry);
1011 } while (!list_empty(&dir_head->list));
1015 free_namelist(files, count);
1017 free(parent_dir_entry->path);
1018 free(parent_dir_entry);
1022 static int open_target(char *output_name)
1025 output_fd = open(output_name, O_CREAT | O_RDWR | O_TRUNC,
1026 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH);
1031 static int create_chunks(struct btrfs_trans_handle *trans,
1032 struct btrfs_root *root, u64 num_of_meta_chunks,
1037 u64 meta_type = BTRFS_BLOCK_GROUP_METADATA;
1038 u64 data_type = BTRFS_BLOCK_GROUP_DATA;
1039 u64 minimum_data_chunk_size = 8 * 1024 * 1024;
1043 for (i = 0; i < num_of_meta_chunks; i++) {
1044 ret = btrfs_alloc_chunk(trans, root->fs_info->extent_root,
1045 &chunk_start, &chunk_size, meta_type);
1047 ret = btrfs_make_block_group(trans, root->fs_info->extent_root, 0,
1048 meta_type, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
1049 chunk_start, chunk_size);
1051 set_extent_dirty(&root->fs_info->free_space_cache,
1052 chunk_start, chunk_start + chunk_size - 1, 0);
1055 if (size_of_data < minimum_data_chunk_size)
1056 size_of_data = minimum_data_chunk_size;
1057 ret = btrfs_alloc_data_chunk(trans, root->fs_info->extent_root,
1058 &chunk_start, size_of_data, data_type);
1060 ret = btrfs_make_block_group(trans, root->fs_info->extent_root, 0,
1061 data_type, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
1062 chunk_start, size_of_data);
1064 set_extent_dirty(&root->fs_info->free_space_cache,
1065 chunk_start, chunk_start + size_of_data - 1, 0);
1069 static int make_image(char *source_dir, struct btrfs_root *root, int out_fd)
1072 struct btrfs_trans_handle *trans;
1074 struct stat root_st;
1076 struct directory_name_entry dir_head;
1078 ret = lstat(source_dir, &root_st);
1080 fprintf(stderr, "unable to lstat the %s\n", source_dir);
1084 INIT_LIST_HEAD(&dir_head.list);
1086 trans = btrfs_start_transaction(root, 1);
1087 ret = traverse_directory(trans, root, source_dir, &dir_head, out_fd);
1089 fprintf(stderr, "unable to traverse_directory\n");
1092 btrfs_commit_transaction(trans, root);
1094 printf("Making image is completed.\n");
1097 fprintf(stderr, "Making image is aborted.\n");
1102 * This ignores symlinks with unreadable targets and subdirs that can't
1103 * be read. It's a best-effort to give a rough estimate of the size of
1104 * a subdir. It doesn't guarantee that prepopulating btrfs from this
1105 * tree won't still run out of space.
1107 * The rounding up to 4096 is questionable. Previous code used du -B 4096.
1109 static u64 global_total_size;
1110 static int ftw_add_entry_size(const char *fpath, const struct stat *st,
1113 if (type == FTW_F || type == FTW_D)
1114 global_total_size += round_up(st->st_size, 4096);
1119 static u64 size_sourcedir(char *dir_name, u64 sectorsize,
1120 u64 *num_of_meta_chunks_ret, u64 *size_of_data_ret)
1125 u64 default_chunk_size = 8 * 1024 * 1024; /* 8MB */
1126 u64 allocated_meta_size = 8 * 1024 * 1024; /* 8MB */
1127 u64 allocated_total_size = 20 * 1024 * 1024; /* 20MB */
1128 u64 num_of_meta_chunks = 0;
1129 u64 num_of_allocated_meta_chunks =
1130 allocated_meta_size / default_chunk_size;
1132 global_total_size = 0;
1133 ret = ftw(dir_name, ftw_add_entry_size, 10);
1134 dir_size = global_total_size;
1136 fprintf(stderr, "ftw subdir walk of '%s' failed: %s\n",
1137 dir_name, strerror(errno));
1141 num_of_meta_chunks = (dir_size / 2) / default_chunk_size;
1142 if (((dir_size / 2) % default_chunk_size) != 0)
1143 num_of_meta_chunks++;
1144 if (num_of_meta_chunks <= num_of_allocated_meta_chunks)
1145 num_of_meta_chunks = 0;
1147 num_of_meta_chunks -= num_of_allocated_meta_chunks;
1149 total_size = allocated_total_size + dir_size +
1150 (num_of_meta_chunks * default_chunk_size);
1152 *num_of_meta_chunks_ret = num_of_meta_chunks;
1157 static int zero_output_file(int out_fd, u64 size, u32 sectorsize)
1159 int len = sectorsize;
1160 int loop_num = size / sectorsize;
1162 char *buf = malloc(len);
1168 memset(buf, 0, len);
1169 for (i = 0; i < loop_num; i++) {
1170 written = pwrite64(out_fd, buf, len, location);
1173 location += sectorsize;
1179 static int check_leaf_or_node_size(u32 size, u32 sectorsize)
1181 if (size < sectorsize) {
1183 "Illegal leafsize (or nodesize) %u (smaller than %u)\n",
1186 } else if (size > BTRFS_MAX_METADATA_BLOCKSIZE) {
1188 "Illegal leafsize (or nodesize) %u (larger than %u)\n",
1189 size, BTRFS_MAX_METADATA_BLOCKSIZE);
1191 } else if (size & (sectorsize - 1)) {
1193 "Illegal leafsize (or nodesize) %u (not align to %u)\n",
1200 static int is_ssd(const char *file)
1204 char sysfs_path[PATH_MAX];
1209 probe = blkid_new_probe_from_filename(file);
1213 /* Device number of this disk (possibly a partition) */
1214 devno = blkid_probe_get_devno(probe);
1218 /* Get whole disk name (not full path) for this devno */
1219 blkid_devno_to_wholedisk(devno, wholedisk, sizeof(wholedisk), NULL);
1221 snprintf(sysfs_path, PATH_MAX, "/sys/block/%s/queue/rotational",
1224 blkid_free_probe(probe);
1226 fd = open(sysfs_path, O_RDONLY);
1231 if (read(fd, &rotational, sizeof(char)) < sizeof(char)) {
1237 return !atoi((const char *)&rotational);
1240 #define BTRFS_FEATURE_LIST_ALL (1ULL << 63)
1242 static const struct btrfs_fs_feature {
1246 } mkfs_features[] = {
1247 { "mixed-bg", BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS,
1248 "mixed data and metadata block groups" },
1249 { "extref", BTRFS_FEATURE_INCOMPAT_EXTENDED_IREF,
1250 "increased hardlink limit per file to 65536" },
1251 { "raid56", BTRFS_FEATURE_INCOMPAT_RAID56,
1252 "raid56 extended format" },
1253 { "skinny-metadata", BTRFS_FEATURE_INCOMPAT_SKINNY_METADATA,
1254 "reduced-size metadata extent refs" },
1255 /* Keep this one last */
1256 { "list-all", BTRFS_FEATURE_LIST_ALL, NULL }
1259 static void list_all_fs_features(void)
1263 fprintf(stderr, "Filesystem features available at mkfs time:\n");
1264 for (i = 0; i < ARRAY_SIZE(mkfs_features) - 1; i++) {
1265 fprintf(stderr, "%-20s- %s (0x%llx)\n",
1266 mkfs_features[i].name,
1267 mkfs_features[i].desc,
1268 mkfs_features[i].flag);
1272 static int parse_one_fs_feature(const char *name, u64 *flags)
1277 for (i = 0; i < ARRAY_SIZE(mkfs_features); i++) {
1278 if (!strcmp(mkfs_features[i].name, name)) {
1279 *flags |= mkfs_features[i].flag;
1287 static void process_fs_features(u64 flags)
1291 for (i = 0; i < ARRAY_SIZE(mkfs_features); i++) {
1292 if (flags & mkfs_features[i].flag) {
1294 "Turning ON incompat feature '%s': %s\n",
1295 mkfs_features[i].name,
1296 mkfs_features[i].desc);
1303 * Return NULL if all features were parsed fine, otherwise return the name of
1304 * the first unparsed.
1306 static char* parse_fs_features(char *namelist, u64 *flags)
1309 char *save_ptr = NULL; /* Satisfy static checkers */
1311 for (this_char = strtok_r(namelist, ",", &save_ptr);
1313 this_char = strtok_r(NULL, ",", &save_ptr)) {
1314 if (parse_one_fs_feature(this_char, flags))
1321 int main(int ac, char **av)
1324 struct btrfs_root *root;
1325 struct btrfs_trans_handle *trans;
1328 u64 block_count = 0;
1329 u64 dev_block_count = 0;
1331 u64 alloc_start = 0;
1332 u64 metadata_profile = 0;
1333 u64 data_profile = 0;
1334 u32 leafsize = sysconf(_SC_PAGESIZE);
1335 u32 sectorsize = 4096;
1336 u32 nodesize = leafsize;
1337 u32 stripesize = 4096;
1339 int option_index = 0;
1344 int data_profile_opt = 0;
1345 int metadata_profile_opt = 0;
1348 int force_overwrite = 0;
1350 char *source_dir = NULL;
1351 int source_dir_set = 0;
1352 u64 num_of_meta_chunks = 0;
1353 u64 size_of_data = 0;
1354 u64 source_dir_size = 0;
1355 struct btrfs_super_block *super;
1364 c = getopt_long(ac, av, "A:b:fl:n:s:m:d:L:O:r:VMK",
1365 long_options, &option_index);
1370 alloc_start = parse_size(optarg);
1373 force_overwrite = 1;
1376 data_profile = parse_profile(optarg);
1377 data_profile_opt = 1;
1381 nodesize = parse_size(optarg);
1382 leafsize = parse_size(optarg);
1385 label = parse_label(optarg);
1388 metadata_profile = parse_profile(optarg);
1389 metadata_profile_opt = 1;
1395 char *orig = strdup(optarg);
1398 tmp = parse_fs_features(tmp, &features);
1401 "Unrecognized filesystem feature '%s'\n",
1407 if (features & BTRFS_FEATURE_LIST_ALL) {
1408 list_all_fs_features();
1414 sectorsize = parse_size(optarg);
1417 block_count = parse_size(optarg);
1418 if (block_count <= 1024*1024*1024) {
1419 printf("SMALL VOLUME: forcing mixed "
1420 "metadata/data groups\n");
1429 source_dir = optarg;
1439 sectorsize = max(sectorsize, (u32)sysconf(_SC_PAGESIZE));
1440 if (check_leaf_or_node_size(leafsize, sectorsize))
1442 if (check_leaf_or_node_size(nodesize, sectorsize))
1444 saved_optind = optind;
1445 dev_cnt = ac - optind;
1449 if (source_dir_set && dev_cnt > 1) {
1451 "The -r option is limited to a single device\n");
1454 while (dev_cnt-- > 0) {
1455 file = av[optind++];
1456 if (is_block_device(file))
1457 if (test_dev_for_mkfs(file, force_overwrite, estr)) {
1458 fprintf(stderr, "Error: %s", estr);
1463 /* if we are here that means all devs are good to btrfsify */
1464 optind = saved_optind;
1465 dev_cnt = ac - optind;
1467 printf("\nWARNING! - %s IS EXPERIMENTAL\n", BTRFS_BUILD_VERSION);
1468 printf("WARNING! - see http://btrfs.wiki.kernel.org before using\n\n");
1470 file = av[optind++];
1473 if (!source_dir_set) {
1475 * open without O_EXCL so that the problem should not
1476 * occur by the following processing.
1477 * (btrfs_register_one_device() fails if O_EXCL is on)
1479 fd = open(file, O_RDWR);
1481 fprintf(stderr, "unable to open %s: %s\n", file,
1486 ret = btrfs_prepare_device(fd, file, zero_end, &dev_block_count,
1487 block_count, &mixed, nodiscard);
1488 if (block_count && block_count > dev_block_count) {
1489 fprintf(stderr, "%s is smaller than requested size\n", file);
1493 fd = open_target(file);
1495 fprintf(stderr, "unable to open the %s\n", file);
1500 source_dir_size = size_sourcedir(source_dir, sectorsize,
1501 &num_of_meta_chunks, &size_of_data);
1502 if(block_count < source_dir_size)
1503 block_count = source_dir_size;
1504 ret = zero_output_file(fd, block_count, sectorsize);
1506 fprintf(stderr, "unable to zero the output file\n");
1509 /* our "device" is the new image file */
1510 dev_block_count = block_count;
1516 if (metadata_profile != data_profile) {
1517 fprintf(stderr, "With mixed block groups data and metadata "
1518 "profiles must be the same\n");
1523 blocks[0] = BTRFS_SUPER_INFO_OFFSET;
1524 for (i = 1; i < 7; i++) {
1525 blocks[i] = BTRFS_SUPER_INFO_OFFSET + 1024 * 1024 +
1529 ret = make_btrfs(fd, file, label, blocks, dev_block_count,
1531 sectorsize, stripesize);
1533 fprintf(stderr, "error during mkfs: %s\n", strerror(-ret));
1537 root = open_ctree(file, 0, O_RDWR);
1539 fprintf(stderr, "Open ctree failed\n");
1543 root->fs_info->alloc_start = alloc_start;
1545 ret = make_root_dir(root, mixed);
1547 fprintf(stderr, "failed to setup the root directory\n");
1551 trans = btrfs_start_transaction(root, 1);
1556 btrfs_register_one_device(file);
1559 while (dev_cnt-- > 0) {
1560 int old_mixed = mixed;
1562 file = av[optind++];
1565 * open without O_EXCL so that the problem should not
1566 * occur by the following processing.
1567 * (btrfs_register_one_device() fails if O_EXCL is on)
1569 fd = open(file, O_RDWR);
1571 fprintf(stderr, "unable to open %s: %s\n", file,
1575 ret = btrfs_device_already_in_root(root, fd,
1576 BTRFS_SUPER_INFO_OFFSET);
1578 fprintf(stderr, "skipping duplicate device %s in FS\n",
1583 ret = btrfs_prepare_device(fd, file, zero_end, &dev_block_count,
1584 block_count, &mixed, nodiscard);
1588 ret = btrfs_add_to_fsid(trans, root, fd, file, dev_block_count,
1589 sectorsize, sectorsize, sectorsize);
1591 btrfs_register_one_device(file);
1595 if (!source_dir_set) {
1596 ret = create_raid_groups(trans, root, data_profile,
1597 data_profile_opt, metadata_profile,
1598 metadata_profile_opt, mixed, ssd);
1602 ret = create_data_reloc_tree(trans, root);
1605 super = root->fs_info->super_copy;
1606 flags = btrfs_super_incompat_flags(super);
1609 * FS features that can be set by other means than -O
1610 * just set the bit here
1613 features |= BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS;
1615 if ((data_profile | metadata_profile) &
1616 (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6)) {
1617 features |= BTRFS_FEATURE_INCOMPAT_RAID56;
1620 process_fs_features(features);
1622 btrfs_set_super_incompat_flags(super, flags);
1624 printf("fs created label %s on %s\n\tnodesize %u leafsize %u "
1625 "sectorsize %u size %s\n",
1626 label, first_file, nodesize, leafsize, sectorsize,
1627 pretty_size(btrfs_super_total_bytes(root->fs_info->super_copy)));
1629 printf("%s\n", BTRFS_BUILD_VERSION);
1630 btrfs_commit_transaction(trans, root);
1632 if (source_dir_set) {
1633 trans = btrfs_start_transaction(root, 1);
1634 ret = create_chunks(trans, root,
1635 num_of_meta_chunks, size_of_data);
1637 btrfs_commit_transaction(trans, root);
1639 ret = make_image(source_dir, root, fd);
1643 ret = close_ctree(root);