Commit 2e635a27 authored by Chris Mason's avatar Chris Mason Committed by David Woodhouse

Btrfs: initial move to kernel module land

Signed-off-by: default avatarChris Mason <chris.mason@oracle.com>
parent 1261ec42
CC=gcc
CFLAGS = -g -Wall -Werror
headers = radix-tree.h ctree.h disk-io.h kerncompat.h print-tree.h list.h \
transaction.h
objects = ctree.o disk-io.o radix-tree.o extent-tree.o print-tree.o \
root-tree.o dir-item.o hash.o file-item.o inode-item.o \
inode-map.o \
ifneq ($(KERNELRELEASE),)
# kbuild part of makefile
# if you don't have sparse installed, use ls instead
CHECKFLAGS=-D__linux__ -Dlinux -D__STDC__ -Dunix -D__unix__ -Wbitwise \
-Wcontext -Wcast-truncate -Wuninitialized -Wshadow -Wundef
check=sparse $(CHECKFLAGS)
#check=ls
obj-m := btrfs.o
btrfs-y := super.o
.c.o:
$(check) $<
$(CC) $(CFLAGS) -c $<
#btrfs-y := ctree.o disk-io.o radix-tree.o extent-tree.o print-tree.o \
# root-tree.o dir-item.o hash.o file-item.o inode-item.o \
# inode-map.o \
all: tester debug-tree quick-test dir-test tags mkfs.btrfs
mkfs.btrfs: $(objects) mkfs.o
gcc $(CFLAGS) -o mkfs.btrfs $(objects) mkfs.o
debug-tree: $(objects) debug-tree.o
gcc $(CFLAGS) -o debug-tree $(objects) debug-tree.o
tester: $(objects) random-test.o
gcc $(CFLAGS) -o tester $(objects) random-test.o
dir-test: $(objects) dir-test.o
gcc $(CFLAGS) -o dir-test $(objects) dir-test.o
quick-test: $(objects) quick-test.o
gcc $(CFLAGS) -o quick-test $(objects) quick-test.o
$(objects): $(headers)
clean :
rm debug-tree tester *.o
else
# Normal Makefile
KERNELDIR := /lib/modules/`uname -r`/build
all::
$(MAKE) -C $(KERNELDIR) M=`pwd` modules
clean::
rm *.o btrfs.ko
endif
#include <stdio.h>
#include <stdlib.h>
#include "kerncompat.h"
#include "radix-tree.h"
#include <linux/module.h>
#include "ctree.h"
#include "disk-io.h"
#include "print-tree.h"
static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root
*root, struct btrfs_path *path, int level);
......
#ifndef __BTRFS__
#define __BTRFS__
#include "list.h"
#include "kerncompat.h"
struct btrfs_trans_handle;
#define BTRFS_MAGIC "_BtRfS_M"
......@@ -75,6 +72,7 @@ struct btrfs_super_block {
__le64 root;
__le64 total_blocks;
__le64 blocks_used;
__le64 root_dir_objectid;
} __attribute__ ((__packed__));
/*
......@@ -693,6 +691,17 @@ static inline void btrfs_set_super_blocksize(struct btrfs_super_block *s,
s->blocksize = cpu_to_le32(val);
}
static inline u64 btrfs_super_root_dir(struct btrfs_super_block *s)
{
return le64_to_cpu(s->root_dir_objectid);
}
static inline void btrfs_set_super_root_dir(struct btrfs_super_block *s, u64
val)
{
s->root_dir_objectid = cpu_to_le64(val);
}
static inline u8 *btrfs_leaf_data(struct btrfs_leaf *l)
{
return (u8 *)l->items;
......
#include <stdio.h>
#include <stdlib.h>
#include "kerncompat.h"
#include "radix-tree.h"
#include "ctree.h"
#include "disk-io.h"
#include "print-tree.h"
#include "transaction.h"
int main(int ac, char **av) {
struct btrfs_super_block super;
struct btrfs_root *root;
if (ac != 2) {
fprintf(stderr, "usage: %s device\n", av[0]);
exit(1);
}
radix_tree_init();
root = open_ctree(av[1], &super);
if (!root) {
fprintf(stderr, "unable to open %s\n", av[1]);
exit(1);
}
printf("fs tree\n");
btrfs_print_tree(root, root->node);
printf("map tree\n");
btrfs_print_tree(root->fs_info->extent_root,
root->fs_info->extent_root->node);
printf("inode tree\n");
btrfs_print_tree(root->fs_info->inode_root,
root->fs_info->inode_root->node);
printf("root tree\n");
btrfs_print_tree(root->fs_info->tree_root,
root->fs_info->tree_root->node);
printf("total blocks %Lu\n", btrfs_super_total_blocks(&super));
printf("blocks used %Lu\n", btrfs_super_blocks_used(&super));
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include "kerncompat.h"
#include "radix-tree.h"
#include <linux/module.h>
#include "ctree.h"
#include "disk-io.h"
#include "hash.h"
......@@ -21,7 +18,12 @@ int btrfs_insert_dir_item(struct btrfs_trans_handle *trans, struct btrfs_root
key.objectid = dir;
key.flags = 0;
btrfs_set_key_type(&key, BTRFS_DIR_ITEM_KEY);
ret = btrfs_name_hash(name, name_len, &key.offset);
if (name_len == 1 && *name == '.')
key.offset = 1;
else if (name_len == 2 && name[0] == '.' && name[1] == '.')
key.offset = 2;
else
ret = btrfs_name_hash(name, name_len, &key.offset);
BUG_ON(ret);
btrfs_init_path(&path);
data_size = sizeof(*dir_item) + name_len;
......
This diff is collapsed.
......@@ -266,20 +266,25 @@ static int find_and_setup_root(struct btrfs_super_block *super,
}
struct btrfs_root *open_ctree(char *filename, struct btrfs_super_block *super)
{
int fp;
fp = open(filename, O_CREAT | O_RDWR, 0600);
if (fp < 0) {
return NULL;
}
return open_ctree_fd(fp, super);
}
struct btrfs_root *open_ctree_fd(int fp, struct btrfs_super_block *super)
{
struct btrfs_root *root = malloc(sizeof(struct btrfs_root));
struct btrfs_root *extent_root = malloc(sizeof(struct btrfs_root));
struct btrfs_root *tree_root = malloc(sizeof(struct btrfs_root));
struct btrfs_root *inode_root = malloc(sizeof(struct btrfs_root));
struct btrfs_fs_info *fs_info = malloc(sizeof(*fs_info));
int fp;
int ret;
fp = open(filename, O_CREAT | O_RDWR, 0600);
if (fp < 0) {
free(root);
return NULL;
}
INIT_RADIX_TREE(&fs_info->cache_radix, GFP_KERNEL);
INIT_RADIX_TREE(&fs_info->pinned_radix, GFP_KERNEL);
INIT_LIST_HEAD(&fs_info->trans);
......
......@@ -24,6 +24,7 @@ int clean_tree_block(struct btrfs_trans_handle *trans,
int btrfs_commit_transaction(struct btrfs_trans_handle *trans, struct btrfs_root
*root, struct btrfs_super_block *s);
struct btrfs_root *open_ctree(char *filename, struct btrfs_super_block *s);
struct btrfs_root *open_ctree_fd(int fp, struct btrfs_super_block *super);
int close_ctree(struct btrfs_root *root, struct btrfs_super_block *s);
void btrfs_block_release(struct btrfs_root *root, struct btrfs_buffer *buf);
int write_ctree_super(struct btrfs_trans_handle *trans, struct btrfs_root *root,
......
#include <stdio.h>
#include <stdlib.h>
#include "kerncompat.h"
#include "radix-tree.h"
#include <linux/module.h>
#include <linux/radix-tree.h>
#include "ctree.h"
#include "disk-io.h"
#include "print-tree.h"
......@@ -183,9 +181,9 @@ static int __free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
btrfs_init_path(&path);
ret = btrfs_search_slot(trans, extent_root, &key, &path, -1, 1);
if (ret) {
printf("failed to find %Lu\n", key.objectid);
printk("failed to find %Lu\n", key.objectid);
btrfs_print_tree(extent_root, extent_root->node);
printf("failed to find %Lu\n", key.objectid);
printk("failed to find %Lu\n", key.objectid);
BUG();
}
ei = btrfs_item_ptr(&path.nodes[0]->leaf, path.slots[0],
......
#include <stdio.h>
#include <stdlib.h>
#include "kerncompat.h"
#include "radix-tree.h"
#include <linux/module.h>
#include "ctree.h"
#include "disk-io.h"
#include "transaction.h"
int btrfs_create_file(struct btrfs_trans_handle *trans,
......
......@@ -10,7 +10,6 @@
* License.
*/
#include "kerncompat.h"
#define DELTA 0x9E3779B9
static void TEA_transform(__u32 buf[2], __u32 const in[])
......
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "kerncompat.h"
#include "hash.h"
int main() {
u64 result;
int ret;
char line[255];
char *p;
while(1) {
p = fgets(line, 255, stdin);
if (!p)
break;
if (strlen(line) == 0)
continue;
ret = btrfs_name_hash(line, strlen(line), &result);
BUG_ON(ret);
printf("hash returns %Lu\n", result);
}
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include "kerncompat.h"
#include "radix-tree.h"
#include <linux/module.h>
#include "ctree.h"
#include "disk-io.h"
#include "transaction.h"
......
#include <stdio.h>
#include <stdlib.h>
#include "kerncompat.h"
#include "radix-tree.h"
#include <linux/module.h>
#include "ctree.h"
#include "disk-io.h"
#include "transaction.h"
......
#ifndef __KERNCOMPAT
#define __KERNCOMPAT
#define gfp_t int
#define get_cpu_var(p) (p)
#define __get_cpu_var(p) (p)
#define BITS_PER_LONG 64
#define __GFP_BITS_SHIFT 20
#define __GFP_BITS_MASK ((int)((1 << __GFP_BITS_SHIFT) - 1))
#define GFP_KERNEL 0
#define __read_mostly
#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
#define PAGE_SHIFT 12
#define ULONG_MAX (~0UL)
#define BUG() abort()
#ifdef __CHECKER__
#define __force __attribute__((force))
#define __bitwise__ __attribute__((bitwise))
#else
#define __force
#define __bitwise__
#endif
typedef unsigned int u32;
typedef u32 __u32;
typedef unsigned long long u64;
typedef unsigned char u8;
typedef unsigned short u16;
typedef unsigned long pgoff_t;
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct vma_shared { int prio_tree_node; };
struct vm_area_struct {
unsigned long vm_pgoff;
unsigned long vm_start;
unsigned long vm_end;
struct vma_shared shared;
};
struct page {
unsigned long index;
};
static inline void preempt_enable(void) { do {; } while(0);}
static inline void preempt_disable(void) { do {; } while(0);}
static inline void __set_bit(int bit, unsigned long *map) {
unsigned long *p = map + bit / BITS_PER_LONG;
bit = bit & (BITS_PER_LONG -1);
*p |= 1UL << bit;
}
static inline int test_bit(int bit, unsigned long *map) {
unsigned long *p = map + bit / BITS_PER_LONG;
bit = bit & (BITS_PER_LONG -1);
return *p & (1UL << bit) ? 1 : 0;
}
static inline void __clear_bit(int bit, unsigned long *map) {
unsigned long *p = map + bit / BITS_PER_LONG;
bit = bit & (BITS_PER_LONG -1);
*p &= ~(1UL << bit);
}
#define BUG_ON(c) do { if (c) abort(); } while (0)
#define container_of(ptr, type, member) ({ \
const typeof( ((type *)0)->member ) *__mptr = (ptr); \
(type *)( (char *)__mptr - __builtin_offsetof(type,member) );})
#define ENOMEM 5
#define EEXIST 6
#define __CHECK_ENDIAN__
#ifdef __CHECK_ENDIAN__
#define __bitwise __bitwise__
#else
#define __bitwise
#endif
typedef u16 __bitwise __le16;
typedef u16 __bitwise __be16;
typedef u32 __bitwise __le32;
typedef u32 __bitwise __be32;
typedef u64 __bitwise __le64;
typedef u64 __bitwise __be64;
#define cpu_to_le64(x) ((__force __le64)(u64)(x))
#define le64_to_cpu(x) ((__force u64)(__le64)(x))
#define cpu_to_le32(x) ((__force __le32)(u32)(x))
#define le32_to_cpu(x) ((__force u32)(__le32)(x))
#define cpu_to_le16(x) ((__force __le16)(u16)(x))
#define le16_to_cpu(x) ((__force u16)(__le16)(x))
#endif
This diff is collapsed.
#define _XOPEN_SOURCE 500
#ifndef __CHECKER__
#include <sys/ioctl.h>
#include <sys/mount.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include "kerncompat.h"
#include "radix-tree.h"
#include "ctree.h"
#include "disk-io.h"
#ifdef __CHECKER__
#define BLKGETSIZE64 0
static inline int ioctl(int fd, int define, u64 *size) { return 0; }
#endif
#if 0
#if defined(__linux__) && defined(_IOR) && !defined(BLKGETSIZE64)
# define BLKGETSIZE64 _IOR(0x12, 114, __u64)
#endif
#endif
int mkfs(int fd, u64 num_blocks, u32 blocksize)
{
struct btrfs_super_block super;
struct btrfs_leaf *empty_leaf;
struct btrfs_root_item root_item;
struct btrfs_item item;
struct btrfs_extent_item extent_item;
char *block;
int ret;
u32 itemoff;
u32 start_block = BTRFS_SUPER_INFO_OFFSET / blocksize;
btrfs_set_super_blocknr(&super, start_block);
btrfs_set_super_root(&super, start_block + 1);
strcpy((char *)(&super.magic), BTRFS_MAGIC);
btrfs_set_super_blocksize(&super, blocksize);
btrfs_set_super_total_blocks(&super, num_blocks);
btrfs_set_super_blocks_used(&super, start_block + 5);
block = malloc(blocksize);
memset(block, 0, blocksize);
BUG_ON(sizeof(super) > blocksize);
memcpy(block, &super, sizeof(super));
ret = pwrite(fd, block, blocksize, BTRFS_SUPER_INFO_OFFSET);
BUG_ON(ret != blocksize);
/* create the tree of root objects */
empty_leaf = malloc(blocksize);
memset(empty_leaf, 0, blocksize);
btrfs_set_header_parentid(&empty_leaf->header,
BTRFS_ROOT_TREE_OBJECTID);
btrfs_set_header_blocknr(&empty_leaf->header, start_block + 1);
btrfs_set_header_nritems(&empty_leaf->header, 3);
/* create the items for the root tree */
btrfs_set_root_blocknr(&root_item, start_block + 2);
btrfs_set_root_refs(&root_item, 1);
itemoff = __BTRFS_LEAF_DATA_SIZE(blocksize) - sizeof(root_item);
btrfs_set_item_offset(&item, itemoff);
btrfs_set_item_size(&item, sizeof(root_item));
btrfs_set_disk_key_objectid(&item.key, BTRFS_EXTENT_TREE_OBJECTID);
btrfs_set_disk_key_offset(&item.key, 0);
btrfs_set_disk_key_flags(&item.key, 0);
btrfs_set_disk_key_type(&item.key, BTRFS_ROOT_ITEM_KEY);
memcpy(empty_leaf->items, &item, sizeof(item));
memcpy(btrfs_leaf_data(empty_leaf) + itemoff,
&root_item, sizeof(root_item));
btrfs_set_root_blocknr(&root_item, start_block + 3);
itemoff = itemoff - sizeof(root_item);
btrfs_set_item_offset(&item, itemoff);
btrfs_set_disk_key_objectid(&item.key, BTRFS_INODE_MAP_OBJECTID);
memcpy(empty_leaf->items + 1, &item, sizeof(item));
memcpy(btrfs_leaf_data(empty_leaf) + itemoff,
&root_item, sizeof(root_item));
btrfs_set_root_blocknr(&root_item, start_block + 4);
itemoff = itemoff - sizeof(root_item);
btrfs_set_item_offset(&item, itemoff);
btrfs_set_disk_key_objectid(&item.key, BTRFS_FS_TREE_OBJECTID);
memcpy(empty_leaf->items + 2, &item, sizeof(item));
memcpy(btrfs_leaf_data(empty_leaf) + itemoff,
&root_item, sizeof(root_item));
ret = pwrite(fd, empty_leaf, blocksize, (start_block + 1) * blocksize);
/* create the items for the extent tree */
btrfs_set_header_parentid(&empty_leaf->header,
BTRFS_EXTENT_TREE_OBJECTID);
btrfs_set_header_blocknr(&empty_leaf->header, start_block + 2);
btrfs_set_header_nritems(&empty_leaf->header, 5);
/* item1, reserve blocks 0-16 */
btrfs_set_disk_key_objectid(&item.key, 0);
btrfs_set_disk_key_offset(&item.key, start_block + 1);
btrfs_set_disk_key_flags(&item.key, 0);
btrfs_set_disk_key_type(&item.key, BTRFS_EXTENT_ITEM_KEY);
itemoff = __BTRFS_LEAF_DATA_SIZE(blocksize) -
sizeof(struct btrfs_extent_item);
btrfs_set_item_offset(&item, itemoff);
btrfs_set_item_size(&item, sizeof(struct btrfs_extent_item));
btrfs_set_extent_refs(&extent_item, 1);
btrfs_set_extent_owner(&extent_item, 0);
memcpy(empty_leaf->items, &item, sizeof(item));
memcpy(btrfs_leaf_data(empty_leaf) + btrfs_item_offset(&item),
&extent_item, btrfs_item_size(&item));
/* item2, give block 17 to the root */
btrfs_set_disk_key_objectid(&item.key, start_block + 1);
btrfs_set_disk_key_offset(&item.key, 1);
itemoff = itemoff - sizeof(struct btrfs_extent_item);
btrfs_set_item_offset(&item, itemoff);
btrfs_set_extent_owner(&extent_item, BTRFS_ROOT_TREE_OBJECTID);
memcpy(empty_leaf->items + 1, &item, sizeof(item));
memcpy(btrfs_leaf_data(empty_leaf) + btrfs_item_offset(&item),
&extent_item, btrfs_item_size(&item));
/* item3, give block 18 to the extent root */
btrfs_set_disk_key_objectid(&item.key, start_block + 2);
btrfs_set_disk_key_offset(&item.key, 1);
itemoff = itemoff - sizeof(struct btrfs_extent_item);
btrfs_set_item_offset(&item, itemoff);
btrfs_set_extent_owner(&extent_item, BTRFS_EXTENT_TREE_OBJECTID);
memcpy(empty_leaf->items + 2, &item, sizeof(item));
memcpy(btrfs_leaf_data(empty_leaf) + btrfs_item_offset(&item),
&extent_item, btrfs_item_size(&item));
/* item4, give block 19 to the inode map */
btrfs_set_disk_key_objectid(&item.key, start_block + 3);
btrfs_set_disk_key_offset(&item.key, 1);
itemoff = itemoff - sizeof(struct btrfs_extent_item);
btrfs_set_item_offset(&item, itemoff);
btrfs_set_extent_owner(&extent_item, BTRFS_INODE_MAP_OBJECTID);
memcpy(empty_leaf->items + 3, &item, sizeof(item));
memcpy(btrfs_leaf_data(empty_leaf) + btrfs_item_offset(&item),
&extent_item, btrfs_item_size(&item));
ret = pwrite(fd, empty_leaf, blocksize, (start_block + 2) * blocksize);
if (ret != blocksize)
return -1;
/* item5, give block 20 to the FS root */
btrfs_set_disk_key_objectid(&item.key, start_block + 4);
btrfs_set_disk_key_offset(&item.key, 1);
itemoff = itemoff - sizeof(struct btrfs_extent_item);
btrfs_set_item_offset(&item, itemoff);
btrfs_set_extent_owner(&extent_item, BTRFS_FS_TREE_OBJECTID);
memcpy(empty_leaf->items + 4, &item, sizeof(item));
memcpy(btrfs_leaf_data(empty_leaf) + btrfs_item_offset(&item),
&extent_item, btrfs_item_size(&item));
ret = pwrite(fd, empty_leaf, blocksize, (start_block + 2) * blocksize);
if (ret != blocksize)
return -1;
/* create the inode map */
btrfs_set_header_parentid(&empty_leaf->header,
BTRFS_INODE_MAP_OBJECTID);
btrfs_set_header_blocknr(&empty_leaf->header, start_block + 3);
btrfs_set_header_nritems(&empty_leaf->header, 0);
ret = pwrite(fd, empty_leaf, blocksize, (start_block + 3) * blocksize);
if (ret != blocksize)
return -1;
/* finally create the FS root */
btrfs_set_header_parentid(&empty_leaf->header, BTRFS_FS_TREE_OBJECTID);
btrfs_set_header_blocknr(&empty_leaf->header, start_block + 4);
btrfs_set_header_nritems(&empty_leaf->header, 0);
ret = pwrite(fd, empty_leaf, blocksize, (start_block + 4) * blocksize);
if (ret != blocksize)
return -1;
return 0;
}
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;
}
return 0;
}
int main(int ac, char **av)
{
char *file;
u64 block_count = 0;
int fd;
struct stat st;
int ret;
int i;
char *buf = malloc(4096);
if (ac >= 2) {
file = av[1];
if (ac == 3) {
block_count = atoi(av[2]);
if (!block_count) {
fprintf(stderr, "error finding block count\n");
exit(1);
}
}
} else {
fprintf(stderr, "usage: mkfs.btrfs file [block count]\n");
exit(1);
}
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);
}
}
block_count /= 4096;
if (block_count < 256) {
fprintf(stderr, "device %s is too small\n", file);
exit(1);
}
memset(buf, 0, 4096);
for(i = 0; i < 6; i++) {
ret = write(fd, buf, 4096);
if (ret != 4096) {
fprintf(stderr, "unable to zero fill device\n");
exit(1);
}
}
ret = mkfs(fd, block_count, 4096);
if (ret) {
fprintf(stderr, "error during mkfs %d\n", ret);
exit(1);
}
printf("fs created on %s blocksize %d blocks %Lu\n",
file, 4096, block_count);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include "kerncompat.h"
#include "radix-tree.h"
#include <linux/module.h>
#include "ctree.h"
#include "disk-io.h"
......@@ -17,14 +14,14 @@ void btrfs_print_leaf(struct btrfs_root *root, struct btrfs_leaf *l)
struct btrfs_inode_item *ii;
u32 type;
printf("leaf %Lu total ptrs %d free space %d\n",
printk("leaf %Lu total ptrs %d free space %d\n",
btrfs_header_blocknr(&l->header), nr,
btrfs_leaf_free_space(root, l));
fflush(stdout);
for (i = 0 ; i < nr ; i++) {
item = l->items + i;
type = btrfs_disk_key_type(&item->key);
printf("\titem %d key (%Lu %u %Lu) itemoff %d itemsize %d\n",
printk("\titem %d key (%Lu %u %Lu) itemoff %d itemsize %d\n",
i,
btrfs_disk_key_objectid(&item->key),
btrfs_disk_key_flags(&item->key),
......@@ -34,38 +31,39 @@ void btrfs_print_leaf(struct btrfs_root *root, struct btrfs_leaf *l)
switch (type) {
case BTRFS_INODE_ITEM_KEY:
ii = btrfs_item_ptr(l, i, struct btrfs_inode_item);
printf("\t\tinode generation %Lu size %Lu\n",
printk("\t\tinode generation %Lu size %Lu mode %o\n",
btrfs_inode_generation(ii),
btrfs_inode_size(ii));
btrfs_inode_size(ii),
btrfs_inode_mode(ii));
break;
case BTRFS_DIR_ITEM_KEY:
di = btrfs_item_ptr(l, i, struct btrfs_dir_item);
printf("\t\tdir oid %Lu flags %u type %u\n",
printk("\t\tdir oid %Lu flags %u type %u\n",
btrfs_dir_objectid(di),
btrfs_dir_flags(di),
btrfs_dir_type(di));
printf("\t\tname %.*s\n",
printk("\t\tname %.*s\n",
btrfs_dir_name_len(di),(char *)(di + 1));
break;
case BTRFS_ROOT_ITEM_KEY:
ri = btrfs_item_ptr(l, i, struct btrfs_root_item);
printf("\t\troot data blocknr %Lu refs %u\n",
printk("\t\troot data blocknr %Lu refs %u\n",
btrfs_root_blocknr(ri), btrfs_root_refs(ri));
break;
case BTRFS_EXTENT_ITEM_KEY:
ei = btrfs_item_ptr(l, i, struct btrfs_extent_item);
printf("\t\textent data refs %u owner %Lu\n",
printk("\t\textent data refs %u owner %Lu\n",
btrfs_extent_refs(ei), btrfs_extent_owner(ei));
break;
case BTRFS_INODE_MAP_ITEM_KEY:
mi = btrfs_item_ptr(l, i, struct btrfs_inode_map_item);
printf("\t\tinode map key %Lu %u %Lu\n",
printk("\t\tinode map key %Lu %u %Lu\n",
btrfs_disk_key_objectid(&mi->key),
btrfs_disk_key_flags(&mi->key),
btrfs_disk_key_offset(&mi->key));
break;
case BTRFS_STRING_ITEM_KEY:
printf("\t\titem data %.*s\n", btrfs_item_size(item),
printk("\t\titem data %.*s\n", btrfs_item_size(item),
btrfs_leaf_data(l) + btrfs_item_offset(item));
break;
};
......@@ -86,12 +84,12 @@ void btrfs_print_tree(struct btrfs_root *root, struct btrfs_buffer *t)
btrfs_print_leaf(root, (struct btrfs_leaf *)c);
return;
}
printf("node %Lu level %d total ptrs %d free spc %u\n", t->blocknr,
printk("node %Lu level %d total ptrs %d free spc %u\n", t->blocknr,
btrfs_header_level(&c->header), nr,
(u32)BTRFS_NODEPTRS_PER_BLOCK(root) - nr);
fflush(stdout);
for (i = 0; i < nr; i++) {
printf("\tkey %d (%Lu %u %Lu) block %Lu\n",
printk("\tkey %d (%Lu %u %Lu) block %Lu\n",
i,
c->ptrs[i].key.objectid,
c->ptrs[i].key.flags,
......
#include <stdio.h>
#include <stdlib.h>
#include "kerncompat.h"
#include "radix-tree.h"
#include "ctree.h"
#include "disk-io.h"
#include "print-tree.h"
#include "transaction.h"
/* for testing only */
int next_key(int i, int max_key) {
return rand() % max_key;
// return i;
}
int main(int ac, char **av) {
struct btrfs_key ins;
struct btrfs_key last = { (u64)-1, 0, 0};
char *buf;
int i;
int num;
int ret;
int run_size = 100000;
int max_key = 100000000;
int tree_size = 0;
struct btrfs_path path;
struct btrfs_super_block super;
struct btrfs_root *root;
struct btrfs_trans_handle *trans;
radix_tree_init();
root = open_ctree("dbfile", &super);
trans = btrfs_start_transaction(root, 1);
srand(55);
ins.flags = 0;
btrfs_set_key_type(&ins, BTRFS_STRING_ITEM_KEY);
for (i = 0; i < run_size; i++) {
buf = malloc(64);
num = next_key(i, max_key);
// num = i;
sprintf(buf, "string-%d", num);
if (i % 10000 == 0)
fprintf(stderr, "insert %d:%d\n", num, i);
ins.objectid = num;
ins.offset = 0;
ret = btrfs_insert_item(trans, root, &ins, buf, strlen(buf));
if (!ret)
tree_size++;
free(buf);
if (i == run_size - 5) {
btrfs_commit_transaction(trans, root, &super);
}
}
close_ctree(root, &super);
root = open_ctree("dbfile", &super);
printf("starting search\n");
srand(55);
for (i = 0; i < run_size; i++) {
num = next_key(i, max_key);
ins.objectid = num;
btrfs_init_path(&path);
if (i % 10000 == 0)
fprintf(stderr, "search %d:%d\n", num, i);
ret = btrfs_search_slot(trans, root, &ins, &path, 0, 0);
if (ret) {
btrfs_print_tree(root, root->node);
printf("unable to find %d\n", num);
exit(1);
}
btrfs_release_path(root, &path);
}
close_ctree(root, &super);
root = open_ctree("dbfile", &super);
printf("node %p level %d total ptrs %d free spc %lu\n", root->node,
btrfs_header_level(&root->node->node.header),
btrfs_header_nritems(&root->node->node.header),
BTRFS_NODEPTRS_PER_BLOCK(root) -
btrfs_header_nritems(&root->node->node.header));
printf("all searches good, deleting some items\n");
i = 0;
srand(55);
for (i = 0 ; i < run_size/4; i++) {
num = next_key(i, max_key);
ins.objectid = num;
btrfs_init_path(&path);
ret = btrfs_search_slot(trans, root, &ins, &path, -1, 1);
if (!ret) {
if (i % 10000 == 0)
fprintf(stderr, "del %d:%d\n", num, i);
ret = btrfs_del_item(trans, root, &path);
if (ret != 0)
BUG();
tree_size--;
}
btrfs_release_path(root, &path);
}
close_ctree(root, &super);
root = open_ctree("dbfile", &super);
srand(128);
for (i = 0; i < run_size; i++) {
buf = malloc(64);
num = next_key(i, max_key);
sprintf(buf, "string-%d", num);
ins.objectid = num;
if (i % 10000 == 0)
fprintf(stderr, "insert %d:%d\n", num, i);
ret = btrfs_insert_item(trans, root, &ins, buf, strlen(buf));
if (!ret)
tree_size++;
free(buf);
}
close_ctree(root, &super);
root = open_ctree("dbfile", &super);
srand(128);
printf("starting search2\n");
for (i = 0; i < run_size; i++) {
num = next_key(i, max_key);
ins.objectid = num;
btrfs_init_path(&path);
if (i % 10000 == 0)
fprintf(stderr, "search %d:%d\n", num, i);
ret = btrfs_search_slot(trans, root, &ins, &path, 0, 0);
if (ret) {
btrfs_print_tree(root, root->node);
printf("unable to find %d\n", num);
exit(1);
}
btrfs_release_path(root, &path);
}
printf("starting big long delete run\n");
while(root->node &&
btrfs_header_nritems(&root->node->node.header) > 0) {
struct btrfs_leaf *leaf;
int slot;
ins.objectid = (u64)-1;
btrfs_init_path(&path);
ret = btrfs_search_slot(trans, root, &ins, &path, -1, 1);
if (ret == 0)
BUG();
leaf = &path.nodes[0]->leaf;
slot = path.slots[0];
if (slot != btrfs_header_nritems(&leaf->header))
BUG();
while(path.slots[0] > 0) {
path.slots[0] -= 1;
slot = path.slots[0];
leaf = &path.nodes[0]->leaf;
btrfs_disk_key_to_cpu(&last, &leaf->items[slot].key);
if (tree_size % 10000 == 0)
printf("big del %d:%d\n", tree_size, i);
ret = btrfs_del_item(trans, root, &path);
if (ret != 0) {
printf("del_item returned %d\n", ret);
BUG();
}
tree_size--;
}
btrfs_release_path(root, &path);
}
/*
printf("previous tree:\n");
btrfs_print_tree(root, root->commit_root);
printf("map before commit\n");
btrfs_print_tree(root->extent_root, root->extent_root->node);
*/
btrfs_commit_transaction(trans, root, &super);
printf("tree size is now %d\n", tree_size);
printf("root %p commit root %p\n", root->node, root->commit_root);
printf("map tree\n");
btrfs_print_tree(root->fs_info->extent_root,
root->fs_info->extent_root->node);
close_ctree(root, &super);
return 0;
}
This diff is collapsed.
/*
* Copyright (C) 2001 Momchil Velikov
* Portions Copyright (C) 2001 Christoph Hellwig
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2, or (at
* your option) any later version.
*
* 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., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#ifndef _LINUX_RADIX_TREE_H
#define _LINUX_RADIX_TREE_H
#define RADIX_TREE_MAX_TAGS 2
/* root tags are stored in gfp_mask, shifted by __GFP_BITS_SHIFT */
struct radix_tree_root {
unsigned int height;
gfp_t gfp_mask;
struct radix_tree_node *rnode;
};
#define RADIX_TREE_INIT(mask) { \
.height = 0, \
.gfp_mask = (mask), \
.rnode = NULL, \
}
#define RADIX_TREE(name, mask) \
struct radix_tree_root name = RADIX_TREE_INIT(mask)
#define INIT_RADIX_TREE(root, mask) \
do { \
(root)->height = 0; \
(root)->gfp_mask = (mask); \
(root)->rnode = NULL; \
} while (0)
int radix_tree_insert(struct radix_tree_root *, unsigned long, void *);
void *radix_tree_lookup(struct radix_tree_root *, unsigned long);
void **radix_tree_lookup_slot(struct radix_tree_root *, unsigned long);
void *radix_tree_delete(struct radix_tree_root *, unsigned long);
unsigned int
radix_tree_gang_lookup(struct radix_tree_root *root, void **results,
unsigned long first_index, unsigned int max_items);
int radix_tree_preload(gfp_t gfp_mask);
void radix_tree_init(void);
void *radix_tree_tag_set(struct radix_tree_root *root,
unsigned long index, unsigned int tag);
void *radix_tree_tag_clear(struct radix_tree_root *root,
unsigned long index, unsigned int tag);
int radix_tree_tag_get(struct radix_tree_root *root,
unsigned long index, unsigned int tag);
unsigned int
radix_tree_gang_lookup_tag(struct radix_tree_root *root, void **results,
unsigned long first_index, unsigned int max_items,
unsigned int tag);
int radix_tree_tagged(struct radix_tree_root *root, unsigned int tag);
static inline void radix_tree_preload_end(void)
{
preempt_enable();
}
#endif /* _LINUX_RADIX_TREE_H */
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include "kerncompat.h"
#include "radix-tree.h"
#include "ctree.h"
#include "disk-io.h"
#include "print-tree.h"
#include "transaction.h"
int keep_running = 1;
struct btrfs_super_block super;
static int setup_key(struct radix_tree_root *root, struct btrfs_key *key,
int exists)
{
int num = rand();
unsigned long res[2];
int ret;
key->flags = 0;
btrfs_set_key_type(key, BTRFS_STRING_ITEM_KEY);
key->offset = 0;
again:
ret = radix_tree_gang_lookup(root, (void **)res, num, 2);
if (exists) {
if (ret == 0)
return -1;
num = res[0];
} else if (ret != 0 && num == res[0]) {
num++;
if (ret > 1 && num == res[1]) {
num++;
goto again;
}
}
key->objectid = num;
return 0;
}
static int ins_one(struct btrfs_trans_handle *trans, struct btrfs_root *root,
struct radix_tree_root *radix)
{
struct btrfs_path path;
struct btrfs_key key;
int ret;
char buf[128];
unsigned long oid;
btrfs_init_path(&path);
ret = setup_key(radix, &key, 0);
sprintf(buf, "str-%Lu\n", key.objectid);
ret = btrfs_insert_item(trans, root, &key, buf, strlen(buf));
if (ret)
goto error;
oid = (unsigned long)key.objectid;
radix_tree_preload(GFP_KERNEL);
ret = radix_tree_insert(radix, oid, (void *)oid);
radix_tree_preload_end();
if (ret)
goto error;
return ret;
error:
printf("failed to insert %Lu\n", key.objectid);
return -1;
}
static int insert_dup(struct btrfs_trans_handle *trans, struct btrfs_root
*root, struct radix_tree_root *radix)
{
struct btrfs_path path;
struct btrfs_key key;
int ret;
char buf[128];
btrfs_init_path(&path);
ret = setup_key(radix, &key, 1);
if (ret < 0)
return 0;
sprintf(buf, "str-%Lu\n", key.objectid);
ret = btrfs_insert_item(trans, root, &key, buf, strlen(buf));
if (ret != -EEXIST) {
printf("insert on %Lu gave us %d\n", key.objectid, ret);
return 1;
}
return 0;
}
static int del_one(struct btrfs_trans_handle *trans, struct btrfs_root *root,
struct radix_tree_root *radix)
{
struct btrfs_path path;
struct btrfs_key key;
int ret;
unsigned long *ptr;
btrfs_init_path(&path);
ret = setup_key(radix, &key, 1);
if (ret < 0)
return 0;
ret = btrfs_search_slot(trans, root, &key, &path, -1, 1);
if (ret)
goto error;
ret = btrfs_del_item(trans, root, &path);
btrfs_release_path(root, &path);
if (ret != 0)
goto error;
ptr = radix_tree_delete(radix, key.objectid);
if (!ptr)
goto error;
return 0;
error:
printf("failed to delete %Lu\n", key.objectid);
return -1;
}
static int lookup_item(struct btrfs_trans_handle *trans, struct btrfs_root
*root, struct radix_tree_root *radix)
{
struct btrfs_path path;
struct btrfs_key key;
int ret;
btrfs_init_path(&path);
ret = setup_key(radix, &key, 1);
if (ret < 0)
return 0;
ret = btrfs_search_slot(trans, root, &key, &path, 0, 1);
btrfs_release_path(root, &path);
if (ret)
goto error;
return 0;
error:
printf("unable to find key %Lu\n", key.objectid);
return -1;
}
static int lookup_enoent(struct btrfs_trans_handle *trans, struct btrfs_root
*root, struct radix_tree_root *radix)
{
struct btrfs_path path;
struct btrfs_key key;
int ret;
btrfs_init_path(&path);
ret = setup_key(radix, &key, 0);
if (ret < 0)
return ret;
ret = btrfs_search_slot(trans, root, &key, &path, 0, 0);
btrfs_release_path(root, &path);
if (ret <= 0)
goto error;
return 0;
error:
printf("able to find key that should not exist %Lu\n", key.objectid);
return -1;
}
static int empty_tree(struct btrfs_trans_handle *trans, struct btrfs_root
*root, struct radix_tree_root *radix, int nr)
{
struct btrfs_path path;
struct btrfs_key key;
unsigned long found = 0;
int ret;
int slot;
int *ptr;
int count = 0;
key.offset = 0;
key.flags = 0;
btrfs_set_key_type(&key, BTRFS_STRING_ITEM_KEY);
key.objectid = (unsigned long)-1;
while(nr-- >= 0) {
btrfs_init_path(&path);
ret = btrfs_search_slot(trans, root, &key, &path, -1, 1);
if (ret < 0) {
btrfs_release_path(root, &path);
return ret;
}
if (ret != 0) {
if (path.slots[0] == 0) {
btrfs_release_path(root, &path);
break;
}
path.slots[0] -= 1;
}
slot = path.slots[0];
found = btrfs_disk_key_objectid(
&path.nodes[0]->leaf.items[slot].key);
ret = btrfs_del_item(trans, root, &path);
count++;
if (ret) {
fprintf(stderr,
"failed to remove %lu from tree\n",
found);
return -1;
}
btrfs_release_path(root, &path);
ptr = radix_tree_delete(radix, found);
if (!ptr)
goto error;
if (!keep_running)
break;
}
return 0;
error:
fprintf(stderr, "failed to delete from the radix %lu\n", found);
return -1;
}
static int fill_tree(struct btrfs_trans_handle *trans, struct btrfs_root *root,
struct radix_tree_root *radix, int count)
{
int i;
int ret = 0;
for (i = 0; i < count; i++) {
ret = ins_one(trans, root, radix);
if (ret) {
fprintf(stderr, "fill failed\n");
goto out;
}
if (i % 1000 == 0) {
ret = btrfs_commit_transaction(trans, root, &super);
if (ret) {
fprintf(stderr, "fill commit failed\n");
return ret;
}
}
if (i && i % 10000 == 0) {
printf("bigfill %d\n", i);
}
if (!keep_running)
break;
}
out:
return ret;
}
static int bulk_op(struct btrfs_trans_handle *trans, struct btrfs_root *root,
struct radix_tree_root *radix)
{
int ret;
int nr = rand() % 5000;
static int run_nr = 0;
/* do the bulk op much less frequently */
if (run_nr++ % 100)
return 0;
ret = empty_tree(trans, root, radix, nr);
if (ret)
return ret;
ret = fill_tree(trans, root, radix, nr);
if (ret)
return ret;
return 0;
}
int (*ops[])(struct btrfs_trans_handle *,
struct btrfs_root *root, struct radix_tree_root *radix) =
{ ins_one, insert_dup, del_one, lookup_item,
lookup_enoent, bulk_op };
static int fill_radix(struct btrfs_root *root, struct radix_tree_root *radix)
{
struct btrfs_path path;
struct btrfs_key key;
unsigned long found;
int ret;
int slot;
int i;
key.offset = 0;
key.flags = 0;
btrfs_set_key_type(&key, BTRFS_STRING_ITEM_KEY);
key.objectid = (unsigned long)-1;
while(1) {
btrfs_init_path(&path);
ret = btrfs_search_slot(NULL, root, &key, &path, 0, 0);
if (ret < 0) {
btrfs_release_path(root, &path);
return ret;
}
slot = path.slots[0];
if (ret != 0) {
if (slot == 0) {
btrfs_release_path(root, &path);
break;
}
slot -= 1;
}
for (i = slot; i >= 0; i--) {
found = btrfs_disk_key_objectid(&path.nodes[0]->
leaf.items[i].key);
radix_tree_preload(GFP_KERNEL);
ret = radix_tree_insert(radix, found, (void *)found);
if (ret) {
fprintf(stderr,
"failed to insert %lu into radix\n",
found);
exit(1);
}
radix_tree_preload_end();
}
btrfs_release_path(root, &path);
key.objectid = found - 1;
if (key.objectid > found)
break;
}
return 0;
}
void sigstopper(int ignored)
{
keep_running = 0;
fprintf(stderr, "caught exit signal, stopping\n");
}
int print_usage(void)
{
printf("usage: tester [-ih] [-c count] [-f count]\n");
printf("\t -c count -- iteration count after filling\n");
printf("\t -f count -- run this many random inserts before starting\n");
printf("\t -i -- only do initial fill\n");
printf("\t -h -- this help text\n");
exit(1);
}
int main(int ac, char **av)
{
RADIX_TREE(radix, GFP_KERNEL);
struct btrfs_root *root;
int i;
int ret;
int count;
int op;
int iterations = 20000;
int init_fill_count = 800000;
int err = 0;
int initial_only = 0;
struct btrfs_trans_handle *trans;
radix_tree_init();
root = open_ctree("dbfile", &super);
fill_radix(root, &radix);
signal(SIGTERM, sigstopper);
signal(SIGINT, sigstopper);
for (i = 1 ; i < ac ; i++) {
if (strcmp(av[i], "-i") == 0) {
initial_only = 1;
} else if (strcmp(av[i], "-c") == 0) {
iterations = atoi(av[i+1]);
i++;
} else if (strcmp(av[i], "-f") == 0) {
init_fill_count = atoi(av[i+1]);
i++;
} else {
print_usage();
}
}
printf("initial fill\n");
trans = btrfs_start_transaction(root, 1);
ret = fill_tree(trans, root, &radix, init_fill_count);
printf("starting run\n");
if (ret) {
err = ret;
goto out;
}
if (initial_only == 1) {
goto out;
}
for (i = 0; i < iterations; i++) {
op = rand() % ARRAY_SIZE(ops);
count = rand() % 128;
if (i % 2000 == 0) {
printf("%d\n", i);
fflush(stdout);
}
if (i && i % 5000 == 0) {
printf("open & close, root level %d nritems %d\n",
btrfs_header_level(&root->node->node.header),
btrfs_header_nritems(&root->node->node.header));
close_ctree(root, &super);
root = open_ctree("dbfile", &super);
}
while(count--) {
ret = ops[op](trans, root, &radix);
if (ret) {
fprintf(stderr, "op %d failed %d:%d\n",
op, i, iterations);
btrfs_print_tree(root, root->node);
fprintf(stderr, "op %d failed %d:%d\n",
op, i, iterations);
err = ret;
goto out;
}
if (ops[op] == bulk_op)
break;
if (keep_running == 0) {
err = 0;
goto out;
}
}
}
out:
close_ctree(root, &super);
return err;
}
#include <stdio.h>
#include <stdlib.h>
#include "kerncompat.h"
#include "radix-tree.h"
#include <linux/module.h>
#include "ctree.h"
#include "disk-io.h"
#include "print-tree.h"
......
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/pagemap.h>
#include <linux/highmem.h>
#include <linux/time.h>
#include <linux/init.h>
#include <linux/string.h>
#include <linux/smp_lock.h>
#include <linux/backing-dev.h>
#include "ctree.h"
#define BTRFS_SUPER_MAGIC 0x9123682E
#if 0
/* some random number */
static struct super_operations ramfs_ops;
static struct inode_operations ramfs_dir_inode_operations;
static struct backing_dev_info ramfs_backing_dev_info = {
.ra_pages = 0, /* No readahead */
.capabilities = BDI_CAP_NO_ACCT_DIRTY | BDI_CAP_NO_WRITEBACK |
BDI_CAP_MAP_DIRECT | BDI_CAP_MAP_COPY |
BDI_CAP_READ_MAP | BDI_CAP_WRITE_MAP | BDI_CAP_EXEC_MAP,
};
struct inode *ramfs_get_inode(struct super_block *sb, int mode, dev_t dev)
{
struct inode * inode = new_inode(sb);
if (inode) {
inode->i_mode = mode;
inode->i_uid = current->fsuid;
inode->i_gid = current->fsgid;
inode->i_blocks = 0;
inode->i_mapping->a_ops = &ramfs_aops;
inode->i_mapping->backing_dev_info = &ramfs_backing_dev_info;
inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
switch (mode & S_IFMT) {
default:
init_special_inode(inode, mode, dev);
break;
case S_IFREG:
inode->i_op = &ramfs_file_inode_operations;
inode->i_fop = &ramfs_file_operations;
break;
case S_IFDIR:
inode->i_op = &ramfs_dir_inode_operations;
inode->i_fop = &simple_dir_operations;
/* directory inodes start off with i_nlink == 2 (for "." entry) */
inc_nlink(inode);
break;
case S_IFLNK:
inode->i_op = &page_symlink_inode_operations;
break;
}
}
return inode;
}
/*
* File creation. Allocate an inode, and we're done..
*/
/* SMP-safe */
static int
ramfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
{
struct inode * inode = ramfs_get_inode(dir->i_sb, mode, dev);
int error = -ENOSPC;
if (inode) {
if (dir->i_mode & S_ISGID) {
inode->i_gid = dir->i_gid;
if (S_ISDIR(mode))
inode->i_mode |= S_ISGID;
}
d_instantiate(dentry, inode);
dget(dentry); /* Extra count - pin the dentry in core */
error = 0;
dir->i_mtime = dir->i_ctime = CURRENT_TIME;
}
return error;
}
static int ramfs_mkdir(struct inode * dir, struct dentry * dentry, int mode)
{
int retval = ramfs_mknod(dir, dentry, mode | S_IFDIR, 0);
if (!retval)
inc_nlink(dir);
return retval;
}
static int ramfs_create(struct inode *dir, struct dentry *dentry, int mode, struct nameidata *nd)
{
return ramfs_mknod(dir, dentry, mode | S_IFREG, 0);
}
static int ramfs_symlink(struct inode * dir, struct dentry *dentry, const char * symname)
{
struct inode *inode;
int error = -ENOSPC;
inode = ramfs_get_inode(dir->i_sb, S_IFLNK|S_IRWXUGO, 0);
if (inode) {
int l = strlen(symname)+1;
error = page_symlink(inode, symname, l);
if (!error) {
if (dir->i_mode & S_ISGID)
inode->i_gid = dir->i_gid;
d_instantiate(dentry, inode);
dget(dentry);
dir->i_mtime = dir->i_ctime = CURRENT_TIME;
} else
iput(inode);
}
return error;
}
static struct inode_operations ramfs_dir_inode_operations = {
.create = ramfs_create,
.lookup = simple_lookup,
.link = simple_link,
.unlink = simple_unlink,
.symlink = ramfs_symlink,
.mkdir = ramfs_mkdir,
.rmdir = simple_rmdir,
.mknod = ramfs_mknod,
.rename = simple_rename,
};
#endif
struct inode *btrfs_get_inode(struct super_block *sb, int mode, dev_t dev)
{
struct inode * inode = new_inode(sb);
if (inode) {
inode->i_mode = mode;
inode->i_uid = current->fsuid;
inode->i_gid = current->fsgid;
inode->i_blocks = 0;
inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
}
return inode;
}
static struct super_operations btrfs_ops = {
.statfs = simple_statfs,
.drop_inode = generic_delete_inode,
};
static int btrfs_fill_super(struct super_block * sb, void * data, int silent)
{
struct inode * inode;
struct dentry * root;
sb->s_maxbytes = MAX_LFS_FILESIZE;
sb->s_blocksize = PAGE_CACHE_SIZE;
sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
sb->s_magic = BTRFS_SUPER_MAGIC;
sb->s_op = &btrfs_ops;
sb->s_time_gran = 1;
inode = btrfs_get_inode(sb, S_IFDIR | 0755, 0);
if (!inode)
return -ENOMEM;
root = d_alloc_root(inode);
if (!root) {
iput(inode);
return -ENOMEM;
}
sb->s_root = root;
return 0;
}
static int btrfs_get_sb(struct file_system_type *fs_type,
int flags, const char *dev_name, void *data, struct vfsmount *mnt)
{
return get_sb_bdev(fs_type, flags, dev_name, data,
btrfs_fill_super, mnt);
}
static struct file_system_type btrfs_fs_type = {
.owner = THIS_MODULE,
.name = "btrfs",
.get_sb = btrfs_get_sb,
.kill_sb = kill_block_super,
.fs_flags = FS_REQUIRES_DEV,
};
static int __init init_btrfs_fs(void)
{
printk("btrfs loaded!\n");
return register_filesystem(&btrfs_fs_type);
}
static void __exit exit_btrfs_fs(void)
{
unregister_filesystem(&btrfs_fs_type);
printk("btrfs unloaded\n");
}
module_init(init_btrfs_fs)
module_exit(exit_btrfs_fs)
MODULE_LICENSE("GPL");
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment