bnode.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  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. hfs_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. hfs_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. hfs_dbg(BNODE_MOD, "bnode: %d\n", node->this);
  117. hfs_bnode_read(node, &desc, 0, sizeof(desc));
  118. hfs_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. hfs_dbg_cont(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. hfs_dbg_cont(BNODE_MOD, " (%d,%d",
  132. tmp, hfs_bnode_read_u8(node, key_off));
  133. hfs_bnode_read(node, &cnid, key_off + tmp, 4);
  134. hfs_dbg_cont(BNODE_MOD, ",%d)", be32_to_cpu(cnid));
  135. } else if (i && node->type == HFS_NODE_LEAF) {
  136. int tmp;
  137. tmp = hfs_bnode_read_u8(node, key_off);
  138. hfs_dbg_cont(BNODE_MOD, " (%d)", tmp);
  139. }
  140. }
  141. hfs_dbg_cont(BNODE_MOD, "\n");
  142. }
  143. void hfs_bnode_unlink(struct hfs_bnode *node)
  144. {
  145. struct hfs_btree *tree;
  146. struct hfs_bnode *tmp;
  147. __be32 cnid;
  148. tree = node->tree;
  149. if (node->prev) {
  150. tmp = hfs_bnode_find(tree, node->prev);
  151. if (IS_ERR(tmp))
  152. return;
  153. tmp->next = node->next;
  154. cnid = cpu_to_be32(tmp->next);
  155. hfs_bnode_write(tmp, &cnid, offsetof(struct hfs_bnode_desc, next), 4);
  156. hfs_bnode_put(tmp);
  157. } else if (node->type == HFS_NODE_LEAF)
  158. tree->leaf_head = node->next;
  159. if (node->next) {
  160. tmp = hfs_bnode_find(tree, node->next);
  161. if (IS_ERR(tmp))
  162. return;
  163. tmp->prev = node->prev;
  164. cnid = cpu_to_be32(tmp->prev);
  165. hfs_bnode_write(tmp, &cnid, offsetof(struct hfs_bnode_desc, prev), 4);
  166. hfs_bnode_put(tmp);
  167. } else if (node->type == HFS_NODE_LEAF)
  168. tree->leaf_tail = node->prev;
  169. // move down?
  170. if (!node->prev && !node->next) {
  171. printk(KERN_DEBUG "hfs_btree_del_level\n");
  172. }
  173. if (!node->parent) {
  174. tree->root = 0;
  175. tree->depth = 0;
  176. }
  177. set_bit(HFS_BNODE_DELETED, &node->flags);
  178. }
  179. static inline int hfs_bnode_hash(u32 num)
  180. {
  181. num = (num >> 16) + num;
  182. num += num >> 8;
  183. return num & (NODE_HASH_SIZE - 1);
  184. }
  185. struct hfs_bnode *hfs_bnode_findhash(struct hfs_btree *tree, u32 cnid)
  186. {
  187. struct hfs_bnode *node;
  188. if (cnid >= tree->node_count) {
  189. pr_err("request for non-existent node %d in B*Tree\n", cnid);
  190. return NULL;
  191. }
  192. for (node = tree->node_hash[hfs_bnode_hash(cnid)];
  193. node; node = node->next_hash) {
  194. if (node->this == cnid) {
  195. return node;
  196. }
  197. }
  198. return NULL;
  199. }
  200. static struct hfs_bnode *__hfs_bnode_create(struct hfs_btree *tree, u32 cnid)
  201. {
  202. struct super_block *sb;
  203. struct hfs_bnode *node, *node2;
  204. struct address_space *mapping;
  205. struct page *page;
  206. int size, block, i, hash;
  207. loff_t off;
  208. if (cnid >= tree->node_count) {
  209. pr_err("request for non-existent node %d in B*Tree\n", cnid);
  210. return NULL;
  211. }
  212. sb = tree->inode->i_sb;
  213. size = sizeof(struct hfs_bnode) + tree->pages_per_bnode *
  214. sizeof(struct page *);
  215. node = kzalloc(size, GFP_KERNEL);
  216. if (!node)
  217. return NULL;
  218. node->tree = tree;
  219. node->this = cnid;
  220. set_bit(HFS_BNODE_NEW, &node->flags);
  221. atomic_set(&node->refcnt, 1);
  222. hfs_dbg(BNODE_REFS, "new_node(%d:%d): 1\n",
  223. node->tree->cnid, node->this);
  224. init_waitqueue_head(&node->lock_wq);
  225. spin_lock(&tree->hash_lock);
  226. node2 = hfs_bnode_findhash(tree, cnid);
  227. if (!node2) {
  228. hash = hfs_bnode_hash(cnid);
  229. node->next_hash = tree->node_hash[hash];
  230. tree->node_hash[hash] = node;
  231. tree->node_hash_cnt++;
  232. } else {
  233. spin_unlock(&tree->hash_lock);
  234. kfree(node);
  235. wait_event(node2->lock_wq, !test_bit(HFS_BNODE_NEW, &node2->flags));
  236. return node2;
  237. }
  238. spin_unlock(&tree->hash_lock);
  239. mapping = tree->inode->i_mapping;
  240. off = (loff_t)cnid * tree->node_size;
  241. block = off >> PAGE_SHIFT;
  242. node->page_offset = off & ~PAGE_MASK;
  243. for (i = 0; i < tree->pages_per_bnode; i++) {
  244. page = read_mapping_page(mapping, block++, NULL);
  245. if (IS_ERR(page))
  246. goto fail;
  247. if (PageError(page)) {
  248. put_page(page);
  249. goto fail;
  250. }
  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. hfs_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. put_page(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. if (node) {
  365. pr_crit("new node %u already hashed?\n", num);
  366. WARN_ON(1);
  367. return node;
  368. }
  369. node = __hfs_bnode_create(tree, num);
  370. if (!node)
  371. return ERR_PTR(-ENOMEM);
  372. if (test_bit(HFS_BNODE_ERROR, &node->flags)) {
  373. hfs_bnode_put(node);
  374. return ERR_PTR(-EIO);
  375. }
  376. pagep = node->page;
  377. memset(kmap(*pagep) + node->page_offset, 0,
  378. min((int)PAGE_SIZE, (int)tree->node_size));
  379. set_page_dirty(*pagep);
  380. kunmap(*pagep);
  381. for (i = 1; i < tree->pages_per_bnode; i++) {
  382. memset(kmap(*++pagep), 0, PAGE_SIZE);
  383. set_page_dirty(*pagep);
  384. kunmap(*pagep);
  385. }
  386. clear_bit(HFS_BNODE_NEW, &node->flags);
  387. wake_up(&node->lock_wq);
  388. return node;
  389. }
  390. void hfs_bnode_get(struct hfs_bnode *node)
  391. {
  392. if (node) {
  393. atomic_inc(&node->refcnt);
  394. hfs_dbg(BNODE_REFS, "get_node(%d:%d): %d\n",
  395. node->tree->cnid, node->this,
  396. atomic_read(&node->refcnt));
  397. }
  398. }
  399. /* Dispose of resources used by a node */
  400. void hfs_bnode_put(struct hfs_bnode *node)
  401. {
  402. if (node) {
  403. struct hfs_btree *tree = node->tree;
  404. int i;
  405. hfs_dbg(BNODE_REFS, "put_node(%d:%d): %d\n",
  406. node->tree->cnid, node->this,
  407. atomic_read(&node->refcnt));
  408. BUG_ON(!atomic_read(&node->refcnt));
  409. if (!atomic_dec_and_lock(&node->refcnt, &tree->hash_lock))
  410. return;
  411. for (i = 0; i < tree->pages_per_bnode; i++) {
  412. if (!node->page[i])
  413. continue;
  414. mark_page_accessed(node->page[i]);
  415. }
  416. if (test_bit(HFS_BNODE_DELETED, &node->flags)) {
  417. hfs_bnode_unhash(node);
  418. spin_unlock(&tree->hash_lock);
  419. hfs_bmap_free(node);
  420. hfs_bnode_free(node);
  421. return;
  422. }
  423. spin_unlock(&tree->hash_lock);
  424. }
  425. }