forked from torvalds/linux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
do_balan.c
1911 lines (1598 loc) · 54.3 KB
/
do_balan.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2000 by Hans Reiser, licensing governed by reiserfs/README
*/
/*
* Now we have all buffers that must be used in balancing of the tree
* Further calculations can not cause schedule(), and thus the buffer
* tree will be stable until the balancing will be finished
* balance the tree according to the analysis made before,
* and using buffers obtained after all above.
*/
#include <linux/uaccess.h>
#include <linux/time.h>
#include "reiserfs.h"
#include <linux/buffer_head.h>
#include <linux/kernel.h>
static inline void buffer_info_init_left(struct tree_balance *tb,
struct buffer_info *bi)
{
bi->tb = tb;
bi->bi_bh = tb->L[0];
bi->bi_parent = tb->FL[0];
bi->bi_position = get_left_neighbor_position(tb, 0);
}
static inline void buffer_info_init_right(struct tree_balance *tb,
struct buffer_info *bi)
{
bi->tb = tb;
bi->bi_bh = tb->R[0];
bi->bi_parent = tb->FR[0];
bi->bi_position = get_right_neighbor_position(tb, 0);
}
static inline void buffer_info_init_tbS0(struct tree_balance *tb,
struct buffer_info *bi)
{
bi->tb = tb;
bi->bi_bh = PATH_PLAST_BUFFER(tb->tb_path);
bi->bi_parent = PATH_H_PPARENT(tb->tb_path, 0);
bi->bi_position = PATH_H_POSITION(tb->tb_path, 1);
}
static inline void buffer_info_init_bh(struct tree_balance *tb,
struct buffer_info *bi,
struct buffer_head *bh)
{
bi->tb = tb;
bi->bi_bh = bh;
bi->bi_parent = NULL;
bi->bi_position = 0;
}
inline void do_balance_mark_leaf_dirty(struct tree_balance *tb,
struct buffer_head *bh, int flag)
{
journal_mark_dirty(tb->transaction_handle, bh);
}
#define do_balance_mark_internal_dirty do_balance_mark_leaf_dirty
#define do_balance_mark_sb_dirty do_balance_mark_leaf_dirty
/*
* summary:
* if deleting something ( tb->insert_size[0] < 0 )
* return(balance_leaf_when_delete()); (flag d handled here)
* else
* if lnum is larger than 0 we put items into the left node
* if rnum is larger than 0 we put items into the right node
* if snum1 is larger than 0 we put items into the new node s1
* if snum2 is larger than 0 we put items into the new node s2
* Note that all *num* count new items being created.
*/
static void balance_leaf_when_delete_del(struct tree_balance *tb)
{
struct buffer_head *tbS0 = PATH_PLAST_BUFFER(tb->tb_path);
int item_pos = PATH_LAST_POSITION(tb->tb_path);
struct buffer_info bi;
#ifdef CONFIG_REISERFS_CHECK
struct item_head *ih = item_head(tbS0, item_pos);
#endif
RFALSE(ih_item_len(ih) + IH_SIZE != -tb->insert_size[0],
"vs-12013: mode Delete, insert size %d, ih to be deleted %h",
-tb->insert_size[0], ih);
buffer_info_init_tbS0(tb, &bi);
leaf_delete_items(&bi, 0, item_pos, 1, -1);
if (!item_pos && tb->CFL[0]) {
if (B_NR_ITEMS(tbS0)) {
replace_key(tb, tb->CFL[0], tb->lkey[0], tbS0, 0);
} else {
if (!PATH_H_POSITION(tb->tb_path, 1))
replace_key(tb, tb->CFL[0], tb->lkey[0],
PATH_H_PPARENT(tb->tb_path, 0), 0);
}
}
RFALSE(!item_pos && !tb->CFL[0],
"PAP-12020: tb->CFL[0]==%p, tb->L[0]==%p", tb->CFL[0],
tb->L[0]);
}
/* cut item in S[0] */
static void balance_leaf_when_delete_cut(struct tree_balance *tb)
{
struct buffer_head *tbS0 = PATH_PLAST_BUFFER(tb->tb_path);
int item_pos = PATH_LAST_POSITION(tb->tb_path);
struct item_head *ih = item_head(tbS0, item_pos);
int pos_in_item = tb->tb_path->pos_in_item;
struct buffer_info bi;
buffer_info_init_tbS0(tb, &bi);
if (is_direntry_le_ih(ih)) {
/*
* UFS unlink semantics are such that you can only
* delete one directory entry at a time.
*
* when we cut a directory tb->insert_size[0] means
* number of entries to be cut (always 1)
*/
tb->insert_size[0] = -1;
leaf_cut_from_buffer(&bi, item_pos, pos_in_item,
-tb->insert_size[0]);
RFALSE(!item_pos && !pos_in_item && !tb->CFL[0],
"PAP-12030: can not change delimiting key. CFL[0]=%p",
tb->CFL[0]);
if (!item_pos && !pos_in_item && tb->CFL[0])
replace_key(tb, tb->CFL[0], tb->lkey[0], tbS0, 0);
} else {
leaf_cut_from_buffer(&bi, item_pos, pos_in_item,
-tb->insert_size[0]);
RFALSE(!ih_item_len(ih),
"PAP-12035: cut must leave non-zero dynamic "
"length of item");
}
}
static int balance_leaf_when_delete_left(struct tree_balance *tb)
{
struct buffer_head *tbS0 = PATH_PLAST_BUFFER(tb->tb_path);
int n = B_NR_ITEMS(tbS0);
/* L[0] must be joined with S[0] */
if (tb->lnum[0] == -1) {
/* R[0] must be also joined with S[0] */
if (tb->rnum[0] == -1) {
if (tb->FR[0] == PATH_H_PPARENT(tb->tb_path, 0)) {
/*
* all contents of all the
* 3 buffers will be in L[0]
*/
if (PATH_H_POSITION(tb->tb_path, 1) == 0 &&
1 < B_NR_ITEMS(tb->FR[0]))
replace_key(tb, tb->CFL[0],
tb->lkey[0], tb->FR[0], 1);
leaf_move_items(LEAF_FROM_S_TO_L, tb, n, -1,
NULL);
leaf_move_items(LEAF_FROM_R_TO_L, tb,
B_NR_ITEMS(tb->R[0]), -1,
NULL);
reiserfs_invalidate_buffer(tb, tbS0);
reiserfs_invalidate_buffer(tb, tb->R[0]);
return 0;
}
/* all contents of all the 3 buffers will be in R[0] */
leaf_move_items(LEAF_FROM_S_TO_R, tb, n, -1, NULL);
leaf_move_items(LEAF_FROM_L_TO_R, tb,
B_NR_ITEMS(tb->L[0]), -1, NULL);
/* right_delimiting_key is correct in R[0] */
replace_key(tb, tb->CFR[0], tb->rkey[0], tb->R[0], 0);
reiserfs_invalidate_buffer(tb, tbS0);
reiserfs_invalidate_buffer(tb, tb->L[0]);
return -1;
}
RFALSE(tb->rnum[0] != 0,
"PAP-12045: rnum must be 0 (%d)", tb->rnum[0]);
/* all contents of L[0] and S[0] will be in L[0] */
leaf_shift_left(tb, n, -1);
reiserfs_invalidate_buffer(tb, tbS0);
return 0;
}
/*
* a part of contents of S[0] will be in L[0] and
* the rest part of S[0] will be in R[0]
*/
RFALSE((tb->lnum[0] + tb->rnum[0] < n) ||
(tb->lnum[0] + tb->rnum[0] > n + 1),
"PAP-12050: rnum(%d) and lnum(%d) and item "
"number(%d) in S[0] are not consistent",
tb->rnum[0], tb->lnum[0], n);
RFALSE((tb->lnum[0] + tb->rnum[0] == n) &&
(tb->lbytes != -1 || tb->rbytes != -1),
"PAP-12055: bad rbytes (%d)/lbytes (%d) "
"parameters when items are not split",
tb->rbytes, tb->lbytes);
RFALSE((tb->lnum[0] + tb->rnum[0] == n + 1) &&
(tb->lbytes < 1 || tb->rbytes != -1),
"PAP-12060: bad rbytes (%d)/lbytes (%d) "
"parameters when items are split",
tb->rbytes, tb->lbytes);
leaf_shift_left(tb, tb->lnum[0], tb->lbytes);
leaf_shift_right(tb, tb->rnum[0], tb->rbytes);
reiserfs_invalidate_buffer(tb, tbS0);
return 0;
}
/*
* Balance leaf node in case of delete or cut: insert_size[0] < 0
*
* lnum, rnum can have values >= -1
* -1 means that the neighbor must be joined with S
* 0 means that nothing should be done with the neighbor
* >0 means to shift entirely or partly the specified number of items
* to the neighbor
*/
static int balance_leaf_when_delete(struct tree_balance *tb, int flag)
{
struct buffer_head *tbS0 = PATH_PLAST_BUFFER(tb->tb_path);
int item_pos = PATH_LAST_POSITION(tb->tb_path);
struct buffer_info bi;
int n;
struct item_head *ih;
RFALSE(tb->FR[0] && B_LEVEL(tb->FR[0]) != DISK_LEAF_NODE_LEVEL + 1,
"vs- 12000: level: wrong FR %z", tb->FR[0]);
RFALSE(tb->blknum[0] > 1,
"PAP-12005: tb->blknum == %d, can not be > 1", tb->blknum[0]);
RFALSE(!tb->blknum[0] && !PATH_H_PPARENT(tb->tb_path, 0),
"PAP-12010: tree can not be empty");
ih = item_head(tbS0, item_pos);
buffer_info_init_tbS0(tb, &bi);
/* Delete or truncate the item */
BUG_ON(flag != M_DELETE && flag != M_CUT);
if (flag == M_DELETE)
balance_leaf_when_delete_del(tb);
else /* M_CUT */
balance_leaf_when_delete_cut(tb);
/*
* the rule is that no shifting occurs unless by shifting
* a node can be freed
*/
n = B_NR_ITEMS(tbS0);
/* L[0] takes part in balancing */
if (tb->lnum[0])
return balance_leaf_when_delete_left(tb);
if (tb->rnum[0] == -1) {
/* all contents of R[0] and S[0] will be in R[0] */
leaf_shift_right(tb, n, -1);
reiserfs_invalidate_buffer(tb, tbS0);
return 0;
}
RFALSE(tb->rnum[0],
"PAP-12065: bad rnum parameter must be 0 (%d)", tb->rnum[0]);
return 0;
}
static unsigned int balance_leaf_insert_left(struct tree_balance *tb,
struct item_head *const ih,
const char * const body)
{
int ret;
struct buffer_info bi;
int n = B_NR_ITEMS(tb->L[0]);
unsigned body_shift_bytes = 0;
if (tb->item_pos == tb->lnum[0] - 1 && tb->lbytes != -1) {
/* part of new item falls into L[0] */
int new_item_len, shift;
int version;
ret = leaf_shift_left(tb, tb->lnum[0] - 1, -1);
/* Calculate item length to insert to S[0] */
new_item_len = ih_item_len(ih) - tb->lbytes;
/* Calculate and check item length to insert to L[0] */
put_ih_item_len(ih, ih_item_len(ih) - new_item_len);
RFALSE(ih_item_len(ih) <= 0,
"PAP-12080: there is nothing to insert into L[0]: "
"ih_item_len=%d", ih_item_len(ih));
/* Insert new item into L[0] */
buffer_info_init_left(tb, &bi);
leaf_insert_into_buf(&bi, n + tb->item_pos - ret, ih, body,
min_t(int, tb->zeroes_num, ih_item_len(ih)));
version = ih_version(ih);
/*
* Calculate key component, item length and body to
* insert into S[0]
*/
shift = 0;
if (is_indirect_le_ih(ih))
shift = tb->tb_sb->s_blocksize_bits - UNFM_P_SHIFT;
add_le_ih_k_offset(ih, tb->lbytes << shift);
put_ih_item_len(ih, new_item_len);
if (tb->lbytes > tb->zeroes_num) {
body_shift_bytes = tb->lbytes - tb->zeroes_num;
tb->zeroes_num = 0;
} else
tb->zeroes_num -= tb->lbytes;
RFALSE(ih_item_len(ih) <= 0,
"PAP-12085: there is nothing to insert into S[0]: "
"ih_item_len=%d", ih_item_len(ih));
} else {
/* new item in whole falls into L[0] */
/* Shift lnum[0]-1 items to L[0] */
ret = leaf_shift_left(tb, tb->lnum[0] - 1, tb->lbytes);
/* Insert new item into L[0] */
buffer_info_init_left(tb, &bi);
leaf_insert_into_buf(&bi, n + tb->item_pos - ret, ih, body,
tb->zeroes_num);
tb->insert_size[0] = 0;
tb->zeroes_num = 0;
}
return body_shift_bytes;
}
static void balance_leaf_paste_left_shift_dirent(struct tree_balance *tb,
struct item_head * const ih,
const char * const body)
{
int n = B_NR_ITEMS(tb->L[0]);
struct buffer_info bi;
RFALSE(tb->zeroes_num,
"PAP-12090: invalid parameter in case of a directory");
/* directory item */
if (tb->lbytes > tb->pos_in_item) {
/* new directory entry falls into L[0] */
struct item_head *pasted;
int ret, l_pos_in_item = tb->pos_in_item;
/*
* Shift lnum[0] - 1 items in whole.
* Shift lbytes - 1 entries from given directory item
*/
ret = leaf_shift_left(tb, tb->lnum[0], tb->lbytes - 1);
if (ret && !tb->item_pos) {
pasted = item_head(tb->L[0], B_NR_ITEMS(tb->L[0]) - 1);
l_pos_in_item += ih_entry_count(pasted) -
(tb->lbytes - 1);
}
/* Append given directory entry to directory item */
buffer_info_init_left(tb, &bi);
leaf_paste_in_buffer(&bi, n + tb->item_pos - ret,
l_pos_in_item, tb->insert_size[0],
body, tb->zeroes_num);
/*
* previous string prepared space for pasting new entry,
* following string pastes this entry
*/
/*
* when we have merge directory item, pos_in_item
* has been changed too
*/
/* paste new directory entry. 1 is entry number */
leaf_paste_entries(&bi, n + tb->item_pos - ret,
l_pos_in_item, 1,
(struct reiserfs_de_head *) body,
body + DEH_SIZE, tb->insert_size[0]);
tb->insert_size[0] = 0;
} else {
/* new directory item doesn't fall into L[0] */
/*
* Shift lnum[0]-1 items in whole. Shift lbytes
* directory entries from directory item number lnum[0]
*/
leaf_shift_left(tb, tb->lnum[0], tb->lbytes);
}
/* Calculate new position to append in item body */
tb->pos_in_item -= tb->lbytes;
}
static unsigned int balance_leaf_paste_left_shift(struct tree_balance *tb,
struct item_head * const ih,
const char * const body)
{
struct buffer_head *tbS0 = PATH_PLAST_BUFFER(tb->tb_path);
int n = B_NR_ITEMS(tb->L[0]);
struct buffer_info bi;
int body_shift_bytes = 0;
if (is_direntry_le_ih(item_head(tbS0, tb->item_pos))) {
balance_leaf_paste_left_shift_dirent(tb, ih, body);
return 0;
}
RFALSE(tb->lbytes <= 0,
"PAP-12095: there is nothing to shift to L[0]. "
"lbytes=%d", tb->lbytes);
RFALSE(tb->pos_in_item != ih_item_len(item_head(tbS0, tb->item_pos)),
"PAP-12100: incorrect position to paste: "
"item_len=%d, pos_in_item=%d",
ih_item_len(item_head(tbS0, tb->item_pos)), tb->pos_in_item);
/* appended item will be in L[0] in whole */
if (tb->lbytes >= tb->pos_in_item) {
struct item_head *tbS0_pos_ih, *tbL0_ih;
struct item_head *tbS0_0_ih;
struct reiserfs_key *left_delim_key;
int ret, l_n, version, temp_l;
tbS0_pos_ih = item_head(tbS0, tb->item_pos);
tbS0_0_ih = item_head(tbS0, 0);
/*
* this bytes number must be appended
* to the last item of L[h]
*/
l_n = tb->lbytes - tb->pos_in_item;
/* Calculate new insert_size[0] */
tb->insert_size[0] -= l_n;
RFALSE(tb->insert_size[0] <= 0,
"PAP-12105: there is nothing to paste into "
"L[0]. insert_size=%d", tb->insert_size[0]);
ret = leaf_shift_left(tb, tb->lnum[0],
ih_item_len(tbS0_pos_ih));
tbL0_ih = item_head(tb->L[0], n + tb->item_pos - ret);
/* Append to body of item in L[0] */
buffer_info_init_left(tb, &bi);
leaf_paste_in_buffer(&bi, n + tb->item_pos - ret,
ih_item_len(tbL0_ih), l_n, body,
min_t(int, l_n, tb->zeroes_num));
/*
* 0-th item in S0 can be only of DIRECT type
* when l_n != 0
*/
temp_l = l_n;
RFALSE(ih_item_len(tbS0_0_ih),
"PAP-12106: item length must be 0");
RFALSE(comp_short_le_keys(&tbS0_0_ih->ih_key,
leaf_key(tb->L[0], n + tb->item_pos - ret)),
"PAP-12107: items must be of the same file");
if (is_indirect_le_ih(tbL0_ih)) {
int shift = tb->tb_sb->s_blocksize_bits - UNFM_P_SHIFT;
temp_l = l_n << shift;
}
/* update key of first item in S0 */
version = ih_version(tbS0_0_ih);
add_le_key_k_offset(version, &tbS0_0_ih->ih_key, temp_l);
/* update left delimiting key */
left_delim_key = internal_key(tb->CFL[0], tb->lkey[0]);
add_le_key_k_offset(version, left_delim_key, temp_l);
/*
* Calculate new body, position in item and
* insert_size[0]
*/
if (l_n > tb->zeroes_num) {
body_shift_bytes = l_n - tb->zeroes_num;
tb->zeroes_num = 0;
} else
tb->zeroes_num -= l_n;
tb->pos_in_item = 0;
RFALSE(comp_short_le_keys(&tbS0_0_ih->ih_key,
leaf_key(tb->L[0],
B_NR_ITEMS(tb->L[0]) - 1)) ||
!op_is_left_mergeable(leaf_key(tbS0, 0), tbS0->b_size) ||
!op_is_left_mergeable(left_delim_key, tbS0->b_size),
"PAP-12120: item must be merge-able with left "
"neighboring item");
} else {
/* only part of the appended item will be in L[0] */
/* Calculate position in item for append in S[0] */
tb->pos_in_item -= tb->lbytes;
RFALSE(tb->pos_in_item <= 0,
"PAP-12125: no place for paste. pos_in_item=%d",
tb->pos_in_item);
/*
* Shift lnum[0] - 1 items in whole.
* Shift lbytes - 1 byte from item number lnum[0]
*/
leaf_shift_left(tb, tb->lnum[0], tb->lbytes);
}
return body_shift_bytes;
}
/* appended item will be in L[0] in whole */
static void balance_leaf_paste_left_whole(struct tree_balance *tb,
struct item_head * const ih,
const char * const body)
{
struct buffer_head *tbS0 = PATH_PLAST_BUFFER(tb->tb_path);
int n = B_NR_ITEMS(tb->L[0]);
struct buffer_info bi;
struct item_head *pasted;
int ret;
/* if we paste into first item of S[0] and it is left mergable */
if (!tb->item_pos &&
op_is_left_mergeable(leaf_key(tbS0, 0), tbS0->b_size)) {
/*
* then increment pos_in_item by the size of the
* last item in L[0]
*/
pasted = item_head(tb->L[0], n - 1);
if (is_direntry_le_ih(pasted))
tb->pos_in_item += ih_entry_count(pasted);
else
tb->pos_in_item += ih_item_len(pasted);
}
/*
* Shift lnum[0] - 1 items in whole.
* Shift lbytes - 1 byte from item number lnum[0]
*/
ret = leaf_shift_left(tb, tb->lnum[0], tb->lbytes);
/* Append to body of item in L[0] */
buffer_info_init_left(tb, &bi);
leaf_paste_in_buffer(&bi, n + tb->item_pos - ret, tb->pos_in_item,
tb->insert_size[0], body, tb->zeroes_num);
/* if appended item is directory, paste entry */
pasted = item_head(tb->L[0], n + tb->item_pos - ret);
if (is_direntry_le_ih(pasted))
leaf_paste_entries(&bi, n + tb->item_pos - ret,
tb->pos_in_item, 1,
(struct reiserfs_de_head *)body,
body + DEH_SIZE, tb->insert_size[0]);
/*
* if appended item is indirect item, put unformatted node
* into un list
*/
if (is_indirect_le_ih(pasted))
set_ih_free_space(pasted, 0);
tb->insert_size[0] = 0;
tb->zeroes_num = 0;
}
static unsigned int balance_leaf_paste_left(struct tree_balance *tb,
struct item_head * const ih,
const char * const body)
{
/* we must shift the part of the appended item */
if (tb->item_pos == tb->lnum[0] - 1 && tb->lbytes != -1)
return balance_leaf_paste_left_shift(tb, ih, body);
else
balance_leaf_paste_left_whole(tb, ih, body);
return 0;
}
/* Shift lnum[0] items from S[0] to the left neighbor L[0] */
static unsigned int balance_leaf_left(struct tree_balance *tb,
struct item_head * const ih,
const char * const body, int flag)
{
if (tb->lnum[0] <= 0)
return 0;
/* new item or it part falls to L[0], shift it too */
if (tb->item_pos < tb->lnum[0]) {
BUG_ON(flag != M_INSERT && flag != M_PASTE);
if (flag == M_INSERT)
return balance_leaf_insert_left(tb, ih, body);
else /* M_PASTE */
return balance_leaf_paste_left(tb, ih, body);
} else
/* new item doesn't fall into L[0] */
leaf_shift_left(tb, tb->lnum[0], tb->lbytes);
return 0;
}
static void balance_leaf_insert_right(struct tree_balance *tb,
struct item_head * const ih,
const char * const body)
{
struct buffer_head *tbS0 = PATH_PLAST_BUFFER(tb->tb_path);
int n = B_NR_ITEMS(tbS0);
struct buffer_info bi;
int ret;
/* new item or part of it doesn't fall into R[0] */
if (n - tb->rnum[0] >= tb->item_pos) {
leaf_shift_right(tb, tb->rnum[0], tb->rbytes);
return;
}
/* new item or its part falls to R[0] */
/* part of new item falls into R[0] */
if (tb->item_pos == n - tb->rnum[0] + 1 && tb->rbytes != -1) {
loff_t old_key_comp, old_len, r_zeroes_number;
const char *r_body;
int version, shift;
loff_t offset;
leaf_shift_right(tb, tb->rnum[0] - 1, -1);
version = ih_version(ih);
/* Remember key component and item length */
old_key_comp = le_ih_k_offset(ih);
old_len = ih_item_len(ih);
/*
* Calculate key component and item length to insert
* into R[0]
*/
shift = 0;
if (is_indirect_le_ih(ih))
shift = tb->tb_sb->s_blocksize_bits - UNFM_P_SHIFT;
offset = le_ih_k_offset(ih) + ((old_len - tb->rbytes) << shift);
set_le_ih_k_offset(ih, offset);
put_ih_item_len(ih, tb->rbytes);
/* Insert part of the item into R[0] */
buffer_info_init_right(tb, &bi);
if ((old_len - tb->rbytes) > tb->zeroes_num) {
r_zeroes_number = 0;
r_body = body + (old_len - tb->rbytes) - tb->zeroes_num;
} else {
r_body = body;
r_zeroes_number = tb->zeroes_num -
(old_len - tb->rbytes);
tb->zeroes_num -= r_zeroes_number;
}
leaf_insert_into_buf(&bi, 0, ih, r_body, r_zeroes_number);
/* Replace right delimiting key by first key in R[0] */
replace_key(tb, tb->CFR[0], tb->rkey[0], tb->R[0], 0);
/*
* Calculate key component and item length to
* insert into S[0]
*/
set_le_ih_k_offset(ih, old_key_comp);
put_ih_item_len(ih, old_len - tb->rbytes);
tb->insert_size[0] -= tb->rbytes;
} else {
/* whole new item falls into R[0] */
/* Shift rnum[0]-1 items to R[0] */
ret = leaf_shift_right(tb, tb->rnum[0] - 1, tb->rbytes);
/* Insert new item into R[0] */
buffer_info_init_right(tb, &bi);
leaf_insert_into_buf(&bi, tb->item_pos - n + tb->rnum[0] - 1,
ih, body, tb->zeroes_num);
if (tb->item_pos - n + tb->rnum[0] - 1 == 0)
replace_key(tb, tb->CFR[0], tb->rkey[0], tb->R[0], 0);
tb->zeroes_num = tb->insert_size[0] = 0;
}
}
static void balance_leaf_paste_right_shift_dirent(struct tree_balance *tb,
struct item_head * const ih,
const char * const body)
{
struct buffer_head *tbS0 = PATH_PLAST_BUFFER(tb->tb_path);
struct buffer_info bi;
int entry_count;
RFALSE(tb->zeroes_num,
"PAP-12145: invalid parameter in case of a directory");
entry_count = ih_entry_count(item_head(tbS0, tb->item_pos));
/* new directory entry falls into R[0] */
if (entry_count - tb->rbytes < tb->pos_in_item) {
int paste_entry_position;
RFALSE(tb->rbytes - 1 >= entry_count || !tb->insert_size[0],
"PAP-12150: no enough of entries to shift to R[0]: "
"rbytes=%d, entry_count=%d", tb->rbytes, entry_count);
/*
* Shift rnum[0]-1 items in whole.
* Shift rbytes-1 directory entries from directory
* item number rnum[0]
*/
leaf_shift_right(tb, tb->rnum[0], tb->rbytes - 1);
/* Paste given directory entry to directory item */
paste_entry_position = tb->pos_in_item - entry_count +
tb->rbytes - 1;
buffer_info_init_right(tb, &bi);
leaf_paste_in_buffer(&bi, 0, paste_entry_position,
tb->insert_size[0], body, tb->zeroes_num);
/* paste entry */
leaf_paste_entries(&bi, 0, paste_entry_position, 1,
(struct reiserfs_de_head *) body,
body + DEH_SIZE, tb->insert_size[0]);
/* change delimiting keys */
if (paste_entry_position == 0)
replace_key(tb, tb->CFR[0], tb->rkey[0], tb->R[0], 0);
tb->insert_size[0] = 0;
tb->pos_in_item++;
} else {
/* new directory entry doesn't fall into R[0] */
leaf_shift_right(tb, tb->rnum[0], tb->rbytes);
}
}
static void balance_leaf_paste_right_shift(struct tree_balance *tb,
struct item_head * const ih,
const char * const body)
{
struct buffer_head *tbS0 = PATH_PLAST_BUFFER(tb->tb_path);
int n_shift, n_rem, r_zeroes_number, version;
unsigned long temp_rem;
const char *r_body;
struct buffer_info bi;
/* we append to directory item */
if (is_direntry_le_ih(item_head(tbS0, tb->item_pos))) {
balance_leaf_paste_right_shift_dirent(tb, ih, body);
return;
}
/* regular object */
/*
* Calculate number of bytes which must be shifted
* from appended item
*/
n_shift = tb->rbytes - tb->insert_size[0];
if (n_shift < 0)
n_shift = 0;
RFALSE(tb->pos_in_item != ih_item_len(item_head(tbS0, tb->item_pos)),
"PAP-12155: invalid position to paste. ih_item_len=%d, "
"pos_in_item=%d", tb->pos_in_item,
ih_item_len(item_head(tbS0, tb->item_pos)));
leaf_shift_right(tb, tb->rnum[0], n_shift);
/*
* Calculate number of bytes which must remain in body
* after appending to R[0]
*/
n_rem = tb->insert_size[0] - tb->rbytes;
if (n_rem < 0)
n_rem = 0;
temp_rem = n_rem;
version = ih_version(item_head(tb->R[0], 0));
if (is_indirect_le_key(version, leaf_key(tb->R[0], 0))) {
int shift = tb->tb_sb->s_blocksize_bits - UNFM_P_SHIFT;
temp_rem = n_rem << shift;
}
add_le_key_k_offset(version, leaf_key(tb->R[0], 0), temp_rem);
add_le_key_k_offset(version, internal_key(tb->CFR[0], tb->rkey[0]),
temp_rem);
do_balance_mark_internal_dirty(tb, tb->CFR[0], 0);
/* Append part of body into R[0] */
buffer_info_init_right(tb, &bi);
if (n_rem > tb->zeroes_num) {
r_zeroes_number = 0;
r_body = body + n_rem - tb->zeroes_num;
} else {
r_body = body;
r_zeroes_number = tb->zeroes_num - n_rem;
tb->zeroes_num -= r_zeroes_number;
}
leaf_paste_in_buffer(&bi, 0, n_shift, tb->insert_size[0] - n_rem,
r_body, r_zeroes_number);
if (is_indirect_le_ih(item_head(tb->R[0], 0)))
set_ih_free_space(item_head(tb->R[0], 0), 0);
tb->insert_size[0] = n_rem;
if (!n_rem)
tb->pos_in_item++;
}
static void balance_leaf_paste_right_whole(struct tree_balance *tb,
struct item_head * const ih,
const char * const body)
{
struct buffer_head *tbS0 = PATH_PLAST_BUFFER(tb->tb_path);
int n = B_NR_ITEMS(tbS0);
struct item_head *pasted;
struct buffer_info bi;
buffer_info_init_right(tb, &bi);
leaf_shift_right(tb, tb->rnum[0], tb->rbytes);
/* append item in R[0] */
if (tb->pos_in_item >= 0) {
buffer_info_init_right(tb, &bi);
leaf_paste_in_buffer(&bi, tb->item_pos - n + tb->rnum[0],
tb->pos_in_item, tb->insert_size[0], body,
tb->zeroes_num);
}
/* paste new entry, if item is directory item */
pasted = item_head(tb->R[0], tb->item_pos - n + tb->rnum[0]);
if (is_direntry_le_ih(pasted) && tb->pos_in_item >= 0) {
leaf_paste_entries(&bi, tb->item_pos - n + tb->rnum[0],
tb->pos_in_item, 1,
(struct reiserfs_de_head *)body,
body + DEH_SIZE, tb->insert_size[0]);
if (!tb->pos_in_item) {
RFALSE(tb->item_pos - n + tb->rnum[0],
"PAP-12165: directory item must be first "
"item of node when pasting is in 0th position");
/* update delimiting keys */
replace_key(tb, tb->CFR[0], tb->rkey[0], tb->R[0], 0);
}
}
if (is_indirect_le_ih(pasted))
set_ih_free_space(pasted, 0);
tb->zeroes_num = tb->insert_size[0] = 0;
}
static void balance_leaf_paste_right(struct tree_balance *tb,
struct item_head * const ih,
const char * const body)
{
struct buffer_head *tbS0 = PATH_PLAST_BUFFER(tb->tb_path);
int n = B_NR_ITEMS(tbS0);
/* new item doesn't fall into R[0] */
if (n - tb->rnum[0] > tb->item_pos) {
leaf_shift_right(tb, tb->rnum[0], tb->rbytes);
return;
}
/* pasted item or part of it falls to R[0] */
if (tb->item_pos == n - tb->rnum[0] && tb->rbytes != -1)
/* we must shift the part of the appended item */
balance_leaf_paste_right_shift(tb, ih, body);
else
/* pasted item in whole falls into R[0] */
balance_leaf_paste_right_whole(tb, ih, body);
}
/* shift rnum[0] items from S[0] to the right neighbor R[0] */
static void balance_leaf_right(struct tree_balance *tb,
struct item_head * const ih,
const char * const body, int flag)
{
if (tb->rnum[0] <= 0)
return;
BUG_ON(flag != M_INSERT && flag != M_PASTE);
if (flag == M_INSERT)
balance_leaf_insert_right(tb, ih, body);
else /* M_PASTE */
balance_leaf_paste_right(tb, ih, body);
}
static void balance_leaf_new_nodes_insert(struct tree_balance *tb,
struct item_head * const ih,
const char * const body,
struct item_head *insert_key,
struct buffer_head **insert_ptr,
int i)
{
struct buffer_head *tbS0 = PATH_PLAST_BUFFER(tb->tb_path);
int n = B_NR_ITEMS(tbS0);
struct buffer_info bi;
int shift;
/* new item or it part don't falls into S_new[i] */
if (n - tb->snum[i] >= tb->item_pos) {
leaf_move_items(LEAF_FROM_S_TO_SNEW, tb,
tb->snum[i], tb->sbytes[i], tb->S_new[i]);
return;
}
/* new item or it's part falls to first new node S_new[i] */
/* part of new item falls into S_new[i] */
if (tb->item_pos == n - tb->snum[i] + 1 && tb->sbytes[i] != -1) {
int old_key_comp, old_len, r_zeroes_number;
const char *r_body;
int version;
/* Move snum[i]-1 items from S[0] to S_new[i] */
leaf_move_items(LEAF_FROM_S_TO_SNEW, tb, tb->snum[i] - 1, -1,
tb->S_new[i]);
/* Remember key component and item length */
version = ih_version(ih);
old_key_comp = le_ih_k_offset(ih);
old_len = ih_item_len(ih);
/*
* Calculate key component and item length to insert
* into S_new[i]
*/
shift = 0;
if (is_indirect_le_ih(ih))
shift = tb->tb_sb->s_blocksize_bits - UNFM_P_SHIFT;
set_le_ih_k_offset(ih,
le_ih_k_offset(ih) +
((old_len - tb->sbytes[i]) << shift));
put_ih_item_len(ih, tb->sbytes[i]);
/* Insert part of the item into S_new[i] before 0-th item */
buffer_info_init_bh(tb, &bi, tb->S_new[i]);
if ((old_len - tb->sbytes[i]) > tb->zeroes_num) {
r_zeroes_number = 0;
r_body = body + (old_len - tb->sbytes[i]) -
tb->zeroes_num;
} else {
r_body = body;
r_zeroes_number = tb->zeroes_num - (old_len -
tb->sbytes[i]);
tb->zeroes_num -= r_zeroes_number;
}
leaf_insert_into_buf(&bi, 0, ih, r_body, r_zeroes_number);
/*
* Calculate key component and item length to
* insert into S[i]
*/
set_le_ih_k_offset(ih, old_key_comp);
put_ih_item_len(ih, old_len - tb->sbytes[i]);
tb->insert_size[0] -= tb->sbytes[i];
} else {