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.
42 #include "spdk/blobfs.h"
45 #include "spdk/io_channel.h"
46 #include "spdk/barrier.h"
48 #include "spdk_cunit.h"
49 #include "lib/blob/bs_dev_common.c"
50 #include "lib/test_env.c"
54 struct spdk_filesystem
*g_fs
;
55 struct spdk_file
*g_file
;
58 struct spdk_bs_dev g_dev
;
67 volatile struct ut_request
*g_req
= NULL
;
68 volatile int g_phase
= 0;
71 send_request(fs_request_fn fn
, void *arg
)
73 struct ut_request
*req
;
75 req
= calloc(1, sizeof(*req
));
88 ut_send_request(fs_request_fn fn
, void *arg
)
90 struct ut_request req
;
100 while (req
.done
== 0)
105 fs_op_complete(void *ctx
, int fserrno
)
111 fs_op_with_handle_complete(void *ctx
, struct spdk_filesystem
*fs
, int fserrno
)
122 spdk_fs_init(&g_dev
, send_request
, fs_op_with_handle_complete
, NULL
);
123 SPDK_CU_ASSERT_FATAL(g_fs
!= NULL
);
124 CU_ASSERT(g_fserrno
== 0);
128 _fs_unload(void *arg
)
131 spdk_fs_unload(g_fs
, fs_op_complete
, NULL
);
132 CU_ASSERT(g_fserrno
== 0);
142 struct spdk_io_channel
*channel
;
144 ut_send_request(_fs_init
, NULL
);
146 spdk_allocate_thread();
147 channel
= spdk_fs_alloc_io_channel_sync(g_fs
, SPDK_IO_PRIORITY_DEFAULT
);
149 rc
= spdk_fs_open_file(g_fs
, channel
, "testfile", SPDK_BLOBFS_OPEN_CREATE
, &g_file
);
151 SPDK_CU_ASSERT_FATAL(g_file
!= NULL
);
153 length
= (4 * 1024 * 1024);
154 spdk_file_truncate(g_file
, channel
, length
);
156 spdk_file_write(g_file
, channel
, buf
, 0, sizeof(buf
));
158 CU_ASSERT(spdk_file_get_length(g_file
) == length
);
160 spdk_file_truncate(g_file
, channel
, sizeof(buf
));
162 spdk_file_close(g_file
, channel
);
163 rc
= spdk_fs_delete_file(g_fs
, channel
, "testfile");
166 rc
= spdk_fs_delete_file(g_fs
, channel
, "testfile");
167 CU_ASSERT(rc
== -ENOENT
);
169 spdk_fs_free_io_channel(channel
);
172 ut_send_request(_fs_unload
, NULL
);
176 cache_append_no_cache(void)
180 struct spdk_io_channel
*channel
;
182 ut_send_request(_fs_init
, NULL
);
184 spdk_allocate_thread();
185 channel
= spdk_fs_alloc_io_channel_sync(g_fs
, SPDK_IO_PRIORITY_DEFAULT
);
187 rc
= spdk_fs_open_file(g_fs
, channel
, "testfile", SPDK_BLOBFS_OPEN_CREATE
, &g_file
);
189 SPDK_CU_ASSERT_FATAL(g_file
!= NULL
);
191 spdk_file_write(g_file
, channel
, buf
, 0 * sizeof(buf
), sizeof(buf
));
192 CU_ASSERT(spdk_file_get_length(g_file
) == 1 * sizeof(buf
));
193 spdk_file_write(g_file
, channel
, buf
, 1 * sizeof(buf
), sizeof(buf
));
194 CU_ASSERT(spdk_file_get_length(g_file
) == 2 * sizeof(buf
));
195 spdk_file_sync(g_file
, channel
);
196 cache_free_buffers(g_file
);
197 spdk_file_write(g_file
, channel
, buf
, 2 * sizeof(buf
), sizeof(buf
));
198 CU_ASSERT(spdk_file_get_length(g_file
) == 3 * sizeof(buf
));
199 spdk_file_write(g_file
, channel
, buf
, 3 * sizeof(buf
), sizeof(buf
));
200 CU_ASSERT(spdk_file_get_length(g_file
) == 4 * sizeof(buf
));
201 spdk_file_write(g_file
, channel
, buf
, 4 * sizeof(buf
), sizeof(buf
));
202 CU_ASSERT(spdk_file_get_length(g_file
) == 5 * sizeof(buf
));
204 spdk_file_close(g_file
, channel
);
205 rc
= spdk_fs_delete_file(g_fs
, channel
, "testfile");
208 spdk_fs_free_io_channel(channel
);
211 ut_send_request(_fs_unload
, NULL
);
215 terminate_spdk_thread(void *arg
)
222 spdk_thread(void *arg
)
224 struct ut_request
*req
;
226 spdk_allocate_thread();
230 if (phase
!= g_phase
) {
245 int main(int argc
, char **argv
)
247 CU_pSuite suite
= NULL
;
249 unsigned int num_failures
;
251 if (CU_initialize_registry() != CUE_SUCCESS
) {
252 return CU_get_error();
255 suite
= CU_add_suite("cache_ut", NULL
, NULL
);
257 CU_cleanup_registry();
258 return CU_get_error();
262 CU_add_test(suite
, "write", cache_write
) == NULL
||
263 CU_add_test(suite
, "append_no_cache", cache_append_no_cache
) == NULL
265 CU_cleanup_registry();
266 return CU_get_error();
270 pthread_create(&spdk_tid
, NULL
, spdk_thread
, NULL
);
271 g_dev_buffer
= calloc(1, DEV_BUFFER_SIZE
);
272 CU_basic_set_mode(CU_BRM_VERBOSE
);
273 CU_basic_run_tests();
274 num_failures
= CU_get_number_of_failures();
275 CU_cleanup_registry();
277 send_request(terminate_spdk_thread
, NULL
);
278 pthread_join(spdk_tid
, NULL
);