-
Notifications
You must be signed in to change notification settings - Fork 94
/
Copy pathextent.c
72 lines (65 loc) · 2.03 KB
/
extent.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <linux/fs.h>
#include <linux/kernel.h>
#include "simplefs.h"
/* Search for the extent containing the target block. Binary search is used
* for efficiency.
*
* Returns the first unused file index if not found.
* Returns -1 if the target block is out of range.
*/
uint32_t simplefs_ext_search(struct simplefs_file_ei_block *index,
uint32_t iblock)
{
/* First, find the first unused file index with binary search.
* It will be our right boundary for actual binary search and the return
* value when the file index is not found.
*/
uint32_t start = 0;
uint32_t end = SIMPLEFS_MAX_EXTENTS - 1;
uint32_t boundary;
uint32_t end_block;
uint32_t end_len;
while (start < end) {
uint32_t mid = start + (end - start) / 2;
if (index->extents[mid].ee_start == 0) {
end = mid;
} else {
start = mid + 1;
}
}
if (index->extents[end].ee_start == 0) {
boundary = end;
} else {
/* File index full */
boundary = end + 1;
}
if (boundary == 0) /* No used file index */
return boundary;
/* try finding target block using binary search */
start = 0;
end = boundary - 1;
while (start < end) {
uint32_t mid = start + (end - start) / 2;
uint32_t block = index->extents[mid].ee_block;
uint32_t len = index->extents[mid].ee_len;
if (iblock >= block && iblock < block + len) {
/* found before search finished */
return mid;
}
if (iblock < block) {
end = mid;
} else {
start = mid + 1;
}
}
/* Return 'end' if it directs to valid block.
* Return 'boundary' if index is not found and eiblock has remaining space
*/
end_block = index->extents[end].ee_block;
end_len = index->extents[end].ee_len;
if (iblock >= end_block && iblock < end_len)
return end;
if (boundary < SIMPLEFS_MAX_EXTENTS)
return boundary;
return boundary;
}