2 * Copyright (C) International Business Machines Corp., 2000-2004
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)) {
377 struct dir_table_slot temp_table[12];
380 * It's time to move the inline table to an external
381 * page and begin to build the xtree
383 if (dbAlloc(ip, 0, sbi->nbperpage, &xaddr))
384 goto clean_up; /* No space */
387 * Save the table, we're going to overwrite it with the
390 memcpy(temp_table, &jfs_ip->i_dirtable, sizeof(temp_table));
393 * Initialize empty x-tree
398 * Allocate the first block & add it to the xtree
400 if (xtInsert(tid, ip, 0, 0, sbi->nbperpage, &xaddr, 0)) {
401 /* This really shouldn't fail */
402 jfs_warn("add_index: xtInsert failed!");
403 memcpy(&jfs_ip->i_dirtable, temp_table,
404 sizeof (temp_table));
408 ip->i_blocks += LBLK2PBLK(sb, sbi->nbperpage);
410 if ((mp = get_index_page(ip, 0)) == 0) {
411 jfs_err("add_index: get_metapage failed!");
412 xtTruncate(tid, ip, 0, COMMIT_PWMAP);
413 memcpy(&jfs_ip->i_dirtable, temp_table,
414 sizeof (temp_table));
417 tlck = txLock(tid, ip, mp, tlckDATA);
418 llck = (struct linelock *) & tlck->lock;
419 ASSERT(llck->index == 0);
423 lv->length = 6; /* tlckDATA slot size is 16 bytes */
426 memcpy(mp->data, temp_table, sizeof(temp_table));
428 mark_metapage_dirty(mp);
429 release_metapage(mp);
432 * Logging is now directed by xtree tlocks
434 clear_cflag(COMMIT_Dirtable, ip);
437 offset = (index - 2) * sizeof(struct dir_table_slot);
438 page_offset = offset & (PSIZE - 1);
439 blkno = ((offset + 1) >> L2PSIZE) << sbi->l2nbperpage;
440 if (page_offset == 0) {
442 * This will be the beginning of a new page
445 if (xtInsert(tid, ip, 0, blkno, sbi->nbperpage, &xaddr, 0)) {
446 jfs_warn("add_index: xtInsert failed!");
450 ip->i_blocks += LBLK2PBLK(sb, sbi->nbperpage);
452 if ((mp = get_index_page(ip, blkno)))
453 memset(mp->data, 0, PSIZE); /* Just looks better */
455 xtTruncate(tid, ip, offset, COMMIT_PWMAP);
457 mp = read_index_page(ip, blkno);
460 jfs_err("add_index: get/read_metapage failed!");
464 lock_index(tid, ip, mp, index);
467 (struct dir_table_slot *) ((char *) mp->data + page_offset);
468 dirtab_slot->flag = DIR_INDEX_VALID;
469 dirtab_slot->slot = slot;
470 DTSaddress(dirtab_slot, bn);
472 mark_metapage_dirty(mp);
473 release_metapage(mp);
479 jfs_ip->next_index--;
487 * Marks an entry to the directory index table as free.
489 static void free_index(tid_t tid, struct inode *ip, u32 index, u32 next)
491 struct dir_table_slot *dirtab_slot;
493 struct metapage *mp = NULL;
495 dirtab_slot = find_index(ip, index, &mp, &lblock);
497 if (dirtab_slot == 0)
500 dirtab_slot->flag = DIR_INDEX_FREE;
501 dirtab_slot->slot = dirtab_slot->addr1 = 0;
502 dirtab_slot->addr2 = cpu_to_le32(next);
505 lock_index(tid, ip, mp, index);
506 mark_metapage_dirty(mp);
507 release_metapage(mp);
509 set_cflag(COMMIT_Dirtable, ip);
515 * Changes an entry in the directory index table
517 static void modify_index(tid_t tid, struct inode *ip, u32 index, s64 bn,
518 int slot, struct metapage ** mp, u64 *lblock)
520 struct dir_table_slot *dirtab_slot;
522 dirtab_slot = find_index(ip, index, mp, lblock);
524 if (dirtab_slot == 0)
527 DTSaddress(dirtab_slot, bn);
528 dirtab_slot->slot = slot;
531 lock_index(tid, ip, *mp, index);
532 mark_metapage_dirty(*mp);
534 set_cflag(COMMIT_Dirtable, ip);
540 * reads a directory table slot
542 static int read_index(struct inode *ip, u32 index,
543 struct dir_table_slot * dirtab_slot)
546 struct metapage *mp = NULL;
547 struct dir_table_slot *slot;
549 slot = find_index(ip, index, &mp, &lblock);
554 memcpy(dirtab_slot, slot, sizeof(struct dir_table_slot));
557 release_metapage(mp);
566 * Search for the entry with specified key
570 * return: 0 - search result on stack, leaf page pinned;
573 int dtSearch(struct inode *ip, struct component_name * key, ino_t * data,
574 struct btstack * btstack, int flag)
577 int cmp = 1; /* init for empty page */
582 int base, index, lim;
583 struct btframe *btsp;
585 int psize = 288; /* initial in-line directory */
587 struct component_name ciKey;
588 struct super_block *sb = ip->i_sb;
591 (wchar_t *) kmalloc((JFS_NAME_MAX + 1) * sizeof(wchar_t),
593 if (ciKey.name == 0) {
599 /* uppercase search key for c-i directory */
600 UniStrcpy(ciKey.name, key->name);
601 ciKey.namlen = key->namlen;
603 /* only uppercase if case-insensitive support is on */
604 if ((JFS_SBI(sb)->mntflag & JFS_OS2) == JFS_OS2) {
607 BT_CLR(btstack); /* reset stack */
609 /* init level count for max pages to split */
613 * search down tree from root:
615 * between two consecutive entries of <Ki, Pi> and <Kj, Pj> of
616 * internal page, child page Pi contains entry with k, Ki <= K < Kj.
618 * if entry with search key K is not found
619 * internal page search find the entry with largest key Ki
620 * less than K which point to the child page to search;
621 * leaf page search find the entry with smallest key Kj
622 * greater than K so that the returned index is the position of
623 * the entry to be shifted right for insertion of new entry.
624 * for empty tree, search key is greater than any key of the tree.
626 * by convention, root bn = 0.
629 /* get/pin the page to search */
630 DT_GETPAGE(ip, bn, mp, psize, p, rc);
634 /* get sorted entry table of the page */
635 stbl = DT_GETSTBL(p);
638 * binary search with search key K on the current page.
640 for (base = 0, lim = p->header.nextindex; lim; lim >>= 1) {
641 index = base + (lim >> 1);
643 if (p->header.flag & BT_LEAF) {
644 /* uppercase leaf name to compare */
646 ciCompare(&ciKey, p, stbl[index],
647 JFS_SBI(sb)->mntflag);
649 /* router key is in uppercase */
651 cmp = dtCompare(&ciKey, p, stbl[index]);
659 /* search hit - leaf page:
660 * return the entry found
662 if (p->header.flag & BT_LEAF) {
663 inumber = le32_to_cpu(
664 ((struct ldtentry *) & p->slot[stbl[index]])->inumber);
667 * search for JFS_LOOKUP
669 if (flag == JFS_LOOKUP) {
676 * search for JFS_CREATE
678 if (flag == JFS_CREATE) {
685 * search for JFS_REMOVE or JFS_RENAME
687 if ((flag == JFS_REMOVE ||
688 flag == JFS_RENAME) &&
695 * JFS_REMOVE|JFS_FINDDIR|JFS_RENAME
697 /* save search result */
708 /* search hit - internal page:
709 * descend/search its child page
723 * base is the smallest index with key (Kj) greater than
724 * search key (K) and may be zero or (maxindex + 1) index.
727 * search miss - leaf page
729 * return location of entry (base) where new entry with
730 * search key K is to be inserted.
732 if (p->header.flag & BT_LEAF) {
734 * search for JFS_LOOKUP, JFS_REMOVE, or JFS_RENAME
736 if (flag == JFS_LOOKUP || flag == JFS_REMOVE ||
737 flag == JFS_RENAME) {
743 * search for JFS_CREATE|JFS_FINDDIR:
758 * search miss - internal page
760 * if base is non-zero, decrement base by one to get the parent
761 * entry of the child page to search.
763 index = base ? base - 1 : base;
766 * go down to child page
769 /* update max. number of pages to split */
770 if (BT_STACK_FULL(btstack)) {
771 /* Something's corrupted, mark filesytem dirty so
772 * chkdsk will fix it.
774 jfs_error(sb, "stack overrun in dtSearch!");
775 BT_STACK_DUMP(btstack);
781 /* push (bn, index) of the parent page/entry */
782 BT_PUSH(btstack, bn, index);
784 /* get the child page block number */
785 pxd = (pxd_t *) & p->slot[stbl[index]];
786 bn = addressPXD(pxd);
787 psize = lengthPXD(pxd) << JFS_SBI(ip->i_sb)->l2bsize;
789 /* unpin the parent page */
809 * function: insert an entry to directory tree
813 * return: 0 - success;
816 int dtInsert(tid_t tid, struct inode *ip,
817 struct component_name * name, ino_t * fsn, struct btstack * btstack)
820 struct metapage *mp; /* meta-page buffer */
821 dtpage_t *p; /* base B+-tree index page */
824 struct dtsplit split; /* split information */
826 struct dt_lock *dtlck;
832 * retrieve search result
834 * dtSearch() returns (leaf page pinned, index at which to insert).
835 * n.b. dtSearch() may return index of (maxindex + 1) of
838 DT_GETSEARCH(ip, btstack->top, bn, mp, p, index);
841 * insert entry for new key
844 if (JFS_IP(ip)->next_index == DIREND) {
848 n = NDTLEAF(name->namlen);
852 n = NDTLEAF_LEGACY(name->namlen);
853 data.leaf.ip = NULL; /* signifies legacy directory format */
855 data.leaf.ino = cpu_to_le32(*fsn);
858 * leaf page does not have enough room for new entry:
860 * extend/split the leaf page;
862 * dtSplitUp() will insert the entry and unpin the leaf page.
864 if (n > p->header.freecnt) {
870 rc = dtSplitUp(tid, ip, &split, btstack);
875 * leaf page does have enough room for new entry:
877 * insert the new data entry into the leaf page;
879 BT_MARK_DIRTY(mp, ip);
881 * acquire a transaction lock on the leaf page
883 tlck = txLock(tid, ip, mp, tlckDTREE | tlckENTRY);
884 dtlck = (struct dt_lock *) & tlck->lock;
885 ASSERT(dtlck->index == 0);
888 /* linelock header */
893 dtInsertEntry(p, index, name, &data, &dtlck);
895 /* linelock stbl of non-root leaf page */
896 if (!(p->header.flag & BT_ROOT)) {
897 if (dtlck->index >= dtlck->maxcnt)
898 dtlck = (struct dt_lock *) txLinelock(dtlck);
899 lv = & dtlck->lv[dtlck->index];
900 n = index >> L2DTSLOTSIZE;
901 lv->offset = p->header.stblindex + n;
903 ((p->header.nextindex - 1) >> L2DTSLOTSIZE) - n + 1;
907 /* unpin the leaf page */
917 * function: propagate insertion bottom up;
921 * return: 0 - success;
923 * leaf page unpinned;
925 static int dtSplitUp(tid_t tid,
926 struct inode *ip, struct dtsplit * split, struct btstack * btstack)
928 struct jfs_sb_info *sbi = JFS_SBI(ip->i_sb);
930 struct metapage *smp;
931 dtpage_t *sp; /* split page */
932 struct metapage *rmp;
933 dtpage_t *rp; /* new right page split from sp */
934 pxd_t rpxd; /* new right page extent descriptor */
935 struct metapage *lmp;
936 dtpage_t *lp; /* left child page */
937 int skip; /* index of entry of insertion */
938 struct btframe *parent; /* parent page entry on traverse stack */
941 struct pxdlist pxdlist;
943 struct component_name key = { 0, NULL };
944 ddata_t *data = split->data;
946 struct dt_lock *dtlck;
952 sp = DT_PAGE(ip, smp);
955 (wchar_t *) kmalloc((JFS_NAME_MAX + 2) * sizeof(wchar_t),
966 * The split routines insert the new entry, and
967 * acquire txLock as appropriate.
970 * split root leaf page:
972 if (sp->header.flag & BT_ROOT) {
974 * allocate a single extent child page
977 n = sbi->bsize >> L2DTSLOTSIZE;
978 n -= (n + 31) >> L2DTSLOTSIZE; /* stbl size */
979 n -= DTROOTMAXSLOT - sp->header.freecnt; /* header + entries */
980 if (n <= split->nslot)
982 if ((rc = dbAlloc(ip, 0, (s64) xlen, &xaddr))) {
989 pxd = &pxdlist.pxd[0];
990 PXDaddress(pxd, xaddr);
991 PXDlength(pxd, xlen);
992 split->pxdlist = &pxdlist;
993 rc = dtSplitRoot(tid, ip, split, &rmp);
1004 * extend first leaf page
1006 * extend the 1st extent if less than buffer page size
1007 * (dtExtendPage() reurns leaf page unpinned)
1009 pxd = &sp->header.self;
1010 xlen = lengthPXD(pxd);
1011 xsize = xlen << sbi->l2bsize;
1012 if (xsize < PSIZE) {
1013 xaddr = addressPXD(pxd);
1014 n = xsize >> L2DTSLOTSIZE;
1015 n -= (n + 31) >> L2DTSLOTSIZE; /* stbl size */
1016 if ((n + sp->header.freecnt) <= split->nslot)
1017 n = xlen + (xlen << 1);
1020 if ((rc = dbReAlloc(sbi->ipbmap, xaddr, (s64) xlen,
1024 pxdlist.maxnpxd = 1;
1026 pxd = &pxdlist.pxd[0];
1027 PXDaddress(pxd, nxaddr)
1028 PXDlength(pxd, xlen + n);
1029 split->pxdlist = &pxdlist;
1030 if ((rc = dtExtendPage(tid, ip, split, btstack))) {
1031 nxaddr = addressPXD(pxd);
1032 if (xaddr != nxaddr) {
1033 /* free relocated extent */
1034 xlen = lengthPXD(pxd);
1035 dbFree(ip, nxaddr, (s64) xlen);
1037 /* free extended delta */
1038 xlen = lengthPXD(pxd) - n;
1039 xaddr = addressPXD(pxd) + xlen;
1040 dbFree(ip, xaddr, (s64) n);
1050 * split leaf page <sp> into <sp> and a new right page <rp>.
1052 * return <rp> pinned and its extent descriptor <rpxd>
1055 * allocate new directory page extent and
1056 * new index page(s) to cover page split(s)
1058 * allocation hint: ?
1060 n = btstack->nsplit;
1061 pxdlist.maxnpxd = pxdlist.npxd = 0;
1062 xlen = sbi->nbperpage;
1063 for (pxd = pxdlist.pxd; n > 0; n--, pxd++) {
1064 if ((rc = dbAlloc(ip, 0, (s64) xlen, &xaddr)) == 0) {
1065 PXDaddress(pxd, xaddr);
1066 PXDlength(pxd, xlen);
1073 /* undo allocation */
1077 split->pxdlist = &pxdlist;
1078 if ((rc = dtSplitPage(tid, ip, split, &rmp, &rp, &rpxd))) {
1081 /* undo allocation */
1086 * propagate up the router entry for the leaf page just split
1088 * insert a router entry for the new page into the parent page,
1089 * propagate the insert/split up the tree by walking back the stack
1090 * of (bn of parent page, index of child page entry in parent page)
1091 * that were traversed during the search for the page that split.
1093 * the propagation of insert/split up the tree stops if the root
1094 * splits or the page inserted into doesn't have to split to hold
1097 * the parent entry for the split page remains the same, and
1098 * a new entry is inserted at its right with the first key and
1099 * block number of the new right page.
1101 * There are a maximum of 4 pages pinned at any time:
1102 * two children, left parent and right parent (when the parent splits).
1103 * keep the child pages pinned while working on the parent.
1104 * make sure that all pins are released at exit.
1106 while ((parent = BT_POP(btstack)) != NULL) {
1107 /* parent page specified by stack frame <parent> */
1109 /* keep current child pages (<lp>, <rp>) pinned */
1114 * insert router entry in parent for new right child page <rp>
1116 /* get the parent page <sp> */
1117 DT_GETPAGE(ip, parent->bn, smp, PSIZE, sp, rc);
1125 * The new key entry goes ONE AFTER the index of parent entry,
1126 * because the split was to the right.
1128 skip = parent->index + 1;
1131 * compute the key for the router entry
1133 * key suffix compression:
1134 * for internal pages that have leaf pages as children,
1135 * retain only what's needed to distinguish between
1136 * the new entry and the entry on the page to its left.
1137 * If the keys compare equal, retain the entire key.
1139 * note that compression is performed only at computing
1140 * router key at the lowest internal level.
1141 * further compression of the key between pairs of higher
1142 * level internal pages loses too much information and
1143 * the search may fail.
1144 * (e.g., two adjacent leaf pages of {a, ..., x} {xx, ...,}
1145 * results in two adjacent parent entries (a)(xx).
1146 * if split occurs between these two entries, and
1147 * if compression is applied, the router key of parent entry
1148 * of right page (x) will divert search for x into right
1149 * subtree and miss x in the left subtree.)
1151 * the entire key must be retained for the next-to-leftmost
1152 * internal key at any level of the tree, or search may fail
1155 switch (rp->header.flag & BT_TYPE) {
1158 * compute the length of prefix for suffix compression
1159 * between last entry of left page and first entry
1162 if ((sp->header.flag & BT_ROOT && skip > 1) ||
1163 sp->header.prev != 0 || skip > 1) {
1164 /* compute uppercase router prefix key */
1165 rc = ciGetLeafPrefixKey(lp,
1166 lp->header.nextindex-1,
1176 /* next to leftmost entry of
1177 lowest internal level */
1179 /* compute uppercase router key */
1180 dtGetKey(rp, 0, &key, sbi->mntflag);
1181 key.name[key.namlen] = 0;
1183 if ((sbi->mntflag & JFS_OS2) == JFS_OS2)
1187 n = NDTINTERNAL(key.namlen);
1191 dtGetKey(rp, 0, &key, sbi->mntflag);
1192 n = NDTINTERNAL(key.namlen);
1196 jfs_err("dtSplitUp(): UFO!");
1200 /* unpin left child page */
1204 * compute the data for the router entry
1206 data->xd = rpxd; /* child page xd */
1209 * parent page is full - split the parent page
1211 if (n > sp->header.freecnt) {
1212 /* init for parent page split */
1214 split->index = skip; /* index at insert */
1217 /* split->data = data; */
1219 /* unpin right child page */
1222 /* The split routines insert the new entry,
1223 * acquire txLock as appropriate.
1224 * return <rp> pinned and its block number <rbn>.
1226 rc = (sp->header.flag & BT_ROOT) ?
1227 dtSplitRoot(tid, ip, split, &rmp) :
1228 dtSplitPage(tid, ip, split, &rmp, &rp, &rpxd);
1234 /* smp and rmp are pinned */
1237 * parent page is not full - insert router entry in parent page
1240 BT_MARK_DIRTY(smp, ip);
1242 * acquire a transaction lock on the parent page
1244 tlck = txLock(tid, ip, smp, tlckDTREE | tlckENTRY);
1245 dtlck = (struct dt_lock *) & tlck->lock;
1246 ASSERT(dtlck->index == 0);
1247 lv = & dtlck->lv[0];
1249 /* linelock header */
1254 /* linelock stbl of non-root parent page */
1255 if (!(sp->header.flag & BT_ROOT)) {
1257 n = skip >> L2DTSLOTSIZE;
1258 lv->offset = sp->header.stblindex + n;
1260 ((sp->header.nextindex -
1261 1) >> L2DTSLOTSIZE) - n + 1;
1265 dtInsertEntry(sp, skip, &key, data, &dtlck);
1267 /* exit propagate up */
1272 /* unpin current split and its right page */
1277 * free remaining extents allocated for split
1281 pxd = &pxdlist.pxd[n];
1282 for (; n < pxdlist.maxnpxd; n++, pxd++)
1283 dbFree(ip, addressPXD(pxd), (s64) lengthPXD(pxd));
1297 * function: Split a non-root page of a btree.
1301 * return: 0 - success;
1303 * return split and new page pinned;
1305 static int dtSplitPage(tid_t tid, struct inode *ip, struct dtsplit * split,
1306 struct metapage ** rmpp, dtpage_t ** rpp, pxd_t * rpxdp)
1308 struct super_block *sb = ip->i_sb;
1310 struct metapage *smp;
1312 struct metapage *rmp;
1313 dtpage_t *rp; /* new right page allocated */
1314 s64 rbn; /* new right page block number */
1315 struct metapage *mp;
1318 struct pxdlist *pxdlist;
1320 int skip, nextindex, half, left, nxt, off, si;
1321 struct ldtentry *ldtentry;
1322 struct idtentry *idtentry;
1327 struct dt_lock *sdtlck, *rdtlck;
1329 struct dt_lock *dtlck;
1330 struct lv *slv, *rlv, *lv;
1332 /* get split page */
1334 sp = DT_PAGE(ip, smp);
1337 * allocate the new right page for the split
1339 pxdlist = split->pxdlist;
1340 pxd = &pxdlist->pxd[pxdlist->npxd];
1342 rbn = addressPXD(pxd);
1343 rmp = get_metapage(ip, rbn, PSIZE, 1);
1347 jfs_info("dtSplitPage: ip:0x%p smp:0x%p rmp:0x%p", ip, smp, rmp);
1349 BT_MARK_DIRTY(rmp, ip);
1351 * acquire a transaction lock on the new right page
1353 tlck = txLock(tid, ip, rmp, tlckDTREE | tlckNEW);
1354 rdtlck = (struct dt_lock *) & tlck->lock;
1356 rp = (dtpage_t *) rmp->data;
1358 rp->header.self = *pxd;
1360 BT_MARK_DIRTY(smp, ip);
1362 * acquire a transaction lock on the split page
1366 tlck = txLock(tid, ip, smp, tlckDTREE | tlckENTRY);
1367 sdtlck = (struct dt_lock *) & tlck->lock;
1369 /* linelock header of split page */
1370 ASSERT(sdtlck->index == 0);
1371 slv = & sdtlck->lv[0];
1377 * initialize/update sibling pointers between sp and rp
1379 nextbn = le64_to_cpu(sp->header.next);
1380 rp->header.next = cpu_to_le64(nextbn);
1381 rp->header.prev = cpu_to_le64(addressPXD(&sp->header.self));
1382 sp->header.next = cpu_to_le64(rbn);
1385 * initialize new right page
1387 rp->header.flag = sp->header.flag;
1389 /* compute sorted entry table at start of extent data area */
1390 rp->header.nextindex = 0;
1391 rp->header.stblindex = 1;
1393 n = PSIZE >> L2DTSLOTSIZE;
1394 rp->header.maxslot = n;
1395 stblsize = (n + 31) >> L2DTSLOTSIZE; /* in unit of slot */
1398 fsi = rp->header.stblindex + stblsize;
1399 rp->header.freelist = fsi;
1400 rp->header.freecnt = rp->header.maxslot - fsi;
1403 * sequential append at tail: append without split
1405 * If splitting the last page on a level because of appending
1406 * a entry to it (skip is maxentry), it's likely that the access is
1407 * sequential. Adding an empty page on the side of the level is less
1408 * work and can push the fill factor much higher than normal.
1409 * If we're wrong it's no big deal, we'll just do the split the right
1411 * (It may look like it's equally easy to do a similar hack for
1412 * reverse sorted data, that is, split the tree left,
1413 * but it's not. Be my guest.)
1415 if (nextbn == 0 && split->index == sp->header.nextindex) {
1416 /* linelock header + stbl (first slot) of new page */
1417 rlv = & rdtlck->lv[rdtlck->index];
1423 * initialize freelist of new right page
1426 for (fsi++; fsi < rp->header.maxslot; f++, fsi++)
1430 /* insert entry at the first entry of the new right page */
1431 dtInsertEntry(rp, 0, split->key, split->data, &rdtlck);
1437 * non-sequential insert (at possibly middle page)
1441 * update prev pointer of previous right sibling page;
1444 DT_GETPAGE(ip, nextbn, mp, PSIZE, p, rc);
1446 discard_metapage(rmp);
1450 BT_MARK_DIRTY(mp, ip);
1452 * acquire a transaction lock on the next page
1454 tlck = txLock(tid, ip, mp, tlckDTREE | tlckRELINK);
1455 jfs_info("dtSplitPage: tlck = 0x%p, ip = 0x%p, mp=0x%p",
1457 dtlck = (struct dt_lock *) & tlck->lock;
1459 /* linelock header of previous right sibling page */
1460 lv = & dtlck->lv[dtlck->index];
1465 p->header.prev = cpu_to_le64(rbn);
1471 * split the data between the split and right pages.
1473 skip = split->index;
1474 half = (PSIZE >> L2DTSLOTSIZE) >> 1; /* swag */
1478 * compute fill factor for split pages
1480 * <nxt> traces the next entry to move to rp
1481 * <off> traces the next entry to stay in sp
1483 stbl = (u8 *) & sp->slot[sp->header.stblindex];
1484 nextindex = sp->header.nextindex;
1485 for (nxt = off = 0; nxt < nextindex; ++off) {
1487 /* check for fill factor with new entry size */
1491 switch (sp->header.flag & BT_TYPE) {
1493 ldtentry = (struct ldtentry *) & sp->slot[si];
1495 n = NDTLEAF(ldtentry->namlen);
1497 n = NDTLEAF_LEGACY(ldtentry->
1502 idtentry = (struct idtentry *) & sp->slot[si];
1503 n = NDTINTERNAL(idtentry->namlen);
1510 ++nxt; /* advance to next entry to move in sp */
1518 /* <nxt> poins to the 1st entry to move */
1521 * move entries to right page
1523 * dtMoveEntry() initializes rp and reserves entry for insertion
1525 * split page moved out entries are linelocked;
1526 * new/right page moved in entries are linelocked;
1528 /* linelock header + stbl of new right page */
1529 rlv = & rdtlck->lv[rdtlck->index];
1534 dtMoveEntry(sp, nxt, rp, &sdtlck, &rdtlck, DO_INDEX(ip));
1536 sp->header.nextindex = nxt;
1539 * finalize freelist of new right page
1541 fsi = rp->header.freelist;
1543 for (fsi++; fsi < rp->header.maxslot; f++, fsi++)
1548 * Update directory index table for entries now in right page
1550 if ((rp->header.flag & BT_LEAF) && DO_INDEX(ip)) {
1554 stbl = DT_GETSTBL(rp);
1555 for (n = 0; n < rp->header.nextindex; n++) {
1556 ldtentry = (struct ldtentry *) & rp->slot[stbl[n]];
1557 modify_index(tid, ip, le32_to_cpu(ldtentry->index),
1558 rbn, n, &mp, &lblock);
1561 release_metapage(mp);
1565 * the skipped index was on the left page,
1568 /* insert the new entry in the split page */
1569 dtInsertEntry(sp, skip, split->key, split->data, &sdtlck);
1571 /* linelock stbl of split page */
1572 if (sdtlck->index >= sdtlck->maxcnt)
1573 sdtlck = (struct dt_lock *) txLinelock(sdtlck);
1574 slv = & sdtlck->lv[sdtlck->index];
1575 n = skip >> L2DTSLOTSIZE;
1576 slv->offset = sp->header.stblindex + n;
1578 ((sp->header.nextindex - 1) >> L2DTSLOTSIZE) - n + 1;
1582 * the skipped index was on the right page,
1585 /* adjust the skip index to reflect the new position */
1588 /* insert the new entry in the right page */
1589 dtInsertEntry(rp, skip, split->key, split->data, &rdtlck);
1596 ip->i_blocks += LBLK2PBLK(sb, lengthPXD(pxd));
1605 * function: extend 1st/only directory leaf page
1609 * return: 0 - success;
1611 * return extended page pinned;
1613 static int dtExtendPage(tid_t tid,
1614 struct inode *ip, struct dtsplit * split, struct btstack * btstack)
1616 struct super_block *sb = ip->i_sb;
1618 struct metapage *smp, *pmp, *mp;
1620 struct pxdlist *pxdlist;
1623 int newstblindex, newstblsize;
1624 int oldstblindex, oldstblsize;
1627 struct btframe *parent;
1629 struct dt_lock *dtlck;
1632 struct pxd_lock *pxdlock;
1635 struct ldtentry *ldtentry;
1638 /* get page to extend */
1640 sp = DT_PAGE(ip, smp);
1642 /* get parent/root page */
1643 parent = BT_POP(btstack);
1644 DT_GETPAGE(ip, parent->bn, pmp, PSIZE, pp, rc);
1651 pxdlist = split->pxdlist;
1652 pxd = &pxdlist->pxd[pxdlist->npxd];
1655 xaddr = addressPXD(pxd);
1656 tpxd = &sp->header.self;
1657 txaddr = addressPXD(tpxd);
1658 /* in-place extension */
1659 if (xaddr == txaddr) {
1666 /* save moved extent descriptor for later free */
1667 tlck = txMaplock(tid, ip, tlckDTREE | tlckRELOCATE);
1668 pxdlock = (struct pxd_lock *) & tlck->lock;
1669 pxdlock->flag = mlckFREEPXD;
1670 pxdlock->pxd = sp->header.self;
1674 * Update directory index table to reflect new page address
1680 stbl = DT_GETSTBL(sp);
1681 for (n = 0; n < sp->header.nextindex; n++) {
1683 (struct ldtentry *) & sp->slot[stbl[n]];
1684 modify_index(tid, ip,
1685 le32_to_cpu(ldtentry->index),
1686 xaddr, n, &mp, &lblock);
1689 release_metapage(mp);
1696 sp->header.self = *pxd;
1698 jfs_info("dtExtendPage: ip:0x%p smp:0x%p sp:0x%p", ip, smp, sp);
1700 BT_MARK_DIRTY(smp, ip);
1702 * acquire a transaction lock on the extended/leaf page
1704 tlck = txLock(tid, ip, smp, tlckDTREE | type);
1705 dtlck = (struct dt_lock *) & tlck->lock;
1706 lv = & dtlck->lv[0];
1708 /* update buffer extent descriptor of extended page */
1709 xlen = lengthPXD(pxd);
1710 xsize = xlen << JFS_SBI(sb)->l2bsize;
1711 #ifdef _STILL_TO_PORT
1712 bmSetXD(smp, xaddr, xsize);
1713 #endif /* _STILL_TO_PORT */
1716 * copy old stbl to new stbl at start of extended area
1718 oldstblindex = sp->header.stblindex;
1719 oldstblsize = (sp->header.maxslot + 31) >> L2DTSLOTSIZE;
1720 newstblindex = sp->header.maxslot;
1721 n = xsize >> L2DTSLOTSIZE;
1722 newstblsize = (n + 31) >> L2DTSLOTSIZE;
1723 memcpy(&sp->slot[newstblindex], &sp->slot[oldstblindex],
1724 sp->header.nextindex);
1727 * in-line extension: linelock old area of extended page
1729 if (type == tlckEXTEND) {
1730 /* linelock header */
1736 /* linelock new stbl of extended page */
1737 lv->offset = newstblindex;
1738 lv->length = newstblsize;
1741 * relocation: linelock whole relocated area
1745 lv->length = sp->header.maxslot + newstblsize;
1750 sp->header.maxslot = n;
1751 sp->header.stblindex = newstblindex;
1752 /* sp->header.nextindex remains the same */
1755 * add old stbl region at head of freelist
1759 last = sp->header.freelist;
1760 for (n = 0; n < oldstblsize; n++, fsi++, f++) {
1764 sp->header.freelist = last;
1765 sp->header.freecnt += oldstblsize;
1768 * append free region of newly extended area at tail of freelist
1770 /* init free region of newly extended area */
1771 fsi = n = newstblindex + newstblsize;
1773 for (fsi++; fsi < sp->header.maxslot; f++, fsi++)
1777 /* append new free region at tail of old freelist */
1778 fsi = sp->header.freelist;
1780 sp->header.freelist = n;
1785 } while (fsi != -1);
1790 sp->header.freecnt += sp->header.maxslot - n;
1793 * insert the new entry
1795 dtInsertEntry(sp, split->index, split->key, split->data, &dtlck);
1797 BT_MARK_DIRTY(pmp, ip);
1799 * linelock any freeslots residing in old extent
1801 if (type == tlckEXTEND) {
1802 n = sp->header.maxslot >> 2;
1803 if (sp->header.freelist < n)
1804 dtLinelockFreelist(sp, n, &dtlck);
1808 * update parent entry on the parent/root page
1811 * acquire a transaction lock on the parent/root page
1813 tlck = txLock(tid, ip, pmp, tlckDTREE | tlckENTRY);
1814 dtlck = (struct dt_lock *) & tlck->lock;
1815 lv = & dtlck->lv[dtlck->index];
1817 /* linelock parent entry - 1st slot */
1822 /* update the parent pxd for page extension */
1823 tpxd = (pxd_t *) & pp->slot[1];
1826 /* Since the directory might have an EA and/or ACL associated with it
1827 * we need to make sure we take that into account when setting the
1830 ip->i_blocks = LBLK2PBLK(ip->i_sb, xlen +
1831 ((JFS_IP(ip)->ea.flag & DXD_EXTENT) ?
1832 lengthDXD(&JFS_IP(ip)->ea) : 0) +
1833 ((JFS_IP(ip)->acl.flag & DXD_EXTENT) ?
1834 lengthDXD(&JFS_IP(ip)->acl) : 0));
1845 * split the full root page into
1846 * original/root/split page and new right page
1847 * i.e., root remains fixed in tree anchor (inode) and
1848 * the root is copied to a single new right child page
1849 * since root page << non-root page, and
1850 * the split root page contains a single entry for the
1851 * new right child page.
1855 * return: 0 - success;
1857 * return new page pinned;
1859 static int dtSplitRoot(tid_t tid,
1860 struct inode *ip, struct dtsplit * split, struct metapage ** rmpp)
1862 struct super_block *sb = ip->i_sb;
1863 struct metapage *smp;
1865 struct metapage *rmp;
1872 int fsi, stblsize, n;
1875 struct pxdlist *pxdlist;
1877 struct dt_lock *dtlck;
1881 /* get split root page */
1883 sp = &JFS_IP(ip)->i_dtroot;
1886 * allocate/initialize a single (right) child page
1888 * N.B. at first split, a one (or two) block to fit new entry
1889 * is allocated; at subsequent split, a full page is allocated;
1891 pxdlist = split->pxdlist;
1892 pxd = &pxdlist->pxd[pxdlist->npxd];
1894 rbn = addressPXD(pxd);
1895 xlen = lengthPXD(pxd);
1896 xsize = xlen << JFS_SBI(sb)->l2bsize;
1897 rmp = get_metapage(ip, rbn, xsize, 1);
1903 BT_MARK_DIRTY(rmp, ip);
1905 * acquire a transaction lock on the new right page
1907 tlck = txLock(tid, ip, rmp, tlckDTREE | tlckNEW);
1908 dtlck = (struct dt_lock *) & tlck->lock;
1911 (sp->header.flag & BT_LEAF) ? BT_LEAF : BT_INTERNAL;
1912 rp->header.self = *pxd;
1914 /* initialize sibling pointers */
1915 rp->header.next = 0;
1916 rp->header.prev = 0;
1919 * move in-line root page into new right page extent
1921 /* linelock header + copied entries + new stbl (1st slot) in new page */
1922 ASSERT(dtlck->index == 0);
1923 lv = & dtlck->lv[0];
1925 lv->length = 10; /* 1 + 8 + 1 */
1928 n = xsize >> L2DTSLOTSIZE;
1929 rp->header.maxslot = n;
1930 stblsize = (n + 31) >> L2DTSLOTSIZE;
1932 /* copy old stbl to new stbl at start of extended area */
1933 rp->header.stblindex = DTROOTMAXSLOT;
1934 stbl = (s8 *) & rp->slot[DTROOTMAXSLOT];
1935 memcpy(stbl, sp->header.stbl, sp->header.nextindex);
1936 rp->header.nextindex = sp->header.nextindex;
1938 /* copy old data area to start of new data area */
1939 memcpy(&rp->slot[1], &sp->slot[1], IDATASIZE);
1942 * append free region of newly extended area at tail of freelist
1944 /* init free region of newly extended area */
1945 fsi = n = DTROOTMAXSLOT + stblsize;
1947 for (fsi++; fsi < rp->header.maxslot; f++, fsi++)
1951 /* append new free region at tail of old freelist */
1952 fsi = sp->header.freelist;
1954 rp->header.freelist = n;
1956 rp->header.freelist = fsi;
1961 } while (fsi != -1);
1966 rp->header.freecnt = sp->header.freecnt + rp->header.maxslot - n;
1969 * Update directory index table for entries now in right page
1971 if ((rp->header.flag & BT_LEAF) && DO_INDEX(ip)) {
1973 struct metapage *mp = NULL;
1974 struct ldtentry *ldtentry;
1976 stbl = DT_GETSTBL(rp);
1977 for (n = 0; n < rp->header.nextindex; n++) {
1978 ldtentry = (struct ldtentry *) & rp->slot[stbl[n]];
1979 modify_index(tid, ip, le32_to_cpu(ldtentry->index),
1980 rbn, n, &mp, &lblock);
1983 release_metapage(mp);
1986 * insert the new entry into the new right/child page
1987 * (skip index in the new right page will not change)
1989 dtInsertEntry(rp, split->index, split->key, split->data, &dtlck);
1992 * reset parent/root page
1994 * set the 1st entry offset to 0, which force the left-most key
1995 * at any level of the tree to be less than any search key.
1997 * The btree comparison code guarantees that the left-most key on any
1998 * level of the tree is never used, so it doesn't need to be filled in.
2000 BT_MARK_DIRTY(smp, ip);
2002 * acquire a transaction lock on the root page (in-memory inode)
2004 tlck = txLock(tid, ip, smp, tlckDTREE | tlckNEW | tlckBTROOT);
2005 dtlck = (struct dt_lock *) & tlck->lock;
2008 ASSERT(dtlck->index == 0);
2009 lv = & dtlck->lv[0];
2011 lv->length = DTROOTMAXSLOT;
2014 /* update page header of root */
2015 if (sp->header.flag & BT_LEAF) {
2016 sp->header.flag &= ~BT_LEAF;
2017 sp->header.flag |= BT_INTERNAL;
2020 /* init the first entry */
2021 s = (struct idtentry *) & sp->slot[DTENTRYSTART];
2027 stbl = sp->header.stbl;
2028 stbl[0] = DTENTRYSTART;
2029 sp->header.nextindex = 1;
2032 fsi = DTENTRYSTART + 1;
2035 /* init free region of remaining area */
2036 for (fsi++; fsi < DTROOTMAXSLOT; f++, fsi++)
2040 sp->header.freelist = DTENTRYSTART + 1;
2041 sp->header.freecnt = DTROOTMAXSLOT - (DTENTRYSTART + 1);
2045 ip->i_blocks += LBLK2PBLK(ip->i_sb, lengthPXD(pxd));
2053 * function: delete the entry(s) referenced by a key.
2059 int dtDelete(tid_t tid,
2060 struct inode *ip, struct component_name * key, ino_t * ino, int flag)
2064 struct metapage *mp, *imp;
2067 struct btstack btstack;
2068 struct dt_lock *dtlck;
2072 struct ldtentry *ldtentry;
2074 u32 table_index, next_index;
2075 struct metapage *nmp;
2079 * search for the entry to delete:
2081 * dtSearch() returns (leaf page pinned, index at which to delete).
2083 if ((rc = dtSearch(ip, key, ino, &btstack, flag)))
2086 /* retrieve search result */
2087 DT_GETSEARCH(ip, btstack.top, bn, mp, p, index);
2090 * We need to find put the index of the next entry into the
2091 * directory index table in order to resume a readdir from this
2095 stbl = DT_GETSTBL(p);
2096 ldtentry = (struct ldtentry *) & p->slot[stbl[index]];
2097 table_index = le32_to_cpu(ldtentry->index);
2098 if (index == (p->header.nextindex - 1)) {
2100 * Last entry in this leaf page
2102 if ((p->header.flag & BT_ROOT)
2103 || (p->header.next == 0))
2106 /* Read next leaf page */
2107 DT_GETPAGE(ip, le64_to_cpu(p->header.next),
2108 nmp, PSIZE, np, rc);
2112 stbl = DT_GETSTBL(np);
2114 (struct ldtentry *) & np->
2117 le32_to_cpu(ldtentry->index);
2123 (struct ldtentry *) & p->slot[stbl[index + 1]];
2124 next_index = le32_to_cpu(ldtentry->index);
2126 free_index(tid, ip, table_index, next_index);
2129 * the leaf page becomes empty, delete the page
2131 if (p->header.nextindex == 1) {
2132 /* delete empty page */
2133 rc = dtDeleteUp(tid, ip, mp, p, &btstack);
2136 * the leaf page has other entries remaining:
2138 * delete the entry from the leaf page.
2141 BT_MARK_DIRTY(mp, ip);
2143 * acquire a transaction lock on the leaf page
2145 tlck = txLock(tid, ip, mp, tlckDTREE | tlckENTRY);
2146 dtlck = (struct dt_lock *) & tlck->lock;
2149 * Do not assume that dtlck->index will be zero. During a
2150 * rename within a directory, this transaction may have
2151 * modified this page already when adding the new entry.
2154 /* linelock header */
2155 if (dtlck->index >= dtlck->maxcnt)
2156 dtlck = (struct dt_lock *) txLinelock(dtlck);
2157 lv = & dtlck->lv[dtlck->index];
2162 /* linelock stbl of non-root leaf page */
2163 if (!(p->header.flag & BT_ROOT)) {
2164 if (dtlck->index >= dtlck->maxcnt)
2165 dtlck = (struct dt_lock *) txLinelock(dtlck);
2166 lv = & dtlck->lv[dtlck->index];
2167 i = index >> L2DTSLOTSIZE;
2168 lv->offset = p->header.stblindex + i;
2170 ((p->header.nextindex - 1) >> L2DTSLOTSIZE) -
2175 /* free the leaf entry */
2176 dtDeleteEntry(p, index, &dtlck);
2179 * Update directory index table for entries moved in stbl
2181 if (DO_INDEX(ip) && index < p->header.nextindex) {
2185 stbl = DT_GETSTBL(p);
2186 for (i = index; i < p->header.nextindex; i++) {
2188 (struct ldtentry *) & p->slot[stbl[i]];
2189 modify_index(tid, ip,
2190 le32_to_cpu(ldtentry->index),
2191 bn, i, &imp, &lblock);
2194 release_metapage(imp);
2208 * free empty pages as propagating deletion up the tree
2214 static int dtDeleteUp(tid_t tid, struct inode *ip,
2215 struct metapage * fmp, dtpage_t * fp, struct btstack * btstack)
2218 struct metapage *mp;
2220 int index, nextindex;
2222 struct btframe *parent;
2223 struct dt_lock *dtlck;
2226 struct pxd_lock *pxdlock;
2230 * keep the root leaf page which has become empty
2232 if (BT_IS_ROOT(fmp)) {
2236 * dtInitRoot() acquires txlock on the root
2238 dtInitRoot(tid, ip, PARENT(ip));
2246 * free the non-root leaf page
2249 * acquire a transaction lock on the page
2251 * write FREEXTENT|NOREDOPAGE log record
2252 * N.B. linelock is overlaid as freed extent descriptor, and
2253 * the buffer page is freed;
2255 tlck = txMaplock(tid, ip, tlckDTREE | tlckFREE);
2256 pxdlock = (struct pxd_lock *) & tlck->lock;
2257 pxdlock->flag = mlckFREEPXD;
2258 pxdlock->pxd = fp->header.self;
2261 /* update sibling pointers */
2262 if ((rc = dtRelink(tid, ip, fp))) {
2267 xlen = lengthPXD(&fp->header.self);
2268 ip->i_blocks -= LBLK2PBLK(ip->i_sb, xlen);
2270 /* free/invalidate its buffer page */
2271 discard_metapage(fmp);
2274 * propagate page deletion up the directory tree
2276 * If the delete from the parent page makes it empty,
2277 * continue all the way up the tree.
2278 * stop if the root page is reached (which is never deleted) or
2279 * if the entry deletion does not empty the page.
2281 while ((parent = BT_POP(btstack)) != NULL) {
2282 /* pin the parent page <sp> */
2283 DT_GETPAGE(ip, parent->bn, mp, PSIZE, p, rc);
2288 * free the extent of the child page deleted
2290 index = parent->index;
2293 * delete the entry for the child page from parent
2295 nextindex = p->header.nextindex;
2298 * the parent has the single entry being deleted:
2300 * free the parent page which has become empty.
2302 if (nextindex == 1) {
2304 * keep the root internal page which has become empty
2306 if (p->header.flag & BT_ROOT) {
2310 * dtInitRoot() acquires txlock on the root
2312 dtInitRoot(tid, ip, PARENT(ip));
2319 * free the parent page
2323 * acquire a transaction lock on the page
2325 * write FREEXTENT|NOREDOPAGE log record
2329 tlckDTREE | tlckFREE);
2330 pxdlock = (struct pxd_lock *) & tlck->lock;
2331 pxdlock->flag = mlckFREEPXD;
2332 pxdlock->pxd = p->header.self;
2335 /* update sibling pointers */
2336 if ((rc = dtRelink(tid, ip, p))) {
2341 xlen = lengthPXD(&p->header.self);
2342 ip->i_blocks -= LBLK2PBLK(ip->i_sb, xlen);
2344 /* free/invalidate its buffer page */
2345 discard_metapage(mp);
2353 * the parent has other entries remaining:
2355 * delete the router entry from the parent page.
2357 BT_MARK_DIRTY(mp, ip);
2359 * acquire a transaction lock on the page
2361 * action: router entry deletion
2363 tlck = txLock(tid, ip, mp, tlckDTREE | tlckENTRY);
2364 dtlck = (struct dt_lock *) & tlck->lock;
2366 /* linelock header */
2367 if (dtlck->index >= dtlck->maxcnt)
2368 dtlck = (struct dt_lock *) txLinelock(dtlck);
2369 lv = & dtlck->lv[dtlck->index];
2374 /* linelock stbl of non-root leaf page */
2375 if (!(p->header.flag & BT_ROOT)) {
2376 if (dtlck->index < dtlck->maxcnt)
2379 dtlck = (struct dt_lock *) txLinelock(dtlck);
2380 lv = & dtlck->lv[0];
2382 i = index >> L2DTSLOTSIZE;
2383 lv->offset = p->header.stblindex + i;
2385 ((p->header.nextindex - 1) >> L2DTSLOTSIZE) -
2390 /* free the router entry */
2391 dtDeleteEntry(p, index, &dtlck);
2393 /* reset key of new leftmost entry of level (for consistency) */
2395 ((p->header.flag & BT_ROOT) || p->header.prev == 0))
2396 dtTruncateEntry(p, 0, &dtlck);
2398 /* unpin the parent page */
2401 /* exit propagation up */
2410 * NAME: dtRelocate()
2412 * FUNCTION: relocate dtpage (internal or leaf) of directory;
2413 * This function is mainly used by defragfs utility.
2415 int dtRelocate(tid_t tid, struct inode *ip, s64 lmxaddr, pxd_t * opxd,
2419 struct metapage *mp, *pmp, *lmp, *rmp;
2420 dtpage_t *p, *pp, *rp = 0, *lp= 0;
2423 struct btstack btstack;
2425 s64 oxaddr, nextbn, prevbn;
2428 struct dt_lock *dtlck;
2429 struct pxd_lock *pxdlock;
2433 oxaddr = addressPXD(opxd);
2434 xlen = lengthPXD(opxd);
2436 jfs_info("dtRelocate: lmxaddr:%Ld xaddr:%Ld:%Ld xlen:%d",
2437 (long long)lmxaddr, (long long)oxaddr, (long long)nxaddr,
2441 * 1. get the internal parent dtpage covering
2442 * router entry for the tartget page to be relocated;
2444 rc = dtSearchNode(ip, lmxaddr, opxd, &btstack);
2448 /* retrieve search result */
2449 DT_GETSEARCH(ip, btstack.top, bn, pmp, pp, index);
2450 jfs_info("dtRelocate: parent router entry validated.");
2453 * 2. relocate the target dtpage
2455 /* read in the target page from src extent */
2456 DT_GETPAGE(ip, oxaddr, mp, PSIZE, p, rc);
2458 /* release the pinned parent page */
2464 * read in sibling pages if any to update sibling pointers;
2467 if (p->header.next) {
2468 nextbn = le64_to_cpu(p->header.next);
2469 DT_GETPAGE(ip, nextbn, rmp, PSIZE, rp, rc);
2478 if (p->header.prev) {
2479 prevbn = le64_to_cpu(p->header.prev);
2480 DT_GETPAGE(ip, prevbn, lmp, PSIZE, lp, rc);
2490 /* at this point, all xtpages to be updated are in memory */
2493 * update sibling pointers of sibling dtpages if any;
2496 tlck = txLock(tid, ip, lmp, tlckDTREE | tlckRELINK);
2497 dtlck = (struct dt_lock *) & tlck->lock;
2498 /* linelock header */
2499 ASSERT(dtlck->index == 0);
2500 lv = & dtlck->lv[0];
2505 lp->header.next = cpu_to_le64(nxaddr);
2510 tlck = txLock(tid, ip, rmp, tlckDTREE | tlckRELINK);
2511 dtlck = (struct dt_lock *) & tlck->lock;
2512 /* linelock header */
2513 ASSERT(dtlck->index == 0);
2514 lv = & dtlck->lv[0];
2519 rp->header.prev = cpu_to_le64(nxaddr);
2524 * update the target dtpage to be relocated
2526 * write LOG_REDOPAGE of LOG_NEW type for dst page
2527 * for the whole target page (logredo() will apply
2528 * after image and update bmap for allocation of the
2529 * dst extent), and update bmap for allocation of
2532 tlck = txLock(tid, ip, mp, tlckDTREE | tlckNEW);
2533 dtlck = (struct dt_lock *) & tlck->lock;
2534 /* linelock header */
2535 ASSERT(dtlck->index == 0);
2536 lv = & dtlck->lv[0];
2538 /* update the self address in the dtpage header */
2539 pxd = &p->header.self;
2540 PXDaddress(pxd, nxaddr);
2542 /* the dst page is the same as the src page, i.e.,
2543 * linelock for afterimage of the whole page;
2546 lv->length = p->header.maxslot;
2549 /* update the buffer extent descriptor of the dtpage */
2550 xsize = xlen << JFS_SBI(ip->i_sb)->l2bsize;
2551 #ifdef _STILL_TO_PORT
2552 bmSetXD(mp, nxaddr, xsize);
2553 #endif /* _STILL_TO_PORT */
2554 /* unpin the relocated page */
2556 jfs_info("dtRelocate: target dtpage relocated.");
2558 /* the moved extent is dtpage, then a LOG_NOREDOPAGE log rec
2559 * needs to be written (in logredo(), the LOG_NOREDOPAGE log rec
2560 * will also force a bmap update ).
2564 * 3. acquire maplock for the source extent to be freed;
2566 /* for dtpage relocation, write a LOG_NOREDOPAGE record
2567 * for the source dtpage (logredo() will init NoRedoPage
2568 * filter and will also update bmap for free of the source
2569 * dtpage), and upadte bmap for free of the source dtpage;
2571 tlck = txMaplock(tid, ip, tlckDTREE | tlckFREE);
2572 pxdlock = (struct pxd_lock *) & tlck->lock;
2573 pxdlock->flag = mlckFREEPXD;
2574 PXDaddress(&pxdlock->pxd, oxaddr);
2575 PXDlength(&pxdlock->pxd, xlen);
2579 * 4. update the parent router entry for relocation;
2581 * acquire tlck for the parent entry covering the target dtpage;
2582 * write LOG_REDOPAGE to apply after image only;
2584 jfs_info("dtRelocate: update parent router entry.");
2585 tlck = txLock(tid, ip, pmp, tlckDTREE | tlckENTRY);
2586 dtlck = (struct dt_lock *) & tlck->lock;
2587 lv = & dtlck->lv[dtlck->index];
2589 /* update the PXD with the new address */
2590 stbl = DT_GETSTBL(pp);
2591 pxd = (pxd_t *) & pp->slot[stbl[index]];
2592 PXDaddress(pxd, nxaddr);
2593 lv->offset = stbl[index];
2597 /* unpin the parent dtpage */
2604 * NAME: dtSearchNode()
2606 * FUNCTION: Search for an dtpage containing a specified address
2607 * This function is mainly used by defragfs utility.
2609 * NOTE: Search result on stack, the found page is pinned at exit.
2610 * The result page must be an internal dtpage.
2611 * lmxaddr give the address of the left most page of the
2612 * dtree level, in which the required dtpage resides.
2614 static int dtSearchNode(struct inode *ip, s64 lmxaddr, pxd_t * kpxd,
2615 struct btstack * btstack)
2619 struct metapage *mp;
2621 int psize = 288; /* initial in-line directory */
2625 struct btframe *btsp;
2627 BT_CLR(btstack); /* reset stack */
2630 * descend tree to the level with specified leftmost page
2632 * by convention, root bn = 0.
2635 /* get/pin the page to search */
2636 DT_GETPAGE(ip, bn, mp, psize, p, rc);
2640 /* does the xaddr of leftmost page of the levevl
2641 * matches levevl search key ?
2643 if (p->header.flag & BT_ROOT) {
2646 } else if (addressPXD(&p->header.self) == lmxaddr)
2650 * descend down to leftmost child page
2652 if (p->header.flag & BT_LEAF) {
2657 /* get the leftmost entry */
2658 stbl = DT_GETSTBL(p);
2659 pxd = (pxd_t *) & p->slot[stbl[0]];
2661 /* get the child page block address */
2662 bn = addressPXD(pxd);
2663 psize = lengthPXD(pxd) << JFS_SBI(ip->i_sb)->l2bsize;
2664 /* unpin the parent page */
2669 * search each page at the current levevl
2672 stbl = DT_GETSTBL(p);
2673 for (i = 0; i < p->header.nextindex; i++) {
2674 pxd = (pxd_t *) & p->slot[stbl[i]];
2676 /* found the specified router entry */
2677 if (addressPXD(pxd) == addressPXD(kpxd) &&
2678 lengthPXD(pxd) == lengthPXD(kpxd)) {
2679 btsp = btstack->top;
2688 /* get the right sibling page if any */
2690 bn = le64_to_cpu(p->header.next);
2696 /* unpin current page */
2699 /* get the right sibling page */
2700 DT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
2706 #endif /* _NOTYET */
2712 * link around a freed page.
2715 * fp: page to be freed
2719 static int dtRelink(tid_t tid, struct inode *ip, dtpage_t * p)
2722 struct metapage *mp;
2725 struct dt_lock *dtlck;
2728 nextbn = le64_to_cpu(p->header.next);
2729 prevbn = le64_to_cpu(p->header.prev);
2731 /* update prev pointer of the next page */
2733 DT_GETPAGE(ip, nextbn, mp, PSIZE, p, rc);
2737 BT_MARK_DIRTY(mp, ip);
2739 * acquire a transaction lock on the next page
2741 * action: update prev pointer;
2743 tlck = txLock(tid, ip, mp, tlckDTREE | tlckRELINK);
2744 jfs_info("dtRelink nextbn: tlck = 0x%p, ip = 0x%p, mp=0x%p",
2746 dtlck = (struct dt_lock *) & tlck->lock;
2748 /* linelock header */
2749 if (dtlck->index >= dtlck->maxcnt)
2750 dtlck = (struct dt_lock *) txLinelock(dtlck);
2751 lv = & dtlck->lv[dtlck->index];
2756 p->header.prev = cpu_to_le64(prevbn);
2760 /* update next pointer of the previous page */
2762 DT_GETPAGE(ip, prevbn, mp, PSIZE, p, rc);
2766 BT_MARK_DIRTY(mp, ip);
2768 * acquire a transaction lock on the prev page
2770 * action: update next pointer;
2772 tlck = txLock(tid, ip, mp, tlckDTREE | tlckRELINK);
2773 jfs_info("dtRelink prevbn: tlck = 0x%p, ip = 0x%p, mp=0x%p",
2775 dtlck = (struct dt_lock *) & tlck->lock;
2777 /* linelock header */
2778 if (dtlck->index >= dtlck->maxcnt)
2779 dtlck = (struct dt_lock *) txLinelock(dtlck);
2780 lv = & dtlck->lv[dtlck->index];
2785 p->header.next = cpu_to_le64(nextbn);
2796 * initialize directory root (inline in inode)
2798 void dtInitRoot(tid_t tid, struct inode *ip, u32 idotdot)
2800 struct jfs_inode_info *jfs_ip = JFS_IP(ip);
2805 struct dt_lock *dtlck;
2810 * If this was previously an non-empty directory, we need to remove
2811 * the old directory table.
2814 if (jfs_ip->next_index > (MAX_INLINE_DIRTABLE_ENTRY + 1)) {
2815 struct tblock *tblk = tid_to_tblock(tid);
2817 * We're playing games with the tid's xflag. If
2818 * we're removing a regular file, the file's xtree
2819 * is committed with COMMIT_PMAP, but we always
2820 * commit the directories xtree with COMMIT_PWMAP.
2822 xflag_save = tblk->xflag;
2825 * xtTruncate isn't guaranteed to fully truncate
2826 * the xtree. The caller needs to check i_size
2827 * after committing the transaction to see if
2828 * additional truncation is needed. The
2829 * COMMIT_Stale flag tells caller that we
2830 * initiated the truncation.
2832 xtTruncate(tid, ip, 0, COMMIT_PWMAP);
2833 set_cflag(COMMIT_Stale, ip);
2835 tblk->xflag = xflag_save;
2839 jfs_ip->next_index = 2;
2841 ip->i_size = IDATASIZE;
2844 * acquire a transaction lock on the root
2846 * action: directory initialization;
2848 tlck = txLock(tid, ip, (struct metapage *) & jfs_ip->bxflag,
2849 tlckDTREE | tlckENTRY | tlckBTROOT);
2850 dtlck = (struct dt_lock *) & tlck->lock;
2853 ASSERT(dtlck->index == 0);
2854 lv = & dtlck->lv[0];
2856 lv->length = DTROOTMAXSLOT;
2859 p = &jfs_ip->i_dtroot;
2861 p->header.flag = DXD_INDEX | BT_ROOT | BT_LEAF;
2863 p->header.nextindex = 0;
2869 /* init data area of root */
2870 for (fsi++; fsi < DTROOTMAXSLOT; f++, fsi++)
2874 p->header.freelist = 1;
2875 p->header.freecnt = 8;
2877 /* init '..' entry */
2878 p->header.idotdot = cpu_to_le32(idotdot);
2881 ip->i_blocks = LBLK2PBLK(ip->i_sb,
2882 ((jfs_ip->ea.flag & DXD_EXTENT) ?
2883 lengthDXD(&jfs_ip->ea) : 0) +
2884 ((jfs_ip->acl.flag & DXD_EXTENT) ?
2885 lengthDXD(&jfs_ip->acl) : 0));
2892 * add_missing_indices()
2894 * function: Fix dtree page in which one or more entries has an invalid index.
2895 * fsck.jfs should really fix this, but it currently does not.
2896 * Called from jfs_readdir when bad index is detected.
2898 static void add_missing_indices(struct inode *inode, s64 bn)
2901 struct dt_lock *dtlck;
2905 struct metapage *mp;
2912 tid = txBegin(inode->i_sb, 0);
2914 DT_GETPAGE(inode, bn, mp, PSIZE, p, rc);
2917 printk(KERN_ERR "DT_GETPAGE failed!\n");
2920 BT_MARK_DIRTY(mp, inode);
2922 ASSERT(p->header.flag & BT_LEAF);
2924 tlck = txLock(tid, inode, mp, tlckDTREE | tlckENTRY);
2925 dtlck = (struct dt_lock *) &tlck->lock;
2927 stbl = DT_GETSTBL(p);
2928 for (i = 0; i < p->header.nextindex; i++) {
2929 d = (struct ldtentry *) &p->slot[stbl[i]];
2930 index = le32_to_cpu(d->index);
2931 if ((index < 2) || (index >= JFS_IP(inode)->next_index)) {
2932 d->index = cpu_to_le32(add_index(tid, inode, bn, i));
2933 if (dtlck->index >= dtlck->maxcnt)
2934 dtlck = (struct dt_lock *) txLinelock(dtlck);
2935 lv = &dtlck->lv[dtlck->index];
2936 lv->offset = stbl[i];
2943 (void) txCommit(tid, 1, &inode, 0);
2949 * Buffer to hold directory entry info while traversing a dtree page
2950 * before being fed to the filldir function
2960 * function to determine next variable-sized jfs_dirent in buffer
2962 static inline struct jfs_dirent *next_jfs_dirent(struct jfs_dirent *dirent)
2964 return (struct jfs_dirent *)
2966 ((sizeof (struct jfs_dirent) + dirent->name_len + 1 +
2967 sizeof (loff_t) - 1) &
2968 ~(sizeof (loff_t) - 1)));
2974 * function: read directory entries sequentially
2975 * from the specified entry offset
2979 * return: offset = (pn, index) of start entry
2980 * of next jfs_readdir()/dtRead()
2982 int jfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
2984 struct inode *ip = filp->f_dentry->d_inode;
2985 struct nls_table *codepage = JFS_SBI(ip->i_sb)->nls_tab;
2987 loff_t dtpos; /* legacy OS/2 style position */
2992 } *dtoffset = (struct dtoffset *) &dtpos;
2994 struct metapage *mp;
2998 struct btstack btstack;
3002 int d_namleft, len, outlen;
3003 unsigned long dirent_buf;
3007 uint loop_count = 0;
3008 struct jfs_dirent *jfs_dirent;
3010 int overflow, fix_page, page_fixed = 0;
3011 static int unique_pos = 2; /* If we can't fix broken index */
3013 if (filp->f_pos == DIREND)
3018 * persistent index is stored in directory entries.
3019 * Special cases: 0 = .
3021 * -1 = End of directory
3025 dir_index = (u32) filp->f_pos;
3027 if (dir_index > 1) {
3028 struct dir_table_slot dirtab_slot;
3031 (dir_index >= JFS_IP(ip)->next_index)) {
3032 /* Stale position. Directory has shrunk */
3033 filp->f_pos = DIREND;
3037 rc = read_index(ip, dir_index, &dirtab_slot);
3039 filp->f_pos = DIREND;
3042 if (dirtab_slot.flag == DIR_INDEX_FREE) {
3043 if (loop_count++ > JFS_IP(ip)->next_index) {
3044 jfs_err("jfs_readdir detected "
3046 filp->f_pos = DIREND;
3049 dir_index = le32_to_cpu(dirtab_slot.addr2);
3050 if (dir_index == -1) {
3051 filp->f_pos = DIREND;
3056 bn = addressDTS(&dirtab_slot);
3057 index = dirtab_slot.slot;
3058 DT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
3060 filp->f_pos = DIREND;
3063 if (p->header.flag & BT_INTERNAL) {
3064 jfs_err("jfs_readdir: bad index table");
3070 if (dir_index == 0) {
3075 if (filldir(dirent, ".", 1, 0, ip->i_ino,
3083 if (filldir(dirent, "..", 2, 1, PARENT(ip), DT_DIR))
3087 * Find first entry of left-most leaf
3090 filp->f_pos = DIREND;
3094 if ((rc = dtReadFirst(ip, &btstack)))
3097 DT_GETSEARCH(ip, btstack.top, bn, mp, p, index);
3101 * Legacy filesystem - OS/2 & Linux JFS < 0.3.6
3103 * pn = index = 0: First entry "."
3104 * pn = 0; index = 1: Second entry ".."
3105 * pn > 0: Real entries, pn=1 -> leftmost page
3106 * pn = index = -1: No more entries
3108 dtpos = filp->f_pos;
3110 /* build "." entry */
3112 if (filldir(dirent, ".", 1, filp->f_pos, ip->i_ino,
3115 dtoffset->index = 1;
3116 filp->f_pos = dtpos;
3119 if (dtoffset->pn == 0) {
3120 if (dtoffset->index == 1) {
3121 /* build ".." entry */
3123 if (filldir(dirent, "..", 2, filp->f_pos,
3124 PARENT(ip), DT_DIR))
3127 jfs_err("jfs_readdir called with "
3131 dtoffset->index = 0;
3132 filp->f_pos = dtpos;
3136 filp->f_pos = DIREND;
3140 if ((rc = dtReadNext(ip, &filp->f_pos, &btstack))) {
3141 jfs_err("jfs_readdir: unexpected rc = %d "
3142 "from dtReadNext", rc);
3143 filp->f_pos = DIREND;
3146 /* get start leaf page and index */
3147 DT_GETSEARCH(ip, btstack.top, bn, mp, p, index);
3149 /* offset beyond directory eof ? */
3151 filp->f_pos = DIREND;
3156 dirent_buf = __get_free_page(GFP_KERNEL);
3157 if (dirent_buf == 0) {
3159 jfs_warn("jfs_readdir: __get_free_page failed!");
3160 filp->f_pos = DIREND;
3165 jfs_dirent = (struct jfs_dirent *) dirent_buf;
3167 overflow = fix_page = 0;
3169 stbl = DT_GETSTBL(p);
3171 for (i = index; i < p->header.nextindex; i++) {
3172 d = (struct ldtentry *) & p->slot[stbl[i]];
3174 if (((long) jfs_dirent + d->namlen + 1) >
3175 (dirent_buf + PSIZE)) {
3176 /* DBCS codepages could overrun dirent_buf */
3182 d_namleft = d->namlen;
3183 name_ptr = jfs_dirent->name;
3184 jfs_dirent->ino = le32_to_cpu(d->inumber);
3187 len = min(d_namleft, DTLHDRDATALEN);
3188 jfs_dirent->position = le32_to_cpu(d->index);
3190 * d->index should always be valid, but it
3191 * isn't. fsck.jfs doesn't create the
3192 * directory index for the lost+found
3193 * directory. Rather than let it go,
3194 * we can try to fix it.
3196 if ((jfs_dirent->position < 2) ||
3197 (jfs_dirent->position >=
3198 JFS_IP(ip)->next_index)) {
3199 if (!page_fixed && !isReadOnly(ip)) {
3202 * setting overflow and setting
3203 * index to i will cause the
3204 * same page to be processed
3205 * again starting here
3211 jfs_dirent->position = unique_pos++;
3214 jfs_dirent->position = dtpos;
3215 len = min(d_namleft, DTLHDRDATALEN_LEGACY);
3218 /* copy the name of head/only segment */
3219 outlen = jfs_strfromUCS_le(name_ptr, d->name, len,
3221 jfs_dirent->name_len = outlen;
3223 /* copy name in the additional segment(s) */
3226 t = (struct dtslot *) & p->slot[next];
3230 if (d_namleft == 0) {
3232 "JFS:Dtree error: ino = "
3233 "%ld, bn=%Ld, index = %d",
3239 len = min(d_namleft, DTSLOTDATALEN);
3240 outlen = jfs_strfromUCS_le(name_ptr, t->name,
3242 jfs_dirent->name_len += outlen;
3248 jfs_dirent = next_jfs_dirent(jfs_dirent);
3255 /* Point to next leaf page */
3256 if (p->header.flag & BT_ROOT)
3259 bn = le64_to_cpu(p->header.next);
3261 /* update offset (pn:index) for new page */
3264 dtoffset->index = 0;
3270 /* unpin previous leaf page */
3273 jfs_dirent = (struct jfs_dirent *) dirent_buf;
3274 while (jfs_dirents--) {
3275 filp->f_pos = jfs_dirent->position;
3276 if (filldir(dirent, jfs_dirent->name,
3277 jfs_dirent->name_len, filp->f_pos,
3278 jfs_dirent->ino, DT_UNKNOWN))
3280 jfs_dirent = next_jfs_dirent(jfs_dirent);
3284 add_missing_indices(ip, bn);
3288 if (!overflow && (bn == 0)) {
3289 filp->f_pos = DIREND;
3293 DT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
3295 free_page(dirent_buf);
3301 free_page(dirent_buf);
3310 * function: get the leftmost page of the directory
3312 static int dtReadFirst(struct inode *ip, struct btstack * btstack)
3316 int psize = 288; /* initial in-line directory */
3317 struct metapage *mp;
3320 struct btframe *btsp;
3323 BT_CLR(btstack); /* reset stack */
3326 * descend leftmost path of the tree
3328 * by convention, root bn = 0.
3331 DT_GETPAGE(ip, bn, mp, psize, p, rc);
3336 * leftmost leaf page
3338 if (p->header.flag & BT_LEAF) {
3339 /* return leftmost entry */
3340 btsp = btstack->top;
3349 * descend down to leftmost child page
3351 if (BT_STACK_FULL(btstack)) {
3353 jfs_error(ip->i_sb, "dtReadFirst: btstack overrun");
3354 BT_STACK_DUMP(btstack);
3357 /* push (bn, index) of the parent page/entry */
3358 BT_PUSH(btstack, bn, 0);
3360 /* get the leftmost entry */
3361 stbl = DT_GETSTBL(p);
3362 xd = (pxd_t *) & p->slot[stbl[0]];
3364 /* get the child page block address */
3365 bn = addressPXD(xd);
3366 psize = lengthPXD(xd) << JFS_SBI(ip->i_sb)->l2bsize;
3368 /* unpin the parent page */
3377 * function: get the page of the specified offset (pn:index)
3379 * return: if (offset > eof), bn = -1;
3381 * note: if index > nextindex of the target leaf page,
3382 * start with 1st entry of next leaf page;
3384 static int dtReadNext(struct inode *ip, loff_t * offset,
3385 struct btstack * btstack)
3392 } *dtoffset = (struct dtoffset *) offset;
3394 struct metapage *mp;
3399 struct btframe *btsp, *parent;
3403 * get leftmost leaf page pinned
3405 if ((rc = dtReadFirst(ip, btstack)))
3409 DT_GETSEARCH(ip, btstack->top, bn, mp, p, index);
3411 /* get the start offset (pn:index) */
3412 pn = dtoffset->pn - 1; /* Now pn = 0 represents leftmost leaf */
3413 index = dtoffset->index;
3415 /* start at leftmost page ? */
3417 /* offset beyond eof ? */
3418 if (index < p->header.nextindex)
3421 if (p->header.flag & BT_ROOT) {
3426 /* start with 1st entry of next leaf page */
3428 dtoffset->index = index = 0;
3432 /* start at non-leftmost page: scan parent pages for large pn */
3433 if (p->header.flag & BT_ROOT) {
3438 /* start after next leaf page ? */
3442 /* get leaf page pn = 1 */
3444 bn = le64_to_cpu(p->header.next);
3446 /* unpin leaf page */
3449 /* offset beyond eof ? */
3458 * scan last internal page level to get target leaf page
3461 /* unpin leftmost leaf page */
3464 /* get left most parent page */
3465 btsp = btstack->top;
3468 DT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
3472 /* scan parent pages at last internal page level */
3473 while (pn >= p->header.nextindex) {
3474 pn -= p->header.nextindex;
3476 /* get next parent page address */
3477 bn = le64_to_cpu(p->header.next);
3479 /* unpin current parent page */
3482 /* offset beyond eof ? */
3488 /* get next parent page */
3489 DT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
3493 /* update parent page stack frame */
3497 /* get leaf page address */
3498 stbl = DT_GETSTBL(p);
3499 xd = (pxd_t *) & p->slot[stbl[pn]];
3500 bn = addressPXD(xd);
3502 /* unpin parent page */
3506 * get target leaf page
3509 DT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
3514 * leaf page has been completed:
3515 * start with 1st entry of next leaf page
3517 if (index >= p->header.nextindex) {
3518 bn = le64_to_cpu(p->header.next);
3520 /* unpin leaf page */
3523 /* offset beyond eof ? */
3529 /* get next leaf page */
3530 DT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
3534 /* start with 1st entry of next leaf page */
3536 dtoffset->index = 0;
3540 /* return target leaf page pinned */
3541 btsp = btstack->top;
3543 btsp->index = dtoffset->index;
3553 * function: compare search key with an internal entry
3556 * < 0 if k is < record
3557 * = 0 if k is = record
3558 * > 0 if k is > record
3560 static int dtCompare(struct component_name * key, /* search key */
3561 dtpage_t * p, /* directory page */
3563 { /* entry slot index */
3564 wchar_t *kname, *name;
3565 int klen, namlen, len, rc;
3566 struct idtentry *ih;
3570 * force the left-most key on internal pages, at any level of
3571 * the tree, to be less than any search key.
3572 * this obviates having to update the leftmost key on an internal
3573 * page when the user inserts a new key in the tree smaller than
3574 * anything that has been stored.
3576 * (? if/when dtSearch() narrows down to 1st entry (index = 0),
3577 * at any internal page at any level of the tree,
3578 * it descends to child of the entry anyway -
3579 * ? make the entry as min size dummy entry)
3581 * if (e->index == 0 && h->prevpg == P_INVALID && !(h->flags & BT_LEAF))
3588 ih = (struct idtentry *) & p->slot[si];
3591 namlen = ih->namlen;
3592 len = min(namlen, DTIHDRDATALEN);
3594 /* compare with head/only segment */
3595 len = min(klen, len);
3596 if ((rc = UniStrncmp_le(kname, name, len)))
3602 /* compare with additional segment(s) */
3604 while (klen > 0 && namlen > 0) {
3605 /* compare with next name segment */
3606 t = (struct dtslot *) & p->slot[si];
3607 len = min(namlen, DTSLOTDATALEN);
3608 len = min(klen, len);
3610 if ((rc = UniStrncmp_le(kname, name, len)))
3619 return (klen - namlen);
3628 * function: compare search key with an (leaf/internal) entry
3631 * < 0 if k is < record
3632 * = 0 if k is = record
3633 * > 0 if k is > record
3635 static int ciCompare(struct component_name * key, /* search key */
3636 dtpage_t * p, /* directory page */
3637 int si, /* entry slot index */
3640 wchar_t *kname, *name, x;
3641 int klen, namlen, len, rc;
3642 struct ldtentry *lh;
3643 struct idtentry *ih;
3648 * force the left-most key on internal pages, at any level of
3649 * the tree, to be less than any search key.
3650 * this obviates having to update the leftmost key on an internal
3651 * page when the user inserts a new key in the tree smaller than
3652 * anything that has been stored.
3654 * (? if/when dtSearch() narrows down to 1st entry (index = 0),
3655 * at any internal page at any level of the tree,
3656 * it descends to child of the entry anyway -
3657 * ? make the entry as min size dummy entry)
3659 * if (e->index == 0 && h->prevpg == P_INVALID && !(h->flags & BT_LEAF))
3669 if (p->header.flag & BT_LEAF) {
3670 lh = (struct ldtentry *) & p->slot[si];
3673 namlen = lh->namlen;
3674 if (flag & JFS_DIR_INDEX)
3675 len = min(namlen, DTLHDRDATALEN);
3677 len = min(namlen, DTLHDRDATALEN_LEGACY);
3680 * internal page entry
3683 ih = (struct idtentry *) & p->slot[si];
3686 namlen = ih->namlen;
3687 len = min(namlen, DTIHDRDATALEN);
3690 /* compare with head/only segment */
3691 len = min(klen, len);
3692 for (i = 0; i < len; i++, kname++, name++) {
3693 /* only uppercase if case-insensitive support is on */
3694 if ((flag & JFS_OS2) == JFS_OS2)
3695 x = UniToupper(le16_to_cpu(*name));
3697 x = le16_to_cpu(*name);
3698 if ((rc = *kname - x))
3705 /* compare with additional segment(s) */
3706 while (klen > 0 && namlen > 0) {
3707 /* compare with next name segment */
3708 t = (struct dtslot *) & p->slot[si];
3709 len = min(namlen, DTSLOTDATALEN);
3710 len = min(klen, len);
3712 for (i = 0; i < len; i++, kname++, name++) {
3713 /* only uppercase if case-insensitive support is on */
3714 if ((flag & JFS_OS2) == JFS_OS2)
3715 x = UniToupper(le16_to_cpu(*name));
3717 x = le16_to_cpu(*name);
3719 if ((rc = *kname - x))
3728 return (klen - namlen);
3733 * ciGetLeafPrefixKey()
3735 * function: compute prefix of suffix compression
3736 * from two adjacent leaf entries
3737 * across page boundary
3739 * return: non-zero on error
3742 static int ciGetLeafPrefixKey(dtpage_t * lp, int li, dtpage_t * rp,
3743 int ri, struct component_name * key, int flag)
3746 wchar_t *pl, *pr, *kname;
3747 struct component_name lkey;
3748 struct component_name rkey;
3750 lkey.name = (wchar_t *) kmalloc((JFS_NAME_MAX + 1) * sizeof(wchar_t),
3752 if (lkey.name == NULL)
3755 rkey.name = (wchar_t *) kmalloc((JFS_NAME_MAX + 1) * sizeof(wchar_t),
3757 if (rkey.name == NULL) {
3762 /* get left and right key */
3763 dtGetKey(lp, li, &lkey, flag);
3764 lkey.name[lkey.namlen] = 0;
3766 if ((flag & JFS_OS2) == JFS_OS2)
3769 dtGetKey(rp, ri, &rkey, flag);
3770 rkey.name[rkey.namlen] = 0;
3773 if ((flag & JFS_OS2) == JFS_OS2)
3776 /* compute prefix */
3779 namlen = min(lkey.namlen, rkey.namlen);
3780 for (pl = lkey.name, pr = rkey.name;
3781 namlen; pl++, pr++, namlen--, klen++, kname++) {
3784 key->namlen = klen + 1;
3789 /* l->namlen <= r->namlen since l <= r */
3790 if (lkey.namlen < rkey.namlen) {
3792 key->namlen = klen + 1;
3793 } else /* l->namelen == r->namelen */
3807 * function: get key of the entry
3809 static void dtGetKey(dtpage_t * p, int i, /* entry index */
3810 struct component_name * key, int flag)
3814 struct ldtentry *lh;
3815 struct idtentry *ih;
3818 wchar_t *name, *kname;
3821 stbl = DT_GETSTBL(p);
3823 if (p->header.flag & BT_LEAF) {
3824 lh = (struct ldtentry *) & p->slot[si];
3826 namlen = lh->namlen;
3828 if (flag & JFS_DIR_INDEX)
3829 len = min(namlen, DTLHDRDATALEN);
3831 len = min(namlen, DTLHDRDATALEN_LEGACY);
3833 ih = (struct idtentry *) & p->slot[si];
3835 namlen = ih->namlen;
3837 len = min(namlen, DTIHDRDATALEN);
3840 key->namlen = namlen;
3844 * move head/only segment
3846 UniStrncpy_le(kname, name, len);
3849 * move additional segment(s)
3852 /* get next segment */
3856 len = min(namlen, DTSLOTDATALEN);
3857 UniStrncpy_le(kname, t->name, len);
3867 * function: allocate free slot(s) and
3868 * write a leaf/internal entry
3870 * return: entry slot index
3872 static void dtInsertEntry(dtpage_t * p, int index, struct component_name * key,
3873 ddata_t * data, struct dt_lock ** dtlock)
3875 struct dtslot *h, *t;
3876 struct ldtentry *lh = NULL;
3877 struct idtentry *ih = NULL;
3878 int hsi, fsi, klen, len, nextindex;
3879 wchar_t *kname, *name;
3882 struct dt_lock *dtlck = *dtlock;
3886 struct metapage *mp = NULL;
3891 /* allocate a free slot */
3892 hsi = fsi = p->header.freelist;
3894 p->header.freelist = h->next;
3895 --p->header.freecnt;
3897 /* open new linelock */
3898 if (dtlck->index >= dtlck->maxcnt)
3899 dtlck = (struct dt_lock *) txLinelock(dtlck);
3901 lv = & dtlck->lv[dtlck->index];
3904 /* write head/only segment */
3905 if (p->header.flag & BT_LEAF) {
3906 lh = (struct ldtentry *) h;
3908 lh->inumber = data->leaf.ino; /* little-endian */
3911 if (data->leaf.ip) {
3912 len = min(klen, DTLHDRDATALEN);
3913 if (!(p->header.flag & BT_ROOT))
3914 bn = addressPXD(&p->header.self);
3915 lh->index = cpu_to_le32(add_index(data->leaf.tid,
3919 len = min(klen, DTLHDRDATALEN_LEGACY);
3921 ih = (struct idtentry *) h;
3927 len = min(klen, DTIHDRDATALEN);
3930 UniStrncpy_le(name, kname, len);
3935 /* write additional segment(s) */
3940 fsi = p->header.freelist;
3942 p->header.freelist = t->next;
3943 --p->header.freecnt;
3945 /* is next slot contiguous ? */
3946 if (fsi != xsi + 1) {
3947 /* close current linelock */
3951 /* open new linelock */
3952 if (dtlck->index < dtlck->maxcnt)
3955 dtlck = (struct dt_lock *) txLinelock(dtlck);
3956 lv = & dtlck->lv[0];
3964 len = min(klen, DTSLOTDATALEN);
3965 UniStrncpy_le(t->name, kname, len);
3972 /* close current linelock */
3978 /* terminate last/only segment */
3980 /* single segment entry */
3981 if (p->header.flag & BT_LEAF)
3986 /* multi-segment entry */
3989 /* if insert into middle, shift right succeeding entries in stbl */
3990 stbl = DT_GETSTBL(p);
3991 nextindex = p->header.nextindex;
3992 if (index < nextindex) {
3993 memmove(stbl + index + 1, stbl + index, nextindex - index);
3995 if ((p->header.flag & BT_LEAF) && data->leaf.ip) {
3999 * Need to update slot number for entries that moved
4003 for (n = index + 1; n <= nextindex; n++) {
4004 lh = (struct ldtentry *) & (p->slot[stbl[n]]);
4005 modify_index(data->leaf.tid, data->leaf.ip,
4006 le32_to_cpu(lh->index), bn, n,
4010 release_metapage(mp);
4016 /* advance next available entry index of stbl */
4017 ++p->header.nextindex;
4024 * function: move entries from split/left page to new/right page
4026 * nextindex of dst page and freelist/freecnt of both pages
4029 static void dtMoveEntry(dtpage_t * sp, int si, dtpage_t * dp,
4030 struct dt_lock ** sdtlock, struct dt_lock ** ddtlock,
4033 int ssi, next; /* src slot index */
4034 int di; /* dst entry index */
4035 int dsi; /* dst slot index */
4036 s8 *sstbl, *dstbl; /* sorted entry table */
4038 struct ldtentry *slh, *dlh = NULL;
4039 struct idtentry *sih, *dih = NULL;
4040 struct dtslot *h, *s, *d;
4041 struct dt_lock *sdtlck = *sdtlock, *ddtlck = *ddtlock;
4042 struct lv *slv, *dlv;
4046 sstbl = (s8 *) & sp->slot[sp->header.stblindex];
4047 dstbl = (s8 *) & dp->slot[dp->header.stblindex];
4049 dsi = dp->header.freelist; /* first (whole page) free slot */
4050 sfsi = sp->header.freelist;
4052 /* linelock destination entry slot */
4053 dlv = & ddtlck->lv[ddtlck->index];
4056 /* linelock source entry slot */
4057 slv = & sdtlck->lv[sdtlck->index];
4058 slv->offset = sstbl[si];
4059 xssi = slv->offset - 1;
4065 for (di = 0; si < sp->header.nextindex; si++, di++) {
4069 /* is next slot contiguous ? */
4070 if (ssi != xssi + 1) {
4071 /* close current linelock */
4075 /* open new linelock */
4076 if (sdtlck->index < sdtlck->maxcnt)
4079 sdtlck = (struct dt_lock *) txLinelock(sdtlck);
4080 slv = & sdtlck->lv[0];
4088 * move head/only segment of an entry
4091 h = d = &dp->slot[dsi];
4093 /* get src slot and move */
4095 if (sp->header.flag & BT_LEAF) {
4096 /* get source entry */
4097 slh = (struct ldtentry *) s;
4098 dlh = (struct ldtentry *) h;
4099 snamlen = slh->namlen;
4102 len = min(snamlen, DTLHDRDATALEN);
4103 dlh->index = slh->index; /* little-endian */
4105 len = min(snamlen, DTLHDRDATALEN_LEGACY);
4107 memcpy(dlh, slh, 6 + len * 2);
4111 /* update dst head/only segment next field */
4115 sih = (struct idtentry *) s;
4116 snamlen = sih->namlen;
4118 len = min(snamlen, DTIHDRDATALEN);
4119 dih = (struct idtentry *) h;
4120 memcpy(dih, sih, 10 + len * 2);
4127 /* free src head/only segment */
4137 * move additional segment(s) of the entry
4140 while ((ssi = next) >= 0) {
4141 /* is next slot contiguous ? */
4142 if (ssi != xssi + 1) {
4143 /* close current linelock */
4147 /* open new linelock */
4148 if (sdtlck->index < sdtlck->maxcnt)
4154 slv = & sdtlck->lv[0];
4161 /* get next source segment */
4164 /* get next destination free slot */
4167 len = min(snamlen, DTSLOTDATALEN);
4168 UniStrncpy(d->name, s->name, len);
4177 /* free source segment */
4186 /* terminate dst last/only segment */
4188 /* single segment entry */
4189 if (dp->header.flag & BT_LEAF)
4194 /* multi-segment entry */
4198 /* close current linelock */
4207 /* update source header */
4208 sp->header.freelist = sfsi;
4209 sp->header.freecnt += nd;
4211 /* update destination header */
4212 dp->header.nextindex = di;
4214 dp->header.freelist = dsi;
4215 dp->header.freecnt -= nd;
4222 * function: free a (leaf/internal) entry
4224 * log freelist header, stbl, and each segment slot of entry
4225 * (even though last/only segment next field is modified,
4226 * physical image logging requires all segment slots of
4227 * the entry logged to avoid applying previous updates
4228 * to the same slots)
4230 static void dtDeleteEntry(dtpage_t * p, int fi, struct dt_lock ** dtlock)
4232 int fsi; /* free entry slot index */
4236 struct dt_lock *dtlck = *dtlock;
4240 /* get free entry slot index */
4241 stbl = DT_GETSTBL(p);
4244 /* open new linelock */
4245 if (dtlck->index >= dtlck->maxcnt)
4246 dtlck = (struct dt_lock *) txLinelock(dtlck);
4247 lv = & dtlck->lv[dtlck->index];
4251 /* get the head/only segment */
4253 if (p->header.flag & BT_LEAF)
4254 si = ((struct ldtentry *) t)->next;
4256 si = ((struct idtentry *) t)->next;
4263 /* find the last/only segment */
4265 /* is next slot contiguous ? */
4266 if (si != xsi + 1) {
4267 /* close current linelock */
4271 /* open new linelock */
4272 if (dtlck->index < dtlck->maxcnt)
4275 dtlck = (struct dt_lock *) txLinelock(dtlck);
4276 lv = & dtlck->lv[0];
4292 /* close current linelock */
4298 /* update freelist */
4299 t->next = p->header.freelist;
4300 p->header.freelist = fsi;
4301 p->header.freecnt += freecnt;
4303 /* if delete from middle,
4304 * shift left the succedding entries in the stbl
4306 si = p->header.nextindex;
4308 memmove(&stbl[fi], &stbl[fi + 1], si - fi - 1);
4310 p->header.nextindex--;
4317 * function: truncate a (leaf/internal) entry
4319 * log freelist header, stbl, and each segment slot of entry
4320 * (even though last/only segment next field is modified,
4321 * physical image logging requires all segment slots of
4322 * the entry logged to avoid applying previous updates
4323 * to the same slots)
4325 static void dtTruncateEntry(dtpage_t * p, int ti, struct dt_lock ** dtlock)
4327 int tsi; /* truncate entry slot index */
4331 struct dt_lock *dtlck = *dtlock;
4335 /* get free entry slot index */
4336 stbl = DT_GETSTBL(p);
4339 /* open new linelock */
4340 if (dtlck->index >= dtlck->maxcnt)
4341 dtlck = (struct dt_lock *) txLinelock(dtlck);
4342 lv = & dtlck->lv[dtlck->index];
4346 /* get the head/only segment */
4348 ASSERT(p->header.flag & BT_INTERNAL);
4349 ((struct idtentry *) t)->namlen = 0;
4350 si = ((struct idtentry *) t)->next;
4351 ((struct idtentry *) t)->next = -1;
4358 /* find the last/only segment */
4360 /* is next slot contiguous ? */
4361 if (si != xsi + 1) {
4362 /* close current linelock */
4366 /* open new linelock */
4367 if (dtlck->index < dtlck->maxcnt)
4370 dtlck = (struct dt_lock *) txLinelock(dtlck);
4371 lv = & dtlck->lv[0];
4387 /* close current linelock */
4393 /* update freelist */
4396 t->next = p->header.freelist;
4397 p->header.freelist = fsi;
4398 p->header.freecnt += freecnt;
4403 * dtLinelockFreelist()
4405 static void dtLinelockFreelist(dtpage_t * p, /* directory page */
4406 int m, /* max slot index */
4407 struct dt_lock ** dtlock)
4409 int fsi; /* free entry slot index */
4412 struct dt_lock *dtlck = *dtlock;
4416 /* get free entry slot index */
4417 fsi = p->header.freelist;
4419 /* open new linelock */
4420 if (dtlck->index >= dtlck->maxcnt)
4421 dtlck = (struct dt_lock *) txLinelock(dtlck);
4422 lv = & dtlck->lv[dtlck->index];
4432 /* find the last/only segment */
4433 while (si < m && si >= 0) {
4434 /* is next slot contiguous ? */
4435 if (si != xsi + 1) {
4436 /* close current linelock */
4440 /* open new linelock */
4441 if (dtlck->index < dtlck->maxcnt)
4444 dtlck = (struct dt_lock *) txLinelock(dtlck);
4445 lv = & dtlck->lv[0];
4459 /* close current linelock */
4470 * FUNCTION: Modify the inode number part of a directory entry
4473 * tid - Transaction id
4474 * ip - Inode of parent directory
4475 * key - Name of entry to be modified
4476 * orig_ino - Original inode number expected in entry
4477 * new_ino - New inode number to put into entry
4481 * -ESTALE - If entry found does not match orig_ino passed in
4482 * -ENOENT - If no entry can be found to match key
4483 * 0 - If successfully modified entry
4485 int dtModify(tid_t tid, struct inode *ip,
4486 struct component_name * key, ino_t * orig_ino, ino_t new_ino, int flag)
4490 struct metapage *mp;
4493 struct btstack btstack;
4495 struct dt_lock *dtlck;
4498 int entry_si; /* entry slot index */
4499 struct ldtentry *entry;
4502 * search for the entry to modify:
4504 * dtSearch() returns (leaf page pinned, index at which to modify).
4506 if ((rc = dtSearch(ip, key, orig_ino, &btstack, flag)))
4509 /* retrieve search result */
4510 DT_GETSEARCH(ip, btstack.top, bn, mp, p, index);
4512 BT_MARK_DIRTY(mp, ip);
4514 * acquire a transaction lock on the leaf page of named entry
4516 tlck = txLock(tid, ip, mp, tlckDTREE | tlckENTRY);
4517 dtlck = (struct dt_lock *) & tlck->lock;
4519 /* get slot index of the entry */
4520 stbl = DT_GETSTBL(p);
4521 entry_si = stbl[index];
4523 /* linelock entry */
4524 ASSERT(dtlck->index == 0);
4525 lv = & dtlck->lv[0];
4526 lv->offset = entry_si;
4530 /* get the head/only segment */
4531 entry = (struct ldtentry *) & p->slot[entry_si];
4533 /* substitute the inode number of the entry */
4534 entry->inumber = cpu_to_le32(new_ino);
4536 /* unpin the leaf page */
4542 #ifdef _JFS_DEBUG_DTREE
4546 * function: traverse forward
4548 int dtDisplayTree(struct inode *ip)
4551 struct metapage *mp;
4554 int index, lastindex, v, h;
4556 struct btstack btstack;
4557 struct btframe *btsp;
4558 struct btframe *parent;
4562 printk("display B+-tree.\n");
4565 btsp = btstack.stack;
4570 * root resides in the inode
4576 * first access of each page:
4579 DT_GETPAGE(ip, bn, mp, psize, p, rc);
4583 /* process entries forward from first index */
4585 lastindex = p->header.nextindex - 1;
4587 if (p->header.flag & BT_INTERNAL) {
4589 * first access of each internal page
4591 printf("internal page ");
4592 dtDisplayPage(ip, bn, p);
4595 } else { /* (p->header.flag & BT_LEAF) */
4598 * first access of each leaf page
4600 printf("leaf page ");
4601 dtDisplayPage(ip, bn, p);
4604 * process leaf page entries
4606 for ( ; index <= lastindex; index++)
4611 /* unpin the leaf page */
4616 * go back up to the parent page
4619 /* pop/restore parent entry for the current child page */
4620 if ((parent = (btsp == btstack.stack ? NULL : --btsp)) == NULL)
4621 /* current page must have been root */
4625 * parent page scan completed
4627 if ((index = parent->index) == (lastindex = parent->lastindex)) {
4628 /* go back up to the parent page */
4633 * parent page has entries remaining
4635 /* get back the parent page */
4637 /* v = parent->level; */
4638 DT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
4642 /* get next parent entry */
4646 * internal page: go down to child page of current entry
4649 /* push/save current parent entry for the child page */
4650 btsp->bn = pbn = bn;
4651 btsp->index = index;
4652 btsp->lastindex = lastindex;
4653 /* btsp->level = v; */
4654 /* btsp->node = h; */
4657 /* get current entry for the child page */
4658 stbl = DT_GETSTBL(p);
4659 xd = (pxd_t *) & p->slot[stbl[index]];
4662 * first access of each internal entry:
4665 /* get child page */
4666 bn = addressPXD(xd);
4667 psize = lengthPXD(xd) << ip->i_ipmnt->i_l2bsize;
4669 printk("traverse down 0x%Lx[%d]->0x%Lx\n", pbn, index, bn);
4673 /* release parent page */
4676 /* process the child page */
4684 * function: display page
4686 int dtDisplayPage(struct inode *ip, s64 bn, dtpage_t * p)
4689 struct metapage *mp;
4690 struct ldtentry *lh;
4691 struct idtentry *ih;
4695 wchar_t name[JFS_NAME_MAX + 1];
4696 struct component_name key = { 0, name };
4701 DT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
4706 /* display page control */
4707 printk("bn:0x%Lx flag:0x%08x nextindex:%d\n",
4708 bn, p->header.flag, p->header.nextindex);
4710 /* display entries */
4711 stbl = DT_GETSTBL(p);
4712 for (i = 0, j = 1; i < p->header.nextindex; i++, j++) {
4713 dtGetKey(p, i, &key, JFS_SBI(ip->i_sb)->mntflag);
4714 key.name[key.namlen] = '\0';
4715 if (p->header.flag & BT_LEAF) {
4716 lh = (struct ldtentry *) & p->slot[stbl[i]];
4717 printf("\t[%d] %s:%d", i, key.name,
4718 le32_to_cpu(lh->inumber));
4720 ih = (struct idtentry *) & p->slot[stbl[i]];
4722 bn = addressPXD(xd);
4723 printf("\t[%d] %s:0x%Lx", i, key.name, bn);
4739 #endif /* _JFS_DEBUG_DTREE */