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 "jfs_incore.h"
106 #include "jfs_superblock.h"
107 #include "jfs_filsys.h"
108 #include "jfs_metapage.h"
109 #include "jfs_dmap.h"
110 #include "jfs_unicode.h"
111 #include "jfs_debug.h"
113 /* dtree split parameter */
118 struct component_name
*key
;
120 struct pxdlist
*pxdlist
;
123 #define DT_PAGE(IP, MP) BT_PAGE(IP, MP, dtpage_t, i_dtroot)
125 /* get page buffer for specified block address */
126 #define DT_GETPAGE(IP, BN, MP, SIZE, P, RC)\
128 BT_GETPAGE(IP, BN, MP, dtpage_t, SIZE, P, RC, i_dtroot)\
131 if (((P)->header.nextindex > (((BN)==0)?DTROOTMAXSLOT:(P)->header.maxslot)) ||\
132 ((BN) && ((P)->header.maxslot > DTPAGEMAXSLOT)))\
135 jfs_error((IP)->i_sb, "DT_GETPAGE: dtree page corrupt");\
142 /* for consistency */
143 #define DT_PUTPAGE(MP) BT_PUTPAGE(MP)
145 #define DT_GETSEARCH(IP, LEAF, BN, MP, P, INDEX) \
146 BT_GETSEARCH(IP, LEAF, BN, MP, dtpage_t, P, INDEX, i_dtroot)
151 static int dtSplitUp(tid_t tid
, struct inode
*ip
,
152 struct dtsplit
* split
, struct btstack
* btstack
);
154 static int dtSplitPage(tid_t tid
, struct inode
*ip
, struct dtsplit
* split
,
155 struct metapage
** rmpp
, dtpage_t
** rpp
, pxd_t
* rxdp
);
157 static int dtExtendPage(tid_t tid
, struct inode
*ip
,
158 struct dtsplit
* split
, struct btstack
* btstack
);
160 static int dtSplitRoot(tid_t tid
, struct inode
*ip
,
161 struct dtsplit
* split
, struct metapage
** rmpp
);
163 static int dtDeleteUp(tid_t tid
, struct inode
*ip
, struct metapage
* fmp
,
164 dtpage_t
* fp
, struct btstack
* btstack
);
166 static int dtRelink(tid_t tid
, struct inode
*ip
, dtpage_t
* p
);
168 static int dtReadFirst(struct inode
*ip
, struct btstack
* btstack
);
170 static int dtReadNext(struct inode
*ip
,
171 loff_t
* offset
, struct btstack
* btstack
);
173 static int dtCompare(struct component_name
* key
, dtpage_t
* p
, int si
);
175 static int ciCompare(struct component_name
* key
, dtpage_t
* p
, int si
,
178 static void dtGetKey(dtpage_t
* p
, int i
, struct component_name
* key
,
181 static int ciGetLeafPrefixKey(dtpage_t
* lp
, int li
, dtpage_t
* rp
,
182 int ri
, struct component_name
* key
, int flag
);
184 static void dtInsertEntry(dtpage_t
* p
, int index
, struct component_name
* key
,
185 ddata_t
* data
, struct dt_lock
**);
187 static void dtMoveEntry(dtpage_t
* sp
, int si
, dtpage_t
* dp
,
188 struct dt_lock
** sdtlock
, struct dt_lock
** ddtlock
,
191 static void dtDeleteEntry(dtpage_t
* p
, int fi
, struct dt_lock
** dtlock
);
193 static void dtTruncateEntry(dtpage_t
* p
, int ti
, struct dt_lock
** dtlock
);
195 static void dtLinelockFreelist(dtpage_t
* p
, int m
, struct dt_lock
** dtlock
);
197 #define ciToUpper(c) UniStrupr((c)->name)
202 * Reads a page of a directory's index table.
203 * Having metadata mapped into the directory inode's address space
204 * presents a multitude of problems. We avoid this by mapping to
205 * the absolute address space outside of the *_metapage routines
207 static struct metapage
*read_index_page(struct inode
*inode
, s64 blkno
)
214 rc
= xtLookup(inode
, blkno
, 1, &xflag
, &xaddr
, &xlen
, 1);
215 if (rc
|| (xaddr
== 0))
218 return read_metapage(inode
, xaddr
, PSIZE
, 1);
224 * Same as get_index_page(), but get's a new page without reading
226 static struct metapage
*get_index_page(struct inode
*inode
, s64 blkno
)
233 rc
= xtLookup(inode
, blkno
, 1, &xflag
, &xaddr
, &xlen
, 1);
234 if (rc
|| (xaddr
== 0))
237 return get_metapage(inode
, xaddr
, PSIZE
, 1);
243 * Returns dtree page containing directory table entry for specified
244 * index and pointer to its entry.
246 * mp must be released by caller.
248 static struct dir_table_slot
*find_index(struct inode
*ip
, u32 index
,
249 struct metapage
** mp
, s64
*lblock
)
251 struct jfs_inode_info
*jfs_ip
= JFS_IP(ip
);
255 struct dir_table_slot
*slot
;
256 static int maxWarnings
= 10;
260 jfs_warn("find_entry called with index = %d", index
);
266 if (index
>= jfs_ip
->next_index
) {
267 jfs_warn("find_entry called with index >= next_index");
271 if (jfs_dirtable_inline(ip
)) {
273 * Inline directory table
276 slot
= &jfs_ip
->i_dirtable
[index
- 2];
278 offset
= (index
- 2) * sizeof(struct dir_table_slot
);
279 page_offset
= offset
& (PSIZE
- 1);
280 blkno
= ((offset
+ 1) >> L2PSIZE
) <<
281 JFS_SBI(ip
->i_sb
)->l2nbperpage
;
283 if (*mp
&& (*lblock
!= blkno
)) {
284 release_metapage(*mp
);
289 *mp
= read_index_page(ip
, blkno
);
292 jfs_err("free_index: error reading directory table");
297 (struct dir_table_slot
*) ((char *) (*mp
)->data
+
303 static inline void lock_index(tid_t tid
, struct inode
*ip
, struct metapage
* mp
,
307 struct linelock
*llck
;
310 tlck
= txLock(tid
, ip
, mp
, tlckDATA
);
311 llck
= (struct linelock
*) tlck
->lock
;
313 if (llck
->index
>= llck
->maxcnt
)
314 llck
= txLinelock(llck
);
315 lv
= &llck
->lv
[llck
->index
];
318 * Linelock slot size is twice the size of directory table
319 * slot size. 512 entries per page.
321 lv
->offset
= ((index
- 2) & 511) >> 1;
329 * Adds an entry to the directory index table. This is used to provide
330 * each directory entry with a persistent index in which to resume
331 * directory traversals
333 static u32
add_index(tid_t tid
, struct inode
*ip
, s64 bn
, int slot
)
335 struct super_block
*sb
= ip
->i_sb
;
336 struct jfs_sb_info
*sbi
= JFS_SBI(sb
);
337 struct jfs_inode_info
*jfs_ip
= JFS_IP(ip
);
339 struct dir_table_slot
*dirtab_slot
;
341 struct linelock
*llck
;
349 ASSERT(DO_INDEX(ip
));
351 if (jfs_ip
->next_index
< 2) {
352 jfs_warn("add_index: next_index = %d. Resetting!",
354 jfs_ip
->next_index
= 2;
357 index
= jfs_ip
->next_index
++;
359 if (index
<= MAX_INLINE_DIRTABLE_ENTRY
) {
361 * i_size reflects size of index table, or 8 bytes per entry.
363 ip
->i_size
= (loff_t
) (index
- 1) << 3;
366 * dir table fits inline within inode
368 dirtab_slot
= &jfs_ip
->i_dirtable
[index
-2];
369 dirtab_slot
->flag
= DIR_INDEX_VALID
;
370 dirtab_slot
->slot
= slot
;
371 DTSaddress(dirtab_slot
, bn
);
373 set_cflag(COMMIT_Dirtable
, ip
);
377 if (index
== (MAX_INLINE_DIRTABLE_ENTRY
+ 1)) {
378 struct dir_table_slot temp_table
[12];
381 * It's time to move the inline table to an external
382 * page and begin to build the xtree
384 if (DQUOT_ALLOC_BLOCK(ip
, sbi
->nbperpage
) ||
385 dbAlloc(ip
, 0, sbi
->nbperpage
, &xaddr
))
386 goto clean_up
; /* No space */
389 * Save the table, we're going to overwrite it with the
392 memcpy(temp_table
, &jfs_ip
->i_dirtable
, sizeof(temp_table
));
395 * Initialize empty x-tree
400 * Allocate the first block & add it to the xtree
402 if (xtInsert(tid
, ip
, 0, 0, sbi
->nbperpage
, &xaddr
, 0)) {
403 /* This really shouldn't fail */
404 jfs_warn("add_index: xtInsert failed!");
405 memcpy(&jfs_ip
->i_dirtable
, temp_table
,
406 sizeof (temp_table
));
411 if ((mp
= get_index_page(ip
, 0)) == 0) {
412 jfs_err("add_index: get_metapage failed!");
413 xtTruncate(tid
, ip
, 0, COMMIT_PWMAP
);
414 memcpy(&jfs_ip
->i_dirtable
, temp_table
,
415 sizeof (temp_table
));
418 tlck
= txLock(tid
, ip
, mp
, tlckDATA
);
419 llck
= (struct linelock
*) & tlck
->lock
;
420 ASSERT(llck
->index
== 0);
424 lv
->length
= 6; /* tlckDATA slot size is 16 bytes */
427 memcpy(mp
->data
, temp_table
, sizeof(temp_table
));
429 mark_metapage_dirty(mp
);
430 release_metapage(mp
);
433 * Logging is now directed by xtree tlocks
435 clear_cflag(COMMIT_Dirtable
, ip
);
438 offset
= (index
- 2) * sizeof(struct dir_table_slot
);
439 page_offset
= offset
& (PSIZE
- 1);
440 blkno
= ((offset
+ 1) >> L2PSIZE
) << sbi
->l2nbperpage
;
441 if (page_offset
== 0) {
443 * This will be the beginning of a new page
446 if (xtInsert(tid
, ip
, 0, blkno
, sbi
->nbperpage
, &xaddr
, 0)) {
447 jfs_warn("add_index: xtInsert failed!");
452 if ((mp
= get_index_page(ip
, blkno
)))
453 memset(mp
->data
, 0, PSIZE
); /* Just looks better */
455 xtTruncate(tid
, ip
, offset
, COMMIT_PWMAP
);
457 mp
= read_index_page(ip
, blkno
);
460 jfs_err("add_index: get/read_metapage failed!");
464 lock_index(tid
, ip
, mp
, index
);
467 (struct dir_table_slot
*) ((char *) mp
->data
+ page_offset
);
468 dirtab_slot
->flag
= DIR_INDEX_VALID
;
469 dirtab_slot
->slot
= slot
;
470 DTSaddress(dirtab_slot
, bn
);
472 mark_metapage_dirty(mp
);
473 release_metapage(mp
);
479 jfs_ip
->next_index
--;
487 * Marks an entry to the directory index table as free.
489 static void free_index(tid_t tid
, struct inode
*ip
, u32 index
, u32 next
)
491 struct dir_table_slot
*dirtab_slot
;
493 struct metapage
*mp
= NULL
;
495 dirtab_slot
= find_index(ip
, index
, &mp
, &lblock
);
497 if (dirtab_slot
== 0)
500 dirtab_slot
->flag
= DIR_INDEX_FREE
;
501 dirtab_slot
->slot
= dirtab_slot
->addr1
= 0;
502 dirtab_slot
->addr2
= cpu_to_le32(next
);
505 lock_index(tid
, ip
, mp
, index
);
506 mark_metapage_dirty(mp
);
507 release_metapage(mp
);
509 set_cflag(COMMIT_Dirtable
, ip
);
515 * Changes an entry in the directory index table
517 static void modify_index(tid_t tid
, struct inode
*ip
, u32 index
, s64 bn
,
518 int slot
, struct metapage
** mp
, u64
*lblock
)
520 struct dir_table_slot
*dirtab_slot
;
522 dirtab_slot
= find_index(ip
, index
, mp
, lblock
);
524 if (dirtab_slot
== 0)
527 DTSaddress(dirtab_slot
, bn
);
528 dirtab_slot
->slot
= slot
;
531 lock_index(tid
, ip
, *mp
, index
);
532 mark_metapage_dirty(*mp
);
534 set_cflag(COMMIT_Dirtable
, ip
);
540 * reads a directory table slot
542 static int read_index(struct inode
*ip
, u32 index
,
543 struct dir_table_slot
* dirtab_slot
)
546 struct metapage
*mp
= NULL
;
547 struct dir_table_slot
*slot
;
549 slot
= find_index(ip
, index
, &mp
, &lblock
);
554 memcpy(dirtab_slot
, slot
, sizeof(struct dir_table_slot
));
557 release_metapage(mp
);
566 * Search for the entry with specified key
570 * return: 0 - search result on stack, leaf page pinned;
573 int dtSearch(struct inode
*ip
, struct component_name
* key
, ino_t
* data
,
574 struct btstack
* btstack
, int flag
)
577 int cmp
= 1; /* init for empty page */
582 int base
, index
, lim
;
583 struct btframe
*btsp
;
585 int psize
= 288; /* initial in-line directory */
587 struct component_name ciKey
;
588 struct super_block
*sb
= ip
->i_sb
;
591 (wchar_t *) kmalloc((JFS_NAME_MAX
+ 1) * sizeof(wchar_t),
593 if (ciKey
.name
== 0) {
599 /* uppercase search key for c-i directory */
600 UniStrcpy(ciKey
.name
, key
->name
);
601 ciKey
.namlen
= key
->namlen
;
603 /* only uppercase if case-insensitive support is on */
604 if ((JFS_SBI(sb
)->mntflag
& JFS_OS2
) == JFS_OS2
) {
607 BT_CLR(btstack
); /* reset stack */
609 /* init level count for max pages to split */
613 * search down tree from root:
615 * between two consecutive entries of <Ki, Pi> and <Kj, Pj> of
616 * internal page, child page Pi contains entry with k, Ki <= K < Kj.
618 * if entry with search key K is not found
619 * internal page search find the entry with largest key Ki
620 * less than K which point to the child page to search;
621 * leaf page search find the entry with smallest key Kj
622 * greater than K so that the returned index is the position of
623 * the entry to be shifted right for insertion of new entry.
624 * for empty tree, search key is greater than any key of the tree.
626 * by convention, root bn = 0.
629 /* get/pin the page to search */
630 DT_GETPAGE(ip
, bn
, mp
, psize
, p
, rc
);
634 /* get sorted entry table of the page */
635 stbl
= DT_GETSTBL(p
);
638 * binary search with search key K on the current page.
640 for (base
= 0, lim
= p
->header
.nextindex
; lim
; lim
>>= 1) {
641 index
= base
+ (lim
>> 1);
643 if (p
->header
.flag
& BT_LEAF
) {
644 /* uppercase leaf name to compare */
646 ciCompare(&ciKey
, p
, stbl
[index
],
647 JFS_SBI(sb
)->mntflag
);
649 /* router key is in uppercase */
651 cmp
= dtCompare(&ciKey
, p
, stbl
[index
]);
659 /* search hit - leaf page:
660 * return the entry found
662 if (p
->header
.flag
& BT_LEAF
) {
663 inumber
= le32_to_cpu(
664 ((struct ldtentry
*) & p
->slot
[stbl
[index
]])->inumber
);
667 * search for JFS_LOOKUP
669 if (flag
== JFS_LOOKUP
) {
676 * search for JFS_CREATE
678 if (flag
== JFS_CREATE
) {
685 * search for JFS_REMOVE or JFS_RENAME
687 if ((flag
== JFS_REMOVE
||
688 flag
== JFS_RENAME
) &&
695 * JFS_REMOVE|JFS_FINDDIR|JFS_RENAME
697 /* save search result */
708 /* search hit - internal page:
709 * descend/search its child page
723 * base is the smallest index with key (Kj) greater than
724 * search key (K) and may be zero or (maxindex + 1) index.
727 * search miss - leaf page
729 * return location of entry (base) where new entry with
730 * search key K is to be inserted.
732 if (p
->header
.flag
& BT_LEAF
) {
734 * search for JFS_LOOKUP, JFS_REMOVE, or JFS_RENAME
736 if (flag
== JFS_LOOKUP
|| flag
== JFS_REMOVE
||
737 flag
== JFS_RENAME
) {
743 * search for JFS_CREATE|JFS_FINDDIR:
758 * search miss - internal page
760 * if base is non-zero, decrement base by one to get the parent
761 * entry of the child page to search.
763 index
= base
? base
- 1 : base
;
766 * go down to child page
769 /* update max. number of pages to split */
770 if (BT_STACK_FULL(btstack
)) {
771 /* Something's corrupted, mark filesytem dirty so
772 * chkdsk will fix it.
774 jfs_error(sb
, "stack overrun in dtSearch!");
775 BT_STACK_DUMP(btstack
);
781 /* push (bn, index) of the parent page/entry */
782 BT_PUSH(btstack
, bn
, index
);
784 /* get the child page block number */
785 pxd
= (pxd_t
*) & p
->slot
[stbl
[index
]];
786 bn
= addressPXD(pxd
);
787 psize
= lengthPXD(pxd
) << JFS_SBI(ip
->i_sb
)->l2bsize
;
789 /* unpin the parent page */
809 * function: insert an entry to directory tree
813 * return: 0 - success;
816 int dtInsert(tid_t tid
, struct inode
*ip
,
817 struct component_name
* name
, ino_t
* fsn
, struct btstack
* btstack
)
820 struct metapage
*mp
; /* meta-page buffer */
821 dtpage_t
*p
; /* base B+-tree index page */
824 struct dtsplit split
; /* split information */
826 struct dt_lock
*dtlck
;
832 * retrieve search result
834 * dtSearch() returns (leaf page pinned, index at which to insert).
835 * n.b. dtSearch() may return index of (maxindex + 1) of
838 DT_GETSEARCH(ip
, btstack
->top
, bn
, mp
, p
, index
);
841 * insert entry for new key
844 if (JFS_IP(ip
)->next_index
== DIREND
) {
848 n
= NDTLEAF(name
->namlen
);
852 n
= NDTLEAF_LEGACY(name
->namlen
);
853 data
.leaf
.ip
= NULL
; /* signifies legacy directory format */
855 data
.leaf
.ino
= *fsn
;
858 * leaf page does not have enough room for new entry:
860 * extend/split the leaf page;
862 * dtSplitUp() will insert the entry and unpin the leaf page.
864 if (n
> p
->header
.freecnt
) {
870 rc
= dtSplitUp(tid
, ip
, &split
, btstack
);
875 * leaf page does have enough room for new entry:
877 * insert the new data entry into the leaf page;
879 BT_MARK_DIRTY(mp
, ip
);
881 * acquire a transaction lock on the leaf page
883 tlck
= txLock(tid
, ip
, mp
, tlckDTREE
| tlckENTRY
);
884 dtlck
= (struct dt_lock
*) & tlck
->lock
;
885 ASSERT(dtlck
->index
== 0);
888 /* linelock header */
893 dtInsertEntry(p
, index
, name
, &data
, &dtlck
);
895 /* linelock stbl of non-root leaf page */
896 if (!(p
->header
.flag
& BT_ROOT
)) {
897 if (dtlck
->index
>= dtlck
->maxcnt
)
898 dtlck
= (struct dt_lock
*) txLinelock(dtlck
);
899 lv
= & dtlck
->lv
[dtlck
->index
];
900 n
= index
>> L2DTSLOTSIZE
;
901 lv
->offset
= p
->header
.stblindex
+ n
;
903 ((p
->header
.nextindex
- 1) >> L2DTSLOTSIZE
) - n
+ 1;
907 /* unpin the leaf page */
917 * function: propagate insertion bottom up;
921 * return: 0 - success;
923 * leaf page unpinned;
925 static int dtSplitUp(tid_t tid
,
926 struct inode
*ip
, struct dtsplit
* split
, struct btstack
* btstack
)
928 struct jfs_sb_info
*sbi
= JFS_SBI(ip
->i_sb
);
930 struct metapage
*smp
;
931 dtpage_t
*sp
; /* split page */
932 struct metapage
*rmp
;
933 dtpage_t
*rp
; /* new right page split from sp */
934 pxd_t rpxd
; /* new right page extent descriptor */
935 struct metapage
*lmp
;
936 dtpage_t
*lp
; /* left child page */
937 int skip
; /* index of entry of insertion */
938 struct btframe
*parent
; /* parent page entry on traverse stack */
941 struct pxdlist pxdlist
;
943 struct component_name key
= { 0, NULL
};
944 ddata_t
*data
= split
->data
;
946 struct dt_lock
*dtlck
;
949 int quota_allocation
= 0;
953 sp
= DT_PAGE(ip
, smp
);
956 (wchar_t *) kmalloc((JFS_NAME_MAX
+ 2) * sizeof(wchar_t),
967 * The split routines insert the new entry, and
968 * acquire txLock as appropriate.
971 * split root leaf page:
973 if (sp
->header
.flag
& BT_ROOT
) {
975 * allocate a single extent child page
978 n
= sbi
->bsize
>> L2DTSLOTSIZE
;
979 n
-= (n
+ 31) >> L2DTSLOTSIZE
; /* stbl size */
980 n
-= DTROOTMAXSLOT
- sp
->header
.freecnt
; /* header + entries */
981 if (n
<= split
->nslot
)
983 if ((rc
= dbAlloc(ip
, 0, (s64
) xlen
, &xaddr
))) {
990 pxd
= &pxdlist
.pxd
[0];
991 PXDaddress(pxd
, xaddr
);
992 PXDlength(pxd
, xlen
);
993 split
->pxdlist
= &pxdlist
;
994 rc
= dtSplitRoot(tid
, ip
, split
, &rmp
);
997 dbFree(ip
, xaddr
, xlen
);
1007 * extend first leaf page
1009 * extend the 1st extent if less than buffer page size
1010 * (dtExtendPage() reurns leaf page unpinned)
1012 pxd
= &sp
->header
.self
;
1013 xlen
= lengthPXD(pxd
);
1014 xsize
= xlen
<< sbi
->l2bsize
;
1015 if (xsize
< PSIZE
) {
1016 xaddr
= addressPXD(pxd
);
1017 n
= xsize
>> L2DTSLOTSIZE
;
1018 n
-= (n
+ 31) >> L2DTSLOTSIZE
; /* stbl size */
1019 if ((n
+ sp
->header
.freecnt
) <= split
->nslot
)
1020 n
= xlen
+ (xlen
<< 1);
1024 /* Allocate blocks to quota. */
1025 if (DQUOT_ALLOC_BLOCK(ip
, n
)) {
1029 quota_allocation
+= n
;
1031 if ((rc
= dbReAlloc(sbi
->ipbmap
, xaddr
, (s64
) xlen
,
1035 pxdlist
.maxnpxd
= 1;
1037 pxd
= &pxdlist
.pxd
[0];
1038 PXDaddress(pxd
, nxaddr
)
1039 PXDlength(pxd
, xlen
+ n
);
1040 split
->pxdlist
= &pxdlist
;
1041 if ((rc
= dtExtendPage(tid
, ip
, split
, btstack
))) {
1042 nxaddr
= addressPXD(pxd
);
1043 if (xaddr
!= nxaddr
) {
1044 /* free relocated extent */
1045 xlen
= lengthPXD(pxd
);
1046 dbFree(ip
, nxaddr
, (s64
) xlen
);
1048 /* free extended delta */
1049 xlen
= lengthPXD(pxd
) - n
;
1050 xaddr
= addressPXD(pxd
) + xlen
;
1051 dbFree(ip
, xaddr
, (s64
) n
);
1061 * split leaf page <sp> into <sp> and a new right page <rp>.
1063 * return <rp> pinned and its extent descriptor <rpxd>
1066 * allocate new directory page extent and
1067 * new index page(s) to cover page split(s)
1069 * allocation hint: ?
1071 n
= btstack
->nsplit
;
1072 pxdlist
.maxnpxd
= pxdlist
.npxd
= 0;
1073 xlen
= sbi
->nbperpage
;
1074 for (pxd
= pxdlist
.pxd
; n
> 0; n
--, pxd
++) {
1075 if ((rc
= dbAlloc(ip
, 0, (s64
) xlen
, &xaddr
)) == 0) {
1076 PXDaddress(pxd
, xaddr
);
1077 PXDlength(pxd
, xlen
);
1084 /* undo allocation */
1088 split
->pxdlist
= &pxdlist
;
1089 if ((rc
= dtSplitPage(tid
, ip
, split
, &rmp
, &rp
, &rpxd
))) {
1092 /* undo allocation */
1097 * propagate up the router entry for the leaf page just split
1099 * insert a router entry for the new page into the parent page,
1100 * propagate the insert/split up the tree by walking back the stack
1101 * of (bn of parent page, index of child page entry in parent page)
1102 * that were traversed during the search for the page that split.
1104 * the propagation of insert/split up the tree stops if the root
1105 * splits or the page inserted into doesn't have to split to hold
1108 * the parent entry for the split page remains the same, and
1109 * a new entry is inserted at its right with the first key and
1110 * block number of the new right page.
1112 * There are a maximum of 4 pages pinned at any time:
1113 * two children, left parent and right parent (when the parent splits).
1114 * keep the child pages pinned while working on the parent.
1115 * make sure that all pins are released at exit.
1117 while ((parent
= BT_POP(btstack
)) != NULL
) {
1118 /* parent page specified by stack frame <parent> */
1120 /* keep current child pages (<lp>, <rp>) pinned */
1125 * insert router entry in parent for new right child page <rp>
1127 /* get the parent page <sp> */
1128 DT_GETPAGE(ip
, parent
->bn
, smp
, PSIZE
, sp
, rc
);
1136 * The new key entry goes ONE AFTER the index of parent entry,
1137 * because the split was to the right.
1139 skip
= parent
->index
+ 1;
1142 * compute the key for the router entry
1144 * key suffix compression:
1145 * for internal pages that have leaf pages as children,
1146 * retain only what's needed to distinguish between
1147 * the new entry and the entry on the page to its left.
1148 * If the keys compare equal, retain the entire key.
1150 * note that compression is performed only at computing
1151 * router key at the lowest internal level.
1152 * further compression of the key between pairs of higher
1153 * level internal pages loses too much information and
1154 * the search may fail.
1155 * (e.g., two adjacent leaf pages of {a, ..., x} {xx, ...,}
1156 * results in two adjacent parent entries (a)(xx).
1157 * if split occurs between these two entries, and
1158 * if compression is applied, the router key of parent entry
1159 * of right page (x) will divert search for x into right
1160 * subtree and miss x in the left subtree.)
1162 * the entire key must be retained for the next-to-leftmost
1163 * internal key at any level of the tree, or search may fail
1166 switch (rp
->header
.flag
& BT_TYPE
) {
1169 * compute the length of prefix for suffix compression
1170 * between last entry of left page and first entry
1173 if ((sp
->header
.flag
& BT_ROOT
&& skip
> 1) ||
1174 sp
->header
.prev
!= 0 || skip
> 1) {
1175 /* compute uppercase router prefix key */
1176 rc
= ciGetLeafPrefixKey(lp
,
1177 lp
->header
.nextindex
-1,
1187 /* next to leftmost entry of
1188 lowest internal level */
1190 /* compute uppercase router key */
1191 dtGetKey(rp
, 0, &key
, sbi
->mntflag
);
1192 key
.name
[key
.namlen
] = 0;
1194 if ((sbi
->mntflag
& JFS_OS2
) == JFS_OS2
)
1198 n
= NDTINTERNAL(key
.namlen
);
1202 dtGetKey(rp
, 0, &key
, sbi
->mntflag
);
1203 n
= NDTINTERNAL(key
.namlen
);
1207 jfs_err("dtSplitUp(): UFO!");
1211 /* unpin left child page */
1215 * compute the data for the router entry
1217 data
->xd
= rpxd
; /* child page xd */
1220 * parent page is full - split the parent page
1222 if (n
> sp
->header
.freecnt
) {
1223 /* init for parent page split */
1225 split
->index
= skip
; /* index at insert */
1228 /* split->data = data; */
1230 /* unpin right child page */
1233 /* The split routines insert the new entry,
1234 * acquire txLock as appropriate.
1235 * return <rp> pinned and its block number <rbn>.
1237 rc
= (sp
->header
.flag
& BT_ROOT
) ?
1238 dtSplitRoot(tid
, ip
, split
, &rmp
) :
1239 dtSplitPage(tid
, ip
, split
, &rmp
, &rp
, &rpxd
);
1245 /* smp and rmp are pinned */
1248 * parent page is not full - insert router entry in parent page
1251 BT_MARK_DIRTY(smp
, ip
);
1253 * acquire a transaction lock on the parent page
1255 tlck
= txLock(tid
, ip
, smp
, tlckDTREE
| tlckENTRY
);
1256 dtlck
= (struct dt_lock
*) & tlck
->lock
;
1257 ASSERT(dtlck
->index
== 0);
1258 lv
= & dtlck
->lv
[0];
1260 /* linelock header */
1265 /* linelock stbl of non-root parent page */
1266 if (!(sp
->header
.flag
& BT_ROOT
)) {
1268 n
= skip
>> L2DTSLOTSIZE
;
1269 lv
->offset
= sp
->header
.stblindex
+ n
;
1271 ((sp
->header
.nextindex
-
1272 1) >> L2DTSLOTSIZE
) - n
+ 1;
1276 dtInsertEntry(sp
, skip
, &key
, data
, &dtlck
);
1278 /* exit propagate up */
1283 /* unpin current split and its right page */
1288 * free remaining extents allocated for split
1292 pxd
= &pxdlist
.pxd
[n
];
1293 for (; n
< pxdlist
.maxnpxd
; n
++, pxd
++)
1294 dbFree(ip
, addressPXD(pxd
), (s64
) lengthPXD(pxd
));
1299 /* Rollback quota allocation */
1300 if (rc
&& quota_allocation
)
1301 DQUOT_FREE_BLOCK(ip
, quota_allocation
);
1312 * function: Split a non-root page of a btree.
1316 * return: 0 - success;
1318 * return split and new page pinned;
1320 static int dtSplitPage(tid_t tid
, struct inode
*ip
, struct dtsplit
* split
,
1321 struct metapage
** rmpp
, dtpage_t
** rpp
, pxd_t
* rpxdp
)
1324 struct metapage
*smp
;
1326 struct metapage
*rmp
;
1327 dtpage_t
*rp
; /* new right page allocated */
1328 s64 rbn
; /* new right page block number */
1329 struct metapage
*mp
;
1332 struct pxdlist
*pxdlist
;
1334 int skip
, nextindex
, half
, left
, nxt
, off
, si
;
1335 struct ldtentry
*ldtentry
;
1336 struct idtentry
*idtentry
;
1341 struct dt_lock
*sdtlck
, *rdtlck
;
1343 struct dt_lock
*dtlck
;
1344 struct lv
*slv
, *rlv
, *lv
;
1346 /* get split page */
1348 sp
= DT_PAGE(ip
, smp
);
1351 * allocate the new right page for the split
1353 pxdlist
= split
->pxdlist
;
1354 pxd
= &pxdlist
->pxd
[pxdlist
->npxd
];
1356 rbn
= addressPXD(pxd
);
1357 rmp
= get_metapage(ip
, rbn
, PSIZE
, 1);
1361 /* Allocate blocks to quota. */
1362 if (DQUOT_ALLOC_BLOCK(ip
, lengthPXD(pxd
))) {
1363 release_metapage(rmp
);
1367 jfs_info("dtSplitPage: ip:0x%p smp:0x%p rmp:0x%p", ip
, smp
, rmp
);
1369 BT_MARK_DIRTY(rmp
, ip
);
1371 * acquire a transaction lock on the new right page
1373 tlck
= txLock(tid
, ip
, rmp
, tlckDTREE
| tlckNEW
);
1374 rdtlck
= (struct dt_lock
*) & tlck
->lock
;
1376 rp
= (dtpage_t
*) rmp
->data
;
1378 rp
->header
.self
= *pxd
;
1380 BT_MARK_DIRTY(smp
, ip
);
1382 * acquire a transaction lock on the split page
1386 tlck
= txLock(tid
, ip
, smp
, tlckDTREE
| tlckENTRY
);
1387 sdtlck
= (struct dt_lock
*) & tlck
->lock
;
1389 /* linelock header of split page */
1390 ASSERT(sdtlck
->index
== 0);
1391 slv
= & sdtlck
->lv
[0];
1397 * initialize/update sibling pointers between sp and rp
1399 nextbn
= le64_to_cpu(sp
->header
.next
);
1400 rp
->header
.next
= cpu_to_le64(nextbn
);
1401 rp
->header
.prev
= cpu_to_le64(addressPXD(&sp
->header
.self
));
1402 sp
->header
.next
= cpu_to_le64(rbn
);
1405 * initialize new right page
1407 rp
->header
.flag
= sp
->header
.flag
;
1409 /* compute sorted entry table at start of extent data area */
1410 rp
->header
.nextindex
= 0;
1411 rp
->header
.stblindex
= 1;
1413 n
= PSIZE
>> L2DTSLOTSIZE
;
1414 rp
->header
.maxslot
= n
;
1415 stblsize
= (n
+ 31) >> L2DTSLOTSIZE
; /* in unit of slot */
1418 fsi
= rp
->header
.stblindex
+ stblsize
;
1419 rp
->header
.freelist
= fsi
;
1420 rp
->header
.freecnt
= rp
->header
.maxslot
- fsi
;
1423 * sequential append at tail: append without split
1425 * If splitting the last page on a level because of appending
1426 * a entry to it (skip is maxentry), it's likely that the access is
1427 * sequential. Adding an empty page on the side of the level is less
1428 * work and can push the fill factor much higher than normal.
1429 * If we're wrong it's no big deal, we'll just do the split the right
1431 * (It may look like it's equally easy to do a similar hack for
1432 * reverse sorted data, that is, split the tree left,
1433 * but it's not. Be my guest.)
1435 if (nextbn
== 0 && split
->index
== sp
->header
.nextindex
) {
1436 /* linelock header + stbl (first slot) of new page */
1437 rlv
= & rdtlck
->lv
[rdtlck
->index
];
1443 * initialize freelist of new right page
1446 for (fsi
++; fsi
< rp
->header
.maxslot
; f
++, fsi
++)
1450 /* insert entry at the first entry of the new right page */
1451 dtInsertEntry(rp
, 0, split
->key
, split
->data
, &rdtlck
);
1457 * non-sequential insert (at possibly middle page)
1461 * update prev pointer of previous right sibling page;
1464 DT_GETPAGE(ip
, nextbn
, mp
, PSIZE
, p
, rc
);
1466 discard_metapage(rmp
);
1470 BT_MARK_DIRTY(mp
, ip
);
1472 * acquire a transaction lock on the next page
1474 tlck
= txLock(tid
, ip
, mp
, tlckDTREE
| tlckRELINK
);
1475 jfs_info("dtSplitPage: tlck = 0x%p, ip = 0x%p, mp=0x%p",
1477 dtlck
= (struct dt_lock
*) & tlck
->lock
;
1479 /* linelock header of previous right sibling page */
1480 lv
= & dtlck
->lv
[dtlck
->index
];
1485 p
->header
.prev
= cpu_to_le64(rbn
);
1491 * split the data between the split and right pages.
1493 skip
= split
->index
;
1494 half
= (PSIZE
>> L2DTSLOTSIZE
) >> 1; /* swag */
1498 * compute fill factor for split pages
1500 * <nxt> traces the next entry to move to rp
1501 * <off> traces the next entry to stay in sp
1503 stbl
= (u8
*) & sp
->slot
[sp
->header
.stblindex
];
1504 nextindex
= sp
->header
.nextindex
;
1505 for (nxt
= off
= 0; nxt
< nextindex
; ++off
) {
1507 /* check for fill factor with new entry size */
1511 switch (sp
->header
.flag
& BT_TYPE
) {
1513 ldtentry
= (struct ldtentry
*) & sp
->slot
[si
];
1515 n
= NDTLEAF(ldtentry
->namlen
);
1517 n
= NDTLEAF_LEGACY(ldtentry
->
1522 idtentry
= (struct idtentry
*) & sp
->slot
[si
];
1523 n
= NDTINTERNAL(idtentry
->namlen
);
1530 ++nxt
; /* advance to next entry to move in sp */
1538 /* <nxt> poins to the 1st entry to move */
1541 * move entries to right page
1543 * dtMoveEntry() initializes rp and reserves entry for insertion
1545 * split page moved out entries are linelocked;
1546 * new/right page moved in entries are linelocked;
1548 /* linelock header + stbl of new right page */
1549 rlv
= & rdtlck
->lv
[rdtlck
->index
];
1554 dtMoveEntry(sp
, nxt
, rp
, &sdtlck
, &rdtlck
, DO_INDEX(ip
));
1556 sp
->header
.nextindex
= nxt
;
1559 * finalize freelist of new right page
1561 fsi
= rp
->header
.freelist
;
1563 for (fsi
++; fsi
< rp
->header
.maxslot
; f
++, fsi
++)
1568 * Update directory index table for entries now in right page
1570 if ((rp
->header
.flag
& BT_LEAF
) && DO_INDEX(ip
)) {
1574 stbl
= DT_GETSTBL(rp
);
1575 for (n
= 0; n
< rp
->header
.nextindex
; n
++) {
1576 ldtentry
= (struct ldtentry
*) & rp
->slot
[stbl
[n
]];
1577 modify_index(tid
, ip
, le32_to_cpu(ldtentry
->index
),
1578 rbn
, n
, &mp
, &lblock
);
1581 release_metapage(mp
);
1585 * the skipped index was on the left page,
1588 /* insert the new entry in the split page */
1589 dtInsertEntry(sp
, skip
, split
->key
, split
->data
, &sdtlck
);
1591 /* linelock stbl of split page */
1592 if (sdtlck
->index
>= sdtlck
->maxcnt
)
1593 sdtlck
= (struct dt_lock
*) txLinelock(sdtlck
);
1594 slv
= & sdtlck
->lv
[sdtlck
->index
];
1595 n
= skip
>> L2DTSLOTSIZE
;
1596 slv
->offset
= sp
->header
.stblindex
+ n
;
1598 ((sp
->header
.nextindex
- 1) >> L2DTSLOTSIZE
) - n
+ 1;
1602 * the skipped index was on the right page,
1605 /* adjust the skip index to reflect the new position */
1608 /* insert the new entry in the right page */
1609 dtInsertEntry(rp
, skip
, split
->key
, split
->data
, &rdtlck
);
1623 * function: extend 1st/only directory leaf page
1627 * return: 0 - success;
1629 * return extended page pinned;
1631 static int dtExtendPage(tid_t tid
,
1632 struct inode
*ip
, struct dtsplit
* split
, struct btstack
* btstack
)
1634 struct super_block
*sb
= ip
->i_sb
;
1636 struct metapage
*smp
, *pmp
, *mp
;
1638 struct pxdlist
*pxdlist
;
1641 int newstblindex
, newstblsize
;
1642 int oldstblindex
, oldstblsize
;
1645 struct btframe
*parent
;
1647 struct dt_lock
*dtlck
;
1650 struct pxd_lock
*pxdlock
;
1653 struct ldtentry
*ldtentry
;
1656 /* get page to extend */
1658 sp
= DT_PAGE(ip
, smp
);
1660 /* get parent/root page */
1661 parent
= BT_POP(btstack
);
1662 DT_GETPAGE(ip
, parent
->bn
, pmp
, PSIZE
, pp
, rc
);
1669 pxdlist
= split
->pxdlist
;
1670 pxd
= &pxdlist
->pxd
[pxdlist
->npxd
];
1673 xaddr
= addressPXD(pxd
);
1674 tpxd
= &sp
->header
.self
;
1675 txaddr
= addressPXD(tpxd
);
1676 /* in-place extension */
1677 if (xaddr
== txaddr
) {
1684 /* save moved extent descriptor for later free */
1685 tlck
= txMaplock(tid
, ip
, tlckDTREE
| tlckRELOCATE
);
1686 pxdlock
= (struct pxd_lock
*) & tlck
->lock
;
1687 pxdlock
->flag
= mlckFREEPXD
;
1688 pxdlock
->pxd
= sp
->header
.self
;
1692 * Update directory index table to reflect new page address
1698 stbl
= DT_GETSTBL(sp
);
1699 for (n
= 0; n
< sp
->header
.nextindex
; n
++) {
1701 (struct ldtentry
*) & sp
->slot
[stbl
[n
]];
1702 modify_index(tid
, ip
,
1703 le32_to_cpu(ldtentry
->index
),
1704 xaddr
, n
, &mp
, &lblock
);
1707 release_metapage(mp
);
1714 sp
->header
.self
= *pxd
;
1716 jfs_info("dtExtendPage: ip:0x%p smp:0x%p sp:0x%p", ip
, smp
, sp
);
1718 BT_MARK_DIRTY(smp
, ip
);
1720 * acquire a transaction lock on the extended/leaf page
1722 tlck
= txLock(tid
, ip
, smp
, tlckDTREE
| type
);
1723 dtlck
= (struct dt_lock
*) & tlck
->lock
;
1724 lv
= & dtlck
->lv
[0];
1726 /* update buffer extent descriptor of extended page */
1727 xlen
= lengthPXD(pxd
);
1728 xsize
= xlen
<< JFS_SBI(sb
)->l2bsize
;
1729 #ifdef _STILL_TO_PORT
1730 bmSetXD(smp
, xaddr
, xsize
);
1731 #endif /* _STILL_TO_PORT */
1734 * copy old stbl to new stbl at start of extended area
1736 oldstblindex
= sp
->header
.stblindex
;
1737 oldstblsize
= (sp
->header
.maxslot
+ 31) >> L2DTSLOTSIZE
;
1738 newstblindex
= sp
->header
.maxslot
;
1739 n
= xsize
>> L2DTSLOTSIZE
;
1740 newstblsize
= (n
+ 31) >> L2DTSLOTSIZE
;
1741 memcpy(&sp
->slot
[newstblindex
], &sp
->slot
[oldstblindex
],
1742 sp
->header
.nextindex
);
1745 * in-line extension: linelock old area of extended page
1747 if (type
== tlckEXTEND
) {
1748 /* linelock header */
1754 /* linelock new stbl of extended page */
1755 lv
->offset
= newstblindex
;
1756 lv
->length
= newstblsize
;
1759 * relocation: linelock whole relocated area
1763 lv
->length
= sp
->header
.maxslot
+ newstblsize
;
1768 sp
->header
.maxslot
= n
;
1769 sp
->header
.stblindex
= newstblindex
;
1770 /* sp->header.nextindex remains the same */
1773 * add old stbl region at head of freelist
1777 last
= sp
->header
.freelist
;
1778 for (n
= 0; n
< oldstblsize
; n
++, fsi
++, f
++) {
1782 sp
->header
.freelist
= last
;
1783 sp
->header
.freecnt
+= oldstblsize
;
1786 * append free region of newly extended area at tail of freelist
1788 /* init free region of newly extended area */
1789 fsi
= n
= newstblindex
+ newstblsize
;
1791 for (fsi
++; fsi
< sp
->header
.maxslot
; f
++, fsi
++)
1795 /* append new free region at tail of old freelist */
1796 fsi
= sp
->header
.freelist
;
1798 sp
->header
.freelist
= n
;
1803 } while (fsi
!= -1);
1808 sp
->header
.freecnt
+= sp
->header
.maxslot
- n
;
1811 * insert the new entry
1813 dtInsertEntry(sp
, split
->index
, split
->key
, split
->data
, &dtlck
);
1815 BT_MARK_DIRTY(pmp
, ip
);
1817 * linelock any freeslots residing in old extent
1819 if (type
== tlckEXTEND
) {
1820 n
= sp
->header
.maxslot
>> 2;
1821 if (sp
->header
.freelist
< n
)
1822 dtLinelockFreelist(sp
, n
, &dtlck
);
1826 * update parent entry on the parent/root page
1829 * acquire a transaction lock on the parent/root page
1831 tlck
= txLock(tid
, ip
, pmp
, tlckDTREE
| tlckENTRY
);
1832 dtlck
= (struct dt_lock
*) & tlck
->lock
;
1833 lv
= & dtlck
->lv
[dtlck
->index
];
1835 /* linelock parent entry - 1st slot */
1840 /* update the parent pxd for page extension */
1841 tpxd
= (pxd_t
*) & pp
->slot
[1];
1853 * split the full root page into
1854 * original/root/split page and new right page
1855 * i.e., root remains fixed in tree anchor (inode) and
1856 * the root is copied to a single new right child page
1857 * since root page << non-root page, and
1858 * the split root page contains a single entry for the
1859 * new right child page.
1863 * return: 0 - success;
1865 * return new page pinned;
1867 static int dtSplitRoot(tid_t tid
,
1868 struct inode
*ip
, struct dtsplit
* split
, struct metapage
** rmpp
)
1870 struct super_block
*sb
= ip
->i_sb
;
1871 struct metapage
*smp
;
1873 struct metapage
*rmp
;
1880 int fsi
, stblsize
, n
;
1883 struct pxdlist
*pxdlist
;
1885 struct dt_lock
*dtlck
;
1889 /* get split root page */
1891 sp
= &JFS_IP(ip
)->i_dtroot
;
1894 * allocate/initialize a single (right) child page
1896 * N.B. at first split, a one (or two) block to fit new entry
1897 * is allocated; at subsequent split, a full page is allocated;
1899 pxdlist
= split
->pxdlist
;
1900 pxd
= &pxdlist
->pxd
[pxdlist
->npxd
];
1902 rbn
= addressPXD(pxd
);
1903 xlen
= lengthPXD(pxd
);
1904 xsize
= xlen
<< JFS_SBI(sb
)->l2bsize
;
1905 rmp
= get_metapage(ip
, rbn
, xsize
, 1);
1911 /* Allocate blocks to quota. */
1912 if (DQUOT_ALLOC_BLOCK(ip
, lengthPXD(pxd
))) {
1913 release_metapage(rmp
);
1917 BT_MARK_DIRTY(rmp
, ip
);
1919 * acquire a transaction lock on the new right page
1921 tlck
= txLock(tid
, ip
, rmp
, tlckDTREE
| tlckNEW
);
1922 dtlck
= (struct dt_lock
*) & tlck
->lock
;
1925 (sp
->header
.flag
& BT_LEAF
) ? BT_LEAF
: BT_INTERNAL
;
1926 rp
->header
.self
= *pxd
;
1928 /* initialize sibling pointers */
1929 rp
->header
.next
= 0;
1930 rp
->header
.prev
= 0;
1933 * move in-line root page into new right page extent
1935 /* linelock header + copied entries + new stbl (1st slot) in new page */
1936 ASSERT(dtlck
->index
== 0);
1937 lv
= & dtlck
->lv
[0];
1939 lv
->length
= 10; /* 1 + 8 + 1 */
1942 n
= xsize
>> L2DTSLOTSIZE
;
1943 rp
->header
.maxslot
= n
;
1944 stblsize
= (n
+ 31) >> L2DTSLOTSIZE
;
1946 /* copy old stbl to new stbl at start of extended area */
1947 rp
->header
.stblindex
= DTROOTMAXSLOT
;
1948 stbl
= (s8
*) & rp
->slot
[DTROOTMAXSLOT
];
1949 memcpy(stbl
, sp
->header
.stbl
, sp
->header
.nextindex
);
1950 rp
->header
.nextindex
= sp
->header
.nextindex
;
1952 /* copy old data area to start of new data area */
1953 memcpy(&rp
->slot
[1], &sp
->slot
[1], IDATASIZE
);
1956 * append free region of newly extended area at tail of freelist
1958 /* init free region of newly extended area */
1959 fsi
= n
= DTROOTMAXSLOT
+ stblsize
;
1961 for (fsi
++; fsi
< rp
->header
.maxslot
; f
++, fsi
++)
1965 /* append new free region at tail of old freelist */
1966 fsi
= sp
->header
.freelist
;
1968 rp
->header
.freelist
= n
;
1970 rp
->header
.freelist
= fsi
;
1975 } while (fsi
!= -1);
1980 rp
->header
.freecnt
= sp
->header
.freecnt
+ rp
->header
.maxslot
- n
;
1983 * Update directory index table for entries now in right page
1985 if ((rp
->header
.flag
& BT_LEAF
) && DO_INDEX(ip
)) {
1987 struct metapage
*mp
= NULL
;
1988 struct ldtentry
*ldtentry
;
1990 stbl
= DT_GETSTBL(rp
);
1991 for (n
= 0; n
< rp
->header
.nextindex
; n
++) {
1992 ldtentry
= (struct ldtentry
*) & rp
->slot
[stbl
[n
]];
1993 modify_index(tid
, ip
, le32_to_cpu(ldtentry
->index
),
1994 rbn
, n
, &mp
, &lblock
);
1997 release_metapage(mp
);
2000 * insert the new entry into the new right/child page
2001 * (skip index in the new right page will not change)
2003 dtInsertEntry(rp
, split
->index
, split
->key
, split
->data
, &dtlck
);
2006 * reset parent/root page
2008 * set the 1st entry offset to 0, which force the left-most key
2009 * at any level of the tree to be less than any search key.
2011 * The btree comparison code guarantees that the left-most key on any
2012 * level of the tree is never used, so it doesn't need to be filled in.
2014 BT_MARK_DIRTY(smp
, ip
);
2016 * acquire a transaction lock on the root page (in-memory inode)
2018 tlck
= txLock(tid
, ip
, smp
, tlckDTREE
| tlckNEW
| tlckBTROOT
);
2019 dtlck
= (struct dt_lock
*) & tlck
->lock
;
2022 ASSERT(dtlck
->index
== 0);
2023 lv
= & dtlck
->lv
[0];
2025 lv
->length
= DTROOTMAXSLOT
;
2028 /* update page header of root */
2029 if (sp
->header
.flag
& BT_LEAF
) {
2030 sp
->header
.flag
&= ~BT_LEAF
;
2031 sp
->header
.flag
|= BT_INTERNAL
;
2034 /* init the first entry */
2035 s
= (struct idtentry
*) & sp
->slot
[DTENTRYSTART
];
2041 stbl
= sp
->header
.stbl
;
2042 stbl
[0] = DTENTRYSTART
;
2043 sp
->header
.nextindex
= 1;
2046 fsi
= DTENTRYSTART
+ 1;
2049 /* init free region of remaining area */
2050 for (fsi
++; fsi
< DTROOTMAXSLOT
; f
++, fsi
++)
2054 sp
->header
.freelist
= DTENTRYSTART
+ 1;
2055 sp
->header
.freecnt
= DTROOTMAXSLOT
- (DTENTRYSTART
+ 1);
2066 * function: delete the entry(s) referenced by a key.
2072 int dtDelete(tid_t tid
,
2073 struct inode
*ip
, struct component_name
* key
, ino_t
* ino
, int flag
)
2077 struct metapage
*mp
, *imp
;
2080 struct btstack btstack
;
2081 struct dt_lock
*dtlck
;
2085 struct ldtentry
*ldtentry
;
2087 u32 table_index
, next_index
;
2088 struct metapage
*nmp
;
2092 * search for the entry to delete:
2094 * dtSearch() returns (leaf page pinned, index at which to delete).
2096 if ((rc
= dtSearch(ip
, key
, ino
, &btstack
, flag
)))
2099 /* retrieve search result */
2100 DT_GETSEARCH(ip
, btstack
.top
, bn
, mp
, p
, index
);
2103 * We need to find put the index of the next entry into the
2104 * directory index table in order to resume a readdir from this
2108 stbl
= DT_GETSTBL(p
);
2109 ldtentry
= (struct ldtentry
*) & p
->slot
[stbl
[index
]];
2110 table_index
= le32_to_cpu(ldtentry
->index
);
2111 if (index
== (p
->header
.nextindex
- 1)) {
2113 * Last entry in this leaf page
2115 if ((p
->header
.flag
& BT_ROOT
)
2116 || (p
->header
.next
== 0))
2119 /* Read next leaf page */
2120 DT_GETPAGE(ip
, le64_to_cpu(p
->header
.next
),
2121 nmp
, PSIZE
, np
, rc
);
2125 stbl
= DT_GETSTBL(np
);
2127 (struct ldtentry
*) & np
->
2130 le32_to_cpu(ldtentry
->index
);
2136 (struct ldtentry
*) & p
->slot
[stbl
[index
+ 1]];
2137 next_index
= le32_to_cpu(ldtentry
->index
);
2139 free_index(tid
, ip
, table_index
, next_index
);
2142 * the leaf page becomes empty, delete the page
2144 if (p
->header
.nextindex
== 1) {
2145 /* delete empty page */
2146 rc
= dtDeleteUp(tid
, ip
, mp
, p
, &btstack
);
2149 * the leaf page has other entries remaining:
2151 * delete the entry from the leaf page.
2154 BT_MARK_DIRTY(mp
, ip
);
2156 * acquire a transaction lock on the leaf page
2158 tlck
= txLock(tid
, ip
, mp
, tlckDTREE
| tlckENTRY
);
2159 dtlck
= (struct dt_lock
*) & tlck
->lock
;
2162 * Do not assume that dtlck->index will be zero. During a
2163 * rename within a directory, this transaction may have
2164 * modified this page already when adding the new entry.
2167 /* linelock header */
2168 if (dtlck
->index
>= dtlck
->maxcnt
)
2169 dtlck
= (struct dt_lock
*) txLinelock(dtlck
);
2170 lv
= & dtlck
->lv
[dtlck
->index
];
2175 /* linelock stbl of non-root leaf page */
2176 if (!(p
->header
.flag
& BT_ROOT
)) {
2177 if (dtlck
->index
>= dtlck
->maxcnt
)
2178 dtlck
= (struct dt_lock
*) txLinelock(dtlck
);
2179 lv
= & dtlck
->lv
[dtlck
->index
];
2180 i
= index
>> L2DTSLOTSIZE
;
2181 lv
->offset
= p
->header
.stblindex
+ i
;
2183 ((p
->header
.nextindex
- 1) >> L2DTSLOTSIZE
) -
2188 /* free the leaf entry */
2189 dtDeleteEntry(p
, index
, &dtlck
);
2192 * Update directory index table for entries moved in stbl
2194 if (DO_INDEX(ip
) && index
< p
->header
.nextindex
) {
2198 stbl
= DT_GETSTBL(p
);
2199 for (i
= index
; i
< p
->header
.nextindex
; i
++) {
2201 (struct ldtentry
*) & p
->slot
[stbl
[i
]];
2202 modify_index(tid
, ip
,
2203 le32_to_cpu(ldtentry
->index
),
2204 bn
, i
, &imp
, &lblock
);
2207 release_metapage(imp
);
2221 * free empty pages as propagating deletion up the tree
2227 static int dtDeleteUp(tid_t tid
, struct inode
*ip
,
2228 struct metapage
* fmp
, dtpage_t
* fp
, struct btstack
* btstack
)
2231 struct metapage
*mp
;
2233 int index
, nextindex
;
2235 struct btframe
*parent
;
2236 struct dt_lock
*dtlck
;
2239 struct pxd_lock
*pxdlock
;
2243 * keep the root leaf page which has become empty
2245 if (BT_IS_ROOT(fmp
)) {
2249 * dtInitRoot() acquires txlock on the root
2251 dtInitRoot(tid
, ip
, PARENT(ip
));
2259 * free the non-root leaf page
2262 * acquire a transaction lock on the page
2264 * write FREEXTENT|NOREDOPAGE log record
2265 * N.B. linelock is overlaid as freed extent descriptor, and
2266 * the buffer page is freed;
2268 tlck
= txMaplock(tid
, ip
, tlckDTREE
| tlckFREE
);
2269 pxdlock
= (struct pxd_lock
*) & tlck
->lock
;
2270 pxdlock
->flag
= mlckFREEPXD
;
2271 pxdlock
->pxd
= fp
->header
.self
;
2274 /* update sibling pointers */
2275 if ((rc
= dtRelink(tid
, ip
, fp
))) {
2280 xlen
= lengthPXD(&fp
->header
.self
);
2282 /* Free quota allocation. */
2283 DQUOT_FREE_BLOCK(ip
, xlen
);
2285 /* free/invalidate its buffer page */
2286 discard_metapage(fmp
);
2289 * propagate page deletion up the directory tree
2291 * If the delete from the parent page makes it empty,
2292 * continue all the way up the tree.
2293 * stop if the root page is reached (which is never deleted) or
2294 * if the entry deletion does not empty the page.
2296 while ((parent
= BT_POP(btstack
)) != NULL
) {
2297 /* pin the parent page <sp> */
2298 DT_GETPAGE(ip
, parent
->bn
, mp
, PSIZE
, p
, rc
);
2303 * free the extent of the child page deleted
2305 index
= parent
->index
;
2308 * delete the entry for the child page from parent
2310 nextindex
= p
->header
.nextindex
;
2313 * the parent has the single entry being deleted:
2315 * free the parent page which has become empty.
2317 if (nextindex
== 1) {
2319 * keep the root internal page which has become empty
2321 if (p
->header
.flag
& BT_ROOT
) {
2325 * dtInitRoot() acquires txlock on the root
2327 dtInitRoot(tid
, ip
, PARENT(ip
));
2334 * free the parent page
2338 * acquire a transaction lock on the page
2340 * write FREEXTENT|NOREDOPAGE log record
2344 tlckDTREE
| tlckFREE
);
2345 pxdlock
= (struct pxd_lock
*) & tlck
->lock
;
2346 pxdlock
->flag
= mlckFREEPXD
;
2347 pxdlock
->pxd
= p
->header
.self
;
2350 /* update sibling pointers */
2351 if ((rc
= dtRelink(tid
, ip
, p
))) {
2356 xlen
= lengthPXD(&p
->header
.self
);
2358 /* Free quota allocation */
2359 DQUOT_FREE_BLOCK(ip
, xlen
);
2361 /* free/invalidate its buffer page */
2362 discard_metapage(mp
);
2370 * the parent has other entries remaining:
2372 * delete the router entry from the parent page.
2374 BT_MARK_DIRTY(mp
, ip
);
2376 * acquire a transaction lock on the page
2378 * action: router entry deletion
2380 tlck
= txLock(tid
, ip
, mp
, tlckDTREE
| tlckENTRY
);
2381 dtlck
= (struct dt_lock
*) & tlck
->lock
;
2383 /* linelock header */
2384 if (dtlck
->index
>= dtlck
->maxcnt
)
2385 dtlck
= (struct dt_lock
*) txLinelock(dtlck
);
2386 lv
= & dtlck
->lv
[dtlck
->index
];
2391 /* linelock stbl of non-root leaf page */
2392 if (!(p
->header
.flag
& BT_ROOT
)) {
2393 if (dtlck
->index
< dtlck
->maxcnt
)
2396 dtlck
= (struct dt_lock
*) txLinelock(dtlck
);
2397 lv
= & dtlck
->lv
[0];
2399 i
= index
>> L2DTSLOTSIZE
;
2400 lv
->offset
= p
->header
.stblindex
+ i
;
2402 ((p
->header
.nextindex
- 1) >> L2DTSLOTSIZE
) -
2407 /* free the router entry */
2408 dtDeleteEntry(p
, index
, &dtlck
);
2410 /* reset key of new leftmost entry of level (for consistency) */
2412 ((p
->header
.flag
& BT_ROOT
) || p
->header
.prev
== 0))
2413 dtTruncateEntry(p
, 0, &dtlck
);
2415 /* unpin the parent page */
2418 /* exit propagation up */
2427 * NAME: dtRelocate()
2429 * FUNCTION: relocate dtpage (internal or leaf) of directory;
2430 * This function is mainly used by defragfs utility.
2432 int dtRelocate(tid_t tid
, struct inode
*ip
, s64 lmxaddr
, pxd_t
* opxd
,
2436 struct metapage
*mp
, *pmp
, *lmp
, *rmp
;
2437 dtpage_t
*p
, *pp
, *rp
= 0, *lp
= 0;
2440 struct btstack btstack
;
2442 s64 oxaddr
, nextbn
, prevbn
;
2445 struct dt_lock
*dtlck
;
2446 struct pxd_lock
*pxdlock
;
2450 oxaddr
= addressPXD(opxd
);
2451 xlen
= lengthPXD(opxd
);
2453 jfs_info("dtRelocate: lmxaddr:%Ld xaddr:%Ld:%Ld xlen:%d",
2454 (long long)lmxaddr
, (long long)oxaddr
, (long long)nxaddr
,
2458 * 1. get the internal parent dtpage covering
2459 * router entry for the tartget page to be relocated;
2461 rc
= dtSearchNode(ip
, lmxaddr
, opxd
, &btstack
);
2465 /* retrieve search result */
2466 DT_GETSEARCH(ip
, btstack
.top
, bn
, pmp
, pp
, index
);
2467 jfs_info("dtRelocate: parent router entry validated.");
2470 * 2. relocate the target dtpage
2472 /* read in the target page from src extent */
2473 DT_GETPAGE(ip
, oxaddr
, mp
, PSIZE
, p
, rc
);
2475 /* release the pinned parent page */
2481 * read in sibling pages if any to update sibling pointers;
2484 if (p
->header
.next
) {
2485 nextbn
= le64_to_cpu(p
->header
.next
);
2486 DT_GETPAGE(ip
, nextbn
, rmp
, PSIZE
, rp
, rc
);
2495 if (p
->header
.prev
) {
2496 prevbn
= le64_to_cpu(p
->header
.prev
);
2497 DT_GETPAGE(ip
, prevbn
, lmp
, PSIZE
, lp
, rc
);
2507 /* at this point, all xtpages to be updated are in memory */
2510 * update sibling pointers of sibling dtpages if any;
2513 tlck
= txLock(tid
, ip
, lmp
, tlckDTREE
| tlckRELINK
);
2514 dtlck
= (struct dt_lock
*) & tlck
->lock
;
2515 /* linelock header */
2516 ASSERT(dtlck
->index
== 0);
2517 lv
= & dtlck
->lv
[0];
2522 lp
->header
.next
= cpu_to_le64(nxaddr
);
2527 tlck
= txLock(tid
, ip
, rmp
, tlckDTREE
| tlckRELINK
);
2528 dtlck
= (struct dt_lock
*) & tlck
->lock
;
2529 /* linelock header */
2530 ASSERT(dtlck
->index
== 0);
2531 lv
= & dtlck
->lv
[0];
2536 rp
->header
.prev
= cpu_to_le64(nxaddr
);
2541 * update the target dtpage to be relocated
2543 * write LOG_REDOPAGE of LOG_NEW type for dst page
2544 * for the whole target page (logredo() will apply
2545 * after image and update bmap for allocation of the
2546 * dst extent), and update bmap for allocation of
2549 tlck
= txLock(tid
, ip
, mp
, tlckDTREE
| tlckNEW
);
2550 dtlck
= (struct dt_lock
*) & tlck
->lock
;
2551 /* linelock header */
2552 ASSERT(dtlck
->index
== 0);
2553 lv
= & dtlck
->lv
[0];
2555 /* update the self address in the dtpage header */
2556 pxd
= &p
->header
.self
;
2557 PXDaddress(pxd
, nxaddr
);
2559 /* the dst page is the same as the src page, i.e.,
2560 * linelock for afterimage of the whole page;
2563 lv
->length
= p
->header
.maxslot
;
2566 /* update the buffer extent descriptor of the dtpage */
2567 xsize
= xlen
<< JFS_SBI(ip
->i_sb
)->l2bsize
;
2568 #ifdef _STILL_TO_PORT
2569 bmSetXD(mp
, nxaddr
, xsize
);
2570 #endif /* _STILL_TO_PORT */
2571 /* unpin the relocated page */
2573 jfs_info("dtRelocate: target dtpage relocated.");
2575 /* the moved extent is dtpage, then a LOG_NOREDOPAGE log rec
2576 * needs to be written (in logredo(), the LOG_NOREDOPAGE log rec
2577 * will also force a bmap update ).
2581 * 3. acquire maplock for the source extent to be freed;
2583 /* for dtpage relocation, write a LOG_NOREDOPAGE record
2584 * for the source dtpage (logredo() will init NoRedoPage
2585 * filter and will also update bmap for free of the source
2586 * dtpage), and upadte bmap for free of the source dtpage;
2588 tlck
= txMaplock(tid
, ip
, tlckDTREE
| tlckFREE
);
2589 pxdlock
= (struct pxd_lock
*) & tlck
->lock
;
2590 pxdlock
->flag
= mlckFREEPXD
;
2591 PXDaddress(&pxdlock
->pxd
, oxaddr
);
2592 PXDlength(&pxdlock
->pxd
, xlen
);
2596 * 4. update the parent router entry for relocation;
2598 * acquire tlck for the parent entry covering the target dtpage;
2599 * write LOG_REDOPAGE to apply after image only;
2601 jfs_info("dtRelocate: update parent router entry.");
2602 tlck
= txLock(tid
, ip
, pmp
, tlckDTREE
| tlckENTRY
);
2603 dtlck
= (struct dt_lock
*) & tlck
->lock
;
2604 lv
= & dtlck
->lv
[dtlck
->index
];
2606 /* update the PXD with the new address */
2607 stbl
= DT_GETSTBL(pp
);
2608 pxd
= (pxd_t
*) & pp
->slot
[stbl
[index
]];
2609 PXDaddress(pxd
, nxaddr
);
2610 lv
->offset
= stbl
[index
];
2614 /* unpin the parent dtpage */
2621 * NAME: dtSearchNode()
2623 * FUNCTION: Search for an dtpage containing a specified address
2624 * This function is mainly used by defragfs utility.
2626 * NOTE: Search result on stack, the found page is pinned at exit.
2627 * The result page must be an internal dtpage.
2628 * lmxaddr give the address of the left most page of the
2629 * dtree level, in which the required dtpage resides.
2631 static int dtSearchNode(struct inode
*ip
, s64 lmxaddr
, pxd_t
* kpxd
,
2632 struct btstack
* btstack
)
2636 struct metapage
*mp
;
2638 int psize
= 288; /* initial in-line directory */
2642 struct btframe
*btsp
;
2644 BT_CLR(btstack
); /* reset stack */
2647 * descend tree to the level with specified leftmost page
2649 * by convention, root bn = 0.
2652 /* get/pin the page to search */
2653 DT_GETPAGE(ip
, bn
, mp
, psize
, p
, rc
);
2657 /* does the xaddr of leftmost page of the levevl
2658 * matches levevl search key ?
2660 if (p
->header
.flag
& BT_ROOT
) {
2663 } else if (addressPXD(&p
->header
.self
) == lmxaddr
)
2667 * descend down to leftmost child page
2669 if (p
->header
.flag
& BT_LEAF
) {
2674 /* get the leftmost entry */
2675 stbl
= DT_GETSTBL(p
);
2676 pxd
= (pxd_t
*) & p
->slot
[stbl
[0]];
2678 /* get the child page block address */
2679 bn
= addressPXD(pxd
);
2680 psize
= lengthPXD(pxd
) << JFS_SBI(ip
->i_sb
)->l2bsize
;
2681 /* unpin the parent page */
2686 * search each page at the current levevl
2689 stbl
= DT_GETSTBL(p
);
2690 for (i
= 0; i
< p
->header
.nextindex
; i
++) {
2691 pxd
= (pxd_t
*) & p
->slot
[stbl
[i
]];
2693 /* found the specified router entry */
2694 if (addressPXD(pxd
) == addressPXD(kpxd
) &&
2695 lengthPXD(pxd
) == lengthPXD(kpxd
)) {
2696 btsp
= btstack
->top
;
2705 /* get the right sibling page if any */
2707 bn
= le64_to_cpu(p
->header
.next
);
2713 /* unpin current page */
2716 /* get the right sibling page */
2717 DT_GETPAGE(ip
, bn
, mp
, PSIZE
, p
, rc
);
2723 #endif /* _NOTYET */
2729 * link around a freed page.
2732 * fp: page to be freed
2736 static int dtRelink(tid_t tid
, struct inode
*ip
, dtpage_t
* p
)
2739 struct metapage
*mp
;
2742 struct dt_lock
*dtlck
;
2745 nextbn
= le64_to_cpu(p
->header
.next
);
2746 prevbn
= le64_to_cpu(p
->header
.prev
);
2748 /* update prev pointer of the next page */
2750 DT_GETPAGE(ip
, nextbn
, mp
, PSIZE
, p
, rc
);
2754 BT_MARK_DIRTY(mp
, ip
);
2756 * acquire a transaction lock on the next page
2758 * action: update prev pointer;
2760 tlck
= txLock(tid
, ip
, mp
, tlckDTREE
| tlckRELINK
);
2761 jfs_info("dtRelink nextbn: tlck = 0x%p, ip = 0x%p, mp=0x%p",
2763 dtlck
= (struct dt_lock
*) & tlck
->lock
;
2765 /* linelock header */
2766 if (dtlck
->index
>= dtlck
->maxcnt
)
2767 dtlck
= (struct dt_lock
*) txLinelock(dtlck
);
2768 lv
= & dtlck
->lv
[dtlck
->index
];
2773 p
->header
.prev
= cpu_to_le64(prevbn
);
2777 /* update next pointer of the previous page */
2779 DT_GETPAGE(ip
, prevbn
, mp
, PSIZE
, p
, rc
);
2783 BT_MARK_DIRTY(mp
, ip
);
2785 * acquire a transaction lock on the prev page
2787 * action: update next pointer;
2789 tlck
= txLock(tid
, ip
, mp
, tlckDTREE
| tlckRELINK
);
2790 jfs_info("dtRelink prevbn: tlck = 0x%p, ip = 0x%p, mp=0x%p",
2792 dtlck
= (struct dt_lock
*) & tlck
->lock
;
2794 /* linelock header */
2795 if (dtlck
->index
>= dtlck
->maxcnt
)
2796 dtlck
= (struct dt_lock
*) txLinelock(dtlck
);
2797 lv
= & dtlck
->lv
[dtlck
->index
];
2802 p
->header
.next
= cpu_to_le64(nextbn
);
2813 * initialize directory root (inline in inode)
2815 void dtInitRoot(tid_t tid
, struct inode
*ip
, u32 idotdot
)
2817 struct jfs_inode_info
*jfs_ip
= JFS_IP(ip
);
2822 struct dt_lock
*dtlck
;
2827 * If this was previously an non-empty directory, we need to remove
2828 * the old directory table.
2831 if (!jfs_dirtable_inline(ip
)) {
2832 struct tblock
*tblk
= tid_to_tblock(tid
);
2834 * We're playing games with the tid's xflag. If
2835 * we're removing a regular file, the file's xtree
2836 * is committed with COMMIT_PMAP, but we always
2837 * commit the directories xtree with COMMIT_PWMAP.
2839 xflag_save
= tblk
->xflag
;
2842 * xtTruncate isn't guaranteed to fully truncate
2843 * the xtree. The caller needs to check i_size
2844 * after committing the transaction to see if
2845 * additional truncation is needed. The
2846 * COMMIT_Stale flag tells caller that we
2847 * initiated the truncation.
2849 xtTruncate(tid
, ip
, 0, COMMIT_PWMAP
);
2850 set_cflag(COMMIT_Stale
, ip
);
2852 tblk
->xflag
= xflag_save
;
2856 jfs_ip
->next_index
= 2;
2858 ip
->i_size
= IDATASIZE
;
2861 * acquire a transaction lock on the root
2863 * action: directory initialization;
2865 tlck
= txLock(tid
, ip
, (struct metapage
*) & jfs_ip
->bxflag
,
2866 tlckDTREE
| tlckENTRY
| tlckBTROOT
);
2867 dtlck
= (struct dt_lock
*) & tlck
->lock
;
2870 ASSERT(dtlck
->index
== 0);
2871 lv
= & dtlck
->lv
[0];
2873 lv
->length
= DTROOTMAXSLOT
;
2876 p
= &jfs_ip
->i_dtroot
;
2878 p
->header
.flag
= DXD_INDEX
| BT_ROOT
| BT_LEAF
;
2880 p
->header
.nextindex
= 0;
2886 /* init data area of root */
2887 for (fsi
++; fsi
< DTROOTMAXSLOT
; f
++, fsi
++)
2891 p
->header
.freelist
= 1;
2892 p
->header
.freecnt
= 8;
2894 /* init '..' entry */
2895 p
->header
.idotdot
= cpu_to_le32(idotdot
);
2901 * add_missing_indices()
2903 * function: Fix dtree page in which one or more entries has an invalid index.
2904 * fsck.jfs should really fix this, but it currently does not.
2905 * Called from jfs_readdir when bad index is detected.
2907 static void add_missing_indices(struct inode
*inode
, s64 bn
)
2910 struct dt_lock
*dtlck
;
2914 struct metapage
*mp
;
2921 tid
= txBegin(inode
->i_sb
, 0);
2923 DT_GETPAGE(inode
, bn
, mp
, PSIZE
, p
, rc
);
2926 printk(KERN_ERR
"DT_GETPAGE failed!\n");
2929 BT_MARK_DIRTY(mp
, inode
);
2931 ASSERT(p
->header
.flag
& BT_LEAF
);
2933 tlck
= txLock(tid
, inode
, mp
, tlckDTREE
| tlckENTRY
);
2934 dtlck
= (struct dt_lock
*) &tlck
->lock
;
2936 stbl
= DT_GETSTBL(p
);
2937 for (i
= 0; i
< p
->header
.nextindex
; i
++) {
2938 d
= (struct ldtentry
*) &p
->slot
[stbl
[i
]];
2939 index
= le32_to_cpu(d
->index
);
2940 if ((index
< 2) || (index
>= JFS_IP(inode
)->next_index
)) {
2941 d
->index
= cpu_to_le32(add_index(tid
, inode
, bn
, i
));
2942 if (dtlck
->index
>= dtlck
->maxcnt
)
2943 dtlck
= (struct dt_lock
*) txLinelock(dtlck
);
2944 lv
= &dtlck
->lv
[dtlck
->index
];
2945 lv
->offset
= stbl
[i
];
2952 (void) txCommit(tid
, 1, &inode
, 0);
2958 * Buffer to hold directory entry info while traversing a dtree page
2959 * before being fed to the filldir function
2969 * function to determine next variable-sized jfs_dirent in buffer
2971 static inline struct jfs_dirent
*next_jfs_dirent(struct jfs_dirent
*dirent
)
2973 return (struct jfs_dirent
*)
2975 ((sizeof (struct jfs_dirent
) + dirent
->name_len
+ 1 +
2976 sizeof (loff_t
) - 1) &
2977 ~(sizeof (loff_t
) - 1)));
2983 * function: read directory entries sequentially
2984 * from the specified entry offset
2988 * return: offset = (pn, index) of start entry
2989 * of next jfs_readdir()/dtRead()
2991 int jfs_readdir(struct file
*filp
, void *dirent
, filldir_t filldir
)
2993 struct inode
*ip
= filp
->f_dentry
->d_inode
;
2994 struct nls_table
*codepage
= JFS_SBI(ip
->i_sb
)->nls_tab
;
2996 loff_t dtpos
; /* legacy OS/2 style position */
3001 } *dtoffset
= (struct dtoffset
*) &dtpos
;
3003 struct metapage
*mp
;
3007 struct btstack btstack
;
3011 int d_namleft
, len
, outlen
;
3012 unsigned long dirent_buf
;
3016 uint loop_count
= 0;
3017 struct jfs_dirent
*jfs_dirent
;
3019 int overflow
, fix_page
, page_fixed
= 0;
3020 static int unique_pos
= 2; /* If we can't fix broken index */
3022 if (filp
->f_pos
== DIREND
)
3027 * persistent index is stored in directory entries.
3028 * Special cases: 0 = .
3030 * -1 = End of directory
3034 dir_index
= (u32
) filp
->f_pos
;
3036 if (dir_index
> 1) {
3037 struct dir_table_slot dirtab_slot
;
3040 (dir_index
>= JFS_IP(ip
)->next_index
)) {
3041 /* Stale position. Directory has shrunk */
3042 filp
->f_pos
= DIREND
;
3046 rc
= read_index(ip
, dir_index
, &dirtab_slot
);
3048 filp
->f_pos
= DIREND
;
3051 if (dirtab_slot
.flag
== DIR_INDEX_FREE
) {
3052 if (loop_count
++ > JFS_IP(ip
)->next_index
) {
3053 jfs_err("jfs_readdir detected "
3055 filp
->f_pos
= DIREND
;
3058 dir_index
= le32_to_cpu(dirtab_slot
.addr2
);
3059 if (dir_index
== -1) {
3060 filp
->f_pos
= DIREND
;
3065 bn
= addressDTS(&dirtab_slot
);
3066 index
= dirtab_slot
.slot
;
3067 DT_GETPAGE(ip
, bn
, mp
, PSIZE
, p
, rc
);
3069 filp
->f_pos
= DIREND
;
3072 if (p
->header
.flag
& BT_INTERNAL
) {
3073 jfs_err("jfs_readdir: bad index table");
3079 if (dir_index
== 0) {
3084 if (filldir(dirent
, ".", 1, 0, ip
->i_ino
,
3092 if (filldir(dirent
, "..", 2, 1, PARENT(ip
), DT_DIR
))
3096 * Find first entry of left-most leaf
3099 filp
->f_pos
= DIREND
;
3103 if ((rc
= dtReadFirst(ip
, &btstack
)))
3106 DT_GETSEARCH(ip
, btstack
.top
, bn
, mp
, p
, index
);
3110 * Legacy filesystem - OS/2 & Linux JFS < 0.3.6
3112 * pn = index = 0: First entry "."
3113 * pn = 0; index = 1: Second entry ".."
3114 * pn > 0: Real entries, pn=1 -> leftmost page
3115 * pn = index = -1: No more entries
3117 dtpos
= filp
->f_pos
;
3119 /* build "." entry */
3121 if (filldir(dirent
, ".", 1, filp
->f_pos
, ip
->i_ino
,
3124 dtoffset
->index
= 1;
3125 filp
->f_pos
= dtpos
;
3128 if (dtoffset
->pn
== 0) {
3129 if (dtoffset
->index
== 1) {
3130 /* build ".." entry */
3132 if (filldir(dirent
, "..", 2, filp
->f_pos
,
3133 PARENT(ip
), DT_DIR
))
3136 jfs_err("jfs_readdir called with "
3140 dtoffset
->index
= 0;
3141 filp
->f_pos
= dtpos
;
3145 filp
->f_pos
= DIREND
;
3149 if ((rc
= dtReadNext(ip
, &filp
->f_pos
, &btstack
))) {
3150 jfs_err("jfs_readdir: unexpected rc = %d "
3151 "from dtReadNext", rc
);
3152 filp
->f_pos
= DIREND
;
3155 /* get start leaf page and index */
3156 DT_GETSEARCH(ip
, btstack
.top
, bn
, mp
, p
, index
);
3158 /* offset beyond directory eof ? */
3160 filp
->f_pos
= DIREND
;
3165 dirent_buf
= __get_free_page(GFP_KERNEL
);
3166 if (dirent_buf
== 0) {
3168 jfs_warn("jfs_readdir: __get_free_page failed!");
3169 filp
->f_pos
= DIREND
;
3174 jfs_dirent
= (struct jfs_dirent
*) dirent_buf
;
3176 overflow
= fix_page
= 0;
3178 stbl
= DT_GETSTBL(p
);
3180 for (i
= index
; i
< p
->header
.nextindex
; i
++) {
3181 d
= (struct ldtentry
*) & p
->slot
[stbl
[i
]];
3183 if (((long) jfs_dirent
+ d
->namlen
+ 1) >
3184 (dirent_buf
+ PAGE_SIZE
)) {
3185 /* DBCS codepages could overrun dirent_buf */
3191 d_namleft
= d
->namlen
;
3192 name_ptr
= jfs_dirent
->name
;
3193 jfs_dirent
->ino
= le32_to_cpu(d
->inumber
);
3196 len
= min(d_namleft
, DTLHDRDATALEN
);
3197 jfs_dirent
->position
= le32_to_cpu(d
->index
);
3199 * d->index should always be valid, but it
3200 * isn't. fsck.jfs doesn't create the
3201 * directory index for the lost+found
3202 * directory. Rather than let it go,
3203 * we can try to fix it.
3205 if ((jfs_dirent
->position
< 2) ||
3206 (jfs_dirent
->position
>=
3207 JFS_IP(ip
)->next_index
)) {
3208 if (!page_fixed
&& !isReadOnly(ip
)) {
3211 * setting overflow and setting
3212 * index to i will cause the
3213 * same page to be processed
3214 * again starting here
3220 jfs_dirent
->position
= unique_pos
++;
3223 jfs_dirent
->position
= dtpos
;
3224 len
= min(d_namleft
, DTLHDRDATALEN_LEGACY
);
3227 /* copy the name of head/only segment */
3228 outlen
= jfs_strfromUCS_le(name_ptr
, d
->name
, len
,
3230 jfs_dirent
->name_len
= outlen
;
3232 /* copy name in the additional segment(s) */
3235 t
= (struct dtslot
*) & p
->slot
[next
];
3239 if (d_namleft
== 0) {
3241 "JFS:Dtree error: ino = "
3242 "%ld, bn=%Ld, index = %d",
3248 len
= min(d_namleft
, DTSLOTDATALEN
);
3249 outlen
= jfs_strfromUCS_le(name_ptr
, t
->name
,
3251 jfs_dirent
->name_len
+= outlen
;
3257 jfs_dirent
= next_jfs_dirent(jfs_dirent
);
3264 /* Point to next leaf page */
3265 if (p
->header
.flag
& BT_ROOT
)
3268 bn
= le64_to_cpu(p
->header
.next
);
3270 /* update offset (pn:index) for new page */
3273 dtoffset
->index
= 0;
3279 /* unpin previous leaf page */
3282 jfs_dirent
= (struct jfs_dirent
*) dirent_buf
;
3283 while (jfs_dirents
--) {
3284 filp
->f_pos
= jfs_dirent
->position
;
3285 if (filldir(dirent
, jfs_dirent
->name
,
3286 jfs_dirent
->name_len
, filp
->f_pos
,
3287 jfs_dirent
->ino
, DT_UNKNOWN
))
3289 jfs_dirent
= next_jfs_dirent(jfs_dirent
);
3293 add_missing_indices(ip
, bn
);
3297 if (!overflow
&& (bn
== 0)) {
3298 filp
->f_pos
= DIREND
;
3302 DT_GETPAGE(ip
, bn
, mp
, PSIZE
, p
, rc
);
3304 free_page(dirent_buf
);
3310 free_page(dirent_buf
);
3319 * function: get the leftmost page of the directory
3321 static int dtReadFirst(struct inode
*ip
, struct btstack
* btstack
)
3325 int psize
= 288; /* initial in-line directory */
3326 struct metapage
*mp
;
3329 struct btframe
*btsp
;
3332 BT_CLR(btstack
); /* reset stack */
3335 * descend leftmost path of the tree
3337 * by convention, root bn = 0.
3340 DT_GETPAGE(ip
, bn
, mp
, psize
, p
, rc
);
3345 * leftmost leaf page
3347 if (p
->header
.flag
& BT_LEAF
) {
3348 /* return leftmost entry */
3349 btsp
= btstack
->top
;
3358 * descend down to leftmost child page
3360 if (BT_STACK_FULL(btstack
)) {
3362 jfs_error(ip
->i_sb
, "dtReadFirst: btstack overrun");
3363 BT_STACK_DUMP(btstack
);
3366 /* push (bn, index) of the parent page/entry */
3367 BT_PUSH(btstack
, bn
, 0);
3369 /* get the leftmost entry */
3370 stbl
= DT_GETSTBL(p
);
3371 xd
= (pxd_t
*) & p
->slot
[stbl
[0]];
3373 /* get the child page block address */
3374 bn
= addressPXD(xd
);
3375 psize
= lengthPXD(xd
) << JFS_SBI(ip
->i_sb
)->l2bsize
;
3377 /* unpin the parent page */
3386 * function: get the page of the specified offset (pn:index)
3388 * return: if (offset > eof), bn = -1;
3390 * note: if index > nextindex of the target leaf page,
3391 * start with 1st entry of next leaf page;
3393 static int dtReadNext(struct inode
*ip
, loff_t
* offset
,
3394 struct btstack
* btstack
)
3401 } *dtoffset
= (struct dtoffset
*) offset
;
3403 struct metapage
*mp
;
3408 struct btframe
*btsp
, *parent
;
3412 * get leftmost leaf page pinned
3414 if ((rc
= dtReadFirst(ip
, btstack
)))
3418 DT_GETSEARCH(ip
, btstack
->top
, bn
, mp
, p
, index
);
3420 /* get the start offset (pn:index) */
3421 pn
= dtoffset
->pn
- 1; /* Now pn = 0 represents leftmost leaf */
3422 index
= dtoffset
->index
;
3424 /* start at leftmost page ? */
3426 /* offset beyond eof ? */
3427 if (index
< p
->header
.nextindex
)
3430 if (p
->header
.flag
& BT_ROOT
) {
3435 /* start with 1st entry of next leaf page */
3437 dtoffset
->index
= index
= 0;
3441 /* start at non-leftmost page: scan parent pages for large pn */
3442 if (p
->header
.flag
& BT_ROOT
) {
3447 /* start after next leaf page ? */
3451 /* get leaf page pn = 1 */
3453 bn
= le64_to_cpu(p
->header
.next
);
3455 /* unpin leaf page */
3458 /* offset beyond eof ? */
3467 * scan last internal page level to get target leaf page
3470 /* unpin leftmost leaf page */
3473 /* get left most parent page */
3474 btsp
= btstack
->top
;
3477 DT_GETPAGE(ip
, bn
, mp
, PSIZE
, p
, rc
);
3481 /* scan parent pages at last internal page level */
3482 while (pn
>= p
->header
.nextindex
) {
3483 pn
-= p
->header
.nextindex
;
3485 /* get next parent page address */
3486 bn
= le64_to_cpu(p
->header
.next
);
3488 /* unpin current parent page */
3491 /* offset beyond eof ? */
3497 /* get next parent page */
3498 DT_GETPAGE(ip
, bn
, mp
, PSIZE
, p
, rc
);
3502 /* update parent page stack frame */
3506 /* get leaf page address */
3507 stbl
= DT_GETSTBL(p
);
3508 xd
= (pxd_t
*) & p
->slot
[stbl
[pn
]];
3509 bn
= addressPXD(xd
);
3511 /* unpin parent page */
3515 * get target leaf page
3518 DT_GETPAGE(ip
, bn
, mp
, PSIZE
, p
, rc
);
3523 * leaf page has been completed:
3524 * start with 1st entry of next leaf page
3526 if (index
>= p
->header
.nextindex
) {
3527 bn
= le64_to_cpu(p
->header
.next
);
3529 /* unpin leaf page */
3532 /* offset beyond eof ? */
3538 /* get next leaf page */
3539 DT_GETPAGE(ip
, bn
, mp
, PSIZE
, p
, rc
);
3543 /* start with 1st entry of next leaf page */
3545 dtoffset
->index
= 0;
3549 /* return target leaf page pinned */
3550 btsp
= btstack
->top
;
3552 btsp
->index
= dtoffset
->index
;
3562 * function: compare search key with an internal entry
3565 * < 0 if k is < record
3566 * = 0 if k is = record
3567 * > 0 if k is > record
3569 static int dtCompare(struct component_name
* key
, /* search key */
3570 dtpage_t
* p
, /* directory page */
3572 { /* entry slot index */
3575 int klen
, namlen
, len
, rc
;
3576 struct idtentry
*ih
;
3580 * force the left-most key on internal pages, at any level of
3581 * the tree, to be less than any search key.
3582 * this obviates having to update the leftmost key on an internal
3583 * page when the user inserts a new key in the tree smaller than
3584 * anything that has been stored.
3586 * (? if/when dtSearch() narrows down to 1st entry (index = 0),
3587 * at any internal page at any level of the tree,
3588 * it descends to child of the entry anyway -
3589 * ? make the entry as min size dummy entry)
3591 * if (e->index == 0 && h->prevpg == P_INVALID && !(h->flags & BT_LEAF))
3598 ih
= (struct idtentry
*) & p
->slot
[si
];
3601 namlen
= ih
->namlen
;
3602 len
= min(namlen
, DTIHDRDATALEN
);
3604 /* compare with head/only segment */
3605 len
= min(klen
, len
);
3606 if ((rc
= UniStrncmp_le(kname
, name
, len
)))
3612 /* compare with additional segment(s) */
3614 while (klen
> 0 && namlen
> 0) {
3615 /* compare with next name segment */
3616 t
= (struct dtslot
*) & p
->slot
[si
];
3617 len
= min(namlen
, DTSLOTDATALEN
);
3618 len
= min(klen
, len
);
3620 if ((rc
= UniStrncmp_le(kname
, name
, len
)))
3629 return (klen
- namlen
);
3638 * function: compare search key with an (leaf/internal) entry
3641 * < 0 if k is < record
3642 * = 0 if k is = record
3643 * > 0 if k is > record
3645 static int ciCompare(struct component_name
* key
, /* search key */
3646 dtpage_t
* p
, /* directory page */
3647 int si
, /* entry slot index */
3652 int klen
, namlen
, len
, rc
;
3653 struct ldtentry
*lh
;
3654 struct idtentry
*ih
;
3659 * force the left-most key on internal pages, at any level of
3660 * the tree, to be less than any search key.
3661 * this obviates having to update the leftmost key on an internal
3662 * page when the user inserts a new key in the tree smaller than
3663 * anything that has been stored.
3665 * (? if/when dtSearch() narrows down to 1st entry (index = 0),
3666 * at any internal page at any level of the tree,
3667 * it descends to child of the entry anyway -
3668 * ? make the entry as min size dummy entry)
3670 * if (e->index == 0 && h->prevpg == P_INVALID && !(h->flags & BT_LEAF))
3680 if (p
->header
.flag
& BT_LEAF
) {
3681 lh
= (struct ldtentry
*) & p
->slot
[si
];
3684 namlen
= lh
->namlen
;
3685 if (flag
& JFS_DIR_INDEX
)
3686 len
= min(namlen
, DTLHDRDATALEN
);
3688 len
= min(namlen
, DTLHDRDATALEN_LEGACY
);
3691 * internal page entry
3694 ih
= (struct idtentry
*) & p
->slot
[si
];
3697 namlen
= ih
->namlen
;
3698 len
= min(namlen
, DTIHDRDATALEN
);
3701 /* compare with head/only segment */
3702 len
= min(klen
, len
);
3703 for (i
= 0; i
< len
; i
++, kname
++, name
++) {
3704 /* only uppercase if case-insensitive support is on */
3705 if ((flag
& JFS_OS2
) == JFS_OS2
)
3706 x
= UniToupper(le16_to_cpu(*name
));
3708 x
= le16_to_cpu(*name
);
3709 if ((rc
= *kname
- x
))
3716 /* compare with additional segment(s) */
3717 while (klen
> 0 && namlen
> 0) {
3718 /* compare with next name segment */
3719 t
= (struct dtslot
*) & p
->slot
[si
];
3720 len
= min(namlen
, DTSLOTDATALEN
);
3721 len
= min(klen
, len
);
3723 for (i
= 0; i
< len
; i
++, kname
++, name
++) {
3724 /* only uppercase if case-insensitive support is on */
3725 if ((flag
& JFS_OS2
) == JFS_OS2
)
3726 x
= UniToupper(le16_to_cpu(*name
));
3728 x
= le16_to_cpu(*name
);
3730 if ((rc
= *kname
- x
))
3739 return (klen
- namlen
);
3744 * ciGetLeafPrefixKey()
3746 * function: compute prefix of suffix compression
3747 * from two adjacent leaf entries
3748 * across page boundary
3750 * return: non-zero on error
3753 static int ciGetLeafPrefixKey(dtpage_t
* lp
, int li
, dtpage_t
* rp
,
3754 int ri
, struct component_name
* key
, int flag
)
3757 wchar_t *pl
, *pr
, *kname
;
3758 struct component_name lkey
;
3759 struct component_name rkey
;
3761 lkey
.name
= (wchar_t *) kmalloc((JFS_NAME_MAX
+ 1) * sizeof(wchar_t),
3763 if (lkey
.name
== NULL
)
3766 rkey
.name
= (wchar_t *) kmalloc((JFS_NAME_MAX
+ 1) * sizeof(wchar_t),
3768 if (rkey
.name
== NULL
) {
3773 /* get left and right key */
3774 dtGetKey(lp
, li
, &lkey
, flag
);
3775 lkey
.name
[lkey
.namlen
] = 0;
3777 if ((flag
& JFS_OS2
) == JFS_OS2
)
3780 dtGetKey(rp
, ri
, &rkey
, flag
);
3781 rkey
.name
[rkey
.namlen
] = 0;
3784 if ((flag
& JFS_OS2
) == JFS_OS2
)
3787 /* compute prefix */
3790 namlen
= min(lkey
.namlen
, rkey
.namlen
);
3791 for (pl
= lkey
.name
, pr
= rkey
.name
;
3792 namlen
; pl
++, pr
++, namlen
--, klen
++, kname
++) {
3795 key
->namlen
= klen
+ 1;
3800 /* l->namlen <= r->namlen since l <= r */
3801 if (lkey
.namlen
< rkey
.namlen
) {
3803 key
->namlen
= klen
+ 1;
3804 } else /* l->namelen == r->namelen */
3818 * function: get key of the entry
3820 static void dtGetKey(dtpage_t
* p
, int i
, /* entry index */
3821 struct component_name
* key
, int flag
)
3825 struct ldtentry
*lh
;
3826 struct idtentry
*ih
;
3833 stbl
= DT_GETSTBL(p
);
3835 if (p
->header
.flag
& BT_LEAF
) {
3836 lh
= (struct ldtentry
*) & p
->slot
[si
];
3838 namlen
= lh
->namlen
;
3840 if (flag
& JFS_DIR_INDEX
)
3841 len
= min(namlen
, DTLHDRDATALEN
);
3843 len
= min(namlen
, DTLHDRDATALEN_LEGACY
);
3845 ih
= (struct idtentry
*) & p
->slot
[si
];
3847 namlen
= ih
->namlen
;
3849 len
= min(namlen
, DTIHDRDATALEN
);
3852 key
->namlen
= namlen
;
3856 * move head/only segment
3858 UniStrncpy_from_le(kname
, name
, len
);
3861 * move additional segment(s)
3864 /* get next segment */
3868 len
= min(namlen
, DTSLOTDATALEN
);
3869 UniStrncpy_from_le(kname
, t
->name
, len
);
3879 * function: allocate free slot(s) and
3880 * write a leaf/internal entry
3882 * return: entry slot index
3884 static void dtInsertEntry(dtpage_t
* p
, int index
, struct component_name
* key
,
3885 ddata_t
* data
, struct dt_lock
** dtlock
)
3887 struct dtslot
*h
, *t
;
3888 struct ldtentry
*lh
= NULL
;
3889 struct idtentry
*ih
= NULL
;
3890 int hsi
, fsi
, klen
, len
, nextindex
;
3895 struct dt_lock
*dtlck
= *dtlock
;
3899 struct metapage
*mp
= NULL
;
3904 /* allocate a free slot */
3905 hsi
= fsi
= p
->header
.freelist
;
3907 p
->header
.freelist
= h
->next
;
3908 --p
->header
.freecnt
;
3910 /* open new linelock */
3911 if (dtlck
->index
>= dtlck
->maxcnt
)
3912 dtlck
= (struct dt_lock
*) txLinelock(dtlck
);
3914 lv
= & dtlck
->lv
[dtlck
->index
];
3917 /* write head/only segment */
3918 if (p
->header
.flag
& BT_LEAF
) {
3919 lh
= (struct ldtentry
*) h
;
3921 lh
->inumber
= cpu_to_le32(data
->leaf
.ino
);
3924 if (data
->leaf
.ip
) {
3925 len
= min(klen
, DTLHDRDATALEN
);
3926 if (!(p
->header
.flag
& BT_ROOT
))
3927 bn
= addressPXD(&p
->header
.self
);
3928 lh
->index
= cpu_to_le32(add_index(data
->leaf
.tid
,
3932 len
= min(klen
, DTLHDRDATALEN_LEGACY
);
3934 ih
= (struct idtentry
*) h
;
3940 len
= min(klen
, DTIHDRDATALEN
);
3943 UniStrncpy_to_le(name
, kname
, len
);
3948 /* write additional segment(s) */
3953 fsi
= p
->header
.freelist
;
3955 p
->header
.freelist
= t
->next
;
3956 --p
->header
.freecnt
;
3958 /* is next slot contiguous ? */
3959 if (fsi
!= xsi
+ 1) {
3960 /* close current linelock */
3964 /* open new linelock */
3965 if (dtlck
->index
< dtlck
->maxcnt
)
3968 dtlck
= (struct dt_lock
*) txLinelock(dtlck
);
3969 lv
= & dtlck
->lv
[0];
3977 len
= min(klen
, DTSLOTDATALEN
);
3978 UniStrncpy_to_le(t
->name
, kname
, len
);
3985 /* close current linelock */
3991 /* terminate last/only segment */
3993 /* single segment entry */
3994 if (p
->header
.flag
& BT_LEAF
)
3999 /* multi-segment entry */
4002 /* if insert into middle, shift right succeeding entries in stbl */
4003 stbl
= DT_GETSTBL(p
);
4004 nextindex
= p
->header
.nextindex
;
4005 if (index
< nextindex
) {
4006 memmove(stbl
+ index
+ 1, stbl
+ index
, nextindex
- index
);
4008 if ((p
->header
.flag
& BT_LEAF
) && data
->leaf
.ip
) {
4012 * Need to update slot number for entries that moved
4016 for (n
= index
+ 1; n
<= nextindex
; n
++) {
4017 lh
= (struct ldtentry
*) & (p
->slot
[stbl
[n
]]);
4018 modify_index(data
->leaf
.tid
, data
->leaf
.ip
,
4019 le32_to_cpu(lh
->index
), bn
, n
,
4023 release_metapage(mp
);
4029 /* advance next available entry index of stbl */
4030 ++p
->header
.nextindex
;
4037 * function: move entries from split/left page to new/right page
4039 * nextindex of dst page and freelist/freecnt of both pages
4042 static void dtMoveEntry(dtpage_t
* sp
, int si
, dtpage_t
* dp
,
4043 struct dt_lock
** sdtlock
, struct dt_lock
** ddtlock
,
4046 int ssi
, next
; /* src slot index */
4047 int di
; /* dst entry index */
4048 int dsi
; /* dst slot index */
4049 s8
*sstbl
, *dstbl
; /* sorted entry table */
4051 struct ldtentry
*slh
, *dlh
= NULL
;
4052 struct idtentry
*sih
, *dih
= NULL
;
4053 struct dtslot
*h
, *s
, *d
;
4054 struct dt_lock
*sdtlck
= *sdtlock
, *ddtlck
= *ddtlock
;
4055 struct lv
*slv
, *dlv
;
4059 sstbl
= (s8
*) & sp
->slot
[sp
->header
.stblindex
];
4060 dstbl
= (s8
*) & dp
->slot
[dp
->header
.stblindex
];
4062 dsi
= dp
->header
.freelist
; /* first (whole page) free slot */
4063 sfsi
= sp
->header
.freelist
;
4065 /* linelock destination entry slot */
4066 dlv
= & ddtlck
->lv
[ddtlck
->index
];
4069 /* linelock source entry slot */
4070 slv
= & sdtlck
->lv
[sdtlck
->index
];
4071 slv
->offset
= sstbl
[si
];
4072 xssi
= slv
->offset
- 1;
4078 for (di
= 0; si
< sp
->header
.nextindex
; si
++, di
++) {
4082 /* is next slot contiguous ? */
4083 if (ssi
!= xssi
+ 1) {
4084 /* close current linelock */
4088 /* open new linelock */
4089 if (sdtlck
->index
< sdtlck
->maxcnt
)
4092 sdtlck
= (struct dt_lock
*) txLinelock(sdtlck
);
4093 slv
= & sdtlck
->lv
[0];
4101 * move head/only segment of an entry
4104 h
= d
= &dp
->slot
[dsi
];
4106 /* get src slot and move */
4108 if (sp
->header
.flag
& BT_LEAF
) {
4109 /* get source entry */
4110 slh
= (struct ldtentry
*) s
;
4111 dlh
= (struct ldtentry
*) h
;
4112 snamlen
= slh
->namlen
;
4115 len
= min(snamlen
, DTLHDRDATALEN
);
4116 dlh
->index
= slh
->index
; /* little-endian */
4118 len
= min(snamlen
, DTLHDRDATALEN_LEGACY
);
4120 memcpy(dlh
, slh
, 6 + len
* 2);
4124 /* update dst head/only segment next field */
4128 sih
= (struct idtentry
*) s
;
4129 snamlen
= sih
->namlen
;
4131 len
= min(snamlen
, DTIHDRDATALEN
);
4132 dih
= (struct idtentry
*) h
;
4133 memcpy(dih
, sih
, 10 + len
* 2);
4140 /* free src head/only segment */
4150 * move additional segment(s) of the entry
4153 while ((ssi
= next
) >= 0) {
4154 /* is next slot contiguous ? */
4155 if (ssi
!= xssi
+ 1) {
4156 /* close current linelock */
4160 /* open new linelock */
4161 if (sdtlck
->index
< sdtlck
->maxcnt
)
4167 slv
= & sdtlck
->lv
[0];
4174 /* get next source segment */
4177 /* get next destination free slot */
4180 len
= min(snamlen
, DTSLOTDATALEN
);
4181 UniStrncpy_le(d
->name
, s
->name
, len
);
4190 /* free source segment */
4199 /* terminate dst last/only segment */
4201 /* single segment entry */
4202 if (dp
->header
.flag
& BT_LEAF
)
4207 /* multi-segment entry */
4211 /* close current linelock */
4220 /* update source header */
4221 sp
->header
.freelist
= sfsi
;
4222 sp
->header
.freecnt
+= nd
;
4224 /* update destination header */
4225 dp
->header
.nextindex
= di
;
4227 dp
->header
.freelist
= dsi
;
4228 dp
->header
.freecnt
-= nd
;
4235 * function: free a (leaf/internal) entry
4237 * log freelist header, stbl, and each segment slot of entry
4238 * (even though last/only segment next field is modified,
4239 * physical image logging requires all segment slots of
4240 * the entry logged to avoid applying previous updates
4241 * to the same slots)
4243 static void dtDeleteEntry(dtpage_t
* p
, int fi
, struct dt_lock
** dtlock
)
4245 int fsi
; /* free entry slot index */
4249 struct dt_lock
*dtlck
= *dtlock
;
4253 /* get free entry slot index */
4254 stbl
= DT_GETSTBL(p
);
4257 /* open new linelock */
4258 if (dtlck
->index
>= dtlck
->maxcnt
)
4259 dtlck
= (struct dt_lock
*) txLinelock(dtlck
);
4260 lv
= & dtlck
->lv
[dtlck
->index
];
4264 /* get the head/only segment */
4266 if (p
->header
.flag
& BT_LEAF
)
4267 si
= ((struct ldtentry
*) t
)->next
;
4269 si
= ((struct idtentry
*) t
)->next
;
4276 /* find the last/only segment */
4278 /* is next slot contiguous ? */
4279 if (si
!= xsi
+ 1) {
4280 /* close current linelock */
4284 /* open new linelock */
4285 if (dtlck
->index
< dtlck
->maxcnt
)
4288 dtlck
= (struct dt_lock
*) txLinelock(dtlck
);
4289 lv
= & dtlck
->lv
[0];
4305 /* close current linelock */
4311 /* update freelist */
4312 t
->next
= p
->header
.freelist
;
4313 p
->header
.freelist
= fsi
;
4314 p
->header
.freecnt
+= freecnt
;
4316 /* if delete from middle,
4317 * shift left the succedding entries in the stbl
4319 si
= p
->header
.nextindex
;
4321 memmove(&stbl
[fi
], &stbl
[fi
+ 1], si
- fi
- 1);
4323 p
->header
.nextindex
--;
4330 * function: truncate a (leaf/internal) entry
4332 * log freelist header, stbl, and each segment slot of entry
4333 * (even though last/only segment next field is modified,
4334 * physical image logging requires all segment slots of
4335 * the entry logged to avoid applying previous updates
4336 * to the same slots)
4338 static void dtTruncateEntry(dtpage_t
* p
, int ti
, struct dt_lock
** dtlock
)
4340 int tsi
; /* truncate entry slot index */
4344 struct dt_lock
*dtlck
= *dtlock
;
4348 /* get free entry slot index */
4349 stbl
= DT_GETSTBL(p
);
4352 /* open new linelock */
4353 if (dtlck
->index
>= dtlck
->maxcnt
)
4354 dtlck
= (struct dt_lock
*) txLinelock(dtlck
);
4355 lv
= & dtlck
->lv
[dtlck
->index
];
4359 /* get the head/only segment */
4361 ASSERT(p
->header
.flag
& BT_INTERNAL
);
4362 ((struct idtentry
*) t
)->namlen
= 0;
4363 si
= ((struct idtentry
*) t
)->next
;
4364 ((struct idtentry
*) t
)->next
= -1;
4371 /* find the last/only segment */
4373 /* is next slot contiguous ? */
4374 if (si
!= xsi
+ 1) {
4375 /* close current linelock */
4379 /* open new linelock */
4380 if (dtlck
->index
< dtlck
->maxcnt
)
4383 dtlck
= (struct dt_lock
*) txLinelock(dtlck
);
4384 lv
= & dtlck
->lv
[0];
4400 /* close current linelock */
4406 /* update freelist */
4409 t
->next
= p
->header
.freelist
;
4410 p
->header
.freelist
= fsi
;
4411 p
->header
.freecnt
+= freecnt
;
4416 * dtLinelockFreelist()
4418 static void dtLinelockFreelist(dtpage_t
* p
, /* directory page */
4419 int m
, /* max slot index */
4420 struct dt_lock
** dtlock
)
4422 int fsi
; /* free entry slot index */
4425 struct dt_lock
*dtlck
= *dtlock
;
4429 /* get free entry slot index */
4430 fsi
= p
->header
.freelist
;
4432 /* open new linelock */
4433 if (dtlck
->index
>= dtlck
->maxcnt
)
4434 dtlck
= (struct dt_lock
*) txLinelock(dtlck
);
4435 lv
= & dtlck
->lv
[dtlck
->index
];
4445 /* find the last/only segment */
4446 while (si
< m
&& si
>= 0) {
4447 /* is next slot contiguous ? */
4448 if (si
!= xsi
+ 1) {
4449 /* close current linelock */
4453 /* open new linelock */
4454 if (dtlck
->index
< dtlck
->maxcnt
)
4457 dtlck
= (struct dt_lock
*) txLinelock(dtlck
);
4458 lv
= & dtlck
->lv
[0];
4472 /* close current linelock */
4483 * FUNCTION: Modify the inode number part of a directory entry
4486 * tid - Transaction id
4487 * ip - Inode of parent directory
4488 * key - Name of entry to be modified
4489 * orig_ino - Original inode number expected in entry
4490 * new_ino - New inode number to put into entry
4494 * -ESTALE - If entry found does not match orig_ino passed in
4495 * -ENOENT - If no entry can be found to match key
4496 * 0 - If successfully modified entry
4498 int dtModify(tid_t tid
, struct inode
*ip
,
4499 struct component_name
* key
, ino_t
* orig_ino
, ino_t new_ino
, int flag
)
4503 struct metapage
*mp
;
4506 struct btstack btstack
;
4508 struct dt_lock
*dtlck
;
4511 int entry_si
; /* entry slot index */
4512 struct ldtentry
*entry
;
4515 * search for the entry to modify:
4517 * dtSearch() returns (leaf page pinned, index at which to modify).
4519 if ((rc
= dtSearch(ip
, key
, orig_ino
, &btstack
, flag
)))
4522 /* retrieve search result */
4523 DT_GETSEARCH(ip
, btstack
.top
, bn
, mp
, p
, index
);
4525 BT_MARK_DIRTY(mp
, ip
);
4527 * acquire a transaction lock on the leaf page of named entry
4529 tlck
= txLock(tid
, ip
, mp
, tlckDTREE
| tlckENTRY
);
4530 dtlck
= (struct dt_lock
*) & tlck
->lock
;
4532 /* get slot index of the entry */
4533 stbl
= DT_GETSTBL(p
);
4534 entry_si
= stbl
[index
];
4536 /* linelock entry */
4537 ASSERT(dtlck
->index
== 0);
4538 lv
= & dtlck
->lv
[0];
4539 lv
->offset
= entry_si
;
4543 /* get the head/only segment */
4544 entry
= (struct ldtentry
*) & p
->slot
[entry_si
];
4546 /* substitute the inode number of the entry */
4547 entry
->inumber
= cpu_to_le32(new_ino
);
4549 /* unpin the leaf page */
4555 #ifdef _JFS_DEBUG_DTREE
4559 * function: traverse forward
4561 int dtDisplayTree(struct inode
*ip
)
4564 struct metapage
*mp
;
4567 int index
, lastindex
, v
, h
;
4569 struct btstack btstack
;
4570 struct btframe
*btsp
;
4571 struct btframe
*parent
;
4575 printk("display B+-tree.\n");
4578 btsp
= btstack
.stack
;
4583 * root resides in the inode
4589 * first access of each page:
4592 DT_GETPAGE(ip
, bn
, mp
, psize
, p
, rc
);
4596 /* process entries forward from first index */
4598 lastindex
= p
->header
.nextindex
- 1;
4600 if (p
->header
.flag
& BT_INTERNAL
) {
4602 * first access of each internal page
4604 printf("internal page ");
4605 dtDisplayPage(ip
, bn
, p
);
4608 } else { /* (p->header.flag & BT_LEAF) */
4611 * first access of each leaf page
4613 printf("leaf page ");
4614 dtDisplayPage(ip
, bn
, p
);
4617 * process leaf page entries
4619 for ( ; index <= lastindex; index++)
4624 /* unpin the leaf page */
4629 * go back up to the parent page
4632 /* pop/restore parent entry for the current child page */
4633 if ((parent
= (btsp
== btstack
.stack
? NULL
: --btsp
)) == NULL
)
4634 /* current page must have been root */
4638 * parent page scan completed
4640 if ((index
= parent
->index
) == (lastindex
= parent
->lastindex
)) {
4641 /* go back up to the parent page */
4646 * parent page has entries remaining
4648 /* get back the parent page */
4650 /* v = parent->level; */
4651 DT_GETPAGE(ip
, bn
, mp
, PSIZE
, p
, rc
);
4655 /* get next parent entry */
4659 * internal page: go down to child page of current entry
4662 /* push/save current parent entry for the child page */
4663 btsp
->bn
= pbn
= bn
;
4664 btsp
->index
= index
;
4665 btsp
->lastindex
= lastindex
;
4666 /* btsp->level = v; */
4667 /* btsp->node = h; */
4670 /* get current entry for the child page */
4671 stbl
= DT_GETSTBL(p
);
4672 xd
= (pxd_t
*) & p
->slot
[stbl
[index
]];
4675 * first access of each internal entry:
4678 /* get child page */
4679 bn
= addressPXD(xd
);
4680 psize
= lengthPXD(xd
) << ip
->i_ipmnt
->i_l2bsize
;
4682 printk("traverse down 0x%Lx[%d]->0x%Lx\n", pbn
, index
, bn
);
4686 /* release parent page */
4689 /* process the child page */
4697 * function: display page
4699 int dtDisplayPage(struct inode
*ip
, s64 bn
, dtpage_t
* p
)
4702 struct metapage
*mp
;
4703 struct ldtentry
*lh
;
4704 struct idtentry
*ih
;
4708 wchar_t name
[JFS_NAME_MAX
+ 1];
4709 struct component_name key
= { 0, name
};
4714 DT_GETPAGE(ip
, bn
, mp
, PSIZE
, p
, rc
);
4719 /* display page control */
4720 printk("bn:0x%Lx flag:0x%08x nextindex:%d\n",
4721 bn
, p
->header
.flag
, p
->header
.nextindex
);
4723 /* display entries */
4724 stbl
= DT_GETSTBL(p
);
4725 for (i
= 0, j
= 1; i
< p
->header
.nextindex
; i
++, j
++) {
4726 dtGetKey(p
, i
, &key
, JFS_SBI(ip
->i_sb
)->mntflag
);
4727 key
.name
[key
.namlen
] = '\0';
4728 if (p
->header
.flag
& BT_LEAF
) {
4729 lh
= (struct ldtentry
*) & p
->slot
[stbl
[i
]];
4730 printf("\t[%d] %s:%d", i
, key
.name
,
4731 le32_to_cpu(lh
->inumber
));
4733 ih
= (struct idtentry
*) & p
->slot
[stbl
[i
]];
4735 bn
= addressPXD(xd
);
4736 printf("\t[%d] %s:0x%Lx", i
, key
.name
, bn
);
4752 #endif /* _JFS_DEBUG_DTREE */