xref: /spdk/lib/blobfs/blobfs.c (revision 7079a18f21032100bcfe216dfbda1d10c4a42357)
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 <stdbool.h>
35 #include <assert.h>
36 #include <errno.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <pthread.h>
40 
41 #include "spdk/blobfs.h"
42 #include "blobfs_internal.h"
43 
44 #include "spdk/queue.h"
45 #include "spdk/io_channel.h"
46 #include "spdk/assert.h"
47 #include "spdk/env.h"
48 #include "spdk_internal/log.h"
49 
50 #define BLOBFS_TRACE(file, str, args...) \
51 	SPDK_TRACELOG(SPDK_TRACE_BLOBFS, "file=%s " str, file->name, ##args)
52 
53 #define BLOBFS_TRACE_RW(file, str, args...) \
54 	SPDK_TRACELOG(SPDK_TRACE_BLOBFS_RW, "file=%s " str, file->name, ##args)
55 
56 #define BLOBFS_CACHE_SIZE (4ULL * 1024 * 1024 * 1024)
57 
58 static uint64_t g_fs_cache_size = BLOBFS_CACHE_SIZE;
59 static struct spdk_mempool *g_cache_pool;
60 static TAILQ_HEAD(, spdk_file) g_caches;
61 static pthread_spinlock_t g_caches_lock;
62 
63 static void
64 __sem_post(void *arg, int bserrno)
65 {
66 	sem_t *sem = arg;
67 
68 	sem_post(sem);
69 }
70 
71 void
72 spdk_cache_buffer_free(struct cache_buffer *cache_buffer)
73 {
74 	spdk_mempool_put(g_cache_pool, cache_buffer->buf);
75 	free(cache_buffer);
76 }
77 
78 #define CACHE_READAHEAD_THRESHOLD	(128 * 1024)
79 
80 struct spdk_file {
81 	struct spdk_filesystem	*fs;
82 	struct spdk_blob	*blob;
83 	char			*name;
84 	uint64_t		length;
85 	bool			open_for_writing;
86 	uint64_t		length_flushed;
87 	uint64_t		append_pos;
88 	uint64_t		seq_byte_count;
89 	uint64_t		next_seq_offset;
90 	uint32_t		priority;
91 	TAILQ_ENTRY(spdk_file)	tailq;
92 	spdk_blob_id		blobid;
93 	uint32_t		ref_count;
94 	pthread_spinlock_t	lock;
95 	struct cache_buffer	*last;
96 	struct cache_tree	*tree;
97 	TAILQ_HEAD(open_requests_head, spdk_fs_request) open_requests;
98 	TAILQ_HEAD(sync_requests_head, spdk_fs_request) sync_requests;
99 	TAILQ_ENTRY(spdk_file)	cache_tailq;
100 };
101 
102 struct spdk_filesystem {
103 	struct spdk_blob_store	*bs;
104 	TAILQ_HEAD(, spdk_file)	files;
105 	struct spdk_bs_opts	bs_opts;
106 	struct spdk_bs_dev	*bdev;
107 	fs_send_request_fn	send_request;
108 	struct spdk_io_channel	*sync_io_channel;
109 	struct spdk_fs_channel	*sync_fs_channel;
110 	struct spdk_io_channel	*md_io_channel;
111 	struct spdk_fs_channel	*md_fs_channel;
112 };
113 
114 struct spdk_fs_cb_args {
115 	union {
116 		spdk_fs_op_with_handle_complete		fs_op_with_handle;
117 		spdk_fs_op_complete			fs_op;
118 		spdk_file_op_with_handle_complete	file_op_with_handle;
119 		spdk_file_op_complete			file_op;
120 		spdk_file_stat_op_complete		stat_op;
121 	} fn;
122 	void *arg;
123 	sem_t *sem;
124 	struct spdk_filesystem *fs;
125 	struct spdk_file *file;
126 	int rc;
127 	bool from_request;
128 	union {
129 		struct {
130 			uint64_t	length;
131 		} truncate;
132 		struct {
133 			struct spdk_io_channel	*channel;
134 			void		*user_buf;
135 			void		*pin_buf;
136 			int		is_read;
137 			off_t		offset;
138 			size_t		length;
139 			uint64_t	start_page;
140 			uint64_t	num_pages;
141 			uint32_t	blocklen;
142 		} rw;
143 		struct {
144 			const char	*old_name;
145 			const char	*new_name;
146 		} rename;
147 		struct {
148 			struct cache_buffer	*cache_buffer;
149 			uint64_t		length;
150 		} flush;
151 		struct {
152 			struct cache_buffer	*cache_buffer;
153 			uint64_t		length;
154 			uint64_t		offset;
155 		} readahead;
156 		struct {
157 			uint64_t			offset;
158 			TAILQ_ENTRY(spdk_fs_request)	tailq;
159 		} sync;
160 		struct {
161 			uint32_t			num_clusters;
162 		} resize;
163 		struct {
164 			const char	*name;
165 			uint32_t	flags;
166 			TAILQ_ENTRY(spdk_fs_request)	tailq;
167 		} open;
168 		struct {
169 			const char	*name;
170 		} create;
171 		struct {
172 			const char	*name;
173 		} delete;
174 		struct {
175 			const char	*name;
176 		} stat;
177 	} op;
178 };
179 
180 static void cache_free_buffers(struct spdk_file *file);
181 
182 static void
183 __initialize_cache(void)
184 {
185 	if (g_cache_pool != NULL) {
186 		return;
187 	}
188 
189 	g_cache_pool = spdk_mempool_create("spdk_fs_cache",
190 					   g_fs_cache_size / CACHE_BUFFER_SIZE,
191 					   CACHE_BUFFER_SIZE, -1, SPDK_ENV_SOCKET_ID_ANY);
192 	TAILQ_INIT(&g_caches);
193 	pthread_spin_init(&g_caches_lock, 0);
194 }
195 
196 static uint64_t
197 __file_get_blob_size(struct spdk_file *file)
198 {
199 	uint64_t cluster_sz;
200 
201 	cluster_sz = file->fs->bs_opts.cluster_sz;
202 	return cluster_sz * spdk_blob_get_num_clusters(file->blob);
203 }
204 
205 struct spdk_fs_request {
206 	struct spdk_fs_cb_args		args;
207 	TAILQ_ENTRY(spdk_fs_request)	link;
208 	struct spdk_fs_channel		*channel;
209 };
210 
211 struct spdk_fs_channel {
212 	struct spdk_fs_request		*req_mem;
213 	TAILQ_HEAD(, spdk_fs_request)	reqs;
214 	sem_t				sem;
215 	struct spdk_filesystem		*fs;
216 	struct spdk_io_channel		*bs_channel;
217 	fs_send_request_fn		send_request;
218 };
219 
220 static struct spdk_fs_request *
221 alloc_fs_request(struct spdk_fs_channel *channel)
222 {
223 	struct spdk_fs_request *req;
224 
225 	req = TAILQ_FIRST(&channel->reqs);
226 	if (!req) {
227 		return NULL;
228 	}
229 	TAILQ_REMOVE(&channel->reqs, req, link);
230 	memset(req, 0, sizeof(*req));
231 	req->channel = channel;
232 	req->args.from_request = true;
233 
234 	return req;
235 }
236 
237 static void
238 free_fs_request(struct spdk_fs_request *req)
239 {
240 	TAILQ_INSERT_HEAD(&req->channel->reqs, req, link);
241 }
242 
243 static int
244 _spdk_fs_channel_create(void *io_device, uint32_t priority, void *ctx_buf, void *unique_ctx)
245 {
246 	struct spdk_filesystem		*fs = io_device;
247 	struct spdk_fs_channel		*channel = ctx_buf;
248 	uint32_t			max_ops = *(uint32_t *)unique_ctx;
249 	uint32_t			i;
250 
251 	channel->req_mem = calloc(max_ops, sizeof(struct spdk_fs_request));
252 	if (!channel->req_mem) {
253 		free(channel);
254 		return -1;
255 	}
256 
257 	TAILQ_INIT(&channel->reqs);
258 	sem_init(&channel->sem, 0, 0);
259 
260 	for (i = 0; i < max_ops; i++) {
261 		TAILQ_INSERT_TAIL(&channel->reqs, &channel->req_mem[i], link);
262 	}
263 
264 	channel->fs = fs;
265 
266 	return 0;
267 }
268 
269 static void
270 _spdk_fs_channel_destroy(void *io_device, void *ctx_buf)
271 {
272 	struct spdk_fs_channel *channel = ctx_buf;
273 
274 	free(channel->req_mem);
275 	if (channel->bs_channel != NULL) {
276 		spdk_bs_free_io_channel(channel->bs_channel);
277 	}
278 }
279 
280 static void
281 __send_request_direct(fs_request_fn fn, void *arg)
282 {
283 	fn(arg);
284 }
285 
286 static void
287 common_fs_bs_init(struct spdk_filesystem *fs, struct spdk_blob_store *bs)
288 {
289 	fs->bs = bs;
290 	fs->bs_opts.cluster_sz = spdk_bs_get_cluster_size(bs);
291 	fs->md_fs_channel->bs_channel = spdk_bs_alloc_io_channel(fs->bs, SPDK_IO_PRIORITY_DEFAULT, 512);
292 	fs->md_fs_channel->send_request = __send_request_direct;
293 	fs->sync_fs_channel->bs_channel = spdk_bs_alloc_io_channel(fs->bs, SPDK_IO_PRIORITY_DEFAULT, 512);
294 	fs->sync_fs_channel->send_request = __send_request_direct;
295 }
296 
297 static void
298 init_cb(void *ctx, struct spdk_blob_store *bs, int bserrno)
299 {
300 	struct spdk_fs_request *req = ctx;
301 	struct spdk_fs_cb_args *args = &req->args;
302 	struct spdk_filesystem *fs = args->fs;
303 
304 	if (bserrno == 0) {
305 		common_fs_bs_init(fs, bs);
306 	} else {
307 		free(fs);
308 		fs = NULL;
309 	}
310 
311 	args->fn.fs_op_with_handle(args->arg, fs, bserrno);
312 	free_fs_request(req);
313 }
314 
315 static struct spdk_filesystem *
316 fs_alloc(struct spdk_bs_dev *dev, fs_send_request_fn send_request_fn)
317 {
318 	struct spdk_filesystem *fs;
319 	uint32_t max_ops = 512;
320 
321 	fs = calloc(1, sizeof(*fs));
322 	if (fs == NULL) {
323 		return NULL;
324 	}
325 
326 	fs->bdev = dev;
327 	fs->send_request = send_request_fn;
328 	TAILQ_INIT(&fs->files);
329 	spdk_io_device_register(fs, _spdk_fs_channel_create, _spdk_fs_channel_destroy,
330 				sizeof(struct spdk_fs_channel));
331 
332 	fs->md_io_channel = spdk_get_io_channel(fs, SPDK_IO_PRIORITY_DEFAULT, true, (void *)&max_ops);
333 	fs->md_fs_channel = spdk_io_channel_get_ctx(fs->md_io_channel);
334 
335 	fs->sync_io_channel = spdk_get_io_channel(fs, SPDK_IO_PRIORITY_DEFAULT, true, (void *)&max_ops);
336 	fs->sync_fs_channel = spdk_io_channel_get_ctx(fs->sync_io_channel);
337 
338 	__initialize_cache();
339 
340 	return fs;
341 }
342 
343 void
344 spdk_fs_init(struct spdk_bs_dev *dev, fs_send_request_fn send_request_fn,
345 	     spdk_fs_op_with_handle_complete cb_fn, void *cb_arg)
346 {
347 	struct spdk_filesystem *fs;
348 	struct spdk_fs_request *req;
349 	struct spdk_fs_cb_args *args;
350 
351 	fs = fs_alloc(dev, send_request_fn);
352 	if (fs == NULL) {
353 		cb_fn(cb_arg, NULL, -ENOMEM);
354 		return;
355 	}
356 
357 	req = alloc_fs_request(fs->md_fs_channel);
358 	if (req == NULL) {
359 		cb_fn(cb_arg, NULL, -ENOMEM);
360 		return;
361 	}
362 
363 	args = &req->args;
364 	args->fn.fs_op_with_handle = cb_fn;
365 	args->arg = cb_arg;
366 	args->fs = fs;
367 
368 	spdk_bs_init(dev, NULL, init_cb, req);
369 }
370 
371 static struct spdk_file *
372 file_alloc(struct spdk_filesystem *fs)
373 {
374 	struct spdk_file *file;
375 
376 	file = calloc(1, sizeof(*file));
377 	if (file == NULL) {
378 		return NULL;
379 	}
380 
381 	file->fs = fs;
382 	TAILQ_INIT(&file->open_requests);
383 	TAILQ_INIT(&file->sync_requests);
384 	pthread_spin_init(&file->lock, 0);
385 	file->tree = calloc(1, sizeof(*file->tree));
386 	TAILQ_INSERT_TAIL(&fs->files, file, tailq);
387 	file->priority = SPDK_FILE_PRIORITY_LOW;
388 	return file;
389 }
390 
391 static void
392 iter_cb(void *ctx, struct spdk_blob *blob, int rc)
393 {
394 	struct spdk_fs_request *req = ctx;
395 	struct spdk_fs_cb_args *args = &req->args;
396 	struct spdk_filesystem *fs = args->fs;
397 	struct spdk_file *f;
398 	uint64_t *length;
399 	const char *name;
400 	size_t value_len;
401 
402 	if (rc == -ENOENT) {
403 		/* Finished iterating */
404 		args->fn.fs_op_with_handle(args->arg, fs, 0);
405 		free_fs_request(req);
406 		return;
407 	} else if (rc < 0) {
408 		args->fn.fs_op_with_handle(args->arg, fs, rc);
409 		free_fs_request(req);
410 		return;
411 	}
412 
413 	rc = spdk_bs_md_get_xattr_value(blob, "name", (const void **)&name, &value_len);
414 	if (rc < 0) {
415 		args->fn.fs_op_with_handle(args->arg, fs, rc);
416 		free_fs_request(req);
417 		return;
418 	}
419 
420 	rc = spdk_bs_md_get_xattr_value(blob, "length", (const void **)&length, &value_len);
421 	if (rc < 0) {
422 		args->fn.fs_op_with_handle(args->arg, fs, rc);
423 		free_fs_request(req);
424 		return;
425 	}
426 	assert(value_len == 8);
427 
428 	f = file_alloc(fs);
429 	if (f == NULL) {
430 		args->fn.fs_op_with_handle(args->arg, fs, -ENOMEM);
431 		free_fs_request(req);
432 		return;
433 	}
434 
435 	f->name = strdup(name);
436 	f->blobid = spdk_blob_get_id(blob);
437 	f->length = *length;
438 	f->length_flushed = *length;
439 	f->append_pos = *length;
440 	SPDK_TRACELOG(SPDK_TRACE_BLOBFS, "added file %s length=%ju\n", f->name, f->length);
441 
442 	spdk_bs_md_iter_next(fs->bs, &blob, iter_cb, req);
443 }
444 
445 static void
446 load_cb(void *ctx, struct spdk_blob_store *bs, int bserrno)
447 {
448 	struct spdk_fs_request *req = ctx;
449 	struct spdk_fs_cb_args *args = &req->args;
450 	struct spdk_filesystem *fs = args->fs;
451 
452 	if (bserrno != 0) {
453 		args->fn.fs_op_with_handle(args->arg, NULL, bserrno);
454 		free_fs_request(req);
455 		free(fs);
456 		return;
457 	}
458 
459 	common_fs_bs_init(fs, bs);
460 	spdk_bs_md_iter_first(fs->bs, iter_cb, req);
461 }
462 
463 void
464 spdk_fs_load(struct spdk_bs_dev *dev, fs_send_request_fn send_request_fn,
465 	     spdk_fs_op_with_handle_complete cb_fn, void *cb_arg)
466 {
467 	struct spdk_filesystem *fs;
468 	struct spdk_fs_cb_args *args;
469 	struct spdk_fs_request *req;
470 
471 	fs = fs_alloc(dev, send_request_fn);
472 	if (fs == NULL) {
473 		cb_fn(cb_arg, NULL, -ENOMEM);
474 		return;
475 	}
476 
477 	req = alloc_fs_request(fs->md_fs_channel);
478 	if (req == NULL) {
479 		cb_fn(cb_arg, NULL, -ENOMEM);
480 		return;
481 	}
482 
483 	args = &req->args;
484 	args->fn.fs_op_with_handle = cb_fn;
485 	args->arg = cb_arg;
486 	args->fs = fs;
487 
488 	spdk_bs_load(dev, load_cb, req);
489 }
490 
491 static void
492 unload_cb(void *ctx, int bserrno)
493 {
494 	struct spdk_fs_request *req = ctx;
495 	struct spdk_fs_cb_args *args = &req->args;
496 	struct spdk_filesystem *fs = args->fs;
497 
498 	args->fn.fs_op(args->arg, bserrno);
499 	free(req);
500 	spdk_io_device_unregister(fs);
501 	free(fs);
502 }
503 
504 void
505 spdk_fs_unload(struct spdk_filesystem *fs, spdk_fs_op_complete cb_fn, void *cb_arg)
506 {
507 	struct spdk_fs_request *req;
508 	struct spdk_fs_cb_args *args;
509 
510 	/*
511 	 * We must free the md_channel before unloading the blobstore, so just
512 	 *  allocate this request from the general heap.
513 	 */
514 	req = calloc(1, sizeof(*req));
515 	if (req == NULL) {
516 		cb_fn(cb_arg, -ENOMEM);
517 		return;
518 	}
519 
520 	args = &req->args;
521 	args->fn.fs_op = cb_fn;
522 	args->arg = cb_arg;
523 	args->fs = fs;
524 
525 	spdk_fs_free_io_channel(fs->md_io_channel);
526 	spdk_fs_free_io_channel(fs->sync_io_channel);
527 	spdk_bs_unload(fs->bs, unload_cb, req);
528 }
529 
530 static struct spdk_file *
531 fs_find_file(struct spdk_filesystem *fs, const char *name)
532 {
533 	struct spdk_file *file;
534 
535 	TAILQ_FOREACH(file, &fs->files, tailq) {
536 		if (!strncmp(name, file->name, SPDK_FILE_NAME_MAX)) {
537 			return file;
538 		}
539 	}
540 
541 	return NULL;
542 }
543 
544 void
545 spdk_fs_file_stat_async(struct spdk_filesystem *fs, const char *name,
546 			spdk_file_stat_op_complete cb_fn, void *cb_arg)
547 {
548 	struct spdk_file_stat stat;
549 	struct spdk_file *f = NULL;
550 
551 	if (strnlen(name, SPDK_FILE_NAME_MAX + 1) == SPDK_FILE_NAME_MAX + 1) {
552 		cb_fn(cb_arg, NULL, -ENAMETOOLONG);
553 		return;
554 	}
555 
556 	f = fs_find_file(fs, name);
557 	if (f != NULL) {
558 		stat.blobid = f->blobid;
559 		stat.size = f->length;
560 		cb_fn(cb_arg, &stat, 0);
561 		return;
562 	}
563 
564 	cb_fn(cb_arg, NULL, -ENOENT);
565 }
566 
567 static void
568 __copy_stat(void *arg, struct spdk_file_stat *stat, int fserrno)
569 {
570 	struct spdk_fs_request *req = arg;
571 	struct spdk_fs_cb_args *args = &req->args;
572 
573 	args->rc = fserrno;
574 	if (fserrno == 0) {
575 		memcpy(args->arg, stat, sizeof(*stat));
576 	}
577 	sem_post(args->sem);
578 }
579 
580 static void
581 __file_stat(void *arg)
582 {
583 	struct spdk_fs_request *req = arg;
584 	struct spdk_fs_cb_args *args = &req->args;
585 
586 	spdk_fs_file_stat_async(args->fs, args->op.stat.name,
587 				args->fn.stat_op, req);
588 }
589 
590 int
591 spdk_fs_file_stat(struct spdk_filesystem *fs, struct spdk_io_channel *_channel,
592 		  const char *name, struct spdk_file_stat *stat)
593 {
594 	struct spdk_fs_channel *channel = spdk_io_channel_get_ctx(_channel);
595 	struct spdk_fs_request *req;
596 	int rc;
597 
598 	req = alloc_fs_request(channel);
599 	assert(req != NULL);
600 
601 	req->args.fs = fs;
602 	req->args.op.stat.name = name;
603 	req->args.fn.stat_op = __copy_stat;
604 	req->args.arg = stat;
605 	req->args.sem = &channel->sem;
606 	channel->send_request(__file_stat, req);
607 	sem_wait(&channel->sem);
608 
609 	rc = req->args.rc;
610 	free_fs_request(req);
611 
612 	return rc;
613 }
614 
615 static void
616 fs_create_blob_close_cb(void *ctx, int bserrno)
617 {
618 	struct spdk_fs_request *req = ctx;
619 	struct spdk_fs_cb_args *args = &req->args;
620 
621 	args->fn.file_op(args->arg, bserrno);
622 	free_fs_request(req);
623 }
624 
625 static void
626 fs_create_blob_open_cb(void *ctx, struct spdk_blob *blob, int bserrno)
627 {
628 	struct spdk_fs_request *req = ctx;
629 	struct spdk_fs_cb_args *args = &req->args;
630 	struct spdk_file *f = args->file;
631 	uint64_t length = 0;
632 
633 	f->blob = blob;
634 	spdk_bs_md_resize_blob(blob, 1);
635 	spdk_blob_md_set_xattr(blob, "name", f->name, strlen(f->name) + 1);
636 	spdk_blob_md_set_xattr(blob, "length", &length, sizeof(length));
637 
638 	spdk_bs_md_close_blob(&f->blob, fs_create_blob_close_cb, args);
639 }
640 
641 static void
642 fs_create_blob_create_cb(void *ctx, spdk_blob_id blobid, int bserrno)
643 {
644 	struct spdk_fs_request *req = ctx;
645 	struct spdk_fs_cb_args *args = &req->args;
646 	struct spdk_file *f = args->file;
647 
648 	f->blobid = blobid;
649 	spdk_bs_md_open_blob(f->fs->bs, blobid, fs_create_blob_open_cb, req);
650 }
651 
652 void
653 spdk_fs_create_file_async(struct spdk_filesystem *fs, const char *name,
654 			  spdk_file_op_complete cb_fn, void *cb_arg)
655 {
656 	struct spdk_file *file;
657 	struct spdk_fs_request *req;
658 	struct spdk_fs_cb_args *args;
659 
660 	if (strnlen(name, SPDK_FILE_NAME_MAX + 1) == SPDK_FILE_NAME_MAX + 1) {
661 		cb_fn(cb_arg, -ENAMETOOLONG);
662 		return;
663 	}
664 
665 	file = fs_find_file(fs, name);
666 	if (file != NULL) {
667 		cb_fn(cb_arg, -EEXIST);
668 		return;
669 	}
670 
671 	file = file_alloc(fs);
672 	if (file == NULL) {
673 		cb_fn(cb_arg, -ENOMEM);
674 		return;
675 	}
676 
677 	req = alloc_fs_request(fs->md_fs_channel);
678 	if (req == NULL) {
679 		cb_fn(cb_arg, -ENOMEM);
680 		return;
681 	}
682 
683 	args = &req->args;
684 	args->file = file;
685 	args->fn.file_op = cb_fn;
686 	args->arg = cb_arg;
687 
688 	file->name = strdup(name);
689 	spdk_bs_md_create_blob(fs->bs, fs_create_blob_create_cb, args);
690 }
691 
692 static void
693 __fs_create_file_done(void *arg, int fserrno)
694 {
695 	struct spdk_fs_request *req = arg;
696 	struct spdk_fs_cb_args *args = &req->args;
697 
698 	args->rc = fserrno;
699 	sem_post(args->sem);
700 	SPDK_TRACELOG(SPDK_TRACE_BLOBFS, "file=%s\n", args->op.create.name);
701 }
702 
703 static void
704 __fs_create_file(void *arg)
705 {
706 	struct spdk_fs_request *req = arg;
707 	struct spdk_fs_cb_args *args = &req->args;
708 
709 	SPDK_TRACELOG(SPDK_TRACE_BLOBFS, "file=%s\n", args->op.create.name);
710 	spdk_fs_create_file_async(args->fs, args->op.create.name, __fs_create_file_done, req);
711 }
712 
713 int
714 spdk_fs_create_file(struct spdk_filesystem *fs, struct spdk_io_channel *_channel, const char *name)
715 {
716 	struct spdk_fs_channel *channel = spdk_io_channel_get_ctx(_channel);
717 	struct spdk_fs_request *req;
718 	struct spdk_fs_cb_args *args;
719 	int rc;
720 
721 	SPDK_TRACELOG(SPDK_TRACE_BLOBFS, "file=%s\n", name);
722 
723 	req = alloc_fs_request(channel);
724 	assert(req != NULL);
725 
726 	args = &req->args;
727 	args->fs = fs;
728 	args->op.create.name = name;
729 	args->sem = &channel->sem;
730 	fs->send_request(__fs_create_file, req);
731 	sem_wait(&channel->sem);
732 	rc = args->rc;
733 	free_fs_request(req);
734 
735 	return rc;
736 }
737 
738 static void
739 fs_open_blob_done(void *ctx, struct spdk_blob *blob, int bserrno)
740 {
741 	struct spdk_fs_request *req = ctx;
742 	struct spdk_fs_cb_args *args = &req->args;
743 	struct spdk_file *f = args->file;
744 
745 	f->blob = blob;
746 	while (!TAILQ_EMPTY(&f->open_requests)) {
747 		req = TAILQ_FIRST(&f->open_requests);
748 		args = &req->args;
749 		TAILQ_REMOVE(&f->open_requests, req, args.op.open.tailq);
750 		args->fn.file_op_with_handle(args->arg, f, bserrno);
751 		free_fs_request(req);
752 	}
753 }
754 
755 static void
756 fs_open_blob_create_cb(void *ctx, int bserrno)
757 {
758 	struct spdk_fs_request *req = ctx;
759 	struct spdk_fs_cb_args *args = &req->args;
760 	struct spdk_file *file = args->file;
761 	struct spdk_filesystem *fs = args->fs;
762 
763 	if (file == NULL) {
764 		/*
765 		 * This is from an open with CREATE flag - the file
766 		 *  is now created so look it up in the file list for this
767 		 *  filesystem.
768 		 */
769 		file = fs_find_file(fs, args->op.open.name);
770 		assert(file != NULL);
771 		args->file = file;
772 	}
773 
774 	file->ref_count++;
775 	TAILQ_INSERT_TAIL(&file->open_requests, req, args.op.open.tailq);
776 	if (file->ref_count == 1) {
777 		assert(file->blob == NULL);
778 		spdk_bs_md_open_blob(fs->bs, file->blobid, fs_open_blob_done, req);
779 	} else if (file->blob != NULL) {
780 		fs_open_blob_done(req, file->blob, 0);
781 	} else {
782 		/*
783 		 * The blob open for this file is in progress due to a previous
784 		 *  open request.  When that open completes, it will invoke the
785 		 *  open callback for this request.
786 		 */
787 	}
788 }
789 
790 void
791 spdk_fs_open_file_async(struct spdk_filesystem *fs, const char *name, uint32_t flags,
792 			spdk_file_op_with_handle_complete cb_fn, void *cb_arg)
793 {
794 	struct spdk_file *f = NULL;
795 	struct spdk_fs_request *req;
796 	struct spdk_fs_cb_args *args;
797 
798 	if (strnlen(name, SPDK_FILE_NAME_MAX + 1) == SPDK_FILE_NAME_MAX + 1) {
799 		cb_fn(cb_arg, NULL, -ENAMETOOLONG);
800 		return;
801 	}
802 
803 	f = fs_find_file(fs, name);
804 	if (f == NULL && !(flags & SPDK_BLOBFS_OPEN_CREATE)) {
805 		cb_fn(cb_arg, NULL, -ENOENT);
806 		return;
807 	}
808 
809 	req = alloc_fs_request(fs->md_fs_channel);
810 	if (req == NULL) {
811 		cb_fn(cb_arg, NULL, -ENOMEM);
812 		return;
813 	}
814 
815 	args = &req->args;
816 	args->fn.file_op_with_handle = cb_fn;
817 	args->arg = cb_arg;
818 	args->file = f;
819 	args->fs = fs;
820 	args->op.open.name = name;
821 
822 	if (f == NULL) {
823 		spdk_fs_create_file_async(fs, name, fs_open_blob_create_cb, req);
824 	} else {
825 		fs_open_blob_create_cb(req, 0);
826 	}
827 }
828 
829 static void
830 __fs_open_file_done(void *arg, struct spdk_file *file, int bserrno)
831 {
832 	struct spdk_fs_request *req = arg;
833 	struct spdk_fs_cb_args *args = &req->args;
834 
835 	args->file = file;
836 	args->rc = bserrno;
837 	sem_post(args->sem);
838 	SPDK_TRACELOG(SPDK_TRACE_BLOBFS, "file=%s\n", args->op.open.name);
839 }
840 
841 static void
842 __fs_open_file(void *arg)
843 {
844 	struct spdk_fs_request *req = arg;
845 	struct spdk_fs_cb_args *args = &req->args;
846 
847 	SPDK_TRACELOG(SPDK_TRACE_BLOBFS, "file=%s\n", args->op.open.name);
848 	spdk_fs_open_file_async(args->fs, args->op.open.name, args->op.open.flags,
849 				__fs_open_file_done, req);
850 }
851 
852 int
853 spdk_fs_open_file(struct spdk_filesystem *fs, struct spdk_io_channel *_channel,
854 		  const char *name, uint32_t flags, struct spdk_file **file)
855 {
856 	struct spdk_fs_channel *channel = spdk_io_channel_get_ctx(_channel);
857 	struct spdk_fs_request *req;
858 	struct spdk_fs_cb_args *args;
859 	int rc;
860 
861 	SPDK_TRACELOG(SPDK_TRACE_BLOBFS, "file=%s\n", name);
862 
863 	req = alloc_fs_request(channel);
864 	assert(req != NULL);
865 
866 	args = &req->args;
867 	args->fs = fs;
868 	args->op.open.name = name;
869 	args->op.open.flags = flags;
870 	args->sem = &channel->sem;
871 	fs->send_request(__fs_open_file, req);
872 	sem_wait(&channel->sem);
873 	rc = args->rc;
874 	if (rc == 0) {
875 		*file = args->file;
876 	} else {
877 		*file = NULL;
878 	}
879 	free_fs_request(req);
880 
881 	return rc;
882 }
883 
884 static void
885 fs_rename_blob_close_cb(void *ctx, int bserrno)
886 {
887 	struct spdk_fs_request *req = ctx;
888 	struct spdk_fs_cb_args *args = &req->args;
889 
890 	args->fn.fs_op(args->arg, bserrno);
891 	free_fs_request(req);
892 }
893 
894 static void
895 fs_rename_blob_open_cb(void *ctx, struct spdk_blob *blob, int bserrno)
896 {
897 	struct spdk_fs_request *req = ctx;
898 	struct spdk_fs_cb_args *args = &req->args;
899 	struct spdk_file *f = args->file;
900 	const char *new_name = args->op.rename.new_name;
901 
902 	f->blob = blob;
903 	spdk_blob_md_set_xattr(blob, "name", new_name, strlen(new_name) + 1);
904 	spdk_bs_md_close_blob(&f->blob, fs_rename_blob_close_cb, req);
905 }
906 
907 static void
908 __spdk_fs_md_rename_file(struct spdk_fs_request *req)
909 {
910 	struct spdk_fs_cb_args *args = &req->args;
911 	struct spdk_file *f;
912 
913 	f = fs_find_file(args->fs, args->op.rename.old_name);
914 	if (f == NULL) {
915 		args->fn.fs_op(args->arg, -ENOENT);
916 		free_fs_request(req);
917 		return;
918 	}
919 
920 	free(f->name);
921 	f->name = strdup(args->op.rename.new_name);
922 	args->file = f;
923 	spdk_bs_md_open_blob(args->fs->bs, f->blobid, fs_rename_blob_open_cb, req);
924 }
925 
926 static void
927 fs_rename_delete_done(void *arg, int fserrno)
928 {
929 	__spdk_fs_md_rename_file(arg);
930 }
931 
932 void
933 spdk_fs_rename_file_async(struct spdk_filesystem *fs,
934 			  const char *old_name, const char *new_name,
935 			  spdk_file_op_complete cb_fn, void *cb_arg)
936 {
937 	struct spdk_file *f;
938 	struct spdk_fs_request *req;
939 	struct spdk_fs_cb_args *args;
940 
941 	SPDK_TRACELOG(SPDK_TRACE_BLOBFS, "old=%s new=%s\n", old_name, new_name);
942 	if (strnlen(new_name, SPDK_FILE_NAME_MAX + 1) == SPDK_FILE_NAME_MAX + 1) {
943 		cb_fn(cb_arg, -ENAMETOOLONG);
944 		return;
945 	}
946 
947 	req = alloc_fs_request(fs->md_fs_channel);
948 	if (req == NULL) {
949 		cb_fn(cb_arg, -ENOMEM);
950 		return;
951 	}
952 
953 	args = &req->args;
954 	args->fn.fs_op = cb_fn;
955 	args->fs = fs;
956 	args->arg = cb_arg;
957 	args->op.rename.old_name = old_name;
958 	args->op.rename.new_name = new_name;
959 
960 	f = fs_find_file(fs, new_name);
961 	if (f == NULL) {
962 		__spdk_fs_md_rename_file(req);
963 		return;
964 	}
965 
966 	/*
967 	 * The rename overwrites an existing file.  So delete the existing file, then
968 	 *  do the actual rename.
969 	 */
970 	spdk_fs_delete_file_async(fs, new_name, fs_rename_delete_done, req);
971 }
972 
973 static void
974 __fs_rename_file_done(void *arg, int fserrno)
975 {
976 	struct spdk_fs_request *req = arg;
977 	struct spdk_fs_cb_args *args = &req->args;
978 
979 	args->rc = fserrno;
980 	sem_post(args->sem);
981 }
982 
983 static void
984 __fs_rename_file(void *arg)
985 {
986 	struct spdk_fs_request *req = arg;
987 	struct spdk_fs_cb_args *args = &req->args;
988 
989 	spdk_fs_rename_file_async(args->fs, args->op.rename.old_name, args->op.rename.new_name,
990 				  __fs_rename_file_done, req);
991 }
992 
993 int
994 spdk_fs_rename_file(struct spdk_filesystem *fs, struct spdk_io_channel *_channel,
995 		    const char *old_name, const char *new_name)
996 {
997 	struct spdk_fs_channel *channel = spdk_io_channel_get_ctx(_channel);
998 	struct spdk_fs_request *req;
999 	struct spdk_fs_cb_args *args;
1000 	int rc;
1001 
1002 	req = alloc_fs_request(channel);
1003 	assert(req != NULL);
1004 
1005 	args = &req->args;
1006 
1007 	args->fs = fs;
1008 	args->op.rename.old_name = old_name;
1009 	args->op.rename.new_name = new_name;
1010 	args->sem = &channel->sem;
1011 	fs->send_request(__fs_rename_file, req);
1012 	sem_wait(&channel->sem);
1013 	rc = args->rc;
1014 	free_fs_request(req);
1015 	return rc;
1016 }
1017 
1018 static void
1019 blob_delete_cb(void *ctx, int bserrno)
1020 {
1021 	struct spdk_fs_request *req = ctx;
1022 	struct spdk_fs_cb_args *args = &req->args;
1023 
1024 	args->fn.file_op(args->arg, bserrno);
1025 	free_fs_request(req);
1026 }
1027 
1028 void
1029 spdk_fs_delete_file_async(struct spdk_filesystem *fs, const char *name,
1030 			  spdk_file_op_complete cb_fn, void *cb_arg)
1031 {
1032 	struct spdk_file *f;
1033 	spdk_blob_id blobid;
1034 	struct spdk_fs_request *req;
1035 	struct spdk_fs_cb_args *args;
1036 
1037 	SPDK_TRACELOG(SPDK_TRACE_BLOBFS, "file=%s\n", name);
1038 
1039 	if (strnlen(name, SPDK_FILE_NAME_MAX + 1) == SPDK_FILE_NAME_MAX + 1) {
1040 		cb_fn(cb_arg, -ENAMETOOLONG);
1041 		return;
1042 	}
1043 
1044 	f = fs_find_file(fs, name);
1045 	if (f == NULL) {
1046 		cb_fn(cb_arg, -ENOENT);
1047 		return;
1048 	}
1049 
1050 	if (f->ref_count > 0) {
1051 		/* For now, do not allow deleting files with open references. */
1052 		cb_fn(cb_arg, -EBUSY);
1053 		return;
1054 	}
1055 
1056 	req = alloc_fs_request(fs->md_fs_channel);
1057 	if (req == NULL) {
1058 		cb_fn(cb_arg, -ENOMEM);
1059 		return;
1060 	}
1061 
1062 	TAILQ_REMOVE(&fs->files, f, tailq);
1063 
1064 	cache_free_buffers(f);
1065 
1066 	blobid = f->blobid;
1067 
1068 	free(f->name);
1069 	free(f->tree);
1070 	free(f);
1071 
1072 	args = &req->args;
1073 	args->fn.file_op = cb_fn;
1074 	args->arg = cb_arg;
1075 	spdk_bs_md_delete_blob(fs->bs, blobid, blob_delete_cb, req);
1076 }
1077 
1078 static void
1079 __fs_delete_file_done(void *arg, int fserrno)
1080 {
1081 	struct spdk_fs_request *req = arg;
1082 	struct spdk_fs_cb_args *args = &req->args;
1083 
1084 	args->rc = fserrno;
1085 	sem_post(args->sem);
1086 }
1087 
1088 static void
1089 __fs_delete_file(void *arg)
1090 {
1091 	struct spdk_fs_request *req = arg;
1092 	struct spdk_fs_cb_args *args = &req->args;
1093 
1094 	spdk_fs_delete_file_async(args->fs, args->op.delete.name, __fs_delete_file_done, req);
1095 }
1096 
1097 int
1098 spdk_fs_delete_file(struct spdk_filesystem *fs, struct spdk_io_channel *_channel,
1099 		    const char *name)
1100 {
1101 	struct spdk_fs_channel *channel = spdk_io_channel_get_ctx(_channel);
1102 	struct spdk_fs_request *req;
1103 	struct spdk_fs_cb_args *args;
1104 	int rc;
1105 
1106 	req = alloc_fs_request(channel);
1107 	assert(req != NULL);
1108 
1109 	args = &req->args;
1110 	args->fs = fs;
1111 	args->op.delete.name = name;
1112 	args->sem = &channel->sem;
1113 	fs->send_request(__fs_delete_file, req);
1114 	sem_wait(&channel->sem);
1115 	rc = args->rc;
1116 	free_fs_request(req);
1117 
1118 	return rc;
1119 }
1120 
1121 spdk_fs_iter
1122 spdk_fs_iter_first(struct spdk_filesystem *fs)
1123 {
1124 	struct spdk_file *f;
1125 
1126 	f = TAILQ_FIRST(&fs->files);
1127 	return f;
1128 }
1129 
1130 spdk_fs_iter
1131 spdk_fs_iter_next(spdk_fs_iter iter)
1132 {
1133 	struct spdk_file *f = iter;
1134 
1135 	if (f == NULL) {
1136 		return NULL;
1137 	}
1138 
1139 	f = TAILQ_NEXT(f, tailq);
1140 	return f;
1141 }
1142 
1143 const char *
1144 spdk_file_get_name(struct spdk_file *file)
1145 {
1146 	return file->name;
1147 }
1148 
1149 uint64_t
1150 spdk_file_get_length(struct spdk_file *file)
1151 {
1152 	assert(file != NULL);
1153 	SPDK_TRACELOG(SPDK_TRACE_BLOBFS, "file=%s length=0x%jx\n", file->name, file->length);
1154 	return file->length;
1155 }
1156 
1157 static void
1158 fs_truncate_complete_cb(void *ctx, int bserrno)
1159 {
1160 	struct spdk_fs_request *req = ctx;
1161 	struct spdk_fs_cb_args *args = &req->args;
1162 
1163 	args->fn.file_op(args->arg, bserrno);
1164 	free_fs_request(req);
1165 }
1166 
1167 static uint64_t
1168 __bytes_to_clusters(uint64_t length, uint64_t cluster_sz)
1169 {
1170 	return (length + cluster_sz - 1) / cluster_sz;
1171 }
1172 
1173 void
1174 spdk_file_truncate_async(struct spdk_file *file, uint64_t length,
1175 			 spdk_file_op_complete cb_fn, void *cb_arg)
1176 {
1177 	struct spdk_filesystem *fs;
1178 	size_t num_clusters;
1179 	struct spdk_fs_request *req;
1180 	struct spdk_fs_cb_args *args;
1181 
1182 	SPDK_TRACELOG(SPDK_TRACE_BLOBFS, "file=%s old=0x%jx new=0x%jx\n", file->name, file->length, length);
1183 	if (length == file->length) {
1184 		cb_fn(cb_arg, 0);
1185 		return;
1186 	}
1187 
1188 	req = alloc_fs_request(file->fs->md_fs_channel);
1189 	if (req == NULL) {
1190 		cb_fn(cb_arg, -ENOMEM);
1191 		return;
1192 	}
1193 
1194 	args = &req->args;
1195 	args->fn.file_op = cb_fn;
1196 	args->arg = cb_arg;
1197 	args->file = file;
1198 	fs = file->fs;
1199 
1200 	num_clusters = __bytes_to_clusters(length, fs->bs_opts.cluster_sz);
1201 
1202 	spdk_bs_md_resize_blob(file->blob, num_clusters);
1203 	spdk_blob_md_set_xattr(file->blob, "length", &length, sizeof(length));
1204 
1205 	file->length = length;
1206 	if (file->append_pos > file->length) {
1207 		file->append_pos = file->length;
1208 	}
1209 
1210 	spdk_bs_md_sync_blob(file->blob, fs_truncate_complete_cb, args);
1211 }
1212 
1213 static void
1214 __truncate(void *arg)
1215 {
1216 	struct spdk_fs_request *req = arg;
1217 	struct spdk_fs_cb_args *args = &req->args;
1218 
1219 	spdk_file_truncate_async(args->file, args->op.truncate.length,
1220 				 args->fn.file_op, args->arg);
1221 }
1222 
1223 void
1224 spdk_file_truncate(struct spdk_file *file, struct spdk_io_channel *_channel,
1225 		   uint64_t length)
1226 {
1227 	struct spdk_fs_channel *channel = spdk_io_channel_get_ctx(_channel);
1228 	struct spdk_fs_request *req;
1229 	struct spdk_fs_cb_args *args;
1230 
1231 	req = alloc_fs_request(channel);
1232 	assert(req != NULL);
1233 
1234 	args = &req->args;
1235 
1236 	args->file = file;
1237 	args->op.truncate.length = length;
1238 	args->fn.file_op = __sem_post;
1239 	args->arg = &channel->sem;
1240 
1241 	channel->send_request(__truncate, req);
1242 	sem_wait(&channel->sem);
1243 	free_fs_request(req);
1244 }
1245 
1246 static void
1247 __rw_done(void *ctx, int bserrno)
1248 {
1249 	struct spdk_fs_request *req = ctx;
1250 	struct spdk_fs_cb_args *args = &req->args;
1251 
1252 	spdk_free(args->op.rw.pin_buf);
1253 	args->fn.file_op(args->arg, bserrno);
1254 	free_fs_request(req);
1255 }
1256 
1257 static void
1258 __read_done(void *ctx, int bserrno)
1259 {
1260 	struct spdk_fs_request *req = ctx;
1261 	struct spdk_fs_cb_args *args = &req->args;
1262 
1263 	if (args->op.rw.is_read) {
1264 		memcpy(args->op.rw.user_buf,
1265 		       args->op.rw.pin_buf + (args->op.rw.offset & 0xFFF),
1266 		       args->op.rw.length);
1267 		__rw_done(req, 0);
1268 	} else {
1269 		memcpy(args->op.rw.pin_buf + (args->op.rw.offset & 0xFFF),
1270 		       args->op.rw.user_buf,
1271 		       args->op.rw.length);
1272 		spdk_bs_io_write_blob(args->file->blob, args->op.rw.channel,
1273 				      args->op.rw.pin_buf,
1274 				      args->op.rw.start_page, args->op.rw.num_pages,
1275 				      __rw_done, req);
1276 	}
1277 }
1278 
1279 static void
1280 __do_blob_read(void *ctx, int fserrno)
1281 {
1282 	struct spdk_fs_request *req = ctx;
1283 	struct spdk_fs_cb_args *args = &req->args;
1284 
1285 	spdk_bs_io_read_blob(args->file->blob, args->op.rw.channel,
1286 			     args->op.rw.pin_buf,
1287 			     args->op.rw.start_page, args->op.rw.num_pages,
1288 			     __read_done, req);
1289 }
1290 
1291 static void
1292 __get_page_parameters(struct spdk_file *file, uint64_t offset, uint64_t length,
1293 		      uint64_t *start_page, uint32_t *page_size, uint64_t *num_pages)
1294 {
1295 	uint64_t end_page;
1296 
1297 	*page_size = spdk_bs_get_page_size(file->fs->bs);
1298 	*start_page = offset / *page_size;
1299 	end_page = (offset + length - 1) / *page_size;
1300 	*num_pages = (end_page - *start_page + 1);
1301 }
1302 
1303 static void
1304 __readwrite(struct spdk_file *file, struct spdk_io_channel *_channel,
1305 	    void *payload, uint64_t offset, uint64_t length,
1306 	    spdk_file_op_complete cb_fn, void *cb_arg, int is_read)
1307 {
1308 	struct spdk_fs_request *req;
1309 	struct spdk_fs_cb_args *args;
1310 	struct spdk_fs_channel *channel = spdk_io_channel_get_ctx(_channel);
1311 	uint64_t start_page, num_pages, pin_buf_length;
1312 	uint32_t page_size;
1313 
1314 	if (is_read && offset + length > file->length) {
1315 		cb_fn(cb_arg, -EINVAL);
1316 		return;
1317 	}
1318 
1319 	req = alloc_fs_request(channel);
1320 	if (req == NULL) {
1321 		cb_fn(cb_arg, -ENOMEM);
1322 		return;
1323 	}
1324 
1325 	args = &req->args;
1326 	args->fn.file_op = cb_fn;
1327 	args->arg = cb_arg;
1328 	args->file = file;
1329 	args->op.rw.channel = channel->bs_channel;
1330 	args->op.rw.user_buf = payload;
1331 	args->op.rw.is_read = is_read;
1332 	args->op.rw.offset = offset;
1333 	args->op.rw.length = length;
1334 
1335 	__get_page_parameters(file, offset, length, &start_page, &page_size, &num_pages);
1336 	pin_buf_length = num_pages * page_size;
1337 	args->op.rw.pin_buf = spdk_malloc(pin_buf_length, 4096, NULL);
1338 
1339 	args->op.rw.start_page = start_page;
1340 	args->op.rw.num_pages = num_pages;
1341 
1342 	if (!is_read && file->length < offset + length) {
1343 		spdk_file_truncate_async(file, offset + length, __do_blob_read, req);
1344 	} else {
1345 		__do_blob_read(req, 0);
1346 	}
1347 }
1348 
1349 void
1350 spdk_file_write_async(struct spdk_file *file, struct spdk_io_channel *channel,
1351 		      void *payload, uint64_t offset, uint64_t length,
1352 		      spdk_file_op_complete cb_fn, void *cb_arg)
1353 {
1354 	__readwrite(file, channel, payload, offset, length, cb_fn, cb_arg, 0);
1355 }
1356 
1357 void
1358 spdk_file_read_async(struct spdk_file *file, struct spdk_io_channel *channel,
1359 		     void *payload, uint64_t offset, uint64_t length,
1360 		     spdk_file_op_complete cb_fn, void *cb_arg)
1361 {
1362 	SPDK_TRACELOG(SPDK_TRACE_BLOBFS, "file=%s offset=%jx length=%jx\n",
1363 		      file->name, offset, length);
1364 	__readwrite(file, channel, payload, offset, length, cb_fn, cb_arg, 1);
1365 }
1366 
1367 struct spdk_io_channel *
1368 spdk_fs_alloc_io_channel(struct spdk_filesystem *fs, uint32_t priority)
1369 {
1370 	struct spdk_io_channel *io_channel;
1371 	struct spdk_fs_channel *fs_channel;
1372 	uint32_t max_ops = 512;
1373 
1374 	io_channel = spdk_get_io_channel(fs, priority, true, (void *)&max_ops);
1375 	fs_channel = spdk_io_channel_get_ctx(io_channel);
1376 	fs_channel->bs_channel = spdk_bs_alloc_io_channel(fs->bs, SPDK_IO_PRIORITY_DEFAULT, 512);
1377 	fs_channel->send_request = __send_request_direct;
1378 
1379 	return io_channel;
1380 }
1381 
1382 struct spdk_io_channel *
1383 spdk_fs_alloc_io_channel_sync(struct spdk_filesystem *fs, uint32_t priority)
1384 {
1385 	struct spdk_io_channel *io_channel;
1386 	struct spdk_fs_channel *fs_channel;
1387 	uint32_t max_ops = 16;
1388 
1389 	io_channel = spdk_get_io_channel(fs, priority, true, (void *)&max_ops);
1390 	fs_channel = spdk_io_channel_get_ctx(io_channel);
1391 	fs_channel->send_request = fs->send_request;
1392 
1393 	return io_channel;
1394 }
1395 
1396 void
1397 spdk_fs_free_io_channel(struct spdk_io_channel *channel)
1398 {
1399 	spdk_put_io_channel(channel);
1400 }
1401 
1402 void
1403 spdk_fs_set_cache_size(uint64_t size_in_mb)
1404 {
1405 	g_fs_cache_size = size_in_mb * 1024 * 1024;
1406 }
1407 
1408 uint64_t
1409 spdk_fs_get_cache_size(void)
1410 {
1411 	return g_fs_cache_size / (1024 * 1024);
1412 }
1413 
1414 static void __file_flush(void *_args);
1415 
1416 static void *
1417 alloc_cache_memory_buffer(struct spdk_file *context)
1418 {
1419 	struct spdk_file *file;
1420 	void *buf;
1421 
1422 	buf = spdk_mempool_get(g_cache_pool);
1423 	if (buf != NULL) {
1424 		return buf;
1425 	}
1426 
1427 	pthread_spin_lock(&g_caches_lock);
1428 	TAILQ_FOREACH(file, &g_caches, cache_tailq) {
1429 		if (!file->open_for_writing &&
1430 		    file->priority == SPDK_FILE_PRIORITY_LOW &&
1431 		    file != context) {
1432 			TAILQ_REMOVE(&g_caches, file, cache_tailq);
1433 			TAILQ_INSERT_TAIL(&g_caches, file, cache_tailq);
1434 			break;
1435 		}
1436 	}
1437 	pthread_spin_unlock(&g_caches_lock);
1438 	if (file != NULL) {
1439 		cache_free_buffers(file);
1440 		buf = spdk_mempool_get(g_cache_pool);
1441 		if (buf != NULL) {
1442 			return buf;
1443 		}
1444 	}
1445 
1446 	pthread_spin_lock(&g_caches_lock);
1447 	TAILQ_FOREACH(file, &g_caches, cache_tailq) {
1448 		if (!file->open_for_writing && file != context) {
1449 			TAILQ_REMOVE(&g_caches, file, cache_tailq);
1450 			TAILQ_INSERT_TAIL(&g_caches, file, cache_tailq);
1451 			break;
1452 		}
1453 	}
1454 	pthread_spin_unlock(&g_caches_lock);
1455 	if (file != NULL) {
1456 		cache_free_buffers(file);
1457 		buf = spdk_mempool_get(g_cache_pool);
1458 		if (buf != NULL) {
1459 			return buf;
1460 		}
1461 	}
1462 
1463 	pthread_spin_lock(&g_caches_lock);
1464 	TAILQ_FOREACH(file, &g_caches, cache_tailq) {
1465 		if (file != context) {
1466 			TAILQ_REMOVE(&g_caches, file, cache_tailq);
1467 			TAILQ_INSERT_TAIL(&g_caches, file, cache_tailq);
1468 			break;
1469 		}
1470 	}
1471 	pthread_spin_unlock(&g_caches_lock);
1472 	if (file != NULL) {
1473 		cache_free_buffers(file);
1474 		buf = spdk_mempool_get(g_cache_pool);
1475 		if (buf != NULL) {
1476 			return buf;
1477 		}
1478 	}
1479 
1480 	assert(false);
1481 	return NULL;
1482 }
1483 
1484 static struct cache_buffer *
1485 cache_insert_buffer(struct spdk_file *file, uint64_t offset)
1486 {
1487 	struct cache_buffer *buf;
1488 	int count = 0;
1489 
1490 	buf = calloc(1, sizeof(*buf));
1491 	if (buf == NULL) {
1492 		SPDK_TRACELOG(SPDK_TRACE_BLOBFS, "calloc failed\n");
1493 		return NULL;
1494 	}
1495 
1496 	buf->buf = alloc_cache_memory_buffer(file);
1497 	if (buf->buf == NULL) {
1498 		while (buf->buf == NULL) {
1499 			count++;
1500 			buf->buf = alloc_cache_memory_buffer(file);
1501 			/*
1502 			 * TODO: __free_oldest_cache() should eventually free some buffers.
1503 			 *  Should have a more sophisticated check here, instead of just
1504 			 *  bailing if 100 tries does not result in getting a free buffer.
1505 			 */
1506 			if (count == 100) {
1507 				SPDK_ERRLOG("could not allocate cache buffer\n");
1508 				assert(false);
1509 				return NULL;
1510 			}
1511 		}
1512 	}
1513 
1514 	buf->buf_size = CACHE_BUFFER_SIZE;
1515 	buf->offset = offset;
1516 
1517 	pthread_spin_lock(&g_caches_lock);
1518 	if (file->tree->present_mask == 0) {
1519 		TAILQ_INSERT_TAIL(&g_caches, file, cache_tailq);
1520 	}
1521 	file->tree = spdk_tree_insert_buffer(file->tree, buf);
1522 	pthread_spin_unlock(&g_caches_lock);
1523 
1524 	return buf;
1525 }
1526 
1527 static struct cache_buffer *
1528 cache_append_buffer(struct spdk_file *file)
1529 {
1530 	struct cache_buffer *last;
1531 
1532 	assert(file->last == NULL || file->last->bytes_filled == file->last->buf_size);
1533 	assert((file->append_pos % CACHE_BUFFER_SIZE) == 0);
1534 
1535 	last = cache_insert_buffer(file, file->append_pos);
1536 	if (last == NULL) {
1537 		SPDK_TRACELOG(SPDK_TRACE_BLOBFS, "cache_insert_buffer failed\n");
1538 		return NULL;
1539 	}
1540 
1541 	if (file->last != NULL) {
1542 		file->last->next = last;
1543 	}
1544 	file->last = last;
1545 
1546 	return last;
1547 }
1548 
1549 static void
1550 __wake_caller(struct spdk_fs_cb_args *args)
1551 {
1552 	sem_post(args->sem);
1553 }
1554 
1555 static void
1556 __file_cache_finish_sync(struct spdk_file *file)
1557 {
1558 	struct spdk_fs_request *sync_req;
1559 	struct spdk_fs_cb_args *sync_args;
1560 
1561 	pthread_spin_lock(&file->lock);
1562 	while (!TAILQ_EMPTY(&file->sync_requests)) {
1563 		sync_req = TAILQ_FIRST(&file->sync_requests);
1564 		sync_args = &sync_req->args;
1565 		if (sync_args->op.sync.offset > file->length_flushed) {
1566 			break;
1567 		}
1568 		BLOBFS_TRACE(file, "sync done offset=%jx\n", sync_args->op.sync.offset);
1569 		TAILQ_REMOVE(&file->sync_requests, sync_req, args.op.sync.tailq);
1570 		pthread_spin_unlock(&file->lock);
1571 		sync_args->fn.file_op(sync_args->arg, 0);
1572 		pthread_spin_lock(&file->lock);
1573 		free_fs_request(sync_req);
1574 	}
1575 	pthread_spin_unlock(&file->lock);
1576 }
1577 
1578 static void
1579 __file_cache_finish_sync_bs_cb(void *ctx, int bserrno)
1580 {
1581 	struct spdk_file *file = ctx;
1582 
1583 	__file_cache_finish_sync(file);
1584 }
1585 
1586 static void
1587 __free_args(struct spdk_fs_cb_args *args)
1588 {
1589 	struct spdk_fs_request *req;
1590 
1591 	if (!args->from_request) {
1592 		free(args);
1593 	} else {
1594 		/* Depends on args being at the start of the spdk_fs_request structure. */
1595 		req = (struct spdk_fs_request *)args;
1596 		free_fs_request(req);
1597 	}
1598 }
1599 
1600 static void
1601 __file_flush_done(void *arg, int bserrno)
1602 {
1603 	struct spdk_fs_cb_args *args = arg;
1604 	struct spdk_fs_request *sync_req;
1605 	struct spdk_file *file = args->file;
1606 	struct cache_buffer *next = args->op.flush.cache_buffer;
1607 
1608 	BLOBFS_TRACE(file, "length=%jx\n", args->op.flush.length);
1609 
1610 	pthread_spin_lock(&file->lock);
1611 	next->in_progress = false;
1612 	next->bytes_flushed += args->op.flush.length;
1613 	file->length_flushed += args->op.flush.length;
1614 	if (file->length_flushed > file->length) {
1615 		file->length = file->length_flushed;
1616 	}
1617 	if (next->bytes_flushed == next->buf_size) {
1618 		BLOBFS_TRACE(file, "write buffer fully flushed 0x%jx\n", file->length_flushed);
1619 		next = spdk_tree_find_buffer(file->tree, file->length_flushed);
1620 	}
1621 
1622 	TAILQ_FOREACH_REVERSE(sync_req, &file->sync_requests, sync_requests_head, args.op.sync.tailq) {
1623 		if (sync_req->args.op.sync.offset <= file->length_flushed) {
1624 			break;
1625 		}
1626 	}
1627 
1628 	if (sync_req != NULL) {
1629 		BLOBFS_TRACE(file, "set xattr length 0x%jx\n", file->length_flushed);
1630 		spdk_blob_md_set_xattr(file->blob, "length", &file->length_flushed,
1631 				       sizeof(file->length_flushed));
1632 
1633 		pthread_spin_unlock(&file->lock);
1634 		spdk_bs_md_sync_blob(file->blob, __file_cache_finish_sync_bs_cb, file);
1635 	} else {
1636 		pthread_spin_unlock(&file->lock);
1637 		__file_cache_finish_sync(file);
1638 	}
1639 
1640 	/*
1641 	 * Assert that there is no cached data that extends past the end of the underlying
1642 	 *  blob.
1643 	 */
1644 	assert(next == NULL || next->offset < __file_get_blob_size(file) ||
1645 	       next->bytes_filled == 0);
1646 
1647 	__file_flush(args);
1648 }
1649 
1650 static void
1651 __file_flush(void *_args)
1652 {
1653 	struct spdk_fs_cb_args *args = _args;
1654 	struct spdk_file *file = args->file;
1655 	struct cache_buffer *next;
1656 	uint64_t offset, length, start_page, num_pages;
1657 	uint32_t page_size;
1658 
1659 	pthread_spin_lock(&file->lock);
1660 	next = spdk_tree_find_buffer(file->tree, file->length_flushed);
1661 	if (next == NULL || next->in_progress) {
1662 		/*
1663 		 * There is either no data to flush, or a flush I/O is already in
1664 		 *  progress.  So return immediately - if a flush I/O is in
1665 		 *  progress we will flush more data after that is completed.
1666 		 */
1667 		__free_args(args);
1668 		pthread_spin_unlock(&file->lock);
1669 		return;
1670 	}
1671 
1672 	offset = next->offset + next->bytes_flushed;
1673 	length = next->bytes_filled - next->bytes_flushed;
1674 	if (length == 0) {
1675 		__free_args(args);
1676 		pthread_spin_unlock(&file->lock);
1677 		return;
1678 	}
1679 	args->op.flush.length = length;
1680 	args->op.flush.cache_buffer = next;
1681 
1682 	__get_page_parameters(file, offset, length, &start_page, &page_size, &num_pages);
1683 
1684 	next->in_progress = true;
1685 	BLOBFS_TRACE(file, "offset=%jx length=%jx page start=%jx num=%jx\n",
1686 		     offset, length, start_page, num_pages);
1687 	pthread_spin_unlock(&file->lock);
1688 	spdk_bs_io_write_blob(file->blob, file->fs->sync_fs_channel->bs_channel,
1689 			      next->buf + (start_page * page_size) - next->offset,
1690 			      start_page, num_pages,
1691 			      __file_flush_done, args);
1692 }
1693 
1694 static void
1695 __file_extend_done(void *arg, int bserrno)
1696 {
1697 	struct spdk_fs_cb_args *args = arg;
1698 
1699 	__wake_caller(args);
1700 }
1701 
1702 static void
1703 __file_extend_blob(void *_args)
1704 {
1705 	struct spdk_fs_cb_args *args = _args;
1706 	struct spdk_file *file = args->file;
1707 
1708 	spdk_bs_md_resize_blob(file->blob, args->op.resize.num_clusters);
1709 
1710 	spdk_bs_md_sync_blob(file->blob, __file_extend_done, args);
1711 }
1712 
1713 static void
1714 __rw_from_file_done(void *arg, int bserrno)
1715 {
1716 	struct spdk_fs_cb_args *args = arg;
1717 
1718 	__wake_caller(args);
1719 	__free_args(args);
1720 }
1721 
1722 static void
1723 __rw_from_file(void *_args)
1724 {
1725 	struct spdk_fs_cb_args *args = _args;
1726 	struct spdk_file *file = args->file;
1727 
1728 	if (args->op.rw.is_read) {
1729 		spdk_file_read_async(file, file->fs->sync_io_channel, args->op.rw.user_buf,
1730 				     args->op.rw.offset, args->op.rw.length,
1731 				     __rw_from_file_done, args);
1732 	} else {
1733 		spdk_file_write_async(file, file->fs->sync_io_channel, args->op.rw.user_buf,
1734 				      args->op.rw.offset, args->op.rw.length,
1735 				      __rw_from_file_done, args);
1736 	}
1737 }
1738 
1739 static int
1740 __send_rw_from_file(struct spdk_file *file, sem_t *sem, void *payload,
1741 		    uint64_t offset, uint64_t length, bool is_read)
1742 {
1743 	struct spdk_fs_cb_args *args;
1744 
1745 	args = calloc(1, sizeof(*args));
1746 	if (args == NULL) {
1747 		sem_post(sem);
1748 		return -ENOMEM;
1749 	}
1750 
1751 	args->file = file;
1752 	args->sem = sem;
1753 	args->op.rw.user_buf = payload;
1754 	args->op.rw.offset = offset;
1755 	args->op.rw.length = length;
1756 	args->op.rw.is_read = is_read;
1757 	file->fs->send_request(__rw_from_file, args);
1758 	return 0;
1759 }
1760 
1761 int
1762 spdk_file_write(struct spdk_file *file, struct spdk_io_channel *_channel,
1763 		void *payload, uint64_t offset, uint64_t length)
1764 {
1765 	struct spdk_fs_channel *channel = spdk_io_channel_get_ctx(_channel);
1766 	struct spdk_fs_cb_args *args;
1767 	uint64_t rem_length, copy, blob_size, cluster_sz;
1768 	uint32_t cache_buffers_filled = 0;
1769 	uint8_t *cur_payload;
1770 	struct cache_buffer *last;
1771 
1772 	BLOBFS_TRACE_RW(file, "offset=%jx length=%jx\n", offset, length);
1773 
1774 	if (length == 0) {
1775 		return 0;
1776 	}
1777 
1778 	if (offset != file->append_pos) {
1779 		BLOBFS_TRACE(file, " error offset=%jx append_pos=%jx\n", offset, file->append_pos);
1780 		return -EINVAL;
1781 	}
1782 
1783 	pthread_spin_lock(&file->lock);
1784 	file->open_for_writing = true;
1785 
1786 	if (file->last == NULL) {
1787 		if (file->append_pos % CACHE_BUFFER_SIZE == 0) {
1788 			cache_append_buffer(file);
1789 		} else {
1790 			int rc;
1791 
1792 			file->append_pos += length;
1793 			rc = __send_rw_from_file(file, &channel->sem, payload,
1794 						 offset, length, false);
1795 			pthread_spin_unlock(&file->lock);
1796 			sem_wait(&channel->sem);
1797 			return rc;
1798 		}
1799 	}
1800 
1801 	blob_size = __file_get_blob_size(file);
1802 
1803 	if ((offset + length) > blob_size) {
1804 		struct spdk_fs_cb_args extend_args = {};
1805 
1806 		cluster_sz = file->fs->bs_opts.cluster_sz;
1807 		extend_args.sem = &channel->sem;
1808 		extend_args.op.resize.num_clusters = __bytes_to_clusters((offset + length), cluster_sz);
1809 		extend_args.file = file;
1810 		BLOBFS_TRACE(file, "start resize to %u clusters\n", extend_args.op.resize.num_clusters);
1811 		pthread_spin_unlock(&file->lock);
1812 		file->fs->send_request(__file_extend_blob, &extend_args);
1813 		sem_wait(&channel->sem);
1814 	}
1815 
1816 	last = file->last;
1817 	rem_length = length;
1818 	cur_payload = payload;
1819 	while (rem_length > 0) {
1820 		copy = last->buf_size - last->bytes_filled;
1821 		if (copy > rem_length) {
1822 			copy = rem_length;
1823 		}
1824 		BLOBFS_TRACE_RW(file, "  fill offset=%jx length=%jx\n", file->append_pos, copy);
1825 		memcpy(&last->buf[last->bytes_filled], cur_payload, copy);
1826 		file->append_pos += copy;
1827 		if (file->length < file->append_pos) {
1828 			file->length = file->append_pos;
1829 		}
1830 		cur_payload += copy;
1831 		last->bytes_filled += copy;
1832 		rem_length -= copy;
1833 		if (last->bytes_filled == last->buf_size) {
1834 			cache_buffers_filled++;
1835 			last = cache_append_buffer(file);
1836 			if (last == NULL) {
1837 				BLOBFS_TRACE(file, "nomem\n");
1838 				pthread_spin_unlock(&file->lock);
1839 				return -ENOMEM;
1840 			}
1841 		}
1842 	}
1843 
1844 	if (cache_buffers_filled == 0) {
1845 		pthread_spin_unlock(&file->lock);
1846 		return 0;
1847 	}
1848 
1849 	args = calloc(1, sizeof(*args));
1850 	if (args == NULL) {
1851 		pthread_spin_unlock(&file->lock);
1852 		return -ENOMEM;
1853 	}
1854 
1855 	args->file = file;
1856 	file->fs->send_request(__file_flush, args);
1857 	pthread_spin_unlock(&file->lock);
1858 	return 0;
1859 }
1860 
1861 static void
1862 __readahead_done(void *arg, int bserrno)
1863 {
1864 	struct spdk_fs_cb_args *args = arg;
1865 	struct cache_buffer *cache_buffer = args->op.readahead.cache_buffer;
1866 	struct spdk_file *file = args->file;
1867 
1868 	BLOBFS_TRACE(file, "offset=%jx\n", cache_buffer->offset);
1869 
1870 	pthread_spin_lock(&file->lock);
1871 	cache_buffer->bytes_filled = args->op.readahead.length;
1872 	cache_buffer->bytes_flushed = args->op.readahead.length;
1873 	cache_buffer->in_progress = false;
1874 	pthread_spin_unlock(&file->lock);
1875 
1876 	__free_args(args);
1877 }
1878 
1879 static void
1880 __readahead(void *_args)
1881 {
1882 	struct spdk_fs_cb_args *args = _args;
1883 	struct spdk_file *file = args->file;
1884 	uint64_t offset, length, start_page, num_pages;
1885 	uint32_t page_size;
1886 
1887 	offset = args->op.readahead.offset;
1888 	length = args->op.readahead.length;
1889 	assert(length > 0);
1890 
1891 	__get_page_parameters(file, offset, length, &start_page, &page_size, &num_pages);
1892 
1893 	BLOBFS_TRACE(file, "offset=%jx length=%jx page start=%jx num=%jx\n",
1894 		     offset, length, start_page, num_pages);
1895 	spdk_bs_io_read_blob(file->blob, file->fs->sync_fs_channel->bs_channel,
1896 			     args->op.readahead.cache_buffer->buf,
1897 			     start_page, num_pages,
1898 			     __readahead_done, args);
1899 }
1900 
1901 static uint64_t
1902 __next_cache_buffer_offset(uint64_t offset)
1903 {
1904 	return (offset + CACHE_BUFFER_SIZE) & ~(CACHE_TREE_LEVEL_MASK(0));
1905 }
1906 
1907 static void
1908 check_readahead(struct spdk_file *file, uint64_t offset)
1909 {
1910 	struct spdk_fs_cb_args *args;
1911 
1912 	offset = __next_cache_buffer_offset(offset);
1913 	if (spdk_tree_find_buffer(file->tree, offset) != NULL || file->length <= offset) {
1914 		return;
1915 	}
1916 
1917 	BLOBFS_TRACE(file, "offset=%jx\n", offset);
1918 	args = calloc(1, sizeof(*args));
1919 	args->file = file;
1920 	args->op.readahead.offset = offset;
1921 	args->op.readahead.cache_buffer = cache_insert_buffer(file, offset);
1922 	args->op.readahead.cache_buffer->in_progress = true;
1923 	if (file->length < (offset + CACHE_BUFFER_SIZE)) {
1924 		args->op.readahead.length = file->length & (CACHE_BUFFER_SIZE - 1);
1925 	} else {
1926 		args->op.readahead.length = CACHE_BUFFER_SIZE;
1927 	}
1928 	file->fs->send_request(__readahead, args);
1929 }
1930 
1931 static int
1932 __file_read(struct spdk_file *file, void *payload, uint64_t offset, uint64_t length, sem_t *sem)
1933 {
1934 	struct cache_buffer *buf;
1935 
1936 	buf = spdk_tree_find_filled_buffer(file->tree, offset);
1937 	if (buf == NULL) {
1938 		return __send_rw_from_file(file, sem, payload, offset, length, true);
1939 	}
1940 
1941 	if ((offset + length) > (buf->offset + buf->bytes_filled)) {
1942 		length = buf->offset + buf->bytes_filled - offset;
1943 	}
1944 	BLOBFS_TRACE(file, "read %p offset=%ju length=%ju\n", payload, offset, length);
1945 	memcpy(payload, &buf->buf[offset - buf->offset], length);
1946 	if ((offset + length) % CACHE_BUFFER_SIZE == 0) {
1947 		pthread_spin_lock(&g_caches_lock);
1948 		spdk_tree_remove_buffer(file->tree, buf);
1949 		if (file->tree->present_mask == 0) {
1950 			TAILQ_REMOVE(&g_caches, file, cache_tailq);
1951 		}
1952 		pthread_spin_unlock(&g_caches_lock);
1953 	}
1954 
1955 	sem_post(sem);
1956 	return 0;
1957 }
1958 
1959 int64_t
1960 spdk_file_read(struct spdk_file *file, struct spdk_io_channel *_channel,
1961 	       void *payload, uint64_t offset, uint64_t length)
1962 {
1963 	struct spdk_fs_channel *channel = spdk_io_channel_get_ctx(_channel);
1964 	uint64_t final_offset, final_length;
1965 	uint32_t sub_reads = 0;
1966 	int rc = 0;
1967 
1968 	pthread_spin_lock(&file->lock);
1969 
1970 	BLOBFS_TRACE_RW(file, "offset=%ju length=%ju\n", offset, length);
1971 
1972 	file->open_for_writing = false;
1973 
1974 	if (length == 0 || offset >= file->length) {
1975 		pthread_spin_unlock(&file->lock);
1976 		return 0;
1977 	}
1978 
1979 	if (offset + length > file->length) {
1980 		length = file->length - offset;
1981 	}
1982 
1983 	if (offset != file->next_seq_offset) {
1984 		file->seq_byte_count = 0;
1985 	}
1986 	file->seq_byte_count += length;
1987 	file->next_seq_offset = offset + length;
1988 	if (file->seq_byte_count >= CACHE_READAHEAD_THRESHOLD) {
1989 		check_readahead(file, offset);
1990 		check_readahead(file, offset + CACHE_BUFFER_SIZE);
1991 	}
1992 
1993 	final_length = 0;
1994 	final_offset = offset + length;
1995 	while (offset < final_offset) {
1996 		length = NEXT_CACHE_BUFFER_OFFSET(offset) - offset;
1997 		if (length > (final_offset - offset)) {
1998 			length = final_offset - offset;
1999 		}
2000 		rc = __file_read(file, payload, offset, length, &channel->sem);
2001 		if (rc == 0) {
2002 			final_length += length;
2003 		} else {
2004 			break;
2005 		}
2006 		payload += length;
2007 		offset += length;
2008 		sub_reads++;
2009 	}
2010 	pthread_spin_unlock(&file->lock);
2011 	while (sub_reads-- > 0) {
2012 		sem_wait(&channel->sem);
2013 	}
2014 	if (rc == 0) {
2015 		return final_length;
2016 	} else {
2017 		return rc;
2018 	}
2019 }
2020 
2021 static void
2022 _file_sync(struct spdk_file *file, struct spdk_fs_channel *channel,
2023 	   spdk_file_op_complete cb_fn, void *cb_arg)
2024 {
2025 	struct spdk_fs_request *sync_req;
2026 	struct spdk_fs_request *flush_req;
2027 	struct spdk_fs_cb_args *sync_args;
2028 	struct spdk_fs_cb_args *flush_args;
2029 
2030 	BLOBFS_TRACE(file, "offset=%jx\n", file->append_pos);
2031 
2032 	pthread_spin_lock(&file->lock);
2033 	if (file->append_pos <= file->length_flushed || file->last == NULL) {
2034 		BLOBFS_TRACE(file, "done - no data to flush\n");
2035 		pthread_spin_unlock(&file->lock);
2036 		cb_fn(cb_arg, 0);
2037 		return;
2038 	}
2039 
2040 	sync_req = alloc_fs_request(channel);
2041 	assert(sync_req != NULL);
2042 	sync_args = &sync_req->args;
2043 
2044 	flush_req = alloc_fs_request(channel);
2045 	assert(flush_req != NULL);
2046 	flush_args = &flush_req->args;
2047 
2048 	sync_args->file = file;
2049 	sync_args->fn.file_op = cb_fn;
2050 	sync_args->arg = cb_arg;
2051 	sync_args->op.sync.offset = file->append_pos;
2052 	TAILQ_INSERT_TAIL(&file->sync_requests, sync_req, args.op.sync.tailq);
2053 	pthread_spin_unlock(&file->lock);
2054 
2055 	flush_args->file = file;
2056 	channel->send_request(__file_flush, flush_args);
2057 }
2058 
2059 int
2060 spdk_file_sync(struct spdk_file *file, struct spdk_io_channel *_channel)
2061 {
2062 	struct spdk_fs_channel *channel = spdk_io_channel_get_ctx(_channel);
2063 
2064 	_file_sync(file, channel, __sem_post, &channel->sem);
2065 	sem_wait(&channel->sem);
2066 
2067 	return 0;
2068 }
2069 
2070 void
2071 spdk_file_sync_async(struct spdk_file *file, struct spdk_io_channel *_channel,
2072 		     spdk_file_op_complete cb_fn, void *cb_arg)
2073 {
2074 	struct spdk_fs_channel *channel = spdk_io_channel_get_ctx(_channel);
2075 
2076 	_file_sync(file, channel, cb_fn, cb_arg);
2077 }
2078 
2079 void
2080 spdk_file_set_priority(struct spdk_file *file, uint32_t priority)
2081 {
2082 	BLOBFS_TRACE(file, "priority=%u\n", priority);
2083 	file->priority = priority;
2084 
2085 }
2086 
2087 /*
2088  * Close routines
2089  */
2090 
2091 static void
2092 __file_close_async_done(void *ctx, int bserrno)
2093 {
2094 	struct spdk_fs_request *req = ctx;
2095 	struct spdk_fs_cb_args *args = &req->args;
2096 
2097 	args->fn.file_op(args->arg, bserrno);
2098 	free_fs_request(req);
2099 }
2100 
2101 static void
2102 __file_close_async(struct spdk_file *file, struct spdk_fs_request *req)
2103 {
2104 	pthread_spin_lock(&file->lock);
2105 	if (file->ref_count == 0) {
2106 		pthread_spin_unlock(&file->lock);
2107 		__file_close_async_done(req, -EBADF);
2108 		return;
2109 	}
2110 
2111 	file->ref_count--;
2112 	if (file->ref_count > 0) {
2113 		pthread_spin_unlock(&file->lock);
2114 		__file_close_async_done(req, 0);
2115 		return;
2116 	}
2117 
2118 	pthread_spin_unlock(&file->lock);
2119 
2120 	spdk_bs_md_close_blob(&file->blob, __file_close_async_done, req);
2121 }
2122 
2123 static void
2124 __file_close_async__sync_done(void *arg, int fserrno)
2125 {
2126 	struct spdk_fs_request *req = arg;
2127 	struct spdk_fs_cb_args *args = &req->args;
2128 
2129 	__file_close_async(args->file, req);
2130 }
2131 
2132 void
2133 spdk_file_close_async(struct spdk_file *file, spdk_file_op_complete cb_fn, void *cb_arg)
2134 {
2135 	struct spdk_fs_request *req;
2136 	struct spdk_fs_cb_args *args;
2137 
2138 	req = alloc_fs_request(file->fs->md_fs_channel);
2139 	if (req == NULL) {
2140 		cb_fn(cb_arg, -ENOMEM);
2141 		return;
2142 	}
2143 
2144 	args = &req->args;
2145 	args->file = file;
2146 	args->fn.file_op = cb_fn;
2147 	args->arg = cb_arg;
2148 
2149 	spdk_file_sync_async(file, file->fs->md_io_channel, __file_close_async__sync_done, req);
2150 }
2151 
2152 static void
2153 __file_close_done(void *arg, int fserrno)
2154 {
2155 	struct spdk_fs_cb_args *args = arg;
2156 
2157 	args->rc = fserrno;
2158 	sem_post(args->sem);
2159 }
2160 
2161 static void
2162 __file_close(void *arg)
2163 {
2164 	struct spdk_fs_request *req = arg;
2165 	struct spdk_fs_cb_args *args = &req->args;
2166 	struct spdk_file *file = args->file;
2167 
2168 	__file_close_async(file, req);
2169 }
2170 
2171 int
2172 spdk_file_close(struct spdk_file *file, struct spdk_io_channel *_channel)
2173 {
2174 	struct spdk_fs_channel *channel = spdk_io_channel_get_ctx(_channel);
2175 	struct spdk_fs_request *req;
2176 	struct spdk_fs_cb_args *args;
2177 
2178 	req = alloc_fs_request(channel);
2179 	assert(req != NULL);
2180 
2181 	args = &req->args;
2182 
2183 	spdk_file_sync(file, _channel);
2184 	BLOBFS_TRACE(file, "name=%s\n", file->name);
2185 	args->file = file;
2186 	args->sem = &channel->sem;
2187 	args->fn.file_op = __file_close_done;
2188 	args->arg = req;
2189 	channel->send_request(__file_close, req);
2190 	sem_wait(&channel->sem);
2191 
2192 	return args->rc;
2193 }
2194 
2195 static void
2196 cache_free_buffers(struct spdk_file *file)
2197 {
2198 	BLOBFS_TRACE(file, "free=%s\n", file->name);
2199 	pthread_spin_lock(&file->lock);
2200 	pthread_spin_lock(&g_caches_lock);
2201 	if (file->tree->present_mask == 0) {
2202 		pthread_spin_unlock(&g_caches_lock);
2203 		pthread_spin_unlock(&file->lock);
2204 		return;
2205 	}
2206 	spdk_tree_free_buffers(file->tree);
2207 	if (file->tree->present_mask == 0) {
2208 		TAILQ_REMOVE(&g_caches, file, cache_tailq);
2209 	}
2210 	file->last = NULL;
2211 	pthread_spin_unlock(&g_caches_lock);
2212 	pthread_spin_unlock(&file->lock);
2213 }
2214 
2215 SPDK_LOG_REGISTER_TRACE_FLAG("blobfs", SPDK_TRACE_BLOBFS);
2216 SPDK_LOG_REGISTER_TRACE_FLAG("blobfs_rw", SPDK_TRACE_BLOBFS_RW);
2217