2 * Copyright (C) International Business Machines Corp., 2000-2003
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
20 * jfs_dtree.c: directory B+-tree manager
22 * B+-tree with variable length key directory:
24 * each directory page is structured as an array of 32-byte
25 * directory entry slots initialized as a freelist
26 * to avoid search/compaction of free space at insertion.
27 * when an entry is inserted, a number of slots are allocated
28 * from the freelist as required to store variable length data
29 * of the entry; when the entry is deleted, slots of the entry
30 * are returned to freelist.
32 * leaf entry stores full name as key and file serial number
33 * (aka inode number) as data.
34 * internal/router entry stores sufffix compressed name
35 * as key and simple extent descriptor as data.
37 * each directory page maintains a sorted entry index table
38 * which stores the start slot index of sorted entries
39 * to allow binary search on the table.
41 * directory starts as a root/leaf page in on-disk inode
43 * when it becomes full, it starts a leaf of a external extent
44 * of length of 1 block. each time the first leaf becomes full,
45 * it is extended rather than split (its size is doubled),
46 * until its length becoms 4 KBytes, from then the extent is split
47 * with new 4 Kbyte extent when it becomes full
48 * to reduce external fragmentation of small directories.
50 * blah, blah, blah, for linear scan of directory in pieces by
54 * case-insensitive directory file system
56 * names are stored in case-sensitive way in leaf entry.
57 * but stored, searched and compared in case-insensitive (uppercase) order
58 * (i.e., both search key and entry key are folded for search/compare):
59 * (note that case-sensitive order is BROKEN in storage, e.g.,
60 * sensitive: Ad, aB, aC, aD -> insensitive: aB, aC, aD, Ad
62 * entries which folds to the same key makes up a equivalent class
63 * whose members are stored as contiguous cluster (may cross page boundary)
64 * but whose order is arbitrary and acts as duplicate, e.g.,
67 * once match is found at leaf, requires scan forward/backward
68 * either for, in case-insensitive search, duplicate
69 * or for, in case-sensitive search, for exact match
71 * router entry must be created/stored in case-insensitive way
73 * (right most key of left page and left most key of right page
74 * are folded, and its suffix compression is propagated as router
76 * (e.g., if split occurs <abc> and <aBd>, <ABD> trather than <aB>
77 * should be made the router key for the split)
79 * case-insensitive search:
83 * case-insensitive search of B-tree:
84 * for internal entry, router key is already folded;
85 * for leaf entry, fold the entry key before comparison.
87 * if (leaf entry case-insensitive match found)
88 * if (next entry satisfies case-insensitive match)
90 * if (prev entry satisfies case-insensitive match)
97 * target directory inode lock is being held on entry/exit
98 * of all main directory service routines.
100 * log based recovery:
103 #include <linux/fs.h>
104 #include "jfs_incore.h"
105 #include "jfs_superblock.h"
106 #include "jfs_filsys.h"
107 #include "jfs_metapage.h"
108 #include "jfs_dmap.h"
109 #include "jfs_unicode.h"
110 #include "jfs_debug.h"
112 /* dtree split parameter */
117 struct component_name *key;
119 struct pxdlist *pxdlist;
122 #define DT_PAGE(IP, MP) BT_PAGE(IP, MP, dtpage_t, i_dtroot)
124 /* get page buffer for specified block address */
125 #define DT_GETPAGE(IP, BN, MP, SIZE, P, RC)\
127 BT_GETPAGE(IP, BN, MP, dtpage_t, SIZE, P, RC, i_dtroot)\
130 if (((P)->header.nextindex > (((BN)==0)?DTROOTMAXSLOT:(P)->header.maxslot)) ||\
131 ((BN) && ((P)->header.maxslot > DTPAGEMAXSLOT)))\
134 jfs_error((IP)->i_sb, "DT_GETPAGE: dtree page corrupt");\
141 /* for consistency */
142 #define DT_PUTPAGE(MP) BT_PUTPAGE(MP)
144 #define DT_GETSEARCH(IP, LEAF, BN, MP, P, INDEX) \
145 BT_GETSEARCH(IP, LEAF, BN, MP, dtpage_t, P, INDEX, i_dtroot)
150 static int dtSplitUp(tid_t tid, struct inode *ip,
151 struct dtsplit * split, struct btstack * btstack);
153 static int dtSplitPage(tid_t tid, struct inode *ip, struct dtsplit * split,
154 struct metapage ** rmpp, dtpage_t ** rpp, pxd_t * rxdp);
156 static int dtExtendPage(tid_t tid, struct inode *ip,
157 struct dtsplit * split, struct btstack * btstack);
159 static int dtSplitRoot(tid_t tid, struct inode *ip,
160 struct dtsplit * split, struct metapage ** rmpp);
162 static int dtDeleteUp(tid_t tid, struct inode *ip, struct metapage * fmp,
163 dtpage_t * fp, struct btstack * btstack);
165 static int dtRelink(tid_t tid, struct inode *ip, dtpage_t * p);
167 static int dtReadFirst(struct inode *ip, struct btstack * btstack);
169 static int dtReadNext(struct inode *ip,
170 loff_t * offset, struct btstack * btstack);
172 static int dtCompare(struct component_name * key, dtpage_t * p, int si);
174 static int ciCompare(struct component_name * key, dtpage_t * p, int si,
177 static void dtGetKey(dtpage_t * p, int i, struct component_name * key,
180 static void ciGetLeafPrefixKey(dtpage_t * lp, int li, dtpage_t * rp,
181 int ri, struct component_name * key, int flag);
183 static void dtInsertEntry(dtpage_t * p, int index, struct component_name * key,
184 ddata_t * data, struct dt_lock **);
186 static void dtMoveEntry(dtpage_t * sp, int si, dtpage_t * dp,
187 struct dt_lock ** sdtlock, struct dt_lock ** ddtlock,
190 static void dtDeleteEntry(dtpage_t * p, int fi, struct dt_lock ** dtlock);
192 static void dtTruncateEntry(dtpage_t * p, int ti, struct dt_lock ** dtlock);
194 static void dtLinelockFreelist(dtpage_t * p, int m, struct dt_lock ** dtlock);
196 #define ciToUpper(c) UniStrupr((c)->name)
201 * Reads a page of a directory's index table.
202 * Having metadata mapped into the directory inode's address space
203 * presents a multitude of problems. We avoid this by mapping to
204 * the absolute address space outside of the *_metapage routines
206 static struct metapage *read_index_page(struct inode *inode, s64 blkno)
213 rc = xtLookup(inode, blkno, 1, &xflag, &xaddr, &xlen, 1);
214 if (rc || (xlen == 0))
217 return read_metapage(inode, xaddr, PSIZE, 1);
223 * Same as get_index_page(), but get's a new page without reading
225 static struct metapage *get_index_page(struct inode *inode, s64 blkno)
232 rc = xtLookup(inode, blkno, 1, &xflag, &xaddr, &xlen, 1);
233 if (rc || (xlen == 0))
236 return get_metapage(inode, xaddr, PSIZE, 1);
242 * Returns dtree page containing directory table entry for specified
243 * index and pointer to its entry.
245 * mp must be released by caller.
247 static struct dir_table_slot *find_index(struct inode *ip, u32 index,
248 struct metapage ** mp, s64 *lblock)
250 struct jfs_inode_info *jfs_ip = JFS_IP(ip);
254 struct dir_table_slot *slot;
255 static int maxWarnings = 10;
259 jfs_warn("find_entry called with index = %d", index);
265 if (index >= jfs_ip->next_index) {
266 jfs_warn("find_entry called with index >= next_index");
270 if (jfs_ip->next_index <= (MAX_INLINE_DIRTABLE_ENTRY + 1)) {
272 * Inline directory table
275 slot = &jfs_ip->i_dirtable[index - 2];
277 offset = (index - 2) * sizeof(struct dir_table_slot);
278 page_offset = offset & (PSIZE - 1);
279 blkno = ((offset + 1) >> L2PSIZE) <<
280 JFS_SBI(ip->i_sb)->l2nbperpage;
282 if (*mp && (*lblock != blkno)) {
283 release_metapage(*mp);
288 *mp = read_index_page(ip, blkno);
291 jfs_err("free_index: error reading directory table");
296 (struct dir_table_slot *) ((char *) (*mp)->data +
302 static inline void lock_index(tid_t tid, struct inode *ip, struct metapage * mp,
306 struct linelock *llck;
309 tlck = txLock(tid, ip, mp, tlckDATA);
310 llck = (struct linelock *) tlck->lock;
312 if (llck->index >= llck->maxcnt)
313 llck = txLinelock(llck);
314 lv = &llck->lv[llck->index];
317 * Linelock slot size is twice the size of directory table
318 * slot size. 512 entries per page.
320 lv->offset = ((index - 2) & 511) >> 1;
328 * Adds an entry to the directory index table. This is used to provide
329 * each directory entry with a persistent index in which to resume
330 * directory traversals
332 static u32 add_index(tid_t tid, struct inode *ip, s64 bn, int slot)
334 struct super_block *sb = ip->i_sb;
335 struct jfs_sb_info *sbi = JFS_SBI(sb);
336 struct jfs_inode_info *jfs_ip = JFS_IP(ip);
338 struct dir_table_slot *dirtab_slot;
340 struct linelock *llck;
349 ASSERT(DO_INDEX(ip));
351 if (jfs_ip->next_index < 2) {
352 jfs_warn("add_index: next_index = %d. Resetting!",
354 jfs_ip->next_index = 2;
357 index = jfs_ip->next_index++;
359 if (index <= MAX_INLINE_DIRTABLE_ENTRY) {
361 * i_size reflects size of index table, or 8 bytes per entry.
363 ip->i_size = (loff_t) (index - 1) << 3;
366 * dir table fits inline within inode
368 dirtab_slot = &jfs_ip->i_dirtable[index-2];
369 dirtab_slot->flag = DIR_INDEX_VALID;
370 dirtab_slot->slot = slot;
371 DTSaddress(dirtab_slot, bn);
373 set_cflag(COMMIT_Dirtable, ip);
377 if (index == (MAX_INLINE_DIRTABLE_ENTRY + 1)) {
379 * It's time to move the inline table to an external
380 * page and begin to build the xtree
384 * Save the table, we're going to overwrite it with the
387 struct dir_table_slot temp_table[12];
388 memcpy(temp_table, &jfs_ip->i_dirtable, sizeof(temp_table));
391 * Initialize empty x-tree
396 * Allocate the first block & add it to the xtree
400 xtInsert(tid, ip, 0, 0, sbi->nbperpage,
402 jfs_warn("add_index: xtInsert failed!");
406 ip->i_blocks += LBLK2PBLK(sb, sbi->nbperpage);
408 if ((mp = get_index_page(ip, 0)) == 0) {
409 jfs_err("add_index: get_metapage failed!");
410 xtTruncate(tid, ip, 0, COMMIT_PWMAP);
413 tlck = txLock(tid, ip, mp, tlckDATA);
414 llck = (struct linelock *) & tlck->lock;
415 ASSERT(llck->index == 0);
419 lv->length = 6; /* tlckDATA slot size is 16 bytes */
422 memcpy(mp->data, temp_table, sizeof(temp_table));
424 mark_metapage_dirty(mp);
425 release_metapage(mp);
428 * Logging is now directed by xtree tlocks
430 clear_cflag(COMMIT_Dirtable, ip);
433 offset = (index - 2) * sizeof(struct dir_table_slot);
434 page_offset = offset & (PSIZE - 1);
435 blkno = ((offset + 1) >> L2PSIZE) << sbi->l2nbperpage;
436 if (page_offset == 0) {
438 * This will be the beginning of a new page
442 xtInsert(tid, ip, 0, blkno, sbi->nbperpage,
444 jfs_warn("add_index: xtInsert failed!");
445 jfs_ip->next_index--;
449 ip->i_blocks += LBLK2PBLK(sb, sbi->nbperpage);
451 if ((mp = get_index_page(ip, blkno)))
452 memset(mp->data, 0, PSIZE); /* Just looks better */
454 xtTruncate(tid, ip, offset, COMMIT_PWMAP);
456 mp = read_index_page(ip, blkno);
459 jfs_err("add_index: get/read_metapage failed!");
463 lock_index(tid, ip, mp, index);
466 (struct dir_table_slot *) ((char *) mp->data + page_offset);
467 dirtab_slot->flag = DIR_INDEX_VALID;
468 dirtab_slot->slot = slot;
469 DTSaddress(dirtab_slot, bn);
471 mark_metapage_dirty(mp);
472 release_metapage(mp);
480 * Marks an entry to the directory index table as free.
482 static void free_index(tid_t tid, struct inode *ip, u32 index, u32 next)
484 struct dir_table_slot *dirtab_slot;
486 struct metapage *mp = 0;
488 dirtab_slot = find_index(ip, index, &mp, &lblock);
490 if (dirtab_slot == 0)
493 dirtab_slot->flag = DIR_INDEX_FREE;
494 dirtab_slot->slot = dirtab_slot->addr1 = 0;
495 dirtab_slot->addr2 = cpu_to_le32(next);
498 lock_index(tid, ip, mp, index);
499 mark_metapage_dirty(mp);
500 release_metapage(mp);
502 set_cflag(COMMIT_Dirtable, ip);
508 * Changes an entry in the directory index table
510 static void modify_index(tid_t tid, struct inode *ip, u32 index, s64 bn,
511 int slot, struct metapage ** mp, u64 *lblock)
513 struct dir_table_slot *dirtab_slot;
515 dirtab_slot = find_index(ip, index, mp, lblock);
517 if (dirtab_slot == 0)
520 DTSaddress(dirtab_slot, bn);
521 dirtab_slot->slot = slot;
524 lock_index(tid, ip, *mp, index);
525 mark_metapage_dirty(*mp);
527 set_cflag(COMMIT_Dirtable, ip);
533 * reads a directory table slot
535 static int read_index(struct inode *ip, u32 index,
536 struct dir_table_slot * dirtab_slot)
539 struct metapage *mp = 0;
540 struct dir_table_slot *slot;
542 slot = find_index(ip, index, &mp, &lblock);
547 memcpy(dirtab_slot, slot, sizeof(struct dir_table_slot));
550 release_metapage(mp);
559 * Search for the entry with specified key
563 * return: 0 - search result on stack, leaf page pinned;
566 int dtSearch(struct inode *ip, struct component_name * key, ino_t * data,
567 struct btstack * btstack, int flag)
570 int cmp = 1; /* init for empty page */
575 int base, index, lim;
576 struct btframe *btsp;
578 int psize = 288; /* initial in-line directory */
580 struct component_name ciKey;
581 struct super_block *sb = ip->i_sb;
584 (wchar_t *) kmalloc((JFS_NAME_MAX + 1) * sizeof(wchar_t),
586 if (ciKey.name == 0) {
592 /* uppercase search key for c-i directory */
593 UniStrcpy(ciKey.name, key->name);
594 ciKey.namlen = key->namlen;
596 /* only uppercase if case-insensitive support is on */
597 if ((JFS_SBI(sb)->mntflag & JFS_OS2) == JFS_OS2) {
600 BT_CLR(btstack); /* reset stack */
602 /* init level count for max pages to split */
606 * search down tree from root:
608 * between two consecutive entries of <Ki, Pi> and <Kj, Pj> of
609 * internal page, child page Pi contains entry with k, Ki <= K < Kj.
611 * if entry with search key K is not found
612 * internal page search find the entry with largest key Ki
613 * less than K which point to the child page to search;
614 * leaf page search find the entry with smallest key Kj
615 * greater than K so that the returned index is the position of
616 * the entry to be shifted right for insertion of new entry.
617 * for empty tree, search key is greater than any key of the tree.
619 * by convention, root bn = 0.
622 /* get/pin the page to search */
623 DT_GETPAGE(ip, bn, mp, psize, p, rc);
627 /* get sorted entry table of the page */
628 stbl = DT_GETSTBL(p);
631 * binary search with search key K on the current page.
633 for (base = 0, lim = p->header.nextindex; lim; lim >>= 1) {
634 index = base + (lim >> 1);
636 if (p->header.flag & BT_LEAF) {
637 /* uppercase leaf name to compare */
639 ciCompare(&ciKey, p, stbl[index],
640 JFS_SBI(sb)->mntflag);
642 /* router key is in uppercase */
644 cmp = dtCompare(&ciKey, p, stbl[index]);
652 /* search hit - leaf page:
653 * return the entry found
655 if (p->header.flag & BT_LEAF) {
656 inumber = le32_to_cpu(
657 ((struct ldtentry *) & p->slot[stbl[index]])->inumber);
660 * search for JFS_LOOKUP
662 if (flag == JFS_LOOKUP) {
669 * search for JFS_CREATE
671 if (flag == JFS_CREATE) {
678 * search for JFS_REMOVE or JFS_RENAME
680 if ((flag == JFS_REMOVE ||
681 flag == JFS_RENAME) &&
688 * JFS_REMOVE|JFS_FINDDIR|JFS_RENAME
690 /* save search result */
701 /* search hit - internal page:
702 * descend/search its child page
716 * base is the smallest index with key (Kj) greater than
717 * search key (K) and may be zero or (maxindex + 1) index.
720 * search miss - leaf page
722 * return location of entry (base) where new entry with
723 * search key K is to be inserted.
725 if (p->header.flag & BT_LEAF) {
727 * search for JFS_LOOKUP, JFS_REMOVE, or JFS_RENAME
729 if (flag == JFS_LOOKUP || flag == JFS_REMOVE ||
730 flag == JFS_RENAME) {
736 * search for JFS_CREATE|JFS_FINDDIR:
751 * search miss - internal page
753 * if base is non-zero, decrement base by one to get the parent
754 * entry of the child page to search.
756 index = base ? base - 1 : base;
759 * go down to child page
762 /* update max. number of pages to split */
763 if (btstack->nsplit >= 8) {
764 /* Something's corrupted, mark filesytem dirty so
765 * chkdsk will fix it.
767 jfs_error(sb, "stack overrun in dtSearch!");
773 /* push (bn, index) of the parent page/entry */
774 BT_PUSH(btstack, bn, index);
776 /* get the child page block number */
777 pxd = (pxd_t *) & p->slot[stbl[index]];
778 bn = addressPXD(pxd);
779 psize = lengthPXD(pxd) << JFS_SBI(ip->i_sb)->l2bsize;
781 /* unpin the parent page */
801 * function: insert an entry to directory tree
805 * return: 0 - success;
808 int dtInsert(tid_t tid, struct inode *ip,
809 struct component_name * name, ino_t * fsn, struct btstack * btstack)
812 struct metapage *mp; /* meta-page buffer */
813 dtpage_t *p; /* base B+-tree index page */
816 struct dtsplit split; /* split information */
818 struct dt_lock *dtlck;
824 * retrieve search result
826 * dtSearch() returns (leaf page pinned, index at which to insert).
827 * n.b. dtSearch() may return index of (maxindex + 1) of
830 DT_GETSEARCH(ip, btstack->top, bn, mp, p, index);
833 * insert entry for new key
836 if (JFS_IP(ip)->next_index == DIREND) {
840 n = NDTLEAF(name->namlen);
844 n = NDTLEAF_LEGACY(name->namlen);
845 data.leaf.ip = 0; /* signifies legacy directory format */
847 data.leaf.ino = cpu_to_le32(*fsn);
850 * leaf page does not have enough room for new entry:
852 * extend/split the leaf page;
854 * dtSplitUp() will insert the entry and unpin the leaf page.
856 if (n > p->header.freecnt) {
862 rc = dtSplitUp(tid, ip, &split, btstack);
867 * leaf page does have enough room for new entry:
869 * insert the new data entry into the leaf page;
871 BT_MARK_DIRTY(mp, ip);
873 * acquire a transaction lock on the leaf page
875 tlck = txLock(tid, ip, mp, tlckDTREE | tlckENTRY);
876 dtlck = (struct dt_lock *) & tlck->lock;
877 ASSERT(dtlck->index == 0);
880 /* linelock header */
885 dtInsertEntry(p, index, name, &data, &dtlck);
887 /* linelock stbl of non-root leaf page */
888 if (!(p->header.flag & BT_ROOT)) {
889 if (dtlck->index >= dtlck->maxcnt)
890 dtlck = (struct dt_lock *) txLinelock(dtlck);
891 lv = & dtlck->lv[dtlck->index];
892 n = index >> L2DTSLOTSIZE;
893 lv->offset = p->header.stblindex + n;
895 ((p->header.nextindex - 1) >> L2DTSLOTSIZE) - n + 1;
899 /* unpin the leaf page */
909 * function: propagate insertion bottom up;
913 * return: 0 - success;
915 * leaf page unpinned;
917 static int dtSplitUp(tid_t tid,
918 struct inode *ip, struct dtsplit * split, struct btstack * btstack)
920 struct jfs_sb_info *sbi = JFS_SBI(ip->i_sb);
922 struct metapage *smp;
923 dtpage_t *sp; /* split page */
924 struct metapage *rmp;
925 dtpage_t *rp; /* new right page split from sp */
926 pxd_t rpxd; /* new right page extent descriptor */
927 struct metapage *lmp;
928 dtpage_t *lp; /* left child page */
929 int skip; /* index of entry of insertion */
930 struct btframe *parent; /* parent page entry on traverse stack */
933 struct pxdlist pxdlist;
935 struct component_name key = { 0, 0 };
936 ddata_t *data = split->data;
938 struct dt_lock *dtlck;
944 sp = DT_PAGE(ip, smp);
947 (wchar_t *) kmalloc((JFS_NAME_MAX + 2) * sizeof(wchar_t),
958 * The split routines insert the new entry, and
959 * acquire txLock as appropriate.
962 * split root leaf page:
964 if (sp->header.flag & BT_ROOT) {
966 * allocate a single extent child page
969 n = sbi->bsize >> L2DTSLOTSIZE;
970 n -= (n + 31) >> L2DTSLOTSIZE; /* stbl size */
971 n -= DTROOTMAXSLOT - sp->header.freecnt; /* header + entries */
972 if (n <= split->nslot)
974 if ((rc = dbAlloc(ip, 0, (s64) xlen, &xaddr)))
979 pxd = &pxdlist.pxd[0];
980 PXDaddress(pxd, xaddr);
981 PXDlength(pxd, xlen);
982 split->pxdlist = &pxdlist;
983 rc = dtSplitRoot(tid, ip, split, &rmp);
994 * extend first leaf page
996 * extend the 1st extent if less than buffer page size
997 * (dtExtendPage() reurns leaf page unpinned)
999 pxd = &sp->header.self;
1000 xlen = lengthPXD(pxd);
1001 xsize = xlen << sbi->l2bsize;
1002 if (xsize < PSIZE) {
1003 xaddr = addressPXD(pxd);
1004 n = xsize >> L2DTSLOTSIZE;
1005 n -= (n + 31) >> L2DTSLOTSIZE; /* stbl size */
1006 if ((n + sp->header.freecnt) <= split->nslot)
1007 n = xlen + (xlen << 1);
1010 if ((rc = dbReAlloc(sbi->ipbmap, xaddr, (s64) xlen,
1014 pxdlist.maxnpxd = 1;
1016 pxd = &pxdlist.pxd[0];
1017 PXDaddress(pxd, nxaddr)
1018 PXDlength(pxd, xlen + n);
1019 split->pxdlist = &pxdlist;
1020 if ((rc = dtExtendPage(tid, ip, split, btstack))) {
1021 nxaddr = addressPXD(pxd);
1022 if (xaddr != nxaddr) {
1023 /* free relocated extent */
1024 xlen = lengthPXD(pxd);
1025 dbFree(ip, nxaddr, (s64) xlen);
1027 /* free extended delta */
1028 xlen = lengthPXD(pxd) - n;
1029 xaddr = addressPXD(pxd) + xlen;
1030 dbFree(ip, xaddr, (s64) n);
1040 * split leaf page <sp> into <sp> and a new right page <rp>.
1042 * return <rp> pinned and its extent descriptor <rpxd>
1045 * allocate new directory page extent and
1046 * new index page(s) to cover page split(s)
1048 * allocation hint: ?
1050 n = btstack->nsplit;
1051 pxdlist.maxnpxd = pxdlist.npxd = 0;
1052 xlen = sbi->nbperpage;
1053 for (pxd = pxdlist.pxd; n > 0; n--, pxd++) {
1054 if ((rc = dbAlloc(ip, 0, (s64) xlen, &xaddr)) == 0) {
1055 PXDaddress(pxd, xaddr);
1056 PXDlength(pxd, xlen);
1063 /* undo allocation */
1067 split->pxdlist = &pxdlist;
1068 if ((rc = dtSplitPage(tid, ip, split, &rmp, &rp, &rpxd))) {
1071 /* undo allocation */
1076 * propagate up the router entry for the leaf page just split
1078 * insert a router entry for the new page into the parent page,
1079 * propagate the insert/split up the tree by walking back the stack
1080 * of (bn of parent page, index of child page entry in parent page)
1081 * that were traversed during the search for the page that split.
1083 * the propagation of insert/split up the tree stops if the root
1084 * splits or the page inserted into doesn't have to split to hold
1087 * the parent entry for the split page remains the same, and
1088 * a new entry is inserted at its right with the first key and
1089 * block number of the new right page.
1091 * There are a maximum of 4 pages pinned at any time:
1092 * two children, left parent and right parent (when the parent splits).
1093 * keep the child pages pinned while working on the parent.
1094 * make sure that all pins are released at exit.
1096 while ((parent = BT_POP(btstack)) != NULL) {
1097 /* parent page specified by stack frame <parent> */
1099 /* keep current child pages (<lp>, <rp>) pinned */
1104 * insert router entry in parent for new right child page <rp>
1106 /* get the parent page <sp> */
1107 DT_GETPAGE(ip, parent->bn, smp, PSIZE, sp, rc);
1115 * The new key entry goes ONE AFTER the index of parent entry,
1116 * because the split was to the right.
1118 skip = parent->index + 1;
1121 * compute the key for the router entry
1123 * key suffix compression:
1124 * for internal pages that have leaf pages as children,
1125 * retain only what's needed to distinguish between
1126 * the new entry and the entry on the page to its left.
1127 * If the keys compare equal, retain the entire key.
1129 * note that compression is performed only at computing
1130 * router key at the lowest internal level.
1131 * further compression of the key between pairs of higher
1132 * level internal pages loses too much information and
1133 * the search may fail.
1134 * (e.g., two adjacent leaf pages of {a, ..., x} {xx, ...,}
1135 * results in two adjacent parent entries (a)(xx).
1136 * if split occurs between these two entries, and
1137 * if compression is applied, the router key of parent entry
1138 * of right page (x) will divert search for x into right
1139 * subtree and miss x in the left subtree.)
1141 * the entire key must be retained for the next-to-leftmost
1142 * internal key at any level of the tree, or search may fail
1145 switch (rp->header.flag & BT_TYPE) {
1148 * compute the length of prefix for suffix compression
1149 * between last entry of left page and first entry
1152 if ((sp->header.flag & BT_ROOT && skip > 1) ||
1153 sp->header.prev != 0 || skip > 1) {
1154 /* compute uppercase router prefix key */
1155 ciGetLeafPrefixKey(lp,
1156 lp->header.nextindex - 1,
1157 rp, 0, &key, sbi->mntflag);
1159 /* next to leftmost entry of
1160 lowest internal level */
1162 /* compute uppercase router key */
1163 dtGetKey(rp, 0, &key, sbi->mntflag);
1164 key.name[key.namlen] = 0;
1166 if ((sbi->mntflag & JFS_OS2) == JFS_OS2)
1170 n = NDTINTERNAL(key.namlen);
1174 dtGetKey(rp, 0, &key, sbi->mntflag);
1175 n = NDTINTERNAL(key.namlen);
1179 jfs_err("dtSplitUp(): UFO!");
1183 /* unpin left child page */
1187 * compute the data for the router entry
1189 data->xd = rpxd; /* child page xd */
1192 * parent page is full - split the parent page
1194 if (n > sp->header.freecnt) {
1195 /* init for parent page split */
1197 split->index = skip; /* index at insert */
1200 /* split->data = data; */
1202 /* unpin right child page */
1205 /* The split routines insert the new entry,
1206 * acquire txLock as appropriate.
1207 * return <rp> pinned and its block number <rbn>.
1209 rc = (sp->header.flag & BT_ROOT) ?
1210 dtSplitRoot(tid, ip, split, &rmp) :
1211 dtSplitPage(tid, ip, split, &rmp, &rp, &rpxd);
1217 /* smp and rmp are pinned */
1220 * parent page is not full - insert router entry in parent page
1223 BT_MARK_DIRTY(smp, ip);
1225 * acquire a transaction lock on the parent page
1227 tlck = txLock(tid, ip, smp, tlckDTREE | tlckENTRY);
1228 dtlck = (struct dt_lock *) & tlck->lock;
1229 ASSERT(dtlck->index == 0);
1230 lv = & dtlck->lv[0];
1232 /* linelock header */
1237 /* linelock stbl of non-root parent page */
1238 if (!(sp->header.flag & BT_ROOT)) {
1240 n = skip >> L2DTSLOTSIZE;
1241 lv->offset = sp->header.stblindex + n;
1243 ((sp->header.nextindex -
1244 1) >> L2DTSLOTSIZE) - n + 1;
1248 dtInsertEntry(sp, skip, &key, data, &dtlck);
1250 /* exit propagate up */
1255 /* unpin current split and its right page */
1260 * free remaining extents allocated for split
1264 pxd = &pxdlist.pxd[n];
1265 for (; n < pxdlist.maxnpxd; n++, pxd++)
1266 dbFree(ip, addressPXD(pxd), (s64) lengthPXD(pxd));
1280 * function: Split a non-root page of a btree.
1284 * return: 0 - success;
1286 * return split and new page pinned;
1288 static int dtSplitPage(tid_t tid, struct inode *ip, struct dtsplit * split,
1289 struct metapage ** rmpp, dtpage_t ** rpp, pxd_t * rpxdp)
1291 struct super_block *sb = ip->i_sb;
1293 struct metapage *smp;
1295 struct metapage *rmp;
1296 dtpage_t *rp; /* new right page allocated */
1297 s64 rbn; /* new right page block number */
1298 struct metapage *mp;
1301 struct pxdlist *pxdlist;
1303 int skip, nextindex, half, left, nxt, off, si;
1304 struct ldtentry *ldtentry;
1305 struct idtentry *idtentry;
1310 struct dt_lock *sdtlck, *rdtlck;
1312 struct dt_lock *dtlck;
1313 struct lv *slv, *rlv, *lv;
1315 /* get split page */
1317 sp = DT_PAGE(ip, smp);
1320 * allocate the new right page for the split
1322 pxdlist = split->pxdlist;
1323 pxd = &pxdlist->pxd[pxdlist->npxd];
1325 rbn = addressPXD(pxd);
1326 rmp = get_metapage(ip, rbn, PSIZE, 1);
1330 jfs_info("dtSplitPage: ip:0x%p smp:0x%p rmp:0x%p", ip, smp, rmp);
1332 BT_MARK_DIRTY(rmp, ip);
1334 * acquire a transaction lock on the new right page
1336 tlck = txLock(tid, ip, rmp, tlckDTREE | tlckNEW);
1337 rdtlck = (struct dt_lock *) & tlck->lock;
1339 rp = (dtpage_t *) rmp->data;
1341 rp->header.self = *pxd;
1343 BT_MARK_DIRTY(smp, ip);
1345 * acquire a transaction lock on the split page
1349 tlck = txLock(tid, ip, smp, tlckDTREE | tlckENTRY);
1350 sdtlck = (struct dt_lock *) & tlck->lock;
1352 /* linelock header of split page */
1353 ASSERT(sdtlck->index == 0);
1354 slv = & sdtlck->lv[0];
1360 * initialize/update sibling pointers between sp and rp
1362 nextbn = le64_to_cpu(sp->header.next);
1363 rp->header.next = cpu_to_le64(nextbn);
1364 rp->header.prev = cpu_to_le64(addressPXD(&sp->header.self));
1365 sp->header.next = cpu_to_le64(rbn);
1368 * initialize new right page
1370 rp->header.flag = sp->header.flag;
1372 /* compute sorted entry table at start of extent data area */
1373 rp->header.nextindex = 0;
1374 rp->header.stblindex = 1;
1376 n = PSIZE >> L2DTSLOTSIZE;
1377 rp->header.maxslot = n;
1378 stblsize = (n + 31) >> L2DTSLOTSIZE; /* in unit of slot */
1381 fsi = rp->header.stblindex + stblsize;
1382 rp->header.freelist = fsi;
1383 rp->header.freecnt = rp->header.maxslot - fsi;
1386 * sequential append at tail: append without split
1388 * If splitting the last page on a level because of appending
1389 * a entry to it (skip is maxentry), it's likely that the access is
1390 * sequential. Adding an empty page on the side of the level is less
1391 * work and can push the fill factor much higher than normal.
1392 * If we're wrong it's no big deal, we'll just do the split the right
1394 * (It may look like it's equally easy to do a similar hack for
1395 * reverse sorted data, that is, split the tree left,
1396 * but it's not. Be my guest.)
1398 if (nextbn == 0 && split->index == sp->header.nextindex) {
1399 /* linelock header + stbl (first slot) of new page */
1400 rlv = & rdtlck->lv[rdtlck->index];
1406 * initialize freelist of new right page
1409 for (fsi++; fsi < rp->header.maxslot; f++, fsi++)
1413 /* insert entry at the first entry of the new right page */
1414 dtInsertEntry(rp, 0, split->key, split->data, &rdtlck);
1420 * non-sequential insert (at possibly middle page)
1424 * update prev pointer of previous right sibling page;
1427 DT_GETPAGE(ip, nextbn, mp, PSIZE, p, rc);
1429 discard_metapage(rmp);
1433 BT_MARK_DIRTY(mp, ip);
1435 * acquire a transaction lock on the next page
1437 tlck = txLock(tid, ip, mp, tlckDTREE | tlckRELINK);
1438 jfs_info("dtSplitPage: tlck = 0x%p, ip = 0x%p, mp=0x%p",
1440 dtlck = (struct dt_lock *) & tlck->lock;
1442 /* linelock header of previous right sibling page */
1443 lv = & dtlck->lv[dtlck->index];
1448 p->header.prev = cpu_to_le64(rbn);
1454 * split the data between the split and right pages.
1456 skip = split->index;
1457 half = (PSIZE >> L2DTSLOTSIZE) >> 1; /* swag */
1461 * compute fill factor for split pages
1463 * <nxt> traces the next entry to move to rp
1464 * <off> traces the next entry to stay in sp
1466 stbl = (u8 *) & sp->slot[sp->header.stblindex];
1467 nextindex = sp->header.nextindex;
1468 for (nxt = off = 0; nxt < nextindex; ++off) {
1470 /* check for fill factor with new entry size */
1474 switch (sp->header.flag & BT_TYPE) {
1476 ldtentry = (struct ldtentry *) & sp->slot[si];
1478 n = NDTLEAF(ldtentry->namlen);
1480 n = NDTLEAF_LEGACY(ldtentry->
1485 idtentry = (struct idtentry *) & sp->slot[si];
1486 n = NDTINTERNAL(idtentry->namlen);
1493 ++nxt; /* advance to next entry to move in sp */
1501 /* <nxt> poins to the 1st entry to move */
1504 * move entries to right page
1506 * dtMoveEntry() initializes rp and reserves entry for insertion
1508 * split page moved out entries are linelocked;
1509 * new/right page moved in entries are linelocked;
1511 /* linelock header + stbl of new right page */
1512 rlv = & rdtlck->lv[rdtlck->index];
1517 dtMoveEntry(sp, nxt, rp, &sdtlck, &rdtlck, DO_INDEX(ip));
1519 sp->header.nextindex = nxt;
1522 * finalize freelist of new right page
1524 fsi = rp->header.freelist;
1526 for (fsi++; fsi < rp->header.maxslot; f++, fsi++)
1531 * Update directory index table for entries now in right page
1533 if ((rp->header.flag & BT_LEAF) && DO_INDEX(ip)) {
1537 stbl = DT_GETSTBL(rp);
1538 for (n = 0; n < rp->header.nextindex; n++) {
1539 ldtentry = (struct ldtentry *) & rp->slot[stbl[n]];
1540 modify_index(tid, ip, le32_to_cpu(ldtentry->index),
1541 rbn, n, &mp, &lblock);
1544 release_metapage(mp);
1548 * the skipped index was on the left page,
1551 /* insert the new entry in the split page */
1552 dtInsertEntry(sp, skip, split->key, split->data, &sdtlck);
1554 /* linelock stbl of split page */
1555 if (sdtlck->index >= sdtlck->maxcnt)
1556 sdtlck = (struct dt_lock *) txLinelock(sdtlck);
1557 slv = & sdtlck->lv[sdtlck->index];
1558 n = skip >> L2DTSLOTSIZE;
1559 slv->offset = sp->header.stblindex + n;
1561 ((sp->header.nextindex - 1) >> L2DTSLOTSIZE) - n + 1;
1565 * the skipped index was on the right page,
1568 /* adjust the skip index to reflect the new position */
1571 /* insert the new entry in the right page */
1572 dtInsertEntry(rp, skip, split->key, split->data, &rdtlck);
1579 ip->i_blocks += LBLK2PBLK(sb, lengthPXD(pxd));
1588 * function: extend 1st/only directory leaf page
1592 * return: 0 - success;
1594 * return extended page pinned;
1596 static int dtExtendPage(tid_t tid,
1597 struct inode *ip, struct dtsplit * split, struct btstack * btstack)
1599 struct super_block *sb = ip->i_sb;
1601 struct metapage *smp, *pmp, *mp;
1603 struct pxdlist *pxdlist;
1606 int newstblindex, newstblsize;
1607 int oldstblindex, oldstblsize;
1610 struct btframe *parent;
1612 struct dt_lock *dtlck;
1615 struct pxd_lock *pxdlock;
1618 struct ldtentry *ldtentry;
1621 /* get page to extend */
1623 sp = DT_PAGE(ip, smp);
1625 /* get parent/root page */
1626 parent = BT_POP(btstack);
1627 DT_GETPAGE(ip, parent->bn, pmp, PSIZE, pp, rc);
1634 pxdlist = split->pxdlist;
1635 pxd = &pxdlist->pxd[pxdlist->npxd];
1638 xaddr = addressPXD(pxd);
1639 tpxd = &sp->header.self;
1640 txaddr = addressPXD(tpxd);
1641 /* in-place extension */
1642 if (xaddr == txaddr) {
1649 /* save moved extent descriptor for later free */
1650 tlck = txMaplock(tid, ip, tlckDTREE | tlckRELOCATE);
1651 pxdlock = (struct pxd_lock *) & tlck->lock;
1652 pxdlock->flag = mlckFREEPXD;
1653 pxdlock->pxd = sp->header.self;
1657 * Update directory index table to reflect new page address
1663 stbl = DT_GETSTBL(sp);
1664 for (n = 0; n < sp->header.nextindex; n++) {
1666 (struct ldtentry *) & sp->slot[stbl[n]];
1667 modify_index(tid, ip,
1668 le32_to_cpu(ldtentry->index),
1669 xaddr, n, &mp, &lblock);
1672 release_metapage(mp);
1679 sp->header.self = *pxd;
1681 jfs_info("dtExtendPage: ip:0x%p smp:0x%p sp:0x%p", ip, smp, sp);
1683 BT_MARK_DIRTY(smp, ip);
1685 * acquire a transaction lock on the extended/leaf page
1687 tlck = txLock(tid, ip, smp, tlckDTREE | type);
1688 dtlck = (struct dt_lock *) & tlck->lock;
1689 lv = & dtlck->lv[0];
1691 /* update buffer extent descriptor of extended page */
1692 xlen = lengthPXD(pxd);
1693 xsize = xlen << JFS_SBI(sb)->l2bsize;
1694 #ifdef _STILL_TO_PORT
1695 bmSetXD(smp, xaddr, xsize);
1696 #endif /* _STILL_TO_PORT */
1699 * copy old stbl to new stbl at start of extended area
1701 oldstblindex = sp->header.stblindex;
1702 oldstblsize = (sp->header.maxslot + 31) >> L2DTSLOTSIZE;
1703 newstblindex = sp->header.maxslot;
1704 n = xsize >> L2DTSLOTSIZE;
1705 newstblsize = (n + 31) >> L2DTSLOTSIZE;
1706 memcpy(&sp->slot[newstblindex], &sp->slot[oldstblindex],
1707 sp->header.nextindex);
1710 * in-line extension: linelock old area of extended page
1712 if (type == tlckEXTEND) {
1713 /* linelock header */
1719 /* linelock new stbl of extended page */
1720 lv->offset = newstblindex;
1721 lv->length = newstblsize;
1724 * relocation: linelock whole relocated area
1728 lv->length = sp->header.maxslot + newstblsize;
1733 sp->header.maxslot = n;
1734 sp->header.stblindex = newstblindex;
1735 /* sp->header.nextindex remains the same */
1738 * add old stbl region at head of freelist
1742 last = sp->header.freelist;
1743 for (n = 0; n < oldstblsize; n++, fsi++, f++) {
1747 sp->header.freelist = last;
1748 sp->header.freecnt += oldstblsize;
1751 * append free region of newly extended area at tail of freelist
1753 /* init free region of newly extended area */
1754 fsi = n = newstblindex + newstblsize;
1756 for (fsi++; fsi < sp->header.maxslot; f++, fsi++)
1760 /* append new free region at tail of old freelist */
1761 fsi = sp->header.freelist;
1763 sp->header.freelist = n;
1768 } while (fsi != -1);
1773 sp->header.freecnt += sp->header.maxslot - n;
1776 * insert the new entry
1778 dtInsertEntry(sp, split->index, split->key, split->data, &dtlck);
1780 BT_MARK_DIRTY(pmp, ip);
1782 * linelock any freeslots residing in old extent
1784 if (type == tlckEXTEND) {
1785 n = sp->header.maxslot >> 2;
1786 if (sp->header.freelist < n)
1787 dtLinelockFreelist(sp, n, &dtlck);
1791 * update parent entry on the parent/root page
1794 * acquire a transaction lock on the parent/root page
1796 tlck = txLock(tid, ip, pmp, tlckDTREE | tlckENTRY);
1797 dtlck = (struct dt_lock *) & tlck->lock;
1798 lv = & dtlck->lv[dtlck->index];
1800 /* linelock parent entry - 1st slot */
1805 /* update the parent pxd for page extension */
1806 tpxd = (pxd_t *) & pp->slot[1];
1809 /* Since the directory might have an EA and/or ACL associated with it
1810 * we need to make sure we take that into account when setting the
1813 ip->i_blocks = LBLK2PBLK(ip->i_sb, xlen +
1814 ((JFS_IP(ip)->ea.flag & DXD_EXTENT) ?
1815 lengthDXD(&JFS_IP(ip)->ea) : 0) +
1816 ((JFS_IP(ip)->acl.flag & DXD_EXTENT) ?
1817 lengthDXD(&JFS_IP(ip)->acl) : 0));
1828 * split the full root page into
1829 * original/root/split page and new right page
1830 * i.e., root remains fixed in tree anchor (inode) and
1831 * the root is copied to a single new right child page
1832 * since root page << non-root page, and
1833 * the split root page contains a single entry for the
1834 * new right child page.
1838 * return: 0 - success;
1840 * return new page pinned;
1842 static int dtSplitRoot(tid_t tid,
1843 struct inode *ip, struct dtsplit * split, struct metapage ** rmpp)
1845 struct super_block *sb = ip->i_sb;
1846 struct metapage *smp;
1848 struct metapage *rmp;
1855 int fsi, stblsize, n;
1858 struct pxdlist *pxdlist;
1860 struct dt_lock *dtlck;
1864 /* get split root page */
1866 sp = &JFS_IP(ip)->i_dtroot;
1869 * allocate/initialize a single (right) child page
1871 * N.B. at first split, a one (or two) block to fit new entry
1872 * is allocated; at subsequent split, a full page is allocated;
1874 pxdlist = split->pxdlist;
1875 pxd = &pxdlist->pxd[pxdlist->npxd];
1877 rbn = addressPXD(pxd);
1878 xlen = lengthPXD(pxd);
1879 xsize = xlen << JFS_SBI(sb)->l2bsize;
1880 rmp = get_metapage(ip, rbn, xsize, 1);
1886 BT_MARK_DIRTY(rmp, ip);
1888 * acquire a transaction lock on the new right page
1890 tlck = txLock(tid, ip, rmp, tlckDTREE | tlckNEW);
1891 dtlck = (struct dt_lock *) & tlck->lock;
1894 (sp->header.flag & BT_LEAF) ? BT_LEAF : BT_INTERNAL;
1895 rp->header.self = *pxd;
1897 /* initialize sibling pointers */
1898 rp->header.next = 0;
1899 rp->header.prev = 0;
1902 * move in-line root page into new right page extent
1904 /* linelock header + copied entries + new stbl (1st slot) in new page */
1905 ASSERT(dtlck->index == 0);
1906 lv = & dtlck->lv[0];
1908 lv->length = 10; /* 1 + 8 + 1 */
1911 n = xsize >> L2DTSLOTSIZE;
1912 rp->header.maxslot = n;
1913 stblsize = (n + 31) >> L2DTSLOTSIZE;
1915 /* copy old stbl to new stbl at start of extended area */
1916 rp->header.stblindex = DTROOTMAXSLOT;
1917 stbl = (s8 *) & rp->slot[DTROOTMAXSLOT];
1918 memcpy(stbl, sp->header.stbl, sp->header.nextindex);
1919 rp->header.nextindex = sp->header.nextindex;
1921 /* copy old data area to start of new data area */
1922 memcpy(&rp->slot[1], &sp->slot[1], IDATASIZE);
1925 * append free region of newly extended area at tail of freelist
1927 /* init free region of newly extended area */
1928 fsi = n = DTROOTMAXSLOT + stblsize;
1930 for (fsi++; fsi < rp->header.maxslot; f++, fsi++)
1934 /* append new free region at tail of old freelist */
1935 fsi = sp->header.freelist;
1937 rp->header.freelist = n;
1939 rp->header.freelist = fsi;
1944 } while (fsi != -1);
1949 rp->header.freecnt = sp->header.freecnt + rp->header.maxslot - n;
1952 * Update directory index table for entries now in right page
1954 if ((rp->header.flag & BT_LEAF) && DO_INDEX(ip)) {
1956 struct metapage *mp = 0;
1957 struct ldtentry *ldtentry;
1959 stbl = DT_GETSTBL(rp);
1960 for (n = 0; n < rp->header.nextindex; n++) {
1961 ldtentry = (struct ldtentry *) & rp->slot[stbl[n]];
1962 modify_index(tid, ip, le32_to_cpu(ldtentry->index),
1963 rbn, n, &mp, &lblock);
1966 release_metapage(mp);
1969 * insert the new entry into the new right/child page
1970 * (skip index in the new right page will not change)
1972 dtInsertEntry(rp, split->index, split->key, split->data, &dtlck);
1975 * reset parent/root page
1977 * set the 1st entry offset to 0, which force the left-most key
1978 * at any level of the tree to be less than any search key.
1980 * The btree comparison code guarantees that the left-most key on any
1981 * level of the tree is never used, so it doesn't need to be filled in.
1983 BT_MARK_DIRTY(smp, ip);
1985 * acquire a transaction lock on the root page (in-memory inode)
1987 tlck = txLock(tid, ip, smp, tlckDTREE | tlckNEW | tlckBTROOT);
1988 dtlck = (struct dt_lock *) & tlck->lock;
1991 ASSERT(dtlck->index == 0);
1992 lv = & dtlck->lv[0];
1994 lv->length = DTROOTMAXSLOT;
1997 /* update page header of root */
1998 if (sp->header.flag & BT_LEAF) {
1999 sp->header.flag &= ~BT_LEAF;
2000 sp->header.flag |= BT_INTERNAL;
2003 /* init the first entry */
2004 s = (struct idtentry *) & sp->slot[DTENTRYSTART];
2010 stbl = sp->header.stbl;
2011 stbl[0] = DTENTRYSTART;
2012 sp->header.nextindex = 1;
2015 fsi = DTENTRYSTART + 1;
2018 /* init free region of remaining area */
2019 for (fsi++; fsi < DTROOTMAXSLOT; f++, fsi++)
2023 sp->header.freelist = DTENTRYSTART + 1;
2024 sp->header.freecnt = DTROOTMAXSLOT - (DTENTRYSTART + 1);
2028 ip->i_blocks += LBLK2PBLK(ip->i_sb, lengthPXD(pxd));
2036 * function: delete the entry(s) referenced by a key.
2042 int dtDelete(tid_t tid,
2043 struct inode *ip, struct component_name * key, ino_t * ino, int flag)
2047 struct metapage *mp, *imp;
2050 struct btstack btstack;
2051 struct dt_lock *dtlck;
2055 struct ldtentry *ldtentry;
2057 u32 table_index, next_index;
2058 struct metapage *nmp;
2062 * search for the entry to delete:
2064 * dtSearch() returns (leaf page pinned, index at which to delete).
2066 if ((rc = dtSearch(ip, key, ino, &btstack, flag)))
2069 /* retrieve search result */
2070 DT_GETSEARCH(ip, btstack.top, bn, mp, p, index);
2073 * We need to find put the index of the next entry into the
2074 * directory index table in order to resume a readdir from this
2078 stbl = DT_GETSTBL(p);
2079 ldtentry = (struct ldtentry *) & p->slot[stbl[index]];
2080 table_index = le32_to_cpu(ldtentry->index);
2081 if (index == (p->header.nextindex - 1)) {
2083 * Last entry in this leaf page
2085 if ((p->header.flag & BT_ROOT)
2086 || (p->header.next == 0))
2089 /* Read next leaf page */
2090 DT_GETPAGE(ip, le64_to_cpu(p->header.next),
2091 nmp, PSIZE, np, rc);
2095 stbl = DT_GETSTBL(np);
2097 (struct ldtentry *) & np->
2100 le32_to_cpu(ldtentry->index);
2106 (struct ldtentry *) & p->slot[stbl[index + 1]];
2107 next_index = le32_to_cpu(ldtentry->index);
2109 free_index(tid, ip, table_index, next_index);
2112 * the leaf page becomes empty, delete the page
2114 if (p->header.nextindex == 1) {
2115 /* delete empty page */
2116 rc = dtDeleteUp(tid, ip, mp, p, &btstack);
2119 * the leaf page has other entries remaining:
2121 * delete the entry from the leaf page.
2124 BT_MARK_DIRTY(mp, ip);
2126 * acquire a transaction lock on the leaf page
2128 tlck = txLock(tid, ip, mp, tlckDTREE | tlckENTRY);
2129 dtlck = (struct dt_lock *) & tlck->lock;
2132 * Do not assume that dtlck->index will be zero. During a
2133 * rename within a directory, this transaction may have
2134 * modified this page already when adding the new entry.
2137 /* linelock header */
2138 if (dtlck->index >= dtlck->maxcnt)
2139 dtlck = (struct dt_lock *) txLinelock(dtlck);
2140 lv = & dtlck->lv[dtlck->index];
2145 /* linelock stbl of non-root leaf page */
2146 if (!(p->header.flag & BT_ROOT)) {
2147 if (dtlck->index >= dtlck->maxcnt)
2148 dtlck = (struct dt_lock *) txLinelock(dtlck);
2149 lv = & dtlck->lv[dtlck->index];
2150 i = index >> L2DTSLOTSIZE;
2151 lv->offset = p->header.stblindex + i;
2153 ((p->header.nextindex - 1) >> L2DTSLOTSIZE) -
2158 /* free the leaf entry */
2159 dtDeleteEntry(p, index, &dtlck);
2162 * Update directory index table for entries moved in stbl
2164 if (DO_INDEX(ip) && index < p->header.nextindex) {
2168 stbl = DT_GETSTBL(p);
2169 for (i = index; i < p->header.nextindex; i++) {
2171 (struct ldtentry *) & p->slot[stbl[i]];
2172 modify_index(tid, ip,
2173 le32_to_cpu(ldtentry->index),
2174 bn, i, &imp, &lblock);
2177 release_metapage(imp);
2191 * free empty pages as propagating deletion up the tree
2197 static int dtDeleteUp(tid_t tid, struct inode *ip,
2198 struct metapage * fmp, dtpage_t * fp, struct btstack * btstack)
2201 struct metapage *mp;
2203 int index, nextindex;
2205 struct btframe *parent;
2206 struct dt_lock *dtlck;
2209 struct pxd_lock *pxdlock;
2213 * keep the root leaf page which has become empty
2215 if (BT_IS_ROOT(fmp)) {
2219 * dtInitRoot() acquires txlock on the root
2221 dtInitRoot(tid, ip, PARENT(ip));
2229 * free the non-root leaf page
2232 * acquire a transaction lock on the page
2234 * write FREEXTENT|NOREDOPAGE log record
2235 * N.B. linelock is overlaid as freed extent descriptor, and
2236 * the buffer page is freed;
2238 tlck = txMaplock(tid, ip, tlckDTREE | tlckFREE);
2239 pxdlock = (struct pxd_lock *) & tlck->lock;
2240 pxdlock->flag = mlckFREEPXD;
2241 pxdlock->pxd = fp->header.self;
2244 /* update sibling pointers */
2245 if ((rc = dtRelink(tid, ip, fp))) {
2250 xlen = lengthPXD(&fp->header.self);
2251 ip->i_blocks -= LBLK2PBLK(ip->i_sb, xlen);
2253 /* free/invalidate its buffer page */
2254 discard_metapage(fmp);
2257 * propagate page deletion up the directory tree
2259 * If the delete from the parent page makes it empty,
2260 * continue all the way up the tree.
2261 * stop if the root page is reached (which is never deleted) or
2262 * if the entry deletion does not empty the page.
2264 while ((parent = BT_POP(btstack)) != NULL) {
2265 /* pin the parent page <sp> */
2266 DT_GETPAGE(ip, parent->bn, mp, PSIZE, p, rc);
2271 * free the extent of the child page deleted
2273 index = parent->index;
2276 * delete the entry for the child page from parent
2278 nextindex = p->header.nextindex;
2281 * the parent has the single entry being deleted:
2283 * free the parent page which has become empty.
2285 if (nextindex == 1) {
2287 * keep the root internal page which has become empty
2289 if (p->header.flag & BT_ROOT) {
2293 * dtInitRoot() acquires txlock on the root
2295 dtInitRoot(tid, ip, PARENT(ip));
2302 * free the parent page
2306 * acquire a transaction lock on the page
2308 * write FREEXTENT|NOREDOPAGE log record
2312 tlckDTREE | tlckFREE);
2313 pxdlock = (struct pxd_lock *) & tlck->lock;
2314 pxdlock->flag = mlckFREEPXD;
2315 pxdlock->pxd = p->header.self;
2318 /* update sibling pointers */
2319 if ((rc = dtRelink(tid, ip, p))) {
2324 xlen = lengthPXD(&p->header.self);
2325 ip->i_blocks -= LBLK2PBLK(ip->i_sb, xlen);
2327 /* free/invalidate its buffer page */
2328 discard_metapage(mp);
2336 * the parent has other entries remaining:
2338 * delete the router entry from the parent page.
2340 BT_MARK_DIRTY(mp, ip);
2342 * acquire a transaction lock on the page
2344 * action: router entry deletion
2346 tlck = txLock(tid, ip, mp, tlckDTREE | tlckENTRY);
2347 dtlck = (struct dt_lock *) & tlck->lock;
2349 /* linelock header */
2350 if (dtlck->index >= dtlck->maxcnt)
2351 dtlck = (struct dt_lock *) txLinelock(dtlck);
2352 lv = & dtlck->lv[dtlck->index];
2357 /* linelock stbl of non-root leaf page */
2358 if (!(p->header.flag & BT_ROOT)) {
2359 if (dtlck->index < dtlck->maxcnt)
2362 dtlck = (struct dt_lock *) txLinelock(dtlck);
2363 lv = & dtlck->lv[0];
2365 i = index >> L2DTSLOTSIZE;
2366 lv->offset = p->header.stblindex + i;
2368 ((p->header.nextindex - 1) >> L2DTSLOTSIZE) -
2373 /* free the router entry */
2374 dtDeleteEntry(p, index, &dtlck);
2376 /* reset key of new leftmost entry of level (for consistency) */
2378 ((p->header.flag & BT_ROOT) || p->header.prev == 0))
2379 dtTruncateEntry(p, 0, &dtlck);
2381 /* unpin the parent page */
2384 /* exit propagation up */
2393 * NAME: dtRelocate()
2395 * FUNCTION: relocate dtpage (internal or leaf) of directory;
2396 * This function is mainly used by defragfs utility.
2398 int dtRelocate(tid_t tid, struct inode *ip, s64 lmxaddr, pxd_t * opxd,
2402 struct metapage *mp, *pmp, *lmp, *rmp;
2403 dtpage_t *p, *pp, *rp = 0, *lp= 0;
2406 struct btstack btstack;
2408 s64 oxaddr, nextbn, prevbn;
2411 struct dt_lock *dtlck;
2412 struct pxd_lock *pxdlock;
2416 oxaddr = addressPXD(opxd);
2417 xlen = lengthPXD(opxd);
2419 jfs_info("dtRelocate: lmxaddr:%Ld xaddr:%Ld:%Ld xlen:%d",
2420 (long long)lmxaddr, (long long)oxaddr, (long long)nxaddr,
2424 * 1. get the internal parent dtpage covering
2425 * router entry for the tartget page to be relocated;
2427 rc = dtSearchNode(ip, lmxaddr, opxd, &btstack);
2431 /* retrieve search result */
2432 DT_GETSEARCH(ip, btstack.top, bn, pmp, pp, index);
2433 jfs_info("dtRelocate: parent router entry validated.");
2436 * 2. relocate the target dtpage
2438 /* read in the target page from src extent */
2439 DT_GETPAGE(ip, oxaddr, mp, PSIZE, p, rc);
2441 /* release the pinned parent page */
2447 * read in sibling pages if any to update sibling pointers;
2450 if (p->header.next) {
2451 nextbn = le64_to_cpu(p->header.next);
2452 DT_GETPAGE(ip, nextbn, rmp, PSIZE, rp, rc);
2461 if (p->header.prev) {
2462 prevbn = le64_to_cpu(p->header.prev);
2463 DT_GETPAGE(ip, prevbn, lmp, PSIZE, lp, rc);
2473 /* at this point, all xtpages to be updated are in memory */
2476 * update sibling pointers of sibling dtpages if any;
2479 tlck = txLock(tid, ip, lmp, tlckDTREE | tlckRELINK);
2480 dtlck = (struct dt_lock *) & tlck->lock;
2481 /* linelock header */
2482 ASSERT(dtlck->index == 0);
2483 lv = & dtlck->lv[0];
2488 lp->header.next = cpu_to_le64(nxaddr);
2493 tlck = txLock(tid, ip, rmp, tlckDTREE | tlckRELINK);
2494 dtlck = (struct dt_lock *) & tlck->lock;
2495 /* linelock header */
2496 ASSERT(dtlck->index == 0);
2497 lv = & dtlck->lv[0];
2502 rp->header.prev = cpu_to_le64(nxaddr);
2507 * update the target dtpage to be relocated
2509 * write LOG_REDOPAGE of LOG_NEW type for dst page
2510 * for the whole target page (logredo() will apply
2511 * after image and update bmap for allocation of the
2512 * dst extent), and update bmap for allocation of
2515 tlck = txLock(tid, ip, mp, tlckDTREE | tlckNEW);
2516 dtlck = (struct dt_lock *) & tlck->lock;
2517 /* linelock header */
2518 ASSERT(dtlck->index == 0);
2519 lv = & dtlck->lv[0];
2521 /* update the self address in the dtpage header */
2522 pxd = &p->header.self;
2523 PXDaddress(pxd, nxaddr);
2525 /* the dst page is the same as the src page, i.e.,
2526 * linelock for afterimage of the whole page;
2529 lv->length = p->header.maxslot;
2532 /* update the buffer extent descriptor of the dtpage */
2533 xsize = xlen << JFS_SBI(ip->i_sb)->l2bsize;
2534 #ifdef _STILL_TO_PORT
2535 bmSetXD(mp, nxaddr, xsize);
2536 #endif /* _STILL_TO_PORT */
2537 /* unpin the relocated page */
2539 jfs_info("dtRelocate: target dtpage relocated.");
2541 /* the moved extent is dtpage, then a LOG_NOREDOPAGE log rec
2542 * needs to be written (in logredo(), the LOG_NOREDOPAGE log rec
2543 * will also force a bmap update ).
2547 * 3. acquire maplock for the source extent to be freed;
2549 /* for dtpage relocation, write a LOG_NOREDOPAGE record
2550 * for the source dtpage (logredo() will init NoRedoPage
2551 * filter and will also update bmap for free of the source
2552 * dtpage), and upadte bmap for free of the source dtpage;
2554 tlck = txMaplock(tid, ip, tlckDTREE | tlckFREE);
2555 pxdlock = (struct pxd_lock *) & tlck->lock;
2556 pxdlock->flag = mlckFREEPXD;
2557 PXDaddress(&pxdlock->pxd, oxaddr);
2558 PXDlength(&pxdlock->pxd, xlen);
2562 * 4. update the parent router entry for relocation;
2564 * acquire tlck for the parent entry covering the target dtpage;
2565 * write LOG_REDOPAGE to apply after image only;
2567 jfs_info("dtRelocate: update parent router entry.");
2568 tlck = txLock(tid, ip, pmp, tlckDTREE | tlckENTRY);
2569 dtlck = (struct dt_lock *) & tlck->lock;
2570 lv = & dtlck->lv[dtlck->index];
2572 /* update the PXD with the new address */
2573 stbl = DT_GETSTBL(pp);
2574 pxd = (pxd_t *) & pp->slot[stbl[index]];
2575 PXDaddress(pxd, nxaddr);
2576 lv->offset = stbl[index];
2580 /* unpin the parent dtpage */
2587 * NAME: dtSearchNode()
2589 * FUNCTION: Search for an dtpage containing a specified address
2590 * This function is mainly used by defragfs utility.
2592 * NOTE: Search result on stack, the found page is pinned at exit.
2593 * The result page must be an internal dtpage.
2594 * lmxaddr give the address of the left most page of the
2595 * dtree level, in which the required dtpage resides.
2597 static int dtSearchNode(struct inode *ip, s64 lmxaddr, pxd_t * kpxd,
2598 struct btstack * btstack)
2602 struct metapage *mp;
2604 int psize = 288; /* initial in-line directory */
2608 struct btframe *btsp;
2610 BT_CLR(btstack); /* reset stack */
2613 * descend tree to the level with specified leftmost page
2615 * by convention, root bn = 0.
2618 /* get/pin the page to search */
2619 DT_GETPAGE(ip, bn, mp, psize, p, rc);
2623 /* does the xaddr of leftmost page of the levevl
2624 * matches levevl search key ?
2626 if (p->header.flag & BT_ROOT) {
2629 } else if (addressPXD(&p->header.self) == lmxaddr)
2633 * descend down to leftmost child page
2635 if (p->header.flag & BT_LEAF) {
2640 /* get the leftmost entry */
2641 stbl = DT_GETSTBL(p);
2642 pxd = (pxd_t *) & p->slot[stbl[0]];
2644 /* get the child page block address */
2645 bn = addressPXD(pxd);
2646 psize = lengthPXD(pxd) << JFS_SBI(ip->i_sb)->l2bsize;
2647 /* unpin the parent page */
2652 * search each page at the current levevl
2655 stbl = DT_GETSTBL(p);
2656 for (i = 0; i < p->header.nextindex; i++) {
2657 pxd = (pxd_t *) & p->slot[stbl[i]];
2659 /* found the specified router entry */
2660 if (addressPXD(pxd) == addressPXD(kpxd) &&
2661 lengthPXD(pxd) == lengthPXD(kpxd)) {
2662 btsp = btstack->top;
2671 /* get the right sibling page if any */
2673 bn = le64_to_cpu(p->header.next);
2679 /* unpin current page */
2682 /* get the right sibling page */
2683 DT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
2689 #endif /* _NOTYET */
2695 * link around a freed page.
2698 * fp: page to be freed
2702 static int dtRelink(tid_t tid, struct inode *ip, dtpage_t * p)
2705 struct metapage *mp;
2708 struct dt_lock *dtlck;
2711 nextbn = le64_to_cpu(p->header.next);
2712 prevbn = le64_to_cpu(p->header.prev);
2714 /* update prev pointer of the next page */
2716 DT_GETPAGE(ip, nextbn, mp, PSIZE, p, rc);
2720 BT_MARK_DIRTY(mp, ip);
2722 * acquire a transaction lock on the next page
2724 * action: update prev pointer;
2726 tlck = txLock(tid, ip, mp, tlckDTREE | tlckRELINK);
2727 jfs_info("dtRelink nextbn: tlck = 0x%p, ip = 0x%p, mp=0x%p",
2729 dtlck = (struct dt_lock *) & tlck->lock;
2731 /* linelock header */
2732 if (dtlck->index >= dtlck->maxcnt)
2733 dtlck = (struct dt_lock *) txLinelock(dtlck);
2734 lv = & dtlck->lv[dtlck->index];
2739 p->header.prev = cpu_to_le64(prevbn);
2743 /* update next pointer of the previous page */
2745 DT_GETPAGE(ip, prevbn, mp, PSIZE, p, rc);
2749 BT_MARK_DIRTY(mp, ip);
2751 * acquire a transaction lock on the prev page
2753 * action: update next pointer;
2755 tlck = txLock(tid, ip, mp, tlckDTREE | tlckRELINK);
2756 jfs_info("dtRelink prevbn: tlck = 0x%p, ip = 0x%p, mp=0x%p",
2758 dtlck = (struct dt_lock *) & tlck->lock;
2760 /* linelock header */
2761 if (dtlck->index >= dtlck->maxcnt)
2762 dtlck = (struct dt_lock *) txLinelock(dtlck);
2763 lv = & dtlck->lv[dtlck->index];
2768 p->header.next = cpu_to_le64(nextbn);
2779 * initialize directory root (inline in inode)
2781 void dtInitRoot(tid_t tid, struct inode *ip, u32 idotdot)
2783 struct jfs_inode_info *jfs_ip = JFS_IP(ip);
2788 struct dt_lock *dtlck;
2793 * If this was previously an non-empty directory, we need to remove
2794 * the old directory table.
2797 if (jfs_ip->next_index > (MAX_INLINE_DIRTABLE_ENTRY + 1)) {
2798 struct tblock *tblk = tid_to_tblock(tid);
2800 * We're playing games with the tid's xflag. If
2801 * we're removing a regular file, the file's xtree
2802 * is committed with COMMIT_PMAP, but we always
2803 * commit the directories xtree with COMMIT_PWMAP.
2805 xflag_save = tblk->xflag;
2808 * xtTruncate isn't guaranteed to fully truncate
2809 * the xtree. The caller needs to check i_size
2810 * after committing the transaction to see if
2811 * additional truncation is needed. The
2812 * COMMIT_Stale flag tells caller that we
2813 * initiated the truncation.
2815 xtTruncate(tid, ip, 0, COMMIT_PWMAP);
2816 set_cflag(COMMIT_Stale, ip);
2818 tblk->xflag = xflag_save;
2822 jfs_ip->next_index = 2;
2824 ip->i_size = IDATASIZE;
2827 * acquire a transaction lock on the root
2829 * action: directory initialization;
2831 tlck = txLock(tid, ip, (struct metapage *) & jfs_ip->bxflag,
2832 tlckDTREE | tlckENTRY | tlckBTROOT);
2833 dtlck = (struct dt_lock *) & tlck->lock;
2836 ASSERT(dtlck->index == 0);
2837 lv = & dtlck->lv[0];
2839 lv->length = DTROOTMAXSLOT;
2842 p = &jfs_ip->i_dtroot;
2844 p->header.flag = DXD_INDEX | BT_ROOT | BT_LEAF;
2846 p->header.nextindex = 0;
2852 /* init data area of root */
2853 for (fsi++; fsi < DTROOTMAXSLOT; f++, fsi++)
2857 p->header.freelist = 1;
2858 p->header.freecnt = 8;
2860 /* init '..' entry */
2861 p->header.idotdot = cpu_to_le32(idotdot);
2864 ip->i_blocks = LBLK2PBLK(ip->i_sb,
2865 ((jfs_ip->ea.flag & DXD_EXTENT) ?
2866 lengthDXD(&jfs_ip->ea) : 0) +
2867 ((jfs_ip->acl.flag & DXD_EXTENT) ?
2868 lengthDXD(&jfs_ip->acl) : 0));
2875 * add_missing_indices()
2877 * function: Fix dtree page in which one or more entries has an invalid index.
2878 * fsck.jfs should really fix this, but it currently does not.
2879 * Called from jfs_readdir when bad index is detected.
2881 static void add_missing_indices(struct inode *inode, s64 bn)
2884 struct dt_lock *dtlck;
2888 struct metapage *mp;
2895 tid = txBegin(inode->i_sb, 0);
2897 DT_GETPAGE(inode, bn, mp, PSIZE, p, rc);
2900 printk(KERN_ERR "DT_GETPAGE failed!\n");
2903 BT_MARK_DIRTY(mp, inode);
2905 ASSERT(p->header.flag & BT_LEAF);
2907 tlck = txLock(tid, inode, mp, tlckDTREE | tlckENTRY);
2908 dtlck = (struct dt_lock *) &tlck->lock;
2910 stbl = DT_GETSTBL(p);
2911 for (i = 0; i < p->header.nextindex; i++) {
2912 d = (struct ldtentry *) &p->slot[stbl[i]];
2913 index = le32_to_cpu(d->index);
2914 if ((index < 2) || (index >= JFS_IP(inode)->next_index)) {
2915 d->index = cpu_to_le32(add_index(tid, inode, bn, i));
2916 if (dtlck->index >= dtlck->maxcnt)
2917 dtlck = (struct dt_lock *) txLinelock(dtlck);
2918 lv = &dtlck->lv[dtlck->index];
2919 lv->offset = stbl[i];
2926 (void) txCommit(tid, 1, &inode, 0);
2932 * Buffer to hold directory entry info while traversing a dtree page
2933 * before being fed to the filldir function
2943 * function to determine next variable-sized jfs_dirent in buffer
2945 static inline struct jfs_dirent *next_jfs_dirent(struct jfs_dirent *dirent)
2947 return (struct jfs_dirent *)
2949 ((sizeof (struct jfs_dirent) + dirent->name_len + 1 +
2950 sizeof (loff_t) - 1) &
2951 ~(sizeof (loff_t) - 1)));
2957 * function: read directory entries sequentially
2958 * from the specified entry offset
2962 * return: offset = (pn, index) of start entry
2963 * of next jfs_readdir()/dtRead()
2965 int jfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
2967 struct inode *ip = filp->f_dentry->d_inode;
2968 struct nls_table *codepage = JFS_SBI(ip->i_sb)->nls_tab;
2970 loff_t dtpos; /* legacy OS/2 style position */
2975 } *dtoffset = (struct dtoffset *) &dtpos;
2977 struct metapage *mp;
2981 struct btstack btstack;
2985 int d_namleft, len, outlen;
2986 unsigned long dirent_buf;
2990 uint loop_count = 0;
2991 struct jfs_dirent *jfs_dirent;
2993 int overflow, fix_page, page_fixed = 0;
2994 static int unique_pos = 2; /* If we can't fix broken index */
2996 if (filp->f_pos == DIREND)
3001 * persistent index is stored in directory entries.
3002 * Special cases: 0 = .
3004 * -1 = End of directory
3008 dir_index = (u32) filp->f_pos;
3010 if (dir_index > 1) {
3011 struct dir_table_slot dirtab_slot;
3014 (dir_index >= JFS_IP(ip)->next_index)) {
3015 /* Stale position. Directory has shrunk */
3016 filp->f_pos = DIREND;
3020 rc = read_index(ip, dir_index, &dirtab_slot);
3022 filp->f_pos = DIREND;
3025 if (dirtab_slot.flag == DIR_INDEX_FREE) {
3026 if (loop_count++ > JFS_IP(ip)->next_index) {
3027 jfs_err("jfs_readdir detected "
3029 filp->f_pos = DIREND;
3032 dir_index = le32_to_cpu(dirtab_slot.addr2);
3033 if (dir_index == -1) {
3034 filp->f_pos = DIREND;
3039 bn = addressDTS(&dirtab_slot);
3040 index = dirtab_slot.slot;
3041 DT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
3043 filp->f_pos = DIREND;
3046 if (p->header.flag & BT_INTERNAL) {
3047 jfs_err("jfs_readdir: bad index table");
3053 if (dir_index == 0) {
3058 if (filldir(dirent, ".", 1, 0, ip->i_ino,
3066 if (filldir(dirent, "..", 2, 1, PARENT(ip), DT_DIR))
3070 * Find first entry of left-most leaf
3073 filp->f_pos = DIREND;
3077 if ((rc = dtReadFirst(ip, &btstack)))
3080 DT_GETSEARCH(ip, btstack.top, bn, mp, p, index);
3084 * Legacy filesystem - OS/2 & Linux JFS < 0.3.6
3086 * pn = index = 0: First entry "."
3087 * pn = 0; index = 1: Second entry ".."
3088 * pn > 0: Real entries, pn=1 -> leftmost page
3089 * pn = index = -1: No more entries
3091 dtpos = filp->f_pos;
3093 /* build "." entry */
3095 if (filldir(dirent, ".", 1, filp->f_pos, ip->i_ino,
3098 dtoffset->index = 1;
3099 filp->f_pos = dtpos;
3102 if (dtoffset->pn == 0) {
3103 if (dtoffset->index == 1) {
3104 /* build ".." entry */
3106 if (filldir(dirent, "..", 2, filp->f_pos,
3107 PARENT(ip), DT_DIR))
3110 jfs_err("jfs_readdir called with "
3114 dtoffset->index = 0;
3115 filp->f_pos = dtpos;
3119 filp->f_pos = DIREND;
3123 if ((rc = dtReadNext(ip, &filp->f_pos, &btstack))) {
3124 jfs_err("jfs_readdir: unexpected rc = %d "
3125 "from dtReadNext", rc);
3126 filp->f_pos = DIREND;
3129 /* get start leaf page and index */
3130 DT_GETSEARCH(ip, btstack.top, bn, mp, p, index);
3132 /* offset beyond directory eof ? */
3134 filp->f_pos = DIREND;
3139 dirent_buf = __get_free_page(GFP_KERNEL);
3140 if (dirent_buf == 0) {
3142 jfs_warn("jfs_readdir: __get_free_page failed!");
3143 filp->f_pos = DIREND;
3148 jfs_dirent = (struct jfs_dirent *) dirent_buf;
3150 overflow = fix_page = 0;
3152 stbl = DT_GETSTBL(p);
3154 for (i = index; i < p->header.nextindex; i++) {
3155 d = (struct ldtentry *) & p->slot[stbl[i]];
3157 if (((long) jfs_dirent + d->namlen + 1) >
3158 (dirent_buf + PSIZE)) {
3159 /* DBCS codepages could overrun dirent_buf */
3165 d_namleft = d->namlen;
3166 name_ptr = jfs_dirent->name;
3167 jfs_dirent->ino = le32_to_cpu(d->inumber);
3170 len = min(d_namleft, DTLHDRDATALEN);
3171 jfs_dirent->position = le32_to_cpu(d->index);
3173 * d->index should always be valid, but it
3174 * isn't. fsck.jfs doesn't create the
3175 * directory index for the lost+found
3176 * directory. Rather than let it go,
3177 * we can try to fix it.
3179 if ((jfs_dirent->position < 2) ||
3180 (jfs_dirent->position >=
3181 JFS_IP(ip)->next_index)) {
3182 if (!page_fixed && !isReadOnly(ip)) {
3185 * setting overflow and setting
3186 * index to i will cause the
3187 * same page to be processed
3188 * again starting here
3194 jfs_dirent->position = unique_pos++;
3197 jfs_dirent->position = dtpos;
3198 len = min(d_namleft, DTLHDRDATALEN_LEGACY);
3201 /* copy the name of head/only segment */
3202 outlen = jfs_strfromUCS_le(name_ptr, d->name, len,
3204 jfs_dirent->name_len = outlen;
3206 /* copy name in the additional segment(s) */
3209 t = (struct dtslot *) & p->slot[next];
3213 if (d_namleft == 0) {
3215 "JFS:Dtree error: ino = "
3216 "%ld, bn=%Ld, index = %d",
3222 len = min(d_namleft, DTSLOTDATALEN);
3223 outlen = jfs_strfromUCS_le(name_ptr, t->name,
3225 jfs_dirent->name_len += outlen;
3231 jfs_dirent = next_jfs_dirent(jfs_dirent);
3238 /* Point to next leaf page */
3239 if (p->header.flag & BT_ROOT)
3242 bn = le64_to_cpu(p->header.next);
3244 /* update offset (pn:index) for new page */
3247 dtoffset->index = 0;
3253 /* unpin previous leaf page */
3256 jfs_dirent = (struct jfs_dirent *) dirent_buf;
3257 while (jfs_dirents--) {
3258 filp->f_pos = jfs_dirent->position;
3259 if (filldir(dirent, jfs_dirent->name,
3260 jfs_dirent->name_len, filp->f_pos,
3261 jfs_dirent->ino, DT_UNKNOWN))
3263 jfs_dirent = next_jfs_dirent(jfs_dirent);
3267 add_missing_indices(ip, bn);
3271 if (!overflow && (bn == 0)) {
3272 filp->f_pos = DIREND;
3276 DT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
3278 free_page(dirent_buf);
3284 free_page(dirent_buf);
3293 * function: get the leftmost page of the directory
3295 static int dtReadFirst(struct inode *ip, struct btstack * btstack)
3299 int psize = 288; /* initial in-line directory */
3300 struct metapage *mp;
3303 struct btframe *btsp;
3306 BT_CLR(btstack); /* reset stack */
3309 * descend leftmost path of the tree
3311 * by convention, root bn = 0.
3314 DT_GETPAGE(ip, bn, mp, psize, p, rc);
3319 * leftmost leaf page
3321 if (p->header.flag & BT_LEAF) {
3322 /* return leftmost entry */
3323 btsp = btstack->top;
3332 * descend down to leftmost child page
3334 /* push (bn, index) of the parent page/entry */
3335 BT_PUSH(btstack, bn, 0);
3337 /* get the leftmost entry */
3338 stbl = DT_GETSTBL(p);
3339 xd = (pxd_t *) & p->slot[stbl[0]];
3341 /* get the child page block address */
3342 bn = addressPXD(xd);
3343 psize = lengthPXD(xd) << JFS_SBI(ip->i_sb)->l2bsize;
3345 /* unpin the parent page */
3354 * function: get the page of the specified offset (pn:index)
3356 * return: if (offset > eof), bn = -1;
3358 * note: if index > nextindex of the target leaf page,
3359 * start with 1st entry of next leaf page;
3361 static int dtReadNext(struct inode *ip, loff_t * offset,
3362 struct btstack * btstack)
3369 } *dtoffset = (struct dtoffset *) offset;
3371 struct metapage *mp;
3376 struct btframe *btsp, *parent;
3380 * get leftmost leaf page pinned
3382 if ((rc = dtReadFirst(ip, btstack)))
3386 DT_GETSEARCH(ip, btstack->top, bn, mp, p, index);
3388 /* get the start offset (pn:index) */
3389 pn = dtoffset->pn - 1; /* Now pn = 0 represents leftmost leaf */
3390 index = dtoffset->index;
3392 /* start at leftmost page ? */
3394 /* offset beyond eof ? */
3395 if (index < p->header.nextindex)
3398 if (p->header.flag & BT_ROOT) {
3403 /* start with 1st entry of next leaf page */
3405 dtoffset->index = index = 0;
3409 /* start at non-leftmost page: scan parent pages for large pn */
3410 if (p->header.flag & BT_ROOT) {
3415 /* start after next leaf page ? */
3419 /* get leaf page pn = 1 */
3421 bn = le64_to_cpu(p->header.next);
3423 /* unpin leaf page */
3426 /* offset beyond eof ? */
3435 * scan last internal page level to get target leaf page
3438 /* unpin leftmost leaf page */
3441 /* get left most parent page */
3442 btsp = btstack->top;
3445 DT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
3449 /* scan parent pages at last internal page level */
3450 while (pn >= p->header.nextindex) {
3451 pn -= p->header.nextindex;
3453 /* get next parent page address */
3454 bn = le64_to_cpu(p->header.next);
3456 /* unpin current parent page */
3459 /* offset beyond eof ? */
3465 /* get next parent page */
3466 DT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
3470 /* update parent page stack frame */
3474 /* get leaf page address */
3475 stbl = DT_GETSTBL(p);
3476 xd = (pxd_t *) & p->slot[stbl[pn]];
3477 bn = addressPXD(xd);
3479 /* unpin parent page */
3483 * get target leaf page
3486 DT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
3491 * leaf page has been completed:
3492 * start with 1st entry of next leaf page
3494 if (index >= p->header.nextindex) {
3495 bn = le64_to_cpu(p->header.next);
3497 /* unpin leaf page */
3500 /* offset beyond eof ? */
3506 /* get next leaf page */
3507 DT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
3511 /* start with 1st entry of next leaf page */
3513 dtoffset->index = 0;
3517 /* return target leaf page pinned */
3518 btsp = btstack->top;
3520 btsp->index = dtoffset->index;
3530 * function: compare search key with an internal entry
3533 * < 0 if k is < record
3534 * = 0 if k is = record
3535 * > 0 if k is > record
3537 static int dtCompare(struct component_name * key, /* search key */
3538 dtpage_t * p, /* directory page */
3540 { /* entry slot index */
3541 wchar_t *kname, *name;
3542 int klen, namlen, len, rc;
3543 struct idtentry *ih;
3547 * force the left-most key on internal pages, at any level of
3548 * the tree, to be less than any search key.
3549 * this obviates having to update the leftmost key on an internal
3550 * page when the user inserts a new key in the tree smaller than
3551 * anything that has been stored.
3553 * (? if/when dtSearch() narrows down to 1st entry (index = 0),
3554 * at any internal page at any level of the tree,
3555 * it descends to child of the entry anyway -
3556 * ? make the entry as min size dummy entry)
3558 * if (e->index == 0 && h->prevpg == P_INVALID && !(h->flags & BT_LEAF))
3565 ih = (struct idtentry *) & p->slot[si];
3568 namlen = ih->namlen;
3569 len = min(namlen, DTIHDRDATALEN);
3571 /* compare with head/only segment */
3572 len = min(klen, len);
3573 if ((rc = UniStrncmp_le(kname, name, len)))
3579 /* compare with additional segment(s) */
3581 while (klen > 0 && namlen > 0) {
3582 /* compare with next name segment */
3583 t = (struct dtslot *) & p->slot[si];
3584 len = min(namlen, DTSLOTDATALEN);
3585 len = min(klen, len);
3587 if ((rc = UniStrncmp_le(kname, name, len)))
3596 return (klen - namlen);
3605 * function: compare search key with an (leaf/internal) entry
3608 * < 0 if k is < record
3609 * = 0 if k is = record
3610 * > 0 if k is > record
3612 static int ciCompare(struct component_name * key, /* search key */
3613 dtpage_t * p, /* directory page */
3614 int si, /* entry slot index */
3617 wchar_t *kname, *name, x;
3618 int klen, namlen, len, rc;
3619 struct ldtentry *lh;
3620 struct idtentry *ih;
3625 * force the left-most key on internal pages, at any level of
3626 * the tree, to be less than any search key.
3627 * this obviates having to update the leftmost key on an internal
3628 * page when the user inserts a new key in the tree smaller than
3629 * anything that has been stored.
3631 * (? if/when dtSearch() narrows down to 1st entry (index = 0),
3632 * at any internal page at any level of the tree,
3633 * it descends to child of the entry anyway -
3634 * ? make the entry as min size dummy entry)
3636 * if (e->index == 0 && h->prevpg == P_INVALID && !(h->flags & BT_LEAF))
3646 if (p->header.flag & BT_LEAF) {
3647 lh = (struct ldtentry *) & p->slot[si];
3650 namlen = lh->namlen;
3651 if (flag & JFS_DIR_INDEX)
3652 len = min(namlen, DTLHDRDATALEN);
3654 len = min(namlen, DTLHDRDATALEN_LEGACY);
3657 * internal page entry
3660 ih = (struct idtentry *) & p->slot[si];
3663 namlen = ih->namlen;
3664 len = min(namlen, DTIHDRDATALEN);
3667 /* compare with head/only segment */
3668 len = min(klen, len);
3669 for (i = 0; i < len; i++, kname++, name++) {
3670 /* only uppercase if case-insensitive support is on */
3671 if ((flag & JFS_OS2) == JFS_OS2)
3672 x = UniToupper(le16_to_cpu(*name));
3674 x = le16_to_cpu(*name);
3675 if ((rc = *kname - x))
3682 /* compare with additional segment(s) */
3683 while (klen > 0 && namlen > 0) {
3684 /* compare with next name segment */
3685 t = (struct dtslot *) & p->slot[si];
3686 len = min(namlen, DTSLOTDATALEN);
3687 len = min(klen, len);
3689 for (i = 0; i < len; i++, kname++, name++) {
3690 /* only uppercase if case-insensitive support is on */
3691 if ((flag & JFS_OS2) == JFS_OS2)
3692 x = UniToupper(le16_to_cpu(*name));
3694 x = le16_to_cpu(*name);
3696 if ((rc = *kname - x))
3705 return (klen - namlen);
3710 * ciGetLeafPrefixKey()
3712 * function: compute prefix of suffix compression
3713 * from two adjacent leaf entries
3714 * across page boundary
3717 * Number of prefix bytes needed to distinguish b from a.
3719 static void ciGetLeafPrefixKey(dtpage_t * lp, int li, dtpage_t * rp,
3720 int ri, struct component_name * key, int flag)
3723 wchar_t *pl, *pr, *kname;
3724 wchar_t lname[JFS_NAME_MAX + 1];
3725 struct component_name lkey = { 0, lname };
3726 wchar_t rname[JFS_NAME_MAX + 1];
3727 struct component_name rkey = { 0, rname };
3729 /* get left and right key */
3730 dtGetKey(lp, li, &lkey, flag);
3731 lkey.name[lkey.namlen] = 0;
3733 if ((flag & JFS_OS2) == JFS_OS2)
3736 dtGetKey(rp, ri, &rkey, flag);
3737 rkey.name[rkey.namlen] = 0;
3740 if ((flag & JFS_OS2) == JFS_OS2)
3743 /* compute prefix */
3746 namlen = min(lkey.namlen, rkey.namlen);
3747 for (pl = lkey.name, pr = rkey.name;
3748 namlen; pl++, pr++, namlen--, klen++, kname++) {
3751 key->namlen = klen + 1;
3756 /* l->namlen <= r->namlen since l <= r */
3757 if (lkey.namlen < rkey.namlen) {
3759 key->namlen = klen + 1;
3760 } else /* l->namelen == r->namelen */
3771 * function: get key of the entry
3773 static void dtGetKey(dtpage_t * p, int i, /* entry index */
3774 struct component_name * key, int flag)
3778 struct ldtentry *lh;
3779 struct idtentry *ih;
3782 wchar_t *name, *kname;
3785 stbl = DT_GETSTBL(p);
3787 if (p->header.flag & BT_LEAF) {
3788 lh = (struct ldtentry *) & p->slot[si];
3790 namlen = lh->namlen;
3792 if (flag & JFS_DIR_INDEX)
3793 len = min(namlen, DTLHDRDATALEN);
3795 len = min(namlen, DTLHDRDATALEN_LEGACY);
3797 ih = (struct idtentry *) & p->slot[si];
3799 namlen = ih->namlen;
3801 len = min(namlen, DTIHDRDATALEN);
3804 key->namlen = namlen;
3808 * move head/only segment
3810 UniStrncpy_le(kname, name, len);
3813 * move additional segment(s)
3816 /* get next segment */
3820 len = min(namlen, DTSLOTDATALEN);
3821 UniStrncpy_le(kname, t->name, len);
3831 * function: allocate free slot(s) and
3832 * write a leaf/internal entry
3834 * return: entry slot index
3836 static void dtInsertEntry(dtpage_t * p, int index, struct component_name * key,
3837 ddata_t * data, struct dt_lock ** dtlock)
3839 struct dtslot *h, *t;
3840 struct ldtentry *lh = 0;
3841 struct idtentry *ih = 0;
3842 int hsi, fsi, klen, len, nextindex;
3843 wchar_t *kname, *name;
3846 struct dt_lock *dtlck = *dtlock;
3850 struct metapage *mp = 0;
3855 /* allocate a free slot */
3856 hsi = fsi = p->header.freelist;
3858 p->header.freelist = h->next;
3859 --p->header.freecnt;
3861 /* open new linelock */
3862 if (dtlck->index >= dtlck->maxcnt)
3863 dtlck = (struct dt_lock *) txLinelock(dtlck);
3865 lv = & dtlck->lv[dtlck->index];
3868 /* write head/only segment */
3869 if (p->header.flag & BT_LEAF) {
3870 lh = (struct ldtentry *) h;
3872 lh->inumber = data->leaf.ino; /* little-endian */
3875 if (data->leaf.ip) {
3876 len = min(klen, DTLHDRDATALEN);
3877 if (!(p->header.flag & BT_ROOT))
3878 bn = addressPXD(&p->header.self);
3879 lh->index = cpu_to_le32(add_index(data->leaf.tid,
3883 len = min(klen, DTLHDRDATALEN_LEGACY);
3885 ih = (struct idtentry *) h;
3891 len = min(klen, DTIHDRDATALEN);
3894 UniStrncpy_le(name, kname, len);
3899 /* write additional segment(s) */
3904 fsi = p->header.freelist;
3906 p->header.freelist = t->next;
3907 --p->header.freecnt;
3909 /* is next slot contiguous ? */
3910 if (fsi != xsi + 1) {
3911 /* close current linelock */
3915 /* open new linelock */
3916 if (dtlck->index < dtlck->maxcnt)
3919 dtlck = (struct dt_lock *) txLinelock(dtlck);
3920 lv = & dtlck->lv[0];
3928 len = min(klen, DTSLOTDATALEN);
3929 UniStrncpy_le(t->name, kname, len);
3936 /* close current linelock */
3942 /* terminate last/only segment */
3944 /* single segment entry */
3945 if (p->header.flag & BT_LEAF)
3950 /* multi-segment entry */
3953 /* if insert into middle, shift right succeeding entries in stbl */
3954 stbl = DT_GETSTBL(p);
3955 nextindex = p->header.nextindex;
3956 if (index < nextindex) {
3957 memmove(stbl + index + 1, stbl + index, nextindex - index);
3959 if ((p->header.flag & BT_LEAF) && data->leaf.ip) {
3963 * Need to update slot number for entries that moved
3967 for (n = index + 1; n <= nextindex; n++) {
3968 lh = (struct ldtentry *) & (p->slot[stbl[n]]);
3969 modify_index(data->leaf.tid, data->leaf.ip,
3970 le32_to_cpu(lh->index), bn, n,
3974 release_metapage(mp);
3980 /* advance next available entry index of stbl */
3981 ++p->header.nextindex;
3988 * function: move entries from split/left page to new/right page
3990 * nextindex of dst page and freelist/freecnt of both pages
3993 static void dtMoveEntry(dtpage_t * sp, int si, dtpage_t * dp,
3994 struct dt_lock ** sdtlock, struct dt_lock ** ddtlock,
3997 int ssi, next; /* src slot index */
3998 int di; /* dst entry index */
3999 int dsi; /* dst slot index */
4000 s8 *sstbl, *dstbl; /* sorted entry table */
4002 struct ldtentry *slh, *dlh = 0;
4003 struct idtentry *sih, *dih = 0;
4004 struct dtslot *h, *s, *d;
4005 struct dt_lock *sdtlck = *sdtlock, *ddtlck = *ddtlock;
4006 struct lv *slv, *dlv;
4010 sstbl = (s8 *) & sp->slot[sp->header.stblindex];
4011 dstbl = (s8 *) & dp->slot[dp->header.stblindex];
4013 dsi = dp->header.freelist; /* first (whole page) free slot */
4014 sfsi = sp->header.freelist;
4016 /* linelock destination entry slot */
4017 dlv = & ddtlck->lv[ddtlck->index];
4020 /* linelock source entry slot */
4021 slv = & sdtlck->lv[sdtlck->index];
4022 slv->offset = sstbl[si];
4023 xssi = slv->offset - 1;
4029 for (di = 0; si < sp->header.nextindex; si++, di++) {
4033 /* is next slot contiguous ? */
4034 if (ssi != xssi + 1) {
4035 /* close current linelock */
4039 /* open new linelock */
4040 if (sdtlck->index < sdtlck->maxcnt)
4043 sdtlck = (struct dt_lock *) txLinelock(sdtlck);
4044 slv = & sdtlck->lv[0];
4052 * move head/only segment of an entry
4055 h = d = &dp->slot[dsi];
4057 /* get src slot and move */
4059 if (sp->header.flag & BT_LEAF) {
4060 /* get source entry */
4061 slh = (struct ldtentry *) s;
4062 dlh = (struct ldtentry *) h;
4063 snamlen = slh->namlen;
4066 len = min(snamlen, DTLHDRDATALEN);
4067 dlh->index = slh->index; /* little-endian */
4069 len = min(snamlen, DTLHDRDATALEN_LEGACY);
4071 memcpy(dlh, slh, 6 + len * 2);
4075 /* update dst head/only segment next field */
4079 sih = (struct idtentry *) s;
4080 snamlen = sih->namlen;
4082 len = min(snamlen, DTIHDRDATALEN);
4083 dih = (struct idtentry *) h;
4084 memcpy(dih, sih, 10 + len * 2);
4091 /* free src head/only segment */
4101 * move additional segment(s) of the entry
4104 while ((ssi = next) >= 0) {
4105 /* is next slot contiguous ? */
4106 if (ssi != xssi + 1) {
4107 /* close current linelock */
4111 /* open new linelock */
4112 if (sdtlck->index < sdtlck->maxcnt)
4118 slv = & sdtlck->lv[0];
4125 /* get next source segment */
4128 /* get next destination free slot */
4131 len = min(snamlen, DTSLOTDATALEN);
4132 UniStrncpy(d->name, s->name, len);
4141 /* free source segment */
4150 /* terminate dst last/only segment */
4152 /* single segment entry */
4153 if (dp->header.flag & BT_LEAF)
4158 /* multi-segment entry */
4162 /* close current linelock */
4171 /* update source header */
4172 sp->header.freelist = sfsi;
4173 sp->header.freecnt += nd;
4175 /* update destination header */
4176 dp->header.nextindex = di;
4178 dp->header.freelist = dsi;
4179 dp->header.freecnt -= nd;
4186 * function: free a (leaf/internal) entry
4188 * log freelist header, stbl, and each segment slot of entry
4189 * (even though last/only segment next field is modified,
4190 * physical image logging requires all segment slots of
4191 * the entry logged to avoid applying previous updates
4192 * to the same slots)
4194 static void dtDeleteEntry(dtpage_t * p, int fi, struct dt_lock ** dtlock)
4196 int fsi; /* free entry slot index */
4200 struct dt_lock *dtlck = *dtlock;
4204 /* get free entry slot index */
4205 stbl = DT_GETSTBL(p);
4208 /* open new linelock */
4209 if (dtlck->index >= dtlck->maxcnt)
4210 dtlck = (struct dt_lock *) txLinelock(dtlck);
4211 lv = & dtlck->lv[dtlck->index];
4215 /* get the head/only segment */
4217 if (p->header.flag & BT_LEAF)
4218 si = ((struct ldtentry *) t)->next;
4220 si = ((struct idtentry *) t)->next;
4227 /* find the last/only segment */
4229 /* is next slot contiguous ? */
4230 if (si != xsi + 1) {
4231 /* close current linelock */
4235 /* open new linelock */
4236 if (dtlck->index < dtlck->maxcnt)
4239 dtlck = (struct dt_lock *) txLinelock(dtlck);
4240 lv = & dtlck->lv[0];
4256 /* close current linelock */
4262 /* update freelist */
4263 t->next = p->header.freelist;
4264 p->header.freelist = fsi;
4265 p->header.freecnt += freecnt;
4267 /* if delete from middle,
4268 * shift left the succedding entries in the stbl
4270 si = p->header.nextindex;
4272 memmove(&stbl[fi], &stbl[fi + 1], si - fi - 1);
4274 p->header.nextindex--;
4281 * function: truncate a (leaf/internal) entry
4283 * log freelist header, stbl, and each segment slot of entry
4284 * (even though last/only segment next field is modified,
4285 * physical image logging requires all segment slots of
4286 * the entry logged to avoid applying previous updates
4287 * to the same slots)
4289 static void dtTruncateEntry(dtpage_t * p, int ti, struct dt_lock ** dtlock)
4291 int tsi; /* truncate entry slot index */
4295 struct dt_lock *dtlck = *dtlock;
4299 /* get free entry slot index */
4300 stbl = DT_GETSTBL(p);
4303 /* open new linelock */
4304 if (dtlck->index >= dtlck->maxcnt)
4305 dtlck = (struct dt_lock *) txLinelock(dtlck);
4306 lv = & dtlck->lv[dtlck->index];
4310 /* get the head/only segment */
4312 ASSERT(p->header.flag & BT_INTERNAL);
4313 ((struct idtentry *) t)->namlen = 0;
4314 si = ((struct idtentry *) t)->next;
4315 ((struct idtentry *) t)->next = -1;
4322 /* find the last/only segment */
4324 /* is next slot contiguous ? */
4325 if (si != xsi + 1) {
4326 /* close current linelock */
4330 /* open new linelock */
4331 if (dtlck->index < dtlck->maxcnt)
4334 dtlck = (struct dt_lock *) txLinelock(dtlck);
4335 lv = & dtlck->lv[0];
4351 /* close current linelock */
4357 /* update freelist */
4360 t->next = p->header.freelist;
4361 p->header.freelist = fsi;
4362 p->header.freecnt += freecnt;
4367 * dtLinelockFreelist()
4369 static void dtLinelockFreelist(dtpage_t * p, /* directory page */
4370 int m, /* max slot index */
4371 struct dt_lock ** dtlock)
4373 int fsi; /* free entry slot index */
4376 struct dt_lock *dtlck = *dtlock;
4380 /* get free entry slot index */
4381 fsi = p->header.freelist;
4383 /* open new linelock */
4384 if (dtlck->index >= dtlck->maxcnt)
4385 dtlck = (struct dt_lock *) txLinelock(dtlck);
4386 lv = & dtlck->lv[dtlck->index];
4396 /* find the last/only segment */
4397 while (si < m && si >= 0) {
4398 /* is next slot contiguous ? */
4399 if (si != xsi + 1) {
4400 /* close current linelock */
4404 /* open new linelock */
4405 if (dtlck->index < dtlck->maxcnt)
4408 dtlck = (struct dt_lock *) txLinelock(dtlck);
4409 lv = & dtlck->lv[0];
4423 /* close current linelock */
4434 * FUNCTION: Modify the inode number part of a directory entry
4437 * tid - Transaction id
4438 * ip - Inode of parent directory
4439 * key - Name of entry to be modified
4440 * orig_ino - Original inode number expected in entry
4441 * new_ino - New inode number to put into entry
4445 * -ESTALE - If entry found does not match orig_ino passed in
4446 * -ENOENT - If no entry can be found to match key
4447 * 0 - If successfully modified entry
4449 int dtModify(tid_t tid, struct inode *ip,
4450 struct component_name * key, ino_t * orig_ino, ino_t new_ino, int flag)
4454 struct metapage *mp;
4457 struct btstack btstack;
4459 struct dt_lock *dtlck;
4462 int entry_si; /* entry slot index */
4463 struct ldtentry *entry;
4466 * search for the entry to modify:
4468 * dtSearch() returns (leaf page pinned, index at which to modify).
4470 if ((rc = dtSearch(ip, key, orig_ino, &btstack, flag)))
4473 /* retrieve search result */
4474 DT_GETSEARCH(ip, btstack.top, bn, mp, p, index);
4476 BT_MARK_DIRTY(mp, ip);
4478 * acquire a transaction lock on the leaf page of named entry
4480 tlck = txLock(tid, ip, mp, tlckDTREE | tlckENTRY);
4481 dtlck = (struct dt_lock *) & tlck->lock;
4483 /* get slot index of the entry */
4484 stbl = DT_GETSTBL(p);
4485 entry_si = stbl[index];
4487 /* linelock entry */
4488 ASSERT(dtlck->index == 0);
4489 lv = & dtlck->lv[0];
4490 lv->offset = entry_si;
4494 /* get the head/only segment */
4495 entry = (struct ldtentry *) & p->slot[entry_si];
4497 /* substitute the inode number of the entry */
4498 entry->inumber = cpu_to_le32(new_ino);
4500 /* unpin the leaf page */
4506 #ifdef _JFS_DEBUG_DTREE
4510 * function: traverse forward
4512 int dtDisplayTree(struct inode *ip)
4515 struct metapage *mp;
4518 int index, lastindex, v, h;
4520 struct btstack btstack;
4521 struct btframe *btsp;
4522 struct btframe *parent;
4526 printk("display B+-tree.\n");
4529 btsp = btstack.stack;
4534 * root resides in the inode
4540 * first access of each page:
4543 DT_GETPAGE(ip, bn, mp, psize, p, rc);
4547 /* process entries forward from first index */
4549 lastindex = p->header.nextindex - 1;
4551 if (p->header.flag & BT_INTERNAL) {
4553 * first access of each internal page
4555 printf("internal page ");
4556 dtDisplayPage(ip, bn, p);
4559 } else { /* (p->header.flag & BT_LEAF) */
4562 * first access of each leaf page
4564 printf("leaf page ");
4565 dtDisplayPage(ip, bn, p);
4568 * process leaf page entries
4570 for ( ; index <= lastindex; index++)
4575 /* unpin the leaf page */
4580 * go back up to the parent page
4583 /* pop/restore parent entry for the current child page */
4584 if ((parent = (btsp == btstack.stack ? NULL : --btsp)) == NULL)
4585 /* current page must have been root */
4589 * parent page scan completed
4591 if ((index = parent->index) == (lastindex = parent->lastindex)) {
4592 /* go back up to the parent page */
4597 * parent page has entries remaining
4599 /* get back the parent page */
4601 /* v = parent->level; */
4602 DT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
4606 /* get next parent entry */
4610 * internal page: go down to child page of current entry
4613 /* push/save current parent entry for the child page */
4614 btsp->bn = pbn = bn;
4615 btsp->index = index;
4616 btsp->lastindex = lastindex;
4617 /* btsp->level = v; */
4618 /* btsp->node = h; */
4621 /* get current entry for the child page */
4622 stbl = DT_GETSTBL(p);
4623 xd = (pxd_t *) & p->slot[stbl[index]];
4626 * first access of each internal entry:
4629 /* get child page */
4630 bn = addressPXD(xd);
4631 psize = lengthPXD(xd) << ip->i_ipmnt->i_l2bsize;
4633 printk("traverse down 0x%Lx[%d]->0x%Lx\n", pbn, index, bn);
4637 /* release parent page */
4640 /* process the child page */
4648 * function: display page
4650 int dtDisplayPage(struct inode *ip, s64 bn, dtpage_t * p)
4653 struct metapage *mp;
4654 struct ldtentry *lh;
4655 struct idtentry *ih;
4659 wchar_t name[JFS_NAME_MAX + 1];
4660 struct component_name key = { 0, name };
4665 DT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
4670 /* display page control */
4671 printk("bn:0x%Lx flag:0x%08x nextindex:%d\n",
4672 bn, p->header.flag, p->header.nextindex);
4674 /* display entries */
4675 stbl = DT_GETSTBL(p);
4676 for (i = 0, j = 1; i < p->header.nextindex; i++, j++) {
4677 dtGetKey(p, i, &key, JFS_SBI(ip->i_sb)->mntflag);
4678 key.name[key.namlen] = '\0';
4679 if (p->header.flag & BT_LEAF) {
4680 lh = (struct ldtentry *) & p->slot[stbl[i]];
4681 printf("\t[%d] %s:%d", i, key.name,
4682 le32_to_cpu(lh->inumber));
4684 ih = (struct idtentry *) & p->slot[stbl[i]];
4686 bn = addressPXD(xd);
4687 printf("\t[%d] %s:0x%Lx", i, key.name, bn);
4703 #endif /* _JFS_DEBUG_DTREE */