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
, u64
*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
;
596 (wchar_t *) kmalloc((JFS_NAME_MAX
+ 1) * sizeof(wchar_t),
598 if (ciKey
.name
== 0) {
604 /* uppercase search key for c-i directory */
605 UniStrcpy(ciKey
.name
, key
->name
);
606 ciKey
.namlen
= key
->namlen
;
608 /* only uppercase if case-insensitive support is on */
609 if ((JFS_SBI(sb
)->mntflag
& JFS_OS2
) == JFS_OS2
) {
612 BT_CLR(btstack
); /* reset stack */
614 /* init level count for max pages to split */
618 * search down tree from root:
620 * between two consecutive entries of <Ki, Pi> and <Kj, Pj> of
621 * internal page, child page Pi contains entry with k, Ki <= K < Kj.
623 * if entry with search key K is not found
624 * internal page search find the entry with largest key Ki
625 * less than K which point to the child page to search;
626 * leaf page search find the entry with smallest key Kj
627 * greater than K so that the returned index is the position of
628 * the entry to be shifted right for insertion of new entry.
629 * for empty tree, search key is greater than any key of the tree.
631 * by convention, root bn = 0.
634 /* get/pin the page to search */
635 DT_GETPAGE(ip
, bn
, mp
, psize
, p
, rc
);
639 /* get sorted entry table of the page */
640 stbl
= DT_GETSTBL(p
);
643 * binary search with search key K on the current page.
645 for (base
= 0, lim
= p
->header
.nextindex
; lim
; lim
>>= 1) {
646 index
= base
+ (lim
>> 1);
648 if (p
->header
.flag
& BT_LEAF
) {
649 /* uppercase leaf name to compare */
651 ciCompare(&ciKey
, p
, stbl
[index
],
652 JFS_SBI(sb
)->mntflag
);
654 /* router key is in uppercase */
656 cmp
= dtCompare(&ciKey
, p
, stbl
[index
]);
664 /* search hit - leaf page:
665 * return the entry found
667 if (p
->header
.flag
& BT_LEAF
) {
668 inumber
= le32_to_cpu(
669 ((struct ldtentry
*) & p
->slot
[stbl
[index
]])->inumber
);
672 * search for JFS_LOOKUP
674 if (flag
== JFS_LOOKUP
) {
681 * search for JFS_CREATE
683 if (flag
== JFS_CREATE
) {
690 * search for JFS_REMOVE or JFS_RENAME
692 if ((flag
== JFS_REMOVE
||
693 flag
== JFS_RENAME
) &&
700 * JFS_REMOVE|JFS_FINDDIR|JFS_RENAME
702 /* save search result */
713 /* search hit - internal page:
714 * descend/search its child page
728 * base is the smallest index with key (Kj) greater than
729 * search key (K) and may be zero or (maxindex + 1) index.
732 * search miss - leaf page
734 * return location of entry (base) where new entry with
735 * search key K is to be inserted.
737 if (p
->header
.flag
& BT_LEAF
) {
739 * search for JFS_LOOKUP, JFS_REMOVE, or JFS_RENAME
741 if (flag
== JFS_LOOKUP
|| flag
== JFS_REMOVE
||
742 flag
== JFS_RENAME
) {
748 * search for JFS_CREATE|JFS_FINDDIR:
763 * search miss - internal page
765 * if base is non-zero, decrement base by one to get the parent
766 * entry of the child page to search.
768 index
= base
? base
- 1 : base
;
771 * go down to child page
774 /* update max. number of pages to split */
775 if (BT_STACK_FULL(btstack
)) {
776 /* Something's corrupted, mark filesytem dirty so
777 * chkdsk will fix it.
779 jfs_error(sb
, "stack overrun in dtSearch!");
780 BT_STACK_DUMP(btstack
);
786 /* push (bn, index) of the parent page/entry */
787 BT_PUSH(btstack
, bn
, index
);
789 /* get the child page block number */
790 pxd
= (pxd_t
*) & p
->slot
[stbl
[index
]];
791 bn
= addressPXD(pxd
);
792 psize
= lengthPXD(pxd
) << JFS_SBI(ip
->i_sb
)->l2bsize
;
794 /* unpin the parent page */
814 * function: insert an entry to directory tree
818 * return: 0 - success;
821 int dtInsert(tid_t tid
, struct inode
*ip
,
822 struct component_name
* name
, ino_t
* fsn
, struct btstack
* btstack
)
825 struct metapage
*mp
; /* meta-page buffer */
826 dtpage_t
*p
; /* base B+-tree index page */
829 struct dtsplit split
; /* split information */
831 struct dt_lock
*dtlck
;
837 * retrieve search result
839 * dtSearch() returns (leaf page pinned, index at which to insert).
840 * n.b. dtSearch() may return index of (maxindex + 1) of
843 DT_GETSEARCH(ip
, btstack
->top
, bn
, mp
, p
, index
);
846 * insert entry for new key
849 if (JFS_IP(ip
)->next_index
== DIREND
) {
853 n
= NDTLEAF(name
->namlen
);
857 n
= NDTLEAF_LEGACY(name
->namlen
);
858 data
.leaf
.ip
= NULL
; /* signifies legacy directory format */
860 data
.leaf
.ino
= *fsn
;
863 * leaf page does not have enough room for new entry:
865 * extend/split the leaf page;
867 * dtSplitUp() will insert the entry and unpin the leaf page.
869 if (n
> p
->header
.freecnt
) {
875 rc
= dtSplitUp(tid
, ip
, &split
, btstack
);
880 * leaf page does have enough room for new entry:
882 * insert the new data entry into the leaf page;
884 BT_MARK_DIRTY(mp
, ip
);
886 * acquire a transaction lock on the leaf page
888 tlck
= txLock(tid
, ip
, mp
, tlckDTREE
| tlckENTRY
);
889 dtlck
= (struct dt_lock
*) & tlck
->lock
;
890 ASSERT(dtlck
->index
== 0);
893 /* linelock header */
898 dtInsertEntry(p
, index
, name
, &data
, &dtlck
);
900 /* linelock stbl of non-root leaf page */
901 if (!(p
->header
.flag
& BT_ROOT
)) {
902 if (dtlck
->index
>= dtlck
->maxcnt
)
903 dtlck
= (struct dt_lock
*) txLinelock(dtlck
);
904 lv
= & dtlck
->lv
[dtlck
->index
];
905 n
= index
>> L2DTSLOTSIZE
;
906 lv
->offset
= p
->header
.stblindex
+ n
;
908 ((p
->header
.nextindex
- 1) >> L2DTSLOTSIZE
) - n
+ 1;
912 /* unpin the leaf page */
922 * function: propagate insertion bottom up;
926 * return: 0 - success;
928 * leaf page unpinned;
930 static int dtSplitUp(tid_t tid
,
931 struct inode
*ip
, struct dtsplit
* split
, struct btstack
* btstack
)
933 struct jfs_sb_info
*sbi
= JFS_SBI(ip
->i_sb
);
935 struct metapage
*smp
;
936 dtpage_t
*sp
; /* split page */
937 struct metapage
*rmp
;
938 dtpage_t
*rp
; /* new right page split from sp */
939 pxd_t rpxd
; /* new right page extent descriptor */
940 struct metapage
*lmp
;
941 dtpage_t
*lp
; /* left child page */
942 int skip
; /* index of entry of insertion */
943 struct btframe
*parent
; /* parent page entry on traverse stack */
946 struct pxdlist pxdlist
;
948 struct component_name key
= { 0, NULL
};
949 ddata_t
*data
= split
->data
;
951 struct dt_lock
*dtlck
;
954 int quota_allocation
= 0;
958 sp
= DT_PAGE(ip
, smp
);
961 (wchar_t *) kmalloc((JFS_NAME_MAX
+ 2) * sizeof(wchar_t),
972 * The split routines insert the new entry, and
973 * acquire txLock as appropriate.
976 * split root leaf page:
978 if (sp
->header
.flag
& BT_ROOT
) {
980 * allocate a single extent child page
983 n
= sbi
->bsize
>> L2DTSLOTSIZE
;
984 n
-= (n
+ 31) >> L2DTSLOTSIZE
; /* stbl size */
985 n
-= DTROOTMAXSLOT
- sp
->header
.freecnt
; /* header + entries */
986 if (n
<= split
->nslot
)
988 if ((rc
= dbAlloc(ip
, 0, (s64
) xlen
, &xaddr
))) {
995 pxd
= &pxdlist
.pxd
[0];
996 PXDaddress(pxd
, xaddr
);
997 PXDlength(pxd
, xlen
);
998 split
->pxdlist
= &pxdlist
;
999 rc
= dtSplitRoot(tid
, ip
, split
, &rmp
);
1002 dbFree(ip
, xaddr
, xlen
);
1009 ip
->i_size
= xlen
<< sbi
->l2bsize
;
1015 * extend first leaf page
1017 * extend the 1st extent if less than buffer page size
1018 * (dtExtendPage() reurns leaf page unpinned)
1020 pxd
= &sp
->header
.self
;
1021 xlen
= lengthPXD(pxd
);
1022 xsize
= xlen
<< sbi
->l2bsize
;
1023 if (xsize
< PSIZE
) {
1024 xaddr
= addressPXD(pxd
);
1025 n
= xsize
>> L2DTSLOTSIZE
;
1026 n
-= (n
+ 31) >> L2DTSLOTSIZE
; /* stbl size */
1027 if ((n
+ sp
->header
.freecnt
) <= split
->nslot
)
1028 n
= xlen
+ (xlen
<< 1);
1032 /* Allocate blocks to quota. */
1033 if (DQUOT_ALLOC_BLOCK(ip
, n
)) {
1037 quota_allocation
+= n
;
1039 if ((rc
= dbReAlloc(sbi
->ipbmap
, xaddr
, (s64
) xlen
,
1043 pxdlist
.maxnpxd
= 1;
1045 pxd
= &pxdlist
.pxd
[0];
1046 PXDaddress(pxd
, nxaddr
)
1047 PXDlength(pxd
, xlen
+ n
);
1048 split
->pxdlist
= &pxdlist
;
1049 if ((rc
= dtExtendPage(tid
, ip
, split
, btstack
))) {
1050 nxaddr
= addressPXD(pxd
);
1051 if (xaddr
!= nxaddr
) {
1052 /* free relocated extent */
1053 xlen
= lengthPXD(pxd
);
1054 dbFree(ip
, nxaddr
, (s64
) xlen
);
1056 /* free extended delta */
1057 xlen
= lengthPXD(pxd
) - n
;
1058 xaddr
= addressPXD(pxd
) + xlen
;
1059 dbFree(ip
, xaddr
, (s64
) n
);
1061 } else if (!DO_INDEX(ip
))
1062 ip
->i_size
= lengthPXD(pxd
) << sbi
->l2bsize
;
1071 * split leaf page <sp> into <sp> and a new right page <rp>.
1073 * return <rp> pinned and its extent descriptor <rpxd>
1076 * allocate new directory page extent and
1077 * new index page(s) to cover page split(s)
1079 * allocation hint: ?
1081 n
= btstack
->nsplit
;
1082 pxdlist
.maxnpxd
= pxdlist
.npxd
= 0;
1083 xlen
= sbi
->nbperpage
;
1084 for (pxd
= pxdlist
.pxd
; n
> 0; n
--, pxd
++) {
1085 if ((rc
= dbAlloc(ip
, 0, (s64
) xlen
, &xaddr
)) == 0) {
1086 PXDaddress(pxd
, xaddr
);
1087 PXDlength(pxd
, xlen
);
1094 /* undo allocation */
1098 split
->pxdlist
= &pxdlist
;
1099 if ((rc
= dtSplitPage(tid
, ip
, split
, &rmp
, &rp
, &rpxd
))) {
1102 /* undo allocation */
1107 ip
->i_size
+= PSIZE
;
1110 * propagate up the router entry for the leaf page just split
1112 * insert a router entry for the new page into the parent page,
1113 * propagate the insert/split up the tree by walking back the stack
1114 * of (bn of parent page, index of child page entry in parent page)
1115 * that were traversed during the search for the page that split.
1117 * the propagation of insert/split up the tree stops if the root
1118 * splits or the page inserted into doesn't have to split to hold
1121 * the parent entry for the split page remains the same, and
1122 * a new entry is inserted at its right with the first key and
1123 * block number of the new right page.
1125 * There are a maximum of 4 pages pinned at any time:
1126 * two children, left parent and right parent (when the parent splits).
1127 * keep the child pages pinned while working on the parent.
1128 * make sure that all pins are released at exit.
1130 while ((parent
= BT_POP(btstack
)) != NULL
) {
1131 /* parent page specified by stack frame <parent> */
1133 /* keep current child pages (<lp>, <rp>) pinned */
1138 * insert router entry in parent for new right child page <rp>
1140 /* get the parent page <sp> */
1141 DT_GETPAGE(ip
, parent
->bn
, smp
, PSIZE
, sp
, rc
);
1149 * The new key entry goes ONE AFTER the index of parent entry,
1150 * because the split was to the right.
1152 skip
= parent
->index
+ 1;
1155 * compute the key for the router entry
1157 * key suffix compression:
1158 * for internal pages that have leaf pages as children,
1159 * retain only what's needed to distinguish between
1160 * the new entry and the entry on the page to its left.
1161 * If the keys compare equal, retain the entire key.
1163 * note that compression is performed only at computing
1164 * router key at the lowest internal level.
1165 * further compression of the key between pairs of higher
1166 * level internal pages loses too much information and
1167 * the search may fail.
1168 * (e.g., two adjacent leaf pages of {a, ..., x} {xx, ...,}
1169 * results in two adjacent parent entries (a)(xx).
1170 * if split occurs between these two entries, and
1171 * if compression is applied, the router key of parent entry
1172 * of right page (x) will divert search for x into right
1173 * subtree and miss x in the left subtree.)
1175 * the entire key must be retained for the next-to-leftmost
1176 * internal key at any level of the tree, or search may fail
1179 switch (rp
->header
.flag
& BT_TYPE
) {
1182 * compute the length of prefix for suffix compression
1183 * between last entry of left page and first entry
1186 if ((sp
->header
.flag
& BT_ROOT
&& skip
> 1) ||
1187 sp
->header
.prev
!= 0 || skip
> 1) {
1188 /* compute uppercase router prefix key */
1189 rc
= ciGetLeafPrefixKey(lp
,
1190 lp
->header
.nextindex
-1,
1200 /* next to leftmost entry of
1201 lowest internal level */
1203 /* compute uppercase router key */
1204 dtGetKey(rp
, 0, &key
, sbi
->mntflag
);
1205 key
.name
[key
.namlen
] = 0;
1207 if ((sbi
->mntflag
& JFS_OS2
) == JFS_OS2
)
1211 n
= NDTINTERNAL(key
.namlen
);
1215 dtGetKey(rp
, 0, &key
, sbi
->mntflag
);
1216 n
= NDTINTERNAL(key
.namlen
);
1220 jfs_err("dtSplitUp(): UFO!");
1224 /* unpin left child page */
1228 * compute the data for the router entry
1230 data
->xd
= rpxd
; /* child page xd */
1233 * parent page is full - split the parent page
1235 if (n
> sp
->header
.freecnt
) {
1236 /* init for parent page split */
1238 split
->index
= skip
; /* index at insert */
1241 /* split->data = data; */
1243 /* unpin right child page */
1246 /* The split routines insert the new entry,
1247 * acquire txLock as appropriate.
1248 * return <rp> pinned and its block number <rbn>.
1250 rc
= (sp
->header
.flag
& BT_ROOT
) ?
1251 dtSplitRoot(tid
, ip
, split
, &rmp
) :
1252 dtSplitPage(tid
, ip
, split
, &rmp
, &rp
, &rpxd
);
1258 /* smp and rmp are pinned */
1261 * parent page is not full - insert router entry in parent page
1264 BT_MARK_DIRTY(smp
, ip
);
1266 * acquire a transaction lock on the parent page
1268 tlck
= txLock(tid
, ip
, smp
, tlckDTREE
| tlckENTRY
);
1269 dtlck
= (struct dt_lock
*) & tlck
->lock
;
1270 ASSERT(dtlck
->index
== 0);
1271 lv
= & dtlck
->lv
[0];
1273 /* linelock header */
1278 /* linelock stbl of non-root parent page */
1279 if (!(sp
->header
.flag
& BT_ROOT
)) {
1281 n
= skip
>> L2DTSLOTSIZE
;
1282 lv
->offset
= sp
->header
.stblindex
+ n
;
1284 ((sp
->header
.nextindex
-
1285 1) >> L2DTSLOTSIZE
) - n
+ 1;
1289 dtInsertEntry(sp
, skip
, &key
, data
, &dtlck
);
1291 /* exit propagate up */
1296 /* unpin current split and its right page */
1301 * free remaining extents allocated for split
1305 pxd
= &pxdlist
.pxd
[n
];
1306 for (; n
< pxdlist
.maxnpxd
; n
++, pxd
++)
1307 dbFree(ip
, addressPXD(pxd
), (s64
) lengthPXD(pxd
));
1312 /* Rollback quota allocation */
1313 if (rc
&& quota_allocation
)
1314 DQUOT_FREE_BLOCK(ip
, quota_allocation
);
1325 * function: Split a non-root page of a btree.
1329 * return: 0 - success;
1331 * return split and new page pinned;
1333 static int dtSplitPage(tid_t tid
, struct inode
*ip
, struct dtsplit
* split
,
1334 struct metapage
** rmpp
, dtpage_t
** rpp
, pxd_t
* rpxdp
)
1337 struct metapage
*smp
;
1339 struct metapage
*rmp
;
1340 dtpage_t
*rp
; /* new right page allocated */
1341 s64 rbn
; /* new right page block number */
1342 struct metapage
*mp
;
1345 struct pxdlist
*pxdlist
;
1347 int skip
, nextindex
, half
, left
, nxt
, off
, si
;
1348 struct ldtentry
*ldtentry
;
1349 struct idtentry
*idtentry
;
1354 struct dt_lock
*sdtlck
, *rdtlck
;
1356 struct dt_lock
*dtlck
;
1357 struct lv
*slv
, *rlv
, *lv
;
1359 /* get split page */
1361 sp
= DT_PAGE(ip
, smp
);
1364 * allocate the new right page for the split
1366 pxdlist
= split
->pxdlist
;
1367 pxd
= &pxdlist
->pxd
[pxdlist
->npxd
];
1369 rbn
= addressPXD(pxd
);
1370 rmp
= get_metapage(ip
, rbn
, PSIZE
, 1);
1374 /* Allocate blocks to quota. */
1375 if (DQUOT_ALLOC_BLOCK(ip
, lengthPXD(pxd
))) {
1376 release_metapage(rmp
);
1380 jfs_info("dtSplitPage: ip:0x%p smp:0x%p rmp:0x%p", ip
, smp
, rmp
);
1382 BT_MARK_DIRTY(rmp
, ip
);
1384 * acquire a transaction lock on the new right page
1386 tlck
= txLock(tid
, ip
, rmp
, tlckDTREE
| tlckNEW
);
1387 rdtlck
= (struct dt_lock
*) & tlck
->lock
;
1389 rp
= (dtpage_t
*) rmp
->data
;
1391 rp
->header
.self
= *pxd
;
1393 BT_MARK_DIRTY(smp
, ip
);
1395 * acquire a transaction lock on the split page
1399 tlck
= txLock(tid
, ip
, smp
, tlckDTREE
| tlckENTRY
);
1400 sdtlck
= (struct dt_lock
*) & tlck
->lock
;
1402 /* linelock header of split page */
1403 ASSERT(sdtlck
->index
== 0);
1404 slv
= & sdtlck
->lv
[0];
1410 * initialize/update sibling pointers between sp and rp
1412 nextbn
= le64_to_cpu(sp
->header
.next
);
1413 rp
->header
.next
= cpu_to_le64(nextbn
);
1414 rp
->header
.prev
= cpu_to_le64(addressPXD(&sp
->header
.self
));
1415 sp
->header
.next
= cpu_to_le64(rbn
);
1418 * initialize new right page
1420 rp
->header
.flag
= sp
->header
.flag
;
1422 /* compute sorted entry table at start of extent data area */
1423 rp
->header
.nextindex
= 0;
1424 rp
->header
.stblindex
= 1;
1426 n
= PSIZE
>> L2DTSLOTSIZE
;
1427 rp
->header
.maxslot
= n
;
1428 stblsize
= (n
+ 31) >> L2DTSLOTSIZE
; /* in unit of slot */
1431 fsi
= rp
->header
.stblindex
+ stblsize
;
1432 rp
->header
.freelist
= fsi
;
1433 rp
->header
.freecnt
= rp
->header
.maxslot
- fsi
;
1436 * sequential append at tail: append without split
1438 * If splitting the last page on a level because of appending
1439 * a entry to it (skip is maxentry), it's likely that the access is
1440 * sequential. Adding an empty page on the side of the level is less
1441 * work and can push the fill factor much higher than normal.
1442 * If we're wrong it's no big deal, we'll just do the split the right
1444 * (It may look like it's equally easy to do a similar hack for
1445 * reverse sorted data, that is, split the tree left,
1446 * but it's not. Be my guest.)
1448 if (nextbn
== 0 && split
->index
== sp
->header
.nextindex
) {
1449 /* linelock header + stbl (first slot) of new page */
1450 rlv
= & rdtlck
->lv
[rdtlck
->index
];
1456 * initialize freelist of new right page
1459 for (fsi
++; fsi
< rp
->header
.maxslot
; f
++, fsi
++)
1463 /* insert entry at the first entry of the new right page */
1464 dtInsertEntry(rp
, 0, split
->key
, split
->data
, &rdtlck
);
1470 * non-sequential insert (at possibly middle page)
1474 * update prev pointer of previous right sibling page;
1477 DT_GETPAGE(ip
, nextbn
, mp
, PSIZE
, p
, rc
);
1479 discard_metapage(rmp
);
1483 BT_MARK_DIRTY(mp
, ip
);
1485 * acquire a transaction lock on the next page
1487 tlck
= txLock(tid
, ip
, mp
, tlckDTREE
| tlckRELINK
);
1488 jfs_info("dtSplitPage: tlck = 0x%p, ip = 0x%p, mp=0x%p",
1490 dtlck
= (struct dt_lock
*) & tlck
->lock
;
1492 /* linelock header of previous right sibling page */
1493 lv
= & dtlck
->lv
[dtlck
->index
];
1498 p
->header
.prev
= cpu_to_le64(rbn
);
1504 * split the data between the split and right pages.
1506 skip
= split
->index
;
1507 half
= (PSIZE
>> L2DTSLOTSIZE
) >> 1; /* swag */
1511 * compute fill factor for split pages
1513 * <nxt> traces the next entry to move to rp
1514 * <off> traces the next entry to stay in sp
1516 stbl
= (u8
*) & sp
->slot
[sp
->header
.stblindex
];
1517 nextindex
= sp
->header
.nextindex
;
1518 for (nxt
= off
= 0; nxt
< nextindex
; ++off
) {
1520 /* check for fill factor with new entry size */
1524 switch (sp
->header
.flag
& BT_TYPE
) {
1526 ldtentry
= (struct ldtentry
*) & sp
->slot
[si
];
1528 n
= NDTLEAF(ldtentry
->namlen
);
1530 n
= NDTLEAF_LEGACY(ldtentry
->
1535 idtentry
= (struct idtentry
*) & sp
->slot
[si
];
1536 n
= NDTINTERNAL(idtentry
->namlen
);
1543 ++nxt
; /* advance to next entry to move in sp */
1551 /* <nxt> poins to the 1st entry to move */
1554 * move entries to right page
1556 * dtMoveEntry() initializes rp and reserves entry for insertion
1558 * split page moved out entries are linelocked;
1559 * new/right page moved in entries are linelocked;
1561 /* linelock header + stbl of new right page */
1562 rlv
= & rdtlck
->lv
[rdtlck
->index
];
1567 dtMoveEntry(sp
, nxt
, rp
, &sdtlck
, &rdtlck
, DO_INDEX(ip
));
1569 sp
->header
.nextindex
= nxt
;
1572 * finalize freelist of new right page
1574 fsi
= rp
->header
.freelist
;
1576 for (fsi
++; fsi
< rp
->header
.maxslot
; f
++, fsi
++)
1581 * Update directory index table for entries now in right page
1583 if ((rp
->header
.flag
& BT_LEAF
) && DO_INDEX(ip
)) {
1587 stbl
= DT_GETSTBL(rp
);
1588 for (n
= 0; n
< rp
->header
.nextindex
; n
++) {
1589 ldtentry
= (struct ldtentry
*) & rp
->slot
[stbl
[n
]];
1590 modify_index(tid
, ip
, le32_to_cpu(ldtentry
->index
),
1591 rbn
, n
, &mp
, &lblock
);
1594 release_metapage(mp
);
1598 * the skipped index was on the left page,
1601 /* insert the new entry in the split page */
1602 dtInsertEntry(sp
, skip
, split
->key
, split
->data
, &sdtlck
);
1604 /* linelock stbl of split page */
1605 if (sdtlck
->index
>= sdtlck
->maxcnt
)
1606 sdtlck
= (struct dt_lock
*) txLinelock(sdtlck
);
1607 slv
= & sdtlck
->lv
[sdtlck
->index
];
1608 n
= skip
>> L2DTSLOTSIZE
;
1609 slv
->offset
= sp
->header
.stblindex
+ n
;
1611 ((sp
->header
.nextindex
- 1) >> L2DTSLOTSIZE
) - n
+ 1;
1615 * the skipped index was on the right page,
1618 /* adjust the skip index to reflect the new position */
1621 /* insert the new entry in the right page */
1622 dtInsertEntry(rp
, skip
, split
->key
, split
->data
, &rdtlck
);
1636 * function: extend 1st/only directory leaf page
1640 * return: 0 - success;
1642 * return extended page pinned;
1644 static int dtExtendPage(tid_t tid
,
1645 struct inode
*ip
, struct dtsplit
* split
, struct btstack
* btstack
)
1647 struct super_block
*sb
= ip
->i_sb
;
1649 struct metapage
*smp
, *pmp
, *mp
;
1651 struct pxdlist
*pxdlist
;
1654 int newstblindex
, newstblsize
;
1655 int oldstblindex
, oldstblsize
;
1658 struct btframe
*parent
;
1660 struct dt_lock
*dtlck
;
1663 struct pxd_lock
*pxdlock
;
1666 struct ldtentry
*ldtentry
;
1669 /* get page to extend */
1671 sp
= DT_PAGE(ip
, smp
);
1673 /* get parent/root page */
1674 parent
= BT_POP(btstack
);
1675 DT_GETPAGE(ip
, parent
->bn
, pmp
, PSIZE
, pp
, rc
);
1682 pxdlist
= split
->pxdlist
;
1683 pxd
= &pxdlist
->pxd
[pxdlist
->npxd
];
1686 xaddr
= addressPXD(pxd
);
1687 tpxd
= &sp
->header
.self
;
1688 txaddr
= addressPXD(tpxd
);
1689 /* in-place extension */
1690 if (xaddr
== txaddr
) {
1697 /* save moved extent descriptor for later free */
1698 tlck
= txMaplock(tid
, ip
, tlckDTREE
| tlckRELOCATE
);
1699 pxdlock
= (struct pxd_lock
*) & tlck
->lock
;
1700 pxdlock
->flag
= mlckFREEPXD
;
1701 pxdlock
->pxd
= sp
->header
.self
;
1705 * Update directory index table to reflect new page address
1711 stbl
= DT_GETSTBL(sp
);
1712 for (n
= 0; n
< sp
->header
.nextindex
; n
++) {
1714 (struct ldtentry
*) & sp
->slot
[stbl
[n
]];
1715 modify_index(tid
, ip
,
1716 le32_to_cpu(ldtentry
->index
),
1717 xaddr
, n
, &mp
, &lblock
);
1720 release_metapage(mp
);
1727 sp
->header
.self
= *pxd
;
1729 jfs_info("dtExtendPage: ip:0x%p smp:0x%p sp:0x%p", ip
, smp
, sp
);
1731 BT_MARK_DIRTY(smp
, ip
);
1733 * acquire a transaction lock on the extended/leaf page
1735 tlck
= txLock(tid
, ip
, smp
, tlckDTREE
| type
);
1736 dtlck
= (struct dt_lock
*) & tlck
->lock
;
1737 lv
= & dtlck
->lv
[0];
1739 /* update buffer extent descriptor of extended page */
1740 xlen
= lengthPXD(pxd
);
1741 xsize
= xlen
<< JFS_SBI(sb
)->l2bsize
;
1742 #ifdef _STILL_TO_PORT
1743 bmSetXD(smp
, xaddr
, xsize
);
1744 #endif /* _STILL_TO_PORT */
1747 * copy old stbl to new stbl at start of extended area
1749 oldstblindex
= sp
->header
.stblindex
;
1750 oldstblsize
= (sp
->header
.maxslot
+ 31) >> L2DTSLOTSIZE
;
1751 newstblindex
= sp
->header
.maxslot
;
1752 n
= xsize
>> L2DTSLOTSIZE
;
1753 newstblsize
= (n
+ 31) >> L2DTSLOTSIZE
;
1754 memcpy(&sp
->slot
[newstblindex
], &sp
->slot
[oldstblindex
],
1755 sp
->header
.nextindex
);
1758 * in-line extension: linelock old area of extended page
1760 if (type
== tlckEXTEND
) {
1761 /* linelock header */
1767 /* linelock new stbl of extended page */
1768 lv
->offset
= newstblindex
;
1769 lv
->length
= newstblsize
;
1772 * relocation: linelock whole relocated area
1776 lv
->length
= sp
->header
.maxslot
+ newstblsize
;
1781 sp
->header
.maxslot
= n
;
1782 sp
->header
.stblindex
= newstblindex
;
1783 /* sp->header.nextindex remains the same */
1786 * add old stbl region at head of freelist
1790 last
= sp
->header
.freelist
;
1791 for (n
= 0; n
< oldstblsize
; n
++, fsi
++, f
++) {
1795 sp
->header
.freelist
= last
;
1796 sp
->header
.freecnt
+= oldstblsize
;
1799 * append free region of newly extended area at tail of freelist
1801 /* init free region of newly extended area */
1802 fsi
= n
= newstblindex
+ newstblsize
;
1804 for (fsi
++; fsi
< sp
->header
.maxslot
; f
++, fsi
++)
1808 /* append new free region at tail of old freelist */
1809 fsi
= sp
->header
.freelist
;
1811 sp
->header
.freelist
= n
;
1816 } while (fsi
!= -1);
1821 sp
->header
.freecnt
+= sp
->header
.maxslot
- n
;
1824 * insert the new entry
1826 dtInsertEntry(sp
, split
->index
, split
->key
, split
->data
, &dtlck
);
1828 BT_MARK_DIRTY(pmp
, ip
);
1830 * linelock any freeslots residing in old extent
1832 if (type
== tlckEXTEND
) {
1833 n
= sp
->header
.maxslot
>> 2;
1834 if (sp
->header
.freelist
< n
)
1835 dtLinelockFreelist(sp
, n
, &dtlck
);
1839 * update parent entry on the parent/root page
1842 * acquire a transaction lock on the parent/root page
1844 tlck
= txLock(tid
, ip
, pmp
, tlckDTREE
| tlckENTRY
);
1845 dtlck
= (struct dt_lock
*) & tlck
->lock
;
1846 lv
= & dtlck
->lv
[dtlck
->index
];
1848 /* linelock parent entry - 1st slot */
1853 /* update the parent pxd for page extension */
1854 tpxd
= (pxd_t
*) & pp
->slot
[1];
1866 * split the full root page into
1867 * original/root/split page and new right page
1868 * i.e., root remains fixed in tree anchor (inode) and
1869 * the root is copied to a single new right child page
1870 * since root page << non-root page, and
1871 * the split root page contains a single entry for the
1872 * new right child page.
1876 * return: 0 - success;
1878 * return new page pinned;
1880 static int dtSplitRoot(tid_t tid
,
1881 struct inode
*ip
, struct dtsplit
* split
, struct metapage
** rmpp
)
1883 struct super_block
*sb
= ip
->i_sb
;
1884 struct metapage
*smp
;
1886 struct metapage
*rmp
;
1893 int fsi
, stblsize
, n
;
1896 struct pxdlist
*pxdlist
;
1898 struct dt_lock
*dtlck
;
1902 /* get split root page */
1904 sp
= &JFS_IP(ip
)->i_dtroot
;
1907 * allocate/initialize a single (right) child page
1909 * N.B. at first split, a one (or two) block to fit new entry
1910 * is allocated; at subsequent split, a full page is allocated;
1912 pxdlist
= split
->pxdlist
;
1913 pxd
= &pxdlist
->pxd
[pxdlist
->npxd
];
1915 rbn
= addressPXD(pxd
);
1916 xlen
= lengthPXD(pxd
);
1917 xsize
= xlen
<< JFS_SBI(sb
)->l2bsize
;
1918 rmp
= get_metapage(ip
, rbn
, xsize
, 1);
1924 /* Allocate blocks to quota. */
1925 if (DQUOT_ALLOC_BLOCK(ip
, lengthPXD(pxd
))) {
1926 release_metapage(rmp
);
1930 BT_MARK_DIRTY(rmp
, ip
);
1932 * acquire a transaction lock on the new right page
1934 tlck
= txLock(tid
, ip
, rmp
, tlckDTREE
| tlckNEW
);
1935 dtlck
= (struct dt_lock
*) & tlck
->lock
;
1938 (sp
->header
.flag
& BT_LEAF
) ? BT_LEAF
: BT_INTERNAL
;
1939 rp
->header
.self
= *pxd
;
1941 /* initialize sibling pointers */
1942 rp
->header
.next
= 0;
1943 rp
->header
.prev
= 0;
1946 * move in-line root page into new right page extent
1948 /* linelock header + copied entries + new stbl (1st slot) in new page */
1949 ASSERT(dtlck
->index
== 0);
1950 lv
= & dtlck
->lv
[0];
1952 lv
->length
= 10; /* 1 + 8 + 1 */
1955 n
= xsize
>> L2DTSLOTSIZE
;
1956 rp
->header
.maxslot
= n
;
1957 stblsize
= (n
+ 31) >> L2DTSLOTSIZE
;
1959 /* copy old stbl to new stbl at start of extended area */
1960 rp
->header
.stblindex
= DTROOTMAXSLOT
;
1961 stbl
= (s8
*) & rp
->slot
[DTROOTMAXSLOT
];
1962 memcpy(stbl
, sp
->header
.stbl
, sp
->header
.nextindex
);
1963 rp
->header
.nextindex
= sp
->header
.nextindex
;
1965 /* copy old data area to start of new data area */
1966 memcpy(&rp
->slot
[1], &sp
->slot
[1], IDATASIZE
);
1969 * append free region of newly extended area at tail of freelist
1971 /* init free region of newly extended area */
1972 fsi
= n
= DTROOTMAXSLOT
+ stblsize
;
1974 for (fsi
++; fsi
< rp
->header
.maxslot
; f
++, fsi
++)
1978 /* append new free region at tail of old freelist */
1979 fsi
= sp
->header
.freelist
;
1981 rp
->header
.freelist
= n
;
1983 rp
->header
.freelist
= fsi
;
1988 } while (fsi
!= -1);
1993 rp
->header
.freecnt
= sp
->header
.freecnt
+ rp
->header
.maxslot
- n
;
1996 * Update directory index table for entries now in right page
1998 if ((rp
->header
.flag
& BT_LEAF
) && DO_INDEX(ip
)) {
2000 struct metapage
*mp
= NULL
;
2001 struct ldtentry
*ldtentry
;
2003 stbl
= DT_GETSTBL(rp
);
2004 for (n
= 0; n
< rp
->header
.nextindex
; n
++) {
2005 ldtentry
= (struct ldtentry
*) & rp
->slot
[stbl
[n
]];
2006 modify_index(tid
, ip
, le32_to_cpu(ldtentry
->index
),
2007 rbn
, n
, &mp
, &lblock
);
2010 release_metapage(mp
);
2013 * insert the new entry into the new right/child page
2014 * (skip index in the new right page will not change)
2016 dtInsertEntry(rp
, split
->index
, split
->key
, split
->data
, &dtlck
);
2019 * reset parent/root page
2021 * set the 1st entry offset to 0, which force the left-most key
2022 * at any level of the tree to be less than any search key.
2024 * The btree comparison code guarantees that the left-most key on any
2025 * level of the tree is never used, so it doesn't need to be filled in.
2027 BT_MARK_DIRTY(smp
, ip
);
2029 * acquire a transaction lock on the root page (in-memory inode)
2031 tlck
= txLock(tid
, ip
, smp
, tlckDTREE
| tlckNEW
| tlckBTROOT
);
2032 dtlck
= (struct dt_lock
*) & tlck
->lock
;
2035 ASSERT(dtlck
->index
== 0);
2036 lv
= & dtlck
->lv
[0];
2038 lv
->length
= DTROOTMAXSLOT
;
2041 /* update page header of root */
2042 if (sp
->header
.flag
& BT_LEAF
) {
2043 sp
->header
.flag
&= ~BT_LEAF
;
2044 sp
->header
.flag
|= BT_INTERNAL
;
2047 /* init the first entry */
2048 s
= (struct idtentry
*) & sp
->slot
[DTENTRYSTART
];
2054 stbl
= sp
->header
.stbl
;
2055 stbl
[0] = DTENTRYSTART
;
2056 sp
->header
.nextindex
= 1;
2059 fsi
= DTENTRYSTART
+ 1;
2062 /* init free region of remaining area */
2063 for (fsi
++; fsi
< DTROOTMAXSLOT
; f
++, fsi
++)
2067 sp
->header
.freelist
= DTENTRYSTART
+ 1;
2068 sp
->header
.freecnt
= DTROOTMAXSLOT
- (DTENTRYSTART
+ 1);
2079 * function: delete the entry(s) referenced by a key.
2085 int dtDelete(tid_t tid
,
2086 struct inode
*ip
, struct component_name
* key
, ino_t
* ino
, int flag
)
2090 struct metapage
*mp
, *imp
;
2093 struct btstack btstack
;
2094 struct dt_lock
*dtlck
;
2098 struct ldtentry
*ldtentry
;
2100 u32 table_index
, next_index
;
2101 struct metapage
*nmp
;
2105 * search for the entry to delete:
2107 * dtSearch() returns (leaf page pinned, index at which to delete).
2109 if ((rc
= dtSearch(ip
, key
, ino
, &btstack
, flag
)))
2112 /* retrieve search result */
2113 DT_GETSEARCH(ip
, btstack
.top
, bn
, mp
, p
, index
);
2116 * We need to find put the index of the next entry into the
2117 * directory index table in order to resume a readdir from this
2121 stbl
= DT_GETSTBL(p
);
2122 ldtentry
= (struct ldtentry
*) & p
->slot
[stbl
[index
]];
2123 table_index
= le32_to_cpu(ldtentry
->index
);
2124 if (index
== (p
->header
.nextindex
- 1)) {
2126 * Last entry in this leaf page
2128 if ((p
->header
.flag
& BT_ROOT
)
2129 || (p
->header
.next
== 0))
2132 /* Read next leaf page */
2133 DT_GETPAGE(ip
, le64_to_cpu(p
->header
.next
),
2134 nmp
, PSIZE
, np
, rc
);
2138 stbl
= DT_GETSTBL(np
);
2140 (struct ldtentry
*) & np
->
2143 le32_to_cpu(ldtentry
->index
);
2149 (struct ldtentry
*) & p
->slot
[stbl
[index
+ 1]];
2150 next_index
= le32_to_cpu(ldtentry
->index
);
2152 free_index(tid
, ip
, table_index
, next_index
);
2155 * the leaf page becomes empty, delete the page
2157 if (p
->header
.nextindex
== 1) {
2158 /* delete empty page */
2159 rc
= dtDeleteUp(tid
, ip
, mp
, p
, &btstack
);
2162 * the leaf page has other entries remaining:
2164 * delete the entry from the leaf page.
2167 BT_MARK_DIRTY(mp
, ip
);
2169 * acquire a transaction lock on the leaf page
2171 tlck
= txLock(tid
, ip
, mp
, tlckDTREE
| tlckENTRY
);
2172 dtlck
= (struct dt_lock
*) & tlck
->lock
;
2175 * Do not assume that dtlck->index will be zero. During a
2176 * rename within a directory, this transaction may have
2177 * modified this page already when adding the new entry.
2180 /* linelock header */
2181 if (dtlck
->index
>= dtlck
->maxcnt
)
2182 dtlck
= (struct dt_lock
*) txLinelock(dtlck
);
2183 lv
= & dtlck
->lv
[dtlck
->index
];
2188 /* linelock stbl of non-root leaf page */
2189 if (!(p
->header
.flag
& BT_ROOT
)) {
2190 if (dtlck
->index
>= dtlck
->maxcnt
)
2191 dtlck
= (struct dt_lock
*) txLinelock(dtlck
);
2192 lv
= & dtlck
->lv
[dtlck
->index
];
2193 i
= index
>> L2DTSLOTSIZE
;
2194 lv
->offset
= p
->header
.stblindex
+ i
;
2196 ((p
->header
.nextindex
- 1) >> L2DTSLOTSIZE
) -
2201 /* free the leaf entry */
2202 dtDeleteEntry(p
, index
, &dtlck
);
2205 * Update directory index table for entries moved in stbl
2207 if (DO_INDEX(ip
) && index
< p
->header
.nextindex
) {
2211 stbl
= DT_GETSTBL(p
);
2212 for (i
= index
; i
< p
->header
.nextindex
; i
++) {
2214 (struct ldtentry
*) & p
->slot
[stbl
[i
]];
2215 modify_index(tid
, ip
,
2216 le32_to_cpu(ldtentry
->index
),
2217 bn
, i
, &imp
, &lblock
);
2220 release_metapage(imp
);
2234 * free empty pages as propagating deletion up the tree
2240 static int dtDeleteUp(tid_t tid
, struct inode
*ip
,
2241 struct metapage
* fmp
, dtpage_t
* fp
, struct btstack
* btstack
)
2244 struct metapage
*mp
;
2246 int index
, nextindex
;
2248 struct btframe
*parent
;
2249 struct dt_lock
*dtlck
;
2252 struct pxd_lock
*pxdlock
;
2256 * keep the root leaf page which has become empty
2258 if (BT_IS_ROOT(fmp
)) {
2262 * dtInitRoot() acquires txlock on the root
2264 dtInitRoot(tid
, ip
, PARENT(ip
));
2272 * free the non-root leaf page
2275 * acquire a transaction lock on the page
2277 * write FREEXTENT|NOREDOPAGE log record
2278 * N.B. linelock is overlaid as freed extent descriptor, and
2279 * the buffer page is freed;
2281 tlck
= txMaplock(tid
, ip
, tlckDTREE
| tlckFREE
);
2282 pxdlock
= (struct pxd_lock
*) & tlck
->lock
;
2283 pxdlock
->flag
= mlckFREEPXD
;
2284 pxdlock
->pxd
= fp
->header
.self
;
2287 /* update sibling pointers */
2288 if ((rc
= dtRelink(tid
, ip
, fp
))) {
2293 xlen
= lengthPXD(&fp
->header
.self
);
2295 /* Free quota allocation. */
2296 DQUOT_FREE_BLOCK(ip
, xlen
);
2298 /* free/invalidate its buffer page */
2299 discard_metapage(fmp
);
2302 * propagate page deletion up the directory tree
2304 * If the delete from the parent page makes it empty,
2305 * continue all the way up the tree.
2306 * stop if the root page is reached (which is never deleted) or
2307 * if the entry deletion does not empty the page.
2309 while ((parent
= BT_POP(btstack
)) != NULL
) {
2310 /* pin the parent page <sp> */
2311 DT_GETPAGE(ip
, parent
->bn
, mp
, PSIZE
, p
, rc
);
2316 * free the extent of the child page deleted
2318 index
= parent
->index
;
2321 * delete the entry for the child page from parent
2323 nextindex
= p
->header
.nextindex
;
2326 * the parent has the single entry being deleted:
2328 * free the parent page which has become empty.
2330 if (nextindex
== 1) {
2332 * keep the root internal page which has become empty
2334 if (p
->header
.flag
& BT_ROOT
) {
2338 * dtInitRoot() acquires txlock on the root
2340 dtInitRoot(tid
, ip
, PARENT(ip
));
2347 * free the parent page
2351 * acquire a transaction lock on the page
2353 * write FREEXTENT|NOREDOPAGE log record
2357 tlckDTREE
| tlckFREE
);
2358 pxdlock
= (struct pxd_lock
*) & tlck
->lock
;
2359 pxdlock
->flag
= mlckFREEPXD
;
2360 pxdlock
->pxd
= p
->header
.self
;
2363 /* update sibling pointers */
2364 if ((rc
= dtRelink(tid
, ip
, p
))) {
2369 xlen
= lengthPXD(&p
->header
.self
);
2371 /* Free quota allocation */
2372 DQUOT_FREE_BLOCK(ip
, xlen
);
2374 /* free/invalidate its buffer page */
2375 discard_metapage(mp
);
2383 * the parent has other entries remaining:
2385 * delete the router entry from the parent page.
2387 BT_MARK_DIRTY(mp
, ip
);
2389 * acquire a transaction lock on the page
2391 * action: router entry deletion
2393 tlck
= txLock(tid
, ip
, mp
, tlckDTREE
| tlckENTRY
);
2394 dtlck
= (struct dt_lock
*) & tlck
->lock
;
2396 /* linelock header */
2397 if (dtlck
->index
>= dtlck
->maxcnt
)
2398 dtlck
= (struct dt_lock
*) txLinelock(dtlck
);
2399 lv
= & dtlck
->lv
[dtlck
->index
];
2404 /* linelock stbl of non-root leaf page */
2405 if (!(p
->header
.flag
& BT_ROOT
)) {
2406 if (dtlck
->index
< dtlck
->maxcnt
)
2409 dtlck
= (struct dt_lock
*) txLinelock(dtlck
);
2410 lv
= & dtlck
->lv
[0];
2412 i
= index
>> L2DTSLOTSIZE
;
2413 lv
->offset
= p
->header
.stblindex
+ i
;
2415 ((p
->header
.nextindex
- 1) >> L2DTSLOTSIZE
) -
2420 /* free the router entry */
2421 dtDeleteEntry(p
, index
, &dtlck
);
2423 /* reset key of new leftmost entry of level (for consistency) */
2425 ((p
->header
.flag
& BT_ROOT
) || p
->header
.prev
== 0))
2426 dtTruncateEntry(p
, 0, &dtlck
);
2428 /* unpin the parent page */
2431 /* exit propagation up */
2436 ip
->i_size
-= PSIZE
;
2443 * NAME: dtRelocate()
2445 * FUNCTION: relocate dtpage (internal or leaf) of directory;
2446 * This function is mainly used by defragfs utility.
2448 int dtRelocate(tid_t tid
, struct inode
*ip
, s64 lmxaddr
, pxd_t
* opxd
,
2452 struct metapage
*mp
, *pmp
, *lmp
, *rmp
;
2453 dtpage_t
*p
, *pp
, *rp
= 0, *lp
= 0;
2456 struct btstack btstack
;
2458 s64 oxaddr
, nextbn
, prevbn
;
2461 struct dt_lock
*dtlck
;
2462 struct pxd_lock
*pxdlock
;
2466 oxaddr
= addressPXD(opxd
);
2467 xlen
= lengthPXD(opxd
);
2469 jfs_info("dtRelocate: lmxaddr:%Ld xaddr:%Ld:%Ld xlen:%d",
2470 (long long)lmxaddr
, (long long)oxaddr
, (long long)nxaddr
,
2474 * 1. get the internal parent dtpage covering
2475 * router entry for the tartget page to be relocated;
2477 rc
= dtSearchNode(ip
, lmxaddr
, opxd
, &btstack
);
2481 /* retrieve search result */
2482 DT_GETSEARCH(ip
, btstack
.top
, bn
, pmp
, pp
, index
);
2483 jfs_info("dtRelocate: parent router entry validated.");
2486 * 2. relocate the target dtpage
2488 /* read in the target page from src extent */
2489 DT_GETPAGE(ip
, oxaddr
, mp
, PSIZE
, p
, rc
);
2491 /* release the pinned parent page */
2497 * read in sibling pages if any to update sibling pointers;
2500 if (p
->header
.next
) {
2501 nextbn
= le64_to_cpu(p
->header
.next
);
2502 DT_GETPAGE(ip
, nextbn
, rmp
, PSIZE
, rp
, rc
);
2511 if (p
->header
.prev
) {
2512 prevbn
= le64_to_cpu(p
->header
.prev
);
2513 DT_GETPAGE(ip
, prevbn
, lmp
, PSIZE
, lp
, rc
);
2523 /* at this point, all xtpages to be updated are in memory */
2526 * update sibling pointers of sibling dtpages if any;
2529 tlck
= txLock(tid
, ip
, lmp
, tlckDTREE
| tlckRELINK
);
2530 dtlck
= (struct dt_lock
*) & tlck
->lock
;
2531 /* linelock header */
2532 ASSERT(dtlck
->index
== 0);
2533 lv
= & dtlck
->lv
[0];
2538 lp
->header
.next
= cpu_to_le64(nxaddr
);
2543 tlck
= txLock(tid
, ip
, rmp
, tlckDTREE
| tlckRELINK
);
2544 dtlck
= (struct dt_lock
*) & tlck
->lock
;
2545 /* linelock header */
2546 ASSERT(dtlck
->index
== 0);
2547 lv
= & dtlck
->lv
[0];
2552 rp
->header
.prev
= cpu_to_le64(nxaddr
);
2557 * update the target dtpage to be relocated
2559 * write LOG_REDOPAGE of LOG_NEW type for dst page
2560 * for the whole target page (logredo() will apply
2561 * after image and update bmap for allocation of the
2562 * dst extent), and update bmap for allocation of
2565 tlck
= txLock(tid
, ip
, mp
, tlckDTREE
| tlckNEW
);
2566 dtlck
= (struct dt_lock
*) & tlck
->lock
;
2567 /* linelock header */
2568 ASSERT(dtlck
->index
== 0);
2569 lv
= & dtlck
->lv
[0];
2571 /* update the self address in the dtpage header */
2572 pxd
= &p
->header
.self
;
2573 PXDaddress(pxd
, nxaddr
);
2575 /* the dst page is the same as the src page, i.e.,
2576 * linelock for afterimage of the whole page;
2579 lv
->length
= p
->header
.maxslot
;
2582 /* update the buffer extent descriptor of the dtpage */
2583 xsize
= xlen
<< JFS_SBI(ip
->i_sb
)->l2bsize
;
2584 #ifdef _STILL_TO_PORT
2585 bmSetXD(mp
, nxaddr
, xsize
);
2586 #endif /* _STILL_TO_PORT */
2587 /* unpin the relocated page */
2589 jfs_info("dtRelocate: target dtpage relocated.");
2591 /* the moved extent is dtpage, then a LOG_NOREDOPAGE log rec
2592 * needs to be written (in logredo(), the LOG_NOREDOPAGE log rec
2593 * will also force a bmap update ).
2597 * 3. acquire maplock for the source extent to be freed;
2599 /* for dtpage relocation, write a LOG_NOREDOPAGE record
2600 * for the source dtpage (logredo() will init NoRedoPage
2601 * filter and will also update bmap for free of the source
2602 * dtpage), and upadte bmap for free of the source dtpage;
2604 tlck
= txMaplock(tid
, ip
, tlckDTREE
| tlckFREE
);
2605 pxdlock
= (struct pxd_lock
*) & tlck
->lock
;
2606 pxdlock
->flag
= mlckFREEPXD
;
2607 PXDaddress(&pxdlock
->pxd
, oxaddr
);
2608 PXDlength(&pxdlock
->pxd
, xlen
);
2612 * 4. update the parent router entry for relocation;
2614 * acquire tlck for the parent entry covering the target dtpage;
2615 * write LOG_REDOPAGE to apply after image only;
2617 jfs_info("dtRelocate: update parent router entry.");
2618 tlck
= txLock(tid
, ip
, pmp
, tlckDTREE
| tlckENTRY
);
2619 dtlck
= (struct dt_lock
*) & tlck
->lock
;
2620 lv
= & dtlck
->lv
[dtlck
->index
];
2622 /* update the PXD with the new address */
2623 stbl
= DT_GETSTBL(pp
);
2624 pxd
= (pxd_t
*) & pp
->slot
[stbl
[index
]];
2625 PXDaddress(pxd
, nxaddr
);
2626 lv
->offset
= stbl
[index
];
2630 /* unpin the parent dtpage */
2637 * NAME: dtSearchNode()
2639 * FUNCTION: Search for an dtpage containing a specified address
2640 * This function is mainly used by defragfs utility.
2642 * NOTE: Search result on stack, the found page is pinned at exit.
2643 * The result page must be an internal dtpage.
2644 * lmxaddr give the address of the left most page of the
2645 * dtree level, in which the required dtpage resides.
2647 static int dtSearchNode(struct inode
*ip
, s64 lmxaddr
, pxd_t
* kpxd
,
2648 struct btstack
* btstack
)
2652 struct metapage
*mp
;
2654 int psize
= 288; /* initial in-line directory */
2658 struct btframe
*btsp
;
2660 BT_CLR(btstack
); /* reset stack */
2663 * descend tree to the level with specified leftmost page
2665 * by convention, root bn = 0.
2668 /* get/pin the page to search */
2669 DT_GETPAGE(ip
, bn
, mp
, psize
, p
, rc
);
2673 /* does the xaddr of leftmost page of the levevl
2674 * matches levevl search key ?
2676 if (p
->header
.flag
& BT_ROOT
) {
2679 } else if (addressPXD(&p
->header
.self
) == lmxaddr
)
2683 * descend down to leftmost child page
2685 if (p
->header
.flag
& BT_LEAF
) {
2690 /* get the leftmost entry */
2691 stbl
= DT_GETSTBL(p
);
2692 pxd
= (pxd_t
*) & p
->slot
[stbl
[0]];
2694 /* get the child page block address */
2695 bn
= addressPXD(pxd
);
2696 psize
= lengthPXD(pxd
) << JFS_SBI(ip
->i_sb
)->l2bsize
;
2697 /* unpin the parent page */
2702 * search each page at the current levevl
2705 stbl
= DT_GETSTBL(p
);
2706 for (i
= 0; i
< p
->header
.nextindex
; i
++) {
2707 pxd
= (pxd_t
*) & p
->slot
[stbl
[i
]];
2709 /* found the specified router entry */
2710 if (addressPXD(pxd
) == addressPXD(kpxd
) &&
2711 lengthPXD(pxd
) == lengthPXD(kpxd
)) {
2712 btsp
= btstack
->top
;
2721 /* get the right sibling page if any */
2723 bn
= le64_to_cpu(p
->header
.next
);
2729 /* unpin current page */
2732 /* get the right sibling page */
2733 DT_GETPAGE(ip
, bn
, mp
, PSIZE
, p
, rc
);
2739 #endif /* _NOTYET */
2745 * link around a freed page.
2748 * fp: page to be freed
2752 static int dtRelink(tid_t tid
, struct inode
*ip
, dtpage_t
* p
)
2755 struct metapage
*mp
;
2758 struct dt_lock
*dtlck
;
2761 nextbn
= le64_to_cpu(p
->header
.next
);
2762 prevbn
= le64_to_cpu(p
->header
.prev
);
2764 /* update prev pointer of the next page */
2766 DT_GETPAGE(ip
, nextbn
, mp
, PSIZE
, p
, rc
);
2770 BT_MARK_DIRTY(mp
, ip
);
2772 * acquire a transaction lock on the next page
2774 * action: update prev pointer;
2776 tlck
= txLock(tid
, ip
, mp
, tlckDTREE
| tlckRELINK
);
2777 jfs_info("dtRelink nextbn: tlck = 0x%p, ip = 0x%p, mp=0x%p",
2779 dtlck
= (struct dt_lock
*) & tlck
->lock
;
2781 /* linelock header */
2782 if (dtlck
->index
>= dtlck
->maxcnt
)
2783 dtlck
= (struct dt_lock
*) txLinelock(dtlck
);
2784 lv
= & dtlck
->lv
[dtlck
->index
];
2789 p
->header
.prev
= cpu_to_le64(prevbn
);
2793 /* update next pointer of the previous page */
2795 DT_GETPAGE(ip
, prevbn
, mp
, PSIZE
, p
, rc
);
2799 BT_MARK_DIRTY(mp
, ip
);
2801 * acquire a transaction lock on the prev page
2803 * action: update next pointer;
2805 tlck
= txLock(tid
, ip
, mp
, tlckDTREE
| tlckRELINK
);
2806 jfs_info("dtRelink prevbn: tlck = 0x%p, ip = 0x%p, mp=0x%p",
2808 dtlck
= (struct dt_lock
*) & tlck
->lock
;
2810 /* linelock header */
2811 if (dtlck
->index
>= dtlck
->maxcnt
)
2812 dtlck
= (struct dt_lock
*) txLinelock(dtlck
);
2813 lv
= & dtlck
->lv
[dtlck
->index
];
2818 p
->header
.next
= cpu_to_le64(nextbn
);
2829 * initialize directory root (inline in inode)
2831 void dtInitRoot(tid_t tid
, struct inode
*ip
, u32 idotdot
)
2833 struct jfs_inode_info
*jfs_ip
= JFS_IP(ip
);
2838 struct dt_lock
*dtlck
;
2843 * If this was previously an non-empty directory, we need to remove
2844 * the old directory table.
2847 if (!jfs_dirtable_inline(ip
)) {
2848 struct tblock
*tblk
= tid_to_tblock(tid
);
2850 * We're playing games with the tid's xflag. If
2851 * we're removing a regular file, the file's xtree
2852 * is committed with COMMIT_PMAP, but we always
2853 * commit the directories xtree with COMMIT_PWMAP.
2855 xflag_save
= tblk
->xflag
;
2858 * xtTruncate isn't guaranteed to fully truncate
2859 * the xtree. The caller needs to check i_size
2860 * after committing the transaction to see if
2861 * additional truncation is needed. The
2862 * COMMIT_Stale flag tells caller that we
2863 * initiated the truncation.
2865 xtTruncate(tid
, ip
, 0, COMMIT_PWMAP
);
2866 set_cflag(COMMIT_Stale
, ip
);
2868 tblk
->xflag
= xflag_save
;
2872 jfs_ip
->next_index
= 2;
2874 ip
->i_size
= IDATASIZE
;
2877 * acquire a transaction lock on the root
2879 * action: directory initialization;
2881 tlck
= txLock(tid
, ip
, (struct metapage
*) & jfs_ip
->bxflag
,
2882 tlckDTREE
| tlckENTRY
| tlckBTROOT
);
2883 dtlck
= (struct dt_lock
*) & tlck
->lock
;
2886 ASSERT(dtlck
->index
== 0);
2887 lv
= & dtlck
->lv
[0];
2889 lv
->length
= DTROOTMAXSLOT
;
2892 p
= &jfs_ip
->i_dtroot
;
2894 p
->header
.flag
= DXD_INDEX
| BT_ROOT
| BT_LEAF
;
2896 p
->header
.nextindex
= 0;
2902 /* init data area of root */
2903 for (fsi
++; fsi
< DTROOTMAXSLOT
; f
++, fsi
++)
2907 p
->header
.freelist
= 1;
2908 p
->header
.freecnt
= 8;
2910 /* init '..' entry */
2911 p
->header
.idotdot
= cpu_to_le32(idotdot
);
2917 * add_missing_indices()
2919 * function: Fix dtree page in which one or more entries has an invalid index.
2920 * fsck.jfs should really fix this, but it currently does not.
2921 * Called from jfs_readdir when bad index is detected.
2923 static void add_missing_indices(struct inode
*inode
, s64 bn
)
2926 struct dt_lock
*dtlck
;
2930 struct metapage
*mp
;
2937 tid
= txBegin(inode
->i_sb
, 0);
2939 DT_GETPAGE(inode
, bn
, mp
, PSIZE
, p
, rc
);
2942 printk(KERN_ERR
"DT_GETPAGE failed!\n");
2945 BT_MARK_DIRTY(mp
, inode
);
2947 ASSERT(p
->header
.flag
& BT_LEAF
);
2949 tlck
= txLock(tid
, inode
, mp
, tlckDTREE
| tlckENTRY
);
2951 tlck
->type
|= tlckBTROOT
;
2953 dtlck
= (struct dt_lock
*) &tlck
->lock
;
2955 stbl
= DT_GETSTBL(p
);
2956 for (i
= 0; i
< p
->header
.nextindex
; i
++) {
2957 d
= (struct ldtentry
*) &p
->slot
[stbl
[i
]];
2958 index
= le32_to_cpu(d
->index
);
2959 if ((index
< 2) || (index
>= JFS_IP(inode
)->next_index
)) {
2960 d
->index
= cpu_to_le32(add_index(tid
, inode
, bn
, i
));
2961 if (dtlck
->index
>= dtlck
->maxcnt
)
2962 dtlck
= (struct dt_lock
*) txLinelock(dtlck
);
2963 lv
= &dtlck
->lv
[dtlck
->index
];
2964 lv
->offset
= stbl
[i
];
2971 (void) txCommit(tid
, 1, &inode
, 0);
2977 * Buffer to hold directory entry info while traversing a dtree page
2978 * before being fed to the filldir function
2988 * function to determine next variable-sized jfs_dirent in buffer
2990 static inline struct jfs_dirent
*next_jfs_dirent(struct jfs_dirent
*dirent
)
2992 return (struct jfs_dirent
*)
2994 ((sizeof (struct jfs_dirent
) + dirent
->name_len
+ 1 +
2995 sizeof (loff_t
) - 1) &
2996 ~(sizeof (loff_t
) - 1)));
3002 * function: read directory entries sequentially
3003 * from the specified entry offset
3007 * return: offset = (pn, index) of start entry
3008 * of next jfs_readdir()/dtRead()
3010 int jfs_readdir(struct file
*filp
, void *dirent
, filldir_t filldir
)
3012 struct inode
*ip
= filp
->f_dentry
->d_inode
;
3013 struct nls_table
*codepage
= JFS_SBI(ip
->i_sb
)->nls_tab
;
3015 loff_t dtpos
; /* legacy OS/2 style position */
3020 } *dtoffset
= (struct dtoffset
*) &dtpos
;
3022 struct metapage
*mp
;
3026 struct btstack btstack
;
3030 int d_namleft
, len
, outlen
;
3031 unsigned long dirent_buf
;
3035 uint loop_count
= 0;
3036 struct jfs_dirent
*jfs_dirent
;
3038 int overflow
, fix_page
, page_fixed
= 0;
3039 static int unique_pos
= 2; /* If we can't fix broken index */
3041 if (filp
->f_pos
== DIREND
)
3046 * persistent index is stored in directory entries.
3047 * Special cases: 0 = .
3049 * -1 = End of directory
3053 dir_index
= (u32
) filp
->f_pos
;
3055 if (dir_index
> 1) {
3056 struct dir_table_slot dirtab_slot
;
3059 (dir_index
>= JFS_IP(ip
)->next_index
)) {
3060 /* Stale position. Directory has shrunk */
3061 filp
->f_pos
= DIREND
;
3065 rc
= read_index(ip
, dir_index
, &dirtab_slot
);
3067 filp
->f_pos
= DIREND
;
3070 if (dirtab_slot
.flag
== DIR_INDEX_FREE
) {
3071 if (loop_count
++ > JFS_IP(ip
)->next_index
) {
3072 jfs_err("jfs_readdir detected "
3074 filp
->f_pos
= DIREND
;
3077 dir_index
= le32_to_cpu(dirtab_slot
.addr2
);
3078 if (dir_index
== -1) {
3079 filp
->f_pos
= DIREND
;
3084 bn
= addressDTS(&dirtab_slot
);
3085 index
= dirtab_slot
.slot
;
3086 DT_GETPAGE(ip
, bn
, mp
, PSIZE
, p
, rc
);
3088 filp
->f_pos
= DIREND
;
3091 if (p
->header
.flag
& BT_INTERNAL
) {
3092 jfs_err("jfs_readdir: bad index table");
3098 if (dir_index
== 0) {
3103 if (filldir(dirent
, ".", 1, 0, ip
->i_ino
,
3111 if (filldir(dirent
, "..", 2, 1, PARENT(ip
), DT_DIR
))
3115 * Find first entry of left-most leaf
3118 filp
->f_pos
= DIREND
;
3122 if ((rc
= dtReadFirst(ip
, &btstack
)))
3125 DT_GETSEARCH(ip
, btstack
.top
, bn
, mp
, p
, index
);
3129 * Legacy filesystem - OS/2 & Linux JFS < 0.3.6
3131 * pn = index = 0: First entry "."
3132 * pn = 0; index = 1: Second entry ".."
3133 * pn > 0: Real entries, pn=1 -> leftmost page
3134 * pn = index = -1: No more entries
3136 dtpos
= filp
->f_pos
;
3138 /* build "." entry */
3140 if (filldir(dirent
, ".", 1, filp
->f_pos
, ip
->i_ino
,
3143 dtoffset
->index
= 1;
3144 filp
->f_pos
= dtpos
;
3147 if (dtoffset
->pn
== 0) {
3148 if (dtoffset
->index
== 1) {
3149 /* build ".." entry */
3151 if (filldir(dirent
, "..", 2, filp
->f_pos
,
3152 PARENT(ip
), DT_DIR
))
3155 jfs_err("jfs_readdir called with "
3159 dtoffset
->index
= 0;
3160 filp
->f_pos
= dtpos
;
3164 filp
->f_pos
= DIREND
;
3168 if ((rc
= dtReadNext(ip
, &filp
->f_pos
, &btstack
))) {
3169 jfs_err("jfs_readdir: unexpected rc = %d "
3170 "from dtReadNext", rc
);
3171 filp
->f_pos
= DIREND
;
3174 /* get start leaf page and index */
3175 DT_GETSEARCH(ip
, btstack
.top
, bn
, mp
, p
, index
);
3177 /* offset beyond directory eof ? */
3179 filp
->f_pos
= DIREND
;
3184 dirent_buf
= __get_free_page(GFP_KERNEL
);
3185 if (dirent_buf
== 0) {
3187 jfs_warn("jfs_readdir: __get_free_page failed!");
3188 filp
->f_pos
= DIREND
;
3193 jfs_dirent
= (struct jfs_dirent
*) dirent_buf
;
3195 overflow
= fix_page
= 0;
3197 stbl
= DT_GETSTBL(p
);
3199 for (i
= index
; i
< p
->header
.nextindex
; i
++) {
3200 d
= (struct ldtentry
*) & p
->slot
[stbl
[i
]];
3202 if (((long) jfs_dirent
+ d
->namlen
+ 1) >
3203 (dirent_buf
+ PAGE_SIZE
)) {
3204 /* DBCS codepages could overrun dirent_buf */
3210 d_namleft
= d
->namlen
;
3211 name_ptr
= jfs_dirent
->name
;
3212 jfs_dirent
->ino
= le32_to_cpu(d
->inumber
);
3215 len
= min(d_namleft
, DTLHDRDATALEN
);
3216 jfs_dirent
->position
= le32_to_cpu(d
->index
);
3218 * d->index should always be valid, but it
3219 * isn't. fsck.jfs doesn't create the
3220 * directory index for the lost+found
3221 * directory. Rather than let it go,
3222 * we can try to fix it.
3224 if ((jfs_dirent
->position
< 2) ||
3225 (jfs_dirent
->position
>=
3226 JFS_IP(ip
)->next_index
)) {
3227 if (!page_fixed
&& !isReadOnly(ip
)) {
3230 * setting overflow and setting
3231 * index to i will cause the
3232 * same page to be processed
3233 * again starting here
3239 jfs_dirent
->position
= unique_pos
++;
3242 jfs_dirent
->position
= dtpos
;
3243 len
= min(d_namleft
, DTLHDRDATALEN_LEGACY
);
3246 /* copy the name of head/only segment */
3247 outlen
= jfs_strfromUCS_le(name_ptr
, d
->name
, len
,
3249 jfs_dirent
->name_len
= outlen
;
3251 /* copy name in the additional segment(s) */
3254 t
= (struct dtslot
*) & p
->slot
[next
];
3258 if (d_namleft
== 0) {
3260 "JFS:Dtree error: ino = "
3261 "%ld, bn=%Ld, index = %d",
3267 len
= min(d_namleft
, DTSLOTDATALEN
);
3268 outlen
= jfs_strfromUCS_le(name_ptr
, t
->name
,
3270 jfs_dirent
->name_len
+= outlen
;
3276 jfs_dirent
= next_jfs_dirent(jfs_dirent
);
3283 /* Point to next leaf page */
3284 if (p
->header
.flag
& BT_ROOT
)
3287 bn
= le64_to_cpu(p
->header
.next
);
3289 /* update offset (pn:index) for new page */
3292 dtoffset
->index
= 0;
3298 /* unpin previous leaf page */
3301 jfs_dirent
= (struct jfs_dirent
*) dirent_buf
;
3302 while (jfs_dirents
--) {
3303 filp
->f_pos
= jfs_dirent
->position
;
3304 if (filldir(dirent
, jfs_dirent
->name
,
3305 jfs_dirent
->name_len
, filp
->f_pos
,
3306 jfs_dirent
->ino
, DT_UNKNOWN
))
3308 jfs_dirent
= next_jfs_dirent(jfs_dirent
);
3312 add_missing_indices(ip
, bn
);
3316 if (!overflow
&& (bn
== 0)) {
3317 filp
->f_pos
= DIREND
;
3321 DT_GETPAGE(ip
, bn
, mp
, PSIZE
, p
, rc
);
3323 free_page(dirent_buf
);
3329 free_page(dirent_buf
);
3338 * function: get the leftmost page of the directory
3340 static int dtReadFirst(struct inode
*ip
, struct btstack
* btstack
)
3344 int psize
= 288; /* initial in-line directory */
3345 struct metapage
*mp
;
3348 struct btframe
*btsp
;
3351 BT_CLR(btstack
); /* reset stack */
3354 * descend leftmost path of the tree
3356 * by convention, root bn = 0.
3359 DT_GETPAGE(ip
, bn
, mp
, psize
, p
, rc
);
3364 * leftmost leaf page
3366 if (p
->header
.flag
& BT_LEAF
) {
3367 /* return leftmost entry */
3368 btsp
= btstack
->top
;
3377 * descend down to leftmost child page
3379 if (BT_STACK_FULL(btstack
)) {
3381 jfs_error(ip
->i_sb
, "dtReadFirst: btstack overrun");
3382 BT_STACK_DUMP(btstack
);
3385 /* push (bn, index) of the parent page/entry */
3386 BT_PUSH(btstack
, bn
, 0);
3388 /* get the leftmost entry */
3389 stbl
= DT_GETSTBL(p
);
3390 xd
= (pxd_t
*) & p
->slot
[stbl
[0]];
3392 /* get the child page block address */
3393 bn
= addressPXD(xd
);
3394 psize
= lengthPXD(xd
) << JFS_SBI(ip
->i_sb
)->l2bsize
;
3396 /* unpin the parent page */
3405 * function: get the page of the specified offset (pn:index)
3407 * return: if (offset > eof), bn = -1;
3409 * note: if index > nextindex of the target leaf page,
3410 * start with 1st entry of next leaf page;
3412 static int dtReadNext(struct inode
*ip
, loff_t
* offset
,
3413 struct btstack
* btstack
)
3420 } *dtoffset
= (struct dtoffset
*) offset
;
3422 struct metapage
*mp
;
3427 struct btframe
*btsp
, *parent
;
3431 * get leftmost leaf page pinned
3433 if ((rc
= dtReadFirst(ip
, btstack
)))
3437 DT_GETSEARCH(ip
, btstack
->top
, bn
, mp
, p
, index
);
3439 /* get the start offset (pn:index) */
3440 pn
= dtoffset
->pn
- 1; /* Now pn = 0 represents leftmost leaf */
3441 index
= dtoffset
->index
;
3443 /* start at leftmost page ? */
3445 /* offset beyond eof ? */
3446 if (index
< p
->header
.nextindex
)
3449 if (p
->header
.flag
& BT_ROOT
) {
3454 /* start with 1st entry of next leaf page */
3456 dtoffset
->index
= index
= 0;
3460 /* start at non-leftmost page: scan parent pages for large pn */
3461 if (p
->header
.flag
& BT_ROOT
) {
3466 /* start after next leaf page ? */
3470 /* get leaf page pn = 1 */
3472 bn
= le64_to_cpu(p
->header
.next
);
3474 /* unpin leaf page */
3477 /* offset beyond eof ? */
3486 * scan last internal page level to get target leaf page
3489 /* unpin leftmost leaf page */
3492 /* get left most parent page */
3493 btsp
= btstack
->top
;
3496 DT_GETPAGE(ip
, bn
, mp
, PSIZE
, p
, rc
);
3500 /* scan parent pages at last internal page level */
3501 while (pn
>= p
->header
.nextindex
) {
3502 pn
-= p
->header
.nextindex
;
3504 /* get next parent page address */
3505 bn
= le64_to_cpu(p
->header
.next
);
3507 /* unpin current parent page */
3510 /* offset beyond eof ? */
3516 /* get next parent page */
3517 DT_GETPAGE(ip
, bn
, mp
, PSIZE
, p
, rc
);
3521 /* update parent page stack frame */
3525 /* get leaf page address */
3526 stbl
= DT_GETSTBL(p
);
3527 xd
= (pxd_t
*) & p
->slot
[stbl
[pn
]];
3528 bn
= addressPXD(xd
);
3530 /* unpin parent page */
3534 * get target leaf page
3537 DT_GETPAGE(ip
, bn
, mp
, PSIZE
, p
, rc
);
3542 * leaf page has been completed:
3543 * start with 1st entry of next leaf page
3545 if (index
>= p
->header
.nextindex
) {
3546 bn
= le64_to_cpu(p
->header
.next
);
3548 /* unpin leaf page */
3551 /* offset beyond eof ? */
3557 /* get next leaf page */
3558 DT_GETPAGE(ip
, bn
, mp
, PSIZE
, p
, rc
);
3562 /* start with 1st entry of next leaf page */
3564 dtoffset
->index
= 0;
3568 /* return target leaf page pinned */
3569 btsp
= btstack
->top
;
3571 btsp
->index
= dtoffset
->index
;
3581 * function: compare search key with an internal entry
3584 * < 0 if k is < record
3585 * = 0 if k is = record
3586 * > 0 if k is > record
3588 static int dtCompare(struct component_name
* key
, /* search key */
3589 dtpage_t
* p
, /* directory page */
3591 { /* entry slot index */
3594 int klen
, namlen
, len
, rc
;
3595 struct idtentry
*ih
;
3599 * force the left-most key on internal pages, at any level of
3600 * the tree, to be less than any search key.
3601 * this obviates having to update the leftmost key on an internal
3602 * page when the user inserts a new key in the tree smaller than
3603 * anything that has been stored.
3605 * (? if/when dtSearch() narrows down to 1st entry (index = 0),
3606 * at any internal page at any level of the tree,
3607 * it descends to child of the entry anyway -
3608 * ? make the entry as min size dummy entry)
3610 * if (e->index == 0 && h->prevpg == P_INVALID && !(h->flags & BT_LEAF))
3617 ih
= (struct idtentry
*) & p
->slot
[si
];
3620 namlen
= ih
->namlen
;
3621 len
= min(namlen
, DTIHDRDATALEN
);
3623 /* compare with head/only segment */
3624 len
= min(klen
, len
);
3625 if ((rc
= UniStrncmp_le(kname
, name
, len
)))
3631 /* compare with additional segment(s) */
3633 while (klen
> 0 && namlen
> 0) {
3634 /* compare with next name segment */
3635 t
= (struct dtslot
*) & p
->slot
[si
];
3636 len
= min(namlen
, DTSLOTDATALEN
);
3637 len
= min(klen
, len
);
3639 if ((rc
= UniStrncmp_le(kname
, name
, len
)))
3648 return (klen
- namlen
);
3657 * function: compare search key with an (leaf/internal) entry
3660 * < 0 if k is < record
3661 * = 0 if k is = record
3662 * > 0 if k is > record
3664 static int ciCompare(struct component_name
* key
, /* search key */
3665 dtpage_t
* p
, /* directory page */
3666 int si
, /* entry slot index */
3671 int klen
, namlen
, len
, rc
;
3672 struct ldtentry
*lh
;
3673 struct idtentry
*ih
;
3678 * force the left-most key on internal pages, at any level of
3679 * the tree, to be less than any search key.
3680 * this obviates having to update the leftmost key on an internal
3681 * page when the user inserts a new key in the tree smaller than
3682 * anything that has been stored.
3684 * (? if/when dtSearch() narrows down to 1st entry (index = 0),
3685 * at any internal page at any level of the tree,
3686 * it descends to child of the entry anyway -
3687 * ? make the entry as min size dummy entry)
3689 * if (e->index == 0 && h->prevpg == P_INVALID && !(h->flags & BT_LEAF))
3699 if (p
->header
.flag
& BT_LEAF
) {
3700 lh
= (struct ldtentry
*) & p
->slot
[si
];
3703 namlen
= lh
->namlen
;
3704 if (flag
& JFS_DIR_INDEX
)
3705 len
= min(namlen
, DTLHDRDATALEN
);
3707 len
= min(namlen
, DTLHDRDATALEN_LEGACY
);
3710 * internal page entry
3713 ih
= (struct idtentry
*) & p
->slot
[si
];
3716 namlen
= ih
->namlen
;
3717 len
= min(namlen
, DTIHDRDATALEN
);
3720 /* compare with head/only segment */
3721 len
= min(klen
, len
);
3722 for (i
= 0; i
< len
; i
++, kname
++, name
++) {
3723 /* only uppercase if case-insensitive support is on */
3724 if ((flag
& JFS_OS2
) == JFS_OS2
)
3725 x
= UniToupper(le16_to_cpu(*name
));
3727 x
= le16_to_cpu(*name
);
3728 if ((rc
= *kname
- x
))
3735 /* compare with additional segment(s) */
3736 while (klen
> 0 && namlen
> 0) {
3737 /* compare with next name segment */
3738 t
= (struct dtslot
*) & p
->slot
[si
];
3739 len
= min(namlen
, DTSLOTDATALEN
);
3740 len
= min(klen
, len
);
3742 for (i
= 0; i
< len
; i
++, kname
++, name
++) {
3743 /* only uppercase if case-insensitive support is on */
3744 if ((flag
& JFS_OS2
) == JFS_OS2
)
3745 x
= UniToupper(le16_to_cpu(*name
));
3747 x
= le16_to_cpu(*name
);
3749 if ((rc
= *kname
- x
))
3758 return (klen
- namlen
);
3763 * ciGetLeafPrefixKey()
3765 * function: compute prefix of suffix compression
3766 * from two adjacent leaf entries
3767 * across page boundary
3769 * return: non-zero on error
3772 static int ciGetLeafPrefixKey(dtpage_t
* lp
, int li
, dtpage_t
* rp
,
3773 int ri
, struct component_name
* key
, int flag
)
3776 wchar_t *pl
, *pr
, *kname
;
3777 struct component_name lkey
;
3778 struct component_name rkey
;
3780 lkey
.name
= (wchar_t *) kmalloc((JFS_NAME_MAX
+ 1) * sizeof(wchar_t),
3782 if (lkey
.name
== NULL
)
3785 rkey
.name
= (wchar_t *) kmalloc((JFS_NAME_MAX
+ 1) * sizeof(wchar_t),
3787 if (rkey
.name
== NULL
) {
3792 /* get left and right key */
3793 dtGetKey(lp
, li
, &lkey
, flag
);
3794 lkey
.name
[lkey
.namlen
] = 0;
3796 if ((flag
& JFS_OS2
) == JFS_OS2
)
3799 dtGetKey(rp
, ri
, &rkey
, flag
);
3800 rkey
.name
[rkey
.namlen
] = 0;
3803 if ((flag
& JFS_OS2
) == JFS_OS2
)
3806 /* compute prefix */
3809 namlen
= min(lkey
.namlen
, rkey
.namlen
);
3810 for (pl
= lkey
.name
, pr
= rkey
.name
;
3811 namlen
; pl
++, pr
++, namlen
--, klen
++, kname
++) {
3814 key
->namlen
= klen
+ 1;
3819 /* l->namlen <= r->namlen since l <= r */
3820 if (lkey
.namlen
< rkey
.namlen
) {
3822 key
->namlen
= klen
+ 1;
3823 } else /* l->namelen == r->namelen */
3837 * function: get key of the entry
3839 static void dtGetKey(dtpage_t
* p
, int i
, /* entry index */
3840 struct component_name
* key
, int flag
)
3844 struct ldtentry
*lh
;
3845 struct idtentry
*ih
;
3852 stbl
= DT_GETSTBL(p
);
3854 if (p
->header
.flag
& BT_LEAF
) {
3855 lh
= (struct ldtentry
*) & p
->slot
[si
];
3857 namlen
= lh
->namlen
;
3859 if (flag
& JFS_DIR_INDEX
)
3860 len
= min(namlen
, DTLHDRDATALEN
);
3862 len
= min(namlen
, DTLHDRDATALEN_LEGACY
);
3864 ih
= (struct idtentry
*) & p
->slot
[si
];
3866 namlen
= ih
->namlen
;
3868 len
= min(namlen
, DTIHDRDATALEN
);
3871 key
->namlen
= namlen
;
3875 * move head/only segment
3877 UniStrncpy_from_le(kname
, name
, len
);
3880 * move additional segment(s)
3883 /* get next segment */
3887 len
= min(namlen
, DTSLOTDATALEN
);
3888 UniStrncpy_from_le(kname
, t
->name
, len
);
3898 * function: allocate free slot(s) and
3899 * write a leaf/internal entry
3901 * return: entry slot index
3903 static void dtInsertEntry(dtpage_t
* p
, int index
, struct component_name
* key
,
3904 ddata_t
* data
, struct dt_lock
** dtlock
)
3906 struct dtslot
*h
, *t
;
3907 struct ldtentry
*lh
= NULL
;
3908 struct idtentry
*ih
= NULL
;
3909 int hsi
, fsi
, klen
, len
, nextindex
;
3914 struct dt_lock
*dtlck
= *dtlock
;
3918 struct metapage
*mp
= NULL
;
3923 /* allocate a free slot */
3924 hsi
= fsi
= p
->header
.freelist
;
3926 p
->header
.freelist
= h
->next
;
3927 --p
->header
.freecnt
;
3929 /* open new linelock */
3930 if (dtlck
->index
>= dtlck
->maxcnt
)
3931 dtlck
= (struct dt_lock
*) txLinelock(dtlck
);
3933 lv
= & dtlck
->lv
[dtlck
->index
];
3936 /* write head/only segment */
3937 if (p
->header
.flag
& BT_LEAF
) {
3938 lh
= (struct ldtentry
*) h
;
3940 lh
->inumber
= cpu_to_le32(data
->leaf
.ino
);
3943 if (data
->leaf
.ip
) {
3944 len
= min(klen
, DTLHDRDATALEN
);
3945 if (!(p
->header
.flag
& BT_ROOT
))
3946 bn
= addressPXD(&p
->header
.self
);
3947 lh
->index
= cpu_to_le32(add_index(data
->leaf
.tid
,
3951 len
= min(klen
, DTLHDRDATALEN_LEGACY
);
3953 ih
= (struct idtentry
*) h
;
3959 len
= min(klen
, DTIHDRDATALEN
);
3962 UniStrncpy_to_le(name
, kname
, len
);
3967 /* write additional segment(s) */
3972 fsi
= p
->header
.freelist
;
3974 p
->header
.freelist
= t
->next
;
3975 --p
->header
.freecnt
;
3977 /* is next slot contiguous ? */
3978 if (fsi
!= xsi
+ 1) {
3979 /* close current linelock */
3983 /* open new linelock */
3984 if (dtlck
->index
< dtlck
->maxcnt
)
3987 dtlck
= (struct dt_lock
*) txLinelock(dtlck
);
3988 lv
= & dtlck
->lv
[0];
3996 len
= min(klen
, DTSLOTDATALEN
);
3997 UniStrncpy_to_le(t
->name
, kname
, len
);
4004 /* close current linelock */
4010 /* terminate last/only segment */
4012 /* single segment entry */
4013 if (p
->header
.flag
& BT_LEAF
)
4018 /* multi-segment entry */
4021 /* if insert into middle, shift right succeeding entries in stbl */
4022 stbl
= DT_GETSTBL(p
);
4023 nextindex
= p
->header
.nextindex
;
4024 if (index
< nextindex
) {
4025 memmove(stbl
+ index
+ 1, stbl
+ index
, nextindex
- index
);
4027 if ((p
->header
.flag
& BT_LEAF
) && data
->leaf
.ip
) {
4031 * Need to update slot number for entries that moved
4035 for (n
= index
+ 1; n
<= nextindex
; n
++) {
4036 lh
= (struct ldtentry
*) & (p
->slot
[stbl
[n
]]);
4037 modify_index(data
->leaf
.tid
, data
->leaf
.ip
,
4038 le32_to_cpu(lh
->index
), bn
, n
,
4042 release_metapage(mp
);
4048 /* advance next available entry index of stbl */
4049 ++p
->header
.nextindex
;
4056 * function: move entries from split/left page to new/right page
4058 * nextindex of dst page and freelist/freecnt of both pages
4061 static void dtMoveEntry(dtpage_t
* sp
, int si
, dtpage_t
* dp
,
4062 struct dt_lock
** sdtlock
, struct dt_lock
** ddtlock
,
4065 int ssi
, next
; /* src slot index */
4066 int di
; /* dst entry index */
4067 int dsi
; /* dst slot index */
4068 s8
*sstbl
, *dstbl
; /* sorted entry table */
4070 struct ldtentry
*slh
, *dlh
= NULL
;
4071 struct idtentry
*sih
, *dih
= NULL
;
4072 struct dtslot
*h
, *s
, *d
;
4073 struct dt_lock
*sdtlck
= *sdtlock
, *ddtlck
= *ddtlock
;
4074 struct lv
*slv
, *dlv
;
4078 sstbl
= (s8
*) & sp
->slot
[sp
->header
.stblindex
];
4079 dstbl
= (s8
*) & dp
->slot
[dp
->header
.stblindex
];
4081 dsi
= dp
->header
.freelist
; /* first (whole page) free slot */
4082 sfsi
= sp
->header
.freelist
;
4084 /* linelock destination entry slot */
4085 dlv
= & ddtlck
->lv
[ddtlck
->index
];
4088 /* linelock source entry slot */
4089 slv
= & sdtlck
->lv
[sdtlck
->index
];
4090 slv
->offset
= sstbl
[si
];
4091 xssi
= slv
->offset
- 1;
4097 for (di
= 0; si
< sp
->header
.nextindex
; si
++, di
++) {
4101 /* is next slot contiguous ? */
4102 if (ssi
!= xssi
+ 1) {
4103 /* close current linelock */
4107 /* open new linelock */
4108 if (sdtlck
->index
< sdtlck
->maxcnt
)
4111 sdtlck
= (struct dt_lock
*) txLinelock(sdtlck
);
4112 slv
= & sdtlck
->lv
[0];
4120 * move head/only segment of an entry
4123 h
= d
= &dp
->slot
[dsi
];
4125 /* get src slot and move */
4127 if (sp
->header
.flag
& BT_LEAF
) {
4128 /* get source entry */
4129 slh
= (struct ldtentry
*) s
;
4130 dlh
= (struct ldtentry
*) h
;
4131 snamlen
= slh
->namlen
;
4134 len
= min(snamlen
, DTLHDRDATALEN
);
4135 dlh
->index
= slh
->index
; /* little-endian */
4137 len
= min(snamlen
, DTLHDRDATALEN_LEGACY
);
4139 memcpy(dlh
, slh
, 6 + len
* 2);
4143 /* update dst head/only segment next field */
4147 sih
= (struct idtentry
*) s
;
4148 snamlen
= sih
->namlen
;
4150 len
= min(snamlen
, DTIHDRDATALEN
);
4151 dih
= (struct idtentry
*) h
;
4152 memcpy(dih
, sih
, 10 + len
* 2);
4159 /* free src head/only segment */
4169 * move additional segment(s) of the entry
4172 while ((ssi
= next
) >= 0) {
4173 /* is next slot contiguous ? */
4174 if (ssi
!= xssi
+ 1) {
4175 /* close current linelock */
4179 /* open new linelock */
4180 if (sdtlck
->index
< sdtlck
->maxcnt
)
4186 slv
= & sdtlck
->lv
[0];
4193 /* get next source segment */
4196 /* get next destination free slot */
4199 len
= min(snamlen
, DTSLOTDATALEN
);
4200 UniStrncpy_le(d
->name
, s
->name
, len
);
4209 /* free source segment */
4218 /* terminate dst last/only segment */
4220 /* single segment entry */
4221 if (dp
->header
.flag
& BT_LEAF
)
4226 /* multi-segment entry */
4230 /* close current linelock */
4239 /* update source header */
4240 sp
->header
.freelist
= sfsi
;
4241 sp
->header
.freecnt
+= nd
;
4243 /* update destination header */
4244 dp
->header
.nextindex
= di
;
4246 dp
->header
.freelist
= dsi
;
4247 dp
->header
.freecnt
-= nd
;
4254 * function: free a (leaf/internal) entry
4256 * log freelist header, stbl, and each segment slot of entry
4257 * (even though last/only segment next field is modified,
4258 * physical image logging requires all segment slots of
4259 * the entry logged to avoid applying previous updates
4260 * to the same slots)
4262 static void dtDeleteEntry(dtpage_t
* p
, int fi
, struct dt_lock
** dtlock
)
4264 int fsi
; /* free entry slot index */
4268 struct dt_lock
*dtlck
= *dtlock
;
4272 /* get free entry slot index */
4273 stbl
= DT_GETSTBL(p
);
4276 /* open new linelock */
4277 if (dtlck
->index
>= dtlck
->maxcnt
)
4278 dtlck
= (struct dt_lock
*) txLinelock(dtlck
);
4279 lv
= & dtlck
->lv
[dtlck
->index
];
4283 /* get the head/only segment */
4285 if (p
->header
.flag
& BT_LEAF
)
4286 si
= ((struct ldtentry
*) t
)->next
;
4288 si
= ((struct idtentry
*) t
)->next
;
4295 /* find the last/only segment */
4297 /* is next slot contiguous ? */
4298 if (si
!= xsi
+ 1) {
4299 /* close current linelock */
4303 /* open new linelock */
4304 if (dtlck
->index
< dtlck
->maxcnt
)
4307 dtlck
= (struct dt_lock
*) txLinelock(dtlck
);
4308 lv
= & dtlck
->lv
[0];
4324 /* close current linelock */
4330 /* update freelist */
4331 t
->next
= p
->header
.freelist
;
4332 p
->header
.freelist
= fsi
;
4333 p
->header
.freecnt
+= freecnt
;
4335 /* if delete from middle,
4336 * shift left the succedding entries in the stbl
4338 si
= p
->header
.nextindex
;
4340 memmove(&stbl
[fi
], &stbl
[fi
+ 1], si
- fi
- 1);
4342 p
->header
.nextindex
--;
4349 * function: truncate a (leaf/internal) entry
4351 * log freelist header, stbl, and each segment slot of entry
4352 * (even though last/only segment next field is modified,
4353 * physical image logging requires all segment slots of
4354 * the entry logged to avoid applying previous updates
4355 * to the same slots)
4357 static void dtTruncateEntry(dtpage_t
* p
, int ti
, struct dt_lock
** dtlock
)
4359 int tsi
; /* truncate entry slot index */
4363 struct dt_lock
*dtlck
= *dtlock
;
4367 /* get free entry slot index */
4368 stbl
= DT_GETSTBL(p
);
4371 /* open new linelock */
4372 if (dtlck
->index
>= dtlck
->maxcnt
)
4373 dtlck
= (struct dt_lock
*) txLinelock(dtlck
);
4374 lv
= & dtlck
->lv
[dtlck
->index
];
4378 /* get the head/only segment */
4380 ASSERT(p
->header
.flag
& BT_INTERNAL
);
4381 ((struct idtentry
*) t
)->namlen
= 0;
4382 si
= ((struct idtentry
*) t
)->next
;
4383 ((struct idtentry
*) t
)->next
= -1;
4390 /* find the last/only segment */
4392 /* is next slot contiguous ? */
4393 if (si
!= xsi
+ 1) {
4394 /* close current linelock */
4398 /* open new linelock */
4399 if (dtlck
->index
< dtlck
->maxcnt
)
4402 dtlck
= (struct dt_lock
*) txLinelock(dtlck
);
4403 lv
= & dtlck
->lv
[0];
4419 /* close current linelock */
4425 /* update freelist */
4428 t
->next
= p
->header
.freelist
;
4429 p
->header
.freelist
= fsi
;
4430 p
->header
.freecnt
+= freecnt
;
4435 * dtLinelockFreelist()
4437 static void dtLinelockFreelist(dtpage_t
* p
, /* directory page */
4438 int m
, /* max slot index */
4439 struct dt_lock
** dtlock
)
4441 int fsi
; /* free entry slot index */
4444 struct dt_lock
*dtlck
= *dtlock
;
4448 /* get free entry slot index */
4449 fsi
= p
->header
.freelist
;
4451 /* open new linelock */
4452 if (dtlck
->index
>= dtlck
->maxcnt
)
4453 dtlck
= (struct dt_lock
*) txLinelock(dtlck
);
4454 lv
= & dtlck
->lv
[dtlck
->index
];
4464 /* find the last/only segment */
4465 while (si
< m
&& si
>= 0) {
4466 /* is next slot contiguous ? */
4467 if (si
!= xsi
+ 1) {
4468 /* close current linelock */
4472 /* open new linelock */
4473 if (dtlck
->index
< dtlck
->maxcnt
)
4476 dtlck
= (struct dt_lock
*) txLinelock(dtlck
);
4477 lv
= & dtlck
->lv
[0];
4491 /* close current linelock */
4502 * FUNCTION: Modify the inode number part of a directory entry
4505 * tid - Transaction id
4506 * ip - Inode of parent directory
4507 * key - Name of entry to be modified
4508 * orig_ino - Original inode number expected in entry
4509 * new_ino - New inode number to put into entry
4513 * -ESTALE - If entry found does not match orig_ino passed in
4514 * -ENOENT - If no entry can be found to match key
4515 * 0 - If successfully modified entry
4517 int dtModify(tid_t tid
, struct inode
*ip
,
4518 struct component_name
* key
, ino_t
* orig_ino
, ino_t new_ino
, int flag
)
4522 struct metapage
*mp
;
4525 struct btstack btstack
;
4527 struct dt_lock
*dtlck
;
4530 int entry_si
; /* entry slot index */
4531 struct ldtentry
*entry
;
4534 * search for the entry to modify:
4536 * dtSearch() returns (leaf page pinned, index at which to modify).
4538 if ((rc
= dtSearch(ip
, key
, orig_ino
, &btstack
, flag
)))
4541 /* retrieve search result */
4542 DT_GETSEARCH(ip
, btstack
.top
, bn
, mp
, p
, index
);
4544 BT_MARK_DIRTY(mp
, ip
);
4546 * acquire a transaction lock on the leaf page of named entry
4548 tlck
= txLock(tid
, ip
, mp
, tlckDTREE
| tlckENTRY
);
4549 dtlck
= (struct dt_lock
*) & tlck
->lock
;
4551 /* get slot index of the entry */
4552 stbl
= DT_GETSTBL(p
);
4553 entry_si
= stbl
[index
];
4555 /* linelock entry */
4556 ASSERT(dtlck
->index
== 0);
4557 lv
= & dtlck
->lv
[0];
4558 lv
->offset
= entry_si
;
4562 /* get the head/only segment */
4563 entry
= (struct ldtentry
*) & p
->slot
[entry_si
];
4565 /* substitute the inode number of the entry */
4566 entry
->inumber
= cpu_to_le32(new_ino
);
4568 /* unpin the leaf page */