Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> |
Vignesh Babu BM | e1b5c1d | 2007-05-08 00:24:30 -0700 | [diff] [blame] | 13 | #include <linux/log2.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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; |
David Howells | 6352539 | 2008-02-07 00:15:40 -0800 | [diff] [blame] | 25 | struct inode *inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | struct page *page; |
| 27 | unsigned int size; |
| 28 | |
Panagiotis Issaris | f8314dc | 2006-09-27 01:49:37 -0700 | [diff] [blame] | 29 | tree = kzalloc(sizeof(*tree), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | if (!tree) |
| 31 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | |
| 33 | init_MUTEX(&tree->tree_lock); |
| 34 | spin_lock_init(&tree->hash_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | tree->sb = sb; |
| 36 | tree->cnid = id; |
David Howells | 6352539 | 2008-02-07 00:15:40 -0800 | [diff] [blame] | 37 | inode = hfsplus_iget(sb, id); |
| 38 | if (IS_ERR(inode)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | goto free_tree; |
David Howells | 6352539 | 2008-02-07 00:15:40 -0800 | [diff] [blame] | 40 | tree->inode = inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | |
| 42 | mapping = tree->inode->i_mapping; |
Pekka Enberg | 090d2b1 | 2006-06-23 02:05:08 -0700 | [diff] [blame] | 43 | page = read_mapping_page(mapping, 0, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | if (IS_ERR(page)) |
| 45 | goto free_tree; |
| 46 | |
| 47 | /* Load the header */ |
| 48 | head = (struct hfs_btree_header_rec *)(kmap(page) + sizeof(struct hfs_bnode_desc)); |
| 49 | tree->root = be32_to_cpu(head->root); |
| 50 | tree->leaf_count = be32_to_cpu(head->leaf_count); |
| 51 | tree->leaf_head = be32_to_cpu(head->leaf_head); |
| 52 | tree->leaf_tail = be32_to_cpu(head->leaf_tail); |
| 53 | tree->node_count = be32_to_cpu(head->node_count); |
| 54 | tree->free_nodes = be32_to_cpu(head->free_nodes); |
| 55 | tree->attributes = be32_to_cpu(head->attributes); |
| 56 | tree->node_size = be16_to_cpu(head->node_size); |
| 57 | tree->max_key_len = be16_to_cpu(head->max_key_len); |
| 58 | tree->depth = be16_to_cpu(head->depth); |
| 59 | |
David Elliott | 2179d37 | 2006-01-18 17:43:08 -0800 | [diff] [blame] | 60 | /* Set the correct compare function */ |
| 61 | if (id == HFSPLUS_EXT_CNID) { |
| 62 | tree->keycmp = hfsplus_ext_cmp_key; |
| 63 | } else if (id == HFSPLUS_CAT_CNID) { |
Christoph Hellwig | dd73a01 | 2010-10-01 05:42:59 +0200 | [diff] [blame] | 64 | if ((HFSPLUS_SB(sb)->flags & HFSPLUS_SB_HFSX) && |
David Elliott | 2179d37 | 2006-01-18 17:43:08 -0800 | [diff] [blame] | 65 | (head->key_type == HFSPLUS_KEY_BINARY)) |
| 66 | tree->keycmp = hfsplus_cat_bin_cmp_key; |
Duane Griffin | d45bce8 | 2007-07-15 23:41:23 -0700 | [diff] [blame] | 67 | else { |
David Elliott | 2179d37 | 2006-01-18 17:43:08 -0800 | [diff] [blame] | 68 | tree->keycmp = hfsplus_cat_case_cmp_key; |
Christoph Hellwig | dd73a01 | 2010-10-01 05:42:59 +0200 | [diff] [blame] | 69 | HFSPLUS_SB(sb)->flags |= HFSPLUS_SB_CASEFOLD; |
Duane Griffin | d45bce8 | 2007-07-15 23:41:23 -0700 | [diff] [blame] | 70 | } |
David Elliott | 2179d37 | 2006-01-18 17:43:08 -0800 | [diff] [blame] | 71 | } else { |
| 72 | printk(KERN_ERR "hfs: unknown B*Tree requested\n"); |
| 73 | goto fail_page; |
| 74 | } |
| 75 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | size = tree->node_size; |
Vignesh Babu BM | e1b5c1d | 2007-05-08 00:24:30 -0700 | [diff] [blame] | 77 | if (!is_power_of_2(size)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | goto fail_page; |
| 79 | if (!tree->node_count) |
| 80 | goto fail_page; |
| 81 | tree->node_size_shift = ffs(size) - 1; |
| 82 | |
| 83 | tree->pages_per_bnode = (tree->node_size + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT; |
| 84 | |
| 85 | kunmap(page); |
| 86 | page_cache_release(page); |
| 87 | return tree; |
| 88 | |
| 89 | fail_page: |
| 90 | tree->inode->i_mapping->a_ops = &hfsplus_aops; |
| 91 | page_cache_release(page); |
| 92 | free_tree: |
| 93 | iput(tree->inode); |
| 94 | kfree(tree); |
| 95 | return NULL; |
| 96 | } |
| 97 | |
| 98 | /* Release resources used by a btree */ |
| 99 | void hfs_btree_close(struct hfs_btree *tree) |
| 100 | { |
| 101 | struct hfs_bnode *node; |
| 102 | int i; |
| 103 | |
| 104 | if (!tree) |
| 105 | return; |
| 106 | |
| 107 | for (i = 0; i < NODE_HASH_SIZE; i++) { |
| 108 | while ((node = tree->node_hash[i])) { |
| 109 | tree->node_hash[i] = node->next_hash; |
| 110 | if (atomic_read(&node->refcnt)) |
Roman Zippel | 634725a | 2006-01-18 17:43:05 -0800 | [diff] [blame] | 111 | printk(KERN_CRIT "hfs: node %d:%d still has %d user(s)!\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | node->tree->cnid, node->this, atomic_read(&node->refcnt)); |
| 113 | hfs_bnode_free(node); |
| 114 | tree->node_hash_cnt--; |
| 115 | } |
| 116 | } |
| 117 | iput(tree->inode); |
| 118 | kfree(tree); |
| 119 | } |
| 120 | |
| 121 | void hfs_btree_write(struct hfs_btree *tree) |
| 122 | { |
| 123 | struct hfs_btree_header_rec *head; |
| 124 | struct hfs_bnode *node; |
| 125 | struct page *page; |
| 126 | |
| 127 | node = hfs_bnode_find(tree, 0); |
| 128 | if (IS_ERR(node)) |
| 129 | /* panic? */ |
| 130 | return; |
| 131 | /* Load the header */ |
| 132 | page = node->page[0]; |
| 133 | head = (struct hfs_btree_header_rec *)(kmap(page) + sizeof(struct hfs_bnode_desc)); |
| 134 | |
| 135 | head->root = cpu_to_be32(tree->root); |
| 136 | head->leaf_count = cpu_to_be32(tree->leaf_count); |
| 137 | head->leaf_head = cpu_to_be32(tree->leaf_head); |
| 138 | head->leaf_tail = cpu_to_be32(tree->leaf_tail); |
| 139 | head->node_count = cpu_to_be32(tree->node_count); |
| 140 | head->free_nodes = cpu_to_be32(tree->free_nodes); |
| 141 | head->attributes = cpu_to_be32(tree->attributes); |
| 142 | head->depth = cpu_to_be16(tree->depth); |
| 143 | |
| 144 | kunmap(page); |
| 145 | set_page_dirty(page); |
| 146 | hfs_bnode_put(node); |
| 147 | } |
| 148 | |
| 149 | static struct hfs_bnode *hfs_bmap_new_bmap(struct hfs_bnode *prev, u32 idx) |
| 150 | { |
| 151 | struct hfs_btree *tree = prev->tree; |
| 152 | struct hfs_bnode *node; |
| 153 | struct hfs_bnode_desc desc; |
| 154 | __be32 cnid; |
| 155 | |
| 156 | node = hfs_bnode_create(tree, idx); |
| 157 | if (IS_ERR(node)) |
| 158 | return node; |
| 159 | |
| 160 | tree->free_nodes--; |
| 161 | prev->next = idx; |
| 162 | cnid = cpu_to_be32(idx); |
| 163 | hfs_bnode_write(prev, &cnid, offsetof(struct hfs_bnode_desc, next), 4); |
| 164 | |
| 165 | node->type = HFS_NODE_MAP; |
| 166 | node->num_recs = 1; |
| 167 | hfs_bnode_clear(node, 0, tree->node_size); |
| 168 | desc.next = 0; |
| 169 | desc.prev = 0; |
| 170 | desc.type = HFS_NODE_MAP; |
| 171 | desc.height = 0; |
| 172 | desc.num_recs = cpu_to_be16(1); |
| 173 | desc.reserved = 0; |
| 174 | hfs_bnode_write(node, &desc, 0, sizeof(desc)); |
| 175 | hfs_bnode_write_u16(node, 14, 0x8000); |
| 176 | hfs_bnode_write_u16(node, tree->node_size - 2, 14); |
| 177 | hfs_bnode_write_u16(node, tree->node_size - 4, tree->node_size - 6); |
| 178 | |
| 179 | return node; |
| 180 | } |
| 181 | |
| 182 | struct hfs_bnode *hfs_bmap_alloc(struct hfs_btree *tree) |
| 183 | { |
| 184 | struct hfs_bnode *node, *next_node; |
| 185 | struct page **pagep; |
| 186 | u32 nidx, idx; |
Andrew Morton | 487798d | 2008-04-30 00:54:54 -0700 | [diff] [blame] | 187 | unsigned off; |
| 188 | u16 off16; |
| 189 | u16 len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | u8 *data, byte, m; |
| 191 | int i; |
| 192 | |
| 193 | while (!tree->free_nodes) { |
| 194 | struct inode *inode = tree->inode; |
Christoph Hellwig | 6af502d | 2010-10-01 05:43:31 +0200 | [diff] [blame^] | 195 | struct hfsplus_inode_info *hip = HFSPLUS_I(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | u32 count; |
| 197 | int res; |
| 198 | |
| 199 | res = hfsplus_file_extend(inode); |
| 200 | if (res) |
| 201 | return ERR_PTR(res); |
Christoph Hellwig | 6af502d | 2010-10-01 05:43:31 +0200 | [diff] [blame^] | 202 | hip->phys_size = inode->i_size = |
| 203 | (loff_t)hip->alloc_blocks << |
Christoph Hellwig | dd73a01 | 2010-10-01 05:42:59 +0200 | [diff] [blame] | 204 | HFSPLUS_SB(tree->sb)->alloc_blksz_shift; |
Christoph Hellwig | 6af502d | 2010-10-01 05:43:31 +0200 | [diff] [blame^] | 205 | hip->fs_blocks = |
| 206 | hip->alloc_blocks << HFSPLUS_SB(tree->sb)->fs_shift; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | inode_set_bytes(inode, inode->i_size); |
| 208 | count = inode->i_size >> tree->node_size_shift; |
| 209 | tree->free_nodes = count - tree->node_count; |
| 210 | tree->node_count = count; |
| 211 | } |
| 212 | |
| 213 | nidx = 0; |
| 214 | node = hfs_bnode_find(tree, nidx); |
| 215 | if (IS_ERR(node)) |
| 216 | return node; |
Andrew Morton | 487798d | 2008-04-30 00:54:54 -0700 | [diff] [blame] | 217 | len = hfs_brec_lenoff(node, 2, &off16); |
| 218 | off = off16; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | |
| 220 | off += node->page_offset; |
| 221 | pagep = node->page + (off >> PAGE_CACHE_SHIFT); |
| 222 | data = kmap(*pagep); |
| 223 | off &= ~PAGE_CACHE_MASK; |
| 224 | idx = 0; |
| 225 | |
| 226 | for (;;) { |
| 227 | while (len) { |
| 228 | byte = data[off]; |
| 229 | if (byte != 0xff) { |
| 230 | for (m = 0x80, i = 0; i < 8; m >>= 1, i++) { |
| 231 | if (!(byte & m)) { |
| 232 | idx += i; |
| 233 | data[off] |= m; |
| 234 | set_page_dirty(*pagep); |
| 235 | kunmap(*pagep); |
| 236 | tree->free_nodes--; |
| 237 | mark_inode_dirty(tree->inode); |
| 238 | hfs_bnode_put(node); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | return hfs_bnode_create(tree, idx); |
| 240 | } |
| 241 | } |
| 242 | } |
| 243 | if (++off >= PAGE_CACHE_SIZE) { |
| 244 | kunmap(*pagep); |
| 245 | data = kmap(*++pagep); |
| 246 | off = 0; |
| 247 | } |
| 248 | idx += 8; |
| 249 | len--; |
| 250 | } |
| 251 | kunmap(*pagep); |
| 252 | nidx = node->next; |
| 253 | if (!nidx) { |
Roman Zippel | 634725a | 2006-01-18 17:43:05 -0800 | [diff] [blame] | 254 | printk(KERN_DEBUG "hfs: create new bmap node...\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | next_node = hfs_bmap_new_bmap(node, idx); |
| 256 | } else |
| 257 | next_node = hfs_bnode_find(tree, nidx); |
| 258 | hfs_bnode_put(node); |
| 259 | if (IS_ERR(next_node)) |
| 260 | return next_node; |
| 261 | node = next_node; |
| 262 | |
Andrew Morton | 487798d | 2008-04-30 00:54:54 -0700 | [diff] [blame] | 263 | len = hfs_brec_lenoff(node, 0, &off16); |
| 264 | off = off16; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | off += node->page_offset; |
| 266 | pagep = node->page + (off >> PAGE_CACHE_SHIFT); |
| 267 | data = kmap(*pagep); |
| 268 | off &= ~PAGE_CACHE_MASK; |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | void hfs_bmap_free(struct hfs_bnode *node) |
| 273 | { |
| 274 | struct hfs_btree *tree; |
| 275 | struct page *page; |
| 276 | u16 off, len; |
| 277 | u32 nidx; |
| 278 | u8 *data, byte, m; |
| 279 | |
| 280 | dprint(DBG_BNODE_MOD, "btree_free_node: %u\n", node->this); |
Eric Sesterhenn | 0bf3ba5 | 2006-04-01 01:14:43 +0200 | [diff] [blame] | 281 | BUG_ON(!node->this); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 282 | tree = node->tree; |
| 283 | nidx = node->this; |
| 284 | node = hfs_bnode_find(tree, 0); |
| 285 | if (IS_ERR(node)) |
| 286 | return; |
| 287 | len = hfs_brec_lenoff(node, 2, &off); |
| 288 | while (nidx >= len * 8) { |
| 289 | u32 i; |
| 290 | |
| 291 | nidx -= len * 8; |
| 292 | i = node->next; |
| 293 | hfs_bnode_put(node); |
| 294 | if (!i) { |
| 295 | /* panic */; |
Roman Zippel | 634725a | 2006-01-18 17:43:05 -0800 | [diff] [blame] | 296 | printk(KERN_CRIT "hfs: unable to free bnode %u. bmap not found!\n", node->this); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | return; |
| 298 | } |
| 299 | node = hfs_bnode_find(tree, i); |
| 300 | if (IS_ERR(node)) |
| 301 | return; |
| 302 | if (node->type != HFS_NODE_MAP) { |
| 303 | /* panic */; |
Roman Zippel | 634725a | 2006-01-18 17:43:05 -0800 | [diff] [blame] | 304 | printk(KERN_CRIT "hfs: invalid bmap found! (%u,%d)\n", node->this, node->type); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | hfs_bnode_put(node); |
| 306 | return; |
| 307 | } |
| 308 | len = hfs_brec_lenoff(node, 0, &off); |
| 309 | } |
| 310 | off += node->page_offset + nidx / 8; |
| 311 | page = node->page[off >> PAGE_CACHE_SHIFT]; |
| 312 | data = kmap(page); |
| 313 | off &= ~PAGE_CACHE_MASK; |
| 314 | m = 1 << (~nidx & 7); |
| 315 | byte = data[off]; |
| 316 | if (!(byte & m)) { |
Roman Zippel | 634725a | 2006-01-18 17:43:05 -0800 | [diff] [blame] | 317 | printk(KERN_CRIT "hfs: trying to free free bnode %u(%d)\n", node->this, node->type); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 318 | kunmap(page); |
| 319 | hfs_bnode_put(node); |
| 320 | return; |
| 321 | } |
| 322 | data[off] = byte & ~m; |
| 323 | set_page_dirty(page); |
| 324 | kunmap(page); |
| 325 | hfs_bnode_put(node); |
| 326 | tree->free_nodes++; |
| 327 | mark_inode_dirty(tree->inode); |
| 328 | } |