brec.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  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. printk(KERN_ERR "hfs: keylen %d too large\n",
  44. 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. printk(KERN_ERR "hfs: keylen %d too large\n",
  51. retval);
  52. retval = 0;
  53. }
  54. }
  55. }
  56. return retval;
  57. }
  58. int hfs_brec_insert(struct hfs_find_data *fd, void *entry, int entry_len)
  59. {
  60. struct hfs_btree *tree;
  61. struct hfs_bnode *node, *new_node;
  62. int size, key_len, rec;
  63. int data_off, end_off;
  64. int idx_rec_off, data_rec_off, end_rec_off;
  65. __be32 cnid;
  66. tree = fd->tree;
  67. if (!fd->bnode) {
  68. if (!tree->root)
  69. hfs_btree_inc_height(tree);
  70. fd->bnode = hfs_bnode_find(tree, tree->leaf_head);
  71. if (IS_ERR(fd->bnode))
  72. return PTR_ERR(fd->bnode);
  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. dprint(DBG_BNODE_MOD, "insert_rec: %d, %d, %d, %d\n", 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. if (new_node) {
  123. /* update parent key if we inserted a key
  124. * at the start of the first node
  125. */
  126. if (!rec && new_node != node)
  127. hfs_brec_update_parent(fd);
  128. hfs_bnode_put(fd->bnode);
  129. if (!new_node->parent) {
  130. hfs_btree_inc_height(tree);
  131. new_node->parent = tree->root;
  132. }
  133. fd->bnode = hfs_bnode_find(tree, new_node->parent);
  134. /* create index data entry */
  135. cnid = cpu_to_be32(new_node->this);
  136. entry = &cnid;
  137. entry_len = sizeof(cnid);
  138. /* get index key */
  139. hfs_bnode_read_key(new_node, fd->search_key, 14);
  140. __hfs_brec_find(fd->bnode, fd);
  141. hfs_bnode_put(new_node);
  142. new_node = NULL;
  143. if (tree->attributes & HFS_TREE_VARIDXKEYS)
  144. key_len = fd->search_key->key_len + 1;
  145. else {
  146. fd->search_key->key_len = tree->max_key_len;
  147. key_len = tree->max_key_len + 1;
  148. }
  149. goto again;
  150. }
  151. if (!rec)
  152. hfs_brec_update_parent(fd);
  153. return 0;
  154. }
  155. int hfs_brec_remove(struct hfs_find_data *fd)
  156. {
  157. struct hfs_btree *tree;
  158. struct hfs_bnode *node, *parent;
  159. int end_off, rec_off, data_off, size;
  160. tree = fd->tree;
  161. node = fd->bnode;
  162. again:
  163. rec_off = tree->node_size - (fd->record + 2) * 2;
  164. end_off = tree->node_size - (node->num_recs + 1) * 2;
  165. if (node->type == HFS_NODE_LEAF) {
  166. tree->leaf_count--;
  167. mark_inode_dirty(tree->inode);
  168. }
  169. hfs_bnode_dump(node);
  170. dprint(DBG_BNODE_MOD, "remove_rec: %d, %d\n", fd->record, fd->keylength + fd->entrylength);
  171. if (!--node->num_recs) {
  172. hfs_bnode_unlink(node);
  173. if (!node->parent)
  174. return 0;
  175. parent = hfs_bnode_find(tree, node->parent);
  176. if (IS_ERR(parent))
  177. return PTR_ERR(parent);
  178. hfs_bnode_put(node);
  179. node = fd->bnode = parent;
  180. __hfs_brec_find(node, fd);
  181. goto again;
  182. }
  183. hfs_bnode_write_u16(node, offsetof(struct hfs_bnode_desc, num_recs), node->num_recs);
  184. if (rec_off == end_off)
  185. goto skip;
  186. size = fd->keylength + fd->entrylength;
  187. do {
  188. data_off = hfs_bnode_read_u16(node, rec_off);
  189. hfs_bnode_write_u16(node, rec_off + 2, data_off - size);
  190. rec_off -= 2;
  191. } while (rec_off >= end_off);
  192. /* fill hole */
  193. hfs_bnode_move(node, fd->keyoffset, fd->keyoffset + size,
  194. data_off - fd->keyoffset - size);
  195. skip:
  196. hfs_bnode_dump(node);
  197. if (!fd->record)
  198. hfs_brec_update_parent(fd);
  199. return 0;
  200. }
  201. static struct hfs_bnode *hfs_bnode_split(struct hfs_find_data *fd)
  202. {
  203. struct hfs_btree *tree;
  204. struct hfs_bnode *node, *new_node, *next_node;
  205. struct hfs_bnode_desc node_desc;
  206. int num_recs, new_rec_off, new_off, old_rec_off;
  207. int data_start, data_end, size;
  208. tree = fd->tree;
  209. node = fd->bnode;
  210. new_node = hfs_bmap_alloc(tree);
  211. if (IS_ERR(new_node))
  212. return new_node;
  213. hfs_bnode_get(node);
  214. dprint(DBG_BNODE_MOD, "split_nodes: %d - %d - %d\n",
  215. node->this, new_node->this, node->next);
  216. new_node->next = node->next;
  217. new_node->prev = node->this;
  218. new_node->parent = node->parent;
  219. new_node->type = node->type;
  220. new_node->height = node->height;
  221. if (node->next)
  222. next_node = hfs_bnode_find(tree, node->next);
  223. else
  224. next_node = NULL;
  225. if (IS_ERR(next_node)) {
  226. hfs_bnode_put(node);
  227. hfs_bnode_put(new_node);
  228. return next_node;
  229. }
  230. size = tree->node_size / 2 - node->num_recs * 2 - 14;
  231. old_rec_off = tree->node_size - 4;
  232. num_recs = 1;
  233. for (;;) {
  234. data_start = hfs_bnode_read_u16(node, old_rec_off);
  235. if (data_start > size)
  236. break;
  237. old_rec_off -= 2;
  238. if (++num_recs < node->num_recs)
  239. continue;
  240. /* panic? */
  241. hfs_bnode_put(node);
  242. hfs_bnode_put(new_node);
  243. if (next_node)
  244. hfs_bnode_put(next_node);
  245. return ERR_PTR(-ENOSPC);
  246. }
  247. if (fd->record + 1 < num_recs) {
  248. /* new record is in the lower half,
  249. * so leave some more space there
  250. */
  251. old_rec_off += 2;
  252. num_recs--;
  253. data_start = hfs_bnode_read_u16(node, old_rec_off);
  254. } else {
  255. hfs_bnode_put(node);
  256. hfs_bnode_get(new_node);
  257. fd->bnode = new_node;
  258. fd->record -= num_recs;
  259. fd->keyoffset -= data_start - 14;
  260. fd->entryoffset -= data_start - 14;
  261. }
  262. new_node->num_recs = node->num_recs - num_recs;
  263. node->num_recs = num_recs;
  264. new_rec_off = tree->node_size - 2;
  265. new_off = 14;
  266. size = data_start - new_off;
  267. num_recs = new_node->num_recs;
  268. data_end = data_start;
  269. while (num_recs) {
  270. hfs_bnode_write_u16(new_node, new_rec_off, new_off);
  271. old_rec_off -= 2;
  272. new_rec_off -= 2;
  273. data_end = hfs_bnode_read_u16(node, old_rec_off);
  274. new_off = data_end - size;
  275. num_recs--;
  276. }
  277. hfs_bnode_write_u16(new_node, new_rec_off, new_off);
  278. hfs_bnode_copy(new_node, 14, node, data_start, data_end - data_start);
  279. /* update new bnode header */
  280. node_desc.next = cpu_to_be32(new_node->next);
  281. node_desc.prev = cpu_to_be32(new_node->prev);
  282. node_desc.type = new_node->type;
  283. node_desc.height = new_node->height;
  284. node_desc.num_recs = cpu_to_be16(new_node->num_recs);
  285. node_desc.reserved = 0;
  286. hfs_bnode_write(new_node, &node_desc, 0, sizeof(node_desc));
  287. /* update previous bnode header */
  288. node->next = new_node->this;
  289. hfs_bnode_read(node, &node_desc, 0, sizeof(node_desc));
  290. node_desc.next = cpu_to_be32(node->next);
  291. node_desc.num_recs = cpu_to_be16(node->num_recs);
  292. hfs_bnode_write(node, &node_desc, 0, sizeof(node_desc));
  293. /* update next bnode header */
  294. if (next_node) {
  295. next_node->prev = new_node->this;
  296. hfs_bnode_read(next_node, &node_desc, 0, sizeof(node_desc));
  297. node_desc.prev = cpu_to_be32(next_node->prev);
  298. hfs_bnode_write(next_node, &node_desc, 0, sizeof(node_desc));
  299. hfs_bnode_put(next_node);
  300. } else if (node->this == tree->leaf_tail) {
  301. /* if there is no next node, this might be the new tail */
  302. tree->leaf_tail = new_node->this;
  303. mark_inode_dirty(tree->inode);
  304. }
  305. hfs_bnode_dump(node);
  306. hfs_bnode_dump(new_node);
  307. hfs_bnode_put(node);
  308. return new_node;
  309. }
  310. static int hfs_brec_update_parent(struct hfs_find_data *fd)
  311. {
  312. struct hfs_btree *tree;
  313. struct hfs_bnode *node, *new_node, *parent;
  314. int newkeylen, diff;
  315. int rec, rec_off, end_rec_off;
  316. int start_off, end_off;
  317. tree = fd->tree;
  318. node = fd->bnode;
  319. new_node = NULL;
  320. if (!node->parent)
  321. return 0;
  322. again:
  323. parent = hfs_bnode_find(tree, node->parent);
  324. if (IS_ERR(parent))
  325. return PTR_ERR(parent);
  326. __hfs_brec_find(parent, fd);
  327. hfs_bnode_dump(parent);
  328. rec = fd->record;
  329. /* size difference between old and new key */
  330. if (tree->attributes & HFS_TREE_VARIDXKEYS)
  331. newkeylen = (hfs_bnode_read_u8(node, 14) | 1) + 1;
  332. else
  333. fd->keylength = newkeylen = tree->max_key_len + 1;
  334. dprint(DBG_BNODE_MOD, "update_rec: %d, %d, %d\n", rec, fd->keylength, newkeylen);
  335. rec_off = tree->node_size - (rec + 2) * 2;
  336. end_rec_off = tree->node_size - (parent->num_recs + 1) * 2;
  337. diff = newkeylen - fd->keylength;
  338. if (!diff)
  339. goto skip;
  340. if (diff > 0) {
  341. end_off = hfs_bnode_read_u16(parent, end_rec_off);
  342. if (end_rec_off - end_off < diff) {
  343. printk(KERN_DEBUG "hfs: splitting index node...\n");
  344. fd->bnode = parent;
  345. new_node = hfs_bnode_split(fd);
  346. if (IS_ERR(new_node))
  347. return PTR_ERR(new_node);
  348. parent = fd->bnode;
  349. rec = fd->record;
  350. rec_off = tree->node_size - (rec + 2) * 2;
  351. end_rec_off = tree->node_size - (parent->num_recs + 1) * 2;
  352. }
  353. }
  354. end_off = start_off = hfs_bnode_read_u16(parent, rec_off);
  355. hfs_bnode_write_u16(parent, rec_off, start_off + diff);
  356. start_off -= 4; /* move previous cnid too */
  357. while (rec_off > end_rec_off) {
  358. rec_off -= 2;
  359. end_off = hfs_bnode_read_u16(parent, rec_off);
  360. hfs_bnode_write_u16(parent, rec_off, end_off + diff);
  361. }
  362. hfs_bnode_move(parent, start_off + diff, start_off,
  363. end_off - start_off);
  364. skip:
  365. hfs_bnode_copy(parent, fd->keyoffset, node, 14, newkeylen);
  366. if (!(tree->attributes & HFS_TREE_VARIDXKEYS))
  367. hfs_bnode_write_u8(parent, fd->keyoffset, newkeylen - 1);
  368. hfs_bnode_dump(parent);
  369. hfs_bnode_put(node);
  370. node = parent;
  371. if (new_node) {
  372. __be32 cnid;
  373. fd->bnode = hfs_bnode_find(tree, new_node->parent);
  374. /* create index key and entry */
  375. hfs_bnode_read_key(new_node, fd->search_key, 14);
  376. cnid = cpu_to_be32(new_node->this);
  377. __hfs_brec_find(fd->bnode, fd);
  378. hfs_brec_insert(fd, &cnid, sizeof(cnid));
  379. hfs_bnode_put(fd->bnode);
  380. hfs_bnode_put(new_node);
  381. if (!rec) {
  382. if (new_node == node)
  383. goto out;
  384. /* restore search_key */
  385. hfs_bnode_read_key(node, fd->search_key, 14);
  386. }
  387. }
  388. if (!rec && node->parent)
  389. goto again;
  390. out:
  391. fd->bnode = node;
  392. return 0;
  393. }
  394. static int hfs_btree_inc_height(struct hfs_btree *tree)
  395. {
  396. struct hfs_bnode *node, *new_node;
  397. struct hfs_bnode_desc node_desc;
  398. int key_size, rec;
  399. __be32 cnid;
  400. node = NULL;
  401. if (tree->root) {
  402. node = hfs_bnode_find(tree, tree->root);
  403. if (IS_ERR(node))
  404. return PTR_ERR(node);
  405. }
  406. new_node = hfs_bmap_alloc(tree);
  407. if (IS_ERR(new_node)) {
  408. hfs_bnode_put(node);
  409. return PTR_ERR(new_node);
  410. }
  411. tree->root = new_node->this;
  412. if (!tree->depth) {
  413. tree->leaf_head = tree->leaf_tail = new_node->this;
  414. new_node->type = HFS_NODE_LEAF;
  415. new_node->num_recs = 0;
  416. } else {
  417. new_node->type = HFS_NODE_INDEX;
  418. new_node->num_recs = 1;
  419. }
  420. new_node->parent = 0;
  421. new_node->next = 0;
  422. new_node->prev = 0;
  423. new_node->height = ++tree->depth;
  424. node_desc.next = cpu_to_be32(new_node->next);
  425. node_desc.prev = cpu_to_be32(new_node->prev);
  426. node_desc.type = new_node->type;
  427. node_desc.height = new_node->height;
  428. node_desc.num_recs = cpu_to_be16(new_node->num_recs);
  429. node_desc.reserved = 0;
  430. hfs_bnode_write(new_node, &node_desc, 0, sizeof(node_desc));
  431. rec = tree->node_size - 2;
  432. hfs_bnode_write_u16(new_node, rec, 14);
  433. if (node) {
  434. /* insert old root idx into new root */
  435. node->parent = tree->root;
  436. if (node->type == HFS_NODE_LEAF ||
  437. tree->attributes & HFS_TREE_VARIDXKEYS)
  438. key_size = hfs_bnode_read_u8(node, 14) + 1;
  439. else
  440. key_size = tree->max_key_len + 1;
  441. hfs_bnode_copy(new_node, 14, node, 14, key_size);
  442. if (!(tree->attributes & HFS_TREE_VARIDXKEYS)) {
  443. key_size = tree->max_key_len + 1;
  444. hfs_bnode_write_u8(new_node, 14, tree->max_key_len);
  445. }
  446. key_size = (key_size + 1) & -2;
  447. cnid = cpu_to_be32(node->this);
  448. hfs_bnode_write(new_node, &cnid, 14 + key_size, 4);
  449. rec -= 2;
  450. hfs_bnode_write_u16(new_node, rec, 14 + key_size + 4);
  451. hfs_bnode_put(node);
  452. }
  453. hfs_bnode_put(new_node);
  454. mark_inode_dirty(tree->inode);
  455. return 0;
  456. }