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 <linux/slab.h>
106 #include "jfs_incore.h"
107 #include "jfs_superblock.h"
108 #include "jfs_filsys.h"
109 #include "jfs_metapage.h"
110 #include "jfs_dmap.h"
111 #include "jfs_unicode.h"
112 #include "jfs_debug.h"
114 /* dtree split parameter */
119 struct component_name
*key
;
121 struct pxdlist
*pxdlist
;
124 #define DT_PAGE(IP, MP) BT_PAGE(IP, MP, dtpage_t, i_dtroot)
126 /* get page buffer for specified block address */
127 #define DT_GETPAGE(IP, BN, MP, SIZE, P, RC) \
129 BT_GETPAGE(IP, BN, MP, dtpage_t, SIZE, P, RC, i_dtroot); \
131 if (((P)->header.nextindex > \
132 (((BN) == 0) ? DTROOTMAXSLOT : (P)->header.maxslot)) || \
133 ((BN) && ((P)->header.maxslot > DTPAGEMAXSLOT))) { \
135 jfs_error((IP)->i_sb, \
136 "DT_GETPAGE: dtree page corrupt\n"); \
143 /* for consistency */
144 #define DT_PUTPAGE(MP) BT_PUTPAGE(MP)
146 #define DT_GETSEARCH(IP, LEAF, BN, MP, P, INDEX) \
147 BT_GETSEARCH(IP, LEAF, BN, MP, dtpage_t, P, INDEX, i_dtroot)
152 static int dtSplitUp(tid_t tid
, struct inode
*ip
,
153 struct dtsplit
* split
, struct btstack
* btstack
);
155 static int dtSplitPage(tid_t tid
, struct inode
*ip
, struct dtsplit
* split
,
156 struct metapage
** rmpp
, dtpage_t
** rpp
, pxd_t
* rxdp
);
158 static int dtExtendPage(tid_t tid
, struct inode
*ip
,
159 struct dtsplit
* split
, struct btstack
* btstack
);
161 static int dtSplitRoot(tid_t tid
, struct inode
*ip
,
162 struct dtsplit
* split
, struct metapage
** rmpp
);
164 static int dtDeleteUp(tid_t tid
, struct inode
*ip
, struct metapage
* fmp
,
165 dtpage_t
* fp
, struct btstack
* btstack
);
167 static int dtRelink(tid_t tid
, struct inode
*ip
, dtpage_t
* p
);
169 static int dtReadFirst(struct inode
*ip
, struct btstack
* btstack
);
171 static int dtReadNext(struct inode
*ip
,
172 loff_t
* offset
, struct btstack
* btstack
);
174 static int dtCompare(struct component_name
* key
, dtpage_t
* p
, int si
);
176 static int ciCompare(struct component_name
* key
, dtpage_t
* p
, int si
,
179 static void dtGetKey(dtpage_t
* p
, int i
, struct component_name
* key
,
182 static int ciGetLeafPrefixKey(dtpage_t
* lp
, int li
, dtpage_t
* rp
,
183 int ri
, struct component_name
* key
, int flag
);
185 static void dtInsertEntry(dtpage_t
* p
, int index
, struct component_name
* key
,
186 ddata_t
* data
, struct dt_lock
**);
188 static void dtMoveEntry(dtpage_t
* sp
, int si
, dtpage_t
* dp
,
189 struct dt_lock
** sdtlock
, struct dt_lock
** ddtlock
,
192 static void dtDeleteEntry(dtpage_t
* p
, int fi
, struct dt_lock
** dtlock
);
194 static void dtTruncateEntry(dtpage_t
* p
, int ti
, struct dt_lock
** dtlock
);
196 static void dtLinelockFreelist(dtpage_t
* p
, int m
, struct dt_lock
** dtlock
);
198 #define ciToUpper(c) UniStrupr((c)->name)
203 * Reads a page of a directory's index table.
204 * Having metadata mapped into the directory inode's address space
205 * presents a multitude of problems. We avoid this by mapping to
206 * the absolute address space outside of the *_metapage routines
208 static struct metapage
*read_index_page(struct inode
*inode
, s64 blkno
)
215 rc
= xtLookup(inode
, blkno
, 1, &xflag
, &xaddr
, &xlen
, 1);
216 if (rc
|| (xaddr
== 0))
219 return read_metapage(inode
, xaddr
, PSIZE
, 1);
225 * Same as get_index_page(), but get's a new page without reading
227 static struct metapage
*get_index_page(struct inode
*inode
, s64 blkno
)
234 rc
= xtLookup(inode
, blkno
, 1, &xflag
, &xaddr
, &xlen
, 1);
235 if (rc
|| (xaddr
== 0))
238 return get_metapage(inode
, xaddr
, PSIZE
, 1);
244 * Returns dtree page containing directory table entry for specified
245 * index and pointer to its entry.
247 * mp must be released by caller.
249 static struct dir_table_slot
*find_index(struct inode
*ip
, u32 index
,
250 struct metapage
** mp
, s64
*lblock
)
252 struct jfs_inode_info
*jfs_ip
= JFS_IP(ip
);
256 struct dir_table_slot
*slot
;
257 static int maxWarnings
= 10;
261 jfs_warn("find_entry called with index = %d", index
);
267 if (index
>= jfs_ip
->next_index
) {
268 jfs_warn("find_entry called with index >= next_index");
272 if (jfs_dirtable_inline(ip
)) {
274 * Inline directory table
277 slot
= &jfs_ip
->i_dirtable
[index
- 2];
279 offset
= (index
- 2) * sizeof(struct dir_table_slot
);
280 page_offset
= offset
& (PSIZE
- 1);
281 blkno
= ((offset
+ 1) >> L2PSIZE
) <<
282 JFS_SBI(ip
->i_sb
)->l2nbperpage
;
284 if (*mp
&& (*lblock
!= blkno
)) {
285 release_metapage(*mp
);
290 *mp
= read_index_page(ip
, blkno
);
293 jfs_err("free_index: error reading directory table");
298 (struct dir_table_slot
*) ((char *) (*mp
)->data
+
304 static inline void lock_index(tid_t tid
, struct inode
*ip
, struct metapage
* mp
,
308 struct linelock
*llck
;
311 tlck
= txLock(tid
, ip
, mp
, tlckDATA
);
312 llck
= (struct linelock
*) tlck
->lock
;
314 if (llck
->index
>= llck
->maxcnt
)
315 llck
= txLinelock(llck
);
316 lv
= &llck
->lv
[llck
->index
];
319 * Linelock slot size is twice the size of directory table
320 * slot size. 512 entries per page.
322 lv
->offset
= ((index
- 2) & 511) >> 1;
330 * Adds an entry to the directory index table. This is used to provide
331 * each directory entry with a persistent index in which to resume
332 * directory traversals
334 static u32
add_index(tid_t tid
, struct inode
*ip
, s64 bn
, int slot
)
336 struct super_block
*sb
= ip
->i_sb
;
337 struct jfs_sb_info
*sbi
= JFS_SBI(sb
);
338 struct jfs_inode_info
*jfs_ip
= JFS_IP(ip
);
340 struct dir_table_slot
*dirtab_slot
;
342 struct linelock
*llck
;
350 ASSERT(DO_INDEX(ip
));
352 if (jfs_ip
->next_index
< 2) {
353 jfs_warn("add_index: next_index = %d. Resetting!",
355 jfs_ip
->next_index
= 2;
358 index
= jfs_ip
->next_index
++;
360 if (index
<= MAX_INLINE_DIRTABLE_ENTRY
) {
362 * i_size reflects size of index table, or 8 bytes per entry.
364 ip
->i_size
= (loff_t
) (index
- 1) << 3;
367 * dir table fits inline within inode
369 dirtab_slot
= &jfs_ip
->i_dirtable
[index
-2];
370 dirtab_slot
->flag
= DIR_INDEX_VALID
;
371 dirtab_slot
->slot
= slot
;
372 DTSaddress(dirtab_slot
, bn
);
374 set_cflag(COMMIT_Dirtable
, ip
);
378 if (index
== (MAX_INLINE_DIRTABLE_ENTRY
+ 1)) {
379 struct dir_table_slot temp_table
[12];
382 * It's time to move the inline table to an external
383 * page and begin to build the xtree
385 if (dquot_alloc_block(ip
, sbi
->nbperpage
))
387 if (dbAlloc(ip
, 0, sbi
->nbperpage
, &xaddr
)) {
388 dquot_free_block(ip
, sbi
->nbperpage
);
393 * Save the table, we're going to overwrite it with the
396 memcpy(temp_table
, &jfs_ip
->i_dirtable
, sizeof(temp_table
));
399 * Initialize empty x-tree
404 * Add the first block to the xtree
406 if (xtInsert(tid
, ip
, 0, 0, sbi
->nbperpage
, &xaddr
, 0)) {
407 /* This really shouldn't fail */
408 jfs_warn("add_index: xtInsert failed!");
409 memcpy(&jfs_ip
->i_dirtable
, temp_table
,
410 sizeof (temp_table
));
411 dbFree(ip
, xaddr
, sbi
->nbperpage
);
412 dquot_free_block(ip
, sbi
->nbperpage
);
417 mp
= get_index_page(ip
, 0);
419 jfs_err("add_index: get_metapage failed!");
420 xtTruncate(tid
, ip
, 0, COMMIT_PWMAP
);
421 memcpy(&jfs_ip
->i_dirtable
, temp_table
,
422 sizeof (temp_table
));
425 tlck
= txLock(tid
, ip
, mp
, tlckDATA
);
426 llck
= (struct linelock
*) & tlck
->lock
;
427 ASSERT(llck
->index
== 0);
431 lv
->length
= 6; /* tlckDATA slot size is 16 bytes */
434 memcpy(mp
->data
, temp_table
, sizeof(temp_table
));
436 mark_metapage_dirty(mp
);
437 release_metapage(mp
);
440 * Logging is now directed by xtree tlocks
442 clear_cflag(COMMIT_Dirtable
, ip
);
445 offset
= (index
- 2) * sizeof(struct dir_table_slot
);
446 page_offset
= offset
& (PSIZE
- 1);
447 blkno
= ((offset
+ 1) >> L2PSIZE
) << sbi
->l2nbperpage
;
448 if (page_offset
== 0) {
450 * This will be the beginning of a new page
453 if (xtInsert(tid
, ip
, 0, blkno
, sbi
->nbperpage
, &xaddr
, 0)) {
454 jfs_warn("add_index: xtInsert failed!");
459 if ((mp
= get_index_page(ip
, blkno
)))
460 memset(mp
->data
, 0, PSIZE
); /* Just looks better */
462 xtTruncate(tid
, ip
, offset
, COMMIT_PWMAP
);
464 mp
= read_index_page(ip
, blkno
);
467 jfs_err("add_index: get/read_metapage failed!");
471 lock_index(tid
, ip
, mp
, index
);
474 (struct dir_table_slot
*) ((char *) mp
->data
+ page_offset
);
475 dirtab_slot
->flag
= DIR_INDEX_VALID
;
476 dirtab_slot
->slot
= slot
;
477 DTSaddress(dirtab_slot
, bn
);
479 mark_metapage_dirty(mp
);
480 release_metapage(mp
);
486 jfs_ip
->next_index
--;
494 * Marks an entry to the directory index table as free.
496 static void free_index(tid_t tid
, struct inode
*ip
, u32 index
, u32 next
)
498 struct dir_table_slot
*dirtab_slot
;
500 struct metapage
*mp
= NULL
;
502 dirtab_slot
= find_index(ip
, index
, &mp
, &lblock
);
507 dirtab_slot
->flag
= DIR_INDEX_FREE
;
508 dirtab_slot
->slot
= dirtab_slot
->addr1
= 0;
509 dirtab_slot
->addr2
= cpu_to_le32(next
);
512 lock_index(tid
, ip
, mp
, index
);
513 mark_metapage_dirty(mp
);
514 release_metapage(mp
);
516 set_cflag(COMMIT_Dirtable
, ip
);
522 * Changes an entry in the directory index table
524 static void modify_index(tid_t tid
, struct inode
*ip
, u32 index
, s64 bn
,
525 int slot
, struct metapage
** mp
, s64
*lblock
)
527 struct dir_table_slot
*dirtab_slot
;
529 dirtab_slot
= find_index(ip
, index
, mp
, lblock
);
534 DTSaddress(dirtab_slot
, bn
);
535 dirtab_slot
->slot
= slot
;
538 lock_index(tid
, ip
, *mp
, index
);
539 mark_metapage_dirty(*mp
);
541 set_cflag(COMMIT_Dirtable
, ip
);
547 * reads a directory table slot
549 static int read_index(struct inode
*ip
, u32 index
,
550 struct dir_table_slot
* dirtab_slot
)
553 struct metapage
*mp
= NULL
;
554 struct dir_table_slot
*slot
;
556 slot
= find_index(ip
, index
, &mp
, &lblock
);
561 memcpy(dirtab_slot
, slot
, sizeof(struct dir_table_slot
));
564 release_metapage(mp
);
573 * Search for the entry with specified key
577 * return: 0 - search result on stack, leaf page pinned;
580 int dtSearch(struct inode
*ip
, struct component_name
* key
, ino_t
* data
,
581 struct btstack
* btstack
, int flag
)
584 int cmp
= 1; /* init for empty page */
589 int base
, index
, lim
;
590 struct btframe
*btsp
;
592 int psize
= 288; /* initial in-line directory */
594 struct component_name ciKey
;
595 struct super_block
*sb
= ip
->i_sb
;
597 ciKey
.name
= kmalloc_array(JFS_NAME_MAX
+ 1, sizeof(wchar_t),
605 /* uppercase search key for c-i directory */
606 UniStrcpy(ciKey
.name
, key
->name
);
607 ciKey
.namlen
= key
->namlen
;
609 /* only uppercase if case-insensitive support is on */
610 if ((JFS_SBI(sb
)->mntflag
& JFS_OS2
) == JFS_OS2
) {
613 BT_CLR(btstack
); /* reset stack */
615 /* init level count for max pages to split */
619 * search down tree from root:
621 * between two consecutive entries of <Ki, Pi> and <Kj, Pj> of
622 * internal page, child page Pi contains entry with k, Ki <= K < Kj.
624 * if entry with search key K is not found
625 * internal page search find the entry with largest key Ki
626 * less than K which point to the child page to search;
627 * leaf page search find the entry with smallest key Kj
628 * greater than K so that the returned index is the position of
629 * the entry to be shifted right for insertion of new entry.
630 * for empty tree, search key is greater than any key of the tree.
632 * by convention, root bn = 0.
635 /* get/pin the page to search */
636 DT_GETPAGE(ip
, bn
, mp
, psize
, p
, rc
);
640 /* get sorted entry table of the page */
641 stbl
= DT_GETSTBL(p
);
644 * binary search with search key K on the current page.
646 for (base
= 0, lim
= p
->header
.nextindex
; lim
; lim
>>= 1) {
647 index
= base
+ (lim
>> 1);
649 if (p
->header
.flag
& BT_LEAF
) {
650 /* uppercase leaf name to compare */
652 ciCompare(&ciKey
, p
, stbl
[index
],
653 JFS_SBI(sb
)->mntflag
);
655 /* router key is in uppercase */
657 cmp
= dtCompare(&ciKey
, p
, stbl
[index
]);
665 /* search hit - leaf page:
666 * return the entry found
668 if (p
->header
.flag
& BT_LEAF
) {
669 inumber
= le32_to_cpu(
670 ((struct ldtentry
*) & p
->slot
[stbl
[index
]])->inumber
);
673 * search for JFS_LOOKUP
675 if (flag
== JFS_LOOKUP
) {
682 * search for JFS_CREATE
684 if (flag
== JFS_CREATE
) {
691 * search for JFS_REMOVE or JFS_RENAME
693 if ((flag
== JFS_REMOVE
||
694 flag
== JFS_RENAME
) &&
701 * JFS_REMOVE|JFS_FINDDIR|JFS_RENAME
703 /* save search result */
714 /* search hit - internal page:
715 * descend/search its child page
729 * base is the smallest index with key (Kj) greater than
730 * search key (K) and may be zero or (maxindex + 1) index.
733 * search miss - leaf page
735 * return location of entry (base) where new entry with
736 * search key K is to be inserted.
738 if (p
->header
.flag
& BT_LEAF
) {
740 * search for JFS_LOOKUP, JFS_REMOVE, or JFS_RENAME
742 if (flag
== JFS_LOOKUP
|| flag
== JFS_REMOVE
||
743 flag
== JFS_RENAME
) {
749 * search for JFS_CREATE|JFS_FINDDIR:
764 * search miss - internal page
766 * if base is non-zero, decrement base by one to get the parent
767 * entry of the child page to search.
769 index
= base
? base
- 1 : base
;
772 * go down to child page
775 /* update max. number of pages to split */
776 if (BT_STACK_FULL(btstack
)) {
777 /* Something's corrupted, mark filesystem dirty so
778 * chkdsk will fix it.
780 jfs_error(sb
, "stack overrun!\n");
781 BT_STACK_DUMP(btstack
);
787 /* push (bn, index) of the parent page/entry */
788 BT_PUSH(btstack
, bn
, index
);
790 /* get the child page block number */
791 pxd
= (pxd_t
*) & p
->slot
[stbl
[index
]];
792 bn
= addressPXD(pxd
);
793 psize
= lengthPXD(pxd
) << JFS_SBI(ip
->i_sb
)->l2bsize
;
795 /* unpin the parent page */
815 * function: insert an entry to directory tree
819 * return: 0 - success;
822 int dtInsert(tid_t tid
, struct inode
*ip
,
823 struct component_name
* name
, ino_t
* fsn
, struct btstack
* btstack
)
826 struct metapage
*mp
; /* meta-page buffer */
827 dtpage_t
*p
; /* base B+-tree index page */
830 struct dtsplit split
; /* split information */
832 struct dt_lock
*dtlck
;
838 * retrieve search result
840 * dtSearch() returns (leaf page pinned, index at which to insert).
841 * n.b. dtSearch() may return index of (maxindex + 1) of
844 DT_GETSEARCH(ip
, btstack
->top
, bn
, mp
, p
, index
);
847 * insert entry for new key
850 if (JFS_IP(ip
)->next_index
== DIREND
) {
854 n
= NDTLEAF(name
->namlen
);
858 n
= NDTLEAF_LEGACY(name
->namlen
);
859 data
.leaf
.ip
= NULL
; /* signifies legacy directory format */
861 data
.leaf
.ino
= *fsn
;
864 * leaf page does not have enough room for new entry:
866 * extend/split the leaf page;
868 * dtSplitUp() will insert the entry and unpin the leaf page.
870 if (n
> p
->header
.freecnt
) {
876 rc
= dtSplitUp(tid
, ip
, &split
, btstack
);
881 * leaf page does have enough room for new entry:
883 * insert the new data entry into the leaf page;
885 BT_MARK_DIRTY(mp
, ip
);
887 * acquire a transaction lock on the leaf page
889 tlck
= txLock(tid
, ip
, mp
, tlckDTREE
| tlckENTRY
);
890 dtlck
= (struct dt_lock
*) & tlck
->lock
;
891 ASSERT(dtlck
->index
== 0);
894 /* linelock header */
899 dtInsertEntry(p
, index
, name
, &data
, &dtlck
);
901 /* linelock stbl of non-root leaf page */
902 if (!(p
->header
.flag
& BT_ROOT
)) {
903 if (dtlck
->index
>= dtlck
->maxcnt
)
904 dtlck
= (struct dt_lock
*) txLinelock(dtlck
);
905 lv
= & dtlck
->lv
[dtlck
->index
];
906 n
= index
>> L2DTSLOTSIZE
;
907 lv
->offset
= p
->header
.stblindex
+ n
;
909 ((p
->header
.nextindex
- 1) >> L2DTSLOTSIZE
) - n
+ 1;
913 /* unpin the leaf page */
923 * function: propagate insertion bottom up;
927 * return: 0 - success;
929 * leaf page unpinned;
931 static int dtSplitUp(tid_t tid
,
932 struct inode
*ip
, struct dtsplit
* split
, struct btstack
* btstack
)
934 struct jfs_sb_info
*sbi
= JFS_SBI(ip
->i_sb
);
936 struct metapage
*smp
;
937 dtpage_t
*sp
; /* split page */
938 struct metapage
*rmp
;
939 dtpage_t
*rp
; /* new right page split from sp */
940 pxd_t rpxd
; /* new right page extent descriptor */
941 struct metapage
*lmp
;
942 dtpage_t
*lp
; /* left child page */
943 int skip
; /* index of entry of insertion */
944 struct btframe
*parent
; /* parent page entry on traverse stack */
947 struct pxdlist pxdlist
;
949 struct component_name key
= { 0, NULL
};
950 ddata_t
*data
= split
->data
;
952 struct dt_lock
*dtlck
;
955 int quota_allocation
= 0;
959 sp
= DT_PAGE(ip
, smp
);
961 key
.name
= kmalloc_array(JFS_NAME_MAX
+ 2, sizeof(wchar_t), GFP_NOFS
);
971 * The split routines insert the new entry, and
972 * acquire txLock as appropriate.
975 * split root leaf page:
977 if (sp
->header
.flag
& BT_ROOT
) {
979 * allocate a single extent child page
982 n
= sbi
->bsize
>> L2DTSLOTSIZE
;
983 n
-= (n
+ 31) >> L2DTSLOTSIZE
; /* stbl size */
984 n
-= DTROOTMAXSLOT
- sp
->header
.freecnt
; /* header + entries */
985 if (n
<= split
->nslot
)
987 if ((rc
= dbAlloc(ip
, 0, (s64
) xlen
, &xaddr
))) {
994 pxd
= &pxdlist
.pxd
[0];
995 PXDaddress(pxd
, xaddr
);
996 PXDlength(pxd
, xlen
);
997 split
->pxdlist
= &pxdlist
;
998 rc
= dtSplitRoot(tid
, ip
, split
, &rmp
);
1001 dbFree(ip
, xaddr
, xlen
);
1008 ip
->i_size
= xlen
<< sbi
->l2bsize
;
1014 * extend first leaf page
1016 * extend the 1st extent if less than buffer page size
1017 * (dtExtendPage() reurns leaf page unpinned)
1019 pxd
= &sp
->header
.self
;
1020 xlen
= lengthPXD(pxd
);
1021 xsize
= xlen
<< sbi
->l2bsize
;
1022 if (xsize
< PSIZE
) {
1023 xaddr
= addressPXD(pxd
);
1024 n
= xsize
>> L2DTSLOTSIZE
;
1025 n
-= (n
+ 31) >> L2DTSLOTSIZE
; /* stbl size */
1026 if ((n
+ sp
->header
.freecnt
) <= split
->nslot
)
1027 n
= xlen
+ (xlen
<< 1);
1031 /* Allocate blocks to quota. */
1032 rc
= dquot_alloc_block(ip
, n
);
1035 quota_allocation
+= n
;
1037 if ((rc
= dbReAlloc(sbi
->ipbmap
, xaddr
, (s64
) xlen
,
1041 pxdlist
.maxnpxd
= 1;
1043 pxd
= &pxdlist
.pxd
[0];
1044 PXDaddress(pxd
, nxaddr
);
1045 PXDlength(pxd
, xlen
+ n
);
1046 split
->pxdlist
= &pxdlist
;
1047 if ((rc
= dtExtendPage(tid
, ip
, split
, btstack
))) {
1048 nxaddr
= addressPXD(pxd
);
1049 if (xaddr
!= nxaddr
) {
1050 /* free relocated extent */
1051 xlen
= lengthPXD(pxd
);
1052 dbFree(ip
, nxaddr
, (s64
) xlen
);
1054 /* free extended delta */
1055 xlen
= lengthPXD(pxd
) - n
;
1056 xaddr
= addressPXD(pxd
) + xlen
;
1057 dbFree(ip
, xaddr
, (s64
) n
);
1059 } else if (!DO_INDEX(ip
))
1060 ip
->i_size
= lengthPXD(pxd
) << sbi
->l2bsize
;
1069 * split leaf page <sp> into <sp> and a new right page <rp>.
1071 * return <rp> pinned and its extent descriptor <rpxd>
1074 * allocate new directory page extent and
1075 * new index page(s) to cover page split(s)
1077 * allocation hint: ?
1079 n
= btstack
->nsplit
;
1080 pxdlist
.maxnpxd
= pxdlist
.npxd
= 0;
1081 xlen
= sbi
->nbperpage
;
1082 for (pxd
= pxdlist
.pxd
; n
> 0; n
--, pxd
++) {
1083 if ((rc
= dbAlloc(ip
, 0, (s64
) xlen
, &xaddr
)) == 0) {
1084 PXDaddress(pxd
, xaddr
);
1085 PXDlength(pxd
, xlen
);
1092 /* undo allocation */
1096 split
->pxdlist
= &pxdlist
;
1097 if ((rc
= dtSplitPage(tid
, ip
, split
, &rmp
, &rp
, &rpxd
))) {
1100 /* undo allocation */
1105 ip
->i_size
+= PSIZE
;
1108 * propagate up the router entry for the leaf page just split
1110 * insert a router entry for the new page into the parent page,
1111 * propagate the insert/split up the tree by walking back the stack
1112 * of (bn of parent page, index of child page entry in parent page)
1113 * that were traversed during the search for the page that split.
1115 * the propagation of insert/split up the tree stops if the root
1116 * splits or the page inserted into doesn't have to split to hold
1119 * the parent entry for the split page remains the same, and
1120 * a new entry is inserted at its right with the first key and
1121 * block number of the new right page.
1123 * There are a maximum of 4 pages pinned at any time:
1124 * two children, left parent and right parent (when the parent splits).
1125 * keep the child pages pinned while working on the parent.
1126 * make sure that all pins are released at exit.
1128 while ((parent
= BT_POP(btstack
)) != NULL
) {
1129 /* parent page specified by stack frame <parent> */
1131 /* keep current child pages (<lp>, <rp>) pinned */
1136 * insert router entry in parent for new right child page <rp>
1138 /* get the parent page <sp> */
1139 DT_GETPAGE(ip
, parent
->bn
, smp
, PSIZE
, sp
, rc
);
1147 * The new key entry goes ONE AFTER the index of parent entry,
1148 * because the split was to the right.
1150 skip
= parent
->index
+ 1;
1153 * compute the key for the router entry
1155 * key suffix compression:
1156 * for internal pages that have leaf pages as children,
1157 * retain only what's needed to distinguish between
1158 * the new entry and the entry on the page to its left.
1159 * If the keys compare equal, retain the entire key.
1161 * note that compression is performed only at computing
1162 * router key at the lowest internal level.
1163 * further compression of the key between pairs of higher
1164 * level internal pages loses too much information and
1165 * the search may fail.
1166 * (e.g., two adjacent leaf pages of {a, ..., x} {xx, ...,}
1167 * results in two adjacent parent entries (a)(xx).
1168 * if split occurs between these two entries, and
1169 * if compression is applied, the router key of parent entry
1170 * of right page (x) will divert search for x into right
1171 * subtree and miss x in the left subtree.)
1173 * the entire key must be retained for the next-to-leftmost
1174 * internal key at any level of the tree, or search may fail
1177 switch (rp
->header
.flag
& BT_TYPE
) {
1180 * compute the length of prefix for suffix compression
1181 * between last entry of left page and first entry
1184 if ((sp
->header
.flag
& BT_ROOT
&& skip
> 1) ||
1185 sp
->header
.prev
!= 0 || skip
> 1) {
1186 /* compute uppercase router prefix key */
1187 rc
= ciGetLeafPrefixKey(lp
,
1188 lp
->header
.nextindex
-1,
1198 /* next to leftmost entry of
1199 lowest internal level */
1201 /* compute uppercase router key */
1202 dtGetKey(rp
, 0, &key
, sbi
->mntflag
);
1203 key
.name
[key
.namlen
] = 0;
1205 if ((sbi
->mntflag
& JFS_OS2
) == JFS_OS2
)
1209 n
= NDTINTERNAL(key
.namlen
);
1213 dtGetKey(rp
, 0, &key
, sbi
->mntflag
);
1214 n
= NDTINTERNAL(key
.namlen
);
1218 jfs_err("dtSplitUp(): UFO!");
1222 /* unpin left child page */
1226 * compute the data for the router entry
1228 data
->xd
= rpxd
; /* child page xd */
1231 * parent page is full - split the parent page
1233 if (n
> sp
->header
.freecnt
) {
1234 /* init for parent page split */
1236 split
->index
= skip
; /* index at insert */
1239 /* split->data = data; */
1241 /* unpin right child page */
1244 /* The split routines insert the new entry,
1245 * acquire txLock as appropriate.
1246 * return <rp> pinned and its block number <rbn>.
1248 rc
= (sp
->header
.flag
& BT_ROOT
) ?
1249 dtSplitRoot(tid
, ip
, split
, &rmp
) :
1250 dtSplitPage(tid
, ip
, split
, &rmp
, &rp
, &rpxd
);
1256 /* smp and rmp are pinned */
1259 * parent page is not full - insert router entry in parent page
1262 BT_MARK_DIRTY(smp
, ip
);
1264 * acquire a transaction lock on the parent page
1266 tlck
= txLock(tid
, ip
, smp
, tlckDTREE
| tlckENTRY
);
1267 dtlck
= (struct dt_lock
*) & tlck
->lock
;
1268 ASSERT(dtlck
->index
== 0);
1269 lv
= & dtlck
->lv
[0];
1271 /* linelock header */
1276 /* linelock stbl of non-root parent page */
1277 if (!(sp
->header
.flag
& BT_ROOT
)) {
1279 n
= skip
>> L2DTSLOTSIZE
;
1280 lv
->offset
= sp
->header
.stblindex
+ n
;
1282 ((sp
->header
.nextindex
-
1283 1) >> L2DTSLOTSIZE
) - n
+ 1;
1287 dtInsertEntry(sp
, skip
, &key
, data
, &dtlck
);
1289 /* exit propagate up */
1294 /* unpin current split and its right page */
1299 * free remaining extents allocated for split
1303 pxd
= &pxdlist
.pxd
[n
];
1304 for (; n
< pxdlist
.maxnpxd
; n
++, pxd
++)
1305 dbFree(ip
, addressPXD(pxd
), (s64
) lengthPXD(pxd
));
1310 /* Rollback quota allocation */
1311 if (rc
&& quota_allocation
)
1312 dquot_free_block(ip
, quota_allocation
);
1323 * function: Split a non-root page of a btree.
1327 * return: 0 - success;
1329 * return split and new page pinned;
1331 static int dtSplitPage(tid_t tid
, struct inode
*ip
, struct dtsplit
* split
,
1332 struct metapage
** rmpp
, dtpage_t
** rpp
, pxd_t
* rpxdp
)
1335 struct metapage
*smp
;
1337 struct metapage
*rmp
;
1338 dtpage_t
*rp
; /* new right page allocated */
1339 s64 rbn
; /* new right page block number */
1340 struct metapage
*mp
;
1343 struct pxdlist
*pxdlist
;
1345 int skip
, nextindex
, half
, left
, nxt
, off
, si
;
1346 struct ldtentry
*ldtentry
;
1347 struct idtentry
*idtentry
;
1352 struct dt_lock
*sdtlck
, *rdtlck
;
1354 struct dt_lock
*dtlck
;
1355 struct lv
*slv
, *rlv
, *lv
;
1357 /* get split page */
1359 sp
= DT_PAGE(ip
, smp
);
1362 * allocate the new right page for the split
1364 pxdlist
= split
->pxdlist
;
1365 pxd
= &pxdlist
->pxd
[pxdlist
->npxd
];
1367 rbn
= addressPXD(pxd
);
1368 rmp
= get_metapage(ip
, rbn
, PSIZE
, 1);
1372 /* Allocate blocks to quota. */
1373 rc
= dquot_alloc_block(ip
, lengthPXD(pxd
));
1375 release_metapage(rmp
);
1379 jfs_info("dtSplitPage: ip:0x%p smp:0x%p rmp:0x%p", ip
, smp
, rmp
);
1381 BT_MARK_DIRTY(rmp
, ip
);
1383 * acquire a transaction lock on the new right page
1385 tlck
= txLock(tid
, ip
, rmp
, tlckDTREE
| tlckNEW
);
1386 rdtlck
= (struct dt_lock
*) & tlck
->lock
;
1388 rp
= (dtpage_t
*) rmp
->data
;
1390 rp
->header
.self
= *pxd
;
1392 BT_MARK_DIRTY(smp
, ip
);
1394 * acquire a transaction lock on the split page
1398 tlck
= txLock(tid
, ip
, smp
, tlckDTREE
| tlckENTRY
);
1399 sdtlck
= (struct dt_lock
*) & tlck
->lock
;
1401 /* linelock header of split page */
1402 ASSERT(sdtlck
->index
== 0);
1403 slv
= & sdtlck
->lv
[0];
1409 * initialize/update sibling pointers between sp and rp
1411 nextbn
= le64_to_cpu(sp
->header
.next
);
1412 rp
->header
.next
= cpu_to_le64(nextbn
);
1413 rp
->header
.prev
= cpu_to_le64(addressPXD(&sp
->header
.self
));
1414 sp
->header
.next
= cpu_to_le64(rbn
);
1417 * initialize new right page
1419 rp
->header
.flag
= sp
->header
.flag
;
1421 /* compute sorted entry table at start of extent data area */
1422 rp
->header
.nextindex
= 0;
1423 rp
->header
.stblindex
= 1;
1425 n
= PSIZE
>> L2DTSLOTSIZE
;
1426 rp
->header
.maxslot
= n
;
1427 stblsize
= (n
+ 31) >> L2DTSLOTSIZE
; /* in unit of slot */
1430 fsi
= rp
->header
.stblindex
+ stblsize
;
1431 rp
->header
.freelist
= fsi
;
1432 rp
->header
.freecnt
= rp
->header
.maxslot
- fsi
;
1435 * sequential append at tail: append without split
1437 * If splitting the last page on a level because of appending
1438 * a entry to it (skip is maxentry), it's likely that the access is
1439 * sequential. Adding an empty page on the side of the level is less
1440 * work and can push the fill factor much higher than normal.
1441 * If we're wrong it's no big deal, we'll just do the split the right
1443 * (It may look like it's equally easy to do a similar hack for
1444 * reverse sorted data, that is, split the tree left,
1445 * but it's not. Be my guest.)
1447 if (nextbn
== 0 && split
->index
== sp
->header
.nextindex
) {
1448 /* linelock header + stbl (first slot) of new page */
1449 rlv
= & rdtlck
->lv
[rdtlck
->index
];
1455 * initialize freelist of new right page
1458 for (fsi
++; fsi
< rp
->header
.maxslot
; f
++, fsi
++)
1462 /* insert entry at the first entry of the new right page */
1463 dtInsertEntry(rp
, 0, split
->key
, split
->data
, &rdtlck
);
1469 * non-sequential insert (at possibly middle page)
1473 * update prev pointer of previous right sibling page;
1476 DT_GETPAGE(ip
, nextbn
, mp
, PSIZE
, p
, rc
);
1478 discard_metapage(rmp
);
1482 BT_MARK_DIRTY(mp
, ip
);
1484 * acquire a transaction lock on the next page
1486 tlck
= txLock(tid
, ip
, mp
, tlckDTREE
| tlckRELINK
);
1487 jfs_info("dtSplitPage: tlck = 0x%p, ip = 0x%p, mp=0x%p",
1489 dtlck
= (struct dt_lock
*) & tlck
->lock
;
1491 /* linelock header of previous right sibling page */
1492 lv
= & dtlck
->lv
[dtlck
->index
];
1497 p
->header
.prev
= cpu_to_le64(rbn
);
1503 * split the data between the split and right pages.
1505 skip
= split
->index
;
1506 half
= (PSIZE
>> L2DTSLOTSIZE
) >> 1; /* swag */
1510 * compute fill factor for split pages
1512 * <nxt> traces the next entry to move to rp
1513 * <off> traces the next entry to stay in sp
1515 stbl
= (u8
*) & sp
->slot
[sp
->header
.stblindex
];
1516 nextindex
= sp
->header
.nextindex
;
1517 for (nxt
= off
= 0; nxt
< nextindex
; ++off
) {
1519 /* check for fill factor with new entry size */
1523 switch (sp
->header
.flag
& BT_TYPE
) {
1525 ldtentry
= (struct ldtentry
*) & sp
->slot
[si
];
1527 n
= NDTLEAF(ldtentry
->namlen
);
1529 n
= NDTLEAF_LEGACY(ldtentry
->
1534 idtentry
= (struct idtentry
*) & sp
->slot
[si
];
1535 n
= NDTINTERNAL(idtentry
->namlen
);
1542 ++nxt
; /* advance to next entry to move in sp */
1550 /* <nxt> poins to the 1st entry to move */
1553 * move entries to right page
1555 * dtMoveEntry() initializes rp and reserves entry for insertion
1557 * split page moved out entries are linelocked;
1558 * new/right page moved in entries are linelocked;
1560 /* linelock header + stbl of new right page */
1561 rlv
= & rdtlck
->lv
[rdtlck
->index
];
1566 dtMoveEntry(sp
, nxt
, rp
, &sdtlck
, &rdtlck
, DO_INDEX(ip
));
1568 sp
->header
.nextindex
= nxt
;
1571 * finalize freelist of new right page
1573 fsi
= rp
->header
.freelist
;
1575 for (fsi
++; fsi
< rp
->header
.maxslot
; f
++, fsi
++)
1580 * Update directory index table for entries now in right page
1582 if ((rp
->header
.flag
& BT_LEAF
) && DO_INDEX(ip
)) {
1586 stbl
= DT_GETSTBL(rp
);
1587 for (n
= 0; n
< rp
->header
.nextindex
; n
++) {
1588 ldtentry
= (struct ldtentry
*) & rp
->slot
[stbl
[n
]];
1589 modify_index(tid
, ip
, le32_to_cpu(ldtentry
->index
),
1590 rbn
, n
, &mp
, &lblock
);
1593 release_metapage(mp
);
1597 * the skipped index was on the left page,
1600 /* insert the new entry in the split page */
1601 dtInsertEntry(sp
, skip
, split
->key
, split
->data
, &sdtlck
);
1603 /* linelock stbl of split page */
1604 if (sdtlck
->index
>= sdtlck
->maxcnt
)
1605 sdtlck
= (struct dt_lock
*) txLinelock(sdtlck
);
1606 slv
= & sdtlck
->lv
[sdtlck
->index
];
1607 n
= skip
>> L2DTSLOTSIZE
;
1608 slv
->offset
= sp
->header
.stblindex
+ n
;
1610 ((sp
->header
.nextindex
- 1) >> L2DTSLOTSIZE
) - n
+ 1;
1614 * the skipped index was on the right page,
1617 /* adjust the skip index to reflect the new position */
1620 /* insert the new entry in the right page */
1621 dtInsertEntry(rp
, skip
, split
->key
, split
->data
, &rdtlck
);
1635 * function: extend 1st/only directory leaf page
1639 * return: 0 - success;
1641 * return extended page pinned;
1643 static int dtExtendPage(tid_t tid
,
1644 struct inode
*ip
, struct dtsplit
* split
, struct btstack
* btstack
)
1646 struct super_block
*sb
= ip
->i_sb
;
1648 struct metapage
*smp
, *pmp
, *mp
;
1650 struct pxdlist
*pxdlist
;
1653 int newstblindex
, newstblsize
;
1654 int oldstblindex
, oldstblsize
;
1657 struct btframe
*parent
;
1659 struct dt_lock
*dtlck
;
1662 struct pxd_lock
*pxdlock
;
1665 struct ldtentry
*ldtentry
;
1668 /* get page to extend */
1670 sp
= DT_PAGE(ip
, smp
);
1672 /* get parent/root page */
1673 parent
= BT_POP(btstack
);
1674 DT_GETPAGE(ip
, parent
->bn
, pmp
, PSIZE
, pp
, rc
);
1681 pxdlist
= split
->pxdlist
;
1682 pxd
= &pxdlist
->pxd
[pxdlist
->npxd
];
1685 xaddr
= addressPXD(pxd
);
1686 tpxd
= &sp
->header
.self
;
1687 txaddr
= addressPXD(tpxd
);
1688 /* in-place extension */
1689 if (xaddr
== txaddr
) {
1696 /* save moved extent descriptor for later free */
1697 tlck
= txMaplock(tid
, ip
, tlckDTREE
| tlckRELOCATE
);
1698 pxdlock
= (struct pxd_lock
*) & tlck
->lock
;
1699 pxdlock
->flag
= mlckFREEPXD
;
1700 pxdlock
->pxd
= sp
->header
.self
;
1704 * Update directory index table to reflect new page address
1710 stbl
= DT_GETSTBL(sp
);
1711 for (n
= 0; n
< sp
->header
.nextindex
; n
++) {
1713 (struct ldtentry
*) & sp
->slot
[stbl
[n
]];
1714 modify_index(tid
, ip
,
1715 le32_to_cpu(ldtentry
->index
),
1716 xaddr
, n
, &mp
, &lblock
);
1719 release_metapage(mp
);
1726 sp
->header
.self
= *pxd
;
1728 jfs_info("dtExtendPage: ip:0x%p smp:0x%p sp:0x%p", ip
, smp
, sp
);
1730 BT_MARK_DIRTY(smp
, ip
);
1732 * acquire a transaction lock on the extended/leaf page
1734 tlck
= txLock(tid
, ip
, smp
, tlckDTREE
| type
);
1735 dtlck
= (struct dt_lock
*) & tlck
->lock
;
1736 lv
= & dtlck
->lv
[0];
1738 /* update buffer extent descriptor of extended page */
1739 xlen
= lengthPXD(pxd
);
1740 xsize
= xlen
<< JFS_SBI(sb
)->l2bsize
;
1743 * copy old stbl to new stbl at start of extended area
1745 oldstblindex
= sp
->header
.stblindex
;
1746 oldstblsize
= (sp
->header
.maxslot
+ 31) >> L2DTSLOTSIZE
;
1747 newstblindex
= sp
->header
.maxslot
;
1748 n
= xsize
>> L2DTSLOTSIZE
;
1749 newstblsize
= (n
+ 31) >> L2DTSLOTSIZE
;
1750 memcpy(&sp
->slot
[newstblindex
], &sp
->slot
[oldstblindex
],
1751 sp
->header
.nextindex
);
1754 * in-line extension: linelock old area of extended page
1756 if (type
== tlckEXTEND
) {
1757 /* linelock header */
1763 /* linelock new stbl of extended page */
1764 lv
->offset
= newstblindex
;
1765 lv
->length
= newstblsize
;
1768 * relocation: linelock whole relocated area
1772 lv
->length
= sp
->header
.maxslot
+ newstblsize
;
1777 sp
->header
.maxslot
= n
;
1778 sp
->header
.stblindex
= newstblindex
;
1779 /* sp->header.nextindex remains the same */
1782 * add old stbl region at head of freelist
1786 last
= sp
->header
.freelist
;
1787 for (n
= 0; n
< oldstblsize
; n
++, fsi
++, f
++) {
1791 sp
->header
.freelist
= last
;
1792 sp
->header
.freecnt
+= oldstblsize
;
1795 * append free region of newly extended area at tail of freelist
1797 /* init free region of newly extended area */
1798 fsi
= n
= newstblindex
+ newstblsize
;
1800 for (fsi
++; fsi
< sp
->header
.maxslot
; f
++, fsi
++)
1804 /* append new free region at tail of old freelist */
1805 fsi
= sp
->header
.freelist
;
1807 sp
->header
.freelist
= n
;
1812 } while (fsi
!= -1);
1817 sp
->header
.freecnt
+= sp
->header
.maxslot
- n
;
1820 * insert the new entry
1822 dtInsertEntry(sp
, split
->index
, split
->key
, split
->data
, &dtlck
);
1824 BT_MARK_DIRTY(pmp
, ip
);
1826 * linelock any freeslots residing in old extent
1828 if (type
== tlckEXTEND
) {
1829 n
= sp
->header
.maxslot
>> 2;
1830 if (sp
->header
.freelist
< n
)
1831 dtLinelockFreelist(sp
, n
, &dtlck
);
1835 * update parent entry on the parent/root page
1838 * acquire a transaction lock on the parent/root page
1840 tlck
= txLock(tid
, ip
, pmp
, tlckDTREE
| tlckENTRY
);
1841 dtlck
= (struct dt_lock
*) & tlck
->lock
;
1842 lv
= & dtlck
->lv
[dtlck
->index
];
1844 /* linelock parent entry - 1st slot */
1849 /* update the parent pxd for page extension */
1850 tpxd
= (pxd_t
*) & pp
->slot
[1];
1862 * split the full root page into
1863 * original/root/split page and new right page
1864 * i.e., root remains fixed in tree anchor (inode) and
1865 * the root is copied to a single new right child page
1866 * since root page << non-root page, and
1867 * the split root page contains a single entry for the
1868 * new right child page.
1872 * return: 0 - success;
1874 * return new page pinned;
1876 static int dtSplitRoot(tid_t tid
,
1877 struct inode
*ip
, struct dtsplit
* split
, struct metapage
** rmpp
)
1879 struct super_block
*sb
= ip
->i_sb
;
1880 struct metapage
*smp
;
1882 struct metapage
*rmp
;
1889 int fsi
, stblsize
, n
;
1892 struct pxdlist
*pxdlist
;
1894 struct dt_lock
*dtlck
;
1899 /* get split root page */
1901 sp
= &JFS_IP(ip
)->i_dtroot
;
1904 * allocate/initialize a single (right) child page
1906 * N.B. at first split, a one (or two) block to fit new entry
1907 * is allocated; at subsequent split, a full page is allocated;
1909 pxdlist
= split
->pxdlist
;
1910 pxd
= &pxdlist
->pxd
[pxdlist
->npxd
];
1912 rbn
= addressPXD(pxd
);
1913 xlen
= lengthPXD(pxd
);
1914 xsize
= xlen
<< JFS_SBI(sb
)->l2bsize
;
1915 rmp
= get_metapage(ip
, rbn
, xsize
, 1);
1921 /* Allocate blocks to quota. */
1922 rc
= dquot_alloc_block(ip
, lengthPXD(pxd
));
1924 release_metapage(rmp
);
1928 BT_MARK_DIRTY(rmp
, ip
);
1930 * acquire a transaction lock on the new right page
1932 tlck
= txLock(tid
, ip
, rmp
, tlckDTREE
| tlckNEW
);
1933 dtlck
= (struct dt_lock
*) & tlck
->lock
;
1936 (sp
->header
.flag
& BT_LEAF
) ? BT_LEAF
: BT_INTERNAL
;
1937 rp
->header
.self
= *pxd
;
1939 /* initialize sibling pointers */
1940 rp
->header
.next
= 0;
1941 rp
->header
.prev
= 0;
1944 * move in-line root page into new right page extent
1946 /* linelock header + copied entries + new stbl (1st slot) in new page */
1947 ASSERT(dtlck
->index
== 0);
1948 lv
= & dtlck
->lv
[0];
1950 lv
->length
= 10; /* 1 + 8 + 1 */
1953 n
= xsize
>> L2DTSLOTSIZE
;
1954 rp
->header
.maxslot
= n
;
1955 stblsize
= (n
+ 31) >> L2DTSLOTSIZE
;
1957 /* copy old stbl to new stbl at start of extended area */
1958 rp
->header
.stblindex
= DTROOTMAXSLOT
;
1959 stbl
= (s8
*) & rp
->slot
[DTROOTMAXSLOT
];
1960 memcpy(stbl
, sp
->header
.stbl
, sp
->header
.nextindex
);
1961 rp
->header
.nextindex
= sp
->header
.nextindex
;
1963 /* copy old data area to start of new data area */
1964 memcpy(&rp
->slot
[1], &sp
->slot
[1], IDATASIZE
);
1967 * append free region of newly extended area at tail of freelist
1969 /* init free region of newly extended area */
1970 fsi
= n
= DTROOTMAXSLOT
+ stblsize
;
1972 for (fsi
++; fsi
< rp
->header
.maxslot
; f
++, fsi
++)
1976 /* append new free region at tail of old freelist */
1977 fsi
= sp
->header
.freelist
;
1979 rp
->header
.freelist
= n
;
1981 rp
->header
.freelist
= fsi
;
1986 } while (fsi
!= -1);
1991 rp
->header
.freecnt
= sp
->header
.freecnt
+ rp
->header
.maxslot
- n
;
1994 * Update directory index table for entries now in right page
1996 if ((rp
->header
.flag
& BT_LEAF
) && DO_INDEX(ip
)) {
1998 struct metapage
*mp
= NULL
;
1999 struct ldtentry
*ldtentry
;
2001 stbl
= DT_GETSTBL(rp
);
2002 for (n
= 0; n
< rp
->header
.nextindex
; n
++) {
2003 ldtentry
= (struct ldtentry
*) & rp
->slot
[stbl
[n
]];
2004 modify_index(tid
, ip
, le32_to_cpu(ldtentry
->index
),
2005 rbn
, n
, &mp
, &lblock
);
2008 release_metapage(mp
);
2011 * insert the new entry into the new right/child page
2012 * (skip index in the new right page will not change)
2014 dtInsertEntry(rp
, split
->index
, split
->key
, split
->data
, &dtlck
);
2017 * reset parent/root page
2019 * set the 1st entry offset to 0, which force the left-most key
2020 * at any level of the tree to be less than any search key.
2022 * The btree comparison code guarantees that the left-most key on any
2023 * level of the tree is never used, so it doesn't need to be filled in.
2025 BT_MARK_DIRTY(smp
, ip
);
2027 * acquire a transaction lock on the root page (in-memory inode)
2029 tlck
= txLock(tid
, ip
, smp
, tlckDTREE
| tlckNEW
| tlckBTROOT
);
2030 dtlck
= (struct dt_lock
*) & tlck
->lock
;
2033 ASSERT(dtlck
->index
== 0);
2034 lv
= & dtlck
->lv
[0];
2036 lv
->length
= DTROOTMAXSLOT
;
2039 /* update page header of root */
2040 if (sp
->header
.flag
& BT_LEAF
) {
2041 sp
->header
.flag
&= ~BT_LEAF
;
2042 sp
->header
.flag
|= BT_INTERNAL
;
2045 /* init the first entry */
2046 s
= (struct idtentry
*) & sp
->slot
[DTENTRYSTART
];
2052 stbl
= sp
->header
.stbl
;
2053 stbl
[0] = DTENTRYSTART
;
2054 sp
->header
.nextindex
= 1;
2057 fsi
= DTENTRYSTART
+ 1;
2060 /* init free region of remaining area */
2061 for (fsi
++; fsi
< DTROOTMAXSLOT
; f
++, fsi
++)
2065 sp
->header
.freelist
= DTENTRYSTART
+ 1;
2066 sp
->header
.freecnt
= DTROOTMAXSLOT
- (DTENTRYSTART
+ 1);
2077 * function: delete the entry(s) referenced by a key.
2083 int dtDelete(tid_t tid
,
2084 struct inode
*ip
, struct component_name
* key
, ino_t
* ino
, int flag
)
2088 struct metapage
*mp
, *imp
;
2091 struct btstack btstack
;
2092 struct dt_lock
*dtlck
;
2096 struct ldtentry
*ldtentry
;
2098 u32 table_index
, next_index
;
2099 struct metapage
*nmp
;
2103 * search for the entry to delete:
2105 * dtSearch() returns (leaf page pinned, index at which to delete).
2107 if ((rc
= dtSearch(ip
, key
, ino
, &btstack
, flag
)))
2110 /* retrieve search result */
2111 DT_GETSEARCH(ip
, btstack
.top
, bn
, mp
, p
, index
);
2114 * We need to find put the index of the next entry into the
2115 * directory index table in order to resume a readdir from this
2119 stbl
= DT_GETSTBL(p
);
2120 ldtentry
= (struct ldtentry
*) & p
->slot
[stbl
[index
]];
2121 table_index
= le32_to_cpu(ldtentry
->index
);
2122 if (index
== (p
->header
.nextindex
- 1)) {
2124 * Last entry in this leaf page
2126 if ((p
->header
.flag
& BT_ROOT
)
2127 || (p
->header
.next
== 0))
2130 /* Read next leaf page */
2131 DT_GETPAGE(ip
, le64_to_cpu(p
->header
.next
),
2132 nmp
, PSIZE
, np
, rc
);
2136 stbl
= DT_GETSTBL(np
);
2138 (struct ldtentry
*) & np
->
2141 le32_to_cpu(ldtentry
->index
);
2147 (struct ldtentry
*) & p
->slot
[stbl
[index
+ 1]];
2148 next_index
= le32_to_cpu(ldtentry
->index
);
2150 free_index(tid
, ip
, table_index
, next_index
);
2153 * the leaf page becomes empty, delete the page
2155 if (p
->header
.nextindex
== 1) {
2156 /* delete empty page */
2157 rc
= dtDeleteUp(tid
, ip
, mp
, p
, &btstack
);
2160 * the leaf page has other entries remaining:
2162 * delete the entry from the leaf page.
2165 BT_MARK_DIRTY(mp
, ip
);
2167 * acquire a transaction lock on the leaf page
2169 tlck
= txLock(tid
, ip
, mp
, tlckDTREE
| tlckENTRY
);
2170 dtlck
= (struct dt_lock
*) & tlck
->lock
;
2173 * Do not assume that dtlck->index will be zero. During a
2174 * rename within a directory, this transaction may have
2175 * modified this page already when adding the new entry.
2178 /* linelock header */
2179 if (dtlck
->index
>= dtlck
->maxcnt
)
2180 dtlck
= (struct dt_lock
*) txLinelock(dtlck
);
2181 lv
= & dtlck
->lv
[dtlck
->index
];
2186 /* linelock stbl of non-root leaf page */
2187 if (!(p
->header
.flag
& BT_ROOT
)) {
2188 if (dtlck
->index
>= dtlck
->maxcnt
)
2189 dtlck
= (struct dt_lock
*) txLinelock(dtlck
);
2190 lv
= & dtlck
->lv
[dtlck
->index
];
2191 i
= index
>> L2DTSLOTSIZE
;
2192 lv
->offset
= p
->header
.stblindex
+ i
;
2194 ((p
->header
.nextindex
- 1) >> L2DTSLOTSIZE
) -
2199 /* free the leaf entry */
2200 dtDeleteEntry(p
, index
, &dtlck
);
2203 * Update directory index table for entries moved in stbl
2205 if (DO_INDEX(ip
) && index
< p
->header
.nextindex
) {
2209 stbl
= DT_GETSTBL(p
);
2210 for (i
= index
; i
< p
->header
.nextindex
; i
++) {
2212 (struct ldtentry
*) & p
->slot
[stbl
[i
]];
2213 modify_index(tid
, ip
,
2214 le32_to_cpu(ldtentry
->index
),
2215 bn
, i
, &imp
, &lblock
);
2218 release_metapage(imp
);
2232 * free empty pages as propagating deletion up the tree
2238 static int dtDeleteUp(tid_t tid
, struct inode
*ip
,
2239 struct metapage
* fmp
, dtpage_t
* fp
, struct btstack
* btstack
)
2242 struct metapage
*mp
;
2244 int index
, nextindex
;
2246 struct btframe
*parent
;
2247 struct dt_lock
*dtlck
;
2250 struct pxd_lock
*pxdlock
;
2254 * keep the root leaf page which has become empty
2256 if (BT_IS_ROOT(fmp
)) {
2260 * dtInitRoot() acquires txlock on the root
2262 dtInitRoot(tid
, ip
, PARENT(ip
));
2270 * free the non-root leaf page
2273 * acquire a transaction lock on the page
2275 * write FREEXTENT|NOREDOPAGE log record
2276 * N.B. linelock is overlaid as freed extent descriptor, and
2277 * the buffer page is freed;
2279 tlck
= txMaplock(tid
, ip
, tlckDTREE
| tlckFREE
);
2280 pxdlock
= (struct pxd_lock
*) & tlck
->lock
;
2281 pxdlock
->flag
= mlckFREEPXD
;
2282 pxdlock
->pxd
= fp
->header
.self
;
2285 /* update sibling pointers */
2286 if ((rc
= dtRelink(tid
, ip
, fp
))) {
2291 xlen
= lengthPXD(&fp
->header
.self
);
2293 /* Free quota allocation. */
2294 dquot_free_block(ip
, xlen
);
2296 /* free/invalidate its buffer page */
2297 discard_metapage(fmp
);
2300 * propagate page deletion up the directory tree
2302 * If the delete from the parent page makes it empty,
2303 * continue all the way up the tree.
2304 * stop if the root page is reached (which is never deleted) or
2305 * if the entry deletion does not empty the page.
2307 while ((parent
= BT_POP(btstack
)) != NULL
) {
2308 /* pin the parent page <sp> */
2309 DT_GETPAGE(ip
, parent
->bn
, mp
, PSIZE
, p
, rc
);
2314 * free the extent of the child page deleted
2316 index
= parent
->index
;
2319 * delete the entry for the child page from parent
2321 nextindex
= p
->header
.nextindex
;
2324 * the parent has the single entry being deleted:
2326 * free the parent page which has become empty.
2328 if (nextindex
== 1) {
2330 * keep the root internal page which has become empty
2332 if (p
->header
.flag
& BT_ROOT
) {
2336 * dtInitRoot() acquires txlock on the root
2338 dtInitRoot(tid
, ip
, PARENT(ip
));
2345 * free the parent page
2349 * acquire a transaction lock on the page
2351 * write FREEXTENT|NOREDOPAGE log record
2355 tlckDTREE
| tlckFREE
);
2356 pxdlock
= (struct pxd_lock
*) & tlck
->lock
;
2357 pxdlock
->flag
= mlckFREEPXD
;
2358 pxdlock
->pxd
= p
->header
.self
;
2361 /* update sibling pointers */
2362 if ((rc
= dtRelink(tid
, ip
, p
))) {
2367 xlen
= lengthPXD(&p
->header
.self
);
2369 /* Free quota allocation */
2370 dquot_free_block(ip
, xlen
);
2372 /* free/invalidate its buffer page */
2373 discard_metapage(mp
);
2381 * the parent has other entries remaining:
2383 * delete the router entry from the parent page.
2385 BT_MARK_DIRTY(mp
, ip
);
2387 * acquire a transaction lock on the page
2389 * action: router entry deletion
2391 tlck
= txLock(tid
, ip
, mp
, tlckDTREE
| tlckENTRY
);
2392 dtlck
= (struct dt_lock
*) & tlck
->lock
;
2394 /* linelock header */
2395 if (dtlck
->index
>= dtlck
->maxcnt
)
2396 dtlck
= (struct dt_lock
*) txLinelock(dtlck
);
2397 lv
= & dtlck
->lv
[dtlck
->index
];
2402 /* linelock stbl of non-root leaf page */
2403 if (!(p
->header
.flag
& BT_ROOT
)) {
2404 if (dtlck
->index
< dtlck
->maxcnt
)
2407 dtlck
= (struct dt_lock
*) txLinelock(dtlck
);
2408 lv
= & dtlck
->lv
[0];
2410 i
= index
>> L2DTSLOTSIZE
;
2411 lv
->offset
= p
->header
.stblindex
+ i
;
2413 ((p
->header
.nextindex
- 1) >> L2DTSLOTSIZE
) -
2418 /* free the router entry */
2419 dtDeleteEntry(p
, index
, &dtlck
);
2421 /* reset key of new leftmost entry of level (for consistency) */
2423 ((p
->header
.flag
& BT_ROOT
) || p
->header
.prev
== 0))
2424 dtTruncateEntry(p
, 0, &dtlck
);
2426 /* unpin the parent page */
2429 /* exit propagation up */
2434 ip
->i_size
-= PSIZE
;
2441 * NAME: dtRelocate()
2443 * FUNCTION: relocate dtpage (internal or leaf) of directory;
2444 * This function is mainly used by defragfs utility.
2446 int dtRelocate(tid_t tid
, struct inode
*ip
, s64 lmxaddr
, pxd_t
* opxd
,
2450 struct metapage
*mp
, *pmp
, *lmp
, *rmp
;
2451 dtpage_t
*p
, *pp
, *rp
= 0, *lp
= 0;
2454 struct btstack btstack
;
2456 s64 oxaddr
, nextbn
, prevbn
;
2459 struct dt_lock
*dtlck
;
2460 struct pxd_lock
*pxdlock
;
2464 oxaddr
= addressPXD(opxd
);
2465 xlen
= lengthPXD(opxd
);
2467 jfs_info("dtRelocate: lmxaddr:%Ld xaddr:%Ld:%Ld xlen:%d",
2468 (long long)lmxaddr
, (long long)oxaddr
, (long long)nxaddr
,
2472 * 1. get the internal parent dtpage covering
2473 * router entry for the tartget page to be relocated;
2475 rc
= dtSearchNode(ip
, lmxaddr
, opxd
, &btstack
);
2479 /* retrieve search result */
2480 DT_GETSEARCH(ip
, btstack
.top
, bn
, pmp
, pp
, index
);
2481 jfs_info("dtRelocate: parent router entry validated.");
2484 * 2. relocate the target dtpage
2486 /* read in the target page from src extent */
2487 DT_GETPAGE(ip
, oxaddr
, mp
, PSIZE
, p
, rc
);
2489 /* release the pinned parent page */
2495 * read in sibling pages if any to update sibling pointers;
2498 if (p
->header
.next
) {
2499 nextbn
= le64_to_cpu(p
->header
.next
);
2500 DT_GETPAGE(ip
, nextbn
, rmp
, PSIZE
, rp
, rc
);
2509 if (p
->header
.prev
) {
2510 prevbn
= le64_to_cpu(p
->header
.prev
);
2511 DT_GETPAGE(ip
, prevbn
, lmp
, PSIZE
, lp
, rc
);
2521 /* at this point, all xtpages to be updated are in memory */
2524 * update sibling pointers of sibling dtpages if any;
2527 tlck
= txLock(tid
, ip
, lmp
, tlckDTREE
| tlckRELINK
);
2528 dtlck
= (struct dt_lock
*) & tlck
->lock
;
2529 /* linelock header */
2530 ASSERT(dtlck
->index
== 0);
2531 lv
= & dtlck
->lv
[0];
2536 lp
->header
.next
= cpu_to_le64(nxaddr
);
2541 tlck
= txLock(tid
, ip
, rmp
, tlckDTREE
| tlckRELINK
);
2542 dtlck
= (struct dt_lock
*) & tlck
->lock
;
2543 /* linelock header */
2544 ASSERT(dtlck
->index
== 0);
2545 lv
= & dtlck
->lv
[0];
2550 rp
->header
.prev
= cpu_to_le64(nxaddr
);
2555 * update the target dtpage to be relocated
2557 * write LOG_REDOPAGE of LOG_NEW type for dst page
2558 * for the whole target page (logredo() will apply
2559 * after image and update bmap for allocation of the
2560 * dst extent), and update bmap for allocation of
2563 tlck
= txLock(tid
, ip
, mp
, tlckDTREE
| tlckNEW
);
2564 dtlck
= (struct dt_lock
*) & tlck
->lock
;
2565 /* linelock header */
2566 ASSERT(dtlck
->index
== 0);
2567 lv
= & dtlck
->lv
[0];
2569 /* update the self address in the dtpage header */
2570 pxd
= &p
->header
.self
;
2571 PXDaddress(pxd
, nxaddr
);
2573 /* the dst page is the same as the src page, i.e.,
2574 * linelock for afterimage of the whole page;
2577 lv
->length
= p
->header
.maxslot
;
2580 /* update the buffer extent descriptor of the dtpage */
2581 xsize
= xlen
<< JFS_SBI(ip
->i_sb
)->l2bsize
;
2583 /* unpin the relocated page */
2585 jfs_info("dtRelocate: target dtpage relocated.");
2587 /* the moved extent is dtpage, then a LOG_NOREDOPAGE log rec
2588 * needs to be written (in logredo(), the LOG_NOREDOPAGE log rec
2589 * will also force a bmap update ).
2593 * 3. acquire maplock for the source extent to be freed;
2595 /* for dtpage relocation, write a LOG_NOREDOPAGE record
2596 * for the source dtpage (logredo() will init NoRedoPage
2597 * filter and will also update bmap for free of the source
2598 * dtpage), and upadte bmap for free of the source dtpage;
2600 tlck
= txMaplock(tid
, ip
, tlckDTREE
| tlckFREE
);
2601 pxdlock
= (struct pxd_lock
*) & tlck
->lock
;
2602 pxdlock
->flag
= mlckFREEPXD
;
2603 PXDaddress(&pxdlock
->pxd
, oxaddr
);
2604 PXDlength(&pxdlock
->pxd
, xlen
);
2608 * 4. update the parent router entry for relocation;
2610 * acquire tlck for the parent entry covering the target dtpage;
2611 * write LOG_REDOPAGE to apply after image only;
2613 jfs_info("dtRelocate: update parent router entry.");
2614 tlck
= txLock(tid
, ip
, pmp
, tlckDTREE
| tlckENTRY
);
2615 dtlck
= (struct dt_lock
*) & tlck
->lock
;
2616 lv
= & dtlck
->lv
[dtlck
->index
];
2618 /* update the PXD with the new address */
2619 stbl
= DT_GETSTBL(pp
);
2620 pxd
= (pxd_t
*) & pp
->slot
[stbl
[index
]];
2621 PXDaddress(pxd
, nxaddr
);
2622 lv
->offset
= stbl
[index
];
2626 /* unpin the parent dtpage */
2633 * NAME: dtSearchNode()
2635 * FUNCTION: Search for an dtpage containing a specified address
2636 * This function is mainly used by defragfs utility.
2638 * NOTE: Search result on stack, the found page is pinned at exit.
2639 * The result page must be an internal dtpage.
2640 * lmxaddr give the address of the left most page of the
2641 * dtree level, in which the required dtpage resides.
2643 static int dtSearchNode(struct inode
*ip
, s64 lmxaddr
, pxd_t
* kpxd
,
2644 struct btstack
* btstack
)
2648 struct metapage
*mp
;
2650 int psize
= 288; /* initial in-line directory */
2654 struct btframe
*btsp
;
2656 BT_CLR(btstack
); /* reset stack */
2659 * descend tree to the level with specified leftmost page
2661 * by convention, root bn = 0.
2664 /* get/pin the page to search */
2665 DT_GETPAGE(ip
, bn
, mp
, psize
, p
, rc
);
2669 /* does the xaddr of leftmost page of the levevl
2670 * matches levevl search key ?
2672 if (p
->header
.flag
& BT_ROOT
) {
2675 } else if (addressPXD(&p
->header
.self
) == lmxaddr
)
2679 * descend down to leftmost child page
2681 if (p
->header
.flag
& BT_LEAF
) {
2686 /* get the leftmost entry */
2687 stbl
= DT_GETSTBL(p
);
2688 pxd
= (pxd_t
*) & p
->slot
[stbl
[0]];
2690 /* get the child page block address */
2691 bn
= addressPXD(pxd
);
2692 psize
= lengthPXD(pxd
) << JFS_SBI(ip
->i_sb
)->l2bsize
;
2693 /* unpin the parent page */
2698 * search each page at the current levevl
2701 stbl
= DT_GETSTBL(p
);
2702 for (i
= 0; i
< p
->header
.nextindex
; i
++) {
2703 pxd
= (pxd_t
*) & p
->slot
[stbl
[i
]];
2705 /* found the specified router entry */
2706 if (addressPXD(pxd
) == addressPXD(kpxd
) &&
2707 lengthPXD(pxd
) == lengthPXD(kpxd
)) {
2708 btsp
= btstack
->top
;
2717 /* get the right sibling page if any */
2719 bn
= le64_to_cpu(p
->header
.next
);
2725 /* unpin current page */
2728 /* get the right sibling page */
2729 DT_GETPAGE(ip
, bn
, mp
, PSIZE
, p
, rc
);
2735 #endif /* _NOTYET */
2741 * link around a freed page.
2744 * fp: page to be freed
2748 static int dtRelink(tid_t tid
, struct inode
*ip
, dtpage_t
* p
)
2751 struct metapage
*mp
;
2754 struct dt_lock
*dtlck
;
2757 nextbn
= le64_to_cpu(p
->header
.next
);
2758 prevbn
= le64_to_cpu(p
->header
.prev
);
2760 /* update prev pointer of the next page */
2762 DT_GETPAGE(ip
, nextbn
, mp
, PSIZE
, p
, rc
);
2766 BT_MARK_DIRTY(mp
, ip
);
2768 * acquire a transaction lock on the next page
2770 * action: update prev pointer;
2772 tlck
= txLock(tid
, ip
, mp
, tlckDTREE
| tlckRELINK
);
2773 jfs_info("dtRelink nextbn: tlck = 0x%p, ip = 0x%p, mp=0x%p",
2775 dtlck
= (struct dt_lock
*) & tlck
->lock
;
2777 /* linelock header */
2778 if (dtlck
->index
>= dtlck
->maxcnt
)
2779 dtlck
= (struct dt_lock
*) txLinelock(dtlck
);
2780 lv
= & dtlck
->lv
[dtlck
->index
];
2785 p
->header
.prev
= cpu_to_le64(prevbn
);
2789 /* update next pointer of the previous page */
2791 DT_GETPAGE(ip
, prevbn
, mp
, PSIZE
, p
, rc
);
2795 BT_MARK_DIRTY(mp
, ip
);
2797 * acquire a transaction lock on the prev page
2799 * action: update next pointer;
2801 tlck
= txLock(tid
, ip
, mp
, tlckDTREE
| tlckRELINK
);
2802 jfs_info("dtRelink prevbn: tlck = 0x%p, ip = 0x%p, mp=0x%p",
2804 dtlck
= (struct dt_lock
*) & tlck
->lock
;
2806 /* linelock header */
2807 if (dtlck
->index
>= dtlck
->maxcnt
)
2808 dtlck
= (struct dt_lock
*) txLinelock(dtlck
);
2809 lv
= & dtlck
->lv
[dtlck
->index
];
2814 p
->header
.next
= cpu_to_le64(nextbn
);
2825 * initialize directory root (inline in inode)
2827 void dtInitRoot(tid_t tid
, struct inode
*ip
, u32 idotdot
)
2829 struct jfs_inode_info
*jfs_ip
= JFS_IP(ip
);
2834 struct dt_lock
*dtlck
;
2839 * If this was previously an non-empty directory, we need to remove
2840 * the old directory table.
2843 if (!jfs_dirtable_inline(ip
)) {
2844 struct tblock
*tblk
= tid_to_tblock(tid
);
2846 * We're playing games with the tid's xflag. If
2847 * we're removing a regular file, the file's xtree
2848 * is committed with COMMIT_PMAP, but we always
2849 * commit the directories xtree with COMMIT_PWMAP.
2851 xflag_save
= tblk
->xflag
;
2854 * xtTruncate isn't guaranteed to fully truncate
2855 * the xtree. The caller needs to check i_size
2856 * after committing the transaction to see if
2857 * additional truncation is needed. The
2858 * COMMIT_Stale flag tells caller that we
2859 * initiated the truncation.
2861 xtTruncate(tid
, ip
, 0, COMMIT_PWMAP
);
2862 set_cflag(COMMIT_Stale
, ip
);
2864 tblk
->xflag
= xflag_save
;
2868 jfs_ip
->next_index
= 2;
2870 ip
->i_size
= IDATASIZE
;
2873 * acquire a transaction lock on the root
2875 * action: directory initialization;
2877 tlck
= txLock(tid
, ip
, (struct metapage
*) & jfs_ip
->bxflag
,
2878 tlckDTREE
| tlckENTRY
| tlckBTROOT
);
2879 dtlck
= (struct dt_lock
*) & tlck
->lock
;
2882 ASSERT(dtlck
->index
== 0);
2883 lv
= & dtlck
->lv
[0];
2885 lv
->length
= DTROOTMAXSLOT
;
2888 p
= &jfs_ip
->i_dtroot
;
2890 p
->header
.flag
= DXD_INDEX
| BT_ROOT
| BT_LEAF
;
2892 p
->header
.nextindex
= 0;
2898 /* init data area of root */
2899 for (fsi
++; fsi
< DTROOTMAXSLOT
; f
++, fsi
++)
2903 p
->header
.freelist
= 1;
2904 p
->header
.freecnt
= 8;
2906 /* init '..' entry */
2907 p
->header
.idotdot
= cpu_to_le32(idotdot
);
2913 * add_missing_indices()
2915 * function: Fix dtree page in which one or more entries has an invalid index.
2916 * fsck.jfs should really fix this, but it currently does not.
2917 * Called from jfs_readdir when bad index is detected.
2919 static void add_missing_indices(struct inode
*inode
, s64 bn
)
2922 struct dt_lock
*dtlck
;
2926 struct metapage
*mp
;
2933 tid
= txBegin(inode
->i_sb
, 0);
2935 DT_GETPAGE(inode
, bn
, mp
, PSIZE
, p
, rc
);
2938 printk(KERN_ERR
"DT_GETPAGE failed!\n");
2941 BT_MARK_DIRTY(mp
, inode
);
2943 ASSERT(p
->header
.flag
& BT_LEAF
);
2945 tlck
= txLock(tid
, inode
, mp
, tlckDTREE
| tlckENTRY
);
2947 tlck
->type
|= tlckBTROOT
;
2949 dtlck
= (struct dt_lock
*) &tlck
->lock
;
2951 stbl
= DT_GETSTBL(p
);
2952 for (i
= 0; i
< p
->header
.nextindex
; i
++) {
2953 d
= (struct ldtentry
*) &p
->slot
[stbl
[i
]];
2954 index
= le32_to_cpu(d
->index
);
2955 if ((index
< 2) || (index
>= JFS_IP(inode
)->next_index
)) {
2956 d
->index
= cpu_to_le32(add_index(tid
, inode
, bn
, i
));
2957 if (dtlck
->index
>= dtlck
->maxcnt
)
2958 dtlck
= (struct dt_lock
*) txLinelock(dtlck
);
2959 lv
= &dtlck
->lv
[dtlck
->index
];
2960 lv
->offset
= stbl
[i
];
2967 (void) txCommit(tid
, 1, &inode
, 0);
2973 * Buffer to hold directory entry info while traversing a dtree page
2974 * before being fed to the filldir function
2984 * function to determine next variable-sized jfs_dirent in buffer
2986 static inline struct jfs_dirent
*next_jfs_dirent(struct jfs_dirent
*dirent
)
2988 return (struct jfs_dirent
*)
2990 ((sizeof (struct jfs_dirent
) + dirent
->name_len
+ 1 +
2991 sizeof (loff_t
) - 1) &
2992 ~(sizeof (loff_t
) - 1)));
2998 * function: read directory entries sequentially
2999 * from the specified entry offset
3003 * return: offset = (pn, index) of start entry
3004 * of next jfs_readdir()/dtRead()
3006 int jfs_readdir(struct file
*file
, struct dir_context
*ctx
)
3008 struct inode
*ip
= file_inode(file
);
3009 struct nls_table
*codepage
= JFS_SBI(ip
->i_sb
)->nls_tab
;
3011 loff_t dtpos
; /* legacy OS/2 style position */
3016 } *dtoffset
= (struct dtoffset
*) &dtpos
;
3018 struct metapage
*mp
;
3022 struct btstack btstack
;
3026 int d_namleft
, len
, outlen
;
3027 unsigned long dirent_buf
;
3031 uint loop_count
= 0;
3032 struct jfs_dirent
*jfs_dirent
;
3034 int overflow
, fix_page
, page_fixed
= 0;
3035 static int unique_pos
= 2; /* If we can't fix broken index */
3037 if (ctx
->pos
== DIREND
)
3042 * persistent index is stored in directory entries.
3043 * Special cases: 0 = .
3045 * -1 = End of directory
3049 dir_index
= (u32
) ctx
->pos
;
3052 * NFSv4 reserves cookies 1 and 2 for . and .. so the value
3053 * we return to the vfs is one greater than the one we use
3059 if (dir_index
> 1) {
3060 struct dir_table_slot dirtab_slot
;
3063 (dir_index
>= JFS_IP(ip
)->next_index
)) {
3064 /* Stale position. Directory has shrunk */
3069 rc
= read_index(ip
, dir_index
, &dirtab_slot
);
3074 if (dirtab_slot
.flag
== DIR_INDEX_FREE
) {
3075 if (loop_count
++ > JFS_IP(ip
)->next_index
) {
3076 jfs_err("jfs_readdir detected infinite loop!");
3080 dir_index
= le32_to_cpu(dirtab_slot
.addr2
);
3081 if (dir_index
== -1) {
3087 bn
= addressDTS(&dirtab_slot
);
3088 index
= dirtab_slot
.slot
;
3089 DT_GETPAGE(ip
, bn
, mp
, PSIZE
, p
, rc
);
3094 if (p
->header
.flag
& BT_INTERNAL
) {
3095 jfs_err("jfs_readdir: bad index table");
3101 if (dir_index
== 0) {
3106 if (!dir_emit(ctx
, ".", 1, ip
->i_ino
, DT_DIR
))
3113 if (!dir_emit(ctx
, "..", 2, PARENT(ip
), DT_DIR
))
3117 * Find first entry of left-most leaf
3124 if ((rc
= dtReadFirst(ip
, &btstack
)))
3127 DT_GETSEARCH(ip
, btstack
.top
, bn
, mp
, p
, index
);
3131 * Legacy filesystem - OS/2 & Linux JFS < 0.3.6
3133 * pn = 0; index = 1: First entry "."
3134 * pn = 0; index = 2: Second entry ".."
3135 * pn > 0: Real entries, pn=1 -> leftmost page
3136 * pn = index = -1: No more entries
3140 /* build "." entry */
3142 if (!dir_emit(ctx
, ".", 1, ip
->i_ino
, DT_DIR
))
3144 dtoffset
->index
= 2;
3148 if (dtoffset
->pn
== 0) {
3149 if (dtoffset
->index
== 2) {
3150 /* build ".." entry */
3151 if (!dir_emit(ctx
, "..", 2, PARENT(ip
), DT_DIR
))
3154 jfs_err("jfs_readdir called with invalid offset!");
3157 dtoffset
->index
= 0;
3166 if ((rc
= dtReadNext(ip
, &ctx
->pos
, &btstack
))) {
3167 jfs_err("jfs_readdir: unexpected rc = %d from dtReadNext",
3172 /* get start leaf page and index */
3173 DT_GETSEARCH(ip
, btstack
.top
, bn
, mp
, p
, index
);
3175 /* offset beyond directory eof ? */
3182 dirent_buf
= __get_free_page(GFP_KERNEL
);
3183 if (dirent_buf
== 0) {
3185 jfs_warn("jfs_readdir: __get_free_page failed!");
3191 jfs_dirent
= (struct jfs_dirent
*) dirent_buf
;
3193 overflow
= fix_page
= 0;
3195 stbl
= DT_GETSTBL(p
);
3197 for (i
= index
; i
< p
->header
.nextindex
; i
++) {
3198 d
= (struct ldtentry
*) & p
->slot
[stbl
[i
]];
3200 if (((long) jfs_dirent
+ d
->namlen
+ 1) >
3201 (dirent_buf
+ PAGE_SIZE
)) {
3202 /* DBCS codepages could overrun dirent_buf */
3208 d_namleft
= d
->namlen
;
3209 name_ptr
= jfs_dirent
->name
;
3210 jfs_dirent
->ino
= le32_to_cpu(d
->inumber
);
3213 len
= min(d_namleft
, DTLHDRDATALEN
);
3214 jfs_dirent
->position
= le32_to_cpu(d
->index
);
3216 * d->index should always be valid, but it
3217 * isn't. fsck.jfs doesn't create the
3218 * directory index for the lost+found
3219 * directory. Rather than let it go,
3220 * we can try to fix it.
3222 if ((jfs_dirent
->position
< 2) ||
3223 (jfs_dirent
->position
>=
3224 JFS_IP(ip
)->next_index
)) {
3225 if (!page_fixed
&& !isReadOnly(ip
)) {
3228 * setting overflow and setting
3229 * index to i will cause the
3230 * same page to be processed
3231 * again starting here
3237 jfs_dirent
->position
= unique_pos
++;
3240 * We add 1 to the index because we may
3241 * use a value of 2 internally, and NFSv4
3242 * doesn't like that.
3244 jfs_dirent
->position
++;
3246 jfs_dirent
->position
= dtpos
;
3247 len
= min(d_namleft
, DTLHDRDATALEN_LEGACY
);
3250 /* copy the name of head/only segment */
3251 outlen
= jfs_strfromUCS_le(name_ptr
, d
->name
, len
,
3253 jfs_dirent
->name_len
= outlen
;
3255 /* copy name in the additional segment(s) */
3258 t
= (struct dtslot
*) & p
->slot
[next
];
3262 if (d_namleft
== 0) {
3264 "JFS:Dtree error: ino = %ld, bn=%lld, index = %d\n",
3270 len
= min(d_namleft
, DTSLOTDATALEN
);
3271 outlen
= jfs_strfromUCS_le(name_ptr
, t
->name
,
3273 jfs_dirent
->name_len
+= outlen
;
3279 jfs_dirent
= next_jfs_dirent(jfs_dirent
);
3286 /* Point to next leaf page */
3287 if (p
->header
.flag
& BT_ROOT
)
3290 bn
= le64_to_cpu(p
->header
.next
);
3292 /* update offset (pn:index) for new page */
3295 dtoffset
->index
= 0;
3301 /* unpin previous leaf page */
3304 jfs_dirent
= (struct jfs_dirent
*) dirent_buf
;
3305 while (jfs_dirents
--) {
3306 ctx
->pos
= jfs_dirent
->position
;
3307 if (!dir_emit(ctx
, jfs_dirent
->name
,
3308 jfs_dirent
->name_len
,
3309 jfs_dirent
->ino
, DT_UNKNOWN
))
3311 jfs_dirent
= next_jfs_dirent(jfs_dirent
);
3315 add_missing_indices(ip
, bn
);
3319 if (!overflow
&& (bn
== 0)) {
3324 DT_GETPAGE(ip
, bn
, mp
, PSIZE
, p
, rc
);
3326 free_page(dirent_buf
);
3332 free_page(dirent_buf
);
3341 * function: get the leftmost page of the directory
3343 static int dtReadFirst(struct inode
*ip
, struct btstack
* btstack
)
3347 int psize
= 288; /* initial in-line directory */
3348 struct metapage
*mp
;
3351 struct btframe
*btsp
;
3354 BT_CLR(btstack
); /* reset stack */
3357 * descend leftmost path of the tree
3359 * by convention, root bn = 0.
3362 DT_GETPAGE(ip
, bn
, mp
, psize
, p
, rc
);
3367 * leftmost leaf page
3369 if (p
->header
.flag
& BT_LEAF
) {
3370 /* return leftmost entry */
3371 btsp
= btstack
->top
;
3380 * descend down to leftmost child page
3382 if (BT_STACK_FULL(btstack
)) {
3384 jfs_error(ip
->i_sb
, "btstack overrun\n");
3385 BT_STACK_DUMP(btstack
);
3388 /* push (bn, index) of the parent page/entry */
3389 BT_PUSH(btstack
, bn
, 0);
3391 /* get the leftmost entry */
3392 stbl
= DT_GETSTBL(p
);
3393 xd
= (pxd_t
*) & p
->slot
[stbl
[0]];
3395 /* get the child page block address */
3396 bn
= addressPXD(xd
);
3397 psize
= lengthPXD(xd
) << JFS_SBI(ip
->i_sb
)->l2bsize
;
3399 /* unpin the parent page */
3408 * function: get the page of the specified offset (pn:index)
3410 * return: if (offset > eof), bn = -1;
3412 * note: if index > nextindex of the target leaf page,
3413 * start with 1st entry of next leaf page;
3415 static int dtReadNext(struct inode
*ip
, loff_t
* offset
,
3416 struct btstack
* btstack
)
3423 } *dtoffset
= (struct dtoffset
*) offset
;
3425 struct metapage
*mp
;
3430 struct btframe
*btsp
, *parent
;
3434 * get leftmost leaf page pinned
3436 if ((rc
= dtReadFirst(ip
, btstack
)))
3440 DT_GETSEARCH(ip
, btstack
->top
, bn
, mp
, p
, index
);
3442 /* get the start offset (pn:index) */
3443 pn
= dtoffset
->pn
- 1; /* Now pn = 0 represents leftmost leaf */
3444 index
= dtoffset
->index
;
3446 /* start at leftmost page ? */
3448 /* offset beyond eof ? */
3449 if (index
< p
->header
.nextindex
)
3452 if (p
->header
.flag
& BT_ROOT
) {
3457 /* start with 1st entry of next leaf page */
3459 dtoffset
->index
= index
= 0;
3463 /* start at non-leftmost page: scan parent pages for large pn */
3464 if (p
->header
.flag
& BT_ROOT
) {
3469 /* start after next leaf page ? */
3473 /* get leaf page pn = 1 */
3475 bn
= le64_to_cpu(p
->header
.next
);
3477 /* unpin leaf page */
3480 /* offset beyond eof ? */
3489 * scan last internal page level to get target leaf page
3492 /* unpin leftmost leaf page */
3495 /* get left most parent page */
3496 btsp
= btstack
->top
;
3499 DT_GETPAGE(ip
, bn
, mp
, PSIZE
, p
, rc
);
3503 /* scan parent pages at last internal page level */
3504 while (pn
>= p
->header
.nextindex
) {
3505 pn
-= p
->header
.nextindex
;
3507 /* get next parent page address */
3508 bn
= le64_to_cpu(p
->header
.next
);
3510 /* unpin current parent page */
3513 /* offset beyond eof ? */
3519 /* get next parent page */
3520 DT_GETPAGE(ip
, bn
, mp
, PSIZE
, p
, rc
);
3524 /* update parent page stack frame */
3528 /* get leaf page address */
3529 stbl
= DT_GETSTBL(p
);
3530 xd
= (pxd_t
*) & p
->slot
[stbl
[pn
]];
3531 bn
= addressPXD(xd
);
3533 /* unpin parent page */
3537 * get target leaf page
3540 DT_GETPAGE(ip
, bn
, mp
, PSIZE
, p
, rc
);
3545 * leaf page has been completed:
3546 * start with 1st entry of next leaf page
3548 if (index
>= p
->header
.nextindex
) {
3549 bn
= le64_to_cpu(p
->header
.next
);
3551 /* unpin leaf page */
3554 /* offset beyond eof ? */
3560 /* get next leaf page */
3561 DT_GETPAGE(ip
, bn
, mp
, PSIZE
, p
, rc
);
3565 /* start with 1st entry of next leaf page */
3567 dtoffset
->index
= 0;
3571 /* return target leaf page pinned */
3572 btsp
= btstack
->top
;
3574 btsp
->index
= dtoffset
->index
;
3584 * function: compare search key with an internal entry
3587 * < 0 if k is < record
3588 * = 0 if k is = record
3589 * > 0 if k is > record
3591 static int dtCompare(struct component_name
* key
, /* search key */
3592 dtpage_t
* p
, /* directory page */
3594 { /* entry slot index */
3597 int klen
, namlen
, len
, rc
;
3598 struct idtentry
*ih
;
3602 * force the left-most key on internal pages, at any level of
3603 * the tree, to be less than any search key.
3604 * this obviates having to update the leftmost key on an internal
3605 * page when the user inserts a new key in the tree smaller than
3606 * anything that has been stored.
3608 * (? if/when dtSearch() narrows down to 1st entry (index = 0),
3609 * at any internal page at any level of the tree,
3610 * it descends to child of the entry anyway -
3611 * ? make the entry as min size dummy entry)
3613 * if (e->index == 0 && h->prevpg == P_INVALID && !(h->flags & BT_LEAF))
3620 ih
= (struct idtentry
*) & p
->slot
[si
];
3623 namlen
= ih
->namlen
;
3624 len
= min(namlen
, DTIHDRDATALEN
);
3626 /* compare with head/only segment */
3627 len
= min(klen
, len
);
3628 if ((rc
= UniStrncmp_le(kname
, name
, len
)))
3634 /* compare with additional segment(s) */
3636 while (klen
> 0 && namlen
> 0) {
3637 /* compare with next name segment */
3638 t
= (struct dtslot
*) & p
->slot
[si
];
3639 len
= min(namlen
, DTSLOTDATALEN
);
3640 len
= min(klen
, len
);
3642 if ((rc
= UniStrncmp_le(kname
, name
, len
)))
3651 return (klen
- namlen
);
3660 * function: compare search key with an (leaf/internal) entry
3663 * < 0 if k is < record
3664 * = 0 if k is = record
3665 * > 0 if k is > record
3667 static int ciCompare(struct component_name
* key
, /* search key */
3668 dtpage_t
* p
, /* directory page */
3669 int si
, /* entry slot index */
3674 int klen
, namlen
, len
, rc
;
3675 struct ldtentry
*lh
;
3676 struct idtentry
*ih
;
3681 * force the left-most key on internal pages, at any level of
3682 * the tree, to be less than any search key.
3683 * this obviates having to update the leftmost key on an internal
3684 * page when the user inserts a new key in the tree smaller than
3685 * anything that has been stored.
3687 * (? if/when dtSearch() narrows down to 1st entry (index = 0),
3688 * at any internal page at any level of the tree,
3689 * it descends to child of the entry anyway -
3690 * ? make the entry as min size dummy entry)
3692 * if (e->index == 0 && h->prevpg == P_INVALID && !(h->flags & BT_LEAF))
3702 if (p
->header
.flag
& BT_LEAF
) {
3703 lh
= (struct ldtentry
*) & p
->slot
[si
];
3706 namlen
= lh
->namlen
;
3707 if (flag
& JFS_DIR_INDEX
)
3708 len
= min(namlen
, DTLHDRDATALEN
);
3710 len
= min(namlen
, DTLHDRDATALEN_LEGACY
);
3713 * internal page entry
3716 ih
= (struct idtentry
*) & p
->slot
[si
];
3719 namlen
= ih
->namlen
;
3720 len
= min(namlen
, DTIHDRDATALEN
);
3723 /* compare with head/only segment */
3724 len
= min(klen
, len
);
3725 for (i
= 0; i
< len
; i
++, kname
++, name
++) {
3726 /* only uppercase if case-insensitive support is on */
3727 if ((flag
& JFS_OS2
) == JFS_OS2
)
3728 x
= UniToupper(le16_to_cpu(*name
));
3730 x
= le16_to_cpu(*name
);
3731 if ((rc
= *kname
- x
))
3738 /* compare with additional segment(s) */
3739 while (klen
> 0 && namlen
> 0) {
3740 /* compare with next name segment */
3741 t
= (struct dtslot
*) & p
->slot
[si
];
3742 len
= min(namlen
, DTSLOTDATALEN
);
3743 len
= min(klen
, len
);
3745 for (i
= 0; i
< len
; i
++, kname
++, name
++) {
3746 /* only uppercase if case-insensitive support is on */
3747 if ((flag
& JFS_OS2
) == JFS_OS2
)
3748 x
= UniToupper(le16_to_cpu(*name
));
3750 x
= le16_to_cpu(*name
);
3752 if ((rc
= *kname
- x
))
3761 return (klen
- namlen
);
3766 * ciGetLeafPrefixKey()
3768 * function: compute prefix of suffix compression
3769 * from two adjacent leaf entries
3770 * across page boundary
3772 * return: non-zero on error
3775 static int ciGetLeafPrefixKey(dtpage_t
* lp
, int li
, dtpage_t
* rp
,
3776 int ri
, struct component_name
* key
, int flag
)
3779 wchar_t *pl
, *pr
, *kname
;
3780 struct component_name lkey
;
3781 struct component_name rkey
;
3783 lkey
.name
= kmalloc_array(JFS_NAME_MAX
+ 1, sizeof(wchar_t),
3785 if (lkey
.name
== NULL
)
3788 rkey
.name
= kmalloc_array(JFS_NAME_MAX
+ 1, sizeof(wchar_t),
3790 if (rkey
.name
== NULL
) {
3795 /* get left and right key */
3796 dtGetKey(lp
, li
, &lkey
, flag
);
3797 lkey
.name
[lkey
.namlen
] = 0;
3799 if ((flag
& JFS_OS2
) == JFS_OS2
)
3802 dtGetKey(rp
, ri
, &rkey
, flag
);
3803 rkey
.name
[rkey
.namlen
] = 0;
3806 if ((flag
& JFS_OS2
) == JFS_OS2
)
3809 /* compute prefix */
3812 namlen
= min(lkey
.namlen
, rkey
.namlen
);
3813 for (pl
= lkey
.name
, pr
= rkey
.name
;
3814 namlen
; pl
++, pr
++, namlen
--, klen
++, kname
++) {
3817 key
->namlen
= klen
+ 1;
3822 /* l->namlen <= r->namlen since l <= r */
3823 if (lkey
.namlen
< rkey
.namlen
) {
3825 key
->namlen
= klen
+ 1;
3826 } else /* l->namelen == r->namelen */
3840 * function: get key of the entry
3842 static void dtGetKey(dtpage_t
* p
, int i
, /* entry index */
3843 struct component_name
* key
, int flag
)
3847 struct ldtentry
*lh
;
3848 struct idtentry
*ih
;
3855 stbl
= DT_GETSTBL(p
);
3857 if (p
->header
.flag
& BT_LEAF
) {
3858 lh
= (struct ldtentry
*) & p
->slot
[si
];
3860 namlen
= lh
->namlen
;
3862 if (flag
& JFS_DIR_INDEX
)
3863 len
= min(namlen
, DTLHDRDATALEN
);
3865 len
= min(namlen
, DTLHDRDATALEN_LEGACY
);
3867 ih
= (struct idtentry
*) & p
->slot
[si
];
3869 namlen
= ih
->namlen
;
3871 len
= min(namlen
, DTIHDRDATALEN
);
3874 key
->namlen
= namlen
;
3878 * move head/only segment
3880 UniStrncpy_from_le(kname
, name
, len
);
3883 * move additional segment(s)
3886 /* get next segment */
3890 len
= min(namlen
, DTSLOTDATALEN
);
3891 UniStrncpy_from_le(kname
, t
->name
, len
);
3901 * function: allocate free slot(s) and
3902 * write a leaf/internal entry
3904 * return: entry slot index
3906 static void dtInsertEntry(dtpage_t
* p
, int index
, struct component_name
* key
,
3907 ddata_t
* data
, struct dt_lock
** dtlock
)
3909 struct dtslot
*h
, *t
;
3910 struct ldtentry
*lh
= NULL
;
3911 struct idtentry
*ih
= NULL
;
3912 int hsi
, fsi
, klen
, len
, nextindex
;
3917 struct dt_lock
*dtlck
= *dtlock
;
3921 struct metapage
*mp
= NULL
;
3926 /* allocate a free slot */
3927 hsi
= fsi
= p
->header
.freelist
;
3929 p
->header
.freelist
= h
->next
;
3930 --p
->header
.freecnt
;
3932 /* open new linelock */
3933 if (dtlck
->index
>= dtlck
->maxcnt
)
3934 dtlck
= (struct dt_lock
*) txLinelock(dtlck
);
3936 lv
= & dtlck
->lv
[dtlck
->index
];
3939 /* write head/only segment */
3940 if (p
->header
.flag
& BT_LEAF
) {
3941 lh
= (struct ldtentry
*) h
;
3943 lh
->inumber
= cpu_to_le32(data
->leaf
.ino
);
3946 if (data
->leaf
.ip
) {
3947 len
= min(klen
, DTLHDRDATALEN
);
3948 if (!(p
->header
.flag
& BT_ROOT
))
3949 bn
= addressPXD(&p
->header
.self
);
3950 lh
->index
= cpu_to_le32(add_index(data
->leaf
.tid
,
3954 len
= min(klen
, DTLHDRDATALEN_LEGACY
);
3956 ih
= (struct idtentry
*) h
;
3962 len
= min(klen
, DTIHDRDATALEN
);
3965 UniStrncpy_to_le(name
, kname
, len
);
3970 /* write additional segment(s) */
3975 fsi
= p
->header
.freelist
;
3977 p
->header
.freelist
= t
->next
;
3978 --p
->header
.freecnt
;
3980 /* is next slot contiguous ? */
3981 if (fsi
!= xsi
+ 1) {
3982 /* close current linelock */
3986 /* open new linelock */
3987 if (dtlck
->index
< dtlck
->maxcnt
)
3990 dtlck
= (struct dt_lock
*) txLinelock(dtlck
);
3991 lv
= & dtlck
->lv
[0];
3999 len
= min(klen
, DTSLOTDATALEN
);
4000 UniStrncpy_to_le(t
->name
, kname
, len
);
4007 /* close current linelock */
4013 /* terminate last/only segment */
4015 /* single segment entry */
4016 if (p
->header
.flag
& BT_LEAF
)
4021 /* multi-segment entry */
4024 /* if insert into middle, shift right succeeding entries in stbl */
4025 stbl
= DT_GETSTBL(p
);
4026 nextindex
= p
->header
.nextindex
;
4027 if (index
< nextindex
) {
4028 memmove(stbl
+ index
+ 1, stbl
+ index
, nextindex
- index
);
4030 if ((p
->header
.flag
& BT_LEAF
) && data
->leaf
.ip
) {
4034 * Need to update slot number for entries that moved
4038 for (n
= index
+ 1; n
<= nextindex
; n
++) {
4039 lh
= (struct ldtentry
*) & (p
->slot
[stbl
[n
]]);
4040 modify_index(data
->leaf
.tid
, data
->leaf
.ip
,
4041 le32_to_cpu(lh
->index
), bn
, n
,
4045 release_metapage(mp
);
4051 /* advance next available entry index of stbl */
4052 ++p
->header
.nextindex
;
4059 * function: move entries from split/left page to new/right page
4061 * nextindex of dst page and freelist/freecnt of both pages
4064 static void dtMoveEntry(dtpage_t
* sp
, int si
, dtpage_t
* dp
,
4065 struct dt_lock
** sdtlock
, struct dt_lock
** ddtlock
,
4068 int ssi
, next
; /* src slot index */
4069 int di
; /* dst entry index */
4070 int dsi
; /* dst slot index */
4071 s8
*sstbl
, *dstbl
; /* sorted entry table */
4073 struct ldtentry
*slh
, *dlh
= NULL
;
4074 struct idtentry
*sih
, *dih
= NULL
;
4075 struct dtslot
*h
, *s
, *d
;
4076 struct dt_lock
*sdtlck
= *sdtlock
, *ddtlck
= *ddtlock
;
4077 struct lv
*slv
, *dlv
;
4081 sstbl
= (s8
*) & sp
->slot
[sp
->header
.stblindex
];
4082 dstbl
= (s8
*) & dp
->slot
[dp
->header
.stblindex
];
4084 dsi
= dp
->header
.freelist
; /* first (whole page) free slot */
4085 sfsi
= sp
->header
.freelist
;
4087 /* linelock destination entry slot */
4088 dlv
= & ddtlck
->lv
[ddtlck
->index
];
4091 /* linelock source entry slot */
4092 slv
= & sdtlck
->lv
[sdtlck
->index
];
4093 slv
->offset
= sstbl
[si
];
4094 xssi
= slv
->offset
- 1;
4100 for (di
= 0; si
< sp
->header
.nextindex
; si
++, di
++) {
4104 /* is next slot contiguous ? */
4105 if (ssi
!= xssi
+ 1) {
4106 /* close current linelock */
4110 /* open new linelock */
4111 if (sdtlck
->index
< sdtlck
->maxcnt
)
4114 sdtlck
= (struct dt_lock
*) txLinelock(sdtlck
);
4115 slv
= & sdtlck
->lv
[0];
4123 * move head/only segment of an entry
4126 h
= d
= &dp
->slot
[dsi
];
4128 /* get src slot and move */
4130 if (sp
->header
.flag
& BT_LEAF
) {
4131 /* get source entry */
4132 slh
= (struct ldtentry
*) s
;
4133 dlh
= (struct ldtentry
*) h
;
4134 snamlen
= slh
->namlen
;
4137 len
= min(snamlen
, DTLHDRDATALEN
);
4138 dlh
->index
= slh
->index
; /* little-endian */
4140 len
= min(snamlen
, DTLHDRDATALEN_LEGACY
);
4142 memcpy(dlh
, slh
, 6 + len
* 2);
4146 /* update dst head/only segment next field */
4150 sih
= (struct idtentry
*) s
;
4151 snamlen
= sih
->namlen
;
4153 len
= min(snamlen
, DTIHDRDATALEN
);
4154 dih
= (struct idtentry
*) h
;
4155 memcpy(dih
, sih
, 10 + len
* 2);
4162 /* free src head/only segment */
4172 * move additional segment(s) of the entry
4175 while ((ssi
= next
) >= 0) {
4176 /* is next slot contiguous ? */
4177 if (ssi
!= xssi
+ 1) {
4178 /* close current linelock */
4182 /* open new linelock */
4183 if (sdtlck
->index
< sdtlck
->maxcnt
)
4189 slv
= & sdtlck
->lv
[0];
4196 /* get next source segment */
4199 /* get next destination free slot */
4202 len
= min(snamlen
, DTSLOTDATALEN
);
4203 UniStrncpy_le(d
->name
, s
->name
, len
);
4212 /* free source segment */
4221 /* terminate dst last/only segment */
4223 /* single segment entry */
4224 if (dp
->header
.flag
& BT_LEAF
)
4229 /* multi-segment entry */
4233 /* close current linelock */
4242 /* update source header */
4243 sp
->header
.freelist
= sfsi
;
4244 sp
->header
.freecnt
+= nd
;
4246 /* update destination header */
4247 dp
->header
.nextindex
= di
;
4249 dp
->header
.freelist
= dsi
;
4250 dp
->header
.freecnt
-= nd
;
4257 * function: free a (leaf/internal) entry
4259 * log freelist header, stbl, and each segment slot of entry
4260 * (even though last/only segment next field is modified,
4261 * physical image logging requires all segment slots of
4262 * the entry logged to avoid applying previous updates
4263 * to the same slots)
4265 static void dtDeleteEntry(dtpage_t
* p
, int fi
, struct dt_lock
** dtlock
)
4267 int fsi
; /* free entry slot index */
4271 struct dt_lock
*dtlck
= *dtlock
;
4275 /* get free entry slot index */
4276 stbl
= DT_GETSTBL(p
);
4279 /* open new linelock */
4280 if (dtlck
->index
>= dtlck
->maxcnt
)
4281 dtlck
= (struct dt_lock
*) txLinelock(dtlck
);
4282 lv
= & dtlck
->lv
[dtlck
->index
];
4286 /* get the head/only segment */
4288 if (p
->header
.flag
& BT_LEAF
)
4289 si
= ((struct ldtentry
*) t
)->next
;
4291 si
= ((struct idtentry
*) t
)->next
;
4298 /* find the last/only segment */
4300 /* is next slot contiguous ? */
4301 if (si
!= xsi
+ 1) {
4302 /* close current linelock */
4306 /* open new linelock */
4307 if (dtlck
->index
< dtlck
->maxcnt
)
4310 dtlck
= (struct dt_lock
*) txLinelock(dtlck
);
4311 lv
= & dtlck
->lv
[0];
4327 /* close current linelock */
4333 /* update freelist */
4334 t
->next
= p
->header
.freelist
;
4335 p
->header
.freelist
= fsi
;
4336 p
->header
.freecnt
+= freecnt
;
4338 /* if delete from middle,
4339 * shift left the succedding entries in the stbl
4341 si
= p
->header
.nextindex
;
4343 memmove(&stbl
[fi
], &stbl
[fi
+ 1], si
- fi
- 1);
4345 p
->header
.nextindex
--;
4352 * function: truncate a (leaf/internal) entry
4354 * log freelist header, stbl, and each segment slot of entry
4355 * (even though last/only segment next field is modified,
4356 * physical image logging requires all segment slots of
4357 * the entry logged to avoid applying previous updates
4358 * to the same slots)
4360 static void dtTruncateEntry(dtpage_t
* p
, int ti
, struct dt_lock
** dtlock
)
4362 int tsi
; /* truncate entry slot index */
4366 struct dt_lock
*dtlck
= *dtlock
;
4370 /* get free entry slot index */
4371 stbl
= DT_GETSTBL(p
);
4374 /* open new linelock */
4375 if (dtlck
->index
>= dtlck
->maxcnt
)
4376 dtlck
= (struct dt_lock
*) txLinelock(dtlck
);
4377 lv
= & dtlck
->lv
[dtlck
->index
];
4381 /* get the head/only segment */
4383 ASSERT(p
->header
.flag
& BT_INTERNAL
);
4384 ((struct idtentry
*) t
)->namlen
= 0;
4385 si
= ((struct idtentry
*) t
)->next
;
4386 ((struct idtentry
*) t
)->next
= -1;
4393 /* find the last/only segment */
4395 /* is next slot contiguous ? */
4396 if (si
!= xsi
+ 1) {
4397 /* close current linelock */
4401 /* open new linelock */
4402 if (dtlck
->index
< dtlck
->maxcnt
)
4405 dtlck
= (struct dt_lock
*) txLinelock(dtlck
);
4406 lv
= & dtlck
->lv
[0];
4422 /* close current linelock */
4428 /* update freelist */
4431 t
->next
= p
->header
.freelist
;
4432 p
->header
.freelist
= fsi
;
4433 p
->header
.freecnt
+= freecnt
;
4438 * dtLinelockFreelist()
4440 static void dtLinelockFreelist(dtpage_t
* p
, /* directory page */
4441 int m
, /* max slot index */
4442 struct dt_lock
** dtlock
)
4444 int fsi
; /* free entry slot index */
4447 struct dt_lock
*dtlck
= *dtlock
;
4451 /* get free entry slot index */
4452 fsi
= p
->header
.freelist
;
4454 /* open new linelock */
4455 if (dtlck
->index
>= dtlck
->maxcnt
)
4456 dtlck
= (struct dt_lock
*) txLinelock(dtlck
);
4457 lv
= & dtlck
->lv
[dtlck
->index
];
4467 /* find the last/only segment */
4468 while (si
< m
&& si
>= 0) {
4469 /* is next slot contiguous ? */
4470 if (si
!= xsi
+ 1) {
4471 /* close current linelock */
4475 /* open new linelock */
4476 if (dtlck
->index
< dtlck
->maxcnt
)
4479 dtlck
= (struct dt_lock
*) txLinelock(dtlck
);
4480 lv
= & dtlck
->lv
[0];
4494 /* close current linelock */
4505 * FUNCTION: Modify the inode number part of a directory entry
4508 * tid - Transaction id
4509 * ip - Inode of parent directory
4510 * key - Name of entry to be modified
4511 * orig_ino - Original inode number expected in entry
4512 * new_ino - New inode number to put into entry
4516 * -ESTALE - If entry found does not match orig_ino passed in
4517 * -ENOENT - If no entry can be found to match key
4518 * 0 - If successfully modified entry
4520 int dtModify(tid_t tid
, struct inode
*ip
,
4521 struct component_name
* key
, ino_t
* orig_ino
, ino_t new_ino
, int flag
)
4525 struct metapage
*mp
;
4528 struct btstack btstack
;
4530 struct dt_lock
*dtlck
;
4533 int entry_si
; /* entry slot index */
4534 struct ldtentry
*entry
;
4537 * search for the entry to modify:
4539 * dtSearch() returns (leaf page pinned, index at which to modify).
4541 if ((rc
= dtSearch(ip
, key
, orig_ino
, &btstack
, flag
)))
4544 /* retrieve search result */
4545 DT_GETSEARCH(ip
, btstack
.top
, bn
, mp
, p
, index
);
4547 BT_MARK_DIRTY(mp
, ip
);
4549 * acquire a transaction lock on the leaf page of named entry
4551 tlck
= txLock(tid
, ip
, mp
, tlckDTREE
| tlckENTRY
);
4552 dtlck
= (struct dt_lock
*) & tlck
->lock
;
4554 /* get slot index of the entry */
4555 stbl
= DT_GETSTBL(p
);
4556 entry_si
= stbl
[index
];
4558 /* linelock entry */
4559 ASSERT(dtlck
->index
== 0);
4560 lv
= & dtlck
->lv
[0];
4561 lv
->offset
= entry_si
;
4565 /* get the head/only segment */
4566 entry
= (struct ldtentry
*) & p
->slot
[entry_si
];
4568 /* substitute the inode number of the entry */
4569 entry
->inumber
= cpu_to_le32(new_ino
);
4571 /* unpin the leaf page */