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>
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
31 struct btrfs_root *root,
32 struct btrfs_path *path,
33 struct btrfs_key *cpu_key,
40 struct btrfs_item *item;
41 struct extent_buffer *leaf;
43 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
45 struct btrfs_dir_item *di;
46 di = btrfs_match_dir_item_name(root, path, name, name_len);
48 return ERR_PTR(-EEXIST);
49 ret = btrfs_extend_item(trans, root, path, data_size);
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,
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;
78 key.type = BTRFS_XATTR_ITEM_KEY;
79 key.offset = btrfs_name_hash(name, name_len);
80 path = btrfs_alloc_path();
84 data_size = sizeof(*dir_item) + name_len + data_len;
85 dir_item = insert_with_overflow(trans, root, path, &key, data_size,
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);
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)
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;
128 key.type = BTRFS_DIR_ITEM_KEY;
129 key.offset = btrfs_name_hash(name, name_len);
130 path = btrfs_alloc_path();
133 data_size = sizeof(*dir_item) + name_len;
134 dir_item = insert_with_overflow(trans, root, path, &key, data_size,
136 if (IS_ERR(dir_item)) {
137 ret = PTR_ERR(dir_item);
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) {
157 btrfs_release_path(path);
159 key.type = BTRFS_DIR_INDEX_KEY;
161 dir_item = insert_with_overflow(trans, root, path, &key, data_size,
163 if (IS_ERR(dir_item)) {
164 ret2 = PTR_ERR(dir_item);
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);
177 btrfs_free_path(path);
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,
192 struct btrfs_key key;
193 int ins_len = mod < 0 ? -1 : 0;
195 struct btrfs_key found_key;
196 struct extent_buffer *leaf;
199 key.type = 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);
207 if (path->slots[0] == 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 found_key.type != BTRFS_DIR_ITEM_KEY ||
217 found_key.offset != key.offset)
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,
230 struct btrfs_key key;
231 int ins_len = mod < 0 ? -1 : 0;
235 key.type = BTRFS_DIR_INDEX_KEY;
238 ret = btrfs_search_slot(trans, root, &key, path, ins_len, cow);
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;
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);
269 unsigned long ptr = (unsigned long)di;
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);
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",
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));
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));
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;
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))
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");
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)
347 dir_item = (struct btrfs_dir_item *)((char *)dir_item +