blob: 22e4d4e329999c3ba9848036a3639bc194598f74 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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>
Vignesh Babu BMe1b5c1d2007-05-08 00:24:30 -070013#include <linux/log2.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014
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 */
20struct 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;
David Howells63525392008-02-07 00:15:40 -080025 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -070026 struct page *page;
27 unsigned int size;
28
Panagiotis Issarisf8314dc2006-09-27 01:49:37 -070029 tree = kzalloc(sizeof(*tree), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 if (!tree)
31 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
Thomas Gleixner467c3d92010-10-01 05:46:52 +020033 mutex_init(&tree->tree_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 spin_lock_init(&tree->hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070035 tree->sb = sb;
36 tree->cnid = id;
David Howells63525392008-02-07 00:15:40 -080037 inode = hfsplus_iget(sb, id);
38 if (IS_ERR(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 goto free_tree;
David Howells63525392008-02-07 00:15:40 -080040 tree->inode = inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Jeff Mahoneyee527162010-10-14 09:53:37 -040042 if (!HFSPLUS_I(tree->inode)->first_blocks) {
43 printk(KERN_ERR
44 "hfs: invalid btree extent records (0 size).\n");
45 goto free_inode;
46 }
47
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 mapping = tree->inode->i_mapping;
Pekka Enberg090d2b12006-06-23 02:05:08 -070049 page = read_mapping_page(mapping, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 if (IS_ERR(page))
Jeff Mahoneyee527162010-10-14 09:53:37 -040051 goto free_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
53 /* Load the header */
54 head = (struct hfs_btree_header_rec *)(kmap(page) + sizeof(struct hfs_bnode_desc));
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
Eric Sandeen9250f922010-10-14 09:53:48 -040066 /* 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)) {
70 printk(KERN_ERR "hfs: invalid extent max_key_len %d\n",
71 tree->max_key_len);
72 goto fail_page;
73 }
Christoph Hellwig13571a62010-10-14 09:54:23 -040074 if (tree->attributes & HFS_TREE_VARIDXKEYS) {
75 printk(KERN_ERR "hfs: invalid extent btree flag\n");
76 goto fail_page;
77 }
78
David Elliott2179d372006-01-18 17:43:08 -080079 tree->keycmp = hfsplus_ext_cmp_key;
Eric Sandeen9250f922010-10-14 09:53:48 -040080 break;
81 case HFSPLUS_CAT_CNID:
82 if (tree->max_key_len != HFSPLUS_CAT_KEYLEN - sizeof(u16)) {
83 printk(KERN_ERR "hfs: invalid catalog max_key_len %d\n",
84 tree->max_key_len);
85 goto fail_page;
86 }
Christoph Hellwig13571a62010-10-14 09:54:23 -040087 if (!(tree->attributes & HFS_TREE_VARIDXKEYS)) {
88 printk(KERN_ERR "hfs: invalid catalog btree flag\n");
89 goto fail_page;
90 }
Eric Sandeen9250f922010-10-14 09:53:48 -040091
Christoph Hellwig84adede2010-10-01 05:45:20 +020092 if (test_bit(HFSPLUS_SB_HFSX, &HFSPLUS_SB(sb)->flags) &&
David Elliott2179d372006-01-18 17:43:08 -080093 (head->key_type == HFSPLUS_KEY_BINARY))
94 tree->keycmp = hfsplus_cat_bin_cmp_key;
Duane Griffind45bce82007-07-15 23:41:23 -070095 else {
David Elliott2179d372006-01-18 17:43:08 -080096 tree->keycmp = hfsplus_cat_case_cmp_key;
Christoph Hellwig84adede2010-10-01 05:45:20 +020097 set_bit(HFSPLUS_SB_CASEFOLD, &HFSPLUS_SB(sb)->flags);
Duane Griffind45bce82007-07-15 23:41:23 -070098 }
Eric Sandeen9250f922010-10-14 09:53:48 -040099 break;
100 default:
David Elliott2179d372006-01-18 17:43:08 -0800101 printk(KERN_ERR "hfs: unknown B*Tree requested\n");
102 goto fail_page;
103 }
104
Christoph Hellwig13571a62010-10-14 09:54:23 -0400105 if (!(tree->attributes & HFS_TREE_BIGKEYS)) {
106 printk(KERN_ERR "hfs: invalid btree flag\n");
107 goto fail_page;
108 }
109
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 size = tree->node_size;
Vignesh Babu BMe1b5c1d2007-05-08 00:24:30 -0700111 if (!is_power_of_2(size))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 goto fail_page;
113 if (!tree->node_count)
114 goto fail_page;
Eric Sandeen9250f922010-10-14 09:53:48 -0400115
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 tree->node_size_shift = ffs(size) - 1;
117
118 tree->pages_per_bnode = (tree->node_size + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
119
120 kunmap(page);
121 page_cache_release(page);
122 return tree;
123
124 fail_page:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 page_cache_release(page);
Jeff Mahoneyee527162010-10-14 09:53:37 -0400126 free_inode:
Eric Sandeen9250f922010-10-14 09:53:48 -0400127 tree->inode->i_mapping->a_ops = &hfsplus_aops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 iput(tree->inode);
Jeff Mahoneyee527162010-10-14 09:53:37 -0400129 free_tree:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 kfree(tree);
131 return NULL;
132}
133
134/* Release resources used by a btree */
135void hfs_btree_close(struct hfs_btree *tree)
136{
137 struct hfs_bnode *node;
138 int i;
139
140 if (!tree)
141 return;
142
143 for (i = 0; i < NODE_HASH_SIZE; i++) {
144 while ((node = tree->node_hash[i])) {
145 tree->node_hash[i] = node->next_hash;
146 if (atomic_read(&node->refcnt))
Roman Zippel634725a2006-01-18 17:43:05 -0800147 printk(KERN_CRIT "hfs: node %d:%d still has %d user(s)!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 node->tree->cnid, node->this, atomic_read(&node->refcnt));
149 hfs_bnode_free(node);
150 tree->node_hash_cnt--;
151 }
152 }
153 iput(tree->inode);
154 kfree(tree);
155}
156
157void hfs_btree_write(struct hfs_btree *tree)
158{
159 struct hfs_btree_header_rec *head;
160 struct hfs_bnode *node;
161 struct page *page;
162
163 node = hfs_bnode_find(tree, 0);
164 if (IS_ERR(node))
165 /* panic? */
166 return;
167 /* Load the header */
168 page = node->page[0];
169 head = (struct hfs_btree_header_rec *)(kmap(page) + sizeof(struct hfs_bnode_desc));
170
171 head->root = cpu_to_be32(tree->root);
172 head->leaf_count = cpu_to_be32(tree->leaf_count);
173 head->leaf_head = cpu_to_be32(tree->leaf_head);
174 head->leaf_tail = cpu_to_be32(tree->leaf_tail);
175 head->node_count = cpu_to_be32(tree->node_count);
176 head->free_nodes = cpu_to_be32(tree->free_nodes);
177 head->attributes = cpu_to_be32(tree->attributes);
178 head->depth = cpu_to_be16(tree->depth);
179
180 kunmap(page);
181 set_page_dirty(page);
182 hfs_bnode_put(node);
183}
184
185static struct hfs_bnode *hfs_bmap_new_bmap(struct hfs_bnode *prev, u32 idx)
186{
187 struct hfs_btree *tree = prev->tree;
188 struct hfs_bnode *node;
189 struct hfs_bnode_desc desc;
190 __be32 cnid;
191
192 node = hfs_bnode_create(tree, idx);
193 if (IS_ERR(node))
194 return node;
195
196 tree->free_nodes--;
197 prev->next = idx;
198 cnid = cpu_to_be32(idx);
199 hfs_bnode_write(prev, &cnid, offsetof(struct hfs_bnode_desc, next), 4);
200
201 node->type = HFS_NODE_MAP;
202 node->num_recs = 1;
203 hfs_bnode_clear(node, 0, tree->node_size);
204 desc.next = 0;
205 desc.prev = 0;
206 desc.type = HFS_NODE_MAP;
207 desc.height = 0;
208 desc.num_recs = cpu_to_be16(1);
209 desc.reserved = 0;
210 hfs_bnode_write(node, &desc, 0, sizeof(desc));
211 hfs_bnode_write_u16(node, 14, 0x8000);
212 hfs_bnode_write_u16(node, tree->node_size - 2, 14);
213 hfs_bnode_write_u16(node, tree->node_size - 4, tree->node_size - 6);
214
215 return node;
216}
217
218struct hfs_bnode *hfs_bmap_alloc(struct hfs_btree *tree)
219{
220 struct hfs_bnode *node, *next_node;
221 struct page **pagep;
222 u32 nidx, idx;
Andrew Morton487798d2008-04-30 00:54:54 -0700223 unsigned off;
224 u16 off16;
225 u16 len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 u8 *data, byte, m;
227 int i;
228
229 while (!tree->free_nodes) {
230 struct inode *inode = tree->inode;
Christoph Hellwig6af502d2010-10-01 05:43:31 +0200231 struct hfsplus_inode_info *hip = HFSPLUS_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 u32 count;
233 int res;
234
235 res = hfsplus_file_extend(inode);
236 if (res)
237 return ERR_PTR(res);
Christoph Hellwig6af502d2010-10-01 05:43:31 +0200238 hip->phys_size = inode->i_size =
239 (loff_t)hip->alloc_blocks <<
Christoph Hellwigdd73a012010-10-01 05:42:59 +0200240 HFSPLUS_SB(tree->sb)->alloc_blksz_shift;
Christoph Hellwig6af502d2010-10-01 05:43:31 +0200241 hip->fs_blocks =
242 hip->alloc_blocks << HFSPLUS_SB(tree->sb)->fs_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 inode_set_bytes(inode, inode->i_size);
244 count = inode->i_size >> tree->node_size_shift;
245 tree->free_nodes = count - tree->node_count;
246 tree->node_count = count;
247 }
248
249 nidx = 0;
250 node = hfs_bnode_find(tree, nidx);
251 if (IS_ERR(node))
252 return node;
Andrew Morton487798d2008-04-30 00:54:54 -0700253 len = hfs_brec_lenoff(node, 2, &off16);
254 off = off16;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
256 off += node->page_offset;
257 pagep = node->page + (off >> PAGE_CACHE_SHIFT);
258 data = kmap(*pagep);
259 off &= ~PAGE_CACHE_MASK;
260 idx = 0;
261
262 for (;;) {
263 while (len) {
264 byte = data[off];
265 if (byte != 0xff) {
266 for (m = 0x80, i = 0; i < 8; m >>= 1, i++) {
267 if (!(byte & m)) {
268 idx += i;
269 data[off] |= m;
270 set_page_dirty(*pagep);
271 kunmap(*pagep);
272 tree->free_nodes--;
273 mark_inode_dirty(tree->inode);
274 hfs_bnode_put(node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 return hfs_bnode_create(tree, idx);
276 }
277 }
278 }
279 if (++off >= PAGE_CACHE_SIZE) {
280 kunmap(*pagep);
281 data = kmap(*++pagep);
282 off = 0;
283 }
284 idx += 8;
285 len--;
286 }
287 kunmap(*pagep);
288 nidx = node->next;
289 if (!nidx) {
Roman Zippel634725a2006-01-18 17:43:05 -0800290 printk(KERN_DEBUG "hfs: create new bmap node...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 next_node = hfs_bmap_new_bmap(node, idx);
292 } else
293 next_node = hfs_bnode_find(tree, nidx);
294 hfs_bnode_put(node);
295 if (IS_ERR(next_node))
296 return next_node;
297 node = next_node;
298
Andrew Morton487798d2008-04-30 00:54:54 -0700299 len = hfs_brec_lenoff(node, 0, &off16);
300 off = off16;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 off += node->page_offset;
302 pagep = node->page + (off >> PAGE_CACHE_SHIFT);
303 data = kmap(*pagep);
304 off &= ~PAGE_CACHE_MASK;
305 }
306}
307
308void hfs_bmap_free(struct hfs_bnode *node)
309{
310 struct hfs_btree *tree;
311 struct page *page;
312 u16 off, len;
313 u32 nidx;
314 u8 *data, byte, m;
315
316 dprint(DBG_BNODE_MOD, "btree_free_node: %u\n", node->this);
Eric Sesterhenn0bf3ba52006-04-01 01:14:43 +0200317 BUG_ON(!node->this);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 tree = node->tree;
319 nidx = node->this;
320 node = hfs_bnode_find(tree, 0);
321 if (IS_ERR(node))
322 return;
323 len = hfs_brec_lenoff(node, 2, &off);
324 while (nidx >= len * 8) {
325 u32 i;
326
327 nidx -= len * 8;
328 i = node->next;
329 hfs_bnode_put(node);
330 if (!i) {
331 /* panic */;
Roman Zippel634725a2006-01-18 17:43:05 -0800332 printk(KERN_CRIT "hfs: unable to free bnode %u. bmap not found!\n", node->this);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 return;
334 }
335 node = hfs_bnode_find(tree, i);
336 if (IS_ERR(node))
337 return;
338 if (node->type != HFS_NODE_MAP) {
339 /* panic */;
Roman Zippel634725a2006-01-18 17:43:05 -0800340 printk(KERN_CRIT "hfs: invalid bmap found! (%u,%d)\n", node->this, node->type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 hfs_bnode_put(node);
342 return;
343 }
344 len = hfs_brec_lenoff(node, 0, &off);
345 }
346 off += node->page_offset + nidx / 8;
347 page = node->page[off >> PAGE_CACHE_SHIFT];
348 data = kmap(page);
349 off &= ~PAGE_CACHE_MASK;
350 m = 1 << (~nidx & 7);
351 byte = data[off];
352 if (!(byte & m)) {
Roman Zippel634725a2006-01-18 17:43:05 -0800353 printk(KERN_CRIT "hfs: trying to free free bnode %u(%d)\n", node->this, node->type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 kunmap(page);
355 hfs_bnode_put(node);
356 return;
357 }
358 data[off] = byte & ~m;
359 set_page_dirty(page);
360 kunmap(page);
361 hfs_bnode_put(node);
362 tree->free_nodes++;
363 mark_inode_dirty(tree->inode);
364}