1 /*- 2 * BSD LICENSE 3 * 4 * Copyright (c) Intel Corporation. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 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 16 * distribution. 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. 20 * 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. 32 */ 33 34 #include "spdk/stdinc.h" 35 36 #include "spdk/blobfs.h" 37 #include "spdk/env.h" 38 #include "spdk/log.h" 39 #include "spdk/thread.h" 40 #include "spdk/barrier.h" 41 #include "spdk_internal/thread.h" 42 43 #include "spdk_cunit.h" 44 #include "unit/lib/blob/bs_dev_common.c" 45 #include "common/lib/test_env.c" 46 #include "blobfs/blobfs.c" 47 #include "blobfs/tree.c" 48 49 struct spdk_filesystem *g_fs; 50 struct spdk_file *g_file; 51 int g_fserrno; 52 53 /* Return NULL to test hardcoded defaults. */ 54 struct spdk_conf_section * 55 spdk_conf_find_section(struct spdk_conf *cp, const char *name) 56 { 57 return NULL; 58 } 59 60 /* Return -1 to test hardcoded defaults. */ 61 int 62 spdk_conf_section_get_intval(struct spdk_conf_section *sp, const char *key) 63 { 64 return -1; 65 } 66 67 struct ut_request { 68 fs_request_fn fn; 69 void *arg; 70 volatile int done; 71 int from_ut; 72 }; 73 74 static struct ut_request *g_req = NULL; 75 static pthread_mutex_t g_mutex = PTHREAD_MUTEX_INITIALIZER; 76 77 static void 78 send_request(fs_request_fn fn, void *arg) 79 { 80 struct ut_request *req; 81 82 req = calloc(1, sizeof(*req)); 83 assert(req != NULL); 84 req->fn = fn; 85 req->arg = arg; 86 req->done = 0; 87 req->from_ut = 0; 88 89 pthread_mutex_lock(&g_mutex); 90 g_req = req; 91 pthread_mutex_unlock(&g_mutex); 92 } 93 94 static void 95 ut_send_request(fs_request_fn fn, void *arg) 96 { 97 struct ut_request req; 98 99 100 req.fn = fn; 101 req.arg = arg; 102 req.done = 0; 103 req.from_ut = 1; 104 105 pthread_mutex_lock(&g_mutex); 106 g_req = &req; 107 pthread_mutex_unlock(&g_mutex); 108 109 while (1) { 110 pthread_mutex_lock(&g_mutex); 111 if (req.done == 1) { 112 pthread_mutex_unlock(&g_mutex); 113 break; 114 } 115 pthread_mutex_unlock(&g_mutex); 116 } 117 118 /* 119 * Make sure the address of the local req variable is not in g_req when we exit this 120 * function to make static analysis tools happy. 121 */ 122 g_req = NULL; 123 } 124 125 static void 126 fs_op_complete(void *ctx, int fserrno) 127 { 128 g_fserrno = fserrno; 129 } 130 131 static void 132 fs_op_with_handle_complete(void *ctx, struct spdk_filesystem *fs, int fserrno) 133 { 134 g_fs = fs; 135 g_fserrno = fserrno; 136 } 137 138 static void 139 _fs_init(void *arg) 140 { 141 struct spdk_thread *thread; 142 struct spdk_bs_dev *dev; 143 144 g_fs = NULL; 145 g_fserrno = -1; 146 dev = init_dev(); 147 spdk_fs_init(dev, NULL, send_request, fs_op_with_handle_complete, NULL); 148 thread = spdk_get_thread(); 149 while (spdk_thread_poll(thread, 0, 0) > 0) {} 150 151 SPDK_CU_ASSERT_FATAL(g_fs != NULL); 152 SPDK_CU_ASSERT_FATAL(g_fs->bdev == dev); 153 CU_ASSERT(g_fserrno == 0); 154 } 155 156 static void 157 _fs_unload(void *arg) 158 { 159 struct spdk_thread *thread; 160 161 g_fserrno = -1; 162 spdk_fs_unload(g_fs, fs_op_complete, NULL); 163 thread = spdk_get_thread(); 164 while (spdk_thread_poll(thread, 0, 0) > 0) {} 165 CU_ASSERT(g_fserrno == 0); 166 g_fs = NULL; 167 } 168 169 static void 170 cache_write(void) 171 { 172 uint64_t length; 173 int rc; 174 char buf[100]; 175 struct spdk_io_channel *channel; 176 177 ut_send_request(_fs_init, NULL); 178 179 channel = spdk_fs_alloc_io_channel_sync(g_fs); 180 181 rc = spdk_fs_open_file(g_fs, channel, "testfile", SPDK_BLOBFS_OPEN_CREATE, &g_file); 182 CU_ASSERT(rc == 0); 183 SPDK_CU_ASSERT_FATAL(g_file != NULL); 184 185 length = (4 * 1024 * 1024); 186 rc = spdk_file_truncate(g_file, channel, length); 187 CU_ASSERT(rc == 0); 188 189 spdk_file_write(g_file, channel, buf, 0, sizeof(buf)); 190 191 CU_ASSERT(spdk_file_get_length(g_file) == length); 192 193 rc = spdk_file_truncate(g_file, channel, sizeof(buf)); 194 CU_ASSERT(rc == 0); 195 196 spdk_file_close(g_file, channel); 197 rc = spdk_fs_delete_file(g_fs, channel, "testfile"); 198 CU_ASSERT(rc == 0); 199 200 rc = spdk_fs_delete_file(g_fs, channel, "testfile"); 201 CU_ASSERT(rc == -ENOENT); 202 203 spdk_fs_free_io_channel(channel); 204 205 ut_send_request(_fs_unload, NULL); 206 } 207 208 static void 209 cache_write_null_buffer(void) 210 { 211 uint64_t length; 212 int rc; 213 struct spdk_io_channel *channel; 214 struct spdk_thread *thread; 215 216 ut_send_request(_fs_init, NULL); 217 218 channel = spdk_fs_alloc_io_channel_sync(g_fs); 219 220 rc = spdk_fs_open_file(g_fs, channel, "testfile", SPDK_BLOBFS_OPEN_CREATE, &g_file); 221 CU_ASSERT(rc == 0); 222 SPDK_CU_ASSERT_FATAL(g_file != NULL); 223 224 length = 0; 225 rc = spdk_file_truncate(g_file, channel, length); 226 CU_ASSERT(rc == 0); 227 228 rc = spdk_file_write(g_file, channel, NULL, 0, 0); 229 CU_ASSERT(rc == 0); 230 231 spdk_file_close(g_file, channel); 232 rc = spdk_fs_delete_file(g_fs, channel, "testfile"); 233 CU_ASSERT(rc == 0); 234 235 spdk_fs_free_io_channel(channel); 236 237 thread = spdk_get_thread(); 238 while (spdk_thread_poll(thread, 0, 0) > 0) {} 239 240 ut_send_request(_fs_unload, NULL); 241 } 242 243 static void 244 fs_create_sync(void) 245 { 246 int rc; 247 struct spdk_io_channel *channel; 248 struct spdk_thread *thread; 249 250 ut_send_request(_fs_init, NULL); 251 252 channel = spdk_fs_alloc_io_channel_sync(g_fs); 253 CU_ASSERT(channel != NULL); 254 255 rc = spdk_fs_create_file(g_fs, channel, "testfile"); 256 CU_ASSERT(rc == 0); 257 258 /* Create should fail, because the file already exists. */ 259 rc = spdk_fs_create_file(g_fs, channel, "testfile"); 260 CU_ASSERT(rc != 0); 261 262 rc = spdk_fs_delete_file(g_fs, channel, "testfile"); 263 CU_ASSERT(rc == 0); 264 265 spdk_fs_free_io_channel(channel); 266 267 thread = spdk_get_thread(); 268 while (spdk_thread_poll(thread, 0, 0) > 0) {} 269 270 ut_send_request(_fs_unload, NULL); 271 } 272 273 static void 274 cache_append_no_cache(void) 275 { 276 int rc; 277 char buf[100]; 278 struct spdk_io_channel *channel; 279 struct spdk_thread *thread; 280 281 ut_send_request(_fs_init, NULL); 282 283 channel = spdk_fs_alloc_io_channel_sync(g_fs); 284 285 rc = spdk_fs_open_file(g_fs, channel, "testfile", SPDK_BLOBFS_OPEN_CREATE, &g_file); 286 CU_ASSERT(rc == 0); 287 SPDK_CU_ASSERT_FATAL(g_file != NULL); 288 289 spdk_file_write(g_file, channel, buf, 0 * sizeof(buf), sizeof(buf)); 290 CU_ASSERT(spdk_file_get_length(g_file) == 1 * sizeof(buf)); 291 spdk_file_write(g_file, channel, buf, 1 * sizeof(buf), sizeof(buf)); 292 CU_ASSERT(spdk_file_get_length(g_file) == 2 * sizeof(buf)); 293 spdk_file_sync(g_file, channel); 294 cache_free_buffers(g_file); 295 spdk_file_write(g_file, channel, buf, 2 * sizeof(buf), sizeof(buf)); 296 CU_ASSERT(spdk_file_get_length(g_file) == 3 * sizeof(buf)); 297 spdk_file_write(g_file, channel, buf, 3 * sizeof(buf), sizeof(buf)); 298 CU_ASSERT(spdk_file_get_length(g_file) == 4 * sizeof(buf)); 299 spdk_file_write(g_file, channel, buf, 4 * sizeof(buf), sizeof(buf)); 300 CU_ASSERT(spdk_file_get_length(g_file) == 5 * sizeof(buf)); 301 302 spdk_file_close(g_file, channel); 303 rc = spdk_fs_delete_file(g_fs, channel, "testfile"); 304 CU_ASSERT(rc == 0); 305 306 spdk_fs_free_io_channel(channel); 307 308 thread = spdk_get_thread(); 309 while (spdk_thread_poll(thread, 0, 0) > 0) {} 310 311 ut_send_request(_fs_unload, NULL); 312 } 313 314 static void 315 fs_delete_file_without_close(void) 316 { 317 int rc; 318 struct spdk_io_channel *channel; 319 struct spdk_file *file; 320 struct spdk_thread *thread; 321 322 ut_send_request(_fs_init, NULL); 323 channel = spdk_fs_alloc_io_channel_sync(g_fs); 324 CU_ASSERT(channel != NULL); 325 326 rc = spdk_fs_open_file(g_fs, channel, "testfile", SPDK_BLOBFS_OPEN_CREATE, &g_file); 327 CU_ASSERT(rc == 0); 328 SPDK_CU_ASSERT_FATAL(g_file != NULL); 329 330 rc = spdk_fs_delete_file(g_fs, channel, "testfile"); 331 CU_ASSERT(rc == 0); 332 CU_ASSERT(g_file->ref_count != 0); 333 CU_ASSERT(g_file->is_deleted == true); 334 335 rc = spdk_fs_open_file(g_fs, channel, "testfile", 0, &file); 336 CU_ASSERT(rc != 0); 337 338 spdk_file_close(g_file, channel); 339 340 rc = spdk_fs_open_file(g_fs, channel, "testfile", 0, &file); 341 CU_ASSERT(rc != 0); 342 343 spdk_fs_free_io_channel(channel); 344 345 thread = spdk_get_thread(); 346 while (spdk_thread_poll(thread, 0, 0) > 0) {} 347 348 ut_send_request(_fs_unload, NULL); 349 350 } 351 352 static void 353 terminate_spdk_thread(void *arg) 354 { 355 spdk_thread_exit(spdk_get_thread()); 356 pthread_exit(NULL); 357 } 358 359 static void * 360 spdk_thread(void *arg) 361 { 362 struct spdk_thread *thread; 363 struct ut_request *req; 364 365 thread = spdk_thread_create("thread1"); 366 spdk_set_thread(thread); 367 368 while (1) { 369 pthread_mutex_lock(&g_mutex); 370 if (g_req != NULL) { 371 req = g_req; 372 req->fn(req->arg); 373 req->done = 1; 374 if (!req->from_ut) { 375 free(req); 376 } 377 g_req = NULL; 378 } 379 pthread_mutex_unlock(&g_mutex); 380 381 spdk_thread_poll(thread, 0, 0); 382 } 383 384 return NULL; 385 } 386 387 int main(int argc, char **argv) 388 { 389 struct spdk_thread *thread; 390 CU_pSuite suite = NULL; 391 pthread_t spdk_tid; 392 unsigned int num_failures; 393 394 if (CU_initialize_registry() != CUE_SUCCESS) { 395 return CU_get_error(); 396 } 397 398 suite = CU_add_suite("blobfs_sync_ut", NULL, NULL); 399 if (suite == NULL) { 400 CU_cleanup_registry(); 401 return CU_get_error(); 402 } 403 404 if ( 405 CU_add_test(suite, "write", cache_write) == NULL || 406 CU_add_test(suite, "write_null_buffer", cache_write_null_buffer) == NULL || 407 CU_add_test(suite, "create_sync", fs_create_sync) == NULL || 408 CU_add_test(suite, "append_no_cache", cache_append_no_cache) == NULL || 409 CU_add_test(suite, "delete_file_without_close", fs_delete_file_without_close) == NULL 410 ) { 411 CU_cleanup_registry(); 412 return CU_get_error(); 413 } 414 415 thread = spdk_thread_create("thread0"); 416 spdk_set_thread(thread); 417 418 pthread_create(&spdk_tid, NULL, spdk_thread, NULL); 419 g_dev_buffer = calloc(1, DEV_BUFFER_SIZE); 420 CU_basic_set_mode(CU_BRM_VERBOSE); 421 CU_basic_run_tests(); 422 num_failures = CU_get_number_of_failures(); 423 CU_cleanup_registry(); 424 free(g_dev_buffer); 425 send_request(terminate_spdk_thread, NULL); 426 pthread_join(spdk_tid, NULL); 427 spdk_thread_exit(thread); 428 return num_failures; 429 } 430