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))
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);
486 DQUOT_FREE_BLOCK(ip, sbi->nbperpage);
489 jfs_ip->next_index--;
497 * Marks an entry to the directory index table as free.
499 static void free_index(tid_t tid, struct inode *ip, u32 index, u32 next)
501 struct dir_table_slot *dirtab_slot;
503 struct metapage *mp = NULL;
505 dirtab_slot = find_index(ip, index, &mp, &lblock);
507 if (dirtab_slot == 0)
510 dirtab_slot->flag = DIR_INDEX_FREE;
511 dirtab_slot->slot = dirtab_slot->addr1 = 0;
512 dirtab_slot->addr2 = cpu_to_le32(next);
515 lock_index(tid, ip, mp, index);
516 mark_metapage_dirty(mp);
517 release_metapage(mp);
519 set_cflag(COMMIT_Dirtable, ip);
525 * Changes an entry in the directory index table
527 static void modify_index(tid_t tid, struct inode *ip, u32 index, s64 bn,
528 int slot, struct metapage ** mp, u64 *lblock)
530 struct dir_table_slot *dirtab_slot;
532 dirtab_slot = find_index(ip, index, mp, lblock);
534 if (dirtab_slot == 0)
537 DTSaddress(dirtab_slot, bn);
538 dirtab_slot->slot = slot;
541 lock_index(tid, ip, *mp, index);
542 mark_metapage_dirty(*mp);
544 set_cflag(COMMIT_Dirtable, ip);
550 * reads a directory table slot
552 static int read_index(struct inode *ip, u32 index,
553 struct dir_table_slot * dirtab_slot)
556 struct metapage *mp = NULL;
557 struct dir_table_slot *slot;
559 slot = find_index(ip, index, &mp, &lblock);
564 memcpy(dirtab_slot, slot, sizeof(struct dir_table_slot));
567 release_metapage(mp);
576 * Search for the entry with specified key
580 * return: 0 - search result on stack, leaf page pinned;
583 int dtSearch(struct inode *ip, struct component_name * key, ino_t * data,
584 struct btstack * btstack, int flag)
587 int cmp = 1; /* init for empty page */
592 int base, index, lim;
593 struct btframe *btsp;
595 int psize = 288; /* initial in-line directory */
597 struct component_name ciKey;
598 struct super_block *sb = ip->i_sb;
601 (wchar_t *) kmalloc((JFS_NAME_MAX + 1) * sizeof(wchar_t),
603 if (ciKey.name == 0) {
609 /* uppercase search key for c-i directory */
610 UniStrcpy(ciKey.name, key->name);
611 ciKey.namlen = key->namlen;
613 /* only uppercase if case-insensitive support is on */
614 if ((JFS_SBI(sb)->mntflag & JFS_OS2) == JFS_OS2) {
617 BT_CLR(btstack); /* reset stack */
619 /* init level count for max pages to split */
623 * search down tree from root:
625 * between two consecutive entries of <Ki, Pi> and <Kj, Pj> of
626 * internal page, child page Pi contains entry with k, Ki <= K < Kj.
628 * if entry with search key K is not found
629 * internal page search find the entry with largest key Ki
630 * less than K which point to the child page to search;
631 * leaf page search find the entry with smallest key Kj
632 * greater than K so that the returned index is the position of
633 * the entry to be shifted right for insertion of new entry.
634 * for empty tree, search key is greater than any key of the tree.
636 * by convention, root bn = 0.
639 /* get/pin the page to search */
640 DT_GETPAGE(ip, bn, mp, psize, p, rc);
644 /* get sorted entry table of the page */
645 stbl = DT_GETSTBL(p);
648 * binary search with search key K on the current page.
650 for (base = 0, lim = p->header.nextindex; lim; lim >>= 1) {
651 index = base + (lim >> 1);
653 if (p->header.flag & BT_LEAF) {
654 /* uppercase leaf name to compare */
656 ciCompare(&ciKey, p, stbl[index],
657 JFS_SBI(sb)->mntflag);
659 /* router key is in uppercase */
661 cmp = dtCompare(&ciKey, p, stbl[index]);
669 /* search hit - leaf page:
670 * return the entry found
672 if (p->header.flag & BT_LEAF) {
673 inumber = le32_to_cpu(
674 ((struct ldtentry *) & p->slot[stbl[index]])->inumber);
677 * search for JFS_LOOKUP
679 if (flag == JFS_LOOKUP) {
686 * search for JFS_CREATE
688 if (flag == JFS_CREATE) {
695 * search for JFS_REMOVE or JFS_RENAME
697 if ((flag == JFS_REMOVE ||
698 flag == JFS_RENAME) &&
705 * JFS_REMOVE|JFS_FINDDIR|JFS_RENAME
707 /* save search result */
718 /* search hit - internal page:
719 * descend/search its child page
733 * base is the smallest index with key (Kj) greater than
734 * search key (K) and may be zero or (maxindex + 1) index.
737 * search miss - leaf page
739 * return location of entry (base) where new entry with
740 * search key K is to be inserted.
742 if (p->header.flag & BT_LEAF) {
744 * search for JFS_LOOKUP, JFS_REMOVE, or JFS_RENAME
746 if (flag == JFS_LOOKUP || flag == JFS_REMOVE ||
747 flag == JFS_RENAME) {
753 * search for JFS_CREATE|JFS_FINDDIR:
768 * search miss - internal page
770 * if base is non-zero, decrement base by one to get the parent
771 * entry of the child page to search.
773 index = base ? base - 1 : base;
776 * go down to child page
779 /* update max. number of pages to split */
780 if (BT_STACK_FULL(btstack)) {
781 /* Something's corrupted, mark filesytem dirty so
782 * chkdsk will fix it.
784 jfs_error(sb, "stack overrun in dtSearch!");
785 BT_STACK_DUMP(btstack);
791 /* push (bn, index) of the parent page/entry */
792 BT_PUSH(btstack, bn, index);
794 /* get the child page block number */
795 pxd = (pxd_t *) & p->slot[stbl[index]];
796 bn = addressPXD(pxd);
797 psize = lengthPXD(pxd) << JFS_SBI(ip->i_sb)->l2bsize;
799 /* unpin the parent page */
819 * function: insert an entry to directory tree
823 * return: 0 - success;
826 int dtInsert(tid_t tid, struct inode *ip,
827 struct component_name * name, ino_t * fsn, struct btstack * btstack)
830 struct metapage *mp; /* meta-page buffer */
831 dtpage_t *p; /* base B+-tree index page */
834 struct dtsplit split; /* split information */
836 struct dt_lock *dtlck;
842 * retrieve search result
844 * dtSearch() returns (leaf page pinned, index at which to insert).
845 * n.b. dtSearch() may return index of (maxindex + 1) of
848 DT_GETSEARCH(ip, btstack->top, bn, mp, p, index);
851 * insert entry for new key
854 if (JFS_IP(ip)->next_index == DIREND) {
858 n = NDTLEAF(name->namlen);
862 n = NDTLEAF_LEGACY(name->namlen);
863 data.leaf.ip = NULL; /* signifies legacy directory format */
865 data.leaf.ino = *fsn;
868 * leaf page does not have enough room for new entry:
870 * extend/split the leaf page;
872 * dtSplitUp() will insert the entry and unpin the leaf page.
874 if (n > p->header.freecnt) {
880 rc = dtSplitUp(tid, ip, &split, btstack);
885 * leaf page does have enough room for new entry:
887 * insert the new data entry into the leaf page;
889 BT_MARK_DIRTY(mp, ip);
891 * acquire a transaction lock on the leaf page
893 tlck = txLock(tid, ip, mp, tlckDTREE | tlckENTRY);
894 dtlck = (struct dt_lock *) & tlck->lock;
895 ASSERT(dtlck->index == 0);
898 /* linelock header */
903 dtInsertEntry(p, index, name, &data, &dtlck);
905 /* linelock stbl of non-root leaf page */
906 if (!(p->header.flag & BT_ROOT)) {
907 if (dtlck->index >= dtlck->maxcnt)
908 dtlck = (struct dt_lock *) txLinelock(dtlck);
909 lv = & dtlck->lv[dtlck->index];
910 n = index >> L2DTSLOTSIZE;
911 lv->offset = p->header.stblindex + n;
913 ((p->header.nextindex - 1) >> L2DTSLOTSIZE) - n + 1;
917 /* unpin the leaf page */
927 * function: propagate insertion bottom up;
931 * return: 0 - success;
933 * leaf page unpinned;
935 static int dtSplitUp(tid_t tid,
936 struct inode *ip, struct dtsplit * split, struct btstack * btstack)
938 struct super_block *sb = ip->i_sb;
939 struct jfs_sb_info *sbi = JFS_SBI(sb);
941 struct metapage *smp;
942 dtpage_t *sp; /* split page */
943 struct metapage *rmp;
944 dtpage_t *rp; /* new right page split from sp */
945 pxd_t rpxd; /* new right page extent descriptor */
946 struct metapage *lmp;
947 dtpage_t *lp; /* left child page */
948 int skip; /* index of entry of insertion */
949 struct btframe *parent; /* parent page entry on traverse stack */
952 struct pxdlist pxdlist;
954 struct component_name key = { 0, NULL };
955 ddata_t *data = split->data;
957 struct dt_lock *dtlck;
960 int quota_allocation = 0;
961 int dlimit_allocation = 0;
965 sp = DT_PAGE(ip, smp);
968 (wchar_t *) kmalloc((JFS_NAME_MAX + 2) * sizeof(wchar_t),
979 * The split routines insert the new entry, and
980 * acquire txLock as appropriate.
983 * split root leaf page:
985 if (sp->header.flag & BT_ROOT) {
987 * allocate a single extent child page
990 n = sbi->bsize >> L2DTSLOTSIZE;
991 n -= (n + 31) >> L2DTSLOTSIZE; /* stbl size */
992 n -= DTROOTMAXSLOT - sp->header.freecnt; /* header + entries */
993 if (n <= split->nslot)
995 if ((rc = dbAlloc(ip, 0, (s64) xlen, &xaddr))) {
1000 pxdlist.maxnpxd = 1;
1002 pxd = &pxdlist.pxd[0];
1003 PXDaddress(pxd, xaddr);
1004 PXDlength(pxd, xlen);
1005 split->pxdlist = &pxdlist;
1006 rc = dtSplitRoot(tid, ip, split, &rmp);
1009 dbFree(ip, xaddr, xlen);
1016 ip->i_size = xlen << sbi->l2bsize;
1022 * extend first leaf page
1024 * extend the 1st extent if less than buffer page size
1025 * (dtExtendPage() reurns leaf page unpinned)
1027 pxd = &sp->header.self;
1028 xlen = lengthPXD(pxd);
1029 xsize = xlen << sbi->l2bsize;
1030 if (xsize < PSIZE) {
1031 xaddr = addressPXD(pxd);
1032 n = xsize >> L2DTSLOTSIZE;
1033 n -= (n + 31) >> L2DTSLOTSIZE; /* stbl size */
1034 if ((n + sp->header.freecnt) <= split->nslot)
1035 n = xlen + (xlen << 1);
1039 /* Allocate blocks to quota. */
1040 if (DQUOT_ALLOC_BLOCK(ip, n)) {
1044 quota_allocation += n;
1046 if (DLIMIT_ALLOC_BLOCK(ip, n)) {
1050 dlimit_allocation += n;
1052 if ((rc = dbReAlloc(sbi->ipbmap, xaddr, (s64) xlen,
1056 pxdlist.maxnpxd = 1;
1058 pxd = &pxdlist.pxd[0];
1059 PXDaddress(pxd, nxaddr)
1060 PXDlength(pxd, xlen + n);
1061 split->pxdlist = &pxdlist;
1062 if ((rc = dtExtendPage(tid, ip, split, btstack))) {
1063 nxaddr = addressPXD(pxd);
1064 if (xaddr != nxaddr) {
1065 /* free relocated extent */
1066 xlen = lengthPXD(pxd);
1067 dbFree(ip, nxaddr, (s64) xlen);
1069 /* free extended delta */
1070 xlen = lengthPXD(pxd) - n;
1071 xaddr = addressPXD(pxd) + xlen;
1072 dbFree(ip, xaddr, (s64) n);
1074 } else if (!DO_INDEX(ip))
1075 ip->i_size = lengthPXD(pxd) << sbi->l2bsize;
1084 * split leaf page <sp> into <sp> and a new right page <rp>.
1086 * return <rp> pinned and its extent descriptor <rpxd>
1089 * allocate new directory page extent and
1090 * new index page(s) to cover page split(s)
1092 * allocation hint: ?
1094 n = btstack->nsplit;
1095 pxdlist.maxnpxd = pxdlist.npxd = 0;
1096 xlen = sbi->nbperpage;
1097 for (pxd = pxdlist.pxd; n > 0; n--, pxd++) {
1098 if ((rc = dbAlloc(ip, 0, (s64) xlen, &xaddr)) == 0) {
1099 PXDaddress(pxd, xaddr);
1100 PXDlength(pxd, xlen);
1107 /* undo allocation */
1111 split->pxdlist = &pxdlist;
1112 if ((rc = dtSplitPage(tid, ip, split, &rmp, &rp, &rpxd))) {
1115 /* undo allocation */
1120 ip->i_size += PSIZE;
1123 * propagate up the router entry for the leaf page just split
1125 * insert a router entry for the new page into the parent page,
1126 * propagate the insert/split up the tree by walking back the stack
1127 * of (bn of parent page, index of child page entry in parent page)
1128 * that were traversed during the search for the page that split.
1130 * the propagation of insert/split up the tree stops if the root
1131 * splits or the page inserted into doesn't have to split to hold
1134 * the parent entry for the split page remains the same, and
1135 * a new entry is inserted at its right with the first key and
1136 * block number of the new right page.
1138 * There are a maximum of 4 pages pinned at any time:
1139 * two children, left parent and right parent (when the parent splits).
1140 * keep the child pages pinned while working on the parent.
1141 * make sure that all pins are released at exit.
1143 while ((parent = BT_POP(btstack)) != NULL) {
1144 /* parent page specified by stack frame <parent> */
1146 /* keep current child pages (<lp>, <rp>) pinned */
1151 * insert router entry in parent for new right child page <rp>
1153 /* get the parent page <sp> */
1154 DT_GETPAGE(ip, parent->bn, smp, PSIZE, sp, rc);
1162 * The new key entry goes ONE AFTER the index of parent entry,
1163 * because the split was to the right.
1165 skip = parent->index + 1;
1168 * compute the key for the router entry
1170 * key suffix compression:
1171 * for internal pages that have leaf pages as children,
1172 * retain only what's needed to distinguish between
1173 * the new entry and the entry on the page to its left.
1174 * If the keys compare equal, retain the entire key.
1176 * note that compression is performed only at computing
1177 * router key at the lowest internal level.
1178 * further compression of the key between pairs of higher
1179 * level internal pages loses too much information and
1180 * the search may fail.
1181 * (e.g., two adjacent leaf pages of {a, ..., x} {xx, ...,}
1182 * results in two adjacent parent entries (a)(xx).
1183 * if split occurs between these two entries, and
1184 * if compression is applied, the router key of parent entry
1185 * of right page (x) will divert search for x into right
1186 * subtree and miss x in the left subtree.)
1188 * the entire key must be retained for the next-to-leftmost
1189 * internal key at any level of the tree, or search may fail
1192 switch (rp->header.flag & BT_TYPE) {
1195 * compute the length of prefix for suffix compression
1196 * between last entry of left page and first entry
1199 if ((sp->header.flag & BT_ROOT && skip > 1) ||
1200 sp->header.prev != 0 || skip > 1) {
1201 /* compute uppercase router prefix key */
1202 rc = ciGetLeafPrefixKey(lp,
1203 lp->header.nextindex-1,
1213 /* next to leftmost entry of
1214 lowest internal level */
1216 /* compute uppercase router key */
1217 dtGetKey(rp, 0, &key, sbi->mntflag);
1218 key.name[key.namlen] = 0;
1220 if ((sbi->mntflag & JFS_OS2) == JFS_OS2)
1224 n = NDTINTERNAL(key.namlen);
1228 dtGetKey(rp, 0, &key, sbi->mntflag);
1229 n = NDTINTERNAL(key.namlen);
1233 jfs_err("dtSplitUp(): UFO!");
1237 /* unpin left child page */
1241 * compute the data for the router entry
1243 data->xd = rpxd; /* child page xd */
1246 * parent page is full - split the parent page
1248 if (n > sp->header.freecnt) {
1249 /* init for parent page split */
1251 split->index = skip; /* index at insert */
1254 /* split->data = data; */
1256 /* unpin right child page */
1259 /* The split routines insert the new entry,
1260 * acquire txLock as appropriate.
1261 * return <rp> pinned and its block number <rbn>.
1263 rc = (sp->header.flag & BT_ROOT) ?
1264 dtSplitRoot(tid, ip, split, &rmp) :
1265 dtSplitPage(tid, ip, split, &rmp, &rp, &rpxd);
1271 /* smp and rmp are pinned */
1274 * parent page is not full - insert router entry in parent page
1277 BT_MARK_DIRTY(smp, ip);
1279 * acquire a transaction lock on the parent page
1281 tlck = txLock(tid, ip, smp, tlckDTREE | tlckENTRY);
1282 dtlck = (struct dt_lock *) & tlck->lock;
1283 ASSERT(dtlck->index == 0);
1284 lv = & dtlck->lv[0];
1286 /* linelock header */
1291 /* linelock stbl of non-root parent page */
1292 if (!(sp->header.flag & BT_ROOT)) {
1294 n = skip >> L2DTSLOTSIZE;
1295 lv->offset = sp->header.stblindex + n;
1297 ((sp->header.nextindex -
1298 1) >> L2DTSLOTSIZE) - n + 1;
1302 dtInsertEntry(sp, skip, &key, data, &dtlck);
1304 /* exit propagate up */
1309 /* unpin current split and its right page */
1314 * free remaining extents allocated for split
1318 pxd = &pxdlist.pxd[n];
1319 for (; n < pxdlist.maxnpxd; n++, pxd++)
1320 dbFree(ip, addressPXD(pxd), (s64) lengthPXD(pxd));
1325 /* Rollback dlimit allocation */
1326 if (rc && dlimit_allocation)
1327 DLIMIT_FREE_BLOCK(ip, dlimit_allocation);
1328 /* Rollback quota allocation */
1329 if (rc && quota_allocation)
1330 DQUOT_FREE_BLOCK(ip, quota_allocation);
1341 * function: Split a non-root page of a btree.
1345 * return: 0 - success;
1347 * return split and new page pinned;
1349 static int dtSplitPage(tid_t tid, struct inode *ip, struct dtsplit * split,
1350 struct metapage ** rmpp, dtpage_t ** rpp, pxd_t * rpxdp)
1353 struct metapage *smp;
1355 struct metapage *rmp;
1356 dtpage_t *rp; /* new right page allocated */
1357 s64 rbn; /* new right page block number */
1358 struct metapage *mp;
1361 struct pxdlist *pxdlist;
1363 int skip, nextindex, half, left, nxt, off, si;
1364 struct ldtentry *ldtentry;
1365 struct idtentry *idtentry;
1370 struct dt_lock *sdtlck, *rdtlck;
1372 struct dt_lock *dtlck;
1373 struct lv *slv, *rlv, *lv;
1375 /* get split page */
1377 sp = DT_PAGE(ip, smp);
1380 * allocate the new right page for the split
1382 pxdlist = split->pxdlist;
1383 pxd = &pxdlist->pxd[pxdlist->npxd];
1385 rbn = addressPXD(pxd);
1386 rmp = get_metapage(ip, rbn, PSIZE, 1);
1390 /* Allocate blocks to quota. */
1391 if (DQUOT_ALLOC_BLOCK(ip, lengthPXD(pxd))) {
1392 release_metapage(rmp);
1395 /* Allocate blocks to dlimit. */
1396 if (DLIMIT_ALLOC_BLOCK(ip, lengthPXD(pxd))) {
1397 DQUOT_FREE_BLOCK(ip, lengthPXD(pxd));
1398 release_metapage(rmp);
1402 jfs_info("dtSplitPage: ip:0x%p smp:0x%p rmp:0x%p", ip, smp, rmp);
1404 BT_MARK_DIRTY(rmp, ip);
1406 * acquire a transaction lock on the new right page
1408 tlck = txLock(tid, ip, rmp, tlckDTREE | tlckNEW);
1409 rdtlck = (struct dt_lock *) & tlck->lock;
1411 rp = (dtpage_t *) rmp->data;
1413 rp->header.self = *pxd;
1415 BT_MARK_DIRTY(smp, ip);
1417 * acquire a transaction lock on the split page
1421 tlck = txLock(tid, ip, smp, tlckDTREE | tlckENTRY);
1422 sdtlck = (struct dt_lock *) & tlck->lock;
1424 /* linelock header of split page */
1425 ASSERT(sdtlck->index == 0);
1426 slv = & sdtlck->lv[0];
1432 * initialize/update sibling pointers between sp and rp
1434 nextbn = le64_to_cpu(sp->header.next);
1435 rp->header.next = cpu_to_le64(nextbn);
1436 rp->header.prev = cpu_to_le64(addressPXD(&sp->header.self));
1437 sp->header.next = cpu_to_le64(rbn);
1440 * initialize new right page
1442 rp->header.flag = sp->header.flag;
1444 /* compute sorted entry table at start of extent data area */
1445 rp->header.nextindex = 0;
1446 rp->header.stblindex = 1;
1448 n = PSIZE >> L2DTSLOTSIZE;
1449 rp->header.maxslot = n;
1450 stblsize = (n + 31) >> L2DTSLOTSIZE; /* in unit of slot */
1453 fsi = rp->header.stblindex + stblsize;
1454 rp->header.freelist = fsi;
1455 rp->header.freecnt = rp->header.maxslot - fsi;
1458 * sequential append at tail: append without split
1460 * If splitting the last page on a level because of appending
1461 * a entry to it (skip is maxentry), it's likely that the access is
1462 * sequential. Adding an empty page on the side of the level is less
1463 * work and can push the fill factor much higher than normal.
1464 * If we're wrong it's no big deal, we'll just do the split the right
1466 * (It may look like it's equally easy to do a similar hack for
1467 * reverse sorted data, that is, split the tree left,
1468 * but it's not. Be my guest.)
1470 if (nextbn == 0 && split->index == sp->header.nextindex) {
1471 /* linelock header + stbl (first slot) of new page */
1472 rlv = & rdtlck->lv[rdtlck->index];
1478 * initialize freelist of new right page
1481 for (fsi++; fsi < rp->header.maxslot; f++, fsi++)
1485 /* insert entry at the first entry of the new right page */
1486 dtInsertEntry(rp, 0, split->key, split->data, &rdtlck);
1492 * non-sequential insert (at possibly middle page)
1496 * update prev pointer of previous right sibling page;
1499 DT_GETPAGE(ip, nextbn, mp, PSIZE, p, rc);
1501 discard_metapage(rmp);
1505 BT_MARK_DIRTY(mp, ip);
1507 * acquire a transaction lock on the next page
1509 tlck = txLock(tid, ip, mp, tlckDTREE | tlckRELINK);
1510 jfs_info("dtSplitPage: tlck = 0x%p, ip = 0x%p, mp=0x%p",
1512 dtlck = (struct dt_lock *) & tlck->lock;
1514 /* linelock header of previous right sibling page */
1515 lv = & dtlck->lv[dtlck->index];
1520 p->header.prev = cpu_to_le64(rbn);
1526 * split the data between the split and right pages.
1528 skip = split->index;
1529 half = (PSIZE >> L2DTSLOTSIZE) >> 1; /* swag */
1533 * compute fill factor for split pages
1535 * <nxt> traces the next entry to move to rp
1536 * <off> traces the next entry to stay in sp
1538 stbl = (u8 *) & sp->slot[sp->header.stblindex];
1539 nextindex = sp->header.nextindex;
1540 for (nxt = off = 0; nxt < nextindex; ++off) {
1542 /* check for fill factor with new entry size */
1546 switch (sp->header.flag & BT_TYPE) {
1548 ldtentry = (struct ldtentry *) & sp->slot[si];
1550 n = NDTLEAF(ldtentry->namlen);
1552 n = NDTLEAF_LEGACY(ldtentry->
1557 idtentry = (struct idtentry *) & sp->slot[si];
1558 n = NDTINTERNAL(idtentry->namlen);
1565 ++nxt; /* advance to next entry to move in sp */
1573 /* <nxt> poins to the 1st entry to move */
1576 * move entries to right page
1578 * dtMoveEntry() initializes rp and reserves entry for insertion
1580 * split page moved out entries are linelocked;
1581 * new/right page moved in entries are linelocked;
1583 /* linelock header + stbl of new right page */
1584 rlv = & rdtlck->lv[rdtlck->index];
1589 dtMoveEntry(sp, nxt, rp, &sdtlck, &rdtlck, DO_INDEX(ip));
1591 sp->header.nextindex = nxt;
1594 * finalize freelist of new right page
1596 fsi = rp->header.freelist;
1598 for (fsi++; fsi < rp->header.maxslot; f++, fsi++)
1603 * Update directory index table for entries now in right page
1605 if ((rp->header.flag & BT_LEAF) && DO_INDEX(ip)) {
1609 stbl = DT_GETSTBL(rp);
1610 for (n = 0; n < rp->header.nextindex; n++) {
1611 ldtentry = (struct ldtentry *) & rp->slot[stbl[n]];
1612 modify_index(tid, ip, le32_to_cpu(ldtentry->index),
1613 rbn, n, &mp, &lblock);
1616 release_metapage(mp);
1620 * the skipped index was on the left page,
1623 /* insert the new entry in the split page */
1624 dtInsertEntry(sp, skip, split->key, split->data, &sdtlck);
1626 /* linelock stbl of split page */
1627 if (sdtlck->index >= sdtlck->maxcnt)
1628 sdtlck = (struct dt_lock *) txLinelock(sdtlck);
1629 slv = & sdtlck->lv[sdtlck->index];
1630 n = skip >> L2DTSLOTSIZE;
1631 slv->offset = sp->header.stblindex + n;
1633 ((sp->header.nextindex - 1) >> L2DTSLOTSIZE) - n + 1;
1637 * the skipped index was on the right page,
1640 /* adjust the skip index to reflect the new position */
1643 /* insert the new entry in the right page */
1644 dtInsertEntry(rp, skip, split->key, split->data, &rdtlck);
1658 * function: extend 1st/only directory leaf page
1662 * return: 0 - success;
1664 * return extended page pinned;
1666 static int dtExtendPage(tid_t tid,
1667 struct inode *ip, struct dtsplit * split, struct btstack * btstack)
1669 struct super_block *sb = ip->i_sb;
1671 struct metapage *smp, *pmp, *mp;
1673 struct pxdlist *pxdlist;
1676 int newstblindex, newstblsize;
1677 int oldstblindex, oldstblsize;
1680 struct btframe *parent;
1682 struct dt_lock *dtlck;
1685 struct pxd_lock *pxdlock;
1688 struct ldtentry *ldtentry;
1691 /* get page to extend */
1693 sp = DT_PAGE(ip, smp);
1695 /* get parent/root page */
1696 parent = BT_POP(btstack);
1697 DT_GETPAGE(ip, parent->bn, pmp, PSIZE, pp, rc);
1704 pxdlist = split->pxdlist;
1705 pxd = &pxdlist->pxd[pxdlist->npxd];
1708 xaddr = addressPXD(pxd);
1709 tpxd = &sp->header.self;
1710 txaddr = addressPXD(tpxd);
1711 /* in-place extension */
1712 if (xaddr == txaddr) {
1719 /* save moved extent descriptor for later free */
1720 tlck = txMaplock(tid, ip, tlckDTREE | tlckRELOCATE);
1721 pxdlock = (struct pxd_lock *) & tlck->lock;
1722 pxdlock->flag = mlckFREEPXD;
1723 pxdlock->pxd = sp->header.self;
1727 * Update directory index table to reflect new page address
1733 stbl = DT_GETSTBL(sp);
1734 for (n = 0; n < sp->header.nextindex; n++) {
1736 (struct ldtentry *) & sp->slot[stbl[n]];
1737 modify_index(tid, ip,
1738 le32_to_cpu(ldtentry->index),
1739 xaddr, n, &mp, &lblock);
1742 release_metapage(mp);
1749 sp->header.self = *pxd;
1751 jfs_info("dtExtendPage: ip:0x%p smp:0x%p sp:0x%p", ip, smp, sp);
1753 BT_MARK_DIRTY(smp, ip);
1755 * acquire a transaction lock on the extended/leaf page
1757 tlck = txLock(tid, ip, smp, tlckDTREE | type);
1758 dtlck = (struct dt_lock *) & tlck->lock;
1759 lv = & dtlck->lv[0];
1761 /* update buffer extent descriptor of extended page */
1762 xlen = lengthPXD(pxd);
1763 xsize = xlen << JFS_SBI(sb)->l2bsize;
1764 #ifdef _STILL_TO_PORT
1765 bmSetXD(smp, xaddr, xsize);
1766 #endif /* _STILL_TO_PORT */
1769 * copy old stbl to new stbl at start of extended area
1771 oldstblindex = sp->header.stblindex;
1772 oldstblsize = (sp->header.maxslot + 31) >> L2DTSLOTSIZE;
1773 newstblindex = sp->header.maxslot;
1774 n = xsize >> L2DTSLOTSIZE;
1775 newstblsize = (n + 31) >> L2DTSLOTSIZE;
1776 memcpy(&sp->slot[newstblindex], &sp->slot[oldstblindex],
1777 sp->header.nextindex);
1780 * in-line extension: linelock old area of extended page
1782 if (type == tlckEXTEND) {
1783 /* linelock header */
1789 /* linelock new stbl of extended page */
1790 lv->offset = newstblindex;
1791 lv->length = newstblsize;
1794 * relocation: linelock whole relocated area
1798 lv->length = sp->header.maxslot + newstblsize;
1803 sp->header.maxslot = n;
1804 sp->header.stblindex = newstblindex;
1805 /* sp->header.nextindex remains the same */
1808 * add old stbl region at head of freelist
1812 last = sp->header.freelist;
1813 for (n = 0; n < oldstblsize; n++, fsi++, f++) {
1817 sp->header.freelist = last;
1818 sp->header.freecnt += oldstblsize;
1821 * append free region of newly extended area at tail of freelist
1823 /* init free region of newly extended area */
1824 fsi = n = newstblindex + newstblsize;
1826 for (fsi++; fsi < sp->header.maxslot; f++, fsi++)
1830 /* append new free region at tail of old freelist */
1831 fsi = sp->header.freelist;
1833 sp->header.freelist = n;
1838 } while (fsi != -1);
1843 sp->header.freecnt += sp->header.maxslot - n;
1846 * insert the new entry
1848 dtInsertEntry(sp, split->index, split->key, split->data, &dtlck);
1850 BT_MARK_DIRTY(pmp, ip);
1852 * linelock any freeslots residing in old extent
1854 if (type == tlckEXTEND) {
1855 n = sp->header.maxslot >> 2;
1856 if (sp->header.freelist < n)
1857 dtLinelockFreelist(sp, n, &dtlck);
1861 * update parent entry on the parent/root page
1864 * acquire a transaction lock on the parent/root page
1866 tlck = txLock(tid, ip, pmp, tlckDTREE | tlckENTRY);
1867 dtlck = (struct dt_lock *) & tlck->lock;
1868 lv = & dtlck->lv[dtlck->index];
1870 /* linelock parent entry - 1st slot */
1875 /* update the parent pxd for page extension */
1876 tpxd = (pxd_t *) & pp->slot[1];
1888 * split the full root page into
1889 * original/root/split page and new right page
1890 * i.e., root remains fixed in tree anchor (inode) and
1891 * the root is copied to a single new right child page
1892 * since root page << non-root page, and
1893 * the split root page contains a single entry for the
1894 * new right child page.
1898 * return: 0 - success;
1900 * return new page pinned;
1902 static int dtSplitRoot(tid_t tid,
1903 struct inode *ip, struct dtsplit * split, struct metapage ** rmpp)
1905 struct super_block *sb = ip->i_sb;
1906 struct metapage *smp;
1908 struct metapage *rmp;
1915 int fsi, stblsize, n;
1918 struct pxdlist *pxdlist;
1920 struct dt_lock *dtlck;
1924 /* get split root page */
1926 sp = &JFS_IP(ip)->i_dtroot;
1929 * allocate/initialize a single (right) child page
1931 * N.B. at first split, a one (or two) block to fit new entry
1932 * is allocated; at subsequent split, a full page is allocated;
1934 pxdlist = split->pxdlist;
1935 pxd = &pxdlist->pxd[pxdlist->npxd];
1937 rbn = addressPXD(pxd);
1938 xlen = lengthPXD(pxd);
1939 xsize = xlen << JFS_SBI(sb)->l2bsize;
1940 rmp = get_metapage(ip, rbn, xsize, 1);
1946 /* Allocate blocks to quota. */
1947 if (DQUOT_ALLOC_BLOCK(ip, lengthPXD(pxd))) {
1948 release_metapage(rmp);
1951 /* Allocate blocks to dlimit. */
1952 if (DLIMIT_ALLOC_BLOCK(ip, lengthPXD(pxd))) {
1953 DQUOT_FREE_BLOCK(ip, lengthPXD(pxd));
1954 release_metapage(rmp);
1958 BT_MARK_DIRTY(rmp, ip);
1960 * acquire a transaction lock on the new right page
1962 tlck = txLock(tid, ip, rmp, tlckDTREE | tlckNEW);
1963 dtlck = (struct dt_lock *) & tlck->lock;
1966 (sp->header.flag & BT_LEAF) ? BT_LEAF : BT_INTERNAL;
1967 rp->header.self = *pxd;
1969 /* initialize sibling pointers */
1970 rp->header.next = 0;
1971 rp->header.prev = 0;
1974 * move in-line root page into new right page extent
1976 /* linelock header + copied entries + new stbl (1st slot) in new page */
1977 ASSERT(dtlck->index == 0);
1978 lv = & dtlck->lv[0];
1980 lv->length = 10; /* 1 + 8 + 1 */
1983 n = xsize >> L2DTSLOTSIZE;
1984 rp->header.maxslot = n;
1985 stblsize = (n + 31) >> L2DTSLOTSIZE;
1987 /* copy old stbl to new stbl at start of extended area */
1988 rp->header.stblindex = DTROOTMAXSLOT;
1989 stbl = (s8 *) & rp->slot[DTROOTMAXSLOT];
1990 memcpy(stbl, sp->header.stbl, sp->header.nextindex);
1991 rp->header.nextindex = sp->header.nextindex;
1993 /* copy old data area to start of new data area */
1994 memcpy(&rp->slot[1], &sp->slot[1], IDATASIZE);
1997 * append free region of newly extended area at tail of freelist
1999 /* init free region of newly extended area */
2000 fsi = n = DTROOTMAXSLOT + stblsize;
2002 for (fsi++; fsi < rp->header.maxslot; f++, fsi++)
2006 /* append new free region at tail of old freelist */
2007 fsi = sp->header.freelist;
2009 rp->header.freelist = n;
2011 rp->header.freelist = fsi;
2016 } while (fsi != -1);
2021 rp->header.freecnt = sp->header.freecnt + rp->header.maxslot - n;
2024 * Update directory index table for entries now in right page
2026 if ((rp->header.flag & BT_LEAF) && DO_INDEX(ip)) {
2028 struct metapage *mp = NULL;
2029 struct ldtentry *ldtentry;
2031 stbl = DT_GETSTBL(rp);
2032 for (n = 0; n < rp->header.nextindex; n++) {
2033 ldtentry = (struct ldtentry *) & rp->slot[stbl[n]];
2034 modify_index(tid, ip, le32_to_cpu(ldtentry->index),
2035 rbn, n, &mp, &lblock);
2038 release_metapage(mp);
2041 * insert the new entry into the new right/child page
2042 * (skip index in the new right page will not change)
2044 dtInsertEntry(rp, split->index, split->key, split->data, &dtlck);
2047 * reset parent/root page
2049 * set the 1st entry offset to 0, which force the left-most key
2050 * at any level of the tree to be less than any search key.
2052 * The btree comparison code guarantees that the left-most key on any
2053 * level of the tree is never used, so it doesn't need to be filled in.
2055 BT_MARK_DIRTY(smp, ip);
2057 * acquire a transaction lock on the root page (in-memory inode)
2059 tlck = txLock(tid, ip, smp, tlckDTREE | tlckNEW | tlckBTROOT);
2060 dtlck = (struct dt_lock *) & tlck->lock;
2063 ASSERT(dtlck->index == 0);
2064 lv = & dtlck->lv[0];
2066 lv->length = DTROOTMAXSLOT;
2069 /* update page header of root */
2070 if (sp->header.flag & BT_LEAF) {
2071 sp->header.flag &= ~BT_LEAF;
2072 sp->header.flag |= BT_INTERNAL;
2075 /* init the first entry */
2076 s = (struct idtentry *) & sp->slot[DTENTRYSTART];
2082 stbl = sp->header.stbl;
2083 stbl[0] = DTENTRYSTART;
2084 sp->header.nextindex = 1;
2087 fsi = DTENTRYSTART + 1;
2090 /* init free region of remaining area */
2091 for (fsi++; fsi < DTROOTMAXSLOT; f++, fsi++)
2095 sp->header.freelist = DTENTRYSTART + 1;
2096 sp->header.freecnt = DTROOTMAXSLOT - (DTENTRYSTART + 1);
2107 * function: delete the entry(s) referenced by a key.
2113 int dtDelete(tid_t tid,
2114 struct inode *ip, struct component_name * key, ino_t * ino, int flag)
2118 struct metapage *mp, *imp;
2121 struct btstack btstack;
2122 struct dt_lock *dtlck;
2126 struct ldtentry *ldtentry;
2128 u32 table_index, next_index;
2129 struct metapage *nmp;
2133 * search for the entry to delete:
2135 * dtSearch() returns (leaf page pinned, index at which to delete).
2137 if ((rc = dtSearch(ip, key, ino, &btstack, flag)))
2140 /* retrieve search result */
2141 DT_GETSEARCH(ip, btstack.top, bn, mp, p, index);
2144 * We need to find put the index of the next entry into the
2145 * directory index table in order to resume a readdir from this
2149 stbl = DT_GETSTBL(p);
2150 ldtentry = (struct ldtentry *) & p->slot[stbl[index]];
2151 table_index = le32_to_cpu(ldtentry->index);
2152 if (index == (p->header.nextindex - 1)) {
2154 * Last entry in this leaf page
2156 if ((p->header.flag & BT_ROOT)
2157 || (p->header.next == 0))
2160 /* Read next leaf page */
2161 DT_GETPAGE(ip, le64_to_cpu(p->header.next),
2162 nmp, PSIZE, np, rc);
2166 stbl = DT_GETSTBL(np);
2168 (struct ldtentry *) & np->
2171 le32_to_cpu(ldtentry->index);
2177 (struct ldtentry *) & p->slot[stbl[index + 1]];
2178 next_index = le32_to_cpu(ldtentry->index);
2180 free_index(tid, ip, table_index, next_index);
2183 * the leaf page becomes empty, delete the page
2185 if (p->header.nextindex == 1) {
2186 /* delete empty page */
2187 rc = dtDeleteUp(tid, ip, mp, p, &btstack);
2190 * the leaf page has other entries remaining:
2192 * delete the entry from the leaf page.
2195 BT_MARK_DIRTY(mp, ip);
2197 * acquire a transaction lock on the leaf page
2199 tlck = txLock(tid, ip, mp, tlckDTREE | tlckENTRY);
2200 dtlck = (struct dt_lock *) & tlck->lock;
2203 * Do not assume that dtlck->index will be zero. During a
2204 * rename within a directory, this transaction may have
2205 * modified this page already when adding the new entry.
2208 /* linelock header */
2209 if (dtlck->index >= dtlck->maxcnt)
2210 dtlck = (struct dt_lock *) txLinelock(dtlck);
2211 lv = & dtlck->lv[dtlck->index];
2216 /* linelock stbl of non-root leaf page */
2217 if (!(p->header.flag & BT_ROOT)) {
2218 if (dtlck->index >= dtlck->maxcnt)
2219 dtlck = (struct dt_lock *) txLinelock(dtlck);
2220 lv = & dtlck->lv[dtlck->index];
2221 i = index >> L2DTSLOTSIZE;
2222 lv->offset = p->header.stblindex + i;
2224 ((p->header.nextindex - 1) >> L2DTSLOTSIZE) -
2229 /* free the leaf entry */
2230 dtDeleteEntry(p, index, &dtlck);
2233 * Update directory index table for entries moved in stbl
2235 if (DO_INDEX(ip) && index < p->header.nextindex) {
2239 stbl = DT_GETSTBL(p);
2240 for (i = index; i < p->header.nextindex; i++) {
2242 (struct ldtentry *) & p->slot[stbl[i]];
2243 modify_index(tid, ip,
2244 le32_to_cpu(ldtentry->index),
2245 bn, i, &imp, &lblock);
2248 release_metapage(imp);
2262 * free empty pages as propagating deletion up the tree
2268 static int dtDeleteUp(tid_t tid, struct inode *ip,
2269 struct metapage * fmp, dtpage_t * fp, struct btstack * btstack)
2272 struct metapage *mp;
2274 int index, nextindex;
2276 struct btframe *parent;
2277 struct dt_lock *dtlck;
2280 struct pxd_lock *pxdlock;
2284 * keep the root leaf page which has become empty
2286 if (BT_IS_ROOT(fmp)) {
2290 * dtInitRoot() acquires txlock on the root
2292 dtInitRoot(tid, ip, PARENT(ip));
2300 * free the non-root leaf page
2303 * acquire a transaction lock on the page
2305 * write FREEXTENT|NOREDOPAGE log record
2306 * N.B. linelock is overlaid as freed extent descriptor, and
2307 * the buffer page is freed;
2309 tlck = txMaplock(tid, ip, tlckDTREE | tlckFREE);
2310 pxdlock = (struct pxd_lock *) & tlck->lock;
2311 pxdlock->flag = mlckFREEPXD;
2312 pxdlock->pxd = fp->header.self;
2315 /* update sibling pointers */
2316 if ((rc = dtRelink(tid, ip, fp))) {
2321 xlen = lengthPXD(&fp->header.self);
2323 /* Free dlimit allocation. */
2324 DLIMIT_FREE_BLOCK(ip, xlen);
2325 /* Free quota allocation. */
2326 DQUOT_FREE_BLOCK(ip, xlen);
2328 /* free/invalidate its buffer page */
2329 discard_metapage(fmp);
2332 * propagate page deletion up the directory tree
2334 * If the delete from the parent page makes it empty,
2335 * continue all the way up the tree.
2336 * stop if the root page is reached (which is never deleted) or
2337 * if the entry deletion does not empty the page.
2339 while ((parent = BT_POP(btstack)) != NULL) {
2340 /* pin the parent page <sp> */
2341 DT_GETPAGE(ip, parent->bn, mp, PSIZE, p, rc);
2346 * free the extent of the child page deleted
2348 index = parent->index;
2351 * delete the entry for the child page from parent
2353 nextindex = p->header.nextindex;
2356 * the parent has the single entry being deleted:
2358 * free the parent page which has become empty.
2360 if (nextindex == 1) {
2362 * keep the root internal page which has become empty
2364 if (p->header.flag & BT_ROOT) {
2368 * dtInitRoot() acquires txlock on the root
2370 dtInitRoot(tid, ip, PARENT(ip));
2377 * free the parent page
2381 * acquire a transaction lock on the page
2383 * write FREEXTENT|NOREDOPAGE log record
2387 tlckDTREE | tlckFREE);
2388 pxdlock = (struct pxd_lock *) & tlck->lock;
2389 pxdlock->flag = mlckFREEPXD;
2390 pxdlock->pxd = p->header.self;
2393 /* update sibling pointers */
2394 if ((rc = dtRelink(tid, ip, p))) {
2399 xlen = lengthPXD(&p->header.self);
2401 /* Free dlimit allocation */
2402 DLIMIT_FREE_BLOCK(ip, xlen);
2403 /* Free quota allocation */
2404 DQUOT_FREE_BLOCK(ip, xlen);
2406 /* free/invalidate its buffer page */
2407 discard_metapage(mp);
2415 * the parent has other entries remaining:
2417 * delete the router entry from the parent page.
2419 BT_MARK_DIRTY(mp, ip);
2421 * acquire a transaction lock on the page
2423 * action: router entry deletion
2425 tlck = txLock(tid, ip, mp, tlckDTREE | tlckENTRY);
2426 dtlck = (struct dt_lock *) & tlck->lock;
2428 /* linelock header */
2429 if (dtlck->index >= dtlck->maxcnt)
2430 dtlck = (struct dt_lock *) txLinelock(dtlck);
2431 lv = & dtlck->lv[dtlck->index];
2436 /* linelock stbl of non-root leaf page */
2437 if (!(p->header.flag & BT_ROOT)) {
2438 if (dtlck->index < dtlck->maxcnt)
2441 dtlck = (struct dt_lock *) txLinelock(dtlck);
2442 lv = & dtlck->lv[0];
2444 i = index >> L2DTSLOTSIZE;
2445 lv->offset = p->header.stblindex + i;
2447 ((p->header.nextindex - 1) >> L2DTSLOTSIZE) -
2452 /* free the router entry */
2453 dtDeleteEntry(p, index, &dtlck);
2455 /* reset key of new leftmost entry of level (for consistency) */
2457 ((p->header.flag & BT_ROOT) || p->header.prev == 0))
2458 dtTruncateEntry(p, 0, &dtlck);
2460 /* unpin the parent page */
2463 /* exit propagation up */
2468 ip->i_size -= PSIZE;
2475 * NAME: dtRelocate()
2477 * FUNCTION: relocate dtpage (internal or leaf) of directory;
2478 * This function is mainly used by defragfs utility.
2480 int dtRelocate(tid_t tid, struct inode *ip, s64 lmxaddr, pxd_t * opxd,
2484 struct metapage *mp, *pmp, *lmp, *rmp;
2485 dtpage_t *p, *pp, *rp = 0, *lp= 0;
2488 struct btstack btstack;
2490 s64 oxaddr, nextbn, prevbn;
2493 struct dt_lock *dtlck;
2494 struct pxd_lock *pxdlock;
2498 oxaddr = addressPXD(opxd);
2499 xlen = lengthPXD(opxd);
2501 jfs_info("dtRelocate: lmxaddr:%Ld xaddr:%Ld:%Ld xlen:%d",
2502 (long long)lmxaddr, (long long)oxaddr, (long long)nxaddr,
2506 * 1. get the internal parent dtpage covering
2507 * router entry for the tartget page to be relocated;
2509 rc = dtSearchNode(ip, lmxaddr, opxd, &btstack);
2513 /* retrieve search result */
2514 DT_GETSEARCH(ip, btstack.top, bn, pmp, pp, index);
2515 jfs_info("dtRelocate: parent router entry validated.");
2518 * 2. relocate the target dtpage
2520 /* read in the target page from src extent */
2521 DT_GETPAGE(ip, oxaddr, mp, PSIZE, p, rc);
2523 /* release the pinned parent page */
2529 * read in sibling pages if any to update sibling pointers;
2532 if (p->header.next) {
2533 nextbn = le64_to_cpu(p->header.next);
2534 DT_GETPAGE(ip, nextbn, rmp, PSIZE, rp, rc);
2543 if (p->header.prev) {
2544 prevbn = le64_to_cpu(p->header.prev);
2545 DT_GETPAGE(ip, prevbn, lmp, PSIZE, lp, rc);
2555 /* at this point, all xtpages to be updated are in memory */
2558 * update sibling pointers of sibling dtpages if any;
2561 tlck = txLock(tid, ip, lmp, tlckDTREE | tlckRELINK);
2562 dtlck = (struct dt_lock *) & tlck->lock;
2563 /* linelock header */
2564 ASSERT(dtlck->index == 0);
2565 lv = & dtlck->lv[0];
2570 lp->header.next = cpu_to_le64(nxaddr);
2575 tlck = txLock(tid, ip, rmp, tlckDTREE | tlckRELINK);
2576 dtlck = (struct dt_lock *) & tlck->lock;
2577 /* linelock header */
2578 ASSERT(dtlck->index == 0);
2579 lv = & dtlck->lv[0];
2584 rp->header.prev = cpu_to_le64(nxaddr);
2589 * update the target dtpage to be relocated
2591 * write LOG_REDOPAGE of LOG_NEW type for dst page
2592 * for the whole target page (logredo() will apply
2593 * after image and update bmap for allocation of the
2594 * dst extent), and update bmap for allocation of
2597 tlck = txLock(tid, ip, mp, tlckDTREE | tlckNEW);
2598 dtlck = (struct dt_lock *) & tlck->lock;
2599 /* linelock header */
2600 ASSERT(dtlck->index == 0);
2601 lv = & dtlck->lv[0];
2603 /* update the self address in the dtpage header */
2604 pxd = &p->header.self;
2605 PXDaddress(pxd, nxaddr);
2607 /* the dst page is the same as the src page, i.e.,
2608 * linelock for afterimage of the whole page;
2611 lv->length = p->header.maxslot;
2614 /* update the buffer extent descriptor of the dtpage */
2615 xsize = xlen << JFS_SBI(ip->i_sb)->l2bsize;
2616 #ifdef _STILL_TO_PORT
2617 bmSetXD(mp, nxaddr, xsize);
2618 #endif /* _STILL_TO_PORT */
2619 /* unpin the relocated page */
2621 jfs_info("dtRelocate: target dtpage relocated.");
2623 /* the moved extent is dtpage, then a LOG_NOREDOPAGE log rec
2624 * needs to be written (in logredo(), the LOG_NOREDOPAGE log rec
2625 * will also force a bmap update ).
2629 * 3. acquire maplock for the source extent to be freed;
2631 /* for dtpage relocation, write a LOG_NOREDOPAGE record
2632 * for the source dtpage (logredo() will init NoRedoPage
2633 * filter and will also update bmap for free of the source
2634 * dtpage), and upadte bmap for free of the source dtpage;
2636 tlck = txMaplock(tid, ip, tlckDTREE | tlckFREE);
2637 pxdlock = (struct pxd_lock *) & tlck->lock;
2638 pxdlock->flag = mlckFREEPXD;
2639 PXDaddress(&pxdlock->pxd, oxaddr);
2640 PXDlength(&pxdlock->pxd, xlen);
2644 * 4. update the parent router entry for relocation;
2646 * acquire tlck for the parent entry covering the target dtpage;
2647 * write LOG_REDOPAGE to apply after image only;
2649 jfs_info("dtRelocate: update parent router entry.");
2650 tlck = txLock(tid, ip, pmp, tlckDTREE | tlckENTRY);
2651 dtlck = (struct dt_lock *) & tlck->lock;
2652 lv = & dtlck->lv[dtlck->index];
2654 /* update the PXD with the new address */
2655 stbl = DT_GETSTBL(pp);
2656 pxd = (pxd_t *) & pp->slot[stbl[index]];
2657 PXDaddress(pxd, nxaddr);
2658 lv->offset = stbl[index];
2662 /* unpin the parent dtpage */
2669 * NAME: dtSearchNode()
2671 * FUNCTION: Search for an dtpage containing a specified address
2672 * This function is mainly used by defragfs utility.
2674 * NOTE: Search result on stack, the found page is pinned at exit.
2675 * The result page must be an internal dtpage.
2676 * lmxaddr give the address of the left most page of the
2677 * dtree level, in which the required dtpage resides.
2679 static int dtSearchNode(struct inode *ip, s64 lmxaddr, pxd_t * kpxd,
2680 struct btstack * btstack)
2684 struct metapage *mp;
2686 int psize = 288; /* initial in-line directory */
2690 struct btframe *btsp;
2692 BT_CLR(btstack); /* reset stack */
2695 * descend tree to the level with specified leftmost page
2697 * by convention, root bn = 0.
2700 /* get/pin the page to search */
2701 DT_GETPAGE(ip, bn, mp, psize, p, rc);
2705 /* does the xaddr of leftmost page of the levevl
2706 * matches levevl search key ?
2708 if (p->header.flag & BT_ROOT) {
2711 } else if (addressPXD(&p->header.self) == lmxaddr)
2715 * descend down to leftmost child page
2717 if (p->header.flag & BT_LEAF) {
2722 /* get the leftmost entry */
2723 stbl = DT_GETSTBL(p);
2724 pxd = (pxd_t *) & p->slot[stbl[0]];
2726 /* get the child page block address */
2727 bn = addressPXD(pxd);
2728 psize = lengthPXD(pxd) << JFS_SBI(ip->i_sb)->l2bsize;
2729 /* unpin the parent page */
2734 * search each page at the current levevl
2737 stbl = DT_GETSTBL(p);
2738 for (i = 0; i < p->header.nextindex; i++) {
2739 pxd = (pxd_t *) & p->slot[stbl[i]];
2741 /* found the specified router entry */
2742 if (addressPXD(pxd) == addressPXD(kpxd) &&
2743 lengthPXD(pxd) == lengthPXD(kpxd)) {
2744 btsp = btstack->top;
2753 /* get the right sibling page if any */
2755 bn = le64_to_cpu(p->header.next);
2761 /* unpin current page */
2764 /* get the right sibling page */
2765 DT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
2771 #endif /* _NOTYET */
2777 * link around a freed page.
2780 * fp: page to be freed
2784 static int dtRelink(tid_t tid, struct inode *ip, dtpage_t * p)
2787 struct metapage *mp;
2790 struct dt_lock *dtlck;
2793 nextbn = le64_to_cpu(p->header.next);
2794 prevbn = le64_to_cpu(p->header.prev);
2796 /* update prev pointer of the next page */
2798 DT_GETPAGE(ip, nextbn, mp, PSIZE, p, rc);
2802 BT_MARK_DIRTY(mp, ip);
2804 * acquire a transaction lock on the next page
2806 * action: update prev pointer;
2808 tlck = txLock(tid, ip, mp, tlckDTREE | tlckRELINK);
2809 jfs_info("dtRelink nextbn: tlck = 0x%p, ip = 0x%p, mp=0x%p",
2811 dtlck = (struct dt_lock *) & tlck->lock;
2813 /* linelock header */
2814 if (dtlck->index >= dtlck->maxcnt)
2815 dtlck = (struct dt_lock *) txLinelock(dtlck);
2816 lv = & dtlck->lv[dtlck->index];
2821 p->header.prev = cpu_to_le64(prevbn);
2825 /* update next pointer of the previous page */
2827 DT_GETPAGE(ip, prevbn, mp, PSIZE, p, rc);
2831 BT_MARK_DIRTY(mp, ip);
2833 * acquire a transaction lock on the prev page
2835 * action: update next pointer;
2837 tlck = txLock(tid, ip, mp, tlckDTREE | tlckRELINK);
2838 jfs_info("dtRelink prevbn: tlck = 0x%p, ip = 0x%p, mp=0x%p",
2840 dtlck = (struct dt_lock *) & tlck->lock;
2842 /* linelock header */
2843 if (dtlck->index >= dtlck->maxcnt)
2844 dtlck = (struct dt_lock *) txLinelock(dtlck);
2845 lv = & dtlck->lv[dtlck->index];
2850 p->header.next = cpu_to_le64(nextbn);
2861 * initialize directory root (inline in inode)
2863 void dtInitRoot(tid_t tid, struct inode *ip, u32 idotdot)
2865 struct jfs_inode_info *jfs_ip = JFS_IP(ip);
2870 struct dt_lock *dtlck;
2875 * If this was previously an non-empty directory, we need to remove
2876 * the old directory table.
2879 if (!jfs_dirtable_inline(ip)) {
2880 struct tblock *tblk = tid_to_tblock(tid);
2882 * We're playing games with the tid's xflag. If
2883 * we're removing a regular file, the file's xtree
2884 * is committed with COMMIT_PMAP, but we always
2885 * commit the directories xtree with COMMIT_PWMAP.
2887 xflag_save = tblk->xflag;
2890 * xtTruncate isn't guaranteed to fully truncate
2891 * the xtree. The caller needs to check i_size
2892 * after committing the transaction to see if
2893 * additional truncation is needed. The
2894 * COMMIT_Stale flag tells caller that we
2895 * initiated the truncation.
2897 xtTruncate(tid, ip, 0, COMMIT_PWMAP);
2898 set_cflag(COMMIT_Stale, ip);
2900 tblk->xflag = xflag_save;
2904 jfs_ip->next_index = 2;
2906 ip->i_size = IDATASIZE;
2909 * acquire a transaction lock on the root
2911 * action: directory initialization;
2913 tlck = txLock(tid, ip, (struct metapage *) & jfs_ip->bxflag,
2914 tlckDTREE | tlckENTRY | tlckBTROOT);
2915 dtlck = (struct dt_lock *) & tlck->lock;
2918 ASSERT(dtlck->index == 0);
2919 lv = & dtlck->lv[0];
2921 lv->length = DTROOTMAXSLOT;
2924 p = &jfs_ip->i_dtroot;
2926 p->header.flag = DXD_INDEX | BT_ROOT | BT_LEAF;
2928 p->header.nextindex = 0;
2934 /* init data area of root */
2935 for (fsi++; fsi < DTROOTMAXSLOT; f++, fsi++)
2939 p->header.freelist = 1;
2940 p->header.freecnt = 8;
2942 /* init '..' entry */
2943 p->header.idotdot = cpu_to_le32(idotdot);
2949 * add_missing_indices()
2951 * function: Fix dtree page in which one or more entries has an invalid index.
2952 * fsck.jfs should really fix this, but it currently does not.
2953 * Called from jfs_readdir when bad index is detected.
2955 static void add_missing_indices(struct inode *inode, s64 bn)
2958 struct dt_lock *dtlck;
2962 struct metapage *mp;
2969 tid = txBegin(inode->i_sb, 0);
2971 DT_GETPAGE(inode, bn, mp, PSIZE, p, rc);
2974 printk(KERN_ERR "DT_GETPAGE failed!\n");
2977 BT_MARK_DIRTY(mp, inode);
2979 ASSERT(p->header.flag & BT_LEAF);
2981 tlck = txLock(tid, inode, mp, tlckDTREE | tlckENTRY);
2983 tlck->type |= tlckBTROOT;
2985 dtlck = (struct dt_lock *) &tlck->lock;
2987 stbl = DT_GETSTBL(p);
2988 for (i = 0; i < p->header.nextindex; i++) {
2989 d = (struct ldtentry *) &p->slot[stbl[i]];
2990 index = le32_to_cpu(d->index);
2991 if ((index < 2) || (index >= JFS_IP(inode)->next_index)) {
2992 d->index = cpu_to_le32(add_index(tid, inode, bn, i));
2993 if (dtlck->index >= dtlck->maxcnt)
2994 dtlck = (struct dt_lock *) txLinelock(dtlck);
2995 lv = &dtlck->lv[dtlck->index];
2996 lv->offset = stbl[i];
3003 (void) txCommit(tid, 1, &inode, 0);
3009 * Buffer to hold directory entry info while traversing a dtree page
3010 * before being fed to the filldir function
3020 * function to determine next variable-sized jfs_dirent in buffer
3022 static inline struct jfs_dirent *next_jfs_dirent(struct jfs_dirent *dirent)
3024 return (struct jfs_dirent *)
3026 ((sizeof (struct jfs_dirent) + dirent->name_len + 1 +
3027 sizeof (loff_t) - 1) &
3028 ~(sizeof (loff_t) - 1)));
3034 * function: read directory entries sequentially
3035 * from the specified entry offset
3039 * return: offset = (pn, index) of start entry
3040 * of next jfs_readdir()/dtRead()
3042 int jfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
3044 struct inode *ip = filp->f_dentry->d_inode;
3045 struct nls_table *codepage = JFS_SBI(ip->i_sb)->nls_tab;
3047 loff_t dtpos; /* legacy OS/2 style position */
3052 } *dtoffset = (struct dtoffset *) &dtpos;
3054 struct metapage *mp;
3058 struct btstack btstack;
3062 int d_namleft, len, outlen;
3063 unsigned long dirent_buf;
3067 uint loop_count = 0;
3068 struct jfs_dirent *jfs_dirent;
3070 int overflow, fix_page, page_fixed = 0;
3071 static int unique_pos = 2; /* If we can't fix broken index */
3073 if (filp->f_pos == DIREND)
3078 * persistent index is stored in directory entries.
3079 * Special cases: 0 = .
3081 * -1 = End of directory
3085 dir_index = (u32) filp->f_pos;
3087 if (dir_index > 1) {
3088 struct dir_table_slot dirtab_slot;
3091 (dir_index >= JFS_IP(ip)->next_index)) {
3092 /* Stale position. Directory has shrunk */
3093 filp->f_pos = DIREND;
3097 rc = read_index(ip, dir_index, &dirtab_slot);
3099 filp->f_pos = DIREND;
3102 if (dirtab_slot.flag == DIR_INDEX_FREE) {
3103 if (loop_count++ > JFS_IP(ip)->next_index) {
3104 jfs_err("jfs_readdir detected "
3106 filp->f_pos = DIREND;
3109 dir_index = le32_to_cpu(dirtab_slot.addr2);
3110 if (dir_index == -1) {
3111 filp->f_pos = DIREND;
3116 bn = addressDTS(&dirtab_slot);
3117 index = dirtab_slot.slot;
3118 DT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
3120 filp->f_pos = DIREND;
3123 if (p->header.flag & BT_INTERNAL) {
3124 jfs_err("jfs_readdir: bad index table");
3130 if (dir_index == 0) {
3135 if (filldir(dirent, ".", 1, 0, ip->i_ino,
3143 if (filldir(dirent, "..", 2, 1, PARENT(ip), DT_DIR))
3147 * Find first entry of left-most leaf
3150 filp->f_pos = DIREND;
3154 if ((rc = dtReadFirst(ip, &btstack)))
3157 DT_GETSEARCH(ip, btstack.top, bn, mp, p, index);
3161 * Legacy filesystem - OS/2 & Linux JFS < 0.3.6
3163 * pn = index = 0: First entry "."
3164 * pn = 0; index = 1: Second entry ".."
3165 * pn > 0: Real entries, pn=1 -> leftmost page
3166 * pn = index = -1: No more entries
3168 dtpos = filp->f_pos;
3170 /* build "." entry */
3172 if (filldir(dirent, ".", 1, filp->f_pos, ip->i_ino,
3175 dtoffset->index = 1;
3176 filp->f_pos = dtpos;
3179 if (dtoffset->pn == 0) {
3180 if (dtoffset->index == 1) {
3181 /* build ".." entry */
3183 if (filldir(dirent, "..", 2, filp->f_pos,
3184 PARENT(ip), DT_DIR))
3187 jfs_err("jfs_readdir called with "
3191 dtoffset->index = 0;
3192 filp->f_pos = dtpos;
3196 filp->f_pos = DIREND;
3200 if ((rc = dtReadNext(ip, &filp->f_pos, &btstack))) {
3201 jfs_err("jfs_readdir: unexpected rc = %d "
3202 "from dtReadNext", rc);
3203 filp->f_pos = DIREND;
3206 /* get start leaf page and index */
3207 DT_GETSEARCH(ip, btstack.top, bn, mp, p, index);
3209 /* offset beyond directory eof ? */
3211 filp->f_pos = DIREND;
3216 dirent_buf = __get_free_page(GFP_KERNEL);
3217 if (dirent_buf == 0) {
3219 jfs_warn("jfs_readdir: __get_free_page failed!");
3220 filp->f_pos = DIREND;
3225 jfs_dirent = (struct jfs_dirent *) dirent_buf;
3227 overflow = fix_page = 0;
3229 stbl = DT_GETSTBL(p);
3231 for (i = index; i < p->header.nextindex; i++) {
3232 d = (struct ldtentry *) & p->slot[stbl[i]];
3234 if (((long) jfs_dirent + d->namlen + 1) >
3235 (dirent_buf + PAGE_SIZE)) {
3236 /* DBCS codepages could overrun dirent_buf */
3242 d_namleft = d->namlen;
3243 name_ptr = jfs_dirent->name;
3244 jfs_dirent->ino = le32_to_cpu(d->inumber);
3247 len = min(d_namleft, DTLHDRDATALEN);
3248 jfs_dirent->position = le32_to_cpu(d->index);
3250 * d->index should always be valid, but it
3251 * isn't. fsck.jfs doesn't create the
3252 * directory index for the lost+found
3253 * directory. Rather than let it go,
3254 * we can try to fix it.
3256 if ((jfs_dirent->position < 2) ||
3257 (jfs_dirent->position >=
3258 JFS_IP(ip)->next_index)) {
3259 if (!page_fixed && !isReadOnly(ip)) {
3262 * setting overflow and setting
3263 * index to i will cause the
3264 * same page to be processed
3265 * again starting here
3271 jfs_dirent->position = unique_pos++;
3274 jfs_dirent->position = dtpos;
3275 len = min(d_namleft, DTLHDRDATALEN_LEGACY);
3278 /* copy the name of head/only segment */
3279 outlen = jfs_strfromUCS_le(name_ptr, d->name, len,
3281 jfs_dirent->name_len = outlen;
3283 /* copy name in the additional segment(s) */
3286 t = (struct dtslot *) & p->slot[next];
3290 if (d_namleft == 0) {
3292 "JFS:Dtree error: ino = "
3293 "%ld, bn=%Ld, index = %d",
3299 len = min(d_namleft, DTSLOTDATALEN);
3300 outlen = jfs_strfromUCS_le(name_ptr, t->name,
3302 jfs_dirent->name_len += outlen;
3308 jfs_dirent = next_jfs_dirent(jfs_dirent);
3315 /* Point to next leaf page */
3316 if (p->header.flag & BT_ROOT)
3319 bn = le64_to_cpu(p->header.next);
3321 /* update offset (pn:index) for new page */
3324 dtoffset->index = 0;
3330 /* unpin previous leaf page */
3333 jfs_dirent = (struct jfs_dirent *) dirent_buf;
3334 while (jfs_dirents--) {
3335 filp->f_pos = jfs_dirent->position;
3336 if (filldir(dirent, jfs_dirent->name,
3337 jfs_dirent->name_len, filp->f_pos,
3338 jfs_dirent->ino, DT_UNKNOWN))
3340 jfs_dirent = next_jfs_dirent(jfs_dirent);
3344 add_missing_indices(ip, bn);
3348 if (!overflow && (bn == 0)) {
3349 filp->f_pos = DIREND;
3353 DT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
3355 free_page(dirent_buf);
3361 free_page(dirent_buf);
3370 * function: get the leftmost page of the directory
3372 static int dtReadFirst(struct inode *ip, struct btstack * btstack)
3376 int psize = 288; /* initial in-line directory */
3377 struct metapage *mp;
3380 struct btframe *btsp;
3383 BT_CLR(btstack); /* reset stack */
3386 * descend leftmost path of the tree
3388 * by convention, root bn = 0.
3391 DT_GETPAGE(ip, bn, mp, psize, p, rc);
3396 * leftmost leaf page
3398 if (p->header.flag & BT_LEAF) {
3399 /* return leftmost entry */
3400 btsp = btstack->top;
3409 * descend down to leftmost child page
3411 if (BT_STACK_FULL(btstack)) {
3413 jfs_error(ip->i_sb, "dtReadFirst: btstack overrun");
3414 BT_STACK_DUMP(btstack);
3417 /* push (bn, index) of the parent page/entry */
3418 BT_PUSH(btstack, bn, 0);
3420 /* get the leftmost entry */
3421 stbl = DT_GETSTBL(p);
3422 xd = (pxd_t *) & p->slot[stbl[0]];
3424 /* get the child page block address */
3425 bn = addressPXD(xd);
3426 psize = lengthPXD(xd) << JFS_SBI(ip->i_sb)->l2bsize;
3428 /* unpin the parent page */
3437 * function: get the page of the specified offset (pn:index)
3439 * return: if (offset > eof), bn = -1;
3441 * note: if index > nextindex of the target leaf page,
3442 * start with 1st entry of next leaf page;
3444 static int dtReadNext(struct inode *ip, loff_t * offset,
3445 struct btstack * btstack)
3452 } *dtoffset = (struct dtoffset *) offset;
3454 struct metapage *mp;
3459 struct btframe *btsp, *parent;
3463 * get leftmost leaf page pinned
3465 if ((rc = dtReadFirst(ip, btstack)))
3469 DT_GETSEARCH(ip, btstack->top, bn, mp, p, index);
3471 /* get the start offset (pn:index) */
3472 pn = dtoffset->pn - 1; /* Now pn = 0 represents leftmost leaf */
3473 index = dtoffset->index;
3475 /* start at leftmost page ? */
3477 /* offset beyond eof ? */
3478 if (index < p->header.nextindex)
3481 if (p->header.flag & BT_ROOT) {
3486 /* start with 1st entry of next leaf page */
3488 dtoffset->index = index = 0;
3492 /* start at non-leftmost page: scan parent pages for large pn */
3493 if (p->header.flag & BT_ROOT) {
3498 /* start after next leaf page ? */
3502 /* get leaf page pn = 1 */
3504 bn = le64_to_cpu(p->header.next);
3506 /* unpin leaf page */
3509 /* offset beyond eof ? */
3518 * scan last internal page level to get target leaf page
3521 /* unpin leftmost leaf page */
3524 /* get left most parent page */
3525 btsp = btstack->top;
3528 DT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
3532 /* scan parent pages at last internal page level */
3533 while (pn >= p->header.nextindex) {
3534 pn -= p->header.nextindex;
3536 /* get next parent page address */
3537 bn = le64_to_cpu(p->header.next);
3539 /* unpin current parent page */
3542 /* offset beyond eof ? */
3548 /* get next parent page */
3549 DT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
3553 /* update parent page stack frame */
3557 /* get leaf page address */
3558 stbl = DT_GETSTBL(p);
3559 xd = (pxd_t *) & p->slot[stbl[pn]];
3560 bn = addressPXD(xd);
3562 /* unpin parent page */
3566 * get target leaf page
3569 DT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
3574 * leaf page has been completed:
3575 * start with 1st entry of next leaf page
3577 if (index >= p->header.nextindex) {
3578 bn = le64_to_cpu(p->header.next);
3580 /* unpin leaf page */
3583 /* offset beyond eof ? */
3589 /* get next leaf page */
3590 DT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
3594 /* start with 1st entry of next leaf page */
3596 dtoffset->index = 0;
3600 /* return target leaf page pinned */
3601 btsp = btstack->top;
3603 btsp->index = dtoffset->index;
3613 * function: compare search key with an internal entry
3616 * < 0 if k is < record
3617 * = 0 if k is = record
3618 * > 0 if k is > record
3620 static int dtCompare(struct component_name * key, /* search key */
3621 dtpage_t * p, /* directory page */
3623 { /* entry slot index */
3626 int klen, namlen, len, rc;
3627 struct idtentry *ih;
3631 * force the left-most key on internal pages, at any level of
3632 * the tree, to be less than any search key.
3633 * this obviates having to update the leftmost key on an internal
3634 * page when the user inserts a new key in the tree smaller than
3635 * anything that has been stored.
3637 * (? if/when dtSearch() narrows down to 1st entry (index = 0),
3638 * at any internal page at any level of the tree,
3639 * it descends to child of the entry anyway -
3640 * ? make the entry as min size dummy entry)
3642 * if (e->index == 0 && h->prevpg == P_INVALID && !(h->flags & BT_LEAF))
3649 ih = (struct idtentry *) & p->slot[si];
3652 namlen = ih->namlen;
3653 len = min(namlen, DTIHDRDATALEN);
3655 /* compare with head/only segment */
3656 len = min(klen, len);
3657 if ((rc = UniStrncmp_le(kname, name, len)))
3663 /* compare with additional segment(s) */
3665 while (klen > 0 && namlen > 0) {
3666 /* compare with next name segment */
3667 t = (struct dtslot *) & p->slot[si];
3668 len = min(namlen, DTSLOTDATALEN);
3669 len = min(klen, len);
3671 if ((rc = UniStrncmp_le(kname, name, len)))
3680 return (klen - namlen);
3689 * function: compare search key with an (leaf/internal) entry
3692 * < 0 if k is < record
3693 * = 0 if k is = record
3694 * > 0 if k is > record
3696 static int ciCompare(struct component_name * key, /* search key */
3697 dtpage_t * p, /* directory page */
3698 int si, /* entry slot index */
3703 int klen, namlen, len, rc;
3704 struct ldtentry *lh;
3705 struct idtentry *ih;
3710 * force the left-most key on internal pages, at any level of
3711 * the tree, to be less than any search key.
3712 * this obviates having to update the leftmost key on an internal
3713 * page when the user inserts a new key in the tree smaller than
3714 * anything that has been stored.
3716 * (? if/when dtSearch() narrows down to 1st entry (index = 0),
3717 * at any internal page at any level of the tree,
3718 * it descends to child of the entry anyway -
3719 * ? make the entry as min size dummy entry)
3721 * if (e->index == 0 && h->prevpg == P_INVALID && !(h->flags & BT_LEAF))
3731 if (p->header.flag & BT_LEAF) {
3732 lh = (struct ldtentry *) & p->slot[si];
3735 namlen = lh->namlen;
3736 if (flag & JFS_DIR_INDEX)
3737 len = min(namlen, DTLHDRDATALEN);
3739 len = min(namlen, DTLHDRDATALEN_LEGACY);
3742 * internal page entry
3745 ih = (struct idtentry *) & p->slot[si];
3748 namlen = ih->namlen;
3749 len = min(namlen, DTIHDRDATALEN);
3752 /* compare with head/only segment */
3753 len = min(klen, len);
3754 for (i = 0; i < len; i++, kname++, name++) {
3755 /* only uppercase if case-insensitive support is on */
3756 if ((flag & JFS_OS2) == JFS_OS2)
3757 x = UniToupper(le16_to_cpu(*name));
3759 x = le16_to_cpu(*name);
3760 if ((rc = *kname - x))
3767 /* compare with additional segment(s) */
3768 while (klen > 0 && namlen > 0) {
3769 /* compare with next name segment */
3770 t = (struct dtslot *) & p->slot[si];
3771 len = min(namlen, DTSLOTDATALEN);
3772 len = min(klen, len);
3774 for (i = 0; i < len; i++, kname++, name++) {
3775 /* only uppercase if case-insensitive support is on */
3776 if ((flag & JFS_OS2) == JFS_OS2)
3777 x = UniToupper(le16_to_cpu(*name));
3779 x = le16_to_cpu(*name);
3781 if ((rc = *kname - x))
3790 return (klen - namlen);
3795 * ciGetLeafPrefixKey()
3797 * function: compute prefix of suffix compression
3798 * from two adjacent leaf entries
3799 * across page boundary
3801 * return: non-zero on error
3804 static int ciGetLeafPrefixKey(dtpage_t * lp, int li, dtpage_t * rp,
3805 int ri, struct component_name * key, int flag)
3808 wchar_t *pl, *pr, *kname;
3809 struct component_name lkey;
3810 struct component_name rkey;
3812 lkey.name = (wchar_t *) kmalloc((JFS_NAME_MAX + 1) * sizeof(wchar_t),
3814 if (lkey.name == NULL)
3817 rkey.name = (wchar_t *) kmalloc((JFS_NAME_MAX + 1) * sizeof(wchar_t),
3819 if (rkey.name == NULL) {
3824 /* get left and right key */
3825 dtGetKey(lp, li, &lkey, flag);
3826 lkey.name[lkey.namlen] = 0;
3828 if ((flag & JFS_OS2) == JFS_OS2)
3831 dtGetKey(rp, ri, &rkey, flag);
3832 rkey.name[rkey.namlen] = 0;
3835 if ((flag & JFS_OS2) == JFS_OS2)
3838 /* compute prefix */
3841 namlen = min(lkey.namlen, rkey.namlen);
3842 for (pl = lkey.name, pr = rkey.name;
3843 namlen; pl++, pr++, namlen--, klen++, kname++) {
3846 key->namlen = klen + 1;
3851 /* l->namlen <= r->namlen since l <= r */
3852 if (lkey.namlen < rkey.namlen) {
3854 key->namlen = klen + 1;
3855 } else /* l->namelen == r->namelen */
3869 * function: get key of the entry
3871 static void dtGetKey(dtpage_t * p, int i, /* entry index */
3872 struct component_name * key, int flag)
3876 struct ldtentry *lh;
3877 struct idtentry *ih;
3884 stbl = DT_GETSTBL(p);
3886 if (p->header.flag & BT_LEAF) {
3887 lh = (struct ldtentry *) & p->slot[si];
3889 namlen = lh->namlen;
3891 if (flag & JFS_DIR_INDEX)
3892 len = min(namlen, DTLHDRDATALEN);
3894 len = min(namlen, DTLHDRDATALEN_LEGACY);
3896 ih = (struct idtentry *) & p->slot[si];
3898 namlen = ih->namlen;
3900 len = min(namlen, DTIHDRDATALEN);
3903 key->namlen = namlen;
3907 * move head/only segment
3909 UniStrncpy_from_le(kname, name, len);
3912 * move additional segment(s)
3915 /* get next segment */
3919 len = min(namlen, DTSLOTDATALEN);
3920 UniStrncpy_from_le(kname, t->name, len);
3930 * function: allocate free slot(s) and
3931 * write a leaf/internal entry
3933 * return: entry slot index
3935 static void dtInsertEntry(dtpage_t * p, int index, struct component_name * key,
3936 ddata_t * data, struct dt_lock ** dtlock)
3938 struct dtslot *h, *t;
3939 struct ldtentry *lh = NULL;
3940 struct idtentry *ih = NULL;
3941 int hsi, fsi, klen, len, nextindex;
3946 struct dt_lock *dtlck = *dtlock;
3950 struct metapage *mp = NULL;
3955 /* allocate a free slot */
3956 hsi = fsi = p->header.freelist;
3958 p->header.freelist = h->next;
3959 --p->header.freecnt;
3961 /* open new linelock */
3962 if (dtlck->index >= dtlck->maxcnt)
3963 dtlck = (struct dt_lock *) txLinelock(dtlck);
3965 lv = & dtlck->lv[dtlck->index];
3968 /* write head/only segment */
3969 if (p->header.flag & BT_LEAF) {
3970 lh = (struct ldtentry *) h;
3972 lh->inumber = cpu_to_le32(data->leaf.ino);
3975 if (data->leaf.ip) {
3976 len = min(klen, DTLHDRDATALEN);
3977 if (!(p->header.flag & BT_ROOT))
3978 bn = addressPXD(&p->header.self);
3979 lh->index = cpu_to_le32(add_index(data->leaf.tid,
3983 len = min(klen, DTLHDRDATALEN_LEGACY);
3985 ih = (struct idtentry *) h;
3991 len = min(klen, DTIHDRDATALEN);
3994 UniStrncpy_to_le(name, kname, len);
3999 /* write additional segment(s) */
4004 fsi = p->header.freelist;
4006 p->header.freelist = t->next;
4007 --p->header.freecnt;
4009 /* is next slot contiguous ? */
4010 if (fsi != xsi + 1) {
4011 /* close current linelock */
4015 /* open new linelock */
4016 if (dtlck->index < dtlck->maxcnt)
4019 dtlck = (struct dt_lock *) txLinelock(dtlck);
4020 lv = & dtlck->lv[0];
4028 len = min(klen, DTSLOTDATALEN);
4029 UniStrncpy_to_le(t->name, kname, len);
4036 /* close current linelock */
4042 /* terminate last/only segment */
4044 /* single segment entry */
4045 if (p->header.flag & BT_LEAF)
4050 /* multi-segment entry */
4053 /* if insert into middle, shift right succeeding entries in stbl */
4054 stbl = DT_GETSTBL(p);
4055 nextindex = p->header.nextindex;
4056 if (index < nextindex) {
4057 memmove(stbl + index + 1, stbl + index, nextindex - index);
4059 if ((p->header.flag & BT_LEAF) && data->leaf.ip) {
4063 * Need to update slot number for entries that moved
4067 for (n = index + 1; n <= nextindex; n++) {
4068 lh = (struct ldtentry *) & (p->slot[stbl[n]]);
4069 modify_index(data->leaf.tid, data->leaf.ip,
4070 le32_to_cpu(lh->index), bn, n,
4074 release_metapage(mp);
4080 /* advance next available entry index of stbl */
4081 ++p->header.nextindex;
4088 * function: move entries from split/left page to new/right page
4090 * nextindex of dst page and freelist/freecnt of both pages
4093 static void dtMoveEntry(dtpage_t * sp, int si, dtpage_t * dp,
4094 struct dt_lock ** sdtlock, struct dt_lock ** ddtlock,
4097 int ssi, next; /* src slot index */
4098 int di; /* dst entry index */
4099 int dsi; /* dst slot index */
4100 s8 *sstbl, *dstbl; /* sorted entry table */
4102 struct ldtentry *slh, *dlh = NULL;
4103 struct idtentry *sih, *dih = NULL;
4104 struct dtslot *h, *s, *d;
4105 struct dt_lock *sdtlck = *sdtlock, *ddtlck = *ddtlock;
4106 struct lv *slv, *dlv;
4110 sstbl = (s8 *) & sp->slot[sp->header.stblindex];
4111 dstbl = (s8 *) & dp->slot[dp->header.stblindex];
4113 dsi = dp->header.freelist; /* first (whole page) free slot */
4114 sfsi = sp->header.freelist;
4116 /* linelock destination entry slot */
4117 dlv = & ddtlck->lv[ddtlck->index];
4120 /* linelock source entry slot */
4121 slv = & sdtlck->lv[sdtlck->index];
4122 slv->offset = sstbl[si];
4123 xssi = slv->offset - 1;
4129 for (di = 0; si < sp->header.nextindex; si++, di++) {
4133 /* is next slot contiguous ? */
4134 if (ssi != xssi + 1) {
4135 /* close current linelock */
4139 /* open new linelock */
4140 if (sdtlck->index < sdtlck->maxcnt)
4143 sdtlck = (struct dt_lock *) txLinelock(sdtlck);
4144 slv = & sdtlck->lv[0];
4152 * move head/only segment of an entry
4155 h = d = &dp->slot[dsi];
4157 /* get src slot and move */
4159 if (sp->header.flag & BT_LEAF) {
4160 /* get source entry */
4161 slh = (struct ldtentry *) s;
4162 dlh = (struct ldtentry *) h;
4163 snamlen = slh->namlen;
4166 len = min(snamlen, DTLHDRDATALEN);
4167 dlh->index = slh->index; /* little-endian */
4169 len = min(snamlen, DTLHDRDATALEN_LEGACY);
4171 memcpy(dlh, slh, 6 + len * 2);
4175 /* update dst head/only segment next field */
4179 sih = (struct idtentry *) s;
4180 snamlen = sih->namlen;
4182 len = min(snamlen, DTIHDRDATALEN);
4183 dih = (struct idtentry *) h;
4184 memcpy(dih, sih, 10 + len * 2);
4191 /* free src head/only segment */
4201 * move additional segment(s) of the entry
4204 while ((ssi = next) >= 0) {
4205 /* is next slot contiguous ? */
4206 if (ssi != xssi + 1) {
4207 /* close current linelock */
4211 /* open new linelock */
4212 if (sdtlck->index < sdtlck->maxcnt)
4218 slv = & sdtlck->lv[0];
4225 /* get next source segment */
4228 /* get next destination free slot */
4231 len = min(snamlen, DTSLOTDATALEN);
4232 UniStrncpy_le(d->name, s->name, len);
4241 /* free source segment */
4250 /* terminate dst last/only segment */
4252 /* single segment entry */
4253 if (dp->header.flag & BT_LEAF)
4258 /* multi-segment entry */
4262 /* close current linelock */
4271 /* update source header */
4272 sp->header.freelist = sfsi;
4273 sp->header.freecnt += nd;
4275 /* update destination header */
4276 dp->header.nextindex = di;
4278 dp->header.freelist = dsi;
4279 dp->header.freecnt -= nd;
4286 * function: free a (leaf/internal) entry
4288 * log freelist header, stbl, and each segment slot of entry
4289 * (even though last/only segment next field is modified,
4290 * physical image logging requires all segment slots of
4291 * the entry logged to avoid applying previous updates
4292 * to the same slots)
4294 static void dtDeleteEntry(dtpage_t * p, int fi, struct dt_lock ** dtlock)
4296 int fsi; /* free entry slot index */
4300 struct dt_lock *dtlck = *dtlock;
4304 /* get free entry slot index */
4305 stbl = DT_GETSTBL(p);
4308 /* open new linelock */
4309 if (dtlck->index >= dtlck->maxcnt)
4310 dtlck = (struct dt_lock *) txLinelock(dtlck);
4311 lv = & dtlck->lv[dtlck->index];
4315 /* get the head/only segment */
4317 if (p->header.flag & BT_LEAF)
4318 si = ((struct ldtentry *) t)->next;
4320 si = ((struct idtentry *) t)->next;
4327 /* find the last/only segment */
4329 /* is next slot contiguous ? */
4330 if (si != xsi + 1) {
4331 /* close current linelock */
4335 /* open new linelock */
4336 if (dtlck->index < dtlck->maxcnt)
4339 dtlck = (struct dt_lock *) txLinelock(dtlck);
4340 lv = & dtlck->lv[0];
4356 /* close current linelock */
4362 /* update freelist */
4363 t->next = p->header.freelist;
4364 p->header.freelist = fsi;
4365 p->header.freecnt += freecnt;
4367 /* if delete from middle,
4368 * shift left the succedding entries in the stbl
4370 si = p->header.nextindex;
4372 memmove(&stbl[fi], &stbl[fi + 1], si - fi - 1);
4374 p->header.nextindex--;
4381 * function: truncate a (leaf/internal) entry
4383 * log freelist header, stbl, and each segment slot of entry
4384 * (even though last/only segment next field is modified,
4385 * physical image logging requires all segment slots of
4386 * the entry logged to avoid applying previous updates
4387 * to the same slots)
4389 static void dtTruncateEntry(dtpage_t * p, int ti, struct dt_lock ** dtlock)
4391 int tsi; /* truncate entry slot index */
4395 struct dt_lock *dtlck = *dtlock;
4399 /* get free entry slot index */
4400 stbl = DT_GETSTBL(p);
4403 /* open new linelock */
4404 if (dtlck->index >= dtlck->maxcnt)
4405 dtlck = (struct dt_lock *) txLinelock(dtlck);
4406 lv = & dtlck->lv[dtlck->index];
4410 /* get the head/only segment */
4412 ASSERT(p->header.flag & BT_INTERNAL);
4413 ((struct idtentry *) t)->namlen = 0;
4414 si = ((struct idtentry *) t)->next;
4415 ((struct idtentry *) t)->next = -1;
4422 /* find the last/only segment */
4424 /* is next slot contiguous ? */
4425 if (si != xsi + 1) {
4426 /* close current linelock */
4430 /* open new linelock */
4431 if (dtlck->index < dtlck->maxcnt)
4434 dtlck = (struct dt_lock *) txLinelock(dtlck);
4435 lv = & dtlck->lv[0];
4451 /* close current linelock */
4457 /* update freelist */
4460 t->next = p->header.freelist;
4461 p->header.freelist = fsi;
4462 p->header.freecnt += freecnt;
4467 * dtLinelockFreelist()
4469 static void dtLinelockFreelist(dtpage_t * p, /* directory page */
4470 int m, /* max slot index */
4471 struct dt_lock ** dtlock)
4473 int fsi; /* free entry slot index */
4476 struct dt_lock *dtlck = *dtlock;
4480 /* get free entry slot index */
4481 fsi = p->header.freelist;
4483 /* open new linelock */
4484 if (dtlck->index >= dtlck->maxcnt)
4485 dtlck = (struct dt_lock *) txLinelock(dtlck);
4486 lv = & dtlck->lv[dtlck->index];
4496 /* find the last/only segment */
4497 while (si < m && si >= 0) {
4498 /* is next slot contiguous ? */
4499 if (si != xsi + 1) {
4500 /* close current linelock */
4504 /* open new linelock */
4505 if (dtlck->index < dtlck->maxcnt)
4508 dtlck = (struct dt_lock *) txLinelock(dtlck);
4509 lv = & dtlck->lv[0];
4523 /* close current linelock */
4534 * FUNCTION: Modify the inode number part of a directory entry
4537 * tid - Transaction id
4538 * ip - Inode of parent directory
4539 * key - Name of entry to be modified
4540 * orig_ino - Original inode number expected in entry
4541 * new_ino - New inode number to put into entry
4545 * -ESTALE - If entry found does not match orig_ino passed in
4546 * -ENOENT - If no entry can be found to match key
4547 * 0 - If successfully modified entry
4549 int dtModify(tid_t tid, struct inode *ip,
4550 struct component_name * key, ino_t * orig_ino, ino_t new_ino, int flag)
4554 struct metapage *mp;
4557 struct btstack btstack;
4559 struct dt_lock *dtlck;
4562 int entry_si; /* entry slot index */
4563 struct ldtentry *entry;
4566 * search for the entry to modify:
4568 * dtSearch() returns (leaf page pinned, index at which to modify).
4570 if ((rc = dtSearch(ip, key, orig_ino, &btstack, flag)))
4573 /* retrieve search result */
4574 DT_GETSEARCH(ip, btstack.top, bn, mp, p, index);
4576 BT_MARK_DIRTY(mp, ip);
4578 * acquire a transaction lock on the leaf page of named entry
4580 tlck = txLock(tid, ip, mp, tlckDTREE | tlckENTRY);
4581 dtlck = (struct dt_lock *) & tlck->lock;
4583 /* get slot index of the entry */
4584 stbl = DT_GETSTBL(p);
4585 entry_si = stbl[index];
4587 /* linelock entry */
4588 ASSERT(dtlck->index == 0);
4589 lv = & dtlck->lv[0];
4590 lv->offset = entry_si;
4594 /* get the head/only segment */
4595 entry = (struct ldtentry *) & p->slot[entry_si];
4597 /* substitute the inode number of the entry */
4598 entry->inumber = cpu_to_le32(new_ino);
4600 /* unpin the leaf page */