btrfs: drop fs_info parameter from tree_mod_log_set_node_key
[linux-2.6-block.git] / fs / btrfs / dir-item.c
CommitLineData
6cbd5570
CM
1/*
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
62e2749e
CM
19#include "ctree.h"
20#include "disk-io.h"
e089f05c 21#include "transaction.h"
62e2749e 22
d352ac68
CM
23/*
24 * insert a name into a directory, doing overflow properly if there is a hash
25 * collision. data_size indicates how big the item inserted should be. On
26 * success a struct btrfs_dir_item pointer is returned, otherwise it is
27 * an ERR_PTR.
28 *
29 * The name is not copied into the dir item, you have to do that yourself.
30 */
35b7e476
CM
31static struct btrfs_dir_item *insert_with_overflow(struct btrfs_trans_handle
32 *trans,
33 struct btrfs_root *root,
34 struct btrfs_path *path,
35 struct btrfs_key *cpu_key,
e06afa83
CM
36 u32 data_size,
37 const char *name,
38 int name_len)
7fcde0e3 39{
2ff7e61e 40 struct btrfs_fs_info *fs_info = root->fs_info;
7fcde0e3 41 int ret;
7e38180e
CM
42 char *ptr;
43 struct btrfs_item *item;
5f39d397 44 struct extent_buffer *leaf;
7fcde0e3
CM
45
46 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
7e38180e 47 if (ret == -EEXIST) {
e06afa83 48 struct btrfs_dir_item *di;
2ff7e61e 49 di = btrfs_match_dir_item_name(fs_info, path, name, name_len);
e06afa83
CM
50 if (di)
51 return ERR_PTR(-EEXIST);
2ff7e61e 52 btrfs_extend_item(fs_info, path, data_size);
143bede5 53 } else if (ret < 0)
54aa1f4d 54 return ERR_PTR(ret);
7e38180e 55 WARN_ON(ret > 0);
5f39d397 56 leaf = path->nodes[0];
dd3cc16b 57 item = btrfs_item_nr(path->slots[0]);
7e38180e 58 ptr = btrfs_item_ptr(leaf, path->slots[0], char);
5f39d397
CM
59 BUG_ON(data_size > btrfs_item_size(leaf, item));
60 ptr += btrfs_item_size(leaf, item) - data_size;
7e38180e 61 return (struct btrfs_dir_item *)ptr;
7fcde0e3
CM
62}
63
d352ac68
CM
64/*
65 * xattrs work a lot like directories, this inserts an xattr item
66 * into the tree
67 */
5103e947 68int btrfs_insert_xattr_item(struct btrfs_trans_handle *trans,
f34f57a3
YZ
69 struct btrfs_root *root,
70 struct btrfs_path *path, u64 objectid,
71 const char *name, u16 name_len,
72 const void *data, u16 data_len)
5103e947
JB
73{
74 int ret = 0;
5103e947
JB
75 struct btrfs_dir_item *dir_item;
76 unsigned long name_ptr, data_ptr;
77 struct btrfs_key key, location;
78 struct btrfs_disk_key disk_key;
79 struct extent_buffer *leaf;
80 u32 data_size;
81
b9d04c60
DS
82 if (name_len + data_len > BTRFS_MAX_XATTR_SIZE(root->fs_info))
83 return -ENOSPC;
f34f57a3
YZ
84
85 key.objectid = objectid;
962a298f 86 key.type = BTRFS_XATTR_ITEM_KEY;
df68b8a7 87 key.offset = btrfs_name_hash(name, name_len);
5103e947
JB
88
89 data_size = sizeof(*dir_item) + name_len + data_len;
90 dir_item = insert_with_overflow(trans, root, path, &key, data_size,
91 name, name_len);
fa09200b
JB
92 if (IS_ERR(dir_item))
93 return PTR_ERR(dir_item);
5103e947
JB
94 memset(&location, 0, sizeof(location));
95
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);
e02119d5 101 btrfs_set_dir_transid(leaf, dir_item, trans->transid);
5103e947
JB
102 btrfs_set_dir_data_len(leaf, dir_item, data_len);
103 name_ptr = (unsigned long)(dir_item + 1);
104 data_ptr = (unsigned long)((char *)name_ptr + name_len);
105
106 write_extent_buffer(leaf, name, name_ptr, name_len);
107 write_extent_buffer(leaf, data, data_ptr, data_len);
108 btrfs_mark_buffer_dirty(path->nodes[0]);
109
5103e947
JB
110 return ret;
111}
112
d352ac68
CM
113/*
114 * insert a directory item in the tree, doing all the magic for
115 * both indexes. 'dir' indicates which objectid to insert it into,
116 * 'location' is the key to stuff into the directory item, 'type' is the
117 * type of the inode we're pointing to, and 'index' is the sequence number
118 * to use for the second index (if one is created).
79787eaa 119 * Will return 0 or -ENOMEM
d352ac68 120 */
e089f05c 121int btrfs_insert_dir_item(struct btrfs_trans_handle *trans, struct btrfs_root
16cdcec7 122 *root, const char *name, int name_len,
8e7611cf 123 struct btrfs_inode *dir, struct btrfs_key *location,
16cdcec7 124 u8 type, u64 index)
62e2749e
CM
125{
126 int ret = 0;
e06afa83 127 int ret2 = 0;
5caf2a00 128 struct btrfs_path *path;
62e2749e 129 struct btrfs_dir_item *dir_item;
5f39d397
CM
130 struct extent_buffer *leaf;
131 unsigned long name_ptr;
62e2749e 132 struct btrfs_key key;
5f39d397 133 struct btrfs_disk_key disk_key;
62e2749e
CM
134 u32 data_size;
135
8e7611cf 136 key.objectid = btrfs_ino(dir);
962a298f 137 key.type = BTRFS_DIR_ITEM_KEY;
df68b8a7 138 key.offset = btrfs_name_hash(name, name_len);
b9473439 139
5caf2a00 140 path = btrfs_alloc_path();
16cdcec7
MX
141 if (!path)
142 return -ENOMEM;
b9473439
CM
143 path->leave_spinning = 1;
144
16cdcec7
MX
145 btrfs_cpu_key_to_disk(&disk_key, location);
146
62e2749e 147 data_size = sizeof(*dir_item) + name_len;
e06afa83
CM
148 dir_item = insert_with_overflow(trans, root, path, &key, data_size,
149 name, name_len);
7e38180e
CM
150 if (IS_ERR(dir_item)) {
151 ret = PTR_ERR(dir_item);
e06afa83
CM
152 if (ret == -EEXIST)
153 goto second_insert;
c2db1073 154 goto out_free;
7e38180e 155 }
62e2749e 156
5f39d397 157 leaf = path->nodes[0];
5f39d397
CM
158 btrfs_set_dir_item_key(leaf, dir_item, &disk_key);
159 btrfs_set_dir_type(leaf, dir_item, type);
5103e947 160 btrfs_set_dir_data_len(leaf, dir_item, 0);
5f39d397 161 btrfs_set_dir_name_len(leaf, dir_item, name_len);
e02119d5 162 btrfs_set_dir_transid(leaf, dir_item, trans->transid);
5f39d397 163 name_ptr = (unsigned long)(dir_item + 1);
c5739bba 164
5f39d397
CM
165 write_extent_buffer(leaf, name, name_ptr, name_len);
166 btrfs_mark_buffer_dirty(leaf);
7e38180e 167
e06afa83 168second_insert:
7e38180e
CM
169 /* FIXME, use some real flag for selecting the extra index */
170 if (root == root->fs_info->tree_root) {
171 ret = 0;
c2db1073 172 goto out_free;
7e38180e 173 }
b3b4aa74 174 btrfs_release_path(path);
7e38180e 175
2ff7e61e 176 ret2 = btrfs_insert_delayed_dir_index(trans, root->fs_info, name,
8e7611cf 177 name_len, dir, &disk_key, type, index);
c2db1073 178out_free:
5caf2a00 179 btrfs_free_path(path);
e06afa83
CM
180 if (ret)
181 return ret;
182 if (ret2)
183 return ret2;
184 return 0;
62e2749e
CM
185}
186
d352ac68
CM
187/*
188 * lookup a directory item based on name. 'dir' is the objectid
189 * we're searching in, and 'mod' tells us if you plan on deleting the
190 * item (use mod < 0) or changing the options (use mod > 0)
191 */
7e38180e
CM
192struct btrfs_dir_item *btrfs_lookup_dir_item(struct btrfs_trans_handle *trans,
193 struct btrfs_root *root,
194 struct btrfs_path *path, u64 dir,
195 const char *name, int name_len,
196 int mod)
62e2749e 197{
1d4f6404 198 int ret;
62e2749e 199 struct btrfs_key key;
1d4f6404
CM
200 int ins_len = mod < 0 ? -1 : 0;
201 int cow = mod != 0;
62e2749e
CM
202
203 key.objectid = dir;
962a298f 204 key.type = BTRFS_DIR_ITEM_KEY;
5f39d397 205
df68b8a7 206 key.offset = btrfs_name_hash(name, name_len);
5f39d397 207
7e38180e
CM
208 ret = btrfs_search_slot(trans, root, &key, path, ins_len, cow);
209 if (ret < 0)
210 return ERR_PTR(ret);
85d85a74 211 if (ret > 0)
7e38180e
CM
212 return NULL;
213
2ff7e61e 214 return btrfs_match_dir_item_name(root->fs_info, path, name, name_len);
62e2749e
CM
215}
216
9c52057c
CM
217int btrfs_check_dir_item_collision(struct btrfs_root *root, u64 dir,
218 const char *name, int name_len)
219{
220 int ret;
221 struct btrfs_key key;
222 struct btrfs_dir_item *di;
223 int data_size;
224 struct extent_buffer *leaf;
225 int slot;
226 struct btrfs_path *path;
227
228
229 path = btrfs_alloc_path();
230 if (!path)
231 return -ENOMEM;
232
233 key.objectid = dir;
962a298f 234 key.type = BTRFS_DIR_ITEM_KEY;
9c52057c
CM
235 key.offset = btrfs_name_hash(name, name_len);
236
237 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
238
239 /* return back any errors */
240 if (ret < 0)
241 goto out;
242
243 /* nothing found, we're safe */
244 if (ret > 0) {
245 ret = 0;
246 goto out;
247 }
248
249 /* we found an item, look for our name in the item */
2ff7e61e 250 di = btrfs_match_dir_item_name(root->fs_info, path, name, name_len);
9c52057c
CM
251 if (di) {
252 /* our exact name was found */
253 ret = -EEXIST;
254 goto out;
255 }
256
257 /*
258 * see if there is room in the item to insert this
259 * name
260 */
878f2d2c 261 data_size = sizeof(*di) + name_len;
9c52057c
CM
262 leaf = path->nodes[0];
263 slot = path->slots[0];
264 if (data_size + btrfs_item_size_nr(leaf, slot) +
da17066c 265 sizeof(struct btrfs_item) > BTRFS_LEAF_DATA_SIZE(root->fs_info)) {
9c52057c
CM
266 ret = -EOVERFLOW;
267 } else {
268 /* plenty of insertion room */
269 ret = 0;
270 }
271out:
272 btrfs_free_path(path);
273 return ret;
274}
275
d352ac68
CM
276/*
277 * lookup a directory item based on index. 'dir' is the objectid
278 * we're searching in, and 'mod' tells us if you plan on deleting the
279 * item (use mod < 0) or changing the options (use mod > 0)
280 *
281 * The name is used to make sure the index really points to the name you were
282 * looking for.
283 */
7e38180e
CM
284struct btrfs_dir_item *
285btrfs_lookup_dir_index_item(struct btrfs_trans_handle *trans,
286 struct btrfs_root *root,
287 struct btrfs_path *path, u64 dir,
288 u64 objectid, const char *name, int name_len,
289 int mod)
290{
291 int ret;
292 struct btrfs_key key;
293 int ins_len = mod < 0 ? -1 : 0;
294 int cow = mod != 0;
295
296 key.objectid = dir;
962a298f 297 key.type = BTRFS_DIR_INDEX_KEY;
7e38180e
CM
298 key.offset = objectid;
299
300 ret = btrfs_search_slot(trans, root, &key, path, ins_len, cow);
301 if (ret < 0)
302 return ERR_PTR(ret);
303 if (ret > 0)
304 return ERR_PTR(-ENOENT);
2ff7e61e 305 return btrfs_match_dir_item_name(root->fs_info, path, name, name_len);
7e38180e
CM
306}
307
4df27c4d
YZ
308struct btrfs_dir_item *
309btrfs_search_dir_index_item(struct btrfs_root *root,
310 struct btrfs_path *path, u64 dirid,
311 const char *name, int name_len)
312{
313 struct extent_buffer *leaf;
314 struct btrfs_dir_item *di;
315 struct btrfs_key key;
316 u32 nritems;
317 int ret;
318
319 key.objectid = dirid;
320 key.type = BTRFS_DIR_INDEX_KEY;
321 key.offset = 0;
322
323 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
324 if (ret < 0)
325 return ERR_PTR(ret);
326
327 leaf = path->nodes[0];
328 nritems = btrfs_header_nritems(leaf);
329
330 while (1) {
331 if (path->slots[0] >= nritems) {
332 ret = btrfs_next_leaf(root, path);
333 if (ret < 0)
334 return ERR_PTR(ret);
335 if (ret > 0)
336 break;
337 leaf = path->nodes[0];
338 nritems = btrfs_header_nritems(leaf);
339 continue;
340 }
341
342 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
343 if (key.objectid != dirid || key.type != BTRFS_DIR_INDEX_KEY)
344 break;
345
2ff7e61e
JM
346 di = btrfs_match_dir_item_name(root->fs_info, path,
347 name, name_len);
4df27c4d
YZ
348 if (di)
349 return di;
350
351 path->slots[0]++;
352 }
353 return NULL;
354}
355
5103e947
JB
356struct btrfs_dir_item *btrfs_lookup_xattr(struct btrfs_trans_handle *trans,
357 struct btrfs_root *root,
358 struct btrfs_path *path, u64 dir,
359 const char *name, u16 name_len,
360 int mod)
361{
362 int ret;
363 struct btrfs_key key;
364 int ins_len = mod < 0 ? -1 : 0;
365 int cow = mod != 0;
5103e947
JB
366
367 key.objectid = dir;
962a298f 368 key.type = BTRFS_XATTR_ITEM_KEY;
df68b8a7 369 key.offset = btrfs_name_hash(name, name_len);
5103e947
JB
370 ret = btrfs_search_slot(trans, root, &key, path, ins_len, cow);
371 if (ret < 0)
372 return ERR_PTR(ret);
85d85a74 373 if (ret > 0)
5103e947
JB
374 return NULL;
375
2ff7e61e 376 return btrfs_match_dir_item_name(root->fs_info, path, name, name_len);
5103e947
JB
377}
378
d352ac68
CM
379/*
380 * helper function to look at the directory item pointed to by 'path'
381 * this walks through all the entries in a dir item and finds one
382 * for a specific name.
383 */
2ff7e61e 384struct btrfs_dir_item *btrfs_match_dir_item_name(struct btrfs_fs_info *fs_info,
5f5bc6b1
FM
385 struct btrfs_path *path,
386 const char *name, int name_len)
62e2749e 387{
62e2749e 388 struct btrfs_dir_item *dir_item;
5f39d397 389 unsigned long name_ptr;
7e38180e
CM
390 u32 total_len;
391 u32 cur = 0;
392 u32 this_len;
5f39d397 393 struct extent_buffer *leaf;
a8a2ee0c 394
5f39d397 395 leaf = path->nodes[0];
7e38180e 396 dir_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dir_item);
22a94d44 397
5f39d397 398 total_len = btrfs_item_size_nr(leaf, path->slots[0]);
d397712b 399 while (cur < total_len) {
5f39d397 400 this_len = sizeof(*dir_item) +
5103e947
JB
401 btrfs_dir_name_len(leaf, dir_item) +
402 btrfs_dir_data_len(leaf, dir_item);
5f39d397 403 name_ptr = (unsigned long)(dir_item + 1);
7e38180e 404
5f39d397
CM
405 if (btrfs_dir_name_len(leaf, dir_item) == name_len &&
406 memcmp_extent_buffer(leaf, name, name_ptr, name_len) == 0)
7e38180e
CM
407 return dir_item;
408
409 cur += this_len;
410 dir_item = (struct btrfs_dir_item *)((char *)dir_item +
411 this_len);
412 }
413 return NULL;
62e2749e 414}
7e38180e 415
d352ac68
CM
416/*
417 * given a pointer into a directory item, delete it. This
418 * handles items that have more than one entry in them.
419 */
7e38180e
CM
420int btrfs_delete_one_dir_name(struct btrfs_trans_handle *trans,
421 struct btrfs_root *root,
422 struct btrfs_path *path,
423 struct btrfs_dir_item *di)
424{
425
5f39d397 426 struct extent_buffer *leaf;
7e38180e
CM
427 u32 sub_item_len;
428 u32 item_len;
54aa1f4d 429 int ret = 0;
7e38180e 430
5f39d397 431 leaf = path->nodes[0];
5103e947
JB
432 sub_item_len = sizeof(*di) + btrfs_dir_name_len(leaf, di) +
433 btrfs_dir_data_len(leaf, di);
5f39d397
CM
434 item_len = btrfs_item_size_nr(leaf, path->slots[0]);
435 if (sub_item_len == item_len) {
7e38180e 436 ret = btrfs_del_item(trans, root, path);
7e38180e 437 } else {
5f39d397
CM
438 /* MARKER */
439 unsigned long ptr = (unsigned long)di;
440 unsigned long start;
441
442 start = btrfs_item_ptr_offset(leaf, path->slots[0]);
443 memmove_extent_buffer(leaf, ptr, ptr + sub_item_len,
7e38180e 444 item_len - (ptr + sub_item_len - start));
2ff7e61e
JM
445 btrfs_truncate_item(root->fs_info, path,
446 item_len - sub_item_len, 1);
7e38180e 447 }
411fc6bc 448 return ret;
7e38180e 449}