brec.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * linux/fs/hfs/brec.c
  4. *
  5. * Copyright (C) 2001
  6. * Brad Boyer (flar@allandria.com)
  7. * (C) 2003 Ardis Technologies <roman@ardistech.com>
  8. *
  9. * Handle individual btree records
  10. */
  11. #include "btree.h"
  12. static struct hfs_bnode *hfs_bnode_split(struct hfs_find_data *fd);
  13. static int hfs_brec_update_parent(struct hfs_find_data *fd);
  14. static int hfs_btree_inc_height(struct hfs_btree *tree);
  15. /* Get the length and offset of the given record in the given node */
  16. u16 hfs_brec_lenoff(struct hfs_bnode *node, u16 rec, u16 *off)
  17. {
  18. __be16 retval[2];
  19. u16 dataoff;
  20. dataoff = node->tree->node_size - (rec + 2) * 2;
  21. hfs_bnode_read(node, retval, dataoff, 4);
  22. *off = be16_to_cpu(retval[1]);
  23. return be16_to_cpu(retval[0]) - *off;
  24. }
  25. /* Get the length of the key from a keyed record */
  26. u16 hfs_brec_keylen(struct hfs_bnode *node, u16 rec)
  27. {
  28. u16 retval, recoff;
  29. if (node->type != HFS_NODE_INDEX && node->type != HFS_NODE_LEAF)
  30. return 0;
  31. if ((node->type == HFS_NODE_INDEX) &&
  32. !(node->tree->attributes & HFS_TREE_VARIDXKEYS)) {
  33. if (node->tree->attributes & HFS_TREE_BIGKEYS)
  34. retval = node->tree->max_key_len + 2;
  35. else
  36. retval = node->tree->max_key_len + 1;
  37. } else {
  38. recoff = hfs_bnode_read_u16(node, node->tree->node_size - (rec + 1) * 2);
  39. if (!recoff)
  40. return 0;
  41. if (node->tree->attributes & HFS_TREE_BIGKEYS) {
  42. retval = hfs_bnode_read_u16(node, recoff) + 2;
  43. if (retval > node->tree->max_key_len + 2) {
  44. pr_err("keylen %d too large\n", retval);
  45. retval = 0;
  46. }
  47. } else {
  48. retval = (hfs_bnode_read_u8(node, recoff) | 1) + 1;
  49. if (retval > node->tree->max_key_len + 1) {
  50. pr_err("keylen %d too large\n", retval);
  51. retval = 0;
  52. }
  53. }
  54. }
  55. return retval;
  56. }
  57. int hfs_brec_insert(struct hfs_find_data *fd, void *entry, int entry_len)
  58. {
  59. struct hfs_btree *tree;
  60. struct hfs_bnode *node, *new_node;
  61. int size, key_len, rec;
  62. int data_off, end_off;
  63. int idx_rec_off, data_rec_off, end_rec_off;
  64. __be32 cnid;
  65. tree = fd->tree;
  66. if (!fd->bnode) {
  67. if (!tree->root)
  68. hfs_btree_inc_height(tree);
  69. node = hfs_bnode_find(tree, tree->leaf_head);
  70. if (IS_ERR(node))
  71. return PTR_ERR(node);
  72. fd->bnode = node;
  73. fd->record = -1;
  74. }
  75. new_node = NULL;
  76. key_len = (fd->search_key->key_len | 1) + 1;
  77. again:
  78. /* new record idx and complete record size */
  79. rec = fd->record + 1;
  80. size = key_len + entry_len;
  81. node = fd->bnode;
  82. hfs_bnode_dump(node);
  83. /* get last offset */
  84. end_rec_off = tree->node_size - (node->num_recs + 1) * 2;
  85. end_off = hfs_bnode_read_u16(node, end_rec_off);
  86. end_rec_off -= 2;
  87. hfs_dbg(BNODE_MOD, "insert_rec: %d, %d, %d, %d\n",
  88. rec, size, end_off, end_rec_off);
  89. if (size > end_rec_off - end_off) {
  90. if (new_node)
  91. panic("not enough room!\n");
  92. new_node = hfs_bnode_split(fd);
  93. if (IS_ERR(new_node))
  94. return PTR_ERR(new_node);
  95. goto again;
  96. }
  97. if (node->type == HFS_NODE_LEAF) {
  98. tree->leaf_count++;
  99. mark_inode_dirty(tree->inode);
  100. }
  101. node->num_recs++;
  102. /* write new last offset */
  103. hfs_bnode_write_u16(node, offsetof(struct hfs_bnode_desc, num_recs), node->num_recs);
  104. hfs_bnode_write_u16(node, end_rec_off, end_off + size);
  105. data_off = end_off;
  106. data_rec_off = end_rec_off + 2;
  107. idx_rec_off = tree->node_size - (rec + 1) * 2;
  108. if (idx_rec_off == data_rec_off)
  109. goto skip;
  110. /* move all following entries */
  111. do {
  112. data_off = hfs_bnode_read_u16(node, data_rec_off + 2);
  113. hfs_bnode_write_u16(node, data_rec_off, data_off + size);
  114. data_rec_off += 2;
  115. } while (data_rec_off < idx_rec_off);
  116. /* move data away */
  117. hfs_bnode_move(node, data_off + size, data_off,
  118. end_off - data_off);
  119. skip:
  120. hfs_bnode_write(node, fd->search_key, data_off, key_len);
  121. hfs_bnode_write(node, entry, data_off + key_len, entry_len);
  122. hfs_bnode_dump(node);
  123. /*
  124. * update parent key if we inserted a key
  125. * at the start of the node and it is not the new node
  126. */
  127. if (!rec && new_node != node) {
  128. hfs_bnode_read_key(node, fd->search_key, data_off + size);
  129. hfs_brec_update_parent(fd);
  130. }
  131. if (new_node) {
  132. hfs_bnode_put(fd->bnode);
  133. if (!new_node->parent) {
  134. hfs_btree_inc_height(tree);
  135. new_node->parent = tree->root;
  136. }
  137. fd->bnode = hfs_bnode_find(tree, new_node->parent);
  138. /* create index data entry */
  139. cnid = cpu_to_be32(new_node->this);
  140. entry = &cnid;
  141. entry_len = sizeof(cnid);
  142. /* get index key */
  143. hfs_bnode_read_key(new_node, fd->search_key, 14);
  144. __hfs_brec_find(fd->bnode, fd);
  145. hfs_bnode_put(new_node);
  146. new_node = NULL;
  147. if (tree->attributes & HFS_TREE_VARIDXKEYS)
  148. key_len = fd->search_key->key_len + 1;
  149. else {
  150. fd->search_key->key_len = tree->max_key_len;
  151. key_len = tree->max_key_len + 1;
  152. }
  153. goto again;
  154. }
  155. return 0;
  156. }
  157. int hfs_brec_remove(struct hfs_find_data *fd)
  158. {
  159. struct hfs_btree *tree;
  160. struct hfs_bnode *node, *parent;
  161. int end_off, rec_off, data_off, size;
  162. tree = fd->tree;
  163. node = fd->bnode;
  164. again:
  165. rec_off = tree->node_size - (fd->record + 2) * 2;
  166. end_off = tree->node_size - (node->num_recs + 1) * 2;
  167. if (node->type == HFS_NODE_LEAF) {
  168. tree->leaf_count--;
  169. mark_inode_dirty(tree->inode);
  170. }
  171. hfs_bnode_dump(node);
  172. hfs_dbg(BNODE_MOD, "remove_rec: %d, %d\n",
  173. fd->record, fd->keylength + fd->entrylength);
  174. if (!--node->num_recs) {
  175. hfs_bnode_unlink(node);
  176. if (!node->parent)
  177. return 0;
  178. parent = hfs_bnode_find(tree, node->parent);
  179. if (IS_ERR(parent))
  180. return PTR_ERR(parent);
  181. hfs_bnode_put(node);
  182. node = fd->bnode = parent;
  183. __hfs_brec_find(node, fd);
  184. goto again;
  185. }
  186. hfs_bnode_write_u16(node, offsetof(struct hfs_bnode_desc, num_recs), node->num_recs);
  187. if (rec_off == end_off)
  188. goto skip;
  189. size = fd->keylength + fd->entrylength;
  190. do {
  191. data_off = hfs_bnode_read_u16(node, rec_off);
  192. hfs_bnode_write_u16(node, rec_off + 2, data_off - size);
  193. rec_off -= 2;
  194. } while (rec_off >= end_off);
  195. /* fill hole */
  196. hfs_bnode_move(node, fd->keyoffset, fd->keyoffset + size,
  197. data_off - fd->keyoffset - size);
  198. skip:
  199. hfs_bnode_dump(node);
  200. if (!fd->record)
  201. hfs_brec_update_parent(fd);
  202. return 0;
  203. }
  204. static struct hfs_bnode *hfs_bnode_split(struct hfs_find_data *fd)
  205. {
  206. struct hfs_btree *tree;
  207. struct hfs_bnode *node, *new_node, *next_node;
  208. struct hfs_bnode_desc node_desc;
  209. int num_recs, new_rec_off, new_off, old_rec_off;
  210. int data_start, data_end, size;
  211. tree = fd->tree;
  212. node = fd->bnode;
  213. new_node = hfs_bmap_alloc(tree);
  214. if (IS_ERR(new_node))
  215. return new_node;
  216. hfs_bnode_get(node);
  217. hfs_dbg(BNODE_MOD, "split_nodes: %d - %d - %d\n",
  218. node->this, new_node->this, node->next);
  219. new_node->next = node->next;
  220. new_node->prev = node->this;
  221. new_node->parent = node->parent;
  222. new_node->type = node->type;
  223. new_node->height = node->height;
  224. if (node->next)
  225. next_node = hfs_bnode_find(tree, node->next);
  226. else
  227. next_node = NULL;
  228. if (IS_ERR(next_node)) {
  229. hfs_bnode_put(node);
  230. hfs_bnode_put(new_node);
  231. return next_node;
  232. }
  233. size = tree->node_size / 2 - node->num_recs * 2 - 14;
  234. old_rec_off = tree->node_size - 4;
  235. num_recs = 1;
  236. for (;;) {
  237. data_start = hfs_bnode_read_u16(node, old_rec_off);
  238. if (data_start > size)
  239. break;
  240. old_rec_off -= 2;
  241. if (++num_recs < node->num_recs)
  242. continue;
  243. /* panic? */
  244. hfs_bnode_put(node);
  245. hfs_bnode_put(new_node);
  246. if (next_node)
  247. hfs_bnode_put(next_node);
  248. return ERR_PTR(-ENOSPC);
  249. }
  250. if (fd->record + 1 < num_recs) {
  251. /* new record is in the lower half,
  252. * so leave some more space there
  253. */
  254. old_rec_off += 2;
  255. num_recs--;
  256. data_start = hfs_bnode_read_u16(node, old_rec_off);
  257. } else {
  258. hfs_bnode_put(node);
  259. hfs_bnode_get(new_node);
  260. fd->bnode = new_node;
  261. fd->record -= num_recs;
  262. fd->keyoffset -= data_start - 14;
  263. fd->entryoffset -= data_start - 14;
  264. }
  265. new_node->num_recs = node->num_recs - num_recs;
  266. node->num_recs = num_recs;
  267. new_rec_off = tree->node_size - 2;
  268. new_off = 14;
  269. size = data_start - new_off;
  270. num_recs = new_node->num_recs;
  271. data_end = data_start;
  272. while (num_recs) {
  273. hfs_bnode_write_u16(new_node, new_rec_off, new_off);
  274. old_rec_off -= 2;
  275. new_rec_off -= 2;
  276. data_end = hfs_bnode_read_u16(node, old_rec_off);
  277. new_off = data_end - size;
  278. num_recs--;
  279. }
  280. hfs_bnode_write_u16(new_node, new_rec_off, new_off);
  281. hfs_bnode_copy(new_node, 14, node, data_start, data_end - data_start);
  282. /* update new bnode header */
  283. node_desc.next = cpu_to_be32(new_node->next);
  284. node_desc.prev = cpu_to_be32(new_node->prev);
  285. node_desc.type = new_node->type;
  286. node_desc.height = new_node->height;
  287. node_desc.num_recs = cpu_to_be16(new_node->num_recs);
  288. node_desc.reserved = 0;
  289. hfs_bnode_write(new_node, &node_desc, 0, sizeof(node_desc));
  290. /* update previous bnode header */
  291. node->next = new_node->this;
  292. hfs_bnode_read(node, &node_desc, 0, sizeof(node_desc));
  293. node_desc.next = cpu_to_be32(node->next);
  294. node_desc.num_recs = cpu_to_be16(node->num_recs);
  295. hfs_bnode_write(node, &node_desc, 0, sizeof(node_desc));
  296. /* update next bnode header */
  297. if (next_node) {
  298. next_node->prev = new_node->this;
  299. hfs_bnode_read(next_node, &node_desc, 0, sizeof(node_desc));
  300. node_desc.prev = cpu_to_be32(next_node->prev);
  301. hfs_bnode_write(next_node, &node_desc, 0, sizeof(node_desc));
  302. hfs_bnode_put(next_node);
  303. } else if (node->this == tree->leaf_tail) {
  304. /* if there is no next node, this might be the new tail */
  305. tree->leaf_tail = new_node->this;
  306. mark_inode_dirty(tree->inode);
  307. }
  308. hfs_bnode_dump(node);
  309. hfs_bnode_dump(new_node);
  310. hfs_bnode_put(node);
  311. return new_node;
  312. }
  313. static int hfs_brec_update_parent(struct hfs_find_data *fd)
  314. {
  315. struct hfs_btree *tree;
  316. struct hfs_bnode *node, *new_node, *parent;
  317. int newkeylen, diff;
  318. int rec, rec_off, end_rec_off;
  319. int start_off, end_off;
  320. tree = fd->tree;
  321. node = fd->bnode;
  322. new_node = NULL;
  323. if (!node->parent)
  324. return 0;
  325. again:
  326. parent = hfs_bnode_find(tree, node->parent);
  327. if (IS_ERR(parent))
  328. return PTR_ERR(parent);
  329. __hfs_brec_find(parent, fd);
  330. if (fd->record < 0)
  331. return -ENOENT;
  332. hfs_bnode_dump(parent);
  333. rec = fd->record;
  334. /* size difference between old and new key */
  335. if (tree->attributes & HFS_TREE_VARIDXKEYS)
  336. newkeylen = (hfs_bnode_read_u8(node, 14) | 1) + 1;
  337. else
  338. fd->keylength = newkeylen = tree->max_key_len + 1;
  339. hfs_dbg(BNODE_MOD, "update_rec: %d, %d, %d\n",
  340. rec, fd->keylength, newkeylen);
  341. rec_off = tree->node_size - (rec + 2) * 2;
  342. end_rec_off = tree->node_size - (parent->num_recs + 1) * 2;
  343. diff = newkeylen - fd->keylength;
  344. if (!diff)
  345. goto skip;
  346. if (diff > 0) {
  347. end_off = hfs_bnode_read_u16(parent, end_rec_off);
  348. if (end_rec_off - end_off < diff) {
  349. printk(KERN_DEBUG "splitting index node...\n");
  350. fd->bnode = parent;
  351. new_node = hfs_bnode_split(fd);
  352. if (IS_ERR(new_node))
  353. return PTR_ERR(new_node);
  354. parent = fd->bnode;
  355. rec = fd->record;
  356. rec_off = tree->node_size - (rec + 2) * 2;
  357. end_rec_off = tree->node_size - (parent->num_recs + 1) * 2;
  358. }
  359. }
  360. end_off = start_off = hfs_bnode_read_u16(parent, rec_off);
  361. hfs_bnode_write_u16(parent, rec_off, start_off + diff);
  362. start_off -= 4; /* move previous cnid too */
  363. while (rec_off > end_rec_off) {
  364. rec_off -= 2;
  365. end_off = hfs_bnode_read_u16(parent, rec_off);
  366. hfs_bnode_write_u16(parent, rec_off, end_off + diff);
  367. }
  368. hfs_bnode_move(parent, start_off + diff, start_off,
  369. end_off - start_off);
  370. skip:
  371. hfs_bnode_copy(parent, fd->keyoffset, node, 14, newkeylen);
  372. if (!(tree->attributes & HFS_TREE_VARIDXKEYS))
  373. hfs_bnode_write_u8(parent, fd->keyoffset, newkeylen - 1);
  374. hfs_bnode_dump(parent);
  375. hfs_bnode_put(node);
  376. node = parent;
  377. if (new_node) {
  378. __be32 cnid;
  379. if (!new_node->parent) {
  380. hfs_btree_inc_height(tree);
  381. new_node->parent = tree->root;
  382. }
  383. fd->bnode = hfs_bnode_find(tree, new_node->parent);
  384. /* create index key and entry */
  385. hfs_bnode_read_key(new_node, fd->search_key, 14);
  386. cnid = cpu_to_be32(new_node->this);
  387. __hfs_brec_find(fd->bnode, fd);
  388. hfs_brec_insert(fd, &cnid, sizeof(cnid));
  389. hfs_bnode_put(fd->bnode);
  390. hfs_bnode_put(new_node);
  391. if (!rec) {
  392. if (new_node == node)
  393. goto out;
  394. /* restore search_key */
  395. hfs_bnode_read_key(node, fd->search_key, 14);
  396. }
  397. new_node = NULL;
  398. }
  399. if (!rec && node->parent)
  400. goto again;
  401. out:
  402. fd->bnode = node;
  403. return 0;
  404. }
  405. static int hfs_btree_inc_height(struct hfs_btree *tree)
  406. {
  407. struct hfs_bnode *node, *new_node;
  408. struct hfs_bnode_desc node_desc;
  409. int key_size, rec;
  410. __be32 cnid;
  411. node = NULL;
  412. if (tree->root) {
  413. node = hfs_bnode_find(tree, tree->root);
  414. if (IS_ERR(node))
  415. return PTR_ERR(node);
  416. }
  417. new_node = hfs_bmap_alloc(tree);
  418. if (IS_ERR(new_node)) {
  419. hfs_bnode_put(node);
  420. return PTR_ERR(new_node);
  421. }
  422. tree->root = new_node->this;
  423. if (!tree->depth) {
  424. tree->leaf_head = tree->leaf_tail = new_node->this;
  425. new_node->type = HFS_NODE_LEAF;
  426. new_node->num_recs = 0;
  427. } else {
  428. new_node->type = HFS_NODE_INDEX;
  429. new_node->num_recs = 1;
  430. }
  431. new_node->parent = 0;
  432. new_node->next = 0;
  433. new_node->prev = 0;
  434. new_node->height = ++tree->depth;
  435. node_desc.next = cpu_to_be32(new_node->next);
  436. node_desc.prev = cpu_to_be32(new_node->prev);
  437. node_desc.type = new_node->type;
  438. node_desc.height = new_node->height;
  439. node_desc.num_recs = cpu_to_be16(new_node->num_recs);
  440. node_desc.reserved = 0;
  441. hfs_bnode_write(new_node, &node_desc, 0, sizeof(node_desc));
  442. rec = tree->node_size - 2;
  443. hfs_bnode_write_u16(new_node, rec, 14);
  444. if (node) {
  445. /* insert old root idx into new root */
  446. node->parent = tree->root;
  447. if (node->type == HFS_NODE_LEAF ||
  448. tree->attributes & HFS_TREE_VARIDXKEYS)
  449. key_size = hfs_bnode_read_u8(node, 14) + 1;
  450. else
  451. key_size = tree->max_key_len + 1;
  452. hfs_bnode_copy(new_node, 14, node, 14, key_size);
  453. if (!(tree->attributes & HFS_TREE_VARIDXKEYS)) {
  454. key_size = tree->max_key_len + 1;
  455. hfs_bnode_write_u8(new_node, 14, tree->max_key_len);
  456. }
  457. key_size = (key_size + 1) & -2;
  458. cnid = cpu_to_be32(node->this);
  459. hfs_bnode_write(new_node, &cnid, 14 + key_size, 4);
  460. rec -= 2;
  461. hfs_bnode_write_u16(new_node, rec, 14 + key_size + 4);
  462. hfs_bnode_put(node);
  463. }
  464. hfs_bnode_put(new_node);
  465. mark_inode_dirty(tree->inode);
  466. return 0;
  467. }