1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) International Business Machines Corp., 2000-2005
6 * jfs_xtree.c: extent allocation descriptor B+-tree manager
10 #include <linux/module.h>
11 #include <linux/quotaops.h>
12 #include <linux/seq_file.h>
13 #include "jfs_incore.h"
14 #include "jfs_filsys.h"
15 #include "jfs_metapage.h"
17 #include "jfs_dinode.h"
18 #include "jfs_superblock.h"
19 #include "jfs_debug.h"
24 #define XT_INSERT 0x00000001
27 * xtree key/entry comparison: extent offset
30 * -1: k < start of extent
31 * 0: start_of_extent <= k <= end_of_extent
32 * 1: k > end_of_extent
34 #define XT_CMP(CMP, K, X, OFFSET64)\
36 OFFSET64 = offsetXAD(X);\
37 (CMP) = ((K) >= OFFSET64 + lengthXAD(X)) ? 1 :\
38 ((K) < OFFSET64) ? -1 : 0;\
41 /* write a xad entry */
42 #define XT_PUTENTRY(XAD, FLAG, OFF, LEN, ADDR)\
44 (XAD)->flag = (FLAG);\
45 XADoffset((XAD), (OFF));\
46 XADlength((XAD), (LEN));\
47 XADaddress((XAD), (ADDR));\
50 #define XT_PAGE(IP, MP) BT_PAGE(IP, MP, xtpage_t, i_xtroot)
52 /* get page buffer for specified block address */
53 /* ToDo: Replace this ugly macro with a function */
54 #define XT_GETPAGE(IP, BN, MP, SIZE, P, RC) \
56 BT_GETPAGE(IP, BN, MP, xtpage_t, SIZE, P, RC, i_xtroot); \
58 if ((le16_to_cpu((P)->header.nextindex) < XTENTRYSTART) || \
59 (le16_to_cpu((P)->header.nextindex) > \
60 le16_to_cpu((P)->header.maxentry)) || \
61 (le16_to_cpu((P)->header.maxentry) > \
62 (((BN) == 0) ? XTROOTMAXSLOT : PSIZE >> L2XTSLOTSIZE))) { \
63 jfs_error((IP)->i_sb, \
64 "XT_GETPAGE: xtree page corrupt\n"); \
73 #define XT_PUTPAGE(MP) BT_PUTPAGE(MP)
75 #define XT_GETSEARCH(IP, LEAF, BN, MP, P, INDEX) \
76 BT_GETSEARCH(IP, LEAF, BN, MP, xtpage_t, P, INDEX, i_xtroot)
77 /* xtree entry parameter descriptor */
85 struct pxdlist *pxdlist;
92 #ifdef CONFIG_JFS_STATISTICS
104 static int xtSearch(struct inode *ip, s64 xoff, s64 *next, int *cmpp,
105 struct btstack * btstack, int flag);
107 static int xtSplitUp(tid_t tid,
109 struct xtsplit * split, struct btstack * btstack);
111 static int xtSplitPage(tid_t tid, struct inode *ip, struct xtsplit * split,
112 struct metapage ** rmpp, s64 * rbnp);
114 static int xtSplitRoot(tid_t tid, struct inode *ip,
115 struct xtsplit * split, struct metapage ** rmpp);
120 * function: map a single page into a physical extent;
122 int xtLookup(struct inode *ip, s64 lstart,
123 s64 llen, int *pflag, s64 * paddr, s32 * plen, int no_check)
126 struct btstack btstack;
133 s64 next, size, xoff, xend;
141 /* is lookup offset beyond eof ? */
142 size = ((u64) ip->i_size + (JFS_SBI(ip->i_sb)->bsize - 1)) >>
143 JFS_SBI(ip->i_sb)->l2bsize;
149 * search for the xad entry covering the logical extent
152 if ((rc = xtSearch(ip, lstart, &next, &cmp, &btstack, 0))) {
153 jfs_err("xtLookup: xtSearch returned %d", rc);
158 * compute the physical extent covering logical extent
160 * N.B. search may have failed (e.g., hole in sparse file),
161 * and returned the index of the next entry.
163 /* retrieve search result */
164 XT_GETSEARCH(ip, btstack.top, bn, mp, p, index);
166 /* is xad found covering start of logical extent ?
167 * lstart is a page start address,
168 * i.e., lstart cannot start in a hole;
172 *plen = min(next - lstart, llen);
179 xad = &p->xad[index];
180 xoff = offsetXAD(xad);
181 xlen = lengthXAD(xad);
183 xaddr = addressXAD(xad);
185 /* initialize new pxd */
187 *paddr = xaddr + (lstart - xoff);
188 /* a page must be fully covered by an xad */
189 *plen = min(xend - lstart, llen);
200 * function: search for the xad entry covering specified offset.
204 * xoff - extent offset;
205 * nextp - address of next extent (if any) for search miss
206 * cmpp - comparison result:
207 * btstack - traverse stack;
208 * flag - search process flag (XT_INSERT);
211 * btstack contains (bn, index) of search path traversed to the entry.
212 * *cmpp is set to result of comparison with the entry returned.
213 * the page containing the entry is pinned at exit.
215 static int xtSearch(struct inode *ip, s64 xoff, s64 *nextp,
216 int *cmpp, struct btstack * btstack, int flag)
218 struct jfs_inode_info *jfs_ip = JFS_IP(ip);
220 int cmp = 1; /* init for empty page */
221 s64 bn; /* block number */
222 struct metapage *mp; /* page buffer */
223 xtpage_t *p; /* page */
225 int base, index, lim, btindex;
226 struct btframe *btsp;
227 int nsplit = 0; /* number of pages to split */
231 INCREMENT(xtStat.search);
238 * search down tree from root:
240 * between two consecutive entries of <Ki, Pi> and <Kj, Pj> of
241 * internal page, child page Pi contains entry with k, Ki <= K < Kj.
243 * if entry with search key K is not found
244 * internal page search find the entry with largest key Ki
245 * less than K which point to the child page to search;
246 * leaf page search find the entry with smallest key Kj
247 * greater than K so that the returned index is the position of
248 * the entry to be shifted right for insertion of new entry.
249 * for empty tree, search key is greater than any key of the tree.
251 * by convention, root bn = 0.
254 /* get/pin the page to search */
255 XT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
259 /* try sequential access heuristics with the previous
260 * access entry in target leaf page:
261 * once search narrowed down into the target leaf,
262 * key must either match an entry in the leaf or
263 * key entry does not exist in the tree;
266 if ((jfs_ip->btorder & BT_SEQUENTIAL) &&
267 (p->header.flag & BT_LEAF) &&
268 (index = jfs_ip->btindex) <
269 le16_to_cpu(p->header.nextindex)) {
270 xad = &p->xad[index];
271 t64 = offsetXAD(xad);
272 if (xoff < t64 + lengthXAD(xad)) {
278 /* stop sequential access heuristics */
280 } else { /* (t64 + lengthXAD(xad)) <= xoff */
282 /* try next sequential entry */
285 le16_to_cpu(p->header.nextindex)) {
287 t64 = offsetXAD(xad);
288 if (xoff < t64 + lengthXAD(xad)) {
294 /* miss: key falls between
295 * previous and this entry
302 /* (xoff >= t64 + lengthXAD(xad));
303 * matching entry may be further out:
304 * stop heuristic search
306 /* stop sequential access heuristics */
310 /* (index == p->header.nextindex);
311 * miss: key entry does not exist in
312 * the target leaf/tree
319 * if hit, return index of the entry found, and
320 * if miss, where new entry with search key is
324 /* compute number of pages to split */
325 if (flag & XT_INSERT) {
326 if (p->header.nextindex == /* little-endian */
331 btstack->nsplit = nsplit;
334 /* save search result */
340 /* update sequential access heuristics */
341 jfs_ip->btindex = index;
346 INCREMENT(xtStat.fastSearch);
350 /* well, ... full search now */
352 lim = le16_to_cpu(p->header.nextindex) - XTENTRYSTART;
355 * binary search with search key K on the current page
357 for (base = XTENTRYSTART; lim; lim >>= 1) {
358 index = base + (lim >> 1);
360 XT_CMP(cmp, xoff, &p->xad[index], t64);
365 /* search hit - leaf page:
366 * return the entry found
368 if (p->header.flag & BT_LEAF) {
371 /* compute number of pages to split */
372 if (flag & XT_INSERT) {
373 if (p->header.nextindex ==
378 btstack->nsplit = nsplit;
381 /* save search result */
387 /* init sequential access heuristics */
388 btindex = jfs_ip->btindex;
389 if (index == btindex ||
390 index == btindex + 1)
391 jfs_ip->btorder = BT_SEQUENTIAL;
393 jfs_ip->btorder = BT_RANDOM;
394 jfs_ip->btindex = index;
398 /* search hit - internal page:
399 * descend/search its child page
401 if (index < le16_to_cpu(p->header.nextindex)-1)
402 next = offsetXAD(&p->xad[index + 1]);
415 * base is the smallest index with key (Kj) greater than
416 * search key (K) and may be zero or maxentry index.
418 if (base < le16_to_cpu(p->header.nextindex))
419 next = offsetXAD(&p->xad[base]);
421 * search miss - leaf page:
423 * return location of entry (base) where new entry with
424 * search key K is to be inserted.
426 if (p->header.flag & BT_LEAF) {
429 /* compute number of pages to split */
430 if (flag & XT_INSERT) {
431 if (p->header.nextindex ==
436 btstack->nsplit = nsplit;
439 /* save search result */
445 /* init sequential access heuristics */
446 btindex = jfs_ip->btindex;
447 if (base == btindex || base == btindex + 1)
448 jfs_ip->btorder = BT_SEQUENTIAL;
450 jfs_ip->btorder = BT_RANDOM;
451 jfs_ip->btindex = base;
460 * search miss - non-leaf page:
462 * if base is non-zero, decrement base by one to get the parent
463 * entry of the child page to search.
465 index = base ? base - 1 : base;
468 * go down to child page
471 /* update number of pages to split */
472 if (p->header.nextindex == p->header.maxentry)
477 /* push (bn, index) of the parent page/entry */
478 if (BT_STACK_FULL(btstack)) {
479 jfs_error(ip->i_sb, "stack overrun!\n");
483 BT_PUSH(btstack, bn, index);
485 /* get the child page block number */
486 bn = addressXAD(&p->xad[index]);
488 /* unpin the parent page */
499 * tid - transaction id;
501 * xflag - extent flag (XAD_NOTRECORDED):
502 * xoff - extent offset;
503 * xlen - extent length;
504 * xaddrp - extent address pointer (in/out):
506 * caller allocated data extent at *xaddrp;
508 * allocate data extent and return its xaddr;
513 int xtInsert(tid_t tid, /* transaction id */
514 struct inode *ip, int xflag, s64 xoff, s32 xlen, s64 * xaddrp,
519 struct metapage *mp; /* meta-page buffer */
520 xtpage_t *p; /* base B+-tree index page */
522 int index, nextindex;
523 struct btstack btstack; /* traverse stack */
524 struct xtsplit split; /* split information */
529 struct xtlock *xtlck;
531 jfs_info("xtInsert: nxoff:0x%lx nxlen:0x%x", (ulong) xoff, xlen);
534 * search for the entry location at which to insert:
536 * xtFastSearch() and xtSearch() both returns (leaf page
537 * pinned, index at which to insert).
538 * n.b. xtSearch() may return index of maxentry of
541 if ((rc = xtSearch(ip, xoff, &next, &cmp, &btstack, XT_INSERT)))
544 /* retrieve search result */
545 XT_GETSEARCH(ip, btstack.top, bn, mp, p, index);
547 /* This test must follow XT_GETSEARCH since mp must be valid if
548 * we branch to out: */
549 if ((cmp == 0) || (next && (xlen > next - xoff))) {
555 * allocate data extent requested
557 * allocation hint: last xad
559 if ((xaddr = *xaddrp) == 0) {
560 if (index > XTENTRYSTART) {
561 xad = &p->xad[index - 1];
562 hint = addressXAD(xad) + lengthXAD(xad) - 1;
565 if ((rc = dquot_alloc_block(ip, xlen)))
567 if ((rc = dbAlloc(ip, hint, (s64) xlen, &xaddr))) {
568 dquot_free_block(ip, xlen);
574 * insert entry for new extent
579 * if the leaf page is full, split the page and
580 * propagate up the router entry for the new page from split
582 * The xtSplitUp() will insert the entry and unpin the leaf page.
584 nextindex = le16_to_cpu(p->header.nextindex);
585 if (nextindex == le16_to_cpu(p->header.maxentry)) {
592 split.pxdlist = NULL;
593 if ((rc = xtSplitUp(tid, ip, &split, &btstack))) {
594 /* undo data extent allocation */
596 dbFree(ip, xaddr, (s64) xlen);
597 dquot_free_block(ip, xlen);
607 * insert the new entry into the leaf page
610 * acquire a transaction lock on the leaf page;
612 * action: xad insertion/extension;
614 BT_MARK_DIRTY(mp, ip);
616 /* if insert into middle, shift right remaining entries. */
617 if (index < nextindex)
618 memmove(&p->xad[index + 1], &p->xad[index],
619 (nextindex - index) * sizeof(xad_t));
621 /* insert the new entry: mark the entry NEW */
622 xad = &p->xad[index];
623 XT_PUTENTRY(xad, xflag, xoff, xlen, xaddr);
625 /* advance next available entry index */
626 le16_add_cpu(&p->header.nextindex, 1);
628 /* Don't log it if there are no links to the file */
629 if (!test_cflag(COMMIT_Nolink, ip)) {
630 tlck = txLock(tid, ip, mp, tlckXTREE | tlckGROW);
631 xtlck = (struct xtlock *) & tlck->lock;
633 (xtlck->lwm.offset) ? min(index,
634 (int)xtlck->lwm.offset) : index;
636 le16_to_cpu(p->header.nextindex) - xtlck->lwm.offset;
642 /* unpin the leaf page */
653 * split full pages as propagating insertion up the tree
656 * tid - transaction id;
658 * split - entry parameter descriptor;
659 * btstack - traverse stack from xtSearch()
665 struct inode *ip, struct xtsplit * split, struct btstack * btstack)
668 struct metapage *smp;
669 xtpage_t *sp; /* split page */
670 struct metapage *rmp;
671 s64 rbn; /* new right page block number */
672 struct metapage *rcmp;
673 xtpage_t *rcp; /* right child page */
674 s64 rcbn; /* right child page block number */
675 int skip; /* index of entry of insertion */
676 int nextindex; /* next available entry index of p */
677 struct btframe *parent; /* parent page entry on traverse stack */
681 int nsplit; /* number of pages split */
682 struct pxdlist pxdlist;
685 struct xtlock *xtlck;
688 sp = XT_PAGE(ip, smp);
690 /* is inode xtree root extension/inline EA area free ? */
691 if ((sp->header.flag & BT_ROOT) && (!S_ISDIR(ip->i_mode)) &&
692 (le16_to_cpu(sp->header.maxentry) < XTROOTMAXSLOT) &&
693 (JFS_IP(ip)->mode2 & INLINEEA)) {
694 sp->header.maxentry = cpu_to_le16(XTROOTMAXSLOT);
695 JFS_IP(ip)->mode2 &= ~INLINEEA;
697 BT_MARK_DIRTY(smp, ip);
699 * acquire a transaction lock on the leaf page;
701 * action: xad insertion/extension;
704 /* if insert into middle, shift right remaining entries. */
706 nextindex = le16_to_cpu(sp->header.nextindex);
707 if (skip < nextindex)
708 memmove(&sp->xad[skip + 1], &sp->xad[skip],
709 (nextindex - skip) * sizeof(xad_t));
711 /* insert the new entry: mark the entry NEW */
712 xad = &sp->xad[skip];
713 XT_PUTENTRY(xad, split->flag, split->off, split->len,
716 /* advance next available entry index */
717 le16_add_cpu(&sp->header.nextindex, 1);
719 /* Don't log it if there are no links to the file */
720 if (!test_cflag(COMMIT_Nolink, ip)) {
721 tlck = txLock(tid, ip, smp, tlckXTREE | tlckGROW);
722 xtlck = (struct xtlock *) & tlck->lock;
723 xtlck->lwm.offset = (xtlck->lwm.offset) ?
724 min(skip, (int)xtlck->lwm.offset) : skip;
726 le16_to_cpu(sp->header.nextindex) -
734 * allocate new index blocks to cover index page split(s)
738 if (split->pxdlist == NULL) {
739 nsplit = btstack->nsplit;
740 split->pxdlist = &pxdlist;
741 pxdlist.maxnpxd = pxdlist.npxd = 0;
742 pxd = &pxdlist.pxd[0];
743 xlen = JFS_SBI(ip->i_sb)->nbperpage;
744 for (; nsplit > 0; nsplit--, pxd++) {
745 if ((rc = dbAlloc(ip, (s64) 0, (s64) xlen, &xaddr))
747 PXDaddress(pxd, xaddr);
748 PXDlength(pxd, xlen);
755 /* undo allocation */
763 * Split leaf page <sp> into <sp> and a new right page <rp>.
765 * The split routines insert the new entry into the leaf page,
766 * and acquire txLock as appropriate.
767 * return <rp> pinned and its block number <rpbn>.
769 rc = (sp->header.flag & BT_ROOT) ?
770 xtSplitRoot(tid, ip, split, &rmp) :
771 xtSplitPage(tid, ip, split, &rmp, &rbn);
778 * propagate up the router entry for the leaf page just split
780 * insert a router entry for the new page into the parent page,
781 * propagate the insert/split up the tree by walking back the stack
782 * of (bn of parent page, index of child page entry in parent page)
783 * that were traversed during the search for the page that split.
785 * the propagation of insert/split up the tree stops if the root
786 * splits or the page inserted into doesn't have to split to hold
789 * the parent entry for the split page remains the same, and
790 * a new entry is inserted at its right with the first key and
791 * block number of the new right page.
793 * There are a maximum of 3 pages pinned at any time:
794 * right child, left parent and right parent (when the parent splits)
795 * to keep the child page pinned while working on the parent.
796 * make sure that all pins are released at exit.
798 while ((parent = BT_POP(btstack)) != NULL) {
799 /* parent page specified by stack frame <parent> */
801 /* keep current child pages <rcp> pinned */
804 rcp = XT_PAGE(ip, rcmp);
807 * insert router entry in parent for new right child page <rp>
809 /* get/pin the parent page <sp> */
810 XT_GETPAGE(ip, parent->bn, smp, PSIZE, sp, rc);
817 * The new key entry goes ONE AFTER the index of parent entry,
818 * because the split was to the right.
820 skip = parent->index + 1;
823 * split or shift right remaining entries of the parent page
825 nextindex = le16_to_cpu(sp->header.nextindex);
827 * parent page is full - split the parent page
829 if (nextindex == le16_to_cpu(sp->header.maxentry)) {
830 /* init for parent page split */
832 split->index = skip; /* index at insert */
833 split->flag = XAD_NEW;
834 split->off = offsetXAD(&rcp->xad[XTENTRYSTART]);
835 split->len = JFS_SBI(ip->i_sb)->nbperpage;
838 /* unpin previous right child page */
841 /* The split routines insert the new entry,
842 * and acquire txLock as appropriate.
843 * return <rp> pinned and its block number <rpbn>.
845 rc = (sp->header.flag & BT_ROOT) ?
846 xtSplitRoot(tid, ip, split, &rmp) :
847 xtSplitPage(tid, ip, split, &rmp, &rbn);
854 /* keep new child page <rp> pinned */
857 * parent page is not full - insert in parent page
861 * insert router entry in parent for the right child
862 * page from the first entry of the right child page:
865 * acquire a transaction lock on the parent page;
867 * action: router xad insertion;
869 BT_MARK_DIRTY(smp, ip);
872 * if insert into middle, shift right remaining entries
874 if (skip < nextindex)
875 memmove(&sp->xad[skip + 1], &sp->xad[skip],
877 skip) << L2XTSLOTSIZE);
879 /* insert the router entry */
880 xad = &sp->xad[skip];
881 XT_PUTENTRY(xad, XAD_NEW,
882 offsetXAD(&rcp->xad[XTENTRYSTART]),
883 JFS_SBI(ip->i_sb)->nbperpage, rcbn);
885 /* advance next available entry index. */
886 le16_add_cpu(&sp->header.nextindex, 1);
888 /* Don't log it if there are no links to the file */
889 if (!test_cflag(COMMIT_Nolink, ip)) {
890 tlck = txLock(tid, ip, smp,
891 tlckXTREE | tlckGROW);
892 xtlck = (struct xtlock *) & tlck->lock;
893 xtlck->lwm.offset = (xtlck->lwm.offset) ?
894 min(skip, (int)xtlck->lwm.offset) : skip;
896 le16_to_cpu(sp->header.nextindex) -
900 /* unpin parent page */
903 /* exit propagate up */
908 /* unpin current right page */
919 * split a full non-root page into
920 * original/split/left page and new right page
921 * i.e., the original/split page remains as left page.
926 * struct xtsplit *split,
927 * struct metapage **rmpp,
931 * Pointer to page in which to insert or NULL on error.
934 xtSplitPage(tid_t tid, struct inode *ip,
935 struct xtsplit * split, struct metapage ** rmpp, s64 * rbnp)
938 struct metapage *smp;
940 struct metapage *rmp;
941 xtpage_t *rp; /* new right page allocated */
942 s64 rbn; /* new right page block number */
946 int skip, maxentry, middle, righthalf, n;
948 struct pxdlist *pxdlist;
951 struct xtlock *sxtlck = NULL, *rxtlck = NULL;
952 int quota_allocation = 0;
955 sp = XT_PAGE(ip, smp);
957 INCREMENT(xtStat.split);
959 pxdlist = split->pxdlist;
960 pxd = &pxdlist->pxd[pxdlist->npxd];
962 rbn = addressPXD(pxd);
964 /* Allocate blocks to quota. */
965 rc = dquot_alloc_block(ip, lengthPXD(pxd));
969 quota_allocation += lengthPXD(pxd);
972 * allocate the new right page for the split
974 rmp = get_metapage(ip, rbn, PSIZE, 1);
980 jfs_info("xtSplitPage: ip:0x%p smp:0x%p rmp:0x%p", ip, smp, rmp);
982 BT_MARK_DIRTY(rmp, ip);
987 rp = (xtpage_t *) rmp->data;
988 rp->header.self = *pxd;
989 rp->header.flag = sp->header.flag & BT_TYPE;
990 rp->header.maxentry = sp->header.maxentry; /* little-endian */
991 rp->header.nextindex = cpu_to_le16(XTENTRYSTART);
993 BT_MARK_DIRTY(smp, ip);
994 /* Don't log it if there are no links to the file */
995 if (!test_cflag(COMMIT_Nolink, ip)) {
997 * acquire a transaction lock on the new right page;
999 tlck = txLock(tid, ip, rmp, tlckXTREE | tlckNEW);
1000 rxtlck = (struct xtlock *) & tlck->lock;
1001 rxtlck->lwm.offset = XTENTRYSTART;
1003 * acquire a transaction lock on the split page
1005 tlck = txLock(tid, ip, smp, tlckXTREE | tlckGROW);
1006 sxtlck = (struct xtlock *) & tlck->lock;
1010 * initialize/update sibling pointers of <sp> and <rp>
1012 nextbn = le64_to_cpu(sp->header.next);
1013 rp->header.next = cpu_to_le64(nextbn);
1014 rp->header.prev = cpu_to_le64(addressPXD(&sp->header.self));
1015 sp->header.next = cpu_to_le64(rbn);
1017 skip = split->index;
1020 * sequential append at tail (after last entry of last page)
1022 * if splitting the last page on a level because of appending
1023 * a entry to it (skip is maxentry), it's likely that the access is
1024 * sequential. adding an empty page on the side of the level is less
1025 * work and can push the fill factor much higher than normal.
1026 * if we're wrong it's no big deal - we will do the split the right
1028 * (it may look like it's equally easy to do a similar hack for
1029 * reverse sorted data, that is, split the tree left, but it's not.
1032 if (nextbn == 0 && skip == le16_to_cpu(sp->header.maxentry)) {
1034 * acquire a transaction lock on the new/right page;
1036 * action: xad insertion;
1038 /* insert entry at the first entry of the new right page */
1039 xad = &rp->xad[XTENTRYSTART];
1040 XT_PUTENTRY(xad, split->flag, split->off, split->len,
1043 rp->header.nextindex = cpu_to_le16(XTENTRYSTART + 1);
1045 if (!test_cflag(COMMIT_Nolink, ip)) {
1046 /* rxtlck->lwm.offset = XTENTRYSTART; */
1047 rxtlck->lwm.length = 1;
1053 jfs_info("xtSplitPage: sp:0x%p rp:0x%p", sp, rp);
1058 * non-sequential insert (at possibly middle page)
1062 * update previous pointer of old next/right page of <sp>
1065 XT_GETPAGE(ip, nextbn, mp, PSIZE, p, rc);
1071 BT_MARK_DIRTY(mp, ip);
1073 * acquire a transaction lock on the next page;
1075 * action:sibling pointer update;
1077 if (!test_cflag(COMMIT_Nolink, ip))
1078 tlck = txLock(tid, ip, mp, tlckXTREE | tlckRELINK);
1080 p->header.prev = cpu_to_le64(rbn);
1082 /* sibling page may have been updated previously, or
1083 * it may be updated later;
1090 * split the data between the split and new/right pages
1092 maxentry = le16_to_cpu(sp->header.maxentry);
1093 middle = maxentry >> 1;
1094 righthalf = maxentry - middle;
1097 * skip index in old split/left page - insert into left page:
1099 if (skip <= middle) {
1100 /* move right half of split page to the new right page */
1101 memmove(&rp->xad[XTENTRYSTART], &sp->xad[middle],
1102 righthalf << L2XTSLOTSIZE);
1104 /* shift right tail of left half to make room for new entry */
1106 memmove(&sp->xad[skip + 1], &sp->xad[skip],
1107 (middle - skip) << L2XTSLOTSIZE);
1109 /* insert new entry */
1110 xad = &sp->xad[skip];
1111 XT_PUTENTRY(xad, split->flag, split->off, split->len,
1114 /* update page header */
1115 sp->header.nextindex = cpu_to_le16(middle + 1);
1116 if (!test_cflag(COMMIT_Nolink, ip)) {
1117 sxtlck->lwm.offset = (sxtlck->lwm.offset) ?
1118 min(skip, (int)sxtlck->lwm.offset) : skip;
1121 rp->header.nextindex =
1122 cpu_to_le16(XTENTRYSTART + righthalf);
1125 * skip index in new right page - insert into right page:
1128 /* move left head of right half to right page */
1130 memmove(&rp->xad[XTENTRYSTART], &sp->xad[middle],
1133 /* insert new entry */
1136 XT_PUTENTRY(xad, split->flag, split->off, split->len,
1139 /* move right tail of right half to right page */
1140 if (skip < maxentry)
1141 memmove(&rp->xad[n + 1], &sp->xad[skip],
1142 (maxentry - skip) << L2XTSLOTSIZE);
1144 /* update page header */
1145 sp->header.nextindex = cpu_to_le16(middle);
1146 if (!test_cflag(COMMIT_Nolink, ip)) {
1147 sxtlck->lwm.offset = (sxtlck->lwm.offset) ?
1148 min(middle, (int)sxtlck->lwm.offset) : middle;
1151 rp->header.nextindex = cpu_to_le16(XTENTRYSTART +
1155 if (!test_cflag(COMMIT_Nolink, ip)) {
1156 sxtlck->lwm.length = le16_to_cpu(sp->header.nextindex) -
1159 /* rxtlck->lwm.offset = XTENTRYSTART; */
1160 rxtlck->lwm.length = le16_to_cpu(rp->header.nextindex) -
1167 jfs_info("xtSplitPage: sp:0x%p rp:0x%p", sp, rp);
1172 /* Rollback quota allocation. */
1173 if (quota_allocation)
1174 dquot_free_block(ip, quota_allocation);
1184 * split the full root page into original/root/split page and new
1186 * i.e., root remains fixed in tree anchor (inode) and the root is
1187 * copied to a single new right child page since root page <<
1188 * non-root page, and the split root page contains a single entry
1189 * for the new right child page.
1194 * struct xtsplit *split,
1195 * struct metapage **rmpp)
1198 * Pointer to page in which to insert or NULL on error.
1201 xtSplitRoot(tid_t tid,
1202 struct inode *ip, struct xtsplit * split, struct metapage ** rmpp)
1205 struct metapage *rmp;
1208 int skip, nextindex;
1211 struct pxdlist *pxdlist;
1213 struct xtlock *xtlck;
1216 sp = &JFS_IP(ip)->i_xtroot;
1218 INCREMENT(xtStat.split);
1221 * allocate a single (right) child page
1223 pxdlist = split->pxdlist;
1224 pxd = &pxdlist->pxd[pxdlist->npxd];
1226 rbn = addressPXD(pxd);
1227 rmp = get_metapage(ip, rbn, PSIZE, 1);
1231 /* Allocate blocks to quota. */
1232 rc = dquot_alloc_block(ip, lengthPXD(pxd));
1234 release_metapage(rmp);
1238 jfs_info("xtSplitRoot: ip:0x%p rmp:0x%p", ip, rmp);
1241 * acquire a transaction lock on the new right page;
1245 BT_MARK_DIRTY(rmp, ip);
1247 rp = (xtpage_t *) rmp->data;
1249 (sp->header.flag & BT_LEAF) ? BT_LEAF : BT_INTERNAL;
1250 rp->header.self = *pxd;
1251 rp->header.nextindex = cpu_to_le16(XTENTRYSTART);
1252 rp->header.maxentry = cpu_to_le16(PSIZE >> L2XTSLOTSIZE);
1254 /* initialize sibling pointers */
1255 rp->header.next = 0;
1256 rp->header.prev = 0;
1259 * copy the in-line root page into new right page extent
1261 nextindex = le16_to_cpu(sp->header.maxentry);
1262 memmove(&rp->xad[XTENTRYSTART], &sp->xad[XTENTRYSTART],
1263 (nextindex - XTENTRYSTART) << L2XTSLOTSIZE);
1266 * insert the new entry into the new right/child page
1267 * (skip index in the new right page will not change)
1269 skip = split->index;
1270 /* if insert into middle, shift right remaining entries */
1271 if (skip != nextindex)
1272 memmove(&rp->xad[skip + 1], &rp->xad[skip],
1273 (nextindex - skip) * sizeof(xad_t));
1275 xad = &rp->xad[skip];
1276 XT_PUTENTRY(xad, split->flag, split->off, split->len, split->addr);
1278 /* update page header */
1279 rp->header.nextindex = cpu_to_le16(nextindex + 1);
1281 if (!test_cflag(COMMIT_Nolink, ip)) {
1282 tlck = txLock(tid, ip, rmp, tlckXTREE | tlckNEW);
1283 xtlck = (struct xtlock *) & tlck->lock;
1284 xtlck->lwm.offset = XTENTRYSTART;
1285 xtlck->lwm.length = le16_to_cpu(rp->header.nextindex) -
1292 * init root with the single entry for the new right page
1293 * set the 1st entry offset to 0, which force the left-most key
1294 * at any level of the tree to be less than any search key.
1297 * acquire a transaction lock on the root page (in-memory inode);
1299 * action: root split;
1301 BT_MARK_DIRTY(split->mp, ip);
1303 xad = &sp->xad[XTENTRYSTART];
1304 XT_PUTENTRY(xad, XAD_NEW, 0, JFS_SBI(ip->i_sb)->nbperpage, rbn);
1306 /* update page header of root */
1307 sp->header.flag &= ~BT_LEAF;
1308 sp->header.flag |= BT_INTERNAL;
1310 sp->header.nextindex = cpu_to_le16(XTENTRYSTART + 1);
1312 if (!test_cflag(COMMIT_Nolink, ip)) {
1313 tlck = txLock(tid, ip, split->mp, tlckXTREE | tlckGROW);
1314 xtlck = (struct xtlock *) & tlck->lock;
1315 xtlck->lwm.offset = XTENTRYSTART;
1316 xtlck->lwm.length = 1;
1321 jfs_info("xtSplitRoot: sp:0x%p rp:0x%p", sp, rp);
1329 * function: extend in-place;
1331 * note: existing extent may or may not have been committed.
1332 * caller is responsible for pager buffer cache update, and
1333 * working block allocation map update;
1334 * update pmap: alloc whole extended extent;
1336 int xtExtend(tid_t tid, /* transaction id */
1337 struct inode *ip, s64 xoff, /* delta extent offset */
1338 s32 xlen, /* delta extent length */
1343 struct metapage *mp; /* meta-page buffer */
1344 xtpage_t *p; /* base B+-tree index page */
1346 int index, nextindex, len;
1347 struct btstack btstack; /* traverse stack */
1348 struct xtsplit split; /* split information */
1352 struct xtlock *xtlck = NULL;
1354 jfs_info("xtExtend: nxoff:0x%lx nxlen:0x%x", (ulong) xoff, xlen);
1356 /* there must exist extent to be extended */
1357 if ((rc = xtSearch(ip, xoff - 1, NULL, &cmp, &btstack, XT_INSERT)))
1360 /* retrieve search result */
1361 XT_GETSEARCH(ip, btstack.top, bn, mp, p, index);
1365 jfs_error(ip->i_sb, "xtSearch did not find extent\n");
1369 /* extension must be contiguous */
1370 xad = &p->xad[index];
1371 if ((offsetXAD(xad) + lengthXAD(xad)) != xoff) {
1373 jfs_error(ip->i_sb, "extension is not contiguous\n");
1378 * acquire a transaction lock on the leaf page;
1380 * action: xad insertion/extension;
1382 BT_MARK_DIRTY(mp, ip);
1383 if (!test_cflag(COMMIT_Nolink, ip)) {
1384 tlck = txLock(tid, ip, mp, tlckXTREE | tlckGROW);
1385 xtlck = (struct xtlock *) & tlck->lock;
1388 /* extend will overflow extent ? */
1389 xlen = lengthXAD(xad) + xlen;
1390 if ((len = xlen - MAXXLEN) <= 0)
1394 * extent overflow: insert entry for new extent
1397 xoff = offsetXAD(xad) + MAXXLEN;
1398 xaddr = addressXAD(xad) + MAXXLEN;
1399 nextindex = le16_to_cpu(p->header.nextindex);
1402 * if the leaf page is full, insert the new entry and
1403 * propagate up the router entry for the new page from split
1405 * The xtSplitUp() will insert the entry and unpin the leaf page.
1407 if (nextindex == le16_to_cpu(p->header.maxentry)) {
1408 /* xtSpliUp() unpins leaf pages */
1410 split.index = index + 1;
1411 split.flag = XAD_NEW;
1412 split.off = xoff; /* split offset */
1415 split.pxdlist = NULL;
1416 if ((rc = xtSplitUp(tid, ip, &split, &btstack)))
1419 /* get back old page */
1420 XT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
1424 * if leaf root has been split, original root has been
1425 * copied to new child page, i.e., original entry now
1426 * resides on the new child page;
1428 if (p->header.flag & BT_INTERNAL) {
1429 ASSERT(p->header.nextindex ==
1430 cpu_to_le16(XTENTRYSTART + 1));
1431 xad = &p->xad[XTENTRYSTART];
1432 bn = addressXAD(xad);
1435 /* get new child page */
1436 XT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
1440 BT_MARK_DIRTY(mp, ip);
1441 if (!test_cflag(COMMIT_Nolink, ip)) {
1442 tlck = txLock(tid, ip, mp, tlckXTREE|tlckGROW);
1443 xtlck = (struct xtlock *) & tlck->lock;
1448 * insert the new entry into the leaf page
1451 /* insert the new entry: mark the entry NEW */
1452 xad = &p->xad[index + 1];
1453 XT_PUTENTRY(xad, XAD_NEW, xoff, len, xaddr);
1455 /* advance next available entry index */
1456 le16_add_cpu(&p->header.nextindex, 1);
1459 /* get back old entry */
1460 xad = &p->xad[index];
1467 XADlength(xad, xlen);
1468 if (!(xad->flag & XAD_NEW))
1469 xad->flag |= XAD_EXTENDED;
1471 if (!test_cflag(COMMIT_Nolink, ip)) {
1473 (xtlck->lwm.offset) ? min(index,
1474 (int)xtlck->lwm.offset) : index;
1476 le16_to_cpu(p->header.nextindex) - xtlck->lwm.offset;
1479 /* unpin the leaf page */
1488 * function: update XAD;
1490 * update extent for allocated_but_not_recorded or
1491 * compressed extent;
1495 * logical extent of the specified XAD must be completely
1496 * contained by an existing XAD;
1498 int xtUpdate(tid_t tid, struct inode *ip, xad_t * nxad)
1502 struct metapage *mp; /* meta-page buffer */
1503 xtpage_t *p; /* base B+-tree index page */
1505 int index0, index, newindex, nextindex;
1506 struct btstack btstack; /* traverse stack */
1507 struct xtsplit split; /* split information */
1508 xad_t *xad, *lxad, *rxad;
1511 int nxlen, xlen, lxlen, rxlen;
1514 struct xtlock *xtlck = NULL;
1517 /* there must exist extent to be tailgated */
1518 nxoff = offsetXAD(nxad);
1519 nxlen = lengthXAD(nxad);
1520 nxaddr = addressXAD(nxad);
1522 if ((rc = xtSearch(ip, nxoff, NULL, &cmp, &btstack, XT_INSERT)))
1525 /* retrieve search result */
1526 XT_GETSEARCH(ip, btstack.top, bn, mp, p, index0);
1530 jfs_error(ip->i_sb, "Could not find extent\n");
1534 BT_MARK_DIRTY(mp, ip);
1536 * acquire tlock of the leaf page containing original entry
1538 if (!test_cflag(COMMIT_Nolink, ip)) {
1539 tlck = txLock(tid, ip, mp, tlckXTREE | tlckGROW);
1540 xtlck = (struct xtlock *) & tlck->lock;
1543 xad = &p->xad[index0];
1545 xoff = offsetXAD(xad);
1546 xlen = lengthXAD(xad);
1547 xaddr = addressXAD(xad);
1549 /* nXAD must be completely contained within XAD */
1550 if ((xoff > nxoff) ||
1551 (nxoff + nxlen > xoff + xlen)) {
1554 "nXAD in not completely contained within XAD\n");
1559 newindex = index + 1;
1560 nextindex = le16_to_cpu(p->header.nextindex);
1566 * coalesce with left XAD
1568 /* is XAD first entry of page ? */
1569 if (index == XTENTRYSTART)
1572 /* is nXAD logically and physically contiguous with lXAD ? */
1573 lxad = &p->xad[index - 1];
1574 lxlen = lengthXAD(lxad);
1575 if (!(lxad->flag & XAD_NOTRECORDED) &&
1576 (nxoff == offsetXAD(lxad) + lxlen) &&
1577 (nxaddr == addressXAD(lxad) + lxlen) &&
1578 (lxlen + nxlen < MAXXLEN)) {
1579 /* extend right lXAD */
1581 XADlength(lxad, lxlen + nxlen);
1583 /* If we just merged two extents together, need to make sure the
1584 * right extent gets logged. If the left one is marked XAD_NEW,
1585 * then we know it will be logged. Otherwise, mark as
1588 if (!(lxad->flag & XAD_NEW))
1589 lxad->flag |= XAD_EXTENDED;
1593 XADoffset(xad, xoff + nxlen);
1594 XADlength(xad, xlen - nxlen);
1595 XADaddress(xad, xaddr + nxlen);
1597 } else { /* (xlen == nxlen) */
1600 if (index < nextindex - 1)
1601 memmove(&p->xad[index], &p->xad[index + 1],
1602 (nextindex - index -
1603 1) << L2XTSLOTSIZE);
1605 p->header.nextindex =
1606 cpu_to_le16(le16_to_cpu(p->header.nextindex) -
1610 newindex = index + 1;
1611 nextindex = le16_to_cpu(p->header.nextindex);
1612 xoff = nxoff = offsetXAD(lxad);
1613 xlen = nxlen = lxlen + nxlen;
1614 xaddr = nxaddr = addressXAD(lxad);
1620 * replace XAD with nXAD
1622 replace: /* (nxoff == xoff) */
1623 if (nxlen == xlen) {
1624 /* replace XAD with nXAD:recorded */
1626 xad->flag = xflag & ~XAD_NOTRECORDED;
1629 } else /* (nxlen < xlen) */
1633 * coalesce with right XAD
1635 coalesceRight: /* (xoff <= nxoff) */
1636 /* is XAD last entry of page ? */
1637 if (newindex == nextindex) {
1643 /* is nXAD logically and physically contiguous with rXAD ? */
1644 rxad = &p->xad[index + 1];
1645 rxlen = lengthXAD(rxad);
1646 if (!(rxad->flag & XAD_NOTRECORDED) &&
1647 (nxoff + nxlen == offsetXAD(rxad)) &&
1648 (nxaddr + nxlen == addressXAD(rxad)) &&
1649 (rxlen + nxlen < MAXXLEN)) {
1650 /* extend left rXAD */
1651 XADoffset(rxad, nxoff);
1652 XADlength(rxad, rxlen + nxlen);
1653 XADaddress(rxad, nxaddr);
1655 /* If we just merged two extents together, need to make sure
1656 * the left extent gets logged. If the right one is marked
1657 * XAD_NEW, then we know it will be logged. Otherwise, mark as
1660 if (!(rxad->flag & XAD_NEW))
1661 rxad->flag |= XAD_EXTENDED;
1665 XADlength(xad, xlen - nxlen);
1666 else { /* (xlen == nxlen) */
1669 memmove(&p->xad[index], &p->xad[index + 1],
1670 (nextindex - index - 1) << L2XTSLOTSIZE);
1672 p->header.nextindex =
1673 cpu_to_le16(le16_to_cpu(p->header.nextindex) -
1678 } else if (xoff == nxoff)
1681 if (xoff >= nxoff) {
1683 jfs_error(ip->i_sb, "xoff >= nxoff\n");
1688 * split XAD into (lXAD, nXAD):
1691 * --|----------XAD----------|--
1694 updateRight: /* (xoff < nxoff) */
1695 /* truncate old XAD as lXAD:not_recorded */
1696 xad = &p->xad[index];
1697 XADlength(xad, nxoff - xoff);
1699 /* insert nXAD:recorded */
1700 if (nextindex == le16_to_cpu(p->header.maxentry)) {
1702 /* xtSpliUp() unpins leaf pages */
1704 split.index = newindex;
1705 split.flag = xflag & ~XAD_NOTRECORDED;
1708 split.addr = nxaddr;
1709 split.pxdlist = NULL;
1710 if ((rc = xtSplitUp(tid, ip, &split, &btstack)))
1713 /* get back old page */
1714 XT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
1718 * if leaf root has been split, original root has been
1719 * copied to new child page, i.e., original entry now
1720 * resides on the new child page;
1722 if (p->header.flag & BT_INTERNAL) {
1723 ASSERT(p->header.nextindex ==
1724 cpu_to_le16(XTENTRYSTART + 1));
1725 xad = &p->xad[XTENTRYSTART];
1726 bn = addressXAD(xad);
1729 /* get new child page */
1730 XT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
1734 BT_MARK_DIRTY(mp, ip);
1735 if (!test_cflag(COMMIT_Nolink, ip)) {
1736 tlck = txLock(tid, ip, mp, tlckXTREE|tlckGROW);
1737 xtlck = (struct xtlock *) & tlck->lock;
1740 /* is nXAD on new page ? */
1742 (le16_to_cpu(p->header.maxentry) >> 1)) {
1745 le16_to_cpu(p->header.nextindex) +
1751 /* if insert into middle, shift right remaining entries */
1752 if (newindex < nextindex)
1753 memmove(&p->xad[newindex + 1], &p->xad[newindex],
1754 (nextindex - newindex) << L2XTSLOTSIZE);
1756 /* insert the entry */
1757 xad = &p->xad[newindex];
1759 xad->flag = xflag & ~XAD_NOTRECORDED;
1761 /* advance next available entry index. */
1762 p->header.nextindex =
1763 cpu_to_le16(le16_to_cpu(p->header.nextindex) + 1);
1767 * does nXAD force 3-way split ?
1770 * --|----------XAD-------------|--
1771 * |-lXAD-| |-rXAD -|
1773 if (nxoff + nxlen == xoff + xlen)
1776 /* reorient nXAD as XAD for further split XAD into (nXAD, rXAD) */
1778 /* close out old page */
1779 if (!test_cflag(COMMIT_Nolink, ip)) {
1780 xtlck->lwm.offset = (xtlck->lwm.offset) ?
1781 min(index0, (int)xtlck->lwm.offset) : index0;
1783 le16_to_cpu(p->header.nextindex) -
1787 bn = le64_to_cpu(p->header.next);
1790 /* get new right page */
1791 XT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
1795 BT_MARK_DIRTY(mp, ip);
1796 if (!test_cflag(COMMIT_Nolink, ip)) {
1797 tlck = txLock(tid, ip, mp, tlckXTREE | tlckGROW);
1798 xtlck = (struct xtlock *) & tlck->lock;
1801 index0 = index = newindex;
1805 newindex = index + 1;
1806 nextindex = le16_to_cpu(p->header.nextindex);
1807 xlen = xlen - (nxoff - xoff);
1811 /* recompute split pages */
1812 if (nextindex == le16_to_cpu(p->header.maxentry)) {
1815 if ((rc = xtSearch(ip, nxoff, NULL, &cmp, &btstack, XT_INSERT)))
1818 /* retrieve search result */
1819 XT_GETSEARCH(ip, btstack.top, bn, mp, p, index0);
1823 jfs_error(ip->i_sb, "xtSearch failed\n");
1827 if (index0 != index) {
1829 jfs_error(ip->i_sb, "unexpected value of index\n");
1835 * split XAD into (nXAD, rXAD)
1838 * --|----------XAD----------|--
1841 updateLeft: /* (nxoff == xoff) && (nxlen < xlen) */
1842 /* update old XAD with nXAD:recorded */
1843 xad = &p->xad[index];
1845 xad->flag = xflag & ~XAD_NOTRECORDED;
1847 /* insert rXAD:not_recorded */
1848 xoff = xoff + nxlen;
1849 xlen = xlen - nxlen;
1850 xaddr = xaddr + nxlen;
1851 if (nextindex == le16_to_cpu(p->header.maxentry)) {
1853 printf("xtUpdate.updateLeft.split p:0x%p\n", p);
1855 /* xtSpliUp() unpins leaf pages */
1857 split.index = newindex;
1862 split.pxdlist = NULL;
1863 if ((rc = xtSplitUp(tid, ip, &split, &btstack)))
1866 /* get back old page */
1867 XT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
1872 * if leaf root has been split, original root has been
1873 * copied to new child page, i.e., original entry now
1874 * resides on the new child page;
1876 if (p->header.flag & BT_INTERNAL) {
1877 ASSERT(p->header.nextindex ==
1878 cpu_to_le16(XTENTRYSTART + 1));
1879 xad = &p->xad[XTENTRYSTART];
1880 bn = addressXAD(xad);
1883 /* get new child page */
1884 XT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
1888 BT_MARK_DIRTY(mp, ip);
1889 if (!test_cflag(COMMIT_Nolink, ip)) {
1890 tlck = txLock(tid, ip, mp, tlckXTREE|tlckGROW);
1891 xtlck = (struct xtlock *) & tlck->lock;
1895 /* if insert into middle, shift right remaining entries */
1896 if (newindex < nextindex)
1897 memmove(&p->xad[newindex + 1], &p->xad[newindex],
1898 (nextindex - newindex) << L2XTSLOTSIZE);
1900 /* insert the entry */
1901 xad = &p->xad[newindex];
1902 XT_PUTENTRY(xad, xflag, xoff, xlen, xaddr);
1904 /* advance next available entry index. */
1905 p->header.nextindex =
1906 cpu_to_le16(le16_to_cpu(p->header.nextindex) + 1);
1910 if (!test_cflag(COMMIT_Nolink, ip)) {
1911 xtlck->lwm.offset = (xtlck->lwm.offset) ?
1912 min(index0, (int)xtlck->lwm.offset) : index0;
1913 xtlck->lwm.length = le16_to_cpu(p->header.nextindex) -
1917 /* unpin the leaf page */
1927 * function: grow in append mode from contiguous region specified ;
1930 * tid - transaction id;
1932 * xflag - extent flag:
1933 * xoff - extent offset;
1934 * maxblocks - max extent length;
1935 * xlen - extent length (in/out);
1936 * xaddrp - extent address pointer (in/out):
1941 int xtAppend(tid_t tid, /* transaction id */
1942 struct inode *ip, int xflag, s64 xoff, s32 maxblocks,
1943 s32 * xlenp, /* (in/out) */
1944 s64 * xaddrp, /* (in/out) */
1948 struct metapage *mp; /* meta-page buffer */
1949 xtpage_t *p; /* base B+-tree index page */
1951 int index, nextindex;
1952 struct btstack btstack; /* traverse stack */
1953 struct xtsplit split; /* split information */
1957 struct xtlock *xtlck;
1958 int nsplit, nblocks, xlen;
1959 struct pxdlist pxdlist;
1965 jfs_info("xtAppend: xoff:0x%lx maxblocks:%d xlen:%d xaddr:0x%lx",
1966 (ulong) xoff, maxblocks, xlen, (ulong) xaddr);
1969 * search for the entry location at which to insert:
1971 * xtFastSearch() and xtSearch() both returns (leaf page
1972 * pinned, index at which to insert).
1973 * n.b. xtSearch() may return index of maxentry of
1976 if ((rc = xtSearch(ip, xoff, &next, &cmp, &btstack, XT_INSERT)))
1979 /* retrieve search result */
1980 XT_GETSEARCH(ip, btstack.top, bn, mp, p, index);
1988 xlen = min(xlen, (int)(next - xoff));
1991 * insert entry for new extent
1996 * if the leaf page is full, split the page and
1997 * propagate up the router entry for the new page from split
1999 * The xtSplitUp() will insert the entry and unpin the leaf page.
2001 nextindex = le16_to_cpu(p->header.nextindex);
2002 if (nextindex < le16_to_cpu(p->header.maxentry))
2006 * allocate new index blocks to cover index page split(s)
2008 nsplit = btstack.nsplit;
2009 split.pxdlist = &pxdlist;
2010 pxdlist.maxnpxd = pxdlist.npxd = 0;
2011 pxd = &pxdlist.pxd[0];
2012 nblocks = JFS_SBI(ip->i_sb)->nbperpage;
2013 for (; nsplit > 0; nsplit--, pxd++, xaddr += nblocks, maxblocks -= nblocks) {
2014 if ((rc = dbAllocBottomUp(ip, xaddr, (s64) nblocks)) == 0) {
2015 PXDaddress(pxd, xaddr);
2016 PXDlength(pxd, nblocks);
2023 /* undo allocation */
2028 xlen = min(xlen, maxblocks);
2031 * allocate data extent requested
2033 if ((rc = dbAllocBottomUp(ip, xaddr, (s64) xlen)))
2037 split.index = index;
2042 if ((rc = xtSplitUp(tid, ip, &split, &btstack))) {
2043 /* undo data extent allocation */
2044 dbFree(ip, *xaddrp, (s64) * xlenp);
2054 * insert the new entry into the leaf page
2058 * allocate data extent requested
2060 if ((rc = dbAllocBottomUp(ip, xaddr, (s64) xlen)))
2063 BT_MARK_DIRTY(mp, ip);
2065 * acquire a transaction lock on the leaf page;
2067 * action: xad insertion/extension;
2069 tlck = txLock(tid, ip, mp, tlckXTREE | tlckGROW);
2070 xtlck = (struct xtlock *) & tlck->lock;
2072 /* insert the new entry: mark the entry NEW */
2073 xad = &p->xad[index];
2074 XT_PUTENTRY(xad, xflag, xoff, xlen, xaddr);
2076 /* advance next available entry index */
2077 le16_add_cpu(&p->header.nextindex, 1);
2080 (xtlck->lwm.offset) ? min(index,(int) xtlck->lwm.offset) : index;
2081 xtlck->lwm.length = le16_to_cpu(p->header.nextindex) -
2088 /* unpin the leaf page */
2097 * initialize file root (inline in inode)
2099 void xtInitRoot(tid_t tid, struct inode *ip)
2104 * acquire a transaction lock on the root
2108 txLock(tid, ip, (struct metapage *) &JFS_IP(ip)->bxflag,
2109 tlckXTREE | tlckNEW);
2110 p = &JFS_IP(ip)->i_xtroot;
2112 p->header.flag = DXD_INDEX | BT_ROOT | BT_LEAF;
2113 p->header.nextindex = cpu_to_le16(XTENTRYSTART);
2115 if (S_ISDIR(ip->i_mode))
2116 p->header.maxentry = cpu_to_le16(XTROOTINITSLOT_DIR);
2118 p->header.maxentry = cpu_to_le16(XTROOTINITSLOT);
2128 * We can run into a deadlock truncating a file with a large number of
2129 * xtree pages (large fragmented file). A robust fix would entail a
2130 * reservation system where we would reserve a number of metadata pages
2131 * and tlocks which we would be guaranteed without a deadlock. Without
2132 * this, a partial fix is to limit number of metadata pages we will lock
2133 * in a single transaction. Currently we will truncate the file so that
2134 * no more than 50 leaf pages will be locked. The caller of xtTruncate
2135 * will be responsible for ensuring that the current transaction gets
2136 * committed, and that subsequent transactions are created to truncate
2137 * the file further if needed.
2139 #define MAX_TRUNCATE_LEAVES 50
2145 * traverse for truncation logging backward bottom up;
2146 * terminate at the last extent entry at the current subtree
2147 * root page covering new down size.
2148 * truncation may occur within the last extent entry.
2154 * int type) {PWMAP, PMAP, WMAP; DELETE, TRUNCATE}
2160 * 1. truncate (non-COMMIT_NOLINK file)
2161 * by jfs_truncate() or jfs_open(O_TRUNC):
2163 * 2. truncate index table of directory when last entry removed
2164 * map update via tlock at commit time;
2166 * Call xtTruncate_pmap instead
2168 * 1. remove (free zero link count) on last reference release
2169 * (pmap has been freed at commit zero link count);
2170 * 2. truncate (COMMIT_NOLINK file, i.e., tmp file):
2172 * map update directly at truncation time;
2175 * no LOG_NOREDOPAGE is required (NOREDOFILE is sufficient);
2176 * else if (TRUNCATE)
2177 * must write LOG_NOREDOPAGE for deleted index page;
2179 * pages may already have been tlocked by anonymous transactions
2180 * during file growth (i.e., write) before truncation;
2182 * except last truncated entry, deleted entries remains as is
2183 * in the page (nextindex is updated) for other use
2184 * (e.g., log/update allocation map): this avoid copying the page
2185 * info but delay free of pages;
2188 s64 xtTruncate(tid_t tid, struct inode *ip, s64 newsize, int flag)
2192 struct metapage *mp;
2195 int index, nextindex;
2198 int xlen, len, freexlen;
2199 struct btstack btstack;
2200 struct btframe *parent;
2201 struct tblock *tblk = NULL;
2202 struct tlock *tlck = NULL;
2203 struct xtlock *xtlck = NULL;
2204 struct xdlistlock xadlock; /* maplock for COMMIT_WMAP */
2205 struct pxd_lock *pxdlock; /* maplock for COMMIT_WMAP */
2208 int locked_leaves = 0;
2210 /* save object truncation type */
2212 tblk = tid_to_tblock(tid);
2213 tblk->xflag |= flag;
2219 assert(flag != COMMIT_PMAP);
2221 if (flag == COMMIT_PWMAP)
2225 xadlock.flag = mlckFREEXADLIST;
2230 * if the newsize is not an integral number of pages,
2231 * the file between newsize and next page boundary will
2233 * if truncating into a file hole, it will cause
2234 * a full block to be allocated for the logical block.
2238 * release page blocks of truncated region <teof, eof>
2240 * free the data blocks from the leaf index blocks.
2241 * delete the parent index entries corresponding to
2242 * the freed child data/index blocks.
2243 * free the index blocks themselves which aren't needed
2244 * in new sized file.
2246 * index blocks are updated only if the blocks are to be
2247 * retained in the new sized file.
2248 * if type is PMAP, the data and index pages are NOT
2249 * freed, and the data and index blocks are NOT freed
2251 * (this will allow continued access of data/index of
2252 * temporary file (zerolink count file truncated to zero-length)).
2254 teof = (newsize + (JFS_SBI(ip->i_sb)->bsize - 1)) >>
2255 JFS_SBI(ip->i_sb)->l2bsize;
2263 * root resides in the inode
2268 * first access of each page:
2271 XT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
2275 /* process entries backward from last index */
2276 index = le16_to_cpu(p->header.nextindex) - 1;
2279 /* Since this is the rightmost page at this level, and we may have
2280 * already freed a page that was formerly to the right, let's make
2281 * sure that the next pointer is zero.
2283 if (p->header.next) {
2286 * Make sure this change to the header is logged.
2287 * If we really truncate this leaf, the flag
2288 * will be changed to tlckTRUNCATE
2290 tlck = txLock(tid, ip, mp, tlckXTREE|tlckGROW);
2291 BT_MARK_DIRTY(mp, ip);
2295 if (p->header.flag & BT_INTERNAL)
2303 /* does region covered by leaf page precede Teof ? */
2304 xad = &p->xad[index];
2305 xoff = offsetXAD(xad);
2306 xlen = lengthXAD(xad);
2307 if (teof >= xoff + xlen) {
2312 /* (re)acquire tlock of the leaf page */
2314 if (++locked_leaves > MAX_TRUNCATE_LEAVES) {
2316 * We need to limit the size of the transaction
2317 * to avoid exhausting pagecache & tlocks
2320 newsize = (xoff + xlen) << JFS_SBI(ip->i_sb)->l2bsize;
2323 tlck = txLock(tid, ip, mp, tlckXTREE);
2324 tlck->type = tlckXTREE | tlckTRUNCATE;
2325 xtlck = (struct xtlock *) & tlck->lock;
2326 xtlck->hwm.offset = le16_to_cpu(p->header.nextindex) - 1;
2328 BT_MARK_DIRTY(mp, ip);
2331 * scan backward leaf page entries
2333 for (; index >= XTENTRYSTART; index--) {
2334 xad = &p->xad[index];
2335 xoff = offsetXAD(xad);
2336 xlen = lengthXAD(xad);
2337 xaddr = addressXAD(xad);
2340 * The "data" for a directory is indexed by the block
2341 * device's address space. This metadata must be invalidated
2344 if (S_ISDIR(ip->i_mode) && (teof == 0))
2345 invalidate_xad_metapages(ip, *xad);
2347 * entry beyond eof: continue scan of current page
2349 * ---|---=======------->
2358 * (xoff <= teof): last entry to be deleted from page;
2359 * If other entries remain in page: keep and update the page.
2363 * eof == entry_start: delete the entry
2365 * -------|=======------->
2372 if (index == XTENTRYSTART)
2378 * eof within the entry: truncate the entry.
2380 * -------===|===------->
2383 else if (teof < xoff + xlen) {
2384 /* update truncated entry */
2386 freexlen = xlen - len;
2387 XADlength(xad, len);
2389 /* save pxd of truncated extent in tlck */
2391 if (log) { /* COMMIT_PWMAP */
2392 xtlck->lwm.offset = (xtlck->lwm.offset) ?
2393 min(index, (int)xtlck->lwm.offset) : index;
2394 xtlck->lwm.length = index + 1 -
2396 xtlck->twm.offset = index;
2397 pxdlock = (struct pxd_lock *) & xtlck->pxdlock;
2398 pxdlock->flag = mlckFREEPXD;
2399 PXDaddress(&pxdlock->pxd, xaddr);
2400 PXDlength(&pxdlock->pxd, freexlen);
2402 /* free truncated extent */
2403 else { /* COMMIT_WMAP */
2405 pxdlock = (struct pxd_lock *) & xadlock;
2406 pxdlock->flag = mlckFREEPXD;
2407 PXDaddress(&pxdlock->pxd, xaddr);
2408 PXDlength(&pxdlock->pxd, freexlen);
2409 txFreeMap(ip, pxdlock, NULL, COMMIT_WMAP);
2411 /* reset map lock */
2412 xadlock.flag = mlckFREEXADLIST;
2415 /* current entry is new last entry; */
2416 nextindex = index + 1;
2421 * eof beyond the entry:
2423 * -------=======---|--->
2426 else { /* (xoff + xlen < teof) */
2428 nextindex = index + 1;
2431 if (nextindex < le16_to_cpu(p->header.nextindex)) {
2432 if (!log) { /* COMMIT_WAMP */
2433 xadlock.xdlist = &p->xad[nextindex];
2435 le16_to_cpu(p->header.nextindex) -
2437 txFreeMap(ip, (struct maplock *) & xadlock,
2440 p->header.nextindex = cpu_to_le16(nextindex);
2445 /* assert(freed == 0); */
2447 } /* end scan of leaf page entries */
2452 * leaf page become empty: free the page if type != PMAP
2454 if (log) { /* COMMIT_PWMAP */
2455 /* txCommit() with tlckFREE:
2456 * free data extents covered by leaf [XTENTRYSTART:hwm);
2457 * invalidate leaf if COMMIT_PWMAP;
2458 * if (TRUNCATE), will write LOG_NOREDOPAGE;
2460 tlck->type = tlckXTREE | tlckFREE;
2461 } else { /* COMMIT_WAMP */
2463 /* free data extents covered by leaf */
2464 xadlock.xdlist = &p->xad[XTENTRYSTART];
2466 le16_to_cpu(p->header.nextindex) - XTENTRYSTART;
2467 txFreeMap(ip, (struct maplock *) & xadlock, NULL, COMMIT_WMAP);
2470 if (p->header.flag & BT_ROOT) {
2471 p->header.flag &= ~BT_INTERNAL;
2472 p->header.flag |= BT_LEAF;
2473 p->header.nextindex = cpu_to_le16(XTENTRYSTART);
2475 XT_PUTPAGE(mp); /* debug */
2478 if (log) { /* COMMIT_PWMAP */
2479 /* page will be invalidated at tx completion
2482 } else { /* COMMIT_WMAP */
2485 lid_to_tlock(mp->lid)->flag |= tlckFREELOCK;
2487 /* invalidate empty leaf page */
2488 discard_metapage(mp);
2493 * the leaf page become empty: delete the parent entry
2494 * for the leaf page if the parent page is to be kept
2495 * in the new sized file.
2499 * go back up to the parent page
2502 /* pop/restore parent entry for the current child page */
2503 if ((parent = BT_POP(&btstack)) == NULL)
2504 /* current page must have been root */
2507 /* get back the parent page */
2509 XT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
2513 index = parent->index;
2516 * child page was not empty:
2519 /* has any entry deleted from parent ? */
2520 if (index < le16_to_cpu(p->header.nextindex) - 1) {
2521 /* (re)acquire tlock on the parent page */
2522 if (log) { /* COMMIT_PWMAP */
2523 /* txCommit() with tlckTRUNCATE:
2524 * free child extents covered by parent [);
2526 tlck = txLock(tid, ip, mp, tlckXTREE);
2527 xtlck = (struct xtlock *) & tlck->lock;
2528 if (!(tlck->type & tlckTRUNCATE)) {
2530 le16_to_cpu(p->header.
2533 tlckXTREE | tlckTRUNCATE;
2535 } else { /* COMMIT_WMAP */
2537 /* free child extents covered by parent */
2538 xadlock.xdlist = &p->xad[index + 1];
2540 le16_to_cpu(p->header.nextindex) -
2542 txFreeMap(ip, (struct maplock *) & xadlock,
2545 BT_MARK_DIRTY(mp, ip);
2547 p->header.nextindex = cpu_to_le16(index + 1);
2554 * child page was empty:
2556 nfreed += lengthXAD(&p->xad[index]);
2559 * During working map update, child page's tlock must be handled
2560 * before parent's. This is because the parent's tlock will cause
2561 * the child's disk space to be marked available in the wmap, so
2562 * it's important that the child page be released by that time.
2564 * ToDo: tlocks should be on doubly-linked list, so we can
2565 * quickly remove it and add it to the end.
2569 * Move parent page's tlock to the end of the tid's tlock list
2571 if (log && mp->lid && (tblk->last != mp->lid) &&
2572 lid_to_tlock(mp->lid)->tid) {
2573 lid_t lid = mp->lid;
2576 tlck = lid_to_tlock(lid);
2578 if (tblk->next == lid)
2579 tblk->next = tlck->next;
2581 for (prev = lid_to_tlock(tblk->next);
2583 prev = lid_to_tlock(prev->next)) {
2586 prev->next = tlck->next;
2588 lid_to_tlock(tblk->last)->next = lid;
2594 * parent page become empty: free the page
2596 if (index == XTENTRYSTART) {
2597 if (log) { /* COMMIT_PWMAP */
2598 /* txCommit() with tlckFREE:
2599 * free child extents covered by parent;
2600 * invalidate parent if COMMIT_PWMAP;
2602 tlck = txLock(tid, ip, mp, tlckXTREE);
2603 xtlck = (struct xtlock *) & tlck->lock;
2605 le16_to_cpu(p->header.nextindex) - 1;
2606 tlck->type = tlckXTREE | tlckFREE;
2607 } else { /* COMMIT_WMAP */
2609 /* free child extents covered by parent */
2610 xadlock.xdlist = &p->xad[XTENTRYSTART];
2612 le16_to_cpu(p->header.nextindex) -
2614 txFreeMap(ip, (struct maplock *) & xadlock, NULL,
2617 BT_MARK_DIRTY(mp, ip);
2619 if (p->header.flag & BT_ROOT) {
2620 p->header.flag &= ~BT_INTERNAL;
2621 p->header.flag |= BT_LEAF;
2622 p->header.nextindex = cpu_to_le16(XTENTRYSTART);
2623 if (le16_to_cpu(p->header.maxentry) == XTROOTMAXSLOT) {
2625 * Shrink root down to allow inline
2626 * EA (otherwise fsck complains)
2628 p->header.maxentry =
2629 cpu_to_le16(XTROOTINITSLOT);
2630 JFS_IP(ip)->mode2 |= INLINEEA;
2633 XT_PUTPAGE(mp); /* debug */
2636 if (log) { /* COMMIT_PWMAP */
2637 /* page will be invalidated at tx completion
2640 } else { /* COMMIT_WMAP */
2643 lid_to_tlock(mp->lid)->flag |=
2646 /* invalidate parent page */
2647 discard_metapage(mp);
2650 /* parent has become empty and freed:
2651 * go back up to its parent page
2658 * parent page still has entries for front region;
2661 /* try truncate region covered by preceding entry
2662 * (process backward)
2666 /* go back down to the child page corresponding
2673 * internal page: go down to child page of current entry
2676 /* save current parent entry for the child page */
2677 if (BT_STACK_FULL(&btstack)) {
2678 jfs_error(ip->i_sb, "stack overrun!\n");
2682 BT_PUSH(&btstack, bn, index);
2684 /* get child page */
2685 xad = &p->xad[index];
2686 bn = addressXAD(xad);
2689 * first access of each internal entry:
2691 /* release parent page */
2694 /* process the child page */
2699 * update file resource stat
2703 if (S_ISDIR(ip->i_mode) && !newsize)
2704 ip->i_size = 1; /* fsck hates zero-length directories */
2706 ip->i_size = newsize;
2708 /* update quota allocation to reflect freed blocks */
2709 dquot_free_block(ip, nfreed);
2712 * free tlock of invalidated pages
2714 if (flag == COMMIT_WMAP)
2725 * Perform truncate to zero length for deleted file, leaving the
2726 * xtree and working map untouched. This allows the file to
2727 * be accessed via open file handles, while the delete of the file
2728 * is committed to disk.
2733 * s64 committed_size)
2735 * return: new committed size
2739 * To avoid deadlock by holding too many transaction locks, the
2740 * truncation may be broken up into multiple transactions.
2741 * The committed_size keeps track of part of the file has been
2742 * freed from the pmaps.
2744 s64 xtTruncate_pmap(tid_t tid, struct inode *ip, s64 committed_size)
2747 struct btstack btstack;
2750 int locked_leaves = 0;
2751 struct metapage *mp;
2753 struct btframe *parent;
2755 struct tblock *tblk;
2756 struct tlock *tlck = NULL;
2760 struct xtlock *xtlck = NULL;
2762 /* save object truncation type */
2763 tblk = tid_to_tblock(tid);
2764 tblk->xflag |= COMMIT_PMAP;
2769 if (committed_size) {
2770 xoff = (committed_size >> JFS_SBI(ip->i_sb)->l2bsize) - 1;
2771 rc = xtSearch(ip, xoff, NULL, &cmp, &btstack, 0);
2775 XT_GETSEARCH(ip, btstack.top, bn, mp, p, index);
2779 jfs_error(ip->i_sb, "did not find extent\n");
2786 * root resides in the inode
2791 * first access of each page:
2794 XT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
2798 /* process entries backward from last index */
2799 index = le16_to_cpu(p->header.nextindex) - 1;
2801 if (p->header.flag & BT_INTERNAL)
2809 if (++locked_leaves > MAX_TRUNCATE_LEAVES) {
2811 * We need to limit the size of the transaction
2812 * to avoid exhausting pagecache & tlocks
2814 xad = &p->xad[index];
2815 xoff = offsetXAD(xad);
2816 xlen = lengthXAD(xad);
2818 return (xoff + xlen) << JFS_SBI(ip->i_sb)->l2bsize;
2820 tlck = txLock(tid, ip, mp, tlckXTREE);
2821 tlck->type = tlckXTREE | tlckFREE;
2822 xtlck = (struct xtlock *) & tlck->lock;
2823 xtlck->hwm.offset = index;
2829 * go back up to the parent page
2832 /* pop/restore parent entry for the current child page */
2833 if ((parent = BT_POP(&btstack)) == NULL)
2834 /* current page must have been root */
2837 /* get back the parent page */
2839 XT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
2843 index = parent->index;
2846 * parent page become empty: free the page
2848 if (index == XTENTRYSTART) {
2849 /* txCommit() with tlckFREE:
2850 * free child extents covered by parent;
2851 * invalidate parent if COMMIT_PWMAP;
2853 tlck = txLock(tid, ip, mp, tlckXTREE);
2854 xtlck = (struct xtlock *) & tlck->lock;
2855 xtlck->hwm.offset = le16_to_cpu(p->header.nextindex) - 1;
2856 tlck->type = tlckXTREE | tlckFREE;
2860 if (p->header.flag & BT_ROOT) {
2868 * parent page still has entries for front region;
2873 * internal page: go down to child page of current entry
2876 /* save current parent entry for the child page */
2877 if (BT_STACK_FULL(&btstack)) {
2878 jfs_error(ip->i_sb, "stack overrun!\n");
2882 BT_PUSH(&btstack, bn, index);
2884 /* get child page */
2885 xad = &p->xad[index];
2886 bn = addressXAD(xad);
2889 * first access of each internal entry:
2891 /* release parent page */
2894 /* process the child page */
2902 #ifdef CONFIG_JFS_STATISTICS
2903 int jfs_xtstat_proc_show(struct seq_file *m, void *v)
2906 "JFS Xtree statistics\n"
2907 "====================\n"
2909 "fast searches = %d\n"