xref: /spdk/test/unit/lib/blobfs/blobfs_sync_ut/blobfs_sync_ut.c (revision 9cba82b955516bac7faedc1b3b2e158e6827212e)
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 	spdk_set_thread(NULL);
150 	while (spdk_thread_poll(thread, 0) > 0) {}
151 	spdk_set_thread(thread);
152 
153 	SPDK_CU_ASSERT_FATAL(g_fs != NULL);
154 	SPDK_CU_ASSERT_FATAL(g_fs->bdev == dev);
155 	CU_ASSERT(g_fserrno == 0);
156 }
157 
158 static void
159 _fs_unload(void *arg)
160 {
161 	struct spdk_thread *thread;
162 
163 	g_fserrno = -1;
164 	spdk_fs_unload(g_fs, fs_op_complete, NULL);
165 	thread = spdk_get_thread();
166 	spdk_set_thread(NULL);
167 	while (spdk_thread_poll(thread, 0) > 0) {}
168 	spdk_set_thread(thread);
169 	CU_ASSERT(g_fserrno == 0);
170 	g_fs = NULL;
171 }
172 
173 static void
174 cache_write(void)
175 {
176 	uint64_t length;
177 	int rc;
178 	char buf[100];
179 	struct spdk_io_channel *channel;
180 
181 	ut_send_request(_fs_init, NULL);
182 
183 	channel = spdk_fs_alloc_io_channel_sync(g_fs);
184 
185 	rc = spdk_fs_open_file(g_fs, channel, "testfile", SPDK_BLOBFS_OPEN_CREATE, &g_file);
186 	CU_ASSERT(rc == 0);
187 	SPDK_CU_ASSERT_FATAL(g_file != NULL);
188 
189 	length = (4 * 1024 * 1024);
190 	rc = spdk_file_truncate(g_file, channel, length);
191 	CU_ASSERT(rc == 0);
192 
193 	spdk_file_write(g_file, channel, buf, 0, sizeof(buf));
194 
195 	CU_ASSERT(spdk_file_get_length(g_file) == length);
196 
197 	rc = spdk_file_truncate(g_file, channel, sizeof(buf));
198 	CU_ASSERT(rc == 0);
199 
200 	spdk_file_close(g_file, channel);
201 	rc = spdk_fs_delete_file(g_fs, channel, "testfile");
202 	CU_ASSERT(rc == 0);
203 
204 	rc = spdk_fs_delete_file(g_fs, channel, "testfile");
205 	CU_ASSERT(rc == -ENOENT);
206 
207 	spdk_fs_free_io_channel(channel);
208 
209 	ut_send_request(_fs_unload, NULL);
210 }
211 
212 static void
213 cache_write_null_buffer(void)
214 {
215 	uint64_t length;
216 	int rc;
217 	struct spdk_io_channel *channel;
218 	struct spdk_thread *thread;
219 
220 	ut_send_request(_fs_init, NULL);
221 
222 	channel = spdk_fs_alloc_io_channel_sync(g_fs);
223 
224 	rc = spdk_fs_open_file(g_fs, channel, "testfile", SPDK_BLOBFS_OPEN_CREATE, &g_file);
225 	CU_ASSERT(rc == 0);
226 	SPDK_CU_ASSERT_FATAL(g_file != NULL);
227 
228 	length = 0;
229 	rc = spdk_file_truncate(g_file, channel, length);
230 	CU_ASSERT(rc == 0);
231 
232 	rc = spdk_file_write(g_file, channel, NULL, 0, 0);
233 	CU_ASSERT(rc == 0);
234 
235 	spdk_file_close(g_file, channel);
236 	rc = spdk_fs_delete_file(g_fs, channel, "testfile");
237 	CU_ASSERT(rc == 0);
238 
239 	spdk_fs_free_io_channel(channel);
240 
241 	thread = spdk_get_thread();
242 	spdk_set_thread(NULL);
243 	while (spdk_thread_poll(thread, 0) > 0) {}
244 	spdk_set_thread(thread);
245 
246 	ut_send_request(_fs_unload, NULL);
247 }
248 
249 static void
250 fs_create_sync(void)
251 {
252 	int rc;
253 	struct spdk_io_channel *channel;
254 	struct spdk_thread *thread;
255 
256 	ut_send_request(_fs_init, NULL);
257 
258 	channel = spdk_fs_alloc_io_channel_sync(g_fs);
259 	CU_ASSERT(channel != NULL);
260 
261 	rc = spdk_fs_create_file(g_fs, channel, "testfile");
262 	CU_ASSERT(rc == 0);
263 
264 	/* Create should fail, because the file already exists. */
265 	rc = spdk_fs_create_file(g_fs, channel, "testfile");
266 	CU_ASSERT(rc != 0);
267 
268 	rc = spdk_fs_delete_file(g_fs, channel, "testfile");
269 	CU_ASSERT(rc == 0);
270 
271 	spdk_fs_free_io_channel(channel);
272 
273 	thread = spdk_get_thread();
274 	spdk_set_thread(NULL);
275 	while (spdk_thread_poll(thread, 0) > 0) {}
276 	spdk_set_thread(thread);
277 
278 	ut_send_request(_fs_unload, NULL);
279 }
280 
281 static void
282 cache_append_no_cache(void)
283 {
284 	int rc;
285 	char buf[100];
286 	struct spdk_io_channel *channel;
287 	struct spdk_thread *thread;
288 
289 	ut_send_request(_fs_init, NULL);
290 
291 	channel = spdk_fs_alloc_io_channel_sync(g_fs);
292 
293 	rc = spdk_fs_open_file(g_fs, channel, "testfile", SPDK_BLOBFS_OPEN_CREATE, &g_file);
294 	CU_ASSERT(rc == 0);
295 	SPDK_CU_ASSERT_FATAL(g_file != NULL);
296 
297 	spdk_file_write(g_file, channel, buf, 0 * sizeof(buf), sizeof(buf));
298 	CU_ASSERT(spdk_file_get_length(g_file) == 1 * sizeof(buf));
299 	spdk_file_write(g_file, channel, buf, 1 * sizeof(buf), sizeof(buf));
300 	CU_ASSERT(spdk_file_get_length(g_file) == 2 * sizeof(buf));
301 	spdk_file_sync(g_file, channel);
302 	cache_free_buffers(g_file);
303 	spdk_file_write(g_file, channel, buf, 2 * sizeof(buf), sizeof(buf));
304 	CU_ASSERT(spdk_file_get_length(g_file) == 3 * sizeof(buf));
305 	spdk_file_write(g_file, channel, buf, 3 * sizeof(buf), sizeof(buf));
306 	CU_ASSERT(spdk_file_get_length(g_file) == 4 * sizeof(buf));
307 	spdk_file_write(g_file, channel, buf, 4 * sizeof(buf), sizeof(buf));
308 	CU_ASSERT(spdk_file_get_length(g_file) == 5 * sizeof(buf));
309 
310 	spdk_file_close(g_file, channel);
311 	rc = spdk_fs_delete_file(g_fs, channel, "testfile");
312 	CU_ASSERT(rc == 0);
313 
314 	spdk_fs_free_io_channel(channel);
315 
316 	thread = spdk_get_thread();
317 	spdk_set_thread(NULL);
318 	while (spdk_thread_poll(thread, 0) > 0) {}
319 	spdk_set_thread(thread);
320 
321 	ut_send_request(_fs_unload, NULL);
322 }
323 
324 static void
325 fs_delete_file_without_close(void)
326 {
327 	int rc;
328 	struct spdk_io_channel *channel;
329 	struct spdk_file *file;
330 	struct spdk_thread *thread;
331 
332 	ut_send_request(_fs_init, NULL);
333 	channel = spdk_fs_alloc_io_channel_sync(g_fs);
334 	CU_ASSERT(channel != NULL);
335 
336 	rc = spdk_fs_open_file(g_fs, channel, "testfile", SPDK_BLOBFS_OPEN_CREATE, &g_file);
337 	CU_ASSERT(rc == 0);
338 	SPDK_CU_ASSERT_FATAL(g_file != NULL);
339 
340 	rc = spdk_fs_delete_file(g_fs, channel, "testfile");
341 	CU_ASSERT(rc == 0);
342 	CU_ASSERT(g_file->ref_count != 0);
343 	CU_ASSERT(g_file->is_deleted == true);
344 
345 	rc = spdk_fs_open_file(g_fs, channel, "testfile", 0, &file);
346 	CU_ASSERT(rc != 0);
347 
348 	spdk_file_close(g_file, channel);
349 
350 	rc = spdk_fs_open_file(g_fs, channel, "testfile", 0, &file);
351 	CU_ASSERT(rc != 0);
352 
353 	spdk_fs_free_io_channel(channel);
354 
355 	thread = spdk_get_thread();
356 	spdk_set_thread(NULL);
357 	while (spdk_thread_poll(thread, 0) > 0) {}
358 	spdk_set_thread(thread);
359 
360 	ut_send_request(_fs_unload, NULL);
361 
362 }
363 
364 static void
365 terminate_spdk_thread(void *arg)
366 {
367 	spdk_thread_exit(spdk_get_thread());
368 	pthread_exit(NULL);
369 }
370 
371 static void *
372 spdk_thread(void *arg)
373 {
374 	struct spdk_thread *thread;
375 	struct ut_request *req;
376 
377 	thread = spdk_thread_create("thread1");
378 	spdk_set_thread(thread);
379 
380 	while (1) {
381 		pthread_mutex_lock(&g_mutex);
382 		if (g_req != NULL) {
383 			req = g_req;
384 			req->fn(req->arg);
385 			req->done = 1;
386 			if (!req->from_ut) {
387 				free(req);
388 			}
389 			g_req = NULL;
390 		}
391 		pthread_mutex_unlock(&g_mutex);
392 
393 		spdk_set_thread(NULL);
394 		spdk_thread_poll(thread, 0);
395 		spdk_set_thread(thread);
396 	}
397 
398 	return NULL;
399 }
400 
401 int main(int argc, char **argv)
402 {
403 	struct spdk_thread *thread;
404 	CU_pSuite	suite = NULL;
405 	pthread_t	spdk_tid;
406 	unsigned int	num_failures;
407 
408 	if (CU_initialize_registry() != CUE_SUCCESS) {
409 		return CU_get_error();
410 	}
411 
412 	suite = CU_add_suite("blobfs_sync_ut", NULL, NULL);
413 	if (suite == NULL) {
414 		CU_cleanup_registry();
415 		return CU_get_error();
416 	}
417 
418 	if (
419 		CU_add_test(suite, "write", cache_write) == NULL ||
420 		CU_add_test(suite, "write_null_buffer", cache_write_null_buffer) == NULL ||
421 		CU_add_test(suite, "create_sync", fs_create_sync) == NULL ||
422 		CU_add_test(suite, "append_no_cache", cache_append_no_cache) == NULL ||
423 		CU_add_test(suite, "delete_file_without_close", fs_delete_file_without_close) == NULL
424 	) {
425 		CU_cleanup_registry();
426 		return CU_get_error();
427 	}
428 
429 	thread = spdk_thread_create("thread0");
430 	spdk_set_thread(thread);
431 
432 	pthread_create(&spdk_tid, NULL, spdk_thread, NULL);
433 	g_dev_buffer = calloc(1, DEV_BUFFER_SIZE);
434 	CU_basic_set_mode(CU_BRM_VERBOSE);
435 	CU_basic_run_tests();
436 	num_failures = CU_get_number_of_failures();
437 	CU_cleanup_registry();
438 	free(g_dev_buffer);
439 	send_request(terminate_spdk_thread, NULL);
440 	pthread_join(spdk_tid, NULL);
441 	spdk_thread_exit(thread);
442 	return num_failures;
443 }
444