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