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.
21 #include "transaction.h"
24 static int find_name_in_backref(struct btrfs_path
*path
, const char * name
,
25 int name_len
, struct btrfs_inode_ref
**ref_ret
)
27 struct extent_buffer
*leaf
;
28 struct btrfs_inode_ref
*ref
;
30 unsigned long name_ptr
;
35 leaf
= path
->nodes
[0];
36 item_size
= btrfs_item_size_nr(leaf
, path
->slots
[0]);
37 ptr
= btrfs_item_ptr_offset(leaf
, path
->slots
[0]);
38 while (cur_offset
< item_size
) {
39 ref
= (struct btrfs_inode_ref
*)(ptr
+ cur_offset
);
40 len
= btrfs_inode_ref_name_len(leaf
, ref
);
41 name_ptr
= (unsigned long)(ref
+ 1);
42 cur_offset
+= len
+ sizeof(*ref
);
45 if (memcmp_extent_buffer(leaf
, name
, name_ptr
, name_len
) == 0) {
53 int btrfs_insert_inode_ref(struct btrfs_trans_handle
*trans
,
54 struct btrfs_root
*root
,
55 const char *name
, int name_len
,
56 u64 inode_objectid
, u64 ref_objectid
, u64 index
)
58 struct btrfs_path
*path
;
60 struct btrfs_inode_ref
*ref
;
63 int ins_len
= name_len
+ sizeof(*ref
);
65 key
.objectid
= inode_objectid
;
66 key
.offset
= ref_objectid
;
67 key
.type
= BTRFS_INODE_REF_KEY
;
69 path
= btrfs_alloc_path();
73 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
78 if (find_name_in_backref(path
, name
, name_len
, &ref
))
81 old_size
= btrfs_item_size_nr(path
->nodes
[0], path
->slots
[0]);
82 ret
= btrfs_extend_item(root
, path
, ins_len
);
84 ref
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
85 struct btrfs_inode_ref
);
86 ref
= (struct btrfs_inode_ref
*)((unsigned long)ref
+ old_size
);
87 btrfs_set_inode_ref_name_len(path
->nodes
[0], ref
, name_len
);
88 btrfs_set_inode_ref_index(path
->nodes
[0], ref
, index
);
89 ptr
= (unsigned long)(ref
+ 1);
92 if (ret
== -EOVERFLOW
)
96 ref
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
97 struct btrfs_inode_ref
);
98 btrfs_set_inode_ref_name_len(path
->nodes
[0], ref
, name_len
);
99 btrfs_set_inode_ref_index(path
->nodes
[0], ref
, index
);
100 ptr
= (unsigned long)(ref
+ 1);
102 write_extent_buffer(path
->nodes
[0], name
, ptr
, name_len
);
103 btrfs_mark_buffer_dirty(path
->nodes
[0]);
106 btrfs_free_path(path
);
108 if (ret
== -EMLINK
) {
109 if (btrfs_fs_incompat(root
->fs_info
, EXTENDED_IREF
))
110 ret
= btrfs_insert_inode_extref(trans
, root
, name
,
113 ref_objectid
, index
);
118 int btrfs_lookup_inode(struct btrfs_trans_handle
*trans
, struct btrfs_root
119 *root
, struct btrfs_path
*path
,
120 struct btrfs_key
*location
, int mod
)
122 int ins_len
= mod
< 0 ? -1 : 0;
126 struct extent_buffer
*leaf
;
127 struct btrfs_key found_key
;
129 ret
= btrfs_search_slot(trans
, root
, location
, path
, ins_len
, cow
);
130 if (ret
> 0 && location
->type
== BTRFS_ROOT_ITEM_KEY
&&
131 location
->offset
== (u64
)-1 && path
->slots
[0] != 0) {
132 slot
= path
->slots
[0] - 1;
133 leaf
= path
->nodes
[0];
134 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
135 if (found_key
.objectid
== location
->objectid
&&
136 found_key
.type
== location
->type
) {
144 int btrfs_insert_inode(struct btrfs_trans_handle
*trans
, struct btrfs_root
145 *root
, u64 objectid
, struct btrfs_inode_item
149 struct btrfs_key key
;
151 key
.objectid
= objectid
;
152 key
.type
= BTRFS_INODE_ITEM_KEY
;
155 ret
= btrfs_insert_item(trans
, root
, &key
, inode_item
,
156 sizeof(*inode_item
));
160 struct btrfs_inode_ref
*btrfs_lookup_inode_ref(struct btrfs_trans_handle
*trans
,
161 struct btrfs_root
*root
, struct btrfs_path
*path
,
162 const char *name
, int namelen
, u64 ino
, u64 parent_ino
,
165 struct btrfs_key key
;
166 struct btrfs_inode_ref
*ret_inode_ref
= NULL
;
170 key
.type
= BTRFS_INODE_REF_KEY
;
171 key
.offset
= parent_ino
;
173 ret
= btrfs_search_slot(trans
, root
, &key
, path
, ins_len
,
178 find_name_in_backref(path
, name
, namelen
, &ret_inode_ref
);
183 return ret_inode_ref
;
186 static int btrfs_find_name_in_ext_backref(struct btrfs_path
*path
,
187 u64 parent_ino
, const char *name
, int namelen
,
188 struct btrfs_inode_extref
**extref_ret
)
190 struct extent_buffer
*node
;
191 struct btrfs_inode_extref
*extref
;
193 unsigned long name_ptr
;
199 node
= path
->nodes
[0];
200 slot
= path
->slots
[0];
201 item_size
= btrfs_item_size_nr(node
, slot
);
202 ptr
= btrfs_item_ptr_offset(node
, slot
);
205 * Search all extended backrefs in this item. We're only looking
206 * through any collisions so most of the time this is just going to
207 * compare against one buffer. If all is well, we'll return success and
208 * the inode ref object.
210 while (cur_offset
< item_size
) {
211 extref
= (struct btrfs_inode_extref
*) (ptr
+ cur_offset
);
212 name_ptr
= (unsigned long)(&extref
->name
);
213 ref_name_len
= btrfs_inode_extref_name_len(node
, extref
);
215 if (ref_name_len
== namelen
&&
216 btrfs_inode_extref_parent(node
, extref
) == parent_ino
&&
217 (memcmp_extent_buffer(node
, name
, name_ptr
, namelen
) == 0))
220 *extref_ret
= extref
;
224 cur_offset
+= ref_name_len
+ sizeof(*extref
);
230 struct btrfs_inode_extref
*btrfs_lookup_inode_extref(struct btrfs_trans_handle
231 *trans
, struct btrfs_path
*path
, struct btrfs_root
*root
,
232 u64 ino
, u64 parent_ino
, u64 index
, const char *name
,
233 int namelen
, int ins_len
)
235 struct btrfs_key key
;
236 struct btrfs_inode_extref
*extref
;
240 key
.type
= BTRFS_INODE_EXTREF_KEY
;
241 key
.offset
= btrfs_extref_hash(parent_ino
, name
, namelen
);
243 ret
= btrfs_search_slot(trans
, root
, &key
, path
, ins_len
,
249 if (!btrfs_find_name_in_ext_backref(path
, parent_ino
, name
,
256 int btrfs_del_inode_extref(struct btrfs_trans_handle
*trans
,
257 struct btrfs_root
*root
,
258 const char *name
, int name_len
,
259 u64 inode_objectid
, u64 ref_objectid
,
262 struct btrfs_path
*path
;
263 struct btrfs_key key
;
264 struct btrfs_inode_extref
*extref
;
265 struct extent_buffer
*leaf
;
267 int del_len
= name_len
+ sizeof(*extref
);
269 unsigned long item_start
;
272 key
.objectid
= inode_objectid
;
273 key
.type
= BTRFS_INODE_EXTREF_KEY
;
274 key
.offset
= btrfs_extref_hash(ref_objectid
, name
, name_len
);
276 path
= btrfs_alloc_path();
280 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
287 * Sanity check - did we find the right item for this name? This
288 * should always succeed so error here will make the FS readonly.
290 if (!btrfs_find_name_in_ext_backref(path
, ref_objectid
,
291 name
, name_len
, &extref
)) {
296 leaf
= path
->nodes
[0];
297 item_size
= btrfs_item_size_nr(leaf
, path
->slots
[0]);
299 *index
= btrfs_inode_extref_index(leaf
, extref
);
301 if (del_len
== item_size
) {
303 * Common case only one ref in the item, remove the whole item.
305 ret
= btrfs_del_item(trans
, root
, path
);
309 ptr
= (unsigned long)extref
;
310 item_start
= btrfs_item_ptr_offset(leaf
, path
->slots
[0]);
312 memmove_extent_buffer(leaf
, ptr
, ptr
+ del_len
,
313 item_size
- (ptr
+ del_len
- item_start
));
315 btrfs_truncate_item(root
, path
, item_size
- del_len
, 1);
318 btrfs_free_path(path
);
324 * btrfs_insert_inode_extref() - Inserts an extended inode ref into a tree.
326 * The caller must have checked against BTRFS_LINK_MAX already.
328 int btrfs_insert_inode_extref(struct btrfs_trans_handle
*trans
,
329 struct btrfs_root
*root
,
330 const char *name
, int name_len
,
331 u64 inode_objectid
, u64 ref_objectid
, u64 index
)
333 struct btrfs_inode_extref
*extref
;
335 int ins_len
= name_len
+ sizeof(*extref
);
337 struct btrfs_path
*path
;
338 struct btrfs_key key
;
339 struct extent_buffer
*leaf
;
340 struct btrfs_item
*item
;
342 key
.objectid
= inode_objectid
;
343 key
.type
= BTRFS_INODE_EXTREF_KEY
;
344 key
.offset
= btrfs_extref_hash(ref_objectid
, name
, name_len
);
346 path
= btrfs_alloc_path();
350 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
352 if (ret
== -EEXIST
) {
353 if (btrfs_find_name_in_ext_backref(path
, ref_objectid
,
354 name
, name_len
, NULL
))
357 btrfs_extend_item(root
, path
, ins_len
);
364 leaf
= path
->nodes
[0];
365 item
= btrfs_item_nr(path
->slots
[0]);
366 ptr
= (unsigned long)btrfs_item_ptr(leaf
, path
->slots
[0], char);
367 ptr
+= btrfs_item_size(leaf
, item
) - ins_len
;
368 extref
= (struct btrfs_inode_extref
*)ptr
;
370 btrfs_set_inode_extref_name_len(path
->nodes
[0], extref
, name_len
);
371 btrfs_set_inode_extref_index(path
->nodes
[0], extref
, index
);
372 btrfs_set_inode_extref_parent(path
->nodes
[0], extref
, ref_objectid
);
374 ptr
= (unsigned long)&extref
->name
;
375 write_extent_buffer(path
->nodes
[0], name
, ptr
, name_len
);
376 btrfs_mark_buffer_dirty(path
->nodes
[0]);
379 btrfs_free_path(path
);
384 int btrfs_del_inode_ref(struct btrfs_trans_handle
*trans
,
385 struct btrfs_root
*root
, const char *name
, int name_len
,
386 u64 ino
, u64 parent_ino
, u64
*index
)
388 struct btrfs_path
*path
;
389 struct btrfs_key key
;
390 struct btrfs_inode_ref
*ref
;
391 struct extent_buffer
*leaf
;
393 unsigned long item_start
;
397 int search_ext_refs
= 0;
398 int del_len
= name_len
+ sizeof(*ref
);
401 key
.offset
= parent_ino
;
402 key
.type
= BTRFS_INODE_REF_KEY
;
404 path
= btrfs_alloc_path();
408 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
413 } else if (ret
< 0) {
416 if (!find_name_in_backref(path
, name
, name_len
, &ref
)) {
421 leaf
= path
->nodes
[0];
422 item_size
= btrfs_item_size_nr(leaf
, path
->slots
[0]);
425 *index
= btrfs_inode_ref_index(leaf
, ref
);
427 if (del_len
== item_size
) {
428 ret
= btrfs_del_item(trans
, root
, path
);
431 ptr
= (unsigned long)ref
;
432 sub_item_len
= name_len
+ sizeof(*ref
);
433 item_start
= btrfs_item_ptr_offset(leaf
, path
->slots
[0]);
434 memmove_extent_buffer(leaf
, ptr
, ptr
+ sub_item_len
,
435 item_size
- (ptr
+ sub_item_len
- item_start
));
436 btrfs_truncate_item(root
, path
, item_size
- sub_item_len
, 1);
437 btrfs_mark_buffer_dirty(path
->nodes
[0]);
439 btrfs_free_path(path
);
441 if (search_ext_refs
&&
442 btrfs_fs_incompat(root
->fs_info
, EXTENDED_IREF
)) {
444 * No refs were found, or we could not find the name in our ref
445 * array. Find and remove the extended inode ref then.
447 return btrfs_del_inode_extref(trans
, root
, name
, name_len
,
448 ino
, parent_ino
, index
);