#include <uuid/uuid.h>
#include <blkid/blkid.h>
#include <fcntl.h>
+#include <limits.h>
#include "ctree.h"
#include "disk-io.h"
#include "volumes.h"
[6] = BTRFS_CSUM_TREE_OBJECTID,
};
+static int btrfs_create_tree_root(int fd, struct btrfs_mkfs_config *cfg,
+ struct extent_buffer *buf)
+{
+ struct btrfs_root_item root_item;
+ struct btrfs_inode_item *inode_item;
+ struct btrfs_disk_key disk_key;
+ u32 nritems = 0;
+ u32 itemoff;
+ int ret = 0;
+ int blk;
+
+ memset(buf->data + sizeof(struct btrfs_header), 0,
+ cfg->nodesize - sizeof(struct btrfs_header));
+ memset(&root_item, 0, sizeof(root_item));
+ memset(&disk_key, 0, sizeof(disk_key));
+
+ /* create the items for the root tree */
+ inode_item = &root_item.inode;
+ btrfs_set_stack_inode_generation(inode_item, 1);
+ btrfs_set_stack_inode_size(inode_item, 3);
+ btrfs_set_stack_inode_nlink(inode_item, 1);
+ btrfs_set_stack_inode_nbytes(inode_item, cfg->nodesize);
+ btrfs_set_stack_inode_mode(inode_item, S_IFDIR | 0755);
+ btrfs_set_root_refs(&root_item, 1);
+ btrfs_set_root_used(&root_item, cfg->nodesize);
+ btrfs_set_root_generation(&root_item, 1);
+
+ btrfs_set_disk_key_type(&disk_key, BTRFS_ROOT_ITEM_KEY);
+ btrfs_set_disk_key_offset(&disk_key, 0);
+ itemoff = __BTRFS_LEAF_DATA_SIZE(cfg->nodesize) - sizeof(root_item);
+
+ for (blk = 0; blk < MKFS_BLOCK_COUNT; blk++) {
+ if (blk == MKFS_SUPER_BLOCK || blk == MKFS_ROOT_TREE
+ || blk == MKFS_CHUNK_TREE)
+ continue;
+
+ btrfs_set_root_bytenr(&root_item, cfg->blocks[blk]);
+ btrfs_set_disk_key_objectid(&disk_key,
+ reference_root_table[blk]);
+ btrfs_set_item_key(buf, &disk_key, nritems);
+ btrfs_set_item_offset(buf, btrfs_item_nr(nritems), itemoff);
+ btrfs_set_item_size(buf, btrfs_item_nr(nritems),
+ sizeof(root_item));
+ write_extent_buffer(buf, &root_item,
+ btrfs_item_ptr_offset(buf, nritems),
+ sizeof(root_item));
+ nritems++;
+ itemoff -= sizeof(root_item);
+ }
+
+ /* generate checksum */
+ csum_tree_block_size(buf, BTRFS_CRC32_SIZE, 0);
+
+ /* write back root tree */
+ ret = pwrite(fd, buf->data, cfg->nodesize, cfg->blocks[MKFS_ROOT_TREE]);
+ if (ret != cfg->nodesize)
+ return (ret < 0 ? -errno : -EIO);
+
+ return ret;
+}
+
/*
* @fs_uuid - if NULL, generates a UUID, returns back the new filesystem UUID
*
* The superblock signature is not valid, denotes a partially created
* filesystem, needs to be finalized.
+ *
+ * The temporary fs will have the following chunk layout:
+ * Device extent:
+ * 0 1M 5M ......
+ * | Reserved | dev extent for SYS chunk |
+ *
+ * And chunk mapping will be:
+ * Chunk mapping:
+ * 0 1M 5M
+ * | | System chunk, 1:1 mapped |
+ *
+ * That's to say, there will only be *ONE* system chunk, mapped to
+ * [1M, 5M) physical offset.
+ * And the only chunk is also in logical address [1M, 5M), containing
+ * all essential tree blocks.
*/
int make_btrfs(int fd, struct btrfs_mkfs_config *cfg)
{
struct btrfs_super_block super;
struct extent_buffer *buf;
- struct btrfs_root_item root_item;
struct btrfs_disk_key disk_key;
struct btrfs_extent_item *extent_item;
- struct btrfs_inode_item *inode_item;
struct btrfs_chunk *chunk;
struct btrfs_dev_item *dev_item;
struct btrfs_dev_extent *dev_extent;
uuid_generate(super.dev_item.uuid);
uuid_generate(chunk_tree_uuid);
- cfg->blocks[0] = BTRFS_SUPER_INFO_OFFSET;
- for (i = 1; i < 7; i++) {
- cfg->blocks[i] = BTRFS_SUPER_INFO_OFFSET + 1024 * 1024 +
- cfg->nodesize * i;
+ cfg->blocks[MKFS_SUPER_BLOCK] = BTRFS_SUPER_INFO_OFFSET;
+ for (i = 1; i < MKFS_BLOCK_COUNT; i++) {
+ cfg->blocks[i] = BTRFS_BLOCK_RESERVED_1M_FOR_SUPER +
+ cfg->nodesize * (i - 1);
}
- btrfs_set_super_bytenr(&super, cfg->blocks[0]);
+ btrfs_set_super_bytenr(&super, cfg->blocks[MKFS_SUPER_BLOCK]);
btrfs_set_super_num_devices(&super, 1);
btrfs_set_super_magic(&super, BTRFS_MAGIC_PARTIAL);
btrfs_set_super_generation(&super, 1);
- btrfs_set_super_root(&super, cfg->blocks[1]);
- btrfs_set_super_chunk_root(&super, cfg->blocks[3]);
+ btrfs_set_super_root(&super, cfg->blocks[MKFS_ROOT_TREE]);
+ btrfs_set_super_chunk_root(&super, cfg->blocks[MKFS_CHUNK_TREE]);
btrfs_set_super_total_bytes(&super, num_bytes);
btrfs_set_super_bytes_used(&super, 6 * cfg->nodesize);
btrfs_set_super_sectorsize(&super, cfg->sectorsize);
- btrfs_set_super_leafsize(&super, cfg->nodesize);
+ super.__unused_leafsize = cpu_to_le32(cfg->nodesize);
btrfs_set_super_nodesize(&super, cfg->nodesize);
btrfs_set_super_stripesize(&super, cfg->stripesize);
btrfs_set_super_csum_type(&super, BTRFS_CSUM_TYPE_CRC32);
/* create the tree of root objects */
memset(buf->data, 0, cfg->nodesize);
buf->len = cfg->nodesize;
- btrfs_set_header_bytenr(buf, cfg->blocks[1]);
+ btrfs_set_header_bytenr(buf, cfg->blocks[MKFS_ROOT_TREE]);
btrfs_set_header_nritems(buf, 4);
btrfs_set_header_generation(buf, 1);
btrfs_set_header_backref_rev(buf, BTRFS_MIXED_BACKREF_REV);
btrfs_header_chunk_tree_uuid(buf),
BTRFS_UUID_SIZE);
- /* create the items for the root tree */
- memset(&root_item, 0, sizeof(root_item));
- inode_item = &root_item.inode;
- btrfs_set_stack_inode_generation(inode_item, 1);
- btrfs_set_stack_inode_size(inode_item, 3);
- btrfs_set_stack_inode_nlink(inode_item, 1);
- btrfs_set_stack_inode_nbytes(inode_item, cfg->nodesize);
- btrfs_set_stack_inode_mode(inode_item, S_IFDIR | 0755);
- btrfs_set_root_refs(&root_item, 1);
- btrfs_set_root_used(&root_item, cfg->nodesize);
- btrfs_set_root_generation(&root_item, 1);
-
- memset(&disk_key, 0, sizeof(disk_key));
- btrfs_set_disk_key_type(&disk_key, BTRFS_ROOT_ITEM_KEY);
- btrfs_set_disk_key_offset(&disk_key, 0);
- nritems = 0;
-
- itemoff = __BTRFS_LEAF_DATA_SIZE(cfg->nodesize) - sizeof(root_item);
- btrfs_set_root_bytenr(&root_item, cfg->blocks[2]);
- btrfs_set_disk_key_objectid(&disk_key, BTRFS_EXTENT_TREE_OBJECTID);
- btrfs_set_item_key(buf, &disk_key, nritems);
- btrfs_set_item_offset(buf, btrfs_item_nr(nritems), itemoff);
- btrfs_set_item_size(buf, btrfs_item_nr(nritems),
- sizeof(root_item));
- write_extent_buffer(buf, &root_item, btrfs_item_ptr_offset(buf,
- nritems), sizeof(root_item));
- nritems++;
-
- itemoff = itemoff - sizeof(root_item);
- btrfs_set_root_bytenr(&root_item, cfg->blocks[4]);
- btrfs_set_disk_key_objectid(&disk_key, BTRFS_DEV_TREE_OBJECTID);
- btrfs_set_item_key(buf, &disk_key, nritems);
- btrfs_set_item_offset(buf, btrfs_item_nr(nritems), itemoff);
- btrfs_set_item_size(buf, btrfs_item_nr(nritems),
- sizeof(root_item));
- write_extent_buffer(buf, &root_item,
- btrfs_item_ptr_offset(buf, nritems),
- sizeof(root_item));
- nritems++;
-
- itemoff = itemoff - sizeof(root_item);
- btrfs_set_root_bytenr(&root_item, cfg->blocks[5]);
- btrfs_set_disk_key_objectid(&disk_key, BTRFS_FS_TREE_OBJECTID);
- btrfs_set_item_key(buf, &disk_key, nritems);
- btrfs_set_item_offset(buf, btrfs_item_nr(nritems), itemoff);
- btrfs_set_item_size(buf, btrfs_item_nr(nritems),
- sizeof(root_item));
- write_extent_buffer(buf, &root_item,
- btrfs_item_ptr_offset(buf, nritems),
- sizeof(root_item));
- nritems++;
-
- itemoff = itemoff - sizeof(root_item);
- btrfs_set_root_bytenr(&root_item, cfg->blocks[6]);
- btrfs_set_disk_key_objectid(&disk_key, BTRFS_CSUM_TREE_OBJECTID);
- btrfs_set_item_key(buf, &disk_key, nritems);
- btrfs_set_item_offset(buf, btrfs_item_nr(nritems), itemoff);
- btrfs_set_item_size(buf, btrfs_item_nr(nritems),
- sizeof(root_item));
- write_extent_buffer(buf, &root_item,
- btrfs_item_ptr_offset(buf, nritems),
- sizeof(root_item));
- nritems++;
-
-
- csum_tree_block_size(buf, BTRFS_CRC32_SIZE, 0);
- ret = pwrite(fd, buf->data, cfg->nodesize, cfg->blocks[1]);
- if (ret != cfg->nodesize) {
- ret = (ret < 0 ? -errno : -EIO);
+ ret = btrfs_create_tree_root(fd, cfg, buf);
+ if (ret < 0)
goto out;
- }
/* create the items for the extent tree */
memset(buf->data + sizeof(struct btrfs_header), 0,
cfg->nodesize - sizeof(struct btrfs_header));
nritems = 0;
itemoff = __BTRFS_LEAF_DATA_SIZE(cfg->nodesize);
- for (i = 1; i < 7; i++) {
+ for (i = 1; i < MKFS_BLOCK_COUNT; i++) {
item_size = sizeof(struct btrfs_extent_item);
if (!skinny_metadata)
item_size += sizeof(struct btrfs_tree_block_info);
btrfs_set_item_size(buf, btrfs_item_nr(nritems), 0);
nritems++;
}
- btrfs_set_header_bytenr(buf, cfg->blocks[2]);
+ btrfs_set_header_bytenr(buf, cfg->blocks[MKFS_EXTENT_TREE]);
btrfs_set_header_owner(buf, BTRFS_EXTENT_TREE_OBJECTID);
btrfs_set_header_nritems(buf, nritems);
csum_tree_block_size(buf, BTRFS_CRC32_SIZE, 0);
- ret = pwrite(fd, buf->data, cfg->nodesize, cfg->blocks[2]);
+ ret = pwrite(fd, buf->data, cfg->nodesize, cfg->blocks[MKFS_EXTENT_TREE]);
if (ret != cfg->nodesize) {
ret = (ret < 0 ? -errno : -EIO);
goto out;
/* then we have chunk 0 */
btrfs_set_disk_key_objectid(&disk_key, BTRFS_FIRST_CHUNK_TREE_OBJECTID);
- btrfs_set_disk_key_offset(&disk_key, 0);
+ btrfs_set_disk_key_offset(&disk_key, BTRFS_BLOCK_RESERVED_1M_FOR_SUPER);
btrfs_set_disk_key_type(&disk_key, BTRFS_CHUNK_ITEM_KEY);
btrfs_set_item_key(buf, &disk_key, nritems);
btrfs_set_item_offset(buf, btrfs_item_nr(nritems), itemoff);
btrfs_set_chunk_sector_size(buf, chunk, cfg->sectorsize);
btrfs_set_chunk_num_stripes(buf, chunk, 1);
btrfs_set_stripe_devid_nr(buf, chunk, 0, 1);
- btrfs_set_stripe_offset_nr(buf, chunk, 0, 0);
+ btrfs_set_stripe_offset_nr(buf, chunk, 0,
+ BTRFS_BLOCK_RESERVED_1M_FOR_SUPER);
nritems++;
write_extent_buffer(buf, super.dev_item.uuid,
ptr += item_size;
btrfs_set_super_sys_array_size(&super, array_size);
- btrfs_set_header_bytenr(buf, cfg->blocks[3]);
+ btrfs_set_header_bytenr(buf, cfg->blocks[MKFS_CHUNK_TREE]);
btrfs_set_header_owner(buf, BTRFS_CHUNK_TREE_OBJECTID);
btrfs_set_header_nritems(buf, nritems);
csum_tree_block_size(buf, BTRFS_CRC32_SIZE, 0);
- ret = pwrite(fd, buf->data, cfg->nodesize, cfg->blocks[3]);
+ ret = pwrite(fd, buf->data, cfg->nodesize, cfg->blocks[MKFS_CHUNK_TREE]);
if (ret != cfg->nodesize) {
ret = (ret < 0 ? -errno : -EIO);
goto out;
sizeof(struct btrfs_dev_extent);
btrfs_set_disk_key_objectid(&disk_key, 1);
- btrfs_set_disk_key_offset(&disk_key, 0);
+ btrfs_set_disk_key_offset(&disk_key, BTRFS_BLOCK_RESERVED_1M_FOR_SUPER);
btrfs_set_disk_key_type(&disk_key, BTRFS_DEV_EXTENT_KEY);
btrfs_set_item_key(buf, &disk_key, nritems);
btrfs_set_item_offset(buf, btrfs_item_nr(nritems), itemoff);
BTRFS_CHUNK_TREE_OBJECTID);
btrfs_set_dev_extent_chunk_objectid(buf, dev_extent,
BTRFS_FIRST_CHUNK_TREE_OBJECTID);
- btrfs_set_dev_extent_chunk_offset(buf, dev_extent, 0);
+ btrfs_set_dev_extent_chunk_offset(buf, dev_extent,
+ BTRFS_BLOCK_RESERVED_1M_FOR_SUPER);
write_extent_buffer(buf, chunk_tree_uuid,
(unsigned long)btrfs_dev_extent_chunk_tree_uuid(dev_extent),
BTRFS_MKFS_SYSTEM_GROUP_SIZE);
nritems++;
- btrfs_set_header_bytenr(buf, cfg->blocks[4]);
+ btrfs_set_header_bytenr(buf, cfg->blocks[MKFS_DEV_TREE]);
btrfs_set_header_owner(buf, BTRFS_DEV_TREE_OBJECTID);
btrfs_set_header_nritems(buf, nritems);
csum_tree_block_size(buf, BTRFS_CRC32_SIZE, 0);
- ret = pwrite(fd, buf->data, cfg->nodesize, cfg->blocks[4]);
+ ret = pwrite(fd, buf->data, cfg->nodesize, cfg->blocks[MKFS_DEV_TREE]);
if (ret != cfg->nodesize) {
ret = (ret < 0 ? -errno : -EIO);
goto out;
/* create the FS root */
memset(buf->data + sizeof(struct btrfs_header), 0,
cfg->nodesize - sizeof(struct btrfs_header));
- btrfs_set_header_bytenr(buf, cfg->blocks[5]);
+ btrfs_set_header_bytenr(buf, cfg->blocks[MKFS_FS_TREE]);
btrfs_set_header_owner(buf, BTRFS_FS_TREE_OBJECTID);
btrfs_set_header_nritems(buf, 0);
csum_tree_block_size(buf, BTRFS_CRC32_SIZE, 0);
- ret = pwrite(fd, buf->data, cfg->nodesize, cfg->blocks[5]);
+ ret = pwrite(fd, buf->data, cfg->nodesize, cfg->blocks[MKFS_FS_TREE]);
if (ret != cfg->nodesize) {
ret = (ret < 0 ? -errno : -EIO);
goto out;
/* finally create the csum root */
memset(buf->data + sizeof(struct btrfs_header), 0,
cfg->nodesize - sizeof(struct btrfs_header));
- btrfs_set_header_bytenr(buf, cfg->blocks[6]);
+ btrfs_set_header_bytenr(buf, cfg->blocks[MKFS_CSUM_TREE]);
btrfs_set_header_owner(buf, BTRFS_CSUM_TREE_OBJECTID);
btrfs_set_header_nritems(buf, 0);
csum_tree_block_size(buf, BTRFS_CRC32_SIZE, 0);
- ret = pwrite(fd, buf->data, cfg->nodesize, cfg->blocks[6]);
+ ret = pwrite(fd, buf->data, cfg->nodesize, cfg->blocks[MKFS_CSUM_TREE]);
if (ret != cfg->nodesize) {
ret = (ret < 0 ? -errno : -EIO);
goto out;
memcpy(buf->data, &super, sizeof(super));
buf->len = BTRFS_SUPER_INFO_SIZE;
csum_tree_block_size(buf, BTRFS_CRC32_SIZE, 0);
- ret = pwrite(fd, buf->data, BTRFS_SUPER_INFO_SIZE, cfg->blocks[0]);
+ ret = pwrite(fd, buf->data, BTRFS_SUPER_INFO_SIZE,
+ cfg->blocks[MKFS_SUPER_BLOCK]);
if (ret != BTRFS_SUPER_INFO_SIZE) {
ret = (ret < 0 ? -errno : -EIO);
goto out;
return ret;
}
-u64 btrfs_min_dev_size(u32 nodesize)
-{
- return 2 * (BTRFS_MKFS_SYSTEM_GROUP_SIZE +
- btrfs_min_global_blk_rsv_size(nodesize));
-}
-
/*
* Btrfs minimum size calculation is complicated, it should include at least:
* 1. system group size
* To avoid the overkill calculation, (system group + global block rsv) * 2
* for *EACH* device should be good enough.
*/
-u64 btrfs_min_global_blk_rsv_size(u32 nodesize)
+static u64 btrfs_min_global_blk_rsv_size(u32 nodesize)
{
return (u64)nodesize << 10;
}
+u64 btrfs_min_dev_size(u32 nodesize, int mixed, u64 meta_profile,
+ u64 data_profile)
+{
+ u64 reserved = 0;
+ u64 meta_size;
+ u64 data_size;
+
+ if (mixed)
+ return 2 * (BTRFS_MKFS_SYSTEM_GROUP_SIZE +
+ btrfs_min_global_blk_rsv_size(nodesize));
+
+ /*
+ * Minimal size calculation is complex due to several factors:
+ * 0) Reserved 1M range.
+ *
+ * 1) Temporary chunk reuse
+ * If specified chunk profile is SINGLE, we can reuse
+ * temporary chunks, no need to allocate new chunks.
+ *
+ * 2) Different minimal chunk size for different profiles:
+ * For initial sys chunk, chunk size is fixed to 4M.
+ * For single profile, minimal chunk size is 8M for all.
+ * For other profiles, minimal chunk and stripe size ranges from 8M
+ * to 64M.
+ *
+ * To calculate it a little easier, here we assume we don't reuse any
+ * temporary chunk, and calculate the size completely by ourselves.
+ *
+ * Temporary chunks sizes are always fixed:
+ * One initial sys chunk, one SINGLE meta, and one SINGLE data.
+ * The latter two are all 8M, accroding to @calc_size of
+ * btrfs_alloc_chunk().
+ */
+ reserved += BTRFS_BLOCK_RESERVED_1M_FOR_SUPER +
+ BTRFS_MKFS_SYSTEM_GROUP_SIZE + SZ_8M * 2;
+
+ /*
+ * For real chunks, we need to select different sizes:
+ * For SINGLE, it's still fixed to 8M (@calc_size).
+ * For other profiles, refer to max(@min_stripe_size, @calc_size).
+ *
+ * And use the stripe size to calculate its physical used space.
+ */
+ if (meta_profile & BTRFS_BLOCK_GROUP_PROFILE_MASK)
+ meta_size = SZ_8M + SZ_32M;
+ else
+ meta_size = SZ_8M + SZ_8M;
+ /* For DUP/metadata, 2 stripes on one disk */
+ if (meta_profile & BTRFS_BLOCK_GROUP_DUP)
+ meta_size *= 2;
+ reserved += meta_size;
+
+ if (data_profile & BTRFS_BLOCK_GROUP_PROFILE_MASK)
+ data_size = SZ_64M;
+ else
+ data_size = SZ_8M;
+ /* For DUP/data, 2 stripes on one disk */
+ if (data_profile & BTRFS_BLOCK_GROUP_DUP)
+ data_size *= 2;
+ reserved += data_size;
+
+ return reserved;
+}
+
#define isoctal(c) (((c) & ~7) == '0')
static inline void translate(char *f, char *t)
error("%s is a swap device", file);
return 1;
}
+ ret = test_status_for_mkfs(file, force_overwrite);
+ if (ret)
+ return 1;
+ /* check if the device is busy */
+ fd = open(file, O_RDWR|O_EXCL);
+ if (fd < 0) {
+ error("unable to open %s: %m", file);
+ return 1;
+ }
+ if (fstat(fd, &st)) {
+ error("unable to stat %s: %m", file);
+ close(fd);
+ return 1;
+ }
+ if (!S_ISBLK(st.st_mode)) {
+ error("%s is not a block device", file);
+ close(fd);
+ return 1;
+ }
+ close(fd);
+ return 0;
+}
+
+/*
+ * check if the file (device) is formatted or mounted
+ */
+int test_status_for_mkfs(const char *file, bool force_overwrite)
+{
+ int ret;
+
if (!force_overwrite) {
if (check_overwrite(file)) {
error("use the -f option to force overwrite of %s",
error("%s is mounted", file);
return 1;
}
- /* check if the device is busy */
- fd = open(file, O_RDWR|O_EXCL);
- if (fd < 0) {
- error("unable to open %s: %s", file, strerror(errno));
- return 1;
- }
- if (fstat(fd, &st)) {
- error("unable to stat %s: %s", file, strerror(errno));
- close(fd);
- return 1;
- }
- if (!S_ISBLK(st.st_mode)) {
- error("%s is not a block device", file);
- close(fd);
- return 1;
- }
- close(fd);
+
return 0;
}
}
}
-int test_minimum_size(const char *file, u32 nodesize)
+int test_minimum_size(const char *file, u64 min_dev_size)
{
int fd;
struct stat statbuf;
close(fd);
return -errno;
}
- if (btrfs_device_size(fd, &statbuf) < btrfs_min_dev_size(nodesize)) {
+ if (btrfs_device_size(fd, &statbuf) < min_dev_size) {
close(fd);
return 1;
}