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>
38 #include <attr/xattr.h>
39 #include <blkid/blkid.h>
44 #include "transaction.h"
48 static u64 index_cnt = 2;
50 struct directory_name_entry {
54 struct list_head list;
57 static int make_root_dir(struct btrfs_root *root, int mixed)
59 struct btrfs_trans_handle *trans;
60 struct btrfs_key location;
66 trans = btrfs_start_transaction(root, 1);
67 bytes_used = btrfs_super_bytes_used(root->fs_info->super_copy);
69 root->fs_info->system_allocs = 1;
70 ret = btrfs_make_block_group(trans, root, bytes_used,
71 BTRFS_BLOCK_GROUP_SYSTEM,
72 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
73 0, BTRFS_MKFS_SYSTEM_GROUP_SIZE);
77 ret = btrfs_alloc_chunk(trans, root->fs_info->extent_root,
78 &chunk_start, &chunk_size,
79 BTRFS_BLOCK_GROUP_METADATA |
80 BTRFS_BLOCK_GROUP_DATA);
82 ret = btrfs_make_block_group(trans, root, 0,
83 BTRFS_BLOCK_GROUP_METADATA |
84 BTRFS_BLOCK_GROUP_DATA,
85 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
86 chunk_start, chunk_size);
88 printf("Created a data/metadata chunk of size %llu\n", chunk_size);
90 ret = btrfs_alloc_chunk(trans, root->fs_info->extent_root,
91 &chunk_start, &chunk_size,
92 BTRFS_BLOCK_GROUP_METADATA);
94 ret = btrfs_make_block_group(trans, root, 0,
95 BTRFS_BLOCK_GROUP_METADATA,
96 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
97 chunk_start, chunk_size);
101 root->fs_info->system_allocs = 0;
102 btrfs_commit_transaction(trans, root);
103 trans = btrfs_start_transaction(root, 1);
107 ret = btrfs_alloc_chunk(trans, root->fs_info->extent_root,
108 &chunk_start, &chunk_size,
109 BTRFS_BLOCK_GROUP_DATA);
111 ret = btrfs_make_block_group(trans, root, 0,
112 BTRFS_BLOCK_GROUP_DATA,
113 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
114 chunk_start, chunk_size);
118 ret = btrfs_make_root_dir(trans, root->fs_info->tree_root,
119 BTRFS_ROOT_TREE_DIR_OBJECTID);
122 ret = btrfs_make_root_dir(trans, root, BTRFS_FIRST_FREE_OBJECTID);
125 memcpy(&location, &root->fs_info->fs_root->root_key, sizeof(location));
126 location.offset = (u64)-1;
127 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
129 btrfs_super_root_dir(root->fs_info->super_copy),
130 &location, BTRFS_FT_DIR, 0);
134 ret = btrfs_insert_inode_ref(trans, root->fs_info->tree_root,
135 "default", 7, location.objectid,
136 BTRFS_ROOT_TREE_DIR_OBJECTID, 0);
140 btrfs_commit_transaction(trans, root);
145 static void __recow_root(struct btrfs_trans_handle *trans,
146 struct btrfs_root *root)
149 struct extent_buffer *tmp;
151 if (trans->transid != btrfs_root_generation(&root->root_item)) {
152 ret = __btrfs_cow_block(trans, root, root->node,
153 NULL, 0, &tmp, 0, 0);
155 free_extent_buffer(tmp);
159 static void recow_roots(struct btrfs_trans_handle *trans,
160 struct btrfs_root *root)
162 struct btrfs_fs_info *info = root->fs_info;
164 __recow_root(trans, info->fs_root);
165 __recow_root(trans, info->tree_root);
166 __recow_root(trans, info->extent_root);
167 __recow_root(trans, info->chunk_root);
168 __recow_root(trans, info->dev_root);
169 __recow_root(trans, info->csum_root);
172 static int create_one_raid_group(struct btrfs_trans_handle *trans,
173 struct btrfs_root *root, u64 type)
179 ret = btrfs_alloc_chunk(trans, root->fs_info->extent_root,
180 &chunk_start, &chunk_size, type);
182 ret = btrfs_make_block_group(trans, root->fs_info->extent_root, 0,
183 type, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
184 chunk_start, chunk_size);
189 static int create_raid_groups(struct btrfs_trans_handle *trans,
190 struct btrfs_root *root, u64 data_profile,
191 int data_profile_opt, u64 metadata_profile,
192 int metadata_profile_opt, int mixed, int ssd)
194 u64 num_devices = btrfs_super_num_devices(root->fs_info->super_copy);
196 u64 devices_for_raid = num_devices;
200 * Set default profiles according to number of added devices.
201 * For mixed groups defaults are single/single.
203 if (!metadata_profile_opt && !mixed) {
204 if (num_devices == 1 && ssd)
205 printf("Detected a SSD, turning off metadata "
206 "duplication. Mkfs with -m dup if you want to "
207 "force metadata duplication.\n");
208 metadata_profile = (num_devices > 1) ?
209 BTRFS_BLOCK_GROUP_RAID1 : (ssd) ? 0: BTRFS_BLOCK_GROUP_DUP;
211 if (!data_profile_opt && !mixed) {
212 data_profile = (num_devices > 1) ?
213 BTRFS_BLOCK_GROUP_RAID0 : 0; /* raid0 or single */
216 if (devices_for_raid > 4)
217 devices_for_raid = 4;
219 switch (devices_for_raid) {
222 allowed |= BTRFS_BLOCK_GROUP_RAID10;
224 allowed |= BTRFS_BLOCK_GROUP_RAID6;
226 allowed |= BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1 |
227 BTRFS_BLOCK_GROUP_RAID5;
230 allowed |= BTRFS_BLOCK_GROUP_DUP;
233 if (metadata_profile & ~allowed) {
234 fprintf(stderr, "unable to create FS with metadata "
235 "profile %llu (have %llu devices)\n", metadata_profile,
239 if (data_profile & ~allowed) {
240 fprintf(stderr, "unable to create FS with data "
241 "profile %llu (have %llu devices)\n", data_profile,
246 /* allow dup'ed data chunks only in mixed mode */
247 if (!mixed && (data_profile & BTRFS_BLOCK_GROUP_DUP)) {
248 fprintf(stderr, "dup for data is allowed only in mixed mode\n");
252 if (allowed & metadata_profile) {
253 u64 meta_flags = BTRFS_BLOCK_GROUP_METADATA;
255 ret = create_one_raid_group(trans, root,
256 BTRFS_BLOCK_GROUP_SYSTEM |
257 (allowed & metadata_profile));
261 meta_flags |= BTRFS_BLOCK_GROUP_DATA;
263 ret = create_one_raid_group(trans, root, meta_flags |
264 (allowed & metadata_profile));
268 if (!mixed && num_devices > 1 && (allowed & data_profile)) {
269 ret = create_one_raid_group(trans, root,
270 BTRFS_BLOCK_GROUP_DATA |
271 (allowed & data_profile));
274 recow_roots(trans, root);
279 static int create_data_reloc_tree(struct btrfs_trans_handle *trans,
280 struct btrfs_root *root)
282 struct btrfs_key location;
283 struct btrfs_root_item root_item;
284 struct extent_buffer *tmp;
285 u64 objectid = BTRFS_DATA_RELOC_TREE_OBJECTID;
288 ret = btrfs_copy_root(trans, root, root->node, &tmp, objectid);
291 memcpy(&root_item, &root->root_item, sizeof(root_item));
292 btrfs_set_root_bytenr(&root_item, tmp->start);
293 btrfs_set_root_level(&root_item, btrfs_header_level(tmp));
294 btrfs_set_root_generation(&root_item, trans->transid);
295 free_extent_buffer(tmp);
297 location.objectid = objectid;
298 location.type = BTRFS_ROOT_ITEM_KEY;
300 ret = btrfs_insert_root(trans, root->fs_info->tree_root,
301 &location, &root_item);
306 static void print_usage(void)
308 fprintf(stderr, "usage: mkfs.btrfs [options] dev [ dev ... ]\n");
309 fprintf(stderr, "options:\n");
310 fprintf(stderr, "\t -A --alloc-start the offset to start the FS\n");
311 fprintf(stderr, "\t -b --byte-count total number of bytes in the FS\n");
312 fprintf(stderr, "\t -d --data data profile, raid0, raid1, raid5, raid6, raid10, dup or single\n");
313 fprintf(stderr, "\t -f --force force overwrite of existing filesystem\n");
314 fprintf(stderr, "\t -l --leafsize size of btree leaves\n");
315 fprintf(stderr, "\t -L --label set a label\n");
316 fprintf(stderr, "\t -m --metadata metadata profile, values like data profile\n");
317 fprintf(stderr, "\t -M --mixed mix metadata and data together\n");
318 fprintf(stderr, "\t -n --nodesize size of btree nodes\n");
319 fprintf(stderr, "\t -s --sectorsize min block allocation (may not mountable by current kernel)\n");
320 fprintf(stderr, "\t -r --rootdir the source directory\n");
321 fprintf(stderr, "\t -K --nodiscard do not perform whole device TRIM\n");
322 fprintf(stderr, "\t -O --features comma separated list of filesystem features\n");
323 fprintf(stderr, "\t -V --version print the mkfs.btrfs version and exit\n");
324 fprintf(stderr, "%s\n", BTRFS_BUILD_VERSION);
328 static void print_version(void)
330 fprintf(stderr, "mkfs.btrfs, part of %s\n", BTRFS_BUILD_VERSION);
334 static u64 parse_profile(char *s)
336 if (strcmp(s, "raid0") == 0) {
337 return BTRFS_BLOCK_GROUP_RAID0;
338 } else if (strcmp(s, "raid1") == 0) {
339 return BTRFS_BLOCK_GROUP_RAID1;
340 } else if (strcmp(s, "raid5") == 0) {
341 return BTRFS_BLOCK_GROUP_RAID5;
342 } else if (strcmp(s, "raid6") == 0) {
343 return BTRFS_BLOCK_GROUP_RAID6;
344 } else if (strcmp(s, "raid10") == 0) {
345 return BTRFS_BLOCK_GROUP_RAID10;
346 } else if (strcmp(s, "dup") == 0) {
347 return BTRFS_BLOCK_GROUP_DUP;
348 } else if (strcmp(s, "single") == 0) {
351 fprintf(stderr, "Unknown profile %s\n", s);
358 static char *parse_label(char *input)
360 int len = strlen(input);
362 if (len >= BTRFS_LABEL_SIZE) {
363 fprintf(stderr, "Label %s is too long (max %d)\n", input,
364 BTRFS_LABEL_SIZE - 1);
367 return strdup(input);
370 static struct option long_options[] = {
371 { "alloc-start", 1, NULL, 'A'},
372 { "byte-count", 1, NULL, 'b' },
373 { "force", 0, NULL, 'f' },
374 { "leafsize", 1, NULL, 'l' },
375 { "label", 1, NULL, 'L'},
376 { "metadata", 1, NULL, 'm' },
377 { "mixed", 0, NULL, 'M' },
378 { "nodesize", 1, NULL, 'n' },
379 { "sectorsize", 1, NULL, 's' },
380 { "data", 1, NULL, 'd' },
381 { "version", 0, NULL, 'V' },
382 { "rootdir", 1, NULL, 'r' },
383 { "nodiscard", 0, NULL, 'K' },
384 { "features", 0, NULL, 'O' },
388 static int add_directory_items(struct btrfs_trans_handle *trans,
389 struct btrfs_root *root, u64 objectid,
390 ino_t parent_inum, const char *name,
391 struct stat *st, int *dir_index_cnt)
395 struct btrfs_key location;
398 name_len = strlen(name);
400 location.objectid = objectid;
402 btrfs_set_key_type(&location, BTRFS_INODE_ITEM_KEY);
404 if (S_ISDIR(st->st_mode))
405 filetype = BTRFS_FT_DIR;
406 if (S_ISREG(st->st_mode))
407 filetype = BTRFS_FT_REG_FILE;
408 if (S_ISLNK(st->st_mode))
409 filetype = BTRFS_FT_SYMLINK;
411 ret = btrfs_insert_dir_item(trans, root, name, name_len,
412 parent_inum, &location,
413 filetype, index_cnt);
415 *dir_index_cnt = index_cnt;
421 static int fill_inode_item(struct btrfs_trans_handle *trans,
422 struct btrfs_root *root,
423 struct btrfs_inode_item *dst, struct stat *src)
426 u64 sectorsize = root->sectorsize;
429 * btrfs_inode_item has some reserved fields
430 * and represents on-disk inode entry, so
431 * zero everything to prevent information leak
433 memset(dst, 0, sizeof (*dst));
435 btrfs_set_stack_inode_generation(dst, trans->transid);
436 btrfs_set_stack_inode_size(dst, src->st_size);
437 btrfs_set_stack_inode_nbytes(dst, 0);
438 btrfs_set_stack_inode_block_group(dst, 0);
439 btrfs_set_stack_inode_nlink(dst, src->st_nlink);
440 btrfs_set_stack_inode_uid(dst, src->st_uid);
441 btrfs_set_stack_inode_gid(dst, src->st_gid);
442 btrfs_set_stack_inode_mode(dst, src->st_mode);
443 btrfs_set_stack_inode_rdev(dst, 0);
444 btrfs_set_stack_inode_flags(dst, 0);
445 btrfs_set_stack_timespec_sec(&dst->atime, src->st_atime);
446 btrfs_set_stack_timespec_nsec(&dst->atime, 0);
447 btrfs_set_stack_timespec_sec(&dst->ctime, src->st_ctime);
448 btrfs_set_stack_timespec_nsec(&dst->ctime, 0);
449 btrfs_set_stack_timespec_sec(&dst->mtime, src->st_mtime);
450 btrfs_set_stack_timespec_nsec(&dst->mtime, 0);
451 btrfs_set_stack_timespec_sec(&dst->otime, 0);
452 btrfs_set_stack_timespec_nsec(&dst->otime, 0);
454 if (S_ISDIR(src->st_mode)) {
455 btrfs_set_stack_inode_size(dst, 0);
456 btrfs_set_stack_inode_nlink(dst, 1);
458 if (S_ISREG(src->st_mode)) {
459 btrfs_set_stack_inode_size(dst, (u64)src->st_size);
460 if (src->st_size <= BTRFS_MAX_INLINE_DATA_SIZE(root))
461 btrfs_set_stack_inode_nbytes(dst, src->st_size);
463 blocks = src->st_size / sectorsize;
464 if (src->st_size % sectorsize)
466 blocks *= sectorsize;
467 btrfs_set_stack_inode_nbytes(dst, blocks);
470 if (S_ISLNK(src->st_mode))
471 btrfs_set_stack_inode_nbytes(dst, src->st_size + 1);
476 static int directory_select(const struct direct *entry)
478 if ((strncmp(entry->d_name, ".", entry->d_reclen) == 0) ||
479 (strncmp(entry->d_name, "..", entry->d_reclen) == 0))
485 static void free_namelist(struct direct **files, int count)
492 for (i = 0; i < count; ++i)
497 static u64 calculate_dir_inode_size(char *dirname)
500 struct direct **files, *cur_file;
501 u64 dir_inode_size = 0;
503 count = scandir(dirname, &files, directory_select, NULL);
505 for (i = 0; i < count; i++) {
507 dir_inode_size += strlen(cur_file->d_name);
510 free_namelist(files, count);
513 return dir_inode_size;
516 static int add_inode_items(struct btrfs_trans_handle *trans,
517 struct btrfs_root *root,
518 struct stat *st, char *name,
519 u64 self_objectid, ino_t parent_inum,
520 int dir_index_cnt, struct btrfs_inode_item *inode_ret)
523 struct btrfs_key inode_key;
524 struct btrfs_inode_item btrfs_inode;
529 name_len = strlen(name);
530 fill_inode_item(trans, root, &btrfs_inode, st);
531 objectid = self_objectid;
533 if (S_ISDIR(st->st_mode)) {
534 inode_size = calculate_dir_inode_size(name);
535 btrfs_set_stack_inode_size(&btrfs_inode, inode_size);
538 inode_key.objectid = objectid;
539 inode_key.offset = 0;
540 btrfs_set_key_type(&inode_key, BTRFS_INODE_ITEM_KEY);
542 ret = btrfs_insert_inode(trans, root, objectid, &btrfs_inode);
546 ret = btrfs_insert_inode_ref(trans, root, name, name_len,
547 objectid, parent_inum, dir_index_cnt);
551 *inode_ret = btrfs_inode;
556 static int add_xattr_item(struct btrfs_trans_handle *trans,
557 struct btrfs_root *root, u64 objectid,
558 const char *file_name)
562 char xattr_list[XATTR_LIST_MAX];
564 char cur_value[XATTR_SIZE_MAX];
565 char delimiter = '\0';
566 char *next_location = xattr_list;
568 ret = llistxattr(file_name, xattr_list, XATTR_LIST_MAX);
572 fprintf(stderr, "get a list of xattr failed for %s\n",
579 cur_name = strtok(xattr_list, &delimiter);
580 while (cur_name != NULL) {
581 cur_name_len = strlen(cur_name);
582 next_location += cur_name_len + 1;
584 ret = getxattr(file_name, cur_name, cur_value, XATTR_SIZE_MAX);
588 fprintf(stderr, "get a xattr value failed for %s attr %s\n",
589 file_name, cur_name);
593 ret = btrfs_insert_xattr_item(trans, root, cur_name,
594 cur_name_len, cur_value,
597 fprintf(stderr, "insert a xattr item failed for %s\n",
601 cur_name = strtok(next_location, &delimiter);
606 static int custom_alloc_extent(struct btrfs_root *root, u64 num_bytes,
607 u64 hint_byte, struct btrfs_key *ins)
611 u64 last = hint_byte;
614 struct btrfs_block_group_cache *cache;
617 ret = find_first_extent_bit(&root->fs_info->free_space_cache,
618 last, &start, &end, EXTENT_DIRTY);
620 if (wrapped++ == 0) {
628 start = max(last, start);
630 if (last - start < num_bytes)
633 last = start + num_bytes;
634 if (test_range_bit(&root->fs_info->pinned_extents,
635 start, last - 1, EXTENT_DIRTY, 0))
638 cache = btrfs_lookup_block_group(root->fs_info, start);
640 if (cache->flags & BTRFS_BLOCK_GROUP_SYSTEM ||
641 last > cache->key.objectid + cache->key.offset) {
642 last = cache->key.objectid + cache->key.offset;
646 if (cache->flags & (BTRFS_BLOCK_GROUP_SYSTEM |
647 BTRFS_BLOCK_GROUP_METADATA)) {
648 last = cache->key.objectid + cache->key.offset;
652 clear_extent_dirty(&root->fs_info->free_space_cache,
653 start, start + num_bytes - 1, 0);
655 ins->objectid = start;
656 ins->offset = num_bytes;
657 ins->type = BTRFS_EXTENT_ITEM_KEY;
661 fprintf(stderr, "not enough free space\n");
665 static int record_file_extent(struct btrfs_trans_handle *trans,
666 struct btrfs_root *root, u64 objectid,
667 struct btrfs_inode_item *inode,
668 u64 file_pos, u64 disk_bytenr,
672 struct btrfs_fs_info *info = root->fs_info;
673 struct btrfs_root *extent_root = info->extent_root;
674 struct extent_buffer *leaf;
675 struct btrfs_file_extent_item *fi;
676 struct btrfs_key ins_key;
677 struct btrfs_path path;
678 struct btrfs_extent_item *ei;
680 btrfs_init_path(&path);
682 ins_key.objectid = objectid;
684 btrfs_set_key_type(&ins_key, BTRFS_EXTENT_DATA_KEY);
685 ret = btrfs_insert_empty_item(trans, root, &path, &ins_key,
689 leaf = path.nodes[0];
690 fi = btrfs_item_ptr(leaf, path.slots[0],
691 struct btrfs_file_extent_item);
692 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
693 btrfs_set_file_extent_type(leaf, fi, BTRFS_FILE_EXTENT_REG);
694 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
695 btrfs_set_file_extent_disk_num_bytes(leaf, fi, num_bytes);
696 btrfs_set_file_extent_offset(leaf, fi, 0);
697 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
698 btrfs_set_file_extent_ram_bytes(leaf, fi, num_bytes);
699 btrfs_set_file_extent_compression(leaf, fi, 0);
700 btrfs_set_file_extent_encryption(leaf, fi, 0);
701 btrfs_set_file_extent_other_encoding(leaf, fi, 0);
702 btrfs_mark_buffer_dirty(leaf);
704 btrfs_release_path(&path);
706 ins_key.objectid = disk_bytenr;
707 ins_key.offset = num_bytes;
708 ins_key.type = BTRFS_EXTENT_ITEM_KEY;
710 ret = btrfs_insert_empty_item(trans, extent_root, &path,
711 &ins_key, sizeof(*ei));
713 leaf = path.nodes[0];
714 ei = btrfs_item_ptr(leaf, path.slots[0],
715 struct btrfs_extent_item);
717 btrfs_set_extent_refs(leaf, ei, 0);
718 btrfs_set_extent_generation(leaf, ei, trans->transid);
719 btrfs_set_extent_flags(leaf, ei, BTRFS_EXTENT_FLAG_DATA);
721 btrfs_mark_buffer_dirty(leaf);
722 ret = btrfs_update_block_group(trans, root, disk_bytenr,
726 } else if (ret != -EEXIST) {
730 ret = btrfs_inc_extent_ref(trans, root, disk_bytenr, num_bytes, 0,
731 root->root_key.objectid,
734 btrfs_release_path(&path);
738 static int add_symbolic_link(struct btrfs_trans_handle *trans,
739 struct btrfs_root *root,
740 u64 objectid, const char *path_name)
743 u64 sectorsize = root->sectorsize;
744 char *buf = malloc(sectorsize);
746 ret = readlink(path_name, buf, sectorsize);
748 fprintf(stderr, "readlink failed for %s\n", path_name);
751 if (ret >= sectorsize) {
752 fprintf(stderr, "symlink too long for %s", path_name);
757 buf[ret] = '\0'; /* readlink does not do it for us */
758 ret = btrfs_insert_inline_extent(trans, root, objectid, 0,
765 static int add_file_items(struct btrfs_trans_handle *trans,
766 struct btrfs_root *root,
767 struct btrfs_inode_item *btrfs_inode, u64 objectid,
768 ino_t parent_inum, struct stat *st,
769 const char *path_name, int out_fd)
775 struct btrfs_key key;
777 u32 sectorsize = root->sectorsize;
782 fd = open(path_name, O_RDONLY);
784 fprintf(stderr, "%s open failed\n", path_name);
788 blocks = st->st_size / sectorsize;
789 if (st->st_size % sectorsize)
792 if (st->st_size <= BTRFS_MAX_INLINE_DATA_SIZE(root)) {
793 buffer = malloc(st->st_size);
794 ret_read = pread64(fd, buffer, st->st_size, bytes_read);
795 if (ret_read == -1) {
796 fprintf(stderr, "%s read failed\n", path_name);
800 ret = btrfs_insert_inline_extent(trans, root, objectid, 0,
801 buffer, st->st_size);
805 ret = custom_alloc_extent(root, blocks * sectorsize, 0, &key);
809 first_block = key.objectid;
811 buffer = malloc(sectorsize);
814 memset(buffer, 0, sectorsize);
815 ret_read = pread64(fd, buffer, sectorsize, bytes_read);
816 if (ret_read == -1) {
817 fprintf(stderr, "%s read failed\n", path_name);
821 ret = pwrite64(out_fd, buffer, sectorsize,
822 first_block + bytes_read);
823 if (ret != sectorsize) {
824 fprintf(stderr, "output file write failed\n");
828 /* checksum for file data */
829 ret = btrfs_csum_file_block(trans, root->fs_info->csum_root,
830 first_block + (blocks * sectorsize),
831 first_block + bytes_read,
834 fprintf(stderr, "%s checksum failed\n", path_name);
838 bytes_read += ret_read;
840 } while (ret_read == sectorsize);
842 if (num_blocks > 0) {
843 ret = record_file_extent(trans, root, objectid, btrfs_inode,
844 first_block, first_block,
845 blocks * sectorsize);
857 static char *make_path(char *dir, char *name)
861 path = malloc(strlen(dir) + strlen(name) + 2);
865 if (dir[strlen(dir) - 1] != '/')
871 static int traverse_directory(struct btrfs_trans_handle *trans,
872 struct btrfs_root *root, char *dir_name,
873 struct directory_name_entry *dir_head, int out_fd)
877 struct btrfs_inode_item cur_inode;
878 struct btrfs_inode_item *inode_item;
879 int count, i, dir_index_cnt;
880 struct direct **files;
882 struct directory_name_entry *dir_entry, *parent_dir_entry;
883 struct direct *cur_file;
884 ino_t parent_inum, cur_inum;
885 ino_t highest_inum = 0;
886 char *parent_dir_name;
887 struct btrfs_path path;
888 struct extent_buffer *leaf;
889 struct btrfs_key root_dir_key;
890 u64 root_dir_inode_size = 0;
892 /* Add list for source directory */
893 dir_entry = malloc(sizeof(struct directory_name_entry));
894 dir_entry->dir_name = dir_name;
895 dir_entry->path = strdup(dir_name);
897 parent_inum = highest_inum + BTRFS_FIRST_FREE_OBJECTID;
898 dir_entry->inum = parent_inum;
899 list_add_tail(&dir_entry->list, &dir_head->list);
901 btrfs_init_path(&path);
903 root_dir_key.objectid = btrfs_root_dirid(&root->root_item);
904 root_dir_key.offset = 0;
905 btrfs_set_key_type(&root_dir_key, BTRFS_INODE_ITEM_KEY);
906 ret = btrfs_lookup_inode(trans, root, &path, &root_dir_key, 1);
908 fprintf(stderr, "root dir lookup error\n");
912 leaf = path.nodes[0];
913 inode_item = btrfs_item_ptr(leaf, path.slots[0],
914 struct btrfs_inode_item);
916 root_dir_inode_size = calculate_dir_inode_size(dir_name);
917 btrfs_set_inode_size(leaf, inode_item, root_dir_inode_size);
918 btrfs_mark_buffer_dirty(leaf);
920 btrfs_release_path(&path);
923 parent_dir_entry = list_entry(dir_head->list.next,
924 struct directory_name_entry,
926 list_del(&parent_dir_entry->list);
928 parent_inum = parent_dir_entry->inum;
929 parent_dir_name = parent_dir_entry->dir_name;
930 if (chdir(parent_dir_entry->path)) {
931 fprintf(stderr, "chdir error for %s\n",
936 count = scandir(parent_dir_entry->path, &files,
937 directory_select, NULL);
940 fprintf(stderr, "scandir for %s failed: %s\n",
941 parent_dir_name, strerror (errno));
945 for (i = 0; i < count; i++) {
948 if (lstat(cur_file->d_name, &st) == -1) {
949 fprintf(stderr, "lstat failed for file %s\n",
954 cur_inum = ++highest_inum + BTRFS_FIRST_FREE_OBJECTID;
955 ret = add_directory_items(trans, root,
956 cur_inum, parent_inum,
958 &st, &dir_index_cnt);
960 fprintf(stderr, "add_directory_items failed\n");
964 ret = add_inode_items(trans, root, &st,
965 cur_file->d_name, cur_inum,
966 parent_inum, dir_index_cnt,
969 fprintf(stderr, "add_inode_items failed\n");
973 ret = add_xattr_item(trans, root,
974 cur_inum, cur_file->d_name);
976 fprintf(stderr, "add_xattr_item failed\n");
981 if (S_ISDIR(st.st_mode)) {
982 dir_entry = malloc(sizeof(struct directory_name_entry));
983 dir_entry->dir_name = cur_file->d_name;
984 dir_entry->path = make_path(parent_dir_entry->path,
986 dir_entry->inum = cur_inum;
987 list_add_tail(&dir_entry->list, &dir_head->list);
988 } else if (S_ISREG(st.st_mode)) {
989 ret = add_file_items(trans, root, &cur_inode,
990 cur_inum, parent_inum, &st,
991 cur_file->d_name, out_fd);
993 fprintf(stderr, "add_file_items failed\n");
996 } else if (S_ISLNK(st.st_mode)) {
997 ret = add_symbolic_link(trans, root,
998 cur_inum, cur_file->d_name);
1000 fprintf(stderr, "add_symbolic_link failed\n");
1006 free_namelist(files, count);
1007 free(parent_dir_entry->path);
1008 free(parent_dir_entry);
1012 } while (!list_empty(&dir_head->list));
1016 free_namelist(files, count);
1018 free(parent_dir_entry->path);
1019 free(parent_dir_entry);
1023 static int open_target(char *output_name)
1026 output_fd = open(output_name, O_CREAT | O_RDWR | O_TRUNC,
1027 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH);
1032 static int create_chunks(struct btrfs_trans_handle *trans,
1033 struct btrfs_root *root, u64 num_of_meta_chunks,
1038 u64 meta_type = BTRFS_BLOCK_GROUP_METADATA;
1039 u64 data_type = BTRFS_BLOCK_GROUP_DATA;
1040 u64 minimum_data_chunk_size = 8 * 1024 * 1024;
1044 for (i = 0; i < num_of_meta_chunks; i++) {
1045 ret = btrfs_alloc_chunk(trans, root->fs_info->extent_root,
1046 &chunk_start, &chunk_size, meta_type);
1048 ret = btrfs_make_block_group(trans, root->fs_info->extent_root, 0,
1049 meta_type, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
1050 chunk_start, chunk_size);
1052 set_extent_dirty(&root->fs_info->free_space_cache,
1053 chunk_start, chunk_start + chunk_size - 1, 0);
1056 if (size_of_data < minimum_data_chunk_size)
1057 size_of_data = minimum_data_chunk_size;
1058 ret = btrfs_alloc_data_chunk(trans, root->fs_info->extent_root,
1059 &chunk_start, size_of_data, data_type);
1061 ret = btrfs_make_block_group(trans, root->fs_info->extent_root, 0,
1062 data_type, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
1063 chunk_start, size_of_data);
1065 set_extent_dirty(&root->fs_info->free_space_cache,
1066 chunk_start, chunk_start + size_of_data - 1, 0);
1070 static int make_image(char *source_dir, struct btrfs_root *root, int out_fd)
1073 struct btrfs_trans_handle *trans;
1075 struct stat root_st;
1077 struct directory_name_entry dir_head;
1079 ret = lstat(source_dir, &root_st);
1081 fprintf(stderr, "unable to lstat the %s\n", source_dir);
1085 INIT_LIST_HEAD(&dir_head.list);
1087 trans = btrfs_start_transaction(root, 1);
1088 ret = traverse_directory(trans, root, source_dir, &dir_head, out_fd);
1090 fprintf(stderr, "unable to traverse_directory\n");
1093 btrfs_commit_transaction(trans, root);
1095 printf("Making image is completed.\n");
1098 fprintf(stderr, "Making image is aborted.\n");
1103 * This ignores symlinks with unreadable targets and subdirs that can't
1104 * be read. It's a best-effort to give a rough estimate of the size of
1105 * a subdir. It doesn't guarantee that prepopulating btrfs from this
1106 * tree won't still run out of space.
1108 * The rounding up to 4096 is questionable. Previous code used du -B 4096.
1110 static u64 global_total_size;
1111 static int ftw_add_entry_size(const char *fpath, const struct stat *st,
1114 if (type == FTW_F || type == FTW_D)
1115 global_total_size += round_up(st->st_size, 4096);
1120 static u64 size_sourcedir(char *dir_name, u64 sectorsize,
1121 u64 *num_of_meta_chunks_ret, u64 *size_of_data_ret)
1126 u64 default_chunk_size = 8 * 1024 * 1024; /* 8MB */
1127 u64 allocated_meta_size = 8 * 1024 * 1024; /* 8MB */
1128 u64 allocated_total_size = 20 * 1024 * 1024; /* 20MB */
1129 u64 num_of_meta_chunks = 0;
1130 u64 num_of_allocated_meta_chunks =
1131 allocated_meta_size / default_chunk_size;
1133 global_total_size = 0;
1134 ret = ftw(dir_name, ftw_add_entry_size, 10);
1135 dir_size = global_total_size;
1137 fprintf(stderr, "ftw subdir walk of '%s' failed: %s\n",
1138 dir_name, strerror(errno));
1142 num_of_meta_chunks = (dir_size / 2) / default_chunk_size;
1143 if (((dir_size / 2) % default_chunk_size) != 0)
1144 num_of_meta_chunks++;
1145 if (num_of_meta_chunks <= num_of_allocated_meta_chunks)
1146 num_of_meta_chunks = 0;
1148 num_of_meta_chunks -= num_of_allocated_meta_chunks;
1150 total_size = allocated_total_size + dir_size +
1151 (num_of_meta_chunks * default_chunk_size);
1153 *num_of_meta_chunks_ret = num_of_meta_chunks;
1158 static int zero_output_file(int out_fd, u64 size, u32 sectorsize)
1160 int len = sectorsize;
1161 int loop_num = size / sectorsize;
1163 char *buf = malloc(len);
1169 memset(buf, 0, len);
1170 for (i = 0; i < loop_num; i++) {
1171 written = pwrite64(out_fd, buf, len, location);
1174 location += sectorsize;
1180 static int check_leaf_or_node_size(u32 size, u32 sectorsize)
1182 if (size < sectorsize) {
1184 "Illegal leafsize (or nodesize) %u (smaller than %u)\n",
1187 } else if (size > BTRFS_MAX_METADATA_BLOCKSIZE) {
1189 "Illegal leafsize (or nodesize) %u (larger than %u)\n",
1190 size, BTRFS_MAX_METADATA_BLOCKSIZE);
1192 } else if (size & (sectorsize - 1)) {
1194 "Illegal leafsize (or nodesize) %u (not align to %u)\n",
1201 static int is_ssd(const char *file)
1205 char sysfs_path[PATH_MAX];
1210 probe = blkid_new_probe_from_filename(file);
1214 /* Device number of this disk (possibly a partition) */
1215 devno = blkid_probe_get_devno(probe);
1219 /* Get whole disk name (not full path) for this devno */
1220 blkid_devno_to_wholedisk(devno, wholedisk, sizeof(wholedisk), NULL);
1222 snprintf(sysfs_path, PATH_MAX, "/sys/block/%s/queue/rotational",
1225 blkid_free_probe(probe);
1227 fd = open(sysfs_path, O_RDONLY);
1232 if (read(fd, &rotational, sizeof(char)) < sizeof(char)) {
1238 return !atoi((const char *)&rotational);
1241 #define BTRFS_FEATURE_LIST_ALL (1ULL << 63)
1243 static const struct btrfs_fs_feature {
1247 } mkfs_features[] = {
1248 { "mixed-bg", BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS,
1249 "mixed data and metadata block groups" },
1250 { "extref", BTRFS_FEATURE_INCOMPAT_EXTENDED_IREF,
1251 "increased hardlink limit per file to 65536" },
1252 { "raid56", BTRFS_FEATURE_INCOMPAT_RAID56,
1253 "raid56 extended format" },
1254 { "skinny-metadata", BTRFS_FEATURE_INCOMPAT_SKINNY_METADATA,
1255 "reduced-size metadata extent refs" },
1256 /* Keep this one last */
1257 { "list-all", BTRFS_FEATURE_LIST_ALL, NULL }
1260 static void list_all_fs_features(void)
1264 fprintf(stderr, "Filesystem features available at mkfs time:\n");
1265 for (i = 0; i < ARRAY_SIZE(mkfs_features) - 1; i++) {
1266 fprintf(stderr, "%-20s- %s (0x%llx)\n",
1267 mkfs_features[i].name,
1268 mkfs_features[i].desc,
1269 mkfs_features[i].flag);
1273 static int parse_one_fs_feature(const char *name, u64 *flags)
1278 for (i = 0; i < ARRAY_SIZE(mkfs_features); i++) {
1279 if (!strcmp(mkfs_features[i].name, name)) {
1280 *flags |= mkfs_features[i].flag;
1288 static void process_fs_features(u64 flags)
1292 for (i = 0; i < ARRAY_SIZE(mkfs_features); i++) {
1293 if (flags & mkfs_features[i].flag) {
1295 "Turning ON incompat feature '%s': %s\n",
1296 mkfs_features[i].name,
1297 mkfs_features[i].desc);
1304 * Return NULL if all features were parsed fine, otherwise return the name of
1305 * the first unparsed.
1307 static char* parse_fs_features(char *namelist, u64 *flags)
1310 char *save_ptr = NULL; /* Satisfy static checkers */
1312 for (this_char = strtok_r(namelist, ",", &save_ptr);
1314 this_char = strtok_r(NULL, ",", &save_ptr)) {
1315 if (parse_one_fs_feature(this_char, flags))
1322 int main(int ac, char **av)
1325 struct btrfs_root *root;
1326 struct btrfs_trans_handle *trans;
1329 u64 block_count = 0;
1330 u64 dev_block_count = 0;
1332 u64 alloc_start = 0;
1333 u64 metadata_profile = 0;
1334 u64 data_profile = 0;
1335 u32 leafsize = sysconf(_SC_PAGESIZE);
1336 u32 sectorsize = 4096;
1337 u32 nodesize = leafsize;
1338 u32 stripesize = 4096;
1340 int option_index = 0;
1345 int data_profile_opt = 0;
1346 int metadata_profile_opt = 0;
1349 int force_overwrite = 0;
1351 char *source_dir = NULL;
1352 int source_dir_set = 0;
1353 u64 num_of_meta_chunks = 0;
1354 u64 size_of_data = 0;
1355 u64 source_dir_size = 0;
1356 struct btrfs_super_block *super;
1365 c = getopt_long(ac, av, "A:b:fl:n:s:m:d:L:O:r:VMK",
1366 long_options, &option_index);
1371 alloc_start = parse_size(optarg);
1374 force_overwrite = 1;
1377 data_profile = parse_profile(optarg);
1378 data_profile_opt = 1;
1382 nodesize = parse_size(optarg);
1383 leafsize = parse_size(optarg);
1386 label = parse_label(optarg);
1389 metadata_profile = parse_profile(optarg);
1390 metadata_profile_opt = 1;
1396 char *orig = strdup(optarg);
1399 tmp = parse_fs_features(tmp, &features);
1402 "Unrecognized filesystem feature '%s'\n",
1408 if (features & BTRFS_FEATURE_LIST_ALL) {
1409 list_all_fs_features();
1415 sectorsize = parse_size(optarg);
1418 block_count = parse_size(optarg);
1419 if (block_count <= 1024*1024*1024) {
1420 printf("SMALL VOLUME: forcing mixed "
1421 "metadata/data groups\n");
1430 source_dir = optarg;
1440 sectorsize = max(sectorsize, (u32)sysconf(_SC_PAGESIZE));
1441 if (check_leaf_or_node_size(leafsize, sectorsize))
1443 if (check_leaf_or_node_size(nodesize, sectorsize))
1445 saved_optind = optind;
1446 dev_cnt = ac - optind;
1450 if (source_dir_set && dev_cnt > 1) {
1452 "The -r option is limited to a single device\n");
1455 while (dev_cnt-- > 0) {
1456 file = av[optind++];
1457 if (is_block_device(file))
1458 if (test_dev_for_mkfs(file, force_overwrite, estr)) {
1459 fprintf(stderr, "Error: %s", estr);
1464 /* if we are here that means all devs are good to btrfsify */
1465 optind = saved_optind;
1466 dev_cnt = ac - optind;
1468 printf("\nWARNING! - %s IS EXPERIMENTAL\n", BTRFS_BUILD_VERSION);
1469 printf("WARNING! - see http://btrfs.wiki.kernel.org before using\n\n");
1471 file = av[optind++];
1474 if (!source_dir_set) {
1476 * open without O_EXCL so that the problem should not
1477 * occur by the following processing.
1478 * (btrfs_register_one_device() fails if O_EXCL is on)
1480 fd = open(file, O_RDWR);
1482 fprintf(stderr, "unable to open %s: %s\n", file,
1487 ret = btrfs_prepare_device(fd, file, zero_end, &dev_block_count,
1488 block_count, &mixed, nodiscard);
1489 if (block_count && block_count > dev_block_count) {
1490 fprintf(stderr, "%s is smaller than requested size\n", file);
1494 fd = open_target(file);
1496 fprintf(stderr, "unable to open the %s\n", file);
1501 source_dir_size = size_sourcedir(source_dir, sectorsize,
1502 &num_of_meta_chunks, &size_of_data);
1503 if(block_count < source_dir_size)
1504 block_count = source_dir_size;
1505 ret = zero_output_file(fd, block_count, sectorsize);
1507 fprintf(stderr, "unable to zero the output file\n");
1510 /* our "device" is the new image file */
1511 dev_block_count = block_count;
1517 if (metadata_profile != data_profile) {
1518 fprintf(stderr, "With mixed block groups data and metadata "
1519 "profiles must be the same\n");
1524 blocks[0] = BTRFS_SUPER_INFO_OFFSET;
1525 for (i = 1; i < 7; i++) {
1526 blocks[i] = BTRFS_SUPER_INFO_OFFSET + 1024 * 1024 +
1530 ret = make_btrfs(fd, file, label, blocks, dev_block_count,
1532 sectorsize, stripesize);
1534 fprintf(stderr, "error during mkfs: %s\n", strerror(-ret));
1538 root = open_ctree(file, 0, O_RDWR);
1540 fprintf(stderr, "Open ctree failed\n");
1544 root->fs_info->alloc_start = alloc_start;
1546 ret = make_root_dir(root, mixed);
1548 fprintf(stderr, "failed to setup the root directory\n");
1552 trans = btrfs_start_transaction(root, 1);
1557 btrfs_register_one_device(file);
1560 while (dev_cnt-- > 0) {
1561 int old_mixed = mixed;
1563 file = av[optind++];
1566 * open without O_EXCL so that the problem should not
1567 * occur by the following processing.
1568 * (btrfs_register_one_device() fails if O_EXCL is on)
1570 fd = open(file, O_RDWR);
1572 fprintf(stderr, "unable to open %s: %s\n", file,
1576 ret = btrfs_device_already_in_root(root, fd,
1577 BTRFS_SUPER_INFO_OFFSET);
1579 fprintf(stderr, "skipping duplicate device %s in FS\n",
1584 ret = btrfs_prepare_device(fd, file, zero_end, &dev_block_count,
1585 block_count, &mixed, nodiscard);
1589 ret = btrfs_add_to_fsid(trans, root, fd, file, dev_block_count,
1590 sectorsize, sectorsize, sectorsize);
1592 btrfs_register_one_device(file);
1596 if (!source_dir_set) {
1597 ret = create_raid_groups(trans, root, data_profile,
1598 data_profile_opt, metadata_profile,
1599 metadata_profile_opt, mixed, ssd);
1603 ret = create_data_reloc_tree(trans, root);
1606 super = root->fs_info->super_copy;
1607 flags = btrfs_super_incompat_flags(super);
1610 * FS features that can be set by other means than -O
1611 * just set the bit here
1614 features |= BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS;
1616 if ((data_profile | metadata_profile) &
1617 (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6)) {
1618 features |= BTRFS_FEATURE_INCOMPAT_RAID56;
1621 process_fs_features(features);
1623 btrfs_set_super_incompat_flags(super, flags);
1625 printf("fs created label %s on %s\n\tnodesize %u leafsize %u "
1626 "sectorsize %u size %s\n",
1627 label, first_file, nodesize, leafsize, sectorsize,
1628 pretty_size(btrfs_super_total_bytes(root->fs_info->super_copy)));
1630 printf("%s\n", BTRFS_BUILD_VERSION);
1631 btrfs_commit_transaction(trans, root);
1633 if (source_dir_set) {
1634 trans = btrfs_start_transaction(root, 1);
1635 ret = create_chunks(trans, root,
1636 num_of_meta_chunks, size_of_data);
1638 btrfs_commit_transaction(trans, root);
1640 ret = make_image(source_dir, root, fd);
1644 ret = close_ctree(root);