forked from torvalds/linux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jfs_xtree.c
3903 lines (3393 loc) · 92.1 KB
/
jfs_xtree.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 (C) International Business Machines Corp., 2000-2005
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
* the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/*
* jfs_xtree.c: extent allocation descriptor B+-tree manager
*/
#include <linux/fs.h>
#include <linux/module.h>
#include <linux/quotaops.h>
#include <linux/seq_file.h>
#include "jfs_incore.h"
#include "jfs_filsys.h"
#include "jfs_metapage.h"
#include "jfs_dmap.h"
#include "jfs_dinode.h"
#include "jfs_superblock.h"
#include "jfs_debug.h"
/*
* xtree local flag
*/
#define XT_INSERT 0x00000001
/*
* xtree key/entry comparison: extent offset
*
* return:
* -1: k < start of extent
* 0: start_of_extent <= k <= end_of_extent
* 1: k > end_of_extent
*/
#define XT_CMP(CMP, K, X, OFFSET64)\
{\
OFFSET64 = offsetXAD(X);\
(CMP) = ((K) >= OFFSET64 + lengthXAD(X)) ? 1 :\
((K) < OFFSET64) ? -1 : 0;\
}
/* write a xad entry */
#define XT_PUTENTRY(XAD, FLAG, OFF, LEN, ADDR)\
{\
(XAD)->flag = (FLAG);\
XADoffset((XAD), (OFF));\
XADlength((XAD), (LEN));\
XADaddress((XAD), (ADDR));\
}
#define XT_PAGE(IP, MP) BT_PAGE(IP, MP, xtpage_t, i_xtroot)
/* get page buffer for specified block address */
/* ToDo: Replace this ugly macro with a function */
#define XT_GETPAGE(IP, BN, MP, SIZE, P, RC) \
do { \
BT_GETPAGE(IP, BN, MP, xtpage_t, SIZE, P, RC, i_xtroot); \
if (!(RC)) { \
if ((le16_to_cpu((P)->header.nextindex) < XTENTRYSTART) || \
(le16_to_cpu((P)->header.nextindex) > \
le16_to_cpu((P)->header.maxentry)) || \
(le16_to_cpu((P)->header.maxentry) > \
(((BN) == 0) ? XTROOTMAXSLOT : PSIZE >> L2XTSLOTSIZE))) { \
jfs_error((IP)->i_sb, \
"XT_GETPAGE: xtree page corrupt\n"); \
BT_PUTPAGE(MP); \
MP = NULL; \
RC = -EIO; \
} \
} \
} while (0)
/* for consistency */
#define XT_PUTPAGE(MP) BT_PUTPAGE(MP)
#define XT_GETSEARCH(IP, LEAF, BN, MP, P, INDEX) \
BT_GETSEARCH(IP, LEAF, BN, MP, xtpage_t, P, INDEX, i_xtroot)
/* xtree entry parameter descriptor */
struct xtsplit {
struct metapage *mp;
s16 index;
u8 flag;
s64 off;
s64 addr;
int len;
struct pxdlist *pxdlist;
};
/*
* statistics
*/
#ifdef CONFIG_JFS_STATISTICS
static struct {
uint search;
uint fastSearch;
uint split;
} xtStat;
#endif
/*
* forward references
*/
static int xtSearch(struct inode *ip, s64 xoff, s64 *next, int *cmpp,
struct btstack * btstack, int flag);
static int xtSplitUp(tid_t tid,
struct inode *ip,
struct xtsplit * split, struct btstack * btstack);
static int xtSplitPage(tid_t tid, struct inode *ip, struct xtsplit * split,
struct metapage ** rmpp, s64 * rbnp);
static int xtSplitRoot(tid_t tid, struct inode *ip,
struct xtsplit * split, struct metapage ** rmpp);
#ifdef _STILL_TO_PORT
static int xtDeleteUp(tid_t tid, struct inode *ip, struct metapage * fmp,
xtpage_t * fp, struct btstack * btstack);
static int xtSearchNode(struct inode *ip,
xad_t * xad,
int *cmpp, struct btstack * btstack, int flag);
static int xtRelink(tid_t tid, struct inode *ip, xtpage_t * fp);
#endif /* _STILL_TO_PORT */
/*
* xtLookup()
*
* function: map a single page into a physical extent;
*/
int xtLookup(struct inode *ip, s64 lstart,
s64 llen, int *pflag, s64 * paddr, s32 * plen, int no_check)
{
int rc = 0;
struct btstack btstack;
int cmp;
s64 bn;
struct metapage *mp;
xtpage_t *p;
int index;
xad_t *xad;
s64 next, size, xoff, xend;
int xlen;
s64 xaddr;
*paddr = 0;
*plen = llen;
if (!no_check) {
/* is lookup offset beyond eof ? */
size = ((u64) ip->i_size + (JFS_SBI(ip->i_sb)->bsize - 1)) >>
JFS_SBI(ip->i_sb)->l2bsize;
if (lstart >= size)
return 0;
}
/*
* search for the xad entry covering the logical extent
*/
//search:
if ((rc = xtSearch(ip, lstart, &next, &cmp, &btstack, 0))) {
jfs_err("xtLookup: xtSearch returned %d", rc);
return rc;
}
/*
* compute the physical extent covering logical extent
*
* N.B. search may have failed (e.g., hole in sparse file),
* and returned the index of the next entry.
*/
/* retrieve search result */
XT_GETSEARCH(ip, btstack.top, bn, mp, p, index);
/* is xad found covering start of logical extent ?
* lstart is a page start address,
* i.e., lstart cannot start in a hole;
*/
if (cmp) {
if (next)
*plen = min(next - lstart, llen);
goto out;
}
/*
* lxd covered by xad
*/
xad = &p->xad[index];
xoff = offsetXAD(xad);
xlen = lengthXAD(xad);
xend = xoff + xlen;
xaddr = addressXAD(xad);
/* initialize new pxd */
*pflag = xad->flag;
*paddr = xaddr + (lstart - xoff);
/* a page must be fully covered by an xad */
*plen = min(xend - lstart, llen);
out:
XT_PUTPAGE(mp);
return rc;
}
/*
* xtSearch()
*
* function: search for the xad entry covering specified offset.
*
* parameters:
* ip - file object;
* xoff - extent offset;
* nextp - address of next extent (if any) for search miss
* cmpp - comparison result:
* btstack - traverse stack;
* flag - search process flag (XT_INSERT);
*
* returns:
* btstack contains (bn, index) of search path traversed to the entry.
* *cmpp is set to result of comparison with the entry returned.
* the page containing the entry is pinned at exit.
*/
static int xtSearch(struct inode *ip, s64 xoff, s64 *nextp,
int *cmpp, struct btstack * btstack, int flag)
{
struct jfs_inode_info *jfs_ip = JFS_IP(ip);
int rc = 0;
int cmp = 1; /* init for empty page */
s64 bn; /* block number */
struct metapage *mp; /* page buffer */
xtpage_t *p; /* page */
xad_t *xad;
int base, index, lim, btindex;
struct btframe *btsp;
int nsplit = 0; /* number of pages to split */
s64 t64;
s64 next = 0;
INCREMENT(xtStat.search);
BT_CLR(btstack);
btstack->nsplit = 0;
/*
* search down tree from root:
*
* between two consecutive entries of <Ki, Pi> and <Kj, Pj> of
* internal page, child page Pi contains entry with k, Ki <= K < Kj.
*
* if entry with search key K is not found
* internal page search find the entry with largest key Ki
* less than K which point to the child page to search;
* leaf page search find the entry with smallest key Kj
* greater than K so that the returned index is the position of
* the entry to be shifted right for insertion of new entry.
* for empty tree, search key is greater than any key of the tree.
*
* by convention, root bn = 0.
*/
for (bn = 0;;) {
/* get/pin the page to search */
XT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
if (rc)
return rc;
/* try sequential access heuristics with the previous
* access entry in target leaf page:
* once search narrowed down into the target leaf,
* key must either match an entry in the leaf or
* key entry does not exist in the tree;
*/
//fastSearch:
if ((jfs_ip->btorder & BT_SEQUENTIAL) &&
(p->header.flag & BT_LEAF) &&
(index = jfs_ip->btindex) <
le16_to_cpu(p->header.nextindex)) {
xad = &p->xad[index];
t64 = offsetXAD(xad);
if (xoff < t64 + lengthXAD(xad)) {
if (xoff >= t64) {
*cmpp = 0;
goto out;
}
/* stop sequential access heuristics */
goto binarySearch;
} else { /* (t64 + lengthXAD(xad)) <= xoff */
/* try next sequential entry */
index++;
if (index <
le16_to_cpu(p->header.nextindex)) {
xad++;
t64 = offsetXAD(xad);
if (xoff < t64 + lengthXAD(xad)) {
if (xoff >= t64) {
*cmpp = 0;
goto out;
}
/* miss: key falls between
* previous and this entry
*/
*cmpp = 1;
next = t64;
goto out;
}
/* (xoff >= t64 + lengthXAD(xad));
* matching entry may be further out:
* stop heuristic search
*/
/* stop sequential access heuristics */
goto binarySearch;
}
/* (index == p->header.nextindex);
* miss: key entry does not exist in
* the target leaf/tree
*/
*cmpp = 1;
goto out;
}
/*
* if hit, return index of the entry found, and
* if miss, where new entry with search key is
* to be inserted;
*/
out:
/* compute number of pages to split */
if (flag & XT_INSERT) {
if (p->header.nextindex == /* little-endian */
p->header.maxentry)
nsplit++;
else
nsplit = 0;
btstack->nsplit = nsplit;
}
/* save search result */
btsp = btstack->top;
btsp->bn = bn;
btsp->index = index;
btsp->mp = mp;
/* update sequential access heuristics */
jfs_ip->btindex = index;
if (nextp)
*nextp = next;
INCREMENT(xtStat.fastSearch);
return 0;
}
/* well, ... full search now */
binarySearch:
lim = le16_to_cpu(p->header.nextindex) - XTENTRYSTART;
/*
* binary search with search key K on the current page
*/
for (base = XTENTRYSTART; lim; lim >>= 1) {
index = base + (lim >> 1);
XT_CMP(cmp, xoff, &p->xad[index], t64);
if (cmp == 0) {
/*
* search hit
*/
/* search hit - leaf page:
* return the entry found
*/
if (p->header.flag & BT_LEAF) {
*cmpp = cmp;
/* compute number of pages to split */
if (flag & XT_INSERT) {
if (p->header.nextindex ==
p->header.maxentry)
nsplit++;
else
nsplit = 0;
btstack->nsplit = nsplit;
}
/* save search result */
btsp = btstack->top;
btsp->bn = bn;
btsp->index = index;
btsp->mp = mp;
/* init sequential access heuristics */
btindex = jfs_ip->btindex;
if (index == btindex ||
index == btindex + 1)
jfs_ip->btorder = BT_SEQUENTIAL;
else
jfs_ip->btorder = BT_RANDOM;
jfs_ip->btindex = index;
return 0;
}
/* search hit - internal page:
* descend/search its child page
*/
if (index < le16_to_cpu(p->header.nextindex)-1)
next = offsetXAD(&p->xad[index + 1]);
goto next;
}
if (cmp > 0) {
base = index + 1;
--lim;
}
}
/*
* search miss
*
* base is the smallest index with key (Kj) greater than
* search key (K) and may be zero or maxentry index.
*/
if (base < le16_to_cpu(p->header.nextindex))
next = offsetXAD(&p->xad[base]);
/*
* search miss - leaf page:
*
* return location of entry (base) where new entry with
* search key K is to be inserted.
*/
if (p->header.flag & BT_LEAF) {
*cmpp = cmp;
/* compute number of pages to split */
if (flag & XT_INSERT) {
if (p->header.nextindex ==
p->header.maxentry)
nsplit++;
else
nsplit = 0;
btstack->nsplit = nsplit;
}
/* save search result */
btsp = btstack->top;
btsp->bn = bn;
btsp->index = base;
btsp->mp = mp;
/* init sequential access heuristics */
btindex = jfs_ip->btindex;
if (base == btindex || base == btindex + 1)
jfs_ip->btorder = BT_SEQUENTIAL;
else
jfs_ip->btorder = BT_RANDOM;
jfs_ip->btindex = base;
if (nextp)
*nextp = next;
return 0;
}
/*
* search miss - non-leaf page:
*
* if base is non-zero, decrement base by one to get the parent
* entry of the child page to search.
*/
index = base ? base - 1 : base;
/*
* go down to child page
*/
next:
/* update number of pages to split */
if (p->header.nextindex == p->header.maxentry)
nsplit++;
else
nsplit = 0;
/* push (bn, index) of the parent page/entry */
if (BT_STACK_FULL(btstack)) {
jfs_error(ip->i_sb, "stack overrun!\n");
XT_PUTPAGE(mp);
return -EIO;
}
BT_PUSH(btstack, bn, index);
/* get the child page block number */
bn = addressXAD(&p->xad[index]);
/* unpin the parent page */
XT_PUTPAGE(mp);
}
}
/*
* xtInsert()
*
* function:
*
* parameter:
* tid - transaction id;
* ip - file object;
* xflag - extent flag (XAD_NOTRECORDED):
* xoff - extent offset;
* xlen - extent length;
* xaddrp - extent address pointer (in/out):
* if (*xaddrp)
* caller allocated data extent at *xaddrp;
* else
* allocate data extent and return its xaddr;
* flag -
*
* return:
*/
int xtInsert(tid_t tid, /* transaction id */
struct inode *ip, int xflag, s64 xoff, s32 xlen, s64 * xaddrp,
int flag)
{
int rc = 0;
s64 xaddr, hint;
struct metapage *mp; /* meta-page buffer */
xtpage_t *p; /* base B+-tree index page */
s64 bn;
int index, nextindex;
struct btstack btstack; /* traverse stack */
struct xtsplit split; /* split information */
xad_t *xad;
int cmp;
s64 next;
struct tlock *tlck;
struct xtlock *xtlck;
jfs_info("xtInsert: nxoff:0x%lx nxlen:0x%x", (ulong) xoff, xlen);
/*
* search for the entry location at which to insert:
*
* xtFastSearch() and xtSearch() both returns (leaf page
* pinned, index at which to insert).
* n.b. xtSearch() may return index of maxentry of
* the full page.
*/
if ((rc = xtSearch(ip, xoff, &next, &cmp, &btstack, XT_INSERT)))
return rc;
/* retrieve search result */
XT_GETSEARCH(ip, btstack.top, bn, mp, p, index);
/* This test must follow XT_GETSEARCH since mp must be valid if
* we branch to out: */
if ((cmp == 0) || (next && (xlen > next - xoff))) {
rc = -EEXIST;
goto out;
}
/*
* allocate data extent requested
*
* allocation hint: last xad
*/
if ((xaddr = *xaddrp) == 0) {
if (index > XTENTRYSTART) {
xad = &p->xad[index - 1];
hint = addressXAD(xad) + lengthXAD(xad) - 1;
} else
hint = 0;
if ((rc = dquot_alloc_block(ip, xlen)))
goto out;
if ((rc = dbAlloc(ip, hint, (s64) xlen, &xaddr))) {
dquot_free_block(ip, xlen);
goto out;
}
}
/*
* insert entry for new extent
*/
xflag |= XAD_NEW;
/*
* if the leaf page is full, split the page and
* propagate up the router entry for the new page from split
*
* The xtSplitUp() will insert the entry and unpin the leaf page.
*/
nextindex = le16_to_cpu(p->header.nextindex);
if (nextindex == le16_to_cpu(p->header.maxentry)) {
split.mp = mp;
split.index = index;
split.flag = xflag;
split.off = xoff;
split.len = xlen;
split.addr = xaddr;
split.pxdlist = NULL;
if ((rc = xtSplitUp(tid, ip, &split, &btstack))) {
/* undo data extent allocation */
if (*xaddrp == 0) {
dbFree(ip, xaddr, (s64) xlen);
dquot_free_block(ip, xlen);
}
return rc;
}
*xaddrp = xaddr;
return 0;
}
/*
* insert the new entry into the leaf page
*/
/*
* acquire a transaction lock on the leaf page;
*
* action: xad insertion/extension;
*/
BT_MARK_DIRTY(mp, ip);
/* if insert into middle, shift right remaining entries. */
if (index < nextindex)
memmove(&p->xad[index + 1], &p->xad[index],
(nextindex - index) * sizeof(xad_t));
/* insert the new entry: mark the entry NEW */
xad = &p->xad[index];
XT_PUTENTRY(xad, xflag, xoff, xlen, xaddr);
/* advance next available entry index */
le16_add_cpu(&p->header.nextindex, 1);
/* Don't log it if there are no links to the file */
if (!test_cflag(COMMIT_Nolink, ip)) {
tlck = txLock(tid, ip, mp, tlckXTREE | tlckGROW);
xtlck = (struct xtlock *) & tlck->lock;
xtlck->lwm.offset =
(xtlck->lwm.offset) ? min(index,
(int)xtlck->lwm.offset) : index;
xtlck->lwm.length =
le16_to_cpu(p->header.nextindex) - xtlck->lwm.offset;
}
*xaddrp = xaddr;
out:
/* unpin the leaf page */
XT_PUTPAGE(mp);
return rc;
}
/*
* xtSplitUp()
*
* function:
* split full pages as propagating insertion up the tree
*
* parameter:
* tid - transaction id;
* ip - file object;
* split - entry parameter descriptor;
* btstack - traverse stack from xtSearch()
*
* return:
*/
static int
xtSplitUp(tid_t tid,
struct inode *ip, struct xtsplit * split, struct btstack * btstack)
{
int rc = 0;
struct metapage *smp;
xtpage_t *sp; /* split page */
struct metapage *rmp;
s64 rbn; /* new right page block number */
struct metapage *rcmp;
xtpage_t *rcp; /* right child page */
s64 rcbn; /* right child page block number */
int skip; /* index of entry of insertion */
int nextindex; /* next available entry index of p */
struct btframe *parent; /* parent page entry on traverse stack */
xad_t *xad;
s64 xaddr;
int xlen;
int nsplit; /* number of pages split */
struct pxdlist pxdlist;
pxd_t *pxd;
struct tlock *tlck;
struct xtlock *xtlck;
smp = split->mp;
sp = XT_PAGE(ip, smp);
/* is inode xtree root extension/inline EA area free ? */
if ((sp->header.flag & BT_ROOT) && (!S_ISDIR(ip->i_mode)) &&
(le16_to_cpu(sp->header.maxentry) < XTROOTMAXSLOT) &&
(JFS_IP(ip)->mode2 & INLINEEA)) {
sp->header.maxentry = cpu_to_le16(XTROOTMAXSLOT);
JFS_IP(ip)->mode2 &= ~INLINEEA;
BT_MARK_DIRTY(smp, ip);
/*
* acquire a transaction lock on the leaf page;
*
* action: xad insertion/extension;
*/
/* if insert into middle, shift right remaining entries. */
skip = split->index;
nextindex = le16_to_cpu(sp->header.nextindex);
if (skip < nextindex)
memmove(&sp->xad[skip + 1], &sp->xad[skip],
(nextindex - skip) * sizeof(xad_t));
/* insert the new entry: mark the entry NEW */
xad = &sp->xad[skip];
XT_PUTENTRY(xad, split->flag, split->off, split->len,
split->addr);
/* advance next available entry index */
le16_add_cpu(&sp->header.nextindex, 1);
/* Don't log it if there are no links to the file */
if (!test_cflag(COMMIT_Nolink, ip)) {
tlck = txLock(tid, ip, smp, tlckXTREE | tlckGROW);
xtlck = (struct xtlock *) & tlck->lock;
xtlck->lwm.offset = (xtlck->lwm.offset) ?
min(skip, (int)xtlck->lwm.offset) : skip;
xtlck->lwm.length =
le16_to_cpu(sp->header.nextindex) -
xtlck->lwm.offset;
}
return 0;
}
/*
* allocate new index blocks to cover index page split(s)
*
* allocation hint: ?
*/
if (split->pxdlist == NULL) {
nsplit = btstack->nsplit;
split->pxdlist = &pxdlist;
pxdlist.maxnpxd = pxdlist.npxd = 0;
pxd = &pxdlist.pxd[0];
xlen = JFS_SBI(ip->i_sb)->nbperpage;
for (; nsplit > 0; nsplit--, pxd++) {
if ((rc = dbAlloc(ip, (s64) 0, (s64) xlen, &xaddr))
== 0) {
PXDaddress(pxd, xaddr);
PXDlength(pxd, xlen);
pxdlist.maxnpxd++;
continue;
}
/* undo allocation */
XT_PUTPAGE(smp);
return rc;
}
}
/*
* Split leaf page <sp> into <sp> and a new right page <rp>.
*
* The split routines insert the new entry into the leaf page,
* and acquire txLock as appropriate.
* return <rp> pinned and its block number <rpbn>.
*/
rc = (sp->header.flag & BT_ROOT) ?
xtSplitRoot(tid, ip, split, &rmp) :
xtSplitPage(tid, ip, split, &rmp, &rbn);
XT_PUTPAGE(smp);
if (rc)
return -EIO;
/*
* propagate up the router entry for the leaf page just split
*
* insert a router entry for the new page into the parent page,
* propagate the insert/split up the tree by walking back the stack
* of (bn of parent page, index of child page entry in parent page)
* that were traversed during the search for the page that split.
*
* the propagation of insert/split up the tree stops if the root
* splits or the page inserted into doesn't have to split to hold
* the new entry.
*
* the parent entry for the split page remains the same, and
* a new entry is inserted at its right with the first key and
* block number of the new right page.
*
* There are a maximum of 3 pages pinned at any time:
* right child, left parent and right parent (when the parent splits)
* to keep the child page pinned while working on the parent.
* make sure that all pins are released at exit.
*/
while ((parent = BT_POP(btstack)) != NULL) {
/* parent page specified by stack frame <parent> */
/* keep current child pages <rcp> pinned */
rcmp = rmp;
rcbn = rbn;
rcp = XT_PAGE(ip, rcmp);
/*
* insert router entry in parent for new right child page <rp>
*/
/* get/pin the parent page <sp> */
XT_GETPAGE(ip, parent->bn, smp, PSIZE, sp, rc);
if (rc) {
XT_PUTPAGE(rcmp);
return rc;
}
/*
* The new key entry goes ONE AFTER the index of parent entry,
* because the split was to the right.
*/
skip = parent->index + 1;
/*
* split or shift right remaining entries of the parent page
*/
nextindex = le16_to_cpu(sp->header.nextindex);
/*
* parent page is full - split the parent page
*/
if (nextindex == le16_to_cpu(sp->header.maxentry)) {
/* init for parent page split */
split->mp = smp;
split->index = skip; /* index at insert */
split->flag = XAD_NEW;
split->off = offsetXAD(&rcp->xad[XTENTRYSTART]);
split->len = JFS_SBI(ip->i_sb)->nbperpage;
split->addr = rcbn;
/* unpin previous right child page */
XT_PUTPAGE(rcmp);
/* The split routines insert the new entry,
* and acquire txLock as appropriate.
* return <rp> pinned and its block number <rpbn>.
*/
rc = (sp->header.flag & BT_ROOT) ?
xtSplitRoot(tid, ip, split, &rmp) :
xtSplitPage(tid, ip, split, &rmp, &rbn);
if (rc) {
XT_PUTPAGE(smp);
return rc;
}
XT_PUTPAGE(smp);
/* keep new child page <rp> pinned */
}
/*
* parent page is not full - insert in parent page
*/
else {
/*
* insert router entry in parent for the right child
* page from the first entry of the right child page:
*/
/*
* acquire a transaction lock on the parent page;
*
* action: router xad insertion;
*/
BT_MARK_DIRTY(smp, ip);
/*
* if insert into middle, shift right remaining entries
*/
if (skip < nextindex)
memmove(&sp->xad[skip + 1], &sp->xad[skip],
(nextindex -
skip) << L2XTSLOTSIZE);
/* insert the router entry */
xad = &sp->xad[skip];
XT_PUTENTRY(xad, XAD_NEW,
offsetXAD(&rcp->xad[XTENTRYSTART]),
JFS_SBI(ip->i_sb)->nbperpage, rcbn);
/* advance next available entry index. */
le16_add_cpu(&sp->header.nextindex, 1);
/* Don't log it if there are no links to the file */
if (!test_cflag(COMMIT_Nolink, ip)) {
tlck = txLock(tid, ip, smp,
tlckXTREE | tlckGROW);
xtlck = (struct xtlock *) & tlck->lock;
xtlck->lwm.offset = (xtlck->lwm.offset) ?
min(skip, (int)xtlck->lwm.offset) : skip;
xtlck->lwm.length =
le16_to_cpu(sp->header.nextindex) -
xtlck->lwm.offset;
}
/* unpin parent page */
XT_PUTPAGE(smp);
/* exit propagate up */
break;
}
}
/* unpin current right page */
XT_PUTPAGE(rmp);
return 0;
}
/*
* xtSplitPage()
*
* function:
* split a full non-root page into
* original/split/left page and new right page
* i.e., the original/split page remains as left page.
*
* parameter:
* int tid,
* struct inode *ip,
* struct xtsplit *split,
* struct metapage **rmpp,
* u64 *rbnp,
*
* return:
* Pointer to page in which to insert or NULL on error.
*/
static int
xtSplitPage(tid_t tid, struct inode *ip,
struct xtsplit * split, struct metapage ** rmpp, s64 * rbnp)
{
int rc = 0;
struct metapage *smp;
xtpage_t *sp;
struct metapage *rmp;
xtpage_t *rp; /* new right page allocated */
s64 rbn; /* new right page block number */
struct metapage *mp;
xtpage_t *p;
s64 nextbn;
int skip, maxentry, middle, righthalf, n;
xad_t *xad;
struct pxdlist *pxdlist;
pxd_t *pxd;
struct tlock *tlck;
struct xtlock *sxtlck = NULL, *rxtlck = NULL;
int quota_allocation = 0;
smp = split->mp;
sp = XT_PAGE(ip, smp);
INCREMENT(xtStat.split);
pxdlist = split->pxdlist;
pxd = &pxdlist->pxd[pxdlist->npxd];
pxdlist->npxd++;
rbn = addressPXD(pxd);
/* Allocate blocks to quota. */
rc = dquot_alloc_block(ip, lengthPXD(pxd));
if (rc)
goto clean_up;
quota_allocation += lengthPXD(pxd);
/*
* allocate the new right page for the split
*/
rmp = get_metapage(ip, rbn, PSIZE, 1);
if (rmp == NULL) {
rc = -EIO;