2 * Copyright (C) International Business Machines Corp., 2000-2005
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
12 * the GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 * jfs_xtree.c: extent allocation descriptor B+-tree manager
23 #include <linux/quotaops.h>
24 #include "jfs_incore.h"
25 #include "jfs_filsys.h"
26 #include "jfs_metapage.h"
28 #include "jfs_dinode.h"
29 #include "jfs_superblock.h"
30 #include "jfs_debug.h"
35 #define XT_INSERT 0x00000001
38 * xtree key/entry comparison: extent offset
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)\
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");\
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 */
95 struct pxdlist *pxdlist;
102 #ifdef CONFIG_JFS_STATISTICS
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,
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,
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 */
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)
147 struct btstack btstack;
154 s64 next, size, xoff, xend;
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);
173 * search for the xad entry covering the logical extent
176 if ((rc = xtSearch(ip, lstart, &next, &cmp, &btstack, 0))) {
177 jfs_err("xtLookup: xtSearch returned %d", rc);
182 * compute the physical extent covering logical extent
184 * N.B. search may have failed (e.g., hole in sparse file),
185 * and returned the index of the next entry.
187 /* retrieve search result */
188 XT_GETSEARCH(ip, btstack.top, bn, mp, p, index);
190 /* is xad found covering start of logical extent ?
191 * lstart is a page start address,
192 * i.e., lstart cannot start in a hole;
196 *plen = min(next - lstart, llen);
203 xad = &p->xad[index];
204 xoff = offsetXAD(xad);
205 xlen = lengthXAD(xad);
207 xaddr = addressXAD(xad);
209 /* initialize new pxd */
211 *paddr = xaddr + (lstart - xoff);
212 /* a page must be fully covered by an xad */
213 *plen = min(xend - lstart, llen);
225 * function: map a single logical extent into a list of physical extent;
229 * struct lxdlist *lxdlist, lxd list (in)
230 * struct xadlist *xadlist, xad list (in/out)
233 * coverage of lxd by xad under assumption of
234 * . lxd's are ordered and disjoint.
235 * . xad's are ordered and disjoint.
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)
249 struct btstack btstack;
257 s64 size, lstart, lend, xstart, xend, pstart;
258 s64 llen, xlen, plen;
260 int nlxd, npxd, maxnpxd;
262 npxd = xadlist->nxad = 0;
263 maxnpxd = xadlist->maxnxad;
266 nlxd = lxdlist->nlxd;
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
283 if ((rc = xtSearch(ip, lstart, NULL, &cmp, &btstack, 0)))
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.
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)
301 if ((bn = le64_to_cpu(p->header.next)) == 0)
306 /* get next sibling page */
307 XT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
311 index = XTENTRYSTART;
314 xad = &p->xad[index];
317 * is lxd covered by xad ?
320 xstart = offsetXAD(xad);
321 xlen = lengthXAD(xad);
322 xend = xstart + xlen;
323 xaddr = addressXAD(xad);
329 /* (lstart <= xstart) */
331 /* lxd is NOT covered by xad */
332 if (lend <= xstart) {
340 lstart = offsetLXD(lxd);
341 llen = lengthLXD(lxd);
342 lend = lstart + llen;
346 /* compare with the current xad */
349 /* lxd is covered by xad */
350 else { /* (xstart < lend) */
352 /* initialize new pxd */
354 plen = min(lend - xstart, xlen);
360 /* (xstart < lstart) */
362 /* lxd is covered by xad */
364 /* initialize new pxd */
366 plen = min(xend - lstart, llen);
367 paddr = xaddr + (lstart - xstart);
371 /* lxd is NOT covered by xad */
372 else { /* (xend <= lstart) */
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)
390 /* compare with new xad */
396 * lxd is covered by xad and a new pxd has been initialized
397 * (lstart <= xstart < lend) or (xstart < lstart < xend)
400 /* finalize pxd corresponding to current xad */
401 XT_PUTENTRY(pxd, xad->flag, pstart, plen, paddr);
403 if (++npxd >= maxnpxd)
408 * lxd is fully covered by xad
418 lstart = offsetLXD(lxd);
419 llen = lengthLXD(lxd);
420 lend = lstart + llen;
425 * test for old xad covering new lxd
426 * (old xstart < new lstart)
431 * lxd is partially covered by xad
433 else { /* (xend < lend) */
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)
445 if ((bn = le64_to_cpu(p->header.next)) == 0)
450 /* get next sibling page */
451 XT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
455 index = XTENTRYSTART;
456 xad = &p->xad[index];
463 * test for new xad covering old lxd
464 * (old lstart < new xstart)
470 xadlist->nxad = npxd;
482 * function: search for the xad entry covering specified offset.
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);
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);
502 int cmp = 1; /* init for empty page */
503 s64 bn; /* block number */
504 struct metapage *mp; /* page buffer */
505 xtpage_t *p; /* page */
507 int base, index, lim, btindex;
508 struct btframe *btsp;
509 int nsplit = 0; /* number of pages to split */
513 INCREMENT(xtStat.search);
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.
536 /* get/pin the page to search */
537 XT_GETPAGE(ip, bn, mp, PSIZE, p, 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;
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)) {
560 /* stop sequential access heuristics */
562 } else { /* (t64 + lengthXAD(xad)) <= xoff */
564 /* try next sequential entry */
567 le16_to_cpu(p->header.nextindex)) {
569 t64 = offsetXAD(xad);
570 if (xoff < t64 + lengthXAD(xad)) {
576 /* miss: key falls between
577 * previous and this entry
584 /* (xoff >= t64 + lengthXAD(xad));
585 * matching entry may be further out:
586 * stop heuristic search
588 /* stop sequential access heuristics */
592 /* (index == p->header.nextindex);
593 * miss: key entry does not exist in
594 * the target leaf/tree
601 * if hit, return index of the entry found, and
602 * if miss, where new entry with search key is
606 /* compute number of pages to split */
607 if (flag & XT_INSERT) {
608 if (p->header.nextindex == /* little-endian */
613 btstack->nsplit = nsplit;
616 /* save search result */
622 /* update sequential access heuristics */
623 jfs_ip->btindex = index;
628 INCREMENT(xtStat.fastSearch);
632 /* well, ... full search now */
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);
647 /* search hit - leaf page:
648 * return the entry found
650 if (p->header.flag & BT_LEAF) {
653 /* compute number of pages to split */
654 if (flag & XT_INSERT) {
655 if (p->header.nextindex ==
660 btstack->nsplit = nsplit;
663 /* save search result */
669 /* init sequential access heuristics */
670 btindex = jfs_ip->btindex;
671 if (index == btindex ||
672 index == btindex + 1)
673 jfs_ip->btorder = BT_SEQUENTIAL;
675 jfs_ip->btorder = BT_RANDOM;
676 jfs_ip->btindex = index;
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]);
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) {
711 /* compute number of pages to split */
712 if (flag & XT_INSERT) {
713 if (p->header.nextindex ==
718 btstack->nsplit = nsplit;
721 /* save search result */
727 /* init sequential access heuristics */
728 btindex = jfs_ip->btindex;
729 if (base == btindex || base == btindex + 1)
730 jfs_ip->btorder = BT_SEQUENTIAL;
732 jfs_ip->btorder = BT_RANDOM;
733 jfs_ip->btindex = base;
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
753 /* update number of pages to split */
754 if (p->header.nextindex == p->header.maxentry)
759 /* push (bn, index) of the parent page/entry */
760 if (BT_STACK_FULL(btstack)) {
761 jfs_error(ip->i_sb, "stack overrun in xtSearch!");
765 BT_PUSH(btstack, bn, index);
767 /* get the child page block number */
768 bn = addressXAD(&p->xad[index]);
770 /* unpin the parent page */
781 * tid - transaction id;
783 * xflag - extent flag (XAD_NOTRECORDED):
784 * xoff - extent offset;
785 * xlen - extent length;
786 * xaddrp - extent address pointer (in/out):
788 * caller allocated data extent at *xaddrp;
790 * allocate data extent and return its xaddr;
795 int xtInsert(tid_t tid, /* transaction id */
796 struct inode *ip, int xflag, s64 xoff, s32 xlen, s64 * xaddrp,
801 struct metapage *mp; /* meta-page buffer */
802 xtpage_t *p; /* base B+-tree index page */
804 int index, nextindex;
805 struct btstack btstack; /* traverse stack */
806 struct xtsplit split; /* split information */
811 struct xtlock *xtlck;
813 jfs_info("xtInsert: nxoff:0x%lx nxlen:0x%x", (ulong) xoff, xlen);
816 * search for the entry location at which to insert:
818 * xtFastSearch() and xtSearch() both returns (leaf page
819 * pinned, index at which to insert).
820 * n.b. xtSearch() may return index of maxentry of
823 if ((rc = xtSearch(ip, xoff, &next, &cmp, &btstack, XT_INSERT)))
826 /* retrieve search result */
827 XT_GETSEARCH(ip, btstack.top, bn, mp, p, index);
829 /* This test must follow XT_GETSEARCH since mp must be valid if
830 * we branch to out: */
831 if ((cmp == 0) || (next && (xlen > next - xoff))) {
837 * allocate data extent requested
839 * allocation hint: last xad
841 if ((xaddr = *xaddrp) == 0) {
842 if (index > XTENTRYSTART) {
843 xad = &p->xad[index - 1];
844 hint = addressXAD(xad) + lengthXAD(xad) - 1;
847 if ((rc = DQUOT_ALLOC_BLOCK(ip, xlen)))
849 if ((rc = dbAlloc(ip, hint, (s64) xlen, &xaddr))) {
850 DQUOT_FREE_BLOCK(ip, xlen);
856 * insert entry for new extent
861 * if the leaf page is full, split the page and
862 * propagate up the router entry for the new page from split
864 * The xtSplitUp() will insert the entry and unpin the leaf page.
866 nextindex = le16_to_cpu(p->header.nextindex);
867 if (nextindex == le16_to_cpu(p->header.maxentry)) {
874 split.pxdlist = NULL;
875 if ((rc = xtSplitUp(tid, ip, &split, &btstack))) {
876 /* undo data extent allocation */
878 dbFree(ip, xaddr, (s64) xlen);
879 DQUOT_FREE_BLOCK(ip, xlen);
889 * insert the new entry into the leaf page
892 * acquire a transaction lock on the leaf page;
894 * action: xad insertion/extension;
896 BT_MARK_DIRTY(mp, ip);
898 /* if insert into middle, shift right remaining entries. */
899 if (index < nextindex)
900 memmove(&p->xad[index + 1], &p->xad[index],
901 (nextindex - index) * sizeof(xad_t));
903 /* insert the new entry: mark the entry NEW */
904 xad = &p->xad[index];
905 XT_PUTENTRY(xad, xflag, xoff, xlen, xaddr);
907 /* advance next available entry index */
908 le16_add_cpu(&p->header.nextindex, 1);
910 /* Don't log it if there are no links to the file */
911 if (!test_cflag(COMMIT_Nolink, ip)) {
912 tlck = txLock(tid, ip, mp, tlckXTREE | tlckGROW);
913 xtlck = (struct xtlock *) & tlck->lock;
915 (xtlck->lwm.offset) ? min(index,
916 (int)xtlck->lwm.offset) : index;
918 le16_to_cpu(p->header.nextindex) - xtlck->lwm.offset;
924 /* unpin the leaf page */
935 * split full pages as propagating insertion up the tree
938 * tid - transaction id;
940 * split - entry parameter descriptor;
941 * btstack - traverse stack from xtSearch()
947 struct inode *ip, struct xtsplit * split, struct btstack * btstack)
950 struct metapage *smp;
951 xtpage_t *sp; /* split page */
952 struct metapage *rmp;
953 s64 rbn; /* new right page block number */
954 struct metapage *rcmp;
955 xtpage_t *rcp; /* right child page */
956 s64 rcbn; /* right child page block number */
957 int skip; /* index of entry of insertion */
958 int nextindex; /* next available entry index of p */
959 struct btframe *parent; /* parent page entry on traverse stack */
963 int nsplit; /* number of pages split */
964 struct pxdlist pxdlist;
967 struct xtlock *xtlck;
970 sp = XT_PAGE(ip, smp);
972 /* is inode xtree root extension/inline EA area free ? */
973 if ((sp->header.flag & BT_ROOT) && (!S_ISDIR(ip->i_mode)) &&
974 (le16_to_cpu(sp->header.maxentry) < XTROOTMAXSLOT) &&
975 (JFS_IP(ip)->mode2 & INLINEEA)) {
976 sp->header.maxentry = cpu_to_le16(XTROOTMAXSLOT);
977 JFS_IP(ip)->mode2 &= ~INLINEEA;
979 BT_MARK_DIRTY(smp, ip);
981 * acquire a transaction lock on the leaf page;
983 * action: xad insertion/extension;
986 /* if insert into middle, shift right remaining entries. */
988 nextindex = le16_to_cpu(sp->header.nextindex);
989 if (skip < nextindex)
990 memmove(&sp->xad[skip + 1], &sp->xad[skip],
991 (nextindex - skip) * sizeof(xad_t));
993 /* insert the new entry: mark the entry NEW */
994 xad = &sp->xad[skip];
995 XT_PUTENTRY(xad, split->flag, split->off, split->len,
998 /* advance next available entry index */
999 le16_add_cpu(&sp->header.nextindex, 1);
1001 /* Don't log it if there are no links to the file */
1002 if (!test_cflag(COMMIT_Nolink, ip)) {
1003 tlck = txLock(tid, ip, smp, tlckXTREE | tlckGROW);
1004 xtlck = (struct xtlock *) & tlck->lock;
1005 xtlck->lwm.offset = (xtlck->lwm.offset) ?
1006 min(skip, (int)xtlck->lwm.offset) : skip;
1008 le16_to_cpu(sp->header.nextindex) -
1016 * allocate new index blocks to cover index page split(s)
1018 * allocation hint: ?
1020 if (split->pxdlist == NULL) {
1021 nsplit = btstack->nsplit;
1022 split->pxdlist = &pxdlist;
1023 pxdlist.maxnpxd = pxdlist.npxd = 0;
1024 pxd = &pxdlist.pxd[0];
1025 xlen = JFS_SBI(ip->i_sb)->nbperpage;
1026 for (; nsplit > 0; nsplit--, pxd++) {
1027 if ((rc = dbAlloc(ip, (s64) 0, (s64) xlen, &xaddr))
1029 PXDaddress(pxd, xaddr);
1030 PXDlength(pxd, xlen);
1037 /* undo allocation */
1045 * Split leaf page <sp> into <sp> and a new right page <rp>.
1047 * The split routines insert the new entry into the leaf page,
1048 * and acquire txLock as appropriate.
1049 * return <rp> pinned and its block number <rpbn>.
1051 rc = (sp->header.flag & BT_ROOT) ?
1052 xtSplitRoot(tid, ip, split, &rmp) :
1053 xtSplitPage(tid, ip, split, &rmp, &rbn);
1060 * propagate up the router entry for the leaf page just split
1062 * insert a router entry for the new page into the parent page,
1063 * propagate the insert/split up the tree by walking back the stack
1064 * of (bn of parent page, index of child page entry in parent page)
1065 * that were traversed during the search for the page that split.
1067 * the propagation of insert/split up the tree stops if the root
1068 * splits or the page inserted into doesn't have to split to hold
1071 * the parent entry for the split page remains the same, and
1072 * a new entry is inserted at its right with the first key and
1073 * block number of the new right page.
1075 * There are a maximum of 3 pages pinned at any time:
1076 * right child, left parent and right parent (when the parent splits)
1077 * to keep the child page pinned while working on the parent.
1078 * make sure that all pins are released at exit.
1080 while ((parent = BT_POP(btstack)) != NULL) {
1081 /* parent page specified by stack frame <parent> */
1083 /* keep current child pages <rcp> pinned */
1086 rcp = XT_PAGE(ip, rcmp);
1089 * insert router entry in parent for new right child page <rp>
1091 /* get/pin the parent page <sp> */
1092 XT_GETPAGE(ip, parent->bn, smp, PSIZE, sp, rc);
1099 * The new key entry goes ONE AFTER the index of parent entry,
1100 * because the split was to the right.
1102 skip = parent->index + 1;
1105 * split or shift right remaining entries of the parent page
1107 nextindex = le16_to_cpu(sp->header.nextindex);
1109 * parent page is full - split the parent page
1111 if (nextindex == le16_to_cpu(sp->header.maxentry)) {
1112 /* init for parent page split */
1114 split->index = skip; /* index at insert */
1115 split->flag = XAD_NEW;
1116 split->off = offsetXAD(&rcp->xad[XTENTRYSTART]);
1117 split->len = JFS_SBI(ip->i_sb)->nbperpage;
1120 /* unpin previous right child page */
1123 /* The split routines insert the new entry,
1124 * and acquire txLock as appropriate.
1125 * return <rp> pinned and its block number <rpbn>.
1127 rc = (sp->header.flag & BT_ROOT) ?
1128 xtSplitRoot(tid, ip, split, &rmp) :
1129 xtSplitPage(tid, ip, split, &rmp, &rbn);
1136 /* keep new child page <rp> pinned */
1139 * parent page is not full - insert in parent page
1143 * insert router entry in parent for the right child
1144 * page from the first entry of the right child page:
1147 * acquire a transaction lock on the parent page;
1149 * action: router xad insertion;
1151 BT_MARK_DIRTY(smp, ip);
1154 * if insert into middle, shift right remaining entries
1156 if (skip < nextindex)
1157 memmove(&sp->xad[skip + 1], &sp->xad[skip],
1159 skip) << L2XTSLOTSIZE);
1161 /* insert the router entry */
1162 xad = &sp->xad[skip];
1163 XT_PUTENTRY(xad, XAD_NEW,
1164 offsetXAD(&rcp->xad[XTENTRYSTART]),
1165 JFS_SBI(ip->i_sb)->nbperpage, rcbn);
1167 /* advance next available entry index. */
1168 le16_add_cpu(&sp->header.nextindex, 1);
1170 /* Don't log it if there are no links to the file */
1171 if (!test_cflag(COMMIT_Nolink, ip)) {
1172 tlck = txLock(tid, ip, smp,
1173 tlckXTREE | tlckGROW);
1174 xtlck = (struct xtlock *) & tlck->lock;
1175 xtlck->lwm.offset = (xtlck->lwm.offset) ?
1176 min(skip, (int)xtlck->lwm.offset) : skip;
1178 le16_to_cpu(sp->header.nextindex) -
1182 /* unpin parent page */
1185 /* exit propagate up */
1190 /* unpin current right page */
1201 * split a full non-root page into
1202 * original/split/left page and new right page
1203 * i.e., the original/split page remains as left page.
1208 * struct xtsplit *split,
1209 * struct metapage **rmpp,
1213 * Pointer to page in which to insert or NULL on error.
1216 xtSplitPage(tid_t tid, struct inode *ip,
1217 struct xtsplit * split, struct metapage ** rmpp, s64 * rbnp)
1220 struct metapage *smp;
1222 struct metapage *rmp;
1223 xtpage_t *rp; /* new right page allocated */
1224 s64 rbn; /* new right page block number */
1225 struct metapage *mp;
1228 int skip, maxentry, middle, righthalf, n;
1230 struct pxdlist *pxdlist;
1233 struct xtlock *sxtlck = NULL, *rxtlck = NULL;
1234 int quota_allocation = 0;
1237 sp = XT_PAGE(ip, smp);
1239 INCREMENT(xtStat.split);
1241 pxdlist = split->pxdlist;
1242 pxd = &pxdlist->pxd[pxdlist->npxd];
1244 rbn = addressPXD(pxd);
1246 /* Allocate blocks to quota. */
1247 if (DQUOT_ALLOC_BLOCK(ip, lengthPXD(pxd))) {
1252 quota_allocation += lengthPXD(pxd);
1255 * allocate the new right page for the split
1257 rmp = get_metapage(ip, rbn, PSIZE, 1);
1263 jfs_info("xtSplitPage: ip:0x%p smp:0x%p rmp:0x%p", ip, smp, rmp);
1265 BT_MARK_DIRTY(rmp, ip);
1270 rp = (xtpage_t *) rmp->data;
1271 rp->header.self = *pxd;
1272 rp->header.flag = sp->header.flag & BT_TYPE;
1273 rp->header.maxentry = sp->header.maxentry; /* little-endian */
1274 rp->header.nextindex = cpu_to_le16(XTENTRYSTART);
1276 BT_MARK_DIRTY(smp, ip);
1277 /* Don't log it if there are no links to the file */
1278 if (!test_cflag(COMMIT_Nolink, ip)) {
1280 * acquire a transaction lock on the new right page;
1282 tlck = txLock(tid, ip, rmp, tlckXTREE | tlckNEW);
1283 rxtlck = (struct xtlock *) & tlck->lock;
1284 rxtlck->lwm.offset = XTENTRYSTART;
1286 * acquire a transaction lock on the split page
1288 tlck = txLock(tid, ip, smp, tlckXTREE | tlckGROW);
1289 sxtlck = (struct xtlock *) & tlck->lock;
1293 * initialize/update sibling pointers of <sp> and <rp>
1295 nextbn = le64_to_cpu(sp->header.next);
1296 rp->header.next = cpu_to_le64(nextbn);
1297 rp->header.prev = cpu_to_le64(addressPXD(&sp->header.self));
1298 sp->header.next = cpu_to_le64(rbn);
1300 skip = split->index;
1303 * sequential append at tail (after last entry of last page)
1305 * if splitting the last page on a level because of appending
1306 * a entry to it (skip is maxentry), it's likely that the access is
1307 * sequential. adding an empty page on the side of the level is less
1308 * work and can push the fill factor much higher than normal.
1309 * if we're wrong it's no big deal - we will do the split the right
1311 * (it may look like it's equally easy to do a similar hack for
1312 * reverse sorted data, that is, split the tree left, but it's not.
1315 if (nextbn == 0 && skip == le16_to_cpu(sp->header.maxentry)) {
1317 * acquire a transaction lock on the new/right page;
1319 * action: xad insertion;
1321 /* insert entry at the first entry of the new right page */
1322 xad = &rp->xad[XTENTRYSTART];
1323 XT_PUTENTRY(xad, split->flag, split->off, split->len,
1326 rp->header.nextindex = cpu_to_le16(XTENTRYSTART + 1);
1328 if (!test_cflag(COMMIT_Nolink, ip)) {
1329 /* rxtlck->lwm.offset = XTENTRYSTART; */
1330 rxtlck->lwm.length = 1;
1336 jfs_info("xtSplitPage: sp:0x%p rp:0x%p", sp, rp);
1341 * non-sequential insert (at possibly middle page)
1345 * update previous pointer of old next/right page of <sp>
1348 XT_GETPAGE(ip, nextbn, mp, PSIZE, p, rc);
1354 BT_MARK_DIRTY(mp, ip);
1356 * acquire a transaction lock on the next page;
1358 * action:sibling pointer update;
1360 if (!test_cflag(COMMIT_Nolink, ip))
1361 tlck = txLock(tid, ip, mp, tlckXTREE | tlckRELINK);
1363 p->header.prev = cpu_to_le64(rbn);
1365 /* sibling page may have been updated previously, or
1366 * it may be updated later;
1373 * split the data between the split and new/right pages
1375 maxentry = le16_to_cpu(sp->header.maxentry);
1376 middle = maxentry >> 1;
1377 righthalf = maxentry - middle;
1380 * skip index in old split/left page - insert into left page:
1382 if (skip <= middle) {
1383 /* move right half of split page to the new right page */
1384 memmove(&rp->xad[XTENTRYSTART], &sp->xad[middle],
1385 righthalf << L2XTSLOTSIZE);
1387 /* shift right tail of left half to make room for new entry */
1389 memmove(&sp->xad[skip + 1], &sp->xad[skip],
1390 (middle - skip) << L2XTSLOTSIZE);
1392 /* insert new entry */
1393 xad = &sp->xad[skip];
1394 XT_PUTENTRY(xad, split->flag, split->off, split->len,
1397 /* update page header */
1398 sp->header.nextindex = cpu_to_le16(middle + 1);
1399 if (!test_cflag(COMMIT_Nolink, ip)) {
1400 sxtlck->lwm.offset = (sxtlck->lwm.offset) ?
1401 min(skip, (int)sxtlck->lwm.offset) : skip;
1404 rp->header.nextindex =
1405 cpu_to_le16(XTENTRYSTART + righthalf);
1408 * skip index in new right page - insert into right page:
1411 /* move left head of right half to right page */
1413 memmove(&rp->xad[XTENTRYSTART], &sp->xad[middle],
1416 /* insert new entry */
1419 XT_PUTENTRY(xad, split->flag, split->off, split->len,
1422 /* move right tail of right half to right page */
1423 if (skip < maxentry)
1424 memmove(&rp->xad[n + 1], &sp->xad[skip],
1425 (maxentry - skip) << L2XTSLOTSIZE);
1427 /* update page header */
1428 sp->header.nextindex = cpu_to_le16(middle);
1429 if (!test_cflag(COMMIT_Nolink, ip)) {
1430 sxtlck->lwm.offset = (sxtlck->lwm.offset) ?
1431 min(middle, (int)sxtlck->lwm.offset) : middle;
1434 rp->header.nextindex = cpu_to_le16(XTENTRYSTART +
1438 if (!test_cflag(COMMIT_Nolink, ip)) {
1439 sxtlck->lwm.length = le16_to_cpu(sp->header.nextindex) -
1442 /* rxtlck->lwm.offset = XTENTRYSTART; */
1443 rxtlck->lwm.length = le16_to_cpu(rp->header.nextindex) -
1450 jfs_info("xtSplitPage: sp:0x%p rp:0x%p", sp, rp);
1455 /* Rollback quota allocation. */
1456 if (quota_allocation)
1457 DQUOT_FREE_BLOCK(ip, quota_allocation);
1467 * split the full root page into original/root/split page and new
1469 * i.e., root remains fixed in tree anchor (inode) and the root is
1470 * copied to a single new right child page since root page <<
1471 * non-root page, and the split root page contains a single entry
1472 * for the new right child page.
1477 * struct xtsplit *split,
1478 * struct metapage **rmpp)
1481 * Pointer to page in which to insert or NULL on error.
1484 xtSplitRoot(tid_t tid,
1485 struct inode *ip, struct xtsplit * split, struct metapage ** rmpp)
1488 struct metapage *rmp;
1491 int skip, nextindex;
1494 struct pxdlist *pxdlist;
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];
1508 rbn = addressPXD(pxd);
1509 rmp = get_metapage(ip, rbn, PSIZE, 1);
1513 /* Allocate blocks to quota. */
1514 if (DQUOT_ALLOC_BLOCK(ip, lengthPXD(pxd))) {
1515 release_metapage(rmp);
1519 jfs_info("xtSplitRoot: ip:0x%p rmp:0x%p", ip, rmp);
1522 * acquire a transaction lock on the new right page;
1526 BT_MARK_DIRTY(rmp, ip);
1528 rp = (xtpage_t *) rmp->data;
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) -
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;
1602 jfs_info("xtSplitRoot: sp:0x%p rp:0x%p", sp, rp);
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 */
1624 struct metapage *mp; /* meta-page buffer */
1625 xtpage_t *p; /* base B+-tree index page */
1627 int index, nextindex, len;
1628 struct btstack btstack; /* traverse stack */
1629 struct xtsplit split; /* split information */
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)))
1641 /* retrieve search result */
1642 XT_GETSEARCH(ip, btstack.top, bn, mp, p, index);
1646 jfs_error(ip->i_sb, "xtExtend: xtSearch did not find extent");
1650 /* extension must be contiguous */
1651 xad = &p->xad[index];
1652 if ((offsetXAD(xad) + lengthXAD(xad)) != xoff) {
1654 jfs_error(ip->i_sb, "xtExtend: extension is not contiguous");
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)
1675 * extent overflow: insert entry for new extent
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 */
1691 split.index = index + 1;
1692 split.flag = XAD_NEW;
1693 split.off = xoff; /* split offset */
1696 split.pxdlist = NULL;
1697 if ((rc = xtSplitUp(tid, ip, &split, &btstack)))
1700 /* get back old page */
1701 XT_GETPAGE(ip, bn, mp, PSIZE, p, 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);
1716 /* get new child page */
1717 XT_GETPAGE(ip, bn, mp, PSIZE, p, 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
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 le16_add_cpu(&p->header.nextindex, 1);
1740 /* get back old entry */
1741 xad = &p->xad[index];
1748 XADlength(xad, xlen);
1749 if (!(xad->flag & XAD_NEW))
1750 xad->flag |= XAD_EXTENDED;
1752 if (!test_cflag(COMMIT_Nolink, ip)) {
1754 (xtlck->lwm.offset) ? min(index,
1755 (int)xtlck->lwm.offset) : index;
1757 le16_to_cpu(p->header.nextindex) - xtlck->lwm.offset;
1760 /* unpin the leaf page */
1770 * function: split existing 'tail' extent
1771 * (split offset >= start offset of tail extent), and
1772 * relocate and extend the split tail half;
1774 * note: existing extent may or may not have been committed.
1775 * caller is responsible for pager buffer cache update, and
1776 * working block allocation map update;
1777 * update pmap: free old split tail extent, alloc new extent;
1779 int xtTailgate(tid_t tid, /* transaction id */
1780 struct inode *ip, s64 xoff, /* split/new extent offset */
1781 s32 xlen, /* new extent length */
1782 s64 xaddr, /* new extent address */
1787 struct metapage *mp; /* meta-page buffer */
1788 xtpage_t *p; /* base B+-tree index page */
1790 int index, nextindex, llen, rlen;
1791 struct btstack btstack; /* traverse stack */
1792 struct xtsplit split; /* split information */
1795 struct xtlock *xtlck = 0;
1796 struct tlock *mtlck;
1797 struct maplock *pxdlock;
1800 printf("xtTailgate: nxoff:0x%lx nxlen:0x%x nxaddr:0x%lx\n",
1801 (ulong)xoff, xlen, (ulong)xaddr);
1804 /* there must exist extent to be tailgated */
1805 if ((rc = xtSearch(ip, xoff, NULL, &cmp, &btstack, XT_INSERT)))
1808 /* retrieve search result */
1809 XT_GETSEARCH(ip, btstack.top, bn, mp, p, index);
1813 jfs_error(ip->i_sb, "xtTailgate: couldn't find extent");
1817 /* entry found must be last entry */
1818 nextindex = le16_to_cpu(p->header.nextindex);
1819 if (index != nextindex - 1) {
1822 "xtTailgate: the entry found is not the last entry");
1826 BT_MARK_DIRTY(mp, ip);
1828 * acquire tlock of the leaf page containing original entry
1830 if (!test_cflag(COMMIT_Nolink, ip)) {
1831 tlck = txLock(tid, ip, mp, tlckXTREE | tlckGROW);
1832 xtlck = (struct xtlock *) & tlck->lock;
1835 /* completely replace extent ? */
1836 xad = &p->xad[index];
1838 printf("xtTailgate: xoff:0x%lx xlen:0x%x xaddr:0x%lx\n",
1839 (ulong)offsetXAD(xad), lengthXAD(xad), (ulong)addressXAD(xad));
1841 if ((llen = xoff - offsetXAD(xad)) == 0)
1845 * partially replace extent: insert entry for new extent
1849 * if the leaf page is full, insert the new entry and
1850 * propagate up the router entry for the new page from split
1852 * The xtSplitUp() will insert the entry and unpin the leaf page.
1854 if (nextindex == le16_to_cpu(p->header.maxentry)) {
1855 /* xtSpliUp() unpins leaf pages */
1857 split.index = index + 1;
1858 split.flag = XAD_NEW;
1859 split.off = xoff; /* split offset */
1862 split.pxdlist = NULL;
1863 if ((rc = xtSplitUp(tid, ip, &split, &btstack)))
1866 /* get back old page */
1867 XT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
1871 * if leaf root has been split, original root has been
1872 * copied to new child page, i.e., original entry now
1873 * resides on the new child page;
1875 if (p->header.flag & BT_INTERNAL) {
1876 ASSERT(p->header.nextindex ==
1877 cpu_to_le16(XTENTRYSTART + 1));
1878 xad = &p->xad[XTENTRYSTART];
1879 bn = addressXAD(xad);
1882 /* get new child page */
1883 XT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
1887 BT_MARK_DIRTY(mp, ip);
1888 if (!test_cflag(COMMIT_Nolink, ip)) {
1889 tlck = txLock(tid, ip, mp, tlckXTREE|tlckGROW);
1890 xtlck = (struct xtlock *) & tlck->lock;
1895 * insert the new entry into the leaf page
1898 /* insert the new entry: mark the entry NEW */
1899 xad = &p->xad[index + 1];
1900 XT_PUTENTRY(xad, XAD_NEW, xoff, xlen, xaddr);
1902 /* advance next available entry index */
1903 le16_add_cpu(&p->header.nextindex, 1);
1906 /* get back old XAD */
1907 xad = &p->xad[index];
1910 * truncate/relocate old extent at split offset
1913 /* update dmap for old/committed/truncated extent */
1914 rlen = lengthXAD(xad) - llen;
1915 if (!(xad->flag & XAD_NEW)) {
1916 /* free from PWMAP at commit */
1917 if (!test_cflag(COMMIT_Nolink, ip)) {
1918 mtlck = txMaplock(tid, ip, tlckMAP);
1919 pxdlock = (struct maplock *) & mtlck->lock;
1920 pxdlock->flag = mlckFREEPXD;
1921 PXDaddress(&pxdlock->pxd, addressXAD(xad) + llen);
1922 PXDlength(&pxdlock->pxd, rlen);
1926 /* free from WMAP */
1927 dbFree(ip, addressXAD(xad) + llen, (s64) rlen);
1931 XADlength(xad, llen);
1934 XT_PUTENTRY(xad, XAD_NEW, xoff, xlen, xaddr);
1936 if (!test_cflag(COMMIT_Nolink, ip)) {
1937 xtlck->lwm.offset = (xtlck->lwm.offset) ?
1938 min(index, (int)xtlck->lwm.offset) : index;
1939 xtlck->lwm.length = le16_to_cpu(p->header.nextindex) -
1943 /* unpin the leaf page */
1948 #endif /* _NOTYET */
1953 * function: update XAD;
1955 * update extent for allocated_but_not_recorded or
1956 * compressed extent;
1960 * logical extent of the specified XAD must be completely
1961 * contained by an existing XAD;
1963 int xtUpdate(tid_t tid, struct inode *ip, xad_t * nxad)
1967 struct metapage *mp; /* meta-page buffer */
1968 xtpage_t *p; /* base B+-tree index page */
1970 int index0, index, newindex, nextindex;
1971 struct btstack btstack; /* traverse stack */
1972 struct xtsplit split; /* split information */
1973 xad_t *xad, *lxad, *rxad;
1976 int nxlen, xlen, lxlen, rxlen;
1979 struct xtlock *xtlck = NULL;
1982 /* there must exist extent to be tailgated */
1983 nxoff = offsetXAD(nxad);
1984 nxlen = lengthXAD(nxad);
1985 nxaddr = addressXAD(nxad);
1987 if ((rc = xtSearch(ip, nxoff, NULL, &cmp, &btstack, XT_INSERT)))
1990 /* retrieve search result */
1991 XT_GETSEARCH(ip, btstack.top, bn, mp, p, index0);
1995 jfs_error(ip->i_sb, "xtUpdate: Could not find extent");
1999 BT_MARK_DIRTY(mp, ip);
2001 * acquire tlock of the leaf page containing original entry
2003 if (!test_cflag(COMMIT_Nolink, ip)) {
2004 tlck = txLock(tid, ip, mp, tlckXTREE | tlckGROW);
2005 xtlck = (struct xtlock *) & tlck->lock;
2008 xad = &p->xad[index0];
2010 xoff = offsetXAD(xad);
2011 xlen = lengthXAD(xad);
2012 xaddr = addressXAD(xad);
2014 /* nXAD must be completely contained within XAD */
2015 if ((xoff > nxoff) ||
2016 (nxoff + nxlen > xoff + xlen)) {
2019 "xtUpdate: nXAD in not completely contained within XAD");
2024 newindex = index + 1;
2025 nextindex = le16_to_cpu(p->header.nextindex);
2027 #ifdef _JFS_WIP_NOCOALESCE
2032 * replace XAD with nXAD
2034 replace: /* (nxoff == xoff) */
2035 if (nxlen == xlen) {
2036 /* replace XAD with nXAD:recorded */
2038 xad->flag = xflag & ~XAD_NOTRECORDED;
2041 } else /* (nxlen < xlen) */
2043 #endif /* _JFS_WIP_NOCOALESCE */
2045 /* #ifdef _JFS_WIP_COALESCE */
2050 * coalesce with left XAD
2052 //coalesceLeft: /* (xoff == nxoff) */
2053 /* is XAD first entry of page ? */
2054 if (index == XTENTRYSTART)
2057 /* is nXAD logically and physically contiguous with lXAD ? */
2058 lxad = &p->xad[index - 1];
2059 lxlen = lengthXAD(lxad);
2060 if (!(lxad->flag & XAD_NOTRECORDED) &&
2061 (nxoff == offsetXAD(lxad) + lxlen) &&
2062 (nxaddr == addressXAD(lxad) + lxlen) &&
2063 (lxlen + nxlen < MAXXLEN)) {
2064 /* extend right lXAD */
2066 XADlength(lxad, lxlen + nxlen);
2068 /* If we just merged two extents together, need to make sure the
2069 * right extent gets logged. If the left one is marked XAD_NEW,
2070 * then we know it will be logged. Otherwise, mark as
2073 if (!(lxad->flag & XAD_NEW))
2074 lxad->flag |= XAD_EXTENDED;
2078 XADoffset(xad, xoff + nxlen);
2079 XADlength(xad, xlen - nxlen);
2080 XADaddress(xad, xaddr + nxlen);
2082 } else { /* (xlen == nxlen) */
2085 if (index < nextindex - 1)
2086 memmove(&p->xad[index], &p->xad[index + 1],
2087 (nextindex - index -
2088 1) << L2XTSLOTSIZE);
2090 p->header.nextindex =
2091 cpu_to_le16(le16_to_cpu(p->header.nextindex) -
2095 newindex = index + 1;
2096 nextindex = le16_to_cpu(p->header.nextindex);
2097 xoff = nxoff = offsetXAD(lxad);
2098 xlen = nxlen = lxlen + nxlen;
2099 xaddr = nxaddr = addressXAD(lxad);
2105 * replace XAD with nXAD
2107 replace: /* (nxoff == xoff) */
2108 if (nxlen == xlen) {
2109 /* replace XAD with nXAD:recorded */
2111 xad->flag = xflag & ~XAD_NOTRECORDED;
2114 } else /* (nxlen < xlen) */
2118 * coalesce with right XAD
2120 coalesceRight: /* (xoff <= nxoff) */
2121 /* is XAD last entry of page ? */
2122 if (newindex == nextindex) {
2128 /* is nXAD logically and physically contiguous with rXAD ? */
2129 rxad = &p->xad[index + 1];
2130 rxlen = lengthXAD(rxad);
2131 if (!(rxad->flag & XAD_NOTRECORDED) &&
2132 (nxoff + nxlen == offsetXAD(rxad)) &&
2133 (nxaddr + nxlen == addressXAD(rxad)) &&
2134 (rxlen + nxlen < MAXXLEN)) {
2135 /* extend left rXAD */
2136 XADoffset(rxad, nxoff);
2137 XADlength(rxad, rxlen + nxlen);
2138 XADaddress(rxad, nxaddr);
2140 /* If we just merged two extents together, need to make sure
2141 * the left extent gets logged. If the right one is marked
2142 * XAD_NEW, then we know it will be logged. Otherwise, mark as
2145 if (!(rxad->flag & XAD_NEW))
2146 rxad->flag |= XAD_EXTENDED;
2150 XADlength(xad, xlen - nxlen);
2151 else { /* (xlen == nxlen) */
2154 memmove(&p->xad[index], &p->xad[index + 1],
2155 (nextindex - index - 1) << L2XTSLOTSIZE);
2157 p->header.nextindex =
2158 cpu_to_le16(le16_to_cpu(p->header.nextindex) -
2163 } else if (xoff == nxoff)
2166 if (xoff >= nxoff) {
2168 jfs_error(ip->i_sb, "xtUpdate: xoff >= nxoff");
2171 /* #endif _JFS_WIP_COALESCE */
2174 * split XAD into (lXAD, nXAD):
2177 * --|----------XAD----------|--
2180 updateRight: /* (xoff < nxoff) */
2181 /* truncate old XAD as lXAD:not_recorded */
2182 xad = &p->xad[index];
2183 XADlength(xad, nxoff - xoff);
2185 /* insert nXAD:recorded */
2186 if (nextindex == le16_to_cpu(p->header.maxentry)) {
2188 /* xtSpliUp() unpins leaf pages */
2190 split.index = newindex;
2191 split.flag = xflag & ~XAD_NOTRECORDED;
2194 split.addr = nxaddr;
2195 split.pxdlist = NULL;
2196 if ((rc = xtSplitUp(tid, ip, &split, &btstack)))
2199 /* get back old page */
2200 XT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
2204 * if leaf root has been split, original root has been
2205 * copied to new child page, i.e., original entry now
2206 * resides on the new child page;
2208 if (p->header.flag & BT_INTERNAL) {
2209 ASSERT(p->header.nextindex ==
2210 cpu_to_le16(XTENTRYSTART + 1));
2211 xad = &p->xad[XTENTRYSTART];
2212 bn = addressXAD(xad);
2215 /* get new child page */
2216 XT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
2220 BT_MARK_DIRTY(mp, ip);
2221 if (!test_cflag(COMMIT_Nolink, ip)) {
2222 tlck = txLock(tid, ip, mp, tlckXTREE|tlckGROW);
2223 xtlck = (struct xtlock *) & tlck->lock;
2226 /* is nXAD on new page ? */
2228 (le16_to_cpu(p->header.maxentry) >> 1)) {
2231 le16_to_cpu(p->header.nextindex) +
2237 /* if insert into middle, shift right remaining entries */
2238 if (newindex < nextindex)
2239 memmove(&p->xad[newindex + 1], &p->xad[newindex],
2240 (nextindex - newindex) << L2XTSLOTSIZE);
2242 /* insert the entry */
2243 xad = &p->xad[newindex];
2245 xad->flag = xflag & ~XAD_NOTRECORDED;
2247 /* advance next available entry index. */
2248 p->header.nextindex =
2249 cpu_to_le16(le16_to_cpu(p->header.nextindex) + 1);
2253 * does nXAD force 3-way split ?
2256 * --|----------XAD-------------|--
2257 * |-lXAD-| |-rXAD -|
2259 if (nxoff + nxlen == xoff + xlen)
2262 /* reorient nXAD as XAD for further split XAD into (nXAD, rXAD) */
2264 /* close out old page */
2265 if (!test_cflag(COMMIT_Nolink, ip)) {
2266 xtlck->lwm.offset = (xtlck->lwm.offset) ?
2267 min(index0, (int)xtlck->lwm.offset) : index0;
2269 le16_to_cpu(p->header.nextindex) -
2273 bn = le64_to_cpu(p->header.next);
2276 /* get new right page */
2277 XT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
2281 BT_MARK_DIRTY(mp, ip);
2282 if (!test_cflag(COMMIT_Nolink, ip)) {
2283 tlck = txLock(tid, ip, mp, tlckXTREE | tlckGROW);
2284 xtlck = (struct xtlock *) & tlck->lock;
2287 index0 = index = newindex;
2291 newindex = index + 1;
2292 nextindex = le16_to_cpu(p->header.nextindex);
2293 xlen = xlen - (nxoff - xoff);
2297 /* recompute split pages */
2298 if (nextindex == le16_to_cpu(p->header.maxentry)) {
2301 if ((rc = xtSearch(ip, nxoff, NULL, &cmp, &btstack, XT_INSERT)))
2304 /* retrieve search result */
2305 XT_GETSEARCH(ip, btstack.top, bn, mp, p, index0);
2309 jfs_error(ip->i_sb, "xtUpdate: xtSearch failed");
2313 if (index0 != index) {
2316 "xtUpdate: unexpected value of index");
2322 * split XAD into (nXAD, rXAD)
2325 * --|----------XAD----------|--
2328 updateLeft: /* (nxoff == xoff) && (nxlen < xlen) */
2329 /* update old XAD with nXAD:recorded */
2330 xad = &p->xad[index];
2332 xad->flag = xflag & ~XAD_NOTRECORDED;
2334 /* insert rXAD:not_recorded */
2335 xoff = xoff + nxlen;
2336 xlen = xlen - nxlen;
2337 xaddr = xaddr + nxlen;
2338 if (nextindex == le16_to_cpu(p->header.maxentry)) {
2340 printf("xtUpdate.updateLeft.split p:0x%p\n", p);
2342 /* xtSpliUp() unpins leaf pages */
2344 split.index = newindex;
2349 split.pxdlist = NULL;
2350 if ((rc = xtSplitUp(tid, ip, &split, &btstack)))
2353 /* get back old page */
2354 XT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
2359 * if leaf root has been split, original root has been
2360 * copied to new child page, i.e., original entry now
2361 * resides on the new child page;
2363 if (p->header.flag & BT_INTERNAL) {
2364 ASSERT(p->header.nextindex ==
2365 cpu_to_le16(XTENTRYSTART + 1));
2366 xad = &p->xad[XTENTRYSTART];
2367 bn = addressXAD(xad);
2370 /* get new child page */
2371 XT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
2375 BT_MARK_DIRTY(mp, ip);
2376 if (!test_cflag(COMMIT_Nolink, ip)) {
2377 tlck = txLock(tid, ip, mp, tlckXTREE|tlckGROW);
2378 xtlck = (struct xtlock *) & tlck->lock;
2382 /* if insert into middle, shift right remaining entries */
2383 if (newindex < nextindex)
2384 memmove(&p->xad[newindex + 1], &p->xad[newindex],
2385 (nextindex - newindex) << L2XTSLOTSIZE);
2387 /* insert the entry */
2388 xad = &p->xad[newindex];
2389 XT_PUTENTRY(xad, xflag, xoff, xlen, xaddr);
2391 /* advance next available entry index. */
2392 p->header.nextindex =
2393 cpu_to_le16(le16_to_cpu(p->header.nextindex) + 1);
2397 if (!test_cflag(COMMIT_Nolink, ip)) {
2398 xtlck->lwm.offset = (xtlck->lwm.offset) ?
2399 min(index0, (int)xtlck->lwm.offset) : index0;
2400 xtlck->lwm.length = le16_to_cpu(p->header.nextindex) -
2404 /* unpin the leaf page */
2414 * function: grow in append mode from contiguous region specified ;
2417 * tid - transaction id;
2419 * xflag - extent flag:
2420 * xoff - extent offset;
2421 * maxblocks - max extent length;
2422 * xlen - extent length (in/out);
2423 * xaddrp - extent address pointer (in/out):
2428 int xtAppend(tid_t tid, /* transaction id */
2429 struct inode *ip, int xflag, s64 xoff, s32 maxblocks,
2430 s32 * xlenp, /* (in/out) */
2431 s64 * xaddrp, /* (in/out) */
2435 struct metapage *mp; /* meta-page buffer */
2436 xtpage_t *p; /* base B+-tree index page */
2438 int index, nextindex;
2439 struct btstack btstack; /* traverse stack */
2440 struct xtsplit split; /* split information */
2444 struct xtlock *xtlck;
2445 int nsplit, nblocks, xlen;
2446 struct pxdlist pxdlist;
2452 jfs_info("xtAppend: xoff:0x%lx maxblocks:%d xlen:%d xaddr:0x%lx",
2453 (ulong) xoff, maxblocks, xlen, (ulong) xaddr);
2456 * search for the entry location at which to insert:
2458 * xtFastSearch() and xtSearch() both returns (leaf page
2459 * pinned, index at which to insert).
2460 * n.b. xtSearch() may return index of maxentry of
2463 if ((rc = xtSearch(ip, xoff, &next, &cmp, &btstack, XT_INSERT)))
2466 /* retrieve search result */
2467 XT_GETSEARCH(ip, btstack.top, bn, mp, p, index);
2475 xlen = min(xlen, (int)(next - xoff));
2478 * insert entry for new extent
2483 * if the leaf page is full, split the page and
2484 * propagate up the router entry for the new page from split
2486 * The xtSplitUp() will insert the entry and unpin the leaf page.
2488 nextindex = le16_to_cpu(p->header.nextindex);
2489 if (nextindex < le16_to_cpu(p->header.maxentry))
2493 * allocate new index blocks to cover index page split(s)
2495 nsplit = btstack.nsplit;
2496 split.pxdlist = &pxdlist;
2497 pxdlist.maxnpxd = pxdlist.npxd = 0;
2498 pxd = &pxdlist.pxd[0];
2499 nblocks = JFS_SBI(ip->i_sb)->nbperpage;
2500 for (; nsplit > 0; nsplit--, pxd++, xaddr += nblocks, maxblocks -= nblocks) {
2501 if ((rc = dbAllocBottomUp(ip, xaddr, (s64) nblocks)) == 0) {
2502 PXDaddress(pxd, xaddr);
2503 PXDlength(pxd, nblocks);
2510 /* undo allocation */
2515 xlen = min(xlen, maxblocks);
2518 * allocate data extent requested
2520 if ((rc = dbAllocBottomUp(ip, xaddr, (s64) xlen)))
2524 split.index = index;
2529 if ((rc = xtSplitUp(tid, ip, &split, &btstack))) {
2530 /* undo data extent allocation */
2531 dbFree(ip, *xaddrp, (s64) * xlenp);
2541 * insert the new entry into the leaf page
2545 * allocate data extent requested
2547 if ((rc = dbAllocBottomUp(ip, xaddr, (s64) xlen)))
2550 BT_MARK_DIRTY(mp, ip);
2552 * acquire a transaction lock on the leaf page;
2554 * action: xad insertion/extension;
2556 tlck = txLock(tid, ip, mp, tlckXTREE | tlckGROW);
2557 xtlck = (struct xtlock *) & tlck->lock;
2559 /* insert the new entry: mark the entry NEW */
2560 xad = &p->xad[index];
2561 XT_PUTENTRY(xad, xflag, xoff, xlen, xaddr);
2563 /* advance next available entry index */
2564 le16_add_cpu(&p->header.nextindex, 1);
2567 (xtlck->lwm.offset) ? min(index,(int) xtlck->lwm.offset) : index;
2568 xtlck->lwm.length = le16_to_cpu(p->header.nextindex) -
2575 /* unpin the leaf page */
2580 #ifdef _STILL_TO_PORT
2582 /* - TBD for defragmentaion/reorganization -
2587 * delete the entry with the specified key.
2589 * N.B.: whole extent of the entry is assumed to be deleted.
2594 * ENOENT: if the entry is not found.
2598 int xtDelete(tid_t tid, struct inode *ip, s64 xoff, s32 xlen, int flag)
2601 struct btstack btstack;
2604 struct metapage *mp;
2606 int index, nextindex;
2608 struct xtlock *xtlck;
2611 * find the matching entry; xtSearch() pins the page
2613 if ((rc = xtSearch(ip, xoff, NULL, &cmp, &btstack, 0)))
2616 XT_GETSEARCH(ip, btstack.top, bn, mp, p, index);
2618 /* unpin the leaf page */
2624 * delete the entry from the leaf page
2626 nextindex = le16_to_cpu(p->header.nextindex);
2627 le16_add_cpu(&p->header.nextindex, -1);
2630 * if the leaf page bocome empty, free the page
2632 if (p->header.nextindex == cpu_to_le16(XTENTRYSTART))
2633 return (xtDeleteUp(tid, ip, mp, p, &btstack));
2635 BT_MARK_DIRTY(mp, ip);
2637 * acquire a transaction lock on the leaf page;
2639 * action:xad deletion;
2641 tlck = txLock(tid, ip, mp, tlckXTREE);
2642 xtlck = (struct xtlock *) & tlck->lock;
2644 (xtlck->lwm.offset) ? min(index, xtlck->lwm.offset) : index;
2646 /* if delete from middle, shift left/compact the remaining entries */
2647 if (index < nextindex - 1)
2648 memmove(&p->xad[index], &p->xad[index + 1],
2649 (nextindex - index - 1) * sizeof(xad_t));
2657 /* - TBD for defragmentaion/reorganization -
2662 * free empty pages as propagating deletion up the tree
2669 xtDeleteUp(tid_t tid, struct inode *ip,
2670 struct metapage * fmp, xtpage_t * fp, struct btstack * btstack)
2673 struct metapage *mp;
2675 int index, nextindex;
2678 struct btframe *parent;
2680 struct xtlock *xtlck;
2683 * keep root leaf page which has become empty
2685 if (fp->header.flag & BT_ROOT) {
2686 /* keep the root page */
2687 fp->header.flag &= ~BT_INTERNAL;
2688 fp->header.flag |= BT_LEAF;
2689 fp->header.nextindex = cpu_to_le16(XTENTRYSTART);
2691 /* XT_PUTPAGE(fmp); */
2697 * free non-root leaf page
2699 if ((rc = xtRelink(tid, ip, fp))) {
2704 xaddr = addressPXD(&fp->header.self);
2705 xlen = lengthPXD(&fp->header.self);
2706 /* free the page extent */
2707 dbFree(ip, xaddr, (s64) xlen);
2709 /* free the buffer page */
2710 discard_metapage(fmp);
2713 * propagate page deletion up the index tree
2715 * If the delete from the parent page makes it empty,
2716 * continue all the way up the tree.
2717 * stop if the root page is reached (which is never deleted) or
2718 * if the entry deletion does not empty the page.
2720 while ((parent = BT_POP(btstack)) != NULL) {
2721 /* get/pin the parent page <sp> */
2722 XT_GETPAGE(ip, parent->bn, mp, PSIZE, p, rc);
2726 index = parent->index;
2728 /* delete the entry for the freed child page from parent.
2730 nextindex = le16_to_cpu(p->header.nextindex);
2733 * the parent has the single entry being deleted:
2734 * free the parent page which has become empty.
2736 if (nextindex == 1) {
2737 if (p->header.flag & BT_ROOT) {
2738 /* keep the root page */
2739 p->header.flag &= ~BT_INTERNAL;
2740 p->header.flag |= BT_LEAF;
2741 p->header.nextindex =
2742 cpu_to_le16(XTENTRYSTART);
2744 /* XT_PUTPAGE(mp); */
2748 /* free the parent page */
2749 if ((rc = xtRelink(tid, ip, p)))
2752 xaddr = addressPXD(&p->header.self);
2753 /* free the page extent */
2755 (s64) JFS_SBI(ip->i_sb)->nbperpage);
2757 /* unpin/free the buffer page */
2758 discard_metapage(mp);
2765 * the parent has other entries remaining:
2766 * delete the router entry from the parent page.
2769 BT_MARK_DIRTY(mp, ip);
2771 * acquire a transaction lock on the leaf page;
2773 * action:xad deletion;
2775 tlck = txLock(tid, ip, mp, tlckXTREE);
2776 xtlck = (struct xtlock *) & tlck->lock;
2778 (xtlck->lwm.offset) ? min(index,
2782 /* if delete from middle,
2783 * shift left/compact the remaining entries in the page
2785 if (index < nextindex - 1)
2786 memmove(&p->xad[index], &p->xad[index + 1],
2787 (nextindex - index -
2788 1) << L2XTSLOTSIZE);
2790 le16_add_cpu(&p->header.nextindex, -1);
2791 jfs_info("xtDeleteUp(entry): 0x%lx[%d]",
2792 (ulong) parent->bn, index);
2795 /* unpin the parent page */
2798 /* exit propagation up */
2807 * NAME: xtRelocate()
2809 * FUNCTION: relocate xtpage or data extent of regular file;
2810 * This function is mainly used by defragfs utility.
2812 * NOTE: This routine does not have the logic to handle
2813 * uncommitted allocated extent. The caller should call
2814 * txCommit() to commit all the allocation before call
2818 xtRelocate(tid_t tid, struct inode * ip, xad_t * oxad, /* old XAD */
2819 s64 nxaddr, /* new xaddr */
2821 { /* extent type: XTPAGE or DATAEXT */
2823 struct tblock *tblk;
2825 struct xtlock *xtlck;
2826 struct metapage *mp, *pmp, *lmp, *rmp; /* meta-page buffer */
2827 xtpage_t *p, *pp, *rp, *lp; /* base B+-tree index page */
2832 s64 oxaddr, sxaddr, dxaddr, nextbn, prevbn;
2834 s64 offset, nbytes, nbrd, pno;
2835 int nb, npages, nblks;
2839 struct pxd_lock *pxdlock;
2840 struct btstack btstack; /* traverse stack */
2842 xtype = xtype & EXTENT_TYPE;
2844 xoff = offsetXAD(oxad);
2845 oxaddr = addressXAD(oxad);
2846 xlen = lengthXAD(oxad);
2848 /* validate extent offset */
2849 offset = xoff << JFS_SBI(ip->i_sb)->l2bsize;
2850 if (offset >= ip->i_size)
2851 return -ESTALE; /* stale extent */
2853 jfs_info("xtRelocate: xtype:%d xoff:0x%lx xlen:0x%x xaddr:0x%lx:0x%lx",
2854 xtype, (ulong) xoff, xlen, (ulong) oxaddr, (ulong) nxaddr);
2857 * 1. get and validate the parent xtpage/xad entry
2858 * covering the source extent to be relocated;
2860 if (xtype == DATAEXT) {
2861 /* search in leaf entry */
2862 rc = xtSearch(ip, xoff, NULL, &cmp, &btstack, 0);
2866 /* retrieve search result */
2867 XT_GETSEARCH(ip, btstack.top, bn, pmp, pp, index);
2874 /* validate for exact match with a single entry */
2875 xad = &pp->xad[index];
2876 if (addressXAD(xad) != oxaddr || lengthXAD(xad) != xlen) {
2880 } else { /* (xtype == XTPAGE) */
2882 /* search in internal entry */
2883 rc = xtSearchNode(ip, oxad, &cmp, &btstack, 0);
2887 /* retrieve search result */
2888 XT_GETSEARCH(ip, btstack.top, bn, pmp, pp, index);
2895 /* xtSearchNode() validated for exact match with a single entry
2897 xad = &pp->xad[index];
2899 jfs_info("xtRelocate: parent xad entry validated.");
2902 * 2. relocate the extent
2904 if (xtype == DATAEXT) {
2905 /* if the extent is allocated-but-not-recorded
2906 * there is no real data to be moved in this extent,
2908 if (xad->flag & XAD_NOTRECORDED)
2911 /* release xtpage for cmRead()/xtLookup() */
2917 * copy target data pages to be relocated;
2919 * data extent must start at page boundary and
2920 * multiple of page size (except the last data extent);
2921 * read in each page of the source data extent into cbuf,
2922 * update the cbuf extent descriptor of the page to be
2923 * homeward bound to new dst data extent
2924 * copy the data from the old extent to new extent.
2925 * copy is essential for compressed files to avoid problems
2926 * that can arise if there was a change in compression
2928 * it is a good strategy because it may disrupt cache
2929 * policy to keep the pages in memory afterwards.
2931 offset = xoff << JFS_SBI(ip->i_sb)->l2bsize;
2932 assert((offset & CM_OFFSET) == 0);
2933 nbytes = xlen << JFS_SBI(ip->i_sb)->l2bsize;
2934 pno = offset >> CM_L2BSIZE;
2935 npages = (nbytes + (CM_BSIZE - 1)) >> CM_L2BSIZE;
2937 npages = ((offset + nbytes - 1) >> CM_L2BSIZE) -
2938 (offset >> CM_L2BSIZE) + 1;
2943 /* process the request one cache buffer at a time */
2944 for (nbrd = 0; nbrd < nbytes; nbrd += nb,
2945 offset += nb, pno++, npages--) {
2946 /* compute page size */
2947 nb = min(nbytes - nbrd, CM_BSIZE);
2949 /* get the cache buffer of the page */
2950 if (rc = cmRead(ip, offset, npages, &cp))
2953 assert(addressPXD(&cp->cm_pxd) == sxaddr);
2954 assert(!cp->cm_modified);
2956 /* bind buffer with the new extent address */
2957 nblks = nb >> JFS_IP(ip->i_sb)->l2bsize;
2958 cmSetXD(ip, cp, pno, dxaddr, nblks);
2960 /* release the cbuf, mark it as modified */
2967 /* get back parent page */
2968 if ((rc = xtSearch(ip, xoff, NULL, &cmp, &btstack, 0)))
2971 XT_GETSEARCH(ip, btstack.top, bn, pmp, pp, index);
2972 jfs_info("xtRelocate: target data extent relocated.");
2973 } else { /* (xtype == XTPAGE) */
2976 * read in the target xtpage from the source extent;
2978 XT_GETPAGE(ip, oxaddr, mp, PSIZE, p, rc);
2985 * read in sibling pages if any to update sibling pointers;
2988 if (p->header.next) {
2989 nextbn = le64_to_cpu(p->header.next);
2990 XT_GETPAGE(ip, nextbn, rmp, PSIZE, rp, rc);
2999 if (p->header.prev) {
3000 prevbn = le64_to_cpu(p->header.prev);
3001 XT_GETPAGE(ip, prevbn, lmp, PSIZE, lp, rc);
3011 /* at this point, all xtpages to be updated are in memory */
3014 * update sibling pointers of sibling xtpages if any;
3017 BT_MARK_DIRTY(lmp, ip);
3018 tlck = txLock(tid, ip, lmp, tlckXTREE | tlckRELINK);
3019 lp->header.next = cpu_to_le64(nxaddr);
3024 BT_MARK_DIRTY(rmp, ip);
3025 tlck = txLock(tid, ip, rmp, tlckXTREE | tlckRELINK);
3026 rp->header.prev = cpu_to_le64(nxaddr);
3031 * update the target xtpage to be relocated
3033 * update the self address of the target page
3034 * and write to destination extent;
3035 * redo image covers the whole xtpage since it is new page
3036 * to the destination extent;
3037 * update of bmap for the free of source extent
3038 * of the target xtpage itself:
3039 * update of bmap for the allocation of destination extent
3040 * of the target xtpage itself:
3041 * update of bmap for the extents covered by xad entries in
3042 * the target xtpage is not necessary since they are not
3044 * if not committed before this relocation,
3045 * target page may contain XAD_NEW entries which must
3046 * be scanned for bmap update (logredo() always
3047 * scan xtpage REDOPAGE image for bmap update);
3048 * if committed before this relocation (tlckRELOCATE),
3049 * scan may be skipped by commit() and logredo();
3051 BT_MARK_DIRTY(mp, ip);
3052 /* tlckNEW init xtlck->lwm.offset = XTENTRYSTART; */
3053 tlck = txLock(tid, ip, mp, tlckXTREE | tlckNEW);
3054 xtlck = (struct xtlock *) & tlck->lock;
3056 /* update the self address in the xtpage header */
3057 pxd = &p->header.self;
3058 PXDaddress(pxd, nxaddr);
3060 /* linelock for the after image of the whole page */
3062 le16_to_cpu(p->header.nextindex) - xtlck->lwm.offset;
3064 /* update the buffer extent descriptor of target xtpage */
3065 xsize = xlen << JFS_SBI(ip->i_sb)->l2bsize;
3066 bmSetXD(mp, nxaddr, xsize);
3068 /* unpin the target page to new homeward bound */
3070 jfs_info("xtRelocate: target xtpage relocated.");
3074 * 3. acquire maplock for the source extent to be freed;
3076 * acquire a maplock saving the src relocated extent address;
3077 * to free of the extent at commit time;
3080 /* if DATAEXT relocation, write a LOG_UPDATEMAP record for
3081 * free PXD of the source data extent (logredo() will update
3082 * bmap for free of source data extent), and update bmap for
3083 * free of the source data extent;
3085 if (xtype == DATAEXT)
3086 tlck = txMaplock(tid, ip, tlckMAP);
3087 /* if XTPAGE relocation, write a LOG_NOREDOPAGE record
3088 * for the source xtpage (logredo() will init NoRedoPage
3089 * filter and will also update bmap for free of the source
3090 * xtpage), and update bmap for free of the source xtpage;
3091 * N.B. We use tlckMAP instead of tlkcXTREE because there
3092 * is no buffer associated with this lock since the buffer
3093 * has been redirected to the target location.
3095 else /* (xtype == XTPAGE) */
3096 tlck = txMaplock(tid, ip, tlckMAP | tlckRELOCATE);
3098 pxdlock = (struct pxd_lock *) & tlck->lock;
3099 pxdlock->flag = mlckFREEPXD;
3100 PXDaddress(&pxdlock->pxd, oxaddr);
3101 PXDlength(&pxdlock->pxd, xlen);
3105 * 4. update the parent xad entry for relocation;
3107 * acquire tlck for the parent entry with XAD_NEW as entry
3108 * update which will write LOG_REDOPAGE and update bmap for
3109 * allocation of XAD_NEW destination extent;
3111 jfs_info("xtRelocate: update parent xad entry.");
3112 BT_MARK_DIRTY(pmp, ip);
3113 tlck = txLock(tid, ip, pmp, tlckXTREE | tlckGROW);
3114 xtlck = (struct xtlock *) & tlck->lock;
3116 /* update the XAD with the new destination extent; */
3117 xad = &pp->xad[index];
3118 xad->flag |= XAD_NEW;
3119 XADaddress(xad, nxaddr);
3121 xtlck->lwm.offset = min(index, xtlck->lwm.offset);
3122 xtlck->lwm.length = le16_to_cpu(pp->header.nextindex) -
3125 /* unpin the parent xtpage */
3135 * function: search for the internal xad entry covering specified extent.
3136 * This function is mainly used by defragfs utility.
3140 * xad - extent to find;
3141 * cmpp - comparison result:
3142 * btstack - traverse stack;
3143 * flag - search process flag;
3146 * btstack contains (bn, index) of search path traversed to the entry.
3147 * *cmpp is set to result of comparison with the entry returned.
3148 * the page containing the entry is pinned at exit.
3150 static int xtSearchNode(struct inode *ip, xad_t * xad, /* required XAD entry */
3151 int *cmpp, struct btstack * btstack, int flag)
3156 int cmp = 1; /* init for empty page */
3157 s64 bn; /* block number */
3158 struct metapage *mp; /* meta-page buffer */
3159 xtpage_t *p; /* page */
3160 int base, index, lim;
3161 struct btframe *btsp;
3166 xoff = offsetXAD(xad);
3167 xlen = lengthXAD(xad);
3168 xaddr = addressXAD(xad);
3171 * search down tree from root:
3173 * between two consecutive entries of <Ki, Pi> and <Kj, Pj> of
3174 * internal page, child page Pi contains entry with k, Ki <= K < Kj.
3176 * if entry with search key K is not found
3177 * internal page search find the entry with largest key Ki
3178 * less than K which point to the child page to search;
3179 * leaf page search find the entry with smallest key Kj
3180 * greater than K so that the returned index is the position of
3181 * the entry to be shifted right for insertion of new entry.
3182 * for empty tree, search key is greater than any key of the tree.
3184 * by convention, root bn = 0.
3187 /* get/pin the page to search */
3188 XT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
3191 if (p->header.flag & BT_LEAF) {
3196 lim = le16_to_cpu(p->header.nextindex) - XTENTRYSTART;
3199 * binary search with search key K on the current page
3201 for (base = XTENTRYSTART; lim; lim >>= 1) {
3202 index = base + (lim >> 1);
3204 XT_CMP(cmp, xoff, &p->xad[index], t64);
3209 * verify for exact match;
3211 if (xaddr == addressXAD(&p->xad[index]) &&
3212 xoff == offsetXAD(&p->xad[index])) {
3215 /* save search result */
3216 btsp = btstack->top;
3218 btsp->index = index;
3224 /* descend/search its child page */
3235 * search miss - non-leaf page:
3237 * base is the smallest index with key (Kj) greater than
3238 * search key (K) and may be zero or maxentry index.
3239 * if base is non-zero, decrement base by one to get the parent
3240 * entry of the child page to search.
3242 index = base ? base - 1 : base;
3245 * go down to child page
3248 /* get the child page block number */
3249 bn = addressXAD(&p->xad[index]);
3251 /* unpin the parent page */
3261 * link around a freed page.
3270 static int xtRelink(tid_t tid, struct inode *ip, xtpage_t * p)
3273 struct metapage *mp;
3277 nextbn = le64_to_cpu(p->header.next);
3278 prevbn = le64_to_cpu(p->header.prev);
3280 /* update prev pointer of the next page */
3282 XT_GETPAGE(ip, nextbn, mp, PSIZE, p, rc);
3287 * acquire a transaction lock on the page;
3289 * action: update prev pointer;
3291 BT_MARK_DIRTY(mp, ip);
3292 tlck = txLock(tid, ip, mp, tlckXTREE | tlckRELINK);
3294 /* the page may already have been tlock'd */
3296 p->header.prev = cpu_to_le64(prevbn);
3301 /* update next pointer of the previous page */
3303 XT_GETPAGE(ip, prevbn, mp, PSIZE, p, rc);
3308 * acquire a transaction lock on the page;
3310 * action: update next pointer;
3312 BT_MARK_DIRTY(mp, ip);
3313 tlck = txLock(tid, ip, mp, tlckXTREE | tlckRELINK);
3315 /* the page may already have been tlock'd */
3317 p->header.next = le64_to_cpu(nextbn);
3324 #endif /* _STILL_TO_PORT */
3330 * initialize file root (inline in inode)
3332 void xtInitRoot(tid_t tid, struct inode *ip)
3337 * acquire a transaction lock on the root
3341 txLock(tid, ip, (struct metapage *) &JFS_IP(ip)->bxflag,
3342 tlckXTREE | tlckNEW);
3343 p = &JFS_IP(ip)->i_xtroot;
3345 p->header.flag = DXD_INDEX | BT_ROOT | BT_LEAF;
3346 p->header.nextindex = cpu_to_le16(XTENTRYSTART);
3348 if (S_ISDIR(ip->i_mode))
3349 p->header.maxentry = cpu_to_le16(XTROOTINITSLOT_DIR);
3351 p->header.maxentry = cpu_to_le16(XTROOTINITSLOT);
3361 * We can run into a deadlock truncating a file with a large number of
3362 * xtree pages (large fragmented file). A robust fix would entail a
3363 * reservation system where we would reserve a number of metadata pages
3364 * and tlocks which we would be guaranteed without a deadlock. Without
3365 * this, a partial fix is to limit number of metadata pages we will lock
3366 * in a single transaction. Currently we will truncate the file so that
3367 * no more than 50 leaf pages will be locked. The caller of xtTruncate
3368 * will be responsible for ensuring that the current transaction gets
3369 * committed, and that subsequent transactions are created to truncate
3370 * the file further if needed.
3372 #define MAX_TRUNCATE_LEAVES 50
3378 * traverse for truncation logging backward bottom up;
3379 * terminate at the last extent entry at the current subtree
3380 * root page covering new down size.
3381 * truncation may occur within the last extent entry.
3387 * int type) {PWMAP, PMAP, WMAP; DELETE, TRUNCATE}
3393 * 1. truncate (non-COMMIT_NOLINK file)
3394 * by jfs_truncate() or jfs_open(O_TRUNC):
3396 * 2. truncate index table of directory when last entry removed
3397 * map update via tlock at commit time;
3399 * Call xtTruncate_pmap instead
3401 * 1. remove (free zero link count) on last reference release
3402 * (pmap has been freed at commit zero link count);
3403 * 2. truncate (COMMIT_NOLINK file, i.e., tmp file):
3405 * map update directly at truncation time;
3408 * no LOG_NOREDOPAGE is required (NOREDOFILE is sufficient);
3409 * else if (TRUNCATE)
3410 * must write LOG_NOREDOPAGE for deleted index page;
3412 * pages may already have been tlocked by anonymous transactions
3413 * during file growth (i.e., write) before truncation;
3415 * except last truncated entry, deleted entries remains as is
3416 * in the page (nextindex is updated) for other use
3417 * (e.g., log/update allocation map): this avoid copying the page
3418 * info but delay free of pages;
3421 s64 xtTruncate(tid_t tid, struct inode *ip, s64 newsize, int flag)
3425 struct metapage *mp;
3428 int index, nextindex;
3431 int xlen, len, freexlen;
3432 struct btstack btstack;
3433 struct btframe *parent;
3434 struct tblock *tblk = NULL;
3435 struct tlock *tlck = NULL;
3436 struct xtlock *xtlck = NULL;
3437 struct xdlistlock xadlock; /* maplock for COMMIT_WMAP */
3438 struct pxd_lock *pxdlock; /* maplock for COMMIT_WMAP */
3441 int locked_leaves = 0;
3443 /* save object truncation type */
3445 tblk = tid_to_tblock(tid);
3446 tblk->xflag |= flag;
3452 assert(flag != COMMIT_PMAP);
3454 if (flag == COMMIT_PWMAP)
3458 xadlock.flag = mlckFREEXADLIST;
3463 * if the newsize is not an integral number of pages,
3464 * the file between newsize and next page boundary will
3466 * if truncating into a file hole, it will cause
3467 * a full block to be allocated for the logical block.
3471 * release page blocks of truncated region <teof, eof>
3473 * free the data blocks from the leaf index blocks.
3474 * delete the parent index entries corresponding to
3475 * the freed child data/index blocks.
3476 * free the index blocks themselves which aren't needed
3477 * in new sized file.
3479 * index blocks are updated only if the blocks are to be
3480 * retained in the new sized file.
3481 * if type is PMAP, the data and index pages are NOT
3482 * freed, and the data and index blocks are NOT freed
3484 * (this will allow continued access of data/index of
3485 * temporary file (zerolink count file truncated to zero-length)).
3487 teof = (newsize + (JFS_SBI(ip->i_sb)->bsize - 1)) >>
3488 JFS_SBI(ip->i_sb)->l2bsize;
3496 * root resides in the inode
3501 * first access of each page:
3504 XT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
3508 /* process entries backward from last index */
3509 index = le16_to_cpu(p->header.nextindex) - 1;
3512 /* Since this is the rightmost page at this level, and we may have
3513 * already freed a page that was formerly to the right, let's make
3514 * sure that the next pointer is zero.
3516 if (p->header.next) {
3519 * Make sure this change to the header is logged.
3520 * If we really truncate this leaf, the flag
3521 * will be changed to tlckTRUNCATE
3523 tlck = txLock(tid, ip, mp, tlckXTREE|tlckGROW);
3524 BT_MARK_DIRTY(mp, ip);
3528 if (p->header.flag & BT_INTERNAL)
3536 /* does region covered by leaf page precede Teof ? */
3537 xad = &p->xad[index];
3538 xoff = offsetXAD(xad);
3539 xlen = lengthXAD(xad);
3540 if (teof >= xoff + xlen) {
3545 /* (re)acquire tlock of the leaf page */
3547 if (++locked_leaves > MAX_TRUNCATE_LEAVES) {
3549 * We need to limit the size of the transaction
3550 * to avoid exhausting pagecache & tlocks
3553 newsize = (xoff + xlen) << JFS_SBI(ip->i_sb)->l2bsize;
3556 tlck = txLock(tid, ip, mp, tlckXTREE);
3557 tlck->type = tlckXTREE | tlckTRUNCATE;
3558 xtlck = (struct xtlock *) & tlck->lock;
3559 xtlck->hwm.offset = le16_to_cpu(p->header.nextindex) - 1;
3561 BT_MARK_DIRTY(mp, ip);
3564 * scan backward leaf page entries
3566 for (; index >= XTENTRYSTART; index--) {
3567 xad = &p->xad[index];
3568 xoff = offsetXAD(xad);
3569 xlen = lengthXAD(xad);
3570 xaddr = addressXAD(xad);
3573 * The "data" for a directory is indexed by the block
3574 * device's address space. This metadata must be invalidated
3577 if (S_ISDIR(ip->i_mode) && (teof == 0))
3578 invalidate_xad_metapages(ip, *xad);
3580 * entry beyond eof: continue scan of current page
3582 * ---|---=======------->
3591 * (xoff <= teof): last entry to be deleted from page;
3592 * If other entries remain in page: keep and update the page.
3596 * eof == entry_start: delete the entry
3598 * -------|=======------->
3605 if (index == XTENTRYSTART)
3611 * eof within the entry: truncate the entry.
3613 * -------===|===------->
3616 else if (teof < xoff + xlen) {
3617 /* update truncated entry */
3619 freexlen = xlen - len;
3620 XADlength(xad, len);
3622 /* save pxd of truncated extent in tlck */
3624 if (log) { /* COMMIT_PWMAP */
3625 xtlck->lwm.offset = (xtlck->lwm.offset) ?
3626 min(index, (int)xtlck->lwm.offset) : index;
3627 xtlck->lwm.length = index + 1 -
3629 xtlck->twm.offset = index;
3630 pxdlock = (struct pxd_lock *) & xtlck->pxdlock;
3631 pxdlock->flag = mlckFREEPXD;
3632 PXDaddress(&pxdlock->pxd, xaddr);
3633 PXDlength(&pxdlock->pxd, freexlen);
3635 /* free truncated extent */
3636 else { /* COMMIT_WMAP */
3638 pxdlock = (struct pxd_lock *) & xadlock;
3639 pxdlock->flag = mlckFREEPXD;
3640 PXDaddress(&pxdlock->pxd, xaddr);
3641 PXDlength(&pxdlock->pxd, freexlen);
3642 txFreeMap(ip, pxdlock, NULL, COMMIT_WMAP);
3644 /* reset map lock */
3645 xadlock.flag = mlckFREEXADLIST;
3648 /* current entry is new last entry; */
3649 nextindex = index + 1;
3654 * eof beyond the entry:
3656 * -------=======---|--->
3659 else { /* (xoff + xlen < teof) */
3661 nextindex = index + 1;
3664 if (nextindex < le16_to_cpu(p->header.nextindex)) {
3665 if (!log) { /* COMMIT_WAMP */
3666 xadlock.xdlist = &p->xad[nextindex];
3668 le16_to_cpu(p->header.nextindex) -
3670 txFreeMap(ip, (struct maplock *) & xadlock,
3673 p->header.nextindex = cpu_to_le16(nextindex);
3678 /* assert(freed == 0); */
3680 } /* end scan of leaf page entries */
3685 * leaf page become empty: free the page if type != PMAP
3687 if (log) { /* COMMIT_PWMAP */
3688 /* txCommit() with tlckFREE:
3689 * free data extents covered by leaf [XTENTRYSTART:hwm);
3690 * invalidate leaf if COMMIT_PWMAP;
3691 * if (TRUNCATE), will write LOG_NOREDOPAGE;
3693 tlck->type = tlckXTREE | tlckFREE;
3694 } else { /* COMMIT_WAMP */
3696 /* free data extents covered by leaf */
3697 xadlock.xdlist = &p->xad[XTENTRYSTART];
3699 le16_to_cpu(p->header.nextindex) - XTENTRYSTART;
3700 txFreeMap(ip, (struct maplock *) & xadlock, NULL, COMMIT_WMAP);
3703 if (p->header.flag & BT_ROOT) {
3704 p->header.flag &= ~BT_INTERNAL;
3705 p->header.flag |= BT_LEAF;
3706 p->header.nextindex = cpu_to_le16(XTENTRYSTART);
3708 XT_PUTPAGE(mp); /* debug */
3711 if (log) { /* COMMIT_PWMAP */
3712 /* page will be invalidated at tx completion
3715 } else { /* COMMIT_WMAP */
3718 lid_to_tlock(mp->lid)->flag |= tlckFREELOCK;
3720 /* invalidate empty leaf page */
3721 discard_metapage(mp);
3726 * the leaf page become empty: delete the parent entry
3727 * for the leaf page if the parent page is to be kept
3728 * in the new sized file.
3732 * go back up to the parent page
3735 /* pop/restore parent entry for the current child page */
3736 if ((parent = BT_POP(&btstack)) == NULL)
3737 /* current page must have been root */
3740 /* get back the parent page */
3742 XT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
3746 index = parent->index;
3749 * child page was not empty:
3752 /* has any entry deleted from parent ? */
3753 if (index < le16_to_cpu(p->header.nextindex) - 1) {
3754 /* (re)acquire tlock on the parent page */
3755 if (log) { /* COMMIT_PWMAP */
3756 /* txCommit() with tlckTRUNCATE:
3757 * free child extents covered by parent [);
3759 tlck = txLock(tid, ip, mp, tlckXTREE);
3760 xtlck = (struct xtlock *) & tlck->lock;
3761 if (!(tlck->type & tlckTRUNCATE)) {
3763 le16_to_cpu(p->header.
3766 tlckXTREE | tlckTRUNCATE;
3768 } else { /* COMMIT_WMAP */
3770 /* free child extents covered by parent */
3771 xadlock.xdlist = &p->xad[index + 1];
3773 le16_to_cpu(p->header.nextindex) -
3775 txFreeMap(ip, (struct maplock *) & xadlock,
3778 BT_MARK_DIRTY(mp, ip);
3780 p->header.nextindex = cpu_to_le16(index + 1);
3787 * child page was empty:
3789 nfreed += lengthXAD(&p->xad[index]);
3792 * During working map update, child page's tlock must be handled
3793 * before parent's. This is because the parent's tlock will cause
3794 * the child's disk space to be marked available in the wmap, so
3795 * it's important that the child page be released by that time.
3797 * ToDo: tlocks should be on doubly-linked list, so we can
3798 * quickly remove it and add it to the end.
3802 * Move parent page's tlock to the end of the tid's tlock list
3804 if (log && mp->lid && (tblk->last != mp->lid) &&
3805 lid_to_tlock(mp->lid)->tid) {
3806 lid_t lid = mp->lid;
3809 tlck = lid_to_tlock(lid);
3811 if (tblk->next == lid)
3812 tblk->next = tlck->next;
3814 for (prev = lid_to_tlock(tblk->next);
3816 prev = lid_to_tlock(prev->next)) {
3819 prev->next = tlck->next;
3821 lid_to_tlock(tblk->last)->next = lid;
3827 * parent page become empty: free the page
3829 if (index == XTENTRYSTART) {
3830 if (log) { /* COMMIT_PWMAP */
3831 /* txCommit() with tlckFREE:
3832 * free child extents covered by parent;
3833 * invalidate parent if COMMIT_PWMAP;
3835 tlck = txLock(tid, ip, mp, tlckXTREE);
3836 xtlck = (struct xtlock *) & tlck->lock;
3838 le16_to_cpu(p->header.nextindex) - 1;
3839 tlck->type = tlckXTREE | tlckFREE;
3840 } else { /* COMMIT_WMAP */
3842 /* free child extents covered by parent */
3843 xadlock.xdlist = &p->xad[XTENTRYSTART];
3845 le16_to_cpu(p->header.nextindex) -
3847 txFreeMap(ip, (struct maplock *) & xadlock, NULL,
3850 BT_MARK_DIRTY(mp, ip);
3852 if (p->header.flag & BT_ROOT) {
3853 p->header.flag &= ~BT_INTERNAL;
3854 p->header.flag |= BT_LEAF;
3855 p->header.nextindex = cpu_to_le16(XTENTRYSTART);
3856 if (le16_to_cpu(p->header.maxentry) == XTROOTMAXSLOT) {
3858 * Shrink root down to allow inline
3859 * EA (otherwise fsck complains)
3861 p->header.maxentry =
3862 cpu_to_le16(XTROOTINITSLOT);
3863 JFS_IP(ip)->mode2 |= INLINEEA;
3866 XT_PUTPAGE(mp); /* debug */
3869 if (log) { /* COMMIT_PWMAP */
3870 /* page will be invalidated at tx completion
3873 } else { /* COMMIT_WMAP */
3876 lid_to_tlock(mp->lid)->flag |=
3879 /* invalidate parent page */
3880 discard_metapage(mp);
3883 /* parent has become empty and freed:
3884 * go back up to its parent page
3891 * parent page still has entries for front region;
3894 /* try truncate region covered by preceding entry
3895 * (process backward)
3899 /* go back down to the child page corresponding
3906 * internal page: go down to child page of current entry
3909 /* save current parent entry for the child page */
3910 if (BT_STACK_FULL(&btstack)) {
3911 jfs_error(ip->i_sb, "stack overrun in xtTruncate!");
3915 BT_PUSH(&btstack, bn, index);
3917 /* get child page */
3918 xad = &p->xad[index];
3919 bn = addressXAD(xad);
3922 * first access of each internal entry:
3924 /* release parent page */
3927 /* process the child page */
3932 * update file resource stat
3936 if (S_ISDIR(ip->i_mode) && !newsize)
3937 ip->i_size = 1; /* fsck hates zero-length directories */
3939 ip->i_size = newsize;
3941 /* update quota allocation to reflect freed blocks */
3942 DQUOT_FREE_BLOCK(ip, nfreed);
3945 * free tlock of invalidated pages
3947 if (flag == COMMIT_WMAP)
3958 * Perform truncate to zero length for deleted file, leaving the
3959 * the xtree and working map untouched. This allows the file to
3960 * be accessed via open file handles, while the delete of the file
3961 * is committed to disk.
3966 * s64 committed_size)
3968 * return: new committed size
3972 * To avoid deadlock by holding too many transaction locks, the
3973 * truncation may be broken up into multiple transactions.
3974 * The committed_size keeps track of part of the file has been
3975 * freed from the pmaps.
3977 s64 xtTruncate_pmap(tid_t tid, struct inode *ip, s64 committed_size)
3980 struct btstack btstack;
3983 int locked_leaves = 0;
3984 struct metapage *mp;
3986 struct btframe *parent;
3988 struct tblock *tblk;
3989 struct tlock *tlck = NULL;
3993 struct xtlock *xtlck = NULL;
3995 /* save object truncation type */
3996 tblk = tid_to_tblock(tid);
3997 tblk->xflag |= COMMIT_PMAP;
4002 if (committed_size) {
4003 xoff = (committed_size >> JFS_SBI(ip->i_sb)->l2bsize) - 1;
4004 rc = xtSearch(ip, xoff, NULL, &cmp, &btstack, 0);
4008 XT_GETSEARCH(ip, btstack.top, bn, mp, p, index);
4013 "xtTruncate_pmap: did not find extent");
4020 * root resides in the inode
4025 * first access of each page:
4028 XT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
4032 /* process entries backward from last index */
4033 index = le16_to_cpu(p->header.nextindex) - 1;
4035 if (p->header.flag & BT_INTERNAL)
4043 if (++locked_leaves > MAX_TRUNCATE_LEAVES) {
4045 * We need to limit the size of the transaction
4046 * to avoid exhausting pagecache & tlocks
4048 xad = &p->xad[index];
4049 xoff = offsetXAD(xad);
4050 xlen = lengthXAD(xad);
4052 return (xoff + xlen) << JFS_SBI(ip->i_sb)->l2bsize;
4054 tlck = txLock(tid, ip, mp, tlckXTREE);
4055 tlck->type = tlckXTREE | tlckFREE;
4056 xtlck = (struct xtlock *) & tlck->lock;
4057 xtlck->hwm.offset = index;
4063 * go back up to the parent page
4066 /* pop/restore parent entry for the current child page */
4067 if ((parent = BT_POP(&btstack)) == NULL)
4068 /* current page must have been root */
4071 /* get back the parent page */
4073 XT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
4077 index = parent->index;
4080 * parent page become empty: free the page
4082 if (index == XTENTRYSTART) {
4083 /* txCommit() with tlckFREE:
4084 * free child extents covered by parent;
4085 * invalidate parent if COMMIT_PWMAP;
4087 tlck = txLock(tid, ip, mp, tlckXTREE);
4088 xtlck = (struct xtlock *) & tlck->lock;
4089 xtlck->hwm.offset = le16_to_cpu(p->header.nextindex) - 1;
4090 tlck->type = tlckXTREE | tlckFREE;
4094 if (p->header.flag & BT_ROOT) {
4102 * parent page still has entries for front region;
4107 * internal page: go down to child page of current entry
4110 /* save current parent entry for the child page */
4111 if (BT_STACK_FULL(&btstack)) {
4112 jfs_error(ip->i_sb, "stack overrun in xtTruncate_pmap!");
4116 BT_PUSH(&btstack, bn, index);
4118 /* get child page */
4119 xad = &p->xad[index];
4120 bn = addressXAD(xad);
4123 * first access of each internal entry:
4125 /* release parent page */
4128 /* process the child page */
4136 #ifdef CONFIG_JFS_STATISTICS
4137 int jfs_xtstat_read(char *buffer, char **start, off_t offset, int length,
4138 int *eof, void *data)
4143 len += sprintf(buffer,
4144 "JFS Xtree statistics\n"
4145 "====================\n"
4147 "fast searches = %d\n"
4154 *start = buffer + begin;