btrfs-progs: check: switch to iterating over the backref_tree
[btrfs-progs-unstable/devel.git] / dir-item.c
blobc2ae953eb44a5c484459de0539aac9164b1d0b0d
1 /*
2 * Copyright (C) 2007 Oracle. All rights reserved.
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.
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.
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.
19 #include <linux/limits.h>
20 #include "ctree.h"
21 #include "disk-io.h"
22 #include "hash.h"
23 #include "transaction.h"
25 static struct btrfs_dir_item *btrfs_match_dir_item_name(struct btrfs_root *root,
26 struct btrfs_path *path,
27 const char *name, int name_len);
29 static struct btrfs_dir_item *insert_with_overflow(struct btrfs_trans_handle
30 *trans,
31 struct btrfs_root *root,
32 struct btrfs_path *path,
33 struct btrfs_key *cpu_key,
34 u32 data_size,
35 const char *name,
36 int name_len)
38 int ret;
39 char *ptr;
40 struct btrfs_item *item;
41 struct extent_buffer *leaf;
43 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
44 if (ret == -EEXIST) {
45 struct btrfs_dir_item *di;
46 di = btrfs_match_dir_item_name(root, path, name, name_len);
47 if (di)
48 return ERR_PTR(-EEXIST);
49 ret = btrfs_extend_item(trans, root, path, data_size);
50 WARN_ON(ret > 0);
52 if (ret < 0)
53 return ERR_PTR(ret);
54 WARN_ON(ret > 0);
55 leaf = path->nodes[0];
56 item = btrfs_item_nr(path->slots[0]);
57 ptr = btrfs_item_ptr(leaf, path->slots[0], char);
58 BUG_ON(data_size > btrfs_item_size(leaf, item));
59 ptr += btrfs_item_size(leaf, item) - data_size;
60 return (struct btrfs_dir_item *)ptr;
63 int btrfs_insert_xattr_item(struct btrfs_trans_handle *trans,
64 struct btrfs_root *root, const char *name,
65 u16 name_len, const void *data, u16 data_len,
66 u64 dir)
68 int ret = 0;
69 struct btrfs_path *path;
70 struct btrfs_dir_item *dir_item;
71 unsigned long name_ptr, data_ptr;
72 struct btrfs_key key, location;
73 struct btrfs_disk_key disk_key;
74 struct extent_buffer *leaf;
75 u32 data_size;
77 key.objectid = dir;
78 btrfs_set_key_type(&key, BTRFS_XATTR_ITEM_KEY);
79 key.offset = btrfs_name_hash(name, name_len);
80 path = btrfs_alloc_path();
81 if (!path)
82 return -ENOMEM;
84 data_size = sizeof(*dir_item) + name_len + data_len;
85 dir_item = insert_with_overflow(trans, root, path, &key, data_size,
86 name, name_len);
88 * FIXME: at some point we should handle xattr's that are larger than
89 * what we can fit in our leaf. We set location to NULL b/c we aren't
90 * pointing at anything else, that will change if we store the xattr
91 * data in a separate inode.
93 BUG_ON(IS_ERR(dir_item));
94 memset(&location, 0, sizeof(location));
96 leaf = path->nodes[0];
97 btrfs_cpu_key_to_disk(&disk_key, &location);
98 btrfs_set_dir_item_key(leaf, dir_item, &disk_key);
99 btrfs_set_dir_type(leaf, dir_item, BTRFS_FT_XATTR);
100 btrfs_set_dir_name_len(leaf, dir_item, name_len);
101 btrfs_set_dir_data_len(leaf, dir_item, data_len);
102 name_ptr = (unsigned long)(dir_item + 1);
103 data_ptr = (unsigned long)((char *)name_ptr + name_len);
105 write_extent_buffer(leaf, name, name_ptr, name_len);
106 write_extent_buffer(leaf, data, data_ptr, data_len);
107 btrfs_mark_buffer_dirty(path->nodes[0]);
109 btrfs_free_path(path);
110 return ret;
113 int btrfs_insert_dir_item(struct btrfs_trans_handle *trans, struct btrfs_root
114 *root, const char *name, int name_len, u64 dir,
115 struct btrfs_key *location, u8 type, u64 index)
117 int ret = 0;
118 int ret2 = 0;
119 struct btrfs_path *path;
120 struct btrfs_dir_item *dir_item;
121 struct extent_buffer *leaf;
122 unsigned long name_ptr;
123 struct btrfs_key key;
124 struct btrfs_disk_key disk_key;
125 u32 data_size;
127 key.objectid = dir;
128 btrfs_set_key_type(&key, BTRFS_DIR_ITEM_KEY);
129 key.offset = btrfs_name_hash(name, name_len);
130 path = btrfs_alloc_path();
131 if (!path)
132 return -ENOMEM;
133 data_size = sizeof(*dir_item) + name_len;
134 dir_item = insert_with_overflow(trans, root, path, &key, data_size,
135 name, name_len);
136 if (IS_ERR(dir_item)) {
137 ret = PTR_ERR(dir_item);
138 goto out;
141 leaf = path->nodes[0];
142 btrfs_cpu_key_to_disk(&disk_key, location);
143 btrfs_set_dir_item_key(leaf, dir_item, &disk_key);
144 btrfs_set_dir_type(leaf, dir_item, type);
145 btrfs_set_dir_data_len(leaf, dir_item, 0);
146 btrfs_set_dir_name_len(leaf, dir_item, name_len);
147 name_ptr = (unsigned long)(dir_item + 1);
149 write_extent_buffer(leaf, name, name_ptr, name_len);
150 btrfs_mark_buffer_dirty(leaf);
152 /* FIXME, use some real flag for selecting the extra index */
153 if (root == root->fs_info->tree_root) {
154 ret = 0;
155 goto out;
157 btrfs_release_path(path);
159 btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
160 key.offset = index;
161 dir_item = insert_with_overflow(trans, root, path, &key, data_size,
162 name, name_len);
163 if (IS_ERR(dir_item)) {
164 ret2 = PTR_ERR(dir_item);
165 goto out;
167 leaf = path->nodes[0];
168 btrfs_cpu_key_to_disk(&disk_key, location);
169 btrfs_set_dir_item_key(leaf, dir_item, &disk_key);
170 btrfs_set_dir_type(leaf, dir_item, type);
171 btrfs_set_dir_data_len(leaf, dir_item, 0);
172 btrfs_set_dir_name_len(leaf, dir_item, name_len);
173 name_ptr = (unsigned long)(dir_item + 1);
174 write_extent_buffer(leaf, name, name_ptr, name_len);
175 btrfs_mark_buffer_dirty(leaf);
176 out:
177 btrfs_free_path(path);
178 if (ret)
179 return ret;
180 if (ret2)
181 return ret2;
182 return 0;
185 struct btrfs_dir_item *btrfs_lookup_dir_item(struct btrfs_trans_handle *trans,
186 struct btrfs_root *root,
187 struct btrfs_path *path, u64 dir,
188 const char *name, int name_len,
189 int mod)
191 int ret;
192 struct btrfs_key key;
193 int ins_len = mod < 0 ? -1 : 0;
194 int cow = mod != 0;
195 struct btrfs_key found_key;
196 struct extent_buffer *leaf;
198 key.objectid = dir;
199 btrfs_set_key_type(&key, BTRFS_DIR_ITEM_KEY);
201 key.offset = btrfs_name_hash(name, name_len);
203 ret = btrfs_search_slot(trans, root, &key, path, ins_len, cow);
204 if (ret < 0)
205 return ERR_PTR(ret);
206 if (ret > 0) {
207 if (path->slots[0] == 0)
208 return NULL;
209 path->slots[0]--;
212 leaf = path->nodes[0];
213 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
215 if (found_key.objectid != dir ||
216 btrfs_key_type(&found_key) != BTRFS_DIR_ITEM_KEY ||
217 found_key.offset != key.offset)
218 return NULL;
220 return btrfs_match_dir_item_name(root, path, name, name_len);
223 struct btrfs_dir_item *btrfs_lookup_dir_index(struct btrfs_trans_handle *trans,
224 struct btrfs_root *root,
225 struct btrfs_path *path, u64 dir,
226 const char *name, int name_len,
227 u64 index, int mod)
229 int ret;
230 struct btrfs_key key;
231 int ins_len = mod < 0 ? -1 : 0;
232 int cow = mod != 0;
234 key.objectid = dir;
235 key.type = BTRFS_DIR_INDEX_KEY;
236 key.offset = index;
238 ret = btrfs_search_slot(trans, root, &key, path, ins_len, cow);
239 if (ret < 0)
240 return ERR_PTR(ret);
241 if (ret > 0)
242 return ERR_PTR(-ENOENT);
244 return btrfs_match_dir_item_name(root, path, name, name_len);
248 * given a pointer into a directory item, delete it. This
249 * handles items that have more than one entry in them.
251 int btrfs_delete_one_dir_name(struct btrfs_trans_handle *trans,
252 struct btrfs_root *root,
253 struct btrfs_path *path,
254 struct btrfs_dir_item *di)
257 struct extent_buffer *leaf;
258 u32 sub_item_len;
259 u32 item_len;
260 int ret = 0;
262 leaf = path->nodes[0];
263 sub_item_len = sizeof(*di) + btrfs_dir_name_len(leaf, di) +
264 btrfs_dir_data_len(leaf, di);
265 item_len = btrfs_item_size_nr(leaf, path->slots[0]);
266 if (sub_item_len == item_len) {
267 ret = btrfs_del_item(trans, root, path);
268 } else {
269 unsigned long ptr = (unsigned long)di;
270 unsigned long start;
272 start = btrfs_item_ptr_offset(leaf, path->slots[0]);
273 memmove_extent_buffer(leaf, ptr, ptr + sub_item_len,
274 item_len - (ptr + sub_item_len - start));
275 btrfs_truncate_item(trans, root, path, item_len - sub_item_len, 1);
277 return ret;
280 static int verify_dir_item(struct btrfs_root *root,
281 struct extent_buffer *leaf,
282 struct btrfs_dir_item *dir_item)
284 u16 namelen = BTRFS_NAME_LEN;
285 u8 type = btrfs_dir_type(leaf, dir_item);
287 if (type >= BTRFS_FT_MAX) {
288 fprintf(stderr, "invalid dir item type: %d\n",
289 (int)type);
290 return 1;
293 if (type == BTRFS_FT_XATTR)
294 namelen = XATTR_NAME_MAX;
296 if (btrfs_dir_name_len(leaf, dir_item) > namelen) {
297 fprintf(stderr, "invalid dir item name len: %u\n",
298 (unsigned)btrfs_dir_data_len(leaf, dir_item));
299 return 1;
302 /* BTRFS_MAX_XATTR_SIZE is the same for all dir items */
303 if ((btrfs_dir_data_len(leaf, dir_item) +
304 btrfs_dir_name_len(leaf, dir_item)) > BTRFS_MAX_XATTR_SIZE(root)) {
305 fprintf(stderr, "invalid dir item name + data len: %u + %u\n",
306 (unsigned)btrfs_dir_name_len(leaf, dir_item),
307 (unsigned)btrfs_dir_data_len(leaf, dir_item));
308 return 1;
311 return 0;
314 static struct btrfs_dir_item *btrfs_match_dir_item_name(struct btrfs_root *root,
315 struct btrfs_path *path,
316 const char *name, int name_len)
318 struct btrfs_dir_item *dir_item;
319 unsigned long name_ptr;
320 u32 total_len;
321 u32 cur = 0;
322 u32 this_len;
323 struct extent_buffer *leaf;
325 leaf = path->nodes[0];
326 dir_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dir_item);
327 total_len = btrfs_item_size_nr(leaf, path->slots[0]);
328 if (verify_dir_item(root, leaf, dir_item))
329 return NULL;
331 while(cur < total_len) {
332 this_len = sizeof(*dir_item) +
333 btrfs_dir_name_len(leaf, dir_item) +
334 btrfs_dir_data_len(leaf, dir_item);
335 if (this_len > (total_len - cur)) {
336 fprintf(stderr, "invalid dir item size\n");
337 return NULL;
340 name_ptr = (unsigned long)(dir_item + 1);
342 if (btrfs_dir_name_len(leaf, dir_item) == name_len &&
343 memcmp_extent_buffer(leaf, name, name_ptr, name_len) == 0)
344 return dir_item;
346 cur += this_len;
347 dir_item = (struct btrfs_dir_item *)((char *)dir_item +
348 this_len);
350 return NULL;