2007-06-12 21:07:11 +08:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2007 Oracle. All rights reserved.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public
|
|
|
|
* License v2 as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public
|
|
|
|
* License along with this program; if not, write to the
|
|
|
|
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
|
|
* Boston, MA 021110-1307, USA.
|
|
|
|
*/
|
|
|
|
|
2007-02-21 05:41:09 +08:00
|
|
|
#define _XOPEN_SOURCE 500
|
2008-04-01 23:08:13 +08:00
|
|
|
#define _GNU_SOURCE
|
2008-04-29 04:44:22 +08:00
|
|
|
|
|
|
|
#ifndef __CHECKER__
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/mount.h>
|
|
|
|
#include "ioctl.h"
|
|
|
|
#endif
|
|
|
|
|
2007-02-21 05:41:09 +08:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
2008-04-01 23:08:13 +08:00
|
|
|
#include <getopt.h>
|
2007-04-06 02:29:12 +08:00
|
|
|
#include <uuid/uuid.h>
|
2007-06-12 23:39:09 +08:00
|
|
|
#include <linux/fs.h>
|
2007-12-22 05:25:35 +08:00
|
|
|
#include <ctype.h>
|
2007-06-12 23:39:09 +08:00
|
|
|
#include "kerncompat.h"
|
2007-02-21 05:41:09 +08:00
|
|
|
#include "ctree.h"
|
|
|
|
#include "disk-io.h"
|
2008-03-25 03:03:18 +08:00
|
|
|
#include "volumes.h"
|
2007-03-21 23:13:29 +08:00
|
|
|
#include "transaction.h"
|
2008-01-04 23:38:22 +08:00
|
|
|
#include "utils.h"
|
2008-07-25 00:13:30 +08:00
|
|
|
#include "version.h"
|
2007-02-21 05:41:09 +08:00
|
|
|
|
2007-12-22 05:25:35 +08:00
|
|
|
static u64 parse_size(char *s)
|
|
|
|
{
|
|
|
|
int len = strlen(s);
|
|
|
|
char c;
|
|
|
|
u64 mult = 1;
|
|
|
|
|
|
|
|
if (!isdigit(s[len - 1])) {
|
|
|
|
c = tolower(s[len - 1]);
|
|
|
|
switch (c) {
|
|
|
|
case 'g':
|
|
|
|
mult *= 1024;
|
|
|
|
case 'm':
|
|
|
|
mult *= 1024;
|
|
|
|
case 'k':
|
|
|
|
mult *= 1024;
|
|
|
|
case 'b':
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
fprintf(stderr, "Unknown size descriptor %c\n", c);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
s[len - 1] = '\0';
|
|
|
|
}
|
|
|
|
return atol(s) * mult;
|
|
|
|
}
|
|
|
|
|
2008-09-24 00:29:10 +08:00
|
|
|
static int make_root_dir(struct btrfs_root *root)
|
|
|
|
{
|
2007-04-07 03:39:12 +08:00
|
|
|
struct btrfs_trans_handle *trans;
|
|
|
|
struct btrfs_key location;
|
2008-03-25 03:03:18 +08:00
|
|
|
u64 bytes_used;
|
|
|
|
u64 chunk_start = 0;
|
|
|
|
u64 chunk_size = 0;
|
2008-01-04 23:38:22 +08:00
|
|
|
int ret;
|
2007-04-07 03:39:12 +08:00
|
|
|
|
|
|
|
trans = btrfs_start_transaction(root, 1);
|
2008-03-25 03:03:18 +08:00
|
|
|
bytes_used = btrfs_super_bytes_used(&root->fs_info->super_copy);
|
|
|
|
|
2008-04-23 02:06:56 +08:00
|
|
|
root->fs_info->system_allocs = 1;
|
2008-03-25 03:03:18 +08:00
|
|
|
ret = btrfs_make_block_group(trans, root, bytes_used,
|
|
|
|
BTRFS_BLOCK_GROUP_SYSTEM,
|
2008-04-16 03:42:08 +08:00
|
|
|
BTRFS_FIRST_CHUNK_TREE_OBJECTID,
|
2008-03-25 03:03:18 +08:00
|
|
|
0, BTRFS_MKFS_SYSTEM_GROUP_SIZE);
|
|
|
|
BUG_ON(ret);
|
2008-04-16 03:42:08 +08:00
|
|
|
|
2008-03-25 03:03:18 +08:00
|
|
|
ret = btrfs_alloc_chunk(trans, root->fs_info->extent_root,
|
|
|
|
&chunk_start, &chunk_size,
|
|
|
|
BTRFS_BLOCK_GROUP_METADATA);
|
|
|
|
BUG_ON(ret);
|
|
|
|
ret = btrfs_make_block_group(trans, root, 0,
|
|
|
|
BTRFS_BLOCK_GROUP_METADATA,
|
2008-04-16 03:42:08 +08:00
|
|
|
BTRFS_FIRST_CHUNK_TREE_OBJECTID,
|
2008-03-25 03:03:18 +08:00
|
|
|
chunk_start, chunk_size);
|
|
|
|
BUG_ON(ret);
|
|
|
|
|
2008-04-23 02:06:56 +08:00
|
|
|
root->fs_info->system_allocs = 0;
|
2008-03-25 03:03:18 +08:00
|
|
|
btrfs_commit_transaction(trans, root);
|
|
|
|
trans = btrfs_start_transaction(root, 1);
|
|
|
|
BUG_ON(!trans);
|
|
|
|
|
|
|
|
ret = btrfs_alloc_chunk(trans, root->fs_info->extent_root,
|
|
|
|
&chunk_start, &chunk_size,
|
|
|
|
BTRFS_BLOCK_GROUP_DATA);
|
|
|
|
BUG_ON(ret);
|
|
|
|
ret = btrfs_make_block_group(trans, root, 0,
|
|
|
|
BTRFS_BLOCK_GROUP_DATA,
|
2008-04-16 03:42:08 +08:00
|
|
|
BTRFS_FIRST_CHUNK_TREE_OBJECTID,
|
2008-03-25 03:03:18 +08:00
|
|
|
chunk_start, chunk_size);
|
|
|
|
BUG_ON(ret);
|
|
|
|
|
2008-01-04 23:38:22 +08:00
|
|
|
ret = btrfs_make_root_dir(trans, root->fs_info->tree_root,
|
2007-04-11 20:58:53 +08:00
|
|
|
BTRFS_ROOT_TREE_DIR_OBJECTID);
|
2007-03-21 23:13:29 +08:00
|
|
|
if (ret)
|
2007-04-07 03:39:12 +08:00
|
|
|
goto err;
|
2008-01-04 23:38:22 +08:00
|
|
|
ret = btrfs_make_root_dir(trans, root, BTRFS_FIRST_FREE_OBJECTID);
|
2007-04-07 03:39:12 +08:00
|
|
|
if (ret)
|
|
|
|
goto err;
|
|
|
|
memcpy(&location, &root->fs_info->fs_root->root_key, sizeof(location));
|
|
|
|
location.offset = (u64)-1;
|
|
|
|
ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
|
2007-08-30 03:56:44 +08:00
|
|
|
"default", 7,
|
2008-01-04 23:38:22 +08:00
|
|
|
btrfs_super_root_dir(&root->fs_info->super_copy),
|
2008-12-18 05:10:07 +08:00
|
|
|
&location, BTRFS_FT_DIR, 0);
|
2007-04-07 03:39:12 +08:00
|
|
|
if (ret)
|
|
|
|
goto err;
|
2007-12-13 03:39:46 +08:00
|
|
|
|
|
|
|
ret = btrfs_insert_inode_ref(trans, root->fs_info->tree_root,
|
|
|
|
"default", 7, location.objectid,
|
2008-07-25 00:13:32 +08:00
|
|
|
BTRFS_ROOT_TREE_DIR_OBJECTID, 0);
|
2007-12-13 03:39:46 +08:00
|
|
|
if (ret)
|
|
|
|
goto err;
|
|
|
|
|
2008-01-04 23:38:22 +08:00
|
|
|
btrfs_commit_transaction(trans, root);
|
2007-04-07 03:39:12 +08:00
|
|
|
err:
|
2007-03-21 23:13:29 +08:00
|
|
|
return ret;
|
|
|
|
}
|
2007-03-21 08:35:03 +08:00
|
|
|
|
2008-04-05 03:42:17 +08:00
|
|
|
static int recow_roots(struct btrfs_trans_handle *trans,
|
|
|
|
struct btrfs_root *root)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
struct extent_buffer *tmp;
|
|
|
|
struct btrfs_fs_info *info = root->fs_info;
|
|
|
|
|
|
|
|
ret = __btrfs_cow_block(trans, info->fs_root, info->fs_root->node,
|
|
|
|
NULL, 0, &tmp, 0, 0);
|
|
|
|
BUG_ON(ret);
|
|
|
|
free_extent_buffer(tmp);
|
|
|
|
|
|
|
|
ret = __btrfs_cow_block(trans, info->tree_root, info->tree_root->node,
|
|
|
|
NULL, 0, &tmp, 0, 0);
|
|
|
|
BUG_ON(ret);
|
|
|
|
free_extent_buffer(tmp);
|
|
|
|
|
|
|
|
ret = __btrfs_cow_block(trans, info->extent_root,
|
|
|
|
info->extent_root->node, NULL, 0, &tmp, 0, 0);
|
|
|
|
BUG_ON(ret);
|
|
|
|
free_extent_buffer(tmp);
|
|
|
|
|
|
|
|
ret = __btrfs_cow_block(trans, info->chunk_root, info->chunk_root->node,
|
|
|
|
NULL, 0, &tmp, 0, 0);
|
|
|
|
BUG_ON(ret);
|
|
|
|
free_extent_buffer(tmp);
|
|
|
|
|
|
|
|
|
|
|
|
ret = __btrfs_cow_block(trans, info->dev_root, info->dev_root->node,
|
|
|
|
NULL, 0, &tmp, 0, 0);
|
|
|
|
BUG_ON(ret);
|
|
|
|
free_extent_buffer(tmp);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-04-04 04:35:48 +08:00
|
|
|
static int create_one_raid_group(struct btrfs_trans_handle *trans,
|
|
|
|
struct btrfs_root *root, u64 type)
|
|
|
|
{
|
|
|
|
u64 chunk_start;
|
|
|
|
u64 chunk_size;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = btrfs_alloc_chunk(trans, root->fs_info->extent_root,
|
|
|
|
&chunk_start, &chunk_size, type);
|
|
|
|
BUG_ON(ret);
|
|
|
|
ret = btrfs_make_block_group(trans, root->fs_info->extent_root, 0,
|
2008-04-16 03:42:08 +08:00
|
|
|
type, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
|
2008-04-04 04:35:48 +08:00
|
|
|
chunk_start, chunk_size);
|
|
|
|
BUG_ON(ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int create_raid_groups(struct btrfs_trans_handle *trans,
|
|
|
|
struct btrfs_root *root, u64 data_profile,
|
|
|
|
u64 metadata_profile)
|
|
|
|
{
|
|
|
|
u64 num_devices = btrfs_super_num_devices(&root->fs_info->super_copy);
|
|
|
|
u64 allowed;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (num_devices == 1)
|
|
|
|
allowed = BTRFS_BLOCK_GROUP_DUP;
|
2008-04-16 23:14:21 +08:00
|
|
|
else if (num_devices >= 4) {
|
|
|
|
allowed = BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1 |
|
|
|
|
BTRFS_BLOCK_GROUP_RAID10;
|
|
|
|
} else
|
2008-04-04 04:35:48 +08:00
|
|
|
allowed = BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1;
|
|
|
|
|
2008-04-04 04:35:48 +08:00
|
|
|
if (allowed & metadata_profile) {
|
2008-04-05 03:42:17 +08:00
|
|
|
ret = create_one_raid_group(trans, root,
|
|
|
|
BTRFS_BLOCK_GROUP_SYSTEM |
|
|
|
|
(allowed & metadata_profile));
|
|
|
|
BUG_ON(ret);
|
|
|
|
|
2008-04-04 04:35:48 +08:00
|
|
|
ret = create_one_raid_group(trans, root,
|
|
|
|
BTRFS_BLOCK_GROUP_METADATA |
|
|
|
|
(allowed & metadata_profile));
|
|
|
|
BUG_ON(ret);
|
2008-04-05 03:42:17 +08:00
|
|
|
|
|
|
|
ret = recow_roots(trans, root);
|
|
|
|
BUG_ON(ret);
|
2008-04-04 04:35:48 +08:00
|
|
|
}
|
|
|
|
if (num_devices > 1 && (allowed & data_profile)) {
|
2008-04-04 04:35:48 +08:00
|
|
|
ret = create_one_raid_group(trans, root,
|
|
|
|
BTRFS_BLOCK_GROUP_DATA |
|
|
|
|
(allowed & data_profile));
|
|
|
|
BUG_ON(ret);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-09-26 22:26:53 +08:00
|
|
|
static int create_data_reloc_tree(struct btrfs_trans_handle *trans,
|
|
|
|
struct btrfs_root *root)
|
|
|
|
{
|
|
|
|
struct btrfs_key location;
|
|
|
|
struct btrfs_root_item root_item;
|
|
|
|
struct extent_buffer *tmp;
|
|
|
|
u64 objectid = BTRFS_DATA_RELOC_TREE_OBJECTID;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = btrfs_copy_root(trans, root, root->node, &tmp, objectid);
|
|
|
|
BUG_ON(ret);
|
|
|
|
|
|
|
|
memcpy(&root_item, &root->root_item, sizeof(root_item));
|
|
|
|
btrfs_set_root_bytenr(&root_item, tmp->start);
|
|
|
|
btrfs_set_root_level(&root_item, btrfs_header_level(tmp));
|
2008-10-30 02:07:47 +08:00
|
|
|
btrfs_set_root_generation(&root_item, trans->transid);
|
2008-09-26 22:26:53 +08:00
|
|
|
free_extent_buffer(tmp);
|
|
|
|
|
|
|
|
location.objectid = objectid;
|
|
|
|
location.type = BTRFS_ROOT_ITEM_KEY;
|
|
|
|
location.offset = trans->transid;
|
|
|
|
ret = btrfs_insert_root(trans, root->fs_info->tree_root,
|
|
|
|
&location, &root_item);
|
|
|
|
BUG_ON(ret);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-10-16 04:25:14 +08:00
|
|
|
static void print_usage(void)
|
|
|
|
{
|
2008-04-01 23:08:13 +08:00
|
|
|
fprintf(stderr, "usage: mkfs.btrfs [options] dev [ dev ... ]\n");
|
|
|
|
fprintf(stderr, "options:\n");
|
|
|
|
fprintf(stderr, "\t -b --byte-count total number of bytes in the FS\n");
|
|
|
|
fprintf(stderr, "\t -l --leafsize size of btree leaves\n");
|
|
|
|
fprintf(stderr, "\t -n --nodesize size of btree leaves\n");
|
|
|
|
fprintf(stderr, "\t -s --sectorsize min block allocation\n");
|
2008-07-25 00:13:30 +08:00
|
|
|
fprintf(stderr, "%s\n", BTRFS_BUILD_VERSION);
|
2007-10-16 04:25:14 +08:00
|
|
|
exit(1);
|
|
|
|
}
|
2007-03-21 08:35:03 +08:00
|
|
|
|
2008-04-04 04:35:48 +08:00
|
|
|
static u64 parse_profile(char *s)
|
|
|
|
{
|
|
|
|
if (strcmp(s, "raid0") == 0) {
|
|
|
|
return BTRFS_BLOCK_GROUP_RAID0;
|
|
|
|
} else if (strcmp(s, "raid1") == 0) {
|
|
|
|
return BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_DUP;
|
2008-04-16 23:14:21 +08:00
|
|
|
} else if (strcmp(s, "raid10") == 0) {
|
|
|
|
return BTRFS_BLOCK_GROUP_RAID10 | BTRFS_BLOCK_GROUP_DUP;
|
2008-04-04 04:35:48 +08:00
|
|
|
} else if (strcmp(s, "single") == 0) {
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
fprintf(stderr, "Unknown option %s\n", s);
|
|
|
|
print_usage();
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-04-18 22:31:42 +08:00
|
|
|
static char *parse_label(char *input)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int len = strlen(input);
|
|
|
|
|
|
|
|
if (len > BTRFS_LABEL_SIZE) {
|
|
|
|
fprintf(stderr, "Label %s is too long (max %d)\n", input,
|
|
|
|
BTRFS_LABEL_SIZE);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
for (i = 0; i < len; i++) {
|
|
|
|
if (input[i] == '/' || input[i] == '\\') {
|
|
|
|
fprintf(stderr, "invalid label %s\n", input);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return strdup(input);
|
|
|
|
}
|
|
|
|
|
2008-04-01 23:08:13 +08:00
|
|
|
static struct option long_options[] = {
|
2008-04-26 04:55:21 +08:00
|
|
|
{ "alloc-start", 1, NULL, 'A'},
|
2008-04-01 23:08:13 +08:00
|
|
|
{ "byte-count", 1, NULL, 'b' },
|
|
|
|
{ "leafsize", 1, NULL, 'l' },
|
2008-04-18 22:31:42 +08:00
|
|
|
{ "label", 1, NULL, 'L'},
|
|
|
|
{ "metadata", 1, NULL, 'm' },
|
2008-04-01 23:08:13 +08:00
|
|
|
{ "nodesize", 1, NULL, 'n' },
|
|
|
|
{ "sectorsize", 1, NULL, 's' },
|
2008-04-04 04:35:48 +08:00
|
|
|
{ "data", 1, NULL, 'd' },
|
2008-04-01 23:08:13 +08:00
|
|
|
{ 0, 0, 0, 0}
|
|
|
|
};
|
|
|
|
|
2007-03-21 08:35:03 +08:00
|
|
|
int main(int ac, char **av)
|
|
|
|
{
|
|
|
|
char *file;
|
2008-04-18 22:31:42 +08:00
|
|
|
struct btrfs_root *root;
|
|
|
|
struct btrfs_trans_handle *trans;
|
|
|
|
char *label = NULL;
|
2008-04-23 02:06:31 +08:00
|
|
|
char *first_file;
|
2007-03-21 08:35:03 +08:00
|
|
|
u64 block_count = 0;
|
2008-03-25 03:04:49 +08:00
|
|
|
u64 dev_block_count = 0;
|
Btrfs: move data checksumming into a dedicated tree
Btrfs stores checksums for each data block. Until now, they have
been stored in the subvolume trees, indexed by the inode that is
referencing the data block. This means that when we read the inode,
we've probably read in at least some checksums as well.
But, this has a few problems:
* The checksums are indexed by logical offset in the file. When
compression is on, this means we have to do the expensive checksumming
on the uncompressed data. It would be faster if we could checksum
the compressed data instead.
* If we implement encryption, we'll be checksumming the plain text and
storing that on disk. This is significantly less secure.
* For either compression or encryption, we have to get the plain text
back before we can verify the checksum as correct. This makes the raid
layer balancing and extent moving much more expensive.
* It makes the front end caching code more complex, as we have touch
the subvolume and inodes as we cache extents.
* There is potentitally one copy of the checksum in each subvolume
referencing an extent.
The solution used here is to store the extent checksums in a dedicated
tree. This allows us to index the checksums by phyiscal extent
start and length. It means:
* The checksum is against the data stored on disk, after any compression
or encryption is done.
* The checksum is stored in a central location, and can be verified without
following back references, or reading inodes.
This makes compression significantly faster by reducing the amount of
data that needs to be checksummed. It will also allow much faster
raid management code in general.
The checksums are indexed by a key with a fixed objectid (a magic value
in ctree.h) and offset set to the starting byte of the extent. This
allows us to copy the checksum items into the fsync log tree directly (or
any other tree), without having to invent a second format for them.
Signed-off-by: Chris Mason <chris.mason@oracle.com>
2008-12-09 06:00:31 +08:00
|
|
|
u64 blocks[7];
|
2008-05-01 22:22:47 +08:00
|
|
|
u64 alloc_start = 0;
|
2008-04-18 22:31:42 +08:00
|
|
|
u64 metadata_profile = BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_DUP;
|
|
|
|
u64 data_profile = BTRFS_BLOCK_GROUP_RAID0;
|
2008-04-10 07:49:01 +08:00
|
|
|
u32 leafsize = getpagesize();
|
2007-10-16 04:25:14 +08:00
|
|
|
u32 sectorsize = 4096;
|
2008-04-10 07:49:01 +08:00
|
|
|
u32 nodesize = leafsize;
|
2007-12-01 00:30:24 +08:00
|
|
|
u32 stripesize = 4096;
|
2008-03-25 03:04:49 +08:00
|
|
|
int zero_end = 1;
|
2008-04-01 23:08:13 +08:00
|
|
|
int option_index = 0;
|
2008-04-18 22:31:42 +08:00
|
|
|
int fd;
|
|
|
|
int first_fd;
|
|
|
|
int ret;
|
|
|
|
int i;
|
2007-03-21 23:13:29 +08:00
|
|
|
|
2007-10-16 04:25:14 +08:00
|
|
|
while(1) {
|
|
|
|
int c;
|
2008-04-26 04:55:21 +08:00
|
|
|
c = getopt_long(ac, av, "A:b:l:n:s:m:d:L:", long_options,
|
2008-04-01 23:08:13 +08:00
|
|
|
&option_index);
|
2007-10-16 04:25:14 +08:00
|
|
|
if (c < 0)
|
|
|
|
break;
|
|
|
|
switch(c) {
|
2008-04-26 04:55:21 +08:00
|
|
|
case 'A':
|
|
|
|
alloc_start = parse_size(optarg);
|
|
|
|
break;
|
2008-04-04 04:35:48 +08:00
|
|
|
case 'd':
|
|
|
|
data_profile = parse_profile(optarg);
|
|
|
|
break;
|
2007-10-16 04:25:14 +08:00
|
|
|
case 'l':
|
2007-12-22 05:25:35 +08:00
|
|
|
leafsize = parse_size(optarg);
|
2007-10-16 04:25:14 +08:00
|
|
|
break;
|
2008-04-18 22:31:42 +08:00
|
|
|
case 'L':
|
|
|
|
label = parse_label(optarg);
|
|
|
|
break;
|
|
|
|
case 'm':
|
|
|
|
metadata_profile = parse_profile(optarg);
|
2008-04-18 22:31:42 +08:00
|
|
|
break;
|
2007-10-16 04:25:14 +08:00
|
|
|
case 'n':
|
2007-12-22 05:25:35 +08:00
|
|
|
nodesize = parse_size(optarg);
|
2007-10-16 04:25:14 +08:00
|
|
|
break;
|
2007-12-01 00:30:24 +08:00
|
|
|
case 's':
|
2008-04-01 23:08:13 +08:00
|
|
|
sectorsize = parse_size(optarg);
|
2007-12-01 00:30:24 +08:00
|
|
|
break;
|
2008-03-25 03:04:49 +08:00
|
|
|
case 'b':
|
|
|
|
block_count = parse_size(optarg);
|
2009-01-08 03:57:12 +08:00
|
|
|
if (block_count < 256*1024*1024) {
|
|
|
|
fprintf(stderr, "File system size "
|
|
|
|
"%llu bytes is too small, "
|
|
|
|
"256M is required at least\n",
|
|
|
|
block_count);
|
|
|
|
exit(1);
|
|
|
|
}
|
2008-03-25 03:04:49 +08:00
|
|
|
zero_end = 0;
|
|
|
|
break;
|
2007-10-16 04:25:14 +08:00
|
|
|
default:
|
|
|
|
print_usage();
|
|
|
|
}
|
|
|
|
}
|
2008-04-01 23:08:13 +08:00
|
|
|
sectorsize = max(sectorsize, (u32)getpagesize());
|
2007-10-16 04:25:14 +08:00
|
|
|
if (leafsize < sectorsize || (leafsize & (sectorsize - 1))) {
|
|
|
|
fprintf(stderr, "Illegal leafsize %u\n", leafsize);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
if (nodesize < sectorsize || (nodesize & (sectorsize - 1))) {
|
|
|
|
fprintf(stderr, "Illegal nodesize %u\n", nodesize);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
ac = ac - optind;
|
2008-03-25 03:04:49 +08:00
|
|
|
if (ac == 0)
|
2007-10-16 04:25:14 +08:00
|
|
|
print_usage();
|
2008-03-25 03:04:49 +08:00
|
|
|
|
|
|
|
file = av[optind++];
|
2008-04-01 22:36:46 +08:00
|
|
|
ret = check_mounted(file);
|
|
|
|
if (ret < 0) {
|
|
|
|
fprintf(stderr, "error checking %s mount status\n", file);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
if (ret == 1) {
|
|
|
|
fprintf(stderr, "%s is mounted\n", file);
|
|
|
|
exit(1);
|
|
|
|
}
|
2008-03-25 03:04:49 +08:00
|
|
|
ac--;
|
2007-03-21 08:35:03 +08:00
|
|
|
fd = open(file, O_RDWR);
|
|
|
|
if (fd < 0) {
|
|
|
|
fprintf(stderr, "unable to open %s\n", file);
|
|
|
|
exit(1);
|
|
|
|
}
|
2008-03-25 03:04:49 +08:00
|
|
|
first_fd = fd;
|
2008-04-23 02:06:31 +08:00
|
|
|
first_file = file;
|
2008-03-25 03:04:49 +08:00
|
|
|
ret = btrfs_prepare_device(fd, file, zero_end, &dev_block_count);
|
|
|
|
if (block_count == 0)
|
|
|
|
block_count = dev_block_count;
|
2008-02-16 00:19:26 +08:00
|
|
|
|
2008-12-06 01:21:31 +08:00
|
|
|
blocks[0] = BTRFS_SUPER_INFO_OFFSET;
|
|
|
|
for (i = 1; i < 7; i++) {
|
|
|
|
blocks[i] = BTRFS_SUPER_INFO_OFFSET + 1024 * 1024 +
|
|
|
|
leafsize * i;
|
|
|
|
}
|
2008-02-16 00:19:26 +08:00
|
|
|
|
2008-04-18 22:31:42 +08:00
|
|
|
ret = make_btrfs(fd, file, label, blocks, block_count,
|
|
|
|
nodesize, leafsize,
|
2008-01-04 23:38:22 +08:00
|
|
|
sectorsize, stripesize);
|
2007-03-21 08:35:03 +08:00
|
|
|
if (ret) {
|
|
|
|
fprintf(stderr, "error during mkfs %d\n", ret);
|
|
|
|
exit(1);
|
|
|
|
}
|
2008-09-24 00:29:10 +08:00
|
|
|
root = open_ctree(file, 0, O_RDWR);
|
|
|
|
root->fs_info->alloc_start = alloc_start;
|
|
|
|
|
|
|
|
ret = make_root_dir(root);
|
2007-03-21 23:13:29 +08:00
|
|
|
if (ret) {
|
|
|
|
fprintf(stderr, "failed to setup the root directory\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
2008-09-24 00:29:10 +08:00
|
|
|
|
2008-04-04 04:35:48 +08:00
|
|
|
trans = btrfs_start_transaction(root, 1);
|
|
|
|
|
2008-03-25 03:04:49 +08:00
|
|
|
if (ac == 0)
|
2008-04-04 04:35:48 +08:00
|
|
|
goto raid_groups;
|
2008-03-25 03:04:49 +08:00
|
|
|
|
2008-03-25 03:05:44 +08:00
|
|
|
btrfs_register_one_device(file);
|
2008-03-25 03:04:49 +08:00
|
|
|
if (!root) {
|
|
|
|
fprintf(stderr, "ctree init failed\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
zero_end = 1;
|
|
|
|
while(ac-- > 0) {
|
|
|
|
file = av[optind++];
|
2008-04-01 22:36:46 +08:00
|
|
|
ret = check_mounted(file);
|
|
|
|
if (ret < 0) {
|
|
|
|
fprintf(stderr, "error checking %s mount status\n",
|
|
|
|
file);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
if (ret == 1) {
|
|
|
|
fprintf(stderr, "%s is mounted\n", file);
|
|
|
|
exit(1);
|
|
|
|
}
|
2008-03-25 03:04:49 +08:00
|
|
|
fd = open(file, O_RDWR);
|
|
|
|
if (fd < 0) {
|
|
|
|
fprintf(stderr, "unable to open %s\n", file);
|
|
|
|
exit(1);
|
|
|
|
}
|
2008-04-18 22:45:17 +08:00
|
|
|
ret = btrfs_device_already_in_root(root, fd,
|
|
|
|
BTRFS_SUPER_INFO_OFFSET);
|
|
|
|
if (ret) {
|
|
|
|
fprintf(stderr, "skipping duplicate device %s in FS\n",
|
|
|
|
file);
|
|
|
|
close(fd);
|
|
|
|
continue;
|
|
|
|
}
|
2008-03-25 03:04:49 +08:00
|
|
|
ret = btrfs_prepare_device(fd, file, zero_end,
|
|
|
|
&dev_block_count);
|
|
|
|
|
|
|
|
BUG_ON(ret);
|
|
|
|
|
2008-04-18 22:31:42 +08:00
|
|
|
ret = btrfs_add_to_fsid(trans, root, fd, file, dev_block_count,
|
2008-03-25 03:04:49 +08:00
|
|
|
sectorsize, sectorsize, sectorsize);
|
|
|
|
BUG_ON(ret);
|
2008-03-25 03:05:44 +08:00
|
|
|
btrfs_register_one_device(file);
|
2008-03-25 03:04:49 +08:00
|
|
|
}
|
2008-04-04 04:35:48 +08:00
|
|
|
|
2008-04-04 04:35:48 +08:00
|
|
|
raid_groups:
|
2008-04-04 04:35:48 +08:00
|
|
|
ret = create_raid_groups(trans, root, data_profile,
|
|
|
|
metadata_profile);
|
2008-09-24 00:29:10 +08:00
|
|
|
BUG_ON(ret);
|
2008-04-23 02:06:31 +08:00
|
|
|
|
2008-09-26 22:26:53 +08:00
|
|
|
ret = create_data_reloc_tree(trans, root);
|
|
|
|
BUG_ON(ret);
|
|
|
|
|
2008-04-23 02:06:31 +08:00
|
|
|
printf("fs created label %s on %s\n\tnodesize %u leafsize %u "
|
|
|
|
"sectorsize %u size %s\n",
|
|
|
|
label, first_file, nodesize, leafsize, sectorsize,
|
|
|
|
pretty_sizes(btrfs_super_total_bytes(&root->fs_info->super_copy)));
|
|
|
|
|
2008-07-25 00:13:30 +08:00
|
|
|
printf("%s\n", BTRFS_BUILD_VERSION);
|
2008-03-25 03:04:49 +08:00
|
|
|
btrfs_commit_transaction(trans, root);
|
|
|
|
ret = close_ctree(root);
|
|
|
|
BUG_ON(ret);
|
2008-04-23 02:06:31 +08:00
|
|
|
|
|
|
|
free(label);
|
2007-03-21 08:35:03 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|