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.
22 #include "transaction.h"
24 static struct btrfs_dir_item *insert_with_overflow(struct btrfs_trans_handle
26 struct btrfs_root *root,
27 struct btrfs_path *path,
28 struct btrfs_key *cpu_key,
35 struct btrfs_item *item;
36 struct extent_buffer *leaf;
38 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
40 struct btrfs_dir_item *di;
41 di = btrfs_match_dir_item_name(root, path, name, name_len);
43 return ERR_PTR(-EEXIST);
44 ret = btrfs_extend_item(trans, root, path, data_size);
50 leaf = path->nodes[0];
51 item = btrfs_item_nr(leaf, path->slots[0]);
52 ptr = btrfs_item_ptr(leaf, path->slots[0], char);
53 BUG_ON(data_size > btrfs_item_size(leaf, item));
54 ptr += btrfs_item_size(leaf, item) - data_size;
55 return (struct btrfs_dir_item *)ptr;
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,
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;
73 btrfs_set_key_type(&key, BTRFS_XATTR_ITEM_KEY);
74 ret = btrfs_name_hash(name, name_len, &key.offset);
76 path = btrfs_alloc_path();
80 data_size = sizeof(*dir_item) + name_len + data_len;
81 dir_item = insert_with_overflow(trans, root, path, &key, data_size,
84 * FIXME: at some point we should handle xattr's that are larger than
85 * what we can fit in our leaf. We set location to NULL b/c we arent
86 * pointing at anything else, that will change if we store the xattr
87 * data in a separate inode.
89 BUG_ON(IS_ERR(dir_item));
90 memset(&location, 0, sizeof(location));
92 leaf = path->nodes[0];
93 btrfs_cpu_key_to_disk(&disk_key, &location);
94 btrfs_set_dir_item_key(leaf, dir_item, &disk_key);
95 btrfs_set_dir_type(leaf, dir_item, BTRFS_FT_XATTR);
96 btrfs_set_dir_name_len(leaf, dir_item, name_len);
97 btrfs_set_dir_data_len(leaf, dir_item, data_len);
98 name_ptr = (unsigned long)(dir_item + 1);
99 data_ptr = (unsigned long)((char *)name_ptr + name_len);
101 write_extent_buffer(leaf, name, name_ptr, name_len);
102 write_extent_buffer(leaf, data, data_ptr, data_len);
103 btrfs_mark_buffer_dirty(path->nodes[0]);
105 btrfs_free_path(path);
109 int btrfs_insert_dir_item(struct btrfs_trans_handle *trans, struct btrfs_root
110 *root, const char *name, int name_len, u64 dir,
111 struct btrfs_key *location, u8 type)
115 struct btrfs_path *path;
116 struct btrfs_dir_item *dir_item;
117 struct extent_buffer *leaf;
118 unsigned long name_ptr;
119 struct btrfs_key key;
120 struct btrfs_disk_key disk_key;
124 btrfs_set_key_type(&key, BTRFS_DIR_ITEM_KEY);
125 ret = btrfs_name_hash(name, name_len, &key.offset);
127 path = btrfs_alloc_path();
128 data_size = sizeof(*dir_item) + name_len;
129 dir_item = insert_with_overflow(trans, root, path, &key, data_size,
131 if (IS_ERR(dir_item)) {
132 ret = PTR_ERR(dir_item);
138 leaf = path->nodes[0];
139 btrfs_cpu_key_to_disk(&disk_key, location);
140 btrfs_set_dir_item_key(leaf, dir_item, &disk_key);
141 btrfs_set_dir_type(leaf, dir_item, type);
142 btrfs_set_dir_data_len(leaf, dir_item, 0);
143 btrfs_set_dir_name_len(leaf, dir_item, name_len);
144 name_ptr = (unsigned long)(dir_item + 1);
146 write_extent_buffer(leaf, name, name_ptr, name_len);
147 btrfs_mark_buffer_dirty(leaf);
150 /* FIXME, use some real flag for selecting the extra index */
151 if (root == root->fs_info->tree_root) {
155 btrfs_release_path(root, path);
157 btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
158 key.offset = location->objectid;
159 dir_item = insert_with_overflow(trans, root, path, &key, data_size,
161 if (IS_ERR(dir_item)) {
162 ret2 = PTR_ERR(dir_item);
165 leaf = path->nodes[0];
166 btrfs_cpu_key_to_disk(&disk_key, location);
167 btrfs_set_dir_item_key(leaf, dir_item, &disk_key);
168 btrfs_set_dir_type(leaf, dir_item, type);
169 btrfs_set_dir_data_len(leaf, dir_item, 0);
170 btrfs_set_dir_name_len(leaf, dir_item, name_len);
171 name_ptr = (unsigned long)(dir_item + 1);
172 write_extent_buffer(leaf, name, name_ptr, name_len);
173 btrfs_mark_buffer_dirty(leaf);
175 btrfs_free_path(path);
183 struct btrfs_dir_item *btrfs_lookup_dir_item(struct btrfs_trans_handle *trans,
184 struct btrfs_root *root,
185 struct btrfs_path *path, u64 dir,
186 const char *name, int name_len,
190 struct btrfs_key key;
191 int ins_len = mod < 0 ? -1 : 0;
193 struct btrfs_key found_key;
194 struct extent_buffer *leaf;
197 btrfs_set_key_type(&key, BTRFS_DIR_ITEM_KEY);
199 ret = btrfs_name_hash(name, name_len, &key.offset);
202 ret = btrfs_search_slot(trans, root, &key, path, ins_len, cow);
206 if (path->slots[0] == 0)
211 leaf = path->nodes[0];
212 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
214 if (found_key.objectid != dir ||
215 btrfs_key_type(&found_key) != BTRFS_DIR_ITEM_KEY ||
216 found_key.offset != key.offset)
219 return btrfs_match_dir_item_name(root, path, name, name_len);
222 struct btrfs_dir_item *
223 btrfs_lookup_dir_index_item(struct btrfs_trans_handle *trans,
224 struct btrfs_root *root,
225 struct btrfs_path *path, u64 dir,
226 u64 objectid, const char *name, int name_len,
230 struct btrfs_key key;
231 int ins_len = mod < 0 ? -1 : 0;
235 btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
236 key.offset = objectid;
238 ret = btrfs_search_slot(trans, root, &key, path, ins_len, cow);
242 return ERR_PTR(-ENOENT);
243 return btrfs_match_dir_item_name(root, path, name, name_len);
246 struct btrfs_dir_item *btrfs_lookup_xattr(struct btrfs_trans_handle *trans,
247 struct btrfs_root *root,
248 struct btrfs_path *path, u64 dir,
249 const char *name, u16 name_len,
253 struct btrfs_key key;
254 int ins_len = mod < 0 ? -1 : 0;
256 struct btrfs_key found_key;
257 struct extent_buffer *leaf;
260 btrfs_set_key_type(&key, BTRFS_XATTR_ITEM_KEY);
261 ret = btrfs_name_hash(name, name_len, &key.offset);
263 ret = btrfs_search_slot(trans, root, &key, path, ins_len, cow);
267 if (path->slots[0] == 0)
272 leaf = path->nodes[0];
273 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
275 if (found_key.objectid != dir ||
276 btrfs_key_type(&found_key) != BTRFS_XATTR_ITEM_KEY ||
277 found_key.offset != key.offset)
280 return btrfs_match_dir_item_name(root, path, name, name_len);
283 struct btrfs_dir_item *btrfs_match_dir_item_name(struct btrfs_root *root,
284 struct btrfs_path *path,
285 const char *name, int name_len)
287 struct btrfs_dir_item *dir_item;
288 unsigned long name_ptr;
292 struct extent_buffer *leaf;
294 leaf = path->nodes[0];
295 dir_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dir_item);
296 total_len = btrfs_item_size_nr(leaf, path->slots[0]);
297 while(cur < total_len) {
298 this_len = sizeof(*dir_item) +
299 btrfs_dir_name_len(leaf, dir_item) +
300 btrfs_dir_data_len(leaf, dir_item);
301 name_ptr = (unsigned long)(dir_item + 1);
303 if (btrfs_dir_name_len(leaf, dir_item) == name_len &&
304 memcmp_extent_buffer(leaf, name, name_ptr, name_len) == 0)
308 dir_item = (struct btrfs_dir_item *)((char *)dir_item +
314 int btrfs_delete_one_dir_name(struct btrfs_trans_handle *trans,
315 struct btrfs_root *root,
316 struct btrfs_path *path,
317 struct btrfs_dir_item *di)
320 struct extent_buffer *leaf;
325 leaf = path->nodes[0];
326 sub_item_len = sizeof(*di) + btrfs_dir_name_len(leaf, di) +
327 btrfs_dir_data_len(leaf, di);
328 item_len = btrfs_item_size_nr(leaf, path->slots[0]);
329 if (sub_item_len == item_len) {
330 ret = btrfs_del_item(trans, root, path);
333 unsigned long ptr = (unsigned long)di;
336 start = btrfs_item_ptr_offset(leaf, path->slots[0]);
337 memmove_extent_buffer(leaf, ptr, ptr + sub_item_len,
338 item_len - (ptr + sub_item_len - start));
339 ret = btrfs_truncate_item(trans, root, path,
340 item_len - sub_item_len, 1);