blob: d5db8bdea3d81622608b25983d9455235a347ada [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 Mason54aa1f42007-06-22 14:16:25 -040050 if (ret < 0)
51 return ERR_PTR(ret);
Chris Mason7e381802007-04-19 15:36:27 -040052 WARN_ON(ret > 0);
53 leaf = btrfs_buffer_leaf(path->nodes[0]);
54 item = leaf->items + path->slots[0];
55 ptr = btrfs_item_ptr(leaf, path->slots[0], char);
56 BUG_ON(data_size > btrfs_item_size(item));
57 ptr += btrfs_item_size(item) - data_size;
58 return (struct btrfs_dir_item *)ptr;
Chris Mason7fcde0e2007-04-05 12:13:21 -040059}
60
Chris Masone089f052007-03-16 16:20:31 -040061int btrfs_insert_dir_item(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masond6e4a422007-04-06 15:37:36 -040062 *root, const char *name, int name_len, u64 dir,
63 struct btrfs_key *location, u8 type)
Chris Mason62e27492007-03-15 12:56:47 -040064{
65 int ret = 0;
Chris Masone06afa82007-05-23 15:44:28 -040066 int ret2 = 0;
Chris Mason5caf2a02007-04-02 11:20:42 -040067 struct btrfs_path *path;
Chris Mason62e27492007-03-15 12:56:47 -040068 struct btrfs_dir_item *dir_item;
69 char *name_ptr;
70 struct btrfs_key key;
71 u32 data_size;
72
73 key.objectid = dir;
74 key.flags = 0;
Chris Mason1d4f6402007-03-15 15:18:43 -040075 btrfs_set_key_type(&key, BTRFS_DIR_ITEM_KEY);
Chris Masone20d96d2007-03-22 12:13:20 -040076 ret = btrfs_name_hash(name, name_len, &key.offset);
Chris Mason62e27492007-03-15 12:56:47 -040077 BUG_ON(ret);
Chris Mason5caf2a02007-04-02 11:20:42 -040078 path = btrfs_alloc_path();
Chris Mason62e27492007-03-15 12:56:47 -040079 data_size = sizeof(*dir_item) + name_len;
Chris Masone06afa82007-05-23 15:44:28 -040080 dir_item = insert_with_overflow(trans, root, path, &key, data_size,
81 name, name_len);
Chris Mason7e381802007-04-19 15:36:27 -040082 if (IS_ERR(dir_item)) {
83 ret = PTR_ERR(dir_item);
Chris Masone06afa82007-05-23 15:44:28 -040084 if (ret == -EEXIST)
85 goto second_insert;
Chris Mason62e27492007-03-15 12:56:47 -040086 goto out;
Chris Mason7e381802007-04-19 15:36:27 -040087 }
Chris Mason62e27492007-03-15 12:56:47 -040088
Chris Masond6e4a422007-04-06 15:37:36 -040089 btrfs_cpu_key_to_disk(&dir_item->location, location);
Chris Mason62e27492007-03-15 12:56:47 -040090 btrfs_set_dir_type(dir_item, type);
91 btrfs_set_dir_flags(dir_item, 0);
Chris Masona8a2ee02007-03-16 08:46:49 -040092 btrfs_set_dir_name_len(dir_item, name_len);
Chris Mason62e27492007-03-15 12:56:47 -040093 name_ptr = (char *)(dir_item + 1);
Chris Masonc5739bb2007-04-10 09:27:04 -040094
95 btrfs_memcpy(root, path->nodes[0]->b_data, name_ptr, name, name_len);
96 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Mason7e381802007-04-19 15:36:27 -040097
Chris Masone06afa82007-05-23 15:44:28 -040098second_insert:
Chris Mason7e381802007-04-19 15:36:27 -040099 /* FIXME, use some real flag for selecting the extra index */
100 if (root == root->fs_info->tree_root) {
101 ret = 0;
102 goto out;
103 }
Chris Mason5caf2a02007-04-02 11:20:42 -0400104 btrfs_release_path(root, path);
Chris Mason7e381802007-04-19 15:36:27 -0400105
106 btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
107 key.offset = location->objectid;
Chris Masone06afa82007-05-23 15:44:28 -0400108 dir_item = insert_with_overflow(trans, root, path, &key, data_size,
109 name, name_len);
Chris Mason7e381802007-04-19 15:36:27 -0400110 if (IS_ERR(dir_item)) {
Chris Masone06afa82007-05-23 15:44:28 -0400111 ret2 = PTR_ERR(dir_item);
Chris Mason7e381802007-04-19 15:36:27 -0400112 goto out;
113 }
114 btrfs_cpu_key_to_disk(&dir_item->location, location);
115 btrfs_set_dir_type(dir_item, type);
116 btrfs_set_dir_flags(dir_item, 0);
117 btrfs_set_dir_name_len(dir_item, name_len);
118 name_ptr = (char *)(dir_item + 1);
119 btrfs_memcpy(root, path->nodes[0]->b_data, name_ptr, name, name_len);
120 btrfs_mark_buffer_dirty(path->nodes[0]);
121out:
Chris Mason5caf2a02007-04-02 11:20:42 -0400122 btrfs_free_path(path);
Chris Masone06afa82007-05-23 15:44:28 -0400123 if (ret)
124 return ret;
125 if (ret2)
126 return ret2;
127 return 0;
Chris Mason62e27492007-03-15 12:56:47 -0400128}
129
Chris Mason7e381802007-04-19 15:36:27 -0400130struct btrfs_dir_item *btrfs_lookup_dir_item(struct btrfs_trans_handle *trans,
131 struct btrfs_root *root,
132 struct btrfs_path *path, u64 dir,
133 const char *name, int name_len,
134 int mod)
Chris Mason62e27492007-03-15 12:56:47 -0400135{
Chris Mason1d4f6402007-03-15 15:18:43 -0400136 int ret;
Chris Mason62e27492007-03-15 12:56:47 -0400137 struct btrfs_key key;
Chris Mason1d4f6402007-03-15 15:18:43 -0400138 int ins_len = mod < 0 ? -1 : 0;
139 int cow = mod != 0;
Chris Mason7fcde0e2007-04-05 12:13:21 -0400140 struct btrfs_disk_key *found_key;
141 struct btrfs_leaf *leaf;
Chris Mason62e27492007-03-15 12:56:47 -0400142
143 key.objectid = dir;
144 key.flags = 0;
Chris Mason1d4f6402007-03-15 15:18:43 -0400145 btrfs_set_key_type(&key, BTRFS_DIR_ITEM_KEY);
Chris Mason62e27492007-03-15 12:56:47 -0400146 ret = btrfs_name_hash(name, name_len, &key.offset);
147 BUG_ON(ret);
Chris Mason7e381802007-04-19 15:36:27 -0400148 ret = btrfs_search_slot(trans, root, &key, path, ins_len, cow);
149 if (ret < 0)
150 return ERR_PTR(ret);
151 if (ret > 0) {
152 if (path->slots[0] == 0)
153 return NULL;
154 path->slots[0]--;
Chris Mason7fcde0e2007-04-05 12:13:21 -0400155 }
Chris Mason7e381802007-04-19 15:36:27 -0400156 leaf = btrfs_buffer_leaf(path->nodes[0]);
157 found_key = &leaf->items[path->slots[0]].key;
158
159 if (btrfs_disk_key_objectid(found_key) != dir ||
160 btrfs_disk_key_type(found_key) != BTRFS_DIR_ITEM_KEY ||
161 btrfs_disk_key_offset(found_key) != key.offset)
162 return NULL;
163
164 return btrfs_match_dir_item_name(root, path, name, name_len);
Chris Mason62e27492007-03-15 12:56:47 -0400165}
166
Chris Mason7e381802007-04-19 15:36:27 -0400167struct btrfs_dir_item *
168btrfs_lookup_dir_index_item(struct btrfs_trans_handle *trans,
169 struct btrfs_root *root,
170 struct btrfs_path *path, u64 dir,
171 u64 objectid, const char *name, int name_len,
172 int mod)
173{
174 int ret;
175 struct btrfs_key key;
176 int ins_len = mod < 0 ? -1 : 0;
177 int cow = mod != 0;
178
179 key.objectid = dir;
180 key.flags = 0;
181 btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
182 key.offset = objectid;
183
184 ret = btrfs_search_slot(trans, root, &key, path, ins_len, cow);
185 if (ret < 0)
186 return ERR_PTR(ret);
187 if (ret > 0)
188 return ERR_PTR(-ENOENT);
189 return btrfs_match_dir_item_name(root, path, name, name_len);
190}
191
192struct btrfs_dir_item *btrfs_match_dir_item_name(struct btrfs_root *root,
Chris Mason7f5c1512007-03-23 15:56:19 -0400193 struct btrfs_path *path,
194 const char *name, int name_len)
Chris Mason62e27492007-03-15 12:56:47 -0400195{
Chris Mason62e27492007-03-15 12:56:47 -0400196 struct btrfs_dir_item *dir_item;
197 char *name_ptr;
Chris Mason7e381802007-04-19 15:36:27 -0400198 u32 total_len;
199 u32 cur = 0;
200 u32 this_len;
201 struct btrfs_leaf *leaf;
Chris Masona8a2ee02007-03-16 08:46:49 -0400202
Chris Mason7e381802007-04-19 15:36:27 -0400203 leaf = btrfs_buffer_leaf(path->nodes[0]);
204 dir_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dir_item);
205 total_len = btrfs_item_size(leaf->items + path->slots[0]);
206 while(cur < total_len) {
207 this_len = sizeof(*dir_item) + btrfs_dir_name_len(dir_item);
208 name_ptr = (char *)(dir_item + 1);
209
210 if (btrfs_dir_name_len(dir_item) == name_len &&
211 memcmp(name_ptr, name, name_len) == 0)
212 return dir_item;
213
214 cur += this_len;
215 dir_item = (struct btrfs_dir_item *)((char *)dir_item +
216 this_len);
217 }
218 return NULL;
Chris Mason62e27492007-03-15 12:56:47 -0400219}
Chris Mason7e381802007-04-19 15:36:27 -0400220
221int btrfs_delete_one_dir_name(struct btrfs_trans_handle *trans,
222 struct btrfs_root *root,
223 struct btrfs_path *path,
224 struct btrfs_dir_item *di)
225{
226
227 struct btrfs_leaf *leaf;
228 u32 sub_item_len;
229 u32 item_len;
Chris Mason54aa1f42007-06-22 14:16:25 -0400230 int ret = 0;
Chris Mason7e381802007-04-19 15:36:27 -0400231
232 leaf = btrfs_buffer_leaf(path->nodes[0]);
233 sub_item_len = sizeof(*di) + btrfs_dir_name_len(di);
234 item_len = btrfs_item_size(leaf->items + path->slots[0]);
235 if (sub_item_len == btrfs_item_size(leaf->items + path->slots[0])) {
236 ret = btrfs_del_item(trans, root, path);
Chris Mason7e381802007-04-19 15:36:27 -0400237 } else {
238 char *ptr = (char *)di;
239 char *start = btrfs_item_ptr(leaf, path->slots[0], char);
240 btrfs_memmove(root, leaf, ptr, ptr + sub_item_len,
241 item_len - (ptr + sub_item_len - start));
242 ret = btrfs_truncate_item(trans, root, path,
243 item_len - sub_item_len);
Chris Mason7e381802007-04-19 15:36:27 -0400244 }
245 return 0;
246}
247