2007-06-12 13:07:11 +00: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-20 21:41:09 +00:00
|
|
|
#define _XOPEN_SOURCE 500
|
2007-03-21 00:35:03 +00:00
|
|
|
#ifndef __CHECKER__
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/mount.h>
|
|
|
|
#endif
|
2007-02-20 21:41:09 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
2007-04-05 18:29:12 +00:00
|
|
|
#include <uuid/uuid.h>
|
2007-06-12 15:39:09 +00:00
|
|
|
#include <linux/fs.h>
|
2007-12-21 21:25:35 +00:00
|
|
|
#include <ctype.h>
|
2007-06-12 15:39:09 +00:00
|
|
|
#include "kerncompat.h"
|
2007-02-20 21:41:09 +00:00
|
|
|
#include "ctree.h"
|
|
|
|
#include "disk-io.h"
|
2008-03-24 19:03:18 +00:00
|
|
|
#include "volumes.h"
|
2007-03-21 15:13:29 +00:00
|
|
|
#include "transaction.h"
|
2008-01-04 15:38:22 +00:00
|
|
|
#include "utils.h"
|
2007-02-20 21:41:09 +00:00
|
|
|
|
2007-03-21 00:35:03 +00:00
|
|
|
#ifdef __CHECKER__
|
|
|
|
#define BLKGETSIZE64 0
|
|
|
|
static inline int ioctl(int fd, int define, u64 *size) { return 0; }
|
|
|
|
#endif
|
|
|
|
|
2007-12-21 21:25:35 +00: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-02-15 16:19:26 +00:00
|
|
|
static int zero_blocks(int fd, off_t start, size_t len)
|
|
|
|
{
|
|
|
|
char *buf = malloc(len);
|
|
|
|
int ret = 0;
|
|
|
|
ssize_t written;
|
|
|
|
|
|
|
|
if (!buf)
|
|
|
|
return -ENOMEM;
|
|
|
|
memset(buf, 0, len);
|
|
|
|
written = pwrite(fd, buf, len, start);
|
|
|
|
if (written != len)
|
|
|
|
ret = -EIO;
|
|
|
|
free(buf);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int zero_dev_start(int fd)
|
|
|
|
{
|
|
|
|
off_t start = 0;
|
|
|
|
size_t len = 2 * 1024 * 1024;
|
|
|
|
|
|
|
|
#ifdef __sparc__
|
|
|
|
/* don't overwrite the disk labels on sparc */
|
|
|
|
start = 1024;
|
|
|
|
len -= 1024;
|
|
|
|
#endif
|
|
|
|
return zero_blocks(fd, start, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int zero_dev_end(int fd, u64 dev_size)
|
|
|
|
{
|
|
|
|
size_t len = 2 * 1024 * 1024;
|
|
|
|
off_t start = dev_size - len;
|
|
|
|
|
|
|
|
return zero_blocks(fd, start, len);
|
|
|
|
}
|
|
|
|
|
2007-04-06 19:39:12 +00:00
|
|
|
static int make_root_dir(int fd) {
|
|
|
|
struct btrfs_root *root;
|
|
|
|
struct btrfs_trans_handle *trans;
|
|
|
|
struct btrfs_key location;
|
2008-03-24 19:03:18 +00:00
|
|
|
u64 bytes_used;
|
|
|
|
u64 chunk_start = 0;
|
|
|
|
u64 chunk_size = 0;
|
2008-01-04 15:38:22 +00:00
|
|
|
int ret;
|
2007-04-06 19:39:12 +00:00
|
|
|
|
2008-01-04 15:38:22 +00:00
|
|
|
root = open_ctree_fd(fd, 0);
|
2007-04-06 19:39:12 +00:00
|
|
|
|
|
|
|
if (!root) {
|
|
|
|
fprintf(stderr, "ctree init failed\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
trans = btrfs_start_transaction(root, 1);
|
2008-03-24 19:03:18 +00:00
|
|
|
bytes_used = btrfs_super_bytes_used(&root->fs_info->super_copy);
|
|
|
|
|
|
|
|
root->fs_info->force_system_allocs = 1;
|
|
|
|
ret = btrfs_make_block_group(trans, root, bytes_used,
|
|
|
|
BTRFS_BLOCK_GROUP_SYSTEM,
|
|
|
|
BTRFS_CHUNK_TREE_OBJECTID,
|
|
|
|
0, BTRFS_MKFS_SYSTEM_GROUP_SIZE);
|
|
|
|
BUG_ON(ret);
|
|
|
|
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,
|
|
|
|
BTRFS_CHUNK_TREE_OBJECTID,
|
|
|
|
chunk_start, chunk_size);
|
|
|
|
BUG_ON(ret);
|
|
|
|
|
|
|
|
root->fs_info->force_system_allocs = 0;
|
|
|
|
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,
|
|
|
|
BTRFS_CHUNK_TREE_OBJECTID,
|
|
|
|
chunk_start, chunk_size);
|
|
|
|
BUG_ON(ret);
|
|
|
|
|
|
|
|
// ret = btrfs_make_block_group(trans, root, 0, 1);
|
2008-01-04 15:38:22 +00:00
|
|
|
ret = btrfs_make_root_dir(trans, root->fs_info->tree_root,
|
2007-04-11 12:58:53 +00:00
|
|
|
BTRFS_ROOT_TREE_DIR_OBJECTID);
|
2007-03-21 15:13:29 +00:00
|
|
|
if (ret)
|
2007-04-06 19:39:12 +00:00
|
|
|
goto err;
|
2008-01-04 15:38:22 +00:00
|
|
|
ret = btrfs_make_root_dir(trans, root, BTRFS_FIRST_FREE_OBJECTID);
|
2007-04-06 19:39:12 +00: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-29 19:56:44 +00:00
|
|
|
"default", 7,
|
2008-01-04 15:38:22 +00:00
|
|
|
btrfs_super_root_dir(&root->fs_info->super_copy),
|
2007-06-08 02:12:21 +00:00
|
|
|
&location, BTRFS_FT_DIR);
|
2007-04-06 19:39:12 +00:00
|
|
|
if (ret)
|
|
|
|
goto err;
|
2007-12-12 19:39:46 +00: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;
|
|
|
|
|
2008-01-04 15:38:22 +00:00
|
|
|
btrfs_commit_transaction(trans, root);
|
|
|
|
ret = close_ctree(root);
|
2007-04-06 19:39:12 +00:00
|
|
|
err:
|
2007-03-21 15:13:29 +00:00
|
|
|
return ret;
|
|
|
|
}
|
2007-03-21 00:35:03 +00: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-15 20:25:14 +00: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 00:35:03 +00:00
|
|
|
|
|
|
|
int main(int ac, char **av)
|
|
|
|
{
|
|
|
|
char *file;
|
|
|
|
u64 block_count = 0;
|
|
|
|
int fd;
|
|
|
|
struct stat st;
|
|
|
|
int ret;
|
|
|
|
int i;
|
2007-12-13 16:12:45 +00:00
|
|
|
u32 leafsize = 16 * 1024;
|
2007-10-15 20:25:14 +00:00
|
|
|
u32 sectorsize = 4096;
|
2007-12-13 16:12:45 +00:00
|
|
|
u32 nodesize = 16 * 1024;
|
2007-11-30 16:30:24 +00:00
|
|
|
u32 stripesize = 4096;
|
2008-03-24 19:03:18 +00:00
|
|
|
u64 blocks[6];
|
2008-02-15 16:19:26 +00:00
|
|
|
int zero_end = 0;
|
2007-03-21 15:13:29 +00:00
|
|
|
|
2007-10-15 20:25:14 +00:00
|
|
|
while(1) {
|
|
|
|
int c;
|
2007-11-30 16:30:24 +00:00
|
|
|
c = getopt(ac, av, "l:n:s:");
|
2007-10-15 20:25:14 +00:00
|
|
|
if (c < 0)
|
|
|
|
break;
|
|
|
|
switch(c) {
|
|
|
|
case 'l':
|
2007-12-21 21:25:35 +00:00
|
|
|
leafsize = parse_size(optarg);
|
2007-10-15 20:25:14 +00:00
|
|
|
break;
|
|
|
|
case 'n':
|
2007-12-21 21:25:35 +00:00
|
|
|
nodesize = parse_size(optarg);
|
2007-10-15 20:25:14 +00:00
|
|
|
break;
|
2007-11-30 16:30:24 +00:00
|
|
|
case 's':
|
2007-12-21 21:25:35 +00:00
|
|
|
stripesize = parse_size(optarg);
|
2007-11-30 16:30:24 +00:00
|
|
|
break;
|
2007-10-15 20:25:14 +00: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-21 21:25:35 +00:00
|
|
|
block_count = parse_size(av[optind + 1]);
|
2007-03-21 00:35:03 +00:00
|
|
|
if (!block_count) {
|
|
|
|
fprintf(stderr, "error finding block count\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2007-10-15 20:25:14 +00:00
|
|
|
print_usage();
|
2007-03-21 00:35:03 +00: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);
|
|
|
|
}
|
2008-02-15 16:19:26 +00:00
|
|
|
zero_end = 1;
|
2007-03-21 00:35:03 +00:00
|
|
|
}
|
2007-12-21 21:25:35 +00:00
|
|
|
block_count /= sectorsize;
|
|
|
|
block_count *= sectorsize;
|
|
|
|
|
|
|
|
if (block_count < 256 * 1024 * 1024) {
|
2007-03-21 00:35:03 +00:00
|
|
|
fprintf(stderr, "device %s is too small\n", file);
|
|
|
|
exit(1);
|
|
|
|
}
|
2008-02-15 16:19:26 +00:00
|
|
|
ret = zero_dev_start(fd);
|
|
|
|
if (ret) {
|
|
|
|
fprintf(stderr, "failed to zero device start %d\n", ret);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (zero_end) {
|
|
|
|
ret = zero_dev_end(fd, block_count);
|
|
|
|
if (ret) {
|
|
|
|
fprintf(stderr, "failed to zero device end %d\n", ret);
|
2007-03-21 00:35:03 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
2008-02-15 16:19:26 +00:00
|
|
|
|
2008-03-24 19:03:18 +00:00
|
|
|
for (i = 0; i < 6; i++)
|
2008-01-04 15:38:22 +00:00
|
|
|
blocks[i] = BTRFS_SUPER_INFO_OFFSET + leafsize * i;
|
2008-02-15 16:19:26 +00:00
|
|
|
|
2008-03-24 19:03:18 +00:00
|
|
|
ret = make_btrfs(fd, file, blocks, block_count, nodesize, leafsize,
|
2008-01-04 15:38:22 +00:00
|
|
|
sectorsize, stripesize);
|
2007-03-21 00:35:03 +00:00
|
|
|
if (ret) {
|
|
|
|
fprintf(stderr, "error during mkfs %d\n", ret);
|
|
|
|
exit(1);
|
|
|
|
}
|
2007-03-21 15:13:29 +00:00
|
|
|
ret = make_root_dir(fd);
|
|
|
|
if (ret) {
|
|
|
|
fprintf(stderr, "failed to setup the root directory\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
2007-10-15 20:25:14 +00: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 00:35:03 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|