blob: 754fdf8c6356388f16bbdc285240394b350bd90e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
11#include "hfsplus_fs.h"
12#include "hfsplus_raw.h"
13
14static struct hfs_bnode *hfs_bnode_split(struct hfs_find_data *fd);
15static int hfs_brec_update_parent(struct hfs_find_data *fd);
16static int hfs_btree_inc_height(struct hfs_btree *);
17
18/* Get the length and offset of the given record in the given node */
19u16 hfs_brec_lenoff(struct hfs_bnode *node, u16 rec, u16 *off)
20{
21 __be16 retval[2];
22 u16 dataoff;
23
24 dataoff = node->tree->node_size - (rec + 2) * 2;
25 hfs_bnode_read(node, retval, dataoff, 4);
26 *off = be16_to_cpu(retval[1]);
27 return be16_to_cpu(retval[0]) - *off;
28}
29
30/* Get the length of the key from a keyed record */
31u16 hfs_brec_keylen(struct hfs_bnode *node, u16 rec)
32{
33 u16 retval, recoff;
34
35 if (node->type != HFS_NODE_INDEX && node->type != HFS_NODE_LEAF)
36 return 0;
37
38 if ((node->type == HFS_NODE_INDEX) &&
Vyacheslav Dubeyko324ef392013-02-27 17:03:04 -080039 !(node->tree->attributes & HFS_TREE_VARIDXKEYS) &&
40 (node->tree->cnid != HFSPLUS_ATTR_CNID)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 retval = node->tree->max_key_len + 2;
42 } else {
Anton Salikhmetov2753cc22010-12-16 18:08:38 +020043 recoff = hfs_bnode_read_u16(node,
44 node->tree->node_size - (rec + 1) * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 if (!recoff)
46 return 0;
Naohiro Aotaaac4e4192011-07-12 02:54:13 +090047 if (recoff > node->tree->node_size - 2) {
Joe Perchesd6142672013-04-30 15:27:55 -070048 pr_err("recoff %d too large\n", recoff);
Naohiro Aotaaac4e4192011-07-12 02:54:13 +090049 return 0;
50 }
Christoph Hellwig13571a62010-10-14 09:54:23 -040051
52 retval = hfs_bnode_read_u16(node, recoff) + 2;
53 if (retval > node->tree->max_key_len + 2) {
Joe Perchesd6142672013-04-30 15:27:55 -070054 pr_err("keylen %d too large\n",
Christoph Hellwig13571a62010-10-14 09:54:23 -040055 retval);
56 retval = 0;
Eric Sandeen9250f922010-10-14 09:53:48 -040057 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 }
59 return retval;
60}
61
62int hfs_brec_insert(struct hfs_find_data *fd, void *entry, int entry_len)
63{
64 struct hfs_btree *tree;
65 struct hfs_bnode *node, *new_node;
66 int size, key_len, rec;
67 int data_off, end_off;
68 int idx_rec_off, data_rec_off, end_rec_off;
69 __be32 cnid;
70
71 tree = fd->tree;
72 if (!fd->bnode) {
73 if (!tree->root)
74 hfs_btree_inc_height(tree);
75 fd->bnode = hfs_bnode_find(tree, tree->leaf_head);
76 if (IS_ERR(fd->bnode))
77 return PTR_ERR(fd->bnode);
78 fd->record = -1;
79 }
80 new_node = NULL;
81 key_len = be16_to_cpu(fd->search_key->key_len) + 2;
82again:
83 /* new record idx and complete record size */
84 rec = fd->record + 1;
85 size = key_len + entry_len;
86
87 node = fd->bnode;
88 hfs_bnode_dump(node);
89 /* get last offset */
90 end_rec_off = tree->node_size - (node->num_recs + 1) * 2;
91 end_off = hfs_bnode_read_u16(node, end_rec_off);
92 end_rec_off -= 2;
Joe Perchesc2b3e1f2013-04-30 15:27:54 -070093 hfs_dbg(BNODE_MOD, "insert_rec: %d, %d, %d, %d\n",
Anton Salikhmetov2753cc22010-12-16 18:08:38 +020094 rec, size, end_off, end_rec_off);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 if (size > end_rec_off - end_off) {
96 if (new_node)
97 panic("not enough room!\n");
98 new_node = hfs_bnode_split(fd);
99 if (IS_ERR(new_node))
100 return PTR_ERR(new_node);
101 goto again;
102 }
103 if (node->type == HFS_NODE_LEAF) {
104 tree->leaf_count++;
105 mark_inode_dirty(tree->inode);
106 }
107 node->num_recs++;
108 /* write new last offset */
Anton Salikhmetov2753cc22010-12-16 18:08:38 +0200109 hfs_bnode_write_u16(node,
110 offsetof(struct hfs_bnode_desc, num_recs),
111 node->num_recs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 hfs_bnode_write_u16(node, end_rec_off, end_off + size);
113 data_off = end_off;
114 data_rec_off = end_rec_off + 2;
115 idx_rec_off = tree->node_size - (rec + 1) * 2;
116 if (idx_rec_off == data_rec_off)
117 goto skip;
118 /* move all following entries */
119 do {
120 data_off = hfs_bnode_read_u16(node, data_rec_off + 2);
121 hfs_bnode_write_u16(node, data_rec_off, data_off + size);
122 data_rec_off += 2;
123 } while (data_rec_off < idx_rec_off);
124
125 /* move data away */
126 hfs_bnode_move(node, data_off + size, data_off,
127 end_off - data_off);
128
129skip:
130 hfs_bnode_write(node, fd->search_key, data_off, key_len);
131 hfs_bnode_write(node, entry, data_off + key_len, entry_len);
132 hfs_bnode_dump(node);
133
Sergei Antonov98cf21c2015-03-25 15:55:34 -0700134 /*
135 * update parent key if we inserted a key
136 * at the start of the node and it is not the new node
137 */
138 if (!rec && new_node != node) {
139 hfs_bnode_read_key(node, fd->search_key, data_off + size);
140 hfs_brec_update_parent(fd);
141 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
Sergei Antonov98cf21c2015-03-25 15:55:34 -0700143 if (new_node) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 hfs_bnode_put(fd->bnode);
145 if (!new_node->parent) {
146 hfs_btree_inc_height(tree);
147 new_node->parent = tree->root;
148 }
149 fd->bnode = hfs_bnode_find(tree, new_node->parent);
150
151 /* create index data entry */
152 cnid = cpu_to_be32(new_node->this);
153 entry = &cnid;
154 entry_len = sizeof(cnid);
155
156 /* get index key */
157 hfs_bnode_read_key(new_node, fd->search_key, 14);
Vyacheslav Dubeyko324ef392013-02-27 17:03:04 -0800158 __hfs_brec_find(fd->bnode, fd, hfs_find_rec_by_key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
160 hfs_bnode_put(new_node);
161 new_node = NULL;
162
Vyacheslav Dubeyko324ef392013-02-27 17:03:04 -0800163 if ((tree->attributes & HFS_TREE_VARIDXKEYS) ||
164 (tree->cnid == HFSPLUS_ATTR_CNID))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 key_len = be16_to_cpu(fd->search_key->key_len) + 2;
166 else {
Anton Salikhmetov2753cc22010-12-16 18:08:38 +0200167 fd->search_key->key_len =
168 cpu_to_be16(tree->max_key_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 key_len = tree->max_key_len + 2;
170 }
171 goto again;
172 }
173
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 return 0;
175}
176
177int hfs_brec_remove(struct hfs_find_data *fd)
178{
179 struct hfs_btree *tree;
180 struct hfs_bnode *node, *parent;
181 int end_off, rec_off, data_off, size;
182
183 tree = fd->tree;
184 node = fd->bnode;
185again:
186 rec_off = tree->node_size - (fd->record + 2) * 2;
187 end_off = tree->node_size - (node->num_recs + 1) * 2;
188
189 if (node->type == HFS_NODE_LEAF) {
190 tree->leaf_count--;
191 mark_inode_dirty(tree->inode);
192 }
193 hfs_bnode_dump(node);
Joe Perchesc2b3e1f2013-04-30 15:27:54 -0700194 hfs_dbg(BNODE_MOD, "remove_rec: %d, %d\n",
Anton Salikhmetov2753cc22010-12-16 18:08:38 +0200195 fd->record, fd->keylength + fd->entrylength);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 if (!--node->num_recs) {
197 hfs_bnode_unlink(node);
198 if (!node->parent)
199 return 0;
200 parent = hfs_bnode_find(tree, node->parent);
201 if (IS_ERR(parent))
202 return PTR_ERR(parent);
203 hfs_bnode_put(node);
204 node = fd->bnode = parent;
205
Vyacheslav Dubeyko324ef392013-02-27 17:03:04 -0800206 __hfs_brec_find(node, fd, hfs_find_rec_by_key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 goto again;
208 }
Anton Salikhmetov2753cc22010-12-16 18:08:38 +0200209 hfs_bnode_write_u16(node,
210 offsetof(struct hfs_bnode_desc, num_recs),
211 node->num_recs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
213 if (rec_off == end_off)
214 goto skip;
215 size = fd->keylength + fd->entrylength;
216
217 do {
218 data_off = hfs_bnode_read_u16(node, rec_off);
219 hfs_bnode_write_u16(node, rec_off + 2, data_off - size);
220 rec_off -= 2;
221 } while (rec_off >= end_off);
222
223 /* fill hole */
224 hfs_bnode_move(node, fd->keyoffset, fd->keyoffset + size,
225 data_off - fd->keyoffset - size);
226skip:
227 hfs_bnode_dump(node);
228 if (!fd->record)
229 hfs_brec_update_parent(fd);
230 return 0;
231}
232
233static struct hfs_bnode *hfs_bnode_split(struct hfs_find_data *fd)
234{
235 struct hfs_btree *tree;
Al Virob6b41422010-10-14 09:53:42 -0400236 struct hfs_bnode *node, *new_node, *next_node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 struct hfs_bnode_desc node_desc;
238 int num_recs, new_rec_off, new_off, old_rec_off;
239 int data_start, data_end, size;
240
241 tree = fd->tree;
242 node = fd->bnode;
243 new_node = hfs_bmap_alloc(tree);
244 if (IS_ERR(new_node))
245 return new_node;
246 hfs_bnode_get(node);
Joe Perchesc2b3e1f2013-04-30 15:27:54 -0700247 hfs_dbg(BNODE_MOD, "split_nodes: %d - %d - %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 node->this, new_node->this, node->next);
249 new_node->next = node->next;
250 new_node->prev = node->this;
251 new_node->parent = node->parent;
252 new_node->type = node->type;
253 new_node->height = node->height;
254
Al Virob6b41422010-10-14 09:53:42 -0400255 if (node->next)
256 next_node = hfs_bnode_find(tree, node->next);
257 else
258 next_node = NULL;
259
260 if (IS_ERR(next_node)) {
261 hfs_bnode_put(node);
262 hfs_bnode_put(new_node);
263 return next_node;
264 }
265
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 size = tree->node_size / 2 - node->num_recs * 2 - 14;
267 old_rec_off = tree->node_size - 4;
268 num_recs = 1;
269 for (;;) {
270 data_start = hfs_bnode_read_u16(node, old_rec_off);
271 if (data_start > size)
272 break;
273 old_rec_off -= 2;
274 if (++num_recs < node->num_recs)
275 continue;
276 /* panic? */
277 hfs_bnode_put(node);
278 hfs_bnode_put(new_node);
Al Virob6b41422010-10-14 09:53:42 -0400279 if (next_node)
280 hfs_bnode_put(next_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 return ERR_PTR(-ENOSPC);
282 }
283
284 if (fd->record + 1 < num_recs) {
285 /* new record is in the lower half,
286 * so leave some more space there
287 */
288 old_rec_off += 2;
289 num_recs--;
290 data_start = hfs_bnode_read_u16(node, old_rec_off);
291 } else {
292 hfs_bnode_put(node);
293 hfs_bnode_get(new_node);
294 fd->bnode = new_node;
295 fd->record -= num_recs;
296 fd->keyoffset -= data_start - 14;
297 fd->entryoffset -= data_start - 14;
298 }
299 new_node->num_recs = node->num_recs - num_recs;
300 node->num_recs = num_recs;
301
302 new_rec_off = tree->node_size - 2;
303 new_off = 14;
304 size = data_start - new_off;
305 num_recs = new_node->num_recs;
306 data_end = data_start;
307 while (num_recs) {
308 hfs_bnode_write_u16(new_node, new_rec_off, new_off);
309 old_rec_off -= 2;
310 new_rec_off -= 2;
311 data_end = hfs_bnode_read_u16(node, old_rec_off);
312 new_off = data_end - size;
313 num_recs--;
314 }
315 hfs_bnode_write_u16(new_node, new_rec_off, new_off);
316 hfs_bnode_copy(new_node, 14, node, data_start, data_end - data_start);
317
318 /* update new bnode header */
319 node_desc.next = cpu_to_be32(new_node->next);
320 node_desc.prev = cpu_to_be32(new_node->prev);
321 node_desc.type = new_node->type;
322 node_desc.height = new_node->height;
323 node_desc.num_recs = cpu_to_be16(new_node->num_recs);
324 node_desc.reserved = 0;
325 hfs_bnode_write(new_node, &node_desc, 0, sizeof(node_desc));
326
327 /* update previous bnode header */
328 node->next = new_node->this;
329 hfs_bnode_read(node, &node_desc, 0, sizeof(node_desc));
330 node_desc.next = cpu_to_be32(node->next);
331 node_desc.num_recs = cpu_to_be16(node->num_recs);
332 hfs_bnode_write(node, &node_desc, 0, sizeof(node_desc));
333
334 /* update next bnode header */
Al Virob6b41422010-10-14 09:53:42 -0400335 if (next_node) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 next_node->prev = new_node->this;
337 hfs_bnode_read(next_node, &node_desc, 0, sizeof(node_desc));
338 node_desc.prev = cpu_to_be32(next_node->prev);
339 hfs_bnode_write(next_node, &node_desc, 0, sizeof(node_desc));
340 hfs_bnode_put(next_node);
341 } else if (node->this == tree->leaf_tail) {
342 /* if there is no next node, this might be the new tail */
343 tree->leaf_tail = new_node->this;
344 mark_inode_dirty(tree->inode);
345 }
346
347 hfs_bnode_dump(node);
348 hfs_bnode_dump(new_node);
349 hfs_bnode_put(node);
350
351 return new_node;
352}
353
354static int hfs_brec_update_parent(struct hfs_find_data *fd)
355{
356 struct hfs_btree *tree;
357 struct hfs_bnode *node, *new_node, *parent;
358 int newkeylen, diff;
359 int rec, rec_off, end_rec_off;
360 int start_off, end_off;
361
362 tree = fd->tree;
363 node = fd->bnode;
364 new_node = NULL;
365 if (!node->parent)
366 return 0;
367
368again:
369 parent = hfs_bnode_find(tree, node->parent);
370 if (IS_ERR(parent))
371 return PTR_ERR(parent);
Vyacheslav Dubeyko324ef392013-02-27 17:03:04 -0800372 __hfs_brec_find(parent, fd, hfs_find_rec_by_key);
Sergei Antonov98cf21c2015-03-25 15:55:34 -0700373 if (fd->record < 0)
374 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 hfs_bnode_dump(parent);
376 rec = fd->record;
377
378 /* size difference between old and new key */
Vyacheslav Dubeyko324ef392013-02-27 17:03:04 -0800379 if ((tree->attributes & HFS_TREE_VARIDXKEYS) ||
380 (tree->cnid == HFSPLUS_ATTR_CNID))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 newkeylen = hfs_bnode_read_u16(node, 14) + 2;
382 else
383 fd->keylength = newkeylen = tree->max_key_len + 2;
Joe Perchesc2b3e1f2013-04-30 15:27:54 -0700384 hfs_dbg(BNODE_MOD, "update_rec: %d, %d, %d\n",
Anton Salikhmetov2753cc22010-12-16 18:08:38 +0200385 rec, fd->keylength, newkeylen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
387 rec_off = tree->node_size - (rec + 2) * 2;
388 end_rec_off = tree->node_size - (parent->num_recs + 1) * 2;
389 diff = newkeylen - fd->keylength;
390 if (!diff)
391 goto skip;
392 if (diff > 0) {
393 end_off = hfs_bnode_read_u16(parent, end_rec_off);
394 if (end_rec_off - end_off < diff) {
395
Joe Perchesc2b3e1f2013-04-30 15:27:54 -0700396 hfs_dbg(BNODE_MOD, "splitting index node\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 fd->bnode = parent;
398 new_node = hfs_bnode_split(fd);
399 if (IS_ERR(new_node))
400 return PTR_ERR(new_node);
401 parent = fd->bnode;
402 rec = fd->record;
403 rec_off = tree->node_size - (rec + 2) * 2;
Anton Salikhmetov2753cc22010-12-16 18:08:38 +0200404 end_rec_off = tree->node_size -
405 (parent->num_recs + 1) * 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 }
407 }
408
409 end_off = start_off = hfs_bnode_read_u16(parent, rec_off);
410 hfs_bnode_write_u16(parent, rec_off, start_off + diff);
411 start_off -= 4; /* move previous cnid too */
412
413 while (rec_off > end_rec_off) {
414 rec_off -= 2;
415 end_off = hfs_bnode_read_u16(parent, rec_off);
416 hfs_bnode_write_u16(parent, rec_off, end_off + diff);
417 }
418 hfs_bnode_move(parent, start_off + diff, start_off,
419 end_off - start_off);
420skip:
421 hfs_bnode_copy(parent, fd->keyoffset, node, 14, newkeylen);
422 hfs_bnode_dump(parent);
423
424 hfs_bnode_put(node);
425 node = parent;
426
427 if (new_node) {
428 __be32 cnid;
429
430 fd->bnode = hfs_bnode_find(tree, new_node->parent);
431 /* create index key and entry */
432 hfs_bnode_read_key(new_node, fd->search_key, 14);
433 cnid = cpu_to_be32(new_node->this);
434
Vyacheslav Dubeyko324ef392013-02-27 17:03:04 -0800435 __hfs_brec_find(fd->bnode, fd, hfs_find_rec_by_key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 hfs_brec_insert(fd, &cnid, sizeof(cnid));
437 hfs_bnode_put(fd->bnode);
438 hfs_bnode_put(new_node);
439
440 if (!rec) {
441 if (new_node == node)
442 goto out;
443 /* restore search_key */
444 hfs_bnode_read_key(node, fd->search_key, 14);
445 }
446 }
447
448 if (!rec && node->parent)
449 goto again;
450out:
451 fd->bnode = node;
452 return 0;
453}
454
455static int hfs_btree_inc_height(struct hfs_btree *tree)
456{
457 struct hfs_bnode *node, *new_node;
458 struct hfs_bnode_desc node_desc;
459 int key_size, rec;
460 __be32 cnid;
461
462 node = NULL;
463 if (tree->root) {
464 node = hfs_bnode_find(tree, tree->root);
465 if (IS_ERR(node))
466 return PTR_ERR(node);
467 }
468 new_node = hfs_bmap_alloc(tree);
469 if (IS_ERR(new_node)) {
470 hfs_bnode_put(node);
471 return PTR_ERR(new_node);
472 }
473
474 tree->root = new_node->this;
475 if (!tree->depth) {
476 tree->leaf_head = tree->leaf_tail = new_node->this;
477 new_node->type = HFS_NODE_LEAF;
478 new_node->num_recs = 0;
479 } else {
480 new_node->type = HFS_NODE_INDEX;
481 new_node->num_recs = 1;
482 }
483 new_node->parent = 0;
484 new_node->next = 0;
485 new_node->prev = 0;
486 new_node->height = ++tree->depth;
487
488 node_desc.next = cpu_to_be32(new_node->next);
489 node_desc.prev = cpu_to_be32(new_node->prev);
490 node_desc.type = new_node->type;
491 node_desc.height = new_node->height;
492 node_desc.num_recs = cpu_to_be16(new_node->num_recs);
493 node_desc.reserved = 0;
494 hfs_bnode_write(new_node, &node_desc, 0, sizeof(node_desc));
495
496 rec = tree->node_size - 2;
497 hfs_bnode_write_u16(new_node, rec, 14);
498
499 if (node) {
500 /* insert old root idx into new root */
501 node->parent = tree->root;
502 if (node->type == HFS_NODE_LEAF ||
Vyacheslav Dubeyko324ef392013-02-27 17:03:04 -0800503 tree->attributes & HFS_TREE_VARIDXKEYS ||
504 tree->cnid == HFSPLUS_ATTR_CNID)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 key_size = hfs_bnode_read_u16(node, 14) + 2;
506 else
507 key_size = tree->max_key_len + 2;
508 hfs_bnode_copy(new_node, 14, node, 14, key_size);
509
Vyacheslav Dubeyko324ef392013-02-27 17:03:04 -0800510 if (!(tree->attributes & HFS_TREE_VARIDXKEYS) &&
511 (tree->cnid != HFSPLUS_ATTR_CNID)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 key_size = tree->max_key_len + 2;
513 hfs_bnode_write_u16(new_node, 14, tree->max_key_len);
514 }
515 cnid = cpu_to_be32(node->this);
516 hfs_bnode_write(new_node, &cnid, 14 + key_size, 4);
517
518 rec -= 2;
519 hfs_bnode_write_u16(new_node, rec, 14 + key_size + 4);
520
521 hfs_bnode_put(node);
522 }
523 hfs_bnode_put(new_node);
524 mark_inode_dirty(tree->inode);
525
526 return 0;
527}