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) || \
121 ((P)->header.stblindex >= DTPAGEMAXSLOT)))) { \
123 jfs_error((IP)->i_sb, \
124 "DT_GETPAGE: dtree page corrupt\n"); \
131 /* for consistency */
132 #define DT_PUTPAGE(MP) BT_PUTPAGE(MP)
134 #define DT_GETSEARCH(IP, LEAF, BN, MP, P, INDEX) \
135 BT_GETSEARCH(IP, LEAF, BN, MP, dtpage_t, P, INDEX, i_dtroot)
140 static int dtSplitUp(tid_t tid, struct inode *ip,
141 struct dtsplit * split, struct btstack * btstack);
143 static int dtSplitPage(tid_t tid, struct inode *ip, struct dtsplit * split,
144 struct metapage ** rmpp, dtpage_t ** rpp, pxd_t * rxdp);
146 static int dtExtendPage(tid_t tid, struct inode *ip,
147 struct dtsplit * split, struct btstack * btstack);
149 static int dtSplitRoot(tid_t tid, struct inode *ip,
150 struct dtsplit * split, struct metapage ** rmpp);
152 static int dtDeleteUp(tid_t tid, struct inode *ip, struct metapage * fmp,
153 dtpage_t * fp, struct btstack * btstack);
155 static int dtRelink(tid_t tid, struct inode *ip, dtpage_t * p);
157 static int dtReadFirst(struct inode *ip, struct btstack * btstack);
159 static int dtReadNext(struct inode *ip,
160 loff_t * offset, struct btstack * btstack);
162 static int dtCompare(struct component_name * key, dtpage_t * p, int si);
164 static int ciCompare(struct component_name * key, dtpage_t * p, int si,
167 static void dtGetKey(dtpage_t * p, int i, struct component_name * key,
170 static int ciGetLeafPrefixKey(dtpage_t * lp, int li, dtpage_t * rp,
171 int ri, struct component_name * key, int flag);
173 static void dtInsertEntry(dtpage_t * p, int index, struct component_name * key,
174 ddata_t * data, struct dt_lock **);
176 static void dtMoveEntry(dtpage_t * sp, int si, dtpage_t * dp,
177 struct dt_lock ** sdtlock, struct dt_lock ** ddtlock,
180 static void dtDeleteEntry(dtpage_t * p, int fi, struct dt_lock ** dtlock);
182 static void dtTruncateEntry(dtpage_t * p, int ti, struct dt_lock ** dtlock);
184 static void dtLinelockFreelist(dtpage_t * p, int m, struct dt_lock ** dtlock);
186 #define ciToUpper(c) UniStrupr((c)->name)
191 * Reads a page of a directory's index table.
192 * Having metadata mapped into the directory inode's address space
193 * presents a multitude of problems. We avoid this by mapping to
194 * the absolute address space outside of the *_metapage routines
196 static struct metapage *read_index_page(struct inode *inode, s64 blkno)
203 rc = xtLookup(inode, blkno, 1, &xflag, &xaddr, &xlen, 1);
204 if (rc || (xaddr == 0))
207 return read_metapage(inode, xaddr, PSIZE, 1);
213 * Same as get_index_page(), but get's a new page without reading
215 static struct metapage *get_index_page(struct inode *inode, s64 blkno)
222 rc = xtLookup(inode, blkno, 1, &xflag, &xaddr, &xlen, 1);
223 if (rc || (xaddr == 0))
226 return get_metapage(inode, xaddr, PSIZE, 1);
232 * Returns dtree page containing directory table entry for specified
233 * index and pointer to its entry.
235 * mp must be released by caller.
237 static struct dir_table_slot *find_index(struct inode *ip, u32 index,
238 struct metapage ** mp, s64 *lblock)
240 struct jfs_inode_info *jfs_ip = JFS_IP(ip);
244 struct dir_table_slot *slot;
245 static int maxWarnings = 10;
249 jfs_warn("find_entry called with index = %d", index);
255 if (index >= jfs_ip->next_index) {
256 jfs_warn("find_entry called with index >= next_index");
260 if (jfs_dirtable_inline(ip)) {
262 * Inline directory table
265 slot = &jfs_ip->i_dirtable[index - 2];
267 offset = (index - 2) * sizeof(struct dir_table_slot);
268 page_offset = offset & (PSIZE - 1);
269 blkno = ((offset + 1) >> L2PSIZE) <<
270 JFS_SBI(ip->i_sb)->l2nbperpage;
272 if (*mp && (*lblock != blkno)) {
273 release_metapage(*mp);
278 *mp = read_index_page(ip, blkno);
281 jfs_err("free_index: error reading directory table");
286 (struct dir_table_slot *) ((char *) (*mp)->data +
292 static inline void lock_index(tid_t tid, struct inode *ip, struct metapage * mp,
296 struct linelock *llck;
299 tlck = txLock(tid, ip, mp, tlckDATA);
300 llck = (struct linelock *) tlck->lock;
302 if (llck->index >= llck->maxcnt)
303 llck = txLinelock(llck);
304 lv = &llck->lv[llck->index];
307 * Linelock slot size is twice the size of directory table
308 * slot size. 512 entries per page.
310 lv->offset = ((index - 2) & 511) >> 1;
318 * Adds an entry to the directory index table. This is used to provide
319 * each directory entry with a persistent index in which to resume
320 * directory traversals
322 static u32 add_index(tid_t tid, struct inode *ip, s64 bn, int slot)
324 struct super_block *sb = ip->i_sb;
325 struct jfs_sb_info *sbi = JFS_SBI(sb);
326 struct jfs_inode_info *jfs_ip = JFS_IP(ip);
328 struct dir_table_slot *dirtab_slot;
330 struct linelock *llck;
338 ASSERT(DO_INDEX(ip));
340 if (jfs_ip->next_index < 2) {
341 jfs_warn("add_index: next_index = %d. Resetting!",
343 jfs_ip->next_index = 2;
346 index = jfs_ip->next_index++;
348 if (index <= MAX_INLINE_DIRTABLE_ENTRY) {
350 * i_size reflects size of index table, or 8 bytes per entry.
352 ip->i_size = (loff_t) (index - 1) << 3;
355 * dir table fits inline within inode
357 dirtab_slot = &jfs_ip->i_dirtable[index-2];
358 dirtab_slot->flag = DIR_INDEX_VALID;
359 dirtab_slot->slot = slot;
360 DTSaddress(dirtab_slot, bn);
362 set_cflag(COMMIT_Dirtable, ip);
366 if (index == (MAX_INLINE_DIRTABLE_ENTRY + 1)) {
367 struct dir_table_slot temp_table[12];
370 * It's time to move the inline table to an external
371 * page and begin to build the xtree
373 if (dquot_alloc_block(ip, sbi->nbperpage))
375 if (dbAlloc(ip, 0, sbi->nbperpage, &xaddr)) {
376 dquot_free_block(ip, sbi->nbperpage);
381 * Save the table, we're going to overwrite it with the
384 memcpy(temp_table, &jfs_ip->i_dirtable, sizeof(temp_table));
387 * Initialize empty x-tree
392 * Add the first block to the xtree
394 if (xtInsert(tid, ip, 0, 0, sbi->nbperpage, &xaddr, 0)) {
395 /* This really shouldn't fail */
396 jfs_warn("add_index: xtInsert failed!");
397 memcpy(&jfs_ip->i_dirtable, temp_table,
398 sizeof (temp_table));
399 dbFree(ip, xaddr, sbi->nbperpage);
400 dquot_free_block(ip, sbi->nbperpage);
405 mp = get_index_page(ip, 0);
407 jfs_err("add_index: get_metapage failed!");
408 xtTruncate(tid, ip, 0, COMMIT_PWMAP);
409 memcpy(&jfs_ip->i_dirtable, temp_table,
410 sizeof (temp_table));
413 tlck = txLock(tid, ip, mp, tlckDATA);
414 llck = (struct linelock *) & tlck->lock;
415 ASSERT(llck->index == 0);
419 lv->length = 6; /* tlckDATA slot size is 16 bytes */
422 memcpy(mp->data, temp_table, sizeof(temp_table));
424 mark_metapage_dirty(mp);
425 release_metapage(mp);
428 * Logging is now directed by xtree tlocks
430 clear_cflag(COMMIT_Dirtable, ip);
433 offset = (index - 2) * sizeof(struct dir_table_slot);
434 page_offset = offset & (PSIZE - 1);
435 blkno = ((offset + 1) >> L2PSIZE) << sbi->l2nbperpage;
436 if (page_offset == 0) {
438 * This will be the beginning of a new page
441 if (xtInsert(tid, ip, 0, blkno, sbi->nbperpage, &xaddr, 0)) {
442 jfs_warn("add_index: xtInsert failed!");
447 if ((mp = get_index_page(ip, blkno)))
448 memset(mp->data, 0, PSIZE); /* Just looks better */
450 xtTruncate(tid, ip, offset, COMMIT_PWMAP);
452 mp = read_index_page(ip, blkno);
455 jfs_err("add_index: get/read_metapage failed!");
459 lock_index(tid, ip, mp, index);
462 (struct dir_table_slot *) ((char *) mp->data + page_offset);
463 dirtab_slot->flag = DIR_INDEX_VALID;
464 dirtab_slot->slot = slot;
465 DTSaddress(dirtab_slot, bn);
467 mark_metapage_dirty(mp);
468 release_metapage(mp);
474 jfs_ip->next_index--;
482 * Marks an entry to the directory index table as free.
484 static void free_index(tid_t tid, struct inode *ip, u32 index, u32 next)
486 struct dir_table_slot *dirtab_slot;
488 struct metapage *mp = NULL;
490 dirtab_slot = find_index(ip, index, &mp, &lblock);
495 dirtab_slot->flag = DIR_INDEX_FREE;
496 dirtab_slot->slot = dirtab_slot->addr1 = 0;
497 dirtab_slot->addr2 = cpu_to_le32(next);
500 lock_index(tid, ip, mp, index);
501 mark_metapage_dirty(mp);
502 release_metapage(mp);
504 set_cflag(COMMIT_Dirtable, ip);
510 * Changes an entry in the directory index table
512 static void modify_index(tid_t tid, struct inode *ip, u32 index, s64 bn,
513 int slot, struct metapage ** mp, s64 *lblock)
515 struct dir_table_slot *dirtab_slot;
517 dirtab_slot = find_index(ip, index, mp, lblock);
522 DTSaddress(dirtab_slot, bn);
523 dirtab_slot->slot = slot;
526 lock_index(tid, ip, *mp, index);
527 mark_metapage_dirty(*mp);
529 set_cflag(COMMIT_Dirtable, ip);
535 * reads a directory table slot
537 static int read_index(struct inode *ip, u32 index,
538 struct dir_table_slot * dirtab_slot)
541 struct metapage *mp = NULL;
542 struct dir_table_slot *slot;
544 slot = find_index(ip, index, &mp, &lblock);
549 memcpy(dirtab_slot, slot, sizeof(struct dir_table_slot));
552 release_metapage(mp);
561 * Search for the entry with specified key
565 * return: 0 - search result on stack, leaf page pinned;
568 int dtSearch(struct inode *ip, struct component_name * key, ino_t * data,
569 struct btstack * btstack, int flag)
572 int cmp = 1; /* init for empty page */
577 int base, index, lim;
578 struct btframe *btsp;
580 int psize = 288; /* initial in-line directory */
582 struct component_name ciKey;
583 struct super_block *sb = ip->i_sb;
585 ciKey.name = kmalloc_array(JFS_NAME_MAX + 1, sizeof(wchar_t),
593 /* uppercase search key for c-i directory */
594 UniStrcpy(ciKey.name, key->name);
595 ciKey.namlen = key->namlen;
597 /* only uppercase if case-insensitive support is on */
598 if ((JFS_SBI(sb)->mntflag & JFS_OS2) == JFS_OS2) {
601 BT_CLR(btstack); /* reset stack */
603 /* init level count for max pages to split */
607 * search down tree from root:
609 * between two consecutive entries of <Ki, Pi> and <Kj, Pj> of
610 * internal page, child page Pi contains entry with k, Ki <= K < Kj.
612 * if entry with search key K is not found
613 * internal page search find the entry with largest key Ki
614 * less than K which point to the child page to search;
615 * leaf page search find the entry with smallest key Kj
616 * greater than K so that the returned index is the position of
617 * the entry to be shifted right for insertion of new entry.
618 * for empty tree, search key is greater than any key of the tree.
620 * by convention, root bn = 0.
623 /* get/pin the page to search */
624 DT_GETPAGE(ip, bn, mp, psize, p, rc);
628 /* get sorted entry table of the page */
629 stbl = DT_GETSTBL(p);
632 * binary search with search key K on the current page.
634 for (base = 0, lim = p->header.nextindex; lim; lim >>= 1) {
635 index = base + (lim >> 1);
637 if (stbl[index] < 0) {
642 if (p->header.flag & BT_LEAF) {
643 /* uppercase leaf name to compare */
645 ciCompare(&ciKey, p, stbl[index],
646 JFS_SBI(sb)->mntflag);
648 /* router key is in uppercase */
650 cmp = dtCompare(&ciKey, p, stbl[index]);
658 /* search hit - leaf page:
659 * return the entry found
661 if (p->header.flag & BT_LEAF) {
662 inumber = le32_to_cpu(
663 ((struct ldtentry *) & p->slot[stbl[index]])->inumber);
666 * search for JFS_LOOKUP
668 if (flag == JFS_LOOKUP) {
675 * search for JFS_CREATE
677 if (flag == JFS_CREATE) {
684 * search for JFS_REMOVE or JFS_RENAME
686 if ((flag == JFS_REMOVE ||
687 flag == JFS_RENAME) &&
694 * JFS_REMOVE|JFS_FINDDIR|JFS_RENAME
696 /* save search result */
707 /* search hit - internal page:
708 * descend/search its child page
722 * base is the smallest index with key (Kj) greater than
723 * search key (K) and may be zero or (maxindex + 1) index.
726 * search miss - leaf page
728 * return location of entry (base) where new entry with
729 * search key K is to be inserted.
731 if (p->header.flag & BT_LEAF) {
733 * search for JFS_LOOKUP, JFS_REMOVE, or JFS_RENAME
735 if (flag == JFS_LOOKUP || flag == JFS_REMOVE ||
736 flag == JFS_RENAME) {
742 * search for JFS_CREATE|JFS_FINDDIR:
757 * search miss - internal page
759 * if base is non-zero, decrement base by one to get the parent
760 * entry of the child page to search.
762 index = base ? base - 1 : base;
765 * go down to child page
768 /* update max. number of pages to split */
769 if (BT_STACK_FULL(btstack)) {
770 /* Something's corrupted, mark filesystem dirty so
771 * chkdsk will fix it.
773 jfs_error(sb, "stack overrun!\n");
774 BT_STACK_DUMP(btstack);
780 /* push (bn, index) of the parent page/entry */
781 BT_PUSH(btstack, bn, index);
783 /* get the child page block number */
784 pxd = (pxd_t *) & p->slot[stbl[index]];
785 bn = addressPXD(pxd);
786 psize = lengthPXD(pxd) << JFS_SBI(ip->i_sb)->l2bsize;
788 /* unpin the parent page */
808 * function: insert an entry to directory tree
812 * return: 0 - success;
815 int dtInsert(tid_t tid, struct inode *ip,
816 struct component_name * name, ino_t * fsn, struct btstack * btstack)
819 struct metapage *mp; /* meta-page buffer */
820 dtpage_t *p; /* base B+-tree index page */
823 struct dtsplit split; /* split information */
825 struct dt_lock *dtlck;
831 * retrieve search result
833 * dtSearch() returns (leaf page pinned, index at which to insert).
834 * n.b. dtSearch() may return index of (maxindex + 1) of
837 DT_GETSEARCH(ip, btstack->top, bn, mp, p, index);
838 if (p->header.freelist == 0)
842 * insert entry for new key
845 if (JFS_IP(ip)->next_index == DIREND) {
849 n = NDTLEAF(name->namlen);
853 n = NDTLEAF_LEGACY(name->namlen);
854 data.leaf.ip = NULL; /* signifies legacy directory format */
856 data.leaf.ino = *fsn;
859 * leaf page does not have enough room for new entry:
861 * extend/split the leaf page;
863 * dtSplitUp() will insert the entry and unpin the leaf page.
865 if (n > p->header.freecnt) {
871 rc = dtSplitUp(tid, ip, &split, btstack);
876 * leaf page does have enough room for new entry:
878 * insert the new data entry into the leaf page;
880 BT_MARK_DIRTY(mp, ip);
882 * acquire a transaction lock on the leaf page
884 tlck = txLock(tid, ip, mp, tlckDTREE | tlckENTRY);
885 dtlck = (struct dt_lock *) & tlck->lock;
886 ASSERT(dtlck->index == 0);
889 /* linelock header */
894 dtInsertEntry(p, index, name, &data, &dtlck);
896 /* linelock stbl of non-root leaf page */
897 if (!(p->header.flag & BT_ROOT)) {
898 if (dtlck->index >= dtlck->maxcnt)
899 dtlck = (struct dt_lock *) txLinelock(dtlck);
900 lv = & dtlck->lv[dtlck->index];
901 n = index >> L2DTSLOTSIZE;
902 lv->offset = p->header.stblindex + n;
904 ((p->header.nextindex - 1) >> L2DTSLOTSIZE) - n + 1;
908 /* unpin the leaf page */
918 * function: propagate insertion bottom up;
922 * return: 0 - success;
924 * leaf page unpinned;
926 static int dtSplitUp(tid_t tid,
927 struct inode *ip, struct dtsplit * split, struct btstack * btstack)
929 struct jfs_sb_info *sbi = JFS_SBI(ip->i_sb);
931 struct metapage *smp;
932 dtpage_t *sp; /* split page */
933 struct metapage *rmp;
934 dtpage_t *rp; /* new right page split from sp */
935 pxd_t rpxd; /* new right page extent descriptor */
936 struct metapage *lmp;
937 dtpage_t *lp; /* left child page */
938 int skip; /* index of entry of insertion */
939 struct btframe *parent; /* parent page entry on traverse stack */
942 struct pxdlist pxdlist;
944 struct component_name key = { 0, NULL };
945 ddata_t *data = split->data;
947 struct dt_lock *dtlck;
950 int quota_allocation = 0;
954 sp = DT_PAGE(ip, smp);
956 key.name = kmalloc_array(JFS_NAME_MAX + 2, sizeof(wchar_t), GFP_NOFS);
966 * The split routines insert the new entry, and
967 * acquire txLock as appropriate.
970 * split root leaf page:
972 if (sp->header.flag & BT_ROOT) {
974 * allocate a single extent child page
977 n = sbi->bsize >> L2DTSLOTSIZE;
978 n -= (n + 31) >> L2DTSLOTSIZE; /* stbl size */
979 n -= DTROOTMAXSLOT - sp->header.freecnt; /* header + entries */
980 if (n <= split->nslot)
982 if ((rc = dbAlloc(ip, 0, (s64) xlen, &xaddr))) {
989 pxd = &pxdlist.pxd[0];
990 PXDaddress(pxd, xaddr);
991 PXDlength(pxd, xlen);
992 split->pxdlist = &pxdlist;
993 rc = dtSplitRoot(tid, ip, split, &rmp);
996 dbFree(ip, xaddr, xlen);
1003 ip->i_size = xlen << sbi->l2bsize;
1009 * extend first leaf page
1011 * extend the 1st extent if less than buffer page size
1012 * (dtExtendPage() reurns leaf page unpinned)
1014 pxd = &sp->header.self;
1015 xlen = lengthPXD(pxd);
1016 xsize = xlen << sbi->l2bsize;
1017 if (xsize < PSIZE) {
1018 xaddr = addressPXD(pxd);
1019 n = xsize >> L2DTSLOTSIZE;
1020 n -= (n + 31) >> L2DTSLOTSIZE; /* stbl size */
1021 if ((n + sp->header.freecnt) <= split->nslot)
1022 n = xlen + (xlen << 1);
1026 /* Allocate blocks to quota. */
1027 rc = dquot_alloc_block(ip, n);
1030 quota_allocation += n;
1032 if ((rc = dbReAlloc(sbi->ipbmap, xaddr, (s64) xlen,
1036 pxdlist.maxnpxd = 1;
1038 pxd = &pxdlist.pxd[0];
1039 PXDaddress(pxd, nxaddr);
1040 PXDlength(pxd, xlen + n);
1041 split->pxdlist = &pxdlist;
1042 if ((rc = dtExtendPage(tid, ip, split, btstack))) {
1043 nxaddr = addressPXD(pxd);
1044 if (xaddr != nxaddr) {
1045 /* free relocated extent */
1046 xlen = lengthPXD(pxd);
1047 dbFree(ip, nxaddr, (s64) xlen);
1049 /* free extended delta */
1050 xlen = lengthPXD(pxd) - n;
1051 xaddr = addressPXD(pxd) + xlen;
1052 dbFree(ip, xaddr, (s64) n);
1054 } else if (!DO_INDEX(ip))
1055 ip->i_size = lengthPXD(pxd) << sbi->l2bsize;
1064 * split leaf page <sp> into <sp> and a new right page <rp>.
1066 * return <rp> pinned and its extent descriptor <rpxd>
1069 * allocate new directory page extent and
1070 * new index page(s) to cover page split(s)
1072 * allocation hint: ?
1074 n = btstack->nsplit;
1075 pxdlist.maxnpxd = pxdlist.npxd = 0;
1076 xlen = sbi->nbperpage;
1077 for (pxd = pxdlist.pxd; n > 0; n--, pxd++) {
1078 if ((rc = dbAlloc(ip, 0, (s64) xlen, &xaddr)) == 0) {
1079 PXDaddress(pxd, xaddr);
1080 PXDlength(pxd, xlen);
1087 /* undo allocation */
1091 split->pxdlist = &pxdlist;
1092 if ((rc = dtSplitPage(tid, ip, split, &rmp, &rp, &rpxd))) {
1095 /* undo allocation */
1100 ip->i_size += PSIZE;
1103 * propagate up the router entry for the leaf page just split
1105 * insert a router entry for the new page into the parent page,
1106 * propagate the insert/split up the tree by walking back the stack
1107 * of (bn of parent page, index of child page entry in parent page)
1108 * that were traversed during the search for the page that split.
1110 * the propagation of insert/split up the tree stops if the root
1111 * splits or the page inserted into doesn't have to split to hold
1114 * the parent entry for the split page remains the same, and
1115 * a new entry is inserted at its right with the first key and
1116 * block number of the new right page.
1118 * There are a maximum of 4 pages pinned at any time:
1119 * two children, left parent and right parent (when the parent splits).
1120 * keep the child pages pinned while working on the parent.
1121 * make sure that all pins are released at exit.
1123 while ((parent = BT_POP(btstack)) != NULL) {
1124 /* parent page specified by stack frame <parent> */
1126 /* keep current child pages (<lp>, <rp>) pinned */
1131 * insert router entry in parent for new right child page <rp>
1133 /* get the parent page <sp> */
1134 DT_GETPAGE(ip, parent->bn, smp, PSIZE, sp, rc);
1142 * The new key entry goes ONE AFTER the index of parent entry,
1143 * because the split was to the right.
1145 skip = parent->index + 1;
1148 * compute the key for the router entry
1150 * key suffix compression:
1151 * for internal pages that have leaf pages as children,
1152 * retain only what's needed to distinguish between
1153 * the new entry and the entry on the page to its left.
1154 * If the keys compare equal, retain the entire key.
1156 * note that compression is performed only at computing
1157 * router key at the lowest internal level.
1158 * further compression of the key between pairs of higher
1159 * level internal pages loses too much information and
1160 * the search may fail.
1161 * (e.g., two adjacent leaf pages of {a, ..., x} {xx, ...,}
1162 * results in two adjacent parent entries (a)(xx).
1163 * if split occurs between these two entries, and
1164 * if compression is applied, the router key of parent entry
1165 * of right page (x) will divert search for x into right
1166 * subtree and miss x in the left subtree.)
1168 * the entire key must be retained for the next-to-leftmost
1169 * internal key at any level of the tree, or search may fail
1172 switch (rp->header.flag & BT_TYPE) {
1175 * compute the length of prefix for suffix compression
1176 * between last entry of left page and first entry
1179 if ((sp->header.flag & BT_ROOT && skip > 1) ||
1180 sp->header.prev != 0 || skip > 1) {
1181 /* compute uppercase router prefix key */
1182 rc = ciGetLeafPrefixKey(lp,
1183 lp->header.nextindex-1,
1193 /* next to leftmost entry of
1194 lowest internal level */
1196 /* compute uppercase router key */
1197 dtGetKey(rp, 0, &key, sbi->mntflag);
1198 key.name[key.namlen] = 0;
1200 if ((sbi->mntflag & JFS_OS2) == JFS_OS2)
1204 n = NDTINTERNAL(key.namlen);
1208 dtGetKey(rp, 0, &key, sbi->mntflag);
1209 n = NDTINTERNAL(key.namlen);
1213 jfs_err("dtSplitUp(): UFO!");
1217 /* unpin left child page */
1221 * compute the data for the router entry
1223 data->xd = rpxd; /* child page xd */
1226 * parent page is full - split the parent page
1228 if (n > sp->header.freecnt) {
1229 /* init for parent page split */
1231 split->index = skip; /* index at insert */
1234 /* split->data = data; */
1236 /* unpin right child page */
1239 /* The split routines insert the new entry,
1240 * acquire txLock as appropriate.
1241 * return <rp> pinned and its block number <rbn>.
1243 rc = (sp->header.flag & BT_ROOT) ?
1244 dtSplitRoot(tid, ip, split, &rmp) :
1245 dtSplitPage(tid, ip, split, &rmp, &rp, &rpxd);
1251 /* smp and rmp are pinned */
1254 * parent page is not full - insert router entry in parent page
1257 BT_MARK_DIRTY(smp, ip);
1259 * acquire a transaction lock on the parent page
1261 tlck = txLock(tid, ip, smp, tlckDTREE | tlckENTRY);
1262 dtlck = (struct dt_lock *) & tlck->lock;
1263 ASSERT(dtlck->index == 0);
1264 lv = & dtlck->lv[0];
1266 /* linelock header */
1271 /* linelock stbl of non-root parent page */
1272 if (!(sp->header.flag & BT_ROOT)) {
1274 n = skip >> L2DTSLOTSIZE;
1275 lv->offset = sp->header.stblindex + n;
1277 ((sp->header.nextindex -
1278 1) >> L2DTSLOTSIZE) - n + 1;
1282 dtInsertEntry(sp, skip, &key, data, &dtlck);
1284 /* exit propagate up */
1289 /* unpin current split and its right page */
1294 * free remaining extents allocated for split
1298 pxd = &pxdlist.pxd[n];
1299 for (; n < pxdlist.maxnpxd; n++, pxd++)
1300 dbFree(ip, addressPXD(pxd), (s64) lengthPXD(pxd));
1305 /* Rollback quota allocation */
1306 if (rc && quota_allocation)
1307 dquot_free_block(ip, quota_allocation);
1318 * function: Split a non-root page of a btree.
1322 * return: 0 - success;
1324 * return split and new page pinned;
1326 static int dtSplitPage(tid_t tid, struct inode *ip, struct dtsplit * split,
1327 struct metapage ** rmpp, dtpage_t ** rpp, pxd_t * rpxdp)
1330 struct metapage *smp;
1332 struct metapage *rmp;
1333 dtpage_t *rp; /* new right page allocated */
1334 s64 rbn; /* new right page block number */
1335 struct metapage *mp;
1338 struct pxdlist *pxdlist;
1340 int skip, nextindex, half, left, nxt, off, si;
1341 struct ldtentry *ldtentry;
1342 struct idtentry *idtentry;
1347 struct dt_lock *sdtlck, *rdtlck;
1349 struct dt_lock *dtlck;
1350 struct lv *slv, *rlv, *lv;
1352 /* get split page */
1354 sp = DT_PAGE(ip, smp);
1357 * allocate the new right page for the split
1359 pxdlist = split->pxdlist;
1360 pxd = &pxdlist->pxd[pxdlist->npxd];
1362 rbn = addressPXD(pxd);
1363 rmp = get_metapage(ip, rbn, PSIZE, 1);
1367 /* Allocate blocks to quota. */
1368 rc = dquot_alloc_block(ip, lengthPXD(pxd));
1370 release_metapage(rmp);
1374 jfs_info("dtSplitPage: ip:0x%p smp:0x%p rmp:0x%p", ip, smp, rmp);
1376 BT_MARK_DIRTY(rmp, ip);
1378 * acquire a transaction lock on the new right page
1380 tlck = txLock(tid, ip, rmp, tlckDTREE | tlckNEW);
1381 rdtlck = (struct dt_lock *) & tlck->lock;
1383 rp = (dtpage_t *) rmp->data;
1385 rp->header.self = *pxd;
1387 BT_MARK_DIRTY(smp, ip);
1389 * acquire a transaction lock on the split page
1393 tlck = txLock(tid, ip, smp, tlckDTREE | tlckENTRY);
1394 sdtlck = (struct dt_lock *) & tlck->lock;
1396 /* linelock header of split page */
1397 ASSERT(sdtlck->index == 0);
1398 slv = & sdtlck->lv[0];
1404 * initialize/update sibling pointers between sp and rp
1406 nextbn = le64_to_cpu(sp->header.next);
1407 rp->header.next = cpu_to_le64(nextbn);
1408 rp->header.prev = cpu_to_le64(addressPXD(&sp->header.self));
1409 sp->header.next = cpu_to_le64(rbn);
1412 * initialize new right page
1414 rp->header.flag = sp->header.flag;
1416 /* compute sorted entry table at start of extent data area */
1417 rp->header.nextindex = 0;
1418 rp->header.stblindex = 1;
1420 n = PSIZE >> L2DTSLOTSIZE;
1421 rp->header.maxslot = n;
1422 stblsize = (n + 31) >> L2DTSLOTSIZE; /* in unit of slot */
1425 fsi = rp->header.stblindex + stblsize;
1426 rp->header.freelist = fsi;
1427 rp->header.freecnt = rp->header.maxslot - fsi;
1430 * sequential append at tail: append without split
1432 * If splitting the last page on a level because of appending
1433 * a entry to it (skip is maxentry), it's likely that the access is
1434 * sequential. Adding an empty page on the side of the level is less
1435 * work and can push the fill factor much higher than normal.
1436 * If we're wrong it's no big deal, we'll just do the split the right
1438 * (It may look like it's equally easy to do a similar hack for
1439 * reverse sorted data, that is, split the tree left,
1440 * but it's not. Be my guest.)
1442 if (nextbn == 0 && split->index == sp->header.nextindex) {
1443 /* linelock header + stbl (first slot) of new page */
1444 rlv = & rdtlck->lv[rdtlck->index];
1450 * initialize freelist of new right page
1453 for (fsi++; fsi < rp->header.maxslot; f++, fsi++)
1457 /* insert entry at the first entry of the new right page */
1458 dtInsertEntry(rp, 0, split->key, split->data, &rdtlck);
1464 * non-sequential insert (at possibly middle page)
1468 * update prev pointer of previous right sibling page;
1471 DT_GETPAGE(ip, nextbn, mp, PSIZE, p, rc);
1473 discard_metapage(rmp);
1477 BT_MARK_DIRTY(mp, ip);
1479 * acquire a transaction lock on the next page
1481 tlck = txLock(tid, ip, mp, tlckDTREE | tlckRELINK);
1482 jfs_info("dtSplitPage: tlck = 0x%p, ip = 0x%p, mp=0x%p",
1484 dtlck = (struct dt_lock *) & tlck->lock;
1486 /* linelock header of previous right sibling page */
1487 lv = & dtlck->lv[dtlck->index];
1492 p->header.prev = cpu_to_le64(rbn);
1498 * split the data between the split and right pages.
1500 skip = split->index;
1501 half = (PSIZE >> L2DTSLOTSIZE) >> 1; /* swag */
1505 * compute fill factor for split pages
1507 * <nxt> traces the next entry to move to rp
1508 * <off> traces the next entry to stay in sp
1510 stbl = (u8 *) & sp->slot[sp->header.stblindex];
1511 nextindex = sp->header.nextindex;
1512 for (nxt = off = 0; nxt < nextindex; ++off) {
1514 /* check for fill factor with new entry size */
1518 switch (sp->header.flag & BT_TYPE) {
1520 ldtentry = (struct ldtentry *) & sp->slot[si];
1522 n = NDTLEAF(ldtentry->namlen);
1524 n = NDTLEAF_LEGACY(ldtentry->
1529 idtentry = (struct idtentry *) & sp->slot[si];
1530 n = NDTINTERNAL(idtentry->namlen);
1537 ++nxt; /* advance to next entry to move in sp */
1545 /* <nxt> poins to the 1st entry to move */
1548 * move entries to right page
1550 * dtMoveEntry() initializes rp and reserves entry for insertion
1552 * split page moved out entries are linelocked;
1553 * new/right page moved in entries are linelocked;
1555 /* linelock header + stbl of new right page */
1556 rlv = & rdtlck->lv[rdtlck->index];
1561 dtMoveEntry(sp, nxt, rp, &sdtlck, &rdtlck, DO_INDEX(ip));
1563 sp->header.nextindex = nxt;
1566 * finalize freelist of new right page
1568 fsi = rp->header.freelist;
1570 for (fsi++; fsi < rp->header.maxslot; f++, fsi++)
1575 * Update directory index table for entries now in right page
1577 if ((rp->header.flag & BT_LEAF) && DO_INDEX(ip)) {
1581 stbl = DT_GETSTBL(rp);
1582 for (n = 0; n < rp->header.nextindex; n++) {
1583 ldtentry = (struct ldtentry *) & rp->slot[stbl[n]];
1584 modify_index(tid, ip, le32_to_cpu(ldtentry->index),
1585 rbn, n, &mp, &lblock);
1588 release_metapage(mp);
1592 * the skipped index was on the left page,
1595 /* insert the new entry in the split page */
1596 dtInsertEntry(sp, skip, split->key, split->data, &sdtlck);
1598 /* linelock stbl of split page */
1599 if (sdtlck->index >= sdtlck->maxcnt)
1600 sdtlck = (struct dt_lock *) txLinelock(sdtlck);
1601 slv = & sdtlck->lv[sdtlck->index];
1602 n = skip >> L2DTSLOTSIZE;
1603 slv->offset = sp->header.stblindex + n;
1605 ((sp->header.nextindex - 1) >> L2DTSLOTSIZE) - n + 1;
1609 * the skipped index was on the right page,
1612 /* adjust the skip index to reflect the new position */
1615 /* insert the new entry in the right page */
1616 dtInsertEntry(rp, skip, split->key, split->data, &rdtlck);
1630 * function: extend 1st/only directory leaf page
1634 * return: 0 - success;
1636 * return extended page pinned;
1638 static int dtExtendPage(tid_t tid,
1639 struct inode *ip, struct dtsplit * split, struct btstack * btstack)
1641 struct super_block *sb = ip->i_sb;
1643 struct metapage *smp, *pmp, *mp;
1645 struct pxdlist *pxdlist;
1648 int newstblindex, newstblsize;
1649 int oldstblindex, oldstblsize;
1652 struct btframe *parent;
1654 struct dt_lock *dtlck;
1657 struct pxd_lock *pxdlock;
1660 struct ldtentry *ldtentry;
1663 /* get page to extend */
1665 sp = DT_PAGE(ip, smp);
1667 /* get parent/root page */
1668 parent = BT_POP(btstack);
1669 DT_GETPAGE(ip, parent->bn, pmp, PSIZE, pp, rc);
1676 pxdlist = split->pxdlist;
1677 pxd = &pxdlist->pxd[pxdlist->npxd];
1680 xaddr = addressPXD(pxd);
1681 tpxd = &sp->header.self;
1682 txaddr = addressPXD(tpxd);
1683 /* in-place extension */
1684 if (xaddr == txaddr) {
1691 /* save moved extent descriptor for later free */
1692 tlck = txMaplock(tid, ip, tlckDTREE | tlckRELOCATE);
1693 pxdlock = (struct pxd_lock *) & tlck->lock;
1694 pxdlock->flag = mlckFREEPXD;
1695 pxdlock->pxd = sp->header.self;
1699 * Update directory index table to reflect new page address
1705 stbl = DT_GETSTBL(sp);
1706 for (n = 0; n < sp->header.nextindex; n++) {
1708 (struct ldtentry *) & sp->slot[stbl[n]];
1709 modify_index(tid, ip,
1710 le32_to_cpu(ldtentry->index),
1711 xaddr, n, &mp, &lblock);
1714 release_metapage(mp);
1721 sp->header.self = *pxd;
1723 jfs_info("dtExtendPage: ip:0x%p smp:0x%p sp:0x%p", ip, smp, sp);
1725 BT_MARK_DIRTY(smp, ip);
1727 * acquire a transaction lock on the extended/leaf page
1729 tlck = txLock(tid, ip, smp, tlckDTREE | type);
1730 dtlck = (struct dt_lock *) & tlck->lock;
1731 lv = & dtlck->lv[0];
1733 /* update buffer extent descriptor of extended page */
1734 xlen = lengthPXD(pxd);
1735 xsize = xlen << JFS_SBI(sb)->l2bsize;
1738 * copy old stbl to new stbl at start of extended area
1740 oldstblindex = sp->header.stblindex;
1741 oldstblsize = (sp->header.maxslot + 31) >> L2DTSLOTSIZE;
1742 newstblindex = sp->header.maxslot;
1743 n = xsize >> L2DTSLOTSIZE;
1744 newstblsize = (n + 31) >> L2DTSLOTSIZE;
1745 memcpy(&sp->slot[newstblindex], &sp->slot[oldstblindex],
1746 sp->header.nextindex);
1749 * in-line extension: linelock old area of extended page
1751 if (type == tlckEXTEND) {
1752 /* linelock header */
1758 /* linelock new stbl of extended page */
1759 lv->offset = newstblindex;
1760 lv->length = newstblsize;
1763 * relocation: linelock whole relocated area
1767 lv->length = sp->header.maxslot + newstblsize;
1772 sp->header.maxslot = n;
1773 sp->header.stblindex = newstblindex;
1774 /* sp->header.nextindex remains the same */
1777 * add old stbl region at head of freelist
1781 last = sp->header.freelist;
1782 for (n = 0; n < oldstblsize; n++, fsi++, f++) {
1786 sp->header.freelist = last;
1787 sp->header.freecnt += oldstblsize;
1790 * append free region of newly extended area at tail of freelist
1792 /* init free region of newly extended area */
1793 fsi = n = newstblindex + newstblsize;
1795 for (fsi++; fsi < sp->header.maxslot; f++, fsi++)
1799 /* append new free region at tail of old freelist */
1800 fsi = sp->header.freelist;
1802 sp->header.freelist = n;
1807 } while (fsi != -1);
1812 sp->header.freecnt += sp->header.maxslot - n;
1815 * insert the new entry
1817 dtInsertEntry(sp, split->index, split->key, split->data, &dtlck);
1819 BT_MARK_DIRTY(pmp, ip);
1821 * linelock any freeslots residing in old extent
1823 if (type == tlckEXTEND) {
1824 n = sp->header.maxslot >> 2;
1825 if (sp->header.freelist < n)
1826 dtLinelockFreelist(sp, n, &dtlck);
1830 * update parent entry on the parent/root page
1833 * acquire a transaction lock on the parent/root page
1835 tlck = txLock(tid, ip, pmp, tlckDTREE | tlckENTRY);
1836 dtlck = (struct dt_lock *) & tlck->lock;
1837 lv = & dtlck->lv[dtlck->index];
1839 /* linelock parent entry - 1st slot */
1844 /* update the parent pxd for page extension */
1845 tpxd = (pxd_t *) & pp->slot[1];
1857 * split the full root page into
1858 * original/root/split page and new right page
1859 * i.e., root remains fixed in tree anchor (inode) and
1860 * the root is copied to a single new right child page
1861 * since root page << non-root page, and
1862 * the split root page contains a single entry for the
1863 * new right child page.
1867 * return: 0 - success;
1869 * return new page pinned;
1871 static int dtSplitRoot(tid_t tid,
1872 struct inode *ip, struct dtsplit * split, struct metapage ** rmpp)
1874 struct super_block *sb = ip->i_sb;
1875 struct metapage *smp;
1877 struct metapage *rmp;
1884 int fsi, stblsize, n;
1887 struct pxdlist *pxdlist;
1889 struct dt_lock *dtlck;
1894 /* get split root page */
1896 sp = &JFS_IP(ip)->i_dtroot;
1899 * allocate/initialize a single (right) child page
1901 * N.B. at first split, a one (or two) block to fit new entry
1902 * is allocated; at subsequent split, a full page is allocated;
1904 pxdlist = split->pxdlist;
1905 pxd = &pxdlist->pxd[pxdlist->npxd];
1907 rbn = addressPXD(pxd);
1908 xlen = lengthPXD(pxd);
1909 xsize = xlen << JFS_SBI(sb)->l2bsize;
1910 rmp = get_metapage(ip, rbn, xsize, 1);
1916 /* Allocate blocks to quota. */
1917 rc = dquot_alloc_block(ip, lengthPXD(pxd));
1919 release_metapage(rmp);
1923 BT_MARK_DIRTY(rmp, ip);
1925 * acquire a transaction lock on the new right page
1927 tlck = txLock(tid, ip, rmp, tlckDTREE | tlckNEW);
1928 dtlck = (struct dt_lock *) & tlck->lock;
1931 (sp->header.flag & BT_LEAF) ? BT_LEAF : BT_INTERNAL;
1932 rp->header.self = *pxd;
1934 /* initialize sibling pointers */
1935 rp->header.next = 0;
1936 rp->header.prev = 0;
1939 * move in-line root page into new right page extent
1941 /* linelock header + copied entries + new stbl (1st slot) in new page */
1942 ASSERT(dtlck->index == 0);
1943 lv = & dtlck->lv[0];
1945 lv->length = 10; /* 1 + 8 + 1 */
1948 n = xsize >> L2DTSLOTSIZE;
1949 rp->header.maxslot = n;
1950 stblsize = (n + 31) >> L2DTSLOTSIZE;
1952 /* copy old stbl to new stbl at start of extended area */
1953 rp->header.stblindex = DTROOTMAXSLOT;
1954 stbl = (s8 *) & rp->slot[DTROOTMAXSLOT];
1955 memcpy(stbl, sp->header.stbl, sp->header.nextindex);
1956 rp->header.nextindex = sp->header.nextindex;
1958 /* copy old data area to start of new data area */
1959 memcpy(&rp->slot[1], &sp->slot[1], IDATASIZE);
1962 * append free region of newly extended area at tail of freelist
1964 /* init free region of newly extended area */
1965 fsi = n = DTROOTMAXSLOT + stblsize;
1967 for (fsi++; fsi < rp->header.maxslot; f++, fsi++)
1971 /* append new free region at tail of old freelist */
1972 fsi = sp->header.freelist;
1974 rp->header.freelist = n;
1976 rp->header.freelist = fsi;
1986 rp->header.freecnt = sp->header.freecnt + rp->header.maxslot - n;
1989 * Update directory index table for entries now in right page
1991 if ((rp->header.flag & BT_LEAF) && DO_INDEX(ip)) {
1993 struct metapage *mp = NULL;
1994 struct ldtentry *ldtentry;
1996 stbl = DT_GETSTBL(rp);
1997 for (n = 0; n < rp->header.nextindex; n++) {
1998 ldtentry = (struct ldtentry *) & rp->slot[stbl[n]];
1999 modify_index(tid, ip, le32_to_cpu(ldtentry->index),
2000 rbn, n, &mp, &lblock);
2003 release_metapage(mp);
2006 * insert the new entry into the new right/child page
2007 * (skip index in the new right page will not change)
2009 dtInsertEntry(rp, split->index, split->key, split->data, &dtlck);
2012 * reset parent/root page
2014 * set the 1st entry offset to 0, which force the left-most key
2015 * at any level of the tree to be less than any search key.
2017 * The btree comparison code guarantees that the left-most key on any
2018 * level of the tree is never used, so it doesn't need to be filled in.
2020 BT_MARK_DIRTY(smp, ip);
2022 * acquire a transaction lock on the root page (in-memory inode)
2024 tlck = txLock(tid, ip, smp, tlckDTREE | tlckNEW | tlckBTROOT);
2025 dtlck = (struct dt_lock *) & tlck->lock;
2028 ASSERT(dtlck->index == 0);
2029 lv = & dtlck->lv[0];
2031 lv->length = DTROOTMAXSLOT;
2034 /* update page header of root */
2035 if (sp->header.flag & BT_LEAF) {
2036 sp->header.flag &= ~BT_LEAF;
2037 sp->header.flag |= BT_INTERNAL;
2040 /* init the first entry */
2041 s = (struct idtentry *) & sp->slot[DTENTRYSTART];
2047 stbl = sp->header.stbl;
2048 stbl[0] = DTENTRYSTART;
2049 sp->header.nextindex = 1;
2052 fsi = DTENTRYSTART + 1;
2055 /* init free region of remaining area */
2056 for (fsi++; fsi < DTROOTMAXSLOT; f++, fsi++)
2060 sp->header.freelist = DTENTRYSTART + 1;
2061 sp->header.freecnt = DTROOTMAXSLOT - (DTENTRYSTART + 1);
2072 * function: delete the entry(s) referenced by a key.
2078 int dtDelete(tid_t tid,
2079 struct inode *ip, struct component_name * key, ino_t * ino, int flag)
2083 struct metapage *mp, *imp;
2086 struct btstack btstack;
2087 struct dt_lock *dtlck;
2091 struct ldtentry *ldtentry;
2093 u32 table_index, next_index;
2094 struct metapage *nmp;
2098 * search for the entry to delete:
2100 * dtSearch() returns (leaf page pinned, index at which to delete).
2102 if ((rc = dtSearch(ip, key, ino, &btstack, flag)))
2105 /* retrieve search result */
2106 DT_GETSEARCH(ip, btstack.top, bn, mp, p, index);
2109 * We need to find put the index of the next entry into the
2110 * directory index table in order to resume a readdir from this
2114 stbl = DT_GETSTBL(p);
2115 ldtentry = (struct ldtentry *) & p->slot[stbl[index]];
2116 table_index = le32_to_cpu(ldtentry->index);
2117 if (index == (p->header.nextindex - 1)) {
2119 * Last entry in this leaf page
2121 if ((p->header.flag & BT_ROOT)
2122 || (p->header.next == 0))
2125 /* Read next leaf page */
2126 DT_GETPAGE(ip, le64_to_cpu(p->header.next),
2127 nmp, PSIZE, np, rc);
2131 stbl = DT_GETSTBL(np);
2133 (struct ldtentry *) & np->
2136 le32_to_cpu(ldtentry->index);
2142 (struct ldtentry *) & p->slot[stbl[index + 1]];
2143 next_index = le32_to_cpu(ldtentry->index);
2145 free_index(tid, ip, table_index, next_index);
2148 * the leaf page becomes empty, delete the page
2150 if (p->header.nextindex == 1) {
2151 /* delete empty page */
2152 rc = dtDeleteUp(tid, ip, mp, p, &btstack);
2155 * the leaf page has other entries remaining:
2157 * delete the entry from the leaf page.
2160 BT_MARK_DIRTY(mp, ip);
2162 * acquire a transaction lock on the leaf page
2164 tlck = txLock(tid, ip, mp, tlckDTREE | tlckENTRY);
2165 dtlck = (struct dt_lock *) & tlck->lock;
2168 * Do not assume that dtlck->index will be zero. During a
2169 * rename within a directory, this transaction may have
2170 * modified this page already when adding the new entry.
2173 /* linelock header */
2174 if (dtlck->index >= dtlck->maxcnt)
2175 dtlck = (struct dt_lock *) txLinelock(dtlck);
2176 lv = & dtlck->lv[dtlck->index];
2181 /* linelock stbl of non-root leaf page */
2182 if (!(p->header.flag & BT_ROOT)) {
2183 if (dtlck->index >= dtlck->maxcnt)
2184 dtlck = (struct dt_lock *) txLinelock(dtlck);
2185 lv = & dtlck->lv[dtlck->index];
2186 i = index >> L2DTSLOTSIZE;
2187 lv->offset = p->header.stblindex + i;
2189 ((p->header.nextindex - 1) >> L2DTSLOTSIZE) -
2194 /* free the leaf entry */
2195 dtDeleteEntry(p, index, &dtlck);
2198 * Update directory index table for entries moved in stbl
2200 if (DO_INDEX(ip) && index < p->header.nextindex) {
2204 stbl = DT_GETSTBL(p);
2205 for (i = index; i < p->header.nextindex; i++) {
2207 (struct ldtentry *) & p->slot[stbl[i]];
2208 modify_index(tid, ip,
2209 le32_to_cpu(ldtentry->index),
2210 bn, i, &imp, &lblock);
2213 release_metapage(imp);
2227 * free empty pages as propagating deletion up the tree
2233 static int dtDeleteUp(tid_t tid, struct inode *ip,
2234 struct metapage * fmp, dtpage_t * fp, struct btstack * btstack)
2237 struct metapage *mp;
2239 int index, nextindex;
2241 struct btframe *parent;
2242 struct dt_lock *dtlck;
2245 struct pxd_lock *pxdlock;
2249 * keep the root leaf page which has become empty
2251 if (BT_IS_ROOT(fmp)) {
2255 * dtInitRoot() acquires txlock on the root
2257 dtInitRoot(tid, ip, PARENT(ip));
2265 * free the non-root leaf page
2268 * acquire a transaction lock on the page
2270 * write FREEXTENT|NOREDOPAGE log record
2271 * N.B. linelock is overlaid as freed extent descriptor, and
2272 * the buffer page is freed;
2274 tlck = txMaplock(tid, ip, tlckDTREE | tlckFREE);
2275 pxdlock = (struct pxd_lock *) & tlck->lock;
2276 pxdlock->flag = mlckFREEPXD;
2277 pxdlock->pxd = fp->header.self;
2280 /* update sibling pointers */
2281 if ((rc = dtRelink(tid, ip, fp))) {
2286 xlen = lengthPXD(&fp->header.self);
2288 /* Free quota allocation. */
2289 dquot_free_block(ip, xlen);
2291 /* free/invalidate its buffer page */
2292 discard_metapage(fmp);
2295 * propagate page deletion up the directory tree
2297 * If the delete from the parent page makes it empty,
2298 * continue all the way up the tree.
2299 * stop if the root page is reached (which is never deleted) or
2300 * if the entry deletion does not empty the page.
2302 while ((parent = BT_POP(btstack)) != NULL) {
2303 /* pin the parent page <sp> */
2304 DT_GETPAGE(ip, parent->bn, mp, PSIZE, p, rc);
2309 * free the extent of the child page deleted
2311 index = parent->index;
2314 * delete the entry for the child page from parent
2316 nextindex = p->header.nextindex;
2319 * the parent has the single entry being deleted:
2321 * free the parent page which has become empty.
2323 if (nextindex == 1) {
2325 * keep the root internal page which has become empty
2327 if (p->header.flag & BT_ROOT) {
2331 * dtInitRoot() acquires txlock on the root
2333 dtInitRoot(tid, ip, PARENT(ip));
2340 * free the parent page
2344 * acquire a transaction lock on the page
2346 * write FREEXTENT|NOREDOPAGE log record
2350 tlckDTREE | tlckFREE);
2351 pxdlock = (struct pxd_lock *) & tlck->lock;
2352 pxdlock->flag = mlckFREEPXD;
2353 pxdlock->pxd = p->header.self;
2356 /* update sibling pointers */
2357 if ((rc = dtRelink(tid, ip, p))) {
2362 xlen = lengthPXD(&p->header.self);
2364 /* Free quota allocation */
2365 dquot_free_block(ip, xlen);
2367 /* free/invalidate its buffer page */
2368 discard_metapage(mp);
2376 * the parent has other entries remaining:
2378 * delete the router entry from the parent page.
2380 BT_MARK_DIRTY(mp, ip);
2382 * acquire a transaction lock on the page
2384 * action: router entry deletion
2386 tlck = txLock(tid, ip, mp, tlckDTREE | tlckENTRY);
2387 dtlck = (struct dt_lock *) & tlck->lock;
2389 /* linelock header */
2390 if (dtlck->index >= dtlck->maxcnt)
2391 dtlck = (struct dt_lock *) txLinelock(dtlck);
2392 lv = & dtlck->lv[dtlck->index];
2397 /* linelock stbl of non-root leaf page */
2398 if (!(p->header.flag & BT_ROOT)) {
2399 if (dtlck->index < dtlck->maxcnt)
2402 dtlck = (struct dt_lock *) txLinelock(dtlck);
2403 lv = & dtlck->lv[0];
2405 i = index >> L2DTSLOTSIZE;
2406 lv->offset = p->header.stblindex + i;
2408 ((p->header.nextindex - 1) >> L2DTSLOTSIZE) -
2413 /* free the router entry */
2414 dtDeleteEntry(p, index, &dtlck);
2416 /* reset key of new leftmost entry of level (for consistency) */
2418 ((p->header.flag & BT_ROOT) || p->header.prev == 0))
2419 dtTruncateEntry(p, 0, &dtlck);
2421 /* unpin the parent page */
2424 /* exit propagation up */
2429 ip->i_size -= PSIZE;
2438 * link around a freed page.
2441 * fp: page to be freed
2445 static int dtRelink(tid_t tid, struct inode *ip, dtpage_t * p)
2448 struct metapage *mp;
2451 struct dt_lock *dtlck;
2454 nextbn = le64_to_cpu(p->header.next);
2455 prevbn = le64_to_cpu(p->header.prev);
2457 /* update prev pointer of the next page */
2459 DT_GETPAGE(ip, nextbn, mp, PSIZE, p, rc);
2463 BT_MARK_DIRTY(mp, ip);
2465 * acquire a transaction lock on the next page
2467 * action: update prev pointer;
2469 tlck = txLock(tid, ip, mp, tlckDTREE | tlckRELINK);
2470 jfs_info("dtRelink nextbn: tlck = 0x%p, ip = 0x%p, mp=0x%p",
2472 dtlck = (struct dt_lock *) & tlck->lock;
2474 /* linelock header */
2475 if (dtlck->index >= dtlck->maxcnt)
2476 dtlck = (struct dt_lock *) txLinelock(dtlck);
2477 lv = & dtlck->lv[dtlck->index];
2482 p->header.prev = cpu_to_le64(prevbn);
2486 /* update next pointer of the previous page */
2488 DT_GETPAGE(ip, prevbn, mp, PSIZE, p, rc);
2492 BT_MARK_DIRTY(mp, ip);
2494 * acquire a transaction lock on the prev page
2496 * action: update next pointer;
2498 tlck = txLock(tid, ip, mp, tlckDTREE | tlckRELINK);
2499 jfs_info("dtRelink prevbn: tlck = 0x%p, ip = 0x%p, mp=0x%p",
2501 dtlck = (struct dt_lock *) & tlck->lock;
2503 /* linelock header */
2504 if (dtlck->index >= dtlck->maxcnt)
2505 dtlck = (struct dt_lock *) txLinelock(dtlck);
2506 lv = & dtlck->lv[dtlck->index];
2511 p->header.next = cpu_to_le64(nextbn);
2522 * initialize directory root (inline in inode)
2524 void dtInitRoot(tid_t tid, struct inode *ip, u32 idotdot)
2526 struct jfs_inode_info *jfs_ip = JFS_IP(ip);
2531 struct dt_lock *dtlck;
2536 * If this was previously an non-empty directory, we need to remove
2537 * the old directory table.
2540 if (!jfs_dirtable_inline(ip)) {
2541 struct tblock *tblk = tid_to_tblock(tid);
2543 * We're playing games with the tid's xflag. If
2544 * we're removing a regular file, the file's xtree
2545 * is committed with COMMIT_PMAP, but we always
2546 * commit the directories xtree with COMMIT_PWMAP.
2548 xflag_save = tblk->xflag;
2551 * xtTruncate isn't guaranteed to fully truncate
2552 * the xtree. The caller needs to check i_size
2553 * after committing the transaction to see if
2554 * additional truncation is needed. The
2555 * COMMIT_Stale flag tells caller that we
2556 * initiated the truncation.
2558 xtTruncate(tid, ip, 0, COMMIT_PWMAP);
2559 set_cflag(COMMIT_Stale, ip);
2561 tblk->xflag = xflag_save;
2565 jfs_ip->next_index = 2;
2567 ip->i_size = IDATASIZE;
2570 * acquire a transaction lock on the root
2572 * action: directory initialization;
2574 tlck = txLock(tid, ip, (struct metapage *) & jfs_ip->bxflag,
2575 tlckDTREE | tlckENTRY | tlckBTROOT);
2576 dtlck = (struct dt_lock *) & tlck->lock;
2579 ASSERT(dtlck->index == 0);
2580 lv = & dtlck->lv[0];
2582 lv->length = DTROOTMAXSLOT;
2585 p = &jfs_ip->i_dtroot;
2587 p->header.flag = DXD_INDEX | BT_ROOT | BT_LEAF;
2589 p->header.nextindex = 0;
2595 /* init data area of root */
2596 for (fsi++; fsi < DTROOTMAXSLOT; f++, fsi++)
2600 p->header.freelist = 1;
2601 p->header.freecnt = 8;
2603 /* init '..' entry */
2604 p->header.idotdot = cpu_to_le32(idotdot);
2610 * add_missing_indices()
2612 * function: Fix dtree page in which one or more entries has an invalid index.
2613 * fsck.jfs should really fix this, but it currently does not.
2614 * Called from jfs_readdir when bad index is detected.
2616 static int add_missing_indices(struct inode *inode, s64 bn)
2619 struct dt_lock *dtlck;
2623 struct metapage *mp;
2630 tid = txBegin(inode->i_sb, 0);
2632 DT_GETPAGE(inode, bn, mp, PSIZE, p, rc);
2635 printk(KERN_ERR "DT_GETPAGE failed!\n");
2638 BT_MARK_DIRTY(mp, inode);
2640 ASSERT(p->header.flag & BT_LEAF);
2642 tlck = txLock(tid, inode, mp, tlckDTREE | tlckENTRY);
2644 tlck->type |= tlckBTROOT;
2646 dtlck = (struct dt_lock *) &tlck->lock;
2648 stbl = DT_GETSTBL(p);
2649 for (i = 0; i < p->header.nextindex; i++) {
2651 jfs_err("jfs: add_missing_indices: Invalid stbl[%d] = %d for inode %ld, block = %lld",
2652 i, stbl[i], (long)inode->i_ino, (long long)bn);
2660 d = (struct ldtentry *) &p->slot[stbl[i]];
2661 index = le32_to_cpu(d->index);
2662 if ((index < 2) || (index >= JFS_IP(inode)->next_index)) {
2663 d->index = cpu_to_le32(add_index(tid, inode, bn, i));
2664 if (dtlck->index >= dtlck->maxcnt)
2665 dtlck = (struct dt_lock *) txLinelock(dtlck);
2666 lv = &dtlck->lv[dtlck->index];
2667 lv->offset = stbl[i];
2674 (void) txCommit(tid, 1, &inode, 0);
2681 * Buffer to hold directory entry info while traversing a dtree page
2682 * before being fed to the filldir function
2692 * function to determine next variable-sized jfs_dirent in buffer
2694 static inline struct jfs_dirent *next_jfs_dirent(struct jfs_dirent *dirent)
2696 return (struct jfs_dirent *)
2698 ((sizeof (struct jfs_dirent) + dirent->name_len + 1 +
2699 sizeof (loff_t) - 1) &
2700 ~(sizeof (loff_t) - 1)));
2706 * function: read directory entries sequentially
2707 * from the specified entry offset
2711 * return: offset = (pn, index) of start entry
2712 * of next jfs_readdir()/dtRead()
2714 int jfs_readdir(struct file *file, struct dir_context *ctx)
2716 struct inode *ip = file_inode(file);
2717 struct nls_table *codepage = JFS_SBI(ip->i_sb)->nls_tab;
2719 loff_t dtpos; /* legacy OS/2 style position */
2724 } *dtoffset = (struct dtoffset *) &dtpos;
2726 struct metapage *mp;
2730 struct btstack btstack;
2734 int d_namleft, len, outlen;
2735 unsigned long dirent_buf;
2739 uint loop_count = 0;
2740 struct jfs_dirent *jfs_dirent;
2742 int overflow, fix_page, page_fixed = 0;
2743 static int unique_pos = 2; /* If we can't fix broken index */
2745 if (ctx->pos == DIREND)
2750 * persistent index is stored in directory entries.
2751 * Special cases: 0 = .
2753 * -1 = End of directory
2757 dir_index = (u32) ctx->pos;
2760 * NFSv4 reserves cookies 1 and 2 for . and .. so the value
2761 * we return to the vfs is one greater than the one we use
2767 if (dir_index > 1) {
2768 struct dir_table_slot dirtab_slot;
2771 (dir_index >= JFS_IP(ip)->next_index)) {
2772 /* Stale position. Directory has shrunk */
2777 rc = read_index(ip, dir_index, &dirtab_slot);
2782 if (dirtab_slot.flag == DIR_INDEX_FREE) {
2783 if (loop_count++ > JFS_IP(ip)->next_index) {
2784 jfs_err("jfs_readdir detected infinite loop!");
2788 dir_index = le32_to_cpu(dirtab_slot.addr2);
2789 if (dir_index == -1) {
2795 bn = addressDTS(&dirtab_slot);
2796 index = dirtab_slot.slot;
2797 DT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
2802 if (p->header.flag & BT_INTERNAL) {
2803 jfs_err("jfs_readdir: bad index table");
2809 if (dir_index == 0) {
2814 if (!dir_emit(ctx, ".", 1, ip->i_ino, DT_DIR))
2821 if (!dir_emit(ctx, "..", 2, PARENT(ip), DT_DIR))
2825 * Find first entry of left-most leaf
2832 if ((rc = dtReadFirst(ip, &btstack)))
2835 DT_GETSEARCH(ip, btstack.top, bn, mp, p, index);
2839 * Legacy filesystem - OS/2 & Linux JFS < 0.3.6
2841 * pn = 0; index = 1: First entry "."
2842 * pn = 0; index = 2: Second entry ".."
2843 * pn > 0: Real entries, pn=1 -> leftmost page
2844 * pn = index = -1: No more entries
2848 /* build "." entry */
2850 if (!dir_emit(ctx, ".", 1, ip->i_ino, DT_DIR))
2852 dtoffset->index = 2;
2856 if (dtoffset->pn == 0) {
2857 if (dtoffset->index == 2) {
2858 /* build ".." entry */
2859 if (!dir_emit(ctx, "..", 2, PARENT(ip), DT_DIR))
2862 jfs_err("jfs_readdir called with invalid offset!");
2865 dtoffset->index = 0;
2874 if ((rc = dtReadNext(ip, &ctx->pos, &btstack))) {
2875 jfs_err("jfs_readdir: unexpected rc = %d from dtReadNext",
2880 /* get start leaf page and index */
2881 DT_GETSEARCH(ip, btstack.top, bn, mp, p, index);
2883 /* offset beyond directory eof ? */
2890 dirent_buf = __get_free_page(GFP_KERNEL);
2891 if (dirent_buf == 0) {
2893 jfs_warn("jfs_readdir: __get_free_page failed!");
2899 jfs_dirent = (struct jfs_dirent *) dirent_buf;
2901 overflow = fix_page = 0;
2903 stbl = DT_GETSTBL(p);
2905 for (i = index; i < p->header.nextindex; i++) {
2906 if (stbl[i] < 0 || stbl[i] > 127) {
2907 jfs_err("JFS: Invalid stbl[%d] = %d for inode %ld, block = %lld",
2908 i, stbl[i], (long)ip->i_ino, (long long)bn);
2909 free_page(dirent_buf);
2914 d = (struct ldtentry *) & p->slot[stbl[i]];
2916 if (((long) jfs_dirent + d->namlen + 1) >
2917 (dirent_buf + PAGE_SIZE)) {
2918 /* DBCS codepages could overrun dirent_buf */
2924 d_namleft = d->namlen;
2925 name_ptr = jfs_dirent->name;
2926 jfs_dirent->ino = le32_to_cpu(d->inumber);
2929 len = min(d_namleft, DTLHDRDATALEN);
2930 jfs_dirent->position = le32_to_cpu(d->index);
2932 * d->index should always be valid, but it
2933 * isn't. fsck.jfs doesn't create the
2934 * directory index for the lost+found
2935 * directory. Rather than let it go,
2936 * we can try to fix it.
2938 if ((jfs_dirent->position < 2) ||
2939 (jfs_dirent->position >=
2940 JFS_IP(ip)->next_index)) {
2941 if (!page_fixed && !isReadOnly(ip)) {
2944 * setting overflow and setting
2945 * index to i will cause the
2946 * same page to be processed
2947 * again starting here
2953 jfs_dirent->position = unique_pos++;
2956 * We add 1 to the index because we may
2957 * use a value of 2 internally, and NFSv4
2958 * doesn't like that.
2960 jfs_dirent->position++;
2962 jfs_dirent->position = dtpos;
2963 len = min(d_namleft, DTLHDRDATALEN_LEGACY);
2966 /* copy the name of head/only segment */
2967 outlen = jfs_strfromUCS_le(name_ptr, d->name, len,
2969 jfs_dirent->name_len = outlen;
2971 /* copy name in the additional segment(s) */
2974 t = (struct dtslot *) & p->slot[next];
2978 if (d_namleft == 0) {
2980 "JFS:Dtree error: ino = %ld, bn=%lld, index = %d\n",
2986 len = min(d_namleft, DTSLOTDATALEN);
2987 outlen = jfs_strfromUCS_le(name_ptr, t->name,
2989 jfs_dirent->name_len += outlen;
2995 jfs_dirent = next_jfs_dirent(jfs_dirent);
3002 /* Point to next leaf page */
3003 if (p->header.flag & BT_ROOT)
3006 bn = le64_to_cpu(p->header.next);
3008 /* update offset (pn:index) for new page */
3011 dtoffset->index = 0;
3017 /* unpin previous leaf page */
3020 jfs_dirent = (struct jfs_dirent *) dirent_buf;
3021 while (jfs_dirents--) {
3022 ctx->pos = jfs_dirent->position;
3023 if (!dir_emit(ctx, jfs_dirent->name,
3024 jfs_dirent->name_len,
3025 jfs_dirent->ino, DT_UNKNOWN))
3027 jfs_dirent = next_jfs_dirent(jfs_dirent);
3031 if ((rc = add_missing_indices(ip, bn)))
3036 if (!overflow && (bn == 0)) {
3041 DT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
3043 free_page(dirent_buf);
3049 free_page(dirent_buf);
3058 * function: get the leftmost page of the directory
3060 static int dtReadFirst(struct inode *ip, struct btstack * btstack)
3064 int psize = 288; /* initial in-line directory */
3065 struct metapage *mp;
3068 struct btframe *btsp;
3071 BT_CLR(btstack); /* reset stack */
3074 * descend leftmost path of the tree
3076 * by convention, root bn = 0.
3079 DT_GETPAGE(ip, bn, mp, psize, p, rc);
3084 * leftmost leaf page
3086 if (p->header.flag & BT_LEAF) {
3087 /* return leftmost entry */
3088 btsp = btstack->top;
3097 * descend down to leftmost child page
3099 if (BT_STACK_FULL(btstack)) {
3101 jfs_error(ip->i_sb, "btstack overrun\n");
3102 BT_STACK_DUMP(btstack);
3105 /* push (bn, index) of the parent page/entry */
3106 BT_PUSH(btstack, bn, 0);
3108 /* get the leftmost entry */
3109 stbl = DT_GETSTBL(p);
3111 if (stbl[0] < 0 || stbl[0] > 127) {
3113 jfs_error(ip->i_sb, "stbl[0] out of bound\n");
3117 xd = (pxd_t *) & p->slot[stbl[0]];
3119 /* get the child page block address */
3120 bn = addressPXD(xd);
3121 psize = lengthPXD(xd) << JFS_SBI(ip->i_sb)->l2bsize;
3123 /* unpin the parent page */
3132 * function: get the page of the specified offset (pn:index)
3134 * return: if (offset > eof), bn = -1;
3136 * note: if index > nextindex of the target leaf page,
3137 * start with 1st entry of next leaf page;
3139 static int dtReadNext(struct inode *ip, loff_t * offset,
3140 struct btstack * btstack)
3147 } *dtoffset = (struct dtoffset *) offset;
3149 struct metapage *mp;
3154 struct btframe *btsp, *parent;
3158 * get leftmost leaf page pinned
3160 if ((rc = dtReadFirst(ip, btstack)))
3164 DT_GETSEARCH(ip, btstack->top, bn, mp, p, index);
3166 /* get the start offset (pn:index) */
3167 pn = dtoffset->pn - 1; /* Now pn = 0 represents leftmost leaf */
3168 index = dtoffset->index;
3170 /* start at leftmost page ? */
3172 /* offset beyond eof ? */
3173 if (index < p->header.nextindex)
3176 if (p->header.flag & BT_ROOT) {
3181 /* start with 1st entry of next leaf page */
3183 dtoffset->index = index = 0;
3187 /* start at non-leftmost page: scan parent pages for large pn */
3188 if (p->header.flag & BT_ROOT) {
3193 /* start after next leaf page ? */
3197 /* get leaf page pn = 1 */
3199 bn = le64_to_cpu(p->header.next);
3201 /* unpin leaf page */
3204 /* offset beyond eof ? */
3213 * scan last internal page level to get target leaf page
3216 /* unpin leftmost leaf page */
3219 /* get left most parent page */
3220 btsp = btstack->top;
3223 DT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
3227 /* scan parent pages at last internal page level */
3228 while (pn >= p->header.nextindex) {
3229 pn -= p->header.nextindex;
3231 /* get next parent page address */
3232 bn = le64_to_cpu(p->header.next);
3234 /* unpin current parent page */
3237 /* offset beyond eof ? */
3243 /* get next parent page */
3244 DT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
3248 /* update parent page stack frame */
3252 /* get leaf page address */
3253 stbl = DT_GETSTBL(p);
3254 xd = (pxd_t *) & p->slot[stbl[pn]];
3255 bn = addressPXD(xd);
3257 /* unpin parent page */
3261 * get target leaf page
3264 DT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
3269 * leaf page has been completed:
3270 * start with 1st entry of next leaf page
3272 if (index >= p->header.nextindex) {
3273 bn = le64_to_cpu(p->header.next);
3275 /* unpin leaf page */
3278 /* offset beyond eof ? */
3284 /* get next leaf page */
3285 DT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
3289 /* start with 1st entry of next leaf page */
3291 dtoffset->index = 0;
3295 /* return target leaf page pinned */
3296 btsp = btstack->top;
3298 btsp->index = dtoffset->index;
3308 * function: compare search key with an internal entry
3311 * < 0 if k is < record
3312 * = 0 if k is = record
3313 * > 0 if k is > record
3315 static int dtCompare(struct component_name * key, /* search key */
3316 dtpage_t * p, /* directory page */
3318 { /* entry slot index */
3321 int klen, namlen, len, rc;
3322 struct idtentry *ih;
3326 * force the left-most key on internal pages, at any level of
3327 * the tree, to be less than any search key.
3328 * this obviates having to update the leftmost key on an internal
3329 * page when the user inserts a new key in the tree smaller than
3330 * anything that has been stored.
3332 * (? if/when dtSearch() narrows down to 1st entry (index = 0),
3333 * at any internal page at any level of the tree,
3334 * it descends to child of the entry anyway -
3335 * ? make the entry as min size dummy entry)
3337 * if (e->index == 0 && h->prevpg == P_INVALID && !(h->flags & BT_LEAF))
3344 ih = (struct idtentry *) & p->slot[si];
3347 namlen = ih->namlen;
3348 len = min(namlen, DTIHDRDATALEN);
3350 /* compare with head/only segment */
3351 len = min(klen, len);
3352 if ((rc = UniStrncmp_le(kname, name, len)))
3358 /* compare with additional segment(s) */
3360 while (klen > 0 && namlen > 0) {
3361 /* compare with next name segment */
3362 t = (struct dtslot *) & p->slot[si];
3363 len = min(namlen, DTSLOTDATALEN);
3364 len = min(klen, len);
3366 if ((rc = UniStrncmp_le(kname, name, len)))
3375 return (klen - namlen);
3384 * function: compare search key with an (leaf/internal) entry
3387 * < 0 if k is < record
3388 * = 0 if k is = record
3389 * > 0 if k is > record
3391 static int ciCompare(struct component_name * key, /* search key */
3392 dtpage_t * p, /* directory page */
3393 int si, /* entry slot index */
3398 int klen, namlen, len, rc;
3399 struct ldtentry *lh;
3400 struct idtentry *ih;
3405 * force the left-most key on internal pages, at any level of
3406 * the tree, to be less than any search key.
3407 * this obviates having to update the leftmost key on an internal
3408 * page when the user inserts a new key in the tree smaller than
3409 * anything that has been stored.
3411 * (? if/when dtSearch() narrows down to 1st entry (index = 0),
3412 * at any internal page at any level of the tree,
3413 * it descends to child of the entry anyway -
3414 * ? make the entry as min size dummy entry)
3416 * if (e->index == 0 && h->prevpg == P_INVALID && !(h->flags & BT_LEAF))
3426 if (p->header.flag & BT_LEAF) {
3427 lh = (struct ldtentry *) & p->slot[si];
3430 namlen = lh->namlen;
3431 if (flag & JFS_DIR_INDEX)
3432 len = min(namlen, DTLHDRDATALEN);
3434 len = min(namlen, DTLHDRDATALEN_LEGACY);
3437 * internal page entry
3440 ih = (struct idtentry *) & p->slot[si];
3443 namlen = ih->namlen;
3444 len = min(namlen, DTIHDRDATALEN);
3447 /* compare with head/only segment */
3448 len = min(klen, len);
3449 for (i = 0; i < len; i++, kname++, name++) {
3450 /* only uppercase if case-insensitive support is on */
3451 if ((flag & JFS_OS2) == JFS_OS2)
3452 x = UniToupper(le16_to_cpu(*name));
3454 x = le16_to_cpu(*name);
3455 if ((rc = *kname - x))
3462 /* compare with additional segment(s) */
3463 while (klen > 0 && namlen > 0) {
3464 /* compare with next name segment */
3465 t = (struct dtslot *) & p->slot[si];
3466 len = min(namlen, DTSLOTDATALEN);
3467 len = min(klen, len);
3469 for (i = 0; i < len; i++, kname++, name++) {
3470 /* only uppercase if case-insensitive support is on */
3471 if ((flag & JFS_OS2) == JFS_OS2)
3472 x = UniToupper(le16_to_cpu(*name));
3474 x = le16_to_cpu(*name);
3476 if ((rc = *kname - x))
3485 return (klen - namlen);
3490 * ciGetLeafPrefixKey()
3492 * function: compute prefix of suffix compression
3493 * from two adjacent leaf entries
3494 * across page boundary
3496 * return: non-zero on error
3499 static int ciGetLeafPrefixKey(dtpage_t * lp, int li, dtpage_t * rp,
3500 int ri, struct component_name * key, int flag)
3503 wchar_t *pl, *pr, *kname;
3504 struct component_name lkey;
3505 struct component_name rkey;
3507 lkey.name = kmalloc_array(JFS_NAME_MAX + 1, sizeof(wchar_t),
3509 if (lkey.name == NULL)
3512 rkey.name = kmalloc_array(JFS_NAME_MAX + 1, sizeof(wchar_t),
3514 if (rkey.name == NULL) {
3519 /* get left and right key */
3520 dtGetKey(lp, li, &lkey, flag);
3521 lkey.name[lkey.namlen] = 0;
3523 if ((flag & JFS_OS2) == JFS_OS2)
3526 dtGetKey(rp, ri, &rkey, flag);
3527 rkey.name[rkey.namlen] = 0;
3530 if ((flag & JFS_OS2) == JFS_OS2)
3533 /* compute prefix */
3536 namlen = min(lkey.namlen, rkey.namlen);
3537 for (pl = lkey.name, pr = rkey.name;
3538 namlen; pl++, pr++, namlen--, klen++, kname++) {
3541 key->namlen = klen + 1;
3546 /* l->namlen <= r->namlen since l <= r */
3547 if (lkey.namlen < rkey.namlen) {
3549 key->namlen = klen + 1;
3550 } else /* l->namelen == r->namelen */
3564 * function: get key of the entry
3566 static void dtGetKey(dtpage_t * p, int i, /* entry index */
3567 struct component_name * key, int flag)
3571 struct ldtentry *lh;
3572 struct idtentry *ih;
3579 stbl = DT_GETSTBL(p);
3581 if (p->header.flag & BT_LEAF) {
3582 lh = (struct ldtentry *) & p->slot[si];
3584 namlen = lh->namlen;
3586 if (flag & JFS_DIR_INDEX)
3587 len = min(namlen, DTLHDRDATALEN);
3589 len = min(namlen, DTLHDRDATALEN_LEGACY);
3591 ih = (struct idtentry *) & p->slot[si];
3593 namlen = ih->namlen;
3595 len = min(namlen, DTIHDRDATALEN);
3598 key->namlen = namlen;
3602 * move head/only segment
3604 UniStrncpy_from_le(kname, name, len);
3607 * move additional segment(s)
3610 /* get next segment */
3614 len = min(namlen, DTSLOTDATALEN);
3615 UniStrncpy_from_le(kname, t->name, len);
3625 * function: allocate free slot(s) and
3626 * write a leaf/internal entry
3628 * return: entry slot index
3630 static void dtInsertEntry(dtpage_t * p, int index, struct component_name * key,
3631 ddata_t * data, struct dt_lock ** dtlock)
3633 struct dtslot *h, *t;
3634 struct ldtentry *lh = NULL;
3635 struct idtentry *ih = NULL;
3636 int hsi, fsi, klen, len, nextindex;
3641 struct dt_lock *dtlck = *dtlock;
3645 struct metapage *mp = NULL;
3650 /* allocate a free slot */
3651 hsi = fsi = p->header.freelist;
3653 p->header.freelist = h->next;
3654 --p->header.freecnt;
3656 /* open new linelock */
3657 if (dtlck->index >= dtlck->maxcnt)
3658 dtlck = (struct dt_lock *) txLinelock(dtlck);
3660 lv = & dtlck->lv[dtlck->index];
3663 /* write head/only segment */
3664 if (p->header.flag & BT_LEAF) {
3665 lh = (struct ldtentry *) h;
3667 lh->inumber = cpu_to_le32(data->leaf.ino);
3670 if (data->leaf.ip) {
3671 len = min(klen, DTLHDRDATALEN);
3672 if (!(p->header.flag & BT_ROOT))
3673 bn = addressPXD(&p->header.self);
3674 lh->index = cpu_to_le32(add_index(data->leaf.tid,
3678 len = min(klen, DTLHDRDATALEN_LEGACY);
3680 ih = (struct idtentry *) h;
3686 len = min(klen, DTIHDRDATALEN);
3689 UniStrncpy_to_le(name, kname, len);
3694 /* write additional segment(s) */
3699 fsi = p->header.freelist;
3701 p->header.freelist = t->next;
3702 --p->header.freecnt;
3704 /* is next slot contiguous ? */
3705 if (fsi != xsi + 1) {
3706 /* close current linelock */
3710 /* open new linelock */
3711 if (dtlck->index < dtlck->maxcnt)
3714 dtlck = (struct dt_lock *) txLinelock(dtlck);
3715 lv = & dtlck->lv[0];
3723 len = min(klen, DTSLOTDATALEN);
3724 UniStrncpy_to_le(t->name, kname, len);
3731 /* close current linelock */
3737 /* terminate last/only segment */
3739 /* single segment entry */
3740 if (p->header.flag & BT_LEAF)
3745 /* multi-segment entry */
3748 /* if insert into middle, shift right succeeding entries in stbl */
3749 stbl = DT_GETSTBL(p);
3750 nextindex = p->header.nextindex;
3751 if (index < nextindex) {
3752 memmove(stbl + index + 1, stbl + index, nextindex - index);
3754 if ((p->header.flag & BT_LEAF) && data->leaf.ip) {
3758 * Need to update slot number for entries that moved
3762 for (n = index + 1; n <= nextindex; n++) {
3763 lh = (struct ldtentry *) & (p->slot[stbl[n]]);
3764 modify_index(data->leaf.tid, data->leaf.ip,
3765 le32_to_cpu(lh->index), bn, n,
3769 release_metapage(mp);
3775 /* advance next available entry index of stbl */
3776 ++p->header.nextindex;
3783 * function: move entries from split/left page to new/right page
3785 * nextindex of dst page and freelist/freecnt of both pages
3788 static void dtMoveEntry(dtpage_t * sp, int si, dtpage_t * dp,
3789 struct dt_lock ** sdtlock, struct dt_lock ** ddtlock,
3792 int ssi, next; /* src slot index */
3793 int di; /* dst entry index */
3794 int dsi; /* dst slot index */
3795 s8 *sstbl, *dstbl; /* sorted entry table */
3797 struct ldtentry *slh, *dlh = NULL;
3798 struct idtentry *sih, *dih = NULL;
3799 struct dtslot *h, *s, *d;
3800 struct dt_lock *sdtlck = *sdtlock, *ddtlck = *ddtlock;
3801 struct lv *slv, *dlv;
3805 sstbl = (s8 *) & sp->slot[sp->header.stblindex];
3806 dstbl = (s8 *) & dp->slot[dp->header.stblindex];
3808 dsi = dp->header.freelist; /* first (whole page) free slot */
3809 sfsi = sp->header.freelist;
3811 /* linelock destination entry slot */
3812 dlv = & ddtlck->lv[ddtlck->index];
3815 /* linelock source entry slot */
3816 slv = & sdtlck->lv[sdtlck->index];
3817 slv->offset = sstbl[si];
3818 xssi = slv->offset - 1;
3824 for (di = 0; si < sp->header.nextindex; si++, di++) {
3828 /* is next slot contiguous ? */
3829 if (ssi != xssi + 1) {
3830 /* close current linelock */
3834 /* open new linelock */
3835 if (sdtlck->index < sdtlck->maxcnt)
3838 sdtlck = (struct dt_lock *) txLinelock(sdtlck);
3839 slv = & sdtlck->lv[0];
3847 * move head/only segment of an entry
3850 h = d = &dp->slot[dsi];
3852 /* get src slot and move */
3854 if (sp->header.flag & BT_LEAF) {
3855 /* get source entry */
3856 slh = (struct ldtentry *) s;
3857 dlh = (struct ldtentry *) h;
3858 snamlen = slh->namlen;
3861 len = min(snamlen, DTLHDRDATALEN);
3862 dlh->index = slh->index; /* little-endian */
3864 len = min(snamlen, DTLHDRDATALEN_LEGACY);
3866 memcpy(dlh, slh, 6 + len * 2);
3870 /* update dst head/only segment next field */
3874 sih = (struct idtentry *) s;
3875 snamlen = sih->namlen;
3877 len = min(snamlen, DTIHDRDATALEN);
3878 dih = (struct idtentry *) h;
3879 memcpy(dih, sih, 10 + len * 2);
3886 /* free src head/only segment */
3896 * move additional segment(s) of the entry
3899 while ((ssi = next) >= 0) {
3900 /* is next slot contiguous ? */
3901 if (ssi != xssi + 1) {
3902 /* close current linelock */
3906 /* open new linelock */
3907 if (sdtlck->index < sdtlck->maxcnt)
3913 slv = & sdtlck->lv[0];
3920 /* get next source segment */
3923 /* get next destination free slot */
3926 len = min(snamlen, DTSLOTDATALEN);
3927 UniStrncpy_le(d->name, s->name, len);
3936 /* free source segment */
3945 /* terminate dst last/only segment */
3947 /* single segment entry */
3948 if (dp->header.flag & BT_LEAF)
3953 /* multi-segment entry */
3957 /* close current linelock */
3966 /* update source header */
3967 sp->header.freelist = sfsi;
3968 sp->header.freecnt += nd;
3970 /* update destination header */
3971 dp->header.nextindex = di;
3973 dp->header.freelist = dsi;
3974 dp->header.freecnt -= nd;
3981 * function: free a (leaf/internal) entry
3983 * log freelist header, stbl, and each segment slot of entry
3984 * (even though last/only segment next field is modified,
3985 * physical image logging requires all segment slots of
3986 * the entry logged to avoid applying previous updates
3987 * to the same slots)
3989 static void dtDeleteEntry(dtpage_t * p, int fi, struct dt_lock ** dtlock)
3991 int fsi; /* free entry slot index */
3995 struct dt_lock *dtlck = *dtlock;
3999 /* get free entry slot index */
4000 stbl = DT_GETSTBL(p);
4003 /* open new linelock */
4004 if (dtlck->index >= dtlck->maxcnt)
4005 dtlck = (struct dt_lock *) txLinelock(dtlck);
4006 lv = & dtlck->lv[dtlck->index];
4010 /* get the head/only segment */
4012 if (p->header.flag & BT_LEAF)
4013 si = ((struct ldtentry *) t)->next;
4015 si = ((struct idtentry *) t)->next;
4022 /* find the last/only segment */
4024 /* is next slot contiguous ? */
4025 if (si != xsi + 1) {
4026 /* close current linelock */
4030 /* open new linelock */
4031 if (dtlck->index < dtlck->maxcnt)
4034 dtlck = (struct dt_lock *) txLinelock(dtlck);
4035 lv = & dtlck->lv[0];
4051 /* close current linelock */
4057 /* update freelist */
4058 t->next = p->header.freelist;
4059 p->header.freelist = fsi;
4060 p->header.freecnt += freecnt;
4062 /* if delete from middle,
4063 * shift left the succedding entries in the stbl
4065 si = p->header.nextindex;
4067 memmove(&stbl[fi], &stbl[fi + 1], si - fi - 1);
4069 p->header.nextindex--;
4076 * function: truncate a (leaf/internal) entry
4078 * log freelist header, stbl, and each segment slot of entry
4079 * (even though last/only segment next field is modified,
4080 * physical image logging requires all segment slots of
4081 * the entry logged to avoid applying previous updates
4082 * to the same slots)
4084 static void dtTruncateEntry(dtpage_t * p, int ti, struct dt_lock ** dtlock)
4086 int tsi; /* truncate entry slot index */
4090 struct dt_lock *dtlck = *dtlock;
4094 /* get free entry slot index */
4095 stbl = DT_GETSTBL(p);
4098 /* open new linelock */
4099 if (dtlck->index >= dtlck->maxcnt)
4100 dtlck = (struct dt_lock *) txLinelock(dtlck);
4101 lv = & dtlck->lv[dtlck->index];
4105 /* get the head/only segment */
4107 ASSERT(p->header.flag & BT_INTERNAL);
4108 ((struct idtentry *) t)->namlen = 0;
4109 si = ((struct idtentry *) t)->next;
4110 ((struct idtentry *) t)->next = -1;
4117 /* find the last/only segment */
4119 /* is next slot contiguous ? */
4120 if (si != xsi + 1) {
4121 /* close current linelock */
4125 /* open new linelock */
4126 if (dtlck->index < dtlck->maxcnt)
4129 dtlck = (struct dt_lock *) txLinelock(dtlck);
4130 lv = & dtlck->lv[0];
4146 /* close current linelock */
4152 /* update freelist */
4155 t->next = p->header.freelist;
4156 p->header.freelist = fsi;
4157 p->header.freecnt += freecnt;
4162 * dtLinelockFreelist()
4164 static void dtLinelockFreelist(dtpage_t * p, /* directory page */
4165 int m, /* max slot index */
4166 struct dt_lock ** dtlock)
4168 int fsi; /* free entry slot index */
4171 struct dt_lock *dtlck = *dtlock;
4175 /* get free entry slot index */
4176 fsi = p->header.freelist;
4178 /* open new linelock */
4179 if (dtlck->index >= dtlck->maxcnt)
4180 dtlck = (struct dt_lock *) txLinelock(dtlck);
4181 lv = & dtlck->lv[dtlck->index];
4191 /* find the last/only segment */
4192 while (si < m && si >= 0) {
4193 /* is next slot contiguous ? */
4194 if (si != xsi + 1) {
4195 /* close current linelock */
4199 /* open new linelock */
4200 if (dtlck->index < dtlck->maxcnt)
4203 dtlck = (struct dt_lock *) txLinelock(dtlck);
4204 lv = & dtlck->lv[0];
4218 /* close current linelock */
4229 * FUNCTION: Modify the inode number part of a directory entry
4232 * tid - Transaction id
4233 * ip - Inode of parent directory
4234 * key - Name of entry to be modified
4235 * orig_ino - Original inode number expected in entry
4236 * new_ino - New inode number to put into entry
4240 * -ESTALE - If entry found does not match orig_ino passed in
4241 * -ENOENT - If no entry can be found to match key
4242 * 0 - If successfully modified entry
4244 int dtModify(tid_t tid, struct inode *ip,
4245 struct component_name * key, ino_t * orig_ino, ino_t new_ino, int flag)
4249 struct metapage *mp;
4252 struct btstack btstack;
4254 struct dt_lock *dtlck;
4257 int entry_si; /* entry slot index */
4258 struct ldtentry *entry;
4261 * search for the entry to modify:
4263 * dtSearch() returns (leaf page pinned, index at which to modify).
4265 if ((rc = dtSearch(ip, key, orig_ino, &btstack, flag)))
4268 /* retrieve search result */
4269 DT_GETSEARCH(ip, btstack.top, bn, mp, p, index);
4271 BT_MARK_DIRTY(mp, ip);
4273 * acquire a transaction lock on the leaf page of named entry
4275 tlck = txLock(tid, ip, mp, tlckDTREE | tlckENTRY);
4276 dtlck = (struct dt_lock *) & tlck->lock;
4278 /* get slot index of the entry */
4279 stbl = DT_GETSTBL(p);
4280 entry_si = stbl[index];
4282 /* linelock entry */
4283 ASSERT(dtlck->index == 0);
4284 lv = & dtlck->lv[0];
4285 lv->offset = entry_si;
4289 /* get the head/only segment */
4290 entry = (struct ldtentry *) & p->slot[entry_si];
4292 /* substitute the inode number of the entry */
4293 entry->inumber = cpu_to_le32(new_ino);
4295 /* unpin the leaf page */