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 jfs_sb_info *sbi = JFS_SBI(ip->i_sb);
940 struct metapage *smp;
941 dtpage_t *sp; /* split page */
942 struct metapage *rmp;
943 dtpage_t *rp; /* new right page split from sp */
944 pxd_t rpxd; /* new right page extent descriptor */
945 struct metapage *lmp;
946 dtpage_t *lp; /* left child page */
947 int skip; /* index of entry of insertion */
948 struct btframe *parent; /* parent page entry on traverse stack */
951 struct pxdlist pxdlist;
953 struct component_name key = { 0, NULL };
954 ddata_t *data = split->data;
956 struct dt_lock *dtlck;
959 int quota_allocation = 0;
960 int dlimit_allocation = 0;
964 sp = DT_PAGE(ip, smp);
967 (wchar_t *) kmalloc((JFS_NAME_MAX + 2) * sizeof(wchar_t),
978 * The split routines insert the new entry, and
979 * acquire txLock as appropriate.
982 * split root leaf page:
984 if (sp->header.flag & BT_ROOT) {
986 * allocate a single extent child page
989 n = sbi->bsize >> L2DTSLOTSIZE;
990 n -= (n + 31) >> L2DTSLOTSIZE; /* stbl size */
991 n -= DTROOTMAXSLOT - sp->header.freecnt; /* header + entries */
992 if (n <= split->nslot)
994 if ((rc = dbAlloc(ip, 0, (s64) xlen, &xaddr))) {
1001 pxd = &pxdlist.pxd[0];
1002 PXDaddress(pxd, xaddr);
1003 PXDlength(pxd, xlen);
1004 split->pxdlist = &pxdlist;
1005 rc = dtSplitRoot(tid, ip, split, &rmp);
1008 dbFree(ip, xaddr, xlen);
1015 ip->i_size = xlen << sbi->l2bsize;
1021 * extend first leaf page
1023 * extend the 1st extent if less than buffer page size
1024 * (dtExtendPage() reurns leaf page unpinned)
1026 pxd = &sp->header.self;
1027 xlen = lengthPXD(pxd);
1028 xsize = xlen << sbi->l2bsize;
1029 if (xsize < PSIZE) {
1030 xaddr = addressPXD(pxd);
1031 n = xsize >> L2DTSLOTSIZE;
1032 n -= (n + 31) >> L2DTSLOTSIZE; /* stbl size */
1033 if ((n + sp->header.freecnt) <= split->nslot)
1034 n = xlen + (xlen << 1);
1038 /* Allocate blocks to quota. */
1039 if (DQUOT_ALLOC_BLOCK(ip, n)) {
1043 quota_allocation += n;
1045 if (DLIMIT_ALLOC_BLOCK(ip, n)) {
1049 dlimit_allocation += n;
1051 if ((rc = dbReAlloc(sbi->ipbmap, xaddr, (s64) xlen,
1055 pxdlist.maxnpxd = 1;
1057 pxd = &pxdlist.pxd[0];
1058 PXDaddress(pxd, nxaddr)
1059 PXDlength(pxd, xlen + n);
1060 split->pxdlist = &pxdlist;
1061 if ((rc = dtExtendPage(tid, ip, split, btstack))) {
1062 nxaddr = addressPXD(pxd);
1063 if (xaddr != nxaddr) {
1064 /* free relocated extent */
1065 xlen = lengthPXD(pxd);
1066 dbFree(ip, nxaddr, (s64) xlen);
1068 /* free extended delta */
1069 xlen = lengthPXD(pxd) - n;
1070 xaddr = addressPXD(pxd) + xlen;
1071 dbFree(ip, xaddr, (s64) n);
1073 } else if (!DO_INDEX(ip))
1074 ip->i_size = lengthPXD(pxd) << sbi->l2bsize;
1083 * split leaf page <sp> into <sp> and a new right page <rp>.
1085 * return <rp> pinned and its extent descriptor <rpxd>
1088 * allocate new directory page extent and
1089 * new index page(s) to cover page split(s)
1091 * allocation hint: ?
1093 n = btstack->nsplit;
1094 pxdlist.maxnpxd = pxdlist.npxd = 0;
1095 xlen = sbi->nbperpage;
1096 for (pxd = pxdlist.pxd; n > 0; n--, pxd++) {
1097 if ((rc = dbAlloc(ip, 0, (s64) xlen, &xaddr)) == 0) {
1098 PXDaddress(pxd, xaddr);
1099 PXDlength(pxd, xlen);
1106 /* undo allocation */
1110 split->pxdlist = &pxdlist;
1111 if ((rc = dtSplitPage(tid, ip, split, &rmp, &rp, &rpxd))) {
1114 /* undo allocation */
1119 ip->i_size += PSIZE;
1122 * propagate up the router entry for the leaf page just split
1124 * insert a router entry for the new page into the parent page,
1125 * propagate the insert/split up the tree by walking back the stack
1126 * of (bn of parent page, index of child page entry in parent page)
1127 * that were traversed during the search for the page that split.
1129 * the propagation of insert/split up the tree stops if the root
1130 * splits or the page inserted into doesn't have to split to hold
1133 * the parent entry for the split page remains the same, and
1134 * a new entry is inserted at its right with the first key and
1135 * block number of the new right page.
1137 * There are a maximum of 4 pages pinned at any time:
1138 * two children, left parent and right parent (when the parent splits).
1139 * keep the child pages pinned while working on the parent.
1140 * make sure that all pins are released at exit.
1142 while ((parent = BT_POP(btstack)) != NULL) {
1143 /* parent page specified by stack frame <parent> */
1145 /* keep current child pages (<lp>, <rp>) pinned */
1150 * insert router entry in parent for new right child page <rp>
1152 /* get the parent page <sp> */
1153 DT_GETPAGE(ip, parent->bn, smp, PSIZE, sp, rc);
1161 * The new key entry goes ONE AFTER the index of parent entry,
1162 * because the split was to the right.
1164 skip = parent->index + 1;
1167 * compute the key for the router entry
1169 * key suffix compression:
1170 * for internal pages that have leaf pages as children,
1171 * retain only what's needed to distinguish between
1172 * the new entry and the entry on the page to its left.
1173 * If the keys compare equal, retain the entire key.
1175 * note that compression is performed only at computing
1176 * router key at the lowest internal level.
1177 * further compression of the key between pairs of higher
1178 * level internal pages loses too much information and
1179 * the search may fail.
1180 * (e.g., two adjacent leaf pages of {a, ..., x} {xx, ...,}
1181 * results in two adjacent parent entries (a)(xx).
1182 * if split occurs between these two entries, and
1183 * if compression is applied, the router key of parent entry
1184 * of right page (x) will divert search for x into right
1185 * subtree and miss x in the left subtree.)
1187 * the entire key must be retained for the next-to-leftmost
1188 * internal key at any level of the tree, or search may fail
1191 switch (rp->header.flag & BT_TYPE) {
1194 * compute the length of prefix for suffix compression
1195 * between last entry of left page and first entry
1198 if ((sp->header.flag & BT_ROOT && skip > 1) ||
1199 sp->header.prev != 0 || skip > 1) {
1200 /* compute uppercase router prefix key */
1201 rc = ciGetLeafPrefixKey(lp,
1202 lp->header.nextindex-1,
1212 /* next to leftmost entry of
1213 lowest internal level */
1215 /* compute uppercase router key */
1216 dtGetKey(rp, 0, &key, sbi->mntflag);
1217 key.name[key.namlen] = 0;
1219 if ((sbi->mntflag & JFS_OS2) == JFS_OS2)
1223 n = NDTINTERNAL(key.namlen);
1227 dtGetKey(rp, 0, &key, sbi->mntflag);
1228 n = NDTINTERNAL(key.namlen);
1232 jfs_err("dtSplitUp(): UFO!");
1236 /* unpin left child page */
1240 * compute the data for the router entry
1242 data->xd = rpxd; /* child page xd */
1245 * parent page is full - split the parent page
1247 if (n > sp->header.freecnt) {
1248 /* init for parent page split */
1250 split->index = skip; /* index at insert */
1253 /* split->data = data; */
1255 /* unpin right child page */
1258 /* The split routines insert the new entry,
1259 * acquire txLock as appropriate.
1260 * return <rp> pinned and its block number <rbn>.
1262 rc = (sp->header.flag & BT_ROOT) ?
1263 dtSplitRoot(tid, ip, split, &rmp) :
1264 dtSplitPage(tid, ip, split, &rmp, &rp, &rpxd);
1270 /* smp and rmp are pinned */
1273 * parent page is not full - insert router entry in parent page
1276 BT_MARK_DIRTY(smp, ip);
1278 * acquire a transaction lock on the parent page
1280 tlck = txLock(tid, ip, smp, tlckDTREE | tlckENTRY);
1281 dtlck = (struct dt_lock *) & tlck->lock;
1282 ASSERT(dtlck->index == 0);
1283 lv = & dtlck->lv[0];
1285 /* linelock header */
1290 /* linelock stbl of non-root parent page */
1291 if (!(sp->header.flag & BT_ROOT)) {
1293 n = skip >> L2DTSLOTSIZE;
1294 lv->offset = sp->header.stblindex + n;
1296 ((sp->header.nextindex -
1297 1) >> L2DTSLOTSIZE) - n + 1;
1301 dtInsertEntry(sp, skip, &key, data, &dtlck);
1303 /* exit propagate up */
1308 /* unpin current split and its right page */
1313 * free remaining extents allocated for split
1317 pxd = &pxdlist.pxd[n];
1318 for (; n < pxdlist.maxnpxd; n++, pxd++)
1319 dbFree(ip, addressPXD(pxd), (s64) lengthPXD(pxd));
1324 /* Rollback dlimit allocation */
1325 if (rc && dlimit_allocation)
1326 DLIMIT_FREE_BLOCK(ip, dlimit_allocation);
1327 /* Rollback quota allocation */
1328 if (rc && quota_allocation)
1329 DQUOT_FREE_BLOCK(ip, quota_allocation);
1340 * function: Split a non-root page of a btree.
1344 * return: 0 - success;
1346 * return split and new page pinned;
1348 static int dtSplitPage(tid_t tid, struct inode *ip, struct dtsplit * split,
1349 struct metapage ** rmpp, dtpage_t ** rpp, pxd_t * rpxdp)
1352 struct metapage *smp;
1354 struct metapage *rmp;
1355 dtpage_t *rp; /* new right page allocated */
1356 s64 rbn; /* new right page block number */
1357 struct metapage *mp;
1360 struct pxdlist *pxdlist;
1362 int skip, nextindex, half, left, nxt, off, si;
1363 struct ldtentry *ldtentry;
1364 struct idtentry *idtentry;
1369 struct dt_lock *sdtlck, *rdtlck;
1371 struct dt_lock *dtlck;
1372 struct lv *slv, *rlv, *lv;
1374 /* get split page */
1376 sp = DT_PAGE(ip, smp);
1379 * allocate the new right page for the split
1381 pxdlist = split->pxdlist;
1382 pxd = &pxdlist->pxd[pxdlist->npxd];
1384 rbn = addressPXD(pxd);
1385 rmp = get_metapage(ip, rbn, PSIZE, 1);
1389 /* Allocate blocks to quota. */
1390 if (DQUOT_ALLOC_BLOCK(ip, lengthPXD(pxd))) {
1391 release_metapage(rmp);
1394 /* Allocate blocks to dlimit. */
1395 if (DLIMIT_ALLOC_BLOCK(ip, lengthPXD(pxd))) {
1396 DQUOT_FREE_BLOCK(ip, lengthPXD(pxd));
1397 release_metapage(rmp);
1401 jfs_info("dtSplitPage: ip:0x%p smp:0x%p rmp:0x%p", ip, smp, rmp);
1403 BT_MARK_DIRTY(rmp, ip);
1405 * acquire a transaction lock on the new right page
1407 tlck = txLock(tid, ip, rmp, tlckDTREE | tlckNEW);
1408 rdtlck = (struct dt_lock *) & tlck->lock;
1410 rp = (dtpage_t *) rmp->data;
1412 rp->header.self = *pxd;
1414 BT_MARK_DIRTY(smp, ip);
1416 * acquire a transaction lock on the split page
1420 tlck = txLock(tid, ip, smp, tlckDTREE | tlckENTRY);
1421 sdtlck = (struct dt_lock *) & tlck->lock;
1423 /* linelock header of split page */
1424 ASSERT(sdtlck->index == 0);
1425 slv = & sdtlck->lv[0];
1431 * initialize/update sibling pointers between sp and rp
1433 nextbn = le64_to_cpu(sp->header.next);
1434 rp->header.next = cpu_to_le64(nextbn);
1435 rp->header.prev = cpu_to_le64(addressPXD(&sp->header.self));
1436 sp->header.next = cpu_to_le64(rbn);
1439 * initialize new right page
1441 rp->header.flag = sp->header.flag;
1443 /* compute sorted entry table at start of extent data area */
1444 rp->header.nextindex = 0;
1445 rp->header.stblindex = 1;
1447 n = PSIZE >> L2DTSLOTSIZE;
1448 rp->header.maxslot = n;
1449 stblsize = (n + 31) >> L2DTSLOTSIZE; /* in unit of slot */
1452 fsi = rp->header.stblindex + stblsize;
1453 rp->header.freelist = fsi;
1454 rp->header.freecnt = rp->header.maxslot - fsi;
1457 * sequential append at tail: append without split
1459 * If splitting the last page on a level because of appending
1460 * a entry to it (skip is maxentry), it's likely that the access is
1461 * sequential. Adding an empty page on the side of the level is less
1462 * work and can push the fill factor much higher than normal.
1463 * If we're wrong it's no big deal, we'll just do the split the right
1465 * (It may look like it's equally easy to do a similar hack for
1466 * reverse sorted data, that is, split the tree left,
1467 * but it's not. Be my guest.)
1469 if (nextbn == 0 && split->index == sp->header.nextindex) {
1470 /* linelock header + stbl (first slot) of new page */
1471 rlv = & rdtlck->lv[rdtlck->index];
1477 * initialize freelist of new right page
1480 for (fsi++; fsi < rp->header.maxslot; f++, fsi++)
1484 /* insert entry at the first entry of the new right page */
1485 dtInsertEntry(rp, 0, split->key, split->data, &rdtlck);
1491 * non-sequential insert (at possibly middle page)
1495 * update prev pointer of previous right sibling page;
1498 DT_GETPAGE(ip, nextbn, mp, PSIZE, p, rc);
1500 discard_metapage(rmp);
1504 BT_MARK_DIRTY(mp, ip);
1506 * acquire a transaction lock on the next page
1508 tlck = txLock(tid, ip, mp, tlckDTREE | tlckRELINK);
1509 jfs_info("dtSplitPage: tlck = 0x%p, ip = 0x%p, mp=0x%p",
1511 dtlck = (struct dt_lock *) & tlck->lock;
1513 /* linelock header of previous right sibling page */
1514 lv = & dtlck->lv[dtlck->index];
1519 p->header.prev = cpu_to_le64(rbn);
1525 * split the data between the split and right pages.
1527 skip = split->index;
1528 half = (PSIZE >> L2DTSLOTSIZE) >> 1; /* swag */
1532 * compute fill factor for split pages
1534 * <nxt> traces the next entry to move to rp
1535 * <off> traces the next entry to stay in sp
1537 stbl = (u8 *) & sp->slot[sp->header.stblindex];
1538 nextindex = sp->header.nextindex;
1539 for (nxt = off = 0; nxt < nextindex; ++off) {
1541 /* check for fill factor with new entry size */
1545 switch (sp->header.flag & BT_TYPE) {
1547 ldtentry = (struct ldtentry *) & sp->slot[si];
1549 n = NDTLEAF(ldtentry->namlen);
1551 n = NDTLEAF_LEGACY(ldtentry->
1556 idtentry = (struct idtentry *) & sp->slot[si];
1557 n = NDTINTERNAL(idtentry->namlen);
1564 ++nxt; /* advance to next entry to move in sp */
1572 /* <nxt> poins to the 1st entry to move */
1575 * move entries to right page
1577 * dtMoveEntry() initializes rp and reserves entry for insertion
1579 * split page moved out entries are linelocked;
1580 * new/right page moved in entries are linelocked;
1582 /* linelock header + stbl of new right page */
1583 rlv = & rdtlck->lv[rdtlck->index];
1588 dtMoveEntry(sp, nxt, rp, &sdtlck, &rdtlck, DO_INDEX(ip));
1590 sp->header.nextindex = nxt;
1593 * finalize freelist of new right page
1595 fsi = rp->header.freelist;
1597 for (fsi++; fsi < rp->header.maxslot; f++, fsi++)
1602 * Update directory index table for entries now in right page
1604 if ((rp->header.flag & BT_LEAF) && DO_INDEX(ip)) {
1608 stbl = DT_GETSTBL(rp);
1609 for (n = 0; n < rp->header.nextindex; n++) {
1610 ldtentry = (struct ldtentry *) & rp->slot[stbl[n]];
1611 modify_index(tid, ip, le32_to_cpu(ldtentry->index),
1612 rbn, n, &mp, &lblock);
1615 release_metapage(mp);
1619 * the skipped index was on the left page,
1622 /* insert the new entry in the split page */
1623 dtInsertEntry(sp, skip, split->key, split->data, &sdtlck);
1625 /* linelock stbl of split page */
1626 if (sdtlck->index >= sdtlck->maxcnt)
1627 sdtlck = (struct dt_lock *) txLinelock(sdtlck);
1628 slv = & sdtlck->lv[sdtlck->index];
1629 n = skip >> L2DTSLOTSIZE;
1630 slv->offset = sp->header.stblindex + n;
1632 ((sp->header.nextindex - 1) >> L2DTSLOTSIZE) - n + 1;
1636 * the skipped index was on the right page,
1639 /* adjust the skip index to reflect the new position */
1642 /* insert the new entry in the right page */
1643 dtInsertEntry(rp, skip, split->key, split->data, &rdtlck);
1657 * function: extend 1st/only directory leaf page
1661 * return: 0 - success;
1663 * return extended page pinned;
1665 static int dtExtendPage(tid_t tid,
1666 struct inode *ip, struct dtsplit * split, struct btstack * btstack)
1668 struct super_block *sb = ip->i_sb;
1670 struct metapage *smp, *pmp, *mp;
1672 struct pxdlist *pxdlist;
1675 int newstblindex, newstblsize;
1676 int oldstblindex, oldstblsize;
1679 struct btframe *parent;
1681 struct dt_lock *dtlck;
1684 struct pxd_lock *pxdlock;
1687 struct ldtentry *ldtentry;
1690 /* get page to extend */
1692 sp = DT_PAGE(ip, smp);
1694 /* get parent/root page */
1695 parent = BT_POP(btstack);
1696 DT_GETPAGE(ip, parent->bn, pmp, PSIZE, pp, rc);
1703 pxdlist = split->pxdlist;
1704 pxd = &pxdlist->pxd[pxdlist->npxd];
1707 xaddr = addressPXD(pxd);
1708 tpxd = &sp->header.self;
1709 txaddr = addressPXD(tpxd);
1710 /* in-place extension */
1711 if (xaddr == txaddr) {
1718 /* save moved extent descriptor for later free */
1719 tlck = txMaplock(tid, ip, tlckDTREE | tlckRELOCATE);
1720 pxdlock = (struct pxd_lock *) & tlck->lock;
1721 pxdlock->flag = mlckFREEPXD;
1722 pxdlock->pxd = sp->header.self;
1726 * Update directory index table to reflect new page address
1732 stbl = DT_GETSTBL(sp);
1733 for (n = 0; n < sp->header.nextindex; n++) {
1735 (struct ldtentry *) & sp->slot[stbl[n]];
1736 modify_index(tid, ip,
1737 le32_to_cpu(ldtentry->index),
1738 xaddr, n, &mp, &lblock);
1741 release_metapage(mp);
1748 sp->header.self = *pxd;
1750 jfs_info("dtExtendPage: ip:0x%p smp:0x%p sp:0x%p", ip, smp, sp);
1752 BT_MARK_DIRTY(smp, ip);
1754 * acquire a transaction lock on the extended/leaf page
1756 tlck = txLock(tid, ip, smp, tlckDTREE | type);
1757 dtlck = (struct dt_lock *) & tlck->lock;
1758 lv = & dtlck->lv[0];
1760 /* update buffer extent descriptor of extended page */
1761 xlen = lengthPXD(pxd);
1762 xsize = xlen << JFS_SBI(sb)->l2bsize;
1763 #ifdef _STILL_TO_PORT
1764 bmSetXD(smp, xaddr, xsize);
1765 #endif /* _STILL_TO_PORT */
1768 * copy old stbl to new stbl at start of extended area
1770 oldstblindex = sp->header.stblindex;
1771 oldstblsize = (sp->header.maxslot + 31) >> L2DTSLOTSIZE;
1772 newstblindex = sp->header.maxslot;
1773 n = xsize >> L2DTSLOTSIZE;
1774 newstblsize = (n + 31) >> L2DTSLOTSIZE;
1775 memcpy(&sp->slot[newstblindex], &sp->slot[oldstblindex],
1776 sp->header.nextindex);
1779 * in-line extension: linelock old area of extended page
1781 if (type == tlckEXTEND) {
1782 /* linelock header */
1788 /* linelock new stbl of extended page */
1789 lv->offset = newstblindex;
1790 lv->length = newstblsize;
1793 * relocation: linelock whole relocated area
1797 lv->length = sp->header.maxslot + newstblsize;
1802 sp->header.maxslot = n;
1803 sp->header.stblindex = newstblindex;
1804 /* sp->header.nextindex remains the same */
1807 * add old stbl region at head of freelist
1811 last = sp->header.freelist;
1812 for (n = 0; n < oldstblsize; n++, fsi++, f++) {
1816 sp->header.freelist = last;
1817 sp->header.freecnt += oldstblsize;
1820 * append free region of newly extended area at tail of freelist
1822 /* init free region of newly extended area */
1823 fsi = n = newstblindex + newstblsize;
1825 for (fsi++; fsi < sp->header.maxslot; f++, fsi++)
1829 /* append new free region at tail of old freelist */
1830 fsi = sp->header.freelist;
1832 sp->header.freelist = n;
1837 } while (fsi != -1);
1842 sp->header.freecnt += sp->header.maxslot - n;
1845 * insert the new entry
1847 dtInsertEntry(sp, split->index, split->key, split->data, &dtlck);
1849 BT_MARK_DIRTY(pmp, ip);
1851 * linelock any freeslots residing in old extent
1853 if (type == tlckEXTEND) {
1854 n = sp->header.maxslot >> 2;
1855 if (sp->header.freelist < n)
1856 dtLinelockFreelist(sp, n, &dtlck);
1860 * update parent entry on the parent/root page
1863 * acquire a transaction lock on the parent/root page
1865 tlck = txLock(tid, ip, pmp, tlckDTREE | tlckENTRY);
1866 dtlck = (struct dt_lock *) & tlck->lock;
1867 lv = & dtlck->lv[dtlck->index];
1869 /* linelock parent entry - 1st slot */
1874 /* update the parent pxd for page extension */
1875 tpxd = (pxd_t *) & pp->slot[1];
1887 * split the full root page into
1888 * original/root/split page and new right page
1889 * i.e., root remains fixed in tree anchor (inode) and
1890 * the root is copied to a single new right child page
1891 * since root page << non-root page, and
1892 * the split root page contains a single entry for the
1893 * new right child page.
1897 * return: 0 - success;
1899 * return new page pinned;
1901 static int dtSplitRoot(tid_t tid,
1902 struct inode *ip, struct dtsplit * split, struct metapage ** rmpp)
1904 struct super_block *sb = ip->i_sb;
1905 struct metapage *smp;
1907 struct metapage *rmp;
1914 int fsi, stblsize, n;
1917 struct pxdlist *pxdlist;
1919 struct dt_lock *dtlck;
1923 /* get split root page */
1925 sp = &JFS_IP(ip)->i_dtroot;
1928 * allocate/initialize a single (right) child page
1930 * N.B. at first split, a one (or two) block to fit new entry
1931 * is allocated; at subsequent split, a full page is allocated;
1933 pxdlist = split->pxdlist;
1934 pxd = &pxdlist->pxd[pxdlist->npxd];
1936 rbn = addressPXD(pxd);
1937 xlen = lengthPXD(pxd);
1938 xsize = xlen << JFS_SBI(sb)->l2bsize;
1939 rmp = get_metapage(ip, rbn, xsize, 1);
1945 /* Allocate blocks to quota. */
1946 if (DQUOT_ALLOC_BLOCK(ip, lengthPXD(pxd))) {
1947 release_metapage(rmp);
1950 /* Allocate blocks to dlimit. */
1951 if (DLIMIT_ALLOC_BLOCK(ip, lengthPXD(pxd))) {
1952 DQUOT_FREE_BLOCK(ip, lengthPXD(pxd));
1953 release_metapage(rmp);
1957 BT_MARK_DIRTY(rmp, ip);
1959 * acquire a transaction lock on the new right page
1961 tlck = txLock(tid, ip, rmp, tlckDTREE | tlckNEW);
1962 dtlck = (struct dt_lock *) & tlck->lock;
1965 (sp->header.flag & BT_LEAF) ? BT_LEAF : BT_INTERNAL;
1966 rp->header.self = *pxd;
1968 /* initialize sibling pointers */
1969 rp->header.next = 0;
1970 rp->header.prev = 0;
1973 * move in-line root page into new right page extent
1975 /* linelock header + copied entries + new stbl (1st slot) in new page */
1976 ASSERT(dtlck->index == 0);
1977 lv = & dtlck->lv[0];
1979 lv->length = 10; /* 1 + 8 + 1 */
1982 n = xsize >> L2DTSLOTSIZE;
1983 rp->header.maxslot = n;
1984 stblsize = (n + 31) >> L2DTSLOTSIZE;
1986 /* copy old stbl to new stbl at start of extended area */
1987 rp->header.stblindex = DTROOTMAXSLOT;
1988 stbl = (s8 *) & rp->slot[DTROOTMAXSLOT];
1989 memcpy(stbl, sp->header.stbl, sp->header.nextindex);
1990 rp->header.nextindex = sp->header.nextindex;
1992 /* copy old data area to start of new data area */
1993 memcpy(&rp->slot[1], &sp->slot[1], IDATASIZE);
1996 * append free region of newly extended area at tail of freelist
1998 /* init free region of newly extended area */
1999 fsi = n = DTROOTMAXSLOT + stblsize;
2001 for (fsi++; fsi < rp->header.maxslot; f++, fsi++)
2005 /* append new free region at tail of old freelist */
2006 fsi = sp->header.freelist;
2008 rp->header.freelist = n;
2010 rp->header.freelist = fsi;
2015 } while (fsi != -1);
2020 rp->header.freecnt = sp->header.freecnt + rp->header.maxslot - n;
2023 * Update directory index table for entries now in right page
2025 if ((rp->header.flag & BT_LEAF) && DO_INDEX(ip)) {
2027 struct metapage *mp = NULL;
2028 struct ldtentry *ldtentry;
2030 stbl = DT_GETSTBL(rp);
2031 for (n = 0; n < rp->header.nextindex; n++) {
2032 ldtentry = (struct ldtentry *) & rp->slot[stbl[n]];
2033 modify_index(tid, ip, le32_to_cpu(ldtentry->index),
2034 rbn, n, &mp, &lblock);
2037 release_metapage(mp);
2040 * insert the new entry into the new right/child page
2041 * (skip index in the new right page will not change)
2043 dtInsertEntry(rp, split->index, split->key, split->data, &dtlck);
2046 * reset parent/root page
2048 * set the 1st entry offset to 0, which force the left-most key
2049 * at any level of the tree to be less than any search key.
2051 * The btree comparison code guarantees that the left-most key on any
2052 * level of the tree is never used, so it doesn't need to be filled in.
2054 BT_MARK_DIRTY(smp, ip);
2056 * acquire a transaction lock on the root page (in-memory inode)
2058 tlck = txLock(tid, ip, smp, tlckDTREE | tlckNEW | tlckBTROOT);
2059 dtlck = (struct dt_lock *) & tlck->lock;
2062 ASSERT(dtlck->index == 0);
2063 lv = & dtlck->lv[0];
2065 lv->length = DTROOTMAXSLOT;
2068 /* update page header of root */
2069 if (sp->header.flag & BT_LEAF) {
2070 sp->header.flag &= ~BT_LEAF;
2071 sp->header.flag |= BT_INTERNAL;
2074 /* init the first entry */
2075 s = (struct idtentry *) & sp->slot[DTENTRYSTART];
2081 stbl = sp->header.stbl;
2082 stbl[0] = DTENTRYSTART;
2083 sp->header.nextindex = 1;
2086 fsi = DTENTRYSTART + 1;
2089 /* init free region of remaining area */
2090 for (fsi++; fsi < DTROOTMAXSLOT; f++, fsi++)
2094 sp->header.freelist = DTENTRYSTART + 1;
2095 sp->header.freecnt = DTROOTMAXSLOT - (DTENTRYSTART + 1);
2106 * function: delete the entry(s) referenced by a key.
2112 int dtDelete(tid_t tid,
2113 struct inode *ip, struct component_name * key, ino_t * ino, int flag)
2117 struct metapage *mp, *imp;
2120 struct btstack btstack;
2121 struct dt_lock *dtlck;
2125 struct ldtentry *ldtentry;
2127 u32 table_index, next_index;
2128 struct metapage *nmp;
2132 * search for the entry to delete:
2134 * dtSearch() returns (leaf page pinned, index at which to delete).
2136 if ((rc = dtSearch(ip, key, ino, &btstack, flag)))
2139 /* retrieve search result */
2140 DT_GETSEARCH(ip, btstack.top, bn, mp, p, index);
2143 * We need to find put the index of the next entry into the
2144 * directory index table in order to resume a readdir from this
2148 stbl = DT_GETSTBL(p);
2149 ldtentry = (struct ldtentry *) & p->slot[stbl[index]];
2150 table_index = le32_to_cpu(ldtentry->index);
2151 if (index == (p->header.nextindex - 1)) {
2153 * Last entry in this leaf page
2155 if ((p->header.flag & BT_ROOT)
2156 || (p->header.next == 0))
2159 /* Read next leaf page */
2160 DT_GETPAGE(ip, le64_to_cpu(p->header.next),
2161 nmp, PSIZE, np, rc);
2165 stbl = DT_GETSTBL(np);
2167 (struct ldtentry *) & np->
2170 le32_to_cpu(ldtentry->index);
2176 (struct ldtentry *) & p->slot[stbl[index + 1]];
2177 next_index = le32_to_cpu(ldtentry->index);
2179 free_index(tid, ip, table_index, next_index);
2182 * the leaf page becomes empty, delete the page
2184 if (p->header.nextindex == 1) {
2185 /* delete empty page */
2186 rc = dtDeleteUp(tid, ip, mp, p, &btstack);
2189 * the leaf page has other entries remaining:
2191 * delete the entry from the leaf page.
2194 BT_MARK_DIRTY(mp, ip);
2196 * acquire a transaction lock on the leaf page
2198 tlck = txLock(tid, ip, mp, tlckDTREE | tlckENTRY);
2199 dtlck = (struct dt_lock *) & tlck->lock;
2202 * Do not assume that dtlck->index will be zero. During a
2203 * rename within a directory, this transaction may have
2204 * modified this page already when adding the new entry.
2207 /* linelock header */
2208 if (dtlck->index >= dtlck->maxcnt)
2209 dtlck = (struct dt_lock *) txLinelock(dtlck);
2210 lv = & dtlck->lv[dtlck->index];
2215 /* linelock stbl of non-root leaf page */
2216 if (!(p->header.flag & BT_ROOT)) {
2217 if (dtlck->index >= dtlck->maxcnt)
2218 dtlck = (struct dt_lock *) txLinelock(dtlck);
2219 lv = & dtlck->lv[dtlck->index];
2220 i = index >> L2DTSLOTSIZE;
2221 lv->offset = p->header.stblindex + i;
2223 ((p->header.nextindex - 1) >> L2DTSLOTSIZE) -
2228 /* free the leaf entry */
2229 dtDeleteEntry(p, index, &dtlck);
2232 * Update directory index table for entries moved in stbl
2234 if (DO_INDEX(ip) && index < p->header.nextindex) {
2238 stbl = DT_GETSTBL(p);
2239 for (i = index; i < p->header.nextindex; i++) {
2241 (struct ldtentry *) & p->slot[stbl[i]];
2242 modify_index(tid, ip,
2243 le32_to_cpu(ldtentry->index),
2244 bn, i, &imp, &lblock);
2247 release_metapage(imp);
2261 * free empty pages as propagating deletion up the tree
2267 static int dtDeleteUp(tid_t tid, struct inode *ip,
2268 struct metapage * fmp, dtpage_t * fp, struct btstack * btstack)
2271 struct metapage *mp;
2273 int index, nextindex;
2275 struct btframe *parent;
2276 struct dt_lock *dtlck;
2279 struct pxd_lock *pxdlock;
2283 * keep the root leaf page which has become empty
2285 if (BT_IS_ROOT(fmp)) {
2289 * dtInitRoot() acquires txlock on the root
2291 dtInitRoot(tid, ip, PARENT(ip));
2299 * free the non-root leaf page
2302 * acquire a transaction lock on the page
2304 * write FREEXTENT|NOREDOPAGE log record
2305 * N.B. linelock is overlaid as freed extent descriptor, and
2306 * the buffer page is freed;
2308 tlck = txMaplock(tid, ip, tlckDTREE | tlckFREE);
2309 pxdlock = (struct pxd_lock *) & tlck->lock;
2310 pxdlock->flag = mlckFREEPXD;
2311 pxdlock->pxd = fp->header.self;
2314 /* update sibling pointers */
2315 if ((rc = dtRelink(tid, ip, fp))) {
2320 xlen = lengthPXD(&fp->header.self);
2322 /* Free dlimit allocation. */
2323 DLIMIT_FREE_BLOCK(ip, xlen);
2324 /* Free quota allocation. */
2325 DQUOT_FREE_BLOCK(ip, xlen);
2327 /* free/invalidate its buffer page */
2328 discard_metapage(fmp);
2331 * propagate page deletion up the directory tree
2333 * If the delete from the parent page makes it empty,
2334 * continue all the way up the tree.
2335 * stop if the root page is reached (which is never deleted) or
2336 * if the entry deletion does not empty the page.
2338 while ((parent = BT_POP(btstack)) != NULL) {
2339 /* pin the parent page <sp> */
2340 DT_GETPAGE(ip, parent->bn, mp, PSIZE, p, rc);
2345 * free the extent of the child page deleted
2347 index = parent->index;
2350 * delete the entry for the child page from parent
2352 nextindex = p->header.nextindex;
2355 * the parent has the single entry being deleted:
2357 * free the parent page which has become empty.
2359 if (nextindex == 1) {
2361 * keep the root internal page which has become empty
2363 if (p->header.flag & BT_ROOT) {
2367 * dtInitRoot() acquires txlock on the root
2369 dtInitRoot(tid, ip, PARENT(ip));
2376 * free the parent page
2380 * acquire a transaction lock on the page
2382 * write FREEXTENT|NOREDOPAGE log record
2386 tlckDTREE | tlckFREE);
2387 pxdlock = (struct pxd_lock *) & tlck->lock;
2388 pxdlock->flag = mlckFREEPXD;
2389 pxdlock->pxd = p->header.self;
2392 /* update sibling pointers */
2393 if ((rc = dtRelink(tid, ip, p))) {
2398 xlen = lengthPXD(&p->header.self);
2400 /* Free dlimit allocation */
2401 DLIMIT_FREE_BLOCK(ip, xlen);
2402 /* Free quota allocation */
2403 DQUOT_FREE_BLOCK(ip, xlen);
2405 /* free/invalidate its buffer page */
2406 discard_metapage(mp);
2414 * the parent has other entries remaining:
2416 * delete the router entry from the parent page.
2418 BT_MARK_DIRTY(mp, ip);
2420 * acquire a transaction lock on the page
2422 * action: router entry deletion
2424 tlck = txLock(tid, ip, mp, tlckDTREE | tlckENTRY);
2425 dtlck = (struct dt_lock *) & tlck->lock;
2427 /* linelock header */
2428 if (dtlck->index >= dtlck->maxcnt)
2429 dtlck = (struct dt_lock *) txLinelock(dtlck);
2430 lv = & dtlck->lv[dtlck->index];
2435 /* linelock stbl of non-root leaf page */
2436 if (!(p->header.flag & BT_ROOT)) {
2437 if (dtlck->index < dtlck->maxcnt)
2440 dtlck = (struct dt_lock *) txLinelock(dtlck);
2441 lv = & dtlck->lv[0];
2443 i = index >> L2DTSLOTSIZE;
2444 lv->offset = p->header.stblindex + i;
2446 ((p->header.nextindex - 1) >> L2DTSLOTSIZE) -
2451 /* free the router entry */
2452 dtDeleteEntry(p, index, &dtlck);
2454 /* reset key of new leftmost entry of level (for consistency) */
2456 ((p->header.flag & BT_ROOT) || p->header.prev == 0))
2457 dtTruncateEntry(p, 0, &dtlck);
2459 /* unpin the parent page */
2462 /* exit propagation up */
2467 ip->i_size -= PSIZE;
2474 * NAME: dtRelocate()
2476 * FUNCTION: relocate dtpage (internal or leaf) of directory;
2477 * This function is mainly used by defragfs utility.
2479 int dtRelocate(tid_t tid, struct inode *ip, s64 lmxaddr, pxd_t * opxd,
2483 struct metapage *mp, *pmp, *lmp, *rmp;
2484 dtpage_t *p, *pp, *rp = 0, *lp= 0;
2487 struct btstack btstack;
2489 s64 oxaddr, nextbn, prevbn;
2492 struct dt_lock *dtlck;
2493 struct pxd_lock *pxdlock;
2497 oxaddr = addressPXD(opxd);
2498 xlen = lengthPXD(opxd);
2500 jfs_info("dtRelocate: lmxaddr:%Ld xaddr:%Ld:%Ld xlen:%d",
2501 (long long)lmxaddr, (long long)oxaddr, (long long)nxaddr,
2505 * 1. get the internal parent dtpage covering
2506 * router entry for the tartget page to be relocated;
2508 rc = dtSearchNode(ip, lmxaddr, opxd, &btstack);
2512 /* retrieve search result */
2513 DT_GETSEARCH(ip, btstack.top, bn, pmp, pp, index);
2514 jfs_info("dtRelocate: parent router entry validated.");
2517 * 2. relocate the target dtpage
2519 /* read in the target page from src extent */
2520 DT_GETPAGE(ip, oxaddr, mp, PSIZE, p, rc);
2522 /* release the pinned parent page */
2528 * read in sibling pages if any to update sibling pointers;
2531 if (p->header.next) {
2532 nextbn = le64_to_cpu(p->header.next);
2533 DT_GETPAGE(ip, nextbn, rmp, PSIZE, rp, rc);
2542 if (p->header.prev) {
2543 prevbn = le64_to_cpu(p->header.prev);
2544 DT_GETPAGE(ip, prevbn, lmp, PSIZE, lp, rc);
2554 /* at this point, all xtpages to be updated are in memory */
2557 * update sibling pointers of sibling dtpages if any;
2560 tlck = txLock(tid, ip, lmp, tlckDTREE | tlckRELINK);
2561 dtlck = (struct dt_lock *) & tlck->lock;
2562 /* linelock header */
2563 ASSERT(dtlck->index == 0);
2564 lv = & dtlck->lv[0];
2569 lp->header.next = cpu_to_le64(nxaddr);
2574 tlck = txLock(tid, ip, rmp, tlckDTREE | tlckRELINK);
2575 dtlck = (struct dt_lock *) & tlck->lock;
2576 /* linelock header */
2577 ASSERT(dtlck->index == 0);
2578 lv = & dtlck->lv[0];
2583 rp->header.prev = cpu_to_le64(nxaddr);
2588 * update the target dtpage to be relocated
2590 * write LOG_REDOPAGE of LOG_NEW type for dst page
2591 * for the whole target page (logredo() will apply
2592 * after image and update bmap for allocation of the
2593 * dst extent), and update bmap for allocation of
2596 tlck = txLock(tid, ip, mp, tlckDTREE | tlckNEW);
2597 dtlck = (struct dt_lock *) & tlck->lock;
2598 /* linelock header */
2599 ASSERT(dtlck->index == 0);
2600 lv = & dtlck->lv[0];
2602 /* update the self address in the dtpage header */
2603 pxd = &p->header.self;
2604 PXDaddress(pxd, nxaddr);
2606 /* the dst page is the same as the src page, i.e.,
2607 * linelock for afterimage of the whole page;
2610 lv->length = p->header.maxslot;
2613 /* update the buffer extent descriptor of the dtpage */
2614 xsize = xlen << JFS_SBI(ip->i_sb)->l2bsize;
2615 #ifdef _STILL_TO_PORT
2616 bmSetXD(mp, nxaddr, xsize);
2617 #endif /* _STILL_TO_PORT */
2618 /* unpin the relocated page */
2620 jfs_info("dtRelocate: target dtpage relocated.");
2622 /* the moved extent is dtpage, then a LOG_NOREDOPAGE log rec
2623 * needs to be written (in logredo(), the LOG_NOREDOPAGE log rec
2624 * will also force a bmap update ).
2628 * 3. acquire maplock for the source extent to be freed;
2630 /* for dtpage relocation, write a LOG_NOREDOPAGE record
2631 * for the source dtpage (logredo() will init NoRedoPage
2632 * filter and will also update bmap for free of the source
2633 * dtpage), and upadte bmap for free of the source dtpage;
2635 tlck = txMaplock(tid, ip, tlckDTREE | tlckFREE);
2636 pxdlock = (struct pxd_lock *) & tlck->lock;
2637 pxdlock->flag = mlckFREEPXD;
2638 PXDaddress(&pxdlock->pxd, oxaddr);
2639 PXDlength(&pxdlock->pxd, xlen);
2643 * 4. update the parent router entry for relocation;
2645 * acquire tlck for the parent entry covering the target dtpage;
2646 * write LOG_REDOPAGE to apply after image only;
2648 jfs_info("dtRelocate: update parent router entry.");
2649 tlck = txLock(tid, ip, pmp, tlckDTREE | tlckENTRY);
2650 dtlck = (struct dt_lock *) & tlck->lock;
2651 lv = & dtlck->lv[dtlck->index];
2653 /* update the PXD with the new address */
2654 stbl = DT_GETSTBL(pp);
2655 pxd = (pxd_t *) & pp->slot[stbl[index]];
2656 PXDaddress(pxd, nxaddr);
2657 lv->offset = stbl[index];
2661 /* unpin the parent dtpage */
2668 * NAME: dtSearchNode()
2670 * FUNCTION: Search for an dtpage containing a specified address
2671 * This function is mainly used by defragfs utility.
2673 * NOTE: Search result on stack, the found page is pinned at exit.
2674 * The result page must be an internal dtpage.
2675 * lmxaddr give the address of the left most page of the
2676 * dtree level, in which the required dtpage resides.
2678 static int dtSearchNode(struct inode *ip, s64 lmxaddr, pxd_t * kpxd,
2679 struct btstack * btstack)
2683 struct metapage *mp;
2685 int psize = 288; /* initial in-line directory */
2689 struct btframe *btsp;
2691 BT_CLR(btstack); /* reset stack */
2694 * descend tree to the level with specified leftmost page
2696 * by convention, root bn = 0.
2699 /* get/pin the page to search */
2700 DT_GETPAGE(ip, bn, mp, psize, p, rc);
2704 /* does the xaddr of leftmost page of the levevl
2705 * matches levevl search key ?
2707 if (p->header.flag & BT_ROOT) {
2710 } else if (addressPXD(&p->header.self) == lmxaddr)
2714 * descend down to leftmost child page
2716 if (p->header.flag & BT_LEAF) {
2721 /* get the leftmost entry */
2722 stbl = DT_GETSTBL(p);
2723 pxd = (pxd_t *) & p->slot[stbl[0]];
2725 /* get the child page block address */
2726 bn = addressPXD(pxd);
2727 psize = lengthPXD(pxd) << JFS_SBI(ip->i_sb)->l2bsize;
2728 /* unpin the parent page */
2733 * search each page at the current levevl
2736 stbl = DT_GETSTBL(p);
2737 for (i = 0; i < p->header.nextindex; i++) {
2738 pxd = (pxd_t *) & p->slot[stbl[i]];
2740 /* found the specified router entry */
2741 if (addressPXD(pxd) == addressPXD(kpxd) &&
2742 lengthPXD(pxd) == lengthPXD(kpxd)) {
2743 btsp = btstack->top;
2752 /* get the right sibling page if any */
2754 bn = le64_to_cpu(p->header.next);
2760 /* unpin current page */
2763 /* get the right sibling page */
2764 DT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
2770 #endif /* _NOTYET */
2776 * link around a freed page.
2779 * fp: page to be freed
2783 static int dtRelink(tid_t tid, struct inode *ip, dtpage_t * p)
2786 struct metapage *mp;
2789 struct dt_lock *dtlck;
2792 nextbn = le64_to_cpu(p->header.next);
2793 prevbn = le64_to_cpu(p->header.prev);
2795 /* update prev pointer of the next page */
2797 DT_GETPAGE(ip, nextbn, mp, PSIZE, p, rc);
2801 BT_MARK_DIRTY(mp, ip);
2803 * acquire a transaction lock on the next page
2805 * action: update prev pointer;
2807 tlck = txLock(tid, ip, mp, tlckDTREE | tlckRELINK);
2808 jfs_info("dtRelink nextbn: tlck = 0x%p, ip = 0x%p, mp=0x%p",
2810 dtlck = (struct dt_lock *) & tlck->lock;
2812 /* linelock header */
2813 if (dtlck->index >= dtlck->maxcnt)
2814 dtlck = (struct dt_lock *) txLinelock(dtlck);
2815 lv = & dtlck->lv[dtlck->index];
2820 p->header.prev = cpu_to_le64(prevbn);
2824 /* update next pointer of the previous page */
2826 DT_GETPAGE(ip, prevbn, mp, PSIZE, p, rc);
2830 BT_MARK_DIRTY(mp, ip);
2832 * acquire a transaction lock on the prev page
2834 * action: update next pointer;
2836 tlck = txLock(tid, ip, mp, tlckDTREE | tlckRELINK);
2837 jfs_info("dtRelink prevbn: tlck = 0x%p, ip = 0x%p, mp=0x%p",
2839 dtlck = (struct dt_lock *) & tlck->lock;
2841 /* linelock header */
2842 if (dtlck->index >= dtlck->maxcnt)
2843 dtlck = (struct dt_lock *) txLinelock(dtlck);
2844 lv = & dtlck->lv[dtlck->index];
2849 p->header.next = cpu_to_le64(nextbn);
2860 * initialize directory root (inline in inode)
2862 void dtInitRoot(tid_t tid, struct inode *ip, u32 idotdot)
2864 struct jfs_inode_info *jfs_ip = JFS_IP(ip);
2869 struct dt_lock *dtlck;
2874 * If this was previously an non-empty directory, we need to remove
2875 * the old directory table.
2878 if (!jfs_dirtable_inline(ip)) {
2879 struct tblock *tblk = tid_to_tblock(tid);
2881 * We're playing games with the tid's xflag. If
2882 * we're removing a regular file, the file's xtree
2883 * is committed with COMMIT_PMAP, but we always
2884 * commit the directories xtree with COMMIT_PWMAP.
2886 xflag_save = tblk->xflag;
2889 * xtTruncate isn't guaranteed to fully truncate
2890 * the xtree. The caller needs to check i_size
2891 * after committing the transaction to see if
2892 * additional truncation is needed. The
2893 * COMMIT_Stale flag tells caller that we
2894 * initiated the truncation.
2896 xtTruncate(tid, ip, 0, COMMIT_PWMAP);
2897 set_cflag(COMMIT_Stale, ip);
2899 tblk->xflag = xflag_save;
2903 jfs_ip->next_index = 2;
2905 ip->i_size = IDATASIZE;
2908 * acquire a transaction lock on the root
2910 * action: directory initialization;
2912 tlck = txLock(tid, ip, (struct metapage *) & jfs_ip->bxflag,
2913 tlckDTREE | tlckENTRY | tlckBTROOT);
2914 dtlck = (struct dt_lock *) & tlck->lock;
2917 ASSERT(dtlck->index == 0);
2918 lv = & dtlck->lv[0];
2920 lv->length = DTROOTMAXSLOT;
2923 p = &jfs_ip->i_dtroot;
2925 p->header.flag = DXD_INDEX | BT_ROOT | BT_LEAF;
2927 p->header.nextindex = 0;
2933 /* init data area of root */
2934 for (fsi++; fsi < DTROOTMAXSLOT; f++, fsi++)
2938 p->header.freelist = 1;
2939 p->header.freecnt = 8;
2941 /* init '..' entry */
2942 p->header.idotdot = cpu_to_le32(idotdot);
2948 * add_missing_indices()
2950 * function: Fix dtree page in which one or more entries has an invalid index.
2951 * fsck.jfs should really fix this, but it currently does not.
2952 * Called from jfs_readdir when bad index is detected.
2954 static void add_missing_indices(struct inode *inode, s64 bn)
2957 struct dt_lock *dtlck;
2961 struct metapage *mp;
2968 tid = txBegin(inode->i_sb, 0);
2970 DT_GETPAGE(inode, bn, mp, PSIZE, p, rc);
2973 printk(KERN_ERR "DT_GETPAGE failed!\n");
2976 BT_MARK_DIRTY(mp, inode);
2978 ASSERT(p->header.flag & BT_LEAF);
2980 tlck = txLock(tid, inode, mp, tlckDTREE | tlckENTRY);
2982 tlck->type |= tlckBTROOT;
2984 dtlck = (struct dt_lock *) &tlck->lock;
2986 stbl = DT_GETSTBL(p);
2987 for (i = 0; i < p->header.nextindex; i++) {
2988 d = (struct ldtentry *) &p->slot[stbl[i]];
2989 index = le32_to_cpu(d->index);
2990 if ((index < 2) || (index >= JFS_IP(inode)->next_index)) {
2991 d->index = cpu_to_le32(add_index(tid, inode, bn, i));
2992 if (dtlck->index >= dtlck->maxcnt)
2993 dtlck = (struct dt_lock *) txLinelock(dtlck);
2994 lv = &dtlck->lv[dtlck->index];
2995 lv->offset = stbl[i];
3002 (void) txCommit(tid, 1, &inode, 0);
3008 * Buffer to hold directory entry info while traversing a dtree page
3009 * before being fed to the filldir function
3019 * function to determine next variable-sized jfs_dirent in buffer
3021 static inline struct jfs_dirent *next_jfs_dirent(struct jfs_dirent *dirent)
3023 return (struct jfs_dirent *)
3025 ((sizeof (struct jfs_dirent) + dirent->name_len + 1 +
3026 sizeof (loff_t) - 1) &
3027 ~(sizeof (loff_t) - 1)));
3033 * function: read directory entries sequentially
3034 * from the specified entry offset
3038 * return: offset = (pn, index) of start entry
3039 * of next jfs_readdir()/dtRead()
3041 int jfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
3043 struct inode *ip = filp->f_dentry->d_inode;
3044 struct nls_table *codepage = JFS_SBI(ip->i_sb)->nls_tab;
3046 loff_t dtpos; /* legacy OS/2 style position */
3051 } *dtoffset = (struct dtoffset *) &dtpos;
3053 struct metapage *mp;
3057 struct btstack btstack;
3061 int d_namleft, len, outlen;
3062 unsigned long dirent_buf;
3066 uint loop_count = 0;
3067 struct jfs_dirent *jfs_dirent;
3069 int overflow, fix_page, page_fixed = 0;
3070 static int unique_pos = 2; /* If we can't fix broken index */
3072 if (filp->f_pos == DIREND)
3077 * persistent index is stored in directory entries.
3078 * Special cases: 0 = .
3080 * -1 = End of directory
3084 dir_index = (u32) filp->f_pos;
3086 if (dir_index > 1) {
3087 struct dir_table_slot dirtab_slot;
3090 (dir_index >= JFS_IP(ip)->next_index)) {
3091 /* Stale position. Directory has shrunk */
3092 filp->f_pos = DIREND;
3096 rc = read_index(ip, dir_index, &dirtab_slot);
3098 filp->f_pos = DIREND;
3101 if (dirtab_slot.flag == DIR_INDEX_FREE) {
3102 if (loop_count++ > JFS_IP(ip)->next_index) {
3103 jfs_err("jfs_readdir detected "
3105 filp->f_pos = DIREND;
3108 dir_index = le32_to_cpu(dirtab_slot.addr2);
3109 if (dir_index == -1) {
3110 filp->f_pos = DIREND;
3115 bn = addressDTS(&dirtab_slot);
3116 index = dirtab_slot.slot;
3117 DT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
3119 filp->f_pos = DIREND;
3122 if (p->header.flag & BT_INTERNAL) {
3123 jfs_err("jfs_readdir: bad index table");
3129 if (dir_index == 0) {
3134 if (filldir(dirent, ".", 1, 0, ip->i_ino,
3142 if (filldir(dirent, "..", 2, 1, PARENT(ip), DT_DIR))
3146 * Find first entry of left-most leaf
3149 filp->f_pos = DIREND;
3153 if ((rc = dtReadFirst(ip, &btstack)))
3156 DT_GETSEARCH(ip, btstack.top, bn, mp, p, index);
3160 * Legacy filesystem - OS/2 & Linux JFS < 0.3.6
3162 * pn = index = 0: First entry "."
3163 * pn = 0; index = 1: Second entry ".."
3164 * pn > 0: Real entries, pn=1 -> leftmost page
3165 * pn = index = -1: No more entries
3167 dtpos = filp->f_pos;
3169 /* build "." entry */
3171 if (filldir(dirent, ".", 1, filp->f_pos, ip->i_ino,
3174 dtoffset->index = 1;
3175 filp->f_pos = dtpos;
3178 if (dtoffset->pn == 0) {
3179 if (dtoffset->index == 1) {
3180 /* build ".." entry */
3182 if (filldir(dirent, "..", 2, filp->f_pos,
3183 PARENT(ip), DT_DIR))
3186 jfs_err("jfs_readdir called with "
3190 dtoffset->index = 0;
3191 filp->f_pos = dtpos;
3195 filp->f_pos = DIREND;
3199 if ((rc = dtReadNext(ip, &filp->f_pos, &btstack))) {
3200 jfs_err("jfs_readdir: unexpected rc = %d "
3201 "from dtReadNext", rc);
3202 filp->f_pos = DIREND;
3205 /* get start leaf page and index */
3206 DT_GETSEARCH(ip, btstack.top, bn, mp, p, index);
3208 /* offset beyond directory eof ? */
3210 filp->f_pos = DIREND;
3215 dirent_buf = __get_free_page(GFP_KERNEL);
3216 if (dirent_buf == 0) {
3218 jfs_warn("jfs_readdir: __get_free_page failed!");
3219 filp->f_pos = DIREND;
3224 jfs_dirent = (struct jfs_dirent *) dirent_buf;
3226 overflow = fix_page = 0;
3228 stbl = DT_GETSTBL(p);
3230 for (i = index; i < p->header.nextindex; i++) {
3231 d = (struct ldtentry *) & p->slot[stbl[i]];
3233 if (((long) jfs_dirent + d->namlen + 1) >
3234 (dirent_buf + PAGE_SIZE)) {
3235 /* DBCS codepages could overrun dirent_buf */
3241 d_namleft = d->namlen;
3242 name_ptr = jfs_dirent->name;
3243 jfs_dirent->ino = le32_to_cpu(d->inumber);
3246 len = min(d_namleft, DTLHDRDATALEN);
3247 jfs_dirent->position = le32_to_cpu(d->index);
3249 * d->index should always be valid, but it
3250 * isn't. fsck.jfs doesn't create the
3251 * directory index for the lost+found
3252 * directory. Rather than let it go,
3253 * we can try to fix it.
3255 if ((jfs_dirent->position < 2) ||
3256 (jfs_dirent->position >=
3257 JFS_IP(ip)->next_index)) {
3258 if (!page_fixed && !isReadOnly(ip)) {
3261 * setting overflow and setting
3262 * index to i will cause the
3263 * same page to be processed
3264 * again starting here
3270 jfs_dirent->position = unique_pos++;
3273 jfs_dirent->position = dtpos;
3274 len = min(d_namleft, DTLHDRDATALEN_LEGACY);
3277 /* copy the name of head/only segment */
3278 outlen = jfs_strfromUCS_le(name_ptr, d->name, len,
3280 jfs_dirent->name_len = outlen;
3282 /* copy name in the additional segment(s) */
3285 t = (struct dtslot *) & p->slot[next];
3289 if (d_namleft == 0) {
3291 "JFS:Dtree error: ino = "
3292 "%ld, bn=%Ld, index = %d",
3298 len = min(d_namleft, DTSLOTDATALEN);
3299 outlen = jfs_strfromUCS_le(name_ptr, t->name,
3301 jfs_dirent->name_len += outlen;
3307 jfs_dirent = next_jfs_dirent(jfs_dirent);
3314 /* Point to next leaf page */
3315 if (p->header.flag & BT_ROOT)
3318 bn = le64_to_cpu(p->header.next);
3320 /* update offset (pn:index) for new page */
3323 dtoffset->index = 0;
3329 /* unpin previous leaf page */
3332 jfs_dirent = (struct jfs_dirent *) dirent_buf;
3333 while (jfs_dirents--) {
3334 filp->f_pos = jfs_dirent->position;
3335 if (filldir(dirent, jfs_dirent->name,
3336 jfs_dirent->name_len, filp->f_pos,
3337 jfs_dirent->ino, DT_UNKNOWN))
3339 jfs_dirent = next_jfs_dirent(jfs_dirent);
3343 add_missing_indices(ip, bn);
3347 if (!overflow && (bn == 0)) {
3348 filp->f_pos = DIREND;
3352 DT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
3354 free_page(dirent_buf);
3360 free_page(dirent_buf);
3369 * function: get the leftmost page of the directory
3371 static int dtReadFirst(struct inode *ip, struct btstack * btstack)
3375 int psize = 288; /* initial in-line directory */
3376 struct metapage *mp;
3379 struct btframe *btsp;
3382 BT_CLR(btstack); /* reset stack */
3385 * descend leftmost path of the tree
3387 * by convention, root bn = 0.
3390 DT_GETPAGE(ip, bn, mp, psize, p, rc);
3395 * leftmost leaf page
3397 if (p->header.flag & BT_LEAF) {
3398 /* return leftmost entry */
3399 btsp = btstack->top;
3408 * descend down to leftmost child page
3410 if (BT_STACK_FULL(btstack)) {
3412 jfs_error(ip->i_sb, "dtReadFirst: btstack overrun");
3413 BT_STACK_DUMP(btstack);
3416 /* push (bn, index) of the parent page/entry */
3417 BT_PUSH(btstack, bn, 0);
3419 /* get the leftmost entry */
3420 stbl = DT_GETSTBL(p);
3421 xd = (pxd_t *) & p->slot[stbl[0]];
3423 /* get the child page block address */
3424 bn = addressPXD(xd);
3425 psize = lengthPXD(xd) << JFS_SBI(ip->i_sb)->l2bsize;
3427 /* unpin the parent page */
3436 * function: get the page of the specified offset (pn:index)
3438 * return: if (offset > eof), bn = -1;
3440 * note: if index > nextindex of the target leaf page,
3441 * start with 1st entry of next leaf page;
3443 static int dtReadNext(struct inode *ip, loff_t * offset,
3444 struct btstack * btstack)
3451 } *dtoffset = (struct dtoffset *) offset;
3453 struct metapage *mp;
3458 struct btframe *btsp, *parent;
3462 * get leftmost leaf page pinned
3464 if ((rc = dtReadFirst(ip, btstack)))
3468 DT_GETSEARCH(ip, btstack->top, bn, mp, p, index);
3470 /* get the start offset (pn:index) */
3471 pn = dtoffset->pn - 1; /* Now pn = 0 represents leftmost leaf */
3472 index = dtoffset->index;
3474 /* start at leftmost page ? */
3476 /* offset beyond eof ? */
3477 if (index < p->header.nextindex)
3480 if (p->header.flag & BT_ROOT) {
3485 /* start with 1st entry of next leaf page */
3487 dtoffset->index = index = 0;
3491 /* start at non-leftmost page: scan parent pages for large pn */
3492 if (p->header.flag & BT_ROOT) {
3497 /* start after next leaf page ? */
3501 /* get leaf page pn = 1 */
3503 bn = le64_to_cpu(p->header.next);
3505 /* unpin leaf page */
3508 /* offset beyond eof ? */
3517 * scan last internal page level to get target leaf page
3520 /* unpin leftmost leaf page */
3523 /* get left most parent page */
3524 btsp = btstack->top;
3527 DT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
3531 /* scan parent pages at last internal page level */
3532 while (pn >= p->header.nextindex) {
3533 pn -= p->header.nextindex;
3535 /* get next parent page address */
3536 bn = le64_to_cpu(p->header.next);
3538 /* unpin current parent page */
3541 /* offset beyond eof ? */
3547 /* get next parent page */
3548 DT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
3552 /* update parent page stack frame */
3556 /* get leaf page address */
3557 stbl = DT_GETSTBL(p);
3558 xd = (pxd_t *) & p->slot[stbl[pn]];
3559 bn = addressPXD(xd);
3561 /* unpin parent page */
3565 * get target leaf page
3568 DT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
3573 * leaf page has been completed:
3574 * start with 1st entry of next leaf page
3576 if (index >= p->header.nextindex) {
3577 bn = le64_to_cpu(p->header.next);
3579 /* unpin leaf page */
3582 /* offset beyond eof ? */
3588 /* get next leaf page */
3589 DT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
3593 /* start with 1st entry of next leaf page */
3595 dtoffset->index = 0;
3599 /* return target leaf page pinned */
3600 btsp = btstack->top;
3602 btsp->index = dtoffset->index;
3612 * function: compare search key with an internal entry
3615 * < 0 if k is < record
3616 * = 0 if k is = record
3617 * > 0 if k is > record
3619 static int dtCompare(struct component_name * key, /* search key */
3620 dtpage_t * p, /* directory page */
3622 { /* entry slot index */
3625 int klen, namlen, len, rc;
3626 struct idtentry *ih;
3630 * force the left-most key on internal pages, at any level of
3631 * the tree, to be less than any search key.
3632 * this obviates having to update the leftmost key on an internal
3633 * page when the user inserts a new key in the tree smaller than
3634 * anything that has been stored.
3636 * (? if/when dtSearch() narrows down to 1st entry (index = 0),
3637 * at any internal page at any level of the tree,
3638 * it descends to child of the entry anyway -
3639 * ? make the entry as min size dummy entry)
3641 * if (e->index == 0 && h->prevpg == P_INVALID && !(h->flags & BT_LEAF))
3648 ih = (struct idtentry *) & p->slot[si];
3651 namlen = ih->namlen;
3652 len = min(namlen, DTIHDRDATALEN);
3654 /* compare with head/only segment */
3655 len = min(klen, len);
3656 if ((rc = UniStrncmp_le(kname, name, len)))
3662 /* compare with additional segment(s) */
3664 while (klen > 0 && namlen > 0) {
3665 /* compare with next name segment */
3666 t = (struct dtslot *) & p->slot[si];
3667 len = min(namlen, DTSLOTDATALEN);
3668 len = min(klen, len);
3670 if ((rc = UniStrncmp_le(kname, name, len)))
3679 return (klen - namlen);
3688 * function: compare search key with an (leaf/internal) entry
3691 * < 0 if k is < record
3692 * = 0 if k is = record
3693 * > 0 if k is > record
3695 static int ciCompare(struct component_name * key, /* search key */
3696 dtpage_t * p, /* directory page */
3697 int si, /* entry slot index */
3702 int klen, namlen, len, rc;
3703 struct ldtentry *lh;
3704 struct idtentry *ih;
3709 * force the left-most key on internal pages, at any level of
3710 * the tree, to be less than any search key.
3711 * this obviates having to update the leftmost key on an internal
3712 * page when the user inserts a new key in the tree smaller than
3713 * anything that has been stored.
3715 * (? if/when dtSearch() narrows down to 1st entry (index = 0),
3716 * at any internal page at any level of the tree,
3717 * it descends to child of the entry anyway -
3718 * ? make the entry as min size dummy entry)
3720 * if (e->index == 0 && h->prevpg == P_INVALID && !(h->flags & BT_LEAF))
3730 if (p->header.flag & BT_LEAF) {
3731 lh = (struct ldtentry *) & p->slot[si];
3734 namlen = lh->namlen;
3735 if (flag & JFS_DIR_INDEX)
3736 len = min(namlen, DTLHDRDATALEN);
3738 len = min(namlen, DTLHDRDATALEN_LEGACY);
3741 * internal page entry
3744 ih = (struct idtentry *) & p->slot[si];
3747 namlen = ih->namlen;
3748 len = min(namlen, DTIHDRDATALEN);
3751 /* compare with head/only segment */
3752 len = min(klen, len);
3753 for (i = 0; i < len; i++, kname++, name++) {
3754 /* only uppercase if case-insensitive support is on */
3755 if ((flag & JFS_OS2) == JFS_OS2)
3756 x = UniToupper(le16_to_cpu(*name));
3758 x = le16_to_cpu(*name);
3759 if ((rc = *kname - x))
3766 /* compare with additional segment(s) */
3767 while (klen > 0 && namlen > 0) {
3768 /* compare with next name segment */
3769 t = (struct dtslot *) & p->slot[si];
3770 len = min(namlen, DTSLOTDATALEN);
3771 len = min(klen, len);
3773 for (i = 0; i < len; i++, kname++, name++) {
3774 /* only uppercase if case-insensitive support is on */
3775 if ((flag & JFS_OS2) == JFS_OS2)
3776 x = UniToupper(le16_to_cpu(*name));
3778 x = le16_to_cpu(*name);
3780 if ((rc = *kname - x))
3789 return (klen - namlen);
3794 * ciGetLeafPrefixKey()
3796 * function: compute prefix of suffix compression
3797 * from two adjacent leaf entries
3798 * across page boundary
3800 * return: non-zero on error
3803 static int ciGetLeafPrefixKey(dtpage_t * lp, int li, dtpage_t * rp,
3804 int ri, struct component_name * key, int flag)
3807 wchar_t *pl, *pr, *kname;
3808 struct component_name lkey;
3809 struct component_name rkey;
3811 lkey.name = (wchar_t *) kmalloc((JFS_NAME_MAX + 1) * sizeof(wchar_t),
3813 if (lkey.name == NULL)
3816 rkey.name = (wchar_t *) kmalloc((JFS_NAME_MAX + 1) * sizeof(wchar_t),
3818 if (rkey.name == NULL) {
3823 /* get left and right key */
3824 dtGetKey(lp, li, &lkey, flag);
3825 lkey.name[lkey.namlen] = 0;
3827 if ((flag & JFS_OS2) == JFS_OS2)
3830 dtGetKey(rp, ri, &rkey, flag);
3831 rkey.name[rkey.namlen] = 0;
3834 if ((flag & JFS_OS2) == JFS_OS2)
3837 /* compute prefix */
3840 namlen = min(lkey.namlen, rkey.namlen);
3841 for (pl = lkey.name, pr = rkey.name;
3842 namlen; pl++, pr++, namlen--, klen++, kname++) {
3845 key->namlen = klen + 1;
3850 /* l->namlen <= r->namlen since l <= r */
3851 if (lkey.namlen < rkey.namlen) {
3853 key->namlen = klen + 1;
3854 } else /* l->namelen == r->namelen */
3868 * function: get key of the entry
3870 static void dtGetKey(dtpage_t * p, int i, /* entry index */
3871 struct component_name * key, int flag)
3875 struct ldtentry *lh;
3876 struct idtentry *ih;
3883 stbl = DT_GETSTBL(p);
3885 if (p->header.flag & BT_LEAF) {
3886 lh = (struct ldtentry *) & p->slot[si];
3888 namlen = lh->namlen;
3890 if (flag & JFS_DIR_INDEX)
3891 len = min(namlen, DTLHDRDATALEN);
3893 len = min(namlen, DTLHDRDATALEN_LEGACY);
3895 ih = (struct idtentry *) & p->slot[si];
3897 namlen = ih->namlen;
3899 len = min(namlen, DTIHDRDATALEN);
3902 key->namlen = namlen;
3906 * move head/only segment
3908 UniStrncpy_from_le(kname, name, len);
3911 * move additional segment(s)
3914 /* get next segment */
3918 len = min(namlen, DTSLOTDATALEN);
3919 UniStrncpy_from_le(kname, t->name, len);
3929 * function: allocate free slot(s) and
3930 * write a leaf/internal entry
3932 * return: entry slot index
3934 static void dtInsertEntry(dtpage_t * p, int index, struct component_name * key,
3935 ddata_t * data, struct dt_lock ** dtlock)
3937 struct dtslot *h, *t;
3938 struct ldtentry *lh = NULL;
3939 struct idtentry *ih = NULL;
3940 int hsi, fsi, klen, len, nextindex;
3945 struct dt_lock *dtlck = *dtlock;
3949 struct metapage *mp = NULL;
3954 /* allocate a free slot */
3955 hsi = fsi = p->header.freelist;
3957 p->header.freelist = h->next;
3958 --p->header.freecnt;
3960 /* open new linelock */
3961 if (dtlck->index >= dtlck->maxcnt)
3962 dtlck = (struct dt_lock *) txLinelock(dtlck);
3964 lv = & dtlck->lv[dtlck->index];
3967 /* write head/only segment */
3968 if (p->header.flag & BT_LEAF) {
3969 lh = (struct ldtentry *) h;
3971 lh->inumber = cpu_to_le32(data->leaf.ino);
3974 if (data->leaf.ip) {
3975 len = min(klen, DTLHDRDATALEN);
3976 if (!(p->header.flag & BT_ROOT))
3977 bn = addressPXD(&p->header.self);
3978 lh->index = cpu_to_le32(add_index(data->leaf.tid,
3982 len = min(klen, DTLHDRDATALEN_LEGACY);
3984 ih = (struct idtentry *) h;
3990 len = min(klen, DTIHDRDATALEN);
3993 UniStrncpy_to_le(name, kname, len);
3998 /* write additional segment(s) */
4003 fsi = p->header.freelist;
4005 p->header.freelist = t->next;
4006 --p->header.freecnt;
4008 /* is next slot contiguous ? */
4009 if (fsi != xsi + 1) {
4010 /* close current linelock */
4014 /* open new linelock */
4015 if (dtlck->index < dtlck->maxcnt)
4018 dtlck = (struct dt_lock *) txLinelock(dtlck);
4019 lv = & dtlck->lv[0];
4027 len = min(klen, DTSLOTDATALEN);
4028 UniStrncpy_to_le(t->name, kname, len);
4035 /* close current linelock */
4041 /* terminate last/only segment */
4043 /* single segment entry */
4044 if (p->header.flag & BT_LEAF)
4049 /* multi-segment entry */
4052 /* if insert into middle, shift right succeeding entries in stbl */
4053 stbl = DT_GETSTBL(p);
4054 nextindex = p->header.nextindex;
4055 if (index < nextindex) {
4056 memmove(stbl + index + 1, stbl + index, nextindex - index);
4058 if ((p->header.flag & BT_LEAF) && data->leaf.ip) {
4062 * Need to update slot number for entries that moved
4066 for (n = index + 1; n <= nextindex; n++) {
4067 lh = (struct ldtentry *) & (p->slot[stbl[n]]);
4068 modify_index(data->leaf.tid, data->leaf.ip,
4069 le32_to_cpu(lh->index), bn, n,
4073 release_metapage(mp);
4079 /* advance next available entry index of stbl */
4080 ++p->header.nextindex;
4087 * function: move entries from split/left page to new/right page
4089 * nextindex of dst page and freelist/freecnt of both pages
4092 static void dtMoveEntry(dtpage_t * sp, int si, dtpage_t * dp,
4093 struct dt_lock ** sdtlock, struct dt_lock ** ddtlock,
4096 int ssi, next; /* src slot index */
4097 int di; /* dst entry index */
4098 int dsi; /* dst slot index */
4099 s8 *sstbl, *dstbl; /* sorted entry table */
4101 struct ldtentry *slh, *dlh = NULL;
4102 struct idtentry *sih, *dih = NULL;
4103 struct dtslot *h, *s, *d;
4104 struct dt_lock *sdtlck = *sdtlock, *ddtlck = *ddtlock;
4105 struct lv *slv, *dlv;
4109 sstbl = (s8 *) & sp->slot[sp->header.stblindex];
4110 dstbl = (s8 *) & dp->slot[dp->header.stblindex];
4112 dsi = dp->header.freelist; /* first (whole page) free slot */
4113 sfsi = sp->header.freelist;
4115 /* linelock destination entry slot */
4116 dlv = & ddtlck->lv[ddtlck->index];
4119 /* linelock source entry slot */
4120 slv = & sdtlck->lv[sdtlck->index];
4121 slv->offset = sstbl[si];
4122 xssi = slv->offset - 1;
4128 for (di = 0; si < sp->header.nextindex; si++, di++) {
4132 /* is next slot contiguous ? */
4133 if (ssi != xssi + 1) {
4134 /* close current linelock */
4138 /* open new linelock */
4139 if (sdtlck->index < sdtlck->maxcnt)
4142 sdtlck = (struct dt_lock *) txLinelock(sdtlck);
4143 slv = & sdtlck->lv[0];
4151 * move head/only segment of an entry
4154 h = d = &dp->slot[dsi];
4156 /* get src slot and move */
4158 if (sp->header.flag & BT_LEAF) {
4159 /* get source entry */
4160 slh = (struct ldtentry *) s;
4161 dlh = (struct ldtentry *) h;
4162 snamlen = slh->namlen;
4165 len = min(snamlen, DTLHDRDATALEN);
4166 dlh->index = slh->index; /* little-endian */
4168 len = min(snamlen, DTLHDRDATALEN_LEGACY);
4170 memcpy(dlh, slh, 6 + len * 2);
4174 /* update dst head/only segment next field */
4178 sih = (struct idtentry *) s;
4179 snamlen = sih->namlen;
4181 len = min(snamlen, DTIHDRDATALEN);
4182 dih = (struct idtentry *) h;
4183 memcpy(dih, sih, 10 + len * 2);
4190 /* free src head/only segment */
4200 * move additional segment(s) of the entry
4203 while ((ssi = next) >= 0) {
4204 /* is next slot contiguous ? */
4205 if (ssi != xssi + 1) {
4206 /* close current linelock */
4210 /* open new linelock */
4211 if (sdtlck->index < sdtlck->maxcnt)
4217 slv = & sdtlck->lv[0];
4224 /* get next source segment */
4227 /* get next destination free slot */
4230 len = min(snamlen, DTSLOTDATALEN);
4231 UniStrncpy_le(d->name, s->name, len);
4240 /* free source segment */
4249 /* terminate dst last/only segment */
4251 /* single segment entry */
4252 if (dp->header.flag & BT_LEAF)
4257 /* multi-segment entry */
4261 /* close current linelock */
4270 /* update source header */
4271 sp->header.freelist = sfsi;
4272 sp->header.freecnt += nd;
4274 /* update destination header */
4275 dp->header.nextindex = di;
4277 dp->header.freelist = dsi;
4278 dp->header.freecnt -= nd;
4285 * function: free a (leaf/internal) entry
4287 * log freelist header, stbl, and each segment slot of entry
4288 * (even though last/only segment next field is modified,
4289 * physical image logging requires all segment slots of
4290 * the entry logged to avoid applying previous updates
4291 * to the same slots)
4293 static void dtDeleteEntry(dtpage_t * p, int fi, struct dt_lock ** dtlock)
4295 int fsi; /* free entry slot index */
4299 struct dt_lock *dtlck = *dtlock;
4303 /* get free entry slot index */
4304 stbl = DT_GETSTBL(p);
4307 /* open new linelock */
4308 if (dtlck->index >= dtlck->maxcnt)
4309 dtlck = (struct dt_lock *) txLinelock(dtlck);
4310 lv = & dtlck->lv[dtlck->index];
4314 /* get the head/only segment */
4316 if (p->header.flag & BT_LEAF)
4317 si = ((struct ldtentry *) t)->next;
4319 si = ((struct idtentry *) t)->next;
4326 /* find the last/only segment */
4328 /* is next slot contiguous ? */
4329 if (si != xsi + 1) {
4330 /* close current linelock */
4334 /* open new linelock */
4335 if (dtlck->index < dtlck->maxcnt)
4338 dtlck = (struct dt_lock *) txLinelock(dtlck);
4339 lv = & dtlck->lv[0];
4355 /* close current linelock */
4361 /* update freelist */
4362 t->next = p->header.freelist;
4363 p->header.freelist = fsi;
4364 p->header.freecnt += freecnt;
4366 /* if delete from middle,
4367 * shift left the succedding entries in the stbl
4369 si = p->header.nextindex;
4371 memmove(&stbl[fi], &stbl[fi + 1], si - fi - 1);
4373 p->header.nextindex--;
4380 * function: truncate a (leaf/internal) entry
4382 * log freelist header, stbl, and each segment slot of entry
4383 * (even though last/only segment next field is modified,
4384 * physical image logging requires all segment slots of
4385 * the entry logged to avoid applying previous updates
4386 * to the same slots)
4388 static void dtTruncateEntry(dtpage_t * p, int ti, struct dt_lock ** dtlock)
4390 int tsi; /* truncate entry slot index */
4394 struct dt_lock *dtlck = *dtlock;
4398 /* get free entry slot index */
4399 stbl = DT_GETSTBL(p);
4402 /* open new linelock */
4403 if (dtlck->index >= dtlck->maxcnt)
4404 dtlck = (struct dt_lock *) txLinelock(dtlck);
4405 lv = & dtlck->lv[dtlck->index];
4409 /* get the head/only segment */
4411 ASSERT(p->header.flag & BT_INTERNAL);
4412 ((struct idtentry *) t)->namlen = 0;
4413 si = ((struct idtentry *) t)->next;
4414 ((struct idtentry *) t)->next = -1;
4421 /* find the last/only segment */
4423 /* is next slot contiguous ? */
4424 if (si != xsi + 1) {
4425 /* close current linelock */
4429 /* open new linelock */
4430 if (dtlck->index < dtlck->maxcnt)
4433 dtlck = (struct dt_lock *) txLinelock(dtlck);
4434 lv = & dtlck->lv[0];
4450 /* close current linelock */
4456 /* update freelist */
4459 t->next = p->header.freelist;
4460 p->header.freelist = fsi;
4461 p->header.freecnt += freecnt;
4466 * dtLinelockFreelist()
4468 static void dtLinelockFreelist(dtpage_t * p, /* directory page */
4469 int m, /* max slot index */
4470 struct dt_lock ** dtlock)
4472 int fsi; /* free entry slot index */
4475 struct dt_lock *dtlck = *dtlock;
4479 /* get free entry slot index */
4480 fsi = p->header.freelist;
4482 /* open new linelock */
4483 if (dtlck->index >= dtlck->maxcnt)
4484 dtlck = (struct dt_lock *) txLinelock(dtlck);
4485 lv = & dtlck->lv[dtlck->index];
4495 /* find the last/only segment */
4496 while (si < m && si >= 0) {
4497 /* is next slot contiguous ? */
4498 if (si != xsi + 1) {
4499 /* close current linelock */
4503 /* open new linelock */
4504 if (dtlck->index < dtlck->maxcnt)
4507 dtlck = (struct dt_lock *) txLinelock(dtlck);
4508 lv = & dtlck->lv[0];
4522 /* close current linelock */
4533 * FUNCTION: Modify the inode number part of a directory entry
4536 * tid - Transaction id
4537 * ip - Inode of parent directory
4538 * key - Name of entry to be modified
4539 * orig_ino - Original inode number expected in entry
4540 * new_ino - New inode number to put into entry
4544 * -ESTALE - If entry found does not match orig_ino passed in
4545 * -ENOENT - If no entry can be found to match key
4546 * 0 - If successfully modified entry
4548 int dtModify(tid_t tid, struct inode *ip,
4549 struct component_name * key, ino_t * orig_ino, ino_t new_ino, int flag)
4553 struct metapage *mp;
4556 struct btstack btstack;
4558 struct dt_lock *dtlck;
4561 int entry_si; /* entry slot index */
4562 struct ldtentry *entry;
4565 * search for the entry to modify:
4567 * dtSearch() returns (leaf page pinned, index at which to modify).
4569 if ((rc = dtSearch(ip, key, orig_ino, &btstack, flag)))
4572 /* retrieve search result */
4573 DT_GETSEARCH(ip, btstack.top, bn, mp, p, index);
4575 BT_MARK_DIRTY(mp, ip);
4577 * acquire a transaction lock on the leaf page of named entry
4579 tlck = txLock(tid, ip, mp, tlckDTREE | tlckENTRY);
4580 dtlck = (struct dt_lock *) & tlck->lock;
4582 /* get slot index of the entry */
4583 stbl = DT_GETSTBL(p);
4584 entry_si = stbl[index];
4586 /* linelock entry */
4587 ASSERT(dtlck->index == 0);
4588 lv = & dtlck->lv[0];
4589 lv->offset = entry_si;
4593 /* get the head/only segment */
4594 entry = (struct ldtentry *) & p->slot[entry_si];
4596 /* substitute the inode number of the entry */
4597 entry->inumber = cpu_to_le32(new_ino);
4599 /* unpin the leaf page */