[NETFILTER]: nf_conntrack: add helper function for expectation initialization
[hh.org.git] / fs / jfs / jfs_xtree.c
blobe98eb03e53105083b3fb33b0c35b11a3c5b3f8f3
1 /*
2 * Copyright (C) International Business Machines Corp., 2000-2005
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
12 * the GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 * jfs_xtree.c: extent allocation descriptor B+-tree manager
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, s64 xoff, s64 *next, int *cmpp,
115 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 */
139 * xtLookup()
141 * function: map a single page into a physical extent;
143 int xtLookup(struct inode *ip, s64 lstart,
144 s64 llen, int *pflag, s64 * paddr, s32 * plen, int no_check)
146 int rc = 0;
147 struct btstack btstack;
148 int cmp;
149 s64 bn;
150 struct metapage *mp;
151 xtpage_t *p;
152 int index;
153 xad_t *xad;
154 s64 next, size, xoff, xend;
155 int xlen;
156 s64 xaddr;
158 *paddr = 0;
159 *plen = llen;
161 if (!no_check) {
162 /* is lookup offset beyond eof ? */
163 size = ((u64) ip->i_size + (JFS_SBI(ip->i_sb)->bsize - 1)) >>
164 JFS_SBI(ip->i_sb)->l2bsize;
165 if (lstart >= size) {
166 jfs_err("xtLookup: lstart (0x%lx) >= size (0x%lx)",
167 (ulong) lstart, (ulong) size);
168 return 0;
173 * search for the xad entry covering the logical extent
175 //search:
176 if ((rc = xtSearch(ip, lstart, &next, &cmp, &btstack, 0))) {
177 jfs_err("xtLookup: xtSearch returned %d", rc);
178 return rc;
182 * compute the physical extent covering logical extent
184 * N.B. search may have failed (e.g., hole in sparse file),
185 * and returned the index of the next entry.
187 /* retrieve search result */
188 XT_GETSEARCH(ip, btstack.top, bn, mp, p, index);
190 /* is xad found covering start of logical extent ?
191 * lstart is a page start address,
192 * i.e., lstart cannot start in a hole;
194 if (cmp) {
195 if (next)
196 *plen = min(next - lstart, llen);
197 goto out;
201 * lxd covered by xad
203 xad = &p->xad[index];
204 xoff = offsetXAD(xad);
205 xlen = lengthXAD(xad);
206 xend = xoff + xlen;
207 xaddr = addressXAD(xad);
209 /* initialize new pxd */
210 *pflag = xad->flag;
211 *paddr = xaddr + (lstart - xoff);
212 /* a page must be fully covered by an xad */
213 *plen = min(xend - lstart, llen);
215 out:
216 XT_PUTPAGE(mp);
218 return rc;
223 * xtLookupList()
225 * function: map a single logical extent into a list of physical extent;
227 * parameter:
228 * struct inode *ip,
229 * struct lxdlist *lxdlist, lxd list (in)
230 * struct xadlist *xadlist, xad list (in/out)
231 * int flag)
233 * coverage of lxd by xad under assumption of
234 * . lxd's are ordered and disjoint.
235 * . xad's are ordered and disjoint.
237 * return:
238 * 0: success
240 * note: a page being written (even a single byte) is backed fully,
241 * except the last page which is only backed with blocks
242 * required to cover the last byte;
243 * the extent backing a page is fully contained within an xad;
245 int xtLookupList(struct inode *ip, struct lxdlist * lxdlist,
246 struct xadlist * xadlist, int flag)
248 int rc = 0;
249 struct btstack btstack;
250 int cmp;
251 s64 bn;
252 struct metapage *mp;
253 xtpage_t *p;
254 int index;
255 lxd_t *lxd;
256 xad_t *xad, *pxd;
257 s64 size, lstart, lend, xstart, xend, pstart;
258 s64 llen, xlen, plen;
259 s64 xaddr, paddr;
260 int nlxd, npxd, maxnpxd;
262 npxd = xadlist->nxad = 0;
263 maxnpxd = xadlist->maxnxad;
264 pxd = xadlist->xad;
266 nlxd = lxdlist->nlxd;
267 lxd = lxdlist->lxd;
269 lstart = offsetLXD(lxd);
270 llen = lengthLXD(lxd);
271 lend = lstart + llen;
273 size = (ip->i_size + (JFS_SBI(ip->i_sb)->bsize - 1)) >>
274 JFS_SBI(ip->i_sb)->l2bsize;
277 * search for the xad entry covering the logical extent
279 search:
280 if (lstart >= size)
281 return 0;
283 if ((rc = xtSearch(ip, lstart, NULL, &cmp, &btstack, 0)))
284 return rc;
287 * compute the physical extent covering logical extent
289 * N.B. search may have failed (e.g., hole in sparse file),
290 * and returned the index of the next entry.
292 //map:
293 /* retrieve search result */
294 XT_GETSEARCH(ip, btstack.top, bn, mp, p, index);
296 /* is xad on the next sibling page ? */
297 if (index == le16_to_cpu(p->header.nextindex)) {
298 if (p->header.flag & BT_ROOT)
299 goto mapend;
301 if ((bn = le64_to_cpu(p->header.next)) == 0)
302 goto mapend;
304 XT_PUTPAGE(mp);
306 /* get next sibling page */
307 XT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
308 if (rc)
309 return rc;
311 index = XTENTRYSTART;
314 xad = &p->xad[index];
317 * is lxd covered by xad ?
319 compare:
320 xstart = offsetXAD(xad);
321 xlen = lengthXAD(xad);
322 xend = xstart + xlen;
323 xaddr = addressXAD(xad);
325 compare1:
326 if (xstart < lstart)
327 goto compare2;
329 /* (lstart <= xstart) */
331 /* lxd is NOT covered by xad */
332 if (lend <= xstart) {
334 * get next lxd
336 if (--nlxd == 0)
337 goto mapend;
338 lxd++;
340 lstart = offsetLXD(lxd);
341 llen = lengthLXD(lxd);
342 lend = lstart + llen;
343 if (lstart >= size)
344 goto mapend;
346 /* compare with the current xad */
347 goto compare1;
349 /* lxd is covered by xad */
350 else { /* (xstart < lend) */
352 /* initialize new pxd */
353 pstart = xstart;
354 plen = min(lend - xstart, xlen);
355 paddr = xaddr;
357 goto cover;
360 /* (xstart < lstart) */
361 compare2:
362 /* lxd is covered by xad */
363 if (lstart < xend) {
364 /* initialize new pxd */
365 pstart = lstart;
366 plen = min(xend - lstart, llen);
367 paddr = xaddr + (lstart - xstart);
369 goto cover;
371 /* lxd is NOT covered by xad */
372 else { /* (xend <= lstart) */
375 * get next xad
377 * linear search next xad covering lxd on
378 * the current xad page, and then tree search
380 if (index == le16_to_cpu(p->header.nextindex) - 1) {
381 if (p->header.flag & BT_ROOT)
382 goto mapend;
384 XT_PUTPAGE(mp);
385 goto search;
386 } else {
387 index++;
388 xad++;
390 /* compare with new xad */
391 goto compare;
396 * lxd is covered by xad and a new pxd has been initialized
397 * (lstart <= xstart < lend) or (xstart < lstart < xend)
399 cover:
400 /* finalize pxd corresponding to current xad */
401 XT_PUTENTRY(pxd, xad->flag, pstart, plen, paddr);
403 if (++npxd >= maxnpxd)
404 goto mapend;
405 pxd++;
408 * lxd is fully covered by xad
410 if (lend <= xend) {
412 * get next lxd
414 if (--nlxd == 0)
415 goto mapend;
416 lxd++;
418 lstart = offsetLXD(lxd);
419 llen = lengthLXD(lxd);
420 lend = lstart + llen;
421 if (lstart >= size)
422 goto mapend;
425 * test for old xad covering new lxd
426 * (old xstart < new lstart)
428 goto compare2;
431 * lxd is partially covered by xad
433 else { /* (xend < lend) */
436 * get next xad
438 * linear search next xad covering lxd on
439 * the current xad page, and then next xad page search
441 if (index == le16_to_cpu(p->header.nextindex) - 1) {
442 if (p->header.flag & BT_ROOT)
443 goto mapend;
445 if ((bn = le64_to_cpu(p->header.next)) == 0)
446 goto mapend;
448 XT_PUTPAGE(mp);
450 /* get next sibling page */
451 XT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
452 if (rc)
453 return rc;
455 index = XTENTRYSTART;
456 xad = &p->xad[index];
457 } else {
458 index++;
459 xad++;
463 * test for new xad covering old lxd
464 * (old lstart < new xstart)
466 goto compare;
469 mapend:
470 xadlist->nxad = npxd;
472 //out:
473 XT_PUTPAGE(mp);
475 return rc;
480 * xtSearch()
482 * function: search for the xad entry covering specified offset.
484 * parameters:
485 * ip - file object;
486 * xoff - extent offset;
487 * nextp - address of next extent (if any) for search miss
488 * cmpp - comparison result:
489 * btstack - traverse stack;
490 * flag - search process flag (XT_INSERT);
492 * returns:
493 * btstack contains (bn, index) of search path traversed to the entry.
494 * *cmpp is set to result of comparison with the entry returned.
495 * the page containing the entry is pinned at exit.
497 static int xtSearch(struct inode *ip, s64 xoff, s64 *nextp,
498 int *cmpp, struct btstack * btstack, int flag)
500 struct jfs_inode_info *jfs_ip = JFS_IP(ip);
501 int rc = 0;
502 int cmp = 1; /* init for empty page */
503 s64 bn; /* block number */
504 struct metapage *mp; /* page buffer */
505 xtpage_t *p; /* page */
506 xad_t *xad;
507 int base, index, lim, btindex;
508 struct btframe *btsp;
509 int nsplit = 0; /* number of pages to split */
510 s64 t64;
511 s64 next = 0;
513 INCREMENT(xtStat.search);
515 BT_CLR(btstack);
517 btstack->nsplit = 0;
520 * search down tree from root:
522 * between two consecutive entries of <Ki, Pi> and <Kj, Pj> of
523 * internal page, child page Pi contains entry with k, Ki <= K < Kj.
525 * if entry with search key K is not found
526 * internal page search find the entry with largest key Ki
527 * less than K which point to the child page to search;
528 * leaf page search find the entry with smallest key Kj
529 * greater than K so that the returned index is the position of
530 * the entry to be shifted right for insertion of new entry.
531 * for empty tree, search key is greater than any key of the tree.
533 * by convention, root bn = 0.
535 for (bn = 0;;) {
536 /* get/pin the page to search */
537 XT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
538 if (rc)
539 return rc;
541 /* try sequential access heuristics with the previous
542 * access entry in target leaf page:
543 * once search narrowed down into the target leaf,
544 * key must either match an entry in the leaf or
545 * key entry does not exist in the tree;
547 //fastSearch:
548 if ((jfs_ip->btorder & BT_SEQUENTIAL) &&
549 (p->header.flag & BT_LEAF) &&
550 (index = jfs_ip->btindex) <
551 le16_to_cpu(p->header.nextindex)) {
552 xad = &p->xad[index];
553 t64 = offsetXAD(xad);
554 if (xoff < t64 + lengthXAD(xad)) {
555 if (xoff >= t64) {
556 *cmpp = 0;
557 goto out;
560 /* stop sequential access heuristics */
561 goto binarySearch;
562 } else { /* (t64 + lengthXAD(xad)) <= xoff */
564 /* try next sequential entry */
565 index++;
566 if (index <
567 le16_to_cpu(p->header.nextindex)) {
568 xad++;
569 t64 = offsetXAD(xad);
570 if (xoff < t64 + lengthXAD(xad)) {
571 if (xoff >= t64) {
572 *cmpp = 0;
573 goto out;
576 /* miss: key falls between
577 * previous and this entry
579 *cmpp = 1;
580 next = t64;
581 goto out;
584 /* (xoff >= t64 + lengthXAD(xad));
585 * matching entry may be further out:
586 * stop heuristic search
588 /* stop sequential access heuristics */
589 goto binarySearch;
592 /* (index == p->header.nextindex);
593 * miss: key entry does not exist in
594 * the target leaf/tree
596 *cmpp = 1;
597 goto out;
601 * if hit, return index of the entry found, and
602 * if miss, where new entry with search key is
603 * to be inserted;
605 out:
606 /* compute number of pages to split */
607 if (flag & XT_INSERT) {
608 if (p->header.nextindex == /* little-endian */
609 p->header.maxentry)
610 nsplit++;
611 else
612 nsplit = 0;
613 btstack->nsplit = nsplit;
616 /* save search result */
617 btsp = btstack->top;
618 btsp->bn = bn;
619 btsp->index = index;
620 btsp->mp = mp;
622 /* update sequential access heuristics */
623 jfs_ip->btindex = index;
625 if (nextp)
626 *nextp = next;
628 INCREMENT(xtStat.fastSearch);
629 return 0;
632 /* well, ... full search now */
633 binarySearch:
634 lim = le16_to_cpu(p->header.nextindex) - XTENTRYSTART;
637 * binary search with search key K on the current page
639 for (base = XTENTRYSTART; lim; lim >>= 1) {
640 index = base + (lim >> 1);
642 XT_CMP(cmp, xoff, &p->xad[index], t64);
643 if (cmp == 0) {
645 * search hit
647 /* search hit - leaf page:
648 * return the entry found
650 if (p->header.flag & BT_LEAF) {
651 *cmpp = cmp;
653 /* compute number of pages to split */
654 if (flag & XT_INSERT) {
655 if (p->header.nextindex ==
656 p->header.maxentry)
657 nsplit++;
658 else
659 nsplit = 0;
660 btstack->nsplit = nsplit;
663 /* save search result */
664 btsp = btstack->top;
665 btsp->bn = bn;
666 btsp->index = index;
667 btsp->mp = mp;
669 /* init sequential access heuristics */
670 btindex = jfs_ip->btindex;
671 if (index == btindex ||
672 index == btindex + 1)
673 jfs_ip->btorder = BT_SEQUENTIAL;
674 else
675 jfs_ip->btorder = BT_RANDOM;
676 jfs_ip->btindex = index;
678 return 0;
680 /* search hit - internal page:
681 * descend/search its child page
683 if (index < le16_to_cpu(p->header.nextindex)-1)
684 next = offsetXAD(&p->xad[index + 1]);
685 goto next;
688 if (cmp > 0) {
689 base = index + 1;
690 --lim;
695 * search miss
697 * base is the smallest index with key (Kj) greater than
698 * search key (K) and may be zero or maxentry index.
700 if (base < le16_to_cpu(p->header.nextindex))
701 next = offsetXAD(&p->xad[base]);
703 * search miss - leaf page:
705 * return location of entry (base) where new entry with
706 * search key K is to be inserted.
708 if (p->header.flag & BT_LEAF) {
709 *cmpp = cmp;
711 /* compute number of pages to split */
712 if (flag & XT_INSERT) {
713 if (p->header.nextindex ==
714 p->header.maxentry)
715 nsplit++;
716 else
717 nsplit = 0;
718 btstack->nsplit = nsplit;
721 /* save search result */
722 btsp = btstack->top;
723 btsp->bn = bn;
724 btsp->index = base;
725 btsp->mp = mp;
727 /* init sequential access heuristics */
728 btindex = jfs_ip->btindex;
729 if (base == btindex || base == btindex + 1)
730 jfs_ip->btorder = BT_SEQUENTIAL;
731 else
732 jfs_ip->btorder = BT_RANDOM;
733 jfs_ip->btindex = base;
735 if (nextp)
736 *nextp = next;
738 return 0;
742 * search miss - non-leaf page:
744 * if base is non-zero, decrement base by one to get the parent
745 * entry of the child page to search.
747 index = base ? base - 1 : base;
750 * go down to child page
752 next:
753 /* update number of pages to split */
754 if (p->header.nextindex == p->header.maxentry)
755 nsplit++;
756 else
757 nsplit = 0;
759 /* push (bn, index) of the parent page/entry */
760 BT_PUSH(btstack, bn, index);
762 /* get the child page block number */
763 bn = addressXAD(&p->xad[index]);
765 /* unpin the parent page */
766 XT_PUTPAGE(mp);
771 * xtInsert()
773 * function:
775 * parameter:
776 * tid - transaction id;
777 * ip - file object;
778 * xflag - extent flag (XAD_NOTRECORDED):
779 * xoff - extent offset;
780 * xlen - extent length;
781 * xaddrp - extent address pointer (in/out):
782 * if (*xaddrp)
783 * caller allocated data extent at *xaddrp;
784 * else
785 * allocate data extent and return its xaddr;
786 * flag -
788 * return:
790 int xtInsert(tid_t tid, /* transaction id */
791 struct inode *ip, int xflag, s64 xoff, s32 xlen, s64 * xaddrp,
792 int flag)
794 int rc = 0;
795 s64 xaddr, hint;
796 struct metapage *mp; /* meta-page buffer */
797 xtpage_t *p; /* base B+-tree index page */
798 s64 bn;
799 int index, nextindex;
800 struct btstack btstack; /* traverse stack */
801 struct xtsplit split; /* split information */
802 xad_t *xad;
803 int cmp;
804 s64 next;
805 struct tlock *tlck;
806 struct xtlock *xtlck;
808 jfs_info("xtInsert: nxoff:0x%lx nxlen:0x%x", (ulong) xoff, xlen);
811 * search for the entry location at which to insert:
813 * xtFastSearch() and xtSearch() both returns (leaf page
814 * pinned, index at which to insert).
815 * n.b. xtSearch() may return index of maxentry of
816 * the full page.
818 if ((rc = xtSearch(ip, xoff, &next, &cmp, &btstack, XT_INSERT)))
819 return rc;
821 /* retrieve search result */
822 XT_GETSEARCH(ip, btstack.top, bn, mp, p, index);
824 /* This test must follow XT_GETSEARCH since mp must be valid if
825 * we branch to out: */
826 if ((cmp == 0) || (next && (xlen > next - xoff))) {
827 rc = -EEXIST;
828 goto out;
832 * allocate data extent requested
834 * allocation hint: last xad
836 if ((xaddr = *xaddrp) == 0) {
837 if (index > XTENTRYSTART) {
838 xad = &p->xad[index - 1];
839 hint = addressXAD(xad) + lengthXAD(xad) - 1;
840 } else
841 hint = 0;
842 if ((rc = DQUOT_ALLOC_BLOCK(ip, xlen)))
843 goto out;
844 if ((rc = dbAlloc(ip, hint, (s64) xlen, &xaddr))) {
845 DQUOT_FREE_BLOCK(ip, xlen);
846 goto out;
851 * insert entry for new extent
853 xflag |= XAD_NEW;
856 * if the leaf page is full, split the page and
857 * propagate up the router entry for the new page from split
859 * The xtSplitUp() will insert the entry and unpin the leaf page.
861 nextindex = le16_to_cpu(p->header.nextindex);
862 if (nextindex == le16_to_cpu(p->header.maxentry)) {
863 split.mp = mp;
864 split.index = index;
865 split.flag = xflag;
866 split.off = xoff;
867 split.len = xlen;
868 split.addr = xaddr;
869 split.pxdlist = NULL;
870 if ((rc = xtSplitUp(tid, ip, &split, &btstack))) {
871 /* undo data extent allocation */
872 if (*xaddrp == 0) {
873 dbFree(ip, xaddr, (s64) xlen);
874 DQUOT_FREE_BLOCK(ip, xlen);
876 return rc;
879 *xaddrp = xaddr;
880 return 0;
884 * insert the new entry into the leaf page
887 * acquire a transaction lock on the leaf page;
889 * action: xad insertion/extension;
891 BT_MARK_DIRTY(mp, ip);
893 /* if insert into middle, shift right remaining entries. */
894 if (index < nextindex)
895 memmove(&p->xad[index + 1], &p->xad[index],
896 (nextindex - index) * sizeof(xad_t));
898 /* insert the new entry: mark the entry NEW */
899 xad = &p->xad[index];
900 XT_PUTENTRY(xad, xflag, xoff, xlen, xaddr);
902 /* advance next available entry index */
903 p->header.nextindex =
904 cpu_to_le16(le16_to_cpu(p->header.nextindex) + 1);
906 /* Don't log it if there are no links to the file */
907 if (!test_cflag(COMMIT_Nolink, ip)) {
908 tlck = txLock(tid, ip, mp, tlckXTREE | tlckGROW);
909 xtlck = (struct xtlock *) & tlck->lock;
910 xtlck->lwm.offset =
911 (xtlck->lwm.offset) ? min(index,
912 (int)xtlck->lwm.offset) : index;
913 xtlck->lwm.length =
914 le16_to_cpu(p->header.nextindex) - xtlck->lwm.offset;
917 *xaddrp = xaddr;
919 out:
920 /* unpin the leaf page */
921 XT_PUTPAGE(mp);
923 return rc;
928 * xtSplitUp()
930 * function:
931 * split full pages as propagating insertion up the tree
933 * parameter:
934 * tid - transaction id;
935 * ip - file object;
936 * split - entry parameter descriptor;
937 * btstack - traverse stack from xtSearch()
939 * return:
941 static int
942 xtSplitUp(tid_t tid,
943 struct inode *ip, struct xtsplit * split, struct btstack * btstack)
945 int rc = 0;
946 struct metapage *smp;
947 xtpage_t *sp; /* split page */
948 struct metapage *rmp;
949 s64 rbn; /* new right page block number */
950 struct metapage *rcmp;
951 xtpage_t *rcp; /* right child page */
952 s64 rcbn; /* right child page block number */
953 int skip; /* index of entry of insertion */
954 int nextindex; /* next available entry index of p */
955 struct btframe *parent; /* parent page entry on traverse stack */
956 xad_t *xad;
957 s64 xaddr;
958 int xlen;
959 int nsplit; /* number of pages split */
960 struct pxdlist pxdlist;
961 pxd_t *pxd;
962 struct tlock *tlck;
963 struct xtlock *xtlck;
965 smp = split->mp;
966 sp = XT_PAGE(ip, smp);
968 /* is inode xtree root extension/inline EA area free ? */
969 if ((sp->header.flag & BT_ROOT) && (!S_ISDIR(ip->i_mode)) &&
970 (le16_to_cpu(sp->header.maxentry) < XTROOTMAXSLOT) &&
971 (JFS_IP(ip)->mode2 & INLINEEA)) {
972 sp->header.maxentry = cpu_to_le16(XTROOTMAXSLOT);
973 JFS_IP(ip)->mode2 &= ~INLINEEA;
975 BT_MARK_DIRTY(smp, ip);
977 * acquire a transaction lock on the leaf page;
979 * action: xad insertion/extension;
982 /* if insert into middle, shift right remaining entries. */
983 skip = split->index;
984 nextindex = le16_to_cpu(sp->header.nextindex);
985 if (skip < nextindex)
986 memmove(&sp->xad[skip + 1], &sp->xad[skip],
987 (nextindex - skip) * sizeof(xad_t));
989 /* insert the new entry: mark the entry NEW */
990 xad = &sp->xad[skip];
991 XT_PUTENTRY(xad, split->flag, split->off, split->len,
992 split->addr);
994 /* advance next available entry index */
995 sp->header.nextindex =
996 cpu_to_le16(le16_to_cpu(sp->header.nextindex) + 1);
998 /* Don't log it if there are no links to the file */
999 if (!test_cflag(COMMIT_Nolink, ip)) {
1000 tlck = txLock(tid, ip, smp, tlckXTREE | tlckGROW);
1001 xtlck = (struct xtlock *) & tlck->lock;
1002 xtlck->lwm.offset = (xtlck->lwm.offset) ?
1003 min(skip, (int)xtlck->lwm.offset) : skip;
1004 xtlck->lwm.length =
1005 le16_to_cpu(sp->header.nextindex) -
1006 xtlck->lwm.offset;
1009 return 0;
1013 * allocate new index blocks to cover index page split(s)
1015 * allocation hint: ?
1017 if (split->pxdlist == NULL) {
1018 nsplit = btstack->nsplit;
1019 split->pxdlist = &pxdlist;
1020 pxdlist.maxnpxd = pxdlist.npxd = 0;
1021 pxd = &pxdlist.pxd[0];
1022 xlen = JFS_SBI(ip->i_sb)->nbperpage;
1023 for (; nsplit > 0; nsplit--, pxd++) {
1024 if ((rc = dbAlloc(ip, (s64) 0, (s64) xlen, &xaddr))
1025 == 0) {
1026 PXDaddress(pxd, xaddr);
1027 PXDlength(pxd, xlen);
1029 pxdlist.maxnpxd++;
1031 continue;
1034 /* undo allocation */
1036 XT_PUTPAGE(smp);
1037 return rc;
1042 * Split leaf page <sp> into <sp> and a new right page <rp>.
1044 * The split routines insert the new entry into the leaf page,
1045 * and acquire txLock as appropriate.
1046 * return <rp> pinned and its block number <rpbn>.
1048 rc = (sp->header.flag & BT_ROOT) ?
1049 xtSplitRoot(tid, ip, split, &rmp) :
1050 xtSplitPage(tid, ip, split, &rmp, &rbn);
1052 XT_PUTPAGE(smp);
1054 if (rc)
1055 return -EIO;
1057 * propagate up the router entry for the leaf page just split
1059 * insert a router entry for the new page into the parent page,
1060 * propagate the insert/split up the tree by walking back the stack
1061 * of (bn of parent page, index of child page entry in parent page)
1062 * that were traversed during the search for the page that split.
1064 * the propagation of insert/split up the tree stops if the root
1065 * splits or the page inserted into doesn't have to split to hold
1066 * the new entry.
1068 * the parent entry for the split page remains the same, and
1069 * a new entry is inserted at its right with the first key and
1070 * block number of the new right page.
1072 * There are a maximum of 3 pages pinned at any time:
1073 * right child, left parent and right parent (when the parent splits)
1074 * to keep the child page pinned while working on the parent.
1075 * make sure that all pins are released at exit.
1077 while ((parent = BT_POP(btstack)) != NULL) {
1078 /* parent page specified by stack frame <parent> */
1080 /* keep current child pages <rcp> pinned */
1081 rcmp = rmp;
1082 rcbn = rbn;
1083 rcp = XT_PAGE(ip, rcmp);
1086 * insert router entry in parent for new right child page <rp>
1088 /* get/pin the parent page <sp> */
1089 XT_GETPAGE(ip, parent->bn, smp, PSIZE, sp, rc);
1090 if (rc) {
1091 XT_PUTPAGE(rcmp);
1092 return rc;
1096 * The new key entry goes ONE AFTER the index of parent entry,
1097 * because the split was to the right.
1099 skip = parent->index + 1;
1102 * split or shift right remaining entries of the parent page
1104 nextindex = le16_to_cpu(sp->header.nextindex);
1106 * parent page is full - split the parent page
1108 if (nextindex == le16_to_cpu(sp->header.maxentry)) {
1109 /* init for parent page split */
1110 split->mp = smp;
1111 split->index = skip; /* index at insert */
1112 split->flag = XAD_NEW;
1113 split->off = offsetXAD(&rcp->xad[XTENTRYSTART]);
1114 split->len = JFS_SBI(ip->i_sb)->nbperpage;
1115 split->addr = rcbn;
1117 /* unpin previous right child page */
1118 XT_PUTPAGE(rcmp);
1120 /* The split routines insert the new entry,
1121 * and acquire txLock as appropriate.
1122 * return <rp> pinned and its block number <rpbn>.
1124 rc = (sp->header.flag & BT_ROOT) ?
1125 xtSplitRoot(tid, ip, split, &rmp) :
1126 xtSplitPage(tid, ip, split, &rmp, &rbn);
1127 if (rc) {
1128 XT_PUTPAGE(smp);
1129 return rc;
1132 XT_PUTPAGE(smp);
1133 /* keep new child page <rp> pinned */
1136 * parent page is not full - insert in parent page
1138 else {
1140 * insert router entry in parent for the right child
1141 * page from the first entry of the right child page:
1144 * acquire a transaction lock on the parent page;
1146 * action: router xad insertion;
1148 BT_MARK_DIRTY(smp, ip);
1151 * if insert into middle, shift right remaining entries
1153 if (skip < nextindex)
1154 memmove(&sp->xad[skip + 1], &sp->xad[skip],
1155 (nextindex -
1156 skip) << L2XTSLOTSIZE);
1158 /* insert the router entry */
1159 xad = &sp->xad[skip];
1160 XT_PUTENTRY(xad, XAD_NEW,
1161 offsetXAD(&rcp->xad[XTENTRYSTART]),
1162 JFS_SBI(ip->i_sb)->nbperpage, rcbn);
1164 /* advance next available entry index. */
1165 sp->header.nextindex =
1166 cpu_to_le16(le16_to_cpu(sp->header.nextindex) +
1169 /* Don't log it if there are no links to the file */
1170 if (!test_cflag(COMMIT_Nolink, ip)) {
1171 tlck = txLock(tid, ip, smp,
1172 tlckXTREE | tlckGROW);
1173 xtlck = (struct xtlock *) & tlck->lock;
1174 xtlck->lwm.offset = (xtlck->lwm.offset) ?
1175 min(skip, (int)xtlck->lwm.offset) : skip;
1176 xtlck->lwm.length =
1177 le16_to_cpu(sp->header.nextindex) -
1178 xtlck->lwm.offset;
1181 /* unpin parent page */
1182 XT_PUTPAGE(smp);
1184 /* exit propagate up */
1185 break;
1189 /* unpin current right page */
1190 XT_PUTPAGE(rmp);
1192 return 0;
1197 * xtSplitPage()
1199 * function:
1200 * split a full non-root page into
1201 * original/split/left page and new right page
1202 * i.e., the original/split page remains as left page.
1204 * parameter:
1205 * int tid,
1206 * struct inode *ip,
1207 * struct xtsplit *split,
1208 * struct metapage **rmpp,
1209 * u64 *rbnp,
1211 * return:
1212 * Pointer to page in which to insert or NULL on error.
1214 static int
1215 xtSplitPage(tid_t tid, struct inode *ip,
1216 struct xtsplit * split, struct metapage ** rmpp, s64 * rbnp)
1218 int rc = 0;
1219 struct metapage *smp;
1220 xtpage_t *sp;
1221 struct metapage *rmp;
1222 xtpage_t *rp; /* new right page allocated */
1223 s64 rbn; /* new right page block number */
1224 struct metapage *mp;
1225 xtpage_t *p;
1226 s64 nextbn;
1227 int skip, maxentry, middle, righthalf, n;
1228 xad_t *xad;
1229 struct pxdlist *pxdlist;
1230 pxd_t *pxd;
1231 struct tlock *tlck;
1232 struct xtlock *sxtlck = NULL, *rxtlck = NULL;
1233 int quota_allocation = 0;
1235 smp = split->mp;
1236 sp = XT_PAGE(ip, smp);
1238 INCREMENT(xtStat.split);
1240 pxdlist = split->pxdlist;
1241 pxd = &pxdlist->pxd[pxdlist->npxd];
1242 pxdlist->npxd++;
1243 rbn = addressPXD(pxd);
1245 /* Allocate blocks to quota. */
1246 if (DQUOT_ALLOC_BLOCK(ip, lengthPXD(pxd))) {
1247 rc = -EDQUOT;
1248 goto clean_up;
1251 quota_allocation += lengthPXD(pxd);
1254 * allocate the new right page for the split
1256 rmp = get_metapage(ip, rbn, PSIZE, 1);
1257 if (rmp == NULL) {
1258 rc = -EIO;
1259 goto clean_up;
1262 jfs_info("xtSplitPage: ip:0x%p smp:0x%p rmp:0x%p", ip, smp, rmp);
1264 BT_MARK_DIRTY(rmp, ip);
1266 * action: new page;
1269 rp = (xtpage_t *) rmp->data;
1270 rp->header.self = *pxd;
1271 rp->header.flag = sp->header.flag & BT_TYPE;
1272 rp->header.maxentry = sp->header.maxentry; /* little-endian */
1273 rp->header.nextindex = cpu_to_le16(XTENTRYSTART);
1275 BT_MARK_DIRTY(smp, ip);
1276 /* Don't log it if there are no links to the file */
1277 if (!test_cflag(COMMIT_Nolink, ip)) {
1279 * acquire a transaction lock on the new right page;
1281 tlck = txLock(tid, ip, rmp, tlckXTREE | tlckNEW);
1282 rxtlck = (struct xtlock *) & tlck->lock;
1283 rxtlck->lwm.offset = XTENTRYSTART;
1285 * acquire a transaction lock on the split page
1287 tlck = txLock(tid, ip, smp, tlckXTREE | tlckGROW);
1288 sxtlck = (struct xtlock *) & tlck->lock;
1292 * initialize/update sibling pointers of <sp> and <rp>
1294 nextbn = le64_to_cpu(sp->header.next);
1295 rp->header.next = cpu_to_le64(nextbn);
1296 rp->header.prev = cpu_to_le64(addressPXD(&sp->header.self));
1297 sp->header.next = cpu_to_le64(rbn);
1299 skip = split->index;
1302 * sequential append at tail (after last entry of last page)
1304 * if splitting the last page on a level because of appending
1305 * a entry to it (skip is maxentry), it's likely that the access is
1306 * sequential. adding an empty page on the side of the level is less
1307 * work and can push the fill factor much higher than normal.
1308 * if we're wrong it's no big deal - we will do the split the right
1309 * way next time.
1310 * (it may look like it's equally easy to do a similar hack for
1311 * reverse sorted data, that is, split the tree left, but it's not.
1312 * Be my guest.)
1314 if (nextbn == 0 && skip == le16_to_cpu(sp->header.maxentry)) {
1316 * acquire a transaction lock on the new/right page;
1318 * action: xad insertion;
1320 /* insert entry at the first entry of the new right page */
1321 xad = &rp->xad[XTENTRYSTART];
1322 XT_PUTENTRY(xad, split->flag, split->off, split->len,
1323 split->addr);
1325 rp->header.nextindex = cpu_to_le16(XTENTRYSTART + 1);
1327 if (!test_cflag(COMMIT_Nolink, ip)) {
1328 /* rxtlck->lwm.offset = XTENTRYSTART; */
1329 rxtlck->lwm.length = 1;
1332 *rmpp = rmp;
1333 *rbnp = rbn;
1335 jfs_info("xtSplitPage: sp:0x%p rp:0x%p", sp, rp);
1336 return 0;
1340 * non-sequential insert (at possibly middle page)
1344 * update previous pointer of old next/right page of <sp>
1346 if (nextbn != 0) {
1347 XT_GETPAGE(ip, nextbn, mp, PSIZE, p, rc);
1348 if (rc) {
1349 XT_PUTPAGE(rmp);
1350 goto clean_up;
1353 BT_MARK_DIRTY(mp, ip);
1355 * acquire a transaction lock on the next page;
1357 * action:sibling pointer update;
1359 if (!test_cflag(COMMIT_Nolink, ip))
1360 tlck = txLock(tid, ip, mp, tlckXTREE | tlckRELINK);
1362 p->header.prev = cpu_to_le64(rbn);
1364 /* sibling page may have been updated previously, or
1365 * it may be updated later;
1368 XT_PUTPAGE(mp);
1372 * split the data between the split and new/right pages
1374 maxentry = le16_to_cpu(sp->header.maxentry);
1375 middle = maxentry >> 1;
1376 righthalf = maxentry - middle;
1379 * skip index in old split/left page - insert into left page:
1381 if (skip <= middle) {
1382 /* move right half of split page to the new right page */
1383 memmove(&rp->xad[XTENTRYSTART], &sp->xad[middle],
1384 righthalf << L2XTSLOTSIZE);
1386 /* shift right tail of left half to make room for new entry */
1387 if (skip < middle)
1388 memmove(&sp->xad[skip + 1], &sp->xad[skip],
1389 (middle - skip) << L2XTSLOTSIZE);
1391 /* insert new entry */
1392 xad = &sp->xad[skip];
1393 XT_PUTENTRY(xad, split->flag, split->off, split->len,
1394 split->addr);
1396 /* update page header */
1397 sp->header.nextindex = cpu_to_le16(middle + 1);
1398 if (!test_cflag(COMMIT_Nolink, ip)) {
1399 sxtlck->lwm.offset = (sxtlck->lwm.offset) ?
1400 min(skip, (int)sxtlck->lwm.offset) : skip;
1403 rp->header.nextindex =
1404 cpu_to_le16(XTENTRYSTART + righthalf);
1407 * skip index in new right page - insert into right page:
1409 else {
1410 /* move left head of right half to right page */
1411 n = skip - middle;
1412 memmove(&rp->xad[XTENTRYSTART], &sp->xad[middle],
1413 n << L2XTSLOTSIZE);
1415 /* insert new entry */
1416 n += XTENTRYSTART;
1417 xad = &rp->xad[n];
1418 XT_PUTENTRY(xad, split->flag, split->off, split->len,
1419 split->addr);
1421 /* move right tail of right half to right page */
1422 if (skip < maxentry)
1423 memmove(&rp->xad[n + 1], &sp->xad[skip],
1424 (maxentry - skip) << L2XTSLOTSIZE);
1426 /* update page header */
1427 sp->header.nextindex = cpu_to_le16(middle);
1428 if (!test_cflag(COMMIT_Nolink, ip)) {
1429 sxtlck->lwm.offset = (sxtlck->lwm.offset) ?
1430 min(middle, (int)sxtlck->lwm.offset) : middle;
1433 rp->header.nextindex = cpu_to_le16(XTENTRYSTART +
1434 righthalf + 1);
1437 if (!test_cflag(COMMIT_Nolink, ip)) {
1438 sxtlck->lwm.length = le16_to_cpu(sp->header.nextindex) -
1439 sxtlck->lwm.offset;
1441 /* rxtlck->lwm.offset = XTENTRYSTART; */
1442 rxtlck->lwm.length = le16_to_cpu(rp->header.nextindex) -
1443 XTENTRYSTART;
1446 *rmpp = rmp;
1447 *rbnp = rbn;
1449 jfs_info("xtSplitPage: sp:0x%p rp:0x%p", sp, rp);
1450 return rc;
1452 clean_up:
1454 /* Rollback quota allocation. */
1455 if (quota_allocation)
1456 DQUOT_FREE_BLOCK(ip, quota_allocation);
1458 return (rc);
1463 * xtSplitRoot()
1465 * function:
1466 * split the full root page into
1467 * original/root/split page and new right page
1468 * i.e., root remains fixed in tree anchor (inode) and
1469 * the root is copied to a single new right child page
1470 * since root page << non-root page, and
1471 * the split root page contains a single entry for the
1472 * new right child page.
1474 * parameter:
1475 * int tid,
1476 * struct inode *ip,
1477 * struct xtsplit *split,
1478 * struct metapage **rmpp)
1480 * return:
1481 * Pointer to page in which to insert or NULL on error.
1483 static int
1484 xtSplitRoot(tid_t tid,
1485 struct inode *ip, struct xtsplit * split, struct metapage ** rmpp)
1487 xtpage_t *sp;
1488 struct metapage *rmp;
1489 xtpage_t *rp;
1490 s64 rbn;
1491 int skip, nextindex;
1492 xad_t *xad;
1493 pxd_t *pxd;
1494 struct pxdlist *pxdlist;
1495 struct tlock *tlck;
1496 struct xtlock *xtlck;
1498 sp = &JFS_IP(ip)->i_xtroot;
1500 INCREMENT(xtStat.split);
1503 * allocate a single (right) child page
1505 pxdlist = split->pxdlist;
1506 pxd = &pxdlist->pxd[pxdlist->npxd];
1507 pxdlist->npxd++;
1508 rbn = addressPXD(pxd);
1509 rmp = get_metapage(ip, rbn, PSIZE, 1);
1510 if (rmp == NULL)
1511 return -EIO;
1513 /* Allocate blocks to quota. */
1514 if (DQUOT_ALLOC_BLOCK(ip, lengthPXD(pxd))) {
1515 release_metapage(rmp);
1516 return -EDQUOT;
1519 jfs_info("xtSplitRoot: ip:0x%p rmp:0x%p", ip, rmp);
1522 * acquire a transaction lock on the new right page;
1524 * action: new page;
1526 BT_MARK_DIRTY(rmp, ip);
1528 rp = (xtpage_t *) rmp->data;
1529 rp->header.flag =
1530 (sp->header.flag & BT_LEAF) ? BT_LEAF : BT_INTERNAL;
1531 rp->header.self = *pxd;
1532 rp->header.nextindex = cpu_to_le16(XTENTRYSTART);
1533 rp->header.maxentry = cpu_to_le16(PSIZE >> L2XTSLOTSIZE);
1535 /* initialize sibling pointers */
1536 rp->header.next = 0;
1537 rp->header.prev = 0;
1540 * copy the in-line root page into new right page extent
1542 nextindex = le16_to_cpu(sp->header.maxentry);
1543 memmove(&rp->xad[XTENTRYSTART], &sp->xad[XTENTRYSTART],
1544 (nextindex - XTENTRYSTART) << L2XTSLOTSIZE);
1547 * insert the new entry into the new right/child page
1548 * (skip index in the new right page will not change)
1550 skip = split->index;
1551 /* if insert into middle, shift right remaining entries */
1552 if (skip != nextindex)
1553 memmove(&rp->xad[skip + 1], &rp->xad[skip],
1554 (nextindex - skip) * sizeof(xad_t));
1556 xad = &rp->xad[skip];
1557 XT_PUTENTRY(xad, split->flag, split->off, split->len, split->addr);
1559 /* update page header */
1560 rp->header.nextindex = cpu_to_le16(nextindex + 1);
1562 if (!test_cflag(COMMIT_Nolink, ip)) {
1563 tlck = txLock(tid, ip, rmp, tlckXTREE | tlckNEW);
1564 xtlck = (struct xtlock *) & tlck->lock;
1565 xtlck->lwm.offset = XTENTRYSTART;
1566 xtlck->lwm.length = le16_to_cpu(rp->header.nextindex) -
1567 XTENTRYSTART;
1571 * reset the root
1573 * init root with the single entry for the new right page
1574 * set the 1st entry offset to 0, which force the left-most key
1575 * at any level of the tree to be less than any search key.
1578 * acquire a transaction lock on the root page (in-memory inode);
1580 * action: root split;
1582 BT_MARK_DIRTY(split->mp, ip);
1584 xad = &sp->xad[XTENTRYSTART];
1585 XT_PUTENTRY(xad, XAD_NEW, 0, JFS_SBI(ip->i_sb)->nbperpage, rbn);
1587 /* update page header of root */
1588 sp->header.flag &= ~BT_LEAF;
1589 sp->header.flag |= BT_INTERNAL;
1591 sp->header.nextindex = cpu_to_le16(XTENTRYSTART + 1);
1593 if (!test_cflag(COMMIT_Nolink, ip)) {
1594 tlck = txLock(tid, ip, split->mp, tlckXTREE | tlckGROW);
1595 xtlck = (struct xtlock *) & tlck->lock;
1596 xtlck->lwm.offset = XTENTRYSTART;
1597 xtlck->lwm.length = 1;
1600 *rmpp = rmp;
1602 jfs_info("xtSplitRoot: sp:0x%p rp:0x%p", sp, rp);
1603 return 0;
1608 * xtExtend()
1610 * function: extend in-place;
1612 * note: existing extent may or may not have been committed.
1613 * caller is responsible for pager buffer cache update, and
1614 * working block allocation map update;
1615 * update pmap: alloc whole extended extent;
1617 int xtExtend(tid_t tid, /* transaction id */
1618 struct inode *ip, s64 xoff, /* delta extent offset */
1619 s32 xlen, /* delta extent length */
1620 int flag)
1622 int rc = 0;
1623 int cmp;
1624 struct metapage *mp; /* meta-page buffer */
1625 xtpage_t *p; /* base B+-tree index page */
1626 s64 bn;
1627 int index, nextindex, len;
1628 struct btstack btstack; /* traverse stack */
1629 struct xtsplit split; /* split information */
1630 xad_t *xad;
1631 s64 xaddr;
1632 struct tlock *tlck;
1633 struct xtlock *xtlck = NULL;
1635 jfs_info("xtExtend: nxoff:0x%lx nxlen:0x%x", (ulong) xoff, xlen);
1637 /* there must exist extent to be extended */
1638 if ((rc = xtSearch(ip, xoff - 1, NULL, &cmp, &btstack, XT_INSERT)))
1639 return rc;
1641 /* retrieve search result */
1642 XT_GETSEARCH(ip, btstack.top, bn, mp, p, index);
1644 if (cmp != 0) {
1645 XT_PUTPAGE(mp);
1646 jfs_error(ip->i_sb, "xtExtend: xtSearch did not find extent");
1647 return -EIO;
1650 /* extension must be contiguous */
1651 xad = &p->xad[index];
1652 if ((offsetXAD(xad) + lengthXAD(xad)) != xoff) {
1653 XT_PUTPAGE(mp);
1654 jfs_error(ip->i_sb, "xtExtend: extension is not contiguous");
1655 return -EIO;
1659 * acquire a transaction lock on the leaf page;
1661 * action: xad insertion/extension;
1663 BT_MARK_DIRTY(mp, ip);
1664 if (!test_cflag(COMMIT_Nolink, ip)) {
1665 tlck = txLock(tid, ip, mp, tlckXTREE | tlckGROW);
1666 xtlck = (struct xtlock *) & tlck->lock;
1669 /* extend will overflow extent ? */
1670 xlen = lengthXAD(xad) + xlen;
1671 if ((len = xlen - MAXXLEN) <= 0)
1672 goto extendOld;
1675 * extent overflow: insert entry for new extent
1677 //insertNew:
1678 xoff = offsetXAD(xad) + MAXXLEN;
1679 xaddr = addressXAD(xad) + MAXXLEN;
1680 nextindex = le16_to_cpu(p->header.nextindex);
1683 * if the leaf page is full, insert the new entry and
1684 * propagate up the router entry for the new page from split
1686 * The xtSplitUp() will insert the entry and unpin the leaf page.
1688 if (nextindex == le16_to_cpu(p->header.maxentry)) {
1689 /* xtSpliUp() unpins leaf pages */
1690 split.mp = mp;
1691 split.index = index + 1;
1692 split.flag = XAD_NEW;
1693 split.off = xoff; /* split offset */
1694 split.len = len;
1695 split.addr = xaddr;
1696 split.pxdlist = NULL;
1697 if ((rc = xtSplitUp(tid, ip, &split, &btstack)))
1698 return rc;
1700 /* get back old page */
1701 XT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
1702 if (rc)
1703 return rc;
1705 * if leaf root has been split, original root has been
1706 * copied to new child page, i.e., original entry now
1707 * resides on the new child page;
1709 if (p->header.flag & BT_INTERNAL) {
1710 ASSERT(p->header.nextindex ==
1711 cpu_to_le16(XTENTRYSTART + 1));
1712 xad = &p->xad[XTENTRYSTART];
1713 bn = addressXAD(xad);
1714 XT_PUTPAGE(mp);
1716 /* get new child page */
1717 XT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
1718 if (rc)
1719 return rc;
1721 BT_MARK_DIRTY(mp, ip);
1722 if (!test_cflag(COMMIT_Nolink, ip)) {
1723 tlck = txLock(tid, ip, mp, tlckXTREE|tlckGROW);
1724 xtlck = (struct xtlock *) & tlck->lock;
1729 * insert the new entry into the leaf page
1731 else {
1732 /* insert the new entry: mark the entry NEW */
1733 xad = &p->xad[index + 1];
1734 XT_PUTENTRY(xad, XAD_NEW, xoff, len, xaddr);
1736 /* advance next available entry index */
1737 p->header.nextindex =
1738 cpu_to_le16(le16_to_cpu(p->header.nextindex) + 1);
1741 /* get back old entry */
1742 xad = &p->xad[index];
1743 xlen = MAXXLEN;
1746 * extend old extent
1748 extendOld:
1749 XADlength(xad, xlen);
1750 if (!(xad->flag & XAD_NEW))
1751 xad->flag |= XAD_EXTENDED;
1753 if (!test_cflag(COMMIT_Nolink, ip)) {
1754 xtlck->lwm.offset =
1755 (xtlck->lwm.offset) ? min(index,
1756 (int)xtlck->lwm.offset) : index;
1757 xtlck->lwm.length =
1758 le16_to_cpu(p->header.nextindex) - xtlck->lwm.offset;
1761 /* unpin the leaf page */
1762 XT_PUTPAGE(mp);
1764 return rc;
1767 #ifdef _NOTYET
1769 * xtTailgate()
1771 * function: split existing 'tail' extent
1772 * (split offset >= start offset of tail extent), and
1773 * relocate and extend the split tail half;
1775 * note: existing extent may or may not have been committed.
1776 * caller is responsible for pager buffer cache update, and
1777 * working block allocation map update;
1778 * update pmap: free old split tail extent, alloc new extent;
1780 int xtTailgate(tid_t tid, /* transaction id */
1781 struct inode *ip, s64 xoff, /* split/new extent offset */
1782 s32 xlen, /* new extent length */
1783 s64 xaddr, /* new extent address */
1784 int flag)
1786 int rc = 0;
1787 int cmp;
1788 struct metapage *mp; /* meta-page buffer */
1789 xtpage_t *p; /* base B+-tree index page */
1790 s64 bn;
1791 int index, nextindex, llen, rlen;
1792 struct btstack btstack; /* traverse stack */
1793 struct xtsplit split; /* split information */
1794 xad_t *xad;
1795 struct tlock *tlck;
1796 struct xtlock *xtlck = 0;
1797 struct tlock *mtlck;
1798 struct maplock *pxdlock;
1801 printf("xtTailgate: nxoff:0x%lx nxlen:0x%x nxaddr:0x%lx\n",
1802 (ulong)xoff, xlen, (ulong)xaddr);
1805 /* there must exist extent to be tailgated */
1806 if ((rc = xtSearch(ip, xoff, NULL, &cmp, &btstack, XT_INSERT)))
1807 return rc;
1809 /* retrieve search result */
1810 XT_GETSEARCH(ip, btstack.top, bn, mp, p, index);
1812 if (cmp != 0) {
1813 XT_PUTPAGE(mp);
1814 jfs_error(ip->i_sb, "xtTailgate: couldn't find extent");
1815 return -EIO;
1818 /* entry found must be last entry */
1819 nextindex = le16_to_cpu(p->header.nextindex);
1820 if (index != nextindex - 1) {
1821 XT_PUTPAGE(mp);
1822 jfs_error(ip->i_sb,
1823 "xtTailgate: the entry found is not the last entry");
1824 return -EIO;
1827 BT_MARK_DIRTY(mp, ip);
1829 * acquire tlock of the leaf page containing original entry
1831 if (!test_cflag(COMMIT_Nolink, ip)) {
1832 tlck = txLock(tid, ip, mp, tlckXTREE | tlckGROW);
1833 xtlck = (struct xtlock *) & tlck->lock;
1836 /* completely replace extent ? */
1837 xad = &p->xad[index];
1839 printf("xtTailgate: xoff:0x%lx xlen:0x%x xaddr:0x%lx\n",
1840 (ulong)offsetXAD(xad), lengthXAD(xad), (ulong)addressXAD(xad));
1842 if ((llen = xoff - offsetXAD(xad)) == 0)
1843 goto updateOld;
1846 * partially replace extent: insert entry for new extent
1848 //insertNew:
1850 * if the leaf page is full, insert the new entry and
1851 * propagate up the router entry for the new page from split
1853 * The xtSplitUp() will insert the entry and unpin the leaf page.
1855 if (nextindex == le16_to_cpu(p->header.maxentry)) {
1856 /* xtSpliUp() unpins leaf pages */
1857 split.mp = mp;
1858 split.index = index + 1;
1859 split.flag = XAD_NEW;
1860 split.off = xoff; /* split offset */
1861 split.len = xlen;
1862 split.addr = xaddr;
1863 split.pxdlist = NULL;
1864 if ((rc = xtSplitUp(tid, ip, &split, &btstack)))
1865 return rc;
1867 /* get back old page */
1868 XT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
1869 if (rc)
1870 return rc;
1872 * if leaf root has been split, original root has been
1873 * copied to new child page, i.e., original entry now
1874 * resides on the new child page;
1876 if (p->header.flag & BT_INTERNAL) {
1877 ASSERT(p->header.nextindex ==
1878 cpu_to_le16(XTENTRYSTART + 1));
1879 xad = &p->xad[XTENTRYSTART];
1880 bn = addressXAD(xad);
1881 XT_PUTPAGE(mp);
1883 /* get new child page */
1884 XT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
1885 if (rc)
1886 return rc;
1888 BT_MARK_DIRTY(mp, ip);
1889 if (!test_cflag(COMMIT_Nolink, ip)) {
1890 tlck = txLock(tid, ip, mp, tlckXTREE|tlckGROW);
1891 xtlck = (struct xtlock *) & tlck->lock;
1896 * insert the new entry into the leaf page
1898 else {
1899 /* insert the new entry: mark the entry NEW */
1900 xad = &p->xad[index + 1];
1901 XT_PUTENTRY(xad, XAD_NEW, xoff, xlen, xaddr);
1903 /* advance next available entry index */
1904 p->header.nextindex =
1905 cpu_to_le16(le16_to_cpu(p->header.nextindex) + 1);
1908 /* get back old XAD */
1909 xad = &p->xad[index];
1912 * truncate/relocate old extent at split offset
1914 updateOld:
1915 /* update dmap for old/committed/truncated extent */
1916 rlen = lengthXAD(xad) - llen;
1917 if (!(xad->flag & XAD_NEW)) {
1918 /* free from PWMAP at commit */
1919 if (!test_cflag(COMMIT_Nolink, ip)) {
1920 mtlck = txMaplock(tid, ip, tlckMAP);
1921 pxdlock = (struct maplock *) & mtlck->lock;
1922 pxdlock->flag = mlckFREEPXD;
1923 PXDaddress(&pxdlock->pxd, addressXAD(xad) + llen);
1924 PXDlength(&pxdlock->pxd, rlen);
1925 pxdlock->index = 1;
1927 } else
1928 /* free from WMAP */
1929 dbFree(ip, addressXAD(xad) + llen, (s64) rlen);
1931 if (llen)
1932 /* truncate */
1933 XADlength(xad, llen);
1934 else
1935 /* replace */
1936 XT_PUTENTRY(xad, XAD_NEW, xoff, xlen, xaddr);
1938 if (!test_cflag(COMMIT_Nolink, ip)) {
1939 xtlck->lwm.offset = (xtlck->lwm.offset) ?
1940 min(index, (int)xtlck->lwm.offset) : index;
1941 xtlck->lwm.length = le16_to_cpu(p->header.nextindex) -
1942 xtlck->lwm.offset;
1945 /* unpin the leaf page */
1946 XT_PUTPAGE(mp);
1948 return rc;
1950 #endif /* _NOTYET */
1953 * xtUpdate()
1955 * function: update XAD;
1957 * update extent for allocated_but_not_recorded or
1958 * compressed extent;
1960 * parameter:
1961 * nxad - new XAD;
1962 * logical extent of the specified XAD must be completely
1963 * contained by an existing XAD;
1965 int xtUpdate(tid_t tid, struct inode *ip, xad_t * nxad)
1966 { /* new XAD */
1967 int rc = 0;
1968 int cmp;
1969 struct metapage *mp; /* meta-page buffer */
1970 xtpage_t *p; /* base B+-tree index page */
1971 s64 bn;
1972 int index0, index, newindex, nextindex;
1973 struct btstack btstack; /* traverse stack */
1974 struct xtsplit split; /* split information */
1975 xad_t *xad, *lxad, *rxad;
1976 int xflag;
1977 s64 nxoff, xoff;
1978 int nxlen, xlen, lxlen, rxlen;
1979 s64 nxaddr, xaddr;
1980 struct tlock *tlck;
1981 struct xtlock *xtlck = NULL;
1982 int newpage = 0;
1984 /* there must exist extent to be tailgated */
1985 nxoff = offsetXAD(nxad);
1986 nxlen = lengthXAD(nxad);
1987 nxaddr = addressXAD(nxad);
1989 if ((rc = xtSearch(ip, nxoff, NULL, &cmp, &btstack, XT_INSERT)))
1990 return rc;
1992 /* retrieve search result */
1993 XT_GETSEARCH(ip, btstack.top, bn, mp, p, index0);
1995 if (cmp != 0) {
1996 XT_PUTPAGE(mp);
1997 jfs_error(ip->i_sb, "xtUpdate: Could not find extent");
1998 return -EIO;
2001 BT_MARK_DIRTY(mp, ip);
2003 * acquire tlock of the leaf page containing original entry
2005 if (!test_cflag(COMMIT_Nolink, ip)) {
2006 tlck = txLock(tid, ip, mp, tlckXTREE | tlckGROW);
2007 xtlck = (struct xtlock *) & tlck->lock;
2010 xad = &p->xad[index0];
2011 xflag = xad->flag;
2012 xoff = offsetXAD(xad);
2013 xlen = lengthXAD(xad);
2014 xaddr = addressXAD(xad);
2016 /* nXAD must be completely contained within XAD */
2017 if ((xoff > nxoff) ||
2018 (nxoff + nxlen > xoff + xlen)) {
2019 XT_PUTPAGE(mp);
2020 jfs_error(ip->i_sb,
2021 "xtUpdate: nXAD in not completely contained within XAD");
2022 return -EIO;
2025 index = index0;
2026 newindex = index + 1;
2027 nextindex = le16_to_cpu(p->header.nextindex);
2029 #ifdef _JFS_WIP_NOCOALESCE
2030 if (xoff < nxoff)
2031 goto updateRight;
2034 * replace XAD with nXAD
2036 replace: /* (nxoff == xoff) */
2037 if (nxlen == xlen) {
2038 /* replace XAD with nXAD:recorded */
2039 *xad = *nxad;
2040 xad->flag = xflag & ~XAD_NOTRECORDED;
2042 goto out;
2043 } else /* (nxlen < xlen) */
2044 goto updateLeft;
2045 #endif /* _JFS_WIP_NOCOALESCE */
2047 /* #ifdef _JFS_WIP_COALESCE */
2048 if (xoff < nxoff)
2049 goto coalesceRight;
2052 * coalesce with left XAD
2054 //coalesceLeft: /* (xoff == nxoff) */
2055 /* is XAD first entry of page ? */
2056 if (index == XTENTRYSTART)
2057 goto replace;
2059 /* is nXAD logically and physically contiguous with lXAD ? */
2060 lxad = &p->xad[index - 1];
2061 lxlen = lengthXAD(lxad);
2062 if (!(lxad->flag & XAD_NOTRECORDED) &&
2063 (nxoff == offsetXAD(lxad) + lxlen) &&
2064 (nxaddr == addressXAD(lxad) + lxlen) &&
2065 (lxlen + nxlen < MAXXLEN)) {
2066 /* extend right lXAD */
2067 index0 = index - 1;
2068 XADlength(lxad, lxlen + nxlen);
2070 /* If we just merged two extents together, need to make sure the
2071 * right extent gets logged. If the left one is marked XAD_NEW,
2072 * then we know it will be logged. Otherwise, mark as
2073 * XAD_EXTENDED
2075 if (!(lxad->flag & XAD_NEW))
2076 lxad->flag |= XAD_EXTENDED;
2078 if (xlen > nxlen) {
2079 /* truncate XAD */
2080 XADoffset(xad, xoff + nxlen);
2081 XADlength(xad, xlen - nxlen);
2082 XADaddress(xad, xaddr + nxlen);
2083 goto out;
2084 } else { /* (xlen == nxlen) */
2086 /* remove XAD */
2087 if (index < nextindex - 1)
2088 memmove(&p->xad[index], &p->xad[index + 1],
2089 (nextindex - index -
2090 1) << L2XTSLOTSIZE);
2092 p->header.nextindex =
2093 cpu_to_le16(le16_to_cpu(p->header.nextindex) -
2096 index = index0;
2097 newindex = index + 1;
2098 nextindex = le16_to_cpu(p->header.nextindex);
2099 xoff = nxoff = offsetXAD(lxad);
2100 xlen = nxlen = lxlen + nxlen;
2101 xaddr = nxaddr = addressXAD(lxad);
2102 goto coalesceRight;
2107 * replace XAD with nXAD
2109 replace: /* (nxoff == xoff) */
2110 if (nxlen == xlen) {
2111 /* replace XAD with nXAD:recorded */
2112 *xad = *nxad;
2113 xad->flag = xflag & ~XAD_NOTRECORDED;
2115 goto coalesceRight;
2116 } else /* (nxlen < xlen) */
2117 goto updateLeft;
2120 * coalesce with right XAD
2122 coalesceRight: /* (xoff <= nxoff) */
2123 /* is XAD last entry of page ? */
2124 if (newindex == nextindex) {
2125 if (xoff == nxoff)
2126 goto out;
2127 goto updateRight;
2130 /* is nXAD logically and physically contiguous with rXAD ? */
2131 rxad = &p->xad[index + 1];
2132 rxlen = lengthXAD(rxad);
2133 if (!(rxad->flag & XAD_NOTRECORDED) &&
2134 (nxoff + nxlen == offsetXAD(rxad)) &&
2135 (nxaddr + nxlen == addressXAD(rxad)) &&
2136 (rxlen + nxlen < MAXXLEN)) {
2137 /* extend left rXAD */
2138 XADoffset(rxad, nxoff);
2139 XADlength(rxad, rxlen + nxlen);
2140 XADaddress(rxad, nxaddr);
2142 /* If we just merged two extents together, need to make sure
2143 * the left extent gets logged. If the right one is marked
2144 * XAD_NEW, then we know it will be logged. Otherwise, mark as
2145 * XAD_EXTENDED
2147 if (!(rxad->flag & XAD_NEW))
2148 rxad->flag |= XAD_EXTENDED;
2150 if (xlen > nxlen)
2151 /* truncate XAD */
2152 XADlength(xad, xlen - nxlen);
2153 else { /* (xlen == nxlen) */
2155 /* remove XAD */
2156 memmove(&p->xad[index], &p->xad[index + 1],
2157 (nextindex - index - 1) << L2XTSLOTSIZE);
2159 p->header.nextindex =
2160 cpu_to_le16(le16_to_cpu(p->header.nextindex) -
2164 goto out;
2165 } else if (xoff == nxoff)
2166 goto out;
2168 if (xoff >= nxoff) {
2169 XT_PUTPAGE(mp);
2170 jfs_error(ip->i_sb, "xtUpdate: xoff >= nxoff");
2171 return -EIO;
2173 /* #endif _JFS_WIP_COALESCE */
2176 * split XAD into (lXAD, nXAD):
2178 * |---nXAD--->
2179 * --|----------XAD----------|--
2180 * |-lXAD-|
2182 updateRight: /* (xoff < nxoff) */
2183 /* truncate old XAD as lXAD:not_recorded */
2184 xad = &p->xad[index];
2185 XADlength(xad, nxoff - xoff);
2187 /* insert nXAD:recorded */
2188 if (nextindex == le16_to_cpu(p->header.maxentry)) {
2190 /* xtSpliUp() unpins leaf pages */
2191 split.mp = mp;
2192 split.index = newindex;
2193 split.flag = xflag & ~XAD_NOTRECORDED;
2194 split.off = nxoff;
2195 split.len = nxlen;
2196 split.addr = nxaddr;
2197 split.pxdlist = NULL;
2198 if ((rc = xtSplitUp(tid, ip, &split, &btstack)))
2199 return rc;
2201 /* get back old page */
2202 XT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
2203 if (rc)
2204 return rc;
2206 * if leaf root has been split, original root has been
2207 * copied to new child page, i.e., original entry now
2208 * resides on the new child page;
2210 if (p->header.flag & BT_INTERNAL) {
2211 ASSERT(p->header.nextindex ==
2212 cpu_to_le16(XTENTRYSTART + 1));
2213 xad = &p->xad[XTENTRYSTART];
2214 bn = addressXAD(xad);
2215 XT_PUTPAGE(mp);
2217 /* get new child page */
2218 XT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
2219 if (rc)
2220 return rc;
2222 BT_MARK_DIRTY(mp, ip);
2223 if (!test_cflag(COMMIT_Nolink, ip)) {
2224 tlck = txLock(tid, ip, mp, tlckXTREE|tlckGROW);
2225 xtlck = (struct xtlock *) & tlck->lock;
2227 } else {
2228 /* is nXAD on new page ? */
2229 if (newindex >
2230 (le16_to_cpu(p->header.maxentry) >> 1)) {
2231 newindex =
2232 newindex -
2233 le16_to_cpu(p->header.nextindex) +
2234 XTENTRYSTART;
2235 newpage = 1;
2238 } else {
2239 /* if insert into middle, shift right remaining entries */
2240 if (newindex < nextindex)
2241 memmove(&p->xad[newindex + 1], &p->xad[newindex],
2242 (nextindex - newindex) << L2XTSLOTSIZE);
2244 /* insert the entry */
2245 xad = &p->xad[newindex];
2246 *xad = *nxad;
2247 xad->flag = xflag & ~XAD_NOTRECORDED;
2249 /* advance next available entry index. */
2250 p->header.nextindex =
2251 cpu_to_le16(le16_to_cpu(p->header.nextindex) + 1);
2255 * does nXAD force 3-way split ?
2257 * |---nXAD--->|
2258 * --|----------XAD-------------|--
2259 * |-lXAD-| |-rXAD -|
2261 if (nxoff + nxlen == xoff + xlen)
2262 goto out;
2264 /* reorient nXAD as XAD for further split XAD into (nXAD, rXAD) */
2265 if (newpage) {
2266 /* close out old page */
2267 if (!test_cflag(COMMIT_Nolink, ip)) {
2268 xtlck->lwm.offset = (xtlck->lwm.offset) ?
2269 min(index0, (int)xtlck->lwm.offset) : index0;
2270 xtlck->lwm.length =
2271 le16_to_cpu(p->header.nextindex) -
2272 xtlck->lwm.offset;
2275 bn = le64_to_cpu(p->header.next);
2276 XT_PUTPAGE(mp);
2278 /* get new right page */
2279 XT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
2280 if (rc)
2281 return rc;
2283 BT_MARK_DIRTY(mp, ip);
2284 if (!test_cflag(COMMIT_Nolink, ip)) {
2285 tlck = txLock(tid, ip, mp, tlckXTREE | tlckGROW);
2286 xtlck = (struct xtlock *) & tlck->lock;
2289 index0 = index = newindex;
2290 } else
2291 index++;
2293 newindex = index + 1;
2294 nextindex = le16_to_cpu(p->header.nextindex);
2295 xlen = xlen - (nxoff - xoff);
2296 xoff = nxoff;
2297 xaddr = nxaddr;
2299 /* recompute split pages */
2300 if (nextindex == le16_to_cpu(p->header.maxentry)) {
2301 XT_PUTPAGE(mp);
2303 if ((rc = xtSearch(ip, nxoff, NULL, &cmp, &btstack, XT_INSERT)))
2304 return rc;
2306 /* retrieve search result */
2307 XT_GETSEARCH(ip, btstack.top, bn, mp, p, index0);
2309 if (cmp != 0) {
2310 XT_PUTPAGE(mp);
2311 jfs_error(ip->i_sb, "xtUpdate: xtSearch failed");
2312 return -EIO;
2315 if (index0 != index) {
2316 XT_PUTPAGE(mp);
2317 jfs_error(ip->i_sb,
2318 "xtUpdate: unexpected value of index");
2319 return -EIO;
2324 * split XAD into (nXAD, rXAD)
2326 * ---nXAD---|
2327 * --|----------XAD----------|--
2328 * |-rXAD-|
2330 updateLeft: /* (nxoff == xoff) && (nxlen < xlen) */
2331 /* update old XAD with nXAD:recorded */
2332 xad = &p->xad[index];
2333 *xad = *nxad;
2334 xad->flag = xflag & ~XAD_NOTRECORDED;
2336 /* insert rXAD:not_recorded */
2337 xoff = xoff + nxlen;
2338 xlen = xlen - nxlen;
2339 xaddr = xaddr + nxlen;
2340 if (nextindex == le16_to_cpu(p->header.maxentry)) {
2342 printf("xtUpdate.updateLeft.split p:0x%p\n", p);
2344 /* xtSpliUp() unpins leaf pages */
2345 split.mp = mp;
2346 split.index = newindex;
2347 split.flag = xflag;
2348 split.off = xoff;
2349 split.len = xlen;
2350 split.addr = xaddr;
2351 split.pxdlist = NULL;
2352 if ((rc = xtSplitUp(tid, ip, &split, &btstack)))
2353 return rc;
2355 /* get back old page */
2356 XT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
2357 if (rc)
2358 return rc;
2361 * if leaf root has been split, original root has been
2362 * copied to new child page, i.e., original entry now
2363 * resides on the new child page;
2365 if (p->header.flag & BT_INTERNAL) {
2366 ASSERT(p->header.nextindex ==
2367 cpu_to_le16(XTENTRYSTART + 1));
2368 xad = &p->xad[XTENTRYSTART];
2369 bn = addressXAD(xad);
2370 XT_PUTPAGE(mp);
2372 /* get new child page */
2373 XT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
2374 if (rc)
2375 return rc;
2377 BT_MARK_DIRTY(mp, ip);
2378 if (!test_cflag(COMMIT_Nolink, ip)) {
2379 tlck = txLock(tid, ip, mp, tlckXTREE|tlckGROW);
2380 xtlck = (struct xtlock *) & tlck->lock;
2383 } else {
2384 /* if insert into middle, shift right remaining entries */
2385 if (newindex < nextindex)
2386 memmove(&p->xad[newindex + 1], &p->xad[newindex],
2387 (nextindex - newindex) << L2XTSLOTSIZE);
2389 /* insert the entry */
2390 xad = &p->xad[newindex];
2391 XT_PUTENTRY(xad, xflag, xoff, xlen, xaddr);
2393 /* advance next available entry index. */
2394 p->header.nextindex =
2395 cpu_to_le16(le16_to_cpu(p->header.nextindex) + 1);
2398 out:
2399 if (!test_cflag(COMMIT_Nolink, ip)) {
2400 xtlck->lwm.offset = (xtlck->lwm.offset) ?
2401 min(index0, (int)xtlck->lwm.offset) : index0;
2402 xtlck->lwm.length = le16_to_cpu(p->header.nextindex) -
2403 xtlck->lwm.offset;
2406 /* unpin the leaf page */
2407 XT_PUTPAGE(mp);
2409 return rc;
2414 * xtAppend()
2416 * function: grow in append mode from contiguous region specified ;
2418 * parameter:
2419 * tid - transaction id;
2420 * ip - file object;
2421 * xflag - extent flag:
2422 * xoff - extent offset;
2423 * maxblocks - max extent length;
2424 * xlen - extent length (in/out);
2425 * xaddrp - extent address pointer (in/out):
2426 * flag -
2428 * return:
2430 int xtAppend(tid_t tid, /* transaction id */
2431 struct inode *ip, int xflag, s64 xoff, s32 maxblocks,
2432 s32 * xlenp, /* (in/out) */
2433 s64 * xaddrp, /* (in/out) */
2434 int flag)
2436 int rc = 0;
2437 struct metapage *mp; /* meta-page buffer */
2438 xtpage_t *p; /* base B+-tree index page */
2439 s64 bn, xaddr;
2440 int index, nextindex;
2441 struct btstack btstack; /* traverse stack */
2442 struct xtsplit split; /* split information */
2443 xad_t *xad;
2444 int cmp;
2445 struct tlock *tlck;
2446 struct xtlock *xtlck;
2447 int nsplit, nblocks, xlen;
2448 struct pxdlist pxdlist;
2449 pxd_t *pxd;
2450 s64 next;
2452 xaddr = *xaddrp;
2453 xlen = *xlenp;
2454 jfs_info("xtAppend: xoff:0x%lx maxblocks:%d xlen:%d xaddr:0x%lx",
2455 (ulong) xoff, maxblocks, xlen, (ulong) xaddr);
2458 * search for the entry location at which to insert:
2460 * xtFastSearch() and xtSearch() both returns (leaf page
2461 * pinned, index at which to insert).
2462 * n.b. xtSearch() may return index of maxentry of
2463 * the full page.
2465 if ((rc = xtSearch(ip, xoff, &next, &cmp, &btstack, XT_INSERT)))
2466 return rc;
2468 /* retrieve search result */
2469 XT_GETSEARCH(ip, btstack.top, bn, mp, p, index);
2471 if (cmp == 0) {
2472 rc = -EEXIST;
2473 goto out;
2476 if (next)
2477 xlen = min(xlen, (int)(next - xoff));
2478 //insert:
2480 * insert entry for new extent
2482 xflag |= XAD_NEW;
2485 * if the leaf page is full, split the page and
2486 * propagate up the router entry for the new page from split
2488 * The xtSplitUp() will insert the entry and unpin the leaf page.
2490 nextindex = le16_to_cpu(p->header.nextindex);
2491 if (nextindex < le16_to_cpu(p->header.maxentry))
2492 goto insertLeaf;
2495 * allocate new index blocks to cover index page split(s)
2497 nsplit = btstack.nsplit;
2498 split.pxdlist = &pxdlist;
2499 pxdlist.maxnpxd = pxdlist.npxd = 0;
2500 pxd = &pxdlist.pxd[0];
2501 nblocks = JFS_SBI(ip->i_sb)->nbperpage;
2502 for (; nsplit > 0; nsplit--, pxd++, xaddr += nblocks, maxblocks -= nblocks) {
2503 if ((rc = dbAllocBottomUp(ip, xaddr, (s64) nblocks)) == 0) {
2504 PXDaddress(pxd, xaddr);
2505 PXDlength(pxd, nblocks);
2507 pxdlist.maxnpxd++;
2509 continue;
2512 /* undo allocation */
2514 goto out;
2517 xlen = min(xlen, maxblocks);
2520 * allocate data extent requested
2522 if ((rc = dbAllocBottomUp(ip, xaddr, (s64) xlen)))
2523 goto out;
2525 split.mp = mp;
2526 split.index = index;
2527 split.flag = xflag;
2528 split.off = xoff;
2529 split.len = xlen;
2530 split.addr = xaddr;
2531 if ((rc = xtSplitUp(tid, ip, &split, &btstack))) {
2532 /* undo data extent allocation */
2533 dbFree(ip, *xaddrp, (s64) * xlenp);
2535 return rc;
2538 *xaddrp = xaddr;
2539 *xlenp = xlen;
2540 return 0;
2543 * insert the new entry into the leaf page
2545 insertLeaf:
2547 * allocate data extent requested
2549 if ((rc = dbAllocBottomUp(ip, xaddr, (s64) xlen)))
2550 goto out;
2552 BT_MARK_DIRTY(mp, ip);
2554 * acquire a transaction lock on the leaf page;
2556 * action: xad insertion/extension;
2558 tlck = txLock(tid, ip, mp, tlckXTREE | tlckGROW);
2559 xtlck = (struct xtlock *) & tlck->lock;
2561 /* insert the new entry: mark the entry NEW */
2562 xad = &p->xad[index];
2563 XT_PUTENTRY(xad, xflag, xoff, xlen, xaddr);
2565 /* advance next available entry index */
2566 p->header.nextindex =
2567 cpu_to_le16(le16_to_cpu(p->header.nextindex) + 1);
2569 xtlck->lwm.offset =
2570 (xtlck->lwm.offset) ? min(index,(int) xtlck->lwm.offset) : index;
2571 xtlck->lwm.length = le16_to_cpu(p->header.nextindex) -
2572 xtlck->lwm.offset;
2574 *xaddrp = xaddr;
2575 *xlenp = xlen;
2577 out:
2578 /* unpin the leaf page */
2579 XT_PUTPAGE(mp);
2581 return rc;
2583 #ifdef _STILL_TO_PORT
2585 /* - TBD for defragmentaion/reorganization -
2587 * xtDelete()
2589 * function:
2590 * delete the entry with the specified key.
2592 * N.B.: whole extent of the entry is assumed to be deleted.
2594 * parameter:
2596 * return:
2597 * ENOENT: if the entry is not found.
2599 * exception:
2601 int xtDelete(tid_t tid, struct inode *ip, s64 xoff, s32 xlen, int flag)
2603 int rc = 0;
2604 struct btstack btstack;
2605 int cmp;
2606 s64 bn;
2607 struct metapage *mp;
2608 xtpage_t *p;
2609 int index, nextindex;
2610 struct tlock *tlck;
2611 struct xtlock *xtlck;
2614 * find the matching entry; xtSearch() pins the page
2616 if ((rc = xtSearch(ip, xoff, NULL, &cmp, &btstack, 0)))
2617 return rc;
2619 XT_GETSEARCH(ip, btstack.top, bn, mp, p, index);
2620 if (cmp) {
2621 /* unpin the leaf page */
2622 XT_PUTPAGE(mp);
2623 return -ENOENT;
2627 * delete the entry from the leaf page
2629 nextindex = le16_to_cpu(p->header.nextindex);
2630 p->header.nextindex =
2631 cpu_to_le16(le16_to_cpu(p->header.nextindex) - 1);
2634 * if the leaf page bocome empty, free the page
2636 if (p->header.nextindex == cpu_to_le16(XTENTRYSTART))
2637 return (xtDeleteUp(tid, ip, mp, p, &btstack));
2639 BT_MARK_DIRTY(mp, ip);
2641 * acquire a transaction lock on the leaf page;
2643 * action:xad deletion;
2645 tlck = txLock(tid, ip, mp, tlckXTREE);
2646 xtlck = (struct xtlock *) & tlck->lock;
2647 xtlck->lwm.offset =
2648 (xtlck->lwm.offset) ? min(index, xtlck->lwm.offset) : index;
2650 /* if delete from middle, shift left/compact the remaining entries */
2651 if (index < nextindex - 1)
2652 memmove(&p->xad[index], &p->xad[index + 1],
2653 (nextindex - index - 1) * sizeof(xad_t));
2655 XT_PUTPAGE(mp);
2657 return 0;
2661 /* - TBD for defragmentaion/reorganization -
2663 * xtDeleteUp()
2665 * function:
2666 * free empty pages as propagating deletion up the tree
2668 * parameter:
2670 * return:
2672 static int
2673 xtDeleteUp(tid_t tid, struct inode *ip,
2674 struct metapage * fmp, xtpage_t * fp, struct btstack * btstack)
2676 int rc = 0;
2677 struct metapage *mp;
2678 xtpage_t *p;
2679 int index, nextindex;
2680 s64 xaddr;
2681 int xlen;
2682 struct btframe *parent;
2683 struct tlock *tlck;
2684 struct xtlock *xtlck;
2687 * keep root leaf page which has become empty
2689 if (fp->header.flag & BT_ROOT) {
2690 /* keep the root page */
2691 fp->header.flag &= ~BT_INTERNAL;
2692 fp->header.flag |= BT_LEAF;
2693 fp->header.nextindex = cpu_to_le16(XTENTRYSTART);
2695 /* XT_PUTPAGE(fmp); */
2697 return 0;
2701 * free non-root leaf page
2703 if ((rc = xtRelink(tid, ip, fp))) {
2704 XT_PUTPAGE(fmp);
2705 return rc;
2708 xaddr = addressPXD(&fp->header.self);
2709 xlen = lengthPXD(&fp->header.self);
2710 /* free the page extent */
2711 dbFree(ip, xaddr, (s64) xlen);
2713 /* free the buffer page */
2714 discard_metapage(fmp);
2717 * propagate page deletion up the index tree
2719 * If the delete from the parent page makes it empty,
2720 * continue all the way up the tree.
2721 * stop if the root page is reached (which is never deleted) or
2722 * if the entry deletion does not empty the page.
2724 while ((parent = BT_POP(btstack)) != NULL) {
2725 /* get/pin the parent page <sp> */
2726 XT_GETPAGE(ip, parent->bn, mp, PSIZE, p, rc);
2727 if (rc)
2728 return rc;
2730 index = parent->index;
2732 /* delete the entry for the freed child page from parent.
2734 nextindex = le16_to_cpu(p->header.nextindex);
2737 * the parent has the single entry being deleted:
2738 * free the parent page which has become empty.
2740 if (nextindex == 1) {
2741 if (p->header.flag & BT_ROOT) {
2742 /* keep the root page */
2743 p->header.flag &= ~BT_INTERNAL;
2744 p->header.flag |= BT_LEAF;
2745 p->header.nextindex =
2746 cpu_to_le16(XTENTRYSTART);
2748 /* XT_PUTPAGE(mp); */
2750 break;
2751 } else {
2752 /* free the parent page */
2753 if ((rc = xtRelink(tid, ip, p)))
2754 return rc;
2756 xaddr = addressPXD(&p->header.self);
2757 /* free the page extent */
2758 dbFree(ip, xaddr,
2759 (s64) JFS_SBI(ip->i_sb)->nbperpage);
2761 /* unpin/free the buffer page */
2762 discard_metapage(mp);
2764 /* propagate up */
2765 continue;
2769 * the parent has other entries remaining:
2770 * delete the router entry from the parent page.
2772 else {
2773 BT_MARK_DIRTY(mp, ip);
2775 * acquire a transaction lock on the leaf page;
2777 * action:xad deletion;
2779 tlck = txLock(tid, ip, mp, tlckXTREE);
2780 xtlck = (struct xtlock *) & tlck->lock;
2781 xtlck->lwm.offset =
2782 (xtlck->lwm.offset) ? min(index,
2783 xtlck->lwm.
2784 offset) : index;
2786 /* if delete from middle,
2787 * shift left/compact the remaining entries in the page
2789 if (index < nextindex - 1)
2790 memmove(&p->xad[index], &p->xad[index + 1],
2791 (nextindex - index -
2792 1) << L2XTSLOTSIZE);
2794 p->header.nextindex =
2795 cpu_to_le16(le16_to_cpu(p->header.nextindex) -
2797 jfs_info("xtDeleteUp(entry): 0x%lx[%d]",
2798 (ulong) parent->bn, index);
2801 /* unpin the parent page */
2802 XT_PUTPAGE(mp);
2804 /* exit propagation up */
2805 break;
2808 return 0;
2813 * NAME: xtRelocate()
2815 * FUNCTION: relocate xtpage or data extent of regular file;
2816 * This function is mainly used by defragfs utility.
2818 * NOTE: This routine does not have the logic to handle
2819 * uncommitted allocated extent. The caller should call
2820 * txCommit() to commit all the allocation before call
2821 * this routine.
2824 xtRelocate(tid_t tid, struct inode * ip, xad_t * oxad, /* old XAD */
2825 s64 nxaddr, /* new xaddr */
2826 int xtype)
2827 { /* extent type: XTPAGE or DATAEXT */
2828 int rc = 0;
2829 struct tblock *tblk;
2830 struct tlock *tlck;
2831 struct xtlock *xtlck;
2832 struct metapage *mp, *pmp, *lmp, *rmp; /* meta-page buffer */
2833 xtpage_t *p, *pp, *rp, *lp; /* base B+-tree index page */
2834 xad_t *xad;
2835 pxd_t *pxd;
2836 s64 xoff, xsize;
2837 int xlen;
2838 s64 oxaddr, sxaddr, dxaddr, nextbn, prevbn;
2839 cbuf_t *cp;
2840 s64 offset, nbytes, nbrd, pno;
2841 int nb, npages, nblks;
2842 s64 bn;
2843 int cmp;
2844 int index;
2845 struct pxd_lock *pxdlock;
2846 struct btstack btstack; /* traverse stack */
2848 xtype = xtype & EXTENT_TYPE;
2850 xoff = offsetXAD(oxad);
2851 oxaddr = addressXAD(oxad);
2852 xlen = lengthXAD(oxad);
2854 /* validate extent offset */
2855 offset = xoff << JFS_SBI(ip->i_sb)->l2bsize;
2856 if (offset >= ip->i_size)
2857 return -ESTALE; /* stale extent */
2859 jfs_info("xtRelocate: xtype:%d xoff:0x%lx xlen:0x%x xaddr:0x%lx:0x%lx",
2860 xtype, (ulong) xoff, xlen, (ulong) oxaddr, (ulong) nxaddr);
2863 * 1. get and validate the parent xtpage/xad entry
2864 * covering the source extent to be relocated;
2866 if (xtype == DATAEXT) {
2867 /* search in leaf entry */
2868 rc = xtSearch(ip, xoff, NULL, &cmp, &btstack, 0);
2869 if (rc)
2870 return rc;
2872 /* retrieve search result */
2873 XT_GETSEARCH(ip, btstack.top, bn, pmp, pp, index);
2875 if (cmp) {
2876 XT_PUTPAGE(pmp);
2877 return -ESTALE;
2880 /* validate for exact match with a single entry */
2881 xad = &pp->xad[index];
2882 if (addressXAD(xad) != oxaddr || lengthXAD(xad) != xlen) {
2883 XT_PUTPAGE(pmp);
2884 return -ESTALE;
2886 } else { /* (xtype == XTPAGE) */
2888 /* search in internal entry */
2889 rc = xtSearchNode(ip, oxad, &cmp, &btstack, 0);
2890 if (rc)
2891 return rc;
2893 /* retrieve search result */
2894 XT_GETSEARCH(ip, btstack.top, bn, pmp, pp, index);
2896 if (cmp) {
2897 XT_PUTPAGE(pmp);
2898 return -ESTALE;
2901 /* xtSearchNode() validated for exact match with a single entry
2903 xad = &pp->xad[index];
2905 jfs_info("xtRelocate: parent xad entry validated.");
2908 * 2. relocate the extent
2910 if (xtype == DATAEXT) {
2911 /* if the extent is allocated-but-not-recorded
2912 * there is no real data to be moved in this extent,
2914 if (xad->flag & XAD_NOTRECORDED)
2915 goto out;
2916 else
2917 /* release xtpage for cmRead()/xtLookup() */
2918 XT_PUTPAGE(pmp);
2921 * cmRelocate()
2923 * copy target data pages to be relocated;
2925 * data extent must start at page boundary and
2926 * multiple of page size (except the last data extent);
2927 * read in each page of the source data extent into cbuf,
2928 * update the cbuf extent descriptor of the page to be
2929 * homeward bound to new dst data extent
2930 * copy the data from the old extent to new extent.
2931 * copy is essential for compressed files to avoid problems
2932 * that can arise if there was a change in compression
2933 * algorithms.
2934 * it is a good strategy because it may disrupt cache
2935 * policy to keep the pages in memory afterwards.
2937 offset = xoff << JFS_SBI(ip->i_sb)->l2bsize;
2938 assert((offset & CM_OFFSET) == 0);
2939 nbytes = xlen << JFS_SBI(ip->i_sb)->l2bsize;
2940 pno = offset >> CM_L2BSIZE;
2941 npages = (nbytes + (CM_BSIZE - 1)) >> CM_L2BSIZE;
2943 npages = ((offset + nbytes - 1) >> CM_L2BSIZE) -
2944 (offset >> CM_L2BSIZE) + 1;
2946 sxaddr = oxaddr;
2947 dxaddr = nxaddr;
2949 /* process the request one cache buffer at a time */
2950 for (nbrd = 0; nbrd < nbytes; nbrd += nb,
2951 offset += nb, pno++, npages--) {
2952 /* compute page size */
2953 nb = min(nbytes - nbrd, CM_BSIZE);
2955 /* get the cache buffer of the page */
2956 if (rc = cmRead(ip, offset, npages, &cp))
2957 break;
2959 assert(addressPXD(&cp->cm_pxd) == sxaddr);
2960 assert(!cp->cm_modified);
2962 /* bind buffer with the new extent address */
2963 nblks = nb >> JFS_IP(ip->i_sb)->l2bsize;
2964 cmSetXD(ip, cp, pno, dxaddr, nblks);
2966 /* release the cbuf, mark it as modified */
2967 cmPut(cp, true);
2969 dxaddr += nblks;
2970 sxaddr += nblks;
2973 /* get back parent page */
2974 if ((rc = xtSearch(ip, xoff, NULL, &cmp, &btstack, 0)))
2975 return rc;
2977 XT_GETSEARCH(ip, btstack.top, bn, pmp, pp, index);
2978 jfs_info("xtRelocate: target data extent relocated.");
2979 } else { /* (xtype == XTPAGE) */
2982 * read in the target xtpage from the source extent;
2984 XT_GETPAGE(ip, oxaddr, mp, PSIZE, p, rc);
2985 if (rc) {
2986 XT_PUTPAGE(pmp);
2987 return rc;
2991 * read in sibling pages if any to update sibling pointers;
2993 rmp = NULL;
2994 if (p->header.next) {
2995 nextbn = le64_to_cpu(p->header.next);
2996 XT_GETPAGE(ip, nextbn, rmp, PSIZE, rp, rc);
2997 if (rc) {
2998 XT_PUTPAGE(pmp);
2999 XT_PUTPAGE(mp);
3000 return (rc);
3004 lmp = NULL;
3005 if (p->header.prev) {
3006 prevbn = le64_to_cpu(p->header.prev);
3007 XT_GETPAGE(ip, prevbn, lmp, PSIZE, lp, rc);
3008 if (rc) {
3009 XT_PUTPAGE(pmp);
3010 XT_PUTPAGE(mp);
3011 if (rmp)
3012 XT_PUTPAGE(rmp);
3013 return (rc);
3017 /* at this point, all xtpages to be updated are in memory */
3020 * update sibling pointers of sibling xtpages if any;
3022 if (lmp) {
3023 BT_MARK_DIRTY(lmp, ip);
3024 tlck =
3025 txLock(tid, ip, lmp, tlckXTREE | tlckRELINK);
3026 lp->header.next = cpu_to_le64(nxaddr);
3027 XT_PUTPAGE(lmp);
3030 if (rmp) {
3031 BT_MARK_DIRTY(rmp, ip);
3032 tlck =
3033 txLock(tid, ip, rmp, tlckXTREE | tlckRELINK);
3034 rp->header.prev = cpu_to_le64(nxaddr);
3035 XT_PUTPAGE(rmp);
3039 * update the target xtpage to be relocated
3041 * update the self address of the target page
3042 * and write to destination extent;
3043 * redo image covers the whole xtpage since it is new page
3044 * to the destination extent;
3045 * update of bmap for the free of source extent
3046 * of the target xtpage itself:
3047 * update of bmap for the allocation of destination extent
3048 * of the target xtpage itself:
3049 * update of bmap for the extents covered by xad entries in
3050 * the target xtpage is not necessary since they are not
3051 * updated;
3052 * if not committed before this relocation,
3053 * target page may contain XAD_NEW entries which must
3054 * be scanned for bmap update (logredo() always
3055 * scan xtpage REDOPAGE image for bmap update);
3056 * if committed before this relocation (tlckRELOCATE),
3057 * scan may be skipped by commit() and logredo();
3059 BT_MARK_DIRTY(mp, ip);
3060 /* tlckNEW init xtlck->lwm.offset = XTENTRYSTART; */
3061 tlck = txLock(tid, ip, mp, tlckXTREE | tlckNEW);
3062 xtlck = (struct xtlock *) & tlck->lock;
3064 /* update the self address in the xtpage header */
3065 pxd = &p->header.self;
3066 PXDaddress(pxd, nxaddr);
3068 /* linelock for the after image of the whole page */
3069 xtlck->lwm.length =
3070 le16_to_cpu(p->header.nextindex) - xtlck->lwm.offset;
3072 /* update the buffer extent descriptor of target xtpage */
3073 xsize = xlen << JFS_SBI(ip->i_sb)->l2bsize;
3074 bmSetXD(mp, nxaddr, xsize);
3076 /* unpin the target page to new homeward bound */
3077 XT_PUTPAGE(mp);
3078 jfs_info("xtRelocate: target xtpage relocated.");
3082 * 3. acquire maplock for the source extent to be freed;
3084 * acquire a maplock saving the src relocated extent address;
3085 * to free of the extent at commit time;
3087 out:
3088 /* if DATAEXT relocation, write a LOG_UPDATEMAP record for
3089 * free PXD of the source data extent (logredo() will update
3090 * bmap for free of source data extent), and update bmap for
3091 * free of the source data extent;
3093 if (xtype == DATAEXT)
3094 tlck = txMaplock(tid, ip, tlckMAP);
3095 /* if XTPAGE relocation, write a LOG_NOREDOPAGE record
3096 * for the source xtpage (logredo() will init NoRedoPage
3097 * filter and will also update bmap for free of the source
3098 * xtpage), and update bmap for free of the source xtpage;
3099 * N.B. We use tlckMAP instead of tlkcXTREE because there
3100 * is no buffer associated with this lock since the buffer
3101 * has been redirected to the target location.
3103 else /* (xtype == XTPAGE) */
3104 tlck = txMaplock(tid, ip, tlckMAP | tlckRELOCATE);
3106 pxdlock = (struct pxd_lock *) & tlck->lock;
3107 pxdlock->flag = mlckFREEPXD;
3108 PXDaddress(&pxdlock->pxd, oxaddr);
3109 PXDlength(&pxdlock->pxd, xlen);
3110 pxdlock->index = 1;
3113 * 4. update the parent xad entry for relocation;
3115 * acquire tlck for the parent entry with XAD_NEW as entry
3116 * update which will write LOG_REDOPAGE and update bmap for
3117 * allocation of XAD_NEW destination extent;
3119 jfs_info("xtRelocate: update parent xad entry.");
3120 BT_MARK_DIRTY(pmp, ip);
3121 tlck = txLock(tid, ip, pmp, tlckXTREE | tlckGROW);
3122 xtlck = (struct xtlock *) & tlck->lock;
3124 /* update the XAD with the new destination extent; */
3125 xad = &pp->xad[index];
3126 xad->flag |= XAD_NEW;
3127 XADaddress(xad, nxaddr);
3129 xtlck->lwm.offset = min(index, xtlck->lwm.offset);
3130 xtlck->lwm.length = le16_to_cpu(pp->header.nextindex) -
3131 xtlck->lwm.offset;
3133 /* unpin the parent xtpage */
3134 XT_PUTPAGE(pmp);
3136 return rc;
3141 * xtSearchNode()
3143 * function: search for the internal xad entry covering specified extent.
3144 * This function is mainly used by defragfs utility.
3146 * parameters:
3147 * ip - file object;
3148 * xad - extent to find;
3149 * cmpp - comparison result:
3150 * btstack - traverse stack;
3151 * flag - search process flag;
3153 * returns:
3154 * btstack contains (bn, index) of search path traversed to the entry.
3155 * *cmpp is set to result of comparison with the entry returned.
3156 * the page containing the entry is pinned at exit.
3158 static int xtSearchNode(struct inode *ip, xad_t * xad, /* required XAD entry */
3159 int *cmpp, struct btstack * btstack, int flag)
3161 int rc = 0;
3162 s64 xoff, xaddr;
3163 int xlen;
3164 int cmp = 1; /* init for empty page */
3165 s64 bn; /* block number */
3166 struct metapage *mp; /* meta-page buffer */
3167 xtpage_t *p; /* page */
3168 int base, index, lim;
3169 struct btframe *btsp;
3170 s64 t64;
3172 BT_CLR(btstack);
3174 xoff = offsetXAD(xad);
3175 xlen = lengthXAD(xad);
3176 xaddr = addressXAD(xad);
3179 * search down tree from root:
3181 * between two consecutive entries of <Ki, Pi> and <Kj, Pj> of
3182 * internal page, child page Pi contains entry with k, Ki <= K < Kj.
3184 * if entry with search key K is not found
3185 * internal page search find the entry with largest key Ki
3186 * less than K which point to the child page to search;
3187 * leaf page search find the entry with smallest key Kj
3188 * greater than K so that the returned index is the position of
3189 * the entry to be shifted right for insertion of new entry.
3190 * for empty tree, search key is greater than any key of the tree.
3192 * by convention, root bn = 0.
3194 for (bn = 0;;) {
3195 /* get/pin the page to search */
3196 XT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
3197 if (rc)
3198 return rc;
3199 if (p->header.flag & BT_LEAF) {
3200 XT_PUTPAGE(mp);
3201 return -ESTALE;
3204 lim = le16_to_cpu(p->header.nextindex) - XTENTRYSTART;
3207 * binary search with search key K on the current page
3209 for (base = XTENTRYSTART; lim; lim >>= 1) {
3210 index = base + (lim >> 1);
3212 XT_CMP(cmp, xoff, &p->xad[index], t64);
3213 if (cmp == 0) {
3215 * search hit
3217 * verify for exact match;
3219 if (xaddr == addressXAD(&p->xad[index]) &&
3220 xoff == offsetXAD(&p->xad[index])) {
3221 *cmpp = cmp;
3223 /* save search result */
3224 btsp = btstack->top;
3225 btsp->bn = bn;
3226 btsp->index = index;
3227 btsp->mp = mp;
3229 return 0;
3232 /* descend/search its child page */
3233 goto next;
3236 if (cmp > 0) {
3237 base = index + 1;
3238 --lim;
3243 * search miss - non-leaf page:
3245 * base is the smallest index with key (Kj) greater than
3246 * search key (K) and may be zero or maxentry index.
3247 * if base is non-zero, decrement base by one to get the parent
3248 * entry of the child page to search.
3250 index = base ? base - 1 : base;
3253 * go down to child page
3255 next:
3256 /* get the child page block number */
3257 bn = addressXAD(&p->xad[index]);
3259 /* unpin the parent page */
3260 XT_PUTPAGE(mp);
3266 * xtRelink()
3268 * function:
3269 * link around a freed page.
3271 * Parameter:
3272 * int tid,
3273 * struct inode *ip,
3274 * xtpage_t *p)
3276 * returns:
3278 static int xtRelink(tid_t tid, struct inode *ip, xtpage_t * p)
3280 int rc = 0;
3281 struct metapage *mp;
3282 s64 nextbn, prevbn;
3283 struct tlock *tlck;
3285 nextbn = le64_to_cpu(p->header.next);
3286 prevbn = le64_to_cpu(p->header.prev);
3288 /* update prev pointer of the next page */
3289 if (nextbn != 0) {
3290 XT_GETPAGE(ip, nextbn, mp, PSIZE, p, rc);
3291 if (rc)
3292 return rc;
3295 * acquire a transaction lock on the page;
3297 * action: update prev pointer;
3299 BT_MARK_DIRTY(mp, ip);
3300 tlck = txLock(tid, ip, mp, tlckXTREE | tlckRELINK);
3302 /* the page may already have been tlock'd */
3304 p->header.prev = cpu_to_le64(prevbn);
3306 XT_PUTPAGE(mp);
3309 /* update next pointer of the previous page */
3310 if (prevbn != 0) {
3311 XT_GETPAGE(ip, prevbn, mp, PSIZE, p, rc);
3312 if (rc)
3313 return rc;
3316 * acquire a transaction lock on the page;
3318 * action: update next pointer;
3320 BT_MARK_DIRTY(mp, ip);
3321 tlck = txLock(tid, ip, mp, tlckXTREE | tlckRELINK);
3323 /* the page may already have been tlock'd */
3325 p->header.next = le64_to_cpu(nextbn);
3327 XT_PUTPAGE(mp);
3330 return 0;
3332 #endif /* _STILL_TO_PORT */
3336 * xtInitRoot()
3338 * initialize file root (inline in inode)
3340 void xtInitRoot(tid_t tid, struct inode *ip)
3342 xtpage_t *p;
3345 * acquire a transaction lock on the root
3347 * action:
3349 txLock(tid, ip, (struct metapage *) &JFS_IP(ip)->bxflag,
3350 tlckXTREE | tlckNEW);
3351 p = &JFS_IP(ip)->i_xtroot;
3353 p->header.flag = DXD_INDEX | BT_ROOT | BT_LEAF;
3354 p->header.nextindex = cpu_to_le16(XTENTRYSTART);
3356 if (S_ISDIR(ip->i_mode))
3357 p->header.maxentry = cpu_to_le16(XTROOTINITSLOT_DIR);
3358 else {
3359 p->header.maxentry = cpu_to_le16(XTROOTINITSLOT);
3360 ip->i_size = 0;
3364 return;
3369 * We can run into a deadlock truncating a file with a large number of
3370 * xtree pages (large fragmented file). A robust fix would entail a
3371 * reservation system where we would reserve a number of metadata pages
3372 * and tlocks which we would be guaranteed without a deadlock. Without
3373 * this, a partial fix is to limit number of metadata pages we will lock
3374 * in a single transaction. Currently we will truncate the file so that
3375 * no more than 50 leaf pages will be locked. The caller of xtTruncate
3376 * will be responsible for ensuring that the current transaction gets
3377 * committed, and that subsequent transactions are created to truncate
3378 * the file further if needed.
3380 #define MAX_TRUNCATE_LEAVES 50
3383 * xtTruncate()
3385 * function:
3386 * traverse for truncation logging backward bottom up;
3387 * terminate at the last extent entry at the current subtree
3388 * root page covering new down size.
3389 * truncation may occur within the last extent entry.
3391 * parameter:
3392 * int tid,
3393 * struct inode *ip,
3394 * s64 newsize,
3395 * int type) {PWMAP, PMAP, WMAP; DELETE, TRUNCATE}
3397 * return:
3399 * note:
3400 * PWMAP:
3401 * 1. truncate (non-COMMIT_NOLINK file)
3402 * by jfs_truncate() or jfs_open(O_TRUNC):
3403 * xtree is updated;
3404 * 2. truncate index table of directory when last entry removed
3405 * map update via tlock at commit time;
3406 * PMAP:
3407 * Call xtTruncate_pmap instead
3408 * WMAP:
3409 * 1. remove (free zero link count) on last reference release
3410 * (pmap has been freed at commit zero link count);
3411 * 2. truncate (COMMIT_NOLINK file, i.e., tmp file):
3412 * xtree is updated;
3413 * map update directly at truncation time;
3415 * if (DELETE)
3416 * no LOG_NOREDOPAGE is required (NOREDOFILE is sufficient);
3417 * else if (TRUNCATE)
3418 * must write LOG_NOREDOPAGE for deleted index page;
3420 * pages may already have been tlocked by anonymous transactions
3421 * during file growth (i.e., write) before truncation;
3423 * except last truncated entry, deleted entries remains as is
3424 * in the page (nextindex is updated) for other use
3425 * (e.g., log/update allocation map): this avoid copying the page
3426 * info but delay free of pages;
3429 s64 xtTruncate(tid_t tid, struct inode *ip, s64 newsize, int flag)
3431 int rc = 0;
3432 s64 teof;
3433 struct metapage *mp;
3434 xtpage_t *p;
3435 s64 bn;
3436 int index, nextindex;
3437 xad_t *xad;
3438 s64 xoff, xaddr;
3439 int xlen, len, freexlen;
3440 struct btstack btstack;
3441 struct btframe *parent;
3442 struct tblock *tblk = NULL;
3443 struct tlock *tlck = NULL;
3444 struct xtlock *xtlck = NULL;
3445 struct xdlistlock xadlock; /* maplock for COMMIT_WMAP */
3446 struct pxd_lock *pxdlock; /* maplock for COMMIT_WMAP */
3447 s64 nfreed;
3448 int freed, log;
3449 int locked_leaves = 0;
3451 /* save object truncation type */
3452 if (tid) {
3453 tblk = tid_to_tblock(tid);
3454 tblk->xflag |= flag;
3457 nfreed = 0;
3459 flag &= COMMIT_MAP;
3460 assert(flag != COMMIT_PMAP);
3462 if (flag == COMMIT_PWMAP)
3463 log = 1;
3464 else {
3465 log = 0;
3466 xadlock.flag = mlckFREEXADLIST;
3467 xadlock.index = 1;
3471 * if the newsize is not an integral number of pages,
3472 * the file between newsize and next page boundary will
3473 * be cleared.
3474 * if truncating into a file hole, it will cause
3475 * a full block to be allocated for the logical block.
3479 * release page blocks of truncated region <teof, eof>
3481 * free the data blocks from the leaf index blocks.
3482 * delete the parent index entries corresponding to
3483 * the freed child data/index blocks.
3484 * free the index blocks themselves which aren't needed
3485 * in new sized file.
3487 * index blocks are updated only if the blocks are to be
3488 * retained in the new sized file.
3489 * if type is PMAP, the data and index pages are NOT
3490 * freed, and the data and index blocks are NOT freed
3491 * from working map.
3492 * (this will allow continued access of data/index of
3493 * temporary file (zerolink count file truncated to zero-length)).
3495 teof = (newsize + (JFS_SBI(ip->i_sb)->bsize - 1)) >>
3496 JFS_SBI(ip->i_sb)->l2bsize;
3498 /* clear stack */
3499 BT_CLR(&btstack);
3502 * start with root
3504 * root resides in the inode
3506 bn = 0;
3509 * first access of each page:
3511 getPage:
3512 XT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
3513 if (rc)
3514 return rc;
3516 /* process entries backward from last index */
3517 index = le16_to_cpu(p->header.nextindex) - 1;
3520 /* Since this is the rightmost page at this level, and we may have
3521 * already freed a page that was formerly to the right, let's make
3522 * sure that the next pointer is zero.
3524 if (p->header.next) {
3525 if (log)
3527 * Make sure this change to the header is logged.
3528 * If we really truncate this leaf, the flag
3529 * will be changed to tlckTRUNCATE
3531 tlck = txLock(tid, ip, mp, tlckXTREE|tlckGROW);
3532 BT_MARK_DIRTY(mp, ip);
3533 p->header.next = 0;
3536 if (p->header.flag & BT_INTERNAL)
3537 goto getChild;
3540 * leaf page
3542 freed = 0;
3544 /* does region covered by leaf page precede Teof ? */
3545 xad = &p->xad[index];
3546 xoff = offsetXAD(xad);
3547 xlen = lengthXAD(xad);
3548 if (teof >= xoff + xlen) {
3549 XT_PUTPAGE(mp);
3550 goto getParent;
3553 /* (re)acquire tlock of the leaf page */
3554 if (log) {
3555 if (++locked_leaves > MAX_TRUNCATE_LEAVES) {
3557 * We need to limit the size of the transaction
3558 * to avoid exhausting pagecache & tlocks
3560 XT_PUTPAGE(mp);
3561 newsize = (xoff + xlen) << JFS_SBI(ip->i_sb)->l2bsize;
3562 goto getParent;
3564 tlck = txLock(tid, ip, mp, tlckXTREE);
3565 tlck->type = tlckXTREE | tlckTRUNCATE;
3566 xtlck = (struct xtlock *) & tlck->lock;
3567 xtlck->hwm.offset = le16_to_cpu(p->header.nextindex) - 1;
3569 BT_MARK_DIRTY(mp, ip);
3572 * scan backward leaf page entries
3574 for (; index >= XTENTRYSTART; index--) {
3575 xad = &p->xad[index];
3576 xoff = offsetXAD(xad);
3577 xlen = lengthXAD(xad);
3578 xaddr = addressXAD(xad);
3581 * The "data" for a directory is indexed by the block
3582 * device's address space. This metadata must be invalidated
3583 * here
3585 if (S_ISDIR(ip->i_mode) && (teof == 0))
3586 invalidate_xad_metapages(ip, *xad);
3588 * entry beyond eof: continue scan of current page
3589 * xad
3590 * ---|---=======------->
3591 * eof
3593 if (teof < xoff) {
3594 nfreed += xlen;
3595 continue;
3599 * (xoff <= teof): last entry to be deleted from page;
3600 * If other entries remain in page: keep and update the page.
3604 * eof == entry_start: delete the entry
3605 * xad
3606 * -------|=======------->
3607 * eof
3610 if (teof == xoff) {
3611 nfreed += xlen;
3613 if (index == XTENTRYSTART)
3614 break;
3616 nextindex = index;
3619 * eof within the entry: truncate the entry.
3620 * xad
3621 * -------===|===------->
3622 * eof
3624 else if (teof < xoff + xlen) {
3625 /* update truncated entry */
3626 len = teof - xoff;
3627 freexlen = xlen - len;
3628 XADlength(xad, len);
3630 /* save pxd of truncated extent in tlck */
3631 xaddr += len;
3632 if (log) { /* COMMIT_PWMAP */
3633 xtlck->lwm.offset = (xtlck->lwm.offset) ?
3634 min(index, (int)xtlck->lwm.offset) : index;
3635 xtlck->lwm.length = index + 1 -
3636 xtlck->lwm.offset;
3637 xtlck->twm.offset = index;
3638 pxdlock = (struct pxd_lock *) & xtlck->pxdlock;
3639 pxdlock->flag = mlckFREEPXD;
3640 PXDaddress(&pxdlock->pxd, xaddr);
3641 PXDlength(&pxdlock->pxd, freexlen);
3643 /* free truncated extent */
3644 else { /* COMMIT_WMAP */
3646 pxdlock = (struct pxd_lock *) & xadlock;
3647 pxdlock->flag = mlckFREEPXD;
3648 PXDaddress(&pxdlock->pxd, xaddr);
3649 PXDlength(&pxdlock->pxd, freexlen);
3650 txFreeMap(ip, pxdlock, NULL, COMMIT_WMAP);
3652 /* reset map lock */
3653 xadlock.flag = mlckFREEXADLIST;
3656 /* current entry is new last entry; */
3657 nextindex = index + 1;
3659 nfreed += freexlen;
3662 * eof beyond the entry:
3663 * xad
3664 * -------=======---|--->
3665 * eof
3667 else { /* (xoff + xlen < teof) */
3669 nextindex = index + 1;
3672 if (nextindex < le16_to_cpu(p->header.nextindex)) {
3673 if (!log) { /* COMMIT_WAMP */
3674 xadlock.xdlist = &p->xad[nextindex];
3675 xadlock.count =
3676 le16_to_cpu(p->header.nextindex) -
3677 nextindex;
3678 txFreeMap(ip, (struct maplock *) & xadlock,
3679 NULL, COMMIT_WMAP);
3681 p->header.nextindex = cpu_to_le16(nextindex);
3684 XT_PUTPAGE(mp);
3686 /* assert(freed == 0); */
3687 goto getParent;
3688 } /* end scan of leaf page entries */
3690 freed = 1;
3693 * leaf page become empty: free the page if type != PMAP
3695 if (log) { /* COMMIT_PWMAP */
3696 /* txCommit() with tlckFREE:
3697 * free data extents covered by leaf [XTENTRYSTART:hwm);
3698 * invalidate leaf if COMMIT_PWMAP;
3699 * if (TRUNCATE), will write LOG_NOREDOPAGE;
3701 tlck->type = tlckXTREE | tlckFREE;
3702 } else { /* COMMIT_WAMP */
3704 /* free data extents covered by leaf */
3705 xadlock.xdlist = &p->xad[XTENTRYSTART];
3706 xadlock.count =
3707 le16_to_cpu(p->header.nextindex) - XTENTRYSTART;
3708 txFreeMap(ip, (struct maplock *) & xadlock, NULL, COMMIT_WMAP);
3711 if (p->header.flag & BT_ROOT) {
3712 p->header.flag &= ~BT_INTERNAL;
3713 p->header.flag |= BT_LEAF;
3714 p->header.nextindex = cpu_to_le16(XTENTRYSTART);
3716 XT_PUTPAGE(mp); /* debug */
3717 goto out;
3718 } else {
3719 if (log) { /* COMMIT_PWMAP */
3720 /* page will be invalidated at tx completion
3722 XT_PUTPAGE(mp);
3723 } else { /* COMMIT_WMAP */
3725 if (mp->lid)
3726 lid_to_tlock(mp->lid)->flag |= tlckFREELOCK;
3728 /* invalidate empty leaf page */
3729 discard_metapage(mp);
3734 * the leaf page become empty: delete the parent entry
3735 * for the leaf page if the parent page is to be kept
3736 * in the new sized file.
3740 * go back up to the parent page
3742 getParent:
3743 /* pop/restore parent entry for the current child page */
3744 if ((parent = BT_POP(&btstack)) == NULL)
3745 /* current page must have been root */
3746 goto out;
3748 /* get back the parent page */
3749 bn = parent->bn;
3750 XT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
3751 if (rc)
3752 return rc;
3754 index = parent->index;
3757 * child page was not empty:
3759 if (freed == 0) {
3760 /* has any entry deleted from parent ? */
3761 if (index < le16_to_cpu(p->header.nextindex) - 1) {
3762 /* (re)acquire tlock on the parent page */
3763 if (log) { /* COMMIT_PWMAP */
3764 /* txCommit() with tlckTRUNCATE:
3765 * free child extents covered by parent [);
3767 tlck = txLock(tid, ip, mp, tlckXTREE);
3768 xtlck = (struct xtlock *) & tlck->lock;
3769 if (!(tlck->type & tlckTRUNCATE)) {
3770 xtlck->hwm.offset =
3771 le16_to_cpu(p->header.
3772 nextindex) - 1;
3773 tlck->type =
3774 tlckXTREE | tlckTRUNCATE;
3776 } else { /* COMMIT_WMAP */
3778 /* free child extents covered by parent */
3779 xadlock.xdlist = &p->xad[index + 1];
3780 xadlock.count =
3781 le16_to_cpu(p->header.nextindex) -
3782 index - 1;
3783 txFreeMap(ip, (struct maplock *) & xadlock,
3784 NULL, COMMIT_WMAP);
3786 BT_MARK_DIRTY(mp, ip);
3788 p->header.nextindex = cpu_to_le16(index + 1);
3790 XT_PUTPAGE(mp);
3791 goto getParent;
3795 * child page was empty:
3797 nfreed += lengthXAD(&p->xad[index]);
3800 * During working map update, child page's tlock must be handled
3801 * before parent's. This is because the parent's tlock will cause
3802 * the child's disk space to be marked available in the wmap, so
3803 * it's important that the child page be released by that time.
3805 * ToDo: tlocks should be on doubly-linked list, so we can
3806 * quickly remove it and add it to the end.
3810 * Move parent page's tlock to the end of the tid's tlock list
3812 if (log && mp->lid && (tblk->last != mp->lid) &&
3813 lid_to_tlock(mp->lid)->tid) {
3814 lid_t lid = mp->lid;
3815 struct tlock *prev;
3817 tlck = lid_to_tlock(lid);
3819 if (tblk->next == lid)
3820 tblk->next = tlck->next;
3821 else {
3822 for (prev = lid_to_tlock(tblk->next);
3823 prev->next != lid;
3824 prev = lid_to_tlock(prev->next)) {
3825 assert(prev->next);
3827 prev->next = tlck->next;
3829 lid_to_tlock(tblk->last)->next = lid;
3830 tlck->next = 0;
3831 tblk->last = lid;
3835 * parent page become empty: free the page
3837 if (index == XTENTRYSTART) {
3838 if (log) { /* COMMIT_PWMAP */
3839 /* txCommit() with tlckFREE:
3840 * free child extents covered by parent;
3841 * invalidate parent if COMMIT_PWMAP;
3843 tlck = txLock(tid, ip, mp, tlckXTREE);
3844 xtlck = (struct xtlock *) & tlck->lock;
3845 xtlck->hwm.offset =
3846 le16_to_cpu(p->header.nextindex) - 1;
3847 tlck->type = tlckXTREE | tlckFREE;
3848 } else { /* COMMIT_WMAP */
3850 /* free child extents covered by parent */
3851 xadlock.xdlist = &p->xad[XTENTRYSTART];
3852 xadlock.count =
3853 le16_to_cpu(p->header.nextindex) -
3854 XTENTRYSTART;
3855 txFreeMap(ip, (struct maplock *) & xadlock, NULL,
3856 COMMIT_WMAP);
3858 BT_MARK_DIRTY(mp, ip);
3860 if (p->header.flag & BT_ROOT) {
3861 p->header.flag &= ~BT_INTERNAL;
3862 p->header.flag |= BT_LEAF;
3863 p->header.nextindex = cpu_to_le16(XTENTRYSTART);
3864 if (le16_to_cpu(p->header.maxentry) == XTROOTMAXSLOT) {
3866 * Shrink root down to allow inline
3867 * EA (otherwise fsck complains)
3869 p->header.maxentry =
3870 cpu_to_le16(XTROOTINITSLOT);
3871 JFS_IP(ip)->mode2 |= INLINEEA;
3874 XT_PUTPAGE(mp); /* debug */
3875 goto out;
3876 } else {
3877 if (log) { /* COMMIT_PWMAP */
3878 /* page will be invalidated at tx completion
3880 XT_PUTPAGE(mp);
3881 } else { /* COMMIT_WMAP */
3883 if (mp->lid)
3884 lid_to_tlock(mp->lid)->flag |=
3885 tlckFREELOCK;
3887 /* invalidate parent page */
3888 discard_metapage(mp);
3891 /* parent has become empty and freed:
3892 * go back up to its parent page
3894 /* freed = 1; */
3895 goto getParent;
3899 * parent page still has entries for front region;
3901 else {
3902 /* try truncate region covered by preceding entry
3903 * (process backward)
3905 index--;
3907 /* go back down to the child page corresponding
3908 * to the entry
3910 goto getChild;
3914 * internal page: go down to child page of current entry
3916 getChild:
3917 /* save current parent entry for the child page */
3918 BT_PUSH(&btstack, bn, index);
3920 /* get child page */
3921 xad = &p->xad[index];
3922 bn = addressXAD(xad);
3925 * first access of each internal entry:
3927 /* release parent page */
3928 XT_PUTPAGE(mp);
3930 /* process the child page */
3931 goto getPage;
3933 out:
3935 * update file resource stat
3937 /* set size
3939 if (S_ISDIR(ip->i_mode) && !newsize)
3940 ip->i_size = 1; /* fsck hates zero-length directories */
3941 else
3942 ip->i_size = newsize;
3944 /* update quota allocation to reflect freed blocks */
3945 DQUOT_FREE_BLOCK(ip, nfreed);
3948 * free tlock of invalidated pages
3950 if (flag == COMMIT_WMAP)
3951 txFreelock(ip);
3953 return newsize;
3958 * xtTruncate_pmap()
3960 * function:
3961 * Perform truncate to zero lenghth for deleted file, leaving the
3962 * the xtree and working map untouched. This allows the file to
3963 * be accessed via open file handles, while the delete of the file
3964 * is committed to disk.
3966 * parameter:
3967 * tid_t tid,
3968 * struct inode *ip,
3969 * s64 committed_size)
3971 * return: new committed size
3973 * note:
3975 * To avoid deadlock by holding too many transaction locks, the
3976 * truncation may be broken up into multiple transactions.
3977 * The committed_size keeps track of part of the file has been
3978 * freed from the pmaps.
3980 s64 xtTruncate_pmap(tid_t tid, struct inode *ip, s64 committed_size)
3982 s64 bn;
3983 struct btstack btstack;
3984 int cmp;
3985 int index;
3986 int locked_leaves = 0;
3987 struct metapage *mp;
3988 xtpage_t *p;
3989 struct btframe *parent;
3990 int rc;
3991 struct tblock *tblk;
3992 struct tlock *tlck = NULL;
3993 xad_t *xad;
3994 int xlen;
3995 s64 xoff;
3996 struct xtlock *xtlck = NULL;
3998 /* save object truncation type */
3999 tblk = tid_to_tblock(tid);
4000 tblk->xflag |= COMMIT_PMAP;
4002 /* clear stack */
4003 BT_CLR(&btstack);
4005 if (committed_size) {
4006 xoff = (committed_size >> JFS_SBI(ip->i_sb)->l2bsize) - 1;
4007 rc = xtSearch(ip, xoff, NULL, &cmp, &btstack, 0);
4008 if (rc)
4009 return rc;
4011 XT_GETSEARCH(ip, btstack.top, bn, mp, p, index);
4013 if (cmp != 0) {
4014 XT_PUTPAGE(mp);
4015 jfs_error(ip->i_sb,
4016 "xtTruncate_pmap: did not find extent");
4017 return -EIO;
4019 } else {
4021 * start with root
4023 * root resides in the inode
4025 bn = 0;
4028 * first access of each page:
4030 getPage:
4031 XT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
4032 if (rc)
4033 return rc;
4035 /* process entries backward from last index */
4036 index = le16_to_cpu(p->header.nextindex) - 1;
4038 if (p->header.flag & BT_INTERNAL)
4039 goto getChild;
4043 * leaf page
4046 if (++locked_leaves > MAX_TRUNCATE_LEAVES) {
4048 * We need to limit the size of the transaction
4049 * to avoid exhausting pagecache & tlocks
4051 xad = &p->xad[index];
4052 xoff = offsetXAD(xad);
4053 xlen = lengthXAD(xad);
4054 XT_PUTPAGE(mp);
4055 return (xoff + xlen) << JFS_SBI(ip->i_sb)->l2bsize;
4057 tlck = txLock(tid, ip, mp, tlckXTREE);
4058 tlck->type = tlckXTREE | tlckFREE;
4059 xtlck = (struct xtlock *) & tlck->lock;
4060 xtlck->hwm.offset = index;
4063 XT_PUTPAGE(mp);
4066 * go back up to the parent page
4068 getParent:
4069 /* pop/restore parent entry for the current child page */
4070 if ((parent = BT_POP(&btstack)) == NULL)
4071 /* current page must have been root */
4072 goto out;
4074 /* get back the parent page */
4075 bn = parent->bn;
4076 XT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
4077 if (rc)
4078 return rc;
4080 index = parent->index;
4083 * parent page become empty: free the page
4085 if (index == XTENTRYSTART) {
4086 /* txCommit() with tlckFREE:
4087 * free child extents covered by parent;
4088 * invalidate parent if COMMIT_PWMAP;
4090 tlck = txLock(tid, ip, mp, tlckXTREE);
4091 xtlck = (struct xtlock *) & tlck->lock;
4092 xtlck->hwm.offset =
4093 le16_to_cpu(p->header.nextindex) - 1;
4094 tlck->type = tlckXTREE | tlckFREE;
4096 XT_PUTPAGE(mp);
4098 if (p->header.flag & BT_ROOT) {
4100 goto out;
4101 } else {
4102 goto getParent;
4106 * parent page still has entries for front region;
4108 else
4109 index--;
4111 * internal page: go down to child page of current entry
4113 getChild:
4114 /* save current parent entry for the child page */
4115 BT_PUSH(&btstack, bn, index);
4117 /* get child page */
4118 xad = &p->xad[index];
4119 bn = addressXAD(xad);
4122 * first access of each internal entry:
4124 /* release parent page */
4125 XT_PUTPAGE(mp);
4127 /* process the child page */
4128 goto getPage;
4130 out:
4132 return 0;
4135 #ifdef CONFIG_JFS_STATISTICS
4136 int jfs_xtstat_read(char *buffer, char **start, off_t offset, int length,
4137 int *eof, void *data)
4139 int len = 0;
4140 off_t begin;
4142 len += sprintf(buffer,
4143 "JFS Xtree statistics\n"
4144 "====================\n"
4145 "searches = %d\n"
4146 "fast searches = %d\n"
4147 "splits = %d\n",
4148 xtStat.search,
4149 xtStat.fastSearch,
4150 xtStat.split);
4152 begin = offset;
4153 *start = buffer + begin;
4154 len -= begin;
4156 if (len > length)
4157 len = length;
4158 else
4159 *eof = 1;
4161 if (len < 0)
4162 len = 0;
4164 return len;
4166 #endif