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); |
Chris Mason | 7fcde0e | 2007-04-05 12:13:21 -0400 | [diff] [blame] | 46 | } |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 47 | if (ret < 0) |
| 48 | return ERR_PTR(ret); |
Chris Mason | 7e38180 | 2007-04-19 15:36:27 -0400 | [diff] [blame] | 49 | WARN_ON(ret > 0); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 50 | leaf = path->nodes[0]; |
| 51 | item = btrfs_item_nr(leaf, path->slots[0]); |
Chris Mason | 7e38180 | 2007-04-19 15:36:27 -0400 | [diff] [blame] | 52 | ptr = btrfs_item_ptr(leaf, path->slots[0], char); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 53 | BUG_ON(data_size > btrfs_item_size(leaf, item)); |
| 54 | ptr += btrfs_item_size(leaf, item) - data_size; |
Chris Mason | 7e38180 | 2007-04-19 15:36:27 -0400 | [diff] [blame] | 55 | return (struct btrfs_dir_item *)ptr; |
Chris Mason | 7fcde0e | 2007-04-05 12:13:21 -0400 | [diff] [blame] | 56 | } |
| 57 | |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 58 | int btrfs_insert_xattr_item(struct btrfs_trans_handle *trans, |
| 59 | struct btrfs_root *root, const char *name, |
| 60 | u16 name_len, const void *data, u16 data_len, |
| 61 | u64 dir) |
| 62 | { |
| 63 | int ret = 0; |
| 64 | struct btrfs_path *path; |
| 65 | struct btrfs_dir_item *dir_item; |
| 66 | unsigned long name_ptr, data_ptr; |
| 67 | struct btrfs_key key, location; |
| 68 | struct btrfs_disk_key disk_key; |
| 69 | struct extent_buffer *leaf; |
| 70 | u32 data_size; |
| 71 | |
| 72 | key.objectid = dir; |
| 73 | btrfs_set_key_type(&key, BTRFS_XATTR_ITEM_KEY); |
David Miller | df68b8a | 2008-02-15 10:40:52 -0500 | [diff] [blame] | 74 | key.offset = btrfs_name_hash(name, name_len); |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 75 | path = btrfs_alloc_path(); |
| 76 | if (!path) |
| 77 | return -ENOMEM; |
Yan | 744f52f | 2008-01-14 13:26:08 -0500 | [diff] [blame] | 78 | if (name_len + data_len + sizeof(struct btrfs_dir_item) > |
| 79 | BTRFS_LEAF_DATA_SIZE(root) - sizeof(struct btrfs_item)) |
| 80 | return -ENOSPC; |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 81 | |
| 82 | data_size = sizeof(*dir_item) + name_len + data_len; |
| 83 | dir_item = insert_with_overflow(trans, root, path, &key, data_size, |
| 84 | name, name_len); |
| 85 | /* |
| 86 | * FIXME: at some point we should handle xattr's that are larger than |
| 87 | * what we can fit in our leaf. We set location to NULL b/c we arent |
| 88 | * pointing at anything else, that will change if we store the xattr |
| 89 | * data in a separate inode. |
| 90 | */ |
| 91 | BUG_ON(IS_ERR(dir_item)); |
| 92 | memset(&location, 0, sizeof(location)); |
| 93 | |
| 94 | leaf = path->nodes[0]; |
| 95 | btrfs_cpu_key_to_disk(&disk_key, &location); |
| 96 | btrfs_set_dir_item_key(leaf, dir_item, &disk_key); |
| 97 | btrfs_set_dir_type(leaf, dir_item, BTRFS_FT_XATTR); |
| 98 | btrfs_set_dir_name_len(leaf, dir_item, name_len); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 99 | btrfs_set_dir_transid(leaf, dir_item, trans->transid); |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 100 | btrfs_set_dir_data_len(leaf, dir_item, data_len); |
| 101 | name_ptr = (unsigned long)(dir_item + 1); |
| 102 | data_ptr = (unsigned long)((char *)name_ptr + name_len); |
| 103 | |
| 104 | write_extent_buffer(leaf, name, name_ptr, name_len); |
| 105 | write_extent_buffer(leaf, data, data_ptr, data_len); |
| 106 | btrfs_mark_buffer_dirty(path->nodes[0]); |
| 107 | |
| 108 | btrfs_free_path(path); |
| 109 | return ret; |
| 110 | } |
| 111 | |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 112 | 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] | 113 | *root, const char *name, int name_len, u64 dir, |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 114 | struct btrfs_key *location, u8 type, u64 index) |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 115 | { |
| 116 | int ret = 0; |
Chris Mason | e06afa8 | 2007-05-23 15:44:28 -0400 | [diff] [blame] | 117 | int ret2 = 0; |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 118 | struct btrfs_path *path; |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 119 | struct btrfs_dir_item *dir_item; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 120 | struct extent_buffer *leaf; |
| 121 | unsigned long name_ptr; |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 122 | struct btrfs_key key; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 123 | struct btrfs_disk_key disk_key; |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 124 | u32 data_size; |
| 125 | |
| 126 | key.objectid = dir; |
Chris Mason | 1d4f640 | 2007-03-15 15:18:43 -0400 | [diff] [blame] | 127 | btrfs_set_key_type(&key, BTRFS_DIR_ITEM_KEY); |
David Miller | df68b8a | 2008-02-15 10:40:52 -0500 | [diff] [blame] | 128 | key.offset = btrfs_name_hash(name, name_len); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 129 | path = btrfs_alloc_path(); |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 130 | data_size = sizeof(*dir_item) + name_len; |
Chris Mason | e06afa8 | 2007-05-23 15:44:28 -0400 | [diff] [blame] | 131 | dir_item = insert_with_overflow(trans, root, path, &key, data_size, |
| 132 | name, name_len); |
Chris Mason | 7e38180 | 2007-04-19 15:36:27 -0400 | [diff] [blame] | 133 | if (IS_ERR(dir_item)) { |
| 134 | ret = PTR_ERR(dir_item); |
Chris Mason | e06afa8 | 2007-05-23 15:44:28 -0400 | [diff] [blame] | 135 | if (ret == -EEXIST) |
| 136 | goto second_insert; |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 137 | goto out; |
Chris Mason | 7e38180 | 2007-04-19 15:36:27 -0400 | [diff] [blame] | 138 | } |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 139 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 140 | leaf = path->nodes[0]; |
| 141 | btrfs_cpu_key_to_disk(&disk_key, location); |
| 142 | btrfs_set_dir_item_key(leaf, dir_item, &disk_key); |
| 143 | btrfs_set_dir_type(leaf, dir_item, type); |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 144 | btrfs_set_dir_data_len(leaf, dir_item, 0); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 145 | btrfs_set_dir_name_len(leaf, dir_item, name_len); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 146 | btrfs_set_dir_transid(leaf, dir_item, trans->transid); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 147 | name_ptr = (unsigned long)(dir_item + 1); |
Chris Mason | c5739bb | 2007-04-10 09:27:04 -0400 | [diff] [blame] | 148 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 149 | write_extent_buffer(leaf, name, name_ptr, name_len); |
| 150 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 7e38180 | 2007-04-19 15:36:27 -0400 | [diff] [blame] | 151 | |
Chris Mason | e06afa8 | 2007-05-23 15:44:28 -0400 | [diff] [blame] | 152 | second_insert: |
Chris Mason | 7e38180 | 2007-04-19 15:36:27 -0400 | [diff] [blame] | 153 | /* FIXME, use some real flag for selecting the extra index */ |
| 154 | if (root == root->fs_info->tree_root) { |
| 155 | ret = 0; |
| 156 | goto out; |
| 157 | } |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 158 | btrfs_release_path(root, path); |
Chris Mason | 7e38180 | 2007-04-19 15:36:27 -0400 | [diff] [blame] | 159 | |
| 160 | btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY); |
Josef Bacik | aec7477 | 2008-07-24 12:12:38 -0400 | [diff] [blame] | 161 | key.offset = index; |
Chris Mason | e06afa8 | 2007-05-23 15:44:28 -0400 | [diff] [blame] | 162 | dir_item = insert_with_overflow(trans, root, path, &key, data_size, |
| 163 | name, name_len); |
Chris Mason | 7e38180 | 2007-04-19 15:36:27 -0400 | [diff] [blame] | 164 | if (IS_ERR(dir_item)) { |
Chris Mason | e06afa8 | 2007-05-23 15:44:28 -0400 | [diff] [blame] | 165 | ret2 = PTR_ERR(dir_item); |
Chris Mason | 7e38180 | 2007-04-19 15:36:27 -0400 | [diff] [blame] | 166 | goto out; |
| 167 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 168 | leaf = path->nodes[0]; |
| 169 | btrfs_cpu_key_to_disk(&disk_key, location); |
| 170 | btrfs_set_dir_item_key(leaf, dir_item, &disk_key); |
| 171 | btrfs_set_dir_type(leaf, dir_item, type); |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 172 | btrfs_set_dir_data_len(leaf, dir_item, 0); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 173 | btrfs_set_dir_name_len(leaf, dir_item, name_len); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 174 | btrfs_set_dir_transid(leaf, dir_item, trans->transid); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 175 | name_ptr = (unsigned long)(dir_item + 1); |
| 176 | write_extent_buffer(leaf, name, name_ptr, name_len); |
| 177 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 7e38180 | 2007-04-19 15:36:27 -0400 | [diff] [blame] | 178 | out: |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 179 | btrfs_free_path(path); |
Chris Mason | e06afa8 | 2007-05-23 15:44:28 -0400 | [diff] [blame] | 180 | if (ret) |
| 181 | return ret; |
| 182 | if (ret2) |
| 183 | return ret2; |
| 184 | return 0; |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 185 | } |
| 186 | |
Chris Mason | 7e38180 | 2007-04-19 15:36:27 -0400 | [diff] [blame] | 187 | struct btrfs_dir_item *btrfs_lookup_dir_item(struct btrfs_trans_handle *trans, |
| 188 | struct btrfs_root *root, |
| 189 | struct btrfs_path *path, u64 dir, |
| 190 | const char *name, int name_len, |
| 191 | int mod) |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 192 | { |
Chris Mason | 1d4f640 | 2007-03-15 15:18:43 -0400 | [diff] [blame] | 193 | int ret; |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 194 | struct btrfs_key key; |
Chris Mason | 1d4f640 | 2007-03-15 15:18:43 -0400 | [diff] [blame] | 195 | int ins_len = mod < 0 ? -1 : 0; |
| 196 | int cow = mod != 0; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 197 | struct btrfs_key found_key; |
| 198 | struct extent_buffer *leaf; |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 199 | |
| 200 | key.objectid = dir; |
Chris Mason | 1d4f640 | 2007-03-15 15:18:43 -0400 | [diff] [blame] | 201 | btrfs_set_key_type(&key, BTRFS_DIR_ITEM_KEY); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 202 | |
David Miller | df68b8a | 2008-02-15 10:40:52 -0500 | [diff] [blame] | 203 | key.offset = btrfs_name_hash(name, name_len); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 204 | |
Chris Mason | 7e38180 | 2007-04-19 15:36:27 -0400 | [diff] [blame] | 205 | ret = btrfs_search_slot(trans, root, &key, path, ins_len, cow); |
| 206 | if (ret < 0) |
| 207 | return ERR_PTR(ret); |
| 208 | if (ret > 0) { |
| 209 | if (path->slots[0] == 0) |
| 210 | return NULL; |
| 211 | path->slots[0]--; |
Chris Mason | 7fcde0e | 2007-04-05 12:13:21 -0400 | [diff] [blame] | 212 | } |
Chris Mason | 7e38180 | 2007-04-19 15:36:27 -0400 | [diff] [blame] | 213 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 214 | leaf = path->nodes[0]; |
| 215 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 216 | |
| 217 | if (found_key.objectid != dir || |
| 218 | btrfs_key_type(&found_key) != BTRFS_DIR_ITEM_KEY || |
| 219 | found_key.offset != key.offset) |
Chris Mason | 7e38180 | 2007-04-19 15:36:27 -0400 | [diff] [blame] | 220 | return NULL; |
| 221 | |
| 222 | return btrfs_match_dir_item_name(root, path, name, name_len); |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 223 | } |
| 224 | |
Chris Mason | 7e38180 | 2007-04-19 15:36:27 -0400 | [diff] [blame] | 225 | struct btrfs_dir_item * |
| 226 | btrfs_lookup_dir_index_item(struct btrfs_trans_handle *trans, |
| 227 | struct btrfs_root *root, |
| 228 | struct btrfs_path *path, u64 dir, |
| 229 | u64 objectid, const char *name, int name_len, |
| 230 | int mod) |
| 231 | { |
| 232 | int ret; |
| 233 | struct btrfs_key key; |
| 234 | int ins_len = mod < 0 ? -1 : 0; |
| 235 | int cow = mod != 0; |
| 236 | |
| 237 | key.objectid = dir; |
Chris Mason | 7e38180 | 2007-04-19 15:36:27 -0400 | [diff] [blame] | 238 | btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY); |
| 239 | key.offset = objectid; |
| 240 | |
| 241 | ret = btrfs_search_slot(trans, root, &key, path, ins_len, cow); |
| 242 | if (ret < 0) |
| 243 | return ERR_PTR(ret); |
| 244 | if (ret > 0) |
| 245 | return ERR_PTR(-ENOENT); |
| 246 | return btrfs_match_dir_item_name(root, path, name, name_len); |
| 247 | } |
| 248 | |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 249 | struct btrfs_dir_item *btrfs_lookup_xattr(struct btrfs_trans_handle *trans, |
| 250 | struct btrfs_root *root, |
| 251 | struct btrfs_path *path, u64 dir, |
| 252 | const char *name, u16 name_len, |
| 253 | int mod) |
| 254 | { |
| 255 | int ret; |
| 256 | struct btrfs_key key; |
| 257 | int ins_len = mod < 0 ? -1 : 0; |
| 258 | int cow = mod != 0; |
| 259 | struct btrfs_key found_key; |
| 260 | struct extent_buffer *leaf; |
| 261 | |
| 262 | key.objectid = dir; |
| 263 | btrfs_set_key_type(&key, BTRFS_XATTR_ITEM_KEY); |
David Miller | df68b8a | 2008-02-15 10:40:52 -0500 | [diff] [blame] | 264 | key.offset = btrfs_name_hash(name, name_len); |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 265 | ret = btrfs_search_slot(trans, root, &key, path, ins_len, cow); |
| 266 | if (ret < 0) |
| 267 | return ERR_PTR(ret); |
| 268 | if (ret > 0) { |
| 269 | if (path->slots[0] == 0) |
| 270 | return NULL; |
| 271 | path->slots[0]--; |
| 272 | } |
| 273 | |
| 274 | leaf = path->nodes[0]; |
| 275 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 276 | |
| 277 | if (found_key.objectid != dir || |
| 278 | btrfs_key_type(&found_key) != BTRFS_XATTR_ITEM_KEY || |
| 279 | found_key.offset != key.offset) |
| 280 | return NULL; |
| 281 | |
| 282 | return btrfs_match_dir_item_name(root, path, name, name_len); |
| 283 | } |
| 284 | |
Chris Mason | 7e38180 | 2007-04-19 15:36:27 -0400 | [diff] [blame] | 285 | 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] | 286 | struct btrfs_path *path, |
| 287 | const char *name, int name_len) |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 288 | { |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 289 | struct btrfs_dir_item *dir_item; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 290 | unsigned long name_ptr; |
Chris Mason | 7e38180 | 2007-04-19 15:36:27 -0400 | [diff] [blame] | 291 | u32 total_len; |
| 292 | u32 cur = 0; |
| 293 | u32 this_len; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 294 | struct extent_buffer *leaf; |
Chris Mason | a8a2ee0 | 2007-03-16 08:46:49 -0400 | [diff] [blame] | 295 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 296 | leaf = path->nodes[0]; |
Chris Mason | 7e38180 | 2007-04-19 15:36:27 -0400 | [diff] [blame] | 297 | 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] | 298 | total_len = btrfs_item_size_nr(leaf, path->slots[0]); |
Chris Mason | 7e38180 | 2007-04-19 15:36:27 -0400 | [diff] [blame] | 299 | while(cur < total_len) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 300 | this_len = sizeof(*dir_item) + |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 301 | btrfs_dir_name_len(leaf, dir_item) + |
| 302 | btrfs_dir_data_len(leaf, dir_item); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 303 | name_ptr = (unsigned long)(dir_item + 1); |
Chris Mason | 7e38180 | 2007-04-19 15:36:27 -0400 | [diff] [blame] | 304 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 305 | if (btrfs_dir_name_len(leaf, dir_item) == name_len && |
| 306 | memcmp_extent_buffer(leaf, name, name_ptr, name_len) == 0) |
Chris Mason | 7e38180 | 2007-04-19 15:36:27 -0400 | [diff] [blame] | 307 | return dir_item; |
| 308 | |
| 309 | cur += this_len; |
| 310 | dir_item = (struct btrfs_dir_item *)((char *)dir_item + |
| 311 | this_len); |
| 312 | } |
| 313 | return NULL; |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 314 | } |
Chris Mason | 7e38180 | 2007-04-19 15:36:27 -0400 | [diff] [blame] | 315 | |
| 316 | int btrfs_delete_one_dir_name(struct btrfs_trans_handle *trans, |
| 317 | struct btrfs_root *root, |
| 318 | struct btrfs_path *path, |
| 319 | struct btrfs_dir_item *di) |
| 320 | { |
| 321 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 322 | struct extent_buffer *leaf; |
Chris Mason | 7e38180 | 2007-04-19 15:36:27 -0400 | [diff] [blame] | 323 | u32 sub_item_len; |
| 324 | u32 item_len; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 325 | int ret = 0; |
Chris Mason | 7e38180 | 2007-04-19 15:36:27 -0400 | [diff] [blame] | 326 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 327 | leaf = path->nodes[0]; |
Josef Bacik | 5103e94 | 2007-11-16 11:45:54 -0500 | [diff] [blame] | 328 | sub_item_len = sizeof(*di) + btrfs_dir_name_len(leaf, di) + |
| 329 | btrfs_dir_data_len(leaf, di); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 330 | item_len = btrfs_item_size_nr(leaf, path->slots[0]); |
| 331 | if (sub_item_len == item_len) { |
Chris Mason | 7e38180 | 2007-04-19 15:36:27 -0400 | [diff] [blame] | 332 | ret = btrfs_del_item(trans, root, path); |
Chris Mason | 7e38180 | 2007-04-19 15:36:27 -0400 | [diff] [blame] | 333 | } else { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 334 | /* MARKER */ |
| 335 | unsigned long ptr = (unsigned long)di; |
| 336 | unsigned long start; |
| 337 | |
| 338 | start = btrfs_item_ptr_offset(leaf, path->slots[0]); |
| 339 | memmove_extent_buffer(leaf, ptr, ptr + sub_item_len, |
Chris Mason | 7e38180 | 2007-04-19 15:36:27 -0400 | [diff] [blame] | 340 | item_len - (ptr + sub_item_len - start)); |
| 341 | ret = btrfs_truncate_item(trans, root, path, |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 342 | item_len - sub_item_len, 1); |
Chris Mason | 7e38180 | 2007-04-19 15:36:27 -0400 | [diff] [blame] | 343 | } |
| 344 | return 0; |
| 345 | } |