blob: b408a3d20c7bd33d9cd5f5335c251736559c069e [file] [log] [blame]
Chris Mason2e635a22007-03-21 11:12:56 -04001#include <linux/module.h>
Chris Mason62e27492007-03-15 12:56:47 -04002#include "ctree.h"
3#include "disk-io.h"
4#include "hash.h"
Chris Masone089f052007-03-16 16:20:31 -04005#include "transaction.h"
Chris Mason62e27492007-03-15 12:56:47 -04006
Chris Mason35b7e472007-05-02 15:53:43 -04007static struct btrfs_dir_item *insert_with_overflow(struct btrfs_trans_handle
8 *trans,
9 struct btrfs_root *root,
10 struct btrfs_path *path,
11 struct btrfs_key *cpu_key,
Chris Masone06afa82007-05-23 15:44:28 -040012 u32 data_size,
13 const char *name,
14 int name_len)
Chris Mason7fcde0e2007-04-05 12:13:21 -040015{
Chris Mason7fcde0e2007-04-05 12:13:21 -040016 int ret;
Chris Mason7e381802007-04-19 15:36:27 -040017 char *ptr;
18 struct btrfs_item *item;
19 struct btrfs_leaf *leaf;
Chris Mason7fcde0e2007-04-05 12:13:21 -040020
21 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
Chris Mason7e381802007-04-19 15:36:27 -040022 if (ret == -EEXIST) {
Chris Masone06afa82007-05-23 15:44:28 -040023 struct btrfs_dir_item *di;
24 di = btrfs_match_dir_item_name(root, path, name, name_len);
25 if (di)
26 return ERR_PTR(-EEXIST);
Chris Mason7e381802007-04-19 15:36:27 -040027 ret = btrfs_extend_item(trans, root, path, data_size);
28 WARN_ON(ret > 0);
29 if (ret)
30 return ERR_PTR(ret);
Chris Mason7fcde0e2007-04-05 12:13:21 -040031 }
Chris Mason7e381802007-04-19 15:36:27 -040032 WARN_ON(ret > 0);
33 leaf = btrfs_buffer_leaf(path->nodes[0]);
34 item = leaf->items + path->slots[0];
35 ptr = btrfs_item_ptr(leaf, path->slots[0], char);
36 BUG_ON(data_size > btrfs_item_size(item));
37 ptr += btrfs_item_size(item) - data_size;
38 return (struct btrfs_dir_item *)ptr;
Chris Mason7fcde0e2007-04-05 12:13:21 -040039}
40
Chris Masone089f052007-03-16 16:20:31 -040041int btrfs_insert_dir_item(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masond6e4a422007-04-06 15:37:36 -040042 *root, const char *name, int name_len, u64 dir,
43 struct btrfs_key *location, u8 type)
Chris Mason62e27492007-03-15 12:56:47 -040044{
45 int ret = 0;
Chris Masone06afa82007-05-23 15:44:28 -040046 int ret2 = 0;
Chris Mason5caf2a02007-04-02 11:20:42 -040047 struct btrfs_path *path;
Chris Mason62e27492007-03-15 12:56:47 -040048 struct btrfs_dir_item *dir_item;
49 char *name_ptr;
50 struct btrfs_key key;
51 u32 data_size;
52
53 key.objectid = dir;
54 key.flags = 0;
Chris Mason1d4f6402007-03-15 15:18:43 -040055 btrfs_set_key_type(&key, BTRFS_DIR_ITEM_KEY);
Chris Masone20d96d2007-03-22 12:13:20 -040056 ret = btrfs_name_hash(name, name_len, &key.offset);
Chris Mason62e27492007-03-15 12:56:47 -040057 BUG_ON(ret);
Chris Mason5caf2a02007-04-02 11:20:42 -040058 path = btrfs_alloc_path();
59 btrfs_init_path(path);
Chris Mason62e27492007-03-15 12:56:47 -040060 data_size = sizeof(*dir_item) + name_len;
Chris Masone06afa82007-05-23 15:44:28 -040061 dir_item = insert_with_overflow(trans, root, path, &key, data_size,
62 name, name_len);
Chris Mason7e381802007-04-19 15:36:27 -040063 if (IS_ERR(dir_item)) {
64 ret = PTR_ERR(dir_item);
Chris Masone06afa82007-05-23 15:44:28 -040065 if (ret == -EEXIST)
66 goto second_insert;
Chris Mason62e27492007-03-15 12:56:47 -040067 goto out;
Chris Mason7e381802007-04-19 15:36:27 -040068 }
Chris Mason62e27492007-03-15 12:56:47 -040069
Chris Masond6e4a422007-04-06 15:37:36 -040070 btrfs_cpu_key_to_disk(&dir_item->location, location);
Chris Mason62e27492007-03-15 12:56:47 -040071 btrfs_set_dir_type(dir_item, type);
72 btrfs_set_dir_flags(dir_item, 0);
Chris Masona8a2ee02007-03-16 08:46:49 -040073 btrfs_set_dir_name_len(dir_item, name_len);
Chris Mason62e27492007-03-15 12:56:47 -040074 name_ptr = (char *)(dir_item + 1);
Chris Masonc5739bb2007-04-10 09:27:04 -040075
76 btrfs_memcpy(root, path->nodes[0]->b_data, name_ptr, name, name_len);
77 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Mason7e381802007-04-19 15:36:27 -040078
Chris Masone06afa82007-05-23 15:44:28 -040079second_insert:
Chris Mason7e381802007-04-19 15:36:27 -040080 /* FIXME, use some real flag for selecting the extra index */
81 if (root == root->fs_info->tree_root) {
82 ret = 0;
83 goto out;
84 }
Chris Mason5caf2a02007-04-02 11:20:42 -040085 btrfs_release_path(root, path);
Chris Mason7e381802007-04-19 15:36:27 -040086
87 btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
88 key.offset = location->objectid;
Chris Masone06afa82007-05-23 15:44:28 -040089 dir_item = insert_with_overflow(trans, root, path, &key, data_size,
90 name, name_len);
Chris Mason7e381802007-04-19 15:36:27 -040091 if (IS_ERR(dir_item)) {
Chris Masone06afa82007-05-23 15:44:28 -040092 ret2 = PTR_ERR(dir_item);
Chris Mason7e381802007-04-19 15:36:27 -040093 goto out;
94 }
95 btrfs_cpu_key_to_disk(&dir_item->location, location);
96 btrfs_set_dir_type(dir_item, type);
97 btrfs_set_dir_flags(dir_item, 0);
98 btrfs_set_dir_name_len(dir_item, name_len);
99 name_ptr = (char *)(dir_item + 1);
100 btrfs_memcpy(root, path->nodes[0]->b_data, name_ptr, name, name_len);
101 btrfs_mark_buffer_dirty(path->nodes[0]);
102out:
Chris Mason5caf2a02007-04-02 11:20:42 -0400103 btrfs_free_path(path);
Chris Masone06afa82007-05-23 15:44:28 -0400104 if (ret)
105 return ret;
106 if (ret2)
107 return ret2;
108 return 0;
Chris Mason62e27492007-03-15 12:56:47 -0400109}
110
Chris Mason7e381802007-04-19 15:36:27 -0400111struct btrfs_dir_item *btrfs_lookup_dir_item(struct btrfs_trans_handle *trans,
112 struct btrfs_root *root,
113 struct btrfs_path *path, u64 dir,
114 const char *name, int name_len,
115 int mod)
Chris Mason62e27492007-03-15 12:56:47 -0400116{
Chris Mason1d4f6402007-03-15 15:18:43 -0400117 int ret;
Chris Mason62e27492007-03-15 12:56:47 -0400118 struct btrfs_key key;
Chris Mason1d4f6402007-03-15 15:18:43 -0400119 int ins_len = mod < 0 ? -1 : 0;
120 int cow = mod != 0;
Chris Mason7fcde0e2007-04-05 12:13:21 -0400121 struct btrfs_disk_key *found_key;
122 struct btrfs_leaf *leaf;
Chris Mason62e27492007-03-15 12:56:47 -0400123
124 key.objectid = dir;
125 key.flags = 0;
Chris Mason1d4f6402007-03-15 15:18:43 -0400126 btrfs_set_key_type(&key, BTRFS_DIR_ITEM_KEY);
Chris Mason62e27492007-03-15 12:56:47 -0400127 ret = btrfs_name_hash(name, name_len, &key.offset);
128 BUG_ON(ret);
Chris Mason7e381802007-04-19 15:36:27 -0400129 ret = btrfs_search_slot(trans, root, &key, path, ins_len, cow);
130 if (ret < 0)
131 return ERR_PTR(ret);
132 if (ret > 0) {
133 if (path->slots[0] == 0)
134 return NULL;
135 path->slots[0]--;
Chris Mason7fcde0e2007-04-05 12:13:21 -0400136 }
Chris Mason7e381802007-04-19 15:36:27 -0400137 leaf = btrfs_buffer_leaf(path->nodes[0]);
138 found_key = &leaf->items[path->slots[0]].key;
139
140 if (btrfs_disk_key_objectid(found_key) != dir ||
141 btrfs_disk_key_type(found_key) != BTRFS_DIR_ITEM_KEY ||
142 btrfs_disk_key_offset(found_key) != key.offset)
143 return NULL;
144
145 return btrfs_match_dir_item_name(root, path, name, name_len);
Chris Mason62e27492007-03-15 12:56:47 -0400146}
147
Chris Mason7e381802007-04-19 15:36:27 -0400148struct btrfs_dir_item *
149btrfs_lookup_dir_index_item(struct btrfs_trans_handle *trans,
150 struct btrfs_root *root,
151 struct btrfs_path *path, u64 dir,
152 u64 objectid, const char *name, int name_len,
153 int mod)
154{
155 int ret;
156 struct btrfs_key key;
157 int ins_len = mod < 0 ? -1 : 0;
158 int cow = mod != 0;
159
160 key.objectid = dir;
161 key.flags = 0;
162 btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
163 key.offset = objectid;
164
165 ret = btrfs_search_slot(trans, root, &key, path, ins_len, cow);
166 if (ret < 0)
167 return ERR_PTR(ret);
168 if (ret > 0)
169 return ERR_PTR(-ENOENT);
170 return btrfs_match_dir_item_name(root, path, name, name_len);
171}
172
173struct btrfs_dir_item *btrfs_match_dir_item_name(struct btrfs_root *root,
Chris Mason7f5c1512007-03-23 15:56:19 -0400174 struct btrfs_path *path,
175 const char *name, int name_len)
Chris Mason62e27492007-03-15 12:56:47 -0400176{
Chris Mason62e27492007-03-15 12:56:47 -0400177 struct btrfs_dir_item *dir_item;
178 char *name_ptr;
Chris Mason7e381802007-04-19 15:36:27 -0400179 u32 total_len;
180 u32 cur = 0;
181 u32 this_len;
182 struct btrfs_leaf *leaf;
Chris Masona8a2ee02007-03-16 08:46:49 -0400183
Chris Mason7e381802007-04-19 15:36:27 -0400184 leaf = btrfs_buffer_leaf(path->nodes[0]);
185 dir_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dir_item);
186 total_len = btrfs_item_size(leaf->items + path->slots[0]);
187 while(cur < total_len) {
188 this_len = sizeof(*dir_item) + btrfs_dir_name_len(dir_item);
189 name_ptr = (char *)(dir_item + 1);
190
191 if (btrfs_dir_name_len(dir_item) == name_len &&
192 memcmp(name_ptr, name, name_len) == 0)
193 return dir_item;
194
195 cur += this_len;
196 dir_item = (struct btrfs_dir_item *)((char *)dir_item +
197 this_len);
198 }
199 return NULL;
Chris Mason62e27492007-03-15 12:56:47 -0400200}
Chris Mason7e381802007-04-19 15:36:27 -0400201
202int btrfs_delete_one_dir_name(struct btrfs_trans_handle *trans,
203 struct btrfs_root *root,
204 struct btrfs_path *path,
205 struct btrfs_dir_item *di)
206{
207
208 struct btrfs_leaf *leaf;
209 u32 sub_item_len;
210 u32 item_len;
211 int ret;
212
213 leaf = btrfs_buffer_leaf(path->nodes[0]);
214 sub_item_len = sizeof(*di) + btrfs_dir_name_len(di);
215 item_len = btrfs_item_size(leaf->items + path->slots[0]);
216 if (sub_item_len == btrfs_item_size(leaf->items + path->slots[0])) {
217 ret = btrfs_del_item(trans, root, path);
218 BUG_ON(ret);
219 } else {
220 char *ptr = (char *)di;
221 char *start = btrfs_item_ptr(leaf, path->slots[0], char);
222 btrfs_memmove(root, leaf, ptr, ptr + sub_item_len,
223 item_len - (ptr + sub_item_len - start));
224 ret = btrfs_truncate_item(trans, root, path,
225 item_len - sub_item_len);
226 BUG_ON(ret);
227 }
228 return 0;
229}
230