2 * Copyright (C) International Business Machines Corp., 2000-2004
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
12 * the GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 * jfs_dtree.c: directory B+-tree manager
22 * B+-tree with variable length key directory:
24 * each directory page is structured as an array of 32-byte
25 * directory entry slots initialized as a freelist
26 * to avoid search/compaction of free space at insertion.
27 * when an entry is inserted, a number of slots are allocated
28 * from the freelist as required to store variable length data
29 * of the entry; when the entry is deleted, slots of the entry
30 * are returned to freelist.
32 * leaf entry stores full name as key and file serial number
33 * (aka inode number) as data.
34 * internal/router entry stores sufffix compressed name
35 * as key and simple extent descriptor as data.
37 * each directory page maintains a sorted entry index table
38 * which stores the start slot index of sorted entries
39 * to allow binary search on the table.
41 * directory starts as a root/leaf page in on-disk inode
43 * when it becomes full, it starts a leaf of a external extent
44 * of length of 1 block. each time the first leaf becomes full,
45 * it is extended rather than split (its size is doubled),
46 * until its length becoms 4 KBytes, from then the extent is split
47 * with new 4 Kbyte extent when it becomes full
48 * to reduce external fragmentation of small directories.
50 * blah, blah, blah, for linear scan of directory in pieces by
54 * case-insensitive directory file system
56 * names are stored in case-sensitive way in leaf entry.
57 * but stored, searched and compared in case-insensitive (uppercase) order
58 * (i.e., both search key and entry key are folded for search/compare):
59 * (note that case-sensitive order is BROKEN in storage, e.g.,
60 * sensitive: Ad, aB, aC, aD -> insensitive: aB, aC, aD, Ad
62 * entries which folds to the same key makes up a equivalent class
63 * whose members are stored as contiguous cluster (may cross page boundary)
64 * but whose order is arbitrary and acts as duplicate, e.g.,
67 * once match is found at leaf, requires scan forward/backward
68 * either for, in case-insensitive search, duplicate
69 * or for, in case-sensitive search, for exact match
71 * router entry must be created/stored in case-insensitive way
73 * (right most key of left page and left most key of right page
74 * are folded, and its suffix compression is propagated as router
76 * (e.g., if split occurs <abc> and <aBd>, <ABD> trather than <aB>
77 * should be made the router key for the split)
79 * case-insensitive search:
83 * case-insensitive search of B-tree:
84 * for internal entry, router key is already folded;
85 * for leaf entry, fold the entry key before comparison.
87 * if (leaf entry case-insensitive match found)
88 * if (next entry satisfies case-insensitive match)
90 * if (prev entry satisfies case-insensitive match)
97 * target directory inode lock is being held on entry/exit
98 * of all main directory service routines.
100 * log based recovery:
103 #include <linux/fs.h>
104 #include <linux/quotaops.h>
105 #include <linux/slab.h>
106 #include "jfs_incore.h"
107 #include "jfs_superblock.h"
108 #include "jfs_filsys.h"
109 #include "jfs_metapage.h"
110 #include "jfs_dmap.h"
111 #include "jfs_unicode.h"
112 #include "jfs_debug.h"
114 /* dtree split parameter */
119 struct component_name *key;
121 struct pxdlist *pxdlist;
124 #define DT_PAGE(IP, MP) BT_PAGE(IP, MP, dtpage_t, i_dtroot)
126 /* get page buffer for specified block address */
127 #define DT_GETPAGE(IP, BN, MP, SIZE, P, RC)\
129 BT_GETPAGE(IP, BN, MP, dtpage_t, SIZE, P, RC, i_dtroot)\
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 (dbAlloc(ip, 0, sbi->nbperpage, &xaddr)) {
388 dquot_free_block(ip, sbi->nbperpage);
393 * Save the table, we're going to overwrite it with the
396 memcpy(temp_table, &jfs_ip->i_dirtable, sizeof(temp_table));
399 * Initialize empty x-tree
404 * Add the first block to the xtree
406 if (xtInsert(tid, ip, 0, 0, sbi->nbperpage, &xaddr, 0)) {
407 /* This really shouldn't fail */
408 jfs_warn("add_index: xtInsert failed!");
409 memcpy(&jfs_ip->i_dirtable, temp_table,
410 sizeof (temp_table));
411 dbFree(ip, xaddr, sbi->nbperpage);
412 dquot_free_block(ip, sbi->nbperpage);
417 mp = get_index_page(ip, 0);
419 jfs_err("add_index: get_metapage failed!");
420 xtTruncate(tid, ip, 0, COMMIT_PWMAP);
421 memcpy(&jfs_ip->i_dirtable, temp_table,
422 sizeof (temp_table));
425 tlck = txLock(tid, ip, mp, tlckDATA);
426 llck = (struct linelock *) & tlck->lock;
427 ASSERT(llck->index == 0);
431 lv->length = 6; /* tlckDATA slot size is 16 bytes */
434 memcpy(mp->data, temp_table, sizeof(temp_table));
436 mark_metapage_dirty(mp);
437 release_metapage(mp);
440 * Logging is now directed by xtree tlocks
442 clear_cflag(COMMIT_Dirtable, ip);
445 offset = (index - 2) * sizeof(struct dir_table_slot);
446 page_offset = offset & (PSIZE - 1);
447 blkno = ((offset + 1) >> L2PSIZE) << sbi->l2nbperpage;
448 if (page_offset == 0) {
450 * This will be the beginning of a new page
453 if (xtInsert(tid, ip, 0, blkno, sbi->nbperpage, &xaddr, 0)) {
454 jfs_warn("add_index: xtInsert failed!");
459 if ((mp = get_index_page(ip, blkno)))
460 memset(mp->data, 0, PSIZE); /* Just looks better */
462 xtTruncate(tid, ip, offset, COMMIT_PWMAP);
464 mp = read_index_page(ip, blkno);
467 jfs_err("add_index: get/read_metapage failed!");
471 lock_index(tid, ip, mp, index);
474 (struct dir_table_slot *) ((char *) mp->data + page_offset);
475 dirtab_slot->flag = DIR_INDEX_VALID;
476 dirtab_slot->slot = slot;
477 DTSaddress(dirtab_slot, bn);
479 mark_metapage_dirty(mp);
480 release_metapage(mp);
486 jfs_ip->next_index--;
494 * Marks an entry to the directory index table as free.
496 static void free_index(tid_t tid, struct inode *ip, u32 index, u32 next)
498 struct dir_table_slot *dirtab_slot;
500 struct metapage *mp = NULL;
502 dirtab_slot = find_index(ip, index, &mp, &lblock);
507 dirtab_slot->flag = DIR_INDEX_FREE;
508 dirtab_slot->slot = dirtab_slot->addr1 = 0;
509 dirtab_slot->addr2 = cpu_to_le32(next);
512 lock_index(tid, ip, mp, index);
513 mark_metapage_dirty(mp);
514 release_metapage(mp);
516 set_cflag(COMMIT_Dirtable, ip);
522 * Changes an entry in the directory index table
524 static void modify_index(tid_t tid, struct inode *ip, u32 index, s64 bn,
525 int slot, struct metapage ** mp, s64 *lblock)
527 struct dir_table_slot *dirtab_slot;
529 dirtab_slot = find_index(ip, index, mp, lblock);
534 DTSaddress(dirtab_slot, bn);
535 dirtab_slot->slot = slot;
538 lock_index(tid, ip, *mp, index);
539 mark_metapage_dirty(*mp);
541 set_cflag(COMMIT_Dirtable, ip);
547 * reads a directory table slot
549 static int read_index(struct inode *ip, u32 index,
550 struct dir_table_slot * dirtab_slot)
553 struct metapage *mp = NULL;
554 struct dir_table_slot *slot;
556 slot = find_index(ip, index, &mp, &lblock);
561 memcpy(dirtab_slot, slot, sizeof(struct dir_table_slot));
564 release_metapage(mp);
573 * Search for the entry with specified key
577 * return: 0 - search result on stack, leaf page pinned;
580 int dtSearch(struct inode *ip, struct component_name * key, ino_t * data,
581 struct btstack * btstack, int flag)
584 int cmp = 1; /* init for empty page */
589 int base, index, lim;
590 struct btframe *btsp;
592 int psize = 288; /* initial in-line directory */
594 struct component_name ciKey;
595 struct super_block *sb = ip->i_sb;
597 ciKey.name = kmalloc((JFS_NAME_MAX + 1) * sizeof(wchar_t), GFP_NOFS);
604 /* uppercase search key for c-i directory */
605 UniStrcpy(ciKey.name, key->name);
606 ciKey.namlen = key->namlen;
608 /* only uppercase if case-insensitive support is on */
609 if ((JFS_SBI(sb)->mntflag & JFS_OS2) == JFS_OS2) {
612 BT_CLR(btstack); /* reset stack */
614 /* init level count for max pages to split */
618 * search down tree from root:
620 * between two consecutive entries of <Ki, Pi> and <Kj, Pj> of
621 * internal page, child page Pi contains entry with k, Ki <= K < Kj.
623 * if entry with search key K is not found
624 * internal page search find the entry with largest key Ki
625 * less than K which point to the child page to search;
626 * leaf page search find the entry with smallest key Kj
627 * greater than K so that the returned index is the position of
628 * the entry to be shifted right for insertion of new entry.
629 * for empty tree, search key is greater than any key of the tree.
631 * by convention, root bn = 0.
634 /* get/pin the page to search */
635 DT_GETPAGE(ip, bn, mp, psize, p, rc);
639 /* get sorted entry table of the page */
640 stbl = DT_GETSTBL(p);
643 * binary search with search key K on the current page.
645 for (base = 0, lim = p->header.nextindex; lim; lim >>= 1) {
646 index = base + (lim >> 1);
648 if (p->header.flag & BT_LEAF) {
649 /* uppercase leaf name to compare */
651 ciCompare(&ciKey, p, stbl[index],
652 JFS_SBI(sb)->mntflag);
654 /* router key is in uppercase */
656 cmp = dtCompare(&ciKey, p, stbl[index]);
664 /* search hit - leaf page:
665 * return the entry found
667 if (p->header.flag & BT_LEAF) {
668 inumber = le32_to_cpu(
669 ((struct ldtentry *) & p->slot[stbl[index]])->inumber);
672 * search for JFS_LOOKUP
674 if (flag == JFS_LOOKUP) {
681 * search for JFS_CREATE
683 if (flag == JFS_CREATE) {
690 * search for JFS_REMOVE or JFS_RENAME
692 if ((flag == JFS_REMOVE ||
693 flag == JFS_RENAME) &&
700 * JFS_REMOVE|JFS_FINDDIR|JFS_RENAME
702 /* save search result */
713 /* search hit - internal page:
714 * descend/search its child page
728 * base is the smallest index with key (Kj) greater than
729 * search key (K) and may be zero or (maxindex + 1) index.
732 * search miss - leaf page
734 * return location of entry (base) where new entry with
735 * search key K is to be inserted.
737 if (p->header.flag & BT_LEAF) {
739 * search for JFS_LOOKUP, JFS_REMOVE, or JFS_RENAME
741 if (flag == JFS_LOOKUP || flag == JFS_REMOVE ||
742 flag == JFS_RENAME) {
748 * search for JFS_CREATE|JFS_FINDDIR:
763 * search miss - internal page
765 * if base is non-zero, decrement base by one to get the parent
766 * entry of the child page to search.
768 index = base ? base - 1 : base;
771 * go down to child page
774 /* update max. number of pages to split */
775 if (BT_STACK_FULL(btstack)) {
776 /* Something's corrupted, mark filesystem dirty so
777 * chkdsk will fix it.
779 jfs_error(sb, "stack overrun in dtSearch!");
780 BT_STACK_DUMP(btstack);
786 /* push (bn, index) of the parent page/entry */
787 BT_PUSH(btstack, bn, index);
789 /* get the child page block number */
790 pxd = (pxd_t *) & p->slot[stbl[index]];
791 bn = addressPXD(pxd);
792 psize = lengthPXD(pxd) << JFS_SBI(ip->i_sb)->l2bsize;
794 /* unpin the parent page */
814 * function: insert an entry to directory tree
818 * return: 0 - success;
821 int dtInsert(tid_t tid, struct inode *ip,
822 struct component_name * name, ino_t * fsn, struct btstack * btstack)
825 struct metapage *mp; /* meta-page buffer */
826 dtpage_t *p; /* base B+-tree index page */
829 struct dtsplit split; /* split information */
831 struct dt_lock *dtlck;
837 * retrieve search result
839 * dtSearch() returns (leaf page pinned, index at which to insert).
840 * n.b. dtSearch() may return index of (maxindex + 1) of
843 DT_GETSEARCH(ip, btstack->top, bn, mp, p, index);
846 * insert entry for new key
849 if (JFS_IP(ip)->next_index == DIREND) {
853 n = NDTLEAF(name->namlen);
857 n = NDTLEAF_LEGACY(name->namlen);
858 data.leaf.ip = NULL; /* signifies legacy directory format */
860 data.leaf.ino = *fsn;
863 * leaf page does not have enough room for new entry:
865 * extend/split the leaf page;
867 * dtSplitUp() will insert the entry and unpin the leaf page.
869 if (n > p->header.freecnt) {
875 rc = dtSplitUp(tid, ip, &split, btstack);
880 * leaf page does have enough room for new entry:
882 * insert the new data entry into the leaf page;
884 BT_MARK_DIRTY(mp, ip);
886 * acquire a transaction lock on the leaf page
888 tlck = txLock(tid, ip, mp, tlckDTREE | tlckENTRY);
889 dtlck = (struct dt_lock *) & tlck->lock;
890 ASSERT(dtlck->index == 0);
893 /* linelock header */
898 dtInsertEntry(p, index, name, &data, &dtlck);
900 /* linelock stbl of non-root leaf page */
901 if (!(p->header.flag & BT_ROOT)) {
902 if (dtlck->index >= dtlck->maxcnt)
903 dtlck = (struct dt_lock *) txLinelock(dtlck);
904 lv = & dtlck->lv[dtlck->index];
905 n = index >> L2DTSLOTSIZE;
906 lv->offset = p->header.stblindex + n;
908 ((p->header.nextindex - 1) >> L2DTSLOTSIZE) - n + 1;
912 /* unpin the leaf page */
922 * function: propagate insertion bottom up;
926 * return: 0 - success;
928 * leaf page unpinned;
930 static int dtSplitUp(tid_t tid,
931 struct inode *ip, struct dtsplit * split, struct btstack * btstack)
933 struct jfs_sb_info *sbi = JFS_SBI(ip->i_sb);
935 struct metapage *smp;
936 dtpage_t *sp; /* split page */
937 struct metapage *rmp;
938 dtpage_t *rp; /* new right page split from sp */
939 pxd_t rpxd; /* new right page extent descriptor */
940 struct metapage *lmp;
941 dtpage_t *lp; /* left child page */
942 int skip; /* index of entry of insertion */
943 struct btframe *parent; /* parent page entry on traverse stack */
946 struct pxdlist pxdlist;
948 struct component_name key = { 0, NULL };
949 ddata_t *data = split->data;
951 struct dt_lock *dtlck;
954 int quota_allocation = 0;
958 sp = DT_PAGE(ip, smp);
960 key.name = kmalloc((JFS_NAME_MAX + 2) * sizeof(wchar_t), GFP_NOFS);
970 * The split routines insert the new entry, and
971 * acquire txLock as appropriate.
974 * split root leaf page:
976 if (sp->header.flag & BT_ROOT) {
978 * allocate a single extent child page
981 n = sbi->bsize >> L2DTSLOTSIZE;
982 n -= (n + 31) >> L2DTSLOTSIZE; /* stbl size */
983 n -= DTROOTMAXSLOT - sp->header.freecnt; /* header + entries */
984 if (n <= split->nslot)
986 if ((rc = dbAlloc(ip, 0, (s64) xlen, &xaddr))) {
993 pxd = &pxdlist.pxd[0];
994 PXDaddress(pxd, xaddr);
995 PXDlength(pxd, xlen);
996 split->pxdlist = &pxdlist;
997 rc = dtSplitRoot(tid, ip, split, &rmp);
1000 dbFree(ip, xaddr, xlen);
1007 ip->i_size = xlen << sbi->l2bsize;
1013 * extend first leaf page
1015 * extend the 1st extent if less than buffer page size
1016 * (dtExtendPage() reurns leaf page unpinned)
1018 pxd = &sp->header.self;
1019 xlen = lengthPXD(pxd);
1020 xsize = xlen << sbi->l2bsize;
1021 if (xsize < PSIZE) {
1022 xaddr = addressPXD(pxd);
1023 n = xsize >> L2DTSLOTSIZE;
1024 n -= (n + 31) >> L2DTSLOTSIZE; /* stbl size */
1025 if ((n + sp->header.freecnt) <= split->nslot)
1026 n = xlen + (xlen << 1);
1030 /* Allocate blocks to quota. */
1031 rc = dquot_alloc_block(ip, n);
1034 quota_allocation += n;
1036 if ((rc = dbReAlloc(sbi->ipbmap, xaddr, (s64) xlen,
1040 pxdlist.maxnpxd = 1;
1042 pxd = &pxdlist.pxd[0];
1043 PXDaddress(pxd, nxaddr)
1044 PXDlength(pxd, xlen + n);
1045 split->pxdlist = &pxdlist;
1046 if ((rc = dtExtendPage(tid, ip, split, btstack))) {
1047 nxaddr = addressPXD(pxd);
1048 if (xaddr != nxaddr) {
1049 /* free relocated extent */
1050 xlen = lengthPXD(pxd);
1051 dbFree(ip, nxaddr, (s64) xlen);
1053 /* free extended delta */
1054 xlen = lengthPXD(pxd) - n;
1055 xaddr = addressPXD(pxd) + xlen;
1056 dbFree(ip, xaddr, (s64) n);
1058 } else if (!DO_INDEX(ip))
1059 ip->i_size = lengthPXD(pxd) << sbi->l2bsize;
1068 * split leaf page <sp> into <sp> and a new right page <rp>.
1070 * return <rp> pinned and its extent descriptor <rpxd>
1073 * allocate new directory page extent and
1074 * new index page(s) to cover page split(s)
1076 * allocation hint: ?
1078 n = btstack->nsplit;
1079 pxdlist.maxnpxd = pxdlist.npxd = 0;
1080 xlen = sbi->nbperpage;
1081 for (pxd = pxdlist.pxd; n > 0; n--, pxd++) {
1082 if ((rc = dbAlloc(ip, 0, (s64) xlen, &xaddr)) == 0) {
1083 PXDaddress(pxd, xaddr);
1084 PXDlength(pxd, xlen);
1091 /* undo allocation */
1095 split->pxdlist = &pxdlist;
1096 if ((rc = dtSplitPage(tid, ip, split, &rmp, &rp, &rpxd))) {
1099 /* undo allocation */
1104 ip->i_size += PSIZE;
1107 * propagate up the router entry for the leaf page just split
1109 * insert a router entry for the new page into the parent page,
1110 * propagate the insert/split up the tree by walking back the stack
1111 * of (bn of parent page, index of child page entry in parent page)
1112 * that were traversed during the search for the page that split.
1114 * the propagation of insert/split up the tree stops if the root
1115 * splits or the page inserted into doesn't have to split to hold
1118 * the parent entry for the split page remains the same, and
1119 * a new entry is inserted at its right with the first key and
1120 * block number of the new right page.
1122 * There are a maximum of 4 pages pinned at any time:
1123 * two children, left parent and right parent (when the parent splits).
1124 * keep the child pages pinned while working on the parent.
1125 * make sure that all pins are released at exit.
1127 while ((parent = BT_POP(btstack)) != NULL) {
1128 /* parent page specified by stack frame <parent> */
1130 /* keep current child pages (<lp>, <rp>) pinned */
1135 * insert router entry in parent for new right child page <rp>
1137 /* get the parent page <sp> */
1138 DT_GETPAGE(ip, parent->bn, smp, PSIZE, sp, rc);
1146 * The new key entry goes ONE AFTER the index of parent entry,
1147 * because the split was to the right.
1149 skip = parent->index + 1;
1152 * compute the key for the router entry
1154 * key suffix compression:
1155 * for internal pages that have leaf pages as children,
1156 * retain only what's needed to distinguish between
1157 * the new entry and the entry on the page to its left.
1158 * If the keys compare equal, retain the entire key.
1160 * note that compression is performed only at computing
1161 * router key at the lowest internal level.
1162 * further compression of the key between pairs of higher
1163 * level internal pages loses too much information and
1164 * the search may fail.
1165 * (e.g., two adjacent leaf pages of {a, ..., x} {xx, ...,}
1166 * results in two adjacent parent entries (a)(xx).
1167 * if split occurs between these two entries, and
1168 * if compression is applied, the router key of parent entry
1169 * of right page (x) will divert search for x into right
1170 * subtree and miss x in the left subtree.)
1172 * the entire key must be retained for the next-to-leftmost
1173 * internal key at any level of the tree, or search may fail
1176 switch (rp->header.flag & BT_TYPE) {
1179 * compute the length of prefix for suffix compression
1180 * between last entry of left page and first entry
1183 if ((sp->header.flag & BT_ROOT && skip > 1) ||
1184 sp->header.prev != 0 || skip > 1) {
1185 /* compute uppercase router prefix key */
1186 rc = ciGetLeafPrefixKey(lp,
1187 lp->header.nextindex-1,
1197 /* next to leftmost entry of
1198 lowest internal level */
1200 /* compute uppercase router key */
1201 dtGetKey(rp, 0, &key, sbi->mntflag);
1202 key.name[key.namlen] = 0;
1204 if ((sbi->mntflag & JFS_OS2) == JFS_OS2)
1208 n = NDTINTERNAL(key.namlen);
1212 dtGetKey(rp, 0, &key, sbi->mntflag);
1213 n = NDTINTERNAL(key.namlen);
1217 jfs_err("dtSplitUp(): UFO!");
1221 /* unpin left child page */
1225 * compute the data for the router entry
1227 data->xd = rpxd; /* child page xd */
1230 * parent page is full - split the parent page
1232 if (n > sp->header.freecnt) {
1233 /* init for parent page split */
1235 split->index = skip; /* index at insert */
1238 /* split->data = data; */
1240 /* unpin right child page */
1243 /* The split routines insert the new entry,
1244 * acquire txLock as appropriate.
1245 * return <rp> pinned and its block number <rbn>.
1247 rc = (sp->header.flag & BT_ROOT) ?
1248 dtSplitRoot(tid, ip, split, &rmp) :
1249 dtSplitPage(tid, ip, split, &rmp, &rp, &rpxd);
1255 /* smp and rmp are pinned */
1258 * parent page is not full - insert router entry in parent page
1261 BT_MARK_DIRTY(smp, ip);
1263 * acquire a transaction lock on the parent page
1265 tlck = txLock(tid, ip, smp, tlckDTREE | tlckENTRY);
1266 dtlck = (struct dt_lock *) & tlck->lock;
1267 ASSERT(dtlck->index == 0);
1268 lv = & dtlck->lv[0];
1270 /* linelock header */
1275 /* linelock stbl of non-root parent page */
1276 if (!(sp->header.flag & BT_ROOT)) {
1278 n = skip >> L2DTSLOTSIZE;
1279 lv->offset = sp->header.stblindex + n;
1281 ((sp->header.nextindex -
1282 1) >> L2DTSLOTSIZE) - n + 1;
1286 dtInsertEntry(sp, skip, &key, data, &dtlck);
1288 /* exit propagate up */
1293 /* unpin current split and its right page */
1298 * free remaining extents allocated for split
1302 pxd = &pxdlist.pxd[n];
1303 for (; n < pxdlist.maxnpxd; n++, pxd++)
1304 dbFree(ip, addressPXD(pxd), (s64) lengthPXD(pxd));
1309 /* Rollback quota allocation */
1310 if (rc && quota_allocation)
1311 dquot_free_block(ip, quota_allocation);
1322 * function: Split a non-root page of a btree.
1326 * return: 0 - success;
1328 * return split and new page pinned;
1330 static int dtSplitPage(tid_t tid, struct inode *ip, struct dtsplit * split,
1331 struct metapage ** rmpp, dtpage_t ** rpp, pxd_t * rpxdp)
1334 struct metapage *smp;
1336 struct metapage *rmp;
1337 dtpage_t *rp; /* new right page allocated */
1338 s64 rbn; /* new right page block number */
1339 struct metapage *mp;
1342 struct pxdlist *pxdlist;
1344 int skip, nextindex, half, left, nxt, off, si;
1345 struct ldtentry *ldtentry;
1346 struct idtentry *idtentry;
1351 struct dt_lock *sdtlck, *rdtlck;
1353 struct dt_lock *dtlck;
1354 struct lv *slv, *rlv, *lv;
1356 /* get split page */
1358 sp = DT_PAGE(ip, smp);
1361 * allocate the new right page for the split
1363 pxdlist = split->pxdlist;
1364 pxd = &pxdlist->pxd[pxdlist->npxd];
1366 rbn = addressPXD(pxd);
1367 rmp = get_metapage(ip, rbn, PSIZE, 1);
1371 /* Allocate blocks to quota. */
1372 rc = dquot_alloc_block(ip, lengthPXD(pxd));
1374 release_metapage(rmp);
1378 jfs_info("dtSplitPage: ip:0x%p smp:0x%p rmp:0x%p", ip, smp, rmp);
1380 BT_MARK_DIRTY(rmp, ip);
1382 * acquire a transaction lock on the new right page
1384 tlck = txLock(tid, ip, rmp, tlckDTREE | tlckNEW);
1385 rdtlck = (struct dt_lock *) & tlck->lock;
1387 rp = (dtpage_t *) rmp->data;
1389 rp->header.self = *pxd;
1391 BT_MARK_DIRTY(smp, ip);
1393 * acquire a transaction lock on the split page
1397 tlck = txLock(tid, ip, smp, tlckDTREE | tlckENTRY);
1398 sdtlck = (struct dt_lock *) & tlck->lock;
1400 /* linelock header of split page */
1401 ASSERT(sdtlck->index == 0);
1402 slv = & sdtlck->lv[0];
1408 * initialize/update sibling pointers between sp and rp
1410 nextbn = le64_to_cpu(sp->header.next);
1411 rp->header.next = cpu_to_le64(nextbn);
1412 rp->header.prev = cpu_to_le64(addressPXD(&sp->header.self));
1413 sp->header.next = cpu_to_le64(rbn);
1416 * initialize new right page
1418 rp->header.flag = sp->header.flag;
1420 /* compute sorted entry table at start of extent data area */
1421 rp->header.nextindex = 0;
1422 rp->header.stblindex = 1;
1424 n = PSIZE >> L2DTSLOTSIZE;
1425 rp->header.maxslot = n;
1426 stblsize = (n + 31) >> L2DTSLOTSIZE; /* in unit of slot */
1429 fsi = rp->header.stblindex + stblsize;
1430 rp->header.freelist = fsi;
1431 rp->header.freecnt = rp->header.maxslot - fsi;
1434 * sequential append at tail: append without split
1436 * If splitting the last page on a level because of appending
1437 * a entry to it (skip is maxentry), it's likely that the access is
1438 * sequential. Adding an empty page on the side of the level is less
1439 * work and can push the fill factor much higher than normal.
1440 * If we're wrong it's no big deal, we'll just do the split the right
1442 * (It may look like it's equally easy to do a similar hack for
1443 * reverse sorted data, that is, split the tree left,
1444 * but it's not. Be my guest.)
1446 if (nextbn == 0 && split->index == sp->header.nextindex) {
1447 /* linelock header + stbl (first slot) of new page */
1448 rlv = & rdtlck->lv[rdtlck->index];
1454 * initialize freelist of new right page
1457 for (fsi++; fsi < rp->header.maxslot; f++, fsi++)
1461 /* insert entry at the first entry of the new right page */
1462 dtInsertEntry(rp, 0, split->key, split->data, &rdtlck);
1468 * non-sequential insert (at possibly middle page)
1472 * update prev pointer of previous right sibling page;
1475 DT_GETPAGE(ip, nextbn, mp, PSIZE, p, rc);
1477 discard_metapage(rmp);
1481 BT_MARK_DIRTY(mp, ip);
1483 * acquire a transaction lock on the next page
1485 tlck = txLock(tid, ip, mp, tlckDTREE | tlckRELINK);
1486 jfs_info("dtSplitPage: tlck = 0x%p, ip = 0x%p, mp=0x%p",
1488 dtlck = (struct dt_lock *) & tlck->lock;
1490 /* linelock header of previous right sibling page */
1491 lv = & dtlck->lv[dtlck->index];
1496 p->header.prev = cpu_to_le64(rbn);
1502 * split the data between the split and right pages.
1504 skip = split->index;
1505 half = (PSIZE >> L2DTSLOTSIZE) >> 1; /* swag */
1509 * compute fill factor for split pages
1511 * <nxt> traces the next entry to move to rp
1512 * <off> traces the next entry to stay in sp
1514 stbl = (u8 *) & sp->slot[sp->header.stblindex];
1515 nextindex = sp->header.nextindex;
1516 for (nxt = off = 0; nxt < nextindex; ++off) {
1518 /* check for fill factor with new entry size */
1522 switch (sp->header.flag & BT_TYPE) {
1524 ldtentry = (struct ldtentry *) & sp->slot[si];
1526 n = NDTLEAF(ldtentry->namlen);
1528 n = NDTLEAF_LEGACY(ldtentry->
1533 idtentry = (struct idtentry *) & sp->slot[si];
1534 n = NDTINTERNAL(idtentry->namlen);
1541 ++nxt; /* advance to next entry to move in sp */
1549 /* <nxt> poins to the 1st entry to move */
1552 * move entries to right page
1554 * dtMoveEntry() initializes rp and reserves entry for insertion
1556 * split page moved out entries are linelocked;
1557 * new/right page moved in entries are linelocked;
1559 /* linelock header + stbl of new right page */
1560 rlv = & rdtlck->lv[rdtlck->index];
1565 dtMoveEntry(sp, nxt, rp, &sdtlck, &rdtlck, DO_INDEX(ip));
1567 sp->header.nextindex = nxt;
1570 * finalize freelist of new right page
1572 fsi = rp->header.freelist;
1574 for (fsi++; fsi < rp->header.maxslot; f++, fsi++)
1579 * Update directory index table for entries now in right page
1581 if ((rp->header.flag & BT_LEAF) && DO_INDEX(ip)) {
1585 stbl = DT_GETSTBL(rp);
1586 for (n = 0; n < rp->header.nextindex; n++) {
1587 ldtentry = (struct ldtentry *) & rp->slot[stbl[n]];
1588 modify_index(tid, ip, le32_to_cpu(ldtentry->index),
1589 rbn, n, &mp, &lblock);
1592 release_metapage(mp);
1596 * the skipped index was on the left page,
1599 /* insert the new entry in the split page */
1600 dtInsertEntry(sp, skip, split->key, split->data, &sdtlck);
1602 /* linelock stbl of split page */
1603 if (sdtlck->index >= sdtlck->maxcnt)
1604 sdtlck = (struct dt_lock *) txLinelock(sdtlck);
1605 slv = & sdtlck->lv[sdtlck->index];
1606 n = skip >> L2DTSLOTSIZE;
1607 slv->offset = sp->header.stblindex + n;
1609 ((sp->header.nextindex - 1) >> L2DTSLOTSIZE) - n + 1;
1613 * the skipped index was on the right page,
1616 /* adjust the skip index to reflect the new position */
1619 /* insert the new entry in the right page */
1620 dtInsertEntry(rp, skip, split->key, split->data, &rdtlck);
1634 * function: extend 1st/only directory leaf page
1638 * return: 0 - success;
1640 * return extended page pinned;
1642 static int dtExtendPage(tid_t tid,
1643 struct inode *ip, struct dtsplit * split, struct btstack * btstack)
1645 struct super_block *sb = ip->i_sb;
1647 struct metapage *smp, *pmp, *mp;
1649 struct pxdlist *pxdlist;
1652 int newstblindex, newstblsize;
1653 int oldstblindex, oldstblsize;
1656 struct btframe *parent;
1658 struct dt_lock *dtlck;
1661 struct pxd_lock *pxdlock;
1664 struct ldtentry *ldtentry;
1667 /* get page to extend */
1669 sp = DT_PAGE(ip, smp);
1671 /* get parent/root page */
1672 parent = BT_POP(btstack);
1673 DT_GETPAGE(ip, parent->bn, pmp, PSIZE, pp, rc);
1680 pxdlist = split->pxdlist;
1681 pxd = &pxdlist->pxd[pxdlist->npxd];
1684 xaddr = addressPXD(pxd);
1685 tpxd = &sp->header.self;
1686 txaddr = addressPXD(tpxd);
1687 /* in-place extension */
1688 if (xaddr == txaddr) {
1695 /* save moved extent descriptor for later free */
1696 tlck = txMaplock(tid, ip, tlckDTREE | tlckRELOCATE);
1697 pxdlock = (struct pxd_lock *) & tlck->lock;
1698 pxdlock->flag = mlckFREEPXD;
1699 pxdlock->pxd = sp->header.self;
1703 * Update directory index table to reflect new page address
1709 stbl = DT_GETSTBL(sp);
1710 for (n = 0; n < sp->header.nextindex; n++) {
1712 (struct ldtentry *) & sp->slot[stbl[n]];
1713 modify_index(tid, ip,
1714 le32_to_cpu(ldtentry->index),
1715 xaddr, n, &mp, &lblock);
1718 release_metapage(mp);
1725 sp->header.self = *pxd;
1727 jfs_info("dtExtendPage: ip:0x%p smp:0x%p sp:0x%p", ip, smp, sp);
1729 BT_MARK_DIRTY(smp, ip);
1731 * acquire a transaction lock on the extended/leaf page
1733 tlck = txLock(tid, ip, smp, tlckDTREE | type);
1734 dtlck = (struct dt_lock *) & tlck->lock;
1735 lv = & dtlck->lv[0];
1737 /* update buffer extent descriptor of extended page */
1738 xlen = lengthPXD(pxd);
1739 xsize = xlen << JFS_SBI(sb)->l2bsize;
1742 * copy old stbl to new stbl at start of extended area
1744 oldstblindex = sp->header.stblindex;
1745 oldstblsize = (sp->header.maxslot + 31) >> L2DTSLOTSIZE;
1746 newstblindex = sp->header.maxslot;
1747 n = xsize >> L2DTSLOTSIZE;
1748 newstblsize = (n + 31) >> L2DTSLOTSIZE;
1749 memcpy(&sp->slot[newstblindex], &sp->slot[oldstblindex],
1750 sp->header.nextindex);
1753 * in-line extension: linelock old area of extended page
1755 if (type == tlckEXTEND) {
1756 /* linelock header */
1762 /* linelock new stbl of extended page */
1763 lv->offset = newstblindex;
1764 lv->length = newstblsize;
1767 * relocation: linelock whole relocated area
1771 lv->length = sp->header.maxslot + newstblsize;
1776 sp->header.maxslot = n;
1777 sp->header.stblindex = newstblindex;
1778 /* sp->header.nextindex remains the same */
1781 * add old stbl region at head of freelist
1785 last = sp->header.freelist;
1786 for (n = 0; n < oldstblsize; n++, fsi++, f++) {
1790 sp->header.freelist = last;
1791 sp->header.freecnt += oldstblsize;
1794 * append free region of newly extended area at tail of freelist
1796 /* init free region of newly extended area */
1797 fsi = n = newstblindex + newstblsize;
1799 for (fsi++; fsi < sp->header.maxslot; f++, fsi++)
1803 /* append new free region at tail of old freelist */
1804 fsi = sp->header.freelist;
1806 sp->header.freelist = n;
1811 } while (fsi != -1);
1816 sp->header.freecnt += sp->header.maxslot - n;
1819 * insert the new entry
1821 dtInsertEntry(sp, split->index, split->key, split->data, &dtlck);
1823 BT_MARK_DIRTY(pmp, ip);
1825 * linelock any freeslots residing in old extent
1827 if (type == tlckEXTEND) {
1828 n = sp->header.maxslot >> 2;
1829 if (sp->header.freelist < n)
1830 dtLinelockFreelist(sp, n, &dtlck);
1834 * update parent entry on the parent/root page
1837 * acquire a transaction lock on the parent/root page
1839 tlck = txLock(tid, ip, pmp, tlckDTREE | tlckENTRY);
1840 dtlck = (struct dt_lock *) & tlck->lock;
1841 lv = & dtlck->lv[dtlck->index];
1843 /* linelock parent entry - 1st slot */
1848 /* update the parent pxd for page extension */
1849 tpxd = (pxd_t *) & pp->slot[1];
1861 * split the full root page into
1862 * original/root/split page and new right page
1863 * i.e., root remains fixed in tree anchor (inode) and
1864 * the root is copied to a single new right child page
1865 * since root page << non-root page, and
1866 * the split root page contains a single entry for the
1867 * new right child page.
1871 * return: 0 - success;
1873 * return new page pinned;
1875 static int dtSplitRoot(tid_t tid,
1876 struct inode *ip, struct dtsplit * split, struct metapage ** rmpp)
1878 struct super_block *sb = ip->i_sb;
1879 struct metapage *smp;
1881 struct metapage *rmp;
1888 int fsi, stblsize, n;
1891 struct pxdlist *pxdlist;
1893 struct dt_lock *dtlck;
1898 /* get split root page */
1900 sp = &JFS_IP(ip)->i_dtroot;
1903 * allocate/initialize a single (right) child page
1905 * N.B. at first split, a one (or two) block to fit new entry
1906 * is allocated; at subsequent split, a full page is allocated;
1908 pxdlist = split->pxdlist;
1909 pxd = &pxdlist->pxd[pxdlist->npxd];
1911 rbn = addressPXD(pxd);
1912 xlen = lengthPXD(pxd);
1913 xsize = xlen << JFS_SBI(sb)->l2bsize;
1914 rmp = get_metapage(ip, rbn, xsize, 1);
1920 /* Allocate blocks to quota. */
1921 rc = dquot_alloc_block(ip, lengthPXD(pxd));
1923 release_metapage(rmp);
1927 BT_MARK_DIRTY(rmp, ip);
1929 * acquire a transaction lock on the new right page
1931 tlck = txLock(tid, ip, rmp, tlckDTREE | tlckNEW);
1932 dtlck = (struct dt_lock *) & tlck->lock;
1935 (sp->header.flag & BT_LEAF) ? BT_LEAF : BT_INTERNAL;
1936 rp->header.self = *pxd;
1938 /* initialize sibling pointers */
1939 rp->header.next = 0;
1940 rp->header.prev = 0;
1943 * move in-line root page into new right page extent
1945 /* linelock header + copied entries + new stbl (1st slot) in new page */
1946 ASSERT(dtlck->index == 0);
1947 lv = & dtlck->lv[0];
1949 lv->length = 10; /* 1 + 8 + 1 */
1952 n = xsize >> L2DTSLOTSIZE;
1953 rp->header.maxslot = n;
1954 stblsize = (n + 31) >> L2DTSLOTSIZE;
1956 /* copy old stbl to new stbl at start of extended area */
1957 rp->header.stblindex = DTROOTMAXSLOT;
1958 stbl = (s8 *) & rp->slot[DTROOTMAXSLOT];
1959 memcpy(stbl, sp->header.stbl, sp->header.nextindex);
1960 rp->header.nextindex = sp->header.nextindex;
1962 /* copy old data area to start of new data area */
1963 memcpy(&rp->slot[1], &sp->slot[1], IDATASIZE);
1966 * append free region of newly extended area at tail of freelist
1968 /* init free region of newly extended area */
1969 fsi = n = DTROOTMAXSLOT + stblsize;
1971 for (fsi++; fsi < rp->header.maxslot; f++, fsi++)
1975 /* append new free region at tail of old freelist */
1976 fsi = sp->header.freelist;
1978 rp->header.freelist = n;
1980 rp->header.freelist = fsi;
1985 } while (fsi != -1);
1990 rp->header.freecnt = sp->header.freecnt + rp->header.maxslot - n;
1993 * Update directory index table for entries now in right page
1995 if ((rp->header.flag & BT_LEAF) && DO_INDEX(ip)) {
1997 struct metapage *mp = NULL;
1998 struct ldtentry *ldtentry;
2000 stbl = DT_GETSTBL(rp);
2001 for (n = 0; n < rp->header.nextindex; n++) {
2002 ldtentry = (struct ldtentry *) & rp->slot[stbl[n]];
2003 modify_index(tid, ip, le32_to_cpu(ldtentry->index),
2004 rbn, n, &mp, &lblock);
2007 release_metapage(mp);
2010 * insert the new entry into the new right/child page
2011 * (skip index in the new right page will not change)
2013 dtInsertEntry(rp, split->index, split->key, split->data, &dtlck);
2016 * reset parent/root page
2018 * set the 1st entry offset to 0, which force the left-most key
2019 * at any level of the tree to be less than any search key.
2021 * The btree comparison code guarantees that the left-most key on any
2022 * level of the tree is never used, so it doesn't need to be filled in.
2024 BT_MARK_DIRTY(smp, ip);
2026 * acquire a transaction lock on the root page (in-memory inode)
2028 tlck = txLock(tid, ip, smp, tlckDTREE | tlckNEW | tlckBTROOT);
2029 dtlck = (struct dt_lock *) & tlck->lock;
2032 ASSERT(dtlck->index == 0);
2033 lv = & dtlck->lv[0];
2035 lv->length = DTROOTMAXSLOT;
2038 /* update page header of root */
2039 if (sp->header.flag & BT_LEAF) {
2040 sp->header.flag &= ~BT_LEAF;
2041 sp->header.flag |= BT_INTERNAL;
2044 /* init the first entry */
2045 s = (struct idtentry *) & sp->slot[DTENTRYSTART];
2051 stbl = sp->header.stbl;
2052 stbl[0] = DTENTRYSTART;
2053 sp->header.nextindex = 1;
2056 fsi = DTENTRYSTART + 1;
2059 /* init free region of remaining area */
2060 for (fsi++; fsi < DTROOTMAXSLOT; f++, fsi++)
2064 sp->header.freelist = DTENTRYSTART + 1;
2065 sp->header.freecnt = DTROOTMAXSLOT - (DTENTRYSTART + 1);
2076 * function: delete the entry(s) referenced by a key.
2082 int dtDelete(tid_t tid,
2083 struct inode *ip, struct component_name * key, ino_t * ino, int flag)
2087 struct metapage *mp, *imp;
2090 struct btstack btstack;
2091 struct dt_lock *dtlck;
2095 struct ldtentry *ldtentry;
2097 u32 table_index, next_index;
2098 struct metapage *nmp;
2102 * search for the entry to delete:
2104 * dtSearch() returns (leaf page pinned, index at which to delete).
2106 if ((rc = dtSearch(ip, key, ino, &btstack, flag)))
2109 /* retrieve search result */
2110 DT_GETSEARCH(ip, btstack.top, bn, mp, p, index);
2113 * We need to find put the index of the next entry into the
2114 * directory index table in order to resume a readdir from this
2118 stbl = DT_GETSTBL(p);
2119 ldtentry = (struct ldtentry *) & p->slot[stbl[index]];
2120 table_index = le32_to_cpu(ldtentry->index);
2121 if (index == (p->header.nextindex - 1)) {
2123 * Last entry in this leaf page
2125 if ((p->header.flag & BT_ROOT)
2126 || (p->header.next == 0))
2129 /* Read next leaf page */
2130 DT_GETPAGE(ip, le64_to_cpu(p->header.next),
2131 nmp, PSIZE, np, rc);
2135 stbl = DT_GETSTBL(np);
2137 (struct ldtentry *) & np->
2140 le32_to_cpu(ldtentry->index);
2146 (struct ldtentry *) & p->slot[stbl[index + 1]];
2147 next_index = le32_to_cpu(ldtentry->index);
2149 free_index(tid, ip, table_index, next_index);
2152 * the leaf page becomes empty, delete the page
2154 if (p->header.nextindex == 1) {
2155 /* delete empty page */
2156 rc = dtDeleteUp(tid, ip, mp, p, &btstack);
2159 * the leaf page has other entries remaining:
2161 * delete the entry from the leaf page.
2164 BT_MARK_DIRTY(mp, ip);
2166 * acquire a transaction lock on the leaf page
2168 tlck = txLock(tid, ip, mp, tlckDTREE | tlckENTRY);
2169 dtlck = (struct dt_lock *) & tlck->lock;
2172 * Do not assume that dtlck->index will be zero. During a
2173 * rename within a directory, this transaction may have
2174 * modified this page already when adding the new entry.
2177 /* linelock header */
2178 if (dtlck->index >= dtlck->maxcnt)
2179 dtlck = (struct dt_lock *) txLinelock(dtlck);
2180 lv = & dtlck->lv[dtlck->index];
2185 /* linelock stbl of non-root leaf page */
2186 if (!(p->header.flag & BT_ROOT)) {
2187 if (dtlck->index >= dtlck->maxcnt)
2188 dtlck = (struct dt_lock *) txLinelock(dtlck);
2189 lv = & dtlck->lv[dtlck->index];
2190 i = index >> L2DTSLOTSIZE;
2191 lv->offset = p->header.stblindex + i;
2193 ((p->header.nextindex - 1) >> L2DTSLOTSIZE) -
2198 /* free the leaf entry */
2199 dtDeleteEntry(p, index, &dtlck);
2202 * Update directory index table for entries moved in stbl
2204 if (DO_INDEX(ip) && index < p->header.nextindex) {
2208 stbl = DT_GETSTBL(p);
2209 for (i = index; i < p->header.nextindex; i++) {
2211 (struct ldtentry *) & p->slot[stbl[i]];
2212 modify_index(tid, ip,
2213 le32_to_cpu(ldtentry->index),
2214 bn, i, &imp, &lblock);
2217 release_metapage(imp);
2231 * free empty pages as propagating deletion up the tree
2237 static int dtDeleteUp(tid_t tid, struct inode *ip,
2238 struct metapage * fmp, dtpage_t * fp, struct btstack * btstack)
2241 struct metapage *mp;
2243 int index, nextindex;
2245 struct btframe *parent;
2246 struct dt_lock *dtlck;
2249 struct pxd_lock *pxdlock;
2253 * keep the root leaf page which has become empty
2255 if (BT_IS_ROOT(fmp)) {
2259 * dtInitRoot() acquires txlock on the root
2261 dtInitRoot(tid, ip, PARENT(ip));
2269 * free the non-root leaf page
2272 * acquire a transaction lock on the page
2274 * write FREEXTENT|NOREDOPAGE log record
2275 * N.B. linelock is overlaid as freed extent descriptor, and
2276 * the buffer page is freed;
2278 tlck = txMaplock(tid, ip, tlckDTREE | tlckFREE);
2279 pxdlock = (struct pxd_lock *) & tlck->lock;
2280 pxdlock->flag = mlckFREEPXD;
2281 pxdlock->pxd = fp->header.self;
2284 /* update sibling pointers */
2285 if ((rc = dtRelink(tid, ip, fp))) {
2290 xlen = lengthPXD(&fp->header.self);
2292 /* Free quota allocation. */
2293 dquot_free_block(ip, xlen);
2295 /* free/invalidate its buffer page */
2296 discard_metapage(fmp);
2299 * propagate page deletion up the directory tree
2301 * If the delete from the parent page makes it empty,
2302 * continue all the way up the tree.
2303 * stop if the root page is reached (which is never deleted) or
2304 * if the entry deletion does not empty the page.
2306 while ((parent = BT_POP(btstack)) != NULL) {
2307 /* pin the parent page <sp> */
2308 DT_GETPAGE(ip, parent->bn, mp, PSIZE, p, rc);
2313 * free the extent of the child page deleted
2315 index = parent->index;
2318 * delete the entry for the child page from parent
2320 nextindex = p->header.nextindex;
2323 * the parent has the single entry being deleted:
2325 * free the parent page which has become empty.
2327 if (nextindex == 1) {
2329 * keep the root internal page which has become empty
2331 if (p->header.flag & BT_ROOT) {
2335 * dtInitRoot() acquires txlock on the root
2337 dtInitRoot(tid, ip, PARENT(ip));
2344 * free the parent page
2348 * acquire a transaction lock on the page
2350 * write FREEXTENT|NOREDOPAGE log record
2354 tlckDTREE | tlckFREE);
2355 pxdlock = (struct pxd_lock *) & tlck->lock;
2356 pxdlock->flag = mlckFREEPXD;
2357 pxdlock->pxd = p->header.self;
2360 /* update sibling pointers */
2361 if ((rc = dtRelink(tid, ip, p))) {
2366 xlen = lengthPXD(&p->header.self);
2368 /* Free quota allocation */
2369 dquot_free_block(ip, xlen);
2371 /* free/invalidate its buffer page */
2372 discard_metapage(mp);
2380 * the parent has other entries remaining:
2382 * delete the router entry from the parent page.
2384 BT_MARK_DIRTY(mp, ip);
2386 * acquire a transaction lock on the page
2388 * action: router entry deletion
2390 tlck = txLock(tid, ip, mp, tlckDTREE | tlckENTRY);
2391 dtlck = (struct dt_lock *) & tlck->lock;
2393 /* linelock header */
2394 if (dtlck->index >= dtlck->maxcnt)
2395 dtlck = (struct dt_lock *) txLinelock(dtlck);
2396 lv = & dtlck->lv[dtlck->index];
2401 /* linelock stbl of non-root leaf page */
2402 if (!(p->header.flag & BT_ROOT)) {
2403 if (dtlck->index < dtlck->maxcnt)
2406 dtlck = (struct dt_lock *) txLinelock(dtlck);
2407 lv = & dtlck->lv[0];
2409 i = index >> L2DTSLOTSIZE;
2410 lv->offset = p->header.stblindex + i;
2412 ((p->header.nextindex - 1) >> L2DTSLOTSIZE) -
2417 /* free the router entry */
2418 dtDeleteEntry(p, index, &dtlck);
2420 /* reset key of new leftmost entry of level (for consistency) */
2422 ((p->header.flag & BT_ROOT) || p->header.prev == 0))
2423 dtTruncateEntry(p, 0, &dtlck);
2425 /* unpin the parent page */
2428 /* exit propagation up */
2433 ip->i_size -= PSIZE;
2440 * NAME: dtRelocate()
2442 * FUNCTION: relocate dtpage (internal or leaf) of directory;
2443 * This function is mainly used by defragfs utility.
2445 int dtRelocate(tid_t tid, struct inode *ip, s64 lmxaddr, pxd_t * opxd,
2449 struct metapage *mp, *pmp, *lmp, *rmp;
2450 dtpage_t *p, *pp, *rp = 0, *lp= 0;
2453 struct btstack btstack;
2455 s64 oxaddr, nextbn, prevbn;
2458 struct dt_lock *dtlck;
2459 struct pxd_lock *pxdlock;
2463 oxaddr = addressPXD(opxd);
2464 xlen = lengthPXD(opxd);
2466 jfs_info("dtRelocate: lmxaddr:%Ld xaddr:%Ld:%Ld xlen:%d",
2467 (long long)lmxaddr, (long long)oxaddr, (long long)nxaddr,
2471 * 1. get the internal parent dtpage covering
2472 * router entry for the tartget page to be relocated;
2474 rc = dtSearchNode(ip, lmxaddr, opxd, &btstack);
2478 /* retrieve search result */
2479 DT_GETSEARCH(ip, btstack.top, bn, pmp, pp, index);
2480 jfs_info("dtRelocate: parent router entry validated.");
2483 * 2. relocate the target dtpage
2485 /* read in the target page from src extent */
2486 DT_GETPAGE(ip, oxaddr, mp, PSIZE, p, rc);
2488 /* release the pinned parent page */
2494 * read in sibling pages if any to update sibling pointers;
2497 if (p->header.next) {
2498 nextbn = le64_to_cpu(p->header.next);
2499 DT_GETPAGE(ip, nextbn, rmp, PSIZE, rp, rc);
2508 if (p->header.prev) {
2509 prevbn = le64_to_cpu(p->header.prev);
2510 DT_GETPAGE(ip, prevbn, lmp, PSIZE, lp, rc);
2520 /* at this point, all xtpages to be updated are in memory */
2523 * update sibling pointers of sibling dtpages if any;
2526 tlck = txLock(tid, ip, lmp, tlckDTREE | tlckRELINK);
2527 dtlck = (struct dt_lock *) & tlck->lock;
2528 /* linelock header */
2529 ASSERT(dtlck->index == 0);
2530 lv = & dtlck->lv[0];
2535 lp->header.next = cpu_to_le64(nxaddr);
2540 tlck = txLock(tid, ip, rmp, tlckDTREE | tlckRELINK);
2541 dtlck = (struct dt_lock *) & tlck->lock;
2542 /* linelock header */
2543 ASSERT(dtlck->index == 0);
2544 lv = & dtlck->lv[0];
2549 rp->header.prev = cpu_to_le64(nxaddr);
2554 * update the target dtpage to be relocated
2556 * write LOG_REDOPAGE of LOG_NEW type for dst page
2557 * for the whole target page (logredo() will apply
2558 * after image and update bmap for allocation of the
2559 * dst extent), and update bmap for allocation of
2562 tlck = txLock(tid, ip, mp, tlckDTREE | tlckNEW);
2563 dtlck = (struct dt_lock *) & tlck->lock;
2564 /* linelock header */
2565 ASSERT(dtlck->index == 0);
2566 lv = & dtlck->lv[0];
2568 /* update the self address in the dtpage header */
2569 pxd = &p->header.self;
2570 PXDaddress(pxd, nxaddr);
2572 /* the dst page is the same as the src page, i.e.,
2573 * linelock for afterimage of the whole page;
2576 lv->length = p->header.maxslot;
2579 /* update the buffer extent descriptor of the dtpage */
2580 xsize = xlen << JFS_SBI(ip->i_sb)->l2bsize;
2582 /* unpin the relocated page */
2584 jfs_info("dtRelocate: target dtpage relocated.");
2586 /* the moved extent is dtpage, then a LOG_NOREDOPAGE log rec
2587 * needs to be written (in logredo(), the LOG_NOREDOPAGE log rec
2588 * will also force a bmap update ).
2592 * 3. acquire maplock for the source extent to be freed;
2594 /* for dtpage relocation, write a LOG_NOREDOPAGE record
2595 * for the source dtpage (logredo() will init NoRedoPage
2596 * filter and will also update bmap for free of the source
2597 * dtpage), and upadte bmap for free of the source dtpage;
2599 tlck = txMaplock(tid, ip, tlckDTREE | tlckFREE);
2600 pxdlock = (struct pxd_lock *) & tlck->lock;
2601 pxdlock->flag = mlckFREEPXD;
2602 PXDaddress(&pxdlock->pxd, oxaddr);
2603 PXDlength(&pxdlock->pxd, xlen);
2607 * 4. update the parent router entry for relocation;
2609 * acquire tlck for the parent entry covering the target dtpage;
2610 * write LOG_REDOPAGE to apply after image only;
2612 jfs_info("dtRelocate: update parent router entry.");
2613 tlck = txLock(tid, ip, pmp, tlckDTREE | tlckENTRY);
2614 dtlck = (struct dt_lock *) & tlck->lock;
2615 lv = & dtlck->lv[dtlck->index];
2617 /* update the PXD with the new address */
2618 stbl = DT_GETSTBL(pp);
2619 pxd = (pxd_t *) & pp->slot[stbl[index]];
2620 PXDaddress(pxd, nxaddr);
2621 lv->offset = stbl[index];
2625 /* unpin the parent dtpage */
2632 * NAME: dtSearchNode()
2634 * FUNCTION: Search for an dtpage containing a specified address
2635 * This function is mainly used by defragfs utility.
2637 * NOTE: Search result on stack, the found page is pinned at exit.
2638 * The result page must be an internal dtpage.
2639 * lmxaddr give the address of the left most page of the
2640 * dtree level, in which the required dtpage resides.
2642 static int dtSearchNode(struct inode *ip, s64 lmxaddr, pxd_t * kpxd,
2643 struct btstack * btstack)
2647 struct metapage *mp;
2649 int psize = 288; /* initial in-line directory */
2653 struct btframe *btsp;
2655 BT_CLR(btstack); /* reset stack */
2658 * descend tree to the level with specified leftmost page
2660 * by convention, root bn = 0.
2663 /* get/pin the page to search */
2664 DT_GETPAGE(ip, bn, mp, psize, p, rc);
2668 /* does the xaddr of leftmost page of the levevl
2669 * matches levevl search key ?
2671 if (p->header.flag & BT_ROOT) {
2674 } else if (addressPXD(&p->header.self) == lmxaddr)
2678 * descend down to leftmost child page
2680 if (p->header.flag & BT_LEAF) {
2685 /* get the leftmost entry */
2686 stbl = DT_GETSTBL(p);
2687 pxd = (pxd_t *) & p->slot[stbl[0]];
2689 /* get the child page block address */
2690 bn = addressPXD(pxd);
2691 psize = lengthPXD(pxd) << JFS_SBI(ip->i_sb)->l2bsize;
2692 /* unpin the parent page */
2697 * search each page at the current levevl
2700 stbl = DT_GETSTBL(p);
2701 for (i = 0; i < p->header.nextindex; i++) {
2702 pxd = (pxd_t *) & p->slot[stbl[i]];
2704 /* found the specified router entry */
2705 if (addressPXD(pxd) == addressPXD(kpxd) &&
2706 lengthPXD(pxd) == lengthPXD(kpxd)) {
2707 btsp = btstack->top;
2716 /* get the right sibling page if any */
2718 bn = le64_to_cpu(p->header.next);
2724 /* unpin current page */
2727 /* get the right sibling page */
2728 DT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
2734 #endif /* _NOTYET */
2740 * link around a freed page.
2743 * fp: page to be freed
2747 static int dtRelink(tid_t tid, struct inode *ip, dtpage_t * p)
2750 struct metapage *mp;
2753 struct dt_lock *dtlck;
2756 nextbn = le64_to_cpu(p->header.next);
2757 prevbn = le64_to_cpu(p->header.prev);
2759 /* update prev pointer of the next page */
2761 DT_GETPAGE(ip, nextbn, mp, PSIZE, p, rc);
2765 BT_MARK_DIRTY(mp, ip);
2767 * acquire a transaction lock on the next page
2769 * action: update prev pointer;
2771 tlck = txLock(tid, ip, mp, tlckDTREE | tlckRELINK);
2772 jfs_info("dtRelink nextbn: tlck = 0x%p, ip = 0x%p, mp=0x%p",
2774 dtlck = (struct dt_lock *) & tlck->lock;
2776 /* linelock header */
2777 if (dtlck->index >= dtlck->maxcnt)
2778 dtlck = (struct dt_lock *) txLinelock(dtlck);
2779 lv = & dtlck->lv[dtlck->index];
2784 p->header.prev = cpu_to_le64(prevbn);
2788 /* update next pointer of the previous page */
2790 DT_GETPAGE(ip, prevbn, mp, PSIZE, p, rc);
2794 BT_MARK_DIRTY(mp, ip);
2796 * acquire a transaction lock on the prev page
2798 * action: update next pointer;
2800 tlck = txLock(tid, ip, mp, tlckDTREE | tlckRELINK);
2801 jfs_info("dtRelink prevbn: tlck = 0x%p, ip = 0x%p, mp=0x%p",
2803 dtlck = (struct dt_lock *) & tlck->lock;
2805 /* linelock header */
2806 if (dtlck->index >= dtlck->maxcnt)
2807 dtlck = (struct dt_lock *) txLinelock(dtlck);
2808 lv = & dtlck->lv[dtlck->index];
2813 p->header.next = cpu_to_le64(nextbn);
2824 * initialize directory root (inline in inode)
2826 void dtInitRoot(tid_t tid, struct inode *ip, u32 idotdot)
2828 struct jfs_inode_info *jfs_ip = JFS_IP(ip);
2833 struct dt_lock *dtlck;
2838 * If this was previously an non-empty directory, we need to remove
2839 * the old directory table.
2842 if (!jfs_dirtable_inline(ip)) {
2843 struct tblock *tblk = tid_to_tblock(tid);
2845 * We're playing games with the tid's xflag. If
2846 * we're removing a regular file, the file's xtree
2847 * is committed with COMMIT_PMAP, but we always
2848 * commit the directories xtree with COMMIT_PWMAP.
2850 xflag_save = tblk->xflag;
2853 * xtTruncate isn't guaranteed to fully truncate
2854 * the xtree. The caller needs to check i_size
2855 * after committing the transaction to see if
2856 * additional truncation is needed. The
2857 * COMMIT_Stale flag tells caller that we
2858 * initiated the truncation.
2860 xtTruncate(tid, ip, 0, COMMIT_PWMAP);
2861 set_cflag(COMMIT_Stale, ip);
2863 tblk->xflag = xflag_save;
2867 jfs_ip->next_index = 2;
2869 ip->i_size = IDATASIZE;
2872 * acquire a transaction lock on the root
2874 * action: directory initialization;
2876 tlck = txLock(tid, ip, (struct metapage *) & jfs_ip->bxflag,
2877 tlckDTREE | tlckENTRY | tlckBTROOT);
2878 dtlck = (struct dt_lock *) & tlck->lock;
2881 ASSERT(dtlck->index == 0);
2882 lv = & dtlck->lv[0];
2884 lv->length = DTROOTMAXSLOT;
2887 p = &jfs_ip->i_dtroot;
2889 p->header.flag = DXD_INDEX | BT_ROOT | BT_LEAF;
2891 p->header.nextindex = 0;
2897 /* init data area of root */
2898 for (fsi++; fsi < DTROOTMAXSLOT; f++, fsi++)
2902 p->header.freelist = 1;
2903 p->header.freecnt = 8;
2905 /* init '..' entry */
2906 p->header.idotdot = cpu_to_le32(idotdot);
2912 * add_missing_indices()
2914 * function: Fix dtree page in which one or more entries has an invalid index.
2915 * fsck.jfs should really fix this, but it currently does not.
2916 * Called from jfs_readdir when bad index is detected.
2918 static void add_missing_indices(struct inode *inode, s64 bn)
2921 struct dt_lock *dtlck;
2925 struct metapage *mp;
2932 tid = txBegin(inode->i_sb, 0);
2934 DT_GETPAGE(inode, bn, mp, PSIZE, p, rc);
2937 printk(KERN_ERR "DT_GETPAGE failed!\n");
2940 BT_MARK_DIRTY(mp, inode);
2942 ASSERT(p->header.flag & BT_LEAF);
2944 tlck = txLock(tid, inode, mp, tlckDTREE | tlckENTRY);
2946 tlck->type |= tlckBTROOT;
2948 dtlck = (struct dt_lock *) &tlck->lock;
2950 stbl = DT_GETSTBL(p);
2951 for (i = 0; i < p->header.nextindex; i++) {
2952 d = (struct ldtentry *) &p->slot[stbl[i]];
2953 index = le32_to_cpu(d->index);
2954 if ((index < 2) || (index >= JFS_IP(inode)->next_index)) {
2955 d->index = cpu_to_le32(add_index(tid, inode, bn, i));
2956 if (dtlck->index >= dtlck->maxcnt)
2957 dtlck = (struct dt_lock *) txLinelock(dtlck);
2958 lv = &dtlck->lv[dtlck->index];
2959 lv->offset = stbl[i];
2966 (void) txCommit(tid, 1, &inode, 0);
2972 * Buffer to hold directory entry info while traversing a dtree page
2973 * before being fed to the filldir function
2983 * function to determine next variable-sized jfs_dirent in buffer
2985 static inline struct jfs_dirent *next_jfs_dirent(struct jfs_dirent *dirent)
2987 return (struct jfs_dirent *)
2989 ((sizeof (struct jfs_dirent) + dirent->name_len + 1 +
2990 sizeof (loff_t) - 1) &
2991 ~(sizeof (loff_t) - 1)));
2997 * function: read directory entries sequentially
2998 * from the specified entry offset
3002 * return: offset = (pn, index) of start entry
3003 * of next jfs_readdir()/dtRead()
3005 int jfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
3007 struct inode *ip = filp->f_path.dentry->d_inode;
3008 struct nls_table *codepage = JFS_SBI(ip->i_sb)->nls_tab;
3010 loff_t dtpos; /* legacy OS/2 style position */
3015 } *dtoffset = (struct dtoffset *) &dtpos;
3017 struct metapage *mp;
3021 struct btstack btstack;
3025 int d_namleft, len, outlen;
3026 unsigned long dirent_buf;
3030 uint loop_count = 0;
3031 struct jfs_dirent *jfs_dirent;
3033 int overflow, fix_page, page_fixed = 0;
3034 static int unique_pos = 2; /* If we can't fix broken index */
3036 if (filp->f_pos == DIREND)
3041 * persistent index is stored in directory entries.
3042 * Special cases: 0 = .
3044 * -1 = End of directory
3048 dir_index = (u32) filp->f_pos;
3050 if (dir_index > 1) {
3051 struct dir_table_slot dirtab_slot;
3054 (dir_index >= JFS_IP(ip)->next_index)) {
3055 /* Stale position. Directory has shrunk */
3056 filp->f_pos = DIREND;
3060 rc = read_index(ip, dir_index, &dirtab_slot);
3062 filp->f_pos = DIREND;
3065 if (dirtab_slot.flag == DIR_INDEX_FREE) {
3066 if (loop_count++ > JFS_IP(ip)->next_index) {
3067 jfs_err("jfs_readdir detected "
3069 filp->f_pos = DIREND;
3072 dir_index = le32_to_cpu(dirtab_slot.addr2);
3073 if (dir_index == -1) {
3074 filp->f_pos = DIREND;
3079 bn = addressDTS(&dirtab_slot);
3080 index = dirtab_slot.slot;
3081 DT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
3083 filp->f_pos = DIREND;
3086 if (p->header.flag & BT_INTERNAL) {
3087 jfs_err("jfs_readdir: bad index table");
3093 if (dir_index == 0) {
3098 if (filldir(dirent, ".", 1, 0, ip->i_ino,
3106 if (filldir(dirent, "..", 2, 1, PARENT(ip), DT_DIR))
3110 * Find first entry of left-most leaf
3113 filp->f_pos = DIREND;
3117 if ((rc = dtReadFirst(ip, &btstack)))
3120 DT_GETSEARCH(ip, btstack.top, bn, mp, p, index);
3124 * Legacy filesystem - OS/2 & Linux JFS < 0.3.6
3126 * pn = index = 0: First entry "."
3127 * pn = 0; index = 1: Second entry ".."
3128 * pn > 0: Real entries, pn=1 -> leftmost page
3129 * pn = index = -1: No more entries
3131 dtpos = filp->f_pos;
3133 /* build "." entry */
3135 if (filldir(dirent, ".", 1, filp->f_pos, ip->i_ino,
3138 dtoffset->index = 1;
3139 filp->f_pos = dtpos;
3142 if (dtoffset->pn == 0) {
3143 if (dtoffset->index == 1) {
3144 /* build ".." entry */
3146 if (filldir(dirent, "..", 2, filp->f_pos,
3147 PARENT(ip), DT_DIR))
3150 jfs_err("jfs_readdir called with "
3154 dtoffset->index = 0;
3155 filp->f_pos = dtpos;
3159 filp->f_pos = DIREND;
3163 if ((rc = dtReadNext(ip, &filp->f_pos, &btstack))) {
3164 jfs_err("jfs_readdir: unexpected rc = %d "
3165 "from dtReadNext", rc);
3166 filp->f_pos = DIREND;
3169 /* get start leaf page and index */
3170 DT_GETSEARCH(ip, btstack.top, bn, mp, p, index);
3172 /* offset beyond directory eof ? */
3174 filp->f_pos = DIREND;
3179 dirent_buf = __get_free_page(GFP_KERNEL);
3180 if (dirent_buf == 0) {
3182 jfs_warn("jfs_readdir: __get_free_page failed!");
3183 filp->f_pos = DIREND;
3188 jfs_dirent = (struct jfs_dirent *) dirent_buf;
3190 overflow = fix_page = 0;
3192 stbl = DT_GETSTBL(p);
3194 for (i = index; i < p->header.nextindex; i++) {
3195 d = (struct ldtentry *) & p->slot[stbl[i]];
3197 if (((long) jfs_dirent + d->namlen + 1) >
3198 (dirent_buf + PAGE_SIZE)) {
3199 /* DBCS codepages could overrun dirent_buf */
3205 d_namleft = d->namlen;
3206 name_ptr = jfs_dirent->name;
3207 jfs_dirent->ino = le32_to_cpu(d->inumber);
3210 len = min(d_namleft, DTLHDRDATALEN);
3211 jfs_dirent->position = le32_to_cpu(d->index);
3213 * d->index should always be valid, but it
3214 * isn't. fsck.jfs doesn't create the
3215 * directory index for the lost+found
3216 * directory. Rather than let it go,
3217 * we can try to fix it.
3219 if ((jfs_dirent->position < 2) ||
3220 (jfs_dirent->position >=
3221 JFS_IP(ip)->next_index)) {
3222 if (!page_fixed && !isReadOnly(ip)) {
3225 * setting overflow and setting
3226 * index to i will cause the
3227 * same page to be processed
3228 * again starting here
3234 jfs_dirent->position = unique_pos++;
3237 jfs_dirent->position = dtpos;
3238 len = min(d_namleft, DTLHDRDATALEN_LEGACY);
3241 /* copy the name of head/only segment */
3242 outlen = jfs_strfromUCS_le(name_ptr, d->name, len,
3244 jfs_dirent->name_len = outlen;
3246 /* copy name in the additional segment(s) */
3249 t = (struct dtslot *) & p->slot[next];
3253 if (d_namleft == 0) {
3255 "JFS:Dtree error: ino = "
3256 "%ld, bn=%Ld, index = %d",
3262 len = min(d_namleft, DTSLOTDATALEN);
3263 outlen = jfs_strfromUCS_le(name_ptr, t->name,
3265 jfs_dirent->name_len += outlen;
3271 jfs_dirent = next_jfs_dirent(jfs_dirent);
3278 /* Point to next leaf page */
3279 if (p->header.flag & BT_ROOT)
3282 bn = le64_to_cpu(p->header.next);
3284 /* update offset (pn:index) for new page */
3287 dtoffset->index = 0;
3293 /* unpin previous leaf page */
3296 jfs_dirent = (struct jfs_dirent *) dirent_buf;
3297 while (jfs_dirents--) {
3298 filp->f_pos = jfs_dirent->position;
3299 if (filldir(dirent, jfs_dirent->name,
3300 jfs_dirent->name_len, filp->f_pos,
3301 jfs_dirent->ino, DT_UNKNOWN))
3303 jfs_dirent = next_jfs_dirent(jfs_dirent);
3307 add_missing_indices(ip, bn);
3311 if (!overflow && (bn == 0)) {
3312 filp->f_pos = DIREND;
3316 DT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
3318 free_page(dirent_buf);
3324 free_page(dirent_buf);
3333 * function: get the leftmost page of the directory
3335 static int dtReadFirst(struct inode *ip, struct btstack * btstack)
3339 int psize = 288; /* initial in-line directory */
3340 struct metapage *mp;
3343 struct btframe *btsp;
3346 BT_CLR(btstack); /* reset stack */
3349 * descend leftmost path of the tree
3351 * by convention, root bn = 0.
3354 DT_GETPAGE(ip, bn, mp, psize, p, rc);
3359 * leftmost leaf page
3361 if (p->header.flag & BT_LEAF) {
3362 /* return leftmost entry */
3363 btsp = btstack->top;
3372 * descend down to leftmost child page
3374 if (BT_STACK_FULL(btstack)) {
3376 jfs_error(ip->i_sb, "dtReadFirst: btstack overrun");
3377 BT_STACK_DUMP(btstack);
3380 /* push (bn, index) of the parent page/entry */
3381 BT_PUSH(btstack, bn, 0);
3383 /* get the leftmost entry */
3384 stbl = DT_GETSTBL(p);
3385 xd = (pxd_t *) & p->slot[stbl[0]];
3387 /* get the child page block address */
3388 bn = addressPXD(xd);
3389 psize = lengthPXD(xd) << JFS_SBI(ip->i_sb)->l2bsize;
3391 /* unpin the parent page */
3400 * function: get the page of the specified offset (pn:index)
3402 * return: if (offset > eof), bn = -1;
3404 * note: if index > nextindex of the target leaf page,
3405 * start with 1st entry of next leaf page;
3407 static int dtReadNext(struct inode *ip, loff_t * offset,
3408 struct btstack * btstack)
3415 } *dtoffset = (struct dtoffset *) offset;
3417 struct metapage *mp;
3422 struct btframe *btsp, *parent;
3426 * get leftmost leaf page pinned
3428 if ((rc = dtReadFirst(ip, btstack)))
3432 DT_GETSEARCH(ip, btstack->top, bn, mp, p, index);
3434 /* get the start offset (pn:index) */
3435 pn = dtoffset->pn - 1; /* Now pn = 0 represents leftmost leaf */
3436 index = dtoffset->index;
3438 /* start at leftmost page ? */
3440 /* offset beyond eof ? */
3441 if (index < p->header.nextindex)
3444 if (p->header.flag & BT_ROOT) {
3449 /* start with 1st entry of next leaf page */
3451 dtoffset->index = index = 0;
3455 /* start at non-leftmost page: scan parent pages for large pn */
3456 if (p->header.flag & BT_ROOT) {
3461 /* start after next leaf page ? */
3465 /* get leaf page pn = 1 */
3467 bn = le64_to_cpu(p->header.next);
3469 /* unpin leaf page */
3472 /* offset beyond eof ? */
3481 * scan last internal page level to get target leaf page
3484 /* unpin leftmost leaf page */
3487 /* get left most parent page */
3488 btsp = btstack->top;
3491 DT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
3495 /* scan parent pages at last internal page level */
3496 while (pn >= p->header.nextindex) {
3497 pn -= p->header.nextindex;
3499 /* get next parent page address */
3500 bn = le64_to_cpu(p->header.next);
3502 /* unpin current parent page */
3505 /* offset beyond eof ? */
3511 /* get next parent page */
3512 DT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
3516 /* update parent page stack frame */
3520 /* get leaf page address */
3521 stbl = DT_GETSTBL(p);
3522 xd = (pxd_t *) & p->slot[stbl[pn]];
3523 bn = addressPXD(xd);
3525 /* unpin parent page */
3529 * get target leaf page
3532 DT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
3537 * leaf page has been completed:
3538 * start with 1st entry of next leaf page
3540 if (index >= p->header.nextindex) {
3541 bn = le64_to_cpu(p->header.next);
3543 /* unpin leaf page */
3546 /* offset beyond eof ? */
3552 /* get next leaf page */
3553 DT_GETPAGE(ip, bn, mp, PSIZE, p, rc);
3557 /* start with 1st entry of next leaf page */
3559 dtoffset->index = 0;
3563 /* return target leaf page pinned */
3564 btsp = btstack->top;
3566 btsp->index = dtoffset->index;
3576 * function: compare search key with an internal entry
3579 * < 0 if k is < record
3580 * = 0 if k is = record
3581 * > 0 if k is > record
3583 static int dtCompare(struct component_name * key, /* search key */
3584 dtpage_t * p, /* directory page */
3586 { /* entry slot index */
3589 int klen, namlen, len, rc;
3590 struct idtentry *ih;
3594 * force the left-most key on internal pages, at any level of
3595 * the tree, to be less than any search key.
3596 * this obviates having to update the leftmost key on an internal
3597 * page when the user inserts a new key in the tree smaller than
3598 * anything that has been stored.
3600 * (? if/when dtSearch() narrows down to 1st entry (index = 0),
3601 * at any internal page at any level of the tree,
3602 * it descends to child of the entry anyway -
3603 * ? make the entry as min size dummy entry)
3605 * if (e->index == 0 && h->prevpg == P_INVALID && !(h->flags & BT_LEAF))
3612 ih = (struct idtentry *) & p->slot[si];
3615 namlen = ih->namlen;
3616 len = min(namlen, DTIHDRDATALEN);
3618 /* compare with head/only segment */
3619 len = min(klen, len);
3620 if ((rc = UniStrncmp_le(kname, name, len)))
3626 /* compare with additional segment(s) */
3628 while (klen > 0 && namlen > 0) {
3629 /* compare with next name segment */
3630 t = (struct dtslot *) & p->slot[si];
3631 len = min(namlen, DTSLOTDATALEN);
3632 len = min(klen, len);
3634 if ((rc = UniStrncmp_le(kname, name, len)))
3643 return (klen - namlen);
3652 * function: compare search key with an (leaf/internal) entry
3655 * < 0 if k is < record
3656 * = 0 if k is = record
3657 * > 0 if k is > record
3659 static int ciCompare(struct component_name * key, /* search key */
3660 dtpage_t * p, /* directory page */
3661 int si, /* entry slot index */
3666 int klen, namlen, len, rc;
3667 struct ldtentry *lh;
3668 struct idtentry *ih;
3673 * force the left-most key on internal pages, at any level of
3674 * the tree, to be less than any search key.
3675 * this obviates having to update the leftmost key on an internal
3676 * page when the user inserts a new key in the tree smaller than
3677 * anything that has been stored.
3679 * (? if/when dtSearch() narrows down to 1st entry (index = 0),
3680 * at any internal page at any level of the tree,
3681 * it descends to child of the entry anyway -
3682 * ? make the entry as min size dummy entry)
3684 * if (e->index == 0 && h->prevpg == P_INVALID && !(h->flags & BT_LEAF))
3694 if (p->header.flag & BT_LEAF) {
3695 lh = (struct ldtentry *) & p->slot[si];
3698 namlen = lh->namlen;
3699 if (flag & JFS_DIR_INDEX)
3700 len = min(namlen, DTLHDRDATALEN);
3702 len = min(namlen, DTLHDRDATALEN_LEGACY);
3705 * internal page entry
3708 ih = (struct idtentry *) & p->slot[si];
3711 namlen = ih->namlen;
3712 len = min(namlen, DTIHDRDATALEN);
3715 /* compare with head/only segment */
3716 len = min(klen, len);
3717 for (i = 0; i < len; i++, kname++, name++) {
3718 /* only uppercase if case-insensitive support is on */
3719 if ((flag & JFS_OS2) == JFS_OS2)
3720 x = UniToupper(le16_to_cpu(*name));
3722 x = le16_to_cpu(*name);
3723 if ((rc = *kname - x))
3730 /* compare with additional segment(s) */
3731 while (klen > 0 && namlen > 0) {
3732 /* compare with next name segment */
3733 t = (struct dtslot *) & p->slot[si];
3734 len = min(namlen, DTSLOTDATALEN);
3735 len = min(klen, len);
3737 for (i = 0; i < len; i++, kname++, name++) {
3738 /* only uppercase if case-insensitive support is on */
3739 if ((flag & JFS_OS2) == JFS_OS2)
3740 x = UniToupper(le16_to_cpu(*name));
3742 x = le16_to_cpu(*name);
3744 if ((rc = *kname - x))
3753 return (klen - namlen);
3758 * ciGetLeafPrefixKey()
3760 * function: compute prefix of suffix compression
3761 * from two adjacent leaf entries
3762 * across page boundary
3764 * return: non-zero on error
3767 static int ciGetLeafPrefixKey(dtpage_t * lp, int li, dtpage_t * rp,
3768 int ri, struct component_name * key, int flag)
3771 wchar_t *pl, *pr, *kname;
3772 struct component_name lkey;
3773 struct component_name rkey;
3775 lkey.name = kmalloc((JFS_NAME_MAX + 1) * sizeof(wchar_t),
3777 if (lkey.name == NULL)
3780 rkey.name = kmalloc((JFS_NAME_MAX + 1) * sizeof(wchar_t),
3782 if (rkey.name == NULL) {
3787 /* get left and right key */
3788 dtGetKey(lp, li, &lkey, flag);
3789 lkey.name[lkey.namlen] = 0;
3791 if ((flag & JFS_OS2) == JFS_OS2)
3794 dtGetKey(rp, ri, &rkey, flag);
3795 rkey.name[rkey.namlen] = 0;
3798 if ((flag & JFS_OS2) == JFS_OS2)
3801 /* compute prefix */
3804 namlen = min(lkey.namlen, rkey.namlen);
3805 for (pl = lkey.name, pr = rkey.name;
3806 namlen; pl++, pr++, namlen--, klen++, kname++) {
3809 key->namlen = klen + 1;
3814 /* l->namlen <= r->namlen since l <= r */
3815 if (lkey.namlen < rkey.namlen) {
3817 key->namlen = klen + 1;
3818 } else /* l->namelen == r->namelen */
3832 * function: get key of the entry
3834 static void dtGetKey(dtpage_t * p, int i, /* entry index */
3835 struct component_name * key, int flag)
3839 struct ldtentry *lh;
3840 struct idtentry *ih;
3847 stbl = DT_GETSTBL(p);
3849 if (p->header.flag & BT_LEAF) {
3850 lh = (struct ldtentry *) & p->slot[si];
3852 namlen = lh->namlen;
3854 if (flag & JFS_DIR_INDEX)
3855 len = min(namlen, DTLHDRDATALEN);
3857 len = min(namlen, DTLHDRDATALEN_LEGACY);
3859 ih = (struct idtentry *) & p->slot[si];
3861 namlen = ih->namlen;
3863 len = min(namlen, DTIHDRDATALEN);
3866 key->namlen = namlen;
3870 * move head/only segment
3872 UniStrncpy_from_le(kname, name, len);
3875 * move additional segment(s)
3878 /* get next segment */
3882 len = min(namlen, DTSLOTDATALEN);
3883 UniStrncpy_from_le(kname, t->name, len);
3893 * function: allocate free slot(s) and
3894 * write a leaf/internal entry
3896 * return: entry slot index
3898 static void dtInsertEntry(dtpage_t * p, int index, struct component_name * key,
3899 ddata_t * data, struct dt_lock ** dtlock)
3901 struct dtslot *h, *t;
3902 struct ldtentry *lh = NULL;
3903 struct idtentry *ih = NULL;
3904 int hsi, fsi, klen, len, nextindex;
3909 struct dt_lock *dtlck = *dtlock;
3913 struct metapage *mp = NULL;
3918 /* allocate a free slot */
3919 hsi = fsi = p->header.freelist;
3921 p->header.freelist = h->next;
3922 --p->header.freecnt;
3924 /* open new linelock */
3925 if (dtlck->index >= dtlck->maxcnt)
3926 dtlck = (struct dt_lock *) txLinelock(dtlck);
3928 lv = & dtlck->lv[dtlck->index];
3931 /* write head/only segment */
3932 if (p->header.flag & BT_LEAF) {
3933 lh = (struct ldtentry *) h;
3935 lh->inumber = cpu_to_le32(data->leaf.ino);
3938 if (data->leaf.ip) {
3939 len = min(klen, DTLHDRDATALEN);
3940 if (!(p->header.flag & BT_ROOT))
3941 bn = addressPXD(&p->header.self);
3942 lh->index = cpu_to_le32(add_index(data->leaf.tid,
3946 len = min(klen, DTLHDRDATALEN_LEGACY);
3948 ih = (struct idtentry *) h;
3954 len = min(klen, DTIHDRDATALEN);
3957 UniStrncpy_to_le(name, kname, len);
3962 /* write additional segment(s) */
3967 fsi = p->header.freelist;
3969 p->header.freelist = t->next;
3970 --p->header.freecnt;
3972 /* is next slot contiguous ? */
3973 if (fsi != xsi + 1) {
3974 /* close current linelock */
3978 /* open new linelock */
3979 if (dtlck->index < dtlck->maxcnt)
3982 dtlck = (struct dt_lock *) txLinelock(dtlck);
3983 lv = & dtlck->lv[0];
3991 len = min(klen, DTSLOTDATALEN);
3992 UniStrncpy_to_le(t->name, kname, len);
3999 /* close current linelock */
4005 /* terminate last/only segment */
4007 /* single segment entry */
4008 if (p->header.flag & BT_LEAF)
4013 /* multi-segment entry */
4016 /* if insert into middle, shift right succeeding entries in stbl */
4017 stbl = DT_GETSTBL(p);
4018 nextindex = p->header.nextindex;
4019 if (index < nextindex) {
4020 memmove(stbl + index + 1, stbl + index, nextindex - index);
4022 if ((p->header.flag & BT_LEAF) && data->leaf.ip) {
4026 * Need to update slot number for entries that moved
4030 for (n = index + 1; n <= nextindex; n++) {
4031 lh = (struct ldtentry *) & (p->slot[stbl[n]]);
4032 modify_index(data->leaf.tid, data->leaf.ip,
4033 le32_to_cpu(lh->index), bn, n,
4037 release_metapage(mp);
4043 /* advance next available entry index of stbl */
4044 ++p->header.nextindex;
4051 * function: move entries from split/left page to new/right page
4053 * nextindex of dst page and freelist/freecnt of both pages
4056 static void dtMoveEntry(dtpage_t * sp, int si, dtpage_t * dp,
4057 struct dt_lock ** sdtlock, struct dt_lock ** ddtlock,
4060 int ssi, next; /* src slot index */
4061 int di; /* dst entry index */
4062 int dsi; /* dst slot index */
4063 s8 *sstbl, *dstbl; /* sorted entry table */
4065 struct ldtentry *slh, *dlh = NULL;
4066 struct idtentry *sih, *dih = NULL;
4067 struct dtslot *h, *s, *d;
4068 struct dt_lock *sdtlck = *sdtlock, *ddtlck = *ddtlock;
4069 struct lv *slv, *dlv;
4073 sstbl = (s8 *) & sp->slot[sp->header.stblindex];
4074 dstbl = (s8 *) & dp->slot[dp->header.stblindex];
4076 dsi = dp->header.freelist; /* first (whole page) free slot */
4077 sfsi = sp->header.freelist;
4079 /* linelock destination entry slot */
4080 dlv = & ddtlck->lv[ddtlck->index];
4083 /* linelock source entry slot */
4084 slv = & sdtlck->lv[sdtlck->index];
4085 slv->offset = sstbl[si];
4086 xssi = slv->offset - 1;
4092 for (di = 0; si < sp->header.nextindex; si++, di++) {
4096 /* is next slot contiguous ? */
4097 if (ssi != xssi + 1) {
4098 /* close current linelock */
4102 /* open new linelock */
4103 if (sdtlck->index < sdtlck->maxcnt)
4106 sdtlck = (struct dt_lock *) txLinelock(sdtlck);
4107 slv = & sdtlck->lv[0];
4115 * move head/only segment of an entry
4118 h = d = &dp->slot[dsi];
4120 /* get src slot and move */
4122 if (sp->header.flag & BT_LEAF) {
4123 /* get source entry */
4124 slh = (struct ldtentry *) s;
4125 dlh = (struct ldtentry *) h;
4126 snamlen = slh->namlen;
4129 len = min(snamlen, DTLHDRDATALEN);
4130 dlh->index = slh->index; /* little-endian */
4132 len = min(snamlen, DTLHDRDATALEN_LEGACY);
4134 memcpy(dlh, slh, 6 + len * 2);
4138 /* update dst head/only segment next field */
4142 sih = (struct idtentry *) s;
4143 snamlen = sih->namlen;
4145 len = min(snamlen, DTIHDRDATALEN);
4146 dih = (struct idtentry *) h;
4147 memcpy(dih, sih, 10 + len * 2);
4154 /* free src head/only segment */
4164 * move additional segment(s) of the entry
4167 while ((ssi = next) >= 0) {
4168 /* is next slot contiguous ? */
4169 if (ssi != xssi + 1) {
4170 /* close current linelock */
4174 /* open new linelock */
4175 if (sdtlck->index < sdtlck->maxcnt)
4181 slv = & sdtlck->lv[0];
4188 /* get next source segment */
4191 /* get next destination free slot */
4194 len = min(snamlen, DTSLOTDATALEN);
4195 UniStrncpy_le(d->name, s->name, len);
4204 /* free source segment */
4213 /* terminate dst last/only segment */
4215 /* single segment entry */
4216 if (dp->header.flag & BT_LEAF)
4221 /* multi-segment entry */
4225 /* close current linelock */
4234 /* update source header */
4235 sp->header.freelist = sfsi;
4236 sp->header.freecnt += nd;
4238 /* update destination header */
4239 dp->header.nextindex = di;
4241 dp->header.freelist = dsi;
4242 dp->header.freecnt -= nd;
4249 * function: free a (leaf/internal) entry
4251 * log freelist header, stbl, and each segment slot of entry
4252 * (even though last/only segment next field is modified,
4253 * physical image logging requires all segment slots of
4254 * the entry logged to avoid applying previous updates
4255 * to the same slots)
4257 static void dtDeleteEntry(dtpage_t * p, int fi, struct dt_lock ** dtlock)
4259 int fsi; /* free entry slot index */
4263 struct dt_lock *dtlck = *dtlock;
4267 /* get free entry slot index */
4268 stbl = DT_GETSTBL(p);
4271 /* open new linelock */
4272 if (dtlck->index >= dtlck->maxcnt)
4273 dtlck = (struct dt_lock *) txLinelock(dtlck);
4274 lv = & dtlck->lv[dtlck->index];
4278 /* get the head/only segment */
4280 if (p->header.flag & BT_LEAF)
4281 si = ((struct ldtentry *) t)->next;
4283 si = ((struct idtentry *) t)->next;
4290 /* find the last/only segment */
4292 /* is next slot contiguous ? */
4293 if (si != xsi + 1) {
4294 /* close current linelock */
4298 /* open new linelock */
4299 if (dtlck->index < dtlck->maxcnt)
4302 dtlck = (struct dt_lock *) txLinelock(dtlck);
4303 lv = & dtlck->lv[0];
4319 /* close current linelock */
4325 /* update freelist */
4326 t->next = p->header.freelist;
4327 p->header.freelist = fsi;
4328 p->header.freecnt += freecnt;
4330 /* if delete from middle,
4331 * shift left the succedding entries in the stbl
4333 si = p->header.nextindex;
4335 memmove(&stbl[fi], &stbl[fi + 1], si - fi - 1);
4337 p->header.nextindex--;
4344 * function: truncate a (leaf/internal) entry
4346 * log freelist header, stbl, and each segment slot of entry
4347 * (even though last/only segment next field is modified,
4348 * physical image logging requires all segment slots of
4349 * the entry logged to avoid applying previous updates
4350 * to the same slots)
4352 static void dtTruncateEntry(dtpage_t * p, int ti, struct dt_lock ** dtlock)
4354 int tsi; /* truncate entry slot index */
4358 struct dt_lock *dtlck = *dtlock;
4362 /* get free entry slot index */
4363 stbl = DT_GETSTBL(p);
4366 /* open new linelock */
4367 if (dtlck->index >= dtlck->maxcnt)
4368 dtlck = (struct dt_lock *) txLinelock(dtlck);
4369 lv = & dtlck->lv[dtlck->index];
4373 /* get the head/only segment */
4375 ASSERT(p->header.flag & BT_INTERNAL);
4376 ((struct idtentry *) t)->namlen = 0;
4377 si = ((struct idtentry *) t)->next;
4378 ((struct idtentry *) t)->next = -1;
4385 /* find the last/only segment */
4387 /* is next slot contiguous ? */
4388 if (si != xsi + 1) {
4389 /* close current linelock */
4393 /* open new linelock */
4394 if (dtlck->index < dtlck->maxcnt)
4397 dtlck = (struct dt_lock *) txLinelock(dtlck);
4398 lv = & dtlck->lv[0];
4414 /* close current linelock */
4420 /* update freelist */
4423 t->next = p->header.freelist;
4424 p->header.freelist = fsi;
4425 p->header.freecnt += freecnt;
4430 * dtLinelockFreelist()
4432 static void dtLinelockFreelist(dtpage_t * p, /* directory page */
4433 int m, /* max slot index */
4434 struct dt_lock ** dtlock)
4436 int fsi; /* free entry slot index */
4439 struct dt_lock *dtlck = *dtlock;
4443 /* get free entry slot index */
4444 fsi = p->header.freelist;
4446 /* open new linelock */
4447 if (dtlck->index >= dtlck->maxcnt)
4448 dtlck = (struct dt_lock *) txLinelock(dtlck);
4449 lv = & dtlck->lv[dtlck->index];
4459 /* find the last/only segment */
4460 while (si < m && si >= 0) {
4461 /* is next slot contiguous ? */
4462 if (si != xsi + 1) {
4463 /* close current linelock */
4467 /* open new linelock */
4468 if (dtlck->index < dtlck->maxcnt)
4471 dtlck = (struct dt_lock *) txLinelock(dtlck);
4472 lv = & dtlck->lv[0];
4486 /* close current linelock */
4497 * FUNCTION: Modify the inode number part of a directory entry
4500 * tid - Transaction id
4501 * ip - Inode of parent directory
4502 * key - Name of entry to be modified
4503 * orig_ino - Original inode number expected in entry
4504 * new_ino - New inode number to put into entry
4508 * -ESTALE - If entry found does not match orig_ino passed in
4509 * -ENOENT - If no entry can be found to match key
4510 * 0 - If successfully modified entry
4512 int dtModify(tid_t tid, struct inode *ip,
4513 struct component_name * key, ino_t * orig_ino, ino_t new_ino, int flag)
4517 struct metapage *mp;
4520 struct btstack btstack;
4522 struct dt_lock *dtlck;
4525 int entry_si; /* entry slot index */
4526 struct ldtentry *entry;
4529 * search for the entry to modify:
4531 * dtSearch() returns (leaf page pinned, index at which to modify).
4533 if ((rc = dtSearch(ip, key, orig_ino, &btstack, flag)))
4536 /* retrieve search result */
4537 DT_GETSEARCH(ip, btstack.top, bn, mp, p, index);
4539 BT_MARK_DIRTY(mp, ip);
4541 * acquire a transaction lock on the leaf page of named entry
4543 tlck = txLock(tid, ip, mp, tlckDTREE | tlckENTRY);
4544 dtlck = (struct dt_lock *) & tlck->lock;
4546 /* get slot index of the entry */
4547 stbl = DT_GETSTBL(p);
4548 entry_si = stbl[index];
4550 /* linelock entry */
4551 ASSERT(dtlck->index == 0);
4552 lv = & dtlck->lv[0];
4553 lv->offset = entry_si;
4557 /* get the head/only segment */
4558 entry = (struct ldtentry *) & p->slot[entry_si];
4560 /* substitute the inode number of the entry */
4561 entry->inumber = cpu_to_le32(new_ino);
4563 /* unpin the leaf page */