2 * Copyright (C) International Business Machines Corp., 2000-2004
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
12 * the GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 * jfs_xtree.c: extent allocation descriptor B+-tree manager
23 #include <linux/quotaops.h>
24 #include "jfs_incore.h"
25 #include "jfs_filsys.h"
26 #include "jfs_metapage.h"
28 #include "jfs_dinode.h"
29 #include "jfs_superblock.h"
30 #include "jfs_debug.h"
35 #define XT_INSERT 0x00000001
38 * xtree key/entry comparison: extent offset
41 * -1: k < start of extent
42 * 0: start_of_extent <= k <= end_of_extent
43 * 1: k > end_of_extent
45 #define XT_CMP(CMP, K, X, OFFSET64)\
47 OFFSET64 = offsetXAD(X);\
48 (CMP) = ((K) >= OFFSET64 + lengthXAD(X)) ? 1 :\
49 ((K) < OFFSET64) ? -1 : 0;\
52 /* write a xad entry */
53 #define XT_PUTENTRY(XAD, FLAG, OFF, LEN, ADDR)\
55 (XAD)->flag = (FLAG);\
56 XADoffset((XAD), (OFF));\
57 XADlength((XAD), (LEN));\
58 XADaddress((XAD), (ADDR));\
61 #define XT_PAGE(IP, MP) BT_PAGE(IP, MP, xtpage_t, i_xtroot)
63 /* get page buffer for specified block address */
64 /* ToDo: Replace this ugly macro with a function */
65 #define XT_GETPAGE(IP, BN, MP, SIZE, P, RC)\
67 BT_GETPAGE(IP, BN, MP, xtpage_t, SIZE, P, RC, i_xtroot)\
70 if ((le16_to_cpu((P)->header.nextindex) < XTENTRYSTART) ||\
71 (le16_to_cpu((P)->header.nextindex) > le16_to_cpu((P)->header.maxentry)) ||\
72 (le16_to_cpu((P)->header.maxentry) > (((BN)==0)?XTROOTMAXSLOT:PSIZE>>L2XTSLOTSIZE)))\
74 jfs_error((IP)->i_sb, "XT_GETPAGE: xtree page corrupt");\
83 #define XT_PUTPAGE(MP) BT_PUTPAGE(MP)
85 #define XT_GETSEARCH(IP, LEAF, BN, MP, P, INDEX) \
86 BT_GETSEARCH(IP, LEAF, BN, MP, xtpage_t, P, INDEX, i_xtroot)
87 /* xtree entry parameter descriptor */
95 struct pxdlist
*pxdlist
;
102 #ifdef CONFIG_JFS_STATISTICS
114 static int xtSearch(struct inode
*ip
,
115 s64 xoff
, int *cmpp
, struct btstack
* btstack
, int flag
);
117 static int xtSplitUp(tid_t tid
,
119 struct xtsplit
* split
, struct btstack
* btstack
);
121 static int xtSplitPage(tid_t tid
, struct inode
*ip
, struct xtsplit
* split
,
122 struct metapage
** rmpp
, s64
* rbnp
);
124 static int xtSplitRoot(tid_t tid
, struct inode
*ip
,
125 struct xtsplit
* split
, struct metapage
** rmpp
);
127 #ifdef _STILL_TO_PORT
128 static int xtDeleteUp(tid_t tid
, struct inode
*ip
, struct metapage
* fmp
,
129 xtpage_t
* fp
, struct btstack
* btstack
);
131 static int xtSearchNode(struct inode
*ip
,
133 int *cmpp
, struct btstack
* btstack
, int flag
);
135 static int xtRelink(tid_t tid
, struct inode
*ip
, xtpage_t
* fp
);
136 #endif /* _STILL_TO_PORT */
138 /* External references */
143 /* #define _JFS_DEBUG_XTREE 1 */
149 * function: map a single page into a physical extent;
151 int xtLookup(struct inode
*ip
, s64 lstart
,
152 s64 llen
, int *pflag
, s64
* paddr
, s32
* plen
, int no_check
)
155 struct btstack btstack
;
162 s64 size
, xoff
, xend
;
169 /* is lookup offset beyond eof ? */
170 size
= ((u64
) ip
->i_size
+ (JFS_SBI(ip
->i_sb
)->bsize
- 1)) >>
171 JFS_SBI(ip
->i_sb
)->l2bsize
;
172 if (lstart
>= size
) {
173 jfs_err("xtLookup: lstart (0x%lx) >= size (0x%lx)",
174 (ulong
) lstart
, (ulong
) size
);
180 * search for the xad entry covering the logical extent
183 if ((rc
= xtSearch(ip
, lstart
, &cmp
, &btstack
, 0))) {
184 jfs_err("xtLookup: xtSearch returned %d", rc
);
189 * compute the physical extent covering logical extent
191 * N.B. search may have failed (e.g., hole in sparse file),
192 * and returned the index of the next entry.
194 /* retrieve search result */
195 XT_GETSEARCH(ip
, btstack
.top
, bn
, mp
, p
, index
);
197 /* is xad found covering start of logical extent ?
198 * lstart is a page start address,
199 * i.e., lstart cannot start in a hole;
207 xad
= &p
->xad
[index
];
208 xoff
= offsetXAD(xad
);
209 xlen
= lengthXAD(xad
);
211 xaddr
= addressXAD(xad
);
213 /* initialize new pxd */
215 *paddr
= xaddr
+ (lstart
- xoff
);
216 /* a page must be fully covered by an xad */
217 *plen
= min(xend
- lstart
, llen
);
229 * function: map a single logical extent into a list of physical extent;
233 * struct lxdlist *lxdlist, lxd list (in)
234 * struct xadlist *xadlist, xad list (in/out)
237 * coverage of lxd by xad under assumption of
238 * . lxd's are ordered and disjoint.
239 * . xad's are ordered and disjoint.
244 * note: a page being written (even a single byte) is backed fully,
245 * except the last page which is only backed with blocks
246 * required to cover the last byte;
247 * the extent backing a page is fully contained within an xad;
249 int xtLookupList(struct inode
*ip
, struct lxdlist
* lxdlist
,
250 struct xadlist
* xadlist
, int flag
)
253 struct btstack btstack
;
261 s64 size
, lstart
, lend
, xstart
, xend
, pstart
;
262 s64 llen
, xlen
, plen
;
264 int nlxd
, npxd
, maxnpxd
;
266 npxd
= xadlist
->nxad
= 0;
267 maxnpxd
= xadlist
->maxnxad
;
270 nlxd
= lxdlist
->nlxd
;
273 lstart
= offsetLXD(lxd
);
274 llen
= lengthLXD(lxd
);
275 lend
= lstart
+ llen
;
277 size
= (ip
->i_size
+ (JFS_SBI(ip
->i_sb
)->bsize
- 1)) >>
278 JFS_SBI(ip
->i_sb
)->l2bsize
;
281 * search for the xad entry covering the logical extent
287 if ((rc
= xtSearch(ip
, lstart
, &cmp
, &btstack
, 0)))
291 * compute the physical extent covering logical extent
293 * N.B. search may have failed (e.g., hole in sparse file),
294 * and returned the index of the next entry.
297 /* retrieve search result */
298 XT_GETSEARCH(ip
, btstack
.top
, bn
, mp
, p
, index
);
300 /* is xad on the next sibling page ? */
301 if (index
== le16_to_cpu(p
->header
.nextindex
)) {
302 if (p
->header
.flag
& BT_ROOT
)
305 if ((bn
= le64_to_cpu(p
->header
.next
)) == 0)
310 /* get next sibling page */
311 XT_GETPAGE(ip
, bn
, mp
, PSIZE
, p
, rc
);
315 index
= XTENTRYSTART
;
318 xad
= &p
->xad
[index
];
321 * is lxd covered by xad ?
324 xstart
= offsetXAD(xad
);
325 xlen
= lengthXAD(xad
);
326 xend
= xstart
+ xlen
;
327 xaddr
= addressXAD(xad
);
333 /* (lstart <= xstart) */
335 /* lxd is NOT covered by xad */
336 if (lend
<= xstart
) {
344 lstart
= offsetLXD(lxd
);
345 llen
= lengthLXD(lxd
);
346 lend
= lstart
+ llen
;
350 /* compare with the current xad */
353 /* lxd is covered by xad */
354 else { /* (xstart < lend) */
356 /* initialize new pxd */
358 plen
= min(lend
- xstart
, xlen
);
364 /* (xstart < lstart) */
366 /* lxd is covered by xad */
368 /* initialize new pxd */
370 plen
= min(xend
- lstart
, llen
);
371 paddr
= xaddr
+ (lstart
- xstart
);
375 /* lxd is NOT covered by xad */
376 else { /* (xend <= lstart) */
381 * linear search next xad covering lxd on
382 * the current xad page, and then tree search
384 if (index
== le16_to_cpu(p
->header
.nextindex
) - 1) {
385 if (p
->header
.flag
& BT_ROOT
)
394 /* compare with new xad */
400 * lxd is covered by xad and a new pxd has been initialized
401 * (lstart <= xstart < lend) or (xstart < lstart < xend)
404 /* finalize pxd corresponding to current xad */
405 XT_PUTENTRY(pxd
, xad
->flag
, pstart
, plen
, paddr
);
407 if (++npxd
>= maxnpxd
)
412 * lxd is fully covered by xad
422 lstart
= offsetLXD(lxd
);
423 llen
= lengthLXD(lxd
);
424 lend
= lstart
+ llen
;
429 * test for old xad covering new lxd
430 * (old xstart < new lstart)
435 * lxd is partially covered by xad
437 else { /* (xend < lend) */
442 * linear search next xad covering lxd on
443 * the current xad page, and then next xad page search
445 if (index
== le16_to_cpu(p
->header
.nextindex
) - 1) {
446 if (p
->header
.flag
& BT_ROOT
)
449 if ((bn
= le64_to_cpu(p
->header
.next
)) == 0)
454 /* get next sibling page */
455 XT_GETPAGE(ip
, bn
, mp
, PSIZE
, p
, rc
);
459 index
= XTENTRYSTART
;
460 xad
= &p
->xad
[index
];
467 * test for new xad covering old lxd
468 * (old lstart < new xstart)
474 xadlist
->nxad
= npxd
;
486 * function: search for the xad entry covering specified offset.
490 * xoff - extent offset;
491 * cmpp - comparison result:
492 * btstack - traverse stack;
493 * flag - search process flag (XT_INSERT);
496 * btstack contains (bn, index) of search path traversed to the entry.
497 * *cmpp is set to result of comparison with the entry returned.
498 * the page containing the entry is pinned at exit.
500 static int xtSearch(struct inode
*ip
, s64 xoff
, /* offset of extent */
501 int *cmpp
, struct btstack
* btstack
, int flag
)
503 struct jfs_inode_info
*jfs_ip
= JFS_IP(ip
);
505 int cmp
= 1; /* init for empty page */
506 s64 bn
; /* block number */
507 struct metapage
*mp
; /* page buffer */
508 xtpage_t
*p
; /* page */
510 int base
, index
, lim
, btindex
;
511 struct btframe
*btsp
;
512 int nsplit
= 0; /* number of pages to split */
515 INCREMENT(xtStat
.search
);
522 * search down tree from root:
524 * between two consecutive entries of <Ki, Pi> and <Kj, Pj> of
525 * internal page, child page Pi contains entry with k, Ki <= K < Kj.
527 * if entry with search key K is not found
528 * internal page search find the entry with largest key Ki
529 * less than K which point to the child page to search;
530 * leaf page search find the entry with smallest key Kj
531 * greater than K so that the returned index is the position of
532 * the entry to be shifted right for insertion of new entry.
533 * for empty tree, search key is greater than any key of the tree.
535 * by convention, root bn = 0.
538 /* get/pin the page to search */
539 XT_GETPAGE(ip
, bn
, mp
, PSIZE
, p
, rc
);
543 /* try sequential access heuristics with the previous
544 * access entry in target leaf page:
545 * once search narrowed down into the target leaf,
546 * key must either match an entry in the leaf or
547 * key entry does not exist in the tree;
550 if ((jfs_ip
->btorder
& BT_SEQUENTIAL
) &&
551 (p
->header
.flag
& BT_LEAF
) &&
552 (index
= jfs_ip
->btindex
) <
553 le16_to_cpu(p
->header
.nextindex
)) {
554 xad
= &p
->xad
[index
];
555 t64
= offsetXAD(xad
);
556 if (xoff
< t64
+ lengthXAD(xad
)) {
562 /* stop sequential access heuristics */
564 } else { /* (t64 + lengthXAD(xad)) <= xoff */
566 /* try next sequential entry */
569 le16_to_cpu(p
->header
.nextindex
)) {
571 t64
= offsetXAD(xad
);
572 if (xoff
< t64
+ lengthXAD(xad
)) {
578 /* miss: key falls between
579 * previous and this entry
585 /* (xoff >= t64 + lengthXAD(xad));
586 * matching entry may be further out:
587 * stop heuristic search
589 /* stop sequential access heuristics */
593 /* (index == p->header.nextindex);
594 * miss: key entry does not exist in
595 * the target leaf/tree
602 * if hit, return index of the entry found, and
603 * if miss, where new entry with search key is
607 /* compute number of pages to split */
608 if (flag
& XT_INSERT
) {
609 if (p
->header
.nextindex
== /* little-endian */
614 btstack
->nsplit
= nsplit
;
617 /* save search result */
623 /* update sequential access heuristics */
624 jfs_ip
->btindex
= index
;
626 INCREMENT(xtStat
.fastSearch
);
630 /* well, ... full search now */
632 lim
= le16_to_cpu(p
->header
.nextindex
) - XTENTRYSTART
;
635 * binary search with search key K on the current page
637 for (base
= XTENTRYSTART
; lim
; lim
>>= 1) {
638 index
= base
+ (lim
>> 1);
640 XT_CMP(cmp
, xoff
, &p
->xad
[index
], t64
);
645 /* search hit - leaf page:
646 * return the entry found
648 if (p
->header
.flag
& BT_LEAF
) {
651 /* compute number of pages to split */
652 if (flag
& XT_INSERT
) {
653 if (p
->header
.nextindex
==
658 btstack
->nsplit
= nsplit
;
661 /* save search result */
667 /* init sequential access heuristics */
668 btindex
= jfs_ip
->btindex
;
669 if (index
== btindex
||
670 index
== btindex
+ 1)
671 jfs_ip
->btorder
= BT_SEQUENTIAL
;
673 jfs_ip
->btorder
= BT_RANDOM
;
674 jfs_ip
->btindex
= index
;
679 /* search hit - internal page:
680 * descend/search its child page
694 * base is the smallest index with key (Kj) greater than
695 * search key (K) and may be zero or maxentry index.
698 * search miss - leaf page:
700 * return location of entry (base) where new entry with
701 * search key K is to be inserted.
703 if (p
->header
.flag
& BT_LEAF
) {
706 /* compute number of pages to split */
707 if (flag
& XT_INSERT
) {
708 if (p
->header
.nextindex
==
713 btstack
->nsplit
= nsplit
;
716 /* save search result */
722 /* init sequential access heuristics */
723 btindex
= jfs_ip
->btindex
;
724 if (base
== btindex
|| base
== btindex
+ 1)
725 jfs_ip
->btorder
= BT_SEQUENTIAL
;
727 jfs_ip
->btorder
= BT_RANDOM
;
728 jfs_ip
->btindex
= base
;
734 * search miss - non-leaf page:
736 * if base is non-zero, decrement base by one to get the parent
737 * entry of the child page to search.
739 index
= base
? base
- 1 : base
;
742 * go down to child page
745 /* update number of pages to split */
746 if (p
->header
.nextindex
== p
->header
.maxentry
)
751 /* push (bn, index) of the parent page/entry */
752 BT_PUSH(btstack
, bn
, index
);
754 /* get the child page block number */
755 bn
= addressXAD(&p
->xad
[index
]);
757 /* unpin the parent page */
768 * tid - transaction id;
770 * xflag - extent flag (XAD_NOTRECORDED):
771 * xoff - extent offset;
772 * xlen - extent length;
773 * xaddrp - extent address pointer (in/out):
775 * caller allocated data extent at *xaddrp;
777 * allocate data extent and return its xaddr;
782 int xtInsert(tid_t tid
, /* transaction id */
783 struct inode
*ip
, int xflag
, s64 xoff
, s32 xlen
, s64
* xaddrp
,
788 struct metapage
*mp
; /* meta-page buffer */
789 xtpage_t
*p
; /* base B+-tree index page */
791 int index
, nextindex
;
792 struct btstack btstack
; /* traverse stack */
793 struct xtsplit split
; /* split information */
797 struct xtlock
*xtlck
;
799 jfs_info("xtInsert: nxoff:0x%lx nxlen:0x%x", (ulong
) xoff
, xlen
);
802 * search for the entry location at which to insert:
804 * xtFastSearch() and xtSearch() both returns (leaf page
805 * pinned, index at which to insert).
806 * n.b. xtSearch() may return index of maxentry of
809 if ((rc
= xtSearch(ip
, xoff
, &cmp
, &btstack
, XT_INSERT
)))
812 /* retrieve search result */
813 XT_GETSEARCH(ip
, btstack
.top
, bn
, mp
, p
, index
);
815 /* This test must follow XT_GETSEARCH since mp must be valid if
816 * we branch to out: */
823 * allocate data extent requested
825 * allocation hint: last xad
827 if ((xaddr
= *xaddrp
) == 0) {
828 if (index
> XTENTRYSTART
) {
829 xad
= &p
->xad
[index
- 1];
830 hint
= addressXAD(xad
) + lengthXAD(xad
) - 1;
833 if ((rc
= DQUOT_ALLOC_BLOCK(ip
, xlen
)))
835 if ((rc
= dbAlloc(ip
, hint
, (s64
) xlen
, &xaddr
))) {
836 DQUOT_FREE_BLOCK(ip
, xlen
);
842 * insert entry for new extent
847 * if the leaf page is full, split the page and
848 * propagate up the router entry for the new page from split
850 * The xtSplitUp() will insert the entry and unpin the leaf page.
852 nextindex
= le16_to_cpu(p
->header
.nextindex
);
853 if (nextindex
== le16_to_cpu(p
->header
.maxentry
)) {
860 split
.pxdlist
= NULL
;
861 if ((rc
= xtSplitUp(tid
, ip
, &split
, &btstack
))) {
862 /* undo data extent allocation */
864 dbFree(ip
, xaddr
, (s64
) xlen
);
865 DQUOT_FREE_BLOCK(ip
, xlen
);
875 * insert the new entry into the leaf page
878 * acquire a transaction lock on the leaf page;
880 * action: xad insertion/extension;
882 BT_MARK_DIRTY(mp
, ip
);
884 /* if insert into middle, shift right remaining entries. */
885 if (index
< nextindex
)
886 memmove(&p
->xad
[index
+ 1], &p
->xad
[index
],
887 (nextindex
- index
) * sizeof(xad_t
));
889 /* insert the new entry: mark the entry NEW */
890 xad
= &p
->xad
[index
];
891 XT_PUTENTRY(xad
, xflag
, xoff
, xlen
, xaddr
);
893 /* advance next available entry index */
894 p
->header
.nextindex
=
895 cpu_to_le16(le16_to_cpu(p
->header
.nextindex
) + 1);
897 /* Don't log it if there are no links to the file */
898 if (!test_cflag(COMMIT_Nolink
, ip
)) {
899 tlck
= txLock(tid
, ip
, mp
, tlckXTREE
| tlckGROW
);
900 xtlck
= (struct xtlock
*) & tlck
->lock
;
902 (xtlck
->lwm
.offset
) ? min(index
,
903 (int)xtlck
->lwm
.offset
) : index
;
905 le16_to_cpu(p
->header
.nextindex
) - xtlck
->lwm
.offset
;
911 /* unpin the leaf page */
922 * split full pages as propagating insertion up the tree
925 * tid - transaction id;
927 * split - entry parameter descriptor;
928 * btstack - traverse stack from xtSearch()
934 struct inode
*ip
, struct xtsplit
* split
, struct btstack
* btstack
)
937 struct metapage
*smp
;
938 xtpage_t
*sp
; /* split page */
939 struct metapage
*rmp
;
940 s64 rbn
; /* new right page block number */
941 struct metapage
*rcmp
;
942 xtpage_t
*rcp
; /* right child page */
943 s64 rcbn
; /* right child page block number */
944 int skip
; /* index of entry of insertion */
945 int nextindex
; /* next available entry index of p */
946 struct btframe
*parent
; /* parent page entry on traverse stack */
950 int nsplit
; /* number of pages split */
951 struct pxdlist pxdlist
;
954 struct xtlock
*xtlck
;
957 sp
= XT_PAGE(ip
, smp
);
959 /* is inode xtree root extension/inline EA area free ? */
960 if ((sp
->header
.flag
& BT_ROOT
) && (!S_ISDIR(ip
->i_mode
)) &&
961 (le16_to_cpu(sp
->header
.maxentry
) < XTROOTMAXSLOT
) &&
962 (JFS_IP(ip
)->mode2
& INLINEEA
)) {
963 sp
->header
.maxentry
= cpu_to_le16(XTROOTMAXSLOT
);
964 JFS_IP(ip
)->mode2
&= ~INLINEEA
;
966 BT_MARK_DIRTY(smp
, ip
);
968 * acquire a transaction lock on the leaf page;
970 * action: xad insertion/extension;
973 /* if insert into middle, shift right remaining entries. */
975 nextindex
= le16_to_cpu(sp
->header
.nextindex
);
976 if (skip
< nextindex
)
977 memmove(&sp
->xad
[skip
+ 1], &sp
->xad
[skip
],
978 (nextindex
- skip
) * sizeof(xad_t
));
980 /* insert the new entry: mark the entry NEW */
981 xad
= &sp
->xad
[skip
];
982 XT_PUTENTRY(xad
, split
->flag
, split
->off
, split
->len
,
985 /* advance next available entry index */
986 sp
->header
.nextindex
=
987 cpu_to_le16(le16_to_cpu(sp
->header
.nextindex
) + 1);
989 /* Don't log it if there are no links to the file */
990 if (!test_cflag(COMMIT_Nolink
, ip
)) {
991 tlck
= txLock(tid
, ip
, smp
, tlckXTREE
| tlckGROW
);
992 xtlck
= (struct xtlock
*) & tlck
->lock
;
993 xtlck
->lwm
.offset
= (xtlck
->lwm
.offset
) ?
994 min(skip
, (int)xtlck
->lwm
.offset
) : skip
;
996 le16_to_cpu(sp
->header
.nextindex
) -
1004 * allocate new index blocks to cover index page split(s)
1006 * allocation hint: ?
1008 if (split
->pxdlist
== NULL
) {
1009 nsplit
= btstack
->nsplit
;
1010 split
->pxdlist
= &pxdlist
;
1011 pxdlist
.maxnpxd
= pxdlist
.npxd
= 0;
1012 pxd
= &pxdlist
.pxd
[0];
1013 xlen
= JFS_SBI(ip
->i_sb
)->nbperpage
;
1014 for (; nsplit
> 0; nsplit
--, pxd
++) {
1015 if ((rc
= dbAlloc(ip
, (s64
) 0, (s64
) xlen
, &xaddr
))
1017 PXDaddress(pxd
, xaddr
);
1018 PXDlength(pxd
, xlen
);
1025 /* undo allocation */
1033 * Split leaf page <sp> into <sp> and a new right page <rp>.
1035 * The split routines insert the new entry into the leaf page,
1036 * and acquire txLock as appropriate.
1037 * return <rp> pinned and its block number <rpbn>.
1039 rc
= (sp
->header
.flag
& BT_ROOT
) ?
1040 xtSplitRoot(tid
, ip
, split
, &rmp
) :
1041 xtSplitPage(tid
, ip
, split
, &rmp
, &rbn
);
1048 * propagate up the router entry for the leaf page just split
1050 * insert a router entry for the new page into the parent page,
1051 * propagate the insert/split up the tree by walking back the stack
1052 * of (bn of parent page, index of child page entry in parent page)
1053 * that were traversed during the search for the page that split.
1055 * the propagation of insert/split up the tree stops if the root
1056 * splits or the page inserted into doesn't have to split to hold
1059 * the parent entry for the split page remains the same, and
1060 * a new entry is inserted at its right with the first key and
1061 * block number of the new right page.
1063 * There are a maximum of 3 pages pinned at any time:
1064 * right child, left parent and right parent (when the parent splits)
1065 * to keep the child page pinned while working on the parent.
1066 * make sure that all pins are released at exit.
1068 while ((parent
= BT_POP(btstack
)) != NULL
) {
1069 /* parent page specified by stack frame <parent> */
1071 /* keep current child pages <rcp> pinned */
1074 rcp
= XT_PAGE(ip
, rcmp
);
1077 * insert router entry in parent for new right child page <rp>
1079 /* get/pin the parent page <sp> */
1080 XT_GETPAGE(ip
, parent
->bn
, smp
, PSIZE
, sp
, rc
);
1087 * The new key entry goes ONE AFTER the index of parent entry,
1088 * because the split was to the right.
1090 skip
= parent
->index
+ 1;
1093 * split or shift right remaining entries of the parent page
1095 nextindex
= le16_to_cpu(sp
->header
.nextindex
);
1097 * parent page is full - split the parent page
1099 if (nextindex
== le16_to_cpu(sp
->header
.maxentry
)) {
1100 /* init for parent page split */
1102 split
->index
= skip
; /* index at insert */
1103 split
->flag
= XAD_NEW
;
1104 split
->off
= offsetXAD(&rcp
->xad
[XTENTRYSTART
]);
1105 split
->len
= JFS_SBI(ip
->i_sb
)->nbperpage
;
1108 /* unpin previous right child page */
1111 /* The split routines insert the new entry,
1112 * and acquire txLock as appropriate.
1113 * return <rp> pinned and its block number <rpbn>.
1115 rc
= (sp
->header
.flag
& BT_ROOT
) ?
1116 xtSplitRoot(tid
, ip
, split
, &rmp
) :
1117 xtSplitPage(tid
, ip
, split
, &rmp
, &rbn
);
1124 /* keep new child page <rp> pinned */
1127 * parent page is not full - insert in parent page
1131 * insert router entry in parent for the right child
1132 * page from the first entry of the right child page:
1135 * acquire a transaction lock on the parent page;
1137 * action: router xad insertion;
1139 BT_MARK_DIRTY(smp
, ip
);
1142 * if insert into middle, shift right remaining entries
1144 if (skip
< nextindex
)
1145 memmove(&sp
->xad
[skip
+ 1], &sp
->xad
[skip
],
1147 skip
) << L2XTSLOTSIZE
);
1149 /* insert the router entry */
1150 xad
= &sp
->xad
[skip
];
1151 XT_PUTENTRY(xad
, XAD_NEW
,
1152 offsetXAD(&rcp
->xad
[XTENTRYSTART
]),
1153 JFS_SBI(ip
->i_sb
)->nbperpage
, rcbn
);
1155 /* advance next available entry index. */
1156 sp
->header
.nextindex
=
1157 cpu_to_le16(le16_to_cpu(sp
->header
.nextindex
) +
1160 /* Don't log it if there are no links to the file */
1161 if (!test_cflag(COMMIT_Nolink
, ip
)) {
1162 tlck
= txLock(tid
, ip
, smp
,
1163 tlckXTREE
| tlckGROW
);
1164 xtlck
= (struct xtlock
*) & tlck
->lock
;
1165 xtlck
->lwm
.offset
= (xtlck
->lwm
.offset
) ?
1166 min(skip
, (int)xtlck
->lwm
.offset
) : skip
;
1168 le16_to_cpu(sp
->header
.nextindex
) -
1172 /* unpin parent page */
1175 /* exit propagate up */
1180 /* unpin current right page */
1191 * split a full non-root page into
1192 * original/split/left page and new right page
1193 * i.e., the original/split page remains as left page.
1198 * struct xtsplit *split,
1199 * struct metapage **rmpp,
1203 * Pointer to page in which to insert or NULL on error.
1206 xtSplitPage(tid_t tid
, struct inode
*ip
,
1207 struct xtsplit
* split
, struct metapage
** rmpp
, s64
* rbnp
)
1210 struct metapage
*smp
;
1212 struct metapage
*rmp
;
1213 xtpage_t
*rp
; /* new right page allocated */
1214 s64 rbn
; /* new right page block number */
1215 struct metapage
*mp
;
1218 int skip
, maxentry
, middle
, righthalf
, n
;
1220 struct pxdlist
*pxdlist
;
1223 struct xtlock
*sxtlck
= NULL
, *rxtlck
= NULL
;
1224 int quota_allocation
= 0;
1227 sp
= XT_PAGE(ip
, smp
);
1229 INCREMENT(xtStat
.split
);
1231 pxdlist
= split
->pxdlist
;
1232 pxd
= &pxdlist
->pxd
[pxdlist
->npxd
];
1234 rbn
= addressPXD(pxd
);
1236 /* Allocate blocks to quota. */
1237 if (DQUOT_ALLOC_BLOCK(ip
, lengthPXD(pxd
))) {
1242 quota_allocation
+= lengthPXD(pxd
);
1245 * allocate the new right page for the split
1247 rmp
= get_metapage(ip
, rbn
, PSIZE
, 1);
1253 jfs_info("xtSplitPage: ip:0x%p smp:0x%p rmp:0x%p", ip
, smp
, rmp
);
1255 BT_MARK_DIRTY(rmp
, ip
);
1260 rp
= (xtpage_t
*) rmp
->data
;
1261 rp
->header
.self
= *pxd
;
1262 rp
->header
.flag
= sp
->header
.flag
& BT_TYPE
;
1263 rp
->header
.maxentry
= sp
->header
.maxentry
; /* little-endian */
1264 rp
->header
.nextindex
= cpu_to_le16(XTENTRYSTART
);
1266 BT_MARK_DIRTY(smp
, ip
);
1267 /* Don't log it if there are no links to the file */
1268 if (!test_cflag(COMMIT_Nolink
, ip
)) {
1270 * acquire a transaction lock on the new right page;
1272 tlck
= txLock(tid
, ip
, rmp
, tlckXTREE
| tlckNEW
);
1273 rxtlck
= (struct xtlock
*) & tlck
->lock
;
1274 rxtlck
->lwm
.offset
= XTENTRYSTART
;
1276 * acquire a transaction lock on the split page
1278 tlck
= txLock(tid
, ip
, smp
, tlckXTREE
| tlckGROW
);
1279 sxtlck
= (struct xtlock
*) & tlck
->lock
;
1283 * initialize/update sibling pointers of <sp> and <rp>
1285 nextbn
= le64_to_cpu(sp
->header
.next
);
1286 rp
->header
.next
= cpu_to_le64(nextbn
);
1287 rp
->header
.prev
= cpu_to_le64(addressPXD(&sp
->header
.self
));
1288 sp
->header
.next
= cpu_to_le64(rbn
);
1290 skip
= split
->index
;
1293 * sequential append at tail (after last entry of last page)
1295 * if splitting the last page on a level because of appending
1296 * a entry to it (skip is maxentry), it's likely that the access is
1297 * sequential. adding an empty page on the side of the level is less
1298 * work and can push the fill factor much higher than normal.
1299 * if we're wrong it's no big deal - we will do the split the right
1301 * (it may look like it's equally easy to do a similar hack for
1302 * reverse sorted data, that is, split the tree left, but it's not.
1305 if (nextbn
== 0 && skip
== le16_to_cpu(sp
->header
.maxentry
)) {
1307 * acquire a transaction lock on the new/right page;
1309 * action: xad insertion;
1311 /* insert entry at the first entry of the new right page */
1312 xad
= &rp
->xad
[XTENTRYSTART
];
1313 XT_PUTENTRY(xad
, split
->flag
, split
->off
, split
->len
,
1316 rp
->header
.nextindex
= cpu_to_le16(XTENTRYSTART
+ 1);
1318 if (!test_cflag(COMMIT_Nolink
, ip
)) {
1319 /* rxtlck->lwm.offset = XTENTRYSTART; */
1320 rxtlck
->lwm
.length
= 1;
1326 jfs_info("xtSplitPage: sp:0x%p rp:0x%p", sp
, rp
);
1331 * non-sequential insert (at possibly middle page)
1335 * update previous pointer of old next/right page of <sp>
1338 XT_GETPAGE(ip
, nextbn
, mp
, PSIZE
, p
, rc
);
1344 BT_MARK_DIRTY(mp
, ip
);
1346 * acquire a transaction lock on the next page;
1348 * action:sibling pointer update;
1350 if (!test_cflag(COMMIT_Nolink
, ip
))
1351 tlck
= txLock(tid
, ip
, mp
, tlckXTREE
| tlckRELINK
);
1353 p
->header
.prev
= cpu_to_le64(rbn
);
1355 /* sibling page may have been updated previously, or
1356 * it may be updated later;
1363 * split the data between the split and new/right pages
1365 maxentry
= le16_to_cpu(sp
->header
.maxentry
);
1366 middle
= maxentry
>> 1;
1367 righthalf
= maxentry
- middle
;
1370 * skip index in old split/left page - insert into left page:
1372 if (skip
<= middle
) {
1373 /* move right half of split page to the new right page */
1374 memmove(&rp
->xad
[XTENTRYSTART
], &sp
->xad
[middle
],
1375 righthalf
<< L2XTSLOTSIZE
);
1377 /* shift right tail of left half to make room for new entry */
1379 memmove(&sp
->xad
[skip
+ 1], &sp
->xad
[skip
],
1380 (middle
- skip
) << L2XTSLOTSIZE
);
1382 /* insert new entry */
1383 xad
= &sp
->xad
[skip
];
1384 XT_PUTENTRY(xad
, split
->flag
, split
->off
, split
->len
,
1387 /* update page header */
1388 sp
->header
.nextindex
= cpu_to_le16(middle
+ 1);
1389 if (!test_cflag(COMMIT_Nolink
, ip
)) {
1390 sxtlck
->lwm
.offset
= (sxtlck
->lwm
.offset
) ?
1391 min(skip
, (int)sxtlck
->lwm
.offset
) : skip
;
1394 rp
->header
.nextindex
=
1395 cpu_to_le16(XTENTRYSTART
+ righthalf
);
1398 * skip index in new right page - insert into right page:
1401 /* move left head of right half to right page */
1403 memmove(&rp
->xad
[XTENTRYSTART
], &sp
->xad
[middle
],
1406 /* insert new entry */
1409 XT_PUTENTRY(xad
, split
->flag
, split
->off
, split
->len
,
1412 /* move right tail of right half to right page */
1413 if (skip
< maxentry
)
1414 memmove(&rp
->xad
[n
+ 1], &sp
->xad
[skip
],
1415 (maxentry
- skip
) << L2XTSLOTSIZE
);
1417 /* update page header */
1418 sp
->header
.nextindex
= cpu_to_le16(middle
);
1419 if (!test_cflag(COMMIT_Nolink
, ip
)) {
1420 sxtlck
->lwm
.offset
= (sxtlck
->lwm
.offset
) ?
1421 min(middle
, (int)sxtlck
->lwm
.offset
) : middle
;
1424 rp
->header
.nextindex
= cpu_to_le16(XTENTRYSTART
+
1428 if (!test_cflag(COMMIT_Nolink
, ip
)) {
1429 sxtlck
->lwm
.length
= le16_to_cpu(sp
->header
.nextindex
) -
1432 /* rxtlck->lwm.offset = XTENTRYSTART; */
1433 rxtlck
->lwm
.length
= le16_to_cpu(rp
->header
.nextindex
) -
1440 jfs_info("xtSplitPage: sp:0x%p rp:0x%p", sp
, rp
);
1445 /* Rollback quota allocation. */
1446 if (quota_allocation
)
1447 DQUOT_FREE_BLOCK(ip
, quota_allocation
);
1457 * split the full root page into
1458 * original/root/split page and new right page
1459 * i.e., root remains fixed in tree anchor (inode) and
1460 * the root is copied to a single new right child page
1461 * since root page << non-root page, and
1462 * the split root page contains a single entry for the
1463 * new right child page.
1468 * struct xtsplit *split,
1469 * struct metapage **rmpp)
1472 * Pointer to page in which to insert or NULL on error.
1475 xtSplitRoot(tid_t tid
,
1476 struct inode
*ip
, struct xtsplit
* split
, struct metapage
** rmpp
)
1479 struct metapage
*rmp
;
1482 int skip
, nextindex
;
1485 struct pxdlist
*pxdlist
;
1487 struct xtlock
*xtlck
;
1489 sp
= &JFS_IP(ip
)->i_xtroot
;
1491 INCREMENT(xtStat
.split
);
1494 * allocate a single (right) child page
1496 pxdlist
= split
->pxdlist
;
1497 pxd
= &pxdlist
->pxd
[pxdlist
->npxd
];
1499 rbn
= addressPXD(pxd
);
1500 rmp
= get_metapage(ip
, rbn
, PSIZE
, 1);
1504 /* Allocate blocks to quota. */
1505 if (DQUOT_ALLOC_BLOCK(ip
, lengthPXD(pxd
))) {
1506 release_metapage(rmp
);
1510 jfs_info("xtSplitRoot: ip:0x%p rmp:0x%p", ip
, rmp
);
1513 * acquire a transaction lock on the new right page;
1517 BT_MARK_DIRTY(rmp
, ip
);
1519 rp
= (xtpage_t
*) rmp
->data
;
1521 (sp
->header
.flag
& BT_LEAF
) ? BT_LEAF
: BT_INTERNAL
;
1522 rp
->header
.self
= *pxd
;
1523 rp
->header
.nextindex
= cpu_to_le16(XTENTRYSTART
);
1524 rp
->header
.maxentry
= cpu_to_le16(PSIZE
>> L2XTSLOTSIZE
);
1526 /* initialize sibling pointers */
1527 rp
->header
.next
= 0;
1528 rp
->header
.prev
= 0;
1531 * copy the in-line root page into new right page extent
1533 nextindex
= le16_to_cpu(sp
->header
.maxentry
);
1534 memmove(&rp
->xad
[XTENTRYSTART
], &sp
->xad
[XTENTRYSTART
],
1535 (nextindex
- XTENTRYSTART
) << L2XTSLOTSIZE
);
1538 * insert the new entry into the new right/child page
1539 * (skip index in the new right page will not change)
1541 skip
= split
->index
;
1542 /* if insert into middle, shift right remaining entries */
1543 if (skip
!= nextindex
)
1544 memmove(&rp
->xad
[skip
+ 1], &rp
->xad
[skip
],
1545 (nextindex
- skip
) * sizeof(xad_t
));
1547 xad
= &rp
->xad
[skip
];
1548 XT_PUTENTRY(xad
, split
->flag
, split
->off
, split
->len
, split
->addr
);
1550 /* update page header */
1551 rp
->header
.nextindex
= cpu_to_le16(nextindex
+ 1);
1553 if (!test_cflag(COMMIT_Nolink
, ip
)) {
1554 tlck
= txLock(tid
, ip
, rmp
, tlckXTREE
| tlckNEW
);
1555 xtlck
= (struct xtlock
*) & tlck
->lock
;
1556 xtlck
->lwm
.offset
= XTENTRYSTART
;
1557 xtlck
->lwm
.length
= le16_to_cpu(rp
->header
.nextindex
) -
1564 * init root with the single entry for the new right page
1565 * set the 1st entry offset to 0, which force the left-most key
1566 * at any level of the tree to be less than any search key.
1569 * acquire a transaction lock on the root page (in-memory inode);
1571 * action: root split;
1573 BT_MARK_DIRTY(split
->mp
, ip
);
1575 xad
= &sp
->xad
[XTENTRYSTART
];
1576 XT_PUTENTRY(xad
, XAD_NEW
, 0, JFS_SBI(ip
->i_sb
)->nbperpage
, rbn
);
1578 /* update page header of root */
1579 sp
->header
.flag
&= ~BT_LEAF
;
1580 sp
->header
.flag
|= BT_INTERNAL
;
1582 sp
->header
.nextindex
= cpu_to_le16(XTENTRYSTART
+ 1);
1584 if (!test_cflag(COMMIT_Nolink
, ip
)) {
1585 tlck
= txLock(tid
, ip
, split
->mp
, tlckXTREE
| tlckGROW
);
1586 xtlck
= (struct xtlock
*) & tlck
->lock
;
1587 xtlck
->lwm
.offset
= XTENTRYSTART
;
1588 xtlck
->lwm
.length
= 1;
1593 jfs_info("xtSplitRoot: sp:0x%p rp:0x%p", sp
, rp
);
1601 * function: extend in-place;
1603 * note: existing extent may or may not have been committed.
1604 * caller is responsible for pager buffer cache update, and
1605 * working block allocation map update;
1606 * update pmap: alloc whole extended extent;
1608 int xtExtend(tid_t tid
, /* transaction id */
1609 struct inode
*ip
, s64 xoff
, /* delta extent offset */
1610 s32 xlen
, /* delta extent length */
1615 struct metapage
*mp
; /* meta-page buffer */
1616 xtpage_t
*p
; /* base B+-tree index page */
1618 int index
, nextindex
, len
;
1619 struct btstack btstack
; /* traverse stack */
1620 struct xtsplit split
; /* split information */
1624 struct xtlock
*xtlck
= NULL
;
1626 jfs_info("xtExtend: nxoff:0x%lx nxlen:0x%x", (ulong
) xoff
, xlen
);
1628 /* there must exist extent to be extended */
1629 if ((rc
= xtSearch(ip
, xoff
- 1, &cmp
, &btstack
, XT_INSERT
)))
1632 /* retrieve search result */
1633 XT_GETSEARCH(ip
, btstack
.top
, bn
, mp
, p
, index
);
1637 jfs_error(ip
->i_sb
, "xtExtend: xtSearch did not find extent");
1641 /* extension must be contiguous */
1642 xad
= &p
->xad
[index
];
1643 if ((offsetXAD(xad
) + lengthXAD(xad
)) != xoff
) {
1645 jfs_error(ip
->i_sb
, "xtExtend: extension is not contiguous");
1650 * acquire a transaction lock on the leaf page;
1652 * action: xad insertion/extension;
1654 BT_MARK_DIRTY(mp
, ip
);
1655 if (!test_cflag(COMMIT_Nolink
, ip
)) {
1656 tlck
= txLock(tid
, ip
, mp
, tlckXTREE
| tlckGROW
);
1657 xtlck
= (struct xtlock
*) & tlck
->lock
;
1660 /* extend will overflow extent ? */
1661 xlen
= lengthXAD(xad
) + xlen
;
1662 if ((len
= xlen
- MAXXLEN
) <= 0)
1666 * extent overflow: insert entry for new extent
1669 xoff
= offsetXAD(xad
) + MAXXLEN
;
1670 xaddr
= addressXAD(xad
) + MAXXLEN
;
1671 nextindex
= le16_to_cpu(p
->header
.nextindex
);
1674 * if the leaf page is full, insert the new entry and
1675 * propagate up the router entry for the new page from split
1677 * The xtSplitUp() will insert the entry and unpin the leaf page.
1679 if (nextindex
== le16_to_cpu(p
->header
.maxentry
)) {
1680 /* xtSpliUp() unpins leaf pages */
1682 split
.index
= index
+ 1;
1683 split
.flag
= XAD_NEW
;
1684 split
.off
= xoff
; /* split offset */
1687 split
.pxdlist
= NULL
;
1688 if ((rc
= xtSplitUp(tid
, ip
, &split
, &btstack
)))
1691 /* get back old page */
1692 XT_GETPAGE(ip
, bn
, mp
, PSIZE
, p
, rc
);
1696 * if leaf root has been split, original root has been
1697 * copied to new child page, i.e., original entry now
1698 * resides on the new child page;
1700 if (p
->header
.flag
& BT_INTERNAL
) {
1701 ASSERT(p
->header
.nextindex
==
1702 cpu_to_le16(XTENTRYSTART
+ 1));
1703 xad
= &p
->xad
[XTENTRYSTART
];
1704 bn
= addressXAD(xad
);
1707 /* get new child page */
1708 XT_GETPAGE(ip
, bn
, mp
, PSIZE
, p
, rc
);
1712 BT_MARK_DIRTY(mp
, ip
);
1713 if (!test_cflag(COMMIT_Nolink
, ip
)) {
1714 tlck
= txLock(tid
, ip
, mp
, tlckXTREE
|tlckGROW
);
1715 xtlck
= (struct xtlock
*) & tlck
->lock
;
1720 * insert the new entry into the leaf page
1723 /* insert the new entry: mark the entry NEW */
1724 xad
= &p
->xad
[index
+ 1];
1725 XT_PUTENTRY(xad
, XAD_NEW
, xoff
, len
, xaddr
);
1727 /* advance next available entry index */
1728 p
->header
.nextindex
=
1729 cpu_to_le16(le16_to_cpu(p
->header
.nextindex
) + 1);
1732 /* get back old entry */
1733 xad
= &p
->xad
[index
];
1740 XADlength(xad
, xlen
);
1741 if (!(xad
->flag
& XAD_NEW
))
1742 xad
->flag
|= XAD_EXTENDED
;
1744 if (!test_cflag(COMMIT_Nolink
, ip
)) {
1746 (xtlck
->lwm
.offset
) ? min(index
,
1747 (int)xtlck
->lwm
.offset
) : index
;
1749 le16_to_cpu(p
->header
.nextindex
) - xtlck
->lwm
.offset
;
1752 /* unpin the leaf page */
1762 * function: split existing 'tail' extent
1763 * (split offset >= start offset of tail extent), and
1764 * relocate and extend the split tail half;
1766 * note: existing extent may or may not have been committed.
1767 * caller is responsible for pager buffer cache update, and
1768 * working block allocation map update;
1769 * update pmap: free old split tail extent, alloc new extent;
1771 int xtTailgate(tid_t tid
, /* transaction id */
1772 struct inode
*ip
, s64 xoff
, /* split/new extent offset */
1773 s32 xlen
, /* new extent length */
1774 s64 xaddr
, /* new extent address */
1779 struct metapage
*mp
; /* meta-page buffer */
1780 xtpage_t
*p
; /* base B+-tree index page */
1782 int index
, nextindex
, llen
, rlen
;
1783 struct btstack btstack
; /* traverse stack */
1784 struct xtsplit split
; /* split information */
1787 struct xtlock
*xtlck
= 0;
1788 struct tlock
*mtlck
;
1789 struct maplock
*pxdlock
;
1792 printf("xtTailgate: nxoff:0x%lx nxlen:0x%x nxaddr:0x%lx\n",
1793 (ulong)xoff, xlen, (ulong)xaddr);
1796 /* there must exist extent to be tailgated */
1797 if ((rc
= xtSearch(ip
, xoff
, &cmp
, &btstack
, XT_INSERT
)))
1800 /* retrieve search result */
1801 XT_GETSEARCH(ip
, btstack
.top
, bn
, mp
, p
, index
);
1805 jfs_error(ip
->i_sb
, "xtTailgate: couldn't find extent");
1809 /* entry found must be last entry */
1810 nextindex
= le16_to_cpu(p
->header
.nextindex
);
1811 if (index
!= nextindex
- 1) {
1814 "xtTailgate: the entry found is not the last entry");
1818 BT_MARK_DIRTY(mp
, ip
);
1820 * acquire tlock of the leaf page containing original entry
1822 if (!test_cflag(COMMIT_Nolink
, ip
)) {
1823 tlck
= txLock(tid
, ip
, mp
, tlckXTREE
| tlckGROW
);
1824 xtlck
= (struct xtlock
*) & tlck
->lock
;
1827 /* completely replace extent ? */
1828 xad
= &p
->xad
[index
];
1830 printf("xtTailgate: xoff:0x%lx xlen:0x%x xaddr:0x%lx\n",
1831 (ulong)offsetXAD(xad), lengthXAD(xad), (ulong)addressXAD(xad));
1833 if ((llen
= xoff
- offsetXAD(xad
)) == 0)
1837 * partially replace extent: insert entry for new extent
1841 * if the leaf page is full, insert the new entry and
1842 * propagate up the router entry for the new page from split
1844 * The xtSplitUp() will insert the entry and unpin the leaf page.
1846 if (nextindex
== le16_to_cpu(p
->header
.maxentry
)) {
1847 /* xtSpliUp() unpins leaf pages */
1849 split
.index
= index
+ 1;
1850 split
.flag
= XAD_NEW
;
1851 split
.off
= xoff
; /* split offset */
1854 split
.pxdlist
= NULL
;
1855 if ((rc
= xtSplitUp(tid
, ip
, &split
, &btstack
)))
1858 /* get back old page */
1859 XT_GETPAGE(ip
, bn
, mp
, PSIZE
, p
, rc
);
1863 * if leaf root has been split, original root has been
1864 * copied to new child page, i.e., original entry now
1865 * resides on the new child page;
1867 if (p
->header
.flag
& BT_INTERNAL
) {
1868 ASSERT(p
->header
.nextindex
==
1869 cpu_to_le16(XTENTRYSTART
+ 1));
1870 xad
= &p
->xad
[XTENTRYSTART
];
1871 bn
= addressXAD(xad
);
1874 /* get new child page */
1875 XT_GETPAGE(ip
, bn
, mp
, PSIZE
, p
, rc
);
1879 BT_MARK_DIRTY(mp
, ip
);
1880 if (!test_cflag(COMMIT_Nolink
, ip
)) {
1881 tlck
= txLock(tid
, ip
, mp
, tlckXTREE
|tlckGROW
);
1882 xtlck
= (struct xtlock
*) & tlck
->lock
;
1887 * insert the new entry into the leaf page
1890 /* insert the new entry: mark the entry NEW */
1891 xad
= &p
->xad
[index
+ 1];
1892 XT_PUTENTRY(xad
, XAD_NEW
, xoff
, xlen
, xaddr
);
1894 /* advance next available entry index */
1895 p
->header
.nextindex
=
1896 cpu_to_le16(le16_to_cpu(p
->header
.nextindex
) + 1);
1899 /* get back old XAD */
1900 xad
= &p
->xad
[index
];
1903 * truncate/relocate old extent at split offset
1906 /* update dmap for old/committed/truncated extent */
1907 rlen
= lengthXAD(xad
) - llen
;
1908 if (!(xad
->flag
& XAD_NEW
)) {
1909 /* free from PWMAP at commit */
1910 if (!test_cflag(COMMIT_Nolink
, ip
)) {
1911 mtlck
= txMaplock(tid
, ip
, tlckMAP
);
1912 pxdlock
= (struct maplock
*) & mtlck
->lock
;
1913 pxdlock
->flag
= mlckFREEPXD
;
1914 PXDaddress(&pxdlock
->pxd
, addressXAD(xad
) + llen
);
1915 PXDlength(&pxdlock
->pxd
, rlen
);
1919 /* free from WMAP */
1920 dbFree(ip
, addressXAD(xad
) + llen
, (s64
) rlen
);
1924 XADlength(xad
, llen
);
1927 XT_PUTENTRY(xad
, XAD_NEW
, xoff
, xlen
, xaddr
);
1929 if (!test_cflag(COMMIT_Nolink
, ip
)) {
1930 xtlck
->lwm
.offset
= (xtlck
->lwm
.offset
) ?
1931 min(index
, (int)xtlck
->lwm
.offset
) : index
;
1932 xtlck
->lwm
.length
= le16_to_cpu(p
->header
.nextindex
) -
1936 /* unpin the leaf page */
1941 #endif /* _NOTYET */
1946 * function: update XAD;
1948 * update extent for allocated_but_not_recorded or
1949 * compressed extent;
1953 * logical extent of the specified XAD must be completely
1954 * contained by an existing XAD;
1956 int xtUpdate(tid_t tid
, struct inode
*ip
, xad_t
* nxad
)
1960 struct metapage
*mp
; /* meta-page buffer */
1961 xtpage_t
*p
; /* base B+-tree index page */
1963 int index0
, index
, newindex
, nextindex
;
1964 struct btstack btstack
; /* traverse stack */
1965 struct xtsplit split
; /* split information */
1966 xad_t
*xad
, *lxad
, *rxad
;
1969 int nxlen
, xlen
, lxlen
, rxlen
;
1972 struct xtlock
*xtlck
= NULL
;
1975 /* there must exist extent to be tailgated */
1976 nxoff
= offsetXAD(nxad
);
1977 nxlen
= lengthXAD(nxad
);
1978 nxaddr
= addressXAD(nxad
);
1980 if ((rc
= xtSearch(ip
, nxoff
, &cmp
, &btstack
, XT_INSERT
)))
1983 /* retrieve search result */
1984 XT_GETSEARCH(ip
, btstack
.top
, bn
, mp
, p
, index0
);
1988 jfs_error(ip
->i_sb
, "xtUpdate: Could not find extent");
1992 BT_MARK_DIRTY(mp
, ip
);
1994 * acquire tlock of the leaf page containing original entry
1996 if (!test_cflag(COMMIT_Nolink
, ip
)) {
1997 tlck
= txLock(tid
, ip
, mp
, tlckXTREE
| tlckGROW
);
1998 xtlck
= (struct xtlock
*) & tlck
->lock
;
2001 xad
= &p
->xad
[index0
];
2003 xoff
= offsetXAD(xad
);
2004 xlen
= lengthXAD(xad
);
2005 xaddr
= addressXAD(xad
);
2007 /* nXAD must be completely contained within XAD */
2008 if ((xoff
> nxoff
) ||
2009 (nxoff
+ nxlen
> xoff
+ xlen
)) {
2012 "xtUpdate: nXAD in not completely contained within XAD");
2017 newindex
= index
+ 1;
2018 nextindex
= le16_to_cpu(p
->header
.nextindex
);
2020 #ifdef _JFS_WIP_NOCOALESCE
2025 * replace XAD with nXAD
2027 replace
: /* (nxoff == xoff) */
2028 if (nxlen
== xlen
) {
2029 /* replace XAD with nXAD:recorded */
2031 xad
->flag
= xflag
& ~XAD_NOTRECORDED
;
2034 } else /* (nxlen < xlen) */
2036 #endif /* _JFS_WIP_NOCOALESCE */
2038 /* #ifdef _JFS_WIP_COALESCE */
2043 * coalesce with left XAD
2045 //coalesceLeft: /* (xoff == nxoff) */
2046 /* is XAD first entry of page ? */
2047 if (index
== XTENTRYSTART
)
2050 /* is nXAD logically and physically contiguous with lXAD ? */
2051 lxad
= &p
->xad
[index
- 1];
2052 lxlen
= lengthXAD(lxad
);
2053 if (!(lxad
->flag
& XAD_NOTRECORDED
) &&
2054 (nxoff
== offsetXAD(lxad
) + lxlen
) &&
2055 (nxaddr
== addressXAD(lxad
) + lxlen
) &&
2056 (lxlen
+ nxlen
< MAXXLEN
)) {
2057 /* extend right lXAD */
2059 XADlength(lxad
, lxlen
+ nxlen
);
2061 /* If we just merged two extents together, need to make sure the
2062 * right extent gets logged. If the left one is marked XAD_NEW,
2063 * then we know it will be logged. Otherwise, mark as
2066 if (!(lxad
->flag
& XAD_NEW
))
2067 lxad
->flag
|= XAD_EXTENDED
;
2071 XADoffset(xad
, xoff
+ nxlen
);
2072 XADlength(xad
, xlen
- nxlen
);
2073 XADaddress(xad
, xaddr
+ nxlen
);
2075 } else { /* (xlen == nxlen) */
2078 if (index
< nextindex
- 1)
2079 memmove(&p
->xad
[index
], &p
->xad
[index
+ 1],
2080 (nextindex
- index
-
2081 1) << L2XTSLOTSIZE
);
2083 p
->header
.nextindex
=
2084 cpu_to_le16(le16_to_cpu(p
->header
.nextindex
) -
2088 newindex
= index
+ 1;
2089 nextindex
= le16_to_cpu(p
->header
.nextindex
);
2090 xoff
= nxoff
= offsetXAD(lxad
);
2091 xlen
= nxlen
= lxlen
+ nxlen
;
2092 xaddr
= nxaddr
= addressXAD(lxad
);
2098 * replace XAD with nXAD
2100 replace
: /* (nxoff == xoff) */
2101 if (nxlen
== xlen
) {
2102 /* replace XAD with nXAD:recorded */
2104 xad
->flag
= xflag
& ~XAD_NOTRECORDED
;
2107 } else /* (nxlen < xlen) */
2111 * coalesce with right XAD
2113 coalesceRight
: /* (xoff <= nxoff) */
2114 /* is XAD last entry of page ? */
2115 if (newindex
== nextindex
) {
2121 /* is nXAD logically and physically contiguous with rXAD ? */
2122 rxad
= &p
->xad
[index
+ 1];
2123 rxlen
= lengthXAD(rxad
);
2124 if (!(rxad
->flag
& XAD_NOTRECORDED
) &&
2125 (nxoff
+ nxlen
== offsetXAD(rxad
)) &&
2126 (nxaddr
+ nxlen
== addressXAD(rxad
)) &&
2127 (rxlen
+ nxlen
< MAXXLEN
)) {
2128 /* extend left rXAD */
2129 XADoffset(rxad
, nxoff
);
2130 XADlength(rxad
, rxlen
+ nxlen
);
2131 XADaddress(rxad
, nxaddr
);
2133 /* If we just merged two extents together, need to make sure
2134 * the left extent gets logged. If the right one is marked
2135 * XAD_NEW, then we know it will be logged. Otherwise, mark as
2138 if (!(rxad
->flag
& XAD_NEW
))
2139 rxad
->flag
|= XAD_EXTENDED
;
2143 XADlength(xad
, xlen
- nxlen
);
2144 else { /* (xlen == nxlen) */
2147 memmove(&p
->xad
[index
], &p
->xad
[index
+ 1],
2148 (nextindex
- index
- 1) << L2XTSLOTSIZE
);
2150 p
->header
.nextindex
=
2151 cpu_to_le16(le16_to_cpu(p
->header
.nextindex
) -
2156 } else if (xoff
== nxoff
)
2159 if (xoff
>= nxoff
) {
2161 jfs_error(ip
->i_sb
, "xtUpdate: xoff >= nxoff");
2164 /* #endif _JFS_WIP_COALESCE */
2167 * split XAD into (lXAD, nXAD):
2170 * --|----------XAD----------|--
2173 updateRight
: /* (xoff < nxoff) */
2174 /* truncate old XAD as lXAD:not_recorded */
2175 xad
= &p
->xad
[index
];
2176 XADlength(xad
, nxoff
- xoff
);
2178 /* insert nXAD:recorded */
2179 if (nextindex
== le16_to_cpu(p
->header
.maxentry
)) {
2181 /* xtSpliUp() unpins leaf pages */
2183 split
.index
= newindex
;
2184 split
.flag
= xflag
& ~XAD_NOTRECORDED
;
2187 split
.addr
= nxaddr
;
2188 split
.pxdlist
= NULL
;
2189 if ((rc
= xtSplitUp(tid
, ip
, &split
, &btstack
)))
2192 /* get back old page */
2193 XT_GETPAGE(ip
, bn
, mp
, PSIZE
, p
, rc
);
2197 * if leaf root has been split, original root has been
2198 * copied to new child page, i.e., original entry now
2199 * resides on the new child page;
2201 if (p
->header
.flag
& BT_INTERNAL
) {
2202 ASSERT(p
->header
.nextindex
==
2203 cpu_to_le16(XTENTRYSTART
+ 1));
2204 xad
= &p
->xad
[XTENTRYSTART
];
2205 bn
= addressXAD(xad
);
2208 /* get new child page */
2209 XT_GETPAGE(ip
, bn
, mp
, PSIZE
, p
, rc
);
2213 BT_MARK_DIRTY(mp
, ip
);
2214 if (!test_cflag(COMMIT_Nolink
, ip
)) {
2215 tlck
= txLock(tid
, ip
, mp
, tlckXTREE
|tlckGROW
);
2216 xtlck
= (struct xtlock
*) & tlck
->lock
;
2219 /* is nXAD on new page ? */
2221 (le16_to_cpu(p
->header
.maxentry
) >> 1)) {
2224 le16_to_cpu(p
->header
.nextindex
) +
2230 /* if insert into middle, shift right remaining entries */
2231 if (newindex
< nextindex
)
2232 memmove(&p
->xad
[newindex
+ 1], &p
->xad
[newindex
],
2233 (nextindex
- newindex
) << L2XTSLOTSIZE
);
2235 /* insert the entry */
2236 xad
= &p
->xad
[newindex
];
2238 xad
->flag
= xflag
& ~XAD_NOTRECORDED
;
2240 /* advance next available entry index. */
2241 p
->header
.nextindex
=
2242 cpu_to_le16(le16_to_cpu(p
->header
.nextindex
) + 1);
2246 * does nXAD force 3-way split ?
2249 * --|----------XAD-------------|--
2250 * |-lXAD-| |-rXAD -|
2252 if (nxoff
+ nxlen
== xoff
+ xlen
)
2255 /* reorient nXAD as XAD for further split XAD into (nXAD, rXAD) */
2257 /* close out old page */
2258 if (!test_cflag(COMMIT_Nolink
, ip
)) {
2259 xtlck
->lwm
.offset
= (xtlck
->lwm
.offset
) ?
2260 min(index0
, (int)xtlck
->lwm
.offset
) : index0
;
2262 le16_to_cpu(p
->header
.nextindex
) -
2266 bn
= le64_to_cpu(p
->header
.next
);
2269 /* get new right page */
2270 XT_GETPAGE(ip
, bn
, mp
, PSIZE
, p
, rc
);
2274 BT_MARK_DIRTY(mp
, ip
);
2275 if (!test_cflag(COMMIT_Nolink
, ip
)) {
2276 tlck
= txLock(tid
, ip
, mp
, tlckXTREE
| tlckGROW
);
2277 xtlck
= (struct xtlock
*) & tlck
->lock
;
2280 index0
= index
= newindex
;
2284 newindex
= index
+ 1;
2285 nextindex
= le16_to_cpu(p
->header
.nextindex
);
2286 xlen
= xlen
- (nxoff
- xoff
);
2290 /* recompute split pages */
2291 if (nextindex
== le16_to_cpu(p
->header
.maxentry
)) {
2294 if ((rc
= xtSearch(ip
, nxoff
, &cmp
, &btstack
, XT_INSERT
)))
2297 /* retrieve search result */
2298 XT_GETSEARCH(ip
, btstack
.top
, bn
, mp
, p
, index0
);
2302 jfs_error(ip
->i_sb
, "xtUpdate: xtSearch failed");
2306 if (index0
!= index
) {
2309 "xtUpdate: unexpected value of index");
2315 * split XAD into (nXAD, rXAD)
2318 * --|----------XAD----------|--
2321 updateLeft
: /* (nxoff == xoff) && (nxlen < xlen) */
2322 /* update old XAD with nXAD:recorded */
2323 xad
= &p
->xad
[index
];
2325 xad
->flag
= xflag
& ~XAD_NOTRECORDED
;
2327 /* insert rXAD:not_recorded */
2328 xoff
= xoff
+ nxlen
;
2329 xlen
= xlen
- nxlen
;
2330 xaddr
= xaddr
+ nxlen
;
2331 if (nextindex
== le16_to_cpu(p
->header
.maxentry
)) {
2333 printf("xtUpdate.updateLeft.split p:0x%p\n", p);
2335 /* xtSpliUp() unpins leaf pages */
2337 split
.index
= newindex
;
2342 split
.pxdlist
= NULL
;
2343 if ((rc
= xtSplitUp(tid
, ip
, &split
, &btstack
)))
2346 /* get back old page */
2347 XT_GETPAGE(ip
, bn
, mp
, PSIZE
, p
, rc
);
2352 * if leaf root has been split, original root has been
2353 * copied to new child page, i.e., original entry now
2354 * resides on the new child page;
2356 if (p
->header
.flag
& BT_INTERNAL
) {
2357 ASSERT(p
->header
.nextindex
==
2358 cpu_to_le16(XTENTRYSTART
+ 1));
2359 xad
= &p
->xad
[XTENTRYSTART
];
2360 bn
= addressXAD(xad
);
2363 /* get new child page */
2364 XT_GETPAGE(ip
, bn
, mp
, PSIZE
, p
, rc
);
2368 BT_MARK_DIRTY(mp
, ip
);
2369 if (!test_cflag(COMMIT_Nolink
, ip
)) {
2370 tlck
= txLock(tid
, ip
, mp
, tlckXTREE
|tlckGROW
);
2371 xtlck
= (struct xtlock
*) & tlck
->lock
;
2375 /* if insert into middle, shift right remaining entries */
2376 if (newindex
< nextindex
)
2377 memmove(&p
->xad
[newindex
+ 1], &p
->xad
[newindex
],
2378 (nextindex
- newindex
) << L2XTSLOTSIZE
);
2380 /* insert the entry */
2381 xad
= &p
->xad
[newindex
];
2382 XT_PUTENTRY(xad
, xflag
, xoff
, xlen
, xaddr
);
2384 /* advance next available entry index. */
2385 p
->header
.nextindex
=
2386 cpu_to_le16(le16_to_cpu(p
->header
.nextindex
) + 1);
2390 if (!test_cflag(COMMIT_Nolink
, ip
)) {
2391 xtlck
->lwm
.offset
= (xtlck
->lwm
.offset
) ?
2392 min(index0
, (int)xtlck
->lwm
.offset
) : index0
;
2393 xtlck
->lwm
.length
= le16_to_cpu(p
->header
.nextindex
) -
2397 /* unpin the leaf page */
2407 * function: grow in append mode from contiguous region specified ;
2410 * tid - transaction id;
2412 * xflag - extent flag:
2413 * xoff - extent offset;
2414 * maxblocks - max extent length;
2415 * xlen - extent length (in/out);
2416 * xaddrp - extent address pointer (in/out):
2421 int xtAppend(tid_t tid
, /* transaction id */
2422 struct inode
*ip
, int xflag
, s64 xoff
, s32 maxblocks
,
2423 s32
* xlenp
, /* (in/out) */
2424 s64
* xaddrp
, /* (in/out) */
2428 struct metapage
*mp
; /* meta-page buffer */
2429 xtpage_t
*p
; /* base B+-tree index page */
2431 int index
, nextindex
;
2432 struct btstack btstack
; /* traverse stack */
2433 struct xtsplit split
; /* split information */
2437 struct xtlock
*xtlck
;
2438 int nsplit
, nblocks
, xlen
;
2439 struct pxdlist pxdlist
;
2444 jfs_info("xtAppend: xoff:0x%lx maxblocks:%d xlen:%d xaddr:0x%lx",
2445 (ulong
) xoff
, maxblocks
, xlen
, (ulong
) xaddr
);
2448 * search for the entry location at which to insert:
2450 * xtFastSearch() and xtSearch() both returns (leaf page
2451 * pinned, index at which to insert).
2452 * n.b. xtSearch() may return index of maxentry of
2455 if ((rc
= xtSearch(ip
, xoff
, &cmp
, &btstack
, XT_INSERT
)))
2458 /* retrieve search result */
2459 XT_GETSEARCH(ip
, btstack
.top
, bn
, mp
, p
, index
);
2467 * insert entry for new extent
2472 * if the leaf page is full, split the page and
2473 * propagate up the router entry for the new page from split
2475 * The xtSplitUp() will insert the entry and unpin the leaf page.
2477 nextindex
= le16_to_cpu(p
->header
.nextindex
);
2478 if (nextindex
< le16_to_cpu(p
->header
.maxentry
))
2482 * allocate new index blocks to cover index page split(s)
2484 nsplit
= btstack
.nsplit
;
2485 split
.pxdlist
= &pxdlist
;
2486 pxdlist
.maxnpxd
= pxdlist
.npxd
= 0;
2487 pxd
= &pxdlist
.pxd
[0];
2488 nblocks
= JFS_SBI(ip
->i_sb
)->nbperpage
;
2489 for (; nsplit
> 0; nsplit
--, pxd
++, xaddr
+= nblocks
, maxblocks
-= nblocks
) {
2490 if ((rc
= dbAllocBottomUp(ip
, xaddr
, (s64
) nblocks
)) == 0) {
2491 PXDaddress(pxd
, xaddr
);
2492 PXDlength(pxd
, nblocks
);
2499 /* undo allocation */
2504 xlen
= min(xlen
, maxblocks
);
2507 * allocate data extent requested
2509 if ((rc
= dbAllocBottomUp(ip
, xaddr
, (s64
) xlen
)))
2513 split
.index
= index
;
2518 if ((rc
= xtSplitUp(tid
, ip
, &split
, &btstack
))) {
2519 /* undo data extent allocation */
2520 dbFree(ip
, *xaddrp
, (s64
) * xlenp
);
2530 * insert the new entry into the leaf page
2534 * allocate data extent requested
2536 if ((rc
= dbAllocBottomUp(ip
, xaddr
, (s64
) xlen
)))
2539 BT_MARK_DIRTY(mp
, ip
);
2541 * acquire a transaction lock on the leaf page;
2543 * action: xad insertion/extension;
2545 tlck
= txLock(tid
, ip
, mp
, tlckXTREE
| tlckGROW
);
2546 xtlck
= (struct xtlock
*) & tlck
->lock
;
2548 /* insert the new entry: mark the entry NEW */
2549 xad
= &p
->xad
[index
];
2550 XT_PUTENTRY(xad
, xflag
, xoff
, xlen
, xaddr
);
2552 /* advance next available entry index */
2553 p
->header
.nextindex
=
2554 cpu_to_le16(le16_to_cpu(p
->header
.nextindex
) + 1);
2557 (xtlck
->lwm
.offset
) ? min(index
,(int) xtlck
->lwm
.offset
) : index
;
2558 xtlck
->lwm
.length
= le16_to_cpu(p
->header
.nextindex
) -
2565 /* unpin the leaf page */
2570 #ifdef _STILL_TO_PORT
2572 /* - TBD for defragmentaion/reorganization -
2577 * delete the entry with the specified key.
2579 * N.B.: whole extent of the entry is assumed to be deleted.
2584 * ENOENT: if the entry is not found.
2588 int xtDelete(tid_t tid
, struct inode
*ip
, s64 xoff
, s32 xlen
, int flag
)
2591 struct btstack btstack
;
2594 struct metapage
*mp
;
2596 int index
, nextindex
;
2598 struct xtlock
*xtlck
;
2601 * find the matching entry; xtSearch() pins the page
2603 if ((rc
= xtSearch(ip
, xoff
, &cmp
, &btstack
, 0)))
2606 XT_GETSEARCH(ip
, btstack
.top
, bn
, mp
, p
, index
);
2608 /* unpin the leaf page */
2614 * delete the entry from the leaf page
2616 nextindex
= le16_to_cpu(p
->header
.nextindex
);
2617 p
->header
.nextindex
=
2618 cpu_to_le16(le16_to_cpu(p
->header
.nextindex
) - 1);
2621 * if the leaf page bocome empty, free the page
2623 if (p
->header
.nextindex
== cpu_to_le16(XTENTRYSTART
))
2624 return (xtDeleteUp(tid
, ip
, mp
, p
, &btstack
));
2626 BT_MARK_DIRTY(mp
, ip
);
2628 * acquire a transaction lock on the leaf page;
2630 * action:xad deletion;
2632 tlck
= txLock(tid
, ip
, mp
, tlckXTREE
);
2633 xtlck
= (struct xtlock
*) & tlck
->lock
;
2635 (xtlck
->lwm
.offset
) ? min(index
, xtlck
->lwm
.offset
) : index
;
2637 /* if delete from middle, shift left/compact the remaining entries */
2638 if (index
< nextindex
- 1)
2639 memmove(&p
->xad
[index
], &p
->xad
[index
+ 1],
2640 (nextindex
- index
- 1) * sizeof(xad_t
));
2648 /* - TBD for defragmentaion/reorganization -
2653 * free empty pages as propagating deletion up the tree
2660 xtDeleteUp(tid_t tid
, struct inode
*ip
,
2661 struct metapage
* fmp
, xtpage_t
* fp
, struct btstack
* btstack
)
2664 struct metapage
*mp
;
2666 int index
, nextindex
;
2669 struct btframe
*parent
;
2671 struct xtlock
*xtlck
;
2674 * keep root leaf page which has become empty
2676 if (fp
->header
.flag
& BT_ROOT
) {
2677 /* keep the root page */
2678 fp
->header
.flag
&= ~BT_INTERNAL
;
2679 fp
->header
.flag
|= BT_LEAF
;
2680 fp
->header
.nextindex
= cpu_to_le16(XTENTRYSTART
);
2682 /* XT_PUTPAGE(fmp); */
2688 * free non-root leaf page
2690 if ((rc
= xtRelink(tid
, ip
, fp
))) {
2695 xaddr
= addressPXD(&fp
->header
.self
);
2696 xlen
= lengthPXD(&fp
->header
.self
);
2697 /* free the page extent */
2698 dbFree(ip
, xaddr
, (s64
) xlen
);
2700 /* free the buffer page */
2701 discard_metapage(fmp
);
2704 * propagate page deletion up the index tree
2706 * If the delete from the parent page makes it empty,
2707 * continue all the way up the tree.
2708 * stop if the root page is reached (which is never deleted) or
2709 * if the entry deletion does not empty the page.
2711 while ((parent
= BT_POP(btstack
)) != NULL
) {
2712 /* get/pin the parent page <sp> */
2713 XT_GETPAGE(ip
, parent
->bn
, mp
, PSIZE
, p
, rc
);
2717 index
= parent
->index
;
2719 /* delete the entry for the freed child page from parent.
2721 nextindex
= le16_to_cpu(p
->header
.nextindex
);
2724 * the parent has the single entry being deleted:
2725 * free the parent page which has become empty.
2727 if (nextindex
== 1) {
2728 if (p
->header
.flag
& BT_ROOT
) {
2729 /* keep the root page */
2730 p
->header
.flag
&= ~BT_INTERNAL
;
2731 p
->header
.flag
|= BT_LEAF
;
2732 p
->header
.nextindex
=
2733 cpu_to_le16(XTENTRYSTART
);
2735 /* XT_PUTPAGE(mp); */
2739 /* free the parent page */
2740 if ((rc
= xtRelink(tid
, ip
, p
)))
2743 xaddr
= addressPXD(&p
->header
.self
);
2744 /* free the page extent */
2746 (s64
) JFS_SBI(ip
->i_sb
)->nbperpage
);
2748 /* unpin/free the buffer page */
2749 discard_metapage(mp
);
2756 * the parent has other entries remaining:
2757 * delete the router entry from the parent page.
2760 BT_MARK_DIRTY(mp
, ip
);
2762 * acquire a transaction lock on the leaf page;
2764 * action:xad deletion;
2766 tlck
= txLock(tid
, ip
, mp
, tlckXTREE
);
2767 xtlck
= (struct xtlock
*) & tlck
->lock
;
2769 (xtlck
->lwm
.offset
) ? min(index
,
2773 /* if delete from middle,
2774 * shift left/compact the remaining entries in the page
2776 if (index
< nextindex
- 1)
2777 memmove(&p
->xad
[index
], &p
->xad
[index
+ 1],
2778 (nextindex
- index
-
2779 1) << L2XTSLOTSIZE
);
2781 p
->header
.nextindex
=
2782 cpu_to_le16(le16_to_cpu(p
->header
.nextindex
) -
2784 jfs_info("xtDeleteUp(entry): 0x%lx[%d]",
2785 (ulong
) parent
->bn
, index
);
2788 /* unpin the parent page */
2791 /* exit propagation up */
2800 * NAME: xtRelocate()
2802 * FUNCTION: relocate xtpage or data extent of regular file;
2803 * This function is mainly used by defragfs utility.
2805 * NOTE: This routine does not have the logic to handle
2806 * uncommitted allocated extent. The caller should call
2807 * txCommit() to commit all the allocation before call
2811 xtRelocate(tid_t tid
, struct inode
* ip
, xad_t
* oxad
, /* old XAD */
2812 s64 nxaddr
, /* new xaddr */
2814 { /* extent type: XTPAGE or DATAEXT */
2816 struct tblock
*tblk
;
2818 struct xtlock
*xtlck
;
2819 struct metapage
*mp
, *pmp
, *lmp
, *rmp
; /* meta-page buffer */
2820 xtpage_t
*p
, *pp
, *rp
, *lp
; /* base B+-tree index page */
2825 s64 oxaddr
, sxaddr
, dxaddr
, nextbn
, prevbn
;
2827 s64 offset
, nbytes
, nbrd
, pno
;
2828 int nb
, npages
, nblks
;
2832 struct pxd_lock
*pxdlock
;
2833 struct btstack btstack
; /* traverse stack */
2835 xtype
= xtype
& EXTENT_TYPE
;
2837 xoff
= offsetXAD(oxad
);
2838 oxaddr
= addressXAD(oxad
);
2839 xlen
= lengthXAD(oxad
);
2841 /* validate extent offset */
2842 offset
= xoff
<< JFS_SBI(ip
->i_sb
)->l2bsize
;
2843 if (offset
>= ip
->i_size
)
2844 return -ESTALE
; /* stale extent */
2846 jfs_info("xtRelocate: xtype:%d xoff:0x%lx xlen:0x%x xaddr:0x%lx:0x%lx",
2847 xtype
, (ulong
) xoff
, xlen
, (ulong
) oxaddr
, (ulong
) nxaddr
);
2850 * 1. get and validate the parent xtpage/xad entry
2851 * covering the source extent to be relocated;
2853 if (xtype
== DATAEXT
) {
2854 /* search in leaf entry */
2855 rc
= xtSearch(ip
, xoff
, &cmp
, &btstack
, 0);
2859 /* retrieve search result */
2860 XT_GETSEARCH(ip
, btstack
.top
, bn
, pmp
, pp
, index
);
2867 /* validate for exact match with a single entry */
2868 xad
= &pp
->xad
[index
];
2869 if (addressXAD(xad
) != oxaddr
|| lengthXAD(xad
) != xlen
) {
2873 } else { /* (xtype == XTPAGE) */
2875 /* search in internal entry */
2876 rc
= xtSearchNode(ip
, oxad
, &cmp
, &btstack
, 0);
2880 /* retrieve search result */
2881 XT_GETSEARCH(ip
, btstack
.top
, bn
, pmp
, pp
, index
);
2888 /* xtSearchNode() validated for exact match with a single entry
2890 xad
= &pp
->xad
[index
];
2892 jfs_info("xtRelocate: parent xad entry validated.");
2895 * 2. relocate the extent
2897 if (xtype
== DATAEXT
) {
2898 /* if the extent is allocated-but-not-recorded
2899 * there is no real data to be moved in this extent,
2901 if (xad
->flag
& XAD_NOTRECORDED
)
2904 /* release xtpage for cmRead()/xtLookup() */
2910 * copy target data pages to be relocated;
2912 * data extent must start at page boundary and
2913 * multiple of page size (except the last data extent);
2914 * read in each page of the source data extent into cbuf,
2915 * update the cbuf extent descriptor of the page to be
2916 * homeward bound to new dst data extent
2917 * copy the data from the old extent to new extent.
2918 * copy is essential for compressed files to avoid problems
2919 * that can arise if there was a change in compression
2921 * it is a good strategy because it may disrupt cache
2922 * policy to keep the pages in memory afterwards.
2924 offset
= xoff
<< JFS_SBI(ip
->i_sb
)->l2bsize
;
2925 assert((offset
& CM_OFFSET
) == 0);
2926 nbytes
= xlen
<< JFS_SBI(ip
->i_sb
)->l2bsize
;
2927 pno
= offset
>> CM_L2BSIZE
;
2928 npages
= (nbytes
+ (CM_BSIZE
- 1)) >> CM_L2BSIZE
;
2930 npages = ((offset + nbytes - 1) >> CM_L2BSIZE) -
2931 (offset >> CM_L2BSIZE) + 1;
2936 /* process the request one cache buffer at a time */
2937 for (nbrd
= 0; nbrd
< nbytes
; nbrd
+= nb
,
2938 offset
+= nb
, pno
++, npages
--) {
2939 /* compute page size */
2940 nb
= min(nbytes
- nbrd
, CM_BSIZE
);
2942 /* get the cache buffer of the page */
2943 if (rc
= cmRead(ip
, offset
, npages
, &cp
))
2946 assert(addressPXD(&cp
->cm_pxd
) == sxaddr
);
2947 assert(!cp
->cm_modified
);
2949 /* bind buffer with the new extent address */
2950 nblks
= nb
>> JFS_IP(ip
->i_sb
)->l2bsize
;
2951 cmSetXD(ip
, cp
, pno
, dxaddr
, nblks
);
2953 /* release the cbuf, mark it as modified */
2960 /* get back parent page */
2961 if ((rc
= xtSearch(ip
, xoff
, &cmp
, &btstack
, 0)))
2964 XT_GETSEARCH(ip
, btstack
.top
, bn
, pmp
, pp
, index
);
2965 jfs_info("xtRelocate: target data extent relocated.");
2966 } else { /* (xtype == XTPAGE) */
2969 * read in the target xtpage from the source extent;
2971 XT_GETPAGE(ip
, oxaddr
, mp
, PSIZE
, p
, rc
);
2978 * read in sibling pages if any to update sibling pointers;
2981 if (p
->header
.next
) {
2982 nextbn
= le64_to_cpu(p
->header
.next
);
2983 XT_GETPAGE(ip
, nextbn
, rmp
, PSIZE
, rp
, rc
);
2992 if (p
->header
.prev
) {
2993 prevbn
= le64_to_cpu(p
->header
.prev
);
2994 XT_GETPAGE(ip
, prevbn
, lmp
, PSIZE
, lp
, rc
);
3004 /* at this point, all xtpages to be updated are in memory */
3007 * update sibling pointers of sibling xtpages if any;
3010 BT_MARK_DIRTY(lmp
, ip
);
3012 txLock(tid
, ip
, lmp
, tlckXTREE
| tlckRELINK
);
3013 lp
->header
.next
= cpu_to_le64(nxaddr
);
3018 BT_MARK_DIRTY(rmp
, ip
);
3020 txLock(tid
, ip
, rmp
, tlckXTREE
| tlckRELINK
);
3021 rp
->header
.prev
= cpu_to_le64(nxaddr
);
3026 * update the target xtpage to be relocated
3028 * update the self address of the target page
3029 * and write to destination extent;
3030 * redo image covers the whole xtpage since it is new page
3031 * to the destination extent;
3032 * update of bmap for the free of source extent
3033 * of the target xtpage itself:
3034 * update of bmap for the allocation of destination extent
3035 * of the target xtpage itself:
3036 * update of bmap for the extents covered by xad entries in
3037 * the target xtpage is not necessary since they are not
3039 * if not committed before this relocation,
3040 * target page may contain XAD_NEW entries which must
3041 * be scanned for bmap update (logredo() always
3042 * scan xtpage REDOPAGE image for bmap update);
3043 * if committed before this relocation (tlckRELOCATE),
3044 * scan may be skipped by commit() and logredo();
3046 BT_MARK_DIRTY(mp
, ip
);
3047 /* tlckNEW init xtlck->lwm.offset = XTENTRYSTART; */
3048 tlck
= txLock(tid
, ip
, mp
, tlckXTREE
| tlckNEW
);
3049 xtlck
= (struct xtlock
*) & tlck
->lock
;
3051 /* update the self address in the xtpage header */
3052 pxd
= &p
->header
.self
;
3053 PXDaddress(pxd
, nxaddr
);
3055 /* linelock for the after image of the whole page */
3057 le16_to_cpu(p
->header
.nextindex
) - xtlck
->lwm
.offset
;
3059 /* update the buffer extent descriptor of target xtpage */
3060 xsize
= xlen
<< JFS_SBI(ip
->i_sb
)->l2bsize
;
3061 bmSetXD(mp
, nxaddr
, xsize
);
3063 /* unpin the target page to new homeward bound */
3065 jfs_info("xtRelocate: target xtpage relocated.");
3069 * 3. acquire maplock for the source extent to be freed;
3071 * acquire a maplock saving the src relocated extent address;
3072 * to free of the extent at commit time;
3075 /* if DATAEXT relocation, write a LOG_UPDATEMAP record for
3076 * free PXD of the source data extent (logredo() will update
3077 * bmap for free of source data extent), and update bmap for
3078 * free of the source data extent;
3080 if (xtype
== DATAEXT
)
3081 tlck
= txMaplock(tid
, ip
, tlckMAP
);
3082 /* if XTPAGE relocation, write a LOG_NOREDOPAGE record
3083 * for the source xtpage (logredo() will init NoRedoPage
3084 * filter and will also update bmap for free of the source
3085 * xtpage), and update bmap for free of the source xtpage;
3086 * N.B. We use tlckMAP instead of tlkcXTREE because there
3087 * is no buffer associated with this lock since the buffer
3088 * has been redirected to the target location.
3090 else /* (xtype == XTPAGE) */
3091 tlck
= txMaplock(tid
, ip
, tlckMAP
| tlckRELOCATE
);
3093 pxdlock
= (struct pxd_lock
*) & tlck
->lock
;
3094 pxdlock
->flag
= mlckFREEPXD
;
3095 PXDaddress(&pxdlock
->pxd
, oxaddr
);
3096 PXDlength(&pxdlock
->pxd
, xlen
);
3100 * 4. update the parent xad entry for relocation;
3102 * acquire tlck for the parent entry with XAD_NEW as entry
3103 * update which will write LOG_REDOPAGE and update bmap for
3104 * allocation of XAD_NEW destination extent;
3106 jfs_info("xtRelocate: update parent xad entry.");
3107 BT_MARK_DIRTY(pmp
, ip
);
3108 tlck
= txLock(tid
, ip
, pmp
, tlckXTREE
| tlckGROW
);
3109 xtlck
= (struct xtlock
*) & tlck
->lock
;
3111 /* update the XAD with the new destination extent; */
3112 xad
= &pp
->xad
[index
];
3113 xad
->flag
|= XAD_NEW
;
3114 XADaddress(xad
, nxaddr
);
3116 xtlck
->lwm
.offset
= min(index
, xtlck
->lwm
.offset
);
3117 xtlck
->lwm
.length
= le16_to_cpu(pp
->header
.nextindex
) -
3120 /* unpin the parent xtpage */
3130 * function: search for the internal xad entry covering specified extent.
3131 * This function is mainly used by defragfs utility.
3135 * xad - extent to find;
3136 * cmpp - comparison result:
3137 * btstack - traverse stack;
3138 * flag - search process flag;
3141 * btstack contains (bn, index) of search path traversed to the entry.
3142 * *cmpp is set to result of comparison with the entry returned.
3143 * the page containing the entry is pinned at exit.
3145 static int xtSearchNode(struct inode
*ip
, xad_t
* xad
, /* required XAD entry */
3146 int *cmpp
, struct btstack
* btstack
, int flag
)
3151 int cmp
= 1; /* init for empty page */
3152 s64 bn
; /* block number */
3153 struct metapage
*mp
; /* meta-page buffer */
3154 xtpage_t
*p
; /* page */
3155 int base
, index
, lim
;
3156 struct btframe
*btsp
;
3161 xoff
= offsetXAD(xad
);
3162 xlen
= lengthXAD(xad
);
3163 xaddr
= addressXAD(xad
);
3166 * search down tree from root:
3168 * between two consecutive entries of <Ki, Pi> and <Kj, Pj> of
3169 * internal page, child page Pi contains entry with k, Ki <= K < Kj.
3171 * if entry with search key K is not found
3172 * internal page search find the entry with largest key Ki
3173 * less than K which point to the child page to search;
3174 * leaf page search find the entry with smallest key Kj
3175 * greater than K so that the returned index is the position of
3176 * the entry to be shifted right for insertion of new entry.
3177 * for empty tree, search key is greater than any key of the tree.
3179 * by convention, root bn = 0.
3182 /* get/pin the page to search */
3183 XT_GETPAGE(ip
, bn
, mp
, PSIZE
, p
, rc
);
3186 if (p
->header
.flag
& BT_LEAF
) {
3191 lim
= le16_to_cpu(p
->header
.nextindex
) - XTENTRYSTART
;
3194 * binary search with search key K on the current page
3196 for (base
= XTENTRYSTART
; lim
; lim
>>= 1) {
3197 index
= base
+ (lim
>> 1);
3199 XT_CMP(cmp
, xoff
, &p
->xad
[index
], t64
);
3204 * verify for exact match;
3206 if (xaddr
== addressXAD(&p
->xad
[index
]) &&
3207 xoff
== offsetXAD(&p
->xad
[index
])) {
3210 /* save search result */
3211 btsp
= btstack
->top
;
3213 btsp
->index
= index
;
3219 /* descend/search its child page */
3230 * search miss - non-leaf page:
3232 * base is the smallest index with key (Kj) greater than
3233 * search key (K) and may be zero or maxentry index.
3234 * if base is non-zero, decrement base by one to get the parent
3235 * entry of the child page to search.
3237 index
= base
? base
- 1 : base
;
3240 * go down to child page
3243 /* get the child page block number */
3244 bn
= addressXAD(&p
->xad
[index
]);
3246 /* unpin the parent page */
3256 * link around a freed page.
3265 static int xtRelink(tid_t tid
, struct inode
*ip
, xtpage_t
* p
)
3268 struct metapage
*mp
;
3272 nextbn
= le64_to_cpu(p
->header
.next
);
3273 prevbn
= le64_to_cpu(p
->header
.prev
);
3275 /* update prev pointer of the next page */
3277 XT_GETPAGE(ip
, nextbn
, mp
, PSIZE
, p
, rc
);
3282 * acquire a transaction lock on the page;
3284 * action: update prev pointer;
3286 BT_MARK_DIRTY(mp
, ip
);
3287 tlck
= txLock(tid
, ip
, mp
, tlckXTREE
| tlckRELINK
);
3289 /* the page may already have been tlock'd */
3291 p
->header
.prev
= cpu_to_le64(prevbn
);
3296 /* update next pointer of the previous page */
3298 XT_GETPAGE(ip
, prevbn
, mp
, PSIZE
, p
, rc
);
3303 * acquire a transaction lock on the page;
3305 * action: update next pointer;
3307 BT_MARK_DIRTY(mp
, ip
);
3308 tlck
= txLock(tid
, ip
, mp
, tlckXTREE
| tlckRELINK
);
3310 /* the page may already have been tlock'd */
3312 p
->header
.next
= le64_to_cpu(nextbn
);
3319 #endif /* _STILL_TO_PORT */
3325 * initialize file root (inline in inode)
3327 void xtInitRoot(tid_t tid
, struct inode
*ip
)
3332 * acquire a transaction lock on the root
3336 txLock(tid
, ip
, (struct metapage
*) &JFS_IP(ip
)->bxflag
,
3337 tlckXTREE
| tlckNEW
);
3338 p
= &JFS_IP(ip
)->i_xtroot
;
3340 p
->header
.flag
= DXD_INDEX
| BT_ROOT
| BT_LEAF
;
3341 p
->header
.nextindex
= cpu_to_le16(XTENTRYSTART
);
3343 if (S_ISDIR(ip
->i_mode
))
3344 p
->header
.maxentry
= cpu_to_le16(XTROOTINITSLOT_DIR
);
3346 p
->header
.maxentry
= cpu_to_le16(XTROOTINITSLOT
);
3356 * We can run into a deadlock truncating a file with a large number of
3357 * xtree pages (large fragmented file). A robust fix would entail a
3358 * reservation system where we would reserve a number of metadata pages
3359 * and tlocks which we would be guaranteed without a deadlock. Without
3360 * this, a partial fix is to limit number of metadata pages we will lock
3361 * in a single transaction. Currently we will truncate the file so that
3362 * no more than 50 leaf pages will be locked. The caller of xtTruncate
3363 * will be responsible for ensuring that the current transaction gets
3364 * committed, and that subsequent transactions are created to truncate
3365 * the file further if needed.
3367 #define MAX_TRUNCATE_LEAVES 50
3373 * traverse for truncation logging backward bottom up;
3374 * terminate at the last extent entry at the current subtree
3375 * root page covering new down size.
3376 * truncation may occur within the last extent entry.
3382 * int type) {PWMAP, PMAP, WMAP; DELETE, TRUNCATE}
3388 * 1. truncate (non-COMMIT_NOLINK file)
3389 * by jfs_truncate() or jfs_open(O_TRUNC):
3391 * 2. truncate index table of directory when last entry removed
3392 * map update via tlock at commit time;
3394 * Call xtTruncate_pmap instead
3396 * 1. remove (free zero link count) on last reference release
3397 * (pmap has been freed at commit zero link count);
3398 * 2. truncate (COMMIT_NOLINK file, i.e., tmp file):
3400 * map update directly at truncation time;
3403 * no LOG_NOREDOPAGE is required (NOREDOFILE is sufficient);
3404 * else if (TRUNCATE)
3405 * must write LOG_NOREDOPAGE for deleted index page;
3407 * pages may already have been tlocked by anonymous transactions
3408 * during file growth (i.e., write) before truncation;
3410 * except last truncated entry, deleted entries remains as is
3411 * in the page (nextindex is updated) for other use
3412 * (e.g., log/update allocation map): this avoid copying the page
3413 * info but delay free of pages;
3416 s64
xtTruncate(tid_t tid
, struct inode
*ip
, s64 newsize
, int flag
)
3420 struct metapage
*mp
;
3423 int index
, nextindex
;
3426 int xlen
, len
, freexlen
;
3427 struct btstack btstack
;
3428 struct btframe
*parent
;
3429 struct tblock
*tblk
= NULL
;
3430 struct tlock
*tlck
= NULL
;
3431 struct xtlock
*xtlck
= NULL
;
3432 struct xdlistlock xadlock
; /* maplock for COMMIT_WMAP */
3433 struct pxd_lock
*pxdlock
; /* maplock for COMMIT_WMAP */
3436 int locked_leaves
= 0;
3438 /* save object truncation type */
3440 tblk
= tid_to_tblock(tid
);
3441 tblk
->xflag
|= flag
;
3447 assert(flag
!= COMMIT_PMAP
);
3449 if (flag
== COMMIT_PWMAP
)
3453 xadlock
.flag
= mlckFREEXADLIST
;
3458 * if the newsize is not an integral number of pages,
3459 * the file between newsize and next page boundary will
3461 * if truncating into a file hole, it will cause
3462 * a full block to be allocated for the logical block.
3466 * release page blocks of truncated region <teof, eof>
3468 * free the data blocks from the leaf index blocks.
3469 * delete the parent index entries corresponding to
3470 * the freed child data/index blocks.
3471 * free the index blocks themselves which aren't needed
3472 * in new sized file.
3474 * index blocks are updated only if the blocks are to be
3475 * retained in the new sized file.
3476 * if type is PMAP, the data and index pages are NOT
3477 * freed, and the data and index blocks are NOT freed
3479 * (this will allow continued access of data/index of
3480 * temporary file (zerolink count file truncated to zero-length)).
3482 teof
= (newsize
+ (JFS_SBI(ip
->i_sb
)->bsize
- 1)) >>
3483 JFS_SBI(ip
->i_sb
)->l2bsize
;
3491 * root resides in the inode
3496 * first access of each page:
3499 XT_GETPAGE(ip
, bn
, mp
, PSIZE
, p
, rc
);
3503 /* process entries backward from last index */
3504 index
= le16_to_cpu(p
->header
.nextindex
) - 1;
3506 if (p
->header
.flag
& BT_INTERNAL
)
3513 /* Since this is the rightmost leaf, and we may have already freed
3514 * a page that was formerly to the right, let's make sure that the
3515 * next pointer is zero.
3517 if (p
->header
.next
) {
3520 * Make sure this change to the header is logged.
3521 * If we really truncate this leaf, the flag
3522 * will be changed to tlckTRUNCATE
3524 tlck
= txLock(tid
, ip
, mp
, tlckXTREE
|tlckGROW
);
3525 BT_MARK_DIRTY(mp
, ip
);
3531 /* does region covered by leaf page precede Teof ? */
3532 xad
= &p
->xad
[index
];
3533 xoff
= offsetXAD(xad
);
3534 xlen
= lengthXAD(xad
);
3535 if (teof
>= xoff
+ xlen
) {
3540 /* (re)acquire tlock of the leaf page */
3542 if (++locked_leaves
> MAX_TRUNCATE_LEAVES
) {
3544 * We need to limit the size of the transaction
3545 * to avoid exhausting pagecache & tlocks
3548 newsize
= (xoff
+ xlen
) << JFS_SBI(ip
->i_sb
)->l2bsize
;
3551 tlck
= txLock(tid
, ip
, mp
, tlckXTREE
);
3552 tlck
->type
= tlckXTREE
| tlckTRUNCATE
;
3553 xtlck
= (struct xtlock
*) & tlck
->lock
;
3554 xtlck
->hwm
.offset
= le16_to_cpu(p
->header
.nextindex
) - 1;
3556 BT_MARK_DIRTY(mp
, ip
);
3559 * scan backward leaf page entries
3561 for (; index
>= XTENTRYSTART
; index
--) {
3562 xad
= &p
->xad
[index
];
3563 xoff
= offsetXAD(xad
);
3564 xlen
= lengthXAD(xad
);
3565 xaddr
= addressXAD(xad
);
3568 * The "data" for a directory is indexed by the block
3569 * device's address space. This metadata must be invalidated
3572 if (S_ISDIR(ip
->i_mode
) && (teof
== 0))
3573 invalidate_xad_metapages(ip
, *xad
);
3575 * entry beyond eof: continue scan of current page
3577 * ---|---=======------->
3586 * (xoff <= teof): last entry to be deleted from page;
3587 * If other entries remain in page: keep and update the page.
3591 * eof == entry_start: delete the entry
3593 * -------|=======------->
3600 if (index
== XTENTRYSTART
)
3606 * eof within the entry: truncate the entry.
3608 * -------===|===------->
3611 else if (teof
< xoff
+ xlen
) {
3612 /* update truncated entry */
3614 freexlen
= xlen
- len
;
3615 XADlength(xad
, len
);
3617 /* save pxd of truncated extent in tlck */
3619 if (log
) { /* COMMIT_PWMAP */
3620 xtlck
->lwm
.offset
= (xtlck
->lwm
.offset
) ?
3621 min(index
, (int)xtlck
->lwm
.offset
) : index
;
3622 xtlck
->lwm
.length
= index
+ 1 -
3624 xtlck
->twm
.offset
= index
;
3625 pxdlock
= (struct pxd_lock
*) & xtlck
->pxdlock
;
3626 pxdlock
->flag
= mlckFREEPXD
;
3627 PXDaddress(&pxdlock
->pxd
, xaddr
);
3628 PXDlength(&pxdlock
->pxd
, freexlen
);
3630 /* free truncated extent */
3631 else { /* COMMIT_WMAP */
3633 pxdlock
= (struct pxd_lock
*) & xadlock
;
3634 pxdlock
->flag
= mlckFREEPXD
;
3635 PXDaddress(&pxdlock
->pxd
, xaddr
);
3636 PXDlength(&pxdlock
->pxd
, freexlen
);
3637 txFreeMap(ip
, pxdlock
, NULL
, COMMIT_WMAP
);
3639 /* reset map lock */
3640 xadlock
.flag
= mlckFREEXADLIST
;
3643 /* current entry is new last entry; */
3644 nextindex
= index
+ 1;
3649 * eof beyond the entry:
3651 * -------=======---|--->
3654 else { /* (xoff + xlen < teof) */
3656 nextindex
= index
+ 1;
3659 if (nextindex
< le16_to_cpu(p
->header
.nextindex
)) {
3660 if (!log
) { /* COMMIT_WAMP */
3661 xadlock
.xdlist
= &p
->xad
[nextindex
];
3663 le16_to_cpu(p
->header
.nextindex
) -
3665 txFreeMap(ip
, (struct maplock
*) & xadlock
,
3668 p
->header
.nextindex
= cpu_to_le16(nextindex
);
3673 /* assert(freed == 0); */
3675 } /* end scan of leaf page entries */
3680 * leaf page become empty: free the page if type != PMAP
3682 if (log
) { /* COMMIT_PWMAP */
3683 /* txCommit() with tlckFREE:
3684 * free data extents covered by leaf [XTENTRYSTART:hwm);
3685 * invalidate leaf if COMMIT_PWMAP;
3686 * if (TRUNCATE), will write LOG_NOREDOPAGE;
3688 tlck
->type
= tlckXTREE
| tlckFREE
;
3689 } else { /* COMMIT_WAMP */
3691 /* free data extents covered by leaf */
3692 xadlock
.xdlist
= &p
->xad
[XTENTRYSTART
];
3694 le16_to_cpu(p
->header
.nextindex
) - XTENTRYSTART
;
3695 txFreeMap(ip
, (struct maplock
*) & xadlock
, NULL
, COMMIT_WMAP
);
3698 if (p
->header
.flag
& BT_ROOT
) {
3699 p
->header
.flag
&= ~BT_INTERNAL
;
3700 p
->header
.flag
|= BT_LEAF
;
3701 p
->header
.nextindex
= cpu_to_le16(XTENTRYSTART
);
3703 XT_PUTPAGE(mp
); /* debug */
3706 if (log
) { /* COMMIT_PWMAP */
3707 /* page will be invalidated at tx completion
3710 } else { /* COMMIT_WMAP */
3713 lid_to_tlock(mp
->lid
)->flag
|= tlckFREELOCK
;
3715 /* invalidate empty leaf page */
3716 discard_metapage(mp
);
3721 * the leaf page become empty: delete the parent entry
3722 * for the leaf page if the parent page is to be kept
3723 * in the new sized file.
3727 * go back up to the parent page
3730 /* pop/restore parent entry for the current child page */
3731 if ((parent
= BT_POP(&btstack
)) == NULL
)
3732 /* current page must have been root */
3735 /* get back the parent page */
3737 XT_GETPAGE(ip
, bn
, mp
, PSIZE
, p
, rc
);
3741 index
= parent
->index
;
3744 * child page was not empty:
3747 /* has any entry deleted from parent ? */
3748 if (index
< le16_to_cpu(p
->header
.nextindex
) - 1) {
3749 /* (re)acquire tlock on the parent page */
3750 if (log
) { /* COMMIT_PWMAP */
3751 /* txCommit() with tlckTRUNCATE:
3752 * free child extents covered by parent [);
3754 tlck
= txLock(tid
, ip
, mp
, tlckXTREE
);
3755 xtlck
= (struct xtlock
*) & tlck
->lock
;
3756 if (!(tlck
->type
& tlckTRUNCATE
)) {
3758 le16_to_cpu(p
->header
.
3761 tlckXTREE
| tlckTRUNCATE
;
3763 } else { /* COMMIT_WMAP */
3765 /* free child extents covered by parent */
3766 xadlock
.xdlist
= &p
->xad
[index
+ 1];
3768 le16_to_cpu(p
->header
.nextindex
) -
3770 txFreeMap(ip
, (struct maplock
*) & xadlock
,
3773 BT_MARK_DIRTY(mp
, ip
);
3775 p
->header
.nextindex
= cpu_to_le16(index
+ 1);
3782 * child page was empty:
3784 nfreed
+= lengthXAD(&p
->xad
[index
]);
3787 * During working map update, child page's tlock must be handled
3788 * before parent's. This is because the parent's tlock will cause
3789 * the child's disk space to be marked available in the wmap, so
3790 * it's important that the child page be released by that time.
3792 * ToDo: tlocks should be on doubly-linked list, so we can
3793 * quickly remove it and add it to the end.
3797 * Move parent page's tlock to the end of the tid's tlock list
3799 if (log
&& mp
->lid
&& (tblk
->last
!= mp
->lid
) &&
3800 lid_to_tlock(mp
->lid
)->tid
) {
3801 lid_t lid
= mp
->lid
;
3804 tlck
= lid_to_tlock(lid
);
3806 if (tblk
->next
== lid
)
3807 tblk
->next
= tlck
->next
;
3809 for (prev
= lid_to_tlock(tblk
->next
);
3811 prev
= lid_to_tlock(prev
->next
)) {
3814 prev
->next
= tlck
->next
;
3816 lid_to_tlock(tblk
->last
)->next
= lid
;
3822 * parent page become empty: free the page
3824 if (index
== XTENTRYSTART
) {
3825 if (log
) { /* COMMIT_PWMAP */
3826 /* txCommit() with tlckFREE:
3827 * free child extents covered by parent;
3828 * invalidate parent if COMMIT_PWMAP;
3830 tlck
= txLock(tid
, ip
, mp
, tlckXTREE
);
3831 xtlck
= (struct xtlock
*) & tlck
->lock
;
3833 le16_to_cpu(p
->header
.nextindex
) - 1;
3834 tlck
->type
= tlckXTREE
| tlckFREE
;
3835 } else { /* COMMIT_WMAP */
3837 /* free child extents covered by parent */
3838 xadlock
.xdlist
= &p
->xad
[XTENTRYSTART
];
3840 le16_to_cpu(p
->header
.nextindex
) -
3842 txFreeMap(ip
, (struct maplock
*) & xadlock
, NULL
,
3845 BT_MARK_DIRTY(mp
, ip
);
3847 if (p
->header
.flag
& BT_ROOT
) {
3848 p
->header
.flag
&= ~BT_INTERNAL
;
3849 p
->header
.flag
|= BT_LEAF
;
3850 p
->header
.nextindex
= cpu_to_le16(XTENTRYSTART
);
3851 if (le16_to_cpu(p
->header
.maxentry
) == XTROOTMAXSLOT
) {
3853 * Shrink root down to allow inline
3854 * EA (otherwise fsck complains)
3856 p
->header
.maxentry
=
3857 cpu_to_le16(XTROOTINITSLOT
);
3858 JFS_IP(ip
)->mode2
|= INLINEEA
;
3861 XT_PUTPAGE(mp
); /* debug */
3864 if (log
) { /* COMMIT_PWMAP */
3865 /* page will be invalidated at tx completion
3868 } else { /* COMMIT_WMAP */
3871 lid_to_tlock(mp
->lid
)->flag
|=
3874 /* invalidate parent page */
3875 discard_metapage(mp
);
3878 /* parent has become empty and freed:
3879 * go back up to its parent page
3886 * parent page still has entries for front region;
3889 /* try truncate region covered by preceding entry
3890 * (process backward)
3894 /* go back down to the child page corresponding
3901 * internal page: go down to child page of current entry
3904 /* save current parent entry for the child page */
3905 BT_PUSH(&btstack
, bn
, index
);
3907 /* get child page */
3908 xad
= &p
->xad
[index
];
3909 bn
= addressXAD(xad
);
3912 * first access of each internal entry:
3914 /* release parent page */
3917 /* process the child page */
3922 * update file resource stat
3926 if (S_ISDIR(ip
->i_mode
) && !newsize
)
3927 ip
->i_size
= 1; /* fsck hates zero-length directories */
3929 ip
->i_size
= newsize
;
3931 /* update quota allocation to reflect freed blocks */
3932 DQUOT_FREE_BLOCK(ip
, nfreed
);
3935 * free tlock of invalidated pages
3937 if (flag
== COMMIT_WMAP
)
3948 * Perform truncate to zero lenghth for deleted file, leaving the
3949 * the xtree and working map untouched. This allows the file to
3950 * be accessed via open file handles, while the delete of the file
3951 * is committed to disk.
3956 * s64 committed_size)
3958 * return: new committed size
3962 * To avoid deadlock by holding too many transaction locks, the
3963 * truncation may be broken up into multiple transactions.
3964 * The committed_size keeps track of part of the file has been
3965 * freed from the pmaps.
3967 s64
xtTruncate_pmap(tid_t tid
, struct inode
*ip
, s64 committed_size
)
3970 struct btstack btstack
;
3973 int locked_leaves
= 0;
3974 struct metapage
*mp
;
3976 struct btframe
*parent
;
3978 struct tblock
*tblk
;
3979 struct tlock
*tlck
= NULL
;
3983 struct xtlock
*xtlck
= NULL
;
3985 /* save object truncation type */
3986 tblk
= tid_to_tblock(tid
);
3987 tblk
->xflag
|= COMMIT_PMAP
;
3992 if (committed_size
) {
3993 xoff
= (committed_size
>> JFS_SBI(ip
->i_sb
)->l2bsize
) - 1;
3994 rc
= xtSearch(ip
, xoff
, &cmp
, &btstack
, 0);
3998 XT_GETSEARCH(ip
, btstack
.top
, bn
, mp
, p
, index
);
4003 "xtTruncate_pmap: did not find extent");
4010 * root resides in the inode
4015 * first access of each page:
4018 XT_GETPAGE(ip
, bn
, mp
, PSIZE
, p
, rc
);
4022 /* process entries backward from last index */
4023 index
= le16_to_cpu(p
->header
.nextindex
) - 1;
4025 if (p
->header
.flag
& BT_INTERNAL
)
4033 if (++locked_leaves
> MAX_TRUNCATE_LEAVES
) {
4035 * We need to limit the size of the transaction
4036 * to avoid exhausting pagecache & tlocks
4038 xad
= &p
->xad
[index
];
4039 xoff
= offsetXAD(xad
);
4040 xlen
= lengthXAD(xad
);
4042 return (xoff
+ xlen
) << JFS_SBI(ip
->i_sb
)->l2bsize
;
4044 tlck
= txLock(tid
, ip
, mp
, tlckXTREE
);
4045 tlck
->type
= tlckXTREE
| tlckFREE
;
4046 xtlck
= (struct xtlock
*) & tlck
->lock
;
4047 xtlck
->hwm
.offset
= index
;
4053 * go back up to the parent page
4056 /* pop/restore parent entry for the current child page */
4057 if ((parent
= BT_POP(&btstack
)) == NULL
)
4058 /* current page must have been root */
4061 /* get back the parent page */
4063 XT_GETPAGE(ip
, bn
, mp
, PSIZE
, p
, rc
);
4067 index
= parent
->index
;
4070 * parent page become empty: free the page
4072 if (index
== XTENTRYSTART
) {
4073 /* txCommit() with tlckFREE:
4074 * free child extents covered by parent;
4075 * invalidate parent if COMMIT_PWMAP;
4077 tlck
= txLock(tid
, ip
, mp
, tlckXTREE
);
4078 xtlck
= (struct xtlock
*) & tlck
->lock
;
4080 le16_to_cpu(p
->header
.nextindex
) - 1;
4081 tlck
->type
= tlckXTREE
| tlckFREE
;
4085 if (p
->header
.flag
& BT_ROOT
) {
4093 * parent page still has entries for front region;
4098 * internal page: go down to child page of current entry
4101 /* save current parent entry for the child page */
4102 BT_PUSH(&btstack
, bn
, index
);
4104 /* get child page */
4105 xad
= &p
->xad
[index
];
4106 bn
= addressXAD(xad
);
4109 * first access of each internal entry:
4111 /* release parent page */
4114 /* process the child page */
4123 #ifdef _JFS_DEBUG_XTREE
4127 * function: traverse forward
4129 int xtDisplayTree(struct inode
*ip
)
4132 struct metapage
*mp
;
4135 int index
, lastindex
, v
, h
;
4137 struct btstack btstack
;
4138 struct btframe
*btsp
;
4139 struct btframe
*parent
;
4141 printk("display B+-tree.\n");
4144 btsp
= btstack
.stack
;
4149 * root resides in the inode
4155 * first access of each page:
4158 XT_GETPAGE(ip
, bn
, mp
, PSIZE
, p
, rc
);
4162 /* process entries forward from first index */
4163 index
= XTENTRYSTART
;
4164 lastindex
= le16_to_cpu(p
->header
.nextindex
) - 1;
4166 if (p
->header
.flag
& BT_INTERNAL
) {
4168 * first access of each internal page
4171 } else { /* (p->header.flag & BT_LEAF) */
4174 * first access of each leaf page
4176 printf("leaf page ");
4177 xtDisplayPage(ip
, bn
, p
);
4179 /* unpin the leaf page */
4184 * go back up to the parent page
4187 /* pop/restore parent entry for the current child page */
4188 if ((parent
= (btsp
== btstack
.stack
? NULL
: --btsp
)) == NULL
)
4189 /* current page must have been root */
4193 * parent page scan completed
4195 if ((index
= parent
->index
) == (lastindex
= parent
->lastindex
)) {
4196 /* go back up to the parent page */
4201 * parent page has entries remaining
4203 /* get back the parent page */
4205 /* v = parent->level; */
4206 XT_GETPAGE(ip
, bn
, mp
, PSIZE
, p
, rc
);
4210 /* get next parent entry */
4214 * internal page: go down to child page of current entry
4217 /* push/save current parent entry for the child page */
4218 btsp
->bn
= pbn
= bn
;
4219 btsp
->index
= index
;
4220 btsp
->lastindex
= lastindex
;
4221 /* btsp->level = v; */
4222 /* btsp->node = h; */
4225 /* get child page */
4226 xad
= &p
->xad
[index
];
4227 bn
= addressXAD(xad
);
4230 * first access of each internal entry:
4232 /* release parent page */
4235 printk("traverse down 0x%lx[%d]->0x%lx\n", (ulong
) pbn
, index
,
4240 /* process the child page */
4248 * function: display page
4250 int xtDisplayPage(struct inode
*ip
, s64 bn
, xtpage_t
* p
)
4257 /* display page control */
4258 printf("bn:0x%lx flag:0x%x nextindex:%d\n",
4259 (ulong
) bn
, p
->header
.flag
,
4260 le16_to_cpu(p
->header
.nextindex
));
4262 /* display entries */
4263 xad
= &p
->xad
[XTENTRYSTART
];
4264 for (i
= XTENTRYSTART
, j
= 1; i
< le16_to_cpu(p
->header
.nextindex
);
4266 xoff
= offsetXAD(xad
);
4267 xaddr
= addressXAD(xad
);
4268 xlen
= lengthXAD(xad
);
4269 printf("\t[%d] 0x%lx:0x%lx(0x%x)", i
, (ulong
) xoff
,
4270 (ulong
) xaddr
, xlen
);
4280 #endif /* _JFS_DEBUG_XTREE */
4288 * traverse for allocation acquiring tlock at commit time
4289 * (vs at the time of update) logging backward top down
4292 * problem - establishing that all new allocation have been
4293 * processed both for append and random write in sparse file
4294 * at the current entry at the current subtree root page
4297 int xtGather(btree_t
*t
)
4304 struct btstack btstack
;
4305 struct btsf
*parent
;
4313 * root resides in the inode
4316 XT_GETPAGE(ip
, bn
, mp
, PSIZE
, p
, rc
);
4320 /* new root is NOT pointed by a new entry
4321 if (p->header.flag & NEW)
4322 allocate new page lock;
4323 write a NEWPAGE log;
4328 * first access of each page:
4330 /* process entries backward from last index */
4331 index
= le16_to_cpu(p
->header
.nextindex
) - 1;
4333 if (p
->header
.flag
& BT_LEAF
) {
4335 * first access of each leaf page
4337 /* process leaf page entries backward */
4338 for (; index
>= XTENTRYSTART
; index
--) {
4341 * if newpage, log NEWPAGE.
4343 if (e->flag & XAD_NEW) {
4344 nfound =+ entry->length;
4345 update current page lock for the entry;
4348 * if moved, log move.
4350 } else if (e->flag & XAD_MOVED) {
4352 update current page lock for the entry;
4357 /* unpin the leaf page */
4361 * go back up to the parent page
4364 /* restore parent entry for the current child page */
4365 if ((parent
= BT_POP(&btstack
)) == NULL
)
4366 /* current page must have been root */
4369 if ((index
= parent
->index
) == XTENTRYSTART
) {
4371 * parent page scan completed
4373 /* go back up to the parent page */
4377 * parent page has entries remaining
4379 /* get back the parent page */
4381 XT_GETPAGE(ip
, bn
, mp
, PSIZE
, p
, rc
);
4385 /* first subroot page which
4386 * covers all new allocated blocks
4387 * itself not new/modified.
4388 * (if modified from split of descendent,
4389 * go down path of split page)
4391 if (nfound == nnew &&
4392 !(p->header.flag & (NEW | MOD)))
4396 /* process parent page entries backward */
4401 * first access of each internal page
4406 * internal page: go down to child page of current entry
4409 /* save current parent entry for the child page */
4410 BT_PUSH(&btstack
, bn
, index
);
4412 /* get current entry for the child page */
4416 * first access of each internal entry:
4419 * if new entry, log btree_tnewentry.
4421 if (e->flag & XAD_NEW)
4422 update parent page lock for the entry;
4425 /* release parent page */
4428 /* get child page */
4430 XT_GETPAGE(ip
, bn
, mp
, PSIZE
, p
, rc
);
4435 * first access of each non-root page:
4438 * if new, log btree_newpage.
4440 if (p->header.flag & NEW)
4441 allocate new page lock;
4442 write a NEWPAGE log (next, prev);
4445 /* process the child page */
4451 #endif /* _JFS_WIP */
4454 #ifdef CONFIG_JFS_STATISTICS
4455 int jfs_xtstat_read(char *buffer
, char **start
, off_t offset
, int length
,
4456 int *eof
, void *data
)
4461 len
+= sprintf(buffer
,
4462 "JFS Xtree statistics\n"
4463 "====================\n"
4465 "fast searches = %d\n"
4472 *start
= buffer
+ begin
;