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 | 35b7e47 | 2007-05-02 15:53:43 -0400 | [diff] [blame] | 7 | static struct btrfs_dir_item *insert_with_overflow(struct btrfs_trans_handle |
| 8 | *trans, |
| 9 | struct btrfs_root *root, |
| 10 | struct btrfs_path *path, |
| 11 | struct btrfs_key *cpu_key, |
Chris Mason | e06afa8 | 2007-05-23 15:44:28 -0400 | [diff] [blame^] | 12 | u32 data_size, |
| 13 | const char *name, |
| 14 | int name_len) |
Chris Mason | 7fcde0e | 2007-04-05 12:13:21 -0400 | [diff] [blame] | 15 | { |
Chris Mason | 7fcde0e | 2007-04-05 12:13:21 -0400 | [diff] [blame] | 16 | int ret; |
Chris Mason | 7e38180 | 2007-04-19 15:36:27 -0400 | [diff] [blame] | 17 | char *ptr; |
| 18 | struct btrfs_item *item; |
| 19 | struct btrfs_leaf *leaf; |
Chris Mason | 7fcde0e | 2007-04-05 12:13:21 -0400 | [diff] [blame] | 20 | |
| 21 | ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size); |
Chris Mason | 7e38180 | 2007-04-19 15:36:27 -0400 | [diff] [blame] | 22 | if (ret == -EEXIST) { |
Chris Mason | e06afa8 | 2007-05-23 15:44:28 -0400 | [diff] [blame^] | 23 | struct btrfs_dir_item *di; |
| 24 | di = btrfs_match_dir_item_name(root, path, name, name_len); |
| 25 | if (di) |
| 26 | return ERR_PTR(-EEXIST); |
Chris Mason | 7e38180 | 2007-04-19 15:36:27 -0400 | [diff] [blame] | 27 | ret = btrfs_extend_item(trans, root, path, data_size); |
| 28 | WARN_ON(ret > 0); |
| 29 | if (ret) |
| 30 | return ERR_PTR(ret); |
Chris Mason | 7fcde0e | 2007-04-05 12:13:21 -0400 | [diff] [blame] | 31 | } |
Chris Mason | 7e38180 | 2007-04-19 15:36:27 -0400 | [diff] [blame] | 32 | WARN_ON(ret > 0); |
| 33 | leaf = btrfs_buffer_leaf(path->nodes[0]); |
| 34 | item = leaf->items + path->slots[0]; |
| 35 | ptr = btrfs_item_ptr(leaf, path->slots[0], char); |
| 36 | BUG_ON(data_size > btrfs_item_size(item)); |
| 37 | ptr += btrfs_item_size(item) - data_size; |
| 38 | return (struct btrfs_dir_item *)ptr; |
Chris Mason | 7fcde0e | 2007-04-05 12:13:21 -0400 | [diff] [blame] | 39 | } |
| 40 | |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 41 | int btrfs_insert_dir_item(struct btrfs_trans_handle *trans, struct btrfs_root |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 42 | *root, const char *name, int name_len, u64 dir, |
| 43 | struct btrfs_key *location, u8 type) |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 44 | { |
| 45 | int ret = 0; |
Chris Mason | e06afa8 | 2007-05-23 15:44:28 -0400 | [diff] [blame^] | 46 | int ret2 = 0; |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 47 | struct btrfs_path *path; |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 48 | struct btrfs_dir_item *dir_item; |
| 49 | char *name_ptr; |
| 50 | struct btrfs_key key; |
| 51 | u32 data_size; |
| 52 | |
| 53 | key.objectid = dir; |
| 54 | key.flags = 0; |
Chris Mason | 1d4f640 | 2007-03-15 15:18:43 -0400 | [diff] [blame] | 55 | btrfs_set_key_type(&key, BTRFS_DIR_ITEM_KEY); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 56 | ret = btrfs_name_hash(name, name_len, &key.offset); |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 57 | BUG_ON(ret); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 58 | path = btrfs_alloc_path(); |
| 59 | btrfs_init_path(path); |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 60 | data_size = sizeof(*dir_item) + name_len; |
Chris Mason | e06afa8 | 2007-05-23 15:44:28 -0400 | [diff] [blame^] | 61 | dir_item = insert_with_overflow(trans, root, path, &key, data_size, |
| 62 | name, name_len); |
Chris Mason | 7e38180 | 2007-04-19 15:36:27 -0400 | [diff] [blame] | 63 | if (IS_ERR(dir_item)) { |
| 64 | ret = PTR_ERR(dir_item); |
Chris Mason | e06afa8 | 2007-05-23 15:44:28 -0400 | [diff] [blame^] | 65 | if (ret == -EEXIST) |
| 66 | goto second_insert; |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 67 | goto out; |
Chris Mason | 7e38180 | 2007-04-19 15:36:27 -0400 | [diff] [blame] | 68 | } |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 69 | |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 70 | btrfs_cpu_key_to_disk(&dir_item->location, location); |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 71 | btrfs_set_dir_type(dir_item, type); |
| 72 | btrfs_set_dir_flags(dir_item, 0); |
Chris Mason | a8a2ee0 | 2007-03-16 08:46:49 -0400 | [diff] [blame] | 73 | btrfs_set_dir_name_len(dir_item, name_len); |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 74 | name_ptr = (char *)(dir_item + 1); |
Chris Mason | c5739bb | 2007-04-10 09:27:04 -0400 | [diff] [blame] | 75 | |
| 76 | btrfs_memcpy(root, path->nodes[0]->b_data, name_ptr, name, name_len); |
| 77 | btrfs_mark_buffer_dirty(path->nodes[0]); |
Chris Mason | 7e38180 | 2007-04-19 15:36:27 -0400 | [diff] [blame] | 78 | |
Chris Mason | e06afa8 | 2007-05-23 15:44:28 -0400 | [diff] [blame^] | 79 | second_insert: |
Chris Mason | 7e38180 | 2007-04-19 15:36:27 -0400 | [diff] [blame] | 80 | /* FIXME, use some real flag for selecting the extra index */ |
| 81 | if (root == root->fs_info->tree_root) { |
| 82 | ret = 0; |
| 83 | goto out; |
| 84 | } |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 85 | btrfs_release_path(root, path); |
Chris Mason | 7e38180 | 2007-04-19 15:36:27 -0400 | [diff] [blame] | 86 | |
| 87 | btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY); |
| 88 | key.offset = location->objectid; |
Chris Mason | e06afa8 | 2007-05-23 15:44:28 -0400 | [diff] [blame^] | 89 | dir_item = insert_with_overflow(trans, root, path, &key, data_size, |
| 90 | name, name_len); |
Chris Mason | 7e38180 | 2007-04-19 15:36:27 -0400 | [diff] [blame] | 91 | if (IS_ERR(dir_item)) { |
Chris Mason | e06afa8 | 2007-05-23 15:44:28 -0400 | [diff] [blame^] | 92 | ret2 = PTR_ERR(dir_item); |
Chris Mason | 7e38180 | 2007-04-19 15:36:27 -0400 | [diff] [blame] | 93 | goto out; |
| 94 | } |
| 95 | btrfs_cpu_key_to_disk(&dir_item->location, location); |
| 96 | btrfs_set_dir_type(dir_item, type); |
| 97 | btrfs_set_dir_flags(dir_item, 0); |
| 98 | btrfs_set_dir_name_len(dir_item, name_len); |
| 99 | name_ptr = (char *)(dir_item + 1); |
| 100 | btrfs_memcpy(root, path->nodes[0]->b_data, name_ptr, name, name_len); |
| 101 | btrfs_mark_buffer_dirty(path->nodes[0]); |
| 102 | out: |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 103 | btrfs_free_path(path); |
Chris Mason | e06afa8 | 2007-05-23 15:44:28 -0400 | [diff] [blame^] | 104 | if (ret) |
| 105 | return ret; |
| 106 | if (ret2) |
| 107 | return ret2; |
| 108 | return 0; |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 109 | } |
| 110 | |
Chris Mason | 7e38180 | 2007-04-19 15:36:27 -0400 | [diff] [blame] | 111 | struct btrfs_dir_item *btrfs_lookup_dir_item(struct btrfs_trans_handle *trans, |
| 112 | struct btrfs_root *root, |
| 113 | struct btrfs_path *path, u64 dir, |
| 114 | const char *name, int name_len, |
| 115 | int mod) |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 116 | { |
Chris Mason | 1d4f640 | 2007-03-15 15:18:43 -0400 | [diff] [blame] | 117 | int ret; |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 118 | struct btrfs_key key; |
Chris Mason | 1d4f640 | 2007-03-15 15:18:43 -0400 | [diff] [blame] | 119 | int ins_len = mod < 0 ? -1 : 0; |
| 120 | int cow = mod != 0; |
Chris Mason | 7fcde0e | 2007-04-05 12:13:21 -0400 | [diff] [blame] | 121 | struct btrfs_disk_key *found_key; |
| 122 | struct btrfs_leaf *leaf; |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 123 | |
| 124 | key.objectid = dir; |
| 125 | key.flags = 0; |
Chris Mason | 1d4f640 | 2007-03-15 15:18:43 -0400 | [diff] [blame] | 126 | btrfs_set_key_type(&key, BTRFS_DIR_ITEM_KEY); |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 127 | ret = btrfs_name_hash(name, name_len, &key.offset); |
| 128 | BUG_ON(ret); |
Chris Mason | 7e38180 | 2007-04-19 15:36:27 -0400 | [diff] [blame] | 129 | ret = btrfs_search_slot(trans, root, &key, path, ins_len, cow); |
| 130 | if (ret < 0) |
| 131 | return ERR_PTR(ret); |
| 132 | if (ret > 0) { |
| 133 | if (path->slots[0] == 0) |
| 134 | return NULL; |
| 135 | path->slots[0]--; |
Chris Mason | 7fcde0e | 2007-04-05 12:13:21 -0400 | [diff] [blame] | 136 | } |
Chris Mason | 7e38180 | 2007-04-19 15:36:27 -0400 | [diff] [blame] | 137 | leaf = btrfs_buffer_leaf(path->nodes[0]); |
| 138 | found_key = &leaf->items[path->slots[0]].key; |
| 139 | |
| 140 | if (btrfs_disk_key_objectid(found_key) != dir || |
| 141 | btrfs_disk_key_type(found_key) != BTRFS_DIR_ITEM_KEY || |
| 142 | btrfs_disk_key_offset(found_key) != key.offset) |
| 143 | return NULL; |
| 144 | |
| 145 | return btrfs_match_dir_item_name(root, path, name, name_len); |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 146 | } |
| 147 | |
Chris Mason | 7e38180 | 2007-04-19 15:36:27 -0400 | [diff] [blame] | 148 | struct btrfs_dir_item * |
| 149 | btrfs_lookup_dir_index_item(struct btrfs_trans_handle *trans, |
| 150 | struct btrfs_root *root, |
| 151 | struct btrfs_path *path, u64 dir, |
| 152 | u64 objectid, const char *name, int name_len, |
| 153 | int mod) |
| 154 | { |
| 155 | int ret; |
| 156 | struct btrfs_key key; |
| 157 | int ins_len = mod < 0 ? -1 : 0; |
| 158 | int cow = mod != 0; |
| 159 | |
| 160 | key.objectid = dir; |
| 161 | key.flags = 0; |
| 162 | btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY); |
| 163 | key.offset = objectid; |
| 164 | |
| 165 | ret = btrfs_search_slot(trans, root, &key, path, ins_len, cow); |
| 166 | if (ret < 0) |
| 167 | return ERR_PTR(ret); |
| 168 | if (ret > 0) |
| 169 | return ERR_PTR(-ENOENT); |
| 170 | return btrfs_match_dir_item_name(root, path, name, name_len); |
| 171 | } |
| 172 | |
| 173 | struct btrfs_dir_item *btrfs_match_dir_item_name(struct btrfs_root *root, |
Chris Mason | 7f5c151 | 2007-03-23 15:56:19 -0400 | [diff] [blame] | 174 | struct btrfs_path *path, |
| 175 | const char *name, int name_len) |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 176 | { |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 177 | struct btrfs_dir_item *dir_item; |
| 178 | char *name_ptr; |
Chris Mason | 7e38180 | 2007-04-19 15:36:27 -0400 | [diff] [blame] | 179 | u32 total_len; |
| 180 | u32 cur = 0; |
| 181 | u32 this_len; |
| 182 | struct btrfs_leaf *leaf; |
Chris Mason | a8a2ee0 | 2007-03-16 08:46:49 -0400 | [diff] [blame] | 183 | |
Chris Mason | 7e38180 | 2007-04-19 15:36:27 -0400 | [diff] [blame] | 184 | leaf = btrfs_buffer_leaf(path->nodes[0]); |
| 185 | dir_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dir_item); |
| 186 | total_len = btrfs_item_size(leaf->items + path->slots[0]); |
| 187 | while(cur < total_len) { |
| 188 | this_len = sizeof(*dir_item) + btrfs_dir_name_len(dir_item); |
| 189 | name_ptr = (char *)(dir_item + 1); |
| 190 | |
| 191 | if (btrfs_dir_name_len(dir_item) == name_len && |
| 192 | memcmp(name_ptr, name, name_len) == 0) |
| 193 | return dir_item; |
| 194 | |
| 195 | cur += this_len; |
| 196 | dir_item = (struct btrfs_dir_item *)((char *)dir_item + |
| 197 | this_len); |
| 198 | } |
| 199 | return NULL; |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 200 | } |
Chris Mason | 7e38180 | 2007-04-19 15:36:27 -0400 | [diff] [blame] | 201 | |
| 202 | int btrfs_delete_one_dir_name(struct btrfs_trans_handle *trans, |
| 203 | struct btrfs_root *root, |
| 204 | struct btrfs_path *path, |
| 205 | struct btrfs_dir_item *di) |
| 206 | { |
| 207 | |
| 208 | struct btrfs_leaf *leaf; |
| 209 | u32 sub_item_len; |
| 210 | u32 item_len; |
| 211 | int ret; |
| 212 | |
| 213 | leaf = btrfs_buffer_leaf(path->nodes[0]); |
| 214 | sub_item_len = sizeof(*di) + btrfs_dir_name_len(di); |
| 215 | item_len = btrfs_item_size(leaf->items + path->slots[0]); |
| 216 | if (sub_item_len == btrfs_item_size(leaf->items + path->slots[0])) { |
| 217 | ret = btrfs_del_item(trans, root, path); |
| 218 | BUG_ON(ret); |
| 219 | } else { |
| 220 | char *ptr = (char *)di; |
| 221 | char *start = btrfs_item_ptr(leaf, path->slots[0], char); |
| 222 | btrfs_memmove(root, leaf, ptr, ptr + sub_item_len, |
| 223 | item_len - (ptr + sub_item_len - start)); |
| 224 | ret = btrfs_truncate_item(trans, root, path, |
| 225 | item_len - sub_item_len); |
| 226 | BUG_ON(ret); |
| 227 | } |
| 228 | return 0; |
| 229 | } |
| 230 | |