brec.c 13 KB

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