Add (untested and simple) directory item code Fix comp_keys to use the new key ordering Add btrfs_insert_empty_item Signed-off-by: Chris Mason <chris.mason@oracle.com>tirimbino
parent
a1516c8921
commit
62e2749e03
@ -0,0 +1,102 @@ |
||||
#include <stdio.h> |
||||
#include <stdlib.h> |
||||
#include "kerncompat.h" |
||||
#include "radix-tree.h" |
||||
#include "ctree.h" |
||||
#include "disk-io.h" |
||||
#include "hash.h" |
||||
|
||||
int btrfs_insert_dir_item(struct btrfs_root *root, char *name, int name_len, |
||||
u64 dir, u64 objectid, u8 type) |
||||
{ |
||||
int ret = 0; |
||||
struct btrfs_path path; |
||||
struct btrfs_dir_item *dir_item; |
||||
char *name_ptr; |
||||
struct btrfs_key key; |
||||
u32 data_size; |
||||
|
||||
key.objectid = dir; |
||||
key.flags = 0; |
||||
ret = btrfs_name_hash(name, name_len, &key.offset); |
||||
BUG_ON(ret); |
||||
btrfs_init_path(&path); |
||||
data_size = sizeof(*dir_item) + name_len; |
||||
ret = btrfs_insert_empty_item(root, &path, &key, data_size); |
||||
if (ret) |
||||
goto out; |
||||
|
||||
dir_item = btrfs_item_ptr(&path.nodes[0]->leaf, path.slots[0], |
||||
struct btrfs_dir_item); |
||||
btrfs_set_dir_objectid(dir_item, objectid); |
||||
btrfs_set_dir_type(dir_item, type); |
||||
btrfs_set_dir_flags(dir_item, 0); |
||||
name_ptr = (char *)(dir_item + 1); |
||||
memcpy(name_ptr, name, name_len); |
||||
out: |
||||
btrfs_release_path(root, &path); |
||||
return ret; |
||||
} |
||||
|
||||
int btrfs_del_dir_item(struct btrfs_root *root, u64 dir, char *name, |
||||
int name_len) |
||||
{ |
||||
int ret = 0; |
||||
struct btrfs_path path; |
||||
struct btrfs_key key; |
||||
|
||||
key.objectid = dir; |
||||
key.flags = 0; |
||||
ret = btrfs_name_hash(name, name_len, &key.offset); |
||||
BUG_ON(ret); |
||||
btrfs_init_path(&path); |
||||
ret = btrfs_search_slot(root, &key, &path, 0, 1); |
||||
if (ret) |
||||
goto out; |
||||
ret = btrfs_del_item(root, &path); |
||||
out: |
||||
btrfs_release_path(root, &path); |
||||
return ret; |
||||
} |
||||
|
||||
int btrfs_lookup_dir_item(struct btrfs_root *root, u64 dir, char *name, |
||||
int name_len, u64 *objectid) |
||||
{ |
||||
int ret = 0; |
||||
struct btrfs_path path; |
||||
struct btrfs_dir_item *dir_item; |
||||
char *name_ptr; |
||||
struct btrfs_key key; |
||||
u32 item_len; |
||||
struct btrfs_item *item; |
||||
|
||||
key.objectid = dir; |
||||
key.flags = 0; |
||||
ret = btrfs_name_hash(name, name_len, &key.offset); |
||||
BUG_ON(ret); |
||||
btrfs_init_path(&path); |
||||
ret = btrfs_search_slot(root, &key, &path, 0, 0); |
||||
if (ret) |
||||
goto out; |
||||
|
||||
dir_item = btrfs_item_ptr(&path.nodes[0]->leaf, path.slots[0], |
||||
struct btrfs_dir_item); |
||||
|
||||
item = path.nodes[0]->leaf.items + path.slots[0]; |
||||
item_len = btrfs_item_size(item); |
||||
if (item_len != name_len + sizeof(struct btrfs_dir_item)) { |
||||
BUG(); |
||||
ret = 1; |
||||
goto out; |
||||
} |
||||
name_ptr = (char *)(dir_item + 1); |
||||
if (memcmp(name_ptr, name, name_len)) { |
||||
BUG(); |
||||
ret = 1; |
||||
goto out; |
||||
} |
||||
*objectid = btrfs_dir_objectid(dir_item); |
||||
out: |
||||
btrfs_release_path(root, &path); |
||||
return ret; |
||||
} |
@ -0,0 +1,86 @@ |
||||
/*
|
||||
* Original copy from: |
||||
* linux/fs/ext3/hash.c |
||||
* |
||||
* Copyright (C) 2002 by Theodore Ts'o |
||||
* |
||||
* This file is released under the GPL v2. |
||||
* |
||||
* This file may be redistributed under the terms of the GNU Public |
||||
* License. |
||||
*/ |
||||
|
||||
#include "kerncompat.h" |
||||
#define DELTA 0x9E3779B9 |
||||
|
||||
static void TEA_transform(__u32 buf[2], __u32 const in[]) |
||||
{ |
||||
__u32 sum = 0; |
||||
__u32 b0 = buf[0], b1 = buf[1]; |
||||
__u32 a = in[0], b = in[1], c = in[2], d = in[3]; |
||||
int n = 16; |
||||
|
||||
do { |
||||
sum += DELTA; |
||||
b0 += ((b1 << 4)+a) ^ (b1+sum) ^ ((b1 >> 5)+b); |
||||
b1 += ((b0 << 4)+c) ^ (b0+sum) ^ ((b0 >> 5)+d); |
||||
} while(--n); |
||||
|
||||
buf[0] += b0; |
||||
buf[1] += b1; |
||||
} |
||||
|
||||
static void str2hashbuf(const char *msg, int len, __u32 *buf, int num) |
||||
{ |
||||
__u32 pad, val; |
||||
int i; |
||||
|
||||
pad = (__u32)len | ((__u32)len << 8); |
||||
pad |= pad << 16; |
||||
|
||||
val = pad; |
||||
if (len > num*4) |
||||
len = num * 4; |
||||
for (i=0; i < len; i++) { |
||||
if ((i % 4) == 0) |
||||
val = pad; |
||||
val = msg[i] + (val << 8); |
||||
if ((i % 4) == 3) { |
||||
*buf++ = val; |
||||
val = pad; |
||||
num--; |
||||
} |
||||
} |
||||
if (--num >= 0) |
||||
*buf++ = val; |
||||
while (--num >= 0) |
||||
*buf++ = pad; |
||||
} |
||||
|
||||
int btrfs_name_hash(const char *name, int len, u64 *hash_result) |
||||
{ |
||||
__u32 hash; |
||||
__u32 minor_hash = 0; |
||||
const char *p; |
||||
__u32 in[8], buf[2]; |
||||
|
||||
/* Initialize the default seed for the hash checksum functions */ |
||||
buf[0] = 0x67452301; |
||||
buf[1] = 0xefcdab89; |
||||
buf[2] = 0x98badcfe; |
||||
buf[3] = 0x10325476; |
||||
|
||||
p = name; |
||||
while (len > 0) { |
||||
str2hashbuf(p, len, in, 4); |
||||
TEA_transform(buf, in); |
||||
len -= 16; |
||||
p += 16; |
||||
} |
||||
hash = buf[0]; |
||||
minor_hash = buf[1]; |
||||
*hash_result = buf[0]; |
||||
*hash_result <<= 32; |
||||
*hash_result |= buf[1]; |
||||
return 0; |
||||
} |
@ -0,0 +1,23 @@ |
||||
#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; |
||||
} |
Loading…
Reference in new issue