1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) International Business Machines Corp., 2000-2004
7 * jfs_dtree.c: directory B+-tree manager
9 * B+-tree with variable length key directory:
11 * each directory page is structured as an array of 32-byte
12 * directory entry slots initialized as a freelist
13 * to avoid search/compaction of free space at insertion.
14 * when an entry is inserted, a number of slots are allocated
15 * from the freelist as required to store variable length data
16 * of the entry; when the entry is deleted, slots of the entry
17 * are returned to freelist.
19 * leaf entry stores full name as key and file serial number
20 * (aka inode number) as data.
21 * internal/router entry stores sufffix compressed name
22 * as key and simple extent descriptor as data.
24 * each directory page maintains a sorted entry index table
25 * which stores the start slot index of sorted entries
26 * to allow binary search on the table.
28 * directory starts as a root/leaf page in on-disk inode
30 * when it becomes full, it starts a leaf of a external extent
31 * of length of 1 block. each time the first leaf becomes full,
32 * it is extended rather than split (its size is doubled),
33 * until its length becoms 4 KBytes, from then the extent is split
34 * with new 4 Kbyte extent when it becomes full
35 * to reduce external fragmentation of small directories.
37 * blah, blah, blah, for linear scan of directory in pieces by
41 * case-insensitive directory file system
43 * names are stored in case-sensitive way in leaf entry.
44 * but stored, searched and compared in case-insensitive (uppercase) order
45 * (i.e., both search key and entry key are folded for search/compare):
46 * (note that case-sensitive order is BROKEN in storage, e.g.,
47 * sensitive: Ad, aB, aC, aD -> insensitive: aB, aC, aD, Ad
49 * entries which folds to the same key makes up a equivalent class
50 * whose members are stored as contiguous cluster (may cross page boundary)
51 * but whose order is arbitrary and acts as duplicate, e.g.,
54 * once match is found at leaf, requires scan forward/backward
55 * either for, in case-insensitive search, duplicate
56 * or for, in case-sensitive search, for exact match
58 * router entry must be created/stored in case-insensitive way
60 * (right most key of left page and left most key of right page
61 * are folded, and its suffix compression is propagated as router
63 * (e.g., if split occurs <abc> and <aBd>, <ABD> trather than <aB>
64 * should be made the router key for the split)
66 * case-insensitive search:
70 * case-insensitive search of B-tree:
71 * for internal entry, router key is already folded;
72 * for leaf entry, fold the entry key before comparison.
74 * if (leaf entry case-insensitive match found)
75 * if (next entry satisfies case-insensitive match)
77 * if (prev entry satisfies case-insensitive match)
84 * target directory inode lock is being held on entry/exit
85 * of all main directory service routines.
91 #include <linux/quotaops.h>
92 #include <linux/slab.h>
93 #include "jfs_incore.h"
94 #include "jfs_superblock.h"
95 #include "jfs_filsys.h"
96 #include "jfs_metapage.h"
98 #include "jfs_unicode.h"
99 #include "jfs_debug.h"
101 /* dtree split parameter */
106 struct component_name
*key
;
108 struct pxdlist
*pxdlist
;
111 #define DT_PAGE(IP, MP) BT_PAGE(IP, MP, dtpage_t, i_dtroot)
113 /* get page buffer for specified block address */
114 #define DT_GETPAGE(IP, BN, MP, SIZE, P, RC) \
116 BT_GETPAGE(IP, BN, MP, dtpage_t, SIZE, P, RC, i_dtroot); \
118 if (((P)->header.nextindex > \
119 (((BN) == 0) ? DTROOTMAXSLOT : (P)->header.maxslot)) || \
120 ((BN) && ((P)->header.maxslot > DTPAGEMAXSLOT))) { \
122 jfs_error((IP)->i_sb, \
123 "DT_GETPAGE: dtree page corrupt\n"); \
130 /* for consistency */
131 #define DT_PUTPAGE(MP) BT_PUTPAGE(MP)
133 #define DT_GETSEARCH(IP, LEAF, BN, MP, P, INDEX) \
134 BT_GETSEARCH(IP, LEAF, BN, MP, dtpage_t, P, INDEX, i_dtroot)
139 static int dtSplitUp(tid_t tid
, struct inode
*ip
,
140 struct dtsplit
* split
, struct btstack
* btstack
);
142 static int dtSplitPage(tid_t tid
, struct inode
*ip
, struct dtsplit
* split
,
143 struct metapage
** rmpp
, dtpage_t
** rpp
, pxd_t
* rxdp
);
145 static int dtExtendPage(tid_t tid
, struct inode
*ip
,
146 struct dtsplit
* split
, struct btstack
* btstack
);
148 static int dtSplitRoot(tid_t tid
, struct inode
*ip
,
149 struct dtsplit
* split
, struct metapage
** rmpp
);
151 static int dtDeleteUp(tid_t tid
, struct inode
*ip
, struct metapage
* fmp
,
152 dtpage_t
* fp
, struct btstack
* btstack
);
154 static int dtRelink(tid_t tid
, struct inode
*ip
, dtpage_t
* p
);
156 static int dtReadFirst(struct inode
*ip
, struct btstack
* btstack
);
158 static int dtReadNext(struct inode
*ip
,
159 loff_t
* offset
, struct btstack
* btstack
);
161 static int dtCompare(struct component_name
* key
, dtpage_t
* p
, int si
);
163 static int ciCompare(struct component_name
* key
, dtpage_t
* p
, int si
,
166 static void dtGetKey(dtpage_t
* p
, int i
, struct component_name
* key
,
169 static int ciGetLeafPrefixKey(dtpage_t
* lp
, int li
, dtpage_t
* rp
,
170 int ri
, struct component_name
* key
, int flag
);
172 static void dtInsertEntry(dtpage_t
* p
, int index
, struct component_name
* key
,
173 ddata_t
* data
, struct dt_lock
**);
175 static void dtMoveEntry(dtpage_t
* sp
, int si
, dtpage_t
* dp
,
176 struct dt_lock
** sdtlock
, struct dt_lock
** ddtlock
,
179 static void dtDeleteEntry(dtpage_t
* p
, int fi
, struct dt_lock
** dtlock
);
181 static void dtTruncateEntry(dtpage_t
* p
, int ti
, struct dt_lock
** dtlock
);
183 static void dtLinelockFreelist(dtpage_t
* p
, int m
, struct dt_lock
** dtlock
);
185 #define ciToUpper(c) UniStrupr((c)->name)
190 * Reads a page of a directory's index table.
191 * Having metadata mapped into the directory inode's address space
192 * presents a multitude of problems. We avoid this by mapping to
193 * the absolute address space outside of the *_metapage routines
195 static struct metapage
*read_index_page(struct inode
*inode
, s64 blkno
)
202 rc
= xtLookup(inode
, blkno
, 1, &xflag
, &xaddr
, &xlen
, 1);
203 if (rc
|| (xaddr
== 0))
206 return read_metapage(inode
, xaddr
, PSIZE
, 1);
212 * Same as get_index_page(), but get's a new page without reading
214 static struct metapage
*get_index_page(struct inode
*inode
, s64 blkno
)
221 rc
= xtLookup(inode
, blkno
, 1, &xflag
, &xaddr
, &xlen
, 1);
222 if (rc
|| (xaddr
== 0))
225 return get_metapage(inode
, xaddr
, PSIZE
, 1);
231 * Returns dtree page containing directory table entry for specified
232 * index and pointer to its entry.
234 * mp must be released by caller.
236 static struct dir_table_slot
*find_index(struct inode
*ip
, u32 index
,
237 struct metapage
** mp
, s64
*lblock
)
239 struct jfs_inode_info
*jfs_ip
= JFS_IP(ip
);
243 struct dir_table_slot
*slot
;
244 static int maxWarnings
= 10;
248 jfs_warn("find_entry called with index = %d", index
);
254 if (index
>= jfs_ip
->next_index
) {
255 jfs_warn("find_entry called with index >= next_index");
259 if (jfs_dirtable_inline(ip
)) {
261 * Inline directory table
264 slot
= &jfs_ip
->i_dirtable
[index
- 2];
266 offset
= (index
- 2) * sizeof(struct dir_table_slot
);
267 page_offset
= offset
& (PSIZE
- 1);
268 blkno
= ((offset
+ 1) >> L2PSIZE
) <<
269 JFS_SBI(ip
->i_sb
)->l2nbperpage
;
271 if (*mp
&& (*lblock
!= blkno
)) {
272 release_metapage(*mp
);
277 *mp
= read_index_page(ip
, blkno
);
280 jfs_err("free_index: error reading directory table");
285 (struct dir_table_slot
*) ((char *) (*mp
)->data
+
291 static inline void lock_index(tid_t tid
, struct inode
*ip
, struct metapage
* mp
,
295 struct linelock
*llck
;
298 tlck
= txLock(tid
, ip
, mp
, tlckDATA
);
299 llck
= (struct linelock
*) tlck
->lock
;
301 if (llck
->index
>= llck
->maxcnt
)
302 llck
= txLinelock(llck
);
303 lv
= &llck
->lv
[llck
->index
];
306 * Linelock slot size is twice the size of directory table
307 * slot size. 512 entries per page.
309 lv
->offset
= ((index
- 2) & 511) >> 1;
317 * Adds an entry to the directory index table. This is used to provide
318 * each directory entry with a persistent index in which to resume
319 * directory traversals
321 static u32
add_index(tid_t tid
, struct inode
*ip
, s64 bn
, int slot
)
323 struct super_block
*sb
= ip
->i_sb
;
324 struct jfs_sb_info
*sbi
= JFS_SBI(sb
);
325 struct jfs_inode_info
*jfs_ip
= JFS_IP(ip
);
327 struct dir_table_slot
*dirtab_slot
;
329 struct linelock
*llck
;
337 ASSERT(DO_INDEX(ip
));
339 if (jfs_ip
->next_index
< 2) {
340 jfs_warn("add_index: next_index = %d. Resetting!",
342 jfs_ip
->next_index
= 2;
345 index
= jfs_ip
->next_index
++;
347 if (index
<= MAX_INLINE_DIRTABLE_ENTRY
) {
349 * i_size reflects size of index table, or 8 bytes per entry.
351 ip
->i_size
= (loff_t
) (index
- 1) << 3;
354 * dir table fits inline within inode
356 dirtab_slot
= &jfs_ip
->i_dirtable
[index
-2];
357 dirtab_slot
->flag
= DIR_INDEX_VALID
;
358 dirtab_slot
->slot
= slot
;
359 DTSaddress(dirtab_slot
, bn
);
361 set_cflag(COMMIT_Dirtable
, ip
);
365 if (index
== (MAX_INLINE_DIRTABLE_ENTRY
+ 1)) {
366 struct dir_table_slot temp_table
[12];
369 * It's time to move the inline table to an external
370 * page and begin to build the xtree
372 if (dquot_alloc_block(ip
, sbi
->nbperpage
))
374 if (dbAlloc(ip
, 0, sbi
->nbperpage
, &xaddr
)) {
375 dquot_free_block(ip
, sbi
->nbperpage
);
380 * Save the table, we're going to overwrite it with the
383 memcpy(temp_table
, &jfs_ip
->i_dirtable
, sizeof(temp_table
));
386 * Initialize empty x-tree
391 * Add the first block to the xtree
393 if (xtInsert(tid
, ip
, 0, 0, sbi
->nbperpage
, &xaddr
, 0)) {
394 /* This really shouldn't fail */
395 jfs_warn("add_index: xtInsert failed!");
396 memcpy(&jfs_ip
->i_dirtable
, temp_table
,
397 sizeof (temp_table
));
398 dbFree(ip
, xaddr
, sbi
->nbperpage
);
399 dquot_free_block(ip
, sbi
->nbperpage
);
404 mp
= get_index_page(ip
, 0);
406 jfs_err("add_index: get_metapage failed!");
407 xtTruncate(tid
, ip
, 0, COMMIT_PWMAP
);
408 memcpy(&jfs_ip
->i_dirtable
, temp_table
,
409 sizeof (temp_table
));
412 tlck
= txLock(tid
, ip
, mp
, tlckDATA
);
413 llck
= (struct linelock
*) & tlck
->lock
;
414 ASSERT(llck
->index
== 0);
418 lv
->length
= 6; /* tlckDATA slot size is 16 bytes */
421 memcpy(mp
->data
, temp_table
, sizeof(temp_table
));
423 mark_metapage_dirty(mp
);
424 release_metapage(mp
);
427 * Logging is now directed by xtree tlocks
429 clear_cflag(COMMIT_Dirtable
, ip
);
432 offset
= (index
- 2) * sizeof(struct dir_table_slot
);
433 page_offset
= offset
& (PSIZE
- 1);
434 blkno
= ((offset
+ 1) >> L2PSIZE
) << sbi
->l2nbperpage
;
435 if (page_offset
== 0) {
437 * This will be the beginning of a new page
440 if (xtInsert(tid
, ip
, 0, blkno
, sbi
->nbperpage
, &xaddr
, 0)) {
441 jfs_warn("add_index: xtInsert failed!");
446 if ((mp
= get_index_page(ip
, blkno
)))
447 memset(mp
->data
, 0, PSIZE
); /* Just looks better */
449 xtTruncate(tid
, ip
, offset
, COMMIT_PWMAP
);
451 mp
= read_index_page(ip
, blkno
);
454 jfs_err("add_index: get/read_metapage failed!");
458 lock_index(tid
, ip
, mp
, index
);
461 (struct dir_table_slot
*) ((char *) mp
->data
+ page_offset
);
462 dirtab_slot
->flag
= DIR_INDEX_VALID
;
463 dirtab_slot
->slot
= slot
;
464 DTSaddress(dirtab_slot
, bn
);
466 mark_metapage_dirty(mp
);
467 release_metapage(mp
);
473 jfs_ip
->next_index
--;
481 * Marks an entry to the directory index table as free.
483 static void free_index(tid_t tid
, struct inode
*ip
, u32 index
, u32 next
)
485 struct dir_table_slot
*dirtab_slot
;
487 struct metapage
*mp
= NULL
;
489 dirtab_slot
= find_index(ip
, index
, &mp
, &lblock
);
494 dirtab_slot
->flag
= DIR_INDEX_FREE
;
495 dirtab_slot
->slot
= dirtab_slot
->addr1
= 0;
496 dirtab_slot
->addr2
= cpu_to_le32(next
);
499 lock_index(tid
, ip
, mp
, index
);
500 mark_metapage_dirty(mp
);
501 release_metapage(mp
);
503 set_cflag(COMMIT_Dirtable
, ip
);
509 * Changes an entry in the directory index table
511 static void modify_index(tid_t tid
, struct inode
*ip
, u32 index
, s64 bn
,
512 int slot
, struct metapage
** mp
, s64
*lblock
)
514 struct dir_table_slot
*dirtab_slot
;
516 dirtab_slot
= find_index(ip
, index
, mp
, lblock
);
521 DTSaddress(dirtab_slot
, bn
);
522 dirtab_slot
->slot
= slot
;
525 lock_index(tid
, ip
, *mp
, index
);
526 mark_metapage_dirty(*mp
);
528 set_cflag(COMMIT_Dirtable
, ip
);
534 * reads a directory table slot
536 static int read_index(struct inode
*ip
, u32 index
,
537 struct dir_table_slot
* dirtab_slot
)
540 struct metapage
*mp
= NULL
;
541 struct dir_table_slot
*slot
;
543 slot
= find_index(ip
, index
, &mp
, &lblock
);
548 memcpy(dirtab_slot
, slot
, sizeof(struct dir_table_slot
));
551 release_metapage(mp
);
560 * Search for the entry with specified key
564 * return: 0 - search result on stack, leaf page pinned;
567 int dtSearch(struct inode
*ip
, struct component_name
* key
, ino_t
* data
,
568 struct btstack
* btstack
, int flag
)
571 int cmp
= 1; /* init for empty page */
576 int base
, index
, lim
;
577 struct btframe
*btsp
;
579 int psize
= 288; /* initial in-line directory */
581 struct component_name ciKey
;
582 struct super_block
*sb
= ip
->i_sb
;
584 ciKey
.name
= kmalloc_array(JFS_NAME_MAX
+ 1, sizeof(wchar_t),
592 /* uppercase search key for c-i directory */
593 UniStrcpy(ciKey
.name
, key
->name
);
594 ciKey
.namlen
= key
->namlen
;
596 /* only uppercase if case-insensitive support is on */
597 if ((JFS_SBI(sb
)->mntflag
& JFS_OS2
) == JFS_OS2
) {
600 BT_CLR(btstack
); /* reset stack */
602 /* init level count for max pages to split */
606 * search down tree from root:
608 * between two consecutive entries of <Ki, Pi> and <Kj, Pj> of
609 * internal page, child page Pi contains entry with k, Ki <= K < Kj.
611 * if entry with search key K is not found
612 * internal page search find the entry with largest key Ki
613 * less than K which point to the child page to search;
614 * leaf page search find the entry with smallest key Kj
615 * greater than K so that the returned index is the position of
616 * the entry to be shifted right for insertion of new entry.
617 * for empty tree, search key is greater than any key of the tree.
619 * by convention, root bn = 0.
622 /* get/pin the page to search */
623 DT_GETPAGE(ip
, bn
, mp
, psize
, p
, rc
);
627 /* get sorted entry table of the page */
628 stbl
= DT_GETSTBL(p
);
631 * binary search with search key K on the current page.
633 for (base
= 0, lim
= p
->header
.nextindex
; lim
; lim
>>= 1) {
634 index
= base
+ (lim
>> 1);
636 if (p
->header
.flag
& BT_LEAF
) {
637 /* uppercase leaf name to compare */
639 ciCompare(&ciKey
, p
, stbl
[index
],
640 JFS_SBI(sb
)->mntflag
);
642 /* router key is in uppercase */
644 cmp
= dtCompare(&ciKey
, p
, stbl
[index
]);
652 /* search hit - leaf page:
653 * return the entry found
655 if (p
->header
.flag
& BT_LEAF
) {
656 inumber
= le32_to_cpu(
657 ((struct ldtentry
*) & p
->slot
[stbl
[index
]])->inumber
);
660 * search for JFS_LOOKUP
662 if (flag
== JFS_LOOKUP
) {
669 * search for JFS_CREATE
671 if (flag
== JFS_CREATE
) {
678 * search for JFS_REMOVE or JFS_RENAME
680 if ((flag
== JFS_REMOVE
||
681 flag
== JFS_RENAME
) &&
688 * JFS_REMOVE|JFS_FINDDIR|JFS_RENAME
690 /* save search result */
701 /* search hit - internal page:
702 * descend/search its child page
716 * base is the smallest index with key (Kj) greater than
717 * search key (K) and may be zero or (maxindex + 1) index.
720 * search miss - leaf page
722 * return location of entry (base) where new entry with
723 * search key K is to be inserted.
725 if (p
->header
.flag
& BT_LEAF
) {
727 * search for JFS_LOOKUP, JFS_REMOVE, or JFS_RENAME
729 if (flag
== JFS_LOOKUP
|| flag
== JFS_REMOVE
||
730 flag
== JFS_RENAME
) {
736 * search for JFS_CREATE|JFS_FINDDIR:
751 * search miss - internal page
753 * if base is non-zero, decrement base by one to get the parent
754 * entry of the child page to search.
756 index
= base
? base
- 1 : base
;
759 * go down to child page
762 /* update max. number of pages to split */
763 if (BT_STACK_FULL(btstack
)) {
764 /* Something's corrupted, mark filesystem dirty so
765 * chkdsk will fix it.
767 jfs_error(sb
, "stack overrun!\n");
768 BT_STACK_DUMP(btstack
);
774 /* push (bn, index) of the parent page/entry */
775 BT_PUSH(btstack
, bn
, index
);
777 /* get the child page block number */
778 pxd
= (pxd_t
*) & p
->slot
[stbl
[index
]];
779 bn
= addressPXD(pxd
);
780 psize
= lengthPXD(pxd
) << JFS_SBI(ip
->i_sb
)->l2bsize
;
782 /* unpin the parent page */
802 * function: insert an entry to directory tree
806 * return: 0 - success;
809 int dtInsert(tid_t tid
, struct inode
*ip
,
810 struct component_name
* name
, ino_t
* fsn
, struct btstack
* btstack
)
813 struct metapage
*mp
; /* meta-page buffer */
814 dtpage_t
*p
; /* base B+-tree index page */
817 struct dtsplit split
; /* split information */
819 struct dt_lock
*dtlck
;
825 * retrieve search result
827 * dtSearch() returns (leaf page pinned, index at which to insert).
828 * n.b. dtSearch() may return index of (maxindex + 1) of
831 DT_GETSEARCH(ip
, btstack
->top
, bn
, mp
, p
, index
);
834 * insert entry for new key
837 if (JFS_IP(ip
)->next_index
== DIREND
) {
841 n
= NDTLEAF(name
->namlen
);
845 n
= NDTLEAF_LEGACY(name
->namlen
);
846 data
.leaf
.ip
= NULL
; /* signifies legacy directory format */
848 data
.leaf
.ino
= *fsn
;
851 * leaf page does not have enough room for new entry:
853 * extend/split the leaf page;
855 * dtSplitUp() will insert the entry and unpin the leaf page.
857 if (n
> p
->header
.freecnt
) {
863 rc
= dtSplitUp(tid
, ip
, &split
, btstack
);
868 * leaf page does have enough room for new entry:
870 * insert the new data entry into the leaf page;
872 BT_MARK_DIRTY(mp
, ip
);
874 * acquire a transaction lock on the leaf page
876 tlck
= txLock(tid
, ip
, mp
, tlckDTREE
| tlckENTRY
);
877 dtlck
= (struct dt_lock
*) & tlck
->lock
;
878 ASSERT(dtlck
->index
== 0);
881 /* linelock header */
886 dtInsertEntry(p
, index
, name
, &data
, &dtlck
);
888 /* linelock stbl of non-root leaf page */
889 if (!(p
->header
.flag
& BT_ROOT
)) {
890 if (dtlck
->index
>= dtlck
->maxcnt
)
891 dtlck
= (struct dt_lock
*) txLinelock(dtlck
);
892 lv
= & dtlck
->lv
[dtlck
->index
];
893 n
= index
>> L2DTSLOTSIZE
;
894 lv
->offset
= p
->header
.stblindex
+ n
;
896 ((p
->header
.nextindex
- 1) >> L2DTSLOTSIZE
) - n
+ 1;
900 /* unpin the leaf page */
910 * function: propagate insertion bottom up;
914 * return: 0 - success;
916 * leaf page unpinned;
918 static int dtSplitUp(tid_t tid
,
919 struct inode
*ip
, struct dtsplit
* split
, struct btstack
* btstack
)
921 struct jfs_sb_info
*sbi
= JFS_SBI(ip
->i_sb
);
923 struct metapage
*smp
;
924 dtpage_t
*sp
; /* split page */
925 struct metapage
*rmp
;
926 dtpage_t
*rp
; /* new right page split from sp */
927 pxd_t rpxd
; /* new right page extent descriptor */
928 struct metapage
*lmp
;
929 dtpage_t
*lp
; /* left child page */
930 int skip
; /* index of entry of insertion */
931 struct btframe
*parent
; /* parent page entry on traverse stack */
934 struct pxdlist pxdlist
;
936 struct component_name key
= { 0, NULL
};
937 ddata_t
*data
= split
->data
;
939 struct dt_lock
*dtlck
;
942 int quota_allocation
= 0;
946 sp
= DT_PAGE(ip
, smp
);
948 key
.name
= kmalloc_array(JFS_NAME_MAX
+ 2, sizeof(wchar_t), GFP_NOFS
);
958 * The split routines insert the new entry, and
959 * acquire txLock as appropriate.
962 * split root leaf page:
964 if (sp
->header
.flag
& BT_ROOT
) {
966 * allocate a single extent child page
969 n
= sbi
->bsize
>> L2DTSLOTSIZE
;
970 n
-= (n
+ 31) >> L2DTSLOTSIZE
; /* stbl size */
971 n
-= DTROOTMAXSLOT
- sp
->header
.freecnt
; /* header + entries */
972 if (n
<= split
->nslot
)
974 if ((rc
= dbAlloc(ip
, 0, (s64
) xlen
, &xaddr
))) {
981 pxd
= &pxdlist
.pxd
[0];
982 PXDaddress(pxd
, xaddr
);
983 PXDlength(pxd
, xlen
);
984 split
->pxdlist
= &pxdlist
;
985 rc
= dtSplitRoot(tid
, ip
, split
, &rmp
);
988 dbFree(ip
, xaddr
, xlen
);
995 ip
->i_size
= xlen
<< sbi
->l2bsize
;
1001 * extend first leaf page
1003 * extend the 1st extent if less than buffer page size
1004 * (dtExtendPage() reurns leaf page unpinned)
1006 pxd
= &sp
->header
.self
;
1007 xlen
= lengthPXD(pxd
);
1008 xsize
= xlen
<< sbi
->l2bsize
;
1009 if (xsize
< PSIZE
) {
1010 xaddr
= addressPXD(pxd
);
1011 n
= xsize
>> L2DTSLOTSIZE
;
1012 n
-= (n
+ 31) >> L2DTSLOTSIZE
; /* stbl size */
1013 if ((n
+ sp
->header
.freecnt
) <= split
->nslot
)
1014 n
= xlen
+ (xlen
<< 1);
1018 /* Allocate blocks to quota. */
1019 rc
= dquot_alloc_block(ip
, n
);
1022 quota_allocation
+= n
;
1024 if ((rc
= dbReAlloc(sbi
->ipbmap
, xaddr
, (s64
) xlen
,
1028 pxdlist
.maxnpxd
= 1;
1030 pxd
= &pxdlist
.pxd
[0];
1031 PXDaddress(pxd
, nxaddr
);
1032 PXDlength(pxd
, xlen
+ n
);
1033 split
->pxdlist
= &pxdlist
;
1034 if ((rc
= dtExtendPage(tid
, ip
, split
, btstack
))) {
1035 nxaddr
= addressPXD(pxd
);
1036 if (xaddr
!= nxaddr
) {
1037 /* free relocated extent */
1038 xlen
= lengthPXD(pxd
);
1039 dbFree(ip
, nxaddr
, (s64
) xlen
);
1041 /* free extended delta */
1042 xlen
= lengthPXD(pxd
) - n
;
1043 xaddr
= addressPXD(pxd
) + xlen
;
1044 dbFree(ip
, xaddr
, (s64
) n
);
1046 } else if (!DO_INDEX(ip
))
1047 ip
->i_size
= lengthPXD(pxd
) << sbi
->l2bsize
;
1056 * split leaf page <sp> into <sp> and a new right page <rp>.
1058 * return <rp> pinned and its extent descriptor <rpxd>
1061 * allocate new directory page extent and
1062 * new index page(s) to cover page split(s)
1064 * allocation hint: ?
1066 n
= btstack
->nsplit
;
1067 pxdlist
.maxnpxd
= pxdlist
.npxd
= 0;
1068 xlen
= sbi
->nbperpage
;
1069 for (pxd
= pxdlist
.pxd
; n
> 0; n
--, pxd
++) {
1070 if ((rc
= dbAlloc(ip
, 0, (s64
) xlen
, &xaddr
)) == 0) {
1071 PXDaddress(pxd
, xaddr
);
1072 PXDlength(pxd
, xlen
);
1079 /* undo allocation */
1083 split
->pxdlist
= &pxdlist
;
1084 if ((rc
= dtSplitPage(tid
, ip
, split
, &rmp
, &rp
, &rpxd
))) {
1087 /* undo allocation */
1092 ip
->i_size
+= PSIZE
;
1095 * propagate up the router entry for the leaf page just split
1097 * insert a router entry for the new page into the parent page,
1098 * propagate the insert/split up the tree by walking back the stack
1099 * of (bn of parent page, index of child page entry in parent page)
1100 * that were traversed during the search for the page that split.
1102 * the propagation of insert/split up the tree stops if the root
1103 * splits or the page inserted into doesn't have to split to hold
1106 * the parent entry for the split page remains the same, and
1107 * a new entry is inserted at its right with the first key and
1108 * block number of the new right page.
1110 * There are a maximum of 4 pages pinned at any time:
1111 * two children, left parent and right parent (when the parent splits).
1112 * keep the child pages pinned while working on the parent.
1113 * make sure that all pins are released at exit.
1115 while ((parent
= BT_POP(btstack
)) != NULL
) {
1116 /* parent page specified by stack frame <parent> */
1118 /* keep current child pages (<lp>, <rp>) pinned */
1123 * insert router entry in parent for new right child page <rp>
1125 /* get the parent page <sp> */
1126 DT_GETPAGE(ip
, parent
->bn
, smp
, PSIZE
, sp
, rc
);
1134 * The new key entry goes ONE AFTER the index of parent entry,
1135 * because the split was to the right.
1137 skip
= parent
->index
+ 1;
1140 * compute the key for the router entry
1142 * key suffix compression:
1143 * for internal pages that have leaf pages as children,
1144 * retain only what's needed to distinguish between
1145 * the new entry and the entry on the page to its left.
1146 * If the keys compare equal, retain the entire key.
1148 * note that compression is performed only at computing
1149 * router key at the lowest internal level.
1150 * further compression of the key between pairs of higher
1151 * level internal pages loses too much information and
1152 * the search may fail.
1153 * (e.g., two adjacent leaf pages of {a, ..., x} {xx, ...,}
1154 * results in two adjacent parent entries (a)(xx).
1155 * if split occurs between these two entries, and
1156 * if compression is applied, the router key of parent entry
1157 * of right page (x) will divert search for x into right
1158 * subtree and miss x in the left subtree.)
1160 * the entire key must be retained for the next-to-leftmost
1161 * internal key at any level of the tree, or search may fail
1164 switch (rp
->header
.flag
& BT_TYPE
) {
1167 * compute the length of prefix for suffix compression
1168 * between last entry of left page and first entry
1171 if ((sp
->header
.flag
& BT_ROOT
&& skip
> 1) ||
1172 sp
->header
.prev
!= 0 || skip
> 1) {
1173 /* compute uppercase router prefix key */
1174 rc
= ciGetLeafPrefixKey(lp
,
1175 lp
->header
.nextindex
-1,
1185 /* next to leftmost entry of
1186 lowest internal level */
1188 /* compute uppercase router key */
1189 dtGetKey(rp
, 0, &key
, sbi
->mntflag
);
1190 key
.name
[key
.namlen
] = 0;
1192 if ((sbi
->mntflag
& JFS_OS2
) == JFS_OS2
)
1196 n
= NDTINTERNAL(key
.namlen
);
1200 dtGetKey(rp
, 0, &key
, sbi
->mntflag
);
1201 n
= NDTINTERNAL(key
.namlen
);
1205 jfs_err("dtSplitUp(): UFO!");
1209 /* unpin left child page */
1213 * compute the data for the router entry
1215 data
->xd
= rpxd
; /* child page xd */
1218 * parent page is full - split the parent page
1220 if (n
> sp
->header
.freecnt
) {
1221 /* init for parent page split */
1223 split
->index
= skip
; /* index at insert */
1226 /* split->data = data; */
1228 /* unpin right child page */
1231 /* The split routines insert the new entry,
1232 * acquire txLock as appropriate.
1233 * return <rp> pinned and its block number <rbn>.
1235 rc
= (sp
->header
.flag
& BT_ROOT
) ?
1236 dtSplitRoot(tid
, ip
, split
, &rmp
) :
1237 dtSplitPage(tid
, ip
, split
, &rmp
, &rp
, &rpxd
);
1243 /* smp and rmp are pinned */
1246 * parent page is not full - insert router entry in parent page
1249 BT_MARK_DIRTY(smp
, ip
);
1251 * acquire a transaction lock on the parent page
1253 tlck
= txLock(tid
, ip
, smp
, tlckDTREE
| tlckENTRY
);
1254 dtlck
= (struct dt_lock
*) & tlck
->lock
;
1255 ASSERT(dtlck
->index
== 0);
1256 lv
= & dtlck
->lv
[0];
1258 /* linelock header */
1263 /* linelock stbl of non-root parent page */
1264 if (!(sp
->header
.flag
& BT_ROOT
)) {
1266 n
= skip
>> L2DTSLOTSIZE
;
1267 lv
->offset
= sp
->header
.stblindex
+ n
;
1269 ((sp
->header
.nextindex
-
1270 1) >> L2DTSLOTSIZE
) - n
+ 1;
1274 dtInsertEntry(sp
, skip
, &key
, data
, &dtlck
);
1276 /* exit propagate up */
1281 /* unpin current split and its right page */
1286 * free remaining extents allocated for split
1290 pxd
= &pxdlist
.pxd
[n
];
1291 for (; n
< pxdlist
.maxnpxd
; n
++, pxd
++)
1292 dbFree(ip
, addressPXD(pxd
), (s64
) lengthPXD(pxd
));
1297 /* Rollback quota allocation */
1298 if (rc
&& quota_allocation
)
1299 dquot_free_block(ip
, quota_allocation
);
1310 * function: Split a non-root page of a btree.
1314 * return: 0 - success;
1316 * return split and new page pinned;
1318 static int dtSplitPage(tid_t tid
, struct inode
*ip
, struct dtsplit
* split
,
1319 struct metapage
** rmpp
, dtpage_t
** rpp
, pxd_t
* rpxdp
)
1322 struct metapage
*smp
;
1324 struct metapage
*rmp
;
1325 dtpage_t
*rp
; /* new right page allocated */
1326 s64 rbn
; /* new right page block number */
1327 struct metapage
*mp
;
1330 struct pxdlist
*pxdlist
;
1332 int skip
, nextindex
, half
, left
, nxt
, off
, si
;
1333 struct ldtentry
*ldtentry
;
1334 struct idtentry
*idtentry
;
1339 struct dt_lock
*sdtlck
, *rdtlck
;
1341 struct dt_lock
*dtlck
;
1342 struct lv
*slv
, *rlv
, *lv
;
1344 /* get split page */
1346 sp
= DT_PAGE(ip
, smp
);
1349 * allocate the new right page for the split
1351 pxdlist
= split
->pxdlist
;
1352 pxd
= &pxdlist
->pxd
[pxdlist
->npxd
];
1354 rbn
= addressPXD(pxd
);
1355 rmp
= get_metapage(ip
, rbn
, PSIZE
, 1);
1359 /* Allocate blocks to quota. */
1360 rc
= dquot_alloc_block(ip
, lengthPXD(pxd
));
1362 release_metapage(rmp
);
1366 jfs_info("dtSplitPage: ip:0x%p smp:0x%p rmp:0x%p", ip
, smp
, rmp
);
1368 BT_MARK_DIRTY(rmp
, ip
);
1370 * acquire a transaction lock on the new right page
1372 tlck
= txLock(tid
, ip
, rmp
, tlckDTREE
| tlckNEW
);
1373 rdtlck
= (struct dt_lock
*) & tlck
->lock
;
1375 rp
= (dtpage_t
*) rmp
->data
;
1377 rp
->header
.self
= *pxd
;
1379 BT_MARK_DIRTY(smp
, ip
);
1381 * acquire a transaction lock on the split page
1385 tlck
= txLock(tid
, ip
, smp
, tlckDTREE
| tlckENTRY
);
1386 sdtlck
= (struct dt_lock
*) & tlck
->lock
;
1388 /* linelock header of split page */
1389 ASSERT(sdtlck
->index
== 0);
1390 slv
= & sdtlck
->lv
[0];
1396 * initialize/update sibling pointers between sp and rp
1398 nextbn
= le64_to_cpu(sp
->header
.next
);
1399 rp
->header
.next
= cpu_to_le64(nextbn
);
1400 rp
->header
.prev
= cpu_to_le64(addressPXD(&sp
->header
.self
));
1401 sp
->header
.next
= cpu_to_le64(rbn
);
1404 * initialize new right page
1406 rp
->header
.flag
= sp
->header
.flag
;
1408 /* compute sorted entry table at start of extent data area */
1409 rp
->header
.nextindex
= 0;
1410 rp
->header
.stblindex
= 1;
1412 n
= PSIZE
>> L2DTSLOTSIZE
;
1413 rp
->header
.maxslot
= n
;
1414 stblsize
= (n
+ 31) >> L2DTSLOTSIZE
; /* in unit of slot */
1417 fsi
= rp
->header
.stblindex
+ stblsize
;
1418 rp
->header
.freelist
= fsi
;
1419 rp
->header
.freecnt
= rp
->header
.maxslot
- fsi
;
1422 * sequential append at tail: append without split
1424 * If splitting the last page on a level because of appending
1425 * a entry to it (skip is maxentry), it's likely that the access is
1426 * sequential. Adding an empty page on the side of the level is less
1427 * work and can push the fill factor much higher than normal.
1428 * If we're wrong it's no big deal, we'll just do the split the right
1430 * (It may look like it's equally easy to do a similar hack for
1431 * reverse sorted data, that is, split the tree left,
1432 * but it's not. Be my guest.)
1434 if (nextbn
== 0 && split
->index
== sp
->header
.nextindex
) {
1435 /* linelock header + stbl (first slot) of new page */
1436 rlv
= & rdtlck
->lv
[rdtlck
->index
];
1442 * initialize freelist of new right page
1445 for (fsi
++; fsi
< rp
->header
.maxslot
; f
++, fsi
++)
1449 /* insert entry at the first entry of the new right page */
1450 dtInsertEntry(rp
, 0, split
->key
, split
->data
, &rdtlck
);
1456 * non-sequential insert (at possibly middle page)
1460 * update prev pointer of previous right sibling page;
1463 DT_GETPAGE(ip
, nextbn
, mp
, PSIZE
, p
, rc
);
1465 discard_metapage(rmp
);
1469 BT_MARK_DIRTY(mp
, ip
);
1471 * acquire a transaction lock on the next page
1473 tlck
= txLock(tid
, ip
, mp
, tlckDTREE
| tlckRELINK
);
1474 jfs_info("dtSplitPage: tlck = 0x%p, ip = 0x%p, mp=0x%p",
1476 dtlck
= (struct dt_lock
*) & tlck
->lock
;
1478 /* linelock header of previous right sibling page */
1479 lv
= & dtlck
->lv
[dtlck
->index
];
1484 p
->header
.prev
= cpu_to_le64(rbn
);
1490 * split the data between the split and right pages.
1492 skip
= split
->index
;
1493 half
= (PSIZE
>> L2DTSLOTSIZE
) >> 1; /* swag */
1497 * compute fill factor for split pages
1499 * <nxt> traces the next entry to move to rp
1500 * <off> traces the next entry to stay in sp
1502 stbl
= (u8
*) & sp
->slot
[sp
->header
.stblindex
];
1503 nextindex
= sp
->header
.nextindex
;
1504 for (nxt
= off
= 0; nxt
< nextindex
; ++off
) {
1506 /* check for fill factor with new entry size */
1510 switch (sp
->header
.flag
& BT_TYPE
) {
1512 ldtentry
= (struct ldtentry
*) & sp
->slot
[si
];
1514 n
= NDTLEAF(ldtentry
->namlen
);
1516 n
= NDTLEAF_LEGACY(ldtentry
->
1521 idtentry
= (struct idtentry
*) & sp
->slot
[si
];
1522 n
= NDTINTERNAL(idtentry
->namlen
);
1529 ++nxt
; /* advance to next entry to move in sp */
1537 /* <nxt> poins to the 1st entry to move */
1540 * move entries to right page
1542 * dtMoveEntry() initializes rp and reserves entry for insertion
1544 * split page moved out entries are linelocked;
1545 * new/right page moved in entries are linelocked;
1547 /* linelock header + stbl of new right page */
1548 rlv
= & rdtlck
->lv
[rdtlck
->index
];
1553 dtMoveEntry(sp
, nxt
, rp
, &sdtlck
, &rdtlck
, DO_INDEX(ip
));
1555 sp
->header
.nextindex
= nxt
;
1558 * finalize freelist of new right page
1560 fsi
= rp
->header
.freelist
;
1562 for (fsi
++; fsi
< rp
->header
.maxslot
; f
++, fsi
++)
1567 * Update directory index table for entries now in right page
1569 if ((rp
->header
.flag
& BT_LEAF
) && DO_INDEX(ip
)) {
1573 stbl
= DT_GETSTBL(rp
);
1574 for (n
= 0; n
< rp
->header
.nextindex
; n
++) {
1575 ldtentry
= (struct ldtentry
*) & rp
->slot
[stbl
[n
]];
1576 modify_index(tid
, ip
, le32_to_cpu(ldtentry
->index
),
1577 rbn
, n
, &mp
, &lblock
);
1580 release_metapage(mp
);
1584 * the skipped index was on the left page,
1587 /* insert the new entry in the split page */
1588 dtInsertEntry(sp
, skip
, split
->key
, split
->data
, &sdtlck
);
1590 /* linelock stbl of split page */
1591 if (sdtlck
->index
>= sdtlck
->maxcnt
)
1592 sdtlck
= (struct dt_lock
*) txLinelock(sdtlck
);
1593 slv
= & sdtlck
->lv
[sdtlck
->index
];
1594 n
= skip
>> L2DTSLOTSIZE
;
1595 slv
->offset
= sp
->header
.stblindex
+ n
;
1597 ((sp
->header
.nextindex
- 1) >> L2DTSLOTSIZE
) - n
+ 1;
1601 * the skipped index was on the right page,
1604 /* adjust the skip index to reflect the new position */
1607 /* insert the new entry in the right page */
1608 dtInsertEntry(rp
, skip
, split
->key
, split
->data
, &rdtlck
);
1622 * function: extend 1st/only directory leaf page
1626 * return: 0 - success;
1628 * return extended page pinned;
1630 static int dtExtendPage(tid_t tid
,
1631 struct inode
*ip
, struct dtsplit
* split
, struct btstack
* btstack
)
1633 struct super_block
*sb
= ip
->i_sb
;
1635 struct metapage
*smp
, *pmp
, *mp
;
1637 struct pxdlist
*pxdlist
;
1640 int newstblindex
, newstblsize
;
1641 int oldstblindex
, oldstblsize
;
1644 struct btframe
*parent
;
1646 struct dt_lock
*dtlck
;
1649 struct pxd_lock
*pxdlock
;
1652 struct ldtentry
*ldtentry
;
1655 /* get page to extend */
1657 sp
= DT_PAGE(ip
, smp
);
1659 /* get parent/root page */
1660 parent
= BT_POP(btstack
);
1661 DT_GETPAGE(ip
, parent
->bn
, pmp
, PSIZE
, pp
, rc
);
1668 pxdlist
= split
->pxdlist
;
1669 pxd
= &pxdlist
->pxd
[pxdlist
->npxd
];
1672 xaddr
= addressPXD(pxd
);
1673 tpxd
= &sp
->header
.self
;
1674 txaddr
= addressPXD(tpxd
);
1675 /* in-place extension */
1676 if (xaddr
== txaddr
) {
1683 /* save moved extent descriptor for later free */
1684 tlck
= txMaplock(tid
, ip
, tlckDTREE
| tlckRELOCATE
);
1685 pxdlock
= (struct pxd_lock
*) & tlck
->lock
;
1686 pxdlock
->flag
= mlckFREEPXD
;
1687 pxdlock
->pxd
= sp
->header
.self
;
1691 * Update directory index table to reflect new page address
1697 stbl
= DT_GETSTBL(sp
);
1698 for (n
= 0; n
< sp
->header
.nextindex
; n
++) {
1700 (struct ldtentry
*) & sp
->slot
[stbl
[n
]];
1701 modify_index(tid
, ip
,
1702 le32_to_cpu(ldtentry
->index
),
1703 xaddr
, n
, &mp
, &lblock
);
1706 release_metapage(mp
);
1713 sp
->header
.self
= *pxd
;
1715 jfs_info("dtExtendPage: ip:0x%p smp:0x%p sp:0x%p", ip
, smp
, sp
);
1717 BT_MARK_DIRTY(smp
, ip
);
1719 * acquire a transaction lock on the extended/leaf page
1721 tlck
= txLock(tid
, ip
, smp
, tlckDTREE
| type
);
1722 dtlck
= (struct dt_lock
*) & tlck
->lock
;
1723 lv
= & dtlck
->lv
[0];
1725 /* update buffer extent descriptor of extended page */
1726 xlen
= lengthPXD(pxd
);
1727 xsize
= xlen
<< JFS_SBI(sb
)->l2bsize
;
1730 * copy old stbl to new stbl at start of extended area
1732 oldstblindex
= sp
->header
.stblindex
;
1733 oldstblsize
= (sp
->header
.maxslot
+ 31) >> L2DTSLOTSIZE
;
1734 newstblindex
= sp
->header
.maxslot
;
1735 n
= xsize
>> L2DTSLOTSIZE
;
1736 newstblsize
= (n
+ 31) >> L2DTSLOTSIZE
;
1737 memcpy(&sp
->slot
[newstblindex
], &sp
->slot
[oldstblindex
],
1738 sp
->header
.nextindex
);
1741 * in-line extension: linelock old area of extended page
1743 if (type
== tlckEXTEND
) {
1744 /* linelock header */
1750 /* linelock new stbl of extended page */
1751 lv
->offset
= newstblindex
;
1752 lv
->length
= newstblsize
;
1755 * relocation: linelock whole relocated area
1759 lv
->length
= sp
->header
.maxslot
+ newstblsize
;
1764 sp
->header
.maxslot
= n
;
1765 sp
->header
.stblindex
= newstblindex
;
1766 /* sp->header.nextindex remains the same */
1769 * add old stbl region at head of freelist
1773 last
= sp
->header
.freelist
;
1774 for (n
= 0; n
< oldstblsize
; n
++, fsi
++, f
++) {
1778 sp
->header
.freelist
= last
;
1779 sp
->header
.freecnt
+= oldstblsize
;
1782 * append free region of newly extended area at tail of freelist
1784 /* init free region of newly extended area */
1785 fsi
= n
= newstblindex
+ newstblsize
;
1787 for (fsi
++; fsi
< sp
->header
.maxslot
; f
++, fsi
++)
1791 /* append new free region at tail of old freelist */
1792 fsi
= sp
->header
.freelist
;
1794 sp
->header
.freelist
= n
;
1799 } while (fsi
!= -1);
1804 sp
->header
.freecnt
+= sp
->header
.maxslot
- n
;
1807 * insert the new entry
1809 dtInsertEntry(sp
, split
->index
, split
->key
, split
->data
, &dtlck
);
1811 BT_MARK_DIRTY(pmp
, ip
);
1813 * linelock any freeslots residing in old extent
1815 if (type
== tlckEXTEND
) {
1816 n
= sp
->header
.maxslot
>> 2;
1817 if (sp
->header
.freelist
< n
)
1818 dtLinelockFreelist(sp
, n
, &dtlck
);
1822 * update parent entry on the parent/root page
1825 * acquire a transaction lock on the parent/root page
1827 tlck
= txLock(tid
, ip
, pmp
, tlckDTREE
| tlckENTRY
);
1828 dtlck
= (struct dt_lock
*) & tlck
->lock
;
1829 lv
= & dtlck
->lv
[dtlck
->index
];
1831 /* linelock parent entry - 1st slot */
1836 /* update the parent pxd for page extension */
1837 tpxd
= (pxd_t
*) & pp
->slot
[1];
1849 * split the full root page into
1850 * original/root/split page and new right page
1851 * i.e., root remains fixed in tree anchor (inode) and
1852 * the root is copied to a single new right child page
1853 * since root page << non-root page, and
1854 * the split root page contains a single entry for the
1855 * new right child page.
1859 * return: 0 - success;
1861 * return new page pinned;
1863 static int dtSplitRoot(tid_t tid
,
1864 struct inode
*ip
, struct dtsplit
* split
, struct metapage
** rmpp
)
1866 struct super_block
*sb
= ip
->i_sb
;
1867 struct metapage
*smp
;
1869 struct metapage
*rmp
;
1876 int fsi
, stblsize
, n
;
1879 struct pxdlist
*pxdlist
;
1881 struct dt_lock
*dtlck
;
1886 /* get split root page */
1888 sp
= &JFS_IP(ip
)->i_dtroot
;
1891 * allocate/initialize a single (right) child page
1893 * N.B. at first split, a one (or two) block to fit new entry
1894 * is allocated; at subsequent split, a full page is allocated;
1896 pxdlist
= split
->pxdlist
;
1897 pxd
= &pxdlist
->pxd
[pxdlist
->npxd
];
1899 rbn
= addressPXD(pxd
);
1900 xlen
= lengthPXD(pxd
);
1901 xsize
= xlen
<< JFS_SBI(sb
)->l2bsize
;
1902 rmp
= get_metapage(ip
, rbn
, xsize
, 1);
1908 /* Allocate blocks to quota. */
1909 rc
= dquot_alloc_block(ip
, lengthPXD(pxd
));
1911 release_metapage(rmp
);
1915 BT_MARK_DIRTY(rmp
, ip
);
1917 * acquire a transaction lock on the new right page
1919 tlck
= txLock(tid
, ip
, rmp
, tlckDTREE
| tlckNEW
);
1920 dtlck
= (struct dt_lock
*) & tlck
->lock
;
1923 (sp
->header
.flag
& BT_LEAF
) ? BT_LEAF
: BT_INTERNAL
;
1924 rp
->header
.self
= *pxd
;
1926 /* initialize sibling pointers */
1927 rp
->header
.next
= 0;
1928 rp
->header
.prev
= 0;
1931 * move in-line root page into new right page extent
1933 /* linelock header + copied entries + new stbl (1st slot) in new page */
1934 ASSERT(dtlck
->index
== 0);
1935 lv
= & dtlck
->lv
[0];
1937 lv
->length
= 10; /* 1 + 8 + 1 */
1940 n
= xsize
>> L2DTSLOTSIZE
;
1941 rp
->header
.maxslot
= n
;
1942 stblsize
= (n
+ 31) >> L2DTSLOTSIZE
;
1944 /* copy old stbl to new stbl at start of extended area */
1945 rp
->header
.stblindex
= DTROOTMAXSLOT
;
1946 stbl
= (s8
*) & rp
->slot
[DTROOTMAXSLOT
];
1947 memcpy(stbl
, sp
->header
.stbl
, sp
->header
.nextindex
);
1948 rp
->header
.nextindex
= sp
->header
.nextindex
;
1950 /* copy old data area to start of new data area */
1951 memcpy(&rp
->slot
[1], &sp
->slot
[1], IDATASIZE
);
1954 * append free region of newly extended area at tail of freelist
1956 /* init free region of newly extended area */
1957 fsi
= n
= DTROOTMAXSLOT
+ stblsize
;
1959 for (fsi
++; fsi
< rp
->header
.maxslot
; f
++, fsi
++)
1963 /* append new free region at tail of old freelist */
1964 fsi
= sp
->header
.freelist
;
1966 rp
->header
.freelist
= n
;
1968 rp
->header
.freelist
= fsi
;
1973 } while (fsi
!= -1);
1978 rp
->header
.freecnt
= sp
->header
.freecnt
+ rp
->header
.maxslot
- n
;
1981 * Update directory index table for entries now in right page
1983 if ((rp
->header
.flag
& BT_LEAF
) && DO_INDEX(ip
)) {
1985 struct metapage
*mp
= NULL
;
1986 struct ldtentry
*ldtentry
;
1988 stbl
= DT_GETSTBL(rp
);
1989 for (n
= 0; n
< rp
->header
.nextindex
; n
++) {
1990 ldtentry
= (struct ldtentry
*) & rp
->slot
[stbl
[n
]];
1991 modify_index(tid
, ip
, le32_to_cpu(ldtentry
->index
),
1992 rbn
, n
, &mp
, &lblock
);
1995 release_metapage(mp
);
1998 * insert the new entry into the new right/child page
1999 * (skip index in the new right page will not change)
2001 dtInsertEntry(rp
, split
->index
, split
->key
, split
->data
, &dtlck
);
2004 * reset parent/root page
2006 * set the 1st entry offset to 0, which force the left-most key
2007 * at any level of the tree to be less than any search key.
2009 * The btree comparison code guarantees that the left-most key on any
2010 * level of the tree is never used, so it doesn't need to be filled in.
2012 BT_MARK_DIRTY(smp
, ip
);
2014 * acquire a transaction lock on the root page (in-memory inode)
2016 tlck
= txLock(tid
, ip
, smp
, tlckDTREE
| tlckNEW
| tlckBTROOT
);
2017 dtlck
= (struct dt_lock
*) & tlck
->lock
;
2020 ASSERT(dtlck
->index
== 0);
2021 lv
= & dtlck
->lv
[0];
2023 lv
->length
= DTROOTMAXSLOT
;
2026 /* update page header of root */
2027 if (sp
->header
.flag
& BT_LEAF
) {
2028 sp
->header
.flag
&= ~BT_LEAF
;
2029 sp
->header
.flag
|= BT_INTERNAL
;
2032 /* init the first entry */
2033 s
= (struct idtentry
*) & sp
->slot
[DTENTRYSTART
];
2039 stbl
= sp
->header
.stbl
;
2040 stbl
[0] = DTENTRYSTART
;
2041 sp
->header
.nextindex
= 1;
2044 fsi
= DTENTRYSTART
+ 1;
2047 /* init free region of remaining area */
2048 for (fsi
++; fsi
< DTROOTMAXSLOT
; f
++, fsi
++)
2052 sp
->header
.freelist
= DTENTRYSTART
+ 1;
2053 sp
->header
.freecnt
= DTROOTMAXSLOT
- (DTENTRYSTART
+ 1);
2064 * function: delete the entry(s) referenced by a key.
2070 int dtDelete(tid_t tid
,
2071 struct inode
*ip
, struct component_name
* key
, ino_t
* ino
, int flag
)
2075 struct metapage
*mp
, *imp
;
2078 struct btstack btstack
;
2079 struct dt_lock
*dtlck
;
2083 struct ldtentry
*ldtentry
;
2085 u32 table_index
, next_index
;
2086 struct metapage
*nmp
;
2090 * search for the entry to delete:
2092 * dtSearch() returns (leaf page pinned, index at which to delete).
2094 if ((rc
= dtSearch(ip
, key
, ino
, &btstack
, flag
)))
2097 /* retrieve search result */
2098 DT_GETSEARCH(ip
, btstack
.top
, bn
, mp
, p
, index
);
2101 * We need to find put the index of the next entry into the
2102 * directory index table in order to resume a readdir from this
2106 stbl
= DT_GETSTBL(p
);
2107 ldtentry
= (struct ldtentry
*) & p
->slot
[stbl
[index
]];
2108 table_index
= le32_to_cpu(ldtentry
->index
);
2109 if (index
== (p
->header
.nextindex
- 1)) {
2111 * Last entry in this leaf page
2113 if ((p
->header
.flag
& BT_ROOT
)
2114 || (p
->header
.next
== 0))
2117 /* Read next leaf page */
2118 DT_GETPAGE(ip
, le64_to_cpu(p
->header
.next
),
2119 nmp
, PSIZE
, np
, rc
);
2123 stbl
= DT_GETSTBL(np
);
2125 (struct ldtentry
*) & np
->
2128 le32_to_cpu(ldtentry
->index
);
2134 (struct ldtentry
*) & p
->slot
[stbl
[index
+ 1]];
2135 next_index
= le32_to_cpu(ldtentry
->index
);
2137 free_index(tid
, ip
, table_index
, next_index
);
2140 * the leaf page becomes empty, delete the page
2142 if (p
->header
.nextindex
== 1) {
2143 /* delete empty page */
2144 rc
= dtDeleteUp(tid
, ip
, mp
, p
, &btstack
);
2147 * the leaf page has other entries remaining:
2149 * delete the entry from the leaf page.
2152 BT_MARK_DIRTY(mp
, ip
);
2154 * acquire a transaction lock on the leaf page
2156 tlck
= txLock(tid
, ip
, mp
, tlckDTREE
| tlckENTRY
);
2157 dtlck
= (struct dt_lock
*) & tlck
->lock
;
2160 * Do not assume that dtlck->index will be zero. During a
2161 * rename within a directory, this transaction may have
2162 * modified this page already when adding the new entry.
2165 /* linelock header */
2166 if (dtlck
->index
>= dtlck
->maxcnt
)
2167 dtlck
= (struct dt_lock
*) txLinelock(dtlck
);
2168 lv
= & dtlck
->lv
[dtlck
->index
];
2173 /* linelock stbl of non-root leaf page */
2174 if (!(p
->header
.flag
& BT_ROOT
)) {
2175 if (dtlck
->index
>= dtlck
->maxcnt
)
2176 dtlck
= (struct dt_lock
*) txLinelock(dtlck
);
2177 lv
= & dtlck
->lv
[dtlck
->index
];
2178 i
= index
>> L2DTSLOTSIZE
;
2179 lv
->offset
= p
->header
.stblindex
+ i
;
2181 ((p
->header
.nextindex
- 1) >> L2DTSLOTSIZE
) -
2186 /* free the leaf entry */
2187 dtDeleteEntry(p
, index
, &dtlck
);
2190 * Update directory index table for entries moved in stbl
2192 if (DO_INDEX(ip
) && index
< p
->header
.nextindex
) {
2196 stbl
= DT_GETSTBL(p
);
2197 for (i
= index
; i
< p
->header
.nextindex
; i
++) {
2199 (struct ldtentry
*) & p
->slot
[stbl
[i
]];
2200 modify_index(tid
, ip
,
2201 le32_to_cpu(ldtentry
->index
),
2202 bn
, i
, &imp
, &lblock
);
2205 release_metapage(imp
);
2219 * free empty pages as propagating deletion up the tree
2225 static int dtDeleteUp(tid_t tid
, struct inode
*ip
,
2226 struct metapage
* fmp
, dtpage_t
* fp
, struct btstack
* btstack
)
2229 struct metapage
*mp
;
2231 int index
, nextindex
;
2233 struct btframe
*parent
;
2234 struct dt_lock
*dtlck
;
2237 struct pxd_lock
*pxdlock
;
2241 * keep the root leaf page which has become empty
2243 if (BT_IS_ROOT(fmp
)) {
2247 * dtInitRoot() acquires txlock on the root
2249 dtInitRoot(tid
, ip
, PARENT(ip
));
2257 * free the non-root leaf page
2260 * acquire a transaction lock on the page
2262 * write FREEXTENT|NOREDOPAGE log record
2263 * N.B. linelock is overlaid as freed extent descriptor, and
2264 * the buffer page is freed;
2266 tlck
= txMaplock(tid
, ip
, tlckDTREE
| tlckFREE
);
2267 pxdlock
= (struct pxd_lock
*) & tlck
->lock
;
2268 pxdlock
->flag
= mlckFREEPXD
;
2269 pxdlock
->pxd
= fp
->header
.self
;
2272 /* update sibling pointers */
2273 if ((rc
= dtRelink(tid
, ip
, fp
))) {
2278 xlen
= lengthPXD(&fp
->header
.self
);
2280 /* Free quota allocation. */
2281 dquot_free_block(ip
, xlen
);
2283 /* free/invalidate its buffer page */
2284 discard_metapage(fmp
);
2287 * propagate page deletion up the directory tree
2289 * If the delete from the parent page makes it empty,
2290 * continue all the way up the tree.
2291 * stop if the root page is reached (which is never deleted) or
2292 * if the entry deletion does not empty the page.
2294 while ((parent
= BT_POP(btstack
)) != NULL
) {
2295 /* pin the parent page <sp> */
2296 DT_GETPAGE(ip
, parent
->bn
, mp
, PSIZE
, p
, rc
);
2301 * free the extent of the child page deleted
2303 index
= parent
->index
;
2306 * delete the entry for the child page from parent
2308 nextindex
= p
->header
.nextindex
;
2311 * the parent has the single entry being deleted:
2313 * free the parent page which has become empty.
2315 if (nextindex
== 1) {
2317 * keep the root internal page which has become empty
2319 if (p
->header
.flag
& BT_ROOT
) {
2323 * dtInitRoot() acquires txlock on the root
2325 dtInitRoot(tid
, ip
, PARENT(ip
));
2332 * free the parent page
2336 * acquire a transaction lock on the page
2338 * write FREEXTENT|NOREDOPAGE log record
2342 tlckDTREE
| tlckFREE
);
2343 pxdlock
= (struct pxd_lock
*) & tlck
->lock
;
2344 pxdlock
->flag
= mlckFREEPXD
;
2345 pxdlock
->pxd
= p
->header
.self
;
2348 /* update sibling pointers */
2349 if ((rc
= dtRelink(tid
, ip
, p
))) {
2354 xlen
= lengthPXD(&p
->header
.self
);
2356 /* Free quota allocation */
2357 dquot_free_block(ip
, xlen
);
2359 /* free/invalidate its buffer page */
2360 discard_metapage(mp
);
2368 * the parent has other entries remaining:
2370 * delete the router entry from the parent page.
2372 BT_MARK_DIRTY(mp
, ip
);
2374 * acquire a transaction lock on the page
2376 * action: router entry deletion
2378 tlck
= txLock(tid
, ip
, mp
, tlckDTREE
| tlckENTRY
);
2379 dtlck
= (struct dt_lock
*) & tlck
->lock
;
2381 /* linelock header */
2382 if (dtlck
->index
>= dtlck
->maxcnt
)
2383 dtlck
= (struct dt_lock
*) txLinelock(dtlck
);
2384 lv
= & dtlck
->lv
[dtlck
->index
];
2389 /* linelock stbl of non-root leaf page */
2390 if (!(p
->header
.flag
& BT_ROOT
)) {
2391 if (dtlck
->index
< dtlck
->maxcnt
)
2394 dtlck
= (struct dt_lock
*) txLinelock(dtlck
);
2395 lv
= & dtlck
->lv
[0];
2397 i
= index
>> L2DTSLOTSIZE
;
2398 lv
->offset
= p
->header
.stblindex
+ i
;
2400 ((p
->header
.nextindex
- 1) >> L2DTSLOTSIZE
) -
2405 /* free the router entry */
2406 dtDeleteEntry(p
, index
, &dtlck
);
2408 /* reset key of new leftmost entry of level (for consistency) */
2410 ((p
->header
.flag
& BT_ROOT
) || p
->header
.prev
== 0))
2411 dtTruncateEntry(p
, 0, &dtlck
);
2413 /* unpin the parent page */
2416 /* exit propagation up */
2421 ip
->i_size
-= PSIZE
;
2428 * NAME: dtRelocate()
2430 * FUNCTION: relocate dtpage (internal or leaf) of directory;
2431 * This function is mainly used by defragfs utility.
2433 int dtRelocate(tid_t tid
, struct inode
*ip
, s64 lmxaddr
, pxd_t
* opxd
,
2437 struct metapage
*mp
, *pmp
, *lmp
, *rmp
;
2438 dtpage_t
*p
, *pp
, *rp
= 0, *lp
= 0;
2441 struct btstack btstack
;
2443 s64 oxaddr
, nextbn
, prevbn
;
2446 struct dt_lock
*dtlck
;
2447 struct pxd_lock
*pxdlock
;
2451 oxaddr
= addressPXD(opxd
);
2452 xlen
= lengthPXD(opxd
);
2454 jfs_info("dtRelocate: lmxaddr:%Ld xaddr:%Ld:%Ld xlen:%d",
2455 (long long)lmxaddr
, (long long)oxaddr
, (long long)nxaddr
,
2459 * 1. get the internal parent dtpage covering
2460 * router entry for the tartget page to be relocated;
2462 rc
= dtSearchNode(ip
, lmxaddr
, opxd
, &btstack
);
2466 /* retrieve search result */
2467 DT_GETSEARCH(ip
, btstack
.top
, bn
, pmp
, pp
, index
);
2468 jfs_info("dtRelocate: parent router entry validated.");
2471 * 2. relocate the target dtpage
2473 /* read in the target page from src extent */
2474 DT_GETPAGE(ip
, oxaddr
, mp
, PSIZE
, p
, rc
);
2476 /* release the pinned parent page */
2482 * read in sibling pages if any to update sibling pointers;
2485 if (p
->header
.next
) {
2486 nextbn
= le64_to_cpu(p
->header
.next
);
2487 DT_GETPAGE(ip
, nextbn
, rmp
, PSIZE
, rp
, rc
);
2496 if (p
->header
.prev
) {
2497 prevbn
= le64_to_cpu(p
->header
.prev
);
2498 DT_GETPAGE(ip
, prevbn
, lmp
, PSIZE
, lp
, rc
);
2508 /* at this point, all xtpages to be updated are in memory */
2511 * update sibling pointers of sibling dtpages if any;
2514 tlck
= txLock(tid
, ip
, lmp
, tlckDTREE
| tlckRELINK
);
2515 dtlck
= (struct dt_lock
*) & tlck
->lock
;
2516 /* linelock header */
2517 ASSERT(dtlck
->index
== 0);
2518 lv
= & dtlck
->lv
[0];
2523 lp
->header
.next
= cpu_to_le64(nxaddr
);
2528 tlck
= txLock(tid
, ip
, rmp
, tlckDTREE
| tlckRELINK
);
2529 dtlck
= (struct dt_lock
*) & tlck
->lock
;
2530 /* linelock header */
2531 ASSERT(dtlck
->index
== 0);
2532 lv
= & dtlck
->lv
[0];
2537 rp
->header
.prev
= cpu_to_le64(nxaddr
);
2542 * update the target dtpage to be relocated
2544 * write LOG_REDOPAGE of LOG_NEW type for dst page
2545 * for the whole target page (logredo() will apply
2546 * after image and update bmap for allocation of the
2547 * dst extent), and update bmap for allocation of
2550 tlck
= txLock(tid
, ip
, mp
, tlckDTREE
| tlckNEW
);
2551 dtlck
= (struct dt_lock
*) & tlck
->lock
;
2552 /* linelock header */
2553 ASSERT(dtlck
->index
== 0);
2554 lv
= & dtlck
->lv
[0];
2556 /* update the self address in the dtpage header */
2557 pxd
= &p
->header
.self
;
2558 PXDaddress(pxd
, nxaddr
);
2560 /* the dst page is the same as the src page, i.e.,
2561 * linelock for afterimage of the whole page;
2564 lv
->length
= p
->header
.maxslot
;
2567 /* update the buffer extent descriptor of the dtpage */
2568 xsize
= xlen
<< JFS_SBI(ip
->i_sb
)->l2bsize
;
2570 /* unpin the relocated page */
2572 jfs_info("dtRelocate: target dtpage relocated.");
2574 /* the moved extent is dtpage, then a LOG_NOREDOPAGE log rec
2575 * needs to be written (in logredo(), the LOG_NOREDOPAGE log rec
2576 * will also force a bmap update ).
2580 * 3. acquire maplock for the source extent to be freed;
2582 /* for dtpage relocation, write a LOG_NOREDOPAGE record
2583 * for the source dtpage (logredo() will init NoRedoPage
2584 * filter and will also update bmap for free of the source
2585 * dtpage), and upadte bmap for free of the source dtpage;
2587 tlck
= txMaplock(tid
, ip
, tlckDTREE
| tlckFREE
);
2588 pxdlock
= (struct pxd_lock
*) & tlck
->lock
;
2589 pxdlock
->flag
= mlckFREEPXD
;
2590 PXDaddress(&pxdlock
->pxd
, oxaddr
);
2591 PXDlength(&pxdlock
->pxd
, xlen
);
2595 * 4. update the parent router entry for relocation;
2597 * acquire tlck for the parent entry covering the target dtpage;
2598 * write LOG_REDOPAGE to apply after image only;
2600 jfs_info("dtRelocate: update parent router entry.");
2601 tlck
= txLock(tid
, ip
, pmp
, tlckDTREE
| tlckENTRY
);
2602 dtlck
= (struct dt_lock
*) & tlck
->lock
;
2603 lv
= & dtlck
->lv
[dtlck
->index
];
2605 /* update the PXD with the new address */
2606 stbl
= DT_GETSTBL(pp
);
2607 pxd
= (pxd_t
*) & pp
->slot
[stbl
[index
]];
2608 PXDaddress(pxd
, nxaddr
);
2609 lv
->offset
= stbl
[index
];
2613 /* unpin the parent dtpage */
2620 * NAME: dtSearchNode()
2622 * FUNCTION: Search for an dtpage containing a specified address
2623 * This function is mainly used by defragfs utility.
2625 * NOTE: Search result on stack, the found page is pinned at exit.
2626 * The result page must be an internal dtpage.
2627 * lmxaddr give the address of the left most page of the
2628 * dtree level, in which the required dtpage resides.
2630 static int dtSearchNode(struct inode
*ip
, s64 lmxaddr
, pxd_t
* kpxd
,
2631 struct btstack
* btstack
)
2635 struct metapage
*mp
;
2637 int psize
= 288; /* initial in-line directory */
2641 struct btframe
*btsp
;
2643 BT_CLR(btstack
); /* reset stack */
2646 * descend tree to the level with specified leftmost page
2648 * by convention, root bn = 0.
2651 /* get/pin the page to search */
2652 DT_GETPAGE(ip
, bn
, mp
, psize
, p
, rc
);
2656 /* does the xaddr of leftmost page of the levevl
2657 * matches levevl search key ?
2659 if (p
->header
.flag
& BT_ROOT
) {
2662 } else if (addressPXD(&p
->header
.self
) == lmxaddr
)
2666 * descend down to leftmost child page
2668 if (p
->header
.flag
& BT_LEAF
) {
2673 /* get the leftmost entry */
2674 stbl
= DT_GETSTBL(p
);
2675 pxd
= (pxd_t
*) & p
->slot
[stbl
[0]];
2677 /* get the child page block address */
2678 bn
= addressPXD(pxd
);
2679 psize
= lengthPXD(pxd
) << JFS_SBI(ip
->i_sb
)->l2bsize
;
2680 /* unpin the parent page */
2685 * search each page at the current levevl
2688 stbl
= DT_GETSTBL(p
);
2689 for (i
= 0; i
< p
->header
.nextindex
; i
++) {
2690 pxd
= (pxd_t
*) & p
->slot
[stbl
[i
]];
2692 /* found the specified router entry */
2693 if (addressPXD(pxd
) == addressPXD(kpxd
) &&
2694 lengthPXD(pxd
) == lengthPXD(kpxd
)) {
2695 btsp
= btstack
->top
;
2704 /* get the right sibling page if any */
2706 bn
= le64_to_cpu(p
->header
.next
);
2712 /* unpin current page */
2715 /* get the right sibling page */
2716 DT_GETPAGE(ip
, bn
, mp
, PSIZE
, p
, rc
);
2722 #endif /* _NOTYET */
2728 * link around a freed page.
2731 * fp: page to be freed
2735 static int dtRelink(tid_t tid
, struct inode
*ip
, dtpage_t
* p
)
2738 struct metapage
*mp
;
2741 struct dt_lock
*dtlck
;
2744 nextbn
= le64_to_cpu(p
->header
.next
);
2745 prevbn
= le64_to_cpu(p
->header
.prev
);
2747 /* update prev pointer of the next page */
2749 DT_GETPAGE(ip
, nextbn
, mp
, PSIZE
, p
, rc
);
2753 BT_MARK_DIRTY(mp
, ip
);
2755 * acquire a transaction lock on the next page
2757 * action: update prev pointer;
2759 tlck
= txLock(tid
, ip
, mp
, tlckDTREE
| tlckRELINK
);
2760 jfs_info("dtRelink nextbn: tlck = 0x%p, ip = 0x%p, mp=0x%p",
2762 dtlck
= (struct dt_lock
*) & tlck
->lock
;
2764 /* linelock header */
2765 if (dtlck
->index
>= dtlck
->maxcnt
)
2766 dtlck
= (struct dt_lock
*) txLinelock(dtlck
);
2767 lv
= & dtlck
->lv
[dtlck
->index
];
2772 p
->header
.prev
= cpu_to_le64(prevbn
);
2776 /* update next pointer of the previous page */
2778 DT_GETPAGE(ip
, prevbn
, mp
, PSIZE
, p
, rc
);
2782 BT_MARK_DIRTY(mp
, ip
);
2784 * acquire a transaction lock on the prev page
2786 * action: update next pointer;
2788 tlck
= txLock(tid
, ip
, mp
, tlckDTREE
| tlckRELINK
);
2789 jfs_info("dtRelink prevbn: tlck = 0x%p, ip = 0x%p, mp=0x%p",
2791 dtlck
= (struct dt_lock
*) & tlck
->lock
;
2793 /* linelock header */
2794 if (dtlck
->index
>= dtlck
->maxcnt
)
2795 dtlck
= (struct dt_lock
*) txLinelock(dtlck
);
2796 lv
= & dtlck
->lv
[dtlck
->index
];
2801 p
->header
.next
= cpu_to_le64(nextbn
);
2812 * initialize directory root (inline in inode)
2814 void dtInitRoot(tid_t tid
, struct inode
*ip
, u32 idotdot
)
2816 struct jfs_inode_info
*jfs_ip
= JFS_IP(ip
);
2821 struct dt_lock
*dtlck
;
2826 * If this was previously an non-empty directory, we need to remove
2827 * the old directory table.
2830 if (!jfs_dirtable_inline(ip
)) {
2831 struct tblock
*tblk
= tid_to_tblock(tid
);
2833 * We're playing games with the tid's xflag. If
2834 * we're removing a regular file, the file's xtree
2835 * is committed with COMMIT_PMAP, but we always
2836 * commit the directories xtree with COMMIT_PWMAP.
2838 xflag_save
= tblk
->xflag
;
2841 * xtTruncate isn't guaranteed to fully truncate
2842 * the xtree. The caller needs to check i_size
2843 * after committing the transaction to see if
2844 * additional truncation is needed. The
2845 * COMMIT_Stale flag tells caller that we
2846 * initiated the truncation.
2848 xtTruncate(tid
, ip
, 0, COMMIT_PWMAP
);
2849 set_cflag(COMMIT_Stale
, ip
);
2851 tblk
->xflag
= xflag_save
;
2855 jfs_ip
->next_index
= 2;
2857 ip
->i_size
= IDATASIZE
;
2860 * acquire a transaction lock on the root
2862 * action: directory initialization;
2864 tlck
= txLock(tid
, ip
, (struct metapage
*) & jfs_ip
->bxflag
,
2865 tlckDTREE
| tlckENTRY
| tlckBTROOT
);
2866 dtlck
= (struct dt_lock
*) & tlck
->lock
;
2869 ASSERT(dtlck
->index
== 0);
2870 lv
= & dtlck
->lv
[0];
2872 lv
->length
= DTROOTMAXSLOT
;
2875 p
= &jfs_ip
->i_dtroot
;
2877 p
->header
.flag
= DXD_INDEX
| BT_ROOT
| BT_LEAF
;
2879 p
->header
.nextindex
= 0;
2885 /* init data area of root */
2886 for (fsi
++; fsi
< DTROOTMAXSLOT
; f
++, fsi
++)
2890 p
->header
.freelist
= 1;
2891 p
->header
.freecnt
= 8;
2893 /* init '..' entry */
2894 p
->header
.idotdot
= cpu_to_le32(idotdot
);
2900 * add_missing_indices()
2902 * function: Fix dtree page in which one or more entries has an invalid index.
2903 * fsck.jfs should really fix this, but it currently does not.
2904 * Called from jfs_readdir when bad index is detected.
2906 static void add_missing_indices(struct inode
*inode
, s64 bn
)
2909 struct dt_lock
*dtlck
;
2913 struct metapage
*mp
;
2920 tid
= txBegin(inode
->i_sb
, 0);
2922 DT_GETPAGE(inode
, bn
, mp
, PSIZE
, p
, rc
);
2925 printk(KERN_ERR
"DT_GETPAGE failed!\n");
2928 BT_MARK_DIRTY(mp
, inode
);
2930 ASSERT(p
->header
.flag
& BT_LEAF
);
2932 tlck
= txLock(tid
, inode
, mp
, tlckDTREE
| tlckENTRY
);
2934 tlck
->type
|= tlckBTROOT
;
2936 dtlck
= (struct dt_lock
*) &tlck
->lock
;
2938 stbl
= DT_GETSTBL(p
);
2939 for (i
= 0; i
< p
->header
.nextindex
; i
++) {
2940 d
= (struct ldtentry
*) &p
->slot
[stbl
[i
]];
2941 index
= le32_to_cpu(d
->index
);
2942 if ((index
< 2) || (index
>= JFS_IP(inode
)->next_index
)) {
2943 d
->index
= cpu_to_le32(add_index(tid
, inode
, bn
, i
));
2944 if (dtlck
->index
>= dtlck
->maxcnt
)
2945 dtlck
= (struct dt_lock
*) txLinelock(dtlck
);
2946 lv
= &dtlck
->lv
[dtlck
->index
];
2947 lv
->offset
= stbl
[i
];
2954 (void) txCommit(tid
, 1, &inode
, 0);
2960 * Buffer to hold directory entry info while traversing a dtree page
2961 * before being fed to the filldir function
2971 * function to determine next variable-sized jfs_dirent in buffer
2973 static inline struct jfs_dirent
*next_jfs_dirent(struct jfs_dirent
*dirent
)
2975 return (struct jfs_dirent
*)
2977 ((sizeof (struct jfs_dirent
) + dirent
->name_len
+ 1 +
2978 sizeof (loff_t
) - 1) &
2979 ~(sizeof (loff_t
) - 1)));
2985 * function: read directory entries sequentially
2986 * from the specified entry offset
2990 * return: offset = (pn, index) of start entry
2991 * of next jfs_readdir()/dtRead()
2993 int jfs_readdir(struct file
*file
, struct dir_context
*ctx
)
2995 struct inode
*ip
= file_inode(file
);
2996 struct nls_table
*codepage
= JFS_SBI(ip
->i_sb
)->nls_tab
;
2998 loff_t dtpos
; /* legacy OS/2 style position */
3003 } *dtoffset
= (struct dtoffset
*) &dtpos
;
3005 struct metapage
*mp
;
3009 struct btstack btstack
;
3013 int d_namleft
, len
, outlen
;
3014 unsigned long dirent_buf
;
3018 uint loop_count
= 0;
3019 struct jfs_dirent
*jfs_dirent
;
3021 int overflow
, fix_page
, page_fixed
= 0;
3022 static int unique_pos
= 2; /* If we can't fix broken index */
3024 if (ctx
->pos
== DIREND
)
3029 * persistent index is stored in directory entries.
3030 * Special cases: 0 = .
3032 * -1 = End of directory
3036 dir_index
= (u32
) ctx
->pos
;
3039 * NFSv4 reserves cookies 1 and 2 for . and .. so the value
3040 * we return to the vfs is one greater than the one we use
3046 if (dir_index
> 1) {
3047 struct dir_table_slot dirtab_slot
;
3050 (dir_index
>= JFS_IP(ip
)->next_index
)) {
3051 /* Stale position. Directory has shrunk */
3056 rc
= read_index(ip
, dir_index
, &dirtab_slot
);
3061 if (dirtab_slot
.flag
== DIR_INDEX_FREE
) {
3062 if (loop_count
++ > JFS_IP(ip
)->next_index
) {
3063 jfs_err("jfs_readdir detected infinite loop!");
3067 dir_index
= le32_to_cpu(dirtab_slot
.addr2
);
3068 if (dir_index
== -1) {
3074 bn
= addressDTS(&dirtab_slot
);
3075 index
= dirtab_slot
.slot
;
3076 DT_GETPAGE(ip
, bn
, mp
, PSIZE
, p
, rc
);
3081 if (p
->header
.flag
& BT_INTERNAL
) {
3082 jfs_err("jfs_readdir: bad index table");
3088 if (dir_index
== 0) {
3093 if (!dir_emit(ctx
, ".", 1, ip
->i_ino
, DT_DIR
))
3100 if (!dir_emit(ctx
, "..", 2, PARENT(ip
), DT_DIR
))
3104 * Find first entry of left-most leaf
3111 if ((rc
= dtReadFirst(ip
, &btstack
)))
3114 DT_GETSEARCH(ip
, btstack
.top
, bn
, mp
, p
, index
);
3118 * Legacy filesystem - OS/2 & Linux JFS < 0.3.6
3120 * pn = 0; index = 1: First entry "."
3121 * pn = 0; index = 2: Second entry ".."
3122 * pn > 0: Real entries, pn=1 -> leftmost page
3123 * pn = index = -1: No more entries
3127 /* build "." entry */
3129 if (!dir_emit(ctx
, ".", 1, ip
->i_ino
, DT_DIR
))
3131 dtoffset
->index
= 2;
3135 if (dtoffset
->pn
== 0) {
3136 if (dtoffset
->index
== 2) {
3137 /* build ".." entry */
3138 if (!dir_emit(ctx
, "..", 2, PARENT(ip
), DT_DIR
))
3141 jfs_err("jfs_readdir called with invalid offset!");
3144 dtoffset
->index
= 0;
3153 if ((rc
= dtReadNext(ip
, &ctx
->pos
, &btstack
))) {
3154 jfs_err("jfs_readdir: unexpected rc = %d from dtReadNext",
3159 /* get start leaf page and index */
3160 DT_GETSEARCH(ip
, btstack
.top
, bn
, mp
, p
, index
);
3162 /* offset beyond directory eof ? */
3169 dirent_buf
= __get_free_page(GFP_KERNEL
);
3170 if (dirent_buf
== 0) {
3172 jfs_warn("jfs_readdir: __get_free_page failed!");
3178 jfs_dirent
= (struct jfs_dirent
*) dirent_buf
;
3180 overflow
= fix_page
= 0;
3182 stbl
= DT_GETSTBL(p
);
3184 for (i
= index
; i
< p
->header
.nextindex
; i
++) {
3185 d
= (struct ldtentry
*) & p
->slot
[stbl
[i
]];
3187 if (((long) jfs_dirent
+ d
->namlen
+ 1) >
3188 (dirent_buf
+ PAGE_SIZE
)) {
3189 /* DBCS codepages could overrun dirent_buf */
3195 d_namleft
= d
->namlen
;
3196 name_ptr
= jfs_dirent
->name
;
3197 jfs_dirent
->ino
= le32_to_cpu(d
->inumber
);
3200 len
= min(d_namleft
, DTLHDRDATALEN
);
3201 jfs_dirent
->position
= le32_to_cpu(d
->index
);
3203 * d->index should always be valid, but it
3204 * isn't. fsck.jfs doesn't create the
3205 * directory index for the lost+found
3206 * directory. Rather than let it go,
3207 * we can try to fix it.
3209 if ((jfs_dirent
->position
< 2) ||
3210 (jfs_dirent
->position
>=
3211 JFS_IP(ip
)->next_index
)) {
3212 if (!page_fixed
&& !isReadOnly(ip
)) {
3215 * setting overflow and setting
3216 * index to i will cause the
3217 * same page to be processed
3218 * again starting here
3224 jfs_dirent
->position
= unique_pos
++;
3227 * We add 1 to the index because we may
3228 * use a value of 2 internally, and NFSv4
3229 * doesn't like that.
3231 jfs_dirent
->position
++;
3233 jfs_dirent
->position
= dtpos
;
3234 len
= min(d_namleft
, DTLHDRDATALEN_LEGACY
);
3237 /* copy the name of head/only segment */
3238 outlen
= jfs_strfromUCS_le(name_ptr
, d
->name
, len
,
3240 jfs_dirent
->name_len
= outlen
;
3242 /* copy name in the additional segment(s) */
3245 t
= (struct dtslot
*) & p
->slot
[next
];
3249 if (d_namleft
== 0) {
3251 "JFS:Dtree error: ino = %ld, bn=%lld, index = %d\n",
3257 len
= min(d_namleft
, DTSLOTDATALEN
);
3258 outlen
= jfs_strfromUCS_le(name_ptr
, t
->name
,
3260 jfs_dirent
->name_len
+= outlen
;
3266 jfs_dirent
= next_jfs_dirent(jfs_dirent
);
3273 /* Point to next leaf page */
3274 if (p
->header
.flag
& BT_ROOT
)
3277 bn
= le64_to_cpu(p
->header
.next
);
3279 /* update offset (pn:index) for new page */
3282 dtoffset
->index
= 0;
3288 /* unpin previous leaf page */
3291 jfs_dirent
= (struct jfs_dirent
*) dirent_buf
;
3292 while (jfs_dirents
--) {
3293 ctx
->pos
= jfs_dirent
->position
;
3294 if (!dir_emit(ctx
, jfs_dirent
->name
,
3295 jfs_dirent
->name_len
,
3296 jfs_dirent
->ino
, DT_UNKNOWN
))
3298 jfs_dirent
= next_jfs_dirent(jfs_dirent
);
3302 add_missing_indices(ip
, bn
);
3306 if (!overflow
&& (bn
== 0)) {
3311 DT_GETPAGE(ip
, bn
, mp
, PSIZE
, p
, rc
);
3313 free_page(dirent_buf
);
3319 free_page(dirent_buf
);
3328 * function: get the leftmost page of the directory
3330 static int dtReadFirst(struct inode
*ip
, struct btstack
* btstack
)
3334 int psize
= 288; /* initial in-line directory */
3335 struct metapage
*mp
;
3338 struct btframe
*btsp
;
3341 BT_CLR(btstack
); /* reset stack */
3344 * descend leftmost path of the tree
3346 * by convention, root bn = 0.
3349 DT_GETPAGE(ip
, bn
, mp
, psize
, p
, rc
);
3354 * leftmost leaf page
3356 if (p
->header
.flag
& BT_LEAF
) {
3357 /* return leftmost entry */
3358 btsp
= btstack
->top
;
3367 * descend down to leftmost child page
3369 if (BT_STACK_FULL(btstack
)) {
3371 jfs_error(ip
->i_sb
, "btstack overrun\n");
3372 BT_STACK_DUMP(btstack
);
3375 /* push (bn, index) of the parent page/entry */
3376 BT_PUSH(btstack
, bn
, 0);
3378 /* get the leftmost entry */
3379 stbl
= DT_GETSTBL(p
);
3380 xd
= (pxd_t
*) & p
->slot
[stbl
[0]];
3382 /* get the child page block address */
3383 bn
= addressPXD(xd
);
3384 psize
= lengthPXD(xd
) << JFS_SBI(ip
->i_sb
)->l2bsize
;
3386 /* unpin the parent page */
3395 * function: get the page of the specified offset (pn:index)
3397 * return: if (offset > eof), bn = -1;
3399 * note: if index > nextindex of the target leaf page,
3400 * start with 1st entry of next leaf page;
3402 static int dtReadNext(struct inode
*ip
, loff_t
* offset
,
3403 struct btstack
* btstack
)
3410 } *dtoffset
= (struct dtoffset
*) offset
;
3412 struct metapage
*mp
;
3417 struct btframe
*btsp
, *parent
;
3421 * get leftmost leaf page pinned
3423 if ((rc
= dtReadFirst(ip
, btstack
)))
3427 DT_GETSEARCH(ip
, btstack
->top
, bn
, mp
, p
, index
);
3429 /* get the start offset (pn:index) */
3430 pn
= dtoffset
->pn
- 1; /* Now pn = 0 represents leftmost leaf */
3431 index
= dtoffset
->index
;
3433 /* start at leftmost page ? */
3435 /* offset beyond eof ? */
3436 if (index
< p
->header
.nextindex
)
3439 if (p
->header
.flag
& BT_ROOT
) {
3444 /* start with 1st entry of next leaf page */
3446 dtoffset
->index
= index
= 0;
3450 /* start at non-leftmost page: scan parent pages for large pn */
3451 if (p
->header
.flag
& BT_ROOT
) {
3456 /* start after next leaf page ? */
3460 /* get leaf page pn = 1 */
3462 bn
= le64_to_cpu(p
->header
.next
);
3464 /* unpin leaf page */
3467 /* offset beyond eof ? */
3476 * scan last internal page level to get target leaf page
3479 /* unpin leftmost leaf page */
3482 /* get left most parent page */
3483 btsp
= btstack
->top
;
3486 DT_GETPAGE(ip
, bn
, mp
, PSIZE
, p
, rc
);
3490 /* scan parent pages at last internal page level */
3491 while (pn
>= p
->header
.nextindex
) {
3492 pn
-= p
->header
.nextindex
;
3494 /* get next parent page address */
3495 bn
= le64_to_cpu(p
->header
.next
);
3497 /* unpin current parent page */
3500 /* offset beyond eof ? */
3506 /* get next parent page */
3507 DT_GETPAGE(ip
, bn
, mp
, PSIZE
, p
, rc
);
3511 /* update parent page stack frame */
3515 /* get leaf page address */
3516 stbl
= DT_GETSTBL(p
);
3517 xd
= (pxd_t
*) & p
->slot
[stbl
[pn
]];
3518 bn
= addressPXD(xd
);
3520 /* unpin parent page */
3524 * get target leaf page
3527 DT_GETPAGE(ip
, bn
, mp
, PSIZE
, p
, rc
);
3532 * leaf page has been completed:
3533 * start with 1st entry of next leaf page
3535 if (index
>= p
->header
.nextindex
) {
3536 bn
= le64_to_cpu(p
->header
.next
);
3538 /* unpin leaf page */
3541 /* offset beyond eof ? */
3547 /* get next leaf page */
3548 DT_GETPAGE(ip
, bn
, mp
, PSIZE
, p
, rc
);
3552 /* start with 1st entry of next leaf page */
3554 dtoffset
->index
= 0;
3558 /* return target leaf page pinned */
3559 btsp
= btstack
->top
;
3561 btsp
->index
= dtoffset
->index
;
3571 * function: compare search key with an internal entry
3574 * < 0 if k is < record
3575 * = 0 if k is = record
3576 * > 0 if k is > record
3578 static int dtCompare(struct component_name
* key
, /* search key */
3579 dtpage_t
* p
, /* directory page */
3581 { /* entry slot index */
3584 int klen
, namlen
, len
, rc
;
3585 struct idtentry
*ih
;
3589 * force the left-most key on internal pages, at any level of
3590 * the tree, to be less than any search key.
3591 * this obviates having to update the leftmost key on an internal
3592 * page when the user inserts a new key in the tree smaller than
3593 * anything that has been stored.
3595 * (? if/when dtSearch() narrows down to 1st entry (index = 0),
3596 * at any internal page at any level of the tree,
3597 * it descends to child of the entry anyway -
3598 * ? make the entry as min size dummy entry)
3600 * if (e->index == 0 && h->prevpg == P_INVALID && !(h->flags & BT_LEAF))
3607 ih
= (struct idtentry
*) & p
->slot
[si
];
3610 namlen
= ih
->namlen
;
3611 len
= min(namlen
, DTIHDRDATALEN
);
3613 /* compare with head/only segment */
3614 len
= min(klen
, len
);
3615 if ((rc
= UniStrncmp_le(kname
, name
, len
)))
3621 /* compare with additional segment(s) */
3623 while (klen
> 0 && namlen
> 0) {
3624 /* compare with next name segment */
3625 t
= (struct dtslot
*) & p
->slot
[si
];
3626 len
= min(namlen
, DTSLOTDATALEN
);
3627 len
= min(klen
, len
);
3629 if ((rc
= UniStrncmp_le(kname
, name
, len
)))
3638 return (klen
- namlen
);
3647 * function: compare search key with an (leaf/internal) entry
3650 * < 0 if k is < record
3651 * = 0 if k is = record
3652 * > 0 if k is > record
3654 static int ciCompare(struct component_name
* key
, /* search key */
3655 dtpage_t
* p
, /* directory page */
3656 int si
, /* entry slot index */
3661 int klen
, namlen
, len
, rc
;
3662 struct ldtentry
*lh
;
3663 struct idtentry
*ih
;
3668 * force the left-most key on internal pages, at any level of
3669 * the tree, to be less than any search key.
3670 * this obviates having to update the leftmost key on an internal
3671 * page when the user inserts a new key in the tree smaller than
3672 * anything that has been stored.
3674 * (? if/when dtSearch() narrows down to 1st entry (index = 0),
3675 * at any internal page at any level of the tree,
3676 * it descends to child of the entry anyway -
3677 * ? make the entry as min size dummy entry)
3679 * if (e->index == 0 && h->prevpg == P_INVALID && !(h->flags & BT_LEAF))
3689 if (p
->header
.flag
& BT_LEAF
) {
3690 lh
= (struct ldtentry
*) & p
->slot
[si
];
3693 namlen
= lh
->namlen
;
3694 if (flag
& JFS_DIR_INDEX
)
3695 len
= min(namlen
, DTLHDRDATALEN
);
3697 len
= min(namlen
, DTLHDRDATALEN_LEGACY
);
3700 * internal page entry
3703 ih
= (struct idtentry
*) & p
->slot
[si
];
3706 namlen
= ih
->namlen
;
3707 len
= min(namlen
, DTIHDRDATALEN
);
3710 /* compare with head/only segment */
3711 len
= min(klen
, len
);
3712 for (i
= 0; i
< len
; i
++, kname
++, name
++) {
3713 /* only uppercase if case-insensitive support is on */
3714 if ((flag
& JFS_OS2
) == JFS_OS2
)
3715 x
= UniToupper(le16_to_cpu(*name
));
3717 x
= le16_to_cpu(*name
);
3718 if ((rc
= *kname
- x
))
3725 /* compare with additional segment(s) */
3726 while (klen
> 0 && namlen
> 0) {
3727 /* compare with next name segment */
3728 t
= (struct dtslot
*) & p
->slot
[si
];
3729 len
= min(namlen
, DTSLOTDATALEN
);
3730 len
= min(klen
, len
);
3732 for (i
= 0; i
< len
; i
++, kname
++, name
++) {
3733 /* only uppercase if case-insensitive support is on */
3734 if ((flag
& JFS_OS2
) == JFS_OS2
)
3735 x
= UniToupper(le16_to_cpu(*name
));
3737 x
= le16_to_cpu(*name
);
3739 if ((rc
= *kname
- x
))
3748 return (klen
- namlen
);
3753 * ciGetLeafPrefixKey()
3755 * function: compute prefix of suffix compression
3756 * from two adjacent leaf entries
3757 * across page boundary
3759 * return: non-zero on error
3762 static int ciGetLeafPrefixKey(dtpage_t
* lp
, int li
, dtpage_t
* rp
,
3763 int ri
, struct component_name
* key
, int flag
)
3766 wchar_t *pl
, *pr
, *kname
;
3767 struct component_name lkey
;
3768 struct component_name rkey
;
3770 lkey
.name
= kmalloc_array(JFS_NAME_MAX
+ 1, sizeof(wchar_t),
3772 if (lkey
.name
== NULL
)
3775 rkey
.name
= kmalloc_array(JFS_NAME_MAX
+ 1, sizeof(wchar_t),
3777 if (rkey
.name
== NULL
) {
3782 /* get left and right key */
3783 dtGetKey(lp
, li
, &lkey
, flag
);
3784 lkey
.name
[lkey
.namlen
] = 0;
3786 if ((flag
& JFS_OS2
) == JFS_OS2
)
3789 dtGetKey(rp
, ri
, &rkey
, flag
);
3790 rkey
.name
[rkey
.namlen
] = 0;
3793 if ((flag
& JFS_OS2
) == JFS_OS2
)
3796 /* compute prefix */
3799 namlen
= min(lkey
.namlen
, rkey
.namlen
);
3800 for (pl
= lkey
.name
, pr
= rkey
.name
;
3801 namlen
; pl
++, pr
++, namlen
--, klen
++, kname
++) {
3804 key
->namlen
= klen
+ 1;
3809 /* l->namlen <= r->namlen since l <= r */
3810 if (lkey
.namlen
< rkey
.namlen
) {
3812 key
->namlen
= klen
+ 1;
3813 } else /* l->namelen == r->namelen */
3827 * function: get key of the entry
3829 static void dtGetKey(dtpage_t
* p
, int i
, /* entry index */
3830 struct component_name
* key
, int flag
)
3834 struct ldtentry
*lh
;
3835 struct idtentry
*ih
;
3842 stbl
= DT_GETSTBL(p
);
3844 if (p
->header
.flag
& BT_LEAF
) {
3845 lh
= (struct ldtentry
*) & p
->slot
[si
];
3847 namlen
= lh
->namlen
;
3849 if (flag
& JFS_DIR_INDEX
)
3850 len
= min(namlen
, DTLHDRDATALEN
);
3852 len
= min(namlen
, DTLHDRDATALEN_LEGACY
);
3854 ih
= (struct idtentry
*) & p
->slot
[si
];
3856 namlen
= ih
->namlen
;
3858 len
= min(namlen
, DTIHDRDATALEN
);
3861 key
->namlen
= namlen
;
3865 * move head/only segment
3867 UniStrncpy_from_le(kname
, name
, len
);
3870 * move additional segment(s)
3873 /* get next segment */
3877 len
= min(namlen
, DTSLOTDATALEN
);
3878 UniStrncpy_from_le(kname
, t
->name
, len
);
3888 * function: allocate free slot(s) and
3889 * write a leaf/internal entry
3891 * return: entry slot index
3893 static void dtInsertEntry(dtpage_t
* p
, int index
, struct component_name
* key
,
3894 ddata_t
* data
, struct dt_lock
** dtlock
)
3896 struct dtslot
*h
, *t
;
3897 struct ldtentry
*lh
= NULL
;
3898 struct idtentry
*ih
= NULL
;
3899 int hsi
, fsi
, klen
, len
, nextindex
;
3904 struct dt_lock
*dtlck
= *dtlock
;
3908 struct metapage
*mp
= NULL
;
3913 /* allocate a free slot */
3914 hsi
= fsi
= p
->header
.freelist
;
3916 p
->header
.freelist
= h
->next
;
3917 --p
->header
.freecnt
;
3919 /* open new linelock */
3920 if (dtlck
->index
>= dtlck
->maxcnt
)
3921 dtlck
= (struct dt_lock
*) txLinelock(dtlck
);
3923 lv
= & dtlck
->lv
[dtlck
->index
];
3926 /* write head/only segment */
3927 if (p
->header
.flag
& BT_LEAF
) {
3928 lh
= (struct ldtentry
*) h
;
3930 lh
->inumber
= cpu_to_le32(data
->leaf
.ino
);
3933 if (data
->leaf
.ip
) {
3934 len
= min(klen
, DTLHDRDATALEN
);
3935 if (!(p
->header
.flag
& BT_ROOT
))
3936 bn
= addressPXD(&p
->header
.self
);
3937 lh
->index
= cpu_to_le32(add_index(data
->leaf
.tid
,
3941 len
= min(klen
, DTLHDRDATALEN_LEGACY
);
3943 ih
= (struct idtentry
*) h
;
3949 len
= min(klen
, DTIHDRDATALEN
);
3952 UniStrncpy_to_le(name
, kname
, len
);
3957 /* write additional segment(s) */
3962 fsi
= p
->header
.freelist
;
3964 p
->header
.freelist
= t
->next
;
3965 --p
->header
.freecnt
;
3967 /* is next slot contiguous ? */
3968 if (fsi
!= xsi
+ 1) {
3969 /* close current linelock */
3973 /* open new linelock */
3974 if (dtlck
->index
< dtlck
->maxcnt
)
3977 dtlck
= (struct dt_lock
*) txLinelock(dtlck
);
3978 lv
= & dtlck
->lv
[0];
3986 len
= min(klen
, DTSLOTDATALEN
);
3987 UniStrncpy_to_le(t
->name
, kname
, len
);
3994 /* close current linelock */
4000 /* terminate last/only segment */
4002 /* single segment entry */
4003 if (p
->header
.flag
& BT_LEAF
)
4008 /* multi-segment entry */
4011 /* if insert into middle, shift right succeeding entries in stbl */
4012 stbl
= DT_GETSTBL(p
);
4013 nextindex
= p
->header
.nextindex
;
4014 if (index
< nextindex
) {
4015 memmove(stbl
+ index
+ 1, stbl
+ index
, nextindex
- index
);
4017 if ((p
->header
.flag
& BT_LEAF
) && data
->leaf
.ip
) {
4021 * Need to update slot number for entries that moved
4025 for (n
= index
+ 1; n
<= nextindex
; n
++) {
4026 lh
= (struct ldtentry
*) & (p
->slot
[stbl
[n
]]);
4027 modify_index(data
->leaf
.tid
, data
->leaf
.ip
,
4028 le32_to_cpu(lh
->index
), bn
, n
,
4032 release_metapage(mp
);
4038 /* advance next available entry index of stbl */
4039 ++p
->header
.nextindex
;
4046 * function: move entries from split/left page to new/right page
4048 * nextindex of dst page and freelist/freecnt of both pages
4051 static void dtMoveEntry(dtpage_t
* sp
, int si
, dtpage_t
* dp
,
4052 struct dt_lock
** sdtlock
, struct dt_lock
** ddtlock
,
4055 int ssi
, next
; /* src slot index */
4056 int di
; /* dst entry index */
4057 int dsi
; /* dst slot index */
4058 s8
*sstbl
, *dstbl
; /* sorted entry table */
4060 struct ldtentry
*slh
, *dlh
= NULL
;
4061 struct idtentry
*sih
, *dih
= NULL
;
4062 struct dtslot
*h
, *s
, *d
;
4063 struct dt_lock
*sdtlck
= *sdtlock
, *ddtlck
= *ddtlock
;
4064 struct lv
*slv
, *dlv
;
4068 sstbl
= (s8
*) & sp
->slot
[sp
->header
.stblindex
];
4069 dstbl
= (s8
*) & dp
->slot
[dp
->header
.stblindex
];
4071 dsi
= dp
->header
.freelist
; /* first (whole page) free slot */
4072 sfsi
= sp
->header
.freelist
;
4074 /* linelock destination entry slot */
4075 dlv
= & ddtlck
->lv
[ddtlck
->index
];
4078 /* linelock source entry slot */
4079 slv
= & sdtlck
->lv
[sdtlck
->index
];
4080 slv
->offset
= sstbl
[si
];
4081 xssi
= slv
->offset
- 1;
4087 for (di
= 0; si
< sp
->header
.nextindex
; si
++, di
++) {
4091 /* is next slot contiguous ? */
4092 if (ssi
!= xssi
+ 1) {
4093 /* close current linelock */
4097 /* open new linelock */
4098 if (sdtlck
->index
< sdtlck
->maxcnt
)
4101 sdtlck
= (struct dt_lock
*) txLinelock(sdtlck
);
4102 slv
= & sdtlck
->lv
[0];
4110 * move head/only segment of an entry
4113 h
= d
= &dp
->slot
[dsi
];
4115 /* get src slot and move */
4117 if (sp
->header
.flag
& BT_LEAF
) {
4118 /* get source entry */
4119 slh
= (struct ldtentry
*) s
;
4120 dlh
= (struct ldtentry
*) h
;
4121 snamlen
= slh
->namlen
;
4124 len
= min(snamlen
, DTLHDRDATALEN
);
4125 dlh
->index
= slh
->index
; /* little-endian */
4127 len
= min(snamlen
, DTLHDRDATALEN_LEGACY
);
4129 memcpy(dlh
, slh
, 6 + len
* 2);
4133 /* update dst head/only segment next field */
4137 sih
= (struct idtentry
*) s
;
4138 snamlen
= sih
->namlen
;
4140 len
= min(snamlen
, DTIHDRDATALEN
);
4141 dih
= (struct idtentry
*) h
;
4142 memcpy(dih
, sih
, 10 + len
* 2);
4149 /* free src head/only segment */
4159 * move additional segment(s) of the entry
4162 while ((ssi
= next
) >= 0) {
4163 /* is next slot contiguous ? */
4164 if (ssi
!= xssi
+ 1) {
4165 /* close current linelock */
4169 /* open new linelock */
4170 if (sdtlck
->index
< sdtlck
->maxcnt
)
4176 slv
= & sdtlck
->lv
[0];
4183 /* get next source segment */
4186 /* get next destination free slot */
4189 len
= min(snamlen
, DTSLOTDATALEN
);
4190 UniStrncpy_le(d
->name
, s
->name
, len
);
4199 /* free source segment */
4208 /* terminate dst last/only segment */
4210 /* single segment entry */
4211 if (dp
->header
.flag
& BT_LEAF
)
4216 /* multi-segment entry */
4220 /* close current linelock */
4229 /* update source header */
4230 sp
->header
.freelist
= sfsi
;
4231 sp
->header
.freecnt
+= nd
;
4233 /* update destination header */
4234 dp
->header
.nextindex
= di
;
4236 dp
->header
.freelist
= dsi
;
4237 dp
->header
.freecnt
-= nd
;
4244 * function: free a (leaf/internal) entry
4246 * log freelist header, stbl, and each segment slot of entry
4247 * (even though last/only segment next field is modified,
4248 * physical image logging requires all segment slots of
4249 * the entry logged to avoid applying previous updates
4250 * to the same slots)
4252 static void dtDeleteEntry(dtpage_t
* p
, int fi
, struct dt_lock
** dtlock
)
4254 int fsi
; /* free entry slot index */
4258 struct dt_lock
*dtlck
= *dtlock
;
4262 /* get free entry slot index */
4263 stbl
= DT_GETSTBL(p
);
4266 /* open new linelock */
4267 if (dtlck
->index
>= dtlck
->maxcnt
)
4268 dtlck
= (struct dt_lock
*) txLinelock(dtlck
);
4269 lv
= & dtlck
->lv
[dtlck
->index
];
4273 /* get the head/only segment */
4275 if (p
->header
.flag
& BT_LEAF
)
4276 si
= ((struct ldtentry
*) t
)->next
;
4278 si
= ((struct idtentry
*) t
)->next
;
4285 /* find the last/only segment */
4287 /* is next slot contiguous ? */
4288 if (si
!= xsi
+ 1) {
4289 /* close current linelock */
4293 /* open new linelock */
4294 if (dtlck
->index
< dtlck
->maxcnt
)
4297 dtlck
= (struct dt_lock
*) txLinelock(dtlck
);
4298 lv
= & dtlck
->lv
[0];
4314 /* close current linelock */
4320 /* update freelist */
4321 t
->next
= p
->header
.freelist
;
4322 p
->header
.freelist
= fsi
;
4323 p
->header
.freecnt
+= freecnt
;
4325 /* if delete from middle,
4326 * shift left the succedding entries in the stbl
4328 si
= p
->header
.nextindex
;
4330 memmove(&stbl
[fi
], &stbl
[fi
+ 1], si
- fi
- 1);
4332 p
->header
.nextindex
--;
4339 * function: truncate a (leaf/internal) entry
4341 * log freelist header, stbl, and each segment slot of entry
4342 * (even though last/only segment next field is modified,
4343 * physical image logging requires all segment slots of
4344 * the entry logged to avoid applying previous updates
4345 * to the same slots)
4347 static void dtTruncateEntry(dtpage_t
* p
, int ti
, struct dt_lock
** dtlock
)
4349 int tsi
; /* truncate entry slot index */
4353 struct dt_lock
*dtlck
= *dtlock
;
4357 /* get free entry slot index */
4358 stbl
= DT_GETSTBL(p
);
4361 /* open new linelock */
4362 if (dtlck
->index
>= dtlck
->maxcnt
)
4363 dtlck
= (struct dt_lock
*) txLinelock(dtlck
);
4364 lv
= & dtlck
->lv
[dtlck
->index
];
4368 /* get the head/only segment */
4370 ASSERT(p
->header
.flag
& BT_INTERNAL
);
4371 ((struct idtentry
*) t
)->namlen
= 0;
4372 si
= ((struct idtentry
*) t
)->next
;
4373 ((struct idtentry
*) t
)->next
= -1;
4380 /* find the last/only segment */
4382 /* is next slot contiguous ? */
4383 if (si
!= xsi
+ 1) {
4384 /* close current linelock */
4388 /* open new linelock */
4389 if (dtlck
->index
< dtlck
->maxcnt
)
4392 dtlck
= (struct dt_lock
*) txLinelock(dtlck
);
4393 lv
= & dtlck
->lv
[0];
4409 /* close current linelock */
4415 /* update freelist */
4418 t
->next
= p
->header
.freelist
;
4419 p
->header
.freelist
= fsi
;
4420 p
->header
.freecnt
+= freecnt
;
4425 * dtLinelockFreelist()
4427 static void dtLinelockFreelist(dtpage_t
* p
, /* directory page */
4428 int m
, /* max slot index */
4429 struct dt_lock
** dtlock
)
4431 int fsi
; /* free entry slot index */
4434 struct dt_lock
*dtlck
= *dtlock
;
4438 /* get free entry slot index */
4439 fsi
= p
->header
.freelist
;
4441 /* open new linelock */
4442 if (dtlck
->index
>= dtlck
->maxcnt
)
4443 dtlck
= (struct dt_lock
*) txLinelock(dtlck
);
4444 lv
= & dtlck
->lv
[dtlck
->index
];
4454 /* find the last/only segment */
4455 while (si
< m
&& si
>= 0) {
4456 /* is next slot contiguous ? */
4457 if (si
!= xsi
+ 1) {
4458 /* close current linelock */
4462 /* open new linelock */
4463 if (dtlck
->index
< dtlck
->maxcnt
)
4466 dtlck
= (struct dt_lock
*) txLinelock(dtlck
);
4467 lv
= & dtlck
->lv
[0];
4481 /* close current linelock */
4492 * FUNCTION: Modify the inode number part of a directory entry
4495 * tid - Transaction id
4496 * ip - Inode of parent directory
4497 * key - Name of entry to be modified
4498 * orig_ino - Original inode number expected in entry
4499 * new_ino - New inode number to put into entry
4503 * -ESTALE - If entry found does not match orig_ino passed in
4504 * -ENOENT - If no entry can be found to match key
4505 * 0 - If successfully modified entry
4507 int dtModify(tid_t tid
, struct inode
*ip
,
4508 struct component_name
* key
, ino_t
* orig_ino
, ino_t new_ino
, int flag
)
4512 struct metapage
*mp
;
4515 struct btstack btstack
;
4517 struct dt_lock
*dtlck
;
4520 int entry_si
; /* entry slot index */
4521 struct ldtentry
*entry
;
4524 * search for the entry to modify:
4526 * dtSearch() returns (leaf page pinned, index at which to modify).
4528 if ((rc
= dtSearch(ip
, key
, orig_ino
, &btstack
, flag
)))
4531 /* retrieve search result */
4532 DT_GETSEARCH(ip
, btstack
.top
, bn
, mp
, p
, index
);
4534 BT_MARK_DIRTY(mp
, ip
);
4536 * acquire a transaction lock on the leaf page of named entry
4538 tlck
= txLock(tid
, ip
, mp
, tlckDTREE
| tlckENTRY
);
4539 dtlck
= (struct dt_lock
*) & tlck
->lock
;
4541 /* get slot index of the entry */
4542 stbl
= DT_GETSTBL(p
);
4543 entry_si
= stbl
[index
];
4545 /* linelock entry */
4546 ASSERT(dtlck
->index
== 0);
4547 lv
= & dtlck
->lv
[0];
4548 lv
->offset
= entry_si
;
4552 /* get the head/only segment */
4553 entry
= (struct ldtentry
*) & p
->slot
[entry_si
];
4555 /* substitute the inode number of the entry */
4556 entry
->inumber
= cpu_to_le32(new_ino
);
4558 /* unpin the leaf page */