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
);
2935 tlck
->type
|= tlckBTROOT
;
2937 dtlck
= (struct dt_lock
*) &tlck
->lock
;
2939 stbl
= DT_GETSTBL(p
);
2940 for (i
= 0; i
< p
->header
.nextindex
; i
++) {
2941 d
= (struct ldtentry
*) &p
->slot
[stbl
[i
]];
2942 index
= le32_to_cpu(d
->index
);
2943 if ((index
< 2) || (index
>= JFS_IP(inode
)->next_index
)) {
2944 d
->index
= cpu_to_le32(add_index(tid
, inode
, bn
, i
));
2945 if (dtlck
->index
>= dtlck
->maxcnt
)
2946 dtlck
= (struct dt_lock
*) txLinelock(dtlck
);
2947 lv
= &dtlck
->lv
[dtlck
->index
];
2948 lv
->offset
= stbl
[i
];
2955 (void) txCommit(tid
, 1, &inode
, 0);
2961 * Buffer to hold directory entry info while traversing a dtree page
2962 * before being fed to the filldir function
2972 * function to determine next variable-sized jfs_dirent in buffer
2974 static inline struct jfs_dirent
*next_jfs_dirent(struct jfs_dirent
*dirent
)
2976 return (struct jfs_dirent
*)
2978 ((sizeof (struct jfs_dirent
) + dirent
->name_len
+ 1 +
2979 sizeof (loff_t
) - 1) &
2980 ~(sizeof (loff_t
) - 1)));
2986 * function: read directory entries sequentially
2987 * from the specified entry offset
2991 * return: offset = (pn, index) of start entry
2992 * of next jfs_readdir()/dtRead()
2994 int jfs_readdir(struct file
*filp
, void *dirent
, filldir_t filldir
)
2996 struct inode
*ip
= filp
->f_dentry
->d_inode
;
2997 struct nls_table
*codepage
= JFS_SBI(ip
->i_sb
)->nls_tab
;
2999 loff_t dtpos
; /* legacy OS/2 style position */
3004 } *dtoffset
= (struct dtoffset
*) &dtpos
;
3006 struct metapage
*mp
;
3010 struct btstack btstack
;
3014 int d_namleft
, len
, outlen
;
3015 unsigned long dirent_buf
;
3019 uint loop_count
= 0;
3020 struct jfs_dirent
*jfs_dirent
;
3022 int overflow
, fix_page
, page_fixed
= 0;
3023 static int unique_pos
= 2; /* If we can't fix broken index */
3025 if (filp
->f_pos
== DIREND
)
3030 * persistent index is stored in directory entries.
3031 * Special cases: 0 = .
3033 * -1 = End of directory
3037 dir_index
= (u32
) filp
->f_pos
;
3039 if (dir_index
> 1) {
3040 struct dir_table_slot dirtab_slot
;
3043 (dir_index
>= JFS_IP(ip
)->next_index
)) {
3044 /* Stale position. Directory has shrunk */
3045 filp
->f_pos
= DIREND
;
3049 rc
= read_index(ip
, dir_index
, &dirtab_slot
);
3051 filp
->f_pos
= DIREND
;
3054 if (dirtab_slot
.flag
== DIR_INDEX_FREE
) {
3055 if (loop_count
++ > JFS_IP(ip
)->next_index
) {
3056 jfs_err("jfs_readdir detected "
3058 filp
->f_pos
= DIREND
;
3061 dir_index
= le32_to_cpu(dirtab_slot
.addr2
);
3062 if (dir_index
== -1) {
3063 filp
->f_pos
= DIREND
;
3068 bn
= addressDTS(&dirtab_slot
);
3069 index
= dirtab_slot
.slot
;
3070 DT_GETPAGE(ip
, bn
, mp
, PSIZE
, p
, rc
);
3072 filp
->f_pos
= DIREND
;
3075 if (p
->header
.flag
& BT_INTERNAL
) {
3076 jfs_err("jfs_readdir: bad index table");
3082 if (dir_index
== 0) {
3087 if (filldir(dirent
, ".", 1, 0, ip
->i_ino
,
3095 if (filldir(dirent
, "..", 2, 1, PARENT(ip
), DT_DIR
))
3099 * Find first entry of left-most leaf
3102 filp
->f_pos
= DIREND
;
3106 if ((rc
= dtReadFirst(ip
, &btstack
)))
3109 DT_GETSEARCH(ip
, btstack
.top
, bn
, mp
, p
, index
);
3113 * Legacy filesystem - OS/2 & Linux JFS < 0.3.6
3115 * pn = index = 0: First entry "."
3116 * pn = 0; index = 1: Second entry ".."
3117 * pn > 0: Real entries, pn=1 -> leftmost page
3118 * pn = index = -1: No more entries
3120 dtpos
= filp
->f_pos
;
3122 /* build "." entry */
3124 if (filldir(dirent
, ".", 1, filp
->f_pos
, ip
->i_ino
,
3127 dtoffset
->index
= 1;
3128 filp
->f_pos
= dtpos
;
3131 if (dtoffset
->pn
== 0) {
3132 if (dtoffset
->index
== 1) {
3133 /* build ".." entry */
3135 if (filldir(dirent
, "..", 2, filp
->f_pos
,
3136 PARENT(ip
), DT_DIR
))
3139 jfs_err("jfs_readdir called with "
3143 dtoffset
->index
= 0;
3144 filp
->f_pos
= dtpos
;
3148 filp
->f_pos
= DIREND
;
3152 if ((rc
= dtReadNext(ip
, &filp
->f_pos
, &btstack
))) {
3153 jfs_err("jfs_readdir: unexpected rc = %d "
3154 "from dtReadNext", rc
);
3155 filp
->f_pos
= DIREND
;
3158 /* get start leaf page and index */
3159 DT_GETSEARCH(ip
, btstack
.top
, bn
, mp
, p
, index
);
3161 /* offset beyond directory eof ? */
3163 filp
->f_pos
= DIREND
;
3168 dirent_buf
= __get_free_page(GFP_KERNEL
);
3169 if (dirent_buf
== 0) {
3171 jfs_warn("jfs_readdir: __get_free_page failed!");
3172 filp
->f_pos
= DIREND
;
3177 jfs_dirent
= (struct jfs_dirent
*) dirent_buf
;
3179 overflow
= fix_page
= 0;
3181 stbl
= DT_GETSTBL(p
);
3183 for (i
= index
; i
< p
->header
.nextindex
; i
++) {
3184 d
= (struct ldtentry
*) & p
->slot
[stbl
[i
]];
3186 if (((long) jfs_dirent
+ d
->namlen
+ 1) >
3187 (dirent_buf
+ PAGE_SIZE
)) {
3188 /* DBCS codepages could overrun dirent_buf */
3194 d_namleft
= d
->namlen
;
3195 name_ptr
= jfs_dirent
->name
;
3196 jfs_dirent
->ino
= le32_to_cpu(d
->inumber
);
3199 len
= min(d_namleft
, DTLHDRDATALEN
);
3200 jfs_dirent
->position
= le32_to_cpu(d
->index
);
3202 * d->index should always be valid, but it
3203 * isn't. fsck.jfs doesn't create the
3204 * directory index for the lost+found
3205 * directory. Rather than let it go,
3206 * we can try to fix it.
3208 if ((jfs_dirent
->position
< 2) ||
3209 (jfs_dirent
->position
>=
3210 JFS_IP(ip
)->next_index
)) {
3211 if (!page_fixed
&& !isReadOnly(ip
)) {
3214 * setting overflow and setting
3215 * index to i will cause the
3216 * same page to be processed
3217 * again starting here
3223 jfs_dirent
->position
= unique_pos
++;
3226 jfs_dirent
->position
= dtpos
;
3227 len
= min(d_namleft
, DTLHDRDATALEN_LEGACY
);
3230 /* copy the name of head/only segment */
3231 outlen
= jfs_strfromUCS_le(name_ptr
, d
->name
, len
,
3233 jfs_dirent
->name_len
= outlen
;
3235 /* copy name in the additional segment(s) */
3238 t
= (struct dtslot
*) & p
->slot
[next
];
3242 if (d_namleft
== 0) {
3244 "JFS:Dtree error: ino = "
3245 "%ld, bn=%Ld, index = %d",
3251 len
= min(d_namleft
, DTSLOTDATALEN
);
3252 outlen
= jfs_strfromUCS_le(name_ptr
, t
->name
,
3254 jfs_dirent
->name_len
+= outlen
;
3260 jfs_dirent
= next_jfs_dirent(jfs_dirent
);
3267 /* Point to next leaf page */
3268 if (p
->header
.flag
& BT_ROOT
)
3271 bn
= le64_to_cpu(p
->header
.next
);
3273 /* update offset (pn:index) for new page */
3276 dtoffset
->index
= 0;
3282 /* unpin previous leaf page */
3285 jfs_dirent
= (struct jfs_dirent
*) dirent_buf
;
3286 while (jfs_dirents
--) {
3287 filp
->f_pos
= jfs_dirent
->position
;
3288 if (filldir(dirent
, jfs_dirent
->name
,
3289 jfs_dirent
->name_len
, filp
->f_pos
,
3290 jfs_dirent
->ino
, DT_UNKNOWN
))
3292 jfs_dirent
= next_jfs_dirent(jfs_dirent
);
3296 add_missing_indices(ip
, bn
);
3300 if (!overflow
&& (bn
== 0)) {
3301 filp
->f_pos
= DIREND
;
3305 DT_GETPAGE(ip
, bn
, mp
, PSIZE
, p
, rc
);
3307 free_page(dirent_buf
);
3313 free_page(dirent_buf
);
3322 * function: get the leftmost page of the directory
3324 static int dtReadFirst(struct inode
*ip
, struct btstack
* btstack
)
3328 int psize
= 288; /* initial in-line directory */
3329 struct metapage
*mp
;
3332 struct btframe
*btsp
;
3335 BT_CLR(btstack
); /* reset stack */
3338 * descend leftmost path of the tree
3340 * by convention, root bn = 0.
3343 DT_GETPAGE(ip
, bn
, mp
, psize
, p
, rc
);
3348 * leftmost leaf page
3350 if (p
->header
.flag
& BT_LEAF
) {
3351 /* return leftmost entry */
3352 btsp
= btstack
->top
;
3361 * descend down to leftmost child page
3363 if (BT_STACK_FULL(btstack
)) {
3365 jfs_error(ip
->i_sb
, "dtReadFirst: btstack overrun");
3366 BT_STACK_DUMP(btstack
);
3369 /* push (bn, index) of the parent page/entry */
3370 BT_PUSH(btstack
, bn
, 0);
3372 /* get the leftmost entry */
3373 stbl
= DT_GETSTBL(p
);
3374 xd
= (pxd_t
*) & p
->slot
[stbl
[0]];
3376 /* get the child page block address */
3377 bn
= addressPXD(xd
);
3378 psize
= lengthPXD(xd
) << JFS_SBI(ip
->i_sb
)->l2bsize
;
3380 /* unpin the parent page */
3389 * function: get the page of the specified offset (pn:index)
3391 * return: if (offset > eof), bn = -1;
3393 * note: if index > nextindex of the target leaf page,
3394 * start with 1st entry of next leaf page;
3396 static int dtReadNext(struct inode
*ip
, loff_t
* offset
,
3397 struct btstack
* btstack
)
3404 } *dtoffset
= (struct dtoffset
*) offset
;
3406 struct metapage
*mp
;
3411 struct btframe
*btsp
, *parent
;
3415 * get leftmost leaf page pinned
3417 if ((rc
= dtReadFirst(ip
, btstack
)))
3421 DT_GETSEARCH(ip
, btstack
->top
, bn
, mp
, p
, index
);
3423 /* get the start offset (pn:index) */
3424 pn
= dtoffset
->pn
- 1; /* Now pn = 0 represents leftmost leaf */
3425 index
= dtoffset
->index
;
3427 /* start at leftmost page ? */
3429 /* offset beyond eof ? */
3430 if (index
< p
->header
.nextindex
)
3433 if (p
->header
.flag
& BT_ROOT
) {
3438 /* start with 1st entry of next leaf page */
3440 dtoffset
->index
= index
= 0;
3444 /* start at non-leftmost page: scan parent pages for large pn */
3445 if (p
->header
.flag
& BT_ROOT
) {
3450 /* start after next leaf page ? */
3454 /* get leaf page pn = 1 */
3456 bn
= le64_to_cpu(p
->header
.next
);
3458 /* unpin leaf page */
3461 /* offset beyond eof ? */
3470 * scan last internal page level to get target leaf page
3473 /* unpin leftmost leaf page */
3476 /* get left most parent page */
3477 btsp
= btstack
->top
;
3480 DT_GETPAGE(ip
, bn
, mp
, PSIZE
, p
, rc
);
3484 /* scan parent pages at last internal page level */
3485 while (pn
>= p
->header
.nextindex
) {
3486 pn
-= p
->header
.nextindex
;
3488 /* get next parent page address */
3489 bn
= le64_to_cpu(p
->header
.next
);
3491 /* unpin current parent page */
3494 /* offset beyond eof ? */
3500 /* get next parent page */
3501 DT_GETPAGE(ip
, bn
, mp
, PSIZE
, p
, rc
);
3505 /* update parent page stack frame */
3509 /* get leaf page address */
3510 stbl
= DT_GETSTBL(p
);
3511 xd
= (pxd_t
*) & p
->slot
[stbl
[pn
]];
3512 bn
= addressPXD(xd
);
3514 /* unpin parent page */
3518 * get target leaf page
3521 DT_GETPAGE(ip
, bn
, mp
, PSIZE
, p
, rc
);
3526 * leaf page has been completed:
3527 * start with 1st entry of next leaf page
3529 if (index
>= p
->header
.nextindex
) {
3530 bn
= le64_to_cpu(p
->header
.next
);
3532 /* unpin leaf page */
3535 /* offset beyond eof ? */
3541 /* get next leaf page */
3542 DT_GETPAGE(ip
, bn
, mp
, PSIZE
, p
, rc
);
3546 /* start with 1st entry of next leaf page */
3548 dtoffset
->index
= 0;
3552 /* return target leaf page pinned */
3553 btsp
= btstack
->top
;
3555 btsp
->index
= dtoffset
->index
;
3565 * function: compare search key with an internal entry
3568 * < 0 if k is < record
3569 * = 0 if k is = record
3570 * > 0 if k is > record
3572 static int dtCompare(struct component_name
* key
, /* search key */
3573 dtpage_t
* p
, /* directory page */
3575 { /* entry slot index */
3578 int klen
, namlen
, len
, rc
;
3579 struct idtentry
*ih
;
3583 * force the left-most key on internal pages, at any level of
3584 * the tree, to be less than any search key.
3585 * this obviates having to update the leftmost key on an internal
3586 * page when the user inserts a new key in the tree smaller than
3587 * anything that has been stored.
3589 * (? if/when dtSearch() narrows down to 1st entry (index = 0),
3590 * at any internal page at any level of the tree,
3591 * it descends to child of the entry anyway -
3592 * ? make the entry as min size dummy entry)
3594 * if (e->index == 0 && h->prevpg == P_INVALID && !(h->flags & BT_LEAF))
3601 ih
= (struct idtentry
*) & p
->slot
[si
];
3604 namlen
= ih
->namlen
;
3605 len
= min(namlen
, DTIHDRDATALEN
);
3607 /* compare with head/only segment */
3608 len
= min(klen
, len
);
3609 if ((rc
= UniStrncmp_le(kname
, name
, len
)))
3615 /* compare with additional segment(s) */
3617 while (klen
> 0 && namlen
> 0) {
3618 /* compare with next name segment */
3619 t
= (struct dtslot
*) & p
->slot
[si
];
3620 len
= min(namlen
, DTSLOTDATALEN
);
3621 len
= min(klen
, len
);
3623 if ((rc
= UniStrncmp_le(kname
, name
, len
)))
3632 return (klen
- namlen
);
3641 * function: compare search key with an (leaf/internal) entry
3644 * < 0 if k is < record
3645 * = 0 if k is = record
3646 * > 0 if k is > record
3648 static int ciCompare(struct component_name
* key
, /* search key */
3649 dtpage_t
* p
, /* directory page */
3650 int si
, /* entry slot index */
3655 int klen
, namlen
, len
, rc
;
3656 struct ldtentry
*lh
;
3657 struct idtentry
*ih
;
3662 * force the left-most key on internal pages, at any level of
3663 * the tree, to be less than any search key.
3664 * this obviates having to update the leftmost key on an internal
3665 * page when the user inserts a new key in the tree smaller than
3666 * anything that has been stored.
3668 * (? if/when dtSearch() narrows down to 1st entry (index = 0),
3669 * at any internal page at any level of the tree,
3670 * it descends to child of the entry anyway -
3671 * ? make the entry as min size dummy entry)
3673 * if (e->index == 0 && h->prevpg == P_INVALID && !(h->flags & BT_LEAF))
3683 if (p
->header
.flag
& BT_LEAF
) {
3684 lh
= (struct ldtentry
*) & p
->slot
[si
];
3687 namlen
= lh
->namlen
;
3688 if (flag
& JFS_DIR_INDEX
)
3689 len
= min(namlen
, DTLHDRDATALEN
);
3691 len
= min(namlen
, DTLHDRDATALEN_LEGACY
);
3694 * internal page entry
3697 ih
= (struct idtentry
*) & p
->slot
[si
];
3700 namlen
= ih
->namlen
;
3701 len
= min(namlen
, DTIHDRDATALEN
);
3704 /* compare with head/only segment */
3705 len
= min(klen
, len
);
3706 for (i
= 0; i
< len
; i
++, kname
++, name
++) {
3707 /* only uppercase if case-insensitive support is on */
3708 if ((flag
& JFS_OS2
) == JFS_OS2
)
3709 x
= UniToupper(le16_to_cpu(*name
));
3711 x
= le16_to_cpu(*name
);
3712 if ((rc
= *kname
- x
))
3719 /* compare with additional segment(s) */
3720 while (klen
> 0 && namlen
> 0) {
3721 /* compare with next name segment */
3722 t
= (struct dtslot
*) & p
->slot
[si
];
3723 len
= min(namlen
, DTSLOTDATALEN
);
3724 len
= min(klen
, len
);
3726 for (i
= 0; i
< len
; i
++, kname
++, name
++) {
3727 /* only uppercase if case-insensitive support is on */
3728 if ((flag
& JFS_OS2
) == JFS_OS2
)
3729 x
= UniToupper(le16_to_cpu(*name
));
3731 x
= le16_to_cpu(*name
);
3733 if ((rc
= *kname
- x
))
3742 return (klen
- namlen
);
3747 * ciGetLeafPrefixKey()
3749 * function: compute prefix of suffix compression
3750 * from two adjacent leaf entries
3751 * across page boundary
3753 * return: non-zero on error
3756 static int ciGetLeafPrefixKey(dtpage_t
* lp
, int li
, dtpage_t
* rp
,
3757 int ri
, struct component_name
* key
, int flag
)
3760 wchar_t *pl
, *pr
, *kname
;
3761 struct component_name lkey
;
3762 struct component_name rkey
;
3764 lkey
.name
= (wchar_t *) kmalloc((JFS_NAME_MAX
+ 1) * sizeof(wchar_t),
3766 if (lkey
.name
== NULL
)
3769 rkey
.name
= (wchar_t *) kmalloc((JFS_NAME_MAX
+ 1) * sizeof(wchar_t),
3771 if (rkey
.name
== NULL
) {
3776 /* get left and right key */
3777 dtGetKey(lp
, li
, &lkey
, flag
);
3778 lkey
.name
[lkey
.namlen
] = 0;
3780 if ((flag
& JFS_OS2
) == JFS_OS2
)
3783 dtGetKey(rp
, ri
, &rkey
, flag
);
3784 rkey
.name
[rkey
.namlen
] = 0;
3787 if ((flag
& JFS_OS2
) == JFS_OS2
)
3790 /* compute prefix */
3793 namlen
= min(lkey
.namlen
, rkey
.namlen
);
3794 for (pl
= lkey
.name
, pr
= rkey
.name
;
3795 namlen
; pl
++, pr
++, namlen
--, klen
++, kname
++) {
3798 key
->namlen
= klen
+ 1;
3803 /* l->namlen <= r->namlen since l <= r */
3804 if (lkey
.namlen
< rkey
.namlen
) {
3806 key
->namlen
= klen
+ 1;
3807 } else /* l->namelen == r->namelen */
3821 * function: get key of the entry
3823 static void dtGetKey(dtpage_t
* p
, int i
, /* entry index */
3824 struct component_name
* key
, int flag
)
3828 struct ldtentry
*lh
;
3829 struct idtentry
*ih
;
3836 stbl
= DT_GETSTBL(p
);
3838 if (p
->header
.flag
& BT_LEAF
) {
3839 lh
= (struct ldtentry
*) & p
->slot
[si
];
3841 namlen
= lh
->namlen
;
3843 if (flag
& JFS_DIR_INDEX
)
3844 len
= min(namlen
, DTLHDRDATALEN
);
3846 len
= min(namlen
, DTLHDRDATALEN_LEGACY
);
3848 ih
= (struct idtentry
*) & p
->slot
[si
];
3850 namlen
= ih
->namlen
;
3852 len
= min(namlen
, DTIHDRDATALEN
);
3855 key
->namlen
= namlen
;
3859 * move head/only segment
3861 UniStrncpy_from_le(kname
, name
, len
);
3864 * move additional segment(s)
3867 /* get next segment */
3871 len
= min(namlen
, DTSLOTDATALEN
);
3872 UniStrncpy_from_le(kname
, t
->name
, len
);
3882 * function: allocate free slot(s) and
3883 * write a leaf/internal entry
3885 * return: entry slot index
3887 static void dtInsertEntry(dtpage_t
* p
, int index
, struct component_name
* key
,
3888 ddata_t
* data
, struct dt_lock
** dtlock
)
3890 struct dtslot
*h
, *t
;
3891 struct ldtentry
*lh
= NULL
;
3892 struct idtentry
*ih
= NULL
;
3893 int hsi
, fsi
, klen
, len
, nextindex
;
3898 struct dt_lock
*dtlck
= *dtlock
;
3902 struct metapage
*mp
= NULL
;
3907 /* allocate a free slot */
3908 hsi
= fsi
= p
->header
.freelist
;
3910 p
->header
.freelist
= h
->next
;
3911 --p
->header
.freecnt
;
3913 /* open new linelock */
3914 if (dtlck
->index
>= dtlck
->maxcnt
)
3915 dtlck
= (struct dt_lock
*) txLinelock(dtlck
);
3917 lv
= & dtlck
->lv
[dtlck
->index
];
3920 /* write head/only segment */
3921 if (p
->header
.flag
& BT_LEAF
) {
3922 lh
= (struct ldtentry
*) h
;
3924 lh
->inumber
= cpu_to_le32(data
->leaf
.ino
);
3927 if (data
->leaf
.ip
) {
3928 len
= min(klen
, DTLHDRDATALEN
);
3929 if (!(p
->header
.flag
& BT_ROOT
))
3930 bn
= addressPXD(&p
->header
.self
);
3931 lh
->index
= cpu_to_le32(add_index(data
->leaf
.tid
,
3935 len
= min(klen
, DTLHDRDATALEN_LEGACY
);
3937 ih
= (struct idtentry
*) h
;
3943 len
= min(klen
, DTIHDRDATALEN
);
3946 UniStrncpy_to_le(name
, kname
, len
);
3951 /* write additional segment(s) */
3956 fsi
= p
->header
.freelist
;
3958 p
->header
.freelist
= t
->next
;
3959 --p
->header
.freecnt
;
3961 /* is next slot contiguous ? */
3962 if (fsi
!= xsi
+ 1) {
3963 /* close current linelock */
3967 /* open new linelock */
3968 if (dtlck
->index
< dtlck
->maxcnt
)
3971 dtlck
= (struct dt_lock
*) txLinelock(dtlck
);
3972 lv
= & dtlck
->lv
[0];
3980 len
= min(klen
, DTSLOTDATALEN
);
3981 UniStrncpy_to_le(t
->name
, kname
, len
);
3988 /* close current linelock */
3994 /* terminate last/only segment */
3996 /* single segment entry */
3997 if (p
->header
.flag
& BT_LEAF
)
4002 /* multi-segment entry */
4005 /* if insert into middle, shift right succeeding entries in stbl */
4006 stbl
= DT_GETSTBL(p
);
4007 nextindex
= p
->header
.nextindex
;
4008 if (index
< nextindex
) {
4009 memmove(stbl
+ index
+ 1, stbl
+ index
, nextindex
- index
);
4011 if ((p
->header
.flag
& BT_LEAF
) && data
->leaf
.ip
) {
4015 * Need to update slot number for entries that moved
4019 for (n
= index
+ 1; n
<= nextindex
; n
++) {
4020 lh
= (struct ldtentry
*) & (p
->slot
[stbl
[n
]]);
4021 modify_index(data
->leaf
.tid
, data
->leaf
.ip
,
4022 le32_to_cpu(lh
->index
), bn
, n
,
4026 release_metapage(mp
);
4032 /* advance next available entry index of stbl */
4033 ++p
->header
.nextindex
;
4040 * function: move entries from split/left page to new/right page
4042 * nextindex of dst page and freelist/freecnt of both pages
4045 static void dtMoveEntry(dtpage_t
* sp
, int si
, dtpage_t
* dp
,
4046 struct dt_lock
** sdtlock
, struct dt_lock
** ddtlock
,
4049 int ssi
, next
; /* src slot index */
4050 int di
; /* dst entry index */
4051 int dsi
; /* dst slot index */
4052 s8
*sstbl
, *dstbl
; /* sorted entry table */
4054 struct ldtentry
*slh
, *dlh
= NULL
;
4055 struct idtentry
*sih
, *dih
= NULL
;
4056 struct dtslot
*h
, *s
, *d
;
4057 struct dt_lock
*sdtlck
= *sdtlock
, *ddtlck
= *ddtlock
;
4058 struct lv
*slv
, *dlv
;
4062 sstbl
= (s8
*) & sp
->slot
[sp
->header
.stblindex
];
4063 dstbl
= (s8
*) & dp
->slot
[dp
->header
.stblindex
];
4065 dsi
= dp
->header
.freelist
; /* first (whole page) free slot */
4066 sfsi
= sp
->header
.freelist
;
4068 /* linelock destination entry slot */
4069 dlv
= & ddtlck
->lv
[ddtlck
->index
];
4072 /* linelock source entry slot */
4073 slv
= & sdtlck
->lv
[sdtlck
->index
];
4074 slv
->offset
= sstbl
[si
];
4075 xssi
= slv
->offset
- 1;
4081 for (di
= 0; si
< sp
->header
.nextindex
; si
++, di
++) {
4085 /* is next slot contiguous ? */
4086 if (ssi
!= xssi
+ 1) {
4087 /* close current linelock */
4091 /* open new linelock */
4092 if (sdtlck
->index
< sdtlck
->maxcnt
)
4095 sdtlck
= (struct dt_lock
*) txLinelock(sdtlck
);
4096 slv
= & sdtlck
->lv
[0];
4104 * move head/only segment of an entry
4107 h
= d
= &dp
->slot
[dsi
];
4109 /* get src slot and move */
4111 if (sp
->header
.flag
& BT_LEAF
) {
4112 /* get source entry */
4113 slh
= (struct ldtentry
*) s
;
4114 dlh
= (struct ldtentry
*) h
;
4115 snamlen
= slh
->namlen
;
4118 len
= min(snamlen
, DTLHDRDATALEN
);
4119 dlh
->index
= slh
->index
; /* little-endian */
4121 len
= min(snamlen
, DTLHDRDATALEN_LEGACY
);
4123 memcpy(dlh
, slh
, 6 + len
* 2);
4127 /* update dst head/only segment next field */
4131 sih
= (struct idtentry
*) s
;
4132 snamlen
= sih
->namlen
;
4134 len
= min(snamlen
, DTIHDRDATALEN
);
4135 dih
= (struct idtentry
*) h
;
4136 memcpy(dih
, sih
, 10 + len
* 2);
4143 /* free src head/only segment */
4153 * move additional segment(s) of the entry
4156 while ((ssi
= next
) >= 0) {
4157 /* is next slot contiguous ? */
4158 if (ssi
!= xssi
+ 1) {
4159 /* close current linelock */
4163 /* open new linelock */
4164 if (sdtlck
->index
< sdtlck
->maxcnt
)
4170 slv
= & sdtlck
->lv
[0];
4177 /* get next source segment */
4180 /* get next destination free slot */
4183 len
= min(snamlen
, DTSLOTDATALEN
);
4184 UniStrncpy_le(d
->name
, s
->name
, len
);
4193 /* free source segment */
4202 /* terminate dst last/only segment */
4204 /* single segment entry */
4205 if (dp
->header
.flag
& BT_LEAF
)
4210 /* multi-segment entry */
4214 /* close current linelock */
4223 /* update source header */
4224 sp
->header
.freelist
= sfsi
;
4225 sp
->header
.freecnt
+= nd
;
4227 /* update destination header */
4228 dp
->header
.nextindex
= di
;
4230 dp
->header
.freelist
= dsi
;
4231 dp
->header
.freecnt
-= nd
;
4238 * function: free a (leaf/internal) entry
4240 * log freelist header, stbl, and each segment slot of entry
4241 * (even though last/only segment next field is modified,
4242 * physical image logging requires all segment slots of
4243 * the entry logged to avoid applying previous updates
4244 * to the same slots)
4246 static void dtDeleteEntry(dtpage_t
* p
, int fi
, struct dt_lock
** dtlock
)
4248 int fsi
; /* free entry slot index */
4252 struct dt_lock
*dtlck
= *dtlock
;
4256 /* get free entry slot index */
4257 stbl
= DT_GETSTBL(p
);
4260 /* open new linelock */
4261 if (dtlck
->index
>= dtlck
->maxcnt
)
4262 dtlck
= (struct dt_lock
*) txLinelock(dtlck
);
4263 lv
= & dtlck
->lv
[dtlck
->index
];
4267 /* get the head/only segment */
4269 if (p
->header
.flag
& BT_LEAF
)
4270 si
= ((struct ldtentry
*) t
)->next
;
4272 si
= ((struct idtentry
*) t
)->next
;
4279 /* find the last/only segment */
4281 /* is next slot contiguous ? */
4282 if (si
!= xsi
+ 1) {
4283 /* close current linelock */
4287 /* open new linelock */
4288 if (dtlck
->index
< dtlck
->maxcnt
)
4291 dtlck
= (struct dt_lock
*) txLinelock(dtlck
);
4292 lv
= & dtlck
->lv
[0];
4308 /* close current linelock */
4314 /* update freelist */
4315 t
->next
= p
->header
.freelist
;
4316 p
->header
.freelist
= fsi
;
4317 p
->header
.freecnt
+= freecnt
;
4319 /* if delete from middle,
4320 * shift left the succedding entries in the stbl
4322 si
= p
->header
.nextindex
;
4324 memmove(&stbl
[fi
], &stbl
[fi
+ 1], si
- fi
- 1);
4326 p
->header
.nextindex
--;
4333 * function: truncate a (leaf/internal) entry
4335 * log freelist header, stbl, and each segment slot of entry
4336 * (even though last/only segment next field is modified,
4337 * physical image logging requires all segment slots of
4338 * the entry logged to avoid applying previous updates
4339 * to the same slots)
4341 static void dtTruncateEntry(dtpage_t
* p
, int ti
, struct dt_lock
** dtlock
)
4343 int tsi
; /* truncate entry slot index */
4347 struct dt_lock
*dtlck
= *dtlock
;
4351 /* get free entry slot index */
4352 stbl
= DT_GETSTBL(p
);
4355 /* open new linelock */
4356 if (dtlck
->index
>= dtlck
->maxcnt
)
4357 dtlck
= (struct dt_lock
*) txLinelock(dtlck
);
4358 lv
= & dtlck
->lv
[dtlck
->index
];
4362 /* get the head/only segment */
4364 ASSERT(p
->header
.flag
& BT_INTERNAL
);
4365 ((struct idtentry
*) t
)->namlen
= 0;
4366 si
= ((struct idtentry
*) t
)->next
;
4367 ((struct idtentry
*) t
)->next
= -1;
4374 /* find the last/only segment */
4376 /* is next slot contiguous ? */
4377 if (si
!= xsi
+ 1) {
4378 /* close current linelock */
4382 /* open new linelock */
4383 if (dtlck
->index
< dtlck
->maxcnt
)
4386 dtlck
= (struct dt_lock
*) txLinelock(dtlck
);
4387 lv
= & dtlck
->lv
[0];
4403 /* close current linelock */
4409 /* update freelist */
4412 t
->next
= p
->header
.freelist
;
4413 p
->header
.freelist
= fsi
;
4414 p
->header
.freecnt
+= freecnt
;
4419 * dtLinelockFreelist()
4421 static void dtLinelockFreelist(dtpage_t
* p
, /* directory page */
4422 int m
, /* max slot index */
4423 struct dt_lock
** dtlock
)
4425 int fsi
; /* free entry slot index */
4428 struct dt_lock
*dtlck
= *dtlock
;
4432 /* get free entry slot index */
4433 fsi
= p
->header
.freelist
;
4435 /* open new linelock */
4436 if (dtlck
->index
>= dtlck
->maxcnt
)
4437 dtlck
= (struct dt_lock
*) txLinelock(dtlck
);
4438 lv
= & dtlck
->lv
[dtlck
->index
];
4448 /* find the last/only segment */
4449 while (si
< m
&& si
>= 0) {
4450 /* is next slot contiguous ? */
4451 if (si
!= xsi
+ 1) {
4452 /* close current linelock */
4456 /* open new linelock */
4457 if (dtlck
->index
< dtlck
->maxcnt
)
4460 dtlck
= (struct dt_lock
*) txLinelock(dtlck
);
4461 lv
= & dtlck
->lv
[0];
4475 /* close current linelock */
4486 * FUNCTION: Modify the inode number part of a directory entry
4489 * tid - Transaction id
4490 * ip - Inode of parent directory
4491 * key - Name of entry to be modified
4492 * orig_ino - Original inode number expected in entry
4493 * new_ino - New inode number to put into entry
4497 * -ESTALE - If entry found does not match orig_ino passed in
4498 * -ENOENT - If no entry can be found to match key
4499 * 0 - If successfully modified entry
4501 int dtModify(tid_t tid
, struct inode
*ip
,
4502 struct component_name
* key
, ino_t
* orig_ino
, ino_t new_ino
, int flag
)
4506 struct metapage
*mp
;
4509 struct btstack btstack
;
4511 struct dt_lock
*dtlck
;
4514 int entry_si
; /* entry slot index */
4515 struct ldtentry
*entry
;
4518 * search for the entry to modify:
4520 * dtSearch() returns (leaf page pinned, index at which to modify).
4522 if ((rc
= dtSearch(ip
, key
, orig_ino
, &btstack
, flag
)))
4525 /* retrieve search result */
4526 DT_GETSEARCH(ip
, btstack
.top
, bn
, mp
, p
, index
);
4528 BT_MARK_DIRTY(mp
, ip
);
4530 * acquire a transaction lock on the leaf page of named entry
4532 tlck
= txLock(tid
, ip
, mp
, tlckDTREE
| tlckENTRY
);
4533 dtlck
= (struct dt_lock
*) & tlck
->lock
;
4535 /* get slot index of the entry */
4536 stbl
= DT_GETSTBL(p
);
4537 entry_si
= stbl
[index
];
4539 /* linelock entry */
4540 ASSERT(dtlck
->index
== 0);
4541 lv
= & dtlck
->lv
[0];
4542 lv
->offset
= entry_si
;
4546 /* get the head/only segment */
4547 entry
= (struct ldtentry
*) & p
->slot
[entry_si
];
4549 /* substitute the inode number of the entry */
4550 entry
->inumber
= cpu_to_le32(new_ino
);
4552 /* unpin the leaf page */
4558 #ifdef _JFS_DEBUG_DTREE
4562 * function: traverse forward
4564 int dtDisplayTree(struct inode
*ip
)
4567 struct metapage
*mp
;
4570 int index
, lastindex
, v
, h
;
4572 struct btstack btstack
;
4573 struct btframe
*btsp
;
4574 struct btframe
*parent
;
4578 printk("display B+-tree.\n");
4581 btsp
= btstack
.stack
;
4586 * root resides in the inode
4592 * first access of each page:
4595 DT_GETPAGE(ip
, bn
, mp
, psize
, p
, rc
);
4599 /* process entries forward from first index */
4601 lastindex
= p
->header
.nextindex
- 1;
4603 if (p
->header
.flag
& BT_INTERNAL
) {
4605 * first access of each internal page
4607 printf("internal page ");
4608 dtDisplayPage(ip
, bn
, p
);
4611 } else { /* (p->header.flag & BT_LEAF) */
4614 * first access of each leaf page
4616 printf("leaf page ");
4617 dtDisplayPage(ip
, bn
, p
);
4620 * process leaf page entries
4622 for ( ; index <= lastindex; index++)
4627 /* unpin the leaf page */
4632 * go back up to the parent page
4635 /* pop/restore parent entry for the current child page */
4636 if ((parent
= (btsp
== btstack
.stack
? NULL
: --btsp
)) == NULL
)
4637 /* current page must have been root */
4641 * parent page scan completed
4643 if ((index
= parent
->index
) == (lastindex
= parent
->lastindex
)) {
4644 /* go back up to the parent page */
4649 * parent page has entries remaining
4651 /* get back the parent page */
4653 /* v = parent->level; */
4654 DT_GETPAGE(ip
, bn
, mp
, PSIZE
, p
, rc
);
4658 /* get next parent entry */
4662 * internal page: go down to child page of current entry
4665 /* push/save current parent entry for the child page */
4666 btsp
->bn
= pbn
= bn
;
4667 btsp
->index
= index
;
4668 btsp
->lastindex
= lastindex
;
4669 /* btsp->level = v; */
4670 /* btsp->node = h; */
4673 /* get current entry for the child page */
4674 stbl
= DT_GETSTBL(p
);
4675 xd
= (pxd_t
*) & p
->slot
[stbl
[index
]];
4678 * first access of each internal entry:
4681 /* get child page */
4682 bn
= addressPXD(xd
);
4683 psize
= lengthPXD(xd
) << ip
->i_ipmnt
->i_l2bsize
;
4685 printk("traverse down 0x%Lx[%d]->0x%Lx\n", pbn
, index
, bn
);
4689 /* release parent page */
4692 /* process the child page */
4700 * function: display page
4702 int dtDisplayPage(struct inode
*ip
, s64 bn
, dtpage_t
* p
)
4705 struct metapage
*mp
;
4706 struct ldtentry
*lh
;
4707 struct idtentry
*ih
;
4711 wchar_t name
[JFS_NAME_MAX
+ 1];
4712 struct component_name key
= { 0, name
};
4717 DT_GETPAGE(ip
, bn
, mp
, PSIZE
, p
, rc
);
4722 /* display page control */
4723 printk("bn:0x%Lx flag:0x%08x nextindex:%d\n",
4724 bn
, p
->header
.flag
, p
->header
.nextindex
);
4726 /* display entries */
4727 stbl
= DT_GETSTBL(p
);
4728 for (i
= 0, j
= 1; i
< p
->header
.nextindex
; i
++, j
++) {
4729 dtGetKey(p
, i
, &key
, JFS_SBI(ip
->i_sb
)->mntflag
);
4730 key
.name
[key
.namlen
] = '\0';
4731 if (p
->header
.flag
& BT_LEAF
) {
4732 lh
= (struct ldtentry
*) & p
->slot
[stbl
[i
]];
4733 printf("\t[%d] %s:%d", i
, key
.name
,
4734 le32_to_cpu(lh
->inumber
));
4736 ih
= (struct idtentry
*) & p
->slot
[stbl
[i
]];
4738 bn
= addressPXD(xd
);
4739 printf("\t[%d] %s:0x%Lx", i
, key
.name
, bn
);
4755 #endif /* _JFS_DEBUG_DTREE */