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 (stbl[index] < 0) {
641 if (p->header.flag & BT_LEAF) {
642 /* uppercase leaf name to compare */
644 ciCompare(&ciKey, p, stbl[index],
645 JFS_SBI(sb)->mntflag);
647 /* router key is in uppercase */
649 cmp = dtCompare(&ciKey, p, stbl[index]);
657 /* search hit - leaf page:
658 * return the entry found
660 if (p->header.flag & BT_LEAF) {
661 inumber = le32_to_cpu(
662 ((struct ldtentry *) & p->slot[stbl[index]])->inumber);
665 * search for JFS_LOOKUP
667 if (flag == JFS_LOOKUP) {
674 * search for JFS_CREATE
676 if (flag == JFS_CREATE) {
683 * search for JFS_REMOVE or JFS_RENAME
685 if ((flag == JFS_REMOVE ||
686 flag == JFS_RENAME) &&
693 * JFS_REMOVE|JFS_FINDDIR|JFS_RENAME
695 /* save search result */
706 /* search hit - internal page:
707 * descend/search its child page
721 * base is the smallest index with key (Kj) greater than
722 * search key (K) and may be zero or (maxindex + 1) index.
725 * search miss - leaf page
727 * return location of entry (base) where new entry with
728 * search key K is to be inserted.
730 if (p->header.flag & BT_LEAF) {
732 * search for JFS_LOOKUP, JFS_REMOVE, or JFS_RENAME
734 if (flag == JFS_LOOKUP || flag == JFS_REMOVE ||
735 flag == JFS_RENAME) {
741 * search for JFS_CREATE|JFS_FINDDIR:
756 * search miss - internal page
758 * if base is non-zero, decrement base by one to get the parent
759 * entry of the child page to search.
761 index = base ? base - 1 : base;
764 * go down to child page
767 /* update max. number of pages to split */
768 if (BT_STACK_FULL(btstack)) {
769 /* Something's corrupted, mark filesystem dirty so
770 * chkdsk will fix it.
772 jfs_error(sb, "stack overrun!\n");
773 BT_STACK_DUMP(btstack);
779 /* push (bn, index) of the parent page/entry */
780 BT_PUSH(btstack, bn, index);
782 /* get the child page block number */
783 pxd = (pxd_t *) & p->slot[stbl[index]];
784 bn = addressPXD(pxd);
785 psize = lengthPXD(pxd) << JFS_SBI(ip->i_sb)->l2bsize;
787 /* unpin the parent page */
807 * function: insert an entry to directory tree
811 * return: 0 - success;
814 int dtInsert(tid_t tid, struct inode *ip,
815 struct component_name * name, ino_t * fsn, struct btstack * btstack)
818 struct metapage *mp; /* meta-page buffer */
819 dtpage_t *p; /* base B+-tree index page */
822 struct dtsplit split; /* split information */
824 struct dt_lock *dtlck;
830 * retrieve search result
832 * dtSearch() returns (leaf page pinned, index at which to insert).
833 * n.b. dtSearch() may return index of (maxindex + 1) of
836 DT_GETSEARCH(ip, btstack->top, bn, mp, p, index);
839 * insert entry for new key
842 if (JFS_IP(ip)->next_index == DIREND) {
846 n = NDTLEAF(name->namlen);
850 n = NDTLEAF_LEGACY(name->namlen);
851 data.leaf.ip = NULL; /* signifies legacy directory format */
853 data.leaf.ino = *fsn;
856 * leaf page does not have enough room for new entry:
858 * extend/split the leaf page;
860 * dtSplitUp() will insert the entry and unpin the leaf page.
862 if (n > p->header.freecnt) {
868 rc = dtSplitUp(tid, ip, &split, btstack);
873 * leaf page does have enough room for new entry:
875 * insert the new data entry into the leaf page;
877 BT_MARK_DIRTY(mp, ip);
879 * acquire a transaction lock on the leaf page
881 tlck = txLock(tid, ip, mp, tlckDTREE | tlckENTRY);
882 dtlck = (struct dt_lock *) & tlck->lock;
883 ASSERT(dtlck->index == 0);
886 /* linelock header */
891 dtInsertEntry(p, index, name, &data, &dtlck);
893 /* linelock stbl of non-root leaf page */
894 if (!(p->header.flag & BT_ROOT)) {
895 if (dtlck->index >= dtlck->maxcnt)
896 dtlck = (struct dt_lock *) txLinelock(dtlck);
897 lv = & dtlck->lv[dtlck->index];
898 n = index >> L2DTSLOTSIZE;
899 lv->offset = p->header.stblindex + n;
901 ((p->header.nextindex - 1) >> L2DTSLOTSIZE) - n + 1;
905 /* unpin the leaf page */
915 * function: propagate insertion bottom up;
919 * return: 0 - success;
921 * leaf page unpinned;
923 static int dtSplitUp(tid_t tid,
924 struct inode *ip, struct dtsplit * split, struct btstack * btstack)
926 struct jfs_sb_info *sbi = JFS_SBI(ip->i_sb);
928 struct metapage *smp;
929 dtpage_t *sp; /* split page */
930 struct metapage *rmp;
931 dtpage_t *rp; /* new right page split from sp */
932 pxd_t rpxd; /* new right page extent descriptor */
933 struct metapage *lmp;
934 dtpage_t *lp; /* left child page */
935 int skip; /* index of entry of insertion */
936 struct btframe *parent; /* parent page entry on traverse stack */
939 struct pxdlist pxdlist;
941 struct component_name key = { 0, NULL };
942 ddata_t *data = split->data;
944 struct dt_lock *dtlck;
947 int quota_allocation = 0;
951 sp = DT_PAGE(ip, smp);
953 key.name = kmalloc_array(JFS_NAME_MAX + 2, sizeof(wchar_t), GFP_NOFS);
963 * The split routines insert the new entry, and
964 * acquire txLock as appropriate.
967 * split root leaf page:
969 if (sp->header.flag & BT_ROOT) {
971 * allocate a single extent child page
974 n = sbi->bsize >> L2DTSLOTSIZE;
975 n -= (n + 31) >> L2DTSLOTSIZE; /* stbl size */
976 n -= DTROOTMAXSLOT - sp->header.freecnt; /* header + entries */
977 if (n <= split->nslot)
979 if ((rc = dbAlloc(ip, 0, (s64) xlen, &xaddr))) {
986 pxd = &pxdlist.pxd[0];
987 PXDaddress(pxd, xaddr);
988 PXDlength(pxd, xlen);
989 split->pxdlist = &pxdlist;
990 rc = dtSplitRoot(tid, ip, split, &rmp);
993 dbFree(ip, xaddr, xlen);
1000 ip->i_size = xlen << sbi->l2bsize;
1006 * extend first leaf page
1008 * extend the 1st extent if less than buffer page size
1009 * (dtExtendPage() reurns leaf page unpinned)
1011 pxd = &sp->header.self;
1012 xlen = lengthPXD(pxd);
1013 xsize = xlen << sbi->l2bsize;
1014 if (xsize < PSIZE) {
1015 xaddr = addressPXD(pxd);
1016 n = xsize >> L2DTSLOTSIZE;
1017 n -= (n + 31) >> L2DTSLOTSIZE; /* stbl size */
1018 if ((n + sp->header.freecnt) <= split->nslot)
1019 n = xlen + (xlen << 1);
1023 /* Allocate blocks to quota. */
1024 rc = dquot_alloc_block(ip, n);
1027 quota_allocation += n;
1029 if ((rc = dbReAlloc(sbi->ipbmap, xaddr, (s64) xlen,
1033 pxdlist.maxnpxd = 1;
1035 pxd = &pxdlist.pxd[0];
1036 PXDaddress(pxd, nxaddr);
1037 PXDlength(pxd, xlen + n);
1038 split->pxdlist = &pxdlist;
1039 if ((rc = dtExtendPage(tid, ip, split, btstack))) {
1040 nxaddr = addressPXD(pxd);
1041 if (xaddr != nxaddr) {
1042 /* free relocated extent */
1043 xlen = lengthPXD(pxd);
1044 dbFree(ip, nxaddr, (s64) xlen);
1046 /* free extended delta */
1047 xlen = lengthPXD(pxd) - n;
1048 xaddr = addressPXD(pxd) + xlen;
1049 dbFree(ip, xaddr, (s64) n);
1051 } else if (!DO_INDEX(ip))
1052 ip->i_size = lengthPXD(pxd) << sbi->l2bsize;
1061 * split leaf page <sp> into <sp> and a new right page <rp>.
1063 * return <rp> pinned and its extent descriptor <rpxd>
1066 * allocate new directory page extent and
1067 * new index page(s) to cover page split(s)
1069 * allocation hint: ?
1071 n = btstack->nsplit;
1072 pxdlist.maxnpxd = pxdlist.npxd = 0;
1073 xlen = sbi->nbperpage;
1074 for (pxd = pxdlist.pxd; n > 0; n--, pxd++) {
1075 if ((rc = dbAlloc(ip, 0, (s64) xlen, &xaddr)) == 0) {
1076 PXDaddress(pxd, xaddr);
1077 PXDlength(pxd, xlen);
1084 /* undo allocation */
1088 split->pxdlist = &pxdlist;
1089 if ((rc = dtSplitPage(tid, ip, split, &rmp, &rp, &rpxd))) {
1092 /* undo allocation */
1097 ip->i_size += PSIZE;
1100 * propagate up the router entry for the leaf page just split
1102 * insert a router entry for the new page into the parent page,
1103 * propagate the insert/split up the tree by walking back the stack
1104 * of (bn of parent page, index of child page entry in parent page)
1105 * that were traversed during the search for the page that split.
1107 * the propagation of insert/split up the tree stops if the root
1108 * splits or the page inserted into doesn't have to split to hold
1111 * the parent entry for the split page remains the same, and
1112 * a new entry is inserted at its right with the first key and
1113 * block number of the new right page.
1115 * There are a maximum of 4 pages pinned at any time:
1116 * two children, left parent and right parent (when the parent splits).
1117 * keep the child pages pinned while working on the parent.
1118 * make sure that all pins are released at exit.
1120 while ((parent = BT_POP(btstack)) != NULL) {
1121 /* parent page specified by stack frame <parent> */
1123 /* keep current child pages (<lp>, <rp>) pinned */
1128 * insert router entry in parent for new right child page <rp>
1130 /* get the parent page <sp> */
1131 DT_GETPAGE(ip, parent->bn, smp, PSIZE, sp, rc);
1139 * The new key entry goes ONE AFTER the index of parent entry,
1140 * because the split was to the right.
1142 skip = parent->index + 1;
1145 * compute the key for the router entry
1147 * key suffix compression:
1148 * for internal pages that have leaf pages as children,
1149 * retain only what's needed to distinguish between
1150 * the new entry and the entry on the page to its left.
1151 * If the keys compare equal, retain the entire key.
1153 * note that compression is performed only at computing
1154 * router key at the lowest internal level.
1155 * further compression of the key between pairs of higher
1156 * level internal pages loses too much information and
1157 * the search may fail.
1158 * (e.g., two adjacent leaf pages of {a, ..., x} {xx, ...,}
1159 * results in two adjacent parent entries (a)(xx).
1160 * if split occurs between these two entries, and
1161 * if compression is applied, the router key of parent entry
1162 * of right page (x) will divert search for x into right
1163 * subtree and miss x in the left subtree.)
1165 * the entire key must be retained for the next-to-leftmost
1166 * internal key at any level of the tree, or search may fail
1169 switch (rp->header.flag & BT_TYPE) {
1172 * compute the length of prefix for suffix compression
1173 * between last entry of left page and first entry
1176 if ((sp->header.flag & BT_ROOT && skip > 1) ||
1177 sp->header.prev != 0 || skip > 1) {
1178 /* compute uppercase router prefix key */
1179 rc = ciGetLeafPrefixKey(lp,
1180 lp->header.nextindex-1,
1190 /* next to leftmost entry of
1191 lowest internal level */
1193 /* compute uppercase router key */
1194 dtGetKey(rp, 0, &key, sbi->mntflag);
1195 key.name[key.namlen] = 0;
1197 if ((sbi->mntflag & JFS_OS2) == JFS_OS2)
1201 n = NDTINTERNAL(key.namlen);
1205 dtGetKey(rp, 0, &key, sbi->mntflag);
1206 n = NDTINTERNAL(key.namlen);
1210 jfs_err("dtSplitUp(): UFO!");
1214 /* unpin left child page */
1218 * compute the data for the router entry
1220 data->xd = rpxd; /* child page xd */
1223 * parent page is full - split the parent page
1225 if (n > sp->header.freecnt) {
1226 /* init for parent page split */
1228 split->index = skip; /* index at insert */
1231 /* split->data = data; */
1233 /* unpin right child page */
1236 /* The split routines insert the new entry,
1237 * acquire txLock as appropriate.
1238 * return <rp> pinned and its block number <rbn>.
1240 rc = (sp->header.flag & BT_ROOT) ?
1241 dtSplitRoot(tid, ip, split, &rmp) :
1242 dtSplitPage(tid, ip, split, &rmp, &rp, &rpxd);
1248 /* smp and rmp are pinned */
1251 * parent page is not full - insert router entry in parent page
1254 BT_MARK_DIRTY(smp, ip);
1256 * acquire a transaction lock on the parent page
1258 tlck = txLock(tid, ip, smp, tlckDTREE | tlckENTRY);
1259 dtlck = (struct dt_lock *) & tlck->lock;
1260 ASSERT(dtlck->index == 0);
1261 lv = & dtlck->lv[0];
1263 /* linelock header */
1268 /* linelock stbl of non-root parent page */
1269 if (!(sp->header.flag & BT_ROOT)) {
1271 n = skip >> L2DTSLOTSIZE;
1272 lv->offset = sp->header.stblindex + n;
1274 ((sp->header.nextindex -
1275 1) >> L2DTSLOTSIZE) - n + 1;
1279 dtInsertEntry(sp, skip, &key, data, &dtlck);
1281 /* exit propagate up */
1286 /* unpin current split and its right page */
1291 * free remaining extents allocated for split
1295 pxd = &pxdlist.pxd[n];
1296 for (; n < pxdlist.maxnpxd; n++, pxd++)
1297 dbFree(ip, addressPXD(pxd), (s64) lengthPXD(pxd));
1302 /* Rollback quota allocation */
1303 if (rc && quota_allocation)
1304 dquot_free_block(ip, quota_allocation);
1315 * function: Split a non-root page of a btree.
1319 * return: 0 - success;
1321 * return split and new page pinned;
1323 static int dtSplitPage(tid_t tid, struct inode *ip, struct dtsplit * split,
1324 struct metapage ** rmpp, dtpage_t ** rpp, pxd_t * rpxdp)
1327 struct metapage *smp;
1329 struct metapage *rmp;
1330 dtpage_t *rp; /* new right page allocated */
1331 s64 rbn; /* new right page block number */
1332 struct metapage *mp;
1335 struct pxdlist *pxdlist;
1337 int skip, nextindex, half, left, nxt, off, si;
1338 struct ldtentry *ldtentry;
1339 struct idtentry *idtentry;
1344 struct dt_lock *sdtlck, *rdtlck;
1346 struct dt_lock *dtlck;
1347 struct lv *slv, *rlv, *lv;
1349 /* get split page */
1351 sp = DT_PAGE(ip, smp);
1354 * allocate the new right page for the split
1356 pxdlist = split->pxdlist;
1357 pxd = &pxdlist->pxd[pxdlist->npxd];
1359 rbn = addressPXD(pxd);
1360 rmp = get_metapage(ip, rbn, PSIZE, 1);
1364 /* Allocate blocks to quota. */
1365 rc = dquot_alloc_block(ip, lengthPXD(pxd));
1367 release_metapage(rmp);
1371 jfs_info("dtSplitPage: ip:0x%p smp:0x%p rmp:0x%p", ip, smp, rmp);
1373 BT_MARK_DIRTY(rmp, ip);
1375 * acquire a transaction lock on the new right page
1377 tlck = txLock(tid, ip, rmp, tlckDTREE | tlckNEW);
1378 rdtlck = (struct dt_lock *) & tlck->lock;
1380 rp = (dtpage_t *) rmp->data;
1382 rp->header.self = *pxd;
1384 BT_MARK_DIRTY(smp, ip);
1386 * acquire a transaction lock on the split page
1390 tlck = txLock(tid, ip, smp, tlckDTREE | tlckENTRY);
1391 sdtlck = (struct dt_lock *) & tlck->lock;
1393 /* linelock header of split page */
1394 ASSERT(sdtlck->index == 0);
1395 slv = & sdtlck->lv[0];
1401 * initialize/update sibling pointers between sp and rp
1403 nextbn = le64_to_cpu(sp->header.next);
1404 rp->header.next = cpu_to_le64(nextbn);
1405 rp->header.prev = cpu_to_le64(addressPXD(&sp->header.self));
1406 sp->header.next = cpu_to_le64(rbn);
1409 * initialize new right page
1411 rp->header.flag = sp->header.flag;
1413 /* compute sorted entry table at start of extent data area */
1414 rp->header.nextindex = 0;
1415 rp->header.stblindex = 1;
1417 n = PSIZE >> L2DTSLOTSIZE;
1418 rp->header.maxslot = n;
1419 stblsize = (n + 31) >> L2DTSLOTSIZE; /* in unit of slot */
1422 fsi = rp->header.stblindex + stblsize;
1423 rp->header.freelist = fsi;
1424 rp->header.freecnt = rp->header.maxslot - fsi;
1427 * sequential append at tail: append without split
1429 * If splitting the last page on a level because of appending
1430 * a entry to it (skip is maxentry), it's likely that the access is
1431 * sequential. Adding an empty page on the side of the level is less
1432 * work and can push the fill factor much higher than normal.
1433 * If we're wrong it's no big deal, we'll just do the split the right
1435 * (It may look like it's equally easy to do a similar hack for
1436 * reverse sorted data, that is, split the tree left,
1437 * but it's not. Be my guest.)
1439 if (nextbn == 0 && split->index == sp->header.nextindex) {
1440 /* linelock header + stbl (first slot) of new page */
1441 rlv = & rdtlck->lv[rdtlck->index];
1447 * initialize freelist of new right page
1450 for (fsi++; fsi < rp->header.maxslot; f++, fsi++)
1454 /* insert entry at the first entry of the new right page */
1455 dtInsertEntry(rp, 0, split->key, split->data, &rdtlck);
1461 * non-sequential insert (at possibly middle page)
1465 * update prev pointer of previous right sibling page;
1468 DT_GETPAGE(ip, nextbn, mp, PSIZE, p, rc);
1470 discard_metapage(rmp);
1474 BT_MARK_DIRTY(mp, ip);
1476 * acquire a transaction lock on the next page
1478 tlck = txLock(tid, ip, mp, tlckDTREE | tlckRELINK);
1479 jfs_info("dtSplitPage: tlck = 0x%p, ip = 0x%p, mp=0x%p",
1481 dtlck = (struct dt_lock *) & tlck->lock;
1483 /* linelock header of previous right sibling page */
1484 lv = & dtlck->lv[dtlck->index];
1489 p->header.prev = cpu_to_le64(rbn);
1495 * split the data between the split and right pages.
1497 skip = split->index;
1498 half = (PSIZE >> L2DTSLOTSIZE) >> 1; /* swag */
1502 * compute fill factor for split pages
1504 * <nxt> traces the next entry to move to rp
1505 * <off> traces the next entry to stay in sp
1507 stbl = (u8 *) & sp->slot[sp->header.stblindex];
1508 nextindex = sp->header.nextindex;
1509 for (nxt = off = 0; nxt < nextindex; ++off) {
1511 /* check for fill factor with new entry size */
1515 switch (sp->header.flag & BT_TYPE) {
1517 ldtentry = (struct ldtentry *) & sp->slot[si];
1519 n = NDTLEAF(ldtentry->namlen);
1521 n = NDTLEAF_LEGACY(ldtentry->
1526 idtentry = (struct idtentry *) & sp->slot[si];
1527 n = NDTINTERNAL(idtentry->namlen);
1534 ++nxt; /* advance to next entry to move in sp */
1542 /* <nxt> poins to the 1st entry to move */
1545 * move entries to right page
1547 * dtMoveEntry() initializes rp and reserves entry for insertion
1549 * split page moved out entries are linelocked;
1550 * new/right page moved in entries are linelocked;
1552 /* linelock header + stbl of new right page */
1553 rlv = & rdtlck->lv[rdtlck->index];
1558 dtMoveEntry(sp, nxt, rp, &sdtlck, &rdtlck, DO_INDEX(ip));
1560 sp->header.nextindex = nxt;
1563 * finalize freelist of new right page
1565 fsi = rp->header.freelist;
1567 for (fsi++; fsi < rp->header.maxslot; f++, fsi++)
1572 * Update directory index table for entries now in right page
1574 if ((rp->header.flag & BT_LEAF) && DO_INDEX(ip)) {
1578 stbl = DT_GETSTBL(rp);
1579 for (n = 0; n < rp->header.nextindex; n++) {
1580 ldtentry = (struct ldtentry *) & rp->slot[stbl[n]];
1581 modify_index(tid, ip, le32_to_cpu(ldtentry->index),
1582 rbn, n, &mp, &lblock);
1585 release_metapage(mp);
1589 * the skipped index was on the left page,
1592 /* insert the new entry in the split page */
1593 dtInsertEntry(sp, skip, split->key, split->data, &sdtlck);
1595 /* linelock stbl of split page */
1596 if (sdtlck->index >= sdtlck->maxcnt)
1597 sdtlck = (struct dt_lock *) txLinelock(sdtlck);
1598 slv = & sdtlck->lv[sdtlck->index];
1599 n = skip >> L2DTSLOTSIZE;
1600 slv->offset = sp->header.stblindex + n;
1602 ((sp->header.nextindex - 1) >> L2DTSLOTSIZE) - n + 1;
1606 * the skipped index was on the right page,
1609 /* adjust the skip index to reflect the new position */
1612 /* insert the new entry in the right page */
1613 dtInsertEntry(rp, skip, split->key, split->data, &rdtlck);
1627 * function: extend 1st/only directory leaf page
1631 * return: 0 - success;
1633 * return extended page pinned;
1635 static int dtExtendPage(tid_t tid,
1636 struct inode *ip, struct dtsplit * split, struct btstack * btstack)
1638 struct super_block *sb = ip->i_sb;
1640 struct metapage *smp, *pmp, *mp;
1642 struct pxdlist *pxdlist;
1645 int newstblindex, newstblsize;
1646 int oldstblindex, oldstblsize;
1649 struct btframe *parent;
1651 struct dt_lock *dtlck;
1654 struct pxd_lock *pxdlock;
1657 struct ldtentry *ldtentry;
1660 /* get page to extend */
1662 sp = DT_PAGE(ip, smp);
1664 /* get parent/root page */
1665 parent = BT_POP(btstack);
1666 DT_GETPAGE(ip, parent->bn, pmp, PSIZE, pp, rc);
1673 pxdlist = split->pxdlist;
1674 pxd = &pxdlist->pxd[pxdlist->npxd];
1677 xaddr = addressPXD(pxd);
1678 tpxd = &sp->header.self;
1679 txaddr = addressPXD(tpxd);
1680 /* in-place extension */
1681 if (xaddr == txaddr) {
1688 /* save moved extent descriptor for later free */
1689 tlck = txMaplock(tid, ip, tlckDTREE | tlckRELOCATE);
1690 pxdlock = (struct pxd_lock *) & tlck->lock;
1691 pxdlock->flag = mlckFREEPXD;
1692 pxdlock->pxd = sp->header.self;
1696 * Update directory index table to reflect new page address
1702 stbl = DT_GETSTBL(sp);
1703 for (n = 0; n < sp->header.nextindex; n++) {
1705 (struct ldtentry *) & sp->slot[stbl[n]];
1706 modify_index(tid, ip,
1707 le32_to_cpu(ldtentry->index),
1708 xaddr, n, &mp, &lblock);
1711 release_metapage(mp);
1718 sp->header.self = *pxd;
1720 jfs_info("dtExtendPage: ip:0x%p smp:0x%p sp:0x%p", ip, smp, sp);
1722 BT_MARK_DIRTY(smp, ip);
1724 * acquire a transaction lock on the extended/leaf page
1726 tlck = txLock(tid, ip, smp, tlckDTREE | type);
1727 dtlck = (struct dt_lock *) & tlck->lock;
1728 lv = & dtlck->lv[0];
1730 /* update buffer extent descriptor of extended page */
1731 xlen = lengthPXD(pxd);
1732 xsize = xlen << JFS_SBI(sb)->l2bsize;
1735 * copy old stbl to new stbl at start of extended area
1737 oldstblindex = sp->header.stblindex;
1738 oldstblsize = (sp->header.maxslot + 31) >> L2DTSLOTSIZE;
1739 newstblindex = sp->header.maxslot;
1740 n = xsize >> L2DTSLOTSIZE;
1741 newstblsize = (n + 31) >> L2DTSLOTSIZE;
1742 memcpy(&sp->slot[newstblindex], &sp->slot[oldstblindex],
1743 sp->header.nextindex);
1746 * in-line extension: linelock old area of extended page
1748 if (type == tlckEXTEND) {
1749 /* linelock header */
1755 /* linelock new stbl of extended page */
1756 lv->offset = newstblindex;
1757 lv->length = newstblsize;
1760 * relocation: linelock whole relocated area
1764 lv->length = sp->header.maxslot + newstblsize;
1769 sp->header.maxslot = n;
1770 sp->header.stblindex = newstblindex;
1771 /* sp->header.nextindex remains the same */
1774 * add old stbl region at head of freelist
1778 last = sp->header.freelist;
1779 for (n = 0; n < oldstblsize; n++, fsi++, f++) {
1783 sp->header.freelist = last;
1784 sp->header.freecnt += oldstblsize;
1787 * append free region of newly extended area at tail of freelist
1789 /* init free region of newly extended area */
1790 fsi = n = newstblindex + newstblsize;
1792 for (fsi++; fsi < sp->header.maxslot; f++, fsi++)
1796 /* append new free region at tail of old freelist */
1797 fsi = sp->header.freelist;
1799 sp->header.freelist = n;
1804 } while (fsi != -1);
1809 sp->header.freecnt += sp->header.maxslot - n;
1812 * insert the new entry
1814 dtInsertEntry(sp, split->index, split->key, split->data, &dtlck);
1816 BT_MARK_DIRTY(pmp, ip);
1818 * linelock any freeslots residing in old extent
1820 if (type == tlckEXTEND) {
1821 n = sp->header.maxslot >> 2;
1822 if (sp->header.freelist < n)
1823 dtLinelockFreelist(sp, n, &dtlck);
1827 * update parent entry on the parent/root page
1830 * acquire a transaction lock on the parent/root page
1832 tlck = txLock(tid, ip, pmp, tlckDTREE | tlckENTRY);
1833 dtlck = (struct dt_lock *) & tlck->lock;
1834 lv = & dtlck->lv[dtlck->index];
1836 /* linelock parent entry - 1st slot */
1841 /* update the parent pxd for page extension */
1842 tpxd = (pxd_t *) & pp->slot[1];
1854 * split the full root page into
1855 * original/root/split page and new right page
1856 * i.e., root remains fixed in tree anchor (inode) and
1857 * the root is copied to a single new right child page
1858 * since root page << non-root page, and
1859 * the split root page contains a single entry for the
1860 * new right child page.
1864 * return: 0 - success;
1866 * return new page pinned;
1868 static int dtSplitRoot(tid_t tid,
1869 struct inode *ip, struct dtsplit * split, struct metapage ** rmpp)
1871 struct super_block *sb = ip->i_sb;
1872 struct metapage *smp;
1874 struct metapage *rmp;
1881 int fsi, stblsize, n;
1884 struct pxdlist *pxdlist;
1886 struct dt_lock *dtlck;
1891 /* get split root page */
1893 sp = &JFS_IP(ip)->i_dtroot;
1896 * allocate/initialize a single (right) child page
1898 * N.B. at first split, a one (or two) block to fit new entry
1899 * is allocated; at subsequent split, a full page is allocated;
1901 pxdlist = split->pxdlist;
1902 pxd = &pxdlist->pxd[pxdlist->npxd];
1904 rbn = addressPXD(pxd);
1905 xlen = lengthPXD(pxd);
1906 xsize = xlen << JFS_SBI(sb)->l2bsize;
1907 rmp = get_metapage(ip, rbn, xsize, 1);
1913 /* Allocate blocks to quota. */
1914 rc = dquot_alloc_block(ip, lengthPXD(pxd));
1916 release_metapage(rmp);
1920 BT_MARK_DIRTY(rmp, ip);
1922 * acquire a transaction lock on the new right page
1924 tlck = txLock(tid, ip, rmp, tlckDTREE | tlckNEW);
1925 dtlck = (struct dt_lock *) & tlck->lock;
1928 (sp->header.flag & BT_LEAF) ? BT_LEAF : BT_INTERNAL;
1929 rp->header.self = *pxd;
1931 /* initialize sibling pointers */
1932 rp->header.next = 0;
1933 rp->header.prev = 0;
1936 * move in-line root page into new right page extent
1938 /* linelock header + copied entries + new stbl (1st slot) in new page */
1939 ASSERT(dtlck->index == 0);
1940 lv = & dtlck->lv[0];
1942 lv->length = 10; /* 1 + 8 + 1 */
1945 n = xsize >> L2DTSLOTSIZE;
1946 rp->header.maxslot = n;
1947 stblsize = (n + 31) >> L2DTSLOTSIZE;
1949 /* copy old stbl to new stbl at start of extended area */
1950 rp->header.stblindex = DTROOTMAXSLOT;
1951 stbl = (s8 *) & rp->slot[DTROOTMAXSLOT];
1952 memcpy(stbl, sp->header.stbl, sp->header.nextindex);
1953 rp->header.nextindex = sp->header.nextindex;
1955 /* copy old data area to start of new data area */
1956 memcpy(&rp->slot[1], &sp->slot[1], IDATASIZE);
1959 * append free region of newly extended area at tail of freelist
1961 /* init free region of newly extended area */
1962 fsi = n = DTROOTMAXSLOT + stblsize;
1964 for (fsi++; fsi < rp->header.maxslot; f++, fsi++)
1968 /* append new free region at tail of old freelist */
1969 fsi = sp->header.freelist;
1971 rp->header.freelist = n;
1973 rp->header.freelist = fsi;
1983 rp->header.freecnt = sp->header.freecnt + rp->header.maxslot - n;
1986 * Update directory index table for entries now in right page
1988 if ((rp->header.flag & BT_LEAF) && DO_INDEX(ip)) {
1990 struct metapage *mp = NULL;
1991 struct ldtentry *ldtentry;
1993 stbl = DT_GETSTBL(rp);
1994 for (n = 0; n < rp->header.nextindex; n++) {
1995 ldtentry = (struct ldtentry *) & rp->slot[stbl[n]];
1996 modify_index(tid, ip, le32_to_cpu(ldtentry->index),
1997 rbn, n, &mp, &lblock);
2000 release_metapage(mp);
2003 * insert the new entry into the new right/child page
2004 * (skip index in the new right page will not change)
2006 dtInsertEntry(rp, split->index, split->key, split->data, &dtlck);
2009 * reset parent/root page
2011 * set the 1st entry offset to 0, which force the left-most key
2012 * at any level of the tree to be less than any search key.
2014 * The btree comparison code guarantees that the left-most key on any
2015 * level of the tree is never used, so it doesn't need to be filled in.
2017 BT_MARK_DIRTY(smp, ip);
2019 * acquire a transaction lock on the root page (in-memory inode)
2021 tlck = txLock(tid, ip, smp, tlckDTREE | tlckNEW | tlckBTROOT);
2022 dtlck = (struct dt_lock *) & tlck->lock;
2025 ASSERT(dtlck->index == 0);
2026 lv = & dtlck->lv[0];
2028 lv->length = DTROOTMAXSLOT;
2031 /* update page header of root */
2032 if (sp->header.flag & BT_LEAF) {
2033 sp->header.flag &= ~BT_LEAF;
2034 sp->header.flag |= BT_INTERNAL;
2037 /* init the first entry */
2038 s = (struct idtentry *) & sp->slot[DTENTRYSTART];
2044 stbl = sp->header.stbl;
2045 stbl[0] = DTENTRYSTART;
2046 sp->header.nextindex = 1;
2049 fsi = DTENTRYSTART + 1;
2052 /* init free region of remaining area */
2053 for (fsi++; fsi < DTROOTMAXSLOT; f++, fsi++)
2057 sp->header.freelist = DTENTRYSTART + 1;
2058 sp->header.freecnt = DTROOTMAXSLOT - (DTENTRYSTART + 1);
2069 * function: delete the entry(s) referenced by a key.
2075 int dtDelete(tid_t tid,
2076 struct inode *ip, struct component_name * key, ino_t * ino, int flag)
2080 struct metapage *mp, *imp;
2083 struct btstack btstack;
2084 struct dt_lock *dtlck;
2088 struct ldtentry *ldtentry;
2090 u32 table_index, next_index;
2091 struct metapage *nmp;
2095 * search for the entry to delete:
2097 * dtSearch() returns (leaf page pinned, index at which to delete).
2099 if ((rc = dtSearch(ip, key, ino, &btstack, flag)))
2102 /* retrieve search result */
2103 DT_GETSEARCH(ip, btstack.top, bn, mp, p, index);
2106 * We need to find put the index of the next entry into the
2107 * directory index table in order to resume a readdir from this
2111 stbl = DT_GETSTBL(p);
2112 ldtentry = (struct ldtentry *) & p->slot[stbl[index]];
2113 table_index = le32_to_cpu(ldtentry->index);
2114 if (index == (p->header.nextindex - 1)) {
2116 * Last entry in this leaf page
2118 if ((p->header.flag & BT_ROOT)
2119 || (p->header.next == 0))
2122 /* Read next leaf page */
2123 DT_GETPAGE(ip, le64_to_cpu(p->header.next),
2124 nmp, PSIZE, np, rc);
2128 stbl = DT_GETSTBL(np);
2130 (struct ldtentry *) & np->
2133 le32_to_cpu(ldtentry->index);
2139 (struct ldtentry *) & p->slot[stbl[index + 1]];
2140 next_index = le32_to_cpu(ldtentry->index);
2142 free_index(tid, ip, table_index, next_index);
2145 * the leaf page becomes empty, delete the page
2147 if (p->header.nextindex == 1) {
2148 /* delete empty page */
2149 rc = dtDeleteUp(tid, ip, mp, p, &btstack);
2152 * the leaf page has other entries remaining:
2154 * delete the entry from the leaf page.
2157 BT_MARK_DIRTY(mp, ip);
2159 * acquire a transaction lock on the leaf page
2161 tlck = txLock(tid, ip, mp, tlckDTREE | tlckENTRY);
2162 dtlck = (struct dt_lock *) & tlck->lock;
2165 * Do not assume that dtlck->index will be zero. During a
2166 * rename within a directory, this transaction may have
2167 * modified this page already when adding the new entry.
2170 /* linelock header */
2171 if (dtlck->index >= dtlck->maxcnt)
2172 dtlck = (struct dt_lock *) txLinelock(dtlck);
2173 lv = & dtlck->lv[dtlck->index];
2178 /* linelock stbl of non-root leaf page */
2179 if (!(p->header.flag & BT_ROOT)) {
2180 if (dtlck->index >= dtlck->maxcnt)
2181 dtlck = (struct dt_lock *) txLinelock(dtlck);
2182 lv = & dtlck->lv[dtlck->index];
2183 i = index >> L2DTSLOTSIZE;
2184 lv->offset = p->header.stblindex + i;
2186 ((p->header.nextindex - 1) >> L2DTSLOTSIZE) -
2191 /* free the leaf entry */
2192 dtDeleteEntry(p, index, &dtlck);
2195 * Update directory index table for entries moved in stbl
2197 if (DO_INDEX(ip) && index < p->header.nextindex) {
2201 stbl = DT_GETSTBL(p);
2202 for (i = index; i < p->header.nextindex; i++) {
2204 (struct ldtentry *) & p->slot[stbl[i]];
2205 modify_index(tid, ip,
2206 le32_to_cpu(ldtentry->index),
2207 bn, i, &imp, &lblock);
2210 release_metapage(imp);
2224 * free empty pages as propagating deletion up the tree
2230 static int dtDeleteUp(tid_t tid, struct inode *ip,
2231 struct metapage * fmp, dtpage_t * fp, struct btstack * btstack)
2234 struct metapage *mp;
2236 int index, nextindex;
2238 struct btframe *parent;
2239 struct dt_lock *dtlck;
2242 struct pxd_lock *pxdlock;
2246 * keep the root leaf page which has become empty
2248 if (BT_IS_ROOT(fmp)) {
2252 * dtInitRoot() acquires txlock on the root
2254 dtInitRoot(tid, ip, PARENT(ip));
2262 * free the non-root leaf page
2265 * acquire a transaction lock on the page
2267 * write FREEXTENT|NOREDOPAGE log record
2268 * N.B. linelock is overlaid as freed extent descriptor, and
2269 * the buffer page is freed;
2271 tlck = txMaplock(tid, ip, tlckDTREE | tlckFREE);
2272 pxdlock = (struct pxd_lock *) & tlck->lock;
2273 pxdlock->flag = mlckFREEPXD;
2274 pxdlock->pxd = fp->header.self;
2277 /* update sibling pointers */
2278 if ((rc = dtRelink(tid, ip, fp))) {
2283 xlen = lengthPXD(&fp->header.self);
2285 /* Free quota allocation. */
2286 dquot_free_block(ip, xlen);
2288 /* free/invalidate its buffer page */
2289 discard_metapage(fmp);
2292 * propagate page deletion up the directory tree
2294 * If the delete from the parent page makes it empty,
2295 * continue all the way up the tree.
2296 * stop if the root page is reached (which is never deleted) or
2297 * if the entry deletion does not empty the page.
2299 while ((parent = BT_POP(btstack)) != NULL) {
2300 /* pin the parent page <sp> */
2301 DT_GETPAGE(ip, parent->bn, mp, PSIZE, p, rc);
2306 * free the extent of the child page deleted
2308 index = parent->index;
2311 * delete the entry for the child page from parent
2313 nextindex = p->header.nextindex;
2316 * the parent has the single entry being deleted:
2318 * free the parent page which has become empty.
2320 if (nextindex == 1) {
2322 * keep the root internal page which has become empty
2324 if (p->header.flag & BT_ROOT) {
2328 * dtInitRoot() acquires txlock on the root
2330 dtInitRoot(tid, ip, PARENT(ip));
2337 * free the parent page
2341 * acquire a transaction lock on the page
2343 * write FREEXTENT|NOREDOPAGE log record
2347 tlckDTREE | tlckFREE);
2348 pxdlock = (struct pxd_lock *) & tlck->lock;
2349 pxdlock->flag = mlckFREEPXD;
2350 pxdlock->pxd = p->header.self;
2353 /* update sibling pointers */
2354 if ((rc = dtRelink(tid, ip, p))) {
2359 xlen = lengthPXD(&p->header.self);
2361 /* Free quota allocation */
2362 dquot_free_block(ip, xlen);
2364 /* free/invalidate its buffer page */
2365 discard_metapage(mp);
2373 * the parent has other entries remaining:
2375 * delete the router entry from the parent page.
2377 BT_MARK_DIRTY(mp, ip);
2379 * acquire a transaction lock on the page
2381 * action: router entry deletion
2383 tlck = txLock(tid, ip, mp, tlckDTREE | tlckENTRY);
2384 dtlck = (struct dt_lock *) & tlck->lock;
2386 /* linelock header */
2387 if (dtlck->index >= dtlck->maxcnt)
2388 dtlck = (struct dt_lock *) txLinelock(dtlck);
2389 lv = & dtlck->lv[dtlck->index];
2394 /* linelock stbl of non-root leaf page */
2395 if (!(p->header.flag & BT_ROOT)) {
2396 if (dtlck->index < dtlck->maxcnt)
2399 dtlck = (struct dt_lock *) txLinelock(dtlck);
2400 lv = & dtlck->lv[0];
2402 i = index >> L2DTSLOTSIZE;
2403 lv->offset = p->header.stblindex + i;
2405 ((p->header.nextindex - 1) >> L2DTSLOTSIZE) -
2410 /* free the router entry */
2411 dtDeleteEntry(p, index, &dtlck);
2413 /* reset key of new leftmost entry of level (for consistency) */
2415 ((p->header.flag & BT_ROOT) || p->header.prev == 0))
2416 dtTruncateEntry(p, 0, &dtlck);
2418 /* unpin the parent page */
2421 /* exit propagation up */
2426 ip->i_size -= PSIZE;
2435 * link around a freed page.
2438 * fp: page to be freed
2442 static int dtRelink(tid_t tid, struct inode *ip, dtpage_t * p)
2445 struct metapage *mp;
2448 struct dt_lock *dtlck;
2451 nextbn = le64_to_cpu(p->header.next);
2452 prevbn = le64_to_cpu(p->header.prev);
2454 /* update prev pointer of the next page */
2456 DT_GETPAGE(ip, nextbn, mp, PSIZE, p, rc);
2460 BT_MARK_DIRTY(mp, ip);
2462 * acquire a transaction lock on the next page
2464 * action: update prev pointer;
2466 tlck = txLock(tid, ip, mp, tlckDTREE | tlckRELINK);
2467 jfs_info("dtRelink nextbn: tlck = 0x%p, ip = 0x%p, mp=0x%p",
2469 dtlck = (struct dt_lock *) & tlck->lock;
2471 /* linelock header */
2472 if (dtlck->index >= dtlck->maxcnt)
2473 dtlck = (struct dt_lock *) txLinelock(dtlck);
2474 lv = & dtlck->lv[dtlck->index];
2479 p->header.prev = cpu_to_le64(prevbn);
2483 /* update next pointer of the previous page */
2485 DT_GETPAGE(ip, prevbn, mp, PSIZE, p, rc);
2489 BT_MARK_DIRTY(mp, ip);
2491 * acquire a transaction lock on the prev page
2493 * action: update next pointer;
2495 tlck = txLock(tid, ip, mp, tlckDTREE | tlckRELINK);
2496 jfs_info("dtRelink prevbn: tlck = 0x%p, ip = 0x%p, mp=0x%p",
2498 dtlck = (struct dt_lock *) & tlck->lock;
2500 /* linelock header */
2501 if (dtlck->index >= dtlck->maxcnt)
2502 dtlck = (struct dt_lock *) txLinelock(dtlck);
2503 lv = & dtlck->lv[dtlck->index];
2508 p->header.next = cpu_to_le64(nextbn);
2519 * initialize directory root (inline in inode)
2521 void dtInitRoot(tid_t tid, struct inode *ip, u32 idotdot)
2523 struct jfs_inode_info *jfs_ip = JFS_IP(ip);
2528 struct dt_lock *dtlck;
2533 * If this was previously an non-empty directory, we need to remove
2534 * the old directory table.
2537 if (!jfs_dirtable_inline(ip)) {
2538 struct tblock *tblk = tid_to_tblock(tid);
2540 * We're playing games with the tid's xflag. If
2541 * we're removing a regular file, the file's xtree
2542 * is committed with COMMIT_PMAP, but we always
2543 * commit the directories xtree with COMMIT_PWMAP.
2545 xflag_save = tblk->xflag;
2548 * xtTruncate isn't guaranteed to fully truncate
2549 * the xtree. The caller needs to check i_size
2550 * after committing the transaction to see if
2551 * additional truncation is needed. The
2552 * COMMIT_Stale flag tells caller that we
2553 * initiated the truncation.
2555 xtTruncate(tid, ip, 0, COMMIT_PWMAP);
2556 set_cflag(COMMIT_Stale, ip);
2558 tblk->xflag = xflag_save;
2562 jfs_ip->next_index = 2;
2564 ip->i_size = IDATASIZE;
2567 * acquire a transaction lock on the root
2569 * action: directory initialization;
2571 tlck = txLock(tid, ip, (struct metapage *) & jfs_ip->bxflag,
2572 tlckDTREE | tlckENTRY | tlckBTROOT);
2573 dtlck = (struct dt_lock *) & tlck->lock;
2576 ASSERT(dtlck->index == 0);
2577 lv = & dtlck->lv[0];
2579 lv->length = DTROOTMAXSLOT;
2582 p = &jfs_ip->i_dtroot;
2584 p->header.flag = DXD_INDEX | BT_ROOT | BT_LEAF;
2586 p->header.nextindex = 0;
2592 /* init data area of root */
2593 for (fsi++; fsi < DTROOTMAXSLOT; f++, fsi++)
2597 p->header.freelist = 1;
2598 p->header.freecnt = 8;
2600 /* init '..' entry */
2601 p->header.idotdot = cpu_to_le32(idotdot);
2607 * add_missing_indices()
2609 * function: Fix dtree page in which one or more entries has an invalid index.
2610 * fsck.jfs should really fix this, but it currently does not.
2611 * Called from jfs_readdir when bad index is detected.
2613 static void add_missing_indices(struct inode *inode, s64 bn)
2616 struct dt_lock *dtlck;
2620 struct metapage *mp;
2627 tid = txBegin(inode->i_sb, 0);
2629 DT_GETPAGE(inode, bn, mp, PSIZE, p, rc);
2632 printk(KERN_ERR "DT_GETPAGE failed!\n");
2635 BT_MARK_DIRTY(mp, inode);
2637 ASSERT(p->header.flag & BT_LEAF);
2639 tlck = txLock(tid, inode, mp, tlckDTREE | tlckENTRY);
2641 tlck->type |= tlckBTROOT;
2643 dtlck = (struct dt_lock *) &tlck->lock;
2645 stbl = DT_GETSTBL(p);
2646 for (i = 0; i < p->header.nextindex; i++) {
2647 d = (struct ldtentry *) &p->slot[stbl[i]];
2648 index = le32_to_cpu(d->index);
2649 if ((index < 2) || (index >= JFS_IP(inode)->next_index)) {
2650 d->index = cpu_to_le32(add_index(tid, inode, bn, i));
2651 if (dtlck->index >= dtlck->maxcnt)
2652 dtlck = (struct dt_lock *) txLinelock(dtlck);
2653 lv = &dtlck->lv[dtlck->index];
2654 lv->offset = stbl[i];
2661 (void) txCommit(tid, 1, &inode, 0);
2667 * Buffer to hold directory entry info while traversing a dtree page
2668 * before being fed to the filldir function
2678 * function to determine next variable-sized jfs_dirent in buffer
2680 static inline struct jfs_dirent *next_jfs_dirent(struct jfs_dirent *dirent)
2682 return (struct jfs_dirent *)
2684 ((sizeof (struct jfs_dirent) + dirent->name_len + 1 +
2685 sizeof (loff_t) - 1) &
2686 ~(sizeof (loff_t) - 1)));
2692 * function: read directory entries sequentially
2693 * from the specified entry offset
2697 * return: offset = (pn, index) of start entry
2698 * of next jfs_readdir()/dtRead()
2700 int jfs_readdir(struct file *file, struct dir_context *ctx)
2702 struct inode *ip = file_inode(file);
2703 struct nls_table *codepage = JFS_SBI(ip->i_sb)->nls_tab;
2705 loff_t dtpos; /* legacy OS/2 style position */
2710 } *dtoffset = (struct dtoffset *) &dtpos;
2712 struct metapage *mp;
2716 struct btstack btstack;
2720 int d_namleft, len, outlen;
2721 unsigned long dirent_buf;
2725 uint loop_count = 0;
2726 struct jfs_dirent *jfs_dirent;
2728 int overflow, fix_page, page_fixed = 0;
2729 static int unique_pos = 2; /* If we can't fix broken index */
2731 if (ctx->pos == DIREND)
2736 * persistent index is stored in directory entries.
2737 * Special cases: 0 = .
2739 * -1 = End of directory
2743 dir_index = (u32) ctx->pos;
2746 * NFSv4 reserves cookies 1 and 2 for . and .. so the value
2747 * we return to the vfs is one greater than the one we use
2753 if (dir_index > 1) {
2754 struct dir_table_slot dirtab_slot;
2757 (dir_index >= JFS_IP(ip)->next_index)) {
2758 /* Stale position. Directory has shrunk */
2763 rc = read_index(ip, dir_index, &dirtab_slot);
2768 if (dirtab_slot.flag == DIR_INDEX_FREE) {
2769 if (loop_count++ > JFS_IP(ip)->next_index) {
2770 jfs_err("jfs_readdir detected infinite loop!");
2774 dir_index = le32_to_cpu(dirtab_slot.addr2);
2775 if (dir_index == -1) {
2781 bn = addressDTS(&dirtab_slot);
2782 index = dirtab_slot.slot;
2783 DT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
2788 if (p->header.flag & BT_INTERNAL) {
2789 jfs_err("jfs_readdir: bad index table");
2795 if (dir_index == 0) {
2800 if (!dir_emit(ctx, ".", 1, ip->i_ino, DT_DIR))
2807 if (!dir_emit(ctx, "..", 2, PARENT(ip), DT_DIR))
2811 * Find first entry of left-most leaf
2818 if ((rc = dtReadFirst(ip, &btstack)))
2821 DT_GETSEARCH(ip, btstack.top, bn, mp, p, index);
2825 * Legacy filesystem - OS/2 & Linux JFS < 0.3.6
2827 * pn = 0; index = 1: First entry "."
2828 * pn = 0; index = 2: Second entry ".."
2829 * pn > 0: Real entries, pn=1 -> leftmost page
2830 * pn = index = -1: No more entries
2834 /* build "." entry */
2836 if (!dir_emit(ctx, ".", 1, ip->i_ino, DT_DIR))
2838 dtoffset->index = 2;
2842 if (dtoffset->pn == 0) {
2843 if (dtoffset->index == 2) {
2844 /* build ".." entry */
2845 if (!dir_emit(ctx, "..", 2, PARENT(ip), DT_DIR))
2848 jfs_err("jfs_readdir called with invalid offset!");
2851 dtoffset->index = 0;
2860 if ((rc = dtReadNext(ip, &ctx->pos, &btstack))) {
2861 jfs_err("jfs_readdir: unexpected rc = %d from dtReadNext",
2866 /* get start leaf page and index */
2867 DT_GETSEARCH(ip, btstack.top, bn, mp, p, index);
2869 /* offset beyond directory eof ? */
2876 dirent_buf = __get_free_page(GFP_KERNEL);
2877 if (dirent_buf == 0) {
2879 jfs_warn("jfs_readdir: __get_free_page failed!");
2885 jfs_dirent = (struct jfs_dirent *) dirent_buf;
2887 overflow = fix_page = 0;
2889 stbl = DT_GETSTBL(p);
2891 for (i = index; i < p->header.nextindex; i++) {
2892 d = (struct ldtentry *) & p->slot[stbl[i]];
2894 if (((long) jfs_dirent + d->namlen + 1) >
2895 (dirent_buf + PAGE_SIZE)) {
2896 /* DBCS codepages could overrun dirent_buf */
2902 d_namleft = d->namlen;
2903 name_ptr = jfs_dirent->name;
2904 jfs_dirent->ino = le32_to_cpu(d->inumber);
2907 len = min(d_namleft, DTLHDRDATALEN);
2908 jfs_dirent->position = le32_to_cpu(d->index);
2910 * d->index should always be valid, but it
2911 * isn't. fsck.jfs doesn't create the
2912 * directory index for the lost+found
2913 * directory. Rather than let it go,
2914 * we can try to fix it.
2916 if ((jfs_dirent->position < 2) ||
2917 (jfs_dirent->position >=
2918 JFS_IP(ip)->next_index)) {
2919 if (!page_fixed && !isReadOnly(ip)) {
2922 * setting overflow and setting
2923 * index to i will cause the
2924 * same page to be processed
2925 * again starting here
2931 jfs_dirent->position = unique_pos++;
2934 * We add 1 to the index because we may
2935 * use a value of 2 internally, and NFSv4
2936 * doesn't like that.
2938 jfs_dirent->position++;
2940 jfs_dirent->position = dtpos;
2941 len = min(d_namleft, DTLHDRDATALEN_LEGACY);
2944 /* copy the name of head/only segment */
2945 outlen = jfs_strfromUCS_le(name_ptr, d->name, len,
2947 jfs_dirent->name_len = outlen;
2949 /* copy name in the additional segment(s) */
2952 t = (struct dtslot *) & p->slot[next];
2956 if (d_namleft == 0) {
2958 "JFS:Dtree error: ino = %ld, bn=%lld, index = %d\n",
2964 len = min(d_namleft, DTSLOTDATALEN);
2965 outlen = jfs_strfromUCS_le(name_ptr, t->name,
2967 jfs_dirent->name_len += outlen;
2973 jfs_dirent = next_jfs_dirent(jfs_dirent);
2980 /* Point to next leaf page */
2981 if (p->header.flag & BT_ROOT)
2984 bn = le64_to_cpu(p->header.next);
2986 /* update offset (pn:index) for new page */
2989 dtoffset->index = 0;
2995 /* unpin previous leaf page */
2998 jfs_dirent = (struct jfs_dirent *) dirent_buf;
2999 while (jfs_dirents--) {
3000 ctx->pos = jfs_dirent->position;
3001 if (!dir_emit(ctx, jfs_dirent->name,
3002 jfs_dirent->name_len,
3003 jfs_dirent->ino, DT_UNKNOWN))
3005 jfs_dirent = next_jfs_dirent(jfs_dirent);
3009 add_missing_indices(ip, bn);
3013 if (!overflow && (bn == 0)) {
3018 DT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
3020 free_page(dirent_buf);
3026 free_page(dirent_buf);
3035 * function: get the leftmost page of the directory
3037 static int dtReadFirst(struct inode *ip, struct btstack * btstack)
3041 int psize = 288; /* initial in-line directory */
3042 struct metapage *mp;
3045 struct btframe *btsp;
3048 BT_CLR(btstack); /* reset stack */
3051 * descend leftmost path of the tree
3053 * by convention, root bn = 0.
3056 DT_GETPAGE(ip, bn, mp, psize, p, rc);
3061 * leftmost leaf page
3063 if (p->header.flag & BT_LEAF) {
3064 /* return leftmost entry */
3065 btsp = btstack->top;
3074 * descend down to leftmost child page
3076 if (BT_STACK_FULL(btstack)) {
3078 jfs_error(ip->i_sb, "btstack overrun\n");
3079 BT_STACK_DUMP(btstack);
3082 /* push (bn, index) of the parent page/entry */
3083 BT_PUSH(btstack, bn, 0);
3085 /* get the leftmost entry */
3086 stbl = DT_GETSTBL(p);
3087 xd = (pxd_t *) & p->slot[stbl[0]];
3089 /* get the child page block address */
3090 bn = addressPXD(xd);
3091 psize = lengthPXD(xd) << JFS_SBI(ip->i_sb)->l2bsize;
3093 /* unpin the parent page */
3102 * function: get the page of the specified offset (pn:index)
3104 * return: if (offset > eof), bn = -1;
3106 * note: if index > nextindex of the target leaf page,
3107 * start with 1st entry of next leaf page;
3109 static int dtReadNext(struct inode *ip, loff_t * offset,
3110 struct btstack * btstack)
3117 } *dtoffset = (struct dtoffset *) offset;
3119 struct metapage *mp;
3124 struct btframe *btsp, *parent;
3128 * get leftmost leaf page pinned
3130 if ((rc = dtReadFirst(ip, btstack)))
3134 DT_GETSEARCH(ip, btstack->top, bn, mp, p, index);
3136 /* get the start offset (pn:index) */
3137 pn = dtoffset->pn - 1; /* Now pn = 0 represents leftmost leaf */
3138 index = dtoffset->index;
3140 /* start at leftmost page ? */
3142 /* offset beyond eof ? */
3143 if (index < p->header.nextindex)
3146 if (p->header.flag & BT_ROOT) {
3151 /* start with 1st entry of next leaf page */
3153 dtoffset->index = index = 0;
3157 /* start at non-leftmost page: scan parent pages for large pn */
3158 if (p->header.flag & BT_ROOT) {
3163 /* start after next leaf page ? */
3167 /* get leaf page pn = 1 */
3169 bn = le64_to_cpu(p->header.next);
3171 /* unpin leaf page */
3174 /* offset beyond eof ? */
3183 * scan last internal page level to get target leaf page
3186 /* unpin leftmost leaf page */
3189 /* get left most parent page */
3190 btsp = btstack->top;
3193 DT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
3197 /* scan parent pages at last internal page level */
3198 while (pn >= p->header.nextindex) {
3199 pn -= p->header.nextindex;
3201 /* get next parent page address */
3202 bn = le64_to_cpu(p->header.next);
3204 /* unpin current parent page */
3207 /* offset beyond eof ? */
3213 /* get next parent page */
3214 DT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
3218 /* update parent page stack frame */
3222 /* get leaf page address */
3223 stbl = DT_GETSTBL(p);
3224 xd = (pxd_t *) & p->slot[stbl[pn]];
3225 bn = addressPXD(xd);
3227 /* unpin parent page */
3231 * get target leaf page
3234 DT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
3239 * leaf page has been completed:
3240 * start with 1st entry of next leaf page
3242 if (index >= p->header.nextindex) {
3243 bn = le64_to_cpu(p->header.next);
3245 /* unpin leaf page */
3248 /* offset beyond eof ? */
3254 /* get next leaf page */
3255 DT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
3259 /* start with 1st entry of next leaf page */
3261 dtoffset->index = 0;
3265 /* return target leaf page pinned */
3266 btsp = btstack->top;
3268 btsp->index = dtoffset->index;
3278 * function: compare search key with an internal entry
3281 * < 0 if k is < record
3282 * = 0 if k is = record
3283 * > 0 if k is > record
3285 static int dtCompare(struct component_name * key, /* search key */
3286 dtpage_t * p, /* directory page */
3288 { /* entry slot index */
3291 int klen, namlen, len, rc;
3292 struct idtentry *ih;
3296 * force the left-most key on internal pages, at any level of
3297 * the tree, to be less than any search key.
3298 * this obviates having to update the leftmost key on an internal
3299 * page when the user inserts a new key in the tree smaller than
3300 * anything that has been stored.
3302 * (? if/when dtSearch() narrows down to 1st entry (index = 0),
3303 * at any internal page at any level of the tree,
3304 * it descends to child of the entry anyway -
3305 * ? make the entry as min size dummy entry)
3307 * if (e->index == 0 && h->prevpg == P_INVALID && !(h->flags & BT_LEAF))
3314 ih = (struct idtentry *) & p->slot[si];
3317 namlen = ih->namlen;
3318 len = min(namlen, DTIHDRDATALEN);
3320 /* compare with head/only segment */
3321 len = min(klen, len);
3322 if ((rc = UniStrncmp_le(kname, name, len)))
3328 /* compare with additional segment(s) */
3330 while (klen > 0 && namlen > 0) {
3331 /* compare with next name segment */
3332 t = (struct dtslot *) & p->slot[si];
3333 len = min(namlen, DTSLOTDATALEN);
3334 len = min(klen, len);
3336 if ((rc = UniStrncmp_le(kname, name, len)))
3345 return (klen - namlen);
3354 * function: compare search key with an (leaf/internal) entry
3357 * < 0 if k is < record
3358 * = 0 if k is = record
3359 * > 0 if k is > record
3361 static int ciCompare(struct component_name * key, /* search key */
3362 dtpage_t * p, /* directory page */
3363 int si, /* entry slot index */
3368 int klen, namlen, len, rc;
3369 struct ldtentry *lh;
3370 struct idtentry *ih;
3375 * force the left-most key on internal pages, at any level of
3376 * the tree, to be less than any search key.
3377 * this obviates having to update the leftmost key on an internal
3378 * page when the user inserts a new key in the tree smaller than
3379 * anything that has been stored.
3381 * (? if/when dtSearch() narrows down to 1st entry (index = 0),
3382 * at any internal page at any level of the tree,
3383 * it descends to child of the entry anyway -
3384 * ? make the entry as min size dummy entry)
3386 * if (e->index == 0 && h->prevpg == P_INVALID && !(h->flags & BT_LEAF))
3396 if (p->header.flag & BT_LEAF) {
3397 lh = (struct ldtentry *) & p->slot[si];
3400 namlen = lh->namlen;
3401 if (flag & JFS_DIR_INDEX)
3402 len = min(namlen, DTLHDRDATALEN);
3404 len = min(namlen, DTLHDRDATALEN_LEGACY);
3407 * internal page entry
3410 ih = (struct idtentry *) & p->slot[si];
3413 namlen = ih->namlen;
3414 len = min(namlen, DTIHDRDATALEN);
3417 /* compare with head/only segment */
3418 len = min(klen, len);
3419 for (i = 0; i < len; i++, kname++, name++) {
3420 /* only uppercase if case-insensitive support is on */
3421 if ((flag & JFS_OS2) == JFS_OS2)
3422 x = UniToupper(le16_to_cpu(*name));
3424 x = le16_to_cpu(*name);
3425 if ((rc = *kname - x))
3432 /* compare with additional segment(s) */
3433 while (klen > 0 && namlen > 0) {
3434 /* compare with next name segment */
3435 t = (struct dtslot *) & p->slot[si];
3436 len = min(namlen, DTSLOTDATALEN);
3437 len = min(klen, len);
3439 for (i = 0; i < len; i++, kname++, name++) {
3440 /* only uppercase if case-insensitive support is on */
3441 if ((flag & JFS_OS2) == JFS_OS2)
3442 x = UniToupper(le16_to_cpu(*name));
3444 x = le16_to_cpu(*name);
3446 if ((rc = *kname - x))
3455 return (klen - namlen);
3460 * ciGetLeafPrefixKey()
3462 * function: compute prefix of suffix compression
3463 * from two adjacent leaf entries
3464 * across page boundary
3466 * return: non-zero on error
3469 static int ciGetLeafPrefixKey(dtpage_t * lp, int li, dtpage_t * rp,
3470 int ri, struct component_name * key, int flag)
3473 wchar_t *pl, *pr, *kname;
3474 struct component_name lkey;
3475 struct component_name rkey;
3477 lkey.name = kmalloc_array(JFS_NAME_MAX + 1, sizeof(wchar_t),
3479 if (lkey.name == NULL)
3482 rkey.name = kmalloc_array(JFS_NAME_MAX + 1, sizeof(wchar_t),
3484 if (rkey.name == NULL) {
3489 /* get left and right key */
3490 dtGetKey(lp, li, &lkey, flag);
3491 lkey.name[lkey.namlen] = 0;
3493 if ((flag & JFS_OS2) == JFS_OS2)
3496 dtGetKey(rp, ri, &rkey, flag);
3497 rkey.name[rkey.namlen] = 0;
3500 if ((flag & JFS_OS2) == JFS_OS2)
3503 /* compute prefix */
3506 namlen = min(lkey.namlen, rkey.namlen);
3507 for (pl = lkey.name, pr = rkey.name;
3508 namlen; pl++, pr++, namlen--, klen++, kname++) {
3511 key->namlen = klen + 1;
3516 /* l->namlen <= r->namlen since l <= r */
3517 if (lkey.namlen < rkey.namlen) {
3519 key->namlen = klen + 1;
3520 } else /* l->namelen == r->namelen */
3534 * function: get key of the entry
3536 static void dtGetKey(dtpage_t * p, int i, /* entry index */
3537 struct component_name * key, int flag)
3541 struct ldtentry *lh;
3542 struct idtentry *ih;
3549 stbl = DT_GETSTBL(p);
3551 if (p->header.flag & BT_LEAF) {
3552 lh = (struct ldtentry *) & p->slot[si];
3554 namlen = lh->namlen;
3556 if (flag & JFS_DIR_INDEX)
3557 len = min(namlen, DTLHDRDATALEN);
3559 len = min(namlen, DTLHDRDATALEN_LEGACY);
3561 ih = (struct idtentry *) & p->slot[si];
3563 namlen = ih->namlen;
3565 len = min(namlen, DTIHDRDATALEN);
3568 key->namlen = namlen;
3572 * move head/only segment
3574 UniStrncpy_from_le(kname, name, len);
3577 * move additional segment(s)
3580 /* get next segment */
3584 len = min(namlen, DTSLOTDATALEN);
3585 UniStrncpy_from_le(kname, t->name, len);
3595 * function: allocate free slot(s) and
3596 * write a leaf/internal entry
3598 * return: entry slot index
3600 static void dtInsertEntry(dtpage_t * p, int index, struct component_name * key,
3601 ddata_t * data, struct dt_lock ** dtlock)
3603 struct dtslot *h, *t;
3604 struct ldtentry *lh = NULL;
3605 struct idtentry *ih = NULL;
3606 int hsi, fsi, klen, len, nextindex;
3611 struct dt_lock *dtlck = *dtlock;
3615 struct metapage *mp = NULL;
3620 /* allocate a free slot */
3621 hsi = fsi = p->header.freelist;
3623 p->header.freelist = h->next;
3624 --p->header.freecnt;
3626 /* open new linelock */
3627 if (dtlck->index >= dtlck->maxcnt)
3628 dtlck = (struct dt_lock *) txLinelock(dtlck);
3630 lv = & dtlck->lv[dtlck->index];
3633 /* write head/only segment */
3634 if (p->header.flag & BT_LEAF) {
3635 lh = (struct ldtentry *) h;
3637 lh->inumber = cpu_to_le32(data->leaf.ino);
3640 if (data->leaf.ip) {
3641 len = min(klen, DTLHDRDATALEN);
3642 if (!(p->header.flag & BT_ROOT))
3643 bn = addressPXD(&p->header.self);
3644 lh->index = cpu_to_le32(add_index(data->leaf.tid,
3648 len = min(klen, DTLHDRDATALEN_LEGACY);
3650 ih = (struct idtentry *) h;
3656 len = min(klen, DTIHDRDATALEN);
3659 UniStrncpy_to_le(name, kname, len);
3664 /* write additional segment(s) */
3669 fsi = p->header.freelist;
3671 p->header.freelist = t->next;
3672 --p->header.freecnt;
3674 /* is next slot contiguous ? */
3675 if (fsi != xsi + 1) {
3676 /* close current linelock */
3680 /* open new linelock */
3681 if (dtlck->index < dtlck->maxcnt)
3684 dtlck = (struct dt_lock *) txLinelock(dtlck);
3685 lv = & dtlck->lv[0];
3693 len = min(klen, DTSLOTDATALEN);
3694 UniStrncpy_to_le(t->name, kname, len);
3701 /* close current linelock */
3707 /* terminate last/only segment */
3709 /* single segment entry */
3710 if (p->header.flag & BT_LEAF)
3715 /* multi-segment entry */
3718 /* if insert into middle, shift right succeeding entries in stbl */
3719 stbl = DT_GETSTBL(p);
3720 nextindex = p->header.nextindex;
3721 if (index < nextindex) {
3722 memmove(stbl + index + 1, stbl + index, nextindex - index);
3724 if ((p->header.flag & BT_LEAF) && data->leaf.ip) {
3728 * Need to update slot number for entries that moved
3732 for (n = index + 1; n <= nextindex; n++) {
3733 lh = (struct ldtentry *) & (p->slot[stbl[n]]);
3734 modify_index(data->leaf.tid, data->leaf.ip,
3735 le32_to_cpu(lh->index), bn, n,
3739 release_metapage(mp);
3745 /* advance next available entry index of stbl */
3746 ++p->header.nextindex;
3753 * function: move entries from split/left page to new/right page
3755 * nextindex of dst page and freelist/freecnt of both pages
3758 static void dtMoveEntry(dtpage_t * sp, int si, dtpage_t * dp,
3759 struct dt_lock ** sdtlock, struct dt_lock ** ddtlock,
3762 int ssi, next; /* src slot index */
3763 int di; /* dst entry index */
3764 int dsi; /* dst slot index */
3765 s8 *sstbl, *dstbl; /* sorted entry table */
3767 struct ldtentry *slh, *dlh = NULL;
3768 struct idtentry *sih, *dih = NULL;
3769 struct dtslot *h, *s, *d;
3770 struct dt_lock *sdtlck = *sdtlock, *ddtlck = *ddtlock;
3771 struct lv *slv, *dlv;
3775 sstbl = (s8 *) & sp->slot[sp->header.stblindex];
3776 dstbl = (s8 *) & dp->slot[dp->header.stblindex];
3778 dsi = dp->header.freelist; /* first (whole page) free slot */
3779 sfsi = sp->header.freelist;
3781 /* linelock destination entry slot */
3782 dlv = & ddtlck->lv[ddtlck->index];
3785 /* linelock source entry slot */
3786 slv = & sdtlck->lv[sdtlck->index];
3787 slv->offset = sstbl[si];
3788 xssi = slv->offset - 1;
3794 for (di = 0; si < sp->header.nextindex; si++, di++) {
3798 /* is next slot contiguous ? */
3799 if (ssi != xssi + 1) {
3800 /* close current linelock */
3804 /* open new linelock */
3805 if (sdtlck->index < sdtlck->maxcnt)
3808 sdtlck = (struct dt_lock *) txLinelock(sdtlck);
3809 slv = & sdtlck->lv[0];
3817 * move head/only segment of an entry
3820 h = d = &dp->slot[dsi];
3822 /* get src slot and move */
3824 if (sp->header.flag & BT_LEAF) {
3825 /* get source entry */
3826 slh = (struct ldtentry *) s;
3827 dlh = (struct ldtentry *) h;
3828 snamlen = slh->namlen;
3831 len = min(snamlen, DTLHDRDATALEN);
3832 dlh->index = slh->index; /* little-endian */
3834 len = min(snamlen, DTLHDRDATALEN_LEGACY);
3836 memcpy(dlh, slh, 6 + len * 2);
3840 /* update dst head/only segment next field */
3844 sih = (struct idtentry *) s;
3845 snamlen = sih->namlen;
3847 len = min(snamlen, DTIHDRDATALEN);
3848 dih = (struct idtentry *) h;
3849 memcpy(dih, sih, 10 + len * 2);
3856 /* free src head/only segment */
3866 * move additional segment(s) of the entry
3869 while ((ssi = next) >= 0) {
3870 /* is next slot contiguous ? */
3871 if (ssi != xssi + 1) {
3872 /* close current linelock */
3876 /* open new linelock */
3877 if (sdtlck->index < sdtlck->maxcnt)
3883 slv = & sdtlck->lv[0];
3890 /* get next source segment */
3893 /* get next destination free slot */
3896 len = min(snamlen, DTSLOTDATALEN);
3897 UniStrncpy_le(d->name, s->name, len);
3906 /* free source segment */
3915 /* terminate dst last/only segment */
3917 /* single segment entry */
3918 if (dp->header.flag & BT_LEAF)
3923 /* multi-segment entry */
3927 /* close current linelock */
3936 /* update source header */
3937 sp->header.freelist = sfsi;
3938 sp->header.freecnt += nd;
3940 /* update destination header */
3941 dp->header.nextindex = di;
3943 dp->header.freelist = dsi;
3944 dp->header.freecnt -= nd;
3951 * function: free a (leaf/internal) entry
3953 * log freelist header, stbl, and each segment slot of entry
3954 * (even though last/only segment next field is modified,
3955 * physical image logging requires all segment slots of
3956 * the entry logged to avoid applying previous updates
3957 * to the same slots)
3959 static void dtDeleteEntry(dtpage_t * p, int fi, struct dt_lock ** dtlock)
3961 int fsi; /* free entry slot index */
3965 struct dt_lock *dtlck = *dtlock;
3969 /* get free entry slot index */
3970 stbl = DT_GETSTBL(p);
3973 /* open new linelock */
3974 if (dtlck->index >= dtlck->maxcnt)
3975 dtlck = (struct dt_lock *) txLinelock(dtlck);
3976 lv = & dtlck->lv[dtlck->index];
3980 /* get the head/only segment */
3982 if (p->header.flag & BT_LEAF)
3983 si = ((struct ldtentry *) t)->next;
3985 si = ((struct idtentry *) t)->next;
3992 /* find the last/only segment */
3994 /* is next slot contiguous ? */
3995 if (si != xsi + 1) {
3996 /* close current linelock */
4000 /* open new linelock */
4001 if (dtlck->index < dtlck->maxcnt)
4004 dtlck = (struct dt_lock *) txLinelock(dtlck);
4005 lv = & dtlck->lv[0];
4021 /* close current linelock */
4027 /* update freelist */
4028 t->next = p->header.freelist;
4029 p->header.freelist = fsi;
4030 p->header.freecnt += freecnt;
4032 /* if delete from middle,
4033 * shift left the succedding entries in the stbl
4035 si = p->header.nextindex;
4037 memmove(&stbl[fi], &stbl[fi + 1], si - fi - 1);
4039 p->header.nextindex--;
4046 * function: truncate a (leaf/internal) entry
4048 * log freelist header, stbl, and each segment slot of entry
4049 * (even though last/only segment next field is modified,
4050 * physical image logging requires all segment slots of
4051 * the entry logged to avoid applying previous updates
4052 * to the same slots)
4054 static void dtTruncateEntry(dtpage_t * p, int ti, struct dt_lock ** dtlock)
4056 int tsi; /* truncate entry slot index */
4060 struct dt_lock *dtlck = *dtlock;
4064 /* get free entry slot index */
4065 stbl = DT_GETSTBL(p);
4068 /* open new linelock */
4069 if (dtlck->index >= dtlck->maxcnt)
4070 dtlck = (struct dt_lock *) txLinelock(dtlck);
4071 lv = & dtlck->lv[dtlck->index];
4075 /* get the head/only segment */
4077 ASSERT(p->header.flag & BT_INTERNAL);
4078 ((struct idtentry *) t)->namlen = 0;
4079 si = ((struct idtentry *) t)->next;
4080 ((struct idtentry *) t)->next = -1;
4087 /* find the last/only segment */
4089 /* is next slot contiguous ? */
4090 if (si != xsi + 1) {
4091 /* close current linelock */
4095 /* open new linelock */
4096 if (dtlck->index < dtlck->maxcnt)
4099 dtlck = (struct dt_lock *) txLinelock(dtlck);
4100 lv = & dtlck->lv[0];
4116 /* close current linelock */
4122 /* update freelist */
4125 t->next = p->header.freelist;
4126 p->header.freelist = fsi;
4127 p->header.freecnt += freecnt;
4132 * dtLinelockFreelist()
4134 static void dtLinelockFreelist(dtpage_t * p, /* directory page */
4135 int m, /* max slot index */
4136 struct dt_lock ** dtlock)
4138 int fsi; /* free entry slot index */
4141 struct dt_lock *dtlck = *dtlock;
4145 /* get free entry slot index */
4146 fsi = p->header.freelist;
4148 /* open new linelock */
4149 if (dtlck->index >= dtlck->maxcnt)
4150 dtlck = (struct dt_lock *) txLinelock(dtlck);
4151 lv = & dtlck->lv[dtlck->index];
4161 /* find the last/only segment */
4162 while (si < m && si >= 0) {
4163 /* is next slot contiguous ? */
4164 if (si != xsi + 1) {
4165 /* close current linelock */
4169 /* open new linelock */
4170 if (dtlck->index < dtlck->maxcnt)
4173 dtlck = (struct dt_lock *) txLinelock(dtlck);
4174 lv = & dtlck->lv[0];
4188 /* close current linelock */
4199 * FUNCTION: Modify the inode number part of a directory entry
4202 * tid - Transaction id
4203 * ip - Inode of parent directory
4204 * key - Name of entry to be modified
4205 * orig_ino - Original inode number expected in entry
4206 * new_ino - New inode number to put into entry
4210 * -ESTALE - If entry found does not match orig_ino passed in
4211 * -ENOENT - If no entry can be found to match key
4212 * 0 - If successfully modified entry
4214 int dtModify(tid_t tid, struct inode *ip,
4215 struct component_name * key, ino_t * orig_ino, ino_t new_ino, int flag)
4219 struct metapage *mp;
4222 struct btstack btstack;
4224 struct dt_lock *dtlck;
4227 int entry_si; /* entry slot index */
4228 struct ldtentry *entry;
4231 * search for the entry to modify:
4233 * dtSearch() returns (leaf page pinned, index at which to modify).
4235 if ((rc = dtSearch(ip, key, orig_ino, &btstack, flag)))
4238 /* retrieve search result */
4239 DT_GETSEARCH(ip, btstack.top, bn, mp, p, index);
4241 BT_MARK_DIRTY(mp, ip);
4243 * acquire a transaction lock on the leaf page of named entry
4245 tlck = txLock(tid, ip, mp, tlckDTREE | tlckENTRY);
4246 dtlck = (struct dt_lock *) & tlck->lock;
4248 /* get slot index of the entry */
4249 stbl = DT_GETSTBL(p);
4250 entry_si = stbl[index];
4252 /* linelock entry */
4253 ASSERT(dtlck->index == 0);
4254 lv = & dtlck->lv[0];
4255 lv->offset = entry_si;
4259 /* get the head/only segment */
4260 entry = (struct ldtentry *) & p->slot[entry_si];
4262 /* substitute the inode number of the entry */
4263 entry->inumber = cpu_to_le32(new_ino);
4265 /* unpin the leaf page */