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);
82 "no space to alloc data/metadata chunk\n");
86 ret = btrfs_make_block_group(trans, root, 0,
87 BTRFS_BLOCK_GROUP_METADATA |
88 BTRFS_BLOCK_GROUP_DATA,
89 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
90 chunk_start, chunk_size);
92 printf("Created a data/metadata chunk of size %llu\n", chunk_size);
94 ret = btrfs_alloc_chunk(trans, root->fs_info->extent_root,
95 &chunk_start, &chunk_size,
96 BTRFS_BLOCK_GROUP_METADATA);
98 fprintf(stderr, "no space to alloc metadata chunk\n");
102 ret = btrfs_make_block_group(trans, root, 0,
103 BTRFS_BLOCK_GROUP_METADATA,
104 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
105 chunk_start, chunk_size);
109 root->fs_info->system_allocs = 0;
110 btrfs_commit_transaction(trans, root);
111 trans = btrfs_start_transaction(root, 1);
115 ret = btrfs_alloc_chunk(trans, root->fs_info->extent_root,
116 &chunk_start, &chunk_size,
117 BTRFS_BLOCK_GROUP_DATA);
118 if (ret == -ENOSPC) {
119 fprintf(stderr, "no space to alloc data chunk\n");
123 ret = btrfs_make_block_group(trans, root, 0,
124 BTRFS_BLOCK_GROUP_DATA,
125 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
126 chunk_start, chunk_size);
130 ret = btrfs_make_root_dir(trans, root->fs_info->tree_root,
131 BTRFS_ROOT_TREE_DIR_OBJECTID);
134 ret = btrfs_make_root_dir(trans, root, BTRFS_FIRST_FREE_OBJECTID);
137 memcpy(&location, &root->fs_info->fs_root->root_key, sizeof(location));
138 location.offset = (u64)-1;
139 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
141 btrfs_super_root_dir(root->fs_info->super_copy),
142 &location, BTRFS_FT_DIR, 0);
146 ret = btrfs_insert_inode_ref(trans, root->fs_info->tree_root,
147 "default", 7, location.objectid,
148 BTRFS_ROOT_TREE_DIR_OBJECTID, 0);
152 btrfs_commit_transaction(trans, root);
157 static void __recow_root(struct btrfs_trans_handle *trans,
158 struct btrfs_root *root)
161 struct extent_buffer *tmp;
163 if (trans->transid != btrfs_root_generation(&root->root_item)) {
164 ret = __btrfs_cow_block(trans, root, root->node,
165 NULL, 0, &tmp, 0, 0);
167 free_extent_buffer(tmp);
171 static void recow_roots(struct btrfs_trans_handle *trans,
172 struct btrfs_root *root)
174 struct btrfs_fs_info *info = root->fs_info;
176 __recow_root(trans, info->fs_root);
177 __recow_root(trans, info->tree_root);
178 __recow_root(trans, info->extent_root);
179 __recow_root(trans, info->chunk_root);
180 __recow_root(trans, info->dev_root);
181 __recow_root(trans, info->csum_root);
184 static int create_one_raid_group(struct btrfs_trans_handle *trans,
185 struct btrfs_root *root, u64 type)
191 ret = btrfs_alloc_chunk(trans, root->fs_info->extent_root,
192 &chunk_start, &chunk_size, type);
193 if (ret == -ENOSPC) {
194 fprintf(stderr, "not enough free space\n");
198 ret = btrfs_make_block_group(trans, root->fs_info->extent_root, 0,
199 type, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
200 chunk_start, chunk_size);
205 static int create_raid_groups(struct btrfs_trans_handle *trans,
206 struct btrfs_root *root, u64 data_profile,
207 int data_profile_opt, u64 metadata_profile,
208 int metadata_profile_opt, int mixed, int ssd)
210 u64 num_devices = btrfs_super_num_devices(root->fs_info->super_copy);
213 if (metadata_profile) {
214 u64 meta_flags = BTRFS_BLOCK_GROUP_METADATA;
216 ret = create_one_raid_group(trans, root,
217 BTRFS_BLOCK_GROUP_SYSTEM |
222 meta_flags |= BTRFS_BLOCK_GROUP_DATA;
224 ret = create_one_raid_group(trans, root, meta_flags |
229 if (!mixed && num_devices > 1 && data_profile) {
230 ret = create_one_raid_group(trans, root,
231 BTRFS_BLOCK_GROUP_DATA |
235 recow_roots(trans, root);
240 static int create_data_reloc_tree(struct btrfs_trans_handle *trans,
241 struct btrfs_root *root)
243 struct btrfs_key location;
244 struct btrfs_root_item root_item;
245 struct extent_buffer *tmp;
246 u64 objectid = BTRFS_DATA_RELOC_TREE_OBJECTID;
249 ret = btrfs_copy_root(trans, root, root->node, &tmp, objectid);
252 memcpy(&root_item, &root->root_item, sizeof(root_item));
253 btrfs_set_root_bytenr(&root_item, tmp->start);
254 btrfs_set_root_level(&root_item, btrfs_header_level(tmp));
255 btrfs_set_root_generation(&root_item, trans->transid);
256 free_extent_buffer(tmp);
258 location.objectid = objectid;
259 location.type = BTRFS_ROOT_ITEM_KEY;
261 ret = btrfs_insert_root(trans, root->fs_info->tree_root,
262 &location, &root_item);
268 static void print_usage(void) __attribute__((noreturn));
269 static void print_usage(void)
271 fprintf(stderr, "usage: mkfs.btrfs [options] dev [ dev ... ]\n");
272 fprintf(stderr, "options:\n");
273 fprintf(stderr, "\t -A --alloc-start the offset to start the FS\n");
274 fprintf(stderr, "\t -b --byte-count total number of bytes in the FS\n");
275 fprintf(stderr, "\t -d --data data profile, raid0, raid1, raid5, raid6, raid10, dup or single\n");
276 fprintf(stderr, "\t -f --force force overwrite of existing filesystem\n");
277 fprintf(stderr, "\t -l --leafsize size of btree leaves\n");
278 fprintf(stderr, "\t -L --label set a label\n");
279 fprintf(stderr, "\t -m --metadata metadata profile, values like data profile\n");
280 fprintf(stderr, "\t -M --mixed mix metadata and data together\n");
281 fprintf(stderr, "\t -n --nodesize size of btree nodes\n");
282 fprintf(stderr, "\t -s --sectorsize min block allocation (may not mountable by current kernel)\n");
283 fprintf(stderr, "\t -r --rootdir the source directory\n");
284 fprintf(stderr, "\t -K --nodiscard do not perform whole device TRIM\n");
285 fprintf(stderr, "\t -O --features comma separated list of filesystem features\n");
286 fprintf(stderr, "\t -V --version print the mkfs.btrfs version and exit\n");
287 fprintf(stderr, "%s\n", BTRFS_BUILD_VERSION);
291 static void print_version(void) __attribute__((noreturn));
292 static void print_version(void)
294 fprintf(stderr, "mkfs.btrfs, part of %s\n", BTRFS_BUILD_VERSION);
298 static u64 parse_profile(char *s)
300 if (strcmp(s, "raid0") == 0) {
301 return BTRFS_BLOCK_GROUP_RAID0;
302 } else if (strcmp(s, "raid1") == 0) {
303 return BTRFS_BLOCK_GROUP_RAID1;
304 } else if (strcmp(s, "raid5") == 0) {
305 return BTRFS_BLOCK_GROUP_RAID5;
306 } else if (strcmp(s, "raid6") == 0) {
307 return BTRFS_BLOCK_GROUP_RAID6;
308 } else if (strcmp(s, "raid10") == 0) {
309 return BTRFS_BLOCK_GROUP_RAID10;
310 } else if (strcmp(s, "dup") == 0) {
311 return BTRFS_BLOCK_GROUP_DUP;
312 } else if (strcmp(s, "single") == 0) {
315 fprintf(stderr, "Unknown profile %s\n", s);
322 static char *parse_label(char *input)
324 int len = strlen(input);
326 if (len >= BTRFS_LABEL_SIZE) {
327 fprintf(stderr, "Label %s is too long (max %d)\n", input,
328 BTRFS_LABEL_SIZE - 1);
331 return strdup(input);
334 static struct option long_options[] = {
335 { "alloc-start", 1, NULL, 'A'},
336 { "byte-count", 1, NULL, 'b' },
337 { "force", 0, NULL, 'f' },
338 { "leafsize", 1, NULL, 'l' },
339 { "label", 1, NULL, 'L'},
340 { "metadata", 1, NULL, 'm' },
341 { "mixed", 0, NULL, 'M' },
342 { "nodesize", 1, NULL, 'n' },
343 { "sectorsize", 1, NULL, 's' },
344 { "data", 1, NULL, 'd' },
345 { "version", 0, NULL, 'V' },
346 { "rootdir", 1, NULL, 'r' },
347 { "nodiscard", 0, NULL, 'K' },
348 { "features", 0, NULL, 'O' },
352 static int add_directory_items(struct btrfs_trans_handle *trans,
353 struct btrfs_root *root, u64 objectid,
354 ino_t parent_inum, const char *name,
355 struct stat *st, int *dir_index_cnt)
359 struct btrfs_key location;
362 name_len = strlen(name);
364 location.objectid = objectid;
366 btrfs_set_key_type(&location, BTRFS_INODE_ITEM_KEY);
368 if (S_ISDIR(st->st_mode))
369 filetype = BTRFS_FT_DIR;
370 if (S_ISREG(st->st_mode))
371 filetype = BTRFS_FT_REG_FILE;
372 if (S_ISLNK(st->st_mode))
373 filetype = BTRFS_FT_SYMLINK;
375 ret = btrfs_insert_dir_item(trans, root, name, name_len,
376 parent_inum, &location,
377 filetype, index_cnt);
379 *dir_index_cnt = index_cnt;
385 static int fill_inode_item(struct btrfs_trans_handle *trans,
386 struct btrfs_root *root,
387 struct btrfs_inode_item *dst, struct stat *src)
390 u64 sectorsize = root->sectorsize;
393 * btrfs_inode_item has some reserved fields
394 * and represents on-disk inode entry, so
395 * zero everything to prevent information leak
397 memset(dst, 0, sizeof (*dst));
399 btrfs_set_stack_inode_generation(dst, trans->transid);
400 btrfs_set_stack_inode_size(dst, src->st_size);
401 btrfs_set_stack_inode_nbytes(dst, 0);
402 btrfs_set_stack_inode_block_group(dst, 0);
403 btrfs_set_stack_inode_nlink(dst, src->st_nlink);
404 btrfs_set_stack_inode_uid(dst, src->st_uid);
405 btrfs_set_stack_inode_gid(dst, src->st_gid);
406 btrfs_set_stack_inode_mode(dst, src->st_mode);
407 btrfs_set_stack_inode_rdev(dst, 0);
408 btrfs_set_stack_inode_flags(dst, 0);
409 btrfs_set_stack_timespec_sec(&dst->atime, src->st_atime);
410 btrfs_set_stack_timespec_nsec(&dst->atime, 0);
411 btrfs_set_stack_timespec_sec(&dst->ctime, src->st_ctime);
412 btrfs_set_stack_timespec_nsec(&dst->ctime, 0);
413 btrfs_set_stack_timespec_sec(&dst->mtime, src->st_mtime);
414 btrfs_set_stack_timespec_nsec(&dst->mtime, 0);
415 btrfs_set_stack_timespec_sec(&dst->otime, 0);
416 btrfs_set_stack_timespec_nsec(&dst->otime, 0);
418 if (S_ISDIR(src->st_mode)) {
419 btrfs_set_stack_inode_size(dst, 0);
420 btrfs_set_stack_inode_nlink(dst, 1);
422 if (S_ISREG(src->st_mode)) {
423 btrfs_set_stack_inode_size(dst, (u64)src->st_size);
424 if (src->st_size <= BTRFS_MAX_INLINE_DATA_SIZE(root))
425 btrfs_set_stack_inode_nbytes(dst, src->st_size);
427 blocks = src->st_size / sectorsize;
428 if (src->st_size % sectorsize)
430 blocks *= sectorsize;
431 btrfs_set_stack_inode_nbytes(dst, blocks);
434 if (S_ISLNK(src->st_mode))
435 btrfs_set_stack_inode_nbytes(dst, src->st_size + 1);
440 static int directory_select(const struct direct *entry)
442 if ((strncmp(entry->d_name, ".", entry->d_reclen) == 0) ||
443 (strncmp(entry->d_name, "..", entry->d_reclen) == 0))
449 static void free_namelist(struct direct **files, int count)
456 for (i = 0; i < count; ++i)
461 static u64 calculate_dir_inode_size(char *dirname)
464 struct direct **files, *cur_file;
465 u64 dir_inode_size = 0;
467 count = scandir(dirname, &files, directory_select, NULL);
469 for (i = 0; i < count; i++) {
471 dir_inode_size += strlen(cur_file->d_name);
474 free_namelist(files, count);
477 return dir_inode_size;
480 static int add_inode_items(struct btrfs_trans_handle *trans,
481 struct btrfs_root *root,
482 struct stat *st, char *name,
483 u64 self_objectid, ino_t parent_inum,
484 int dir_index_cnt, struct btrfs_inode_item *inode_ret)
487 struct btrfs_key inode_key;
488 struct btrfs_inode_item btrfs_inode;
493 name_len = strlen(name);
494 fill_inode_item(trans, root, &btrfs_inode, st);
495 objectid = self_objectid;
497 if (S_ISDIR(st->st_mode)) {
498 inode_size = calculate_dir_inode_size(name);
499 btrfs_set_stack_inode_size(&btrfs_inode, inode_size);
502 inode_key.objectid = objectid;
503 inode_key.offset = 0;
504 btrfs_set_key_type(&inode_key, BTRFS_INODE_ITEM_KEY);
506 ret = btrfs_insert_inode(trans, root, objectid, &btrfs_inode);
510 ret = btrfs_insert_inode_ref(trans, root, name, name_len,
511 objectid, parent_inum, dir_index_cnt);
515 *inode_ret = btrfs_inode;
520 static int add_xattr_item(struct btrfs_trans_handle *trans,
521 struct btrfs_root *root, u64 objectid,
522 const char *file_name)
526 char xattr_list[XATTR_LIST_MAX];
528 char cur_value[XATTR_SIZE_MAX];
529 char delimiter = '\0';
530 char *next_location = xattr_list;
532 ret = llistxattr(file_name, xattr_list, XATTR_LIST_MAX);
536 fprintf(stderr, "get a list of xattr failed for %s\n",
543 cur_name = strtok(xattr_list, &delimiter);
544 while (cur_name != NULL) {
545 cur_name_len = strlen(cur_name);
546 next_location += cur_name_len + 1;
548 ret = getxattr(file_name, cur_name, cur_value, XATTR_SIZE_MAX);
552 fprintf(stderr, "get a xattr value failed for %s attr %s\n",
553 file_name, cur_name);
557 ret = btrfs_insert_xattr_item(trans, root, cur_name,
558 cur_name_len, cur_value,
561 fprintf(stderr, "insert a xattr item failed for %s\n",
565 cur_name = strtok(next_location, &delimiter);
571 static int add_symbolic_link(struct btrfs_trans_handle *trans,
572 struct btrfs_root *root,
573 u64 objectid, const char *path_name)
576 u64 sectorsize = root->sectorsize;
577 char *buf = malloc(sectorsize);
579 ret = readlink(path_name, buf, sectorsize);
581 fprintf(stderr, "readlink failed for %s\n", path_name);
584 if (ret >= sectorsize) {
585 fprintf(stderr, "symlink too long for %s", path_name);
590 buf[ret] = '\0'; /* readlink does not do it for us */
591 ret = btrfs_insert_inline_extent(trans, root, objectid, 0,
598 static int add_file_items(struct btrfs_trans_handle *trans,
599 struct btrfs_root *root,
600 struct btrfs_inode_item *btrfs_inode, u64 objectid,
601 ino_t parent_inum, struct stat *st,
602 const char *path_name, int out_fd)
607 struct btrfs_key key;
609 u32 sectorsize = root->sectorsize;
614 struct extent_buffer *eb = NULL;
617 fd = open(path_name, O_RDONLY);
619 fprintf(stderr, "%s open failed\n", path_name);
623 blocks = st->st_size / sectorsize;
624 if (st->st_size % sectorsize)
627 if (st->st_size <= BTRFS_MAX_INLINE_DATA_SIZE(root)) {
628 char *buffer = malloc(st->st_size);
629 ret_read = pread64(fd, buffer, st->st_size, bytes_read);
630 if (ret_read == -1) {
631 fprintf(stderr, "%s read failed\n", path_name);
635 ret = btrfs_insert_inline_extent(trans, root, objectid, 0,
636 buffer, st->st_size);
641 /* round up our st_size to the FS blocksize */
642 total_bytes = (u64)blocks * sectorsize;
645 * do our IO in extent buffers so it can work
646 * against any raid type
648 eb = malloc(sizeof(*eb) + sectorsize);
653 memset(eb, 0, sizeof(*eb) + sectorsize);
658 * keep our extent size at 1MB max, this makes it easier to work inside
659 * the tiny block groups created during mkfs
661 cur_bytes = min(total_bytes, 1024ULL * 1024);
662 ret = btrfs_reserve_extent(trans, root, cur_bytes, 0, 0, (u64)-1,
667 first_block = key.objectid;
670 while (bytes_read < cur_bytes) {
672 memset(eb->data, 0, sectorsize);
674 ret_read = pread64(fd, eb->data, sectorsize, file_pos + bytes_read);
675 if (ret_read == -1) {
676 fprintf(stderr, "%s read failed\n", path_name);
680 eb->start = first_block + bytes_read;
681 eb->len = sectorsize;
684 * we're doing the csum before we record the extent, but
687 ret = btrfs_csum_file_block(trans, root->fs_info->csum_root,
688 first_block + bytes_read + sectorsize,
689 first_block + bytes_read,
690 eb->data, sectorsize);
694 ret = write_and_map_eb(trans, root, eb);
696 fprintf(stderr, "output file write failed\n");
700 bytes_read += sectorsize;
704 ret = btrfs_record_file_extent(trans, root, objectid, btrfs_inode,
705 file_pos, first_block, cur_bytes);
711 file_pos += cur_bytes;
712 total_bytes -= cur_bytes;
724 static char *make_path(char *dir, char *name)
728 path = malloc(strlen(dir) + strlen(name) + 2);
732 if (dir[strlen(dir) - 1] != '/')
738 static int traverse_directory(struct btrfs_trans_handle *trans,
739 struct btrfs_root *root, char *dir_name,
740 struct directory_name_entry *dir_head, int out_fd)
744 struct btrfs_inode_item cur_inode;
745 struct btrfs_inode_item *inode_item;
746 int count, i, dir_index_cnt;
747 struct direct **files;
749 struct directory_name_entry *dir_entry, *parent_dir_entry;
750 struct direct *cur_file;
751 ino_t parent_inum, cur_inum;
752 ino_t highest_inum = 0;
753 char *parent_dir_name;
754 struct btrfs_path path;
755 struct extent_buffer *leaf;
756 struct btrfs_key root_dir_key;
757 u64 root_dir_inode_size = 0;
759 /* Add list for source directory */
760 dir_entry = malloc(sizeof(struct directory_name_entry));
761 dir_entry->dir_name = dir_name;
762 dir_entry->path = strdup(dir_name);
764 parent_inum = highest_inum + BTRFS_FIRST_FREE_OBJECTID;
765 dir_entry->inum = parent_inum;
766 list_add_tail(&dir_entry->list, &dir_head->list);
768 btrfs_init_path(&path);
770 root_dir_key.objectid = btrfs_root_dirid(&root->root_item);
771 root_dir_key.offset = 0;
772 btrfs_set_key_type(&root_dir_key, BTRFS_INODE_ITEM_KEY);
773 ret = btrfs_lookup_inode(trans, root, &path, &root_dir_key, 1);
775 fprintf(stderr, "root dir lookup error\n");
779 leaf = path.nodes[0];
780 inode_item = btrfs_item_ptr(leaf, path.slots[0],
781 struct btrfs_inode_item);
783 root_dir_inode_size = calculate_dir_inode_size(dir_name);
784 btrfs_set_inode_size(leaf, inode_item, root_dir_inode_size);
785 btrfs_mark_buffer_dirty(leaf);
787 btrfs_release_path(&path);
790 parent_dir_entry = list_entry(dir_head->list.next,
791 struct directory_name_entry,
793 list_del(&parent_dir_entry->list);
795 parent_inum = parent_dir_entry->inum;
796 parent_dir_name = parent_dir_entry->dir_name;
797 if (chdir(parent_dir_entry->path)) {
798 fprintf(stderr, "chdir error for %s\n",
803 count = scandir(parent_dir_entry->path, &files,
804 directory_select, NULL);
807 fprintf(stderr, "scandir for %s failed: %s\n",
808 parent_dir_name, strerror (errno));
812 for (i = 0; i < count; i++) {
815 if (lstat(cur_file->d_name, &st) == -1) {
816 fprintf(stderr, "lstat failed for file %s\n",
821 cur_inum = ++highest_inum + BTRFS_FIRST_FREE_OBJECTID;
822 ret = add_directory_items(trans, root,
823 cur_inum, parent_inum,
825 &st, &dir_index_cnt);
827 fprintf(stderr, "add_directory_items failed\n");
831 ret = add_inode_items(trans, root, &st,
832 cur_file->d_name, cur_inum,
833 parent_inum, dir_index_cnt,
836 fprintf(stderr, "add_inode_items failed\n");
840 ret = add_xattr_item(trans, root,
841 cur_inum, cur_file->d_name);
843 fprintf(stderr, "add_xattr_item failed\n");
848 if (S_ISDIR(st.st_mode)) {
849 dir_entry = malloc(sizeof(struct directory_name_entry));
850 dir_entry->dir_name = cur_file->d_name;
851 dir_entry->path = make_path(parent_dir_entry->path,
853 dir_entry->inum = cur_inum;
854 list_add_tail(&dir_entry->list, &dir_head->list);
855 } else if (S_ISREG(st.st_mode)) {
856 ret = add_file_items(trans, root, &cur_inode,
857 cur_inum, parent_inum, &st,
858 cur_file->d_name, out_fd);
860 fprintf(stderr, "add_file_items failed\n");
863 } else if (S_ISLNK(st.st_mode)) {
864 ret = add_symbolic_link(trans, root,
865 cur_inum, cur_file->d_name);
867 fprintf(stderr, "add_symbolic_link failed\n");
873 free_namelist(files, count);
874 free(parent_dir_entry->path);
875 free(parent_dir_entry);
879 } while (!list_empty(&dir_head->list));
883 free_namelist(files, count);
885 free(parent_dir_entry->path);
886 free(parent_dir_entry);
890 static int open_target(char *output_name)
893 output_fd = open(output_name, O_CREAT | O_RDWR | O_TRUNC,
894 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH);
899 static int create_chunks(struct btrfs_trans_handle *trans,
900 struct btrfs_root *root, u64 num_of_meta_chunks,
905 u64 meta_type = BTRFS_BLOCK_GROUP_METADATA;
906 u64 data_type = BTRFS_BLOCK_GROUP_DATA;
907 u64 minimum_data_chunk_size = 8 * 1024 * 1024;
911 for (i = 0; i < num_of_meta_chunks; i++) {
912 ret = btrfs_alloc_chunk(trans, root->fs_info->extent_root,
913 &chunk_start, &chunk_size, meta_type);
915 ret = btrfs_make_block_group(trans, root->fs_info->extent_root, 0,
916 meta_type, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
917 chunk_start, chunk_size);
919 set_extent_dirty(&root->fs_info->free_space_cache,
920 chunk_start, chunk_start + chunk_size - 1, 0);
923 if (size_of_data < minimum_data_chunk_size)
924 size_of_data = minimum_data_chunk_size;
926 ret = btrfs_alloc_data_chunk(trans, root->fs_info->extent_root,
927 &chunk_start, size_of_data, data_type);
929 ret = btrfs_make_block_group(trans, root->fs_info->extent_root, 0,
930 data_type, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
931 chunk_start, size_of_data);
933 set_extent_dirty(&root->fs_info->free_space_cache,
934 chunk_start, chunk_start + size_of_data - 1, 0);
938 static int make_image(char *source_dir, struct btrfs_root *root, int out_fd)
941 struct btrfs_trans_handle *trans;
945 struct directory_name_entry dir_head;
947 struct directory_name_entry *dir_entry = NULL;
949 ret = lstat(source_dir, &root_st);
951 fprintf(stderr, "unable to lstat the %s\n", source_dir);
955 INIT_LIST_HEAD(&dir_head.list);
957 trans = btrfs_start_transaction(root, 1);
958 ret = traverse_directory(trans, root, source_dir, &dir_head, out_fd);
960 fprintf(stderr, "unable to traverse_directory\n");
963 btrfs_commit_transaction(trans, root);
965 printf("Making image is completed.\n");
968 while (!list_empty(&dir_head.list)) {
969 dir_entry = list_entry(dir_head.list.next,
970 struct directory_name_entry, list);
971 list_del(&dir_entry->list);
974 fprintf(stderr, "Making image is aborted.\n");
979 * This ignores symlinks with unreadable targets and subdirs that can't
980 * be read. It's a best-effort to give a rough estimate of the size of
981 * a subdir. It doesn't guarantee that prepopulating btrfs from this
982 * tree won't still run out of space.
984 * The rounding up to 4096 is questionable. Previous code used du -B 4096.
986 static u64 global_total_size;
987 static int ftw_add_entry_size(const char *fpath, const struct stat *st,
990 if (type == FTW_F || type == FTW_D)
991 global_total_size += round_up(st->st_size, 4096);
996 static u64 size_sourcedir(char *dir_name, u64 sectorsize,
997 u64 *num_of_meta_chunks_ret, u64 *size_of_data_ret)
1002 u64 default_chunk_size = 8 * 1024 * 1024; /* 8MB */
1003 u64 allocated_meta_size = 8 * 1024 * 1024; /* 8MB */
1004 u64 allocated_total_size = 20 * 1024 * 1024; /* 20MB */
1005 u64 num_of_meta_chunks = 0;
1006 u64 num_of_data_chunks = 0;
1007 u64 num_of_allocated_meta_chunks =
1008 allocated_meta_size / default_chunk_size;
1010 global_total_size = 0;
1011 ret = ftw(dir_name, ftw_add_entry_size, 10);
1012 dir_size = global_total_size;
1014 fprintf(stderr, "ftw subdir walk of '%s' failed: %s\n",
1015 dir_name, strerror(errno));
1019 num_of_data_chunks = (dir_size + default_chunk_size - 1) /
1022 num_of_meta_chunks = (dir_size / 2) / default_chunk_size;
1023 if (((dir_size / 2) % default_chunk_size) != 0)
1024 num_of_meta_chunks++;
1025 if (num_of_meta_chunks <= num_of_allocated_meta_chunks)
1026 num_of_meta_chunks = 0;
1028 num_of_meta_chunks -= num_of_allocated_meta_chunks;
1030 total_size = allocated_total_size +
1031 (num_of_data_chunks * default_chunk_size) +
1032 (num_of_meta_chunks * default_chunk_size);
1034 *num_of_meta_chunks_ret = num_of_meta_chunks;
1035 *size_of_data_ret = num_of_data_chunks * default_chunk_size;
1039 static int zero_output_file(int out_fd, u64 size, u32 sectorsize)
1041 int len = sectorsize;
1042 int loop_num = size / sectorsize;
1044 char *buf = malloc(len);
1050 memset(buf, 0, len);
1051 for (i = 0; i < loop_num; i++) {
1052 written = pwrite64(out_fd, buf, len, location);
1055 location += sectorsize;
1061 static int check_leaf_or_node_size(u32 size, u32 sectorsize)
1063 if (size < sectorsize) {
1065 "Illegal leafsize (or nodesize) %u (smaller than %u)\n",
1068 } else if (size > BTRFS_MAX_METADATA_BLOCKSIZE) {
1070 "Illegal leafsize (or nodesize) %u (larger than %u)\n",
1071 size, BTRFS_MAX_METADATA_BLOCKSIZE);
1073 } else if (size & (sectorsize - 1)) {
1075 "Illegal leafsize (or nodesize) %u (not align to %u)\n",
1082 static int is_ssd(const char *file)
1086 char sysfs_path[PATH_MAX];
1092 probe = blkid_new_probe_from_filename(file);
1096 /* Device number of this disk (possibly a partition) */
1097 devno = blkid_probe_get_devno(probe);
1099 blkid_free_probe(probe);
1103 /* Get whole disk name (not full path) for this devno */
1104 ret = blkid_devno_to_wholedisk(devno,
1105 wholedisk, sizeof(wholedisk), NULL);
1107 blkid_free_probe(probe);
1111 snprintf(sysfs_path, PATH_MAX, "/sys/block/%s/queue/rotational",
1114 blkid_free_probe(probe);
1116 fd = open(sysfs_path, O_RDONLY);
1121 if (read(fd, &rotational, sizeof(char)) < sizeof(char)) {
1127 return !atoi((const char *)&rotational);
1130 #define BTRFS_FEATURE_LIST_ALL (1ULL << 63)
1132 static const struct btrfs_fs_feature {
1136 } mkfs_features[] = {
1137 { "mixed-bg", BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS,
1138 "mixed data and metadata block groups" },
1139 { "extref", BTRFS_FEATURE_INCOMPAT_EXTENDED_IREF,
1140 "increased hardlink limit per file to 65536" },
1141 { "raid56", BTRFS_FEATURE_INCOMPAT_RAID56,
1142 "raid56 extended format" },
1143 { "skinny-metadata", BTRFS_FEATURE_INCOMPAT_SKINNY_METADATA,
1144 "reduced-size metadata extent refs" },
1145 /* Keep this one last */
1146 { "list-all", BTRFS_FEATURE_LIST_ALL, NULL }
1149 static void list_all_fs_features(void)
1153 fprintf(stderr, "Filesystem features available at mkfs time:\n");
1154 for (i = 0; i < ARRAY_SIZE(mkfs_features) - 1; i++) {
1155 fprintf(stderr, "%-20s- %s (0x%llx)\n",
1156 mkfs_features[i].name,
1157 mkfs_features[i].desc,
1158 mkfs_features[i].flag);
1162 static int parse_one_fs_feature(const char *name, u64 *flags)
1167 for (i = 0; i < ARRAY_SIZE(mkfs_features); i++) {
1168 if (!strcmp(mkfs_features[i].name, name)) {
1169 *flags |= mkfs_features[i].flag;
1177 static void process_fs_features(u64 flags)
1181 for (i = 0; i < ARRAY_SIZE(mkfs_features); i++) {
1182 if (flags & mkfs_features[i].flag) {
1184 "Turning ON incompat feature '%s': %s\n",
1185 mkfs_features[i].name,
1186 mkfs_features[i].desc);
1193 * Return NULL if all features were parsed fine, otherwise return the name of
1194 * the first unparsed.
1196 static char* parse_fs_features(char *namelist, u64 *flags)
1199 char *save_ptr = NULL; /* Satisfy static checkers */
1201 for (this_char = strtok_r(namelist, ",", &save_ptr);
1203 this_char = strtok_r(NULL, ",", &save_ptr)) {
1204 if (parse_one_fs_feature(this_char, flags))
1211 int main(int ac, char **av)
1214 struct btrfs_root *root;
1215 struct btrfs_trans_handle *trans;
1218 u64 block_count = 0;
1219 u64 dev_block_count = 0;
1221 u64 alloc_start = 0;
1222 u64 metadata_profile = 0;
1223 u64 data_profile = 0;
1224 u32 leafsize = sysconf(_SC_PAGESIZE);
1225 u32 sectorsize = 4096;
1226 u32 nodesize = leafsize;
1227 u32 stripesize = 4096;
1229 int option_index = 0;
1234 int data_profile_opt = 0;
1235 int metadata_profile_opt = 0;
1238 int force_overwrite = 0;
1240 char *source_dir = NULL;
1241 int source_dir_set = 0;
1242 u64 num_of_meta_chunks = 0;
1243 u64 size_of_data = 0;
1244 u64 source_dir_size = 0;
1252 c = getopt_long(ac, av, "A:b:fl:n:s:m:d:L:O:r:VMK",
1253 long_options, &option_index);
1258 alloc_start = parse_size(optarg);
1261 force_overwrite = 1;
1264 data_profile = parse_profile(optarg);
1265 data_profile_opt = 1;
1269 nodesize = parse_size(optarg);
1270 leafsize = parse_size(optarg);
1273 label = parse_label(optarg);
1276 metadata_profile = parse_profile(optarg);
1277 metadata_profile_opt = 1;
1283 char *orig = strdup(optarg);
1286 tmp = parse_fs_features(tmp, &features);
1289 "Unrecognized filesystem feature '%s'\n",
1295 if (features & BTRFS_FEATURE_LIST_ALL) {
1296 list_all_fs_features();
1302 sectorsize = parse_size(optarg);
1305 block_count = parse_size(optarg);
1306 if (block_count <= 1024*1024*1024) {
1307 printf("SMALL VOLUME: forcing mixed "
1308 "metadata/data groups\n");
1317 source_dir = optarg;
1327 sectorsize = max(sectorsize, (u32)sysconf(_SC_PAGESIZE));
1328 if (check_leaf_or_node_size(leafsize, sectorsize))
1330 if (check_leaf_or_node_size(nodesize, sectorsize))
1332 saved_optind = optind;
1333 dev_cnt = ac - optind;
1337 if (source_dir_set && dev_cnt > 1) {
1339 "The -r option is limited to a single device\n");
1342 while (dev_cnt-- > 0) {
1343 file = av[optind++];
1344 if (is_block_device(file))
1345 if (test_dev_for_mkfs(file, force_overwrite, estr)) {
1346 fprintf(stderr, "Error: %s", estr);
1351 optind = saved_optind;
1352 dev_cnt = ac - optind;
1354 file = av[optind++];
1357 if (is_vol_small(file)) {
1358 printf("SMALL VOLUME: forcing mixed metadata/data groups\n");
1360 if (metadata_profile != data_profile) {
1361 if (metadata_profile_opt || data_profile_opt) {
1363 "With mixed block groups data and metadata profiles must be the same\n");
1369 * Set default profiles according to number of added devices.
1370 * For mixed groups defaults are single/single.
1373 if (!metadata_profile_opt) {
1374 if (dev_cnt == 1 && ssd)
1375 printf("Detected a SSD, turning off metadata "
1376 "duplication. Mkfs with -m dup if you want to "
1377 "force metadata duplication.\n");
1379 metadata_profile = (dev_cnt > 1) ?
1380 BTRFS_BLOCK_GROUP_RAID1 : (ssd) ?
1381 0: BTRFS_BLOCK_GROUP_DUP;
1383 if (!data_profile_opt) {
1384 data_profile = (dev_cnt > 1) ?
1385 BTRFS_BLOCK_GROUP_RAID0 : 0; /* raid0 or single */
1388 metadata_profile = 0;
1392 ret = test_num_disk_vs_raid(metadata_profile, data_profile,
1393 dev_cnt, mixed, estr);
1395 fprintf(stderr, "Error: %s\n", estr);
1399 /* if we are here that means all devs are good to btrfsify */
1400 printf("\nWARNING! - %s IS EXPERIMENTAL\n", BTRFS_BUILD_VERSION);
1401 printf("WARNING! - see http://btrfs.wiki.kernel.org before using\n\n");
1405 if (!source_dir_set) {
1407 * open without O_EXCL so that the problem should not
1408 * occur by the following processing.
1409 * (btrfs_register_one_device() fails if O_EXCL is on)
1411 fd = open(file, O_RDWR);
1413 fprintf(stderr, "unable to open %s: %s\n", file,
1418 ret = btrfs_prepare_device(fd, file, zero_end, &dev_block_count,
1419 block_count, &mixed, discard);
1420 if (block_count && block_count > dev_block_count) {
1421 fprintf(stderr, "%s is smaller than requested size\n", file);
1425 fd = open_target(file);
1427 fprintf(stderr, "unable to open the %s\n", file);
1432 source_dir_size = size_sourcedir(source_dir, sectorsize,
1433 &num_of_meta_chunks, &size_of_data);
1434 if(block_count < source_dir_size)
1435 block_count = source_dir_size;
1436 ret = zero_output_file(fd, block_count, sectorsize);
1438 fprintf(stderr, "unable to zero the output file\n");
1441 /* our "device" is the new image file */
1442 dev_block_count = block_count;
1445 /* To create the first block group and chunk 0 in make_btrfs */
1446 if (dev_block_count < BTRFS_MKFS_SYSTEM_GROUP_SIZE) {
1447 fprintf(stderr, "device is too small to make filesystem\n");
1451 blocks[0] = BTRFS_SUPER_INFO_OFFSET;
1452 for (i = 1; i < 7; i++) {
1453 blocks[i] = BTRFS_SUPER_INFO_OFFSET + 1024 * 1024 +
1458 * FS features that can be set by other means than -O
1459 * just set the bit here
1462 features |= BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS;
1464 if ((data_profile | metadata_profile) &
1465 (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6)) {
1466 features |= BTRFS_FEATURE_INCOMPAT_RAID56;
1469 process_fs_features(features);
1471 ret = make_btrfs(fd, file, label, blocks, dev_block_count,
1473 sectorsize, stripesize, features);
1475 fprintf(stderr, "error during mkfs: %s\n", strerror(-ret));
1479 root = open_ctree(file, 0, OPEN_CTREE_WRITES);
1481 fprintf(stderr, "Open ctree failed\n");
1485 root->fs_info->alloc_start = alloc_start;
1487 ret = make_root_dir(root, mixed);
1489 fprintf(stderr, "failed to setup the root directory\n");
1493 trans = btrfs_start_transaction(root, 1);
1498 btrfs_register_one_device(file);
1501 while (dev_cnt-- > 0) {
1502 int old_mixed = mixed;
1504 file = av[optind++];
1507 * open without O_EXCL so that the problem should not
1508 * occur by the following processing.
1509 * (btrfs_register_one_device() fails if O_EXCL is on)
1511 fd = open(file, O_RDWR);
1513 fprintf(stderr, "unable to open %s: %s\n", file,
1517 ret = btrfs_device_already_in_root(root, fd,
1518 BTRFS_SUPER_INFO_OFFSET);
1520 fprintf(stderr, "skipping duplicate device %s in FS\n",
1525 ret = btrfs_prepare_device(fd, file, zero_end, &dev_block_count,
1526 block_count, &mixed, discard);
1530 ret = btrfs_add_to_fsid(trans, root, fd, file, dev_block_count,
1531 sectorsize, sectorsize, sectorsize);
1533 btrfs_register_one_device(file);
1537 if (!source_dir_set) {
1538 ret = create_raid_groups(trans, root, data_profile,
1539 data_profile_opt, metadata_profile,
1540 metadata_profile_opt, mixed, ssd);
1544 ret = create_data_reloc_tree(trans, root);
1547 printf("fs created label %s on %s\n\tnodesize %u leafsize %u "
1548 "sectorsize %u size %s\n",
1549 label, first_file, nodesize, leafsize, sectorsize,
1550 pretty_size(btrfs_super_total_bytes(root->fs_info->super_copy)));
1552 printf("%s\n", BTRFS_BUILD_VERSION);
1553 btrfs_commit_transaction(trans, root);
1555 if (source_dir_set) {
1556 trans = btrfs_start_transaction(root, 1);
1557 ret = create_chunks(trans, root,
1558 num_of_meta_chunks, size_of_data);
1560 btrfs_commit_transaction(trans, root);
1562 ret = make_image(source_dir, root, fd);
1566 ret = close_ctree(root);