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
|
2007-03-21 08:35:03 +08:00
|
|
|
#ifndef __CHECKER__
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/mount.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>
|
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"
|
2007-03-21 23:13:29 +08:00
|
|
|
#include "transaction.h"
|
2007-02-21 05:41:09 +08:00
|
|
|
|
2007-03-21 08:35:03 +08:00
|
|
|
#ifdef __CHECKER__
|
|
|
|
#define BLKGETSIZE64 0
|
|
|
|
static inline int ioctl(int fd, int define, u64 *size) { return 0; }
|
|
|
|
#endif
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2007-04-07 03:39:12 +08:00
|
|
|
static int __make_root_dir(struct btrfs_trans_handle *trans,
|
2007-04-11 20:58:53 +08:00
|
|
|
struct btrfs_root *root, u64 objectid)
|
2007-04-07 03:39:12 +08:00
|
|
|
{
|
2007-03-21 23:13:29 +08:00
|
|
|
int ret;
|
|
|
|
char buf[8];
|
|
|
|
struct btrfs_key inode_map;
|
|
|
|
struct btrfs_inode_item inode_item;
|
|
|
|
|
|
|
|
buf[0] = '.';
|
|
|
|
buf[1] = '.';
|
|
|
|
|
|
|
|
inode_map.objectid = objectid;
|
2007-04-07 03:39:12 +08:00
|
|
|
btrfs_set_key_type(&inode_map, BTRFS_INODE_ITEM_KEY);
|
2007-03-21 23:13:29 +08:00
|
|
|
inode_map.offset = 0;
|
|
|
|
|
|
|
|
memset(&inode_item, 0, sizeof(inode_item));
|
|
|
|
btrfs_set_inode_generation(&inode_item, root->fs_info->generation);
|
2007-12-13 03:39:46 +08:00
|
|
|
btrfs_set_inode_size(&inode_item, 0);
|
2007-03-21 23:13:29 +08:00
|
|
|
btrfs_set_inode_nlink(&inode_item, 1);
|
2007-12-13 03:39:46 +08:00
|
|
|
btrfs_set_inode_nblocks(&inode_item, 0);
|
2007-04-07 03:39:12 +08:00
|
|
|
btrfs_set_inode_mode(&inode_item, S_IFDIR | 0555);
|
2007-03-21 23:13:29 +08:00
|
|
|
|
2007-04-07 03:39:12 +08:00
|
|
|
if (root->fs_info->tree_root == root)
|
|
|
|
btrfs_set_super_root_dir(root->fs_info->disk_super, objectid);
|
2007-03-21 23:13:29 +08:00
|
|
|
|
|
|
|
ret = btrfs_insert_inode(trans, root, objectid, &inode_item);
|
|
|
|
if (ret)
|
|
|
|
goto error;
|
2007-12-13 03:39:46 +08:00
|
|
|
|
|
|
|
ret = btrfs_insert_inode_ref(trans, root, "..", 2, objectid, objectid);
|
2007-03-21 23:13:29 +08:00
|
|
|
if (ret)
|
|
|
|
goto error;
|
2007-04-07 03:39:12 +08:00
|
|
|
btrfs_set_root_dirid(&root->root_item, objectid);
|
|
|
|
ret = 0;
|
|
|
|
error:
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2007-04-27 04:46:06 +08:00
|
|
|
static int make_block_groups(struct btrfs_trans_handle *trans,
|
|
|
|
struct btrfs_root *root)
|
|
|
|
{
|
2007-10-16 04:25:14 +08:00
|
|
|
u64 group_size;
|
|
|
|
u64 total_bytes;
|
2007-04-27 04:46:06 +08:00
|
|
|
u64 cur_start;
|
|
|
|
int ret;
|
2007-05-30 22:21:32 +08:00
|
|
|
u64 nr = 0;
|
2007-04-27 04:46:06 +08:00
|
|
|
struct btrfs_block_group_cache *cache;
|
2007-10-17 21:58:56 +08:00
|
|
|
struct cache_tree *bg_cache = &root->fs_info->block_group_cache;
|
2007-04-27 04:46:06 +08:00
|
|
|
|
|
|
|
root = root->fs_info->extent_root;
|
2007-10-16 04:25:14 +08:00
|
|
|
|
2007-04-27 04:46:06 +08:00
|
|
|
/* first we bootstrap the things into cache */
|
2007-10-16 04:25:14 +08:00
|
|
|
group_size = BTRFS_BLOCK_GROUP_SIZE;
|
2007-04-27 04:46:06 +08:00
|
|
|
cache = malloc(sizeof(*cache));
|
|
|
|
cache->key.objectid = 0;
|
2007-10-16 04:25:14 +08:00
|
|
|
cache->key.offset = group_size;
|
2007-10-17 21:58:56 +08:00
|
|
|
cache->cache.start = 0;
|
|
|
|
cache->cache.size = group_size;
|
2007-04-27 04:46:06 +08:00
|
|
|
btrfs_set_key_type(&cache->key, BTRFS_BLOCK_GROUP_ITEM_KEY);
|
2007-10-16 04:25:14 +08:00
|
|
|
|
2007-04-27 04:46:06 +08:00
|
|
|
memset(&cache->item, 0, sizeof(cache->item));
|
|
|
|
btrfs_set_block_group_used(&cache->item,
|
2007-10-16 04:25:14 +08:00
|
|
|
btrfs_super_bytes_used(root->fs_info->disk_super));
|
2007-10-17 21:58:56 +08:00
|
|
|
ret = insert_existing_cache_extent(bg_cache, &cache->cache);
|
2007-04-27 04:46:06 +08:00
|
|
|
BUG_ON(ret);
|
|
|
|
|
2007-10-16 04:25:14 +08:00
|
|
|
total_bytes = btrfs_super_total_bytes(root->fs_info->disk_super);
|
|
|
|
cur_start = group_size;
|
|
|
|
while(cur_start < total_bytes) {
|
2007-04-27 04:46:06 +08:00
|
|
|
cache = malloc(sizeof(*cache));
|
|
|
|
cache->key.objectid = cur_start;
|
2007-10-16 04:25:14 +08:00
|
|
|
cache->key.offset = group_size;
|
2007-10-17 21:58:56 +08:00
|
|
|
cache->cache.start = cur_start;
|
|
|
|
cache->cache.size = group_size;
|
2007-04-27 04:46:06 +08:00
|
|
|
btrfs_set_key_type(&cache->key, BTRFS_BLOCK_GROUP_ITEM_KEY);
|
|
|
|
memset(&cache->item, 0, sizeof(cache->item));
|
2007-05-30 22:21:32 +08:00
|
|
|
if (nr % 3)
|
|
|
|
cache->item.flags |= BTRFS_BLOCK_GROUP_DATA;
|
|
|
|
|
2007-10-17 21:58:56 +08:00
|
|
|
ret = insert_existing_cache_extent(bg_cache, &cache->cache);
|
2007-04-27 04:46:06 +08:00
|
|
|
BUG_ON(ret);
|
2007-10-16 04:25:14 +08:00
|
|
|
cur_start += group_size;
|
2007-05-30 22:21:32 +08:00
|
|
|
nr++;
|
2007-04-27 04:46:06 +08:00
|
|
|
}
|
|
|
|
/* then insert all the items */
|
|
|
|
cur_start = 0;
|
2007-10-16 04:25:14 +08:00
|
|
|
while(cur_start < total_bytes) {
|
2007-10-17 21:58:56 +08:00
|
|
|
struct cache_extent *ce;
|
|
|
|
ce = find_first_cache_extent(bg_cache, cur_start);
|
|
|
|
BUG_ON(!ce);
|
|
|
|
cache = container_of(ce, struct btrfs_block_group_cache,
|
|
|
|
cache);
|
2007-04-27 04:46:06 +08:00
|
|
|
ret = btrfs_insert_block_group(trans, root, &cache->key,
|
|
|
|
&cache->item);
|
|
|
|
BUG_ON(ret);
|
2007-10-16 04:25:14 +08:00
|
|
|
cur_start += group_size;
|
2007-04-27 04:46:06 +08:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-04-07 03:39:12 +08:00
|
|
|
static int make_root_dir(int fd) {
|
|
|
|
struct btrfs_root *root;
|
|
|
|
struct btrfs_super_block super;
|
|
|
|
struct btrfs_trans_handle *trans;
|
|
|
|
int ret;
|
|
|
|
struct btrfs_key location;
|
|
|
|
|
|
|
|
root = open_ctree_fd(fd, &super);
|
|
|
|
|
|
|
|
if (!root) {
|
|
|
|
fprintf(stderr, "ctree init failed\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
trans = btrfs_start_transaction(root, 1);
|
2007-04-27 04:46:06 +08:00
|
|
|
ret = make_block_groups(trans, root);
|
2007-04-11 20:58:53 +08:00
|
|
|
ret = __make_root_dir(trans, root->fs_info->tree_root,
|
|
|
|
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;
|
2007-04-11 20:58:53 +08:00
|
|
|
ret = __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,
|
2007-04-07 03:39:12 +08:00
|
|
|
btrfs_super_root_dir(root->fs_info->disk_super),
|
2007-06-08 10:12:21 +08:00
|
|
|
&location, BTRFS_FT_DIR);
|
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,
|
|
|
|
BTRFS_ROOT_TREE_DIR_OBJECTID);
|
|
|
|
if (ret)
|
|
|
|
goto err;
|
|
|
|
|
2007-04-07 03:39:12 +08:00
|
|
|
btrfs_commit_transaction(trans, root, root->fs_info->disk_super);
|
2007-03-21 23:13:29 +08:00
|
|
|
ret = close_ctree(root, &super);
|
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
|
|
|
|
2007-10-16 04:25:14 +08:00
|
|
|
int mkfs(int fd, char *pathname, u64 num_bytes, u32 nodesize, u32 leafsize,
|
2007-12-01 00:30:24 +08:00
|
|
|
u32 sectorsize, u32 stripesize)
|
2007-03-14 04:47:54 +08:00
|
|
|
{
|
|
|
|
struct btrfs_super_block super;
|
2007-03-15 02:14:43 +08:00
|
|
|
struct btrfs_leaf *empty_leaf;
|
2007-03-14 04:47:54 +08:00
|
|
|
struct btrfs_root_item root_item;
|
|
|
|
struct btrfs_item item;
|
|
|
|
struct btrfs_extent_item extent_item;
|
2007-04-07 03:39:12 +08:00
|
|
|
struct btrfs_inode_item *inode_item;
|
2007-03-14 04:47:54 +08:00
|
|
|
char *block;
|
|
|
|
int ret;
|
2007-03-15 02:14:43 +08:00
|
|
|
u32 itemoff;
|
2007-10-16 04:25:14 +08:00
|
|
|
u32 start_block = BTRFS_SUPER_INFO_OFFSET;
|
|
|
|
u32 first_free = BTRFS_SUPER_INFO_OFFSET + sectorsize;
|
2007-04-12 01:57:55 +08:00
|
|
|
|
2007-04-09 22:42:55 +08:00
|
|
|
btrfs_set_super_generation(&super, 1);
|
2007-10-16 04:25:14 +08:00
|
|
|
btrfs_set_super_bytenr(&super, start_block);
|
|
|
|
btrfs_set_super_root_level(&super, 0);
|
|
|
|
btrfs_set_super_root(&super, first_free);
|
2007-03-14 04:47:54 +08:00
|
|
|
strcpy((char *)(&super.magic), BTRFS_MAGIC);
|
2007-10-16 04:24:39 +08:00
|
|
|
|
2007-10-16 04:25:14 +08:00
|
|
|
printf("blocksize is %d\n", leafsize);
|
|
|
|
btrfs_set_super_sectorsize(&super, sectorsize);
|
|
|
|
btrfs_set_super_leafsize(&super, leafsize);
|
|
|
|
btrfs_set_super_nodesize(&super, nodesize);
|
2007-12-01 00:30:24 +08:00
|
|
|
btrfs_set_super_stripesize(&super, stripesize);
|
2007-10-16 04:24:39 +08:00
|
|
|
|
2007-10-16 04:25:14 +08:00
|
|
|
num_bytes = (num_bytes / sectorsize) * sectorsize;
|
|
|
|
btrfs_set_super_total_bytes(&super, num_bytes);
|
|
|
|
btrfs_set_super_bytes_used(&super, start_block + 3 * leafsize +
|
|
|
|
sectorsize);
|
2007-04-06 02:29:12 +08:00
|
|
|
uuid_generate(super.fsid);
|
2007-03-14 04:47:54 +08:00
|
|
|
|
2007-10-16 04:25:14 +08:00
|
|
|
block = malloc(sectorsize);
|
|
|
|
memset(block, 0, sectorsize);
|
|
|
|
BUG_ON(sizeof(super) > sectorsize);
|
2007-03-14 04:47:54 +08:00
|
|
|
memcpy(block, &super, sizeof(super));
|
2007-10-16 04:25:14 +08:00
|
|
|
ret = pwrite(fd, block, sectorsize, BTRFS_SUPER_INFO_OFFSET);
|
|
|
|
BUG_ON(ret != sectorsize);
|
2007-03-14 04:47:54 +08:00
|
|
|
|
|
|
|
/* create the tree of root objects */
|
2007-10-16 04:25:14 +08:00
|
|
|
empty_leaf = malloc(leafsize);
|
|
|
|
memset(empty_leaf, 0, leafsize);
|
|
|
|
btrfs_set_header_bytenr(&empty_leaf->header, first_free);
|
2007-04-11 00:13:29 +08:00
|
|
|
btrfs_set_header_nritems(&empty_leaf->header, 2);
|
2007-12-10 02:46:24 +08:00
|
|
|
btrfs_set_header_generation(&empty_leaf->header, 1);
|
2007-04-21 08:23:29 +08:00
|
|
|
btrfs_set_header_owner(&empty_leaf->header, BTRFS_ROOT_TREE_OBJECTID);
|
2007-04-06 02:29:12 +08:00
|
|
|
memcpy(empty_leaf->header.fsid, super.fsid,
|
|
|
|
sizeof(empty_leaf->header.fsid));
|
2007-03-14 04:47:54 +08:00
|
|
|
|
|
|
|
/* create the items for the root tree */
|
2007-04-07 03:39:12 +08:00
|
|
|
inode_item = &root_item.inode;
|
|
|
|
memset(inode_item, 0, sizeof(*inode_item));
|
|
|
|
btrfs_set_inode_generation(inode_item, 1);
|
|
|
|
btrfs_set_inode_size(inode_item, 3);
|
|
|
|
btrfs_set_inode_nlink(inode_item, 1);
|
|
|
|
btrfs_set_inode_nblocks(inode_item, 1);
|
|
|
|
btrfs_set_inode_mode(inode_item, S_IFDIR | 0755);
|
|
|
|
|
2007-08-08 04:15:59 +08:00
|
|
|
// memset(&root_item, 0, sizeof(root_item));
|
2007-04-07 03:39:12 +08:00
|
|
|
btrfs_set_root_dirid(&root_item, 0);
|
2007-03-14 04:47:54 +08:00
|
|
|
btrfs_set_root_refs(&root_item, 1);
|
2007-03-16 00:56:47 +08:00
|
|
|
btrfs_set_disk_key_offset(&item.key, 0);
|
2007-04-12 01:57:55 +08:00
|
|
|
btrfs_set_item_size(&item, sizeof(root_item));
|
2007-03-16 00:56:47 +08:00
|
|
|
btrfs_set_disk_key_type(&item.key, BTRFS_ROOT_ITEM_KEY);
|
2007-04-12 01:57:55 +08:00
|
|
|
|
2007-10-16 04:25:14 +08:00
|
|
|
itemoff = __BTRFS_LEAF_DATA_SIZE(leafsize) - sizeof(root_item);
|
|
|
|
btrfs_set_root_bytenr(&root_item, first_free + leafsize);
|
|
|
|
root_item.level = 0;
|
2007-04-12 01:57:55 +08:00
|
|
|
btrfs_set_item_offset(&item, itemoff);
|
|
|
|
btrfs_set_disk_key_objectid(&item.key, BTRFS_EXTENT_TREE_OBJECTID);
|
2007-03-15 02:14:43 +08:00
|
|
|
memcpy(empty_leaf->items, &item, sizeof(item));
|
|
|
|
memcpy(btrfs_leaf_data(empty_leaf) + itemoff,
|
|
|
|
&root_item, sizeof(root_item));
|
2007-03-14 04:47:54 +08:00
|
|
|
|
2007-10-16 04:25:14 +08:00
|
|
|
btrfs_set_root_bytenr(&root_item, first_free + leafsize * 2);
|
|
|
|
btrfs_set_root_bytes_used(&root_item, 1);
|
2007-03-14 04:47:54 +08:00
|
|
|
itemoff = itemoff - sizeof(root_item);
|
|
|
|
btrfs_set_item_offset(&item, itemoff);
|
2007-03-21 02:38:32 +08:00
|
|
|
btrfs_set_disk_key_objectid(&item.key, BTRFS_FS_TREE_OBJECTID);
|
2007-04-11 00:13:29 +08:00
|
|
|
memcpy(empty_leaf->items + 1, &item, sizeof(item));
|
2007-03-15 02:14:43 +08:00
|
|
|
memcpy(btrfs_leaf_data(empty_leaf) + itemoff,
|
|
|
|
&root_item, sizeof(root_item));
|
2007-10-16 04:25:14 +08:00
|
|
|
ret = pwrite(fd, empty_leaf, leafsize, first_free);
|
2007-03-14 04:47:54 +08:00
|
|
|
|
2007-04-12 01:57:55 +08:00
|
|
|
/* create the items for the extent tree */
|
2007-10-16 04:25:14 +08:00
|
|
|
btrfs_set_header_bytenr(&empty_leaf->header, first_free + leafsize);
|
2007-06-09 21:22:37 +08:00
|
|
|
btrfs_set_header_nritems(&empty_leaf->header, 4);
|
2007-03-14 04:47:54 +08:00
|
|
|
|
|
|
|
/* item1, reserve blocks 0-16 */
|
2007-03-16 00:56:47 +08:00
|
|
|
btrfs_set_disk_key_objectid(&item.key, 0);
|
2007-10-16 04:25:14 +08:00
|
|
|
btrfs_set_disk_key_offset(&item.key, first_free);
|
2007-10-16 04:24:39 +08:00
|
|
|
btrfs_set_disk_key_type(&item.key, 0);
|
2007-03-16 00:56:47 +08:00
|
|
|
btrfs_set_disk_key_type(&item.key, BTRFS_EXTENT_ITEM_KEY);
|
2007-10-16 04:25:14 +08:00
|
|
|
itemoff = __BTRFS_LEAF_DATA_SIZE(leafsize) -
|
2007-03-15 02:14:43 +08:00
|
|
|
sizeof(struct btrfs_extent_item);
|
2007-03-14 04:47:54 +08:00
|
|
|
btrfs_set_item_offset(&item, itemoff);
|
|
|
|
btrfs_set_item_size(&item, sizeof(struct btrfs_extent_item));
|
|
|
|
btrfs_set_extent_refs(&extent_item, 1);
|
2007-03-15 02:14:43 +08:00
|
|
|
memcpy(empty_leaf->items, &item, sizeof(item));
|
|
|
|
memcpy(btrfs_leaf_data(empty_leaf) + btrfs_item_offset(&item),
|
|
|
|
&extent_item, btrfs_item_size(&item));
|
2007-03-14 04:47:54 +08:00
|
|
|
|
|
|
|
/* item2, give block 17 to the root */
|
2007-10-16 04:25:14 +08:00
|
|
|
btrfs_set_disk_key_objectid(&item.key, first_free);
|
|
|
|
btrfs_set_disk_key_offset(&item.key, leafsize);
|
2007-03-14 04:47:54 +08:00
|
|
|
itemoff = itemoff - sizeof(struct btrfs_extent_item);
|
|
|
|
btrfs_set_item_offset(&item, itemoff);
|
2007-03-15 02:14:43 +08:00
|
|
|
memcpy(empty_leaf->items + 1, &item, sizeof(item));
|
|
|
|
memcpy(btrfs_leaf_data(empty_leaf) + btrfs_item_offset(&item),
|
|
|
|
&extent_item, btrfs_item_size(&item));
|
2007-03-14 04:47:54 +08:00
|
|
|
|
2007-06-09 21:22:37 +08:00
|
|
|
/* item3, give block 18 to the extent root */
|
2007-10-16 04:25:14 +08:00
|
|
|
btrfs_set_disk_key_objectid(&item.key, first_free + leafsize);
|
|
|
|
btrfs_set_disk_key_offset(&item.key, leafsize);
|
2007-03-14 04:47:54 +08:00
|
|
|
itemoff = itemoff - sizeof(struct btrfs_extent_item);
|
|
|
|
btrfs_set_item_offset(&item, itemoff);
|
2007-03-15 02:14:43 +08:00
|
|
|
memcpy(empty_leaf->items + 2, &item, sizeof(item));
|
|
|
|
memcpy(btrfs_leaf_data(empty_leaf) + btrfs_item_offset(&item),
|
|
|
|
&extent_item, btrfs_item_size(&item));
|
2007-03-14 04:47:54 +08:00
|
|
|
|
2007-06-09 21:22:37 +08:00
|
|
|
/* item4, give block 19 to the FS root */
|
2007-10-16 04:25:14 +08:00
|
|
|
btrfs_set_disk_key_objectid(&item.key, first_free + leafsize * 2);
|
|
|
|
btrfs_set_disk_key_offset(&item.key, leafsize);
|
2007-03-14 04:47:54 +08:00
|
|
|
itemoff = itemoff - sizeof(struct btrfs_extent_item);
|
|
|
|
btrfs_set_item_offset(&item, itemoff);
|
2007-04-11 00:13:29 +08:00
|
|
|
memcpy(empty_leaf->items + 3, &item, sizeof(item));
|
2007-03-21 02:38:32 +08:00
|
|
|
memcpy(btrfs_leaf_data(empty_leaf) + btrfs_item_offset(&item),
|
|
|
|
&extent_item, btrfs_item_size(&item));
|
2007-10-16 04:25:14 +08:00
|
|
|
ret = pwrite(fd, empty_leaf, leafsize, first_free + leafsize);
|
|
|
|
if (ret != leafsize)
|
2007-03-21 02:38:32 +08:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
/* finally create the FS root */
|
2007-10-16 04:25:14 +08:00
|
|
|
btrfs_set_header_bytenr(&empty_leaf->header, first_free + leafsize * 2);
|
2007-03-21 02:38:32 +08:00
|
|
|
btrfs_set_header_nritems(&empty_leaf->header, 0);
|
2007-10-16 04:25:14 +08:00
|
|
|
ret = pwrite(fd, empty_leaf, leafsize, first_free + leafsize * 2);
|
|
|
|
if (ret != leafsize)
|
2007-03-14 04:47:54 +08:00
|
|
|
return -1;
|
|
|
|
return 0;
|
|
|
|
}
|
2007-03-21 08:35:03 +08:00
|
|
|
|
|
|
|
u64 device_size(int fd, struct stat *st)
|
|
|
|
{
|
|
|
|
u64 size;
|
|
|
|
if (S_ISREG(st->st_mode)) {
|
|
|
|
return st->st_size;
|
|
|
|
}
|
|
|
|
if (!S_ISBLK(st->st_mode)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (ioctl(fd, BLKGETSIZE64, &size) >= 0) {
|
|
|
|
return size;
|
|
|
|
}
|
2007-10-16 04:25:14 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void print_usage(void)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "usage: mkfs.btrfs [ -l leafsize ] [ -n nodesize] dev [ blocks ]\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
2007-03-21 08:35:03 +08:00
|
|
|
|
|
|
|
int main(int ac, char **av)
|
|
|
|
{
|
|
|
|
char *file;
|
|
|
|
u64 block_count = 0;
|
|
|
|
int fd;
|
|
|
|
struct stat st;
|
|
|
|
int ret;
|
|
|
|
int i;
|
2007-12-14 00:12:45 +08:00
|
|
|
u32 leafsize = 16 * 1024;
|
2007-10-16 04:25:14 +08:00
|
|
|
u32 sectorsize = 4096;
|
2007-12-14 00:12:45 +08:00
|
|
|
u32 nodesize = 16 * 1024;
|
2007-12-01 00:30:24 +08:00
|
|
|
u32 stripesize = 4096;
|
2007-10-16 04:25:14 +08:00
|
|
|
char *buf = malloc(sectorsize);
|
2007-04-12 01:57:55 +08:00
|
|
|
char *realpath_name;
|
2007-03-21 23:13:29 +08:00
|
|
|
|
2007-10-16 04:25:14 +08:00
|
|
|
while(1) {
|
|
|
|
int c;
|
2007-12-01 00:30:24 +08:00
|
|
|
c = getopt(ac, av, "l:n:s:");
|
2007-10-16 04:25:14 +08:00
|
|
|
if (c < 0)
|
|
|
|
break;
|
|
|
|
switch(c) {
|
|
|
|
case 'l':
|
2007-12-22 05:25:35 +08:00
|
|
|
leafsize = parse_size(optarg);
|
2007-10-16 04:25:14 +08:00
|
|
|
break;
|
|
|
|
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':
|
2007-12-22 05:25:35 +08:00
|
|
|
stripesize = parse_size(optarg);
|
2007-12-01 00:30:24 +08:00
|
|
|
break;
|
2007-10-16 04:25:14 +08:00
|
|
|
default:
|
|
|
|
print_usage();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
if (ac >= 1) {
|
|
|
|
file = av[optind];
|
|
|
|
if (ac == 2) {
|
2007-12-22 05:25:35 +08:00
|
|
|
block_count = parse_size(av[optind + 1]);
|
2007-03-21 08:35:03 +08:00
|
|
|
if (!block_count) {
|
|
|
|
fprintf(stderr, "error finding block count\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2007-10-16 04:25:14 +08:00
|
|
|
print_usage();
|
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);
|
|
|
|
}
|
|
|
|
ret = fstat(fd, &st);
|
|
|
|
if (ret < 0) {
|
|
|
|
fprintf(stderr, "unable to stat %s\n", file);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
if (block_count == 0) {
|
|
|
|
block_count = device_size(fd, &st);
|
|
|
|
if (block_count == 0) {
|
|
|
|
fprintf(stderr, "unable to find %s size\n", file);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
2007-12-22 05:25:35 +08:00
|
|
|
block_count /= sectorsize;
|
|
|
|
block_count *= sectorsize;
|
|
|
|
|
|
|
|
if (block_count < 256 * 1024 * 1024) {
|
2007-03-21 08:35:03 +08:00
|
|
|
fprintf(stderr, "device %s is too small\n", file);
|
|
|
|
exit(1);
|
|
|
|
}
|
2007-10-16 04:25:14 +08:00
|
|
|
memset(buf, 0, sectorsize);
|
2007-04-12 01:57:55 +08:00
|
|
|
for(i = 0; i < 64; i++) {
|
2007-10-16 04:25:14 +08:00
|
|
|
ret = write(fd, buf, sectorsize);
|
|
|
|
if (ret != sectorsize) {
|
2007-03-21 08:35:03 +08:00
|
|
|
fprintf(stderr, "unable to zero fill device\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
2007-04-12 01:57:55 +08:00
|
|
|
realpath_name = realpath(file, NULL);
|
2007-10-16 04:25:14 +08:00
|
|
|
ret = mkfs(fd, realpath_name, block_count, nodesize, leafsize,
|
2007-12-01 00:30:24 +08:00
|
|
|
sectorsize, stripesize);
|
2007-03-21 08:35:03 +08:00
|
|
|
if (ret) {
|
|
|
|
fprintf(stderr, "error during mkfs %d\n", ret);
|
|
|
|
exit(1);
|
|
|
|
}
|
2007-03-21 23:13:29 +08:00
|
|
|
ret = make_root_dir(fd);
|
|
|
|
if (ret) {
|
|
|
|
fprintf(stderr, "failed to setup the root directory\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
2007-10-16 04:25:14 +08:00
|
|
|
printf("fs created on %s nodesize %u leafsize %u sectorsize %u bytes %llu\n",
|
|
|
|
file, nodesize, leafsize, sectorsize,
|
|
|
|
(unsigned long long)block_count);
|
2007-03-21 08:35:03 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|