Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 1 | #include <stdio.h> |
| 2 | #include <stdlib.h> |
| 3 | #include "kerncompat.h" |
| 4 | #include "radix-tree.h" |
| 5 | #include "ctree.h" |
| 6 | #include "disk-io.h" |
| 7 | #include "hash.h" |
| 8 | |
| 9 | int btrfs_insert_dir_item(struct btrfs_root *root, char *name, int name_len, |
| 10 | u64 dir, u64 objectid, u8 type) |
| 11 | { |
| 12 | int ret = 0; |
| 13 | struct btrfs_path path; |
| 14 | struct btrfs_dir_item *dir_item; |
| 15 | char *name_ptr; |
| 16 | struct btrfs_key key; |
| 17 | u32 data_size; |
| 18 | |
| 19 | key.objectid = dir; |
| 20 | key.flags = 0; |
Chris Mason | 1d4f640 | 2007-03-15 15:18:43 -0400 | [diff] [blame] | 21 | btrfs_set_key_type(&key, BTRFS_DIR_ITEM_KEY); |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 22 | ret = btrfs_name_hash(name, name_len, &key.offset); |
| 23 | BUG_ON(ret); |
| 24 | btrfs_init_path(&path); |
| 25 | data_size = sizeof(*dir_item) + name_len; |
| 26 | ret = btrfs_insert_empty_item(root, &path, &key, data_size); |
| 27 | if (ret) |
| 28 | goto out; |
| 29 | |
| 30 | dir_item = btrfs_item_ptr(&path.nodes[0]->leaf, path.slots[0], |
| 31 | struct btrfs_dir_item); |
| 32 | btrfs_set_dir_objectid(dir_item, objectid); |
| 33 | btrfs_set_dir_type(dir_item, type); |
| 34 | btrfs_set_dir_flags(dir_item, 0); |
| 35 | name_ptr = (char *)(dir_item + 1); |
| 36 | memcpy(name_ptr, name, name_len); |
| 37 | out: |
| 38 | btrfs_release_path(root, &path); |
| 39 | return ret; |
| 40 | } |
| 41 | |
Chris Mason | 1d4f640 | 2007-03-15 15:18:43 -0400 | [diff] [blame] | 42 | int btrfs_lookup_dir_item(struct btrfs_root *root, struct btrfs_path *path, |
| 43 | u64 dir, char *name, int name_len, int mod) |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 44 | { |
Chris Mason | 1d4f640 | 2007-03-15 15:18:43 -0400 | [diff] [blame] | 45 | int ret; |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 46 | struct btrfs_key key; |
Chris Mason | 1d4f640 | 2007-03-15 15:18:43 -0400 | [diff] [blame] | 47 | int ins_len = mod < 0 ? -1 : 0; |
| 48 | int cow = mod != 0; |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 49 | |
| 50 | key.objectid = dir; |
| 51 | key.flags = 0; |
Chris Mason | 1d4f640 | 2007-03-15 15:18:43 -0400 | [diff] [blame] | 52 | btrfs_set_key_type(&key, BTRFS_DIR_ITEM_KEY); |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 53 | ret = btrfs_name_hash(name, name_len, &key.offset); |
| 54 | BUG_ON(ret); |
Chris Mason | 1d4f640 | 2007-03-15 15:18:43 -0400 | [diff] [blame] | 55 | ret = btrfs_search_slot(root, &key, path, ins_len, cow); |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 56 | return ret; |
| 57 | } |
| 58 | |
Chris Mason | 1d4f640 | 2007-03-15 15:18:43 -0400 | [diff] [blame] | 59 | int btrfs_match_dir_item_name(struct btrfs_root *root, struct btrfs_path *path, |
| 60 | char *name, int name_len) |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 61 | { |
Chris Mason | 1d4f640 | 2007-03-15 15:18:43 -0400 | [diff] [blame] | 62 | struct btrfs_item *item; |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 63 | struct btrfs_dir_item *dir_item; |
| 64 | char *name_ptr; |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 65 | u32 item_len; |
Chris Mason | 1d4f640 | 2007-03-15 15:18:43 -0400 | [diff] [blame] | 66 | item = path->nodes[0]->leaf.items + path->slots[0]; |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 67 | item_len = btrfs_item_size(item); |
| 68 | if (item_len != name_len + sizeof(struct btrfs_dir_item)) { |
Chris Mason | 1d4f640 | 2007-03-15 15:18:43 -0400 | [diff] [blame] | 69 | return 0; |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 70 | } |
Chris Mason | 1d4f640 | 2007-03-15 15:18:43 -0400 | [diff] [blame] | 71 | dir_item = btrfs_item_ptr(&path->nodes[0]->leaf, path->slots[0], |
| 72 | struct btrfs_dir_item); |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 73 | name_ptr = (char *)(dir_item + 1); |
| 74 | if (memcmp(name_ptr, name, name_len)) { |
Chris Mason | 1d4f640 | 2007-03-15 15:18:43 -0400 | [diff] [blame] | 75 | return 0; |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 76 | } |
Chris Mason | 1d4f640 | 2007-03-15 15:18:43 -0400 | [diff] [blame] | 77 | return 1; |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 78 | } |