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.
22 #include "transaction.h"
24 static struct btrfs_dir_item *btrfs_match_dir_item_name(struct btrfs_root *root,
25 struct btrfs_path *path,
26 const char *name, int name_len);
28 static struct btrfs_dir_item *insert_with_overflow(struct btrfs_trans_handle
30 struct btrfs_root *root,
31 struct btrfs_path *path,
32 struct btrfs_key *cpu_key,
39 struct btrfs_item *item;
40 struct extent_buffer *leaf;
42 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
44 struct btrfs_dir_item *di;
45 di = btrfs_match_dir_item_name(root, path, name, name_len);
47 return ERR_PTR(-EEXIST);
48 ret = btrfs_extend_item(trans, root, path, data_size);
54 leaf = path->nodes[0];
55 item = btrfs_item_nr(path->slots[0]);
56 ptr = btrfs_item_ptr(leaf, path->slots[0], char);
57 BUG_ON(data_size > btrfs_item_size(leaf, item));
58 ptr += btrfs_item_size(leaf, item) - data_size;
59 return (struct btrfs_dir_item *)ptr;
62 int btrfs_insert_xattr_item(struct btrfs_trans_handle *trans,
63 struct btrfs_root *root, const char *name,
64 u16 name_len, const void *data, u16 data_len,
68 struct btrfs_path *path;
69 struct btrfs_dir_item *dir_item;
70 unsigned long name_ptr, data_ptr;
71 struct btrfs_key key, location;
72 struct btrfs_disk_key disk_key;
73 struct extent_buffer *leaf;
77 btrfs_set_key_type(&key, BTRFS_XATTR_ITEM_KEY);
78 key.offset = btrfs_name_hash(name, name_len);
79 path = btrfs_alloc_path();
83 data_size = sizeof(*dir_item) + name_len + data_len;
84 dir_item = insert_with_overflow(trans, root, path, &key, data_size,
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.
92 BUG_ON(IS_ERR(dir_item));
93 memset(&location, 0, sizeof(location));
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);
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]);
108 btrfs_free_path(path);
112 int btrfs_insert_dir_item(struct btrfs_trans_handle *trans, struct btrfs_root
113 *root, const char *name, int name_len, u64 dir,
114 struct btrfs_key *location, u8 type, u64 index)
118 struct btrfs_path *path;
119 struct btrfs_dir_item *dir_item;
120 struct extent_buffer *leaf;
121 unsigned long name_ptr;
122 struct btrfs_key key;
123 struct btrfs_disk_key disk_key;
127 btrfs_set_key_type(&key, BTRFS_DIR_ITEM_KEY);
128 key.offset = btrfs_name_hash(name, name_len);
129 path = btrfs_alloc_path();
132 data_size = sizeof(*dir_item) + name_len;
133 dir_item = insert_with_overflow(trans, root, path, &key, data_size,
135 if (IS_ERR(dir_item)) {
136 ret = PTR_ERR(dir_item);
140 leaf = path->nodes[0];
141 btrfs_cpu_key_to_disk(&disk_key, location);
142 btrfs_set_dir_item_key(leaf, dir_item, &disk_key);
143 btrfs_set_dir_type(leaf, dir_item, type);
144 btrfs_set_dir_data_len(leaf, dir_item, 0);
145 btrfs_set_dir_name_len(leaf, dir_item, name_len);
146 name_ptr = (unsigned long)(dir_item + 1);
148 write_extent_buffer(leaf, name, name_ptr, name_len);
149 btrfs_mark_buffer_dirty(leaf);
151 /* FIXME, use some real flag for selecting the extra index */
152 if (root == root->fs_info->tree_root) {
156 btrfs_release_path(path);
158 btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
160 dir_item = insert_with_overflow(trans, root, path, &key, data_size,
162 if (IS_ERR(dir_item)) {
163 ret2 = PTR_ERR(dir_item);
166 leaf = path->nodes[0];
167 btrfs_cpu_key_to_disk(&disk_key, location);
168 btrfs_set_dir_item_key(leaf, dir_item, &disk_key);
169 btrfs_set_dir_type(leaf, dir_item, type);
170 btrfs_set_dir_data_len(leaf, dir_item, 0);
171 btrfs_set_dir_name_len(leaf, dir_item, name_len);
172 name_ptr = (unsigned long)(dir_item + 1);
173 write_extent_buffer(leaf, name, name_ptr, name_len);
174 btrfs_mark_buffer_dirty(leaf);
176 btrfs_free_path(path);
184 struct btrfs_dir_item *btrfs_lookup_dir_item(struct btrfs_trans_handle *trans,
185 struct btrfs_root *root,
186 struct btrfs_path *path, u64 dir,
187 const char *name, int name_len,
191 struct btrfs_key key;
192 int ins_len = mod < 0 ? -1 : 0;
194 struct btrfs_key found_key;
195 struct extent_buffer *leaf;
198 btrfs_set_key_type(&key, BTRFS_DIR_ITEM_KEY);
200 key.offset = btrfs_name_hash(name, name_len);
202 ret = btrfs_search_slot(trans, root, &key, path, ins_len, cow);
206 if (path->slots[0] == 0)
211 leaf = path->nodes[0];
212 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
214 if (found_key.objectid != dir ||
215 btrfs_key_type(&found_key) != BTRFS_DIR_ITEM_KEY ||
216 found_key.offset != key.offset)
219 return btrfs_match_dir_item_name(root, path, name, name_len);
222 static struct btrfs_dir_item *btrfs_match_dir_item_name(struct btrfs_root *root,
223 struct btrfs_path *path,
224 const char *name, int name_len)
226 struct btrfs_dir_item *dir_item;
227 unsigned long name_ptr;
231 struct extent_buffer *leaf;
233 leaf = path->nodes[0];
234 dir_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dir_item);
235 total_len = btrfs_item_size_nr(leaf, path->slots[0]);
236 while(cur < total_len) {
237 this_len = sizeof(*dir_item) +
238 btrfs_dir_name_len(leaf, dir_item) +
239 btrfs_dir_data_len(leaf, dir_item);
240 name_ptr = (unsigned long)(dir_item + 1);
242 if (btrfs_dir_name_len(leaf, dir_item) == name_len &&
243 memcmp_extent_buffer(leaf, name, name_ptr, name_len) == 0)
247 dir_item = (struct btrfs_dir_item *)((char *)dir_item +