brec.c 14 KB

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