2 * Copyright (C) International Business Machines Corp., 2000-2005
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/module.h>
24 #include <linux/quotaops.h>
25 #include <linux/seq_file.h>
26 #include "jfs_incore.h"
27 #include "jfs_filsys.h"
28 #include "jfs_metapage.h"
30 #include "jfs_dinode.h"
31 #include "jfs_superblock.h"
32 #include "jfs_debug.h"
37 #define XT_INSERT 0x00000001
40 * xtree key/entry comparison: extent offset
43 * -1: k < start of extent
44 * 0: start_of_extent <= k <= end_of_extent
45 * 1: k > end_of_extent
47 #define XT_CMP(CMP, K, X, OFFSET64)\
49 OFFSET64 = offsetXAD(X);\
50 (CMP) = ((K) >= OFFSET64 + lengthXAD(X)) ? 1 :\
51 ((K) < OFFSET64) ? -1 : 0;\
54 /* write a xad entry */
55 #define XT_PUTENTRY(XAD, FLAG, OFF, LEN, ADDR)\
57 (XAD)->flag = (FLAG);\
58 XADoffset((XAD), (OFF));\
59 XADlength((XAD), (LEN));\
60 XADaddress((XAD), (ADDR));\
63 #define XT_PAGE(IP, MP) BT_PAGE(IP, MP, xtpage_t, i_xtroot)
65 /* get page buffer for specified block address */
66 /* ToDo: Replace this ugly macro with a function */
67 #define XT_GETPAGE(IP, BN, MP, SIZE, P, RC)\
69 BT_GETPAGE(IP, BN, MP, xtpage_t, SIZE, P, RC, i_xtroot)\
72 if ((le16_to_cpu((P)->header.nextindex) < XTENTRYSTART) ||\
73 (le16_to_cpu((P)->header.nextindex) > le16_to_cpu((P)->header.maxentry)) ||\
74 (le16_to_cpu((P)->header.maxentry) > (((BN)==0)?XTROOTMAXSLOT:PSIZE>>L2XTSLOTSIZE)))\
76 jfs_error((IP)->i_sb, "XT_GETPAGE: xtree page corrupt");\
85 #define XT_PUTPAGE(MP) BT_PUTPAGE(MP)
87 #define XT_GETSEARCH(IP, LEAF, BN, MP, P, INDEX) \
88 BT_GETSEARCH(IP, LEAF, BN, MP, xtpage_t, P, INDEX, i_xtroot)
89 /* xtree entry parameter descriptor */
97 struct pxdlist *pxdlist;
104 #ifdef CONFIG_JFS_STATISTICS
116 static int xtSearch(struct inode *ip, s64 xoff, s64 *next, int *cmpp,
117 struct btstack * btstack, int flag);
119 static int xtSplitUp(tid_t tid,
121 struct xtsplit * split, struct btstack * btstack);
123 static int xtSplitPage(tid_t tid, struct inode *ip, struct xtsplit * split,
124 struct metapage ** rmpp, s64 * rbnp);
126 static int xtSplitRoot(tid_t tid, struct inode *ip,
127 struct xtsplit * split, struct metapage ** rmpp);
129 #ifdef _STILL_TO_PORT
130 static int xtDeleteUp(tid_t tid, struct inode *ip, struct metapage * fmp,
131 xtpage_t * fp, struct btstack * btstack);
133 static int xtSearchNode(struct inode *ip,
135 int *cmpp, struct btstack * btstack, int flag);
137 static int xtRelink(tid_t tid, struct inode *ip, xtpage_t * fp);
138 #endif /* _STILL_TO_PORT */
143 * function: map a single page into a physical extent;
145 int xtLookup(struct inode *ip, s64 lstart,
146 s64 llen, int *pflag, s64 * paddr, s32 * plen, int no_check)
149 struct btstack btstack;
156 s64 next, size, xoff, xend;
164 /* is lookup offset beyond eof ? */
165 size = ((u64) ip->i_size + (JFS_SBI(ip->i_sb)->bsize - 1)) >>
166 JFS_SBI(ip->i_sb)->l2bsize;
172 * search for the xad entry covering the logical extent
175 if ((rc = xtSearch(ip, lstart, &next, &cmp, &btstack, 0))) {
176 jfs_err("xtLookup: xtSearch returned %d", rc);
181 * compute the physical extent covering logical extent
183 * N.B. search may have failed (e.g., hole in sparse file),
184 * and returned the index of the next entry.
186 /* retrieve search result */
187 XT_GETSEARCH(ip, btstack.top, bn, mp, p, index);
189 /* is xad found covering start of logical extent ?
190 * lstart is a page start address,
191 * i.e., lstart cannot start in a hole;
195 *plen = min(next - lstart, llen);
202 xad = &p->xad[index];
203 xoff = offsetXAD(xad);
204 xlen = lengthXAD(xad);
206 xaddr = addressXAD(xad);
208 /* initialize new pxd */
210 *paddr = xaddr + (lstart - xoff);
211 /* a page must be fully covered by an xad */
212 *plen = min(xend - lstart, llen);
223 * function: search for the xad entry covering specified offset.
227 * xoff - extent offset;
228 * nextp - address of next extent (if any) for search miss
229 * cmpp - comparison result:
230 * btstack - traverse stack;
231 * flag - search process flag (XT_INSERT);
234 * btstack contains (bn, index) of search path traversed to the entry.
235 * *cmpp is set to result of comparison with the entry returned.
236 * the page containing the entry is pinned at exit.
238 static int xtSearch(struct inode *ip, s64 xoff, s64 *nextp,
239 int *cmpp, struct btstack * btstack, int flag)
241 struct jfs_inode_info *jfs_ip = JFS_IP(ip);
243 int cmp = 1; /* init for empty page */
244 s64 bn; /* block number */
245 struct metapage *mp; /* page buffer */
246 xtpage_t *p; /* page */
248 int base, index, lim, btindex;
249 struct btframe *btsp;
250 int nsplit = 0; /* number of pages to split */
254 INCREMENT(xtStat.search);
261 * search down tree from root:
263 * between two consecutive entries of <Ki, Pi> and <Kj, Pj> of
264 * internal page, child page Pi contains entry with k, Ki <= K < Kj.
266 * if entry with search key K is not found
267 * internal page search find the entry with largest key Ki
268 * less than K which point to the child page to search;
269 * leaf page search find the entry with smallest key Kj
270 * greater than K so that the returned index is the position of
271 * the entry to be shifted right for insertion of new entry.
272 * for empty tree, search key is greater than any key of the tree.
274 * by convention, root bn = 0.
277 /* get/pin the page to search */
278 XT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
282 /* try sequential access heuristics with the previous
283 * access entry in target leaf page:
284 * once search narrowed down into the target leaf,
285 * key must either match an entry in the leaf or
286 * key entry does not exist in the tree;
289 if ((jfs_ip->btorder & BT_SEQUENTIAL) &&
290 (p->header.flag & BT_LEAF) &&
291 (index = jfs_ip->btindex) <
292 le16_to_cpu(p->header.nextindex)) {
293 xad = &p->xad[index];
294 t64 = offsetXAD(xad);
295 if (xoff < t64 + lengthXAD(xad)) {
301 /* stop sequential access heuristics */
303 } else { /* (t64 + lengthXAD(xad)) <= xoff */
305 /* try next sequential entry */
308 le16_to_cpu(p->header.nextindex)) {
310 t64 = offsetXAD(xad);
311 if (xoff < t64 + lengthXAD(xad)) {
317 /* miss: key falls between
318 * previous and this entry
325 /* (xoff >= t64 + lengthXAD(xad));
326 * matching entry may be further out:
327 * stop heuristic search
329 /* stop sequential access heuristics */
333 /* (index == p->header.nextindex);
334 * miss: key entry does not exist in
335 * the target leaf/tree
342 * if hit, return index of the entry found, and
343 * if miss, where new entry with search key is
347 /* compute number of pages to split */
348 if (flag & XT_INSERT) {
349 if (p->header.nextindex == /* little-endian */
354 btstack->nsplit = nsplit;
357 /* save search result */
363 /* update sequential access heuristics */
364 jfs_ip->btindex = index;
369 INCREMENT(xtStat.fastSearch);
373 /* well, ... full search now */
375 lim = le16_to_cpu(p->header.nextindex) - XTENTRYSTART;
378 * binary search with search key K on the current page
380 for (base = XTENTRYSTART; lim; lim >>= 1) {
381 index = base + (lim >> 1);
383 XT_CMP(cmp, xoff, &p->xad[index], t64);
388 /* search hit - leaf page:
389 * return the entry found
391 if (p->header.flag & BT_LEAF) {
394 /* compute number of pages to split */
395 if (flag & XT_INSERT) {
396 if (p->header.nextindex ==
401 btstack->nsplit = nsplit;
404 /* save search result */
410 /* init sequential access heuristics */
411 btindex = jfs_ip->btindex;
412 if (index == btindex ||
413 index == btindex + 1)
414 jfs_ip->btorder = BT_SEQUENTIAL;
416 jfs_ip->btorder = BT_RANDOM;
417 jfs_ip->btindex = index;
421 /* search hit - internal page:
422 * descend/search its child page
424 if (index < le16_to_cpu(p->header.nextindex)-1)
425 next = offsetXAD(&p->xad[index + 1]);
438 * base is the smallest index with key (Kj) greater than
439 * search key (K) and may be zero or maxentry index.
441 if (base < le16_to_cpu(p->header.nextindex))
442 next = offsetXAD(&p->xad[base]);
444 * search miss - leaf page:
446 * return location of entry (base) where new entry with
447 * search key K is to be inserted.
449 if (p->header.flag & BT_LEAF) {
452 /* compute number of pages to split */
453 if (flag & XT_INSERT) {
454 if (p->header.nextindex ==
459 btstack->nsplit = nsplit;
462 /* save search result */
468 /* init sequential access heuristics */
469 btindex = jfs_ip->btindex;
470 if (base == btindex || base == btindex + 1)
471 jfs_ip->btorder = BT_SEQUENTIAL;
473 jfs_ip->btorder = BT_RANDOM;
474 jfs_ip->btindex = base;
483 * search miss - non-leaf page:
485 * if base is non-zero, decrement base by one to get the parent
486 * entry of the child page to search.
488 index = base ? base - 1 : base;
491 * go down to child page
494 /* update number of pages to split */
495 if (p->header.nextindex == p->header.maxentry)
500 /* push (bn, index) of the parent page/entry */
501 if (BT_STACK_FULL(btstack)) {
502 jfs_error(ip->i_sb, "stack overrun in xtSearch!");
506 BT_PUSH(btstack, bn, index);
508 /* get the child page block number */
509 bn = addressXAD(&p->xad[index]);
511 /* unpin the parent page */
522 * tid - transaction id;
524 * xflag - extent flag (XAD_NOTRECORDED):
525 * xoff - extent offset;
526 * xlen - extent length;
527 * xaddrp - extent address pointer (in/out):
529 * caller allocated data extent at *xaddrp;
531 * allocate data extent and return its xaddr;
536 int xtInsert(tid_t tid, /* transaction id */
537 struct inode *ip, int xflag, s64 xoff, s32 xlen, s64 * xaddrp,
542 struct metapage *mp; /* meta-page buffer */
543 xtpage_t *p; /* base B+-tree index page */
545 int index, nextindex;
546 struct btstack btstack; /* traverse stack */
547 struct xtsplit split; /* split information */
552 struct xtlock *xtlck;
554 jfs_info("xtInsert: nxoff:0x%lx nxlen:0x%x", (ulong) xoff, xlen);
557 * search for the entry location at which to insert:
559 * xtFastSearch() and xtSearch() both returns (leaf page
560 * pinned, index at which to insert).
561 * n.b. xtSearch() may return index of maxentry of
564 if ((rc = xtSearch(ip, xoff, &next, &cmp, &btstack, XT_INSERT)))
567 /* retrieve search result */
568 XT_GETSEARCH(ip, btstack.top, bn, mp, p, index);
570 /* This test must follow XT_GETSEARCH since mp must be valid if
571 * we branch to out: */
572 if ((cmp == 0) || (next && (xlen > next - xoff))) {
578 * allocate data extent requested
580 * allocation hint: last xad
582 if ((xaddr = *xaddrp) == 0) {
583 if (index > XTENTRYSTART) {
584 xad = &p->xad[index - 1];
585 hint = addressXAD(xad) + lengthXAD(xad) - 1;
588 if ((rc = dquot_alloc_block(ip, xlen)))
590 if ((rc = dbAlloc(ip, hint, (s64) xlen, &xaddr))) {
591 dquot_free_block(ip, xlen);
597 * insert entry for new extent
602 * if the leaf page is full, split the page and
603 * propagate up the router entry for the new page from split
605 * The xtSplitUp() will insert the entry and unpin the leaf page.
607 nextindex = le16_to_cpu(p->header.nextindex);
608 if (nextindex == le16_to_cpu(p->header.maxentry)) {
615 split.pxdlist = NULL;
616 if ((rc = xtSplitUp(tid, ip, &split, &btstack))) {
617 /* undo data extent allocation */
619 dbFree(ip, xaddr, (s64) xlen);
620 dquot_free_block(ip, xlen);
630 * insert the new entry into the leaf page
633 * acquire a transaction lock on the leaf page;
635 * action: xad insertion/extension;
637 BT_MARK_DIRTY(mp, ip);
639 /* if insert into middle, shift right remaining entries. */
640 if (index < nextindex)
641 memmove(&p->xad[index + 1], &p->xad[index],
642 (nextindex - index) * sizeof(xad_t));
644 /* insert the new entry: mark the entry NEW */
645 xad = &p->xad[index];
646 XT_PUTENTRY(xad, xflag, xoff, xlen, xaddr);
648 /* advance next available entry index */
649 le16_add_cpu(&p->header.nextindex, 1);
651 /* Don't log it if there are no links to the file */
652 if (!test_cflag(COMMIT_Nolink, ip)) {
653 tlck = txLock(tid, ip, mp, tlckXTREE | tlckGROW);
654 xtlck = (struct xtlock *) & tlck->lock;
656 (xtlck->lwm.offset) ? min(index,
657 (int)xtlck->lwm.offset) : index;
659 le16_to_cpu(p->header.nextindex) - xtlck->lwm.offset;
665 /* unpin the leaf page */
676 * split full pages as propagating insertion up the tree
679 * tid - transaction id;
681 * split - entry parameter descriptor;
682 * btstack - traverse stack from xtSearch()
688 struct inode *ip, struct xtsplit * split, struct btstack * btstack)
691 struct metapage *smp;
692 xtpage_t *sp; /* split page */
693 struct metapage *rmp;
694 s64 rbn; /* new right page block number */
695 struct metapage *rcmp;
696 xtpage_t *rcp; /* right child page */
697 s64 rcbn; /* right child page block number */
698 int skip; /* index of entry of insertion */
699 int nextindex; /* next available entry index of p */
700 struct btframe *parent; /* parent page entry on traverse stack */
704 int nsplit; /* number of pages split */
705 struct pxdlist pxdlist;
708 struct xtlock *xtlck;
711 sp = XT_PAGE(ip, smp);
713 /* is inode xtree root extension/inline EA area free ? */
714 if ((sp->header.flag & BT_ROOT) && (!S_ISDIR(ip->i_mode)) &&
715 (le16_to_cpu(sp->header.maxentry) < XTROOTMAXSLOT) &&
716 (JFS_IP(ip)->mode2 & INLINEEA)) {
717 sp->header.maxentry = cpu_to_le16(XTROOTMAXSLOT);
718 JFS_IP(ip)->mode2 &= ~INLINEEA;
720 BT_MARK_DIRTY(smp, ip);
722 * acquire a transaction lock on the leaf page;
724 * action: xad insertion/extension;
727 /* if insert into middle, shift right remaining entries. */
729 nextindex = le16_to_cpu(sp->header.nextindex);
730 if (skip < nextindex)
731 memmove(&sp->xad[skip + 1], &sp->xad[skip],
732 (nextindex - skip) * sizeof(xad_t));
734 /* insert the new entry: mark the entry NEW */
735 xad = &sp->xad[skip];
736 XT_PUTENTRY(xad, split->flag, split->off, split->len,
739 /* advance next available entry index */
740 le16_add_cpu(&sp->header.nextindex, 1);
742 /* Don't log it if there are no links to the file */
743 if (!test_cflag(COMMIT_Nolink, ip)) {
744 tlck = txLock(tid, ip, smp, tlckXTREE | tlckGROW);
745 xtlck = (struct xtlock *) & tlck->lock;
746 xtlck->lwm.offset = (xtlck->lwm.offset) ?
747 min(skip, (int)xtlck->lwm.offset) : skip;
749 le16_to_cpu(sp->header.nextindex) -
757 * allocate new index blocks to cover index page split(s)
761 if (split->pxdlist == NULL) {
762 nsplit = btstack->nsplit;
763 split->pxdlist = &pxdlist;
764 pxdlist.maxnpxd = pxdlist.npxd = 0;
765 pxd = &pxdlist.pxd[0];
766 xlen = JFS_SBI(ip->i_sb)->nbperpage;
767 for (; nsplit > 0; nsplit--, pxd++) {
768 if ((rc = dbAlloc(ip, (s64) 0, (s64) xlen, &xaddr))
770 PXDaddress(pxd, xaddr);
771 PXDlength(pxd, xlen);
778 /* undo allocation */
786 * Split leaf page <sp> into <sp> and a new right page <rp>.
788 * The split routines insert the new entry into the leaf page,
789 * and acquire txLock as appropriate.
790 * return <rp> pinned and its block number <rpbn>.
792 rc = (sp->header.flag & BT_ROOT) ?
793 xtSplitRoot(tid, ip, split, &rmp) :
794 xtSplitPage(tid, ip, split, &rmp, &rbn);
801 * propagate up the router entry for the leaf page just split
803 * insert a router entry for the new page into the parent page,
804 * propagate the insert/split up the tree by walking back the stack
805 * of (bn of parent page, index of child page entry in parent page)
806 * that were traversed during the search for the page that split.
808 * the propagation of insert/split up the tree stops if the root
809 * splits or the page inserted into doesn't have to split to hold
812 * the parent entry for the split page remains the same, and
813 * a new entry is inserted at its right with the first key and
814 * block number of the new right page.
816 * There are a maximum of 3 pages pinned at any time:
817 * right child, left parent and right parent (when the parent splits)
818 * to keep the child page pinned while working on the parent.
819 * make sure that all pins are released at exit.
821 while ((parent = BT_POP(btstack)) != NULL) {
822 /* parent page specified by stack frame <parent> */
824 /* keep current child pages <rcp> pinned */
827 rcp = XT_PAGE(ip, rcmp);
830 * insert router entry in parent for new right child page <rp>
832 /* get/pin the parent page <sp> */
833 XT_GETPAGE(ip, parent->bn, smp, PSIZE, sp, rc);
840 * The new key entry goes ONE AFTER the index of parent entry,
841 * because the split was to the right.
843 skip = parent->index + 1;
846 * split or shift right remaining entries of the parent page
848 nextindex = le16_to_cpu(sp->header.nextindex);
850 * parent page is full - split the parent page
852 if (nextindex == le16_to_cpu(sp->header.maxentry)) {
853 /* init for parent page split */
855 split->index = skip; /* index at insert */
856 split->flag = XAD_NEW;
857 split->off = offsetXAD(&rcp->xad[XTENTRYSTART]);
858 split->len = JFS_SBI(ip->i_sb)->nbperpage;
861 /* unpin previous right child page */
864 /* The split routines insert the new entry,
865 * and acquire txLock as appropriate.
866 * return <rp> pinned and its block number <rpbn>.
868 rc = (sp->header.flag & BT_ROOT) ?
869 xtSplitRoot(tid, ip, split, &rmp) :
870 xtSplitPage(tid, ip, split, &rmp, &rbn);
877 /* keep new child page <rp> pinned */
880 * parent page is not full - insert in parent page
884 * insert router entry in parent for the right child
885 * page from the first entry of the right child page:
888 * acquire a transaction lock on the parent page;
890 * action: router xad insertion;
892 BT_MARK_DIRTY(smp, ip);
895 * if insert into middle, shift right remaining entries
897 if (skip < nextindex)
898 memmove(&sp->xad[skip + 1], &sp->xad[skip],
900 skip) << L2XTSLOTSIZE);
902 /* insert the router entry */
903 xad = &sp->xad[skip];
904 XT_PUTENTRY(xad, XAD_NEW,
905 offsetXAD(&rcp->xad[XTENTRYSTART]),
906 JFS_SBI(ip->i_sb)->nbperpage, rcbn);
908 /* advance next available entry index. */
909 le16_add_cpu(&sp->header.nextindex, 1);
911 /* Don't log it if there are no links to the file */
912 if (!test_cflag(COMMIT_Nolink, ip)) {
913 tlck = txLock(tid, ip, smp,
914 tlckXTREE | tlckGROW);
915 xtlck = (struct xtlock *) & tlck->lock;
916 xtlck->lwm.offset = (xtlck->lwm.offset) ?
917 min(skip, (int)xtlck->lwm.offset) : skip;
919 le16_to_cpu(sp->header.nextindex) -
923 /* unpin parent page */
926 /* exit propagate up */
931 /* unpin current right page */
942 * split a full non-root page into
943 * original/split/left page and new right page
944 * i.e., the original/split page remains as left page.
949 * struct xtsplit *split,
950 * struct metapage **rmpp,
954 * Pointer to page in which to insert or NULL on error.
957 xtSplitPage(tid_t tid, struct inode *ip,
958 struct xtsplit * split, struct metapage ** rmpp, s64 * rbnp)
961 struct metapage *smp;
963 struct metapage *rmp;
964 xtpage_t *rp; /* new right page allocated */
965 s64 rbn; /* new right page block number */
969 int skip, maxentry, middle, righthalf, n;
971 struct pxdlist *pxdlist;
974 struct xtlock *sxtlck = NULL, *rxtlck = NULL;
975 int quota_allocation = 0;
978 sp = XT_PAGE(ip, smp);
980 INCREMENT(xtStat.split);
982 pxdlist = split->pxdlist;
983 pxd = &pxdlist->pxd[pxdlist->npxd];
985 rbn = addressPXD(pxd);
987 /* Allocate blocks to quota. */
988 rc = dquot_alloc_block(ip, lengthPXD(pxd));
992 quota_allocation += lengthPXD(pxd);
995 * allocate the new right page for the split
997 rmp = get_metapage(ip, rbn, PSIZE, 1);
1003 jfs_info("xtSplitPage: ip:0x%p smp:0x%p rmp:0x%p", ip, smp, rmp);
1005 BT_MARK_DIRTY(rmp, ip);
1010 rp = (xtpage_t *) rmp->data;
1011 rp->header.self = *pxd;
1012 rp->header.flag = sp->header.flag & BT_TYPE;
1013 rp->header.maxentry = sp->header.maxentry; /* little-endian */
1014 rp->header.nextindex = cpu_to_le16(XTENTRYSTART);
1016 BT_MARK_DIRTY(smp, ip);
1017 /* Don't log it if there are no links to the file */
1018 if (!test_cflag(COMMIT_Nolink, ip)) {
1020 * acquire a transaction lock on the new right page;
1022 tlck = txLock(tid, ip, rmp, tlckXTREE | tlckNEW);
1023 rxtlck = (struct xtlock *) & tlck->lock;
1024 rxtlck->lwm.offset = XTENTRYSTART;
1026 * acquire a transaction lock on the split page
1028 tlck = txLock(tid, ip, smp, tlckXTREE | tlckGROW);
1029 sxtlck = (struct xtlock *) & tlck->lock;
1033 * initialize/update sibling pointers of <sp> and <rp>
1035 nextbn = le64_to_cpu(sp->header.next);
1036 rp->header.next = cpu_to_le64(nextbn);
1037 rp->header.prev = cpu_to_le64(addressPXD(&sp->header.self));
1038 sp->header.next = cpu_to_le64(rbn);
1040 skip = split->index;
1043 * sequential append at tail (after last entry of last page)
1045 * if splitting the last page on a level because of appending
1046 * a entry to it (skip is maxentry), it's likely that the access is
1047 * sequential. adding an empty page on the side of the level is less
1048 * work and can push the fill factor much higher than normal.
1049 * if we're wrong it's no big deal - we will do the split the right
1051 * (it may look like it's equally easy to do a similar hack for
1052 * reverse sorted data, that is, split the tree left, but it's not.
1055 if (nextbn == 0 && skip == le16_to_cpu(sp->header.maxentry)) {
1057 * acquire a transaction lock on the new/right page;
1059 * action: xad insertion;
1061 /* insert entry at the first entry of the new right page */
1062 xad = &rp->xad[XTENTRYSTART];
1063 XT_PUTENTRY(xad, split->flag, split->off, split->len,
1066 rp->header.nextindex = cpu_to_le16(XTENTRYSTART + 1);
1068 if (!test_cflag(COMMIT_Nolink, ip)) {
1069 /* rxtlck->lwm.offset = XTENTRYSTART; */
1070 rxtlck->lwm.length = 1;
1076 jfs_info("xtSplitPage: sp:0x%p rp:0x%p", sp, rp);
1081 * non-sequential insert (at possibly middle page)
1085 * update previous pointer of old next/right page of <sp>
1088 XT_GETPAGE(ip, nextbn, mp, PSIZE, p, rc);
1094 BT_MARK_DIRTY(mp, ip);
1096 * acquire a transaction lock on the next page;
1098 * action:sibling pointer update;
1100 if (!test_cflag(COMMIT_Nolink, ip))
1101 tlck = txLock(tid, ip, mp, tlckXTREE | tlckRELINK);
1103 p->header.prev = cpu_to_le64(rbn);
1105 /* sibling page may have been updated previously, or
1106 * it may be updated later;
1113 * split the data between the split and new/right pages
1115 maxentry = le16_to_cpu(sp->header.maxentry);
1116 middle = maxentry >> 1;
1117 righthalf = maxentry - middle;
1120 * skip index in old split/left page - insert into left page:
1122 if (skip <= middle) {
1123 /* move right half of split page to the new right page */
1124 memmove(&rp->xad[XTENTRYSTART], &sp->xad[middle],
1125 righthalf << L2XTSLOTSIZE);
1127 /* shift right tail of left half to make room for new entry */
1129 memmove(&sp->xad[skip + 1], &sp->xad[skip],
1130 (middle - skip) << L2XTSLOTSIZE);
1132 /* insert new entry */
1133 xad = &sp->xad[skip];
1134 XT_PUTENTRY(xad, split->flag, split->off, split->len,
1137 /* update page header */
1138 sp->header.nextindex = cpu_to_le16(middle + 1);
1139 if (!test_cflag(COMMIT_Nolink, ip)) {
1140 sxtlck->lwm.offset = (sxtlck->lwm.offset) ?
1141 min(skip, (int)sxtlck->lwm.offset) : skip;
1144 rp->header.nextindex =
1145 cpu_to_le16(XTENTRYSTART + righthalf);
1148 * skip index in new right page - insert into right page:
1151 /* move left head of right half to right page */
1153 memmove(&rp->xad[XTENTRYSTART], &sp->xad[middle],
1156 /* insert new entry */
1159 XT_PUTENTRY(xad, split->flag, split->off, split->len,
1162 /* move right tail of right half to right page */
1163 if (skip < maxentry)
1164 memmove(&rp->xad[n + 1], &sp->xad[skip],
1165 (maxentry - skip) << L2XTSLOTSIZE);
1167 /* update page header */
1168 sp->header.nextindex = cpu_to_le16(middle);
1169 if (!test_cflag(COMMIT_Nolink, ip)) {
1170 sxtlck->lwm.offset = (sxtlck->lwm.offset) ?
1171 min(middle, (int)sxtlck->lwm.offset) : middle;
1174 rp->header.nextindex = cpu_to_le16(XTENTRYSTART +
1178 if (!test_cflag(COMMIT_Nolink, ip)) {
1179 sxtlck->lwm.length = le16_to_cpu(sp->header.nextindex) -
1182 /* rxtlck->lwm.offset = XTENTRYSTART; */
1183 rxtlck->lwm.length = le16_to_cpu(rp->header.nextindex) -
1190 jfs_info("xtSplitPage: sp:0x%p rp:0x%p", sp, rp);
1195 /* Rollback quota allocation. */
1196 if (quota_allocation)
1197 dquot_free_block(ip, quota_allocation);
1207 * split the full root page into original/root/split page and new
1209 * i.e., root remains fixed in tree anchor (inode) and the root is
1210 * copied to a single new right child page since root page <<
1211 * non-root page, and the split root page contains a single entry
1212 * for the new right child page.
1217 * struct xtsplit *split,
1218 * struct metapage **rmpp)
1221 * Pointer to page in which to insert or NULL on error.
1224 xtSplitRoot(tid_t tid,
1225 struct inode *ip, struct xtsplit * split, struct metapage ** rmpp)
1228 struct metapage *rmp;
1231 int skip, nextindex;
1234 struct pxdlist *pxdlist;
1236 struct xtlock *xtlck;
1239 sp = &JFS_IP(ip)->i_xtroot;
1241 INCREMENT(xtStat.split);
1244 * allocate a single (right) child page
1246 pxdlist = split->pxdlist;
1247 pxd = &pxdlist->pxd[pxdlist->npxd];
1249 rbn = addressPXD(pxd);
1250 rmp = get_metapage(ip, rbn, PSIZE, 1);
1254 /* Allocate blocks to quota. */
1255 rc = dquot_alloc_block(ip, lengthPXD(pxd));
1257 release_metapage(rmp);
1261 jfs_info("xtSplitRoot: ip:0x%p rmp:0x%p", ip, rmp);
1264 * acquire a transaction lock on the new right page;
1268 BT_MARK_DIRTY(rmp, ip);
1270 rp = (xtpage_t *) rmp->data;
1272 (sp->header.flag & BT_LEAF) ? BT_LEAF : BT_INTERNAL;
1273 rp->header.self = *pxd;
1274 rp->header.nextindex = cpu_to_le16(XTENTRYSTART);
1275 rp->header.maxentry = cpu_to_le16(PSIZE >> L2XTSLOTSIZE);
1277 /* initialize sibling pointers */
1278 rp->header.next = 0;
1279 rp->header.prev = 0;
1282 * copy the in-line root page into new right page extent
1284 nextindex = le16_to_cpu(sp->header.maxentry);
1285 memmove(&rp->xad[XTENTRYSTART], &sp->xad[XTENTRYSTART],
1286 (nextindex - XTENTRYSTART) << L2XTSLOTSIZE);
1289 * insert the new entry into the new right/child page
1290 * (skip index in the new right page will not change)
1292 skip = split->index;
1293 /* if insert into middle, shift right remaining entries */
1294 if (skip != nextindex)
1295 memmove(&rp->xad[skip + 1], &rp->xad[skip],
1296 (nextindex - skip) * sizeof(xad_t));
1298 xad = &rp->xad[skip];
1299 XT_PUTENTRY(xad, split->flag, split->off, split->len, split->addr);
1301 /* update page header */
1302 rp->header.nextindex = cpu_to_le16(nextindex + 1);
1304 if (!test_cflag(COMMIT_Nolink, ip)) {
1305 tlck = txLock(tid, ip, rmp, tlckXTREE | tlckNEW);
1306 xtlck = (struct xtlock *) & tlck->lock;
1307 xtlck->lwm.offset = XTENTRYSTART;
1308 xtlck->lwm.length = le16_to_cpu(rp->header.nextindex) -
1315 * init root with the single entry for the new right page
1316 * set the 1st entry offset to 0, which force the left-most key
1317 * at any level of the tree to be less than any search key.
1320 * acquire a transaction lock on the root page (in-memory inode);
1322 * action: root split;
1324 BT_MARK_DIRTY(split->mp, ip);
1326 xad = &sp->xad[XTENTRYSTART];
1327 XT_PUTENTRY(xad, XAD_NEW, 0, JFS_SBI(ip->i_sb)->nbperpage, rbn);
1329 /* update page header of root */
1330 sp->header.flag &= ~BT_LEAF;
1331 sp->header.flag |= BT_INTERNAL;
1333 sp->header.nextindex = cpu_to_le16(XTENTRYSTART + 1);
1335 if (!test_cflag(COMMIT_Nolink, ip)) {
1336 tlck = txLock(tid, ip, split->mp, tlckXTREE | tlckGROW);
1337 xtlck = (struct xtlock *) & tlck->lock;
1338 xtlck->lwm.offset = XTENTRYSTART;
1339 xtlck->lwm.length = 1;
1344 jfs_info("xtSplitRoot: sp:0x%p rp:0x%p", sp, rp);
1352 * function: extend in-place;
1354 * note: existing extent may or may not have been committed.
1355 * caller is responsible for pager buffer cache update, and
1356 * working block allocation map update;
1357 * update pmap: alloc whole extended extent;
1359 int xtExtend(tid_t tid, /* transaction id */
1360 struct inode *ip, s64 xoff, /* delta extent offset */
1361 s32 xlen, /* delta extent length */
1366 struct metapage *mp; /* meta-page buffer */
1367 xtpage_t *p; /* base B+-tree index page */
1369 int index, nextindex, len;
1370 struct btstack btstack; /* traverse stack */
1371 struct xtsplit split; /* split information */
1375 struct xtlock *xtlck = NULL;
1377 jfs_info("xtExtend: nxoff:0x%lx nxlen:0x%x", (ulong) xoff, xlen);
1379 /* there must exist extent to be extended */
1380 if ((rc = xtSearch(ip, xoff - 1, NULL, &cmp, &btstack, XT_INSERT)))
1383 /* retrieve search result */
1384 XT_GETSEARCH(ip, btstack.top, bn, mp, p, index);
1388 jfs_error(ip->i_sb, "xtExtend: xtSearch did not find extent");
1392 /* extension must be contiguous */
1393 xad = &p->xad[index];
1394 if ((offsetXAD(xad) + lengthXAD(xad)) != xoff) {
1396 jfs_error(ip->i_sb, "xtExtend: extension is not contiguous");
1401 * acquire a transaction lock on the leaf page;
1403 * action: xad insertion/extension;
1405 BT_MARK_DIRTY(mp, ip);
1406 if (!test_cflag(COMMIT_Nolink, ip)) {
1407 tlck = txLock(tid, ip, mp, tlckXTREE | tlckGROW);
1408 xtlck = (struct xtlock *) & tlck->lock;
1411 /* extend will overflow extent ? */
1412 xlen = lengthXAD(xad) + xlen;
1413 if ((len = xlen - MAXXLEN) <= 0)
1417 * extent overflow: insert entry for new extent
1420 xoff = offsetXAD(xad) + MAXXLEN;
1421 xaddr = addressXAD(xad) + MAXXLEN;
1422 nextindex = le16_to_cpu(p->header.nextindex);
1425 * if the leaf page is full, insert the new entry and
1426 * propagate up the router entry for the new page from split
1428 * The xtSplitUp() will insert the entry and unpin the leaf page.
1430 if (nextindex == le16_to_cpu(p->header.maxentry)) {
1431 /* xtSpliUp() unpins leaf pages */
1433 split.index = index + 1;
1434 split.flag = XAD_NEW;
1435 split.off = xoff; /* split offset */
1438 split.pxdlist = NULL;
1439 if ((rc = xtSplitUp(tid, ip, &split, &btstack)))
1442 /* get back old page */
1443 XT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
1447 * if leaf root has been split, original root has been
1448 * copied to new child page, i.e., original entry now
1449 * resides on the new child page;
1451 if (p->header.flag & BT_INTERNAL) {
1452 ASSERT(p->header.nextindex ==
1453 cpu_to_le16(XTENTRYSTART + 1));
1454 xad = &p->xad[XTENTRYSTART];
1455 bn = addressXAD(xad);
1458 /* get new child page */
1459 XT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
1463 BT_MARK_DIRTY(mp, ip);
1464 if (!test_cflag(COMMIT_Nolink, ip)) {
1465 tlck = txLock(tid, ip, mp, tlckXTREE|tlckGROW);
1466 xtlck = (struct xtlock *) & tlck->lock;
1471 * insert the new entry into the leaf page
1474 /* insert the new entry: mark the entry NEW */
1475 xad = &p->xad[index + 1];
1476 XT_PUTENTRY(xad, XAD_NEW, xoff, len, xaddr);
1478 /* advance next available entry index */
1479 le16_add_cpu(&p->header.nextindex, 1);
1482 /* get back old entry */
1483 xad = &p->xad[index];
1490 XADlength(xad, xlen);
1491 if (!(xad->flag & XAD_NEW))
1492 xad->flag |= XAD_EXTENDED;
1494 if (!test_cflag(COMMIT_Nolink, ip)) {
1496 (xtlck->lwm.offset) ? min(index,
1497 (int)xtlck->lwm.offset) : index;
1499 le16_to_cpu(p->header.nextindex) - xtlck->lwm.offset;
1502 /* unpin the leaf page */
1512 * function: split existing 'tail' extent
1513 * (split offset >= start offset of tail extent), and
1514 * relocate and extend the split tail half;
1516 * note: existing extent may or may not have been committed.
1517 * caller is responsible for pager buffer cache update, and
1518 * working block allocation map update;
1519 * update pmap: free old split tail extent, alloc new extent;
1521 int xtTailgate(tid_t tid, /* transaction id */
1522 struct inode *ip, s64 xoff, /* split/new extent offset */
1523 s32 xlen, /* new extent length */
1524 s64 xaddr, /* new extent address */
1529 struct metapage *mp; /* meta-page buffer */
1530 xtpage_t *p; /* base B+-tree index page */
1532 int index, nextindex, llen, rlen;
1533 struct btstack btstack; /* traverse stack */
1534 struct xtsplit split; /* split information */
1537 struct xtlock *xtlck = 0;
1538 struct tlock *mtlck;
1539 struct maplock *pxdlock;
1542 printf("xtTailgate: nxoff:0x%lx nxlen:0x%x nxaddr:0x%lx\n",
1543 (ulong)xoff, xlen, (ulong)xaddr);
1546 /* there must exist extent to be tailgated */
1547 if ((rc = xtSearch(ip, xoff, NULL, &cmp, &btstack, XT_INSERT)))
1550 /* retrieve search result */
1551 XT_GETSEARCH(ip, btstack.top, bn, mp, p, index);
1555 jfs_error(ip->i_sb, "xtTailgate: couldn't find extent");
1559 /* entry found must be last entry */
1560 nextindex = le16_to_cpu(p->header.nextindex);
1561 if (index != nextindex - 1) {
1564 "xtTailgate: the entry found is not the last entry");
1568 BT_MARK_DIRTY(mp, ip);
1570 * acquire tlock of the leaf page containing original entry
1572 if (!test_cflag(COMMIT_Nolink, ip)) {
1573 tlck = txLock(tid, ip, mp, tlckXTREE | tlckGROW);
1574 xtlck = (struct xtlock *) & tlck->lock;
1577 /* completely replace extent ? */
1578 xad = &p->xad[index];
1580 printf("xtTailgate: xoff:0x%lx xlen:0x%x xaddr:0x%lx\n",
1581 (ulong)offsetXAD(xad), lengthXAD(xad), (ulong)addressXAD(xad));
1583 if ((llen = xoff - offsetXAD(xad)) == 0)
1587 * partially replace extent: insert entry for new extent
1591 * if the leaf page is full, insert the new entry and
1592 * propagate up the router entry for the new page from split
1594 * The xtSplitUp() will insert the entry and unpin the leaf page.
1596 if (nextindex == le16_to_cpu(p->header.maxentry)) {
1597 /* xtSpliUp() unpins leaf pages */
1599 split.index = index + 1;
1600 split.flag = XAD_NEW;
1601 split.off = xoff; /* split offset */
1604 split.pxdlist = NULL;
1605 if ((rc = xtSplitUp(tid, ip, &split, &btstack)))
1608 /* get back old page */
1609 XT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
1613 * if leaf root has been split, original root has been
1614 * copied to new child page, i.e., original entry now
1615 * resides on the new child page;
1617 if (p->header.flag & BT_INTERNAL) {
1618 ASSERT(p->header.nextindex ==
1619 cpu_to_le16(XTENTRYSTART + 1));
1620 xad = &p->xad[XTENTRYSTART];
1621 bn = addressXAD(xad);
1624 /* get new child page */
1625 XT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
1629 BT_MARK_DIRTY(mp, ip);
1630 if (!test_cflag(COMMIT_Nolink, ip)) {
1631 tlck = txLock(tid, ip, mp, tlckXTREE|tlckGROW);
1632 xtlck = (struct xtlock *) & tlck->lock;
1637 * insert the new entry into the leaf page
1640 /* insert the new entry: mark the entry NEW */
1641 xad = &p->xad[index + 1];
1642 XT_PUTENTRY(xad, XAD_NEW, xoff, xlen, xaddr);
1644 /* advance next available entry index */
1645 le16_add_cpu(&p->header.nextindex, 1);
1648 /* get back old XAD */
1649 xad = &p->xad[index];
1652 * truncate/relocate old extent at split offset
1655 /* update dmap for old/committed/truncated extent */
1656 rlen = lengthXAD(xad) - llen;
1657 if (!(xad->flag & XAD_NEW)) {
1658 /* free from PWMAP at commit */
1659 if (!test_cflag(COMMIT_Nolink, ip)) {
1660 mtlck = txMaplock(tid, ip, tlckMAP);
1661 pxdlock = (struct maplock *) & mtlck->lock;
1662 pxdlock->flag = mlckFREEPXD;
1663 PXDaddress(&pxdlock->pxd, addressXAD(xad) + llen);
1664 PXDlength(&pxdlock->pxd, rlen);
1668 /* free from WMAP */
1669 dbFree(ip, addressXAD(xad) + llen, (s64) rlen);
1673 XADlength(xad, llen);
1676 XT_PUTENTRY(xad, XAD_NEW, xoff, xlen, xaddr);
1678 if (!test_cflag(COMMIT_Nolink, ip)) {
1679 xtlck->lwm.offset = (xtlck->lwm.offset) ?
1680 min(index, (int)xtlck->lwm.offset) : index;
1681 xtlck->lwm.length = le16_to_cpu(p->header.nextindex) -
1685 /* unpin the leaf page */
1690 #endif /* _NOTYET */
1695 * function: update XAD;
1697 * update extent for allocated_but_not_recorded or
1698 * compressed extent;
1702 * logical extent of the specified XAD must be completely
1703 * contained by an existing XAD;
1705 int xtUpdate(tid_t tid, struct inode *ip, xad_t * nxad)
1709 struct metapage *mp; /* meta-page buffer */
1710 xtpage_t *p; /* base B+-tree index page */
1712 int index0, index, newindex, nextindex;
1713 struct btstack btstack; /* traverse stack */
1714 struct xtsplit split; /* split information */
1715 xad_t *xad, *lxad, *rxad;
1718 int nxlen, xlen, lxlen, rxlen;
1721 struct xtlock *xtlck = NULL;
1724 /* there must exist extent to be tailgated */
1725 nxoff = offsetXAD(nxad);
1726 nxlen = lengthXAD(nxad);
1727 nxaddr = addressXAD(nxad);
1729 if ((rc = xtSearch(ip, nxoff, NULL, &cmp, &btstack, XT_INSERT)))
1732 /* retrieve search result */
1733 XT_GETSEARCH(ip, btstack.top, bn, mp, p, index0);
1737 jfs_error(ip->i_sb, "xtUpdate: Could not find extent");
1741 BT_MARK_DIRTY(mp, ip);
1743 * acquire tlock of the leaf page containing original entry
1745 if (!test_cflag(COMMIT_Nolink, ip)) {
1746 tlck = txLock(tid, ip, mp, tlckXTREE | tlckGROW);
1747 xtlck = (struct xtlock *) & tlck->lock;
1750 xad = &p->xad[index0];
1752 xoff = offsetXAD(xad);
1753 xlen = lengthXAD(xad);
1754 xaddr = addressXAD(xad);
1756 /* nXAD must be completely contained within XAD */
1757 if ((xoff > nxoff) ||
1758 (nxoff + nxlen > xoff + xlen)) {
1761 "xtUpdate: nXAD in not completely contained within XAD");
1766 newindex = index + 1;
1767 nextindex = le16_to_cpu(p->header.nextindex);
1769 #ifdef _JFS_WIP_NOCOALESCE
1774 * replace XAD with nXAD
1776 replace: /* (nxoff == xoff) */
1777 if (nxlen == xlen) {
1778 /* replace XAD with nXAD:recorded */
1780 xad->flag = xflag & ~XAD_NOTRECORDED;
1783 } else /* (nxlen < xlen) */
1785 #endif /* _JFS_WIP_NOCOALESCE */
1787 /* #ifdef _JFS_WIP_COALESCE */
1792 * coalesce with left XAD
1794 //coalesceLeft: /* (xoff == nxoff) */
1795 /* is XAD first entry of page ? */
1796 if (index == XTENTRYSTART)
1799 /* is nXAD logically and physically contiguous with lXAD ? */
1800 lxad = &p->xad[index - 1];
1801 lxlen = lengthXAD(lxad);
1802 if (!(lxad->flag & XAD_NOTRECORDED) &&
1803 (nxoff == offsetXAD(lxad) + lxlen) &&
1804 (nxaddr == addressXAD(lxad) + lxlen) &&
1805 (lxlen + nxlen < MAXXLEN)) {
1806 /* extend right lXAD */
1808 XADlength(lxad, lxlen + nxlen);
1810 /* If we just merged two extents together, need to make sure the
1811 * right extent gets logged. If the left one is marked XAD_NEW,
1812 * then we know it will be logged. Otherwise, mark as
1815 if (!(lxad->flag & XAD_NEW))
1816 lxad->flag |= XAD_EXTENDED;
1820 XADoffset(xad, xoff + nxlen);
1821 XADlength(xad, xlen - nxlen);
1822 XADaddress(xad, xaddr + nxlen);
1824 } else { /* (xlen == nxlen) */
1827 if (index < nextindex - 1)
1828 memmove(&p->xad[index], &p->xad[index + 1],
1829 (nextindex - index -
1830 1) << L2XTSLOTSIZE);
1832 p->header.nextindex =
1833 cpu_to_le16(le16_to_cpu(p->header.nextindex) -
1837 newindex = index + 1;
1838 nextindex = le16_to_cpu(p->header.nextindex);
1839 xoff = nxoff = offsetXAD(lxad);
1840 xlen = nxlen = lxlen + nxlen;
1841 xaddr = nxaddr = addressXAD(lxad);
1847 * replace XAD with nXAD
1849 replace: /* (nxoff == xoff) */
1850 if (nxlen == xlen) {
1851 /* replace XAD with nXAD:recorded */
1853 xad->flag = xflag & ~XAD_NOTRECORDED;
1856 } else /* (nxlen < xlen) */
1860 * coalesce with right XAD
1862 coalesceRight: /* (xoff <= nxoff) */
1863 /* is XAD last entry of page ? */
1864 if (newindex == nextindex) {
1870 /* is nXAD logically and physically contiguous with rXAD ? */
1871 rxad = &p->xad[index + 1];
1872 rxlen = lengthXAD(rxad);
1873 if (!(rxad->flag & XAD_NOTRECORDED) &&
1874 (nxoff + nxlen == offsetXAD(rxad)) &&
1875 (nxaddr + nxlen == addressXAD(rxad)) &&
1876 (rxlen + nxlen < MAXXLEN)) {
1877 /* extend left rXAD */
1878 XADoffset(rxad, nxoff);
1879 XADlength(rxad, rxlen + nxlen);
1880 XADaddress(rxad, nxaddr);
1882 /* If we just merged two extents together, need to make sure
1883 * the left extent gets logged. If the right one is marked
1884 * XAD_NEW, then we know it will be logged. Otherwise, mark as
1887 if (!(rxad->flag & XAD_NEW))
1888 rxad->flag |= XAD_EXTENDED;
1892 XADlength(xad, xlen - nxlen);
1893 else { /* (xlen == nxlen) */
1896 memmove(&p->xad[index], &p->xad[index + 1],
1897 (nextindex - index - 1) << L2XTSLOTSIZE);
1899 p->header.nextindex =
1900 cpu_to_le16(le16_to_cpu(p->header.nextindex) -
1905 } else if (xoff == nxoff)
1908 if (xoff >= nxoff) {
1910 jfs_error(ip->i_sb, "xtUpdate: xoff >= nxoff");
1913 /* #endif _JFS_WIP_COALESCE */
1916 * split XAD into (lXAD, nXAD):
1919 * --|----------XAD----------|--
1922 updateRight: /* (xoff < nxoff) */
1923 /* truncate old XAD as lXAD:not_recorded */
1924 xad = &p->xad[index];
1925 XADlength(xad, nxoff - xoff);
1927 /* insert nXAD:recorded */
1928 if (nextindex == le16_to_cpu(p->header.maxentry)) {
1930 /* xtSpliUp() unpins leaf pages */
1932 split.index = newindex;
1933 split.flag = xflag & ~XAD_NOTRECORDED;
1936 split.addr = nxaddr;
1937 split.pxdlist = NULL;
1938 if ((rc = xtSplitUp(tid, ip, &split, &btstack)))
1941 /* get back old page */
1942 XT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
1946 * if leaf root has been split, original root has been
1947 * copied to new child page, i.e., original entry now
1948 * resides on the new child page;
1950 if (p->header.flag & BT_INTERNAL) {
1951 ASSERT(p->header.nextindex ==
1952 cpu_to_le16(XTENTRYSTART + 1));
1953 xad = &p->xad[XTENTRYSTART];
1954 bn = addressXAD(xad);
1957 /* get new child page */
1958 XT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
1962 BT_MARK_DIRTY(mp, ip);
1963 if (!test_cflag(COMMIT_Nolink, ip)) {
1964 tlck = txLock(tid, ip, mp, tlckXTREE|tlckGROW);
1965 xtlck = (struct xtlock *) & tlck->lock;
1968 /* is nXAD on new page ? */
1970 (le16_to_cpu(p->header.maxentry) >> 1)) {
1973 le16_to_cpu(p->header.nextindex) +
1979 /* if insert into middle, shift right remaining entries */
1980 if (newindex < nextindex)
1981 memmove(&p->xad[newindex + 1], &p->xad[newindex],
1982 (nextindex - newindex) << L2XTSLOTSIZE);
1984 /* insert the entry */
1985 xad = &p->xad[newindex];
1987 xad->flag = xflag & ~XAD_NOTRECORDED;
1989 /* advance next available entry index. */
1990 p->header.nextindex =
1991 cpu_to_le16(le16_to_cpu(p->header.nextindex) + 1);
1995 * does nXAD force 3-way split ?
1998 * --|----------XAD-------------|--
1999 * |-lXAD-| |-rXAD -|
2001 if (nxoff + nxlen == xoff + xlen)
2004 /* reorient nXAD as XAD for further split XAD into (nXAD, rXAD) */
2006 /* close out old page */
2007 if (!test_cflag(COMMIT_Nolink, ip)) {
2008 xtlck->lwm.offset = (xtlck->lwm.offset) ?
2009 min(index0, (int)xtlck->lwm.offset) : index0;
2011 le16_to_cpu(p->header.nextindex) -
2015 bn = le64_to_cpu(p->header.next);
2018 /* get new right page */
2019 XT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
2023 BT_MARK_DIRTY(mp, ip);
2024 if (!test_cflag(COMMIT_Nolink, ip)) {
2025 tlck = txLock(tid, ip, mp, tlckXTREE | tlckGROW);
2026 xtlck = (struct xtlock *) & tlck->lock;
2029 index0 = index = newindex;
2033 newindex = index + 1;
2034 nextindex = le16_to_cpu(p->header.nextindex);
2035 xlen = xlen - (nxoff - xoff);
2039 /* recompute split pages */
2040 if (nextindex == le16_to_cpu(p->header.maxentry)) {
2043 if ((rc = xtSearch(ip, nxoff, NULL, &cmp, &btstack, XT_INSERT)))
2046 /* retrieve search result */
2047 XT_GETSEARCH(ip, btstack.top, bn, mp, p, index0);
2051 jfs_error(ip->i_sb, "xtUpdate: xtSearch failed");
2055 if (index0 != index) {
2058 "xtUpdate: unexpected value of index");
2064 * split XAD into (nXAD, rXAD)
2067 * --|----------XAD----------|--
2070 updateLeft: /* (nxoff == xoff) && (nxlen < xlen) */
2071 /* update old XAD with nXAD:recorded */
2072 xad = &p->xad[index];
2074 xad->flag = xflag & ~XAD_NOTRECORDED;
2076 /* insert rXAD:not_recorded */
2077 xoff = xoff + nxlen;
2078 xlen = xlen - nxlen;
2079 xaddr = xaddr + nxlen;
2080 if (nextindex == le16_to_cpu(p->header.maxentry)) {
2082 printf("xtUpdate.updateLeft.split p:0x%p\n", p);
2084 /* xtSpliUp() unpins leaf pages */
2086 split.index = newindex;
2091 split.pxdlist = NULL;
2092 if ((rc = xtSplitUp(tid, ip, &split, &btstack)))
2095 /* get back old page */
2096 XT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
2101 * if leaf root has been split, original root has been
2102 * copied to new child page, i.e., original entry now
2103 * resides on the new child page;
2105 if (p->header.flag & BT_INTERNAL) {
2106 ASSERT(p->header.nextindex ==
2107 cpu_to_le16(XTENTRYSTART + 1));
2108 xad = &p->xad[XTENTRYSTART];
2109 bn = addressXAD(xad);
2112 /* get new child page */
2113 XT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
2117 BT_MARK_DIRTY(mp, ip);
2118 if (!test_cflag(COMMIT_Nolink, ip)) {
2119 tlck = txLock(tid, ip, mp, tlckXTREE|tlckGROW);
2120 xtlck = (struct xtlock *) & tlck->lock;
2124 /* if insert into middle, shift right remaining entries */
2125 if (newindex < nextindex)
2126 memmove(&p->xad[newindex + 1], &p->xad[newindex],
2127 (nextindex - newindex) << L2XTSLOTSIZE);
2129 /* insert the entry */
2130 xad = &p->xad[newindex];
2131 XT_PUTENTRY(xad, xflag, xoff, xlen, xaddr);
2133 /* advance next available entry index. */
2134 p->header.nextindex =
2135 cpu_to_le16(le16_to_cpu(p->header.nextindex) + 1);
2139 if (!test_cflag(COMMIT_Nolink, ip)) {
2140 xtlck->lwm.offset = (xtlck->lwm.offset) ?
2141 min(index0, (int)xtlck->lwm.offset) : index0;
2142 xtlck->lwm.length = le16_to_cpu(p->header.nextindex) -
2146 /* unpin the leaf page */
2156 * function: grow in append mode from contiguous region specified ;
2159 * tid - transaction id;
2161 * xflag - extent flag:
2162 * xoff - extent offset;
2163 * maxblocks - max extent length;
2164 * xlen - extent length (in/out);
2165 * xaddrp - extent address pointer (in/out):
2170 int xtAppend(tid_t tid, /* transaction id */
2171 struct inode *ip, int xflag, s64 xoff, s32 maxblocks,
2172 s32 * xlenp, /* (in/out) */
2173 s64 * xaddrp, /* (in/out) */
2177 struct metapage *mp; /* meta-page buffer */
2178 xtpage_t *p; /* base B+-tree index page */
2180 int index, nextindex;
2181 struct btstack btstack; /* traverse stack */
2182 struct xtsplit split; /* split information */
2186 struct xtlock *xtlck;
2187 int nsplit, nblocks, xlen;
2188 struct pxdlist pxdlist;
2194 jfs_info("xtAppend: xoff:0x%lx maxblocks:%d xlen:%d xaddr:0x%lx",
2195 (ulong) xoff, maxblocks, xlen, (ulong) xaddr);
2198 * search for the entry location at which to insert:
2200 * xtFastSearch() and xtSearch() both returns (leaf page
2201 * pinned, index at which to insert).
2202 * n.b. xtSearch() may return index of maxentry of
2205 if ((rc = xtSearch(ip, xoff, &next, &cmp, &btstack, XT_INSERT)))
2208 /* retrieve search result */
2209 XT_GETSEARCH(ip, btstack.top, bn, mp, p, index);
2217 xlen = min(xlen, (int)(next - xoff));
2220 * insert entry for new extent
2225 * if the leaf page is full, split the page and
2226 * propagate up the router entry for the new page from split
2228 * The xtSplitUp() will insert the entry and unpin the leaf page.
2230 nextindex = le16_to_cpu(p->header.nextindex);
2231 if (nextindex < le16_to_cpu(p->header.maxentry))
2235 * allocate new index blocks to cover index page split(s)
2237 nsplit = btstack.nsplit;
2238 split.pxdlist = &pxdlist;
2239 pxdlist.maxnpxd = pxdlist.npxd = 0;
2240 pxd = &pxdlist.pxd[0];
2241 nblocks = JFS_SBI(ip->i_sb)->nbperpage;
2242 for (; nsplit > 0; nsplit--, pxd++, xaddr += nblocks, maxblocks -= nblocks) {
2243 if ((rc = dbAllocBottomUp(ip, xaddr, (s64) nblocks)) == 0) {
2244 PXDaddress(pxd, xaddr);
2245 PXDlength(pxd, nblocks);
2252 /* undo allocation */
2257 xlen = min(xlen, maxblocks);
2260 * allocate data extent requested
2262 if ((rc = dbAllocBottomUp(ip, xaddr, (s64) xlen)))
2266 split.index = index;
2271 if ((rc = xtSplitUp(tid, ip, &split, &btstack))) {
2272 /* undo data extent allocation */
2273 dbFree(ip, *xaddrp, (s64) * xlenp);
2283 * insert the new entry into the leaf page
2287 * allocate data extent requested
2289 if ((rc = dbAllocBottomUp(ip, xaddr, (s64) xlen)))
2292 BT_MARK_DIRTY(mp, ip);
2294 * acquire a transaction lock on the leaf page;
2296 * action: xad insertion/extension;
2298 tlck = txLock(tid, ip, mp, tlckXTREE | tlckGROW);
2299 xtlck = (struct xtlock *) & tlck->lock;
2301 /* insert the new entry: mark the entry NEW */
2302 xad = &p->xad[index];
2303 XT_PUTENTRY(xad, xflag, xoff, xlen, xaddr);
2305 /* advance next available entry index */
2306 le16_add_cpu(&p->header.nextindex, 1);
2309 (xtlck->lwm.offset) ? min(index,(int) xtlck->lwm.offset) : index;
2310 xtlck->lwm.length = le16_to_cpu(p->header.nextindex) -
2317 /* unpin the leaf page */
2322 #ifdef _STILL_TO_PORT
2324 /* - TBD for defragmentaion/reorganization -
2329 * delete the entry with the specified key.
2331 * N.B.: whole extent of the entry is assumed to be deleted.
2336 * ENOENT: if the entry is not found.
2340 int xtDelete(tid_t tid, struct inode *ip, s64 xoff, s32 xlen, int flag)
2343 struct btstack btstack;
2346 struct metapage *mp;
2348 int index, nextindex;
2350 struct xtlock *xtlck;
2353 * find the matching entry; xtSearch() pins the page
2355 if ((rc = xtSearch(ip, xoff, NULL, &cmp, &btstack, 0)))
2358 XT_GETSEARCH(ip, btstack.top, bn, mp, p, index);
2360 /* unpin the leaf page */
2366 * delete the entry from the leaf page
2368 nextindex = le16_to_cpu(p->header.nextindex);
2369 le16_add_cpu(&p->header.nextindex, -1);
2372 * if the leaf page bocome empty, free the page
2374 if (p->header.nextindex == cpu_to_le16(XTENTRYSTART))
2375 return (xtDeleteUp(tid, ip, mp, p, &btstack));
2377 BT_MARK_DIRTY(mp, ip);
2379 * acquire a transaction lock on the leaf page;
2381 * action:xad deletion;
2383 tlck = txLock(tid, ip, mp, tlckXTREE);
2384 xtlck = (struct xtlock *) & tlck->lock;
2386 (xtlck->lwm.offset) ? min(index, xtlck->lwm.offset) : index;
2388 /* if delete from middle, shift left/compact the remaining entries */
2389 if (index < nextindex - 1)
2390 memmove(&p->xad[index], &p->xad[index + 1],
2391 (nextindex - index - 1) * sizeof(xad_t));
2399 /* - TBD for defragmentaion/reorganization -
2404 * free empty pages as propagating deletion up the tree
2411 xtDeleteUp(tid_t tid, struct inode *ip,
2412 struct metapage * fmp, xtpage_t * fp, struct btstack * btstack)
2415 struct metapage *mp;
2417 int index, nextindex;
2420 struct btframe *parent;
2422 struct xtlock *xtlck;
2425 * keep root leaf page which has become empty
2427 if (fp->header.flag & BT_ROOT) {
2428 /* keep the root page */
2429 fp->header.flag &= ~BT_INTERNAL;
2430 fp->header.flag |= BT_LEAF;
2431 fp->header.nextindex = cpu_to_le16(XTENTRYSTART);
2433 /* XT_PUTPAGE(fmp); */
2439 * free non-root leaf page
2441 if ((rc = xtRelink(tid, ip, fp))) {
2446 xaddr = addressPXD(&fp->header.self);
2447 xlen = lengthPXD(&fp->header.self);
2448 /* free the page extent */
2449 dbFree(ip, xaddr, (s64) xlen);
2451 /* free the buffer page */
2452 discard_metapage(fmp);
2455 * propagate page deletion up the index tree
2457 * If the delete from the parent page makes it empty,
2458 * continue all the way up the tree.
2459 * stop if the root page is reached (which is never deleted) or
2460 * if the entry deletion does not empty the page.
2462 while ((parent = BT_POP(btstack)) != NULL) {
2463 /* get/pin the parent page <sp> */
2464 XT_GETPAGE(ip, parent->bn, mp, PSIZE, p, rc);
2468 index = parent->index;
2470 /* delete the entry for the freed child page from parent.
2472 nextindex = le16_to_cpu(p->header.nextindex);
2475 * the parent has the single entry being deleted:
2476 * free the parent page which has become empty.
2478 if (nextindex == 1) {
2479 if (p->header.flag & BT_ROOT) {
2480 /* keep the root page */
2481 p->header.flag &= ~BT_INTERNAL;
2482 p->header.flag |= BT_LEAF;
2483 p->header.nextindex =
2484 cpu_to_le16(XTENTRYSTART);
2486 /* XT_PUTPAGE(mp); */
2490 /* free the parent page */
2491 if ((rc = xtRelink(tid, ip, p)))
2494 xaddr = addressPXD(&p->header.self);
2495 /* free the page extent */
2497 (s64) JFS_SBI(ip->i_sb)->nbperpage);
2499 /* unpin/free the buffer page */
2500 discard_metapage(mp);
2507 * the parent has other entries remaining:
2508 * delete the router entry from the parent page.
2511 BT_MARK_DIRTY(mp, ip);
2513 * acquire a transaction lock on the leaf page;
2515 * action:xad deletion;
2517 tlck = txLock(tid, ip, mp, tlckXTREE);
2518 xtlck = (struct xtlock *) & tlck->lock;
2520 (xtlck->lwm.offset) ? min(index,
2524 /* if delete from middle,
2525 * shift left/compact the remaining entries in the page
2527 if (index < nextindex - 1)
2528 memmove(&p->xad[index], &p->xad[index + 1],
2529 (nextindex - index -
2530 1) << L2XTSLOTSIZE);
2532 le16_add_cpu(&p->header.nextindex, -1);
2533 jfs_info("xtDeleteUp(entry): 0x%lx[%d]",
2534 (ulong) parent->bn, index);
2537 /* unpin the parent page */
2540 /* exit propagation up */
2549 * NAME: xtRelocate()
2551 * FUNCTION: relocate xtpage or data extent of regular file;
2552 * This function is mainly used by defragfs utility.
2554 * NOTE: This routine does not have the logic to handle
2555 * uncommitted allocated extent. The caller should call
2556 * txCommit() to commit all the allocation before call
2560 xtRelocate(tid_t tid, struct inode * ip, xad_t * oxad, /* old XAD */
2561 s64 nxaddr, /* new xaddr */
2563 { /* extent type: XTPAGE or DATAEXT */
2565 struct tblock *tblk;
2567 struct xtlock *xtlck;
2568 struct metapage *mp, *pmp, *lmp, *rmp; /* meta-page buffer */
2569 xtpage_t *p, *pp, *rp, *lp; /* base B+-tree index page */
2574 s64 oxaddr, sxaddr, dxaddr, nextbn, prevbn;
2576 s64 offset, nbytes, nbrd, pno;
2577 int nb, npages, nblks;
2581 struct pxd_lock *pxdlock;
2582 struct btstack btstack; /* traverse stack */
2584 xtype = xtype & EXTENT_TYPE;
2586 xoff = offsetXAD(oxad);
2587 oxaddr = addressXAD(oxad);
2588 xlen = lengthXAD(oxad);
2590 /* validate extent offset */
2591 offset = xoff << JFS_SBI(ip->i_sb)->l2bsize;
2592 if (offset >= ip->i_size)
2593 return -ESTALE; /* stale extent */
2595 jfs_info("xtRelocate: xtype:%d xoff:0x%lx xlen:0x%x xaddr:0x%lx:0x%lx",
2596 xtype, (ulong) xoff, xlen, (ulong) oxaddr, (ulong) nxaddr);
2599 * 1. get and validate the parent xtpage/xad entry
2600 * covering the source extent to be relocated;
2602 if (xtype == DATAEXT) {
2603 /* search in leaf entry */
2604 rc = xtSearch(ip, xoff, NULL, &cmp, &btstack, 0);
2608 /* retrieve search result */
2609 XT_GETSEARCH(ip, btstack.top, bn, pmp, pp, index);
2616 /* validate for exact match with a single entry */
2617 xad = &pp->xad[index];
2618 if (addressXAD(xad) != oxaddr || lengthXAD(xad) != xlen) {
2622 } else { /* (xtype == XTPAGE) */
2624 /* search in internal entry */
2625 rc = xtSearchNode(ip, oxad, &cmp, &btstack, 0);
2629 /* retrieve search result */
2630 XT_GETSEARCH(ip, btstack.top, bn, pmp, pp, index);
2637 /* xtSearchNode() validated for exact match with a single entry
2639 xad = &pp->xad[index];
2641 jfs_info("xtRelocate: parent xad entry validated.");
2644 * 2. relocate the extent
2646 if (xtype == DATAEXT) {
2647 /* if the extent is allocated-but-not-recorded
2648 * there is no real data to be moved in this extent,
2650 if (xad->flag & XAD_NOTRECORDED)
2653 /* release xtpage for cmRead()/xtLookup() */
2659 * copy target data pages to be relocated;
2661 * data extent must start at page boundary and
2662 * multiple of page size (except the last data extent);
2663 * read in each page of the source data extent into cbuf,
2664 * update the cbuf extent descriptor of the page to be
2665 * homeward bound to new dst data extent
2666 * copy the data from the old extent to new extent.
2667 * copy is essential for compressed files to avoid problems
2668 * that can arise if there was a change in compression
2670 * it is a good strategy because it may disrupt cache
2671 * policy to keep the pages in memory afterwards.
2673 offset = xoff << JFS_SBI(ip->i_sb)->l2bsize;
2674 assert((offset & CM_OFFSET) == 0);
2675 nbytes = xlen << JFS_SBI(ip->i_sb)->l2bsize;
2676 pno = offset >> CM_L2BSIZE;
2677 npages = (nbytes + (CM_BSIZE - 1)) >> CM_L2BSIZE;
2679 npages = ((offset + nbytes - 1) >> CM_L2BSIZE) -
2680 (offset >> CM_L2BSIZE) + 1;
2685 /* process the request one cache buffer at a time */
2686 for (nbrd = 0; nbrd < nbytes; nbrd += nb,
2687 offset += nb, pno++, npages--) {
2688 /* compute page size */
2689 nb = min(nbytes - nbrd, CM_BSIZE);
2691 /* get the cache buffer of the page */
2692 if (rc = cmRead(ip, offset, npages, &cp))
2695 assert(addressPXD(&cp->cm_pxd) == sxaddr);
2696 assert(!cp->cm_modified);
2698 /* bind buffer with the new extent address */
2699 nblks = nb >> JFS_IP(ip->i_sb)->l2bsize;
2700 cmSetXD(ip, cp, pno, dxaddr, nblks);
2702 /* release the cbuf, mark it as modified */
2709 /* get back parent page */
2710 if ((rc = xtSearch(ip, xoff, NULL, &cmp, &btstack, 0)))
2713 XT_GETSEARCH(ip, btstack.top, bn, pmp, pp, index);
2714 jfs_info("xtRelocate: target data extent relocated.");
2715 } else { /* (xtype == XTPAGE) */
2718 * read in the target xtpage from the source extent;
2720 XT_GETPAGE(ip, oxaddr, mp, PSIZE, p, rc);
2727 * read in sibling pages if any to update sibling pointers;
2730 if (p->header.next) {
2731 nextbn = le64_to_cpu(p->header.next);
2732 XT_GETPAGE(ip, nextbn, rmp, PSIZE, rp, rc);
2741 if (p->header.prev) {
2742 prevbn = le64_to_cpu(p->header.prev);
2743 XT_GETPAGE(ip, prevbn, lmp, PSIZE, lp, rc);
2753 /* at this point, all xtpages to be updated are in memory */
2756 * update sibling pointers of sibling xtpages if any;
2759 BT_MARK_DIRTY(lmp, ip);
2760 tlck = txLock(tid, ip, lmp, tlckXTREE | tlckRELINK);
2761 lp->header.next = cpu_to_le64(nxaddr);
2766 BT_MARK_DIRTY(rmp, ip);
2767 tlck = txLock(tid, ip, rmp, tlckXTREE | tlckRELINK);
2768 rp->header.prev = cpu_to_le64(nxaddr);
2773 * update the target xtpage to be relocated
2775 * update the self address of the target page
2776 * and write to destination extent;
2777 * redo image covers the whole xtpage since it is new page
2778 * to the destination extent;
2779 * update of bmap for the free of source extent
2780 * of the target xtpage itself:
2781 * update of bmap for the allocation of destination extent
2782 * of the target xtpage itself:
2783 * update of bmap for the extents covered by xad entries in
2784 * the target xtpage is not necessary since they are not
2786 * if not committed before this relocation,
2787 * target page may contain XAD_NEW entries which must
2788 * be scanned for bmap update (logredo() always
2789 * scan xtpage REDOPAGE image for bmap update);
2790 * if committed before this relocation (tlckRELOCATE),
2791 * scan may be skipped by commit() and logredo();
2793 BT_MARK_DIRTY(mp, ip);
2794 /* tlckNEW init xtlck->lwm.offset = XTENTRYSTART; */
2795 tlck = txLock(tid, ip, mp, tlckXTREE | tlckNEW);
2796 xtlck = (struct xtlock *) & tlck->lock;
2798 /* update the self address in the xtpage header */
2799 pxd = &p->header.self;
2800 PXDaddress(pxd, nxaddr);
2802 /* linelock for the after image of the whole page */
2804 le16_to_cpu(p->header.nextindex) - xtlck->lwm.offset;
2806 /* update the buffer extent descriptor of target xtpage */
2807 xsize = xlen << JFS_SBI(ip->i_sb)->l2bsize;
2808 bmSetXD(mp, nxaddr, xsize);
2810 /* unpin the target page to new homeward bound */
2812 jfs_info("xtRelocate: target xtpage relocated.");
2816 * 3. acquire maplock for the source extent to be freed;
2818 * acquire a maplock saving the src relocated extent address;
2819 * to free of the extent at commit time;
2822 /* if DATAEXT relocation, write a LOG_UPDATEMAP record for
2823 * free PXD of the source data extent (logredo() will update
2824 * bmap for free of source data extent), and update bmap for
2825 * free of the source data extent;
2827 if (xtype == DATAEXT)
2828 tlck = txMaplock(tid, ip, tlckMAP);
2829 /* if XTPAGE relocation, write a LOG_NOREDOPAGE record
2830 * for the source xtpage (logredo() will init NoRedoPage
2831 * filter and will also update bmap for free of the source
2832 * xtpage), and update bmap for free of the source xtpage;
2833 * N.B. We use tlckMAP instead of tlkcXTREE because there
2834 * is no buffer associated with this lock since the buffer
2835 * has been redirected to the target location.
2837 else /* (xtype == XTPAGE) */
2838 tlck = txMaplock(tid, ip, tlckMAP | tlckRELOCATE);
2840 pxdlock = (struct pxd_lock *) & tlck->lock;
2841 pxdlock->flag = mlckFREEPXD;
2842 PXDaddress(&pxdlock->pxd, oxaddr);
2843 PXDlength(&pxdlock->pxd, xlen);
2847 * 4. update the parent xad entry for relocation;
2849 * acquire tlck for the parent entry with XAD_NEW as entry
2850 * update which will write LOG_REDOPAGE and update bmap for
2851 * allocation of XAD_NEW destination extent;
2853 jfs_info("xtRelocate: update parent xad entry.");
2854 BT_MARK_DIRTY(pmp, ip);
2855 tlck = txLock(tid, ip, pmp, tlckXTREE | tlckGROW);
2856 xtlck = (struct xtlock *) & tlck->lock;
2858 /* update the XAD with the new destination extent; */
2859 xad = &pp->xad[index];
2860 xad->flag |= XAD_NEW;
2861 XADaddress(xad, nxaddr);
2863 xtlck->lwm.offset = min(index, xtlck->lwm.offset);
2864 xtlck->lwm.length = le16_to_cpu(pp->header.nextindex) -
2867 /* unpin the parent xtpage */
2877 * function: search for the internal xad entry covering specified extent.
2878 * This function is mainly used by defragfs utility.
2882 * xad - extent to find;
2883 * cmpp - comparison result:
2884 * btstack - traverse stack;
2885 * flag - search process flag;
2888 * btstack contains (bn, index) of search path traversed to the entry.
2889 * *cmpp is set to result of comparison with the entry returned.
2890 * the page containing the entry is pinned at exit.
2892 static int xtSearchNode(struct inode *ip, xad_t * xad, /* required XAD entry */
2893 int *cmpp, struct btstack * btstack, int flag)
2898 int cmp = 1; /* init for empty page */
2899 s64 bn; /* block number */
2900 struct metapage *mp; /* meta-page buffer */
2901 xtpage_t *p; /* page */
2902 int base, index, lim;
2903 struct btframe *btsp;
2908 xoff = offsetXAD(xad);
2909 xlen = lengthXAD(xad);
2910 xaddr = addressXAD(xad);
2913 * search down tree from root:
2915 * between two consecutive entries of <Ki, Pi> and <Kj, Pj> of
2916 * internal page, child page Pi contains entry with k, Ki <= K < Kj.
2918 * if entry with search key K is not found
2919 * internal page search find the entry with largest key Ki
2920 * less than K which point to the child page to search;
2921 * leaf page search find the entry with smallest key Kj
2922 * greater than K so that the returned index is the position of
2923 * the entry to be shifted right for insertion of new entry.
2924 * for empty tree, search key is greater than any key of the tree.
2926 * by convention, root bn = 0.
2929 /* get/pin the page to search */
2930 XT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
2933 if (p->header.flag & BT_LEAF) {
2938 lim = le16_to_cpu(p->header.nextindex) - XTENTRYSTART;
2941 * binary search with search key K on the current page
2943 for (base = XTENTRYSTART; lim; lim >>= 1) {
2944 index = base + (lim >> 1);
2946 XT_CMP(cmp, xoff, &p->xad[index], t64);
2951 * verify for exact match;
2953 if (xaddr == addressXAD(&p->xad[index]) &&
2954 xoff == offsetXAD(&p->xad[index])) {
2957 /* save search result */
2958 btsp = btstack->top;
2960 btsp->index = index;
2966 /* descend/search its child page */
2977 * search miss - non-leaf page:
2979 * base is the smallest index with key (Kj) greater than
2980 * search key (K) and may be zero or maxentry index.
2981 * if base is non-zero, decrement base by one to get the parent
2982 * entry of the child page to search.
2984 index = base ? base - 1 : base;
2987 * go down to child page
2990 /* get the child page block number */
2991 bn = addressXAD(&p->xad[index]);
2993 /* unpin the parent page */
3003 * link around a freed page.
3012 static int xtRelink(tid_t tid, struct inode *ip, xtpage_t * p)
3015 struct metapage *mp;
3019 nextbn = le64_to_cpu(p->header.next);
3020 prevbn = le64_to_cpu(p->header.prev);
3022 /* update prev pointer of the next page */
3024 XT_GETPAGE(ip, nextbn, mp, PSIZE, p, rc);
3029 * acquire a transaction lock on the page;
3031 * action: update prev pointer;
3033 BT_MARK_DIRTY(mp, ip);
3034 tlck = txLock(tid, ip, mp, tlckXTREE | tlckRELINK);
3036 /* the page may already have been tlock'd */
3038 p->header.prev = cpu_to_le64(prevbn);
3043 /* update next pointer of the previous page */
3045 XT_GETPAGE(ip, prevbn, mp, PSIZE, p, rc);
3050 * acquire a transaction lock on the page;
3052 * action: update next pointer;
3054 BT_MARK_DIRTY(mp, ip);
3055 tlck = txLock(tid, ip, mp, tlckXTREE | tlckRELINK);
3057 /* the page may already have been tlock'd */
3059 p->header.next = le64_to_cpu(nextbn);
3066 #endif /* _STILL_TO_PORT */
3072 * initialize file root (inline in inode)
3074 void xtInitRoot(tid_t tid, struct inode *ip)
3079 * acquire a transaction lock on the root
3083 txLock(tid, ip, (struct metapage *) &JFS_IP(ip)->bxflag,
3084 tlckXTREE | tlckNEW);
3085 p = &JFS_IP(ip)->i_xtroot;
3087 p->header.flag = DXD_INDEX | BT_ROOT | BT_LEAF;
3088 p->header.nextindex = cpu_to_le16(XTENTRYSTART);
3090 if (S_ISDIR(ip->i_mode))
3091 p->header.maxentry = cpu_to_le16(XTROOTINITSLOT_DIR);
3093 p->header.maxentry = cpu_to_le16(XTROOTINITSLOT);
3103 * We can run into a deadlock truncating a file with a large number of
3104 * xtree pages (large fragmented file). A robust fix would entail a
3105 * reservation system where we would reserve a number of metadata pages
3106 * and tlocks which we would be guaranteed without a deadlock. Without
3107 * this, a partial fix is to limit number of metadata pages we will lock
3108 * in a single transaction. Currently we will truncate the file so that
3109 * no more than 50 leaf pages will be locked. The caller of xtTruncate
3110 * will be responsible for ensuring that the current transaction gets
3111 * committed, and that subsequent transactions are created to truncate
3112 * the file further if needed.
3114 #define MAX_TRUNCATE_LEAVES 50
3120 * traverse for truncation logging backward bottom up;
3121 * terminate at the last extent entry at the current subtree
3122 * root page covering new down size.
3123 * truncation may occur within the last extent entry.
3129 * int type) {PWMAP, PMAP, WMAP; DELETE, TRUNCATE}
3135 * 1. truncate (non-COMMIT_NOLINK file)
3136 * by jfs_truncate() or jfs_open(O_TRUNC):
3138 * 2. truncate index table of directory when last entry removed
3139 * map update via tlock at commit time;
3141 * Call xtTruncate_pmap instead
3143 * 1. remove (free zero link count) on last reference release
3144 * (pmap has been freed at commit zero link count);
3145 * 2. truncate (COMMIT_NOLINK file, i.e., tmp file):
3147 * map update directly at truncation time;
3150 * no LOG_NOREDOPAGE is required (NOREDOFILE is sufficient);
3151 * else if (TRUNCATE)
3152 * must write LOG_NOREDOPAGE for deleted index page;
3154 * pages may already have been tlocked by anonymous transactions
3155 * during file growth (i.e., write) before truncation;
3157 * except last truncated entry, deleted entries remains as is
3158 * in the page (nextindex is updated) for other use
3159 * (e.g., log/update allocation map): this avoid copying the page
3160 * info but delay free of pages;
3163 s64 xtTruncate(tid_t tid, struct inode *ip, s64 newsize, int flag)
3167 struct metapage *mp;
3170 int index, nextindex;
3173 int xlen, len, freexlen;
3174 struct btstack btstack;
3175 struct btframe *parent;
3176 struct tblock *tblk = NULL;
3177 struct tlock *tlck = NULL;
3178 struct xtlock *xtlck = NULL;
3179 struct xdlistlock xadlock; /* maplock for COMMIT_WMAP */
3180 struct pxd_lock *pxdlock; /* maplock for COMMIT_WMAP */
3183 int locked_leaves = 0;
3185 /* save object truncation type */
3187 tblk = tid_to_tblock(tid);
3188 tblk->xflag |= flag;
3194 assert(flag != COMMIT_PMAP);
3196 if (flag == COMMIT_PWMAP)
3200 xadlock.flag = mlckFREEXADLIST;
3205 * if the newsize is not an integral number of pages,
3206 * the file between newsize and next page boundary will
3208 * if truncating into a file hole, it will cause
3209 * a full block to be allocated for the logical block.
3213 * release page blocks of truncated region <teof, eof>
3215 * free the data blocks from the leaf index blocks.
3216 * delete the parent index entries corresponding to
3217 * the freed child data/index blocks.
3218 * free the index blocks themselves which aren't needed
3219 * in new sized file.
3221 * index blocks are updated only if the blocks are to be
3222 * retained in the new sized file.
3223 * if type is PMAP, the data and index pages are NOT
3224 * freed, and the data and index blocks are NOT freed
3226 * (this will allow continued access of data/index of
3227 * temporary file (zerolink count file truncated to zero-length)).
3229 teof = (newsize + (JFS_SBI(ip->i_sb)->bsize - 1)) >>
3230 JFS_SBI(ip->i_sb)->l2bsize;
3238 * root resides in the inode
3243 * first access of each page:
3246 XT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
3250 /* process entries backward from last index */
3251 index = le16_to_cpu(p->header.nextindex) - 1;
3254 /* Since this is the rightmost page at this level, and we may have
3255 * already freed a page that was formerly to the right, let's make
3256 * sure that the next pointer is zero.
3258 if (p->header.next) {
3261 * Make sure this change to the header is logged.
3262 * If we really truncate this leaf, the flag
3263 * will be changed to tlckTRUNCATE
3265 tlck = txLock(tid, ip, mp, tlckXTREE|tlckGROW);
3266 BT_MARK_DIRTY(mp, ip);
3270 if (p->header.flag & BT_INTERNAL)
3278 /* does region covered by leaf page precede Teof ? */
3279 xad = &p->xad[index];
3280 xoff = offsetXAD(xad);
3281 xlen = lengthXAD(xad);
3282 if (teof >= xoff + xlen) {
3287 /* (re)acquire tlock of the leaf page */
3289 if (++locked_leaves > MAX_TRUNCATE_LEAVES) {
3291 * We need to limit the size of the transaction
3292 * to avoid exhausting pagecache & tlocks
3295 newsize = (xoff + xlen) << JFS_SBI(ip->i_sb)->l2bsize;
3298 tlck = txLock(tid, ip, mp, tlckXTREE);
3299 tlck->type = tlckXTREE | tlckTRUNCATE;
3300 xtlck = (struct xtlock *) & tlck->lock;
3301 xtlck->hwm.offset = le16_to_cpu(p->header.nextindex) - 1;
3303 BT_MARK_DIRTY(mp, ip);
3306 * scan backward leaf page entries
3308 for (; index >= XTENTRYSTART; index--) {
3309 xad = &p->xad[index];
3310 xoff = offsetXAD(xad);
3311 xlen = lengthXAD(xad);
3312 xaddr = addressXAD(xad);
3315 * The "data" for a directory is indexed by the block
3316 * device's address space. This metadata must be invalidated
3319 if (S_ISDIR(ip->i_mode) && (teof == 0))
3320 invalidate_xad_metapages(ip, *xad);
3322 * entry beyond eof: continue scan of current page
3324 * ---|---=======------->
3333 * (xoff <= teof): last entry to be deleted from page;
3334 * If other entries remain in page: keep and update the page.
3338 * eof == entry_start: delete the entry
3340 * -------|=======------->
3347 if (index == XTENTRYSTART)
3353 * eof within the entry: truncate the entry.
3355 * -------===|===------->
3358 else if (teof < xoff + xlen) {
3359 /* update truncated entry */
3361 freexlen = xlen - len;
3362 XADlength(xad, len);
3364 /* save pxd of truncated extent in tlck */
3366 if (log) { /* COMMIT_PWMAP */
3367 xtlck->lwm.offset = (xtlck->lwm.offset) ?
3368 min(index, (int)xtlck->lwm.offset) : index;
3369 xtlck->lwm.length = index + 1 -
3371 xtlck->twm.offset = index;
3372 pxdlock = (struct pxd_lock *) & xtlck->pxdlock;
3373 pxdlock->flag = mlckFREEPXD;
3374 PXDaddress(&pxdlock->pxd, xaddr);
3375 PXDlength(&pxdlock->pxd, freexlen);
3377 /* free truncated extent */
3378 else { /* COMMIT_WMAP */
3380 pxdlock = (struct pxd_lock *) & xadlock;
3381 pxdlock->flag = mlckFREEPXD;
3382 PXDaddress(&pxdlock->pxd, xaddr);
3383 PXDlength(&pxdlock->pxd, freexlen);
3384 txFreeMap(ip, pxdlock, NULL, COMMIT_WMAP);
3386 /* reset map lock */
3387 xadlock.flag = mlckFREEXADLIST;
3390 /* current entry is new last entry; */
3391 nextindex = index + 1;
3396 * eof beyond the entry:
3398 * -------=======---|--->
3401 else { /* (xoff + xlen < teof) */
3403 nextindex = index + 1;
3406 if (nextindex < le16_to_cpu(p->header.nextindex)) {
3407 if (!log) { /* COMMIT_WAMP */
3408 xadlock.xdlist = &p->xad[nextindex];
3410 le16_to_cpu(p->header.nextindex) -
3412 txFreeMap(ip, (struct maplock *) & xadlock,
3415 p->header.nextindex = cpu_to_le16(nextindex);
3420 /* assert(freed == 0); */
3422 } /* end scan of leaf page entries */
3427 * leaf page become empty: free the page if type != PMAP
3429 if (log) { /* COMMIT_PWMAP */
3430 /* txCommit() with tlckFREE:
3431 * free data extents covered by leaf [XTENTRYSTART:hwm);
3432 * invalidate leaf if COMMIT_PWMAP;
3433 * if (TRUNCATE), will write LOG_NOREDOPAGE;
3435 tlck->type = tlckXTREE | tlckFREE;
3436 } else { /* COMMIT_WAMP */
3438 /* free data extents covered by leaf */
3439 xadlock.xdlist = &p->xad[XTENTRYSTART];
3441 le16_to_cpu(p->header.nextindex) - XTENTRYSTART;
3442 txFreeMap(ip, (struct maplock *) & xadlock, NULL, COMMIT_WMAP);
3445 if (p->header.flag & BT_ROOT) {
3446 p->header.flag &= ~BT_INTERNAL;
3447 p->header.flag |= BT_LEAF;
3448 p->header.nextindex = cpu_to_le16(XTENTRYSTART);
3450 XT_PUTPAGE(mp); /* debug */
3453 if (log) { /* COMMIT_PWMAP */
3454 /* page will be invalidated at tx completion
3457 } else { /* COMMIT_WMAP */
3460 lid_to_tlock(mp->lid)->flag |= tlckFREELOCK;
3462 /* invalidate empty leaf page */
3463 discard_metapage(mp);
3468 * the leaf page become empty: delete the parent entry
3469 * for the leaf page if the parent page is to be kept
3470 * in the new sized file.
3474 * go back up to the parent page
3477 /* pop/restore parent entry for the current child page */
3478 if ((parent = BT_POP(&btstack)) == NULL)
3479 /* current page must have been root */
3482 /* get back the parent page */
3484 XT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
3488 index = parent->index;
3491 * child page was not empty:
3494 /* has any entry deleted from parent ? */
3495 if (index < le16_to_cpu(p->header.nextindex) - 1) {
3496 /* (re)acquire tlock on the parent page */
3497 if (log) { /* COMMIT_PWMAP */
3498 /* txCommit() with tlckTRUNCATE:
3499 * free child extents covered by parent [);
3501 tlck = txLock(tid, ip, mp, tlckXTREE);
3502 xtlck = (struct xtlock *) & tlck->lock;
3503 if (!(tlck->type & tlckTRUNCATE)) {
3505 le16_to_cpu(p->header.
3508 tlckXTREE | tlckTRUNCATE;
3510 } else { /* COMMIT_WMAP */
3512 /* free child extents covered by parent */
3513 xadlock.xdlist = &p->xad[index + 1];
3515 le16_to_cpu(p->header.nextindex) -
3517 txFreeMap(ip, (struct maplock *) & xadlock,
3520 BT_MARK_DIRTY(mp, ip);
3522 p->header.nextindex = cpu_to_le16(index + 1);
3529 * child page was empty:
3531 nfreed += lengthXAD(&p->xad[index]);
3534 * During working map update, child page's tlock must be handled
3535 * before parent's. This is because the parent's tlock will cause
3536 * the child's disk space to be marked available in the wmap, so
3537 * it's important that the child page be released by that time.
3539 * ToDo: tlocks should be on doubly-linked list, so we can
3540 * quickly remove it and add it to the end.
3544 * Move parent page's tlock to the end of the tid's tlock list
3546 if (log && mp->lid && (tblk->last != mp->lid) &&
3547 lid_to_tlock(mp->lid)->tid) {
3548 lid_t lid = mp->lid;
3551 tlck = lid_to_tlock(lid);
3553 if (tblk->next == lid)
3554 tblk->next = tlck->next;
3556 for (prev = lid_to_tlock(tblk->next);
3558 prev = lid_to_tlock(prev->next)) {
3561 prev->next = tlck->next;
3563 lid_to_tlock(tblk->last)->next = lid;
3569 * parent page become empty: free the page
3571 if (index == XTENTRYSTART) {
3572 if (log) { /* COMMIT_PWMAP */
3573 /* txCommit() with tlckFREE:
3574 * free child extents covered by parent;
3575 * invalidate parent if COMMIT_PWMAP;
3577 tlck = txLock(tid, ip, mp, tlckXTREE);
3578 xtlck = (struct xtlock *) & tlck->lock;
3580 le16_to_cpu(p->header.nextindex) - 1;
3581 tlck->type = tlckXTREE | tlckFREE;
3582 } else { /* COMMIT_WMAP */
3584 /* free child extents covered by parent */
3585 xadlock.xdlist = &p->xad[XTENTRYSTART];
3587 le16_to_cpu(p->header.nextindex) -
3589 txFreeMap(ip, (struct maplock *) & xadlock, NULL,
3592 BT_MARK_DIRTY(mp, ip);
3594 if (p->header.flag & BT_ROOT) {
3595 p->header.flag &= ~BT_INTERNAL;
3596 p->header.flag |= BT_LEAF;
3597 p->header.nextindex = cpu_to_le16(XTENTRYSTART);
3598 if (le16_to_cpu(p->header.maxentry) == XTROOTMAXSLOT) {
3600 * Shrink root down to allow inline
3601 * EA (otherwise fsck complains)
3603 p->header.maxentry =
3604 cpu_to_le16(XTROOTINITSLOT);
3605 JFS_IP(ip)->mode2 |= INLINEEA;
3608 XT_PUTPAGE(mp); /* debug */
3611 if (log) { /* COMMIT_PWMAP */
3612 /* page will be invalidated at tx completion
3615 } else { /* COMMIT_WMAP */
3618 lid_to_tlock(mp->lid)->flag |=
3621 /* invalidate parent page */
3622 discard_metapage(mp);
3625 /* parent has become empty and freed:
3626 * go back up to its parent page
3633 * parent page still has entries for front region;
3636 /* try truncate region covered by preceding entry
3637 * (process backward)
3641 /* go back down to the child page corresponding
3648 * internal page: go down to child page of current entry
3651 /* save current parent entry for the child page */
3652 if (BT_STACK_FULL(&btstack)) {
3653 jfs_error(ip->i_sb, "stack overrun in xtTruncate!");
3657 BT_PUSH(&btstack, bn, index);
3659 /* get child page */
3660 xad = &p->xad[index];
3661 bn = addressXAD(xad);
3664 * first access of each internal entry:
3666 /* release parent page */
3669 /* process the child page */
3674 * update file resource stat
3678 if (S_ISDIR(ip->i_mode) && !newsize)
3679 ip->i_size = 1; /* fsck hates zero-length directories */
3681 ip->i_size = newsize;
3683 /* update quota allocation to reflect freed blocks */
3684 dquot_free_block(ip, nfreed);
3687 * free tlock of invalidated pages
3689 if (flag == COMMIT_WMAP)
3700 * Perform truncate to zero length for deleted file, leaving the
3701 * the xtree and working map untouched. This allows the file to
3702 * be accessed via open file handles, while the delete of the file
3703 * is committed to disk.
3708 * s64 committed_size)
3710 * return: new committed size
3714 * To avoid deadlock by holding too many transaction locks, the
3715 * truncation may be broken up into multiple transactions.
3716 * The committed_size keeps track of part of the file has been
3717 * freed from the pmaps.
3719 s64 xtTruncate_pmap(tid_t tid, struct inode *ip, s64 committed_size)
3722 struct btstack btstack;
3725 int locked_leaves = 0;
3726 struct metapage *mp;
3728 struct btframe *parent;
3730 struct tblock *tblk;
3731 struct tlock *tlck = NULL;
3735 struct xtlock *xtlck = NULL;
3737 /* save object truncation type */
3738 tblk = tid_to_tblock(tid);
3739 tblk->xflag |= COMMIT_PMAP;
3744 if (committed_size) {
3745 xoff = (committed_size >> JFS_SBI(ip->i_sb)->l2bsize) - 1;
3746 rc = xtSearch(ip, xoff, NULL, &cmp, &btstack, 0);
3750 XT_GETSEARCH(ip, btstack.top, bn, mp, p, index);
3755 "xtTruncate_pmap: did not find extent");
3762 * root resides in the inode
3767 * first access of each page:
3770 XT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
3774 /* process entries backward from last index */
3775 index = le16_to_cpu(p->header.nextindex) - 1;
3777 if (p->header.flag & BT_INTERNAL)
3785 if (++locked_leaves > MAX_TRUNCATE_LEAVES) {
3787 * We need to limit the size of the transaction
3788 * to avoid exhausting pagecache & tlocks
3790 xad = &p->xad[index];
3791 xoff = offsetXAD(xad);
3792 xlen = lengthXAD(xad);
3794 return (xoff + xlen) << JFS_SBI(ip->i_sb)->l2bsize;
3796 tlck = txLock(tid, ip, mp, tlckXTREE);
3797 tlck->type = tlckXTREE | tlckFREE;
3798 xtlck = (struct xtlock *) & tlck->lock;
3799 xtlck->hwm.offset = index;
3805 * go back up to the parent page
3808 /* pop/restore parent entry for the current child page */
3809 if ((parent = BT_POP(&btstack)) == NULL)
3810 /* current page must have been root */
3813 /* get back the parent page */
3815 XT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
3819 index = parent->index;
3822 * parent page become empty: free the page
3824 if (index == XTENTRYSTART) {
3825 /* txCommit() with tlckFREE:
3826 * free child extents covered by parent;
3827 * invalidate parent if COMMIT_PWMAP;
3829 tlck = txLock(tid, ip, mp, tlckXTREE);
3830 xtlck = (struct xtlock *) & tlck->lock;
3831 xtlck->hwm.offset = le16_to_cpu(p->header.nextindex) - 1;
3832 tlck->type = tlckXTREE | tlckFREE;
3836 if (p->header.flag & BT_ROOT) {
3844 * parent page still has entries for front region;
3849 * internal page: go down to child page of current entry
3852 /* save current parent entry for the child page */
3853 if (BT_STACK_FULL(&btstack)) {
3854 jfs_error(ip->i_sb, "stack overrun in xtTruncate_pmap!");
3858 BT_PUSH(&btstack, bn, index);
3860 /* get child page */
3861 xad = &p->xad[index];
3862 bn = addressXAD(xad);
3865 * first access of each internal entry:
3867 /* release parent page */
3870 /* process the child page */
3878 #ifdef CONFIG_JFS_STATISTICS
3879 static int jfs_xtstat_proc_show(struct seq_file *m, void *v)
3882 "JFS Xtree statistics\n"
3883 "====================\n"
3885 "fast searches = %d\n"
3893 static int jfs_xtstat_proc_open(struct inode *inode, struct file *file)
3895 return single_open(file, jfs_xtstat_proc_show, NULL);
3898 const struct file_operations jfs_xtstat_proc_fops = {
3899 .owner = THIS_MODULE,
3900 .open = jfs_xtstat_proc_open,
3902 .llseek = seq_lseek,
3903 .release = single_release,