LCOV - code coverage report
Current view: top level - spdk/test/unit/lib/blobfs/blobfs_sync_ut - blobfs_sync_ut.c (source / functions) Hit Total Coverage
Test: Combined Lines: 339 341 99.4 %
Date: 2024-07-16 01:22:15 Functions: 22 26 84.6 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 39 66 59.1 %

           Branch data     Line data    Source code
       1                 :            : /*   SPDX-License-Identifier: BSD-3-Clause
       2                 :            :  *   Copyright (C) 2017 Intel Corporation.
       3                 :            :  *   All rights reserved.
       4                 :            :  */
       5                 :            : 
       6                 :            : #include "spdk/stdinc.h"
       7                 :            : 
       8                 :            : #include "spdk/blobfs.h"
       9                 :            : #include "spdk/env.h"
      10                 :            : #include "spdk/log.h"
      11                 :            : #include "spdk/barrier.h"
      12                 :            : #include "thread/thread_internal.h"
      13                 :            : 
      14                 :            : #include "spdk_internal/cunit.h"
      15                 :            : #include "unit/lib/blob/bs_dev_common.c"
      16                 :            : #include "common/lib/test_env.c"
      17                 :            : #include "blobfs/blobfs.c"
      18                 :            : #include "blobfs/tree.c"
      19                 :            : 
      20                 :            : struct spdk_filesystem *g_fs;
      21                 :            : struct spdk_file *g_file;
      22                 :            : int g_fserrno;
      23                 :            : struct spdk_thread *g_dispatch_thread = NULL;
      24                 :            : 
      25                 :            : struct ut_request {
      26                 :            :         fs_request_fn fn;
      27                 :            :         void *arg;
      28                 :            :         volatile int done;
      29                 :            : };
      30                 :            : 
      31         [ #  # ]:          0 : DEFINE_STUB(spdk_memory_domain_memzero, int, (struct spdk_memory_domain *src_domain,
      32                 :            :                 void *src_domain_ctx, struct iovec *iov, uint32_t iovcnt, void (*cpl_cb)(void *, int),
      33                 :            :                 void *cpl_cb_arg), 0);
      34         [ #  # ]:          0 : DEFINE_STUB(spdk_mempool_lookup, struct spdk_mempool *, (const char *name), NULL);
      35                 :            : 
      36                 :            : static void
      37                 :        159 : send_request(fs_request_fn fn, void *arg)
      38                 :            : {
      39                 :        159 :         spdk_thread_send_msg(g_dispatch_thread, (spdk_msg_fn)fn, arg);
      40                 :        159 : }
      41                 :            : 
      42                 :            : static void
      43                 :         87 : ut_call_fn(void *arg)
      44                 :            : {
      45                 :         87 :         struct ut_request *req = arg;
      46                 :            : 
      47                 :         87 :         req->fn(req->arg);
      48                 :         87 :         req->done = 1;
      49                 :         87 : }
      50                 :            : 
      51                 :            : static void
      52                 :         87 : ut_send_request(fs_request_fn fn, void *arg)
      53                 :            : {
      54                 :         87 :         struct ut_request req;
      55                 :            : 
      56                 :         87 :         req.fn = fn;
      57                 :         87 :         req.arg = arg;
      58                 :         87 :         req.done = 0;
      59                 :            : 
      60                 :         87 :         spdk_thread_send_msg(g_dispatch_thread, ut_call_fn, &req);
      61                 :            : 
      62                 :            :         /* Wait for this to finish */
      63         [ +  + ]:   52330242 :         while (req.done == 0) { }
      64                 :         87 : }
      65                 :            : 
      66                 :            : static void
      67                 :         33 : fs_op_complete(void *ctx, int fserrno)
      68                 :            : {
      69                 :         33 :         g_fserrno = fserrno;
      70                 :         33 : }
      71                 :            : 
      72                 :            : static void
      73                 :         33 : fs_op_with_handle_complete(void *ctx, struct spdk_filesystem *fs, int fserrno)
      74                 :            : {
      75                 :         33 :         g_fs = fs;
      76                 :         33 :         g_fserrno = fserrno;
      77                 :         33 : }
      78                 :            : 
      79                 :            : static void
      80                 :        105 : fs_thread_poll(void)
      81                 :            : {
      82                 :            :         struct spdk_thread *thread;
      83                 :            : 
      84                 :        105 :         thread = spdk_get_thread();
      85         [ +  + ]:        426 :         while (spdk_thread_poll(thread, 0, 0) > 0) {}
      86         [ +  + ]:        186 :         while (spdk_thread_poll(g_cache_pool_thread, 0, 0) > 0) {}
      87                 :        105 : }
      88                 :            : 
      89                 :            : static void
      90                 :         27 : _fs_init(void *arg)
      91                 :            : {
      92                 :            :         struct spdk_bs_dev *dev;
      93                 :            : 
      94                 :         27 :         g_fs = NULL;
      95                 :         27 :         g_fserrno = -1;
      96                 :         27 :         dev = init_dev();
      97                 :         27 :         spdk_fs_init(dev, NULL, send_request, fs_op_with_handle_complete, NULL);
      98                 :            : 
      99                 :         27 :         fs_thread_poll();
     100                 :            : 
     101         [ -  + ]:         27 :         SPDK_CU_ASSERT_FATAL(g_fs != NULL);
     102         [ -  + ]:         27 :         SPDK_CU_ASSERT_FATAL(g_fs->bdev == dev);
     103                 :         27 :         CU_ASSERT(g_fserrno == 0);
     104                 :         27 : }
     105                 :            : 
     106                 :            : static void
     107                 :          6 : _fs_load(void *arg)
     108                 :            : {
     109                 :            :         struct spdk_bs_dev *dev;
     110                 :            : 
     111                 :          6 :         g_fs = NULL;
     112                 :          6 :         g_fserrno = -1;
     113                 :          6 :         dev = init_dev();
     114                 :          6 :         spdk_fs_load(dev, send_request, fs_op_with_handle_complete, NULL);
     115                 :            : 
     116                 :          6 :         fs_thread_poll();
     117                 :            : 
     118         [ -  + ]:          6 :         SPDK_CU_ASSERT_FATAL(g_fs != NULL);
     119         [ -  + ]:          6 :         SPDK_CU_ASSERT_FATAL(g_fs->bdev == dev);
     120                 :          6 :         CU_ASSERT(g_fserrno == 0);
     121                 :          6 : }
     122                 :            : 
     123                 :            : static void
     124                 :         33 : _fs_unload(void *arg)
     125                 :            : {
     126                 :         33 :         g_fserrno = -1;
     127                 :         33 :         spdk_fs_unload(g_fs, fs_op_complete, NULL);
     128                 :            : 
     129                 :         33 :         fs_thread_poll();
     130                 :            : 
     131                 :         33 :         CU_ASSERT(g_fserrno == 0);
     132                 :         33 :         g_fs = NULL;
     133                 :         33 : }
     134                 :            : 
     135                 :            : static void
     136                 :         18 : _nop(void *arg)
     137                 :            : {
     138                 :         18 : }
     139                 :            : 
     140                 :            : static void
     141                 :          3 : cache_read_after_write(void)
     142                 :            : {
     143                 :            :         uint64_t length;
     144                 :            :         int rc;
     145                 :          3 :         char w_buf[100], r_buf[100];
     146                 :            :         struct spdk_fs_thread_ctx *channel;
     147                 :          3 :         struct spdk_file_stat stat = {0};
     148                 :            : 
     149                 :          3 :         ut_send_request(_fs_init, NULL);
     150                 :            : 
     151                 :          3 :         channel = spdk_fs_alloc_thread_ctx(g_fs);
     152                 :            : 
     153                 :          3 :         rc = spdk_fs_open_file(g_fs, channel, "testfile", SPDK_BLOBFS_OPEN_CREATE, &g_file);
     154                 :          3 :         CU_ASSERT(rc == 0);
     155         [ -  + ]:          3 :         SPDK_CU_ASSERT_FATAL(g_file != NULL);
     156                 :            : 
     157                 :          3 :         length = (4 * 1024 * 1024);
     158                 :          3 :         rc = spdk_file_truncate(g_file, channel, length);
     159                 :          3 :         CU_ASSERT(rc == 0);
     160                 :            : 
     161                 :          3 :         memset(w_buf, 0x5a, sizeof(w_buf));
     162                 :          3 :         spdk_file_write(g_file, channel, w_buf, 0, sizeof(w_buf));
     163                 :            : 
     164                 :          3 :         CU_ASSERT(spdk_file_get_length(g_file) == length);
     165                 :            : 
     166                 :          3 :         rc = spdk_file_truncate(g_file, channel, sizeof(w_buf));
     167                 :          3 :         CU_ASSERT(rc == 0);
     168                 :            : 
     169                 :          3 :         spdk_file_close(g_file, channel);
     170                 :            : 
     171                 :          3 :         fs_thread_poll();
     172                 :            : 
     173                 :          3 :         rc = spdk_fs_file_stat(g_fs, channel, "testfile", &stat);
     174                 :          3 :         CU_ASSERT(rc == 0);
     175                 :          3 :         CU_ASSERT(sizeof(w_buf) == stat.size);
     176                 :            : 
     177                 :          3 :         rc = spdk_fs_open_file(g_fs, channel, "testfile", 0, &g_file);
     178                 :          3 :         CU_ASSERT(rc == 0);
     179         [ -  + ]:          3 :         SPDK_CU_ASSERT_FATAL(g_file != NULL);
     180                 :            : 
     181                 :          3 :         memset(r_buf, 0, sizeof(r_buf));
     182                 :          3 :         spdk_file_read(g_file, channel, r_buf, 0, sizeof(r_buf));
     183                 :          3 :         CU_ASSERT(memcmp(w_buf, r_buf, sizeof(r_buf)) == 0);
     184                 :            : 
     185                 :          3 :         spdk_file_close(g_file, channel);
     186                 :            : 
     187                 :          3 :         fs_thread_poll();
     188                 :            : 
     189                 :          3 :         rc = spdk_fs_delete_file(g_fs, channel, "testfile");
     190                 :          3 :         CU_ASSERT(rc == 0);
     191                 :            : 
     192                 :          3 :         rc = spdk_fs_delete_file(g_fs, channel, "testfile");
     193                 :          3 :         CU_ASSERT(rc == -ENOENT);
     194                 :            : 
     195                 :          3 :         spdk_fs_free_thread_ctx(channel);
     196                 :            : 
     197                 :          3 :         ut_send_request(_fs_unload, NULL);
     198                 :          3 : }
     199                 :            : 
     200                 :            : static void
     201                 :          3 : file_length(void)
     202                 :            : {
     203                 :            :         int rc;
     204                 :            :         char *buf;
     205                 :            :         uint64_t buf_length;
     206                 :            :         volatile uint64_t *length_flushed;
     207                 :            :         struct spdk_fs_thread_ctx *channel;
     208                 :          3 :         struct spdk_file_stat stat = {0};
     209                 :            : 
     210                 :          3 :         ut_send_request(_fs_init, NULL);
     211                 :            : 
     212                 :          3 :         channel = spdk_fs_alloc_thread_ctx(g_fs);
     213                 :            : 
     214                 :          3 :         g_file = NULL;
     215                 :          3 :         rc = spdk_fs_open_file(g_fs, channel, "testfile", SPDK_BLOBFS_OPEN_CREATE, &g_file);
     216                 :          3 :         CU_ASSERT(rc == 0);
     217         [ -  + ]:          3 :         SPDK_CU_ASSERT_FATAL(g_file != NULL);
     218                 :            : 
     219                 :            :         /* Write one CACHE_BUFFER.  Filling at least one cache buffer triggers
     220                 :            :          * a flush to disk.
     221                 :            :          */
     222                 :          3 :         buf_length = CACHE_BUFFER_SIZE;
     223                 :          3 :         buf = calloc(1, buf_length);
     224                 :          3 :         spdk_file_write(g_file, channel, buf, 0, buf_length);
     225                 :          3 :         free(buf);
     226                 :            : 
     227                 :            :         /* Spin until all of the data has been flushed to the SSD.  There's been no
     228                 :            :          * sync operation yet, so the xattr on the file is still 0.
     229                 :            :          *
     230                 :            :          * length_flushed: This variable is modified by a different thread in this unit
     231                 :            :          * test. So we need to dereference it as a volatile to ensure the value is always
     232                 :            :          * re-read.
     233                 :            :          */
     234                 :          3 :         length_flushed = &g_file->length_flushed;
     235         [ +  + ]:      27126 :         while (*length_flushed != buf_length) {}
     236                 :            : 
     237                 :            :         /* Close the file.  This causes an implicit sync which should write the
     238                 :            :          * length_flushed value as the "length" xattr on the file.
     239                 :            :          */
     240                 :          3 :         spdk_file_close(g_file, channel);
     241                 :            : 
     242                 :          3 :         fs_thread_poll();
     243                 :            : 
     244                 :          3 :         rc = spdk_fs_file_stat(g_fs, channel, "testfile", &stat);
     245                 :          3 :         CU_ASSERT(rc == 0);
     246                 :          3 :         CU_ASSERT(buf_length == stat.size);
     247                 :            : 
     248                 :          3 :         spdk_fs_free_thread_ctx(channel);
     249                 :            : 
     250                 :            :         /* Unload and reload the filesystem.  The file length will be
     251                 :            :          * read during load from the length xattr.  We want to make sure
     252                 :            :          * it matches what was written when the file was originally
     253                 :            :          * written and closed.
     254                 :            :          */
     255                 :          3 :         ut_send_request(_fs_unload, NULL);
     256                 :            : 
     257                 :          3 :         ut_send_request(_fs_load, NULL);
     258                 :            : 
     259                 :          3 :         channel = spdk_fs_alloc_thread_ctx(g_fs);
     260                 :            : 
     261                 :          3 :         rc = spdk_fs_file_stat(g_fs, channel, "testfile", &stat);
     262                 :          3 :         CU_ASSERT(rc == 0);
     263                 :          3 :         CU_ASSERT(buf_length == stat.size);
     264                 :            : 
     265                 :          3 :         g_file = NULL;
     266                 :          3 :         rc = spdk_fs_open_file(g_fs, channel, "testfile", 0, &g_file);
     267                 :          3 :         CU_ASSERT(rc == 0);
     268         [ -  + ]:          3 :         SPDK_CU_ASSERT_FATAL(g_file != NULL);
     269                 :            : 
     270                 :          3 :         spdk_file_close(g_file, channel);
     271                 :            : 
     272                 :          3 :         fs_thread_poll();
     273                 :            : 
     274                 :          3 :         rc = spdk_fs_delete_file(g_fs, channel, "testfile");
     275                 :          3 :         CU_ASSERT(rc == 0);
     276                 :            : 
     277                 :          3 :         spdk_fs_free_thread_ctx(channel);
     278                 :            : 
     279                 :          3 :         ut_send_request(_fs_unload, NULL);
     280                 :          3 : }
     281                 :            : 
     282                 :            : static void
     283                 :          3 : append_write_to_extend_blob(void)
     284                 :            : {
     285                 :            :         uint64_t blob_size, buf_length;
     286                 :          3 :         char *buf, append_buf[64];
     287                 :            :         int rc;
     288                 :            :         struct spdk_fs_thread_ctx *channel;
     289                 :            : 
     290                 :          3 :         ut_send_request(_fs_init, NULL);
     291                 :            : 
     292                 :          3 :         channel = spdk_fs_alloc_thread_ctx(g_fs);
     293                 :            : 
     294                 :            :         /* create a file and write the file with blob_size - 1 data length */
     295                 :          3 :         rc = spdk_fs_open_file(g_fs, channel, "testfile", SPDK_BLOBFS_OPEN_CREATE, &g_file);
     296                 :          3 :         CU_ASSERT(rc == 0);
     297         [ -  + ]:          3 :         SPDK_CU_ASSERT_FATAL(g_file != NULL);
     298                 :            : 
     299                 :          3 :         blob_size = __file_get_blob_size(g_file);
     300                 :            : 
     301                 :          3 :         buf_length = blob_size - 1;
     302                 :          3 :         buf = calloc(1, buf_length);
     303                 :          3 :         rc = spdk_file_write(g_file, channel, buf, 0, buf_length);
     304                 :          3 :         CU_ASSERT(rc == 0);
     305                 :          3 :         free(buf);
     306                 :            : 
     307                 :          3 :         spdk_file_close(g_file, channel);
     308                 :          3 :         fs_thread_poll();
     309                 :          3 :         spdk_fs_free_thread_ctx(channel);
     310                 :          3 :         ut_send_request(_fs_unload, NULL);
     311                 :            : 
     312                 :            :         /* load existing file and write extra 2 bytes to cross blob boundary */
     313                 :          3 :         ut_send_request(_fs_load, NULL);
     314                 :            : 
     315                 :          3 :         channel = spdk_fs_alloc_thread_ctx(g_fs);
     316                 :          3 :         g_file = NULL;
     317                 :          3 :         rc = spdk_fs_open_file(g_fs, channel, "testfile", 0, &g_file);
     318                 :          3 :         CU_ASSERT(rc == 0);
     319         [ -  + ]:          3 :         SPDK_CU_ASSERT_FATAL(g_file != NULL);
     320                 :            : 
     321                 :          3 :         CU_ASSERT(g_file->length == buf_length);
     322                 :          3 :         CU_ASSERT(g_file->last == NULL);
     323                 :          3 :         CU_ASSERT(g_file->append_pos == buf_length);
     324                 :            : 
     325                 :          3 :         rc = spdk_file_write(g_file, channel, append_buf, buf_length, 2);
     326                 :          3 :         CU_ASSERT(rc == 0);
     327                 :          3 :         CU_ASSERT(2 * blob_size == __file_get_blob_size(g_file));
     328                 :          3 :         spdk_file_close(g_file, channel);
     329                 :          3 :         fs_thread_poll();
     330                 :          3 :         CU_ASSERT(g_file->length == buf_length + 2);
     331                 :            : 
     332                 :          3 :         spdk_fs_free_thread_ctx(channel);
     333                 :          3 :         ut_send_request(_fs_unload, NULL);
     334                 :          3 : }
     335                 :            : 
     336                 :            : static void
     337                 :          3 : partial_buffer(void)
     338                 :            : {
     339                 :            :         int rc;
     340                 :            :         char *buf;
     341                 :            :         uint64_t buf_length;
     342                 :            :         struct spdk_fs_thread_ctx *channel;
     343                 :          3 :         struct spdk_file_stat stat = {0};
     344                 :            : 
     345                 :          3 :         ut_send_request(_fs_init, NULL);
     346                 :            : 
     347                 :          3 :         channel = spdk_fs_alloc_thread_ctx(g_fs);
     348                 :            : 
     349                 :          3 :         g_file = NULL;
     350                 :          3 :         rc = spdk_fs_open_file(g_fs, channel, "testfile", SPDK_BLOBFS_OPEN_CREATE, &g_file);
     351                 :          3 :         CU_ASSERT(rc == 0);
     352         [ -  + ]:          3 :         SPDK_CU_ASSERT_FATAL(g_file != NULL);
     353                 :            : 
     354                 :            :         /* Write one CACHE_BUFFER plus one byte.  Filling at least one cache buffer triggers
     355                 :            :          * a flush to disk.  We want to make sure the extra byte is not implicitly flushed.
     356                 :            :          * It should only get flushed once we sync or close the file.
     357                 :            :          */
     358                 :          3 :         buf_length = CACHE_BUFFER_SIZE + 1;
     359                 :          3 :         buf = calloc(1, buf_length);
     360                 :          3 :         spdk_file_write(g_file, channel, buf, 0, buf_length);
     361                 :          3 :         free(buf);
     362                 :            : 
     363                 :            :         /* Send some nop messages to the dispatch thread.  This will ensure any of the
     364                 :            :          * pending write operations are completed.  A well-functioning blobfs should only
     365                 :            :          * issue one write for the filled CACHE_BUFFER - a buggy one might try to write
     366                 :            :          * the extra byte.  So do a bunch of _nops to make sure all of them (even the buggy
     367                 :            :          * ones) get a chance to run.  Note that we can't just send a message to the
     368                 :            :          * dispatch thread to call spdk_thread_poll() because the messages are themselves
     369                 :            :          * run in the context of spdk_thread_poll().
     370                 :            :          */
     371                 :          3 :         ut_send_request(_nop, NULL);
     372                 :          3 :         ut_send_request(_nop, NULL);
     373                 :          3 :         ut_send_request(_nop, NULL);
     374                 :          3 :         ut_send_request(_nop, NULL);
     375                 :          3 :         ut_send_request(_nop, NULL);
     376                 :          3 :         ut_send_request(_nop, NULL);
     377                 :            : 
     378                 :          3 :         CU_ASSERT(g_file->length_flushed == CACHE_BUFFER_SIZE);
     379                 :            : 
     380                 :            :         /* Close the file.  This causes an implicit sync which should write the
     381                 :            :          * length_flushed value as the "length" xattr on the file.
     382                 :            :          */
     383                 :          3 :         spdk_file_close(g_file, channel);
     384                 :            : 
     385                 :          3 :         fs_thread_poll();
     386                 :            : 
     387                 :          3 :         rc = spdk_fs_file_stat(g_fs, channel, "testfile", &stat);
     388                 :          3 :         CU_ASSERT(rc == 0);
     389                 :          3 :         CU_ASSERT(buf_length == stat.size);
     390                 :            : 
     391                 :          3 :         rc = spdk_fs_delete_file(g_fs, channel, "testfile");
     392                 :          3 :         CU_ASSERT(rc == 0);
     393                 :            : 
     394                 :          3 :         spdk_fs_free_thread_ctx(channel);
     395                 :            : 
     396                 :          3 :         ut_send_request(_fs_unload, NULL);
     397                 :          3 : }
     398                 :            : 
     399                 :            : static void
     400                 :          3 : cache_write_null_buffer(void)
     401                 :            : {
     402                 :            :         uint64_t length;
     403                 :            :         int rc;
     404                 :            :         struct spdk_fs_thread_ctx *channel;
     405                 :            :         struct spdk_thread *thread;
     406                 :            : 
     407                 :          3 :         ut_send_request(_fs_init, NULL);
     408                 :            : 
     409                 :          3 :         channel = spdk_fs_alloc_thread_ctx(g_fs);
     410                 :            : 
     411                 :          3 :         rc = spdk_fs_open_file(g_fs, channel, "testfile", SPDK_BLOBFS_OPEN_CREATE, &g_file);
     412                 :          3 :         CU_ASSERT(rc == 0);
     413         [ -  + ]:          3 :         SPDK_CU_ASSERT_FATAL(g_file != NULL);
     414                 :            : 
     415                 :          3 :         length = 0;
     416                 :          3 :         rc = spdk_file_truncate(g_file, channel, length);
     417                 :          3 :         CU_ASSERT(rc == 0);
     418                 :            : 
     419                 :          3 :         rc = spdk_file_write(g_file, channel, NULL, 0, 0);
     420                 :          3 :         CU_ASSERT(rc == 0);
     421                 :            : 
     422                 :          3 :         spdk_file_close(g_file, channel);
     423                 :            : 
     424                 :          3 :         fs_thread_poll();
     425                 :            : 
     426                 :          3 :         rc = spdk_fs_delete_file(g_fs, channel, "testfile");
     427                 :          3 :         CU_ASSERT(rc == 0);
     428                 :            : 
     429                 :          3 :         spdk_fs_free_thread_ctx(channel);
     430                 :            : 
     431                 :          3 :         thread = spdk_get_thread();
     432         [ -  + ]:          3 :         while (spdk_thread_poll(thread, 0, 0) > 0) {}
     433                 :            : 
     434                 :          3 :         ut_send_request(_fs_unload, NULL);
     435                 :          3 : }
     436                 :            : 
     437                 :            : static void
     438                 :          3 : fs_create_sync(void)
     439                 :            : {
     440                 :            :         int rc;
     441                 :            :         struct spdk_fs_thread_ctx *channel;
     442                 :            : 
     443                 :          3 :         ut_send_request(_fs_init, NULL);
     444                 :            : 
     445                 :          3 :         channel = spdk_fs_alloc_thread_ctx(g_fs);
     446                 :          3 :         CU_ASSERT(channel != NULL);
     447                 :            : 
     448                 :          3 :         rc = spdk_fs_create_file(g_fs, channel, "testfile");
     449                 :          3 :         CU_ASSERT(rc == 0);
     450                 :            : 
     451                 :            :         /* Create should fail, because the file already exists. */
     452                 :          3 :         rc = spdk_fs_create_file(g_fs, channel, "testfile");
     453                 :          3 :         CU_ASSERT(rc != 0);
     454                 :            : 
     455                 :          3 :         rc = spdk_fs_delete_file(g_fs, channel, "testfile");
     456                 :          3 :         CU_ASSERT(rc == 0);
     457                 :            : 
     458                 :          3 :         spdk_fs_free_thread_ctx(channel);
     459                 :            : 
     460                 :          3 :         fs_thread_poll();
     461                 :            : 
     462                 :          3 :         ut_send_request(_fs_unload, NULL);
     463                 :          3 : }
     464                 :            : 
     465                 :            : static void
     466                 :          3 : fs_rename_sync(void)
     467                 :            : {
     468                 :            :         int rc;
     469                 :            :         struct spdk_fs_thread_ctx *channel;
     470                 :            : 
     471                 :          3 :         ut_send_request(_fs_init, NULL);
     472                 :            : 
     473                 :          3 :         channel = spdk_fs_alloc_thread_ctx(g_fs);
     474                 :          3 :         CU_ASSERT(channel != NULL);
     475                 :            : 
     476                 :          3 :         rc = spdk_fs_open_file(g_fs, channel, "testfile", SPDK_BLOBFS_OPEN_CREATE, &g_file);
     477                 :          3 :         CU_ASSERT(rc == 0);
     478         [ -  + ]:          3 :         SPDK_CU_ASSERT_FATAL(g_file != NULL);
     479                 :            : 
     480         [ -  + ]:          3 :         CU_ASSERT(strcmp(spdk_file_get_name(g_file), "testfile") == 0);
     481                 :            : 
     482                 :          3 :         rc = spdk_fs_rename_file(g_fs, channel, "testfile", "newtestfile");
     483                 :          3 :         CU_ASSERT(rc == 0);
     484         [ -  + ]:          3 :         CU_ASSERT(strcmp(spdk_file_get_name(g_file), "newtestfile") == 0);
     485                 :            : 
     486                 :          3 :         spdk_file_close(g_file, channel);
     487                 :            : 
     488                 :          3 :         fs_thread_poll();
     489                 :            : 
     490                 :          3 :         spdk_fs_free_thread_ctx(channel);
     491                 :            : 
     492                 :          3 :         ut_send_request(_fs_unload, NULL);
     493                 :          3 : }
     494                 :            : 
     495                 :            : static void
     496                 :          3 : cache_append_no_cache(void)
     497                 :            : {
     498                 :            :         int rc;
     499                 :          3 :         char buf[100];
     500                 :            :         struct spdk_fs_thread_ctx *channel;
     501                 :            : 
     502                 :          3 :         ut_send_request(_fs_init, NULL);
     503                 :            : 
     504                 :          3 :         channel = spdk_fs_alloc_thread_ctx(g_fs);
     505                 :            : 
     506                 :          3 :         rc = spdk_fs_open_file(g_fs, channel, "testfile", SPDK_BLOBFS_OPEN_CREATE, &g_file);
     507                 :          3 :         CU_ASSERT(rc == 0);
     508         [ -  + ]:          3 :         SPDK_CU_ASSERT_FATAL(g_file != NULL);
     509                 :            : 
     510                 :          3 :         spdk_file_write(g_file, channel, buf, 0 * sizeof(buf), sizeof(buf));
     511                 :          3 :         CU_ASSERT(spdk_file_get_length(g_file) == 1 * sizeof(buf));
     512                 :          3 :         spdk_file_write(g_file, channel, buf, 1 * sizeof(buf), sizeof(buf));
     513                 :          3 :         CU_ASSERT(spdk_file_get_length(g_file) == 2 * sizeof(buf));
     514                 :          3 :         spdk_file_sync(g_file, channel);
     515                 :            : 
     516                 :          3 :         fs_thread_poll();
     517                 :            : 
     518                 :          3 :         spdk_file_write(g_file, channel, buf, 2 * sizeof(buf), sizeof(buf));
     519                 :          3 :         CU_ASSERT(spdk_file_get_length(g_file) == 3 * sizeof(buf));
     520                 :          3 :         spdk_file_write(g_file, channel, buf, 3 * sizeof(buf), sizeof(buf));
     521                 :          3 :         CU_ASSERT(spdk_file_get_length(g_file) == 4 * sizeof(buf));
     522                 :          3 :         spdk_file_write(g_file, channel, buf, 4 * sizeof(buf), sizeof(buf));
     523                 :          3 :         CU_ASSERT(spdk_file_get_length(g_file) == 5 * sizeof(buf));
     524                 :            : 
     525                 :          3 :         spdk_file_close(g_file, channel);
     526                 :            : 
     527                 :          3 :         fs_thread_poll();
     528                 :            : 
     529                 :          3 :         rc = spdk_fs_delete_file(g_fs, channel, "testfile");
     530                 :          3 :         CU_ASSERT(rc == 0);
     531                 :            : 
     532                 :          3 :         spdk_fs_free_thread_ctx(channel);
     533                 :            : 
     534                 :          3 :         ut_send_request(_fs_unload, NULL);
     535                 :          3 : }
     536                 :            : 
     537                 :            : static void
     538                 :          3 : fs_delete_file_without_close(void)
     539                 :            : {
     540                 :            :         int rc;
     541                 :            :         struct spdk_fs_thread_ctx *channel;
     542                 :          3 :         struct spdk_file *file;
     543                 :            : 
     544                 :          3 :         ut_send_request(_fs_init, NULL);
     545                 :          3 :         channel = spdk_fs_alloc_thread_ctx(g_fs);
     546                 :          3 :         CU_ASSERT(channel != NULL);
     547                 :            : 
     548                 :          3 :         rc = spdk_fs_open_file(g_fs, channel, "testfile", SPDK_BLOBFS_OPEN_CREATE, &g_file);
     549                 :          3 :         CU_ASSERT(rc == 0);
     550         [ -  + ]:          3 :         SPDK_CU_ASSERT_FATAL(g_file != NULL);
     551                 :            : 
     552                 :          3 :         rc = spdk_fs_delete_file(g_fs, channel, "testfile");
     553                 :          3 :         CU_ASSERT(rc == 0);
     554                 :          3 :         CU_ASSERT(g_file->ref_count != 0);
     555         [ -  + ]:          3 :         CU_ASSERT(g_file->is_deleted == true);
     556                 :            : 
     557                 :          3 :         rc = spdk_fs_open_file(g_fs, channel, "testfile", 0, &file);
     558                 :          3 :         CU_ASSERT(rc != 0);
     559                 :            : 
     560                 :          3 :         spdk_file_close(g_file, channel);
     561                 :            : 
     562                 :          3 :         fs_thread_poll();
     563                 :            : 
     564                 :          3 :         rc = spdk_fs_open_file(g_fs, channel, "testfile", 0, &file);
     565                 :          3 :         CU_ASSERT(rc != 0);
     566                 :            : 
     567                 :          3 :         spdk_fs_free_thread_ctx(channel);
     568                 :            : 
     569                 :          3 :         ut_send_request(_fs_unload, NULL);
     570                 :            : 
     571                 :          3 : }
     572                 :            : 
     573                 :            : static bool g_thread_exit = false;
     574                 :            : 
     575                 :            : static void
     576                 :          3 : terminate_spdk_thread(void *arg)
     577                 :            : {
     578                 :          3 :         g_thread_exit = true;
     579                 :          3 : }
     580                 :            : 
     581                 :            : static void *
     582                 :          3 : spdk_thread(void *arg)
     583                 :            : {
     584                 :          3 :         struct spdk_thread *thread = arg;
     585                 :            : 
     586                 :          3 :         spdk_set_thread(thread);
     587                 :            : 
     588   [ +  +  +  + ]:     145736 :         while (!g_thread_exit) {
     589                 :     145733 :                 spdk_thread_poll(thread, 0, 0);
     590                 :            :         }
     591                 :            : 
     592                 :          3 :         return NULL;
     593                 :            : }
     594                 :            : 
     595                 :            : int
     596                 :          3 : main(int argc, char **argv)
     597                 :            : {
     598                 :            :         struct spdk_thread *thread;
     599                 :          3 :         CU_pSuite       suite = NULL;
     600                 :          3 :         pthread_t       spdk_tid;
     601                 :            :         unsigned int    num_failures;
     602                 :            : 
     603                 :          3 :         CU_initialize_registry();
     604                 :            : 
     605                 :          3 :         suite = CU_add_suite("blobfs_sync_ut", NULL, NULL);
     606                 :            : 
     607                 :          3 :         CU_ADD_TEST(suite, cache_read_after_write);
     608                 :          3 :         CU_ADD_TEST(suite, file_length);
     609                 :          3 :         CU_ADD_TEST(suite, append_write_to_extend_blob);
     610                 :          3 :         CU_ADD_TEST(suite, partial_buffer);
     611                 :          3 :         CU_ADD_TEST(suite, cache_write_null_buffer);
     612                 :          3 :         CU_ADD_TEST(suite, fs_create_sync);
     613                 :          3 :         CU_ADD_TEST(suite, fs_rename_sync);
     614                 :          3 :         CU_ADD_TEST(suite, cache_append_no_cache);
     615                 :          3 :         CU_ADD_TEST(suite, fs_delete_file_without_close);
     616                 :            : 
     617                 :          3 :         spdk_thread_lib_init(NULL, 0);
     618                 :            : 
     619                 :          3 :         thread = spdk_thread_create("test_thread", NULL);
     620                 :          3 :         spdk_set_thread(thread);
     621                 :            : 
     622                 :          3 :         g_dispatch_thread = spdk_thread_create("dispatch_thread", NULL);
     623   [ -  +  -  + ]:          3 :         pthread_create(&spdk_tid, NULL, spdk_thread, g_dispatch_thread);
     624                 :            : 
     625                 :          3 :         g_dev_buffer = calloc(1, DEV_BUFFER_SIZE);
     626                 :            : 
     627                 :          3 :         num_failures = spdk_ut_run_tests(argc, argv, NULL);
     628                 :          3 :         CU_cleanup_registry();
     629                 :            : 
     630                 :          3 :         free(g_dev_buffer);
     631                 :            : 
     632                 :          3 :         ut_send_request(terminate_spdk_thread, NULL);
     633                 :          3 :         pthread_join(spdk_tid, NULL);
     634                 :            : 
     635         [ -  + ]:          3 :         while (spdk_thread_poll(g_dispatch_thread, 0, 0) > 0) {}
     636         [ -  + ]:          3 :         while (spdk_thread_poll(thread, 0, 0) > 0) {}
     637                 :            : 
     638                 :          3 :         spdk_set_thread(thread);
     639                 :          3 :         spdk_thread_exit(thread);
     640         [ +  + ]:          6 :         while (!spdk_thread_is_exited(thread)) {
     641                 :          3 :                 spdk_thread_poll(thread, 0, 0);
     642                 :            :         }
     643                 :          3 :         spdk_thread_destroy(thread);
     644                 :            : 
     645                 :          3 :         spdk_set_thread(g_dispatch_thread);
     646                 :          3 :         spdk_thread_exit(g_dispatch_thread);
     647         [ +  + ]:          6 :         while (!spdk_thread_is_exited(g_dispatch_thread)) {
     648                 :          3 :                 spdk_thread_poll(g_dispatch_thread, 0, 0);
     649                 :            :         }
     650                 :          3 :         spdk_thread_destroy(g_dispatch_thread);
     651                 :            : 
     652                 :          3 :         spdk_thread_lib_fini();
     653                 :            : 
     654                 :          3 :         return num_failures;
     655                 :            : }

Generated by: LCOV version 1.14