Drop quick-test from the list of programs built by default
[btrfs-progs-unstable.git] / dir-item.c
blobaaaad6a6b6bd9d966ae18ccabf0663b791d7bda0
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 "ctree.h"
20 #include "disk-io.h"
21 #include "hash.h"
22 #include "transaction.h"
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,
29 u32 data_size,
30 const char *name,
31 int name_len)
33 int ret;
34 char *ptr;
35 struct btrfs_item *item;
36 struct extent_buffer *leaf;
38 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
39 if (ret == -EEXIST) {
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);
44 ret = btrfs_extend_item(trans, root, path, data_size);
45 WARN_ON(ret > 0);
47 if (ret < 0)
48 return ERR_PTR(ret);
49 WARN_ON(ret > 0);
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,
61 u64 dir)
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;
72 key.objectid = dir;
73 btrfs_set_key_type(&key, BTRFS_XATTR_ITEM_KEY);
74 key.offset = btrfs_name_hash(name, name_len);
75 path = btrfs_alloc_path();
76 if (!path)
77 return -ENOMEM;
79 data_size = sizeof(*dir_item) + name_len + data_len;
80 dir_item = insert_with_overflow(trans, root, path, &key, data_size,
81 name, name_len);
83 * FIXME: at some point we should handle xattr's that are larger than
84 * what we can fit in our leaf. We set location to NULL b/c we arent
85 * pointing at anything else, that will change if we store the xattr
86 * data in a separate inode.
88 BUG_ON(IS_ERR(dir_item));
89 memset(&location, 0, sizeof(location));
91 leaf = path->nodes[0];
92 btrfs_cpu_key_to_disk(&disk_key, &location);
93 btrfs_set_dir_item_key(leaf, dir_item, &disk_key);
94 btrfs_set_dir_type(leaf, dir_item, BTRFS_FT_XATTR);
95 btrfs_set_dir_name_len(leaf, dir_item, name_len);
96 btrfs_set_dir_data_len(leaf, dir_item, data_len);
97 name_ptr = (unsigned long)(dir_item + 1);
98 data_ptr = (unsigned long)((char *)name_ptr + name_len);
100 write_extent_buffer(leaf, name, name_ptr, name_len);
101 write_extent_buffer(leaf, data, data_ptr, data_len);
102 btrfs_mark_buffer_dirty(path->nodes[0]);
104 btrfs_free_path(path);
105 return ret;
108 int btrfs_insert_dir_item(struct btrfs_trans_handle *trans, struct btrfs_root
109 *root, const char *name, int name_len, u64 dir,
110 struct btrfs_key *location, u8 type)
112 int ret = 0;
113 int ret2 = 0;
114 struct btrfs_path *path;
115 struct btrfs_dir_item *dir_item;
116 struct extent_buffer *leaf;
117 unsigned long name_ptr;
118 struct btrfs_key key;
119 struct btrfs_disk_key disk_key;
120 u32 data_size;
122 key.objectid = dir;
123 btrfs_set_key_type(&key, BTRFS_DIR_ITEM_KEY);
124 key.offset = btrfs_name_hash(name, name_len);
125 path = btrfs_alloc_path();
126 data_size = sizeof(*dir_item) + name_len;
127 dir_item = insert_with_overflow(trans, root, path, &key, data_size,
128 name, name_len);
129 if (IS_ERR(dir_item)) {
130 ret = PTR_ERR(dir_item);
131 if (ret == -EEXIST)
132 goto second_insert;
133 goto out;
136 leaf = path->nodes[0];
137 btrfs_cpu_key_to_disk(&disk_key, location);
138 btrfs_set_dir_item_key(leaf, dir_item, &disk_key);
139 btrfs_set_dir_type(leaf, dir_item, type);
140 btrfs_set_dir_data_len(leaf, dir_item, 0);
141 btrfs_set_dir_name_len(leaf, dir_item, name_len);
142 name_ptr = (unsigned long)(dir_item + 1);
144 write_extent_buffer(leaf, name, name_ptr, name_len);
145 btrfs_mark_buffer_dirty(leaf);
147 second_insert:
148 /* FIXME, use some real flag for selecting the extra index */
149 if (root == root->fs_info->tree_root) {
150 ret = 0;
151 goto out;
153 btrfs_release_path(root, path);
155 btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
156 key.offset = location->objectid;
157 dir_item = insert_with_overflow(trans, root, path, &key, data_size,
158 name, name_len);
159 if (IS_ERR(dir_item)) {
160 ret2 = PTR_ERR(dir_item);
161 goto out;
163 leaf = path->nodes[0];
164 btrfs_cpu_key_to_disk(&disk_key, location);
165 btrfs_set_dir_item_key(leaf, dir_item, &disk_key);
166 btrfs_set_dir_type(leaf, dir_item, type);
167 btrfs_set_dir_data_len(leaf, dir_item, 0);
168 btrfs_set_dir_name_len(leaf, dir_item, name_len);
169 name_ptr = (unsigned long)(dir_item + 1);
170 write_extent_buffer(leaf, name, name_ptr, name_len);
171 btrfs_mark_buffer_dirty(leaf);
172 out:
173 btrfs_free_path(path);
174 if (ret)
175 return ret;
176 if (ret2)
177 return ret2;
178 return 0;
181 struct btrfs_dir_item *btrfs_lookup_dir_item(struct btrfs_trans_handle *trans,
182 struct btrfs_root *root,
183 struct btrfs_path *path, u64 dir,
184 const char *name, int name_len,
185 int mod)
187 int ret;
188 struct btrfs_key key;
189 int ins_len = mod < 0 ? -1 : 0;
190 int cow = mod != 0;
191 struct btrfs_key found_key;
192 struct extent_buffer *leaf;
194 key.objectid = dir;
195 btrfs_set_key_type(&key, BTRFS_DIR_ITEM_KEY);
197 key.offset = btrfs_name_hash(name, name_len);
199 ret = btrfs_search_slot(trans, root, &key, path, ins_len, cow);
200 if (ret < 0)
201 return ERR_PTR(ret);
202 if (ret > 0) {
203 if (path->slots[0] == 0)
204 return NULL;
205 path->slots[0]--;
208 leaf = path->nodes[0];
209 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
211 if (found_key.objectid != dir ||
212 btrfs_key_type(&found_key) != BTRFS_DIR_ITEM_KEY ||
213 found_key.offset != key.offset)
214 return NULL;
216 return btrfs_match_dir_item_name(root, path, name, name_len);
219 struct btrfs_dir_item *
220 btrfs_lookup_dir_index_item(struct btrfs_trans_handle *trans,
221 struct btrfs_root *root,
222 struct btrfs_path *path, u64 dir,
223 u64 objectid, const char *name, int name_len,
224 int mod)
226 int ret;
227 struct btrfs_key key;
228 int ins_len = mod < 0 ? -1 : 0;
229 int cow = mod != 0;
231 key.objectid = dir;
232 btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
233 key.offset = objectid;
235 ret = btrfs_search_slot(trans, root, &key, path, ins_len, cow);
236 if (ret < 0)
237 return ERR_PTR(ret);
238 if (ret > 0)
239 return ERR_PTR(-ENOENT);
240 return btrfs_match_dir_item_name(root, path, name, name_len);
243 struct btrfs_dir_item *btrfs_lookup_xattr(struct btrfs_trans_handle *trans,
244 struct btrfs_root *root,
245 struct btrfs_path *path, u64 dir,
246 const char *name, u16 name_len,
247 int mod)
249 int ret;
250 struct btrfs_key key;
251 int ins_len = mod < 0 ? -1 : 0;
252 int cow = mod != 0;
253 struct btrfs_key found_key;
254 struct extent_buffer *leaf;
256 key.objectid = dir;
257 btrfs_set_key_type(&key, BTRFS_XATTR_ITEM_KEY);
258 key.offset = btrfs_name_hash(name, name_len);
259 ret = btrfs_search_slot(trans, root, &key, path, ins_len, cow);
260 if (ret < 0)
261 return ERR_PTR(ret);
262 if (ret > 0) {
263 if (path->slots[0] == 0)
264 return NULL;
265 path->slots[0]--;
268 leaf = path->nodes[0];
269 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
271 if (found_key.objectid != dir ||
272 btrfs_key_type(&found_key) != BTRFS_XATTR_ITEM_KEY ||
273 found_key.offset != key.offset)
274 return NULL;
276 return btrfs_match_dir_item_name(root, path, name, name_len);
279 struct btrfs_dir_item *btrfs_match_dir_item_name(struct btrfs_root *root,
280 struct btrfs_path *path,
281 const char *name, int name_len)
283 struct btrfs_dir_item *dir_item;
284 unsigned long name_ptr;
285 u32 total_len;
286 u32 cur = 0;
287 u32 this_len;
288 struct extent_buffer *leaf;
290 leaf = path->nodes[0];
291 dir_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dir_item);
292 total_len = btrfs_item_size_nr(leaf, path->slots[0]);
293 while(cur < total_len) {
294 this_len = sizeof(*dir_item) +
295 btrfs_dir_name_len(leaf, dir_item) +
296 btrfs_dir_data_len(leaf, dir_item);
297 name_ptr = (unsigned long)(dir_item + 1);
299 if (btrfs_dir_name_len(leaf, dir_item) == name_len &&
300 memcmp_extent_buffer(leaf, name, name_ptr, name_len) == 0)
301 return dir_item;
303 cur += this_len;
304 dir_item = (struct btrfs_dir_item *)((char *)dir_item +
305 this_len);
307 return NULL;
310 int btrfs_delete_one_dir_name(struct btrfs_trans_handle *trans,
311 struct btrfs_root *root,
312 struct btrfs_path *path,
313 struct btrfs_dir_item *di)
316 struct extent_buffer *leaf;
317 u32 sub_item_len;
318 u32 item_len;
319 int ret = 0;
321 leaf = path->nodes[0];
322 sub_item_len = sizeof(*di) + btrfs_dir_name_len(leaf, di) +
323 btrfs_dir_data_len(leaf, di);
324 item_len = btrfs_item_size_nr(leaf, path->slots[0]);
325 if (sub_item_len == item_len) {
326 ret = btrfs_del_item(trans, root, path);
327 } else {
328 /* MARKER */
329 unsigned long ptr = (unsigned long)di;
330 unsigned long start;
332 start = btrfs_item_ptr_offset(leaf, path->slots[0]);
333 memmove_extent_buffer(leaf, ptr, ptr + sub_item_len,
334 item_len - (ptr + sub_item_len - start));
335 ret = btrfs_truncate_item(trans, root, path,
336 item_len - sub_item_len, 1);
338 return 0;