Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 1 | #include <linux/module.h> |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 2 | #include "ctree.h" |
| 3 | #include "disk-io.h" |
| 4 | #include "hash.h" |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 5 | #include "transaction.h" |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 6 | |
Chris Mason | 7fcde0e | 2007-04-05 12:13:21 -0400 | [diff] [blame^] | 7 | int insert_with_overflow(struct btrfs_trans_handle *trans, struct btrfs_root |
| 8 | *root, struct btrfs_path *path, struct btrfs_key |
| 9 | *cpu_key, u32 data_size) |
| 10 | { |
| 11 | int overflow; |
| 12 | int ret; |
| 13 | |
| 14 | ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size); |
| 15 | overflow = btrfs_key_overflow(cpu_key); |
| 16 | |
| 17 | while(ret == -EEXIST && overflow < BTRFS_KEY_OVERFLOW_MAX) { |
| 18 | overflow++; |
| 19 | btrfs_set_key_overflow(cpu_key, overflow); |
| 20 | btrfs_release_path(root, path); |
| 21 | ret = btrfs_insert_empty_item(trans, root, path, cpu_key, |
| 22 | data_size); |
| 23 | } |
| 24 | return ret; |
| 25 | } |
| 26 | |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 27 | int btrfs_insert_dir_item(struct btrfs_trans_handle *trans, struct btrfs_root |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 28 | *root, const char *name, int name_len, u64 dir, u64 |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 29 | objectid, u8 type) |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 30 | { |
| 31 | int ret = 0; |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 32 | struct btrfs_path *path; |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 33 | struct btrfs_dir_item *dir_item; |
| 34 | char *name_ptr; |
| 35 | struct btrfs_key key; |
| 36 | u32 data_size; |
| 37 | |
| 38 | key.objectid = dir; |
| 39 | key.flags = 0; |
Chris Mason | 1d4f640 | 2007-03-15 15:18:43 -0400 | [diff] [blame] | 40 | btrfs_set_key_type(&key, BTRFS_DIR_ITEM_KEY); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 41 | ret = btrfs_name_hash(name, name_len, &key.offset); |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 42 | BUG_ON(ret); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 43 | path = btrfs_alloc_path(); |
| 44 | btrfs_init_path(path); |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 45 | data_size = sizeof(*dir_item) + name_len; |
Chris Mason | 7fcde0e | 2007-04-05 12:13:21 -0400 | [diff] [blame^] | 46 | ret = insert_with_overflow(trans, root, path, &key, data_size); |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 47 | if (ret) |
| 48 | goto out; |
| 49 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 50 | dir_item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), |
| 51 | path->slots[0], |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 52 | struct btrfs_dir_item); |
| 53 | btrfs_set_dir_objectid(dir_item, objectid); |
| 54 | btrfs_set_dir_type(dir_item, type); |
| 55 | btrfs_set_dir_flags(dir_item, 0); |
Chris Mason | a8a2ee0 | 2007-03-16 08:46:49 -0400 | [diff] [blame] | 56 | btrfs_set_dir_name_len(dir_item, name_len); |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 57 | name_ptr = (char *)(dir_item + 1); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 58 | btrfs_memcpy(root, path->nodes[0]->b_data, name_ptr, name, name_len); |
| 59 | btrfs_mark_buffer_dirty(path->nodes[0]); |
Chris Mason | bae45de | 2007-04-04 21:22:22 -0400 | [diff] [blame] | 60 | btrfs_release_path(root, path); |
| 61 | |
| 62 | btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY); |
| 63 | key.offset = objectid; |
Chris Mason | 7fcde0e | 2007-04-05 12:13:21 -0400 | [diff] [blame^] | 64 | ret = insert_with_overflow(trans, root, path, &key, data_size); |
Chris Mason | bae45de | 2007-04-04 21:22:22 -0400 | [diff] [blame] | 65 | // FIXME clear the dirindex bit |
| 66 | if (ret) |
| 67 | goto out; |
| 68 | |
| 69 | dir_item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), |
| 70 | path->slots[0], |
| 71 | struct btrfs_dir_item); |
| 72 | btrfs_set_dir_objectid(dir_item, objectid); |
| 73 | btrfs_set_dir_type(dir_item, type); |
| 74 | btrfs_set_dir_flags(dir_item, 0); |
| 75 | btrfs_set_dir_name_len(dir_item, name_len); |
| 76 | name_ptr = (char *)(dir_item + 1); |
| 77 | btrfs_memcpy(root, path->nodes[0]->b_data, name_ptr, name, name_len); |
| 78 | btrfs_mark_buffer_dirty(path->nodes[0]); |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 79 | out: |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 80 | btrfs_release_path(root, path); |
| 81 | btrfs_free_path(path); |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 82 | return ret; |
| 83 | } |
| 84 | |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 85 | int btrfs_lookup_dir_item(struct btrfs_trans_handle *trans, struct btrfs_root |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 86 | *root, struct btrfs_path *path, u64 dir, |
| 87 | const char *name, int name_len, int mod) |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 88 | { |
Chris Mason | 1d4f640 | 2007-03-15 15:18:43 -0400 | [diff] [blame] | 89 | int ret; |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 90 | struct btrfs_key key; |
Chris Mason | 1d4f640 | 2007-03-15 15:18:43 -0400 | [diff] [blame] | 91 | int ins_len = mod < 0 ? -1 : 0; |
| 92 | int cow = mod != 0; |
Chris Mason | 7fcde0e | 2007-04-05 12:13:21 -0400 | [diff] [blame^] | 93 | struct btrfs_disk_key *found_key; |
| 94 | struct btrfs_leaf *leaf; |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 95 | |
| 96 | key.objectid = dir; |
| 97 | key.flags = 0; |
Chris Mason | 1d4f640 | 2007-03-15 15:18:43 -0400 | [diff] [blame] | 98 | btrfs_set_key_type(&key, BTRFS_DIR_ITEM_KEY); |
Chris Mason | 7fcde0e | 2007-04-05 12:13:21 -0400 | [diff] [blame^] | 99 | btrfs_set_key_overflow(&key, BTRFS_KEY_OVERFLOW_MAX - 1); |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 100 | ret = btrfs_name_hash(name, name_len, &key.offset); |
| 101 | BUG_ON(ret); |
Chris Mason | 7fcde0e | 2007-04-05 12:13:21 -0400 | [diff] [blame^] | 102 | while(1) { |
| 103 | ret = btrfs_search_slot(trans, root, &key, path, ins_len, cow); |
| 104 | if (ret < 0) |
| 105 | return ret; |
| 106 | if (ret > 0) { |
| 107 | if (path->slots[0] == 0) |
| 108 | return 1; |
| 109 | path->slots[0]--; |
| 110 | } |
| 111 | leaf = btrfs_buffer_leaf(path->nodes[0]); |
| 112 | found_key = &leaf->items[path->slots[0]].key; |
| 113 | |
| 114 | if (btrfs_disk_key_objectid(found_key) != dir || |
| 115 | btrfs_disk_key_type(found_key) != BTRFS_DIR_ITEM_KEY || |
| 116 | btrfs_disk_key_offset(found_key) != key.offset) |
| 117 | return 1; |
| 118 | |
| 119 | if (btrfs_match_dir_item_name(root, path, name, name_len)) |
| 120 | return 0; |
| 121 | |
| 122 | if (btrfs_disk_key_overflow(found_key) == 0) |
| 123 | return 1; |
| 124 | btrfs_release_path(root, path); |
| 125 | } |
| 126 | return 1; |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 127 | } |
| 128 | |
Chris Mason | 5f26f77 | 2007-04-05 10:38:44 -0400 | [diff] [blame] | 129 | int btrfs_lookup_dir_index_item(struct btrfs_trans_handle *trans, |
| 130 | struct btrfs_root *root, |
| 131 | struct btrfs_path *path, u64 dir, |
| 132 | u64 objectid, int mod) |
| 133 | { |
| 134 | int ret; |
| 135 | struct btrfs_key key; |
| 136 | int ins_len = mod < 0 ? -1 : 0; |
| 137 | int cow = mod != 0; |
Chris Mason | 7fcde0e | 2007-04-05 12:13:21 -0400 | [diff] [blame^] | 138 | struct btrfs_disk_key *found_key; |
| 139 | struct btrfs_leaf *leaf; |
Chris Mason | 5f26f77 | 2007-04-05 10:38:44 -0400 | [diff] [blame] | 140 | |
| 141 | key.objectid = dir; |
| 142 | key.flags = 0; |
| 143 | btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY); |
Chris Mason | 7fcde0e | 2007-04-05 12:13:21 -0400 | [diff] [blame^] | 144 | btrfs_set_key_overflow(&key, BTRFS_KEY_OVERFLOW_MAX - 1); |
Chris Mason | 5f26f77 | 2007-04-05 10:38:44 -0400 | [diff] [blame] | 145 | key.offset = objectid; |
| 146 | ret = btrfs_search_slot(trans, root, &key, path, ins_len, cow); |
Chris Mason | 7fcde0e | 2007-04-05 12:13:21 -0400 | [diff] [blame^] | 147 | if (ret < 0) |
| 148 | return ret; |
| 149 | if (ret > 0) { |
| 150 | if (path->slots[0] == 0) |
| 151 | return 1; |
| 152 | path->slots[0]--; |
| 153 | } |
| 154 | leaf = btrfs_buffer_leaf(path->nodes[0]); |
| 155 | found_key = &leaf->items[path->slots[0]].key; |
| 156 | |
| 157 | if (btrfs_disk_key_objectid(found_key) != dir || |
| 158 | btrfs_disk_key_type(found_key) != BTRFS_DIR_INDEX_KEY) |
| 159 | return 1; |
| 160 | if (btrfs_disk_key_offset(found_key) == objectid) |
| 161 | return 0; |
| 162 | return 1; |
Chris Mason | 5f26f77 | 2007-04-05 10:38:44 -0400 | [diff] [blame] | 163 | } |
| 164 | |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 165 | int btrfs_match_dir_item_name(struct btrfs_root *root, |
Chris Mason | 7f5c151 | 2007-03-23 15:56:19 -0400 | [diff] [blame] | 166 | struct btrfs_path *path, |
| 167 | const char *name, int name_len) |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 168 | { |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 169 | struct btrfs_dir_item *dir_item; |
| 170 | char *name_ptr; |
Chris Mason | a8a2ee0 | 2007-03-16 08:46:49 -0400 | [diff] [blame] | 171 | |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 172 | dir_item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), |
| 173 | path->slots[0], |
Chris Mason | 1d4f640 | 2007-03-15 15:18:43 -0400 | [diff] [blame] | 174 | struct btrfs_dir_item); |
Chris Mason | a8a2ee0 | 2007-03-16 08:46:49 -0400 | [diff] [blame] | 175 | if (btrfs_dir_name_len(dir_item) != name_len) |
Chris Mason | 1d4f640 | 2007-03-15 15:18:43 -0400 | [diff] [blame] | 176 | return 0; |
Chris Mason | a8a2ee0 | 2007-03-16 08:46:49 -0400 | [diff] [blame] | 177 | name_ptr = (char *)(dir_item + 1); |
| 178 | if (memcmp(name_ptr, name, name_len)) |
| 179 | return 0; |
Chris Mason | 1d4f640 | 2007-03-15 15:18:43 -0400 | [diff] [blame] | 180 | return 1; |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 181 | } |