blob: ff10cf5a84226d6cefcd014e5e3a366121570338 [file] [log] [blame]
Chris Mason6cbd5572007-06-12 09:07:21 -04001/*
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
Chris Mason2e635a22007-03-21 11:12:56 -040019#include <linux/module.h>
Chris Mason62e27492007-03-15 12:56:47 -040020#include "ctree.h"
21#include "disk-io.h"
22#include "hash.h"
Chris Masone089f052007-03-16 16:20:31 -040023#include "transaction.h"
Chris Mason62e27492007-03-15 12:56:47 -040024
Chris Mason35b7e472007-05-02 15:53:43 -040025static struct btrfs_dir_item *insert_with_overflow(struct btrfs_trans_handle
26 *trans,
27 struct btrfs_root *root,
28 struct btrfs_path *path,
29 struct btrfs_key *cpu_key,
Chris Masone06afa82007-05-23 15:44:28 -040030 u32 data_size,
31 const char *name,
32 int name_len)
Chris Mason7fcde0e2007-04-05 12:13:21 -040033{
Chris Mason7fcde0e2007-04-05 12:13:21 -040034 int ret;
Chris Mason7e381802007-04-19 15:36:27 -040035 char *ptr;
36 struct btrfs_item *item;
37 struct btrfs_leaf *leaf;
Chris Mason7fcde0e2007-04-05 12:13:21 -040038
39 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
Chris Mason7e381802007-04-19 15:36:27 -040040 if (ret == -EEXIST) {
Chris Masone06afa82007-05-23 15:44:28 -040041 struct btrfs_dir_item *di;
42 di = btrfs_match_dir_item_name(root, path, name, name_len);
43 if (di)
44 return ERR_PTR(-EEXIST);
Chris Mason7e381802007-04-19 15:36:27 -040045 ret = btrfs_extend_item(trans, root, path, data_size);
46 WARN_ON(ret > 0);
47 if (ret)
48 return ERR_PTR(ret);
Chris Mason7fcde0e2007-04-05 12:13:21 -040049 }
Chris Mason7e381802007-04-19 15:36:27 -040050 WARN_ON(ret > 0);
51 leaf = btrfs_buffer_leaf(path->nodes[0]);
52 item = leaf->items + path->slots[0];
53 ptr = btrfs_item_ptr(leaf, path->slots[0], char);
54 BUG_ON(data_size > btrfs_item_size(item));
55 ptr += btrfs_item_size(item) - data_size;
56 return (struct btrfs_dir_item *)ptr;
Chris Mason7fcde0e2007-04-05 12:13:21 -040057}
58
Chris Masone089f052007-03-16 16:20:31 -040059int btrfs_insert_dir_item(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masond6e4a422007-04-06 15:37:36 -040060 *root, const char *name, int name_len, u64 dir,
61 struct btrfs_key *location, u8 type)
Chris Mason62e27492007-03-15 12:56:47 -040062{
63 int ret = 0;
Chris Masone06afa82007-05-23 15:44:28 -040064 int ret2 = 0;
Chris Mason5caf2a02007-04-02 11:20:42 -040065 struct btrfs_path *path;
Chris Mason62e27492007-03-15 12:56:47 -040066 struct btrfs_dir_item *dir_item;
67 char *name_ptr;
68 struct btrfs_key key;
69 u32 data_size;
70
71 key.objectid = dir;
72 key.flags = 0;
Chris Mason1d4f6402007-03-15 15:18:43 -040073 btrfs_set_key_type(&key, BTRFS_DIR_ITEM_KEY);
Chris Masone20d96d2007-03-22 12:13:20 -040074 ret = btrfs_name_hash(name, name_len, &key.offset);
Chris Mason62e27492007-03-15 12:56:47 -040075 BUG_ON(ret);
Chris Mason5caf2a02007-04-02 11:20:42 -040076 path = btrfs_alloc_path();
Chris Mason62e27492007-03-15 12:56:47 -040077 data_size = sizeof(*dir_item) + name_len;
Chris Masone06afa82007-05-23 15:44:28 -040078 dir_item = insert_with_overflow(trans, root, path, &key, data_size,
79 name, name_len);
Chris Mason7e381802007-04-19 15:36:27 -040080 if (IS_ERR(dir_item)) {
81 ret = PTR_ERR(dir_item);
Chris Masone06afa82007-05-23 15:44:28 -040082 if (ret == -EEXIST)
83 goto second_insert;
Chris Mason62e27492007-03-15 12:56:47 -040084 goto out;
Chris Mason7e381802007-04-19 15:36:27 -040085 }
Chris Mason62e27492007-03-15 12:56:47 -040086
Chris Masond6e4a422007-04-06 15:37:36 -040087 btrfs_cpu_key_to_disk(&dir_item->location, location);
Chris Mason62e27492007-03-15 12:56:47 -040088 btrfs_set_dir_type(dir_item, type);
89 btrfs_set_dir_flags(dir_item, 0);
Chris Masona8a2ee02007-03-16 08:46:49 -040090 btrfs_set_dir_name_len(dir_item, name_len);
Chris Mason62e27492007-03-15 12:56:47 -040091 name_ptr = (char *)(dir_item + 1);
Chris Masonc5739bb2007-04-10 09:27:04 -040092
93 btrfs_memcpy(root, path->nodes[0]->b_data, name_ptr, name, name_len);
94 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Mason7e381802007-04-19 15:36:27 -040095
Chris Masone06afa82007-05-23 15:44:28 -040096second_insert:
Chris Mason7e381802007-04-19 15:36:27 -040097 /* FIXME, use some real flag for selecting the extra index */
98 if (root == root->fs_info->tree_root) {
99 ret = 0;
100 goto out;
101 }
Chris Mason5caf2a02007-04-02 11:20:42 -0400102 btrfs_release_path(root, path);
Chris Mason7e381802007-04-19 15:36:27 -0400103
104 btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
105 key.offset = location->objectid;
Chris Masone06afa82007-05-23 15:44:28 -0400106 dir_item = insert_with_overflow(trans, root, path, &key, data_size,
107 name, name_len);
Chris Mason7e381802007-04-19 15:36:27 -0400108 if (IS_ERR(dir_item)) {
Chris Masone06afa82007-05-23 15:44:28 -0400109 ret2 = PTR_ERR(dir_item);
Chris Mason7e381802007-04-19 15:36:27 -0400110 goto out;
111 }
112 btrfs_cpu_key_to_disk(&dir_item->location, location);
113 btrfs_set_dir_type(dir_item, type);
114 btrfs_set_dir_flags(dir_item, 0);
115 btrfs_set_dir_name_len(dir_item, name_len);
116 name_ptr = (char *)(dir_item + 1);
117 btrfs_memcpy(root, path->nodes[0]->b_data, name_ptr, name, name_len);
118 btrfs_mark_buffer_dirty(path->nodes[0]);
119out:
Chris Mason5caf2a02007-04-02 11:20:42 -0400120 btrfs_free_path(path);
Chris Masone06afa82007-05-23 15:44:28 -0400121 if (ret)
122 return ret;
123 if (ret2)
124 return ret2;
125 return 0;
Chris Mason62e27492007-03-15 12:56:47 -0400126}
127
Chris Mason7e381802007-04-19 15:36:27 -0400128struct btrfs_dir_item *btrfs_lookup_dir_item(struct btrfs_trans_handle *trans,
129 struct btrfs_root *root,
130 struct btrfs_path *path, u64 dir,
131 const char *name, int name_len,
132 int mod)
Chris Mason62e27492007-03-15 12:56:47 -0400133{
Chris Mason1d4f6402007-03-15 15:18:43 -0400134 int ret;
Chris Mason62e27492007-03-15 12:56:47 -0400135 struct btrfs_key key;
Chris Mason1d4f6402007-03-15 15:18:43 -0400136 int ins_len = mod < 0 ? -1 : 0;
137 int cow = mod != 0;
Chris Mason7fcde0e2007-04-05 12:13:21 -0400138 struct btrfs_disk_key *found_key;
139 struct btrfs_leaf *leaf;
Chris Mason62e27492007-03-15 12:56:47 -0400140
141 key.objectid = dir;
142 key.flags = 0;
Chris Mason1d4f6402007-03-15 15:18:43 -0400143 btrfs_set_key_type(&key, BTRFS_DIR_ITEM_KEY);
Chris Mason62e27492007-03-15 12:56:47 -0400144 ret = btrfs_name_hash(name, name_len, &key.offset);
145 BUG_ON(ret);
Chris Mason7e381802007-04-19 15:36:27 -0400146 ret = btrfs_search_slot(trans, root, &key, path, ins_len, cow);
147 if (ret < 0)
148 return ERR_PTR(ret);
149 if (ret > 0) {
150 if (path->slots[0] == 0)
151 return NULL;
152 path->slots[0]--;
Chris Mason7fcde0e2007-04-05 12:13:21 -0400153 }
Chris Mason7e381802007-04-19 15:36:27 -0400154 leaf = btrfs_buffer_leaf(path->nodes[0]);
155 found_key = &leaf->items[path->slots[0]].key;
156
157 if (btrfs_disk_key_objectid(found_key) != dir ||
158 btrfs_disk_key_type(found_key) != BTRFS_DIR_ITEM_KEY ||
159 btrfs_disk_key_offset(found_key) != key.offset)
160 return NULL;
161
162 return btrfs_match_dir_item_name(root, path, name, name_len);
Chris Mason62e27492007-03-15 12:56:47 -0400163}
164
Chris Mason7e381802007-04-19 15:36:27 -0400165struct btrfs_dir_item *
166btrfs_lookup_dir_index_item(struct btrfs_trans_handle *trans,
167 struct btrfs_root *root,
168 struct btrfs_path *path, u64 dir,
169 u64 objectid, const char *name, int name_len,
170 int mod)
171{
172 int ret;
173 struct btrfs_key key;
174 int ins_len = mod < 0 ? -1 : 0;
175 int cow = mod != 0;
176
177 key.objectid = dir;
178 key.flags = 0;
179 btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
180 key.offset = objectid;
181
182 ret = btrfs_search_slot(trans, root, &key, path, ins_len, cow);
183 if (ret < 0)
184 return ERR_PTR(ret);
185 if (ret > 0)
186 return ERR_PTR(-ENOENT);
187 return btrfs_match_dir_item_name(root, path, name, name_len);
188}
189
190struct btrfs_dir_item *btrfs_match_dir_item_name(struct btrfs_root *root,
Chris Mason7f5c1512007-03-23 15:56:19 -0400191 struct btrfs_path *path,
192 const char *name, int name_len)
Chris Mason62e27492007-03-15 12:56:47 -0400193{
Chris Mason62e27492007-03-15 12:56:47 -0400194 struct btrfs_dir_item *dir_item;
195 char *name_ptr;
Chris Mason7e381802007-04-19 15:36:27 -0400196 u32 total_len;
197 u32 cur = 0;
198 u32 this_len;
199 struct btrfs_leaf *leaf;
Chris Masona8a2ee02007-03-16 08:46:49 -0400200
Chris Mason7e381802007-04-19 15:36:27 -0400201 leaf = btrfs_buffer_leaf(path->nodes[0]);
202 dir_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dir_item);
203 total_len = btrfs_item_size(leaf->items + path->slots[0]);
204 while(cur < total_len) {
205 this_len = sizeof(*dir_item) + btrfs_dir_name_len(dir_item);
206 name_ptr = (char *)(dir_item + 1);
207
208 if (btrfs_dir_name_len(dir_item) == name_len &&
209 memcmp(name_ptr, name, name_len) == 0)
210 return dir_item;
211
212 cur += this_len;
213 dir_item = (struct btrfs_dir_item *)((char *)dir_item +
214 this_len);
215 }
216 return NULL;
Chris Mason62e27492007-03-15 12:56:47 -0400217}
Chris Mason7e381802007-04-19 15:36:27 -0400218
219int btrfs_delete_one_dir_name(struct btrfs_trans_handle *trans,
220 struct btrfs_root *root,
221 struct btrfs_path *path,
222 struct btrfs_dir_item *di)
223{
224
225 struct btrfs_leaf *leaf;
226 u32 sub_item_len;
227 u32 item_len;
228 int ret;
229
230 leaf = btrfs_buffer_leaf(path->nodes[0]);
231 sub_item_len = sizeof(*di) + btrfs_dir_name_len(di);
232 item_len = btrfs_item_size(leaf->items + path->slots[0]);
233 if (sub_item_len == btrfs_item_size(leaf->items + path->slots[0])) {
234 ret = btrfs_del_item(trans, root, path);
235 BUG_ON(ret);
236 } else {
237 char *ptr = (char *)di;
238 char *start = btrfs_item_ptr(leaf, path->slots[0], char);
239 btrfs_memmove(root, leaf, ptr, ptr + sub_item_len,
240 item_len - (ptr + sub_item_len - start));
241 ret = btrfs_truncate_item(trans, root, path,
242 item_len - sub_item_len);
243 BUG_ON(ret);
244 }
245 return 0;
246}
247