btree.c 8.8 KB

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