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 | |
Thomas Gleixner | 467c3d9 | 2010-10-01 05:46:52 +0200 | [diff] [blame] | 33 | mutex_init(&tree->tree_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Jeff Mahoney | ee52716 | 2010-10-14 09:53:37 -0400 | [diff] [blame] | 42 | if (!HFSPLUS_I(tree->inode)->first_blocks) { |
Joe Perches | d614267 | 2013-04-30 15:27:55 -0700 | [diff] [blame] | 43 | pr_err("invalid btree extent records (0 size)\n"); |
Jeff Mahoney | ee52716 | 2010-10-14 09:53:37 -0400 | [diff] [blame] | 44 | goto free_inode; |
| 45 | } |
| 46 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | mapping = tree->inode->i_mapping; |
Pekka Enberg | 090d2b1 | 2006-06-23 02:05:08 -0700 | [diff] [blame] | 48 | page = read_mapping_page(mapping, 0, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | if (IS_ERR(page)) |
Jeff Mahoney | ee52716 | 2010-10-14 09:53:37 -0400 | [diff] [blame] | 50 | goto free_inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | |
| 52 | /* Load the header */ |
Anton Salikhmetov | 2753cc2 | 2010-12-16 18:08:38 +0200 | [diff] [blame] | 53 | head = (struct hfs_btree_header_rec *)(kmap(page) + |
| 54 | sizeof(struct hfs_bnode_desc)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 Sandeen | 9250f92 | 2010-10-14 09:53:48 -0400 | [diff] [blame] | 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)) { |
Joe Perches | d614267 | 2013-04-30 15:27:55 -0700 | [diff] [blame] | 70 | pr_err("invalid extent max_key_len %d\n", |
Eric Sandeen | 9250f92 | 2010-10-14 09:53:48 -0400 | [diff] [blame] | 71 | tree->max_key_len); |
| 72 | goto fail_page; |
| 73 | } |
Christoph Hellwig | 13571a6 | 2010-10-14 09:54:23 -0400 | [diff] [blame] | 74 | if (tree->attributes & HFS_TREE_VARIDXKEYS) { |
Joe Perches | d614267 | 2013-04-30 15:27:55 -0700 | [diff] [blame] | 75 | pr_err("invalid extent btree flag\n"); |
Christoph Hellwig | 13571a6 | 2010-10-14 09:54:23 -0400 | [diff] [blame] | 76 | goto fail_page; |
| 77 | } |
| 78 | |
David Elliott | 2179d37 | 2006-01-18 17:43:08 -0800 | [diff] [blame] | 79 | tree->keycmp = hfsplus_ext_cmp_key; |
Eric Sandeen | 9250f92 | 2010-10-14 09:53:48 -0400 | [diff] [blame] | 80 | break; |
| 81 | case HFSPLUS_CAT_CNID: |
| 82 | if (tree->max_key_len != HFSPLUS_CAT_KEYLEN - sizeof(u16)) { |
Joe Perches | d614267 | 2013-04-30 15:27:55 -0700 | [diff] [blame] | 83 | pr_err("invalid catalog max_key_len %d\n", |
Eric Sandeen | 9250f92 | 2010-10-14 09:53:48 -0400 | [diff] [blame] | 84 | tree->max_key_len); |
| 85 | goto fail_page; |
| 86 | } |
Christoph Hellwig | 13571a6 | 2010-10-14 09:54:23 -0400 | [diff] [blame] | 87 | if (!(tree->attributes & HFS_TREE_VARIDXKEYS)) { |
Joe Perches | d614267 | 2013-04-30 15:27:55 -0700 | [diff] [blame] | 88 | pr_err("invalid catalog btree flag\n"); |
Christoph Hellwig | 13571a6 | 2010-10-14 09:54:23 -0400 | [diff] [blame] | 89 | goto fail_page; |
| 90 | } |
Eric Sandeen | 9250f92 | 2010-10-14 09:53:48 -0400 | [diff] [blame] | 91 | |
Christoph Hellwig | 84adede | 2010-10-01 05:45:20 +0200 | [diff] [blame] | 92 | if (test_bit(HFSPLUS_SB_HFSX, &HFSPLUS_SB(sb)->flags) && |
David Elliott | 2179d37 | 2006-01-18 17:43:08 -0800 | [diff] [blame] | 93 | (head->key_type == HFSPLUS_KEY_BINARY)) |
| 94 | tree->keycmp = hfsplus_cat_bin_cmp_key; |
Duane Griffin | d45bce8 | 2007-07-15 23:41:23 -0700 | [diff] [blame] | 95 | else { |
David Elliott | 2179d37 | 2006-01-18 17:43:08 -0800 | [diff] [blame] | 96 | tree->keycmp = hfsplus_cat_case_cmp_key; |
Christoph Hellwig | 84adede | 2010-10-01 05:45:20 +0200 | [diff] [blame] | 97 | set_bit(HFSPLUS_SB_CASEFOLD, &HFSPLUS_SB(sb)->flags); |
Duane Griffin | d45bce8 | 2007-07-15 23:41:23 -0700 | [diff] [blame] | 98 | } |
Eric Sandeen | 9250f92 | 2010-10-14 09:53:48 -0400 | [diff] [blame] | 99 | break; |
Vyacheslav Dubeyko | 324ef39 | 2013-02-27 17:03:04 -0800 | [diff] [blame] | 100 | case HFSPLUS_ATTR_CNID: |
| 101 | if (tree->max_key_len != HFSPLUS_ATTR_KEYLEN - sizeof(u16)) { |
Joe Perches | d614267 | 2013-04-30 15:27:55 -0700 | [diff] [blame] | 102 | pr_err("invalid attributes max_key_len %d\n", |
Vyacheslav Dubeyko | 324ef39 | 2013-02-27 17:03:04 -0800 | [diff] [blame] | 103 | tree->max_key_len); |
| 104 | goto fail_page; |
| 105 | } |
| 106 | tree->keycmp = hfsplus_attr_bin_cmp_key; |
| 107 | break; |
Eric Sandeen | 9250f92 | 2010-10-14 09:53:48 -0400 | [diff] [blame] | 108 | default: |
Joe Perches | d614267 | 2013-04-30 15:27:55 -0700 | [diff] [blame] | 109 | pr_err("unknown B*Tree requested\n"); |
David Elliott | 2179d37 | 2006-01-18 17:43:08 -0800 | [diff] [blame] | 110 | goto fail_page; |
| 111 | } |
| 112 | |
Christoph Hellwig | 13571a6 | 2010-10-14 09:54:23 -0400 | [diff] [blame] | 113 | if (!(tree->attributes & HFS_TREE_BIGKEYS)) { |
Joe Perches | d614267 | 2013-04-30 15:27:55 -0700 | [diff] [blame] | 114 | pr_err("invalid btree flag\n"); |
Christoph Hellwig | 13571a6 | 2010-10-14 09:54:23 -0400 | [diff] [blame] | 115 | goto fail_page; |
| 116 | } |
| 117 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | size = tree->node_size; |
Vignesh Babu BM | e1b5c1d | 2007-05-08 00:24:30 -0700 | [diff] [blame] | 119 | if (!is_power_of_2(size)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | goto fail_page; |
| 121 | if (!tree->node_count) |
| 122 | goto fail_page; |
Eric Sandeen | 9250f92 | 2010-10-14 09:53:48 -0400 | [diff] [blame] | 123 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | tree->node_size_shift = ffs(size) - 1; |
| 125 | |
Anton Salikhmetov | 2753cc2 | 2010-12-16 18:08:38 +0200 | [diff] [blame] | 126 | tree->pages_per_bnode = |
| 127 | (tree->node_size + PAGE_CACHE_SIZE - 1) >> |
| 128 | PAGE_CACHE_SHIFT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | |
| 130 | kunmap(page); |
| 131 | page_cache_release(page); |
| 132 | return tree; |
| 133 | |
| 134 | fail_page: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | page_cache_release(page); |
Jeff Mahoney | ee52716 | 2010-10-14 09:53:37 -0400 | [diff] [blame] | 136 | free_inode: |
Eric Sandeen | 9250f92 | 2010-10-14 09:53:48 -0400 | [diff] [blame] | 137 | tree->inode->i_mapping->a_ops = &hfsplus_aops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | iput(tree->inode); |
Jeff Mahoney | ee52716 | 2010-10-14 09:53:37 -0400 | [diff] [blame] | 139 | free_tree: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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)) |
Joe Perches | d614267 | 2013-04-30 15:27:55 -0700 | [diff] [blame] | 157 | pr_crit("node %d:%d " |
Anton Salikhmetov | 2753cc2 | 2010-12-16 18:08:38 +0200 | [diff] [blame] | 158 | "still has %d user(s)!\n", |
| 159 | node->tree->cnid, node->this, |
| 160 | atomic_read(&node->refcnt)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | hfs_bnode_free(node); |
| 162 | tree->node_hash_cnt--; |
| 163 | } |
| 164 | } |
| 165 | iput(tree->inode); |
| 166 | kfree(tree); |
| 167 | } |
| 168 | |
Vyacheslav Dubeyko | 81cc7fa | 2012-12-20 15:05:28 -0800 | [diff] [blame] | 169 | int hfs_btree_write(struct hfs_btree *tree) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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? */ |
Vyacheslav Dubeyko | 81cc7fa | 2012-12-20 15:05:28 -0800 | [diff] [blame] | 178 | return -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | /* Load the header */ |
| 180 | page = node->page[0]; |
Anton Salikhmetov | 2753cc2 | 2010-12-16 18:08:38 +0200 | [diff] [blame] | 181 | head = (struct hfs_btree_header_rec *)(kmap(page) + |
| 182 | sizeof(struct hfs_bnode_desc)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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); |
Vyacheslav Dubeyko | 81cc7fa | 2012-12-20 15:05:28 -0800 | [diff] [blame] | 196 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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; |
Andrew Morton | 487798d | 2008-04-30 00:54:54 -0700 | [diff] [blame] | 237 | unsigned off; |
| 238 | u16 off16; |
| 239 | u16 len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | u8 *data, byte, m; |
| 241 | int i; |
| 242 | |
| 243 | while (!tree->free_nodes) { |
| 244 | struct inode *inode = tree->inode; |
Christoph Hellwig | 6af502d | 2010-10-01 05:43:31 +0200 | [diff] [blame] | 245 | struct hfsplus_inode_info *hip = HFSPLUS_I(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | u32 count; |
| 247 | int res; |
| 248 | |
| 249 | res = hfsplus_file_extend(inode); |
| 250 | if (res) |
| 251 | return ERR_PTR(res); |
Christoph Hellwig | 6af502d | 2010-10-01 05:43:31 +0200 | [diff] [blame] | 252 | hip->phys_size = inode->i_size = |
| 253 | (loff_t)hip->alloc_blocks << |
Christoph Hellwig | dd73a01 | 2010-10-01 05:42:59 +0200 | [diff] [blame] | 254 | HFSPLUS_SB(tree->sb)->alloc_blksz_shift; |
Christoph Hellwig | 6af502d | 2010-10-01 05:43:31 +0200 | [diff] [blame] | 255 | hip->fs_blocks = |
| 256 | hip->alloc_blocks << HFSPLUS_SB(tree->sb)->fs_shift; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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; |
Andrew Morton | 487798d | 2008-04-30 00:54:54 -0700 | [diff] [blame] | 267 | len = hfs_brec_lenoff(node, 2, &off16); |
| 268 | off = off16; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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); |
Anton Salikhmetov | 2753cc2 | 2010-12-16 18:08:38 +0200 | [diff] [blame] | 289 | return hfs_bnode_create(tree, |
| 290 | idx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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) { |
Joe Perches | c2b3e1f | 2013-04-30 15:27:54 -0700 | [diff] [blame] | 305 | hfs_dbg(BNODE_MOD, "create new bmap node\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Andrew Morton | 487798d | 2008-04-30 00:54:54 -0700 | [diff] [blame] | 314 | len = hfs_brec_lenoff(node, 0, &off16); |
| 315 | off = off16; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Joe Perches | c2b3e1f | 2013-04-30 15:27:54 -0700 | [diff] [blame] | 331 | hfs_dbg(BNODE_MOD, "btree_free_node: %u\n", node->this); |
Eric Sesterhenn | 0bf3ba5 | 2006-04-01 01:14:43 +0200 | [diff] [blame] | 332 | BUG_ON(!node->this); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 */; |
Joe Perches | d614267 | 2013-04-30 15:27:55 -0700 | [diff] [blame] | 347 | pr_crit("unable to free bnode %u. " |
Anton Salikhmetov | 2753cc2 | 2010-12-16 18:08:38 +0200 | [diff] [blame] | 348 | "bmap not found!\n", |
| 349 | node->this); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 */; |
Joe Perches | d614267 | 2013-04-30 15:27:55 -0700 | [diff] [blame] | 357 | pr_crit("invalid bmap found! " |
Anton Salikhmetov | 2753cc2 | 2010-12-16 18:08:38 +0200 | [diff] [blame] | 358 | "(%u,%d)\n", |
| 359 | node->this, node->type); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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)) { |
Joe Perches | d614267 | 2013-04-30 15:27:55 -0700 | [diff] [blame] | 372 | pr_crit("trying to free free bnode " |
Anton Salikhmetov | 2753cc2 | 2010-12-16 18:08:38 +0200 | [diff] [blame] | 373 | "%u(%d)\n", |
| 374 | node->this, node->type); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | } |