Skip to content

Commit

Permalink
xfs: make common lower bound function for calculating index of Hash
Browse files Browse the repository at this point in the history
Change-Id: I5e902031b0e2a1051a7f96ab94dca0dba0ca9112
Reviewed-on: https://review.haiku-os.org/c/haiku/+/5698
Tested-by: Commit checker robot <[email protected]>
Reviewed-by: Adrien Destugues <[email protected]>
  • Loading branch information
Mashijams authored and pulkomandy committed Oct 2, 2022
1 parent 88131dc commit 40b5272
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 117 deletions.
18 changes: 2 additions & 16 deletions src/add-ons/kernel/file_systems/xfs/BPlusTree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -733,24 +733,10 @@ TreeDirectory::Lookup(const char* name, size_t length, xfs_ino_t* ino)
int numberOfLeafEntries = leafHeader->Count();
TRACE("numberOfLeafEntries:(%" B_PRId32 ")\n", numberOfLeafEntries);
int left = 0;
int mid;
int right = numberOfLeafEntries - 1;

// Trying to find the lowerbound of hashValueOfRequest
// This is slightly different from bsearch(), as we want the first
// instance of hashValueOfRequest and not any instance.
while (left < right) {
mid = (left + right) / 2;
uint32 hashval = B_BENDIAN_TO_HOST_INT32(leafEntry[mid].hashval);
if (hashval >= hashValueOfRequest) {
right = mid;
continue;
}
if (hashval < hashValueOfRequest) {
left = mid + 1;
}
}
TRACE("left:(%" B_PRId32 "), right:(%" B_PRId32 ")\n", left, right);
hashLowerBound<ExtentLeafEntry>(leafEntry, left, right, hashValueOfRequest);

uint32 nextLeaf = leafHeader->Forw();
uint32 lastHashVal = B_BENDIAN_TO_HOST_INT32(
leafEntry[numberOfLeafEntries - 1].hashval);
Expand Down
19 changes: 1 addition & 18 deletions src/add-ons/kernel/file_systems/xfs/Extent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -215,26 +215,9 @@ Extent::Lookup(const char* name, size_t length, xfs_ino_t* ino)

int numberOfLeafEntries = B_BENDIAN_TO_HOST_INT32(blockTail->count);
int left = 0;
int mid;
int right = numberOfLeafEntries - 1;

/*
* Trying to find the lowerbound of hashValueOfRequest
* This is slightly different from bsearch(), as we want the first
* instance of hashValueOfRequest and not any instance.
*/
while (left < right) {
mid = (left+right)/2;
uint32 hashval = B_BENDIAN_TO_HOST_INT32(leafEntry[mid].hashval);
if (hashval >= hashValueOfRequest) {
right = mid;
continue;
}
if (hashval < hashValueOfRequest) {
left = mid+1;
}
}
TRACE("left:(%" B_PRId32 "), right:(%" B_PRId32 ")\n", left, right);
hashLowerBound<ExtentLeafEntry>(leafEntry, left, right, hashValueOfRequest);

