brec.c 13 KB

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