blob: 7aed9f015b557c6691f0f29155a22fe41c86c0e5 [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 Mason7fcde0e2007-04-05 12:13:21 -04007int insert_with_overflow(struct btrfs_trans_handle *trans, struct btrfs_root
8 *root, struct btrfs_path *path, struct btrfs_key
9 *cpu_key, u32 data_size)
10{
11 int overflow;
12 int ret;
13
14 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
15 overflow = btrfs_key_overflow(cpu_key);
16
17 while(ret == -EEXIST && overflow < BTRFS_KEY_OVERFLOW_MAX) {
18 overflow++;
19 btrfs_set_key_overflow(cpu_key, overflow);
20 btrfs_release_path(root, path);
21 ret = btrfs_insert_empty_item(trans, root, path, cpu_key,
22 data_size);
23 }
24 return ret;
25}
26
Chris Masone089f052007-03-16 16:20:31 -040027int btrfs_insert_dir_item(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masond6e4a422007-04-06 15:37:36 -040028 *root, const char *name, int name_len, u64 dir,
29 struct btrfs_key *location, u8 type)
Chris Mason62e27492007-03-15 12:56:47 -040030{
31 int ret = 0;
Chris Mason5caf2a02007-04-02 11:20:42 -040032 struct btrfs_path *path;
Chris Mason62e27492007-03-15 12:56:47 -040033 struct btrfs_dir_item *dir_item;
34 char *name_ptr;
35 struct btrfs_key key;
36 u32 data_size;
37
38 key.objectid = dir;
39 key.flags = 0;
Chris Mason1d4f6402007-03-15 15:18:43 -040040 btrfs_set_key_type(&key, BTRFS_DIR_ITEM_KEY);
Chris Masone20d96d2007-03-22 12:13:20 -040041 ret = btrfs_name_hash(name, name_len, &key.offset);
Chris Mason62e27492007-03-15 12:56:47 -040042 BUG_ON(ret);
Chris Mason5caf2a02007-04-02 11:20:42 -040043 path = btrfs_alloc_path();
44 btrfs_init_path(path);
Chris Mason62e27492007-03-15 12:56:47 -040045 data_size = sizeof(*dir_item) + name_len;
Chris Mason7fcde0e2007-04-05 12:13:21 -040046 ret = insert_with_overflow(trans, root, path, &key, data_size);
Chris Mason62e27492007-03-15 12:56:47 -040047 if (ret)
48 goto out;
49
Chris Mason5caf2a02007-04-02 11:20:42 -040050 dir_item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
51 path->slots[0],
Chris Mason62e27492007-03-15 12:56:47 -040052 struct btrfs_dir_item);
Chris Masond6e4a422007-04-06 15:37:36 -040053 btrfs_cpu_key_to_disk(&dir_item->location, location);
Chris Mason62e27492007-03-15 12:56:47 -040054 btrfs_set_dir_type(dir_item, type);
55 btrfs_set_dir_flags(dir_item, 0);
Chris Masona8a2ee02007-03-16 08:46:49 -040056 btrfs_set_dir_name_len(dir_item, name_len);
Chris Mason62e27492007-03-15 12:56:47 -040057 name_ptr = (char *)(dir_item + 1);
Chris Masond6e4a422007-04-06 15:37:36 -040058 /* FIXME, use some real flag for selecting the extra index */
59 if (root == root->fs_info->tree_root)
60 goto out;
61
Chris Mason5caf2a02007-04-02 11:20:42 -040062 btrfs_memcpy(root, path->nodes[0]->b_data, name_ptr, name, name_len);
63 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Masonbae45de2007-04-04 21:22:22 -040064 btrfs_release_path(root, path);
65
66 btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
Chris Masond6e4a422007-04-06 15:37:36 -040067 key.offset = location->objectid;
Chris Mason7fcde0e2007-04-05 12:13:21 -040068 ret = insert_with_overflow(trans, root, path, &key, data_size);
Chris Masonbae45de2007-04-04 21:22:22 -040069 // FIXME clear the dirindex bit
70 if (ret)
71 goto out;
72
73 dir_item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
74 path->slots[0],
75 struct btrfs_dir_item);
Chris Masond6e4a422007-04-06 15:37:36 -040076 btrfs_cpu_key_to_disk(&dir_item->location, location);
Chris Masonbae45de2007-04-04 21:22:22 -040077 btrfs_set_dir_type(dir_item, type);
78 btrfs_set_dir_flags(dir_item, 0);
79 btrfs_set_dir_name_len(dir_item, name_len);
80 name_ptr = (char *)(dir_item + 1);
81 btrfs_memcpy(root, path->nodes[0]->b_data, name_ptr, name, name_len);
82 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Mason62e27492007-03-15 12:56:47 -040083out:
Chris Mason5caf2a02007-04-02 11:20:42 -040084 btrfs_release_path(root, path);
85 btrfs_free_path(path);
Chris Mason62e27492007-03-15 12:56:47 -040086 return ret;
87}
88
Chris Masone089f052007-03-16 16:20:31 -040089int btrfs_lookup_dir_item(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masone20d96d2007-03-22 12:13:20 -040090 *root, struct btrfs_path *path, u64 dir,
91 const char *name, int name_len, int mod)
Chris Mason62e27492007-03-15 12:56:47 -040092{
Chris Mason1d4f6402007-03-15 15:18:43 -040093 int ret;
Chris Mason62e27492007-03-15 12:56:47 -040094 struct btrfs_key key;
Chris Mason1d4f6402007-03-15 15:18:43 -040095 int ins_len = mod < 0 ? -1 : 0;
96 int cow = mod != 0;
Chris Mason7fcde0e2007-04-05 12:13:21 -040097 struct btrfs_disk_key *found_key;
98 struct btrfs_leaf *leaf;
Chris Mason5be6f7f2007-04-05 13:35:25 -040099 u32 overflow;
Chris Mason62e27492007-03-15 12:56:47 -0400100
101 key.objectid = dir;
102 key.flags = 0;
Chris Mason1d4f6402007-03-15 15:18:43 -0400103 btrfs_set_key_type(&key, BTRFS_DIR_ITEM_KEY);
Chris Mason7fcde0e2007-04-05 12:13:21 -0400104 btrfs_set_key_overflow(&key, BTRFS_KEY_OVERFLOW_MAX - 1);
Chris Mason62e27492007-03-15 12:56:47 -0400105 ret = btrfs_name_hash(name, name_len, &key.offset);
106 BUG_ON(ret);
Chris Mason7fcde0e2007-04-05 12:13:21 -0400107 while(1) {
108 ret = btrfs_search_slot(trans, root, &key, path, ins_len, cow);
109 if (ret < 0)
110 return ret;
111 if (ret > 0) {
112 if (path->slots[0] == 0)
113 return 1;
114 path->slots[0]--;
115 }
116 leaf = btrfs_buffer_leaf(path->nodes[0]);
117 found_key = &leaf->items[path->slots[0]].key;
118
119 if (btrfs_disk_key_objectid(found_key) != dir ||
120 btrfs_disk_key_type(found_key) != BTRFS_DIR_ITEM_KEY ||
121 btrfs_disk_key_offset(found_key) != key.offset)
122 return 1;
123
124 if (btrfs_match_dir_item_name(root, path, name, name_len))
125 return 0;
126
Chris Mason5be6f7f2007-04-05 13:35:25 -0400127 overflow = btrfs_disk_key_overflow(found_key);
128 if (overflow == 0)
Chris Mason7fcde0e2007-04-05 12:13:21 -0400129 return 1;
Chris Mason5be6f7f2007-04-05 13:35:25 -0400130 btrfs_set_key_overflow(&key, overflow - 1);
Chris Mason7fcde0e2007-04-05 12:13:21 -0400131 btrfs_release_path(root, path);
132 }
133 return 1;
Chris Mason62e27492007-03-15 12:56:47 -0400134}
135
Chris Mason5f26f772007-04-05 10:38:44 -0400136int btrfs_lookup_dir_index_item(struct btrfs_trans_handle *trans,
137 struct btrfs_root *root,
138 struct btrfs_path *path, u64 dir,
139 u64 objectid, int mod)
140{
141 int ret;
142 struct btrfs_key key;
143 int ins_len = mod < 0 ? -1 : 0;
144 int cow = mod != 0;
Chris Mason7fcde0e2007-04-05 12:13:21 -0400145 struct btrfs_disk_key *found_key;
146 struct btrfs_leaf *leaf;
Chris Mason5f26f772007-04-05 10:38:44 -0400147
148 key.objectid = dir;
149 key.flags = 0;
150 btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
Chris Mason7fcde0e2007-04-05 12:13:21 -0400151 btrfs_set_key_overflow(&key, BTRFS_KEY_OVERFLOW_MAX - 1);
Chris Mason5f26f772007-04-05 10:38:44 -0400152 key.offset = objectid;
153 ret = btrfs_search_slot(trans, root, &key, path, ins_len, cow);
Chris Mason7fcde0e2007-04-05 12:13:21 -0400154 if (ret < 0)
155 return ret;
156 if (ret > 0) {
157 if (path->slots[0] == 0)
158 return 1;
159 path->slots[0]--;
160 }
161 leaf = btrfs_buffer_leaf(path->nodes[0]);
162 found_key = &leaf->items[path->slots[0]].key;
163
164 if (btrfs_disk_key_objectid(found_key) != dir ||
165 btrfs_disk_key_type(found_key) != BTRFS_DIR_INDEX_KEY)
166 return 1;
167 if (btrfs_disk_key_offset(found_key) == objectid)
168 return 0;
169 return 1;
Chris Mason5f26f772007-04-05 10:38:44 -0400170}
171
Chris Masone089f052007-03-16 16:20:31 -0400172int btrfs_match_dir_item_name(struct btrfs_root *root,
Chris Mason7f5c1512007-03-23 15:56:19 -0400173 struct btrfs_path *path,
174 const char *name, int name_len)
Chris Mason62e27492007-03-15 12:56:47 -0400175{
Chris Mason62e27492007-03-15 12:56:47 -0400176 struct btrfs_dir_item *dir_item;
177 char *name_ptr;
Chris Masona8a2ee02007-03-16 08:46:49 -0400178
Chris Masone20d96d2007-03-22 12:13:20 -0400179 dir_item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
180 path->slots[0],
Chris Mason1d4f6402007-03-15 15:18:43 -0400181 struct btrfs_dir_item);
Chris Masona8a2ee02007-03-16 08:46:49 -0400182 if (btrfs_dir_name_len(dir_item) != name_len)
Chris Mason1d4f6402007-03-15 15:18:43 -0400183 return 0;
Chris Masona8a2ee02007-03-16 08:46:49 -0400184 name_ptr = (char *)(dir_item + 1);
185 if (memcmp(name_ptr, name, name_len))
186 return 0;
Chris Mason1d4f6402007-03-15 15:18:43 -0400187 return 1;
Chris Mason62e27492007-03-15 12:56:47 -0400188}