blob: 685d07d0ed18845cceb092cc564142664580929c [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 */
Anton Salikhmetov2753cc22010-12-16 18:08:38 +020054 head = (struct hfs_btree_header_rec *)(kmap(page) +
55 sizeof(struct hfs_bnode_desc));
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 tree->root = be32_to_cpu(head->root);
57 tree->leaf_count = be32_to_cpu(head->leaf_count);
58 tree->leaf_head = be32_to_cpu(head->leaf_head);
59 tree->leaf_tail = be32_to_cpu(head->leaf_tail);
60 tree->node_count = be32_to_cpu(head->node_count);
61 tree->free_nodes = be32_to_cpu(head->free_nodes);
62 tree->attributes = be32_to_cpu(head->attributes);
63 tree->node_size = be16_to_cpu(head->node_size);
64 tree->max_key_len = be16_to_cpu(head->max_key_len);
65 tree->depth = be16_to_cpu(head->depth);
66
Eric Sandeen9250f922010-10-14 09:53:48 -040067 /* Verify the tree and set the correct compare function */
68 switch (id) {
69 case HFSPLUS_EXT_CNID:
70 if (tree->max_key_len != HFSPLUS_EXT_KEYLEN - sizeof(u16)) {
71 printk(KERN_ERR "hfs: invalid extent max_key_len %d\n",
72 tree->max_key_len);
73 goto fail_page;
74 }
Christoph Hellwig13571a62010-10-14 09:54:23 -040075 if (tree->attributes & HFS_TREE_VARIDXKEYS) {
76 printk(KERN_ERR "hfs: invalid extent btree flag\n");
77 goto fail_page;
78 }
79
David Elliott2179d372006-01-18 17:43:08 -080080 tree->keycmp = hfsplus_ext_cmp_key;
Eric Sandeen9250f922010-10-14 09:53:48 -040081 break;
82 case HFSPLUS_CAT_CNID:
83 if (tree->max_key_len != HFSPLUS_CAT_KEYLEN - sizeof(u16)) {
84 printk(KERN_ERR "hfs: invalid catalog max_key_len %d\n",
85 tree->max_key_len);
86 goto fail_page;
87 }
Christoph Hellwig13571a62010-10-14 09:54:23 -040088 if (!(tree->attributes & HFS_TREE_VARIDXKEYS)) {
89 printk(KERN_ERR "hfs: invalid catalog btree flag\n");
90 goto fail_page;
91 }
Eric Sandeen9250f922010-10-14 09:53:48 -040092
Christoph Hellwig84adede2010-10-01 05:45:20 +020093 if (test_bit(HFSPLUS_SB_HFSX, &HFSPLUS_SB(sb)->flags) &&
David Elliott2179d372006-01-18 17:43:08 -080094 (head->key_type == HFSPLUS_KEY_BINARY))
95 tree->keycmp = hfsplus_cat_bin_cmp_key;
Duane Griffind45bce82007-07-15 23:41:23 -070096 else {
David Elliott2179d372006-01-18 17:43:08 -080097 tree->keycmp = hfsplus_cat_case_cmp_key;
Christoph Hellwig84adede2010-10-01 05:45:20 +020098 set_bit(HFSPLUS_SB_CASEFOLD, &HFSPLUS_SB(sb)->flags);
Duane Griffind45bce82007-07-15 23:41:23 -070099 }
Eric Sandeen9250f922010-10-14 09:53:48 -0400100 break;
101 default:
David Elliott2179d372006-01-18 17:43:08 -0800102 printk(KERN_ERR "hfs: unknown B*Tree requested\n");
103 goto fail_page;
104 }
105
Christoph Hellwig13571a62010-10-14 09:54:23 -0400106 if (!(tree->attributes & HFS_TREE_BIGKEYS)) {
107 printk(KERN_ERR "hfs: invalid btree flag\n");
108 goto fail_page;
109 }
110
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 size = tree->node_size;
Vignesh Babu BMe1b5c1d2007-05-08 00:24:30 -0700112 if (!is_power_of_2(size))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 goto fail_page;
114 if (!tree->node_count)
115 goto fail_page;
Eric Sandeen9250f922010-10-14 09:53:48 -0400116
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 tree->node_size_shift = ffs(size) - 1;
118
Anton Salikhmetov2753cc22010-12-16 18:08:38 +0200119 tree->pages_per_bnode =
120 (tree->node_size + PAGE_CACHE_SIZE - 1) >>
121 PAGE_CACHE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
123 kunmap(page);
124 page_cache_release(page);
125 return tree;
126
127 fail_page:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 page_cache_release(page);
Jeff Mahoneyee527162010-10-14 09:53:37 -0400129 free_inode:
Eric Sandeen9250f922010-10-14 09:53:48 -0400130 tree->inode->i_mapping->a_ops = &hfsplus_aops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 iput(tree->inode);
Jeff Mahoneyee527162010-10-14 09:53:37 -0400132 free_tree:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 kfree(tree);
134 return NULL;
135}
136
137/* Release resources used by a btree */
138void hfs_btree_close(struct hfs_btree *tree)
139{
140 struct hfs_bnode *node;
141 int i;
142
143 if (!tree)
144 return;
145
146 for (i = 0; i < NODE_HASH_SIZE; i++) {
147 while ((node = tree->node_hash[i])) {
148 tree->node_hash[i] = node->next_hash;
149 if (atomic_read(&node->refcnt))
Anton Salikhmetov2753cc22010-12-16 18:08:38 +0200150 printk(KERN_CRIT "hfs: node %d:%d "
151 "still has %d user(s)!\n",
152 node->tree->cnid, node->this,
153 atomic_read(&node->refcnt));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 hfs_bnode_free(node);
155 tree->node_hash_cnt--;
156 }
157 }
158 iput(tree->inode);
159 kfree(tree);
160}
161
Vyacheslav Dubeyko81cc7fa2012-12-20 15:05:28 -0800162int hfs_btree_write(struct hfs_btree *tree)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163{
164 struct hfs_btree_header_rec *head;
165 struct hfs_bnode *node;
166 struct page *page;
167
168 node = hfs_bnode_find(tree, 0);
169 if (IS_ERR(node))
170 /* panic? */
Vyacheslav Dubeyko81cc7fa2012-12-20 15:05:28 -0800171 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 /* Load the header */
173 page = node->page[0];
Anton Salikhmetov2753cc22010-12-16 18:08:38 +0200174 head = (struct hfs_btree_header_rec *)(kmap(page) +
175 sizeof(struct hfs_bnode_desc));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
177 head->root = cpu_to_be32(tree->root);
178 head->leaf_count = cpu_to_be32(tree->leaf_count);
179 head->leaf_head = cpu_to_be32(tree->leaf_head);
180 head->leaf_tail = cpu_to_be32(tree->leaf_tail);
181 head->node_count = cpu_to_be32(tree->node_count);
182 head->free_nodes = cpu_to_be32(tree->free_nodes);
183 head->attributes = cpu_to_be32(tree->attributes);
184 head->depth = cpu_to_be16(tree->depth);
185
186 kunmap(page);
187 set_page_dirty(page);
188 hfs_bnode_put(node);
Vyacheslav Dubeyko81cc7fa2012-12-20 15:05:28 -0800189 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190}
191
192static struct hfs_bnode *hfs_bmap_new_bmap(struct hfs_bnode *prev, u32 idx)
193{
194 struct hfs_btree *tree = prev->tree;
195 struct hfs_bnode *node;
196 struct hfs_bnode_desc desc;
197 __be32 cnid;
198
199 node = hfs_bnode_create(tree, idx);
200 if (IS_ERR(node))
201 return node;
202
203 tree->free_nodes--;
204 prev->next = idx;
205 cnid = cpu_to_be32(idx);
206 hfs_bnode_write(prev, &cnid, offsetof(struct hfs_bnode_desc, next), 4);
207
208 node->type = HFS_NODE_MAP;
209 node->num_recs = 1;
210 hfs_bnode_clear(node, 0, tree->node_size);
211 desc.next = 0;
212 desc.prev = 0;
213 desc.type = HFS_NODE_MAP;
214 desc.height = 0;
215 desc.num_recs = cpu_to_be16(1);
216 desc.reserved = 0;
217 hfs_bnode_write(node, &desc, 0, sizeof(desc));
218 hfs_bnode_write_u16(node, 14, 0x8000);
219 hfs_bnode_write_u16(node, tree->node_size - 2, 14);
220 hfs_bnode_write_u16(node, tree->node_size - 4, tree->node_size - 6);
221
222 return node;
223}
224
225struct hfs_bnode *hfs_bmap_alloc(struct hfs_btree *tree)
226{
227 struct hfs_bnode *node, *next_node;
228 struct page **pagep;
229 u32 nidx, idx;
Andrew Morton487798d2008-04-30 00:54:54 -0700230 unsigned off;
231 u16 off16;
232 u16 len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 u8 *data, byte, m;
234 int i;
235
236 while (!tree->free_nodes) {
237 struct inode *inode = tree->inode;
Christoph Hellwig6af502d2010-10-01 05:43:31 +0200238 struct hfsplus_inode_info *hip = HFSPLUS_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 u32 count;
240 int res;
241
242 res = hfsplus_file_extend(inode);
243 if (res)
244 return ERR_PTR(res);
Christoph Hellwig6af502d2010-10-01 05:43:31 +0200245 hip->phys_size = inode->i_size =
246 (loff_t)hip->alloc_blocks <<
Christoph Hellwigdd73a012010-10-01 05:42:59 +0200247 HFSPLUS_SB(tree->sb)->alloc_blksz_shift;
Christoph Hellwig6af502d2010-10-01 05:43:31 +0200248 hip->fs_blocks =
249 hip->alloc_blocks << HFSPLUS_SB(tree->sb)->fs_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 inode_set_bytes(inode, inode->i_size);
251 count = inode->i_size >> tree->node_size_shift;
252 tree->free_nodes = count - tree->node_count;
253 tree->node_count = count;
254 }
255
256 nidx = 0;
257 node = hfs_bnode_find(tree, nidx);
258 if (IS_ERR(node))
259 return node;
Andrew Morton487798d2008-04-30 00:54:54 -0700260 len = hfs_brec_lenoff(node, 2, &off16);
261 off = off16;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
263 off += node->page_offset;
264 pagep = node->page + (off >> PAGE_CACHE_SHIFT);
265 data = kmap(*pagep);
266 off &= ~PAGE_CACHE_MASK;
267 idx = 0;
268
269 for (;;) {
270 while (len) {
271 byte = data[off];
272 if (byte != 0xff) {
273 for (m = 0x80, i = 0; i < 8; m >>= 1, i++) {
274 if (!(byte & m)) {
275 idx += i;
276 data[off] |= m;
277 set_page_dirty(*pagep);
278 kunmap(*pagep);
279 tree->free_nodes--;
280 mark_inode_dirty(tree->inode);
281 hfs_bnode_put(node);
Anton Salikhmetov2753cc22010-12-16 18:08:38 +0200282 return hfs_bnode_create(tree,
283 idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 }
285 }
286 }
287 if (++off >= PAGE_CACHE_SIZE) {
288 kunmap(*pagep);
289 data = kmap(*++pagep);
290 off = 0;
291 }
292 idx += 8;
293 len--;
294 }
295 kunmap(*pagep);
296 nidx = node->next;
297 if (!nidx) {
Christoph Hellwig6d1bbfc2010-11-23 14:37:40 +0100298 dprint(DBG_BNODE_MOD, "hfs: create new bmap node.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 next_node = hfs_bmap_new_bmap(node, idx);
300 } else
301 next_node = hfs_bnode_find(tree, nidx);
302 hfs_bnode_put(node);
303 if (IS_ERR(next_node))
304 return next_node;
305 node = next_node;
306
Andrew Morton487798d2008-04-30 00:54:54 -0700307 len = hfs_brec_lenoff(node, 0, &off16);
308 off = off16;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 off += node->page_offset;
310 pagep = node->page + (off >> PAGE_CACHE_SHIFT);
311 data = kmap(*pagep);
312 off &= ~PAGE_CACHE_MASK;
313 }
314}
315
316void hfs_bmap_free(struct hfs_bnode *node)
317{
318 struct hfs_btree *tree;
319 struct page *page;
320 u16 off, len;
321 u32 nidx;
322 u8 *data, byte, m;
323
324 dprint(DBG_BNODE_MOD, "btree_free_node: %u\n", node->this);
Eric Sesterhenn0bf3ba52006-04-01 01:14:43 +0200325 BUG_ON(!node->this);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 tree = node->tree;
327 nidx = node->this;
328 node = hfs_bnode_find(tree, 0);
329 if (IS_ERR(node))
330 return;
331 len = hfs_brec_lenoff(node, 2, &off);
332 while (nidx >= len * 8) {
333 u32 i;
334
335 nidx -= len * 8;
336 i = node->next;
337 hfs_bnode_put(node);
338 if (!i) {
339 /* panic */;
Anton Salikhmetov2753cc22010-12-16 18:08:38 +0200340 printk(KERN_CRIT "hfs: unable to free bnode %u. "
341 "bmap not found!\n",
342 node->this);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 return;
344 }
345 node = hfs_bnode_find(tree, i);
346 if (IS_ERR(node))
347 return;
348 if (node->type != HFS_NODE_MAP) {
349 /* panic */;
Anton Salikhmetov2753cc22010-12-16 18:08:38 +0200350 printk(KERN_CRIT "hfs: invalid bmap found! "
351 "(%u,%d)\n",
352 node->this, node->type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 hfs_bnode_put(node);
354 return;
355 }
356 len = hfs_brec_lenoff(node, 0, &off);
357 }
358 off += node->page_offset + nidx / 8;
359 page = node->page[off >> PAGE_CACHE_SHIFT];
360 data = kmap(page);
361 off &= ~PAGE_CACHE_MASK;
362 m = 1 << (~nidx & 7);
363 byte = data[off];
364 if (!(byte & m)) {
Anton Salikhmetov2753cc22010-12-16 18:08:38 +0200365 printk(KERN_CRIT "hfs: trying to free free bnode "
366 "%u(%d)\n",
367 node->this, node->type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 kunmap(page);
369 hfs_bnode_put(node);
370 return;
371 }
372 data[off] = byte & ~m;
373 set_page_dirty(page);
374 kunmap(page);
375 hfs_bnode_put(node);
376 tree->free_nodes++;
377 mark_inode_dirty(tree->inode);
378}