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 int 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;
348 ASSERT(DO_INDEX(ip));
350 if (jfs_ip->next_index < 2) {
351 jfs_warn("add_index: next_index = %d. Resetting!",
353 jfs_ip->next_index = 2;
356 index = jfs_ip->next_index++;
358 if (index <= MAX_INLINE_DIRTABLE_ENTRY) {
360 * i_size reflects size of index table, or 8 bytes per entry.
362 ip->i_size = (loff_t) (index - 1) << 3;
365 * dir table fits inline within inode
367 dirtab_slot = &jfs_ip->i_dirtable[index-2];
368 dirtab_slot->flag = DIR_INDEX_VALID;
369 dirtab_slot->slot = slot;
370 DTSaddress(dirtab_slot, bn);
372 set_cflag(COMMIT_Dirtable, ip);
376 if (index == (MAX_INLINE_DIRTABLE_ENTRY + 1)) {
378 * It's time to move the inline table to an external
379 * page and begin to build the xtree
383 * Save the table, we're going to overwrite it with the
386 struct dir_table_slot temp_table[12];
387 memcpy(temp_table, &jfs_ip->i_dirtable, sizeof(temp_table));
390 * Initialize empty x-tree
395 * Allocate the first block & add it to the xtree
398 if (xtInsert(tid, ip, 0, 0, sbi->nbperpage, &xaddr, 0)) {
399 jfs_warn("add_index: xtInsert failed!");
400 memcpy(&jfs_ip->i_dirtable, temp_table,
401 sizeof (temp_table));
405 ip->i_blocks += LBLK2PBLK(sb, sbi->nbperpage);
407 if ((mp = get_index_page(ip, 0)) == 0) {
408 jfs_err("add_index: get_metapage failed!");
409 xtTruncate(tid, ip, 0, COMMIT_PWMAP);
410 memcpy(&jfs_ip->i_dirtable, temp_table,
411 sizeof (temp_table));
414 tlck = txLock(tid, ip, mp, tlckDATA);
415 llck = (struct linelock *) & tlck->lock;
416 ASSERT(llck->index == 0);
420 lv->length = 6; /* tlckDATA slot size is 16 bytes */
423 memcpy(mp->data, temp_table, sizeof(temp_table));
425 mark_metapage_dirty(mp);
426 release_metapage(mp);
429 * Logging is now directed by xtree tlocks
431 clear_cflag(COMMIT_Dirtable, ip);
434 offset = (index - 2) * sizeof(struct dir_table_slot);
435 page_offset = offset & (PSIZE - 1);
436 blkno = ((offset + 1) >> L2PSIZE) << sbi->l2nbperpage;
437 if (page_offset == 0) {
439 * This will be the beginning of a new page
442 if (xtInsert(tid, ip, 0, blkno, sbi->nbperpage, &xaddr, 0)) {
443 jfs_warn("add_index: xtInsert failed!");
447 ip->i_blocks += LBLK2PBLK(sb, sbi->nbperpage);
449 if ((mp = get_index_page(ip, blkno)))
450 memset(mp->data, 0, PSIZE); /* Just looks better */
452 xtTruncate(tid, ip, offset, COMMIT_PWMAP);
454 mp = read_index_page(ip, blkno);
457 jfs_err("add_index: get/read_metapage failed!");
461 lock_index(tid, ip, mp, index);
464 (struct dir_table_slot *) ((char *) mp->data + page_offset);
465 dirtab_slot->flag = DIR_INDEX_VALID;
466 dirtab_slot->slot = slot;
467 DTSaddress(dirtab_slot, bn);
469 mark_metapage_dirty(mp);
470 release_metapage(mp);
476 jfs_ip->next_index--;
484 * Marks an entry to the directory index table as free.
486 static void free_index(tid_t tid, struct inode *ip, u32 index, u32 next)
488 struct dir_table_slot *dirtab_slot;
490 struct metapage *mp = 0;
492 dirtab_slot = find_index(ip, index, &mp, &lblock);
494 if (dirtab_slot == 0)
497 dirtab_slot->flag = DIR_INDEX_FREE;
498 dirtab_slot->slot = dirtab_slot->addr1 = 0;
499 dirtab_slot->addr2 = cpu_to_le32(next);
502 lock_index(tid, ip, mp, index);
503 mark_metapage_dirty(mp);
504 release_metapage(mp);
506 set_cflag(COMMIT_Dirtable, ip);
512 * Changes an entry in the directory index table
514 static void modify_index(tid_t tid, struct inode *ip, u32 index, s64 bn,
515 int slot, struct metapage ** mp, u64 *lblock)
517 struct dir_table_slot *dirtab_slot;
519 dirtab_slot = find_index(ip, index, mp, lblock);
521 if (dirtab_slot == 0)
524 DTSaddress(dirtab_slot, bn);
525 dirtab_slot->slot = slot;
528 lock_index(tid, ip, *mp, index);
529 mark_metapage_dirty(*mp);
531 set_cflag(COMMIT_Dirtable, ip);
537 * reads a directory table slot
539 static int read_index(struct inode *ip, u32 index,
540 struct dir_table_slot * dirtab_slot)
543 struct metapage *mp = 0;
544 struct dir_table_slot *slot;
546 slot = find_index(ip, index, &mp, &lblock);
551 memcpy(dirtab_slot, slot, sizeof(struct dir_table_slot));
554 release_metapage(mp);
563 * Search for the entry with specified key
567 * return: 0 - search result on stack, leaf page pinned;
570 int dtSearch(struct inode *ip, struct component_name * key, ino_t * data,
571 struct btstack * btstack, int flag)
574 int cmp = 1; /* init for empty page */
579 int base, index, lim;
580 struct btframe *btsp;
582 int psize = 288; /* initial in-line directory */
584 struct component_name ciKey;
585 struct super_block *sb = ip->i_sb;
588 (wchar_t *) kmalloc((JFS_NAME_MAX + 1) * sizeof(wchar_t),
590 if (ciKey.name == 0) {
596 /* uppercase search key for c-i directory */
597 UniStrcpy(ciKey.name, key->name);
598 ciKey.namlen = key->namlen;
600 /* only uppercase if case-insensitive support is on */
601 if ((JFS_SBI(sb)->mntflag & JFS_OS2) == JFS_OS2) {
604 BT_CLR(btstack); /* reset stack */
606 /* init level count for max pages to split */
610 * search down tree from root:
612 * between two consecutive entries of <Ki, Pi> and <Kj, Pj> of
613 * internal page, child page Pi contains entry with k, Ki <= K < Kj.
615 * if entry with search key K is not found
616 * internal page search find the entry with largest key Ki
617 * less than K which point to the child page to search;
618 * leaf page search find the entry with smallest key Kj
619 * greater than K so that the returned index is the position of
620 * the entry to be shifted right for insertion of new entry.
621 * for empty tree, search key is greater than any key of the tree.
623 * by convention, root bn = 0.
626 /* get/pin the page to search */
627 DT_GETPAGE(ip, bn, mp, psize, p, rc);
631 /* get sorted entry table of the page */
632 stbl = DT_GETSTBL(p);
635 * binary search with search key K on the current page.
637 for (base = 0, lim = p->header.nextindex; lim; lim >>= 1) {
638 index = base + (lim >> 1);
640 if (p->header.flag & BT_LEAF) {
641 /* uppercase leaf name to compare */
643 ciCompare(&ciKey, p, stbl[index],
644 JFS_SBI(sb)->mntflag);
646 /* router key is in uppercase */
648 cmp = dtCompare(&ciKey, p, stbl[index]);
656 /* search hit - leaf page:
657 * return the entry found
659 if (p->header.flag & BT_LEAF) {
660 inumber = le32_to_cpu(
661 ((struct ldtentry *) & p->slot[stbl[index]])->inumber);
664 * search for JFS_LOOKUP
666 if (flag == JFS_LOOKUP) {
673 * search for JFS_CREATE
675 if (flag == JFS_CREATE) {
682 * search for JFS_REMOVE or JFS_RENAME
684 if ((flag == JFS_REMOVE ||
685 flag == JFS_RENAME) &&
692 * JFS_REMOVE|JFS_FINDDIR|JFS_RENAME
694 /* save search result */
705 /* search hit - internal page:
706 * descend/search its child page
720 * base is the smallest index with key (Kj) greater than
721 * search key (K) and may be zero or (maxindex + 1) index.
724 * search miss - leaf page
726 * return location of entry (base) where new entry with
727 * search key K is to be inserted.
729 if (p->header.flag & BT_LEAF) {
731 * search for JFS_LOOKUP, JFS_REMOVE, or JFS_RENAME
733 if (flag == JFS_LOOKUP || flag == JFS_REMOVE ||
734 flag == JFS_RENAME) {
740 * search for JFS_CREATE|JFS_FINDDIR:
755 * search miss - internal page
757 * if base is non-zero, decrement base by one to get the parent
758 * entry of the child page to search.
760 index = base ? base - 1 : base;
763 * go down to child page
766 /* update max. number of pages to split */
767 if (btstack->nsplit >= 8) {
768 /* Something's corrupted, mark filesytem dirty so
769 * chkdsk will fix it.
771 jfs_error(sb, "stack overrun in dtSearch!");
777 /* push (bn, index) of the parent page/entry */
778 BT_PUSH(btstack, bn, index);
780 /* get the child page block number */
781 pxd = (pxd_t *) & p->slot[stbl[index]];
782 bn = addressPXD(pxd);
783 psize = lengthPXD(pxd) << JFS_SBI(ip->i_sb)->l2bsize;
785 /* unpin the parent page */
805 * function: insert an entry to directory tree
809 * return: 0 - success;
812 int dtInsert(tid_t tid, struct inode *ip,
813 struct component_name * name, ino_t * fsn, struct btstack * btstack)
816 struct metapage *mp; /* meta-page buffer */
817 dtpage_t *p; /* base B+-tree index page */
820 struct dtsplit split; /* split information */
822 struct dt_lock *dtlck;
828 * retrieve search result
830 * dtSearch() returns (leaf page pinned, index at which to insert).
831 * n.b. dtSearch() may return index of (maxindex + 1) of
834 DT_GETSEARCH(ip, btstack->top, bn, mp, p, index);
837 * insert entry for new key
840 if (JFS_IP(ip)->next_index == DIREND) {
844 n = NDTLEAF(name->namlen);
848 n = NDTLEAF_LEGACY(name->namlen);
849 data.leaf.ip = 0; /* signifies legacy directory format */
851 data.leaf.ino = cpu_to_le32(*fsn);
854 * leaf page does not have enough room for new entry:
856 * extend/split the leaf page;
858 * dtSplitUp() will insert the entry and unpin the leaf page.
860 if (n > p->header.freecnt) {
866 rc = dtSplitUp(tid, ip, &split, btstack);
871 * leaf page does have enough room for new entry:
873 * insert the new data entry into the leaf page;
875 BT_MARK_DIRTY(mp, ip);
877 * acquire a transaction lock on the leaf page
879 tlck = txLock(tid, ip, mp, tlckDTREE | tlckENTRY);
880 dtlck = (struct dt_lock *) & tlck->lock;
881 ASSERT(dtlck->index == 0);
884 /* linelock header */
889 dtInsertEntry(p, index, name, &data, &dtlck);
891 /* linelock stbl of non-root leaf page */
892 if (!(p->header.flag & BT_ROOT)) {
893 if (dtlck->index >= dtlck->maxcnt)
894 dtlck = (struct dt_lock *) txLinelock(dtlck);
895 lv = & dtlck->lv[dtlck->index];
896 n = index >> L2DTSLOTSIZE;
897 lv->offset = p->header.stblindex + n;
899 ((p->header.nextindex - 1) >> L2DTSLOTSIZE) - n + 1;
903 /* unpin the leaf page */
913 * function: propagate insertion bottom up;
917 * return: 0 - success;
919 * leaf page unpinned;
921 static int dtSplitUp(tid_t tid,
922 struct inode *ip, struct dtsplit * split, struct btstack * btstack)
924 struct jfs_sb_info *sbi = JFS_SBI(ip->i_sb);
926 struct metapage *smp;
927 dtpage_t *sp; /* split page */
928 struct metapage *rmp;
929 dtpage_t *rp; /* new right page split from sp */
930 pxd_t rpxd; /* new right page extent descriptor */
931 struct metapage *lmp;
932 dtpage_t *lp; /* left child page */
933 int skip; /* index of entry of insertion */
934 struct btframe *parent; /* parent page entry on traverse stack */
937 struct pxdlist pxdlist;
939 struct component_name key = { 0, 0 };
940 ddata_t *data = split->data;
942 struct dt_lock *dtlck;
948 sp = DT_PAGE(ip, smp);
951 (wchar_t *) kmalloc((JFS_NAME_MAX + 2) * sizeof(wchar_t),
962 * The split routines insert the new entry, and
963 * acquire txLock as appropriate.
966 * split root leaf page:
968 if (sp->header.flag & BT_ROOT) {
970 * allocate a single extent child page
973 n = sbi->bsize >> L2DTSLOTSIZE;
974 n -= (n + 31) >> L2DTSLOTSIZE; /* stbl size */
975 n -= DTROOTMAXSLOT - sp->header.freecnt; /* header + entries */
976 if (n <= split->nslot)
978 if ((rc = dbAlloc(ip, 0, (s64) xlen, &xaddr)))
983 pxd = &pxdlist.pxd[0];
984 PXDaddress(pxd, xaddr);
985 PXDlength(pxd, xlen);
986 split->pxdlist = &pxdlist;
987 rc = dtSplitRoot(tid, ip, split, &rmp);
998 * extend first leaf page
1000 * extend the 1st extent if less than buffer page size
1001 * (dtExtendPage() reurns leaf page unpinned)
1003 pxd = &sp->header.self;
1004 xlen = lengthPXD(pxd);
1005 xsize = xlen << sbi->l2bsize;
1006 if (xsize < PSIZE) {
1007 xaddr = addressPXD(pxd);
1008 n = xsize >> L2DTSLOTSIZE;
1009 n -= (n + 31) >> L2DTSLOTSIZE; /* stbl size */
1010 if ((n + sp->header.freecnt) <= split->nslot)
1011 n = xlen + (xlen << 1);
1014 if ((rc = dbReAlloc(sbi->ipbmap, xaddr, (s64) xlen,
1018 pxdlist.maxnpxd = 1;
1020 pxd = &pxdlist.pxd[0];
1021 PXDaddress(pxd, nxaddr)
1022 PXDlength(pxd, xlen + n);
1023 split->pxdlist = &pxdlist;
1024 if ((rc = dtExtendPage(tid, ip, split, btstack))) {
1025 nxaddr = addressPXD(pxd);
1026 if (xaddr != nxaddr) {
1027 /* free relocated extent */
1028 xlen = lengthPXD(pxd);
1029 dbFree(ip, nxaddr, (s64) xlen);
1031 /* free extended delta */
1032 xlen = lengthPXD(pxd) - n;
1033 xaddr = addressPXD(pxd) + xlen;
1034 dbFree(ip, xaddr, (s64) n);
1044 * split leaf page <sp> into <sp> and a new right page <rp>.
1046 * return <rp> pinned and its extent descriptor <rpxd>
1049 * allocate new directory page extent and
1050 * new index page(s) to cover page split(s)
1052 * allocation hint: ?
1054 n = btstack->nsplit;
1055 pxdlist.maxnpxd = pxdlist.npxd = 0;
1056 xlen = sbi->nbperpage;
1057 for (pxd = pxdlist.pxd; n > 0; n--, pxd++) {
1058 if ((rc = dbAlloc(ip, 0, (s64) xlen, &xaddr)) == 0) {
1059 PXDaddress(pxd, xaddr);
1060 PXDlength(pxd, xlen);
1067 /* undo allocation */
1071 split->pxdlist = &pxdlist;
1072 if ((rc = dtSplitPage(tid, ip, split, &rmp, &rp, &rpxd))) {
1075 /* undo allocation */
1080 * propagate up the router entry for the leaf page just split
1082 * insert a router entry for the new page into the parent page,
1083 * propagate the insert/split up the tree by walking back the stack
1084 * of (bn of parent page, index of child page entry in parent page)
1085 * that were traversed during the search for the page that split.
1087 * the propagation of insert/split up the tree stops if the root
1088 * splits or the page inserted into doesn't have to split to hold
1091 * the parent entry for the split page remains the same, and
1092 * a new entry is inserted at its right with the first key and
1093 * block number of the new right page.
1095 * There are a maximum of 4 pages pinned at any time:
1096 * two children, left parent and right parent (when the parent splits).
1097 * keep the child pages pinned while working on the parent.
1098 * make sure that all pins are released at exit.
1100 while ((parent = BT_POP(btstack)) != NULL) {
1101 /* parent page specified by stack frame <parent> */
1103 /* keep current child pages (<lp>, <rp>) pinned */
1108 * insert router entry in parent for new right child page <rp>
1110 /* get the parent page <sp> */
1111 DT_GETPAGE(ip, parent->bn, smp, PSIZE, sp, rc);
1119 * The new key entry goes ONE AFTER the index of parent entry,
1120 * because the split was to the right.
1122 skip = parent->index + 1;
1125 * compute the key for the router entry
1127 * key suffix compression:
1128 * for internal pages that have leaf pages as children,
1129 * retain only what's needed to distinguish between
1130 * the new entry and the entry on the page to its left.
1131 * If the keys compare equal, retain the entire key.
1133 * note that compression is performed only at computing
1134 * router key at the lowest internal level.
1135 * further compression of the key between pairs of higher
1136 * level internal pages loses too much information and
1137 * the search may fail.
1138 * (e.g., two adjacent leaf pages of {a, ..., x} {xx, ...,}
1139 * results in two adjacent parent entries (a)(xx).
1140 * if split occurs between these two entries, and
1141 * if compression is applied, the router key of parent entry
1142 * of right page (x) will divert search for x into right
1143 * subtree and miss x in the left subtree.)
1145 * the entire key must be retained for the next-to-leftmost
1146 * internal key at any level of the tree, or search may fail
1149 switch (rp->header.flag & BT_TYPE) {
1152 * compute the length of prefix for suffix compression
1153 * between last entry of left page and first entry
1156 if ((sp->header.flag & BT_ROOT && skip > 1) ||
1157 sp->header.prev != 0 || skip > 1) {
1158 /* compute uppercase router prefix key */
1159 rc = ciGetLeafPrefixKey(lp,
1160 lp->header.nextindex-1,
1170 /* next to leftmost entry of
1171 lowest internal level */
1173 /* compute uppercase router key */
1174 dtGetKey(rp, 0, &key, sbi->mntflag);
1175 key.name[key.namlen] = 0;
1177 if ((sbi->mntflag & JFS_OS2) == JFS_OS2)
1181 n = NDTINTERNAL(key.namlen);
1185 dtGetKey(rp, 0, &key, sbi->mntflag);
1186 n = NDTINTERNAL(key.namlen);
1190 jfs_err("dtSplitUp(): UFO!");
1194 /* unpin left child page */
1198 * compute the data for the router entry
1200 data->xd = rpxd; /* child page xd */
1203 * parent page is full - split the parent page
1205 if (n > sp->header.freecnt) {
1206 /* init for parent page split */
1208 split->index = skip; /* index at insert */
1211 /* split->data = data; */
1213 /* unpin right child page */
1216 /* The split routines insert the new entry,
1217 * acquire txLock as appropriate.
1218 * return <rp> pinned and its block number <rbn>.
1220 rc = (sp->header.flag & BT_ROOT) ?
1221 dtSplitRoot(tid, ip, split, &rmp) :
1222 dtSplitPage(tid, ip, split, &rmp, &rp, &rpxd);
1228 /* smp and rmp are pinned */
1231 * parent page is not full - insert router entry in parent page
1234 BT_MARK_DIRTY(smp, ip);
1236 * acquire a transaction lock on the parent page
1238 tlck = txLock(tid, ip, smp, tlckDTREE | tlckENTRY);
1239 dtlck = (struct dt_lock *) & tlck->lock;
1240 ASSERT(dtlck->index == 0);
1241 lv = & dtlck->lv[0];
1243 /* linelock header */
1248 /* linelock stbl of non-root parent page */
1249 if (!(sp->header.flag & BT_ROOT)) {
1251 n = skip >> L2DTSLOTSIZE;
1252 lv->offset = sp->header.stblindex + n;
1254 ((sp->header.nextindex -
1255 1) >> L2DTSLOTSIZE) - n + 1;
1259 dtInsertEntry(sp, skip, &key, data, &dtlck);
1261 /* exit propagate up */
1266 /* unpin current split and its right page */
1271 * free remaining extents allocated for split
1275 pxd = &pxdlist.pxd[n];
1276 for (; n < pxdlist.maxnpxd; n++, pxd++)
1277 dbFree(ip, addressPXD(pxd), (s64) lengthPXD(pxd));
1291 * function: Split a non-root page of a btree.
1295 * return: 0 - success;
1297 * return split and new page pinned;
1299 static int dtSplitPage(tid_t tid, struct inode *ip, struct dtsplit * split,
1300 struct metapage ** rmpp, dtpage_t ** rpp, pxd_t * rpxdp)
1302 struct super_block *sb = ip->i_sb;
1304 struct metapage *smp;
1306 struct metapage *rmp;
1307 dtpage_t *rp; /* new right page allocated */
1308 s64 rbn; /* new right page block number */
1309 struct metapage *mp;
1312 struct pxdlist *pxdlist;
1314 int skip, nextindex, half, left, nxt, off, si;
1315 struct ldtentry *ldtentry;
1316 struct idtentry *idtentry;
1321 struct dt_lock *sdtlck, *rdtlck;
1323 struct dt_lock *dtlck;
1324 struct lv *slv, *rlv, *lv;
1326 /* get split page */
1328 sp = DT_PAGE(ip, smp);
1331 * allocate the new right page for the split
1333 pxdlist = split->pxdlist;
1334 pxd = &pxdlist->pxd[pxdlist->npxd];
1336 rbn = addressPXD(pxd);
1337 rmp = get_metapage(ip, rbn, PSIZE, 1);
1341 jfs_info("dtSplitPage: ip:0x%p smp:0x%p rmp:0x%p", ip, smp, rmp);
1343 BT_MARK_DIRTY(rmp, ip);
1345 * acquire a transaction lock on the new right page
1347 tlck = txLock(tid, ip, rmp, tlckDTREE | tlckNEW);
1348 rdtlck = (struct dt_lock *) & tlck->lock;
1350 rp = (dtpage_t *) rmp->data;
1352 rp->header.self = *pxd;
1354 BT_MARK_DIRTY(smp, ip);
1356 * acquire a transaction lock on the split page
1360 tlck = txLock(tid, ip, smp, tlckDTREE | tlckENTRY);
1361 sdtlck = (struct dt_lock *) & tlck->lock;
1363 /* linelock header of split page */
1364 ASSERT(sdtlck->index == 0);
1365 slv = & sdtlck->lv[0];
1371 * initialize/update sibling pointers between sp and rp
1373 nextbn = le64_to_cpu(sp->header.next);
1374 rp->header.next = cpu_to_le64(nextbn);
1375 rp->header.prev = cpu_to_le64(addressPXD(&sp->header.self));
1376 sp->header.next = cpu_to_le64(rbn);
1379 * initialize new right page
1381 rp->header.flag = sp->header.flag;
1383 /* compute sorted entry table at start of extent data area */
1384 rp->header.nextindex = 0;
1385 rp->header.stblindex = 1;
1387 n = PSIZE >> L2DTSLOTSIZE;
1388 rp->header.maxslot = n;
1389 stblsize = (n + 31) >> L2DTSLOTSIZE; /* in unit of slot */
1392 fsi = rp->header.stblindex + stblsize;
1393 rp->header.freelist = fsi;
1394 rp->header.freecnt = rp->header.maxslot - fsi;
1397 * sequential append at tail: append without split
1399 * If splitting the last page on a level because of appending
1400 * a entry to it (skip is maxentry), it's likely that the access is
1401 * sequential. Adding an empty page on the side of the level is less
1402 * work and can push the fill factor much higher than normal.
1403 * If we're wrong it's no big deal, we'll just do the split the right
1405 * (It may look like it's equally easy to do a similar hack for
1406 * reverse sorted data, that is, split the tree left,
1407 * but it's not. Be my guest.)
1409 if (nextbn == 0 && split->index == sp->header.nextindex) {
1410 /* linelock header + stbl (first slot) of new page */
1411 rlv = & rdtlck->lv[rdtlck->index];
1417 * initialize freelist of new right page
1420 for (fsi++; fsi < rp->header.maxslot; f++, fsi++)
1424 /* insert entry at the first entry of the new right page */
1425 dtInsertEntry(rp, 0, split->key, split->data, &rdtlck);
1431 * non-sequential insert (at possibly middle page)
1435 * update prev pointer of previous right sibling page;
1438 DT_GETPAGE(ip, nextbn, mp, PSIZE, p, rc);
1440 discard_metapage(rmp);
1444 BT_MARK_DIRTY(mp, ip);
1446 * acquire a transaction lock on the next page
1448 tlck = txLock(tid, ip, mp, tlckDTREE | tlckRELINK);
1449 jfs_info("dtSplitPage: tlck = 0x%p, ip = 0x%p, mp=0x%p",
1451 dtlck = (struct dt_lock *) & tlck->lock;
1453 /* linelock header of previous right sibling page */
1454 lv = & dtlck->lv[dtlck->index];
1459 p->header.prev = cpu_to_le64(rbn);
1465 * split the data between the split and right pages.
1467 skip = split->index;
1468 half = (PSIZE >> L2DTSLOTSIZE) >> 1; /* swag */
1472 * compute fill factor for split pages
1474 * <nxt> traces the next entry to move to rp
1475 * <off> traces the next entry to stay in sp
1477 stbl = (u8 *) & sp->slot[sp->header.stblindex];
1478 nextindex = sp->header.nextindex;
1479 for (nxt = off = 0; nxt < nextindex; ++off) {
1481 /* check for fill factor with new entry size */
1485 switch (sp->header.flag & BT_TYPE) {
1487 ldtentry = (struct ldtentry *) & sp->slot[si];
1489 n = NDTLEAF(ldtentry->namlen);
1491 n = NDTLEAF_LEGACY(ldtentry->
1496 idtentry = (struct idtentry *) & sp->slot[si];
1497 n = NDTINTERNAL(idtentry->namlen);
1504 ++nxt; /* advance to next entry to move in sp */
1512 /* <nxt> poins to the 1st entry to move */
1515 * move entries to right page
1517 * dtMoveEntry() initializes rp and reserves entry for insertion
1519 * split page moved out entries are linelocked;
1520 * new/right page moved in entries are linelocked;
1522 /* linelock header + stbl of new right page */
1523 rlv = & rdtlck->lv[rdtlck->index];
1528 dtMoveEntry(sp, nxt, rp, &sdtlck, &rdtlck, DO_INDEX(ip));
1530 sp->header.nextindex = nxt;
1533 * finalize freelist of new right page
1535 fsi = rp->header.freelist;
1537 for (fsi++; fsi < rp->header.maxslot; f++, fsi++)
1542 * Update directory index table for entries now in right page
1544 if ((rp->header.flag & BT_LEAF) && DO_INDEX(ip)) {
1548 stbl = DT_GETSTBL(rp);
1549 for (n = 0; n < rp->header.nextindex; n++) {
1550 ldtentry = (struct ldtentry *) & rp->slot[stbl[n]];
1551 modify_index(tid, ip, le32_to_cpu(ldtentry->index),
1552 rbn, n, &mp, &lblock);
1555 release_metapage(mp);
1559 * the skipped index was on the left page,
1562 /* insert the new entry in the split page */
1563 dtInsertEntry(sp, skip, split->key, split->data, &sdtlck);
1565 /* linelock stbl of split page */
1566 if (sdtlck->index >= sdtlck->maxcnt)
1567 sdtlck = (struct dt_lock *) txLinelock(sdtlck);
1568 slv = & sdtlck->lv[sdtlck->index];
1569 n = skip >> L2DTSLOTSIZE;
1570 slv->offset = sp->header.stblindex + n;
1572 ((sp->header.nextindex - 1) >> L2DTSLOTSIZE) - n + 1;
1576 * the skipped index was on the right page,
1579 /* adjust the skip index to reflect the new position */
1582 /* insert the new entry in the right page */
1583 dtInsertEntry(rp, skip, split->key, split->data, &rdtlck);
1590 ip->i_blocks += LBLK2PBLK(sb, lengthPXD(pxd));
1599 * function: extend 1st/only directory leaf page
1603 * return: 0 - success;
1605 * return extended page pinned;
1607 static int dtExtendPage(tid_t tid,
1608 struct inode *ip, struct dtsplit * split, struct btstack * btstack)
1610 struct super_block *sb = ip->i_sb;
1612 struct metapage *smp, *pmp, *mp;
1614 struct pxdlist *pxdlist;
1617 int newstblindex, newstblsize;
1618 int oldstblindex, oldstblsize;
1621 struct btframe *parent;
1623 struct dt_lock *dtlck;
1626 struct pxd_lock *pxdlock;
1629 struct ldtentry *ldtentry;
1632 /* get page to extend */
1634 sp = DT_PAGE(ip, smp);
1636 /* get parent/root page */
1637 parent = BT_POP(btstack);
1638 DT_GETPAGE(ip, parent->bn, pmp, PSIZE, pp, rc);
1645 pxdlist = split->pxdlist;
1646 pxd = &pxdlist->pxd[pxdlist->npxd];
1649 xaddr = addressPXD(pxd);
1650 tpxd = &sp->header.self;
1651 txaddr = addressPXD(tpxd);
1652 /* in-place extension */
1653 if (xaddr == txaddr) {
1660 /* save moved extent descriptor for later free */
1661 tlck = txMaplock(tid, ip, tlckDTREE | tlckRELOCATE);
1662 pxdlock = (struct pxd_lock *) & tlck->lock;
1663 pxdlock->flag = mlckFREEPXD;
1664 pxdlock->pxd = sp->header.self;
1668 * Update directory index table to reflect new page address
1674 stbl = DT_GETSTBL(sp);
1675 for (n = 0; n < sp->header.nextindex; n++) {
1677 (struct ldtentry *) & sp->slot[stbl[n]];
1678 modify_index(tid, ip,
1679 le32_to_cpu(ldtentry->index),
1680 xaddr, n, &mp, &lblock);
1683 release_metapage(mp);
1690 sp->header.self = *pxd;
1692 jfs_info("dtExtendPage: ip:0x%p smp:0x%p sp:0x%p", ip, smp, sp);
1694 BT_MARK_DIRTY(smp, ip);
1696 * acquire a transaction lock on the extended/leaf page
1698 tlck = txLock(tid, ip, smp, tlckDTREE | type);
1699 dtlck = (struct dt_lock *) & tlck->lock;
1700 lv = & dtlck->lv[0];
1702 /* update buffer extent descriptor of extended page */
1703 xlen = lengthPXD(pxd);
1704 xsize = xlen << JFS_SBI(sb)->l2bsize;
1705 #ifdef _STILL_TO_PORT
1706 bmSetXD(smp, xaddr, xsize);
1707 #endif /* _STILL_TO_PORT */
1710 * copy old stbl to new stbl at start of extended area
1712 oldstblindex = sp->header.stblindex;
1713 oldstblsize = (sp->header.maxslot + 31) >> L2DTSLOTSIZE;
1714 newstblindex = sp->header.maxslot;
1715 n = xsize >> L2DTSLOTSIZE;
1716 newstblsize = (n + 31) >> L2DTSLOTSIZE;
1717 memcpy(&sp->slot[newstblindex], &sp->slot[oldstblindex],
1718 sp->header.nextindex);
1721 * in-line extension: linelock old area of extended page
1723 if (type == tlckEXTEND) {
1724 /* linelock header */
1730 /* linelock new stbl of extended page */
1731 lv->offset = newstblindex;
1732 lv->length = newstblsize;
1735 * relocation: linelock whole relocated area
1739 lv->length = sp->header.maxslot + newstblsize;
1744 sp->header.maxslot = n;
1745 sp->header.stblindex = newstblindex;
1746 /* sp->header.nextindex remains the same */
1749 * add old stbl region at head of freelist
1753 last = sp->header.freelist;
1754 for (n = 0; n < oldstblsize; n++, fsi++, f++) {
1758 sp->header.freelist = last;
1759 sp->header.freecnt += oldstblsize;
1762 * append free region of newly extended area at tail of freelist
1764 /* init free region of newly extended area */
1765 fsi = n = newstblindex + newstblsize;
1767 for (fsi++; fsi < sp->header.maxslot; f++, fsi++)
1771 /* append new free region at tail of old freelist */
1772 fsi = sp->header.freelist;
1774 sp->header.freelist = n;
1779 } while (fsi != -1);
1784 sp->header.freecnt += sp->header.maxslot - n;
1787 * insert the new entry
1789 dtInsertEntry(sp, split->index, split->key, split->data, &dtlck);
1791 BT_MARK_DIRTY(pmp, ip);
1793 * linelock any freeslots residing in old extent
1795 if (type == tlckEXTEND) {
1796 n = sp->header.maxslot >> 2;
1797 if (sp->header.freelist < n)
1798 dtLinelockFreelist(sp, n, &dtlck);
1802 * update parent entry on the parent/root page
1805 * acquire a transaction lock on the parent/root page
1807 tlck = txLock(tid, ip, pmp, tlckDTREE | tlckENTRY);
1808 dtlck = (struct dt_lock *) & tlck->lock;
1809 lv = & dtlck->lv[dtlck->index];
1811 /* linelock parent entry - 1st slot */
1816 /* update the parent pxd for page extension */
1817 tpxd = (pxd_t *) & pp->slot[1];
1820 /* Since the directory might have an EA and/or ACL associated with it
1821 * we need to make sure we take that into account when setting the
1824 ip->i_blocks = LBLK2PBLK(ip->i_sb, xlen +
1825 ((JFS_IP(ip)->ea.flag & DXD_EXTENT) ?
1826 lengthDXD(&JFS_IP(ip)->ea) : 0) +
1827 ((JFS_IP(ip)->acl.flag & DXD_EXTENT) ?
1828 lengthDXD(&JFS_IP(ip)->acl) : 0));
1839 * split the full root page into
1840 * original/root/split page and new right page
1841 * i.e., root remains fixed in tree anchor (inode) and
1842 * the root is copied to a single new right child page
1843 * since root page << non-root page, and
1844 * the split root page contains a single entry for the
1845 * new right child page.
1849 * return: 0 - success;
1851 * return new page pinned;
1853 static int dtSplitRoot(tid_t tid,
1854 struct inode *ip, struct dtsplit * split, struct metapage ** rmpp)
1856 struct super_block *sb = ip->i_sb;
1857 struct metapage *smp;
1859 struct metapage *rmp;
1866 int fsi, stblsize, n;
1869 struct pxdlist *pxdlist;
1871 struct dt_lock *dtlck;
1875 /* get split root page */
1877 sp = &JFS_IP(ip)->i_dtroot;
1880 * allocate/initialize a single (right) child page
1882 * N.B. at first split, a one (or two) block to fit new entry
1883 * is allocated; at subsequent split, a full page is allocated;
1885 pxdlist = split->pxdlist;
1886 pxd = &pxdlist->pxd[pxdlist->npxd];
1888 rbn = addressPXD(pxd);
1889 xlen = lengthPXD(pxd);
1890 xsize = xlen << JFS_SBI(sb)->l2bsize;
1891 rmp = get_metapage(ip, rbn, xsize, 1);
1897 BT_MARK_DIRTY(rmp, ip);
1899 * acquire a transaction lock on the new right page
1901 tlck = txLock(tid, ip, rmp, tlckDTREE | tlckNEW);
1902 dtlck = (struct dt_lock *) & tlck->lock;
1905 (sp->header.flag & BT_LEAF) ? BT_LEAF : BT_INTERNAL;
1906 rp->header.self = *pxd;
1908 /* initialize sibling pointers */
1909 rp->header.next = 0;
1910 rp->header.prev = 0;
1913 * move in-line root page into new right page extent
1915 /* linelock header + copied entries + new stbl (1st slot) in new page */
1916 ASSERT(dtlck->index == 0);
1917 lv = & dtlck->lv[0];
1919 lv->length = 10; /* 1 + 8 + 1 */
1922 n = xsize >> L2DTSLOTSIZE;
1923 rp->header.maxslot = n;
1924 stblsize = (n + 31) >> L2DTSLOTSIZE;
1926 /* copy old stbl to new stbl at start of extended area */
1927 rp->header.stblindex = DTROOTMAXSLOT;
1928 stbl = (s8 *) & rp->slot[DTROOTMAXSLOT];
1929 memcpy(stbl, sp->header.stbl, sp->header.nextindex);
1930 rp->header.nextindex = sp->header.nextindex;
1932 /* copy old data area to start of new data area */
1933 memcpy(&rp->slot[1], &sp->slot[1], IDATASIZE);
1936 * append free region of newly extended area at tail of freelist
1938 /* init free region of newly extended area */
1939 fsi = n = DTROOTMAXSLOT + stblsize;
1941 for (fsi++; fsi < rp->header.maxslot; f++, fsi++)
1945 /* append new free region at tail of old freelist */
1946 fsi = sp->header.freelist;
1948 rp->header.freelist = n;
1950 rp->header.freelist = fsi;
1955 } while (fsi != -1);
1960 rp->header.freecnt = sp->header.freecnt + rp->header.maxslot - n;
1963 * Update directory index table for entries now in right page
1965 if ((rp->header.flag & BT_LEAF) && DO_INDEX(ip)) {
1967 struct metapage *mp = 0;
1968 struct ldtentry *ldtentry;
1970 stbl = DT_GETSTBL(rp);
1971 for (n = 0; n < rp->header.nextindex; n++) {
1972 ldtentry = (struct ldtentry *) & rp->slot[stbl[n]];
1973 modify_index(tid, ip, le32_to_cpu(ldtentry->index),
1974 rbn, n, &mp, &lblock);
1977 release_metapage(mp);
1980 * insert the new entry into the new right/child page
1981 * (skip index in the new right page will not change)
1983 dtInsertEntry(rp, split->index, split->key, split->data, &dtlck);
1986 * reset parent/root page
1988 * set the 1st entry offset to 0, which force the left-most key
1989 * at any level of the tree to be less than any search key.
1991 * The btree comparison code guarantees that the left-most key on any
1992 * level of the tree is never used, so it doesn't need to be filled in.
1994 BT_MARK_DIRTY(smp, ip);
1996 * acquire a transaction lock on the root page (in-memory inode)
1998 tlck = txLock(tid, ip, smp, tlckDTREE | tlckNEW | tlckBTROOT);
1999 dtlck = (struct dt_lock *) & tlck->lock;
2002 ASSERT(dtlck->index == 0);
2003 lv = & dtlck->lv[0];
2005 lv->length = DTROOTMAXSLOT;
2008 /* update page header of root */
2009 if (sp->header.flag & BT_LEAF) {
2010 sp->header.flag &= ~BT_LEAF;
2011 sp->header.flag |= BT_INTERNAL;
2014 /* init the first entry */
2015 s = (struct idtentry *) & sp->slot[DTENTRYSTART];
2021 stbl = sp->header.stbl;
2022 stbl[0] = DTENTRYSTART;
2023 sp->header.nextindex = 1;
2026 fsi = DTENTRYSTART + 1;
2029 /* init free region of remaining area */
2030 for (fsi++; fsi < DTROOTMAXSLOT; f++, fsi++)
2034 sp->header.freelist = DTENTRYSTART + 1;
2035 sp->header.freecnt = DTROOTMAXSLOT - (DTENTRYSTART + 1);
2039 ip->i_blocks += LBLK2PBLK(ip->i_sb, lengthPXD(pxd));
2047 * function: delete the entry(s) referenced by a key.
2053 int dtDelete(tid_t tid,
2054 struct inode *ip, struct component_name * key, ino_t * ino, int flag)
2058 struct metapage *mp, *imp;
2061 struct btstack btstack;
2062 struct dt_lock *dtlck;
2066 struct ldtentry *ldtentry;
2068 u32 table_index, next_index;
2069 struct metapage *nmp;
2073 * search for the entry to delete:
2075 * dtSearch() returns (leaf page pinned, index at which to delete).
2077 if ((rc = dtSearch(ip, key, ino, &btstack, flag)))
2080 /* retrieve search result */
2081 DT_GETSEARCH(ip, btstack.top, bn, mp, p, index);
2084 * We need to find put the index of the next entry into the
2085 * directory index table in order to resume a readdir from this
2089 stbl = DT_GETSTBL(p);
2090 ldtentry = (struct ldtentry *) & p->slot[stbl[index]];
2091 table_index = le32_to_cpu(ldtentry->index);
2092 if (index == (p->header.nextindex - 1)) {
2094 * Last entry in this leaf page
2096 if ((p->header.flag & BT_ROOT)
2097 || (p->header.next == 0))
2100 /* Read next leaf page */
2101 DT_GETPAGE(ip, le64_to_cpu(p->header.next),
2102 nmp, PSIZE, np, rc);
2106 stbl = DT_GETSTBL(np);
2108 (struct ldtentry *) & np->
2111 le32_to_cpu(ldtentry->index);
2117 (struct ldtentry *) & p->slot[stbl[index + 1]];
2118 next_index = le32_to_cpu(ldtentry->index);
2120 free_index(tid, ip, table_index, next_index);
2123 * the leaf page becomes empty, delete the page
2125 if (p->header.nextindex == 1) {
2126 /* delete empty page */
2127 rc = dtDeleteUp(tid, ip, mp, p, &btstack);
2130 * the leaf page has other entries remaining:
2132 * delete the entry from the leaf page.
2135 BT_MARK_DIRTY(mp, ip);
2137 * acquire a transaction lock on the leaf page
2139 tlck = txLock(tid, ip, mp, tlckDTREE | tlckENTRY);
2140 dtlck = (struct dt_lock *) & tlck->lock;
2143 * Do not assume that dtlck->index will be zero. During a
2144 * rename within a directory, this transaction may have
2145 * modified this page already when adding the new entry.
2148 /* linelock header */
2149 if (dtlck->index >= dtlck->maxcnt)
2150 dtlck = (struct dt_lock *) txLinelock(dtlck);
2151 lv = & dtlck->lv[dtlck->index];
2156 /* linelock stbl of non-root leaf page */
2157 if (!(p->header.flag & BT_ROOT)) {
2158 if (dtlck->index >= dtlck->maxcnt)
2159 dtlck = (struct dt_lock *) txLinelock(dtlck);
2160 lv = & dtlck->lv[dtlck->index];
2161 i = index >> L2DTSLOTSIZE;
2162 lv->offset = p->header.stblindex + i;
2164 ((p->header.nextindex - 1) >> L2DTSLOTSIZE) -
2169 /* free the leaf entry */
2170 dtDeleteEntry(p, index, &dtlck);
2173 * Update directory index table for entries moved in stbl
2175 if (DO_INDEX(ip) && index < p->header.nextindex) {
2179 stbl = DT_GETSTBL(p);
2180 for (i = index; i < p->header.nextindex; i++) {
2182 (struct ldtentry *) & p->slot[stbl[i]];
2183 modify_index(tid, ip,
2184 le32_to_cpu(ldtentry->index),
2185 bn, i, &imp, &lblock);
2188 release_metapage(imp);
2202 * free empty pages as propagating deletion up the tree
2208 static int dtDeleteUp(tid_t tid, struct inode *ip,
2209 struct metapage * fmp, dtpage_t * fp, struct btstack * btstack)
2212 struct metapage *mp;
2214 int index, nextindex;
2216 struct btframe *parent;
2217 struct dt_lock *dtlck;
2220 struct pxd_lock *pxdlock;
2224 * keep the root leaf page which has become empty
2226 if (BT_IS_ROOT(fmp)) {
2230 * dtInitRoot() acquires txlock on the root
2232 dtInitRoot(tid, ip, PARENT(ip));
2240 * free the non-root leaf page
2243 * acquire a transaction lock on the page
2245 * write FREEXTENT|NOREDOPAGE log record
2246 * N.B. linelock is overlaid as freed extent descriptor, and
2247 * the buffer page is freed;
2249 tlck = txMaplock(tid, ip, tlckDTREE | tlckFREE);
2250 pxdlock = (struct pxd_lock *) & tlck->lock;
2251 pxdlock->flag = mlckFREEPXD;
2252 pxdlock->pxd = fp->header.self;
2255 /* update sibling pointers */
2256 if ((rc = dtRelink(tid, ip, fp))) {
2261 xlen = lengthPXD(&fp->header.self);
2262 ip->i_blocks -= LBLK2PBLK(ip->i_sb, xlen);
2264 /* free/invalidate its buffer page */
2265 discard_metapage(fmp);
2268 * propagate page deletion up the directory tree
2270 * If the delete from the parent page makes it empty,
2271 * continue all the way up the tree.
2272 * stop if the root page is reached (which is never deleted) or
2273 * if the entry deletion does not empty the page.
2275 while ((parent = BT_POP(btstack)) != NULL) {
2276 /* pin the parent page <sp> */
2277 DT_GETPAGE(ip, parent->bn, mp, PSIZE, p, rc);
2282 * free the extent of the child page deleted
2284 index = parent->index;
2287 * delete the entry for the child page from parent
2289 nextindex = p->header.nextindex;
2292 * the parent has the single entry being deleted:
2294 * free the parent page which has become empty.
2296 if (nextindex == 1) {
2298 * keep the root internal page which has become empty
2300 if (p->header.flag & BT_ROOT) {
2304 * dtInitRoot() acquires txlock on the root
2306 dtInitRoot(tid, ip, PARENT(ip));
2313 * free the parent page
2317 * acquire a transaction lock on the page
2319 * write FREEXTENT|NOREDOPAGE log record
2323 tlckDTREE | tlckFREE);
2324 pxdlock = (struct pxd_lock *) & tlck->lock;
2325 pxdlock->flag = mlckFREEPXD;
2326 pxdlock->pxd = p->header.self;
2329 /* update sibling pointers */
2330 if ((rc = dtRelink(tid, ip, p))) {
2335 xlen = lengthPXD(&p->header.self);
2336 ip->i_blocks -= LBLK2PBLK(ip->i_sb, xlen);
2338 /* free/invalidate its buffer page */
2339 discard_metapage(mp);
2347 * the parent has other entries remaining:
2349 * delete the router entry from the parent page.
2351 BT_MARK_DIRTY(mp, ip);
2353 * acquire a transaction lock on the page
2355 * action: router entry deletion
2357 tlck = txLock(tid, ip, mp, tlckDTREE | tlckENTRY);
2358 dtlck = (struct dt_lock *) & tlck->lock;
2360 /* linelock header */
2361 if (dtlck->index >= dtlck->maxcnt)
2362 dtlck = (struct dt_lock *) txLinelock(dtlck);
2363 lv = & dtlck->lv[dtlck->index];
2368 /* linelock stbl of non-root leaf page */
2369 if (!(p->header.flag & BT_ROOT)) {
2370 if (dtlck->index < dtlck->maxcnt)
2373 dtlck = (struct dt_lock *) txLinelock(dtlck);
2374 lv = & dtlck->lv[0];
2376 i = index >> L2DTSLOTSIZE;
2377 lv->offset = p->header.stblindex + i;
2379 ((p->header.nextindex - 1) >> L2DTSLOTSIZE) -
2384 /* free the router entry */
2385 dtDeleteEntry(p, index, &dtlck);
2387 /* reset key of new leftmost entry of level (for consistency) */
2389 ((p->header.flag & BT_ROOT) || p->header.prev == 0))
2390 dtTruncateEntry(p, 0, &dtlck);
2392 /* unpin the parent page */
2395 /* exit propagation up */
2404 * NAME: dtRelocate()
2406 * FUNCTION: relocate dtpage (internal or leaf) of directory;
2407 * This function is mainly used by defragfs utility.
2409 int dtRelocate(tid_t tid, struct inode *ip, s64 lmxaddr, pxd_t * opxd,
2413 struct metapage *mp, *pmp, *lmp, *rmp;
2414 dtpage_t *p, *pp, *rp = 0, *lp= 0;
2417 struct btstack btstack;
2419 s64 oxaddr, nextbn, prevbn;
2422 struct dt_lock *dtlck;
2423 struct pxd_lock *pxdlock;
2427 oxaddr = addressPXD(opxd);
2428 xlen = lengthPXD(opxd);
2430 jfs_info("dtRelocate: lmxaddr:%Ld xaddr:%Ld:%Ld xlen:%d",
2431 (long long)lmxaddr, (long long)oxaddr, (long long)nxaddr,
2435 * 1. get the internal parent dtpage covering
2436 * router entry for the tartget page to be relocated;
2438 rc = dtSearchNode(ip, lmxaddr, opxd, &btstack);
2442 /* retrieve search result */
2443 DT_GETSEARCH(ip, btstack.top, bn, pmp, pp, index);
2444 jfs_info("dtRelocate: parent router entry validated.");
2447 * 2. relocate the target dtpage
2449 /* read in the target page from src extent */
2450 DT_GETPAGE(ip, oxaddr, mp, PSIZE, p, rc);
2452 /* release the pinned parent page */
2458 * read in sibling pages if any to update sibling pointers;
2461 if (p->header.next) {
2462 nextbn = le64_to_cpu(p->header.next);
2463 DT_GETPAGE(ip, nextbn, rmp, PSIZE, rp, rc);
2472 if (p->header.prev) {
2473 prevbn = le64_to_cpu(p->header.prev);
2474 DT_GETPAGE(ip, prevbn, lmp, PSIZE, lp, rc);
2484 /* at this point, all xtpages to be updated are in memory */
2487 * update sibling pointers of sibling dtpages if any;
2490 tlck = txLock(tid, ip, lmp, tlckDTREE | tlckRELINK);
2491 dtlck = (struct dt_lock *) & tlck->lock;
2492 /* linelock header */
2493 ASSERT(dtlck->index == 0);
2494 lv = & dtlck->lv[0];
2499 lp->header.next = cpu_to_le64(nxaddr);
2504 tlck = txLock(tid, ip, rmp, tlckDTREE | tlckRELINK);
2505 dtlck = (struct dt_lock *) & tlck->lock;
2506 /* linelock header */
2507 ASSERT(dtlck->index == 0);
2508 lv = & dtlck->lv[0];
2513 rp->header.prev = cpu_to_le64(nxaddr);
2518 * update the target dtpage to be relocated
2520 * write LOG_REDOPAGE of LOG_NEW type for dst page
2521 * for the whole target page (logredo() will apply
2522 * after image and update bmap for allocation of the
2523 * dst extent), and update bmap for allocation of
2526 tlck = txLock(tid, ip, mp, tlckDTREE | tlckNEW);
2527 dtlck = (struct dt_lock *) & tlck->lock;
2528 /* linelock header */
2529 ASSERT(dtlck->index == 0);
2530 lv = & dtlck->lv[0];
2532 /* update the self address in the dtpage header */
2533 pxd = &p->header.self;
2534 PXDaddress(pxd, nxaddr);
2536 /* the dst page is the same as the src page, i.e.,
2537 * linelock for afterimage of the whole page;
2540 lv->length = p->header.maxslot;
2543 /* update the buffer extent descriptor of the dtpage */
2544 xsize = xlen << JFS_SBI(ip->i_sb)->l2bsize;
2545 #ifdef _STILL_TO_PORT
2546 bmSetXD(mp, nxaddr, xsize);
2547 #endif /* _STILL_TO_PORT */
2548 /* unpin the relocated page */
2550 jfs_info("dtRelocate: target dtpage relocated.");
2552 /* the moved extent is dtpage, then a LOG_NOREDOPAGE log rec
2553 * needs to be written (in logredo(), the LOG_NOREDOPAGE log rec
2554 * will also force a bmap update ).
2558 * 3. acquire maplock for the source extent to be freed;
2560 /* for dtpage relocation, write a LOG_NOREDOPAGE record
2561 * for the source dtpage (logredo() will init NoRedoPage
2562 * filter and will also update bmap for free of the source
2563 * dtpage), and upadte bmap for free of the source dtpage;
2565 tlck = txMaplock(tid, ip, tlckDTREE | tlckFREE);
2566 pxdlock = (struct pxd_lock *) & tlck->lock;
2567 pxdlock->flag = mlckFREEPXD;
2568 PXDaddress(&pxdlock->pxd, oxaddr);
2569 PXDlength(&pxdlock->pxd, xlen);
2573 * 4. update the parent router entry for relocation;
2575 * acquire tlck for the parent entry covering the target dtpage;
2576 * write LOG_REDOPAGE to apply after image only;
2578 jfs_info("dtRelocate: update parent router entry.");
2579 tlck = txLock(tid, ip, pmp, tlckDTREE | tlckENTRY);
2580 dtlck = (struct dt_lock *) & tlck->lock;
2581 lv = & dtlck->lv[dtlck->index];
2583 /* update the PXD with the new address */
2584 stbl = DT_GETSTBL(pp);
2585 pxd = (pxd_t *) & pp->slot[stbl[index]];
2586 PXDaddress(pxd, nxaddr);
2587 lv->offset = stbl[index];
2591 /* unpin the parent dtpage */
2598 * NAME: dtSearchNode()
2600 * FUNCTION: Search for an dtpage containing a specified address
2601 * This function is mainly used by defragfs utility.
2603 * NOTE: Search result on stack, the found page is pinned at exit.
2604 * The result page must be an internal dtpage.
2605 * lmxaddr give the address of the left most page of the
2606 * dtree level, in which the required dtpage resides.
2608 static int dtSearchNode(struct inode *ip, s64 lmxaddr, pxd_t * kpxd,
2609 struct btstack * btstack)
2613 struct metapage *mp;
2615 int psize = 288; /* initial in-line directory */
2619 struct btframe *btsp;
2621 BT_CLR(btstack); /* reset stack */
2624 * descend tree to the level with specified leftmost page
2626 * by convention, root bn = 0.
2629 /* get/pin the page to search */
2630 DT_GETPAGE(ip, bn, mp, psize, p, rc);
2634 /* does the xaddr of leftmost page of the levevl
2635 * matches levevl search key ?
2637 if (p->header.flag & BT_ROOT) {
2640 } else if (addressPXD(&p->header.self) == lmxaddr)
2644 * descend down to leftmost child page
2646 if (p->header.flag & BT_LEAF) {
2651 /* get the leftmost entry */
2652 stbl = DT_GETSTBL(p);
2653 pxd = (pxd_t *) & p->slot[stbl[0]];
2655 /* get the child page block address */
2656 bn = addressPXD(pxd);
2657 psize = lengthPXD(pxd) << JFS_SBI(ip->i_sb)->l2bsize;
2658 /* unpin the parent page */
2663 * search each page at the current levevl
2666 stbl = DT_GETSTBL(p);
2667 for (i = 0; i < p->header.nextindex; i++) {
2668 pxd = (pxd_t *) & p->slot[stbl[i]];
2670 /* found the specified router entry */
2671 if (addressPXD(pxd) == addressPXD(kpxd) &&
2672 lengthPXD(pxd) == lengthPXD(kpxd)) {
2673 btsp = btstack->top;
2682 /* get the right sibling page if any */
2684 bn = le64_to_cpu(p->header.next);
2690 /* unpin current page */
2693 /* get the right sibling page */
2694 DT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
2700 #endif /* _NOTYET */
2706 * link around a freed page.
2709 * fp: page to be freed
2713 static int dtRelink(tid_t tid, struct inode *ip, dtpage_t * p)
2716 struct metapage *mp;
2719 struct dt_lock *dtlck;
2722 nextbn = le64_to_cpu(p->header.next);
2723 prevbn = le64_to_cpu(p->header.prev);
2725 /* update prev pointer of the next page */
2727 DT_GETPAGE(ip, nextbn, mp, PSIZE, p, rc);
2731 BT_MARK_DIRTY(mp, ip);
2733 * acquire a transaction lock on the next page
2735 * action: update prev pointer;
2737 tlck = txLock(tid, ip, mp, tlckDTREE | tlckRELINK);
2738 jfs_info("dtRelink nextbn: tlck = 0x%p, ip = 0x%p, mp=0x%p",
2740 dtlck = (struct dt_lock *) & tlck->lock;
2742 /* linelock header */
2743 if (dtlck->index >= dtlck->maxcnt)
2744 dtlck = (struct dt_lock *) txLinelock(dtlck);
2745 lv = & dtlck->lv[dtlck->index];
2750 p->header.prev = cpu_to_le64(prevbn);
2754 /* update next pointer of the previous page */
2756 DT_GETPAGE(ip, prevbn, mp, PSIZE, p, rc);
2760 BT_MARK_DIRTY(mp, ip);
2762 * acquire a transaction lock on the prev page
2764 * action: update next pointer;
2766 tlck = txLock(tid, ip, mp, tlckDTREE | tlckRELINK);
2767 jfs_info("dtRelink prevbn: tlck = 0x%p, ip = 0x%p, mp=0x%p",
2769 dtlck = (struct dt_lock *) & tlck->lock;
2771 /* linelock header */
2772 if (dtlck->index >= dtlck->maxcnt)
2773 dtlck = (struct dt_lock *) txLinelock(dtlck);
2774 lv = & dtlck->lv[dtlck->index];
2779 p->header.next = cpu_to_le64(nextbn);
2790 * initialize directory root (inline in inode)
2792 void dtInitRoot(tid_t tid, struct inode *ip, u32 idotdot)
2794 struct jfs_inode_info *jfs_ip = JFS_IP(ip);
2799 struct dt_lock *dtlck;
2804 * If this was previously an non-empty directory, we need to remove
2805 * the old directory table.
2808 if (jfs_ip->next_index > (MAX_INLINE_DIRTABLE_ENTRY + 1)) {
2809 struct tblock *tblk = tid_to_tblock(tid);
2811 * We're playing games with the tid's xflag. If
2812 * we're removing a regular file, the file's xtree
2813 * is committed with COMMIT_PMAP, but we always
2814 * commit the directories xtree with COMMIT_PWMAP.
2816 xflag_save = tblk->xflag;
2819 * xtTruncate isn't guaranteed to fully truncate
2820 * the xtree. The caller needs to check i_size
2821 * after committing the transaction to see if
2822 * additional truncation is needed. The
2823 * COMMIT_Stale flag tells caller that we
2824 * initiated the truncation.
2826 xtTruncate(tid, ip, 0, COMMIT_PWMAP);
2827 set_cflag(COMMIT_Stale, ip);
2829 tblk->xflag = xflag_save;
2833 jfs_ip->next_index = 2;
2835 ip->i_size = IDATASIZE;
2838 * acquire a transaction lock on the root
2840 * action: directory initialization;
2842 tlck = txLock(tid, ip, (struct metapage *) & jfs_ip->bxflag,
2843 tlckDTREE | tlckENTRY | tlckBTROOT);
2844 dtlck = (struct dt_lock *) & tlck->lock;
2847 ASSERT(dtlck->index == 0);
2848 lv = & dtlck->lv[0];
2850 lv->length = DTROOTMAXSLOT;
2853 p = &jfs_ip->i_dtroot;
2855 p->header.flag = DXD_INDEX | BT_ROOT | BT_LEAF;
2857 p->header.nextindex = 0;
2863 /* init data area of root */
2864 for (fsi++; fsi < DTROOTMAXSLOT; f++, fsi++)
2868 p->header.freelist = 1;
2869 p->header.freecnt = 8;
2871 /* init '..' entry */
2872 p->header.idotdot = cpu_to_le32(idotdot);
2875 ip->i_blocks = LBLK2PBLK(ip->i_sb,
2876 ((jfs_ip->ea.flag & DXD_EXTENT) ?
2877 lengthDXD(&jfs_ip->ea) : 0) +
2878 ((jfs_ip->acl.flag & DXD_EXTENT) ?
2879 lengthDXD(&jfs_ip->acl) : 0));
2886 * add_missing_indices()
2888 * function: Fix dtree page in which one or more entries has an invalid index.
2889 * fsck.jfs should really fix this, but it currently does not.
2890 * Called from jfs_readdir when bad index is detected.
2892 static void add_missing_indices(struct inode *inode, s64 bn)
2895 struct dt_lock *dtlck;
2899 struct metapage *mp;
2906 tid = txBegin(inode->i_sb, 0);
2908 DT_GETPAGE(inode, bn, mp, PSIZE, p, rc);
2911 printk(KERN_ERR "DT_GETPAGE failed!\n");
2914 BT_MARK_DIRTY(mp, inode);
2916 ASSERT(p->header.flag & BT_LEAF);
2918 tlck = txLock(tid, inode, mp, tlckDTREE | tlckENTRY);
2919 dtlck = (struct dt_lock *) &tlck->lock;
2921 stbl = DT_GETSTBL(p);
2922 for (i = 0; i < p->header.nextindex; i++) {
2923 d = (struct ldtentry *) &p->slot[stbl[i]];
2924 index = le32_to_cpu(d->index);
2925 if ((index < 2) || (index >= JFS_IP(inode)->next_index)) {
2926 d->index = cpu_to_le32(add_index(tid, inode, bn, i));
2927 if (dtlck->index >= dtlck->maxcnt)
2928 dtlck = (struct dt_lock *) txLinelock(dtlck);
2929 lv = &dtlck->lv[dtlck->index];
2930 lv->offset = stbl[i];
2937 (void) txCommit(tid, 1, &inode, 0);
2943 * Buffer to hold directory entry info while traversing a dtree page
2944 * before being fed to the filldir function
2954 * function to determine next variable-sized jfs_dirent in buffer
2956 static inline struct jfs_dirent *next_jfs_dirent(struct jfs_dirent *dirent)
2958 return (struct jfs_dirent *)
2960 ((sizeof (struct jfs_dirent) + dirent->name_len + 1 +
2961 sizeof (loff_t) - 1) &
2962 ~(sizeof (loff_t) - 1)));
2968 * function: read directory entries sequentially
2969 * from the specified entry offset
2973 * return: offset = (pn, index) of start entry
2974 * of next jfs_readdir()/dtRead()
2976 int jfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
2978 struct inode *ip = filp->f_dentry->d_inode;
2979 struct nls_table *codepage = JFS_SBI(ip->i_sb)->nls_tab;
2981 loff_t dtpos; /* legacy OS/2 style position */
2986 } *dtoffset = (struct dtoffset *) &dtpos;
2988 struct metapage *mp;
2992 struct btstack btstack;
2996 int d_namleft, len, outlen;
2997 unsigned long dirent_buf;
3001 uint loop_count = 0;
3002 struct jfs_dirent *jfs_dirent;
3004 int overflow, fix_page, page_fixed = 0;
3005 static int unique_pos = 2; /* If we can't fix broken index */
3007 if (filp->f_pos == DIREND)
3012 * persistent index is stored in directory entries.
3013 * Special cases: 0 = .
3015 * -1 = End of directory
3019 dir_index = (u32) filp->f_pos;
3021 if (dir_index > 1) {
3022 struct dir_table_slot dirtab_slot;
3025 (dir_index >= JFS_IP(ip)->next_index)) {
3026 /* Stale position. Directory has shrunk */
3027 filp->f_pos = DIREND;
3031 rc = read_index(ip, dir_index, &dirtab_slot);
3033 filp->f_pos = DIREND;
3036 if (dirtab_slot.flag == DIR_INDEX_FREE) {
3037 if (loop_count++ > JFS_IP(ip)->next_index) {
3038 jfs_err("jfs_readdir detected "
3040 filp->f_pos = DIREND;
3043 dir_index = le32_to_cpu(dirtab_slot.addr2);
3044 if (dir_index == -1) {
3045 filp->f_pos = DIREND;
3050 bn = addressDTS(&dirtab_slot);
3051 index = dirtab_slot.slot;
3052 DT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
3054 filp->f_pos = DIREND;
3057 if (p->header.flag & BT_INTERNAL) {
3058 jfs_err("jfs_readdir: bad index table");
3064 if (dir_index == 0) {
3069 if (filldir(dirent, ".", 1, 0, ip->i_ino,
3077 if (filldir(dirent, "..", 2, 1, PARENT(ip), DT_DIR))
3081 * Find first entry of left-most leaf
3084 filp->f_pos = DIREND;
3088 if ((rc = dtReadFirst(ip, &btstack)))
3091 DT_GETSEARCH(ip, btstack.top, bn, mp, p, index);
3095 * Legacy filesystem - OS/2 & Linux JFS < 0.3.6
3097 * pn = index = 0: First entry "."
3098 * pn = 0; index = 1: Second entry ".."
3099 * pn > 0: Real entries, pn=1 -> leftmost page
3100 * pn = index = -1: No more entries
3102 dtpos = filp->f_pos;
3104 /* build "." entry */
3106 if (filldir(dirent, ".", 1, filp->f_pos, ip->i_ino,
3109 dtoffset->index = 1;
3110 filp->f_pos = dtpos;
3113 if (dtoffset->pn == 0) {
3114 if (dtoffset->index == 1) {
3115 /* build ".." entry */
3117 if (filldir(dirent, "..", 2, filp->f_pos,
3118 PARENT(ip), DT_DIR))
3121 jfs_err("jfs_readdir called with "
3125 dtoffset->index = 0;
3126 filp->f_pos = dtpos;
3130 filp->f_pos = DIREND;
3134 if ((rc = dtReadNext(ip, &filp->f_pos, &btstack))) {
3135 jfs_err("jfs_readdir: unexpected rc = %d "
3136 "from dtReadNext", rc);
3137 filp->f_pos = DIREND;
3140 /* get start leaf page and index */
3141 DT_GETSEARCH(ip, btstack.top, bn, mp, p, index);
3143 /* offset beyond directory eof ? */
3145 filp->f_pos = DIREND;
3150 dirent_buf = __get_free_page(GFP_KERNEL);
3151 if (dirent_buf == 0) {
3153 jfs_warn("jfs_readdir: __get_free_page failed!");
3154 filp->f_pos = DIREND;
3159 jfs_dirent = (struct jfs_dirent *) dirent_buf;
3161 overflow = fix_page = 0;
3163 stbl = DT_GETSTBL(p);
3165 for (i = index; i < p->header.nextindex; i++) {
3166 d = (struct ldtentry *) & p->slot[stbl[i]];
3168 if (((long) jfs_dirent + d->namlen + 1) >
3169 (dirent_buf + PSIZE)) {
3170 /* DBCS codepages could overrun dirent_buf */
3176 d_namleft = d->namlen;
3177 name_ptr = jfs_dirent->name;
3178 jfs_dirent->ino = le32_to_cpu(d->inumber);
3181 len = min(d_namleft, DTLHDRDATALEN);
3182 jfs_dirent->position = le32_to_cpu(d->index);
3184 * d->index should always be valid, but it
3185 * isn't. fsck.jfs doesn't create the
3186 * directory index for the lost+found
3187 * directory. Rather than let it go,
3188 * we can try to fix it.
3190 if ((jfs_dirent->position < 2) ||
3191 (jfs_dirent->position >=
3192 JFS_IP(ip)->next_index)) {
3193 if (!page_fixed && !isReadOnly(ip)) {
3196 * setting overflow and setting
3197 * index to i will cause the
3198 * same page to be processed
3199 * again starting here
3205 jfs_dirent->position = unique_pos++;
3208 jfs_dirent->position = dtpos;
3209 len = min(d_namleft, DTLHDRDATALEN_LEGACY);
3212 /* copy the name of head/only segment */
3213 outlen = jfs_strfromUCS_le(name_ptr, d->name, len,
3215 jfs_dirent->name_len = outlen;
3217 /* copy name in the additional segment(s) */
3220 t = (struct dtslot *) & p->slot[next];
3224 if (d_namleft == 0) {
3226 "JFS:Dtree error: ino = "
3227 "%ld, bn=%Ld, index = %d",
3233 len = min(d_namleft, DTSLOTDATALEN);
3234 outlen = jfs_strfromUCS_le(name_ptr, t->name,
3236 jfs_dirent->name_len += outlen;
3242 jfs_dirent = next_jfs_dirent(jfs_dirent);
3249 /* Point to next leaf page */
3250 if (p->header.flag & BT_ROOT)
3253 bn = le64_to_cpu(p->header.next);
3255 /* update offset (pn:index) for new page */
3258 dtoffset->index = 0;
3264 /* unpin previous leaf page */
3267 jfs_dirent = (struct jfs_dirent *) dirent_buf;
3268 while (jfs_dirents--) {
3269 filp->f_pos = jfs_dirent->position;
3270 if (filldir(dirent, jfs_dirent->name,
3271 jfs_dirent->name_len, filp->f_pos,
3272 jfs_dirent->ino, DT_UNKNOWN))
3274 jfs_dirent = next_jfs_dirent(jfs_dirent);
3278 add_missing_indices(ip, bn);
3282 if (!overflow && (bn == 0)) {
3283 filp->f_pos = DIREND;
3287 DT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
3289 free_page(dirent_buf);
3295 free_page(dirent_buf);
3304 * function: get the leftmost page of the directory
3306 static int dtReadFirst(struct inode *ip, struct btstack * btstack)
3310 int psize = 288; /* initial in-line directory */
3311 struct metapage *mp;
3314 struct btframe *btsp;
3317 BT_CLR(btstack); /* reset stack */
3320 * descend leftmost path of the tree
3322 * by convention, root bn = 0.
3325 DT_GETPAGE(ip, bn, mp, psize, p, rc);
3330 * leftmost leaf page
3332 if (p->header.flag & BT_LEAF) {
3333 /* return leftmost entry */
3334 btsp = btstack->top;
3343 * descend down to leftmost child page
3345 /* push (bn, index) of the parent page/entry */
3346 BT_PUSH(btstack, bn, 0);
3348 /* get the leftmost entry */
3349 stbl = DT_GETSTBL(p);
3350 xd = (pxd_t *) & p->slot[stbl[0]];
3352 /* get the child page block address */
3353 bn = addressPXD(xd);
3354 psize = lengthPXD(xd) << JFS_SBI(ip->i_sb)->l2bsize;
3356 /* unpin the parent page */
3365 * function: get the page of the specified offset (pn:index)
3367 * return: if (offset > eof), bn = -1;
3369 * note: if index > nextindex of the target leaf page,
3370 * start with 1st entry of next leaf page;
3372 static int dtReadNext(struct inode *ip, loff_t * offset,
3373 struct btstack * btstack)
3380 } *dtoffset = (struct dtoffset *) offset;
3382 struct metapage *mp;
3387 struct btframe *btsp, *parent;
3391 * get leftmost leaf page pinned
3393 if ((rc = dtReadFirst(ip, btstack)))
3397 DT_GETSEARCH(ip, btstack->top, bn, mp, p, index);
3399 /* get the start offset (pn:index) */
3400 pn = dtoffset->pn - 1; /* Now pn = 0 represents leftmost leaf */
3401 index = dtoffset->index;
3403 /* start at leftmost page ? */
3405 /* offset beyond eof ? */
3406 if (index < p->header.nextindex)
3409 if (p->header.flag & BT_ROOT) {
3414 /* start with 1st entry of next leaf page */
3416 dtoffset->index = index = 0;
3420 /* start at non-leftmost page: scan parent pages for large pn */
3421 if (p->header.flag & BT_ROOT) {
3426 /* start after next leaf page ? */
3430 /* get leaf page pn = 1 */
3432 bn = le64_to_cpu(p->header.next);
3434 /* unpin leaf page */
3437 /* offset beyond eof ? */
3446 * scan last internal page level to get target leaf page
3449 /* unpin leftmost leaf page */
3452 /* get left most parent page */
3453 btsp = btstack->top;
3456 DT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
3460 /* scan parent pages at last internal page level */
3461 while (pn >= p->header.nextindex) {
3462 pn -= p->header.nextindex;
3464 /* get next parent page address */
3465 bn = le64_to_cpu(p->header.next);
3467 /* unpin current parent page */
3470 /* offset beyond eof ? */
3476 /* get next parent page */
3477 DT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
3481 /* update parent page stack frame */
3485 /* get leaf page address */
3486 stbl = DT_GETSTBL(p);
3487 xd = (pxd_t *) & p->slot[stbl[pn]];
3488 bn = addressPXD(xd);
3490 /* unpin parent page */
3494 * get target leaf page
3497 DT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
3502 * leaf page has been completed:
3503 * start with 1st entry of next leaf page
3505 if (index >= p->header.nextindex) {
3506 bn = le64_to_cpu(p->header.next);
3508 /* unpin leaf page */
3511 /* offset beyond eof ? */
3517 /* get next leaf page */
3518 DT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
3522 /* start with 1st entry of next leaf page */
3524 dtoffset->index = 0;
3528 /* return target leaf page pinned */
3529 btsp = btstack->top;
3531 btsp->index = dtoffset->index;
3541 * function: compare search key with an internal entry
3544 * < 0 if k is < record
3545 * = 0 if k is = record
3546 * > 0 if k is > record
3548 static int dtCompare(struct component_name * key, /* search key */
3549 dtpage_t * p, /* directory page */
3551 { /* entry slot index */
3552 wchar_t *kname, *name;
3553 int klen, namlen, len, rc;
3554 struct idtentry *ih;
3558 * force the left-most key on internal pages, at any level of
3559 * the tree, to be less than any search key.
3560 * this obviates having to update the leftmost key on an internal
3561 * page when the user inserts a new key in the tree smaller than
3562 * anything that has been stored.
3564 * (? if/when dtSearch() narrows down to 1st entry (index = 0),
3565 * at any internal page at any level of the tree,
3566 * it descends to child of the entry anyway -
3567 * ? make the entry as min size dummy entry)
3569 * if (e->index == 0 && h->prevpg == P_INVALID && !(h->flags & BT_LEAF))
3576 ih = (struct idtentry *) & p->slot[si];
3579 namlen = ih->namlen;
3580 len = min(namlen, DTIHDRDATALEN);
3582 /* compare with head/only segment */
3583 len = min(klen, len);
3584 if ((rc = UniStrncmp_le(kname, name, len)))
3590 /* compare with additional segment(s) */
3592 while (klen > 0 && namlen > 0) {
3593 /* compare with next name segment */
3594 t = (struct dtslot *) & p->slot[si];
3595 len = min(namlen, DTSLOTDATALEN);
3596 len = min(klen, len);
3598 if ((rc = UniStrncmp_le(kname, name, len)))
3607 return (klen - namlen);
3616 * function: compare search key with an (leaf/internal) entry
3619 * < 0 if k is < record
3620 * = 0 if k is = record
3621 * > 0 if k is > record
3623 static int ciCompare(struct component_name * key, /* search key */
3624 dtpage_t * p, /* directory page */
3625 int si, /* entry slot index */
3628 wchar_t *kname, *name, x;
3629 int klen, namlen, len, rc;
3630 struct ldtentry *lh;
3631 struct idtentry *ih;
3636 * force the left-most key on internal pages, at any level of
3637 * the tree, to be less than any search key.
3638 * this obviates having to update the leftmost key on an internal
3639 * page when the user inserts a new key in the tree smaller than
3640 * anything that has been stored.
3642 * (? if/when dtSearch() narrows down to 1st entry (index = 0),
3643 * at any internal page at any level of the tree,
3644 * it descends to child of the entry anyway -
3645 * ? make the entry as min size dummy entry)
3647 * if (e->index == 0 && h->prevpg == P_INVALID && !(h->flags & BT_LEAF))
3657 if (p->header.flag & BT_LEAF) {
3658 lh = (struct ldtentry *) & p->slot[si];
3661 namlen = lh->namlen;
3662 if (flag & JFS_DIR_INDEX)
3663 len = min(namlen, DTLHDRDATALEN);
3665 len = min(namlen, DTLHDRDATALEN_LEGACY);
3668 * internal page entry
3671 ih = (struct idtentry *) & p->slot[si];
3674 namlen = ih->namlen;
3675 len = min(namlen, DTIHDRDATALEN);
3678 /* compare with head/only segment */
3679 len = min(klen, len);
3680 for (i = 0; i < len; i++, kname++, name++) {
3681 /* only uppercase if case-insensitive support is on */
3682 if ((flag & JFS_OS2) == JFS_OS2)
3683 x = UniToupper(le16_to_cpu(*name));
3685 x = le16_to_cpu(*name);
3686 if ((rc = *kname - x))
3693 /* compare with additional segment(s) */
3694 while (klen > 0 && namlen > 0) {
3695 /* compare with next name segment */
3696 t = (struct dtslot *) & p->slot[si];
3697 len = min(namlen, DTSLOTDATALEN);
3698 len = min(klen, len);
3700 for (i = 0; i < len; i++, kname++, name++) {
3701 /* only uppercase if case-insensitive support is on */
3702 if ((flag & JFS_OS2) == JFS_OS2)
3703 x = UniToupper(le16_to_cpu(*name));
3705 x = le16_to_cpu(*name);
3707 if ((rc = *kname - x))
3716 return (klen - namlen);
3721 * ciGetLeafPrefixKey()
3723 * function: compute prefix of suffix compression
3724 * from two adjacent leaf entries
3725 * across page boundary
3727 * return: non-zero on error
3730 static int ciGetLeafPrefixKey(dtpage_t * lp, int li, dtpage_t * rp,
3731 int ri, struct component_name * key, int flag)
3734 wchar_t *pl, *pr, *kname;
3735 struct component_name lkey;
3736 struct component_name rkey;
3738 lkey.name = (wchar_t *) kmalloc((JFS_NAME_MAX + 1) * sizeof(wchar_t),
3740 if (lkey.name == NULL)
3743 rkey.name = (wchar_t *) kmalloc((JFS_NAME_MAX + 1) * sizeof(wchar_t),
3745 if (rkey.name == NULL) {
3750 /* get left and right key */
3751 dtGetKey(lp, li, &lkey, flag);
3752 lkey.name[lkey.namlen] = 0;
3754 if ((flag & JFS_OS2) == JFS_OS2)
3757 dtGetKey(rp, ri, &rkey, flag);
3758 rkey.name[rkey.namlen] = 0;
3761 if ((flag & JFS_OS2) == JFS_OS2)
3764 /* compute prefix */
3767 namlen = min(lkey.namlen, rkey.namlen);
3768 for (pl = lkey.name, pr = rkey.name;
3769 namlen; pl++, pr++, namlen--, klen++, kname++) {
3772 key->namlen = klen + 1;
3777 /* l->namlen <= r->namlen since l <= r */
3778 if (lkey.namlen < rkey.namlen) {
3780 key->namlen = klen + 1;
3781 } else /* l->namelen == r->namelen */
3795 * function: get key of the entry
3797 static void dtGetKey(dtpage_t * p, int i, /* entry index */
3798 struct component_name * key, int flag)
3802 struct ldtentry *lh;
3803 struct idtentry *ih;
3806 wchar_t *name, *kname;
3809 stbl = DT_GETSTBL(p);
3811 if (p->header.flag & BT_LEAF) {
3812 lh = (struct ldtentry *) & p->slot[si];
3814 namlen = lh->namlen;
3816 if (flag & JFS_DIR_INDEX)
3817 len = min(namlen, DTLHDRDATALEN);
3819 len = min(namlen, DTLHDRDATALEN_LEGACY);
3821 ih = (struct idtentry *) & p->slot[si];
3823 namlen = ih->namlen;
3825 len = min(namlen, DTIHDRDATALEN);
3828 key->namlen = namlen;
3832 * move head/only segment
3834 UniStrncpy_le(kname, name, len);
3837 * move additional segment(s)
3840 /* get next segment */
3844 len = min(namlen, DTSLOTDATALEN);
3845 UniStrncpy_le(kname, t->name, len);
3855 * function: allocate free slot(s) and
3856 * write a leaf/internal entry
3858 * return: entry slot index
3860 static void dtInsertEntry(dtpage_t * p, int index, struct component_name * key,
3861 ddata_t * data, struct dt_lock ** dtlock)
3863 struct dtslot *h, *t;
3864 struct ldtentry *lh = 0;
3865 struct idtentry *ih = 0;
3866 int hsi, fsi, klen, len, nextindex;
3867 wchar_t *kname, *name;
3870 struct dt_lock *dtlck = *dtlock;
3874 struct metapage *mp = 0;
3879 /* allocate a free slot */
3880 hsi = fsi = p->header.freelist;
3882 p->header.freelist = h->next;
3883 --p->header.freecnt;
3885 /* open new linelock */
3886 if (dtlck->index >= dtlck->maxcnt)
3887 dtlck = (struct dt_lock *) txLinelock(dtlck);
3889 lv = & dtlck->lv[dtlck->index];
3892 /* write head/only segment */
3893 if (p->header.flag & BT_LEAF) {
3894 lh = (struct ldtentry *) h;
3896 lh->inumber = data->leaf.ino; /* little-endian */
3899 if (data->leaf.ip) {
3900 len = min(klen, DTLHDRDATALEN);
3901 if (!(p->header.flag & BT_ROOT))
3902 bn = addressPXD(&p->header.self);
3903 lh->index = cpu_to_le32(add_index(data->leaf.tid,
3907 len = min(klen, DTLHDRDATALEN_LEGACY);
3909 ih = (struct idtentry *) h;
3915 len = min(klen, DTIHDRDATALEN);
3918 UniStrncpy_le(name, kname, len);
3923 /* write additional segment(s) */
3928 fsi = p->header.freelist;
3930 p->header.freelist = t->next;
3931 --p->header.freecnt;
3933 /* is next slot contiguous ? */
3934 if (fsi != xsi + 1) {
3935 /* close current linelock */
3939 /* open new linelock */
3940 if (dtlck->index < dtlck->maxcnt)
3943 dtlck = (struct dt_lock *) txLinelock(dtlck);
3944 lv = & dtlck->lv[0];
3952 len = min(klen, DTSLOTDATALEN);
3953 UniStrncpy_le(t->name, kname, len);
3960 /* close current linelock */
3966 /* terminate last/only segment */
3968 /* single segment entry */
3969 if (p->header.flag & BT_LEAF)
3974 /* multi-segment entry */
3977 /* if insert into middle, shift right succeeding entries in stbl */
3978 stbl = DT_GETSTBL(p);
3979 nextindex = p->header.nextindex;
3980 if (index < nextindex) {
3981 memmove(stbl + index + 1, stbl + index, nextindex - index);
3983 if ((p->header.flag & BT_LEAF) && data->leaf.ip) {
3987 * Need to update slot number for entries that moved
3991 for (n = index + 1; n <= nextindex; n++) {
3992 lh = (struct ldtentry *) & (p->slot[stbl[n]]);
3993 modify_index(data->leaf.tid, data->leaf.ip,
3994 le32_to_cpu(lh->index), bn, n,
3998 release_metapage(mp);
4004 /* advance next available entry index of stbl */
4005 ++p->header.nextindex;
4012 * function: move entries from split/left page to new/right page
4014 * nextindex of dst page and freelist/freecnt of both pages
4017 static void dtMoveEntry(dtpage_t * sp, int si, dtpage_t * dp,
4018 struct dt_lock ** sdtlock, struct dt_lock ** ddtlock,
4021 int ssi, next; /* src slot index */
4022 int di; /* dst entry index */
4023 int dsi; /* dst slot index */
4024 s8 *sstbl, *dstbl; /* sorted entry table */
4026 struct ldtentry *slh, *dlh = 0;
4027 struct idtentry *sih, *dih = 0;
4028 struct dtslot *h, *s, *d;
4029 struct dt_lock *sdtlck = *sdtlock, *ddtlck = *ddtlock;
4030 struct lv *slv, *dlv;
4034 sstbl = (s8 *) & sp->slot[sp->header.stblindex];
4035 dstbl = (s8 *) & dp->slot[dp->header.stblindex];
4037 dsi = dp->header.freelist; /* first (whole page) free slot */
4038 sfsi = sp->header.freelist;
4040 /* linelock destination entry slot */
4041 dlv = & ddtlck->lv[ddtlck->index];
4044 /* linelock source entry slot */
4045 slv = & sdtlck->lv[sdtlck->index];
4046 slv->offset = sstbl[si];
4047 xssi = slv->offset - 1;
4053 for (di = 0; si < sp->header.nextindex; si++, di++) {
4057 /* is next slot contiguous ? */
4058 if (ssi != xssi + 1) {
4059 /* close current linelock */
4063 /* open new linelock */
4064 if (sdtlck->index < sdtlck->maxcnt)
4067 sdtlck = (struct dt_lock *) txLinelock(sdtlck);
4068 slv = & sdtlck->lv[0];
4076 * move head/only segment of an entry
4079 h = d = &dp->slot[dsi];
4081 /* get src slot and move */
4083 if (sp->header.flag & BT_LEAF) {
4084 /* get source entry */
4085 slh = (struct ldtentry *) s;
4086 dlh = (struct ldtentry *) h;
4087 snamlen = slh->namlen;
4090 len = min(snamlen, DTLHDRDATALEN);
4091 dlh->index = slh->index; /* little-endian */
4093 len = min(snamlen, DTLHDRDATALEN_LEGACY);
4095 memcpy(dlh, slh, 6 + len * 2);
4099 /* update dst head/only segment next field */
4103 sih = (struct idtentry *) s;
4104 snamlen = sih->namlen;
4106 len = min(snamlen, DTIHDRDATALEN);
4107 dih = (struct idtentry *) h;
4108 memcpy(dih, sih, 10 + len * 2);
4115 /* free src head/only segment */
4125 * move additional segment(s) of the entry
4128 while ((ssi = next) >= 0) {
4129 /* is next slot contiguous ? */
4130 if (ssi != xssi + 1) {
4131 /* close current linelock */
4135 /* open new linelock */
4136 if (sdtlck->index < sdtlck->maxcnt)
4142 slv = & sdtlck->lv[0];
4149 /* get next source segment */
4152 /* get next destination free slot */
4155 len = min(snamlen, DTSLOTDATALEN);
4156 UniStrncpy(d->name, s->name, len);
4165 /* free source segment */
4174 /* terminate dst last/only segment */
4176 /* single segment entry */
4177 if (dp->header.flag & BT_LEAF)
4182 /* multi-segment entry */
4186 /* close current linelock */
4195 /* update source header */
4196 sp->header.freelist = sfsi;
4197 sp->header.freecnt += nd;
4199 /* update destination header */
4200 dp->header.nextindex = di;
4202 dp->header.freelist = dsi;
4203 dp->header.freecnt -= nd;
4210 * function: free a (leaf/internal) entry
4212 * log freelist header, stbl, and each segment slot of entry
4213 * (even though last/only segment next field is modified,
4214 * physical image logging requires all segment slots of
4215 * the entry logged to avoid applying previous updates
4216 * to the same slots)
4218 static void dtDeleteEntry(dtpage_t * p, int fi, struct dt_lock ** dtlock)
4220 int fsi; /* free entry slot index */
4224 struct dt_lock *dtlck = *dtlock;
4228 /* get free entry slot index */
4229 stbl = DT_GETSTBL(p);
4232 /* open new linelock */
4233 if (dtlck->index >= dtlck->maxcnt)
4234 dtlck = (struct dt_lock *) txLinelock(dtlck);
4235 lv = & dtlck->lv[dtlck->index];
4239 /* get the head/only segment */
4241 if (p->header.flag & BT_LEAF)
4242 si = ((struct ldtentry *) t)->next;
4244 si = ((struct idtentry *) t)->next;
4251 /* find the last/only segment */
4253 /* is next slot contiguous ? */
4254 if (si != xsi + 1) {
4255 /* close current linelock */
4259 /* open new linelock */
4260 if (dtlck->index < dtlck->maxcnt)
4263 dtlck = (struct dt_lock *) txLinelock(dtlck);
4264 lv = & dtlck->lv[0];
4280 /* close current linelock */
4286 /* update freelist */
4287 t->next = p->header.freelist;
4288 p->header.freelist = fsi;
4289 p->header.freecnt += freecnt;
4291 /* if delete from middle,
4292 * shift left the succedding entries in the stbl
4294 si = p->header.nextindex;
4296 memmove(&stbl[fi], &stbl[fi + 1], si - fi - 1);
4298 p->header.nextindex--;
4305 * function: truncate a (leaf/internal) entry
4307 * log freelist header, stbl, and each segment slot of entry
4308 * (even though last/only segment next field is modified,
4309 * physical image logging requires all segment slots of
4310 * the entry logged to avoid applying previous updates
4311 * to the same slots)
4313 static void dtTruncateEntry(dtpage_t * p, int ti, struct dt_lock ** dtlock)
4315 int tsi; /* truncate entry slot index */
4319 struct dt_lock *dtlck = *dtlock;
4323 /* get free entry slot index */
4324 stbl = DT_GETSTBL(p);
4327 /* open new linelock */
4328 if (dtlck->index >= dtlck->maxcnt)
4329 dtlck = (struct dt_lock *) txLinelock(dtlck);
4330 lv = & dtlck->lv[dtlck->index];
4334 /* get the head/only segment */
4336 ASSERT(p->header.flag & BT_INTERNAL);
4337 ((struct idtentry *) t)->namlen = 0;
4338 si = ((struct idtentry *) t)->next;
4339 ((struct idtentry *) t)->next = -1;
4346 /* find the last/only segment */
4348 /* is next slot contiguous ? */
4349 if (si != xsi + 1) {
4350 /* close current linelock */
4354 /* open new linelock */
4355 if (dtlck->index < dtlck->maxcnt)
4358 dtlck = (struct dt_lock *) txLinelock(dtlck);
4359 lv = & dtlck->lv[0];
4375 /* close current linelock */
4381 /* update freelist */
4384 t->next = p->header.freelist;
4385 p->header.freelist = fsi;
4386 p->header.freecnt += freecnt;
4391 * dtLinelockFreelist()
4393 static void dtLinelockFreelist(dtpage_t * p, /* directory page */
4394 int m, /* max slot index */
4395 struct dt_lock ** dtlock)
4397 int fsi; /* free entry slot index */
4400 struct dt_lock *dtlck = *dtlock;
4404 /* get free entry slot index */
4405 fsi = p->header.freelist;
4407 /* open new linelock */
4408 if (dtlck->index >= dtlck->maxcnt)
4409 dtlck = (struct dt_lock *) txLinelock(dtlck);
4410 lv = & dtlck->lv[dtlck->index];
4420 /* find the last/only segment */
4421 while (si < m && si >= 0) {
4422 /* is next slot contiguous ? */
4423 if (si != xsi + 1) {
4424 /* close current linelock */
4428 /* open new linelock */
4429 if (dtlck->index < dtlck->maxcnt)
4432 dtlck = (struct dt_lock *) txLinelock(dtlck);
4433 lv = & dtlck->lv[0];
4447 /* close current linelock */
4458 * FUNCTION: Modify the inode number part of a directory entry
4461 * tid - Transaction id
4462 * ip - Inode of parent directory
4463 * key - Name of entry to be modified
4464 * orig_ino - Original inode number expected in entry
4465 * new_ino - New inode number to put into entry
4469 * -ESTALE - If entry found does not match orig_ino passed in
4470 * -ENOENT - If no entry can be found to match key
4471 * 0 - If successfully modified entry
4473 int dtModify(tid_t tid, struct inode *ip,
4474 struct component_name * key, ino_t * orig_ino, ino_t new_ino, int flag)
4478 struct metapage *mp;
4481 struct btstack btstack;
4483 struct dt_lock *dtlck;
4486 int entry_si; /* entry slot index */
4487 struct ldtentry *entry;
4490 * search for the entry to modify:
4492 * dtSearch() returns (leaf page pinned, index at which to modify).
4494 if ((rc = dtSearch(ip, key, orig_ino, &btstack, flag)))
4497 /* retrieve search result */
4498 DT_GETSEARCH(ip, btstack.top, bn, mp, p, index);
4500 BT_MARK_DIRTY(mp, ip);
4502 * acquire a transaction lock on the leaf page of named entry
4504 tlck = txLock(tid, ip, mp, tlckDTREE | tlckENTRY);
4505 dtlck = (struct dt_lock *) & tlck->lock;
4507 /* get slot index of the entry */
4508 stbl = DT_GETSTBL(p);
4509 entry_si = stbl[index];
4511 /* linelock entry */
4512 ASSERT(dtlck->index == 0);
4513 lv = & dtlck->lv[0];
4514 lv->offset = entry_si;
4518 /* get the head/only segment */
4519 entry = (struct ldtentry *) & p->slot[entry_si];
4521 /* substitute the inode number of the entry */
4522 entry->inumber = cpu_to_le32(new_ino);
4524 /* unpin the leaf page */
4530 #ifdef _JFS_DEBUG_DTREE
4534 * function: traverse forward
4536 int dtDisplayTree(struct inode *ip)
4539 struct metapage *mp;
4542 int index, lastindex, v, h;
4544 struct btstack btstack;
4545 struct btframe *btsp;
4546 struct btframe *parent;
4550 printk("display B+-tree.\n");
4553 btsp = btstack.stack;
4558 * root resides in the inode
4564 * first access of each page:
4567 DT_GETPAGE(ip, bn, mp, psize, p, rc);
4571 /* process entries forward from first index */
4573 lastindex = p->header.nextindex - 1;
4575 if (p->header.flag & BT_INTERNAL) {
4577 * first access of each internal page
4579 printf("internal page ");
4580 dtDisplayPage(ip, bn, p);
4583 } else { /* (p->header.flag & BT_LEAF) */
4586 * first access of each leaf page
4588 printf("leaf page ");
4589 dtDisplayPage(ip, bn, p);
4592 * process leaf page entries
4594 for ( ; index <= lastindex; index++)
4599 /* unpin the leaf page */
4604 * go back up to the parent page
4607 /* pop/restore parent entry for the current child page */
4608 if ((parent = (btsp == btstack.stack ? NULL : --btsp)) == NULL)
4609 /* current page must have been root */
4613 * parent page scan completed
4615 if ((index = parent->index) == (lastindex = parent->lastindex)) {
4616 /* go back up to the parent page */
4621 * parent page has entries remaining
4623 /* get back the parent page */
4625 /* v = parent->level; */
4626 DT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
4630 /* get next parent entry */
4634 * internal page: go down to child page of current entry
4637 /* push/save current parent entry for the child page */
4638 btsp->bn = pbn = bn;
4639 btsp->index = index;
4640 btsp->lastindex = lastindex;
4641 /* btsp->level = v; */
4642 /* btsp->node = h; */
4645 /* get current entry for the child page */
4646 stbl = DT_GETSTBL(p);
4647 xd = (pxd_t *) & p->slot[stbl[index]];
4650 * first access of each internal entry:
4653 /* get child page */
4654 bn = addressPXD(xd);
4655 psize = lengthPXD(xd) << ip->i_ipmnt->i_l2bsize;
4657 printk("traverse down 0x%Lx[%d]->0x%Lx\n", pbn, index, bn);
4661 /* release parent page */
4664 /* process the child page */
4672 * function: display page
4674 int dtDisplayPage(struct inode *ip, s64 bn, dtpage_t * p)
4677 struct metapage *mp;
4678 struct ldtentry *lh;
4679 struct idtentry *ih;
4683 wchar_t name[JFS_NAME_MAX + 1];
4684 struct component_name key = { 0, name };
4689 DT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
4694 /* display page control */
4695 printk("bn:0x%Lx flag:0x%08x nextindex:%d\n",
4696 bn, p->header.flag, p->header.nextindex);
4698 /* display entries */
4699 stbl = DT_GETSTBL(p);
4700 for (i = 0, j = 1; i < p->header.nextindex; i++, j++) {
4701 dtGetKey(p, i, &key, JFS_SBI(ip->i_sb)->mntflag);
4702 key.name[key.namlen] = '\0';
4703 if (p->header.flag & BT_LEAF) {
4704 lh = (struct ldtentry *) & p->slot[stbl[i]];
4705 printf("\t[%d] %s:%d", i, key.name,
4706 le32_to_cpu(lh->inumber));
4708 ih = (struct idtentry *) & p->slot[stbl[i]];
4710 bn = addressPXD(xd);
4711 printf("\t[%d] %s:0x%Lx", i, key.name, bn);
4727 #endif /* _JFS_DEBUG_DTREE */