1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) International Business Machines Corp., 2000-2004
7 * jfs_dtree.c: directory B+-tree manager
9 * B+-tree with variable length key directory:
11 * each directory page is structured as an array of 32-byte
12 * directory entry slots initialized as a freelist
13 * to avoid search/compaction of free space at insertion.
14 * when an entry is inserted, a number of slots are allocated
15 * from the freelist as required to store variable length data
16 * of the entry; when the entry is deleted, slots of the entry
17 * are returned to freelist.
19 * leaf entry stores full name as key and file serial number
20 * (aka inode number) as data.
21 * internal/router entry stores sufffix compressed name
22 * as key and simple extent descriptor as data.
24 * each directory page maintains a sorted entry index table
25 * which stores the start slot index of sorted entries
26 * to allow binary search on the table.
28 * directory starts as a root/leaf page in on-disk inode
30 * when it becomes full, it starts a leaf of a external extent
31 * of length of 1 block. each time the first leaf becomes full,
32 * it is extended rather than split (its size is doubled),
33 * until its length becoms 4 KBytes, from then the extent is split
34 * with new 4 Kbyte extent when it becomes full
35 * to reduce external fragmentation of small directories.
37 * blah, blah, blah, for linear scan of directory in pieces by
41 * case-insensitive directory file system
43 * names are stored in case-sensitive way in leaf entry.
44 * but stored, searched and compared in case-insensitive (uppercase) order
45 * (i.e., both search key and entry key are folded for search/compare):
46 * (note that case-sensitive order is BROKEN in storage, e.g.,
47 * sensitive: Ad, aB, aC, aD -> insensitive: aB, aC, aD, Ad
49 * entries which folds to the same key makes up a equivalent class
50 * whose members are stored as contiguous cluster (may cross page boundary)
51 * but whose order is arbitrary and acts as duplicate, e.g.,
54 * once match is found at leaf, requires scan forward/backward
55 * either for, in case-insensitive search, duplicate
56 * or for, in case-sensitive search, for exact match
58 * router entry must be created/stored in case-insensitive way
60 * (right most key of left page and left most key of right page
61 * are folded, and its suffix compression is propagated as router
63 * (e.g., if split occurs <abc> and <aBd>, <ABD> trather than <aB>
64 * should be made the router key for the split)
66 * case-insensitive search:
70 * case-insensitive search of B-tree:
71 * for internal entry, router key is already folded;
72 * for leaf entry, fold the entry key before comparison.
74 * if (leaf entry case-insensitive match found)
75 * if (next entry satisfies case-insensitive match)
77 * if (prev entry satisfies case-insensitive match)
84 * target directory inode lock is being held on entry/exit
85 * of all main directory service routines.
91 #include <linux/quotaops.h>
92 #include <linux/slab.h>
93 #include "jfs_incore.h"
94 #include "jfs_superblock.h"
95 #include "jfs_filsys.h"
96 #include "jfs_metapage.h"
98 #include "jfs_unicode.h"
99 #include "jfs_debug.h"
101 /* dtree split parameter */
106 struct component_name *key;
108 struct pxdlist *pxdlist;
111 #define DT_PAGE(IP, MP) BT_PAGE(IP, MP, dtpage_t, i_dtroot)
113 /* get page buffer for specified block address */
114 #define DT_GETPAGE(IP, BN, MP, SIZE, P, RC) \
116 BT_GETPAGE(IP, BN, MP, dtpage_t, SIZE, P, RC, i_dtroot); \
118 if (((P)->header.nextindex > \
119 (((BN) == 0) ? DTROOTMAXSLOT : (P)->header.maxslot)) || \
120 ((BN) && ((P)->header.maxslot > DTPAGEMAXSLOT))) { \
122 jfs_error((IP)->i_sb, \
123 "DT_GETPAGE: dtree page corrupt\n"); \
130 /* for consistency */
131 #define DT_PUTPAGE(MP) BT_PUTPAGE(MP)
133 #define DT_GETSEARCH(IP, LEAF, BN, MP, P, INDEX) \
134 BT_GETSEARCH(IP, LEAF, BN, MP, dtpage_t, P, INDEX, i_dtroot)
139 static int dtSplitUp(tid_t tid, struct inode *ip,
140 struct dtsplit * split, struct btstack * btstack);
142 static int dtSplitPage(tid_t tid, struct inode *ip, struct dtsplit * split,
143 struct metapage ** rmpp, dtpage_t ** rpp, pxd_t * rxdp);
145 static int dtExtendPage(tid_t tid, struct inode *ip,
146 struct dtsplit * split, struct btstack * btstack);
148 static int dtSplitRoot(tid_t tid, struct inode *ip,
149 struct dtsplit * split, struct metapage ** rmpp);
151 static int dtDeleteUp(tid_t tid, struct inode *ip, struct metapage * fmp,
152 dtpage_t * fp, struct btstack * btstack);
154 static int dtRelink(tid_t tid, struct inode *ip, dtpage_t * p);
156 static int dtReadFirst(struct inode *ip, struct btstack * btstack);
158 static int dtReadNext(struct inode *ip,
159 loff_t * offset, struct btstack * btstack);
161 static int dtCompare(struct component_name * key, dtpage_t * p, int si);
163 static int ciCompare(struct component_name * key, dtpage_t * p, int si,
166 static void dtGetKey(dtpage_t * p, int i, struct component_name * key,
169 static int ciGetLeafPrefixKey(dtpage_t * lp, int li, dtpage_t * rp,
170 int ri, struct component_name * key, int flag);
172 static void dtInsertEntry(dtpage_t * p, int index, struct component_name * key,
173 ddata_t * data, struct dt_lock **);
175 static void dtMoveEntry(dtpage_t * sp, int si, dtpage_t * dp,
176 struct dt_lock ** sdtlock, struct dt_lock ** ddtlock,
179 static void dtDeleteEntry(dtpage_t * p, int fi, struct dt_lock ** dtlock);
181 static void dtTruncateEntry(dtpage_t * p, int ti, struct dt_lock ** dtlock);
183 static void dtLinelockFreelist(dtpage_t * p, int m, struct dt_lock ** dtlock);
185 #define ciToUpper(c) UniStrupr((c)->name)
190 * Reads a page of a directory's index table.
191 * Having metadata mapped into the directory inode's address space
192 * presents a multitude of problems. We avoid this by mapping to
193 * the absolute address space outside of the *_metapage routines
195 static struct metapage *read_index_page(struct inode *inode, s64 blkno)
202 rc = xtLookup(inode, blkno, 1, &xflag, &xaddr, &xlen, 1);
203 if (rc || (xaddr == 0))
206 return read_metapage(inode, xaddr, PSIZE, 1);
212 * Same as get_index_page(), but get's a new page without reading
214 static struct metapage *get_index_page(struct inode *inode, s64 blkno)
221 rc = xtLookup(inode, blkno, 1, &xflag, &xaddr, &xlen, 1);
222 if (rc || (xaddr == 0))
225 return get_metapage(inode, xaddr, PSIZE, 1);
231 * Returns dtree page containing directory table entry for specified
232 * index and pointer to its entry.
234 * mp must be released by caller.
236 static struct dir_table_slot *find_index(struct inode *ip, u32 index,
237 struct metapage ** mp, s64 *lblock)
239 struct jfs_inode_info *jfs_ip = JFS_IP(ip);
243 struct dir_table_slot *slot;
244 static int maxWarnings = 10;
248 jfs_warn("find_entry called with index = %d", index);
254 if (index >= jfs_ip->next_index) {
255 jfs_warn("find_entry called with index >= next_index");
259 if (jfs_dirtable_inline(ip)) {
261 * Inline directory table
264 slot = &jfs_ip->i_dirtable[index - 2];
266 offset = (index - 2) * sizeof(struct dir_table_slot);
267 page_offset = offset & (PSIZE - 1);
268 blkno = ((offset + 1) >> L2PSIZE) <<
269 JFS_SBI(ip->i_sb)->l2nbperpage;
271 if (*mp && (*lblock != blkno)) {
272 release_metapage(*mp);
277 *mp = read_index_page(ip, blkno);
280 jfs_err("free_index: error reading directory table");
285 (struct dir_table_slot *) ((char *) (*mp)->data +
291 static inline void lock_index(tid_t tid, struct inode *ip, struct metapage * mp,
295 struct linelock *llck;
298 tlck = txLock(tid, ip, mp, tlckDATA);
299 llck = (struct linelock *) tlck->lock;
301 if (llck->index >= llck->maxcnt)
302 llck = txLinelock(llck);
303 lv = &llck->lv[llck->index];
306 * Linelock slot size is twice the size of directory table
307 * slot size. 512 entries per page.
309 lv->offset = ((index - 2) & 511) >> 1;
317 * Adds an entry to the directory index table. This is used to provide
318 * each directory entry with a persistent index in which to resume
319 * directory traversals
321 static u32 add_index(tid_t tid, struct inode *ip, s64 bn, int slot)
323 struct super_block *sb = ip->i_sb;
324 struct jfs_sb_info *sbi = JFS_SBI(sb);
325 struct jfs_inode_info *jfs_ip = JFS_IP(ip);
327 struct dir_table_slot *dirtab_slot;
329 struct linelock *llck;
337 ASSERT(DO_INDEX(ip));
339 if (jfs_ip->next_index < 2) {
340 jfs_warn("add_index: next_index = %d. Resetting!",
342 jfs_ip->next_index = 2;
345 index = jfs_ip->next_index++;
347 if (index <= MAX_INLINE_DIRTABLE_ENTRY) {
349 * i_size reflects size of index table, or 8 bytes per entry.
351 ip->i_size = (loff_t) (index - 1) << 3;
354 * dir table fits inline within inode
356 dirtab_slot = &jfs_ip->i_dirtable[index-2];
357 dirtab_slot->flag = DIR_INDEX_VALID;
358 dirtab_slot->slot = slot;
359 DTSaddress(dirtab_slot, bn);
361 set_cflag(COMMIT_Dirtable, ip);
365 if (index == (MAX_INLINE_DIRTABLE_ENTRY + 1)) {
366 struct dir_table_slot temp_table[12];
369 * It's time to move the inline table to an external
370 * page and begin to build the xtree
372 if (dquot_alloc_block(ip, sbi->nbperpage))
374 if (dbAlloc(ip, 0, sbi->nbperpage, &xaddr)) {
375 dquot_free_block(ip, sbi->nbperpage);
380 * Save the table, we're going to overwrite it with the
383 memcpy(temp_table, &jfs_ip->i_dirtable, sizeof(temp_table));
386 * Initialize empty x-tree
391 * Add the first block to the xtree
393 if (xtInsert(tid, ip, 0, 0, sbi->nbperpage, &xaddr, 0)) {
394 /* This really shouldn't fail */
395 jfs_warn("add_index: xtInsert failed!");
396 memcpy(&jfs_ip->i_dirtable, temp_table,
397 sizeof (temp_table));
398 dbFree(ip, xaddr, sbi->nbperpage);
399 dquot_free_block(ip, sbi->nbperpage);
404 mp = get_index_page(ip, 0);
406 jfs_err("add_index: get_metapage failed!");
407 xtTruncate(tid, ip, 0, COMMIT_PWMAP);
408 memcpy(&jfs_ip->i_dirtable, temp_table,
409 sizeof (temp_table));
412 tlck = txLock(tid, ip, mp, tlckDATA);
413 llck = (struct linelock *) & tlck->lock;
414 ASSERT(llck->index == 0);
418 lv->length = 6; /* tlckDATA slot size is 16 bytes */
421 memcpy(mp->data, temp_table, sizeof(temp_table));
423 mark_metapage_dirty(mp);
424 release_metapage(mp);
427 * Logging is now directed by xtree tlocks
429 clear_cflag(COMMIT_Dirtable, ip);
432 offset = (index - 2) * sizeof(struct dir_table_slot);
433 page_offset = offset & (PSIZE - 1);
434 blkno = ((offset + 1) >> L2PSIZE) << sbi->l2nbperpage;
435 if (page_offset == 0) {
437 * This will be the beginning of a new page
440 if (xtInsert(tid, ip, 0, blkno, sbi->nbperpage, &xaddr, 0)) {
441 jfs_warn("add_index: xtInsert failed!");
446 if ((mp = get_index_page(ip, blkno)))
447 memset(mp->data, 0, PSIZE); /* Just looks better */
449 xtTruncate(tid, ip, offset, COMMIT_PWMAP);
451 mp = read_index_page(ip, blkno);
454 jfs_err("add_index: get/read_metapage failed!");
458 lock_index(tid, ip, mp, index);
461 (struct dir_table_slot *) ((char *) mp->data + page_offset);
462 dirtab_slot->flag = DIR_INDEX_VALID;
463 dirtab_slot->slot = slot;
464 DTSaddress(dirtab_slot, bn);
466 mark_metapage_dirty(mp);
467 release_metapage(mp);
473 jfs_ip->next_index--;
481 * Marks an entry to the directory index table as free.
483 static void free_index(tid_t tid, struct inode *ip, u32 index, u32 next)
485 struct dir_table_slot *dirtab_slot;
487 struct metapage *mp = NULL;
489 dirtab_slot = find_index(ip, index, &mp, &lblock);
494 dirtab_slot->flag = DIR_INDEX_FREE;
495 dirtab_slot->slot = dirtab_slot->addr1 = 0;
496 dirtab_slot->addr2 = cpu_to_le32(next);
499 lock_index(tid, ip, mp, index);
500 mark_metapage_dirty(mp);
501 release_metapage(mp);
503 set_cflag(COMMIT_Dirtable, ip);
509 * Changes an entry in the directory index table
511 static void modify_index(tid_t tid, struct inode *ip, u32 index, s64 bn,
512 int slot, struct metapage ** mp, s64 *lblock)
514 struct dir_table_slot *dirtab_slot;
516 dirtab_slot = find_index(ip, index, mp, lblock);
521 DTSaddress(dirtab_slot, bn);
522 dirtab_slot->slot = slot;
525 lock_index(tid, ip, *mp, index);
526 mark_metapage_dirty(*mp);
528 set_cflag(COMMIT_Dirtable, ip);
534 * reads a directory table slot
536 static int read_index(struct inode *ip, u32 index,
537 struct dir_table_slot * dirtab_slot)
540 struct metapage *mp = NULL;
541 struct dir_table_slot *slot;
543 slot = find_index(ip, index, &mp, &lblock);
548 memcpy(dirtab_slot, slot, sizeof(struct dir_table_slot));
551 release_metapage(mp);
560 * Search for the entry with specified key
564 * return: 0 - search result on stack, leaf page pinned;
567 int dtSearch(struct inode *ip, struct component_name * key, ino_t * data,
568 struct btstack * btstack, int flag)
571 int cmp = 1; /* init for empty page */
576 int base, index, lim;
577 struct btframe *btsp;
579 int psize = 288; /* initial in-line directory */
581 struct component_name ciKey;
582 struct super_block *sb = ip->i_sb;
584 ciKey.name = kmalloc_array(JFS_NAME_MAX + 1, sizeof(wchar_t),
592 /* uppercase search key for c-i directory */
593 UniStrcpy(ciKey.name, key->name);
594 ciKey.namlen = key->namlen;
596 /* only uppercase if case-insensitive support is on */
597 if ((JFS_SBI(sb)->mntflag & JFS_OS2) == JFS_OS2) {
600 BT_CLR(btstack); /* reset stack */
602 /* init level count for max pages to split */
606 * search down tree from root:
608 * between two consecutive entries of <Ki, Pi> and <Kj, Pj> of
609 * internal page, child page Pi contains entry with k, Ki <= K < Kj.
611 * if entry with search key K is not found
612 * internal page search find the entry with largest key Ki
613 * less than K which point to the child page to search;
614 * leaf page search find the entry with smallest key Kj
615 * greater than K so that the returned index is the position of
616 * the entry to be shifted right for insertion of new entry.
617 * for empty tree, search key is greater than any key of the tree.
619 * by convention, root bn = 0.
622 /* get/pin the page to search */
623 DT_GETPAGE(ip, bn, mp, psize, p, rc);
627 /* get sorted entry table of the page */
628 stbl = DT_GETSTBL(p);
631 * binary search with search key K on the current page.
633 for (base = 0, lim = p->header.nextindex; lim; lim >>= 1) {
634 index = base + (lim >> 1);
636 if (p->header.flag & BT_LEAF) {
637 /* uppercase leaf name to compare */
639 ciCompare(&ciKey, p, stbl[index],
640 JFS_SBI(sb)->mntflag);
642 /* router key is in uppercase */
644 cmp = dtCompare(&ciKey, p, stbl[index]);
652 /* search hit - leaf page:
653 * return the entry found
655 if (p->header.flag & BT_LEAF) {
656 inumber = le32_to_cpu(
657 ((struct ldtentry *) & p->slot[stbl[index]])->inumber);
660 * search for JFS_LOOKUP
662 if (flag == JFS_LOOKUP) {
669 * search for JFS_CREATE
671 if (flag == JFS_CREATE) {
678 * search for JFS_REMOVE or JFS_RENAME
680 if ((flag == JFS_REMOVE ||
681 flag == JFS_RENAME) &&
688 * JFS_REMOVE|JFS_FINDDIR|JFS_RENAME
690 /* save search result */
701 /* search hit - internal page:
702 * descend/search its child page
716 * base is the smallest index with key (Kj) greater than
717 * search key (K) and may be zero or (maxindex + 1) index.
720 * search miss - leaf page
722 * return location of entry (base) where new entry with
723 * search key K is to be inserted.
725 if (p->header.flag & BT_LEAF) {
727 * search for JFS_LOOKUP, JFS_REMOVE, or JFS_RENAME
729 if (flag == JFS_LOOKUP || flag == JFS_REMOVE ||
730 flag == JFS_RENAME) {
736 * search for JFS_CREATE|JFS_FINDDIR:
751 * search miss - internal page
753 * if base is non-zero, decrement base by one to get the parent
754 * entry of the child page to search.
756 index = base ? base - 1 : base;
759 * go down to child page
762 /* update max. number of pages to split */
763 if (BT_STACK_FULL(btstack)) {
764 /* Something's corrupted, mark filesystem dirty so
765 * chkdsk will fix it.
767 jfs_error(sb, "stack overrun!\n");
768 BT_STACK_DUMP(btstack);
774 /* push (bn, index) of the parent page/entry */
775 BT_PUSH(btstack, bn, index);
777 /* get the child page block number */
778 pxd = (pxd_t *) & p->slot[stbl[index]];
779 bn = addressPXD(pxd);
780 psize = lengthPXD(pxd) << JFS_SBI(ip->i_sb)->l2bsize;
782 /* unpin the parent page */
802 * function: insert an entry to directory tree
806 * return: 0 - success;
809 int dtInsert(tid_t tid, struct inode *ip,
810 struct component_name * name, ino_t * fsn, struct btstack * btstack)
813 struct metapage *mp; /* meta-page buffer */
814 dtpage_t *p; /* base B+-tree index page */
817 struct dtsplit split; /* split information */
819 struct dt_lock *dtlck;
825 * retrieve search result
827 * dtSearch() returns (leaf page pinned, index at which to insert).
828 * n.b. dtSearch() may return index of (maxindex + 1) of
831 DT_GETSEARCH(ip, btstack->top, bn, mp, p, index);
834 * insert entry for new key
837 if (JFS_IP(ip)->next_index == DIREND) {
841 n = NDTLEAF(name->namlen);
845 n = NDTLEAF_LEGACY(name->namlen);
846 data.leaf.ip = NULL; /* signifies legacy directory format */
848 data.leaf.ino = *fsn;
851 * leaf page does not have enough room for new entry:
853 * extend/split the leaf page;
855 * dtSplitUp() will insert the entry and unpin the leaf page.
857 if (n > p->header.freecnt) {
863 rc = dtSplitUp(tid, ip, &split, btstack);
868 * leaf page does have enough room for new entry:
870 * insert the new data entry into the leaf page;
872 BT_MARK_DIRTY(mp, ip);
874 * acquire a transaction lock on the leaf page
876 tlck = txLock(tid, ip, mp, tlckDTREE | tlckENTRY);
877 dtlck = (struct dt_lock *) & tlck->lock;
878 ASSERT(dtlck->index == 0);
881 /* linelock header */
886 dtInsertEntry(p, index, name, &data, &dtlck);
888 /* linelock stbl of non-root leaf page */
889 if (!(p->header.flag & BT_ROOT)) {
890 if (dtlck->index >= dtlck->maxcnt)
891 dtlck = (struct dt_lock *) txLinelock(dtlck);
892 lv = & dtlck->lv[dtlck->index];
893 n = index >> L2DTSLOTSIZE;
894 lv->offset = p->header.stblindex + n;
896 ((p->header.nextindex - 1) >> L2DTSLOTSIZE) - n + 1;
900 /* unpin the leaf page */
910 * function: propagate insertion bottom up;
914 * return: 0 - success;
916 * leaf page unpinned;
918 static int dtSplitUp(tid_t tid,
919 struct inode *ip, struct dtsplit * split, struct btstack * btstack)
921 struct jfs_sb_info *sbi = JFS_SBI(ip->i_sb);
923 struct metapage *smp;
924 dtpage_t *sp; /* split page */
925 struct metapage *rmp;
926 dtpage_t *rp; /* new right page split from sp */
927 pxd_t rpxd; /* new right page extent descriptor */
928 struct metapage *lmp;
929 dtpage_t *lp; /* left child page */
930 int skip; /* index of entry of insertion */
931 struct btframe *parent; /* parent page entry on traverse stack */
934 struct pxdlist pxdlist;
936 struct component_name key = { 0, NULL };
937 ddata_t *data = split->data;
939 struct dt_lock *dtlck;
942 int quota_allocation = 0;
946 sp = DT_PAGE(ip, smp);
948 key.name = kmalloc_array(JFS_NAME_MAX + 2, sizeof(wchar_t), GFP_NOFS);
958 * The split routines insert the new entry, and
959 * acquire txLock as appropriate.
962 * split root leaf page:
964 if (sp->header.flag & BT_ROOT) {
966 * allocate a single extent child page
969 n = sbi->bsize >> L2DTSLOTSIZE;
970 n -= (n + 31) >> L2DTSLOTSIZE; /* stbl size */
971 n -= DTROOTMAXSLOT - sp->header.freecnt; /* header + entries */
972 if (n <= split->nslot)
974 if ((rc = dbAlloc(ip, 0, (s64) xlen, &xaddr))) {
981 pxd = &pxdlist.pxd[0];
982 PXDaddress(pxd, xaddr);
983 PXDlength(pxd, xlen);
984 split->pxdlist = &pxdlist;
985 rc = dtSplitRoot(tid, ip, split, &rmp);
988 dbFree(ip, xaddr, xlen);
995 ip->i_size = xlen << sbi->l2bsize;
1001 * extend first leaf page
1003 * extend the 1st extent if less than buffer page size
1004 * (dtExtendPage() reurns leaf page unpinned)
1006 pxd = &sp->header.self;
1007 xlen = lengthPXD(pxd);
1008 xsize = xlen << sbi->l2bsize;
1009 if (xsize < PSIZE) {
1010 xaddr = addressPXD(pxd);
1011 n = xsize >> L2DTSLOTSIZE;
1012 n -= (n + 31) >> L2DTSLOTSIZE; /* stbl size */
1013 if ((n + sp->header.freecnt) <= split->nslot)
1014 n = xlen + (xlen << 1);
1018 /* Allocate blocks to quota. */
1019 rc = dquot_alloc_block(ip, n);
1022 quota_allocation += n;
1024 if ((rc = dbReAlloc(sbi->ipbmap, xaddr, (s64) xlen,
1028 pxdlist.maxnpxd = 1;
1030 pxd = &pxdlist.pxd[0];
1031 PXDaddress(pxd, nxaddr);
1032 PXDlength(pxd, xlen + n);
1033 split->pxdlist = &pxdlist;
1034 if ((rc = dtExtendPage(tid, ip, split, btstack))) {
1035 nxaddr = addressPXD(pxd);
1036 if (xaddr != nxaddr) {
1037 /* free relocated extent */
1038 xlen = lengthPXD(pxd);
1039 dbFree(ip, nxaddr, (s64) xlen);
1041 /* free extended delta */
1042 xlen = lengthPXD(pxd) - n;
1043 xaddr = addressPXD(pxd) + xlen;
1044 dbFree(ip, xaddr, (s64) n);
1046 } else if (!DO_INDEX(ip))
1047 ip->i_size = lengthPXD(pxd) << sbi->l2bsize;
1056 * split leaf page <sp> into <sp> and a new right page <rp>.
1058 * return <rp> pinned and its extent descriptor <rpxd>
1061 * allocate new directory page extent and
1062 * new index page(s) to cover page split(s)
1064 * allocation hint: ?
1066 n = btstack->nsplit;
1067 pxdlist.maxnpxd = pxdlist.npxd = 0;
1068 xlen = sbi->nbperpage;
1069 for (pxd = pxdlist.pxd; n > 0; n--, pxd++) {
1070 if ((rc = dbAlloc(ip, 0, (s64) xlen, &xaddr)) == 0) {
1071 PXDaddress(pxd, xaddr);
1072 PXDlength(pxd, xlen);
1079 /* undo allocation */
1083 split->pxdlist = &pxdlist;
1084 if ((rc = dtSplitPage(tid, ip, split, &rmp, &rp, &rpxd))) {
1087 /* undo allocation */
1092 ip->i_size += PSIZE;
1095 * propagate up the router entry for the leaf page just split
1097 * insert a router entry for the new page into the parent page,
1098 * propagate the insert/split up the tree by walking back the stack
1099 * of (bn of parent page, index of child page entry in parent page)
1100 * that were traversed during the search for the page that split.
1102 * the propagation of insert/split up the tree stops if the root
1103 * splits or the page inserted into doesn't have to split to hold
1106 * the parent entry for the split page remains the same, and
1107 * a new entry is inserted at its right with the first key and
1108 * block number of the new right page.
1110 * There are a maximum of 4 pages pinned at any time:
1111 * two children, left parent and right parent (when the parent splits).
1112 * keep the child pages pinned while working on the parent.
1113 * make sure that all pins are released at exit.
1115 while ((parent = BT_POP(btstack)) != NULL) {
1116 /* parent page specified by stack frame <parent> */
1118 /* keep current child pages (<lp>, <rp>) pinned */
1123 * insert router entry in parent for new right child page <rp>
1125 /* get the parent page <sp> */
1126 DT_GETPAGE(ip, parent->bn, smp, PSIZE, sp, rc);
1134 * The new key entry goes ONE AFTER the index of parent entry,
1135 * because the split was to the right.
1137 skip = parent->index + 1;
1140 * compute the key for the router entry
1142 * key suffix compression:
1143 * for internal pages that have leaf pages as children,
1144 * retain only what's needed to distinguish between
1145 * the new entry and the entry on the page to its left.
1146 * If the keys compare equal, retain the entire key.
1148 * note that compression is performed only at computing
1149 * router key at the lowest internal level.
1150 * further compression of the key between pairs of higher
1151 * level internal pages loses too much information and
1152 * the search may fail.
1153 * (e.g., two adjacent leaf pages of {a, ..., x} {xx, ...,}
1154 * results in two adjacent parent entries (a)(xx).
1155 * if split occurs between these two entries, and
1156 * if compression is applied, the router key of parent entry
1157 * of right page (x) will divert search for x into right
1158 * subtree and miss x in the left subtree.)
1160 * the entire key must be retained for the next-to-leftmost
1161 * internal key at any level of the tree, or search may fail
1164 switch (rp->header.flag & BT_TYPE) {
1167 * compute the length of prefix for suffix compression
1168 * between last entry of left page and first entry
1171 if ((sp->header.flag & BT_ROOT && skip > 1) ||
1172 sp->header.prev != 0 || skip > 1) {
1173 /* compute uppercase router prefix key */
1174 rc = ciGetLeafPrefixKey(lp,
1175 lp->header.nextindex-1,
1185 /* next to leftmost entry of
1186 lowest internal level */
1188 /* compute uppercase router key */
1189 dtGetKey(rp, 0, &key, sbi->mntflag);
1190 key.name[key.namlen] = 0;
1192 if ((sbi->mntflag & JFS_OS2) == JFS_OS2)
1196 n = NDTINTERNAL(key.namlen);
1200 dtGetKey(rp, 0, &key, sbi->mntflag);
1201 n = NDTINTERNAL(key.namlen);
1205 jfs_err("dtSplitUp(): UFO!");
1209 /* unpin left child page */
1213 * compute the data for the router entry
1215 data->xd = rpxd; /* child page xd */
1218 * parent page is full - split the parent page
1220 if (n > sp->header.freecnt) {
1221 /* init for parent page split */
1223 split->index = skip; /* index at insert */
1226 /* split->data = data; */
1228 /* unpin right child page */
1231 /* The split routines insert the new entry,
1232 * acquire txLock as appropriate.
1233 * return <rp> pinned and its block number <rbn>.
1235 rc = (sp->header.flag & BT_ROOT) ?
1236 dtSplitRoot(tid, ip, split, &rmp) :
1237 dtSplitPage(tid, ip, split, &rmp, &rp, &rpxd);
1243 /* smp and rmp are pinned */
1246 * parent page is not full - insert router entry in parent page
1249 BT_MARK_DIRTY(smp, ip);
1251 * acquire a transaction lock on the parent page
1253 tlck = txLock(tid, ip, smp, tlckDTREE | tlckENTRY);
1254 dtlck = (struct dt_lock *) & tlck->lock;
1255 ASSERT(dtlck->index == 0);
1256 lv = & dtlck->lv[0];
1258 /* linelock header */
1263 /* linelock stbl of non-root parent page */
1264 if (!(sp->header.flag & BT_ROOT)) {
1266 n = skip >> L2DTSLOTSIZE;
1267 lv->offset = sp->header.stblindex + n;
1269 ((sp->header.nextindex -
1270 1) >> L2DTSLOTSIZE) - n + 1;
1274 dtInsertEntry(sp, skip, &key, data, &dtlck);
1276 /* exit propagate up */
1281 /* unpin current split and its right page */
1286 * free remaining extents allocated for split
1290 pxd = &pxdlist.pxd[n];
1291 for (; n < pxdlist.maxnpxd; n++, pxd++)
1292 dbFree(ip, addressPXD(pxd), (s64) lengthPXD(pxd));
1297 /* Rollback quota allocation */
1298 if (rc && quota_allocation)
1299 dquot_free_block(ip, quota_allocation);
1310 * function: Split a non-root page of a btree.
1314 * return: 0 - success;
1316 * return split and new page pinned;
1318 static int dtSplitPage(tid_t tid, struct inode *ip, struct dtsplit * split,
1319 struct metapage ** rmpp, dtpage_t ** rpp, pxd_t * rpxdp)
1322 struct metapage *smp;
1324 struct metapage *rmp;
1325 dtpage_t *rp; /* new right page allocated */
1326 s64 rbn; /* new right page block number */
1327 struct metapage *mp;
1330 struct pxdlist *pxdlist;
1332 int skip, nextindex, half, left, nxt, off, si;
1333 struct ldtentry *ldtentry;
1334 struct idtentry *idtentry;
1339 struct dt_lock *sdtlck, *rdtlck;
1341 struct dt_lock *dtlck;
1342 struct lv *slv, *rlv, *lv;
1344 /* get split page */
1346 sp = DT_PAGE(ip, smp);
1349 * allocate the new right page for the split
1351 pxdlist = split->pxdlist;
1352 pxd = &pxdlist->pxd[pxdlist->npxd];
1354 rbn = addressPXD(pxd);
1355 rmp = get_metapage(ip, rbn, PSIZE, 1);
1359 /* Allocate blocks to quota. */
1360 rc = dquot_alloc_block(ip, lengthPXD(pxd));
1362 release_metapage(rmp);
1366 jfs_info("dtSplitPage: ip:0x%p smp:0x%p rmp:0x%p", ip, smp, rmp);
1368 BT_MARK_DIRTY(rmp, ip);
1370 * acquire a transaction lock on the new right page
1372 tlck = txLock(tid, ip, rmp, tlckDTREE | tlckNEW);
1373 rdtlck = (struct dt_lock *) & tlck->lock;
1375 rp = (dtpage_t *) rmp->data;
1377 rp->header.self = *pxd;
1379 BT_MARK_DIRTY(smp, ip);
1381 * acquire a transaction lock on the split page
1385 tlck = txLock(tid, ip, smp, tlckDTREE | tlckENTRY);
1386 sdtlck = (struct dt_lock *) & tlck->lock;
1388 /* linelock header of split page */
1389 ASSERT(sdtlck->index == 0);
1390 slv = & sdtlck->lv[0];
1396 * initialize/update sibling pointers between sp and rp
1398 nextbn = le64_to_cpu(sp->header.next);
1399 rp->header.next = cpu_to_le64(nextbn);
1400 rp->header.prev = cpu_to_le64(addressPXD(&sp->header.self));
1401 sp->header.next = cpu_to_le64(rbn);
1404 * initialize new right page
1406 rp->header.flag = sp->header.flag;
1408 /* compute sorted entry table at start of extent data area */
1409 rp->header.nextindex = 0;
1410 rp->header.stblindex = 1;
1412 n = PSIZE >> L2DTSLOTSIZE;
1413 rp->header.maxslot = n;
1414 stblsize = (n + 31) >> L2DTSLOTSIZE; /* in unit of slot */
1417 fsi = rp->header.stblindex + stblsize;
1418 rp->header.freelist = fsi;
1419 rp->header.freecnt = rp->header.maxslot - fsi;
1422 * sequential append at tail: append without split
1424 * If splitting the last page on a level because of appending
1425 * a entry to it (skip is maxentry), it's likely that the access is
1426 * sequential. Adding an empty page on the side of the level is less
1427 * work and can push the fill factor much higher than normal.
1428 * If we're wrong it's no big deal, we'll just do the split the right
1430 * (It may look like it's equally easy to do a similar hack for
1431 * reverse sorted data, that is, split the tree left,
1432 * but it's not. Be my guest.)
1434 if (nextbn == 0 && split->index == sp->header.nextindex) {
1435 /* linelock header + stbl (first slot) of new page */
1436 rlv = & rdtlck->lv[rdtlck->index];
1442 * initialize freelist of new right page
1445 for (fsi++; fsi < rp->header.maxslot; f++, fsi++)
1449 /* insert entry at the first entry of the new right page */
1450 dtInsertEntry(rp, 0, split->key, split->data, &rdtlck);
1456 * non-sequential insert (at possibly middle page)
1460 * update prev pointer of previous right sibling page;
1463 DT_GETPAGE(ip, nextbn, mp, PSIZE, p, rc);
1465 discard_metapage(rmp);
1469 BT_MARK_DIRTY(mp, ip);
1471 * acquire a transaction lock on the next page
1473 tlck = txLock(tid, ip, mp, tlckDTREE | tlckRELINK);
1474 jfs_info("dtSplitPage: tlck = 0x%p, ip = 0x%p, mp=0x%p",
1476 dtlck = (struct dt_lock *) & tlck->lock;
1478 /* linelock header of previous right sibling page */
1479 lv = & dtlck->lv[dtlck->index];
1484 p->header.prev = cpu_to_le64(rbn);
1490 * split the data between the split and right pages.
1492 skip = split->index;
1493 half = (PSIZE >> L2DTSLOTSIZE) >> 1; /* swag */
1497 * compute fill factor for split pages
1499 * <nxt> traces the next entry to move to rp
1500 * <off> traces the next entry to stay in sp
1502 stbl = (u8 *) & sp->slot[sp->header.stblindex];
1503 nextindex = sp->header.nextindex;
1504 for (nxt = off = 0; nxt < nextindex; ++off) {
1506 /* check for fill factor with new entry size */
1510 switch (sp->header.flag & BT_TYPE) {
1512 ldtentry = (struct ldtentry *) & sp->slot[si];
1514 n = NDTLEAF(ldtentry->namlen);
1516 n = NDTLEAF_LEGACY(ldtentry->
1521 idtentry = (struct idtentry *) & sp->slot[si];
1522 n = NDTINTERNAL(idtentry->namlen);
1529 ++nxt; /* advance to next entry to move in sp */
1537 /* <nxt> poins to the 1st entry to move */
1540 * move entries to right page
1542 * dtMoveEntry() initializes rp and reserves entry for insertion
1544 * split page moved out entries are linelocked;
1545 * new/right page moved in entries are linelocked;
1547 /* linelock header + stbl of new right page */
1548 rlv = & rdtlck->lv[rdtlck->index];
1553 dtMoveEntry(sp, nxt, rp, &sdtlck, &rdtlck, DO_INDEX(ip));
1555 sp->header.nextindex = nxt;
1558 * finalize freelist of new right page
1560 fsi = rp->header.freelist;
1562 for (fsi++; fsi < rp->header.maxslot; f++, fsi++)
1567 * Update directory index table for entries now in right page
1569 if ((rp->header.flag & BT_LEAF) && DO_INDEX(ip)) {
1573 stbl = DT_GETSTBL(rp);
1574 for (n = 0; n < rp->header.nextindex; n++) {
1575 ldtentry = (struct ldtentry *) & rp->slot[stbl[n]];
1576 modify_index(tid, ip, le32_to_cpu(ldtentry->index),
1577 rbn, n, &mp, &lblock);
1580 release_metapage(mp);
1584 * the skipped index was on the left page,
1587 /* insert the new entry in the split page */
1588 dtInsertEntry(sp, skip, split->key, split->data, &sdtlck);
1590 /* linelock stbl of split page */
1591 if (sdtlck->index >= sdtlck->maxcnt)
1592 sdtlck = (struct dt_lock *) txLinelock(sdtlck);
1593 slv = & sdtlck->lv[sdtlck->index];
1594 n = skip >> L2DTSLOTSIZE;
1595 slv->offset = sp->header.stblindex + n;
1597 ((sp->header.nextindex - 1) >> L2DTSLOTSIZE) - n + 1;
1601 * the skipped index was on the right page,
1604 /* adjust the skip index to reflect the new position */
1607 /* insert the new entry in the right page */
1608 dtInsertEntry(rp, skip, split->key, split->data, &rdtlck);
1622 * function: extend 1st/only directory leaf page
1626 * return: 0 - success;
1628 * return extended page pinned;
1630 static int dtExtendPage(tid_t tid,
1631 struct inode *ip, struct dtsplit * split, struct btstack * btstack)
1633 struct super_block *sb = ip->i_sb;
1635 struct metapage *smp, *pmp, *mp;
1637 struct pxdlist *pxdlist;
1640 int newstblindex, newstblsize;
1641 int oldstblindex, oldstblsize;
1644 struct btframe *parent;
1646 struct dt_lock *dtlck;
1649 struct pxd_lock *pxdlock;
1652 struct ldtentry *ldtentry;
1655 /* get page to extend */
1657 sp = DT_PAGE(ip, smp);
1659 /* get parent/root page */
1660 parent = BT_POP(btstack);
1661 DT_GETPAGE(ip, parent->bn, pmp, PSIZE, pp, rc);
1668 pxdlist = split->pxdlist;
1669 pxd = &pxdlist->pxd[pxdlist->npxd];
1672 xaddr = addressPXD(pxd);
1673 tpxd = &sp->header.self;
1674 txaddr = addressPXD(tpxd);
1675 /* in-place extension */
1676 if (xaddr == txaddr) {
1683 /* save moved extent descriptor for later free */
1684 tlck = txMaplock(tid, ip, tlckDTREE | tlckRELOCATE);
1685 pxdlock = (struct pxd_lock *) & tlck->lock;
1686 pxdlock->flag = mlckFREEPXD;
1687 pxdlock->pxd = sp->header.self;
1691 * Update directory index table to reflect new page address
1697 stbl = DT_GETSTBL(sp);
1698 for (n = 0; n < sp->header.nextindex; n++) {
1700 (struct ldtentry *) & sp->slot[stbl[n]];
1701 modify_index(tid, ip,
1702 le32_to_cpu(ldtentry->index),
1703 xaddr, n, &mp, &lblock);
1706 release_metapage(mp);
1713 sp->header.self = *pxd;
1715 jfs_info("dtExtendPage: ip:0x%p smp:0x%p sp:0x%p", ip, smp, sp);
1717 BT_MARK_DIRTY(smp, ip);
1719 * acquire a transaction lock on the extended/leaf page
1721 tlck = txLock(tid, ip, smp, tlckDTREE | type);
1722 dtlck = (struct dt_lock *) & tlck->lock;
1723 lv = & dtlck->lv[0];
1725 /* update buffer extent descriptor of extended page */
1726 xlen = lengthPXD(pxd);
1727 xsize = xlen << JFS_SBI(sb)->l2bsize;
1730 * copy old stbl to new stbl at start of extended area
1732 oldstblindex = sp->header.stblindex;
1733 oldstblsize = (sp->header.maxslot + 31) >> L2DTSLOTSIZE;
1734 newstblindex = sp->header.maxslot;
1735 n = xsize >> L2DTSLOTSIZE;
1736 newstblsize = (n + 31) >> L2DTSLOTSIZE;
1737 memcpy(&sp->slot[newstblindex], &sp->slot[oldstblindex],
1738 sp->header.nextindex);
1741 * in-line extension: linelock old area of extended page
1743 if (type == tlckEXTEND) {
1744 /* linelock header */
1750 /* linelock new stbl of extended page */
1751 lv->offset = newstblindex;
1752 lv->length = newstblsize;
1755 * relocation: linelock whole relocated area
1759 lv->length = sp->header.maxslot + newstblsize;
1764 sp->header.maxslot = n;
1765 sp->header.stblindex = newstblindex;
1766 /* sp->header.nextindex remains the same */
1769 * add old stbl region at head of freelist
1773 last = sp->header.freelist;
1774 for (n = 0; n < oldstblsize; n++, fsi++, f++) {
1778 sp->header.freelist = last;
1779 sp->header.freecnt += oldstblsize;
1782 * append free region of newly extended area at tail of freelist
1784 /* init free region of newly extended area */
1785 fsi = n = newstblindex + newstblsize;
1787 for (fsi++; fsi < sp->header.maxslot; f++, fsi++)
1791 /* append new free region at tail of old freelist */
1792 fsi = sp->header.freelist;
1794 sp->header.freelist = n;
1799 } while (fsi != -1);
1804 sp->header.freecnt += sp->header.maxslot - n;
1807 * insert the new entry
1809 dtInsertEntry(sp, split->index, split->key, split->data, &dtlck);
1811 BT_MARK_DIRTY(pmp, ip);
1813 * linelock any freeslots residing in old extent
1815 if (type == tlckEXTEND) {
1816 n = sp->header.maxslot >> 2;
1817 if (sp->header.freelist < n)
1818 dtLinelockFreelist(sp, n, &dtlck);
1822 * update parent entry on the parent/root page
1825 * acquire a transaction lock on the parent/root page
1827 tlck = txLock(tid, ip, pmp, tlckDTREE | tlckENTRY);
1828 dtlck = (struct dt_lock *) & tlck->lock;
1829 lv = & dtlck->lv[dtlck->index];
1831 /* linelock parent entry - 1st slot */
1836 /* update the parent pxd for page extension */
1837 tpxd = (pxd_t *) & pp->slot[1];
1849 * split the full root page into
1850 * original/root/split page and new right page
1851 * i.e., root remains fixed in tree anchor (inode) and
1852 * the root is copied to a single new right child page
1853 * since root page << non-root page, and
1854 * the split root page contains a single entry for the
1855 * new right child page.
1859 * return: 0 - success;
1861 * return new page pinned;
1863 static int dtSplitRoot(tid_t tid,
1864 struct inode *ip, struct dtsplit * split, struct metapage ** rmpp)
1866 struct super_block *sb = ip->i_sb;
1867 struct metapage *smp;
1869 struct metapage *rmp;
1876 int fsi, stblsize, n;
1879 struct pxdlist *pxdlist;
1881 struct dt_lock *dtlck;
1886 /* get split root page */
1888 sp = &JFS_IP(ip)->i_dtroot;
1891 * allocate/initialize a single (right) child page
1893 * N.B. at first split, a one (or two) block to fit new entry
1894 * is allocated; at subsequent split, a full page is allocated;
1896 pxdlist = split->pxdlist;
1897 pxd = &pxdlist->pxd[pxdlist->npxd];
1899 rbn = addressPXD(pxd);
1900 xlen = lengthPXD(pxd);
1901 xsize = xlen << JFS_SBI(sb)->l2bsize;
1902 rmp = get_metapage(ip, rbn, xsize, 1);
1908 /* Allocate blocks to quota. */
1909 rc = dquot_alloc_block(ip, lengthPXD(pxd));
1911 release_metapage(rmp);
1915 BT_MARK_DIRTY(rmp, ip);
1917 * acquire a transaction lock on the new right page
1919 tlck = txLock(tid, ip, rmp, tlckDTREE | tlckNEW);
1920 dtlck = (struct dt_lock *) & tlck->lock;
1923 (sp->header.flag & BT_LEAF) ? BT_LEAF : BT_INTERNAL;
1924 rp->header.self = *pxd;
1926 /* initialize sibling pointers */
1927 rp->header.next = 0;
1928 rp->header.prev = 0;
1931 * move in-line root page into new right page extent
1933 /* linelock header + copied entries + new stbl (1st slot) in new page */
1934 ASSERT(dtlck->index == 0);
1935 lv = & dtlck->lv[0];
1937 lv->length = 10; /* 1 + 8 + 1 */
1940 n = xsize >> L2DTSLOTSIZE;
1941 rp->header.maxslot = n;
1942 stblsize = (n + 31) >> L2DTSLOTSIZE;
1944 /* copy old stbl to new stbl at start of extended area */
1945 rp->header.stblindex = DTROOTMAXSLOT;
1946 stbl = (s8 *) & rp->slot[DTROOTMAXSLOT];
1947 memcpy(stbl, sp->header.stbl, sp->header.nextindex);
1948 rp->header.nextindex = sp->header.nextindex;
1950 /* copy old data area to start of new data area */
1951 memcpy(&rp->slot[1], &sp->slot[1], IDATASIZE);
1954 * append free region of newly extended area at tail of freelist
1956 /* init free region of newly extended area */
1957 fsi = n = DTROOTMAXSLOT + stblsize;
1959 for (fsi++; fsi < rp->header.maxslot; f++, fsi++)
1963 /* append new free region at tail of old freelist */
1964 fsi = sp->header.freelist;
1966 rp->header.freelist = n;
1968 rp->header.freelist = fsi;
1973 } while (fsi != -1);
1978 rp->header.freecnt = sp->header.freecnt + rp->header.maxslot - n;
1981 * Update directory index table for entries now in right page
1983 if ((rp->header.flag & BT_LEAF) && DO_INDEX(ip)) {
1985 struct metapage *mp = NULL;
1986 struct ldtentry *ldtentry;
1988 stbl = DT_GETSTBL(rp);
1989 for (n = 0; n < rp->header.nextindex; n++) {
1990 ldtentry = (struct ldtentry *) & rp->slot[stbl[n]];
1991 modify_index(tid, ip, le32_to_cpu(ldtentry->index),
1992 rbn, n, &mp, &lblock);
1995 release_metapage(mp);
1998 * insert the new entry into the new right/child page
1999 * (skip index in the new right page will not change)
2001 dtInsertEntry(rp, split->index, split->key, split->data, &dtlck);
2004 * reset parent/root page
2006 * set the 1st entry offset to 0, which force the left-most key
2007 * at any level of the tree to be less than any search key.
2009 * The btree comparison code guarantees that the left-most key on any
2010 * level of the tree is never used, so it doesn't need to be filled in.
2012 BT_MARK_DIRTY(smp, ip);
2014 * acquire a transaction lock on the root page (in-memory inode)
2016 tlck = txLock(tid, ip, smp, tlckDTREE | tlckNEW | tlckBTROOT);
2017 dtlck = (struct dt_lock *) & tlck->lock;
2020 ASSERT(dtlck->index == 0);
2021 lv = & dtlck->lv[0];
2023 lv->length = DTROOTMAXSLOT;
2026 /* update page header of root */
2027 if (sp->header.flag & BT_LEAF) {
2028 sp->header.flag &= ~BT_LEAF;
2029 sp->header.flag |= BT_INTERNAL;
2032 /* init the first entry */
2033 s = (struct idtentry *) & sp->slot[DTENTRYSTART];
2039 stbl = sp->header.stbl;
2040 stbl[0] = DTENTRYSTART;
2041 sp->header.nextindex = 1;
2044 fsi = DTENTRYSTART + 1;
2047 /* init free region of remaining area */
2048 for (fsi++; fsi < DTROOTMAXSLOT; f++, fsi++)
2052 sp->header.freelist = DTENTRYSTART + 1;
2053 sp->header.freecnt = DTROOTMAXSLOT - (DTENTRYSTART + 1);
2064 * function: delete the entry(s) referenced by a key.
2070 int dtDelete(tid_t tid,
2071 struct inode *ip, struct component_name * key, ino_t * ino, int flag)
2075 struct metapage *mp, *imp;
2078 struct btstack btstack;
2079 struct dt_lock *dtlck;
2083 struct ldtentry *ldtentry;
2085 u32 table_index, next_index;
2086 struct metapage *nmp;
2090 * search for the entry to delete:
2092 * dtSearch() returns (leaf page pinned, index at which to delete).
2094 if ((rc = dtSearch(ip, key, ino, &btstack, flag)))
2097 /* retrieve search result */
2098 DT_GETSEARCH(ip, btstack.top, bn, mp, p, index);
2101 * We need to find put the index of the next entry into the
2102 * directory index table in order to resume a readdir from this
2106 stbl = DT_GETSTBL(p);
2107 ldtentry = (struct ldtentry *) & p->slot[stbl[index]];
2108 table_index = le32_to_cpu(ldtentry->index);
2109 if (index == (p->header.nextindex - 1)) {
2111 * Last entry in this leaf page
2113 if ((p->header.flag & BT_ROOT)
2114 || (p->header.next == 0))
2117 /* Read next leaf page */
2118 DT_GETPAGE(ip, le64_to_cpu(p->header.next),
2119 nmp, PSIZE, np, rc);
2123 stbl = DT_GETSTBL(np);
2125 (struct ldtentry *) & np->
2128 le32_to_cpu(ldtentry->index);
2134 (struct ldtentry *) & p->slot[stbl[index + 1]];
2135 next_index = le32_to_cpu(ldtentry->index);
2137 free_index(tid, ip, table_index, next_index);
2140 * the leaf page becomes empty, delete the page
2142 if (p->header.nextindex == 1) {
2143 /* delete empty page */
2144 rc = dtDeleteUp(tid, ip, mp, p, &btstack);
2147 * the leaf page has other entries remaining:
2149 * delete the entry from the leaf page.
2152 BT_MARK_DIRTY(mp, ip);
2154 * acquire a transaction lock on the leaf page
2156 tlck = txLock(tid, ip, mp, tlckDTREE | tlckENTRY);
2157 dtlck = (struct dt_lock *) & tlck->lock;
2160 * Do not assume that dtlck->index will be zero. During a
2161 * rename within a directory, this transaction may have
2162 * modified this page already when adding the new entry.
2165 /* linelock header */
2166 if (dtlck->index >= dtlck->maxcnt)
2167 dtlck = (struct dt_lock *) txLinelock(dtlck);
2168 lv = & dtlck->lv[dtlck->index];
2173 /* linelock stbl of non-root leaf page */
2174 if (!(p->header.flag & BT_ROOT)) {
2175 if (dtlck->index >= dtlck->maxcnt)
2176 dtlck = (struct dt_lock *) txLinelock(dtlck);
2177 lv = & dtlck->lv[dtlck->index];
2178 i = index >> L2DTSLOTSIZE;
2179 lv->offset = p->header.stblindex + i;
2181 ((p->header.nextindex - 1) >> L2DTSLOTSIZE) -
2186 /* free the leaf entry */
2187 dtDeleteEntry(p, index, &dtlck);
2190 * Update directory index table for entries moved in stbl
2192 if (DO_INDEX(ip) && index < p->header.nextindex) {
2196 stbl = DT_GETSTBL(p);
2197 for (i = index; i < p->header.nextindex; i++) {
2199 (struct ldtentry *) & p->slot[stbl[i]];
2200 modify_index(tid, ip,
2201 le32_to_cpu(ldtentry->index),
2202 bn, i, &imp, &lblock);
2205 release_metapage(imp);
2219 * free empty pages as propagating deletion up the tree
2225 static int dtDeleteUp(tid_t tid, struct inode *ip,
2226 struct metapage * fmp, dtpage_t * fp, struct btstack * btstack)
2229 struct metapage *mp;
2231 int index, nextindex;
2233 struct btframe *parent;
2234 struct dt_lock *dtlck;
2237 struct pxd_lock *pxdlock;
2241 * keep the root leaf page which has become empty
2243 if (BT_IS_ROOT(fmp)) {
2247 * dtInitRoot() acquires txlock on the root
2249 dtInitRoot(tid, ip, PARENT(ip));
2257 * free the non-root leaf page
2260 * acquire a transaction lock on the page
2262 * write FREEXTENT|NOREDOPAGE log record
2263 * N.B. linelock is overlaid as freed extent descriptor, and
2264 * the buffer page is freed;
2266 tlck = txMaplock(tid, ip, tlckDTREE | tlckFREE);
2267 pxdlock = (struct pxd_lock *) & tlck->lock;
2268 pxdlock->flag = mlckFREEPXD;
2269 pxdlock->pxd = fp->header.self;
2272 /* update sibling pointers */
2273 if ((rc = dtRelink(tid, ip, fp))) {
2278 xlen = lengthPXD(&fp->header.self);
2280 /* Free quota allocation. */
2281 dquot_free_block(ip, xlen);
2283 /* free/invalidate its buffer page */
2284 discard_metapage(fmp);
2287 * propagate page deletion up the directory tree
2289 * If the delete from the parent page makes it empty,
2290 * continue all the way up the tree.
2291 * stop if the root page is reached (which is never deleted) or
2292 * if the entry deletion does not empty the page.
2294 while ((parent = BT_POP(btstack)) != NULL) {
2295 /* pin the parent page <sp> */
2296 DT_GETPAGE(ip, parent->bn, mp, PSIZE, p, rc);
2301 * free the extent of the child page deleted
2303 index = parent->index;
2306 * delete the entry for the child page from parent
2308 nextindex = p->header.nextindex;
2311 * the parent has the single entry being deleted:
2313 * free the parent page which has become empty.
2315 if (nextindex == 1) {
2317 * keep the root internal page which has become empty
2319 if (p->header.flag & BT_ROOT) {
2323 * dtInitRoot() acquires txlock on the root
2325 dtInitRoot(tid, ip, PARENT(ip));
2332 * free the parent page
2336 * acquire a transaction lock on the page
2338 * write FREEXTENT|NOREDOPAGE log record
2342 tlckDTREE | tlckFREE);
2343 pxdlock = (struct pxd_lock *) & tlck->lock;
2344 pxdlock->flag = mlckFREEPXD;
2345 pxdlock->pxd = p->header.self;
2348 /* update sibling pointers */
2349 if ((rc = dtRelink(tid, ip, p))) {
2354 xlen = lengthPXD(&p->header.self);
2356 /* Free quota allocation */
2357 dquot_free_block(ip, xlen);
2359 /* free/invalidate its buffer page */
2360 discard_metapage(mp);
2368 * the parent has other entries remaining:
2370 * delete the router entry from the parent page.
2372 BT_MARK_DIRTY(mp, ip);
2374 * acquire a transaction lock on the page
2376 * action: router entry deletion
2378 tlck = txLock(tid, ip, mp, tlckDTREE | tlckENTRY);
2379 dtlck = (struct dt_lock *) & tlck->lock;
2381 /* linelock header */
2382 if (dtlck->index >= dtlck->maxcnt)
2383 dtlck = (struct dt_lock *) txLinelock(dtlck);
2384 lv = & dtlck->lv[dtlck->index];
2389 /* linelock stbl of non-root leaf page */
2390 if (!(p->header.flag & BT_ROOT)) {
2391 if (dtlck->index < dtlck->maxcnt)
2394 dtlck = (struct dt_lock *) txLinelock(dtlck);
2395 lv = & dtlck->lv[0];
2397 i = index >> L2DTSLOTSIZE;
2398 lv->offset = p->header.stblindex + i;
2400 ((p->header.nextindex - 1) >> L2DTSLOTSIZE) -
2405 /* free the router entry */
2406 dtDeleteEntry(p, index, &dtlck);
2408 /* reset key of new leftmost entry of level (for consistency) */
2410 ((p->header.flag & BT_ROOT) || p->header.prev == 0))
2411 dtTruncateEntry(p, 0, &dtlck);
2413 /* unpin the parent page */
2416 /* exit propagation up */
2421 ip->i_size -= PSIZE;
2430 * link around a freed page.
2433 * fp: page to be freed
2437 static int dtRelink(tid_t tid, struct inode *ip, dtpage_t * p)
2440 struct metapage *mp;
2443 struct dt_lock *dtlck;
2446 nextbn = le64_to_cpu(p->header.next);
2447 prevbn = le64_to_cpu(p->header.prev);
2449 /* update prev pointer of the next page */
2451 DT_GETPAGE(ip, nextbn, mp, PSIZE, p, rc);
2455 BT_MARK_DIRTY(mp, ip);
2457 * acquire a transaction lock on the next page
2459 * action: update prev pointer;
2461 tlck = txLock(tid, ip, mp, tlckDTREE | tlckRELINK);
2462 jfs_info("dtRelink nextbn: tlck = 0x%p, ip = 0x%p, mp=0x%p",
2464 dtlck = (struct dt_lock *) & tlck->lock;
2466 /* linelock header */
2467 if (dtlck->index >= dtlck->maxcnt)
2468 dtlck = (struct dt_lock *) txLinelock(dtlck);
2469 lv = & dtlck->lv[dtlck->index];
2474 p->header.prev = cpu_to_le64(prevbn);
2478 /* update next pointer of the previous page */
2480 DT_GETPAGE(ip, prevbn, mp, PSIZE, p, rc);
2484 BT_MARK_DIRTY(mp, ip);
2486 * acquire a transaction lock on the prev page
2488 * action: update next pointer;
2490 tlck = txLock(tid, ip, mp, tlckDTREE | tlckRELINK);
2491 jfs_info("dtRelink prevbn: tlck = 0x%p, ip = 0x%p, mp=0x%p",
2493 dtlck = (struct dt_lock *) & tlck->lock;
2495 /* linelock header */
2496 if (dtlck->index >= dtlck->maxcnt)
2497 dtlck = (struct dt_lock *) txLinelock(dtlck);
2498 lv = & dtlck->lv[dtlck->index];
2503 p->header.next = cpu_to_le64(nextbn);
2514 * initialize directory root (inline in inode)
2516 void dtInitRoot(tid_t tid, struct inode *ip, u32 idotdot)
2518 struct jfs_inode_info *jfs_ip = JFS_IP(ip);
2523 struct dt_lock *dtlck;
2528 * If this was previously an non-empty directory, we need to remove
2529 * the old directory table.
2532 if (!jfs_dirtable_inline(ip)) {
2533 struct tblock *tblk = tid_to_tblock(tid);
2535 * We're playing games with the tid's xflag. If
2536 * we're removing a regular file, the file's xtree
2537 * is committed with COMMIT_PMAP, but we always
2538 * commit the directories xtree with COMMIT_PWMAP.
2540 xflag_save = tblk->xflag;
2543 * xtTruncate isn't guaranteed to fully truncate
2544 * the xtree. The caller needs to check i_size
2545 * after committing the transaction to see if
2546 * additional truncation is needed. The
2547 * COMMIT_Stale flag tells caller that we
2548 * initiated the truncation.
2550 xtTruncate(tid, ip, 0, COMMIT_PWMAP);
2551 set_cflag(COMMIT_Stale, ip);
2553 tblk->xflag = xflag_save;
2557 jfs_ip->next_index = 2;
2559 ip->i_size = IDATASIZE;
2562 * acquire a transaction lock on the root
2564 * action: directory initialization;
2566 tlck = txLock(tid, ip, (struct metapage *) & jfs_ip->bxflag,
2567 tlckDTREE | tlckENTRY | tlckBTROOT);
2568 dtlck = (struct dt_lock *) & tlck->lock;
2571 ASSERT(dtlck->index == 0);
2572 lv = & dtlck->lv[0];
2574 lv->length = DTROOTMAXSLOT;
2577 p = &jfs_ip->i_dtroot;
2579 p->header.flag = DXD_INDEX | BT_ROOT | BT_LEAF;
2581 p->header.nextindex = 0;
2587 /* init data area of root */
2588 for (fsi++; fsi < DTROOTMAXSLOT; f++, fsi++)
2592 p->header.freelist = 1;
2593 p->header.freecnt = 8;
2595 /* init '..' entry */
2596 p->header.idotdot = cpu_to_le32(idotdot);
2602 * add_missing_indices()
2604 * function: Fix dtree page in which one or more entries has an invalid index.
2605 * fsck.jfs should really fix this, but it currently does not.
2606 * Called from jfs_readdir when bad index is detected.
2608 static void add_missing_indices(struct inode *inode, s64 bn)
2611 struct dt_lock *dtlck;
2615 struct metapage *mp;
2622 tid = txBegin(inode->i_sb, 0);
2624 DT_GETPAGE(inode, bn, mp, PSIZE, p, rc);
2627 printk(KERN_ERR "DT_GETPAGE failed!\n");
2630 BT_MARK_DIRTY(mp, inode);
2632 ASSERT(p->header.flag & BT_LEAF);
2634 tlck = txLock(tid, inode, mp, tlckDTREE | tlckENTRY);
2636 tlck->type |= tlckBTROOT;
2638 dtlck = (struct dt_lock *) &tlck->lock;
2640 stbl = DT_GETSTBL(p);
2641 for (i = 0; i < p->header.nextindex; i++) {
2642 d = (struct ldtentry *) &p->slot[stbl[i]];
2643 index = le32_to_cpu(d->index);
2644 if ((index < 2) || (index >= JFS_IP(inode)->next_index)) {
2645 d->index = cpu_to_le32(add_index(tid, inode, bn, i));
2646 if (dtlck->index >= dtlck->maxcnt)
2647 dtlck = (struct dt_lock *) txLinelock(dtlck);
2648 lv = &dtlck->lv[dtlck->index];
2649 lv->offset = stbl[i];
2656 (void) txCommit(tid, 1, &inode, 0);
2662 * Buffer to hold directory entry info while traversing a dtree page
2663 * before being fed to the filldir function
2673 * function to determine next variable-sized jfs_dirent in buffer
2675 static inline struct jfs_dirent *next_jfs_dirent(struct jfs_dirent *dirent)
2677 return (struct jfs_dirent *)
2679 ((sizeof (struct jfs_dirent) + dirent->name_len + 1 +
2680 sizeof (loff_t) - 1) &
2681 ~(sizeof (loff_t) - 1)));
2687 * function: read directory entries sequentially
2688 * from the specified entry offset
2692 * return: offset = (pn, index) of start entry
2693 * of next jfs_readdir()/dtRead()
2695 int jfs_readdir(struct file *file, struct dir_context *ctx)
2697 struct inode *ip = file_inode(file);
2698 struct nls_table *codepage = JFS_SBI(ip->i_sb)->nls_tab;
2700 loff_t dtpos; /* legacy OS/2 style position */
2705 } *dtoffset = (struct dtoffset *) &dtpos;
2707 struct metapage *mp;
2711 struct btstack btstack;
2715 int d_namleft, len, outlen;
2716 unsigned long dirent_buf;
2720 uint loop_count = 0;
2721 struct jfs_dirent *jfs_dirent;
2723 int overflow, fix_page, page_fixed = 0;
2724 static int unique_pos = 2; /* If we can't fix broken index */
2726 if (ctx->pos == DIREND)
2731 * persistent index is stored in directory entries.
2732 * Special cases: 0 = .
2734 * -1 = End of directory
2738 dir_index = (u32) ctx->pos;
2741 * NFSv4 reserves cookies 1 and 2 for . and .. so the value
2742 * we return to the vfs is one greater than the one we use
2748 if (dir_index > 1) {
2749 struct dir_table_slot dirtab_slot;
2752 (dir_index >= JFS_IP(ip)->next_index)) {
2753 /* Stale position. Directory has shrunk */
2758 rc = read_index(ip, dir_index, &dirtab_slot);
2763 if (dirtab_slot.flag == DIR_INDEX_FREE) {
2764 if (loop_count++ > JFS_IP(ip)->next_index) {
2765 jfs_err("jfs_readdir detected infinite loop!");
2769 dir_index = le32_to_cpu(dirtab_slot.addr2);
2770 if (dir_index == -1) {
2776 bn = addressDTS(&dirtab_slot);
2777 index = dirtab_slot.slot;
2778 DT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
2783 if (p->header.flag & BT_INTERNAL) {
2784 jfs_err("jfs_readdir: bad index table");
2790 if (dir_index == 0) {
2795 if (!dir_emit(ctx, ".", 1, ip->i_ino, DT_DIR))
2802 if (!dir_emit(ctx, "..", 2, PARENT(ip), DT_DIR))
2806 * Find first entry of left-most leaf
2813 if ((rc = dtReadFirst(ip, &btstack)))
2816 DT_GETSEARCH(ip, btstack.top, bn, mp, p, index);
2820 * Legacy filesystem - OS/2 & Linux JFS < 0.3.6
2822 * pn = 0; index = 1: First entry "."
2823 * pn = 0; index = 2: Second entry ".."
2824 * pn > 0: Real entries, pn=1 -> leftmost page
2825 * pn = index = -1: No more entries
2829 /* build "." entry */
2831 if (!dir_emit(ctx, ".", 1, ip->i_ino, DT_DIR))
2833 dtoffset->index = 2;
2837 if (dtoffset->pn == 0) {
2838 if (dtoffset->index == 2) {
2839 /* build ".." entry */
2840 if (!dir_emit(ctx, "..", 2, PARENT(ip), DT_DIR))
2843 jfs_err("jfs_readdir called with invalid offset!");
2846 dtoffset->index = 0;
2855 if ((rc = dtReadNext(ip, &ctx->pos, &btstack))) {
2856 jfs_err("jfs_readdir: unexpected rc = %d from dtReadNext",
2861 /* get start leaf page and index */
2862 DT_GETSEARCH(ip, btstack.top, bn, mp, p, index);
2864 /* offset beyond directory eof ? */
2871 dirent_buf = __get_free_page(GFP_KERNEL);
2872 if (dirent_buf == 0) {
2874 jfs_warn("jfs_readdir: __get_free_page failed!");
2880 jfs_dirent = (struct jfs_dirent *) dirent_buf;
2882 overflow = fix_page = 0;
2884 stbl = DT_GETSTBL(p);
2886 for (i = index; i < p->header.nextindex; i++) {
2887 d = (struct ldtentry *) & p->slot[stbl[i]];
2889 if (((long) jfs_dirent + d->namlen + 1) >
2890 (dirent_buf + PAGE_SIZE)) {
2891 /* DBCS codepages could overrun dirent_buf */
2897 d_namleft = d->namlen;
2898 name_ptr = jfs_dirent->name;
2899 jfs_dirent->ino = le32_to_cpu(d->inumber);
2902 len = min(d_namleft, DTLHDRDATALEN);
2903 jfs_dirent->position = le32_to_cpu(d->index);
2905 * d->index should always be valid, but it
2906 * isn't. fsck.jfs doesn't create the
2907 * directory index for the lost+found
2908 * directory. Rather than let it go,
2909 * we can try to fix it.
2911 if ((jfs_dirent->position < 2) ||
2912 (jfs_dirent->position >=
2913 JFS_IP(ip)->next_index)) {
2914 if (!page_fixed && !isReadOnly(ip)) {
2917 * setting overflow and setting
2918 * index to i will cause the
2919 * same page to be processed
2920 * again starting here
2926 jfs_dirent->position = unique_pos++;
2929 * We add 1 to the index because we may
2930 * use a value of 2 internally, and NFSv4
2931 * doesn't like that.
2933 jfs_dirent->position++;
2935 jfs_dirent->position = dtpos;
2936 len = min(d_namleft, DTLHDRDATALEN_LEGACY);
2939 /* copy the name of head/only segment */
2940 outlen = jfs_strfromUCS_le(name_ptr, d->name, len,
2942 jfs_dirent->name_len = outlen;
2944 /* copy name in the additional segment(s) */
2947 t = (struct dtslot *) & p->slot[next];
2951 if (d_namleft == 0) {
2953 "JFS:Dtree error: ino = %ld, bn=%lld, index = %d\n",
2959 len = min(d_namleft, DTSLOTDATALEN);
2960 outlen = jfs_strfromUCS_le(name_ptr, t->name,
2962 jfs_dirent->name_len += outlen;
2968 jfs_dirent = next_jfs_dirent(jfs_dirent);
2975 /* Point to next leaf page */
2976 if (p->header.flag & BT_ROOT)
2979 bn = le64_to_cpu(p->header.next);
2981 /* update offset (pn:index) for new page */
2984 dtoffset->index = 0;
2990 /* unpin previous leaf page */
2993 jfs_dirent = (struct jfs_dirent *) dirent_buf;
2994 while (jfs_dirents--) {
2995 ctx->pos = jfs_dirent->position;
2996 if (!dir_emit(ctx, jfs_dirent->name,
2997 jfs_dirent->name_len,
2998 jfs_dirent->ino, DT_UNKNOWN))
3000 jfs_dirent = next_jfs_dirent(jfs_dirent);
3004 add_missing_indices(ip, bn);
3008 if (!overflow && (bn == 0)) {
3013 DT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
3015 free_page(dirent_buf);
3021 free_page(dirent_buf);
3030 * function: get the leftmost page of the directory
3032 static int dtReadFirst(struct inode *ip, struct btstack * btstack)
3036 int psize = 288; /* initial in-line directory */
3037 struct metapage *mp;
3040 struct btframe *btsp;
3043 BT_CLR(btstack); /* reset stack */
3046 * descend leftmost path of the tree
3048 * by convention, root bn = 0.
3051 DT_GETPAGE(ip, bn, mp, psize, p, rc);
3056 * leftmost leaf page
3058 if (p->header.flag & BT_LEAF) {
3059 /* return leftmost entry */
3060 btsp = btstack->top;
3069 * descend down to leftmost child page
3071 if (BT_STACK_FULL(btstack)) {
3073 jfs_error(ip->i_sb, "btstack overrun\n");
3074 BT_STACK_DUMP(btstack);
3077 /* push (bn, index) of the parent page/entry */
3078 BT_PUSH(btstack, bn, 0);
3080 /* get the leftmost entry */
3081 stbl = DT_GETSTBL(p);
3082 xd = (pxd_t *) & p->slot[stbl[0]];
3084 /* get the child page block address */
3085 bn = addressPXD(xd);
3086 psize = lengthPXD(xd) << JFS_SBI(ip->i_sb)->l2bsize;
3088 /* unpin the parent page */
3097 * function: get the page of the specified offset (pn:index)
3099 * return: if (offset > eof), bn = -1;
3101 * note: if index > nextindex of the target leaf page,
3102 * start with 1st entry of next leaf page;
3104 static int dtReadNext(struct inode *ip, loff_t * offset,
3105 struct btstack * btstack)
3112 } *dtoffset = (struct dtoffset *) offset;
3114 struct metapage *mp;
3119 struct btframe *btsp, *parent;
3123 * get leftmost leaf page pinned
3125 if ((rc = dtReadFirst(ip, btstack)))
3129 DT_GETSEARCH(ip, btstack->top, bn, mp, p, index);
3131 /* get the start offset (pn:index) */
3132 pn = dtoffset->pn - 1; /* Now pn = 0 represents leftmost leaf */
3133 index = dtoffset->index;
3135 /* start at leftmost page ? */
3137 /* offset beyond eof ? */
3138 if (index < p->header.nextindex)
3141 if (p->header.flag & BT_ROOT) {
3146 /* start with 1st entry of next leaf page */
3148 dtoffset->index = index = 0;
3152 /* start at non-leftmost page: scan parent pages for large pn */
3153 if (p->header.flag & BT_ROOT) {
3158 /* start after next leaf page ? */
3162 /* get leaf page pn = 1 */
3164 bn = le64_to_cpu(p->header.next);
3166 /* unpin leaf page */
3169 /* offset beyond eof ? */
3178 * scan last internal page level to get target leaf page
3181 /* unpin leftmost leaf page */
3184 /* get left most parent page */
3185 btsp = btstack->top;
3188 DT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
3192 /* scan parent pages at last internal page level */
3193 while (pn >= p->header.nextindex) {
3194 pn -= p->header.nextindex;
3196 /* get next parent page address */
3197 bn = le64_to_cpu(p->header.next);
3199 /* unpin current parent page */
3202 /* offset beyond eof ? */
3208 /* get next parent page */
3209 DT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
3213 /* update parent page stack frame */
3217 /* get leaf page address */
3218 stbl = DT_GETSTBL(p);
3219 xd = (pxd_t *) & p->slot[stbl[pn]];
3220 bn = addressPXD(xd);
3222 /* unpin parent page */
3226 * get target leaf page
3229 DT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
3234 * leaf page has been completed:
3235 * start with 1st entry of next leaf page
3237 if (index >= p->header.nextindex) {
3238 bn = le64_to_cpu(p->header.next);
3240 /* unpin leaf page */
3243 /* offset beyond eof ? */
3249 /* get next leaf page */
3250 DT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
3254 /* start with 1st entry of next leaf page */
3256 dtoffset->index = 0;
3260 /* return target leaf page pinned */
3261 btsp = btstack->top;
3263 btsp->index = dtoffset->index;
3273 * function: compare search key with an internal entry
3276 * < 0 if k is < record
3277 * = 0 if k is = record
3278 * > 0 if k is > record
3280 static int dtCompare(struct component_name * key, /* search key */
3281 dtpage_t * p, /* directory page */
3283 { /* entry slot index */
3286 int klen, namlen, len, rc;
3287 struct idtentry *ih;
3291 * force the left-most key on internal pages, at any level of
3292 * the tree, to be less than any search key.
3293 * this obviates having to update the leftmost key on an internal
3294 * page when the user inserts a new key in the tree smaller than
3295 * anything that has been stored.
3297 * (? if/when dtSearch() narrows down to 1st entry (index = 0),
3298 * at any internal page at any level of the tree,
3299 * it descends to child of the entry anyway -
3300 * ? make the entry as min size dummy entry)
3302 * if (e->index == 0 && h->prevpg == P_INVALID && !(h->flags & BT_LEAF))
3309 ih = (struct idtentry *) & p->slot[si];
3312 namlen = ih->namlen;
3313 len = min(namlen, DTIHDRDATALEN);
3315 /* compare with head/only segment */
3316 len = min(klen, len);
3317 if ((rc = UniStrncmp_le(kname, name, len)))
3323 /* compare with additional segment(s) */
3325 while (klen > 0 && namlen > 0) {
3326 /* compare with next name segment */
3327 t = (struct dtslot *) & p->slot[si];
3328 len = min(namlen, DTSLOTDATALEN);
3329 len = min(klen, len);
3331 if ((rc = UniStrncmp_le(kname, name, len)))
3340 return (klen - namlen);
3349 * function: compare search key with an (leaf/internal) entry
3352 * < 0 if k is < record
3353 * = 0 if k is = record
3354 * > 0 if k is > record
3356 static int ciCompare(struct component_name * key, /* search key */
3357 dtpage_t * p, /* directory page */
3358 int si, /* entry slot index */
3363 int klen, namlen, len, rc;
3364 struct ldtentry *lh;
3365 struct idtentry *ih;
3370 * force the left-most key on internal pages, at any level of
3371 * the tree, to be less than any search key.
3372 * this obviates having to update the leftmost key on an internal
3373 * page when the user inserts a new key in the tree smaller than
3374 * anything that has been stored.
3376 * (? if/when dtSearch() narrows down to 1st entry (index = 0),
3377 * at any internal page at any level of the tree,
3378 * it descends to child of the entry anyway -
3379 * ? make the entry as min size dummy entry)
3381 * if (e->index == 0 && h->prevpg == P_INVALID && !(h->flags & BT_LEAF))
3391 if (p->header.flag & BT_LEAF) {
3392 lh = (struct ldtentry *) & p->slot[si];
3395 namlen = lh->namlen;
3396 if (flag & JFS_DIR_INDEX)
3397 len = min(namlen, DTLHDRDATALEN);
3399 len = min(namlen, DTLHDRDATALEN_LEGACY);
3402 * internal page entry
3405 ih = (struct idtentry *) & p->slot[si];
3408 namlen = ih->namlen;
3409 len = min(namlen, DTIHDRDATALEN);
3412 /* compare with head/only segment */
3413 len = min(klen, len);
3414 for (i = 0; i < len; i++, kname++, name++) {
3415 /* only uppercase if case-insensitive support is on */
3416 if ((flag & JFS_OS2) == JFS_OS2)
3417 x = UniToupper(le16_to_cpu(*name));
3419 x = le16_to_cpu(*name);
3420 if ((rc = *kname - x))
3427 /* compare with additional segment(s) */
3428 while (klen > 0 && namlen > 0) {
3429 /* compare with next name segment */
3430 t = (struct dtslot *) & p->slot[si];
3431 len = min(namlen, DTSLOTDATALEN);
3432 len = min(klen, len);
3434 for (i = 0; i < len; i++, kname++, name++) {
3435 /* only uppercase if case-insensitive support is on */
3436 if ((flag & JFS_OS2) == JFS_OS2)
3437 x = UniToupper(le16_to_cpu(*name));
3439 x = le16_to_cpu(*name);
3441 if ((rc = *kname - x))
3450 return (klen - namlen);
3455 * ciGetLeafPrefixKey()
3457 * function: compute prefix of suffix compression
3458 * from two adjacent leaf entries
3459 * across page boundary
3461 * return: non-zero on error
3464 static int ciGetLeafPrefixKey(dtpage_t * lp, int li, dtpage_t * rp,
3465 int ri, struct component_name * key, int flag)
3468 wchar_t *pl, *pr, *kname;
3469 struct component_name lkey;
3470 struct component_name rkey;
3472 lkey.name = kmalloc_array(JFS_NAME_MAX + 1, sizeof(wchar_t),
3474 if (lkey.name == NULL)
3477 rkey.name = kmalloc_array(JFS_NAME_MAX + 1, sizeof(wchar_t),
3479 if (rkey.name == NULL) {
3484 /* get left and right key */
3485 dtGetKey(lp, li, &lkey, flag);
3486 lkey.name[lkey.namlen] = 0;
3488 if ((flag & JFS_OS2) == JFS_OS2)
3491 dtGetKey(rp, ri, &rkey, flag);
3492 rkey.name[rkey.namlen] = 0;
3495 if ((flag & JFS_OS2) == JFS_OS2)
3498 /* compute prefix */
3501 namlen = min(lkey.namlen, rkey.namlen);
3502 for (pl = lkey.name, pr = rkey.name;
3503 namlen; pl++, pr++, namlen--, klen++, kname++) {
3506 key->namlen = klen + 1;
3511 /* l->namlen <= r->namlen since l <= r */
3512 if (lkey.namlen < rkey.namlen) {
3514 key->namlen = klen + 1;
3515 } else /* l->namelen == r->namelen */
3529 * function: get key of the entry
3531 static void dtGetKey(dtpage_t * p, int i, /* entry index */
3532 struct component_name * key, int flag)
3536 struct ldtentry *lh;
3537 struct idtentry *ih;
3544 stbl = DT_GETSTBL(p);
3546 if (p->header.flag & BT_LEAF) {
3547 lh = (struct ldtentry *) & p->slot[si];
3549 namlen = lh->namlen;
3551 if (flag & JFS_DIR_INDEX)
3552 len = min(namlen, DTLHDRDATALEN);
3554 len = min(namlen, DTLHDRDATALEN_LEGACY);
3556 ih = (struct idtentry *) & p->slot[si];
3558 namlen = ih->namlen;
3560 len = min(namlen, DTIHDRDATALEN);
3563 key->namlen = namlen;
3567 * move head/only segment
3569 UniStrncpy_from_le(kname, name, len);
3572 * move additional segment(s)
3575 /* get next segment */
3579 len = min(namlen, DTSLOTDATALEN);
3580 UniStrncpy_from_le(kname, t->name, len);
3590 * function: allocate free slot(s) and
3591 * write a leaf/internal entry
3593 * return: entry slot index
3595 static void dtInsertEntry(dtpage_t * p, int index, struct component_name * key,
3596 ddata_t * data, struct dt_lock ** dtlock)
3598 struct dtslot *h, *t;
3599 struct ldtentry *lh = NULL;
3600 struct idtentry *ih = NULL;
3601 int hsi, fsi, klen, len, nextindex;
3606 struct dt_lock *dtlck = *dtlock;
3610 struct metapage *mp = NULL;
3615 /* allocate a free slot */
3616 hsi = fsi = p->header.freelist;
3618 p->header.freelist = h->next;
3619 --p->header.freecnt;
3621 /* open new linelock */
3622 if (dtlck->index >= dtlck->maxcnt)
3623 dtlck = (struct dt_lock *) txLinelock(dtlck);
3625 lv = & dtlck->lv[dtlck->index];
3628 /* write head/only segment */
3629 if (p->header.flag & BT_LEAF) {
3630 lh = (struct ldtentry *) h;
3632 lh->inumber = cpu_to_le32(data->leaf.ino);
3635 if (data->leaf.ip) {
3636 len = min(klen, DTLHDRDATALEN);
3637 if (!(p->header.flag & BT_ROOT))
3638 bn = addressPXD(&p->header.self);
3639 lh->index = cpu_to_le32(add_index(data->leaf.tid,
3643 len = min(klen, DTLHDRDATALEN_LEGACY);
3645 ih = (struct idtentry *) h;
3651 len = min(klen, DTIHDRDATALEN);
3654 UniStrncpy_to_le(name, kname, len);
3659 /* write additional segment(s) */
3664 fsi = p->header.freelist;
3666 p->header.freelist = t->next;
3667 --p->header.freecnt;
3669 /* is next slot contiguous ? */
3670 if (fsi != xsi + 1) {
3671 /* close current linelock */
3675 /* open new linelock */
3676 if (dtlck->index < dtlck->maxcnt)
3679 dtlck = (struct dt_lock *) txLinelock(dtlck);
3680 lv = & dtlck->lv[0];
3688 len = min(klen, DTSLOTDATALEN);
3689 UniStrncpy_to_le(t->name, kname, len);
3696 /* close current linelock */
3702 /* terminate last/only segment */
3704 /* single segment entry */
3705 if (p->header.flag & BT_LEAF)
3710 /* multi-segment entry */
3713 /* if insert into middle, shift right succeeding entries in stbl */
3714 stbl = DT_GETSTBL(p);
3715 nextindex = p->header.nextindex;
3716 if (index < nextindex) {
3717 memmove(stbl + index + 1, stbl + index, nextindex - index);
3719 if ((p->header.flag & BT_LEAF) && data->leaf.ip) {
3723 * Need to update slot number for entries that moved
3727 for (n = index + 1; n <= nextindex; n++) {
3728 lh = (struct ldtentry *) & (p->slot[stbl[n]]);
3729 modify_index(data->leaf.tid, data->leaf.ip,
3730 le32_to_cpu(lh->index), bn, n,
3734 release_metapage(mp);
3740 /* advance next available entry index of stbl */
3741 ++p->header.nextindex;
3748 * function: move entries from split/left page to new/right page
3750 * nextindex of dst page and freelist/freecnt of both pages
3753 static void dtMoveEntry(dtpage_t * sp, int si, dtpage_t * dp,
3754 struct dt_lock ** sdtlock, struct dt_lock ** ddtlock,
3757 int ssi, next; /* src slot index */
3758 int di; /* dst entry index */
3759 int dsi; /* dst slot index */
3760 s8 *sstbl, *dstbl; /* sorted entry table */
3762 struct ldtentry *slh, *dlh = NULL;
3763 struct idtentry *sih, *dih = NULL;
3764 struct dtslot *h, *s, *d;
3765 struct dt_lock *sdtlck = *sdtlock, *ddtlck = *ddtlock;
3766 struct lv *slv, *dlv;
3770 sstbl = (s8 *) & sp->slot[sp->header.stblindex];
3771 dstbl = (s8 *) & dp->slot[dp->header.stblindex];
3773 dsi = dp->header.freelist; /* first (whole page) free slot */
3774 sfsi = sp->header.freelist;
3776 /* linelock destination entry slot */
3777 dlv = & ddtlck->lv[ddtlck->index];
3780 /* linelock source entry slot */
3781 slv = & sdtlck->lv[sdtlck->index];
3782 slv->offset = sstbl[si];
3783 xssi = slv->offset - 1;
3789 for (di = 0; si < sp->header.nextindex; si++, di++) {
3793 /* is next slot contiguous ? */
3794 if (ssi != xssi + 1) {
3795 /* close current linelock */
3799 /* open new linelock */
3800 if (sdtlck->index < sdtlck->maxcnt)
3803 sdtlck = (struct dt_lock *) txLinelock(sdtlck);
3804 slv = & sdtlck->lv[0];
3812 * move head/only segment of an entry
3815 h = d = &dp->slot[dsi];
3817 /* get src slot and move */
3819 if (sp->header.flag & BT_LEAF) {
3820 /* get source entry */
3821 slh = (struct ldtentry *) s;
3822 dlh = (struct ldtentry *) h;
3823 snamlen = slh->namlen;
3826 len = min(snamlen, DTLHDRDATALEN);
3827 dlh->index = slh->index; /* little-endian */
3829 len = min(snamlen, DTLHDRDATALEN_LEGACY);
3831 memcpy(dlh, slh, 6 + len * 2);
3835 /* update dst head/only segment next field */
3839 sih = (struct idtentry *) s;
3840 snamlen = sih->namlen;
3842 len = min(snamlen, DTIHDRDATALEN);
3843 dih = (struct idtentry *) h;
3844 memcpy(dih, sih, 10 + len * 2);
3851 /* free src head/only segment */
3861 * move additional segment(s) of the entry
3864 while ((ssi = next) >= 0) {
3865 /* is next slot contiguous ? */
3866 if (ssi != xssi + 1) {
3867 /* close current linelock */
3871 /* open new linelock */
3872 if (sdtlck->index < sdtlck->maxcnt)
3878 slv = & sdtlck->lv[0];
3885 /* get next source segment */
3888 /* get next destination free slot */
3891 len = min(snamlen, DTSLOTDATALEN);
3892 UniStrncpy_le(d->name, s->name, len);
3901 /* free source segment */
3910 /* terminate dst last/only segment */
3912 /* single segment entry */
3913 if (dp->header.flag & BT_LEAF)
3918 /* multi-segment entry */
3922 /* close current linelock */
3931 /* update source header */
3932 sp->header.freelist = sfsi;
3933 sp->header.freecnt += nd;
3935 /* update destination header */
3936 dp->header.nextindex = di;
3938 dp->header.freelist = dsi;
3939 dp->header.freecnt -= nd;
3946 * function: free a (leaf/internal) entry
3948 * log freelist header, stbl, and each segment slot of entry
3949 * (even though last/only segment next field is modified,
3950 * physical image logging requires all segment slots of
3951 * the entry logged to avoid applying previous updates
3952 * to the same slots)
3954 static void dtDeleteEntry(dtpage_t * p, int fi, struct dt_lock ** dtlock)
3956 int fsi; /* free entry slot index */
3960 struct dt_lock *dtlck = *dtlock;
3964 /* get free entry slot index */
3965 stbl = DT_GETSTBL(p);
3968 /* open new linelock */
3969 if (dtlck->index >= dtlck->maxcnt)
3970 dtlck = (struct dt_lock *) txLinelock(dtlck);
3971 lv = & dtlck->lv[dtlck->index];
3975 /* get the head/only segment */
3977 if (p->header.flag & BT_LEAF)
3978 si = ((struct ldtentry *) t)->next;
3980 si = ((struct idtentry *) t)->next;
3987 /* find the last/only segment */
3989 /* is next slot contiguous ? */
3990 if (si != xsi + 1) {
3991 /* close current linelock */
3995 /* open new linelock */
3996 if (dtlck->index < dtlck->maxcnt)
3999 dtlck = (struct dt_lock *) txLinelock(dtlck);
4000 lv = & dtlck->lv[0];
4016 /* close current linelock */
4022 /* update freelist */
4023 t->next = p->header.freelist;
4024 p->header.freelist = fsi;
4025 p->header.freecnt += freecnt;
4027 /* if delete from middle,
4028 * shift left the succedding entries in the stbl
4030 si = p->header.nextindex;
4032 memmove(&stbl[fi], &stbl[fi + 1], si - fi - 1);
4034 p->header.nextindex--;
4041 * function: truncate a (leaf/internal) entry
4043 * log freelist header, stbl, and each segment slot of entry
4044 * (even though last/only segment next field is modified,
4045 * physical image logging requires all segment slots of
4046 * the entry logged to avoid applying previous updates
4047 * to the same slots)
4049 static void dtTruncateEntry(dtpage_t * p, int ti, struct dt_lock ** dtlock)
4051 int tsi; /* truncate entry slot index */
4055 struct dt_lock *dtlck = *dtlock;
4059 /* get free entry slot index */
4060 stbl = DT_GETSTBL(p);
4063 /* open new linelock */
4064 if (dtlck->index >= dtlck->maxcnt)
4065 dtlck = (struct dt_lock *) txLinelock(dtlck);
4066 lv = & dtlck->lv[dtlck->index];
4070 /* get the head/only segment */
4072 ASSERT(p->header.flag & BT_INTERNAL);
4073 ((struct idtentry *) t)->namlen = 0;
4074 si = ((struct idtentry *) t)->next;
4075 ((struct idtentry *) t)->next = -1;
4082 /* find the last/only segment */
4084 /* is next slot contiguous ? */
4085 if (si != xsi + 1) {
4086 /* close current linelock */
4090 /* open new linelock */
4091 if (dtlck->index < dtlck->maxcnt)
4094 dtlck = (struct dt_lock *) txLinelock(dtlck);
4095 lv = & dtlck->lv[0];
4111 /* close current linelock */
4117 /* update freelist */
4120 t->next = p->header.freelist;
4121 p->header.freelist = fsi;
4122 p->header.freecnt += freecnt;
4127 * dtLinelockFreelist()
4129 static void dtLinelockFreelist(dtpage_t * p, /* directory page */
4130 int m, /* max slot index */
4131 struct dt_lock ** dtlock)
4133 int fsi; /* free entry slot index */
4136 struct dt_lock *dtlck = *dtlock;
4140 /* get free entry slot index */
4141 fsi = p->header.freelist;
4143 /* open new linelock */
4144 if (dtlck->index >= dtlck->maxcnt)
4145 dtlck = (struct dt_lock *) txLinelock(dtlck);
4146 lv = & dtlck->lv[dtlck->index];
4156 /* find the last/only segment */
4157 while (si < m && si >= 0) {
4158 /* is next slot contiguous ? */
4159 if (si != xsi + 1) {
4160 /* close current linelock */
4164 /* open new linelock */
4165 if (dtlck->index < dtlck->maxcnt)
4168 dtlck = (struct dt_lock *) txLinelock(dtlck);
4169 lv = & dtlck->lv[0];
4183 /* close current linelock */
4194 * FUNCTION: Modify the inode number part of a directory entry
4197 * tid - Transaction id
4198 * ip - Inode of parent directory
4199 * key - Name of entry to be modified
4200 * orig_ino - Original inode number expected in entry
4201 * new_ino - New inode number to put into entry
4205 * -ESTALE - If entry found does not match orig_ino passed in
4206 * -ENOENT - If no entry can be found to match key
4207 * 0 - If successfully modified entry
4209 int dtModify(tid_t tid, struct inode *ip,
4210 struct component_name * key, ino_t * orig_ino, ino_t new_ino, int flag)
4214 struct metapage *mp;
4217 struct btstack btstack;
4219 struct dt_lock *dtlck;
4222 int entry_si; /* entry slot index */
4223 struct ldtentry *entry;
4226 * search for the entry to modify:
4228 * dtSearch() returns (leaf page pinned, index at which to modify).
4230 if ((rc = dtSearch(ip, key, orig_ino, &btstack, flag)))
4233 /* retrieve search result */
4234 DT_GETSEARCH(ip, btstack.top, bn, mp, p, index);
4236 BT_MARK_DIRTY(mp, ip);
4238 * acquire a transaction lock on the leaf page of named entry
4240 tlck = txLock(tid, ip, mp, tlckDTREE | tlckENTRY);
4241 dtlck = (struct dt_lock *) & tlck->lock;
4243 /* get slot index of the entry */
4244 stbl = DT_GETSTBL(p);
4245 entry_si = stbl[index];
4247 /* linelock entry */
4248 ASSERT(dtlck->index == 0);
4249 lv = & dtlck->lv[0];
4250 lv->offset = entry_si;
4254 /* get the head/only segment */
4255 entry = (struct ldtentry *) & p->slot[entry_si];
4257 /* substitute the inode number of the entry */
4258 entry->inumber = cpu_to_le32(new_ino);
4260 /* unpin the leaf page */