Commit | Line | Data |
---|---|---|
6cbd5570 CM |
1 | /* |
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 | ||
2e635a27 | 19 | #include <linux/module.h> |
62e2749e CM |
20 | #include "ctree.h" |
21 | #include "disk-io.h" | |
22 | #include "hash.h" | |
e089f05c | 23 | #include "transaction.h" |
62e2749e | 24 | |
35b7e476 CM |
25 | static 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, | |
e06afa83 CM |
30 | u32 data_size, |
31 | const char *name, | |
32 | int name_len) | |
7fcde0e3 | 33 | { |
7fcde0e3 | 34 | int ret; |
7e38180e CM |
35 | char *ptr; |
36 | struct btrfs_item *item; | |
37 | struct btrfs_leaf *leaf; | |
7fcde0e3 CM |
38 | |
39 | ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size); | |
7e38180e | 40 | if (ret == -EEXIST) { |
e06afa83 CM |
41 | 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); | |
7e38180e CM |
45 | ret = btrfs_extend_item(trans, root, path, data_size); |
46 | WARN_ON(ret > 0); | |
47 | if (ret) | |
48 | return ERR_PTR(ret); | |
7fcde0e3 | 49 | } |
7e38180e CM |
50 | 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; | |
7fcde0e3 CM |
57 | } |
58 | ||
e089f05c | 59 | int btrfs_insert_dir_item(struct btrfs_trans_handle *trans, struct btrfs_root |
d6e4a428 CM |
60 | *root, const char *name, int name_len, u64 dir, |
61 | struct btrfs_key *location, u8 type) | |
62e2749e CM |
62 | { |
63 | int ret = 0; | |
e06afa83 | 64 | int ret2 = 0; |
5caf2a00 | 65 | struct btrfs_path *path; |
62e2749e CM |
66 | 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; | |
1d4f6404 | 73 | btrfs_set_key_type(&key, BTRFS_DIR_ITEM_KEY); |
e20d96d6 | 74 | ret = btrfs_name_hash(name, name_len, &key.offset); |
62e2749e | 75 | BUG_ON(ret); |
5caf2a00 | 76 | path = btrfs_alloc_path(); |
62e2749e | 77 | data_size = sizeof(*dir_item) + name_len; |
e06afa83 CM |
78 | dir_item = insert_with_overflow(trans, root, path, &key, data_size, |
79 | name, name_len); | |
7e38180e CM |
80 | if (IS_ERR(dir_item)) { |
81 | ret = PTR_ERR(dir_item); | |
e06afa83 CM |
82 | if (ret == -EEXIST) |
83 | goto second_insert; | |
62e2749e | 84 | goto out; |
7e38180e | 85 | } |
62e2749e | 86 | |
d6e4a428 | 87 | btrfs_cpu_key_to_disk(&dir_item->location, location); |
62e2749e CM |
88 | btrfs_set_dir_type(dir_item, type); |
89 | btrfs_set_dir_flags(dir_item, 0); | |
a8a2ee0c | 90 | btrfs_set_dir_name_len(dir_item, name_len); |
62e2749e | 91 | name_ptr = (char *)(dir_item + 1); |
c5739bba CM |
92 | |
93 | btrfs_memcpy(root, path->nodes[0]->b_data, name_ptr, name, name_len); | |
94 | btrfs_mark_buffer_dirty(path->nodes[0]); | |
7e38180e | 95 | |
e06afa83 | 96 | second_insert: |
7e38180e CM |
97 | /* 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 | } | |
5caf2a00 | 102 | btrfs_release_path(root, path); |
7e38180e CM |
103 | |
104 | btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY); | |
105 | key.offset = location->objectid; | |
e06afa83 CM |
106 | dir_item = insert_with_overflow(trans, root, path, &key, data_size, |
107 | name, name_len); | |
7e38180e | 108 | if (IS_ERR(dir_item)) { |
e06afa83 | 109 | ret2 = PTR_ERR(dir_item); |
7e38180e CM |
110 | 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]); | |
119 | out: | |
5caf2a00 | 120 | btrfs_free_path(path); |
e06afa83 CM |
121 | if (ret) |
122 | return ret; | |
123 | if (ret2) | |
124 | return ret2; | |
125 | return 0; | |
62e2749e CM |
126 | } |
127 | ||
7e38180e CM |
128 | struct 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) | |
62e2749e | 133 | { |
1d4f6404 | 134 | int ret; |
62e2749e | 135 | struct btrfs_key key; |
1d4f6404 CM |
136 | int ins_len = mod < 0 ? -1 : 0; |
137 | int cow = mod != 0; | |
7fcde0e3 CM |
138 | struct btrfs_disk_key *found_key; |
139 | struct btrfs_leaf *leaf; | |
62e2749e CM |
140 | |
141 | key.objectid = dir; | |
142 | key.flags = 0; | |
1d4f6404 | 143 | btrfs_set_key_type(&key, BTRFS_DIR_ITEM_KEY); |
62e2749e CM |
144 | ret = btrfs_name_hash(name, name_len, &key.offset); |
145 | BUG_ON(ret); | |
7e38180e CM |
146 | 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]--; | |
7fcde0e3 | 153 | } |
7e38180e CM |
154 | 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); | |
62e2749e CM |
163 | } |
164 | ||
7e38180e CM |
165 | struct btrfs_dir_item * |
166 | btrfs_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 | ||
190 | struct btrfs_dir_item *btrfs_match_dir_item_name(struct btrfs_root *root, | |
7f5c1516 CM |
191 | struct btrfs_path *path, |
192 | const char *name, int name_len) | |
62e2749e | 193 | { |
62e2749e CM |
194 | struct btrfs_dir_item *dir_item; |
195 | char *name_ptr; | |
7e38180e CM |
196 | u32 total_len; |
197 | u32 cur = 0; | |
198 | u32 this_len; | |
199 | struct btrfs_leaf *leaf; | |
a8a2ee0c | 200 | |
7e38180e CM |
201 | 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; | |
62e2749e | 217 | } |
7e38180e CM |
218 | |
219 | int 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 |