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/slab.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); \
131 if (((P)->header.nextindex > \
132 (((BN) == 0) ? DTROOTMAXSLOT : (P)->header.maxslot)) || \
133 ((BN) && ((P)->header.maxslot > DTPAGEMAXSLOT))) { \
135 jfs_error((IP)->i_sb, \
136 "DT_GETPAGE: dtree page corrupt\n"); \
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 (dbAlloc(ip, 0, sbi->nbperpage, &xaddr)) {
388 dquot_free_block(ip, sbi->nbperpage);
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 mp = get_index_page(ip, 0);
419 jfs_err("add_index: get_metapage failed!");
420 xtTruncate(tid, ip, 0, COMMIT_PWMAP);
421 memcpy(&jfs_ip->i_dirtable, temp_table,
422 sizeof (temp_table));
425 tlck = txLock(tid, ip, mp, tlckDATA);
426 llck = (struct linelock *) & tlck->lock;
427 ASSERT(llck->index == 0);
431 lv->length = 6; /* tlckDATA slot size is 16 bytes */
434 memcpy(mp->data, temp_table, sizeof(temp_table));
436 mark_metapage_dirty(mp);
437 release_metapage(mp);
440 * Logging is now directed by xtree tlocks
442 clear_cflag(COMMIT_Dirtable, ip);
445 offset = (index - 2) * sizeof(struct dir_table_slot);
446 page_offset = offset & (PSIZE - 1);
447 blkno = ((offset + 1) >> L2PSIZE) << sbi->l2nbperpage;
448 if (page_offset == 0) {
450 * This will be the beginning of a new page
453 if (xtInsert(tid, ip, 0, blkno, sbi->nbperpage, &xaddr, 0)) {
454 jfs_warn("add_index: xtInsert failed!");
459 if ((mp = get_index_page(ip, blkno)))
460 memset(mp->data, 0, PSIZE); /* Just looks better */
462 xtTruncate(tid, ip, offset, COMMIT_PWMAP);
464 mp = read_index_page(ip, blkno);
467 jfs_err("add_index: get/read_metapage failed!");
471 lock_index(tid, ip, mp, index);
474 (struct dir_table_slot *) ((char *) mp->data + page_offset);
475 dirtab_slot->flag = DIR_INDEX_VALID;
476 dirtab_slot->slot = slot;
477 DTSaddress(dirtab_slot, bn);
479 mark_metapage_dirty(mp);
480 release_metapage(mp);
486 jfs_ip->next_index--;
494 * Marks an entry to the directory index table as free.
496 static void free_index(tid_t tid, struct inode *ip, u32 index, u32 next)
498 struct dir_table_slot *dirtab_slot;
500 struct metapage *mp = NULL;
502 dirtab_slot = find_index(ip, index, &mp, &lblock);
507 dirtab_slot->flag = DIR_INDEX_FREE;
508 dirtab_slot->slot = dirtab_slot->addr1 = 0;
509 dirtab_slot->addr2 = cpu_to_le32(next);
512 lock_index(tid, ip, mp, index);
513 mark_metapage_dirty(mp);
514 release_metapage(mp);
516 set_cflag(COMMIT_Dirtable, ip);
522 * Changes an entry in the directory index table
524 static void modify_index(tid_t tid, struct inode *ip, u32 index, s64 bn,
525 int slot, struct metapage ** mp, s64 *lblock)
527 struct dir_table_slot *dirtab_slot;
529 dirtab_slot = find_index(ip, index, mp, lblock);
534 DTSaddress(dirtab_slot, bn);
535 dirtab_slot->slot = slot;
538 lock_index(tid, ip, *mp, index);
539 mark_metapage_dirty(*mp);
541 set_cflag(COMMIT_Dirtable, ip);
547 * reads a directory table slot
549 static int read_index(struct inode *ip, u32 index,
550 struct dir_table_slot * dirtab_slot)
553 struct metapage *mp = NULL;
554 struct dir_table_slot *slot;
556 slot = find_index(ip, index, &mp, &lblock);
561 memcpy(dirtab_slot, slot, sizeof(struct dir_table_slot));
564 release_metapage(mp);
573 * Search for the entry with specified key
577 * return: 0 - search result on stack, leaf page pinned;
580 int dtSearch(struct inode *ip, struct component_name * key, ino_t * data,
581 struct btstack * btstack, int flag)
584 int cmp = 1; /* init for empty page */
589 int base, index, lim;
590 struct btframe *btsp;
592 int psize = 288; /* initial in-line directory */
594 struct component_name ciKey;
595 struct super_block *sb = ip->i_sb;
597 ciKey.name = kmalloc_array(JFS_NAME_MAX + 1, sizeof(wchar_t),
605 /* uppercase search key for c-i directory */
606 UniStrcpy(ciKey.name, key->name);
607 ciKey.namlen = key->namlen;
609 /* only uppercase if case-insensitive support is on */
610 if ((JFS_SBI(sb)->mntflag & JFS_OS2) == JFS_OS2) {
613 BT_CLR(btstack); /* reset stack */
615 /* init level count for max pages to split */
619 * search down tree from root:
621 * between two consecutive entries of <Ki, Pi> and <Kj, Pj> of
622 * internal page, child page Pi contains entry with k, Ki <= K < Kj.
624 * if entry with search key K is not found
625 * internal page search find the entry with largest key Ki
626 * less than K which point to the child page to search;
627 * leaf page search find the entry with smallest key Kj
628 * greater than K so that the returned index is the position of
629 * the entry to be shifted right for insertion of new entry.
630 * for empty tree, search key is greater than any key of the tree.
632 * by convention, root bn = 0.
635 /* get/pin the page to search */
636 DT_GETPAGE(ip, bn, mp, psize, p, rc);
640 /* get sorted entry table of the page */
641 stbl = DT_GETSTBL(p);
644 * binary search with search key K on the current page.
646 for (base = 0, lim = p->header.nextindex; lim; lim >>= 1) {
647 index = base + (lim >> 1);
649 if (stbl[index] < 0) {
654 if (p->header.flag & BT_LEAF) {
655 /* uppercase leaf name to compare */
657 ciCompare(&ciKey, p, stbl[index],
658 JFS_SBI(sb)->mntflag);
660 /* router key is in uppercase */
662 cmp = dtCompare(&ciKey, p, stbl[index]);
670 /* search hit - leaf page:
671 * return the entry found
673 if (p->header.flag & BT_LEAF) {
674 inumber = le32_to_cpu(
675 ((struct ldtentry *) & p->slot[stbl[index]])->inumber);
678 * search for JFS_LOOKUP
680 if (flag == JFS_LOOKUP) {
687 * search for JFS_CREATE
689 if (flag == JFS_CREATE) {
696 * search for JFS_REMOVE or JFS_RENAME
698 if ((flag == JFS_REMOVE ||
699 flag == JFS_RENAME) &&
706 * JFS_REMOVE|JFS_FINDDIR|JFS_RENAME
708 /* save search result */
719 /* search hit - internal page:
720 * descend/search its child page
734 * base is the smallest index with key (Kj) greater than
735 * search key (K) and may be zero or (maxindex + 1) index.
738 * search miss - leaf page
740 * return location of entry (base) where new entry with
741 * search key K is to be inserted.
743 if (p->header.flag & BT_LEAF) {
745 * search for JFS_LOOKUP, JFS_REMOVE, or JFS_RENAME
747 if (flag == JFS_LOOKUP || flag == JFS_REMOVE ||
748 flag == JFS_RENAME) {
754 * search for JFS_CREATE|JFS_FINDDIR:
769 * search miss - internal page
771 * if base is non-zero, decrement base by one to get the parent
772 * entry of the child page to search.
774 index = base ? base - 1 : base;
777 * go down to child page
780 /* update max. number of pages to split */
781 if (BT_STACK_FULL(btstack)) {
782 /* Something's corrupted, mark filesystem dirty so
783 * chkdsk will fix it.
785 jfs_error(sb, "stack overrun!\n");
786 BT_STACK_DUMP(btstack);
792 /* push (bn, index) of the parent page/entry */
793 BT_PUSH(btstack, bn, index);
795 /* get the child page block number */
796 pxd = (pxd_t *) & p->slot[stbl[index]];
797 bn = addressPXD(pxd);
798 psize = lengthPXD(pxd) << JFS_SBI(ip->i_sb)->l2bsize;
800 /* unpin the parent page */
820 * function: insert an entry to directory tree
824 * return: 0 - success;
827 int dtInsert(tid_t tid, struct inode *ip,
828 struct component_name * name, ino_t * fsn, struct btstack * btstack)
831 struct metapage *mp; /* meta-page buffer */
832 dtpage_t *p; /* base B+-tree index page */
835 struct dtsplit split; /* split information */
837 struct dt_lock *dtlck;
843 * retrieve search result
845 * dtSearch() returns (leaf page pinned, index at which to insert).
846 * n.b. dtSearch() may return index of (maxindex + 1) of
849 DT_GETSEARCH(ip, btstack->top, bn, mp, p, index);
852 * insert entry for new key
855 if (JFS_IP(ip)->next_index == DIREND) {
859 n = NDTLEAF(name->namlen);
863 n = NDTLEAF_LEGACY(name->namlen);
864 data.leaf.ip = NULL; /* signifies legacy directory format */
866 data.leaf.ino = *fsn;
869 * leaf page does not have enough room for new entry:
871 * extend/split the leaf page;
873 * dtSplitUp() will insert the entry and unpin the leaf page.
875 if (n > p->header.freecnt) {
881 rc = dtSplitUp(tid, ip, &split, btstack);
886 * leaf page does have enough room for new entry:
888 * insert the new data entry into the leaf page;
890 BT_MARK_DIRTY(mp, ip);
892 * acquire a transaction lock on the leaf page
894 tlck = txLock(tid, ip, mp, tlckDTREE | tlckENTRY);
895 dtlck = (struct dt_lock *) & tlck->lock;
896 ASSERT(dtlck->index == 0);
899 /* linelock header */
904 dtInsertEntry(p, index, name, &data, &dtlck);
906 /* linelock stbl of non-root leaf page */
907 if (!(p->header.flag & BT_ROOT)) {
908 if (dtlck->index >= dtlck->maxcnt)
909 dtlck = (struct dt_lock *) txLinelock(dtlck);
910 lv = & dtlck->lv[dtlck->index];
911 n = index >> L2DTSLOTSIZE;
912 lv->offset = p->header.stblindex + n;
914 ((p->header.nextindex - 1) >> L2DTSLOTSIZE) - n + 1;
918 /* unpin the leaf page */
928 * function: propagate insertion bottom up;
932 * return: 0 - success;
934 * leaf page unpinned;
936 static int dtSplitUp(tid_t tid,
937 struct inode *ip, struct dtsplit * split, struct btstack * btstack)
939 struct jfs_sb_info *sbi = JFS_SBI(ip->i_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;
964 sp = DT_PAGE(ip, smp);
966 key.name = kmalloc_array(JFS_NAME_MAX + 2, sizeof(wchar_t), GFP_NOFS);
976 * The split routines insert the new entry, and
977 * acquire txLock as appropriate.
980 * split root leaf page:
982 if (sp->header.flag & BT_ROOT) {
984 * allocate a single extent child page
987 n = sbi->bsize >> L2DTSLOTSIZE;
988 n -= (n + 31) >> L2DTSLOTSIZE; /* stbl size */
989 n -= DTROOTMAXSLOT - sp->header.freecnt; /* header + entries */
990 if (n <= split->nslot)
992 if ((rc = dbAlloc(ip, 0, (s64) xlen, &xaddr))) {
999 pxd = &pxdlist.pxd[0];
1000 PXDaddress(pxd, xaddr);
1001 PXDlength(pxd, xlen);
1002 split->pxdlist = &pxdlist;
1003 rc = dtSplitRoot(tid, ip, split, &rmp);
1006 dbFree(ip, xaddr, xlen);
1013 ip->i_size = xlen << sbi->l2bsize;
1019 * extend first leaf page
1021 * extend the 1st extent if less than buffer page size
1022 * (dtExtendPage() reurns leaf page unpinned)
1024 pxd = &sp->header.self;
1025 xlen = lengthPXD(pxd);
1026 xsize = xlen << sbi->l2bsize;
1027 if (xsize < PSIZE) {
1028 xaddr = addressPXD(pxd);
1029 n = xsize >> L2DTSLOTSIZE;
1030 n -= (n + 31) >> L2DTSLOTSIZE; /* stbl size */
1031 if ((n + sp->header.freecnt) <= split->nslot)
1032 n = xlen + (xlen << 1);
1036 /* Allocate blocks to quota. */
1037 rc = dquot_alloc_block(ip, n);
1040 quota_allocation += n;
1042 if ((rc = dbReAlloc(sbi->ipbmap, xaddr, (s64) xlen,
1046 pxdlist.maxnpxd = 1;
1048 pxd = &pxdlist.pxd[0];
1049 PXDaddress(pxd, nxaddr);
1050 PXDlength(pxd, xlen + n);
1051 split->pxdlist = &pxdlist;
1052 if ((rc = dtExtendPage(tid, ip, split, btstack))) {
1053 nxaddr = addressPXD(pxd);
1054 if (xaddr != nxaddr) {
1055 /* free relocated extent */
1056 xlen = lengthPXD(pxd);
1057 dbFree(ip, nxaddr, (s64) xlen);
1059 /* free extended delta */
1060 xlen = lengthPXD(pxd) - n;
1061 xaddr = addressPXD(pxd) + xlen;
1062 dbFree(ip, xaddr, (s64) n);
1064 } else if (!DO_INDEX(ip))
1065 ip->i_size = lengthPXD(pxd) << sbi->l2bsize;
1074 * split leaf page <sp> into <sp> and a new right page <rp>.
1076 * return <rp> pinned and its extent descriptor <rpxd>
1079 * allocate new directory page extent and
1080 * new index page(s) to cover page split(s)
1082 * allocation hint: ?
1084 n = btstack->nsplit;
1085 pxdlist.maxnpxd = pxdlist.npxd = 0;
1086 xlen = sbi->nbperpage;
1087 for (pxd = pxdlist.pxd; n > 0; n--, pxd++) {
1088 if ((rc = dbAlloc(ip, 0, (s64) xlen, &xaddr)) == 0) {
1089 PXDaddress(pxd, xaddr);
1090 PXDlength(pxd, xlen);
1097 /* undo allocation */
1101 split->pxdlist = &pxdlist;
1102 if ((rc = dtSplitPage(tid, ip, split, &rmp, &rp, &rpxd))) {
1105 /* undo allocation */
1110 ip->i_size += PSIZE;
1113 * propagate up the router entry for the leaf page just split
1115 * insert a router entry for the new page into the parent page,
1116 * propagate the insert/split up the tree by walking back the stack
1117 * of (bn of parent page, index of child page entry in parent page)
1118 * that were traversed during the search for the page that split.
1120 * the propagation of insert/split up the tree stops if the root
1121 * splits or the page inserted into doesn't have to split to hold
1124 * the parent entry for the split page remains the same, and
1125 * a new entry is inserted at its right with the first key and
1126 * block number of the new right page.
1128 * There are a maximum of 4 pages pinned at any time:
1129 * two children, left parent and right parent (when the parent splits).
1130 * keep the child pages pinned while working on the parent.
1131 * make sure that all pins are released at exit.
1133 while ((parent = BT_POP(btstack)) != NULL) {
1134 /* parent page specified by stack frame <parent> */
1136 /* keep current child pages (<lp>, <rp>) pinned */
1141 * insert router entry in parent for new right child page <rp>
1143 /* get the parent page <sp> */
1144 DT_GETPAGE(ip, parent->bn, smp, PSIZE, sp, rc);
1152 * The new key entry goes ONE AFTER the index of parent entry,
1153 * because the split was to the right.
1155 skip = parent->index + 1;
1158 * compute the key for the router entry
1160 * key suffix compression:
1161 * for internal pages that have leaf pages as children,
1162 * retain only what's needed to distinguish between
1163 * the new entry and the entry on the page to its left.
1164 * If the keys compare equal, retain the entire key.
1166 * note that compression is performed only at computing
1167 * router key at the lowest internal level.
1168 * further compression of the key between pairs of higher
1169 * level internal pages loses too much information and
1170 * the search may fail.
1171 * (e.g., two adjacent leaf pages of {a, ..., x} {xx, ...,}
1172 * results in two adjacent parent entries (a)(xx).
1173 * if split occurs between these two entries, and
1174 * if compression is applied, the router key of parent entry
1175 * of right page (x) will divert search for x into right
1176 * subtree and miss x in the left subtree.)
1178 * the entire key must be retained for the next-to-leftmost
1179 * internal key at any level of the tree, or search may fail
1182 switch (rp->header.flag & BT_TYPE) {
1185 * compute the length of prefix for suffix compression
1186 * between last entry of left page and first entry
1189 if ((sp->header.flag & BT_ROOT && skip > 1) ||
1190 sp->header.prev != 0 || skip > 1) {
1191 /* compute uppercase router prefix key */
1192 rc = ciGetLeafPrefixKey(lp,
1193 lp->header.nextindex-1,
1203 /* next to leftmost entry of
1204 lowest internal level */
1206 /* compute uppercase router key */
1207 dtGetKey(rp, 0, &key, sbi->mntflag);
1208 key.name[key.namlen] = 0;
1210 if ((sbi->mntflag & JFS_OS2) == JFS_OS2)
1214 n = NDTINTERNAL(key.namlen);
1218 dtGetKey(rp, 0, &key, sbi->mntflag);
1219 n = NDTINTERNAL(key.namlen);
1223 jfs_err("dtSplitUp(): UFO!");
1227 /* unpin left child page */
1231 * compute the data for the router entry
1233 data->xd = rpxd; /* child page xd */
1236 * parent page is full - split the parent page
1238 if (n > sp->header.freecnt) {
1239 /* init for parent page split */
1241 split->index = skip; /* index at insert */
1244 /* split->data = data; */
1246 /* unpin right child page */
1249 /* The split routines insert the new entry,
1250 * acquire txLock as appropriate.
1251 * return <rp> pinned and its block number <rbn>.
1253 rc = (sp->header.flag & BT_ROOT) ?
1254 dtSplitRoot(tid, ip, split, &rmp) :
1255 dtSplitPage(tid, ip, split, &rmp, &rp, &rpxd);
1261 /* smp and rmp are pinned */
1264 * parent page is not full - insert router entry in parent page
1267 BT_MARK_DIRTY(smp, ip);
1269 * acquire a transaction lock on the parent page
1271 tlck = txLock(tid, ip, smp, tlckDTREE | tlckENTRY);
1272 dtlck = (struct dt_lock *) & tlck->lock;
1273 ASSERT(dtlck->index == 0);
1274 lv = & dtlck->lv[0];
1276 /* linelock header */
1281 /* linelock stbl of non-root parent page */
1282 if (!(sp->header.flag & BT_ROOT)) {
1284 n = skip >> L2DTSLOTSIZE;
1285 lv->offset = sp->header.stblindex + n;
1287 ((sp->header.nextindex -
1288 1) >> L2DTSLOTSIZE) - n + 1;
1292 dtInsertEntry(sp, skip, &key, data, &dtlck);
1294 /* exit propagate up */
1299 /* unpin current split and its right page */
1304 * free remaining extents allocated for split
1308 pxd = &pxdlist.pxd[n];
1309 for (; n < pxdlist.maxnpxd; n++, pxd++)
1310 dbFree(ip, addressPXD(pxd), (s64) lengthPXD(pxd));
1315 /* Rollback quota allocation */
1316 if (rc && quota_allocation)
1317 dquot_free_block(ip, quota_allocation);
1328 * function: Split a non-root page of a btree.
1332 * return: 0 - success;
1334 * return split and new page pinned;
1336 static int dtSplitPage(tid_t tid, struct inode *ip, struct dtsplit * split,
1337 struct metapage ** rmpp, dtpage_t ** rpp, pxd_t * rpxdp)
1340 struct metapage *smp;
1342 struct metapage *rmp;
1343 dtpage_t *rp; /* new right page allocated */
1344 s64 rbn; /* new right page block number */
1345 struct metapage *mp;
1348 struct pxdlist *pxdlist;
1350 int skip, nextindex, half, left, nxt, off, si;
1351 struct ldtentry *ldtentry;
1352 struct idtentry *idtentry;
1357 struct dt_lock *sdtlck, *rdtlck;
1359 struct dt_lock *dtlck;
1360 struct lv *slv, *rlv, *lv;
1362 /* get split page */
1364 sp = DT_PAGE(ip, smp);
1367 * allocate the new right page for the split
1369 pxdlist = split->pxdlist;
1370 pxd = &pxdlist->pxd[pxdlist->npxd];
1372 rbn = addressPXD(pxd);
1373 rmp = get_metapage(ip, rbn, PSIZE, 1);
1377 /* Allocate blocks to quota. */
1378 rc = dquot_alloc_block(ip, lengthPXD(pxd));
1380 release_metapage(rmp);
1384 jfs_info("dtSplitPage: ip:0x%p smp:0x%p rmp:0x%p", ip, smp, rmp);
1386 BT_MARK_DIRTY(rmp, ip);
1388 * acquire a transaction lock on the new right page
1390 tlck = txLock(tid, ip, rmp, tlckDTREE | tlckNEW);
1391 rdtlck = (struct dt_lock *) & tlck->lock;
1393 rp = (dtpage_t *) rmp->data;
1395 rp->header.self = *pxd;
1397 BT_MARK_DIRTY(smp, ip);
1399 * acquire a transaction lock on the split page
1403 tlck = txLock(tid, ip, smp, tlckDTREE | tlckENTRY);
1404 sdtlck = (struct dt_lock *) & tlck->lock;
1406 /* linelock header of split page */
1407 ASSERT(sdtlck->index == 0);
1408 slv = & sdtlck->lv[0];
1414 * initialize/update sibling pointers between sp and rp
1416 nextbn = le64_to_cpu(sp->header.next);
1417 rp->header.next = cpu_to_le64(nextbn);
1418 rp->header.prev = cpu_to_le64(addressPXD(&sp->header.self));
1419 sp->header.next = cpu_to_le64(rbn);
1422 * initialize new right page
1424 rp->header.flag = sp->header.flag;
1426 /* compute sorted entry table at start of extent data area */
1427 rp->header.nextindex = 0;
1428 rp->header.stblindex = 1;
1430 n = PSIZE >> L2DTSLOTSIZE;
1431 rp->header.maxslot = n;
1432 stblsize = (n + 31) >> L2DTSLOTSIZE; /* in unit of slot */
1435 fsi = rp->header.stblindex + stblsize;
1436 rp->header.freelist = fsi;
1437 rp->header.freecnt = rp->header.maxslot - fsi;
1440 * sequential append at tail: append without split
1442 * If splitting the last page on a level because of appending
1443 * a entry to it (skip is maxentry), it's likely that the access is
1444 * sequential. Adding an empty page on the side of the level is less
1445 * work and can push the fill factor much higher than normal.
1446 * If we're wrong it's no big deal, we'll just do the split the right
1448 * (It may look like it's equally easy to do a similar hack for
1449 * reverse sorted data, that is, split the tree left,
1450 * but it's not. Be my guest.)
1452 if (nextbn == 0 && split->index == sp->header.nextindex) {
1453 /* linelock header + stbl (first slot) of new page */
1454 rlv = & rdtlck->lv[rdtlck->index];
1460 * initialize freelist of new right page
1463 for (fsi++; fsi < rp->header.maxslot; f++, fsi++)
1467 /* insert entry at the first entry of the new right page */
1468 dtInsertEntry(rp, 0, split->key, split->data, &rdtlck);
1474 * non-sequential insert (at possibly middle page)
1478 * update prev pointer of previous right sibling page;
1481 DT_GETPAGE(ip, nextbn, mp, PSIZE, p, rc);
1483 discard_metapage(rmp);
1487 BT_MARK_DIRTY(mp, ip);
1489 * acquire a transaction lock on the next page
1491 tlck = txLock(tid, ip, mp, tlckDTREE | tlckRELINK);
1492 jfs_info("dtSplitPage: tlck = 0x%p, ip = 0x%p, mp=0x%p",
1494 dtlck = (struct dt_lock *) & tlck->lock;
1496 /* linelock header of previous right sibling page */
1497 lv = & dtlck->lv[dtlck->index];
1502 p->header.prev = cpu_to_le64(rbn);
1508 * split the data between the split and right pages.
1510 skip = split->index;
1511 half = (PSIZE >> L2DTSLOTSIZE) >> 1; /* swag */
1515 * compute fill factor for split pages
1517 * <nxt> traces the next entry to move to rp
1518 * <off> traces the next entry to stay in sp
1520 stbl = (u8 *) & sp->slot[sp->header.stblindex];
1521 nextindex = sp->header.nextindex;
1522 for (nxt = off = 0; nxt < nextindex; ++off) {
1524 /* check for fill factor with new entry size */
1528 switch (sp->header.flag & BT_TYPE) {
1530 ldtentry = (struct ldtentry *) & sp->slot[si];
1532 n = NDTLEAF(ldtentry->namlen);
1534 n = NDTLEAF_LEGACY(ldtentry->
1539 idtentry = (struct idtentry *) & sp->slot[si];
1540 n = NDTINTERNAL(idtentry->namlen);
1547 ++nxt; /* advance to next entry to move in sp */
1555 /* <nxt> poins to the 1st entry to move */
1558 * move entries to right page
1560 * dtMoveEntry() initializes rp and reserves entry for insertion
1562 * split page moved out entries are linelocked;
1563 * new/right page moved in entries are linelocked;
1565 /* linelock header + stbl of new right page */
1566 rlv = & rdtlck->lv[rdtlck->index];
1571 dtMoveEntry(sp, nxt, rp, &sdtlck, &rdtlck, DO_INDEX(ip));
1573 sp->header.nextindex = nxt;
1576 * finalize freelist of new right page
1578 fsi = rp->header.freelist;
1580 for (fsi++; fsi < rp->header.maxslot; f++, fsi++)
1585 * Update directory index table for entries now in right page
1587 if ((rp->header.flag & BT_LEAF) && DO_INDEX(ip)) {
1591 stbl = DT_GETSTBL(rp);
1592 for (n = 0; n < rp->header.nextindex; n++) {
1593 ldtentry = (struct ldtentry *) & rp->slot[stbl[n]];
1594 modify_index(tid, ip, le32_to_cpu(ldtentry->index),
1595 rbn, n, &mp, &lblock);
1598 release_metapage(mp);
1602 * the skipped index was on the left page,
1605 /* insert the new entry in the split page */
1606 dtInsertEntry(sp, skip, split->key, split->data, &sdtlck);
1608 /* linelock stbl of split page */
1609 if (sdtlck->index >= sdtlck->maxcnt)
1610 sdtlck = (struct dt_lock *) txLinelock(sdtlck);
1611 slv = & sdtlck->lv[sdtlck->index];
1612 n = skip >> L2DTSLOTSIZE;
1613 slv->offset = sp->header.stblindex + n;
1615 ((sp->header.nextindex - 1) >> L2DTSLOTSIZE) - n + 1;
1619 * the skipped index was on the right page,
1622 /* adjust the skip index to reflect the new position */
1625 /* insert the new entry in the right page */
1626 dtInsertEntry(rp, skip, split->key, split->data, &rdtlck);
1640 * function: extend 1st/only directory leaf page
1644 * return: 0 - success;
1646 * return extended page pinned;
1648 static int dtExtendPage(tid_t tid,
1649 struct inode *ip, struct dtsplit * split, struct btstack * btstack)
1651 struct super_block *sb = ip->i_sb;
1653 struct metapage *smp, *pmp, *mp;
1655 struct pxdlist *pxdlist;
1658 int newstblindex, newstblsize;
1659 int oldstblindex, oldstblsize;
1662 struct btframe *parent;
1664 struct dt_lock *dtlck;
1667 struct pxd_lock *pxdlock;
1670 struct ldtentry *ldtentry;
1673 /* get page to extend */
1675 sp = DT_PAGE(ip, smp);
1677 /* get parent/root page */
1678 parent = BT_POP(btstack);
1679 DT_GETPAGE(ip, parent->bn, pmp, PSIZE, pp, rc);
1686 pxdlist = split->pxdlist;
1687 pxd = &pxdlist->pxd[pxdlist->npxd];
1690 xaddr = addressPXD(pxd);
1691 tpxd = &sp->header.self;
1692 txaddr = addressPXD(tpxd);
1693 /* in-place extension */
1694 if (xaddr == txaddr) {
1701 /* save moved extent descriptor for later free */
1702 tlck = txMaplock(tid, ip, tlckDTREE | tlckRELOCATE);
1703 pxdlock = (struct pxd_lock *) & tlck->lock;
1704 pxdlock->flag = mlckFREEPXD;
1705 pxdlock->pxd = sp->header.self;
1709 * Update directory index table to reflect new page address
1715 stbl = DT_GETSTBL(sp);
1716 for (n = 0; n < sp->header.nextindex; n++) {
1718 (struct ldtentry *) & sp->slot[stbl[n]];
1719 modify_index(tid, ip,
1720 le32_to_cpu(ldtentry->index),
1721 xaddr, n, &mp, &lblock);
1724 release_metapage(mp);
1731 sp->header.self = *pxd;
1733 jfs_info("dtExtendPage: ip:0x%p smp:0x%p sp:0x%p", ip, smp, sp);
1735 BT_MARK_DIRTY(smp, ip);
1737 * acquire a transaction lock on the extended/leaf page
1739 tlck = txLock(tid, ip, smp, tlckDTREE | type);
1740 dtlck = (struct dt_lock *) & tlck->lock;
1741 lv = & dtlck->lv[0];
1743 /* update buffer extent descriptor of extended page */
1744 xlen = lengthPXD(pxd);
1745 xsize = xlen << JFS_SBI(sb)->l2bsize;
1748 * copy old stbl to new stbl at start of extended area
1750 oldstblindex = sp->header.stblindex;
1751 oldstblsize = (sp->header.maxslot + 31) >> L2DTSLOTSIZE;
1752 newstblindex = sp->header.maxslot;
1753 n = xsize >> L2DTSLOTSIZE;
1754 newstblsize = (n + 31) >> L2DTSLOTSIZE;
1755 memcpy(&sp->slot[newstblindex], &sp->slot[oldstblindex],
1756 sp->header.nextindex);
1759 * in-line extension: linelock old area of extended page
1761 if (type == tlckEXTEND) {
1762 /* linelock header */
1768 /* linelock new stbl of extended page */
1769 lv->offset = newstblindex;
1770 lv->length = newstblsize;
1773 * relocation: linelock whole relocated area
1777 lv->length = sp->header.maxslot + newstblsize;
1782 sp->header.maxslot = n;
1783 sp->header.stblindex = newstblindex;
1784 /* sp->header.nextindex remains the same */
1787 * add old stbl region at head of freelist
1791 last = sp->header.freelist;
1792 for (n = 0; n < oldstblsize; n++, fsi++, f++) {
1796 sp->header.freelist = last;
1797 sp->header.freecnt += oldstblsize;
1800 * append free region of newly extended area at tail of freelist
1802 /* init free region of newly extended area */
1803 fsi = n = newstblindex + newstblsize;
1805 for (fsi++; fsi < sp->header.maxslot; f++, fsi++)
1809 /* append new free region at tail of old freelist */
1810 fsi = sp->header.freelist;
1812 sp->header.freelist = n;
1817 } while (fsi != -1);
1822 sp->header.freecnt += sp->header.maxslot - n;
1825 * insert the new entry
1827 dtInsertEntry(sp, split->index, split->key, split->data, &dtlck);
1829 BT_MARK_DIRTY(pmp, ip);
1831 * linelock any freeslots residing in old extent
1833 if (type == tlckEXTEND) {
1834 n = sp->header.maxslot >> 2;
1835 if (sp->header.freelist < n)
1836 dtLinelockFreelist(sp, n, &dtlck);
1840 * update parent entry on the parent/root page
1843 * acquire a transaction lock on the parent/root page
1845 tlck = txLock(tid, ip, pmp, tlckDTREE | tlckENTRY);
1846 dtlck = (struct dt_lock *) & tlck->lock;
1847 lv = & dtlck->lv[dtlck->index];
1849 /* linelock parent entry - 1st slot */
1854 /* update the parent pxd for page extension */
1855 tpxd = (pxd_t *) & pp->slot[1];
1867 * split the full root page into
1868 * original/root/split page and new right page
1869 * i.e., root remains fixed in tree anchor (inode) and
1870 * the root is copied to a single new right child page
1871 * since root page << non-root page, and
1872 * the split root page contains a single entry for the
1873 * new right child page.
1877 * return: 0 - success;
1879 * return new page pinned;
1881 static int dtSplitRoot(tid_t tid,
1882 struct inode *ip, struct dtsplit * split, struct metapage ** rmpp)
1884 struct super_block *sb = ip->i_sb;
1885 struct metapage *smp;
1887 struct metapage *rmp;
1894 int fsi, stblsize, n;
1897 struct pxdlist *pxdlist;
1899 struct dt_lock *dtlck;
1904 /* get split root page */
1906 sp = &JFS_IP(ip)->i_dtroot;
1909 * allocate/initialize a single (right) child page
1911 * N.B. at first split, a one (or two) block to fit new entry
1912 * is allocated; at subsequent split, a full page is allocated;
1914 pxdlist = split->pxdlist;
1915 pxd = &pxdlist->pxd[pxdlist->npxd];
1917 rbn = addressPXD(pxd);
1918 xlen = lengthPXD(pxd);
1919 xsize = xlen << JFS_SBI(sb)->l2bsize;
1920 rmp = get_metapage(ip, rbn, xsize, 1);
1926 /* Allocate blocks to quota. */
1927 rc = dquot_alloc_block(ip, lengthPXD(pxd));
1929 release_metapage(rmp);
1933 BT_MARK_DIRTY(rmp, ip);
1935 * acquire a transaction lock on the new right page
1937 tlck = txLock(tid, ip, rmp, tlckDTREE | tlckNEW);
1938 dtlck = (struct dt_lock *) & tlck->lock;
1941 (sp->header.flag & BT_LEAF) ? BT_LEAF : BT_INTERNAL;
1942 rp->header.self = *pxd;
1944 /* initialize sibling pointers */
1945 rp->header.next = 0;
1946 rp->header.prev = 0;
1949 * move in-line root page into new right page extent
1951 /* linelock header + copied entries + new stbl (1st slot) in new page */
1952 ASSERT(dtlck->index == 0);
1953 lv = & dtlck->lv[0];
1955 lv->length = 10; /* 1 + 8 + 1 */
1958 n = xsize >> L2DTSLOTSIZE;
1959 rp->header.maxslot = n;
1960 stblsize = (n + 31) >> L2DTSLOTSIZE;
1962 /* copy old stbl to new stbl at start of extended area */
1963 rp->header.stblindex = DTROOTMAXSLOT;
1964 stbl = (s8 *) & rp->slot[DTROOTMAXSLOT];
1965 memcpy(stbl, sp->header.stbl, sp->header.nextindex);
1966 rp->header.nextindex = sp->header.nextindex;
1968 /* copy old data area to start of new data area */
1969 memcpy(&rp->slot[1], &sp->slot[1], IDATASIZE);
1972 * append free region of newly extended area at tail of freelist
1974 /* init free region of newly extended area */
1975 fsi = n = DTROOTMAXSLOT + stblsize;
1977 for (fsi++; fsi < rp->header.maxslot; f++, fsi++)
1981 /* append new free region at tail of old freelist */
1982 fsi = sp->header.freelist;
1984 rp->header.freelist = n;
1986 rp->header.freelist = fsi;
1996 rp->header.freecnt = sp->header.freecnt + rp->header.maxslot - n;
1999 * Update directory index table for entries now in right page
2001 if ((rp->header.flag & BT_LEAF) && DO_INDEX(ip)) {
2003 struct metapage *mp = NULL;
2004 struct ldtentry *ldtentry;
2006 stbl = DT_GETSTBL(rp);
2007 for (n = 0; n < rp->header.nextindex; n++) {
2008 ldtentry = (struct ldtentry *) & rp->slot[stbl[n]];
2009 modify_index(tid, ip, le32_to_cpu(ldtentry->index),
2010 rbn, n, &mp, &lblock);
2013 release_metapage(mp);
2016 * insert the new entry into the new right/child page
2017 * (skip index in the new right page will not change)
2019 dtInsertEntry(rp, split->index, split->key, split->data, &dtlck);
2022 * reset parent/root page
2024 * set the 1st entry offset to 0, which force the left-most key
2025 * at any level of the tree to be less than any search key.
2027 * The btree comparison code guarantees that the left-most key on any
2028 * level of the tree is never used, so it doesn't need to be filled in.
2030 BT_MARK_DIRTY(smp, ip);
2032 * acquire a transaction lock on the root page (in-memory inode)
2034 tlck = txLock(tid, ip, smp, tlckDTREE | tlckNEW | tlckBTROOT);
2035 dtlck = (struct dt_lock *) & tlck->lock;
2038 ASSERT(dtlck->index == 0);
2039 lv = & dtlck->lv[0];
2041 lv->length = DTROOTMAXSLOT;
2044 /* update page header of root */
2045 if (sp->header.flag & BT_LEAF) {
2046 sp->header.flag &= ~BT_LEAF;
2047 sp->header.flag |= BT_INTERNAL;
2050 /* init the first entry */
2051 s = (struct idtentry *) & sp->slot[DTENTRYSTART];
2057 stbl = sp->header.stbl;
2058 stbl[0] = DTENTRYSTART;
2059 sp->header.nextindex = 1;
2062 fsi = DTENTRYSTART + 1;
2065 /* init free region of remaining area */
2066 for (fsi++; fsi < DTROOTMAXSLOT; f++, fsi++)
2070 sp->header.freelist = DTENTRYSTART + 1;
2071 sp->header.freecnt = DTROOTMAXSLOT - (DTENTRYSTART + 1);
2082 * function: delete the entry(s) referenced by a key.
2088 int dtDelete(tid_t tid,
2089 struct inode *ip, struct component_name * key, ino_t * ino, int flag)
2093 struct metapage *mp, *imp;
2096 struct btstack btstack;
2097 struct dt_lock *dtlck;
2101 struct ldtentry *ldtentry;
2103 u32 table_index, next_index;
2104 struct metapage *nmp;
2108 * search for the entry to delete:
2110 * dtSearch() returns (leaf page pinned, index at which to delete).
2112 if ((rc = dtSearch(ip, key, ino, &btstack, flag)))
2115 /* retrieve search result */
2116 DT_GETSEARCH(ip, btstack.top, bn, mp, p, index);
2119 * We need to find put the index of the next entry into the
2120 * directory index table in order to resume a readdir from this
2124 stbl = DT_GETSTBL(p);
2125 ldtentry = (struct ldtentry *) & p->slot[stbl[index]];
2126 table_index = le32_to_cpu(ldtentry->index);
2127 if (index == (p->header.nextindex - 1)) {
2129 * Last entry in this leaf page
2131 if ((p->header.flag & BT_ROOT)
2132 || (p->header.next == 0))
2135 /* Read next leaf page */
2136 DT_GETPAGE(ip, le64_to_cpu(p->header.next),
2137 nmp, PSIZE, np, rc);
2141 stbl = DT_GETSTBL(np);
2143 (struct ldtentry *) & np->
2146 le32_to_cpu(ldtentry->index);
2152 (struct ldtentry *) & p->slot[stbl[index + 1]];
2153 next_index = le32_to_cpu(ldtentry->index);
2155 free_index(tid, ip, table_index, next_index);
2158 * the leaf page becomes empty, delete the page
2160 if (p->header.nextindex == 1) {
2161 /* delete empty page */
2162 rc = dtDeleteUp(tid, ip, mp, p, &btstack);
2165 * the leaf page has other entries remaining:
2167 * delete the entry from the leaf page.
2170 BT_MARK_DIRTY(mp, ip);
2172 * acquire a transaction lock on the leaf page
2174 tlck = txLock(tid, ip, mp, tlckDTREE | tlckENTRY);
2175 dtlck = (struct dt_lock *) & tlck->lock;
2178 * Do not assume that dtlck->index will be zero. During a
2179 * rename within a directory, this transaction may have
2180 * modified this page already when adding the new entry.
2183 /* linelock header */
2184 if (dtlck->index >= dtlck->maxcnt)
2185 dtlck = (struct dt_lock *) txLinelock(dtlck);
2186 lv = & dtlck->lv[dtlck->index];
2191 /* linelock stbl of non-root leaf page */
2192 if (!(p->header.flag & BT_ROOT)) {
2193 if (dtlck->index >= dtlck->maxcnt)
2194 dtlck = (struct dt_lock *) txLinelock(dtlck);
2195 lv = & dtlck->lv[dtlck->index];
2196 i = index >> L2DTSLOTSIZE;
2197 lv->offset = p->header.stblindex + i;
2199 ((p->header.nextindex - 1) >> L2DTSLOTSIZE) -
2204 /* free the leaf entry */
2205 dtDeleteEntry(p, index, &dtlck);
2208 * Update directory index table for entries moved in stbl
2210 if (DO_INDEX(ip) && index < p->header.nextindex) {
2214 stbl = DT_GETSTBL(p);
2215 for (i = index; i < p->header.nextindex; i++) {
2217 (struct ldtentry *) & p->slot[stbl[i]];
2218 modify_index(tid, ip,
2219 le32_to_cpu(ldtentry->index),
2220 bn, i, &imp, &lblock);
2223 release_metapage(imp);
2237 * free empty pages as propagating deletion up the tree
2243 static int dtDeleteUp(tid_t tid, struct inode *ip,
2244 struct metapage * fmp, dtpage_t * fp, struct btstack * btstack)
2247 struct metapage *mp;
2249 int index, nextindex;
2251 struct btframe *parent;
2252 struct dt_lock *dtlck;
2255 struct pxd_lock *pxdlock;
2259 * keep the root leaf page which has become empty
2261 if (BT_IS_ROOT(fmp)) {
2265 * dtInitRoot() acquires txlock on the root
2267 dtInitRoot(tid, ip, PARENT(ip));
2275 * free the non-root leaf page
2278 * acquire a transaction lock on the page
2280 * write FREEXTENT|NOREDOPAGE log record
2281 * N.B. linelock is overlaid as freed extent descriptor, and
2282 * the buffer page is freed;
2284 tlck = txMaplock(tid, ip, tlckDTREE | tlckFREE);
2285 pxdlock = (struct pxd_lock *) & tlck->lock;
2286 pxdlock->flag = mlckFREEPXD;
2287 pxdlock->pxd = fp->header.self;
2290 /* update sibling pointers */
2291 if ((rc = dtRelink(tid, ip, fp))) {
2296 xlen = lengthPXD(&fp->header.self);
2298 /* Free quota allocation. */
2299 dquot_free_block(ip, xlen);
2301 /* free/invalidate its buffer page */
2302 discard_metapage(fmp);
2305 * propagate page deletion up the directory tree
2307 * If the delete from the parent page makes it empty,
2308 * continue all the way up the tree.
2309 * stop if the root page is reached (which is never deleted) or
2310 * if the entry deletion does not empty the page.
2312 while ((parent = BT_POP(btstack)) != NULL) {
2313 /* pin the parent page <sp> */
2314 DT_GETPAGE(ip, parent->bn, mp, PSIZE, p, rc);
2319 * free the extent of the child page deleted
2321 index = parent->index;
2324 * delete the entry for the child page from parent
2326 nextindex = p->header.nextindex;
2329 * the parent has the single entry being deleted:
2331 * free the parent page which has become empty.
2333 if (nextindex == 1) {
2335 * keep the root internal page which has become empty
2337 if (p->header.flag & BT_ROOT) {
2341 * dtInitRoot() acquires txlock on the root
2343 dtInitRoot(tid, ip, PARENT(ip));
2350 * free the parent page
2354 * acquire a transaction lock on the page
2356 * write FREEXTENT|NOREDOPAGE log record
2360 tlckDTREE | tlckFREE);
2361 pxdlock = (struct pxd_lock *) & tlck->lock;
2362 pxdlock->flag = mlckFREEPXD;
2363 pxdlock->pxd = p->header.self;
2366 /* update sibling pointers */
2367 if ((rc = dtRelink(tid, ip, p))) {
2372 xlen = lengthPXD(&p->header.self);
2374 /* Free quota allocation */
2375 dquot_free_block(ip, xlen);
2377 /* free/invalidate its buffer page */
2378 discard_metapage(mp);
2386 * the parent has other entries remaining:
2388 * delete the router entry from the parent page.
2390 BT_MARK_DIRTY(mp, ip);
2392 * acquire a transaction lock on the page
2394 * action: router entry deletion
2396 tlck = txLock(tid, ip, mp, tlckDTREE | tlckENTRY);
2397 dtlck = (struct dt_lock *) & tlck->lock;
2399 /* linelock header */
2400 if (dtlck->index >= dtlck->maxcnt)
2401 dtlck = (struct dt_lock *) txLinelock(dtlck);
2402 lv = & dtlck->lv[dtlck->index];
2407 /* linelock stbl of non-root leaf page */
2408 if (!(p->header.flag & BT_ROOT)) {
2409 if (dtlck->index < dtlck->maxcnt)
2412 dtlck = (struct dt_lock *) txLinelock(dtlck);
2413 lv = & dtlck->lv[0];
2415 i = index >> L2DTSLOTSIZE;
2416 lv->offset = p->header.stblindex + i;
2418 ((p->header.nextindex - 1) >> L2DTSLOTSIZE) -
2423 /* free the router entry */
2424 dtDeleteEntry(p, index, &dtlck);
2426 /* reset key of new leftmost entry of level (for consistency) */
2428 ((p->header.flag & BT_ROOT) || p->header.prev == 0))
2429 dtTruncateEntry(p, 0, &dtlck);
2431 /* unpin the parent page */
2434 /* exit propagation up */
2439 ip->i_size -= PSIZE;
2446 * NAME: dtRelocate()
2448 * FUNCTION: relocate dtpage (internal or leaf) of directory;
2449 * This function is mainly used by defragfs utility.
2451 int dtRelocate(tid_t tid, struct inode *ip, s64 lmxaddr, pxd_t * opxd,
2455 struct metapage *mp, *pmp, *lmp, *rmp;
2456 dtpage_t *p, *pp, *rp = 0, *lp= 0;
2459 struct btstack btstack;
2461 s64 oxaddr, nextbn, prevbn;
2464 struct dt_lock *dtlck;
2465 struct pxd_lock *pxdlock;
2469 oxaddr = addressPXD(opxd);
2470 xlen = lengthPXD(opxd);
2472 jfs_info("dtRelocate: lmxaddr:%Ld xaddr:%Ld:%Ld xlen:%d",
2473 (long long)lmxaddr, (long long)oxaddr, (long long)nxaddr,
2477 * 1. get the internal parent dtpage covering
2478 * router entry for the tartget page to be relocated;
2480 rc = dtSearchNode(ip, lmxaddr, opxd, &btstack);
2484 /* retrieve search result */
2485 DT_GETSEARCH(ip, btstack.top, bn, pmp, pp, index);
2486 jfs_info("dtRelocate: parent router entry validated.");
2489 * 2. relocate the target dtpage
2491 /* read in the target page from src extent */
2492 DT_GETPAGE(ip, oxaddr, mp, PSIZE, p, rc);
2494 /* release the pinned parent page */
2500 * read in sibling pages if any to update sibling pointers;
2503 if (p->header.next) {
2504 nextbn = le64_to_cpu(p->header.next);
2505 DT_GETPAGE(ip, nextbn, rmp, PSIZE, rp, rc);
2514 if (p->header.prev) {
2515 prevbn = le64_to_cpu(p->header.prev);
2516 DT_GETPAGE(ip, prevbn, lmp, PSIZE, lp, rc);
2526 /* at this point, all xtpages to be updated are in memory */
2529 * update sibling pointers of sibling dtpages if any;
2532 tlck = txLock(tid, ip, lmp, tlckDTREE | tlckRELINK);
2533 dtlck = (struct dt_lock *) & tlck->lock;
2534 /* linelock header */
2535 ASSERT(dtlck->index == 0);
2536 lv = & dtlck->lv[0];
2541 lp->header.next = cpu_to_le64(nxaddr);
2546 tlck = txLock(tid, ip, rmp, tlckDTREE | tlckRELINK);
2547 dtlck = (struct dt_lock *) & tlck->lock;
2548 /* linelock header */
2549 ASSERT(dtlck->index == 0);
2550 lv = & dtlck->lv[0];
2555 rp->header.prev = cpu_to_le64(nxaddr);
2560 * update the target dtpage to be relocated
2562 * write LOG_REDOPAGE of LOG_NEW type for dst page
2563 * for the whole target page (logredo() will apply
2564 * after image and update bmap for allocation of the
2565 * dst extent), and update bmap for allocation of
2568 tlck = txLock(tid, ip, mp, tlckDTREE | tlckNEW);
2569 dtlck = (struct dt_lock *) & tlck->lock;
2570 /* linelock header */
2571 ASSERT(dtlck->index == 0);
2572 lv = & dtlck->lv[0];
2574 /* update the self address in the dtpage header */
2575 pxd = &p->header.self;
2576 PXDaddress(pxd, nxaddr);
2578 /* the dst page is the same as the src page, i.e.,
2579 * linelock for afterimage of the whole page;
2582 lv->length = p->header.maxslot;
2585 /* update the buffer extent descriptor of the dtpage */
2586 xsize = xlen << JFS_SBI(ip->i_sb)->l2bsize;
2588 /* unpin the relocated page */
2590 jfs_info("dtRelocate: target dtpage relocated.");
2592 /* the moved extent is dtpage, then a LOG_NOREDOPAGE log rec
2593 * needs to be written (in logredo(), the LOG_NOREDOPAGE log rec
2594 * will also force a bmap update ).
2598 * 3. acquire maplock for the source extent to be freed;
2600 /* for dtpage relocation, write a LOG_NOREDOPAGE record
2601 * for the source dtpage (logredo() will init NoRedoPage
2602 * filter and will also update bmap for free of the source
2603 * dtpage), and upadte bmap for free of the source dtpage;
2605 tlck = txMaplock(tid, ip, tlckDTREE | tlckFREE);
2606 pxdlock = (struct pxd_lock *) & tlck->lock;
2607 pxdlock->flag = mlckFREEPXD;
2608 PXDaddress(&pxdlock->pxd, oxaddr);
2609 PXDlength(&pxdlock->pxd, xlen);
2613 * 4. update the parent router entry for relocation;
2615 * acquire tlck for the parent entry covering the target dtpage;
2616 * write LOG_REDOPAGE to apply after image only;
2618 jfs_info("dtRelocate: update parent router entry.");
2619 tlck = txLock(tid, ip, pmp, tlckDTREE | tlckENTRY);
2620 dtlck = (struct dt_lock *) & tlck->lock;
2621 lv = & dtlck->lv[dtlck->index];
2623 /* update the PXD with the new address */
2624 stbl = DT_GETSTBL(pp);
2625 pxd = (pxd_t *) & pp->slot[stbl[index]];
2626 PXDaddress(pxd, nxaddr);
2627 lv->offset = stbl[index];
2631 /* unpin the parent dtpage */
2638 * NAME: dtSearchNode()
2640 * FUNCTION: Search for an dtpage containing a specified address
2641 * This function is mainly used by defragfs utility.
2643 * NOTE: Search result on stack, the found page is pinned at exit.
2644 * The result page must be an internal dtpage.
2645 * lmxaddr give the address of the left most page of the
2646 * dtree level, in which the required dtpage resides.
2648 static int dtSearchNode(struct inode *ip, s64 lmxaddr, pxd_t * kpxd,
2649 struct btstack * btstack)
2653 struct metapage *mp;
2655 int psize = 288; /* initial in-line directory */
2659 struct btframe *btsp;
2661 BT_CLR(btstack); /* reset stack */
2664 * descend tree to the level with specified leftmost page
2666 * by convention, root bn = 0.
2669 /* get/pin the page to search */
2670 DT_GETPAGE(ip, bn, mp, psize, p, rc);
2674 /* does the xaddr of leftmost page of the levevl
2675 * matches levevl search key ?
2677 if (p->header.flag & BT_ROOT) {
2680 } else if (addressPXD(&p->header.self) == lmxaddr)
2684 * descend down to leftmost child page
2686 if (p->header.flag & BT_LEAF) {
2691 /* get the leftmost entry */
2692 stbl = DT_GETSTBL(p);
2693 pxd = (pxd_t *) & p->slot[stbl[0]];
2695 /* get the child page block address */
2696 bn = addressPXD(pxd);
2697 psize = lengthPXD(pxd) << JFS_SBI(ip->i_sb)->l2bsize;
2698 /* unpin the parent page */
2703 * search each page at the current levevl
2706 stbl = DT_GETSTBL(p);
2707 for (i = 0; i < p->header.nextindex; i++) {
2708 pxd = (pxd_t *) & p->slot[stbl[i]];
2710 /* found the specified router entry */
2711 if (addressPXD(pxd) == addressPXD(kpxd) &&
2712 lengthPXD(pxd) == lengthPXD(kpxd)) {
2713 btsp = btstack->top;
2722 /* get the right sibling page if any */
2724 bn = le64_to_cpu(p->header.next);
2730 /* unpin current page */
2733 /* get the right sibling page */
2734 DT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
2740 #endif /* _NOTYET */
2746 * link around a freed page.
2749 * fp: page to be freed
2753 static int dtRelink(tid_t tid, struct inode *ip, dtpage_t * p)
2756 struct metapage *mp;
2759 struct dt_lock *dtlck;
2762 nextbn = le64_to_cpu(p->header.next);
2763 prevbn = le64_to_cpu(p->header.prev);
2765 /* update prev pointer of the next page */
2767 DT_GETPAGE(ip, nextbn, mp, PSIZE, p, rc);
2771 BT_MARK_DIRTY(mp, ip);
2773 * acquire a transaction lock on the next page
2775 * action: update prev pointer;
2777 tlck = txLock(tid, ip, mp, tlckDTREE | tlckRELINK);
2778 jfs_info("dtRelink nextbn: tlck = 0x%p, ip = 0x%p, mp=0x%p",
2780 dtlck = (struct dt_lock *) & tlck->lock;
2782 /* linelock header */
2783 if (dtlck->index >= dtlck->maxcnt)
2784 dtlck = (struct dt_lock *) txLinelock(dtlck);
2785 lv = & dtlck->lv[dtlck->index];
2790 p->header.prev = cpu_to_le64(prevbn);
2794 /* update next pointer of the previous page */
2796 DT_GETPAGE(ip, prevbn, mp, PSIZE, p, rc);
2800 BT_MARK_DIRTY(mp, ip);
2802 * acquire a transaction lock on the prev page
2804 * action: update next pointer;
2806 tlck = txLock(tid, ip, mp, tlckDTREE | tlckRELINK);
2807 jfs_info("dtRelink prevbn: tlck = 0x%p, ip = 0x%p, mp=0x%p",
2809 dtlck = (struct dt_lock *) & tlck->lock;
2811 /* linelock header */
2812 if (dtlck->index >= dtlck->maxcnt)
2813 dtlck = (struct dt_lock *) txLinelock(dtlck);
2814 lv = & dtlck->lv[dtlck->index];
2819 p->header.next = cpu_to_le64(nextbn);
2830 * initialize directory root (inline in inode)
2832 void dtInitRoot(tid_t tid, struct inode *ip, u32 idotdot)
2834 struct jfs_inode_info *jfs_ip = JFS_IP(ip);
2839 struct dt_lock *dtlck;
2844 * If this was previously an non-empty directory, we need to remove
2845 * the old directory table.
2848 if (!jfs_dirtable_inline(ip)) {
2849 struct tblock *tblk = tid_to_tblock(tid);
2851 * We're playing games with the tid's xflag. If
2852 * we're removing a regular file, the file's xtree
2853 * is committed with COMMIT_PMAP, but we always
2854 * commit the directories xtree with COMMIT_PWMAP.
2856 xflag_save = tblk->xflag;
2859 * xtTruncate isn't guaranteed to fully truncate
2860 * the xtree. The caller needs to check i_size
2861 * after committing the transaction to see if
2862 * additional truncation is needed. The
2863 * COMMIT_Stale flag tells caller that we
2864 * initiated the truncation.
2866 xtTruncate(tid, ip, 0, COMMIT_PWMAP);
2867 set_cflag(COMMIT_Stale, ip);
2869 tblk->xflag = xflag_save;
2873 jfs_ip->next_index = 2;
2875 ip->i_size = IDATASIZE;
2878 * acquire a transaction lock on the root
2880 * action: directory initialization;
2882 tlck = txLock(tid, ip, (struct metapage *) & jfs_ip->bxflag,
2883 tlckDTREE | tlckENTRY | tlckBTROOT);
2884 dtlck = (struct dt_lock *) & tlck->lock;
2887 ASSERT(dtlck->index == 0);
2888 lv = & dtlck->lv[0];
2890 lv->length = DTROOTMAXSLOT;
2893 p = &jfs_ip->i_dtroot;
2895 p->header.flag = DXD_INDEX | BT_ROOT | BT_LEAF;
2897 p->header.nextindex = 0;
2903 /* init data area of root */
2904 for (fsi++; fsi < DTROOTMAXSLOT; f++, fsi++)
2908 p->header.freelist = 1;
2909 p->header.freecnt = 8;
2911 /* init '..' entry */
2912 p->header.idotdot = cpu_to_le32(idotdot);
2918 * add_missing_indices()
2920 * function: Fix dtree page in which one or more entries has an invalid index.
2921 * fsck.jfs should really fix this, but it currently does not.
2922 * Called from jfs_readdir when bad index is detected.
2924 static void add_missing_indices(struct inode *inode, s64 bn)
2927 struct dt_lock *dtlck;
2931 struct metapage *mp;
2938 tid = txBegin(inode->i_sb, 0);
2940 DT_GETPAGE(inode, bn, mp, PSIZE, p, rc);
2943 printk(KERN_ERR "DT_GETPAGE failed!\n");
2946 BT_MARK_DIRTY(mp, inode);
2948 ASSERT(p->header.flag & BT_LEAF);
2950 tlck = txLock(tid, inode, mp, tlckDTREE | tlckENTRY);
2952 tlck->type |= tlckBTROOT;
2954 dtlck = (struct dt_lock *) &tlck->lock;
2956 stbl = DT_GETSTBL(p);
2957 for (i = 0; i < p->header.nextindex; i++) {
2958 d = (struct ldtentry *) &p->slot[stbl[i]];
2959 index = le32_to_cpu(d->index);
2960 if ((index < 2) || (index >= JFS_IP(inode)->next_index)) {
2961 d->index = cpu_to_le32(add_index(tid, inode, bn, i));
2962 if (dtlck->index >= dtlck->maxcnt)
2963 dtlck = (struct dt_lock *) txLinelock(dtlck);
2964 lv = &dtlck->lv[dtlck->index];
2965 lv->offset = stbl[i];
2972 (void) txCommit(tid, 1, &inode, 0);
2978 * Buffer to hold directory entry info while traversing a dtree page
2979 * before being fed to the filldir function
2989 * function to determine next variable-sized jfs_dirent in buffer
2991 static inline struct jfs_dirent *next_jfs_dirent(struct jfs_dirent *dirent)
2993 return (struct jfs_dirent *)
2995 ((sizeof (struct jfs_dirent) + dirent->name_len + 1 +
2996 sizeof (loff_t) - 1) &
2997 ~(sizeof (loff_t) - 1)));
3003 * function: read directory entries sequentially
3004 * from the specified entry offset
3008 * return: offset = (pn, index) of start entry
3009 * of next jfs_readdir()/dtRead()
3011 int jfs_readdir(struct file *file, struct dir_context *ctx)
3013 struct inode *ip = file_inode(file);
3014 struct nls_table *codepage = JFS_SBI(ip->i_sb)->nls_tab;
3016 loff_t dtpos; /* legacy OS/2 style position */
3021 } *dtoffset = (struct dtoffset *) &dtpos;
3023 struct metapage *mp;
3027 struct btstack btstack;
3031 int d_namleft, len, outlen;
3032 unsigned long dirent_buf;
3036 uint loop_count = 0;
3037 struct jfs_dirent *jfs_dirent;
3039 int overflow, fix_page, page_fixed = 0;
3040 static int unique_pos = 2; /* If we can't fix broken index */
3042 if (ctx->pos == DIREND)
3047 * persistent index is stored in directory entries.
3048 * Special cases: 0 = .
3050 * -1 = End of directory
3054 dir_index = (u32) ctx->pos;
3057 * NFSv4 reserves cookies 1 and 2 for . and .. so the value
3058 * we return to the vfs is one greater than the one we use
3064 if (dir_index > 1) {
3065 struct dir_table_slot dirtab_slot;
3068 (dir_index >= JFS_IP(ip)->next_index)) {
3069 /* Stale position. Directory has shrunk */
3074 rc = read_index(ip, dir_index, &dirtab_slot);
3079 if (dirtab_slot.flag == DIR_INDEX_FREE) {
3080 if (loop_count++ > JFS_IP(ip)->next_index) {
3081 jfs_err("jfs_readdir detected infinite loop!");
3085 dir_index = le32_to_cpu(dirtab_slot.addr2);
3086 if (dir_index == -1) {
3092 bn = addressDTS(&dirtab_slot);
3093 index = dirtab_slot.slot;
3094 DT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
3099 if (p->header.flag & BT_INTERNAL) {
3100 jfs_err("jfs_readdir: bad index table");
3106 if (dir_index == 0) {
3111 if (!dir_emit(ctx, ".", 1, ip->i_ino, DT_DIR))
3118 if (!dir_emit(ctx, "..", 2, PARENT(ip), DT_DIR))
3122 * Find first entry of left-most leaf
3129 if ((rc = dtReadFirst(ip, &btstack)))
3132 DT_GETSEARCH(ip, btstack.top, bn, mp, p, index);
3136 * Legacy filesystem - OS/2 & Linux JFS < 0.3.6
3138 * pn = 0; index = 1: First entry "."
3139 * pn = 0; index = 2: Second entry ".."
3140 * pn > 0: Real entries, pn=1 -> leftmost page
3141 * pn = index = -1: No more entries
3145 /* build "." entry */
3147 if (!dir_emit(ctx, ".", 1, ip->i_ino, DT_DIR))
3149 dtoffset->index = 2;
3153 if (dtoffset->pn == 0) {
3154 if (dtoffset->index == 2) {
3155 /* build ".." entry */
3156 if (!dir_emit(ctx, "..", 2, PARENT(ip), DT_DIR))
3159 jfs_err("jfs_readdir called with invalid offset!");
3162 dtoffset->index = 0;
3171 if ((rc = dtReadNext(ip, &ctx->pos, &btstack))) {
3172 jfs_err("jfs_readdir: unexpected rc = %d from dtReadNext",
3177 /* get start leaf page and index */
3178 DT_GETSEARCH(ip, btstack.top, bn, mp, p, index);
3180 /* offset beyond directory eof ? */
3187 dirent_buf = __get_free_page(GFP_KERNEL);
3188 if (dirent_buf == 0) {
3190 jfs_warn("jfs_readdir: __get_free_page failed!");
3196 jfs_dirent = (struct jfs_dirent *) dirent_buf;
3198 overflow = fix_page = 0;
3200 stbl = DT_GETSTBL(p);
3202 for (i = index; i < p->header.nextindex; i++) {
3203 d = (struct ldtentry *) & p->slot[stbl[i]];
3205 if (((long) jfs_dirent + d->namlen + 1) >
3206 (dirent_buf + PAGE_SIZE)) {
3207 /* DBCS codepages could overrun dirent_buf */
3213 d_namleft = d->namlen;
3214 name_ptr = jfs_dirent->name;
3215 jfs_dirent->ino = le32_to_cpu(d->inumber);
3218 len = min(d_namleft, DTLHDRDATALEN);
3219 jfs_dirent->position = le32_to_cpu(d->index);
3221 * d->index should always be valid, but it
3222 * isn't. fsck.jfs doesn't create the
3223 * directory index for the lost+found
3224 * directory. Rather than let it go,
3225 * we can try to fix it.
3227 if ((jfs_dirent->position < 2) ||
3228 (jfs_dirent->position >=
3229 JFS_IP(ip)->next_index)) {
3230 if (!page_fixed && !isReadOnly(ip)) {
3233 * setting overflow and setting
3234 * index to i will cause the
3235 * same page to be processed
3236 * again starting here
3242 jfs_dirent->position = unique_pos++;
3245 * We add 1 to the index because we may
3246 * use a value of 2 internally, and NFSv4
3247 * doesn't like that.
3249 jfs_dirent->position++;
3251 jfs_dirent->position = dtpos;
3252 len = min(d_namleft, DTLHDRDATALEN_LEGACY);
3255 /* copy the name of head/only segment */
3256 outlen = jfs_strfromUCS_le(name_ptr, d->name, len,
3258 jfs_dirent->name_len = outlen;
3260 /* copy name in the additional segment(s) */
3263 t = (struct dtslot *) & p->slot[next];
3267 if (d_namleft == 0) {
3269 "JFS:Dtree error: ino = %ld, bn=%lld, index = %d\n",
3275 len = min(d_namleft, DTSLOTDATALEN);
3276 outlen = jfs_strfromUCS_le(name_ptr, t->name,
3278 jfs_dirent->name_len += outlen;
3284 jfs_dirent = next_jfs_dirent(jfs_dirent);
3291 /* Point to next leaf page */
3292 if (p->header.flag & BT_ROOT)
3295 bn = le64_to_cpu(p->header.next);
3297 /* update offset (pn:index) for new page */
3300 dtoffset->index = 0;
3306 /* unpin previous leaf page */
3309 jfs_dirent = (struct jfs_dirent *) dirent_buf;
3310 while (jfs_dirents--) {
3311 ctx->pos = jfs_dirent->position;
3312 if (!dir_emit(ctx, jfs_dirent->name,
3313 jfs_dirent->name_len,
3314 jfs_dirent->ino, DT_UNKNOWN))
3316 jfs_dirent = next_jfs_dirent(jfs_dirent);
3320 add_missing_indices(ip, bn);
3324 if (!overflow && (bn == 0)) {
3329 DT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
3331 free_page(dirent_buf);
3337 free_page(dirent_buf);
3346 * function: get the leftmost page of the directory
3348 static int dtReadFirst(struct inode *ip, struct btstack * btstack)
3352 int psize = 288; /* initial in-line directory */
3353 struct metapage *mp;
3356 struct btframe *btsp;
3359 BT_CLR(btstack); /* reset stack */
3362 * descend leftmost path of the tree
3364 * by convention, root bn = 0.
3367 DT_GETPAGE(ip, bn, mp, psize, p, rc);
3372 * leftmost leaf page
3374 if (p->header.flag & BT_LEAF) {
3375 /* return leftmost entry */
3376 btsp = btstack->top;
3385 * descend down to leftmost child page
3387 if (BT_STACK_FULL(btstack)) {
3389 jfs_error(ip->i_sb, "btstack overrun\n");
3390 BT_STACK_DUMP(btstack);
3393 /* push (bn, index) of the parent page/entry */
3394 BT_PUSH(btstack, bn, 0);
3396 /* get the leftmost entry */
3397 stbl = DT_GETSTBL(p);
3398 xd = (pxd_t *) & p->slot[stbl[0]];
3400 /* get the child page block address */
3401 bn = addressPXD(xd);
3402 psize = lengthPXD(xd) << JFS_SBI(ip->i_sb)->l2bsize;
3404 /* unpin the parent page */
3413 * function: get the page of the specified offset (pn:index)
3415 * return: if (offset > eof), bn = -1;
3417 * note: if index > nextindex of the target leaf page,
3418 * start with 1st entry of next leaf page;
3420 static int dtReadNext(struct inode *ip, loff_t * offset,
3421 struct btstack * btstack)
3428 } *dtoffset = (struct dtoffset *) offset;
3430 struct metapage *mp;
3435 struct btframe *btsp, *parent;
3439 * get leftmost leaf page pinned
3441 if ((rc = dtReadFirst(ip, btstack)))
3445 DT_GETSEARCH(ip, btstack->top, bn, mp, p, index);
3447 /* get the start offset (pn:index) */
3448 pn = dtoffset->pn - 1; /* Now pn = 0 represents leftmost leaf */
3449 index = dtoffset->index;
3451 /* start at leftmost page ? */
3453 /* offset beyond eof ? */
3454 if (index < p->header.nextindex)
3457 if (p->header.flag & BT_ROOT) {
3462 /* start with 1st entry of next leaf page */
3464 dtoffset->index = index = 0;
3468 /* start at non-leftmost page: scan parent pages for large pn */
3469 if (p->header.flag & BT_ROOT) {
3474 /* start after next leaf page ? */
3478 /* get leaf page pn = 1 */
3480 bn = le64_to_cpu(p->header.next);
3482 /* unpin leaf page */
3485 /* offset beyond eof ? */
3494 * scan last internal page level to get target leaf page
3497 /* unpin leftmost leaf page */
3500 /* get left most parent page */
3501 btsp = btstack->top;
3504 DT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
3508 /* scan parent pages at last internal page level */
3509 while (pn >= p->header.nextindex) {
3510 pn -= p->header.nextindex;
3512 /* get next parent page address */
3513 bn = le64_to_cpu(p->header.next);
3515 /* unpin current parent page */
3518 /* offset beyond eof ? */
3524 /* get next parent page */
3525 DT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
3529 /* update parent page stack frame */
3533 /* get leaf page address */
3534 stbl = DT_GETSTBL(p);
3535 xd = (pxd_t *) & p->slot[stbl[pn]];
3536 bn = addressPXD(xd);
3538 /* unpin parent page */
3542 * get target leaf page
3545 DT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
3550 * leaf page has been completed:
3551 * start with 1st entry of next leaf page
3553 if (index >= p->header.nextindex) {
3554 bn = le64_to_cpu(p->header.next);
3556 /* unpin leaf page */
3559 /* offset beyond eof ? */
3565 /* get next leaf page */
3566 DT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
3570 /* start with 1st entry of next leaf page */
3572 dtoffset->index = 0;
3576 /* return target leaf page pinned */
3577 btsp = btstack->top;
3579 btsp->index = dtoffset->index;
3589 * function: compare search key with an internal entry
3592 * < 0 if k is < record
3593 * = 0 if k is = record
3594 * > 0 if k is > record
3596 static int dtCompare(struct component_name * key, /* search key */
3597 dtpage_t * p, /* directory page */
3599 { /* entry slot index */
3602 int klen, namlen, len, rc;
3603 struct idtentry *ih;
3607 * force the left-most key on internal pages, at any level of
3608 * the tree, to be less than any search key.
3609 * this obviates having to update the leftmost key on an internal
3610 * page when the user inserts a new key in the tree smaller than
3611 * anything that has been stored.
3613 * (? if/when dtSearch() narrows down to 1st entry (index = 0),
3614 * at any internal page at any level of the tree,
3615 * it descends to child of the entry anyway -
3616 * ? make the entry as min size dummy entry)
3618 * if (e->index == 0 && h->prevpg == P_INVALID && !(h->flags & BT_LEAF))
3625 ih = (struct idtentry *) & p->slot[si];
3628 namlen = ih->namlen;
3629 len = min(namlen, DTIHDRDATALEN);
3631 /* compare with head/only segment */
3632 len = min(klen, len);
3633 if ((rc = UniStrncmp_le(kname, name, len)))
3639 /* compare with additional segment(s) */
3641 while (klen > 0 && namlen > 0) {
3642 /* compare with next name segment */
3643 t = (struct dtslot *) & p->slot[si];
3644 len = min(namlen, DTSLOTDATALEN);
3645 len = min(klen, len);
3647 if ((rc = UniStrncmp_le(kname, name, len)))
3656 return (klen - namlen);
3665 * function: compare search key with an (leaf/internal) entry
3668 * < 0 if k is < record
3669 * = 0 if k is = record
3670 * > 0 if k is > record
3672 static int ciCompare(struct component_name * key, /* search key */
3673 dtpage_t * p, /* directory page */
3674 int si, /* entry slot index */
3679 int klen, namlen, len, rc;
3680 struct ldtentry *lh;
3681 struct idtentry *ih;
3686 * force the left-most key on internal pages, at any level of
3687 * the tree, to be less than any search key.
3688 * this obviates having to update the leftmost key on an internal
3689 * page when the user inserts a new key in the tree smaller than
3690 * anything that has been stored.
3692 * (? if/when dtSearch() narrows down to 1st entry (index = 0),
3693 * at any internal page at any level of the tree,
3694 * it descends to child of the entry anyway -
3695 * ? make the entry as min size dummy entry)
3697 * if (e->index == 0 && h->prevpg == P_INVALID && !(h->flags & BT_LEAF))
3707 if (p->header.flag & BT_LEAF) {
3708 lh = (struct ldtentry *) & p->slot[si];
3711 namlen = lh->namlen;
3712 if (flag & JFS_DIR_INDEX)
3713 len = min(namlen, DTLHDRDATALEN);
3715 len = min(namlen, DTLHDRDATALEN_LEGACY);
3718 * internal page entry
3721 ih = (struct idtentry *) & p->slot[si];
3724 namlen = ih->namlen;
3725 len = min(namlen, DTIHDRDATALEN);
3728 /* compare with head/only segment */
3729 len = min(klen, len);
3730 for (i = 0; i < len; i++, kname++, name++) {
3731 /* only uppercase if case-insensitive support is on */
3732 if ((flag & JFS_OS2) == JFS_OS2)
3733 x = UniToupper(le16_to_cpu(*name));
3735 x = le16_to_cpu(*name);
3736 if ((rc = *kname - x))
3743 /* compare with additional segment(s) */
3744 while (klen > 0 && namlen > 0) {
3745 /* compare with next name segment */
3746 t = (struct dtslot *) & p->slot[si];
3747 len = min(namlen, DTSLOTDATALEN);
3748 len = min(klen, len);
3750 for (i = 0; i < len; i++, kname++, name++) {
3751 /* only uppercase if case-insensitive support is on */
3752 if ((flag & JFS_OS2) == JFS_OS2)
3753 x = UniToupper(le16_to_cpu(*name));
3755 x = le16_to_cpu(*name);
3757 if ((rc = *kname - x))
3766 return (klen - namlen);
3771 * ciGetLeafPrefixKey()
3773 * function: compute prefix of suffix compression
3774 * from two adjacent leaf entries
3775 * across page boundary
3777 * return: non-zero on error
3780 static int ciGetLeafPrefixKey(dtpage_t * lp, int li, dtpage_t * rp,
3781 int ri, struct component_name * key, int flag)
3784 wchar_t *pl, *pr, *kname;
3785 struct component_name lkey;
3786 struct component_name rkey;
3788 lkey.name = kmalloc_array(JFS_NAME_MAX + 1, sizeof(wchar_t),
3790 if (lkey.name == NULL)
3793 rkey.name = kmalloc_array(JFS_NAME_MAX + 1, sizeof(wchar_t),
3795 if (rkey.name == NULL) {
3800 /* get left and right key */
3801 dtGetKey(lp, li, &lkey, flag);
3802 lkey.name[lkey.namlen] = 0;
3804 if ((flag & JFS_OS2) == JFS_OS2)
3807 dtGetKey(rp, ri, &rkey, flag);
3808 rkey.name[rkey.namlen] = 0;
3811 if ((flag & JFS_OS2) == JFS_OS2)
3814 /* compute prefix */
3817 namlen = min(lkey.namlen, rkey.namlen);
3818 for (pl = lkey.name, pr = rkey.name;
3819 namlen; pl++, pr++, namlen--, klen++, kname++) {
3822 key->namlen = klen + 1;
3827 /* l->namlen <= r->namlen since l <= r */
3828 if (lkey.namlen < rkey.namlen) {
3830 key->namlen = klen + 1;
3831 } else /* l->namelen == r->namelen */
3845 * function: get key of the entry
3847 static void dtGetKey(dtpage_t * p, int i, /* entry index */
3848 struct component_name * key, int flag)
3852 struct ldtentry *lh;
3853 struct idtentry *ih;
3860 stbl = DT_GETSTBL(p);
3862 if (p->header.flag & BT_LEAF) {
3863 lh = (struct ldtentry *) & p->slot[si];
3865 namlen = lh->namlen;
3867 if (flag & JFS_DIR_INDEX)
3868 len = min(namlen, DTLHDRDATALEN);
3870 len = min(namlen, DTLHDRDATALEN_LEGACY);
3872 ih = (struct idtentry *) & p->slot[si];
3874 namlen = ih->namlen;
3876 len = min(namlen, DTIHDRDATALEN);
3879 key->namlen = namlen;
3883 * move head/only segment
3885 UniStrncpy_from_le(kname, name, len);
3888 * move additional segment(s)
3891 /* get next segment */
3895 len = min(namlen, DTSLOTDATALEN);
3896 UniStrncpy_from_le(kname, t->name, len);
3906 * function: allocate free slot(s) and
3907 * write a leaf/internal entry
3909 * return: entry slot index
3911 static void dtInsertEntry(dtpage_t * p, int index, struct component_name * key,
3912 ddata_t * data, struct dt_lock ** dtlock)
3914 struct dtslot *h, *t;
3915 struct ldtentry *lh = NULL;
3916 struct idtentry *ih = NULL;
3917 int hsi, fsi, klen, len, nextindex;
3922 struct dt_lock *dtlck = *dtlock;
3926 struct metapage *mp = NULL;
3931 /* allocate a free slot */
3932 hsi = fsi = p->header.freelist;
3934 p->header.freelist = h->next;
3935 --p->header.freecnt;
3937 /* open new linelock */
3938 if (dtlck->index >= dtlck->maxcnt)
3939 dtlck = (struct dt_lock *) txLinelock(dtlck);
3941 lv = & dtlck->lv[dtlck->index];
3944 /* write head/only segment */
3945 if (p->header.flag & BT_LEAF) {
3946 lh = (struct ldtentry *) h;
3948 lh->inumber = cpu_to_le32(data->leaf.ino);
3951 if (data->leaf.ip) {
3952 len = min(klen, DTLHDRDATALEN);
3953 if (!(p->header.flag & BT_ROOT))
3954 bn = addressPXD(&p->header.self);
3955 lh->index = cpu_to_le32(add_index(data->leaf.tid,
3959 len = min(klen, DTLHDRDATALEN_LEGACY);
3961 ih = (struct idtentry *) h;
3967 len = min(klen, DTIHDRDATALEN);
3970 UniStrncpy_to_le(name, kname, len);
3975 /* write additional segment(s) */
3980 fsi = p->header.freelist;
3982 p->header.freelist = t->next;
3983 --p->header.freecnt;
3985 /* is next slot contiguous ? */
3986 if (fsi != xsi + 1) {
3987 /* close current linelock */
3991 /* open new linelock */
3992 if (dtlck->index < dtlck->maxcnt)
3995 dtlck = (struct dt_lock *) txLinelock(dtlck);
3996 lv = & dtlck->lv[0];
4004 len = min(klen, DTSLOTDATALEN);
4005 UniStrncpy_to_le(t->name, kname, len);
4012 /* close current linelock */
4018 /* terminate last/only segment */
4020 /* single segment entry */
4021 if (p->header.flag & BT_LEAF)
4026 /* multi-segment entry */
4029 /* if insert into middle, shift right succeeding entries in stbl */
4030 stbl = DT_GETSTBL(p);
4031 nextindex = p->header.nextindex;
4032 if (index < nextindex) {
4033 memmove(stbl + index + 1, stbl + index, nextindex - index);
4035 if ((p->header.flag & BT_LEAF) && data->leaf.ip) {
4039 * Need to update slot number for entries that moved
4043 for (n = index + 1; n <= nextindex; n++) {
4044 lh = (struct ldtentry *) & (p->slot[stbl[n]]);
4045 modify_index(data->leaf.tid, data->leaf.ip,
4046 le32_to_cpu(lh->index), bn, n,
4050 release_metapage(mp);
4056 /* advance next available entry index of stbl */
4057 ++p->header.nextindex;
4064 * function: move entries from split/left page to new/right page
4066 * nextindex of dst page and freelist/freecnt of both pages
4069 static void dtMoveEntry(dtpage_t * sp, int si, dtpage_t * dp,
4070 struct dt_lock ** sdtlock, struct dt_lock ** ddtlock,
4073 int ssi, next; /* src slot index */
4074 int di; /* dst entry index */
4075 int dsi; /* dst slot index */
4076 s8 *sstbl, *dstbl; /* sorted entry table */
4078 struct ldtentry *slh, *dlh = NULL;
4079 struct idtentry *sih, *dih = NULL;
4080 struct dtslot *h, *s, *d;
4081 struct dt_lock *sdtlck = *sdtlock, *ddtlck = *ddtlock;
4082 struct lv *slv, *dlv;
4086 sstbl = (s8 *) & sp->slot[sp->header.stblindex];
4087 dstbl = (s8 *) & dp->slot[dp->header.stblindex];
4089 dsi = dp->header.freelist; /* first (whole page) free slot */
4090 sfsi = sp->header.freelist;
4092 /* linelock destination entry slot */
4093 dlv = & ddtlck->lv[ddtlck->index];
4096 /* linelock source entry slot */
4097 slv = & sdtlck->lv[sdtlck->index];
4098 slv->offset = sstbl[si];
4099 xssi = slv->offset - 1;
4105 for (di = 0; si < sp->header.nextindex; si++, di++) {
4109 /* is next slot contiguous ? */
4110 if (ssi != xssi + 1) {
4111 /* close current linelock */
4115 /* open new linelock */
4116 if (sdtlck->index < sdtlck->maxcnt)
4119 sdtlck = (struct dt_lock *) txLinelock(sdtlck);
4120 slv = & sdtlck->lv[0];
4128 * move head/only segment of an entry
4131 h = d = &dp->slot[dsi];
4133 /* get src slot and move */
4135 if (sp->header.flag & BT_LEAF) {
4136 /* get source entry */
4137 slh = (struct ldtentry *) s;
4138 dlh = (struct ldtentry *) h;
4139 snamlen = slh->namlen;
4142 len = min(snamlen, DTLHDRDATALEN);
4143 dlh->index = slh->index; /* little-endian */
4145 len = min(snamlen, DTLHDRDATALEN_LEGACY);
4147 memcpy(dlh, slh, 6 + len * 2);
4151 /* update dst head/only segment next field */
4155 sih = (struct idtentry *) s;
4156 snamlen = sih->namlen;
4158 len = min(snamlen, DTIHDRDATALEN);
4159 dih = (struct idtentry *) h;
4160 memcpy(dih, sih, 10 + len * 2);
4167 /* free src head/only segment */
4177 * move additional segment(s) of the entry
4180 while ((ssi = next) >= 0) {
4181 /* is next slot contiguous ? */
4182 if (ssi != xssi + 1) {
4183 /* close current linelock */
4187 /* open new linelock */
4188 if (sdtlck->index < sdtlck->maxcnt)
4194 slv = & sdtlck->lv[0];
4201 /* get next source segment */
4204 /* get next destination free slot */
4207 len = min(snamlen, DTSLOTDATALEN);
4208 UniStrncpy_le(d->name, s->name, len);
4217 /* free source segment */
4226 /* terminate dst last/only segment */
4228 /* single segment entry */
4229 if (dp->header.flag & BT_LEAF)
4234 /* multi-segment entry */
4238 /* close current linelock */
4247 /* update source header */
4248 sp->header.freelist = sfsi;
4249 sp->header.freecnt += nd;
4251 /* update destination header */
4252 dp->header.nextindex = di;
4254 dp->header.freelist = dsi;
4255 dp->header.freecnt -= nd;
4262 * function: free a (leaf/internal) entry
4264 * log freelist header, stbl, and each segment slot of entry
4265 * (even though last/only segment next field is modified,
4266 * physical image logging requires all segment slots of
4267 * the entry logged to avoid applying previous updates
4268 * to the same slots)
4270 static void dtDeleteEntry(dtpage_t * p, int fi, struct dt_lock ** dtlock)
4272 int fsi; /* free entry slot index */
4276 struct dt_lock *dtlck = *dtlock;
4280 /* get free entry slot index */
4281 stbl = DT_GETSTBL(p);
4284 /* open new linelock */
4285 if (dtlck->index >= dtlck->maxcnt)
4286 dtlck = (struct dt_lock *) txLinelock(dtlck);
4287 lv = & dtlck->lv[dtlck->index];
4291 /* get the head/only segment */
4293 if (p->header.flag & BT_LEAF)
4294 si = ((struct ldtentry *) t)->next;
4296 si = ((struct idtentry *) t)->next;
4303 /* find the last/only segment */
4305 /* is next slot contiguous ? */
4306 if (si != xsi + 1) {
4307 /* close current linelock */
4311 /* open new linelock */
4312 if (dtlck->index < dtlck->maxcnt)
4315 dtlck = (struct dt_lock *) txLinelock(dtlck);
4316 lv = & dtlck->lv[0];
4332 /* close current linelock */
4338 /* update freelist */
4339 t->next = p->header.freelist;
4340 p->header.freelist = fsi;
4341 p->header.freecnt += freecnt;
4343 /* if delete from middle,
4344 * shift left the succedding entries in the stbl
4346 si = p->header.nextindex;
4348 memmove(&stbl[fi], &stbl[fi + 1], si - fi - 1);
4350 p->header.nextindex--;
4357 * function: truncate a (leaf/internal) entry
4359 * log freelist header, stbl, and each segment slot of entry
4360 * (even though last/only segment next field is modified,
4361 * physical image logging requires all segment slots of
4362 * the entry logged to avoid applying previous updates
4363 * to the same slots)
4365 static void dtTruncateEntry(dtpage_t * p, int ti, struct dt_lock ** dtlock)
4367 int tsi; /* truncate entry slot index */
4371 struct dt_lock *dtlck = *dtlock;
4375 /* get free entry slot index */
4376 stbl = DT_GETSTBL(p);
4379 /* open new linelock */
4380 if (dtlck->index >= dtlck->maxcnt)
4381 dtlck = (struct dt_lock *) txLinelock(dtlck);
4382 lv = & dtlck->lv[dtlck->index];
4386 /* get the head/only segment */
4388 ASSERT(p->header.flag & BT_INTERNAL);
4389 ((struct idtentry *) t)->namlen = 0;
4390 si = ((struct idtentry *) t)->next;
4391 ((struct idtentry *) t)->next = -1;
4398 /* find the last/only segment */
4400 /* is next slot contiguous ? */
4401 if (si != xsi + 1) {
4402 /* close current linelock */
4406 /* open new linelock */
4407 if (dtlck->index < dtlck->maxcnt)
4410 dtlck = (struct dt_lock *) txLinelock(dtlck);
4411 lv = & dtlck->lv[0];
4427 /* close current linelock */
4433 /* update freelist */
4436 t->next = p->header.freelist;
4437 p->header.freelist = fsi;
4438 p->header.freecnt += freecnt;
4443 * dtLinelockFreelist()
4445 static void dtLinelockFreelist(dtpage_t * p, /* directory page */
4446 int m, /* max slot index */
4447 struct dt_lock ** dtlock)
4449 int fsi; /* free entry slot index */
4452 struct dt_lock *dtlck = *dtlock;
4456 /* get free entry slot index */
4457 fsi = p->header.freelist;
4459 /* open new linelock */
4460 if (dtlck->index >= dtlck->maxcnt)
4461 dtlck = (struct dt_lock *) txLinelock(dtlck);
4462 lv = & dtlck->lv[dtlck->index];
4472 /* find the last/only segment */
4473 while (si < m && si >= 0) {
4474 /* is next slot contiguous ? */
4475 if (si != xsi + 1) {
4476 /* close current linelock */
4480 /* open new linelock */
4481 if (dtlck->index < dtlck->maxcnt)
4484 dtlck = (struct dt_lock *) txLinelock(dtlck);
4485 lv = & dtlck->lv[0];
4499 /* close current linelock */
4510 * FUNCTION: Modify the inode number part of a directory entry
4513 * tid - Transaction id
4514 * ip - Inode of parent directory
4515 * key - Name of entry to be modified
4516 * orig_ino - Original inode number expected in entry
4517 * new_ino - New inode number to put into entry
4521 * -ESTALE - If entry found does not match orig_ino passed in
4522 * -ENOENT - If no entry can be found to match key
4523 * 0 - If successfully modified entry
4525 int dtModify(tid_t tid, struct inode *ip,
4526 struct component_name * key, ino_t * orig_ino, ino_t new_ino, int flag)
4530 struct metapage *mp;
4533 struct btstack btstack;
4535 struct dt_lock *dtlck;
4538 int entry_si; /* entry slot index */
4539 struct ldtentry *entry;
4542 * search for the entry to modify:
4544 * dtSearch() returns (leaf page pinned, index at which to modify).
4546 if ((rc = dtSearch(ip, key, orig_ino, &btstack, flag)))
4549 /* retrieve search result */
4550 DT_GETSEARCH(ip, btstack.top, bn, mp, p, index);
4552 BT_MARK_DIRTY(mp, ip);
4554 * acquire a transaction lock on the leaf page of named entry
4556 tlck = txLock(tid, ip, mp, tlckDTREE | tlckENTRY);
4557 dtlck = (struct dt_lock *) & tlck->lock;
4559 /* get slot index of the entry */
4560 stbl = DT_GETSTBL(p);
4561 entry_si = stbl[index];
4563 /* linelock entry */
4564 ASSERT(dtlck->index == 0);
4565 lv = & dtlck->lv[0];
4566 lv->offset = entry_si;
4570 /* get the head/only segment */
4571 entry = (struct ldtentry *) & p->slot[entry_si];
4573 /* substitute the inode number of the entry */
4574 entry->inumber = cpu_to_le32(new_ino);
4576 /* unpin the leaf page */