bnode.c 11 KB

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