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 (sp->header.maxentry < cpu_to_le16(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;
1627 jfs_info("xtExtend: nxoff:0x%lx nxlen:0x%x", (ulong) xoff, xlen);
1629 /* there must exist extent to be extended */
1630 if ((rc = xtSearch(ip, xoff - 1, &cmp, &btstack, XT_INSERT)))
1633 /* retrieve search result */
1634 XT_GETSEARCH(ip, btstack.top, bn, mp, p, index);
1638 jfs_error(ip->i_sb, "xtExtend: xtSearch did not find extent");
1642 /* extension must be contiguous */
1643 xad = &p->xad[index];
1644 if ((offsetXAD(xad) + lengthXAD(xad)) != xoff) {
1646 jfs_error(ip->i_sb, "xtExtend: extension is not contiguous");
1651 * acquire a transaction lock on the leaf page;
1653 * action: xad insertion/extension;
1655 BT_MARK_DIRTY(mp, ip);
1656 if (!test_cflag(COMMIT_Nolink, ip)) {
1657 tlck = txLock(tid, ip, mp, tlckXTREE | tlckGROW);
1658 xtlck = (struct xtlock *) & tlck->lock;
1661 /* extend will overflow extent ? */
1662 xlen = lengthXAD(xad) + xlen;
1663 if ((len = xlen - MAXXLEN) <= 0)
1667 * extent overflow: insert entry for new extent
1670 xoff = offsetXAD(xad) + MAXXLEN;
1671 xaddr = addressXAD(xad) + MAXXLEN;
1672 nextindex = le16_to_cpu(p->header.nextindex);
1675 * if the leaf page is full, insert the new entry and
1676 * propagate up the router entry for the new page from split
1678 * The xtSplitUp() will insert the entry and unpin the leaf page.
1680 if (nextindex == le16_to_cpu(p->header.maxentry)) {
1681 rootsplit = p->header.flag & BT_ROOT;
1683 /* xtSpliUp() unpins leaf pages */
1685 split.index = index + 1;
1686 split.flag = XAD_NEW;
1687 split.off = xoff; /* split offset */
1690 split.pxdlist = NULL;
1691 if ((rc = xtSplitUp(tid, ip, &split, &btstack)))
1695 * if leaf root has been split, original root has been
1696 * copied to new child page, i.e., original entry now
1697 * resides on the new child page;
1700 ASSERT(p->header.nextindex ==
1701 cpu_to_le16(XTENTRYSTART + 1));
1702 xad = &p->xad[XTENTRYSTART];
1703 bn = addressXAD(xad);
1705 /* get new child page */
1706 XT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
1710 BT_MARK_DIRTY(mp, ip);
1711 if (!test_cflag(COMMIT_Nolink, ip)) {
1712 tlck = txLock(tid, ip, mp, tlckXTREE|tlckGROW);
1713 xtlck = (struct xtlock *) & tlck->lock;
1716 /* get back old page */
1717 XT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
1723 * insert the new entry into the leaf page
1726 /* insert the new entry: mark the entry NEW */
1727 xad = &p->xad[index + 1];
1728 XT_PUTENTRY(xad, XAD_NEW, xoff, len, xaddr);
1730 /* advance next available entry index */
1731 p->header.nextindex =
1732 cpu_to_le16(le16_to_cpu(p->header.nextindex) + 1);
1735 /* get back old entry */
1736 xad = &p->xad[index];
1743 XADlength(xad, xlen);
1744 if (!(xad->flag & XAD_NEW))
1745 xad->flag |= XAD_EXTENDED;
1747 if (!test_cflag(COMMIT_Nolink, ip)) {
1749 (xtlck->lwm.offset) ? min(index,
1750 (int)xtlck->lwm.offset) : index;
1752 le16_to_cpu(p->header.nextindex) - xtlck->lwm.offset;
1755 /* unpin the leaf page */
1765 * function: split existing 'tail' extent
1766 * (split offset >= start offset of tail extent), and
1767 * relocate and extend the split tail half;
1769 * note: existing extent may or may not have been committed.
1770 * caller is responsible for pager buffer cache update, and
1771 * working block allocation map update;
1772 * update pmap: free old split tail extent, alloc new extent;
1774 int xtTailgate(tid_t tid, /* transaction id */
1775 struct inode *ip, s64 xoff, /* split/new extent offset */
1776 s32 xlen, /* new extent length */
1777 s64 xaddr, /* new extent address */
1782 struct metapage *mp; /* meta-page buffer */
1783 xtpage_t *p; /* base B+-tree index page */
1785 int index, nextindex, llen, rlen;
1786 struct btstack btstack; /* traverse stack */
1787 struct xtsplit split; /* split information */
1790 struct xtlock *xtlck = 0;
1791 struct tlock *mtlck;
1792 struct maplock *pxdlock;
1796 printf("xtTailgate: nxoff:0x%lx nxlen:0x%x nxaddr:0x%lx\n",
1797 (ulong)xoff, xlen, (ulong)xaddr);
1800 /* there must exist extent to be tailgated */
1801 if ((rc = xtSearch(ip, xoff, &cmp, &btstack, XT_INSERT)))
1804 /* retrieve search result */
1805 XT_GETSEARCH(ip, btstack.top, bn, mp, p, index);
1809 jfs_error(ip->i_sb, "xtTailgate: couldn't find extent");
1813 /* entry found must be last entry */
1814 nextindex = le16_to_cpu(p->header.nextindex);
1815 if (index != nextindex - 1) {
1818 "xtTailgate: the entry found is not the last entry");
1822 BT_MARK_DIRTY(mp, ip);
1824 * acquire tlock of the leaf page containing original entry
1826 if (!test_cflag(COMMIT_Nolink, ip)) {
1827 tlck = txLock(tid, ip, mp, tlckXTREE | tlckGROW);
1828 xtlck = (struct xtlock *) & tlck->lock;
1831 /* completely replace extent ? */
1832 xad = &p->xad[index];
1834 printf("xtTailgate: xoff:0x%lx xlen:0x%x xaddr:0x%lx\n",
1835 (ulong)offsetXAD(xad), lengthXAD(xad), (ulong)addressXAD(xad));
1837 if ((llen = xoff - offsetXAD(xad)) == 0)
1841 * partially replace extent: insert entry for new extent
1845 * if the leaf page is full, insert the new entry and
1846 * propagate up the router entry for the new page from split
1848 * The xtSplitUp() will insert the entry and unpin the leaf page.
1850 if (nextindex == le16_to_cpu(p->header.maxentry)) {
1851 rootsplit = p->header.flag & BT_ROOT;
1853 /* xtSpliUp() unpins leaf pages */
1855 split.index = index + 1;
1856 split.flag = XAD_NEW;
1857 split.off = xoff; /* split offset */
1860 split.pxdlist = NULL;
1861 if ((rc = xtSplitUp(tid, ip, &split, &btstack)))
1865 * if leaf root has been split, original root has been
1866 * copied to new child page, i.e., original entry now
1867 * resides on the new child page;
1870 ASSERT(p->header.nextindex ==
1871 cpu_to_le16(XTENTRYSTART + 1));
1872 xad = &p->xad[XTENTRYSTART];
1873 bn = addressXAD(xad);
1875 /* get new child page */
1876 XT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
1880 BT_MARK_DIRTY(mp, ip);
1881 if (!test_cflag(COMMIT_Nolink, ip)) {
1882 tlck = txLock(tid, ip, mp, tlckXTREE|tlckGROW);
1883 xtlck = (struct xtlock *) & tlck->lock;
1886 /* get back old page */
1887 XT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
1893 * insert the new entry into the leaf page
1896 /* insert the new entry: mark the entry NEW */
1897 xad = &p->xad[index + 1];
1898 XT_PUTENTRY(xad, XAD_NEW, xoff, xlen, xaddr);
1900 /* advance next available entry index */
1901 p->header.nextindex =
1902 cpu_to_le16(le16_to_cpu(p->header.nextindex) + 1);
1905 /* get back old XAD */
1906 xad = &p->xad[index];
1909 * truncate/relocate old extent at split offset
1912 /* update dmap for old/committed/truncated extent */
1913 rlen = lengthXAD(xad) - llen;
1914 if (!(xad->flag & XAD_NEW)) {
1915 /* free from PWMAP at commit */
1916 if (!test_cflag(COMMIT_Nolink, ip)) {
1917 mtlck = txMaplock(tid, ip, tlckMAP);
1918 pxdlock = (struct maplock *) & mtlck->lock;
1919 pxdlock->flag = mlckFREEPXD;
1920 PXDaddress(&pxdlock->pxd, addressXAD(xad) + llen);
1921 PXDlength(&pxdlock->pxd, rlen);
1925 /* free from WMAP */
1926 dbFree(ip, addressXAD(xad) + llen, (s64) rlen);
1930 XADlength(xad, llen);
1933 XT_PUTENTRY(xad, XAD_NEW, xoff, xlen, xaddr);
1935 if (!test_cflag(COMMIT_Nolink, ip)) {
1936 xtlck->lwm.offset = (xtlck->lwm.offset) ?
1937 min(index, (int)xtlck->lwm.offset) : index;
1938 xtlck->lwm.length = le16_to_cpu(p->header.nextindex) -
1942 /* unpin the leaf page */
1947 #endif /* _NOTYET */
1952 * function: update XAD;
1954 * update extent for allocated_but_not_recorded or
1955 * compressed extent;
1959 * logical extent of the specified XAD must be completely
1960 * contained by an existing XAD;
1962 int xtUpdate(tid_t tid, struct inode *ip, xad_t * nxad)
1966 struct metapage *mp; /* meta-page buffer */
1967 xtpage_t *p; /* base B+-tree index page */
1969 int index0, index, newindex, nextindex;
1970 struct btstack btstack; /* traverse stack */
1971 struct xtsplit split; /* split information */
1972 xad_t *xad, *lxad, *rxad;
1975 int nxlen, xlen, lxlen, rxlen;
1978 struct xtlock *xtlck = NULL;
1979 int rootsplit = 0, newpage = 0;
1981 /* there must exist extent to be tailgated */
1982 nxoff = offsetXAD(nxad);
1983 nxlen = lengthXAD(nxad);
1984 nxaddr = addressXAD(nxad);
1986 if ((rc = xtSearch(ip, nxoff, &cmp, &btstack, XT_INSERT)))
1989 /* retrieve search result */
1990 XT_GETSEARCH(ip, btstack.top, bn, mp, p, index0);
1994 jfs_error(ip->i_sb, "xtUpdate: Could not find extent");
1998 BT_MARK_DIRTY(mp, ip);
2000 * acquire tlock of the leaf page containing original entry
2002 if (!test_cflag(COMMIT_Nolink, ip)) {
2003 tlck = txLock(tid, ip, mp, tlckXTREE | tlckGROW);
2004 xtlck = (struct xtlock *) & tlck->lock;
2007 xad = &p->xad[index0];
2009 xoff = offsetXAD(xad);
2010 xlen = lengthXAD(xad);
2011 xaddr = addressXAD(xad);
2013 /* nXAD must be completely contained within XAD */
2014 if ((xoff > nxoff) ||
2015 (nxoff + nxlen > xoff + xlen)) {
2018 "xtUpdate: nXAD in not completely contained within XAD");
2023 newindex = index + 1;
2024 nextindex = le16_to_cpu(p->header.nextindex);
2026 #ifdef _JFS_WIP_NOCOALESCE
2031 * replace XAD with nXAD
2033 replace: /* (nxoff == xoff) */
2034 if (nxlen == xlen) {
2035 /* replace XAD with nXAD:recorded */
2037 xad->flag = xflag & ~XAD_NOTRECORDED;
2040 } else /* (nxlen < xlen) */
2042 #endif /* _JFS_WIP_NOCOALESCE */
2044 /* #ifdef _JFS_WIP_COALESCE */
2049 * coalesce with left XAD
2051 //coalesceLeft: /* (xoff == nxoff) */
2052 /* is XAD first entry of page ? */
2053 if (index == XTENTRYSTART)
2056 /* is nXAD logically and physically contiguous with lXAD ? */
2057 lxad = &p->xad[index - 1];
2058 lxlen = lengthXAD(lxad);
2059 if (!(lxad->flag & XAD_NOTRECORDED) &&
2060 (nxoff == offsetXAD(lxad) + lxlen) &&
2061 (nxaddr == addressXAD(lxad) + lxlen) &&
2062 (lxlen + nxlen < MAXXLEN)) {
2063 /* extend right lXAD */
2065 XADlength(lxad, lxlen + nxlen);
2067 /* If we just merged two extents together, need to make sure the
2068 * right extent gets logged. If the left one is marked XAD_NEW,
2069 * then we know it will be logged. Otherwise, mark as
2072 if (!(lxad->flag & XAD_NEW))
2073 lxad->flag |= XAD_EXTENDED;
2077 XADoffset(xad, xoff + nxlen);
2078 XADlength(xad, xlen - nxlen);
2079 XADaddress(xad, xaddr + nxlen);
2081 } else { /* (xlen == nxlen) */
2084 if (index < nextindex - 1)
2085 memmove(&p->xad[index], &p->xad[index + 1],
2086 (nextindex - index -
2087 1) << L2XTSLOTSIZE);
2089 p->header.nextindex =
2090 cpu_to_le16(le16_to_cpu(p->header.nextindex) -
2094 newindex = index + 1;
2095 nextindex = le16_to_cpu(p->header.nextindex);
2096 xoff = nxoff = offsetXAD(lxad);
2097 xlen = nxlen = lxlen + nxlen;
2098 xaddr = nxaddr = addressXAD(lxad);
2104 * replace XAD with nXAD
2106 replace: /* (nxoff == xoff) */
2107 if (nxlen == xlen) {
2108 /* replace XAD with nXAD:recorded */
2110 xad->flag = xflag & ~XAD_NOTRECORDED;
2113 } else /* (nxlen < xlen) */
2117 * coalesce with right XAD
2119 coalesceRight: /* (xoff <= nxoff) */
2120 /* is XAD last entry of page ? */
2121 if (newindex == nextindex) {
2127 /* is nXAD logically and physically contiguous with rXAD ? */
2128 rxad = &p->xad[index + 1];
2129 rxlen = lengthXAD(rxad);
2130 if (!(rxad->flag & XAD_NOTRECORDED) &&
2131 (nxoff + nxlen == offsetXAD(rxad)) &&
2132 (nxaddr + nxlen == addressXAD(rxad)) &&
2133 (rxlen + nxlen < MAXXLEN)) {
2134 /* extend left rXAD */
2135 XADoffset(rxad, nxoff);
2136 XADlength(rxad, rxlen + nxlen);
2137 XADaddress(rxad, nxaddr);
2139 /* If we just merged two extents together, need to make sure
2140 * the left extent gets logged. If the right one is marked
2141 * XAD_NEW, then we know it will be logged. Otherwise, mark as
2144 if (!(rxad->flag & XAD_NEW))
2145 rxad->flag |= XAD_EXTENDED;
2149 XADlength(xad, xlen - nxlen);
2150 else { /* (xlen == nxlen) */
2153 memmove(&p->xad[index], &p->xad[index + 1],
2154 (nextindex - index - 1) << L2XTSLOTSIZE);
2156 p->header.nextindex =
2157 cpu_to_le16(le16_to_cpu(p->header.nextindex) -
2162 } else if (xoff == nxoff)
2165 if (xoff >= nxoff) {
2167 jfs_error(ip->i_sb, "xtUpdate: xoff >= nxoff");
2170 /* #endif _JFS_WIP_COALESCE */
2173 * split XAD into (lXAD, nXAD):
2176 * --|----------XAD----------|--
2179 updateRight: /* (xoff < nxoff) */
2180 /* truncate old XAD as lXAD:not_recorded */
2181 xad = &p->xad[index];
2182 XADlength(xad, nxoff - xoff);
2184 /* insert nXAD:recorded */
2185 if (nextindex == le16_to_cpu(p->header.maxentry)) {
2186 rootsplit = p->header.flag & BT_ROOT;
2188 /* xtSpliUp() unpins leaf pages */
2190 split.index = newindex;
2191 split.flag = xflag & ~XAD_NOTRECORDED;
2194 split.addr = nxaddr;
2195 split.pxdlist = NULL;
2196 if ((rc = xtSplitUp(tid, ip, &split, &btstack)))
2200 * if leaf root has been split, original root has been
2201 * copied to new child page, i.e., original entry now
2202 * resides on the new child page;
2205 ASSERT(p->header.nextindex ==
2206 cpu_to_le16(XTENTRYSTART + 1));
2207 xad = &p->xad[XTENTRYSTART];
2208 bn = addressXAD(xad);
2210 /* get new child page */
2211 XT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
2215 BT_MARK_DIRTY(mp, ip);
2216 if (!test_cflag(COMMIT_Nolink, ip)) {
2217 tlck = txLock(tid, ip, mp, tlckXTREE|tlckGROW);
2218 xtlck = (struct xtlock *) & tlck->lock;
2221 /* get back old page */
2222 XT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
2226 /* is nXAD on new page ? */
2228 (le16_to_cpu(p->header.maxentry) >> 1)) {
2231 le16_to_cpu(p->header.nextindex) +
2237 /* if insert into middle, shift right remaining entries */
2238 if (newindex < nextindex)
2239 memmove(&p->xad[newindex + 1], &p->xad[newindex],
2240 (nextindex - newindex) << L2XTSLOTSIZE);
2242 /* insert the entry */
2243 xad = &p->xad[newindex];
2245 xad->flag = xflag & ~XAD_NOTRECORDED;
2247 /* advance next available entry index. */
2248 p->header.nextindex =
2249 cpu_to_le16(le16_to_cpu(p->header.nextindex) + 1);
2253 * does nXAD force 3-way split ?
2256 * --|----------XAD-------------|--
2257 * |-lXAD-| |-rXAD -|
2259 if (nxoff + nxlen == xoff + xlen)
2262 /* reorient nXAD as XAD for further split XAD into (nXAD, rXAD) */
2264 /* close out old page */
2265 if (!test_cflag(COMMIT_Nolink, ip)) {
2266 xtlck->lwm.offset = (xtlck->lwm.offset) ?
2267 min(index0, (int)xtlck->lwm.offset) : index0;
2269 le16_to_cpu(p->header.nextindex) -
2273 bn = le64_to_cpu(p->header.next);
2276 /* get new right page */
2277 XT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
2281 BT_MARK_DIRTY(mp, ip);
2282 if (!test_cflag(COMMIT_Nolink, ip)) {
2283 tlck = txLock(tid, ip, mp, tlckXTREE | tlckGROW);
2284 xtlck = (struct xtlock *) & tlck->lock;
2287 index0 = index = newindex;
2291 newindex = index + 1;
2292 nextindex = le16_to_cpu(p->header.nextindex);
2293 xlen = xlen - (nxoff - xoff);
2297 /* recompute split pages */
2298 if (nextindex == le16_to_cpu(p->header.maxentry)) {
2301 if ((rc = xtSearch(ip, nxoff, &cmp, &btstack, XT_INSERT)))
2304 /* retrieve search result */
2305 XT_GETSEARCH(ip, btstack.top, bn, mp, p, index0);
2309 jfs_error(ip->i_sb, "xtUpdate: xtSearch failed");
2313 if (index0 != index) {
2316 "xtUpdate: unexpected value of index");
2322 * split XAD into (nXAD, rXAD)
2325 * --|----------XAD----------|--
2328 updateLeft: /* (nxoff == xoff) && (nxlen < xlen) */
2329 /* update old XAD with nXAD:recorded */
2330 xad = &p->xad[index];
2332 xad->flag = xflag & ~XAD_NOTRECORDED;
2334 /* insert rXAD:not_recorded */
2335 xoff = xoff + nxlen;
2336 xlen = xlen - nxlen;
2337 xaddr = xaddr + nxlen;
2338 if (nextindex == le16_to_cpu(p->header.maxentry)) {
2339 rootsplit = p->header.flag & BT_ROOT;
2342 printf("xtUpdate.updateLeft.split p:0x%p\n", p);
2344 /* xtSpliUp() unpins leaf pages */
2346 split.index = newindex;
2351 split.pxdlist = NULL;
2352 if ((rc = xtSplitUp(tid, ip, &split, &btstack)))
2356 * if leaf root has been split, original root has been
2357 * copied to new child page, i.e., original entry now
2358 * resides on the new child page;
2361 ASSERT(p->header.nextindex ==
2362 cpu_to_le16(XTENTRYSTART + 1));
2363 xad = &p->xad[XTENTRYSTART];
2364 bn = addressXAD(xad);
2366 /* get new child page */
2367 XT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
2371 BT_MARK_DIRTY(mp, ip);
2372 if (!test_cflag(COMMIT_Nolink, ip)) {
2373 tlck = txLock(tid, ip, mp, tlckXTREE|tlckGROW);
2374 xtlck = (struct xtlock *) & tlck->lock;
2377 /* get back old page */
2378 XT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
2383 /* if insert into middle, shift right remaining entries */
2384 if (newindex < nextindex)
2385 memmove(&p->xad[newindex + 1], &p->xad[newindex],
2386 (nextindex - newindex) << L2XTSLOTSIZE);
2388 /* insert the entry */
2389 xad = &p->xad[newindex];
2390 XT_PUTENTRY(xad, xflag, xoff, xlen, xaddr);
2392 /* advance next available entry index. */
2393 p->header.nextindex =
2394 cpu_to_le16(le16_to_cpu(p->header.nextindex) + 1);
2398 if (!test_cflag(COMMIT_Nolink, ip)) {
2399 xtlck->lwm.offset = (xtlck->lwm.offset) ?
2400 min(index0, (int)xtlck->lwm.offset) : index0;
2401 xtlck->lwm.length = le16_to_cpu(p->header.nextindex) -
2405 /* unpin the leaf page */
2415 * function: grow in append mode from contiguous region specified ;
2418 * tid - transaction id;
2420 * xflag - extent flag:
2421 * xoff - extent offset;
2422 * maxblocks - max extent length;
2423 * xlen - extent length (in/out);
2424 * xaddrp - extent address pointer (in/out):
2429 int xtAppend(tid_t tid, /* transaction id */
2430 struct inode *ip, int xflag, s64 xoff, s32 maxblocks,
2431 s32 * xlenp, /* (in/out) */
2432 s64 * xaddrp, /* (in/out) */
2436 struct metapage *mp; /* meta-page buffer */
2437 xtpage_t *p; /* base B+-tree index page */
2439 int index, nextindex;
2440 struct btstack btstack; /* traverse stack */
2441 struct xtsplit split; /* split information */
2445 struct xtlock *xtlck;
2446 int nsplit, nblocks, xlen;
2447 struct pxdlist pxdlist;
2452 jfs_info("xtAppend: xoff:0x%lx maxblocks:%d xlen:%d xaddr:0x%lx",
2453 (ulong) xoff, maxblocks, xlen, (ulong) xaddr);
2456 * search for the entry location at which to insert:
2458 * xtFastSearch() and xtSearch() both returns (leaf page
2459 * pinned, index at which to insert).
2460 * n.b. xtSearch() may return index of maxentry of
2463 if ((rc = xtSearch(ip, xoff, &cmp, &btstack, XT_INSERT)))
2466 /* retrieve search result */
2467 XT_GETSEARCH(ip, btstack.top, bn, mp, p, index);
2475 * insert entry for new extent
2480 * if the leaf page is full, split the page and
2481 * propagate up the router entry for the new page from split
2483 * The xtSplitUp() will insert the entry and unpin the leaf page.
2485 nextindex = le16_to_cpu(p->header.nextindex);
2486 if (nextindex < le16_to_cpu(p->header.maxentry))
2490 * allocate new index blocks to cover index page split(s)
2492 nsplit = btstack.nsplit;
2493 split.pxdlist = &pxdlist;
2494 pxdlist.maxnpxd = pxdlist.npxd = 0;
2495 pxd = &pxdlist.pxd[0];
2496 nblocks = JFS_SBI(ip->i_sb)->nbperpage;
2497 for (; nsplit > 0; nsplit--, pxd++, xaddr += nblocks, maxblocks -= nblocks) {
2498 if ((rc = dbAllocBottomUp(ip, xaddr, (s64) nblocks)) == 0) {
2499 PXDaddress(pxd, xaddr);
2500 PXDlength(pxd, nblocks);
2507 /* undo allocation */
2512 xlen = min(xlen, maxblocks);
2515 * allocate data extent requested
2517 if ((rc = dbAllocBottomUp(ip, xaddr, (s64) xlen)))
2521 split.index = index;
2526 if ((rc = xtSplitUp(tid, ip, &split, &btstack))) {
2527 /* undo data extent allocation */
2528 dbFree(ip, *xaddrp, (s64) * xlenp);
2538 * insert the new entry into the leaf page
2542 * allocate data extent requested
2544 if ((rc = dbAllocBottomUp(ip, xaddr, (s64) xlen)))
2547 BT_MARK_DIRTY(mp, ip);
2549 * acquire a transaction lock on the leaf page;
2551 * action: xad insertion/extension;
2553 tlck = txLock(tid, ip, mp, tlckXTREE | tlckGROW);
2554 xtlck = (struct xtlock *) & tlck->lock;
2556 /* insert the new entry: mark the entry NEW */
2557 xad = &p->xad[index];
2558 XT_PUTENTRY(xad, xflag, xoff, xlen, xaddr);
2560 /* advance next available entry index */
2561 p->header.nextindex =
2562 cpu_to_le16(le16_to_cpu(p->header.nextindex) + 1);
2565 (xtlck->lwm.offset) ? min(index,(int) xtlck->lwm.offset) : index;
2566 xtlck->lwm.length = le16_to_cpu(p->header.nextindex) -
2573 /* unpin the leaf page */
2578 #ifdef _STILL_TO_PORT
2580 /* - TBD for defragmentaion/reorganization -
2585 * delete the entry with the specified key.
2587 * N.B.: whole extent of the entry is assumed to be deleted.
2592 * ENOENT: if the entry is not found.
2596 int xtDelete(tid_t tid, struct inode *ip, s64 xoff, s32 xlen, int flag)
2599 struct btstack btstack;
2602 struct metapage *mp;
2604 int index, nextindex;
2606 struct xtlock *xtlck;
2609 * find the matching entry; xtSearch() pins the page
2611 if ((rc = xtSearch(ip, xoff, &cmp, &btstack, 0)))
2614 XT_GETSEARCH(ip, btstack.top, bn, mp, p, index);
2616 /* unpin the leaf page */
2622 * delete the entry from the leaf page
2624 nextindex = le16_to_cpu(p->header.nextindex);
2625 p->header.nextindex =
2626 cpu_to_le16(le16_to_cpu(p->header.nextindex) - 1);
2629 * if the leaf page bocome empty, free the page
2631 if (p->header.nextindex == cpu_to_le16(XTENTRYSTART))
2632 return (xtDeleteUp(tid, ip, mp, p, &btstack));
2634 BT_MARK_DIRTY(mp, ip);
2636 * acquire a transaction lock on the leaf page;
2638 * action:xad deletion;
2640 tlck = txLock(tid, ip, mp, tlckXTREE);
2641 xtlck = (struct xtlock *) & tlck->lock;
2643 (xtlck->lwm.offset) ? min(index, xtlck->lwm.offset) : index;
2645 /* if delete from middle, shift left/compact the remaining entries */
2646 if (index < nextindex - 1)
2647 memmove(&p->xad[index], &p->xad[index + 1],
2648 (nextindex - index - 1) * sizeof(xad_t));
2656 /* - TBD for defragmentaion/reorganization -
2661 * free empty pages as propagating deletion up the tree
2668 xtDeleteUp(tid_t tid, struct inode *ip,
2669 struct metapage * fmp, xtpage_t * fp, struct btstack * btstack)
2672 struct metapage *mp;
2674 int index, nextindex;
2677 struct btframe *parent;
2679 struct xtlock *xtlck;
2682 * keep root leaf page which has become empty
2684 if (fp->header.flag & BT_ROOT) {
2685 /* keep the root page */
2686 fp->header.flag &= ~BT_INTERNAL;
2687 fp->header.flag |= BT_LEAF;
2688 fp->header.nextindex = cpu_to_le16(XTENTRYSTART);
2690 /* XT_PUTPAGE(fmp); */
2696 * free non-root leaf page
2698 if ((rc = xtRelink(tid, ip, fp))) {
2703 xaddr = addressPXD(&fp->header.self);
2704 xlen = lengthPXD(&fp->header.self);
2705 /* free the page extent */
2706 dbFree(ip, xaddr, (s64) xlen);
2708 /* free the buffer page */
2709 discard_metapage(fmp);
2712 * propagate page deletion up the index tree
2714 * If the delete from the parent page makes it empty,
2715 * continue all the way up the tree.
2716 * stop if the root page is reached (which is never deleted) or
2717 * if the entry deletion does not empty the page.
2719 while ((parent = BT_POP(btstack)) != NULL) {
2720 /* get/pin the parent page <sp> */
2721 XT_GETPAGE(ip, parent->bn, mp, PSIZE, p, rc);
2725 index = parent->index;
2727 /* delete the entry for the freed child page from parent.
2729 nextindex = le16_to_cpu(p->header.nextindex);
2732 * the parent has the single entry being deleted:
2733 * free the parent page which has become empty.
2735 if (nextindex == 1) {
2736 if (p->header.flag & BT_ROOT) {
2737 /* keep the root page */
2738 p->header.flag &= ~BT_INTERNAL;
2739 p->header.flag |= BT_LEAF;
2740 p->header.nextindex =
2741 cpu_to_le16(XTENTRYSTART);
2743 /* XT_PUTPAGE(mp); */
2747 /* free the parent page */
2748 if ((rc = xtRelink(tid, ip, p)))
2751 xaddr = addressPXD(&p->header.self);
2752 /* free the page extent */
2754 (s64) JFS_SBI(ip->i_sb)->nbperpage);
2756 /* unpin/free the buffer page */
2757 discard_metapage(mp);
2764 * the parent has other entries remaining:
2765 * delete the router entry from the parent page.
2768 BT_MARK_DIRTY(mp, ip);
2770 * acquire a transaction lock on the leaf page;
2772 * action:xad deletion;
2774 tlck = txLock(tid, ip, mp, tlckXTREE);
2775 xtlck = (struct xtlock *) & tlck->lock;
2777 (xtlck->lwm.offset) ? min(index,
2781 /* if delete from middle,
2782 * shift left/compact the remaining entries in the page
2784 if (index < nextindex - 1)
2785 memmove(&p->xad[index], &p->xad[index + 1],
2786 (nextindex - index -
2787 1) << L2XTSLOTSIZE);
2789 p->header.nextindex =
2790 cpu_to_le16(le16_to_cpu(p->header.nextindex) -
2792 jfs_info("xtDeleteUp(entry): 0x%lx[%d]",
2793 (ulong) parent->bn, index);
2796 /* unpin the parent page */
2799 /* exit propagation up */
2808 * NAME: xtRelocate()
2810 * FUNCTION: relocate xtpage or data extent of regular file;
2811 * This function is mainly used by defragfs utility.
2813 * NOTE: This routine does not have the logic to handle
2814 * uncommitted allocated extent. The caller should call
2815 * txCommit() to commit all the allocation before call
2819 xtRelocate(tid_t tid, struct inode * ip, xad_t * oxad, /* old XAD */
2820 s64 nxaddr, /* new xaddr */
2822 { /* extent type: XTPAGE or DATAEXT */
2824 struct tblock *tblk;
2826 struct xtlock *xtlck;
2827 struct metapage *mp, *pmp, *lmp, *rmp; /* meta-page buffer */
2828 xtpage_t *p, *pp, *rp, *lp; /* base B+-tree index page */
2833 s64 oxaddr, sxaddr, dxaddr, nextbn, prevbn;
2835 s64 offset, nbytes, nbrd, pno;
2836 int nb, npages, nblks;
2840 struct pxd_lock *pxdlock;
2841 struct btstack btstack; /* traverse stack */
2843 xtype = xtype & EXTENT_TYPE;
2845 xoff = offsetXAD(oxad);
2846 oxaddr = addressXAD(oxad);
2847 xlen = lengthXAD(oxad);
2849 /* validate extent offset */
2850 offset = xoff << JFS_SBI(ip->i_sb)->l2bsize;
2851 if (offset >= ip->i_size)
2852 return -ESTALE; /* stale extent */
2854 jfs_info("xtRelocate: xtype:%d xoff:0x%lx xlen:0x%x xaddr:0x%lx:0x%lx",
2855 xtype, (ulong) xoff, xlen, (ulong) oxaddr, (ulong) nxaddr);
2858 * 1. get and validate the parent xtpage/xad entry
2859 * covering the source extent to be relocated;
2861 if (xtype == DATAEXT) {
2862 /* search in leaf entry */
2863 rc = xtSearch(ip, xoff, &cmp, &btstack, 0);
2867 /* retrieve search result */
2868 XT_GETSEARCH(ip, btstack.top, bn, pmp, pp, index);
2875 /* validate for exact match with a single entry */
2876 xad = &pp->xad[index];
2877 if (addressXAD(xad) != oxaddr || lengthXAD(xad) != xlen) {
2881 } else { /* (xtype == XTPAGE) */
2883 /* search in internal entry */
2884 rc = xtSearchNode(ip, oxad, &cmp, &btstack, 0);
2888 /* retrieve search result */
2889 XT_GETSEARCH(ip, btstack.top, bn, pmp, pp, index);
2896 /* xtSearchNode() validated for exact match with a single entry
2898 xad = &pp->xad[index];
2900 jfs_info("xtRelocate: parent xad entry validated.");
2903 * 2. relocate the extent
2905 if (xtype == DATAEXT) {
2906 /* if the extent is allocated-but-not-recorded
2907 * there is no real data to be moved in this extent,
2909 if (xad->flag & XAD_NOTRECORDED)
2912 /* release xtpage for cmRead()/xtLookup() */
2918 * copy target data pages to be relocated;
2920 * data extent must start at page boundary and
2921 * multiple of page size (except the last data extent);
2922 * read in each page of the source data extent into cbuf,
2923 * update the cbuf extent descriptor of the page to be
2924 * homeward bound to new dst data extent
2925 * copy the data from the old extent to new extent.
2926 * copy is essential for compressed files to avoid problems
2927 * that can arise if there was a change in compression
2929 * it is a good strategy because it may disrupt cache
2930 * policy to keep the pages in memory afterwards.
2932 offset = xoff << JFS_SBI(ip->i_sb)->l2bsize;
2933 assert((offset & CM_OFFSET) == 0);
2934 nbytes = xlen << JFS_SBI(ip->i_sb)->l2bsize;
2935 pno = offset >> CM_L2BSIZE;
2936 npages = (nbytes + (CM_BSIZE - 1)) >> CM_L2BSIZE;
2938 npages = ((offset + nbytes - 1) >> CM_L2BSIZE) -
2939 (offset >> CM_L2BSIZE) + 1;
2944 /* process the request one cache buffer at a time */
2945 for (nbrd = 0; nbrd < nbytes; nbrd += nb,
2946 offset += nb, pno++, npages--) {
2947 /* compute page size */
2948 nb = min(nbytes - nbrd, CM_BSIZE);
2950 /* get the cache buffer of the page */
2951 if (rc = cmRead(ip, offset, npages, &cp))
2954 assert(addressPXD(&cp->cm_pxd) == sxaddr);
2955 assert(!cp->cm_modified);
2957 /* bind buffer with the new extent address */
2958 nblks = nb >> JFS_IP(ip->i_sb)->l2bsize;
2959 cmSetXD(ip, cp, pno, dxaddr, nblks);
2961 /* release the cbuf, mark it as modified */
2968 /* get back parent page */
2969 if ((rc = xtSearch(ip, xoff, &cmp, &btstack, 0)))
2972 XT_GETSEARCH(ip, btstack.top, bn, pmp, pp, index);
2973 jfs_info("xtRelocate: target data extent relocated.");
2974 } else { /* (xtype == XTPAGE) */
2977 * read in the target xtpage from the source extent;
2979 XT_GETPAGE(ip, oxaddr, mp, PSIZE, p, rc);
2986 * read in sibling pages if any to update sibling pointers;
2989 if (p->header.next) {
2990 nextbn = le64_to_cpu(p->header.next);
2991 XT_GETPAGE(ip, nextbn, rmp, PSIZE, rp, rc);
3000 if (p->header.prev) {
3001 prevbn = le64_to_cpu(p->header.prev);
3002 XT_GETPAGE(ip, prevbn, lmp, PSIZE, lp, rc);
3012 /* at this point, all xtpages to be updated are in memory */
3015 * update sibling pointers of sibling xtpages if any;
3018 BT_MARK_DIRTY(lmp, ip);
3020 txLock(tid, ip, lmp, tlckXTREE | tlckRELINK);
3021 lp->header.next = cpu_to_le64(nxaddr);
3026 BT_MARK_DIRTY(rmp, ip);
3028 txLock(tid, ip, rmp, tlckXTREE | tlckRELINK);
3029 rp->header.prev = cpu_to_le64(nxaddr);
3034 * update the target xtpage to be relocated
3036 * update the self address of the target page
3037 * and write to destination extent;
3038 * redo image covers the whole xtpage since it is new page
3039 * to the destination extent;
3040 * update of bmap for the free of source extent
3041 * of the target xtpage itself:
3042 * update of bmap for the allocation of destination extent
3043 * of the target xtpage itself:
3044 * update of bmap for the extents covered by xad entries in
3045 * the target xtpage is not necessary since they are not
3047 * if not committed before this relocation,
3048 * target page may contain XAD_NEW entries which must
3049 * be scanned for bmap update (logredo() always
3050 * scan xtpage REDOPAGE image for bmap update);
3051 * if committed before this relocation (tlckRELOCATE),
3052 * scan may be skipped by commit() and logredo();
3054 BT_MARK_DIRTY(mp, ip);
3055 /* tlckNEW init xtlck->lwm.offset = XTENTRYSTART; */
3056 tlck = txLock(tid, ip, mp, tlckXTREE | tlckNEW);
3057 xtlck = (struct xtlock *) & tlck->lock;
3059 /* update the self address in the xtpage header */
3060 pxd = &p->header.self;
3061 PXDaddress(pxd, nxaddr);
3063 /* linelock for the after image of the whole page */
3065 le16_to_cpu(p->header.nextindex) - xtlck->lwm.offset;
3067 /* update the buffer extent descriptor of target xtpage */
3068 xsize = xlen << JFS_SBI(ip->i_sb)->l2bsize;
3069 bmSetXD(mp, nxaddr, xsize);
3071 /* unpin the target page to new homeward bound */
3073 jfs_info("xtRelocate: target xtpage relocated.");
3077 * 3. acquire maplock for the source extent to be freed;
3079 * acquire a maplock saving the src relocated extent address;
3080 * to free of the extent at commit time;
3083 /* if DATAEXT relocation, write a LOG_UPDATEMAP record for
3084 * free PXD of the source data extent (logredo() will update
3085 * bmap for free of source data extent), and update bmap for
3086 * free of the source data extent;
3088 if (xtype == DATAEXT)
3089 tlck = txMaplock(tid, ip, tlckMAP);
3090 /* if XTPAGE relocation, write a LOG_NOREDOPAGE record
3091 * for the source xtpage (logredo() will init NoRedoPage
3092 * filter and will also update bmap for free of the source
3093 * xtpage), and update bmap for free of the source xtpage;
3094 * N.B. We use tlckMAP instead of tlkcXTREE because there
3095 * is no buffer associated with this lock since the buffer
3096 * has been redirected to the target location.
3098 else /* (xtype == XTPAGE) */
3099 tlck = txMaplock(tid, ip, tlckMAP | tlckRELOCATE);
3101 pxdlock = (struct pxd_lock *) & tlck->lock;
3102 pxdlock->flag = mlckFREEPXD;
3103 PXDaddress(&pxdlock->pxd, oxaddr);
3104 PXDlength(&pxdlock->pxd, xlen);
3108 * 4. update the parent xad entry for relocation;
3110 * acquire tlck for the parent entry with XAD_NEW as entry
3111 * update which will write LOG_REDOPAGE and update bmap for
3112 * allocation of XAD_NEW destination extent;
3114 jfs_info("xtRelocate: update parent xad entry.");
3115 BT_MARK_DIRTY(pmp, ip);
3116 tlck = txLock(tid, ip, pmp, tlckXTREE | tlckGROW);
3117 xtlck = (struct xtlock *) & tlck->lock;
3119 /* update the XAD with the new destination extent; */
3120 xad = &pp->xad[index];
3121 xad->flag |= XAD_NEW;
3122 XADaddress(xad, nxaddr);
3124 xtlck->lwm.offset = min(index, xtlck->lwm.offset);
3125 xtlck->lwm.length = le16_to_cpu(pp->header.nextindex) -
3128 /* unpin the parent xtpage */
3138 * function: search for the internal xad entry covering specified extent.
3139 * This function is mainly used by defragfs utility.
3143 * xad - extent to find;
3144 * cmpp - comparison result:
3145 * btstack - traverse stack;
3146 * flag - search process flag;
3149 * btstack contains (bn, index) of search path traversed to the entry.
3150 * *cmpp is set to result of comparison with the entry returned.
3151 * the page containing the entry is pinned at exit.
3153 static int xtSearchNode(struct inode *ip, xad_t * xad, /* required XAD entry */
3154 int *cmpp, struct btstack * btstack, int flag)
3159 int cmp = 1; /* init for empty page */
3160 s64 bn; /* block number */
3161 struct metapage *mp; /* meta-page buffer */
3162 xtpage_t *p; /* page */
3163 int base, index, lim;
3164 struct btframe *btsp;
3169 xoff = offsetXAD(xad);
3170 xlen = lengthXAD(xad);
3171 xaddr = addressXAD(xad);
3174 * search down tree from root:
3176 * between two consecutive entries of <Ki, Pi> and <Kj, Pj> of
3177 * internal page, child page Pi contains entry with k, Ki <= K < Kj.
3179 * if entry with search key K is not found
3180 * internal page search find the entry with largest key Ki
3181 * less than K which point to the child page to search;
3182 * leaf page search find the entry with smallest key Kj
3183 * greater than K so that the returned index is the position of
3184 * the entry to be shifted right for insertion of new entry.
3185 * for empty tree, search key is greater than any key of the tree.
3187 * by convention, root bn = 0.
3190 /* get/pin the page to search */
3191 XT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
3194 if (p->header.flag & BT_LEAF) {
3199 lim = le16_to_cpu(p->header.nextindex) - XTENTRYSTART;
3202 * binary search with search key K on the current page
3204 for (base = XTENTRYSTART; lim; lim >>= 1) {
3205 index = base + (lim >> 1);
3207 XT_CMP(cmp, xoff, &p->xad[index], t64);
3212 * verify for exact match;
3214 if (xaddr == addressXAD(&p->xad[index]) &&
3215 xoff == offsetXAD(&p->xad[index])) {
3218 /* save search result */
3219 btsp = btstack->top;
3221 btsp->index = index;
3227 /* descend/search its child page */
3238 * search miss - non-leaf page:
3240 * base is the smallest index with key (Kj) greater than
3241 * search key (K) and may be zero or maxentry index.
3242 * if base is non-zero, decrement base by one to get the parent
3243 * entry of the child page to search.
3245 index = base ? base - 1 : base;
3248 * go down to child page
3251 /* get the child page block number */
3252 bn = addressXAD(&p->xad[index]);
3254 /* unpin the parent page */
3264 * link around a freed page.
3273 static int xtRelink(tid_t tid, struct inode *ip, xtpage_t * p)
3276 struct metapage *mp;
3280 nextbn = le64_to_cpu(p->header.next);
3281 prevbn = le64_to_cpu(p->header.prev);
3283 /* update prev pointer of the next page */
3285 XT_GETPAGE(ip, nextbn, mp, PSIZE, p, rc);
3290 * acquire a transaction lock on the page;
3292 * action: update prev pointer;
3294 BT_MARK_DIRTY(mp, ip);
3295 tlck = txLock(tid, ip, mp, tlckXTREE | tlckRELINK);
3297 /* the page may already have been tlock'd */
3299 p->header.prev = cpu_to_le64(prevbn);
3304 /* update next pointer of the previous page */
3306 XT_GETPAGE(ip, prevbn, mp, PSIZE, p, rc);
3311 * acquire a transaction lock on the page;
3313 * action: update next pointer;
3315 BT_MARK_DIRTY(mp, ip);
3316 tlck = txLock(tid, ip, mp, tlckXTREE | tlckRELINK);
3318 /* the page may already have been tlock'd */
3320 p->header.next = le64_to_cpu(nextbn);
3327 #endif /* _STILL_TO_PORT */
3333 * initialize file root (inline in inode)
3335 void xtInitRoot(tid_t tid, struct inode *ip)
3340 * acquire a transaction lock on the root
3344 txLock(tid, ip, (struct metapage *) &JFS_IP(ip)->bxflag,
3345 tlckXTREE | tlckNEW);
3346 p = &JFS_IP(ip)->i_xtroot;
3348 p->header.flag = DXD_INDEX | BT_ROOT | BT_LEAF;
3349 p->header.nextindex = cpu_to_le16(XTENTRYSTART);
3351 if (S_ISDIR(ip->i_mode))
3352 p->header.maxentry = cpu_to_le16(XTROOTINITSLOT_DIR);
3354 p->header.maxentry = cpu_to_le16(XTROOTINITSLOT);
3364 * We can run into a deadlock truncating a file with a large number of
3365 * xtree pages (large fragmented file). A robust fix would entail a
3366 * reservation system where we would reserve a number of metadata pages
3367 * and tlocks which we would be guaranteed without a deadlock. Without
3368 * this, a partial fix is to limit number of metadata pages we will lock
3369 * in a single transaction. Currently we will truncate the file so that
3370 * no more than 50 leaf pages will be locked. The caller of xtTruncate
3371 * will be responsible for ensuring that the current transaction gets
3372 * committed, and that subsequent transactions are created to truncate
3373 * the file further if needed.
3375 #define MAX_TRUNCATE_LEAVES 50
3381 * traverse for truncation logging backward bottom up;
3382 * terminate at the last extent entry at the current subtree
3383 * root page covering new down size.
3384 * truncation may occur within the last extent entry.
3390 * int type) {PWMAP, PMAP, WMAP; DELETE, TRUNCATE}
3396 * 1. truncate (non-COMMIT_NOLINK file)
3397 * by jfs_truncate() or jfs_open(O_TRUNC):
3399 * 2. truncate index table of directory when last entry removed
3400 * map update via tlock at commit time;
3402 * Call xtTruncate_pmap instead
3404 * 1. remove (free zero link count) on last reference release
3405 * (pmap has been freed at commit zero link count);
3406 * 2. truncate (COMMIT_NOLINK file, i.e., tmp file):
3408 * map update directly at truncation time;
3411 * no LOG_NOREDOPAGE is required (NOREDOFILE is sufficient);
3412 * else if (TRUNCATE)
3413 * must write LOG_NOREDOPAGE for deleted index page;
3415 * pages may already have been tlocked by anonymous transactions
3416 * during file growth (i.e., write) before truncation;
3418 * except last truncated entry, deleted entries remains as is
3419 * in the page (nextindex is updated) for other use
3420 * (e.g., log/update allocation map): this avoid copying the page
3421 * info but delay free of pages;
3424 s64 xtTruncate(tid_t tid, struct inode *ip, s64 newsize, int flag)
3428 struct metapage *mp;
3431 int index, nextindex;
3434 int xlen, len, freexlen;
3435 struct btstack btstack;
3436 struct btframe *parent;
3437 struct tblock *tblk = NULL;
3438 struct tlock *tlck = NULL;
3439 struct xtlock *xtlck = NULL;
3440 struct xdlistlock xadlock; /* maplock for COMMIT_WMAP */
3441 struct pxd_lock *pxdlock; /* maplock for COMMIT_WMAP */
3444 int locked_leaves = 0;
3446 /* save object truncation type */
3448 tblk = tid_to_tblock(tid);
3449 tblk->xflag |= flag;
3455 assert(flag != COMMIT_PMAP);
3457 if (flag == COMMIT_PWMAP)
3461 xadlock.flag = mlckFREEXADLIST;
3466 * if the newsize is not an integral number of pages,
3467 * the file between newsize and next page boundary will
3469 * if truncating into a file hole, it will cause
3470 * a full block to be allocated for the logical block.
3474 * release page blocks of truncated region <teof, eof>
3476 * free the data blocks from the leaf index blocks.
3477 * delete the parent index entries corresponding to
3478 * the freed child data/index blocks.
3479 * free the index blocks themselves which aren't needed
3480 * in new sized file.
3482 * index blocks are updated only if the blocks are to be
3483 * retained in the new sized file.
3484 * if type is PMAP, the data and index pages are NOT
3485 * freed, and the data and index blocks are NOT freed
3487 * (this will allow continued access of data/index of
3488 * temporary file (zerolink count file truncated to zero-length)).
3490 teof = (newsize + (JFS_SBI(ip->i_sb)->bsize - 1)) >>
3491 JFS_SBI(ip->i_sb)->l2bsize;
3499 * root resides in the inode
3504 * first access of each page:
3507 XT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
3511 /* process entries backward from last index */
3512 index = le16_to_cpu(p->header.nextindex) - 1;
3514 if (p->header.flag & BT_INTERNAL)
3521 /* Since this is the rightmost leaf, and we may have already freed
3522 * a page that was formerly to the right, let's make sure that the
3523 * next pointer is zero.
3525 if (p->header.next) {
3528 * Make sure this change to the header is logged.
3529 * If we really truncate this leaf, the flag
3530 * will be changed to tlckTRUNCATE
3532 tlck = txLock(tid, ip, mp, tlckXTREE|tlckGROW);
3533 BT_MARK_DIRTY(mp, ip);
3539 /* does region covered by leaf page precede Teof ? */
3540 xad = &p->xad[index];
3541 xoff = offsetXAD(xad);
3542 xlen = lengthXAD(xad);
3543 if (teof >= xoff + xlen) {
3548 /* (re)acquire tlock of the leaf page */
3550 if (++locked_leaves > MAX_TRUNCATE_LEAVES) {
3552 * We need to limit the size of the transaction
3553 * to avoid exhausting pagecache & tlocks
3556 newsize = (xoff + xlen) << JFS_SBI(ip->i_sb)->l2bsize;
3559 tlck = txLock(tid, ip, mp, tlckXTREE);
3560 tlck->type = tlckXTREE | tlckTRUNCATE;
3561 xtlck = (struct xtlock *) & tlck->lock;
3562 xtlck->hwm.offset = le16_to_cpu(p->header.nextindex) - 1;
3564 BT_MARK_DIRTY(mp, ip);
3567 * scan backward leaf page entries
3569 for (; index >= XTENTRYSTART; index--) {
3570 xad = &p->xad[index];
3571 xoff = offsetXAD(xad);
3572 xlen = lengthXAD(xad);
3573 xaddr = addressXAD(xad);
3576 * The "data" for a directory is indexed by the block
3577 * device's address space. This metadata must be invalidated
3580 if (S_ISDIR(ip->i_mode) && (teof == 0))
3581 invalidate_xad_metapages(ip, *xad);
3583 * entry beyond eof: continue scan of current page
3585 * ---|---=======------->
3594 * (xoff <= teof): last entry to be deleted from page;
3595 * If other entries remain in page: keep and update the page.
3599 * eof == entry_start: delete the entry
3601 * -------|=======------->
3608 if (index == XTENTRYSTART)
3614 * eof within the entry: truncate the entry.
3616 * -------===|===------->
3619 else if (teof < xoff + xlen) {
3620 /* update truncated entry */
3622 freexlen = xlen - len;
3623 XADlength(xad, len);
3625 /* save pxd of truncated extent in tlck */
3627 if (log) { /* COMMIT_PWMAP */
3628 xtlck->lwm.offset = (xtlck->lwm.offset) ?
3629 min(index, (int)xtlck->lwm.offset) : index;
3630 xtlck->lwm.length = index + 1 -
3632 xtlck->twm.offset = index;
3633 pxdlock = (struct pxd_lock *) & xtlck->pxdlock;
3634 pxdlock->flag = mlckFREEPXD;
3635 PXDaddress(&pxdlock->pxd, xaddr);
3636 PXDlength(&pxdlock->pxd, freexlen);
3638 /* free truncated extent */
3639 else { /* COMMIT_WMAP */
3641 pxdlock = (struct pxd_lock *) & xadlock;
3642 pxdlock->flag = mlckFREEPXD;
3643 PXDaddress(&pxdlock->pxd, xaddr);
3644 PXDlength(&pxdlock->pxd, freexlen);
3645 txFreeMap(ip, pxdlock, NULL, COMMIT_WMAP);
3647 /* reset map lock */
3648 xadlock.flag = mlckFREEXADLIST;
3651 /* current entry is new last entry; */
3652 nextindex = index + 1;
3657 * eof beyond the entry:
3659 * -------=======---|--->
3662 else { /* (xoff + xlen < teof) */
3664 nextindex = index + 1;
3667 if (nextindex < le16_to_cpu(p->header.nextindex)) {
3668 if (!log) { /* COMMIT_WAMP */
3669 xadlock.xdlist = &p->xad[nextindex];
3671 le16_to_cpu(p->header.nextindex) -
3673 txFreeMap(ip, (struct maplock *) & xadlock,
3676 p->header.nextindex = cpu_to_le16(nextindex);
3681 /* assert(freed == 0); */
3683 } /* end scan of leaf page entries */
3688 * leaf page become empty: free the page if type != PMAP
3690 if (log) { /* COMMIT_PWMAP */
3691 /* txCommit() with tlckFREE:
3692 * free data extents covered by leaf [XTENTRYSTART:hwm);
3693 * invalidate leaf if COMMIT_PWMAP;
3694 * if (TRUNCATE), will write LOG_NOREDOPAGE;
3696 tlck->type = tlckXTREE | tlckFREE;
3697 } else { /* COMMIT_WAMP */
3699 /* free data extents covered by leaf */
3700 xadlock.xdlist = &p->xad[XTENTRYSTART];
3702 le16_to_cpu(p->header.nextindex) - XTENTRYSTART;
3703 txFreeMap(ip, (struct maplock *) & xadlock, NULL, COMMIT_WMAP);
3706 if (p->header.flag & BT_ROOT) {
3707 p->header.flag &= ~BT_INTERNAL;
3708 p->header.flag |= BT_LEAF;
3709 p->header.nextindex = cpu_to_le16(XTENTRYSTART);
3711 XT_PUTPAGE(mp); /* debug */
3714 if (log) { /* COMMIT_PWMAP */
3715 /* page will be invalidated at tx completion
3718 } else { /* COMMIT_WMAP */
3721 lid_to_tlock(mp->lid)->flag |= tlckFREELOCK;
3723 /* invalidate empty leaf page */
3724 discard_metapage(mp);
3729 * the leaf page become empty: delete the parent entry
3730 * for the leaf page if the parent page is to be kept
3731 * in the new sized file.
3735 * go back up to the parent page
3738 /* pop/restore parent entry for the current child page */
3739 if ((parent = BT_POP(&btstack)) == NULL)
3740 /* current page must have been root */
3743 /* get back the parent page */
3745 XT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
3749 index = parent->index;
3752 * child page was not empty:
3755 /* has any entry deleted from parent ? */
3756 if (index < le16_to_cpu(p->header.nextindex) - 1) {
3757 /* (re)acquire tlock on the parent page */
3758 if (log) { /* COMMIT_PWMAP */
3759 /* txCommit() with tlckTRUNCATE:
3760 * free child extents covered by parent [);
3762 tlck = txLock(tid, ip, mp, tlckXTREE);
3763 xtlck = (struct xtlock *) & tlck->lock;
3764 if (!(tlck->type & tlckTRUNCATE)) {
3766 le16_to_cpu(p->header.
3769 tlckXTREE | tlckTRUNCATE;
3771 } else { /* COMMIT_WMAP */
3773 /* free child extents covered by parent */
3774 xadlock.xdlist = &p->xad[index + 1];
3776 le16_to_cpu(p->header.nextindex) -
3778 txFreeMap(ip, (struct maplock *) & xadlock,
3781 BT_MARK_DIRTY(mp, ip);
3783 p->header.nextindex = cpu_to_le16(index + 1);
3790 * child page was empty:
3792 nfreed += lengthXAD(&p->xad[index]);
3795 * During working map update, child page's tlock must be handled
3796 * before parent's. This is because the parent's tlock will cause
3797 * the child's disk space to be marked available in the wmap, so
3798 * it's important that the child page be released by that time.
3800 * ToDo: tlocks should be on doubly-linked list, so we can
3801 * quickly remove it and add it to the end.
3805 * Move parent page's tlock to the end of the tid's tlock list
3807 if (log && mp->lid && (tblk->last != mp->lid) &&
3808 lid_to_tlock(mp->lid)->tid) {
3809 lid_t lid = mp->lid;
3812 tlck = lid_to_tlock(lid);
3814 if (tblk->next == lid)
3815 tblk->next = tlck->next;
3817 for (prev = lid_to_tlock(tblk->next);
3819 prev = lid_to_tlock(prev->next)) {
3822 prev->next = tlck->next;
3824 lid_to_tlock(tblk->last)->next = lid;
3830 * parent page become empty: free the page
3832 if (index == XTENTRYSTART) {
3833 if (log) { /* COMMIT_PWMAP */
3834 /* txCommit() with tlckFREE:
3835 * free child extents covered by parent;
3836 * invalidate parent if COMMIT_PWMAP;
3838 tlck = txLock(tid, ip, mp, tlckXTREE);
3839 xtlck = (struct xtlock *) & tlck->lock;
3841 le16_to_cpu(p->header.nextindex) - 1;
3842 tlck->type = tlckXTREE | tlckFREE;
3843 } else { /* COMMIT_WMAP */
3845 /* free child extents covered by parent */
3846 xadlock.xdlist = &p->xad[XTENTRYSTART];
3848 le16_to_cpu(p->header.nextindex) -
3850 txFreeMap(ip, (struct maplock *) & xadlock, NULL,
3853 BT_MARK_DIRTY(mp, ip);
3855 if (p->header.flag & BT_ROOT) {
3856 p->header.flag &= ~BT_INTERNAL;
3857 p->header.flag |= BT_LEAF;
3858 p->header.nextindex = cpu_to_le16(XTENTRYSTART);
3859 if (le16_to_cpu(p->header.maxentry) == XTROOTMAXSLOT) {
3861 * Shrink root down to allow inline
3862 * EA (otherwise fsck complains)
3864 p->header.maxentry =
3865 cpu_to_le16(XTROOTINITSLOT);
3866 JFS_IP(ip)->mode2 |= INLINEEA;
3869 XT_PUTPAGE(mp); /* debug */
3872 if (log) { /* COMMIT_PWMAP */
3873 /* page will be invalidated at tx completion
3876 } else { /* COMMIT_WMAP */
3879 lid_to_tlock(mp->lid)->flag |=
3882 /* invalidate parent page */
3883 discard_metapage(mp);
3886 /* parent has become empty and freed:
3887 * go back up to its parent page
3894 * parent page still has entries for front region;
3897 /* try truncate region covered by preceding entry
3898 * (process backward)
3902 /* go back down to the child page corresponding
3909 * internal page: go down to child page of current entry
3912 /* save current parent entry for the child page */
3913 BT_PUSH(&btstack, bn, index);
3915 /* get child page */
3916 xad = &p->xad[index];
3917 bn = addressXAD(xad);
3920 * first access of each internal entry:
3922 /* release parent page */
3925 /* process the child page */
3930 * update file resource stat
3934 if (S_ISDIR(ip->i_mode) && !newsize)
3935 ip->i_size = 1; /* fsck hates zero-length directories */
3937 ip->i_size = newsize;
3939 /* update quota allocation to reflect freed blocks */
3940 DQUOT_FREE_BLOCK(ip, nfreed);
3943 * free tlock of invalidated pages
3945 if (flag == COMMIT_WMAP)
3956 * Perform truncate to zero lenghth for deleted file, leaving the
3957 * the xtree and working map untouched. This allows the file to
3958 * be accessed via open file handles, while the delete of the file
3959 * is committed to disk.
3964 * s64 committed_size)
3966 * return: new committed size
3970 * To avoid deadlock by holding too many transaction locks, the
3971 * truncation may be broken up into multiple transactions.
3972 * The committed_size keeps track of part of the file has been
3973 * freed from the pmaps.
3975 s64 xtTruncate_pmap(tid_t tid, struct inode *ip, s64 committed_size)
3978 struct btstack btstack;
3981 int locked_leaves = 0;
3982 struct metapage *mp;
3984 struct btframe *parent;
3986 struct tblock *tblk;
3987 struct tlock *tlck = NULL;
3991 struct xtlock *xtlck = NULL;
3993 /* save object truncation type */
3994 tblk = tid_to_tblock(tid);
3995 tblk->xflag |= COMMIT_PMAP;
4000 if (committed_size) {
4001 xoff = (committed_size >> JFS_SBI(ip->i_sb)->l2bsize) - 1;
4002 rc = xtSearch(ip, xoff, &cmp, &btstack, 0);
4006 XT_GETSEARCH(ip, btstack.top, bn, mp, p, index);
4011 "xtTruncate_pmap: did not find extent");
4018 * root resides in the inode
4023 * first access of each page:
4026 XT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
4030 /* process entries backward from last index */
4031 index = le16_to_cpu(p->header.nextindex) - 1;
4033 if (p->header.flag & BT_INTERNAL)
4041 if (++locked_leaves > MAX_TRUNCATE_LEAVES) {
4043 * We need to limit the size of the transaction
4044 * to avoid exhausting pagecache & tlocks
4046 xad = &p->xad[index];
4047 xoff = offsetXAD(xad);
4048 xlen = lengthXAD(xad);
4050 return (xoff + xlen) << JFS_SBI(ip->i_sb)->l2bsize;
4052 tlck = txLock(tid, ip, mp, tlckXTREE);
4053 tlck->type = tlckXTREE | tlckFREE;
4054 xtlck = (struct xtlock *) & tlck->lock;
4055 xtlck->hwm.offset = index;
4061 * go back up to the parent page
4064 /* pop/restore parent entry for the current child page */
4065 if ((parent = BT_POP(&btstack)) == NULL)
4066 /* current page must have been root */
4069 /* get back the parent page */
4071 XT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
4075 index = parent->index;
4078 * parent page become empty: free the page
4080 if (index == XTENTRYSTART) {
4081 /* txCommit() with tlckFREE:
4082 * free child extents covered by parent;
4083 * invalidate parent if COMMIT_PWMAP;
4085 tlck = txLock(tid, ip, mp, tlckXTREE);
4086 xtlck = (struct xtlock *) & tlck->lock;
4088 le16_to_cpu(p->header.nextindex) - 1;
4089 tlck->type = tlckXTREE | tlckFREE;
4093 if (p->header.flag & BT_ROOT) {
4101 * parent page still has entries for front region;
4106 * internal page: go down to child page of current entry
4109 /* save current parent entry for the child page */
4110 BT_PUSH(&btstack, bn, index);
4112 /* get child page */
4113 xad = &p->xad[index];
4114 bn = addressXAD(xad);
4117 * first access of each internal entry:
4119 /* release parent page */
4122 /* process the child page */
4131 #ifdef _JFS_DEBUG_XTREE
4135 * function: traverse forward
4137 int xtDisplayTree(struct inode *ip)
4140 struct metapage *mp;
4143 int index, lastindex, v, h;
4145 struct btstack btstack;
4146 struct btframe *btsp;
4147 struct btframe *parent;
4149 printk("display B+-tree.\n");
4152 btsp = btstack.stack;
4157 * root resides in the inode
4163 * first access of each page:
4166 XT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
4170 /* process entries forward from first index */
4171 index = XTENTRYSTART;
4172 lastindex = le16_to_cpu(p->header.nextindex) - 1;
4174 if (p->header.flag & BT_INTERNAL) {
4176 * first access of each internal page
4179 } else { /* (p->header.flag & BT_LEAF) */
4182 * first access of each leaf page
4184 printf("leaf page ");
4185 xtDisplayPage(ip, bn, p);
4187 /* unpin the leaf page */
4192 * go back up to the parent page
4195 /* pop/restore parent entry for the current child page */
4196 if ((parent = (btsp == btstack.stack ? NULL : --btsp)) == NULL)
4197 /* current page must have been root */
4201 * parent page scan completed
4203 if ((index = parent->index) == (lastindex = parent->lastindex)) {
4204 /* go back up to the parent page */
4209 * parent page has entries remaining
4211 /* get back the parent page */
4213 /* v = parent->level; */
4214 XT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
4218 /* get next parent entry */
4222 * internal page: go down to child page of current entry
4225 /* push/save current parent entry for the child page */
4226 btsp->bn = pbn = bn;
4227 btsp->index = index;
4228 btsp->lastindex = lastindex;
4229 /* btsp->level = v; */
4230 /* btsp->node = h; */
4233 /* get child page */
4234 xad = &p->xad[index];
4235 bn = addressXAD(xad);
4238 * first access of each internal entry:
4240 /* release parent page */
4243 printk("traverse down 0x%lx[%d]->0x%lx\n", (ulong) pbn, index,
4248 /* process the child page */
4256 * function: display page
4258 int xtDisplayPage(struct inode *ip, s64 bn, xtpage_t * p)
4265 /* display page control */
4266 printf("bn:0x%lx flag:0x%x nextindex:%d\n",
4267 (ulong) bn, p->header.flag,
4268 le16_to_cpu(p->header.nextindex));
4270 /* display entries */
4271 xad = &p->xad[XTENTRYSTART];
4272 for (i = XTENTRYSTART, j = 1; i < le16_to_cpu(p->header.nextindex);
4274 xoff = offsetXAD(xad);
4275 xaddr = addressXAD(xad);
4276 xlen = lengthXAD(xad);
4277 printf("\t[%d] 0x%lx:0x%lx(0x%x)", i, (ulong) xoff,
4278 (ulong) xaddr, xlen);
4288 #endif /* _JFS_DEBUG_XTREE */
4296 * traverse for allocation acquiring tlock at commit time
4297 * (vs at the time of update) logging backward top down
4300 * problem - establishing that all new allocation have been
4301 * processed both for append and random write in sparse file
4302 * at the current entry at the current subtree root page
4305 int xtGather(btree_t *t)
4312 struct btstack btstack;
4313 struct btsf *parent;
4321 * root resides in the inode
4324 XT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
4328 /* new root is NOT pointed by a new entry
4329 if (p->header.flag & NEW)
4330 allocate new page lock;
4331 write a NEWPAGE log;
4336 * first access of each page:
4338 /* process entries backward from last index */
4339 index = le16_to_cpu(p->header.nextindex) - 1;
4341 if (p->header.flag & BT_LEAF) {
4343 * first access of each leaf page
4345 /* process leaf page entries backward */
4346 for (; index >= XTENTRYSTART; index--) {
4349 * if newpage, log NEWPAGE.
4351 if (e->flag & XAD_NEW) {
4352 nfound =+ entry->length;
4353 update current page lock for the entry;
4356 * if moved, log move.
4358 } else if (e->flag & XAD_MOVED) {
4360 update current page lock for the entry;
4365 /* unpin the leaf page */
4369 * go back up to the parent page
4372 /* restore parent entry for the current child page */
4373 if ((parent = BT_POP(&btstack)) == NULL)
4374 /* current page must have been root */
4377 if ((index = parent->index) == XTENTRYSTART) {
4379 * parent page scan completed
4381 /* go back up to the parent page */
4385 * parent page has entries remaining
4387 /* get back the parent page */
4389 XT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
4393 /* first subroot page which
4394 * covers all new allocated blocks
4395 * itself not new/modified.
4396 * (if modified from split of descendent,
4397 * go down path of split page)
4399 if (nfound == nnew &&
4400 !(p->header.flag & (NEW | MOD)))
4404 /* process parent page entries backward */
4409 * first access of each internal page
4414 * internal page: go down to child page of current entry
4417 /* save current parent entry for the child page */
4418 BT_PUSH(&btstack, bn, index);
4420 /* get current entry for the child page */
4424 * first access of each internal entry:
4427 * if new entry, log btree_tnewentry.
4429 if (e->flag & XAD_NEW)
4430 update parent page lock for the entry;
4433 /* release parent page */
4436 /* get child page */
4438 XT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
4443 * first access of each non-root page:
4446 * if new, log btree_newpage.
4448 if (p->header.flag & NEW)
4449 allocate new page lock;
4450 write a NEWPAGE log (next, prev);
4453 /* process the child page */
4459 #endif /* _JFS_WIP */
4462 #ifdef CONFIG_JFS_STATISTICS
4463 int jfs_xtstat_read(char *buffer, char **start, off_t offset, int length,
4464 int *eof, void *data)
4469 len += sprintf(buffer,
4470 "JFS Xtree statistics\n"
4471 "====================\n"
4473 "fast searches = %d\n"
4480 *start = buffer + begin;