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
))
386 if (dbAlloc(ip
, 0, sbi
->nbperpage
, &xaddr
)) {
387 DQUOT_FREE_BLOCK(ip
, sbi
->nbperpage
);
392 * Save the table, we're going to overwrite it with the
395 memcpy(temp_table
, &jfs_ip
->i_dirtable
, sizeof(temp_table
));
398 * Initialize empty x-tree
403 * Add the first block to the xtree
405 if (xtInsert(tid
, ip
, 0, 0, sbi
->nbperpage
, &xaddr
, 0)) {
406 /* This really shouldn't fail */
407 jfs_warn("add_index: xtInsert failed!");
408 memcpy(&jfs_ip
->i_dirtable
, temp_table
,
409 sizeof (temp_table
));
410 dbFree(ip
, xaddr
, sbi
->nbperpage
);
411 DQUOT_FREE_BLOCK(ip
, sbi
->nbperpage
);
416 if ((mp
= get_index_page(ip
, 0)) == 0) {
417 jfs_err("add_index: get_metapage failed!");
418 xtTruncate(tid
, ip
, 0, COMMIT_PWMAP
);
419 memcpy(&jfs_ip
->i_dirtable
, temp_table
,
420 sizeof (temp_table
));
423 tlck
= txLock(tid
, ip
, mp
, tlckDATA
);
424 llck
= (struct linelock
*) & tlck
->lock
;
425 ASSERT(llck
->index
== 0);
429 lv
->length
= 6; /* tlckDATA slot size is 16 bytes */
432 memcpy(mp
->data
, temp_table
, sizeof(temp_table
));
434 mark_metapage_dirty(mp
);
435 release_metapage(mp
);
438 * Logging is now directed by xtree tlocks
440 clear_cflag(COMMIT_Dirtable
, ip
);
443 offset
= (index
- 2) * sizeof(struct dir_table_slot
);
444 page_offset
= offset
& (PSIZE
- 1);
445 blkno
= ((offset
+ 1) >> L2PSIZE
) << sbi
->l2nbperpage
;
446 if (page_offset
== 0) {
448 * This will be the beginning of a new page
451 if (xtInsert(tid
, ip
, 0, blkno
, sbi
->nbperpage
, &xaddr
, 0)) {
452 jfs_warn("add_index: xtInsert failed!");
457 if ((mp
= get_index_page(ip
, blkno
)))
458 memset(mp
->data
, 0, PSIZE
); /* Just looks better */
460 xtTruncate(tid
, ip
, offset
, COMMIT_PWMAP
);
462 mp
= read_index_page(ip
, blkno
);
465 jfs_err("add_index: get/read_metapage failed!");
469 lock_index(tid
, ip
, mp
, index
);
472 (struct dir_table_slot
*) ((char *) mp
->data
+ page_offset
);
473 dirtab_slot
->flag
= DIR_INDEX_VALID
;
474 dirtab_slot
->slot
= slot
;
475 DTSaddress(dirtab_slot
, bn
);
477 mark_metapage_dirty(mp
);
478 release_metapage(mp
);
484 jfs_ip
->next_index
--;
492 * Marks an entry to the directory index table as free.
494 static void free_index(tid_t tid
, struct inode
*ip
, u32 index
, u32 next
)
496 struct dir_table_slot
*dirtab_slot
;
498 struct metapage
*mp
= NULL
;
500 dirtab_slot
= find_index(ip
, index
, &mp
, &lblock
);
502 if (dirtab_slot
== 0)
505 dirtab_slot
->flag
= DIR_INDEX_FREE
;
506 dirtab_slot
->slot
= dirtab_slot
->addr1
= 0;
507 dirtab_slot
->addr2
= cpu_to_le32(next
);
510 lock_index(tid
, ip
, mp
, index
);
511 mark_metapage_dirty(mp
);
512 release_metapage(mp
);
514 set_cflag(COMMIT_Dirtable
, ip
);
520 * Changes an entry in the directory index table
522 static void modify_index(tid_t tid
, struct inode
*ip
, u32 index
, s64 bn
,
523 int slot
, struct metapage
** mp
, s64
*lblock
)
525 struct dir_table_slot
*dirtab_slot
;
527 dirtab_slot
= find_index(ip
, index
, mp
, lblock
);
529 if (dirtab_slot
== 0)
532 DTSaddress(dirtab_slot
, bn
);
533 dirtab_slot
->slot
= slot
;
536 lock_index(tid
, ip
, *mp
, index
);
537 mark_metapage_dirty(*mp
);
539 set_cflag(COMMIT_Dirtable
, ip
);
545 * reads a directory table slot
547 static int read_index(struct inode
*ip
, u32 index
,
548 struct dir_table_slot
* dirtab_slot
)
551 struct metapage
*mp
= NULL
;
552 struct dir_table_slot
*slot
;
554 slot
= find_index(ip
, index
, &mp
, &lblock
);
559 memcpy(dirtab_slot
, slot
, sizeof(struct dir_table_slot
));
562 release_metapage(mp
);
571 * Search for the entry with specified key
575 * return: 0 - search result on stack, leaf page pinned;
578 int dtSearch(struct inode
*ip
, struct component_name
* key
, ino_t
* data
,
579 struct btstack
* btstack
, int flag
)
582 int cmp
= 1; /* init for empty page */
587 int base
, index
, lim
;
588 struct btframe
*btsp
;
590 int psize
= 288; /* initial in-line directory */
592 struct component_name ciKey
;
593 struct super_block
*sb
= ip
->i_sb
;
595 ciKey
.name
= kmalloc((JFS_NAME_MAX
+ 1) * sizeof(wchar_t), GFP_NOFS
);
596 if (ciKey
.name
== 0) {
602 /* uppercase search key for c-i directory */
603 UniStrcpy(ciKey
.name
, key
->name
);
604 ciKey
.namlen
= key
->namlen
;
606 /* only uppercase if case-insensitive support is on */
607 if ((JFS_SBI(sb
)->mntflag
& JFS_OS2
) == JFS_OS2
) {
610 BT_CLR(btstack
); /* reset stack */
612 /* init level count for max pages to split */
616 * search down tree from root:
618 * between two consecutive entries of <Ki, Pi> and <Kj, Pj> of
619 * internal page, child page Pi contains entry with k, Ki <= K < Kj.
621 * if entry with search key K is not found
622 * internal page search find the entry with largest key Ki
623 * less than K which point to the child page to search;
624 * leaf page search find the entry with smallest key Kj
625 * greater than K so that the returned index is the position of
626 * the entry to be shifted right for insertion of new entry.
627 * for empty tree, search key is greater than any key of the tree.
629 * by convention, root bn = 0.
632 /* get/pin the page to search */
633 DT_GETPAGE(ip
, bn
, mp
, psize
, p
, rc
);
637 /* get sorted entry table of the page */
638 stbl
= DT_GETSTBL(p
);
641 * binary search with search key K on the current page.
643 for (base
= 0, lim
= p
->header
.nextindex
; lim
; lim
>>= 1) {
644 index
= base
+ (lim
>> 1);
646 if (p
->header
.flag
& BT_LEAF
) {
647 /* uppercase leaf name to compare */
649 ciCompare(&ciKey
, p
, stbl
[index
],
650 JFS_SBI(sb
)->mntflag
);
652 /* router key is in uppercase */
654 cmp
= dtCompare(&ciKey
, p
, stbl
[index
]);
662 /* search hit - leaf page:
663 * return the entry found
665 if (p
->header
.flag
& BT_LEAF
) {
666 inumber
= le32_to_cpu(
667 ((struct ldtentry
*) & p
->slot
[stbl
[index
]])->inumber
);
670 * search for JFS_LOOKUP
672 if (flag
== JFS_LOOKUP
) {
679 * search for JFS_CREATE
681 if (flag
== JFS_CREATE
) {
688 * search for JFS_REMOVE or JFS_RENAME
690 if ((flag
== JFS_REMOVE
||
691 flag
== JFS_RENAME
) &&
698 * JFS_REMOVE|JFS_FINDDIR|JFS_RENAME
700 /* save search result */
711 /* search hit - internal page:
712 * descend/search its child page
726 * base is the smallest index with key (Kj) greater than
727 * search key (K) and may be zero or (maxindex + 1) index.
730 * search miss - leaf page
732 * return location of entry (base) where new entry with
733 * search key K is to be inserted.
735 if (p
->header
.flag
& BT_LEAF
) {
737 * search for JFS_LOOKUP, JFS_REMOVE, or JFS_RENAME
739 if (flag
== JFS_LOOKUP
|| flag
== JFS_REMOVE
||
740 flag
== JFS_RENAME
) {
746 * search for JFS_CREATE|JFS_FINDDIR:
761 * search miss - internal page
763 * if base is non-zero, decrement base by one to get the parent
764 * entry of the child page to search.
766 index
= base
? base
- 1 : base
;
769 * go down to child page
772 /* update max. number of pages to split */
773 if (BT_STACK_FULL(btstack
)) {
774 /* Something's corrupted, mark filesystem dirty so
775 * chkdsk will fix it.
777 jfs_error(sb
, "stack overrun in dtSearch!");
778 BT_STACK_DUMP(btstack
);
784 /* push (bn, index) of the parent page/entry */
785 BT_PUSH(btstack
, bn
, index
);
787 /* get the child page block number */
788 pxd
= (pxd_t
*) & p
->slot
[stbl
[index
]];
789 bn
= addressPXD(pxd
);
790 psize
= lengthPXD(pxd
) << JFS_SBI(ip
->i_sb
)->l2bsize
;
792 /* unpin the parent page */
812 * function: insert an entry to directory tree
816 * return: 0 - success;
819 int dtInsert(tid_t tid
, struct inode
*ip
,
820 struct component_name
* name
, ino_t
* fsn
, struct btstack
* btstack
)
823 struct metapage
*mp
; /* meta-page buffer */
824 dtpage_t
*p
; /* base B+-tree index page */
827 struct dtsplit split
; /* split information */
829 struct dt_lock
*dtlck
;
835 * retrieve search result
837 * dtSearch() returns (leaf page pinned, index at which to insert).
838 * n.b. dtSearch() may return index of (maxindex + 1) of
841 DT_GETSEARCH(ip
, btstack
->top
, bn
, mp
, p
, index
);
844 * insert entry for new key
847 if (JFS_IP(ip
)->next_index
== DIREND
) {
851 n
= NDTLEAF(name
->namlen
);
855 n
= NDTLEAF_LEGACY(name
->namlen
);
856 data
.leaf
.ip
= NULL
; /* signifies legacy directory format */
858 data
.leaf
.ino
= *fsn
;
861 * leaf page does not have enough room for new entry:
863 * extend/split the leaf page;
865 * dtSplitUp() will insert the entry and unpin the leaf page.
867 if (n
> p
->header
.freecnt
) {
873 rc
= dtSplitUp(tid
, ip
, &split
, btstack
);
878 * leaf page does have enough room for new entry:
880 * insert the new data entry into the leaf page;
882 BT_MARK_DIRTY(mp
, ip
);
884 * acquire a transaction lock on the leaf page
886 tlck
= txLock(tid
, ip
, mp
, tlckDTREE
| tlckENTRY
);
887 dtlck
= (struct dt_lock
*) & tlck
->lock
;
888 ASSERT(dtlck
->index
== 0);
891 /* linelock header */
896 dtInsertEntry(p
, index
, name
, &data
, &dtlck
);
898 /* linelock stbl of non-root leaf page */
899 if (!(p
->header
.flag
& BT_ROOT
)) {
900 if (dtlck
->index
>= dtlck
->maxcnt
)
901 dtlck
= (struct dt_lock
*) txLinelock(dtlck
);
902 lv
= & dtlck
->lv
[dtlck
->index
];
903 n
= index
>> L2DTSLOTSIZE
;
904 lv
->offset
= p
->header
.stblindex
+ n
;
906 ((p
->header
.nextindex
- 1) >> L2DTSLOTSIZE
) - n
+ 1;
910 /* unpin the leaf page */
920 * function: propagate insertion bottom up;
924 * return: 0 - success;
926 * leaf page unpinned;
928 static int dtSplitUp(tid_t tid
,
929 struct inode
*ip
, struct dtsplit
* split
, struct btstack
* btstack
)
931 struct jfs_sb_info
*sbi
= JFS_SBI(ip
->i_sb
);
933 struct metapage
*smp
;
934 dtpage_t
*sp
; /* split page */
935 struct metapage
*rmp
;
936 dtpage_t
*rp
; /* new right page split from sp */
937 pxd_t rpxd
; /* new right page extent descriptor */
938 struct metapage
*lmp
;
939 dtpage_t
*lp
; /* left child page */
940 int skip
; /* index of entry of insertion */
941 struct btframe
*parent
; /* parent page entry on traverse stack */
944 struct pxdlist pxdlist
;
946 struct component_name key
= { 0, NULL
};
947 ddata_t
*data
= split
->data
;
949 struct dt_lock
*dtlck
;
952 int quota_allocation
= 0;
956 sp
= DT_PAGE(ip
, smp
);
958 key
.name
= kmalloc((JFS_NAME_MAX
+ 2) * sizeof(wchar_t), GFP_NOFS
);
968 * The split routines insert the new entry, and
969 * acquire txLock as appropriate.
972 * split root leaf page:
974 if (sp
->header
.flag
& BT_ROOT
) {
976 * allocate a single extent child page
979 n
= sbi
->bsize
>> L2DTSLOTSIZE
;
980 n
-= (n
+ 31) >> L2DTSLOTSIZE
; /* stbl size */
981 n
-= DTROOTMAXSLOT
- sp
->header
.freecnt
; /* header + entries */
982 if (n
<= split
->nslot
)
984 if ((rc
= dbAlloc(ip
, 0, (s64
) xlen
, &xaddr
))) {
991 pxd
= &pxdlist
.pxd
[0];
992 PXDaddress(pxd
, xaddr
);
993 PXDlength(pxd
, xlen
);
994 split
->pxdlist
= &pxdlist
;
995 rc
= dtSplitRoot(tid
, ip
, split
, &rmp
);
998 dbFree(ip
, xaddr
, xlen
);
1005 ip
->i_size
= xlen
<< sbi
->l2bsize
;
1011 * extend first leaf page
1013 * extend the 1st extent if less than buffer page size
1014 * (dtExtendPage() reurns leaf page unpinned)
1016 pxd
= &sp
->header
.self
;
1017 xlen
= lengthPXD(pxd
);
1018 xsize
= xlen
<< sbi
->l2bsize
;
1019 if (xsize
< PSIZE
) {
1020 xaddr
= addressPXD(pxd
);
1021 n
= xsize
>> L2DTSLOTSIZE
;
1022 n
-= (n
+ 31) >> L2DTSLOTSIZE
; /* stbl size */
1023 if ((n
+ sp
->header
.freecnt
) <= split
->nslot
)
1024 n
= xlen
+ (xlen
<< 1);
1028 /* Allocate blocks to quota. */
1029 if (DQUOT_ALLOC_BLOCK(ip
, n
)) {
1033 quota_allocation
+= n
;
1035 if ((rc
= dbReAlloc(sbi
->ipbmap
, xaddr
, (s64
) xlen
,
1039 pxdlist
.maxnpxd
= 1;
1041 pxd
= &pxdlist
.pxd
[0];
1042 PXDaddress(pxd
, nxaddr
)
1043 PXDlength(pxd
, xlen
+ n
);
1044 split
->pxdlist
= &pxdlist
;
1045 if ((rc
= dtExtendPage(tid
, ip
, split
, btstack
))) {
1046 nxaddr
= addressPXD(pxd
);
1047 if (xaddr
!= nxaddr
) {
1048 /* free relocated extent */
1049 xlen
= lengthPXD(pxd
);
1050 dbFree(ip
, nxaddr
, (s64
) xlen
);
1052 /* free extended delta */
1053 xlen
= lengthPXD(pxd
) - n
;
1054 xaddr
= addressPXD(pxd
) + xlen
;
1055 dbFree(ip
, xaddr
, (s64
) n
);
1057 } else if (!DO_INDEX(ip
))
1058 ip
->i_size
= lengthPXD(pxd
) << sbi
->l2bsize
;
1067 * split leaf page <sp> into <sp> and a new right page <rp>.
1069 * return <rp> pinned and its extent descriptor <rpxd>
1072 * allocate new directory page extent and
1073 * new index page(s) to cover page split(s)
1075 * allocation hint: ?
1077 n
= btstack
->nsplit
;
1078 pxdlist
.maxnpxd
= pxdlist
.npxd
= 0;
1079 xlen
= sbi
->nbperpage
;
1080 for (pxd
= pxdlist
.pxd
; n
> 0; n
--, pxd
++) {
1081 if ((rc
= dbAlloc(ip
, 0, (s64
) xlen
, &xaddr
)) == 0) {
1082 PXDaddress(pxd
, xaddr
);
1083 PXDlength(pxd
, xlen
);
1090 /* undo allocation */
1094 split
->pxdlist
= &pxdlist
;
1095 if ((rc
= dtSplitPage(tid
, ip
, split
, &rmp
, &rp
, &rpxd
))) {
1098 /* undo allocation */
1103 ip
->i_size
+= PSIZE
;
1106 * propagate up the router entry for the leaf page just split
1108 * insert a router entry for the new page into the parent page,
1109 * propagate the insert/split up the tree by walking back the stack
1110 * of (bn of parent page, index of child page entry in parent page)
1111 * that were traversed during the search for the page that split.
1113 * the propagation of insert/split up the tree stops if the root
1114 * splits or the page inserted into doesn't have to split to hold
1117 * the parent entry for the split page remains the same, and
1118 * a new entry is inserted at its right with the first key and
1119 * block number of the new right page.
1121 * There are a maximum of 4 pages pinned at any time:
1122 * two children, left parent and right parent (when the parent splits).
1123 * keep the child pages pinned while working on the parent.
1124 * make sure that all pins are released at exit.
1126 while ((parent
= BT_POP(btstack
)) != NULL
) {
1127 /* parent page specified by stack frame <parent> */
1129 /* keep current child pages (<lp>, <rp>) pinned */
1134 * insert router entry in parent for new right child page <rp>
1136 /* get the parent page <sp> */
1137 DT_GETPAGE(ip
, parent
->bn
, smp
, PSIZE
, sp
, rc
);
1145 * The new key entry goes ONE AFTER the index of parent entry,
1146 * because the split was to the right.
1148 skip
= parent
->index
+ 1;
1151 * compute the key for the router entry
1153 * key suffix compression:
1154 * for internal pages that have leaf pages as children,
1155 * retain only what's needed to distinguish between
1156 * the new entry and the entry on the page to its left.
1157 * If the keys compare equal, retain the entire key.
1159 * note that compression is performed only at computing
1160 * router key at the lowest internal level.
1161 * further compression of the key between pairs of higher
1162 * level internal pages loses too much information and
1163 * the search may fail.
1164 * (e.g., two adjacent leaf pages of {a, ..., x} {xx, ...,}
1165 * results in two adjacent parent entries (a)(xx).
1166 * if split occurs between these two entries, and
1167 * if compression is applied, the router key of parent entry
1168 * of right page (x) will divert search for x into right
1169 * subtree and miss x in the left subtree.)
1171 * the entire key must be retained for the next-to-leftmost
1172 * internal key at any level of the tree, or search may fail
1175 switch (rp
->header
.flag
& BT_TYPE
) {
1178 * compute the length of prefix for suffix compression
1179 * between last entry of left page and first entry
1182 if ((sp
->header
.flag
& BT_ROOT
&& skip
> 1) ||
1183 sp
->header
.prev
!= 0 || skip
> 1) {
1184 /* compute uppercase router prefix key */
1185 rc
= ciGetLeafPrefixKey(lp
,
1186 lp
->header
.nextindex
-1,
1196 /* next to leftmost entry of
1197 lowest internal level */
1199 /* compute uppercase router key */
1200 dtGetKey(rp
, 0, &key
, sbi
->mntflag
);
1201 key
.name
[key
.namlen
] = 0;
1203 if ((sbi
->mntflag
& JFS_OS2
) == JFS_OS2
)
1207 n
= NDTINTERNAL(key
.namlen
);
1211 dtGetKey(rp
, 0, &key
, sbi
->mntflag
);
1212 n
= NDTINTERNAL(key
.namlen
);
1216 jfs_err("dtSplitUp(): UFO!");
1220 /* unpin left child page */
1224 * compute the data for the router entry
1226 data
->xd
= rpxd
; /* child page xd */
1229 * parent page is full - split the parent page
1231 if (n
> sp
->header
.freecnt
) {
1232 /* init for parent page split */
1234 split
->index
= skip
; /* index at insert */
1237 /* split->data = data; */
1239 /* unpin right child page */
1242 /* The split routines insert the new entry,
1243 * acquire txLock as appropriate.
1244 * return <rp> pinned and its block number <rbn>.
1246 rc
= (sp
->header
.flag
& BT_ROOT
) ?
1247 dtSplitRoot(tid
, ip
, split
, &rmp
) :
1248 dtSplitPage(tid
, ip
, split
, &rmp
, &rp
, &rpxd
);
1254 /* smp and rmp are pinned */
1257 * parent page is not full - insert router entry in parent page
1260 BT_MARK_DIRTY(smp
, ip
);
1262 * acquire a transaction lock on the parent page
1264 tlck
= txLock(tid
, ip
, smp
, tlckDTREE
| tlckENTRY
);
1265 dtlck
= (struct dt_lock
*) & tlck
->lock
;
1266 ASSERT(dtlck
->index
== 0);
1267 lv
= & dtlck
->lv
[0];
1269 /* linelock header */
1274 /* linelock stbl of non-root parent page */
1275 if (!(sp
->header
.flag
& BT_ROOT
)) {
1277 n
= skip
>> L2DTSLOTSIZE
;
1278 lv
->offset
= sp
->header
.stblindex
+ n
;
1280 ((sp
->header
.nextindex
-
1281 1) >> L2DTSLOTSIZE
) - n
+ 1;
1285 dtInsertEntry(sp
, skip
, &key
, data
, &dtlck
);
1287 /* exit propagate up */
1292 /* unpin current split and its right page */
1297 * free remaining extents allocated for split
1301 pxd
= &pxdlist
.pxd
[n
];
1302 for (; n
< pxdlist
.maxnpxd
; n
++, pxd
++)
1303 dbFree(ip
, addressPXD(pxd
), (s64
) lengthPXD(pxd
));
1308 /* Rollback quota allocation */
1309 if (rc
&& quota_allocation
)
1310 DQUOT_FREE_BLOCK(ip
, quota_allocation
);
1321 * function: Split a non-root page of a btree.
1325 * return: 0 - success;
1327 * return split and new page pinned;
1329 static int dtSplitPage(tid_t tid
, struct inode
*ip
, struct dtsplit
* split
,
1330 struct metapage
** rmpp
, dtpage_t
** rpp
, pxd_t
* rpxdp
)
1333 struct metapage
*smp
;
1335 struct metapage
*rmp
;
1336 dtpage_t
*rp
; /* new right page allocated */
1337 s64 rbn
; /* new right page block number */
1338 struct metapage
*mp
;
1341 struct pxdlist
*pxdlist
;
1343 int skip
, nextindex
, half
, left
, nxt
, off
, si
;
1344 struct ldtentry
*ldtentry
;
1345 struct idtentry
*idtentry
;
1350 struct dt_lock
*sdtlck
, *rdtlck
;
1352 struct dt_lock
*dtlck
;
1353 struct lv
*slv
, *rlv
, *lv
;
1355 /* get split page */
1357 sp
= DT_PAGE(ip
, smp
);
1360 * allocate the new right page for the split
1362 pxdlist
= split
->pxdlist
;
1363 pxd
= &pxdlist
->pxd
[pxdlist
->npxd
];
1365 rbn
= addressPXD(pxd
);
1366 rmp
= get_metapage(ip
, rbn
, PSIZE
, 1);
1370 /* Allocate blocks to quota. */
1371 if (DQUOT_ALLOC_BLOCK(ip
, lengthPXD(pxd
))) {
1372 release_metapage(rmp
);
1376 jfs_info("dtSplitPage: ip:0x%p smp:0x%p rmp:0x%p", ip
, smp
, rmp
);
1378 BT_MARK_DIRTY(rmp
, ip
);
1380 * acquire a transaction lock on the new right page
1382 tlck
= txLock(tid
, ip
, rmp
, tlckDTREE
| tlckNEW
);
1383 rdtlck
= (struct dt_lock
*) & tlck
->lock
;
1385 rp
= (dtpage_t
*) rmp
->data
;
1387 rp
->header
.self
= *pxd
;
1389 BT_MARK_DIRTY(smp
, ip
);
1391 * acquire a transaction lock on the split page
1395 tlck
= txLock(tid
, ip
, smp
, tlckDTREE
| tlckENTRY
);
1396 sdtlck
= (struct dt_lock
*) & tlck
->lock
;
1398 /* linelock header of split page */
1399 ASSERT(sdtlck
->index
== 0);
1400 slv
= & sdtlck
->lv
[0];
1406 * initialize/update sibling pointers between sp and rp
1408 nextbn
= le64_to_cpu(sp
->header
.next
);
1409 rp
->header
.next
= cpu_to_le64(nextbn
);
1410 rp
->header
.prev
= cpu_to_le64(addressPXD(&sp
->header
.self
));
1411 sp
->header
.next
= cpu_to_le64(rbn
);
1414 * initialize new right page
1416 rp
->header
.flag
= sp
->header
.flag
;
1418 /* compute sorted entry table at start of extent data area */
1419 rp
->header
.nextindex
= 0;
1420 rp
->header
.stblindex
= 1;
1422 n
= PSIZE
>> L2DTSLOTSIZE
;
1423 rp
->header
.maxslot
= n
;
1424 stblsize
= (n
+ 31) >> L2DTSLOTSIZE
; /* in unit of slot */
1427 fsi
= rp
->header
.stblindex
+ stblsize
;
1428 rp
->header
.freelist
= fsi
;
1429 rp
->header
.freecnt
= rp
->header
.maxslot
- fsi
;
1432 * sequential append at tail: append without split
1434 * If splitting the last page on a level because of appending
1435 * a entry to it (skip is maxentry), it's likely that the access is
1436 * sequential. Adding an empty page on the side of the level is less
1437 * work and can push the fill factor much higher than normal.
1438 * If we're wrong it's no big deal, we'll just do the split the right
1440 * (It may look like it's equally easy to do a similar hack for
1441 * reverse sorted data, that is, split the tree left,
1442 * but it's not. Be my guest.)
1444 if (nextbn
== 0 && split
->index
== sp
->header
.nextindex
) {
1445 /* linelock header + stbl (first slot) of new page */
1446 rlv
= & rdtlck
->lv
[rdtlck
->index
];
1452 * initialize freelist of new right page
1455 for (fsi
++; fsi
< rp
->header
.maxslot
; f
++, fsi
++)
1459 /* insert entry at the first entry of the new right page */
1460 dtInsertEntry(rp
, 0, split
->key
, split
->data
, &rdtlck
);
1466 * non-sequential insert (at possibly middle page)
1470 * update prev pointer of previous right sibling page;
1473 DT_GETPAGE(ip
, nextbn
, mp
, PSIZE
, p
, rc
);
1475 discard_metapage(rmp
);
1479 BT_MARK_DIRTY(mp
, ip
);
1481 * acquire a transaction lock on the next page
1483 tlck
= txLock(tid
, ip
, mp
, tlckDTREE
| tlckRELINK
);
1484 jfs_info("dtSplitPage: tlck = 0x%p, ip = 0x%p, mp=0x%p",
1486 dtlck
= (struct dt_lock
*) & tlck
->lock
;
1488 /* linelock header of previous right sibling page */
1489 lv
= & dtlck
->lv
[dtlck
->index
];
1494 p
->header
.prev
= cpu_to_le64(rbn
);
1500 * split the data between the split and right pages.
1502 skip
= split
->index
;
1503 half
= (PSIZE
>> L2DTSLOTSIZE
) >> 1; /* swag */
1507 * compute fill factor for split pages
1509 * <nxt> traces the next entry to move to rp
1510 * <off> traces the next entry to stay in sp
1512 stbl
= (u8
*) & sp
->slot
[sp
->header
.stblindex
];
1513 nextindex
= sp
->header
.nextindex
;
1514 for (nxt
= off
= 0; nxt
< nextindex
; ++off
) {
1516 /* check for fill factor with new entry size */
1520 switch (sp
->header
.flag
& BT_TYPE
) {
1522 ldtentry
= (struct ldtentry
*) & sp
->slot
[si
];
1524 n
= NDTLEAF(ldtentry
->namlen
);
1526 n
= NDTLEAF_LEGACY(ldtentry
->
1531 idtentry
= (struct idtentry
*) & sp
->slot
[si
];
1532 n
= NDTINTERNAL(idtentry
->namlen
);
1539 ++nxt
; /* advance to next entry to move in sp */
1547 /* <nxt> poins to the 1st entry to move */
1550 * move entries to right page
1552 * dtMoveEntry() initializes rp and reserves entry for insertion
1554 * split page moved out entries are linelocked;
1555 * new/right page moved in entries are linelocked;
1557 /* linelock header + stbl of new right page */
1558 rlv
= & rdtlck
->lv
[rdtlck
->index
];
1563 dtMoveEntry(sp
, nxt
, rp
, &sdtlck
, &rdtlck
, DO_INDEX(ip
));
1565 sp
->header
.nextindex
= nxt
;
1568 * finalize freelist of new right page
1570 fsi
= rp
->header
.freelist
;
1572 for (fsi
++; fsi
< rp
->header
.maxslot
; f
++, fsi
++)
1577 * Update directory index table for entries now in right page
1579 if ((rp
->header
.flag
& BT_LEAF
) && DO_INDEX(ip
)) {
1583 stbl
= DT_GETSTBL(rp
);
1584 for (n
= 0; n
< rp
->header
.nextindex
; n
++) {
1585 ldtentry
= (struct ldtentry
*) & rp
->slot
[stbl
[n
]];
1586 modify_index(tid
, ip
, le32_to_cpu(ldtentry
->index
),
1587 rbn
, n
, &mp
, &lblock
);
1590 release_metapage(mp
);
1594 * the skipped index was on the left page,
1597 /* insert the new entry in the split page */
1598 dtInsertEntry(sp
, skip
, split
->key
, split
->data
, &sdtlck
);
1600 /* linelock stbl of split page */
1601 if (sdtlck
->index
>= sdtlck
->maxcnt
)
1602 sdtlck
= (struct dt_lock
*) txLinelock(sdtlck
);
1603 slv
= & sdtlck
->lv
[sdtlck
->index
];
1604 n
= skip
>> L2DTSLOTSIZE
;
1605 slv
->offset
= sp
->header
.stblindex
+ n
;
1607 ((sp
->header
.nextindex
- 1) >> L2DTSLOTSIZE
) - n
+ 1;
1611 * the skipped index was on the right page,
1614 /* adjust the skip index to reflect the new position */
1617 /* insert the new entry in the right page */
1618 dtInsertEntry(rp
, skip
, split
->key
, split
->data
, &rdtlck
);
1632 * function: extend 1st/only directory leaf page
1636 * return: 0 - success;
1638 * return extended page pinned;
1640 static int dtExtendPage(tid_t tid
,
1641 struct inode
*ip
, struct dtsplit
* split
, struct btstack
* btstack
)
1643 struct super_block
*sb
= ip
->i_sb
;
1645 struct metapage
*smp
, *pmp
, *mp
;
1647 struct pxdlist
*pxdlist
;
1650 int newstblindex
, newstblsize
;
1651 int oldstblindex
, oldstblsize
;
1654 struct btframe
*parent
;
1656 struct dt_lock
*dtlck
;
1659 struct pxd_lock
*pxdlock
;
1662 struct ldtentry
*ldtentry
;
1665 /* get page to extend */
1667 sp
= DT_PAGE(ip
, smp
);
1669 /* get parent/root page */
1670 parent
= BT_POP(btstack
);
1671 DT_GETPAGE(ip
, parent
->bn
, pmp
, PSIZE
, pp
, rc
);
1678 pxdlist
= split
->pxdlist
;
1679 pxd
= &pxdlist
->pxd
[pxdlist
->npxd
];
1682 xaddr
= addressPXD(pxd
);
1683 tpxd
= &sp
->header
.self
;
1684 txaddr
= addressPXD(tpxd
);
1685 /* in-place extension */
1686 if (xaddr
== txaddr
) {
1693 /* save moved extent descriptor for later free */
1694 tlck
= txMaplock(tid
, ip
, tlckDTREE
| tlckRELOCATE
);
1695 pxdlock
= (struct pxd_lock
*) & tlck
->lock
;
1696 pxdlock
->flag
= mlckFREEPXD
;
1697 pxdlock
->pxd
= sp
->header
.self
;
1701 * Update directory index table to reflect new page address
1707 stbl
= DT_GETSTBL(sp
);
1708 for (n
= 0; n
< sp
->header
.nextindex
; n
++) {
1710 (struct ldtentry
*) & sp
->slot
[stbl
[n
]];
1711 modify_index(tid
, ip
,
1712 le32_to_cpu(ldtentry
->index
),
1713 xaddr
, n
, &mp
, &lblock
);
1716 release_metapage(mp
);
1723 sp
->header
.self
= *pxd
;
1725 jfs_info("dtExtendPage: ip:0x%p smp:0x%p sp:0x%p", ip
, smp
, sp
);
1727 BT_MARK_DIRTY(smp
, ip
);
1729 * acquire a transaction lock on the extended/leaf page
1731 tlck
= txLock(tid
, ip
, smp
, tlckDTREE
| type
);
1732 dtlck
= (struct dt_lock
*) & tlck
->lock
;
1733 lv
= & dtlck
->lv
[0];
1735 /* update buffer extent descriptor of extended page */
1736 xlen
= lengthPXD(pxd
);
1737 xsize
= xlen
<< JFS_SBI(sb
)->l2bsize
;
1740 * copy old stbl to new stbl at start of extended area
1742 oldstblindex
= sp
->header
.stblindex
;
1743 oldstblsize
= (sp
->header
.maxslot
+ 31) >> L2DTSLOTSIZE
;
1744 newstblindex
= sp
->header
.maxslot
;
1745 n
= xsize
>> L2DTSLOTSIZE
;
1746 newstblsize
= (n
+ 31) >> L2DTSLOTSIZE
;
1747 memcpy(&sp
->slot
[newstblindex
], &sp
->slot
[oldstblindex
],
1748 sp
->header
.nextindex
);
1751 * in-line extension: linelock old area of extended page
1753 if (type
== tlckEXTEND
) {
1754 /* linelock header */
1760 /* linelock new stbl of extended page */
1761 lv
->offset
= newstblindex
;
1762 lv
->length
= newstblsize
;
1765 * relocation: linelock whole relocated area
1769 lv
->length
= sp
->header
.maxslot
+ newstblsize
;
1774 sp
->header
.maxslot
= n
;
1775 sp
->header
.stblindex
= newstblindex
;
1776 /* sp->header.nextindex remains the same */
1779 * add old stbl region at head of freelist
1783 last
= sp
->header
.freelist
;
1784 for (n
= 0; n
< oldstblsize
; n
++, fsi
++, f
++) {
1788 sp
->header
.freelist
= last
;
1789 sp
->header
.freecnt
+= oldstblsize
;
1792 * append free region of newly extended area at tail of freelist
1794 /* init free region of newly extended area */
1795 fsi
= n
= newstblindex
+ newstblsize
;
1797 for (fsi
++; fsi
< sp
->header
.maxslot
; f
++, fsi
++)
1801 /* append new free region at tail of old freelist */
1802 fsi
= sp
->header
.freelist
;
1804 sp
->header
.freelist
= n
;
1809 } while (fsi
!= -1);
1814 sp
->header
.freecnt
+= sp
->header
.maxslot
- n
;
1817 * insert the new entry
1819 dtInsertEntry(sp
, split
->index
, split
->key
, split
->data
, &dtlck
);
1821 BT_MARK_DIRTY(pmp
, ip
);
1823 * linelock any freeslots residing in old extent
1825 if (type
== tlckEXTEND
) {
1826 n
= sp
->header
.maxslot
>> 2;
1827 if (sp
->header
.freelist
< n
)
1828 dtLinelockFreelist(sp
, n
, &dtlck
);
1832 * update parent entry on the parent/root page
1835 * acquire a transaction lock on the parent/root page
1837 tlck
= txLock(tid
, ip
, pmp
, tlckDTREE
| tlckENTRY
);
1838 dtlck
= (struct dt_lock
*) & tlck
->lock
;
1839 lv
= & dtlck
->lv
[dtlck
->index
];
1841 /* linelock parent entry - 1st slot */
1846 /* update the parent pxd for page extension */
1847 tpxd
= (pxd_t
*) & pp
->slot
[1];
1859 * split the full root page into
1860 * original/root/split page and new right page
1861 * i.e., root remains fixed in tree anchor (inode) and
1862 * the root is copied to a single new right child page
1863 * since root page << non-root page, and
1864 * the split root page contains a single entry for the
1865 * new right child page.
1869 * return: 0 - success;
1871 * return new page pinned;
1873 static int dtSplitRoot(tid_t tid
,
1874 struct inode
*ip
, struct dtsplit
* split
, struct metapage
** rmpp
)
1876 struct super_block
*sb
= ip
->i_sb
;
1877 struct metapage
*smp
;
1879 struct metapage
*rmp
;
1886 int fsi
, stblsize
, n
;
1889 struct pxdlist
*pxdlist
;
1891 struct dt_lock
*dtlck
;
1895 /* get split root page */
1897 sp
= &JFS_IP(ip
)->i_dtroot
;
1900 * allocate/initialize a single (right) child page
1902 * N.B. at first split, a one (or two) block to fit new entry
1903 * is allocated; at subsequent split, a full page is allocated;
1905 pxdlist
= split
->pxdlist
;
1906 pxd
= &pxdlist
->pxd
[pxdlist
->npxd
];
1908 rbn
= addressPXD(pxd
);
1909 xlen
= lengthPXD(pxd
);
1910 xsize
= xlen
<< JFS_SBI(sb
)->l2bsize
;
1911 rmp
= get_metapage(ip
, rbn
, xsize
, 1);
1917 /* Allocate blocks to quota. */
1918 if (DQUOT_ALLOC_BLOCK(ip
, lengthPXD(pxd
))) {
1919 release_metapage(rmp
);
1923 BT_MARK_DIRTY(rmp
, ip
);
1925 * acquire a transaction lock on the new right page
1927 tlck
= txLock(tid
, ip
, rmp
, tlckDTREE
| tlckNEW
);
1928 dtlck
= (struct dt_lock
*) & tlck
->lock
;
1931 (sp
->header
.flag
& BT_LEAF
) ? BT_LEAF
: BT_INTERNAL
;
1932 rp
->header
.self
= *pxd
;
1934 /* initialize sibling pointers */
1935 rp
->header
.next
= 0;
1936 rp
->header
.prev
= 0;
1939 * move in-line root page into new right page extent
1941 /* linelock header + copied entries + new stbl (1st slot) in new page */
1942 ASSERT(dtlck
->index
== 0);
1943 lv
= & dtlck
->lv
[0];
1945 lv
->length
= 10; /* 1 + 8 + 1 */
1948 n
= xsize
>> L2DTSLOTSIZE
;
1949 rp
->header
.maxslot
= n
;
1950 stblsize
= (n
+ 31) >> L2DTSLOTSIZE
;
1952 /* copy old stbl to new stbl at start of extended area */
1953 rp
->header
.stblindex
= DTROOTMAXSLOT
;
1954 stbl
= (s8
*) & rp
->slot
[DTROOTMAXSLOT
];
1955 memcpy(stbl
, sp
->header
.stbl
, sp
->header
.nextindex
);
1956 rp
->header
.nextindex
= sp
->header
.nextindex
;
1958 /* copy old data area to start of new data area */
1959 memcpy(&rp
->slot
[1], &sp
->slot
[1], IDATASIZE
);
1962 * append free region of newly extended area at tail of freelist
1964 /* init free region of newly extended area */
1965 fsi
= n
= DTROOTMAXSLOT
+ stblsize
;
1967 for (fsi
++; fsi
< rp
->header
.maxslot
; f
++, fsi
++)
1971 /* append new free region at tail of old freelist */
1972 fsi
= sp
->header
.freelist
;
1974 rp
->header
.freelist
= n
;
1976 rp
->header
.freelist
= fsi
;
1981 } while (fsi
!= -1);
1986 rp
->header
.freecnt
= sp
->header
.freecnt
+ rp
->header
.maxslot
- n
;
1989 * Update directory index table for entries now in right page
1991 if ((rp
->header
.flag
& BT_LEAF
) && DO_INDEX(ip
)) {
1993 struct metapage
*mp
= NULL
;
1994 struct ldtentry
*ldtentry
;
1996 stbl
= DT_GETSTBL(rp
);
1997 for (n
= 0; n
< rp
->header
.nextindex
; n
++) {
1998 ldtentry
= (struct ldtentry
*) & rp
->slot
[stbl
[n
]];
1999 modify_index(tid
, ip
, le32_to_cpu(ldtentry
->index
),
2000 rbn
, n
, &mp
, &lblock
);
2003 release_metapage(mp
);
2006 * insert the new entry into the new right/child page
2007 * (skip index in the new right page will not change)
2009 dtInsertEntry(rp
, split
->index
, split
->key
, split
->data
, &dtlck
);
2012 * reset parent/root page
2014 * set the 1st entry offset to 0, which force the left-most key
2015 * at any level of the tree to be less than any search key.
2017 * The btree comparison code guarantees that the left-most key on any
2018 * level of the tree is never used, so it doesn't need to be filled in.
2020 BT_MARK_DIRTY(smp
, ip
);
2022 * acquire a transaction lock on the root page (in-memory inode)
2024 tlck
= txLock(tid
, ip
, smp
, tlckDTREE
| tlckNEW
| tlckBTROOT
);
2025 dtlck
= (struct dt_lock
*) & tlck
->lock
;
2028 ASSERT(dtlck
->index
== 0);
2029 lv
= & dtlck
->lv
[0];
2031 lv
->length
= DTROOTMAXSLOT
;
2034 /* update page header of root */
2035 if (sp
->header
.flag
& BT_LEAF
) {
2036 sp
->header
.flag
&= ~BT_LEAF
;
2037 sp
->header
.flag
|= BT_INTERNAL
;
2040 /* init the first entry */
2041 s
= (struct idtentry
*) & sp
->slot
[DTENTRYSTART
];
2047 stbl
= sp
->header
.stbl
;
2048 stbl
[0] = DTENTRYSTART
;
2049 sp
->header
.nextindex
= 1;
2052 fsi
= DTENTRYSTART
+ 1;
2055 /* init free region of remaining area */
2056 for (fsi
++; fsi
< DTROOTMAXSLOT
; f
++, fsi
++)
2060 sp
->header
.freelist
= DTENTRYSTART
+ 1;
2061 sp
->header
.freecnt
= DTROOTMAXSLOT
- (DTENTRYSTART
+ 1);
2072 * function: delete the entry(s) referenced by a key.
2078 int dtDelete(tid_t tid
,
2079 struct inode
*ip
, struct component_name
* key
, ino_t
* ino
, int flag
)
2083 struct metapage
*mp
, *imp
;
2086 struct btstack btstack
;
2087 struct dt_lock
*dtlck
;
2091 struct ldtentry
*ldtentry
;
2093 u32 table_index
, next_index
;
2094 struct metapage
*nmp
;
2098 * search for the entry to delete:
2100 * dtSearch() returns (leaf page pinned, index at which to delete).
2102 if ((rc
= dtSearch(ip
, key
, ino
, &btstack
, flag
)))
2105 /* retrieve search result */
2106 DT_GETSEARCH(ip
, btstack
.top
, bn
, mp
, p
, index
);
2109 * We need to find put the index of the next entry into the
2110 * directory index table in order to resume a readdir from this
2114 stbl
= DT_GETSTBL(p
);
2115 ldtentry
= (struct ldtentry
*) & p
->slot
[stbl
[index
]];
2116 table_index
= le32_to_cpu(ldtentry
->index
);
2117 if (index
== (p
->header
.nextindex
- 1)) {
2119 * Last entry in this leaf page
2121 if ((p
->header
.flag
& BT_ROOT
)
2122 || (p
->header
.next
== 0))
2125 /* Read next leaf page */
2126 DT_GETPAGE(ip
, le64_to_cpu(p
->header
.next
),
2127 nmp
, PSIZE
, np
, rc
);
2131 stbl
= DT_GETSTBL(np
);
2133 (struct ldtentry
*) & np
->
2136 le32_to_cpu(ldtentry
->index
);
2142 (struct ldtentry
*) & p
->slot
[stbl
[index
+ 1]];
2143 next_index
= le32_to_cpu(ldtentry
->index
);
2145 free_index(tid
, ip
, table_index
, next_index
);
2148 * the leaf page becomes empty, delete the page
2150 if (p
->header
.nextindex
== 1) {
2151 /* delete empty page */
2152 rc
= dtDeleteUp(tid
, ip
, mp
, p
, &btstack
);
2155 * the leaf page has other entries remaining:
2157 * delete the entry from the leaf page.
2160 BT_MARK_DIRTY(mp
, ip
);
2162 * acquire a transaction lock on the leaf page
2164 tlck
= txLock(tid
, ip
, mp
, tlckDTREE
| tlckENTRY
);
2165 dtlck
= (struct dt_lock
*) & tlck
->lock
;
2168 * Do not assume that dtlck->index will be zero. During a
2169 * rename within a directory, this transaction may have
2170 * modified this page already when adding the new entry.
2173 /* linelock header */
2174 if (dtlck
->index
>= dtlck
->maxcnt
)
2175 dtlck
= (struct dt_lock
*) txLinelock(dtlck
);
2176 lv
= & dtlck
->lv
[dtlck
->index
];
2181 /* linelock stbl of non-root leaf page */
2182 if (!(p
->header
.flag
& BT_ROOT
)) {
2183 if (dtlck
->index
>= dtlck
->maxcnt
)
2184 dtlck
= (struct dt_lock
*) txLinelock(dtlck
);
2185 lv
= & dtlck
->lv
[dtlck
->index
];
2186 i
= index
>> L2DTSLOTSIZE
;
2187 lv
->offset
= p
->header
.stblindex
+ i
;
2189 ((p
->header
.nextindex
- 1) >> L2DTSLOTSIZE
) -
2194 /* free the leaf entry */
2195 dtDeleteEntry(p
, index
, &dtlck
);
2198 * Update directory index table for entries moved in stbl
2200 if (DO_INDEX(ip
) && index
< p
->header
.nextindex
) {
2204 stbl
= DT_GETSTBL(p
);
2205 for (i
= index
; i
< p
->header
.nextindex
; i
++) {
2207 (struct ldtentry
*) & p
->slot
[stbl
[i
]];
2208 modify_index(tid
, ip
,
2209 le32_to_cpu(ldtentry
->index
),
2210 bn
, i
, &imp
, &lblock
);
2213 release_metapage(imp
);
2227 * free empty pages as propagating deletion up the tree
2233 static int dtDeleteUp(tid_t tid
, struct inode
*ip
,
2234 struct metapage
* fmp
, dtpage_t
* fp
, struct btstack
* btstack
)
2237 struct metapage
*mp
;
2239 int index
, nextindex
;
2241 struct btframe
*parent
;
2242 struct dt_lock
*dtlck
;
2245 struct pxd_lock
*pxdlock
;
2249 * keep the root leaf page which has become empty
2251 if (BT_IS_ROOT(fmp
)) {
2255 * dtInitRoot() acquires txlock on the root
2257 dtInitRoot(tid
, ip
, PARENT(ip
));
2265 * free the non-root leaf page
2268 * acquire a transaction lock on the page
2270 * write FREEXTENT|NOREDOPAGE log record
2271 * N.B. linelock is overlaid as freed extent descriptor, and
2272 * the buffer page is freed;
2274 tlck
= txMaplock(tid
, ip
, tlckDTREE
| tlckFREE
);
2275 pxdlock
= (struct pxd_lock
*) & tlck
->lock
;
2276 pxdlock
->flag
= mlckFREEPXD
;
2277 pxdlock
->pxd
= fp
->header
.self
;
2280 /* update sibling pointers */
2281 if ((rc
= dtRelink(tid
, ip
, fp
))) {
2286 xlen
= lengthPXD(&fp
->header
.self
);
2288 /* Free quota allocation. */
2289 DQUOT_FREE_BLOCK(ip
, xlen
);
2291 /* free/invalidate its buffer page */
2292 discard_metapage(fmp
);
2295 * propagate page deletion up the directory tree
2297 * If the delete from the parent page makes it empty,
2298 * continue all the way up the tree.
2299 * stop if the root page is reached (which is never deleted) or
2300 * if the entry deletion does not empty the page.
2302 while ((parent
= BT_POP(btstack
)) != NULL
) {
2303 /* pin the parent page <sp> */
2304 DT_GETPAGE(ip
, parent
->bn
, mp
, PSIZE
, p
, rc
);
2309 * free the extent of the child page deleted
2311 index
= parent
->index
;
2314 * delete the entry for the child page from parent
2316 nextindex
= p
->header
.nextindex
;
2319 * the parent has the single entry being deleted:
2321 * free the parent page which has become empty.
2323 if (nextindex
== 1) {
2325 * keep the root internal page which has become empty
2327 if (p
->header
.flag
& BT_ROOT
) {
2331 * dtInitRoot() acquires txlock on the root
2333 dtInitRoot(tid
, ip
, PARENT(ip
));
2340 * free the parent page
2344 * acquire a transaction lock on the page
2346 * write FREEXTENT|NOREDOPAGE log record
2350 tlckDTREE
| tlckFREE
);
2351 pxdlock
= (struct pxd_lock
*) & tlck
->lock
;
2352 pxdlock
->flag
= mlckFREEPXD
;
2353 pxdlock
->pxd
= p
->header
.self
;
2356 /* update sibling pointers */
2357 if ((rc
= dtRelink(tid
, ip
, p
))) {
2362 xlen
= lengthPXD(&p
->header
.self
);
2364 /* Free quota allocation */
2365 DQUOT_FREE_BLOCK(ip
, xlen
);
2367 /* free/invalidate its buffer page */
2368 discard_metapage(mp
);
2376 * the parent has other entries remaining:
2378 * delete the router entry from the parent page.
2380 BT_MARK_DIRTY(mp
, ip
);
2382 * acquire a transaction lock on the page
2384 * action: router entry deletion
2386 tlck
= txLock(tid
, ip
, mp
, tlckDTREE
| tlckENTRY
);
2387 dtlck
= (struct dt_lock
*) & tlck
->lock
;
2389 /* linelock header */
2390 if (dtlck
->index
>= dtlck
->maxcnt
)
2391 dtlck
= (struct dt_lock
*) txLinelock(dtlck
);
2392 lv
= & dtlck
->lv
[dtlck
->index
];
2397 /* linelock stbl of non-root leaf page */
2398 if (!(p
->header
.flag
& BT_ROOT
)) {
2399 if (dtlck
->index
< dtlck
->maxcnt
)
2402 dtlck
= (struct dt_lock
*) txLinelock(dtlck
);
2403 lv
= & dtlck
->lv
[0];
2405 i
= index
>> L2DTSLOTSIZE
;
2406 lv
->offset
= p
->header
.stblindex
+ i
;
2408 ((p
->header
.nextindex
- 1) >> L2DTSLOTSIZE
) -
2413 /* free the router entry */
2414 dtDeleteEntry(p
, index
, &dtlck
);
2416 /* reset key of new leftmost entry of level (for consistency) */
2418 ((p
->header
.flag
& BT_ROOT
) || p
->header
.prev
== 0))
2419 dtTruncateEntry(p
, 0, &dtlck
);
2421 /* unpin the parent page */
2424 /* exit propagation up */
2429 ip
->i_size
-= PSIZE
;
2436 * NAME: dtRelocate()
2438 * FUNCTION: relocate dtpage (internal or leaf) of directory;
2439 * This function is mainly used by defragfs utility.
2441 int dtRelocate(tid_t tid
, struct inode
*ip
, s64 lmxaddr
, pxd_t
* opxd
,
2445 struct metapage
*mp
, *pmp
, *lmp
, *rmp
;
2446 dtpage_t
*p
, *pp
, *rp
= 0, *lp
= 0;
2449 struct btstack btstack
;
2451 s64 oxaddr
, nextbn
, prevbn
;
2454 struct dt_lock
*dtlck
;
2455 struct pxd_lock
*pxdlock
;
2459 oxaddr
= addressPXD(opxd
);
2460 xlen
= lengthPXD(opxd
);
2462 jfs_info("dtRelocate: lmxaddr:%Ld xaddr:%Ld:%Ld xlen:%d",
2463 (long long)lmxaddr
, (long long)oxaddr
, (long long)nxaddr
,
2467 * 1. get the internal parent dtpage covering
2468 * router entry for the tartget page to be relocated;
2470 rc
= dtSearchNode(ip
, lmxaddr
, opxd
, &btstack
);
2474 /* retrieve search result */
2475 DT_GETSEARCH(ip
, btstack
.top
, bn
, pmp
, pp
, index
);
2476 jfs_info("dtRelocate: parent router entry validated.");
2479 * 2. relocate the target dtpage
2481 /* read in the target page from src extent */
2482 DT_GETPAGE(ip
, oxaddr
, mp
, PSIZE
, p
, rc
);
2484 /* release the pinned parent page */
2490 * read in sibling pages if any to update sibling pointers;
2493 if (p
->header
.next
) {
2494 nextbn
= le64_to_cpu(p
->header
.next
);
2495 DT_GETPAGE(ip
, nextbn
, rmp
, PSIZE
, rp
, rc
);
2504 if (p
->header
.prev
) {
2505 prevbn
= le64_to_cpu(p
->header
.prev
);
2506 DT_GETPAGE(ip
, prevbn
, lmp
, PSIZE
, lp
, rc
);
2516 /* at this point, all xtpages to be updated are in memory */
2519 * update sibling pointers of sibling dtpages if any;
2522 tlck
= txLock(tid
, ip
, lmp
, tlckDTREE
| tlckRELINK
);
2523 dtlck
= (struct dt_lock
*) & tlck
->lock
;
2524 /* linelock header */
2525 ASSERT(dtlck
->index
== 0);
2526 lv
= & dtlck
->lv
[0];
2531 lp
->header
.next
= cpu_to_le64(nxaddr
);
2536 tlck
= txLock(tid
, ip
, rmp
, tlckDTREE
| tlckRELINK
);
2537 dtlck
= (struct dt_lock
*) & tlck
->lock
;
2538 /* linelock header */
2539 ASSERT(dtlck
->index
== 0);
2540 lv
= & dtlck
->lv
[0];
2545 rp
->header
.prev
= cpu_to_le64(nxaddr
);
2550 * update the target dtpage to be relocated
2552 * write LOG_REDOPAGE of LOG_NEW type for dst page
2553 * for the whole target page (logredo() will apply
2554 * after image and update bmap for allocation of the
2555 * dst extent), and update bmap for allocation of
2558 tlck
= txLock(tid
, ip
, mp
, tlckDTREE
| tlckNEW
);
2559 dtlck
= (struct dt_lock
*) & tlck
->lock
;
2560 /* linelock header */
2561 ASSERT(dtlck
->index
== 0);
2562 lv
= & dtlck
->lv
[0];
2564 /* update the self address in the dtpage header */
2565 pxd
= &p
->header
.self
;
2566 PXDaddress(pxd
, nxaddr
);
2568 /* the dst page is the same as the src page, i.e.,
2569 * linelock for afterimage of the whole page;
2572 lv
->length
= p
->header
.maxslot
;
2575 /* update the buffer extent descriptor of the dtpage */
2576 xsize
= xlen
<< JFS_SBI(ip
->i_sb
)->l2bsize
;
2578 /* unpin the relocated page */
2580 jfs_info("dtRelocate: target dtpage relocated.");
2582 /* the moved extent is dtpage, then a LOG_NOREDOPAGE log rec
2583 * needs to be written (in logredo(), the LOG_NOREDOPAGE log rec
2584 * will also force a bmap update ).
2588 * 3. acquire maplock for the source extent to be freed;
2590 /* for dtpage relocation, write a LOG_NOREDOPAGE record
2591 * for the source dtpage (logredo() will init NoRedoPage
2592 * filter and will also update bmap for free of the source
2593 * dtpage), and upadte bmap for free of the source dtpage;
2595 tlck
= txMaplock(tid
, ip
, tlckDTREE
| tlckFREE
);
2596 pxdlock
= (struct pxd_lock
*) & tlck
->lock
;
2597 pxdlock
->flag
= mlckFREEPXD
;
2598 PXDaddress(&pxdlock
->pxd
, oxaddr
);
2599 PXDlength(&pxdlock
->pxd
, xlen
);
2603 * 4. update the parent router entry for relocation;
2605 * acquire tlck for the parent entry covering the target dtpage;
2606 * write LOG_REDOPAGE to apply after image only;
2608 jfs_info("dtRelocate: update parent router entry.");
2609 tlck
= txLock(tid
, ip
, pmp
, tlckDTREE
| tlckENTRY
);
2610 dtlck
= (struct dt_lock
*) & tlck
->lock
;
2611 lv
= & dtlck
->lv
[dtlck
->index
];
2613 /* update the PXD with the new address */
2614 stbl
= DT_GETSTBL(pp
);
2615 pxd
= (pxd_t
*) & pp
->slot
[stbl
[index
]];
2616 PXDaddress(pxd
, nxaddr
);
2617 lv
->offset
= stbl
[index
];
2621 /* unpin the parent dtpage */
2628 * NAME: dtSearchNode()
2630 * FUNCTION: Search for an dtpage containing a specified address
2631 * This function is mainly used by defragfs utility.
2633 * NOTE: Search result on stack, the found page is pinned at exit.
2634 * The result page must be an internal dtpage.
2635 * lmxaddr give the address of the left most page of the
2636 * dtree level, in which the required dtpage resides.
2638 static int dtSearchNode(struct inode
*ip
, s64 lmxaddr
, pxd_t
* kpxd
,
2639 struct btstack
* btstack
)
2643 struct metapage
*mp
;
2645 int psize
= 288; /* initial in-line directory */
2649 struct btframe
*btsp
;
2651 BT_CLR(btstack
); /* reset stack */
2654 * descend tree to the level with specified leftmost page
2656 * by convention, root bn = 0.
2659 /* get/pin the page to search */
2660 DT_GETPAGE(ip
, bn
, mp
, psize
, p
, rc
);
2664 /* does the xaddr of leftmost page of the levevl
2665 * matches levevl search key ?
2667 if (p
->header
.flag
& BT_ROOT
) {
2670 } else if (addressPXD(&p
->header
.self
) == lmxaddr
)
2674 * descend down to leftmost child page
2676 if (p
->header
.flag
& BT_LEAF
) {
2681 /* get the leftmost entry */
2682 stbl
= DT_GETSTBL(p
);
2683 pxd
= (pxd_t
*) & p
->slot
[stbl
[0]];
2685 /* get the child page block address */
2686 bn
= addressPXD(pxd
);
2687 psize
= lengthPXD(pxd
) << JFS_SBI(ip
->i_sb
)->l2bsize
;
2688 /* unpin the parent page */
2693 * search each page at the current levevl
2696 stbl
= DT_GETSTBL(p
);
2697 for (i
= 0; i
< p
->header
.nextindex
; i
++) {
2698 pxd
= (pxd_t
*) & p
->slot
[stbl
[i
]];
2700 /* found the specified router entry */
2701 if (addressPXD(pxd
) == addressPXD(kpxd
) &&
2702 lengthPXD(pxd
) == lengthPXD(kpxd
)) {
2703 btsp
= btstack
->top
;
2712 /* get the right sibling page if any */
2714 bn
= le64_to_cpu(p
->header
.next
);
2720 /* unpin current page */
2723 /* get the right sibling page */
2724 DT_GETPAGE(ip
, bn
, mp
, PSIZE
, p
, rc
);
2730 #endif /* _NOTYET */
2736 * link around a freed page.
2739 * fp: page to be freed
2743 static int dtRelink(tid_t tid
, struct inode
*ip
, dtpage_t
* p
)
2746 struct metapage
*mp
;
2749 struct dt_lock
*dtlck
;
2752 nextbn
= le64_to_cpu(p
->header
.next
);
2753 prevbn
= le64_to_cpu(p
->header
.prev
);
2755 /* update prev pointer of the next page */
2757 DT_GETPAGE(ip
, nextbn
, mp
, PSIZE
, p
, rc
);
2761 BT_MARK_DIRTY(mp
, ip
);
2763 * acquire a transaction lock on the next page
2765 * action: update prev pointer;
2767 tlck
= txLock(tid
, ip
, mp
, tlckDTREE
| tlckRELINK
);
2768 jfs_info("dtRelink nextbn: tlck = 0x%p, ip = 0x%p, mp=0x%p",
2770 dtlck
= (struct dt_lock
*) & tlck
->lock
;
2772 /* linelock header */
2773 if (dtlck
->index
>= dtlck
->maxcnt
)
2774 dtlck
= (struct dt_lock
*) txLinelock(dtlck
);
2775 lv
= & dtlck
->lv
[dtlck
->index
];
2780 p
->header
.prev
= cpu_to_le64(prevbn
);
2784 /* update next pointer of the previous page */
2786 DT_GETPAGE(ip
, prevbn
, mp
, PSIZE
, p
, rc
);
2790 BT_MARK_DIRTY(mp
, ip
);
2792 * acquire a transaction lock on the prev page
2794 * action: update next pointer;
2796 tlck
= txLock(tid
, ip
, mp
, tlckDTREE
| tlckRELINK
);
2797 jfs_info("dtRelink prevbn: tlck = 0x%p, ip = 0x%p, mp=0x%p",
2799 dtlck
= (struct dt_lock
*) & tlck
->lock
;
2801 /* linelock header */
2802 if (dtlck
->index
>= dtlck
->maxcnt
)
2803 dtlck
= (struct dt_lock
*) txLinelock(dtlck
);
2804 lv
= & dtlck
->lv
[dtlck
->index
];
2809 p
->header
.next
= cpu_to_le64(nextbn
);
2820 * initialize directory root (inline in inode)
2822 void dtInitRoot(tid_t tid
, struct inode
*ip
, u32 idotdot
)
2824 struct jfs_inode_info
*jfs_ip
= JFS_IP(ip
);
2829 struct dt_lock
*dtlck
;
2834 * If this was previously an non-empty directory, we need to remove
2835 * the old directory table.
2838 if (!jfs_dirtable_inline(ip
)) {
2839 struct tblock
*tblk
= tid_to_tblock(tid
);
2841 * We're playing games with the tid's xflag. If
2842 * we're removing a regular file, the file's xtree
2843 * is committed with COMMIT_PMAP, but we always
2844 * commit the directories xtree with COMMIT_PWMAP.
2846 xflag_save
= tblk
->xflag
;
2849 * xtTruncate isn't guaranteed to fully truncate
2850 * the xtree. The caller needs to check i_size
2851 * after committing the transaction to see if
2852 * additional truncation is needed. The
2853 * COMMIT_Stale flag tells caller that we
2854 * initiated the truncation.
2856 xtTruncate(tid
, ip
, 0, COMMIT_PWMAP
);
2857 set_cflag(COMMIT_Stale
, ip
);
2859 tblk
->xflag
= xflag_save
;
2863 jfs_ip
->next_index
= 2;
2865 ip
->i_size
= IDATASIZE
;
2868 * acquire a transaction lock on the root
2870 * action: directory initialization;
2872 tlck
= txLock(tid
, ip
, (struct metapage
*) & jfs_ip
->bxflag
,
2873 tlckDTREE
| tlckENTRY
| tlckBTROOT
);
2874 dtlck
= (struct dt_lock
*) & tlck
->lock
;
2877 ASSERT(dtlck
->index
== 0);
2878 lv
= & dtlck
->lv
[0];
2880 lv
->length
= DTROOTMAXSLOT
;
2883 p
= &jfs_ip
->i_dtroot
;
2885 p
->header
.flag
= DXD_INDEX
| BT_ROOT
| BT_LEAF
;
2887 p
->header
.nextindex
= 0;
2893 /* init data area of root */
2894 for (fsi
++; fsi
< DTROOTMAXSLOT
; f
++, fsi
++)
2898 p
->header
.freelist
= 1;
2899 p
->header
.freecnt
= 8;
2901 /* init '..' entry */
2902 p
->header
.idotdot
= cpu_to_le32(idotdot
);
2908 * add_missing_indices()
2910 * function: Fix dtree page in which one or more entries has an invalid index.
2911 * fsck.jfs should really fix this, but it currently does not.
2912 * Called from jfs_readdir when bad index is detected.
2914 static void add_missing_indices(struct inode
*inode
, s64 bn
)
2917 struct dt_lock
*dtlck
;
2921 struct metapage
*mp
;
2928 tid
= txBegin(inode
->i_sb
, 0);
2930 DT_GETPAGE(inode
, bn
, mp
, PSIZE
, p
, rc
);
2933 printk(KERN_ERR
"DT_GETPAGE failed!\n");
2936 BT_MARK_DIRTY(mp
, inode
);
2938 ASSERT(p
->header
.flag
& BT_LEAF
);
2940 tlck
= txLock(tid
, inode
, mp
, tlckDTREE
| tlckENTRY
);
2942 tlck
->type
|= tlckBTROOT
;
2944 dtlck
= (struct dt_lock
*) &tlck
->lock
;
2946 stbl
= DT_GETSTBL(p
);
2947 for (i
= 0; i
< p
->header
.nextindex
; i
++) {
2948 d
= (struct ldtentry
*) &p
->slot
[stbl
[i
]];
2949 index
= le32_to_cpu(d
->index
);
2950 if ((index
< 2) || (index
>= JFS_IP(inode
)->next_index
)) {
2951 d
->index
= cpu_to_le32(add_index(tid
, inode
, bn
, i
));
2952 if (dtlck
->index
>= dtlck
->maxcnt
)
2953 dtlck
= (struct dt_lock
*) txLinelock(dtlck
);
2954 lv
= &dtlck
->lv
[dtlck
->index
];
2955 lv
->offset
= stbl
[i
];
2962 (void) txCommit(tid
, 1, &inode
, 0);
2968 * Buffer to hold directory entry info while traversing a dtree page
2969 * before being fed to the filldir function
2979 * function to determine next variable-sized jfs_dirent in buffer
2981 static inline struct jfs_dirent
*next_jfs_dirent(struct jfs_dirent
*dirent
)
2983 return (struct jfs_dirent
*)
2985 ((sizeof (struct jfs_dirent
) + dirent
->name_len
+ 1 +
2986 sizeof (loff_t
) - 1) &
2987 ~(sizeof (loff_t
) - 1)));
2993 * function: read directory entries sequentially
2994 * from the specified entry offset
2998 * return: offset = (pn, index) of start entry
2999 * of next jfs_readdir()/dtRead()
3001 int jfs_readdir(struct file
*filp
, void *dirent
, filldir_t filldir
)
3003 struct inode
*ip
= filp
->f_path
.dentry
->d_inode
;
3004 struct nls_table
*codepage
= JFS_SBI(ip
->i_sb
)->nls_tab
;
3006 loff_t dtpos
; /* legacy OS/2 style position */
3011 } *dtoffset
= (struct dtoffset
*) &dtpos
;
3013 struct metapage
*mp
;
3017 struct btstack btstack
;
3021 int d_namleft
, len
, outlen
;
3022 unsigned long dirent_buf
;
3026 uint loop_count
= 0;
3027 struct jfs_dirent
*jfs_dirent
;
3029 int overflow
, fix_page
, page_fixed
= 0;
3030 static int unique_pos
= 2; /* If we can't fix broken index */
3032 if (filp
->f_pos
== DIREND
)
3037 * persistent index is stored in directory entries.
3038 * Special cases: 0 = .
3040 * -1 = End of directory
3044 dir_index
= (u32
) filp
->f_pos
;
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 */
3052 filp
->f_pos
= DIREND
;
3056 rc
= read_index(ip
, dir_index
, &dirtab_slot
);
3058 filp
->f_pos
= DIREND
;
3061 if (dirtab_slot
.flag
== DIR_INDEX_FREE
) {
3062 if (loop_count
++ > JFS_IP(ip
)->next_index
) {
3063 jfs_err("jfs_readdir detected "
3065 filp
->f_pos
= DIREND
;
3068 dir_index
= le32_to_cpu(dirtab_slot
.addr2
);
3069 if (dir_index
== -1) {
3070 filp
->f_pos
= DIREND
;
3075 bn
= addressDTS(&dirtab_slot
);
3076 index
= dirtab_slot
.slot
;
3077 DT_GETPAGE(ip
, bn
, mp
, PSIZE
, p
, rc
);
3079 filp
->f_pos
= DIREND
;
3082 if (p
->header
.flag
& BT_INTERNAL
) {
3083 jfs_err("jfs_readdir: bad index table");
3089 if (dir_index
== 0) {
3094 if (filldir(dirent
, ".", 1, 0, ip
->i_ino
,
3102 if (filldir(dirent
, "..", 2, 1, PARENT(ip
), DT_DIR
))
3106 * Find first entry of left-most leaf
3109 filp
->f_pos
= DIREND
;
3113 if ((rc
= dtReadFirst(ip
, &btstack
)))
3116 DT_GETSEARCH(ip
, btstack
.top
, bn
, mp
, p
, index
);
3120 * Legacy filesystem - OS/2 & Linux JFS < 0.3.6
3122 * pn = index = 0: First entry "."
3123 * pn = 0; index = 1: Second entry ".."
3124 * pn > 0: Real entries, pn=1 -> leftmost page
3125 * pn = index = -1: No more entries
3127 dtpos
= filp
->f_pos
;
3129 /* build "." entry */
3131 if (filldir(dirent
, ".", 1, filp
->f_pos
, ip
->i_ino
,
3134 dtoffset
->index
= 1;
3135 filp
->f_pos
= dtpos
;
3138 if (dtoffset
->pn
== 0) {
3139 if (dtoffset
->index
== 1) {
3140 /* build ".." entry */
3142 if (filldir(dirent
, "..", 2, filp
->f_pos
,
3143 PARENT(ip
), DT_DIR
))
3146 jfs_err("jfs_readdir called with "
3150 dtoffset
->index
= 0;
3151 filp
->f_pos
= dtpos
;
3155 filp
->f_pos
= DIREND
;
3159 if ((rc
= dtReadNext(ip
, &filp
->f_pos
, &btstack
))) {
3160 jfs_err("jfs_readdir: unexpected rc = %d "
3161 "from dtReadNext", rc
);
3162 filp
->f_pos
= DIREND
;
3165 /* get start leaf page and index */
3166 DT_GETSEARCH(ip
, btstack
.top
, bn
, mp
, p
, index
);
3168 /* offset beyond directory eof ? */
3170 filp
->f_pos
= DIREND
;
3175 dirent_buf
= __get_free_page(GFP_KERNEL
);
3176 if (dirent_buf
== 0) {
3178 jfs_warn("jfs_readdir: __get_free_page failed!");
3179 filp
->f_pos
= DIREND
;
3184 jfs_dirent
= (struct jfs_dirent
*) dirent_buf
;
3186 overflow
= fix_page
= 0;
3188 stbl
= DT_GETSTBL(p
);
3190 for (i
= index
; i
< p
->header
.nextindex
; i
++) {
3191 d
= (struct ldtentry
*) & p
->slot
[stbl
[i
]];
3193 if (((long) jfs_dirent
+ d
->namlen
+ 1) >
3194 (dirent_buf
+ PAGE_SIZE
)) {
3195 /* DBCS codepages could overrun dirent_buf */
3201 d_namleft
= d
->namlen
;
3202 name_ptr
= jfs_dirent
->name
;
3203 jfs_dirent
->ino
= le32_to_cpu(d
->inumber
);
3206 len
= min(d_namleft
, DTLHDRDATALEN
);
3207 jfs_dirent
->position
= le32_to_cpu(d
->index
);
3209 * d->index should always be valid, but it
3210 * isn't. fsck.jfs doesn't create the
3211 * directory index for the lost+found
3212 * directory. Rather than let it go,
3213 * we can try to fix it.
3215 if ((jfs_dirent
->position
< 2) ||
3216 (jfs_dirent
->position
>=
3217 JFS_IP(ip
)->next_index
)) {
3218 if (!page_fixed
&& !isReadOnly(ip
)) {
3221 * setting overflow and setting
3222 * index to i will cause the
3223 * same page to be processed
3224 * again starting here
3230 jfs_dirent
->position
= unique_pos
++;
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 = "
3252 "%ld, bn=%Ld, index = %d",
3258 len
= min(d_namleft
, DTSLOTDATALEN
);
3259 outlen
= jfs_strfromUCS_le(name_ptr
, t
->name
,
3261 jfs_dirent
->name_len
+= outlen
;
3267 jfs_dirent
= next_jfs_dirent(jfs_dirent
);
3274 /* Point to next leaf page */
3275 if (p
->header
.flag
& BT_ROOT
)
3278 bn
= le64_to_cpu(p
->header
.next
);
3280 /* update offset (pn:index) for new page */
3283 dtoffset
->index
= 0;
3289 /* unpin previous leaf page */
3292 jfs_dirent
= (struct jfs_dirent
*) dirent_buf
;
3293 while (jfs_dirents
--) {
3294 filp
->f_pos
= jfs_dirent
->position
;
3295 if (filldir(dirent
, jfs_dirent
->name
,
3296 jfs_dirent
->name_len
, filp
->f_pos
,
3297 jfs_dirent
->ino
, DT_UNKNOWN
))
3299 jfs_dirent
= next_jfs_dirent(jfs_dirent
);
3303 add_missing_indices(ip
, bn
);
3307 if (!overflow
&& (bn
== 0)) {
3308 filp
->f_pos
= DIREND
;
3312 DT_GETPAGE(ip
, bn
, mp
, PSIZE
, p
, rc
);
3314 free_page(dirent_buf
);
3320 free_page(dirent_buf
);
3329 * function: get the leftmost page of the directory
3331 static int dtReadFirst(struct inode
*ip
, struct btstack
* btstack
)
3335 int psize
= 288; /* initial in-line directory */
3336 struct metapage
*mp
;
3339 struct btframe
*btsp
;
3342 BT_CLR(btstack
); /* reset stack */
3345 * descend leftmost path of the tree
3347 * by convention, root bn = 0.
3350 DT_GETPAGE(ip
, bn
, mp
, psize
, p
, rc
);
3355 * leftmost leaf page
3357 if (p
->header
.flag
& BT_LEAF
) {
3358 /* return leftmost entry */
3359 btsp
= btstack
->top
;
3368 * descend down to leftmost child page
3370 if (BT_STACK_FULL(btstack
)) {
3372 jfs_error(ip
->i_sb
, "dtReadFirst: btstack overrun");
3373 BT_STACK_DUMP(btstack
);
3376 /* push (bn, index) of the parent page/entry */
3377 BT_PUSH(btstack
, bn
, 0);
3379 /* get the leftmost entry */
3380 stbl
= DT_GETSTBL(p
);
3381 xd
= (pxd_t
*) & p
->slot
[stbl
[0]];
3383 /* get the child page block address */
3384 bn
= addressPXD(xd
);
3385 psize
= lengthPXD(xd
) << JFS_SBI(ip
->i_sb
)->l2bsize
;
3387 /* unpin the parent page */
3396 * function: get the page of the specified offset (pn:index)
3398 * return: if (offset > eof), bn = -1;
3400 * note: if index > nextindex of the target leaf page,
3401 * start with 1st entry of next leaf page;
3403 static int dtReadNext(struct inode
*ip
, loff_t
* offset
,
3404 struct btstack
* btstack
)
3411 } *dtoffset
= (struct dtoffset
*) offset
;
3413 struct metapage
*mp
;
3418 struct btframe
*btsp
, *parent
;
3422 * get leftmost leaf page pinned
3424 if ((rc
= dtReadFirst(ip
, btstack
)))
3428 DT_GETSEARCH(ip
, btstack
->top
, bn
, mp
, p
, index
);
3430 /* get the start offset (pn:index) */
3431 pn
= dtoffset
->pn
- 1; /* Now pn = 0 represents leftmost leaf */
3432 index
= dtoffset
->index
;
3434 /* start at leftmost page ? */
3436 /* offset beyond eof ? */
3437 if (index
< p
->header
.nextindex
)
3440 if (p
->header
.flag
& BT_ROOT
) {
3445 /* start with 1st entry of next leaf page */
3447 dtoffset
->index
= index
= 0;
3451 /* start at non-leftmost page: scan parent pages for large pn */
3452 if (p
->header
.flag
& BT_ROOT
) {
3457 /* start after next leaf page ? */
3461 /* get leaf page pn = 1 */
3463 bn
= le64_to_cpu(p
->header
.next
);
3465 /* unpin leaf page */
3468 /* offset beyond eof ? */
3477 * scan last internal page level to get target leaf page
3480 /* unpin leftmost leaf page */
3483 /* get left most parent page */
3484 btsp
= btstack
->top
;
3487 DT_GETPAGE(ip
, bn
, mp
, PSIZE
, p
, rc
);
3491 /* scan parent pages at last internal page level */
3492 while (pn
>= p
->header
.nextindex
) {
3493 pn
-= p
->header
.nextindex
;
3495 /* get next parent page address */
3496 bn
= le64_to_cpu(p
->header
.next
);
3498 /* unpin current parent page */
3501 /* offset beyond eof ? */
3507 /* get next parent page */
3508 DT_GETPAGE(ip
, bn
, mp
, PSIZE
, p
, rc
);
3512 /* update parent page stack frame */
3516 /* get leaf page address */
3517 stbl
= DT_GETSTBL(p
);
3518 xd
= (pxd_t
*) & p
->slot
[stbl
[pn
]];
3519 bn
= addressPXD(xd
);
3521 /* unpin parent page */
3525 * get target leaf page
3528 DT_GETPAGE(ip
, bn
, mp
, PSIZE
, p
, rc
);
3533 * leaf page has been completed:
3534 * start with 1st entry of next leaf page
3536 if (index
>= p
->header
.nextindex
) {
3537 bn
= le64_to_cpu(p
->header
.next
);
3539 /* unpin leaf page */
3542 /* offset beyond eof ? */
3548 /* get next leaf page */
3549 DT_GETPAGE(ip
, bn
, mp
, PSIZE
, p
, rc
);
3553 /* start with 1st entry of next leaf page */
3555 dtoffset
->index
= 0;
3559 /* return target leaf page pinned */
3560 btsp
= btstack
->top
;
3562 btsp
->index
= dtoffset
->index
;
3572 * function: compare search key with an internal entry
3575 * < 0 if k is < record
3576 * = 0 if k is = record
3577 * > 0 if k is > record
3579 static int dtCompare(struct component_name
* key
, /* search key */
3580 dtpage_t
* p
, /* directory page */
3582 { /* entry slot index */
3585 int klen
, namlen
, len
, rc
;
3586 struct idtentry
*ih
;
3590 * force the left-most key on internal pages, at any level of
3591 * the tree, to be less than any search key.
3592 * this obviates having to update the leftmost key on an internal
3593 * page when the user inserts a new key in the tree smaller than
3594 * anything that has been stored.
3596 * (? if/when dtSearch() narrows down to 1st entry (index = 0),
3597 * at any internal page at any level of the tree,
3598 * it descends to child of the entry anyway -
3599 * ? make the entry as min size dummy entry)
3601 * if (e->index == 0 && h->prevpg == P_INVALID && !(h->flags & BT_LEAF))
3608 ih
= (struct idtentry
*) & p
->slot
[si
];
3611 namlen
= ih
->namlen
;
3612 len
= min(namlen
, DTIHDRDATALEN
);
3614 /* compare with head/only segment */
3615 len
= min(klen
, len
);
3616 if ((rc
= UniStrncmp_le(kname
, name
, len
)))
3622 /* compare with additional segment(s) */
3624 while (klen
> 0 && namlen
> 0) {
3625 /* compare with next name segment */
3626 t
= (struct dtslot
*) & p
->slot
[si
];
3627 len
= min(namlen
, DTSLOTDATALEN
);
3628 len
= min(klen
, len
);
3630 if ((rc
= UniStrncmp_le(kname
, name
, len
)))
3639 return (klen
- namlen
);
3648 * function: compare search key with an (leaf/internal) entry
3651 * < 0 if k is < record
3652 * = 0 if k is = record
3653 * > 0 if k is > record
3655 static int ciCompare(struct component_name
* key
, /* search key */
3656 dtpage_t
* p
, /* directory page */
3657 int si
, /* entry slot index */
3662 int klen
, namlen
, len
, rc
;
3663 struct ldtentry
*lh
;
3664 struct idtentry
*ih
;
3669 * force the left-most key on internal pages, at any level of
3670 * the tree, to be less than any search key.
3671 * this obviates having to update the leftmost key on an internal
3672 * page when the user inserts a new key in the tree smaller than
3673 * anything that has been stored.
3675 * (? if/when dtSearch() narrows down to 1st entry (index = 0),
3676 * at any internal page at any level of the tree,
3677 * it descends to child of the entry anyway -
3678 * ? make the entry as min size dummy entry)
3680 * if (e->index == 0 && h->prevpg == P_INVALID && !(h->flags & BT_LEAF))
3690 if (p
->header
.flag
& BT_LEAF
) {
3691 lh
= (struct ldtentry
*) & p
->slot
[si
];
3694 namlen
= lh
->namlen
;
3695 if (flag
& JFS_DIR_INDEX
)
3696 len
= min(namlen
, DTLHDRDATALEN
);
3698 len
= min(namlen
, DTLHDRDATALEN_LEGACY
);
3701 * internal page entry
3704 ih
= (struct idtentry
*) & p
->slot
[si
];
3707 namlen
= ih
->namlen
;
3708 len
= min(namlen
, DTIHDRDATALEN
);
3711 /* compare with head/only segment */
3712 len
= min(klen
, len
);
3713 for (i
= 0; i
< len
; i
++, kname
++, name
++) {
3714 /* only uppercase if case-insensitive support is on */
3715 if ((flag
& JFS_OS2
) == JFS_OS2
)
3716 x
= UniToupper(le16_to_cpu(*name
));
3718 x
= le16_to_cpu(*name
);
3719 if ((rc
= *kname
- x
))
3726 /* compare with additional segment(s) */
3727 while (klen
> 0 && namlen
> 0) {
3728 /* compare with next name segment */
3729 t
= (struct dtslot
*) & p
->slot
[si
];
3730 len
= min(namlen
, DTSLOTDATALEN
);
3731 len
= min(klen
, len
);
3733 for (i
= 0; i
< len
; i
++, kname
++, name
++) {
3734 /* only uppercase if case-insensitive support is on */
3735 if ((flag
& JFS_OS2
) == JFS_OS2
)
3736 x
= UniToupper(le16_to_cpu(*name
));
3738 x
= le16_to_cpu(*name
);
3740 if ((rc
= *kname
- x
))
3749 return (klen
- namlen
);
3754 * ciGetLeafPrefixKey()
3756 * function: compute prefix of suffix compression
3757 * from two adjacent leaf entries
3758 * across page boundary
3760 * return: non-zero on error
3763 static int ciGetLeafPrefixKey(dtpage_t
* lp
, int li
, dtpage_t
* rp
,
3764 int ri
, struct component_name
* key
, int flag
)
3767 wchar_t *pl
, *pr
, *kname
;
3768 struct component_name lkey
;
3769 struct component_name rkey
;
3771 lkey
.name
= kmalloc((JFS_NAME_MAX
+ 1) * sizeof(wchar_t),
3773 if (lkey
.name
== NULL
)
3776 rkey
.name
= kmalloc((JFS_NAME_MAX
+ 1) * sizeof(wchar_t),
3778 if (rkey
.name
== NULL
) {
3783 /* get left and right key */
3784 dtGetKey(lp
, li
, &lkey
, flag
);
3785 lkey
.name
[lkey
.namlen
] = 0;
3787 if ((flag
& JFS_OS2
) == JFS_OS2
)
3790 dtGetKey(rp
, ri
, &rkey
, flag
);
3791 rkey
.name
[rkey
.namlen
] = 0;
3794 if ((flag
& JFS_OS2
) == JFS_OS2
)
3797 /* compute prefix */
3800 namlen
= min(lkey
.namlen
, rkey
.namlen
);
3801 for (pl
= lkey
.name
, pr
= rkey
.name
;
3802 namlen
; pl
++, pr
++, namlen
--, klen
++, kname
++) {
3805 key
->namlen
= klen
+ 1;
3810 /* l->namlen <= r->namlen since l <= r */
3811 if (lkey
.namlen
< rkey
.namlen
) {
3813 key
->namlen
= klen
+ 1;
3814 } else /* l->namelen == r->namelen */
3828 * function: get key of the entry
3830 static void dtGetKey(dtpage_t
* p
, int i
, /* entry index */
3831 struct component_name
* key
, int flag
)
3835 struct ldtentry
*lh
;
3836 struct idtentry
*ih
;
3843 stbl
= DT_GETSTBL(p
);
3845 if (p
->header
.flag
& BT_LEAF
) {
3846 lh
= (struct ldtentry
*) & p
->slot
[si
];
3848 namlen
= lh
->namlen
;
3850 if (flag
& JFS_DIR_INDEX
)
3851 len
= min(namlen
, DTLHDRDATALEN
);
3853 len
= min(namlen
, DTLHDRDATALEN_LEGACY
);
3855 ih
= (struct idtentry
*) & p
->slot
[si
];
3857 namlen
= ih
->namlen
;
3859 len
= min(namlen
, DTIHDRDATALEN
);
3862 key
->namlen
= namlen
;
3866 * move head/only segment
3868 UniStrncpy_from_le(kname
, name
, len
);
3871 * move additional segment(s)
3874 /* get next segment */
3878 len
= min(namlen
, DTSLOTDATALEN
);
3879 UniStrncpy_from_le(kname
, t
->name
, len
);
3889 * function: allocate free slot(s) and
3890 * write a leaf/internal entry
3892 * return: entry slot index
3894 static void dtInsertEntry(dtpage_t
* p
, int index
, struct component_name
* key
,
3895 ddata_t
* data
, struct dt_lock
** dtlock
)
3897 struct dtslot
*h
, *t
;
3898 struct ldtentry
*lh
= NULL
;
3899 struct idtentry
*ih
= NULL
;
3900 int hsi
, fsi
, klen
, len
, nextindex
;
3905 struct dt_lock
*dtlck
= *dtlock
;
3909 struct metapage
*mp
= NULL
;
3914 /* allocate a free slot */
3915 hsi
= fsi
= p
->header
.freelist
;
3917 p
->header
.freelist
= h
->next
;
3918 --p
->header
.freecnt
;
3920 /* open new linelock */
3921 if (dtlck
->index
>= dtlck
->maxcnt
)
3922 dtlck
= (struct dt_lock
*) txLinelock(dtlck
);
3924 lv
= & dtlck
->lv
[dtlck
->index
];
3927 /* write head/only segment */
3928 if (p
->header
.flag
& BT_LEAF
) {
3929 lh
= (struct ldtentry
*) h
;
3931 lh
->inumber
= cpu_to_le32(data
->leaf
.ino
);
3934 if (data
->leaf
.ip
) {
3935 len
= min(klen
, DTLHDRDATALEN
);
3936 if (!(p
->header
.flag
& BT_ROOT
))
3937 bn
= addressPXD(&p
->header
.self
);
3938 lh
->index
= cpu_to_le32(add_index(data
->leaf
.tid
,
3942 len
= min(klen
, DTLHDRDATALEN_LEGACY
);
3944 ih
= (struct idtentry
*) h
;
3950 len
= min(klen
, DTIHDRDATALEN
);
3953 UniStrncpy_to_le(name
, kname
, len
);
3958 /* write additional segment(s) */
3963 fsi
= p
->header
.freelist
;
3965 p
->header
.freelist
= t
->next
;
3966 --p
->header
.freecnt
;
3968 /* is next slot contiguous ? */
3969 if (fsi
!= xsi
+ 1) {
3970 /* close current linelock */
3974 /* open new linelock */
3975 if (dtlck
->index
< dtlck
->maxcnt
)
3978 dtlck
= (struct dt_lock
*) txLinelock(dtlck
);
3979 lv
= & dtlck
->lv
[0];
3987 len
= min(klen
, DTSLOTDATALEN
);
3988 UniStrncpy_to_le(t
->name
, kname
, len
);
3995 /* close current linelock */
4001 /* terminate last/only segment */
4003 /* single segment entry */
4004 if (p
->header
.flag
& BT_LEAF
)
4009 /* multi-segment entry */
4012 /* if insert into middle, shift right succeeding entries in stbl */
4013 stbl
= DT_GETSTBL(p
);
4014 nextindex
= p
->header
.nextindex
;
4015 if (index
< nextindex
) {
4016 memmove(stbl
+ index
+ 1, stbl
+ index
, nextindex
- index
);
4018 if ((p
->header
.flag
& BT_LEAF
) && data
->leaf
.ip
) {
4022 * Need to update slot number for entries that moved
4026 for (n
= index
+ 1; n
<= nextindex
; n
++) {
4027 lh
= (struct ldtentry
*) & (p
->slot
[stbl
[n
]]);
4028 modify_index(data
->leaf
.tid
, data
->leaf
.ip
,
4029 le32_to_cpu(lh
->index
), bn
, n
,
4033 release_metapage(mp
);
4039 /* advance next available entry index of stbl */
4040 ++p
->header
.nextindex
;
4047 * function: move entries from split/left page to new/right page
4049 * nextindex of dst page and freelist/freecnt of both pages
4052 static void dtMoveEntry(dtpage_t
* sp
, int si
, dtpage_t
* dp
,
4053 struct dt_lock
** sdtlock
, struct dt_lock
** ddtlock
,
4056 int ssi
, next
; /* src slot index */
4057 int di
; /* dst entry index */
4058 int dsi
; /* dst slot index */
4059 s8
*sstbl
, *dstbl
; /* sorted entry table */
4061 struct ldtentry
*slh
, *dlh
= NULL
;
4062 struct idtentry
*sih
, *dih
= NULL
;
4063 struct dtslot
*h
, *s
, *d
;
4064 struct dt_lock
*sdtlck
= *sdtlock
, *ddtlck
= *ddtlock
;
4065 struct lv
*slv
, *dlv
;
4069 sstbl
= (s8
*) & sp
->slot
[sp
->header
.stblindex
];
4070 dstbl
= (s8
*) & dp
->slot
[dp
->header
.stblindex
];
4072 dsi
= dp
->header
.freelist
; /* first (whole page) free slot */
4073 sfsi
= sp
->header
.freelist
;
4075 /* linelock destination entry slot */
4076 dlv
= & ddtlck
->lv
[ddtlck
->index
];
4079 /* linelock source entry slot */
4080 slv
= & sdtlck
->lv
[sdtlck
->index
];
4081 slv
->offset
= sstbl
[si
];
4082 xssi
= slv
->offset
- 1;
4088 for (di
= 0; si
< sp
->header
.nextindex
; si
++, di
++) {
4092 /* is next slot contiguous ? */
4093 if (ssi
!= xssi
+ 1) {
4094 /* close current linelock */
4098 /* open new linelock */
4099 if (sdtlck
->index
< sdtlck
->maxcnt
)
4102 sdtlck
= (struct dt_lock
*) txLinelock(sdtlck
);
4103 slv
= & sdtlck
->lv
[0];
4111 * move head/only segment of an entry
4114 h
= d
= &dp
->slot
[dsi
];
4116 /* get src slot and move */
4118 if (sp
->header
.flag
& BT_LEAF
) {
4119 /* get source entry */
4120 slh
= (struct ldtentry
*) s
;
4121 dlh
= (struct ldtentry
*) h
;
4122 snamlen
= slh
->namlen
;
4125 len
= min(snamlen
, DTLHDRDATALEN
);
4126 dlh
->index
= slh
->index
; /* little-endian */
4128 len
= min(snamlen
, DTLHDRDATALEN_LEGACY
);
4130 memcpy(dlh
, slh
, 6 + len
* 2);
4134 /* update dst head/only segment next field */
4138 sih
= (struct idtentry
*) s
;
4139 snamlen
= sih
->namlen
;
4141 len
= min(snamlen
, DTIHDRDATALEN
);
4142 dih
= (struct idtentry
*) h
;
4143 memcpy(dih
, sih
, 10 + len
* 2);
4150 /* free src head/only segment */
4160 * move additional segment(s) of the entry
4163 while ((ssi
= next
) >= 0) {
4164 /* is next slot contiguous ? */
4165 if (ssi
!= xssi
+ 1) {
4166 /* close current linelock */
4170 /* open new linelock */
4171 if (sdtlck
->index
< sdtlck
->maxcnt
)
4177 slv
= & sdtlck
->lv
[0];
4184 /* get next source segment */
4187 /* get next destination free slot */
4190 len
= min(snamlen
, DTSLOTDATALEN
);
4191 UniStrncpy_le(d
->name
, s
->name
, len
);
4200 /* free source segment */
4209 /* terminate dst last/only segment */
4211 /* single segment entry */
4212 if (dp
->header
.flag
& BT_LEAF
)
4217 /* multi-segment entry */
4221 /* close current linelock */
4230 /* update source header */
4231 sp
->header
.freelist
= sfsi
;
4232 sp
->header
.freecnt
+= nd
;
4234 /* update destination header */
4235 dp
->header
.nextindex
= di
;
4237 dp
->header
.freelist
= dsi
;
4238 dp
->header
.freecnt
-= nd
;
4245 * function: free a (leaf/internal) entry
4247 * log freelist header, stbl, and each segment slot of entry
4248 * (even though last/only segment next field is modified,
4249 * physical image logging requires all segment slots of
4250 * the entry logged to avoid applying previous updates
4251 * to the same slots)
4253 static void dtDeleteEntry(dtpage_t
* p
, int fi
, struct dt_lock
** dtlock
)
4255 int fsi
; /* free entry slot index */
4259 struct dt_lock
*dtlck
= *dtlock
;
4263 /* get free entry slot index */
4264 stbl
= DT_GETSTBL(p
);
4267 /* open new linelock */
4268 if (dtlck
->index
>= dtlck
->maxcnt
)
4269 dtlck
= (struct dt_lock
*) txLinelock(dtlck
);
4270 lv
= & dtlck
->lv
[dtlck
->index
];
4274 /* get the head/only segment */
4276 if (p
->header
.flag
& BT_LEAF
)
4277 si
= ((struct ldtentry
*) t
)->next
;
4279 si
= ((struct idtentry
*) t
)->next
;
4286 /* find the last/only segment */
4288 /* is next slot contiguous ? */
4289 if (si
!= xsi
+ 1) {
4290 /* close current linelock */
4294 /* open new linelock */
4295 if (dtlck
->index
< dtlck
->maxcnt
)
4298 dtlck
= (struct dt_lock
*) txLinelock(dtlck
);
4299 lv
= & dtlck
->lv
[0];
4315 /* close current linelock */
4321 /* update freelist */
4322 t
->next
= p
->header
.freelist
;
4323 p
->header
.freelist
= fsi
;
4324 p
->header
.freecnt
+= freecnt
;
4326 /* if delete from middle,
4327 * shift left the succedding entries in the stbl
4329 si
= p
->header
.nextindex
;
4331 memmove(&stbl
[fi
], &stbl
[fi
+ 1], si
- fi
- 1);
4333 p
->header
.nextindex
--;
4340 * function: truncate a (leaf/internal) entry
4342 * log freelist header, stbl, and each segment slot of entry
4343 * (even though last/only segment next field is modified,
4344 * physical image logging requires all segment slots of
4345 * the entry logged to avoid applying previous updates
4346 * to the same slots)
4348 static void dtTruncateEntry(dtpage_t
* p
, int ti
, struct dt_lock
** dtlock
)
4350 int tsi
; /* truncate entry slot index */
4354 struct dt_lock
*dtlck
= *dtlock
;
4358 /* get free entry slot index */
4359 stbl
= DT_GETSTBL(p
);
4362 /* open new linelock */
4363 if (dtlck
->index
>= dtlck
->maxcnt
)
4364 dtlck
= (struct dt_lock
*) txLinelock(dtlck
);
4365 lv
= & dtlck
->lv
[dtlck
->index
];
4369 /* get the head/only segment */
4371 ASSERT(p
->header
.flag
& BT_INTERNAL
);
4372 ((struct idtentry
*) t
)->namlen
= 0;
4373 si
= ((struct idtentry
*) t
)->next
;
4374 ((struct idtentry
*) t
)->next
= -1;
4381 /* find the last/only segment */
4383 /* is next slot contiguous ? */
4384 if (si
!= xsi
+ 1) {
4385 /* close current linelock */
4389 /* open new linelock */
4390 if (dtlck
->index
< dtlck
->maxcnt
)
4393 dtlck
= (struct dt_lock
*) txLinelock(dtlck
);
4394 lv
= & dtlck
->lv
[0];
4410 /* close current linelock */
4416 /* update freelist */
4419 t
->next
= p
->header
.freelist
;
4420 p
->header
.freelist
= fsi
;
4421 p
->header
.freecnt
+= freecnt
;
4426 * dtLinelockFreelist()
4428 static void dtLinelockFreelist(dtpage_t
* p
, /* directory page */
4429 int m
, /* max slot index */
4430 struct dt_lock
** dtlock
)
4432 int fsi
; /* free entry slot index */
4435 struct dt_lock
*dtlck
= *dtlock
;
4439 /* get free entry slot index */
4440 fsi
= p
->header
.freelist
;
4442 /* open new linelock */
4443 if (dtlck
->index
>= dtlck
->maxcnt
)
4444 dtlck
= (struct dt_lock
*) txLinelock(dtlck
);
4445 lv
= & dtlck
->lv
[dtlck
->index
];
4455 /* find the last/only segment */
4456 while (si
< m
&& si
>= 0) {
4457 /* is next slot contiguous ? */
4458 if (si
!= xsi
+ 1) {
4459 /* close current linelock */
4463 /* open new linelock */
4464 if (dtlck
->index
< dtlck
->maxcnt
)
4467 dtlck
= (struct dt_lock
*) txLinelock(dtlck
);
4468 lv
= & dtlck
->lv
[0];
4482 /* close current linelock */
4493 * FUNCTION: Modify the inode number part of a directory entry
4496 * tid - Transaction id
4497 * ip - Inode of parent directory
4498 * key - Name of entry to be modified
4499 * orig_ino - Original inode number expected in entry
4500 * new_ino - New inode number to put into entry
4504 * -ESTALE - If entry found does not match orig_ino passed in
4505 * -ENOENT - If no entry can be found to match key
4506 * 0 - If successfully modified entry
4508 int dtModify(tid_t tid
, struct inode
*ip
,
4509 struct component_name
* key
, ino_t
* orig_ino
, ino_t new_ino
, int flag
)
4513 struct metapage
*mp
;
4516 struct btstack btstack
;
4518 struct dt_lock
*dtlck
;
4521 int entry_si
; /* entry slot index */
4522 struct ldtentry
*entry
;
4525 * search for the entry to modify:
4527 * dtSearch() returns (leaf page pinned, index at which to modify).
4529 if ((rc
= dtSearch(ip
, key
, orig_ino
, &btstack
, flag
)))
4532 /* retrieve search result */
4533 DT_GETSEARCH(ip
, btstack
.top
, bn
, mp
, p
, index
);
4535 BT_MARK_DIRTY(mp
, ip
);
4537 * acquire a transaction lock on the leaf page of named entry
4539 tlck
= txLock(tid
, ip
, mp
, tlckDTREE
| tlckENTRY
);
4540 dtlck
= (struct dt_lock
*) & tlck
->lock
;
4542 /* get slot index of the entry */
4543 stbl
= DT_GETSTBL(p
);
4544 entry_si
= stbl
[index
];
4546 /* linelock entry */
4547 ASSERT(dtlck
->index
== 0);
4548 lv
= & dtlck
->lv
[0];
4549 lv
->offset
= entry_si
;
4553 /* get the head/only segment */
4554 entry
= (struct ldtentry
*) & p
->slot
[entry_si
];
4556 /* substitute the inode number of the entry */
4557 entry
->inumber
= cpu_to_le32(new_ino
);
4559 /* unpin the leaf page */