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 <linux/vs_dlimit.h>
106 #include "jfs_incore.h"
107 #include "jfs_superblock.h"
108 #include "jfs_filsys.h"
109 #include "jfs_metapage.h"
110 #include "jfs_dmap.h"
111 #include "jfs_unicode.h"
112 #include "jfs_debug.h"
114 /* dtree split parameter */
119 struct component_name *key;
121 struct pxdlist *pxdlist;
124 #define DT_PAGE(IP, MP) BT_PAGE(IP, MP, dtpage_t, i_dtroot)
126 /* get page buffer for specified block address */
127 #define DT_GETPAGE(IP, BN, MP, SIZE, P, RC)\
129 BT_GETPAGE(IP, BN, MP, dtpage_t, SIZE, P, RC, i_dtroot)\
132 if (((P)->header.nextindex > (((BN)==0)?DTROOTMAXSLOT:(P)->header.maxslot)) ||\
133 ((BN) && ((P)->header.maxslot > DTPAGEMAXSLOT)))\
136 jfs_error((IP)->i_sb, "DT_GETPAGE: dtree page corrupt");\
143 /* for consistency */
144 #define DT_PUTPAGE(MP) BT_PUTPAGE(MP)
146 #define DT_GETSEARCH(IP, LEAF, BN, MP, P, INDEX) \
147 BT_GETSEARCH(IP, LEAF, BN, MP, dtpage_t, P, INDEX, i_dtroot)
152 static int dtSplitUp(tid_t tid, struct inode *ip,
153 struct dtsplit * split, struct btstack * btstack);
155 static int dtSplitPage(tid_t tid, struct inode *ip, struct dtsplit * split,
156 struct metapage ** rmpp, dtpage_t ** rpp, pxd_t * rxdp);
158 static int dtExtendPage(tid_t tid, struct inode *ip,
159 struct dtsplit * split, struct btstack * btstack);
161 static int dtSplitRoot(tid_t tid, struct inode *ip,
162 struct dtsplit * split, struct metapage ** rmpp);
164 static int dtDeleteUp(tid_t tid, struct inode *ip, struct metapage * fmp,
165 dtpage_t * fp, struct btstack * btstack);
167 static int dtRelink(tid_t tid, struct inode *ip, dtpage_t * p);
169 static int dtReadFirst(struct inode *ip, struct btstack * btstack);
171 static int dtReadNext(struct inode *ip,
172 loff_t * offset, struct btstack * btstack);
174 static int dtCompare(struct component_name * key, dtpage_t * p, int si);
176 static int ciCompare(struct component_name * key, dtpage_t * p, int si,
179 static void dtGetKey(dtpage_t * p, int i, struct component_name * key,
182 static int ciGetLeafPrefixKey(dtpage_t * lp, int li, dtpage_t * rp,
183 int ri, struct component_name * key, int flag);
185 static void dtInsertEntry(dtpage_t * p, int index, struct component_name * key,
186 ddata_t * data, struct dt_lock **);
188 static void dtMoveEntry(dtpage_t * sp, int si, dtpage_t * dp,
189 struct dt_lock ** sdtlock, struct dt_lock ** ddtlock,
192 static void dtDeleteEntry(dtpage_t * p, int fi, struct dt_lock ** dtlock);
194 static void dtTruncateEntry(dtpage_t * p, int ti, struct dt_lock ** dtlock);
196 static void dtLinelockFreelist(dtpage_t * p, int m, struct dt_lock ** dtlock);
198 #define ciToUpper(c) UniStrupr((c)->name)
203 * Reads a page of a directory's index table.
204 * Having metadata mapped into the directory inode's address space
205 * presents a multitude of problems. We avoid this by mapping to
206 * the absolute address space outside of the *_metapage routines
208 static struct metapage *read_index_page(struct inode *inode, s64 blkno)
215 rc = xtLookup(inode, blkno, 1, &xflag, &xaddr, &xlen, 1);
216 if (rc || (xaddr == 0))
219 return read_metapage(inode, xaddr, PSIZE, 1);
225 * Same as get_index_page(), but get's a new page without reading
227 static struct metapage *get_index_page(struct inode *inode, s64 blkno)
234 rc = xtLookup(inode, blkno, 1, &xflag, &xaddr, &xlen, 1);
235 if (rc || (xaddr == 0))
238 return get_metapage(inode, xaddr, PSIZE, 1);
244 * Returns dtree page containing directory table entry for specified
245 * index and pointer to its entry.
247 * mp must be released by caller.
249 static struct dir_table_slot *find_index(struct inode *ip, u32 index,
250 struct metapage ** mp, s64 *lblock)
252 struct jfs_inode_info *jfs_ip = JFS_IP(ip);
256 struct dir_table_slot *slot;
257 static int maxWarnings = 10;
261 jfs_warn("find_entry called with index = %d", index);
267 if (index >= jfs_ip->next_index) {
268 jfs_warn("find_entry called with index >= next_index");
272 if (jfs_dirtable_inline(ip)) {
274 * Inline directory table
277 slot = &jfs_ip->i_dirtable[index - 2];
279 offset = (index - 2) * sizeof(struct dir_table_slot);
280 page_offset = offset & (PSIZE - 1);
281 blkno = ((offset + 1) >> L2PSIZE) <<
282 JFS_SBI(ip->i_sb)->l2nbperpage;
284 if (*mp && (*lblock != blkno)) {
285 release_metapage(*mp);
290 *mp = read_index_page(ip, blkno);
293 jfs_err("free_index: error reading directory table");
298 (struct dir_table_slot *) ((char *) (*mp)->data +
304 static inline void lock_index(tid_t tid, struct inode *ip, struct metapage * mp,
308 struct linelock *llck;
311 tlck = txLock(tid, ip, mp, tlckDATA);
312 llck = (struct linelock *) tlck->lock;
314 if (llck->index >= llck->maxcnt)
315 llck = txLinelock(llck);
316 lv = &llck->lv[llck->index];
319 * Linelock slot size is twice the size of directory table
320 * slot size. 512 entries per page.
322 lv->offset = ((index - 2) & 511) >> 1;
330 * Adds an entry to the directory index table. This is used to provide
331 * each directory entry with a persistent index in which to resume
332 * directory traversals
334 static u32 add_index(tid_t tid, struct inode *ip, s64 bn, int slot)
336 struct super_block *sb = ip->i_sb;
337 struct jfs_sb_info *sbi = JFS_SBI(sb);
338 struct jfs_inode_info *jfs_ip = JFS_IP(ip);
340 struct dir_table_slot *dirtab_slot;
342 struct linelock *llck;
350 ASSERT(DO_INDEX(ip));
352 if (jfs_ip->next_index < 2) {
353 jfs_warn("add_index: next_index = %d. Resetting!",
355 jfs_ip->next_index = 2;
358 index = jfs_ip->next_index++;
360 if (index <= MAX_INLINE_DIRTABLE_ENTRY) {
362 * i_size reflects size of index table, or 8 bytes per entry.
364 ip->i_size = (loff_t) (index - 1) << 3;
367 * dir table fits inline within inode
369 dirtab_slot = &jfs_ip->i_dirtable[index-2];
370 dirtab_slot->flag = DIR_INDEX_VALID;
371 dirtab_slot->slot = slot;
372 DTSaddress(dirtab_slot, bn);
374 set_cflag(COMMIT_Dirtable, ip);
378 if (index == (MAX_INLINE_DIRTABLE_ENTRY + 1)) {
379 struct dir_table_slot temp_table[12];
382 * It's time to move the inline table to an external
383 * page and begin to build the xtree
385 if (DQUOT_ALLOC_BLOCK(ip, sbi->nbperpage))
387 if (DLIMIT_ALLOC_BLOCK(ip, sbi->nbperpage))
389 if (dbAlloc(ip, 0, sbi->nbperpage, &xaddr))
390 goto clean_up_dlimit;
393 * Save the table, we're going to overwrite it with the
396 memcpy(temp_table, &jfs_ip->i_dirtable, sizeof(temp_table));
399 * Initialize empty x-tree
404 * Add the first block to the xtree
406 if (xtInsert(tid, ip, 0, 0, sbi->nbperpage, &xaddr, 0)) {
407 /* This really shouldn't fail */
408 jfs_warn("add_index: xtInsert failed!");
409 memcpy(&jfs_ip->i_dirtable, temp_table,
410 sizeof (temp_table));
411 dbFree(ip, xaddr, sbi->nbperpage);
412 DQUOT_FREE_BLOCK(ip, sbi->nbperpage);
417 if ((mp = get_index_page(ip, 0)) == 0) {
418 jfs_err("add_index: get_metapage failed!");
419 xtTruncate(tid, ip, 0, COMMIT_PWMAP);
420 memcpy(&jfs_ip->i_dirtable, temp_table,
421 sizeof (temp_table));
424 tlck = txLock(tid, ip, mp, tlckDATA);
425 llck = (struct linelock *) & tlck->lock;
426 ASSERT(llck->index == 0);
430 lv->length = 6; /* tlckDATA slot size is 16 bytes */
433 memcpy(mp->data, temp_table, sizeof(temp_table));
435 mark_metapage_dirty(mp);
436 release_metapage(mp);
439 * Logging is now directed by xtree tlocks
441 clear_cflag(COMMIT_Dirtable, ip);
444 offset = (index - 2) * sizeof(struct dir_table_slot);
445 page_offset = offset & (PSIZE - 1);
446 blkno = ((offset + 1) >> L2PSIZE) << sbi->l2nbperpage;
447 if (page_offset == 0) {
449 * This will be the beginning of a new page
452 if (xtInsert(tid, ip, 0, blkno, sbi->nbperpage, &xaddr, 0)) {
453 jfs_warn("add_index: xtInsert failed!");
458 if ((mp = get_index_page(ip, blkno)))
459 memset(mp->data, 0, PSIZE); /* Just looks better */
461 xtTruncate(tid, ip, offset, COMMIT_PWMAP);
463 mp = read_index_page(ip, blkno);
466 jfs_err("add_index: get/read_metapage failed!");
470 lock_index(tid, ip, mp, index);
473 (struct dir_table_slot *) ((char *) mp->data + page_offset);
474 dirtab_slot->flag = DIR_INDEX_VALID;
475 dirtab_slot->slot = slot;
476 DTSaddress(dirtab_slot, bn);
478 mark_metapage_dirty(mp);
479 release_metapage(mp);
484 DLIMIT_FREE_BLOCK(ip, sbi->nbperpage);
487 DQUOT_FREE_BLOCK(ip, sbi->nbperpage);
491 jfs_ip->next_index--;
499 * Marks an entry to the directory index table as free.
501 static void free_index(tid_t tid, struct inode *ip, u32 index, u32 next)
503 struct dir_table_slot *dirtab_slot;
505 struct metapage *mp = NULL;
507 dirtab_slot = find_index(ip, index, &mp, &lblock);
509 if (dirtab_slot == 0)
512 dirtab_slot->flag = DIR_INDEX_FREE;
513 dirtab_slot->slot = dirtab_slot->addr1 = 0;
514 dirtab_slot->addr2 = cpu_to_le32(next);
517 lock_index(tid, ip, mp, index);
518 mark_metapage_dirty(mp);
519 release_metapage(mp);
521 set_cflag(COMMIT_Dirtable, ip);
527 * Changes an entry in the directory index table
529 static void modify_index(tid_t tid, struct inode *ip, u32 index, s64 bn,
530 int slot, struct metapage ** mp, u64 *lblock)
532 struct dir_table_slot *dirtab_slot;
534 dirtab_slot = find_index(ip, index, mp, lblock);
536 if (dirtab_slot == 0)
539 DTSaddress(dirtab_slot, bn);
540 dirtab_slot->slot = slot;
543 lock_index(tid, ip, *mp, index);
544 mark_metapage_dirty(*mp);
546 set_cflag(COMMIT_Dirtable, ip);
552 * reads a directory table slot
554 static int read_index(struct inode *ip, u32 index,
555 struct dir_table_slot * dirtab_slot)
558 struct metapage *mp = NULL;
559 struct dir_table_slot *slot;
561 slot = find_index(ip, index, &mp, &lblock);
566 memcpy(dirtab_slot, slot, sizeof(struct dir_table_slot));
569 release_metapage(mp);
578 * Search for the entry with specified key
582 * return: 0 - search result on stack, leaf page pinned;
585 int dtSearch(struct inode *ip, struct component_name * key, ino_t * data,
586 struct btstack * btstack, int flag)
589 int cmp = 1; /* init for empty page */
594 int base, index, lim;
595 struct btframe *btsp;
597 int psize = 288; /* initial in-line directory */
599 struct component_name ciKey;
600 struct super_block *sb = ip->i_sb;
603 (wchar_t *) kmalloc((JFS_NAME_MAX + 1) * sizeof(wchar_t),
605 if (ciKey.name == 0) {
611 /* uppercase search key for c-i directory */
612 UniStrcpy(ciKey.name, key->name);
613 ciKey.namlen = key->namlen;
615 /* only uppercase if case-insensitive support is on */
616 if ((JFS_SBI(sb)->mntflag & JFS_OS2) == JFS_OS2) {
619 BT_CLR(btstack); /* reset stack */
621 /* init level count for max pages to split */
625 * search down tree from root:
627 * between two consecutive entries of <Ki, Pi> and <Kj, Pj> of
628 * internal page, child page Pi contains entry with k, Ki <= K < Kj.
630 * if entry with search key K is not found
631 * internal page search find the entry with largest key Ki
632 * less than K which point to the child page to search;
633 * leaf page search find the entry with smallest key Kj
634 * greater than K so that the returned index is the position of
635 * the entry to be shifted right for insertion of new entry.
636 * for empty tree, search key is greater than any key of the tree.
638 * by convention, root bn = 0.
641 /* get/pin the page to search */
642 DT_GETPAGE(ip, bn, mp, psize, p, rc);
646 /* get sorted entry table of the page */
647 stbl = DT_GETSTBL(p);
650 * binary search with search key K on the current page.
652 for (base = 0, lim = p->header.nextindex; lim; lim >>= 1) {
653 index = base + (lim >> 1);
655 if (p->header.flag & BT_LEAF) {
656 /* uppercase leaf name to compare */
658 ciCompare(&ciKey, p, stbl[index],
659 JFS_SBI(sb)->mntflag);
661 /* router key is in uppercase */
663 cmp = dtCompare(&ciKey, p, stbl[index]);
671 /* search hit - leaf page:
672 * return the entry found
674 if (p->header.flag & BT_LEAF) {
675 inumber = le32_to_cpu(
676 ((struct ldtentry *) & p->slot[stbl[index]])->inumber);
679 * search for JFS_LOOKUP
681 if (flag == JFS_LOOKUP) {
688 * search for JFS_CREATE
690 if (flag == JFS_CREATE) {
697 * search for JFS_REMOVE or JFS_RENAME
699 if ((flag == JFS_REMOVE ||
700 flag == JFS_RENAME) &&
707 * JFS_REMOVE|JFS_FINDDIR|JFS_RENAME
709 /* save search result */
720 /* search hit - internal page:
721 * descend/search its child page
735 * base is the smallest index with key (Kj) greater than
736 * search key (K) and may be zero or (maxindex + 1) index.
739 * search miss - leaf page
741 * return location of entry (base) where new entry with
742 * search key K is to be inserted.
744 if (p->header.flag & BT_LEAF) {
746 * search for JFS_LOOKUP, JFS_REMOVE, or JFS_RENAME
748 if (flag == JFS_LOOKUP || flag == JFS_REMOVE ||
749 flag == JFS_RENAME) {
755 * search for JFS_CREATE|JFS_FINDDIR:
770 * search miss - internal page
772 * if base is non-zero, decrement base by one to get the parent
773 * entry of the child page to search.
775 index = base ? base - 1 : base;
778 * go down to child page
781 /* update max. number of pages to split */
782 if (BT_STACK_FULL(btstack)) {
783 /* Something's corrupted, mark filesytem dirty so
784 * chkdsk will fix it.
786 jfs_error(sb, "stack overrun in dtSearch!");
787 BT_STACK_DUMP(btstack);
793 /* push (bn, index) of the parent page/entry */
794 BT_PUSH(btstack, bn, index);
796 /* get the child page block number */
797 pxd = (pxd_t *) & p->slot[stbl[index]];
798 bn = addressPXD(pxd);
799 psize = lengthPXD(pxd) << JFS_SBI(ip->i_sb)->l2bsize;
801 /* unpin the parent page */
821 * function: insert an entry to directory tree
825 * return: 0 - success;
828 int dtInsert(tid_t tid, struct inode *ip,
829 struct component_name * name, ino_t * fsn, struct btstack * btstack)
832 struct metapage *mp; /* meta-page buffer */
833 dtpage_t *p; /* base B+-tree index page */
836 struct dtsplit split; /* split information */
838 struct dt_lock *dtlck;
844 * retrieve search result
846 * dtSearch() returns (leaf page pinned, index at which to insert).
847 * n.b. dtSearch() may return index of (maxindex + 1) of
850 DT_GETSEARCH(ip, btstack->top, bn, mp, p, index);
853 * insert entry for new key
856 if (JFS_IP(ip)->next_index == DIREND) {
860 n = NDTLEAF(name->namlen);
864 n = NDTLEAF_LEGACY(name->namlen);
865 data.leaf.ip = NULL; /* signifies legacy directory format */
867 data.leaf.ino = *fsn;
870 * leaf page does not have enough room for new entry:
872 * extend/split the leaf page;
874 * dtSplitUp() will insert the entry and unpin the leaf page.
876 if (n > p->header.freecnt) {
882 rc = dtSplitUp(tid, ip, &split, btstack);
887 * leaf page does have enough room for new entry:
889 * insert the new data entry into the leaf page;
891 BT_MARK_DIRTY(mp, ip);
893 * acquire a transaction lock on the leaf page
895 tlck = txLock(tid, ip, mp, tlckDTREE | tlckENTRY);
896 dtlck = (struct dt_lock *) & tlck->lock;
897 ASSERT(dtlck->index == 0);
900 /* linelock header */
905 dtInsertEntry(p, index, name, &data, &dtlck);
907 /* linelock stbl of non-root leaf page */
908 if (!(p->header.flag & BT_ROOT)) {
909 if (dtlck->index >= dtlck->maxcnt)
910 dtlck = (struct dt_lock *) txLinelock(dtlck);
911 lv = & dtlck->lv[dtlck->index];
912 n = index >> L2DTSLOTSIZE;
913 lv->offset = p->header.stblindex + n;
915 ((p->header.nextindex - 1) >> L2DTSLOTSIZE) - n + 1;
919 /* unpin the leaf page */
929 * function: propagate insertion bottom up;
933 * return: 0 - success;
935 * leaf page unpinned;
937 static int dtSplitUp(tid_t tid,
938 struct inode *ip, struct dtsplit * split, struct btstack * btstack)
940 struct jfs_sb_info *sbi = JFS_SBI(ip->i_sb);
942 struct metapage *smp;
943 dtpage_t *sp; /* split page */
944 struct metapage *rmp;
945 dtpage_t *rp; /* new right page split from sp */
946 pxd_t rpxd; /* new right page extent descriptor */
947 struct metapage *lmp;
948 dtpage_t *lp; /* left child page */
949 int skip; /* index of entry of insertion */
950 struct btframe *parent; /* parent page entry on traverse stack */
953 struct pxdlist pxdlist;
955 struct component_name key = { 0, NULL };
956 ddata_t *data = split->data;
958 struct dt_lock *dtlck;
961 int quota_allocation = 0;
962 int dlimit_allocation = 0;
966 sp = DT_PAGE(ip, smp);
969 (wchar_t *) kmalloc((JFS_NAME_MAX + 2) * sizeof(wchar_t),
980 * The split routines insert the new entry, and
981 * acquire txLock as appropriate.
984 * split root leaf page:
986 if (sp->header.flag & BT_ROOT) {
988 * allocate a single extent child page
991 n = sbi->bsize >> L2DTSLOTSIZE;
992 n -= (n + 31) >> L2DTSLOTSIZE; /* stbl size */
993 n -= DTROOTMAXSLOT - sp->header.freecnt; /* header + entries */
994 if (n <= split->nslot)
996 if ((rc = dbAlloc(ip, 0, (s64) xlen, &xaddr))) {
1001 pxdlist.maxnpxd = 1;
1003 pxd = &pxdlist.pxd[0];
1004 PXDaddress(pxd, xaddr);
1005 PXDlength(pxd, xlen);
1006 split->pxdlist = &pxdlist;
1007 rc = dtSplitRoot(tid, ip, split, &rmp);
1010 dbFree(ip, xaddr, xlen);
1017 ip->i_size = xlen << sbi->l2bsize;
1023 * extend first leaf page
1025 * extend the 1st extent if less than buffer page size
1026 * (dtExtendPage() reurns leaf page unpinned)
1028 pxd = &sp->header.self;
1029 xlen = lengthPXD(pxd);
1030 xsize = xlen << sbi->l2bsize;
1031 if (xsize < PSIZE) {
1032 xaddr = addressPXD(pxd);
1033 n = xsize >> L2DTSLOTSIZE;
1034 n -= (n + 31) >> L2DTSLOTSIZE; /* stbl size */
1035 if ((n + sp->header.freecnt) <= split->nslot)
1036 n = xlen + (xlen << 1);
1040 /* Allocate blocks to quota. */
1041 if (DQUOT_ALLOC_BLOCK(ip, n)) {
1045 quota_allocation += n;
1047 if (DLIMIT_ALLOC_BLOCK(ip, n)) {
1051 dlimit_allocation += n;
1053 if ((rc = dbReAlloc(sbi->ipbmap, xaddr, (s64) xlen,
1057 pxdlist.maxnpxd = 1;
1059 pxd = &pxdlist.pxd[0];
1060 PXDaddress(pxd, nxaddr)
1061 PXDlength(pxd, xlen + n);
1062 split->pxdlist = &pxdlist;
1063 if ((rc = dtExtendPage(tid, ip, split, btstack))) {
1064 nxaddr = addressPXD(pxd);
1065 if (xaddr != nxaddr) {
1066 /* free relocated extent */
1067 xlen = lengthPXD(pxd);
1068 dbFree(ip, nxaddr, (s64) xlen);
1070 /* free extended delta */
1071 xlen = lengthPXD(pxd) - n;
1072 xaddr = addressPXD(pxd) + xlen;
1073 dbFree(ip, xaddr, (s64) n);
1075 } else if (!DO_INDEX(ip))
1076 ip->i_size = lengthPXD(pxd) << sbi->l2bsize;
1085 * split leaf page <sp> into <sp> and a new right page <rp>.
1087 * return <rp> pinned and its extent descriptor <rpxd>
1090 * allocate new directory page extent and
1091 * new index page(s) to cover page split(s)
1093 * allocation hint: ?
1095 n = btstack->nsplit;
1096 pxdlist.maxnpxd = pxdlist.npxd = 0;
1097 xlen = sbi->nbperpage;
1098 for (pxd = pxdlist.pxd; n > 0; n--, pxd++) {
1099 if ((rc = dbAlloc(ip, 0, (s64) xlen, &xaddr)) == 0) {
1100 PXDaddress(pxd, xaddr);
1101 PXDlength(pxd, xlen);
1108 /* undo allocation */
1112 split->pxdlist = &pxdlist;
1113 if ((rc = dtSplitPage(tid, ip, split, &rmp, &rp, &rpxd))) {
1116 /* undo allocation */
1121 ip->i_size += PSIZE;
1124 * propagate up the router entry for the leaf page just split
1126 * insert a router entry for the new page into the parent page,
1127 * propagate the insert/split up the tree by walking back the stack
1128 * of (bn of parent page, index of child page entry in parent page)
1129 * that were traversed during the search for the page that split.
1131 * the propagation of insert/split up the tree stops if the root
1132 * splits or the page inserted into doesn't have to split to hold
1135 * the parent entry for the split page remains the same, and
1136 * a new entry is inserted at its right with the first key and
1137 * block number of the new right page.
1139 * There are a maximum of 4 pages pinned at any time:
1140 * two children, left parent and right parent (when the parent splits).
1141 * keep the child pages pinned while working on the parent.
1142 * make sure that all pins are released at exit.
1144 while ((parent = BT_POP(btstack)) != NULL) {
1145 /* parent page specified by stack frame <parent> */
1147 /* keep current child pages (<lp>, <rp>) pinned */
1152 * insert router entry in parent for new right child page <rp>
1154 /* get the parent page <sp> */
1155 DT_GETPAGE(ip, parent->bn, smp, PSIZE, sp, rc);
1163 * The new key entry goes ONE AFTER the index of parent entry,
1164 * because the split was to the right.
1166 skip = parent->index + 1;
1169 * compute the key for the router entry
1171 * key suffix compression:
1172 * for internal pages that have leaf pages as children,
1173 * retain only what's needed to distinguish between
1174 * the new entry and the entry on the page to its left.
1175 * If the keys compare equal, retain the entire key.
1177 * note that compression is performed only at computing
1178 * router key at the lowest internal level.
1179 * further compression of the key between pairs of higher
1180 * level internal pages loses too much information and
1181 * the search may fail.
1182 * (e.g., two adjacent leaf pages of {a, ..., x} {xx, ...,}
1183 * results in two adjacent parent entries (a)(xx).
1184 * if split occurs between these two entries, and
1185 * if compression is applied, the router key of parent entry
1186 * of right page (x) will divert search for x into right
1187 * subtree and miss x in the left subtree.)
1189 * the entire key must be retained for the next-to-leftmost
1190 * internal key at any level of the tree, or search may fail
1193 switch (rp->header.flag & BT_TYPE) {
1196 * compute the length of prefix for suffix compression
1197 * between last entry of left page and first entry
1200 if ((sp->header.flag & BT_ROOT && skip > 1) ||
1201 sp->header.prev != 0 || skip > 1) {
1202 /* compute uppercase router prefix key */
1203 rc = ciGetLeafPrefixKey(lp,
1204 lp->header.nextindex-1,
1214 /* next to leftmost entry of
1215 lowest internal level */
1217 /* compute uppercase router key */
1218 dtGetKey(rp, 0, &key, sbi->mntflag);
1219 key.name[key.namlen] = 0;
1221 if ((sbi->mntflag & JFS_OS2) == JFS_OS2)
1225 n = NDTINTERNAL(key.namlen);
1229 dtGetKey(rp, 0, &key, sbi->mntflag);
1230 n = NDTINTERNAL(key.namlen);
1234 jfs_err("dtSplitUp(): UFO!");
1238 /* unpin left child page */
1242 * compute the data for the router entry
1244 data->xd = rpxd; /* child page xd */
1247 * parent page is full - split the parent page
1249 if (n > sp->header.freecnt) {
1250 /* init for parent page split */
1252 split->index = skip; /* index at insert */
1255 /* split->data = data; */
1257 /* unpin right child page */
1260 /* The split routines insert the new entry,
1261 * acquire txLock as appropriate.
1262 * return <rp> pinned and its block number <rbn>.
1264 rc = (sp->header.flag & BT_ROOT) ?
1265 dtSplitRoot(tid, ip, split, &rmp) :
1266 dtSplitPage(tid, ip, split, &rmp, &rp, &rpxd);
1272 /* smp and rmp are pinned */
1275 * parent page is not full - insert router entry in parent page
1278 BT_MARK_DIRTY(smp, ip);
1280 * acquire a transaction lock on the parent page
1282 tlck = txLock(tid, ip, smp, tlckDTREE | tlckENTRY);
1283 dtlck = (struct dt_lock *) & tlck->lock;
1284 ASSERT(dtlck->index == 0);
1285 lv = & dtlck->lv[0];
1287 /* linelock header */
1292 /* linelock stbl of non-root parent page */
1293 if (!(sp->header.flag & BT_ROOT)) {
1295 n = skip >> L2DTSLOTSIZE;
1296 lv->offset = sp->header.stblindex + n;
1298 ((sp->header.nextindex -
1299 1) >> L2DTSLOTSIZE) - n + 1;
1303 dtInsertEntry(sp, skip, &key, data, &dtlck);
1305 /* exit propagate up */
1310 /* unpin current split and its right page */
1315 * free remaining extents allocated for split
1319 pxd = &pxdlist.pxd[n];
1320 for (; n < pxdlist.maxnpxd; n++, pxd++)
1321 dbFree(ip, addressPXD(pxd), (s64) lengthPXD(pxd));
1326 /* Rollback dlimit allocation */
1327 if (rc && dlimit_allocation)
1328 DLIMIT_FREE_BLOCK(ip, dlimit_allocation);
1329 /* Rollback quota allocation */
1330 if (rc && quota_allocation)
1331 DQUOT_FREE_BLOCK(ip, quota_allocation);
1342 * function: Split a non-root page of a btree.
1346 * return: 0 - success;
1348 * return split and new page pinned;
1350 static int dtSplitPage(tid_t tid, struct inode *ip, struct dtsplit * split,
1351 struct metapage ** rmpp, dtpage_t ** rpp, pxd_t * rpxdp)
1354 struct metapage *smp;
1356 struct metapage *rmp;
1357 dtpage_t *rp; /* new right page allocated */
1358 s64 rbn; /* new right page block number */
1359 struct metapage *mp;
1362 struct pxdlist *pxdlist;
1364 int skip, nextindex, half, left, nxt, off, si;
1365 struct ldtentry *ldtentry;
1366 struct idtentry *idtentry;
1371 struct dt_lock *sdtlck, *rdtlck;
1373 struct dt_lock *dtlck;
1374 struct lv *slv, *rlv, *lv;
1376 /* get split page */
1378 sp = DT_PAGE(ip, smp);
1381 * allocate the new right page for the split
1383 pxdlist = split->pxdlist;
1384 pxd = &pxdlist->pxd[pxdlist->npxd];
1386 rbn = addressPXD(pxd);
1387 rmp = get_metapage(ip, rbn, PSIZE, 1);
1391 /* Allocate blocks to quota. */
1392 if (DQUOT_ALLOC_BLOCK(ip, lengthPXD(pxd))) {
1393 release_metapage(rmp);
1396 /* Allocate blocks to dlimit. */
1397 if (DLIMIT_ALLOC_BLOCK(ip, lengthPXD(pxd))) {
1398 DQUOT_FREE_BLOCK(ip, lengthPXD(pxd));
1399 release_metapage(rmp);
1403 jfs_info("dtSplitPage: ip:0x%p smp:0x%p rmp:0x%p", ip, smp, rmp);
1405 BT_MARK_DIRTY(rmp, ip);
1407 * acquire a transaction lock on the new right page
1409 tlck = txLock(tid, ip, rmp, tlckDTREE | tlckNEW);
1410 rdtlck = (struct dt_lock *) & tlck->lock;
1412 rp = (dtpage_t *) rmp->data;
1414 rp->header.self = *pxd;
1416 BT_MARK_DIRTY(smp, ip);
1418 * acquire a transaction lock on the split page
1422 tlck = txLock(tid, ip, smp, tlckDTREE | tlckENTRY);
1423 sdtlck = (struct dt_lock *) & tlck->lock;
1425 /* linelock header of split page */
1426 ASSERT(sdtlck->index == 0);
1427 slv = & sdtlck->lv[0];
1433 * initialize/update sibling pointers between sp and rp
1435 nextbn = le64_to_cpu(sp->header.next);
1436 rp->header.next = cpu_to_le64(nextbn);
1437 rp->header.prev = cpu_to_le64(addressPXD(&sp->header.self));
1438 sp->header.next = cpu_to_le64(rbn);
1441 * initialize new right page
1443 rp->header.flag = sp->header.flag;
1445 /* compute sorted entry table at start of extent data area */
1446 rp->header.nextindex = 0;
1447 rp->header.stblindex = 1;
1449 n = PSIZE >> L2DTSLOTSIZE;
1450 rp->header.maxslot = n;
1451 stblsize = (n + 31) >> L2DTSLOTSIZE; /* in unit of slot */
1454 fsi = rp->header.stblindex + stblsize;
1455 rp->header.freelist = fsi;
1456 rp->header.freecnt = rp->header.maxslot - fsi;
1459 * sequential append at tail: append without split
1461 * If splitting the last page on a level because of appending
1462 * a entry to it (skip is maxentry), it's likely that the access is
1463 * sequential. Adding an empty page on the side of the level is less
1464 * work and can push the fill factor much higher than normal.
1465 * If we're wrong it's no big deal, we'll just do the split the right
1467 * (It may look like it's equally easy to do a similar hack for
1468 * reverse sorted data, that is, split the tree left,
1469 * but it's not. Be my guest.)
1471 if (nextbn == 0 && split->index == sp->header.nextindex) {
1472 /* linelock header + stbl (first slot) of new page */
1473 rlv = & rdtlck->lv[rdtlck->index];
1479 * initialize freelist of new right page
1482 for (fsi++; fsi < rp->header.maxslot; f++, fsi++)
1486 /* insert entry at the first entry of the new right page */
1487 dtInsertEntry(rp, 0, split->key, split->data, &rdtlck);
1493 * non-sequential insert (at possibly middle page)
1497 * update prev pointer of previous right sibling page;
1500 DT_GETPAGE(ip, nextbn, mp, PSIZE, p, rc);
1502 discard_metapage(rmp);
1506 BT_MARK_DIRTY(mp, ip);
1508 * acquire a transaction lock on the next page
1510 tlck = txLock(tid, ip, mp, tlckDTREE | tlckRELINK);
1511 jfs_info("dtSplitPage: tlck = 0x%p, ip = 0x%p, mp=0x%p",
1513 dtlck = (struct dt_lock *) & tlck->lock;
1515 /* linelock header of previous right sibling page */
1516 lv = & dtlck->lv[dtlck->index];
1521 p->header.prev = cpu_to_le64(rbn);
1527 * split the data between the split and right pages.
1529 skip = split->index;
1530 half = (PSIZE >> L2DTSLOTSIZE) >> 1; /* swag */
1534 * compute fill factor for split pages
1536 * <nxt> traces the next entry to move to rp
1537 * <off> traces the next entry to stay in sp
1539 stbl = (u8 *) & sp->slot[sp->header.stblindex];
1540 nextindex = sp->header.nextindex;
1541 for (nxt = off = 0; nxt < nextindex; ++off) {
1543 /* check for fill factor with new entry size */
1547 switch (sp->header.flag & BT_TYPE) {
1549 ldtentry = (struct ldtentry *) & sp->slot[si];
1551 n = NDTLEAF(ldtentry->namlen);
1553 n = NDTLEAF_LEGACY(ldtentry->
1558 idtentry = (struct idtentry *) & sp->slot[si];
1559 n = NDTINTERNAL(idtentry->namlen);
1566 ++nxt; /* advance to next entry to move in sp */
1574 /* <nxt> poins to the 1st entry to move */
1577 * move entries to right page
1579 * dtMoveEntry() initializes rp and reserves entry for insertion
1581 * split page moved out entries are linelocked;
1582 * new/right page moved in entries are linelocked;
1584 /* linelock header + stbl of new right page */
1585 rlv = & rdtlck->lv[rdtlck->index];
1590 dtMoveEntry(sp, nxt, rp, &sdtlck, &rdtlck, DO_INDEX(ip));
1592 sp->header.nextindex = nxt;
1595 * finalize freelist of new right page
1597 fsi = rp->header.freelist;
1599 for (fsi++; fsi < rp->header.maxslot; f++, fsi++)
1604 * Update directory index table for entries now in right page
1606 if ((rp->header.flag & BT_LEAF) && DO_INDEX(ip)) {
1610 stbl = DT_GETSTBL(rp);
1611 for (n = 0; n < rp->header.nextindex; n++) {
1612 ldtentry = (struct ldtentry *) & rp->slot[stbl[n]];
1613 modify_index(tid, ip, le32_to_cpu(ldtentry->index),
1614 rbn, n, &mp, &lblock);
1617 release_metapage(mp);
1621 * the skipped index was on the left page,
1624 /* insert the new entry in the split page */
1625 dtInsertEntry(sp, skip, split->key, split->data, &sdtlck);
1627 /* linelock stbl of split page */
1628 if (sdtlck->index >= sdtlck->maxcnt)
1629 sdtlck = (struct dt_lock *) txLinelock(sdtlck);
1630 slv = & sdtlck->lv[sdtlck->index];
1631 n = skip >> L2DTSLOTSIZE;
1632 slv->offset = sp->header.stblindex + n;
1634 ((sp->header.nextindex - 1) >> L2DTSLOTSIZE) - n + 1;
1638 * the skipped index was on the right page,
1641 /* adjust the skip index to reflect the new position */
1644 /* insert the new entry in the right page */
1645 dtInsertEntry(rp, skip, split->key, split->data, &rdtlck);
1659 * function: extend 1st/only directory leaf page
1663 * return: 0 - success;
1665 * return extended page pinned;
1667 static int dtExtendPage(tid_t tid,
1668 struct inode *ip, struct dtsplit * split, struct btstack * btstack)
1670 struct super_block *sb = ip->i_sb;
1672 struct metapage *smp, *pmp, *mp;
1674 struct pxdlist *pxdlist;
1677 int newstblindex, newstblsize;
1678 int oldstblindex, oldstblsize;
1681 struct btframe *parent;
1683 struct dt_lock *dtlck;
1686 struct pxd_lock *pxdlock;
1689 struct ldtentry *ldtentry;
1692 /* get page to extend */
1694 sp = DT_PAGE(ip, smp);
1696 /* get parent/root page */
1697 parent = BT_POP(btstack);
1698 DT_GETPAGE(ip, parent->bn, pmp, PSIZE, pp, rc);
1705 pxdlist = split->pxdlist;
1706 pxd = &pxdlist->pxd[pxdlist->npxd];
1709 xaddr = addressPXD(pxd);
1710 tpxd = &sp->header.self;
1711 txaddr = addressPXD(tpxd);
1712 /* in-place extension */
1713 if (xaddr == txaddr) {
1720 /* save moved extent descriptor for later free */
1721 tlck = txMaplock(tid, ip, tlckDTREE | tlckRELOCATE);
1722 pxdlock = (struct pxd_lock *) & tlck->lock;
1723 pxdlock->flag = mlckFREEPXD;
1724 pxdlock->pxd = sp->header.self;
1728 * Update directory index table to reflect new page address
1734 stbl = DT_GETSTBL(sp);
1735 for (n = 0; n < sp->header.nextindex; n++) {
1737 (struct ldtentry *) & sp->slot[stbl[n]];
1738 modify_index(tid, ip,
1739 le32_to_cpu(ldtentry->index),
1740 xaddr, n, &mp, &lblock);
1743 release_metapage(mp);
1750 sp->header.self = *pxd;
1752 jfs_info("dtExtendPage: ip:0x%p smp:0x%p sp:0x%p", ip, smp, sp);
1754 BT_MARK_DIRTY(smp, ip);
1756 * acquire a transaction lock on the extended/leaf page
1758 tlck = txLock(tid, ip, smp, tlckDTREE | type);
1759 dtlck = (struct dt_lock *) & tlck->lock;
1760 lv = & dtlck->lv[0];
1762 /* update buffer extent descriptor of extended page */
1763 xlen = lengthPXD(pxd);
1764 xsize = xlen << JFS_SBI(sb)->l2bsize;
1765 #ifdef _STILL_TO_PORT
1766 bmSetXD(smp, xaddr, xsize);
1767 #endif /* _STILL_TO_PORT */
1770 * copy old stbl to new stbl at start of extended area
1772 oldstblindex = sp->header.stblindex;
1773 oldstblsize = (sp->header.maxslot + 31) >> L2DTSLOTSIZE;
1774 newstblindex = sp->header.maxslot;
1775 n = xsize >> L2DTSLOTSIZE;
1776 newstblsize = (n + 31) >> L2DTSLOTSIZE;
1777 memcpy(&sp->slot[newstblindex], &sp->slot[oldstblindex],
1778 sp->header.nextindex);
1781 * in-line extension: linelock old area of extended page
1783 if (type == tlckEXTEND) {
1784 /* linelock header */
1790 /* linelock new stbl of extended page */
1791 lv->offset = newstblindex;
1792 lv->length = newstblsize;
1795 * relocation: linelock whole relocated area
1799 lv->length = sp->header.maxslot + newstblsize;
1804 sp->header.maxslot = n;
1805 sp->header.stblindex = newstblindex;
1806 /* sp->header.nextindex remains the same */
1809 * add old stbl region at head of freelist
1813 last = sp->header.freelist;
1814 for (n = 0; n < oldstblsize; n++, fsi++, f++) {
1818 sp->header.freelist = last;
1819 sp->header.freecnt += oldstblsize;
1822 * append free region of newly extended area at tail of freelist
1824 /* init free region of newly extended area */
1825 fsi = n = newstblindex + newstblsize;
1827 for (fsi++; fsi < sp->header.maxslot; f++, fsi++)
1831 /* append new free region at tail of old freelist */
1832 fsi = sp->header.freelist;
1834 sp->header.freelist = n;
1839 } while (fsi != -1);
1844 sp->header.freecnt += sp->header.maxslot - n;
1847 * insert the new entry
1849 dtInsertEntry(sp, split->index, split->key, split->data, &dtlck);
1851 BT_MARK_DIRTY(pmp, ip);
1853 * linelock any freeslots residing in old extent
1855 if (type == tlckEXTEND) {
1856 n = sp->header.maxslot >> 2;
1857 if (sp->header.freelist < n)
1858 dtLinelockFreelist(sp, n, &dtlck);
1862 * update parent entry on the parent/root page
1865 * acquire a transaction lock on the parent/root page
1867 tlck = txLock(tid, ip, pmp, tlckDTREE | tlckENTRY);
1868 dtlck = (struct dt_lock *) & tlck->lock;
1869 lv = & dtlck->lv[dtlck->index];
1871 /* linelock parent entry - 1st slot */
1876 /* update the parent pxd for page extension */
1877 tpxd = (pxd_t *) & pp->slot[1];
1889 * split the full root page into
1890 * original/root/split page and new right page
1891 * i.e., root remains fixed in tree anchor (inode) and
1892 * the root is copied to a single new right child page
1893 * since root page << non-root page, and
1894 * the split root page contains a single entry for the
1895 * new right child page.
1899 * return: 0 - success;
1901 * return new page pinned;
1903 static int dtSplitRoot(tid_t tid,
1904 struct inode *ip, struct dtsplit * split, struct metapage ** rmpp)
1906 struct super_block *sb = ip->i_sb;
1907 struct metapage *smp;
1909 struct metapage *rmp;
1916 int fsi, stblsize, n;
1919 struct pxdlist *pxdlist;
1921 struct dt_lock *dtlck;
1925 /* get split root page */
1927 sp = &JFS_IP(ip)->i_dtroot;
1930 * allocate/initialize a single (right) child page
1932 * N.B. at first split, a one (or two) block to fit new entry
1933 * is allocated; at subsequent split, a full page is allocated;
1935 pxdlist = split->pxdlist;
1936 pxd = &pxdlist->pxd[pxdlist->npxd];
1938 rbn = addressPXD(pxd);
1939 xlen = lengthPXD(pxd);
1940 xsize = xlen << JFS_SBI(sb)->l2bsize;
1941 rmp = get_metapage(ip, rbn, xsize, 1);
1947 /* Allocate blocks to quota. */
1948 if (DQUOT_ALLOC_BLOCK(ip, lengthPXD(pxd))) {
1949 release_metapage(rmp);
1952 /* Allocate blocks to dlimit. */
1953 if (DLIMIT_ALLOC_BLOCK(ip, lengthPXD(pxd))) {
1954 DQUOT_FREE_BLOCK(ip, lengthPXD(pxd));
1955 release_metapage(rmp);
1959 BT_MARK_DIRTY(rmp, ip);
1961 * acquire a transaction lock on the new right page
1963 tlck = txLock(tid, ip, rmp, tlckDTREE | tlckNEW);
1964 dtlck = (struct dt_lock *) & tlck->lock;
1967 (sp->header.flag & BT_LEAF) ? BT_LEAF : BT_INTERNAL;
1968 rp->header.self = *pxd;
1970 /* initialize sibling pointers */
1971 rp->header.next = 0;
1972 rp->header.prev = 0;
1975 * move in-line root page into new right page extent
1977 /* linelock header + copied entries + new stbl (1st slot) in new page */
1978 ASSERT(dtlck->index == 0);
1979 lv = & dtlck->lv[0];
1981 lv->length = 10; /* 1 + 8 + 1 */
1984 n = xsize >> L2DTSLOTSIZE;
1985 rp->header.maxslot = n;
1986 stblsize = (n + 31) >> L2DTSLOTSIZE;
1988 /* copy old stbl to new stbl at start of extended area */
1989 rp->header.stblindex = DTROOTMAXSLOT;
1990 stbl = (s8 *) & rp->slot[DTROOTMAXSLOT];
1991 memcpy(stbl, sp->header.stbl, sp->header.nextindex);
1992 rp->header.nextindex = sp->header.nextindex;
1994 /* copy old data area to start of new data area */
1995 memcpy(&rp->slot[1], &sp->slot[1], IDATASIZE);
1998 * append free region of newly extended area at tail of freelist
2000 /* init free region of newly extended area */
2001 fsi = n = DTROOTMAXSLOT + stblsize;
2003 for (fsi++; fsi < rp->header.maxslot; f++, fsi++)
2007 /* append new free region at tail of old freelist */
2008 fsi = sp->header.freelist;
2010 rp->header.freelist = n;
2012 rp->header.freelist = fsi;
2017 } while (fsi != -1);
2022 rp->header.freecnt = sp->header.freecnt + rp->header.maxslot - n;
2025 * Update directory index table for entries now in right page
2027 if ((rp->header.flag & BT_LEAF) && DO_INDEX(ip)) {
2029 struct metapage *mp = NULL;
2030 struct ldtentry *ldtentry;
2032 stbl = DT_GETSTBL(rp);
2033 for (n = 0; n < rp->header.nextindex; n++) {
2034 ldtentry = (struct ldtentry *) & rp->slot[stbl[n]];
2035 modify_index(tid, ip, le32_to_cpu(ldtentry->index),
2036 rbn, n, &mp, &lblock);
2039 release_metapage(mp);
2042 * insert the new entry into the new right/child page
2043 * (skip index in the new right page will not change)
2045 dtInsertEntry(rp, split->index, split->key, split->data, &dtlck);
2048 * reset parent/root page
2050 * set the 1st entry offset to 0, which force the left-most key
2051 * at any level of the tree to be less than any search key.
2053 * The btree comparison code guarantees that the left-most key on any
2054 * level of the tree is never used, so it doesn't need to be filled in.
2056 BT_MARK_DIRTY(smp, ip);
2058 * acquire a transaction lock on the root page (in-memory inode)
2060 tlck = txLock(tid, ip, smp, tlckDTREE | tlckNEW | tlckBTROOT);
2061 dtlck = (struct dt_lock *) & tlck->lock;
2064 ASSERT(dtlck->index == 0);
2065 lv = & dtlck->lv[0];
2067 lv->length = DTROOTMAXSLOT;
2070 /* update page header of root */
2071 if (sp->header.flag & BT_LEAF) {
2072 sp->header.flag &= ~BT_LEAF;
2073 sp->header.flag |= BT_INTERNAL;
2076 /* init the first entry */
2077 s = (struct idtentry *) & sp->slot[DTENTRYSTART];
2083 stbl = sp->header.stbl;
2084 stbl[0] = DTENTRYSTART;
2085 sp->header.nextindex = 1;
2088 fsi = DTENTRYSTART + 1;
2091 /* init free region of remaining area */
2092 for (fsi++; fsi < DTROOTMAXSLOT; f++, fsi++)
2096 sp->header.freelist = DTENTRYSTART + 1;
2097 sp->header.freecnt = DTROOTMAXSLOT - (DTENTRYSTART + 1);
2108 * function: delete the entry(s) referenced by a key.
2114 int dtDelete(tid_t tid,
2115 struct inode *ip, struct component_name * key, ino_t * ino, int flag)
2119 struct metapage *mp, *imp;
2122 struct btstack btstack;
2123 struct dt_lock *dtlck;
2127 struct ldtentry *ldtentry;
2129 u32 table_index, next_index;
2130 struct metapage *nmp;
2134 * search for the entry to delete:
2136 * dtSearch() returns (leaf page pinned, index at which to delete).
2138 if ((rc = dtSearch(ip, key, ino, &btstack, flag)))
2141 /* retrieve search result */
2142 DT_GETSEARCH(ip, btstack.top, bn, mp, p, index);
2145 * We need to find put the index of the next entry into the
2146 * directory index table in order to resume a readdir from this
2150 stbl = DT_GETSTBL(p);
2151 ldtentry = (struct ldtentry *) & p->slot[stbl[index]];
2152 table_index = le32_to_cpu(ldtentry->index);
2153 if (index == (p->header.nextindex - 1)) {
2155 * Last entry in this leaf page
2157 if ((p->header.flag & BT_ROOT)
2158 || (p->header.next == 0))
2161 /* Read next leaf page */
2162 DT_GETPAGE(ip, le64_to_cpu(p->header.next),
2163 nmp, PSIZE, np, rc);
2167 stbl = DT_GETSTBL(np);
2169 (struct ldtentry *) & np->
2172 le32_to_cpu(ldtentry->index);
2178 (struct ldtentry *) & p->slot[stbl[index + 1]];
2179 next_index = le32_to_cpu(ldtentry->index);
2181 free_index(tid, ip, table_index, next_index);
2184 * the leaf page becomes empty, delete the page
2186 if (p->header.nextindex == 1) {
2187 /* delete empty page */
2188 rc = dtDeleteUp(tid, ip, mp, p, &btstack);
2191 * the leaf page has other entries remaining:
2193 * delete the entry from the leaf page.
2196 BT_MARK_DIRTY(mp, ip);
2198 * acquire a transaction lock on the leaf page
2200 tlck = txLock(tid, ip, mp, tlckDTREE | tlckENTRY);
2201 dtlck = (struct dt_lock *) & tlck->lock;
2204 * Do not assume that dtlck->index will be zero. During a
2205 * rename within a directory, this transaction may have
2206 * modified this page already when adding the new entry.
2209 /* linelock header */
2210 if (dtlck->index >= dtlck->maxcnt)
2211 dtlck = (struct dt_lock *) txLinelock(dtlck);
2212 lv = & dtlck->lv[dtlck->index];
2217 /* linelock stbl of non-root leaf page */
2218 if (!(p->header.flag & BT_ROOT)) {
2219 if (dtlck->index >= dtlck->maxcnt)
2220 dtlck = (struct dt_lock *) txLinelock(dtlck);
2221 lv = & dtlck->lv[dtlck->index];
2222 i = index >> L2DTSLOTSIZE;
2223 lv->offset = p->header.stblindex + i;
2225 ((p->header.nextindex - 1) >> L2DTSLOTSIZE) -
2230 /* free the leaf entry */
2231 dtDeleteEntry(p, index, &dtlck);
2234 * Update directory index table for entries moved in stbl
2236 if (DO_INDEX(ip) && index < p->header.nextindex) {
2240 stbl = DT_GETSTBL(p);
2241 for (i = index; i < p->header.nextindex; i++) {
2243 (struct ldtentry *) & p->slot[stbl[i]];
2244 modify_index(tid, ip,
2245 le32_to_cpu(ldtentry->index),
2246 bn, i, &imp, &lblock);
2249 release_metapage(imp);
2263 * free empty pages as propagating deletion up the tree
2269 static int dtDeleteUp(tid_t tid, struct inode *ip,
2270 struct metapage * fmp, dtpage_t * fp, struct btstack * btstack)
2273 struct metapage *mp;
2275 int index, nextindex;
2277 struct btframe *parent;
2278 struct dt_lock *dtlck;
2281 struct pxd_lock *pxdlock;
2285 * keep the root leaf page which has become empty
2287 if (BT_IS_ROOT(fmp)) {
2291 * dtInitRoot() acquires txlock on the root
2293 dtInitRoot(tid, ip, PARENT(ip));
2301 * free the non-root leaf page
2304 * acquire a transaction lock on the page
2306 * write FREEXTENT|NOREDOPAGE log record
2307 * N.B. linelock is overlaid as freed extent descriptor, and
2308 * the buffer page is freed;
2310 tlck = txMaplock(tid, ip, tlckDTREE | tlckFREE);
2311 pxdlock = (struct pxd_lock *) & tlck->lock;
2312 pxdlock->flag = mlckFREEPXD;
2313 pxdlock->pxd = fp->header.self;
2316 /* update sibling pointers */
2317 if ((rc = dtRelink(tid, ip, fp))) {
2322 xlen = lengthPXD(&fp->header.self);
2324 /* Free dlimit allocation. */
2325 DLIMIT_FREE_BLOCK(ip, xlen);
2326 /* Free quota allocation. */
2327 DQUOT_FREE_BLOCK(ip, xlen);
2329 /* free/invalidate its buffer page */
2330 discard_metapage(fmp);
2333 * propagate page deletion up the directory tree
2335 * If the delete from the parent page makes it empty,
2336 * continue all the way up the tree.
2337 * stop if the root page is reached (which is never deleted) or
2338 * if the entry deletion does not empty the page.
2340 while ((parent = BT_POP(btstack)) != NULL) {
2341 /* pin the parent page <sp> */
2342 DT_GETPAGE(ip, parent->bn, mp, PSIZE, p, rc);
2347 * free the extent of the child page deleted
2349 index = parent->index;
2352 * delete the entry for the child page from parent
2354 nextindex = p->header.nextindex;
2357 * the parent has the single entry being deleted:
2359 * free the parent page which has become empty.
2361 if (nextindex == 1) {
2363 * keep the root internal page which has become empty
2365 if (p->header.flag & BT_ROOT) {
2369 * dtInitRoot() acquires txlock on the root
2371 dtInitRoot(tid, ip, PARENT(ip));
2378 * free the parent page
2382 * acquire a transaction lock on the page
2384 * write FREEXTENT|NOREDOPAGE log record
2388 tlckDTREE | tlckFREE);
2389 pxdlock = (struct pxd_lock *) & tlck->lock;
2390 pxdlock->flag = mlckFREEPXD;
2391 pxdlock->pxd = p->header.self;
2394 /* update sibling pointers */
2395 if ((rc = dtRelink(tid, ip, p))) {
2400 xlen = lengthPXD(&p->header.self);
2402 /* Free dlimit allocation */
2403 DLIMIT_FREE_BLOCK(ip, xlen);
2404 /* Free quota allocation */
2405 DQUOT_FREE_BLOCK(ip, xlen);
2407 /* free/invalidate its buffer page */
2408 discard_metapage(mp);
2416 * the parent has other entries remaining:
2418 * delete the router entry from the parent page.
2420 BT_MARK_DIRTY(mp, ip);
2422 * acquire a transaction lock on the page
2424 * action: router entry deletion
2426 tlck = txLock(tid, ip, mp, tlckDTREE | tlckENTRY);
2427 dtlck = (struct dt_lock *) & tlck->lock;
2429 /* linelock header */
2430 if (dtlck->index >= dtlck->maxcnt)
2431 dtlck = (struct dt_lock *) txLinelock(dtlck);
2432 lv = & dtlck->lv[dtlck->index];
2437 /* linelock stbl of non-root leaf page */
2438 if (!(p->header.flag & BT_ROOT)) {
2439 if (dtlck->index < dtlck->maxcnt)
2442 dtlck = (struct dt_lock *) txLinelock(dtlck);
2443 lv = & dtlck->lv[0];
2445 i = index >> L2DTSLOTSIZE;
2446 lv->offset = p->header.stblindex + i;
2448 ((p->header.nextindex - 1) >> L2DTSLOTSIZE) -
2453 /* free the router entry */
2454 dtDeleteEntry(p, index, &dtlck);
2456 /* reset key of new leftmost entry of level (for consistency) */
2458 ((p->header.flag & BT_ROOT) || p->header.prev == 0))
2459 dtTruncateEntry(p, 0, &dtlck);
2461 /* unpin the parent page */
2464 /* exit propagation up */
2469 ip->i_size -= PSIZE;
2476 * NAME: dtRelocate()
2478 * FUNCTION: relocate dtpage (internal or leaf) of directory;
2479 * This function is mainly used by defragfs utility.
2481 int dtRelocate(tid_t tid, struct inode *ip, s64 lmxaddr, pxd_t * opxd,
2485 struct metapage *mp, *pmp, *lmp, *rmp;
2486 dtpage_t *p, *pp, *rp = 0, *lp= 0;
2489 struct btstack btstack;
2491 s64 oxaddr, nextbn, prevbn;
2494 struct dt_lock *dtlck;
2495 struct pxd_lock *pxdlock;
2499 oxaddr = addressPXD(opxd);
2500 xlen = lengthPXD(opxd);
2502 jfs_info("dtRelocate: lmxaddr:%Ld xaddr:%Ld:%Ld xlen:%d",
2503 (long long)lmxaddr, (long long)oxaddr, (long long)nxaddr,
2507 * 1. get the internal parent dtpage covering
2508 * router entry for the tartget page to be relocated;
2510 rc = dtSearchNode(ip, lmxaddr, opxd, &btstack);
2514 /* retrieve search result */
2515 DT_GETSEARCH(ip, btstack.top, bn, pmp, pp, index);
2516 jfs_info("dtRelocate: parent router entry validated.");
2519 * 2. relocate the target dtpage
2521 /* read in the target page from src extent */
2522 DT_GETPAGE(ip, oxaddr, mp, PSIZE, p, rc);
2524 /* release the pinned parent page */
2530 * read in sibling pages if any to update sibling pointers;
2533 if (p->header.next) {
2534 nextbn = le64_to_cpu(p->header.next);
2535 DT_GETPAGE(ip, nextbn, rmp, PSIZE, rp, rc);
2544 if (p->header.prev) {
2545 prevbn = le64_to_cpu(p->header.prev);
2546 DT_GETPAGE(ip, prevbn, lmp, PSIZE, lp, rc);
2556 /* at this point, all xtpages to be updated are in memory */
2559 * update sibling pointers of sibling dtpages if any;
2562 tlck = txLock(tid, ip, lmp, tlckDTREE | tlckRELINK);
2563 dtlck = (struct dt_lock *) & tlck->lock;
2564 /* linelock header */
2565 ASSERT(dtlck->index == 0);
2566 lv = & dtlck->lv[0];
2571 lp->header.next = cpu_to_le64(nxaddr);
2576 tlck = txLock(tid, ip, rmp, tlckDTREE | tlckRELINK);
2577 dtlck = (struct dt_lock *) & tlck->lock;
2578 /* linelock header */
2579 ASSERT(dtlck->index == 0);
2580 lv = & dtlck->lv[0];
2585 rp->header.prev = cpu_to_le64(nxaddr);
2590 * update the target dtpage to be relocated
2592 * write LOG_REDOPAGE of LOG_NEW type for dst page
2593 * for the whole target page (logredo() will apply
2594 * after image and update bmap for allocation of the
2595 * dst extent), and update bmap for allocation of
2598 tlck = txLock(tid, ip, mp, tlckDTREE | tlckNEW);
2599 dtlck = (struct dt_lock *) & tlck->lock;
2600 /* linelock header */
2601 ASSERT(dtlck->index == 0);
2602 lv = & dtlck->lv[0];
2604 /* update the self address in the dtpage header */
2605 pxd = &p->header.self;
2606 PXDaddress(pxd, nxaddr);
2608 /* the dst page is the same as the src page, i.e.,
2609 * linelock for afterimage of the whole page;
2612 lv->length = p->header.maxslot;
2615 /* update the buffer extent descriptor of the dtpage */
2616 xsize = xlen << JFS_SBI(ip->i_sb)->l2bsize;
2617 #ifdef _STILL_TO_PORT
2618 bmSetXD(mp, nxaddr, xsize);
2619 #endif /* _STILL_TO_PORT */
2620 /* unpin the relocated page */
2622 jfs_info("dtRelocate: target dtpage relocated.");
2624 /* the moved extent is dtpage, then a LOG_NOREDOPAGE log rec
2625 * needs to be written (in logredo(), the LOG_NOREDOPAGE log rec
2626 * will also force a bmap update ).
2630 * 3. acquire maplock for the source extent to be freed;
2632 /* for dtpage relocation, write a LOG_NOREDOPAGE record
2633 * for the source dtpage (logredo() will init NoRedoPage
2634 * filter and will also update bmap for free of the source
2635 * dtpage), and upadte bmap for free of the source dtpage;
2637 tlck = txMaplock(tid, ip, tlckDTREE | tlckFREE);
2638 pxdlock = (struct pxd_lock *) & tlck->lock;
2639 pxdlock->flag = mlckFREEPXD;
2640 PXDaddress(&pxdlock->pxd, oxaddr);
2641 PXDlength(&pxdlock->pxd, xlen);
2645 * 4. update the parent router entry for relocation;
2647 * acquire tlck for the parent entry covering the target dtpage;
2648 * write LOG_REDOPAGE to apply after image only;
2650 jfs_info("dtRelocate: update parent router entry.");
2651 tlck = txLock(tid, ip, pmp, tlckDTREE | tlckENTRY);
2652 dtlck = (struct dt_lock *) & tlck->lock;
2653 lv = & dtlck->lv[dtlck->index];
2655 /* update the PXD with the new address */
2656 stbl = DT_GETSTBL(pp);
2657 pxd = (pxd_t *) & pp->slot[stbl[index]];
2658 PXDaddress(pxd, nxaddr);
2659 lv->offset = stbl[index];
2663 /* unpin the parent dtpage */
2670 * NAME: dtSearchNode()
2672 * FUNCTION: Search for an dtpage containing a specified address
2673 * This function is mainly used by defragfs utility.
2675 * NOTE: Search result on stack, the found page is pinned at exit.
2676 * The result page must be an internal dtpage.
2677 * lmxaddr give the address of the left most page of the
2678 * dtree level, in which the required dtpage resides.
2680 static int dtSearchNode(struct inode *ip, s64 lmxaddr, pxd_t * kpxd,
2681 struct btstack * btstack)
2685 struct metapage *mp;
2687 int psize = 288; /* initial in-line directory */
2691 struct btframe *btsp;
2693 BT_CLR(btstack); /* reset stack */
2696 * descend tree to the level with specified leftmost page
2698 * by convention, root bn = 0.
2701 /* get/pin the page to search */
2702 DT_GETPAGE(ip, bn, mp, psize, p, rc);
2706 /* does the xaddr of leftmost page of the levevl
2707 * matches levevl search key ?
2709 if (p->header.flag & BT_ROOT) {
2712 } else if (addressPXD(&p->header.self) == lmxaddr)
2716 * descend down to leftmost child page
2718 if (p->header.flag & BT_LEAF) {
2723 /* get the leftmost entry */
2724 stbl = DT_GETSTBL(p);
2725 pxd = (pxd_t *) & p->slot[stbl[0]];
2727 /* get the child page block address */
2728 bn = addressPXD(pxd);
2729 psize = lengthPXD(pxd) << JFS_SBI(ip->i_sb)->l2bsize;
2730 /* unpin the parent page */
2735 * search each page at the current levevl
2738 stbl = DT_GETSTBL(p);
2739 for (i = 0; i < p->header.nextindex; i++) {
2740 pxd = (pxd_t *) & p->slot[stbl[i]];
2742 /* found the specified router entry */
2743 if (addressPXD(pxd) == addressPXD(kpxd) &&
2744 lengthPXD(pxd) == lengthPXD(kpxd)) {
2745 btsp = btstack->top;
2754 /* get the right sibling page if any */
2756 bn = le64_to_cpu(p->header.next);
2762 /* unpin current page */
2765 /* get the right sibling page */
2766 DT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
2772 #endif /* _NOTYET */
2778 * link around a freed page.
2781 * fp: page to be freed
2785 static int dtRelink(tid_t tid, struct inode *ip, dtpage_t * p)
2788 struct metapage *mp;
2791 struct dt_lock *dtlck;
2794 nextbn = le64_to_cpu(p->header.next);
2795 prevbn = le64_to_cpu(p->header.prev);
2797 /* update prev pointer of the next page */
2799 DT_GETPAGE(ip, nextbn, mp, PSIZE, p, rc);
2803 BT_MARK_DIRTY(mp, ip);
2805 * acquire a transaction lock on the next page
2807 * action: update prev pointer;
2809 tlck = txLock(tid, ip, mp, tlckDTREE | tlckRELINK);
2810 jfs_info("dtRelink nextbn: tlck = 0x%p, ip = 0x%p, mp=0x%p",
2812 dtlck = (struct dt_lock *) & tlck->lock;
2814 /* linelock header */
2815 if (dtlck->index >= dtlck->maxcnt)
2816 dtlck = (struct dt_lock *) txLinelock(dtlck);
2817 lv = & dtlck->lv[dtlck->index];
2822 p->header.prev = cpu_to_le64(prevbn);
2826 /* update next pointer of the previous page */
2828 DT_GETPAGE(ip, prevbn, mp, PSIZE, p, rc);
2832 BT_MARK_DIRTY(mp, ip);
2834 * acquire a transaction lock on the prev page
2836 * action: update next pointer;
2838 tlck = txLock(tid, ip, mp, tlckDTREE | tlckRELINK);
2839 jfs_info("dtRelink prevbn: tlck = 0x%p, ip = 0x%p, mp=0x%p",
2841 dtlck = (struct dt_lock *) & tlck->lock;
2843 /* linelock header */
2844 if (dtlck->index >= dtlck->maxcnt)
2845 dtlck = (struct dt_lock *) txLinelock(dtlck);
2846 lv = & dtlck->lv[dtlck->index];
2851 p->header.next = cpu_to_le64(nextbn);
2862 * initialize directory root (inline in inode)
2864 void dtInitRoot(tid_t tid, struct inode *ip, u32 idotdot)
2866 struct jfs_inode_info *jfs_ip = JFS_IP(ip);
2871 struct dt_lock *dtlck;
2876 * If this was previously an non-empty directory, we need to remove
2877 * the old directory table.
2880 if (!jfs_dirtable_inline(ip)) {
2881 struct tblock *tblk = tid_to_tblock(tid);
2883 * We're playing games with the tid's xflag. If
2884 * we're removing a regular file, the file's xtree
2885 * is committed with COMMIT_PMAP, but we always
2886 * commit the directories xtree with COMMIT_PWMAP.
2888 xflag_save = tblk->xflag;
2891 * xtTruncate isn't guaranteed to fully truncate
2892 * the xtree. The caller needs to check i_size
2893 * after committing the transaction to see if
2894 * additional truncation is needed. The
2895 * COMMIT_Stale flag tells caller that we
2896 * initiated the truncation.
2898 xtTruncate(tid, ip, 0, COMMIT_PWMAP);
2899 set_cflag(COMMIT_Stale, ip);
2901 tblk->xflag = xflag_save;
2905 jfs_ip->next_index = 2;
2907 ip->i_size = IDATASIZE;
2910 * acquire a transaction lock on the root
2912 * action: directory initialization;
2914 tlck = txLock(tid, ip, (struct metapage *) & jfs_ip->bxflag,
2915 tlckDTREE | tlckENTRY | tlckBTROOT);
2916 dtlck = (struct dt_lock *) & tlck->lock;
2919 ASSERT(dtlck->index == 0);
2920 lv = & dtlck->lv[0];
2922 lv->length = DTROOTMAXSLOT;
2925 p = &jfs_ip->i_dtroot;
2927 p->header.flag = DXD_INDEX | BT_ROOT | BT_LEAF;
2929 p->header.nextindex = 0;
2935 /* init data area of root */
2936 for (fsi++; fsi < DTROOTMAXSLOT; f++, fsi++)
2940 p->header.freelist = 1;
2941 p->header.freecnt = 8;
2943 /* init '..' entry */
2944 p->header.idotdot = cpu_to_le32(idotdot);
2950 * add_missing_indices()
2952 * function: Fix dtree page in which one or more entries has an invalid index.
2953 * fsck.jfs should really fix this, but it currently does not.
2954 * Called from jfs_readdir when bad index is detected.
2956 static void add_missing_indices(struct inode *inode, s64 bn)
2959 struct dt_lock *dtlck;
2963 struct metapage *mp;
2970 tid = txBegin(inode->i_sb, 0);
2972 DT_GETPAGE(inode, bn, mp, PSIZE, p, rc);
2975 printk(KERN_ERR "DT_GETPAGE failed!\n");
2978 BT_MARK_DIRTY(mp, inode);
2980 ASSERT(p->header.flag & BT_LEAF);
2982 tlck = txLock(tid, inode, mp, tlckDTREE | tlckENTRY);
2984 tlck->type |= tlckBTROOT;
2986 dtlck = (struct dt_lock *) &tlck->lock;
2988 stbl = DT_GETSTBL(p);
2989 for (i = 0; i < p->header.nextindex; i++) {
2990 d = (struct ldtentry *) &p->slot[stbl[i]];
2991 index = le32_to_cpu(d->index);
2992 if ((index < 2) || (index >= JFS_IP(inode)->next_index)) {
2993 d->index = cpu_to_le32(add_index(tid, inode, bn, i));
2994 if (dtlck->index >= dtlck->maxcnt)
2995 dtlck = (struct dt_lock *) txLinelock(dtlck);
2996 lv = &dtlck->lv[dtlck->index];
2997 lv->offset = stbl[i];
3004 (void) txCommit(tid, 1, &inode, 0);
3010 * Buffer to hold directory entry info while traversing a dtree page
3011 * before being fed to the filldir function
3021 * function to determine next variable-sized jfs_dirent in buffer
3023 static inline struct jfs_dirent *next_jfs_dirent(struct jfs_dirent *dirent)
3025 return (struct jfs_dirent *)
3027 ((sizeof (struct jfs_dirent) + dirent->name_len + 1 +
3028 sizeof (loff_t) - 1) &
3029 ~(sizeof (loff_t) - 1)));
3035 * function: read directory entries sequentially
3036 * from the specified entry offset
3040 * return: offset = (pn, index) of start entry
3041 * of next jfs_readdir()/dtRead()
3043 int jfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
3045 struct inode *ip = filp->f_path.dentry->d_inode;
3046 struct nls_table *codepage = JFS_SBI(ip->i_sb)->nls_tab;
3048 loff_t dtpos; /* legacy OS/2 style position */
3053 } *dtoffset = (struct dtoffset *) &dtpos;
3055 struct metapage *mp;
3059 struct btstack btstack;
3063 int d_namleft, len, outlen;
3064 unsigned long dirent_buf;
3068 uint loop_count = 0;
3069 struct jfs_dirent *jfs_dirent;
3071 int overflow, fix_page, page_fixed = 0;
3072 static int unique_pos = 2; /* If we can't fix broken index */
3074 if (filp->f_pos == DIREND)
3079 * persistent index is stored in directory entries.
3080 * Special cases: 0 = .
3082 * -1 = End of directory
3086 dir_index = (u32) filp->f_pos;
3088 if (dir_index > 1) {
3089 struct dir_table_slot dirtab_slot;
3092 (dir_index >= JFS_IP(ip)->next_index)) {
3093 /* Stale position. Directory has shrunk */
3094 filp->f_pos = DIREND;
3098 rc = read_index(ip, dir_index, &dirtab_slot);
3100 filp->f_pos = DIREND;
3103 if (dirtab_slot.flag == DIR_INDEX_FREE) {
3104 if (loop_count++ > JFS_IP(ip)->next_index) {
3105 jfs_err("jfs_readdir detected "
3107 filp->f_pos = DIREND;
3110 dir_index = le32_to_cpu(dirtab_slot.addr2);
3111 if (dir_index == -1) {
3112 filp->f_pos = DIREND;
3117 bn = addressDTS(&dirtab_slot);
3118 index = dirtab_slot.slot;
3119 DT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
3121 filp->f_pos = DIREND;
3124 if (p->header.flag & BT_INTERNAL) {
3125 jfs_err("jfs_readdir: bad index table");
3131 if (dir_index == 0) {
3136 if (filldir(dirent, ".", 1, 0, ip->i_ino,
3144 if (filldir(dirent, "..", 2, 1, PARENT(ip), DT_DIR))
3148 * Find first entry of left-most leaf
3151 filp->f_pos = DIREND;
3155 if ((rc = dtReadFirst(ip, &btstack)))
3158 DT_GETSEARCH(ip, btstack.top, bn, mp, p, index);
3162 * Legacy filesystem - OS/2 & Linux JFS < 0.3.6
3164 * pn = index = 0: First entry "."
3165 * pn = 0; index = 1: Second entry ".."
3166 * pn > 0: Real entries, pn=1 -> leftmost page
3167 * pn = index = -1: No more entries
3169 dtpos = filp->f_pos;
3171 /* build "." entry */
3173 if (filldir(dirent, ".", 1, filp->f_pos, ip->i_ino,
3176 dtoffset->index = 1;
3177 filp->f_pos = dtpos;
3180 if (dtoffset->pn == 0) {
3181 if (dtoffset->index == 1) {
3182 /* build ".." entry */
3184 if (filldir(dirent, "..", 2, filp->f_pos,
3185 PARENT(ip), DT_DIR))
3188 jfs_err("jfs_readdir called with "
3192 dtoffset->index = 0;
3193 filp->f_pos = dtpos;
3197 filp->f_pos = DIREND;
3201 if ((rc = dtReadNext(ip, &filp->f_pos, &btstack))) {
3202 jfs_err("jfs_readdir: unexpected rc = %d "
3203 "from dtReadNext", rc);
3204 filp->f_pos = DIREND;
3207 /* get start leaf page and index */
3208 DT_GETSEARCH(ip, btstack.top, bn, mp, p, index);
3210 /* offset beyond directory eof ? */
3212 filp->f_pos = DIREND;
3217 dirent_buf = __get_free_page(GFP_KERNEL);
3218 if (dirent_buf == 0) {
3220 jfs_warn("jfs_readdir: __get_free_page failed!");
3221 filp->f_pos = DIREND;
3226 jfs_dirent = (struct jfs_dirent *) dirent_buf;
3228 overflow = fix_page = 0;
3230 stbl = DT_GETSTBL(p);
3232 for (i = index; i < p->header.nextindex; i++) {
3233 d = (struct ldtentry *) & p->slot[stbl[i]];
3235 if (((long) jfs_dirent + d->namlen + 1) >
3236 (dirent_buf + PAGE_SIZE)) {
3237 /* DBCS codepages could overrun dirent_buf */
3243 d_namleft = d->namlen;
3244 name_ptr = jfs_dirent->name;
3245 jfs_dirent->ino = le32_to_cpu(d->inumber);
3248 len = min(d_namleft, DTLHDRDATALEN);
3249 jfs_dirent->position = le32_to_cpu(d->index);
3251 * d->index should always be valid, but it
3252 * isn't. fsck.jfs doesn't create the
3253 * directory index for the lost+found
3254 * directory. Rather than let it go,
3255 * we can try to fix it.
3257 if ((jfs_dirent->position < 2) ||
3258 (jfs_dirent->position >=
3259 JFS_IP(ip)->next_index)) {
3260 if (!page_fixed && !isReadOnly(ip)) {
3263 * setting overflow and setting
3264 * index to i will cause the
3265 * same page to be processed
3266 * again starting here
3272 jfs_dirent->position = unique_pos++;
3275 jfs_dirent->position = dtpos;
3276 len = min(d_namleft, DTLHDRDATALEN_LEGACY);
3279 /* copy the name of head/only segment */
3280 outlen = jfs_strfromUCS_le(name_ptr, d->name, len,
3282 jfs_dirent->name_len = outlen;
3284 /* copy name in the additional segment(s) */
3287 t = (struct dtslot *) & p->slot[next];
3291 if (d_namleft == 0) {
3293 "JFS:Dtree error: ino = "
3294 "%ld, bn=%Ld, index = %d",
3300 len = min(d_namleft, DTSLOTDATALEN);
3301 outlen = jfs_strfromUCS_le(name_ptr, t->name,
3303 jfs_dirent->name_len += outlen;
3309 jfs_dirent = next_jfs_dirent(jfs_dirent);
3316 /* Point to next leaf page */
3317 if (p->header.flag & BT_ROOT)
3320 bn = le64_to_cpu(p->header.next);
3322 /* update offset (pn:index) for new page */
3325 dtoffset->index = 0;
3331 /* unpin previous leaf page */
3334 jfs_dirent = (struct jfs_dirent *) dirent_buf;
3335 while (jfs_dirents--) {
3336 filp->f_pos = jfs_dirent->position;
3337 if (filldir(dirent, jfs_dirent->name,
3338 jfs_dirent->name_len, filp->f_pos,
3339 jfs_dirent->ino, DT_UNKNOWN))
3341 jfs_dirent = next_jfs_dirent(jfs_dirent);
3345 add_missing_indices(ip, bn);
3349 if (!overflow && (bn == 0)) {
3350 filp->f_pos = DIREND;
3354 DT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
3356 free_page(dirent_buf);
3362 free_page(dirent_buf);
3371 * function: get the leftmost page of the directory
3373 static int dtReadFirst(struct inode *ip, struct btstack * btstack)
3377 int psize = 288; /* initial in-line directory */
3378 struct metapage *mp;
3381 struct btframe *btsp;
3384 BT_CLR(btstack); /* reset stack */
3387 * descend leftmost path of the tree
3389 * by convention, root bn = 0.
3392 DT_GETPAGE(ip, bn, mp, psize, p, rc);
3397 * leftmost leaf page
3399 if (p->header.flag & BT_LEAF) {
3400 /* return leftmost entry */
3401 btsp = btstack->top;
3410 * descend down to leftmost child page
3412 if (BT_STACK_FULL(btstack)) {
3414 jfs_error(ip->i_sb, "dtReadFirst: btstack overrun");
3415 BT_STACK_DUMP(btstack);
3418 /* push (bn, index) of the parent page/entry */
3419 BT_PUSH(btstack, bn, 0);
3421 /* get the leftmost entry */
3422 stbl = DT_GETSTBL(p);
3423 xd = (pxd_t *) & p->slot[stbl[0]];
3425 /* get the child page block address */
3426 bn = addressPXD(xd);
3427 psize = lengthPXD(xd) << JFS_SBI(ip->i_sb)->l2bsize;
3429 /* unpin the parent page */
3438 * function: get the page of the specified offset (pn:index)
3440 * return: if (offset > eof), bn = -1;
3442 * note: if index > nextindex of the target leaf page,
3443 * start with 1st entry of next leaf page;
3445 static int dtReadNext(struct inode *ip, loff_t * offset,
3446 struct btstack * btstack)
3453 } *dtoffset = (struct dtoffset *) offset;
3455 struct metapage *mp;
3460 struct btframe *btsp, *parent;
3464 * get leftmost leaf page pinned
3466 if ((rc = dtReadFirst(ip, btstack)))
3470 DT_GETSEARCH(ip, btstack->top, bn, mp, p, index);
3472 /* get the start offset (pn:index) */
3473 pn = dtoffset->pn - 1; /* Now pn = 0 represents leftmost leaf */
3474 index = dtoffset->index;
3476 /* start at leftmost page ? */
3478 /* offset beyond eof ? */
3479 if (index < p->header.nextindex)
3482 if (p->header.flag & BT_ROOT) {
3487 /* start with 1st entry of next leaf page */
3489 dtoffset->index = index = 0;
3493 /* start at non-leftmost page: scan parent pages for large pn */
3494 if (p->header.flag & BT_ROOT) {
3499 /* start after next leaf page ? */
3503 /* get leaf page pn = 1 */
3505 bn = le64_to_cpu(p->header.next);
3507 /* unpin leaf page */
3510 /* offset beyond eof ? */
3519 * scan last internal page level to get target leaf page
3522 /* unpin leftmost leaf page */
3525 /* get left most parent page */
3526 btsp = btstack->top;
3529 DT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
3533 /* scan parent pages at last internal page level */
3534 while (pn >= p->header.nextindex) {
3535 pn -= p->header.nextindex;
3537 /* get next parent page address */
3538 bn = le64_to_cpu(p->header.next);
3540 /* unpin current parent page */
3543 /* offset beyond eof ? */
3549 /* get next parent page */
3550 DT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
3554 /* update parent page stack frame */
3558 /* get leaf page address */
3559 stbl = DT_GETSTBL(p);
3560 xd = (pxd_t *) & p->slot[stbl[pn]];
3561 bn = addressPXD(xd);
3563 /* unpin parent page */
3567 * get target leaf page
3570 DT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
3575 * leaf page has been completed:
3576 * start with 1st entry of next leaf page
3578 if (index >= p->header.nextindex) {
3579 bn = le64_to_cpu(p->header.next);
3581 /* unpin leaf page */
3584 /* offset beyond eof ? */
3590 /* get next leaf page */
3591 DT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
3595 /* start with 1st entry of next leaf page */
3597 dtoffset->index = 0;
3601 /* return target leaf page pinned */
3602 btsp = btstack->top;
3604 btsp->index = dtoffset->index;
3614 * function: compare search key with an internal entry
3617 * < 0 if k is < record
3618 * = 0 if k is = record
3619 * > 0 if k is > record
3621 static int dtCompare(struct component_name * key, /* search key */
3622 dtpage_t * p, /* directory page */
3624 { /* entry slot index */
3627 int klen, namlen, len, rc;
3628 struct idtentry *ih;
3632 * force the left-most key on internal pages, at any level of
3633 * the tree, to be less than any search key.
3634 * this obviates having to update the leftmost key on an internal
3635 * page when the user inserts a new key in the tree smaller than
3636 * anything that has been stored.
3638 * (? if/when dtSearch() narrows down to 1st entry (index = 0),
3639 * at any internal page at any level of the tree,
3640 * it descends to child of the entry anyway -
3641 * ? make the entry as min size dummy entry)
3643 * if (e->index == 0 && h->prevpg == P_INVALID && !(h->flags & BT_LEAF))
3650 ih = (struct idtentry *) & p->slot[si];
3653 namlen = ih->namlen;
3654 len = min(namlen, DTIHDRDATALEN);
3656 /* compare with head/only segment */
3657 len = min(klen, len);
3658 if ((rc = UniStrncmp_le(kname, name, len)))
3664 /* compare with additional segment(s) */
3666 while (klen > 0 && namlen > 0) {
3667 /* compare with next name segment */
3668 t = (struct dtslot *) & p->slot[si];
3669 len = min(namlen, DTSLOTDATALEN);
3670 len = min(klen, len);
3672 if ((rc = UniStrncmp_le(kname, name, len)))
3681 return (klen - namlen);
3690 * function: compare search key with an (leaf/internal) entry
3693 * < 0 if k is < record
3694 * = 0 if k is = record
3695 * > 0 if k is > record
3697 static int ciCompare(struct component_name * key, /* search key */
3698 dtpage_t * p, /* directory page */
3699 int si, /* entry slot index */
3704 int klen, namlen, len, rc;
3705 struct ldtentry *lh;
3706 struct idtentry *ih;
3711 * force the left-most key on internal pages, at any level of
3712 * the tree, to be less than any search key.
3713 * this obviates having to update the leftmost key on an internal
3714 * page when the user inserts a new key in the tree smaller than
3715 * anything that has been stored.
3717 * (? if/when dtSearch() narrows down to 1st entry (index = 0),
3718 * at any internal page at any level of the tree,
3719 * it descends to child of the entry anyway -
3720 * ? make the entry as min size dummy entry)
3722 * if (e->index == 0 && h->prevpg == P_INVALID && !(h->flags & BT_LEAF))
3732 if (p->header.flag & BT_LEAF) {
3733 lh = (struct ldtentry *) & p->slot[si];
3736 namlen = lh->namlen;
3737 if (flag & JFS_DIR_INDEX)
3738 len = min(namlen, DTLHDRDATALEN);
3740 len = min(namlen, DTLHDRDATALEN_LEGACY);
3743 * internal page entry
3746 ih = (struct idtentry *) & p->slot[si];
3749 namlen = ih->namlen;
3750 len = min(namlen, DTIHDRDATALEN);
3753 /* compare with head/only segment */
3754 len = min(klen, len);
3755 for (i = 0; i < len; i++, kname++, name++) {
3756 /* only uppercase if case-insensitive support is on */
3757 if ((flag & JFS_OS2) == JFS_OS2)
3758 x = UniToupper(le16_to_cpu(*name));
3760 x = le16_to_cpu(*name);
3761 if ((rc = *kname - x))
3768 /* compare with additional segment(s) */
3769 while (klen > 0 && namlen > 0) {
3770 /* compare with next name segment */
3771 t = (struct dtslot *) & p->slot[si];
3772 len = min(namlen, DTSLOTDATALEN);
3773 len = min(klen, len);
3775 for (i = 0; i < len; i++, kname++, name++) {
3776 /* only uppercase if case-insensitive support is on */
3777 if ((flag & JFS_OS2) == JFS_OS2)
3778 x = UniToupper(le16_to_cpu(*name));
3780 x = le16_to_cpu(*name);
3782 if ((rc = *kname - x))
3791 return (klen - namlen);
3796 * ciGetLeafPrefixKey()
3798 * function: compute prefix of suffix compression
3799 * from two adjacent leaf entries
3800 * across page boundary
3802 * return: non-zero on error
3805 static int ciGetLeafPrefixKey(dtpage_t * lp, int li, dtpage_t * rp,
3806 int ri, struct component_name * key, int flag)
3809 wchar_t *pl, *pr, *kname;
3810 struct component_name lkey;
3811 struct component_name rkey;
3813 lkey.name = kmalloc((JFS_NAME_MAX + 1) * sizeof(wchar_t),
3815 if (lkey.name == NULL)
3818 rkey.name = kmalloc((JFS_NAME_MAX + 1) * sizeof(wchar_t),
3820 if (rkey.name == NULL) {
3825 /* get left and right key */
3826 dtGetKey(lp, li, &lkey, flag);
3827 lkey.name[lkey.namlen] = 0;
3829 if ((flag & JFS_OS2) == JFS_OS2)
3832 dtGetKey(rp, ri, &rkey, flag);
3833 rkey.name[rkey.namlen] = 0;
3836 if ((flag & JFS_OS2) == JFS_OS2)
3839 /* compute prefix */
3842 namlen = min(lkey.namlen, rkey.namlen);
3843 for (pl = lkey.name, pr = rkey.name;
3844 namlen; pl++, pr++, namlen--, klen++, kname++) {
3847 key->namlen = klen + 1;
3852 /* l->namlen <= r->namlen since l <= r */
3853 if (lkey.namlen < rkey.namlen) {
3855 key->namlen = klen + 1;
3856 } else /* l->namelen == r->namelen */
3870 * function: get key of the entry
3872 static void dtGetKey(dtpage_t * p, int i, /* entry index */
3873 struct component_name * key, int flag)
3877 struct ldtentry *lh;
3878 struct idtentry *ih;
3885 stbl = DT_GETSTBL(p);
3887 if (p->header.flag & BT_LEAF) {
3888 lh = (struct ldtentry *) & p->slot[si];
3890 namlen = lh->namlen;
3892 if (flag & JFS_DIR_INDEX)
3893 len = min(namlen, DTLHDRDATALEN);
3895 len = min(namlen, DTLHDRDATALEN_LEGACY);
3897 ih = (struct idtentry *) & p->slot[si];
3899 namlen = ih->namlen;
3901 len = min(namlen, DTIHDRDATALEN);
3904 key->namlen = namlen;
3908 * move head/only segment
3910 UniStrncpy_from_le(kname, name, len);
3913 * move additional segment(s)
3916 /* get next segment */
3920 len = min(namlen, DTSLOTDATALEN);
3921 UniStrncpy_from_le(kname, t->name, len);
3931 * function: allocate free slot(s) and
3932 * write a leaf/internal entry
3934 * return: entry slot index
3936 static void dtInsertEntry(dtpage_t * p, int index, struct component_name * key,
3937 ddata_t * data, struct dt_lock ** dtlock)
3939 struct dtslot *h, *t;
3940 struct ldtentry *lh = NULL;
3941 struct idtentry *ih = NULL;
3942 int hsi, fsi, klen, len, nextindex;
3947 struct dt_lock *dtlck = *dtlock;
3951 struct metapage *mp = NULL;
3956 /* allocate a free slot */
3957 hsi = fsi = p->header.freelist;
3959 p->header.freelist = h->next;
3960 --p->header.freecnt;
3962 /* open new linelock */
3963 if (dtlck->index >= dtlck->maxcnt)
3964 dtlck = (struct dt_lock *) txLinelock(dtlck);
3966 lv = & dtlck->lv[dtlck->index];
3969 /* write head/only segment */
3970 if (p->header.flag & BT_LEAF) {
3971 lh = (struct ldtentry *) h;
3973 lh->inumber = cpu_to_le32(data->leaf.ino);
3976 if (data->leaf.ip) {
3977 len = min(klen, DTLHDRDATALEN);
3978 if (!(p->header.flag & BT_ROOT))
3979 bn = addressPXD(&p->header.self);
3980 lh->index = cpu_to_le32(add_index(data->leaf.tid,
3984 len = min(klen, DTLHDRDATALEN_LEGACY);
3986 ih = (struct idtentry *) h;
3992 len = min(klen, DTIHDRDATALEN);
3995 UniStrncpy_to_le(name, kname, len);
4000 /* write additional segment(s) */
4005 fsi = p->header.freelist;
4007 p->header.freelist = t->next;
4008 --p->header.freecnt;
4010 /* is next slot contiguous ? */
4011 if (fsi != xsi + 1) {
4012 /* close current linelock */
4016 /* open new linelock */
4017 if (dtlck->index < dtlck->maxcnt)
4020 dtlck = (struct dt_lock *) txLinelock(dtlck);
4021 lv = & dtlck->lv[0];
4029 len = min(klen, DTSLOTDATALEN);
4030 UniStrncpy_to_le(t->name, kname, len);
4037 /* close current linelock */
4043 /* terminate last/only segment */
4045 /* single segment entry */
4046 if (p->header.flag & BT_LEAF)
4051 /* multi-segment entry */
4054 /* if insert into middle, shift right succeeding entries in stbl */
4055 stbl = DT_GETSTBL(p);
4056 nextindex = p->header.nextindex;
4057 if (index < nextindex) {
4058 memmove(stbl + index + 1, stbl + index, nextindex - index);
4060 if ((p->header.flag & BT_LEAF) && data->leaf.ip) {
4064 * Need to update slot number for entries that moved
4068 for (n = index + 1; n <= nextindex; n++) {
4069 lh = (struct ldtentry *) & (p->slot[stbl[n]]);
4070 modify_index(data->leaf.tid, data->leaf.ip,
4071 le32_to_cpu(lh->index), bn, n,
4075 release_metapage(mp);
4081 /* advance next available entry index of stbl */
4082 ++p->header.nextindex;
4089 * function: move entries from split/left page to new/right page
4091 * nextindex of dst page and freelist/freecnt of both pages
4094 static void dtMoveEntry(dtpage_t * sp, int si, dtpage_t * dp,
4095 struct dt_lock ** sdtlock, struct dt_lock ** ddtlock,
4098 int ssi, next; /* src slot index */
4099 int di; /* dst entry index */
4100 int dsi; /* dst slot index */
4101 s8 *sstbl, *dstbl; /* sorted entry table */
4103 struct ldtentry *slh, *dlh = NULL;
4104 struct idtentry *sih, *dih = NULL;
4105 struct dtslot *h, *s, *d;
4106 struct dt_lock *sdtlck = *sdtlock, *ddtlck = *ddtlock;
4107 struct lv *slv, *dlv;
4111 sstbl = (s8 *) & sp->slot[sp->header.stblindex];
4112 dstbl = (s8 *) & dp->slot[dp->header.stblindex];
4114 dsi = dp->header.freelist; /* first (whole page) free slot */
4115 sfsi = sp->header.freelist;
4117 /* linelock destination entry slot */
4118 dlv = & ddtlck->lv[ddtlck->index];
4121 /* linelock source entry slot */
4122 slv = & sdtlck->lv[sdtlck->index];
4123 slv->offset = sstbl[si];
4124 xssi = slv->offset - 1;
4130 for (di = 0; si < sp->header.nextindex; si++, di++) {
4134 /* is next slot contiguous ? */
4135 if (ssi != xssi + 1) {
4136 /* close current linelock */
4140 /* open new linelock */
4141 if (sdtlck->index < sdtlck->maxcnt)
4144 sdtlck = (struct dt_lock *) txLinelock(sdtlck);
4145 slv = & sdtlck->lv[0];
4153 * move head/only segment of an entry
4156 h = d = &dp->slot[dsi];
4158 /* get src slot and move */
4160 if (sp->header.flag & BT_LEAF) {
4161 /* get source entry */
4162 slh = (struct ldtentry *) s;
4163 dlh = (struct ldtentry *) h;
4164 snamlen = slh->namlen;
4167 len = min(snamlen, DTLHDRDATALEN);
4168 dlh->index = slh->index; /* little-endian */
4170 len = min(snamlen, DTLHDRDATALEN_LEGACY);
4172 memcpy(dlh, slh, 6 + len * 2);
4176 /* update dst head/only segment next field */
4180 sih = (struct idtentry *) s;
4181 snamlen = sih->namlen;
4183 len = min(snamlen, DTIHDRDATALEN);
4184 dih = (struct idtentry *) h;
4185 memcpy(dih, sih, 10 + len * 2);
4192 /* free src head/only segment */
4202 * move additional segment(s) of the entry
4205 while ((ssi = next) >= 0) {
4206 /* is next slot contiguous ? */
4207 if (ssi != xssi + 1) {
4208 /* close current linelock */
4212 /* open new linelock */
4213 if (sdtlck->index < sdtlck->maxcnt)
4219 slv = & sdtlck->lv[0];
4226 /* get next source segment */
4229 /* get next destination free slot */
4232 len = min(snamlen, DTSLOTDATALEN);
4233 UniStrncpy_le(d->name, s->name, len);
4242 /* free source segment */
4251 /* terminate dst last/only segment */
4253 /* single segment entry */
4254 if (dp->header.flag & BT_LEAF)
4259 /* multi-segment entry */
4263 /* close current linelock */
4272 /* update source header */
4273 sp->header.freelist = sfsi;
4274 sp->header.freecnt += nd;
4276 /* update destination header */
4277 dp->header.nextindex = di;
4279 dp->header.freelist = dsi;
4280 dp->header.freecnt -= nd;
4287 * function: free a (leaf/internal) entry
4289 * log freelist header, stbl, and each segment slot of entry
4290 * (even though last/only segment next field is modified,
4291 * physical image logging requires all segment slots of
4292 * the entry logged to avoid applying previous updates
4293 * to the same slots)
4295 static void dtDeleteEntry(dtpage_t * p, int fi, struct dt_lock ** dtlock)
4297 int fsi; /* free entry slot index */
4301 struct dt_lock *dtlck = *dtlock;
4305 /* get free entry slot index */
4306 stbl = DT_GETSTBL(p);
4309 /* open new linelock */
4310 if (dtlck->index >= dtlck->maxcnt)
4311 dtlck = (struct dt_lock *) txLinelock(dtlck);
4312 lv = & dtlck->lv[dtlck->index];
4316 /* get the head/only segment */
4318 if (p->header.flag & BT_LEAF)
4319 si = ((struct ldtentry *) t)->next;
4321 si = ((struct idtentry *) t)->next;
4328 /* find the last/only segment */
4330 /* is next slot contiguous ? */
4331 if (si != xsi + 1) {
4332 /* close current linelock */
4336 /* open new linelock */
4337 if (dtlck->index < dtlck->maxcnt)
4340 dtlck = (struct dt_lock *) txLinelock(dtlck);
4341 lv = & dtlck->lv[0];
4357 /* close current linelock */
4363 /* update freelist */
4364 t->next = p->header.freelist;
4365 p->header.freelist = fsi;
4366 p->header.freecnt += freecnt;
4368 /* if delete from middle,
4369 * shift left the succedding entries in the stbl
4371 si = p->header.nextindex;
4373 memmove(&stbl[fi], &stbl[fi + 1], si - fi - 1);
4375 p->header.nextindex--;
4382 * function: truncate a (leaf/internal) entry
4384 * log freelist header, stbl, and each segment slot of entry
4385 * (even though last/only segment next field is modified,
4386 * physical image logging requires all segment slots of
4387 * the entry logged to avoid applying previous updates
4388 * to the same slots)
4390 static void dtTruncateEntry(dtpage_t * p, int ti, struct dt_lock ** dtlock)
4392 int tsi; /* truncate entry slot index */
4396 struct dt_lock *dtlck = *dtlock;
4400 /* get free entry slot index */
4401 stbl = DT_GETSTBL(p);
4404 /* open new linelock */
4405 if (dtlck->index >= dtlck->maxcnt)
4406 dtlck = (struct dt_lock *) txLinelock(dtlck);
4407 lv = & dtlck->lv[dtlck->index];
4411 /* get the head/only segment */
4413 ASSERT(p->header.flag & BT_INTERNAL);
4414 ((struct idtentry *) t)->namlen = 0;
4415 si = ((struct idtentry *) t)->next;
4416 ((struct idtentry *) t)->next = -1;
4423 /* find the last/only segment */
4425 /* is next slot contiguous ? */
4426 if (si != xsi + 1) {
4427 /* close current linelock */
4431 /* open new linelock */
4432 if (dtlck->index < dtlck->maxcnt)
4435 dtlck = (struct dt_lock *) txLinelock(dtlck);
4436 lv = & dtlck->lv[0];
4452 /* close current linelock */
4458 /* update freelist */
4461 t->next = p->header.freelist;
4462 p->header.freelist = fsi;
4463 p->header.freecnt += freecnt;
4468 * dtLinelockFreelist()
4470 static void dtLinelockFreelist(dtpage_t * p, /* directory page */
4471 int m, /* max slot index */
4472 struct dt_lock ** dtlock)
4474 int fsi; /* free entry slot index */
4477 struct dt_lock *dtlck = *dtlock;
4481 /* get free entry slot index */
4482 fsi = p->header.freelist;
4484 /* open new linelock */
4485 if (dtlck->index >= dtlck->maxcnt)
4486 dtlck = (struct dt_lock *) txLinelock(dtlck);
4487 lv = & dtlck->lv[dtlck->index];
4497 /* find the last/only segment */
4498 while (si < m && si >= 0) {
4499 /* is next slot contiguous ? */
4500 if (si != xsi + 1) {
4501 /* close current linelock */
4505 /* open new linelock */
4506 if (dtlck->index < dtlck->maxcnt)
4509 dtlck = (struct dt_lock *) txLinelock(dtlck);
4510 lv = & dtlck->lv[0];
4524 /* close current linelock */
4535 * FUNCTION: Modify the inode number part of a directory entry
4538 * tid - Transaction id
4539 * ip - Inode of parent directory
4540 * key - Name of entry to be modified
4541 * orig_ino - Original inode number expected in entry
4542 * new_ino - New inode number to put into entry
4546 * -ESTALE - If entry found does not match orig_ino passed in
4547 * -ENOENT - If no entry can be found to match key
4548 * 0 - If successfully modified entry
4550 int dtModify(tid_t tid, struct inode *ip,
4551 struct component_name * key, ino_t * orig_ino, ino_t new_ino, int flag)
4555 struct metapage *mp;
4558 struct btstack btstack;
4560 struct dt_lock *dtlck;
4563 int entry_si; /* entry slot index */
4564 struct ldtentry *entry;
4567 * search for the entry to modify:
4569 * dtSearch() returns (leaf page pinned, index at which to modify).
4571 if ((rc = dtSearch(ip, key, orig_ino, &btstack, flag)))
4574 /* retrieve search result */
4575 DT_GETSEARCH(ip, btstack.top, bn, mp, p, index);
4577 BT_MARK_DIRTY(mp, ip);
4579 * acquire a transaction lock on the leaf page of named entry
4581 tlck = txLock(tid, ip, mp, tlckDTREE | tlckENTRY);
4582 dtlck = (struct dt_lock *) & tlck->lock;
4584 /* get slot index of the entry */
4585 stbl = DT_GETSTBL(p);
4586 entry_si = stbl[index];
4588 /* linelock entry */
4589 ASSERT(dtlck->index == 0);
4590 lv = & dtlck->lv[0];
4591 lv->offset = entry_si;
4595 /* get the head/only segment */
4596 entry = (struct ldtentry *) & p->slot[entry_si];
4598 /* substitute the inode number of the entry */
4599 entry->inumber = cpu_to_le32(new_ino);
4601 /* unpin the leaf page */