while (B_BENDIAN_TO_HOST_INT32(leafEntry[left].hashval)
== hashValueOfRequest) {
Expand Down
32 changes: 28 additions & 4 deletions src/add-ons/kernel/file_systems/xfs/Inode.h
Original file line number Diff line number Diff line change
Expand Up @@ -144,10 +144,6 @@ struct ExtentMapEntry {
};


uint32
hashfunction(const char* name, int length);


struct xfs_timestamp_t {
int32 t_sec;
int32 t_nsec;
Expand Down Expand Up @@ -442,4 +438,32 @@ class Inode {
ExtentMapEntry* fExtents;
};


uint32 hashfunction(const char* name, int length);


// A common function to return given hash lowerbound
template<class T> void
hashLowerBound(T* entry, int& left, int& right, uint32 hashValueOfRequest)
{
int mid;

/*
* Trying to find the lowerbound of hashValueOfRequest
* This is slightly different from bsearch(), as we want the first
* instance of hashValueOfRequest and not any instance.
*/
while (left < right) {
mid = (left + right) / 2;
uint32 hashval = B_BENDIAN_TO_HOST_INT32(entry[mid].hashval);
if (hashval >= hashValueOfRequest) {
right = mid;
continue;
}
if (hashval < hashValueOfRequest)
left = mid+1;
}
TRACE("left:(%" B_PRId32 "), right:(%" B_PRId32 ")\n", left, right);
}

#endif
15 changes: 1 addition & 14 deletions src/add-ons/kernel/file_systems/xfs/LeafAttribute.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -286,24 +286,11 @@ LeafAttribute::Lookup(const char* name, size_t* nameLength)

int numberOfLeafEntries = header->Count();
int left = 0;
int mid;
int right = numberOfLeafEntries - 1;

delete header;

// LowerBound
while (left < right) {
mid = (left + right) / 2;
uint32 hashval = B_BENDIAN_TO_HOST_INT32(entry[mid].hashval);
if (hashval >= hashValueOfRequest) {
right = mid;
continue;
}
if (hashval < hashValueOfRequest) {
left = mid+1;
}
}
TRACE("left:(%" B_PRId32 "), right:(%" B_PRId32 ")\n", left, right);
hashLowerBound<AttrLeafEntry>(entry, left, right, hashValueOfRequest);

while (B_BENDIAN_TO_HOST_INT32(entry[left].hashval) == hashValueOfRequest) {

Expand Down
19 changes: 1 addition & 18 deletions src/add-ons/kernel/file_systems/xfs/LeafDirectory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -342,27 +342,10 @@ LeafDirectory::Lookup(const char* name, size_t length, xfs_ino_t* ino)
int numberOfLeafEntries = leafHeader->Count();
TRACE("numberOfLeafEntries:(%" B_PRId32 ")\n", numberOfLeafEntries);
int left = 0;
int mid;
int right = numberOfLeafEntries - 1;
Volume* volume = fInode->GetVolume();

/*
* Trying to find the lowerbound of hashValueOfRequest
* This is slightly different from bsearch(), as we want the first
* instance of hashValueOfRequest and not any instance.
*/
while (left < right) {
mid = (left+right)/2;
uint32 hashval = B_BENDIAN_TO_HOST_INT32(leafEntry[mid].hashval);
if (hashval >= hashValueOfRequest) {
right = mid;
continue;
}
if (hashval < hashValueOfRequest) {
left = mid+1;
}
}
TRACE("left:(%" B_PRId32 "), right:(%" B_PRId32 ")\n", left, right);
hashLowerBound<ExtentLeafEntry>(leafEntry, left, right, hashValueOfRequest);

while (B_BENDIAN_TO_HOST_INT32(leafEntry[left].hashval)
== hashValueOfRequest) {
Expand Down
21 changes: 2 additions & 19 deletions src/add-ons/kernel/file_systems/xfs/Node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -399,27 +399,10 @@ NodeDirectory::Lookup(const char* name, size_t length, xfs_ino_t* ino)
int numberOfLeafEntries = leafHeader->Count();
TRACE("numberOfLeafEntries:(%" B_PRId32 ")\n", numberOfLeafEntries);
int left = 0;
int mid;
int right = numberOfLeafEntries - 1;
Volume* volume = fInode->GetVolume();

/*
* Trying to find the lowerbound of hashValueOfRequest
* This is slightly different from bsearch(), as we want the first
* instance of hashValueOfRequest and not any instance.
*/
while (left < right) {
mid = (left + right) / 2;
uint32 hashval = B_BENDIAN_TO_HOST_INT32(leafEntry[mid].hashval);
if (hashval >= hashValueOfRequest) {
right = mid;
continue;
}
if (hashval < hashValueOfRequest) {
left = mid + 1;
}
}
TRACE("left:(%" B_PRId32 "), right:(%" B_PRId32 ")\n", left, right);
hashLowerBound<ExtentLeafEntry>(leafEntry, left, right, hashValueOfRequest);

while (B_BENDIAN_TO_HOST_INT32(leafEntry[left].hashval)
== hashValueOfRequest) {
Expand Down Expand Up @@ -672,4 +655,4 @@ SizeOfNodeHeader(Inode* inode)
return sizeof(NodeHeaderV4) - XFS_NODE_V4_VPTR_OFF;
else
return sizeof(NodeHeaderV5) - XFS_NODE_V5_VPTR_OFF;
}
}
30 changes: 2 additions & 28 deletions src/add-ons/kernel/file_systems/xfs/NodeAttribute.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -326,27 +326,13 @@ NodeAttribute::Lookup(const char* name, size_t* nameLength)

int TotalNodeEntries = node->Count();
int left = 0;
int mid;
int right = TotalNodeEntries - 1;

delete node;

// LowerBound
while (left < right) {
mid = (left + right) / 2;
uint32 hashval = B_BENDIAN_TO_HOST_INT32(nodeEntry[mid].hashval);
if (hashval >= hashValueOfRequest) {
right = mid;
continue;
}
if (hashval < hashValueOfRequest) {
left = mid + 1;
}
}
TRACE("left:(%" B_PRId32 "), right:(%" B_PRId32 ")\n", left, right);
hashLowerBound<NodeEntry>(nodeEntry, left, right, hashValueOfRequest);

// We found our potential leaf block, now read leaf buffer

// First see the leaf block from NodeEntry and logical block offset
uint32 logicalBlock = B_BENDIAN_TO_HOST_INT32(nodeEntry[left].before);
// Now calculate File system Block of This logical block
Expand All @@ -362,19 +348,7 @@ NodeAttribute::Lookup(const char* name, size_t* nameLength)

delete header;

// LowerBound
while (left < right) {
mid = (left + right) / 2;
uint32 hashval = B_BENDIAN_TO_HOST_INT32(entry[mid].hashval);
if (hashval >= hashValueOfRequest) {
right = mid;
continue;
}
if (hashval < hashValueOfRequest) {
left = mid + 1;
}
}
TRACE("left:(%" B_PRId32 "), right:(%" B_PRId32 ")\n", left, right);
hashLowerBound<AttrLeafEntry>(entry, left, right, hashValueOfRequest);

while (B_BENDIAN_TO_HOST_INT32(entry[left].hashval)
== hashValueOfRequest) {
Expand Down

0 comments on commit 40b5272

Please sign in to comment.