bnode.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  1. /*
  2. * linux/fs/hfs/bnode.c
  3. *
  4. * Copyright (C) 2001
  5. * Brad Boyer (flar@allandria.com)
  6. * (C) 2003 Ardis Technologies <roman@ardistech.com>
  7. *
  8. * Handle basic btree node operations
  9. */
  10. #include <linux/pagemap.h>
  11. #include <linux/slab.h>
  12. #include <linux/swap.h>
  13. #include "btree.h"
  14. void hfs_bnode_read(struct hfs_bnode *node, void *buf,
  15. int off, int len)
  16. {
  17. struct page *page;
  18. off += node->page_offset;
  19. page = node->page[0];
  20. memcpy(buf, kmap(page) + off, len);
  21. kunmap(page);
  22. }
  23. u16 hfs_bnode_read_u16(struct hfs_bnode *node, int off)
  24. {
  25. __be16 data;
  26. // optimize later...
  27. hfs_bnode_read(node, &data, off, 2);
  28. return be16_to_cpu(data);
  29. }
  30. u8 hfs_bnode_read_u8(struct hfs_bnode *node, int off)
  31. {
  32. u8 data;
  33. // optimize later...
  34. hfs_bnode_read(node, &data, off, 1);
  35. return data;
  36. }
  37. void hfs_bnode_read_key(struct hfs_bnode *node, void *key, int off)
  38. {
  39. struct hfs_btree *tree;
  40. int key_len;
  41. tree = node->tree;
  42. if (node->type == HFS_NODE_LEAF ||
  43. tree->attributes & HFS_TREE_VARIDXKEYS)
  44. key_len = hfs_bnode_read_u8(node, off) + 1;
  45. else
  46. key_len = tree->max_key_len + 1;
  47. hfs_bnode_read(node, key, off, key_len);
  48. }
  49. void hfs_bnode_write(struct hfs_bnode *node, void *buf, int off, int len)
  50. {
  51. struct page *page;
  52. off += node->page_offset;
  53. page = node->page[0];
  54. memcpy(kmap(page) + off, buf, len);
  55. kunmap(page);
  56. set_page_dirty(page);
  57. }
  58. void hfs_bnode_write_u16(struct hfs_bnode *node, int off, u16 data)
  59. {
  60. __be16 v = cpu_to_be16(data);
  61. // optimize later...
  62. hfs_bnode_write(node, &v, off, 2);
  63. }
  64. void hfs_bnode_write_u8(struct hfs_bnode *node, int off, u8 data)
  65. {
  66. // optimize later...
  67. hfs_bnode_write(node, &data, off, 1);
  68. }
  69. void hfs_bnode_clear(struct hfs_bnode *node, int off, int len)
  70. {
  71. struct page *page;
  72. off += node->page_offset;
  73. page = node->page[0];
  74. memset(kmap(page) + off, 0, len);
  75. kunmap(page);
  76. set_page_dirty(page);
  77. }
  78. void hfs_bnode_copy(struct hfs_bnode *dst_node, int dst,
  79. struct hfs_bnode *src_node, int src, int len)
  80. {
  81. struct hfs_btree *tree;
  82. struct page *src_page, *dst_page;
  83. dprint(DBG_BNODE_MOD, "copybytes: %u,%u,%u\n", dst, src, len);
  84. if (!len)
  85. return;
  86. tree = src_node->tree;
  87. src += src_node->page_offset;
  88. dst += dst_node->page_offset;
  89. src_page = src_node->page[0];
  90. dst_page = dst_node->page[0];
  91. memcpy(kmap(dst_page) + dst, kmap(src_page) + src, len);
  92. kunmap(src_page);
  93. kunmap(dst_page);
  94. set_page_dirty(dst_page);
  95. }
  96. void hfs_bnode_move(struct hfs_bnode *node, int dst, int src, int len)
  97. {
  98. struct page *page;
  99. void *ptr;
  100. dprint(DBG_BNODE_MOD, "movebytes: %u,%u,%u\n", dst, src, len);
  101. if (!len)
  102. return;
  103. src += node->page_offset;
  104. dst += node->page_offset;
  105. page = node->page[0];
  106. ptr = kmap(page);
  107. memmove(ptr + dst, ptr + src, len);
  108. kunmap(page);
  109. set_page_dirty(page);
  110. }
  111. void hfs_bnode_dump(struct hfs_bnode *node)
  112. {
  113. struct hfs_bnode_desc desc;
  114. __be32 cnid;
  115. int i, off, key_off;
  116. dprint(DBG_BNODE_MOD, "bnode: %d\n", node->this);
  117. hfs_bnode_read(node, &desc, 0, sizeof(desc));
  118. dprint(DBG_BNODE_MOD, "%d, %d, %d, %d, %d\n",
  119. be32_to_cpu(desc.next), be32_to_cpu(desc.prev),
  120. desc.type, desc.height, be16_to_cpu(desc.num_recs));
  121. off = node->tree->node_size - 2;
  122. for (i = be16_to_cpu(desc.num_recs); i >= 0; off -= 2, i--) {
  123. key_off = hfs_bnode_read_u16(node, off);
  124. dprint(DBG_BNODE_MOD, " %d", key_off);
  125. if (i && node->type == HFS_NODE_INDEX) {
  126. int tmp;
  127. if (node->tree->attributes & HFS_TREE_VARIDXKEYS)
  128. tmp = (hfs_bnode_read_u8(node, key_off) | 1) + 1;
  129. else
  130. tmp = node->tree->max_key_len + 1;
  131. dprint(DBG_BNODE_MOD, " (%d,%d", tmp, hfs_bnode_read_u8(node, key_off));
  132. hfs_bnode_read(node, &cnid, key_off + tmp, 4);
  133. dprint(DBG_BNODE_MOD, ",%d)", be32_to_cpu(cnid));
  134. } else if (i && node->type == HFS_NODE_LEAF) {
  135. int tmp;
  136. tmp = hfs_bnode_read_u8(node, key_off);
  137. dprint(DBG_BNODE_MOD, " (%d)", tmp);
  138. }
  139. }
  140. dprint(DBG_BNODE_MOD, "\n");
  141. }
  142. void hfs_bnode_unlink(struct hfs_bnode *node)
  143. {
  144. struct hfs_btree *tree;
  145. struct hfs_bnode *tmp;
  146. __be32 cnid;
  147. tree = node->tree;
  148. if (node->prev) {
  149. tmp = hfs_bnode_find(tree, node->prev);
  150. if (IS_ERR(tmp))
  151. return;
  152. tmp->next = node->next;
  153. cnid = cpu_to_be32(tmp->next);
  154. hfs_bnode_write(tmp, &cnid, offsetof(struct hfs_bnode_desc, next), 4);
  155. hfs_bnode_put(tmp);
  156. } else if (node->type == HFS_NODE_LEAF)
  157. tree->leaf_head = node->next;
  158. if (node->next) {
  159. tmp = hfs_bnode_find(tree, node->next);
  160. if (IS_ERR(tmp))
  161. return;
  162. tmp->prev = node->prev;
  163. cnid = cpu_to_be32(tmp->prev);
  164. hfs_bnode_write(tmp, &cnid, offsetof(struct hfs_bnode_desc, prev), 4);
  165. hfs_bnode_put(tmp);
  166. } else if (node->type == HFS_NODE_LEAF)
  167. tree->leaf_tail = node->prev;
  168. // move down?
  169. if (!node->prev && !node->next) {
  170. printk(KERN_DEBUG "hfs_btree_del_level\n");
  171. }
  172. if (!node->parent) {
  173. tree->root = 0;
  174. tree->depth = 0;
  175. }
  176. set_bit(HFS_BNODE_DELETED, &node->flags);
  177. }
  178. static inline int hfs_bnode_hash(u32 num)
  179. {
  180. num = (num >> 16) + num;
  181. num += num >> 8;
  182. return num & (NODE_HASH_SIZE - 1);
  183. }
  184. struct hfs_bnode *hfs_bnode_findhash(struct hfs_btree *tree, u32 cnid)
  185. {
  186. struct hfs_bnode *node;
  187. if (cnid >= tree->node_count) {
  188. printk(KERN_ERR "hfs: request for non-existent node %d in B*Tree\n", cnid);
  189. return NULL;
  190. }
  191. for (node = tree->node_hash[hfs_bnode_hash(cnid)];
  192. node; node = node->next_hash) {
  193. if (node->this == cnid) {
  194. return node;
  195. }
  196. }
  197. return NULL;
  198. }
  199. static struct hfs_bnode *__hfs_bnode_create(struct hfs_btree *tree, u32 cnid)
  200. {
  201. struct super_block *sb;
  202. struct hfs_bnode *node, *node2;
  203. struct address_space *mapping;
  204. struct page *page;
  205. int size, block, i, hash;
  206. loff_t off;
  207. if (cnid >= tree->node_count) {
  208. printk(KERN_ERR "hfs: request for non-existent node %d in B*Tree\n", cnid);
  209. return NULL;
  210. }
  211. sb = tree->inode->i_sb;
  212. size = sizeof(struct hfs_bnode) + tree->pages_per_bnode *
  213. sizeof(struct page *);
  214. node = kzalloc(size, GFP_KERNEL);
  215. if (!node)
  216. return NULL;
  217. node->tree = tree;
  218. node->this = cnid;
  219. set_bit(HFS_BNODE_NEW, &node->flags);
  220. atomic_set(&node->refcnt, 1);
  221. dprint(DBG_BNODE_REFS, "new_node(%d:%d): 1\n",
  222. node->tree->cnid, node->this);
  223. init_waitqueue_head(&node->lock_wq);
  224. spin_lock(&tree->hash_lock);
  225. node2 = hfs_bnode_findhash(tree, cnid);
  226. if (!node2) {
  227. hash = hfs_bnode_hash(cnid);
  228. node->next_hash = tree->node_hash[hash];
  229. tree->node_hash[hash] = node;
  230. tree->node_hash_cnt++;
  231. } else {
  232. spin_unlock(&tree->hash_lock);
  233. kfree(node);
  234. wait_event(node2->lock_wq, !test_bit(HFS_BNODE_NEW, &node2->flags));
  235. return node2;
  236. }
  237. spin_unlock(&tree->hash_lock);
  238. mapping = tree->inode->i_mapping;
  239. off = (loff_t)cnid * tree->node_size;
  240. block = off >> PAGE_CACHE_SHIFT;
  241. node->page_offset = off & ~PAGE_CACHE_MASK;
  242. for (i = 0; i < tree->pages_per_bnode; i++) {
  243. page = read_mapping_page(mapping, block++, NULL);
  244. if (IS_ERR(page))
  245. goto fail;
  246. if (PageError(page)) {
  247. page_cache_release(page);
  248. goto fail;
  249. }
  250. page_cache_release(page);
  251. node->page[i] = page;
  252. }
  253. return node;
  254. fail:
  255. set_bit(HFS_BNODE_ERROR, &node->flags);
  256. return node;
  257. }
  258. void hfs_bnode_unhash(struct hfs_bnode *node)
  259. {
  260. struct hfs_bnode **p;
  261. dprint(DBG_BNODE_REFS, "remove_node(%d:%d): %d\n",
  262. node->tree->cnid, node->this, atomic_read(&node->refcnt));
  263. for (p = &node->tree->node_hash[hfs_bnode_hash(node->this)];
  264. *p && *p != node; p = &(*p)->next_hash)
  265. ;
  266. BUG_ON(!*p);
  267. *p = node->next_hash;
  268. node->tree->node_hash_cnt--;
  269. }
  270. /* Load a particular node out of a tree */
  271. struct hfs_bnode *hfs_bnode_find(struct hfs_btree *tree, u32 num)
  272. {
  273. struct hfs_bnode *node;
  274. struct hfs_bnode_desc *desc;
  275. int i, rec_off, off, next_off;
  276. int entry_size, key_size;
  277. spin_lock(&tree->hash_lock);
  278. node = hfs_bnode_findhash(tree, num);
  279. if (node) {
  280. hfs_bnode_get(node);
  281. spin_unlock(&tree->hash_lock);
  282. wait_event(node->lock_wq, !test_bit(HFS_BNODE_NEW, &node->flags));
  283. if (test_bit(HFS_BNODE_ERROR, &node->flags))
  284. goto node_error;
  285. return node;
  286. }
  287. spin_unlock(&tree->hash_lock);
  288. node = __hfs_bnode_create(tree, num);
  289. if (!node)
  290. return ERR_PTR(-ENOMEM);
  291. if (test_bit(HFS_BNODE_ERROR, &node->flags))
  292. goto node_error;
  293. if (!test_bit(HFS_BNODE_NEW, &node->flags))
  294. return node;
  295. desc = (struct hfs_bnode_desc *)(kmap(node->page[0]) + node->page_offset);
  296. node->prev = be32_to_cpu(desc->prev);
  297. node->next = be32_to_cpu(desc->next);
  298. node->num_recs = be16_to_cpu(desc->num_recs);
  299. node->type = desc->type;
  300. node->height = desc->height;
  301. kunmap(node->page[0]);
  302. switch (node->type) {
  303. case HFS_NODE_HEADER:
  304. case HFS_NODE_MAP:
  305. if (node->height != 0)
  306. goto node_error;
  307. break;
  308. case HFS_NODE_LEAF:
  309. if (node->height != 1)
  310. goto node_error;
  311. break;
  312. case HFS_NODE_INDEX:
  313. if (node->height <= 1 || node->height > tree->depth)
  314. goto node_error;
  315. break;
  316. default:
  317. goto node_error;
  318. }
  319. rec_off = tree->node_size - 2;
  320. off = hfs_bnode_read_u16(node, rec_off);
  321. if (off != sizeof(struct hfs_bnode_desc))
  322. goto node_error;
  323. for (i = 1; i <= node->num_recs; off = next_off, i++) {
  324. rec_off -= 2;
  325. next_off = hfs_bnode_read_u16(node, rec_off);
  326. if (next_off <= off ||
  327. next_off > tree->node_size ||
  328. next_off & 1)
  329. goto node_error;
  330. entry_size = next_off - off;
  331. if (node->type != HFS_NODE_INDEX &&
  332. node->type != HFS_NODE_LEAF)
  333. continue;
  334. key_size = hfs_bnode_read_u8(node, off) + 1;
  335. if (key_size >= entry_size /*|| key_size & 1*/)
  336. goto node_error;
  337. }
  338. clear_bit(HFS_BNODE_NEW, &node->flags);
  339. wake_up(&node->lock_wq);
  340. return node;
  341. node_error:
  342. set_bit(HFS_BNODE_ERROR, &node->flags);
  343. clear_bit(HFS_BNODE_NEW, &node->flags);
  344. wake_up(&node->lock_wq);
  345. hfs_bnode_put(node);
  346. return ERR_PTR(-EIO);
  347. }
  348. void hfs_bnode_free(struct hfs_bnode *node)
  349. {
  350. //int i;
  351. //for (i = 0; i < node->tree->pages_per_bnode; i++)
  352. // if (node->page[i])
  353. // page_cache_release(node->page[i]);
  354. kfree(node);
  355. }
  356. struct hfs_bnode *hfs_bnode_create(struct hfs_btree *tree, u32 num)
  357. {
  358. struct hfs_bnode *node;
  359. struct page **pagep;
  360. int i;
  361. spin_lock(&tree->hash_lock);
  362. node = hfs_bnode_findhash(tree, num);
  363. spin_unlock(&tree->hash_lock);
  364. BUG_ON(node);
  365. node = __hfs_bnode_create(tree, num);
  366. if (!node)
  367. return ERR_PTR(-ENOMEM);
  368. if (test_bit(HFS_BNODE_ERROR, &node->flags)) {
  369. hfs_bnode_put(node);
  370. return ERR_PTR(-EIO);
  371. }
  372. pagep = node->page;
  373. memset(kmap(*pagep) + node->page_offset, 0,
  374. min((int)PAGE_CACHE_SIZE, (int)tree->node_size));
  375. set_page_dirty(*pagep);
  376. kunmap(*pagep);
  377. for (i = 1; i < tree->pages_per_bnode; i++) {
  378. memset(kmap(*++pagep), 0, PAGE_CACHE_SIZE);
  379. set_page_dirty(*pagep);
  380. kunmap(*pagep);
  381. }
  382. clear_bit(HFS_BNODE_NEW, &node->flags);
  383. wake_up(&node->lock_wq);
  384. return node;
  385. }
  386. void hfs_bnode_get(struct hfs_bnode *node)
  387. {
  388. if (node) {
  389. atomic_inc(&node->refcnt);
  390. dprint(DBG_BNODE_REFS, "get_node(%d:%d): %d\n",
  391. node->tree->cnid, node->this, atomic_read(&node->refcnt));
  392. }
  393. }
  394. /* Dispose of resources used by a node */
  395. void hfs_bnode_put(struct hfs_bnode *node)
  396. {
  397. if (node) {
  398. struct hfs_btree *tree = node->tree;
  399. int i;
  400. dprint(DBG_BNODE_REFS, "put_node(%d:%d): %d\n",
  401. node->tree->cnid, node->this, atomic_read(&node->refcnt));
  402. BUG_ON(!atomic_read(&node->refcnt));
  403. if (!atomic_dec_and_lock(&node->refcnt, &tree->hash_lock))
  404. return;
  405. for (i = 0; i < tree->pages_per_bnode; i++) {
  406. if (!node->page[i])
  407. continue;
  408. mark_page_accessed(node->page[i]);
  409. }
  410. if (test_bit(HFS_BNODE_DELETED, &node->flags)) {
  411. hfs_bnode_unhash(node);
  412. spin_unlock(&tree->hash_lock);
  413. hfs_bmap_free(node);
  414. hfs_bnode_free(node);
  415. return;
  416. }
  417. spin_unlock(&tree->hash_lock);
  418. }
  419. }