1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) International Business Machines Corp., 2000-2004
7 * jfs_dtree.c: directory B+-tree manager
9 * B+-tree with variable length key directory:
11 * each directory page is structured as an array of 32-byte
12 * directory entry slots initialized as a freelist
13 * to avoid search/compaction of free space at insertion.
14 * when an entry is inserted, a number of slots are allocated
15 * from the freelist as required to store variable length data
16 * of the entry; when the entry is deleted, slots of the entry
17 * are returned to freelist.
19 * leaf entry stores full name as key and file serial number
20 * (aka inode number) as data.
21 * internal/router entry stores sufffix compressed name
22 * as key and simple extent descriptor as data.
24 * each directory page maintains a sorted entry index table
25 * which stores the start slot index of sorted entries
26 * to allow binary search on the table.
28 * directory starts as a root/leaf page in on-disk inode
30 * when it becomes full, it starts a leaf of a external extent
31 * of length of 1 block. each time the first leaf becomes full,
32 * it is extended rather than split (its size is doubled),
33 * until its length becoms 4 KBytes, from then the extent is split
34 * with new 4 Kbyte extent when it becomes full
35 * to reduce external fragmentation of small directories.
37 * blah, blah, blah, for linear scan of directory in pieces by
41 * case-insensitive directory file system
43 * names are stored in case-sensitive way in leaf entry.
44 * but stored, searched and compared in case-insensitive (uppercase) order
45 * (i.e., both search key and entry key are folded for search/compare):
46 * (note that case-sensitive order is BROKEN in storage, e.g.,
47 * sensitive: Ad, aB, aC, aD -> insensitive: aB, aC, aD, Ad
49 * entries which folds to the same key makes up a equivalent class
50 * whose members are stored as contiguous cluster (may cross page boundary)
51 * but whose order is arbitrary and acts as duplicate, e.g.,
54 * once match is found at leaf, requires scan forward/backward
55 * either for, in case-insensitive search, duplicate
56 * or for, in case-sensitive search, for exact match
58 * router entry must be created/stored in case-insensitive way
60 * (right most key of left page and left most key of right page
61 * are folded, and its suffix compression is propagated as router
63 * (e.g., if split occurs <abc> and <aBd>, <ABD> trather than <aB>
64 * should be made the router key for the split)
66 * case-insensitive search:
70 * case-insensitive search of B-tree:
71 * for internal entry, router key is already folded;
72 * for leaf entry, fold the entry key before comparison.
74 * if (leaf entry case-insensitive match found)
75 * if (next entry satisfies case-insensitive match)
77 * if (prev entry satisfies case-insensitive match)
84 * target directory inode lock is being held on entry/exit
85 * of all main directory service routines.
91 #include <linux/quotaops.h>
92 #include <linux/slab.h>
93 #include "jfs_incore.h"
94 #include "jfs_superblock.h"
95 #include "jfs_filsys.h"
96 #include "jfs_metapage.h"
98 #include "jfs_unicode.h"
99 #include "jfs_debug.h"
101 /* dtree split parameter */
106 struct component_name
*key
;
108 struct pxdlist
*pxdlist
;
111 #define DT_PAGE(IP, MP) BT_PAGE(IP, MP, dtpage_t, i_dtroot)
113 /* get page buffer for specified block address */
114 #define DT_GETPAGE(IP, BN, MP, SIZE, P, RC) \
116 BT_GETPAGE(IP, BN, MP, dtpage_t, SIZE, P, RC, i_dtroot); \
118 if (((P)->header.nextindex > \
119 (((BN) == 0) ? DTROOTMAXSLOT : (P)->header.maxslot)) || \
120 ((BN) && ((P)->header.maxslot > DTPAGEMAXSLOT))) { \
122 jfs_error((IP)->i_sb, \
123 "DT_GETPAGE: dtree page corrupt\n"); \
130 /* for consistency */
131 #define DT_PUTPAGE(MP) BT_PUTPAGE(MP)
133 #define DT_GETSEARCH(IP, LEAF, BN, MP, P, INDEX) \
134 BT_GETSEARCH(IP, LEAF, BN, MP, dtpage_t, P, INDEX, i_dtroot)
139 static int dtSplitUp(tid_t tid
, struct inode
*ip
,
140 struct dtsplit
* split
, struct btstack
* btstack
);
142 static int dtSplitPage(tid_t tid
, struct inode
*ip
, struct dtsplit
* split
,
143 struct metapage
** rmpp
, dtpage_t
** rpp
, pxd_t
* rxdp
);
145 static int dtExtendPage(tid_t tid
, struct inode
*ip
,
146 struct dtsplit
* split
, struct btstack
* btstack
);
148 static int dtSplitRoot(tid_t tid
, struct inode
*ip
,
149 struct dtsplit
* split
, struct metapage
** rmpp
);
151 static int dtDeleteUp(tid_t tid
, struct inode
*ip
, struct metapage
* fmp
,
152 dtpage_t
* fp
, struct btstack
* btstack
);
154 static int dtRelink(tid_t tid
, struct inode
*ip
, dtpage_t
* p
);
156 static int dtReadFirst(struct inode
*ip
, struct btstack
* btstack
);
158 static int dtReadNext(struct inode
*ip
,
159 loff_t
* offset
, struct btstack
* btstack
);
161 static int dtCompare(struct component_name
* key
, dtpage_t
* p
, int si
);
163 static int ciCompare(struct component_name
* key
, dtpage_t
* p
, int si
,
166 static void dtGetKey(dtpage_t
* p
, int i
, struct component_name
* key
,
169 static int ciGetLeafPrefixKey(dtpage_t
* lp
, int li
, dtpage_t
* rp
,
170 int ri
, struct component_name
* key
, int flag
);
172 static void dtInsertEntry(dtpage_t
* p
, int index
, struct component_name
* key
,
173 ddata_t
* data
, struct dt_lock
**);
175 static void dtMoveEntry(dtpage_t
* sp
, int si
, dtpage_t
* dp
,
176 struct dt_lock
** sdtlock
, struct dt_lock
** ddtlock
,
179 static void dtDeleteEntry(dtpage_t
* p
, int fi
, struct dt_lock
** dtlock
);
181 static void dtTruncateEntry(dtpage_t
* p
, int ti
, struct dt_lock
** dtlock
);
183 static void dtLinelockFreelist(dtpage_t
* p
, int m
, struct dt_lock
** dtlock
);
185 #define ciToUpper(c) UniStrupr((c)->name)
190 * Reads a page of a directory's index table.
191 * Having metadata mapped into the directory inode's address space
192 * presents a multitude of problems. We avoid this by mapping to
193 * the absolute address space outside of the *_metapage routines
195 static struct metapage
*read_index_page(struct inode
*inode
, s64 blkno
)
202 rc
= xtLookup(inode
, blkno
, 1, &xflag
, &xaddr
, &xlen
, 1);
203 if (rc
|| (xaddr
== 0))
206 return read_metapage(inode
, xaddr
, PSIZE
, 1);
212 * Same as get_index_page(), but get's a new page without reading
214 static struct metapage
*get_index_page(struct inode
*inode
, s64 blkno
)
221 rc
= xtLookup(inode
, blkno
, 1, &xflag
, &xaddr
, &xlen
, 1);
222 if (rc
|| (xaddr
== 0))
225 return get_metapage(inode
, xaddr
, PSIZE
, 1);
231 * Returns dtree page containing directory table entry for specified
232 * index and pointer to its entry.
234 * mp must be released by caller.
236 static struct dir_table_slot
*find_index(struct inode
*ip
, u32 index
,
237 struct metapage
** mp
, s64
*lblock
)
239 struct jfs_inode_info
*jfs_ip
= JFS_IP(ip
);
243 struct dir_table_slot
*slot
;
244 static int maxWarnings
= 10;
248 jfs_warn("find_entry called with index = %d", index
);
254 if (index
>= jfs_ip
->next_index
) {
255 jfs_warn("find_entry called with index >= next_index");
259 if (jfs_dirtable_inline(ip
)) {
261 * Inline directory table
264 slot
= &jfs_ip
->i_dirtable
[index
- 2];
266 offset
= (index
- 2) * sizeof(struct dir_table_slot
);
267 page_offset
= offset
& (PSIZE
- 1);
268 blkno
= ((offset
+ 1) >> L2PSIZE
) <<
269 JFS_SBI(ip
->i_sb
)->l2nbperpage
;
271 if (*mp
&& (*lblock
!= blkno
)) {
272 release_metapage(*mp
);
277 *mp
= read_index_page(ip
, blkno
);
280 jfs_err("free_index: error reading directory table");
285 (struct dir_table_slot
*) ((char *) (*mp
)->data
+
291 static inline void lock_index(tid_t tid
, struct inode
*ip
, struct metapage
* mp
,
295 struct linelock
*llck
;
298 tlck
= txLock(tid
, ip
, mp
, tlckDATA
);
299 llck
= (struct linelock
*) tlck
->lock
;
301 if (llck
->index
>= llck
->maxcnt
)
302 llck
= txLinelock(llck
);
303 lv
= &llck
->lv
[llck
->index
];
306 * Linelock slot size is twice the size of directory table
307 * slot size. 512 entries per page.
309 lv
->offset
= ((index
- 2) & 511) >> 1;
317 * Adds an entry to the directory index table. This is used to provide
318 * each directory entry with a persistent index in which to resume
319 * directory traversals
321 static u32
add_index(tid_t tid
, struct inode
*ip
, s64 bn
, int slot
)
323 struct super_block
*sb
= ip
->i_sb
;
324 struct jfs_sb_info
*sbi
= JFS_SBI(sb
);
325 struct jfs_inode_info
*jfs_ip
= JFS_IP(ip
);
327 struct dir_table_slot
*dirtab_slot
;
329 struct linelock
*llck
;
337 ASSERT(DO_INDEX(ip
));
339 if (jfs_ip
->next_index
< 2) {
340 jfs_warn("add_index: next_index = %d. Resetting!",
342 jfs_ip
->next_index
= 2;
345 index
= jfs_ip
->next_index
++;
347 if (index
<= MAX_INLINE_DIRTABLE_ENTRY
) {
349 * i_size reflects size of index table, or 8 bytes per entry.
351 ip
->i_size
= (loff_t
) (index
- 1) << 3;
354 * dir table fits inline within inode
356 dirtab_slot
= &jfs_ip
->i_dirtable
[index
-2];
357 dirtab_slot
->flag
= DIR_INDEX_VALID
;
358 dirtab_slot
->slot
= slot
;
359 DTSaddress(dirtab_slot
, bn
);
361 set_cflag(COMMIT_Dirtable
, ip
);
365 if (index
== (MAX_INLINE_DIRTABLE_ENTRY
+ 1)) {
366 struct dir_table_slot temp_table
[12];
369 * It's time to move the inline table to an external
370 * page and begin to build the xtree
372 if (dquot_alloc_block(ip
, sbi
->nbperpage
))
374 if (dbAlloc(ip
, 0, sbi
->nbperpage
, &xaddr
)) {
375 dquot_free_block(ip
, sbi
->nbperpage
);
380 * Save the table, we're going to overwrite it with the
383 memcpy(temp_table
, &jfs_ip
->i_dirtable
, sizeof(temp_table
));
386 * Initialize empty x-tree
391 * Add the first block to the xtree
393 if (xtInsert(tid
, ip
, 0, 0, sbi
->nbperpage
, &xaddr
, 0)) {
394 /* This really shouldn't fail */
395 jfs_warn("add_index: xtInsert failed!");
396 memcpy(&jfs_ip
->i_dirtable
, temp_table
,
397 sizeof (temp_table
));
398 dbFree(ip
, xaddr
, sbi
->nbperpage
);
399 dquot_free_block(ip
, sbi
->nbperpage
);
404 mp
= get_index_page(ip
, 0);
406 jfs_err("add_index: get_metapage failed!");
407 xtTruncate(tid
, ip
, 0, COMMIT_PWMAP
);
408 memcpy(&jfs_ip
->i_dirtable
, temp_table
,
409 sizeof (temp_table
));
412 tlck
= txLock(tid
, ip
, mp
, tlckDATA
);
413 llck
= (struct linelock
*) & tlck
->lock
;
414 ASSERT(llck
->index
== 0);
418 lv
->length
= 6; /* tlckDATA slot size is 16 bytes */
421 memcpy(mp
->data
, temp_table
, sizeof(temp_table
));
423 mark_metapage_dirty(mp
);
424 release_metapage(mp
);
427 * Logging is now directed by xtree tlocks
429 clear_cflag(COMMIT_Dirtable
, ip
);
432 offset
= (index
- 2) * sizeof(struct dir_table_slot
);
433 page_offset
= offset
& (PSIZE
- 1);
434 blkno
= ((offset
+ 1) >> L2PSIZE
) << sbi
->l2nbperpage
;
435 if (page_offset
== 0) {
437 * This will be the beginning of a new page
440 if (xtInsert(tid
, ip
, 0, blkno
, sbi
->nbperpage
, &xaddr
, 0)) {
441 jfs_warn("add_index: xtInsert failed!");
446 if ((mp
= get_index_page(ip
, blkno
)))
447 memset(mp
->data
, 0, PSIZE
); /* Just looks better */
449 xtTruncate(tid
, ip
, offset
, COMMIT_PWMAP
);
451 mp
= read_index_page(ip
, blkno
);
454 jfs_err("add_index: get/read_metapage failed!");
458 lock_index(tid
, ip
, mp
, index
);
461 (struct dir_table_slot
*) ((char *) mp
->data
+ page_offset
);
462 dirtab_slot
->flag
= DIR_INDEX_VALID
;
463 dirtab_slot
->slot
= slot
;
464 DTSaddress(dirtab_slot
, bn
);
466 mark_metapage_dirty(mp
);
467 release_metapage(mp
);
473 jfs_ip
->next_index
--;
481 * Marks an entry to the directory index table as free.
483 static void free_index(tid_t tid
, struct inode
*ip
, u32 index
, u32 next
)
485 struct dir_table_slot
*dirtab_slot
;
487 struct metapage
*mp
= NULL
;
489 dirtab_slot
= find_index(ip
, index
, &mp
, &lblock
);
494 dirtab_slot
->flag
= DIR_INDEX_FREE
;
495 dirtab_slot
->slot
= dirtab_slot
->addr1
= 0;
496 dirtab_slot
->addr2
= cpu_to_le32(next
);
499 lock_index(tid
, ip
, mp
, index
);
500 mark_metapage_dirty(mp
);
501 release_metapage(mp
);
503 set_cflag(COMMIT_Dirtable
, ip
);
509 * Changes an entry in the directory index table
511 static void modify_index(tid_t tid
, struct inode
*ip
, u32 index
, s64 bn
,
512 int slot
, struct metapage
** mp
, s64
*lblock
)
514 struct dir_table_slot
*dirtab_slot
;
516 dirtab_slot
= find_index(ip
, index
, mp
, lblock
);
521 DTSaddress(dirtab_slot
, bn
);
522 dirtab_slot
->slot
= slot
;
525 lock_index(tid
, ip
, *mp
, index
);
526 mark_metapage_dirty(*mp
);
528 set_cflag(COMMIT_Dirtable
, ip
);
534 * reads a directory table slot
536 static int read_index(struct inode
*ip
, u32 index
,
537 struct dir_table_slot
* dirtab_slot
)
540 struct metapage
*mp
= NULL
;
541 struct dir_table_slot
*slot
;
543 slot
= find_index(ip
, index
, &mp
, &lblock
);
548 memcpy(dirtab_slot
, slot
, sizeof(struct dir_table_slot
));
551 release_metapage(mp
);
560 * Search for the entry with specified key
564 * return: 0 - search result on stack, leaf page pinned;
567 int dtSearch(struct inode
*ip
, struct component_name
* key
, ino_t
* data
,
568 struct btstack
* btstack
, int flag
)
571 int cmp
= 1; /* init for empty page */
576 int base
, index
, lim
;
577 struct btframe
*btsp
;
579 int psize
= 288; /* initial in-line directory */
581 struct component_name ciKey
;
582 struct super_block
*sb
= ip
->i_sb
;
584 ciKey
.name
= kmalloc_array(JFS_NAME_MAX
+ 1, sizeof(wchar_t),
592 /* uppercase search key for c-i directory */
593 UniStrcpy(ciKey
.name
, key
->name
);
594 ciKey
.namlen
= key
->namlen
;
596 /* only uppercase if case-insensitive support is on */
597 if ((JFS_SBI(sb
)->mntflag
& JFS_OS2
) == JFS_OS2
) {
600 BT_CLR(btstack
); /* reset stack */
602 /* init level count for max pages to split */
606 * search down tree from root:
608 * between two consecutive entries of <Ki, Pi> and <Kj, Pj> of
609 * internal page, child page Pi contains entry with k, Ki <= K < Kj.
611 * if entry with search key K is not found
612 * internal page search find the entry with largest key Ki
613 * less than K which point to the child page to search;
614 * leaf page search find the entry with smallest key Kj
615 * greater than K so that the returned index is the position of
616 * the entry to be shifted right for insertion of new entry.
617 * for empty tree, search key is greater than any key of the tree.
619 * by convention, root bn = 0.
622 /* get/pin the page to search */
623 DT_GETPAGE(ip
, bn
, mp
, psize
, p
, rc
);
627 /* get sorted entry table of the page */
628 stbl
= DT_GETSTBL(p
);
631 * binary search with search key K on the current page.
633 for (base
= 0, lim
= p
->header
.nextindex
; lim
; lim
>>= 1) {
634 index
= base
+ (lim
>> 1);
636 if (stbl
[index
] < 0) {
641 if (p
->header
.flag
& BT_LEAF
) {
642 /* uppercase leaf name to compare */
644 ciCompare(&ciKey
, p
, stbl
[index
],
645 JFS_SBI(sb
)->mntflag
);
647 /* router key is in uppercase */
649 cmp
= dtCompare(&ciKey
, p
, stbl
[index
]);
657 /* search hit - leaf page:
658 * return the entry found
660 if (p
->header
.flag
& BT_LEAF
) {
661 inumber
= le32_to_cpu(
662 ((struct ldtentry
*) & p
->slot
[stbl
[index
]])->inumber
);
665 * search for JFS_LOOKUP
667 if (flag
== JFS_LOOKUP
) {
674 * search for JFS_CREATE
676 if (flag
== JFS_CREATE
) {
683 * search for JFS_REMOVE or JFS_RENAME
685 if ((flag
== JFS_REMOVE
||
686 flag
== JFS_RENAME
) &&
693 * JFS_REMOVE|JFS_FINDDIR|JFS_RENAME
695 /* save search result */
706 /* search hit - internal page:
707 * descend/search its child page
721 * base is the smallest index with key (Kj) greater than
722 * search key (K) and may be zero or (maxindex + 1) index.
725 * search miss - leaf page
727 * return location of entry (base) where new entry with
728 * search key K is to be inserted.
730 if (p
->header
.flag
& BT_LEAF
) {
732 * search for JFS_LOOKUP, JFS_REMOVE, or JFS_RENAME
734 if (flag
== JFS_LOOKUP
|| flag
== JFS_REMOVE
||
735 flag
== JFS_RENAME
) {
741 * search for JFS_CREATE|JFS_FINDDIR:
756 * search miss - internal page
758 * if base is non-zero, decrement base by one to get the parent
759 * entry of the child page to search.
761 index
= base
? base
- 1 : base
;
764 * go down to child page
767 /* update max. number of pages to split */
768 if (BT_STACK_FULL(btstack
)) {
769 /* Something's corrupted, mark filesystem dirty so
770 * chkdsk will fix it.
772 jfs_error(sb
, "stack overrun!\n");
773 BT_STACK_DUMP(btstack
);
779 /* push (bn, index) of the parent page/entry */
780 BT_PUSH(btstack
, bn
, index
);
782 /* get the child page block number */
783 pxd
= (pxd_t
*) & p
->slot
[stbl
[index
]];
784 bn
= addressPXD(pxd
);
785 psize
= lengthPXD(pxd
) << JFS_SBI(ip
->i_sb
)->l2bsize
;
787 /* unpin the parent page */
807 * function: insert an entry to directory tree
811 * return: 0 - success;
814 int dtInsert(tid_t tid
, struct inode
*ip
,
815 struct component_name
* name
, ino_t
* fsn
, struct btstack
* btstack
)
818 struct metapage
*mp
; /* meta-page buffer */
819 dtpage_t
*p
; /* base B+-tree index page */
822 struct dtsplit split
; /* split information */
824 struct dt_lock
*dtlck
;
830 * retrieve search result
832 * dtSearch() returns (leaf page pinned, index at which to insert).
833 * n.b. dtSearch() may return index of (maxindex + 1) of
836 DT_GETSEARCH(ip
, btstack
->top
, bn
, mp
, p
, index
);
837 if (p
->header
.freelist
== 0)
841 * insert entry for new key
844 if (JFS_IP(ip
)->next_index
== DIREND
) {
848 n
= NDTLEAF(name
->namlen
);
852 n
= NDTLEAF_LEGACY(name
->namlen
);
853 data
.leaf
.ip
= NULL
; /* signifies legacy directory format */
855 data
.leaf
.ino
= *fsn
;
858 * leaf page does not have enough room for new entry:
860 * extend/split the leaf page;
862 * dtSplitUp() will insert the entry and unpin the leaf page.
864 if (n
> p
->header
.freecnt
) {
870 rc
= dtSplitUp(tid
, ip
, &split
, btstack
);
875 * leaf page does have enough room for new entry:
877 * insert the new data entry into the leaf page;
879 BT_MARK_DIRTY(mp
, ip
);
881 * acquire a transaction lock on the leaf page
883 tlck
= txLock(tid
, ip
, mp
, tlckDTREE
| tlckENTRY
);
884 dtlck
= (struct dt_lock
*) & tlck
->lock
;
885 ASSERT(dtlck
->index
== 0);
888 /* linelock header */
893 dtInsertEntry(p
, index
, name
, &data
, &dtlck
);
895 /* linelock stbl of non-root leaf page */
896 if (!(p
->header
.flag
& BT_ROOT
)) {
897 if (dtlck
->index
>= dtlck
->maxcnt
)
898 dtlck
= (struct dt_lock
*) txLinelock(dtlck
);
899 lv
= & dtlck
->lv
[dtlck
->index
];
900 n
= index
>> L2DTSLOTSIZE
;
901 lv
->offset
= p
->header
.stblindex
+ n
;
903 ((p
->header
.nextindex
- 1) >> L2DTSLOTSIZE
) - n
+ 1;
907 /* unpin the leaf page */
917 * function: propagate insertion bottom up;
921 * return: 0 - success;
923 * leaf page unpinned;
925 static int dtSplitUp(tid_t tid
,
926 struct inode
*ip
, struct dtsplit
* split
, struct btstack
* btstack
)
928 struct jfs_sb_info
*sbi
= JFS_SBI(ip
->i_sb
);
930 struct metapage
*smp
;
931 dtpage_t
*sp
; /* split page */
932 struct metapage
*rmp
;
933 dtpage_t
*rp
; /* new right page split from sp */
934 pxd_t rpxd
; /* new right page extent descriptor */
935 struct metapage
*lmp
;
936 dtpage_t
*lp
; /* left child page */
937 int skip
; /* index of entry of insertion */
938 struct btframe
*parent
; /* parent page entry on traverse stack */
941 struct pxdlist pxdlist
;
943 struct component_name key
= { 0, NULL
};
944 ddata_t
*data
= split
->data
;
946 struct dt_lock
*dtlck
;
949 int quota_allocation
= 0;
953 sp
= DT_PAGE(ip
, smp
);
955 key
.name
= kmalloc_array(JFS_NAME_MAX
+ 2, sizeof(wchar_t), GFP_NOFS
);
965 * The split routines insert the new entry, and
966 * acquire txLock as appropriate.
969 * split root leaf page:
971 if (sp
->header
.flag
& BT_ROOT
) {
973 * allocate a single extent child page
976 n
= sbi
->bsize
>> L2DTSLOTSIZE
;
977 n
-= (n
+ 31) >> L2DTSLOTSIZE
; /* stbl size */
978 n
-= DTROOTMAXSLOT
- sp
->header
.freecnt
; /* header + entries */
979 if (n
<= split
->nslot
)
981 if ((rc
= dbAlloc(ip
, 0, (s64
) xlen
, &xaddr
))) {
988 pxd
= &pxdlist
.pxd
[0];
989 PXDaddress(pxd
, xaddr
);
990 PXDlength(pxd
, xlen
);
991 split
->pxdlist
= &pxdlist
;
992 rc
= dtSplitRoot(tid
, ip
, split
, &rmp
);
995 dbFree(ip
, xaddr
, xlen
);
1002 ip
->i_size
= xlen
<< sbi
->l2bsize
;
1008 * extend first leaf page
1010 * extend the 1st extent if less than buffer page size
1011 * (dtExtendPage() reurns leaf page unpinned)
1013 pxd
= &sp
->header
.self
;
1014 xlen
= lengthPXD(pxd
);
1015 xsize
= xlen
<< sbi
->l2bsize
;
1016 if (xsize
< PSIZE
) {
1017 xaddr
= addressPXD(pxd
);
1018 n
= xsize
>> L2DTSLOTSIZE
;
1019 n
-= (n
+ 31) >> L2DTSLOTSIZE
; /* stbl size */
1020 if ((n
+ sp
->header
.freecnt
) <= split
->nslot
)
1021 n
= xlen
+ (xlen
<< 1);
1025 /* Allocate blocks to quota. */
1026 rc
= dquot_alloc_block(ip
, n
);
1029 quota_allocation
+= n
;
1031 if ((rc
= dbReAlloc(sbi
->ipbmap
, xaddr
, (s64
) xlen
,
1035 pxdlist
.maxnpxd
= 1;
1037 pxd
= &pxdlist
.pxd
[0];
1038 PXDaddress(pxd
, nxaddr
);
1039 PXDlength(pxd
, xlen
+ n
);
1040 split
->pxdlist
= &pxdlist
;
1041 if ((rc
= dtExtendPage(tid
, ip
, split
, btstack
))) {
1042 nxaddr
= addressPXD(pxd
);
1043 if (xaddr
!= nxaddr
) {
1044 /* free relocated extent */
1045 xlen
= lengthPXD(pxd
);
1046 dbFree(ip
, nxaddr
, (s64
) xlen
);
1048 /* free extended delta */
1049 xlen
= lengthPXD(pxd
) - n
;
1050 xaddr
= addressPXD(pxd
) + xlen
;
1051 dbFree(ip
, xaddr
, (s64
) n
);
1053 } else if (!DO_INDEX(ip
))
1054 ip
->i_size
= lengthPXD(pxd
) << sbi
->l2bsize
;
1063 * split leaf page <sp> into <sp> and a new right page <rp>.
1065 * return <rp> pinned and its extent descriptor <rpxd>
1068 * allocate new directory page extent and
1069 * new index page(s) to cover page split(s)
1071 * allocation hint: ?
1073 n
= btstack
->nsplit
;
1074 pxdlist
.maxnpxd
= pxdlist
.npxd
= 0;
1075 xlen
= sbi
->nbperpage
;
1076 for (pxd
= pxdlist
.pxd
; n
> 0; n
--, pxd
++) {
1077 if ((rc
= dbAlloc(ip
, 0, (s64
) xlen
, &xaddr
)) == 0) {
1078 PXDaddress(pxd
, xaddr
);
1079 PXDlength(pxd
, xlen
);
1086 /* undo allocation */
1090 split
->pxdlist
= &pxdlist
;
1091 if ((rc
= dtSplitPage(tid
, ip
, split
, &rmp
, &rp
, &rpxd
))) {
1094 /* undo allocation */
1099 ip
->i_size
+= PSIZE
;
1102 * propagate up the router entry for the leaf page just split
1104 * insert a router entry for the new page into the parent page,
1105 * propagate the insert/split up the tree by walking back the stack
1106 * of (bn of parent page, index of child page entry in parent page)
1107 * that were traversed during the search for the page that split.
1109 * the propagation of insert/split up the tree stops if the root
1110 * splits or the page inserted into doesn't have to split to hold
1113 * the parent entry for the split page remains the same, and
1114 * a new entry is inserted at its right with the first key and
1115 * block number of the new right page.
1117 * There are a maximum of 4 pages pinned at any time:
1118 * two children, left parent and right parent (when the parent splits).
1119 * keep the child pages pinned while working on the parent.
1120 * make sure that all pins are released at exit.
1122 while ((parent
= BT_POP(btstack
)) != NULL
) {
1123 /* parent page specified by stack frame <parent> */
1125 /* keep current child pages (<lp>, <rp>) pinned */
1130 * insert router entry in parent for new right child page <rp>
1132 /* get the parent page <sp> */
1133 DT_GETPAGE(ip
, parent
->bn
, smp
, PSIZE
, sp
, rc
);
1141 * The new key entry goes ONE AFTER the index of parent entry,
1142 * because the split was to the right.
1144 skip
= parent
->index
+ 1;
1147 * compute the key for the router entry
1149 * key suffix compression:
1150 * for internal pages that have leaf pages as children,
1151 * retain only what's needed to distinguish between
1152 * the new entry and the entry on the page to its left.
1153 * If the keys compare equal, retain the entire key.
1155 * note that compression is performed only at computing
1156 * router key at the lowest internal level.
1157 * further compression of the key between pairs of higher
1158 * level internal pages loses too much information and
1159 * the search may fail.
1160 * (e.g., two adjacent leaf pages of {a, ..., x} {xx, ...,}
1161 * results in two adjacent parent entries (a)(xx).
1162 * if split occurs between these two entries, and
1163 * if compression is applied, the router key of parent entry
1164 * of right page (x) will divert search for x into right
1165 * subtree and miss x in the left subtree.)
1167 * the entire key must be retained for the next-to-leftmost
1168 * internal key at any level of the tree, or search may fail
1171 switch (rp
->header
.flag
& BT_TYPE
) {
1174 * compute the length of prefix for suffix compression
1175 * between last entry of left page and first entry
1178 if ((sp
->header
.flag
& BT_ROOT
&& skip
> 1) ||
1179 sp
->header
.prev
!= 0 || skip
> 1) {
1180 /* compute uppercase router prefix key */
1181 rc
= ciGetLeafPrefixKey(lp
,
1182 lp
->header
.nextindex
-1,
1192 /* next to leftmost entry of
1193 lowest internal level */
1195 /* compute uppercase router key */
1196 dtGetKey(rp
, 0, &key
, sbi
->mntflag
);
1197 key
.name
[key
.namlen
] = 0;
1199 if ((sbi
->mntflag
& JFS_OS2
) == JFS_OS2
)
1203 n
= NDTINTERNAL(key
.namlen
);
1207 dtGetKey(rp
, 0, &key
, sbi
->mntflag
);
1208 n
= NDTINTERNAL(key
.namlen
);
1212 jfs_err("dtSplitUp(): UFO!");
1216 /* unpin left child page */
1220 * compute the data for the router entry
1222 data
->xd
= rpxd
; /* child page xd */
1225 * parent page is full - split the parent page
1227 if (n
> sp
->header
.freecnt
) {
1228 /* init for parent page split */
1230 split
->index
= skip
; /* index at insert */
1233 /* split->data = data; */
1235 /* unpin right child page */
1238 /* The split routines insert the new entry,
1239 * acquire txLock as appropriate.
1240 * return <rp> pinned and its block number <rbn>.
1242 rc
= (sp
->header
.flag
& BT_ROOT
) ?
1243 dtSplitRoot(tid
, ip
, split
, &rmp
) :
1244 dtSplitPage(tid
, ip
, split
, &rmp
, &rp
, &rpxd
);
1250 /* smp and rmp are pinned */
1253 * parent page is not full - insert router entry in parent page
1256 BT_MARK_DIRTY(smp
, ip
);
1258 * acquire a transaction lock on the parent page
1260 tlck
= txLock(tid
, ip
, smp
, tlckDTREE
| tlckENTRY
);
1261 dtlck
= (struct dt_lock
*) & tlck
->lock
;
1262 ASSERT(dtlck
->index
== 0);
1263 lv
= & dtlck
->lv
[0];
1265 /* linelock header */
1270 /* linelock stbl of non-root parent page */
1271 if (!(sp
->header
.flag
& BT_ROOT
)) {
1273 n
= skip
>> L2DTSLOTSIZE
;
1274 lv
->offset
= sp
->header
.stblindex
+ n
;
1276 ((sp
->header
.nextindex
-
1277 1) >> L2DTSLOTSIZE
) - n
+ 1;
1281 dtInsertEntry(sp
, skip
, &key
, data
, &dtlck
);
1283 /* exit propagate up */
1288 /* unpin current split and its right page */
1293 * free remaining extents allocated for split
1297 pxd
= &pxdlist
.pxd
[n
];
1298 for (; n
< pxdlist
.maxnpxd
; n
++, pxd
++)
1299 dbFree(ip
, addressPXD(pxd
), (s64
) lengthPXD(pxd
));
1304 /* Rollback quota allocation */
1305 if (rc
&& quota_allocation
)
1306 dquot_free_block(ip
, quota_allocation
);
1317 * function: Split a non-root page of a btree.
1321 * return: 0 - success;
1323 * return split and new page pinned;
1325 static int dtSplitPage(tid_t tid
, struct inode
*ip
, struct dtsplit
* split
,
1326 struct metapage
** rmpp
, dtpage_t
** rpp
, pxd_t
* rpxdp
)
1329 struct metapage
*smp
;
1331 struct metapage
*rmp
;
1332 dtpage_t
*rp
; /* new right page allocated */
1333 s64 rbn
; /* new right page block number */
1334 struct metapage
*mp
;
1337 struct pxdlist
*pxdlist
;
1339 int skip
, nextindex
, half
, left
, nxt
, off
, si
;
1340 struct ldtentry
*ldtentry
;
1341 struct idtentry
*idtentry
;
1346 struct dt_lock
*sdtlck
, *rdtlck
;
1348 struct dt_lock
*dtlck
;
1349 struct lv
*slv
, *rlv
, *lv
;
1351 /* get split page */
1353 sp
= DT_PAGE(ip
, smp
);
1356 * allocate the new right page for the split
1358 pxdlist
= split
->pxdlist
;
1359 pxd
= &pxdlist
->pxd
[pxdlist
->npxd
];
1361 rbn
= addressPXD(pxd
);
1362 rmp
= get_metapage(ip
, rbn
, PSIZE
, 1);
1366 /* Allocate blocks to quota. */
1367 rc
= dquot_alloc_block(ip
, lengthPXD(pxd
));
1369 release_metapage(rmp
);
1373 jfs_info("dtSplitPage: ip:0x%p smp:0x%p rmp:0x%p", ip
, smp
, rmp
);
1375 BT_MARK_DIRTY(rmp
, ip
);
1377 * acquire a transaction lock on the new right page
1379 tlck
= txLock(tid
, ip
, rmp
, tlckDTREE
| tlckNEW
);
1380 rdtlck
= (struct dt_lock
*) & tlck
->lock
;
1382 rp
= (dtpage_t
*) rmp
->data
;
1384 rp
->header
.self
= *pxd
;
1386 BT_MARK_DIRTY(smp
, ip
);
1388 * acquire a transaction lock on the split page
1392 tlck
= txLock(tid
, ip
, smp
, tlckDTREE
| tlckENTRY
);
1393 sdtlck
= (struct dt_lock
*) & tlck
->lock
;
1395 /* linelock header of split page */
1396 ASSERT(sdtlck
->index
== 0);
1397 slv
= & sdtlck
->lv
[0];
1403 * initialize/update sibling pointers between sp and rp
1405 nextbn
= le64_to_cpu(sp
->header
.next
);
1406 rp
->header
.next
= cpu_to_le64(nextbn
);
1407 rp
->header
.prev
= cpu_to_le64(addressPXD(&sp
->header
.self
));
1408 sp
->header
.next
= cpu_to_le64(rbn
);
1411 * initialize new right page
1413 rp
->header
.flag
= sp
->header
.flag
;
1415 /* compute sorted entry table at start of extent data area */
1416 rp
->header
.nextindex
= 0;
1417 rp
->header
.stblindex
= 1;
1419 n
= PSIZE
>> L2DTSLOTSIZE
;
1420 rp
->header
.maxslot
= n
;
1421 stblsize
= (n
+ 31) >> L2DTSLOTSIZE
; /* in unit of slot */
1424 fsi
= rp
->header
.stblindex
+ stblsize
;
1425 rp
->header
.freelist
= fsi
;
1426 rp
->header
.freecnt
= rp
->header
.maxslot
- fsi
;
1429 * sequential append at tail: append without split
1431 * If splitting the last page on a level because of appending
1432 * a entry to it (skip is maxentry), it's likely that the access is
1433 * sequential. Adding an empty page on the side of the level is less
1434 * work and can push the fill factor much higher than normal.
1435 * If we're wrong it's no big deal, we'll just do the split the right
1437 * (It may look like it's equally easy to do a similar hack for
1438 * reverse sorted data, that is, split the tree left,
1439 * but it's not. Be my guest.)
1441 if (nextbn
== 0 && split
->index
== sp
->header
.nextindex
) {
1442 /* linelock header + stbl (first slot) of new page */
1443 rlv
= & rdtlck
->lv
[rdtlck
->index
];
1449 * initialize freelist of new right page
1452 for (fsi
++; fsi
< rp
->header
.maxslot
; f
++, fsi
++)
1456 /* insert entry at the first entry of the new right page */
1457 dtInsertEntry(rp
, 0, split
->key
, split
->data
, &rdtlck
);
1463 * non-sequential insert (at possibly middle page)
1467 * update prev pointer of previous right sibling page;
1470 DT_GETPAGE(ip
, nextbn
, mp
, PSIZE
, p
, rc
);
1472 discard_metapage(rmp
);
1476 BT_MARK_DIRTY(mp
, ip
);
1478 * acquire a transaction lock on the next page
1480 tlck
= txLock(tid
, ip
, mp
, tlckDTREE
| tlckRELINK
);
1481 jfs_info("dtSplitPage: tlck = 0x%p, ip = 0x%p, mp=0x%p",
1483 dtlck
= (struct dt_lock
*) & tlck
->lock
;
1485 /* linelock header of previous right sibling page */
1486 lv
= & dtlck
->lv
[dtlck
->index
];
1491 p
->header
.prev
= cpu_to_le64(rbn
);
1497 * split the data between the split and right pages.
1499 skip
= split
->index
;
1500 half
= (PSIZE
>> L2DTSLOTSIZE
) >> 1; /* swag */
1504 * compute fill factor for split pages
1506 * <nxt> traces the next entry to move to rp
1507 * <off> traces the next entry to stay in sp
1509 stbl
= (u8
*) & sp
->slot
[sp
->header
.stblindex
];
1510 nextindex
= sp
->header
.nextindex
;
1511 for (nxt
= off
= 0; nxt
< nextindex
; ++off
) {
1513 /* check for fill factor with new entry size */
1517 switch (sp
->header
.flag
& BT_TYPE
) {
1519 ldtentry
= (struct ldtentry
*) & sp
->slot
[si
];
1521 n
= NDTLEAF(ldtentry
->namlen
);
1523 n
= NDTLEAF_LEGACY(ldtentry
->
1528 idtentry
= (struct idtentry
*) & sp
->slot
[si
];
1529 n
= NDTINTERNAL(idtentry
->namlen
);
1536 ++nxt
; /* advance to next entry to move in sp */
1544 /* <nxt> poins to the 1st entry to move */
1547 * move entries to right page
1549 * dtMoveEntry() initializes rp and reserves entry for insertion
1551 * split page moved out entries are linelocked;
1552 * new/right page moved in entries are linelocked;
1554 /* linelock header + stbl of new right page */
1555 rlv
= & rdtlck
->lv
[rdtlck
->index
];
1560 dtMoveEntry(sp
, nxt
, rp
, &sdtlck
, &rdtlck
, DO_INDEX(ip
));
1562 sp
->header
.nextindex
= nxt
;
1565 * finalize freelist of new right page
1567 fsi
= rp
->header
.freelist
;
1569 for (fsi
++; fsi
< rp
->header
.maxslot
; f
++, fsi
++)
1574 * Update directory index table for entries now in right page
1576 if ((rp
->header
.flag
& BT_LEAF
) && DO_INDEX(ip
)) {
1580 stbl
= DT_GETSTBL(rp
);
1581 for (n
= 0; n
< rp
->header
.nextindex
; n
++) {
1582 ldtentry
= (struct ldtentry
*) & rp
->slot
[stbl
[n
]];
1583 modify_index(tid
, ip
, le32_to_cpu(ldtentry
->index
),
1584 rbn
, n
, &mp
, &lblock
);
1587 release_metapage(mp
);
1591 * the skipped index was on the left page,
1594 /* insert the new entry in the split page */
1595 dtInsertEntry(sp
, skip
, split
->key
, split
->data
, &sdtlck
);
1597 /* linelock stbl of split page */
1598 if (sdtlck
->index
>= sdtlck
->maxcnt
)
1599 sdtlck
= (struct dt_lock
*) txLinelock(sdtlck
);
1600 slv
= & sdtlck
->lv
[sdtlck
->index
];
1601 n
= skip
>> L2DTSLOTSIZE
;
1602 slv
->offset
= sp
->header
.stblindex
+ n
;
1604 ((sp
->header
.nextindex
- 1) >> L2DTSLOTSIZE
) - n
+ 1;
1608 * the skipped index was on the right page,
1611 /* adjust the skip index to reflect the new position */
1614 /* insert the new entry in the right page */
1615 dtInsertEntry(rp
, skip
, split
->key
, split
->data
, &rdtlck
);
1629 * function: extend 1st/only directory leaf page
1633 * return: 0 - success;
1635 * return extended page pinned;
1637 static int dtExtendPage(tid_t tid
,
1638 struct inode
*ip
, struct dtsplit
* split
, struct btstack
* btstack
)
1640 struct super_block
*sb
= ip
->i_sb
;
1642 struct metapage
*smp
, *pmp
, *mp
;
1644 struct pxdlist
*pxdlist
;
1647 int newstblindex
, newstblsize
;
1648 int oldstblindex
, oldstblsize
;
1651 struct btframe
*parent
;
1653 struct dt_lock
*dtlck
;
1656 struct pxd_lock
*pxdlock
;
1659 struct ldtentry
*ldtentry
;
1662 /* get page to extend */
1664 sp
= DT_PAGE(ip
, smp
);
1666 /* get parent/root page */
1667 parent
= BT_POP(btstack
);
1668 DT_GETPAGE(ip
, parent
->bn
, pmp
, PSIZE
, pp
, rc
);
1675 pxdlist
= split
->pxdlist
;
1676 pxd
= &pxdlist
->pxd
[pxdlist
->npxd
];
1679 xaddr
= addressPXD(pxd
);
1680 tpxd
= &sp
->header
.self
;
1681 txaddr
= addressPXD(tpxd
);
1682 /* in-place extension */
1683 if (xaddr
== txaddr
) {
1690 /* save moved extent descriptor for later free */
1691 tlck
= txMaplock(tid
, ip
, tlckDTREE
| tlckRELOCATE
);
1692 pxdlock
= (struct pxd_lock
*) & tlck
->lock
;
1693 pxdlock
->flag
= mlckFREEPXD
;
1694 pxdlock
->pxd
= sp
->header
.self
;
1698 * Update directory index table to reflect new page address
1704 stbl
= DT_GETSTBL(sp
);
1705 for (n
= 0; n
< sp
->header
.nextindex
; n
++) {
1707 (struct ldtentry
*) & sp
->slot
[stbl
[n
]];
1708 modify_index(tid
, ip
,
1709 le32_to_cpu(ldtentry
->index
),
1710 xaddr
, n
, &mp
, &lblock
);
1713 release_metapage(mp
);
1720 sp
->header
.self
= *pxd
;
1722 jfs_info("dtExtendPage: ip:0x%p smp:0x%p sp:0x%p", ip
, smp
, sp
);
1724 BT_MARK_DIRTY(smp
, ip
);
1726 * acquire a transaction lock on the extended/leaf page
1728 tlck
= txLock(tid
, ip
, smp
, tlckDTREE
| type
);
1729 dtlck
= (struct dt_lock
*) & tlck
->lock
;
1730 lv
= & dtlck
->lv
[0];
1732 /* update buffer extent descriptor of extended page */
1733 xlen
= lengthPXD(pxd
);
1734 xsize
= xlen
<< JFS_SBI(sb
)->l2bsize
;
1737 * copy old stbl to new stbl at start of extended area
1739 oldstblindex
= sp
->header
.stblindex
;
1740 oldstblsize
= (sp
->header
.maxslot
+ 31) >> L2DTSLOTSIZE
;
1741 newstblindex
= sp
->header
.maxslot
;
1742 n
= xsize
>> L2DTSLOTSIZE
;
1743 newstblsize
= (n
+ 31) >> L2DTSLOTSIZE
;
1744 memcpy(&sp
->slot
[newstblindex
], &sp
->slot
[oldstblindex
],
1745 sp
->header
.nextindex
);
1748 * in-line extension: linelock old area of extended page
1750 if (type
== tlckEXTEND
) {
1751 /* linelock header */
1757 /* linelock new stbl of extended page */
1758 lv
->offset
= newstblindex
;
1759 lv
->length
= newstblsize
;
1762 * relocation: linelock whole relocated area
1766 lv
->length
= sp
->header
.maxslot
+ newstblsize
;
1771 sp
->header
.maxslot
= n
;
1772 sp
->header
.stblindex
= newstblindex
;
1773 /* sp->header.nextindex remains the same */
1776 * add old stbl region at head of freelist
1780 last
= sp
->header
.freelist
;
1781 for (n
= 0; n
< oldstblsize
; n
++, fsi
++, f
++) {
1785 sp
->header
.freelist
= last
;
1786 sp
->header
.freecnt
+= oldstblsize
;
1789 * append free region of newly extended area at tail of freelist
1791 /* init free region of newly extended area */
1792 fsi
= n
= newstblindex
+ newstblsize
;
1794 for (fsi
++; fsi
< sp
->header
.maxslot
; f
++, fsi
++)
1798 /* append new free region at tail of old freelist */
1799 fsi
= sp
->header
.freelist
;
1801 sp
->header
.freelist
= n
;
1806 } while (fsi
!= -1);
1811 sp
->header
.freecnt
+= sp
->header
.maxslot
- n
;
1814 * insert the new entry
1816 dtInsertEntry(sp
, split
->index
, split
->key
, split
->data
, &dtlck
);
1818 BT_MARK_DIRTY(pmp
, ip
);
1820 * linelock any freeslots residing in old extent
1822 if (type
== tlckEXTEND
) {
1823 n
= sp
->header
.maxslot
>> 2;
1824 if (sp
->header
.freelist
< n
)
1825 dtLinelockFreelist(sp
, n
, &dtlck
);
1829 * update parent entry on the parent/root page
1832 * acquire a transaction lock on the parent/root page
1834 tlck
= txLock(tid
, ip
, pmp
, tlckDTREE
| tlckENTRY
);
1835 dtlck
= (struct dt_lock
*) & tlck
->lock
;
1836 lv
= & dtlck
->lv
[dtlck
->index
];
1838 /* linelock parent entry - 1st slot */
1843 /* update the parent pxd for page extension */
1844 tpxd
= (pxd_t
*) & pp
->slot
[1];
1856 * split the full root page into
1857 * original/root/split page and new right page
1858 * i.e., root remains fixed in tree anchor (inode) and
1859 * the root is copied to a single new right child page
1860 * since root page << non-root page, and
1861 * the split root page contains a single entry for the
1862 * new right child page.
1866 * return: 0 - success;
1868 * return new page pinned;
1870 static int dtSplitRoot(tid_t tid
,
1871 struct inode
*ip
, struct dtsplit
* split
, struct metapage
** rmpp
)
1873 struct super_block
*sb
= ip
->i_sb
;
1874 struct metapage
*smp
;
1876 struct metapage
*rmp
;
1883 int fsi
, stblsize
, n
;
1886 struct pxdlist
*pxdlist
;
1888 struct dt_lock
*dtlck
;
1893 /* get split root page */
1895 sp
= &JFS_IP(ip
)->i_dtroot
;
1898 * allocate/initialize a single (right) child page
1900 * N.B. at first split, a one (or two) block to fit new entry
1901 * is allocated; at subsequent split, a full page is allocated;
1903 pxdlist
= split
->pxdlist
;
1904 pxd
= &pxdlist
->pxd
[pxdlist
->npxd
];
1906 rbn
= addressPXD(pxd
);
1907 xlen
= lengthPXD(pxd
);
1908 xsize
= xlen
<< JFS_SBI(sb
)->l2bsize
;
1909 rmp
= get_metapage(ip
, rbn
, xsize
, 1);
1915 /* Allocate blocks to quota. */
1916 rc
= dquot_alloc_block(ip
, lengthPXD(pxd
));
1918 release_metapage(rmp
);
1922 BT_MARK_DIRTY(rmp
, ip
);
1924 * acquire a transaction lock on the new right page
1926 tlck
= txLock(tid
, ip
, rmp
, tlckDTREE
| tlckNEW
);
1927 dtlck
= (struct dt_lock
*) & tlck
->lock
;
1930 (sp
->header
.flag
& BT_LEAF
) ? BT_LEAF
: BT_INTERNAL
;
1931 rp
->header
.self
= *pxd
;
1933 /* initialize sibling pointers */
1934 rp
->header
.next
= 0;
1935 rp
->header
.prev
= 0;
1938 * move in-line root page into new right page extent
1940 /* linelock header + copied entries + new stbl (1st slot) in new page */
1941 ASSERT(dtlck
->index
== 0);
1942 lv
= & dtlck
->lv
[0];
1944 lv
->length
= 10; /* 1 + 8 + 1 */
1947 n
= xsize
>> L2DTSLOTSIZE
;
1948 rp
->header
.maxslot
= n
;
1949 stblsize
= (n
+ 31) >> L2DTSLOTSIZE
;
1951 /* copy old stbl to new stbl at start of extended area */
1952 rp
->header
.stblindex
= DTROOTMAXSLOT
;
1953 stbl
= (s8
*) & rp
->slot
[DTROOTMAXSLOT
];
1954 memcpy(stbl
, sp
->header
.stbl
, sp
->header
.nextindex
);
1955 rp
->header
.nextindex
= sp
->header
.nextindex
;
1957 /* copy old data area to start of new data area */
1958 memcpy(&rp
->slot
[1], &sp
->slot
[1], IDATASIZE
);
1961 * append free region of newly extended area at tail of freelist
1963 /* init free region of newly extended area */
1964 fsi
= n
= DTROOTMAXSLOT
+ stblsize
;
1966 for (fsi
++; fsi
< rp
->header
.maxslot
; f
++, fsi
++)
1970 /* append new free region at tail of old freelist */
1971 fsi
= sp
->header
.freelist
;
1973 rp
->header
.freelist
= n
;
1975 rp
->header
.freelist
= fsi
;
1985 rp
->header
.freecnt
= sp
->header
.freecnt
+ rp
->header
.maxslot
- n
;
1988 * Update directory index table for entries now in right page
1990 if ((rp
->header
.flag
& BT_LEAF
) && DO_INDEX(ip
)) {
1992 struct metapage
*mp
= NULL
;
1993 struct ldtentry
*ldtentry
;
1995 stbl
= DT_GETSTBL(rp
);
1996 for (n
= 0; n
< rp
->header
.nextindex
; n
++) {
1997 ldtentry
= (struct ldtentry
*) & rp
->slot
[stbl
[n
]];
1998 modify_index(tid
, ip
, le32_to_cpu(ldtentry
->index
),
1999 rbn
, n
, &mp
, &lblock
);
2002 release_metapage(mp
);
2005 * insert the new entry into the new right/child page
2006 * (skip index in the new right page will not change)
2008 dtInsertEntry(rp
, split
->index
, split
->key
, split
->data
, &dtlck
);
2011 * reset parent/root page
2013 * set the 1st entry offset to 0, which force the left-most key
2014 * at any level of the tree to be less than any search key.
2016 * The btree comparison code guarantees that the left-most key on any
2017 * level of the tree is never used, so it doesn't need to be filled in.
2019 BT_MARK_DIRTY(smp
, ip
);
2021 * acquire a transaction lock on the root page (in-memory inode)
2023 tlck
= txLock(tid
, ip
, smp
, tlckDTREE
| tlckNEW
| tlckBTROOT
);
2024 dtlck
= (struct dt_lock
*) & tlck
->lock
;
2027 ASSERT(dtlck
->index
== 0);
2028 lv
= & dtlck
->lv
[0];
2030 lv
->length
= DTROOTMAXSLOT
;
2033 /* update page header of root */
2034 if (sp
->header
.flag
& BT_LEAF
) {
2035 sp
->header
.flag
&= ~BT_LEAF
;
2036 sp
->header
.flag
|= BT_INTERNAL
;
2039 /* init the first entry */
2040 s
= (struct idtentry
*) & sp
->slot
[DTENTRYSTART
];
2046 stbl
= sp
->header
.stbl
;
2047 stbl
[0] = DTENTRYSTART
;
2048 sp
->header
.nextindex
= 1;
2051 fsi
= DTENTRYSTART
+ 1;
2054 /* init free region of remaining area */
2055 for (fsi
++; fsi
< DTROOTMAXSLOT
; f
++, fsi
++)
2059 sp
->header
.freelist
= DTENTRYSTART
+ 1;
2060 sp
->header
.freecnt
= DTROOTMAXSLOT
- (DTENTRYSTART
+ 1);
2071 * function: delete the entry(s) referenced by a key.
2077 int dtDelete(tid_t tid
,
2078 struct inode
*ip
, struct component_name
* key
, ino_t
* ino
, int flag
)
2082 struct metapage
*mp
, *imp
;
2085 struct btstack btstack
;
2086 struct dt_lock
*dtlck
;
2090 struct ldtentry
*ldtentry
;
2092 u32 table_index
, next_index
;
2093 struct metapage
*nmp
;
2097 * search for the entry to delete:
2099 * dtSearch() returns (leaf page pinned, index at which to delete).
2101 if ((rc
= dtSearch(ip
, key
, ino
, &btstack
, flag
)))
2104 /* retrieve search result */
2105 DT_GETSEARCH(ip
, btstack
.top
, bn
, mp
, p
, index
);
2108 * We need to find put the index of the next entry into the
2109 * directory index table in order to resume a readdir from this
2113 stbl
= DT_GETSTBL(p
);
2114 ldtentry
= (struct ldtentry
*) & p
->slot
[stbl
[index
]];
2115 table_index
= le32_to_cpu(ldtentry
->index
);
2116 if (index
== (p
->header
.nextindex
- 1)) {
2118 * Last entry in this leaf page
2120 if ((p
->header
.flag
& BT_ROOT
)
2121 || (p
->header
.next
== 0))
2124 /* Read next leaf page */
2125 DT_GETPAGE(ip
, le64_to_cpu(p
->header
.next
),
2126 nmp
, PSIZE
, np
, rc
);
2130 stbl
= DT_GETSTBL(np
);
2132 (struct ldtentry
*) & np
->
2135 le32_to_cpu(ldtentry
->index
);
2141 (struct ldtentry
*) & p
->slot
[stbl
[index
+ 1]];
2142 next_index
= le32_to_cpu(ldtentry
->index
);
2144 free_index(tid
, ip
, table_index
, next_index
);
2147 * the leaf page becomes empty, delete the page
2149 if (p
->header
.nextindex
== 1) {
2150 /* delete empty page */
2151 rc
= dtDeleteUp(tid
, ip
, mp
, p
, &btstack
);
2154 * the leaf page has other entries remaining:
2156 * delete the entry from the leaf page.
2159 BT_MARK_DIRTY(mp
, ip
);
2161 * acquire a transaction lock on the leaf page
2163 tlck
= txLock(tid
, ip
, mp
, tlckDTREE
| tlckENTRY
);
2164 dtlck
= (struct dt_lock
*) & tlck
->lock
;
2167 * Do not assume that dtlck->index will be zero. During a
2168 * rename within a directory, this transaction may have
2169 * modified this page already when adding the new entry.
2172 /* linelock header */
2173 if (dtlck
->index
>= dtlck
->maxcnt
)
2174 dtlck
= (struct dt_lock
*) txLinelock(dtlck
);
2175 lv
= & dtlck
->lv
[dtlck
->index
];
2180 /* linelock stbl of non-root leaf page */
2181 if (!(p
->header
.flag
& BT_ROOT
)) {
2182 if (dtlck
->index
>= dtlck
->maxcnt
)
2183 dtlck
= (struct dt_lock
*) txLinelock(dtlck
);
2184 lv
= & dtlck
->lv
[dtlck
->index
];
2185 i
= index
>> L2DTSLOTSIZE
;
2186 lv
->offset
= p
->header
.stblindex
+ i
;
2188 ((p
->header
.nextindex
- 1) >> L2DTSLOTSIZE
) -
2193 /* free the leaf entry */
2194 dtDeleteEntry(p
, index
, &dtlck
);
2197 * Update directory index table for entries moved in stbl
2199 if (DO_INDEX(ip
) && index
< p
->header
.nextindex
) {
2203 stbl
= DT_GETSTBL(p
);
2204 for (i
= index
; i
< p
->header
.nextindex
; i
++) {
2206 (struct ldtentry
*) & p
->slot
[stbl
[i
]];
2207 modify_index(tid
, ip
,
2208 le32_to_cpu(ldtentry
->index
),
2209 bn
, i
, &imp
, &lblock
);
2212 release_metapage(imp
);
2226 * free empty pages as propagating deletion up the tree
2232 static int dtDeleteUp(tid_t tid
, struct inode
*ip
,
2233 struct metapage
* fmp
, dtpage_t
* fp
, struct btstack
* btstack
)
2236 struct metapage
*mp
;
2238 int index
, nextindex
;
2240 struct btframe
*parent
;
2241 struct dt_lock
*dtlck
;
2244 struct pxd_lock
*pxdlock
;
2248 * keep the root leaf page which has become empty
2250 if (BT_IS_ROOT(fmp
)) {
2254 * dtInitRoot() acquires txlock on the root
2256 dtInitRoot(tid
, ip
, PARENT(ip
));
2264 * free the non-root leaf page
2267 * acquire a transaction lock on the page
2269 * write FREEXTENT|NOREDOPAGE log record
2270 * N.B. linelock is overlaid as freed extent descriptor, and
2271 * the buffer page is freed;
2273 tlck
= txMaplock(tid
, ip
, tlckDTREE
| tlckFREE
);
2274 pxdlock
= (struct pxd_lock
*) & tlck
->lock
;
2275 pxdlock
->flag
= mlckFREEPXD
;
2276 pxdlock
->pxd
= fp
->header
.self
;
2279 /* update sibling pointers */
2280 if ((rc
= dtRelink(tid
, ip
, fp
))) {
2285 xlen
= lengthPXD(&fp
->header
.self
);
2287 /* Free quota allocation. */
2288 dquot_free_block(ip
, xlen
);
2290 /* free/invalidate its buffer page */
2291 discard_metapage(fmp
);
2294 * propagate page deletion up the directory tree
2296 * If the delete from the parent page makes it empty,
2297 * continue all the way up the tree.
2298 * stop if the root page is reached (which is never deleted) or
2299 * if the entry deletion does not empty the page.
2301 while ((parent
= BT_POP(btstack
)) != NULL
) {
2302 /* pin the parent page <sp> */
2303 DT_GETPAGE(ip
, parent
->bn
, mp
, PSIZE
, p
, rc
);
2308 * free the extent of the child page deleted
2310 index
= parent
->index
;
2313 * delete the entry for the child page from parent
2315 nextindex
= p
->header
.nextindex
;
2318 * the parent has the single entry being deleted:
2320 * free the parent page which has become empty.
2322 if (nextindex
== 1) {
2324 * keep the root internal page which has become empty
2326 if (p
->header
.flag
& BT_ROOT
) {
2330 * dtInitRoot() acquires txlock on the root
2332 dtInitRoot(tid
, ip
, PARENT(ip
));
2339 * free the parent page
2343 * acquire a transaction lock on the page
2345 * write FREEXTENT|NOREDOPAGE log record
2349 tlckDTREE
| tlckFREE
);
2350 pxdlock
= (struct pxd_lock
*) & tlck
->lock
;
2351 pxdlock
->flag
= mlckFREEPXD
;
2352 pxdlock
->pxd
= p
->header
.self
;
2355 /* update sibling pointers */
2356 if ((rc
= dtRelink(tid
, ip
, p
))) {
2361 xlen
= lengthPXD(&p
->header
.self
);
2363 /* Free quota allocation */
2364 dquot_free_block(ip
, xlen
);
2366 /* free/invalidate its buffer page */
2367 discard_metapage(mp
);
2375 * the parent has other entries remaining:
2377 * delete the router entry from the parent page.
2379 BT_MARK_DIRTY(mp
, ip
);
2381 * acquire a transaction lock on the page
2383 * action: router entry deletion
2385 tlck
= txLock(tid
, ip
, mp
, tlckDTREE
| tlckENTRY
);
2386 dtlck
= (struct dt_lock
*) & tlck
->lock
;
2388 /* linelock header */
2389 if (dtlck
->index
>= dtlck
->maxcnt
)
2390 dtlck
= (struct dt_lock
*) txLinelock(dtlck
);
2391 lv
= & dtlck
->lv
[dtlck
->index
];
2396 /* linelock stbl of non-root leaf page */
2397 if (!(p
->header
.flag
& BT_ROOT
)) {
2398 if (dtlck
->index
< dtlck
->maxcnt
)
2401 dtlck
= (struct dt_lock
*) txLinelock(dtlck
);
2402 lv
= & dtlck
->lv
[0];
2404 i
= index
>> L2DTSLOTSIZE
;
2405 lv
->offset
= p
->header
.stblindex
+ i
;
2407 ((p
->header
.nextindex
- 1) >> L2DTSLOTSIZE
) -
2412 /* free the router entry */
2413 dtDeleteEntry(p
, index
, &dtlck
);
2415 /* reset key of new leftmost entry of level (for consistency) */
2417 ((p
->header
.flag
& BT_ROOT
) || p
->header
.prev
== 0))
2418 dtTruncateEntry(p
, 0, &dtlck
);
2420 /* unpin the parent page */
2423 /* exit propagation up */
2428 ip
->i_size
-= PSIZE
;
2437 * link around a freed page.
2440 * fp: page to be freed
2444 static int dtRelink(tid_t tid
, struct inode
*ip
, dtpage_t
* p
)
2447 struct metapage
*mp
;
2450 struct dt_lock
*dtlck
;
2453 nextbn
= le64_to_cpu(p
->header
.next
);
2454 prevbn
= le64_to_cpu(p
->header
.prev
);
2456 /* update prev pointer of the next page */
2458 DT_GETPAGE(ip
, nextbn
, mp
, PSIZE
, p
, rc
);
2462 BT_MARK_DIRTY(mp
, ip
);
2464 * acquire a transaction lock on the next page
2466 * action: update prev pointer;
2468 tlck
= txLock(tid
, ip
, mp
, tlckDTREE
| tlckRELINK
);
2469 jfs_info("dtRelink nextbn: tlck = 0x%p, ip = 0x%p, mp=0x%p",
2471 dtlck
= (struct dt_lock
*) & tlck
->lock
;
2473 /* linelock header */
2474 if (dtlck
->index
>= dtlck
->maxcnt
)
2475 dtlck
= (struct dt_lock
*) txLinelock(dtlck
);
2476 lv
= & dtlck
->lv
[dtlck
->index
];
2481 p
->header
.prev
= cpu_to_le64(prevbn
);
2485 /* update next pointer of the previous page */
2487 DT_GETPAGE(ip
, prevbn
, mp
, PSIZE
, p
, rc
);
2491 BT_MARK_DIRTY(mp
, ip
);
2493 * acquire a transaction lock on the prev page
2495 * action: update next pointer;
2497 tlck
= txLock(tid
, ip
, mp
, tlckDTREE
| tlckRELINK
);
2498 jfs_info("dtRelink prevbn: tlck = 0x%p, ip = 0x%p, mp=0x%p",
2500 dtlck
= (struct dt_lock
*) & tlck
->lock
;
2502 /* linelock header */
2503 if (dtlck
->index
>= dtlck
->maxcnt
)
2504 dtlck
= (struct dt_lock
*) txLinelock(dtlck
);
2505 lv
= & dtlck
->lv
[dtlck
->index
];
2510 p
->header
.next
= cpu_to_le64(nextbn
);
2521 * initialize directory root (inline in inode)
2523 void dtInitRoot(tid_t tid
, struct inode
*ip
, u32 idotdot
)
2525 struct jfs_inode_info
*jfs_ip
= JFS_IP(ip
);
2530 struct dt_lock
*dtlck
;
2535 * If this was previously an non-empty directory, we need to remove
2536 * the old directory table.
2539 if (!jfs_dirtable_inline(ip
)) {
2540 struct tblock
*tblk
= tid_to_tblock(tid
);
2542 * We're playing games with the tid's xflag. If
2543 * we're removing a regular file, the file's xtree
2544 * is committed with COMMIT_PMAP, but we always
2545 * commit the directories xtree with COMMIT_PWMAP.
2547 xflag_save
= tblk
->xflag
;
2550 * xtTruncate isn't guaranteed to fully truncate
2551 * the xtree. The caller needs to check i_size
2552 * after committing the transaction to see if
2553 * additional truncation is needed. The
2554 * COMMIT_Stale flag tells caller that we
2555 * initiated the truncation.
2557 xtTruncate(tid
, ip
, 0, COMMIT_PWMAP
);
2558 set_cflag(COMMIT_Stale
, ip
);
2560 tblk
->xflag
= xflag_save
;
2564 jfs_ip
->next_index
= 2;
2566 ip
->i_size
= IDATASIZE
;
2569 * acquire a transaction lock on the root
2571 * action: directory initialization;
2573 tlck
= txLock(tid
, ip
, (struct metapage
*) & jfs_ip
->bxflag
,
2574 tlckDTREE
| tlckENTRY
| tlckBTROOT
);
2575 dtlck
= (struct dt_lock
*) & tlck
->lock
;
2578 ASSERT(dtlck
->index
== 0);
2579 lv
= & dtlck
->lv
[0];
2581 lv
->length
= DTROOTMAXSLOT
;
2584 p
= &jfs_ip
->i_dtroot
;
2586 p
->header
.flag
= DXD_INDEX
| BT_ROOT
| BT_LEAF
;
2588 p
->header
.nextindex
= 0;
2594 /* init data area of root */
2595 for (fsi
++; fsi
< DTROOTMAXSLOT
; f
++, fsi
++)
2599 p
->header
.freelist
= 1;
2600 p
->header
.freecnt
= 8;
2602 /* init '..' entry */
2603 p
->header
.idotdot
= cpu_to_le32(idotdot
);
2609 * add_missing_indices()
2611 * function: Fix dtree page in which one or more entries has an invalid index.
2612 * fsck.jfs should really fix this, but it currently does not.
2613 * Called from jfs_readdir when bad index is detected.
2615 static void add_missing_indices(struct inode
*inode
, s64 bn
)
2618 struct dt_lock
*dtlck
;
2622 struct metapage
*mp
;
2629 tid
= txBegin(inode
->i_sb
, 0);
2631 DT_GETPAGE(inode
, bn
, mp
, PSIZE
, p
, rc
);
2634 printk(KERN_ERR
"DT_GETPAGE failed!\n");
2637 BT_MARK_DIRTY(mp
, inode
);
2639 ASSERT(p
->header
.flag
& BT_LEAF
);
2641 tlck
= txLock(tid
, inode
, mp
, tlckDTREE
| tlckENTRY
);
2643 tlck
->type
|= tlckBTROOT
;
2645 dtlck
= (struct dt_lock
*) &tlck
->lock
;
2647 stbl
= DT_GETSTBL(p
);
2648 for (i
= 0; i
< p
->header
.nextindex
; i
++) {
2649 d
= (struct ldtentry
*) &p
->slot
[stbl
[i
]];
2650 index
= le32_to_cpu(d
->index
);
2651 if ((index
< 2) || (index
>= JFS_IP(inode
)->next_index
)) {
2652 d
->index
= cpu_to_le32(add_index(tid
, inode
, bn
, i
));
2653 if (dtlck
->index
>= dtlck
->maxcnt
)
2654 dtlck
= (struct dt_lock
*) txLinelock(dtlck
);
2655 lv
= &dtlck
->lv
[dtlck
->index
];
2656 lv
->offset
= stbl
[i
];
2663 (void) txCommit(tid
, 1, &inode
, 0);
2669 * Buffer to hold directory entry info while traversing a dtree page
2670 * before being fed to the filldir function
2680 * function to determine next variable-sized jfs_dirent in buffer
2682 static inline struct jfs_dirent
*next_jfs_dirent(struct jfs_dirent
*dirent
)
2684 return (struct jfs_dirent
*)
2686 ((sizeof (struct jfs_dirent
) + dirent
->name_len
+ 1 +
2687 sizeof (loff_t
) - 1) &
2688 ~(sizeof (loff_t
) - 1)));
2694 * function: read directory entries sequentially
2695 * from the specified entry offset
2699 * return: offset = (pn, index) of start entry
2700 * of next jfs_readdir()/dtRead()
2702 int jfs_readdir(struct file
*file
, struct dir_context
*ctx
)
2704 struct inode
*ip
= file_inode(file
);
2705 struct nls_table
*codepage
= JFS_SBI(ip
->i_sb
)->nls_tab
;
2707 loff_t dtpos
; /* legacy OS/2 style position */
2712 } *dtoffset
= (struct dtoffset
*) &dtpos
;
2714 struct metapage
*mp
;
2718 struct btstack btstack
;
2722 int d_namleft
, len
, outlen
;
2723 unsigned long dirent_buf
;
2727 uint loop_count
= 0;
2728 struct jfs_dirent
*jfs_dirent
;
2730 int overflow
, fix_page
, page_fixed
= 0;
2731 static int unique_pos
= 2; /* If we can't fix broken index */
2733 if (ctx
->pos
== DIREND
)
2738 * persistent index is stored in directory entries.
2739 * Special cases: 0 = .
2741 * -1 = End of directory
2745 dir_index
= (u32
) ctx
->pos
;
2748 * NFSv4 reserves cookies 1 and 2 for . and .. so the value
2749 * we return to the vfs is one greater than the one we use
2755 if (dir_index
> 1) {
2756 struct dir_table_slot dirtab_slot
;
2759 (dir_index
>= JFS_IP(ip
)->next_index
)) {
2760 /* Stale position. Directory has shrunk */
2765 rc
= read_index(ip
, dir_index
, &dirtab_slot
);
2770 if (dirtab_slot
.flag
== DIR_INDEX_FREE
) {
2771 if (loop_count
++ > JFS_IP(ip
)->next_index
) {
2772 jfs_err("jfs_readdir detected infinite loop!");
2776 dir_index
= le32_to_cpu(dirtab_slot
.addr2
);
2777 if (dir_index
== -1) {
2783 bn
= addressDTS(&dirtab_slot
);
2784 index
= dirtab_slot
.slot
;
2785 DT_GETPAGE(ip
, bn
, mp
, PSIZE
, p
, rc
);
2790 if (p
->header
.flag
& BT_INTERNAL
) {
2791 jfs_err("jfs_readdir: bad index table");
2797 if (dir_index
== 0) {
2802 if (!dir_emit(ctx
, ".", 1, ip
->i_ino
, DT_DIR
))
2809 if (!dir_emit(ctx
, "..", 2, PARENT(ip
), DT_DIR
))
2813 * Find first entry of left-most leaf
2820 if ((rc
= dtReadFirst(ip
, &btstack
)))
2823 DT_GETSEARCH(ip
, btstack
.top
, bn
, mp
, p
, index
);
2827 * Legacy filesystem - OS/2 & Linux JFS < 0.3.6
2829 * pn = 0; index = 1: First entry "."
2830 * pn = 0; index = 2: Second entry ".."
2831 * pn > 0: Real entries, pn=1 -> leftmost page
2832 * pn = index = -1: No more entries
2836 /* build "." entry */
2838 if (!dir_emit(ctx
, ".", 1, ip
->i_ino
, DT_DIR
))
2840 dtoffset
->index
= 2;
2844 if (dtoffset
->pn
== 0) {
2845 if (dtoffset
->index
== 2) {
2846 /* build ".." entry */
2847 if (!dir_emit(ctx
, "..", 2, PARENT(ip
), DT_DIR
))
2850 jfs_err("jfs_readdir called with invalid offset!");
2853 dtoffset
->index
= 0;
2862 if ((rc
= dtReadNext(ip
, &ctx
->pos
, &btstack
))) {
2863 jfs_err("jfs_readdir: unexpected rc = %d from dtReadNext",
2868 /* get start leaf page and index */
2869 DT_GETSEARCH(ip
, btstack
.top
, bn
, mp
, p
, index
);
2871 /* offset beyond directory eof ? */
2878 dirent_buf
= __get_free_page(GFP_KERNEL
);
2879 if (dirent_buf
== 0) {
2881 jfs_warn("jfs_readdir: __get_free_page failed!");
2887 jfs_dirent
= (struct jfs_dirent
*) dirent_buf
;
2889 overflow
= fix_page
= 0;
2891 stbl
= DT_GETSTBL(p
);
2893 for (i
= index
; i
< p
->header
.nextindex
; i
++) {
2894 if (stbl
[i
] < 0 || stbl
[i
] > 127) {
2895 jfs_err("JFS: Invalid stbl[%d] = %d for inode %ld, block = %lld",
2896 i
, stbl
[i
], (long)ip
->i_ino
, (long long)bn
);
2897 free_page(dirent_buf
);
2902 d
= (struct ldtentry
*) & p
->slot
[stbl
[i
]];
2904 if (((long) jfs_dirent
+ d
->namlen
+ 1) >
2905 (dirent_buf
+ PAGE_SIZE
)) {
2906 /* DBCS codepages could overrun dirent_buf */
2912 d_namleft
= d
->namlen
;
2913 name_ptr
= jfs_dirent
->name
;
2914 jfs_dirent
->ino
= le32_to_cpu(d
->inumber
);
2917 len
= min(d_namleft
, DTLHDRDATALEN
);
2918 jfs_dirent
->position
= le32_to_cpu(d
->index
);
2920 * d->index should always be valid, but it
2921 * isn't. fsck.jfs doesn't create the
2922 * directory index for the lost+found
2923 * directory. Rather than let it go,
2924 * we can try to fix it.
2926 if ((jfs_dirent
->position
< 2) ||
2927 (jfs_dirent
->position
>=
2928 JFS_IP(ip
)->next_index
)) {
2929 if (!page_fixed
&& !isReadOnly(ip
)) {
2932 * setting overflow and setting
2933 * index to i will cause the
2934 * same page to be processed
2935 * again starting here
2941 jfs_dirent
->position
= unique_pos
++;
2944 * We add 1 to the index because we may
2945 * use a value of 2 internally, and NFSv4
2946 * doesn't like that.
2948 jfs_dirent
->position
++;
2950 jfs_dirent
->position
= dtpos
;
2951 len
= min(d_namleft
, DTLHDRDATALEN_LEGACY
);
2954 /* copy the name of head/only segment */
2955 outlen
= jfs_strfromUCS_le(name_ptr
, d
->name
, len
,
2957 jfs_dirent
->name_len
= outlen
;
2959 /* copy name in the additional segment(s) */
2962 t
= (struct dtslot
*) & p
->slot
[next
];
2966 if (d_namleft
== 0) {
2968 "JFS:Dtree error: ino = %ld, bn=%lld, index = %d\n",
2974 len
= min(d_namleft
, DTSLOTDATALEN
);
2975 outlen
= jfs_strfromUCS_le(name_ptr
, t
->name
,
2977 jfs_dirent
->name_len
+= outlen
;
2983 jfs_dirent
= next_jfs_dirent(jfs_dirent
);
2990 /* Point to next leaf page */
2991 if (p
->header
.flag
& BT_ROOT
)
2994 bn
= le64_to_cpu(p
->header
.next
);
2996 /* update offset (pn:index) for new page */
2999 dtoffset
->index
= 0;
3005 /* unpin previous leaf page */
3008 jfs_dirent
= (struct jfs_dirent
*) dirent_buf
;
3009 while (jfs_dirents
--) {
3010 ctx
->pos
= jfs_dirent
->position
;
3011 if (!dir_emit(ctx
, jfs_dirent
->name
,
3012 jfs_dirent
->name_len
,
3013 jfs_dirent
->ino
, DT_UNKNOWN
))
3015 jfs_dirent
= next_jfs_dirent(jfs_dirent
);
3019 add_missing_indices(ip
, bn
);
3023 if (!overflow
&& (bn
== 0)) {
3028 DT_GETPAGE(ip
, bn
, mp
, PSIZE
, p
, rc
);
3030 free_page(dirent_buf
);
3036 free_page(dirent_buf
);
3045 * function: get the leftmost page of the directory
3047 static int dtReadFirst(struct inode
*ip
, struct btstack
* btstack
)
3051 int psize
= 288; /* initial in-line directory */
3052 struct metapage
*mp
;
3055 struct btframe
*btsp
;
3058 BT_CLR(btstack
); /* reset stack */
3061 * descend leftmost path of the tree
3063 * by convention, root bn = 0.
3066 DT_GETPAGE(ip
, bn
, mp
, psize
, p
, rc
);
3071 * leftmost leaf page
3073 if (p
->header
.flag
& BT_LEAF
) {
3074 /* return leftmost entry */
3075 btsp
= btstack
->top
;
3084 * descend down to leftmost child page
3086 if (BT_STACK_FULL(btstack
)) {
3088 jfs_error(ip
->i_sb
, "btstack overrun\n");
3089 BT_STACK_DUMP(btstack
);
3092 /* push (bn, index) of the parent page/entry */
3093 BT_PUSH(btstack
, bn
, 0);
3095 /* get the leftmost entry */
3096 stbl
= DT_GETSTBL(p
);
3098 if (stbl
[0] < 0 || stbl
[0] > 127) {
3100 jfs_error(ip
->i_sb
, "stbl[0] out of bound\n");
3104 xd
= (pxd_t
*) & p
->slot
[stbl
[0]];
3106 /* get the child page block address */
3107 bn
= addressPXD(xd
);
3108 psize
= lengthPXD(xd
) << JFS_SBI(ip
->i_sb
)->l2bsize
;
3110 /* unpin the parent page */
3119 * function: get the page of the specified offset (pn:index)
3121 * return: if (offset > eof), bn = -1;
3123 * note: if index > nextindex of the target leaf page,
3124 * start with 1st entry of next leaf page;
3126 static int dtReadNext(struct inode
*ip
, loff_t
* offset
,
3127 struct btstack
* btstack
)
3134 } *dtoffset
= (struct dtoffset
*) offset
;
3136 struct metapage
*mp
;
3141 struct btframe
*btsp
, *parent
;
3145 * get leftmost leaf page pinned
3147 if ((rc
= dtReadFirst(ip
, btstack
)))
3151 DT_GETSEARCH(ip
, btstack
->top
, bn
, mp
, p
, index
);
3153 /* get the start offset (pn:index) */
3154 pn
= dtoffset
->pn
- 1; /* Now pn = 0 represents leftmost leaf */
3155 index
= dtoffset
->index
;
3157 /* start at leftmost page ? */
3159 /* offset beyond eof ? */
3160 if (index
< p
->header
.nextindex
)
3163 if (p
->header
.flag
& BT_ROOT
) {
3168 /* start with 1st entry of next leaf page */
3170 dtoffset
->index
= index
= 0;
3174 /* start at non-leftmost page: scan parent pages for large pn */
3175 if (p
->header
.flag
& BT_ROOT
) {
3180 /* start after next leaf page ? */
3184 /* get leaf page pn = 1 */
3186 bn
= le64_to_cpu(p
->header
.next
);
3188 /* unpin leaf page */
3191 /* offset beyond eof ? */
3200 * scan last internal page level to get target leaf page
3203 /* unpin leftmost leaf page */
3206 /* get left most parent page */
3207 btsp
= btstack
->top
;
3210 DT_GETPAGE(ip
, bn
, mp
, PSIZE
, p
, rc
);
3214 /* scan parent pages at last internal page level */
3215 while (pn
>= p
->header
.nextindex
) {
3216 pn
-= p
->header
.nextindex
;
3218 /* get next parent page address */
3219 bn
= le64_to_cpu(p
->header
.next
);
3221 /* unpin current parent page */
3224 /* offset beyond eof ? */
3230 /* get next parent page */
3231 DT_GETPAGE(ip
, bn
, mp
, PSIZE
, p
, rc
);
3235 /* update parent page stack frame */
3239 /* get leaf page address */
3240 stbl
= DT_GETSTBL(p
);
3241 xd
= (pxd_t
*) & p
->slot
[stbl
[pn
]];
3242 bn
= addressPXD(xd
);
3244 /* unpin parent page */
3248 * get target leaf page
3251 DT_GETPAGE(ip
, bn
, mp
, PSIZE
, p
, rc
);
3256 * leaf page has been completed:
3257 * start with 1st entry of next leaf page
3259 if (index
>= p
->header
.nextindex
) {
3260 bn
= le64_to_cpu(p
->header
.next
);
3262 /* unpin leaf page */
3265 /* offset beyond eof ? */
3271 /* get next leaf page */
3272 DT_GETPAGE(ip
, bn
, mp
, PSIZE
, p
, rc
);
3276 /* start with 1st entry of next leaf page */
3278 dtoffset
->index
= 0;
3282 /* return target leaf page pinned */
3283 btsp
= btstack
->top
;
3285 btsp
->index
= dtoffset
->index
;
3295 * function: compare search key with an internal entry
3298 * < 0 if k is < record
3299 * = 0 if k is = record
3300 * > 0 if k is > record
3302 static int dtCompare(struct component_name
* key
, /* search key */
3303 dtpage_t
* p
, /* directory page */
3305 { /* entry slot index */
3308 int klen
, namlen
, len
, rc
;
3309 struct idtentry
*ih
;
3313 * force the left-most key on internal pages, at any level of
3314 * the tree, to be less than any search key.
3315 * this obviates having to update the leftmost key on an internal
3316 * page when the user inserts a new key in the tree smaller than
3317 * anything that has been stored.
3319 * (? if/when dtSearch() narrows down to 1st entry (index = 0),
3320 * at any internal page at any level of the tree,
3321 * it descends to child of the entry anyway -
3322 * ? make the entry as min size dummy entry)
3324 * if (e->index == 0 && h->prevpg == P_INVALID && !(h->flags & BT_LEAF))
3331 ih
= (struct idtentry
*) & p
->slot
[si
];
3334 namlen
= ih
->namlen
;
3335 len
= min(namlen
, DTIHDRDATALEN
);
3337 /* compare with head/only segment */
3338 len
= min(klen
, len
);
3339 if ((rc
= UniStrncmp_le(kname
, name
, len
)))
3345 /* compare with additional segment(s) */
3347 while (klen
> 0 && namlen
> 0) {
3348 /* compare with next name segment */
3349 t
= (struct dtslot
*) & p
->slot
[si
];
3350 len
= min(namlen
, DTSLOTDATALEN
);
3351 len
= min(klen
, len
);
3353 if ((rc
= UniStrncmp_le(kname
, name
, len
)))
3362 return (klen
- namlen
);
3371 * function: compare search key with an (leaf/internal) entry
3374 * < 0 if k is < record
3375 * = 0 if k is = record
3376 * > 0 if k is > record
3378 static int ciCompare(struct component_name
* key
, /* search key */
3379 dtpage_t
* p
, /* directory page */
3380 int si
, /* entry slot index */
3385 int klen
, namlen
, len
, rc
;
3386 struct ldtentry
*lh
;
3387 struct idtentry
*ih
;
3392 * force the left-most key on internal pages, at any level of
3393 * the tree, to be less than any search key.
3394 * this obviates having to update the leftmost key on an internal
3395 * page when the user inserts a new key in the tree smaller than
3396 * anything that has been stored.
3398 * (? if/when dtSearch() narrows down to 1st entry (index = 0),
3399 * at any internal page at any level of the tree,
3400 * it descends to child of the entry anyway -
3401 * ? make the entry as min size dummy entry)
3403 * if (e->index == 0 && h->prevpg == P_INVALID && !(h->flags & BT_LEAF))
3413 if (p
->header
.flag
& BT_LEAF
) {
3414 lh
= (struct ldtentry
*) & p
->slot
[si
];
3417 namlen
= lh
->namlen
;
3418 if (flag
& JFS_DIR_INDEX
)
3419 len
= min(namlen
, DTLHDRDATALEN
);
3421 len
= min(namlen
, DTLHDRDATALEN_LEGACY
);
3424 * internal page entry
3427 ih
= (struct idtentry
*) & p
->slot
[si
];
3430 namlen
= ih
->namlen
;
3431 len
= min(namlen
, DTIHDRDATALEN
);
3434 /* compare with head/only segment */
3435 len
= min(klen
, len
);
3436 for (i
= 0; i
< len
; i
++, kname
++, name
++) {
3437 /* only uppercase if case-insensitive support is on */
3438 if ((flag
& JFS_OS2
) == JFS_OS2
)
3439 x
= UniToupper(le16_to_cpu(*name
));
3441 x
= le16_to_cpu(*name
);
3442 if ((rc
= *kname
- x
))
3449 /* compare with additional segment(s) */
3450 while (klen
> 0 && namlen
> 0) {
3451 /* compare with next name segment */
3452 t
= (struct dtslot
*) & p
->slot
[si
];
3453 len
= min(namlen
, DTSLOTDATALEN
);
3454 len
= min(klen
, len
);
3456 for (i
= 0; i
< len
; i
++, kname
++, name
++) {
3457 /* only uppercase if case-insensitive support is on */
3458 if ((flag
& JFS_OS2
) == JFS_OS2
)
3459 x
= UniToupper(le16_to_cpu(*name
));
3461 x
= le16_to_cpu(*name
);
3463 if ((rc
= *kname
- x
))
3472 return (klen
- namlen
);
3477 * ciGetLeafPrefixKey()
3479 * function: compute prefix of suffix compression
3480 * from two adjacent leaf entries
3481 * across page boundary
3483 * return: non-zero on error
3486 static int ciGetLeafPrefixKey(dtpage_t
* lp
, int li
, dtpage_t
* rp
,
3487 int ri
, struct component_name
* key
, int flag
)
3490 wchar_t *pl
, *pr
, *kname
;
3491 struct component_name lkey
;
3492 struct component_name rkey
;
3494 lkey
.name
= kmalloc_array(JFS_NAME_MAX
+ 1, sizeof(wchar_t),
3496 if (lkey
.name
== NULL
)
3499 rkey
.name
= kmalloc_array(JFS_NAME_MAX
+ 1, sizeof(wchar_t),
3501 if (rkey
.name
== NULL
) {
3506 /* get left and right key */
3507 dtGetKey(lp
, li
, &lkey
, flag
);
3508 lkey
.name
[lkey
.namlen
] = 0;
3510 if ((flag
& JFS_OS2
) == JFS_OS2
)
3513 dtGetKey(rp
, ri
, &rkey
, flag
);
3514 rkey
.name
[rkey
.namlen
] = 0;
3517 if ((flag
& JFS_OS2
) == JFS_OS2
)
3520 /* compute prefix */
3523 namlen
= min(lkey
.namlen
, rkey
.namlen
);
3524 for (pl
= lkey
.name
, pr
= rkey
.name
;
3525 namlen
; pl
++, pr
++, namlen
--, klen
++, kname
++) {
3528 key
->namlen
= klen
+ 1;
3533 /* l->namlen <= r->namlen since l <= r */
3534 if (lkey
.namlen
< rkey
.namlen
) {
3536 key
->namlen
= klen
+ 1;
3537 } else /* l->namelen == r->namelen */
3551 * function: get key of the entry
3553 static void dtGetKey(dtpage_t
* p
, int i
, /* entry index */
3554 struct component_name
* key
, int flag
)
3558 struct ldtentry
*lh
;
3559 struct idtentry
*ih
;
3566 stbl
= DT_GETSTBL(p
);
3568 if (p
->header
.flag
& BT_LEAF
) {
3569 lh
= (struct ldtentry
*) & p
->slot
[si
];
3571 namlen
= lh
->namlen
;
3573 if (flag
& JFS_DIR_INDEX
)
3574 len
= min(namlen
, DTLHDRDATALEN
);
3576 len
= min(namlen
, DTLHDRDATALEN_LEGACY
);
3578 ih
= (struct idtentry
*) & p
->slot
[si
];
3580 namlen
= ih
->namlen
;
3582 len
= min(namlen
, DTIHDRDATALEN
);
3585 key
->namlen
= namlen
;
3589 * move head/only segment
3591 UniStrncpy_from_le(kname
, name
, len
);
3594 * move additional segment(s)
3597 /* get next segment */
3601 len
= min(namlen
, DTSLOTDATALEN
);
3602 UniStrncpy_from_le(kname
, t
->name
, len
);
3612 * function: allocate free slot(s) and
3613 * write a leaf/internal entry
3615 * return: entry slot index
3617 static void dtInsertEntry(dtpage_t
* p
, int index
, struct component_name
* key
,
3618 ddata_t
* data
, struct dt_lock
** dtlock
)
3620 struct dtslot
*h
, *t
;
3621 struct ldtentry
*lh
= NULL
;
3622 struct idtentry
*ih
= NULL
;
3623 int hsi
, fsi
, klen
, len
, nextindex
;
3628 struct dt_lock
*dtlck
= *dtlock
;
3632 struct metapage
*mp
= NULL
;
3637 /* allocate a free slot */
3638 hsi
= fsi
= p
->header
.freelist
;
3640 p
->header
.freelist
= h
->next
;
3641 --p
->header
.freecnt
;
3643 /* open new linelock */
3644 if (dtlck
->index
>= dtlck
->maxcnt
)
3645 dtlck
= (struct dt_lock
*) txLinelock(dtlck
);
3647 lv
= & dtlck
->lv
[dtlck
->index
];
3650 /* write head/only segment */
3651 if (p
->header
.flag
& BT_LEAF
) {
3652 lh
= (struct ldtentry
*) h
;
3654 lh
->inumber
= cpu_to_le32(data
->leaf
.ino
);
3657 if (data
->leaf
.ip
) {
3658 len
= min(klen
, DTLHDRDATALEN
);
3659 if (!(p
->header
.flag
& BT_ROOT
))
3660 bn
= addressPXD(&p
->header
.self
);
3661 lh
->index
= cpu_to_le32(add_index(data
->leaf
.tid
,
3665 len
= min(klen
, DTLHDRDATALEN_LEGACY
);
3667 ih
= (struct idtentry
*) h
;
3673 len
= min(klen
, DTIHDRDATALEN
);
3676 UniStrncpy_to_le(name
, kname
, len
);
3681 /* write additional segment(s) */
3686 fsi
= p
->header
.freelist
;
3688 p
->header
.freelist
= t
->next
;
3689 --p
->header
.freecnt
;
3691 /* is next slot contiguous ? */
3692 if (fsi
!= xsi
+ 1) {
3693 /* close current linelock */
3697 /* open new linelock */
3698 if (dtlck
->index
< dtlck
->maxcnt
)
3701 dtlck
= (struct dt_lock
*) txLinelock(dtlck
);
3702 lv
= & dtlck
->lv
[0];
3710 len
= min(klen
, DTSLOTDATALEN
);
3711 UniStrncpy_to_le(t
->name
, kname
, len
);
3718 /* close current linelock */
3724 /* terminate last/only segment */
3726 /* single segment entry */
3727 if (p
->header
.flag
& BT_LEAF
)
3732 /* multi-segment entry */
3735 /* if insert into middle, shift right succeeding entries in stbl */
3736 stbl
= DT_GETSTBL(p
);
3737 nextindex
= p
->header
.nextindex
;
3738 if (index
< nextindex
) {
3739 memmove(stbl
+ index
+ 1, stbl
+ index
, nextindex
- index
);
3741 if ((p
->header
.flag
& BT_LEAF
) && data
->leaf
.ip
) {
3745 * Need to update slot number for entries that moved
3749 for (n
= index
+ 1; n
<= nextindex
; n
++) {
3750 lh
= (struct ldtentry
*) & (p
->slot
[stbl
[n
]]);
3751 modify_index(data
->leaf
.tid
, data
->leaf
.ip
,
3752 le32_to_cpu(lh
->index
), bn
, n
,
3756 release_metapage(mp
);
3762 /* advance next available entry index of stbl */
3763 ++p
->header
.nextindex
;
3770 * function: move entries from split/left page to new/right page
3772 * nextindex of dst page and freelist/freecnt of both pages
3775 static void dtMoveEntry(dtpage_t
* sp
, int si
, dtpage_t
* dp
,
3776 struct dt_lock
** sdtlock
, struct dt_lock
** ddtlock
,
3779 int ssi
, next
; /* src slot index */
3780 int di
; /* dst entry index */
3781 int dsi
; /* dst slot index */
3782 s8
*sstbl
, *dstbl
; /* sorted entry table */
3784 struct ldtentry
*slh
, *dlh
= NULL
;
3785 struct idtentry
*sih
, *dih
= NULL
;
3786 struct dtslot
*h
, *s
, *d
;
3787 struct dt_lock
*sdtlck
= *sdtlock
, *ddtlck
= *ddtlock
;
3788 struct lv
*slv
, *dlv
;
3792 sstbl
= (s8
*) & sp
->slot
[sp
->header
.stblindex
];
3793 dstbl
= (s8
*) & dp
->slot
[dp
->header
.stblindex
];
3795 dsi
= dp
->header
.freelist
; /* first (whole page) free slot */
3796 sfsi
= sp
->header
.freelist
;
3798 /* linelock destination entry slot */
3799 dlv
= & ddtlck
->lv
[ddtlck
->index
];
3802 /* linelock source entry slot */
3803 slv
= & sdtlck
->lv
[sdtlck
->index
];
3804 slv
->offset
= sstbl
[si
];
3805 xssi
= slv
->offset
- 1;
3811 for (di
= 0; si
< sp
->header
.nextindex
; si
++, di
++) {
3815 /* is next slot contiguous ? */
3816 if (ssi
!= xssi
+ 1) {
3817 /* close current linelock */
3821 /* open new linelock */
3822 if (sdtlck
->index
< sdtlck
->maxcnt
)
3825 sdtlck
= (struct dt_lock
*) txLinelock(sdtlck
);
3826 slv
= & sdtlck
->lv
[0];
3834 * move head/only segment of an entry
3837 h
= d
= &dp
->slot
[dsi
];
3839 /* get src slot and move */
3841 if (sp
->header
.flag
& BT_LEAF
) {
3842 /* get source entry */
3843 slh
= (struct ldtentry
*) s
;
3844 dlh
= (struct ldtentry
*) h
;
3845 snamlen
= slh
->namlen
;
3848 len
= min(snamlen
, DTLHDRDATALEN
);
3849 dlh
->index
= slh
->index
; /* little-endian */
3851 len
= min(snamlen
, DTLHDRDATALEN_LEGACY
);
3853 memcpy(dlh
, slh
, 6 + len
* 2);
3857 /* update dst head/only segment next field */
3861 sih
= (struct idtentry
*) s
;
3862 snamlen
= sih
->namlen
;
3864 len
= min(snamlen
, DTIHDRDATALEN
);
3865 dih
= (struct idtentry
*) h
;
3866 memcpy(dih
, sih
, 10 + len
* 2);
3873 /* free src head/only segment */
3883 * move additional segment(s) of the entry
3886 while ((ssi
= next
) >= 0) {
3887 /* is next slot contiguous ? */
3888 if (ssi
!= xssi
+ 1) {
3889 /* close current linelock */
3893 /* open new linelock */
3894 if (sdtlck
->index
< sdtlck
->maxcnt
)
3900 slv
= & sdtlck
->lv
[0];
3907 /* get next source segment */
3910 /* get next destination free slot */
3913 len
= min(snamlen
, DTSLOTDATALEN
);
3914 UniStrncpy_le(d
->name
, s
->name
, len
);
3923 /* free source segment */
3932 /* terminate dst last/only segment */
3934 /* single segment entry */
3935 if (dp
->header
.flag
& BT_LEAF
)
3940 /* multi-segment entry */
3944 /* close current linelock */
3953 /* update source header */
3954 sp
->header
.freelist
= sfsi
;
3955 sp
->header
.freecnt
+= nd
;
3957 /* update destination header */
3958 dp
->header
.nextindex
= di
;
3960 dp
->header
.freelist
= dsi
;
3961 dp
->header
.freecnt
-= nd
;
3968 * function: free a (leaf/internal) entry
3970 * log freelist header, stbl, and each segment slot of entry
3971 * (even though last/only segment next field is modified,
3972 * physical image logging requires all segment slots of
3973 * the entry logged to avoid applying previous updates
3974 * to the same slots)
3976 static void dtDeleteEntry(dtpage_t
* p
, int fi
, struct dt_lock
** dtlock
)
3978 int fsi
; /* free entry slot index */
3982 struct dt_lock
*dtlck
= *dtlock
;
3986 /* get free entry slot index */
3987 stbl
= DT_GETSTBL(p
);
3990 /* open new linelock */
3991 if (dtlck
->index
>= dtlck
->maxcnt
)
3992 dtlck
= (struct dt_lock
*) txLinelock(dtlck
);
3993 lv
= & dtlck
->lv
[dtlck
->index
];
3997 /* get the head/only segment */
3999 if (p
->header
.flag
& BT_LEAF
)
4000 si
= ((struct ldtentry
*) t
)->next
;
4002 si
= ((struct idtentry
*) t
)->next
;
4009 /* find the last/only segment */
4011 /* is next slot contiguous ? */
4012 if (si
!= xsi
+ 1) {
4013 /* close current linelock */
4017 /* open new linelock */
4018 if (dtlck
->index
< dtlck
->maxcnt
)
4021 dtlck
= (struct dt_lock
*) txLinelock(dtlck
);
4022 lv
= & dtlck
->lv
[0];
4038 /* close current linelock */
4044 /* update freelist */
4045 t
->next
= p
->header
.freelist
;
4046 p
->header
.freelist
= fsi
;
4047 p
->header
.freecnt
+= freecnt
;
4049 /* if delete from middle,
4050 * shift left the succedding entries in the stbl
4052 si
= p
->header
.nextindex
;
4054 memmove(&stbl
[fi
], &stbl
[fi
+ 1], si
- fi
- 1);
4056 p
->header
.nextindex
--;
4063 * function: truncate a (leaf/internal) entry
4065 * log freelist header, stbl, and each segment slot of entry
4066 * (even though last/only segment next field is modified,
4067 * physical image logging requires all segment slots of
4068 * the entry logged to avoid applying previous updates
4069 * to the same slots)
4071 static void dtTruncateEntry(dtpage_t
* p
, int ti
, struct dt_lock
** dtlock
)
4073 int tsi
; /* truncate entry slot index */
4077 struct dt_lock
*dtlck
= *dtlock
;
4081 /* get free entry slot index */
4082 stbl
= DT_GETSTBL(p
);
4085 /* open new linelock */
4086 if (dtlck
->index
>= dtlck
->maxcnt
)
4087 dtlck
= (struct dt_lock
*) txLinelock(dtlck
);
4088 lv
= & dtlck
->lv
[dtlck
->index
];
4092 /* get the head/only segment */
4094 ASSERT(p
->header
.flag
& BT_INTERNAL
);
4095 ((struct idtentry
*) t
)->namlen
= 0;
4096 si
= ((struct idtentry
*) t
)->next
;
4097 ((struct idtentry
*) t
)->next
= -1;
4104 /* find the last/only segment */
4106 /* is next slot contiguous ? */
4107 if (si
!= xsi
+ 1) {
4108 /* close current linelock */
4112 /* open new linelock */
4113 if (dtlck
->index
< dtlck
->maxcnt
)
4116 dtlck
= (struct dt_lock
*) txLinelock(dtlck
);
4117 lv
= & dtlck
->lv
[0];
4133 /* close current linelock */
4139 /* update freelist */
4142 t
->next
= p
->header
.freelist
;
4143 p
->header
.freelist
= fsi
;
4144 p
->header
.freecnt
+= freecnt
;
4149 * dtLinelockFreelist()
4151 static void dtLinelockFreelist(dtpage_t
* p
, /* directory page */
4152 int m
, /* max slot index */
4153 struct dt_lock
** dtlock
)
4155 int fsi
; /* free entry slot index */
4158 struct dt_lock
*dtlck
= *dtlock
;
4162 /* get free entry slot index */
4163 fsi
= p
->header
.freelist
;
4165 /* open new linelock */
4166 if (dtlck
->index
>= dtlck
->maxcnt
)
4167 dtlck
= (struct dt_lock
*) txLinelock(dtlck
);
4168 lv
= & dtlck
->lv
[dtlck
->index
];
4178 /* find the last/only segment */
4179 while (si
< m
&& si
>= 0) {
4180 /* is next slot contiguous ? */
4181 if (si
!= xsi
+ 1) {
4182 /* close current linelock */
4186 /* open new linelock */
4187 if (dtlck
->index
< dtlck
->maxcnt
)
4190 dtlck
= (struct dt_lock
*) txLinelock(dtlck
);
4191 lv
= & dtlck
->lv
[0];
4205 /* close current linelock */
4216 * FUNCTION: Modify the inode number part of a directory entry
4219 * tid - Transaction id
4220 * ip - Inode of parent directory
4221 * key - Name of entry to be modified
4222 * orig_ino - Original inode number expected in entry
4223 * new_ino - New inode number to put into entry
4227 * -ESTALE - If entry found does not match orig_ino passed in
4228 * -ENOENT - If no entry can be found to match key
4229 * 0 - If successfully modified entry
4231 int dtModify(tid_t tid
, struct inode
*ip
,
4232 struct component_name
* key
, ino_t
* orig_ino
, ino_t new_ino
, int flag
)
4236 struct metapage
*mp
;
4239 struct btstack btstack
;
4241 struct dt_lock
*dtlck
;
4244 int entry_si
; /* entry slot index */
4245 struct ldtentry
*entry
;
4248 * search for the entry to modify:
4250 * dtSearch() returns (leaf page pinned, index at which to modify).
4252 if ((rc
= dtSearch(ip
, key
, orig_ino
, &btstack
, flag
)))
4255 /* retrieve search result */
4256 DT_GETSEARCH(ip
, btstack
.top
, bn
, mp
, p
, index
);
4258 BT_MARK_DIRTY(mp
, ip
);
4260 * acquire a transaction lock on the leaf page of named entry
4262 tlck
= txLock(tid
, ip
, mp
, tlckDTREE
| tlckENTRY
);
4263 dtlck
= (struct dt_lock
*) & tlck
->lock
;
4265 /* get slot index of the entry */
4266 stbl
= DT_GETSTBL(p
);
4267 entry_si
= stbl
[index
];
4269 /* linelock entry */
4270 ASSERT(dtlck
->index
== 0);
4271 lv
= & dtlck
->lv
[0];
4272 lv
->offset
= entry_si
;
4276 /* get the head/only segment */
4277 entry
= (struct ldtentry
*) & p
->slot
[entry_si
];
4279 /* substitute the inode number of the entry */
4280 entry
->inumber
= cpu_to_le32(new_ino
);
4282 /* unpin the leaf page */