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)) {
378 * It's time to move the inline table to an external
379 * page and begin to build the xtree
381 if (dbAlloc(ip, 0, sbi->nbperpage, &xaddr))
382 goto clean_up; /* No space */
385 * Save the table, we're going to overwrite it with the
388 struct dir_table_slot temp_table[12];
389 memcpy(temp_table, &jfs_ip->i_dirtable, sizeof(temp_table));
392 * Initialize empty x-tree
397 * Allocate the first block & add it to the xtree
399 if (xtInsert(tid, ip, 0, 0, sbi->nbperpage, &xaddr, 0)) {
400 /* This really shouldn't fail */
401 jfs_warn("add_index: xtInsert failed!");
402 memcpy(&jfs_ip->i_dirtable, temp_table,
403 sizeof (temp_table));
407 ip->i_blocks += LBLK2PBLK(sb, sbi->nbperpage);
409 if ((mp = get_index_page(ip, 0)) == 0) {
410 jfs_err("add_index: get_metapage failed!");
411 xtTruncate(tid, ip, 0, COMMIT_PWMAP);
412 memcpy(&jfs_ip->i_dirtable, temp_table,
413 sizeof (temp_table));
416 tlck = txLock(tid, ip, mp, tlckDATA);
417 llck = (struct linelock *) & tlck->lock;
418 ASSERT(llck->index == 0);
422 lv->length = 6; /* tlckDATA slot size is 16 bytes */
425 memcpy(mp->data, temp_table, sizeof(temp_table));
427 mark_metapage_dirty(mp);
428 release_metapage(mp);
431 * Logging is now directed by xtree tlocks
433 clear_cflag(COMMIT_Dirtable, ip);
436 offset = (index - 2) * sizeof(struct dir_table_slot);
437 page_offset = offset & (PSIZE - 1);
438 blkno = ((offset + 1) >> L2PSIZE) << sbi->l2nbperpage;
439 if (page_offset == 0) {
441 * This will be the beginning of a new page
444 if (xtInsert(tid, ip, 0, blkno, sbi->nbperpage, &xaddr, 0)) {
445 jfs_warn("add_index: xtInsert failed!");
449 ip->i_blocks += LBLK2PBLK(sb, sbi->nbperpage);
451 if ((mp = get_index_page(ip, blkno)))
452 memset(mp->data, 0, PSIZE); /* Just looks better */
454 xtTruncate(tid, ip, offset, COMMIT_PWMAP);
456 mp = read_index_page(ip, blkno);
459 jfs_err("add_index: get/read_metapage failed!");
463 lock_index(tid, ip, mp, index);
466 (struct dir_table_slot *) ((char *) mp->data + page_offset);
467 dirtab_slot->flag = DIR_INDEX_VALID;
468 dirtab_slot->slot = slot;
469 DTSaddress(dirtab_slot, bn);
471 mark_metapage_dirty(mp);
472 release_metapage(mp);
478 jfs_ip->next_index--;
486 * Marks an entry to the directory index table as free.
488 static void free_index(tid_t tid, struct inode *ip, u32 index, u32 next)
490 struct dir_table_slot *dirtab_slot;
492 struct metapage *mp = 0;
494 dirtab_slot = find_index(ip, index, &mp, &lblock);
496 if (dirtab_slot == 0)
499 dirtab_slot->flag = DIR_INDEX_FREE;
500 dirtab_slot->slot = dirtab_slot->addr1 = 0;
501 dirtab_slot->addr2 = cpu_to_le32(next);
504 lock_index(tid, ip, mp, index);
505 mark_metapage_dirty(mp);
506 release_metapage(mp);
508 set_cflag(COMMIT_Dirtable, ip);
514 * Changes an entry in the directory index table
516 static void modify_index(tid_t tid, struct inode *ip, u32 index, s64 bn,
517 int slot, struct metapage ** mp, u64 *lblock)
519 struct dir_table_slot *dirtab_slot;
521 dirtab_slot = find_index(ip, index, mp, lblock);
523 if (dirtab_slot == 0)
526 DTSaddress(dirtab_slot, bn);
527 dirtab_slot->slot = slot;
530 lock_index(tid, ip, *mp, index);
531 mark_metapage_dirty(*mp);
533 set_cflag(COMMIT_Dirtable, ip);
539 * reads a directory table slot
541 static int read_index(struct inode *ip, u32 index,
542 struct dir_table_slot * dirtab_slot)
545 struct metapage *mp = 0;
546 struct dir_table_slot *slot;
548 slot = find_index(ip, index, &mp, &lblock);
553 memcpy(dirtab_slot, slot, sizeof(struct dir_table_slot));
556 release_metapage(mp);
565 * Search for the entry with specified key
569 * return: 0 - search result on stack, leaf page pinned;
572 int dtSearch(struct inode *ip, struct component_name * key, ino_t * data,
573 struct btstack * btstack, int flag)
576 int cmp = 1; /* init for empty page */
581 int base, index, lim;
582 struct btframe *btsp;
584 int psize = 288; /* initial in-line directory */
586 struct component_name ciKey;
587 struct super_block *sb = ip->i_sb;
590 (wchar_t *) kmalloc((JFS_NAME_MAX + 1) * sizeof(wchar_t),
592 if (ciKey.name == 0) {
598 /* uppercase search key for c-i directory */
599 UniStrcpy(ciKey.name, key->name);
600 ciKey.namlen = key->namlen;
602 /* only uppercase if case-insensitive support is on */
603 if ((JFS_SBI(sb)->mntflag & JFS_OS2) == JFS_OS2) {
606 BT_CLR(btstack); /* reset stack */
608 /* init level count for max pages to split */
612 * search down tree from root:
614 * between two consecutive entries of <Ki, Pi> and <Kj, Pj> of
615 * internal page, child page Pi contains entry with k, Ki <= K < Kj.
617 * if entry with search key K is not found
618 * internal page search find the entry with largest key Ki
619 * less than K which point to the child page to search;
620 * leaf page search find the entry with smallest key Kj
621 * greater than K so that the returned index is the position of
622 * the entry to be shifted right for insertion of new entry.
623 * for empty tree, search key is greater than any key of the tree.
625 * by convention, root bn = 0.
628 /* get/pin the page to search */
629 DT_GETPAGE(ip, bn, mp, psize, p, rc);
633 /* get sorted entry table of the page */
634 stbl = DT_GETSTBL(p);
637 * binary search with search key K on the current page.
639 for (base = 0, lim = p->header.nextindex; lim; lim >>= 1) {
640 index = base + (lim >> 1);
642 if (p->header.flag & BT_LEAF) {
643 /* uppercase leaf name to compare */
645 ciCompare(&ciKey, p, stbl[index],
646 JFS_SBI(sb)->mntflag);
648 /* router key is in uppercase */
650 cmp = dtCompare(&ciKey, p, stbl[index]);
658 /* search hit - leaf page:
659 * return the entry found
661 if (p->header.flag & BT_LEAF) {
662 inumber = le32_to_cpu(
663 ((struct ldtentry *) & p->slot[stbl[index]])->inumber);
666 * search for JFS_LOOKUP
668 if (flag == JFS_LOOKUP) {
675 * search for JFS_CREATE
677 if (flag == JFS_CREATE) {
684 * search for JFS_REMOVE or JFS_RENAME
686 if ((flag == JFS_REMOVE ||
687 flag == JFS_RENAME) &&
694 * JFS_REMOVE|JFS_FINDDIR|JFS_RENAME
696 /* save search result */
707 /* search hit - internal page:
708 * descend/search its child page
722 * base is the smallest index with key (Kj) greater than
723 * search key (K) and may be zero or (maxindex + 1) index.
726 * search miss - leaf page
728 * return location of entry (base) where new entry with
729 * search key K is to be inserted.
731 if (p->header.flag & BT_LEAF) {
733 * search for JFS_LOOKUP, JFS_REMOVE, or JFS_RENAME
735 if (flag == JFS_LOOKUP || flag == JFS_REMOVE ||
736 flag == JFS_RENAME) {
742 * search for JFS_CREATE|JFS_FINDDIR:
757 * search miss - internal page
759 * if base is non-zero, decrement base by one to get the parent
760 * entry of the child page to search.
762 index = base ? base - 1 : base;
765 * go down to child page
768 /* update max. number of pages to split */
769 if (BT_STACK_FULL(btstack)) {
770 /* Something's corrupted, mark filesytem dirty so
771 * chkdsk will fix it.
773 jfs_error(sb, "stack overrun in dtSearch!");
774 BT_STACK_DUMP(btstack);
780 /* push (bn, index) of the parent page/entry */
781 BT_PUSH(btstack, bn, index);
783 /* get the child page block number */
784 pxd = (pxd_t *) & p->slot[stbl[index]];
785 bn = addressPXD(pxd);
786 psize = lengthPXD(pxd) << JFS_SBI(ip->i_sb)->l2bsize;
788 /* unpin the parent page */
808 * function: insert an entry to directory tree
812 * return: 0 - success;
815 int dtInsert(tid_t tid, struct inode *ip,
816 struct component_name * name, ino_t * fsn, struct btstack * btstack)
819 struct metapage *mp; /* meta-page buffer */
820 dtpage_t *p; /* base B+-tree index page */
823 struct dtsplit split; /* split information */
825 struct dt_lock *dtlck;
831 * retrieve search result
833 * dtSearch() returns (leaf page pinned, index at which to insert).
834 * n.b. dtSearch() may return index of (maxindex + 1) of
837 DT_GETSEARCH(ip, btstack->top, bn, mp, p, index);
840 * insert entry for new key
843 if (JFS_IP(ip)->next_index == DIREND) {
847 n = NDTLEAF(name->namlen);
851 n = NDTLEAF_LEGACY(name->namlen);
852 data.leaf.ip = 0; /* signifies legacy directory format */
854 data.leaf.ino = cpu_to_le32(*fsn);
857 * leaf page does not have enough room for new entry:
859 * extend/split the leaf page;
861 * dtSplitUp() will insert the entry and unpin the leaf page.
863 if (n > p->header.freecnt) {
869 rc = dtSplitUp(tid, ip, &split, btstack);
874 * leaf page does have enough room for new entry:
876 * insert the new data entry into the leaf page;
878 BT_MARK_DIRTY(mp, ip);
880 * acquire a transaction lock on the leaf page
882 tlck = txLock(tid, ip, mp, tlckDTREE | tlckENTRY);
883 dtlck = (struct dt_lock *) & tlck->lock;
884 ASSERT(dtlck->index == 0);
887 /* linelock header */
892 dtInsertEntry(p, index, name, &data, &dtlck);
894 /* linelock stbl of non-root leaf page */
895 if (!(p->header.flag & BT_ROOT)) {
896 if (dtlck->index >= dtlck->maxcnt)
897 dtlck = (struct dt_lock *) txLinelock(dtlck);
898 lv = & dtlck->lv[dtlck->index];
899 n = index >> L2DTSLOTSIZE;
900 lv->offset = p->header.stblindex + n;
902 ((p->header.nextindex - 1) >> L2DTSLOTSIZE) - n + 1;
906 /* unpin the leaf page */
916 * function: propagate insertion bottom up;
920 * return: 0 - success;
922 * leaf page unpinned;
924 static int dtSplitUp(tid_t tid,
925 struct inode *ip, struct dtsplit * split, struct btstack * btstack)
927 struct jfs_sb_info *sbi = JFS_SBI(ip->i_sb);
929 struct metapage *smp;
930 dtpage_t *sp; /* split page */
931 struct metapage *rmp;
932 dtpage_t *rp; /* new right page split from sp */
933 pxd_t rpxd; /* new right page extent descriptor */
934 struct metapage *lmp;
935 dtpage_t *lp; /* left child page */
936 int skip; /* index of entry of insertion */
937 struct btframe *parent; /* parent page entry on traverse stack */
940 struct pxdlist pxdlist;
942 struct component_name key = { 0, 0 };
943 ddata_t *data = split->data;
945 struct dt_lock *dtlck;
951 sp = DT_PAGE(ip, smp);
954 (wchar_t *) kmalloc((JFS_NAME_MAX + 2) * sizeof(wchar_t),
965 * The split routines insert the new entry, and
966 * acquire txLock as appropriate.
969 * split root leaf page:
971 if (sp->header.flag & BT_ROOT) {
973 * allocate a single extent child page
976 n = sbi->bsize >> L2DTSLOTSIZE;
977 n -= (n + 31) >> L2DTSLOTSIZE; /* stbl size */
978 n -= DTROOTMAXSLOT - sp->header.freecnt; /* header + entries */
979 if (n <= split->nslot)
981 if ((rc = dbAlloc(ip, 0, (s64) xlen, &xaddr))) {
988 pxd = &pxdlist.pxd[0];
989 PXDaddress(pxd, xaddr);
990 PXDlength(pxd, xlen);
991 split->pxdlist = &pxdlist;
992 rc = dtSplitRoot(tid, ip, split, &rmp);
1003 * extend first leaf page
1005 * extend the 1st extent if less than buffer page size
1006 * (dtExtendPage() reurns leaf page unpinned)
1008 pxd = &sp->header.self;
1009 xlen = lengthPXD(pxd);
1010 xsize = xlen << sbi->l2bsize;
1011 if (xsize < PSIZE) {
1012 xaddr = addressPXD(pxd);
1013 n = xsize >> L2DTSLOTSIZE;
1014 n -= (n + 31) >> L2DTSLOTSIZE; /* stbl size */
1015 if ((n + sp->header.freecnt) <= split->nslot)
1016 n = xlen + (xlen << 1);
1019 if ((rc = dbReAlloc(sbi->ipbmap, xaddr, (s64) xlen,
1023 pxdlist.maxnpxd = 1;
1025 pxd = &pxdlist.pxd[0];
1026 PXDaddress(pxd, nxaddr)
1027 PXDlength(pxd, xlen + n);
1028 split->pxdlist = &pxdlist;
1029 if ((rc = dtExtendPage(tid, ip, split, btstack))) {
1030 nxaddr = addressPXD(pxd);
1031 if (xaddr != nxaddr) {
1032 /* free relocated extent */
1033 xlen = lengthPXD(pxd);
1034 dbFree(ip, nxaddr, (s64) xlen);
1036 /* free extended delta */
1037 xlen = lengthPXD(pxd) - n;
1038 xaddr = addressPXD(pxd) + xlen;
1039 dbFree(ip, xaddr, (s64) n);
1049 * split leaf page <sp> into <sp> and a new right page <rp>.
1051 * return <rp> pinned and its extent descriptor <rpxd>
1054 * allocate new directory page extent and
1055 * new index page(s) to cover page split(s)
1057 * allocation hint: ?
1059 n = btstack->nsplit;
1060 pxdlist.maxnpxd = pxdlist.npxd = 0;
1061 xlen = sbi->nbperpage;
1062 for (pxd = pxdlist.pxd; n > 0; n--, pxd++) {
1063 if ((rc = dbAlloc(ip, 0, (s64) xlen, &xaddr)) == 0) {
1064 PXDaddress(pxd, xaddr);
1065 PXDlength(pxd, xlen);
1072 /* undo allocation */
1076 split->pxdlist = &pxdlist;
1077 if ((rc = dtSplitPage(tid, ip, split, &rmp, &rp, &rpxd))) {
1080 /* undo allocation */
1085 * propagate up the router entry for the leaf page just split
1087 * insert a router entry for the new page into the parent page,
1088 * propagate the insert/split up the tree by walking back the stack
1089 * of (bn of parent page, index of child page entry in parent page)
1090 * that were traversed during the search for the page that split.
1092 * the propagation of insert/split up the tree stops if the root
1093 * splits or the page inserted into doesn't have to split to hold
1096 * the parent entry for the split page remains the same, and
1097 * a new entry is inserted at its right with the first key and
1098 * block number of the new right page.
1100 * There are a maximum of 4 pages pinned at any time:
1101 * two children, left parent and right parent (when the parent splits).
1102 * keep the child pages pinned while working on the parent.
1103 * make sure that all pins are released at exit.
1105 while ((parent = BT_POP(btstack)) != NULL) {
1106 /* parent page specified by stack frame <parent> */
1108 /* keep current child pages (<lp>, <rp>) pinned */
1113 * insert router entry in parent for new right child page <rp>
1115 /* get the parent page <sp> */
1116 DT_GETPAGE(ip, parent->bn, smp, PSIZE, sp, rc);
1124 * The new key entry goes ONE AFTER the index of parent entry,
1125 * because the split was to the right.
1127 skip = parent->index + 1;
1130 * compute the key for the router entry
1132 * key suffix compression:
1133 * for internal pages that have leaf pages as children,
1134 * retain only what's needed to distinguish between
1135 * the new entry and the entry on the page to its left.
1136 * If the keys compare equal, retain the entire key.
1138 * note that compression is performed only at computing
1139 * router key at the lowest internal level.
1140 * further compression of the key between pairs of higher
1141 * level internal pages loses too much information and
1142 * the search may fail.
1143 * (e.g., two adjacent leaf pages of {a, ..., x} {xx, ...,}
1144 * results in two adjacent parent entries (a)(xx).
1145 * if split occurs between these two entries, and
1146 * if compression is applied, the router key of parent entry
1147 * of right page (x) will divert search for x into right
1148 * subtree and miss x in the left subtree.)
1150 * the entire key must be retained for the next-to-leftmost
1151 * internal key at any level of the tree, or search may fail
1154 switch (rp->header.flag & BT_TYPE) {
1157 * compute the length of prefix for suffix compression
1158 * between last entry of left page and first entry
1161 if ((sp->header.flag & BT_ROOT && skip > 1) ||
1162 sp->header.prev != 0 || skip > 1) {
1163 /* compute uppercase router prefix key */
1164 rc = ciGetLeafPrefixKey(lp,
1165 lp->header.nextindex-1,
1175 /* next to leftmost entry of
1176 lowest internal level */
1178 /* compute uppercase router key */
1179 dtGetKey(rp, 0, &key, sbi->mntflag);
1180 key.name[key.namlen] = 0;
1182 if ((sbi->mntflag & JFS_OS2) == JFS_OS2)
1186 n = NDTINTERNAL(key.namlen);
1190 dtGetKey(rp, 0, &key, sbi->mntflag);
1191 n = NDTINTERNAL(key.namlen);
1195 jfs_err("dtSplitUp(): UFO!");
1199 /* unpin left child page */
1203 * compute the data for the router entry
1205 data->xd = rpxd; /* child page xd */
1208 * parent page is full - split the parent page
1210 if (n > sp->header.freecnt) {
1211 /* init for parent page split */
1213 split->index = skip; /* index at insert */
1216 /* split->data = data; */
1218 /* unpin right child page */
1221 /* The split routines insert the new entry,
1222 * acquire txLock as appropriate.
1223 * return <rp> pinned and its block number <rbn>.
1225 rc = (sp->header.flag & BT_ROOT) ?
1226 dtSplitRoot(tid, ip, split, &rmp) :
1227 dtSplitPage(tid, ip, split, &rmp, &rp, &rpxd);
1233 /* smp and rmp are pinned */
1236 * parent page is not full - insert router entry in parent page
1239 BT_MARK_DIRTY(smp, ip);
1241 * acquire a transaction lock on the parent page
1243 tlck = txLock(tid, ip, smp, tlckDTREE | tlckENTRY);
1244 dtlck = (struct dt_lock *) & tlck->lock;
1245 ASSERT(dtlck->index == 0);
1246 lv = & dtlck->lv[0];
1248 /* linelock header */
1253 /* linelock stbl of non-root parent page */
1254 if (!(sp->header.flag & BT_ROOT)) {
1256 n = skip >> L2DTSLOTSIZE;
1257 lv->offset = sp->header.stblindex + n;
1259 ((sp->header.nextindex -
1260 1) >> L2DTSLOTSIZE) - n + 1;
1264 dtInsertEntry(sp, skip, &key, data, &dtlck);
1266 /* exit propagate up */
1271 /* unpin current split and its right page */
1276 * free remaining extents allocated for split
1280 pxd = &pxdlist.pxd[n];
1281 for (; n < pxdlist.maxnpxd; n++, pxd++)
1282 dbFree(ip, addressPXD(pxd), (s64) lengthPXD(pxd));
1296 * function: Split a non-root page of a btree.
1300 * return: 0 - success;
1302 * return split and new page pinned;
1304 static int dtSplitPage(tid_t tid, struct inode *ip, struct dtsplit * split,
1305 struct metapage ** rmpp, dtpage_t ** rpp, pxd_t * rpxdp)
1307 struct super_block *sb = ip->i_sb;
1309 struct metapage *smp;
1311 struct metapage *rmp;
1312 dtpage_t *rp; /* new right page allocated */
1313 s64 rbn; /* new right page block number */
1314 struct metapage *mp;
1317 struct pxdlist *pxdlist;
1319 int skip, nextindex, half, left, nxt, off, si;
1320 struct ldtentry *ldtentry;
1321 struct idtentry *idtentry;
1326 struct dt_lock *sdtlck, *rdtlck;
1328 struct dt_lock *dtlck;
1329 struct lv *slv, *rlv, *lv;
1331 /* get split page */
1333 sp = DT_PAGE(ip, smp);
1336 * allocate the new right page for the split
1338 pxdlist = split->pxdlist;
1339 pxd = &pxdlist->pxd[pxdlist->npxd];
1341 rbn = addressPXD(pxd);
1342 rmp = get_metapage(ip, rbn, PSIZE, 1);
1346 jfs_info("dtSplitPage: ip:0x%p smp:0x%p rmp:0x%p", ip, smp, rmp);
1348 BT_MARK_DIRTY(rmp, ip);
1350 * acquire a transaction lock on the new right page
1352 tlck = txLock(tid, ip, rmp, tlckDTREE | tlckNEW);
1353 rdtlck = (struct dt_lock *) & tlck->lock;
1355 rp = (dtpage_t *) rmp->data;
1357 rp->header.self = *pxd;
1359 BT_MARK_DIRTY(smp, ip);
1361 * acquire a transaction lock on the split page
1365 tlck = txLock(tid, ip, smp, tlckDTREE | tlckENTRY);
1366 sdtlck = (struct dt_lock *) & tlck->lock;
1368 /* linelock header of split page */
1369 ASSERT(sdtlck->index == 0);
1370 slv = & sdtlck->lv[0];
1376 * initialize/update sibling pointers between sp and rp
1378 nextbn = le64_to_cpu(sp->header.next);
1379 rp->header.next = cpu_to_le64(nextbn);
1380 rp->header.prev = cpu_to_le64(addressPXD(&sp->header.self));
1381 sp->header.next = cpu_to_le64(rbn);
1384 * initialize new right page
1386 rp->header.flag = sp->header.flag;
1388 /* compute sorted entry table at start of extent data area */
1389 rp->header.nextindex = 0;
1390 rp->header.stblindex = 1;
1392 n = PSIZE >> L2DTSLOTSIZE;
1393 rp->header.maxslot = n;
1394 stblsize = (n + 31) >> L2DTSLOTSIZE; /* in unit of slot */
1397 fsi = rp->header.stblindex + stblsize;
1398 rp->header.freelist = fsi;
1399 rp->header.freecnt = rp->header.maxslot - fsi;
1402 * sequential append at tail: append without split
1404 * If splitting the last page on a level because of appending
1405 * a entry to it (skip is maxentry), it's likely that the access is
1406 * sequential. Adding an empty page on the side of the level is less
1407 * work and can push the fill factor much higher than normal.
1408 * If we're wrong it's no big deal, we'll just do the split the right
1410 * (It may look like it's equally easy to do a similar hack for
1411 * reverse sorted data, that is, split the tree left,
1412 * but it's not. Be my guest.)
1414 if (nextbn == 0 && split->index == sp->header.nextindex) {
1415 /* linelock header + stbl (first slot) of new page */
1416 rlv = & rdtlck->lv[rdtlck->index];
1422 * initialize freelist of new right page
1425 for (fsi++; fsi < rp->header.maxslot; f++, fsi++)
1429 /* insert entry at the first entry of the new right page */
1430 dtInsertEntry(rp, 0, split->key, split->data, &rdtlck);
1436 * non-sequential insert (at possibly middle page)
1440 * update prev pointer of previous right sibling page;
1443 DT_GETPAGE(ip, nextbn, mp, PSIZE, p, rc);
1445 discard_metapage(rmp);
1449 BT_MARK_DIRTY(mp, ip);
1451 * acquire a transaction lock on the next page
1453 tlck = txLock(tid, ip, mp, tlckDTREE | tlckRELINK);
1454 jfs_info("dtSplitPage: tlck = 0x%p, ip = 0x%p, mp=0x%p",
1456 dtlck = (struct dt_lock *) & tlck->lock;
1458 /* linelock header of previous right sibling page */
1459 lv = & dtlck->lv[dtlck->index];
1464 p->header.prev = cpu_to_le64(rbn);
1470 * split the data between the split and right pages.
1472 skip = split->index;
1473 half = (PSIZE >> L2DTSLOTSIZE) >> 1; /* swag */
1477 * compute fill factor for split pages
1479 * <nxt> traces the next entry to move to rp
1480 * <off> traces the next entry to stay in sp
1482 stbl = (u8 *) & sp->slot[sp->header.stblindex];
1483 nextindex = sp->header.nextindex;
1484 for (nxt = off = 0; nxt < nextindex; ++off) {
1486 /* check for fill factor with new entry size */
1490 switch (sp->header.flag & BT_TYPE) {
1492 ldtentry = (struct ldtentry *) & sp->slot[si];
1494 n = NDTLEAF(ldtentry->namlen);
1496 n = NDTLEAF_LEGACY(ldtentry->
1501 idtentry = (struct idtentry *) & sp->slot[si];
1502 n = NDTINTERNAL(idtentry->namlen);
1509 ++nxt; /* advance to next entry to move in sp */
1517 /* <nxt> poins to the 1st entry to move */
1520 * move entries to right page
1522 * dtMoveEntry() initializes rp and reserves entry for insertion
1524 * split page moved out entries are linelocked;
1525 * new/right page moved in entries are linelocked;
1527 /* linelock header + stbl of new right page */
1528 rlv = & rdtlck->lv[rdtlck->index];
1533 dtMoveEntry(sp, nxt, rp, &sdtlck, &rdtlck, DO_INDEX(ip));
1535 sp->header.nextindex = nxt;
1538 * finalize freelist of new right page
1540 fsi = rp->header.freelist;
1542 for (fsi++; fsi < rp->header.maxslot; f++, fsi++)
1547 * Update directory index table for entries now in right page
1549 if ((rp->header.flag & BT_LEAF) && DO_INDEX(ip)) {
1553 stbl = DT_GETSTBL(rp);
1554 for (n = 0; n < rp->header.nextindex; n++) {
1555 ldtentry = (struct ldtentry *) & rp->slot[stbl[n]];
1556 modify_index(tid, ip, le32_to_cpu(ldtentry->index),
1557 rbn, n, &mp, &lblock);
1560 release_metapage(mp);
1564 * the skipped index was on the left page,
1567 /* insert the new entry in the split page */
1568 dtInsertEntry(sp, skip, split->key, split->data, &sdtlck);
1570 /* linelock stbl of split page */
1571 if (sdtlck->index >= sdtlck->maxcnt)
1572 sdtlck = (struct dt_lock *) txLinelock(sdtlck);
1573 slv = & sdtlck->lv[sdtlck->index];
1574 n = skip >> L2DTSLOTSIZE;
1575 slv->offset = sp->header.stblindex + n;
1577 ((sp->header.nextindex - 1) >> L2DTSLOTSIZE) - n + 1;
1581 * the skipped index was on the right page,
1584 /* adjust the skip index to reflect the new position */
1587 /* insert the new entry in the right page */
1588 dtInsertEntry(rp, skip, split->key, split->data, &rdtlck);
1595 ip->i_blocks += LBLK2PBLK(sb, lengthPXD(pxd));
1604 * function: extend 1st/only directory leaf page
1608 * return: 0 - success;
1610 * return extended page pinned;
1612 static int dtExtendPage(tid_t tid,
1613 struct inode *ip, struct dtsplit * split, struct btstack * btstack)
1615 struct super_block *sb = ip->i_sb;
1617 struct metapage *smp, *pmp, *mp;
1619 struct pxdlist *pxdlist;
1622 int newstblindex, newstblsize;
1623 int oldstblindex, oldstblsize;
1626 struct btframe *parent;
1628 struct dt_lock *dtlck;
1631 struct pxd_lock *pxdlock;
1634 struct ldtentry *ldtentry;
1637 /* get page to extend */
1639 sp = DT_PAGE(ip, smp);
1641 /* get parent/root page */
1642 parent = BT_POP(btstack);
1643 DT_GETPAGE(ip, parent->bn, pmp, PSIZE, pp, rc);
1650 pxdlist = split->pxdlist;
1651 pxd = &pxdlist->pxd[pxdlist->npxd];
1654 xaddr = addressPXD(pxd);
1655 tpxd = &sp->header.self;
1656 txaddr = addressPXD(tpxd);
1657 /* in-place extension */
1658 if (xaddr == txaddr) {
1665 /* save moved extent descriptor for later free */
1666 tlck = txMaplock(tid, ip, tlckDTREE | tlckRELOCATE);
1667 pxdlock = (struct pxd_lock *) & tlck->lock;
1668 pxdlock->flag = mlckFREEPXD;
1669 pxdlock->pxd = sp->header.self;
1673 * Update directory index table to reflect new page address
1679 stbl = DT_GETSTBL(sp);
1680 for (n = 0; n < sp->header.nextindex; n++) {
1682 (struct ldtentry *) & sp->slot[stbl[n]];
1683 modify_index(tid, ip,
1684 le32_to_cpu(ldtentry->index),
1685 xaddr, n, &mp, &lblock);
1688 release_metapage(mp);
1695 sp->header.self = *pxd;
1697 jfs_info("dtExtendPage: ip:0x%p smp:0x%p sp:0x%p", ip, smp, sp);
1699 BT_MARK_DIRTY(smp, ip);
1701 * acquire a transaction lock on the extended/leaf page
1703 tlck = txLock(tid, ip, smp, tlckDTREE | type);
1704 dtlck = (struct dt_lock *) & tlck->lock;
1705 lv = & dtlck->lv[0];
1707 /* update buffer extent descriptor of extended page */
1708 xlen = lengthPXD(pxd);
1709 xsize = xlen << JFS_SBI(sb)->l2bsize;
1710 #ifdef _STILL_TO_PORT
1711 bmSetXD(smp, xaddr, xsize);
1712 #endif /* _STILL_TO_PORT */
1715 * copy old stbl to new stbl at start of extended area
1717 oldstblindex = sp->header.stblindex;
1718 oldstblsize = (sp->header.maxslot + 31) >> L2DTSLOTSIZE;
1719 newstblindex = sp->header.maxslot;
1720 n = xsize >> L2DTSLOTSIZE;
1721 newstblsize = (n + 31) >> L2DTSLOTSIZE;
1722 memcpy(&sp->slot[newstblindex], &sp->slot[oldstblindex],
1723 sp->header.nextindex);
1726 * in-line extension: linelock old area of extended page
1728 if (type == tlckEXTEND) {
1729 /* linelock header */
1735 /* linelock new stbl of extended page */
1736 lv->offset = newstblindex;
1737 lv->length = newstblsize;
1740 * relocation: linelock whole relocated area
1744 lv->length = sp->header.maxslot + newstblsize;
1749 sp->header.maxslot = n;
1750 sp->header.stblindex = newstblindex;
1751 /* sp->header.nextindex remains the same */
1754 * add old stbl region at head of freelist
1758 last = sp->header.freelist;
1759 for (n = 0; n < oldstblsize; n++, fsi++, f++) {
1763 sp->header.freelist = last;
1764 sp->header.freecnt += oldstblsize;
1767 * append free region of newly extended area at tail of freelist
1769 /* init free region of newly extended area */
1770 fsi = n = newstblindex + newstblsize;
1772 for (fsi++; fsi < sp->header.maxslot; f++, fsi++)
1776 /* append new free region at tail of old freelist */
1777 fsi = sp->header.freelist;
1779 sp->header.freelist = n;
1784 } while (fsi != -1);
1789 sp->header.freecnt += sp->header.maxslot - n;
1792 * insert the new entry
1794 dtInsertEntry(sp, split->index, split->key, split->data, &dtlck);
1796 BT_MARK_DIRTY(pmp, ip);
1798 * linelock any freeslots residing in old extent
1800 if (type == tlckEXTEND) {
1801 n = sp->header.maxslot >> 2;
1802 if (sp->header.freelist < n)
1803 dtLinelockFreelist(sp, n, &dtlck);
1807 * update parent entry on the parent/root page
1810 * acquire a transaction lock on the parent/root page
1812 tlck = txLock(tid, ip, pmp, tlckDTREE | tlckENTRY);
1813 dtlck = (struct dt_lock *) & tlck->lock;
1814 lv = & dtlck->lv[dtlck->index];
1816 /* linelock parent entry - 1st slot */
1821 /* update the parent pxd for page extension */
1822 tpxd = (pxd_t *) & pp->slot[1];
1825 /* Since the directory might have an EA and/or ACL associated with it
1826 * we need to make sure we take that into account when setting the
1829 ip->i_blocks = LBLK2PBLK(ip->i_sb, xlen +
1830 ((JFS_IP(ip)->ea.flag & DXD_EXTENT) ?
1831 lengthDXD(&JFS_IP(ip)->ea) : 0) +
1832 ((JFS_IP(ip)->acl.flag & DXD_EXTENT) ?
1833 lengthDXD(&JFS_IP(ip)->acl) : 0));
1844 * split the full root page into
1845 * original/root/split page and new right page
1846 * i.e., root remains fixed in tree anchor (inode) and
1847 * the root is copied to a single new right child page
1848 * since root page << non-root page, and
1849 * the split root page contains a single entry for the
1850 * new right child page.
1854 * return: 0 - success;
1856 * return new page pinned;
1858 static int dtSplitRoot(tid_t tid,
1859 struct inode *ip, struct dtsplit * split, struct metapage ** rmpp)
1861 struct super_block *sb = ip->i_sb;
1862 struct metapage *smp;
1864 struct metapage *rmp;
1871 int fsi, stblsize, n;
1874 struct pxdlist *pxdlist;
1876 struct dt_lock *dtlck;
1880 /* get split root page */
1882 sp = &JFS_IP(ip)->i_dtroot;
1885 * allocate/initialize a single (right) child page
1887 * N.B. at first split, a one (or two) block to fit new entry
1888 * is allocated; at subsequent split, a full page is allocated;
1890 pxdlist = split->pxdlist;
1891 pxd = &pxdlist->pxd[pxdlist->npxd];
1893 rbn = addressPXD(pxd);
1894 xlen = lengthPXD(pxd);
1895 xsize = xlen << JFS_SBI(sb)->l2bsize;
1896 rmp = get_metapage(ip, rbn, xsize, 1);
1902 BT_MARK_DIRTY(rmp, ip);
1904 * acquire a transaction lock on the new right page
1906 tlck = txLock(tid, ip, rmp, tlckDTREE | tlckNEW);
1907 dtlck = (struct dt_lock *) & tlck->lock;
1910 (sp->header.flag & BT_LEAF) ? BT_LEAF : BT_INTERNAL;
1911 rp->header.self = *pxd;
1913 /* initialize sibling pointers */
1914 rp->header.next = 0;
1915 rp->header.prev = 0;
1918 * move in-line root page into new right page extent
1920 /* linelock header + copied entries + new stbl (1st slot) in new page */
1921 ASSERT(dtlck->index == 0);
1922 lv = & dtlck->lv[0];
1924 lv->length = 10; /* 1 + 8 + 1 */
1927 n = xsize >> L2DTSLOTSIZE;
1928 rp->header.maxslot = n;
1929 stblsize = (n + 31) >> L2DTSLOTSIZE;
1931 /* copy old stbl to new stbl at start of extended area */
1932 rp->header.stblindex = DTROOTMAXSLOT;
1933 stbl = (s8 *) & rp->slot[DTROOTMAXSLOT];
1934 memcpy(stbl, sp->header.stbl, sp->header.nextindex);
1935 rp->header.nextindex = sp->header.nextindex;
1937 /* copy old data area to start of new data area */
1938 memcpy(&rp->slot[1], &sp->slot[1], IDATASIZE);
1941 * append free region of newly extended area at tail of freelist
1943 /* init free region of newly extended area */
1944 fsi = n = DTROOTMAXSLOT + stblsize;
1946 for (fsi++; fsi < rp->header.maxslot; f++, fsi++)
1950 /* append new free region at tail of old freelist */
1951 fsi = sp->header.freelist;
1953 rp->header.freelist = n;
1955 rp->header.freelist = fsi;
1960 } while (fsi != -1);
1965 rp->header.freecnt = sp->header.freecnt + rp->header.maxslot - n;
1968 * Update directory index table for entries now in right page
1970 if ((rp->header.flag & BT_LEAF) && DO_INDEX(ip)) {
1972 struct metapage *mp = 0;
1973 struct ldtentry *ldtentry;
1975 stbl = DT_GETSTBL(rp);
1976 for (n = 0; n < rp->header.nextindex; n++) {
1977 ldtentry = (struct ldtentry *) & rp->slot[stbl[n]];
1978 modify_index(tid, ip, le32_to_cpu(ldtentry->index),
1979 rbn, n, &mp, &lblock);
1982 release_metapage(mp);
1985 * insert the new entry into the new right/child page
1986 * (skip index in the new right page will not change)
1988 dtInsertEntry(rp, split->index, split->key, split->data, &dtlck);
1991 * reset parent/root page
1993 * set the 1st entry offset to 0, which force the left-most key
1994 * at any level of the tree to be less than any search key.
1996 * The btree comparison code guarantees that the left-most key on any
1997 * level of the tree is never used, so it doesn't need to be filled in.
1999 BT_MARK_DIRTY(smp, ip);
2001 * acquire a transaction lock on the root page (in-memory inode)
2003 tlck = txLock(tid, ip, smp, tlckDTREE | tlckNEW | tlckBTROOT);
2004 dtlck = (struct dt_lock *) & tlck->lock;
2007 ASSERT(dtlck->index == 0);
2008 lv = & dtlck->lv[0];
2010 lv->length = DTROOTMAXSLOT;
2013 /* update page header of root */
2014 if (sp->header.flag & BT_LEAF) {
2015 sp->header.flag &= ~BT_LEAF;
2016 sp->header.flag |= BT_INTERNAL;
2019 /* init the first entry */
2020 s = (struct idtentry *) & sp->slot[DTENTRYSTART];
2026 stbl = sp->header.stbl;
2027 stbl[0] = DTENTRYSTART;
2028 sp->header.nextindex = 1;
2031 fsi = DTENTRYSTART + 1;
2034 /* init free region of remaining area */
2035 for (fsi++; fsi < DTROOTMAXSLOT; f++, fsi++)
2039 sp->header.freelist = DTENTRYSTART + 1;
2040 sp->header.freecnt = DTROOTMAXSLOT - (DTENTRYSTART + 1);
2044 ip->i_blocks += LBLK2PBLK(ip->i_sb, lengthPXD(pxd));
2052 * function: delete the entry(s) referenced by a key.
2058 int dtDelete(tid_t tid,
2059 struct inode *ip, struct component_name * key, ino_t * ino, int flag)
2063 struct metapage *mp, *imp;
2066 struct btstack btstack;
2067 struct dt_lock *dtlck;
2071 struct ldtentry *ldtentry;
2073 u32 table_index, next_index;
2074 struct metapage *nmp;
2078 * search for the entry to delete:
2080 * dtSearch() returns (leaf page pinned, index at which to delete).
2082 if ((rc = dtSearch(ip, key, ino, &btstack, flag)))
2085 /* retrieve search result */
2086 DT_GETSEARCH(ip, btstack.top, bn, mp, p, index);
2089 * We need to find put the index of the next entry into the
2090 * directory index table in order to resume a readdir from this
2094 stbl = DT_GETSTBL(p);
2095 ldtentry = (struct ldtentry *) & p->slot[stbl[index]];
2096 table_index = le32_to_cpu(ldtentry->index);
2097 if (index == (p->header.nextindex - 1)) {
2099 * Last entry in this leaf page
2101 if ((p->header.flag & BT_ROOT)
2102 || (p->header.next == 0))
2105 /* Read next leaf page */
2106 DT_GETPAGE(ip, le64_to_cpu(p->header.next),
2107 nmp, PSIZE, np, rc);
2111 stbl = DT_GETSTBL(np);
2113 (struct ldtentry *) & np->
2116 le32_to_cpu(ldtentry->index);
2122 (struct ldtentry *) & p->slot[stbl[index + 1]];
2123 next_index = le32_to_cpu(ldtentry->index);
2125 free_index(tid, ip, table_index, next_index);
2128 * the leaf page becomes empty, delete the page
2130 if (p->header.nextindex == 1) {
2131 /* delete empty page */
2132 rc = dtDeleteUp(tid, ip, mp, p, &btstack);
2135 * the leaf page has other entries remaining:
2137 * delete the entry from the leaf page.
2140 BT_MARK_DIRTY(mp, ip);
2142 * acquire a transaction lock on the leaf page
2144 tlck = txLock(tid, ip, mp, tlckDTREE | tlckENTRY);
2145 dtlck = (struct dt_lock *) & tlck->lock;
2148 * Do not assume that dtlck->index will be zero. During a
2149 * rename within a directory, this transaction may have
2150 * modified this page already when adding the new entry.
2153 /* linelock header */
2154 if (dtlck->index >= dtlck->maxcnt)
2155 dtlck = (struct dt_lock *) txLinelock(dtlck);
2156 lv = & dtlck->lv[dtlck->index];
2161 /* linelock stbl of non-root leaf page */
2162 if (!(p->header.flag & BT_ROOT)) {
2163 if (dtlck->index >= dtlck->maxcnt)
2164 dtlck = (struct dt_lock *) txLinelock(dtlck);
2165 lv = & dtlck->lv[dtlck->index];
2166 i = index >> L2DTSLOTSIZE;
2167 lv->offset = p->header.stblindex + i;
2169 ((p->header.nextindex - 1) >> L2DTSLOTSIZE) -
2174 /* free the leaf entry */
2175 dtDeleteEntry(p, index, &dtlck);
2178 * Update directory index table for entries moved in stbl
2180 if (DO_INDEX(ip) && index < p->header.nextindex) {
2184 stbl = DT_GETSTBL(p);
2185 for (i = index; i < p->header.nextindex; i++) {
2187 (struct ldtentry *) & p->slot[stbl[i]];
2188 modify_index(tid, ip,
2189 le32_to_cpu(ldtentry->index),
2190 bn, i, &imp, &lblock);
2193 release_metapage(imp);
2207 * free empty pages as propagating deletion up the tree
2213 static int dtDeleteUp(tid_t tid, struct inode *ip,
2214 struct metapage * fmp, dtpage_t * fp, struct btstack * btstack)
2217 struct metapage *mp;
2219 int index, nextindex;
2221 struct btframe *parent;
2222 struct dt_lock *dtlck;
2225 struct pxd_lock *pxdlock;
2229 * keep the root leaf page which has become empty
2231 if (BT_IS_ROOT(fmp)) {
2235 * dtInitRoot() acquires txlock on the root
2237 dtInitRoot(tid, ip, PARENT(ip));
2245 * free the non-root leaf page
2248 * acquire a transaction lock on the page
2250 * write FREEXTENT|NOREDOPAGE log record
2251 * N.B. linelock is overlaid as freed extent descriptor, and
2252 * the buffer page is freed;
2254 tlck = txMaplock(tid, ip, tlckDTREE | tlckFREE);
2255 pxdlock = (struct pxd_lock *) & tlck->lock;
2256 pxdlock->flag = mlckFREEPXD;
2257 pxdlock->pxd = fp->header.self;
2260 /* update sibling pointers */
2261 if ((rc = dtRelink(tid, ip, fp))) {
2266 xlen = lengthPXD(&fp->header.self);
2267 ip->i_blocks -= LBLK2PBLK(ip->i_sb, xlen);
2269 /* free/invalidate its buffer page */
2270 discard_metapage(fmp);
2273 * propagate page deletion up the directory tree
2275 * If the delete from the parent page makes it empty,
2276 * continue all the way up the tree.
2277 * stop if the root page is reached (which is never deleted) or
2278 * if the entry deletion does not empty the page.
2280 while ((parent = BT_POP(btstack)) != NULL) {
2281 /* pin the parent page <sp> */
2282 DT_GETPAGE(ip, parent->bn, mp, PSIZE, p, rc);
2287 * free the extent of the child page deleted
2289 index = parent->index;
2292 * delete the entry for the child page from parent
2294 nextindex = p->header.nextindex;
2297 * the parent has the single entry being deleted:
2299 * free the parent page which has become empty.
2301 if (nextindex == 1) {
2303 * keep the root internal page which has become empty
2305 if (p->header.flag & BT_ROOT) {
2309 * dtInitRoot() acquires txlock on the root
2311 dtInitRoot(tid, ip, PARENT(ip));
2318 * free the parent page
2322 * acquire a transaction lock on the page
2324 * write FREEXTENT|NOREDOPAGE log record
2328 tlckDTREE | tlckFREE);
2329 pxdlock = (struct pxd_lock *) & tlck->lock;
2330 pxdlock->flag = mlckFREEPXD;
2331 pxdlock->pxd = p->header.self;
2334 /* update sibling pointers */
2335 if ((rc = dtRelink(tid, ip, p))) {
2340 xlen = lengthPXD(&p->header.self);
2341 ip->i_blocks -= LBLK2PBLK(ip->i_sb, xlen);
2343 /* free/invalidate its buffer page */
2344 discard_metapage(mp);
2352 * the parent has other entries remaining:
2354 * delete the router entry from the parent page.
2356 BT_MARK_DIRTY(mp, ip);
2358 * acquire a transaction lock on the page
2360 * action: router entry deletion
2362 tlck = txLock(tid, ip, mp, tlckDTREE | tlckENTRY);
2363 dtlck = (struct dt_lock *) & tlck->lock;
2365 /* linelock header */
2366 if (dtlck->index >= dtlck->maxcnt)
2367 dtlck = (struct dt_lock *) txLinelock(dtlck);
2368 lv = & dtlck->lv[dtlck->index];
2373 /* linelock stbl of non-root leaf page */
2374 if (!(p->header.flag & BT_ROOT)) {
2375 if (dtlck->index < dtlck->maxcnt)
2378 dtlck = (struct dt_lock *) txLinelock(dtlck);
2379 lv = & dtlck->lv[0];
2381 i = index >> L2DTSLOTSIZE;
2382 lv->offset = p->header.stblindex + i;
2384 ((p->header.nextindex - 1) >> L2DTSLOTSIZE) -
2389 /* free the router entry */
2390 dtDeleteEntry(p, index, &dtlck);
2392 /* reset key of new leftmost entry of level (for consistency) */
2394 ((p->header.flag & BT_ROOT) || p->header.prev == 0))
2395 dtTruncateEntry(p, 0, &dtlck);
2397 /* unpin the parent page */
2400 /* exit propagation up */
2409 * NAME: dtRelocate()
2411 * FUNCTION: relocate dtpage (internal or leaf) of directory;
2412 * This function is mainly used by defragfs utility.
2414 int dtRelocate(tid_t tid, struct inode *ip, s64 lmxaddr, pxd_t * opxd,
2418 struct metapage *mp, *pmp, *lmp, *rmp;
2419 dtpage_t *p, *pp, *rp = 0, *lp= 0;
2422 struct btstack btstack;
2424 s64 oxaddr, nextbn, prevbn;
2427 struct dt_lock *dtlck;
2428 struct pxd_lock *pxdlock;
2432 oxaddr = addressPXD(opxd);
2433 xlen = lengthPXD(opxd);
2435 jfs_info("dtRelocate: lmxaddr:%Ld xaddr:%Ld:%Ld xlen:%d",
2436 (long long)lmxaddr, (long long)oxaddr, (long long)nxaddr,
2440 * 1. get the internal parent dtpage covering
2441 * router entry for the tartget page to be relocated;
2443 rc = dtSearchNode(ip, lmxaddr, opxd, &btstack);
2447 /* retrieve search result */
2448 DT_GETSEARCH(ip, btstack.top, bn, pmp, pp, index);
2449 jfs_info("dtRelocate: parent router entry validated.");
2452 * 2. relocate the target dtpage
2454 /* read in the target page from src extent */
2455 DT_GETPAGE(ip, oxaddr, mp, PSIZE, p, rc);
2457 /* release the pinned parent page */
2463 * read in sibling pages if any to update sibling pointers;
2466 if (p->header.next) {
2467 nextbn = le64_to_cpu(p->header.next);
2468 DT_GETPAGE(ip, nextbn, rmp, PSIZE, rp, rc);
2477 if (p->header.prev) {
2478 prevbn = le64_to_cpu(p->header.prev);
2479 DT_GETPAGE(ip, prevbn, lmp, PSIZE, lp, rc);
2489 /* at this point, all xtpages to be updated are in memory */
2492 * update sibling pointers of sibling dtpages if any;
2495 tlck = txLock(tid, ip, lmp, tlckDTREE | tlckRELINK);
2496 dtlck = (struct dt_lock *) & tlck->lock;
2497 /* linelock header */
2498 ASSERT(dtlck->index == 0);
2499 lv = & dtlck->lv[0];
2504 lp->header.next = cpu_to_le64(nxaddr);
2509 tlck = txLock(tid, ip, rmp, tlckDTREE | tlckRELINK);
2510 dtlck = (struct dt_lock *) & tlck->lock;
2511 /* linelock header */
2512 ASSERT(dtlck->index == 0);
2513 lv = & dtlck->lv[0];
2518 rp->header.prev = cpu_to_le64(nxaddr);
2523 * update the target dtpage to be relocated
2525 * write LOG_REDOPAGE of LOG_NEW type for dst page
2526 * for the whole target page (logredo() will apply
2527 * after image and update bmap for allocation of the
2528 * dst extent), and update bmap for allocation of
2531 tlck = txLock(tid, ip, mp, tlckDTREE | tlckNEW);
2532 dtlck = (struct dt_lock *) & tlck->lock;
2533 /* linelock header */
2534 ASSERT(dtlck->index == 0);
2535 lv = & dtlck->lv[0];
2537 /* update the self address in the dtpage header */
2538 pxd = &p->header.self;
2539 PXDaddress(pxd, nxaddr);
2541 /* the dst page is the same as the src page, i.e.,
2542 * linelock for afterimage of the whole page;
2545 lv->length = p->header.maxslot;
2548 /* update the buffer extent descriptor of the dtpage */
2549 xsize = xlen << JFS_SBI(ip->i_sb)->l2bsize;
2550 #ifdef _STILL_TO_PORT
2551 bmSetXD(mp, nxaddr, xsize);
2552 #endif /* _STILL_TO_PORT */
2553 /* unpin the relocated page */
2555 jfs_info("dtRelocate: target dtpage relocated.");
2557 /* the moved extent is dtpage, then a LOG_NOREDOPAGE log rec
2558 * needs to be written (in logredo(), the LOG_NOREDOPAGE log rec
2559 * will also force a bmap update ).
2563 * 3. acquire maplock for the source extent to be freed;
2565 /* for dtpage relocation, write a LOG_NOREDOPAGE record
2566 * for the source dtpage (logredo() will init NoRedoPage
2567 * filter and will also update bmap for free of the source
2568 * dtpage), and upadte bmap for free of the source dtpage;
2570 tlck = txMaplock(tid, ip, tlckDTREE | tlckFREE);
2571 pxdlock = (struct pxd_lock *) & tlck->lock;
2572 pxdlock->flag = mlckFREEPXD;
2573 PXDaddress(&pxdlock->pxd, oxaddr);
2574 PXDlength(&pxdlock->pxd, xlen);
2578 * 4. update the parent router entry for relocation;
2580 * acquire tlck for the parent entry covering the target dtpage;
2581 * write LOG_REDOPAGE to apply after image only;
2583 jfs_info("dtRelocate: update parent router entry.");
2584 tlck = txLock(tid, ip, pmp, tlckDTREE | tlckENTRY);
2585 dtlck = (struct dt_lock *) & tlck->lock;
2586 lv = & dtlck->lv[dtlck->index];
2588 /* update the PXD with the new address */
2589 stbl = DT_GETSTBL(pp);
2590 pxd = (pxd_t *) & pp->slot[stbl[index]];
2591 PXDaddress(pxd, nxaddr);
2592 lv->offset = stbl[index];
2596 /* unpin the parent dtpage */
2603 * NAME: dtSearchNode()
2605 * FUNCTION: Search for an dtpage containing a specified address
2606 * This function is mainly used by defragfs utility.
2608 * NOTE: Search result on stack, the found page is pinned at exit.
2609 * The result page must be an internal dtpage.
2610 * lmxaddr give the address of the left most page of the
2611 * dtree level, in which the required dtpage resides.
2613 static int dtSearchNode(struct inode *ip, s64 lmxaddr, pxd_t * kpxd,
2614 struct btstack * btstack)
2618 struct metapage *mp;
2620 int psize = 288; /* initial in-line directory */
2624 struct btframe *btsp;
2626 BT_CLR(btstack); /* reset stack */
2629 * descend tree to the level with specified leftmost page
2631 * by convention, root bn = 0.
2634 /* get/pin the page to search */
2635 DT_GETPAGE(ip, bn, mp, psize, p, rc);
2639 /* does the xaddr of leftmost page of the levevl
2640 * matches levevl search key ?
2642 if (p->header.flag & BT_ROOT) {
2645 } else if (addressPXD(&p->header.self) == lmxaddr)
2649 * descend down to leftmost child page
2651 if (p->header.flag & BT_LEAF) {
2656 /* get the leftmost entry */
2657 stbl = DT_GETSTBL(p);
2658 pxd = (pxd_t *) & p->slot[stbl[0]];
2660 /* get the child page block address */
2661 bn = addressPXD(pxd);
2662 psize = lengthPXD(pxd) << JFS_SBI(ip->i_sb)->l2bsize;
2663 /* unpin the parent page */
2668 * search each page at the current levevl
2671 stbl = DT_GETSTBL(p);
2672 for (i = 0; i < p->header.nextindex; i++) {
2673 pxd = (pxd_t *) & p->slot[stbl[i]];
2675 /* found the specified router entry */
2676 if (addressPXD(pxd) == addressPXD(kpxd) &&
2677 lengthPXD(pxd) == lengthPXD(kpxd)) {
2678 btsp = btstack->top;
2687 /* get the right sibling page if any */
2689 bn = le64_to_cpu(p->header.next);
2695 /* unpin current page */
2698 /* get the right sibling page */
2699 DT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
2705 #endif /* _NOTYET */
2711 * link around a freed page.
2714 * fp: page to be freed
2718 static int dtRelink(tid_t tid, struct inode *ip, dtpage_t * p)
2721 struct metapage *mp;
2724 struct dt_lock *dtlck;
2727 nextbn = le64_to_cpu(p->header.next);
2728 prevbn = le64_to_cpu(p->header.prev);
2730 /* update prev pointer of the next page */
2732 DT_GETPAGE(ip, nextbn, mp, PSIZE, p, rc);
2736 BT_MARK_DIRTY(mp, ip);
2738 * acquire a transaction lock on the next page
2740 * action: update prev pointer;
2742 tlck = txLock(tid, ip, mp, tlckDTREE | tlckRELINK);
2743 jfs_info("dtRelink nextbn: tlck = 0x%p, ip = 0x%p, mp=0x%p",
2745 dtlck = (struct dt_lock *) & tlck->lock;
2747 /* linelock header */
2748 if (dtlck->index >= dtlck->maxcnt)
2749 dtlck = (struct dt_lock *) txLinelock(dtlck);
2750 lv = & dtlck->lv[dtlck->index];
2755 p->header.prev = cpu_to_le64(prevbn);
2759 /* update next pointer of the previous page */
2761 DT_GETPAGE(ip, prevbn, mp, PSIZE, p, rc);
2765 BT_MARK_DIRTY(mp, ip);
2767 * acquire a transaction lock on the prev page
2769 * action: update next pointer;
2771 tlck = txLock(tid, ip, mp, tlckDTREE | tlckRELINK);
2772 jfs_info("dtRelink prevbn: tlck = 0x%p, ip = 0x%p, mp=0x%p",
2774 dtlck = (struct dt_lock *) & tlck->lock;
2776 /* linelock header */
2777 if (dtlck->index >= dtlck->maxcnt)
2778 dtlck = (struct dt_lock *) txLinelock(dtlck);
2779 lv = & dtlck->lv[dtlck->index];
2784 p->header.next = cpu_to_le64(nextbn);
2795 * initialize directory root (inline in inode)
2797 void dtInitRoot(tid_t tid, struct inode *ip, u32 idotdot)
2799 struct jfs_inode_info *jfs_ip = JFS_IP(ip);
2804 struct dt_lock *dtlck;
2809 * If this was previously an non-empty directory, we need to remove
2810 * the old directory table.
2813 if (jfs_ip->next_index > (MAX_INLINE_DIRTABLE_ENTRY + 1)) {
2814 struct tblock *tblk = tid_to_tblock(tid);
2816 * We're playing games with the tid's xflag. If
2817 * we're removing a regular file, the file's xtree
2818 * is committed with COMMIT_PMAP, but we always
2819 * commit the directories xtree with COMMIT_PWMAP.
2821 xflag_save = tblk->xflag;
2824 * xtTruncate isn't guaranteed to fully truncate
2825 * the xtree. The caller needs to check i_size
2826 * after committing the transaction to see if
2827 * additional truncation is needed. The
2828 * COMMIT_Stale flag tells caller that we
2829 * initiated the truncation.
2831 xtTruncate(tid, ip, 0, COMMIT_PWMAP);
2832 set_cflag(COMMIT_Stale, ip);
2834 tblk->xflag = xflag_save;
2838 jfs_ip->next_index = 2;
2840 ip->i_size = IDATASIZE;
2843 * acquire a transaction lock on the root
2845 * action: directory initialization;
2847 tlck = txLock(tid, ip, (struct metapage *) & jfs_ip->bxflag,
2848 tlckDTREE | tlckENTRY | tlckBTROOT);
2849 dtlck = (struct dt_lock *) & tlck->lock;
2852 ASSERT(dtlck->index == 0);
2853 lv = & dtlck->lv[0];
2855 lv->length = DTROOTMAXSLOT;
2858 p = &jfs_ip->i_dtroot;
2860 p->header.flag = DXD_INDEX | BT_ROOT | BT_LEAF;
2862 p->header.nextindex = 0;
2868 /* init data area of root */
2869 for (fsi++; fsi < DTROOTMAXSLOT; f++, fsi++)
2873 p->header.freelist = 1;
2874 p->header.freecnt = 8;
2876 /* init '..' entry */
2877 p->header.idotdot = cpu_to_le32(idotdot);
2880 ip->i_blocks = LBLK2PBLK(ip->i_sb,
2881 ((jfs_ip->ea.flag & DXD_EXTENT) ?
2882 lengthDXD(&jfs_ip->ea) : 0) +
2883 ((jfs_ip->acl.flag & DXD_EXTENT) ?
2884 lengthDXD(&jfs_ip->acl) : 0));
2891 * add_missing_indices()
2893 * function: Fix dtree page in which one or more entries has an invalid index.
2894 * fsck.jfs should really fix this, but it currently does not.
2895 * Called from jfs_readdir when bad index is detected.
2897 static void add_missing_indices(struct inode *inode, s64 bn)
2900 struct dt_lock *dtlck;
2904 struct metapage *mp;
2911 tid = txBegin(inode->i_sb, 0);
2913 DT_GETPAGE(inode, bn, mp, PSIZE, p, rc);
2916 printk(KERN_ERR "DT_GETPAGE failed!\n");
2919 BT_MARK_DIRTY(mp, inode);
2921 ASSERT(p->header.flag & BT_LEAF);
2923 tlck = txLock(tid, inode, mp, tlckDTREE | tlckENTRY);
2924 dtlck = (struct dt_lock *) &tlck->lock;
2926 stbl = DT_GETSTBL(p);
2927 for (i = 0; i < p->header.nextindex; i++) {
2928 d = (struct ldtentry *) &p->slot[stbl[i]];
2929 index = le32_to_cpu(d->index);
2930 if ((index < 2) || (index >= JFS_IP(inode)->next_index)) {
2931 d->index = cpu_to_le32(add_index(tid, inode, bn, i));
2932 if (dtlck->index >= dtlck->maxcnt)
2933 dtlck = (struct dt_lock *) txLinelock(dtlck);
2934 lv = &dtlck->lv[dtlck->index];
2935 lv->offset = stbl[i];
2942 (void) txCommit(tid, 1, &inode, 0);
2948 * Buffer to hold directory entry info while traversing a dtree page
2949 * before being fed to the filldir function
2959 * function to determine next variable-sized jfs_dirent in buffer
2961 static inline struct jfs_dirent *next_jfs_dirent(struct jfs_dirent *dirent)
2963 return (struct jfs_dirent *)
2965 ((sizeof (struct jfs_dirent) + dirent->name_len + 1 +
2966 sizeof (loff_t) - 1) &
2967 ~(sizeof (loff_t) - 1)));
2973 * function: read directory entries sequentially
2974 * from the specified entry offset
2978 * return: offset = (pn, index) of start entry
2979 * of next jfs_readdir()/dtRead()
2981 int jfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
2983 struct inode *ip = filp->f_dentry->d_inode;
2984 struct nls_table *codepage = JFS_SBI(ip->i_sb)->nls_tab;
2986 loff_t dtpos; /* legacy OS/2 style position */
2991 } *dtoffset = (struct dtoffset *) &dtpos;
2993 struct metapage *mp;
2997 struct btstack btstack;
3001 int d_namleft, len, outlen;
3002 unsigned long dirent_buf;
3006 uint loop_count = 0;
3007 struct jfs_dirent *jfs_dirent;
3009 int overflow, fix_page, page_fixed = 0;
3010 static int unique_pos = 2; /* If we can't fix broken index */
3012 if (filp->f_pos == DIREND)
3017 * persistent index is stored in directory entries.
3018 * Special cases: 0 = .
3020 * -1 = End of directory
3024 dir_index = (u32) filp->f_pos;
3026 if (dir_index > 1) {
3027 struct dir_table_slot dirtab_slot;
3030 (dir_index >= JFS_IP(ip)->next_index)) {
3031 /* Stale position. Directory has shrunk */
3032 filp->f_pos = DIREND;
3036 rc = read_index(ip, dir_index, &dirtab_slot);
3038 filp->f_pos = DIREND;
3041 if (dirtab_slot.flag == DIR_INDEX_FREE) {
3042 if (loop_count++ > JFS_IP(ip)->next_index) {
3043 jfs_err("jfs_readdir detected "
3045 filp->f_pos = DIREND;
3048 dir_index = le32_to_cpu(dirtab_slot.addr2);
3049 if (dir_index == -1) {
3050 filp->f_pos = DIREND;
3055 bn = addressDTS(&dirtab_slot);
3056 index = dirtab_slot.slot;
3057 DT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
3059 filp->f_pos = DIREND;
3062 if (p->header.flag & BT_INTERNAL) {
3063 jfs_err("jfs_readdir: bad index table");
3069 if (dir_index == 0) {
3074 if (filldir(dirent, ".", 1, 0, ip->i_ino,
3082 if (filldir(dirent, "..", 2, 1, PARENT(ip), DT_DIR))
3086 * Find first entry of left-most leaf
3089 filp->f_pos = DIREND;
3093 if ((rc = dtReadFirst(ip, &btstack)))
3096 DT_GETSEARCH(ip, btstack.top, bn, mp, p, index);
3100 * Legacy filesystem - OS/2 & Linux JFS < 0.3.6
3102 * pn = index = 0: First entry "."
3103 * pn = 0; index = 1: Second entry ".."
3104 * pn > 0: Real entries, pn=1 -> leftmost page
3105 * pn = index = -1: No more entries
3107 dtpos = filp->f_pos;
3109 /* build "." entry */
3111 if (filldir(dirent, ".", 1, filp->f_pos, ip->i_ino,
3114 dtoffset->index = 1;
3115 filp->f_pos = dtpos;
3118 if (dtoffset->pn == 0) {
3119 if (dtoffset->index == 1) {
3120 /* build ".." entry */
3122 if (filldir(dirent, "..", 2, filp->f_pos,
3123 PARENT(ip), DT_DIR))
3126 jfs_err("jfs_readdir called with "
3130 dtoffset->index = 0;
3131 filp->f_pos = dtpos;
3135 filp->f_pos = DIREND;
3139 if ((rc = dtReadNext(ip, &filp->f_pos, &btstack))) {
3140 jfs_err("jfs_readdir: unexpected rc = %d "
3141 "from dtReadNext", rc);
3142 filp->f_pos = DIREND;
3145 /* get start leaf page and index */
3146 DT_GETSEARCH(ip, btstack.top, bn, mp, p, index);
3148 /* offset beyond directory eof ? */
3150 filp->f_pos = DIREND;
3155 dirent_buf = __get_free_page(GFP_KERNEL);
3156 if (dirent_buf == 0) {
3158 jfs_warn("jfs_readdir: __get_free_page failed!");
3159 filp->f_pos = DIREND;
3164 jfs_dirent = (struct jfs_dirent *) dirent_buf;
3166 overflow = fix_page = 0;
3168 stbl = DT_GETSTBL(p);
3170 for (i = index; i < p->header.nextindex; i++) {
3171 d = (struct ldtentry *) & p->slot[stbl[i]];
3173 if (((long) jfs_dirent + d->namlen + 1) >
3174 (dirent_buf + PSIZE)) {
3175 /* DBCS codepages could overrun dirent_buf */
3181 d_namleft = d->namlen;
3182 name_ptr = jfs_dirent->name;
3183 jfs_dirent->ino = le32_to_cpu(d->inumber);
3186 len = min(d_namleft, DTLHDRDATALEN);
3187 jfs_dirent->position = le32_to_cpu(d->index);
3189 * d->index should always be valid, but it
3190 * isn't. fsck.jfs doesn't create the
3191 * directory index for the lost+found
3192 * directory. Rather than let it go,
3193 * we can try to fix it.
3195 if ((jfs_dirent->position < 2) ||
3196 (jfs_dirent->position >=
3197 JFS_IP(ip)->next_index)) {
3198 if (!page_fixed && !isReadOnly(ip)) {
3201 * setting overflow and setting
3202 * index to i will cause the
3203 * same page to be processed
3204 * again starting here
3210 jfs_dirent->position = unique_pos++;
3213 jfs_dirent->position = dtpos;
3214 len = min(d_namleft, DTLHDRDATALEN_LEGACY);
3217 /* copy the name of head/only segment */
3218 outlen = jfs_strfromUCS_le(name_ptr, d->name, len,
3220 jfs_dirent->name_len = outlen;
3222 /* copy name in the additional segment(s) */
3225 t = (struct dtslot *) & p->slot[next];
3229 if (d_namleft == 0) {
3231 "JFS:Dtree error: ino = "
3232 "%ld, bn=%Ld, index = %d",
3238 len = min(d_namleft, DTSLOTDATALEN);
3239 outlen = jfs_strfromUCS_le(name_ptr, t->name,
3241 jfs_dirent->name_len += outlen;
3247 jfs_dirent = next_jfs_dirent(jfs_dirent);
3254 /* Point to next leaf page */
3255 if (p->header.flag & BT_ROOT)
3258 bn = le64_to_cpu(p->header.next);
3260 /* update offset (pn:index) for new page */
3263 dtoffset->index = 0;
3269 /* unpin previous leaf page */
3272 jfs_dirent = (struct jfs_dirent *) dirent_buf;
3273 while (jfs_dirents--) {
3274 filp->f_pos = jfs_dirent->position;
3275 if (filldir(dirent, jfs_dirent->name,
3276 jfs_dirent->name_len, filp->f_pos,
3277 jfs_dirent->ino, DT_UNKNOWN))
3279 jfs_dirent = next_jfs_dirent(jfs_dirent);
3283 add_missing_indices(ip, bn);
3287 if (!overflow && (bn == 0)) {
3288 filp->f_pos = DIREND;
3292 DT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
3294 free_page(dirent_buf);
3300 free_page(dirent_buf);
3309 * function: get the leftmost page of the directory
3311 static int dtReadFirst(struct inode *ip, struct btstack * btstack)
3315 int psize = 288; /* initial in-line directory */
3316 struct metapage *mp;
3319 struct btframe *btsp;
3322 BT_CLR(btstack); /* reset stack */
3325 * descend leftmost path of the tree
3327 * by convention, root bn = 0.
3330 DT_GETPAGE(ip, bn, mp, psize, p, rc);
3335 * leftmost leaf page
3337 if (p->header.flag & BT_LEAF) {
3338 /* return leftmost entry */
3339 btsp = btstack->top;
3348 * descend down to leftmost child page
3350 if (BT_STACK_FULL(btstack)) {
3352 jfs_error(ip->i_sb, "dtReadFirst: btstack overrun");
3353 BT_STACK_DUMP(btstack);
3356 /* push (bn, index) of the parent page/entry */
3357 BT_PUSH(btstack, bn, 0);
3359 /* get the leftmost entry */
3360 stbl = DT_GETSTBL(p);
3361 xd = (pxd_t *) & p->slot[stbl[0]];
3363 /* get the child page block address */
3364 bn = addressPXD(xd);
3365 psize = lengthPXD(xd) << JFS_SBI(ip->i_sb)->l2bsize;
3367 /* unpin the parent page */
3376 * function: get the page of the specified offset (pn:index)
3378 * return: if (offset > eof), bn = -1;
3380 * note: if index > nextindex of the target leaf page,
3381 * start with 1st entry of next leaf page;
3383 static int dtReadNext(struct inode *ip, loff_t * offset,
3384 struct btstack * btstack)
3391 } *dtoffset = (struct dtoffset *) offset;
3393 struct metapage *mp;
3398 struct btframe *btsp, *parent;
3402 * get leftmost leaf page pinned
3404 if ((rc = dtReadFirst(ip, btstack)))
3408 DT_GETSEARCH(ip, btstack->top, bn, mp, p, index);
3410 /* get the start offset (pn:index) */
3411 pn = dtoffset->pn - 1; /* Now pn = 0 represents leftmost leaf */
3412 index = dtoffset->index;
3414 /* start at leftmost page ? */
3416 /* offset beyond eof ? */
3417 if (index < p->header.nextindex)
3420 if (p->header.flag & BT_ROOT) {
3425 /* start with 1st entry of next leaf page */
3427 dtoffset->index = index = 0;
3431 /* start at non-leftmost page: scan parent pages for large pn */
3432 if (p->header.flag & BT_ROOT) {
3437 /* start after next leaf page ? */
3441 /* get leaf page pn = 1 */
3443 bn = le64_to_cpu(p->header.next);
3445 /* unpin leaf page */
3448 /* offset beyond eof ? */
3457 * scan last internal page level to get target leaf page
3460 /* unpin leftmost leaf page */
3463 /* get left most parent page */
3464 btsp = btstack->top;
3467 DT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
3471 /* scan parent pages at last internal page level */
3472 while (pn >= p->header.nextindex) {
3473 pn -= p->header.nextindex;
3475 /* get next parent page address */
3476 bn = le64_to_cpu(p->header.next);
3478 /* unpin current parent page */
3481 /* offset beyond eof ? */
3487 /* get next parent page */
3488 DT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
3492 /* update parent page stack frame */
3496 /* get leaf page address */
3497 stbl = DT_GETSTBL(p);
3498 xd = (pxd_t *) & p->slot[stbl[pn]];
3499 bn = addressPXD(xd);
3501 /* unpin parent page */
3505 * get target leaf page
3508 DT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
3513 * leaf page has been completed:
3514 * start with 1st entry of next leaf page
3516 if (index >= p->header.nextindex) {
3517 bn = le64_to_cpu(p->header.next);
3519 /* unpin leaf page */
3522 /* offset beyond eof ? */
3528 /* get next leaf page */
3529 DT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
3533 /* start with 1st entry of next leaf page */
3535 dtoffset->index = 0;
3539 /* return target leaf page pinned */
3540 btsp = btstack->top;
3542 btsp->index = dtoffset->index;
3552 * function: compare search key with an internal entry
3555 * < 0 if k is < record
3556 * = 0 if k is = record
3557 * > 0 if k is > record
3559 static int dtCompare(struct component_name * key, /* search key */
3560 dtpage_t * p, /* directory page */
3562 { /* entry slot index */
3563 wchar_t *kname, *name;
3564 int klen, namlen, len, rc;
3565 struct idtentry *ih;
3569 * force the left-most key on internal pages, at any level of
3570 * the tree, to be less than any search key.
3571 * this obviates having to update the leftmost key on an internal
3572 * page when the user inserts a new key in the tree smaller than
3573 * anything that has been stored.
3575 * (? if/when dtSearch() narrows down to 1st entry (index = 0),
3576 * at any internal page at any level of the tree,
3577 * it descends to child of the entry anyway -
3578 * ? make the entry as min size dummy entry)
3580 * if (e->index == 0 && h->prevpg == P_INVALID && !(h->flags & BT_LEAF))
3587 ih = (struct idtentry *) & p->slot[si];
3590 namlen = ih->namlen;
3591 len = min(namlen, DTIHDRDATALEN);
3593 /* compare with head/only segment */
3594 len = min(klen, len);
3595 if ((rc = UniStrncmp_le(kname, name, len)))
3601 /* compare with additional segment(s) */
3603 while (klen > 0 && namlen > 0) {
3604 /* compare with next name segment */
3605 t = (struct dtslot *) & p->slot[si];
3606 len = min(namlen, DTSLOTDATALEN);
3607 len = min(klen, len);
3609 if ((rc = UniStrncmp_le(kname, name, len)))
3618 return (klen - namlen);
3627 * function: compare search key with an (leaf/internal) entry
3630 * < 0 if k is < record
3631 * = 0 if k is = record
3632 * > 0 if k is > record
3634 static int ciCompare(struct component_name * key, /* search key */
3635 dtpage_t * p, /* directory page */
3636 int si, /* entry slot index */
3639 wchar_t *kname, *name, x;
3640 int klen, namlen, len, rc;
3641 struct ldtentry *lh;
3642 struct idtentry *ih;
3647 * force the left-most key on internal pages, at any level of
3648 * the tree, to be less than any search key.
3649 * this obviates having to update the leftmost key on an internal
3650 * page when the user inserts a new key in the tree smaller than
3651 * anything that has been stored.
3653 * (? if/when dtSearch() narrows down to 1st entry (index = 0),
3654 * at any internal page at any level of the tree,
3655 * it descends to child of the entry anyway -
3656 * ? make the entry as min size dummy entry)
3658 * if (e->index == 0 && h->prevpg == P_INVALID && !(h->flags & BT_LEAF))
3668 if (p->header.flag & BT_LEAF) {
3669 lh = (struct ldtentry *) & p->slot[si];
3672 namlen = lh->namlen;
3673 if (flag & JFS_DIR_INDEX)
3674 len = min(namlen, DTLHDRDATALEN);
3676 len = min(namlen, DTLHDRDATALEN_LEGACY);
3679 * internal page entry
3682 ih = (struct idtentry *) & p->slot[si];
3685 namlen = ih->namlen;
3686 len = min(namlen, DTIHDRDATALEN);
3689 /* compare with head/only segment */
3690 len = min(klen, len);
3691 for (i = 0; i < len; i++, kname++, name++) {
3692 /* only uppercase if case-insensitive support is on */
3693 if ((flag & JFS_OS2) == JFS_OS2)
3694 x = UniToupper(le16_to_cpu(*name));
3696 x = le16_to_cpu(*name);
3697 if ((rc = *kname - x))
3704 /* compare with additional segment(s) */
3705 while (klen > 0 && namlen > 0) {
3706 /* compare with next name segment */
3707 t = (struct dtslot *) & p->slot[si];
3708 len = min(namlen, DTSLOTDATALEN);
3709 len = min(klen, len);
3711 for (i = 0; i < len; i++, kname++, name++) {
3712 /* only uppercase if case-insensitive support is on */
3713 if ((flag & JFS_OS2) == JFS_OS2)
3714 x = UniToupper(le16_to_cpu(*name));
3716 x = le16_to_cpu(*name);
3718 if ((rc = *kname - x))
3727 return (klen - namlen);
3732 * ciGetLeafPrefixKey()
3734 * function: compute prefix of suffix compression
3735 * from two adjacent leaf entries
3736 * across page boundary
3738 * return: non-zero on error
3741 static int ciGetLeafPrefixKey(dtpage_t * lp, int li, dtpage_t * rp,
3742 int ri, struct component_name * key, int flag)
3745 wchar_t *pl, *pr, *kname;
3746 struct component_name lkey;
3747 struct component_name rkey;
3749 lkey.name = (wchar_t *) kmalloc((JFS_NAME_MAX + 1) * sizeof(wchar_t),
3751 if (lkey.name == NULL)
3754 rkey.name = (wchar_t *) kmalloc((JFS_NAME_MAX + 1) * sizeof(wchar_t),
3756 if (rkey.name == NULL) {
3761 /* get left and right key */
3762 dtGetKey(lp, li, &lkey, flag);
3763 lkey.name[lkey.namlen] = 0;
3765 if ((flag & JFS_OS2) == JFS_OS2)
3768 dtGetKey(rp, ri, &rkey, flag);
3769 rkey.name[rkey.namlen] = 0;
3772 if ((flag & JFS_OS2) == JFS_OS2)
3775 /* compute prefix */
3778 namlen = min(lkey.namlen, rkey.namlen);
3779 for (pl = lkey.name, pr = rkey.name;
3780 namlen; pl++, pr++, namlen--, klen++, kname++) {
3783 key->namlen = klen + 1;
3788 /* l->namlen <= r->namlen since l <= r */
3789 if (lkey.namlen < rkey.namlen) {
3791 key->namlen = klen + 1;
3792 } else /* l->namelen == r->namelen */
3806 * function: get key of the entry
3808 static void dtGetKey(dtpage_t * p, int i, /* entry index */
3809 struct component_name * key, int flag)
3813 struct ldtentry *lh;
3814 struct idtentry *ih;
3817 wchar_t *name, *kname;
3820 stbl = DT_GETSTBL(p);
3822 if (p->header.flag & BT_LEAF) {
3823 lh = (struct ldtentry *) & p->slot[si];
3825 namlen = lh->namlen;
3827 if (flag & JFS_DIR_INDEX)
3828 len = min(namlen, DTLHDRDATALEN);
3830 len = min(namlen, DTLHDRDATALEN_LEGACY);
3832 ih = (struct idtentry *) & p->slot[si];
3834 namlen = ih->namlen;
3836 len = min(namlen, DTIHDRDATALEN);
3839 key->namlen = namlen;
3843 * move head/only segment
3845 UniStrncpy_le(kname, name, len);
3848 * move additional segment(s)
3851 /* get next segment */
3855 len = min(namlen, DTSLOTDATALEN);
3856 UniStrncpy_le(kname, t->name, len);
3866 * function: allocate free slot(s) and
3867 * write a leaf/internal entry
3869 * return: entry slot index
3871 static void dtInsertEntry(dtpage_t * p, int index, struct component_name * key,
3872 ddata_t * data, struct dt_lock ** dtlock)
3874 struct dtslot *h, *t;
3875 struct ldtentry *lh = 0;
3876 struct idtentry *ih = 0;
3877 int hsi, fsi, klen, len, nextindex;
3878 wchar_t *kname, *name;
3881 struct dt_lock *dtlck = *dtlock;
3885 struct metapage *mp = 0;
3890 /* allocate a free slot */
3891 hsi = fsi = p->header.freelist;
3893 p->header.freelist = h->next;
3894 --p->header.freecnt;
3896 /* open new linelock */
3897 if (dtlck->index >= dtlck->maxcnt)
3898 dtlck = (struct dt_lock *) txLinelock(dtlck);
3900 lv = & dtlck->lv[dtlck->index];
3903 /* write head/only segment */
3904 if (p->header.flag & BT_LEAF) {
3905 lh = (struct ldtentry *) h;
3907 lh->inumber = data->leaf.ino; /* little-endian */
3910 if (data->leaf.ip) {
3911 len = min(klen, DTLHDRDATALEN);
3912 if (!(p->header.flag & BT_ROOT))
3913 bn = addressPXD(&p->header.self);
3914 lh->index = cpu_to_le32(add_index(data->leaf.tid,
3918 len = min(klen, DTLHDRDATALEN_LEGACY);
3920 ih = (struct idtentry *) h;
3926 len = min(klen, DTIHDRDATALEN);
3929 UniStrncpy_le(name, kname, len);
3934 /* write additional segment(s) */
3939 fsi = p->header.freelist;
3941 p->header.freelist = t->next;
3942 --p->header.freecnt;
3944 /* is next slot contiguous ? */
3945 if (fsi != xsi + 1) {
3946 /* close current linelock */
3950 /* open new linelock */
3951 if (dtlck->index < dtlck->maxcnt)
3954 dtlck = (struct dt_lock *) txLinelock(dtlck);
3955 lv = & dtlck->lv[0];
3963 len = min(klen, DTSLOTDATALEN);
3964 UniStrncpy_le(t->name, kname, len);
3971 /* close current linelock */
3977 /* terminate last/only segment */
3979 /* single segment entry */
3980 if (p->header.flag & BT_LEAF)
3985 /* multi-segment entry */
3988 /* if insert into middle, shift right succeeding entries in stbl */
3989 stbl = DT_GETSTBL(p);
3990 nextindex = p->header.nextindex;
3991 if (index < nextindex) {
3992 memmove(stbl + index + 1, stbl + index, nextindex - index);
3994 if ((p->header.flag & BT_LEAF) && data->leaf.ip) {
3998 * Need to update slot number for entries that moved
4002 for (n = index + 1; n <= nextindex; n++) {
4003 lh = (struct ldtentry *) & (p->slot[stbl[n]]);
4004 modify_index(data->leaf.tid, data->leaf.ip,
4005 le32_to_cpu(lh->index), bn, n,
4009 release_metapage(mp);
4015 /* advance next available entry index of stbl */
4016 ++p->header.nextindex;
4023 * function: move entries from split/left page to new/right page
4025 * nextindex of dst page and freelist/freecnt of both pages
4028 static void dtMoveEntry(dtpage_t * sp, int si, dtpage_t * dp,
4029 struct dt_lock ** sdtlock, struct dt_lock ** ddtlock,
4032 int ssi, next; /* src slot index */
4033 int di; /* dst entry index */
4034 int dsi; /* dst slot index */
4035 s8 *sstbl, *dstbl; /* sorted entry table */
4037 struct ldtentry *slh, *dlh = 0;
4038 struct idtentry *sih, *dih = 0;
4039 struct dtslot *h, *s, *d;
4040 struct dt_lock *sdtlck = *sdtlock, *ddtlck = *ddtlock;
4041 struct lv *slv, *dlv;
4045 sstbl = (s8 *) & sp->slot[sp->header.stblindex];
4046 dstbl = (s8 *) & dp->slot[dp->header.stblindex];
4048 dsi = dp->header.freelist; /* first (whole page) free slot */
4049 sfsi = sp->header.freelist;
4051 /* linelock destination entry slot */
4052 dlv = & ddtlck->lv[ddtlck->index];
4055 /* linelock source entry slot */
4056 slv = & sdtlck->lv[sdtlck->index];
4057 slv->offset = sstbl[si];
4058 xssi = slv->offset - 1;
4064 for (di = 0; si < sp->header.nextindex; si++, di++) {
4068 /* is next slot contiguous ? */
4069 if (ssi != xssi + 1) {
4070 /* close current linelock */
4074 /* open new linelock */
4075 if (sdtlck->index < sdtlck->maxcnt)
4078 sdtlck = (struct dt_lock *) txLinelock(sdtlck);
4079 slv = & sdtlck->lv[0];
4087 * move head/only segment of an entry
4090 h = d = &dp->slot[dsi];
4092 /* get src slot and move */
4094 if (sp->header.flag & BT_LEAF) {
4095 /* get source entry */
4096 slh = (struct ldtentry *) s;
4097 dlh = (struct ldtentry *) h;
4098 snamlen = slh->namlen;
4101 len = min(snamlen, DTLHDRDATALEN);
4102 dlh->index = slh->index; /* little-endian */
4104 len = min(snamlen, DTLHDRDATALEN_LEGACY);
4106 memcpy(dlh, slh, 6 + len * 2);
4110 /* update dst head/only segment next field */
4114 sih = (struct idtentry *) s;
4115 snamlen = sih->namlen;
4117 len = min(snamlen, DTIHDRDATALEN);
4118 dih = (struct idtentry *) h;
4119 memcpy(dih, sih, 10 + len * 2);
4126 /* free src head/only segment */
4136 * move additional segment(s) of the entry
4139 while ((ssi = next) >= 0) {
4140 /* is next slot contiguous ? */
4141 if (ssi != xssi + 1) {
4142 /* close current linelock */
4146 /* open new linelock */
4147 if (sdtlck->index < sdtlck->maxcnt)
4153 slv = & sdtlck->lv[0];
4160 /* get next source segment */
4163 /* get next destination free slot */
4166 len = min(snamlen, DTSLOTDATALEN);
4167 UniStrncpy(d->name, s->name, len);
4176 /* free source segment */
4185 /* terminate dst last/only segment */
4187 /* single segment entry */
4188 if (dp->header.flag & BT_LEAF)
4193 /* multi-segment entry */
4197 /* close current linelock */
4206 /* update source header */
4207 sp->header.freelist = sfsi;
4208 sp->header.freecnt += nd;
4210 /* update destination header */
4211 dp->header.nextindex = di;
4213 dp->header.freelist = dsi;
4214 dp->header.freecnt -= nd;
4221 * function: free a (leaf/internal) entry
4223 * log freelist header, stbl, and each segment slot of entry
4224 * (even though last/only segment next field is modified,
4225 * physical image logging requires all segment slots of
4226 * the entry logged to avoid applying previous updates
4227 * to the same slots)
4229 static void dtDeleteEntry(dtpage_t * p, int fi, struct dt_lock ** dtlock)
4231 int fsi; /* free entry slot index */
4235 struct dt_lock *dtlck = *dtlock;
4239 /* get free entry slot index */
4240 stbl = DT_GETSTBL(p);
4243 /* open new linelock */
4244 if (dtlck->index >= dtlck->maxcnt)
4245 dtlck = (struct dt_lock *) txLinelock(dtlck);
4246 lv = & dtlck->lv[dtlck->index];
4250 /* get the head/only segment */
4252 if (p->header.flag & BT_LEAF)
4253 si = ((struct ldtentry *) t)->next;
4255 si = ((struct idtentry *) t)->next;
4262 /* find the last/only segment */
4264 /* is next slot contiguous ? */
4265 if (si != xsi + 1) {
4266 /* close current linelock */
4270 /* open new linelock */
4271 if (dtlck->index < dtlck->maxcnt)
4274 dtlck = (struct dt_lock *) txLinelock(dtlck);
4275 lv = & dtlck->lv[0];
4291 /* close current linelock */
4297 /* update freelist */
4298 t->next = p->header.freelist;
4299 p->header.freelist = fsi;
4300 p->header.freecnt += freecnt;
4302 /* if delete from middle,
4303 * shift left the succedding entries in the stbl
4305 si = p->header.nextindex;
4307 memmove(&stbl[fi], &stbl[fi + 1], si - fi - 1);
4309 p->header.nextindex--;
4316 * function: truncate a (leaf/internal) entry
4318 * log freelist header, stbl, and each segment slot of entry
4319 * (even though last/only segment next field is modified,
4320 * physical image logging requires all segment slots of
4321 * the entry logged to avoid applying previous updates
4322 * to the same slots)
4324 static void dtTruncateEntry(dtpage_t * p, int ti, struct dt_lock ** dtlock)
4326 int tsi; /* truncate entry slot index */
4330 struct dt_lock *dtlck = *dtlock;
4334 /* get free entry slot index */
4335 stbl = DT_GETSTBL(p);
4338 /* open new linelock */
4339 if (dtlck->index >= dtlck->maxcnt)
4340 dtlck = (struct dt_lock *) txLinelock(dtlck);
4341 lv = & dtlck->lv[dtlck->index];
4345 /* get the head/only segment */
4347 ASSERT(p->header.flag & BT_INTERNAL);
4348 ((struct idtentry *) t)->namlen = 0;
4349 si = ((struct idtentry *) t)->next;
4350 ((struct idtentry *) t)->next = -1;
4357 /* find the last/only segment */
4359 /* is next slot contiguous ? */
4360 if (si != xsi + 1) {
4361 /* close current linelock */
4365 /* open new linelock */
4366 if (dtlck->index < dtlck->maxcnt)
4369 dtlck = (struct dt_lock *) txLinelock(dtlck);
4370 lv = & dtlck->lv[0];
4386 /* close current linelock */
4392 /* update freelist */
4395 t->next = p->header.freelist;
4396 p->header.freelist = fsi;
4397 p->header.freecnt += freecnt;
4402 * dtLinelockFreelist()
4404 static void dtLinelockFreelist(dtpage_t * p, /* directory page */
4405 int m, /* max slot index */
4406 struct dt_lock ** dtlock)
4408 int fsi; /* free entry slot index */
4411 struct dt_lock *dtlck = *dtlock;
4415 /* get free entry slot index */
4416 fsi = p->header.freelist;
4418 /* open new linelock */
4419 if (dtlck->index >= dtlck->maxcnt)
4420 dtlck = (struct dt_lock *) txLinelock(dtlck);
4421 lv = & dtlck->lv[dtlck->index];
4431 /* find the last/only segment */
4432 while (si < m && si >= 0) {
4433 /* is next slot contiguous ? */
4434 if (si != xsi + 1) {
4435 /* close current linelock */
4439 /* open new linelock */
4440 if (dtlck->index < dtlck->maxcnt)
4443 dtlck = (struct dt_lock *) txLinelock(dtlck);
4444 lv = & dtlck->lv[0];
4458 /* close current linelock */
4469 * FUNCTION: Modify the inode number part of a directory entry
4472 * tid - Transaction id
4473 * ip - Inode of parent directory
4474 * key - Name of entry to be modified
4475 * orig_ino - Original inode number expected in entry
4476 * new_ino - New inode number to put into entry
4480 * -ESTALE - If entry found does not match orig_ino passed in
4481 * -ENOENT - If no entry can be found to match key
4482 * 0 - If successfully modified entry
4484 int dtModify(tid_t tid, struct inode *ip,
4485 struct component_name * key, ino_t * orig_ino, ino_t new_ino, int flag)
4489 struct metapage *mp;
4492 struct btstack btstack;
4494 struct dt_lock *dtlck;
4497 int entry_si; /* entry slot index */
4498 struct ldtentry *entry;
4501 * search for the entry to modify:
4503 * dtSearch() returns (leaf page pinned, index at which to modify).
4505 if ((rc = dtSearch(ip, key, orig_ino, &btstack, flag)))
4508 /* retrieve search result */
4509 DT_GETSEARCH(ip, btstack.top, bn, mp, p, index);
4511 BT_MARK_DIRTY(mp, ip);
4513 * acquire a transaction lock on the leaf page of named entry
4515 tlck = txLock(tid, ip, mp, tlckDTREE | tlckENTRY);
4516 dtlck = (struct dt_lock *) & tlck->lock;
4518 /* get slot index of the entry */
4519 stbl = DT_GETSTBL(p);
4520 entry_si = stbl[index];
4522 /* linelock entry */
4523 ASSERT(dtlck->index == 0);
4524 lv = & dtlck->lv[0];
4525 lv->offset = entry_si;
4529 /* get the head/only segment */
4530 entry = (struct ldtentry *) & p->slot[entry_si];
4532 /* substitute the inode number of the entry */
4533 entry->inumber = cpu_to_le32(new_ino);
4535 /* unpin the leaf page */
4541 #ifdef _JFS_DEBUG_DTREE
4545 * function: traverse forward
4547 int dtDisplayTree(struct inode *ip)
4550 struct metapage *mp;
4553 int index, lastindex, v, h;
4555 struct btstack btstack;
4556 struct btframe *btsp;
4557 struct btframe *parent;
4561 printk("display B+-tree.\n");
4564 btsp = btstack.stack;
4569 * root resides in the inode
4575 * first access of each page:
4578 DT_GETPAGE(ip, bn, mp, psize, p, rc);
4582 /* process entries forward from first index */
4584 lastindex = p->header.nextindex - 1;
4586 if (p->header.flag & BT_INTERNAL) {
4588 * first access of each internal page
4590 printf("internal page ");
4591 dtDisplayPage(ip, bn, p);
4594 } else { /* (p->header.flag & BT_LEAF) */
4597 * first access of each leaf page
4599 printf("leaf page ");
4600 dtDisplayPage(ip, bn, p);
4603 * process leaf page entries
4605 for ( ; index <= lastindex; index++)
4610 /* unpin the leaf page */
4615 * go back up to the parent page
4618 /* pop/restore parent entry for the current child page */
4619 if ((parent = (btsp == btstack.stack ? NULL : --btsp)) == NULL)
4620 /* current page must have been root */
4624 * parent page scan completed
4626 if ((index = parent->index) == (lastindex = parent->lastindex)) {
4627 /* go back up to the parent page */
4632 * parent page has entries remaining
4634 /* get back the parent page */
4636 /* v = parent->level; */
4637 DT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
4641 /* get next parent entry */
4645 * internal page: go down to child page of current entry
4648 /* push/save current parent entry for the child page */
4649 btsp->bn = pbn = bn;
4650 btsp->index = index;
4651 btsp->lastindex = lastindex;
4652 /* btsp->level = v; */
4653 /* btsp->node = h; */
4656 /* get current entry for the child page */
4657 stbl = DT_GETSTBL(p);
4658 xd = (pxd_t *) & p->slot[stbl[index]];
4661 * first access of each internal entry:
4664 /* get child page */
4665 bn = addressPXD(xd);
4666 psize = lengthPXD(xd) << ip->i_ipmnt->i_l2bsize;
4668 printk("traverse down 0x%Lx[%d]->0x%Lx\n", pbn, index, bn);
4672 /* release parent page */
4675 /* process the child page */
4683 * function: display page
4685 int dtDisplayPage(struct inode *ip, s64 bn, dtpage_t * p)
4688 struct metapage *mp;
4689 struct ldtentry *lh;
4690 struct idtentry *ih;
4694 wchar_t name[JFS_NAME_MAX + 1];
4695 struct component_name key = { 0, name };
4700 DT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
4705 /* display page control */
4706 printk("bn:0x%Lx flag:0x%08x nextindex:%d\n",
4707 bn, p->header.flag, p->header.nextindex);
4709 /* display entries */
4710 stbl = DT_GETSTBL(p);
4711 for (i = 0, j = 1; i < p->header.nextindex; i++, j++) {
4712 dtGetKey(p, i, &key, JFS_SBI(ip->i_sb)->mntflag);
4713 key.name[key.namlen] = '\0';
4714 if (p->header.flag & BT_LEAF) {
4715 lh = (struct ldtentry *) & p->slot[stbl[i]];
4716 printf("\t[%d] %s:%d", i, key.name,
4717 le32_to_cpu(lh->inumber));
4719 ih = (struct idtentry *) & p->slot[stbl[i]];
4721 bn = addressPXD(xd);
4722 printf("\t[%d] %s:0x%Lx", i, key.name, bn);
4738 #endif /* _JFS_DEBUG_DTREE */