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); \
71 if ((le16_to_cpu((P)->header.nextindex) < XTENTRYSTART) || \
72 (le16_to_cpu((P)->header.nextindex) > \
73 le16_to_cpu((P)->header.maxentry)) || \
74 (le16_to_cpu((P)->header.maxentry) > \
75 (((BN) == 0) ? XTROOTMAXSLOT : PSIZE >> L2XTSLOTSIZE))) { \
76 jfs_error((IP)->i_sb, \
77 "XT_GETPAGE: xtree page corrupt\n"); \
86 #define XT_PUTPAGE(MP) BT_PUTPAGE(MP)
88 #define XT_GETSEARCH(IP, LEAF, BN, MP, P, INDEX) \
89 BT_GETSEARCH(IP, LEAF, BN, MP, xtpage_t, P, INDEX, i_xtroot)
90 /* xtree entry parameter descriptor */
98 struct pxdlist *pxdlist;
105 #ifdef CONFIG_JFS_STATISTICS
117 static int xtSearch(struct inode *ip, s64 xoff, s64 *next, int *cmpp,
118 struct btstack * btstack, int flag);
120 static int xtSplitUp(tid_t tid,
122 struct xtsplit * split, struct btstack * btstack);
124 static int xtSplitPage(tid_t tid, struct inode *ip, struct xtsplit * split,
125 struct metapage ** rmpp, s64 * rbnp);
127 static int xtSplitRoot(tid_t tid, struct inode *ip,
128 struct xtsplit * split, struct metapage ** rmpp);
130 #ifdef _STILL_TO_PORT
131 static int xtDeleteUp(tid_t tid, struct inode *ip, struct metapage * fmp,
132 xtpage_t * fp, struct btstack * btstack);
134 static int xtSearchNode(struct inode *ip,
136 int *cmpp, struct btstack * btstack, int flag);
138 static int xtRelink(tid_t tid, struct inode *ip, xtpage_t * fp);
139 #endif /* _STILL_TO_PORT */
144 * function: map a single page into a physical extent;
146 int xtLookup(struct inode *ip, s64 lstart,
147 s64 llen, int *pflag, s64 * paddr, s32 * plen, int no_check)
150 struct btstack btstack;
157 s64 next, size, xoff, xend;
165 /* is lookup offset beyond eof ? */
166 size = ((u64) ip->i_size + (JFS_SBI(ip->i_sb)->bsize - 1)) >>
167 JFS_SBI(ip->i_sb)->l2bsize;
173 * search for the xad entry covering the logical extent
176 if ((rc = xtSearch(ip, lstart, &next, &cmp, &btstack, 0))) {
177 jfs_err("xtLookup: xtSearch returned %d", rc);
182 * compute the physical extent covering logical extent
184 * N.B. search may have failed (e.g., hole in sparse file),
185 * and returned the index of the next entry.
187 /* retrieve search result */
188 XT_GETSEARCH(ip, btstack.top, bn, mp, p, index);
190 /* is xad found covering start of logical extent ?
191 * lstart is a page start address,
192 * i.e., lstart cannot start in a hole;
196 *plen = min(next - lstart, llen);
203 xad = &p->xad[index];
204 xoff = offsetXAD(xad);
205 xlen = lengthXAD(xad);
207 xaddr = addressXAD(xad);
209 /* initialize new pxd */
211 *paddr = xaddr + (lstart - xoff);
212 /* a page must be fully covered by an xad */
213 *plen = min(xend - lstart, llen);
224 * function: search for the xad entry covering specified offset.
228 * xoff - extent offset;
229 * nextp - address of next extent (if any) for search miss
230 * cmpp - comparison result:
231 * btstack - traverse stack;
232 * flag - search process flag (XT_INSERT);
235 * btstack contains (bn, index) of search path traversed to the entry.
236 * *cmpp is set to result of comparison with the entry returned.
237 * the page containing the entry is pinned at exit.
239 static int xtSearch(struct inode *ip, s64 xoff, s64 *nextp,
240 int *cmpp, struct btstack * btstack, int flag)
242 struct jfs_inode_info *jfs_ip = JFS_IP(ip);
244 int cmp = 1; /* init for empty page */
245 s64 bn; /* block number */
246 struct metapage *mp; /* page buffer */
247 xtpage_t *p; /* page */
249 int base, index, lim, btindex;
250 struct btframe *btsp;
251 int nsplit = 0; /* number of pages to split */
255 INCREMENT(xtStat.search);
262 * search down tree from root:
264 * between two consecutive entries of <Ki, Pi> and <Kj, Pj> of
265 * internal page, child page Pi contains entry with k, Ki <= K < Kj.
267 * if entry with search key K is not found
268 * internal page search find the entry with largest key Ki
269 * less than K which point to the child page to search;
270 * leaf page search find the entry with smallest key Kj
271 * greater than K so that the returned index is the position of
272 * the entry to be shifted right for insertion of new entry.
273 * for empty tree, search key is greater than any key of the tree.
275 * by convention, root bn = 0.
278 /* get/pin the page to search */
279 XT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
283 /* try sequential access heuristics with the previous
284 * access entry in target leaf page:
285 * once search narrowed down into the target leaf,
286 * key must either match an entry in the leaf or
287 * key entry does not exist in the tree;
290 if ((jfs_ip->btorder & BT_SEQUENTIAL) &&
291 (p->header.flag & BT_LEAF) &&
292 (index = jfs_ip->btindex) <
293 le16_to_cpu(p->header.nextindex)) {
294 xad = &p->xad[index];
295 t64 = offsetXAD(xad);
296 if (xoff < t64 + lengthXAD(xad)) {
302 /* stop sequential access heuristics */
304 } else { /* (t64 + lengthXAD(xad)) <= xoff */
306 /* try next sequential entry */
309 le16_to_cpu(p->header.nextindex)) {
311 t64 = offsetXAD(xad);
312 if (xoff < t64 + lengthXAD(xad)) {
318 /* miss: key falls between
319 * previous and this entry
326 /* (xoff >= t64 + lengthXAD(xad));
327 * matching entry may be further out:
328 * stop heuristic search
330 /* stop sequential access heuristics */
334 /* (index == p->header.nextindex);
335 * miss: key entry does not exist in
336 * the target leaf/tree
343 * if hit, return index of the entry found, and
344 * if miss, where new entry with search key is
348 /* compute number of pages to split */
349 if (flag & XT_INSERT) {
350 if (p->header.nextindex == /* little-endian */
355 btstack->nsplit = nsplit;
358 /* save search result */
364 /* update sequential access heuristics */
365 jfs_ip->btindex = index;
370 INCREMENT(xtStat.fastSearch);
374 /* well, ... full search now */
376 lim = le16_to_cpu(p->header.nextindex) - XTENTRYSTART;
379 * binary search with search key K on the current page
381 for (base = XTENTRYSTART; lim; lim >>= 1) {
382 index = base + (lim >> 1);
384 XT_CMP(cmp, xoff, &p->xad[index], t64);
389 /* search hit - leaf page:
390 * return the entry found
392 if (p->header.flag & BT_LEAF) {
395 /* compute number of pages to split */
396 if (flag & XT_INSERT) {
397 if (p->header.nextindex ==
402 btstack->nsplit = nsplit;
405 /* save search result */
411 /* init sequential access heuristics */
412 btindex = jfs_ip->btindex;
413 if (index == btindex ||
414 index == btindex + 1)
415 jfs_ip->btorder = BT_SEQUENTIAL;
417 jfs_ip->btorder = BT_RANDOM;
418 jfs_ip->btindex = index;
422 /* search hit - internal page:
423 * descend/search its child page
425 if (index < le16_to_cpu(p->header.nextindex)-1)
426 next = offsetXAD(&p->xad[index + 1]);
439 * base is the smallest index with key (Kj) greater than
440 * search key (K) and may be zero or maxentry index.
442 if (base < le16_to_cpu(p->header.nextindex))
443 next = offsetXAD(&p->xad[base]);
445 * search miss - leaf page:
447 * return location of entry (base) where new entry with
448 * search key K is to be inserted.
450 if (p->header.flag & BT_LEAF) {
453 /* compute number of pages to split */
454 if (flag & XT_INSERT) {
455 if (p->header.nextindex ==
460 btstack->nsplit = nsplit;
463 /* save search result */
469 /* init sequential access heuristics */
470 btindex = jfs_ip->btindex;
471 if (base == btindex || base == btindex + 1)
472 jfs_ip->btorder = BT_SEQUENTIAL;
474 jfs_ip->btorder = BT_RANDOM;
475 jfs_ip->btindex = base;
484 * search miss - non-leaf page:
486 * if base is non-zero, decrement base by one to get the parent
487 * entry of the child page to search.
489 index = base ? base - 1 : base;
492 * go down to child page
495 /* update number of pages to split */
496 if (p->header.nextindex == p->header.maxentry)
501 /* push (bn, index) of the parent page/entry */
502 if (BT_STACK_FULL(btstack)) {
503 jfs_error(ip->i_sb, "stack overrun!\n");
507 BT_PUSH(btstack, bn, index);
509 /* get the child page block number */
510 bn = addressXAD(&p->xad[index]);
512 /* unpin the parent page */
523 * tid - transaction id;
525 * xflag - extent flag (XAD_NOTRECORDED):
526 * xoff - extent offset;
527 * xlen - extent length;
528 * xaddrp - extent address pointer (in/out):
530 * caller allocated data extent at *xaddrp;
532 * allocate data extent and return its xaddr;
537 int xtInsert(tid_t tid, /* transaction id */
538 struct inode *ip, int xflag, s64 xoff, s32 xlen, s64 * xaddrp,
543 struct metapage *mp; /* meta-page buffer */
544 xtpage_t *p; /* base B+-tree index page */
546 int index, nextindex;
547 struct btstack btstack; /* traverse stack */
548 struct xtsplit split; /* split information */
553 struct xtlock *xtlck;
555 jfs_info("xtInsert: nxoff:0x%lx nxlen:0x%x", (ulong) xoff, xlen);
558 * search for the entry location at which to insert:
560 * xtFastSearch() and xtSearch() both returns (leaf page
561 * pinned, index at which to insert).
562 * n.b. xtSearch() may return index of maxentry of
565 if ((rc = xtSearch(ip, xoff, &next, &cmp, &btstack, XT_INSERT)))
568 /* retrieve search result */
569 XT_GETSEARCH(ip, btstack.top, bn, mp, p, index);
571 /* This test must follow XT_GETSEARCH since mp must be valid if
572 * we branch to out: */
573 if ((cmp == 0) || (next && (xlen > next - xoff))) {
579 * allocate data extent requested
581 * allocation hint: last xad
583 if ((xaddr = *xaddrp) == 0) {
584 if (index > XTENTRYSTART) {
585 xad = &p->xad[index - 1];
586 hint = addressXAD(xad) + lengthXAD(xad) - 1;
589 if ((rc = dquot_alloc_block(ip, xlen)))
591 if ((rc = dbAlloc(ip, hint, (s64) xlen, &xaddr))) {
592 dquot_free_block(ip, xlen);
598 * insert entry for new extent
603 * if the leaf page is full, split the page and
604 * propagate up the router entry for the new page from split
606 * The xtSplitUp() will insert the entry and unpin the leaf page.
608 nextindex = le16_to_cpu(p->header.nextindex);
609 if (nextindex == le16_to_cpu(p->header.maxentry)) {
616 split.pxdlist = NULL;
617 if ((rc = xtSplitUp(tid, ip, &split, &btstack))) {
618 /* undo data extent allocation */
620 dbFree(ip, xaddr, (s64) xlen);
621 dquot_free_block(ip, xlen);
631 * insert the new entry into the leaf page
634 * acquire a transaction lock on the leaf page;
636 * action: xad insertion/extension;
638 BT_MARK_DIRTY(mp, ip);
640 /* if insert into middle, shift right remaining entries. */
641 if (index < nextindex)
642 memmove(&p->xad[index + 1], &p->xad[index],
643 (nextindex - index) * sizeof(xad_t));
645 /* insert the new entry: mark the entry NEW */
646 xad = &p->xad[index];
647 XT_PUTENTRY(xad, xflag, xoff, xlen, xaddr);
649 /* advance next available entry index */
650 le16_add_cpu(&p->header.nextindex, 1);
652 /* Don't log it if there are no links to the file */
653 if (!test_cflag(COMMIT_Nolink, ip)) {
654 tlck = txLock(tid, ip, mp, tlckXTREE | tlckGROW);
655 xtlck = (struct xtlock *) & tlck->lock;
657 (xtlck->lwm.offset) ? min(index,
658 (int)xtlck->lwm.offset) : index;
660 le16_to_cpu(p->header.nextindex) - xtlck->lwm.offset;
666 /* unpin the leaf page */
677 * split full pages as propagating insertion up the tree
680 * tid - transaction id;
682 * split - entry parameter descriptor;
683 * btstack - traverse stack from xtSearch()
689 struct inode *ip, struct xtsplit * split, struct btstack * btstack)
692 struct metapage *smp;
693 xtpage_t *sp; /* split page */
694 struct metapage *rmp;
695 s64 rbn; /* new right page block number */
696 struct metapage *rcmp;
697 xtpage_t *rcp; /* right child page */
698 s64 rcbn; /* right child page block number */
699 int skip; /* index of entry of insertion */
700 int nextindex; /* next available entry index of p */
701 struct btframe *parent; /* parent page entry on traverse stack */
705 int nsplit; /* number of pages split */
706 struct pxdlist pxdlist;
709 struct xtlock *xtlck;
712 sp = XT_PAGE(ip, smp);
714 /* is inode xtree root extension/inline EA area free ? */
715 if ((sp->header.flag & BT_ROOT) && (!S_ISDIR(ip->i_mode)) &&
716 (le16_to_cpu(sp->header.maxentry) < XTROOTMAXSLOT) &&
717 (JFS_IP(ip)->mode2 & INLINEEA)) {
718 sp->header.maxentry = cpu_to_le16(XTROOTMAXSLOT);
719 JFS_IP(ip)->mode2 &= ~INLINEEA;
721 BT_MARK_DIRTY(smp, ip);
723 * acquire a transaction lock on the leaf page;
725 * action: xad insertion/extension;
728 /* if insert into middle, shift right remaining entries. */
730 nextindex = le16_to_cpu(sp->header.nextindex);
731 if (skip < nextindex)
732 memmove(&sp->xad[skip + 1], &sp->xad[skip],
733 (nextindex - skip) * sizeof(xad_t));
735 /* insert the new entry: mark the entry NEW */
736 xad = &sp->xad[skip];
737 XT_PUTENTRY(xad, split->flag, split->off, split->len,
740 /* advance next available entry index */
741 le16_add_cpu(&sp->header.nextindex, 1);
743 /* Don't log it if there are no links to the file */
744 if (!test_cflag(COMMIT_Nolink, ip)) {
745 tlck = txLock(tid, ip, smp, tlckXTREE | tlckGROW);
746 xtlck = (struct xtlock *) & tlck->lock;
747 xtlck->lwm.offset = (xtlck->lwm.offset) ?
748 min(skip, (int)xtlck->lwm.offset) : skip;
750 le16_to_cpu(sp->header.nextindex) -
758 * allocate new index blocks to cover index page split(s)
762 if (split->pxdlist == NULL) {
763 nsplit = btstack->nsplit;
764 split->pxdlist = &pxdlist;
765 pxdlist.maxnpxd = pxdlist.npxd = 0;
766 pxd = &pxdlist.pxd[0];
767 xlen = JFS_SBI(ip->i_sb)->nbperpage;
768 for (; nsplit > 0; nsplit--, pxd++) {
769 if ((rc = dbAlloc(ip, (s64) 0, (s64) xlen, &xaddr))
771 PXDaddress(pxd, xaddr);
772 PXDlength(pxd, xlen);
779 /* undo allocation */
787 * Split leaf page <sp> into <sp> and a new right page <rp>.
789 * The split routines insert the new entry into the leaf page,
790 * and acquire txLock as appropriate.
791 * return <rp> pinned and its block number <rpbn>.
793 rc = (sp->header.flag & BT_ROOT) ?
794 xtSplitRoot(tid, ip, split, &rmp) :
795 xtSplitPage(tid, ip, split, &rmp, &rbn);
802 * propagate up the router entry for the leaf page just split
804 * insert a router entry for the new page into the parent page,
805 * propagate the insert/split up the tree by walking back the stack
806 * of (bn of parent page, index of child page entry in parent page)
807 * that were traversed during the search for the page that split.
809 * the propagation of insert/split up the tree stops if the root
810 * splits or the page inserted into doesn't have to split to hold
813 * the parent entry for the split page remains the same, and
814 * a new entry is inserted at its right with the first key and
815 * block number of the new right page.
817 * There are a maximum of 3 pages pinned at any time:
818 * right child, left parent and right parent (when the parent splits)
819 * to keep the child page pinned while working on the parent.
820 * make sure that all pins are released at exit.
822 while ((parent = BT_POP(btstack)) != NULL) {
823 /* parent page specified by stack frame <parent> */
825 /* keep current child pages <rcp> pinned */
828 rcp = XT_PAGE(ip, rcmp);
831 * insert router entry in parent for new right child page <rp>
833 /* get/pin the parent page <sp> */
834 XT_GETPAGE(ip, parent->bn, smp, PSIZE, sp, rc);
841 * The new key entry goes ONE AFTER the index of parent entry,
842 * because the split was to the right.
844 skip = parent->index + 1;
847 * split or shift right remaining entries of the parent page
849 nextindex = le16_to_cpu(sp->header.nextindex);
851 * parent page is full - split the parent page
853 if (nextindex == le16_to_cpu(sp->header.maxentry)) {
854 /* init for parent page split */
856 split->index = skip; /* index at insert */
857 split->flag = XAD_NEW;
858 split->off = offsetXAD(&rcp->xad[XTENTRYSTART]);
859 split->len = JFS_SBI(ip->i_sb)->nbperpage;
862 /* unpin previous right child page */
865 /* The split routines insert the new entry,
866 * and acquire txLock as appropriate.
867 * return <rp> pinned and its block number <rpbn>.
869 rc = (sp->header.flag & BT_ROOT) ?
870 xtSplitRoot(tid, ip, split, &rmp) :
871 xtSplitPage(tid, ip, split, &rmp, &rbn);
878 /* keep new child page <rp> pinned */
881 * parent page is not full - insert in parent page
885 * insert router entry in parent for the right child
886 * page from the first entry of the right child page:
889 * acquire a transaction lock on the parent page;
891 * action: router xad insertion;
893 BT_MARK_DIRTY(smp, ip);
896 * if insert into middle, shift right remaining entries
898 if (skip < nextindex)
899 memmove(&sp->xad[skip + 1], &sp->xad[skip],
901 skip) << L2XTSLOTSIZE);
903 /* insert the router entry */
904 xad = &sp->xad[skip];
905 XT_PUTENTRY(xad, XAD_NEW,
906 offsetXAD(&rcp->xad[XTENTRYSTART]),
907 JFS_SBI(ip->i_sb)->nbperpage, rcbn);
909 /* advance next available entry index. */
910 le16_add_cpu(&sp->header.nextindex, 1);
912 /* Don't log it if there are no links to the file */
913 if (!test_cflag(COMMIT_Nolink, ip)) {
914 tlck = txLock(tid, ip, smp,
915 tlckXTREE | tlckGROW);
916 xtlck = (struct xtlock *) & tlck->lock;
917 xtlck->lwm.offset = (xtlck->lwm.offset) ?
918 min(skip, (int)xtlck->lwm.offset) : skip;
920 le16_to_cpu(sp->header.nextindex) -
924 /* unpin parent page */
927 /* exit propagate up */
932 /* unpin current right page */
943 * split a full non-root page into
944 * original/split/left page and new right page
945 * i.e., the original/split page remains as left page.
950 * struct xtsplit *split,
951 * struct metapage **rmpp,
955 * Pointer to page in which to insert or NULL on error.
958 xtSplitPage(tid_t tid, struct inode *ip,
959 struct xtsplit * split, struct metapage ** rmpp, s64 * rbnp)
962 struct metapage *smp;
964 struct metapage *rmp;
965 xtpage_t *rp; /* new right page allocated */
966 s64 rbn; /* new right page block number */
970 int skip, maxentry, middle, righthalf, n;
972 struct pxdlist *pxdlist;
975 struct xtlock *sxtlck = NULL, *rxtlck = NULL;
976 int quota_allocation = 0;
979 sp = XT_PAGE(ip, smp);
981 INCREMENT(xtStat.split);
983 pxdlist = split->pxdlist;
984 pxd = &pxdlist->pxd[pxdlist->npxd];
986 rbn = addressPXD(pxd);
988 /* Allocate blocks to quota. */
989 rc = dquot_alloc_block(ip, lengthPXD(pxd));
993 quota_allocation += lengthPXD(pxd);
996 * allocate the new right page for the split
998 rmp = get_metapage(ip, rbn, PSIZE, 1);
1004 jfs_info("xtSplitPage: ip:0x%p smp:0x%p rmp:0x%p", ip, smp, rmp);
1006 BT_MARK_DIRTY(rmp, ip);
1011 rp = (xtpage_t *) rmp->data;
1012 rp->header.self = *pxd;
1013 rp->header.flag = sp->header.flag & BT_TYPE;
1014 rp->header.maxentry = sp->header.maxentry; /* little-endian */
1015 rp->header.nextindex = cpu_to_le16(XTENTRYSTART);
1017 BT_MARK_DIRTY(smp, ip);
1018 /* Don't log it if there are no links to the file */
1019 if (!test_cflag(COMMIT_Nolink, ip)) {
1021 * acquire a transaction lock on the new right page;
1023 tlck = txLock(tid, ip, rmp, tlckXTREE | tlckNEW);
1024 rxtlck = (struct xtlock *) & tlck->lock;
1025 rxtlck->lwm.offset = XTENTRYSTART;
1027 * acquire a transaction lock on the split page
1029 tlck = txLock(tid, ip, smp, tlckXTREE | tlckGROW);
1030 sxtlck = (struct xtlock *) & tlck->lock;
1034 * initialize/update sibling pointers of <sp> and <rp>
1036 nextbn = le64_to_cpu(sp->header.next);
1037 rp->header.next = cpu_to_le64(nextbn);
1038 rp->header.prev = cpu_to_le64(addressPXD(&sp->header.self));
1039 sp->header.next = cpu_to_le64(rbn);
1041 skip = split->index;
1044 * sequential append at tail (after last entry of last page)
1046 * if splitting the last page on a level because of appending
1047 * a entry to it (skip is maxentry), it's likely that the access is
1048 * sequential. adding an empty page on the side of the level is less
1049 * work and can push the fill factor much higher than normal.
1050 * if we're wrong it's no big deal - we will do the split the right
1052 * (it may look like it's equally easy to do a similar hack for
1053 * reverse sorted data, that is, split the tree left, but it's not.
1056 if (nextbn == 0 && skip == le16_to_cpu(sp->header.maxentry)) {
1058 * acquire a transaction lock on the new/right page;
1060 * action: xad insertion;
1062 /* insert entry at the first entry of the new right page */
1063 xad = &rp->xad[XTENTRYSTART];
1064 XT_PUTENTRY(xad, split->flag, split->off, split->len,
1067 rp->header.nextindex = cpu_to_le16(XTENTRYSTART + 1);
1069 if (!test_cflag(COMMIT_Nolink, ip)) {
1070 /* rxtlck->lwm.offset = XTENTRYSTART; */
1071 rxtlck->lwm.length = 1;
1077 jfs_info("xtSplitPage: sp:0x%p rp:0x%p", sp, rp);
1082 * non-sequential insert (at possibly middle page)
1086 * update previous pointer of old next/right page of <sp>
1089 XT_GETPAGE(ip, nextbn, mp, PSIZE, p, rc);
1095 BT_MARK_DIRTY(mp, ip);
1097 * acquire a transaction lock on the next page;
1099 * action:sibling pointer update;
1101 if (!test_cflag(COMMIT_Nolink, ip))
1102 tlck = txLock(tid, ip, mp, tlckXTREE | tlckRELINK);
1104 p->header.prev = cpu_to_le64(rbn);
1106 /* sibling page may have been updated previously, or
1107 * it may be updated later;
1114 * split the data between the split and new/right pages
1116 maxentry = le16_to_cpu(sp->header.maxentry);
1117 middle = maxentry >> 1;
1118 righthalf = maxentry - middle;
1121 * skip index in old split/left page - insert into left page:
1123 if (skip <= middle) {
1124 /* move right half of split page to the new right page */
1125 memmove(&rp->xad[XTENTRYSTART], &sp->xad[middle],
1126 righthalf << L2XTSLOTSIZE);
1128 /* shift right tail of left half to make room for new entry */
1130 memmove(&sp->xad[skip + 1], &sp->xad[skip],
1131 (middle - skip) << L2XTSLOTSIZE);
1133 /* insert new entry */
1134 xad = &sp->xad[skip];
1135 XT_PUTENTRY(xad, split->flag, split->off, split->len,
1138 /* update page header */
1139 sp->header.nextindex = cpu_to_le16(middle + 1);
1140 if (!test_cflag(COMMIT_Nolink, ip)) {
1141 sxtlck->lwm.offset = (sxtlck->lwm.offset) ?
1142 min(skip, (int)sxtlck->lwm.offset) : skip;
1145 rp->header.nextindex =
1146 cpu_to_le16(XTENTRYSTART + righthalf);
1149 * skip index in new right page - insert into right page:
1152 /* move left head of right half to right page */
1154 memmove(&rp->xad[XTENTRYSTART], &sp->xad[middle],
1157 /* insert new entry */
1160 XT_PUTENTRY(xad, split->flag, split->off, split->len,
1163 /* move right tail of right half to right page */
1164 if (skip < maxentry)
1165 memmove(&rp->xad[n + 1], &sp->xad[skip],
1166 (maxentry - skip) << L2XTSLOTSIZE);
1168 /* update page header */
1169 sp->header.nextindex = cpu_to_le16(middle);
1170 if (!test_cflag(COMMIT_Nolink, ip)) {
1171 sxtlck->lwm.offset = (sxtlck->lwm.offset) ?
1172 min(middle, (int)sxtlck->lwm.offset) : middle;
1175 rp->header.nextindex = cpu_to_le16(XTENTRYSTART +
1179 if (!test_cflag(COMMIT_Nolink, ip)) {
1180 sxtlck->lwm.length = le16_to_cpu(sp->header.nextindex) -
1183 /* rxtlck->lwm.offset = XTENTRYSTART; */
1184 rxtlck->lwm.length = le16_to_cpu(rp->header.nextindex) -
1191 jfs_info("xtSplitPage: sp:0x%p rp:0x%p", sp, rp);
1196 /* Rollback quota allocation. */
1197 if (quota_allocation)
1198 dquot_free_block(ip, quota_allocation);
1208 * split the full root page into original/root/split page and new
1210 * i.e., root remains fixed in tree anchor (inode) and the root is
1211 * copied to a single new right child page since root page <<
1212 * non-root page, and the split root page contains a single entry
1213 * for the new right child page.
1218 * struct xtsplit *split,
1219 * struct metapage **rmpp)
1222 * Pointer to page in which to insert or NULL on error.
1225 xtSplitRoot(tid_t tid,
1226 struct inode *ip, struct xtsplit * split, struct metapage ** rmpp)
1229 struct metapage *rmp;
1232 int skip, nextindex;
1235 struct pxdlist *pxdlist;
1237 struct xtlock *xtlck;
1240 sp = &JFS_IP(ip)->i_xtroot;
1242 INCREMENT(xtStat.split);
1245 * allocate a single (right) child page
1247 pxdlist = split->pxdlist;
1248 pxd = &pxdlist->pxd[pxdlist->npxd];
1250 rbn = addressPXD(pxd);
1251 rmp = get_metapage(ip, rbn, PSIZE, 1);
1255 /* Allocate blocks to quota. */
1256 rc = dquot_alloc_block(ip, lengthPXD(pxd));
1258 release_metapage(rmp);
1262 jfs_info("xtSplitRoot: ip:0x%p rmp:0x%p", ip, rmp);
1265 * acquire a transaction lock on the new right page;
1269 BT_MARK_DIRTY(rmp, ip);
1271 rp = (xtpage_t *) rmp->data;
1273 (sp->header.flag & BT_LEAF) ? BT_LEAF : BT_INTERNAL;
1274 rp->header.self = *pxd;
1275 rp->header.nextindex = cpu_to_le16(XTENTRYSTART);
1276 rp->header.maxentry = cpu_to_le16(PSIZE >> L2XTSLOTSIZE);
1278 /* initialize sibling pointers */
1279 rp->header.next = 0;
1280 rp->header.prev = 0;
1283 * copy the in-line root page into new right page extent
1285 nextindex = le16_to_cpu(sp->header.maxentry);
1286 memmove(&rp->xad[XTENTRYSTART], &sp->xad[XTENTRYSTART],
1287 (nextindex - XTENTRYSTART) << L2XTSLOTSIZE);
1290 * insert the new entry into the new right/child page
1291 * (skip index in the new right page will not change)
1293 skip = split->index;
1294 /* if insert into middle, shift right remaining entries */
1295 if (skip != nextindex)
1296 memmove(&rp->xad[skip + 1], &rp->xad[skip],
1297 (nextindex - skip) * sizeof(xad_t));
1299 xad = &rp->xad[skip];
1300 XT_PUTENTRY(xad, split->flag, split->off, split->len, split->addr);
1302 /* update page header */
1303 rp->header.nextindex = cpu_to_le16(nextindex + 1);
1305 if (!test_cflag(COMMIT_Nolink, ip)) {
1306 tlck = txLock(tid, ip, rmp, tlckXTREE | tlckNEW);
1307 xtlck = (struct xtlock *) & tlck->lock;
1308 xtlck->lwm.offset = XTENTRYSTART;
1309 xtlck->lwm.length = le16_to_cpu(rp->header.nextindex) -
1316 * init root with the single entry for the new right page
1317 * set the 1st entry offset to 0, which force the left-most key
1318 * at any level of the tree to be less than any search key.
1321 * acquire a transaction lock on the root page (in-memory inode);
1323 * action: root split;
1325 BT_MARK_DIRTY(split->mp, ip);
1327 xad = &sp->xad[XTENTRYSTART];
1328 XT_PUTENTRY(xad, XAD_NEW, 0, JFS_SBI(ip->i_sb)->nbperpage, rbn);
1330 /* update page header of root */
1331 sp->header.flag &= ~BT_LEAF;
1332 sp->header.flag |= BT_INTERNAL;
1334 sp->header.nextindex = cpu_to_le16(XTENTRYSTART + 1);
1336 if (!test_cflag(COMMIT_Nolink, ip)) {
1337 tlck = txLock(tid, ip, split->mp, tlckXTREE | tlckGROW);
1338 xtlck = (struct xtlock *) & tlck->lock;
1339 xtlck->lwm.offset = XTENTRYSTART;
1340 xtlck->lwm.length = 1;
1345 jfs_info("xtSplitRoot: sp:0x%p rp:0x%p", sp, rp);
1353 * function: extend in-place;
1355 * note: existing extent may or may not have been committed.
1356 * caller is responsible for pager buffer cache update, and
1357 * working block allocation map update;
1358 * update pmap: alloc whole extended extent;
1360 int xtExtend(tid_t tid, /* transaction id */
1361 struct inode *ip, s64 xoff, /* delta extent offset */
1362 s32 xlen, /* delta extent length */
1367 struct metapage *mp; /* meta-page buffer */
1368 xtpage_t *p; /* base B+-tree index page */
1370 int index, nextindex, len;
1371 struct btstack btstack; /* traverse stack */
1372 struct xtsplit split; /* split information */
1376 struct xtlock *xtlck = NULL;
1378 jfs_info("xtExtend: nxoff:0x%lx nxlen:0x%x", (ulong) xoff, xlen);
1380 /* there must exist extent to be extended */
1381 if ((rc = xtSearch(ip, xoff - 1, NULL, &cmp, &btstack, XT_INSERT)))
1384 /* retrieve search result */
1385 XT_GETSEARCH(ip, btstack.top, bn, mp, p, index);
1389 jfs_error(ip->i_sb, "xtSearch did not find extent\n");
1393 /* extension must be contiguous */
1394 xad = &p->xad[index];
1395 if ((offsetXAD(xad) + lengthXAD(xad)) != xoff) {
1397 jfs_error(ip->i_sb, "extension is not contiguous\n");
1402 * acquire a transaction lock on the leaf page;
1404 * action: xad insertion/extension;
1406 BT_MARK_DIRTY(mp, ip);
1407 if (!test_cflag(COMMIT_Nolink, ip)) {
1408 tlck = txLock(tid, ip, mp, tlckXTREE | tlckGROW);
1409 xtlck = (struct xtlock *) & tlck->lock;
1412 /* extend will overflow extent ? */
1413 xlen = lengthXAD(xad) + xlen;
1414 if ((len = xlen - MAXXLEN) <= 0)
1418 * extent overflow: insert entry for new extent
1421 xoff = offsetXAD(xad) + MAXXLEN;
1422 xaddr = addressXAD(xad) + MAXXLEN;
1423 nextindex = le16_to_cpu(p->header.nextindex);
1426 * if the leaf page is full, insert the new entry and
1427 * propagate up the router entry for the new page from split
1429 * The xtSplitUp() will insert the entry and unpin the leaf page.
1431 if (nextindex == le16_to_cpu(p->header.maxentry)) {
1432 /* xtSpliUp() unpins leaf pages */
1434 split.index = index + 1;
1435 split.flag = XAD_NEW;
1436 split.off = xoff; /* split offset */
1439 split.pxdlist = NULL;
1440 if ((rc = xtSplitUp(tid, ip, &split, &btstack)))
1443 /* get back old page */
1444 XT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
1448 * if leaf root has been split, original root has been
1449 * copied to new child page, i.e., original entry now
1450 * resides on the new child page;
1452 if (p->header.flag & BT_INTERNAL) {
1453 ASSERT(p->header.nextindex ==
1454 cpu_to_le16(XTENTRYSTART + 1));
1455 xad = &p->xad[XTENTRYSTART];
1456 bn = addressXAD(xad);
1459 /* get new child page */
1460 XT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
1464 BT_MARK_DIRTY(mp, ip);
1465 if (!test_cflag(COMMIT_Nolink, ip)) {
1466 tlck = txLock(tid, ip, mp, tlckXTREE|tlckGROW);
1467 xtlck = (struct xtlock *) & tlck->lock;
1472 * insert the new entry into the leaf page
1475 /* insert the new entry: mark the entry NEW */
1476 xad = &p->xad[index + 1];
1477 XT_PUTENTRY(xad, XAD_NEW, xoff, len, xaddr);
1479 /* advance next available entry index */
1480 le16_add_cpu(&p->header.nextindex, 1);
1483 /* get back old entry */
1484 xad = &p->xad[index];
1491 XADlength(xad, xlen);
1492 if (!(xad->flag & XAD_NEW))
1493 xad->flag |= XAD_EXTENDED;
1495 if (!test_cflag(COMMIT_Nolink, ip)) {
1497 (xtlck->lwm.offset) ? min(index,
1498 (int)xtlck->lwm.offset) : index;
1500 le16_to_cpu(p->header.nextindex) - xtlck->lwm.offset;
1503 /* unpin the leaf page */
1513 * function: split existing 'tail' extent
1514 * (split offset >= start offset of tail extent), and
1515 * relocate and extend the split tail half;
1517 * note: existing extent may or may not have been committed.
1518 * caller is responsible for pager buffer cache update, and
1519 * working block allocation map update;
1520 * update pmap: free old split tail extent, alloc new extent;
1522 int xtTailgate(tid_t tid, /* transaction id */
1523 struct inode *ip, s64 xoff, /* split/new extent offset */
1524 s32 xlen, /* new extent length */
1525 s64 xaddr, /* new extent address */
1530 struct metapage *mp; /* meta-page buffer */
1531 xtpage_t *p; /* base B+-tree index page */
1533 int index, nextindex, llen, rlen;
1534 struct btstack btstack; /* traverse stack */
1535 struct xtsplit split; /* split information */
1538 struct xtlock *xtlck = 0;
1539 struct tlock *mtlck;
1540 struct maplock *pxdlock;
1543 printf("xtTailgate: nxoff:0x%lx nxlen:0x%x nxaddr:0x%lx\n",
1544 (ulong)xoff, xlen, (ulong)xaddr);
1547 /* there must exist extent to be tailgated */
1548 if ((rc = xtSearch(ip, xoff, NULL, &cmp, &btstack, XT_INSERT)))
1551 /* retrieve search result */
1552 XT_GETSEARCH(ip, btstack.top, bn, mp, p, index);
1556 jfs_error(ip->i_sb, "couldn't find extent\n");
1560 /* entry found must be last entry */
1561 nextindex = le16_to_cpu(p->header.nextindex);
1562 if (index != nextindex - 1) {
1564 jfs_error(ip->i_sb, "the entry found is not the last entry\n");
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, "Could not find extent\n");
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 "nXAD in not completely contained within XAD\n");
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, "xoff >= nxoff\n");
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, "xtSearch failed\n");
2055 if (index0 != index) {
2057 jfs_error(ip->i_sb, "unexpected value of index\n");
2063 * split XAD into (nXAD, rXAD)
2066 * --|----------XAD----------|--
2069 updateLeft: /* (nxoff == xoff) && (nxlen < xlen) */
2070 /* update old XAD with nXAD:recorded */
2071 xad = &p->xad[index];
2073 xad->flag = xflag & ~XAD_NOTRECORDED;
2075 /* insert rXAD:not_recorded */
2076 xoff = xoff + nxlen;
2077 xlen = xlen - nxlen;
2078 xaddr = xaddr + nxlen;
2079 if (nextindex == le16_to_cpu(p->header.maxentry)) {
2081 printf("xtUpdate.updateLeft.split p:0x%p\n", p);
2083 /* xtSpliUp() unpins leaf pages */
2085 split.index = newindex;
2090 split.pxdlist = NULL;
2091 if ((rc = xtSplitUp(tid, ip, &split, &btstack)))
2094 /* get back old page */
2095 XT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
2100 * if leaf root has been split, original root has been
2101 * copied to new child page, i.e., original entry now
2102 * resides on the new child page;
2104 if (p->header.flag & BT_INTERNAL) {
2105 ASSERT(p->header.nextindex ==
2106 cpu_to_le16(XTENTRYSTART + 1));
2107 xad = &p->xad[XTENTRYSTART];
2108 bn = addressXAD(xad);
2111 /* get new child page */
2112 XT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
2116 BT_MARK_DIRTY(mp, ip);
2117 if (!test_cflag(COMMIT_Nolink, ip)) {
2118 tlck = txLock(tid, ip, mp, tlckXTREE|tlckGROW);
2119 xtlck = (struct xtlock *) & tlck->lock;
2123 /* if insert into middle, shift right remaining entries */
2124 if (newindex < nextindex)
2125 memmove(&p->xad[newindex + 1], &p->xad[newindex],
2126 (nextindex - newindex) << L2XTSLOTSIZE);
2128 /* insert the entry */
2129 xad = &p->xad[newindex];
2130 XT_PUTENTRY(xad, xflag, xoff, xlen, xaddr);
2132 /* advance next available entry index. */
2133 p->header.nextindex =
2134 cpu_to_le16(le16_to_cpu(p->header.nextindex) + 1);
2138 if (!test_cflag(COMMIT_Nolink, ip)) {
2139 xtlck->lwm.offset = (xtlck->lwm.offset) ?
2140 min(index0, (int)xtlck->lwm.offset) : index0;
2141 xtlck->lwm.length = le16_to_cpu(p->header.nextindex) -
2145 /* unpin the leaf page */
2155 * function: grow in append mode from contiguous region specified ;
2158 * tid - transaction id;
2160 * xflag - extent flag:
2161 * xoff - extent offset;
2162 * maxblocks - max extent length;
2163 * xlen - extent length (in/out);
2164 * xaddrp - extent address pointer (in/out):
2169 int xtAppend(tid_t tid, /* transaction id */
2170 struct inode *ip, int xflag, s64 xoff, s32 maxblocks,
2171 s32 * xlenp, /* (in/out) */
2172 s64 * xaddrp, /* (in/out) */
2176 struct metapage *mp; /* meta-page buffer */
2177 xtpage_t *p; /* base B+-tree index page */
2179 int index, nextindex;
2180 struct btstack btstack; /* traverse stack */
2181 struct xtsplit split; /* split information */
2185 struct xtlock *xtlck;
2186 int nsplit, nblocks, xlen;
2187 struct pxdlist pxdlist;
2193 jfs_info("xtAppend: xoff:0x%lx maxblocks:%d xlen:%d xaddr:0x%lx",
2194 (ulong) xoff, maxblocks, xlen, (ulong) xaddr);
2197 * search for the entry location at which to insert:
2199 * xtFastSearch() and xtSearch() both returns (leaf page
2200 * pinned, index at which to insert).
2201 * n.b. xtSearch() may return index of maxentry of
2204 if ((rc = xtSearch(ip, xoff, &next, &cmp, &btstack, XT_INSERT)))
2207 /* retrieve search result */
2208 XT_GETSEARCH(ip, btstack.top, bn, mp, p, index);
2216 xlen = min(xlen, (int)(next - xoff));
2219 * insert entry for new extent
2224 * if the leaf page is full, split the page and
2225 * propagate up the router entry for the new page from split
2227 * The xtSplitUp() will insert the entry and unpin the leaf page.
2229 nextindex = le16_to_cpu(p->header.nextindex);
2230 if (nextindex < le16_to_cpu(p->header.maxentry))
2234 * allocate new index blocks to cover index page split(s)
2236 nsplit = btstack.nsplit;
2237 split.pxdlist = &pxdlist;
2238 pxdlist.maxnpxd = pxdlist.npxd = 0;
2239 pxd = &pxdlist.pxd[0];
2240 nblocks = JFS_SBI(ip->i_sb)->nbperpage;
2241 for (; nsplit > 0; nsplit--, pxd++, xaddr += nblocks, maxblocks -= nblocks) {
2242 if ((rc = dbAllocBottomUp(ip, xaddr, (s64) nblocks)) == 0) {
2243 PXDaddress(pxd, xaddr);
2244 PXDlength(pxd, nblocks);
2251 /* undo allocation */
2256 xlen = min(xlen, maxblocks);
2259 * allocate data extent requested
2261 if ((rc = dbAllocBottomUp(ip, xaddr, (s64) xlen)))
2265 split.index = index;
2270 if ((rc = xtSplitUp(tid, ip, &split, &btstack))) {
2271 /* undo data extent allocation */
2272 dbFree(ip, *xaddrp, (s64) * xlenp);
2282 * insert the new entry into the leaf page
2286 * allocate data extent requested
2288 if ((rc = dbAllocBottomUp(ip, xaddr, (s64) xlen)))
2291 BT_MARK_DIRTY(mp, ip);
2293 * acquire a transaction lock on the leaf page;
2295 * action: xad insertion/extension;
2297 tlck = txLock(tid, ip, mp, tlckXTREE | tlckGROW);
2298 xtlck = (struct xtlock *) & tlck->lock;
2300 /* insert the new entry: mark the entry NEW */
2301 xad = &p->xad[index];
2302 XT_PUTENTRY(xad, xflag, xoff, xlen, xaddr);
2304 /* advance next available entry index */
2305 le16_add_cpu(&p->header.nextindex, 1);
2308 (xtlck->lwm.offset) ? min(index,(int) xtlck->lwm.offset) : index;
2309 xtlck->lwm.length = le16_to_cpu(p->header.nextindex) -
2316 /* unpin the leaf page */
2321 #ifdef _STILL_TO_PORT
2323 /* - TBD for defragmentaion/reorganization -
2328 * delete the entry with the specified key.
2330 * N.B.: whole extent of the entry is assumed to be deleted.
2335 * ENOENT: if the entry is not found.
2339 int xtDelete(tid_t tid, struct inode *ip, s64 xoff, s32 xlen, int flag)
2342 struct btstack btstack;
2345 struct metapage *mp;
2347 int index, nextindex;
2349 struct xtlock *xtlck;
2352 * find the matching entry; xtSearch() pins the page
2354 if ((rc = xtSearch(ip, xoff, NULL, &cmp, &btstack, 0)))
2357 XT_GETSEARCH(ip, btstack.top, bn, mp, p, index);
2359 /* unpin the leaf page */
2365 * delete the entry from the leaf page
2367 nextindex = le16_to_cpu(p->header.nextindex);
2368 le16_add_cpu(&p->header.nextindex, -1);
2371 * if the leaf page bocome empty, free the page
2373 if (p->header.nextindex == cpu_to_le16(XTENTRYSTART))
2374 return (xtDeleteUp(tid, ip, mp, p, &btstack));
2376 BT_MARK_DIRTY(mp, ip);
2378 * acquire a transaction lock on the leaf page;
2380 * action:xad deletion;
2382 tlck = txLock(tid, ip, mp, tlckXTREE);
2383 xtlck = (struct xtlock *) & tlck->lock;
2385 (xtlck->lwm.offset) ? min(index, xtlck->lwm.offset) : index;
2387 /* if delete from middle, shift left/compact the remaining entries */
2388 if (index < nextindex - 1)
2389 memmove(&p->xad[index], &p->xad[index + 1],
2390 (nextindex - index - 1) * sizeof(xad_t));
2398 /* - TBD for defragmentaion/reorganization -
2403 * free empty pages as propagating deletion up the tree
2410 xtDeleteUp(tid_t tid, struct inode *ip,
2411 struct metapage * fmp, xtpage_t * fp, struct btstack * btstack)
2414 struct metapage *mp;
2416 int index, nextindex;
2419 struct btframe *parent;
2421 struct xtlock *xtlck;
2424 * keep root leaf page which has become empty
2426 if (fp->header.flag & BT_ROOT) {
2427 /* keep the root page */
2428 fp->header.flag &= ~BT_INTERNAL;
2429 fp->header.flag |= BT_LEAF;
2430 fp->header.nextindex = cpu_to_le16(XTENTRYSTART);
2432 /* XT_PUTPAGE(fmp); */
2438 * free non-root leaf page
2440 if ((rc = xtRelink(tid, ip, fp))) {
2445 xaddr = addressPXD(&fp->header.self);
2446 xlen = lengthPXD(&fp->header.self);
2447 /* free the page extent */
2448 dbFree(ip, xaddr, (s64) xlen);
2450 /* free the buffer page */
2451 discard_metapage(fmp);
2454 * propagate page deletion up the index tree
2456 * If the delete from the parent page makes it empty,
2457 * continue all the way up the tree.
2458 * stop if the root page is reached (which is never deleted) or
2459 * if the entry deletion does not empty the page.
2461 while ((parent = BT_POP(btstack)) != NULL) {
2462 /* get/pin the parent page <sp> */
2463 XT_GETPAGE(ip, parent->bn, mp, PSIZE, p, rc);
2467 index = parent->index;
2469 /* delete the entry for the freed child page from parent.
2471 nextindex = le16_to_cpu(p->header.nextindex);
2474 * the parent has the single entry being deleted:
2475 * free the parent page which has become empty.
2477 if (nextindex == 1) {
2478 if (p->header.flag & BT_ROOT) {
2479 /* keep the root page */
2480 p->header.flag &= ~BT_INTERNAL;
2481 p->header.flag |= BT_LEAF;
2482 p->header.nextindex =
2483 cpu_to_le16(XTENTRYSTART);
2485 /* XT_PUTPAGE(mp); */
2489 /* free the parent page */
2490 if ((rc = xtRelink(tid, ip, p)))
2493 xaddr = addressPXD(&p->header.self);
2494 /* free the page extent */
2496 (s64) JFS_SBI(ip->i_sb)->nbperpage);
2498 /* unpin/free the buffer page */
2499 discard_metapage(mp);
2506 * the parent has other entries remaining:
2507 * delete the router entry from the parent page.
2510 BT_MARK_DIRTY(mp, ip);
2512 * acquire a transaction lock on the leaf page;
2514 * action:xad deletion;
2516 tlck = txLock(tid, ip, mp, tlckXTREE);
2517 xtlck = (struct xtlock *) & tlck->lock;
2519 (xtlck->lwm.offset) ? min(index,
2523 /* if delete from middle,
2524 * shift left/compact the remaining entries in the page
2526 if (index < nextindex - 1)
2527 memmove(&p->xad[index], &p->xad[index + 1],
2528 (nextindex - index -
2529 1) << L2XTSLOTSIZE);
2531 le16_add_cpu(&p->header.nextindex, -1);
2532 jfs_info("xtDeleteUp(entry): 0x%lx[%d]",
2533 (ulong) parent->bn, index);
2536 /* unpin the parent page */
2539 /* exit propagation up */
2548 * NAME: xtRelocate()
2550 * FUNCTION: relocate xtpage or data extent of regular file;
2551 * This function is mainly used by defragfs utility.
2553 * NOTE: This routine does not have the logic to handle
2554 * uncommitted allocated extent. The caller should call
2555 * txCommit() to commit all the allocation before call
2559 xtRelocate(tid_t tid, struct inode * ip, xad_t * oxad, /* old XAD */
2560 s64 nxaddr, /* new xaddr */
2562 { /* extent type: XTPAGE or DATAEXT */
2564 struct tblock *tblk;
2566 struct xtlock *xtlck;
2567 struct metapage *mp, *pmp, *lmp, *rmp; /* meta-page buffer */
2568 xtpage_t *p, *pp, *rp, *lp; /* base B+-tree index page */
2573 s64 oxaddr, sxaddr, dxaddr, nextbn, prevbn;
2575 s64 offset, nbytes, nbrd, pno;
2576 int nb, npages, nblks;
2580 struct pxd_lock *pxdlock;
2581 struct btstack btstack; /* traverse stack */
2583 xtype = xtype & EXTENT_TYPE;
2585 xoff = offsetXAD(oxad);
2586 oxaddr = addressXAD(oxad);
2587 xlen = lengthXAD(oxad);
2589 /* validate extent offset */
2590 offset = xoff << JFS_SBI(ip->i_sb)->l2bsize;
2591 if (offset >= ip->i_size)
2592 return -ESTALE; /* stale extent */
2594 jfs_info("xtRelocate: xtype:%d xoff:0x%lx xlen:0x%x xaddr:0x%lx:0x%lx",
2595 xtype, (ulong) xoff, xlen, (ulong) oxaddr, (ulong) nxaddr);
2598 * 1. get and validate the parent xtpage/xad entry
2599 * covering the source extent to be relocated;
2601 if (xtype == DATAEXT) {
2602 /* search in leaf entry */
2603 rc = xtSearch(ip, xoff, NULL, &cmp, &btstack, 0);
2607 /* retrieve search result */
2608 XT_GETSEARCH(ip, btstack.top, bn, pmp, pp, index);
2615 /* validate for exact match with a single entry */
2616 xad = &pp->xad[index];
2617 if (addressXAD(xad) != oxaddr || lengthXAD(xad) != xlen) {
2621 } else { /* (xtype == XTPAGE) */
2623 /* search in internal entry */
2624 rc = xtSearchNode(ip, oxad, &cmp, &btstack, 0);
2628 /* retrieve search result */
2629 XT_GETSEARCH(ip, btstack.top, bn, pmp, pp, index);
2636 /* xtSearchNode() validated for exact match with a single entry
2638 xad = &pp->xad[index];
2640 jfs_info("xtRelocate: parent xad entry validated.");
2643 * 2. relocate the extent
2645 if (xtype == DATAEXT) {
2646 /* if the extent is allocated-but-not-recorded
2647 * there is no real data to be moved in this extent,
2649 if (xad->flag & XAD_NOTRECORDED)
2652 /* release xtpage for cmRead()/xtLookup() */
2658 * copy target data pages to be relocated;
2660 * data extent must start at page boundary and
2661 * multiple of page size (except the last data extent);
2662 * read in each page of the source data extent into cbuf,
2663 * update the cbuf extent descriptor of the page to be
2664 * homeward bound to new dst data extent
2665 * copy the data from the old extent to new extent.
2666 * copy is essential for compressed files to avoid problems
2667 * that can arise if there was a change in compression
2669 * it is a good strategy because it may disrupt cache
2670 * policy to keep the pages in memory afterwards.
2672 offset = xoff << JFS_SBI(ip->i_sb)->l2bsize;
2673 assert((offset & CM_OFFSET) == 0);
2674 nbytes = xlen << JFS_SBI(ip->i_sb)->l2bsize;
2675 pno = offset >> CM_L2BSIZE;
2676 npages = (nbytes + (CM_BSIZE - 1)) >> CM_L2BSIZE;
2678 npages = ((offset + nbytes - 1) >> CM_L2BSIZE) -
2679 (offset >> CM_L2BSIZE) + 1;
2684 /* process the request one cache buffer at a time */
2685 for (nbrd = 0; nbrd < nbytes; nbrd += nb,
2686 offset += nb, pno++, npages--) {
2687 /* compute page size */
2688 nb = min(nbytes - nbrd, CM_BSIZE);
2690 /* get the cache buffer of the page */
2691 if (rc = cmRead(ip, offset, npages, &cp))
2694 assert(addressPXD(&cp->cm_pxd) == sxaddr);
2695 assert(!cp->cm_modified);
2697 /* bind buffer with the new extent address */
2698 nblks = nb >> JFS_IP(ip->i_sb)->l2bsize;
2699 cmSetXD(ip, cp, pno, dxaddr, nblks);
2701 /* release the cbuf, mark it as modified */
2708 /* get back parent page */
2709 if ((rc = xtSearch(ip, xoff, NULL, &cmp, &btstack, 0)))
2712 XT_GETSEARCH(ip, btstack.top, bn, pmp, pp, index);
2713 jfs_info("xtRelocate: target data extent relocated.");
2714 } else { /* (xtype == XTPAGE) */
2717 * read in the target xtpage from the source extent;
2719 XT_GETPAGE(ip, oxaddr, mp, PSIZE, p, rc);
2726 * read in sibling pages if any to update sibling pointers;
2729 if (p->header.next) {
2730 nextbn = le64_to_cpu(p->header.next);
2731 XT_GETPAGE(ip, nextbn, rmp, PSIZE, rp, rc);
2740 if (p->header.prev) {
2741 prevbn = le64_to_cpu(p->header.prev);
2742 XT_GETPAGE(ip, prevbn, lmp, PSIZE, lp, rc);
2752 /* at this point, all xtpages to be updated are in memory */
2755 * update sibling pointers of sibling xtpages if any;
2758 BT_MARK_DIRTY(lmp, ip);
2759 tlck = txLock(tid, ip, lmp, tlckXTREE | tlckRELINK);
2760 lp->header.next = cpu_to_le64(nxaddr);
2765 BT_MARK_DIRTY(rmp, ip);
2766 tlck = txLock(tid, ip, rmp, tlckXTREE | tlckRELINK);
2767 rp->header.prev = cpu_to_le64(nxaddr);
2772 * update the target xtpage to be relocated
2774 * update the self address of the target page
2775 * and write to destination extent;
2776 * redo image covers the whole xtpage since it is new page
2777 * to the destination extent;
2778 * update of bmap for the free of source extent
2779 * of the target xtpage itself:
2780 * update of bmap for the allocation of destination extent
2781 * of the target xtpage itself:
2782 * update of bmap for the extents covered by xad entries in
2783 * the target xtpage is not necessary since they are not
2785 * if not committed before this relocation,
2786 * target page may contain XAD_NEW entries which must
2787 * be scanned for bmap update (logredo() always
2788 * scan xtpage REDOPAGE image for bmap update);
2789 * if committed before this relocation (tlckRELOCATE),
2790 * scan may be skipped by commit() and logredo();
2792 BT_MARK_DIRTY(mp, ip);
2793 /* tlckNEW init xtlck->lwm.offset = XTENTRYSTART; */
2794 tlck = txLock(tid, ip, mp, tlckXTREE | tlckNEW);
2795 xtlck = (struct xtlock *) & tlck->lock;
2797 /* update the self address in the xtpage header */
2798 pxd = &p->header.self;
2799 PXDaddress(pxd, nxaddr);
2801 /* linelock for the after image of the whole page */
2803 le16_to_cpu(p->header.nextindex) - xtlck->lwm.offset;
2805 /* update the buffer extent descriptor of target xtpage */
2806 xsize = xlen << JFS_SBI(ip->i_sb)->l2bsize;
2807 bmSetXD(mp, nxaddr, xsize);
2809 /* unpin the target page to new homeward bound */
2811 jfs_info("xtRelocate: target xtpage relocated.");
2815 * 3. acquire maplock for the source extent to be freed;
2817 * acquire a maplock saving the src relocated extent address;
2818 * to free of the extent at commit time;
2821 /* if DATAEXT relocation, write a LOG_UPDATEMAP record for
2822 * free PXD of the source data extent (logredo() will update
2823 * bmap for free of source data extent), and update bmap for
2824 * free of the source data extent;
2826 if (xtype == DATAEXT)
2827 tlck = txMaplock(tid, ip, tlckMAP);
2828 /* if XTPAGE relocation, write a LOG_NOREDOPAGE record
2829 * for the source xtpage (logredo() will init NoRedoPage
2830 * filter and will also update bmap for free of the source
2831 * xtpage), and update bmap for free of the source xtpage;
2832 * N.B. We use tlckMAP instead of tlkcXTREE because there
2833 * is no buffer associated with this lock since the buffer
2834 * has been redirected to the target location.
2836 else /* (xtype == XTPAGE) */
2837 tlck = txMaplock(tid, ip, tlckMAP | tlckRELOCATE);
2839 pxdlock = (struct pxd_lock *) & tlck->lock;
2840 pxdlock->flag = mlckFREEPXD;
2841 PXDaddress(&pxdlock->pxd, oxaddr);
2842 PXDlength(&pxdlock->pxd, xlen);
2846 * 4. update the parent xad entry for relocation;
2848 * acquire tlck for the parent entry with XAD_NEW as entry
2849 * update which will write LOG_REDOPAGE and update bmap for
2850 * allocation of XAD_NEW destination extent;
2852 jfs_info("xtRelocate: update parent xad entry.");
2853 BT_MARK_DIRTY(pmp, ip);
2854 tlck = txLock(tid, ip, pmp, tlckXTREE | tlckGROW);
2855 xtlck = (struct xtlock *) & tlck->lock;
2857 /* update the XAD with the new destination extent; */
2858 xad = &pp->xad[index];
2859 xad->flag |= XAD_NEW;
2860 XADaddress(xad, nxaddr);
2862 xtlck->lwm.offset = min(index, xtlck->lwm.offset);
2863 xtlck->lwm.length = le16_to_cpu(pp->header.nextindex) -
2866 /* unpin the parent xtpage */
2876 * function: search for the internal xad entry covering specified extent.
2877 * This function is mainly used by defragfs utility.
2881 * xad - extent to find;
2882 * cmpp - comparison result:
2883 * btstack - traverse stack;
2884 * flag - search process flag;
2887 * btstack contains (bn, index) of search path traversed to the entry.
2888 * *cmpp is set to result of comparison with the entry returned.
2889 * the page containing the entry is pinned at exit.
2891 static int xtSearchNode(struct inode *ip, xad_t * xad, /* required XAD entry */
2892 int *cmpp, struct btstack * btstack, int flag)
2897 int cmp = 1; /* init for empty page */
2898 s64 bn; /* block number */
2899 struct metapage *mp; /* meta-page buffer */
2900 xtpage_t *p; /* page */
2901 int base, index, lim;
2902 struct btframe *btsp;
2907 xoff = offsetXAD(xad);
2908 xlen = lengthXAD(xad);
2909 xaddr = addressXAD(xad);
2912 * search down tree from root:
2914 * between two consecutive entries of <Ki, Pi> and <Kj, Pj> of
2915 * internal page, child page Pi contains entry with k, Ki <= K < Kj.
2917 * if entry with search key K is not found
2918 * internal page search find the entry with largest key Ki
2919 * less than K which point to the child page to search;
2920 * leaf page search find the entry with smallest key Kj
2921 * greater than K so that the returned index is the position of
2922 * the entry to be shifted right for insertion of new entry.
2923 * for empty tree, search key is greater than any key of the tree.
2925 * by convention, root bn = 0.
2928 /* get/pin the page to search */
2929 XT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
2932 if (p->header.flag & BT_LEAF) {
2937 lim = le16_to_cpu(p->header.nextindex) - XTENTRYSTART;
2940 * binary search with search key K on the current page
2942 for (base = XTENTRYSTART; lim; lim >>= 1) {
2943 index = base + (lim >> 1);
2945 XT_CMP(cmp, xoff, &p->xad[index], t64);
2950 * verify for exact match;
2952 if (xaddr == addressXAD(&p->xad[index]) &&
2953 xoff == offsetXAD(&p->xad[index])) {
2956 /* save search result */
2957 btsp = btstack->top;
2959 btsp->index = index;
2965 /* descend/search its child page */
2976 * search miss - non-leaf page:
2978 * base is the smallest index with key (Kj) greater than
2979 * search key (K) and may be zero or maxentry index.
2980 * if base is non-zero, decrement base by one to get the parent
2981 * entry of the child page to search.
2983 index = base ? base - 1 : base;
2986 * go down to child page
2989 /* get the child page block number */
2990 bn = addressXAD(&p->xad[index]);
2992 /* unpin the parent page */
3002 * link around a freed page.
3011 static int xtRelink(tid_t tid, struct inode *ip, xtpage_t * p)
3014 struct metapage *mp;
3018 nextbn = le64_to_cpu(p->header.next);
3019 prevbn = le64_to_cpu(p->header.prev);
3021 /* update prev pointer of the next page */
3023 XT_GETPAGE(ip, nextbn, mp, PSIZE, p, rc);
3028 * acquire a transaction lock on the page;
3030 * action: update prev pointer;
3032 BT_MARK_DIRTY(mp, ip);
3033 tlck = txLock(tid, ip, mp, tlckXTREE | tlckRELINK);
3035 /* the page may already have been tlock'd */
3037 p->header.prev = cpu_to_le64(prevbn);
3042 /* update next pointer of the previous page */
3044 XT_GETPAGE(ip, prevbn, mp, PSIZE, p, rc);
3049 * acquire a transaction lock on the page;
3051 * action: update next pointer;
3053 BT_MARK_DIRTY(mp, ip);
3054 tlck = txLock(tid, ip, mp, tlckXTREE | tlckRELINK);
3056 /* the page may already have been tlock'd */
3058 p->header.next = le64_to_cpu(nextbn);
3065 #endif /* _STILL_TO_PORT */
3071 * initialize file root (inline in inode)
3073 void xtInitRoot(tid_t tid, struct inode *ip)
3078 * acquire a transaction lock on the root
3082 txLock(tid, ip, (struct metapage *) &JFS_IP(ip)->bxflag,
3083 tlckXTREE | tlckNEW);
3084 p = &JFS_IP(ip)->i_xtroot;
3086 p->header.flag = DXD_INDEX | BT_ROOT | BT_LEAF;
3087 p->header.nextindex = cpu_to_le16(XTENTRYSTART);
3089 if (S_ISDIR(ip->i_mode))
3090 p->header.maxentry = cpu_to_le16(XTROOTINITSLOT_DIR);
3092 p->header.maxentry = cpu_to_le16(XTROOTINITSLOT);
3102 * We can run into a deadlock truncating a file with a large number of
3103 * xtree pages (large fragmented file). A robust fix would entail a
3104 * reservation system where we would reserve a number of metadata pages
3105 * and tlocks which we would be guaranteed without a deadlock. Without
3106 * this, a partial fix is to limit number of metadata pages we will lock
3107 * in a single transaction. Currently we will truncate the file so that
3108 * no more than 50 leaf pages will be locked. The caller of xtTruncate
3109 * will be responsible for ensuring that the current transaction gets
3110 * committed, and that subsequent transactions are created to truncate
3111 * the file further if needed.
3113 #define MAX_TRUNCATE_LEAVES 50
3119 * traverse for truncation logging backward bottom up;
3120 * terminate at the last extent entry at the current subtree
3121 * root page covering new down size.
3122 * truncation may occur within the last extent entry.
3128 * int type) {PWMAP, PMAP, WMAP; DELETE, TRUNCATE}
3134 * 1. truncate (non-COMMIT_NOLINK file)
3135 * by jfs_truncate() or jfs_open(O_TRUNC):
3137 * 2. truncate index table of directory when last entry removed
3138 * map update via tlock at commit time;
3140 * Call xtTruncate_pmap instead
3142 * 1. remove (free zero link count) on last reference release
3143 * (pmap has been freed at commit zero link count);
3144 * 2. truncate (COMMIT_NOLINK file, i.e., tmp file):
3146 * map update directly at truncation time;
3149 * no LOG_NOREDOPAGE is required (NOREDOFILE is sufficient);
3150 * else if (TRUNCATE)
3151 * must write LOG_NOREDOPAGE for deleted index page;
3153 * pages may already have been tlocked by anonymous transactions
3154 * during file growth (i.e., write) before truncation;
3156 * except last truncated entry, deleted entries remains as is
3157 * in the page (nextindex is updated) for other use
3158 * (e.g., log/update allocation map): this avoid copying the page
3159 * info but delay free of pages;
3162 s64 xtTruncate(tid_t tid, struct inode *ip, s64 newsize, int flag)
3166 struct metapage *mp;
3169 int index, nextindex;
3172 int xlen, len, freexlen;
3173 struct btstack btstack;
3174 struct btframe *parent;
3175 struct tblock *tblk = NULL;
3176 struct tlock *tlck = NULL;
3177 struct xtlock *xtlck = NULL;
3178 struct xdlistlock xadlock; /* maplock for COMMIT_WMAP */
3179 struct pxd_lock *pxdlock; /* maplock for COMMIT_WMAP */
3182 int locked_leaves = 0;
3184 /* save object truncation type */
3186 tblk = tid_to_tblock(tid);
3187 tblk->xflag |= flag;
3193 assert(flag != COMMIT_PMAP);
3195 if (flag == COMMIT_PWMAP)
3199 xadlock.flag = mlckFREEXADLIST;
3204 * if the newsize is not an integral number of pages,
3205 * the file between newsize and next page boundary will
3207 * if truncating into a file hole, it will cause
3208 * a full block to be allocated for the logical block.
3212 * release page blocks of truncated region <teof, eof>
3214 * free the data blocks from the leaf index blocks.
3215 * delete the parent index entries corresponding to
3216 * the freed child data/index blocks.
3217 * free the index blocks themselves which aren't needed
3218 * in new sized file.
3220 * index blocks are updated only if the blocks are to be
3221 * retained in the new sized file.
3222 * if type is PMAP, the data and index pages are NOT
3223 * freed, and the data and index blocks are NOT freed
3225 * (this will allow continued access of data/index of
3226 * temporary file (zerolink count file truncated to zero-length)).
3228 teof = (newsize + (JFS_SBI(ip->i_sb)->bsize - 1)) >>
3229 JFS_SBI(ip->i_sb)->l2bsize;
3237 * root resides in the inode
3242 * first access of each page:
3245 XT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
3249 /* process entries backward from last index */
3250 index = le16_to_cpu(p->header.nextindex) - 1;
3253 /* Since this is the rightmost page at this level, and we may have
3254 * already freed a page that was formerly to the right, let's make
3255 * sure that the next pointer is zero.
3257 if (p->header.next) {
3260 * Make sure this change to the header is logged.
3261 * If we really truncate this leaf, the flag
3262 * will be changed to tlckTRUNCATE
3264 tlck = txLock(tid, ip, mp, tlckXTREE|tlckGROW);
3265 BT_MARK_DIRTY(mp, ip);
3269 if (p->header.flag & BT_INTERNAL)
3277 /* does region covered by leaf page precede Teof ? */
3278 xad = &p->xad[index];
3279 xoff = offsetXAD(xad);
3280 xlen = lengthXAD(xad);
3281 if (teof >= xoff + xlen) {
3286 /* (re)acquire tlock of the leaf page */
3288 if (++locked_leaves > MAX_TRUNCATE_LEAVES) {
3290 * We need to limit the size of the transaction
3291 * to avoid exhausting pagecache & tlocks
3294 newsize = (xoff + xlen) << JFS_SBI(ip->i_sb)->l2bsize;
3297 tlck = txLock(tid, ip, mp, tlckXTREE);
3298 tlck->type = tlckXTREE | tlckTRUNCATE;
3299 xtlck = (struct xtlock *) & tlck->lock;
3300 xtlck->hwm.offset = le16_to_cpu(p->header.nextindex) - 1;
3302 BT_MARK_DIRTY(mp, ip);
3305 * scan backward leaf page entries
3307 for (; index >= XTENTRYSTART; index--) {
3308 xad = &p->xad[index];
3309 xoff = offsetXAD(xad);
3310 xlen = lengthXAD(xad);
3311 xaddr = addressXAD(xad);
3314 * The "data" for a directory is indexed by the block
3315 * device's address space. This metadata must be invalidated
3318 if (S_ISDIR(ip->i_mode) && (teof == 0))
3319 invalidate_xad_metapages(ip, *xad);
3321 * entry beyond eof: continue scan of current page
3323 * ---|---=======------->
3332 * (xoff <= teof): last entry to be deleted from page;
3333 * If other entries remain in page: keep and update the page.
3337 * eof == entry_start: delete the entry
3339 * -------|=======------->
3346 if (index == XTENTRYSTART)
3352 * eof within the entry: truncate the entry.
3354 * -------===|===------->
3357 else if (teof < xoff + xlen) {
3358 /* update truncated entry */
3360 freexlen = xlen - len;
3361 XADlength(xad, len);
3363 /* save pxd of truncated extent in tlck */
3365 if (log) { /* COMMIT_PWMAP */
3366 xtlck->lwm.offset = (xtlck->lwm.offset) ?
3367 min(index, (int)xtlck->lwm.offset) : index;
3368 xtlck->lwm.length = index + 1 -
3370 xtlck->twm.offset = index;
3371 pxdlock = (struct pxd_lock *) & xtlck->pxdlock;
3372 pxdlock->flag = mlckFREEPXD;
3373 PXDaddress(&pxdlock->pxd, xaddr);
3374 PXDlength(&pxdlock->pxd, freexlen);
3376 /* free truncated extent */
3377 else { /* COMMIT_WMAP */
3379 pxdlock = (struct pxd_lock *) & xadlock;
3380 pxdlock->flag = mlckFREEPXD;
3381 PXDaddress(&pxdlock->pxd, xaddr);
3382 PXDlength(&pxdlock->pxd, freexlen);
3383 txFreeMap(ip, pxdlock, NULL, COMMIT_WMAP);
3385 /* reset map lock */
3386 xadlock.flag = mlckFREEXADLIST;
3389 /* current entry is new last entry; */
3390 nextindex = index + 1;
3395 * eof beyond the entry:
3397 * -------=======---|--->
3400 else { /* (xoff + xlen < teof) */
3402 nextindex = index + 1;
3405 if (nextindex < le16_to_cpu(p->header.nextindex)) {
3406 if (!log) { /* COMMIT_WAMP */
3407 xadlock.xdlist = &p->xad[nextindex];
3409 le16_to_cpu(p->header.nextindex) -
3411 txFreeMap(ip, (struct maplock *) & xadlock,
3414 p->header.nextindex = cpu_to_le16(nextindex);
3419 /* assert(freed == 0); */
3421 } /* end scan of leaf page entries */
3426 * leaf page become empty: free the page if type != PMAP
3428 if (log) { /* COMMIT_PWMAP */
3429 /* txCommit() with tlckFREE:
3430 * free data extents covered by leaf [XTENTRYSTART:hwm);
3431 * invalidate leaf if COMMIT_PWMAP;
3432 * if (TRUNCATE), will write LOG_NOREDOPAGE;
3434 tlck->type = tlckXTREE | tlckFREE;
3435 } else { /* COMMIT_WAMP */
3437 /* free data extents covered by leaf */
3438 xadlock.xdlist = &p->xad[XTENTRYSTART];
3440 le16_to_cpu(p->header.nextindex) - XTENTRYSTART;
3441 txFreeMap(ip, (struct maplock *) & xadlock, NULL, COMMIT_WMAP);
3444 if (p->header.flag & BT_ROOT) {
3445 p->header.flag &= ~BT_INTERNAL;
3446 p->header.flag |= BT_LEAF;
3447 p->header.nextindex = cpu_to_le16(XTENTRYSTART);
3449 XT_PUTPAGE(mp); /* debug */
3452 if (log) { /* COMMIT_PWMAP */
3453 /* page will be invalidated at tx completion
3456 } else { /* COMMIT_WMAP */
3459 lid_to_tlock(mp->lid)->flag |= tlckFREELOCK;
3461 /* invalidate empty leaf page */
3462 discard_metapage(mp);
3467 * the leaf page become empty: delete the parent entry
3468 * for the leaf page if the parent page is to be kept
3469 * in the new sized file.
3473 * go back up to the parent page
3476 /* pop/restore parent entry for the current child page */
3477 if ((parent = BT_POP(&btstack)) == NULL)
3478 /* current page must have been root */
3481 /* get back the parent page */
3483 XT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
3487 index = parent->index;
3490 * child page was not empty:
3493 /* has any entry deleted from parent ? */
3494 if (index < le16_to_cpu(p->header.nextindex) - 1) {
3495 /* (re)acquire tlock on the parent page */
3496 if (log) { /* COMMIT_PWMAP */
3497 /* txCommit() with tlckTRUNCATE:
3498 * free child extents covered by parent [);
3500 tlck = txLock(tid, ip, mp, tlckXTREE);
3501 xtlck = (struct xtlock *) & tlck->lock;
3502 if (!(tlck->type & tlckTRUNCATE)) {
3504 le16_to_cpu(p->header.
3507 tlckXTREE | tlckTRUNCATE;
3509 } else { /* COMMIT_WMAP */
3511 /* free child extents covered by parent */
3512 xadlock.xdlist = &p->xad[index + 1];
3514 le16_to_cpu(p->header.nextindex) -
3516 txFreeMap(ip, (struct maplock *) & xadlock,
3519 BT_MARK_DIRTY(mp, ip);
3521 p->header.nextindex = cpu_to_le16(index + 1);
3528 * child page was empty:
3530 nfreed += lengthXAD(&p->xad[index]);
3533 * During working map update, child page's tlock must be handled
3534 * before parent's. This is because the parent's tlock will cause
3535 * the child's disk space to be marked available in the wmap, so
3536 * it's important that the child page be released by that time.
3538 * ToDo: tlocks should be on doubly-linked list, so we can
3539 * quickly remove it and add it to the end.
3543 * Move parent page's tlock to the end of the tid's tlock list
3545 if (log && mp->lid && (tblk->last != mp->lid) &&
3546 lid_to_tlock(mp->lid)->tid) {
3547 lid_t lid = mp->lid;
3550 tlck = lid_to_tlock(lid);
3552 if (tblk->next == lid)
3553 tblk->next = tlck->next;
3555 for (prev = lid_to_tlock(tblk->next);
3557 prev = lid_to_tlock(prev->next)) {
3560 prev->next = tlck->next;
3562 lid_to_tlock(tblk->last)->next = lid;
3568 * parent page become empty: free the page
3570 if (index == XTENTRYSTART) {
3571 if (log) { /* COMMIT_PWMAP */
3572 /* txCommit() with tlckFREE:
3573 * free child extents covered by parent;
3574 * invalidate parent if COMMIT_PWMAP;
3576 tlck = txLock(tid, ip, mp, tlckXTREE);
3577 xtlck = (struct xtlock *) & tlck->lock;
3579 le16_to_cpu(p->header.nextindex) - 1;
3580 tlck->type = tlckXTREE | tlckFREE;
3581 } else { /* COMMIT_WMAP */
3583 /* free child extents covered by parent */
3584 xadlock.xdlist = &p->xad[XTENTRYSTART];
3586 le16_to_cpu(p->header.nextindex) -
3588 txFreeMap(ip, (struct maplock *) & xadlock, NULL,
3591 BT_MARK_DIRTY(mp, ip);
3593 if (p->header.flag & BT_ROOT) {
3594 p->header.flag &= ~BT_INTERNAL;
3595 p->header.flag |= BT_LEAF;
3596 p->header.nextindex = cpu_to_le16(XTENTRYSTART);
3597 if (le16_to_cpu(p->header.maxentry) == XTROOTMAXSLOT) {
3599 * Shrink root down to allow inline
3600 * EA (otherwise fsck complains)
3602 p->header.maxentry =
3603 cpu_to_le16(XTROOTINITSLOT);
3604 JFS_IP(ip)->mode2 |= INLINEEA;
3607 XT_PUTPAGE(mp); /* debug */
3610 if (log) { /* COMMIT_PWMAP */
3611 /* page will be invalidated at tx completion
3614 } else { /* COMMIT_WMAP */
3617 lid_to_tlock(mp->lid)->flag |=
3620 /* invalidate parent page */
3621 discard_metapage(mp);
3624 /* parent has become empty and freed:
3625 * go back up to its parent page
3632 * parent page still has entries for front region;
3635 /* try truncate region covered by preceding entry
3636 * (process backward)
3640 /* go back down to the child page corresponding
3647 * internal page: go down to child page of current entry
3650 /* save current parent entry for the child page */
3651 if (BT_STACK_FULL(&btstack)) {
3652 jfs_error(ip->i_sb, "stack overrun!\n");
3656 BT_PUSH(&btstack, bn, index);
3658 /* get child page */
3659 xad = &p->xad[index];
3660 bn = addressXAD(xad);
3663 * first access of each internal entry:
3665 /* release parent page */
3668 /* process the child page */
3673 * update file resource stat
3677 if (S_ISDIR(ip->i_mode) && !newsize)
3678 ip->i_size = 1; /* fsck hates zero-length directories */
3680 ip->i_size = newsize;
3682 /* update quota allocation to reflect freed blocks */
3683 dquot_free_block(ip, nfreed);
3686 * free tlock of invalidated pages
3688 if (flag == COMMIT_WMAP)
3699 * Perform truncate to zero length for deleted file, leaving the
3700 * the xtree and working map untouched. This allows the file to
3701 * be accessed via open file handles, while the delete of the file
3702 * is committed to disk.
3707 * s64 committed_size)
3709 * return: new committed size
3713 * To avoid deadlock by holding too many transaction locks, the
3714 * truncation may be broken up into multiple transactions.
3715 * The committed_size keeps track of part of the file has been
3716 * freed from the pmaps.
3718 s64 xtTruncate_pmap(tid_t tid, struct inode *ip, s64 committed_size)
3721 struct btstack btstack;
3724 int locked_leaves = 0;
3725 struct metapage *mp;
3727 struct btframe *parent;
3729 struct tblock *tblk;
3730 struct tlock *tlck = NULL;
3734 struct xtlock *xtlck = NULL;
3736 /* save object truncation type */
3737 tblk = tid_to_tblock(tid);
3738 tblk->xflag |= COMMIT_PMAP;
3743 if (committed_size) {
3744 xoff = (committed_size >> JFS_SBI(ip->i_sb)->l2bsize) - 1;
3745 rc = xtSearch(ip, xoff, NULL, &cmp, &btstack, 0);
3749 XT_GETSEARCH(ip, btstack.top, bn, mp, p, index);
3753 jfs_error(ip->i_sb, "did not find extent\n");
3760 * root resides in the inode
3765 * first access of each page:
3768 XT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
3772 /* process entries backward from last index */
3773 index = le16_to_cpu(p->header.nextindex) - 1;
3775 if (p->header.flag & BT_INTERNAL)
3783 if (++locked_leaves > MAX_TRUNCATE_LEAVES) {
3785 * We need to limit the size of the transaction
3786 * to avoid exhausting pagecache & tlocks
3788 xad = &p->xad[index];
3789 xoff = offsetXAD(xad);
3790 xlen = lengthXAD(xad);
3792 return (xoff + xlen) << JFS_SBI(ip->i_sb)->l2bsize;
3794 tlck = txLock(tid, ip, mp, tlckXTREE);
3795 tlck->type = tlckXTREE | tlckFREE;
3796 xtlck = (struct xtlock *) & tlck->lock;
3797 xtlck->hwm.offset = index;
3803 * go back up to the parent page
3806 /* pop/restore parent entry for the current child page */
3807 if ((parent = BT_POP(&btstack)) == NULL)
3808 /* current page must have been root */
3811 /* get back the parent page */
3813 XT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
3817 index = parent->index;
3820 * parent page become empty: free the page
3822 if (index == XTENTRYSTART) {
3823 /* txCommit() with tlckFREE:
3824 * free child extents covered by parent;
3825 * invalidate parent if COMMIT_PWMAP;
3827 tlck = txLock(tid, ip, mp, tlckXTREE);
3828 xtlck = (struct xtlock *) & tlck->lock;
3829 xtlck->hwm.offset = le16_to_cpu(p->header.nextindex) - 1;
3830 tlck->type = tlckXTREE | tlckFREE;
3834 if (p->header.flag & BT_ROOT) {
3842 * parent page still has entries for front region;
3847 * internal page: go down to child page of current entry
3850 /* save current parent entry for the child page */
3851 if (BT_STACK_FULL(&btstack)) {
3852 jfs_error(ip->i_sb, "stack overrun!\n");
3856 BT_PUSH(&btstack, bn, index);
3858 /* get child page */
3859 xad = &p->xad[index];
3860 bn = addressXAD(xad);
3863 * first access of each internal entry:
3865 /* release parent page */
3868 /* process the child page */
3876 #ifdef CONFIG_JFS_STATISTICS
3877 static int jfs_xtstat_proc_show(struct seq_file *m, void *v)
3880 "JFS Xtree statistics\n"
3881 "====================\n"
3883 "fast searches = %d\n"
3891 static int jfs_xtstat_proc_open(struct inode *inode, struct file *file)
3893 return single_open(file, jfs_xtstat_proc_show, NULL);
3896 const struct file_operations jfs_xtstat_proc_fops = {
3897 .open = jfs_xtstat_proc_open,
3899 .llseek = seq_lseek,
3900 .release = single_release,