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 if (DQUOT_ALLOC_BLOCK(ip
, n
)) {
1034 quota_allocation
+= n
;
1036 if ((rc
= dbReAlloc(sbi
->ipbmap
, xaddr
, (s64
) xlen
,
1040 pxdlist
.maxnpxd
= 1;
1042 pxd
= &pxdlist
.pxd
[0];
1043 PXDaddress(pxd
, nxaddr
)
1044 PXDlength(pxd
, xlen
+ n
);
1045 split
->pxdlist
= &pxdlist
;
1046 if ((rc
= dtExtendPage(tid
, ip
, split
, btstack
))) {
1047 nxaddr
= addressPXD(pxd
);
1048 if (xaddr
!= nxaddr
) {
1049 /* free relocated extent */
1050 xlen
= lengthPXD(pxd
);
1051 dbFree(ip
, nxaddr
, (s64
) xlen
);
1053 /* free extended delta */
1054 xlen
= lengthPXD(pxd
) - n
;
1055 xaddr
= addressPXD(pxd
) + xlen
;
1056 dbFree(ip
, xaddr
, (s64
) n
);
1058 } else if (!DO_INDEX(ip
))
1059 ip
->i_size
= lengthPXD(pxd
) << sbi
->l2bsize
;
1068 * split leaf page <sp> into <sp> and a new right page <rp>.
1070 * return <rp> pinned and its extent descriptor <rpxd>
1073 * allocate new directory page extent and
1074 * new index page(s) to cover page split(s)
1076 * allocation hint: ?
1078 n
= btstack
->nsplit
;
1079 pxdlist
.maxnpxd
= pxdlist
.npxd
= 0;
1080 xlen
= sbi
->nbperpage
;
1081 for (pxd
= pxdlist
.pxd
; n
> 0; n
--, pxd
++) {
1082 if ((rc
= dbAlloc(ip
, 0, (s64
) xlen
, &xaddr
)) == 0) {
1083 PXDaddress(pxd
, xaddr
);
1084 PXDlength(pxd
, xlen
);
1091 /* undo allocation */
1095 split
->pxdlist
= &pxdlist
;
1096 if ((rc
= dtSplitPage(tid
, ip
, split
, &rmp
, &rp
, &rpxd
))) {
1099 /* undo allocation */
1104 ip
->i_size
+= PSIZE
;
1107 * propagate up the router entry for the leaf page just split
1109 * insert a router entry for the new page into the parent page,
1110 * propagate the insert/split up the tree by walking back the stack
1111 * of (bn of parent page, index of child page entry in parent page)
1112 * that were traversed during the search for the page that split.
1114 * the propagation of insert/split up the tree stops if the root
1115 * splits or the page inserted into doesn't have to split to hold
1118 * the parent entry for the split page remains the same, and
1119 * a new entry is inserted at its right with the first key and
1120 * block number of the new right page.
1122 * There are a maximum of 4 pages pinned at any time:
1123 * two children, left parent and right parent (when the parent splits).
1124 * keep the child pages pinned while working on the parent.
1125 * make sure that all pins are released at exit.
1127 while ((parent
= BT_POP(btstack
)) != NULL
) {
1128 /* parent page specified by stack frame <parent> */
1130 /* keep current child pages (<lp>, <rp>) pinned */
1135 * insert router entry in parent for new right child page <rp>
1137 /* get the parent page <sp> */
1138 DT_GETPAGE(ip
, parent
->bn
, smp
, PSIZE
, sp
, rc
);
1146 * The new key entry goes ONE AFTER the index of parent entry,
1147 * because the split was to the right.
1149 skip
= parent
->index
+ 1;
1152 * compute the key for the router entry
1154 * key suffix compression:
1155 * for internal pages that have leaf pages as children,
1156 * retain only what's needed to distinguish between
1157 * the new entry and the entry on the page to its left.
1158 * If the keys compare equal, retain the entire key.
1160 * note that compression is performed only at computing
1161 * router key at the lowest internal level.
1162 * further compression of the key between pairs of higher
1163 * level internal pages loses too much information and
1164 * the search may fail.
1165 * (e.g., two adjacent leaf pages of {a, ..., x} {xx, ...,}
1166 * results in two adjacent parent entries (a)(xx).
1167 * if split occurs between these two entries, and
1168 * if compression is applied, the router key of parent entry
1169 * of right page (x) will divert search for x into right
1170 * subtree and miss x in the left subtree.)
1172 * the entire key must be retained for the next-to-leftmost
1173 * internal key at any level of the tree, or search may fail
1176 switch (rp
->header
.flag
& BT_TYPE
) {
1179 * compute the length of prefix for suffix compression
1180 * between last entry of left page and first entry
1183 if ((sp
->header
.flag
& BT_ROOT
&& skip
> 1) ||
1184 sp
->header
.prev
!= 0 || skip
> 1) {
1185 /* compute uppercase router prefix key */
1186 rc
= ciGetLeafPrefixKey(lp
,
1187 lp
->header
.nextindex
-1,
1197 /* next to leftmost entry of
1198 lowest internal level */
1200 /* compute uppercase router key */
1201 dtGetKey(rp
, 0, &key
, sbi
->mntflag
);
1202 key
.name
[key
.namlen
] = 0;
1204 if ((sbi
->mntflag
& JFS_OS2
) == JFS_OS2
)
1208 n
= NDTINTERNAL(key
.namlen
);
1212 dtGetKey(rp
, 0, &key
, sbi
->mntflag
);
1213 n
= NDTINTERNAL(key
.namlen
);
1217 jfs_err("dtSplitUp(): UFO!");
1221 /* unpin left child page */
1225 * compute the data for the router entry
1227 data
->xd
= rpxd
; /* child page xd */
1230 * parent page is full - split the parent page
1232 if (n
> sp
->header
.freecnt
) {
1233 /* init for parent page split */
1235 split
->index
= skip
; /* index at insert */
1238 /* split->data = data; */
1240 /* unpin right child page */
1243 /* The split routines insert the new entry,
1244 * acquire txLock as appropriate.
1245 * return <rp> pinned and its block number <rbn>.
1247 rc
= (sp
->header
.flag
& BT_ROOT
) ?
1248 dtSplitRoot(tid
, ip
, split
, &rmp
) :
1249 dtSplitPage(tid
, ip
, split
, &rmp
, &rp
, &rpxd
);
1255 /* smp and rmp are pinned */
1258 * parent page is not full - insert router entry in parent page
1261 BT_MARK_DIRTY(smp
, ip
);
1263 * acquire a transaction lock on the parent page
1265 tlck
= txLock(tid
, ip
, smp
, tlckDTREE
| tlckENTRY
);
1266 dtlck
= (struct dt_lock
*) & tlck
->lock
;
1267 ASSERT(dtlck
->index
== 0);
1268 lv
= & dtlck
->lv
[0];
1270 /* linelock header */
1275 /* linelock stbl of non-root parent page */
1276 if (!(sp
->header
.flag
& BT_ROOT
)) {
1278 n
= skip
>> L2DTSLOTSIZE
;
1279 lv
->offset
= sp
->header
.stblindex
+ n
;
1281 ((sp
->header
.nextindex
-
1282 1) >> L2DTSLOTSIZE
) - n
+ 1;
1286 dtInsertEntry(sp
, skip
, &key
, data
, &dtlck
);
1288 /* exit propagate up */
1293 /* unpin current split and its right page */
1298 * free remaining extents allocated for split
1302 pxd
= &pxdlist
.pxd
[n
];
1303 for (; n
< pxdlist
.maxnpxd
; n
++, pxd
++)
1304 dbFree(ip
, addressPXD(pxd
), (s64
) lengthPXD(pxd
));
1309 /* Rollback quota allocation */
1310 if (rc
&& quota_allocation
)
1311 DQUOT_FREE_BLOCK(ip
, quota_allocation
);
1322 * function: Split a non-root page of a btree.
1326 * return: 0 - success;
1328 * return split and new page pinned;
1330 static int dtSplitPage(tid_t tid
, struct inode
*ip
, struct dtsplit
* split
,
1331 struct metapage
** rmpp
, dtpage_t
** rpp
, pxd_t
* rpxdp
)
1334 struct metapage
*smp
;
1336 struct metapage
*rmp
;
1337 dtpage_t
*rp
; /* new right page allocated */
1338 s64 rbn
; /* new right page block number */
1339 struct metapage
*mp
;
1342 struct pxdlist
*pxdlist
;
1344 int skip
, nextindex
, half
, left
, nxt
, off
, si
;
1345 struct ldtentry
*ldtentry
;
1346 struct idtentry
*idtentry
;
1351 struct dt_lock
*sdtlck
, *rdtlck
;
1353 struct dt_lock
*dtlck
;
1354 struct lv
*slv
, *rlv
, *lv
;
1356 /* get split page */
1358 sp
= DT_PAGE(ip
, smp
);
1361 * allocate the new right page for the split
1363 pxdlist
= split
->pxdlist
;
1364 pxd
= &pxdlist
->pxd
[pxdlist
->npxd
];
1366 rbn
= addressPXD(pxd
);
1367 rmp
= get_metapage(ip
, rbn
, PSIZE
, 1);
1371 /* Allocate blocks to quota. */
1372 if (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
;
1896 /* get split root page */
1898 sp
= &JFS_IP(ip
)->i_dtroot
;
1901 * allocate/initialize a single (right) child page
1903 * N.B. at first split, a one (or two) block to fit new entry
1904 * is allocated; at subsequent split, a full page is allocated;
1906 pxdlist
= split
->pxdlist
;
1907 pxd
= &pxdlist
->pxd
[pxdlist
->npxd
];
1909 rbn
= addressPXD(pxd
);
1910 xlen
= lengthPXD(pxd
);
1911 xsize
= xlen
<< JFS_SBI(sb
)->l2bsize
;
1912 rmp
= get_metapage(ip
, rbn
, xsize
, 1);
1918 /* Allocate blocks to quota. */
1919 if (DQUOT_ALLOC_BLOCK(ip
, lengthPXD(pxd
))) {
1920 release_metapage(rmp
);
1924 BT_MARK_DIRTY(rmp
, ip
);
1926 * acquire a transaction lock on the new right page
1928 tlck
= txLock(tid
, ip
, rmp
, tlckDTREE
| tlckNEW
);
1929 dtlck
= (struct dt_lock
*) & tlck
->lock
;
1932 (sp
->header
.flag
& BT_LEAF
) ? BT_LEAF
: BT_INTERNAL
;
1933 rp
->header
.self
= *pxd
;
1935 /* initialize sibling pointers */
1936 rp
->header
.next
= 0;
1937 rp
->header
.prev
= 0;
1940 * move in-line root page into new right page extent
1942 /* linelock header + copied entries + new stbl (1st slot) in new page */
1943 ASSERT(dtlck
->index
== 0);
1944 lv
= & dtlck
->lv
[0];
1946 lv
->length
= 10; /* 1 + 8 + 1 */
1949 n
= xsize
>> L2DTSLOTSIZE
;
1950 rp
->header
.maxslot
= n
;
1951 stblsize
= (n
+ 31) >> L2DTSLOTSIZE
;
1953 /* copy old stbl to new stbl at start of extended area */
1954 rp
->header
.stblindex
= DTROOTMAXSLOT
;
1955 stbl
= (s8
*) & rp
->slot
[DTROOTMAXSLOT
];
1956 memcpy(stbl
, sp
->header
.stbl
, sp
->header
.nextindex
);
1957 rp
->header
.nextindex
= sp
->header
.nextindex
;
1959 /* copy old data area to start of new data area */
1960 memcpy(&rp
->slot
[1], &sp
->slot
[1], IDATASIZE
);
1963 * append free region of newly extended area at tail of freelist
1965 /* init free region of newly extended area */
1966 fsi
= n
= DTROOTMAXSLOT
+ stblsize
;
1968 for (fsi
++; fsi
< rp
->header
.maxslot
; f
++, fsi
++)
1972 /* append new free region at tail of old freelist */
1973 fsi
= sp
->header
.freelist
;
1975 rp
->header
.freelist
= n
;
1977 rp
->header
.freelist
= fsi
;
1982 } while (fsi
!= -1);
1987 rp
->header
.freecnt
= sp
->header
.freecnt
+ rp
->header
.maxslot
- n
;
1990 * Update directory index table for entries now in right page
1992 if ((rp
->header
.flag
& BT_LEAF
) && DO_INDEX(ip
)) {
1994 struct metapage
*mp
= NULL
;
1995 struct ldtentry
*ldtentry
;
1997 stbl
= DT_GETSTBL(rp
);
1998 for (n
= 0; n
< rp
->header
.nextindex
; n
++) {
1999 ldtentry
= (struct ldtentry
*) & rp
->slot
[stbl
[n
]];
2000 modify_index(tid
, ip
, le32_to_cpu(ldtentry
->index
),
2001 rbn
, n
, &mp
, &lblock
);
2004 release_metapage(mp
);
2007 * insert the new entry into the new right/child page
2008 * (skip index in the new right page will not change)
2010 dtInsertEntry(rp
, split
->index
, split
->key
, split
->data
, &dtlck
);
2013 * reset parent/root page
2015 * set the 1st entry offset to 0, which force the left-most key
2016 * at any level of the tree to be less than any search key.
2018 * The btree comparison code guarantees that the left-most key on any
2019 * level of the tree is never used, so it doesn't need to be filled in.
2021 BT_MARK_DIRTY(smp
, ip
);
2023 * acquire a transaction lock on the root page (in-memory inode)
2025 tlck
= txLock(tid
, ip
, smp
, tlckDTREE
| tlckNEW
| tlckBTROOT
);
2026 dtlck
= (struct dt_lock
*) & tlck
->lock
;
2029 ASSERT(dtlck
->index
== 0);
2030 lv
= & dtlck
->lv
[0];
2032 lv
->length
= DTROOTMAXSLOT
;
2035 /* update page header of root */
2036 if (sp
->header
.flag
& BT_LEAF
) {
2037 sp
->header
.flag
&= ~BT_LEAF
;
2038 sp
->header
.flag
|= BT_INTERNAL
;
2041 /* init the first entry */
2042 s
= (struct idtentry
*) & sp
->slot
[DTENTRYSTART
];
2048 stbl
= sp
->header
.stbl
;
2049 stbl
[0] = DTENTRYSTART
;
2050 sp
->header
.nextindex
= 1;
2053 fsi
= DTENTRYSTART
+ 1;
2056 /* init free region of remaining area */
2057 for (fsi
++; fsi
< DTROOTMAXSLOT
; f
++, fsi
++)
2061 sp
->header
.freelist
= DTENTRYSTART
+ 1;
2062 sp
->header
.freecnt
= DTROOTMAXSLOT
- (DTENTRYSTART
+ 1);
2073 * function: delete the entry(s) referenced by a key.
2079 int dtDelete(tid_t tid
,
2080 struct inode
*ip
, struct component_name
* key
, ino_t
* ino
, int flag
)
2084 struct metapage
*mp
, *imp
;
2087 struct btstack btstack
;
2088 struct dt_lock
*dtlck
;
2092 struct ldtentry
*ldtentry
;
2094 u32 table_index
, next_index
;
2095 struct metapage
*nmp
;
2099 * search for the entry to delete:
2101 * dtSearch() returns (leaf page pinned, index at which to delete).
2103 if ((rc
= dtSearch(ip
, key
, ino
, &btstack
, flag
)))
2106 /* retrieve search result */
2107 DT_GETSEARCH(ip
, btstack
.top
, bn
, mp
, p
, index
);
2110 * We need to find put the index of the next entry into the
2111 * directory index table in order to resume a readdir from this
2115 stbl
= DT_GETSTBL(p
);
2116 ldtentry
= (struct ldtentry
*) & p
->slot
[stbl
[index
]];
2117 table_index
= le32_to_cpu(ldtentry
->index
);
2118 if (index
== (p
->header
.nextindex
- 1)) {
2120 * Last entry in this leaf page
2122 if ((p
->header
.flag
& BT_ROOT
)
2123 || (p
->header
.next
== 0))
2126 /* Read next leaf page */
2127 DT_GETPAGE(ip
, le64_to_cpu(p
->header
.next
),
2128 nmp
, PSIZE
, np
, rc
);
2132 stbl
= DT_GETSTBL(np
);
2134 (struct ldtentry
*) & np
->
2137 le32_to_cpu(ldtentry
->index
);
2143 (struct ldtentry
*) & p
->slot
[stbl
[index
+ 1]];
2144 next_index
= le32_to_cpu(ldtentry
->index
);
2146 free_index(tid
, ip
, table_index
, next_index
);
2149 * the leaf page becomes empty, delete the page
2151 if (p
->header
.nextindex
== 1) {
2152 /* delete empty page */
2153 rc
= dtDeleteUp(tid
, ip
, mp
, p
, &btstack
);
2156 * the leaf page has other entries remaining:
2158 * delete the entry from the leaf page.
2161 BT_MARK_DIRTY(mp
, ip
);
2163 * acquire a transaction lock on the leaf page
2165 tlck
= txLock(tid
, ip
, mp
, tlckDTREE
| tlckENTRY
);
2166 dtlck
= (struct dt_lock
*) & tlck
->lock
;
2169 * Do not assume that dtlck->index will be zero. During a
2170 * rename within a directory, this transaction may have
2171 * modified this page already when adding the new entry.
2174 /* linelock header */
2175 if (dtlck
->index
>= dtlck
->maxcnt
)
2176 dtlck
= (struct dt_lock
*) txLinelock(dtlck
);
2177 lv
= & dtlck
->lv
[dtlck
->index
];
2182 /* linelock stbl of non-root leaf page */
2183 if (!(p
->header
.flag
& BT_ROOT
)) {
2184 if (dtlck
->index
>= dtlck
->maxcnt
)
2185 dtlck
= (struct dt_lock
*) txLinelock(dtlck
);
2186 lv
= & dtlck
->lv
[dtlck
->index
];
2187 i
= index
>> L2DTSLOTSIZE
;
2188 lv
->offset
= p
->header
.stblindex
+ i
;
2190 ((p
->header
.nextindex
- 1) >> L2DTSLOTSIZE
) -
2195 /* free the leaf entry */
2196 dtDeleteEntry(p
, index
, &dtlck
);
2199 * Update directory index table for entries moved in stbl
2201 if (DO_INDEX(ip
) && index
< p
->header
.nextindex
) {
2205 stbl
= DT_GETSTBL(p
);
2206 for (i
= index
; i
< p
->header
.nextindex
; i
++) {
2208 (struct ldtentry
*) & p
->slot
[stbl
[i
]];
2209 modify_index(tid
, ip
,
2210 le32_to_cpu(ldtentry
->index
),
2211 bn
, i
, &imp
, &lblock
);
2214 release_metapage(imp
);
2228 * free empty pages as propagating deletion up the tree
2234 static int dtDeleteUp(tid_t tid
, struct inode
*ip
,
2235 struct metapage
* fmp
, dtpage_t
* fp
, struct btstack
* btstack
)
2238 struct metapage
*mp
;
2240 int index
, nextindex
;
2242 struct btframe
*parent
;
2243 struct dt_lock
*dtlck
;
2246 struct pxd_lock
*pxdlock
;
2250 * keep the root leaf page which has become empty
2252 if (BT_IS_ROOT(fmp
)) {
2256 * dtInitRoot() acquires txlock on the root
2258 dtInitRoot(tid
, ip
, PARENT(ip
));
2266 * free the non-root leaf page
2269 * acquire a transaction lock on the page
2271 * write FREEXTENT|NOREDOPAGE log record
2272 * N.B. linelock is overlaid as freed extent descriptor, and
2273 * the buffer page is freed;
2275 tlck
= txMaplock(tid
, ip
, tlckDTREE
| tlckFREE
);
2276 pxdlock
= (struct pxd_lock
*) & tlck
->lock
;
2277 pxdlock
->flag
= mlckFREEPXD
;
2278 pxdlock
->pxd
= fp
->header
.self
;
2281 /* update sibling pointers */
2282 if ((rc
= dtRelink(tid
, ip
, fp
))) {
2287 xlen
= lengthPXD(&fp
->header
.self
);
2289 /* Free quota allocation. */
2290 DQUOT_FREE_BLOCK(ip
, xlen
);
2292 /* free/invalidate its buffer page */
2293 discard_metapage(fmp
);
2296 * propagate page deletion up the directory tree
2298 * If the delete from the parent page makes it empty,
2299 * continue all the way up the tree.
2300 * stop if the root page is reached (which is never deleted) or
2301 * if the entry deletion does not empty the page.
2303 while ((parent
= BT_POP(btstack
)) != NULL
) {
2304 /* pin the parent page <sp> */
2305 DT_GETPAGE(ip
, parent
->bn
, mp
, PSIZE
, p
, rc
);
2310 * free the extent of the child page deleted
2312 index
= parent
->index
;
2315 * delete the entry for the child page from parent
2317 nextindex
= p
->header
.nextindex
;
2320 * the parent has the single entry being deleted:
2322 * free the parent page which has become empty.
2324 if (nextindex
== 1) {
2326 * keep the root internal page which has become empty
2328 if (p
->header
.flag
& BT_ROOT
) {
2332 * dtInitRoot() acquires txlock on the root
2334 dtInitRoot(tid
, ip
, PARENT(ip
));
2341 * free the parent page
2345 * acquire a transaction lock on the page
2347 * write FREEXTENT|NOREDOPAGE log record
2351 tlckDTREE
| tlckFREE
);
2352 pxdlock
= (struct pxd_lock
*) & tlck
->lock
;
2353 pxdlock
->flag
= mlckFREEPXD
;
2354 pxdlock
->pxd
= p
->header
.self
;
2357 /* update sibling pointers */
2358 if ((rc
= dtRelink(tid
, ip
, p
))) {
2363 xlen
= lengthPXD(&p
->header
.self
);
2365 /* Free quota allocation */
2366 DQUOT_FREE_BLOCK(ip
, xlen
);
2368 /* free/invalidate its buffer page */
2369 discard_metapage(mp
);
2377 * the parent has other entries remaining:
2379 * delete the router entry from the parent page.
2381 BT_MARK_DIRTY(mp
, ip
);
2383 * acquire a transaction lock on the page
2385 * action: router entry deletion
2387 tlck
= txLock(tid
, ip
, mp
, tlckDTREE
| tlckENTRY
);
2388 dtlck
= (struct dt_lock
*) & tlck
->lock
;
2390 /* linelock header */
2391 if (dtlck
->index
>= dtlck
->maxcnt
)
2392 dtlck
= (struct dt_lock
*) txLinelock(dtlck
);
2393 lv
= & dtlck
->lv
[dtlck
->index
];
2398 /* linelock stbl of non-root leaf page */
2399 if (!(p
->header
.flag
& BT_ROOT
)) {
2400 if (dtlck
->index
< dtlck
->maxcnt
)
2403 dtlck
= (struct dt_lock
*) txLinelock(dtlck
);
2404 lv
= & dtlck
->lv
[0];
2406 i
= index
>> L2DTSLOTSIZE
;
2407 lv
->offset
= p
->header
.stblindex
+ i
;
2409 ((p
->header
.nextindex
- 1) >> L2DTSLOTSIZE
) -
2414 /* free the router entry */
2415 dtDeleteEntry(p
, index
, &dtlck
);
2417 /* reset key of new leftmost entry of level (for consistency) */
2419 ((p
->header
.flag
& BT_ROOT
) || p
->header
.prev
== 0))
2420 dtTruncateEntry(p
, 0, &dtlck
);
2422 /* unpin the parent page */
2425 /* exit propagation up */
2430 ip
->i_size
-= PSIZE
;
2437 * NAME: dtRelocate()
2439 * FUNCTION: relocate dtpage (internal or leaf) of directory;
2440 * This function is mainly used by defragfs utility.
2442 int dtRelocate(tid_t tid
, struct inode
*ip
, s64 lmxaddr
, pxd_t
* opxd
,
2446 struct metapage
*mp
, *pmp
, *lmp
, *rmp
;
2447 dtpage_t
*p
, *pp
, *rp
= 0, *lp
= 0;
2450 struct btstack btstack
;
2452 s64 oxaddr
, nextbn
, prevbn
;
2455 struct dt_lock
*dtlck
;
2456 struct pxd_lock
*pxdlock
;
2460 oxaddr
= addressPXD(opxd
);
2461 xlen
= lengthPXD(opxd
);
2463 jfs_info("dtRelocate: lmxaddr:%Ld xaddr:%Ld:%Ld xlen:%d",
2464 (long long)lmxaddr
, (long long)oxaddr
, (long long)nxaddr
,
2468 * 1. get the internal parent dtpage covering
2469 * router entry for the tartget page to be relocated;
2471 rc
= dtSearchNode(ip
, lmxaddr
, opxd
, &btstack
);
2475 /* retrieve search result */
2476 DT_GETSEARCH(ip
, btstack
.top
, bn
, pmp
, pp
, index
);
2477 jfs_info("dtRelocate: parent router entry validated.");
2480 * 2. relocate the target dtpage
2482 /* read in the target page from src extent */
2483 DT_GETPAGE(ip
, oxaddr
, mp
, PSIZE
, p
, rc
);
2485 /* release the pinned parent page */
2491 * read in sibling pages if any to update sibling pointers;
2494 if (p
->header
.next
) {
2495 nextbn
= le64_to_cpu(p
->header
.next
);
2496 DT_GETPAGE(ip
, nextbn
, rmp
, PSIZE
, rp
, rc
);
2505 if (p
->header
.prev
) {
2506 prevbn
= le64_to_cpu(p
->header
.prev
);
2507 DT_GETPAGE(ip
, prevbn
, lmp
, PSIZE
, lp
, rc
);
2517 /* at this point, all xtpages to be updated are in memory */
2520 * update sibling pointers of sibling dtpages if any;
2523 tlck
= txLock(tid
, ip
, lmp
, tlckDTREE
| tlckRELINK
);
2524 dtlck
= (struct dt_lock
*) & tlck
->lock
;
2525 /* linelock header */
2526 ASSERT(dtlck
->index
== 0);
2527 lv
= & dtlck
->lv
[0];
2532 lp
->header
.next
= cpu_to_le64(nxaddr
);
2537 tlck
= txLock(tid
, ip
, rmp
, tlckDTREE
| tlckRELINK
);
2538 dtlck
= (struct dt_lock
*) & tlck
->lock
;
2539 /* linelock header */
2540 ASSERT(dtlck
->index
== 0);
2541 lv
= & dtlck
->lv
[0];
2546 rp
->header
.prev
= cpu_to_le64(nxaddr
);
2551 * update the target dtpage to be relocated
2553 * write LOG_REDOPAGE of LOG_NEW type for dst page
2554 * for the whole target page (logredo() will apply
2555 * after image and update bmap for allocation of the
2556 * dst extent), and update bmap for allocation of
2559 tlck
= txLock(tid
, ip
, mp
, tlckDTREE
| tlckNEW
);
2560 dtlck
= (struct dt_lock
*) & tlck
->lock
;
2561 /* linelock header */
2562 ASSERT(dtlck
->index
== 0);
2563 lv
= & dtlck
->lv
[0];
2565 /* update the self address in the dtpage header */
2566 pxd
= &p
->header
.self
;
2567 PXDaddress(pxd
, nxaddr
);
2569 /* the dst page is the same as the src page, i.e.,
2570 * linelock for afterimage of the whole page;
2573 lv
->length
= p
->header
.maxslot
;
2576 /* update the buffer extent descriptor of the dtpage */
2577 xsize
= xlen
<< JFS_SBI(ip
->i_sb
)->l2bsize
;
2579 /* unpin the relocated page */
2581 jfs_info("dtRelocate: target dtpage relocated.");
2583 /* the moved extent is dtpage, then a LOG_NOREDOPAGE log rec
2584 * needs to be written (in logredo(), the LOG_NOREDOPAGE log rec
2585 * will also force a bmap update ).
2589 * 3. acquire maplock for the source extent to be freed;
2591 /* for dtpage relocation, write a LOG_NOREDOPAGE record
2592 * for the source dtpage (logredo() will init NoRedoPage
2593 * filter and will also update bmap for free of the source
2594 * dtpage), and upadte bmap for free of the source dtpage;
2596 tlck
= txMaplock(tid
, ip
, tlckDTREE
| tlckFREE
);
2597 pxdlock
= (struct pxd_lock
*) & tlck
->lock
;
2598 pxdlock
->flag
= mlckFREEPXD
;
2599 PXDaddress(&pxdlock
->pxd
, oxaddr
);
2600 PXDlength(&pxdlock
->pxd
, xlen
);
2604 * 4. update the parent router entry for relocation;
2606 * acquire tlck for the parent entry covering the target dtpage;
2607 * write LOG_REDOPAGE to apply after image only;
2609 jfs_info("dtRelocate: update parent router entry.");
2610 tlck
= txLock(tid
, ip
, pmp
, tlckDTREE
| tlckENTRY
);
2611 dtlck
= (struct dt_lock
*) & tlck
->lock
;
2612 lv
= & dtlck
->lv
[dtlck
->index
];
2614 /* update the PXD with the new address */
2615 stbl
= DT_GETSTBL(pp
);
2616 pxd
= (pxd_t
*) & pp
->slot
[stbl
[index
]];
2617 PXDaddress(pxd
, nxaddr
);
2618 lv
->offset
= stbl
[index
];
2622 /* unpin the parent dtpage */
2629 * NAME: dtSearchNode()
2631 * FUNCTION: Search for an dtpage containing a specified address
2632 * This function is mainly used by defragfs utility.
2634 * NOTE: Search result on stack, the found page is pinned at exit.
2635 * The result page must be an internal dtpage.
2636 * lmxaddr give the address of the left most page of the
2637 * dtree level, in which the required dtpage resides.
2639 static int dtSearchNode(struct inode
*ip
, s64 lmxaddr
, pxd_t
* kpxd
,
2640 struct btstack
* btstack
)
2644 struct metapage
*mp
;
2646 int psize
= 288; /* initial in-line directory */
2650 struct btframe
*btsp
;
2652 BT_CLR(btstack
); /* reset stack */
2655 * descend tree to the level with specified leftmost page
2657 * by convention, root bn = 0.
2660 /* get/pin the page to search */
2661 DT_GETPAGE(ip
, bn
, mp
, psize
, p
, rc
);
2665 /* does the xaddr of leftmost page of the levevl
2666 * matches levevl search key ?
2668 if (p
->header
.flag
& BT_ROOT
) {
2671 } else if (addressPXD(&p
->header
.self
) == lmxaddr
)
2675 * descend down to leftmost child page
2677 if (p
->header
.flag
& BT_LEAF
) {
2682 /* get the leftmost entry */
2683 stbl
= DT_GETSTBL(p
);
2684 pxd
= (pxd_t
*) & p
->slot
[stbl
[0]];
2686 /* get the child page block address */
2687 bn
= addressPXD(pxd
);
2688 psize
= lengthPXD(pxd
) << JFS_SBI(ip
->i_sb
)->l2bsize
;
2689 /* unpin the parent page */
2694 * search each page at the current levevl
2697 stbl
= DT_GETSTBL(p
);
2698 for (i
= 0; i
< p
->header
.nextindex
; i
++) {
2699 pxd
= (pxd_t
*) & p
->slot
[stbl
[i
]];
2701 /* found the specified router entry */
2702 if (addressPXD(pxd
) == addressPXD(kpxd
) &&
2703 lengthPXD(pxd
) == lengthPXD(kpxd
)) {
2704 btsp
= btstack
->top
;
2713 /* get the right sibling page if any */
2715 bn
= le64_to_cpu(p
->header
.next
);
2721 /* unpin current page */
2724 /* get the right sibling page */
2725 DT_GETPAGE(ip
, bn
, mp
, PSIZE
, p
, rc
);
2731 #endif /* _NOTYET */
2737 * link around a freed page.
2740 * fp: page to be freed
2744 static int dtRelink(tid_t tid
, struct inode
*ip
, dtpage_t
* p
)
2747 struct metapage
*mp
;
2750 struct dt_lock
*dtlck
;
2753 nextbn
= le64_to_cpu(p
->header
.next
);
2754 prevbn
= le64_to_cpu(p
->header
.prev
);
2756 /* update prev pointer of the next page */
2758 DT_GETPAGE(ip
, nextbn
, mp
, PSIZE
, p
, rc
);
2762 BT_MARK_DIRTY(mp
, ip
);
2764 * acquire a transaction lock on the next page
2766 * action: update prev pointer;
2768 tlck
= txLock(tid
, ip
, mp
, tlckDTREE
| tlckRELINK
);
2769 jfs_info("dtRelink nextbn: tlck = 0x%p, ip = 0x%p, mp=0x%p",
2771 dtlck
= (struct dt_lock
*) & tlck
->lock
;
2773 /* linelock header */
2774 if (dtlck
->index
>= dtlck
->maxcnt
)
2775 dtlck
= (struct dt_lock
*) txLinelock(dtlck
);
2776 lv
= & dtlck
->lv
[dtlck
->index
];
2781 p
->header
.prev
= cpu_to_le64(prevbn
);
2785 /* update next pointer of the previous page */
2787 DT_GETPAGE(ip
, prevbn
, mp
, PSIZE
, p
, rc
);
2791 BT_MARK_DIRTY(mp
, ip
);
2793 * acquire a transaction lock on the prev page
2795 * action: update next pointer;
2797 tlck
= txLock(tid
, ip
, mp
, tlckDTREE
| tlckRELINK
);
2798 jfs_info("dtRelink prevbn: tlck = 0x%p, ip = 0x%p, mp=0x%p",
2800 dtlck
= (struct dt_lock
*) & tlck
->lock
;
2802 /* linelock header */
2803 if (dtlck
->index
>= dtlck
->maxcnt
)
2804 dtlck
= (struct dt_lock
*) txLinelock(dtlck
);
2805 lv
= & dtlck
->lv
[dtlck
->index
];
2810 p
->header
.next
= cpu_to_le64(nextbn
);
2821 * initialize directory root (inline in inode)
2823 void dtInitRoot(tid_t tid
, struct inode
*ip
, u32 idotdot
)
2825 struct jfs_inode_info
*jfs_ip
= JFS_IP(ip
);
2830 struct dt_lock
*dtlck
;
2835 * If this was previously an non-empty directory, we need to remove
2836 * the old directory table.
2839 if (!jfs_dirtable_inline(ip
)) {
2840 struct tblock
*tblk
= tid_to_tblock(tid
);
2842 * We're playing games with the tid's xflag. If
2843 * we're removing a regular file, the file's xtree
2844 * is committed with COMMIT_PMAP, but we always
2845 * commit the directories xtree with COMMIT_PWMAP.
2847 xflag_save
= tblk
->xflag
;
2850 * xtTruncate isn't guaranteed to fully truncate
2851 * the xtree. The caller needs to check i_size
2852 * after committing the transaction to see if
2853 * additional truncation is needed. The
2854 * COMMIT_Stale flag tells caller that we
2855 * initiated the truncation.
2857 xtTruncate(tid
, ip
, 0, COMMIT_PWMAP
);
2858 set_cflag(COMMIT_Stale
, ip
);
2860 tblk
->xflag
= xflag_save
;
2864 jfs_ip
->next_index
= 2;
2866 ip
->i_size
= IDATASIZE
;
2869 * acquire a transaction lock on the root
2871 * action: directory initialization;
2873 tlck
= txLock(tid
, ip
, (struct metapage
*) & jfs_ip
->bxflag
,
2874 tlckDTREE
| tlckENTRY
| tlckBTROOT
);
2875 dtlck
= (struct dt_lock
*) & tlck
->lock
;
2878 ASSERT(dtlck
->index
== 0);
2879 lv
= & dtlck
->lv
[0];
2881 lv
->length
= DTROOTMAXSLOT
;
2884 p
= &jfs_ip
->i_dtroot
;
2886 p
->header
.flag
= DXD_INDEX
| BT_ROOT
| BT_LEAF
;
2888 p
->header
.nextindex
= 0;
2894 /* init data area of root */
2895 for (fsi
++; fsi
< DTROOTMAXSLOT
; f
++, fsi
++)
2899 p
->header
.freelist
= 1;
2900 p
->header
.freecnt
= 8;
2902 /* init '..' entry */
2903 p
->header
.idotdot
= cpu_to_le32(idotdot
);
2909 * add_missing_indices()
2911 * function: Fix dtree page in which one or more entries has an invalid index.
2912 * fsck.jfs should really fix this, but it currently does not.
2913 * Called from jfs_readdir when bad index is detected.
2915 static void add_missing_indices(struct inode
*inode
, s64 bn
)
2918 struct dt_lock
*dtlck
;
2922 struct metapage
*mp
;
2929 tid
= txBegin(inode
->i_sb
, 0);
2931 DT_GETPAGE(inode
, bn
, mp
, PSIZE
, p
, rc
);
2934 printk(KERN_ERR
"DT_GETPAGE failed!\n");
2937 BT_MARK_DIRTY(mp
, inode
);
2939 ASSERT(p
->header
.flag
& BT_LEAF
);
2941 tlck
= txLock(tid
, inode
, mp
, tlckDTREE
| tlckENTRY
);
2943 tlck
->type
|= tlckBTROOT
;
2945 dtlck
= (struct dt_lock
*) &tlck
->lock
;
2947 stbl
= DT_GETSTBL(p
);
2948 for (i
= 0; i
< p
->header
.nextindex
; i
++) {
2949 d
= (struct ldtentry
*) &p
->slot
[stbl
[i
]];
2950 index
= le32_to_cpu(d
->index
);
2951 if ((index
< 2) || (index
>= JFS_IP(inode
)->next_index
)) {
2952 d
->index
= cpu_to_le32(add_index(tid
, inode
, bn
, i
));
2953 if (dtlck
->index
>= dtlck
->maxcnt
)
2954 dtlck
= (struct dt_lock
*) txLinelock(dtlck
);
2955 lv
= &dtlck
->lv
[dtlck
->index
];
2956 lv
->offset
= stbl
[i
];
2963 (void) txCommit(tid
, 1, &inode
, 0);
2969 * Buffer to hold directory entry info while traversing a dtree page
2970 * before being fed to the filldir function
2980 * function to determine next variable-sized jfs_dirent in buffer
2982 static inline struct jfs_dirent
*next_jfs_dirent(struct jfs_dirent
*dirent
)
2984 return (struct jfs_dirent
*)
2986 ((sizeof (struct jfs_dirent
) + dirent
->name_len
+ 1 +
2987 sizeof (loff_t
) - 1) &
2988 ~(sizeof (loff_t
) - 1)));
2994 * function: read directory entries sequentially
2995 * from the specified entry offset
2999 * return: offset = (pn, index) of start entry
3000 * of next jfs_readdir()/dtRead()
3002 int jfs_readdir(struct file
*filp
, void *dirent
, filldir_t filldir
)
3004 struct inode
*ip
= filp
->f_path
.dentry
->d_inode
;
3005 struct nls_table
*codepage
= JFS_SBI(ip
->i_sb
)->nls_tab
;
3007 loff_t dtpos
; /* legacy OS/2 style position */
3012 } *dtoffset
= (struct dtoffset
*) &dtpos
;
3014 struct metapage
*mp
;
3018 struct btstack btstack
;
3022 int d_namleft
, len
, outlen
;
3023 unsigned long dirent_buf
;
3027 uint loop_count
= 0;
3028 struct jfs_dirent
*jfs_dirent
;
3030 int overflow
, fix_page
, page_fixed
= 0;
3031 static int unique_pos
= 2; /* If we can't fix broken index */
3033 if (filp
->f_pos
== DIREND
)
3038 * persistent index is stored in directory entries.
3039 * Special cases: 0 = .
3041 * -1 = End of directory
3045 dir_index
= (u32
) filp
->f_pos
;
3047 if (dir_index
> 1) {
3048 struct dir_table_slot dirtab_slot
;
3051 (dir_index
>= JFS_IP(ip
)->next_index
)) {
3052 /* Stale position. Directory has shrunk */
3053 filp
->f_pos
= DIREND
;
3057 rc
= read_index(ip
, dir_index
, &dirtab_slot
);
3059 filp
->f_pos
= DIREND
;
3062 if (dirtab_slot
.flag
== DIR_INDEX_FREE
) {
3063 if (loop_count
++ > JFS_IP(ip
)->next_index
) {
3064 jfs_err("jfs_readdir detected "
3066 filp
->f_pos
= DIREND
;
3069 dir_index
= le32_to_cpu(dirtab_slot
.addr2
);
3070 if (dir_index
== -1) {
3071 filp
->f_pos
= DIREND
;
3076 bn
= addressDTS(&dirtab_slot
);
3077 index
= dirtab_slot
.slot
;
3078 DT_GETPAGE(ip
, bn
, mp
, PSIZE
, p
, rc
);
3080 filp
->f_pos
= DIREND
;
3083 if (p
->header
.flag
& BT_INTERNAL
) {
3084 jfs_err("jfs_readdir: bad index table");
3090 if (dir_index
== 0) {
3095 if (filldir(dirent
, ".", 1, 0, ip
->i_ino
,
3103 if (filldir(dirent
, "..", 2, 1, PARENT(ip
), DT_DIR
))
3107 * Find first entry of left-most leaf
3110 filp
->f_pos
= DIREND
;
3114 if ((rc
= dtReadFirst(ip
, &btstack
)))
3117 DT_GETSEARCH(ip
, btstack
.top
, bn
, mp
, p
, index
);
3121 * Legacy filesystem - OS/2 & Linux JFS < 0.3.6
3123 * pn = index = 0: First entry "."
3124 * pn = 0; index = 1: Second entry ".."
3125 * pn > 0: Real entries, pn=1 -> leftmost page
3126 * pn = index = -1: No more entries
3128 dtpos
= filp
->f_pos
;
3130 /* build "." entry */
3132 if (filldir(dirent
, ".", 1, filp
->f_pos
, ip
->i_ino
,
3135 dtoffset
->index
= 1;
3136 filp
->f_pos
= dtpos
;
3139 if (dtoffset
->pn
== 0) {
3140 if (dtoffset
->index
== 1) {
3141 /* build ".." entry */
3143 if (filldir(dirent
, "..", 2, filp
->f_pos
,
3144 PARENT(ip
), DT_DIR
))
3147 jfs_err("jfs_readdir called with "
3151 dtoffset
->index
= 0;
3152 filp
->f_pos
= dtpos
;
3156 filp
->f_pos
= DIREND
;
3160 if ((rc
= dtReadNext(ip
, &filp
->f_pos
, &btstack
))) {
3161 jfs_err("jfs_readdir: unexpected rc = %d "
3162 "from dtReadNext", rc
);
3163 filp
->f_pos
= DIREND
;
3166 /* get start leaf page and index */
3167 DT_GETSEARCH(ip
, btstack
.top
, bn
, mp
, p
, index
);
3169 /* offset beyond directory eof ? */
3171 filp
->f_pos
= DIREND
;
3176 dirent_buf
= __get_free_page(GFP_KERNEL
);
3177 if (dirent_buf
== 0) {
3179 jfs_warn("jfs_readdir: __get_free_page failed!");
3180 filp
->f_pos
= DIREND
;
3185 jfs_dirent
= (struct jfs_dirent
*) dirent_buf
;
3187 overflow
= fix_page
= 0;
3189 stbl
= DT_GETSTBL(p
);
3191 for (i
= index
; i
< p
->header
.nextindex
; i
++) {
3192 d
= (struct ldtentry
*) & p
->slot
[stbl
[i
]];
3194 if (((long) jfs_dirent
+ d
->namlen
+ 1) >
3195 (dirent_buf
+ PAGE_SIZE
)) {
3196 /* DBCS codepages could overrun dirent_buf */
3202 d_namleft
= d
->namlen
;
3203 name_ptr
= jfs_dirent
->name
;
3204 jfs_dirent
->ino
= le32_to_cpu(d
->inumber
);
3207 len
= min(d_namleft
, DTLHDRDATALEN
);
3208 jfs_dirent
->position
= le32_to_cpu(d
->index
);
3210 * d->index should always be valid, but it
3211 * isn't. fsck.jfs doesn't create the
3212 * directory index for the lost+found
3213 * directory. Rather than let it go,
3214 * we can try to fix it.
3216 if ((jfs_dirent
->position
< 2) ||
3217 (jfs_dirent
->position
>=
3218 JFS_IP(ip
)->next_index
)) {
3219 if (!page_fixed
&& !isReadOnly(ip
)) {
3222 * setting overflow and setting
3223 * index to i will cause the
3224 * same page to be processed
3225 * again starting here
3231 jfs_dirent
->position
= unique_pos
++;
3234 jfs_dirent
->position
= dtpos
;
3235 len
= min(d_namleft
, DTLHDRDATALEN_LEGACY
);
3238 /* copy the name of head/only segment */
3239 outlen
= jfs_strfromUCS_le(name_ptr
, d
->name
, len
,
3241 jfs_dirent
->name_len
= outlen
;
3243 /* copy name in the additional segment(s) */
3246 t
= (struct dtslot
*) & p
->slot
[next
];
3250 if (d_namleft
== 0) {
3252 "JFS:Dtree error: ino = "
3253 "%ld, bn=%Ld, index = %d",
3259 len
= min(d_namleft
, DTSLOTDATALEN
);
3260 outlen
= jfs_strfromUCS_le(name_ptr
, t
->name
,
3262 jfs_dirent
->name_len
+= outlen
;
3268 jfs_dirent
= next_jfs_dirent(jfs_dirent
);
3275 /* Point to next leaf page */
3276 if (p
->header
.flag
& BT_ROOT
)
3279 bn
= le64_to_cpu(p
->header
.next
);
3281 /* update offset (pn:index) for new page */
3284 dtoffset
->index
= 0;
3290 /* unpin previous leaf page */
3293 jfs_dirent
= (struct jfs_dirent
*) dirent_buf
;
3294 while (jfs_dirents
--) {
3295 filp
->f_pos
= jfs_dirent
->position
;
3296 if (filldir(dirent
, jfs_dirent
->name
,
3297 jfs_dirent
->name_len
, filp
->f_pos
,
3298 jfs_dirent
->ino
, DT_UNKNOWN
))
3300 jfs_dirent
= next_jfs_dirent(jfs_dirent
);
3304 add_missing_indices(ip
, bn
);
3308 if (!overflow
&& (bn
== 0)) {
3309 filp
->f_pos
= DIREND
;
3313 DT_GETPAGE(ip
, bn
, mp
, PSIZE
, p
, rc
);
3315 free_page(dirent_buf
);
3321 free_page(dirent_buf
);
3330 * function: get the leftmost page of the directory
3332 static int dtReadFirst(struct inode
*ip
, struct btstack
* btstack
)
3336 int psize
= 288; /* initial in-line directory */
3337 struct metapage
*mp
;
3340 struct btframe
*btsp
;
3343 BT_CLR(btstack
); /* reset stack */
3346 * descend leftmost path of the tree
3348 * by convention, root bn = 0.
3351 DT_GETPAGE(ip
, bn
, mp
, psize
, p
, rc
);
3356 * leftmost leaf page
3358 if (p
->header
.flag
& BT_LEAF
) {
3359 /* return leftmost entry */
3360 btsp
= btstack
->top
;
3369 * descend down to leftmost child page
3371 if (BT_STACK_FULL(btstack
)) {
3373 jfs_error(ip
->i_sb
, "dtReadFirst: btstack overrun");
3374 BT_STACK_DUMP(btstack
);
3377 /* push (bn, index) of the parent page/entry */
3378 BT_PUSH(btstack
, bn
, 0);
3380 /* get the leftmost entry */
3381 stbl
= DT_GETSTBL(p
);
3382 xd
= (pxd_t
*) & p
->slot
[stbl
[0]];
3384 /* get the child page block address */
3385 bn
= addressPXD(xd
);
3386 psize
= lengthPXD(xd
) << JFS_SBI(ip
->i_sb
)->l2bsize
;
3388 /* unpin the parent page */
3397 * function: get the page of the specified offset (pn:index)
3399 * return: if (offset > eof), bn = -1;
3401 * note: if index > nextindex of the target leaf page,
3402 * start with 1st entry of next leaf page;
3404 static int dtReadNext(struct inode
*ip
, loff_t
* offset
,
3405 struct btstack
* btstack
)
3412 } *dtoffset
= (struct dtoffset
*) offset
;
3414 struct metapage
*mp
;
3419 struct btframe
*btsp
, *parent
;
3423 * get leftmost leaf page pinned
3425 if ((rc
= dtReadFirst(ip
, btstack
)))
3429 DT_GETSEARCH(ip
, btstack
->top
, bn
, mp
, p
, index
);
3431 /* get the start offset (pn:index) */
3432 pn
= dtoffset
->pn
- 1; /* Now pn = 0 represents leftmost leaf */
3433 index
= dtoffset
->index
;
3435 /* start at leftmost page ? */
3437 /* offset beyond eof ? */
3438 if (index
< p
->header
.nextindex
)
3441 if (p
->header
.flag
& BT_ROOT
) {
3446 /* start with 1st entry of next leaf page */
3448 dtoffset
->index
= index
= 0;
3452 /* start at non-leftmost page: scan parent pages for large pn */
3453 if (p
->header
.flag
& BT_ROOT
) {
3458 /* start after next leaf page ? */
3462 /* get leaf page pn = 1 */
3464 bn
= le64_to_cpu(p
->header
.next
);
3466 /* unpin leaf page */
3469 /* offset beyond eof ? */
3478 * scan last internal page level to get target leaf page
3481 /* unpin leftmost leaf page */
3484 /* get left most parent page */
3485 btsp
= btstack
->top
;
3488 DT_GETPAGE(ip
, bn
, mp
, PSIZE
, p
, rc
);
3492 /* scan parent pages at last internal page level */
3493 while (pn
>= p
->header
.nextindex
) {
3494 pn
-= p
->header
.nextindex
;
3496 /* get next parent page address */
3497 bn
= le64_to_cpu(p
->header
.next
);
3499 /* unpin current parent page */
3502 /* offset beyond eof ? */
3508 /* get next parent page */
3509 DT_GETPAGE(ip
, bn
, mp
, PSIZE
, p
, rc
);
3513 /* update parent page stack frame */
3517 /* get leaf page address */
3518 stbl
= DT_GETSTBL(p
);
3519 xd
= (pxd_t
*) & p
->slot
[stbl
[pn
]];
3520 bn
= addressPXD(xd
);
3522 /* unpin parent page */
3526 * get target leaf page
3529 DT_GETPAGE(ip
, bn
, mp
, PSIZE
, p
, rc
);
3534 * leaf page has been completed:
3535 * start with 1st entry of next leaf page
3537 if (index
>= p
->header
.nextindex
) {
3538 bn
= le64_to_cpu(p
->header
.next
);
3540 /* unpin leaf page */
3543 /* offset beyond eof ? */
3549 /* get next leaf page */
3550 DT_GETPAGE(ip
, bn
, mp
, PSIZE
, p
, rc
);
3554 /* start with 1st entry of next leaf page */
3556 dtoffset
->index
= 0;
3560 /* return target leaf page pinned */
3561 btsp
= btstack
->top
;
3563 btsp
->index
= dtoffset
->index
;
3573 * function: compare search key with an internal entry
3576 * < 0 if k is < record
3577 * = 0 if k is = record
3578 * > 0 if k is > record
3580 static int dtCompare(struct component_name
* key
, /* search key */
3581 dtpage_t
* p
, /* directory page */
3583 { /* entry slot index */
3586 int klen
, namlen
, len
, rc
;
3587 struct idtentry
*ih
;
3591 * force the left-most key on internal pages, at any level of
3592 * the tree, to be less than any search key.
3593 * this obviates having to update the leftmost key on an internal
3594 * page when the user inserts a new key in the tree smaller than
3595 * anything that has been stored.
3597 * (? if/when dtSearch() narrows down to 1st entry (index = 0),
3598 * at any internal page at any level of the tree,
3599 * it descends to child of the entry anyway -
3600 * ? make the entry as min size dummy entry)
3602 * if (e->index == 0 && h->prevpg == P_INVALID && !(h->flags & BT_LEAF))
3609 ih
= (struct idtentry
*) & p
->slot
[si
];
3612 namlen
= ih
->namlen
;
3613 len
= min(namlen
, DTIHDRDATALEN
);
3615 /* compare with head/only segment */
3616 len
= min(klen
, len
);
3617 if ((rc
= UniStrncmp_le(kname
, name
, len
)))
3623 /* compare with additional segment(s) */
3625 while (klen
> 0 && namlen
> 0) {
3626 /* compare with next name segment */
3627 t
= (struct dtslot
*) & p
->slot
[si
];
3628 len
= min(namlen
, DTSLOTDATALEN
);
3629 len
= min(klen
, len
);
3631 if ((rc
= UniStrncmp_le(kname
, name
, len
)))
3640 return (klen
- namlen
);
3649 * function: compare search key with an (leaf/internal) entry
3652 * < 0 if k is < record
3653 * = 0 if k is = record
3654 * > 0 if k is > record
3656 static int ciCompare(struct component_name
* key
, /* search key */
3657 dtpage_t
* p
, /* directory page */
3658 int si
, /* entry slot index */
3663 int klen
, namlen
, len
, rc
;
3664 struct ldtentry
*lh
;
3665 struct idtentry
*ih
;
3670 * force the left-most key on internal pages, at any level of
3671 * the tree, to be less than any search key.
3672 * this obviates having to update the leftmost key on an internal
3673 * page when the user inserts a new key in the tree smaller than
3674 * anything that has been stored.
3676 * (? if/when dtSearch() narrows down to 1st entry (index = 0),
3677 * at any internal page at any level of the tree,
3678 * it descends to child of the entry anyway -
3679 * ? make the entry as min size dummy entry)
3681 * if (e->index == 0 && h->prevpg == P_INVALID && !(h->flags & BT_LEAF))
3691 if (p
->header
.flag
& BT_LEAF
) {
3692 lh
= (struct ldtentry
*) & p
->slot
[si
];
3695 namlen
= lh
->namlen
;
3696 if (flag
& JFS_DIR_INDEX
)
3697 len
= min(namlen
, DTLHDRDATALEN
);
3699 len
= min(namlen
, DTLHDRDATALEN_LEGACY
);
3702 * internal page entry
3705 ih
= (struct idtentry
*) & p
->slot
[si
];
3708 namlen
= ih
->namlen
;
3709 len
= min(namlen
, DTIHDRDATALEN
);
3712 /* compare with head/only segment */
3713 len
= min(klen
, len
);
3714 for (i
= 0; i
< len
; i
++, kname
++, name
++) {
3715 /* only uppercase if case-insensitive support is on */
3716 if ((flag
& JFS_OS2
) == JFS_OS2
)
3717 x
= UniToupper(le16_to_cpu(*name
));
3719 x
= le16_to_cpu(*name
);
3720 if ((rc
= *kname
- x
))
3727 /* compare with additional segment(s) */
3728 while (klen
> 0 && namlen
> 0) {
3729 /* compare with next name segment */
3730 t
= (struct dtslot
*) & p
->slot
[si
];
3731 len
= min(namlen
, DTSLOTDATALEN
);
3732 len
= min(klen
, len
);
3734 for (i
= 0; i
< len
; i
++, kname
++, name
++) {
3735 /* only uppercase if case-insensitive support is on */
3736 if ((flag
& JFS_OS2
) == JFS_OS2
)
3737 x
= UniToupper(le16_to_cpu(*name
));
3739 x
= le16_to_cpu(*name
);
3741 if ((rc
= *kname
- x
))
3750 return (klen
- namlen
);
3755 * ciGetLeafPrefixKey()
3757 * function: compute prefix of suffix compression
3758 * from two adjacent leaf entries
3759 * across page boundary
3761 * return: non-zero on error
3764 static int ciGetLeafPrefixKey(dtpage_t
* lp
, int li
, dtpage_t
* rp
,
3765 int ri
, struct component_name
* key
, int flag
)
3768 wchar_t *pl
, *pr
, *kname
;
3769 struct component_name lkey
;
3770 struct component_name rkey
;
3772 lkey
.name
= kmalloc((JFS_NAME_MAX
+ 1) * sizeof(wchar_t),
3774 if (lkey
.name
== NULL
)
3777 rkey
.name
= kmalloc((JFS_NAME_MAX
+ 1) * sizeof(wchar_t),
3779 if (rkey
.name
== NULL
) {
3784 /* get left and right key */
3785 dtGetKey(lp
, li
, &lkey
, flag
);
3786 lkey
.name
[lkey
.namlen
] = 0;
3788 if ((flag
& JFS_OS2
) == JFS_OS2
)
3791 dtGetKey(rp
, ri
, &rkey
, flag
);
3792 rkey
.name
[rkey
.namlen
] = 0;
3795 if ((flag
& JFS_OS2
) == JFS_OS2
)
3798 /* compute prefix */
3801 namlen
= min(lkey
.namlen
, rkey
.namlen
);
3802 for (pl
= lkey
.name
, pr
= rkey
.name
;
3803 namlen
; pl
++, pr
++, namlen
--, klen
++, kname
++) {
3806 key
->namlen
= klen
+ 1;
3811 /* l->namlen <= r->namlen since l <= r */
3812 if (lkey
.namlen
< rkey
.namlen
) {
3814 key
->namlen
= klen
+ 1;
3815 } else /* l->namelen == r->namelen */
3829 * function: get key of the entry
3831 static void dtGetKey(dtpage_t
* p
, int i
, /* entry index */
3832 struct component_name
* key
, int flag
)
3836 struct ldtentry
*lh
;
3837 struct idtentry
*ih
;
3844 stbl
= DT_GETSTBL(p
);
3846 if (p
->header
.flag
& BT_LEAF
) {
3847 lh
= (struct ldtentry
*) & p
->slot
[si
];
3849 namlen
= lh
->namlen
;
3851 if (flag
& JFS_DIR_INDEX
)
3852 len
= min(namlen
, DTLHDRDATALEN
);
3854 len
= min(namlen
, DTLHDRDATALEN_LEGACY
);
3856 ih
= (struct idtentry
*) & p
->slot
[si
];
3858 namlen
= ih
->namlen
;
3860 len
= min(namlen
, DTIHDRDATALEN
);
3863 key
->namlen
= namlen
;
3867 * move head/only segment
3869 UniStrncpy_from_le(kname
, name
, len
);
3872 * move additional segment(s)
3875 /* get next segment */
3879 len
= min(namlen
, DTSLOTDATALEN
);
3880 UniStrncpy_from_le(kname
, t
->name
, len
);
3890 * function: allocate free slot(s) and
3891 * write a leaf/internal entry
3893 * return: entry slot index
3895 static void dtInsertEntry(dtpage_t
* p
, int index
, struct component_name
* key
,
3896 ddata_t
* data
, struct dt_lock
** dtlock
)
3898 struct dtslot
*h
, *t
;
3899 struct ldtentry
*lh
= NULL
;
3900 struct idtentry
*ih
= NULL
;
3901 int hsi
, fsi
, klen
, len
, nextindex
;
3906 struct dt_lock
*dtlck
= *dtlock
;
3910 struct metapage
*mp
= NULL
;
3915 /* allocate a free slot */
3916 hsi
= fsi
= p
->header
.freelist
;
3918 p
->header
.freelist
= h
->next
;
3919 --p
->header
.freecnt
;
3921 /* open new linelock */
3922 if (dtlck
->index
>= dtlck
->maxcnt
)
3923 dtlck
= (struct dt_lock
*) txLinelock(dtlck
);
3925 lv
= & dtlck
->lv
[dtlck
->index
];
3928 /* write head/only segment */
3929 if (p
->header
.flag
& BT_LEAF
) {
3930 lh
= (struct ldtentry
*) h
;
3932 lh
->inumber
= cpu_to_le32(data
->leaf
.ino
);
3935 if (data
->leaf
.ip
) {
3936 len
= min(klen
, DTLHDRDATALEN
);
3937 if (!(p
->header
.flag
& BT_ROOT
))
3938 bn
= addressPXD(&p
->header
.self
);
3939 lh
->index
= cpu_to_le32(add_index(data
->leaf
.tid
,
3943 len
= min(klen
, DTLHDRDATALEN_LEGACY
);
3945 ih
= (struct idtentry
*) h
;
3951 len
= min(klen
, DTIHDRDATALEN
);
3954 UniStrncpy_to_le(name
, kname
, len
);
3959 /* write additional segment(s) */
3964 fsi
= p
->header
.freelist
;
3966 p
->header
.freelist
= t
->next
;
3967 --p
->header
.freecnt
;
3969 /* is next slot contiguous ? */
3970 if (fsi
!= xsi
+ 1) {
3971 /* close current linelock */
3975 /* open new linelock */
3976 if (dtlck
->index
< dtlck
->maxcnt
)
3979 dtlck
= (struct dt_lock
*) txLinelock(dtlck
);
3980 lv
= & dtlck
->lv
[0];
3988 len
= min(klen
, DTSLOTDATALEN
);
3989 UniStrncpy_to_le(t
->name
, kname
, len
);
3996 /* close current linelock */
4002 /* terminate last/only segment */
4004 /* single segment entry */
4005 if (p
->header
.flag
& BT_LEAF
)
4010 /* multi-segment entry */
4013 /* if insert into middle, shift right succeeding entries in stbl */
4014 stbl
= DT_GETSTBL(p
);
4015 nextindex
= p
->header
.nextindex
;
4016 if (index
< nextindex
) {
4017 memmove(stbl
+ index
+ 1, stbl
+ index
, nextindex
- index
);
4019 if ((p
->header
.flag
& BT_LEAF
) && data
->leaf
.ip
) {
4023 * Need to update slot number for entries that moved
4027 for (n
= index
+ 1; n
<= nextindex
; n
++) {
4028 lh
= (struct ldtentry
*) & (p
->slot
[stbl
[n
]]);
4029 modify_index(data
->leaf
.tid
, data
->leaf
.ip
,
4030 le32_to_cpu(lh
->index
), bn
, n
,
4034 release_metapage(mp
);
4040 /* advance next available entry index of stbl */
4041 ++p
->header
.nextindex
;
4048 * function: move entries from split/left page to new/right page
4050 * nextindex of dst page and freelist/freecnt of both pages
4053 static void dtMoveEntry(dtpage_t
* sp
, int si
, dtpage_t
* dp
,
4054 struct dt_lock
** sdtlock
, struct dt_lock
** ddtlock
,
4057 int ssi
, next
; /* src slot index */
4058 int di
; /* dst entry index */
4059 int dsi
; /* dst slot index */
4060 s8
*sstbl
, *dstbl
; /* sorted entry table */
4062 struct ldtentry
*slh
, *dlh
= NULL
;
4063 struct idtentry
*sih
, *dih
= NULL
;
4064 struct dtslot
*h
, *s
, *d
;
4065 struct dt_lock
*sdtlck
= *sdtlock
, *ddtlck
= *ddtlock
;
4066 struct lv
*slv
, *dlv
;
4070 sstbl
= (s8
*) & sp
->slot
[sp
->header
.stblindex
];
4071 dstbl
= (s8
*) & dp
->slot
[dp
->header
.stblindex
];
4073 dsi
= dp
->header
.freelist
; /* first (whole page) free slot */
4074 sfsi
= sp
->header
.freelist
;
4076 /* linelock destination entry slot */
4077 dlv
= & ddtlck
->lv
[ddtlck
->index
];
4080 /* linelock source entry slot */
4081 slv
= & sdtlck
->lv
[sdtlck
->index
];
4082 slv
->offset
= sstbl
[si
];
4083 xssi
= slv
->offset
- 1;
4089 for (di
= 0; si
< sp
->header
.nextindex
; si
++, di
++) {
4093 /* is next slot contiguous ? */
4094 if (ssi
!= xssi
+ 1) {
4095 /* close current linelock */
4099 /* open new linelock */
4100 if (sdtlck
->index
< sdtlck
->maxcnt
)
4103 sdtlck
= (struct dt_lock
*) txLinelock(sdtlck
);
4104 slv
= & sdtlck
->lv
[0];
4112 * move head/only segment of an entry
4115 h
= d
= &dp
->slot
[dsi
];
4117 /* get src slot and move */
4119 if (sp
->header
.flag
& BT_LEAF
) {
4120 /* get source entry */
4121 slh
= (struct ldtentry
*) s
;
4122 dlh
= (struct ldtentry
*) h
;
4123 snamlen
= slh
->namlen
;
4126 len
= min(snamlen
, DTLHDRDATALEN
);
4127 dlh
->index
= slh
->index
; /* little-endian */
4129 len
= min(snamlen
, DTLHDRDATALEN_LEGACY
);
4131 memcpy(dlh
, slh
, 6 + len
* 2);
4135 /* update dst head/only segment next field */
4139 sih
= (struct idtentry
*) s
;
4140 snamlen
= sih
->namlen
;
4142 len
= min(snamlen
, DTIHDRDATALEN
);
4143 dih
= (struct idtentry
*) h
;
4144 memcpy(dih
, sih
, 10 + len
* 2);
4151 /* free src head/only segment */
4161 * move additional segment(s) of the entry
4164 while ((ssi
= next
) >= 0) {
4165 /* is next slot contiguous ? */
4166 if (ssi
!= xssi
+ 1) {
4167 /* close current linelock */
4171 /* open new linelock */
4172 if (sdtlck
->index
< sdtlck
->maxcnt
)
4178 slv
= & sdtlck
->lv
[0];
4185 /* get next source segment */
4188 /* get next destination free slot */
4191 len
= min(snamlen
, DTSLOTDATALEN
);
4192 UniStrncpy_le(d
->name
, s
->name
, len
);
4201 /* free source segment */
4210 /* terminate dst last/only segment */
4212 /* single segment entry */
4213 if (dp
->header
.flag
& BT_LEAF
)
4218 /* multi-segment entry */
4222 /* close current linelock */
4231 /* update source header */
4232 sp
->header
.freelist
= sfsi
;
4233 sp
->header
.freecnt
+= nd
;
4235 /* update destination header */
4236 dp
->header
.nextindex
= di
;
4238 dp
->header
.freelist
= dsi
;
4239 dp
->header
.freecnt
-= nd
;
4246 * function: free a (leaf/internal) entry
4248 * log freelist header, stbl, and each segment slot of entry
4249 * (even though last/only segment next field is modified,
4250 * physical image logging requires all segment slots of
4251 * the entry logged to avoid applying previous updates
4252 * to the same slots)
4254 static void dtDeleteEntry(dtpage_t
* p
, int fi
, struct dt_lock
** dtlock
)
4256 int fsi
; /* free entry slot index */
4260 struct dt_lock
*dtlck
= *dtlock
;
4264 /* get free entry slot index */
4265 stbl
= DT_GETSTBL(p
);
4268 /* open new linelock */
4269 if (dtlck
->index
>= dtlck
->maxcnt
)
4270 dtlck
= (struct dt_lock
*) txLinelock(dtlck
);
4271 lv
= & dtlck
->lv
[dtlck
->index
];
4275 /* get the head/only segment */
4277 if (p
->header
.flag
& BT_LEAF
)
4278 si
= ((struct ldtentry
*) t
)->next
;
4280 si
= ((struct idtentry
*) t
)->next
;
4287 /* find the last/only segment */
4289 /* is next slot contiguous ? */
4290 if (si
!= xsi
+ 1) {
4291 /* close current linelock */
4295 /* open new linelock */
4296 if (dtlck
->index
< dtlck
->maxcnt
)
4299 dtlck
= (struct dt_lock
*) txLinelock(dtlck
);
4300 lv
= & dtlck
->lv
[0];
4316 /* close current linelock */
4322 /* update freelist */
4323 t
->next
= p
->header
.freelist
;
4324 p
->header
.freelist
= fsi
;
4325 p
->header
.freecnt
+= freecnt
;
4327 /* if delete from middle,
4328 * shift left the succedding entries in the stbl
4330 si
= p
->header
.nextindex
;
4332 memmove(&stbl
[fi
], &stbl
[fi
+ 1], si
- fi
- 1);
4334 p
->header
.nextindex
--;
4341 * function: truncate a (leaf/internal) entry
4343 * log freelist header, stbl, and each segment slot of entry
4344 * (even though last/only segment next field is modified,
4345 * physical image logging requires all segment slots of
4346 * the entry logged to avoid applying previous updates
4347 * to the same slots)
4349 static void dtTruncateEntry(dtpage_t
* p
, int ti
, struct dt_lock
** dtlock
)
4351 int tsi
; /* truncate entry slot index */
4355 struct dt_lock
*dtlck
= *dtlock
;
4359 /* get free entry slot index */
4360 stbl
= DT_GETSTBL(p
);
4363 /* open new linelock */
4364 if (dtlck
->index
>= dtlck
->maxcnt
)
4365 dtlck
= (struct dt_lock
*) txLinelock(dtlck
);
4366 lv
= & dtlck
->lv
[dtlck
->index
];
4370 /* get the head/only segment */
4372 ASSERT(p
->header
.flag
& BT_INTERNAL
);
4373 ((struct idtentry
*) t
)->namlen
= 0;
4374 si
= ((struct idtentry
*) t
)->next
;
4375 ((struct idtentry
*) t
)->next
= -1;
4382 /* find the last/only segment */
4384 /* is next slot contiguous ? */
4385 if (si
!= xsi
+ 1) {
4386 /* close current linelock */
4390 /* open new linelock */
4391 if (dtlck
->index
< dtlck
->maxcnt
)
4394 dtlck
= (struct dt_lock
*) txLinelock(dtlck
);
4395 lv
= & dtlck
->lv
[0];
4411 /* close current linelock */
4417 /* update freelist */
4420 t
->next
= p
->header
.freelist
;
4421 p
->header
.freelist
= fsi
;
4422 p
->header
.freecnt
+= freecnt
;
4427 * dtLinelockFreelist()
4429 static void dtLinelockFreelist(dtpage_t
* p
, /* directory page */
4430 int m
, /* max slot index */
4431 struct dt_lock
** dtlock
)
4433 int fsi
; /* free entry slot index */
4436 struct dt_lock
*dtlck
= *dtlock
;
4440 /* get free entry slot index */
4441 fsi
= p
->header
.freelist
;
4443 /* open new linelock */
4444 if (dtlck
->index
>= dtlck
->maxcnt
)
4445 dtlck
= (struct dt_lock
*) txLinelock(dtlck
);
4446 lv
= & dtlck
->lv
[dtlck
->index
];
4456 /* find the last/only segment */
4457 while (si
< m
&& si
>= 0) {
4458 /* is next slot contiguous ? */
4459 if (si
!= xsi
+ 1) {
4460 /* close current linelock */
4464 /* open new linelock */
4465 if (dtlck
->index
< dtlck
->maxcnt
)
4468 dtlck
= (struct dt_lock
*) txLinelock(dtlck
);
4469 lv
= & dtlck
->lv
[0];
4483 /* close current linelock */
4494 * FUNCTION: Modify the inode number part of a directory entry
4497 * tid - Transaction id
4498 * ip - Inode of parent directory
4499 * key - Name of entry to be modified
4500 * orig_ino - Original inode number expected in entry
4501 * new_ino - New inode number to put into entry
4505 * -ESTALE - If entry found does not match orig_ino passed in
4506 * -ENOENT - If no entry can be found to match key
4507 * 0 - If successfully modified entry
4509 int dtModify(tid_t tid
, struct inode
*ip
,
4510 struct component_name
* key
, ino_t
* orig_ino
, ino_t new_ino
, int flag
)
4514 struct metapage
*mp
;
4517 struct btstack btstack
;
4519 struct dt_lock
*dtlck
;
4522 int entry_si
; /* entry slot index */
4523 struct ldtentry
*entry
;
4526 * search for the entry to modify:
4528 * dtSearch() returns (leaf page pinned, index at which to modify).
4530 if ((rc
= dtSearch(ip
, key
, orig_ino
, &btstack
, flag
)))
4533 /* retrieve search result */
4534 DT_GETSEARCH(ip
, btstack
.top
, bn
, mp
, p
, index
);
4536 BT_MARK_DIRTY(mp
, ip
);
4538 * acquire a transaction lock on the leaf page of named entry
4540 tlck
= txLock(tid
, ip
, mp
, tlckDTREE
| tlckENTRY
);
4541 dtlck
= (struct dt_lock
*) & tlck
->lock
;
4543 /* get slot index of the entry */
4544 stbl
= DT_GETSTBL(p
);
4545 entry_si
= stbl
[index
];
4547 /* linelock entry */
4548 ASSERT(dtlck
->index
== 0);
4549 lv
= & dtlck
->lv
[0];
4550 lv
->offset
= entry_si
;
4554 /* get the head/only segment */
4555 entry
= (struct ldtentry
*) & p
->slot
[entry_si
];
4557 /* substitute the inode number of the entry */
4558 entry
->inumber
= cpu_to_le32(new_ino
);
4560 /* unpin the leaf page */