diff options
author | Chris Mason <chris.mason@oracle.com> | 2007-04-19 15:36:27 -0400 |
---|---|---|
committer | David Woodhouse <dwmw2@hera.kernel.org> | 2007-04-19 15:36:27 -0400 |
commit | 7e38180e207c8707691c12395e990a5d583c5e71 (patch) | |
tree | 50fc0973873f7bc9455a1faf536a5a5b4681fe56 /fs/btrfs/dir-item.c | |
parent | 236454dfffb64a95ee01c50a215153f5de61c475 (diff) |
Btrfs: directory inode index is back
Signed-off-by: Chris Mason <chris.mason@oracle.com>
Diffstat (limited to 'fs/btrfs/dir-item.c')
-rw-r--r-- | fs/btrfs/dir-item.c | 204 |
1 files changed, 144 insertions, 60 deletions
diff --git a/fs/btrfs/dir-item.c b/fs/btrfs/dir-item.c index a43deb726486..7a7e9846860a 100644 --- a/fs/btrfs/dir-item.c +++ b/fs/btrfs/dir-item.c | |||
@@ -4,24 +4,31 @@ | |||
4 | #include "hash.h" | 4 | #include "hash.h" |
5 | #include "transaction.h" | 5 | #include "transaction.h" |
6 | 6 | ||
7 | int insert_with_overflow(struct btrfs_trans_handle *trans, struct btrfs_root | 7 | struct btrfs_dir_item *insert_with_overflow(struct btrfs_trans_handle *trans, |
8 | *root, struct btrfs_path *path, struct btrfs_key | 8 | struct btrfs_root *root, |
9 | *cpu_key, u32 data_size) | 9 | struct btrfs_path *path, |
10 | struct btrfs_key *cpu_key, | ||
11 | u32 data_size) | ||
10 | { | 12 | { |
11 | int overflow; | ||
12 | int ret; | 13 | int ret; |
14 | char *ptr; | ||
15 | struct btrfs_item *item; | ||
16 | struct btrfs_leaf *leaf; | ||
13 | 17 | ||
14 | ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size); | 18 | ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size); |
15 | overflow = btrfs_key_overflow(cpu_key); | 19 | if (ret == -EEXIST) { |
16 | 20 | ret = btrfs_extend_item(trans, root, path, data_size); | |
17 | while(ret == -EEXIST && overflow < BTRFS_KEY_OVERFLOW_MAX) { | 21 | WARN_ON(ret > 0); |
18 | overflow++; | 22 | if (ret) |
19 | btrfs_set_key_overflow(cpu_key, overflow); | 23 | return ERR_PTR(ret); |
20 | btrfs_release_path(root, path); | ||
21 | ret = btrfs_insert_empty_item(trans, root, path, cpu_key, | ||
22 | data_size); | ||
23 | } | 24 | } |
24 | return ret; | 25 | WARN_ON(ret > 0); |
26 | leaf = btrfs_buffer_leaf(path->nodes[0]); | ||
27 | item = leaf->items + path->slots[0]; | ||
28 | ptr = btrfs_item_ptr(leaf, path->slots[0], char); | ||
29 | BUG_ON(data_size > btrfs_item_size(item)); | ||
30 | ptr += btrfs_item_size(item) - data_size; | ||
31 | return (struct btrfs_dir_item *)ptr; | ||
25 | } | 32 | } |
26 | 33 | ||
27 | int btrfs_insert_dir_item(struct btrfs_trans_handle *trans, struct btrfs_root | 34 | int btrfs_insert_dir_item(struct btrfs_trans_handle *trans, struct btrfs_root |
@@ -43,13 +50,12 @@ int btrfs_insert_dir_item(struct btrfs_trans_handle *trans, struct btrfs_root | |||
43 | path = btrfs_alloc_path(); | 50 | path = btrfs_alloc_path(); |
44 | btrfs_init_path(path); | 51 | btrfs_init_path(path); |
45 | data_size = sizeof(*dir_item) + name_len; | 52 | data_size = sizeof(*dir_item) + name_len; |
46 | ret = insert_with_overflow(trans, root, path, &key, data_size); | 53 | dir_item = insert_with_overflow(trans, root, path, &key, data_size); |
47 | if (ret) | 54 | if (IS_ERR(dir_item)) { |
55 | ret = PTR_ERR(dir_item); | ||
48 | goto out; | 56 | goto out; |
57 | } | ||
49 | 58 | ||
50 | dir_item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), | ||
51 | path->slots[0], | ||
52 | struct btrfs_dir_item); | ||
53 | btrfs_cpu_key_to_disk(&dir_item->location, location); | 59 | btrfs_cpu_key_to_disk(&dir_item->location, location); |
54 | btrfs_set_dir_type(dir_item, type); | 60 | btrfs_set_dir_type(dir_item, type); |
55 | btrfs_set_dir_flags(dir_item, 0); | 61 | btrfs_set_dir_flags(dir_item, 0); |
@@ -58,15 +64,39 @@ int btrfs_insert_dir_item(struct btrfs_trans_handle *trans, struct btrfs_root | |||
58 | 64 | ||
59 | btrfs_memcpy(root, path->nodes[0]->b_data, name_ptr, name, name_len); | 65 | btrfs_memcpy(root, path->nodes[0]->b_data, name_ptr, name, name_len); |
60 | btrfs_mark_buffer_dirty(path->nodes[0]); | 66 | btrfs_mark_buffer_dirty(path->nodes[0]); |
61 | out: | 67 | |
68 | /* FIXME, use some real flag for selecting the extra index */ | ||
69 | if (root == root->fs_info->tree_root) { | ||
70 | ret = 0; | ||
71 | goto out; | ||
72 | } | ||
73 | |||
62 | btrfs_release_path(root, path); | 74 | btrfs_release_path(root, path); |
75 | |||
76 | btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY); | ||
77 | key.offset = location->objectid; | ||
78 | dir_item = insert_with_overflow(trans, root, path, &key, data_size); | ||
79 | if (IS_ERR(dir_item)) { | ||
80 | ret = PTR_ERR(dir_item); | ||
81 | goto out; | ||
82 | } | ||
83 | btrfs_cpu_key_to_disk(&dir_item->location, location); | ||
84 | btrfs_set_dir_type(dir_item, type); | ||
85 | btrfs_set_dir_flags(dir_item, 0); | ||
86 | btrfs_set_dir_name_len(dir_item, name_len); | ||
87 | name_ptr = (char *)(dir_item + 1); | ||
88 | btrfs_memcpy(root, path->nodes[0]->b_data, name_ptr, name, name_len); | ||
89 | btrfs_mark_buffer_dirty(path->nodes[0]); | ||
90 | out: | ||
63 | btrfs_free_path(path); | 91 | btrfs_free_path(path); |
64 | return ret; | 92 | return ret; |
65 | } | 93 | } |
66 | 94 | ||
67 | int btrfs_lookup_dir_item(struct btrfs_trans_handle *trans, struct btrfs_root | 95 | struct btrfs_dir_item *btrfs_lookup_dir_item(struct btrfs_trans_handle *trans, |
68 | *root, struct btrfs_path *path, u64 dir, | 96 | struct btrfs_root *root, |
69 | const char *name, int name_len, int mod) | 97 | struct btrfs_path *path, u64 dir, |
98 | const char *name, int name_len, | ||
99 | int mod) | ||
70 | { | 100 | { |
71 | int ret; | 101 | int ret; |
72 | struct btrfs_key key; | 102 | struct btrfs_key key; |
@@ -74,57 +104,111 @@ int btrfs_lookup_dir_item(struct btrfs_trans_handle *trans, struct btrfs_root | |||
74 | int cow = mod != 0; | 104 | int cow = mod != 0; |
75 | struct btrfs_disk_key *found_key; | 105 | struct btrfs_disk_key *found_key; |
76 | struct btrfs_leaf *leaf; | 106 | struct btrfs_leaf *leaf; |
77 | u32 overflow; | ||
78 | 107 | ||
79 | key.objectid = dir; | 108 | key.objectid = dir; |
80 | key.flags = 0; | 109 | key.flags = 0; |
81 | btrfs_set_key_type(&key, BTRFS_DIR_ITEM_KEY); | 110 | btrfs_set_key_type(&key, BTRFS_DIR_ITEM_KEY); |
82 | // btrfs_set_key_overflow(&key, BTRFS_KEY_OVERFLOW_MAX - 1); | ||
83 | ret = btrfs_name_hash(name, name_len, &key.offset); | 111 | ret = btrfs_name_hash(name, name_len, &key.offset); |
84 | BUG_ON(ret); | 112 | BUG_ON(ret); |
85 | while(1) { | 113 | ret = btrfs_search_slot(trans, root, &key, path, ins_len, cow); |
86 | ret = btrfs_search_slot(trans, root, &key, path, ins_len, cow); | 114 | if (ret < 0) |
87 | if (ret < 0) | 115 | return ERR_PTR(ret); |
88 | return ret; | 116 | if (ret > 0) { |
89 | if (ret > 0) { | 117 | if (path->slots[0] == 0) |
90 | if (path->slots[0] == 0) | 118 | return NULL; |
91 | return 1; | 119 | path->slots[0]--; |
92 | path->slots[0]--; | ||
93 | } | ||
94 | leaf = btrfs_buffer_leaf(path->nodes[0]); | ||
95 | found_key = &leaf->items[path->slots[0]].key; | ||
96 | |||
97 | if (btrfs_disk_key_objectid(found_key) != dir || | ||
98 | btrfs_disk_key_type(found_key) != BTRFS_DIR_ITEM_KEY || | ||
99 | btrfs_disk_key_offset(found_key) != key.offset) | ||
100 | return 1; | ||
101 | |||
102 | if (btrfs_match_dir_item_name(root, path, name, name_len)) | ||
103 | return 0; | ||
104 | |||
105 | overflow = btrfs_disk_key_overflow(found_key); | ||
106 | if (overflow == 0) | ||
107 | return 1; | ||
108 | btrfs_set_key_overflow(&key, overflow - 1); | ||
109 | btrfs_release_path(root, path); | ||
110 | } | 120 | } |
111 | return 1; | 121 | leaf = btrfs_buffer_leaf(path->nodes[0]); |
122 | found_key = &leaf->items[path->slots[0]].key; | ||
123 | |||
124 | if (btrfs_disk_key_objectid(found_key) != dir || | ||
125 | btrfs_disk_key_type(found_key) != BTRFS_DIR_ITEM_KEY || | ||
126 | btrfs_disk_key_offset(found_key) != key.offset) | ||
127 | return NULL; | ||
128 | |||
129 | return btrfs_match_dir_item_name(root, path, name, name_len); | ||
112 | } | 130 | } |
113 | 131 | ||
114 | int btrfs_match_dir_item_name(struct btrfs_root *root, | 132 | struct btrfs_dir_item * |
133 | btrfs_lookup_dir_index_item(struct btrfs_trans_handle *trans, | ||
134 | struct btrfs_root *root, | ||
135 | struct btrfs_path *path, u64 dir, | ||
136 | u64 objectid, const char *name, int name_len, | ||
137 | int mod) | ||
138 | { | ||
139 | int ret; | ||
140 | struct btrfs_key key; | ||
141 | int ins_len = mod < 0 ? -1 : 0; | ||
142 | int cow = mod != 0; | ||
143 | |||
144 | key.objectid = dir; | ||
145 | key.flags = 0; | ||
146 | btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY); | ||
147 | key.offset = objectid; | ||
148 | |||
149 | ret = btrfs_search_slot(trans, root, &key, path, ins_len, cow); | ||
150 | if (ret < 0) | ||
151 | return ERR_PTR(ret); | ||
152 | if (ret > 0) | ||
153 | return ERR_PTR(-ENOENT); | ||
154 | return btrfs_match_dir_item_name(root, path, name, name_len); | ||
155 | } | ||
156 | |||
157 | struct btrfs_dir_item *btrfs_match_dir_item_name(struct btrfs_root *root, | ||
115 | struct btrfs_path *path, | 158 | struct btrfs_path *path, |
116 | const char *name, int name_len) | 159 | const char *name, int name_len) |
117 | { | 160 | { |
118 | struct btrfs_dir_item *dir_item; | 161 | struct btrfs_dir_item *dir_item; |
119 | char *name_ptr; | 162 | char *name_ptr; |
163 | u32 total_len; | ||
164 | u32 cur = 0; | ||
165 | u32 this_len; | ||
166 | struct btrfs_leaf *leaf; | ||
120 | 167 | ||
121 | dir_item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), | 168 | leaf = btrfs_buffer_leaf(path->nodes[0]); |
122 | path->slots[0], | 169 | dir_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dir_item); |
123 | struct btrfs_dir_item); | 170 | total_len = btrfs_item_size(leaf->items + path->slots[0]); |
124 | if (btrfs_dir_name_len(dir_item) != name_len) | 171 | while(cur < total_len) { |
125 | return 0; | 172 | this_len = sizeof(*dir_item) + btrfs_dir_name_len(dir_item); |
126 | name_ptr = (char *)(dir_item + 1); | 173 | name_ptr = (char *)(dir_item + 1); |
127 | if (memcmp(name_ptr, name, name_len)) | 174 | |
128 | return 0; | 175 | if (btrfs_dir_name_len(dir_item) == name_len && |
129 | return 1; | 176 | memcmp(name_ptr, name, name_len) == 0) |
177 | return dir_item; | ||
178 | |||
179 | cur += this_len; | ||
180 | dir_item = (struct btrfs_dir_item *)((char *)dir_item + | ||
181 | this_len); | ||
182 | } | ||
183 | return NULL; | ||
130 | } | 184 | } |
185 | |||
186 | int btrfs_delete_one_dir_name(struct btrfs_trans_handle *trans, | ||
187 | struct btrfs_root *root, | ||
188 | struct btrfs_path *path, | ||
189 | struct btrfs_dir_item *di) | ||
190 | { | ||
191 | |||
192 | struct btrfs_leaf *leaf; | ||
193 | u32 sub_item_len; | ||
194 | u32 item_len; | ||
195 | int ret; | ||
196 | |||
197 | leaf = btrfs_buffer_leaf(path->nodes[0]); | ||
198 | sub_item_len = sizeof(*di) + btrfs_dir_name_len(di); | ||
199 | item_len = btrfs_item_size(leaf->items + path->slots[0]); | ||
200 | if (sub_item_len == btrfs_item_size(leaf->items + path->slots[0])) { | ||
201 | ret = btrfs_del_item(trans, root, path); | ||
202 | BUG_ON(ret); | ||
203 | } else { | ||
204 | char *ptr = (char *)di; | ||
205 | char *start = btrfs_item_ptr(leaf, path->slots[0], char); | ||
206 | btrfs_memmove(root, leaf, ptr, ptr + sub_item_len, | ||
207 | item_len - (ptr + sub_item_len - start)); | ||
208 | ret = btrfs_truncate_item(trans, root, path, | ||
209 | item_len - sub_item_len); | ||
210 | BUG_ON(ret); | ||
211 | } | ||
212 | return 0; | ||
213 | } | ||
214 | |||