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 | 5be6f7f | 2007-04-05 13:35:25 -0400 | [diff] [blame] | 95 | u32 overflow; |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 96 | |
| 97 | key.objectid = dir; |
| 98 | key.flags = 0; |
Chris Mason | 1d4f640 | 2007-03-15 15:18:43 -0400 | [diff] [blame] | 99 | btrfs_set_key_type(&key, BTRFS_DIR_ITEM_KEY); |
Chris Mason | 7fcde0e | 2007-04-05 12:13:21 -0400 | [diff] [blame] | 100 | btrfs_set_key_overflow(&key, BTRFS_KEY_OVERFLOW_MAX - 1); |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 101 | ret = btrfs_name_hash(name, name_len, &key.offset); |
| 102 | BUG_ON(ret); |
Chris Mason | 7fcde0e | 2007-04-05 12:13:21 -0400 | [diff] [blame] | 103 | while(1) { |
| 104 | ret = btrfs_search_slot(trans, root, &key, path, ins_len, cow); |
| 105 | if (ret < 0) |
| 106 | return ret; |
| 107 | if (ret > 0) { |
| 108 | if (path->slots[0] == 0) |
| 109 | return 1; |
| 110 | path->slots[0]--; |
| 111 | } |
| 112 | leaf = btrfs_buffer_leaf(path->nodes[0]); |
| 113 | found_key = &leaf->items[path->slots[0]].key; |
| 114 | |
| 115 | if (btrfs_disk_key_objectid(found_key) != dir || |
| 116 | btrfs_disk_key_type(found_key) != BTRFS_DIR_ITEM_KEY || |
| 117 | btrfs_disk_key_offset(found_key) != key.offset) |
| 118 | return 1; |
| 119 | |
| 120 | if (btrfs_match_dir_item_name(root, path, name, name_len)) |
| 121 | return 0; |
| 122 | |
Chris Mason | 5be6f7f | 2007-04-05 13:35:25 -0400 | [diff] [blame] | 123 | overflow = btrfs_disk_key_overflow(found_key); |
| 124 | if (overflow == 0) |
Chris Mason | 7fcde0e | 2007-04-05 12:13:21 -0400 | [diff] [blame] | 125 | return 1; |
Chris Mason | 5be6f7f | 2007-04-05 13:35:25 -0400 | [diff] [blame] | 126 | btrfs_set_key_overflow(&key, overflow - 1); |
Chris Mason | 7fcde0e | 2007-04-05 12:13:21 -0400 | [diff] [blame] | 127 | btrfs_release_path(root, path); |
| 128 | } |
| 129 | return 1; |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 130 | } |
| 131 | |
Chris Mason | 5f26f77 | 2007-04-05 10:38:44 -0400 | [diff] [blame] | 132 | int btrfs_lookup_dir_index_item(struct btrfs_trans_handle *trans, |
| 133 | struct btrfs_root *root, |
| 134 | struct btrfs_path *path, u64 dir, |
| 135 | u64 objectid, int mod) |
| 136 | { |
| 137 | int ret; |
| 138 | struct btrfs_key key; |
| 139 | int ins_len = mod < 0 ? -1 : 0; |
| 140 | int cow = mod != 0; |
Chris Mason | 7fcde0e | 2007-04-05 12:13:21 -0400 | [diff] [blame] | 141 | struct btrfs_disk_key *found_key; |
| 142 | struct btrfs_leaf *leaf; |
Chris Mason | 5f26f77 | 2007-04-05 10:38:44 -0400 | [diff] [blame] | 143 | |
| 144 | key.objectid = dir; |
| 145 | key.flags = 0; |
| 146 | btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY); |
Chris Mason | 7fcde0e | 2007-04-05 12:13:21 -0400 | [diff] [blame] | 147 | btrfs_set_key_overflow(&key, BTRFS_KEY_OVERFLOW_MAX - 1); |
Chris Mason | 5f26f77 | 2007-04-05 10:38:44 -0400 | [diff] [blame] | 148 | key.offset = objectid; |
| 149 | ret = btrfs_search_slot(trans, root, &key, path, ins_len, cow); |
Chris Mason | 7fcde0e | 2007-04-05 12:13:21 -0400 | [diff] [blame] | 150 | if (ret < 0) |
| 151 | return ret; |
| 152 | if (ret > 0) { |
| 153 | if (path->slots[0] == 0) |
| 154 | return 1; |
| 155 | path->slots[0]--; |
| 156 | } |
| 157 | leaf = btrfs_buffer_leaf(path->nodes[0]); |
| 158 | found_key = &leaf->items[path->slots[0]].key; |
| 159 | |
| 160 | if (btrfs_disk_key_objectid(found_key) != dir || |
| 161 | btrfs_disk_key_type(found_key) != BTRFS_DIR_INDEX_KEY) |
| 162 | return 1; |
| 163 | if (btrfs_disk_key_offset(found_key) == objectid) |
| 164 | return 0; |
| 165 | return 1; |
Chris Mason | 5f26f77 | 2007-04-05 10:38:44 -0400 | [diff] [blame] | 166 | } |
| 167 | |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 168 | int btrfs_match_dir_item_name(struct btrfs_root *root, |
Chris Mason | 7f5c151 | 2007-03-23 15:56:19 -0400 | [diff] [blame] | 169 | struct btrfs_path *path, |
| 170 | const char *name, int name_len) |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 171 | { |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 172 | struct btrfs_dir_item *dir_item; |
| 173 | char *name_ptr; |
Chris Mason | a8a2ee0 | 2007-03-16 08:46:49 -0400 | [diff] [blame] | 174 | |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 175 | dir_item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), |
| 176 | path->slots[0], |
Chris Mason | 1d4f640 | 2007-03-15 15:18:43 -0400 | [diff] [blame] | 177 | struct btrfs_dir_item); |
Chris Mason | a8a2ee0 | 2007-03-16 08:46:49 -0400 | [diff] [blame] | 178 | if (btrfs_dir_name_len(dir_item) != name_len) |
Chris Mason | 1d4f640 | 2007-03-15 15:18:43 -0400 | [diff] [blame] | 179 | return 0; |
Chris Mason | a8a2ee0 | 2007-03-16 08:46:49 -0400 | [diff] [blame] | 180 | name_ptr = (char *)(dir_item + 1); |
| 181 | if (memcmp(name_ptr, name, name_len)) |
| 182 | return 0; |
Chris Mason | 1d4f640 | 2007-03-15 15:18:43 -0400 | [diff] [blame] | 183 | return 1; |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 184 | } |