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 mp
= get_index_page(ip
, 0);
418 jfs_err("add_index: get_metapage failed!");
419 xtTruncate(tid
, ip
, 0, COMMIT_PWMAP
);
420 memcpy(&jfs_ip
->i_dirtable
, temp_table
,
421 sizeof (temp_table
));
424 tlck
= txLock(tid
, ip
, mp
, tlckDATA
);
425 llck
= (struct linelock
*) & tlck
->lock
;
426 ASSERT(llck
->index
== 0);
430 lv
->length
= 6; /* tlckDATA slot size is 16 bytes */
433 memcpy(mp
->data
, temp_table
, sizeof(temp_table
));
435 mark_metapage_dirty(mp
);
436 release_metapage(mp
);
439 * Logging is now directed by xtree tlocks
441 clear_cflag(COMMIT_Dirtable
, ip
);
444 offset
= (index
- 2) * sizeof(struct dir_table_slot
);
445 page_offset
= offset
& (PSIZE
- 1);
446 blkno
= ((offset
+ 1) >> L2PSIZE
) << sbi
->l2nbperpage
;
447 if (page_offset
== 0) {
449 * This will be the beginning of a new page
452 if (xtInsert(tid
, ip
, 0, blkno
, sbi
->nbperpage
, &xaddr
, 0)) {
453 jfs_warn("add_index: xtInsert failed!");
458 if ((mp
= get_index_page(ip
, blkno
)))
459 memset(mp
->data
, 0, PSIZE
); /* Just looks better */
461 xtTruncate(tid
, ip
, offset
, COMMIT_PWMAP
);
463 mp
= read_index_page(ip
, blkno
);
466 jfs_err("add_index: get/read_metapage failed!");
470 lock_index(tid
, ip
, mp
, index
);
473 (struct dir_table_slot
*) ((char *) mp
->data
+ page_offset
);
474 dirtab_slot
->flag
= DIR_INDEX_VALID
;
475 dirtab_slot
->slot
= slot
;
476 DTSaddress(dirtab_slot
, bn
);
478 mark_metapage_dirty(mp
);
479 release_metapage(mp
);
485 jfs_ip
->next_index
--;
493 * Marks an entry to the directory index table as free.
495 static void free_index(tid_t tid
, struct inode
*ip
, u32 index
, u32 next
)
497 struct dir_table_slot
*dirtab_slot
;
499 struct metapage
*mp
= NULL
;
501 dirtab_slot
= find_index(ip
, index
, &mp
, &lblock
);
506 dirtab_slot
->flag
= DIR_INDEX_FREE
;
507 dirtab_slot
->slot
= dirtab_slot
->addr1
= 0;
508 dirtab_slot
->addr2
= cpu_to_le32(next
);
511 lock_index(tid
, ip
, mp
, index
);
512 mark_metapage_dirty(mp
);
513 release_metapage(mp
);
515 set_cflag(COMMIT_Dirtable
, ip
);
521 * Changes an entry in the directory index table
523 static void modify_index(tid_t tid
, struct inode
*ip
, u32 index
, s64 bn
,
524 int slot
, struct metapage
** mp
, s64
*lblock
)
526 struct dir_table_slot
*dirtab_slot
;
528 dirtab_slot
= find_index(ip
, index
, mp
, lblock
);
533 DTSaddress(dirtab_slot
, bn
);
534 dirtab_slot
->slot
= slot
;
537 lock_index(tid
, ip
, *mp
, index
);
538 mark_metapage_dirty(*mp
);
540 set_cflag(COMMIT_Dirtable
, ip
);
546 * reads a directory table slot
548 static int read_index(struct inode
*ip
, u32 index
,
549 struct dir_table_slot
* dirtab_slot
)
552 struct metapage
*mp
= NULL
;
553 struct dir_table_slot
*slot
;
555 slot
= find_index(ip
, index
, &mp
, &lblock
);
560 memcpy(dirtab_slot
, slot
, sizeof(struct dir_table_slot
));
563 release_metapage(mp
);
572 * Search for the entry with specified key
576 * return: 0 - search result on stack, leaf page pinned;
579 int dtSearch(struct inode
*ip
, struct component_name
* key
, ino_t
* data
,
580 struct btstack
* btstack
, int flag
)
583 int cmp
= 1; /* init for empty page */
588 int base
, index
, lim
;
589 struct btframe
*btsp
;
591 int psize
= 288; /* initial in-line directory */
593 struct component_name ciKey
;
594 struct super_block
*sb
= ip
->i_sb
;
596 ciKey
.name
= kmalloc((JFS_NAME_MAX
+ 1) * sizeof(wchar_t), GFP_NOFS
);
603 /* uppercase search key for c-i directory */
604 UniStrcpy(ciKey
.name
, key
->name
);
605 ciKey
.namlen
= key
->namlen
;
607 /* only uppercase if case-insensitive support is on */
608 if ((JFS_SBI(sb
)->mntflag
& JFS_OS2
) == JFS_OS2
) {
611 BT_CLR(btstack
); /* reset stack */
613 /* init level count for max pages to split */
617 * search down tree from root:
619 * between two consecutive entries of <Ki, Pi> and <Kj, Pj> of
620 * internal page, child page Pi contains entry with k, Ki <= K < Kj.
622 * if entry with search key K is not found
623 * internal page search find the entry with largest key Ki
624 * less than K which point to the child page to search;
625 * leaf page search find the entry with smallest key Kj
626 * greater than K so that the returned index is the position of
627 * the entry to be shifted right for insertion of new entry.
628 * for empty tree, search key is greater than any key of the tree.
630 * by convention, root bn = 0.
633 /* get/pin the page to search */
634 DT_GETPAGE(ip
, bn
, mp
, psize
, p
, rc
);
638 /* get sorted entry table of the page */
639 stbl
= DT_GETSTBL(p
);
642 * binary search with search key K on the current page.
644 for (base
= 0, lim
= p
->header
.nextindex
; lim
; lim
>>= 1) {
645 index
= base
+ (lim
>> 1);
647 if (p
->header
.flag
& BT_LEAF
) {
648 /* uppercase leaf name to compare */
650 ciCompare(&ciKey
, p
, stbl
[index
],
651 JFS_SBI(sb
)->mntflag
);
653 /* router key is in uppercase */
655 cmp
= dtCompare(&ciKey
, p
, stbl
[index
]);
663 /* search hit - leaf page:
664 * return the entry found
666 if (p
->header
.flag
& BT_LEAF
) {
667 inumber
= le32_to_cpu(
668 ((struct ldtentry
*) & p
->slot
[stbl
[index
]])->inumber
);
671 * search for JFS_LOOKUP
673 if (flag
== JFS_LOOKUP
) {
680 * search for JFS_CREATE
682 if (flag
== JFS_CREATE
) {
689 * search for JFS_REMOVE or JFS_RENAME
691 if ((flag
== JFS_REMOVE
||
692 flag
== JFS_RENAME
) &&
699 * JFS_REMOVE|JFS_FINDDIR|JFS_RENAME
701 /* save search result */
712 /* search hit - internal page:
713 * descend/search its child page
727 * base is the smallest index with key (Kj) greater than
728 * search key (K) and may be zero or (maxindex + 1) index.
731 * search miss - leaf page
733 * return location of entry (base) where new entry with
734 * search key K is to be inserted.
736 if (p
->header
.flag
& BT_LEAF
) {
738 * search for JFS_LOOKUP, JFS_REMOVE, or JFS_RENAME
740 if (flag
== JFS_LOOKUP
|| flag
== JFS_REMOVE
||
741 flag
== JFS_RENAME
) {
747 * search for JFS_CREATE|JFS_FINDDIR:
762 * search miss - internal page
764 * if base is non-zero, decrement base by one to get the parent
765 * entry of the child page to search.
767 index
= base
? base
- 1 : base
;
770 * go down to child page
773 /* update max. number of pages to split */
774 if (BT_STACK_FULL(btstack
)) {
775 /* Something's corrupted, mark filesystem dirty so
776 * chkdsk will fix it.
778 jfs_error(sb
, "stack overrun in dtSearch!");
779 BT_STACK_DUMP(btstack
);
785 /* push (bn, index) of the parent page/entry */
786 BT_PUSH(btstack
, bn
, index
);
788 /* get the child page block number */
789 pxd
= (pxd_t
*) & p
->slot
[stbl
[index
]];
790 bn
= addressPXD(pxd
);
791 psize
= lengthPXD(pxd
) << JFS_SBI(ip
->i_sb
)->l2bsize
;
793 /* unpin the parent page */
813 * function: insert an entry to directory tree
817 * return: 0 - success;
820 int dtInsert(tid_t tid
, struct inode
*ip
,
821 struct component_name
* name
, ino_t
* fsn
, struct btstack
* btstack
)
824 struct metapage
*mp
; /* meta-page buffer */
825 dtpage_t
*p
; /* base B+-tree index page */
828 struct dtsplit split
; /* split information */
830 struct dt_lock
*dtlck
;
836 * retrieve search result
838 * dtSearch() returns (leaf page pinned, index at which to insert).
839 * n.b. dtSearch() may return index of (maxindex + 1) of
842 DT_GETSEARCH(ip
, btstack
->top
, bn
, mp
, p
, index
);
845 * insert entry for new key
848 if (JFS_IP(ip
)->next_index
== DIREND
) {
852 n
= NDTLEAF(name
->namlen
);
856 n
= NDTLEAF_LEGACY(name
->namlen
);
857 data
.leaf
.ip
= NULL
; /* signifies legacy directory format */
859 data
.leaf
.ino
= *fsn
;
862 * leaf page does not have enough room for new entry:
864 * extend/split the leaf page;
866 * dtSplitUp() will insert the entry and unpin the leaf page.
868 if (n
> p
->header
.freecnt
) {
874 rc
= dtSplitUp(tid
, ip
, &split
, btstack
);
879 * leaf page does have enough room for new entry:
881 * insert the new data entry into the leaf page;
883 BT_MARK_DIRTY(mp
, ip
);
885 * acquire a transaction lock on the leaf page
887 tlck
= txLock(tid
, ip
, mp
, tlckDTREE
| tlckENTRY
);
888 dtlck
= (struct dt_lock
*) & tlck
->lock
;
889 ASSERT(dtlck
->index
== 0);
892 /* linelock header */
897 dtInsertEntry(p
, index
, name
, &data
, &dtlck
);
899 /* linelock stbl of non-root leaf page */
900 if (!(p
->header
.flag
& BT_ROOT
)) {
901 if (dtlck
->index
>= dtlck
->maxcnt
)
902 dtlck
= (struct dt_lock
*) txLinelock(dtlck
);
903 lv
= & dtlck
->lv
[dtlck
->index
];
904 n
= index
>> L2DTSLOTSIZE
;
905 lv
->offset
= p
->header
.stblindex
+ n
;
907 ((p
->header
.nextindex
- 1) >> L2DTSLOTSIZE
) - n
+ 1;
911 /* unpin the leaf page */
921 * function: propagate insertion bottom up;
925 * return: 0 - success;
927 * leaf page unpinned;
929 static int dtSplitUp(tid_t tid
,
930 struct inode
*ip
, struct dtsplit
* split
, struct btstack
* btstack
)
932 struct jfs_sb_info
*sbi
= JFS_SBI(ip
->i_sb
);
934 struct metapage
*smp
;
935 dtpage_t
*sp
; /* split page */
936 struct metapage
*rmp
;
937 dtpage_t
*rp
; /* new right page split from sp */
938 pxd_t rpxd
; /* new right page extent descriptor */
939 struct metapage
*lmp
;
940 dtpage_t
*lp
; /* left child page */
941 int skip
; /* index of entry of insertion */
942 struct btframe
*parent
; /* parent page entry on traverse stack */
945 struct pxdlist pxdlist
;
947 struct component_name key
= { 0, NULL
};
948 ddata_t
*data
= split
->data
;
950 struct dt_lock
*dtlck
;
953 int quota_allocation
= 0;
957 sp
= DT_PAGE(ip
, smp
);
959 key
.name
= kmalloc((JFS_NAME_MAX
+ 2) * sizeof(wchar_t), GFP_NOFS
);
969 * The split routines insert the new entry, and
970 * acquire txLock as appropriate.
973 * split root leaf page:
975 if (sp
->header
.flag
& BT_ROOT
) {
977 * allocate a single extent child page
980 n
= sbi
->bsize
>> L2DTSLOTSIZE
;
981 n
-= (n
+ 31) >> L2DTSLOTSIZE
; /* stbl size */
982 n
-= DTROOTMAXSLOT
- sp
->header
.freecnt
; /* header + entries */
983 if (n
<= split
->nslot
)
985 if ((rc
= dbAlloc(ip
, 0, (s64
) xlen
, &xaddr
))) {
992 pxd
= &pxdlist
.pxd
[0];
993 PXDaddress(pxd
, xaddr
);
994 PXDlength(pxd
, xlen
);
995 split
->pxdlist
= &pxdlist
;
996 rc
= dtSplitRoot(tid
, ip
, split
, &rmp
);
999 dbFree(ip
, xaddr
, xlen
);
1006 ip
->i_size
= xlen
<< sbi
->l2bsize
;
1012 * extend first leaf page
1014 * extend the 1st extent if less than buffer page size
1015 * (dtExtendPage() reurns leaf page unpinned)
1017 pxd
= &sp
->header
.self
;
1018 xlen
= lengthPXD(pxd
);
1019 xsize
= xlen
<< sbi
->l2bsize
;
1020 if (xsize
< PSIZE
) {
1021 xaddr
= addressPXD(pxd
);
1022 n
= xsize
>> L2DTSLOTSIZE
;
1023 n
-= (n
+ 31) >> L2DTSLOTSIZE
; /* stbl size */
1024 if ((n
+ sp
->header
.freecnt
) <= split
->nslot
)
1025 n
= xlen
+ (xlen
<< 1);
1029 /* Allocate blocks to quota. */
1030 rc
= dquot_alloc_block(ip
, n
);
1033 quota_allocation
+= n
;
1035 if ((rc
= dbReAlloc(sbi
->ipbmap
, xaddr
, (s64
) xlen
,
1039 pxdlist
.maxnpxd
= 1;
1041 pxd
= &pxdlist
.pxd
[0];
1042 PXDaddress(pxd
, nxaddr
)
1043 PXDlength(pxd
, xlen
+ n
);
1044 split
->pxdlist
= &pxdlist
;
1045 if ((rc
= dtExtendPage(tid
, ip
, split
, btstack
))) {
1046 nxaddr
= addressPXD(pxd
);
1047 if (xaddr
!= nxaddr
) {
1048 /* free relocated extent */
1049 xlen
= lengthPXD(pxd
);
1050 dbFree(ip
, nxaddr
, (s64
) xlen
);
1052 /* free extended delta */
1053 xlen
= lengthPXD(pxd
) - n
;
1054 xaddr
= addressPXD(pxd
) + xlen
;
1055 dbFree(ip
, xaddr
, (s64
) n
);
1057 } else if (!DO_INDEX(ip
))
1058 ip
->i_size
= lengthPXD(pxd
) << sbi
->l2bsize
;
1067 * split leaf page <sp> into <sp> and a new right page <rp>.
1069 * return <rp> pinned and its extent descriptor <rpxd>
1072 * allocate new directory page extent and
1073 * new index page(s) to cover page split(s)
1075 * allocation hint: ?
1077 n
= btstack
->nsplit
;
1078 pxdlist
.maxnpxd
= pxdlist
.npxd
= 0;
1079 xlen
= sbi
->nbperpage
;
1080 for (pxd
= pxdlist
.pxd
; n
> 0; n
--, pxd
++) {
1081 if ((rc
= dbAlloc(ip
, 0, (s64
) xlen
, &xaddr
)) == 0) {
1082 PXDaddress(pxd
, xaddr
);
1083 PXDlength(pxd
, xlen
);
1090 /* undo allocation */
1094 split
->pxdlist
= &pxdlist
;
1095 if ((rc
= dtSplitPage(tid
, ip
, split
, &rmp
, &rp
, &rpxd
))) {
1098 /* undo allocation */
1103 ip
->i_size
+= PSIZE
;
1106 * propagate up the router entry for the leaf page just split
1108 * insert a router entry for the new page into the parent page,
1109 * propagate the insert/split up the tree by walking back the stack
1110 * of (bn of parent page, index of child page entry in parent page)
1111 * that were traversed during the search for the page that split.
1113 * the propagation of insert/split up the tree stops if the root
1114 * splits or the page inserted into doesn't have to split to hold
1117 * the parent entry for the split page remains the same, and
1118 * a new entry is inserted at its right with the first key and
1119 * block number of the new right page.
1121 * There are a maximum of 4 pages pinned at any time:
1122 * two children, left parent and right parent (when the parent splits).
1123 * keep the child pages pinned while working on the parent.
1124 * make sure that all pins are released at exit.
1126 while ((parent
= BT_POP(btstack
)) != NULL
) {
1127 /* parent page specified by stack frame <parent> */
1129 /* keep current child pages (<lp>, <rp>) pinned */
1134 * insert router entry in parent for new right child page <rp>
1136 /* get the parent page <sp> */
1137 DT_GETPAGE(ip
, parent
->bn
, smp
, PSIZE
, sp
, rc
);
1145 * The new key entry goes ONE AFTER the index of parent entry,
1146 * because the split was to the right.
1148 skip
= parent
->index
+ 1;
1151 * compute the key for the router entry
1153 * key suffix compression:
1154 * for internal pages that have leaf pages as children,
1155 * retain only what's needed to distinguish between
1156 * the new entry and the entry on the page to its left.
1157 * If the keys compare equal, retain the entire key.
1159 * note that compression is performed only at computing
1160 * router key at the lowest internal level.
1161 * further compression of the key between pairs of higher
1162 * level internal pages loses too much information and
1163 * the search may fail.
1164 * (e.g., two adjacent leaf pages of {a, ..., x} {xx, ...,}
1165 * results in two adjacent parent entries (a)(xx).
1166 * if split occurs between these two entries, and
1167 * if compression is applied, the router key of parent entry
1168 * of right page (x) will divert search for x into right
1169 * subtree and miss x in the left subtree.)
1171 * the entire key must be retained for the next-to-leftmost
1172 * internal key at any level of the tree, or search may fail
1175 switch (rp
->header
.flag
& BT_TYPE
) {
1178 * compute the length of prefix for suffix compression
1179 * between last entry of left page and first entry
1182 if ((sp
->header
.flag
& BT_ROOT
&& skip
> 1) ||
1183 sp
->header
.prev
!= 0 || skip
> 1) {
1184 /* compute uppercase router prefix key */
1185 rc
= ciGetLeafPrefixKey(lp
,
1186 lp
->header
.nextindex
-1,
1196 /* next to leftmost entry of
1197 lowest internal level */
1199 /* compute uppercase router key */
1200 dtGetKey(rp
, 0, &key
, sbi
->mntflag
);
1201 key
.name
[key
.namlen
] = 0;
1203 if ((sbi
->mntflag
& JFS_OS2
) == JFS_OS2
)
1207 n
= NDTINTERNAL(key
.namlen
);
1211 dtGetKey(rp
, 0, &key
, sbi
->mntflag
);
1212 n
= NDTINTERNAL(key
.namlen
);
1216 jfs_err("dtSplitUp(): UFO!");
1220 /* unpin left child page */
1224 * compute the data for the router entry
1226 data
->xd
= rpxd
; /* child page xd */
1229 * parent page is full - split the parent page
1231 if (n
> sp
->header
.freecnt
) {
1232 /* init for parent page split */
1234 split
->index
= skip
; /* index at insert */
1237 /* split->data = data; */
1239 /* unpin right child page */
1242 /* The split routines insert the new entry,
1243 * acquire txLock as appropriate.
1244 * return <rp> pinned and its block number <rbn>.
1246 rc
= (sp
->header
.flag
& BT_ROOT
) ?
1247 dtSplitRoot(tid
, ip
, split
, &rmp
) :
1248 dtSplitPage(tid
, ip
, split
, &rmp
, &rp
, &rpxd
);
1254 /* smp and rmp are pinned */
1257 * parent page is not full - insert router entry in parent page
1260 BT_MARK_DIRTY(smp
, ip
);
1262 * acquire a transaction lock on the parent page
1264 tlck
= txLock(tid
, ip
, smp
, tlckDTREE
| tlckENTRY
);
1265 dtlck
= (struct dt_lock
*) & tlck
->lock
;
1266 ASSERT(dtlck
->index
== 0);
1267 lv
= & dtlck
->lv
[0];
1269 /* linelock header */
1274 /* linelock stbl of non-root parent page */
1275 if (!(sp
->header
.flag
& BT_ROOT
)) {
1277 n
= skip
>> L2DTSLOTSIZE
;
1278 lv
->offset
= sp
->header
.stblindex
+ n
;
1280 ((sp
->header
.nextindex
-
1281 1) >> L2DTSLOTSIZE
) - n
+ 1;
1285 dtInsertEntry(sp
, skip
, &key
, data
, &dtlck
);
1287 /* exit propagate up */
1292 /* unpin current split and its right page */
1297 * free remaining extents allocated for split
1301 pxd
= &pxdlist
.pxd
[n
];
1302 for (; n
< pxdlist
.maxnpxd
; n
++, pxd
++)
1303 dbFree(ip
, addressPXD(pxd
), (s64
) lengthPXD(pxd
));
1308 /* Rollback quota allocation */
1309 if (rc
&& quota_allocation
)
1310 dquot_free_block(ip
, quota_allocation
);
1321 * function: Split a non-root page of a btree.
1325 * return: 0 - success;
1327 * return split and new page pinned;
1329 static int dtSplitPage(tid_t tid
, struct inode
*ip
, struct dtsplit
* split
,
1330 struct metapage
** rmpp
, dtpage_t
** rpp
, pxd_t
* rpxdp
)
1333 struct metapage
*smp
;
1335 struct metapage
*rmp
;
1336 dtpage_t
*rp
; /* new right page allocated */
1337 s64 rbn
; /* new right page block number */
1338 struct metapage
*mp
;
1341 struct pxdlist
*pxdlist
;
1343 int skip
, nextindex
, half
, left
, nxt
, off
, si
;
1344 struct ldtentry
*ldtentry
;
1345 struct idtentry
*idtentry
;
1350 struct dt_lock
*sdtlck
, *rdtlck
;
1352 struct dt_lock
*dtlck
;
1353 struct lv
*slv
, *rlv
, *lv
;
1355 /* get split page */
1357 sp
= DT_PAGE(ip
, smp
);
1360 * allocate the new right page for the split
1362 pxdlist
= split
->pxdlist
;
1363 pxd
= &pxdlist
->pxd
[pxdlist
->npxd
];
1365 rbn
= addressPXD(pxd
);
1366 rmp
= get_metapage(ip
, rbn
, PSIZE
, 1);
1370 /* Allocate blocks to quota. */
1371 rc
= dquot_alloc_block(ip
, lengthPXD(pxd
));
1373 release_metapage(rmp
);
1377 jfs_info("dtSplitPage: ip:0x%p smp:0x%p rmp:0x%p", ip
, smp
, rmp
);
1379 BT_MARK_DIRTY(rmp
, ip
);
1381 * acquire a transaction lock on the new right page
1383 tlck
= txLock(tid
, ip
, rmp
, tlckDTREE
| tlckNEW
);
1384 rdtlck
= (struct dt_lock
*) & tlck
->lock
;
1386 rp
= (dtpage_t
*) rmp
->data
;
1388 rp
->header
.self
= *pxd
;
1390 BT_MARK_DIRTY(smp
, ip
);
1392 * acquire a transaction lock on the split page
1396 tlck
= txLock(tid
, ip
, smp
, tlckDTREE
| tlckENTRY
);
1397 sdtlck
= (struct dt_lock
*) & tlck
->lock
;
1399 /* linelock header of split page */
1400 ASSERT(sdtlck
->index
== 0);
1401 slv
= & sdtlck
->lv
[0];
1407 * initialize/update sibling pointers between sp and rp
1409 nextbn
= le64_to_cpu(sp
->header
.next
);
1410 rp
->header
.next
= cpu_to_le64(nextbn
);
1411 rp
->header
.prev
= cpu_to_le64(addressPXD(&sp
->header
.self
));
1412 sp
->header
.next
= cpu_to_le64(rbn
);
1415 * initialize new right page
1417 rp
->header
.flag
= sp
->header
.flag
;
1419 /* compute sorted entry table at start of extent data area */
1420 rp
->header
.nextindex
= 0;
1421 rp
->header
.stblindex
= 1;
1423 n
= PSIZE
>> L2DTSLOTSIZE
;
1424 rp
->header
.maxslot
= n
;
1425 stblsize
= (n
+ 31) >> L2DTSLOTSIZE
; /* in unit of slot */
1428 fsi
= rp
->header
.stblindex
+ stblsize
;
1429 rp
->header
.freelist
= fsi
;
1430 rp
->header
.freecnt
= rp
->header
.maxslot
- fsi
;
1433 * sequential append at tail: append without split
1435 * If splitting the last page on a level because of appending
1436 * a entry to it (skip is maxentry), it's likely that the access is
1437 * sequential. Adding an empty page on the side of the level is less
1438 * work and can push the fill factor much higher than normal.
1439 * If we're wrong it's no big deal, we'll just do the split the right
1441 * (It may look like it's equally easy to do a similar hack for
1442 * reverse sorted data, that is, split the tree left,
1443 * but it's not. Be my guest.)
1445 if (nextbn
== 0 && split
->index
== sp
->header
.nextindex
) {
1446 /* linelock header + stbl (first slot) of new page */
1447 rlv
= & rdtlck
->lv
[rdtlck
->index
];
1453 * initialize freelist of new right page
1456 for (fsi
++; fsi
< rp
->header
.maxslot
; f
++, fsi
++)
1460 /* insert entry at the first entry of the new right page */
1461 dtInsertEntry(rp
, 0, split
->key
, split
->data
, &rdtlck
);
1467 * non-sequential insert (at possibly middle page)
1471 * update prev pointer of previous right sibling page;
1474 DT_GETPAGE(ip
, nextbn
, mp
, PSIZE
, p
, rc
);
1476 discard_metapage(rmp
);
1480 BT_MARK_DIRTY(mp
, ip
);
1482 * acquire a transaction lock on the next page
1484 tlck
= txLock(tid
, ip
, mp
, tlckDTREE
| tlckRELINK
);
1485 jfs_info("dtSplitPage: tlck = 0x%p, ip = 0x%p, mp=0x%p",
1487 dtlck
= (struct dt_lock
*) & tlck
->lock
;
1489 /* linelock header of previous right sibling page */
1490 lv
= & dtlck
->lv
[dtlck
->index
];
1495 p
->header
.prev
= cpu_to_le64(rbn
);
1501 * split the data between the split and right pages.
1503 skip
= split
->index
;
1504 half
= (PSIZE
>> L2DTSLOTSIZE
) >> 1; /* swag */
1508 * compute fill factor for split pages
1510 * <nxt> traces the next entry to move to rp
1511 * <off> traces the next entry to stay in sp
1513 stbl
= (u8
*) & sp
->slot
[sp
->header
.stblindex
];
1514 nextindex
= sp
->header
.nextindex
;
1515 for (nxt
= off
= 0; nxt
< nextindex
; ++off
) {
1517 /* check for fill factor with new entry size */
1521 switch (sp
->header
.flag
& BT_TYPE
) {
1523 ldtentry
= (struct ldtentry
*) & sp
->slot
[si
];
1525 n
= NDTLEAF(ldtentry
->namlen
);
1527 n
= NDTLEAF_LEGACY(ldtentry
->
1532 idtentry
= (struct idtentry
*) & sp
->slot
[si
];
1533 n
= NDTINTERNAL(idtentry
->namlen
);
1540 ++nxt
; /* advance to next entry to move in sp */
1548 /* <nxt> poins to the 1st entry to move */
1551 * move entries to right page
1553 * dtMoveEntry() initializes rp and reserves entry for insertion
1555 * split page moved out entries are linelocked;
1556 * new/right page moved in entries are linelocked;
1558 /* linelock header + stbl of new right page */
1559 rlv
= & rdtlck
->lv
[rdtlck
->index
];
1564 dtMoveEntry(sp
, nxt
, rp
, &sdtlck
, &rdtlck
, DO_INDEX(ip
));
1566 sp
->header
.nextindex
= nxt
;
1569 * finalize freelist of new right page
1571 fsi
= rp
->header
.freelist
;
1573 for (fsi
++; fsi
< rp
->header
.maxslot
; f
++, fsi
++)
1578 * Update directory index table for entries now in right page
1580 if ((rp
->header
.flag
& BT_LEAF
) && DO_INDEX(ip
)) {
1584 stbl
= DT_GETSTBL(rp
);
1585 for (n
= 0; n
< rp
->header
.nextindex
; n
++) {
1586 ldtentry
= (struct ldtentry
*) & rp
->slot
[stbl
[n
]];
1587 modify_index(tid
, ip
, le32_to_cpu(ldtentry
->index
),
1588 rbn
, n
, &mp
, &lblock
);
1591 release_metapage(mp
);
1595 * the skipped index was on the left page,
1598 /* insert the new entry in the split page */
1599 dtInsertEntry(sp
, skip
, split
->key
, split
->data
, &sdtlck
);
1601 /* linelock stbl of split page */
1602 if (sdtlck
->index
>= sdtlck
->maxcnt
)
1603 sdtlck
= (struct dt_lock
*) txLinelock(sdtlck
);
1604 slv
= & sdtlck
->lv
[sdtlck
->index
];
1605 n
= skip
>> L2DTSLOTSIZE
;
1606 slv
->offset
= sp
->header
.stblindex
+ n
;
1608 ((sp
->header
.nextindex
- 1) >> L2DTSLOTSIZE
) - n
+ 1;
1612 * the skipped index was on the right page,
1615 /* adjust the skip index to reflect the new position */
1618 /* insert the new entry in the right page */
1619 dtInsertEntry(rp
, skip
, split
->key
, split
->data
, &rdtlck
);
1633 * function: extend 1st/only directory leaf page
1637 * return: 0 - success;
1639 * return extended page pinned;
1641 static int dtExtendPage(tid_t tid
,
1642 struct inode
*ip
, struct dtsplit
* split
, struct btstack
* btstack
)
1644 struct super_block
*sb
= ip
->i_sb
;
1646 struct metapage
*smp
, *pmp
, *mp
;
1648 struct pxdlist
*pxdlist
;
1651 int newstblindex
, newstblsize
;
1652 int oldstblindex
, oldstblsize
;
1655 struct btframe
*parent
;
1657 struct dt_lock
*dtlck
;
1660 struct pxd_lock
*pxdlock
;
1663 struct ldtentry
*ldtentry
;
1666 /* get page to extend */
1668 sp
= DT_PAGE(ip
, smp
);
1670 /* get parent/root page */
1671 parent
= BT_POP(btstack
);
1672 DT_GETPAGE(ip
, parent
->bn
, pmp
, PSIZE
, pp
, rc
);
1679 pxdlist
= split
->pxdlist
;
1680 pxd
= &pxdlist
->pxd
[pxdlist
->npxd
];
1683 xaddr
= addressPXD(pxd
);
1684 tpxd
= &sp
->header
.self
;
1685 txaddr
= addressPXD(tpxd
);
1686 /* in-place extension */
1687 if (xaddr
== txaddr
) {
1694 /* save moved extent descriptor for later free */
1695 tlck
= txMaplock(tid
, ip
, tlckDTREE
| tlckRELOCATE
);
1696 pxdlock
= (struct pxd_lock
*) & tlck
->lock
;
1697 pxdlock
->flag
= mlckFREEPXD
;
1698 pxdlock
->pxd
= sp
->header
.self
;
1702 * Update directory index table to reflect new page address
1708 stbl
= DT_GETSTBL(sp
);
1709 for (n
= 0; n
< sp
->header
.nextindex
; n
++) {
1711 (struct ldtentry
*) & sp
->slot
[stbl
[n
]];
1712 modify_index(tid
, ip
,
1713 le32_to_cpu(ldtentry
->index
),
1714 xaddr
, n
, &mp
, &lblock
);
1717 release_metapage(mp
);
1724 sp
->header
.self
= *pxd
;
1726 jfs_info("dtExtendPage: ip:0x%p smp:0x%p sp:0x%p", ip
, smp
, sp
);
1728 BT_MARK_DIRTY(smp
, ip
);
1730 * acquire a transaction lock on the extended/leaf page
1732 tlck
= txLock(tid
, ip
, smp
, tlckDTREE
| type
);
1733 dtlck
= (struct dt_lock
*) & tlck
->lock
;
1734 lv
= & dtlck
->lv
[0];
1736 /* update buffer extent descriptor of extended page */
1737 xlen
= lengthPXD(pxd
);
1738 xsize
= xlen
<< JFS_SBI(sb
)->l2bsize
;
1741 * copy old stbl to new stbl at start of extended area
1743 oldstblindex
= sp
->header
.stblindex
;
1744 oldstblsize
= (sp
->header
.maxslot
+ 31) >> L2DTSLOTSIZE
;
1745 newstblindex
= sp
->header
.maxslot
;
1746 n
= xsize
>> L2DTSLOTSIZE
;
1747 newstblsize
= (n
+ 31) >> L2DTSLOTSIZE
;
1748 memcpy(&sp
->slot
[newstblindex
], &sp
->slot
[oldstblindex
],
1749 sp
->header
.nextindex
);
1752 * in-line extension: linelock old area of extended page
1754 if (type
== tlckEXTEND
) {
1755 /* linelock header */
1761 /* linelock new stbl of extended page */
1762 lv
->offset
= newstblindex
;
1763 lv
->length
= newstblsize
;
1766 * relocation: linelock whole relocated area
1770 lv
->length
= sp
->header
.maxslot
+ newstblsize
;
1775 sp
->header
.maxslot
= n
;
1776 sp
->header
.stblindex
= newstblindex
;
1777 /* sp->header.nextindex remains the same */
1780 * add old stbl region at head of freelist
1784 last
= sp
->header
.freelist
;
1785 for (n
= 0; n
< oldstblsize
; n
++, fsi
++, f
++) {
1789 sp
->header
.freelist
= last
;
1790 sp
->header
.freecnt
+= oldstblsize
;
1793 * append free region of newly extended area at tail of freelist
1795 /* init free region of newly extended area */
1796 fsi
= n
= newstblindex
+ newstblsize
;
1798 for (fsi
++; fsi
< sp
->header
.maxslot
; f
++, fsi
++)
1802 /* append new free region at tail of old freelist */
1803 fsi
= sp
->header
.freelist
;
1805 sp
->header
.freelist
= n
;
1810 } while (fsi
!= -1);
1815 sp
->header
.freecnt
+= sp
->header
.maxslot
- n
;
1818 * insert the new entry
1820 dtInsertEntry(sp
, split
->index
, split
->key
, split
->data
, &dtlck
);
1822 BT_MARK_DIRTY(pmp
, ip
);
1824 * linelock any freeslots residing in old extent
1826 if (type
== tlckEXTEND
) {
1827 n
= sp
->header
.maxslot
>> 2;
1828 if (sp
->header
.freelist
< n
)
1829 dtLinelockFreelist(sp
, n
, &dtlck
);
1833 * update parent entry on the parent/root page
1836 * acquire a transaction lock on the parent/root page
1838 tlck
= txLock(tid
, ip
, pmp
, tlckDTREE
| tlckENTRY
);
1839 dtlck
= (struct dt_lock
*) & tlck
->lock
;
1840 lv
= & dtlck
->lv
[dtlck
->index
];
1842 /* linelock parent entry - 1st slot */
1847 /* update the parent pxd for page extension */
1848 tpxd
= (pxd_t
*) & pp
->slot
[1];
1860 * split the full root page into
1861 * original/root/split page and new right page
1862 * i.e., root remains fixed in tree anchor (inode) and
1863 * the root is copied to a single new right child page
1864 * since root page << non-root page, and
1865 * the split root page contains a single entry for the
1866 * new right child page.
1870 * return: 0 - success;
1872 * return new page pinned;
1874 static int dtSplitRoot(tid_t tid
,
1875 struct inode
*ip
, struct dtsplit
* split
, struct metapage
** rmpp
)
1877 struct super_block
*sb
= ip
->i_sb
;
1878 struct metapage
*smp
;
1880 struct metapage
*rmp
;
1887 int fsi
, stblsize
, n
;
1890 struct pxdlist
*pxdlist
;
1892 struct dt_lock
*dtlck
;
1897 /* get split root page */
1899 sp
= &JFS_IP(ip
)->i_dtroot
;
1902 * allocate/initialize a single (right) child page
1904 * N.B. at first split, a one (or two) block to fit new entry
1905 * is allocated; at subsequent split, a full page is allocated;
1907 pxdlist
= split
->pxdlist
;
1908 pxd
= &pxdlist
->pxd
[pxdlist
->npxd
];
1910 rbn
= addressPXD(pxd
);
1911 xlen
= lengthPXD(pxd
);
1912 xsize
= xlen
<< JFS_SBI(sb
)->l2bsize
;
1913 rmp
= get_metapage(ip
, rbn
, xsize
, 1);
1919 /* Allocate blocks to quota. */
1920 rc
= dquot_alloc_block(ip
, lengthPXD(pxd
));
1922 release_metapage(rmp
);
1926 BT_MARK_DIRTY(rmp
, ip
);
1928 * acquire a transaction lock on the new right page
1930 tlck
= txLock(tid
, ip
, rmp
, tlckDTREE
| tlckNEW
);
1931 dtlck
= (struct dt_lock
*) & tlck
->lock
;
1934 (sp
->header
.flag
& BT_LEAF
) ? BT_LEAF
: BT_INTERNAL
;
1935 rp
->header
.self
= *pxd
;
1937 /* initialize sibling pointers */
1938 rp
->header
.next
= 0;
1939 rp
->header
.prev
= 0;
1942 * move in-line root page into new right page extent
1944 /* linelock header + copied entries + new stbl (1st slot) in new page */
1945 ASSERT(dtlck
->index
== 0);
1946 lv
= & dtlck
->lv
[0];
1948 lv
->length
= 10; /* 1 + 8 + 1 */
1951 n
= xsize
>> L2DTSLOTSIZE
;
1952 rp
->header
.maxslot
= n
;
1953 stblsize
= (n
+ 31) >> L2DTSLOTSIZE
;
1955 /* copy old stbl to new stbl at start of extended area */
1956 rp
->header
.stblindex
= DTROOTMAXSLOT
;
1957 stbl
= (s8
*) & rp
->slot
[DTROOTMAXSLOT
];
1958 memcpy(stbl
, sp
->header
.stbl
, sp
->header
.nextindex
);
1959 rp
->header
.nextindex
= sp
->header
.nextindex
;
1961 /* copy old data area to start of new data area */
1962 memcpy(&rp
->slot
[1], &sp
->slot
[1], IDATASIZE
);
1965 * append free region of newly extended area at tail of freelist
1967 /* init free region of newly extended area */
1968 fsi
= n
= DTROOTMAXSLOT
+ stblsize
;
1970 for (fsi
++; fsi
< rp
->header
.maxslot
; f
++, fsi
++)
1974 /* append new free region at tail of old freelist */
1975 fsi
= sp
->header
.freelist
;
1977 rp
->header
.freelist
= n
;
1979 rp
->header
.freelist
= fsi
;
1984 } while (fsi
!= -1);
1989 rp
->header
.freecnt
= sp
->header
.freecnt
+ rp
->header
.maxslot
- n
;
1992 * Update directory index table for entries now in right page
1994 if ((rp
->header
.flag
& BT_LEAF
) && DO_INDEX(ip
)) {
1996 struct metapage
*mp
= NULL
;
1997 struct ldtentry
*ldtentry
;
1999 stbl
= DT_GETSTBL(rp
);
2000 for (n
= 0; n
< rp
->header
.nextindex
; n
++) {
2001 ldtentry
= (struct ldtentry
*) & rp
->slot
[stbl
[n
]];
2002 modify_index(tid
, ip
, le32_to_cpu(ldtentry
->index
),
2003 rbn
, n
, &mp
, &lblock
);
2006 release_metapage(mp
);
2009 * insert the new entry into the new right/child page
2010 * (skip index in the new right page will not change)
2012 dtInsertEntry(rp
, split
->index
, split
->key
, split
->data
, &dtlck
);
2015 * reset parent/root page
2017 * set the 1st entry offset to 0, which force the left-most key
2018 * at any level of the tree to be less than any search key.
2020 * The btree comparison code guarantees that the left-most key on any
2021 * level of the tree is never used, so it doesn't need to be filled in.
2023 BT_MARK_DIRTY(smp
, ip
);
2025 * acquire a transaction lock on the root page (in-memory inode)
2027 tlck
= txLock(tid
, ip
, smp
, tlckDTREE
| tlckNEW
| tlckBTROOT
);
2028 dtlck
= (struct dt_lock
*) & tlck
->lock
;
2031 ASSERT(dtlck
->index
== 0);
2032 lv
= & dtlck
->lv
[0];
2034 lv
->length
= DTROOTMAXSLOT
;
2037 /* update page header of root */
2038 if (sp
->header
.flag
& BT_LEAF
) {
2039 sp
->header
.flag
&= ~BT_LEAF
;
2040 sp
->header
.flag
|= BT_INTERNAL
;
2043 /* init the first entry */
2044 s
= (struct idtentry
*) & sp
->slot
[DTENTRYSTART
];
2050 stbl
= sp
->header
.stbl
;
2051 stbl
[0] = DTENTRYSTART
;
2052 sp
->header
.nextindex
= 1;
2055 fsi
= DTENTRYSTART
+ 1;
2058 /* init free region of remaining area */
2059 for (fsi
++; fsi
< DTROOTMAXSLOT
; f
++, fsi
++)
2063 sp
->header
.freelist
= DTENTRYSTART
+ 1;
2064 sp
->header
.freecnt
= DTROOTMAXSLOT
- (DTENTRYSTART
+ 1);
2075 * function: delete the entry(s) referenced by a key.
2081 int dtDelete(tid_t tid
,
2082 struct inode
*ip
, struct component_name
* key
, ino_t
* ino
, int flag
)
2086 struct metapage
*mp
, *imp
;
2089 struct btstack btstack
;
2090 struct dt_lock
*dtlck
;
2094 struct ldtentry
*ldtentry
;
2096 u32 table_index
, next_index
;
2097 struct metapage
*nmp
;
2101 * search for the entry to delete:
2103 * dtSearch() returns (leaf page pinned, index at which to delete).
2105 if ((rc
= dtSearch(ip
, key
, ino
, &btstack
, flag
)))
2108 /* retrieve search result */
2109 DT_GETSEARCH(ip
, btstack
.top
, bn
, mp
, p
, index
);
2112 * We need to find put the index of the next entry into the
2113 * directory index table in order to resume a readdir from this
2117 stbl
= DT_GETSTBL(p
);
2118 ldtentry
= (struct ldtentry
*) & p
->slot
[stbl
[index
]];
2119 table_index
= le32_to_cpu(ldtentry
->index
);
2120 if (index
== (p
->header
.nextindex
- 1)) {
2122 * Last entry in this leaf page
2124 if ((p
->header
.flag
& BT_ROOT
)
2125 || (p
->header
.next
== 0))
2128 /* Read next leaf page */
2129 DT_GETPAGE(ip
, le64_to_cpu(p
->header
.next
),
2130 nmp
, PSIZE
, np
, rc
);
2134 stbl
= DT_GETSTBL(np
);
2136 (struct ldtentry
*) & np
->
2139 le32_to_cpu(ldtentry
->index
);
2145 (struct ldtentry
*) & p
->slot
[stbl
[index
+ 1]];
2146 next_index
= le32_to_cpu(ldtentry
->index
);
2148 free_index(tid
, ip
, table_index
, next_index
);
2151 * the leaf page becomes empty, delete the page
2153 if (p
->header
.nextindex
== 1) {
2154 /* delete empty page */
2155 rc
= dtDeleteUp(tid
, ip
, mp
, p
, &btstack
);
2158 * the leaf page has other entries remaining:
2160 * delete the entry from the leaf page.
2163 BT_MARK_DIRTY(mp
, ip
);
2165 * acquire a transaction lock on the leaf page
2167 tlck
= txLock(tid
, ip
, mp
, tlckDTREE
| tlckENTRY
);
2168 dtlck
= (struct dt_lock
*) & tlck
->lock
;
2171 * Do not assume that dtlck->index will be zero. During a
2172 * rename within a directory, this transaction may have
2173 * modified this page already when adding the new entry.
2176 /* linelock header */
2177 if (dtlck
->index
>= dtlck
->maxcnt
)
2178 dtlck
= (struct dt_lock
*) txLinelock(dtlck
);
2179 lv
= & dtlck
->lv
[dtlck
->index
];
2184 /* linelock stbl of non-root leaf page */
2185 if (!(p
->header
.flag
& BT_ROOT
)) {
2186 if (dtlck
->index
>= dtlck
->maxcnt
)
2187 dtlck
= (struct dt_lock
*) txLinelock(dtlck
);
2188 lv
= & dtlck
->lv
[dtlck
->index
];
2189 i
= index
>> L2DTSLOTSIZE
;
2190 lv
->offset
= p
->header
.stblindex
+ i
;
2192 ((p
->header
.nextindex
- 1) >> L2DTSLOTSIZE
) -
2197 /* free the leaf entry */
2198 dtDeleteEntry(p
, index
, &dtlck
);
2201 * Update directory index table for entries moved in stbl
2203 if (DO_INDEX(ip
) && index
< p
->header
.nextindex
) {
2207 stbl
= DT_GETSTBL(p
);
2208 for (i
= index
; i
< p
->header
.nextindex
; i
++) {
2210 (struct ldtentry
*) & p
->slot
[stbl
[i
]];
2211 modify_index(tid
, ip
,
2212 le32_to_cpu(ldtentry
->index
),
2213 bn
, i
, &imp
, &lblock
);
2216 release_metapage(imp
);
2230 * free empty pages as propagating deletion up the tree
2236 static int dtDeleteUp(tid_t tid
, struct inode
*ip
,
2237 struct metapage
* fmp
, dtpage_t
* fp
, struct btstack
* btstack
)
2240 struct metapage
*mp
;
2242 int index
, nextindex
;
2244 struct btframe
*parent
;
2245 struct dt_lock
*dtlck
;
2248 struct pxd_lock
*pxdlock
;
2252 * keep the root leaf page which has become empty
2254 if (BT_IS_ROOT(fmp
)) {
2258 * dtInitRoot() acquires txlock on the root
2260 dtInitRoot(tid
, ip
, PARENT(ip
));
2268 * free the non-root leaf page
2271 * acquire a transaction lock on the page
2273 * write FREEXTENT|NOREDOPAGE log record
2274 * N.B. linelock is overlaid as freed extent descriptor, and
2275 * the buffer page is freed;
2277 tlck
= txMaplock(tid
, ip
, tlckDTREE
| tlckFREE
);
2278 pxdlock
= (struct pxd_lock
*) & tlck
->lock
;
2279 pxdlock
->flag
= mlckFREEPXD
;
2280 pxdlock
->pxd
= fp
->header
.self
;
2283 /* update sibling pointers */
2284 if ((rc
= dtRelink(tid
, ip
, fp
))) {
2289 xlen
= lengthPXD(&fp
->header
.self
);
2291 /* Free quota allocation. */
2292 dquot_free_block(ip
, xlen
);
2294 /* free/invalidate its buffer page */
2295 discard_metapage(fmp
);
2298 * propagate page deletion up the directory tree
2300 * If the delete from the parent page makes it empty,
2301 * continue all the way up the tree.
2302 * stop if the root page is reached (which is never deleted) or
2303 * if the entry deletion does not empty the page.
2305 while ((parent
= BT_POP(btstack
)) != NULL
) {
2306 /* pin the parent page <sp> */
2307 DT_GETPAGE(ip
, parent
->bn
, mp
, PSIZE
, p
, rc
);
2312 * free the extent of the child page deleted
2314 index
= parent
->index
;
2317 * delete the entry for the child page from parent
2319 nextindex
= p
->header
.nextindex
;
2322 * the parent has the single entry being deleted:
2324 * free the parent page which has become empty.
2326 if (nextindex
== 1) {
2328 * keep the root internal page which has become empty
2330 if (p
->header
.flag
& BT_ROOT
) {
2334 * dtInitRoot() acquires txlock on the root
2336 dtInitRoot(tid
, ip
, PARENT(ip
));
2343 * free the parent page
2347 * acquire a transaction lock on the page
2349 * write FREEXTENT|NOREDOPAGE log record
2353 tlckDTREE
| tlckFREE
);
2354 pxdlock
= (struct pxd_lock
*) & tlck
->lock
;
2355 pxdlock
->flag
= mlckFREEPXD
;
2356 pxdlock
->pxd
= p
->header
.self
;
2359 /* update sibling pointers */
2360 if ((rc
= dtRelink(tid
, ip
, p
))) {
2365 xlen
= lengthPXD(&p
->header
.self
);
2367 /* Free quota allocation */
2368 dquot_free_block(ip
, xlen
);
2370 /* free/invalidate its buffer page */
2371 discard_metapage(mp
);
2379 * the parent has other entries remaining:
2381 * delete the router entry from the parent page.
2383 BT_MARK_DIRTY(mp
, ip
);
2385 * acquire a transaction lock on the page
2387 * action: router entry deletion
2389 tlck
= txLock(tid
, ip
, mp
, tlckDTREE
| tlckENTRY
);
2390 dtlck
= (struct dt_lock
*) & tlck
->lock
;
2392 /* linelock header */
2393 if (dtlck
->index
>= dtlck
->maxcnt
)
2394 dtlck
= (struct dt_lock
*) txLinelock(dtlck
);
2395 lv
= & dtlck
->lv
[dtlck
->index
];
2400 /* linelock stbl of non-root leaf page */
2401 if (!(p
->header
.flag
& BT_ROOT
)) {
2402 if (dtlck
->index
< dtlck
->maxcnt
)
2405 dtlck
= (struct dt_lock
*) txLinelock(dtlck
);
2406 lv
= & dtlck
->lv
[0];
2408 i
= index
>> L2DTSLOTSIZE
;
2409 lv
->offset
= p
->header
.stblindex
+ i
;
2411 ((p
->header
.nextindex
- 1) >> L2DTSLOTSIZE
) -
2416 /* free the router entry */
2417 dtDeleteEntry(p
, index
, &dtlck
);
2419 /* reset key of new leftmost entry of level (for consistency) */
2421 ((p
->header
.flag
& BT_ROOT
) || p
->header
.prev
== 0))
2422 dtTruncateEntry(p
, 0, &dtlck
);
2424 /* unpin the parent page */
2427 /* exit propagation up */
2432 ip
->i_size
-= PSIZE
;
2439 * NAME: dtRelocate()
2441 * FUNCTION: relocate dtpage (internal or leaf) of directory;
2442 * This function is mainly used by defragfs utility.
2444 int dtRelocate(tid_t tid
, struct inode
*ip
, s64 lmxaddr
, pxd_t
* opxd
,
2448 struct metapage
*mp
, *pmp
, *lmp
, *rmp
;
2449 dtpage_t
*p
, *pp
, *rp
= 0, *lp
= 0;
2452 struct btstack btstack
;
2454 s64 oxaddr
, nextbn
, prevbn
;
2457 struct dt_lock
*dtlck
;
2458 struct pxd_lock
*pxdlock
;
2462 oxaddr
= addressPXD(opxd
);
2463 xlen
= lengthPXD(opxd
);
2465 jfs_info("dtRelocate: lmxaddr:%Ld xaddr:%Ld:%Ld xlen:%d",
2466 (long long)lmxaddr
, (long long)oxaddr
, (long long)nxaddr
,
2470 * 1. get the internal parent dtpage covering
2471 * router entry for the tartget page to be relocated;
2473 rc
= dtSearchNode(ip
, lmxaddr
, opxd
, &btstack
);
2477 /* retrieve search result */
2478 DT_GETSEARCH(ip
, btstack
.top
, bn
, pmp
, pp
, index
);
2479 jfs_info("dtRelocate: parent router entry validated.");
2482 * 2. relocate the target dtpage
2484 /* read in the target page from src extent */
2485 DT_GETPAGE(ip
, oxaddr
, mp
, PSIZE
, p
, rc
);
2487 /* release the pinned parent page */
2493 * read in sibling pages if any to update sibling pointers;
2496 if (p
->header
.next
) {
2497 nextbn
= le64_to_cpu(p
->header
.next
);
2498 DT_GETPAGE(ip
, nextbn
, rmp
, PSIZE
, rp
, rc
);
2507 if (p
->header
.prev
) {
2508 prevbn
= le64_to_cpu(p
->header
.prev
);
2509 DT_GETPAGE(ip
, prevbn
, lmp
, PSIZE
, lp
, rc
);
2519 /* at this point, all xtpages to be updated are in memory */
2522 * update sibling pointers of sibling dtpages if any;
2525 tlck
= txLock(tid
, ip
, lmp
, tlckDTREE
| tlckRELINK
);
2526 dtlck
= (struct dt_lock
*) & tlck
->lock
;
2527 /* linelock header */
2528 ASSERT(dtlck
->index
== 0);
2529 lv
= & dtlck
->lv
[0];
2534 lp
->header
.next
= cpu_to_le64(nxaddr
);
2539 tlck
= txLock(tid
, ip
, rmp
, tlckDTREE
| tlckRELINK
);
2540 dtlck
= (struct dt_lock
*) & tlck
->lock
;
2541 /* linelock header */
2542 ASSERT(dtlck
->index
== 0);
2543 lv
= & dtlck
->lv
[0];
2548 rp
->header
.prev
= cpu_to_le64(nxaddr
);
2553 * update the target dtpage to be relocated
2555 * write LOG_REDOPAGE of LOG_NEW type for dst page
2556 * for the whole target page (logredo() will apply
2557 * after image and update bmap for allocation of the
2558 * dst extent), and update bmap for allocation of
2561 tlck
= txLock(tid
, ip
, mp
, tlckDTREE
| tlckNEW
);
2562 dtlck
= (struct dt_lock
*) & tlck
->lock
;
2563 /* linelock header */
2564 ASSERT(dtlck
->index
== 0);
2565 lv
= & dtlck
->lv
[0];
2567 /* update the self address in the dtpage header */
2568 pxd
= &p
->header
.self
;
2569 PXDaddress(pxd
, nxaddr
);
2571 /* the dst page is the same as the src page, i.e.,
2572 * linelock for afterimage of the whole page;
2575 lv
->length
= p
->header
.maxslot
;
2578 /* update the buffer extent descriptor of the dtpage */
2579 xsize
= xlen
<< JFS_SBI(ip
->i_sb
)->l2bsize
;
2581 /* unpin the relocated page */
2583 jfs_info("dtRelocate: target dtpage relocated.");
2585 /* the moved extent is dtpage, then a LOG_NOREDOPAGE log rec
2586 * needs to be written (in logredo(), the LOG_NOREDOPAGE log rec
2587 * will also force a bmap update ).
2591 * 3. acquire maplock for the source extent to be freed;
2593 /* for dtpage relocation, write a LOG_NOREDOPAGE record
2594 * for the source dtpage (logredo() will init NoRedoPage
2595 * filter and will also update bmap for free of the source
2596 * dtpage), and upadte bmap for free of the source dtpage;
2598 tlck
= txMaplock(tid
, ip
, tlckDTREE
| tlckFREE
);
2599 pxdlock
= (struct pxd_lock
*) & tlck
->lock
;
2600 pxdlock
->flag
= mlckFREEPXD
;
2601 PXDaddress(&pxdlock
->pxd
, oxaddr
);
2602 PXDlength(&pxdlock
->pxd
, xlen
);
2606 * 4. update the parent router entry for relocation;
2608 * acquire tlck for the parent entry covering the target dtpage;
2609 * write LOG_REDOPAGE to apply after image only;
2611 jfs_info("dtRelocate: update parent router entry.");
2612 tlck
= txLock(tid
, ip
, pmp
, tlckDTREE
| tlckENTRY
);
2613 dtlck
= (struct dt_lock
*) & tlck
->lock
;
2614 lv
= & dtlck
->lv
[dtlck
->index
];
2616 /* update the PXD with the new address */
2617 stbl
= DT_GETSTBL(pp
);
2618 pxd
= (pxd_t
*) & pp
->slot
[stbl
[index
]];
2619 PXDaddress(pxd
, nxaddr
);
2620 lv
->offset
= stbl
[index
];
2624 /* unpin the parent dtpage */
2631 * NAME: dtSearchNode()
2633 * FUNCTION: Search for an dtpage containing a specified address
2634 * This function is mainly used by defragfs utility.
2636 * NOTE: Search result on stack, the found page is pinned at exit.
2637 * The result page must be an internal dtpage.
2638 * lmxaddr give the address of the left most page of the
2639 * dtree level, in which the required dtpage resides.
2641 static int dtSearchNode(struct inode
*ip
, s64 lmxaddr
, pxd_t
* kpxd
,
2642 struct btstack
* btstack
)
2646 struct metapage
*mp
;
2648 int psize
= 288; /* initial in-line directory */
2652 struct btframe
*btsp
;
2654 BT_CLR(btstack
); /* reset stack */
2657 * descend tree to the level with specified leftmost page
2659 * by convention, root bn = 0.
2662 /* get/pin the page to search */
2663 DT_GETPAGE(ip
, bn
, mp
, psize
, p
, rc
);
2667 /* does the xaddr of leftmost page of the levevl
2668 * matches levevl search key ?
2670 if (p
->header
.flag
& BT_ROOT
) {
2673 } else if (addressPXD(&p
->header
.self
) == lmxaddr
)
2677 * descend down to leftmost child page
2679 if (p
->header
.flag
& BT_LEAF
) {
2684 /* get the leftmost entry */
2685 stbl
= DT_GETSTBL(p
);
2686 pxd
= (pxd_t
*) & p
->slot
[stbl
[0]];
2688 /* get the child page block address */
2689 bn
= addressPXD(pxd
);
2690 psize
= lengthPXD(pxd
) << JFS_SBI(ip
->i_sb
)->l2bsize
;
2691 /* unpin the parent page */
2696 * search each page at the current levevl
2699 stbl
= DT_GETSTBL(p
);
2700 for (i
= 0; i
< p
->header
.nextindex
; i
++) {
2701 pxd
= (pxd_t
*) & p
->slot
[stbl
[i
]];
2703 /* found the specified router entry */
2704 if (addressPXD(pxd
) == addressPXD(kpxd
) &&
2705 lengthPXD(pxd
) == lengthPXD(kpxd
)) {
2706 btsp
= btstack
->top
;
2715 /* get the right sibling page if any */
2717 bn
= le64_to_cpu(p
->header
.next
);
2723 /* unpin current page */
2726 /* get the right sibling page */
2727 DT_GETPAGE(ip
, bn
, mp
, PSIZE
, p
, rc
);
2733 #endif /* _NOTYET */
2739 * link around a freed page.
2742 * fp: page to be freed
2746 static int dtRelink(tid_t tid
, struct inode
*ip
, dtpage_t
* p
)
2749 struct metapage
*mp
;
2752 struct dt_lock
*dtlck
;
2755 nextbn
= le64_to_cpu(p
->header
.next
);
2756 prevbn
= le64_to_cpu(p
->header
.prev
);
2758 /* update prev pointer of the next page */
2760 DT_GETPAGE(ip
, nextbn
, mp
, PSIZE
, p
, rc
);
2764 BT_MARK_DIRTY(mp
, ip
);
2766 * acquire a transaction lock on the next page
2768 * action: update prev pointer;
2770 tlck
= txLock(tid
, ip
, mp
, tlckDTREE
| tlckRELINK
);
2771 jfs_info("dtRelink nextbn: tlck = 0x%p, ip = 0x%p, mp=0x%p",
2773 dtlck
= (struct dt_lock
*) & tlck
->lock
;
2775 /* linelock header */
2776 if (dtlck
->index
>= dtlck
->maxcnt
)
2777 dtlck
= (struct dt_lock
*) txLinelock(dtlck
);
2778 lv
= & dtlck
->lv
[dtlck
->index
];
2783 p
->header
.prev
= cpu_to_le64(prevbn
);
2787 /* update next pointer of the previous page */
2789 DT_GETPAGE(ip
, prevbn
, mp
, PSIZE
, p
, rc
);
2793 BT_MARK_DIRTY(mp
, ip
);
2795 * acquire a transaction lock on the prev page
2797 * action: update next pointer;
2799 tlck
= txLock(tid
, ip
, mp
, tlckDTREE
| tlckRELINK
);
2800 jfs_info("dtRelink prevbn: tlck = 0x%p, ip = 0x%p, mp=0x%p",
2802 dtlck
= (struct dt_lock
*) & tlck
->lock
;
2804 /* linelock header */
2805 if (dtlck
->index
>= dtlck
->maxcnt
)
2806 dtlck
= (struct dt_lock
*) txLinelock(dtlck
);
2807 lv
= & dtlck
->lv
[dtlck
->index
];
2812 p
->header
.next
= cpu_to_le64(nextbn
);
2823 * initialize directory root (inline in inode)
2825 void dtInitRoot(tid_t tid
, struct inode
*ip
, u32 idotdot
)
2827 struct jfs_inode_info
*jfs_ip
= JFS_IP(ip
);
2832 struct dt_lock
*dtlck
;
2837 * If this was previously an non-empty directory, we need to remove
2838 * the old directory table.
2841 if (!jfs_dirtable_inline(ip
)) {
2842 struct tblock
*tblk
= tid_to_tblock(tid
);
2844 * We're playing games with the tid's xflag. If
2845 * we're removing a regular file, the file's xtree
2846 * is committed with COMMIT_PMAP, but we always
2847 * commit the directories xtree with COMMIT_PWMAP.
2849 xflag_save
= tblk
->xflag
;
2852 * xtTruncate isn't guaranteed to fully truncate
2853 * the xtree. The caller needs to check i_size
2854 * after committing the transaction to see if
2855 * additional truncation is needed. The
2856 * COMMIT_Stale flag tells caller that we
2857 * initiated the truncation.
2859 xtTruncate(tid
, ip
, 0, COMMIT_PWMAP
);
2860 set_cflag(COMMIT_Stale
, ip
);
2862 tblk
->xflag
= xflag_save
;
2866 jfs_ip
->next_index
= 2;
2868 ip
->i_size
= IDATASIZE
;
2871 * acquire a transaction lock on the root
2873 * action: directory initialization;
2875 tlck
= txLock(tid
, ip
, (struct metapage
*) & jfs_ip
->bxflag
,
2876 tlckDTREE
| tlckENTRY
| tlckBTROOT
);
2877 dtlck
= (struct dt_lock
*) & tlck
->lock
;
2880 ASSERT(dtlck
->index
== 0);
2881 lv
= & dtlck
->lv
[0];
2883 lv
->length
= DTROOTMAXSLOT
;
2886 p
= &jfs_ip
->i_dtroot
;
2888 p
->header
.flag
= DXD_INDEX
| BT_ROOT
| BT_LEAF
;
2890 p
->header
.nextindex
= 0;
2896 /* init data area of root */
2897 for (fsi
++; fsi
< DTROOTMAXSLOT
; f
++, fsi
++)
2901 p
->header
.freelist
= 1;
2902 p
->header
.freecnt
= 8;
2904 /* init '..' entry */
2905 p
->header
.idotdot
= cpu_to_le32(idotdot
);
2911 * add_missing_indices()
2913 * function: Fix dtree page in which one or more entries has an invalid index.
2914 * fsck.jfs should really fix this, but it currently does not.
2915 * Called from jfs_readdir when bad index is detected.
2917 static void add_missing_indices(struct inode
*inode
, s64 bn
)
2920 struct dt_lock
*dtlck
;
2924 struct metapage
*mp
;
2931 tid
= txBegin(inode
->i_sb
, 0);
2933 DT_GETPAGE(inode
, bn
, mp
, PSIZE
, p
, rc
);
2936 printk(KERN_ERR
"DT_GETPAGE failed!\n");
2939 BT_MARK_DIRTY(mp
, inode
);
2941 ASSERT(p
->header
.flag
& BT_LEAF
);
2943 tlck
= txLock(tid
, inode
, mp
, tlckDTREE
| tlckENTRY
);
2945 tlck
->type
|= tlckBTROOT
;
2947 dtlck
= (struct dt_lock
*) &tlck
->lock
;
2949 stbl
= DT_GETSTBL(p
);
2950 for (i
= 0; i
< p
->header
.nextindex
; i
++) {
2951 d
= (struct ldtentry
*) &p
->slot
[stbl
[i
]];
2952 index
= le32_to_cpu(d
->index
);
2953 if ((index
< 2) || (index
>= JFS_IP(inode
)->next_index
)) {
2954 d
->index
= cpu_to_le32(add_index(tid
, inode
, bn
, i
));
2955 if (dtlck
->index
>= dtlck
->maxcnt
)
2956 dtlck
= (struct dt_lock
*) txLinelock(dtlck
);
2957 lv
= &dtlck
->lv
[dtlck
->index
];
2958 lv
->offset
= stbl
[i
];
2965 (void) txCommit(tid
, 1, &inode
, 0);
2971 * Buffer to hold directory entry info while traversing a dtree page
2972 * before being fed to the filldir function
2982 * function to determine next variable-sized jfs_dirent in buffer
2984 static inline struct jfs_dirent
*next_jfs_dirent(struct jfs_dirent
*dirent
)
2986 return (struct jfs_dirent
*)
2988 ((sizeof (struct jfs_dirent
) + dirent
->name_len
+ 1 +
2989 sizeof (loff_t
) - 1) &
2990 ~(sizeof (loff_t
) - 1)));
2996 * function: read directory entries sequentially
2997 * from the specified entry offset
3001 * return: offset = (pn, index) of start entry
3002 * of next jfs_readdir()/dtRead()
3004 int jfs_readdir(struct file
*filp
, void *dirent
, filldir_t filldir
)
3006 struct inode
*ip
= filp
->f_path
.dentry
->d_inode
;
3007 struct nls_table
*codepage
= JFS_SBI(ip
->i_sb
)->nls_tab
;
3009 loff_t dtpos
; /* legacy OS/2 style position */
3014 } *dtoffset
= (struct dtoffset
*) &dtpos
;
3016 struct metapage
*mp
;
3020 struct btstack btstack
;
3024 int d_namleft
, len
, outlen
;
3025 unsigned long dirent_buf
;
3029 uint loop_count
= 0;
3030 struct jfs_dirent
*jfs_dirent
;
3032 int overflow
, fix_page
, page_fixed
= 0;
3033 static int unique_pos
= 2; /* If we can't fix broken index */
3035 if (filp
->f_pos
== DIREND
)
3040 * persistent index is stored in directory entries.
3041 * Special cases: 0 = .
3043 * -1 = End of directory
3047 dir_index
= (u32
) filp
->f_pos
;
3049 if (dir_index
> 1) {
3050 struct dir_table_slot dirtab_slot
;
3053 (dir_index
>= JFS_IP(ip
)->next_index
)) {
3054 /* Stale position. Directory has shrunk */
3055 filp
->f_pos
= DIREND
;
3059 rc
= read_index(ip
, dir_index
, &dirtab_slot
);
3061 filp
->f_pos
= DIREND
;
3064 if (dirtab_slot
.flag
== DIR_INDEX_FREE
) {
3065 if (loop_count
++ > JFS_IP(ip
)->next_index
) {
3066 jfs_err("jfs_readdir detected "
3068 filp
->f_pos
= DIREND
;
3071 dir_index
= le32_to_cpu(dirtab_slot
.addr2
);
3072 if (dir_index
== -1) {
3073 filp
->f_pos
= DIREND
;
3078 bn
= addressDTS(&dirtab_slot
);
3079 index
= dirtab_slot
.slot
;
3080 DT_GETPAGE(ip
, bn
, mp
, PSIZE
, p
, rc
);
3082 filp
->f_pos
= DIREND
;
3085 if (p
->header
.flag
& BT_INTERNAL
) {
3086 jfs_err("jfs_readdir: bad index table");
3092 if (dir_index
== 0) {
3097 if (filldir(dirent
, ".", 1, 0, ip
->i_ino
,
3105 if (filldir(dirent
, "..", 2, 1, PARENT(ip
), DT_DIR
))
3109 * Find first entry of left-most leaf
3112 filp
->f_pos
= DIREND
;
3116 if ((rc
= dtReadFirst(ip
, &btstack
)))
3119 DT_GETSEARCH(ip
, btstack
.top
, bn
, mp
, p
, index
);
3123 * Legacy filesystem - OS/2 & Linux JFS < 0.3.6
3125 * pn = index = 0: First entry "."
3126 * pn = 0; index = 1: Second entry ".."
3127 * pn > 0: Real entries, pn=1 -> leftmost page
3128 * pn = index = -1: No more entries
3130 dtpos
= filp
->f_pos
;
3132 /* build "." entry */
3134 if (filldir(dirent
, ".", 1, filp
->f_pos
, ip
->i_ino
,
3137 dtoffset
->index
= 1;
3138 filp
->f_pos
= dtpos
;
3141 if (dtoffset
->pn
== 0) {
3142 if (dtoffset
->index
== 1) {
3143 /* build ".." entry */
3145 if (filldir(dirent
, "..", 2, filp
->f_pos
,
3146 PARENT(ip
), DT_DIR
))
3149 jfs_err("jfs_readdir called with "
3153 dtoffset
->index
= 0;
3154 filp
->f_pos
= dtpos
;
3158 filp
->f_pos
= DIREND
;
3162 if ((rc
= dtReadNext(ip
, &filp
->f_pos
, &btstack
))) {
3163 jfs_err("jfs_readdir: unexpected rc = %d "
3164 "from dtReadNext", rc
);
3165 filp
->f_pos
= DIREND
;
3168 /* get start leaf page and index */
3169 DT_GETSEARCH(ip
, btstack
.top
, bn
, mp
, p
, index
);
3171 /* offset beyond directory eof ? */
3173 filp
->f_pos
= DIREND
;
3178 dirent_buf
= __get_free_page(GFP_KERNEL
);
3179 if (dirent_buf
== 0) {
3181 jfs_warn("jfs_readdir: __get_free_page failed!");
3182 filp
->f_pos
= DIREND
;
3187 jfs_dirent
= (struct jfs_dirent
*) dirent_buf
;
3189 overflow
= fix_page
= 0;
3191 stbl
= DT_GETSTBL(p
);
3193 for (i
= index
; i
< p
->header
.nextindex
; i
++) {
3194 d
= (struct ldtentry
*) & p
->slot
[stbl
[i
]];
3196 if (((long) jfs_dirent
+ d
->namlen
+ 1) >
3197 (dirent_buf
+ PAGE_SIZE
)) {
3198 /* DBCS codepages could overrun dirent_buf */
3204 d_namleft
= d
->namlen
;
3205 name_ptr
= jfs_dirent
->name
;
3206 jfs_dirent
->ino
= le32_to_cpu(d
->inumber
);
3209 len
= min(d_namleft
, DTLHDRDATALEN
);
3210 jfs_dirent
->position
= le32_to_cpu(d
->index
);
3212 * d->index should always be valid, but it
3213 * isn't. fsck.jfs doesn't create the
3214 * directory index for the lost+found
3215 * directory. Rather than let it go,
3216 * we can try to fix it.
3218 if ((jfs_dirent
->position
< 2) ||
3219 (jfs_dirent
->position
>=
3220 JFS_IP(ip
)->next_index
)) {
3221 if (!page_fixed
&& !isReadOnly(ip
)) {
3224 * setting overflow and setting
3225 * index to i will cause the
3226 * same page to be processed
3227 * again starting here
3233 jfs_dirent
->position
= unique_pos
++;
3236 jfs_dirent
->position
= dtpos
;
3237 len
= min(d_namleft
, DTLHDRDATALEN_LEGACY
);
3240 /* copy the name of head/only segment */
3241 outlen
= jfs_strfromUCS_le(name_ptr
, d
->name
, len
,
3243 jfs_dirent
->name_len
= outlen
;
3245 /* copy name in the additional segment(s) */
3248 t
= (struct dtslot
*) & p
->slot
[next
];
3252 if (d_namleft
== 0) {
3254 "JFS:Dtree error: ino = "
3255 "%ld, bn=%Ld, index = %d",
3261 len
= min(d_namleft
, DTSLOTDATALEN
);
3262 outlen
= jfs_strfromUCS_le(name_ptr
, t
->name
,
3264 jfs_dirent
->name_len
+= outlen
;
3270 jfs_dirent
= next_jfs_dirent(jfs_dirent
);
3277 /* Point to next leaf page */
3278 if (p
->header
.flag
& BT_ROOT
)
3281 bn
= le64_to_cpu(p
->header
.next
);
3283 /* update offset (pn:index) for new page */
3286 dtoffset
->index
= 0;
3292 /* unpin previous leaf page */
3295 jfs_dirent
= (struct jfs_dirent
*) dirent_buf
;
3296 while (jfs_dirents
--) {
3297 filp
->f_pos
= jfs_dirent
->position
;
3298 if (filldir(dirent
, jfs_dirent
->name
,
3299 jfs_dirent
->name_len
, filp
->f_pos
,
3300 jfs_dirent
->ino
, DT_UNKNOWN
))
3302 jfs_dirent
= next_jfs_dirent(jfs_dirent
);
3306 add_missing_indices(ip
, bn
);
3310 if (!overflow
&& (bn
== 0)) {
3311 filp
->f_pos
= DIREND
;
3315 DT_GETPAGE(ip
, bn
, mp
, PSIZE
, p
, rc
);
3317 free_page(dirent_buf
);
3323 free_page(dirent_buf
);
3332 * function: get the leftmost page of the directory
3334 static int dtReadFirst(struct inode
*ip
, struct btstack
* btstack
)
3338 int psize
= 288; /* initial in-line directory */
3339 struct metapage
*mp
;
3342 struct btframe
*btsp
;
3345 BT_CLR(btstack
); /* reset stack */
3348 * descend leftmost path of the tree
3350 * by convention, root bn = 0.
3353 DT_GETPAGE(ip
, bn
, mp
, psize
, p
, rc
);
3358 * leftmost leaf page
3360 if (p
->header
.flag
& BT_LEAF
) {
3361 /* return leftmost entry */
3362 btsp
= btstack
->top
;
3371 * descend down to leftmost child page
3373 if (BT_STACK_FULL(btstack
)) {
3375 jfs_error(ip
->i_sb
, "dtReadFirst: btstack overrun");
3376 BT_STACK_DUMP(btstack
);
3379 /* push (bn, index) of the parent page/entry */
3380 BT_PUSH(btstack
, bn
, 0);
3382 /* get the leftmost entry */
3383 stbl
= DT_GETSTBL(p
);
3384 xd
= (pxd_t
*) & p
->slot
[stbl
[0]];
3386 /* get the child page block address */
3387 bn
= addressPXD(xd
);
3388 psize
= lengthPXD(xd
) << JFS_SBI(ip
->i_sb
)->l2bsize
;
3390 /* unpin the parent page */
3399 * function: get the page of the specified offset (pn:index)
3401 * return: if (offset > eof), bn = -1;
3403 * note: if index > nextindex of the target leaf page,
3404 * start with 1st entry of next leaf page;
3406 static int dtReadNext(struct inode
*ip
, loff_t
* offset
,
3407 struct btstack
* btstack
)
3414 } *dtoffset
= (struct dtoffset
*) offset
;
3416 struct metapage
*mp
;
3421 struct btframe
*btsp
, *parent
;
3425 * get leftmost leaf page pinned
3427 if ((rc
= dtReadFirst(ip
, btstack
)))
3431 DT_GETSEARCH(ip
, btstack
->top
, bn
, mp
, p
, index
);
3433 /* get the start offset (pn:index) */
3434 pn
= dtoffset
->pn
- 1; /* Now pn = 0 represents leftmost leaf */
3435 index
= dtoffset
->index
;
3437 /* start at leftmost page ? */
3439 /* offset beyond eof ? */
3440 if (index
< p
->header
.nextindex
)
3443 if (p
->header
.flag
& BT_ROOT
) {
3448 /* start with 1st entry of next leaf page */
3450 dtoffset
->index
= index
= 0;
3454 /* start at non-leftmost page: scan parent pages for large pn */
3455 if (p
->header
.flag
& BT_ROOT
) {
3460 /* start after next leaf page ? */
3464 /* get leaf page pn = 1 */
3466 bn
= le64_to_cpu(p
->header
.next
);
3468 /* unpin leaf page */
3471 /* offset beyond eof ? */
3480 * scan last internal page level to get target leaf page
3483 /* unpin leftmost leaf page */
3486 /* get left most parent page */
3487 btsp
= btstack
->top
;
3490 DT_GETPAGE(ip
, bn
, mp
, PSIZE
, p
, rc
);
3494 /* scan parent pages at last internal page level */
3495 while (pn
>= p
->header
.nextindex
) {
3496 pn
-= p
->header
.nextindex
;
3498 /* get next parent page address */
3499 bn
= le64_to_cpu(p
->header
.next
);
3501 /* unpin current parent page */
3504 /* offset beyond eof ? */
3510 /* get next parent page */
3511 DT_GETPAGE(ip
, bn
, mp
, PSIZE
, p
, rc
);
3515 /* update parent page stack frame */
3519 /* get leaf page address */
3520 stbl
= DT_GETSTBL(p
);
3521 xd
= (pxd_t
*) & p
->slot
[stbl
[pn
]];
3522 bn
= addressPXD(xd
);
3524 /* unpin parent page */
3528 * get target leaf page
3531 DT_GETPAGE(ip
, bn
, mp
, PSIZE
, p
, rc
);
3536 * leaf page has been completed:
3537 * start with 1st entry of next leaf page
3539 if (index
>= p
->header
.nextindex
) {
3540 bn
= le64_to_cpu(p
->header
.next
);
3542 /* unpin leaf page */
3545 /* offset beyond eof ? */
3551 /* get next leaf page */
3552 DT_GETPAGE(ip
, bn
, mp
, PSIZE
, p
, rc
);
3556 /* start with 1st entry of next leaf page */
3558 dtoffset
->index
= 0;
3562 /* return target leaf page pinned */
3563 btsp
= btstack
->top
;
3565 btsp
->index
= dtoffset
->index
;
3575 * function: compare search key with an internal entry
3578 * < 0 if k is < record
3579 * = 0 if k is = record
3580 * > 0 if k is > record
3582 static int dtCompare(struct component_name
* key
, /* search key */
3583 dtpage_t
* p
, /* directory page */
3585 { /* entry slot index */
3588 int klen
, namlen
, len
, rc
;
3589 struct idtentry
*ih
;
3593 * force the left-most key on internal pages, at any level of
3594 * the tree, to be less than any search key.
3595 * this obviates having to update the leftmost key on an internal
3596 * page when the user inserts a new key in the tree smaller than
3597 * anything that has been stored.
3599 * (? if/when dtSearch() narrows down to 1st entry (index = 0),
3600 * at any internal page at any level of the tree,
3601 * it descends to child of the entry anyway -
3602 * ? make the entry as min size dummy entry)
3604 * if (e->index == 0 && h->prevpg == P_INVALID && !(h->flags & BT_LEAF))
3611 ih
= (struct idtentry
*) & p
->slot
[si
];
3614 namlen
= ih
->namlen
;
3615 len
= min(namlen
, DTIHDRDATALEN
);
3617 /* compare with head/only segment */
3618 len
= min(klen
, len
);
3619 if ((rc
= UniStrncmp_le(kname
, name
, len
)))
3625 /* compare with additional segment(s) */
3627 while (klen
> 0 && namlen
> 0) {
3628 /* compare with next name segment */
3629 t
= (struct dtslot
*) & p
->slot
[si
];
3630 len
= min(namlen
, DTSLOTDATALEN
);
3631 len
= min(klen
, len
);
3633 if ((rc
= UniStrncmp_le(kname
, name
, len
)))
3642 return (klen
- namlen
);
3651 * function: compare search key with an (leaf/internal) entry
3654 * < 0 if k is < record
3655 * = 0 if k is = record
3656 * > 0 if k is > record
3658 static int ciCompare(struct component_name
* key
, /* search key */
3659 dtpage_t
* p
, /* directory page */
3660 int si
, /* entry slot index */
3665 int klen
, namlen
, len
, rc
;
3666 struct ldtentry
*lh
;
3667 struct idtentry
*ih
;
3672 * force the left-most key on internal pages, at any level of
3673 * the tree, to be less than any search key.
3674 * this obviates having to update the leftmost key on an internal
3675 * page when the user inserts a new key in the tree smaller than
3676 * anything that has been stored.
3678 * (? if/when dtSearch() narrows down to 1st entry (index = 0),
3679 * at any internal page at any level of the tree,
3680 * it descends to child of the entry anyway -
3681 * ? make the entry as min size dummy entry)
3683 * if (e->index == 0 && h->prevpg == P_INVALID && !(h->flags & BT_LEAF))
3693 if (p
->header
.flag
& BT_LEAF
) {
3694 lh
= (struct ldtentry
*) & p
->slot
[si
];
3697 namlen
= lh
->namlen
;
3698 if (flag
& JFS_DIR_INDEX
)
3699 len
= min(namlen
, DTLHDRDATALEN
);
3701 len
= min(namlen
, DTLHDRDATALEN_LEGACY
);
3704 * internal page entry
3707 ih
= (struct idtentry
*) & p
->slot
[si
];
3710 namlen
= ih
->namlen
;
3711 len
= min(namlen
, DTIHDRDATALEN
);
3714 /* compare with head/only segment */
3715 len
= min(klen
, len
);
3716 for (i
= 0; i
< len
; i
++, kname
++, name
++) {
3717 /* only uppercase if case-insensitive support is on */
3718 if ((flag
& JFS_OS2
) == JFS_OS2
)
3719 x
= UniToupper(le16_to_cpu(*name
));
3721 x
= le16_to_cpu(*name
);
3722 if ((rc
= *kname
- x
))
3729 /* compare with additional segment(s) */
3730 while (klen
> 0 && namlen
> 0) {
3731 /* compare with next name segment */
3732 t
= (struct dtslot
*) & p
->slot
[si
];
3733 len
= min(namlen
, DTSLOTDATALEN
);
3734 len
= min(klen
, len
);
3736 for (i
= 0; i
< len
; i
++, kname
++, name
++) {
3737 /* only uppercase if case-insensitive support is on */
3738 if ((flag
& JFS_OS2
) == JFS_OS2
)
3739 x
= UniToupper(le16_to_cpu(*name
));
3741 x
= le16_to_cpu(*name
);
3743 if ((rc
= *kname
- x
))
3752 return (klen
- namlen
);
3757 * ciGetLeafPrefixKey()
3759 * function: compute prefix of suffix compression
3760 * from two adjacent leaf entries
3761 * across page boundary
3763 * return: non-zero on error
3766 static int ciGetLeafPrefixKey(dtpage_t
* lp
, int li
, dtpage_t
* rp
,
3767 int ri
, struct component_name
* key
, int flag
)
3770 wchar_t *pl
, *pr
, *kname
;
3771 struct component_name lkey
;
3772 struct component_name rkey
;
3774 lkey
.name
= kmalloc((JFS_NAME_MAX
+ 1) * sizeof(wchar_t),
3776 if (lkey
.name
== NULL
)
3779 rkey
.name
= kmalloc((JFS_NAME_MAX
+ 1) * sizeof(wchar_t),
3781 if (rkey
.name
== NULL
) {
3786 /* get left and right key */
3787 dtGetKey(lp
, li
, &lkey
, flag
);
3788 lkey
.name
[lkey
.namlen
] = 0;
3790 if ((flag
& JFS_OS2
) == JFS_OS2
)
3793 dtGetKey(rp
, ri
, &rkey
, flag
);
3794 rkey
.name
[rkey
.namlen
] = 0;
3797 if ((flag
& JFS_OS2
) == JFS_OS2
)
3800 /* compute prefix */
3803 namlen
= min(lkey
.namlen
, rkey
.namlen
);
3804 for (pl
= lkey
.name
, pr
= rkey
.name
;
3805 namlen
; pl
++, pr
++, namlen
--, klen
++, kname
++) {
3808 key
->namlen
= klen
+ 1;
3813 /* l->namlen <= r->namlen since l <= r */
3814 if (lkey
.namlen
< rkey
.namlen
) {
3816 key
->namlen
= klen
+ 1;
3817 } else /* l->namelen == r->namelen */
3831 * function: get key of the entry
3833 static void dtGetKey(dtpage_t
* p
, int i
, /* entry index */
3834 struct component_name
* key
, int flag
)
3838 struct ldtentry
*lh
;
3839 struct idtentry
*ih
;
3846 stbl
= DT_GETSTBL(p
);
3848 if (p
->header
.flag
& BT_LEAF
) {
3849 lh
= (struct ldtentry
*) & p
->slot
[si
];
3851 namlen
= lh
->namlen
;
3853 if (flag
& JFS_DIR_INDEX
)
3854 len
= min(namlen
, DTLHDRDATALEN
);
3856 len
= min(namlen
, DTLHDRDATALEN_LEGACY
);
3858 ih
= (struct idtentry
*) & p
->slot
[si
];
3860 namlen
= ih
->namlen
;
3862 len
= min(namlen
, DTIHDRDATALEN
);
3865 key
->namlen
= namlen
;
3869 * move head/only segment
3871 UniStrncpy_from_le(kname
, name
, len
);
3874 * move additional segment(s)
3877 /* get next segment */
3881 len
= min(namlen
, DTSLOTDATALEN
);
3882 UniStrncpy_from_le(kname
, t
->name
, len
);
3892 * function: allocate free slot(s) and
3893 * write a leaf/internal entry
3895 * return: entry slot index
3897 static void dtInsertEntry(dtpage_t
* p
, int index
, struct component_name
* key
,
3898 ddata_t
* data
, struct dt_lock
** dtlock
)
3900 struct dtslot
*h
, *t
;
3901 struct ldtentry
*lh
= NULL
;
3902 struct idtentry
*ih
= NULL
;
3903 int hsi
, fsi
, klen
, len
, nextindex
;
3908 struct dt_lock
*dtlck
= *dtlock
;
3912 struct metapage
*mp
= NULL
;
3917 /* allocate a free slot */
3918 hsi
= fsi
= p
->header
.freelist
;
3920 p
->header
.freelist
= h
->next
;
3921 --p
->header
.freecnt
;
3923 /* open new linelock */
3924 if (dtlck
->index
>= dtlck
->maxcnt
)
3925 dtlck
= (struct dt_lock
*) txLinelock(dtlck
);
3927 lv
= & dtlck
->lv
[dtlck
->index
];
3930 /* write head/only segment */
3931 if (p
->header
.flag
& BT_LEAF
) {
3932 lh
= (struct ldtentry
*) h
;
3934 lh
->inumber
= cpu_to_le32(data
->leaf
.ino
);
3937 if (data
->leaf
.ip
) {
3938 len
= min(klen
, DTLHDRDATALEN
);
3939 if (!(p
->header
.flag
& BT_ROOT
))
3940 bn
= addressPXD(&p
->header
.self
);
3941 lh
->index
= cpu_to_le32(add_index(data
->leaf
.tid
,
3945 len
= min(klen
, DTLHDRDATALEN_LEGACY
);
3947 ih
= (struct idtentry
*) h
;
3953 len
= min(klen
, DTIHDRDATALEN
);
3956 UniStrncpy_to_le(name
, kname
, len
);
3961 /* write additional segment(s) */
3966 fsi
= p
->header
.freelist
;
3968 p
->header
.freelist
= t
->next
;
3969 --p
->header
.freecnt
;
3971 /* is next slot contiguous ? */
3972 if (fsi
!= xsi
+ 1) {
3973 /* close current linelock */
3977 /* open new linelock */
3978 if (dtlck
->index
< dtlck
->maxcnt
)
3981 dtlck
= (struct dt_lock
*) txLinelock(dtlck
);
3982 lv
= & dtlck
->lv
[0];
3990 len
= min(klen
, DTSLOTDATALEN
);
3991 UniStrncpy_to_le(t
->name
, kname
, len
);
3998 /* close current linelock */
4004 /* terminate last/only segment */
4006 /* single segment entry */
4007 if (p
->header
.flag
& BT_LEAF
)
4012 /* multi-segment entry */
4015 /* if insert into middle, shift right succeeding entries in stbl */
4016 stbl
= DT_GETSTBL(p
);
4017 nextindex
= p
->header
.nextindex
;
4018 if (index
< nextindex
) {
4019 memmove(stbl
+ index
+ 1, stbl
+ index
, nextindex
- index
);
4021 if ((p
->header
.flag
& BT_LEAF
) && data
->leaf
.ip
) {
4025 * Need to update slot number for entries that moved
4029 for (n
= index
+ 1; n
<= nextindex
; n
++) {
4030 lh
= (struct ldtentry
*) & (p
->slot
[stbl
[n
]]);
4031 modify_index(data
->leaf
.tid
, data
->leaf
.ip
,
4032 le32_to_cpu(lh
->index
), bn
, n
,
4036 release_metapage(mp
);
4042 /* advance next available entry index of stbl */
4043 ++p
->header
.nextindex
;
4050 * function: move entries from split/left page to new/right page
4052 * nextindex of dst page and freelist/freecnt of both pages
4055 static void dtMoveEntry(dtpage_t
* sp
, int si
, dtpage_t
* dp
,
4056 struct dt_lock
** sdtlock
, struct dt_lock
** ddtlock
,
4059 int ssi
, next
; /* src slot index */
4060 int di
; /* dst entry index */
4061 int dsi
; /* dst slot index */
4062 s8
*sstbl
, *dstbl
; /* sorted entry table */
4064 struct ldtentry
*slh
, *dlh
= NULL
;
4065 struct idtentry
*sih
, *dih
= NULL
;
4066 struct dtslot
*h
, *s
, *d
;
4067 struct dt_lock
*sdtlck
= *sdtlock
, *ddtlck
= *ddtlock
;
4068 struct lv
*slv
, *dlv
;
4072 sstbl
= (s8
*) & sp
->slot
[sp
->header
.stblindex
];
4073 dstbl
= (s8
*) & dp
->slot
[dp
->header
.stblindex
];
4075 dsi
= dp
->header
.freelist
; /* first (whole page) free slot */
4076 sfsi
= sp
->header
.freelist
;
4078 /* linelock destination entry slot */
4079 dlv
= & ddtlck
->lv
[ddtlck
->index
];
4082 /* linelock source entry slot */
4083 slv
= & sdtlck
->lv
[sdtlck
->index
];
4084 slv
->offset
= sstbl
[si
];
4085 xssi
= slv
->offset
- 1;
4091 for (di
= 0; si
< sp
->header
.nextindex
; si
++, di
++) {
4095 /* is next slot contiguous ? */
4096 if (ssi
!= xssi
+ 1) {
4097 /* close current linelock */
4101 /* open new linelock */
4102 if (sdtlck
->index
< sdtlck
->maxcnt
)
4105 sdtlck
= (struct dt_lock
*) txLinelock(sdtlck
);
4106 slv
= & sdtlck
->lv
[0];
4114 * move head/only segment of an entry
4117 h
= d
= &dp
->slot
[dsi
];
4119 /* get src slot and move */
4121 if (sp
->header
.flag
& BT_LEAF
) {
4122 /* get source entry */
4123 slh
= (struct ldtentry
*) s
;
4124 dlh
= (struct ldtentry
*) h
;
4125 snamlen
= slh
->namlen
;
4128 len
= min(snamlen
, DTLHDRDATALEN
);
4129 dlh
->index
= slh
->index
; /* little-endian */
4131 len
= min(snamlen
, DTLHDRDATALEN_LEGACY
);
4133 memcpy(dlh
, slh
, 6 + len
* 2);
4137 /* update dst head/only segment next field */
4141 sih
= (struct idtentry
*) s
;
4142 snamlen
= sih
->namlen
;
4144 len
= min(snamlen
, DTIHDRDATALEN
);
4145 dih
= (struct idtentry
*) h
;
4146 memcpy(dih
, sih
, 10 + len
* 2);
4153 /* free src head/only segment */
4163 * move additional segment(s) of the entry
4166 while ((ssi
= next
) >= 0) {
4167 /* is next slot contiguous ? */
4168 if (ssi
!= xssi
+ 1) {
4169 /* close current linelock */
4173 /* open new linelock */
4174 if (sdtlck
->index
< sdtlck
->maxcnt
)
4180 slv
= & sdtlck
->lv
[0];
4187 /* get next source segment */
4190 /* get next destination free slot */
4193 len
= min(snamlen
, DTSLOTDATALEN
);
4194 UniStrncpy_le(d
->name
, s
->name
, len
);
4203 /* free source segment */
4212 /* terminate dst last/only segment */
4214 /* single segment entry */
4215 if (dp
->header
.flag
& BT_LEAF
)
4220 /* multi-segment entry */
4224 /* close current linelock */
4233 /* update source header */
4234 sp
->header
.freelist
= sfsi
;
4235 sp
->header
.freecnt
+= nd
;
4237 /* update destination header */
4238 dp
->header
.nextindex
= di
;
4240 dp
->header
.freelist
= dsi
;
4241 dp
->header
.freecnt
-= nd
;
4248 * function: free a (leaf/internal) entry
4250 * log freelist header, stbl, and each segment slot of entry
4251 * (even though last/only segment next field is modified,
4252 * physical image logging requires all segment slots of
4253 * the entry logged to avoid applying previous updates
4254 * to the same slots)
4256 static void dtDeleteEntry(dtpage_t
* p
, int fi
, struct dt_lock
** dtlock
)
4258 int fsi
; /* free entry slot index */
4262 struct dt_lock
*dtlck
= *dtlock
;
4266 /* get free entry slot index */
4267 stbl
= DT_GETSTBL(p
);
4270 /* open new linelock */
4271 if (dtlck
->index
>= dtlck
->maxcnt
)
4272 dtlck
= (struct dt_lock
*) txLinelock(dtlck
);
4273 lv
= & dtlck
->lv
[dtlck
->index
];
4277 /* get the head/only segment */
4279 if (p
->header
.flag
& BT_LEAF
)
4280 si
= ((struct ldtentry
*) t
)->next
;
4282 si
= ((struct idtentry
*) t
)->next
;
4289 /* find the last/only segment */
4291 /* is next slot contiguous ? */
4292 if (si
!= xsi
+ 1) {
4293 /* close current linelock */
4297 /* open new linelock */
4298 if (dtlck
->index
< dtlck
->maxcnt
)
4301 dtlck
= (struct dt_lock
*) txLinelock(dtlck
);
4302 lv
= & dtlck
->lv
[0];
4318 /* close current linelock */
4324 /* update freelist */
4325 t
->next
= p
->header
.freelist
;
4326 p
->header
.freelist
= fsi
;
4327 p
->header
.freecnt
+= freecnt
;
4329 /* if delete from middle,
4330 * shift left the succedding entries in the stbl
4332 si
= p
->header
.nextindex
;
4334 memmove(&stbl
[fi
], &stbl
[fi
+ 1], si
- fi
- 1);
4336 p
->header
.nextindex
--;
4343 * function: truncate a (leaf/internal) entry
4345 * log freelist header, stbl, and each segment slot of entry
4346 * (even though last/only segment next field is modified,
4347 * physical image logging requires all segment slots of
4348 * the entry logged to avoid applying previous updates
4349 * to the same slots)
4351 static void dtTruncateEntry(dtpage_t
* p
, int ti
, struct dt_lock
** dtlock
)
4353 int tsi
; /* truncate entry slot index */
4357 struct dt_lock
*dtlck
= *dtlock
;
4361 /* get free entry slot index */
4362 stbl
= DT_GETSTBL(p
);
4365 /* open new linelock */
4366 if (dtlck
->index
>= dtlck
->maxcnt
)
4367 dtlck
= (struct dt_lock
*) txLinelock(dtlck
);
4368 lv
= & dtlck
->lv
[dtlck
->index
];
4372 /* get the head/only segment */
4374 ASSERT(p
->header
.flag
& BT_INTERNAL
);
4375 ((struct idtentry
*) t
)->namlen
= 0;
4376 si
= ((struct idtentry
*) t
)->next
;
4377 ((struct idtentry
*) t
)->next
= -1;
4384 /* find the last/only segment */
4386 /* is next slot contiguous ? */
4387 if (si
!= xsi
+ 1) {
4388 /* close current linelock */
4392 /* open new linelock */
4393 if (dtlck
->index
< dtlck
->maxcnt
)
4396 dtlck
= (struct dt_lock
*) txLinelock(dtlck
);
4397 lv
= & dtlck
->lv
[0];
4413 /* close current linelock */
4419 /* update freelist */
4422 t
->next
= p
->header
.freelist
;
4423 p
->header
.freelist
= fsi
;
4424 p
->header
.freecnt
+= freecnt
;
4429 * dtLinelockFreelist()
4431 static void dtLinelockFreelist(dtpage_t
* p
, /* directory page */
4432 int m
, /* max slot index */
4433 struct dt_lock
** dtlock
)
4435 int fsi
; /* free entry slot index */
4438 struct dt_lock
*dtlck
= *dtlock
;
4442 /* get free entry slot index */
4443 fsi
= p
->header
.freelist
;
4445 /* open new linelock */
4446 if (dtlck
->index
>= dtlck
->maxcnt
)
4447 dtlck
= (struct dt_lock
*) txLinelock(dtlck
);
4448 lv
= & dtlck
->lv
[dtlck
->index
];
4458 /* find the last/only segment */
4459 while (si
< m
&& si
>= 0) {
4460 /* is next slot contiguous ? */
4461 if (si
!= xsi
+ 1) {
4462 /* close current linelock */
4466 /* open new linelock */
4467 if (dtlck
->index
< dtlck
->maxcnt
)
4470 dtlck
= (struct dt_lock
*) txLinelock(dtlck
);
4471 lv
= & dtlck
->lv
[0];
4485 /* close current linelock */
4496 * FUNCTION: Modify the inode number part of a directory entry
4499 * tid - Transaction id
4500 * ip - Inode of parent directory
4501 * key - Name of entry to be modified
4502 * orig_ino - Original inode number expected in entry
4503 * new_ino - New inode number to put into entry
4507 * -ESTALE - If entry found does not match orig_ino passed in
4508 * -ENOENT - If no entry can be found to match key
4509 * 0 - If successfully modified entry
4511 int dtModify(tid_t tid
, struct inode
*ip
,
4512 struct component_name
* key
, ino_t
* orig_ino
, ino_t new_ino
, int flag
)
4516 struct metapage
*mp
;
4519 struct btstack btstack
;
4521 struct dt_lock
*dtlck
;
4524 int entry_si
; /* entry slot index */
4525 struct ldtentry
*entry
;
4528 * search for the entry to modify:
4530 * dtSearch() returns (leaf page pinned, index at which to modify).
4532 if ((rc
= dtSearch(ip
, key
, orig_ino
, &btstack
, flag
)))
4535 /* retrieve search result */
4536 DT_GETSEARCH(ip
, btstack
.top
, bn
, mp
, p
, index
);
4538 BT_MARK_DIRTY(mp
, ip
);
4540 * acquire a transaction lock on the leaf page of named entry
4542 tlck
= txLock(tid
, ip
, mp
, tlckDTREE
| tlckENTRY
);
4543 dtlck
= (struct dt_lock
*) & tlck
->lock
;
4545 /* get slot index of the entry */
4546 stbl
= DT_GETSTBL(p
);
4547 entry_si
= stbl
[index
];
4549 /* linelock entry */
4550 ASSERT(dtlck
->index
== 0);
4551 lv
= & dtlck
->lv
[0];
4552 lv
->offset
= entry_si
;
4556 /* get the head/only segment */
4557 entry
= (struct ldtentry
*) & p
->slot
[entry_si
];
4559 /* substitute the inode number of the entry */
4560 entry
->inumber
= cpu_to_le32(new_ino
);
4562 /* unpin the leaf page */