]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * linux/fs/hfsplus/btree.c | |
3 | * | |
4 | * Copyright (C) 2001 | |
5 | * Brad Boyer (flar@allandria.com) | |
6 | * (C) 2003 Ardis Technologies <roman@ardistech.com> | |
7 | * | |
8 | * Handle opening/closing btree | |
9 | */ | |
10 | ||
11 | #include <linux/slab.h> | |
12 | #include <linux/pagemap.h> | |
e1b5c1d3 | 13 | #include <linux/log2.h> |
1da177e4 LT |
14 | |
15 | #include "hfsplus_fs.h" | |
16 | #include "hfsplus_raw.h" | |
17 | ||
18 | ||
19 | /* Get a reference to a B*Tree and do some initial checks */ | |
20 | struct hfs_btree *hfs_btree_open(struct super_block *sb, u32 id) | |
21 | { | |
22 | struct hfs_btree *tree; | |
23 | struct hfs_btree_header_rec *head; | |
24 | struct address_space *mapping; | |
63525391 | 25 | struct inode *inode; |
1da177e4 LT |
26 | struct page *page; |
27 | unsigned int size; | |
28 | ||
f8314dc6 | 29 | tree = kzalloc(sizeof(*tree), GFP_KERNEL); |
1da177e4 LT |
30 | if (!tree) |
31 | return NULL; | |
1da177e4 | 32 | |
467c3d9c | 33 | mutex_init(&tree->tree_lock); |
1da177e4 | 34 | spin_lock_init(&tree->hash_lock); |
1da177e4 LT |
35 | tree->sb = sb; |
36 | tree->cnid = id; | |
63525391 DH |
37 | inode = hfsplus_iget(sb, id); |
38 | if (IS_ERR(inode)) | |
1da177e4 | 39 | goto free_tree; |
63525391 | 40 | tree->inode = inode; |
1da177e4 | 41 | |
ee527162 | 42 | if (!HFSPLUS_I(tree->inode)->first_blocks) { |
d6142673 | 43 | pr_err("invalid btree extent records (0 size)\n"); |
ee527162 JM |
44 | goto free_inode; |
45 | } | |
46 | ||
1da177e4 | 47 | mapping = tree->inode->i_mapping; |
090d2b18 | 48 | page = read_mapping_page(mapping, 0, NULL); |
1da177e4 | 49 | if (IS_ERR(page)) |
ee527162 | 50 | goto free_inode; |
1da177e4 LT |
51 | |
52 | /* Load the header */ | |
2753cc28 AS |
53 | head = (struct hfs_btree_header_rec *)(kmap(page) + |
54 | sizeof(struct hfs_bnode_desc)); | |
1da177e4 LT |
55 | tree->root = be32_to_cpu(head->root); |
56 | tree->leaf_count = be32_to_cpu(head->leaf_count); | |
57 | tree->leaf_head = be32_to_cpu(head->leaf_head); | |
58 | tree->leaf_tail = be32_to_cpu(head->leaf_tail); | |
59 | tree->node_count = be32_to_cpu(head->node_count); | |
60 | tree->free_nodes = be32_to_cpu(head->free_nodes); | |
61 | tree->attributes = be32_to_cpu(head->attributes); | |
62 | tree->node_size = be16_to_cpu(head->node_size); | |
63 | tree->max_key_len = be16_to_cpu(head->max_key_len); | |
64 | tree->depth = be16_to_cpu(head->depth); | |
65 | ||
9250f925 ES |
66 | /* Verify the tree and set the correct compare function */ |
67 | switch (id) { | |
68 | case HFSPLUS_EXT_CNID: | |
69 | if (tree->max_key_len != HFSPLUS_EXT_KEYLEN - sizeof(u16)) { | |
d6142673 | 70 | pr_err("invalid extent max_key_len %d\n", |
9250f925 ES |
71 | tree->max_key_len); |
72 | goto fail_page; | |
73 | } | |
13571a69 | 74 | if (tree->attributes & HFS_TREE_VARIDXKEYS) { |
d6142673 | 75 | pr_err("invalid extent btree flag\n"); |
13571a69 CH |
76 | goto fail_page; |
77 | } | |
78 | ||
2179d372 | 79 | tree->keycmp = hfsplus_ext_cmp_key; |
9250f925 ES |
80 | break; |
81 | case HFSPLUS_CAT_CNID: | |
82 | if (tree->max_key_len != HFSPLUS_CAT_KEYLEN - sizeof(u16)) { | |
d6142673 | 83 | pr_err("invalid catalog max_key_len %d\n", |
9250f925 ES |
84 | tree->max_key_len); |
85 | goto fail_page; | |
86 | } | |
13571a69 | 87 | if (!(tree->attributes & HFS_TREE_VARIDXKEYS)) { |
d6142673 | 88 | pr_err("invalid catalog btree flag\n"); |
13571a69 CH |
89 | goto fail_page; |
90 | } | |
9250f925 | 91 | |
84adede3 | 92 | if (test_bit(HFSPLUS_SB_HFSX, &HFSPLUS_SB(sb)->flags) && |
2179d372 DE |
93 | (head->key_type == HFSPLUS_KEY_BINARY)) |
94 | tree->keycmp = hfsplus_cat_bin_cmp_key; | |
d45bce8f | 95 | else { |
2179d372 | 96 | tree->keycmp = hfsplus_cat_case_cmp_key; |
84adede3 | 97 | set_bit(HFSPLUS_SB_CASEFOLD, &HFSPLUS_SB(sb)->flags); |
d45bce8f | 98 | } |
9250f925 | 99 | break; |
324ef39a VD |
100 | case HFSPLUS_ATTR_CNID: |
101 | if (tree->max_key_len != HFSPLUS_ATTR_KEYLEN - sizeof(u16)) { | |
d6142673 | 102 | pr_err("invalid attributes max_key_len %d\n", |
324ef39a VD |
103 | tree->max_key_len); |
104 | goto fail_page; | |
105 | } | |
106 | tree->keycmp = hfsplus_attr_bin_cmp_key; | |
107 | break; | |
9250f925 | 108 | default: |
d6142673 | 109 | pr_err("unknown B*Tree requested\n"); |
2179d372 DE |
110 | goto fail_page; |
111 | } | |
112 | ||
13571a69 | 113 | if (!(tree->attributes & HFS_TREE_BIGKEYS)) { |
d6142673 | 114 | pr_err("invalid btree flag\n"); |
13571a69 CH |
115 | goto fail_page; |
116 | } | |
117 | ||
1da177e4 | 118 | size = tree->node_size; |
e1b5c1d3 | 119 | if (!is_power_of_2(size)) |
1da177e4 LT |
120 | goto fail_page; |
121 | if (!tree->node_count) | |
122 | goto fail_page; | |
9250f925 | 123 | |
1da177e4 LT |
124 | tree->node_size_shift = ffs(size) - 1; |
125 | ||
2753cc28 AS |
126 | tree->pages_per_bnode = |
127 | (tree->node_size + PAGE_CACHE_SIZE - 1) >> | |
128 | PAGE_CACHE_SHIFT; | |
1da177e4 LT |
129 | |
130 | kunmap(page); | |
131 | page_cache_release(page); | |
132 | return tree; | |
133 | ||
134 | fail_page: | |
1da177e4 | 135 | page_cache_release(page); |
ee527162 | 136 | free_inode: |
9250f925 | 137 | tree->inode->i_mapping->a_ops = &hfsplus_aops; |
1da177e4 | 138 | iput(tree->inode); |
ee527162 | 139 | free_tree: |
1da177e4 LT |
140 | kfree(tree); |
141 | return NULL; | |
142 | } | |
143 | ||
144 | /* Release resources used by a btree */ | |
145 | void hfs_btree_close(struct hfs_btree *tree) | |
146 | { | |
147 | struct hfs_bnode *node; | |
148 | int i; | |
149 | ||
150 | if (!tree) | |
151 | return; | |
152 | ||
153 | for (i = 0; i < NODE_HASH_SIZE; i++) { | |
154 | while ((node = tree->node_hash[i])) { | |
155 | tree->node_hash[i] = node->next_hash; | |
156 | if (atomic_read(&node->refcnt)) | |
d6142673 | 157 | pr_crit("node %d:%d " |
2753cc28 AS |
158 | "still has %d user(s)!\n", |
159 | node->tree->cnid, node->this, | |
160 | atomic_read(&node->refcnt)); | |
1da177e4 LT |
161 | hfs_bnode_free(node); |
162 | tree->node_hash_cnt--; | |
163 | } | |
164 | } | |
165 | iput(tree->inode); | |
166 | kfree(tree); | |
167 | } | |
168 | ||
81cc7fad | 169 | int hfs_btree_write(struct hfs_btree *tree) |
1da177e4 LT |
170 | { |
171 | struct hfs_btree_header_rec *head; | |
172 | struct hfs_bnode *node; | |
173 | struct page *page; | |
174 | ||
175 | node = hfs_bnode_find(tree, 0); | |
176 | if (IS_ERR(node)) | |
177 | /* panic? */ | |
81cc7fad | 178 | return -EIO; |
1da177e4 LT |
179 | /* Load the header */ |
180 | page = node->page[0]; | |
2753cc28 AS |
181 | head = (struct hfs_btree_header_rec *)(kmap(page) + |
182 | sizeof(struct hfs_bnode_desc)); | |
1da177e4 LT |
183 | |
184 | head->root = cpu_to_be32(tree->root); | |
185 | head->leaf_count = cpu_to_be32(tree->leaf_count); | |
186 | head->leaf_head = cpu_to_be32(tree->leaf_head); | |
187 | head->leaf_tail = cpu_to_be32(tree->leaf_tail); | |
188 | head->node_count = cpu_to_be32(tree->node_count); | |
189 | head->free_nodes = cpu_to_be32(tree->free_nodes); | |
190 | head->attributes = cpu_to_be32(tree->attributes); | |
191 | head->depth = cpu_to_be16(tree->depth); | |
192 | ||
193 | kunmap(page); | |
194 | set_page_dirty(page); | |
195 | hfs_bnode_put(node); | |
81cc7fad | 196 | return 0; |
1da177e4 LT |
197 | } |
198 | ||
199 | static struct hfs_bnode *hfs_bmap_new_bmap(struct hfs_bnode *prev, u32 idx) | |
200 | { | |
201 | struct hfs_btree *tree = prev->tree; | |
202 | struct hfs_bnode *node; | |
203 | struct hfs_bnode_desc desc; | |
204 | __be32 cnid; | |
205 | ||
206 | node = hfs_bnode_create(tree, idx); | |
207 | if (IS_ERR(node)) | |
208 | return node; | |
209 | ||
210 | tree->free_nodes--; | |
211 | prev->next = idx; | |
212 | cnid = cpu_to_be32(idx); | |
213 | hfs_bnode_write(prev, &cnid, offsetof(struct hfs_bnode_desc, next), 4); | |
214 | ||
215 | node->type = HFS_NODE_MAP; | |
216 | node->num_recs = 1; | |
217 | hfs_bnode_clear(node, 0, tree->node_size); | |
218 | desc.next = 0; | |
219 | desc.prev = 0; | |
220 | desc.type = HFS_NODE_MAP; | |
221 | desc.height = 0; | |
222 | desc.num_recs = cpu_to_be16(1); | |
223 | desc.reserved = 0; | |
224 | hfs_bnode_write(node, &desc, 0, sizeof(desc)); | |
225 | hfs_bnode_write_u16(node, 14, 0x8000); | |
226 | hfs_bnode_write_u16(node, tree->node_size - 2, 14); | |
227 | hfs_bnode_write_u16(node, tree->node_size - 4, tree->node_size - 6); | |
228 | ||
229 | return node; | |
230 | } | |
231 | ||
232 | struct hfs_bnode *hfs_bmap_alloc(struct hfs_btree *tree) | |
233 | { | |
234 | struct hfs_bnode *node, *next_node; | |
235 | struct page **pagep; | |
236 | u32 nidx, idx; | |
487798df AM |
237 | unsigned off; |
238 | u16 off16; | |
239 | u16 len; | |
1da177e4 LT |
240 | u8 *data, byte, m; |
241 | int i; | |
242 | ||
243 | while (!tree->free_nodes) { | |
244 | struct inode *inode = tree->inode; | |
6af502de | 245 | struct hfsplus_inode_info *hip = HFSPLUS_I(inode); |
1da177e4 LT |
246 | u32 count; |
247 | int res; | |
248 | ||
249 | res = hfsplus_file_extend(inode); | |
250 | if (res) | |
251 | return ERR_PTR(res); | |
6af502de CH |
252 | hip->phys_size = inode->i_size = |
253 | (loff_t)hip->alloc_blocks << | |
dd73a01a | 254 | HFSPLUS_SB(tree->sb)->alloc_blksz_shift; |
6af502de CH |
255 | hip->fs_blocks = |
256 | hip->alloc_blocks << HFSPLUS_SB(tree->sb)->fs_shift; | |
1da177e4 LT |
257 | inode_set_bytes(inode, inode->i_size); |
258 | count = inode->i_size >> tree->node_size_shift; | |
259 | tree->free_nodes = count - tree->node_count; | |
260 | tree->node_count = count; | |
261 | } | |
262 | ||
263 | nidx = 0; | |
264 | node = hfs_bnode_find(tree, nidx); | |
265 | if (IS_ERR(node)) | |
266 | return node; | |
487798df AM |
267 | len = hfs_brec_lenoff(node, 2, &off16); |
268 | off = off16; | |
1da177e4 LT |
269 | |
270 | off += node->page_offset; | |
271 | pagep = node->page + (off >> PAGE_CACHE_SHIFT); | |
272 | data = kmap(*pagep); | |
273 | off &= ~PAGE_CACHE_MASK; | |
274 | idx = 0; | |
275 | ||
276 | for (;;) { | |
277 | while (len) { | |
278 | byte = data[off]; | |
279 | if (byte != 0xff) { | |
280 | for (m = 0x80, i = 0; i < 8; m >>= 1, i++) { | |
281 | if (!(byte & m)) { | |
282 | idx += i; | |
283 | data[off] |= m; | |
284 | set_page_dirty(*pagep); | |
285 | kunmap(*pagep); | |
286 | tree->free_nodes--; | |
287 | mark_inode_dirty(tree->inode); | |
288 | hfs_bnode_put(node); | |
2753cc28 AS |
289 | return hfs_bnode_create(tree, |
290 | idx); | |
1da177e4 LT |
291 | } |
292 | } | |
293 | } | |
294 | if (++off >= PAGE_CACHE_SIZE) { | |
295 | kunmap(*pagep); | |
296 | data = kmap(*++pagep); | |
297 | off = 0; | |
298 | } | |
299 | idx += 8; | |
300 | len--; | |
301 | } | |
302 | kunmap(*pagep); | |
303 | nidx = node->next; | |
304 | if (!nidx) { | |
c2b3e1f7 | 305 | hfs_dbg(BNODE_MOD, "create new bmap node\n"); |
1da177e4 LT |
306 | next_node = hfs_bmap_new_bmap(node, idx); |
307 | } else | |
308 | next_node = hfs_bnode_find(tree, nidx); | |
309 | hfs_bnode_put(node); | |
310 | if (IS_ERR(next_node)) | |
311 | return next_node; | |
312 | node = next_node; | |
313 | ||
487798df AM |
314 | len = hfs_brec_lenoff(node, 0, &off16); |
315 | off = off16; | |
1da177e4 LT |
316 | off += node->page_offset; |
317 | pagep = node->page + (off >> PAGE_CACHE_SHIFT); | |
318 | data = kmap(*pagep); | |
319 | off &= ~PAGE_CACHE_MASK; | |
320 | } | |
321 | } | |
322 | ||
323 | void hfs_bmap_free(struct hfs_bnode *node) | |
324 | { | |
325 | struct hfs_btree *tree; | |
326 | struct page *page; | |
327 | u16 off, len; | |
328 | u32 nidx; | |
329 | u8 *data, byte, m; | |
330 | ||
c2b3e1f7 | 331 | hfs_dbg(BNODE_MOD, "btree_free_node: %u\n", node->this); |
0bf3ba53 | 332 | BUG_ON(!node->this); |
1da177e4 LT |
333 | tree = node->tree; |
334 | nidx = node->this; | |
335 | node = hfs_bnode_find(tree, 0); | |
336 | if (IS_ERR(node)) | |
337 | return; | |
338 | len = hfs_brec_lenoff(node, 2, &off); | |
339 | while (nidx >= len * 8) { | |
340 | u32 i; | |
341 | ||
342 | nidx -= len * 8; | |
343 | i = node->next; | |
344 | hfs_bnode_put(node); | |
345 | if (!i) { | |
346 | /* panic */; | |
d6142673 | 347 | pr_crit("unable to free bnode %u. " |
2753cc28 AS |
348 | "bmap not found!\n", |
349 | node->this); | |
1da177e4 LT |
350 | return; |
351 | } | |
352 | node = hfs_bnode_find(tree, i); | |
353 | if (IS_ERR(node)) | |
354 | return; | |
355 | if (node->type != HFS_NODE_MAP) { | |
356 | /* panic */; | |
d6142673 | 357 | pr_crit("invalid bmap found! " |
2753cc28 AS |
358 | "(%u,%d)\n", |
359 | node->this, node->type); | |
1da177e4 LT |
360 | hfs_bnode_put(node); |
361 | return; | |
362 | } | |
363 | len = hfs_brec_lenoff(node, 0, &off); | |
364 | } | |
365 | off += node->page_offset + nidx / 8; | |
366 | page = node->page[off >> PAGE_CACHE_SHIFT]; | |
367 | data = kmap(page); | |
368 | off &= ~PAGE_CACHE_MASK; | |
369 | m = 1 << (~nidx & 7); | |
370 | byte = data[off]; | |
371 | if (!(byte & m)) { | |
d6142673 | 372 | pr_crit("trying to free free bnode " |
2753cc28 AS |
373 | "%u(%d)\n", |
374 | node->this, node->type); | |
1da177e4 LT |
375 | kunmap(page); |
376 | hfs_bnode_put(node); | |
377 | return; | |
378 | } | |
379 | data[off] = byte & ~m; | |
380 | set_page_dirty(page); | |
381 | kunmap(page); | |
382 | hfs_bnode_put(node); | |
383 | tree->free_nodes++; | |
384 | mark_inode_dirty(tree->inode); | |
385 | } |