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