Linux-2.6.12-rc2
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / jfs / jfs_xtree.c
blob11c58c54b818e8bfba8e9affffc1e834a987b620
1 /*
2 * Copyright (C) International Business Machines Corp., 2000-2004
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
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
22 #include <linux/fs.h>
23 #include <linux/quotaops.h>
24 #include "jfs_incore.h"
25 #include "jfs_filsys.h"
26 #include "jfs_metapage.h"
27 #include "jfs_dmap.h"
28 #include "jfs_dinode.h"
29 #include "jfs_superblock.h"
30 #include "jfs_debug.h"
33 * xtree local flag
35 #define XT_INSERT 0x00000001
38 * xtree key/entry comparison: extent offset
40 * return:
41 * -1: k < start of extent
42 * 0: start_of_extent <= k <= end_of_extent
43 * 1: k > end_of_extent
45 #define XT_CMP(CMP, K, X, OFFSET64)\
47 OFFSET64 = offsetXAD(X);\
48 (CMP) = ((K) >= OFFSET64 + lengthXAD(X)) ? 1 :\
49 ((K) < OFFSET64) ? -1 : 0;\
52 /* write a xad entry */
53 #define XT_PUTENTRY(XAD, FLAG, OFF, LEN, ADDR)\
55 (XAD)->flag = (FLAG);\
56 XADoffset((XAD), (OFF));\
57 XADlength((XAD), (LEN));\
58 XADaddress((XAD), (ADDR));\
61 #define XT_PAGE(IP, MP) BT_PAGE(IP, MP, xtpage_t, i_xtroot)
63 /* get page buffer for specified block address */
64 /* ToDo: Replace this ugly macro with a function */
65 #define XT_GETPAGE(IP, BN, MP, SIZE, P, RC)\
67 BT_GETPAGE(IP, BN, MP, xtpage_t, SIZE, P, RC, i_xtroot)\
68 if (!(RC))\
70 if ((le16_to_cpu((P)->header.nextindex) < XTENTRYSTART) ||\
71 (le16_to_cpu((P)->header.nextindex) > le16_to_cpu((P)->header.maxentry)) ||\
72 (le16_to_cpu((P)->header.maxentry) > (((BN)==0)?XTROOTMAXSLOT:PSIZE>>L2XTSLOTSIZE)))\
74 jfs_error((IP)->i_sb, "XT_GETPAGE: xtree page corrupt");\
75 BT_PUTPAGE(MP);\
76 MP = NULL;\
77 RC = -EIO;\
82 /* for consistency */
83 #define XT_PUTPAGE(MP) BT_PUTPAGE(MP)
85 #define XT_GETSEARCH(IP, LEAF, BN, MP, P, INDEX) \
86 BT_GETSEARCH(IP, LEAF, BN, MP, xtpage_t, P, INDEX, i_xtroot)
87 /* xtree entry parameter descriptor */
88 struct xtsplit {
89 struct metapage *mp;
90 s16 index;
91 u8 flag;
92 s64 off;
93 s64 addr;
94 int len;
95 struct pxdlist *pxdlist;
100 * statistics
102 #ifdef CONFIG_JFS_STATISTICS
103 static struct {
104 uint search;
105 uint fastSearch;
106 uint split;
107 } xtStat;
108 #endif
112 * forward references
114 static int xtSearch(struct inode *ip,
115 s64 xoff, int *cmpp, struct btstack * btstack, int flag);
117 static int xtSplitUp(tid_t tid,
118 struct inode *ip,
119 struct xtsplit * split, struct btstack * btstack);
121 static int xtSplitPage(tid_t tid, struct inode *ip, struct xtsplit * split,
122 struct metapage ** rmpp, s64 * rbnp);
124 static int xtSplitRoot(tid_t tid, struct inode *ip,
125 struct xtsplit * split, struct metapage ** rmpp);
127 #ifdef _STILL_TO_PORT
128 static int xtDeleteUp(tid_t tid, struct inode *ip, struct metapage * fmp,
129 xtpage_t * fp, struct btstack * btstack);
131 static int xtSearchNode(struct inode *ip,
132 xad_t * xad,
133 int *cmpp, struct btstack * btstack, int flag);
135 static int xtRelink(tid_t tid, struct inode *ip, xtpage_t * fp);
136 #endif /* _STILL_TO_PORT */
138 /* External references */
141 * debug control
143 /* #define _JFS_DEBUG_XTREE 1 */
147 * xtLookup()
149 * function: map a single page into a physical extent;
151 int xtLookup(struct inode *ip, s64 lstart,
152 s64 llen, int *pflag, s64 * paddr, s32 * plen, int no_check)
154 int rc = 0;
155 struct btstack btstack;
156 int cmp;
157 s64 bn;
158 struct metapage *mp;
159 xtpage_t *p;
160 int index;
161 xad_t *xad;
162 s64 size, xoff, xend;
163 int xlen;
164 s64 xaddr;
166 *plen = 0;
168 if (!no_check) {
169 /* is lookup offset beyond eof ? */
170 size = ((u64) ip->i_size + (JFS_SBI(ip->i_sb)->bsize - 1)) >>
171 JFS_SBI(ip->i_sb)->l2bsize;
172 if (lstart >= size) {
173 jfs_err("xtLookup: lstart (0x%lx) >= size (0x%lx)",
174 (ulong) lstart, (ulong) size);
175 return 0;
180 * search for the xad entry covering the logical extent
182 //search:
183 if ((rc = xtSearch(ip, lstart, &cmp, &btstack, 0))) {
184 jfs_err("xtLookup: xtSearch returned %d", rc);
185 return rc;
189 * compute the physical extent covering logical extent
191 * N.B. search may have failed (e.g., hole in sparse file),
192 * and returned the index of the next entry.
194 /* retrieve search result */
195 XT_GETSEARCH(ip, btstack.top, bn, mp, p, index);
197 /* is xad found covering start of logical extent ?
198 * lstart is a page start address,
199 * i.e., lstart cannot start in a hole;
201 if (cmp)
202 goto out;
205 * lxd covered by xad
207 xad = &p->xad[index];
208 xoff = offsetXAD(xad);
209 xlen = lengthXAD(xad);
210 xend = xoff + xlen;
211 xaddr = addressXAD(xad);
213 /* initialize new pxd */
214 *pflag = xad->flag;
215 *paddr = xaddr + (lstart - xoff);
216 /* a page must be fully covered by an xad */
217 *plen = min(xend - lstart, llen);
219 out:
220 XT_PUTPAGE(mp);
222 return rc;
227 * xtLookupList()
229 * function: map a single logical extent into a list of physical extent;
231 * parameter:
232 * struct inode *ip,
233 * struct lxdlist *lxdlist, lxd list (in)
234 * struct xadlist *xadlist, xad list (in/out)
235 * int flag)
237 * coverage of lxd by xad under assumption of
238 * . lxd's are ordered and disjoint.
239 * . xad's are ordered and disjoint.
241 * return:
242 * 0: success
244 * note: a page being written (even a single byte) is backed fully,
245 * except the last page which is only backed with blocks
246 * required to cover the last byte;
247 * the extent backing a page is fully contained within an xad;
249 int xtLookupList(struct inode *ip, struct lxdlist * lxdlist,
250 struct xadlist * xadlist, int flag)
252 int rc = 0;
253 struct btstack btstack;
254 int cmp;
255 s64 bn;
256 struct metapage *mp;
257 xtpage_t *p;
258 int index;
259 lxd_t *lxd;
260 xad_t *xad, *pxd;
261 s64 size, lstart, lend, xstart, xend, pstart;
262 s64 llen, xlen, plen;
263 s64 xaddr, paddr;
264 int nlxd, npxd, maxnpxd;
266 npxd = xadlist->nxad = 0;
267 maxnpxd = xadlist->maxnxad;
268 pxd = xadlist->xad;
270 nlxd = lxdlist->nlxd;
271 lxd = lxdlist->lxd;
273 lstart = offsetLXD(lxd);
274 llen = lengthLXD(lxd);
275 lend = lstart + llen;
277 size = (ip->i_size + (JFS_SBI(ip->i_sb)->bsize - 1)) >>
278 JFS_SBI(ip->i_sb)->l2bsize;
281 * search for the xad entry covering the logical extent
283 search:
284 if (lstart >= size)
285 return 0;
287 if ((rc = xtSearch(ip, lstart, &cmp, &btstack, 0)))
288 return rc;
291 * compute the physical extent covering logical extent
293 * N.B. search may have failed (e.g., hole in sparse file),
294 * and returned the index of the next entry.
296 //map:
297 /* retrieve search result */
298 XT_GETSEARCH(ip, btstack.top, bn, mp, p, index);
300 /* is xad on the next sibling page ? */
301 if (index == le16_to_cpu(p->header.nextindex)) {
302 if (p->header.flag & BT_ROOT)
303 goto mapend;
305 if ((bn = le64_to_cpu(p->header.next)) == 0)
306 goto mapend;
308 XT_PUTPAGE(mp);
310 /* get next sibling page */
311 XT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
312 if (rc)
313 return rc;
315 index = XTENTRYSTART;
318 xad = &p->xad[index];
321 * is lxd covered by xad ?
323 compare:
324 xstart = offsetXAD(xad);
325 xlen = lengthXAD(xad);
326 xend = xstart + xlen;
327 xaddr = addressXAD(xad);
329 compare1:
330 if (xstart < lstart)
331 goto compare2;
333 /* (lstart <= xstart) */
335 /* lxd is NOT covered by xad */
336 if (lend <= xstart) {
338 * get next lxd
340 if (--nlxd == 0)
341 goto mapend;
342 lxd++;
344 lstart = offsetLXD(lxd);
345 llen = lengthLXD(lxd);
346 lend = lstart + llen;
347 if (lstart >= size)
348 goto mapend;
350 /* compare with the current xad */
351 goto compare1;
353 /* lxd is covered by xad */
354 else { /* (xstart < lend) */
356 /* initialize new pxd */
357 pstart = xstart;
358 plen = min(lend - xstart, xlen);
359 paddr = xaddr;
361 goto cover;
364 /* (xstart < lstart) */
365 compare2:
366 /* lxd is covered by xad */
367 if (lstart < xend) {
368 /* initialize new pxd */
369 pstart = lstart;
370 plen = min(xend - lstart, llen);
371 paddr = xaddr + (lstart - xstart);
373 goto cover;
375 /* lxd is NOT covered by xad */
376 else { /* (xend <= lstart) */
379 * get next xad
381 * linear search next xad covering lxd on
382 * the current xad page, and then tree search
384 if (index == le16_to_cpu(p->header.nextindex) - 1) {
385 if (p->header.flag & BT_ROOT)
386 goto mapend;
388 XT_PUTPAGE(mp);
389 goto search;
390 } else {
391 index++;
392 xad++;
394 /* compare with new xad */
395 goto compare;
400 * lxd is covered by xad and a new pxd has been initialized
401 * (lstart <= xstart < lend) or (xstart < lstart < xend)
403 cover:
404 /* finalize pxd corresponding to current xad */
405 XT_PUTENTRY(pxd, xad->flag, pstart, plen, paddr);
407 if (++npxd >= maxnpxd)
408 goto mapend;
409 pxd++;
412 * lxd is fully covered by xad
414 if (lend <= xend) {
416 * get next lxd
418 if (--nlxd == 0)
419 goto mapend;
420 lxd++;
422 lstart = offsetLXD(lxd);
423 llen = lengthLXD(lxd);
424 lend = lstart + llen;
425 if (lstart >= size)
426 goto mapend;
429 * test for old xad covering new lxd
430 * (old xstart < new lstart)
432 goto compare2;
435 * lxd is partially covered by xad
437 else { /* (xend < lend) */
440 * get next xad
442 * linear search next xad covering lxd on
443 * the current xad page, and then next xad page search
445 if (index == le16_to_cpu(p->header.nextindex) - 1) {
446 if (p->header.flag & BT_ROOT)
447 goto mapend;
449 if ((bn = le64_to_cpu(p->header.next)) == 0)
450 goto mapend;
452 XT_PUTPAGE(mp);
454 /* get next sibling page */
455 XT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
456 if (rc)
457 return rc;
459 index = XTENTRYSTART;
460 xad = &p->xad[index];
461 } else {
462 index++;
463 xad++;
467 * test for new xad covering old lxd
468 * (old lstart < new xstart)
470 goto compare;
473 mapend:
474 xadlist->nxad = npxd;
476 //out:
477 XT_PUTPAGE(mp);
479 return rc;
484 * xtSearch()
486 * function: search for the xad entry covering specified offset.
488 * parameters:
489 * ip - file object;
490 * xoff - extent offset;
491 * cmpp - comparison result:
492 * btstack - traverse stack;
493 * flag - search process flag (XT_INSERT);
495 * returns:
496 * btstack contains (bn, index) of search path traversed to the entry.
497 * *cmpp is set to result of comparison with the entry returned.
498 * the page containing the entry is pinned at exit.
500 static int xtSearch(struct inode *ip, s64 xoff, /* offset of extent */
501 int *cmpp, struct btstack * btstack, int flag)
503 struct jfs_inode_info *jfs_ip = JFS_IP(ip);
504 int rc = 0;
505 int cmp = 1; /* init for empty page */
506 s64 bn; /* block number */
507 struct metapage *mp; /* page buffer */
508 xtpage_t *p; /* page */
509 xad_t *xad;
510 int base, index, lim, btindex;
511 struct btframe *btsp;
512 int nsplit = 0; /* number of pages to split */
513 s64 t64;
515 INCREMENT(xtStat.search);
517 BT_CLR(btstack);
519 btstack->nsplit = 0;
522 * search down tree from root:
524 * between two consecutive entries of <Ki, Pi> and <Kj, Pj> of
525 * internal page, child page Pi contains entry with k, Ki <= K < Kj.
527 * if entry with search key K is not found
528 * internal page search find the entry with largest key Ki
529 * less than K which point to the child page to search;
530 * leaf page search find the entry with smallest key Kj
531 * greater than K so that the returned index is the position of
532 * the entry to be shifted right for insertion of new entry.
533 * for empty tree, search key is greater than any key of the tree.
535 * by convention, root bn = 0.
537 for (bn = 0;;) {
538 /* get/pin the page to search */
539 XT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
540 if (rc)
541 return rc;
543 /* try sequential access heuristics with the previous
544 * access entry in target leaf page:
545 * once search narrowed down into the target leaf,
546 * key must either match an entry in the leaf or
547 * key entry does not exist in the tree;
549 //fastSearch:
550 if ((jfs_ip->btorder & BT_SEQUENTIAL) &&
551 (p->header.flag & BT_LEAF) &&
552 (index = jfs_ip->btindex) <
553 le16_to_cpu(p->header.nextindex)) {
554 xad = &p->xad[index];
555 t64 = offsetXAD(xad);
556 if (xoff < t64 + lengthXAD(xad)) {
557 if (xoff >= t64) {
558 *cmpp = 0;
559 goto out;
562 /* stop sequential access heuristics */
563 goto binarySearch;
564 } else { /* (t64 + lengthXAD(xad)) <= xoff */
566 /* try next sequential entry */
567 index++;
568 if (index <
569 le16_to_cpu(p->header.nextindex)) {
570 xad++;
571 t64 = offsetXAD(xad);
572 if (xoff < t64 + lengthXAD(xad)) {
573 if (xoff >= t64) {
574 *cmpp = 0;
575 goto out;
578 /* miss: key falls between
579 * previous and this entry
581 *cmpp = 1;
582 goto out;
585 /* (xoff >= t64 + lengthXAD(xad));
586 * matching entry may be further out:
587 * stop heuristic search
589 /* stop sequential access heuristics */
590 goto binarySearch;
593 /* (index == p->header.nextindex);
594 * miss: key entry does not exist in
595 * the target leaf/tree
597 *cmpp = 1;
598 goto out;
602 * if hit, return index of the entry found, and
603 * if miss, where new entry with search key is
604 * to be inserted;
606 out:
607 /* compute number of pages to split */
608 if (flag & XT_INSERT) {
609 if (p->header.nextindex == /* little-endian */
610 p->header.maxentry)
611 nsplit++;
612 else
613 nsplit = 0;
614 btstack->nsplit = nsplit;
617 /* save search result */
618 btsp = btstack->top;
619 btsp->bn = bn;
620 btsp->index = index;
621 btsp->mp = mp;
623 /* update sequential access heuristics */
624 jfs_ip->btindex = index;
626 INCREMENT(xtStat.fastSearch);
627 return 0;
630 /* well, ... full search now */
631 binarySearch:
632 lim = le16_to_cpu(p->header.nextindex) - XTENTRYSTART;
635 * binary search with search key K on the current page
637 for (base = XTENTRYSTART; lim; lim >>= 1) {
638 index = base + (lim >> 1);
640 XT_CMP(cmp, xoff, &p->xad[index], t64);
641 if (cmp == 0) {
643 * search hit
645 /* search hit - leaf page:
646 * return the entry found
648 if (p->header.flag & BT_LEAF) {
649 *cmpp = cmp;
651 /* compute number of pages to split */
652 if (flag & XT_INSERT) {
653 if (p->header.nextindex ==
654 p->header.maxentry)
655 nsplit++;
656 else
657 nsplit = 0;
658 btstack->nsplit = nsplit;
661 /* save search result */
662 btsp = btstack->top;
663 btsp->bn = bn;
664 btsp->index = index;
665 btsp->mp = mp;
667 /* init sequential access heuristics */
668 btindex = jfs_ip->btindex;
669 if (index == btindex ||
670 index == btindex + 1)
671 jfs_ip->btorder = BT_SEQUENTIAL;
672 else
673 jfs_ip->btorder = BT_RANDOM;
674 jfs_ip->btindex = index;
676 return 0;
679 /* search hit - internal page:
680 * descend/search its child page
682 goto next;
685 if (cmp > 0) {
686 base = index + 1;
687 --lim;
692 * search miss
694 * base is the smallest index with key (Kj) greater than
695 * search key (K) and may be zero or maxentry index.
698 * search miss - leaf page:
700 * return location of entry (base) where new entry with
701 * search key K is to be inserted.
703 if (p->header.flag & BT_LEAF) {
704 *cmpp = cmp;
706 /* compute number of pages to split */
707 if (flag & XT_INSERT) {
708 if (p->header.nextindex ==
709 p->header.maxentry)
710 nsplit++;
711 else
712 nsplit = 0;
713 btstack->nsplit = nsplit;
716 /* save search result */
717 btsp = btstack->top;
718 btsp->bn = bn;
719 btsp->index = base;
720 btsp->mp = mp;
722 /* init sequential access heuristics */
723 btindex = jfs_ip->btindex;
724 if (base == btindex || base == btindex + 1)
725 jfs_ip->btorder = BT_SEQUENTIAL;
726 else
727 jfs_ip->btorder = BT_RANDOM;
728 jfs_ip->btindex = base;
730 return 0;
734 * search miss - non-leaf page:
736 * if base is non-zero, decrement base by one to get the parent
737 * entry of the child page to search.
739 index = base ? base - 1 : base;
742 * go down to child page
744 next:
745 /* update number of pages to split */
746 if (p->header.nextindex == p->header.maxentry)
747 nsplit++;
748 else
749 nsplit = 0;
751 /* push (bn, index) of the parent page/entry */
752 BT_PUSH(btstack, bn, index);
754 /* get the child page block number */
755 bn = addressXAD(&p->xad[index]);
757 /* unpin the parent page */
758 XT_PUTPAGE(mp);
763 * xtInsert()
765 * function:
767 * parameter:
768 * tid - transaction id;
769 * ip - file object;
770 * xflag - extent flag (XAD_NOTRECORDED):
771 * xoff - extent offset;
772 * xlen - extent length;
773 * xaddrp - extent address pointer (in/out):
774 * if (*xaddrp)
775 * caller allocated data extent at *xaddrp;
776 * else
777 * allocate data extent and return its xaddr;
778 * flag -
780 * return:
782 int xtInsert(tid_t tid, /* transaction id */
783 struct inode *ip, int xflag, s64 xoff, s32 xlen, s64 * xaddrp,
784 int flag)
786 int rc = 0;
787 s64 xaddr, hint;
788 struct metapage *mp; /* meta-page buffer */
789 xtpage_t *p; /* base B+-tree index page */
790 s64 bn;
791 int index, nextindex;
792 struct btstack btstack; /* traverse stack */
793 struct xtsplit split; /* split information */
794 xad_t *xad;
795 int cmp;
796 struct tlock *tlck;
797 struct xtlock *xtlck;
799 jfs_info("xtInsert: nxoff:0x%lx nxlen:0x%x", (ulong) xoff, xlen);
802 * search for the entry location at which to insert:
804 * xtFastSearch() and xtSearch() both returns (leaf page
805 * pinned, index at which to insert).
806 * n.b. xtSearch() may return index of maxentry of
807 * the full page.
809 if ((rc = xtSearch(ip, xoff, &cmp, &btstack, XT_INSERT)))
810 return rc;
812 /* retrieve search result */
813 XT_GETSEARCH(ip, btstack.top, bn, mp, p, index);
815 /* This test must follow XT_GETSEARCH since mp must be valid if
816 * we branch to out: */
817 if (cmp == 0) {
818 rc = -EEXIST;
819 goto out;
823 * allocate data extent requested
825 * allocation hint: last xad
827 if ((xaddr = *xaddrp) == 0) {
828 if (index > XTENTRYSTART) {
829 xad = &p->xad[index - 1];
830 hint = addressXAD(xad) + lengthXAD(xad) - 1;
831 } else
832 hint = 0;
833 if ((rc = DQUOT_ALLOC_BLOCK(ip, xlen)))
834 goto out;
835 if ((rc = dbAlloc(ip, hint, (s64) xlen, &xaddr))) {
836 DQUOT_FREE_BLOCK(ip, xlen);
837 goto out;
842 * insert entry for new extent
844 xflag |= XAD_NEW;
847 * if the leaf page is full, split the page and
848 * propagate up the router entry for the new page from split
850 * The xtSplitUp() will insert the entry and unpin the leaf page.
852 nextindex = le16_to_cpu(p->header.nextindex);
853 if (nextindex == le16_to_cpu(p->header.maxentry)) {
854 split.mp = mp;
855 split.index = index;
856 split.flag = xflag;
857 split.off = xoff;
858 split.len = xlen;
859 split.addr = xaddr;
860 split.pxdlist = NULL;
861 if ((rc = xtSplitUp(tid, ip, &split, &btstack))) {
862 /* undo data extent allocation */
863 if (*xaddrp == 0) {
864 dbFree(ip, xaddr, (s64) xlen);
865 DQUOT_FREE_BLOCK(ip, xlen);
867 return rc;
870 *xaddrp = xaddr;
871 return 0;
875 * insert the new entry into the leaf page
878 * acquire a transaction lock on the leaf page;
880 * action: xad insertion/extension;
882 BT_MARK_DIRTY(mp, ip);
884 /* if insert into middle, shift right remaining entries. */
885 if (index < nextindex)
886 memmove(&p->xad[index + 1], &p->xad[index],
887 (nextindex - index) * sizeof(xad_t));
889 /* insert the new entry: mark the entry NEW */
890 xad = &p->xad[index];
891 XT_PUTENTRY(xad, xflag, xoff, xlen, xaddr);
893 /* advance next available entry index */
894 p->header.nextindex =
895 cpu_to_le16(le16_to_cpu(p->header.nextindex) + 1);
897 /* Don't log it if there are no links to the file */
898 if (!test_cflag(COMMIT_Nolink, ip)) {
899 tlck = txLock(tid, ip, mp, tlckXTREE | tlckGROW);
900 xtlck = (struct xtlock *) & tlck->lock;
901 xtlck->lwm.offset =
902 (xtlck->lwm.offset) ? min(index,
903 (int)xtlck->lwm.offset) : index;
904 xtlck->lwm.length =
905 le16_to_cpu(p->header.nextindex) - xtlck->lwm.offset;
908 *xaddrp = xaddr;
910 out:
911 /* unpin the leaf page */
912 XT_PUTPAGE(mp);
914 return rc;
919 * xtSplitUp()
921 * function:
922 * split full pages as propagating insertion up the tree
924 * parameter:
925 * tid - transaction id;
926 * ip - file object;
927 * split - entry parameter descriptor;
928 * btstack - traverse stack from xtSearch()
930 * return:
932 static int
933 xtSplitUp(tid_t tid,
934 struct inode *ip, struct xtsplit * split, struct btstack * btstack)
936 int rc = 0;
937 struct metapage *smp;
938 xtpage_t *sp; /* split page */
939 struct metapage *rmp;
940 s64 rbn; /* new right page block number */
941 struct metapage *rcmp;
942 xtpage_t *rcp; /* right child page */
943 s64 rcbn; /* right child page block number */
944 int skip; /* index of entry of insertion */
945 int nextindex; /* next available entry index of p */
946 struct btframe *parent; /* parent page entry on traverse stack */
947 xad_t *xad;
948 s64 xaddr;
949 int xlen;
950 int nsplit; /* number of pages split */
951 struct pxdlist pxdlist;
952 pxd_t *pxd;
953 struct tlock *tlck;
954 struct xtlock *xtlck;
956 smp = split->mp;
957 sp = XT_PAGE(ip, smp);
959 /* is inode xtree root extension/inline EA area free ? */
960 if ((sp->header.flag & BT_ROOT) && (!S_ISDIR(ip->i_mode)) &&
961 (le16_to_cpu(sp->header.maxentry) < XTROOTMAXSLOT) &&
962 (JFS_IP(ip)->mode2 & INLINEEA)) {
963 sp->header.maxentry = cpu_to_le16(XTROOTMAXSLOT);
964 JFS_IP(ip)->mode2 &= ~INLINEEA;
966 BT_MARK_DIRTY(smp, ip);
968 * acquire a transaction lock on the leaf page;
970 * action: xad insertion/extension;
973 /* if insert into middle, shift right remaining entries. */
974 skip = split->index;
975 nextindex = le16_to_cpu(sp->header.nextindex);
976 if (skip < nextindex)
977 memmove(&sp->xad[skip + 1], &sp->xad[skip],
978 (nextindex - skip) * sizeof(xad_t));
980 /* insert the new entry: mark the entry NEW */
981 xad = &sp->xad[skip];
982 XT_PUTENTRY(xad, split->flag, split->off, split->len,
983 split->addr);
985 /* advance next available entry index */
986 sp->header.nextindex =
987 cpu_to_le16(le16_to_cpu(sp->header.nextindex) + 1);
989 /* Don't log it if there are no links to the file */
990 if (!test_cflag(COMMIT_Nolink, ip)) {
991 tlck = txLock(tid, ip, smp, tlckXTREE | tlckGROW);
992 xtlck = (struct xtlock *) & tlck->lock;
993 xtlck->lwm.offset = (xtlck->lwm.offset) ?
994 min(skip, (int)xtlck->lwm.offset) : skip;
995 xtlck->lwm.length =
996 le16_to_cpu(sp->header.nextindex) -
997 xtlck->lwm.offset;
1000 return 0;
1004 * allocate new index blocks to cover index page split(s)
1006 * allocation hint: ?
1008 if (split->pxdlist == NULL) {
1009 nsplit = btstack->nsplit;
1010 split->pxdlist = &pxdlist;
1011 pxdlist.maxnpxd = pxdlist.npxd = 0;
1012 pxd = &pxdlist.pxd[0];
1013 xlen = JFS_SBI(ip->i_sb)->nbperpage;
1014 for (; nsplit > 0; nsplit--, pxd++) {
1015 if ((rc = dbAlloc(ip, (s64) 0, (s64) xlen, &xaddr))
1016 == 0) {
1017 PXDaddress(pxd, xaddr);
1018 PXDlength(pxd, xlen);
1020 pxdlist.maxnpxd++;
1022 continue;
1025 /* undo allocation */
1027 XT_PUTPAGE(smp);
1028 return rc;
1033 * Split leaf page <sp> into <sp> and a new right page <rp>.
1035 * The split routines insert the new entry into the leaf page,
1036 * and acquire txLock as appropriate.
1037 * return <rp> pinned and its block number <rpbn>.
1039 rc = (sp->header.flag & BT_ROOT) ?
1040 xtSplitRoot(tid, ip, split, &rmp) :
1041 xtSplitPage(tid, ip, split, &rmp, &rbn);
1043 XT_PUTPAGE(smp);
1045 if (rc)
1046 return -EIO;
1048 * propagate up the router entry for the leaf page just split
1050 * insert a router entry for the new page into the parent page,
1051 * propagate the insert/split up the tree by walking back the stack
1052 * of (bn of parent page, index of child page entry in parent page)
1053 * that were traversed during the search for the page that split.
1055 * the propagation of insert/split up the tree stops if the root
1056 * splits or the page inserted into doesn't have to split to hold
1057 * the new entry.
1059 * the parent entry for the split page remains the same, and
1060 * a new entry is inserted at its right with the first key and
1061 * block number of the new right page.
1063 * There are a maximum of 3 pages pinned at any time:
1064 * right child, left parent and right parent (when the parent splits)
1065 * to keep the child page pinned while working on the parent.
1066 * make sure that all pins are released at exit.
1068 while ((parent = BT_POP(btstack)) != NULL) {
1069 /* parent page specified by stack frame <parent> */
1071 /* keep current child pages <rcp> pinned */
1072 rcmp = rmp;
1073 rcbn = rbn;
1074 rcp = XT_PAGE(ip, rcmp);
1077 * insert router entry in parent for new right child page <rp>
1079 /* get/pin the parent page <sp> */
1080 XT_GETPAGE(ip, parent->bn, smp, PSIZE, sp, rc);
1081 if (rc) {
1082 XT_PUTPAGE(rcmp);
1083 return rc;
1087 * The new key entry goes ONE AFTER the index of parent entry,
1088 * because the split was to the right.
1090 skip = parent->index + 1;
1093 * split or shift right remaining entries of the parent page
1095 nextindex = le16_to_cpu(sp->header.nextindex);
1097 * parent page is full - split the parent page
1099 if (nextindex == le16_to_cpu(sp->header.maxentry)) {
1100 /* init for parent page split */
1101 split->mp = smp;
1102 split->index = skip; /* index at insert */
1103 split->flag = XAD_NEW;
1104 split->off = offsetXAD(&rcp->xad[XTENTRYSTART]);
1105 split->len = JFS_SBI(ip->i_sb)->nbperpage;
1106 split->addr = rcbn;
1108 /* unpin previous right child page */
1109 XT_PUTPAGE(rcmp);
1111 /* The split routines insert the new entry,
1112 * and acquire txLock as appropriate.
1113 * return <rp> pinned and its block number <rpbn>.
1115 rc = (sp->header.flag & BT_ROOT) ?
1116 xtSplitRoot(tid, ip, split, &rmp) :
1117 xtSplitPage(tid, ip, split, &rmp, &rbn);
1118 if (rc) {
1119 XT_PUTPAGE(smp);
1120 return rc;
1123 XT_PUTPAGE(smp);
1124 /* keep new child page <rp> pinned */
1127 * parent page is not full - insert in parent page
1129 else {
1131 * insert router entry in parent for the right child
1132 * page from the first entry of the right child page:
1135 * acquire a transaction lock on the parent page;
1137 * action: router xad insertion;
1139 BT_MARK_DIRTY(smp, ip);
1142 * if insert into middle, shift right remaining entries
1144 if (skip < nextindex)
1145 memmove(&sp->xad[skip + 1], &sp->xad[skip],
1146 (nextindex -
1147 skip) << L2XTSLOTSIZE);
1149 /* insert the router entry */
1150 xad = &sp->xad[skip];
1151 XT_PUTENTRY(xad, XAD_NEW,
1152 offsetXAD(&rcp->xad[XTENTRYSTART]),
1153 JFS_SBI(ip->i_sb)->nbperpage, rcbn);
1155 /* advance next available entry index. */
1156 sp->header.nextindex =
1157 cpu_to_le16(le16_to_cpu(sp->header.nextindex) +
1160 /* Don't log it if there are no links to the file */
1161 if (!test_cflag(COMMIT_Nolink, ip)) {
1162 tlck = txLock(tid, ip, smp,
1163 tlckXTREE | tlckGROW);
1164 xtlck = (struct xtlock *) & tlck->lock;
1165 xtlck->lwm.offset = (xtlck->lwm.offset) ?
1166 min(skip, (int)xtlck->lwm.offset) : skip;
1167 xtlck->lwm.length =
1168 le16_to_cpu(sp->header.nextindex) -
1169 xtlck->lwm.offset;
1172 /* unpin parent page */
1173 XT_PUTPAGE(smp);
1175 /* exit propagate up */
1176 break;
1180 /* unpin current right page */
1181 XT_PUTPAGE(rmp);
1183 return 0;
1188 * xtSplitPage()
1190 * function:
1191 * split a full non-root page into
1192 * original/split/left page and new right page
1193 * i.e., the original/split page remains as left page.
1195 * parameter:
1196 * int tid,
1197 * struct inode *ip,
1198 * struct xtsplit *split,
1199 * struct metapage **rmpp,
1200 * u64 *rbnp,
1202 * return:
1203 * Pointer to page in which to insert or NULL on error.
1205 static int
1206 xtSplitPage(tid_t tid, struct inode *ip,
1207 struct xtsplit * split, struct metapage ** rmpp, s64 * rbnp)
1209 int rc = 0;
1210 struct metapage *smp;
1211 xtpage_t *sp;
1212 struct metapage *rmp;
1213 xtpage_t *rp; /* new right page allocated */
1214 s64 rbn; /* new right page block number */
1215 struct metapage *mp;
1216 xtpage_t *p;
1217 s64 nextbn;
1218 int skip, maxentry, middle, righthalf, n;
1219 xad_t *xad;
1220 struct pxdlist *pxdlist;
1221 pxd_t *pxd;
1222 struct tlock *tlck;
1223 struct xtlock *sxtlck = NULL, *rxtlck = NULL;
1224 int quota_allocation = 0;
1226 smp = split->mp;
1227 sp = XT_PAGE(ip, smp);
1229 INCREMENT(xtStat.split);
1231 pxdlist = split->pxdlist;
1232 pxd = &pxdlist->pxd[pxdlist->npxd];
1233 pxdlist->npxd++;
1234 rbn = addressPXD(pxd);
1236 /* Allocate blocks to quota. */
1237 if (DQUOT_ALLOC_BLOCK(ip, lengthPXD(pxd))) {
1238 rc = -EDQUOT;
1239 goto clean_up;
1242 quota_allocation += lengthPXD(pxd);
1245 * allocate the new right page for the split
1247 rmp = get_metapage(ip, rbn, PSIZE, 1);
1248 if (rmp == NULL) {
1249 rc = -EIO;
1250 goto clean_up;
1253 jfs_info("xtSplitPage: ip:0x%p smp:0x%p rmp:0x%p", ip, smp, rmp);
1255 BT_MARK_DIRTY(rmp, ip);
1257 * action: new page;
1260 rp = (xtpage_t *) rmp->data;
1261 rp->header.self = *pxd;
1262 rp->header.flag = sp->header.flag & BT_TYPE;
1263 rp->header.maxentry = sp->header.maxentry; /* little-endian */
1264 rp->header.nextindex = cpu_to_le16(XTENTRYSTART);
1266 BT_MARK_DIRTY(smp, ip);
1267 /* Don't log it if there are no links to the file */
1268 if (!test_cflag(COMMIT_Nolink, ip)) {
1270 * acquire a transaction lock on the new right page;
1272 tlck = txLock(tid, ip, rmp, tlckXTREE | tlckNEW);
1273 rxtlck = (struct xtlock *) & tlck->lock;
1274 rxtlck->lwm.offset = XTENTRYSTART;
1276 * acquire a transaction lock on the split page
1278 tlck = txLock(tid, ip, smp, tlckXTREE | tlckGROW);
1279 sxtlck = (struct xtlock *) & tlck->lock;
1283 * initialize/update sibling pointers of <sp> and <rp>
1285 nextbn = le64_to_cpu(sp->header.next);
1286 rp->header.next = cpu_to_le64(nextbn);
1287 rp->header.prev = cpu_to_le64(addressPXD(&sp->header.self));
1288 sp->header.next = cpu_to_le64(rbn);
1290 skip = split->index;
1293 * sequential append at tail (after last entry of last page)
1295 * if splitting the last page on a level because of appending
1296 * a entry to it (skip is maxentry), it's likely that the access is
1297 * sequential. adding an empty page on the side of the level is less
1298 * work and can push the fill factor much higher than normal.
1299 * if we're wrong it's no big deal - we will do the split the right
1300 * way next time.
1301 * (it may look like it's equally easy to do a similar hack for
1302 * reverse sorted data, that is, split the tree left, but it's not.
1303 * Be my guest.)
1305 if (nextbn == 0 && skip == le16_to_cpu(sp->header.maxentry)) {
1307 * acquire a transaction lock on the new/right page;
1309 * action: xad insertion;
1311 /* insert entry at the first entry of the new right page */
1312 xad = &rp->xad[XTENTRYSTART];
1313 XT_PUTENTRY(xad, split->flag, split->off, split->len,
1314 split->addr);
1316 rp->header.nextindex = cpu_to_le16(XTENTRYSTART + 1);
1318 if (!test_cflag(COMMIT_Nolink, ip)) {
1319 /* rxtlck->lwm.offset = XTENTRYSTART; */
1320 rxtlck->lwm.length = 1;
1323 *rmpp = rmp;
1324 *rbnp = rbn;
1326 jfs_info("xtSplitPage: sp:0x%p rp:0x%p", sp, rp);
1327 return 0;
1331 * non-sequential insert (at possibly middle page)
1335 * update previous pointer of old next/right page of <sp>
1337 if (nextbn != 0) {
1338 XT_GETPAGE(ip, nextbn, mp, PSIZE, p, rc);
1339 if (rc) {
1340 XT_PUTPAGE(rmp);
1341 goto clean_up;
1344 BT_MARK_DIRTY(mp, ip);
1346 * acquire a transaction lock on the next page;
1348 * action:sibling pointer update;
1350 if (!test_cflag(COMMIT_Nolink, ip))
1351 tlck = txLock(tid, ip, mp, tlckXTREE | tlckRELINK);
1353 p->header.prev = cpu_to_le64(rbn);
1355 /* sibling page may have been updated previously, or
1356 * it may be updated later;
1359 XT_PUTPAGE(mp);
1363 * split the data between the split and new/right pages
1365 maxentry = le16_to_cpu(sp->header.maxentry);
1366 middle = maxentry >> 1;
1367 righthalf = maxentry - middle;
1370 * skip index in old split/left page - insert into left page:
1372 if (skip <= middle) {
1373 /* move right half of split page to the new right page */
1374 memmove(&rp->xad[XTENTRYSTART], &sp->xad[middle],
1375 righthalf << L2XTSLOTSIZE);
1377 /* shift right tail of left half to make room for new entry */
1378 if (skip < middle)
1379 memmove(&sp->xad[skip + 1], &sp->xad[skip],
1380 (middle - skip) << L2XTSLOTSIZE);
1382 /* insert new entry */
1383 xad = &sp->xad[skip];
1384 XT_PUTENTRY(xad, split->flag, split->off, split->len,
1385 split->addr);
1387 /* update page header */
1388 sp->header.nextindex = cpu_to_le16(middle + 1);
1389 if (!test_cflag(COMMIT_Nolink, ip)) {
1390 sxtlck->lwm.offset = (sxtlck->lwm.offset) ?
1391 min(skip, (int)sxtlck->lwm.offset) : skip;
1394 rp->header.nextindex =
1395 cpu_to_le16(XTENTRYSTART + righthalf);
1398 * skip index in new right page - insert into right page:
1400 else {
1401 /* move left head of right half to right page */
1402 n = skip - middle;
1403 memmove(&rp->xad[XTENTRYSTART], &sp->xad[middle],
1404 n << L2XTSLOTSIZE);
1406 /* insert new entry */
1407 n += XTENTRYSTART;
1408 xad = &rp->xad[n];
1409 XT_PUTENTRY(xad, split->flag, split->off, split->len,
1410 split->addr);
1412 /* move right tail of right half to right page */
1413 if (skip < maxentry)
1414 memmove(&rp->xad[n + 1], &sp->xad[skip],
1415 (maxentry - skip) << L2XTSLOTSIZE);
1417 /* update page header */
1418 sp->header.nextindex = cpu_to_le16(middle);
1419 if (!test_cflag(COMMIT_Nolink, ip)) {
1420 sxtlck->lwm.offset = (sxtlck->lwm.offset) ?
1421 min(middle, (int)sxtlck->lwm.offset) : middle;
1424 rp->header.nextindex = cpu_to_le16(XTENTRYSTART +
1425 righthalf + 1);
1428 if (!test_cflag(COMMIT_Nolink, ip)) {
1429 sxtlck->lwm.length = le16_to_cpu(sp->header.nextindex) -
1430 sxtlck->lwm.offset;
1432 /* rxtlck->lwm.offset = XTENTRYSTART; */
1433 rxtlck->lwm.length = le16_to_cpu(rp->header.nextindex) -
1434 XTENTRYSTART;
1437 *rmpp = rmp;
1438 *rbnp = rbn;
1440 jfs_info("xtSplitPage: sp:0x%p rp:0x%p", sp, rp);
1441 return rc;
1443 clean_up:
1445 /* Rollback quota allocation. */
1446 if (quota_allocation)
1447 DQUOT_FREE_BLOCK(ip, quota_allocation);
1449 return (rc);
1454 * xtSplitRoot()
1456 * function:
1457 * split the full root page into
1458 * original/root/split page and new right page
1459 * i.e., root remains fixed in tree anchor (inode) and
1460 * the root is copied to a single new right child page
1461 * since root page << non-root page, and
1462 * the split root page contains a single entry for the
1463 * new right child page.
1465 * parameter:
1466 * int tid,
1467 * struct inode *ip,
1468 * struct xtsplit *split,
1469 * struct metapage **rmpp)
1471 * return:
1472 * Pointer to page in which to insert or NULL on error.
1474 static int
1475 xtSplitRoot(tid_t tid,
1476 struct inode *ip, struct xtsplit * split, struct metapage ** rmpp)
1478 xtpage_t *sp;
1479 struct metapage *rmp;
1480 xtpage_t *rp;
1481 s64 rbn;
1482 int skip, nextindex;
1483 xad_t *xad;
1484 pxd_t *pxd;
1485 struct pxdlist *pxdlist;
1486 struct tlock *tlck;
1487 struct xtlock *xtlck;
1489 sp = &JFS_IP(ip)->i_xtroot;
1491 INCREMENT(xtStat.split);
1494 * allocate a single (right) child page
1496 pxdlist = split->pxdlist;
1497 pxd = &pxdlist->pxd[pxdlist->npxd];
1498 pxdlist->npxd++;
1499 rbn = addressPXD(pxd);
1500 rmp = get_metapage(ip, rbn, PSIZE, 1);
1501 if (rmp == NULL)
1502 return -EIO;
1504 /* Allocate blocks to quota. */
1505 if (DQUOT_ALLOC_BLOCK(ip, lengthPXD(pxd))) {
1506 release_metapage(rmp);
1507 return -EDQUOT;
1510 jfs_info("xtSplitRoot: ip:0x%p rmp:0x%p", ip, rmp);
1513 * acquire a transaction lock on the new right page;
1515 * action: new page;
1517 BT_MARK_DIRTY(rmp, ip);
1519 rp = (xtpage_t *) rmp->data;
1520 rp->header.flag =
1521 (sp->header.flag & BT_LEAF) ? BT_LEAF : BT_INTERNAL;
1522 rp->header.self = *pxd;
1523 rp->header.nextindex = cpu_to_le16(XTENTRYSTART);
1524 rp->header.maxentry = cpu_to_le16(PSIZE >> L2XTSLOTSIZE);
1526 /* initialize sibling pointers */
1527 rp->header.next = 0;
1528 rp->header.prev = 0;
1531 * copy the in-line root page into new right page extent
1533 nextindex = le16_to_cpu(sp->header.maxentry);
1534 memmove(&rp->xad[XTENTRYSTART], &sp->xad[XTENTRYSTART],
1535 (nextindex - XTENTRYSTART) << L2XTSLOTSIZE);
1538 * insert the new entry into the new right/child page
1539 * (skip index in the new right page will not change)
1541 skip = split->index;
1542 /* if insert into middle, shift right remaining entries */
1543 if (skip != nextindex)
1544 memmove(&rp->xad[skip + 1], &rp->xad[skip],
1545 (nextindex - skip) * sizeof(xad_t));
1547 xad = &rp->xad[skip];
1548 XT_PUTENTRY(xad, split->flag, split->off, split->len, split->addr);
1550 /* update page header */
1551 rp->header.nextindex = cpu_to_le16(nextindex + 1);
1553 if (!test_cflag(COMMIT_Nolink, ip)) {
1554 tlck = txLock(tid, ip, rmp, tlckXTREE | tlckNEW);
1555 xtlck = (struct xtlock *) & tlck->lock;
1556 xtlck->lwm.offset = XTENTRYSTART;
1557 xtlck->lwm.length = le16_to_cpu(rp->header.nextindex) -
1558 XTENTRYSTART;
1562 * reset the root
1564 * init root with the single entry for the new right page
1565 * set the 1st entry offset to 0, which force the left-most key
1566 * at any level of the tree to be less than any search key.
1569 * acquire a transaction lock on the root page (in-memory inode);
1571 * action: root split;
1573 BT_MARK_DIRTY(split->mp, ip);
1575 xad = &sp->xad[XTENTRYSTART];
1576 XT_PUTENTRY(xad, XAD_NEW, 0, JFS_SBI(ip->i_sb)->nbperpage, rbn);
1578 /* update page header of root */
1579 sp->header.flag &= ~BT_LEAF;
1580 sp->header.flag |= BT_INTERNAL;
1582 sp->header.nextindex = cpu_to_le16(XTENTRYSTART + 1);
1584 if (!test_cflag(COMMIT_Nolink, ip)) {
1585 tlck = txLock(tid, ip, split->mp, tlckXTREE | tlckGROW);
1586 xtlck = (struct xtlock *) & tlck->lock;
1587 xtlck->lwm.offset = XTENTRYSTART;
1588 xtlck->lwm.length = 1;
1591 *rmpp = rmp;
1593 jfs_info("xtSplitRoot: sp:0x%p rp:0x%p", sp, rp);
1594 return 0;
1599 * xtExtend()
1601 * function: extend in-place;
1603 * note: existing extent may or may not have been committed.
1604 * caller is responsible for pager buffer cache update, and
1605 * working block allocation map update;
1606 * update pmap: alloc whole extended extent;
1608 int xtExtend(tid_t tid, /* transaction id */
1609 struct inode *ip, s64 xoff, /* delta extent offset */
1610 s32 xlen, /* delta extent length */
1611 int flag)
1613 int rc = 0;
1614 int cmp;
1615 struct metapage *mp; /* meta-page buffer */
1616 xtpage_t *p; /* base B+-tree index page */
1617 s64 bn;
1618 int index, nextindex, len;
1619 struct btstack btstack; /* traverse stack */
1620 struct xtsplit split; /* split information */
1621 xad_t *xad;
1622 s64 xaddr;
1623 struct tlock *tlck;
1624 struct xtlock *xtlck = NULL;
1626 jfs_info("xtExtend: nxoff:0x%lx nxlen:0x%x", (ulong) xoff, xlen);
1628 /* there must exist extent to be extended */
1629 if ((rc = xtSearch(ip, xoff - 1, &cmp, &btstack, XT_INSERT)))
1630 return rc;
1632 /* retrieve search result */
1633 XT_GETSEARCH(ip, btstack.top, bn, mp, p, index);
1635 if (cmp != 0) {
1636 XT_PUTPAGE(mp);
1637 jfs_error(ip->i_sb, "xtExtend: xtSearch did not find extent");
1638 return -EIO;
1641 /* extension must be contiguous */
1642 xad = &p->xad[index];
1643 if ((offsetXAD(xad) + lengthXAD(xad)) != xoff) {
1644 XT_PUTPAGE(mp);
1645 jfs_error(ip->i_sb, "xtExtend: extension is not contiguous");
1646 return -EIO;
1650 * acquire a transaction lock on the leaf page;
1652 * action: xad insertion/extension;
1654 BT_MARK_DIRTY(mp, ip);
1655 if (!test_cflag(COMMIT_Nolink, ip)) {
1656 tlck = txLock(tid, ip, mp, tlckXTREE | tlckGROW);
1657 xtlck = (struct xtlock *) & tlck->lock;
1660 /* extend will overflow extent ? */
1661 xlen = lengthXAD(xad) + xlen;
1662 if ((len = xlen - MAXXLEN) <= 0)
1663 goto extendOld;
1666 * extent overflow: insert entry for new extent
1668 //insertNew:
1669 xoff = offsetXAD(xad) + MAXXLEN;
1670 xaddr = addressXAD(xad) + MAXXLEN;
1671 nextindex = le16_to_cpu(p->header.nextindex);
1674 * if the leaf page is full, insert the new entry and
1675 * propagate up the router entry for the new page from split
1677 * The xtSplitUp() will insert the entry and unpin the leaf page.
1679 if (nextindex == le16_to_cpu(p->header.maxentry)) {
1680 /* xtSpliUp() unpins leaf pages */
1681 split.mp = mp;
1682 split.index = index + 1;
1683 split.flag = XAD_NEW;
1684 split.off = xoff; /* split offset */
1685 split.len = len;
1686 split.addr = xaddr;
1687 split.pxdlist = NULL;
1688 if ((rc = xtSplitUp(tid, ip, &split, &btstack)))
1689 return rc;
1691 /* get back old page */
1692 XT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
1693 if (rc)
1694 return rc;
1696 * if leaf root has been split, original root has been
1697 * copied to new child page, i.e., original entry now
1698 * resides on the new child page;
1700 if (p->header.flag & BT_INTERNAL) {
1701 ASSERT(p->header.nextindex ==
1702 cpu_to_le16(XTENTRYSTART + 1));
1703 xad = &p->xad[XTENTRYSTART];
1704 bn = addressXAD(xad);
1705 XT_PUTPAGE(mp);
1707 /* get new child page */
1708 XT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
1709 if (rc)
1710 return rc;
1712 BT_MARK_DIRTY(mp, ip);
1713 if (!test_cflag(COMMIT_Nolink, ip)) {
1714 tlck = txLock(tid, ip, mp, tlckXTREE|tlckGROW);
1715 xtlck = (struct xtlock *) & tlck->lock;
1720 * insert the new entry into the leaf page
1722 else {
1723 /* insert the new entry: mark the entry NEW */
1724 xad = &p->xad[index + 1];
1725 XT_PUTENTRY(xad, XAD_NEW, xoff, len, xaddr);
1727 /* advance next available entry index */
1728 p->header.nextindex =
1729 cpu_to_le16(le16_to_cpu(p->header.nextindex) + 1);
1732 /* get back old entry */
1733 xad = &p->xad[index];
1734 xlen = MAXXLEN;
1737 * extend old extent
1739 extendOld:
1740 XADlength(xad, xlen);
1741 if (!(xad->flag & XAD_NEW))
1742 xad->flag |= XAD_EXTENDED;
1744 if (!test_cflag(COMMIT_Nolink, ip)) {
1745 xtlck->lwm.offset =
1746 (xtlck->lwm.offset) ? min(index,
1747 (int)xtlck->lwm.offset) : index;
1748 xtlck->lwm.length =
1749 le16_to_cpu(p->header.nextindex) - xtlck->lwm.offset;
1752 /* unpin the leaf page */
1753 XT_PUTPAGE(mp);
1755 return rc;
1758 #ifdef _NOTYET
1760 * xtTailgate()
1762 * function: split existing 'tail' extent
1763 * (split offset >= start offset of tail extent), and
1764 * relocate and extend the split tail half;
1766 * note: existing extent may or may not have been committed.
1767 * caller is responsible for pager buffer cache update, and
1768 * working block allocation map update;
1769 * update pmap: free old split tail extent, alloc new extent;
1771 int xtTailgate(tid_t tid, /* transaction id */
1772 struct inode *ip, s64 xoff, /* split/new extent offset */
1773 s32 xlen, /* new extent length */
1774 s64 xaddr, /* new extent address */
1775 int flag)
1777 int rc = 0;
1778 int cmp;
1779 struct metapage *mp; /* meta-page buffer */
1780 xtpage_t *p; /* base B+-tree index page */
1781 s64 bn;
1782 int index, nextindex, llen, rlen;
1783 struct btstack btstack; /* traverse stack */
1784 struct xtsplit split; /* split information */
1785 xad_t *xad;
1786 struct tlock *tlck;
1787 struct xtlock *xtlck = 0;
1788 struct tlock *mtlck;
1789 struct maplock *pxdlock;
1792 printf("xtTailgate: nxoff:0x%lx nxlen:0x%x nxaddr:0x%lx\n",
1793 (ulong)xoff, xlen, (ulong)xaddr);
1796 /* there must exist extent to be tailgated */
1797 if ((rc = xtSearch(ip, xoff, &cmp, &btstack, XT_INSERT)))
1798 return rc;
1800 /* retrieve search result */
1801 XT_GETSEARCH(ip, btstack.top, bn, mp, p, index);
1803 if (cmp != 0) {
1804 XT_PUTPAGE(mp);
1805 jfs_error(ip->i_sb, "xtTailgate: couldn't find extent");
1806 return -EIO;
1809 /* entry found must be last entry */
1810 nextindex = le16_to_cpu(p->header.nextindex);
1811 if (index != nextindex - 1) {
1812 XT_PUTPAGE(mp);
1813 jfs_error(ip->i_sb,
1814 "xtTailgate: the entry found is not the last entry");
1815 return -EIO;
1818 BT_MARK_DIRTY(mp, ip);
1820 * acquire tlock of the leaf page containing original entry
1822 if (!test_cflag(COMMIT_Nolink, ip)) {
1823 tlck = txLock(tid, ip, mp, tlckXTREE | tlckGROW);
1824 xtlck = (struct xtlock *) & tlck->lock;
1827 /* completely replace extent ? */
1828 xad = &p->xad[index];
1830 printf("xtTailgate: xoff:0x%lx xlen:0x%x xaddr:0x%lx\n",
1831 (ulong)offsetXAD(xad), lengthXAD(xad), (ulong)addressXAD(xad));
1833 if ((llen = xoff - offsetXAD(xad)) == 0)
1834 goto updateOld;
1837 * partially replace extent: insert entry for new extent
1839 //insertNew:
1841 * if the leaf page is full, insert the new entry and
1842 * propagate up the router entry for the new page from split
1844 * The xtSplitUp() will insert the entry and unpin the leaf page.
1846 if (nextindex == le16_to_cpu(p->header.maxentry)) {
1847 /* xtSpliUp() unpins leaf pages */
1848 split.mp = mp;
1849 split.index = index + 1;
1850 split.flag = XAD_NEW;
1851 split.off = xoff; /* split offset */
1852 split.len = xlen;
1853 split.addr = xaddr;
1854 split.pxdlist = NULL;
1855 if ((rc = xtSplitUp(tid, ip, &split, &btstack)))
1856 return rc;
1858 /* get back old page */
1859 XT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
1860 if (rc)
1861 return rc;
1863 * if leaf root has been split, original root has been
1864 * copied to new child page, i.e., original entry now
1865 * resides on the new child page;
1867 if (p->header.flag & BT_INTERNAL) {
1868 ASSERT(p->header.nextindex ==
1869 cpu_to_le16(XTENTRYSTART + 1));
1870 xad = &p->xad[XTENTRYSTART];
1871 bn = addressXAD(xad);
1872 XT_PUTPAGE(mp);
1874 /* get new child page */
1875 XT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
1876 if (rc)
1877 return rc;
1879 BT_MARK_DIRTY(mp, ip);
1880 if (!test_cflag(COMMIT_Nolink, ip)) {
1881 tlck = txLock(tid, ip, mp, tlckXTREE|tlckGROW);
1882 xtlck = (struct xtlock *) & tlck->lock;
1887 * insert the new entry into the leaf page
1889 else {
1890 /* insert the new entry: mark the entry NEW */
1891 xad = &p->xad[index + 1];
1892 XT_PUTENTRY(xad, XAD_NEW, xoff, xlen, xaddr);
1894 /* advance next available entry index */
1895 p->header.nextindex =
1896 cpu_to_le16(le16_to_cpu(p->header.nextindex) + 1);
1899 /* get back old XAD */
1900 xad = &p->xad[index];
1903 * truncate/relocate old extent at split offset
1905 updateOld:
1906 /* update dmap for old/committed/truncated extent */
1907 rlen = lengthXAD(xad) - llen;
1908 if (!(xad->flag & XAD_NEW)) {
1909 /* free from PWMAP at commit */
1910 if (!test_cflag(COMMIT_Nolink, ip)) {
1911 mtlck = txMaplock(tid, ip, tlckMAP);
1912 pxdlock = (struct maplock *) & mtlck->lock;
1913 pxdlock->flag = mlckFREEPXD;
1914 PXDaddress(&pxdlock->pxd, addressXAD(xad) + llen);
1915 PXDlength(&pxdlock->pxd, rlen);
1916 pxdlock->index = 1;
1918 } else
1919 /* free from WMAP */
1920 dbFree(ip, addressXAD(xad) + llen, (s64) rlen);
1922 if (llen)
1923 /* truncate */
1924 XADlength(xad, llen);
1925 else
1926 /* replace */
1927 XT_PUTENTRY(xad, XAD_NEW, xoff, xlen, xaddr);
1929 if (!test_cflag(COMMIT_Nolink, ip)) {
1930 xtlck->lwm.offset = (xtlck->lwm.offset) ?
1931 min(index, (int)xtlck->lwm.offset) : index;
1932 xtlck->lwm.length = le16_to_cpu(p->header.nextindex) -
1933 xtlck->lwm.offset;
1936 /* unpin the leaf page */
1937 XT_PUTPAGE(mp);
1939 return rc;
1941 #endif /* _NOTYET */
1944 * xtUpdate()
1946 * function: update XAD;
1948 * update extent for allocated_but_not_recorded or
1949 * compressed extent;
1951 * parameter:
1952 * nxad - new XAD;
1953 * logical extent of the specified XAD must be completely
1954 * contained by an existing XAD;
1956 int xtUpdate(tid_t tid, struct inode *ip, xad_t * nxad)
1957 { /* new XAD */
1958 int rc = 0;
1959 int cmp;
1960 struct metapage *mp; /* meta-page buffer */
1961 xtpage_t *p; /* base B+-tree index page */
1962 s64 bn;
1963 int index0, index, newindex, nextindex;
1964 struct btstack btstack; /* traverse stack */
1965 struct xtsplit split; /* split information */
1966 xad_t *xad, *lxad, *rxad;
1967 int xflag;
1968 s64 nxoff, xoff;
1969 int nxlen, xlen, lxlen, rxlen;
1970 s64 nxaddr, xaddr;
1971 struct tlock *tlck;
1972 struct xtlock *xtlck = NULL;
1973 int newpage = 0;
1975 /* there must exist extent to be tailgated */
1976 nxoff = offsetXAD(nxad);
1977 nxlen = lengthXAD(nxad);
1978 nxaddr = addressXAD(nxad);
1980 if ((rc = xtSearch(ip, nxoff, &cmp, &btstack, XT_INSERT)))
1981 return rc;
1983 /* retrieve search result */
1984 XT_GETSEARCH(ip, btstack.top, bn, mp, p, index0);
1986 if (cmp != 0) {
1987 XT_PUTPAGE(mp);
1988 jfs_error(ip->i_sb, "xtUpdate: Could not find extent");
1989 return -EIO;
1992 BT_MARK_DIRTY(mp, ip);
1994 * acquire tlock of the leaf page containing original entry
1996 if (!test_cflag(COMMIT_Nolink, ip)) {
1997 tlck = txLock(tid, ip, mp, tlckXTREE | tlckGROW);
1998 xtlck = (struct xtlock *) & tlck->lock;
2001 xad = &p->xad[index0];
2002 xflag = xad->flag;
2003 xoff = offsetXAD(xad);
2004 xlen = lengthXAD(xad);
2005 xaddr = addressXAD(xad);
2007 /* nXAD must be completely contained within XAD */
2008 if ((xoff > nxoff) ||
2009 (nxoff + nxlen > xoff + xlen)) {
2010 XT_PUTPAGE(mp);
2011 jfs_error(ip->i_sb,
2012 "xtUpdate: nXAD in not completely contained within XAD");
2013 return -EIO;
2016 index = index0;
2017 newindex = index + 1;
2018 nextindex = le16_to_cpu(p->header.nextindex);
2020 #ifdef _JFS_WIP_NOCOALESCE
2021 if (xoff < nxoff)
2022 goto updateRight;
2025 * replace XAD with nXAD
2027 replace: /* (nxoff == xoff) */
2028 if (nxlen == xlen) {
2029 /* replace XAD with nXAD:recorded */
2030 *xad = *nxad;
2031 xad->flag = xflag & ~XAD_NOTRECORDED;
2033 goto out;
2034 } else /* (nxlen < xlen) */
2035 goto updateLeft;
2036 #endif /* _JFS_WIP_NOCOALESCE */
2038 /* #ifdef _JFS_WIP_COALESCE */
2039 if (xoff < nxoff)
2040 goto coalesceRight;
2043 * coalesce with left XAD
2045 //coalesceLeft: /* (xoff == nxoff) */
2046 /* is XAD first entry of page ? */
2047 if (index == XTENTRYSTART)
2048 goto replace;
2050 /* is nXAD logically and physically contiguous with lXAD ? */
2051 lxad = &p->xad[index - 1];
2052 lxlen = lengthXAD(lxad);
2053 if (!(lxad->flag & XAD_NOTRECORDED) &&
2054 (nxoff == offsetXAD(lxad) + lxlen) &&
2055 (nxaddr == addressXAD(lxad) + lxlen) &&
2056 (lxlen + nxlen < MAXXLEN)) {
2057 /* extend right lXAD */
2058 index0 = index - 1;
2059 XADlength(lxad, lxlen + nxlen);
2061 /* If we just merged two extents together, need to make sure the
2062 * right extent gets logged. If the left one is marked XAD_NEW,
2063 * then we know it will be logged. Otherwise, mark as
2064 * XAD_EXTENDED
2066 if (!(lxad->flag & XAD_NEW))
2067 lxad->flag |= XAD_EXTENDED;
2069 if (xlen > nxlen) {
2070 /* truncate XAD */
2071 XADoffset(xad, xoff + nxlen);
2072 XADlength(xad, xlen - nxlen);
2073 XADaddress(xad, xaddr + nxlen);
2074 goto out;
2075 } else { /* (xlen == nxlen) */
2077 /* remove XAD */
2078 if (index < nextindex - 1)
2079 memmove(&p->xad[index], &p->xad[index + 1],
2080 (nextindex - index -
2081 1) << L2XTSLOTSIZE);
2083 p->header.nextindex =
2084 cpu_to_le16(le16_to_cpu(p->header.nextindex) -
2087 index = index0;
2088 newindex = index + 1;
2089 nextindex = le16_to_cpu(p->header.nextindex);
2090 xoff = nxoff = offsetXAD(lxad);
2091 xlen = nxlen = lxlen + nxlen;
2092 xaddr = nxaddr = addressXAD(lxad);
2093 goto coalesceRight;
2098 * replace XAD with nXAD
2100 replace: /* (nxoff == xoff) */
2101 if (nxlen == xlen) {
2102 /* replace XAD with nXAD:recorded */
2103 *xad = *nxad;
2104 xad->flag = xflag & ~XAD_NOTRECORDED;
2106 goto coalesceRight;
2107 } else /* (nxlen < xlen) */
2108 goto updateLeft;
2111 * coalesce with right XAD
2113 coalesceRight: /* (xoff <= nxoff) */
2114 /* is XAD last entry of page ? */
2115 if (newindex == nextindex) {
2116 if (xoff == nxoff)
2117 goto out;
2118 goto updateRight;
2121 /* is nXAD logically and physically contiguous with rXAD ? */
2122 rxad = &p->xad[index + 1];
2123 rxlen = lengthXAD(rxad);
2124 if (!(rxad->flag & XAD_NOTRECORDED) &&
2125 (nxoff + nxlen == offsetXAD(rxad)) &&
2126 (nxaddr + nxlen == addressXAD(rxad)) &&
2127 (rxlen + nxlen < MAXXLEN)) {
2128 /* extend left rXAD */
2129 XADoffset(rxad, nxoff);
2130 XADlength(rxad, rxlen + nxlen);
2131 XADaddress(rxad, nxaddr);
2133 /* If we just merged two extents together, need to make sure
2134 * the left extent gets logged. If the right one is marked
2135 * XAD_NEW, then we know it will be logged. Otherwise, mark as
2136 * XAD_EXTENDED
2138 if (!(rxad->flag & XAD_NEW))
2139 rxad->flag |= XAD_EXTENDED;
2141 if (xlen > nxlen)
2142 /* truncate XAD */
2143 XADlength(xad, xlen - nxlen);
2144 else { /* (xlen == nxlen) */
2146 /* remove XAD */
2147 memmove(&p->xad[index], &p->xad[index + 1],
2148 (nextindex - index - 1) << L2XTSLOTSIZE);
2150 p->header.nextindex =
2151 cpu_to_le16(le16_to_cpu(p->header.nextindex) -
2155 goto out;
2156 } else if (xoff == nxoff)
2157 goto out;
2159 if (xoff >= nxoff) {
2160 XT_PUTPAGE(mp);
2161 jfs_error(ip->i_sb, "xtUpdate: xoff >= nxoff");
2162 return -EIO;
2164 /* #endif _JFS_WIP_COALESCE */
2167 * split XAD into (lXAD, nXAD):
2169 * |---nXAD--->
2170 * --|----------XAD----------|--
2171 * |-lXAD-|
2173 updateRight: /* (xoff < nxoff) */
2174 /* truncate old XAD as lXAD:not_recorded */
2175 xad = &p->xad[index];
2176 XADlength(xad, nxoff - xoff);
2178 /* insert nXAD:recorded */
2179 if (nextindex == le16_to_cpu(p->header.maxentry)) {
2181 /* xtSpliUp() unpins leaf pages */
2182 split.mp = mp;
2183 split.index = newindex;
2184 split.flag = xflag & ~XAD_NOTRECORDED;
2185 split.off = nxoff;
2186 split.len = nxlen;
2187 split.addr = nxaddr;
2188 split.pxdlist = NULL;
2189 if ((rc = xtSplitUp(tid, ip, &split, &btstack)))
2190 return rc;
2192 /* get back old page */
2193 XT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
2194 if (rc)
2195 return rc;
2197 * if leaf root has been split, original root has been
2198 * copied to new child page, i.e., original entry now
2199 * resides on the new child page;
2201 if (p->header.flag & BT_INTERNAL) {
2202 ASSERT(p->header.nextindex ==
2203 cpu_to_le16(XTENTRYSTART + 1));
2204 xad = &p->xad[XTENTRYSTART];
2205 bn = addressXAD(xad);
2206 XT_PUTPAGE(mp);
2208 /* get new child page */
2209 XT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
2210 if (rc)
2211 return rc;
2213 BT_MARK_DIRTY(mp, ip);
2214 if (!test_cflag(COMMIT_Nolink, ip)) {
2215 tlck = txLock(tid, ip, mp, tlckXTREE|tlckGROW);
2216 xtlck = (struct xtlock *) & tlck->lock;
2218 } else {
2219 /* is nXAD on new page ? */
2220 if (newindex >
2221 (le16_to_cpu(p->header.maxentry) >> 1)) {
2222 newindex =
2223 newindex -
2224 le16_to_cpu(p->header.nextindex) +
2225 XTENTRYSTART;
2226 newpage = 1;
2229 } else {
2230 /* if insert into middle, shift right remaining entries */
2231 if (newindex < nextindex)
2232 memmove(&p->xad[newindex + 1], &p->xad[newindex],
2233 (nextindex - newindex) << L2XTSLOTSIZE);
2235 /* insert the entry */
2236 xad = &p->xad[newindex];
2237 *xad = *nxad;
2238 xad->flag = xflag & ~XAD_NOTRECORDED;
2240 /* advance next available entry index. */
2241 p->header.nextindex =
2242 cpu_to_le16(le16_to_cpu(p->header.nextindex) + 1);
2246 * does nXAD force 3-way split ?
2248 * |---nXAD--->|
2249 * --|----------XAD-------------|--
2250 * |-lXAD-| |-rXAD -|
2252 if (nxoff + nxlen == xoff + xlen)
2253 goto out;
2255 /* reorient nXAD as XAD for further split XAD into (nXAD, rXAD) */
2256 if (newpage) {
2257 /* close out old page */
2258 if (!test_cflag(COMMIT_Nolink, ip)) {
2259 xtlck->lwm.offset = (xtlck->lwm.offset) ?
2260 min(index0, (int)xtlck->lwm.offset) : index0;
2261 xtlck->lwm.length =
2262 le16_to_cpu(p->header.nextindex) -
2263 xtlck->lwm.offset;
2266 bn = le64_to_cpu(p->header.next);
2267 XT_PUTPAGE(mp);
2269 /* get new right page */
2270 XT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
2271 if (rc)
2272 return rc;
2274 BT_MARK_DIRTY(mp, ip);
2275 if (!test_cflag(COMMIT_Nolink, ip)) {
2276 tlck = txLock(tid, ip, mp, tlckXTREE | tlckGROW);
2277 xtlck = (struct xtlock *) & tlck->lock;
2280 index0 = index = newindex;
2281 } else
2282 index++;
2284 newindex = index + 1;
2285 nextindex = le16_to_cpu(p->header.nextindex);
2286 xlen = xlen - (nxoff - xoff);
2287 xoff = nxoff;
2288 xaddr = nxaddr;
2290 /* recompute split pages */
2291 if (nextindex == le16_to_cpu(p->header.maxentry)) {
2292 XT_PUTPAGE(mp);
2294 if ((rc = xtSearch(ip, nxoff, &cmp, &btstack, XT_INSERT)))
2295 return rc;
2297 /* retrieve search result */
2298 XT_GETSEARCH(ip, btstack.top, bn, mp, p, index0);
2300 if (cmp != 0) {
2301 XT_PUTPAGE(mp);
2302 jfs_error(ip->i_sb, "xtUpdate: xtSearch failed");
2303 return -EIO;
2306 if (index0 != index) {
2307 XT_PUTPAGE(mp);
2308 jfs_error(ip->i_sb,
2309 "xtUpdate: unexpected value of index");
2310 return -EIO;
2315 * split XAD into (nXAD, rXAD)
2317 * ---nXAD---|
2318 * --|----------XAD----------|--
2319 * |-rXAD-|
2321 updateLeft: /* (nxoff == xoff) && (nxlen < xlen) */
2322 /* update old XAD with nXAD:recorded */
2323 xad = &p->xad[index];
2324 *xad = *nxad;
2325 xad->flag = xflag & ~XAD_NOTRECORDED;
2327 /* insert rXAD:not_recorded */
2328 xoff = xoff + nxlen;
2329 xlen = xlen - nxlen;
2330 xaddr = xaddr + nxlen;
2331 if (nextindex == le16_to_cpu(p->header.maxentry)) {
2333 printf("xtUpdate.updateLeft.split p:0x%p\n", p);
2335 /* xtSpliUp() unpins leaf pages */
2336 split.mp = mp;
2337 split.index = newindex;
2338 split.flag = xflag;
2339 split.off = xoff;
2340 split.len = xlen;
2341 split.addr = xaddr;
2342 split.pxdlist = NULL;
2343 if ((rc = xtSplitUp(tid, ip, &split, &btstack)))
2344 return rc;
2346 /* get back old page */
2347 XT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
2348 if (rc)
2349 return rc;
2352 * if leaf root has been split, original root has been
2353 * copied to new child page, i.e., original entry now
2354 * resides on the new child page;
2356 if (p->header.flag & BT_INTERNAL) {
2357 ASSERT(p->header.nextindex ==
2358 cpu_to_le16(XTENTRYSTART + 1));
2359 xad = &p->xad[XTENTRYSTART];
2360 bn = addressXAD(xad);
2361 XT_PUTPAGE(mp);
2363 /* get new child page */
2364 XT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
2365 if (rc)
2366 return rc;
2368 BT_MARK_DIRTY(mp, ip);
2369 if (!test_cflag(COMMIT_Nolink, ip)) {
2370 tlck = txLock(tid, ip, mp, tlckXTREE|tlckGROW);
2371 xtlck = (struct xtlock *) & tlck->lock;
2374 } else {
2375 /* if insert into middle, shift right remaining entries */
2376 if (newindex < nextindex)
2377 memmove(&p->xad[newindex + 1], &p->xad[newindex],
2378 (nextindex - newindex) << L2XTSLOTSIZE);
2380 /* insert the entry */
2381 xad = &p->xad[newindex];
2382 XT_PUTENTRY(xad, xflag, xoff, xlen, xaddr);
2384 /* advance next available entry index. */
2385 p->header.nextindex =
2386 cpu_to_le16(le16_to_cpu(p->header.nextindex) + 1);
2389 out:
2390 if (!test_cflag(COMMIT_Nolink, ip)) {
2391 xtlck->lwm.offset = (xtlck->lwm.offset) ?
2392 min(index0, (int)xtlck->lwm.offset) : index0;
2393 xtlck->lwm.length = le16_to_cpu(p->header.nextindex) -
2394 xtlck->lwm.offset;
2397 /* unpin the leaf page */
2398 XT_PUTPAGE(mp);
2400 return rc;
2405 * xtAppend()
2407 * function: grow in append mode from contiguous region specified ;
2409 * parameter:
2410 * tid - transaction id;
2411 * ip - file object;
2412 * xflag - extent flag:
2413 * xoff - extent offset;
2414 * maxblocks - max extent length;
2415 * xlen - extent length (in/out);
2416 * xaddrp - extent address pointer (in/out):
2417 * flag -
2419 * return:
2421 int xtAppend(tid_t tid, /* transaction id */
2422 struct inode *ip, int xflag, s64 xoff, s32 maxblocks,
2423 s32 * xlenp, /* (in/out) */
2424 s64 * xaddrp, /* (in/out) */
2425 int flag)
2427 int rc = 0;
2428 struct metapage *mp; /* meta-page buffer */
2429 xtpage_t *p; /* base B+-tree index page */
2430 s64 bn, xaddr;
2431 int index, nextindex;
2432 struct btstack btstack; /* traverse stack */
2433 struct xtsplit split; /* split information */
2434 xad_t *xad;
2435 int cmp;
2436 struct tlock *tlck;
2437 struct xtlock *xtlck;
2438 int nsplit, nblocks, xlen;
2439 struct pxdlist pxdlist;
2440 pxd_t *pxd;
2442 xaddr = *xaddrp;
2443 xlen = *xlenp;
2444 jfs_info("xtAppend: xoff:0x%lx maxblocks:%d xlen:%d xaddr:0x%lx",
2445 (ulong) xoff, maxblocks, xlen, (ulong) xaddr);
2448 * search for the entry location at which to insert:
2450 * xtFastSearch() and xtSearch() both returns (leaf page
2451 * pinned, index at which to insert).
2452 * n.b. xtSearch() may return index of maxentry of
2453 * the full page.
2455 if ((rc = xtSearch(ip, xoff, &cmp, &btstack, XT_INSERT)))
2456 return rc;
2458 /* retrieve search result */
2459 XT_GETSEARCH(ip, btstack.top, bn, mp, p, index);
2461 if (cmp == 0) {
2462 rc = -EEXIST;
2463 goto out;
2465 //insert:
2467 * insert entry for new extent
2469 xflag |= XAD_NEW;
2472 * if the leaf page is full, split the page and
2473 * propagate up the router entry for the new page from split
2475 * The xtSplitUp() will insert the entry and unpin the leaf page.
2477 nextindex = le16_to_cpu(p->header.nextindex);
2478 if (nextindex < le16_to_cpu(p->header.maxentry))
2479 goto insertLeaf;
2482 * allocate new index blocks to cover index page split(s)
2484 nsplit = btstack.nsplit;
2485 split.pxdlist = &pxdlist;
2486 pxdlist.maxnpxd = pxdlist.npxd = 0;
2487 pxd = &pxdlist.pxd[0];
2488 nblocks = JFS_SBI(ip->i_sb)->nbperpage;
2489 for (; nsplit > 0; nsplit--, pxd++, xaddr += nblocks, maxblocks -= nblocks) {
2490 if ((rc = dbAllocBottomUp(ip, xaddr, (s64) nblocks)) == 0) {
2491 PXDaddress(pxd, xaddr);
2492 PXDlength(pxd, nblocks);
2494 pxdlist.maxnpxd++;
2496 continue;
2499 /* undo allocation */
2501 goto out;
2504 xlen = min(xlen, maxblocks);
2507 * allocate data extent requested
2509 if ((rc = dbAllocBottomUp(ip, xaddr, (s64) xlen)))
2510 goto out;
2512 split.mp = mp;
2513 split.index = index;
2514 split.flag = xflag;
2515 split.off = xoff;
2516 split.len = xlen;
2517 split.addr = xaddr;
2518 if ((rc = xtSplitUp(tid, ip, &split, &btstack))) {
2519 /* undo data extent allocation */
2520 dbFree(ip, *xaddrp, (s64) * xlenp);
2522 return rc;
2525 *xaddrp = xaddr;
2526 *xlenp = xlen;
2527 return 0;
2530 * insert the new entry into the leaf page
2532 insertLeaf:
2534 * allocate data extent requested
2536 if ((rc = dbAllocBottomUp(ip, xaddr, (s64) xlen)))
2537 goto out;
2539 BT_MARK_DIRTY(mp, ip);
2541 * acquire a transaction lock on the leaf page;
2543 * action: xad insertion/extension;
2545 tlck = txLock(tid, ip, mp, tlckXTREE | tlckGROW);
2546 xtlck = (struct xtlock *) & tlck->lock;
2548 /* insert the new entry: mark the entry NEW */
2549 xad = &p->xad[index];
2550 XT_PUTENTRY(xad, xflag, xoff, xlen, xaddr);
2552 /* advance next available entry index */
2553 p->header.nextindex =
2554 cpu_to_le16(le16_to_cpu(p->header.nextindex) + 1);
2556 xtlck->lwm.offset =
2557 (xtlck->lwm.offset) ? min(index,(int) xtlck->lwm.offset) : index;
2558 xtlck->lwm.length = le16_to_cpu(p->header.nextindex) -
2559 xtlck->lwm.offset;
2561 *xaddrp = xaddr;
2562 *xlenp = xlen;
2564 out:
2565 /* unpin the leaf page */
2566 XT_PUTPAGE(mp);
2568 return rc;
2570 #ifdef _STILL_TO_PORT
2572 /* - TBD for defragmentaion/reorganization -
2574 * xtDelete()
2576 * function:
2577 * delete the entry with the specified key.
2579 * N.B.: whole extent of the entry is assumed to be deleted.
2581 * parameter:
2583 * return:
2584 * ENOENT: if the entry is not found.
2586 * exception:
2588 int xtDelete(tid_t tid, struct inode *ip, s64 xoff, s32 xlen, int flag)
2590 int rc = 0;
2591 struct btstack btstack;
2592 int cmp;
2593 s64 bn;
2594 struct metapage *mp;
2595 xtpage_t *p;
2596 int index, nextindex;
2597 struct tlock *tlck;
2598 struct xtlock *xtlck;
2601 * find the matching entry; xtSearch() pins the page
2603 if ((rc = xtSearch(ip, xoff, &cmp, &btstack, 0)))
2604 return rc;
2606 XT_GETSEARCH(ip, btstack.top, bn, mp, p, index);
2607 if (cmp) {
2608 /* unpin the leaf page */
2609 XT_PUTPAGE(mp);
2610 return -ENOENT;
2614 * delete the entry from the leaf page
2616 nextindex = le16_to_cpu(p->header.nextindex);
2617 p->header.nextindex =
2618 cpu_to_le16(le16_to_cpu(p->header.nextindex) - 1);
2621 * if the leaf page bocome empty, free the page
2623 if (p->header.nextindex == cpu_to_le16(XTENTRYSTART))
2624 return (xtDeleteUp(tid, ip, mp, p, &btstack));
2626 BT_MARK_DIRTY(mp, ip);
2628 * acquire a transaction lock on the leaf page;
2630 * action:xad deletion;
2632 tlck = txLock(tid, ip, mp, tlckXTREE);
2633 xtlck = (struct xtlock *) & tlck->lock;
2634 xtlck->lwm.offset =
2635 (xtlck->lwm.offset) ? min(index, xtlck->lwm.offset) : index;
2637 /* if delete from middle, shift left/compact the remaining entries */
2638 if (index < nextindex - 1)
2639 memmove(&p->xad[index], &p->xad[index + 1],
2640 (nextindex - index - 1) * sizeof(xad_t));
2642 XT_PUTPAGE(mp);
2644 return 0;
2648 /* - TBD for defragmentaion/reorganization -
2650 * xtDeleteUp()
2652 * function:
2653 * free empty pages as propagating deletion up the tree
2655 * parameter:
2657 * return:
2659 static int
2660 xtDeleteUp(tid_t tid, struct inode *ip,
2661 struct metapage * fmp, xtpage_t * fp, struct btstack * btstack)
2663 int rc = 0;
2664 struct metapage *mp;
2665 xtpage_t *p;
2666 int index, nextindex;
2667 s64 xaddr;
2668 int xlen;
2669 struct btframe *parent;
2670 struct tlock *tlck;
2671 struct xtlock *xtlck;
2674 * keep root leaf page which has become empty
2676 if (fp->header.flag & BT_ROOT) {
2677 /* keep the root page */
2678 fp->header.flag &= ~BT_INTERNAL;
2679 fp->header.flag |= BT_LEAF;
2680 fp->header.nextindex = cpu_to_le16(XTENTRYSTART);
2682 /* XT_PUTPAGE(fmp); */
2684 return 0;
2688 * free non-root leaf page
2690 if ((rc = xtRelink(tid, ip, fp))) {
2691 XT_PUTPAGE(fmp);
2692 return rc;
2695 xaddr = addressPXD(&fp->header.self);
2696 xlen = lengthPXD(&fp->header.self);
2697 /* free the page extent */
2698 dbFree(ip, xaddr, (s64) xlen);
2700 /* free the buffer page */
2701 discard_metapage(fmp);
2704 * propagate page deletion up the index tree
2706 * If the delete from the parent page makes it empty,
2707 * continue all the way up the tree.
2708 * stop if the root page is reached (which is never deleted) or
2709 * if the entry deletion does not empty the page.
2711 while ((parent = BT_POP(btstack)) != NULL) {
2712 /* get/pin the parent page <sp> */
2713 XT_GETPAGE(ip, parent->bn, mp, PSIZE, p, rc);
2714 if (rc)
2715 return rc;
2717 index = parent->index;
2719 /* delete the entry for the freed child page from parent.
2721 nextindex = le16_to_cpu(p->header.nextindex);
2724 * the parent has the single entry being deleted:
2725 * free the parent page which has become empty.
2727 if (nextindex == 1) {
2728 if (p->header.flag & BT_ROOT) {
2729 /* keep the root page */
2730 p->header.flag &= ~BT_INTERNAL;
2731 p->header.flag |= BT_LEAF;
2732 p->header.nextindex =
2733 cpu_to_le16(XTENTRYSTART);
2735 /* XT_PUTPAGE(mp); */
2737 break;
2738 } else {
2739 /* free the parent page */
2740 if ((rc = xtRelink(tid, ip, p)))
2741 return rc;
2743 xaddr = addressPXD(&p->header.self);
2744 /* free the page extent */
2745 dbFree(ip, xaddr,
2746 (s64) JFS_SBI(ip->i_sb)->nbperpage);
2748 /* unpin/free the buffer page */
2749 discard_metapage(mp);
2751 /* propagate up */
2752 continue;
2756 * the parent has other entries remaining:
2757 * delete the router entry from the parent page.
2759 else {
2760 BT_MARK_DIRTY(mp, ip);
2762 * acquire a transaction lock on the leaf page;
2764 * action:xad deletion;
2766 tlck = txLock(tid, ip, mp, tlckXTREE);
2767 xtlck = (struct xtlock *) & tlck->lock;
2768 xtlck->lwm.offset =
2769 (xtlck->lwm.offset) ? min(index,
2770 xtlck->lwm.
2771 offset) : index;
2773 /* if delete from middle,
2774 * shift left/compact the remaining entries in the page
2776 if (index < nextindex - 1)
2777 memmove(&p->xad[index], &p->xad[index + 1],
2778 (nextindex - index -
2779 1) << L2XTSLOTSIZE);
2781 p->header.nextindex =
2782 cpu_to_le16(le16_to_cpu(p->header.nextindex) -
2784 jfs_info("xtDeleteUp(entry): 0x%lx[%d]",
2785 (ulong) parent->bn, index);
2788 /* unpin the parent page */
2789 XT_PUTPAGE(mp);
2791 /* exit propagation up */
2792 break;
2795 return 0;
2800 * NAME: xtRelocate()
2802 * FUNCTION: relocate xtpage or data extent of regular file;
2803 * This function is mainly used by defragfs utility.
2805 * NOTE: This routine does not have the logic to handle
2806 * uncommitted allocated extent. The caller should call
2807 * txCommit() to commit all the allocation before call
2808 * this routine.
2811 xtRelocate(tid_t tid, struct inode * ip, xad_t * oxad, /* old XAD */
2812 s64 nxaddr, /* new xaddr */
2813 int xtype)
2814 { /* extent type: XTPAGE or DATAEXT */
2815 int rc = 0;
2816 struct tblock *tblk;
2817 struct tlock *tlck;
2818 struct xtlock *xtlck;
2819 struct metapage *mp, *pmp, *lmp, *rmp; /* meta-page buffer */
2820 xtpage_t *p, *pp, *rp, *lp; /* base B+-tree index page */
2821 xad_t *xad;
2822 pxd_t *pxd;
2823 s64 xoff, xsize;
2824 int xlen;
2825 s64 oxaddr, sxaddr, dxaddr, nextbn, prevbn;
2826 cbuf_t *cp;
2827 s64 offset, nbytes, nbrd, pno;
2828 int nb, npages, nblks;
2829 s64 bn;
2830 int cmp;
2831 int index;
2832 struct pxd_lock *pxdlock;
2833 struct btstack btstack; /* traverse stack */
2835 xtype = xtype & EXTENT_TYPE;
2837 xoff = offsetXAD(oxad);
2838 oxaddr = addressXAD(oxad);
2839 xlen = lengthXAD(oxad);
2841 /* validate extent offset */
2842 offset = xoff << JFS_SBI(ip->i_sb)->l2bsize;
2843 if (offset >= ip->i_size)
2844 return -ESTALE; /* stale extent */
2846 jfs_info("xtRelocate: xtype:%d xoff:0x%lx xlen:0x%x xaddr:0x%lx:0x%lx",
2847 xtype, (ulong) xoff, xlen, (ulong) oxaddr, (ulong) nxaddr);
2850 * 1. get and validate the parent xtpage/xad entry
2851 * covering the source extent to be relocated;
2853 if (xtype == DATAEXT) {
2854 /* search in leaf entry */
2855 rc = xtSearch(ip, xoff, &cmp, &btstack, 0);
2856 if (rc)
2857 return rc;
2859 /* retrieve search result */
2860 XT_GETSEARCH(ip, btstack.top, bn, pmp, pp, index);
2862 if (cmp) {
2863 XT_PUTPAGE(pmp);
2864 return -ESTALE;
2867 /* validate for exact match with a single entry */
2868 xad = &pp->xad[index];
2869 if (addressXAD(xad) != oxaddr || lengthXAD(xad) != xlen) {
2870 XT_PUTPAGE(pmp);
2871 return -ESTALE;
2873 } else { /* (xtype == XTPAGE) */
2875 /* search in internal entry */
2876 rc = xtSearchNode(ip, oxad, &cmp, &btstack, 0);
2877 if (rc)
2878 return rc;
2880 /* retrieve search result */
2881 XT_GETSEARCH(ip, btstack.top, bn, pmp, pp, index);
2883 if (cmp) {
2884 XT_PUTPAGE(pmp);
2885 return -ESTALE;
2888 /* xtSearchNode() validated for exact match with a single entry
2890 xad = &pp->xad[index];
2892 jfs_info("xtRelocate: parent xad entry validated.");
2895 * 2. relocate the extent
2897 if (xtype == DATAEXT) {
2898 /* if the extent is allocated-but-not-recorded
2899 * there is no real data to be moved in this extent,
2901 if (xad->flag & XAD_NOTRECORDED)
2902 goto out;
2903 else
2904 /* release xtpage for cmRead()/xtLookup() */
2905 XT_PUTPAGE(pmp);
2908 * cmRelocate()
2910 * copy target data pages to be relocated;
2912 * data extent must start at page boundary and
2913 * multiple of page size (except the last data extent);
2914 * read in each page of the source data extent into cbuf,
2915 * update the cbuf extent descriptor of the page to be
2916 * homeward bound to new dst data extent
2917 * copy the data from the old extent to new extent.
2918 * copy is essential for compressed files to avoid problems
2919 * that can arise if there was a change in compression
2920 * algorithms.
2921 * it is a good strategy because it may disrupt cache
2922 * policy to keep the pages in memory afterwards.
2924 offset = xoff << JFS_SBI(ip->i_sb)->l2bsize;
2925 assert((offset & CM_OFFSET) == 0);
2926 nbytes = xlen << JFS_SBI(ip->i_sb)->l2bsize;
2927 pno = offset >> CM_L2BSIZE;
2928 npages = (nbytes + (CM_BSIZE - 1)) >> CM_L2BSIZE;
2930 npages = ((offset + nbytes - 1) >> CM_L2BSIZE) -
2931 (offset >> CM_L2BSIZE) + 1;
2933 sxaddr = oxaddr;
2934 dxaddr = nxaddr;
2936 /* process the request one cache buffer at a time */
2937 for (nbrd = 0; nbrd < nbytes; nbrd += nb,
2938 offset += nb, pno++, npages--) {
2939 /* compute page size */
2940 nb = min(nbytes - nbrd, CM_BSIZE);
2942 /* get the cache buffer of the page */
2943 if (rc = cmRead(ip, offset, npages, &cp))
2944 break;
2946 assert(addressPXD(&cp->cm_pxd) == sxaddr);
2947 assert(!cp->cm_modified);
2949 /* bind buffer with the new extent address */
2950 nblks = nb >> JFS_IP(ip->i_sb)->l2bsize;
2951 cmSetXD(ip, cp, pno, dxaddr, nblks);
2953 /* release the cbuf, mark it as modified */
2954 cmPut(cp, TRUE);
2956 dxaddr += nblks;
2957 sxaddr += nblks;
2960 /* get back parent page */
2961 if ((rc = xtSearch(ip, xoff, &cmp, &btstack, 0)))
2962 return rc;
2964 XT_GETSEARCH(ip, btstack.top, bn, pmp, pp, index);
2965 jfs_info("xtRelocate: target data extent relocated.");
2966 } else { /* (xtype == XTPAGE) */
2969 * read in the target xtpage from the source extent;
2971 XT_GETPAGE(ip, oxaddr, mp, PSIZE, p, rc);
2972 if (rc) {
2973 XT_PUTPAGE(pmp);
2974 return rc;
2978 * read in sibling pages if any to update sibling pointers;
2980 rmp = NULL;
2981 if (p->header.next) {
2982 nextbn = le64_to_cpu(p->header.next);
2983 XT_GETPAGE(ip, nextbn, rmp, PSIZE, rp, rc);
2984 if (rc) {
2985 XT_PUTPAGE(pmp);
2986 XT_PUTPAGE(mp);
2987 return (rc);
2991 lmp = NULL;
2992 if (p->header.prev) {
2993 prevbn = le64_to_cpu(p->header.prev);
2994 XT_GETPAGE(ip, prevbn, lmp, PSIZE, lp, rc);
2995 if (rc) {
2996 XT_PUTPAGE(pmp);
2997 XT_PUTPAGE(mp);
2998 if (rmp)
2999 XT_PUTPAGE(rmp);
3000 return (rc);
3004 /* at this point, all xtpages to be updated are in memory */
3007 * update sibling pointers of sibling xtpages if any;
3009 if (lmp) {
3010 BT_MARK_DIRTY(lmp, ip);
3011 tlck =
3012 txLock(tid, ip, lmp, tlckXTREE | tlckRELINK);
3013 lp->header.next = cpu_to_le64(nxaddr);
3014 XT_PUTPAGE(lmp);
3017 if (rmp) {
3018 BT_MARK_DIRTY(rmp, ip);
3019 tlck =
3020 txLock(tid, ip, rmp, tlckXTREE | tlckRELINK);
3021 rp->header.prev = cpu_to_le64(nxaddr);
3022 XT_PUTPAGE(rmp);
3026 * update the target xtpage to be relocated
3028 * update the self address of the target page
3029 * and write to destination extent;
3030 * redo image covers the whole xtpage since it is new page
3031 * to the destination extent;
3032 * update of bmap for the free of source extent
3033 * of the target xtpage itself:
3034 * update of bmap for the allocation of destination extent
3035 * of the target xtpage itself:
3036 * update of bmap for the extents covered by xad entries in
3037 * the target xtpage is not necessary since they are not
3038 * updated;
3039 * if not committed before this relocation,
3040 * target page may contain XAD_NEW entries which must
3041 * be scanned for bmap update (logredo() always
3042 * scan xtpage REDOPAGE image for bmap update);
3043 * if committed before this relocation (tlckRELOCATE),
3044 * scan may be skipped by commit() and logredo();
3046 BT_MARK_DIRTY(mp, ip);
3047 /* tlckNEW init xtlck->lwm.offset = XTENTRYSTART; */
3048 tlck = txLock(tid, ip, mp, tlckXTREE | tlckNEW);
3049 xtlck = (struct xtlock *) & tlck->lock;
3051 /* update the self address in the xtpage header */
3052 pxd = &p->header.self;
3053 PXDaddress(pxd, nxaddr);
3055 /* linelock for the after image of the whole page */
3056 xtlck->lwm.length =
3057 le16_to_cpu(p->header.nextindex) - xtlck->lwm.offset;
3059 /* update the buffer extent descriptor of target xtpage */
3060 xsize = xlen << JFS_SBI(ip->i_sb)->l2bsize;
3061 bmSetXD(mp, nxaddr, xsize);
3063 /* unpin the target page to new homeward bound */
3064 XT_PUTPAGE(mp);
3065 jfs_info("xtRelocate: target xtpage relocated.");
3069 * 3. acquire maplock for the source extent to be freed;
3071 * acquire a maplock saving the src relocated extent address;
3072 * to free of the extent at commit time;
3074 out:
3075 /* if DATAEXT relocation, write a LOG_UPDATEMAP record for
3076 * free PXD of the source data extent (logredo() will update
3077 * bmap for free of source data extent), and update bmap for
3078 * free of the source data extent;
3080 if (xtype == DATAEXT)
3081 tlck = txMaplock(tid, ip, tlckMAP);
3082 /* if XTPAGE relocation, write a LOG_NOREDOPAGE record
3083 * for the source xtpage (logredo() will init NoRedoPage
3084 * filter and will also update bmap for free of the source
3085 * xtpage), and update bmap for free of the source xtpage;
3086 * N.B. We use tlckMAP instead of tlkcXTREE because there
3087 * is no buffer associated with this lock since the buffer
3088 * has been redirected to the target location.
3090 else /* (xtype == XTPAGE) */
3091 tlck = txMaplock(tid, ip, tlckMAP | tlckRELOCATE);
3093 pxdlock = (struct pxd_lock *) & tlck->lock;
3094 pxdlock->flag = mlckFREEPXD;
3095 PXDaddress(&pxdlock->pxd, oxaddr);
3096 PXDlength(&pxdlock->pxd, xlen);
3097 pxdlock->index = 1;
3100 * 4. update the parent xad entry for relocation;
3102 * acquire tlck for the parent entry with XAD_NEW as entry
3103 * update which will write LOG_REDOPAGE and update bmap for
3104 * allocation of XAD_NEW destination extent;
3106 jfs_info("xtRelocate: update parent xad entry.");
3107 BT_MARK_DIRTY(pmp, ip);
3108 tlck = txLock(tid, ip, pmp, tlckXTREE | tlckGROW);
3109 xtlck = (struct xtlock *) & tlck->lock;
3111 /* update the XAD with the new destination extent; */
3112 xad = &pp->xad[index];
3113 xad->flag |= XAD_NEW;
3114 XADaddress(xad, nxaddr);
3116 xtlck->lwm.offset = min(index, xtlck->lwm.offset);
3117 xtlck->lwm.length = le16_to_cpu(pp->header.nextindex) -
3118 xtlck->lwm.offset;
3120 /* unpin the parent xtpage */
3121 XT_PUTPAGE(pmp);
3123 return rc;
3128 * xtSearchNode()
3130 * function: search for the internal xad entry covering specified extent.
3131 * This function is mainly used by defragfs utility.
3133 * parameters:
3134 * ip - file object;
3135 * xad - extent to find;
3136 * cmpp - comparison result:
3137 * btstack - traverse stack;
3138 * flag - search process flag;
3140 * returns:
3141 * btstack contains (bn, index) of search path traversed to the entry.
3142 * *cmpp is set to result of comparison with the entry returned.
3143 * the page containing the entry is pinned at exit.
3145 static int xtSearchNode(struct inode *ip, xad_t * xad, /* required XAD entry */
3146 int *cmpp, struct btstack * btstack, int flag)
3148 int rc = 0;
3149 s64 xoff, xaddr;
3150 int xlen;
3151 int cmp = 1; /* init for empty page */
3152 s64 bn; /* block number */
3153 struct metapage *mp; /* meta-page buffer */
3154 xtpage_t *p; /* page */
3155 int base, index, lim;
3156 struct btframe *btsp;
3157 s64 t64;
3159 BT_CLR(btstack);
3161 xoff = offsetXAD(xad);
3162 xlen = lengthXAD(xad);
3163 xaddr = addressXAD(xad);
3166 * search down tree from root:
3168 * between two consecutive entries of <Ki, Pi> and <Kj, Pj> of
3169 * internal page, child page Pi contains entry with k, Ki <= K < Kj.
3171 * if entry with search key K is not found
3172 * internal page search find the entry with largest key Ki
3173 * less than K which point to the child page to search;
3174 * leaf page search find the entry with smallest key Kj
3175 * greater than K so that the returned index is the position of
3176 * the entry to be shifted right for insertion of new entry.
3177 * for empty tree, search key is greater than any key of the tree.
3179 * by convention, root bn = 0.
3181 for (bn = 0;;) {
3182 /* get/pin the page to search */
3183 XT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
3184 if (rc)
3185 return rc;
3186 if (p->header.flag & BT_LEAF) {
3187 XT_PUTPAGE(mp);
3188 return -ESTALE;
3191 lim = le16_to_cpu(p->header.nextindex) - XTENTRYSTART;
3194 * binary search with search key K on the current page
3196 for (base = XTENTRYSTART; lim; lim >>= 1) {
3197 index = base + (lim >> 1);
3199 XT_CMP(cmp, xoff, &p->xad[index], t64);
3200 if (cmp == 0) {
3202 * search hit
3204 * verify for exact match;
3206 if (xaddr == addressXAD(&p->xad[index]) &&
3207 xoff == offsetXAD(&p->xad[index])) {
3208 *cmpp = cmp;
3210 /* save search result */
3211 btsp = btstack->top;
3212 btsp->bn = bn;
3213 btsp->index = index;
3214 btsp->mp = mp;
3216 return 0;
3219 /* descend/search its child page */
3220 goto next;
3223 if (cmp > 0) {
3224 base = index + 1;
3225 --lim;
3230 * search miss - non-leaf page:
3232 * base is the smallest index with key (Kj) greater than
3233 * search key (K) and may be zero or maxentry index.
3234 * if base is non-zero, decrement base by one to get the parent
3235 * entry of the child page to search.
3237 index = base ? base - 1 : base;
3240 * go down to child page
3242 next:
3243 /* get the child page block number */
3244 bn = addressXAD(&p->xad[index]);
3246 /* unpin the parent page */
3247 XT_PUTPAGE(mp);
3253 * xtRelink()
3255 * function:
3256 * link around a freed page.
3258 * Parameter:
3259 * int tid,
3260 * struct inode *ip,
3261 * xtpage_t *p)
3263 * returns:
3265 static int xtRelink(tid_t tid, struct inode *ip, xtpage_t * p)
3267 int rc = 0;
3268 struct metapage *mp;
3269 s64 nextbn, prevbn;
3270 struct tlock *tlck;
3272 nextbn = le64_to_cpu(p->header.next);
3273 prevbn = le64_to_cpu(p->header.prev);
3275 /* update prev pointer of the next page */
3276 if (nextbn != 0) {
3277 XT_GETPAGE(ip, nextbn, mp, PSIZE, p, rc);
3278 if (rc)
3279 return rc;
3282 * acquire a transaction lock on the page;
3284 * action: update prev pointer;
3286 BT_MARK_DIRTY(mp, ip);
3287 tlck = txLock(tid, ip, mp, tlckXTREE | tlckRELINK);
3289 /* the page may already have been tlock'd */
3291 p->header.prev = cpu_to_le64(prevbn);
3293 XT_PUTPAGE(mp);
3296 /* update next pointer of the previous page */
3297 if (prevbn != 0) {
3298 XT_GETPAGE(ip, prevbn, mp, PSIZE, p, rc);
3299 if (rc)
3300 return rc;
3303 * acquire a transaction lock on the page;
3305 * action: update next pointer;
3307 BT_MARK_DIRTY(mp, ip);
3308 tlck = txLock(tid, ip, mp, tlckXTREE | tlckRELINK);
3310 /* the page may already have been tlock'd */
3312 p->header.next = le64_to_cpu(nextbn);
3314 XT_PUTPAGE(mp);
3317 return 0;
3319 #endif /* _STILL_TO_PORT */
3323 * xtInitRoot()
3325 * initialize file root (inline in inode)
3327 void xtInitRoot(tid_t tid, struct inode *ip)
3329 xtpage_t *p;
3332 * acquire a transaction lock on the root
3334 * action:
3336 txLock(tid, ip, (struct metapage *) &JFS_IP(ip)->bxflag,
3337 tlckXTREE | tlckNEW);
3338 p = &JFS_IP(ip)->i_xtroot;
3340 p->header.flag = DXD_INDEX | BT_ROOT | BT_LEAF;
3341 p->header.nextindex = cpu_to_le16(XTENTRYSTART);
3343 if (S_ISDIR(ip->i_mode))
3344 p->header.maxentry = cpu_to_le16(XTROOTINITSLOT_DIR);
3345 else {
3346 p->header.maxentry = cpu_to_le16(XTROOTINITSLOT);
3347 ip->i_size = 0;
3351 return;
3356 * We can run into a deadlock truncating a file with a large number of
3357 * xtree pages (large fragmented file). A robust fix would entail a
3358 * reservation system where we would reserve a number of metadata pages
3359 * and tlocks which we would be guaranteed without a deadlock. Without
3360 * this, a partial fix is to limit number of metadata pages we will lock
3361 * in a single transaction. Currently we will truncate the file so that
3362 * no more than 50 leaf pages will be locked. The caller of xtTruncate
3363 * will be responsible for ensuring that the current transaction gets
3364 * committed, and that subsequent transactions are created to truncate
3365 * the file further if needed.
3367 #define MAX_TRUNCATE_LEAVES 50
3370 * xtTruncate()
3372 * function:
3373 * traverse for truncation logging backward bottom up;
3374 * terminate at the last extent entry at the current subtree
3375 * root page covering new down size.
3376 * truncation may occur within the last extent entry.
3378 * parameter:
3379 * int tid,
3380 * struct inode *ip,
3381 * s64 newsize,
3382 * int type) {PWMAP, PMAP, WMAP; DELETE, TRUNCATE}
3384 * return:
3386 * note:
3387 * PWMAP:
3388 * 1. truncate (non-COMMIT_NOLINK file)
3389 * by jfs_truncate() or jfs_open(O_TRUNC):
3390 * xtree is updated;
3391 * 2. truncate index table of directory when last entry removed
3392 * map update via tlock at commit time;
3393 * PMAP:
3394 * Call xtTruncate_pmap instead
3395 * WMAP:
3396 * 1. remove (free zero link count) on last reference release
3397 * (pmap has been freed at commit zero link count);
3398 * 2. truncate (COMMIT_NOLINK file, i.e., tmp file):
3399 * xtree is updated;
3400 * map update directly at truncation time;
3402 * if (DELETE)
3403 * no LOG_NOREDOPAGE is required (NOREDOFILE is sufficient);
3404 * else if (TRUNCATE)
3405 * must write LOG_NOREDOPAGE for deleted index page;
3407 * pages may already have been tlocked by anonymous transactions
3408 * during file growth (i.e., write) before truncation;
3410 * except last truncated entry, deleted entries remains as is
3411 * in the page (nextindex is updated) for other use
3412 * (e.g., log/update allocation map): this avoid copying the page
3413 * info but delay free of pages;
3416 s64 xtTruncate(tid_t tid, struct inode *ip, s64 newsize, int flag)
3418 int rc = 0;
3419 s64 teof;
3420 struct metapage *mp;
3421 xtpage_t *p;
3422 s64 bn;
3423 int index, nextindex;
3424 xad_t *xad;
3425 s64 xoff, xaddr;
3426 int xlen, len, freexlen;
3427 struct btstack btstack;
3428 struct btframe *parent;
3429 struct tblock *tblk = NULL;
3430 struct tlock *tlck = NULL;
3431 struct xtlock *xtlck = NULL;
3432 struct xdlistlock xadlock; /* maplock for COMMIT_WMAP */
3433 struct pxd_lock *pxdlock; /* maplock for COMMIT_WMAP */
3434 s64 nfreed;
3435 int freed, log;
3436 int locked_leaves = 0;
3438 /* save object truncation type */
3439 if (tid) {
3440 tblk = tid_to_tblock(tid);
3441 tblk->xflag |= flag;
3444 nfreed = 0;
3446 flag &= COMMIT_MAP;
3447 assert(flag != COMMIT_PMAP);
3449 if (flag == COMMIT_PWMAP)
3450 log = 1;
3451 else {
3452 log = 0;
3453 xadlock.flag = mlckFREEXADLIST;
3454 xadlock.index = 1;
3458 * if the newsize is not an integral number of pages,
3459 * the file between newsize and next page boundary will
3460 * be cleared.
3461 * if truncating into a file hole, it will cause
3462 * a full block to be allocated for the logical block.
3466 * release page blocks of truncated region <teof, eof>
3468 * free the data blocks from the leaf index blocks.
3469 * delete the parent index entries corresponding to
3470 * the freed child data/index blocks.
3471 * free the index blocks themselves which aren't needed
3472 * in new sized file.
3474 * index blocks are updated only if the blocks are to be
3475 * retained in the new sized file.
3476 * if type is PMAP, the data and index pages are NOT
3477 * freed, and the data and index blocks are NOT freed
3478 * from working map.
3479 * (this will allow continued access of data/index of
3480 * temporary file (zerolink count file truncated to zero-length)).
3482 teof = (newsize + (JFS_SBI(ip->i_sb)->bsize - 1)) >>
3483 JFS_SBI(ip->i_sb)->l2bsize;
3485 /* clear stack */
3486 BT_CLR(&btstack);
3489 * start with root
3491 * root resides in the inode
3493 bn = 0;
3496 * first access of each page:
3498 getPage:
3499 XT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
3500 if (rc)
3501 return rc;
3503 /* process entries backward from last index */
3504 index = le16_to_cpu(p->header.nextindex) - 1;
3506 if (p->header.flag & BT_INTERNAL)
3507 goto getChild;
3510 * leaf page
3513 /* Since this is the rightmost leaf, and we may have already freed
3514 * a page that was formerly to the right, let's make sure that the
3515 * next pointer is zero.
3517 if (p->header.next) {
3518 if (log)
3520 * Make sure this change to the header is logged.
3521 * If we really truncate this leaf, the flag
3522 * will be changed to tlckTRUNCATE
3524 tlck = txLock(tid, ip, mp, tlckXTREE|tlckGROW);
3525 BT_MARK_DIRTY(mp, ip);
3526 p->header.next = 0;
3529 freed = 0;
3531 /* does region covered by leaf page precede Teof ? */
3532 xad = &p->xad[index];
3533 xoff = offsetXAD(xad);
3534 xlen = lengthXAD(xad);
3535 if (teof >= xoff + xlen) {
3536 XT_PUTPAGE(mp);
3537 goto getParent;
3540 /* (re)acquire tlock of the leaf page */
3541 if (log) {
3542 if (++locked_leaves > MAX_TRUNCATE_LEAVES) {
3544 * We need to limit the size of the transaction
3545 * to avoid exhausting pagecache & tlocks
3547 XT_PUTPAGE(mp);
3548 newsize = (xoff + xlen) << JFS_SBI(ip->i_sb)->l2bsize;
3549 goto getParent;
3551 tlck = txLock(tid, ip, mp, tlckXTREE);
3552 tlck->type = tlckXTREE | tlckTRUNCATE;
3553 xtlck = (struct xtlock *) & tlck->lock;
3554 xtlck->hwm.offset = le16_to_cpu(p->header.nextindex) - 1;
3556 BT_MARK_DIRTY(mp, ip);
3559 * scan backward leaf page entries
3561 for (; index >= XTENTRYSTART; index--) {
3562 xad = &p->xad[index];
3563 xoff = offsetXAD(xad);
3564 xlen = lengthXAD(xad);
3565 xaddr = addressXAD(xad);
3568 * The "data" for a directory is indexed by the block
3569 * device's address space. This metadata must be invalidated
3570 * here
3572 if (S_ISDIR(ip->i_mode) && (teof == 0))
3573 invalidate_xad_metapages(ip, *xad);
3575 * entry beyond eof: continue scan of current page
3576 * xad
3577 * ---|---=======------->
3578 * eof
3580 if (teof < xoff) {
3581 nfreed += xlen;
3582 continue;
3586 * (xoff <= teof): last entry to be deleted from page;
3587 * If other entries remain in page: keep and update the page.
3591 * eof == entry_start: delete the entry
3592 * xad
3593 * -------|=======------->
3594 * eof
3597 if (teof == xoff) {
3598 nfreed += xlen;
3600 if (index == XTENTRYSTART)
3601 break;
3603 nextindex = index;
3606 * eof within the entry: truncate the entry.
3607 * xad
3608 * -------===|===------->
3609 * eof
3611 else if (teof < xoff + xlen) {
3612 /* update truncated entry */
3613 len = teof - xoff;
3614 freexlen = xlen - len;
3615 XADlength(xad, len);
3617 /* save pxd of truncated extent in tlck */
3618 xaddr += len;
3619 if (log) { /* COMMIT_PWMAP */
3620 xtlck->lwm.offset = (xtlck->lwm.offset) ?
3621 min(index, (int)xtlck->lwm.offset) : index;
3622 xtlck->lwm.length = index + 1 -
3623 xtlck->lwm.offset;
3624 xtlck->twm.offset = index;
3625 pxdlock = (struct pxd_lock *) & xtlck->pxdlock;
3626 pxdlock->flag = mlckFREEPXD;
3627 PXDaddress(&pxdlock->pxd, xaddr);
3628 PXDlength(&pxdlock->pxd, freexlen);
3630 /* free truncated extent */
3631 else { /* COMMIT_WMAP */
3633 pxdlock = (struct pxd_lock *) & xadlock;
3634 pxdlock->flag = mlckFREEPXD;
3635 PXDaddress(&pxdlock->pxd, xaddr);
3636 PXDlength(&pxdlock->pxd, freexlen);
3637 txFreeMap(ip, pxdlock, NULL, COMMIT_WMAP);
3639 /* reset map lock */
3640 xadlock.flag = mlckFREEXADLIST;
3643 /* current entry is new last entry; */
3644 nextindex = index + 1;
3646 nfreed += freexlen;
3649 * eof beyond the entry:
3650 * xad
3651 * -------=======---|--->
3652 * eof
3654 else { /* (xoff + xlen < teof) */
3656 nextindex = index + 1;
3659 if (nextindex < le16_to_cpu(p->header.nextindex)) {
3660 if (!log) { /* COMMIT_WAMP */
3661 xadlock.xdlist = &p->xad[nextindex];
3662 xadlock.count =
3663 le16_to_cpu(p->header.nextindex) -
3664 nextindex;
3665 txFreeMap(ip, (struct maplock *) & xadlock,
3666 NULL, COMMIT_WMAP);
3668 p->header.nextindex = cpu_to_le16(nextindex);
3671 XT_PUTPAGE(mp);
3673 /* assert(freed == 0); */
3674 goto getParent;
3675 } /* end scan of leaf page entries */
3677 freed = 1;
3680 * leaf page become empty: free the page if type != PMAP
3682 if (log) { /* COMMIT_PWMAP */
3683 /* txCommit() with tlckFREE:
3684 * free data extents covered by leaf [XTENTRYSTART:hwm);
3685 * invalidate leaf if COMMIT_PWMAP;
3686 * if (TRUNCATE), will write LOG_NOREDOPAGE;
3688 tlck->type = tlckXTREE | tlckFREE;
3689 } else { /* COMMIT_WAMP */
3691 /* free data extents covered by leaf */
3692 xadlock.xdlist = &p->xad[XTENTRYSTART];
3693 xadlock.count =
3694 le16_to_cpu(p->header.nextindex) - XTENTRYSTART;
3695 txFreeMap(ip, (struct maplock *) & xadlock, NULL, COMMIT_WMAP);
3698 if (p->header.flag & BT_ROOT) {
3699 p->header.flag &= ~BT_INTERNAL;
3700 p->header.flag |= BT_LEAF;
3701 p->header.nextindex = cpu_to_le16(XTENTRYSTART);
3703 XT_PUTPAGE(mp); /* debug */
3704 goto out;
3705 } else {
3706 if (log) { /* COMMIT_PWMAP */
3707 /* page will be invalidated at tx completion
3709 XT_PUTPAGE(mp);
3710 } else { /* COMMIT_WMAP */
3712 if (mp->lid)
3713 lid_to_tlock(mp->lid)->flag |= tlckFREELOCK;
3715 /* invalidate empty leaf page */
3716 discard_metapage(mp);
3721 * the leaf page become empty: delete the parent entry
3722 * for the leaf page if the parent page is to be kept
3723 * in the new sized file.
3727 * go back up to the parent page
3729 getParent:
3730 /* pop/restore parent entry for the current child page */
3731 if ((parent = BT_POP(&btstack)) == NULL)
3732 /* current page must have been root */
3733 goto out;
3735 /* get back the parent page */
3736 bn = parent->bn;
3737 XT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
3738 if (rc)
3739 return rc;
3741 index = parent->index;
3744 * child page was not empty:
3746 if (freed == 0) {
3747 /* has any entry deleted from parent ? */
3748 if (index < le16_to_cpu(p->header.nextindex) - 1) {
3749 /* (re)acquire tlock on the parent page */
3750 if (log) { /* COMMIT_PWMAP */
3751 /* txCommit() with tlckTRUNCATE:
3752 * free child extents covered by parent [);
3754 tlck = txLock(tid, ip, mp, tlckXTREE);
3755 xtlck = (struct xtlock *) & tlck->lock;
3756 if (!(tlck->type & tlckTRUNCATE)) {
3757 xtlck->hwm.offset =
3758 le16_to_cpu(p->header.
3759 nextindex) - 1;
3760 tlck->type =
3761 tlckXTREE | tlckTRUNCATE;
3763 } else { /* COMMIT_WMAP */
3765 /* free child extents covered by parent */
3766 xadlock.xdlist = &p->xad[index + 1];
3767 xadlock.count =
3768 le16_to_cpu(p->header.nextindex) -
3769 index - 1;
3770 txFreeMap(ip, (struct maplock *) & xadlock,
3771 NULL, COMMIT_WMAP);
3773 BT_MARK_DIRTY(mp, ip);
3775 p->header.nextindex = cpu_to_le16(index + 1);
3777 XT_PUTPAGE(mp);
3778 goto getParent;
3782 * child page was empty:
3784 nfreed += lengthXAD(&p->xad[index]);
3787 * During working map update, child page's tlock must be handled
3788 * before parent's. This is because the parent's tlock will cause
3789 * the child's disk space to be marked available in the wmap, so
3790 * it's important that the child page be released by that time.
3792 * ToDo: tlocks should be on doubly-linked list, so we can
3793 * quickly remove it and add it to the end.
3797 * Move parent page's tlock to the end of the tid's tlock list
3799 if (log && mp->lid && (tblk->last != mp->lid) &&
3800 lid_to_tlock(mp->lid)->tid) {
3801 lid_t lid = mp->lid;
3802 struct tlock *prev;
3804 tlck = lid_to_tlock(lid);
3806 if (tblk->next == lid)
3807 tblk->next = tlck->next;
3808 else {
3809 for (prev = lid_to_tlock(tblk->next);
3810 prev->next != lid;
3811 prev = lid_to_tlock(prev->next)) {
3812 assert(prev->next);
3814 prev->next = tlck->next;
3816 lid_to_tlock(tblk->last)->next = lid;
3817 tlck->next = 0;
3818 tblk->last = lid;
3822 * parent page become empty: free the page
3824 if (index == XTENTRYSTART) {
3825 if (log) { /* COMMIT_PWMAP */
3826 /* txCommit() with tlckFREE:
3827 * free child extents covered by parent;
3828 * invalidate parent if COMMIT_PWMAP;
3830 tlck = txLock(tid, ip, mp, tlckXTREE);
3831 xtlck = (struct xtlock *) & tlck->lock;
3832 xtlck->hwm.offset =
3833 le16_to_cpu(p->header.nextindex) - 1;
3834 tlck->type = tlckXTREE | tlckFREE;
3835 } else { /* COMMIT_WMAP */
3837 /* free child extents covered by parent */
3838 xadlock.xdlist = &p->xad[XTENTRYSTART];
3839 xadlock.count =
3840 le16_to_cpu(p->header.nextindex) -
3841 XTENTRYSTART;
3842 txFreeMap(ip, (struct maplock *) & xadlock, NULL,
3843 COMMIT_WMAP);
3845 BT_MARK_DIRTY(mp, ip);
3847 if (p->header.flag & BT_ROOT) {
3848 p->header.flag &= ~BT_INTERNAL;
3849 p->header.flag |= BT_LEAF;
3850 p->header.nextindex = cpu_to_le16(XTENTRYSTART);
3851 if (le16_to_cpu(p->header.maxentry) == XTROOTMAXSLOT) {
3853 * Shrink root down to allow inline
3854 * EA (otherwise fsck complains)
3856 p->header.maxentry =
3857 cpu_to_le16(XTROOTINITSLOT);
3858 JFS_IP(ip)->mode2 |= INLINEEA;
3861 XT_PUTPAGE(mp); /* debug */
3862 goto out;
3863 } else {
3864 if (log) { /* COMMIT_PWMAP */
3865 /* page will be invalidated at tx completion
3867 XT_PUTPAGE(mp);
3868 } else { /* COMMIT_WMAP */
3870 if (mp->lid)
3871 lid_to_tlock(mp->lid)->flag |=
3872 tlckFREELOCK;
3874 /* invalidate parent page */
3875 discard_metapage(mp);
3878 /* parent has become empty and freed:
3879 * go back up to its parent page
3881 /* freed = 1; */
3882 goto getParent;
3886 * parent page still has entries for front region;
3888 else {
3889 /* try truncate region covered by preceding entry
3890 * (process backward)
3892 index--;
3894 /* go back down to the child page corresponding
3895 * to the entry
3897 goto getChild;
3901 * internal page: go down to child page of current entry
3903 getChild:
3904 /* save current parent entry for the child page */
3905 BT_PUSH(&btstack, bn, index);
3907 /* get child page */
3908 xad = &p->xad[index];
3909 bn = addressXAD(xad);
3912 * first access of each internal entry:
3914 /* release parent page */
3915 XT_PUTPAGE(mp);
3917 /* process the child page */
3918 goto getPage;
3920 out:
3922 * update file resource stat
3924 /* set size
3926 if (S_ISDIR(ip->i_mode) && !newsize)
3927 ip->i_size = 1; /* fsck hates zero-length directories */
3928 else
3929 ip->i_size = newsize;
3931 /* update quota allocation to reflect freed blocks */
3932 DQUOT_FREE_BLOCK(ip, nfreed);
3935 * free tlock of invalidated pages
3937 if (flag == COMMIT_WMAP)
3938 txFreelock(ip);
3940 return newsize;
3945 * xtTruncate_pmap()
3947 * function:
3948 * Perform truncate to zero lenghth for deleted file, leaving the
3949 * the xtree and working map untouched. This allows the file to
3950 * be accessed via open file handles, while the delete of the file
3951 * is committed to disk.
3953 * parameter:
3954 * tid_t tid,
3955 * struct inode *ip,
3956 * s64 committed_size)
3958 * return: new committed size
3960 * note:
3962 * To avoid deadlock by holding too many transaction locks, the
3963 * truncation may be broken up into multiple transactions.
3964 * The committed_size keeps track of part of the file has been
3965 * freed from the pmaps.
3967 s64 xtTruncate_pmap(tid_t tid, struct inode *ip, s64 committed_size)
3969 s64 bn;
3970 struct btstack btstack;
3971 int cmp;
3972 int index;
3973 int locked_leaves = 0;
3974 struct metapage *mp;
3975 xtpage_t *p;
3976 struct btframe *parent;
3977 int rc;
3978 struct tblock *tblk;
3979 struct tlock *tlck = NULL;
3980 xad_t *xad;
3981 int xlen;
3982 s64 xoff;
3983 struct xtlock *xtlck = NULL;
3985 /* save object truncation type */
3986 tblk = tid_to_tblock(tid);
3987 tblk->xflag |= COMMIT_PMAP;
3989 /* clear stack */
3990 BT_CLR(&btstack);
3992 if (committed_size) {
3993 xoff = (committed_size >> JFS_SBI(ip->i_sb)->l2bsize) - 1;
3994 rc = xtSearch(ip, xoff, &cmp, &btstack, 0);
3995 if (rc)
3996 return rc;
3998 XT_GETSEARCH(ip, btstack.top, bn, mp, p, index);
4000 if (cmp != 0) {
4001 XT_PUTPAGE(mp);
4002 jfs_error(ip->i_sb,
4003 "xtTruncate_pmap: did not find extent");
4004 return -EIO;
4006 } else {
4008 * start with root
4010 * root resides in the inode
4012 bn = 0;
4015 * first access of each page:
4017 getPage:
4018 XT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
4019 if (rc)
4020 return rc;
4022 /* process entries backward from last index */
4023 index = le16_to_cpu(p->header.nextindex) - 1;
4025 if (p->header.flag & BT_INTERNAL)
4026 goto getChild;
4030 * leaf page
4033 if (++locked_leaves > MAX_TRUNCATE_LEAVES) {
4035 * We need to limit the size of the transaction
4036 * to avoid exhausting pagecache & tlocks
4038 xad = &p->xad[index];
4039 xoff = offsetXAD(xad);
4040 xlen = lengthXAD(xad);
4041 XT_PUTPAGE(mp);
4042 return (xoff + xlen) << JFS_SBI(ip->i_sb)->l2bsize;
4044 tlck = txLock(tid, ip, mp, tlckXTREE);
4045 tlck->type = tlckXTREE | tlckFREE;
4046 xtlck = (struct xtlock *) & tlck->lock;
4047 xtlck->hwm.offset = index;
4050 XT_PUTPAGE(mp);
4053 * go back up to the parent page
4055 getParent:
4056 /* pop/restore parent entry for the current child page */
4057 if ((parent = BT_POP(&btstack)) == NULL)
4058 /* current page must have been root */
4059 goto out;
4061 /* get back the parent page */
4062 bn = parent->bn;
4063 XT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
4064 if (rc)
4065 return rc;
4067 index = parent->index;
4070 * parent page become empty: free the page
4072 if (index == XTENTRYSTART) {
4073 /* txCommit() with tlckFREE:
4074 * free child extents covered by parent;
4075 * invalidate parent if COMMIT_PWMAP;
4077 tlck = txLock(tid, ip, mp, tlckXTREE);
4078 xtlck = (struct xtlock *) & tlck->lock;
4079 xtlck->hwm.offset =
4080 le16_to_cpu(p->header.nextindex) - 1;
4081 tlck->type = tlckXTREE | tlckFREE;
4083 XT_PUTPAGE(mp);
4085 if (p->header.flag & BT_ROOT) {
4087 goto out;
4088 } else {
4089 goto getParent;
4093 * parent page still has entries for front region;
4095 else
4096 index--;
4098 * internal page: go down to child page of current entry
4100 getChild:
4101 /* save current parent entry for the child page */
4102 BT_PUSH(&btstack, bn, index);
4104 /* get child page */
4105 xad = &p->xad[index];
4106 bn = addressXAD(xad);
4109 * first access of each internal entry:
4111 /* release parent page */
4112 XT_PUTPAGE(mp);
4114 /* process the child page */
4115 goto getPage;
4117 out:
4119 return 0;
4123 #ifdef _JFS_DEBUG_XTREE
4125 * xtDisplayTree()
4127 * function: traverse forward
4129 int xtDisplayTree(struct inode *ip)
4131 int rc = 0;
4132 struct metapage *mp;
4133 xtpage_t *p;
4134 s64 bn, pbn;
4135 int index, lastindex, v, h;
4136 xad_t *xad;
4137 struct btstack btstack;
4138 struct btframe *btsp;
4139 struct btframe *parent;
4141 printk("display B+-tree.\n");
4143 /* clear stack */
4144 btsp = btstack.stack;
4147 * start with root
4149 * root resides in the inode
4151 bn = 0;
4152 v = h = 0;
4155 * first access of each page:
4157 getPage:
4158 XT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
4159 if (rc)
4160 return rc;
4162 /* process entries forward from first index */
4163 index = XTENTRYSTART;
4164 lastindex = le16_to_cpu(p->header.nextindex) - 1;
4166 if (p->header.flag & BT_INTERNAL) {
4168 * first access of each internal page
4170 goto getChild;
4171 } else { /* (p->header.flag & BT_LEAF) */
4174 * first access of each leaf page
4176 printf("leaf page ");
4177 xtDisplayPage(ip, bn, p);
4179 /* unpin the leaf page */
4180 XT_PUTPAGE(mp);
4184 * go back up to the parent page
4186 getParent:
4187 /* pop/restore parent entry for the current child page */
4188 if ((parent = (btsp == btstack.stack ? NULL : --btsp)) == NULL)
4189 /* current page must have been root */
4190 return;
4193 * parent page scan completed
4195 if ((index = parent->index) == (lastindex = parent->lastindex)) {
4196 /* go back up to the parent page */
4197 goto getParent;
4201 * parent page has entries remaining
4203 /* get back the parent page */
4204 bn = parent->bn;
4205 /* v = parent->level; */
4206 XT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
4207 if (rc)
4208 return rc;
4210 /* get next parent entry */
4211 index++;
4214 * internal page: go down to child page of current entry
4216 getChild:
4217 /* push/save current parent entry for the child page */
4218 btsp->bn = pbn = bn;
4219 btsp->index = index;
4220 btsp->lastindex = lastindex;
4221 /* btsp->level = v; */
4222 /* btsp->node = h; */
4223 ++btsp;
4225 /* get child page */
4226 xad = &p->xad[index];
4227 bn = addressXAD(xad);
4230 * first access of each internal entry:
4232 /* release parent page */
4233 XT_PUTPAGE(mp);
4235 printk("traverse down 0x%lx[%d]->0x%lx\n", (ulong) pbn, index,
4236 (ulong) bn);
4237 v++;
4238 h = index;
4240 /* process the child page */
4241 goto getPage;
4246 * xtDisplayPage()
4248 * function: display page
4250 int xtDisplayPage(struct inode *ip, s64 bn, xtpage_t * p)
4252 int rc = 0;
4253 xad_t *xad;
4254 s64 xaddr, xoff;
4255 int xlen, i, j;
4257 /* display page control */
4258 printf("bn:0x%lx flag:0x%x nextindex:%d\n",
4259 (ulong) bn, p->header.flag,
4260 le16_to_cpu(p->header.nextindex));
4262 /* display entries */
4263 xad = &p->xad[XTENTRYSTART];
4264 for (i = XTENTRYSTART, j = 1; i < le16_to_cpu(p->header.nextindex);
4265 i++, xad++, j++) {
4266 xoff = offsetXAD(xad);
4267 xaddr = addressXAD(xad);
4268 xlen = lengthXAD(xad);
4269 printf("\t[%d] 0x%lx:0x%lx(0x%x)", i, (ulong) xoff,
4270 (ulong) xaddr, xlen);
4272 if (j == 4) {
4273 printf("\n");
4274 j = 0;
4278 printf("\n");
4280 #endif /* _JFS_DEBUG_XTREE */
4283 #ifdef _JFS_WIP
4285 * xtGather()
4287 * function:
4288 * traverse for allocation acquiring tlock at commit time
4289 * (vs at the time of update) logging backward top down
4291 * note:
4292 * problem - establishing that all new allocation have been
4293 * processed both for append and random write in sparse file
4294 * at the current entry at the current subtree root page
4297 int xtGather(btree_t *t)
4299 int rc = 0;
4300 xtpage_t *p;
4301 u64 bn;
4302 int index;
4303 btentry_t *e;
4304 struct btstack btstack;
4305 struct btsf *parent;
4307 /* clear stack */
4308 BT_CLR(&btstack);
4311 * start with root
4313 * root resides in the inode
4315 bn = 0;
4316 XT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
4317 if (rc)
4318 return rc;
4320 /* new root is NOT pointed by a new entry
4321 if (p->header.flag & NEW)
4322 allocate new page lock;
4323 write a NEWPAGE log;
4326 dopage:
4328 * first access of each page:
4330 /* process entries backward from last index */
4331 index = le16_to_cpu(p->header.nextindex) - 1;
4333 if (p->header.flag & BT_LEAF) {
4335 * first access of each leaf page
4337 /* process leaf page entries backward */
4338 for (; index >= XTENTRYSTART; index--) {
4339 e = &p->xad[index];
4341 * if newpage, log NEWPAGE.
4343 if (e->flag & XAD_NEW) {
4344 nfound =+ entry->length;
4345 update current page lock for the entry;
4346 newpage(entry);
4348 * if moved, log move.
4350 } else if (e->flag & XAD_MOVED) {
4351 reset flag;
4352 update current page lock for the entry;
4357 /* unpin the leaf page */
4358 XT_PUTPAGE(mp);
4361 * go back up to the parent page
4363 getParent:
4364 /* restore parent entry for the current child page */
4365 if ((parent = BT_POP(&btstack)) == NULL)
4366 /* current page must have been root */
4367 return 0;
4369 if ((index = parent->index) == XTENTRYSTART) {
4371 * parent page scan completed
4373 /* go back up to the parent page */
4374 goto getParent;
4375 } else {
4377 * parent page has entries remaining
4379 /* get back the parent page */
4380 bn = parent->bn;
4381 XT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
4382 if (rc)
4383 return -EIO;
4385 /* first subroot page which
4386 * covers all new allocated blocks
4387 * itself not new/modified.
4388 * (if modified from split of descendent,
4389 * go down path of split page)
4391 if (nfound == nnew &&
4392 !(p->header.flag & (NEW | MOD)))
4393 exit scan;
4396 /* process parent page entries backward */
4397 index--;
4399 } else {
4401 * first access of each internal page
4406 * internal page: go down to child page of current entry
4409 /* save current parent entry for the child page */
4410 BT_PUSH(&btstack, bn, index);
4412 /* get current entry for the child page */
4413 e = &p->xad[index];
4416 * first access of each internal entry:
4419 * if new entry, log btree_tnewentry.
4421 if (e->flag & XAD_NEW)
4422 update parent page lock for the entry;
4425 /* release parent page */
4426 XT_PUTPAGE(mp);
4428 /* get child page */
4429 bn = e->bn;
4430 XT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
4431 if (rc)
4432 return rc;
4435 * first access of each non-root page:
4438 * if new, log btree_newpage.
4440 if (p->header.flag & NEW)
4441 allocate new page lock;
4442 write a NEWPAGE log (next, prev);
4445 /* process the child page */
4446 goto dopage;
4448 out:
4449 return 0;
4451 #endif /* _JFS_WIP */
4454 #ifdef CONFIG_JFS_STATISTICS
4455 int jfs_xtstat_read(char *buffer, char **start, off_t offset, int length,
4456 int *eof, void *data)
4458 int len = 0;
4459 off_t begin;
4461 len += sprintf(buffer,
4462 "JFS Xtree statistics\n"
4463 "====================\n"
4464 "searches = %d\n"
4465 "fast searches = %d\n"
4466 "splits = %d\n",
4467 xtStat.search,
4468 xtStat.fastSearch,
4469 xtStat.split);
4471 begin = offset;
4472 *start = buffer + begin;
4473 len -= begin;
4475 if (len > length)
4476 len = length;
4477 else
4478 *eof = 1;
4480 if (len < 0)
4481 len = 0;
4483 return len;
4485 #endif