btrfs-progs: docs: add note about mount option applicability
[btrfs-progs-unstable/devel.git] / dir-item.c
blob462546c0eaf47cdc2823a2b6ffd40690815c7f11
1 /*
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>
20 #include "ctree.h"
21 #include "disk-io.h"
22 #include "hash.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
30 *trans,
31 struct btrfs_root *root,
32 struct btrfs_path *path,
33 struct btrfs_key *cpu_key,
34 u32 data_size,
35 const char *name,
36 int name_len)
38 int ret;
39 char *ptr;
40 struct btrfs_item *item;
41 struct extent_buffer *leaf;
43 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
44 if (ret == -EEXIST) {
45 struct btrfs_dir_item *di;
46 di = btrfs_match_dir_item_name(root, path, name, name_len);
47 if (di)
48 return ERR_PTR(-EEXIST);
49 ret = btrfs_extend_item(root, path, data_size);
50 WARN_ON(ret > 0);
52 if (ret < 0)
53 return ERR_PTR(ret);
54 WARN_ON(ret > 0);
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,
66 u64 dir)
68 int ret = 0;
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;
75 u32 data_size;
77 key.objectid = dir;
78 key.type = BTRFS_XATTR_ITEM_KEY;
79 key.offset = btrfs_name_hash(name, name_len);
80 path = btrfs_alloc_path();
81 if (!path)
82 return -ENOMEM;
84 data_size = sizeof(*dir_item) + name_len + data_len;
85 dir_item = insert_with_overflow(trans, root, path, &key, data_size,
86 name, name_len);
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);
110 return ret;
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)
117 int ret = 0;
118 int ret2 = 0;
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;
125 u32 data_size;
127 key.objectid = dir;
128 key.type = BTRFS_DIR_ITEM_KEY;
129 key.offset = btrfs_name_hash(name, name_len);
130 path = btrfs_alloc_path();
131 if (!path)
132 return -ENOMEM;
133 data_size = sizeof(*dir_item) + name_len;
134 dir_item = insert_with_overflow(trans, root, path, &key, data_size,
135 name, name_len);
136 if (IS_ERR(dir_item)) {
137 ret = PTR_ERR(dir_item);
139 /* Continue to insert item if existed */
140 if (ret == -EEXIST) {
141 ret = 0;
142 goto insert;
143 } else {
144 goto out;
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);
159 insert:
160 /* FIXME, use some real flag for selecting the extra index */
161 if (root == root->fs_info->tree_root) {
162 ret = 0;
163 goto out;
165 btrfs_release_path(path);
167 key.type = BTRFS_DIR_INDEX_KEY;
168 key.offset = index;
169 dir_item = insert_with_overflow(trans, root, path, &key, data_size,
170 name, name_len);
171 if (IS_ERR(dir_item)) {
172 ret2 = PTR_ERR(dir_item);
173 if (ret2 == -EEXIST)
174 ret = 0;
175 goto out;
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);
186 out:
187 btrfs_free_path(path);
188 if (ret)
189 return ret;
190 if (ret2)
191 return ret2;
192 return 0;
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,
199 int mod)
201 int ret;
202 struct btrfs_key key;
203 int ins_len = mod < 0 ? -1 : 0;
204 int cow = mod != 0;
205 struct btrfs_key found_key;
206 struct extent_buffer *leaf;
208 key.objectid = dir;
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);
214 if (ret < 0)
215 return ERR_PTR(ret);
216 if (ret > 0) {
217 if (path->slots[0] == 0)
218 return NULL;
219 path->slots[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)
228 return NULL;
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,
237 u64 index, int mod)
239 int ret;
240 struct btrfs_key key;
241 int ins_len = mod < 0 ? -1 : 0;
242 int cow = mod != 0;
244 key.objectid = dir;
245 key.type = BTRFS_DIR_INDEX_KEY;
246 key.offset = index;
248 ret = btrfs_search_slot(trans, root, &key, path, ins_len, cow);
249 if (ret < 0)
250 return ERR_PTR(ret);
251 if (ret > 0)
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;
268 u32 sub_item_len;
269 u32 item_len;
270 int ret = 0;
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);
278 } else {
279 unsigned long ptr = (unsigned long)di;
280 unsigned long start;
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);
287 return ret;
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",
299 (int)type);
300 return 1;
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));
309 return 1;
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)) > BTRFS_MAX_XATTR_SIZE(root)) {
315 fprintf(stderr, "invalid dir item name + data len: %u + %u\n",
316 (unsigned)btrfs_dir_name_len(leaf, dir_item),
317 (unsigned)btrfs_dir_data_len(leaf, dir_item));
318 return 1;
321 return 0;
324 static struct btrfs_dir_item *btrfs_match_dir_item_name(struct btrfs_root *root,
325 struct btrfs_path *path,
326 const char *name, int name_len)
328 struct btrfs_dir_item *dir_item;
329 unsigned long name_ptr;
330 u32 total_len;
331 u32 cur = 0;
332 u32 this_len;
333 struct extent_buffer *leaf;
335 leaf = path->nodes[0];
336 dir_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dir_item);
337 total_len = btrfs_item_size_nr(leaf, path->slots[0]);
338 if (verify_dir_item(root, leaf, dir_item))
339 return NULL;
341 while(cur < total_len) {
342 this_len = sizeof(*dir_item) +
343 btrfs_dir_name_len(leaf, dir_item) +
344 btrfs_dir_data_len(leaf, dir_item);
345 if (this_len > (total_len - cur)) {
346 fprintf(stderr, "invalid dir item size\n");
347 return NULL;
350 name_ptr = (unsigned long)(dir_item + 1);
352 if (btrfs_dir_name_len(leaf, dir_item) == name_len &&
353 memcmp_extent_buffer(leaf, name, name_ptr, name_len) == 0)
354 return dir_item;
356 cur += this_len;
357 dir_item = (struct btrfs_dir_item *)((char *)dir_item +
358 this_len);
360 return NULL;