blob: 82caff215e084fc9a0344b05cd750be5ec4ad084 [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 }
David Elliott2179d372006-01-18 17:43:08 -080074 tree->keycmp = hfsplus_ext_cmp_key;
Eric Sandeen9250f922010-10-14 09:53:48 -040075 break;
76 case HFSPLUS_CAT_CNID:
77 if (tree->max_key_len != HFSPLUS_CAT_KEYLEN - sizeof(u16)) {
78 printk(KERN_ERR "hfs: invalid catalog max_key_len %d\n",
79 tree->max_key_len);
80 goto fail_page;
81 }
82
Christoph Hellwig84adede2010-10-01 05:45:20 +020083 if (test_bit(HFSPLUS_SB_HFSX, &HFSPLUS_SB(sb)->flags) &&
David Elliott2179d372006-01-18 17:43:08 -080084 (head->key_type == HFSPLUS_KEY_BINARY))
85 tree->keycmp = hfsplus_cat_bin_cmp_key;
Duane Griffind45bce82007-07-15 23:41:23 -070086 else {
David Elliott2179d372006-01-18 17:43:08 -080087 tree->keycmp = hfsplus_cat_case_cmp_key;
Christoph Hellwig84adede2010-10-01 05:45:20 +020088 set_bit(HFSPLUS_SB_CASEFOLD, &HFSPLUS_SB(sb)->flags);
Duane Griffind45bce82007-07-15 23:41:23 -070089 }
Eric Sandeen9250f922010-10-14 09:53:48 -040090 break;
91 default:
David Elliott2179d372006-01-18 17:43:08 -080092 printk(KERN_ERR "hfs: unknown B*Tree requested\n");
93 goto fail_page;
94 }
95
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 size = tree->node_size;
Vignesh Babu BMe1b5c1d2007-05-08 00:24:30 -070097 if (!is_power_of_2(size))
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 goto fail_page;
99 if (!tree->node_count)
100 goto fail_page;
Eric Sandeen9250f922010-10-14 09:53:48 -0400101
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 tree->node_size_shift = ffs(size) - 1;
103
104 tree->pages_per_bnode = (tree->node_size + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
105
106 kunmap(page);
107 page_cache_release(page);
108 return tree;
109
110 fail_page:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 page_cache_release(page);
Jeff Mahoneyee527162010-10-14 09:53:37 -0400112 free_inode:
Eric Sandeen9250f922010-10-14 09:53:48 -0400113 tree->inode->i_mapping->a_ops = &hfsplus_aops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 iput(tree->inode);
Jeff Mahoneyee527162010-10-14 09:53:37 -0400115 free_tree:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 kfree(tree);
117 return NULL;
118}
119
120/* Release resources used by a btree */
121void hfs_btree_close(struct hfs_btree *tree)
122{
123 struct hfs_bnode *node;
124 int i;
125
126 if (!tree)
127 return;
128
129 for (i = 0; i < NODE_HASH_SIZE; i++) {
130 while ((node = tree->node_hash[i])) {
131 tree->node_hash[i] = node->next_hash;
132 if (atomic_read(&node->refcnt))
Roman Zippel634725a2006-01-18 17:43:05 -0800133 printk(KERN_CRIT "hfs: node %d:%d still has %d user(s)!\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 node->tree->cnid, node->this, atomic_read(&node->refcnt));
135 hfs_bnode_free(node);
136 tree->node_hash_cnt--;
137 }
138 }
139 iput(tree->inode);
140 kfree(tree);
141}
142
143void hfs_btree_write(struct hfs_btree *tree)
144{
145 struct hfs_btree_header_rec *head;
146 struct hfs_bnode *node;
147 struct page *page;
148
149 node = hfs_bnode_find(tree, 0);
150 if (IS_ERR(node))
151 /* panic? */
152 return;
153 /* Load the header */
154 page = node->page[0];
155 head = (struct hfs_btree_header_rec *)(kmap(page) + sizeof(struct hfs_bnode_desc));
156
157 head->root = cpu_to_be32(tree->root);
158 head->leaf_count = cpu_to_be32(tree->leaf_count);
159 head->leaf_head = cpu_to_be32(tree->leaf_head);
160 head->leaf_tail = cpu_to_be32(tree->leaf_tail);
161 head->node_count = cpu_to_be32(tree->node_count);
162 head->free_nodes = cpu_to_be32(tree->free_nodes);
163 head->attributes = cpu_to_be32(tree->attributes);
164 head->depth = cpu_to_be16(tree->depth);
165
166 kunmap(page);
167 set_page_dirty(page);
168 hfs_bnode_put(node);
169}
170
171static struct hfs_bnode *hfs_bmap_new_bmap(struct hfs_bnode *prev, u32 idx)
172{
173 struct hfs_btree *tree = prev->tree;
174 struct hfs_bnode *node;
175 struct hfs_bnode_desc desc;
176 __be32 cnid;
177
178 node = hfs_bnode_create(tree, idx);
179 if (IS_ERR(node))
180 return node;
181
182 tree->free_nodes--;
183 prev->next = idx;
184 cnid = cpu_to_be32(idx);
185 hfs_bnode_write(prev, &cnid, offsetof(struct hfs_bnode_desc, next), 4);
186
187 node->type = HFS_NODE_MAP;
188 node->num_recs = 1;
189 hfs_bnode_clear(node, 0, tree->node_size);
190 desc.next = 0;
191 desc.prev = 0;
192 desc.type = HFS_NODE_MAP;
193 desc.height = 0;
194 desc.num_recs = cpu_to_be16(1);
195 desc.reserved = 0;
196 hfs_bnode_write(node, &desc, 0, sizeof(desc));
197 hfs_bnode_write_u16(node, 14, 0x8000);
198 hfs_bnode_write_u16(node, tree->node_size - 2, 14);
199 hfs_bnode_write_u16(node, tree->node_size - 4, tree->node_size - 6);
200
201 return node;
202}
203
204struct hfs_bnode *hfs_bmap_alloc(struct hfs_btree *tree)
205{
206 struct hfs_bnode *node, *next_node;
207 struct page **pagep;
208 u32 nidx, idx;
Andrew Morton487798d2008-04-30 00:54:54 -0700209 unsigned off;
210 u16 off16;
211 u16 len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 u8 *data, byte, m;
213 int i;
214
215 while (!tree->free_nodes) {
216 struct inode *inode = tree->inode;
Christoph Hellwig6af502d2010-10-01 05:43:31 +0200217 struct hfsplus_inode_info *hip = HFSPLUS_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 u32 count;
219 int res;
220
221 res = hfsplus_file_extend(inode);
222 if (res)
223 return ERR_PTR(res);
Christoph Hellwig6af502d2010-10-01 05:43:31 +0200224 hip->phys_size = inode->i_size =
225 (loff_t)hip->alloc_blocks <<
Christoph Hellwigdd73a012010-10-01 05:42:59 +0200226 HFSPLUS_SB(tree->sb)->alloc_blksz_shift;
Christoph Hellwig6af502d2010-10-01 05:43:31 +0200227 hip->fs_blocks =
228 hip->alloc_blocks << HFSPLUS_SB(tree->sb)->fs_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 inode_set_bytes(inode, inode->i_size);
230 count = inode->i_size >> tree->node_size_shift;
231 tree->free_nodes = count - tree->node_count;
232 tree->node_count = count;
233 }
234
235 nidx = 0;
236 node = hfs_bnode_find(tree, nidx);
237 if (IS_ERR(node))
238 return node;
Andrew Morton487798d2008-04-30 00:54:54 -0700239 len = hfs_brec_lenoff(node, 2, &off16);
240 off = off16;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
242 off += node->page_offset;
243 pagep = node->page + (off >> PAGE_CACHE_SHIFT);
244 data = kmap(*pagep);
245 off &= ~PAGE_CACHE_MASK;
246 idx = 0;
247
248 for (;;) {
249 while (len) {
250 byte = data[off];
251 if (byte != 0xff) {
252 for (m = 0x80, i = 0; i < 8; m >>= 1, i++) {
253 if (!(byte & m)) {
254 idx += i;
255 data[off] |= m;
256 set_page_dirty(*pagep);
257 kunmap(*pagep);
258 tree->free_nodes--;
259 mark_inode_dirty(tree->inode);
260 hfs_bnode_put(node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 return hfs_bnode_create(tree, idx);
262 }
263 }
264 }
265 if (++off >= PAGE_CACHE_SIZE) {
266 kunmap(*pagep);
267 data = kmap(*++pagep);
268 off = 0;
269 }
270 idx += 8;
271 len--;
272 }
273 kunmap(*pagep);
274 nidx = node->next;
275 if (!nidx) {
Roman Zippel634725a2006-01-18 17:43:05 -0800276 printk(KERN_DEBUG "hfs: create new bmap node...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 next_node = hfs_bmap_new_bmap(node, idx);
278 } else
279 next_node = hfs_bnode_find(tree, nidx);
280 hfs_bnode_put(node);
281 if (IS_ERR(next_node))
282 return next_node;
283 node = next_node;
284
Andrew Morton487798d2008-04-30 00:54:54 -0700285 len = hfs_brec_lenoff(node, 0, &off16);
286 off = off16;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 off += node->page_offset;
288 pagep = node->page + (off >> PAGE_CACHE_SHIFT);
289 data = kmap(*pagep);
290 off &= ~PAGE_CACHE_MASK;
291 }
292}
293
294void hfs_bmap_free(struct hfs_bnode *node)
295{
296 struct hfs_btree *tree;
297 struct page *page;
298 u16 off, len;
299 u32 nidx;
300 u8 *data, byte, m;
301
302 dprint(DBG_BNODE_MOD, "btree_free_node: %u\n", node->this);
Eric Sesterhenn0bf3ba52006-04-01 01:14:43 +0200303 BUG_ON(!node->this);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 tree = node->tree;
305 nidx = node->this;
306 node = hfs_bnode_find(tree, 0);
307 if (IS_ERR(node))
308 return;
309 len = hfs_brec_lenoff(node, 2, &off);
310 while (nidx >= len * 8) {
311 u32 i;
312
313 nidx -= len * 8;
314 i = node->next;
315 hfs_bnode_put(node);
316 if (!i) {
317 /* panic */;
Roman Zippel634725a2006-01-18 17:43:05 -0800318 printk(KERN_CRIT "hfs: unable to free bnode %u. bmap not found!\n", node->this);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 return;
320 }
321 node = hfs_bnode_find(tree, i);
322 if (IS_ERR(node))
323 return;
324 if (node->type != HFS_NODE_MAP) {
325 /* panic */;
Roman Zippel634725a2006-01-18 17:43:05 -0800326 printk(KERN_CRIT "hfs: invalid bmap found! (%u,%d)\n", node->this, node->type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 hfs_bnode_put(node);
328 return;
329 }
330 len = hfs_brec_lenoff(node, 0, &off);
331 }
332 off += node->page_offset + nidx / 8;
333 page = node->page[off >> PAGE_CACHE_SHIFT];
334 data = kmap(page);
335 off &= ~PAGE_CACHE_MASK;
336 m = 1 << (~nidx & 7);
337 byte = data[off];
338 if (!(byte & m)) {
Roman Zippel634725a2006-01-18 17:43:05 -0800339 printk(KERN_CRIT "hfs: trying to free free bnode %u(%d)\n", node->this, node->type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 kunmap(page);
341 hfs_bnode_put(node);
342 return;
343 }
344 data[off] = byte & ~m;
345 set_page_dirty(page);
346 kunmap(page);
347 hfs_bnode_put(node);
348 tree->free_nodes++;
349 mark_inode_dirty(tree->inode);
350}