blob: 5247a9a41f08d5c05d04cd34fb5a7bdf9ac51756 [file] [log] [blame]
Chris Mason6cbd5572007-06-12 09:07:21 -04001/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
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.
7 *
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.
12 *
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.
17 */
18
Chris Mason62e27492007-03-15 12:56:47 -040019#include "ctree.h"
20#include "disk-io.h"
21#include "hash.h"
Chris Masone089f052007-03-16 16:20:31 -040022#include "transaction.h"
Chris Mason62e27492007-03-15 12:56:47 -040023
Chris Mason35b7e472007-05-02 15:53:43 -040024static 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,
Chris Masone06afa82007-05-23 15:44:28 -040029 u32 data_size,
30 const char *name,
31 int name_len)
Chris Mason7fcde0e2007-04-05 12:13:21 -040032{
Chris Mason7fcde0e2007-04-05 12:13:21 -040033 int ret;
Chris Mason7e381802007-04-19 15:36:27 -040034 char *ptr;
35 struct btrfs_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -040036 struct extent_buffer *leaf;
Chris Mason7fcde0e2007-04-05 12:13:21 -040037
38 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
Chris Mason7e381802007-04-19 15:36:27 -040039 if (ret == -EEXIST) {
Chris Masone06afa82007-05-23 15:44:28 -040040 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);
Chris Mason7e381802007-04-19 15:36:27 -040044 ret = btrfs_extend_item(trans, root, path, data_size);
45 WARN_ON(ret > 0);
Chris Mason7fcde0e2007-04-05 12:13:21 -040046 }
Chris Mason54aa1f42007-06-22 14:16:25 -040047 if (ret < 0)
48 return ERR_PTR(ret);
Chris Mason7e381802007-04-19 15:36:27 -040049 WARN_ON(ret > 0);
Chris Mason5f39d392007-10-15 16:14:19 -040050 leaf = path->nodes[0];
51 item = btrfs_item_nr(leaf, path->slots[0]);
Chris Mason7e381802007-04-19 15:36:27 -040052 ptr = btrfs_item_ptr(leaf, path->slots[0], char);
Chris Mason5f39d392007-10-15 16:14:19 -040053 BUG_ON(data_size > btrfs_item_size(leaf, item));
54 ptr += btrfs_item_size(leaf, item) - data_size;
Chris Mason7e381802007-04-19 15:36:27 -040055 return (struct btrfs_dir_item *)ptr;
Chris Mason7fcde0e2007-04-05 12:13:21 -040056}
57
Josef Bacik5103e942007-11-16 11:45:54 -050058int 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)
62{
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;
71
72 key.objectid = dir;
73 btrfs_set_key_type(&key, BTRFS_XATTR_ITEM_KEY);
74 ret = btrfs_name_hash(name, name_len, &key.offset);
75 BUG_ON(ret);
76 path = btrfs_alloc_path();
77 if (!path)
78 return -ENOMEM;
Yan744f52f2008-01-14 13:26:08 -050079 if (name_len + data_len + sizeof(struct btrfs_dir_item) >
80 BTRFS_LEAF_DATA_SIZE(root) - sizeof(struct btrfs_item))
81 return -ENOSPC;
Josef Bacik5103e942007-11-16 11:45:54 -050082
83 data_size = sizeof(*dir_item) + name_len + data_len;
84 dir_item = insert_with_overflow(trans, root, path, &key, data_size,
85 name, name_len);
86 /*
87 * FIXME: at some point we should handle xattr's that are larger than
88 * what we can fit in our leaf. We set location to NULL b/c we arent
89 * pointing at anything else, that will change if we store the xattr
90 * data in a separate inode.
91 */
92 BUG_ON(IS_ERR(dir_item));
93 memset(&location, 0, sizeof(location));
94
95 leaf = path->nodes[0];
96 btrfs_cpu_key_to_disk(&disk_key, &location);
97 btrfs_set_dir_item_key(leaf, dir_item, &disk_key);
98 btrfs_set_dir_type(leaf, dir_item, BTRFS_FT_XATTR);
99 btrfs_set_dir_name_len(leaf, dir_item, name_len);
100 btrfs_set_dir_data_len(leaf, dir_item, data_len);
101 name_ptr = (unsigned long)(dir_item + 1);
102 data_ptr = (unsigned long)((char *)name_ptr + name_len);
103
104 write_extent_buffer(leaf, name, name_ptr, name_len);
105 write_extent_buffer(leaf, data, data_ptr, data_len);
106 btrfs_mark_buffer_dirty(path->nodes[0]);
107
108 btrfs_free_path(path);
109 return ret;
110}
111
Chris Masone089f052007-03-16 16:20:31 -0400112int btrfs_insert_dir_item(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masond6e4a422007-04-06 15:37:36 -0400113 *root, const char *name, int name_len, u64 dir,
114 struct btrfs_key *location, u8 type)
Chris Mason62e27492007-03-15 12:56:47 -0400115{
116 int ret = 0;
Chris Masone06afa82007-05-23 15:44:28 -0400117 int ret2 = 0;
Chris Mason5caf2a02007-04-02 11:20:42 -0400118 struct btrfs_path *path;
Chris Mason62e27492007-03-15 12:56:47 -0400119 struct btrfs_dir_item *dir_item;
Chris Mason5f39d392007-10-15 16:14:19 -0400120 struct extent_buffer *leaf;
121 unsigned long name_ptr;
Chris Mason62e27492007-03-15 12:56:47 -0400122 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -0400123 struct btrfs_disk_key disk_key;
Chris Mason62e27492007-03-15 12:56:47 -0400124 u32 data_size;
125
126 key.objectid = dir;
Chris Mason1d4f6402007-03-15 15:18:43 -0400127 btrfs_set_key_type(&key, BTRFS_DIR_ITEM_KEY);
Chris Masone20d96d2007-03-22 12:13:20 -0400128 ret = btrfs_name_hash(name, name_len, &key.offset);
Chris Mason62e27492007-03-15 12:56:47 -0400129 BUG_ON(ret);
Chris Mason5caf2a02007-04-02 11:20:42 -0400130 path = btrfs_alloc_path();
Chris Mason62e27492007-03-15 12:56:47 -0400131 data_size = sizeof(*dir_item) + name_len;
Chris Masone06afa82007-05-23 15:44:28 -0400132 dir_item = insert_with_overflow(trans, root, path, &key, data_size,
133 name, name_len);
Chris Mason7e381802007-04-19 15:36:27 -0400134 if (IS_ERR(dir_item)) {
135 ret = PTR_ERR(dir_item);
Chris Masone06afa82007-05-23 15:44:28 -0400136 if (ret == -EEXIST)
137 goto second_insert;
Chris Mason62e27492007-03-15 12:56:47 -0400138 goto out;
Chris Mason7e381802007-04-19 15:36:27 -0400139 }
Chris Mason62e27492007-03-15 12:56:47 -0400140
Chris Mason5f39d392007-10-15 16:14:19 -0400141 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);
Josef Bacik5103e942007-11-16 11:45:54 -0500145 btrfs_set_dir_data_len(leaf, dir_item, 0);
Chris Mason5f39d392007-10-15 16:14:19 -0400146 btrfs_set_dir_name_len(leaf, dir_item, name_len);
147 name_ptr = (unsigned long)(dir_item + 1);
Chris Masonc5739bb2007-04-10 09:27:04 -0400148
Chris Mason5f39d392007-10-15 16:14:19 -0400149 write_extent_buffer(leaf, name, name_ptr, name_len);
150 btrfs_mark_buffer_dirty(leaf);
Chris Mason7e381802007-04-19 15:36:27 -0400151
Chris Masone06afa82007-05-23 15:44:28 -0400152second_insert:
Chris Mason7e381802007-04-19 15:36:27 -0400153 /* FIXME, use some real flag for selecting the extra index */
154 if (root == root->fs_info->tree_root) {
155 ret = 0;
156 goto out;
157 }
Chris Mason5caf2a02007-04-02 11:20:42 -0400158 btrfs_release_path(root, path);
Chris Mason7e381802007-04-19 15:36:27 -0400159
160 btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
161 key.offset = location->objectid;
Chris Masone06afa82007-05-23 15:44:28 -0400162 dir_item = insert_with_overflow(trans, root, path, &key, data_size,
163 name, name_len);
Chris Mason7e381802007-04-19 15:36:27 -0400164 if (IS_ERR(dir_item)) {
Chris Masone06afa82007-05-23 15:44:28 -0400165 ret2 = PTR_ERR(dir_item);
Chris Mason7e381802007-04-19 15:36:27 -0400166 goto out;
167 }
Chris Mason5f39d392007-10-15 16:14:19 -0400168 leaf = path->nodes[0];
169 btrfs_cpu_key_to_disk(&disk_key, location);
170 btrfs_set_dir_item_key(leaf, dir_item, &disk_key);
171 btrfs_set_dir_type(leaf, dir_item, type);
Josef Bacik5103e942007-11-16 11:45:54 -0500172 btrfs_set_dir_data_len(leaf, dir_item, 0);
Chris Mason5f39d392007-10-15 16:14:19 -0400173 btrfs_set_dir_name_len(leaf, dir_item, name_len);
174 name_ptr = (unsigned long)(dir_item + 1);
175 write_extent_buffer(leaf, name, name_ptr, name_len);
176 btrfs_mark_buffer_dirty(leaf);
Chris Mason7e381802007-04-19 15:36:27 -0400177out:
Chris Mason5caf2a02007-04-02 11:20:42 -0400178 btrfs_free_path(path);
Chris Masone06afa82007-05-23 15:44:28 -0400179 if (ret)
180 return ret;
181 if (ret2)
182 return ret2;
183 return 0;
Chris Mason62e27492007-03-15 12:56:47 -0400184}
185
Chris Mason7e381802007-04-19 15:36:27 -0400186struct btrfs_dir_item *btrfs_lookup_dir_item(struct btrfs_trans_handle *trans,
187 struct btrfs_root *root,
188 struct btrfs_path *path, u64 dir,
189 const char *name, int name_len,
190 int mod)
Chris Mason62e27492007-03-15 12:56:47 -0400191{
Chris Mason1d4f6402007-03-15 15:18:43 -0400192 int ret;
Chris Mason62e27492007-03-15 12:56:47 -0400193 struct btrfs_key key;
Chris Mason1d4f6402007-03-15 15:18:43 -0400194 int ins_len = mod < 0 ? -1 : 0;
195 int cow = mod != 0;
Chris Mason5f39d392007-10-15 16:14:19 -0400196 struct btrfs_key found_key;
197 struct extent_buffer *leaf;
Chris Mason62e27492007-03-15 12:56:47 -0400198
199 key.objectid = dir;
Chris Mason1d4f6402007-03-15 15:18:43 -0400200 btrfs_set_key_type(&key, BTRFS_DIR_ITEM_KEY);
Chris Mason5f39d392007-10-15 16:14:19 -0400201
Chris Mason62e27492007-03-15 12:56:47 -0400202 ret = btrfs_name_hash(name, name_len, &key.offset);
203 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -0400204
Chris Mason7e381802007-04-19 15:36:27 -0400205 ret = btrfs_search_slot(trans, root, &key, path, ins_len, cow);
206 if (ret < 0)
207 return ERR_PTR(ret);
208 if (ret > 0) {
209 if (path->slots[0] == 0)
210 return NULL;
211 path->slots[0]--;
Chris Mason7fcde0e2007-04-05 12:13:21 -0400212 }
Chris Mason7e381802007-04-19 15:36:27 -0400213
Chris Mason5f39d392007-10-15 16:14:19 -0400214 leaf = path->nodes[0];
215 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
216
217 if (found_key.objectid != dir ||
218 btrfs_key_type(&found_key) != BTRFS_DIR_ITEM_KEY ||
219 found_key.offset != key.offset)
Chris Mason7e381802007-04-19 15:36:27 -0400220 return NULL;
221
222 return btrfs_match_dir_item_name(root, path, name, name_len);
Chris Mason62e27492007-03-15 12:56:47 -0400223}
224
Chris Mason7e381802007-04-19 15:36:27 -0400225struct btrfs_dir_item *
226btrfs_lookup_dir_index_item(struct btrfs_trans_handle *trans,
227 struct btrfs_root *root,
228 struct btrfs_path *path, u64 dir,
229 u64 objectid, const char *name, int name_len,
230 int mod)
231{
232 int ret;
233 struct btrfs_key key;
234 int ins_len = mod < 0 ? -1 : 0;
235 int cow = mod != 0;
236
237 key.objectid = dir;
Chris Mason7e381802007-04-19 15:36:27 -0400238 btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
239 key.offset = objectid;
240
241 ret = btrfs_search_slot(trans, root, &key, path, ins_len, cow);
242 if (ret < 0)
243 return ERR_PTR(ret);
244 if (ret > 0)
245 return ERR_PTR(-ENOENT);
246 return btrfs_match_dir_item_name(root, path, name, name_len);
247}
248
Josef Bacik5103e942007-11-16 11:45:54 -0500249struct btrfs_dir_item *btrfs_lookup_xattr(struct btrfs_trans_handle *trans,
250 struct btrfs_root *root,
251 struct btrfs_path *path, u64 dir,
252 const char *name, u16 name_len,
253 int mod)
254{
255 int ret;
256 struct btrfs_key key;
257 int ins_len = mod < 0 ? -1 : 0;
258 int cow = mod != 0;
259 struct btrfs_key found_key;
260 struct extent_buffer *leaf;
261
262 key.objectid = dir;
263 btrfs_set_key_type(&key, BTRFS_XATTR_ITEM_KEY);
264 ret = btrfs_name_hash(name, name_len, &key.offset);
265 BUG_ON(ret);
266 ret = btrfs_search_slot(trans, root, &key, path, ins_len, cow);
267 if (ret < 0)
268 return ERR_PTR(ret);
269 if (ret > 0) {
270 if (path->slots[0] == 0)
271 return NULL;
272 path->slots[0]--;
273 }
274
275 leaf = path->nodes[0];
276 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
277
278 if (found_key.objectid != dir ||
279 btrfs_key_type(&found_key) != BTRFS_XATTR_ITEM_KEY ||
280 found_key.offset != key.offset)
281 return NULL;
282
283 return btrfs_match_dir_item_name(root, path, name, name_len);
284}
285
Chris Mason7e381802007-04-19 15:36:27 -0400286struct btrfs_dir_item *btrfs_match_dir_item_name(struct btrfs_root *root,
Chris Mason7f5c1512007-03-23 15:56:19 -0400287 struct btrfs_path *path,
288 const char *name, int name_len)
Chris Mason62e27492007-03-15 12:56:47 -0400289{
Chris Mason62e27492007-03-15 12:56:47 -0400290 struct btrfs_dir_item *dir_item;
Chris Mason5f39d392007-10-15 16:14:19 -0400291 unsigned long name_ptr;
Chris Mason7e381802007-04-19 15:36:27 -0400292 u32 total_len;
293 u32 cur = 0;
294 u32 this_len;
Chris Mason5f39d392007-10-15 16:14:19 -0400295 struct extent_buffer *leaf;
Chris Masona8a2ee02007-03-16 08:46:49 -0400296
Chris Mason5f39d392007-10-15 16:14:19 -0400297 leaf = path->nodes[0];
Chris Mason7e381802007-04-19 15:36:27 -0400298 dir_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dir_item);
Chris Mason5f39d392007-10-15 16:14:19 -0400299 total_len = btrfs_item_size_nr(leaf, path->slots[0]);
Chris Mason7e381802007-04-19 15:36:27 -0400300 while(cur < total_len) {
Chris Mason5f39d392007-10-15 16:14:19 -0400301 this_len = sizeof(*dir_item) +
Josef Bacik5103e942007-11-16 11:45:54 -0500302 btrfs_dir_name_len(leaf, dir_item) +
303 btrfs_dir_data_len(leaf, dir_item);
Chris Mason5f39d392007-10-15 16:14:19 -0400304 name_ptr = (unsigned long)(dir_item + 1);
Chris Mason7e381802007-04-19 15:36:27 -0400305
Chris Mason5f39d392007-10-15 16:14:19 -0400306 if (btrfs_dir_name_len(leaf, dir_item) == name_len &&
307 memcmp_extent_buffer(leaf, name, name_ptr, name_len) == 0)
Chris Mason7e381802007-04-19 15:36:27 -0400308 return dir_item;
309
310 cur += this_len;
311 dir_item = (struct btrfs_dir_item *)((char *)dir_item +
312 this_len);
313 }
314 return NULL;
Chris Mason62e27492007-03-15 12:56:47 -0400315}
Chris Mason7e381802007-04-19 15:36:27 -0400316
317int btrfs_delete_one_dir_name(struct btrfs_trans_handle *trans,
318 struct btrfs_root *root,
319 struct btrfs_path *path,
320 struct btrfs_dir_item *di)
321{
322
Chris Mason5f39d392007-10-15 16:14:19 -0400323 struct extent_buffer *leaf;
Chris Mason7e381802007-04-19 15:36:27 -0400324 u32 sub_item_len;
325 u32 item_len;
Chris Mason54aa1f42007-06-22 14:16:25 -0400326 int ret = 0;
Chris Mason7e381802007-04-19 15:36:27 -0400327
Chris Mason5f39d392007-10-15 16:14:19 -0400328 leaf = path->nodes[0];
Josef Bacik5103e942007-11-16 11:45:54 -0500329 sub_item_len = sizeof(*di) + btrfs_dir_name_len(leaf, di) +
330 btrfs_dir_data_len(leaf, di);
Chris Mason5f39d392007-10-15 16:14:19 -0400331 item_len = btrfs_item_size_nr(leaf, path->slots[0]);
332 if (sub_item_len == item_len) {
Chris Mason7e381802007-04-19 15:36:27 -0400333 ret = btrfs_del_item(trans, root, path);
Chris Mason7e381802007-04-19 15:36:27 -0400334 } else {
Chris Mason5f39d392007-10-15 16:14:19 -0400335 /* MARKER */
336 unsigned long ptr = (unsigned long)di;
337 unsigned long start;
338
339 start = btrfs_item_ptr_offset(leaf, path->slots[0]);
340 memmove_extent_buffer(leaf, ptr, ptr + sub_item_len,
Chris Mason7e381802007-04-19 15:36:27 -0400341 item_len - (ptr + sub_item_len - start));
342 ret = btrfs_truncate_item(trans, root, path,
Chris Mason179e29e2007-11-01 11:28:41 -0400343 item_len - sub_item_len, 1);
Chris Mason7e381802007-04-19 15:36:27 -0400344 }
345 return 0;
346}
347