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 BT_PUSH(btstack, bn, index);
762 /* get the child page block number */
763 bn = addressXAD(&p->xad[index]);
765 /* unpin the parent page */
776 * tid - transaction id;
778 * xflag - extent flag (XAD_NOTRECORDED):
779 * xoff - extent offset;
780 * xlen - extent length;
781 * xaddrp - extent address pointer (in/out):
783 * caller allocated data extent at *xaddrp;
785 * allocate data extent and return its xaddr;
790 int xtInsert(tid_t tid, /* transaction id */
791 struct inode *ip, int xflag, s64 xoff, s32 xlen, s64 * xaddrp,
796 struct metapage *mp; /* meta-page buffer */
797 xtpage_t *p; /* base B+-tree index page */
799 int index, nextindex;
800 struct btstack btstack; /* traverse stack */
801 struct xtsplit split; /* split information */
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
818 if ((rc = xtSearch(ip, xoff, &next, &cmp, &btstack, XT_INSERT)))
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))) {
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;
842 if ((rc = DQUOT_ALLOC_BLOCK(ip, xlen)))
844 if ((rc = dbAlloc(ip, hint, (s64) xlen, &xaddr))) {
845 DQUOT_FREE_BLOCK(ip, xlen);
851 * insert entry for new extent
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)) {
869 split.pxdlist = NULL;
870 if ((rc = xtSplitUp(tid, ip, &split, &btstack))) {
871 /* undo data extent allocation */
873 dbFree(ip, xaddr, (s64) xlen);
874 DQUOT_FREE_BLOCK(ip, xlen);
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;
911 (xtlck->lwm.offset) ? min(index,
912 (int)xtlck->lwm.offset) : index;
914 le16_to_cpu(p->header.nextindex) - xtlck->lwm.offset;
920 /* unpin the leaf page */
931 * split full pages as propagating insertion up the tree
934 * tid - transaction id;
936 * split - entry parameter descriptor;
937 * btstack - traverse stack from xtSearch()
943 struct inode *ip, struct xtsplit * split, struct btstack * btstack)
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 */
959 int nsplit; /* number of pages split */
960 struct pxdlist pxdlist;
963 struct xtlock *xtlck;
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. */
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,
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;
1005 le16_to_cpu(sp->header.nextindex) -
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))
1026 PXDaddress(pxd, xaddr);
1027 PXDlength(pxd, xlen);
1034 /* undo allocation */
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);
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
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 */
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);
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 */
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;
1117 /* unpin previous right child page */
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);
1133 /* keep new child page <rp> pinned */
1136 * parent page is not full - insert in parent page
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],
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;
1177 le16_to_cpu(sp->header.nextindex) -
1181 /* unpin parent page */
1184 /* exit propagate up */
1189 /* unpin current right page */
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.
1207 * struct xtsplit *split,
1208 * struct metapage **rmpp,
1212 * Pointer to page in which to insert or NULL on error.
1215 xtSplitPage(tid_t tid, struct inode *ip,
1216 struct xtsplit * split, struct metapage ** rmpp, s64 * rbnp)
1219 struct metapage *smp;
1221 struct metapage *rmp;
1222 xtpage_t *rp; /* new right page allocated */
1223 s64 rbn; /* new right page block number */
1224 struct metapage *mp;
1227 int skip, maxentry, middle, righthalf, n;
1229 struct pxdlist *pxdlist;
1232 struct xtlock *sxtlck = NULL, *rxtlck = NULL;
1233 int quota_allocation = 0;
1236 sp = XT_PAGE(ip, smp);
1238 INCREMENT(xtStat.split);
1240 pxdlist = split->pxdlist;
1241 pxd = &pxdlist->pxd[pxdlist->npxd];
1243 rbn = addressPXD(pxd);
1245 /* Allocate blocks to quota. */
1246 if (DQUOT_ALLOC_BLOCK(ip, lengthPXD(pxd))) {
1251 quota_allocation += lengthPXD(pxd);
1254 * allocate the new right page for the split
1256 rmp = get_metapage(ip, rbn, PSIZE, 1);
1262 jfs_info("xtSplitPage: ip:0x%p smp:0x%p rmp:0x%p", ip, smp, rmp);
1264 BT_MARK_DIRTY(rmp, ip);
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
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.
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,
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;
1335 jfs_info("xtSplitPage: sp:0x%p rp:0x%p", sp, rp);
1340 * non-sequential insert (at possibly middle page)
1344 * update previous pointer of old next/right page of <sp>
1347 XT_GETPAGE(ip, nextbn, mp, PSIZE, p, rc);
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;
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 */
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,
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:
1410 /* move left head of right half to right page */
1412 memmove(&rp->xad[XTENTRYSTART], &sp->xad[middle],
1415 /* insert new entry */
1418 XT_PUTENTRY(xad, split->flag, split->off, split->len,
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 +
1437 if (!test_cflag(COMMIT_Nolink, ip)) {
1438 sxtlck->lwm.length = le16_to_cpu(sp->header.nextindex) -
1441 /* rxtlck->lwm.offset = XTENTRYSTART; */
1442 rxtlck->lwm.length = le16_to_cpu(rp->header.nextindex) -
1449 jfs_info("xtSplitPage: sp:0x%p rp:0x%p", sp, rp);
1454 /* Rollback quota allocation. */
1455 if (quota_allocation)
1456 DQUOT_FREE_BLOCK(ip, quota_allocation);
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.
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 p->header.nextindex =
1738 cpu_to_le16(le16_to_cpu(p->header.nextindex) + 1);
1741 /* get back old entry */
1742 xad = &p->xad[index];
1749 XADlength(xad, xlen);
1750 if (!(xad->flag & XAD_NEW))
1751 xad->flag |= XAD_EXTENDED;
1753 if (!test_cflag(COMMIT_Nolink, ip)) {
1755 (xtlck->lwm.offset) ? min(index,
1756 (int)xtlck->lwm.offset) : index;
1758 le16_to_cpu(p->header.nextindex) - xtlck->lwm.offset;
1761 /* unpin the leaf page */
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 */
1788 struct metapage *mp; /* meta-page buffer */
1789 xtpage_t *p; /* base B+-tree index page */
1791 int index, nextindex, llen, rlen;
1792 struct btstack btstack; /* traverse stack */
1793 struct xtsplit split; /* split information */
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)))
1809 /* retrieve search result */
1810 XT_GETSEARCH(ip, btstack.top, bn, mp, p, index);
1814 jfs_error(ip->i_sb, "xtTailgate: couldn't find extent");
1818 /* entry found must be last entry */
1819 nextindex = le16_to_cpu(p->header.nextindex);
1820 if (index != nextindex - 1) {
1823 "xtTailgate: the entry found is not the last entry");
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)
1846 * partially replace extent: insert entry for new extent
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 */
1858 split.index = index + 1;
1859 split.flag = XAD_NEW;
1860 split.off = xoff; /* split offset */
1863 split.pxdlist = NULL;
1864 if ((rc = xtSplitUp(tid, ip, &split, &btstack)))
1867 /* get back old page */
1868 XT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
1872 * if leaf root has been split, original root has been
1873 * copied to new child page, i.e., original entry now
1874 * resides on the new child page;
1876 if (p->header.flag & BT_INTERNAL) {
1877 ASSERT(p->header.nextindex ==
1878 cpu_to_le16(XTENTRYSTART + 1));
1879 xad = &p->xad[XTENTRYSTART];
1880 bn = addressXAD(xad);
1883 /* get new child page */
1884 XT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
1888 BT_MARK_DIRTY(mp, ip);
1889 if (!test_cflag(COMMIT_Nolink, ip)) {
1890 tlck = txLock(tid, ip, mp, tlckXTREE|tlckGROW);
1891 xtlck = (struct xtlock *) & tlck->lock;
1896 * insert the new entry into the leaf page
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
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);
1928 /* free from WMAP */
1929 dbFree(ip, addressXAD(xad) + llen, (s64) rlen);
1933 XADlength(xad, llen);
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) -
1945 /* unpin the leaf page */
1950 #endif /* _NOTYET */
1955 * function: update XAD;
1957 * update extent for allocated_but_not_recorded or
1958 * compressed extent;
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)
1969 struct metapage *mp; /* meta-page buffer */
1970 xtpage_t *p; /* base B+-tree index page */
1972 int index0, index, newindex, nextindex;
1973 struct btstack btstack; /* traverse stack */
1974 struct xtsplit split; /* split information */
1975 xad_t *xad, *lxad, *rxad;
1978 int nxlen, xlen, lxlen, rxlen;
1981 struct xtlock *xtlck = NULL;
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)))
1992 /* retrieve search result */
1993 XT_GETSEARCH(ip, btstack.top, bn, mp, p, index0);
1997 jfs_error(ip->i_sb, "xtUpdate: Could not find extent");
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];
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)) {
2021 "xtUpdate: nXAD in not completely contained within XAD");
2026 newindex = index + 1;
2027 nextindex = le16_to_cpu(p->header.nextindex);
2029 #ifdef _JFS_WIP_NOCOALESCE
2034 * replace XAD with nXAD
2036 replace: /* (nxoff == xoff) */
2037 if (nxlen == xlen) {
2038 /* replace XAD with nXAD:recorded */
2040 xad->flag = xflag & ~XAD_NOTRECORDED;
2043 } else /* (nxlen < xlen) */
2045 #endif /* _JFS_WIP_NOCOALESCE */
2047 /* #ifdef _JFS_WIP_COALESCE */
2052 * coalesce with left XAD
2054 //coalesceLeft: /* (xoff == nxoff) */
2055 /* is XAD first entry of page ? */
2056 if (index == XTENTRYSTART)
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 */
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
2075 if (!(lxad->flag & XAD_NEW))
2076 lxad->flag |= XAD_EXTENDED;
2080 XADoffset(xad, xoff + nxlen);
2081 XADlength(xad, xlen - nxlen);
2082 XADaddress(xad, xaddr + nxlen);
2084 } else { /* (xlen == nxlen) */
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) -
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);
2107 * replace XAD with nXAD
2109 replace: /* (nxoff == xoff) */
2110 if (nxlen == xlen) {
2111 /* replace XAD with nXAD:recorded */
2113 xad->flag = xflag & ~XAD_NOTRECORDED;
2116 } else /* (nxlen < xlen) */
2120 * coalesce with right XAD
2122 coalesceRight: /* (xoff <= nxoff) */
2123 /* is XAD last entry of page ? */
2124 if (newindex == nextindex) {
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
2147 if (!(rxad->flag & XAD_NEW))
2148 rxad->flag |= XAD_EXTENDED;
2152 XADlength(xad, xlen - nxlen);
2153 else { /* (xlen == nxlen) */
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) -
2165 } else if (xoff == nxoff)
2168 if (xoff >= nxoff) {
2170 jfs_error(ip->i_sb, "xtUpdate: xoff >= nxoff");
2173 /* #endif _JFS_WIP_COALESCE */
2176 * split XAD into (lXAD, nXAD):
2179 * --|----------XAD----------|--
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 */
2192 split.index = newindex;
2193 split.flag = xflag & ~XAD_NOTRECORDED;
2196 split.addr = nxaddr;
2197 split.pxdlist = NULL;
2198 if ((rc = xtSplitUp(tid, ip, &split, &btstack)))
2201 /* get back old page */
2202 XT_GETPAGE(ip, bn, mp, PSIZE, p, 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);
2217 /* get new child page */
2218 XT_GETPAGE(ip, bn, mp, PSIZE, p, 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;
2228 /* is nXAD on new page ? */
2230 (le16_to_cpu(p->header.maxentry) >> 1)) {
2233 le16_to_cpu(p->header.nextindex) +
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];
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 ?
2258 * --|----------XAD-------------|--
2259 * |-lXAD-| |-rXAD -|
2261 if (nxoff + nxlen == xoff + xlen)
2264 /* reorient nXAD as XAD for further split XAD into (nXAD, rXAD) */
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;
2271 le16_to_cpu(p->header.nextindex) -
2275 bn = le64_to_cpu(p->header.next);
2278 /* get new right page */
2279 XT_GETPAGE(ip, bn, mp, PSIZE, p, 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;
2293 newindex = index + 1;
2294 nextindex = le16_to_cpu(p->header.nextindex);
2295 xlen = xlen - (nxoff - xoff);
2299 /* recompute split pages */
2300 if (nextindex == le16_to_cpu(p->header.maxentry)) {
2303 if ((rc = xtSearch(ip, nxoff, NULL, &cmp, &btstack, XT_INSERT)))
2306 /* retrieve search result */
2307 XT_GETSEARCH(ip, btstack.top, bn, mp, p, index0);
2311 jfs_error(ip->i_sb, "xtUpdate: xtSearch failed");
2315 if (index0 != index) {
2318 "xtUpdate: unexpected value of index");
2324 * split XAD into (nXAD, rXAD)
2327 * --|----------XAD----------|--
2330 updateLeft: /* (nxoff == xoff) && (nxlen < xlen) */
2331 /* update old XAD with nXAD:recorded */
2332 xad = &p->xad[index];
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 */
2346 split.index = newindex;
2351 split.pxdlist = NULL;
2352 if ((rc = xtSplitUp(tid, ip, &split, &btstack)))
2355 /* get back old page */
2356 XT_GETPAGE(ip, bn, mp, PSIZE, p, 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);
2372 /* get new child page */
2373 XT_GETPAGE(ip, bn, mp, PSIZE, p, 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;
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);
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) -
2406 /* unpin the leaf page */
2416 * function: grow in append mode from contiguous region specified ;
2419 * tid - transaction id;
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):
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) */
2437 struct metapage *mp; /* meta-page buffer */
2438 xtpage_t *p; /* base B+-tree index page */
2440 int index, nextindex;
2441 struct btstack btstack; /* traverse stack */
2442 struct xtsplit split; /* split information */
2446 struct xtlock *xtlck;
2447 int nsplit, nblocks, xlen;
2448 struct pxdlist pxdlist;
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
2465 if ((rc = xtSearch(ip, xoff, &next, &cmp, &btstack, XT_INSERT)))
2468 /* retrieve search result */
2469 XT_GETSEARCH(ip, btstack.top, bn, mp, p, index);
2477 xlen = min(xlen, (int)(next - xoff));
2480 * insert entry for new extent
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))
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);
2512 /* undo allocation */
2517 xlen = min(xlen, maxblocks);
2520 * allocate data extent requested
2522 if ((rc = dbAllocBottomUp(ip, xaddr, (s64) xlen)))
2526 split.index = index;
2531 if ((rc = xtSplitUp(tid, ip, &split, &btstack))) {
2532 /* undo data extent allocation */
2533 dbFree(ip, *xaddrp, (s64) * xlenp);
2543 * insert the new entry into the leaf page
2547 * allocate data extent requested
2549 if ((rc = dbAllocBottomUp(ip, xaddr, (s64) xlen)))
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);
2570 (xtlck->lwm.offset) ? min(index,(int) xtlck->lwm.offset) : index;
2571 xtlck->lwm.length = le16_to_cpu(p->header.nextindex) -
2578 /* unpin the leaf page */
2583 #ifdef _STILL_TO_PORT
2585 /* - TBD for defragmentaion/reorganization -
2590 * delete the entry with the specified key.
2592 * N.B.: whole extent of the entry is assumed to be deleted.
2597 * ENOENT: if the entry is not found.
2601 int xtDelete(tid_t tid, struct inode *ip, s64 xoff, s32 xlen, int flag)
2604 struct btstack btstack;
2607 struct metapage *mp;
2609 int index, nextindex;
2611 struct xtlock *xtlck;
2614 * find the matching entry; xtSearch() pins the page
2616 if ((rc = xtSearch(ip, xoff, NULL, &cmp, &btstack, 0)))
2619 XT_GETSEARCH(ip, btstack.top, bn, mp, p, index);
2621 /* unpin the leaf page */
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;
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));
2661 /* - TBD for defragmentaion/reorganization -
2666 * free empty pages as propagating deletion up the tree
2673 xtDeleteUp(tid_t tid, struct inode *ip,
2674 struct metapage * fmp, xtpage_t * fp, struct btstack * btstack)
2677 struct metapage *mp;
2679 int index, nextindex;
2682 struct btframe *parent;
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); */
2701 * free non-root leaf page
2703 if ((rc = xtRelink(tid, ip, fp))) {
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);
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); */
2752 /* free the parent page */
2753 if ((rc = xtRelink(tid, ip, p)))
2756 xaddr = addressPXD(&p->header.self);
2757 /* free the page extent */
2759 (s64) JFS_SBI(ip->i_sb)->nbperpage);
2761 /* unpin/free the buffer page */
2762 discard_metapage(mp);
2769 * the parent has other entries remaining:
2770 * delete the router entry from the parent page.
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;
2782 (xtlck->lwm.offset) ? min(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 */
2804 /* exit propagation up */
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
2824 xtRelocate(tid_t tid, struct inode * ip, xad_t * oxad, /* old XAD */
2825 s64 nxaddr, /* new xaddr */
2827 { /* extent type: XTPAGE or DATAEXT */
2829 struct tblock *tblk;
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 */
2838 s64 oxaddr, sxaddr, dxaddr, nextbn, prevbn;
2840 s64 offset, nbytes, nbrd, pno;
2841 int nb, npages, nblks;
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);
2872 /* retrieve search result */
2873 XT_GETSEARCH(ip, btstack.top, bn, pmp, pp, index);
2880 /* validate for exact match with a single entry */
2881 xad = &pp->xad[index];
2882 if (addressXAD(xad) != oxaddr || lengthXAD(xad) != xlen) {
2886 } else { /* (xtype == XTPAGE) */
2888 /* search in internal entry */
2889 rc = xtSearchNode(ip, oxad, &cmp, &btstack, 0);
2893 /* retrieve search result */
2894 XT_GETSEARCH(ip, btstack.top, bn, pmp, pp, index);
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)
2917 /* release xtpage for cmRead()/xtLookup() */
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
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;
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))
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 */
2973 /* get back parent page */
2974 if ((rc = xtSearch(ip, xoff, NULL, &cmp, &btstack, 0)))
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);
2991 * read in sibling pages if any to update sibling pointers;
2994 if (p->header.next) {
2995 nextbn = le64_to_cpu(p->header.next);
2996 XT_GETPAGE(ip, nextbn, rmp, PSIZE, rp, rc);
3005 if (p->header.prev) {
3006 prevbn = le64_to_cpu(p->header.prev);
3007 XT_GETPAGE(ip, prevbn, lmp, PSIZE, lp, rc);
3017 /* at this point, all xtpages to be updated are in memory */
3020 * update sibling pointers of sibling xtpages if any;
3023 BT_MARK_DIRTY(lmp, ip);
3025 txLock(tid, ip, lmp, tlckXTREE | tlckRELINK);
3026 lp->header.next = cpu_to_le64(nxaddr);
3031 BT_MARK_DIRTY(rmp, ip);
3033 txLock(tid, ip, rmp, tlckXTREE | tlckRELINK);
3034 rp->header.prev = cpu_to_le64(nxaddr);
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
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 */
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 */
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;
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);
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) -
3133 /* unpin the parent xtpage */
3143 * function: search for the internal xad entry covering specified extent.
3144 * This function is mainly used by defragfs utility.
3148 * xad - extent to find;
3149 * cmpp - comparison result:
3150 * btstack - traverse stack;
3151 * flag - search process flag;
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)
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;
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.
3195 /* get/pin the page to search */
3196 XT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
3199 if (p->header.flag & BT_LEAF) {
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);
3217 * verify for exact match;
3219 if (xaddr == addressXAD(&p->xad[index]) &&
3220 xoff == offsetXAD(&p->xad[index])) {
3223 /* save search result */
3224 btsp = btstack->top;
3226 btsp->index = index;
3232 /* descend/search its child page */
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
3256 /* get the child page block number */
3257 bn = addressXAD(&p->xad[index]);
3259 /* unpin the parent page */
3269 * link around a freed page.
3278 static int xtRelink(tid_t tid, struct inode *ip, xtpage_t * p)
3281 struct metapage *mp;
3285 nextbn = le64_to_cpu(p->header.next);
3286 prevbn = le64_to_cpu(p->header.prev);
3288 /* update prev pointer of the next page */
3290 XT_GETPAGE(ip, nextbn, mp, PSIZE, p, 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);
3309 /* update next pointer of the previous page */
3311 XT_GETPAGE(ip, prevbn, mp, PSIZE, p, 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);
3332 #endif /* _STILL_TO_PORT */
3338 * initialize file root (inline in inode)
3340 void xtInitRoot(tid_t tid, struct inode *ip)
3345 * acquire a transaction lock on the root
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);
3359 p->header.maxentry = cpu_to_le16(XTROOTINITSLOT);
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
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.
3395 * int type) {PWMAP, PMAP, WMAP; DELETE, TRUNCATE}
3401 * 1. truncate (non-COMMIT_NOLINK file)
3402 * by jfs_truncate() or jfs_open(O_TRUNC):
3404 * 2. truncate index table of directory when last entry removed
3405 * map update via tlock at commit time;
3407 * Call xtTruncate_pmap instead
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):
3413 * map update directly at truncation time;
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)
3433 struct metapage *mp;
3436 int index, nextindex;
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 */
3449 int locked_leaves = 0;
3451 /* save object truncation type */
3453 tblk = tid_to_tblock(tid);
3454 tblk->xflag |= flag;
3460 assert(flag != COMMIT_PMAP);
3462 if (flag == COMMIT_PWMAP)
3466 xadlock.flag = mlckFREEXADLIST;
3471 * if the newsize is not an integral number of pages,
3472 * the file between newsize and next page boundary will
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
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;
3504 * root resides in the inode
3509 * first access of each page:
3512 XT_GETPAGE(ip, bn, mp, PSIZE, p, 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) {
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);
3536 if (p->header.flag & BT_INTERNAL)
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) {
3553 /* (re)acquire tlock of the leaf page */
3555 if (++locked_leaves > MAX_TRUNCATE_LEAVES) {
3557 * We need to limit the size of the transaction
3558 * to avoid exhausting pagecache & tlocks
3561 newsize = (xoff + xlen) << JFS_SBI(ip->i_sb)->l2bsize;
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
3585 if (S_ISDIR(ip->i_mode) && (teof == 0))
3586 invalidate_xad_metapages(ip, *xad);
3588 * entry beyond eof: continue scan of current page
3590 * ---|---=======------->
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
3606 * -------|=======------->
3613 if (index == XTENTRYSTART)
3619 * eof within the entry: truncate the entry.
3621 * -------===|===------->
3624 else if (teof < xoff + xlen) {
3625 /* update truncated entry */
3627 freexlen = xlen - len;
3628 XADlength(xad, len);
3630 /* save pxd of truncated extent in tlck */
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 -
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;
3662 * eof beyond the entry:
3664 * -------=======---|--->
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];
3676 le16_to_cpu(p->header.nextindex) -
3678 txFreeMap(ip, (struct maplock *) & xadlock,
3681 p->header.nextindex = cpu_to_le16(nextindex);
3686 /* assert(freed == 0); */
3688 } /* end scan of leaf page entries */
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];
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 */
3719 if (log) { /* COMMIT_PWMAP */
3720 /* page will be invalidated at tx completion
3723 } else { /* COMMIT_WMAP */
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
3743 /* pop/restore parent entry for the current child page */
3744 if ((parent = BT_POP(&btstack)) == NULL)
3745 /* current page must have been root */
3748 /* get back the parent page */
3750 XT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
3754 index = parent->index;
3757 * child page was not empty:
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)) {
3771 le16_to_cpu(p->header.
3774 tlckXTREE | tlckTRUNCATE;
3776 } else { /* COMMIT_WMAP */
3778 /* free child extents covered by parent */
3779 xadlock.xdlist = &p->xad[index + 1];
3781 le16_to_cpu(p->header.nextindex) -
3783 txFreeMap(ip, (struct maplock *) & xadlock,
3786 BT_MARK_DIRTY(mp, ip);
3788 p->header.nextindex = cpu_to_le16(index + 1);
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;
3817 tlck = lid_to_tlock(lid);
3819 if (tblk->next == lid)
3820 tblk->next = tlck->next;
3822 for (prev = lid_to_tlock(tblk->next);
3824 prev = lid_to_tlock(prev->next)) {
3827 prev->next = tlck->next;
3829 lid_to_tlock(tblk->last)->next = 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;
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];
3853 le16_to_cpu(p->header.nextindex) -
3855 txFreeMap(ip, (struct maplock *) & xadlock, NULL,
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 */
3877 if (log) { /* COMMIT_PWMAP */
3878 /* page will be invalidated at tx completion
3881 } else { /* COMMIT_WMAP */
3884 lid_to_tlock(mp->lid)->flag |=
3887 /* invalidate parent page */
3888 discard_metapage(mp);
3891 /* parent has become empty and freed:
3892 * go back up to its parent page
3899 * parent page still has entries for front region;
3902 /* try truncate region covered by preceding entry
3903 * (process backward)
3907 /* go back down to the child page corresponding
3914 * internal page: go down to child page of current entry
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 */
3930 /* process the child page */
3935 * update file resource stat
3939 if (S_ISDIR(ip->i_mode) && !newsize)
3940 ip->i_size = 1; /* fsck hates zero-length directories */
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)
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.
3969 * s64 committed_size)
3971 * return: new committed size
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)
3983 struct btstack btstack;
3986 int locked_leaves = 0;
3987 struct metapage *mp;
3989 struct btframe *parent;
3991 struct tblock *tblk;
3992 struct tlock *tlck = NULL;
3996 struct xtlock *xtlck = NULL;
3998 /* save object truncation type */
3999 tblk = tid_to_tblock(tid);
4000 tblk->xflag |= COMMIT_PMAP;
4005 if (committed_size) {
4006 xoff = (committed_size >> JFS_SBI(ip->i_sb)->l2bsize) - 1;
4007 rc = xtSearch(ip, xoff, NULL, &cmp, &btstack, 0);
4011 XT_GETSEARCH(ip, btstack.top, bn, mp, p, index);
4016 "xtTruncate_pmap: did not find extent");
4023 * root resides in the inode
4028 * first access of each page:
4031 XT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
4035 /* process entries backward from last index */
4036 index = le16_to_cpu(p->header.nextindex) - 1;
4038 if (p->header.flag & BT_INTERNAL)
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);
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;
4066 * go back up to the parent page
4069 /* pop/restore parent entry for the current child page */
4070 if ((parent = BT_POP(&btstack)) == NULL)
4071 /* current page must have been root */
4074 /* get back the parent page */
4076 XT_GETPAGE(ip, bn, mp, PSIZE, p, 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;
4093 le16_to_cpu(p->header.nextindex) - 1;
4094 tlck->type = tlckXTREE | tlckFREE;
4098 if (p->header.flag & BT_ROOT) {
4106 * parent page still has entries for front region;
4111 * internal page: go down to child page of current entry
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 */
4127 /* process the child page */
4135 #ifdef CONFIG_JFS_STATISTICS
4136 int jfs_xtstat_read(char *buffer, char **start, off_t offset, int length,
4137 int *eof, void *data)
4142 len += sprintf(buffer,
4143 "JFS Xtree statistics\n"
4144 "====================\n"
4146 "fast searches = %d\n"
4153 *start = buffer + begin;