4 * Copyright (c) Intel Corporation.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 #include "spdk/stdinc.h"
36 #include "spdk/blobfs.h"
39 #include "spdk/queue.h"
40 #include "spdk/assert.h"
42 #include "spdk_internal/log.h"
44 uint32_t g_fs_cache_buffer_shift
= CACHE_BUFFER_SHIFT_DEFAULT
;
47 tree_find_buffer(struct cache_tree
*tree
, uint64_t offset
)
51 while (tree
!= NULL
) {
52 index
= offset
/ CACHE_TREE_LEVEL_SIZE(tree
->level
);
53 if (index
>= CACHE_TREE_WIDTH
) {
56 if (tree
->level
== 0) {
57 return tree
->u
.buffer
[index
];
59 offset
&= CACHE_TREE_LEVEL_MASK(tree
->level
);
60 tree
= tree
->u
.tree
[index
];
68 tree_find_filled_buffer(struct cache_tree
*tree
, uint64_t offset
)
70 struct cache_buffer
*buf
;
72 buf
= tree_find_buffer(tree
, offset
);
73 if (buf
!= NULL
&& buf
->bytes_filled
> 0) {
81 tree_insert_buffer(struct cache_tree
*root
, struct cache_buffer
*buffer
)
83 struct cache_tree
*tree
;
84 uint64_t index
, offset
;
86 offset
= buffer
->offset
;
87 while (offset
>= CACHE_TREE_LEVEL_SIZE(root
->level
+ 1)) {
88 if (root
->present_mask
!= 0) {
89 tree
= calloc(1, sizeof(*tree
));
90 tree
->level
= root
->level
+ 1;
91 tree
->u
.tree
[0] = root
;
93 root
->present_mask
= 0x1ULL
;
100 while (tree
->level
> 0) {
101 index
= offset
/ CACHE_TREE_LEVEL_SIZE(tree
->level
);
102 assert(index
< CACHE_TREE_WIDTH
);
103 offset
&= CACHE_TREE_LEVEL_MASK(tree
->level
);
104 if (tree
->u
.tree
[index
] == NULL
) {
105 tree
->u
.tree
[index
] = calloc(1, sizeof(*tree
));
106 tree
->u
.tree
[index
]->level
= tree
->level
- 1;
107 tree
->present_mask
|= (1ULL << index
);
109 tree
= tree
->u
.tree
[index
];
112 index
= offset
/ CACHE_BUFFER_SIZE
;
113 assert(index
< CACHE_TREE_WIDTH
);
114 assert(tree
->u
.buffer
[index
] == NULL
);
115 tree
->u
.buffer
[index
] = buffer
;
116 tree
->present_mask
|= (1ULL << index
);
121 tree_remove_buffer(struct cache_tree
*tree
, struct cache_buffer
*buffer
)
123 struct cache_tree
*child
;
126 index
= CACHE_TREE_INDEX(tree
->level
, buffer
->offset
);
128 if (tree
->level
== 0) {
129 assert(tree
->u
.buffer
[index
] != NULL
);
130 assert(buffer
== tree
->u
.buffer
[index
]);
131 tree
->present_mask
&= ~(1ULL << index
);
132 tree
->u
.buffer
[index
] = NULL
;
133 cache_buffer_free(buffer
);
137 child
= tree
->u
.tree
[index
];
138 assert(child
!= NULL
);
139 tree_remove_buffer(child
, buffer
);
140 if (child
->present_mask
== 0) {
141 tree
->present_mask
&= ~(1ULL << index
);
142 tree
->u
.tree
[index
] = NULL
;
148 tree_free_buffers(struct cache_tree
*tree
)
150 struct cache_buffer
*buffer
;
151 struct cache_tree
*child
;
154 if (tree
->present_mask
== 0) {
158 if (tree
->level
== 0) {
159 for (i
= 0; i
< CACHE_TREE_WIDTH
; i
++) {
160 buffer
= tree
->u
.buffer
[i
];
161 if (buffer
!= NULL
&& buffer
->in_progress
== false &&
162 buffer
->bytes_filled
== buffer
->bytes_flushed
) {
163 cache_buffer_free(buffer
);
164 tree
->u
.buffer
[i
] = NULL
;
165 tree
->present_mask
&= ~(1ULL << i
);
169 for (i
= 0; i
< CACHE_TREE_WIDTH
; i
++) {
170 child
= tree
->u
.tree
[i
];
172 tree_free_buffers(child
);
173 if (child
->present_mask
== 0) {
175 tree
->u
.tree
[i
] = NULL
;
176 tree
->present_mask
&= ~(1ULL << i
);