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 <linux/quotaops.h>
105 #include "jfs_incore.h"
106 #include "jfs_superblock.h"
107 #include "jfs_filsys.h"
108 #include "jfs_metapage.h"
109 #include "jfs_dmap.h"
110 #include "jfs_unicode.h"
111 #include "jfs_debug.h"
113 /* dtree split parameter */
118 struct component_name *key;
120 struct pxdlist *pxdlist;
123 #define DT_PAGE(IP, MP) BT_PAGE(IP, MP, dtpage_t, i_dtroot)
125 /* get page buffer for specified block address */
126 #define DT_GETPAGE(IP, BN, MP, SIZE, P, RC)\
128 BT_GETPAGE(IP, BN, MP, dtpage_t, SIZE, P, RC, i_dtroot)\
131 if (((P)->header.nextindex > (((BN)==0)?DTROOTMAXSLOT:(P)->header.maxslot)) ||\
132 ((BN) && ((P)->header.maxslot > DTPAGEMAXSLOT)))\
135 jfs_error((IP)->i_sb, "DT_GETPAGE: dtree page corrupt");\
142 /* for consistency */
143 #define DT_PUTPAGE(MP) BT_PUTPAGE(MP)
145 #define DT_GETSEARCH(IP, LEAF, BN, MP, P, INDEX) \
146 BT_GETSEARCH(IP, LEAF, BN, MP, dtpage_t, P, INDEX, i_dtroot)
151 static int dtSplitUp(tid_t tid, struct inode *ip,
152 struct dtsplit * split, struct btstack * btstack);
154 static int dtSplitPage(tid_t tid, struct inode *ip, struct dtsplit * split,
155 struct metapage ** rmpp, dtpage_t ** rpp, pxd_t * rxdp);
157 static int dtExtendPage(tid_t tid, struct inode *ip,
158 struct dtsplit * split, struct btstack * btstack);
160 static int dtSplitRoot(tid_t tid, struct inode *ip,
161 struct dtsplit * split, struct metapage ** rmpp);
163 static int dtDeleteUp(tid_t tid, struct inode *ip, struct metapage * fmp,
164 dtpage_t * fp, struct btstack * btstack);
166 static int dtRelink(tid_t tid, struct inode *ip, dtpage_t * p);
168 static int dtReadFirst(struct inode *ip, struct btstack * btstack);
170 static int dtReadNext(struct inode *ip,
171 loff_t * offset, struct btstack * btstack);
173 static int dtCompare(struct component_name * key, dtpage_t * p, int si);
175 static int ciCompare(struct component_name * key, dtpage_t * p, int si,
178 static void dtGetKey(dtpage_t * p, int i, struct component_name * key,
181 static int ciGetLeafPrefixKey(dtpage_t * lp, int li, dtpage_t * rp,
182 int ri, struct component_name * key, int flag);
184 static void dtInsertEntry(dtpage_t * p, int index, struct component_name * key,
185 ddata_t * data, struct dt_lock **);
187 static void dtMoveEntry(dtpage_t * sp, int si, dtpage_t * dp,
188 struct dt_lock ** sdtlock, struct dt_lock ** ddtlock,
191 static void dtDeleteEntry(dtpage_t * p, int fi, struct dt_lock ** dtlock);
193 static void dtTruncateEntry(dtpage_t * p, int ti, struct dt_lock ** dtlock);
195 static void dtLinelockFreelist(dtpage_t * p, int m, struct dt_lock ** dtlock);
197 #define ciToUpper(c) UniStrupr((c)->name)
202 * Reads a page of a directory's index table.
203 * Having metadata mapped into the directory inode's address space
204 * presents a multitude of problems. We avoid this by mapping to
205 * the absolute address space outside of the *_metapage routines
207 static struct metapage *read_index_page(struct inode *inode, s64 blkno)
214 rc = xtLookup(inode, blkno, 1, &xflag, &xaddr, &xlen, 1);
215 if (rc || (xaddr == 0))
218 return read_metapage(inode, xaddr, PSIZE, 1);
224 * Same as get_index_page(), but get's a new page without reading
226 static struct metapage *get_index_page(struct inode *inode, s64 blkno)
233 rc = xtLookup(inode, blkno, 1, &xflag, &xaddr, &xlen, 1);
234 if (rc || (xaddr == 0))
237 return get_metapage(inode, xaddr, PSIZE, 1);
243 * Returns dtree page containing directory table entry for specified
244 * index and pointer to its entry.
246 * mp must be released by caller.
248 static struct dir_table_slot *find_index(struct inode *ip, u32 index,
249 struct metapage ** mp, s64 *lblock)
251 struct jfs_inode_info *jfs_ip = JFS_IP(ip);
255 struct dir_table_slot *slot;
256 static int maxWarnings = 10;
260 jfs_warn("find_entry called with index = %d", index);
266 if (index >= jfs_ip->next_index) {
267 jfs_warn("find_entry called with index >= next_index");
271 if (jfs_dirtable_inline(ip)) {
273 * Inline directory table
276 slot = &jfs_ip->i_dirtable[index - 2];
278 offset = (index - 2) * sizeof(struct dir_table_slot);
279 page_offset = offset & (PSIZE - 1);
280 blkno = ((offset + 1) >> L2PSIZE) <<
281 JFS_SBI(ip->i_sb)->l2nbperpage;
283 if (*mp && (*lblock != blkno)) {
284 release_metapage(*mp);
289 *mp = read_index_page(ip, blkno);
292 jfs_err("free_index: error reading directory table");
297 (struct dir_table_slot *) ((char *) (*mp)->data +
303 static inline void lock_index(tid_t tid, struct inode *ip, struct metapage * mp,
307 struct linelock *llck;
310 tlck = txLock(tid, ip, mp, tlckDATA);
311 llck = (struct linelock *) tlck->lock;
313 if (llck->index >= llck->maxcnt)
314 llck = txLinelock(llck);
315 lv = &llck->lv[llck->index];
318 * Linelock slot size is twice the size of directory table
319 * slot size. 512 entries per page.
321 lv->offset = ((index - 2) & 511) >> 1;
329 * Adds an entry to the directory index table. This is used to provide
330 * each directory entry with a persistent index in which to resume
331 * directory traversals
333 static u32 add_index(tid_t tid, struct inode *ip, s64 bn, int slot)
335 struct super_block *sb = ip->i_sb;
336 struct jfs_sb_info *sbi = JFS_SBI(sb);
337 struct jfs_inode_info *jfs_ip = JFS_IP(ip);
339 struct dir_table_slot *dirtab_slot;
341 struct linelock *llck;
349 ASSERT(DO_INDEX(ip));
351 if (jfs_ip->next_index < 2) {
352 jfs_warn("add_index: next_index = %d. Resetting!",
354 jfs_ip->next_index = 2;
357 index = jfs_ip->next_index++;
359 if (index <= MAX_INLINE_DIRTABLE_ENTRY) {
361 * i_size reflects size of index table, or 8 bytes per entry.
363 ip->i_size = (loff_t) (index - 1) << 3;
366 * dir table fits inline within inode
368 dirtab_slot = &jfs_ip->i_dirtable[index-2];
369 dirtab_slot->flag = DIR_INDEX_VALID;
370 dirtab_slot->slot = slot;
371 DTSaddress(dirtab_slot, bn);
373 set_cflag(COMMIT_Dirtable, ip);
377 if (index == (MAX_INLINE_DIRTABLE_ENTRY + 1)) {
378 struct dir_table_slot temp_table[12];
381 * It's time to move the inline table to an external
382 * page and begin to build the xtree
384 if (DQUOT_ALLOC_BLOCK(ip, sbi->nbperpage))
386 if (dbAlloc(ip, 0, sbi->nbperpage, &xaddr)) {
387 DQUOT_FREE_BLOCK(ip, sbi->nbperpage);
392 * Save the table, we're going to overwrite it with the
395 memcpy(temp_table, &jfs_ip->i_dirtable, sizeof(temp_table));
398 * Initialize empty x-tree
403 * Add the first block to the xtree
405 if (xtInsert(tid, ip, 0, 0, sbi->nbperpage, &xaddr, 0)) {
406 /* This really shouldn't fail */
407 jfs_warn("add_index: xtInsert failed!");
408 memcpy(&jfs_ip->i_dirtable, temp_table,
409 sizeof (temp_table));
410 dbFree(ip, xaddr, sbi->nbperpage);
411 DQUOT_FREE_BLOCK(ip, sbi->nbperpage);
416 if ((mp = get_index_page(ip, 0)) == 0) {
417 jfs_err("add_index: get_metapage failed!");
418 xtTruncate(tid, ip, 0, COMMIT_PWMAP);
419 memcpy(&jfs_ip->i_dirtable, temp_table,
420 sizeof (temp_table));
423 tlck = txLock(tid, ip, mp, tlckDATA);
424 llck = (struct linelock *) & tlck->lock;
425 ASSERT(llck->index == 0);
429 lv->length = 6; /* tlckDATA slot size is 16 bytes */
432 memcpy(mp->data, temp_table, sizeof(temp_table));
434 mark_metapage_dirty(mp);
435 release_metapage(mp);
438 * Logging is now directed by xtree tlocks
440 clear_cflag(COMMIT_Dirtable, ip);
443 offset = (index - 2) * sizeof(struct dir_table_slot);
444 page_offset = offset & (PSIZE - 1);
445 blkno = ((offset + 1) >> L2PSIZE) << sbi->l2nbperpage;
446 if (page_offset == 0) {
448 * This will be the beginning of a new page
451 if (xtInsert(tid, ip, 0, blkno, sbi->nbperpage, &xaddr, 0)) {
452 jfs_warn("add_index: xtInsert failed!");
457 if ((mp = get_index_page(ip, blkno)))
458 memset(mp->data, 0, PSIZE); /* Just looks better */
460 xtTruncate(tid, ip, offset, COMMIT_PWMAP);
462 mp = read_index_page(ip, blkno);
465 jfs_err("add_index: get/read_metapage failed!");
469 lock_index(tid, ip, mp, index);
472 (struct dir_table_slot *) ((char *) mp->data + page_offset);
473 dirtab_slot->flag = DIR_INDEX_VALID;
474 dirtab_slot->slot = slot;
475 DTSaddress(dirtab_slot, bn);
477 mark_metapage_dirty(mp);
478 release_metapage(mp);
484 jfs_ip->next_index--;
492 * Marks an entry to the directory index table as free.
494 static void free_index(tid_t tid, struct inode *ip, u32 index, u32 next)
496 struct dir_table_slot *dirtab_slot;
498 struct metapage *mp = NULL;
500 dirtab_slot = find_index(ip, index, &mp, &lblock);
502 if (dirtab_slot == 0)
505 dirtab_slot->flag = DIR_INDEX_FREE;
506 dirtab_slot->slot = dirtab_slot->addr1 = 0;
507 dirtab_slot->addr2 = cpu_to_le32(next);
510 lock_index(tid, ip, mp, index);
511 mark_metapage_dirty(mp);
512 release_metapage(mp);
514 set_cflag(COMMIT_Dirtable, ip);
520 * Changes an entry in the directory index table
522 static void modify_index(tid_t tid, struct inode *ip, u32 index, s64 bn,
523 int slot, struct metapage ** mp, u64 *lblock)
525 struct dir_table_slot *dirtab_slot;
527 dirtab_slot = find_index(ip, index, mp, lblock);
529 if (dirtab_slot == 0)
532 DTSaddress(dirtab_slot, bn);
533 dirtab_slot->slot = slot;
536 lock_index(tid, ip, *mp, index);
537 mark_metapage_dirty(*mp);
539 set_cflag(COMMIT_Dirtable, ip);
545 * reads a directory table slot
547 static int read_index(struct inode *ip, u32 index,
548 struct dir_table_slot * dirtab_slot)
551 struct metapage *mp = NULL;
552 struct dir_table_slot *slot;
554 slot = find_index(ip, index, &mp, &lblock);
559 memcpy(dirtab_slot, slot, sizeof(struct dir_table_slot));
562 release_metapage(mp);
571 * Search for the entry with specified key
575 * return: 0 - search result on stack, leaf page pinned;
578 int dtSearch(struct inode *ip, struct component_name * key, ino_t * data,
579 struct btstack * btstack, int flag)
582 int cmp = 1; /* init for empty page */
587 int base, index, lim;
588 struct btframe *btsp;
590 int psize = 288; /* initial in-line directory */
592 struct component_name ciKey;
593 struct super_block *sb = ip->i_sb;
596 (wchar_t *) kmalloc((JFS_NAME_MAX + 1) * sizeof(wchar_t),
598 if (ciKey.name == 0) {
604 /* uppercase search key for c-i directory */
605 UniStrcpy(ciKey.name, key->name);
606 ciKey.namlen = key->namlen;
608 /* only uppercase if case-insensitive support is on */
609 if ((JFS_SBI(sb)->mntflag & JFS_OS2) == JFS_OS2) {
612 BT_CLR(btstack); /* reset stack */
614 /* init level count for max pages to split */
618 * search down tree from root:
620 * between two consecutive entries of <Ki, Pi> and <Kj, Pj> of
621 * internal page, child page Pi contains entry with k, Ki <= K < Kj.
623 * if entry with search key K is not found
624 * internal page search find the entry with largest key Ki
625 * less than K which point to the child page to search;
626 * leaf page search find the entry with smallest key Kj
627 * greater than K so that the returned index is the position of
628 * the entry to be shifted right for insertion of new entry.
629 * for empty tree, search key is greater than any key of the tree.
631 * by convention, root bn = 0.
634 /* get/pin the page to search */
635 DT_GETPAGE(ip, bn, mp, psize, p, rc);
639 /* get sorted entry table of the page */
640 stbl = DT_GETSTBL(p);
643 * binary search with search key K on the current page.
645 for (base = 0, lim = p->header.nextindex; lim; lim >>= 1) {
646 index = base + (lim >> 1);
648 if (p->header.flag & BT_LEAF) {
649 /* uppercase leaf name to compare */
651 ciCompare(&ciKey, p, stbl[index],
652 JFS_SBI(sb)->mntflag);
654 /* router key is in uppercase */
656 cmp = dtCompare(&ciKey, p, stbl[index]);
664 /* search hit - leaf page:
665 * return the entry found
667 if (p->header.flag & BT_LEAF) {
668 inumber = le32_to_cpu(
669 ((struct ldtentry *) & p->slot[stbl[index]])->inumber);
672 * search for JFS_LOOKUP
674 if (flag == JFS_LOOKUP) {
681 * search for JFS_CREATE
683 if (flag == JFS_CREATE) {
690 * search for JFS_REMOVE or JFS_RENAME
692 if ((flag == JFS_REMOVE ||
693 flag == JFS_RENAME) &&
700 * JFS_REMOVE|JFS_FINDDIR|JFS_RENAME
702 /* save search result */
713 /* search hit - internal page:
714 * descend/search its child page
728 * base is the smallest index with key (Kj) greater than
729 * search key (K) and may be zero or (maxindex + 1) index.
732 * search miss - leaf page
734 * return location of entry (base) where new entry with
735 * search key K is to be inserted.
737 if (p->header.flag & BT_LEAF) {
739 * search for JFS_LOOKUP, JFS_REMOVE, or JFS_RENAME
741 if (flag == JFS_LOOKUP || flag == JFS_REMOVE ||
742 flag == JFS_RENAME) {
748 * search for JFS_CREATE|JFS_FINDDIR:
763 * search miss - internal page
765 * if base is non-zero, decrement base by one to get the parent
766 * entry of the child page to search.
768 index = base ? base - 1 : base;
771 * go down to child page
774 /* update max. number of pages to split */
775 if (BT_STACK_FULL(btstack)) {
776 /* Something's corrupted, mark filesytem dirty so
777 * chkdsk will fix it.
779 jfs_error(sb, "stack overrun in dtSearch!");
780 BT_STACK_DUMP(btstack);
786 /* push (bn, index) of the parent page/entry */
787 BT_PUSH(btstack, bn, index);
789 /* get the child page block number */
790 pxd = (pxd_t *) & p->slot[stbl[index]];
791 bn = addressPXD(pxd);
792 psize = lengthPXD(pxd) << JFS_SBI(ip->i_sb)->l2bsize;
794 /* unpin the parent page */
814 * function: insert an entry to directory tree
818 * return: 0 - success;
821 int dtInsert(tid_t tid, struct inode *ip,
822 struct component_name * name, ino_t * fsn, struct btstack * btstack)
825 struct metapage *mp; /* meta-page buffer */
826 dtpage_t *p; /* base B+-tree index page */
829 struct dtsplit split; /* split information */
831 struct dt_lock *dtlck;
837 * retrieve search result
839 * dtSearch() returns (leaf page pinned, index at which to insert).
840 * n.b. dtSearch() may return index of (maxindex + 1) of
843 DT_GETSEARCH(ip, btstack->top, bn, mp, p, index);
846 * insert entry for new key
849 if (JFS_IP(ip)->next_index == DIREND) {
853 n = NDTLEAF(name->namlen);
857 n = NDTLEAF_LEGACY(name->namlen);
858 data.leaf.ip = NULL; /* signifies legacy directory format */
860 data.leaf.ino = *fsn;
863 * leaf page does not have enough room for new entry:
865 * extend/split the leaf page;
867 * dtSplitUp() will insert the entry and unpin the leaf page.
869 if (n > p->header.freecnt) {
875 rc = dtSplitUp(tid, ip, &split, btstack);
880 * leaf page does have enough room for new entry:
882 * insert the new data entry into the leaf page;
884 BT_MARK_DIRTY(mp, ip);
886 * acquire a transaction lock on the leaf page
888 tlck = txLock(tid, ip, mp, tlckDTREE | tlckENTRY);
889 dtlck = (struct dt_lock *) & tlck->lock;
890 ASSERT(dtlck->index == 0);
893 /* linelock header */
898 dtInsertEntry(p, index, name, &data, &dtlck);
900 /* linelock stbl of non-root leaf page */
901 if (!(p->header.flag & BT_ROOT)) {
902 if (dtlck->index >= dtlck->maxcnt)
903 dtlck = (struct dt_lock *) txLinelock(dtlck);
904 lv = & dtlck->lv[dtlck->index];
905 n = index >> L2DTSLOTSIZE;
906 lv->offset = p->header.stblindex + n;
908 ((p->header.nextindex - 1) >> L2DTSLOTSIZE) - n + 1;
912 /* unpin the leaf page */
922 * function: propagate insertion bottom up;
926 * return: 0 - success;
928 * leaf page unpinned;
930 static int dtSplitUp(tid_t tid,
931 struct inode *ip, struct dtsplit * split, struct btstack * btstack)
933 struct jfs_sb_info *sbi = JFS_SBI(ip->i_sb);
935 struct metapage *smp;
936 dtpage_t *sp; /* split page */
937 struct metapage *rmp;
938 dtpage_t *rp; /* new right page split from sp */
939 pxd_t rpxd; /* new right page extent descriptor */
940 struct metapage *lmp;
941 dtpage_t *lp; /* left child page */
942 int skip; /* index of entry of insertion */
943 struct btframe *parent; /* parent page entry on traverse stack */
946 struct pxdlist pxdlist;
948 struct component_name key = { 0, NULL };
949 ddata_t *data = split->data;
951 struct dt_lock *dtlck;
954 int quota_allocation = 0;
958 sp = DT_PAGE(ip, smp);
961 (wchar_t *) kmalloc((JFS_NAME_MAX + 2) * sizeof(wchar_t),
972 * The split routines insert the new entry, and
973 * acquire txLock as appropriate.
976 * split root leaf page:
978 if (sp->header.flag & BT_ROOT) {
980 * allocate a single extent child page
983 n = sbi->bsize >> L2DTSLOTSIZE;
984 n -= (n + 31) >> L2DTSLOTSIZE; /* stbl size */
985 n -= DTROOTMAXSLOT - sp->header.freecnt; /* header + entries */
986 if (n <= split->nslot)
988 if ((rc = dbAlloc(ip, 0, (s64) xlen, &xaddr))) {
995 pxd = &pxdlist.pxd[0];
996 PXDaddress(pxd, xaddr);
997 PXDlength(pxd, xlen);
998 split->pxdlist = &pxdlist;
999 rc = dtSplitRoot(tid, ip, split, &rmp);
1002 dbFree(ip, xaddr, xlen);
1012 * extend first leaf page
1014 * extend the 1st extent if less than buffer page size
1015 * (dtExtendPage() reurns leaf page unpinned)
1017 pxd = &sp->header.self;
1018 xlen = lengthPXD(pxd);
1019 xsize = xlen << sbi->l2bsize;
1020 if (xsize < PSIZE) {
1021 xaddr = addressPXD(pxd);
1022 n = xsize >> L2DTSLOTSIZE;
1023 n -= (n + 31) >> L2DTSLOTSIZE; /* stbl size */
1024 if ((n + sp->header.freecnt) <= split->nslot)
1025 n = xlen + (xlen << 1);
1029 /* Allocate blocks to quota. */
1030 if (DQUOT_ALLOC_BLOCK(ip, n)) {
1034 quota_allocation += n;
1036 if ((rc = dbReAlloc(sbi->ipbmap, xaddr, (s64) xlen,
1040 pxdlist.maxnpxd = 1;
1042 pxd = &pxdlist.pxd[0];
1043 PXDaddress(pxd, nxaddr)
1044 PXDlength(pxd, xlen + n);
1045 split->pxdlist = &pxdlist;
1046 if ((rc = dtExtendPage(tid, ip, split, btstack))) {
1047 nxaddr = addressPXD(pxd);
1048 if (xaddr != nxaddr) {
1049 /* free relocated extent */
1050 xlen = lengthPXD(pxd);
1051 dbFree(ip, nxaddr, (s64) xlen);
1053 /* free extended delta */
1054 xlen = lengthPXD(pxd) - n;
1055 xaddr = addressPXD(pxd) + xlen;
1056 dbFree(ip, xaddr, (s64) n);
1066 * split leaf page <sp> into <sp> and a new right page <rp>.
1068 * return <rp> pinned and its extent descriptor <rpxd>
1071 * allocate new directory page extent and
1072 * new index page(s) to cover page split(s)
1074 * allocation hint: ?
1076 n = btstack->nsplit;
1077 pxdlist.maxnpxd = pxdlist.npxd = 0;
1078 xlen = sbi->nbperpage;
1079 for (pxd = pxdlist.pxd; n > 0; n--, pxd++) {
1080 if ((rc = dbAlloc(ip, 0, (s64) xlen, &xaddr)) == 0) {
1081 PXDaddress(pxd, xaddr);
1082 PXDlength(pxd, xlen);
1089 /* undo allocation */
1093 split->pxdlist = &pxdlist;
1094 if ((rc = dtSplitPage(tid, ip, split, &rmp, &rp, &rpxd))) {
1097 /* undo allocation */
1102 * propagate up the router entry for the leaf page just split
1104 * insert a router entry for the new page into the parent page,
1105 * propagate the insert/split up the tree by walking back the stack
1106 * of (bn of parent page, index of child page entry in parent page)
1107 * that were traversed during the search for the page that split.
1109 * the propagation of insert/split up the tree stops if the root
1110 * splits or the page inserted into doesn't have to split to hold
1113 * the parent entry for the split page remains the same, and
1114 * a new entry is inserted at its right with the first key and
1115 * block number of the new right page.
1117 * There are a maximum of 4 pages pinned at any time:
1118 * two children, left parent and right parent (when the parent splits).
1119 * keep the child pages pinned while working on the parent.
1120 * make sure that all pins are released at exit.
1122 while ((parent = BT_POP(btstack)) != NULL) {
1123 /* parent page specified by stack frame <parent> */
1125 /* keep current child pages (<lp>, <rp>) pinned */
1130 * insert router entry in parent for new right child page <rp>
1132 /* get the parent page <sp> */
1133 DT_GETPAGE(ip, parent->bn, smp, PSIZE, sp, rc);
1141 * The new key entry goes ONE AFTER the index of parent entry,
1142 * because the split was to the right.
1144 skip = parent->index + 1;
1147 * compute the key for the router entry
1149 * key suffix compression:
1150 * for internal pages that have leaf pages as children,
1151 * retain only what's needed to distinguish between
1152 * the new entry and the entry on the page to its left.
1153 * If the keys compare equal, retain the entire key.
1155 * note that compression is performed only at computing
1156 * router key at the lowest internal level.
1157 * further compression of the key between pairs of higher
1158 * level internal pages loses too much information and
1159 * the search may fail.
1160 * (e.g., two adjacent leaf pages of {a, ..., x} {xx, ...,}
1161 * results in two adjacent parent entries (a)(xx).
1162 * if split occurs between these two entries, and
1163 * if compression is applied, the router key of parent entry
1164 * of right page (x) will divert search for x into right
1165 * subtree and miss x in the left subtree.)
1167 * the entire key must be retained for the next-to-leftmost
1168 * internal key at any level of the tree, or search may fail
1171 switch (rp->header.flag & BT_TYPE) {
1174 * compute the length of prefix for suffix compression
1175 * between last entry of left page and first entry
1178 if ((sp->header.flag & BT_ROOT && skip > 1) ||
1179 sp->header.prev != 0 || skip > 1) {
1180 /* compute uppercase router prefix key */
1181 rc = ciGetLeafPrefixKey(lp,
1182 lp->header.nextindex-1,
1192 /* next to leftmost entry of
1193 lowest internal level */
1195 /* compute uppercase router key */
1196 dtGetKey(rp, 0, &key, sbi->mntflag);
1197 key.name[key.namlen] = 0;
1199 if ((sbi->mntflag & JFS_OS2) == JFS_OS2)
1203 n = NDTINTERNAL(key.namlen);
1207 dtGetKey(rp, 0, &key, sbi->mntflag);
1208 n = NDTINTERNAL(key.namlen);
1212 jfs_err("dtSplitUp(): UFO!");
1216 /* unpin left child page */
1220 * compute the data for the router entry
1222 data->xd = rpxd; /* child page xd */
1225 * parent page is full - split the parent page
1227 if (n > sp->header.freecnt) {
1228 /* init for parent page split */
1230 split->index = skip; /* index at insert */
1233 /* split->data = data; */
1235 /* unpin right child page */
1238 /* The split routines insert the new entry,
1239 * acquire txLock as appropriate.
1240 * return <rp> pinned and its block number <rbn>.
1242 rc = (sp->header.flag & BT_ROOT) ?
1243 dtSplitRoot(tid, ip, split, &rmp) :
1244 dtSplitPage(tid, ip, split, &rmp, &rp, &rpxd);
1250 /* smp and rmp are pinned */
1253 * parent page is not full - insert router entry in parent page
1256 BT_MARK_DIRTY(smp, ip);
1258 * acquire a transaction lock on the parent page
1260 tlck = txLock(tid, ip, smp, tlckDTREE | tlckENTRY);
1261 dtlck = (struct dt_lock *) & tlck->lock;
1262 ASSERT(dtlck->index == 0);
1263 lv = & dtlck->lv[0];
1265 /* linelock header */
1270 /* linelock stbl of non-root parent page */
1271 if (!(sp->header.flag & BT_ROOT)) {
1273 n = skip >> L2DTSLOTSIZE;
1274 lv->offset = sp->header.stblindex + n;
1276 ((sp->header.nextindex -
1277 1) >> L2DTSLOTSIZE) - n + 1;
1281 dtInsertEntry(sp, skip, &key, data, &dtlck);
1283 /* exit propagate up */
1288 /* unpin current split and its right page */
1293 * free remaining extents allocated for split
1297 pxd = &pxdlist.pxd[n];
1298 for (; n < pxdlist.maxnpxd; n++, pxd++)
1299 dbFree(ip, addressPXD(pxd), (s64) lengthPXD(pxd));
1304 /* Rollback quota allocation */
1305 if (rc && quota_allocation)
1306 DQUOT_FREE_BLOCK(ip, quota_allocation);
1317 * function: Split a non-root page of a btree.
1321 * return: 0 - success;
1323 * return split and new page pinned;
1325 static int dtSplitPage(tid_t tid, struct inode *ip, struct dtsplit * split,
1326 struct metapage ** rmpp, dtpage_t ** rpp, pxd_t * rpxdp)
1329 struct metapage *smp;
1331 struct metapage *rmp;
1332 dtpage_t *rp; /* new right page allocated */
1333 s64 rbn; /* new right page block number */
1334 struct metapage *mp;
1337 struct pxdlist *pxdlist;
1339 int skip, nextindex, half, left, nxt, off, si;
1340 struct ldtentry *ldtentry;
1341 struct idtentry *idtentry;
1346 struct dt_lock *sdtlck, *rdtlck;
1348 struct dt_lock *dtlck;
1349 struct lv *slv, *rlv, *lv;
1351 /* get split page */
1353 sp = DT_PAGE(ip, smp);
1356 * allocate the new right page for the split
1358 pxdlist = split->pxdlist;
1359 pxd = &pxdlist->pxd[pxdlist->npxd];
1361 rbn = addressPXD(pxd);
1362 rmp = get_metapage(ip, rbn, PSIZE, 1);
1366 /* Allocate blocks to quota. */
1367 if (DQUOT_ALLOC_BLOCK(ip, lengthPXD(pxd))) {
1368 release_metapage(rmp);
1372 jfs_info("dtSplitPage: ip:0x%p smp:0x%p rmp:0x%p", ip, smp, rmp);
1374 BT_MARK_DIRTY(rmp, ip);
1376 * acquire a transaction lock on the new right page
1378 tlck = txLock(tid, ip, rmp, tlckDTREE | tlckNEW);
1379 rdtlck = (struct dt_lock *) & tlck->lock;
1381 rp = (dtpage_t *) rmp->data;
1383 rp->header.self = *pxd;
1385 BT_MARK_DIRTY(smp, ip);
1387 * acquire a transaction lock on the split page
1391 tlck = txLock(tid, ip, smp, tlckDTREE | tlckENTRY);
1392 sdtlck = (struct dt_lock *) & tlck->lock;
1394 /* linelock header of split page */
1395 ASSERT(sdtlck->index == 0);
1396 slv = & sdtlck->lv[0];
1402 * initialize/update sibling pointers between sp and rp
1404 nextbn = le64_to_cpu(sp->header.next);
1405 rp->header.next = cpu_to_le64(nextbn);
1406 rp->header.prev = cpu_to_le64(addressPXD(&sp->header.self));
1407 sp->header.next = cpu_to_le64(rbn);
1410 * initialize new right page
1412 rp->header.flag = sp->header.flag;
1414 /* compute sorted entry table at start of extent data area */
1415 rp->header.nextindex = 0;
1416 rp->header.stblindex = 1;
1418 n = PSIZE >> L2DTSLOTSIZE;
1419 rp->header.maxslot = n;
1420 stblsize = (n + 31) >> L2DTSLOTSIZE; /* in unit of slot */
1423 fsi = rp->header.stblindex + stblsize;
1424 rp->header.freelist = fsi;
1425 rp->header.freecnt = rp->header.maxslot - fsi;
1428 * sequential append at tail: append without split
1430 * If splitting the last page on a level because of appending
1431 * a entry to it (skip is maxentry), it's likely that the access is
1432 * sequential. Adding an empty page on the side of the level is less
1433 * work and can push the fill factor much higher than normal.
1434 * If we're wrong it's no big deal, we'll just do the split the right
1436 * (It may look like it's equally easy to do a similar hack for
1437 * reverse sorted data, that is, split the tree left,
1438 * but it's not. Be my guest.)
1440 if (nextbn == 0 && split->index == sp->header.nextindex) {
1441 /* linelock header + stbl (first slot) of new page */
1442 rlv = & rdtlck->lv[rdtlck->index];
1448 * initialize freelist of new right page
1451 for (fsi++; fsi < rp->header.maxslot; f++, fsi++)
1455 /* insert entry at the first entry of the new right page */
1456 dtInsertEntry(rp, 0, split->key, split->data, &rdtlck);
1462 * non-sequential insert (at possibly middle page)
1466 * update prev pointer of previous right sibling page;
1469 DT_GETPAGE(ip, nextbn, mp, PSIZE, p, rc);
1471 discard_metapage(rmp);
1475 BT_MARK_DIRTY(mp, ip);
1477 * acquire a transaction lock on the next page
1479 tlck = txLock(tid, ip, mp, tlckDTREE | tlckRELINK);
1480 jfs_info("dtSplitPage: tlck = 0x%p, ip = 0x%p, mp=0x%p",
1482 dtlck = (struct dt_lock *) & tlck->lock;
1484 /* linelock header of previous right sibling page */
1485 lv = & dtlck->lv[dtlck->index];
1490 p->header.prev = cpu_to_le64(rbn);
1496 * split the data between the split and right pages.
1498 skip = split->index;
1499 half = (PSIZE >> L2DTSLOTSIZE) >> 1; /* swag */
1503 * compute fill factor for split pages
1505 * <nxt> traces the next entry to move to rp
1506 * <off> traces the next entry to stay in sp
1508 stbl = (u8 *) & sp->slot[sp->header.stblindex];
1509 nextindex = sp->header.nextindex;
1510 for (nxt = off = 0; nxt < nextindex; ++off) {
1512 /* check for fill factor with new entry size */
1516 switch (sp->header.flag & BT_TYPE) {
1518 ldtentry = (struct ldtentry *) & sp->slot[si];
1520 n = NDTLEAF(ldtentry->namlen);
1522 n = NDTLEAF_LEGACY(ldtentry->
1527 idtentry = (struct idtentry *) & sp->slot[si];
1528 n = NDTINTERNAL(idtentry->namlen);
1535 ++nxt; /* advance to next entry to move in sp */
1543 /* <nxt> poins to the 1st entry to move */
1546 * move entries to right page
1548 * dtMoveEntry() initializes rp and reserves entry for insertion
1550 * split page moved out entries are linelocked;
1551 * new/right page moved in entries are linelocked;
1553 /* linelock header + stbl of new right page */
1554 rlv = & rdtlck->lv[rdtlck->index];
1559 dtMoveEntry(sp, nxt, rp, &sdtlck, &rdtlck, DO_INDEX(ip));
1561 sp->header.nextindex = nxt;
1564 * finalize freelist of new right page
1566 fsi = rp->header.freelist;
1568 for (fsi++; fsi < rp->header.maxslot; f++, fsi++)
1573 * Update directory index table for entries now in right page
1575 if ((rp->header.flag & BT_LEAF) && DO_INDEX(ip)) {
1579 stbl = DT_GETSTBL(rp);
1580 for (n = 0; n < rp->header.nextindex; n++) {
1581 ldtentry = (struct ldtentry *) & rp->slot[stbl[n]];
1582 modify_index(tid, ip, le32_to_cpu(ldtentry->index),
1583 rbn, n, &mp, &lblock);
1586 release_metapage(mp);
1590 * the skipped index was on the left page,
1593 /* insert the new entry in the split page */
1594 dtInsertEntry(sp, skip, split->key, split->data, &sdtlck);
1596 /* linelock stbl of split page */
1597 if (sdtlck->index >= sdtlck->maxcnt)
1598 sdtlck = (struct dt_lock *) txLinelock(sdtlck);
1599 slv = & sdtlck->lv[sdtlck->index];
1600 n = skip >> L2DTSLOTSIZE;
1601 slv->offset = sp->header.stblindex + n;
1603 ((sp->header.nextindex - 1) >> L2DTSLOTSIZE) - n + 1;
1607 * the skipped index was on the right page,
1610 /* adjust the skip index to reflect the new position */
1613 /* insert the new entry in the right page */
1614 dtInsertEntry(rp, skip, split->key, split->data, &rdtlck);
1628 * function: extend 1st/only directory leaf page
1632 * return: 0 - success;
1634 * return extended page pinned;
1636 static int dtExtendPage(tid_t tid,
1637 struct inode *ip, struct dtsplit * split, struct btstack * btstack)
1639 struct super_block *sb = ip->i_sb;
1641 struct metapage *smp, *pmp, *mp;
1643 struct pxdlist *pxdlist;
1646 int newstblindex, newstblsize;
1647 int oldstblindex, oldstblsize;
1650 struct btframe *parent;
1652 struct dt_lock *dtlck;
1655 struct pxd_lock *pxdlock;
1658 struct ldtentry *ldtentry;
1661 /* get page to extend */
1663 sp = DT_PAGE(ip, smp);
1665 /* get parent/root page */
1666 parent = BT_POP(btstack);
1667 DT_GETPAGE(ip, parent->bn, pmp, PSIZE, pp, rc);
1674 pxdlist = split->pxdlist;
1675 pxd = &pxdlist->pxd[pxdlist->npxd];
1678 xaddr = addressPXD(pxd);
1679 tpxd = &sp->header.self;
1680 txaddr = addressPXD(tpxd);
1681 /* in-place extension */
1682 if (xaddr == txaddr) {
1689 /* save moved extent descriptor for later free */
1690 tlck = txMaplock(tid, ip, tlckDTREE | tlckRELOCATE);
1691 pxdlock = (struct pxd_lock *) & tlck->lock;
1692 pxdlock->flag = mlckFREEPXD;
1693 pxdlock->pxd = sp->header.self;
1697 * Update directory index table to reflect new page address
1703 stbl = DT_GETSTBL(sp);
1704 for (n = 0; n < sp->header.nextindex; n++) {
1706 (struct ldtentry *) & sp->slot[stbl[n]];
1707 modify_index(tid, ip,
1708 le32_to_cpu(ldtentry->index),
1709 xaddr, n, &mp, &lblock);
1712 release_metapage(mp);
1719 sp->header.self = *pxd;
1721 jfs_info("dtExtendPage: ip:0x%p smp:0x%p sp:0x%p", ip, smp, sp);
1723 BT_MARK_DIRTY(smp, ip);
1725 * acquire a transaction lock on the extended/leaf page
1727 tlck = txLock(tid, ip, smp, tlckDTREE | type);
1728 dtlck = (struct dt_lock *) & tlck->lock;
1729 lv = & dtlck->lv[0];
1731 /* update buffer extent descriptor of extended page */
1732 xlen = lengthPXD(pxd);
1733 xsize = xlen << JFS_SBI(sb)->l2bsize;
1734 #ifdef _STILL_TO_PORT
1735 bmSetXD(smp, xaddr, xsize);
1736 #endif /* _STILL_TO_PORT */
1739 * copy old stbl to new stbl at start of extended area
1741 oldstblindex = sp->header.stblindex;
1742 oldstblsize = (sp->header.maxslot + 31) >> L2DTSLOTSIZE;
1743 newstblindex = sp->header.maxslot;
1744 n = xsize >> L2DTSLOTSIZE;
1745 newstblsize = (n + 31) >> L2DTSLOTSIZE;
1746 memcpy(&sp->slot[newstblindex], &sp->slot[oldstblindex],
1747 sp->header.nextindex);
1750 * in-line extension: linelock old area of extended page
1752 if (type == tlckEXTEND) {
1753 /* linelock header */
1759 /* linelock new stbl of extended page */
1760 lv->offset = newstblindex;
1761 lv->length = newstblsize;
1764 * relocation: linelock whole relocated area
1768 lv->length = sp->header.maxslot + newstblsize;
1773 sp->header.maxslot = n;
1774 sp->header.stblindex = newstblindex;
1775 /* sp->header.nextindex remains the same */
1778 * add old stbl region at head of freelist
1782 last = sp->header.freelist;
1783 for (n = 0; n < oldstblsize; n++, fsi++, f++) {
1787 sp->header.freelist = last;
1788 sp->header.freecnt += oldstblsize;
1791 * append free region of newly extended area at tail of freelist
1793 /* init free region of newly extended area */
1794 fsi = n = newstblindex + newstblsize;
1796 for (fsi++; fsi < sp->header.maxslot; f++, fsi++)
1800 /* append new free region at tail of old freelist */
1801 fsi = sp->header.freelist;
1803 sp->header.freelist = n;
1808 } while (fsi != -1);
1813 sp->header.freecnt += sp->header.maxslot - n;
1816 * insert the new entry
1818 dtInsertEntry(sp, split->index, split->key, split->data, &dtlck);
1820 BT_MARK_DIRTY(pmp, ip);
1822 * linelock any freeslots residing in old extent
1824 if (type == tlckEXTEND) {
1825 n = sp->header.maxslot >> 2;
1826 if (sp->header.freelist < n)
1827 dtLinelockFreelist(sp, n, &dtlck);
1831 * update parent entry on the parent/root page
1834 * acquire a transaction lock on the parent/root page
1836 tlck = txLock(tid, ip, pmp, tlckDTREE | tlckENTRY);
1837 dtlck = (struct dt_lock *) & tlck->lock;
1838 lv = & dtlck->lv[dtlck->index];
1840 /* linelock parent entry - 1st slot */
1845 /* update the parent pxd for page extension */
1846 tpxd = (pxd_t *) & pp->slot[1];
1858 * split the full root page into
1859 * original/root/split page and new right page
1860 * i.e., root remains fixed in tree anchor (inode) and
1861 * the root is copied to a single new right child page
1862 * since root page << non-root page, and
1863 * the split root page contains a single entry for the
1864 * new right child page.
1868 * return: 0 - success;
1870 * return new page pinned;
1872 static int dtSplitRoot(tid_t tid,
1873 struct inode *ip, struct dtsplit * split, struct metapage ** rmpp)
1875 struct super_block *sb = ip->i_sb;
1876 struct metapage *smp;
1878 struct metapage *rmp;
1885 int fsi, stblsize, n;
1888 struct pxdlist *pxdlist;
1890 struct dt_lock *dtlck;
1894 /* get split root page */
1896 sp = &JFS_IP(ip)->i_dtroot;
1899 * allocate/initialize a single (right) child page
1901 * N.B. at first split, a one (or two) block to fit new entry
1902 * is allocated; at subsequent split, a full page is allocated;
1904 pxdlist = split->pxdlist;
1905 pxd = &pxdlist->pxd[pxdlist->npxd];
1907 rbn = addressPXD(pxd);
1908 xlen = lengthPXD(pxd);
1909 xsize = xlen << JFS_SBI(sb)->l2bsize;
1910 rmp = get_metapage(ip, rbn, xsize, 1);
1916 /* Allocate blocks to quota. */
1917 if (DQUOT_ALLOC_BLOCK(ip, lengthPXD(pxd))) {
1918 release_metapage(rmp);
1922 BT_MARK_DIRTY(rmp, ip);
1924 * acquire a transaction lock on the new right page
1926 tlck = txLock(tid, ip, rmp, tlckDTREE | tlckNEW);
1927 dtlck = (struct dt_lock *) & tlck->lock;
1930 (sp->header.flag & BT_LEAF) ? BT_LEAF : BT_INTERNAL;
1931 rp->header.self = *pxd;
1933 /* initialize sibling pointers */
1934 rp->header.next = 0;
1935 rp->header.prev = 0;
1938 * move in-line root page into new right page extent
1940 /* linelock header + copied entries + new stbl (1st slot) in new page */
1941 ASSERT(dtlck->index == 0);
1942 lv = & dtlck->lv[0];
1944 lv->length = 10; /* 1 + 8 + 1 */
1947 n = xsize >> L2DTSLOTSIZE;
1948 rp->header.maxslot = n;
1949 stblsize = (n + 31) >> L2DTSLOTSIZE;
1951 /* copy old stbl to new stbl at start of extended area */
1952 rp->header.stblindex = DTROOTMAXSLOT;
1953 stbl = (s8 *) & rp->slot[DTROOTMAXSLOT];
1954 memcpy(stbl, sp->header.stbl, sp->header.nextindex);
1955 rp->header.nextindex = sp->header.nextindex;
1957 /* copy old data area to start of new data area */
1958 memcpy(&rp->slot[1], &sp->slot[1], IDATASIZE);
1961 * append free region of newly extended area at tail of freelist
1963 /* init free region of newly extended area */
1964 fsi = n = DTROOTMAXSLOT + stblsize;
1966 for (fsi++; fsi < rp->header.maxslot; f++, fsi++)
1970 /* append new free region at tail of old freelist */
1971 fsi = sp->header.freelist;
1973 rp->header.freelist = n;
1975 rp->header.freelist = fsi;
1980 } while (fsi != -1);
1985 rp->header.freecnt = sp->header.freecnt + rp->header.maxslot - n;
1988 * Update directory index table for entries now in right page
1990 if ((rp->header.flag & BT_LEAF) && DO_INDEX(ip)) {
1992 struct metapage *mp = NULL;
1993 struct ldtentry *ldtentry;
1995 stbl = DT_GETSTBL(rp);
1996 for (n = 0; n < rp->header.nextindex; n++) {
1997 ldtentry = (struct ldtentry *) & rp->slot[stbl[n]];
1998 modify_index(tid, ip, le32_to_cpu(ldtentry->index),
1999 rbn, n, &mp, &lblock);
2002 release_metapage(mp);
2005 * insert the new entry into the new right/child page
2006 * (skip index in the new right page will not change)
2008 dtInsertEntry(rp, split->index, split->key, split->data, &dtlck);
2011 * reset parent/root page
2013 * set the 1st entry offset to 0, which force the left-most key
2014 * at any level of the tree to be less than any search key.
2016 * The btree comparison code guarantees that the left-most key on any
2017 * level of the tree is never used, so it doesn't need to be filled in.
2019 BT_MARK_DIRTY(smp, ip);
2021 * acquire a transaction lock on the root page (in-memory inode)
2023 tlck = txLock(tid, ip, smp, tlckDTREE | tlckNEW | tlckBTROOT);
2024 dtlck = (struct dt_lock *) & tlck->lock;
2027 ASSERT(dtlck->index == 0);
2028 lv = & dtlck->lv[0];
2030 lv->length = DTROOTMAXSLOT;
2033 /* update page header of root */
2034 if (sp->header.flag & BT_LEAF) {
2035 sp->header.flag &= ~BT_LEAF;
2036 sp->header.flag |= BT_INTERNAL;
2039 /* init the first entry */
2040 s = (struct idtentry *) & sp->slot[DTENTRYSTART];
2046 stbl = sp->header.stbl;
2047 stbl[0] = DTENTRYSTART;
2048 sp->header.nextindex = 1;
2051 fsi = DTENTRYSTART + 1;
2054 /* init free region of remaining area */
2055 for (fsi++; fsi < DTROOTMAXSLOT; f++, fsi++)
2059 sp->header.freelist = DTENTRYSTART + 1;
2060 sp->header.freecnt = DTROOTMAXSLOT - (DTENTRYSTART + 1);
2071 * function: delete the entry(s) referenced by a key.
2077 int dtDelete(tid_t tid,
2078 struct inode *ip, struct component_name * key, ino_t * ino, int flag)
2082 struct metapage *mp, *imp;
2085 struct btstack btstack;
2086 struct dt_lock *dtlck;
2090 struct ldtentry *ldtentry;
2092 u32 table_index, next_index;
2093 struct metapage *nmp;
2097 * search for the entry to delete:
2099 * dtSearch() returns (leaf page pinned, index at which to delete).
2101 if ((rc = dtSearch(ip, key, ino, &btstack, flag)))
2104 /* retrieve search result */
2105 DT_GETSEARCH(ip, btstack.top, bn, mp, p, index);
2108 * We need to find put the index of the next entry into the
2109 * directory index table in order to resume a readdir from this
2113 stbl = DT_GETSTBL(p);
2114 ldtentry = (struct ldtentry *) & p->slot[stbl[index]];
2115 table_index = le32_to_cpu(ldtentry->index);
2116 if (index == (p->header.nextindex - 1)) {
2118 * Last entry in this leaf page
2120 if ((p->header.flag & BT_ROOT)
2121 || (p->header.next == 0))
2124 /* Read next leaf page */
2125 DT_GETPAGE(ip, le64_to_cpu(p->header.next),
2126 nmp, PSIZE, np, rc);
2130 stbl = DT_GETSTBL(np);
2132 (struct ldtentry *) & np->
2135 le32_to_cpu(ldtentry->index);
2141 (struct ldtentry *) & p->slot[stbl[index + 1]];
2142 next_index = le32_to_cpu(ldtentry->index);
2144 free_index(tid, ip, table_index, next_index);
2147 * the leaf page becomes empty, delete the page
2149 if (p->header.nextindex == 1) {
2150 /* delete empty page */
2151 rc = dtDeleteUp(tid, ip, mp, p, &btstack);
2154 * the leaf page has other entries remaining:
2156 * delete the entry from the leaf page.
2159 BT_MARK_DIRTY(mp, ip);
2161 * acquire a transaction lock on the leaf page
2163 tlck = txLock(tid, ip, mp, tlckDTREE | tlckENTRY);
2164 dtlck = (struct dt_lock *) & tlck->lock;
2167 * Do not assume that dtlck->index will be zero. During a
2168 * rename within a directory, this transaction may have
2169 * modified this page already when adding the new entry.
2172 /* linelock header */
2173 if (dtlck->index >= dtlck->maxcnt)
2174 dtlck = (struct dt_lock *) txLinelock(dtlck);
2175 lv = & dtlck->lv[dtlck->index];
2180 /* linelock stbl of non-root leaf page */
2181 if (!(p->header.flag & BT_ROOT)) {
2182 if (dtlck->index >= dtlck->maxcnt)
2183 dtlck = (struct dt_lock *) txLinelock(dtlck);
2184 lv = & dtlck->lv[dtlck->index];
2185 i = index >> L2DTSLOTSIZE;
2186 lv->offset = p->header.stblindex + i;
2188 ((p->header.nextindex - 1) >> L2DTSLOTSIZE) -
2193 /* free the leaf entry */
2194 dtDeleteEntry(p, index, &dtlck);
2197 * Update directory index table for entries moved in stbl
2199 if (DO_INDEX(ip) && index < p->header.nextindex) {
2203 stbl = DT_GETSTBL(p);
2204 for (i = index; i < p->header.nextindex; i++) {
2206 (struct ldtentry *) & p->slot[stbl[i]];
2207 modify_index(tid, ip,
2208 le32_to_cpu(ldtentry->index),
2209 bn, i, &imp, &lblock);
2212 release_metapage(imp);
2226 * free empty pages as propagating deletion up the tree
2232 static int dtDeleteUp(tid_t tid, struct inode *ip,
2233 struct metapage * fmp, dtpage_t * fp, struct btstack * btstack)
2236 struct metapage *mp;
2238 int index, nextindex;
2240 struct btframe *parent;
2241 struct dt_lock *dtlck;
2244 struct pxd_lock *pxdlock;
2248 * keep the root leaf page which has become empty
2250 if (BT_IS_ROOT(fmp)) {
2254 * dtInitRoot() acquires txlock on the root
2256 dtInitRoot(tid, ip, PARENT(ip));
2264 * free the non-root leaf page
2267 * acquire a transaction lock on the page
2269 * write FREEXTENT|NOREDOPAGE log record
2270 * N.B. linelock is overlaid as freed extent descriptor, and
2271 * the buffer page is freed;
2273 tlck = txMaplock(tid, ip, tlckDTREE | tlckFREE);
2274 pxdlock = (struct pxd_lock *) & tlck->lock;
2275 pxdlock->flag = mlckFREEPXD;
2276 pxdlock->pxd = fp->header.self;
2279 /* update sibling pointers */
2280 if ((rc = dtRelink(tid, ip, fp))) {
2285 xlen = lengthPXD(&fp->header.self);
2287 /* Free quota allocation. */
2288 DQUOT_FREE_BLOCK(ip, xlen);
2290 /* free/invalidate its buffer page */
2291 discard_metapage(fmp);
2294 * propagate page deletion up the directory tree
2296 * If the delete from the parent page makes it empty,
2297 * continue all the way up the tree.
2298 * stop if the root page is reached (which is never deleted) or
2299 * if the entry deletion does not empty the page.
2301 while ((parent = BT_POP(btstack)) != NULL) {
2302 /* pin the parent page <sp> */
2303 DT_GETPAGE(ip, parent->bn, mp, PSIZE, p, rc);
2308 * free the extent of the child page deleted
2310 index = parent->index;
2313 * delete the entry for the child page from parent
2315 nextindex = p->header.nextindex;
2318 * the parent has the single entry being deleted:
2320 * free the parent page which has become empty.
2322 if (nextindex == 1) {
2324 * keep the root internal page which has become empty
2326 if (p->header.flag & BT_ROOT) {
2330 * dtInitRoot() acquires txlock on the root
2332 dtInitRoot(tid, ip, PARENT(ip));
2339 * free the parent page
2343 * acquire a transaction lock on the page
2345 * write FREEXTENT|NOREDOPAGE log record
2349 tlckDTREE | tlckFREE);
2350 pxdlock = (struct pxd_lock *) & tlck->lock;
2351 pxdlock->flag = mlckFREEPXD;
2352 pxdlock->pxd = p->header.self;
2355 /* update sibling pointers */
2356 if ((rc = dtRelink(tid, ip, p))) {
2361 xlen = lengthPXD(&p->header.self);
2363 /* Free quota allocation */
2364 DQUOT_FREE_BLOCK(ip, xlen);
2366 /* free/invalidate its buffer page */
2367 discard_metapage(mp);
2375 * the parent has other entries remaining:
2377 * delete the router entry from the parent page.
2379 BT_MARK_DIRTY(mp, ip);
2381 * acquire a transaction lock on the page
2383 * action: router entry deletion
2385 tlck = txLock(tid, ip, mp, tlckDTREE | tlckENTRY);
2386 dtlck = (struct dt_lock *) & tlck->lock;
2388 /* linelock header */
2389 if (dtlck->index >= dtlck->maxcnt)
2390 dtlck = (struct dt_lock *) txLinelock(dtlck);
2391 lv = & dtlck->lv[dtlck->index];
2396 /* linelock stbl of non-root leaf page */
2397 if (!(p->header.flag & BT_ROOT)) {
2398 if (dtlck->index < dtlck->maxcnt)
2401 dtlck = (struct dt_lock *) txLinelock(dtlck);
2402 lv = & dtlck->lv[0];
2404 i = index >> L2DTSLOTSIZE;
2405 lv->offset = p->header.stblindex + i;
2407 ((p->header.nextindex - 1) >> L2DTSLOTSIZE) -
2412 /* free the router entry */
2413 dtDeleteEntry(p, index, &dtlck);
2415 /* reset key of new leftmost entry of level (for consistency) */
2417 ((p->header.flag & BT_ROOT) || p->header.prev == 0))
2418 dtTruncateEntry(p, 0, &dtlck);
2420 /* unpin the parent page */
2423 /* exit propagation up */
2432 * NAME: dtRelocate()
2434 * FUNCTION: relocate dtpage (internal or leaf) of directory;
2435 * This function is mainly used by defragfs utility.
2437 int dtRelocate(tid_t tid, struct inode *ip, s64 lmxaddr, pxd_t * opxd,
2441 struct metapage *mp, *pmp, *lmp, *rmp;
2442 dtpage_t *p, *pp, *rp = 0, *lp= 0;
2445 struct btstack btstack;
2447 s64 oxaddr, nextbn, prevbn;
2450 struct dt_lock *dtlck;
2451 struct pxd_lock *pxdlock;
2455 oxaddr = addressPXD(opxd);
2456 xlen = lengthPXD(opxd);
2458 jfs_info("dtRelocate: lmxaddr:%Ld xaddr:%Ld:%Ld xlen:%d",
2459 (long long)lmxaddr, (long long)oxaddr, (long long)nxaddr,
2463 * 1. get the internal parent dtpage covering
2464 * router entry for the tartget page to be relocated;
2466 rc = dtSearchNode(ip, lmxaddr, opxd, &btstack);
2470 /* retrieve search result */
2471 DT_GETSEARCH(ip, btstack.top, bn, pmp, pp, index);
2472 jfs_info("dtRelocate: parent router entry validated.");
2475 * 2. relocate the target dtpage
2477 /* read in the target page from src extent */
2478 DT_GETPAGE(ip, oxaddr, mp, PSIZE, p, rc);
2480 /* release the pinned parent page */
2486 * read in sibling pages if any to update sibling pointers;
2489 if (p->header.next) {
2490 nextbn = le64_to_cpu(p->header.next);
2491 DT_GETPAGE(ip, nextbn, rmp, PSIZE, rp, rc);
2500 if (p->header.prev) {
2501 prevbn = le64_to_cpu(p->header.prev);
2502 DT_GETPAGE(ip, prevbn, lmp, PSIZE, lp, rc);
2512 /* at this point, all xtpages to be updated are in memory */
2515 * update sibling pointers of sibling dtpages if any;
2518 tlck = txLock(tid, ip, lmp, tlckDTREE | tlckRELINK);
2519 dtlck = (struct dt_lock *) & tlck->lock;
2520 /* linelock header */
2521 ASSERT(dtlck->index == 0);
2522 lv = & dtlck->lv[0];
2527 lp->header.next = cpu_to_le64(nxaddr);
2532 tlck = txLock(tid, ip, rmp, tlckDTREE | tlckRELINK);
2533 dtlck = (struct dt_lock *) & tlck->lock;
2534 /* linelock header */
2535 ASSERT(dtlck->index == 0);
2536 lv = & dtlck->lv[0];
2541 rp->header.prev = cpu_to_le64(nxaddr);
2546 * update the target dtpage to be relocated
2548 * write LOG_REDOPAGE of LOG_NEW type for dst page
2549 * for the whole target page (logredo() will apply
2550 * after image and update bmap for allocation of the
2551 * dst extent), and update bmap for allocation of
2554 tlck = txLock(tid, ip, mp, tlckDTREE | tlckNEW);
2555 dtlck = (struct dt_lock *) & tlck->lock;
2556 /* linelock header */
2557 ASSERT(dtlck->index == 0);
2558 lv = & dtlck->lv[0];
2560 /* update the self address in the dtpage header */
2561 pxd = &p->header.self;
2562 PXDaddress(pxd, nxaddr);
2564 /* the dst page is the same as the src page, i.e.,
2565 * linelock for afterimage of the whole page;
2568 lv->length = p->header.maxslot;
2571 /* update the buffer extent descriptor of the dtpage */
2572 xsize = xlen << JFS_SBI(ip->i_sb)->l2bsize;
2573 #ifdef _STILL_TO_PORT
2574 bmSetXD(mp, nxaddr, xsize);
2575 #endif /* _STILL_TO_PORT */
2576 /* unpin the relocated page */
2578 jfs_info("dtRelocate: target dtpage relocated.");
2580 /* the moved extent is dtpage, then a LOG_NOREDOPAGE log rec
2581 * needs to be written (in logredo(), the LOG_NOREDOPAGE log rec
2582 * will also force a bmap update ).
2586 * 3. acquire maplock for the source extent to be freed;
2588 /* for dtpage relocation, write a LOG_NOREDOPAGE record
2589 * for the source dtpage (logredo() will init NoRedoPage
2590 * filter and will also update bmap for free of the source
2591 * dtpage), and upadte bmap for free of the source dtpage;
2593 tlck = txMaplock(tid, ip, tlckDTREE | tlckFREE);
2594 pxdlock = (struct pxd_lock *) & tlck->lock;
2595 pxdlock->flag = mlckFREEPXD;
2596 PXDaddress(&pxdlock->pxd, oxaddr);
2597 PXDlength(&pxdlock->pxd, xlen);
2601 * 4. update the parent router entry for relocation;
2603 * acquire tlck for the parent entry covering the target dtpage;
2604 * write LOG_REDOPAGE to apply after image only;
2606 jfs_info("dtRelocate: update parent router entry.");
2607 tlck = txLock(tid, ip, pmp, tlckDTREE | tlckENTRY);
2608 dtlck = (struct dt_lock *) & tlck->lock;
2609 lv = & dtlck->lv[dtlck->index];
2611 /* update the PXD with the new address */
2612 stbl = DT_GETSTBL(pp);
2613 pxd = (pxd_t *) & pp->slot[stbl[index]];
2614 PXDaddress(pxd, nxaddr);
2615 lv->offset = stbl[index];
2619 /* unpin the parent dtpage */
2626 * NAME: dtSearchNode()
2628 * FUNCTION: Search for an dtpage containing a specified address
2629 * This function is mainly used by defragfs utility.
2631 * NOTE: Search result on stack, the found page is pinned at exit.
2632 * The result page must be an internal dtpage.
2633 * lmxaddr give the address of the left most page of the
2634 * dtree level, in which the required dtpage resides.
2636 static int dtSearchNode(struct inode *ip, s64 lmxaddr, pxd_t * kpxd,
2637 struct btstack * btstack)
2641 struct metapage *mp;
2643 int psize = 288; /* initial in-line directory */
2647 struct btframe *btsp;
2649 BT_CLR(btstack); /* reset stack */
2652 * descend tree to the level with specified leftmost page
2654 * by convention, root bn = 0.
2657 /* get/pin the page to search */
2658 DT_GETPAGE(ip, bn, mp, psize, p, rc);
2662 /* does the xaddr of leftmost page of the levevl
2663 * matches levevl search key ?
2665 if (p->header.flag & BT_ROOT) {
2668 } else if (addressPXD(&p->header.self) == lmxaddr)
2672 * descend down to leftmost child page
2674 if (p->header.flag & BT_LEAF) {
2679 /* get the leftmost entry */
2680 stbl = DT_GETSTBL(p);
2681 pxd = (pxd_t *) & p->slot[stbl[0]];
2683 /* get the child page block address */
2684 bn = addressPXD(pxd);
2685 psize = lengthPXD(pxd) << JFS_SBI(ip->i_sb)->l2bsize;
2686 /* unpin the parent page */
2691 * search each page at the current levevl
2694 stbl = DT_GETSTBL(p);
2695 for (i = 0; i < p->header.nextindex; i++) {
2696 pxd = (pxd_t *) & p->slot[stbl[i]];
2698 /* found the specified router entry */
2699 if (addressPXD(pxd) == addressPXD(kpxd) &&
2700 lengthPXD(pxd) == lengthPXD(kpxd)) {
2701 btsp = btstack->top;
2710 /* get the right sibling page if any */
2712 bn = le64_to_cpu(p->header.next);
2718 /* unpin current page */
2721 /* get the right sibling page */
2722 DT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
2728 #endif /* _NOTYET */
2734 * link around a freed page.
2737 * fp: page to be freed
2741 static int dtRelink(tid_t tid, struct inode *ip, dtpage_t * p)
2744 struct metapage *mp;
2747 struct dt_lock *dtlck;
2750 nextbn = le64_to_cpu(p->header.next);
2751 prevbn = le64_to_cpu(p->header.prev);
2753 /* update prev pointer of the next page */
2755 DT_GETPAGE(ip, nextbn, mp, PSIZE, p, rc);
2759 BT_MARK_DIRTY(mp, ip);
2761 * acquire a transaction lock on the next page
2763 * action: update prev pointer;
2765 tlck = txLock(tid, ip, mp, tlckDTREE | tlckRELINK);
2766 jfs_info("dtRelink nextbn: tlck = 0x%p, ip = 0x%p, mp=0x%p",
2768 dtlck = (struct dt_lock *) & tlck->lock;
2770 /* linelock header */
2771 if (dtlck->index >= dtlck->maxcnt)
2772 dtlck = (struct dt_lock *) txLinelock(dtlck);
2773 lv = & dtlck->lv[dtlck->index];
2778 p->header.prev = cpu_to_le64(prevbn);
2782 /* update next pointer of the previous page */
2784 DT_GETPAGE(ip, prevbn, mp, PSIZE, p, rc);
2788 BT_MARK_DIRTY(mp, ip);
2790 * acquire a transaction lock on the prev page
2792 * action: update next pointer;
2794 tlck = txLock(tid, ip, mp, tlckDTREE | tlckRELINK);
2795 jfs_info("dtRelink prevbn: tlck = 0x%p, ip = 0x%p, mp=0x%p",
2797 dtlck = (struct dt_lock *) & tlck->lock;
2799 /* linelock header */
2800 if (dtlck->index >= dtlck->maxcnt)
2801 dtlck = (struct dt_lock *) txLinelock(dtlck);
2802 lv = & dtlck->lv[dtlck->index];
2807 p->header.next = cpu_to_le64(nextbn);
2818 * initialize directory root (inline in inode)
2820 void dtInitRoot(tid_t tid, struct inode *ip, u32 idotdot)
2822 struct jfs_inode_info *jfs_ip = JFS_IP(ip);
2827 struct dt_lock *dtlck;
2832 * If this was previously an non-empty directory, we need to remove
2833 * the old directory table.
2836 if (!jfs_dirtable_inline(ip)) {
2837 struct tblock *tblk = tid_to_tblock(tid);
2839 * We're playing games with the tid's xflag. If
2840 * we're removing a regular file, the file's xtree
2841 * is committed with COMMIT_PMAP, but we always
2842 * commit the directories xtree with COMMIT_PWMAP.
2844 xflag_save = tblk->xflag;
2847 * xtTruncate isn't guaranteed to fully truncate
2848 * the xtree. The caller needs to check i_size
2849 * after committing the transaction to see if
2850 * additional truncation is needed. The
2851 * COMMIT_Stale flag tells caller that we
2852 * initiated the truncation.
2854 xtTruncate(tid, ip, 0, COMMIT_PWMAP);
2855 set_cflag(COMMIT_Stale, ip);
2857 tblk->xflag = xflag_save;
2861 jfs_ip->next_index = 2;
2863 ip->i_size = IDATASIZE;
2866 * acquire a transaction lock on the root
2868 * action: directory initialization;
2870 tlck = txLock(tid, ip, (struct metapage *) & jfs_ip->bxflag,
2871 tlckDTREE | tlckENTRY | tlckBTROOT);
2872 dtlck = (struct dt_lock *) & tlck->lock;
2875 ASSERT(dtlck->index == 0);
2876 lv = & dtlck->lv[0];
2878 lv->length = DTROOTMAXSLOT;
2881 p = &jfs_ip->i_dtroot;
2883 p->header.flag = DXD_INDEX | BT_ROOT | BT_LEAF;
2885 p->header.nextindex = 0;
2891 /* init data area of root */
2892 for (fsi++; fsi < DTROOTMAXSLOT; f++, fsi++)
2896 p->header.freelist = 1;
2897 p->header.freecnt = 8;
2899 /* init '..' entry */
2900 p->header.idotdot = cpu_to_le32(idotdot);
2906 * add_missing_indices()
2908 * function: Fix dtree page in which one or more entries has an invalid index.
2909 * fsck.jfs should really fix this, but it currently does not.
2910 * Called from jfs_readdir when bad index is detected.
2912 static void add_missing_indices(struct inode *inode, s64 bn)
2915 struct dt_lock *dtlck;
2919 struct metapage *mp;
2926 tid = txBegin(inode->i_sb, 0);
2928 DT_GETPAGE(inode, bn, mp, PSIZE, p, rc);
2931 printk(KERN_ERR "DT_GETPAGE failed!\n");
2934 BT_MARK_DIRTY(mp, inode);
2936 ASSERT(p->header.flag & BT_LEAF);
2938 tlck = txLock(tid, inode, mp, tlckDTREE | tlckENTRY);
2940 tlck->type |= tlckBTROOT;
2942 dtlck = (struct dt_lock *) &tlck->lock;
2944 stbl = DT_GETSTBL(p);
2945 for (i = 0; i < p->header.nextindex; i++) {
2946 d = (struct ldtentry *) &p->slot[stbl[i]];
2947 index = le32_to_cpu(d->index);
2948 if ((index < 2) || (index >= JFS_IP(inode)->next_index)) {
2949 d->index = cpu_to_le32(add_index(tid, inode, bn, i));
2950 if (dtlck->index >= dtlck->maxcnt)
2951 dtlck = (struct dt_lock *) txLinelock(dtlck);
2952 lv = &dtlck->lv[dtlck->index];
2953 lv->offset = stbl[i];
2960 (void) txCommit(tid, 1, &inode, 0);
2966 * Buffer to hold directory entry info while traversing a dtree page
2967 * before being fed to the filldir function
2977 * function to determine next variable-sized jfs_dirent in buffer
2979 static inline struct jfs_dirent *next_jfs_dirent(struct jfs_dirent *dirent)
2981 return (struct jfs_dirent *)
2983 ((sizeof (struct jfs_dirent) + dirent->name_len + 1 +
2984 sizeof (loff_t) - 1) &
2985 ~(sizeof (loff_t) - 1)));
2991 * function: read directory entries sequentially
2992 * from the specified entry offset
2996 * return: offset = (pn, index) of start entry
2997 * of next jfs_readdir()/dtRead()
2999 int jfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
3001 struct inode *ip = filp->f_dentry->d_inode;
3002 struct nls_table *codepage = JFS_SBI(ip->i_sb)->nls_tab;
3004 loff_t dtpos; /* legacy OS/2 style position */
3009 } *dtoffset = (struct dtoffset *) &dtpos;
3011 struct metapage *mp;
3015 struct btstack btstack;
3019 int d_namleft, len, outlen;
3020 unsigned long dirent_buf;
3024 uint loop_count = 0;
3025 struct jfs_dirent *jfs_dirent;
3027 int overflow, fix_page, page_fixed = 0;
3028 static int unique_pos = 2; /* If we can't fix broken index */
3030 if (filp->f_pos == DIREND)
3035 * persistent index is stored in directory entries.
3036 * Special cases: 0 = .
3038 * -1 = End of directory
3042 dir_index = (u32) filp->f_pos;
3044 if (dir_index > 1) {
3045 struct dir_table_slot dirtab_slot;
3048 (dir_index >= JFS_IP(ip)->next_index)) {
3049 /* Stale position. Directory has shrunk */
3050 filp->f_pos = DIREND;
3054 rc = read_index(ip, dir_index, &dirtab_slot);
3056 filp->f_pos = DIREND;
3059 if (dirtab_slot.flag == DIR_INDEX_FREE) {
3060 if (loop_count++ > JFS_IP(ip)->next_index) {
3061 jfs_err("jfs_readdir detected "
3063 filp->f_pos = DIREND;
3066 dir_index = le32_to_cpu(dirtab_slot.addr2);
3067 if (dir_index == -1) {
3068 filp->f_pos = DIREND;
3073 bn = addressDTS(&dirtab_slot);
3074 index = dirtab_slot.slot;
3075 DT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
3077 filp->f_pos = DIREND;
3080 if (p->header.flag & BT_INTERNAL) {
3081 jfs_err("jfs_readdir: bad index table");
3087 if (dir_index == 0) {
3092 if (filldir(dirent, ".", 1, 0, ip->i_ino,
3100 if (filldir(dirent, "..", 2, 1, PARENT(ip), DT_DIR))
3104 * Find first entry of left-most leaf
3107 filp->f_pos = DIREND;
3111 if ((rc = dtReadFirst(ip, &btstack)))
3114 DT_GETSEARCH(ip, btstack.top, bn, mp, p, index);
3118 * Legacy filesystem - OS/2 & Linux JFS < 0.3.6
3120 * pn = index = 0: First entry "."
3121 * pn = 0; index = 1: Second entry ".."
3122 * pn > 0: Real entries, pn=1 -> leftmost page
3123 * pn = index = -1: No more entries
3125 dtpos = filp->f_pos;
3127 /* build "." entry */
3129 if (filldir(dirent, ".", 1, filp->f_pos, ip->i_ino,
3132 dtoffset->index = 1;
3133 filp->f_pos = dtpos;
3136 if (dtoffset->pn == 0) {
3137 if (dtoffset->index == 1) {
3138 /* build ".." entry */
3140 if (filldir(dirent, "..", 2, filp->f_pos,
3141 PARENT(ip), DT_DIR))
3144 jfs_err("jfs_readdir called with "
3148 dtoffset->index = 0;
3149 filp->f_pos = dtpos;
3153 filp->f_pos = DIREND;
3157 if ((rc = dtReadNext(ip, &filp->f_pos, &btstack))) {
3158 jfs_err("jfs_readdir: unexpected rc = %d "
3159 "from dtReadNext", rc);
3160 filp->f_pos = DIREND;
3163 /* get start leaf page and index */
3164 DT_GETSEARCH(ip, btstack.top, bn, mp, p, index);
3166 /* offset beyond directory eof ? */
3168 filp->f_pos = DIREND;
3173 dirent_buf = __get_free_page(GFP_KERNEL);
3174 if (dirent_buf == 0) {
3176 jfs_warn("jfs_readdir: __get_free_page failed!");
3177 filp->f_pos = DIREND;
3182 jfs_dirent = (struct jfs_dirent *) dirent_buf;
3184 overflow = fix_page = 0;
3186 stbl = DT_GETSTBL(p);
3188 for (i = index; i < p->header.nextindex; i++) {
3189 d = (struct ldtentry *) & p->slot[stbl[i]];
3191 if (((long) jfs_dirent + d->namlen + 1) >
3192 (dirent_buf + PAGE_SIZE)) {
3193 /* DBCS codepages could overrun dirent_buf */
3199 d_namleft = d->namlen;
3200 name_ptr = jfs_dirent->name;
3201 jfs_dirent->ino = le32_to_cpu(d->inumber);
3204 len = min(d_namleft, DTLHDRDATALEN);
3205 jfs_dirent->position = le32_to_cpu(d->index);
3207 * d->index should always be valid, but it
3208 * isn't. fsck.jfs doesn't create the
3209 * directory index for the lost+found
3210 * directory. Rather than let it go,
3211 * we can try to fix it.
3213 if ((jfs_dirent->position < 2) ||
3214 (jfs_dirent->position >=
3215 JFS_IP(ip)->next_index)) {
3216 if (!page_fixed && !isReadOnly(ip)) {
3219 * setting overflow and setting
3220 * index to i will cause the
3221 * same page to be processed
3222 * again starting here
3228 jfs_dirent->position = unique_pos++;
3231 jfs_dirent->position = dtpos;
3232 len = min(d_namleft, DTLHDRDATALEN_LEGACY);
3235 /* copy the name of head/only segment */
3236 outlen = jfs_strfromUCS_le(name_ptr, d->name, len,
3238 jfs_dirent->name_len = outlen;
3240 /* copy name in the additional segment(s) */
3243 t = (struct dtslot *) & p->slot[next];
3247 if (d_namleft == 0) {
3249 "JFS:Dtree error: ino = "
3250 "%ld, bn=%Ld, index = %d",
3256 len = min(d_namleft, DTSLOTDATALEN);
3257 outlen = jfs_strfromUCS_le(name_ptr, t->name,
3259 jfs_dirent->name_len += outlen;
3265 jfs_dirent = next_jfs_dirent(jfs_dirent);
3272 /* Point to next leaf page */
3273 if (p->header.flag & BT_ROOT)
3276 bn = le64_to_cpu(p->header.next);
3278 /* update offset (pn:index) for new page */
3281 dtoffset->index = 0;
3287 /* unpin previous leaf page */
3290 jfs_dirent = (struct jfs_dirent *) dirent_buf;
3291 while (jfs_dirents--) {
3292 filp->f_pos = jfs_dirent->position;
3293 if (filldir(dirent, jfs_dirent->name,
3294 jfs_dirent->name_len, filp->f_pos,
3295 jfs_dirent->ino, DT_UNKNOWN))
3297 jfs_dirent = next_jfs_dirent(jfs_dirent);
3301 add_missing_indices(ip, bn);
3305 if (!overflow && (bn == 0)) {
3306 filp->f_pos = DIREND;
3310 DT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
3312 free_page(dirent_buf);
3318 free_page(dirent_buf);
3327 * function: get the leftmost page of the directory
3329 static int dtReadFirst(struct inode *ip, struct btstack * btstack)
3333 int psize = 288; /* initial in-line directory */
3334 struct metapage *mp;
3337 struct btframe *btsp;
3340 BT_CLR(btstack); /* reset stack */
3343 * descend leftmost path of the tree
3345 * by convention, root bn = 0.
3348 DT_GETPAGE(ip, bn, mp, psize, p, rc);
3353 * leftmost leaf page
3355 if (p->header.flag & BT_LEAF) {
3356 /* return leftmost entry */
3357 btsp = btstack->top;
3366 * descend down to leftmost child page
3368 if (BT_STACK_FULL(btstack)) {
3370 jfs_error(ip->i_sb, "dtReadFirst: btstack overrun");
3371 BT_STACK_DUMP(btstack);
3374 /* push (bn, index) of the parent page/entry */
3375 BT_PUSH(btstack, bn, 0);
3377 /* get the leftmost entry */
3378 stbl = DT_GETSTBL(p);
3379 xd = (pxd_t *) & p->slot[stbl[0]];
3381 /* get the child page block address */
3382 bn = addressPXD(xd);
3383 psize = lengthPXD(xd) << JFS_SBI(ip->i_sb)->l2bsize;
3385 /* unpin the parent page */
3394 * function: get the page of the specified offset (pn:index)
3396 * return: if (offset > eof), bn = -1;
3398 * note: if index > nextindex of the target leaf page,
3399 * start with 1st entry of next leaf page;
3401 static int dtReadNext(struct inode *ip, loff_t * offset,
3402 struct btstack * btstack)
3409 } *dtoffset = (struct dtoffset *) offset;
3411 struct metapage *mp;
3416 struct btframe *btsp, *parent;
3420 * get leftmost leaf page pinned
3422 if ((rc = dtReadFirst(ip, btstack)))
3426 DT_GETSEARCH(ip, btstack->top, bn, mp, p, index);
3428 /* get the start offset (pn:index) */
3429 pn = dtoffset->pn - 1; /* Now pn = 0 represents leftmost leaf */
3430 index = dtoffset->index;
3432 /* start at leftmost page ? */
3434 /* offset beyond eof ? */
3435 if (index < p->header.nextindex)
3438 if (p->header.flag & BT_ROOT) {
3443 /* start with 1st entry of next leaf page */
3445 dtoffset->index = index = 0;
3449 /* start at non-leftmost page: scan parent pages for large pn */
3450 if (p->header.flag & BT_ROOT) {
3455 /* start after next leaf page ? */
3459 /* get leaf page pn = 1 */
3461 bn = le64_to_cpu(p->header.next);
3463 /* unpin leaf page */
3466 /* offset beyond eof ? */
3475 * scan last internal page level to get target leaf page
3478 /* unpin leftmost leaf page */
3481 /* get left most parent page */
3482 btsp = btstack->top;
3485 DT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
3489 /* scan parent pages at last internal page level */
3490 while (pn >= p->header.nextindex) {
3491 pn -= p->header.nextindex;
3493 /* get next parent page address */
3494 bn = le64_to_cpu(p->header.next);
3496 /* unpin current parent page */
3499 /* offset beyond eof ? */
3505 /* get next parent page */
3506 DT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
3510 /* update parent page stack frame */
3514 /* get leaf page address */
3515 stbl = DT_GETSTBL(p);
3516 xd = (pxd_t *) & p->slot[stbl[pn]];
3517 bn = addressPXD(xd);
3519 /* unpin parent page */
3523 * get target leaf page
3526 DT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
3531 * leaf page has been completed:
3532 * start with 1st entry of next leaf page
3534 if (index >= p->header.nextindex) {
3535 bn = le64_to_cpu(p->header.next);
3537 /* unpin leaf page */
3540 /* offset beyond eof ? */
3546 /* get next leaf page */
3547 DT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
3551 /* start with 1st entry of next leaf page */
3553 dtoffset->index = 0;
3557 /* return target leaf page pinned */
3558 btsp = btstack->top;
3560 btsp->index = dtoffset->index;
3570 * function: compare search key with an internal entry
3573 * < 0 if k is < record
3574 * = 0 if k is = record
3575 * > 0 if k is > record
3577 static int dtCompare(struct component_name * key, /* search key */
3578 dtpage_t * p, /* directory page */
3580 { /* entry slot index */
3583 int klen, namlen, len, rc;
3584 struct idtentry *ih;
3588 * force the left-most key on internal pages, at any level of
3589 * the tree, to be less than any search key.
3590 * this obviates having to update the leftmost key on an internal
3591 * page when the user inserts a new key in the tree smaller than
3592 * anything that has been stored.
3594 * (? if/when dtSearch() narrows down to 1st entry (index = 0),
3595 * at any internal page at any level of the tree,
3596 * it descends to child of the entry anyway -
3597 * ? make the entry as min size dummy entry)
3599 * if (e->index == 0 && h->prevpg == P_INVALID && !(h->flags & BT_LEAF))
3606 ih = (struct idtentry *) & p->slot[si];
3609 namlen = ih->namlen;
3610 len = min(namlen, DTIHDRDATALEN);
3612 /* compare with head/only segment */
3613 len = min(klen, len);
3614 if ((rc = UniStrncmp_le(kname, name, len)))
3620 /* compare with additional segment(s) */
3622 while (klen > 0 && namlen > 0) {
3623 /* compare with next name segment */
3624 t = (struct dtslot *) & p->slot[si];
3625 len = min(namlen, DTSLOTDATALEN);
3626 len = min(klen, len);
3628 if ((rc = UniStrncmp_le(kname, name, len)))
3637 return (klen - namlen);
3646 * function: compare search key with an (leaf/internal) entry
3649 * < 0 if k is < record
3650 * = 0 if k is = record
3651 * > 0 if k is > record
3653 static int ciCompare(struct component_name * key, /* search key */
3654 dtpage_t * p, /* directory page */
3655 int si, /* entry slot index */
3660 int klen, namlen, len, rc;
3661 struct ldtentry *lh;
3662 struct idtentry *ih;
3667 * force the left-most key on internal pages, at any level of
3668 * the tree, to be less than any search key.
3669 * this obviates having to update the leftmost key on an internal
3670 * page when the user inserts a new key in the tree smaller than
3671 * anything that has been stored.
3673 * (? if/when dtSearch() narrows down to 1st entry (index = 0),
3674 * at any internal page at any level of the tree,
3675 * it descends to child of the entry anyway -
3676 * ? make the entry as min size dummy entry)
3678 * if (e->index == 0 && h->prevpg == P_INVALID && !(h->flags & BT_LEAF))
3688 if (p->header.flag & BT_LEAF) {
3689 lh = (struct ldtentry *) & p->slot[si];
3692 namlen = lh->namlen;
3693 if (flag & JFS_DIR_INDEX)
3694 len = min(namlen, DTLHDRDATALEN);
3696 len = min(namlen, DTLHDRDATALEN_LEGACY);
3699 * internal page entry
3702 ih = (struct idtentry *) & p->slot[si];
3705 namlen = ih->namlen;
3706 len = min(namlen, DTIHDRDATALEN);
3709 /* compare with head/only segment */
3710 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);
3717 if ((rc = *kname - x))
3724 /* compare with additional segment(s) */
3725 while (klen > 0 && namlen > 0) {
3726 /* compare with next name segment */
3727 t = (struct dtslot *) & p->slot[si];
3728 len = min(namlen, DTSLOTDATALEN);
3729 len = min(klen, len);
3731 for (i = 0; i < len; i++, kname++, name++) {
3732 /* only uppercase if case-insensitive support is on */
3733 if ((flag & JFS_OS2) == JFS_OS2)
3734 x = UniToupper(le16_to_cpu(*name));
3736 x = le16_to_cpu(*name);
3738 if ((rc = *kname - x))
3747 return (klen - namlen);
3752 * ciGetLeafPrefixKey()
3754 * function: compute prefix of suffix compression
3755 * from two adjacent leaf entries
3756 * across page boundary
3758 * return: non-zero on error
3761 static int ciGetLeafPrefixKey(dtpage_t * lp, int li, dtpage_t * rp,
3762 int ri, struct component_name * key, int flag)
3765 wchar_t *pl, *pr, *kname;
3766 struct component_name lkey;
3767 struct component_name rkey;
3769 lkey.name = (wchar_t *) kmalloc((JFS_NAME_MAX + 1) * sizeof(wchar_t),
3771 if (lkey.name == NULL)
3774 rkey.name = (wchar_t *) kmalloc((JFS_NAME_MAX + 1) * sizeof(wchar_t),
3776 if (rkey.name == NULL) {
3781 /* get left and right key */
3782 dtGetKey(lp, li, &lkey, flag);
3783 lkey.name[lkey.namlen] = 0;
3785 if ((flag & JFS_OS2) == JFS_OS2)
3788 dtGetKey(rp, ri, &rkey, flag);
3789 rkey.name[rkey.namlen] = 0;
3792 if ((flag & JFS_OS2) == JFS_OS2)
3795 /* compute prefix */
3798 namlen = min(lkey.namlen, rkey.namlen);
3799 for (pl = lkey.name, pr = rkey.name;
3800 namlen; pl++, pr++, namlen--, klen++, kname++) {
3803 key->namlen = klen + 1;
3808 /* l->namlen <= r->namlen since l <= r */
3809 if (lkey.namlen < rkey.namlen) {
3811 key->namlen = klen + 1;
3812 } else /* l->namelen == r->namelen */
3826 * function: get key of the entry
3828 static void dtGetKey(dtpage_t * p, int i, /* entry index */
3829 struct component_name * key, int flag)
3833 struct ldtentry *lh;
3834 struct idtentry *ih;
3841 stbl = DT_GETSTBL(p);
3843 if (p->header.flag & BT_LEAF) {
3844 lh = (struct ldtentry *) & p->slot[si];
3846 namlen = lh->namlen;
3848 if (flag & JFS_DIR_INDEX)
3849 len = min(namlen, DTLHDRDATALEN);
3851 len = min(namlen, DTLHDRDATALEN_LEGACY);
3853 ih = (struct idtentry *) & p->slot[si];
3855 namlen = ih->namlen;
3857 len = min(namlen, DTIHDRDATALEN);
3860 key->namlen = namlen;
3864 * move head/only segment
3866 UniStrncpy_from_le(kname, name, len);
3869 * move additional segment(s)
3872 /* get next segment */
3876 len = min(namlen, DTSLOTDATALEN);
3877 UniStrncpy_from_le(kname, t->name, len);
3887 * function: allocate free slot(s) and
3888 * write a leaf/internal entry
3890 * return: entry slot index
3892 static void dtInsertEntry(dtpage_t * p, int index, struct component_name * key,
3893 ddata_t * data, struct dt_lock ** dtlock)
3895 struct dtslot *h, *t;
3896 struct ldtentry *lh = NULL;
3897 struct idtentry *ih = NULL;
3898 int hsi, fsi, klen, len, nextindex;
3903 struct dt_lock *dtlck = *dtlock;
3907 struct metapage *mp = NULL;
3912 /* allocate a free slot */
3913 hsi = fsi = p->header.freelist;
3915 p->header.freelist = h->next;
3916 --p->header.freecnt;
3918 /* open new linelock */
3919 if (dtlck->index >= dtlck->maxcnt)
3920 dtlck = (struct dt_lock *) txLinelock(dtlck);
3922 lv = & dtlck->lv[dtlck->index];
3925 /* write head/only segment */
3926 if (p->header.flag & BT_LEAF) {
3927 lh = (struct ldtentry *) h;
3929 lh->inumber = cpu_to_le32(data->leaf.ino);
3932 if (data->leaf.ip) {
3933 len = min(klen, DTLHDRDATALEN);
3934 if (!(p->header.flag & BT_ROOT))
3935 bn = addressPXD(&p->header.self);
3936 lh->index = cpu_to_le32(add_index(data->leaf.tid,
3940 len = min(klen, DTLHDRDATALEN_LEGACY);
3942 ih = (struct idtentry *) h;
3948 len = min(klen, DTIHDRDATALEN);
3951 UniStrncpy_to_le(name, kname, len);
3956 /* write additional segment(s) */
3961 fsi = p->header.freelist;
3963 p->header.freelist = t->next;
3964 --p->header.freecnt;
3966 /* is next slot contiguous ? */
3967 if (fsi != xsi + 1) {
3968 /* close current linelock */
3972 /* open new linelock */
3973 if (dtlck->index < dtlck->maxcnt)
3976 dtlck = (struct dt_lock *) txLinelock(dtlck);
3977 lv = & dtlck->lv[0];
3985 len = min(klen, DTSLOTDATALEN);
3986 UniStrncpy_to_le(t->name, kname, len);
3993 /* close current linelock */
3999 /* terminate last/only segment */
4001 /* single segment entry */
4002 if (p->header.flag & BT_LEAF)
4007 /* multi-segment entry */
4010 /* if insert into middle, shift right succeeding entries in stbl */
4011 stbl = DT_GETSTBL(p);
4012 nextindex = p->header.nextindex;
4013 if (index < nextindex) {
4014 memmove(stbl + index + 1, stbl + index, nextindex - index);
4016 if ((p->header.flag & BT_LEAF) && data->leaf.ip) {
4020 * Need to update slot number for entries that moved
4024 for (n = index + 1; n <= nextindex; n++) {
4025 lh = (struct ldtentry *) & (p->slot[stbl[n]]);
4026 modify_index(data->leaf.tid, data->leaf.ip,
4027 le32_to_cpu(lh->index), bn, n,
4031 release_metapage(mp);
4037 /* advance next available entry index of stbl */
4038 ++p->header.nextindex;
4045 * function: move entries from split/left page to new/right page
4047 * nextindex of dst page and freelist/freecnt of both pages
4050 static void dtMoveEntry(dtpage_t * sp, int si, dtpage_t * dp,
4051 struct dt_lock ** sdtlock, struct dt_lock ** ddtlock,
4054 int ssi, next; /* src slot index */
4055 int di; /* dst entry index */
4056 int dsi; /* dst slot index */
4057 s8 *sstbl, *dstbl; /* sorted entry table */
4059 struct ldtentry *slh, *dlh = NULL;
4060 struct idtentry *sih, *dih = NULL;
4061 struct dtslot *h, *s, *d;
4062 struct dt_lock *sdtlck = *sdtlock, *ddtlck = *ddtlock;
4063 struct lv *slv, *dlv;
4067 sstbl = (s8 *) & sp->slot[sp->header.stblindex];
4068 dstbl = (s8 *) & dp->slot[dp->header.stblindex];
4070 dsi = dp->header.freelist; /* first (whole page) free slot */
4071 sfsi = sp->header.freelist;
4073 /* linelock destination entry slot */
4074 dlv = & ddtlck->lv[ddtlck->index];
4077 /* linelock source entry slot */
4078 slv = & sdtlck->lv[sdtlck->index];
4079 slv->offset = sstbl[si];
4080 xssi = slv->offset - 1;
4086 for (di = 0; si < sp->header.nextindex; si++, di++) {
4090 /* is next slot contiguous ? */
4091 if (ssi != xssi + 1) {
4092 /* close current linelock */
4096 /* open new linelock */
4097 if (sdtlck->index < sdtlck->maxcnt)
4100 sdtlck = (struct dt_lock *) txLinelock(sdtlck);
4101 slv = & sdtlck->lv[0];
4109 * move head/only segment of an entry
4112 h = d = &dp->slot[dsi];
4114 /* get src slot and move */
4116 if (sp->header.flag & BT_LEAF) {
4117 /* get source entry */
4118 slh = (struct ldtentry *) s;
4119 dlh = (struct ldtentry *) h;
4120 snamlen = slh->namlen;
4123 len = min(snamlen, DTLHDRDATALEN);
4124 dlh->index = slh->index; /* little-endian */
4126 len = min(snamlen, DTLHDRDATALEN_LEGACY);
4128 memcpy(dlh, slh, 6 + len * 2);
4132 /* update dst head/only segment next field */
4136 sih = (struct idtentry *) s;
4137 snamlen = sih->namlen;
4139 len = min(snamlen, DTIHDRDATALEN);
4140 dih = (struct idtentry *) h;
4141 memcpy(dih, sih, 10 + len * 2);
4148 /* free src head/only segment */
4158 * move additional segment(s) of the entry
4161 while ((ssi = next) >= 0) {
4162 /* is next slot contiguous ? */
4163 if (ssi != xssi + 1) {
4164 /* close current linelock */
4168 /* open new linelock */
4169 if (sdtlck->index < sdtlck->maxcnt)
4175 slv = & sdtlck->lv[0];
4182 /* get next source segment */
4185 /* get next destination free slot */
4188 len = min(snamlen, DTSLOTDATALEN);
4189 UniStrncpy_le(d->name, s->name, len);
4198 /* free source segment */
4207 /* terminate dst last/only segment */
4209 /* single segment entry */
4210 if (dp->header.flag & BT_LEAF)
4215 /* multi-segment entry */
4219 /* close current linelock */
4228 /* update source header */
4229 sp->header.freelist = sfsi;
4230 sp->header.freecnt += nd;
4232 /* update destination header */
4233 dp->header.nextindex = di;
4235 dp->header.freelist = dsi;
4236 dp->header.freecnt -= nd;
4243 * function: free a (leaf/internal) entry
4245 * log freelist header, stbl, and each segment slot of entry
4246 * (even though last/only segment next field is modified,
4247 * physical image logging requires all segment slots of
4248 * the entry logged to avoid applying previous updates
4249 * to the same slots)
4251 static void dtDeleteEntry(dtpage_t * p, int fi, struct dt_lock ** dtlock)
4253 int fsi; /* free entry slot index */
4257 struct dt_lock *dtlck = *dtlock;
4261 /* get free entry slot index */
4262 stbl = DT_GETSTBL(p);
4265 /* open new linelock */
4266 if (dtlck->index >= dtlck->maxcnt)
4267 dtlck = (struct dt_lock *) txLinelock(dtlck);
4268 lv = & dtlck->lv[dtlck->index];
4272 /* get the head/only segment */
4274 if (p->header.flag & BT_LEAF)
4275 si = ((struct ldtentry *) t)->next;
4277 si = ((struct idtentry *) t)->next;
4284 /* find the last/only segment */
4286 /* is next slot contiguous ? */
4287 if (si != xsi + 1) {
4288 /* close current linelock */
4292 /* open new linelock */
4293 if (dtlck->index < dtlck->maxcnt)
4296 dtlck = (struct dt_lock *) txLinelock(dtlck);
4297 lv = & dtlck->lv[0];
4313 /* close current linelock */
4319 /* update freelist */
4320 t->next = p->header.freelist;
4321 p->header.freelist = fsi;
4322 p->header.freecnt += freecnt;
4324 /* if delete from middle,
4325 * shift left the succedding entries in the stbl
4327 si = p->header.nextindex;
4329 memmove(&stbl[fi], &stbl[fi + 1], si - fi - 1);
4331 p->header.nextindex--;
4338 * function: truncate a (leaf/internal) entry
4340 * log freelist header, stbl, and each segment slot of entry
4341 * (even though last/only segment next field is modified,
4342 * physical image logging requires all segment slots of
4343 * the entry logged to avoid applying previous updates
4344 * to the same slots)
4346 static void dtTruncateEntry(dtpage_t * p, int ti, struct dt_lock ** dtlock)
4348 int tsi; /* truncate entry slot index */
4352 struct dt_lock *dtlck = *dtlock;
4356 /* get free entry slot index */
4357 stbl = DT_GETSTBL(p);
4360 /* open new linelock */
4361 if (dtlck->index >= dtlck->maxcnt)
4362 dtlck = (struct dt_lock *) txLinelock(dtlck);
4363 lv = & dtlck->lv[dtlck->index];
4367 /* get the head/only segment */
4369 ASSERT(p->header.flag & BT_INTERNAL);
4370 ((struct idtentry *) t)->namlen = 0;
4371 si = ((struct idtentry *) t)->next;
4372 ((struct idtentry *) t)->next = -1;
4379 /* find the last/only segment */
4381 /* is next slot contiguous ? */
4382 if (si != xsi + 1) {
4383 /* close current linelock */
4387 /* open new linelock */
4388 if (dtlck->index < dtlck->maxcnt)
4391 dtlck = (struct dt_lock *) txLinelock(dtlck);
4392 lv = & dtlck->lv[0];
4408 /* close current linelock */
4414 /* update freelist */
4417 t->next = p->header.freelist;
4418 p->header.freelist = fsi;
4419 p->header.freecnt += freecnt;
4424 * dtLinelockFreelist()
4426 static void dtLinelockFreelist(dtpage_t * p, /* directory page */
4427 int m, /* max slot index */
4428 struct dt_lock ** dtlock)
4430 int fsi; /* free entry slot index */
4433 struct dt_lock *dtlck = *dtlock;
4437 /* get free entry slot index */
4438 fsi = p->header.freelist;
4440 /* open new linelock */
4441 if (dtlck->index >= dtlck->maxcnt)
4442 dtlck = (struct dt_lock *) txLinelock(dtlck);
4443 lv = & dtlck->lv[dtlck->index];
4453 /* find the last/only segment */
4454 while (si < m && si >= 0) {
4455 /* is next slot contiguous ? */
4456 if (si != xsi + 1) {
4457 /* close current linelock */
4461 /* open new linelock */
4462 if (dtlck->index < dtlck->maxcnt)
4465 dtlck = (struct dt_lock *) txLinelock(dtlck);
4466 lv = & dtlck->lv[0];
4480 /* close current linelock */
4491 * FUNCTION: Modify the inode number part of a directory entry
4494 * tid - Transaction id
4495 * ip - Inode of parent directory
4496 * key - Name of entry to be modified
4497 * orig_ino - Original inode number expected in entry
4498 * new_ino - New inode number to put into entry
4502 * -ESTALE - If entry found does not match orig_ino passed in
4503 * -ENOENT - If no entry can be found to match key
4504 * 0 - If successfully modified entry
4506 int dtModify(tid_t tid, struct inode *ip,
4507 struct component_name * key, ino_t * orig_ino, ino_t new_ino, int flag)
4511 struct metapage *mp;
4514 struct btstack btstack;
4516 struct dt_lock *dtlck;
4519 int entry_si; /* entry slot index */
4520 struct ldtentry *entry;
4523 * search for the entry to modify:
4525 * dtSearch() returns (leaf page pinned, index at which to modify).
4527 if ((rc = dtSearch(ip, key, orig_ino, &btstack, flag)))
4530 /* retrieve search result */
4531 DT_GETSEARCH(ip, btstack.top, bn, mp, p, index);
4533 BT_MARK_DIRTY(mp, ip);
4535 * acquire a transaction lock on the leaf page of named entry
4537 tlck = txLock(tid, ip, mp, tlckDTREE | tlckENTRY);
4538 dtlck = (struct dt_lock *) & tlck->lock;
4540 /* get slot index of the entry */
4541 stbl = DT_GETSTBL(p);
4542 entry_si = stbl[index];
4544 /* linelock entry */
4545 ASSERT(dtlck->index == 0);
4546 lv = & dtlck->lv[0];
4547 lv->offset = entry_si;
4551 /* get the head/only segment */
4552 entry = (struct ldtentry *) & p->slot[entry_si];
4554 /* substitute the inode number of the entry */
4555 entry->inumber = cpu_to_le32(new_ino);
4557 /* unpin the leaf page */