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 <linux/limits.h>
23 #include "transaction.h"
25 static struct btrfs_dir_item
*btrfs_match_dir_item_name(struct btrfs_root
*root
,
26 struct btrfs_path
*path
,
27 const char *name
, int name_len
);
29 static struct btrfs_dir_item
*insert_with_overflow(struct btrfs_trans_handle
31 struct btrfs_root
*root
,
32 struct btrfs_path
*path
,
33 struct btrfs_key
*cpu_key
,
40 struct btrfs_item
*item
;
41 struct extent_buffer
*leaf
;
43 ret
= btrfs_insert_empty_item(trans
, root
, path
, cpu_key
, data_size
);
45 struct btrfs_dir_item
*di
;
46 di
= btrfs_match_dir_item_name(root
, path
, name
, name_len
);
48 return ERR_PTR(-EEXIST
);
49 ret
= btrfs_extend_item(root
, path
, data_size
);
55 leaf
= path
->nodes
[0];
56 item
= btrfs_item_nr(path
->slots
[0]);
57 ptr
= btrfs_item_ptr(leaf
, path
->slots
[0], char);
58 BUG_ON(data_size
> btrfs_item_size(leaf
, item
));
59 ptr
+= btrfs_item_size(leaf
, item
) - data_size
;
60 return (struct btrfs_dir_item
*)ptr
;
63 int btrfs_insert_xattr_item(struct btrfs_trans_handle
*trans
,
64 struct btrfs_root
*root
, const char *name
,
65 u16 name_len
, const void *data
, u16 data_len
,
69 struct btrfs_path
*path
;
70 struct btrfs_dir_item
*dir_item
;
71 unsigned long name_ptr
, data_ptr
;
72 struct btrfs_key key
, location
;
73 struct btrfs_disk_key disk_key
;
74 struct extent_buffer
*leaf
;
78 key
.type
= BTRFS_XATTR_ITEM_KEY
;
79 key
.offset
= btrfs_name_hash(name
, name_len
);
80 path
= btrfs_alloc_path();
84 data_size
= sizeof(*dir_item
) + name_len
+ data_len
;
85 dir_item
= insert_with_overflow(trans
, root
, path
, &key
, data_size
,
88 * FIXME: at some point we should handle xattr's that are larger than
89 * what we can fit in our leaf. We set location to NULL b/c we aren't
90 * pointing at anything else, that will change if we store the xattr
91 * data in a separate inode.
93 BUG_ON(IS_ERR(dir_item
));
94 memset(&location
, 0, sizeof(location
));
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
);
101 btrfs_set_dir_data_len(leaf
, dir_item
, data_len
);
102 name_ptr
= (unsigned long)(dir_item
+ 1);
103 data_ptr
= (unsigned long)((char *)name_ptr
+ name_len
);
105 write_extent_buffer(leaf
, name
, name_ptr
, name_len
);
106 write_extent_buffer(leaf
, data
, data_ptr
, data_len
);
107 btrfs_mark_buffer_dirty(path
->nodes
[0]);
109 btrfs_free_path(path
);
113 int btrfs_insert_dir_item(struct btrfs_trans_handle
*trans
, struct btrfs_root
114 *root
, const char *name
, int name_len
, u64 dir
,
115 struct btrfs_key
*location
, u8 type
, u64 index
)
119 struct btrfs_path
*path
;
120 struct btrfs_dir_item
*dir_item
;
121 struct extent_buffer
*leaf
;
122 unsigned long name_ptr
;
123 struct btrfs_key key
;
124 struct btrfs_disk_key disk_key
;
128 key
.type
= BTRFS_DIR_ITEM_KEY
;
129 key
.offset
= btrfs_name_hash(name
, name_len
);
130 path
= btrfs_alloc_path();
133 data_size
= sizeof(*dir_item
) + name_len
;
134 dir_item
= insert_with_overflow(trans
, root
, path
, &key
, data_size
,
136 if (IS_ERR(dir_item
)) {
137 ret
= PTR_ERR(dir_item
);
139 /* Continue to insert item if existed */
140 if (ret
== -EEXIST
) {
148 leaf
= path
->nodes
[0];
149 btrfs_cpu_key_to_disk(&disk_key
, location
);
150 btrfs_set_dir_item_key(leaf
, dir_item
, &disk_key
);
151 btrfs_set_dir_type(leaf
, dir_item
, type
);
152 btrfs_set_dir_data_len(leaf
, dir_item
, 0);
153 btrfs_set_dir_name_len(leaf
, dir_item
, name_len
);
154 name_ptr
= (unsigned long)(dir_item
+ 1);
156 write_extent_buffer(leaf
, name
, name_ptr
, name_len
);
157 btrfs_mark_buffer_dirty(leaf
);
160 /* FIXME, use some real flag for selecting the extra index */
161 if (root
== root
->fs_info
->tree_root
) {
165 btrfs_release_path(path
);
167 key
.type
= BTRFS_DIR_INDEX_KEY
;
169 dir_item
= insert_with_overflow(trans
, root
, path
, &key
, data_size
,
171 if (IS_ERR(dir_item
)) {
172 ret2
= PTR_ERR(dir_item
);
177 leaf
= path
->nodes
[0];
178 btrfs_cpu_key_to_disk(&disk_key
, location
);
179 btrfs_set_dir_item_key(leaf
, dir_item
, &disk_key
);
180 btrfs_set_dir_type(leaf
, dir_item
, type
);
181 btrfs_set_dir_data_len(leaf
, dir_item
, 0);
182 btrfs_set_dir_name_len(leaf
, dir_item
, name_len
);
183 name_ptr
= (unsigned long)(dir_item
+ 1);
184 write_extent_buffer(leaf
, name
, name_ptr
, name_len
);
185 btrfs_mark_buffer_dirty(leaf
);
187 btrfs_free_path(path
);
195 struct btrfs_dir_item
*btrfs_lookup_dir_item(struct btrfs_trans_handle
*trans
,
196 struct btrfs_root
*root
,
197 struct btrfs_path
*path
, u64 dir
,
198 const char *name
, int name_len
,
202 struct btrfs_key key
;
203 int ins_len
= mod
< 0 ? -1 : 0;
205 struct btrfs_key found_key
;
206 struct extent_buffer
*leaf
;
209 key
.type
= BTRFS_DIR_ITEM_KEY
;
211 key
.offset
= btrfs_name_hash(name
, name_len
);
213 ret
= btrfs_search_slot(trans
, root
, &key
, path
, ins_len
, cow
);
217 if (path
->slots
[0] == 0)
222 leaf
= path
->nodes
[0];
223 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
225 if (found_key
.objectid
!= dir
||
226 found_key
.type
!= BTRFS_DIR_ITEM_KEY
||
227 found_key
.offset
!= key
.offset
)
230 return btrfs_match_dir_item_name(root
, path
, name
, name_len
);
233 struct btrfs_dir_item
*btrfs_lookup_dir_index(struct btrfs_trans_handle
*trans
,
234 struct btrfs_root
*root
,
235 struct btrfs_path
*path
, u64 dir
,
236 const char *name
, int name_len
,
240 struct btrfs_key key
;
241 int ins_len
= mod
< 0 ? -1 : 0;
245 key
.type
= BTRFS_DIR_INDEX_KEY
;
248 ret
= btrfs_search_slot(trans
, root
, &key
, path
, ins_len
, cow
);
252 return ERR_PTR(-ENOENT
);
254 return btrfs_match_dir_item_name(root
, path
, name
, name_len
);
258 * given a pointer into a directory item, delete it. This
259 * handles items that have more than one entry in them.
261 int btrfs_delete_one_dir_name(struct btrfs_trans_handle
*trans
,
262 struct btrfs_root
*root
,
263 struct btrfs_path
*path
,
264 struct btrfs_dir_item
*di
)
267 struct extent_buffer
*leaf
;
272 leaf
= path
->nodes
[0];
273 sub_item_len
= sizeof(*di
) + btrfs_dir_name_len(leaf
, di
) +
274 btrfs_dir_data_len(leaf
, di
);
275 item_len
= btrfs_item_size_nr(leaf
, path
->slots
[0]);
276 if (sub_item_len
== item_len
) {
277 ret
= btrfs_del_item(trans
, root
, path
);
279 unsigned long ptr
= (unsigned long)di
;
282 start
= btrfs_item_ptr_offset(leaf
, path
->slots
[0]);
283 memmove_extent_buffer(leaf
, ptr
, ptr
+ sub_item_len
,
284 item_len
- (ptr
+ sub_item_len
- start
));
285 btrfs_truncate_item(root
, path
, item_len
- sub_item_len
, 1);
290 static int verify_dir_item(struct btrfs_root
*root
,
291 struct extent_buffer
*leaf
,
292 struct btrfs_dir_item
*dir_item
)
294 u16 namelen
= BTRFS_NAME_LEN
;
295 u8 type
= btrfs_dir_type(leaf
, dir_item
);
297 if (type
>= BTRFS_FT_MAX
) {
298 fprintf(stderr
, "invalid dir item type: %d\n",
303 if (type
== BTRFS_FT_XATTR
)
304 namelen
= XATTR_NAME_MAX
;
306 if (btrfs_dir_name_len(leaf
, dir_item
) > namelen
) {
307 fprintf(stderr
, "invalid dir item name len: %u\n",
308 (unsigned)btrfs_dir_data_len(leaf
, dir_item
));
312 /* BTRFS_MAX_XATTR_SIZE is the same for all dir items */
313 if ((btrfs_dir_data_len(leaf
, dir_item
) +
314 btrfs_dir_name_len(leaf
, dir_item
)) >
315 BTRFS_MAX_XATTR_SIZE(root
->fs_info
)) {
316 fprintf(stderr
, "invalid dir item name + data len: %u + %u\n",
317 (unsigned)btrfs_dir_name_len(leaf
, dir_item
),
318 (unsigned)btrfs_dir_data_len(leaf
, dir_item
));
325 static struct btrfs_dir_item
*btrfs_match_dir_item_name(struct btrfs_root
*root
,
326 struct btrfs_path
*path
,
327 const char *name
, int name_len
)
329 struct btrfs_dir_item
*dir_item
;
330 unsigned long name_ptr
;
334 struct extent_buffer
*leaf
;
336 leaf
= path
->nodes
[0];
337 dir_item
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_dir_item
);
338 total_len
= btrfs_item_size_nr(leaf
, path
->slots
[0]);
339 if (verify_dir_item(root
, leaf
, dir_item
))
342 while(cur
< total_len
) {
343 this_len
= sizeof(*dir_item
) +
344 btrfs_dir_name_len(leaf
, dir_item
) +
345 btrfs_dir_data_len(leaf
, dir_item
);
346 if (this_len
> (total_len
- cur
)) {
347 fprintf(stderr
, "invalid dir item size\n");
351 name_ptr
= (unsigned long)(dir_item
+ 1);
353 if (btrfs_dir_name_len(leaf
, dir_item
) == name_len
&&
354 memcmp_extent_buffer(leaf
, name
, name_ptr
, name_len
) == 0)
358 dir_item
= (struct btrfs_dir_item
*)((char *)dir_item
+