Chris Mason | 6cbd557 | 2007-06-12 09:07:21 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 Oracle. All rights reserved. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public |
| 6 | * License v2 as published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 11 | * General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public |
| 14 | * License along with this program; if not, write to the |
| 15 | * Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
| 16 | * Boston, MA 021110-1307, USA. |
| 17 | */ |
| 18 | |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 19 | #include "ctree.h" |
| 20 | #include "disk-io.h" |
| 21 | #include "hash.h" |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 22 | #include "transaction.h" |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 23 | |
Chris Mason | 35b7e47 | 2007-05-02 15:53:43 -0400 | [diff] [blame] | 24 | static struct btrfs_dir_item *insert_with_overflow(struct btrfs_trans_handle |
| 25 | *trans, |
| 26 | struct btrfs_root *root, |
| 27 | struct btrfs_path *path, |
| 28 | struct btrfs_key *cpu_key, |
Chris Mason | e06afa8 | 2007-05-23 15:44:28 -0400 | [diff] [blame] | 29 | u32 data_size, |
| 30 | const char *name, |
| 31 | int name_len) |
Chris Mason | 7fcde0e | 2007-04-05 12:13:21 -0400 | [diff] [blame] | 32 | { |
Chris Mason | 7fcde0e | 2007-04-05 12:13:21 -0400 | [diff] [blame] | 33 | int ret; |
Chris Mason | 7e38180 | 2007-04-19 15:36:27 -0400 | [diff] [blame] | 34 | char *ptr; |
| 35 | struct btrfs_item *item; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 36 | struct extent_buffer *leaf; |
Chris Mason | 7fcde0e | 2007-04-05 12:13:21 -0400 | [diff] [blame] | 37 | |
| 38 | ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size); |
Chris Mason | 7e38180 | 2007-04-19 15:36:27 -0400 | [diff] [blame] | 39 | if (ret == -EEXIST) { |
Chris Mason | e06afa8 | 2007-05-23 15:44:28 -0400 | [diff] [blame] | 40 | struct btrfs_dir_item *di; |
| 41 | di = btrfs_match_dir_item_name(root, path, name, name_len); |
| 42 | if (di) |
| 43 | return ERR_PTR(-EEXIST); |
Chris Mason | 7e38180 | 2007-04-19 15:36:27 -0400 | [diff] [blame] | 44 | ret = btrfs_extend_item(trans, root, path, data_size); |
| 45 | WARN_ON(ret > 0); |
| 46 | if (ret) |
| 47 | return ERR_PTR(ret); |
Chris Mason | 7fcde0e | 2007-04-05 12:13:21 -0400 | [diff] [blame] | 48 | } |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 49 | if (ret < 0) |
| 50 | return ERR_PTR(ret); |
Chris Mason | 7e38180 | 2007-04-19 15:36:27 -0400 | [diff] [blame] | 51 | WARN_ON(ret > 0); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 52 | leaf = path->nodes[0]; |
| 53 | item = btrfs_item_nr(leaf, path->slots[0]); |
Chris Mason | 7e38180 | 2007-04-19 15:36:27 -0400 | [diff] [blame] | 54 | ptr = btrfs_item_ptr(leaf, path->slots[0], char); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 55 | BUG_ON(data_size > btrfs_item_size(leaf, item)); |
| 56 | ptr += btrfs_item_size(leaf, item) - data_size; |
Chris Mason | 7e38180 | 2007-04-19 15:36:27 -0400 | [diff] [blame] | 57 | return (struct btrfs_dir_item *)ptr; |
Chris Mason | 7fcde0e | 2007-04-05 12:13:21 -0400 | [diff] [blame] | 58 | } |
| 59 | |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 60 | 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] | 61 | *root, const char *name, int name_len, u64 dir, |
| 62 | struct btrfs_key *location, u8 type) |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 63 | { |
| 64 | int ret = 0; |
Chris Mason | e06afa8 | 2007-05-23 15:44:28 -0400 | [diff] [blame] | 65 | int ret2 = 0; |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 66 | struct btrfs_path *path; |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 67 | struct btrfs_dir_item *dir_item; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 68 | struct extent_buffer *leaf; |
| 69 | unsigned long name_ptr; |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 70 | struct btrfs_key key; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 71 | struct btrfs_disk_key disk_key; |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 72 | u32 data_size; |
| 73 | |
| 74 | key.objectid = dir; |
Chris Mason | 1d4f640 | 2007-03-15 15:18:43 -0400 | [diff] [blame] | 75 | btrfs_set_key_type(&key, BTRFS_DIR_ITEM_KEY); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 76 | ret = btrfs_name_hash(name, name_len, &key.offset); |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 77 | BUG_ON(ret); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 78 | path = btrfs_alloc_path(); |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 79 | data_size = sizeof(*dir_item) + name_len; |
Chris Mason | e06afa8 | 2007-05-23 15:44:28 -0400 | [diff] [blame] | 80 | dir_item = insert_with_overflow(trans, root, path, &key, data_size, |
| 81 | name, name_len); |
Chris Mason | 7e38180 | 2007-04-19 15:36:27 -0400 | [diff] [blame] | 82 | if (IS_ERR(dir_item)) { |
| 83 | ret = PTR_ERR(dir_item); |
Chris Mason | e06afa8 | 2007-05-23 15:44:28 -0400 | [diff] [blame] | 84 | if (ret == -EEXIST) |
| 85 | goto second_insert; |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 86 | goto out; |
Chris Mason | 7e38180 | 2007-04-19 15:36:27 -0400 | [diff] [blame] | 87 | } |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 88 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 89 | leaf = path->nodes[0]; |
| 90 | btrfs_cpu_key_to_disk(&disk_key, location); |
| 91 | btrfs_set_dir_item_key(leaf, dir_item, &disk_key); |
| 92 | btrfs_set_dir_type(leaf, dir_item, type); |
| 93 | btrfs_set_dir_flags(leaf, dir_item, 0); |
| 94 | btrfs_set_dir_name_len(leaf, dir_item, name_len); |
| 95 | name_ptr = (unsigned long)(dir_item + 1); |
Chris Mason | c5739bb | 2007-04-10 09:27:04 -0400 | [diff] [blame] | 96 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 97 | write_extent_buffer(leaf, name, name_ptr, name_len); |
| 98 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 7e38180 | 2007-04-19 15:36:27 -0400 | [diff] [blame] | 99 | |
Chris Mason | e06afa8 | 2007-05-23 15:44:28 -0400 | [diff] [blame] | 100 | second_insert: |
Chris Mason | 7e38180 | 2007-04-19 15:36:27 -0400 | [diff] [blame] | 101 | /* FIXME, use some real flag for selecting the extra index */ |
| 102 | if (root == root->fs_info->tree_root) { |
| 103 | ret = 0; |
| 104 | goto out; |
| 105 | } |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 106 | btrfs_release_path(root, path); |
Chris Mason | 7e38180 | 2007-04-19 15:36:27 -0400 | [diff] [blame] | 107 | |
| 108 | btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY); |
| 109 | key.offset = location->objectid; |
Chris Mason | e06afa8 | 2007-05-23 15:44:28 -0400 | [diff] [blame] | 110 | dir_item = insert_with_overflow(trans, root, path, &key, data_size, |
| 111 | name, name_len); |
Chris Mason | 7e38180 | 2007-04-19 15:36:27 -0400 | [diff] [blame] | 112 | if (IS_ERR(dir_item)) { |
Chris Mason | e06afa8 | 2007-05-23 15:44:28 -0400 | [diff] [blame] | 113 | ret2 = PTR_ERR(dir_item); |
Chris Mason | 7e38180 | 2007-04-19 15:36:27 -0400 | [diff] [blame] | 114 | goto out; |
| 115 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 116 | leaf = path->nodes[0]; |
| 117 | btrfs_cpu_key_to_disk(&disk_key, location); |
| 118 | btrfs_set_dir_item_key(leaf, dir_item, &disk_key); |
| 119 | btrfs_set_dir_type(leaf, dir_item, type); |
| 120 | btrfs_set_dir_flags(leaf, dir_item, 0); |
| 121 | btrfs_set_dir_name_len(leaf, dir_item, name_len); |
| 122 | name_ptr = (unsigned long)(dir_item + 1); |
| 123 | write_extent_buffer(leaf, name, name_ptr, name_len); |
| 124 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 7e38180 | 2007-04-19 15:36:27 -0400 | [diff] [blame] | 125 | out: |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 126 | btrfs_free_path(path); |
Chris Mason | e06afa8 | 2007-05-23 15:44:28 -0400 | [diff] [blame] | 127 | if (ret) |
| 128 | return ret; |
| 129 | if (ret2) |
| 130 | return ret2; |
| 131 | return 0; |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 132 | } |
| 133 | |
Chris Mason | 7e38180 | 2007-04-19 15:36:27 -0400 | [diff] [blame] | 134 | struct btrfs_dir_item *btrfs_lookup_dir_item(struct btrfs_trans_handle *trans, |
| 135 | struct btrfs_root *root, |
| 136 | struct btrfs_path *path, u64 dir, |
| 137 | const char *name, int name_len, |
| 138 | int mod) |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 139 | { |
Chris Mason | 1d4f640 | 2007-03-15 15:18:43 -0400 | [diff] [blame] | 140 | int ret; |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 141 | struct btrfs_key key; |
Chris Mason | 1d4f640 | 2007-03-15 15:18:43 -0400 | [diff] [blame] | 142 | int ins_len = mod < 0 ? -1 : 0; |
| 143 | int cow = mod != 0; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 144 | struct btrfs_key found_key; |
| 145 | struct extent_buffer *leaf; |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 146 | |
| 147 | key.objectid = dir; |
Chris Mason | 1d4f640 | 2007-03-15 15:18:43 -0400 | [diff] [blame] | 148 | btrfs_set_key_type(&key, BTRFS_DIR_ITEM_KEY); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 149 | |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 150 | ret = btrfs_name_hash(name, name_len, &key.offset); |
| 151 | BUG_ON(ret); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 152 | |
Chris Mason | 7e38180 | 2007-04-19 15:36:27 -0400 | [diff] [blame] | 153 | ret = btrfs_search_slot(trans, root, &key, path, ins_len, cow); |
| 154 | if (ret < 0) |
| 155 | return ERR_PTR(ret); |
| 156 | if (ret > 0) { |
| 157 | if (path->slots[0] == 0) |
| 158 | return NULL; |
| 159 | path->slots[0]--; |
Chris Mason | 7fcde0e | 2007-04-05 12:13:21 -0400 | [diff] [blame] | 160 | } |
Chris Mason | 7e38180 | 2007-04-19 15:36:27 -0400 | [diff] [blame] | 161 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 162 | leaf = path->nodes[0]; |
| 163 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 164 | |
| 165 | if (found_key.objectid != dir || |
| 166 | btrfs_key_type(&found_key) != BTRFS_DIR_ITEM_KEY || |
| 167 | found_key.offset != key.offset) |
Chris Mason | 7e38180 | 2007-04-19 15:36:27 -0400 | [diff] [blame] | 168 | return NULL; |
| 169 | |
| 170 | return btrfs_match_dir_item_name(root, path, name, name_len); |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 171 | } |
| 172 | |
Chris Mason | 7e38180 | 2007-04-19 15:36:27 -0400 | [diff] [blame] | 173 | struct btrfs_dir_item * |
| 174 | btrfs_lookup_dir_index_item(struct btrfs_trans_handle *trans, |
| 175 | struct btrfs_root *root, |
| 176 | struct btrfs_path *path, u64 dir, |
| 177 | u64 objectid, const char *name, int name_len, |
| 178 | int mod) |
| 179 | { |
| 180 | int ret; |
| 181 | struct btrfs_key key; |
| 182 | int ins_len = mod < 0 ? -1 : 0; |
| 183 | int cow = mod != 0; |
| 184 | |
| 185 | key.objectid = dir; |
Chris Mason | 7e38180 | 2007-04-19 15:36:27 -0400 | [diff] [blame] | 186 | btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY); |
| 187 | key.offset = objectid; |
| 188 | |
| 189 | ret = btrfs_search_slot(trans, root, &key, path, ins_len, cow); |
| 190 | if (ret < 0) |
| 191 | return ERR_PTR(ret); |
| 192 | if (ret > 0) |
| 193 | return ERR_PTR(-ENOENT); |
| 194 | return btrfs_match_dir_item_name(root, path, name, name_len); |
| 195 | } |
| 196 | |
| 197 | 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] | 198 | struct btrfs_path *path, |
| 199 | const char *name, int name_len) |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 200 | { |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 201 | struct btrfs_dir_item *dir_item; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 202 | unsigned long name_ptr; |
Chris Mason | 7e38180 | 2007-04-19 15:36:27 -0400 | [diff] [blame] | 203 | u32 total_len; |
| 204 | u32 cur = 0; |
| 205 | u32 this_len; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 206 | struct extent_buffer *leaf; |
Chris Mason | a8a2ee0 | 2007-03-16 08:46:49 -0400 | [diff] [blame] | 207 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 208 | leaf = path->nodes[0]; |
Chris Mason | 7e38180 | 2007-04-19 15:36:27 -0400 | [diff] [blame] | 209 | dir_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dir_item); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 210 | total_len = btrfs_item_size_nr(leaf, path->slots[0]); |
Chris Mason | 7e38180 | 2007-04-19 15:36:27 -0400 | [diff] [blame] | 211 | while(cur < total_len) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 212 | this_len = sizeof(*dir_item) + |
| 213 | btrfs_dir_name_len(leaf, dir_item); |
| 214 | name_ptr = (unsigned long)(dir_item + 1); |
Chris Mason | 7e38180 | 2007-04-19 15:36:27 -0400 | [diff] [blame] | 215 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 216 | if (btrfs_dir_name_len(leaf, dir_item) == name_len && |
| 217 | memcmp_extent_buffer(leaf, name, name_ptr, name_len) == 0) |
Chris Mason | 7e38180 | 2007-04-19 15:36:27 -0400 | [diff] [blame] | 218 | return dir_item; |
| 219 | |
| 220 | cur += this_len; |
| 221 | dir_item = (struct btrfs_dir_item *)((char *)dir_item + |
| 222 | this_len); |
| 223 | } |
| 224 | return NULL; |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 225 | } |
Chris Mason | 7e38180 | 2007-04-19 15:36:27 -0400 | [diff] [blame] | 226 | |
| 227 | int btrfs_delete_one_dir_name(struct btrfs_trans_handle *trans, |
| 228 | struct btrfs_root *root, |
| 229 | struct btrfs_path *path, |
| 230 | struct btrfs_dir_item *di) |
| 231 | { |
| 232 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 233 | struct extent_buffer *leaf; |
Chris Mason | 7e38180 | 2007-04-19 15:36:27 -0400 | [diff] [blame] | 234 | u32 sub_item_len; |
| 235 | u32 item_len; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 236 | int ret = 0; |
Chris Mason | 7e38180 | 2007-04-19 15:36:27 -0400 | [diff] [blame] | 237 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 238 | leaf = path->nodes[0]; |
| 239 | sub_item_len = sizeof(*di) + btrfs_dir_name_len(leaf, di); |
| 240 | item_len = btrfs_item_size_nr(leaf, path->slots[0]); |
| 241 | if (sub_item_len == item_len) { |
Chris Mason | 7e38180 | 2007-04-19 15:36:27 -0400 | [diff] [blame] | 242 | ret = btrfs_del_item(trans, root, path); |
Chris Mason | 7e38180 | 2007-04-19 15:36:27 -0400 | [diff] [blame] | 243 | } else { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 244 | /* MARKER */ |
| 245 | unsigned long ptr = (unsigned long)di; |
| 246 | unsigned long start; |
| 247 | |
| 248 | start = btrfs_item_ptr_offset(leaf, path->slots[0]); |
| 249 | memmove_extent_buffer(leaf, ptr, ptr + sub_item_len, |
Chris Mason | 7e38180 | 2007-04-19 15:36:27 -0400 | [diff] [blame] | 250 | item_len - (ptr + sub_item_len - start)); |
| 251 | ret = btrfs_truncate_item(trans, root, path, |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame^] | 252 | item_len - sub_item_len, 1); |
Chris Mason | 7e38180 | 2007-04-19 15:36:27 -0400 | [diff] [blame] | 253 | } |
| 254 | return 0; |
| 255 | } |
| 256 | |