2 * Copyright (C) International Business Machines Corp., 2000-2004
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
12 * the GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 * jfs_dtree.c: directory B+-tree manager
22 * B+-tree with variable length key directory:
24 * each directory page is structured as an array of 32-byte
25 * directory entry slots initialized as a freelist
26 * to avoid search/compaction of free space at insertion.
27 * when an entry is inserted, a number of slots are allocated
28 * from the freelist as required to store variable length data
29 * of the entry; when the entry is deleted, slots of the entry
30 * are returned to freelist.
32 * leaf entry stores full name as key and file serial number
33 * (aka inode number) as data.
34 * internal/router entry stores sufffix compressed name
35 * as key and simple extent descriptor as data.
37 * each directory page maintains a sorted entry index table
38 * which stores the start slot index of sorted entries
39 * to allow binary search on the table.
41 * directory starts as a root/leaf page in on-disk inode
43 * when it becomes full, it starts a leaf of a external extent
44 * of length of 1 block. each time the first leaf becomes full,
45 * it is extended rather than split (its size is doubled),
46 * until its length becoms 4 KBytes, from then the extent is split
47 * with new 4 Kbyte extent when it becomes full
48 * to reduce external fragmentation of small directories.
50 * blah, blah, blah, for linear scan of directory in pieces by
54 * case-insensitive directory file system
56 * names are stored in case-sensitive way in leaf entry.
57 * but stored, searched and compared in case-insensitive (uppercase) order
58 * (i.e., both search key and entry key are folded for search/compare):
59 * (note that case-sensitive order is BROKEN in storage, e.g.,
60 * sensitive: Ad, aB, aC, aD -> insensitive: aB, aC, aD, Ad
62 * entries which folds to the same key makes up a equivalent class
63 * whose members are stored as contiguous cluster (may cross page boundary)
64 * but whose order is arbitrary and acts as duplicate, e.g.,
67 * once match is found at leaf, requires scan forward/backward
68 * either for, in case-insensitive search, duplicate
69 * or for, in case-sensitive search, for exact match
71 * router entry must be created/stored in case-insensitive way
73 * (right most key of left page and left most key of right page
74 * are folded, and its suffix compression is propagated as router
76 * (e.g., if split occurs <abc> and <aBd>, <ABD> trather than <aB>
77 * should be made the router key for the split)
79 * case-insensitive search:
83 * case-insensitive search of B-tree:
84 * for internal entry, router key is already folded;
85 * for leaf entry, fold the entry key before comparison.
87 * if (leaf entry case-insensitive match found)
88 * if (next entry satisfies case-insensitive match)
90 * if (prev entry satisfies case-insensitive match)
97 * target directory inode lock is being held on entry/exit
98 * of all main directory service routines.
100 * log based recovery:
103 #include <linux/fs.h>
104 #include <linux/quotaops.h>
105 #include <linux/vs_dlimit.h>
106 #include "jfs_incore.h"
107 #include "jfs_superblock.h"
108 #include "jfs_filsys.h"
109 #include "jfs_metapage.h"
110 #include "jfs_dmap.h"
111 #include "jfs_unicode.h"
112 #include "jfs_debug.h"
114 /* dtree split parameter */
119 struct component_name *key;
121 struct pxdlist *pxdlist;
124 #define DT_PAGE(IP, MP) BT_PAGE(IP, MP, dtpage_t, i_dtroot)
126 /* get page buffer for specified block address */
127 #define DT_GETPAGE(IP, BN, MP, SIZE, P, RC)\
129 BT_GETPAGE(IP, BN, MP, dtpage_t, SIZE, P, RC, i_dtroot)\
132 if (((P)->header.nextindex > (((BN)==0)?DTROOTMAXSLOT:(P)->header.maxslot)) ||\
133 ((BN) && ((P)->header.maxslot > DTPAGEMAXSLOT)))\
136 jfs_error((IP)->i_sb, "DT_GETPAGE: dtree page corrupt");\
143 /* for consistency */
144 #define DT_PUTPAGE(MP) BT_PUTPAGE(MP)
146 #define DT_GETSEARCH(IP, LEAF, BN, MP, P, INDEX) \
147 BT_GETSEARCH(IP, LEAF, BN, MP, dtpage_t, P, INDEX, i_dtroot)
152 static int dtSplitUp(tid_t tid, struct inode *ip,
153 struct dtsplit * split, struct btstack * btstack);
155 static int dtSplitPage(tid_t tid, struct inode *ip, struct dtsplit * split,
156 struct metapage ** rmpp, dtpage_t ** rpp, pxd_t * rxdp);
158 static int dtExtendPage(tid_t tid, struct inode *ip,
159 struct dtsplit * split, struct btstack * btstack);
161 static int dtSplitRoot(tid_t tid, struct inode *ip,
162 struct dtsplit * split, struct metapage ** rmpp);
164 static int dtDeleteUp(tid_t tid, struct inode *ip, struct metapage * fmp,
165 dtpage_t * fp, struct btstack * btstack);
167 static int dtRelink(tid_t tid, struct inode *ip, dtpage_t * p);
169 static int dtReadFirst(struct inode *ip, struct btstack * btstack);
171 static int dtReadNext(struct inode *ip,
172 loff_t * offset, struct btstack * btstack);
174 static int dtCompare(struct component_name * key, dtpage_t * p, int si);
176 static int ciCompare(struct component_name * key, dtpage_t * p, int si,
179 static void dtGetKey(dtpage_t * p, int i, struct component_name * key,
182 static int ciGetLeafPrefixKey(dtpage_t * lp, int li, dtpage_t * rp,
183 int ri, struct component_name * key, int flag);
185 static void dtInsertEntry(dtpage_t * p, int index, struct component_name * key,
186 ddata_t * data, struct dt_lock **);
188 static void dtMoveEntry(dtpage_t * sp, int si, dtpage_t * dp,
189 struct dt_lock ** sdtlock, struct dt_lock ** ddtlock,
192 static void dtDeleteEntry(dtpage_t * p, int fi, struct dt_lock ** dtlock);
194 static void dtTruncateEntry(dtpage_t * p, int ti, struct dt_lock ** dtlock);
196 static void dtLinelockFreelist(dtpage_t * p, int m, struct dt_lock ** dtlock);
198 #define ciToUpper(c) UniStrupr((c)->name)
203 * Reads a page of a directory's index table.
204 * Having metadata mapped into the directory inode's address space
205 * presents a multitude of problems. We avoid this by mapping to
206 * the absolute address space outside of the *_metapage routines
208 static struct metapage *read_index_page(struct inode *inode, s64 blkno)
215 rc = xtLookup(inode, blkno, 1, &xflag, &xaddr, &xlen, 1);
216 if (rc || (xaddr == 0))
219 return read_metapage(inode, xaddr, PSIZE, 1);
225 * Same as get_index_page(), but get's a new page without reading
227 static struct metapage *get_index_page(struct inode *inode, s64 blkno)
234 rc = xtLookup(inode, blkno, 1, &xflag, &xaddr, &xlen, 1);
235 if (rc || (xaddr == 0))
238 return get_metapage(inode, xaddr, PSIZE, 1);
244 * Returns dtree page containing directory table entry for specified
245 * index and pointer to its entry.
247 * mp must be released by caller.
249 static struct dir_table_slot *find_index(struct inode *ip, u32 index,
250 struct metapage ** mp, s64 *lblock)
252 struct jfs_inode_info *jfs_ip = JFS_IP(ip);
256 struct dir_table_slot *slot;
257 static int maxWarnings = 10;
261 jfs_warn("find_entry called with index = %d", index);
267 if (index >= jfs_ip->next_index) {
268 jfs_warn("find_entry called with index >= next_index");
272 if (jfs_dirtable_inline(ip)) {
274 * Inline directory table
277 slot = &jfs_ip->i_dirtable[index - 2];
279 offset = (index - 2) * sizeof(struct dir_table_slot);
280 page_offset = offset & (PSIZE - 1);
281 blkno = ((offset + 1) >> L2PSIZE) <<
282 JFS_SBI(ip->i_sb)->l2nbperpage;
284 if (*mp && (*lblock != blkno)) {
285 release_metapage(*mp);
290 *mp = read_index_page(ip, blkno);
293 jfs_err("free_index: error reading directory table");
298 (struct dir_table_slot *) ((char *) (*mp)->data +
304 static inline void lock_index(tid_t tid, struct inode *ip, struct metapage * mp,
308 struct linelock *llck;
311 tlck = txLock(tid, ip, mp, tlckDATA);
312 llck = (struct linelock *) tlck->lock;
314 if (llck->index >= llck->maxcnt)
315 llck = txLinelock(llck);
316 lv = &llck->lv[llck->index];
319 * Linelock slot size is twice the size of directory table
320 * slot size. 512 entries per page.
322 lv->offset = ((index - 2) & 511) >> 1;
330 * Adds an entry to the directory index table. This is used to provide
331 * each directory entry with a persistent index in which to resume
332 * directory traversals
334 static u32 add_index(tid_t tid, struct inode *ip, s64 bn, int slot)
336 struct super_block *sb = ip->i_sb;
337 struct jfs_sb_info *sbi = JFS_SBI(sb);
338 struct jfs_inode_info *jfs_ip = JFS_IP(ip);
340 struct dir_table_slot *dirtab_slot;
342 struct linelock *llck;
350 ASSERT(DO_INDEX(ip));
352 if (jfs_ip->next_index < 2) {
353 jfs_warn("add_index: next_index = %d. Resetting!",
355 jfs_ip->next_index = 2;
358 index = jfs_ip->next_index++;
360 if (index <= MAX_INLINE_DIRTABLE_ENTRY) {
362 * i_size reflects size of index table, or 8 bytes per entry.
364 ip->i_size = (loff_t) (index - 1) << 3;
367 * dir table fits inline within inode
369 dirtab_slot = &jfs_ip->i_dirtable[index-2];
370 dirtab_slot->flag = DIR_INDEX_VALID;
371 dirtab_slot->slot = slot;
372 DTSaddress(dirtab_slot, bn);
374 set_cflag(COMMIT_Dirtable, ip);
378 if (index == (MAX_INLINE_DIRTABLE_ENTRY + 1)) {
379 struct dir_table_slot temp_table[12];
382 * It's time to move the inline table to an external
383 * page and begin to build the xtree
385 if (DQUOT_ALLOC_BLOCK(ip, sbi->nbperpage))
387 if (DLIMIT_ALLOC_BLOCK(ip, sbi->nbperpage))
389 if (dbAlloc(ip, 0, sbi->nbperpage, &xaddr))
393 * Save the table, we're going to overwrite it with the
396 memcpy(temp_table, &jfs_ip->i_dirtable, sizeof(temp_table));
399 * Initialize empty x-tree
404 * Add the first block to the xtree
406 if (xtInsert(tid, ip, 0, 0, sbi->nbperpage, &xaddr, 0)) {
407 /* This really shouldn't fail */
408 jfs_warn("add_index: xtInsert failed!");
409 memcpy(&jfs_ip->i_dirtable, temp_table,
410 sizeof (temp_table));
411 dbFree(ip, xaddr, sbi->nbperpage);
412 DQUOT_FREE_BLOCK(ip, sbi->nbperpage);
417 if ((mp = get_index_page(ip, 0)) == 0) {
418 jfs_err("add_index: get_metapage failed!");
419 xtTruncate(tid, ip, 0, COMMIT_PWMAP);
420 memcpy(&jfs_ip->i_dirtable, temp_table,
421 sizeof (temp_table));
424 tlck = txLock(tid, ip, mp, tlckDATA);
425 llck = (struct linelock *) & tlck->lock;
426 ASSERT(llck->index == 0);
430 lv->length = 6; /* tlckDATA slot size is 16 bytes */
433 memcpy(mp->data, temp_table, sizeof(temp_table));
435 mark_metapage_dirty(mp);
436 release_metapage(mp);
439 * Logging is now directed by xtree tlocks
441 clear_cflag(COMMIT_Dirtable, ip);
444 offset = (index - 2) * sizeof(struct dir_table_slot);
445 page_offset = offset & (PSIZE - 1);
446 blkno = ((offset + 1) >> L2PSIZE) << sbi->l2nbperpage;
447 if (page_offset == 0) {
449 * This will be the beginning of a new page
452 if (xtInsert(tid, ip, 0, blkno, sbi->nbperpage, &xaddr, 0)) {
453 jfs_warn("add_index: xtInsert failed!");
458 if ((mp = get_index_page(ip, blkno)))
459 memset(mp->data, 0, PSIZE); /* Just looks better */
461 xtTruncate(tid, ip, offset, COMMIT_PWMAP);
463 mp = read_index_page(ip, blkno);
466 jfs_err("add_index: get/read_metapage failed!");
470 lock_index(tid, ip, mp, index);
473 (struct dir_table_slot *) ((char *) mp->data + page_offset);
474 dirtab_slot->flag = DIR_INDEX_VALID;
475 dirtab_slot->slot = slot;
476 DTSaddress(dirtab_slot, bn);
478 mark_metapage_dirty(mp);
479 release_metapage(mp);
484 DLIMIT_FREE_BLOCK(ip, sbi->nbperpage);
486 DQUOT_FREE_BLOCK(ip, sbi->nbperpage);
489 jfs_ip->next_index--;
497 * Marks an entry to the directory index table as free.
499 static void free_index(tid_t tid, struct inode *ip, u32 index, u32 next)
501 struct dir_table_slot *dirtab_slot;
503 struct metapage *mp = NULL;
505 dirtab_slot = find_index(ip, index, &mp, &lblock);
507 if (dirtab_slot == 0)
510 dirtab_slot->flag = DIR_INDEX_FREE;
511 dirtab_slot->slot = dirtab_slot->addr1 = 0;
512 dirtab_slot->addr2 = cpu_to_le32(next);
515 lock_index(tid, ip, mp, index);
516 mark_metapage_dirty(mp);
517 release_metapage(mp);
519 set_cflag(COMMIT_Dirtable, ip);
525 * Changes an entry in the directory index table
527 static void modify_index(tid_t tid, struct inode *ip, u32 index, s64 bn,
528 int slot, struct metapage ** mp, u64 *lblock)
530 struct dir_table_slot *dirtab_slot;
532 dirtab_slot = find_index(ip, index, mp, lblock);
534 if (dirtab_slot == 0)
537 DTSaddress(dirtab_slot, bn);
538 dirtab_slot->slot = slot;
541 lock_index(tid, ip, *mp, index);
542 mark_metapage_dirty(*mp);
544 set_cflag(COMMIT_Dirtable, ip);
550 * reads a directory table slot
552 static int read_index(struct inode *ip, u32 index,
553 struct dir_table_slot * dirtab_slot)
556 struct metapage *mp = NULL;
557 struct dir_table_slot *slot;
559 slot = find_index(ip, index, &mp, &lblock);
564 memcpy(dirtab_slot, slot, sizeof(struct dir_table_slot));
567 release_metapage(mp);
576 * Search for the entry with specified key
580 * return: 0 - search result on stack, leaf page pinned;
583 int dtSearch(struct inode *ip, struct component_name * key, ino_t * data,
584 struct btstack * btstack, int flag)
587 int cmp = 1; /* init for empty page */
592 int base, index, lim;
593 struct btframe *btsp;
595 int psize = 288; /* initial in-line directory */
597 struct component_name ciKey;
598 struct super_block *sb = ip->i_sb;
601 (wchar_t *) kmalloc((JFS_NAME_MAX + 1) * sizeof(wchar_t),
603 if (ciKey.name == 0) {
609 /* uppercase search key for c-i directory */
610 UniStrcpy(ciKey.name, key->name);
611 ciKey.namlen = key->namlen;
613 /* only uppercase if case-insensitive support is on */
614 if ((JFS_SBI(sb)->mntflag & JFS_OS2) == JFS_OS2) {
617 BT_CLR(btstack); /* reset stack */
619 /* init level count for max pages to split */
623 * search down tree from root:
625 * between two consecutive entries of <Ki, Pi> and <Kj, Pj> of
626 * internal page, child page Pi contains entry with k, Ki <= K < Kj.
628 * if entry with search key K is not found
629 * internal page search find the entry with largest key Ki
630 * less than K which point to the child page to search;
631 * leaf page search find the entry with smallest key Kj
632 * greater than K so that the returned index is the position of
633 * the entry to be shifted right for insertion of new entry.
634 * for empty tree, search key is greater than any key of the tree.
636 * by convention, root bn = 0.
639 /* get/pin the page to search */
640 DT_GETPAGE(ip, bn, mp, psize, p, rc);
644 /* get sorted entry table of the page */
645 stbl = DT_GETSTBL(p);
648 * binary search with search key K on the current page.
650 for (base = 0, lim = p->header.nextindex; lim; lim >>= 1) {
651 index = base + (lim >> 1);
653 if (p->header.flag & BT_LEAF) {
654 /* uppercase leaf name to compare */
656 ciCompare(&ciKey, p, stbl[index],
657 JFS_SBI(sb)->mntflag);
659 /* router key is in uppercase */
661 cmp = dtCompare(&ciKey, p, stbl[index]);
669 /* search hit - leaf page:
670 * return the entry found
672 if (p->header.flag & BT_LEAF) {
673 inumber = le32_to_cpu(
674 ((struct ldtentry *) & p->slot[stbl[index]])->inumber);
677 * search for JFS_LOOKUP
679 if (flag == JFS_LOOKUP) {
686 * search for JFS_CREATE
688 if (flag == JFS_CREATE) {
695 * search for JFS_REMOVE or JFS_RENAME
697 if ((flag == JFS_REMOVE ||
698 flag == JFS_RENAME) &&
705 * JFS_REMOVE|JFS_FINDDIR|JFS_RENAME
707 /* save search result */
718 /* search hit - internal page:
719 * descend/search its child page
733 * base is the smallest index with key (Kj) greater than
734 * search key (K) and may be zero or (maxindex + 1) index.
737 * search miss - leaf page
739 * return location of entry (base) where new entry with
740 * search key K is to be inserted.
742 if (p->header.flag & BT_LEAF) {
744 * search for JFS_LOOKUP, JFS_REMOVE, or JFS_RENAME
746 if (flag == JFS_LOOKUP || flag == JFS_REMOVE ||
747 flag == JFS_RENAME) {
753 * search for JFS_CREATE|JFS_FINDDIR:
768 * search miss - internal page
770 * if base is non-zero, decrement base by one to get the parent
771 * entry of the child page to search.
773 index = base ? base - 1 : base;
776 * go down to child page
779 /* update max. number of pages to split */
780 if (BT_STACK_FULL(btstack)) {
781 /* Something's corrupted, mark filesytem dirty so
782 * chkdsk will fix it.
784 jfs_error(sb, "stack overrun in dtSearch!");
785 BT_STACK_DUMP(btstack);
791 /* push (bn, index) of the parent page/entry */
792 BT_PUSH(btstack, bn, index);
794 /* get the child page block number */
795 pxd = (pxd_t *) & p->slot[stbl[index]];
796 bn = addressPXD(pxd);
797 psize = lengthPXD(pxd) << JFS_SBI(ip->i_sb)->l2bsize;
799 /* unpin the parent page */
819 * function: insert an entry to directory tree
823 * return: 0 - success;
826 int dtInsert(tid_t tid, struct inode *ip,
827 struct component_name * name, ino_t * fsn, struct btstack * btstack)
830 struct metapage *mp; /* meta-page buffer */
831 dtpage_t *p; /* base B+-tree index page */
834 struct dtsplit split; /* split information */
836 struct dt_lock *dtlck;
842 * retrieve search result
844 * dtSearch() returns (leaf page pinned, index at which to insert).
845 * n.b. dtSearch() may return index of (maxindex + 1) of
848 DT_GETSEARCH(ip, btstack->top, bn, mp, p, index);
851 * insert entry for new key
854 if (JFS_IP(ip)->next_index == DIREND) {
858 n = NDTLEAF(name->namlen);
862 n = NDTLEAF_LEGACY(name->namlen);
863 data.leaf.ip = NULL; /* signifies legacy directory format */
865 data.leaf.ino = *fsn;
868 * leaf page does not have enough room for new entry:
870 * extend/split the leaf page;
872 * dtSplitUp() will insert the entry and unpin the leaf page.
874 if (n > p->header.freecnt) {
880 rc = dtSplitUp(tid, ip, &split, btstack);
885 * leaf page does have enough room for new entry:
887 * insert the new data entry into the leaf page;
889 BT_MARK_DIRTY(mp, ip);
891 * acquire a transaction lock on the leaf page
893 tlck = txLock(tid, ip, mp, tlckDTREE | tlckENTRY);
894 dtlck = (struct dt_lock *) & tlck->lock;
895 ASSERT(dtlck->index == 0);
898 /* linelock header */
903 dtInsertEntry(p, index, name, &data, &dtlck);
905 /* linelock stbl of non-root leaf page */
906 if (!(p->header.flag & BT_ROOT)) {
907 if (dtlck->index >= dtlck->maxcnt)
908 dtlck = (struct dt_lock *) txLinelock(dtlck);
909 lv = & dtlck->lv[dtlck->index];
910 n = index >> L2DTSLOTSIZE;
911 lv->offset = p->header.stblindex + n;
913 ((p->header.nextindex - 1) >> L2DTSLOTSIZE) - n + 1;
917 /* unpin the leaf page */
927 * function: propagate insertion bottom up;
931 * return: 0 - success;
933 * leaf page unpinned;
935 static int dtSplitUp(tid_t tid,
936 struct inode *ip, struct dtsplit * split, struct btstack * btstack)
938 struct jfs_sb_info *sbi = JFS_SBI(ip->i_sb);
940 struct metapage *smp;
941 dtpage_t *sp; /* split page */
942 struct metapage *rmp;
943 dtpage_t *rp; /* new right page split from sp */
944 pxd_t rpxd; /* new right page extent descriptor */
945 struct metapage *lmp;
946 dtpage_t *lp; /* left child page */
947 int skip; /* index of entry of insertion */
948 struct btframe *parent; /* parent page entry on traverse stack */
951 struct pxdlist pxdlist;
953 struct component_name key = { 0, NULL };
954 ddata_t *data = split->data;
956 struct dt_lock *dtlck;
959 int quota_allocation = 0;
960 int dlimit_allocation = 0;
964 sp = DT_PAGE(ip, smp);
967 (wchar_t *) kmalloc((JFS_NAME_MAX + 2) * sizeof(wchar_t),
978 * The split routines insert the new entry, and
979 * acquire txLock as appropriate.
982 * split root leaf page:
984 if (sp->header.flag & BT_ROOT) {
986 * allocate a single extent child page
989 n = sbi->bsize >> L2DTSLOTSIZE;
990 n -= (n + 31) >> L2DTSLOTSIZE; /* stbl size */
991 n -= DTROOTMAXSLOT - sp->header.freecnt; /* header + entries */
992 if (n <= split->nslot)
994 if ((rc = dbAlloc(ip, 0, (s64) xlen, &xaddr))) {
1001 pxd = &pxdlist.pxd[0];
1002 PXDaddress(pxd, xaddr);
1003 PXDlength(pxd, xlen);
1004 split->pxdlist = &pxdlist;
1005 rc = dtSplitRoot(tid, ip, split, &rmp);
1008 dbFree(ip, xaddr, xlen);
1018 * extend first leaf page
1020 * extend the 1st extent if less than buffer page size
1021 * (dtExtendPage() reurns leaf page unpinned)
1023 pxd = &sp->header.self;
1024 xlen = lengthPXD(pxd);
1025 xsize = xlen << sbi->l2bsize;
1026 if (xsize < PSIZE) {
1027 xaddr = addressPXD(pxd);
1028 n = xsize >> L2DTSLOTSIZE;
1029 n -= (n + 31) >> L2DTSLOTSIZE; /* stbl size */
1030 if ((n + sp->header.freecnt) <= split->nslot)
1031 n = xlen + (xlen << 1);
1035 /* Allocate blocks to quota. */
1036 if (DQUOT_ALLOC_BLOCK(ip, n)) {
1040 quota_allocation += n;
1042 if (DLIMIT_ALLOC_BLOCK(ip, n)) {
1046 dlimit_allocation += n;
1048 if ((rc = dbReAlloc(sbi->ipbmap, xaddr, (s64) xlen,
1052 pxdlist.maxnpxd = 1;
1054 pxd = &pxdlist.pxd[0];
1055 PXDaddress(pxd, nxaddr)
1056 PXDlength(pxd, xlen + n);
1057 split->pxdlist = &pxdlist;
1058 if ((rc = dtExtendPage(tid, ip, split, btstack))) {
1059 nxaddr = addressPXD(pxd);
1060 if (xaddr != nxaddr) {
1061 /* free relocated extent */
1062 xlen = lengthPXD(pxd);
1063 dbFree(ip, nxaddr, (s64) xlen);
1065 /* free extended delta */
1066 xlen = lengthPXD(pxd) - n;
1067 xaddr = addressPXD(pxd) + xlen;
1068 dbFree(ip, xaddr, (s64) n);
1078 * split leaf page <sp> into <sp> and a new right page <rp>.
1080 * return <rp> pinned and its extent descriptor <rpxd>
1083 * allocate new directory page extent and
1084 * new index page(s) to cover page split(s)
1086 * allocation hint: ?
1088 n = btstack->nsplit;
1089 pxdlist.maxnpxd = pxdlist.npxd = 0;
1090 xlen = sbi->nbperpage;
1091 for (pxd = pxdlist.pxd; n > 0; n--, pxd++) {
1092 if ((rc = dbAlloc(ip, 0, (s64) xlen, &xaddr)) == 0) {
1093 PXDaddress(pxd, xaddr);
1094 PXDlength(pxd, xlen);
1101 /* undo allocation */
1105 split->pxdlist = &pxdlist;
1106 if ((rc = dtSplitPage(tid, ip, split, &rmp, &rp, &rpxd))) {
1109 /* undo allocation */
1114 * propagate up the router entry for the leaf page just split
1116 * insert a router entry for the new page into the parent page,
1117 * propagate the insert/split up the tree by walking back the stack
1118 * of (bn of parent page, index of child page entry in parent page)
1119 * that were traversed during the search for the page that split.
1121 * the propagation of insert/split up the tree stops if the root
1122 * splits or the page inserted into doesn't have to split to hold
1125 * the parent entry for the split page remains the same, and
1126 * a new entry is inserted at its right with the first key and
1127 * block number of the new right page.
1129 * There are a maximum of 4 pages pinned at any time:
1130 * two children, left parent and right parent (when the parent splits).
1131 * keep the child pages pinned while working on the parent.
1132 * make sure that all pins are released at exit.
1134 while ((parent = BT_POP(btstack)) != NULL) {
1135 /* parent page specified by stack frame <parent> */
1137 /* keep current child pages (<lp>, <rp>) pinned */
1142 * insert router entry in parent for new right child page <rp>
1144 /* get the parent page <sp> */
1145 DT_GETPAGE(ip, parent->bn, smp, PSIZE, sp, rc);
1153 * The new key entry goes ONE AFTER the index of parent entry,
1154 * because the split was to the right.
1156 skip = parent->index + 1;
1159 * compute the key for the router entry
1161 * key suffix compression:
1162 * for internal pages that have leaf pages as children,
1163 * retain only what's needed to distinguish between
1164 * the new entry and the entry on the page to its left.
1165 * If the keys compare equal, retain the entire key.
1167 * note that compression is performed only at computing
1168 * router key at the lowest internal level.
1169 * further compression of the key between pairs of higher
1170 * level internal pages loses too much information and
1171 * the search may fail.
1172 * (e.g., two adjacent leaf pages of {a, ..., x} {xx, ...,}
1173 * results in two adjacent parent entries (a)(xx).
1174 * if split occurs between these two entries, and
1175 * if compression is applied, the router key of parent entry
1176 * of right page (x) will divert search for x into right
1177 * subtree and miss x in the left subtree.)
1179 * the entire key must be retained for the next-to-leftmost
1180 * internal key at any level of the tree, or search may fail
1183 switch (rp->header.flag & BT_TYPE) {
1186 * compute the length of prefix for suffix compression
1187 * between last entry of left page and first entry
1190 if ((sp->header.flag & BT_ROOT && skip > 1) ||
1191 sp->header.prev != 0 || skip > 1) {
1192 /* compute uppercase router prefix key */
1193 rc = ciGetLeafPrefixKey(lp,
1194 lp->header.nextindex-1,
1204 /* next to leftmost entry of
1205 lowest internal level */
1207 /* compute uppercase router key */
1208 dtGetKey(rp, 0, &key, sbi->mntflag);
1209 key.name[key.namlen] = 0;
1211 if ((sbi->mntflag & JFS_OS2) == JFS_OS2)
1215 n = NDTINTERNAL(key.namlen);
1219 dtGetKey(rp, 0, &key, sbi->mntflag);
1220 n = NDTINTERNAL(key.namlen);
1224 jfs_err("dtSplitUp(): UFO!");
1228 /* unpin left child page */
1232 * compute the data for the router entry
1234 data->xd = rpxd; /* child page xd */
1237 * parent page is full - split the parent page
1239 if (n > sp->header.freecnt) {
1240 /* init for parent page split */
1242 split->index = skip; /* index at insert */
1245 /* split->data = data; */
1247 /* unpin right child page */
1250 /* The split routines insert the new entry,
1251 * acquire txLock as appropriate.
1252 * return <rp> pinned and its block number <rbn>.
1254 rc = (sp->header.flag & BT_ROOT) ?
1255 dtSplitRoot(tid, ip, split, &rmp) :
1256 dtSplitPage(tid, ip, split, &rmp, &rp, &rpxd);
1262 /* smp and rmp are pinned */
1265 * parent page is not full - insert router entry in parent page
1268 BT_MARK_DIRTY(smp, ip);
1270 * acquire a transaction lock on the parent page
1272 tlck = txLock(tid, ip, smp, tlckDTREE | tlckENTRY);
1273 dtlck = (struct dt_lock *) & tlck->lock;
1274 ASSERT(dtlck->index == 0);
1275 lv = & dtlck->lv[0];
1277 /* linelock header */
1282 /* linelock stbl of non-root parent page */
1283 if (!(sp->header.flag & BT_ROOT)) {
1285 n = skip >> L2DTSLOTSIZE;
1286 lv->offset = sp->header.stblindex + n;
1288 ((sp->header.nextindex -
1289 1) >> L2DTSLOTSIZE) - n + 1;
1293 dtInsertEntry(sp, skip, &key, data, &dtlck);
1295 /* exit propagate up */
1300 /* unpin current split and its right page */
1305 * free remaining extents allocated for split
1309 pxd = &pxdlist.pxd[n];
1310 for (; n < pxdlist.maxnpxd; n++, pxd++)
1311 dbFree(ip, addressPXD(pxd), (s64) lengthPXD(pxd));
1316 /* Rollback dlimit allocation */
1317 if (rc && dlimit_allocation)
1318 DLIMIT_FREE_BLOCK(ip, dlimit_allocation);
1319 /* Rollback quota allocation */
1320 if (rc && quota_allocation)
1321 DQUOT_FREE_BLOCK(ip, quota_allocation);
1332 * function: Split a non-root page of a btree.
1336 * return: 0 - success;
1338 * return split and new page pinned;
1340 static int dtSplitPage(tid_t tid, struct inode *ip, struct dtsplit * split,
1341 struct metapage ** rmpp, dtpage_t ** rpp, pxd_t * rpxdp)
1344 struct metapage *smp;
1346 struct metapage *rmp;
1347 dtpage_t *rp; /* new right page allocated */
1348 s64 rbn; /* new right page block number */
1349 struct metapage *mp;
1352 struct pxdlist *pxdlist;
1354 int skip, nextindex, half, left, nxt, off, si;
1355 struct ldtentry *ldtentry;
1356 struct idtentry *idtentry;
1361 struct dt_lock *sdtlck, *rdtlck;
1363 struct dt_lock *dtlck;
1364 struct lv *slv, *rlv, *lv;
1366 /* get split page */
1368 sp = DT_PAGE(ip, smp);
1371 * allocate the new right page for the split
1373 pxdlist = split->pxdlist;
1374 pxd = &pxdlist->pxd[pxdlist->npxd];
1376 rbn = addressPXD(pxd);
1377 rmp = get_metapage(ip, rbn, PSIZE, 1);
1381 /* Allocate blocks to quota. */
1382 if (DQUOT_ALLOC_BLOCK(ip, lengthPXD(pxd))) {
1383 release_metapage(rmp);
1386 /* Allocate blocks to dlimit. */
1387 if (DLIMIT_ALLOC_BLOCK(ip, lengthPXD(pxd))) {
1388 DQUOT_FREE_BLOCK(ip, lengthPXD(pxd));
1389 release_metapage(rmp);
1393 jfs_info("dtSplitPage: ip:0x%p smp:0x%p rmp:0x%p", ip, smp, rmp);
1395 BT_MARK_DIRTY(rmp, ip);
1397 * acquire a transaction lock on the new right page
1399 tlck = txLock(tid, ip, rmp, tlckDTREE | tlckNEW);
1400 rdtlck = (struct dt_lock *) & tlck->lock;
1402 rp = (dtpage_t *) rmp->data;
1404 rp->header.self = *pxd;
1406 BT_MARK_DIRTY(smp, ip);
1408 * acquire a transaction lock on the split page
1412 tlck = txLock(tid, ip, smp, tlckDTREE | tlckENTRY);
1413 sdtlck = (struct dt_lock *) & tlck->lock;
1415 /* linelock header of split page */
1416 ASSERT(sdtlck->index == 0);
1417 slv = & sdtlck->lv[0];
1423 * initialize/update sibling pointers between sp and rp
1425 nextbn = le64_to_cpu(sp->header.next);
1426 rp->header.next = cpu_to_le64(nextbn);
1427 rp->header.prev = cpu_to_le64(addressPXD(&sp->header.self));
1428 sp->header.next = cpu_to_le64(rbn);
1431 * initialize new right page
1433 rp->header.flag = sp->header.flag;
1435 /* compute sorted entry table at start of extent data area */
1436 rp->header.nextindex = 0;
1437 rp->header.stblindex = 1;
1439 n = PSIZE >> L2DTSLOTSIZE;
1440 rp->header.maxslot = n;
1441 stblsize = (n + 31) >> L2DTSLOTSIZE; /* in unit of slot */
1444 fsi = rp->header.stblindex + stblsize;
1445 rp->header.freelist = fsi;
1446 rp->header.freecnt = rp->header.maxslot - fsi;
1449 * sequential append at tail: append without split
1451 * If splitting the last page on a level because of appending
1452 * a entry to it (skip is maxentry), it's likely that the access is
1453 * sequential. Adding an empty page on the side of the level is less
1454 * work and can push the fill factor much higher than normal.
1455 * If we're wrong it's no big deal, we'll just do the split the right
1457 * (It may look like it's equally easy to do a similar hack for
1458 * reverse sorted data, that is, split the tree left,
1459 * but it's not. Be my guest.)
1461 if (nextbn == 0 && split->index == sp->header.nextindex) {
1462 /* linelock header + stbl (first slot) of new page */
1463 rlv = & rdtlck->lv[rdtlck->index];
1469 * initialize freelist of new right page
1472 for (fsi++; fsi < rp->header.maxslot; f++, fsi++)
1476 /* insert entry at the first entry of the new right page */
1477 dtInsertEntry(rp, 0, split->key, split->data, &rdtlck);
1483 * non-sequential insert (at possibly middle page)
1487 * update prev pointer of previous right sibling page;
1490 DT_GETPAGE(ip, nextbn, mp, PSIZE, p, rc);
1492 discard_metapage(rmp);
1496 BT_MARK_DIRTY(mp, ip);
1498 * acquire a transaction lock on the next page
1500 tlck = txLock(tid, ip, mp, tlckDTREE | tlckRELINK);
1501 jfs_info("dtSplitPage: tlck = 0x%p, ip = 0x%p, mp=0x%p",
1503 dtlck = (struct dt_lock *) & tlck->lock;
1505 /* linelock header of previous right sibling page */
1506 lv = & dtlck->lv[dtlck->index];
1511 p->header.prev = cpu_to_le64(rbn);
1517 * split the data between the split and right pages.
1519 skip = split->index;
1520 half = (PSIZE >> L2DTSLOTSIZE) >> 1; /* swag */
1524 * compute fill factor for split pages
1526 * <nxt> traces the next entry to move to rp
1527 * <off> traces the next entry to stay in sp
1529 stbl = (u8 *) & sp->slot[sp->header.stblindex];
1530 nextindex = sp->header.nextindex;
1531 for (nxt = off = 0; nxt < nextindex; ++off) {
1533 /* check for fill factor with new entry size */
1537 switch (sp->header.flag & BT_TYPE) {
1539 ldtentry = (struct ldtentry *) & sp->slot[si];
1541 n = NDTLEAF(ldtentry->namlen);
1543 n = NDTLEAF_LEGACY(ldtentry->
1548 idtentry = (struct idtentry *) & sp->slot[si];
1549 n = NDTINTERNAL(idtentry->namlen);
1556 ++nxt; /* advance to next entry to move in sp */
1564 /* <nxt> poins to the 1st entry to move */
1567 * move entries to right page
1569 * dtMoveEntry() initializes rp and reserves entry for insertion
1571 * split page moved out entries are linelocked;
1572 * new/right page moved in entries are linelocked;
1574 /* linelock header + stbl of new right page */
1575 rlv = & rdtlck->lv[rdtlck->index];
1580 dtMoveEntry(sp, nxt, rp, &sdtlck, &rdtlck, DO_INDEX(ip));
1582 sp->header.nextindex = nxt;
1585 * finalize freelist of new right page
1587 fsi = rp->header.freelist;
1589 for (fsi++; fsi < rp->header.maxslot; f++, fsi++)
1594 * Update directory index table for entries now in right page
1596 if ((rp->header.flag & BT_LEAF) && DO_INDEX(ip)) {
1600 stbl = DT_GETSTBL(rp);
1601 for (n = 0; n < rp->header.nextindex; n++) {
1602 ldtentry = (struct ldtentry *) & rp->slot[stbl[n]];
1603 modify_index(tid, ip, le32_to_cpu(ldtentry->index),
1604 rbn, n, &mp, &lblock);
1607 release_metapage(mp);
1611 * the skipped index was on the left page,
1614 /* insert the new entry in the split page */
1615 dtInsertEntry(sp, skip, split->key, split->data, &sdtlck);
1617 /* linelock stbl of split page */
1618 if (sdtlck->index >= sdtlck->maxcnt)
1619 sdtlck = (struct dt_lock *) txLinelock(sdtlck);
1620 slv = & sdtlck->lv[sdtlck->index];
1621 n = skip >> L2DTSLOTSIZE;
1622 slv->offset = sp->header.stblindex + n;
1624 ((sp->header.nextindex - 1) >> L2DTSLOTSIZE) - n + 1;
1628 * the skipped index was on the right page,
1631 /* adjust the skip index to reflect the new position */
1634 /* insert the new entry in the right page */
1635 dtInsertEntry(rp, skip, split->key, split->data, &rdtlck);
1649 * function: extend 1st/only directory leaf page
1653 * return: 0 - success;
1655 * return extended page pinned;
1657 static int dtExtendPage(tid_t tid,
1658 struct inode *ip, struct dtsplit * split, struct btstack * btstack)
1660 struct super_block *sb = ip->i_sb;
1662 struct metapage *smp, *pmp, *mp;
1664 struct pxdlist *pxdlist;
1667 int newstblindex, newstblsize;
1668 int oldstblindex, oldstblsize;
1671 struct btframe *parent;
1673 struct dt_lock *dtlck;
1676 struct pxd_lock *pxdlock;
1679 struct ldtentry *ldtentry;
1682 /* get page to extend */
1684 sp = DT_PAGE(ip, smp);
1686 /* get parent/root page */
1687 parent = BT_POP(btstack);
1688 DT_GETPAGE(ip, parent->bn, pmp, PSIZE, pp, rc);
1695 pxdlist = split->pxdlist;
1696 pxd = &pxdlist->pxd[pxdlist->npxd];
1699 xaddr = addressPXD(pxd);
1700 tpxd = &sp->header.self;
1701 txaddr = addressPXD(tpxd);
1702 /* in-place extension */
1703 if (xaddr == txaddr) {
1710 /* save moved extent descriptor for later free */
1711 tlck = txMaplock(tid, ip, tlckDTREE | tlckRELOCATE);
1712 pxdlock = (struct pxd_lock *) & tlck->lock;
1713 pxdlock->flag = mlckFREEPXD;
1714 pxdlock->pxd = sp->header.self;
1718 * Update directory index table to reflect new page address
1724 stbl = DT_GETSTBL(sp);
1725 for (n = 0; n < sp->header.nextindex; n++) {
1727 (struct ldtentry *) & sp->slot[stbl[n]];
1728 modify_index(tid, ip,
1729 le32_to_cpu(ldtentry->index),
1730 xaddr, n, &mp, &lblock);
1733 release_metapage(mp);
1740 sp->header.self = *pxd;
1742 jfs_info("dtExtendPage: ip:0x%p smp:0x%p sp:0x%p", ip, smp, sp);
1744 BT_MARK_DIRTY(smp, ip);
1746 * acquire a transaction lock on the extended/leaf page
1748 tlck = txLock(tid, ip, smp, tlckDTREE | type);
1749 dtlck = (struct dt_lock *) & tlck->lock;
1750 lv = & dtlck->lv[0];
1752 /* update buffer extent descriptor of extended page */
1753 xlen = lengthPXD(pxd);
1754 xsize = xlen << JFS_SBI(sb)->l2bsize;
1755 #ifdef _STILL_TO_PORT
1756 bmSetXD(smp, xaddr, xsize);
1757 #endif /* _STILL_TO_PORT */
1760 * copy old stbl to new stbl at start of extended area
1762 oldstblindex = sp->header.stblindex;
1763 oldstblsize = (sp->header.maxslot + 31) >> L2DTSLOTSIZE;
1764 newstblindex = sp->header.maxslot;
1765 n = xsize >> L2DTSLOTSIZE;
1766 newstblsize = (n + 31) >> L2DTSLOTSIZE;
1767 memcpy(&sp->slot[newstblindex], &sp->slot[oldstblindex],
1768 sp->header.nextindex);
1771 * in-line extension: linelock old area of extended page
1773 if (type == tlckEXTEND) {
1774 /* linelock header */
1780 /* linelock new stbl of extended page */
1781 lv->offset = newstblindex;
1782 lv->length = newstblsize;
1785 * relocation: linelock whole relocated area
1789 lv->length = sp->header.maxslot + newstblsize;
1794 sp->header.maxslot = n;
1795 sp->header.stblindex = newstblindex;
1796 /* sp->header.nextindex remains the same */
1799 * add old stbl region at head of freelist
1803 last = sp->header.freelist;
1804 for (n = 0; n < oldstblsize; n++, fsi++, f++) {
1808 sp->header.freelist = last;
1809 sp->header.freecnt += oldstblsize;
1812 * append free region of newly extended area at tail of freelist
1814 /* init free region of newly extended area */
1815 fsi = n = newstblindex + newstblsize;
1817 for (fsi++; fsi < sp->header.maxslot; f++, fsi++)
1821 /* append new free region at tail of old freelist */
1822 fsi = sp->header.freelist;
1824 sp->header.freelist = n;
1829 } while (fsi != -1);
1834 sp->header.freecnt += sp->header.maxslot - n;
1837 * insert the new entry
1839 dtInsertEntry(sp, split->index, split->key, split->data, &dtlck);
1841 BT_MARK_DIRTY(pmp, ip);
1843 * linelock any freeslots residing in old extent
1845 if (type == tlckEXTEND) {
1846 n = sp->header.maxslot >> 2;
1847 if (sp->header.freelist < n)
1848 dtLinelockFreelist(sp, n, &dtlck);
1852 * update parent entry on the parent/root page
1855 * acquire a transaction lock on the parent/root page
1857 tlck = txLock(tid, ip, pmp, tlckDTREE | tlckENTRY);
1858 dtlck = (struct dt_lock *) & tlck->lock;
1859 lv = & dtlck->lv[dtlck->index];
1861 /* linelock parent entry - 1st slot */
1866 /* update the parent pxd for page extension */
1867 tpxd = (pxd_t *) & pp->slot[1];
1879 * split the full root page into
1880 * original/root/split page and new right page
1881 * i.e., root remains fixed in tree anchor (inode) and
1882 * the root is copied to a single new right child page
1883 * since root page << non-root page, and
1884 * the split root page contains a single entry for the
1885 * new right child page.
1889 * return: 0 - success;
1891 * return new page pinned;
1893 static int dtSplitRoot(tid_t tid,
1894 struct inode *ip, struct dtsplit * split, struct metapage ** rmpp)
1896 struct super_block *sb = ip->i_sb;
1897 struct metapage *smp;
1899 struct metapage *rmp;
1906 int fsi, stblsize, n;
1909 struct pxdlist *pxdlist;
1911 struct dt_lock *dtlck;
1915 /* get split root page */
1917 sp = &JFS_IP(ip)->i_dtroot;
1920 * allocate/initialize a single (right) child page
1922 * N.B. at first split, a one (or two) block to fit new entry
1923 * is allocated; at subsequent split, a full page is allocated;
1925 pxdlist = split->pxdlist;
1926 pxd = &pxdlist->pxd[pxdlist->npxd];
1928 rbn = addressPXD(pxd);
1929 xlen = lengthPXD(pxd);
1930 xsize = xlen << JFS_SBI(sb)->l2bsize;
1931 rmp = get_metapage(ip, rbn, xsize, 1);
1937 /* Allocate blocks to quota. */
1938 if (DQUOT_ALLOC_BLOCK(ip, lengthPXD(pxd))) {
1939 release_metapage(rmp);
1942 /* Allocate blocks to dlimit. */
1943 if (DLIMIT_ALLOC_BLOCK(ip, lengthPXD(pxd))) {
1944 DQUOT_FREE_BLOCK(ip, lengthPXD(pxd));
1945 release_metapage(rmp);
1949 BT_MARK_DIRTY(rmp, ip);
1951 * acquire a transaction lock on the new right page
1953 tlck = txLock(tid, ip, rmp, tlckDTREE | tlckNEW);
1954 dtlck = (struct dt_lock *) & tlck->lock;
1957 (sp->header.flag & BT_LEAF) ? BT_LEAF : BT_INTERNAL;
1958 rp->header.self = *pxd;
1960 /* initialize sibling pointers */
1961 rp->header.next = 0;
1962 rp->header.prev = 0;
1965 * move in-line root page into new right page extent
1967 /* linelock header + copied entries + new stbl (1st slot) in new page */
1968 ASSERT(dtlck->index == 0);
1969 lv = & dtlck->lv[0];
1971 lv->length = 10; /* 1 + 8 + 1 */
1974 n = xsize >> L2DTSLOTSIZE;
1975 rp->header.maxslot = n;
1976 stblsize = (n + 31) >> L2DTSLOTSIZE;
1978 /* copy old stbl to new stbl at start of extended area */
1979 rp->header.stblindex = DTROOTMAXSLOT;
1980 stbl = (s8 *) & rp->slot[DTROOTMAXSLOT];
1981 memcpy(stbl, sp->header.stbl, sp->header.nextindex);
1982 rp->header.nextindex = sp->header.nextindex;
1984 /* copy old data area to start of new data area */
1985 memcpy(&rp->slot[1], &sp->slot[1], IDATASIZE);
1988 * append free region of newly extended area at tail of freelist
1990 /* init free region of newly extended area */
1991 fsi = n = DTROOTMAXSLOT + stblsize;
1993 for (fsi++; fsi < rp->header.maxslot; f++, fsi++)
1997 /* append new free region at tail of old freelist */
1998 fsi = sp->header.freelist;
2000 rp->header.freelist = n;
2002 rp->header.freelist = fsi;
2007 } while (fsi != -1);
2012 rp->header.freecnt = sp->header.freecnt + rp->header.maxslot - n;
2015 * Update directory index table for entries now in right page
2017 if ((rp->header.flag & BT_LEAF) && DO_INDEX(ip)) {
2019 struct metapage *mp = NULL;
2020 struct ldtentry *ldtentry;
2022 stbl = DT_GETSTBL(rp);
2023 for (n = 0; n < rp->header.nextindex; n++) {
2024 ldtentry = (struct ldtentry *) & rp->slot[stbl[n]];
2025 modify_index(tid, ip, le32_to_cpu(ldtentry->index),
2026 rbn, n, &mp, &lblock);
2029 release_metapage(mp);
2032 * insert the new entry into the new right/child page
2033 * (skip index in the new right page will not change)
2035 dtInsertEntry(rp, split->index, split->key, split->data, &dtlck);
2038 * reset parent/root page
2040 * set the 1st entry offset to 0, which force the left-most key
2041 * at any level of the tree to be less than any search key.
2043 * The btree comparison code guarantees that the left-most key on any
2044 * level of the tree is never used, so it doesn't need to be filled in.
2046 BT_MARK_DIRTY(smp, ip);
2048 * acquire a transaction lock on the root page (in-memory inode)
2050 tlck = txLock(tid, ip, smp, tlckDTREE | tlckNEW | tlckBTROOT);
2051 dtlck = (struct dt_lock *) & tlck->lock;
2054 ASSERT(dtlck->index == 0);
2055 lv = & dtlck->lv[0];
2057 lv->length = DTROOTMAXSLOT;
2060 /* update page header of root */
2061 if (sp->header.flag & BT_LEAF) {
2062 sp->header.flag &= ~BT_LEAF;
2063 sp->header.flag |= BT_INTERNAL;
2066 /* init the first entry */
2067 s = (struct idtentry *) & sp->slot[DTENTRYSTART];
2073 stbl = sp->header.stbl;
2074 stbl[0] = DTENTRYSTART;
2075 sp->header.nextindex = 1;
2078 fsi = DTENTRYSTART + 1;
2081 /* init free region of remaining area */
2082 for (fsi++; fsi < DTROOTMAXSLOT; f++, fsi++)
2086 sp->header.freelist = DTENTRYSTART + 1;
2087 sp->header.freecnt = DTROOTMAXSLOT - (DTENTRYSTART + 1);
2098 * function: delete the entry(s) referenced by a key.
2104 int dtDelete(tid_t tid,
2105 struct inode *ip, struct component_name * key, ino_t * ino, int flag)
2109 struct metapage *mp, *imp;
2112 struct btstack btstack;
2113 struct dt_lock *dtlck;
2117 struct ldtentry *ldtentry;
2119 u32 table_index, next_index;
2120 struct metapage *nmp;
2124 * search for the entry to delete:
2126 * dtSearch() returns (leaf page pinned, index at which to delete).
2128 if ((rc = dtSearch(ip, key, ino, &btstack, flag)))
2131 /* retrieve search result */
2132 DT_GETSEARCH(ip, btstack.top, bn, mp, p, index);
2135 * We need to find put the index of the next entry into the
2136 * directory index table in order to resume a readdir from this
2140 stbl = DT_GETSTBL(p);
2141 ldtentry = (struct ldtentry *) & p->slot[stbl[index]];
2142 table_index = le32_to_cpu(ldtentry->index);
2143 if (index == (p->header.nextindex - 1)) {
2145 * Last entry in this leaf page
2147 if ((p->header.flag & BT_ROOT)
2148 || (p->header.next == 0))
2151 /* Read next leaf page */
2152 DT_GETPAGE(ip, le64_to_cpu(p->header.next),
2153 nmp, PSIZE, np, rc);
2157 stbl = DT_GETSTBL(np);
2159 (struct ldtentry *) & np->
2162 le32_to_cpu(ldtentry->index);
2168 (struct ldtentry *) & p->slot[stbl[index + 1]];
2169 next_index = le32_to_cpu(ldtentry->index);
2171 free_index(tid, ip, table_index, next_index);
2174 * the leaf page becomes empty, delete the page
2176 if (p->header.nextindex == 1) {
2177 /* delete empty page */
2178 rc = dtDeleteUp(tid, ip, mp, p, &btstack);
2181 * the leaf page has other entries remaining:
2183 * delete the entry from the leaf page.
2186 BT_MARK_DIRTY(mp, ip);
2188 * acquire a transaction lock on the leaf page
2190 tlck = txLock(tid, ip, mp, tlckDTREE | tlckENTRY);
2191 dtlck = (struct dt_lock *) & tlck->lock;
2194 * Do not assume that dtlck->index will be zero. During a
2195 * rename within a directory, this transaction may have
2196 * modified this page already when adding the new entry.
2199 /* linelock header */
2200 if (dtlck->index >= dtlck->maxcnt)
2201 dtlck = (struct dt_lock *) txLinelock(dtlck);
2202 lv = & dtlck->lv[dtlck->index];
2207 /* linelock stbl of non-root leaf page */
2208 if (!(p->header.flag & BT_ROOT)) {
2209 if (dtlck->index >= dtlck->maxcnt)
2210 dtlck = (struct dt_lock *) txLinelock(dtlck);
2211 lv = & dtlck->lv[dtlck->index];
2212 i = index >> L2DTSLOTSIZE;
2213 lv->offset = p->header.stblindex + i;
2215 ((p->header.nextindex - 1) >> L2DTSLOTSIZE) -
2220 /* free the leaf entry */
2221 dtDeleteEntry(p, index, &dtlck);
2224 * Update directory index table for entries moved in stbl
2226 if (DO_INDEX(ip) && index < p->header.nextindex) {
2230 stbl = DT_GETSTBL(p);
2231 for (i = index; i < p->header.nextindex; i++) {
2233 (struct ldtentry *) & p->slot[stbl[i]];
2234 modify_index(tid, ip,
2235 le32_to_cpu(ldtentry->index),
2236 bn, i, &imp, &lblock);
2239 release_metapage(imp);
2253 * free empty pages as propagating deletion up the tree
2259 static int dtDeleteUp(tid_t tid, struct inode *ip,
2260 struct metapage * fmp, dtpage_t * fp, struct btstack * btstack)
2263 struct metapage *mp;
2265 int index, nextindex;
2267 struct btframe *parent;
2268 struct dt_lock *dtlck;
2271 struct pxd_lock *pxdlock;
2275 * keep the root leaf page which has become empty
2277 if (BT_IS_ROOT(fmp)) {
2281 * dtInitRoot() acquires txlock on the root
2283 dtInitRoot(tid, ip, PARENT(ip));
2291 * free the non-root leaf page
2294 * acquire a transaction lock on the page
2296 * write FREEXTENT|NOREDOPAGE log record
2297 * N.B. linelock is overlaid as freed extent descriptor, and
2298 * the buffer page is freed;
2300 tlck = txMaplock(tid, ip, tlckDTREE | tlckFREE);
2301 pxdlock = (struct pxd_lock *) & tlck->lock;
2302 pxdlock->flag = mlckFREEPXD;
2303 pxdlock->pxd = fp->header.self;
2306 /* update sibling pointers */
2307 if ((rc = dtRelink(tid, ip, fp))) {
2312 xlen = lengthPXD(&fp->header.self);
2314 /* Free dlimit allocation. */
2315 DLIMIT_FREE_BLOCK(ip, xlen);
2316 /* Free quota allocation. */
2317 DQUOT_FREE_BLOCK(ip, xlen);
2319 /* free/invalidate its buffer page */
2320 discard_metapage(fmp);
2323 * propagate page deletion up the directory tree
2325 * If the delete from the parent page makes it empty,
2326 * continue all the way up the tree.
2327 * stop if the root page is reached (which is never deleted) or
2328 * if the entry deletion does not empty the page.
2330 while ((parent = BT_POP(btstack)) != NULL) {
2331 /* pin the parent page <sp> */
2332 DT_GETPAGE(ip, parent->bn, mp, PSIZE, p, rc);
2337 * free the extent of the child page deleted
2339 index = parent->index;
2342 * delete the entry for the child page from parent
2344 nextindex = p->header.nextindex;
2347 * the parent has the single entry being deleted:
2349 * free the parent page which has become empty.
2351 if (nextindex == 1) {
2353 * keep the root internal page which has become empty
2355 if (p->header.flag & BT_ROOT) {
2359 * dtInitRoot() acquires txlock on the root
2361 dtInitRoot(tid, ip, PARENT(ip));
2368 * free the parent page
2372 * acquire a transaction lock on the page
2374 * write FREEXTENT|NOREDOPAGE log record
2378 tlckDTREE | tlckFREE);
2379 pxdlock = (struct pxd_lock *) & tlck->lock;
2380 pxdlock->flag = mlckFREEPXD;
2381 pxdlock->pxd = p->header.self;
2384 /* update sibling pointers */
2385 if ((rc = dtRelink(tid, ip, p))) {
2390 xlen = lengthPXD(&p->header.self);
2392 /* Free dlimit allocation */
2393 DLIMIT_FREE_BLOCK(ip, xlen);
2394 /* Free quota allocation */
2395 DQUOT_FREE_BLOCK(ip, xlen);
2397 /* free/invalidate its buffer page */
2398 discard_metapage(mp);
2406 * the parent has other entries remaining:
2408 * delete the router entry from the parent page.
2410 BT_MARK_DIRTY(mp, ip);
2412 * acquire a transaction lock on the page
2414 * action: router entry deletion
2416 tlck = txLock(tid, ip, mp, tlckDTREE | tlckENTRY);
2417 dtlck = (struct dt_lock *) & tlck->lock;
2419 /* linelock header */
2420 if (dtlck->index >= dtlck->maxcnt)
2421 dtlck = (struct dt_lock *) txLinelock(dtlck);
2422 lv = & dtlck->lv[dtlck->index];
2427 /* linelock stbl of non-root leaf page */
2428 if (!(p->header.flag & BT_ROOT)) {
2429 if (dtlck->index < dtlck->maxcnt)
2432 dtlck = (struct dt_lock *) txLinelock(dtlck);
2433 lv = & dtlck->lv[0];
2435 i = index >> L2DTSLOTSIZE;
2436 lv->offset = p->header.stblindex + i;
2438 ((p->header.nextindex - 1) >> L2DTSLOTSIZE) -
2443 /* free the router entry */
2444 dtDeleteEntry(p, index, &dtlck);
2446 /* reset key of new leftmost entry of level (for consistency) */
2448 ((p->header.flag & BT_ROOT) || p->header.prev == 0))
2449 dtTruncateEntry(p, 0, &dtlck);
2451 /* unpin the parent page */
2454 /* exit propagation up */
2463 * NAME: dtRelocate()
2465 * FUNCTION: relocate dtpage (internal or leaf) of directory;
2466 * This function is mainly used by defragfs utility.
2468 int dtRelocate(tid_t tid, struct inode *ip, s64 lmxaddr, pxd_t * opxd,
2472 struct metapage *mp, *pmp, *lmp, *rmp;
2473 dtpage_t *p, *pp, *rp = 0, *lp= 0;
2476 struct btstack btstack;
2478 s64 oxaddr, nextbn, prevbn;
2481 struct dt_lock *dtlck;
2482 struct pxd_lock *pxdlock;
2486 oxaddr = addressPXD(opxd);
2487 xlen = lengthPXD(opxd);
2489 jfs_info("dtRelocate: lmxaddr:%Ld xaddr:%Ld:%Ld xlen:%d",
2490 (long long)lmxaddr, (long long)oxaddr, (long long)nxaddr,
2494 * 1. get the internal parent dtpage covering
2495 * router entry for the tartget page to be relocated;
2497 rc = dtSearchNode(ip, lmxaddr, opxd, &btstack);
2501 /* retrieve search result */
2502 DT_GETSEARCH(ip, btstack.top, bn, pmp, pp, index);
2503 jfs_info("dtRelocate: parent router entry validated.");
2506 * 2. relocate the target dtpage
2508 /* read in the target page from src extent */
2509 DT_GETPAGE(ip, oxaddr, mp, PSIZE, p, rc);
2511 /* release the pinned parent page */
2517 * read in sibling pages if any to update sibling pointers;
2520 if (p->header.next) {
2521 nextbn = le64_to_cpu(p->header.next);
2522 DT_GETPAGE(ip, nextbn, rmp, PSIZE, rp, rc);
2531 if (p->header.prev) {
2532 prevbn = le64_to_cpu(p->header.prev);
2533 DT_GETPAGE(ip, prevbn, lmp, PSIZE, lp, rc);
2543 /* at this point, all xtpages to be updated are in memory */
2546 * update sibling pointers of sibling dtpages if any;
2549 tlck = txLock(tid, ip, lmp, tlckDTREE | tlckRELINK);
2550 dtlck = (struct dt_lock *) & tlck->lock;
2551 /* linelock header */
2552 ASSERT(dtlck->index == 0);
2553 lv = & dtlck->lv[0];
2558 lp->header.next = cpu_to_le64(nxaddr);
2563 tlck = txLock(tid, ip, rmp, tlckDTREE | tlckRELINK);
2564 dtlck = (struct dt_lock *) & tlck->lock;
2565 /* linelock header */
2566 ASSERT(dtlck->index == 0);
2567 lv = & dtlck->lv[0];
2572 rp->header.prev = cpu_to_le64(nxaddr);
2577 * update the target dtpage to be relocated
2579 * write LOG_REDOPAGE of LOG_NEW type for dst page
2580 * for the whole target page (logredo() will apply
2581 * after image and update bmap for allocation of the
2582 * dst extent), and update bmap for allocation of
2585 tlck = txLock(tid, ip, mp, tlckDTREE | tlckNEW);
2586 dtlck = (struct dt_lock *) & tlck->lock;
2587 /* linelock header */
2588 ASSERT(dtlck->index == 0);
2589 lv = & dtlck->lv[0];
2591 /* update the self address in the dtpage header */
2592 pxd = &p->header.self;
2593 PXDaddress(pxd, nxaddr);
2595 /* the dst page is the same as the src page, i.e.,
2596 * linelock for afterimage of the whole page;
2599 lv->length = p->header.maxslot;
2602 /* update the buffer extent descriptor of the dtpage */
2603 xsize = xlen << JFS_SBI(ip->i_sb)->l2bsize;
2604 #ifdef _STILL_TO_PORT
2605 bmSetXD(mp, nxaddr, xsize);
2606 #endif /* _STILL_TO_PORT */
2607 /* unpin the relocated page */
2609 jfs_info("dtRelocate: target dtpage relocated.");
2611 /* the moved extent is dtpage, then a LOG_NOREDOPAGE log rec
2612 * needs to be written (in logredo(), the LOG_NOREDOPAGE log rec
2613 * will also force a bmap update ).
2617 * 3. acquire maplock for the source extent to be freed;
2619 /* for dtpage relocation, write a LOG_NOREDOPAGE record
2620 * for the source dtpage (logredo() will init NoRedoPage
2621 * filter and will also update bmap for free of the source
2622 * dtpage), and upadte bmap for free of the source dtpage;
2624 tlck = txMaplock(tid, ip, tlckDTREE | tlckFREE);
2625 pxdlock = (struct pxd_lock *) & tlck->lock;
2626 pxdlock->flag = mlckFREEPXD;
2627 PXDaddress(&pxdlock->pxd, oxaddr);
2628 PXDlength(&pxdlock->pxd, xlen);
2632 * 4. update the parent router entry for relocation;
2634 * acquire tlck for the parent entry covering the target dtpage;
2635 * write LOG_REDOPAGE to apply after image only;
2637 jfs_info("dtRelocate: update parent router entry.");
2638 tlck = txLock(tid, ip, pmp, tlckDTREE | tlckENTRY);
2639 dtlck = (struct dt_lock *) & tlck->lock;
2640 lv = & dtlck->lv[dtlck->index];
2642 /* update the PXD with the new address */
2643 stbl = DT_GETSTBL(pp);
2644 pxd = (pxd_t *) & pp->slot[stbl[index]];
2645 PXDaddress(pxd, nxaddr);
2646 lv->offset = stbl[index];
2650 /* unpin the parent dtpage */
2657 * NAME: dtSearchNode()
2659 * FUNCTION: Search for an dtpage containing a specified address
2660 * This function is mainly used by defragfs utility.
2662 * NOTE: Search result on stack, the found page is pinned at exit.
2663 * The result page must be an internal dtpage.
2664 * lmxaddr give the address of the left most page of the
2665 * dtree level, in which the required dtpage resides.
2667 static int dtSearchNode(struct inode *ip, s64 lmxaddr, pxd_t * kpxd,
2668 struct btstack * btstack)
2672 struct metapage *mp;
2674 int psize = 288; /* initial in-line directory */
2678 struct btframe *btsp;
2680 BT_CLR(btstack); /* reset stack */
2683 * descend tree to the level with specified leftmost page
2685 * by convention, root bn = 0.
2688 /* get/pin the page to search */
2689 DT_GETPAGE(ip, bn, mp, psize, p, rc);
2693 /* does the xaddr of leftmost page of the levevl
2694 * matches levevl search key ?
2696 if (p->header.flag & BT_ROOT) {
2699 } else if (addressPXD(&p->header.self) == lmxaddr)
2703 * descend down to leftmost child page
2705 if (p->header.flag & BT_LEAF) {
2710 /* get the leftmost entry */
2711 stbl = DT_GETSTBL(p);
2712 pxd = (pxd_t *) & p->slot[stbl[0]];
2714 /* get the child page block address */
2715 bn = addressPXD(pxd);
2716 psize = lengthPXD(pxd) << JFS_SBI(ip->i_sb)->l2bsize;
2717 /* unpin the parent page */
2722 * search each page at the current levevl
2725 stbl = DT_GETSTBL(p);
2726 for (i = 0; i < p->header.nextindex; i++) {
2727 pxd = (pxd_t *) & p->slot[stbl[i]];
2729 /* found the specified router entry */
2730 if (addressPXD(pxd) == addressPXD(kpxd) &&
2731 lengthPXD(pxd) == lengthPXD(kpxd)) {
2732 btsp = btstack->top;
2741 /* get the right sibling page if any */
2743 bn = le64_to_cpu(p->header.next);
2749 /* unpin current page */
2752 /* get the right sibling page */
2753 DT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
2759 #endif /* _NOTYET */
2765 * link around a freed page.
2768 * fp: page to be freed
2772 static int dtRelink(tid_t tid, struct inode *ip, dtpage_t * p)
2775 struct metapage *mp;
2778 struct dt_lock *dtlck;
2781 nextbn = le64_to_cpu(p->header.next);
2782 prevbn = le64_to_cpu(p->header.prev);
2784 /* update prev pointer of the next page */
2786 DT_GETPAGE(ip, nextbn, mp, PSIZE, p, rc);
2790 BT_MARK_DIRTY(mp, ip);
2792 * acquire a transaction lock on the next page
2794 * action: update prev pointer;
2796 tlck = txLock(tid, ip, mp, tlckDTREE | tlckRELINK);
2797 jfs_info("dtRelink nextbn: tlck = 0x%p, ip = 0x%p, mp=0x%p",
2799 dtlck = (struct dt_lock *) & tlck->lock;
2801 /* linelock header */
2802 if (dtlck->index >= dtlck->maxcnt)
2803 dtlck = (struct dt_lock *) txLinelock(dtlck);
2804 lv = & dtlck->lv[dtlck->index];
2809 p->header.prev = cpu_to_le64(prevbn);
2813 /* update next pointer of the previous page */
2815 DT_GETPAGE(ip, prevbn, mp, PSIZE, p, rc);
2819 BT_MARK_DIRTY(mp, ip);
2821 * acquire a transaction lock on the prev page
2823 * action: update next pointer;
2825 tlck = txLock(tid, ip, mp, tlckDTREE | tlckRELINK);
2826 jfs_info("dtRelink prevbn: tlck = 0x%p, ip = 0x%p, mp=0x%p",
2828 dtlck = (struct dt_lock *) & tlck->lock;
2830 /* linelock header */
2831 if (dtlck->index >= dtlck->maxcnt)
2832 dtlck = (struct dt_lock *) txLinelock(dtlck);
2833 lv = & dtlck->lv[dtlck->index];
2838 p->header.next = cpu_to_le64(nextbn);
2849 * initialize directory root (inline in inode)
2851 void dtInitRoot(tid_t tid, struct inode *ip, u32 idotdot)
2853 struct jfs_inode_info *jfs_ip = JFS_IP(ip);
2858 struct dt_lock *dtlck;
2863 * If this was previously an non-empty directory, we need to remove
2864 * the old directory table.
2867 if (!jfs_dirtable_inline(ip)) {
2868 struct tblock *tblk = tid_to_tblock(tid);
2870 * We're playing games with the tid's xflag. If
2871 * we're removing a regular file, the file's xtree
2872 * is committed with COMMIT_PMAP, but we always
2873 * commit the directories xtree with COMMIT_PWMAP.
2875 xflag_save = tblk->xflag;
2878 * xtTruncate isn't guaranteed to fully truncate
2879 * the xtree. The caller needs to check i_size
2880 * after committing the transaction to see if
2881 * additional truncation is needed. The
2882 * COMMIT_Stale flag tells caller that we
2883 * initiated the truncation.
2885 xtTruncate(tid, ip, 0, COMMIT_PWMAP);
2886 set_cflag(COMMIT_Stale, ip);
2888 tblk->xflag = xflag_save;
2892 jfs_ip->next_index = 2;
2894 ip->i_size = IDATASIZE;
2897 * acquire a transaction lock on the root
2899 * action: directory initialization;
2901 tlck = txLock(tid, ip, (struct metapage *) & jfs_ip->bxflag,
2902 tlckDTREE | tlckENTRY | tlckBTROOT);
2903 dtlck = (struct dt_lock *) & tlck->lock;
2906 ASSERT(dtlck->index == 0);
2907 lv = & dtlck->lv[0];
2909 lv->length = DTROOTMAXSLOT;
2912 p = &jfs_ip->i_dtroot;
2914 p->header.flag = DXD_INDEX | BT_ROOT | BT_LEAF;
2916 p->header.nextindex = 0;
2922 /* init data area of root */
2923 for (fsi++; fsi < DTROOTMAXSLOT; f++, fsi++)
2927 p->header.freelist = 1;
2928 p->header.freecnt = 8;
2930 /* init '..' entry */
2931 p->header.idotdot = cpu_to_le32(idotdot);
2937 * add_missing_indices()
2939 * function: Fix dtree page in which one or more entries has an invalid index.
2940 * fsck.jfs should really fix this, but it currently does not.
2941 * Called from jfs_readdir when bad index is detected.
2943 static void add_missing_indices(struct inode *inode, s64 bn)
2946 struct dt_lock *dtlck;
2950 struct metapage *mp;
2957 tid = txBegin(inode->i_sb, 0);
2959 DT_GETPAGE(inode, bn, mp, PSIZE, p, rc);
2962 printk(KERN_ERR "DT_GETPAGE failed!\n");
2965 BT_MARK_DIRTY(mp, inode);
2967 ASSERT(p->header.flag & BT_LEAF);
2969 tlck = txLock(tid, inode, mp, tlckDTREE | tlckENTRY);
2971 tlck->type |= tlckBTROOT;
2973 dtlck = (struct dt_lock *) &tlck->lock;
2975 stbl = DT_GETSTBL(p);
2976 for (i = 0; i < p->header.nextindex; i++) {
2977 d = (struct ldtentry *) &p->slot[stbl[i]];
2978 index = le32_to_cpu(d->index);
2979 if ((index < 2) || (index >= JFS_IP(inode)->next_index)) {
2980 d->index = cpu_to_le32(add_index(tid, inode, bn, i));
2981 if (dtlck->index >= dtlck->maxcnt)
2982 dtlck = (struct dt_lock *) txLinelock(dtlck);
2983 lv = &dtlck->lv[dtlck->index];
2984 lv->offset = stbl[i];
2991 (void) txCommit(tid, 1, &inode, 0);
2997 * Buffer to hold directory entry info while traversing a dtree page
2998 * before being fed to the filldir function
3008 * function to determine next variable-sized jfs_dirent in buffer
3010 static inline struct jfs_dirent *next_jfs_dirent(struct jfs_dirent *dirent)
3012 return (struct jfs_dirent *)
3014 ((sizeof (struct jfs_dirent) + dirent->name_len + 1 +
3015 sizeof (loff_t) - 1) &
3016 ~(sizeof (loff_t) - 1)));
3022 * function: read directory entries sequentially
3023 * from the specified entry offset
3027 * return: offset = (pn, index) of start entry
3028 * of next jfs_readdir()/dtRead()
3030 int jfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
3032 struct inode *ip = filp->f_dentry->d_inode;
3033 struct nls_table *codepage = JFS_SBI(ip->i_sb)->nls_tab;
3035 loff_t dtpos; /* legacy OS/2 style position */
3040 } *dtoffset = (struct dtoffset *) &dtpos;
3042 struct metapage *mp;
3046 struct btstack btstack;
3050 int d_namleft, len, outlen;
3051 unsigned long dirent_buf;
3055 uint loop_count = 0;
3056 struct jfs_dirent *jfs_dirent;
3058 int overflow, fix_page, page_fixed = 0;
3059 static int unique_pos = 2; /* If we can't fix broken index */
3061 if (filp->f_pos == DIREND)
3066 * persistent index is stored in directory entries.
3067 * Special cases: 0 = .
3069 * -1 = End of directory
3073 dir_index = (u32) filp->f_pos;
3075 if (dir_index > 1) {
3076 struct dir_table_slot dirtab_slot;
3079 (dir_index >= JFS_IP(ip)->next_index)) {
3080 /* Stale position. Directory has shrunk */
3081 filp->f_pos = DIREND;
3085 rc = read_index(ip, dir_index, &dirtab_slot);
3087 filp->f_pos = DIREND;
3090 if (dirtab_slot.flag == DIR_INDEX_FREE) {
3091 if (loop_count++ > JFS_IP(ip)->next_index) {
3092 jfs_err("jfs_readdir detected "
3094 filp->f_pos = DIREND;
3097 dir_index = le32_to_cpu(dirtab_slot.addr2);
3098 if (dir_index == -1) {
3099 filp->f_pos = DIREND;
3104 bn = addressDTS(&dirtab_slot);
3105 index = dirtab_slot.slot;
3106 DT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
3108 filp->f_pos = DIREND;
3111 if (p->header.flag & BT_INTERNAL) {
3112 jfs_err("jfs_readdir: bad index table");
3118 if (dir_index == 0) {
3123 if (filldir(dirent, ".", 1, 0, ip->i_ino,
3131 if (filldir(dirent, "..", 2, 1, PARENT(ip), DT_DIR))
3135 * Find first entry of left-most leaf
3138 filp->f_pos = DIREND;
3142 if ((rc = dtReadFirst(ip, &btstack)))
3145 DT_GETSEARCH(ip, btstack.top, bn, mp, p, index);
3149 * Legacy filesystem - OS/2 & Linux JFS < 0.3.6
3151 * pn = index = 0: First entry "."
3152 * pn = 0; index = 1: Second entry ".."
3153 * pn > 0: Real entries, pn=1 -> leftmost page
3154 * pn = index = -1: No more entries
3156 dtpos = filp->f_pos;
3158 /* build "." entry */
3160 if (filldir(dirent, ".", 1, filp->f_pos, ip->i_ino,
3163 dtoffset->index = 1;
3164 filp->f_pos = dtpos;
3167 if (dtoffset->pn == 0) {
3168 if (dtoffset->index == 1) {
3169 /* build ".." entry */
3171 if (filldir(dirent, "..", 2, filp->f_pos,
3172 PARENT(ip), DT_DIR))
3175 jfs_err("jfs_readdir called with "
3179 dtoffset->index = 0;
3180 filp->f_pos = dtpos;
3184 filp->f_pos = DIREND;
3188 if ((rc = dtReadNext(ip, &filp->f_pos, &btstack))) {
3189 jfs_err("jfs_readdir: unexpected rc = %d "
3190 "from dtReadNext", rc);
3191 filp->f_pos = DIREND;
3194 /* get start leaf page and index */
3195 DT_GETSEARCH(ip, btstack.top, bn, mp, p, index);
3197 /* offset beyond directory eof ? */
3199 filp->f_pos = DIREND;
3204 dirent_buf = __get_free_page(GFP_KERNEL);
3205 if (dirent_buf == 0) {
3207 jfs_warn("jfs_readdir: __get_free_page failed!");
3208 filp->f_pos = DIREND;
3213 jfs_dirent = (struct jfs_dirent *) dirent_buf;
3215 overflow = fix_page = 0;
3217 stbl = DT_GETSTBL(p);
3219 for (i = index; i < p->header.nextindex; i++) {
3220 d = (struct ldtentry *) & p->slot[stbl[i]];
3222 if (((long) jfs_dirent + d->namlen + 1) >
3223 (dirent_buf + PAGE_SIZE)) {
3224 /* DBCS codepages could overrun dirent_buf */
3230 d_namleft = d->namlen;
3231 name_ptr = jfs_dirent->name;
3232 jfs_dirent->ino = le32_to_cpu(d->inumber);
3235 len = min(d_namleft, DTLHDRDATALEN);
3236 jfs_dirent->position = le32_to_cpu(d->index);
3238 * d->index should always be valid, but it
3239 * isn't. fsck.jfs doesn't create the
3240 * directory index for the lost+found
3241 * directory. Rather than let it go,
3242 * we can try to fix it.
3244 if ((jfs_dirent->position < 2) ||
3245 (jfs_dirent->position >=
3246 JFS_IP(ip)->next_index)) {
3247 if (!page_fixed && !isReadOnly(ip)) {
3250 * setting overflow and setting
3251 * index to i will cause the
3252 * same page to be processed
3253 * again starting here
3259 jfs_dirent->position = unique_pos++;
3262 jfs_dirent->position = dtpos;
3263 len = min(d_namleft, DTLHDRDATALEN_LEGACY);
3266 /* copy the name of head/only segment */
3267 outlen = jfs_strfromUCS_le(name_ptr, d->name, len,
3269 jfs_dirent->name_len = outlen;
3271 /* copy name in the additional segment(s) */
3274 t = (struct dtslot *) & p->slot[next];
3278 if (d_namleft == 0) {
3280 "JFS:Dtree error: ino = "
3281 "%ld, bn=%Ld, index = %d",
3287 len = min(d_namleft, DTSLOTDATALEN);
3288 outlen = jfs_strfromUCS_le(name_ptr, t->name,
3290 jfs_dirent->name_len += outlen;
3296 jfs_dirent = next_jfs_dirent(jfs_dirent);
3303 /* Point to next leaf page */
3304 if (p->header.flag & BT_ROOT)
3307 bn = le64_to_cpu(p->header.next);
3309 /* update offset (pn:index) for new page */
3312 dtoffset->index = 0;
3318 /* unpin previous leaf page */
3321 jfs_dirent = (struct jfs_dirent *) dirent_buf;
3322 while (jfs_dirents--) {
3323 filp->f_pos = jfs_dirent->position;
3324 if (filldir(dirent, jfs_dirent->name,
3325 jfs_dirent->name_len, filp->f_pos,
3326 jfs_dirent->ino, DT_UNKNOWN))
3328 jfs_dirent = next_jfs_dirent(jfs_dirent);
3332 add_missing_indices(ip, bn);
3336 if (!overflow && (bn == 0)) {
3337 filp->f_pos = DIREND;
3341 DT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
3343 free_page(dirent_buf);
3349 free_page(dirent_buf);
3358 * function: get the leftmost page of the directory
3360 static int dtReadFirst(struct inode *ip, struct btstack * btstack)
3364 int psize = 288; /* initial in-line directory */
3365 struct metapage *mp;
3368 struct btframe *btsp;
3371 BT_CLR(btstack); /* reset stack */
3374 * descend leftmost path of the tree
3376 * by convention, root bn = 0.
3379 DT_GETPAGE(ip, bn, mp, psize, p, rc);
3384 * leftmost leaf page
3386 if (p->header.flag & BT_LEAF) {
3387 /* return leftmost entry */
3388 btsp = btstack->top;
3397 * descend down to leftmost child page
3399 if (BT_STACK_FULL(btstack)) {
3401 jfs_error(ip->i_sb, "dtReadFirst: btstack overrun");
3402 BT_STACK_DUMP(btstack);
3405 /* push (bn, index) of the parent page/entry */
3406 BT_PUSH(btstack, bn, 0);
3408 /* get the leftmost entry */
3409 stbl = DT_GETSTBL(p);
3410 xd = (pxd_t *) & p->slot[stbl[0]];
3412 /* get the child page block address */
3413 bn = addressPXD(xd);
3414 psize = lengthPXD(xd) << JFS_SBI(ip->i_sb)->l2bsize;
3416 /* unpin the parent page */
3425 * function: get the page of the specified offset (pn:index)
3427 * return: if (offset > eof), bn = -1;
3429 * note: if index > nextindex of the target leaf page,
3430 * start with 1st entry of next leaf page;
3432 static int dtReadNext(struct inode *ip, loff_t * offset,
3433 struct btstack * btstack)
3440 } *dtoffset = (struct dtoffset *) offset;
3442 struct metapage *mp;
3447 struct btframe *btsp, *parent;
3451 * get leftmost leaf page pinned
3453 if ((rc = dtReadFirst(ip, btstack)))
3457 DT_GETSEARCH(ip, btstack->top, bn, mp, p, index);
3459 /* get the start offset (pn:index) */
3460 pn = dtoffset->pn - 1; /* Now pn = 0 represents leftmost leaf */
3461 index = dtoffset->index;
3463 /* start at leftmost page ? */
3465 /* offset beyond eof ? */
3466 if (index < p->header.nextindex)
3469 if (p->header.flag & BT_ROOT) {
3474 /* start with 1st entry of next leaf page */
3476 dtoffset->index = index = 0;
3480 /* start at non-leftmost page: scan parent pages for large pn */
3481 if (p->header.flag & BT_ROOT) {
3486 /* start after next leaf page ? */
3490 /* get leaf page pn = 1 */
3492 bn = le64_to_cpu(p->header.next);
3494 /* unpin leaf page */
3497 /* offset beyond eof ? */
3506 * scan last internal page level to get target leaf page
3509 /* unpin leftmost leaf page */
3512 /* get left most parent page */
3513 btsp = btstack->top;
3516 DT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
3520 /* scan parent pages at last internal page level */
3521 while (pn >= p->header.nextindex) {
3522 pn -= p->header.nextindex;
3524 /* get next parent page address */
3525 bn = le64_to_cpu(p->header.next);
3527 /* unpin current parent page */
3530 /* offset beyond eof ? */
3536 /* get next parent page */
3537 DT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
3541 /* update parent page stack frame */
3545 /* get leaf page address */
3546 stbl = DT_GETSTBL(p);
3547 xd = (pxd_t *) & p->slot[stbl[pn]];
3548 bn = addressPXD(xd);
3550 /* unpin parent page */
3554 * get target leaf page
3557 DT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
3562 * leaf page has been completed:
3563 * start with 1st entry of next leaf page
3565 if (index >= p->header.nextindex) {
3566 bn = le64_to_cpu(p->header.next);
3568 /* unpin leaf page */
3571 /* offset beyond eof ? */
3577 /* get next leaf page */
3578 DT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
3582 /* start with 1st entry of next leaf page */
3584 dtoffset->index = 0;
3588 /* return target leaf page pinned */
3589 btsp = btstack->top;
3591 btsp->index = dtoffset->index;
3601 * function: compare search key with an internal entry
3604 * < 0 if k is < record
3605 * = 0 if k is = record
3606 * > 0 if k is > record
3608 static int dtCompare(struct component_name * key, /* search key */
3609 dtpage_t * p, /* directory page */
3611 { /* entry slot index */
3614 int klen, namlen, len, rc;
3615 struct idtentry *ih;
3619 * force the left-most key on internal pages, at any level of
3620 * the tree, to be less than any search key.
3621 * this obviates having to update the leftmost key on an internal
3622 * page when the user inserts a new key in the tree smaller than
3623 * anything that has been stored.
3625 * (? if/when dtSearch() narrows down to 1st entry (index = 0),
3626 * at any internal page at any level of the tree,
3627 * it descends to child of the entry anyway -
3628 * ? make the entry as min size dummy entry)
3630 * if (e->index == 0 && h->prevpg == P_INVALID && !(h->flags & BT_LEAF))
3637 ih = (struct idtentry *) & p->slot[si];
3640 namlen = ih->namlen;
3641 len = min(namlen, DTIHDRDATALEN);
3643 /* compare with head/only segment */
3644 len = min(klen, len);
3645 if ((rc = UniStrncmp_le(kname, name, len)))
3651 /* compare with additional segment(s) */
3653 while (klen > 0 && namlen > 0) {
3654 /* compare with next name segment */
3655 t = (struct dtslot *) & p->slot[si];
3656 len = min(namlen, DTSLOTDATALEN);
3657 len = min(klen, len);
3659 if ((rc = UniStrncmp_le(kname, name, len)))
3668 return (klen - namlen);
3677 * function: compare search key with an (leaf/internal) entry
3680 * < 0 if k is < record
3681 * = 0 if k is = record
3682 * > 0 if k is > record
3684 static int ciCompare(struct component_name * key, /* search key */
3685 dtpage_t * p, /* directory page */
3686 int si, /* entry slot index */
3691 int klen, namlen, len, rc;
3692 struct ldtentry *lh;
3693 struct idtentry *ih;
3698 * force the left-most key on internal pages, at any level of
3699 * the tree, to be less than any search key.
3700 * this obviates having to update the leftmost key on an internal
3701 * page when the user inserts a new key in the tree smaller than
3702 * anything that has been stored.
3704 * (? if/when dtSearch() narrows down to 1st entry (index = 0),
3705 * at any internal page at any level of the tree,
3706 * it descends to child of the entry anyway -
3707 * ? make the entry as min size dummy entry)
3709 * if (e->index == 0 && h->prevpg == P_INVALID && !(h->flags & BT_LEAF))
3719 if (p->header.flag & BT_LEAF) {
3720 lh = (struct ldtentry *) & p->slot[si];
3723 namlen = lh->namlen;
3724 if (flag & JFS_DIR_INDEX)
3725 len = min(namlen, DTLHDRDATALEN);
3727 len = min(namlen, DTLHDRDATALEN_LEGACY);
3730 * internal page entry
3733 ih = (struct idtentry *) & p->slot[si];
3736 namlen = ih->namlen;
3737 len = min(namlen, DTIHDRDATALEN);
3740 /* compare with head/only segment */
3741 len = min(klen, len);
3742 for (i = 0; i < len; i++, kname++, name++) {
3743 /* only uppercase if case-insensitive support is on */
3744 if ((flag & JFS_OS2) == JFS_OS2)
3745 x = UniToupper(le16_to_cpu(*name));
3747 x = le16_to_cpu(*name);
3748 if ((rc = *kname - x))
3755 /* compare with additional segment(s) */
3756 while (klen > 0 && namlen > 0) {
3757 /* compare with next name segment */
3758 t = (struct dtslot *) & p->slot[si];
3759 len = min(namlen, DTSLOTDATALEN);
3760 len = min(klen, len);
3762 for (i = 0; i < len; i++, kname++, name++) {
3763 /* only uppercase if case-insensitive support is on */
3764 if ((flag & JFS_OS2) == JFS_OS2)
3765 x = UniToupper(le16_to_cpu(*name));
3767 x = le16_to_cpu(*name);
3769 if ((rc = *kname - x))
3778 return (klen - namlen);
3783 * ciGetLeafPrefixKey()
3785 * function: compute prefix of suffix compression
3786 * from two adjacent leaf entries
3787 * across page boundary
3789 * return: non-zero on error
3792 static int ciGetLeafPrefixKey(dtpage_t * lp, int li, dtpage_t * rp,
3793 int ri, struct component_name * key, int flag)
3796 wchar_t *pl, *pr, *kname;
3797 struct component_name lkey;
3798 struct component_name rkey;
3800 lkey.name = (wchar_t *) kmalloc((JFS_NAME_MAX + 1) * sizeof(wchar_t),
3802 if (lkey.name == NULL)
3805 rkey.name = (wchar_t *) kmalloc((JFS_NAME_MAX + 1) * sizeof(wchar_t),
3807 if (rkey.name == NULL) {
3812 /* get left and right key */
3813 dtGetKey(lp, li, &lkey, flag);
3814 lkey.name[lkey.namlen] = 0;
3816 if ((flag & JFS_OS2) == JFS_OS2)
3819 dtGetKey(rp, ri, &rkey, flag);
3820 rkey.name[rkey.namlen] = 0;
3823 if ((flag & JFS_OS2) == JFS_OS2)
3826 /* compute prefix */
3829 namlen = min(lkey.namlen, rkey.namlen);
3830 for (pl = lkey.name, pr = rkey.name;
3831 namlen; pl++, pr++, namlen--, klen++, kname++) {
3834 key->namlen = klen + 1;
3839 /* l->namlen <= r->namlen since l <= r */
3840 if (lkey.namlen < rkey.namlen) {
3842 key->namlen = klen + 1;
3843 } else /* l->namelen == r->namelen */
3857 * function: get key of the entry
3859 static void dtGetKey(dtpage_t * p, int i, /* entry index */
3860 struct component_name * key, int flag)
3864 struct ldtentry *lh;
3865 struct idtentry *ih;
3872 stbl = DT_GETSTBL(p);
3874 if (p->header.flag & BT_LEAF) {
3875 lh = (struct ldtentry *) & p->slot[si];
3877 namlen = lh->namlen;
3879 if (flag & JFS_DIR_INDEX)
3880 len = min(namlen, DTLHDRDATALEN);
3882 len = min(namlen, DTLHDRDATALEN_LEGACY);
3884 ih = (struct idtentry *) & p->slot[si];
3886 namlen = ih->namlen;
3888 len = min(namlen, DTIHDRDATALEN);
3891 key->namlen = namlen;
3895 * move head/only segment
3897 UniStrncpy_from_le(kname, name, len);
3900 * move additional segment(s)
3903 /* get next segment */
3907 len = min(namlen, DTSLOTDATALEN);
3908 UniStrncpy_from_le(kname, t->name, len);
3918 * function: allocate free slot(s) and
3919 * write a leaf/internal entry
3921 * return: entry slot index
3923 static void dtInsertEntry(dtpage_t * p, int index, struct component_name * key,
3924 ddata_t * data, struct dt_lock ** dtlock)
3926 struct dtslot *h, *t;
3927 struct ldtentry *lh = NULL;
3928 struct idtentry *ih = NULL;
3929 int hsi, fsi, klen, len, nextindex;
3934 struct dt_lock *dtlck = *dtlock;
3938 struct metapage *mp = NULL;
3943 /* allocate a free slot */
3944 hsi = fsi = p->header.freelist;
3946 p->header.freelist = h->next;
3947 --p->header.freecnt;
3949 /* open new linelock */
3950 if (dtlck->index >= dtlck->maxcnt)
3951 dtlck = (struct dt_lock *) txLinelock(dtlck);
3953 lv = & dtlck->lv[dtlck->index];
3956 /* write head/only segment */
3957 if (p->header.flag & BT_LEAF) {
3958 lh = (struct ldtentry *) h;
3960 lh->inumber = cpu_to_le32(data->leaf.ino);
3963 if (data->leaf.ip) {
3964 len = min(klen, DTLHDRDATALEN);
3965 if (!(p->header.flag & BT_ROOT))
3966 bn = addressPXD(&p->header.self);
3967 lh->index = cpu_to_le32(add_index(data->leaf.tid,
3971 len = min(klen, DTLHDRDATALEN_LEGACY);
3973 ih = (struct idtentry *) h;
3979 len = min(klen, DTIHDRDATALEN);
3982 UniStrncpy_to_le(name, kname, len);
3987 /* write additional segment(s) */
3992 fsi = p->header.freelist;
3994 p->header.freelist = t->next;
3995 --p->header.freecnt;
3997 /* is next slot contiguous ? */
3998 if (fsi != xsi + 1) {
3999 /* close current linelock */
4003 /* open new linelock */
4004 if (dtlck->index < dtlck->maxcnt)
4007 dtlck = (struct dt_lock *) txLinelock(dtlck);
4008 lv = & dtlck->lv[0];
4016 len = min(klen, DTSLOTDATALEN);
4017 UniStrncpy_to_le(t->name, kname, len);
4024 /* close current linelock */
4030 /* terminate last/only segment */
4032 /* single segment entry */
4033 if (p->header.flag & BT_LEAF)
4038 /* multi-segment entry */
4041 /* if insert into middle, shift right succeeding entries in stbl */
4042 stbl = DT_GETSTBL(p);
4043 nextindex = p->header.nextindex;
4044 if (index < nextindex) {
4045 memmove(stbl + index + 1, stbl + index, nextindex - index);
4047 if ((p->header.flag & BT_LEAF) && data->leaf.ip) {
4051 * Need to update slot number for entries that moved
4055 for (n = index + 1; n <= nextindex; n++) {
4056 lh = (struct ldtentry *) & (p->slot[stbl[n]]);
4057 modify_index(data->leaf.tid, data->leaf.ip,
4058 le32_to_cpu(lh->index), bn, n,
4062 release_metapage(mp);
4068 /* advance next available entry index of stbl */
4069 ++p->header.nextindex;
4076 * function: move entries from split/left page to new/right page
4078 * nextindex of dst page and freelist/freecnt of both pages
4081 static void dtMoveEntry(dtpage_t * sp, int si, dtpage_t * dp,
4082 struct dt_lock ** sdtlock, struct dt_lock ** ddtlock,
4085 int ssi, next; /* src slot index */
4086 int di; /* dst entry index */
4087 int dsi; /* dst slot index */
4088 s8 *sstbl, *dstbl; /* sorted entry table */
4090 struct ldtentry *slh, *dlh = NULL;
4091 struct idtentry *sih, *dih = NULL;
4092 struct dtslot *h, *s, *d;
4093 struct dt_lock *sdtlck = *sdtlock, *ddtlck = *ddtlock;
4094 struct lv *slv, *dlv;
4098 sstbl = (s8 *) & sp->slot[sp->header.stblindex];
4099 dstbl = (s8 *) & dp->slot[dp->header.stblindex];
4101 dsi = dp->header.freelist; /* first (whole page) free slot */
4102 sfsi = sp->header.freelist;
4104 /* linelock destination entry slot */
4105 dlv = & ddtlck->lv[ddtlck->index];
4108 /* linelock source entry slot */
4109 slv = & sdtlck->lv[sdtlck->index];
4110 slv->offset = sstbl[si];
4111 xssi = slv->offset - 1;
4117 for (di = 0; si < sp->header.nextindex; si++, di++) {
4121 /* is next slot contiguous ? */
4122 if (ssi != xssi + 1) {
4123 /* close current linelock */
4127 /* open new linelock */
4128 if (sdtlck->index < sdtlck->maxcnt)
4131 sdtlck = (struct dt_lock *) txLinelock(sdtlck);
4132 slv = & sdtlck->lv[0];
4140 * move head/only segment of an entry
4143 h = d = &dp->slot[dsi];
4145 /* get src slot and move */
4147 if (sp->header.flag & BT_LEAF) {
4148 /* get source entry */
4149 slh = (struct ldtentry *) s;
4150 dlh = (struct ldtentry *) h;
4151 snamlen = slh->namlen;
4154 len = min(snamlen, DTLHDRDATALEN);
4155 dlh->index = slh->index; /* little-endian */
4157 len = min(snamlen, DTLHDRDATALEN_LEGACY);
4159 memcpy(dlh, slh, 6 + len * 2);
4163 /* update dst head/only segment next field */
4167 sih = (struct idtentry *) s;
4168 snamlen = sih->namlen;
4170 len = min(snamlen, DTIHDRDATALEN);
4171 dih = (struct idtentry *) h;
4172 memcpy(dih, sih, 10 + len * 2);
4179 /* free src head/only segment */
4189 * move additional segment(s) of the entry
4192 while ((ssi = next) >= 0) {
4193 /* is next slot contiguous ? */
4194 if (ssi != xssi + 1) {
4195 /* close current linelock */
4199 /* open new linelock */
4200 if (sdtlck->index < sdtlck->maxcnt)
4206 slv = & sdtlck->lv[0];
4213 /* get next source segment */
4216 /* get next destination free slot */
4219 len = min(snamlen, DTSLOTDATALEN);
4220 UniStrncpy_le(d->name, s->name, len);
4229 /* free source segment */
4238 /* terminate dst last/only segment */
4240 /* single segment entry */
4241 if (dp->header.flag & BT_LEAF)
4246 /* multi-segment entry */
4250 /* close current linelock */
4259 /* update source header */
4260 sp->header.freelist = sfsi;
4261 sp->header.freecnt += nd;
4263 /* update destination header */
4264 dp->header.nextindex = di;
4266 dp->header.freelist = dsi;
4267 dp->header.freecnt -= nd;
4274 * function: free a (leaf/internal) entry
4276 * log freelist header, stbl, and each segment slot of entry
4277 * (even though last/only segment next field is modified,
4278 * physical image logging requires all segment slots of
4279 * the entry logged to avoid applying previous updates
4280 * to the same slots)
4282 static void dtDeleteEntry(dtpage_t * p, int fi, struct dt_lock ** dtlock)
4284 int fsi; /* free entry slot index */
4288 struct dt_lock *dtlck = *dtlock;
4292 /* get free entry slot index */
4293 stbl = DT_GETSTBL(p);
4296 /* open new linelock */
4297 if (dtlck->index >= dtlck->maxcnt)
4298 dtlck = (struct dt_lock *) txLinelock(dtlck);
4299 lv = & dtlck->lv[dtlck->index];
4303 /* get the head/only segment */
4305 if (p->header.flag & BT_LEAF)
4306 si = ((struct ldtentry *) t)->next;
4308 si = ((struct idtentry *) t)->next;
4315 /* find the last/only segment */
4317 /* is next slot contiguous ? */
4318 if (si != xsi + 1) {
4319 /* close current linelock */
4323 /* open new linelock */
4324 if (dtlck->index < dtlck->maxcnt)
4327 dtlck = (struct dt_lock *) txLinelock(dtlck);
4328 lv = & dtlck->lv[0];
4344 /* close current linelock */
4350 /* update freelist */
4351 t->next = p->header.freelist;
4352 p->header.freelist = fsi;
4353 p->header.freecnt += freecnt;
4355 /* if delete from middle,
4356 * shift left the succedding entries in the stbl
4358 si = p->header.nextindex;
4360 memmove(&stbl[fi], &stbl[fi + 1], si - fi - 1);
4362 p->header.nextindex--;
4369 * function: truncate a (leaf/internal) entry
4371 * log freelist header, stbl, and each segment slot of entry
4372 * (even though last/only segment next field is modified,
4373 * physical image logging requires all segment slots of
4374 * the entry logged to avoid applying previous updates
4375 * to the same slots)
4377 static void dtTruncateEntry(dtpage_t * p, int ti, struct dt_lock ** dtlock)
4379 int tsi; /* truncate entry slot index */
4383 struct dt_lock *dtlck = *dtlock;
4387 /* get free entry slot index */
4388 stbl = DT_GETSTBL(p);
4391 /* open new linelock */
4392 if (dtlck->index >= dtlck->maxcnt)
4393 dtlck = (struct dt_lock *) txLinelock(dtlck);
4394 lv = & dtlck->lv[dtlck->index];
4398 /* get the head/only segment */
4400 ASSERT(p->header.flag & BT_INTERNAL);
4401 ((struct idtentry *) t)->namlen = 0;
4402 si = ((struct idtentry *) t)->next;
4403 ((struct idtentry *) t)->next = -1;
4410 /* find the last/only segment */
4412 /* is next slot contiguous ? */
4413 if (si != xsi + 1) {
4414 /* close current linelock */
4418 /* open new linelock */
4419 if (dtlck->index < dtlck->maxcnt)
4422 dtlck = (struct dt_lock *) txLinelock(dtlck);
4423 lv = & dtlck->lv[0];
4439 /* close current linelock */
4445 /* update freelist */
4448 t->next = p->header.freelist;
4449 p->header.freelist = fsi;
4450 p->header.freecnt += freecnt;
4455 * dtLinelockFreelist()
4457 static void dtLinelockFreelist(dtpage_t * p, /* directory page */
4458 int m, /* max slot index */
4459 struct dt_lock ** dtlock)
4461 int fsi; /* free entry slot index */
4464 struct dt_lock *dtlck = *dtlock;
4468 /* get free entry slot index */
4469 fsi = p->header.freelist;
4471 /* open new linelock */
4472 if (dtlck->index >= dtlck->maxcnt)
4473 dtlck = (struct dt_lock *) txLinelock(dtlck);
4474 lv = & dtlck->lv[dtlck->index];
4484 /* find the last/only segment */
4485 while (si < m && si >= 0) {
4486 /* is next slot contiguous ? */
4487 if (si != xsi + 1) {
4488 /* close current linelock */
4492 /* open new linelock */
4493 if (dtlck->index < dtlck->maxcnt)
4496 dtlck = (struct dt_lock *) txLinelock(dtlck);
4497 lv = & dtlck->lv[0];
4511 /* close current linelock */
4522 * FUNCTION: Modify the inode number part of a directory entry
4525 * tid - Transaction id
4526 * ip - Inode of parent directory
4527 * key - Name of entry to be modified
4528 * orig_ino - Original inode number expected in entry
4529 * new_ino - New inode number to put into entry
4533 * -ESTALE - If entry found does not match orig_ino passed in
4534 * -ENOENT - If no entry can be found to match key
4535 * 0 - If successfully modified entry
4537 int dtModify(tid_t tid, struct inode *ip,
4538 struct component_name * key, ino_t * orig_ino, ino_t new_ino, int flag)
4542 struct metapage *mp;
4545 struct btstack btstack;
4547 struct dt_lock *dtlck;
4550 int entry_si; /* entry slot index */
4551 struct ldtentry *entry;
4554 * search for the entry to modify:
4556 * dtSearch() returns (leaf page pinned, index at which to modify).
4558 if ((rc = dtSearch(ip, key, orig_ino, &btstack, flag)))
4561 /* retrieve search result */
4562 DT_GETSEARCH(ip, btstack.top, bn, mp, p, index);
4564 BT_MARK_DIRTY(mp, ip);
4566 * acquire a transaction lock on the leaf page of named entry
4568 tlck = txLock(tid, ip, mp, tlckDTREE | tlckENTRY);
4569 dtlck = (struct dt_lock *) & tlck->lock;
4571 /* get slot index of the entry */
4572 stbl = DT_GETSTBL(p);
4573 entry_si = stbl[index];
4575 /* linelock entry */
4576 ASSERT(dtlck->index == 0);
4577 lv = & dtlck->lv[0];
4578 lv->offset = entry_si;
4582 /* get the head/only segment */
4583 entry = (struct ldtentry *) & p->slot[entry_si];
4585 /* substitute the inode number of the entry */
4586 entry->inumber = cpu_to_le32(new_ino);
4588 /* unpin the leaf page */