]> git.proxmox.com Git - ceph.git/blame - ceph/src/spdk/test/unit/lib/blobfs/blobfs_async_ut/blobfs_async_ut.c
bump version to 19.2.0-pve1
[ceph.git] / ceph / src / spdk / test / unit / lib / blobfs / blobfs_async_ut / blobfs_async_ut.c
CommitLineData
7c673cae
FG
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
11fdf7f2 34#include "spdk/stdinc.h"
7c673cae
FG
35
36#include "CUnit/Basic.h"
37
9f95a23c 38#include "common/lib/ut_multithread.c"
7c673cae
FG
39
40#include "spdk_cunit.h"
11fdf7f2
TL
41#include "blobfs/blobfs.c"
42#include "blobfs/tree.c"
9f95a23c
TL
43#include "blob/blobstore.h"
44
45#include "spdk_internal/thread.h"
7c673cae 46
11fdf7f2 47#include "unit/lib/blob/bs_dev_common.c"
7c673cae
FG
48
49struct spdk_filesystem *g_fs;
50struct spdk_file *g_file;
51int g_fserrno;
9f95a23c
TL
52struct spdk_trace_histories *g_trace_histories;
53DEFINE_STUB_V(spdk_trace_add_register_fn, (struct spdk_trace_register_fn *reg_fn));
54DEFINE_STUB_V(spdk_trace_register_description, (const char *name,
55 uint16_t tpoint_id, uint8_t owner_type,
56 uint8_t object_type, uint8_t new_object,
57 uint8_t arg1_is_ptr, const char *arg1_name));
58DEFINE_STUB_V(_spdk_trace_record, (uint64_t tsc, uint16_t tpoint_id, uint16_t poller_id,
59 uint32_t size, uint64_t object_id, uint64_t arg1));
7c673cae 60
11fdf7f2
TL
61/* Return NULL to test hardcoded defaults. */
62struct spdk_conf_section *
63spdk_conf_find_section(struct spdk_conf *cp, const char *name)
64{
65 return NULL;
66}
67
68/* Return -1 to test hardcoded defaults. */
69int
70spdk_conf_section_get_intval(struct spdk_conf_section *sp, const char *key)
71{
72 return -1;
73}
74
7c673cae
FG
75static void
76fs_op_complete(void *ctx, int fserrno)
77{
78 g_fserrno = fserrno;
79}
80
81static void
82fs_op_with_handle_complete(void *ctx, struct spdk_filesystem *fs, int fserrno)
83{
84 g_fs = fs;
85 g_fserrno = fserrno;
86}
87
f67539c2
TL
88static void
89fs_poll_threads(void)
90{
91 poll_threads();
92 while (spdk_thread_poll(g_cache_pool_thread, 0, 0) > 0) {}
93}
94
7c673cae
FG
95static void
96fs_init(void)
97{
98 struct spdk_filesystem *fs;
11fdf7f2 99 struct spdk_bs_dev *dev;
7c673cae 100
11fdf7f2 101 dev = init_dev();
7c673cae 102
11fdf7f2 103 spdk_fs_init(dev, NULL, NULL, fs_op_with_handle_complete, NULL);
f67539c2 104 fs_poll_threads();
11fdf7f2 105 SPDK_CU_ASSERT_FATAL(g_fs != NULL);
7c673cae
FG
106 CU_ASSERT(g_fserrno == 0);
107 fs = g_fs;
9f95a23c 108 SPDK_CU_ASSERT_FATAL(fs->bs->dev == dev);
7c673cae 109
11fdf7f2 110 g_fserrno = 1;
7c673cae 111 spdk_fs_unload(fs, fs_op_complete, NULL);
f67539c2 112 fs_poll_threads();
7c673cae 113 CU_ASSERT(g_fserrno == 0);
7c673cae
FG
114}
115
116static void
117create_cb(void *ctx, int fserrno)
118{
119 g_fserrno = fserrno;
120}
121
122static void
123open_cb(void *ctx, struct spdk_file *f, int fserrno)
124{
125 g_fserrno = fserrno;
126 g_file = f;
127}
128
129static void
130delete_cb(void *ctx, int fserrno)
131{
132 g_fserrno = fserrno;
133}
134
135static void
136fs_open(void)
137{
138 struct spdk_filesystem *fs;
139 spdk_fs_iter iter;
11fdf7f2 140 struct spdk_bs_dev *dev;
7c673cae 141 struct spdk_file *file;
11fdf7f2 142 char name[257] = {'\0'};
7c673cae 143
11fdf7f2
TL
144 dev = init_dev();
145 memset(name, 'a', sizeof(name) - 1);
7c673cae 146
11fdf7f2 147 spdk_fs_init(dev, NULL, NULL, fs_op_with_handle_complete, NULL);
f67539c2 148 fs_poll_threads();
11fdf7f2 149 SPDK_CU_ASSERT_FATAL(g_fs != NULL);
7c673cae
FG
150 CU_ASSERT(g_fserrno == 0);
151 fs = g_fs;
9f95a23c 152 SPDK_CU_ASSERT_FATAL(fs->bs->dev == dev);
7c673cae 153
11fdf7f2
TL
154 g_fserrno = 0;
155 /* Open should fail, because the file name is too long. */
156 spdk_fs_open_file_async(fs, name, SPDK_BLOBFS_OPEN_CREATE, open_cb, NULL);
f67539c2 157 fs_poll_threads();
11fdf7f2
TL
158 CU_ASSERT(g_fserrno == -ENAMETOOLONG);
159
7c673cae
FG
160 g_fserrno = 0;
161 spdk_fs_open_file_async(fs, "file1", 0, open_cb, NULL);
f67539c2 162 fs_poll_threads();
7c673cae
FG
163 CU_ASSERT(g_fserrno == -ENOENT);
164
165 g_file = NULL;
166 g_fserrno = 1;
167 spdk_fs_open_file_async(fs, "file1", SPDK_BLOBFS_OPEN_CREATE, open_cb, NULL);
f67539c2 168 fs_poll_threads();
7c673cae
FG
169 CU_ASSERT(g_fserrno == 0);
170 SPDK_CU_ASSERT_FATAL(g_file != NULL);
171 CU_ASSERT(!strcmp("file1", g_file->name));
172 CU_ASSERT(g_file->ref_count == 1);
173
174 iter = spdk_fs_iter_first(fs);
175 CU_ASSERT(iter != NULL);
176 file = spdk_fs_iter_get_file(iter);
177 SPDK_CU_ASSERT_FATAL(file != NULL);
178 CU_ASSERT(!strcmp("file1", file->name));
179 iter = spdk_fs_iter_next(iter);
180 CU_ASSERT(iter == NULL);
181
182 g_fserrno = 0;
11fdf7f2 183 /* Delete should successful, we will mark the file as deleted. */
7c673cae 184 spdk_fs_delete_file_async(fs, "file1", delete_cb, NULL);
f67539c2 185 fs_poll_threads();
11fdf7f2 186 CU_ASSERT(g_fserrno == 0);
7c673cae
FG
187 CU_ASSERT(!TAILQ_EMPTY(&fs->files));
188
189 g_fserrno = 1;
190 spdk_file_close_async(g_file, fs_op_complete, NULL);
f67539c2 191 fs_poll_threads();
7c673cae 192 CU_ASSERT(g_fserrno == 0);
11fdf7f2
TL
193 CU_ASSERT(TAILQ_EMPTY(&fs->files));
194
195 g_fserrno = 1;
196 spdk_fs_unload(fs, fs_op_complete, NULL);
f67539c2 197 fs_poll_threads();
11fdf7f2 198 CU_ASSERT(g_fserrno == 0);
11fdf7f2
TL
199}
200
201static void
202fs_create(void)
203{
204 struct spdk_filesystem *fs;
205 struct spdk_bs_dev *dev;
206 char name[257] = {'\0'};
207
208 dev = init_dev();
209 memset(name, 'a', sizeof(name) - 1);
11fdf7f2
TL
210
211 spdk_fs_init(dev, NULL, NULL, fs_op_with_handle_complete, NULL);
f67539c2 212 fs_poll_threads();
11fdf7f2
TL
213 SPDK_CU_ASSERT_FATAL(g_fs != NULL);
214 CU_ASSERT(g_fserrno == 0);
215 fs = g_fs;
9f95a23c 216 SPDK_CU_ASSERT_FATAL(fs->bs->dev == dev);
7c673cae
FG
217
218 g_fserrno = 0;
11fdf7f2
TL
219 /* Create should fail, because the file name is too long. */
220 spdk_fs_create_file_async(fs, name, create_cb, NULL);
f67539c2 221 fs_poll_threads();
11fdf7f2
TL
222 CU_ASSERT(g_fserrno == -ENAMETOOLONG);
223
224 g_fserrno = 1;
225 spdk_fs_create_file_async(fs, "file1", create_cb, NULL);
f67539c2 226 fs_poll_threads();
11fdf7f2
TL
227 CU_ASSERT(g_fserrno == 0);
228
229 g_fserrno = 1;
230 spdk_fs_create_file_async(fs, "file1", create_cb, NULL);
f67539c2 231 fs_poll_threads();
11fdf7f2 232 CU_ASSERT(g_fserrno == -EEXIST);
7c673cae
FG
233
234 g_fserrno = 1;
235 spdk_fs_delete_file_async(fs, "file1", delete_cb, NULL);
f67539c2 236 fs_poll_threads();
7c673cae
FG
237 CU_ASSERT(g_fserrno == 0);
238 CU_ASSERT(TAILQ_EMPTY(&fs->files));
239
11fdf7f2 240 g_fserrno = 1;
7c673cae 241 spdk_fs_unload(fs, fs_op_complete, NULL);
f67539c2 242 fs_poll_threads();
7c673cae 243 CU_ASSERT(g_fserrno == 0);
7c673cae
FG
244}
245
246static void
247fs_truncate(void)
248{
249 struct spdk_filesystem *fs;
11fdf7f2 250 struct spdk_bs_dev *dev;
7c673cae 251
11fdf7f2 252 dev = init_dev();
7c673cae 253
11fdf7f2 254 spdk_fs_init(dev, NULL, NULL, fs_op_with_handle_complete, NULL);
f67539c2 255 fs_poll_threads();
7c673cae
FG
256 SPDK_CU_ASSERT_FATAL(g_fs != NULL);
257 CU_ASSERT(g_fserrno == 0);
258 fs = g_fs;
9f95a23c 259 SPDK_CU_ASSERT_FATAL(fs->bs->dev == dev);
7c673cae
FG
260
261 g_file = NULL;
262 g_fserrno = 1;
263 spdk_fs_open_file_async(fs, "file1", SPDK_BLOBFS_OPEN_CREATE, open_cb, NULL);
f67539c2 264 fs_poll_threads();
7c673cae
FG
265 CU_ASSERT(g_fserrno == 0);
266 SPDK_CU_ASSERT_FATAL(g_file != NULL);
267
268 g_fserrno = 1;
269 spdk_file_truncate_async(g_file, 18 * 1024 * 1024 + 1, fs_op_complete, NULL);
f67539c2 270 fs_poll_threads();
7c673cae
FG
271 CU_ASSERT(g_fserrno == 0);
272 CU_ASSERT(g_file->length == 18 * 1024 * 1024 + 1);
273
274 g_fserrno = 1;
275 spdk_file_truncate_async(g_file, 1, fs_op_complete, NULL);
f67539c2 276 fs_poll_threads();
7c673cae
FG
277 CU_ASSERT(g_fserrno == 0);
278 CU_ASSERT(g_file->length == 1);
279
280 g_fserrno = 1;
281 spdk_file_truncate_async(g_file, 18 * 1024 * 1024 + 1, fs_op_complete, NULL);
f67539c2 282 fs_poll_threads();
7c673cae
FG
283 CU_ASSERT(g_fserrno == 0);
284 CU_ASSERT(g_file->length == 18 * 1024 * 1024 + 1);
285
286 g_fserrno = 1;
287 spdk_file_close_async(g_file, fs_op_complete, NULL);
f67539c2 288 fs_poll_threads();
7c673cae
FG
289 CU_ASSERT(g_fserrno == 0);
290 CU_ASSERT(g_file->ref_count == 0);
291
292 g_fserrno = 1;
293 spdk_fs_delete_file_async(fs, "file1", delete_cb, NULL);
f67539c2 294 fs_poll_threads();
7c673cae
FG
295 CU_ASSERT(g_fserrno == 0);
296 CU_ASSERT(TAILQ_EMPTY(&fs->files));
297
11fdf7f2 298 g_fserrno = 1;
7c673cae 299 spdk_fs_unload(fs, fs_op_complete, NULL);
f67539c2 300 fs_poll_threads();
7c673cae 301 CU_ASSERT(g_fserrno == 0);
7c673cae
FG
302}
303
304static void
305fs_rename(void)
306{
307 struct spdk_filesystem *fs;
9f95a23c 308 struct spdk_file *file, *file2, *file_iter;
11fdf7f2 309 struct spdk_bs_dev *dev;
7c673cae 310
11fdf7f2 311 dev = init_dev();
7c673cae 312
11fdf7f2 313 spdk_fs_init(dev, NULL, NULL, fs_op_with_handle_complete, NULL);
f67539c2 314 fs_poll_threads();
7c673cae
FG
315 SPDK_CU_ASSERT_FATAL(g_fs != NULL);
316 CU_ASSERT(g_fserrno == 0);
317 fs = g_fs;
9f95a23c 318 SPDK_CU_ASSERT_FATAL(fs->bs->dev == dev);
7c673cae
FG
319
320 g_fserrno = 1;
321 spdk_fs_create_file_async(fs, "file1", create_cb, NULL);
f67539c2 322 fs_poll_threads();
7c673cae
FG
323 CU_ASSERT(g_fserrno == 0);
324
325 g_file = NULL;
326 g_fserrno = 1;
327 spdk_fs_open_file_async(fs, "file1", 0, open_cb, NULL);
f67539c2 328 fs_poll_threads();
7c673cae
FG
329 CU_ASSERT(g_fserrno == 0);
330 SPDK_CU_ASSERT_FATAL(g_file != NULL);
331 CU_ASSERT(g_file->ref_count == 1);
332
333 file = g_file;
334 g_file = NULL;
335 g_fserrno = 1;
336 spdk_file_close_async(file, fs_op_complete, NULL);
f67539c2 337 fs_poll_threads();
7c673cae
FG
338 CU_ASSERT(g_fserrno == 0);
339 SPDK_CU_ASSERT_FATAL(file->ref_count == 0);
340
341 g_file = NULL;
342 g_fserrno = 1;
343 spdk_fs_open_file_async(fs, "file2", SPDK_BLOBFS_OPEN_CREATE, open_cb, NULL);
f67539c2 344 fs_poll_threads();
7c673cae
FG
345 CU_ASSERT(g_fserrno == 0);
346 SPDK_CU_ASSERT_FATAL(g_file != NULL);
347 CU_ASSERT(g_file->ref_count == 1);
348
349 file2 = g_file;
350 g_file = NULL;
351 g_fserrno = 1;
352 spdk_file_close_async(file2, fs_op_complete, NULL);
f67539c2 353 fs_poll_threads();
7c673cae
FG
354 CU_ASSERT(g_fserrno == 0);
355 SPDK_CU_ASSERT_FATAL(file2->ref_count == 0);
356
357 /*
358 * Do a 3-way rename. This should delete the old "file2", then rename
359 * "file1" to "file2".
360 */
361 g_fserrno = 1;
362 spdk_fs_rename_file_async(fs, "file1", "file2", fs_op_complete, NULL);
f67539c2 363 fs_poll_threads();
7c673cae
FG
364 CU_ASSERT(g_fserrno == 0);
365 CU_ASSERT(file->ref_count == 0);
366 CU_ASSERT(!strcmp(file->name, "file2"));
367 CU_ASSERT(TAILQ_FIRST(&fs->files) == file);
368 CU_ASSERT(TAILQ_NEXT(file, tailq) == NULL);
369
370 g_fserrno = 0;
371 spdk_fs_delete_file_async(fs, "file1", delete_cb, NULL);
f67539c2 372 fs_poll_threads();
7c673cae
FG
373 CU_ASSERT(g_fserrno == -ENOENT);
374 CU_ASSERT(!TAILQ_EMPTY(&fs->files));
9f95a23c
TL
375 TAILQ_FOREACH(file_iter, &fs->files, tailq) {
376 if (file_iter == NULL) {
377 SPDK_CU_ASSERT_FATAL(false);
378 }
379 }
7c673cae
FG
380
381 g_fserrno = 1;
382 spdk_fs_delete_file_async(fs, "file2", delete_cb, NULL);
f67539c2 383 fs_poll_threads();
7c673cae
FG
384 CU_ASSERT(g_fserrno == 0);
385 CU_ASSERT(TAILQ_EMPTY(&fs->files));
386
11fdf7f2 387 g_fserrno = 1;
7c673cae 388 spdk_fs_unload(fs, fs_op_complete, NULL);
f67539c2
TL
389 fs_poll_threads();
390 CU_ASSERT(g_fserrno == 0);
391}
392
393static void
394fs_rw_async(void)
395{
396 struct spdk_filesystem *fs;
397 struct spdk_bs_dev *dev;
398 uint8_t w_buf[4096];
399 uint8_t r_buf[4096];
400
401 dev = init_dev();
402
403 spdk_fs_init(dev, NULL, NULL, fs_op_with_handle_complete, NULL);
404 fs_poll_threads();
405 SPDK_CU_ASSERT_FATAL(g_fs != NULL);
406 CU_ASSERT(g_fserrno == 0);
407 fs = g_fs;
408 SPDK_CU_ASSERT_FATAL(fs->bs->dev == dev);
409
410 g_file = NULL;
411 g_fserrno = 1;
412 spdk_fs_open_file_async(fs, "file1", SPDK_BLOBFS_OPEN_CREATE, open_cb, NULL);
413 fs_poll_threads();
414 CU_ASSERT(g_fserrno == 0);
415 SPDK_CU_ASSERT_FATAL(g_file != NULL);
416
417 /* Write file */
418 CU_ASSERT(g_file->length == 0);
419 g_fserrno = 1;
420 memset(w_buf, 0x5a, sizeof(w_buf));
421 spdk_file_write_async(g_file, fs->sync_target.sync_io_channel, w_buf, 0, 4096,
422 fs_op_complete, NULL);
423 fs_poll_threads();
424 CU_ASSERT(g_fserrno == 0);
425 CU_ASSERT(g_file->length == 4096);
426
427 /* Read file */
428 g_fserrno = 1;
429 memset(r_buf, 0x0, sizeof(r_buf));
430 spdk_file_read_async(g_file, fs->sync_target.sync_io_channel, r_buf, 0, 4096,
431 fs_op_complete, NULL);
432 fs_poll_threads();
433 CU_ASSERT(g_fserrno == 0);
434 CU_ASSERT(memcmp(r_buf, w_buf, sizeof(r_buf)) == 0);
435
436 g_fserrno = 1;
437 spdk_file_close_async(g_file, fs_op_complete, NULL);
438 fs_poll_threads();
439 CU_ASSERT(g_fserrno == 0);
440
441 g_fserrno = 1;
442 spdk_fs_unload(fs, fs_op_complete, NULL);
443 fs_poll_threads();
444 CU_ASSERT(g_fserrno == 0);
445}
446
447static void
448fs_writev_readv_async(void)
449{
450 struct spdk_filesystem *fs;
451 struct spdk_bs_dev *dev;
452 struct iovec w_iov[2];
453 struct iovec r_iov[2];
454 uint8_t w_buf[4096];
455 uint8_t r_buf[4096];
456
457 dev = init_dev();
458
459 spdk_fs_init(dev, NULL, NULL, fs_op_with_handle_complete, NULL);
460 fs_poll_threads();
461 SPDK_CU_ASSERT_FATAL(g_fs != NULL);
462 CU_ASSERT(g_fserrno == 0);
463 fs = g_fs;
464 SPDK_CU_ASSERT_FATAL(fs->bs->dev == dev);
465
466 g_file = NULL;
467 g_fserrno = 1;
468 spdk_fs_open_file_async(fs, "file1", SPDK_BLOBFS_OPEN_CREATE, open_cb, NULL);
469 fs_poll_threads();
470 CU_ASSERT(g_fserrno == 0);
471 SPDK_CU_ASSERT_FATAL(g_file != NULL);
472
473 /* Write file */
474 CU_ASSERT(g_file->length == 0);
475 g_fserrno = 1;
476 memset(w_buf, 0x5a, sizeof(w_buf));
477 w_iov[0].iov_base = w_buf;
478 w_iov[0].iov_len = 2048;
479 w_iov[1].iov_base = w_buf + 2048;
480 w_iov[1].iov_len = 2048;
481 spdk_file_writev_async(g_file, fs->sync_target.sync_io_channel,
482 w_iov, 2, 0, 4096, fs_op_complete, NULL);
483 fs_poll_threads();
484 CU_ASSERT(g_fserrno == 0);
485 CU_ASSERT(g_file->length == 4096);
486
487 /* Read file */
488 g_fserrno = 1;
489 memset(r_buf, 0x0, sizeof(r_buf));
490 r_iov[0].iov_base = r_buf;
491 r_iov[0].iov_len = 2048;
492 r_iov[1].iov_base = r_buf + 2048;
493 r_iov[1].iov_len = 2048;
494 spdk_file_readv_async(g_file, fs->sync_target.sync_io_channel,
495 r_iov, 2, 0, 4096, fs_op_complete, NULL);
496 fs_poll_threads();
497 CU_ASSERT(g_fserrno == 0);
498 CU_ASSERT(memcmp(r_buf, w_buf, sizeof(r_buf)) == 0);
499
500 /* Overwrite file with block aligned */
501 g_fserrno = 1;
502 memset(w_buf, 0x6a, sizeof(w_buf));
503 w_iov[0].iov_base = w_buf;
504 w_iov[0].iov_len = 2048;
505 w_iov[1].iov_base = w_buf + 2048;
506 w_iov[1].iov_len = 2048;
507 spdk_file_writev_async(g_file, fs->sync_target.sync_io_channel,
508 w_iov, 2, 0, 4096, fs_op_complete, NULL);
509 fs_poll_threads();
510 CU_ASSERT(g_fserrno == 0);
511 CU_ASSERT(g_file->length == 4096);
512
513 /* Read file to verify the overwritten data */
514 g_fserrno = 1;
515 memset(r_buf, 0x0, sizeof(r_buf));
516 r_iov[0].iov_base = r_buf;
517 r_iov[0].iov_len = 2048;
518 r_iov[1].iov_base = r_buf + 2048;
519 r_iov[1].iov_len = 2048;
520 spdk_file_readv_async(g_file, fs->sync_target.sync_io_channel,
521 r_iov, 2, 0, 4096, fs_op_complete, NULL);
522 fs_poll_threads();
523 CU_ASSERT(g_fserrno == 0);
524 CU_ASSERT(memcmp(r_buf, w_buf, sizeof(r_buf)) == 0);
525
526 g_fserrno = 1;
527 spdk_file_close_async(g_file, fs_op_complete, NULL);
528 fs_poll_threads();
529 CU_ASSERT(g_fserrno == 0);
530
531 g_fserrno = 1;
532 spdk_fs_unload(fs, fs_op_complete, NULL);
533 fs_poll_threads();
7c673cae 534 CU_ASSERT(g_fserrno == 0);
7c673cae
FG
535}
536
537static void
538tree_find_buffer_ut(void)
539{
540 struct cache_tree *root;
541 struct cache_tree *level1_0;
542 struct cache_tree *level0_0_0;
543 struct cache_tree *level0_0_12;
544 struct cache_buffer *leaf_0_0_4;
545 struct cache_buffer *leaf_0_12_8;
546 struct cache_buffer *leaf_9_23_15;
547 struct cache_buffer *buffer;
548
549 level1_0 = calloc(1, sizeof(struct cache_tree));
550 SPDK_CU_ASSERT_FATAL(level1_0 != NULL);
551 level0_0_0 = calloc(1, sizeof(struct cache_tree));
552 SPDK_CU_ASSERT_FATAL(level0_0_0 != NULL);
553 level0_0_12 = calloc(1, sizeof(struct cache_tree));
554 SPDK_CU_ASSERT_FATAL(level0_0_12 != NULL);
555 leaf_0_0_4 = calloc(1, sizeof(struct cache_buffer));
556 SPDK_CU_ASSERT_FATAL(leaf_0_0_4 != NULL);
557 leaf_0_12_8 = calloc(1, sizeof(struct cache_buffer));
558 SPDK_CU_ASSERT_FATAL(leaf_0_12_8 != NULL);
559 leaf_9_23_15 = calloc(1, sizeof(struct cache_buffer));
560 SPDK_CU_ASSERT_FATAL(leaf_9_23_15 != NULL);
561
562 level1_0->level = 1;
563 level0_0_0->level = 0;
564 level0_0_12->level = 0;
565
566 leaf_0_0_4->offset = CACHE_BUFFER_SIZE * 4;
567 level0_0_0->u.buffer[4] = leaf_0_0_4;
568 level0_0_0->present_mask |= (1ULL << 4);
569
570 leaf_0_12_8->offset = CACHE_TREE_LEVEL_SIZE(1) * 12 + CACHE_BUFFER_SIZE * 8;
571 level0_0_12->u.buffer[8] = leaf_0_12_8;
572 level0_0_12->present_mask |= (1ULL << 8);
573
574 level1_0->u.tree[0] = level0_0_0;
575 level1_0->present_mask |= (1ULL << 0);
576 level1_0->u.tree[12] = level0_0_12;
577 level1_0->present_mask |= (1ULL << 12);
578
f67539c2 579 buffer = tree_find_buffer(NULL, 0);
7c673cae
FG
580 CU_ASSERT(buffer == NULL);
581
f67539c2 582 buffer = tree_find_buffer(level0_0_0, 0);
7c673cae
FG
583 CU_ASSERT(buffer == NULL);
584
f67539c2 585 buffer = tree_find_buffer(level0_0_0, CACHE_TREE_LEVEL_SIZE(0) + 1);
7c673cae
FG
586 CU_ASSERT(buffer == NULL);
587
f67539c2 588 buffer = tree_find_buffer(level0_0_0, leaf_0_0_4->offset);
7c673cae
FG
589 CU_ASSERT(buffer == leaf_0_0_4);
590
f67539c2 591 buffer = tree_find_buffer(level1_0, leaf_0_0_4->offset);
7c673cae
FG
592 CU_ASSERT(buffer == leaf_0_0_4);
593
f67539c2 594 buffer = tree_find_buffer(level1_0, leaf_0_12_8->offset);
7c673cae
FG
595 CU_ASSERT(buffer == leaf_0_12_8);
596
f67539c2 597 buffer = tree_find_buffer(level1_0, leaf_0_12_8->offset + CACHE_BUFFER_SIZE - 1);
7c673cae
FG
598 CU_ASSERT(buffer == leaf_0_12_8);
599
f67539c2 600 buffer = tree_find_buffer(level1_0, leaf_0_12_8->offset - 1);
7c673cae
FG
601 CU_ASSERT(buffer == NULL);
602
603 leaf_9_23_15->offset = CACHE_TREE_LEVEL_SIZE(2) * 9 +
604 CACHE_TREE_LEVEL_SIZE(1) * 23 +
605 CACHE_BUFFER_SIZE * 15;
f67539c2 606 root = tree_insert_buffer(level1_0, leaf_9_23_15);
7c673cae 607 CU_ASSERT(root != level1_0);
f67539c2 608 buffer = tree_find_buffer(root, leaf_9_23_15->offset);
7c673cae 609 CU_ASSERT(buffer == leaf_9_23_15);
f67539c2 610 tree_free_buffers(root);
7c673cae
FG
611 free(root);
612}
613
11fdf7f2
TL
614static void
615channel_ops(void)
616{
617 struct spdk_filesystem *fs;
618 struct spdk_bs_dev *dev;
619 struct spdk_io_channel *channel;
620
621 dev = init_dev();
11fdf7f2
TL
622
623 spdk_fs_init(dev, NULL, NULL, fs_op_with_handle_complete, NULL);
f67539c2 624 fs_poll_threads();
11fdf7f2
TL
625 SPDK_CU_ASSERT_FATAL(g_fs != NULL);
626 CU_ASSERT(g_fserrno == 0);
627 fs = g_fs;
9f95a23c 628 SPDK_CU_ASSERT_FATAL(fs->bs->dev == dev);
11fdf7f2
TL
629
630 channel = spdk_fs_alloc_io_channel(fs);
631 CU_ASSERT(channel != NULL);
632
633 spdk_fs_free_io_channel(channel);
634
635 g_fserrno = 1;
636 spdk_fs_unload(fs, fs_op_complete, NULL);
f67539c2 637 fs_poll_threads();
11fdf7f2
TL
638 CU_ASSERT(g_fserrno == 0);
639 g_fs = NULL;
11fdf7f2
TL
640}
641
642static void
643channel_ops_sync(void)
644{
645 struct spdk_filesystem *fs;
646 struct spdk_bs_dev *dev;
9f95a23c 647 struct spdk_fs_thread_ctx *channel;
11fdf7f2
TL
648
649 dev = init_dev();
11fdf7f2
TL
650
651 spdk_fs_init(dev, NULL, NULL, fs_op_with_handle_complete, NULL);
f67539c2 652 fs_poll_threads();
11fdf7f2
TL
653 SPDK_CU_ASSERT_FATAL(g_fs != NULL);
654 CU_ASSERT(g_fserrno == 0);
655 fs = g_fs;
9f95a23c 656 SPDK_CU_ASSERT_FATAL(fs->bs->dev == dev);
11fdf7f2 657
9f95a23c 658 channel = spdk_fs_alloc_thread_ctx(fs);
11fdf7f2
TL
659 CU_ASSERT(channel != NULL);
660
9f95a23c 661 spdk_fs_free_thread_ctx(channel);
11fdf7f2
TL
662
663 g_fserrno = 1;
664 spdk_fs_unload(fs, fs_op_complete, NULL);
f67539c2 665 fs_poll_threads();
11fdf7f2
TL
666 CU_ASSERT(g_fserrno == 0);
667 g_fs = NULL;
11fdf7f2
TL
668}
669
7c673cae
FG
670int main(int argc, char **argv)
671{
672 CU_pSuite suite = NULL;
673 unsigned int num_failures;
674
f67539c2
TL
675 CU_set_error_action(CUEA_ABORT);
676 CU_initialize_registry();
7c673cae 677
11fdf7f2 678 suite = CU_add_suite("blobfs_async_ut", NULL, NULL);
7c673cae 679
f67539c2
TL
680 CU_ADD_TEST(suite, fs_init);
681 CU_ADD_TEST(suite, fs_open);
682 CU_ADD_TEST(suite, fs_create);
683 CU_ADD_TEST(suite, fs_truncate);
684 CU_ADD_TEST(suite, fs_rename);
685 CU_ADD_TEST(suite, fs_rw_async);
686 CU_ADD_TEST(suite, fs_writev_readv_async);
687 CU_ADD_TEST(suite, tree_find_buffer_ut);
688 CU_ADD_TEST(suite, channel_ops);
689 CU_ADD_TEST(suite, channel_ops_sync);
7c673cae 690
9f95a23c
TL
691 allocate_threads(1);
692 set_thread(0);
693
7c673cae
FG
694 g_dev_buffer = calloc(1, DEV_BUFFER_SIZE);
695 CU_basic_set_mode(CU_BRM_VERBOSE);
696 CU_basic_run_tests();
697 num_failures = CU_get_number_of_failures();
698 CU_cleanup_registry();
699 free(g_dev_buffer);
9f95a23c
TL
700
701 free_threads();
702
7c673cae
FG
703 return num_failures;
704}