btrfs-progs: Refactor the root used bytes are updated
[btrfs-progs-unstable/devel.git] / dir-item.c
blobd64f71e306e08f2701fd598ba05d1a69234d1f80
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(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 key.type = 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 key.type = 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);
139 /* Continue to insert item if existed */
140 if (ret == -EEXIST) {
141 ret = 0;
142 goto insert;
143 } else {
144 goto out;
148 leaf = path->nodes[0];
149 btrfs_cpu_key_to_disk(&disk_key, location);
150 btrfs_set_dir_item_key(leaf, dir_item, &disk_key);
151 btrfs_set_dir_type(leaf, dir_item, type);
152 btrfs_set_dir_data_len(leaf, dir_item, 0);
153 btrfs_set_dir_name_len(leaf, dir_item, name_len);
154 name_ptr = (unsigned long)(dir_item + 1);
156 write_extent_buffer(leaf, name, name_ptr, name_len);
157 btrfs_mark_buffer_dirty(leaf);
159 insert:
160 /* FIXME, use some real flag for selecting the extra index */
161 if (root == root->fs_info->tree_root) {
162 ret = 0;
163 goto out;
165 btrfs_release_path(path);
167 key.type = BTRFS_DIR_INDEX_KEY;
168 key.offset = index;
169 dir_item = insert_with_overflow(trans, root, path, &key, data_size,
170 name, name_len);
171 if (IS_ERR(dir_item)) {
172 ret2 = PTR_ERR(dir_item);
173 if (ret2 == -EEXIST)
174 ret = 0;
175 goto out;
177 leaf = path->nodes[0];
178 btrfs_cpu_key_to_disk(&disk_key, location);
179 btrfs_set_dir_item_key(leaf, dir_item, &disk_key);
180 btrfs_set_dir_type(leaf, dir_item, type);
181 btrfs_set_dir_data_len(leaf, dir_item, 0);
182 btrfs_set_dir_name_len(leaf, dir_item, name_len);
183 name_ptr = (unsigned long)(dir_item + 1);
184 write_extent_buffer(leaf, name, name_ptr, name_len);
185 btrfs_mark_buffer_dirty(leaf);
186 out:
187 btrfs_free_path(path);
188 if (ret)
189 return ret;
190 if (ret2)
191 return ret2;
192 return 0;
195 struct btrfs_dir_item *btrfs_lookup_dir_item(struct btrfs_trans_handle *trans,
196 struct btrfs_root *root,
197 struct btrfs_path *path, u64 dir,
198 const char *name, int name_len,
199 int mod)
201 int ret;
202 struct btrfs_key key;
203 int ins_len = mod < 0 ? -1 : 0;
204 int cow = mod != 0;
205 struct btrfs_key found_key;
206 struct extent_buffer *leaf;
208 key.objectid = dir;
209 key.type = BTRFS_DIR_ITEM_KEY;
211 key.offset = btrfs_name_hash(name, name_len);
213 ret = btrfs_search_slot(trans, root, &key, path, ins_len, cow);
214 if (ret < 0)
215 return ERR_PTR(ret);
216 if (ret > 0) {
217 if (path->slots[0] == 0)
218 return NULL;
219 path->slots[0]--;
222 leaf = path->nodes[0];
223 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
225 if (found_key.objectid != dir ||
226 found_key.type != BTRFS_DIR_ITEM_KEY ||
227 found_key.offset != key.offset)
228 return NULL;
230 return btrfs_match_dir_item_name(root, path, name, name_len);
233 struct btrfs_dir_item *btrfs_lookup_dir_index(struct btrfs_trans_handle *trans,
234 struct btrfs_root *root,
235 struct btrfs_path *path, u64 dir,
236 const char *name, int name_len,
237 u64 index, int mod)
239 int ret;
240 struct btrfs_key key;
241 int ins_len = mod < 0 ? -1 : 0;
242 int cow = mod != 0;
244 key.objectid = dir;
245 key.type = BTRFS_DIR_INDEX_KEY;
246 key.offset = index;
248 ret = btrfs_search_slot(trans, root, &key, path, ins_len, cow);
249 if (ret < 0)
250 return ERR_PTR(ret);
251 if (ret > 0)
252 return ERR_PTR(-ENOENT);
254 return btrfs_match_dir_item_name(root, path, name, name_len);
258 * given a pointer into a directory item, delete it. This
259 * handles items that have more than one entry in them.
261 int btrfs_delete_one_dir_name(struct btrfs_trans_handle *trans,
262 struct btrfs_root *root,
263 struct btrfs_path *path,
264 struct btrfs_dir_item *di)
266 struct extent_buffer *leaf;
267 u32 sub_item_len;
268 u32 item_len;
269 int ret = 0;
271 leaf = path->nodes[0];
272 sub_item_len = sizeof(*di) + btrfs_dir_name_len(leaf, di) +
273 btrfs_dir_data_len(leaf, di);
274 item_len = btrfs_item_size_nr(leaf, path->slots[0]);
277 * If @sub_item_len is longer than @item_len, then it means the
278 * name_len is just corrupted.
279 * No good idea to know if there is anything we can recover from
280 * the corrupted item.
281 * Just delete the item.
283 if (sub_item_len >= item_len) {
284 ret = btrfs_del_item(trans, root, path);
285 } else {
286 unsigned long ptr = (unsigned long)di;
287 unsigned long start;
289 start = btrfs_item_ptr_offset(leaf, path->slots[0]);
290 memmove_extent_buffer(leaf, ptr, ptr + sub_item_len,
291 item_len - (ptr + sub_item_len - start));
292 btrfs_truncate_item(root, path, item_len - sub_item_len, 1);
294 return ret;
297 static int verify_dir_item(struct btrfs_root *root,
298 struct extent_buffer *leaf,
299 struct btrfs_dir_item *dir_item)
301 u16 namelen = BTRFS_NAME_LEN;
302 u8 type = btrfs_dir_type(leaf, dir_item);
304 if (type == BTRFS_FT_XATTR)
305 namelen = XATTR_NAME_MAX;
307 if (btrfs_dir_name_len(leaf, dir_item) > namelen) {
308 fprintf(stderr, "invalid dir item name len: %u\n",
309 (unsigned)btrfs_dir_data_len(leaf, dir_item));
310 return 1;
313 /* BTRFS_MAX_XATTR_SIZE is the same for all dir items */
314 if ((btrfs_dir_data_len(leaf, dir_item) +
315 btrfs_dir_name_len(leaf, dir_item)) >
316 BTRFS_MAX_XATTR_SIZE(root->fs_info)) {
317 fprintf(stderr, "invalid dir item name + data len: %u + %u\n",
318 (unsigned)btrfs_dir_name_len(leaf, dir_item),
319 (unsigned)btrfs_dir_data_len(leaf, dir_item));
320 return 1;
323 return 0;
326 static struct btrfs_dir_item *btrfs_match_dir_item_name(struct btrfs_root *root,
327 struct btrfs_path *path,
328 const char *name, int name_len)
330 struct btrfs_dir_item *dir_item;
331 unsigned long name_ptr;
332 u32 total_len;
333 u32 cur = 0;
334 u32 this_len;
335 struct extent_buffer *leaf;
337 leaf = path->nodes[0];
338 dir_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dir_item);
339 total_len = btrfs_item_size_nr(leaf, path->slots[0]);
340 if (verify_dir_item(root, leaf, dir_item))
341 return NULL;
343 while(cur < total_len) {
344 this_len = sizeof(*dir_item) +
345 btrfs_dir_name_len(leaf, dir_item) +
346 btrfs_dir_data_len(leaf, dir_item);
347 if (this_len > (total_len - cur)) {
348 fprintf(stderr, "invalid dir item size\n");
349 return NULL;
352 name_ptr = (unsigned long)(dir_item + 1);
354 if (btrfs_dir_name_len(leaf, dir_item) == name_len &&
355 memcmp_extent_buffer(leaf, name, name_ptr, name_len) == 0)
356 return dir_item;
358 cur += this_len;
359 dir_item = (struct btrfs_dir_item *)((char *)dir_item +
360 this_len);
362 return NULL;