xref: /spdk/lib/blobfs/blobfs.c (revision 8a0a98d35e21f282088edf28b9e8da66ec390e3a)
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 	assert(req != NULL);
836 
837 	req->args.fs = fs;
838 	req->args.op.stat.name = name;
839 	req->args.fn.stat_op = __copy_stat;
840 	req->args.arg = stat;
841 	req->args.sem = &channel->sem;
842 	channel->send_request(__file_stat, req);
843 	sem_wait(&channel->sem);
844 
845 	rc = req->args.rc;
846 	free_fs_request(req);
847 
848 	return rc;
849 }
850 
851 static void
852 fs_create_blob_close_cb(void *ctx, int bserrno)
853 {
854 	struct spdk_fs_request *req = ctx;
855 	struct spdk_fs_cb_args *args = &req->args;
856 
857 	args->fn.file_op(args->arg, bserrno);
858 	free_fs_request(req);
859 }
860 
861 static void
862 fs_create_blob_resize_cb(void *ctx, int bserrno)
863 {
864 	struct spdk_fs_request *req = ctx;
865 	struct spdk_fs_cb_args *args = &req->args;
866 	struct spdk_file *f = args->file;
867 	struct spdk_blob *blob = args->op.create.blob;
868 	uint64_t length = 0;
869 
870 	spdk_blob_set_xattr(blob, "name", f->name, strlen(f->name) + 1);
871 	spdk_blob_set_xattr(blob, "length", &length, sizeof(length));
872 
873 	spdk_blob_close(blob, fs_create_blob_close_cb, args);
874 }
875 
876 static void
877 fs_create_blob_open_cb(void *ctx, struct spdk_blob *blob, int bserrno)
878 {
879 	struct spdk_fs_request *req = ctx;
880 	struct spdk_fs_cb_args *args = &req->args;
881 
882 	args->op.create.blob = blob;
883 	spdk_blob_resize(blob, 1, fs_create_blob_resize_cb, req);
884 }
885 
886 static void
887 fs_create_blob_create_cb(void *ctx, spdk_blob_id blobid, int bserrno)
888 {
889 	struct spdk_fs_request *req = ctx;
890 	struct spdk_fs_cb_args *args = &req->args;
891 	struct spdk_file *f = args->file;
892 
893 	f->blobid = blobid;
894 	spdk_bs_open_blob(f->fs->bs, blobid, fs_create_blob_open_cb, req);
895 }
896 
897 void
898 spdk_fs_create_file_async(struct spdk_filesystem *fs, const char *name,
899 			  spdk_file_op_complete cb_fn, void *cb_arg)
900 {
901 	struct spdk_file *file;
902 	struct spdk_fs_request *req;
903 	struct spdk_fs_cb_args *args;
904 
905 	if (strnlen(name, SPDK_FILE_NAME_MAX + 1) == SPDK_FILE_NAME_MAX + 1) {
906 		cb_fn(cb_arg, -ENAMETOOLONG);
907 		return;
908 	}
909 
910 	file = fs_find_file(fs, name);
911 	if (file != NULL) {
912 		cb_fn(cb_arg, -EEXIST);
913 		return;
914 	}
915 
916 	file = file_alloc(fs);
917 	if (file == NULL) {
918 		cb_fn(cb_arg, -ENOMEM);
919 		return;
920 	}
921 
922 	req = alloc_fs_request(fs->md_target.md_fs_channel);
923 	if (req == NULL) {
924 		cb_fn(cb_arg, -ENOMEM);
925 		return;
926 	}
927 
928 	args = &req->args;
929 	args->file = file;
930 	args->fn.file_op = cb_fn;
931 	args->arg = cb_arg;
932 
933 	file->name = strdup(name);
934 	spdk_bs_create_blob(fs->bs, fs_create_blob_create_cb, args);
935 }
936 
937 static void
938 __fs_create_file_done(void *arg, int fserrno)
939 {
940 	struct spdk_fs_request *req = arg;
941 	struct spdk_fs_cb_args *args = &req->args;
942 
943 	args->rc = fserrno;
944 	sem_post(args->sem);
945 	SPDK_DEBUGLOG(SPDK_LOG_BLOBFS, "file=%s\n", args->op.create.name);
946 }
947 
948 static void
949 __fs_create_file(void *arg)
950 {
951 	struct spdk_fs_request *req = arg;
952 	struct spdk_fs_cb_args *args = &req->args;
953 
954 	SPDK_DEBUGLOG(SPDK_LOG_BLOBFS, "file=%s\n", args->op.create.name);
955 	spdk_fs_create_file_async(args->fs, args->op.create.name, __fs_create_file_done, req);
956 }
957 
958 int
959 spdk_fs_create_file(struct spdk_filesystem *fs, struct spdk_io_channel *_channel, const char *name)
960 {
961 	struct spdk_fs_channel *channel = spdk_io_channel_get_ctx(_channel);
962 	struct spdk_fs_request *req;
963 	struct spdk_fs_cb_args *args;
964 	int rc;
965 
966 	SPDK_DEBUGLOG(SPDK_LOG_BLOBFS, "file=%s\n", name);
967 
968 	req = alloc_fs_request(channel);
969 	assert(req != NULL);
970 
971 	args = &req->args;
972 	args->fs = fs;
973 	args->op.create.name = name;
974 	args->sem = &channel->sem;
975 	fs->send_request(__fs_create_file, req);
976 	sem_wait(&channel->sem);
977 	rc = args->rc;
978 	free_fs_request(req);
979 
980 	return rc;
981 }
982 
983 static void
984 fs_open_blob_done(void *ctx, struct spdk_blob *blob, int bserrno)
985 {
986 	struct spdk_fs_request *req = ctx;
987 	struct spdk_fs_cb_args *args = &req->args;
988 	struct spdk_file *f = args->file;
989 
990 	f->blob = blob;
991 	while (!TAILQ_EMPTY(&f->open_requests)) {
992 		req = TAILQ_FIRST(&f->open_requests);
993 		args = &req->args;
994 		TAILQ_REMOVE(&f->open_requests, req, args.op.open.tailq);
995 		args->fn.file_op_with_handle(args->arg, f, bserrno);
996 		free_fs_request(req);
997 	}
998 }
999 
1000 static void
1001 fs_open_blob_create_cb(void *ctx, int bserrno)
1002 {
1003 	struct spdk_fs_request *req = ctx;
1004 	struct spdk_fs_cb_args *args = &req->args;
1005 	struct spdk_file *file = args->file;
1006 	struct spdk_filesystem *fs = args->fs;
1007 
1008 	if (file == NULL) {
1009 		/*
1010 		 * This is from an open with CREATE flag - the file
1011 		 *  is now created so look it up in the file list for this
1012 		 *  filesystem.
1013 		 */
1014 		file = fs_find_file(fs, args->op.open.name);
1015 		assert(file != NULL);
1016 		args->file = file;
1017 	}
1018 
1019 	file->ref_count++;
1020 	TAILQ_INSERT_TAIL(&file->open_requests, req, args.op.open.tailq);
1021 	if (file->ref_count == 1) {
1022 		assert(file->blob == NULL);
1023 		spdk_bs_open_blob(fs->bs, file->blobid, fs_open_blob_done, req);
1024 	} else if (file->blob != NULL) {
1025 		fs_open_blob_done(req, file->blob, 0);
1026 	} else {
1027 		/*
1028 		 * The blob open for this file is in progress due to a previous
1029 		 *  open request.  When that open completes, it will invoke the
1030 		 *  open callback for this request.
1031 		 */
1032 	}
1033 }
1034 
1035 void
1036 spdk_fs_open_file_async(struct spdk_filesystem *fs, const char *name, uint32_t flags,
1037 			spdk_file_op_with_handle_complete cb_fn, void *cb_arg)
1038 {
1039 	struct spdk_file *f = NULL;
1040 	struct spdk_fs_request *req;
1041 	struct spdk_fs_cb_args *args;
1042 
1043 	if (strnlen(name, SPDK_FILE_NAME_MAX + 1) == SPDK_FILE_NAME_MAX + 1) {
1044 		cb_fn(cb_arg, NULL, -ENAMETOOLONG);
1045 		return;
1046 	}
1047 
1048 	f = fs_find_file(fs, name);
1049 	if (f == NULL && !(flags & SPDK_BLOBFS_OPEN_CREATE)) {
1050 		cb_fn(cb_arg, NULL, -ENOENT);
1051 		return;
1052 	}
1053 
1054 	if (f != NULL && f->is_deleted == true) {
1055 		cb_fn(cb_arg, NULL, -ENOENT);
1056 		return;
1057 	}
1058 
1059 	req = alloc_fs_request(fs->md_target.md_fs_channel);
1060 	if (req == NULL) {
1061 		cb_fn(cb_arg, NULL, -ENOMEM);
1062 		return;
1063 	}
1064 
1065 	args = &req->args;
1066 	args->fn.file_op_with_handle = cb_fn;
1067 	args->arg = cb_arg;
1068 	args->file = f;
1069 	args->fs = fs;
1070 	args->op.open.name = name;
1071 
1072 	if (f == NULL) {
1073 		spdk_fs_create_file_async(fs, name, fs_open_blob_create_cb, req);
1074 	} else {
1075 		fs_open_blob_create_cb(req, 0);
1076 	}
1077 }
1078 
1079 static void
1080 __fs_open_file_done(void *arg, struct spdk_file *file, int bserrno)
1081 {
1082 	struct spdk_fs_request *req = arg;
1083 	struct spdk_fs_cb_args *args = &req->args;
1084 
1085 	args->file = file;
1086 	args->rc = bserrno;
1087 	sem_post(args->sem);
1088 	SPDK_DEBUGLOG(SPDK_LOG_BLOBFS, "file=%s\n", args->op.open.name);
1089 }
1090 
1091 static void
1092 __fs_open_file(void *arg)
1093 {
1094 	struct spdk_fs_request *req = arg;
1095 	struct spdk_fs_cb_args *args = &req->args;
1096 
1097 	SPDK_DEBUGLOG(SPDK_LOG_BLOBFS, "file=%s\n", args->op.open.name);
1098 	spdk_fs_open_file_async(args->fs, args->op.open.name, args->op.open.flags,
1099 				__fs_open_file_done, req);
1100 }
1101 
1102 int
1103 spdk_fs_open_file(struct spdk_filesystem *fs, struct spdk_io_channel *_channel,
1104 		  const char *name, uint32_t flags, struct spdk_file **file)
1105 {
1106 	struct spdk_fs_channel *channel = spdk_io_channel_get_ctx(_channel);
1107 	struct spdk_fs_request *req;
1108 	struct spdk_fs_cb_args *args;
1109 	int rc;
1110 
1111 	SPDK_DEBUGLOG(SPDK_LOG_BLOBFS, "file=%s\n", name);
1112 
1113 	req = alloc_fs_request(channel);
1114 	assert(req != NULL);
1115 
1116 	args = &req->args;
1117 	args->fs = fs;
1118 	args->op.open.name = name;
1119 	args->op.open.flags = flags;
1120 	args->sem = &channel->sem;
1121 	fs->send_request(__fs_open_file, req);
1122 	sem_wait(&channel->sem);
1123 	rc = args->rc;
1124 	if (rc == 0) {
1125 		*file = args->file;
1126 	} else {
1127 		*file = NULL;
1128 	}
1129 	free_fs_request(req);
1130 
1131 	return rc;
1132 }
1133 
1134 static void
1135 fs_rename_blob_close_cb(void *ctx, int bserrno)
1136 {
1137 	struct spdk_fs_request *req = ctx;
1138 	struct spdk_fs_cb_args *args = &req->args;
1139 
1140 	args->fn.fs_op(args->arg, bserrno);
1141 	free_fs_request(req);
1142 }
1143 
1144 static void
1145 fs_rename_blob_open_cb(void *ctx, struct spdk_blob *blob, int bserrno)
1146 {
1147 	struct spdk_fs_request *req = ctx;
1148 	struct spdk_fs_cb_args *args = &req->args;
1149 	const char *new_name = args->op.rename.new_name;
1150 
1151 	spdk_blob_set_xattr(blob, "name", new_name, strlen(new_name) + 1);
1152 	spdk_blob_close(blob, fs_rename_blob_close_cb, req);
1153 }
1154 
1155 static void
1156 __spdk_fs_md_rename_file(struct spdk_fs_request *req)
1157 {
1158 	struct spdk_fs_cb_args *args = &req->args;
1159 	struct spdk_file *f;
1160 
1161 	f = fs_find_file(args->fs, args->op.rename.old_name);
1162 	if (f == NULL) {
1163 		args->fn.fs_op(args->arg, -ENOENT);
1164 		free_fs_request(req);
1165 		return;
1166 	}
1167 
1168 	free(f->name);
1169 	f->name = strdup(args->op.rename.new_name);
1170 	args->file = f;
1171 	spdk_bs_open_blob(args->fs->bs, f->blobid, fs_rename_blob_open_cb, req);
1172 }
1173 
1174 static void
1175 fs_rename_delete_done(void *arg, int fserrno)
1176 {
1177 	__spdk_fs_md_rename_file(arg);
1178 }
1179 
1180 void
1181 spdk_fs_rename_file_async(struct spdk_filesystem *fs,
1182 			  const char *old_name, const char *new_name,
1183 			  spdk_file_op_complete cb_fn, void *cb_arg)
1184 {
1185 	struct spdk_file *f;
1186 	struct spdk_fs_request *req;
1187 	struct spdk_fs_cb_args *args;
1188 
1189 	SPDK_DEBUGLOG(SPDK_LOG_BLOBFS, "old=%s new=%s\n", old_name, new_name);
1190 	if (strnlen(new_name, SPDK_FILE_NAME_MAX + 1) == SPDK_FILE_NAME_MAX + 1) {
1191 		cb_fn(cb_arg, -ENAMETOOLONG);
1192 		return;
1193 	}
1194 
1195 	req = alloc_fs_request(fs->md_target.md_fs_channel);
1196 	if (req == NULL) {
1197 		cb_fn(cb_arg, -ENOMEM);
1198 		return;
1199 	}
1200 
1201 	args = &req->args;
1202 	args->fn.fs_op = cb_fn;
1203 	args->fs = fs;
1204 	args->arg = cb_arg;
1205 	args->op.rename.old_name = old_name;
1206 	args->op.rename.new_name = new_name;
1207 
1208 	f = fs_find_file(fs, new_name);
1209 	if (f == NULL) {
1210 		__spdk_fs_md_rename_file(req);
1211 		return;
1212 	}
1213 
1214 	/*
1215 	 * The rename overwrites an existing file.  So delete the existing file, then
1216 	 *  do the actual rename.
1217 	 */
1218 	spdk_fs_delete_file_async(fs, new_name, fs_rename_delete_done, req);
1219 }
1220 
1221 static void
1222 __fs_rename_file_done(void *arg, int fserrno)
1223 {
1224 	struct spdk_fs_request *req = arg;
1225 	struct spdk_fs_cb_args *args = &req->args;
1226 
1227 	args->rc = fserrno;
1228 	sem_post(args->sem);
1229 }
1230 
1231 static void
1232 __fs_rename_file(void *arg)
1233 {
1234 	struct spdk_fs_request *req = arg;
1235 	struct spdk_fs_cb_args *args = &req->args;
1236 
1237 	spdk_fs_rename_file_async(args->fs, args->op.rename.old_name, args->op.rename.new_name,
1238 				  __fs_rename_file_done, req);
1239 }
1240 
1241 int
1242 spdk_fs_rename_file(struct spdk_filesystem *fs, struct spdk_io_channel *_channel,
1243 		    const char *old_name, const char *new_name)
1244 {
1245 	struct spdk_fs_channel *channel = spdk_io_channel_get_ctx(_channel);
1246 	struct spdk_fs_request *req;
1247 	struct spdk_fs_cb_args *args;
1248 	int rc;
1249 
1250 	req = alloc_fs_request(channel);
1251 	assert(req != NULL);
1252 
1253 	args = &req->args;
1254 
1255 	args->fs = fs;
1256 	args->op.rename.old_name = old_name;
1257 	args->op.rename.new_name = new_name;
1258 	args->sem = &channel->sem;
1259 	fs->send_request(__fs_rename_file, req);
1260 	sem_wait(&channel->sem);
1261 	rc = args->rc;
1262 	free_fs_request(req);
1263 	return rc;
1264 }
1265 
1266 static void
1267 blob_delete_cb(void *ctx, int bserrno)
1268 {
1269 	struct spdk_fs_request *req = ctx;
1270 	struct spdk_fs_cb_args *args = &req->args;
1271 
1272 	args->fn.file_op(args->arg, bserrno);
1273 	free_fs_request(req);
1274 }
1275 
1276 void
1277 spdk_fs_delete_file_async(struct spdk_filesystem *fs, const char *name,
1278 			  spdk_file_op_complete cb_fn, void *cb_arg)
1279 {
1280 	struct spdk_file *f;
1281 	spdk_blob_id blobid;
1282 	struct spdk_fs_request *req;
1283 	struct spdk_fs_cb_args *args;
1284 
1285 	SPDK_DEBUGLOG(SPDK_LOG_BLOBFS, "file=%s\n", name);
1286 
1287 	if (strnlen(name, SPDK_FILE_NAME_MAX + 1) == SPDK_FILE_NAME_MAX + 1) {
1288 		cb_fn(cb_arg, -ENAMETOOLONG);
1289 		return;
1290 	}
1291 
1292 	f = fs_find_file(fs, name);
1293 	if (f == NULL) {
1294 		cb_fn(cb_arg, -ENOENT);
1295 		return;
1296 	}
1297 
1298 	req = alloc_fs_request(fs->md_target.md_fs_channel);
1299 	if (req == NULL) {
1300 		cb_fn(cb_arg, -ENOMEM);
1301 		return;
1302 	}
1303 
1304 	args = &req->args;
1305 	args->fn.file_op = cb_fn;
1306 	args->arg = cb_arg;
1307 
1308 	if (f->ref_count > 0) {
1309 		/* If the ref > 0, we mark the file as deleted and delete it when we close it. */
1310 		f->is_deleted = true;
1311 		spdk_blob_set_xattr(f->blob, "is_deleted", &f->is_deleted, sizeof(bool));
1312 		spdk_blob_sync_md(f->blob, blob_delete_cb, args);
1313 		return;
1314 	}
1315 
1316 	TAILQ_REMOVE(&fs->files, f, tailq);
1317 
1318 	cache_free_buffers(f);
1319 
1320 	blobid = f->blobid;
1321 
1322 	free(f->name);
1323 	free(f->tree);
1324 	free(f);
1325 
1326 	spdk_bs_delete_blob(fs->bs, blobid, blob_delete_cb, req);
1327 }
1328 
1329 static void
1330 __fs_delete_file_done(void *arg, int fserrno)
1331 {
1332 	struct spdk_fs_request *req = arg;
1333 	struct spdk_fs_cb_args *args = &req->args;
1334 
1335 	args->rc = fserrno;
1336 	sem_post(args->sem);
1337 }
1338 
1339 static void
1340 __fs_delete_file(void *arg)
1341 {
1342 	struct spdk_fs_request *req = arg;
1343 	struct spdk_fs_cb_args *args = &req->args;
1344 
1345 	spdk_fs_delete_file_async(args->fs, args->op.delete.name, __fs_delete_file_done, req);
1346 }
1347 
1348 int
1349 spdk_fs_delete_file(struct spdk_filesystem *fs, struct spdk_io_channel *_channel,
1350 		    const char *name)
1351 {
1352 	struct spdk_fs_channel *channel = spdk_io_channel_get_ctx(_channel);
1353 	struct spdk_fs_request *req;
1354 	struct spdk_fs_cb_args *args;
1355 	int rc;
1356 
1357 	req = alloc_fs_request(channel);
1358 	assert(req != NULL);
1359 
1360 	args = &req->args;
1361 	args->fs = fs;
1362 	args->op.delete.name = name;
1363 	args->sem = &channel->sem;
1364 	fs->send_request(__fs_delete_file, req);
1365 	sem_wait(&channel->sem);
1366 	rc = args->rc;
1367 	free_fs_request(req);
1368 
1369 	return rc;
1370 }
1371 
1372 spdk_fs_iter
1373 spdk_fs_iter_first(struct spdk_filesystem *fs)
1374 {
1375 	struct spdk_file *f;
1376 
1377 	f = TAILQ_FIRST(&fs->files);
1378 	return f;
1379 }
1380 
1381 spdk_fs_iter
1382 spdk_fs_iter_next(spdk_fs_iter iter)
1383 {
1384 	struct spdk_file *f = iter;
1385 
1386 	if (f == NULL) {
1387 		return NULL;
1388 	}
1389 
1390 	f = TAILQ_NEXT(f, tailq);
1391 	return f;
1392 }
1393 
1394 const char *
1395 spdk_file_get_name(struct spdk_file *file)
1396 {
1397 	return file->name;
1398 }
1399 
1400 uint64_t
1401 spdk_file_get_length(struct spdk_file *file)
1402 {
1403 	assert(file != NULL);
1404 	SPDK_DEBUGLOG(SPDK_LOG_BLOBFS, "file=%s length=0x%jx\n", file->name, file->length);
1405 	return file->length;
1406 }
1407 
1408 static void
1409 fs_truncate_complete_cb(void *ctx, int bserrno)
1410 {
1411 	struct spdk_fs_request *req = ctx;
1412 	struct spdk_fs_cb_args *args = &req->args;
1413 
1414 	args->fn.file_op(args->arg, bserrno);
1415 	free_fs_request(req);
1416 }
1417 
1418 static void
1419 fs_truncate_resize_cb(void *ctx, int bserrno)
1420 {
1421 	struct spdk_fs_request *req = ctx;
1422 	struct spdk_fs_cb_args *args = &req->args;
1423 	struct spdk_file *file = args->file;
1424 	uint64_t *length = &args->op.truncate.length;
1425 
1426 	spdk_blob_set_xattr(file->blob, "length", length, sizeof(*length));
1427 
1428 	file->length = *length;
1429 	if (file->append_pos > file->length) {
1430 		file->append_pos = file->length;
1431 	}
1432 
1433 	spdk_blob_sync_md(file->blob, fs_truncate_complete_cb, args);
1434 }
1435 
1436 static uint64_t
1437 __bytes_to_clusters(uint64_t length, uint64_t cluster_sz)
1438 {
1439 	return (length + cluster_sz - 1) / cluster_sz;
1440 }
1441 
1442 void
1443 spdk_file_truncate_async(struct spdk_file *file, uint64_t length,
1444 			 spdk_file_op_complete cb_fn, void *cb_arg)
1445 {
1446 	struct spdk_filesystem *fs;
1447 	size_t num_clusters;
1448 	struct spdk_fs_request *req;
1449 	struct spdk_fs_cb_args *args;
1450 
1451 	SPDK_DEBUGLOG(SPDK_LOG_BLOBFS, "file=%s old=0x%jx new=0x%jx\n", file->name, file->length, length);
1452 	if (length == file->length) {
1453 		cb_fn(cb_arg, 0);
1454 		return;
1455 	}
1456 
1457 	req = alloc_fs_request(file->fs->md_target.md_fs_channel);
1458 	if (req == NULL) {
1459 		cb_fn(cb_arg, -ENOMEM);
1460 		return;
1461 	}
1462 
1463 	args = &req->args;
1464 	args->fn.file_op = cb_fn;
1465 	args->arg = cb_arg;
1466 	args->file = file;
1467 	args->op.truncate.length = length;
1468 	fs = file->fs;
1469 
1470 	num_clusters = __bytes_to_clusters(length, fs->bs_opts.cluster_sz);
1471 
1472 	spdk_blob_resize(file->blob, num_clusters, fs_truncate_resize_cb, req);
1473 }
1474 
1475 static void
1476 __truncate(void *arg)
1477 {
1478 	struct spdk_fs_request *req = arg;
1479 	struct spdk_fs_cb_args *args = &req->args;
1480 
1481 	spdk_file_truncate_async(args->file, args->op.truncate.length,
1482 				 args->fn.file_op, args->arg);
1483 }
1484 
1485 void
1486 spdk_file_truncate(struct spdk_file *file, struct spdk_io_channel *_channel,
1487 		   uint64_t length)
1488 {
1489 	struct spdk_fs_channel *channel = spdk_io_channel_get_ctx(_channel);
1490 	struct spdk_fs_request *req;
1491 	struct spdk_fs_cb_args *args;
1492 
1493 	req = alloc_fs_request(channel);
1494 	assert(req != NULL);
1495 
1496 	args = &req->args;
1497 
1498 	args->file = file;
1499 	args->op.truncate.length = length;
1500 	args->fn.file_op = __sem_post;
1501 	args->arg = &channel->sem;
1502 
1503 	channel->send_request(__truncate, req);
1504 	sem_wait(&channel->sem);
1505 	free_fs_request(req);
1506 }
1507 
1508 static void
1509 __rw_done(void *ctx, int bserrno)
1510 {
1511 	struct spdk_fs_request *req = ctx;
1512 	struct spdk_fs_cb_args *args = &req->args;
1513 
1514 	spdk_dma_free(args->op.rw.pin_buf);
1515 	args->fn.file_op(args->arg, bserrno);
1516 	free_fs_request(req);
1517 }
1518 
1519 static void
1520 __read_done(void *ctx, int bserrno)
1521 {
1522 	struct spdk_fs_request *req = ctx;
1523 	struct spdk_fs_cb_args *args = &req->args;
1524 
1525 	if (args->op.rw.is_read) {
1526 		memcpy(args->op.rw.user_buf,
1527 		       args->op.rw.pin_buf + (args->op.rw.offset & 0xFFF),
1528 		       args->op.rw.length);
1529 		__rw_done(req, 0);
1530 	} else {
1531 		memcpy(args->op.rw.pin_buf + (args->op.rw.offset & 0xFFF),
1532 		       args->op.rw.user_buf,
1533 		       args->op.rw.length);
1534 		spdk_blob_io_write(args->file->blob, args->op.rw.channel,
1535 				   args->op.rw.pin_buf,
1536 				   args->op.rw.start_page, args->op.rw.num_pages,
1537 				   __rw_done, req);
1538 	}
1539 }
1540 
1541 static void
1542 __do_blob_read(void *ctx, int fserrno)
1543 {
1544 	struct spdk_fs_request *req = ctx;
1545 	struct spdk_fs_cb_args *args = &req->args;
1546 
1547 	spdk_blob_io_read(args->file->blob, args->op.rw.channel,
1548 			  args->op.rw.pin_buf,
1549 			  args->op.rw.start_page, args->op.rw.num_pages,
1550 			  __read_done, req);
1551 }
1552 
1553 static void
1554 __get_page_parameters(struct spdk_file *file, uint64_t offset, uint64_t length,
1555 		      uint64_t *start_page, uint32_t *page_size, uint64_t *num_pages)
1556 {
1557 	uint64_t end_page;
1558 
1559 	*page_size = spdk_bs_get_page_size(file->fs->bs);
1560 	*start_page = offset / *page_size;
1561 	end_page = (offset + length - 1) / *page_size;
1562 	*num_pages = (end_page - *start_page + 1);
1563 }
1564 
1565 static void
1566 __readwrite(struct spdk_file *file, struct spdk_io_channel *_channel,
1567 	    void *payload, uint64_t offset, uint64_t length,
1568 	    spdk_file_op_complete cb_fn, void *cb_arg, int is_read)
1569 {
1570 	struct spdk_fs_request *req;
1571 	struct spdk_fs_cb_args *args;
1572 	struct spdk_fs_channel *channel = spdk_io_channel_get_ctx(_channel);
1573 	uint64_t start_page, num_pages, pin_buf_length;
1574 	uint32_t page_size;
1575 
1576 	if (is_read && offset + length > file->length) {
1577 		cb_fn(cb_arg, -EINVAL);
1578 		return;
1579 	}
1580 
1581 	req = alloc_fs_request(channel);
1582 	if (req == NULL) {
1583 		cb_fn(cb_arg, -ENOMEM);
1584 		return;
1585 	}
1586 
1587 	args = &req->args;
1588 	args->fn.file_op = cb_fn;
1589 	args->arg = cb_arg;
1590 	args->file = file;
1591 	args->op.rw.channel = channel->bs_channel;
1592 	args->op.rw.user_buf = payload;
1593 	args->op.rw.is_read = is_read;
1594 	args->op.rw.offset = offset;
1595 	args->op.rw.length = length;
1596 
1597 	__get_page_parameters(file, offset, length, &start_page, &page_size, &num_pages);
1598 	pin_buf_length = num_pages * page_size;
1599 	args->op.rw.pin_buf = spdk_dma_malloc(pin_buf_length, 4096, NULL);
1600 
1601 	args->op.rw.start_page = start_page;
1602 	args->op.rw.num_pages = num_pages;
1603 
1604 	if (!is_read && file->length < offset + length) {
1605 		spdk_file_truncate_async(file, offset + length, __do_blob_read, req);
1606 	} else {
1607 		__do_blob_read(req, 0);
1608 	}
1609 }
1610 
1611 void
1612 spdk_file_write_async(struct spdk_file *file, struct spdk_io_channel *channel,
1613 		      void *payload, uint64_t offset, uint64_t length,
1614 		      spdk_file_op_complete cb_fn, void *cb_arg)
1615 {
1616 	__readwrite(file, channel, payload, offset, length, cb_fn, cb_arg, 0);
1617 }
1618 
1619 void
1620 spdk_file_read_async(struct spdk_file *file, struct spdk_io_channel *channel,
1621 		     void *payload, uint64_t offset, uint64_t length,
1622 		     spdk_file_op_complete cb_fn, void *cb_arg)
1623 {
1624 	SPDK_DEBUGLOG(SPDK_LOG_BLOBFS, "file=%s offset=%jx length=%jx\n",
1625 		      file->name, offset, length);
1626 	__readwrite(file, channel, payload, offset, length, cb_fn, cb_arg, 1);
1627 }
1628 
1629 struct spdk_io_channel *
1630 spdk_fs_alloc_io_channel(struct spdk_filesystem *fs)
1631 {
1632 	struct spdk_io_channel *io_channel;
1633 	struct spdk_fs_channel *fs_channel;
1634 
1635 	io_channel = spdk_get_io_channel(&fs->io_target);
1636 	fs_channel = spdk_io_channel_get_ctx(io_channel);
1637 	fs_channel->bs_channel = spdk_bs_alloc_io_channel(fs->bs);
1638 	fs_channel->send_request = __send_request_direct;
1639 
1640 	return io_channel;
1641 }
1642 
1643 struct spdk_io_channel *
1644 spdk_fs_alloc_io_channel_sync(struct spdk_filesystem *fs)
1645 {
1646 	struct spdk_io_channel *io_channel;
1647 	struct spdk_fs_channel *fs_channel;
1648 
1649 	io_channel = spdk_get_io_channel(&fs->io_target);
1650 	fs_channel = spdk_io_channel_get_ctx(io_channel);
1651 	fs_channel->send_request = fs->send_request;
1652 	fs_channel->sync = 1;
1653 	pthread_spin_init(&fs_channel->lock, 0);
1654 
1655 	return io_channel;
1656 }
1657 
1658 void
1659 spdk_fs_free_io_channel(struct spdk_io_channel *channel)
1660 {
1661 	spdk_put_io_channel(channel);
1662 }
1663 
1664 void
1665 spdk_fs_set_cache_size(uint64_t size_in_mb)
1666 {
1667 	g_fs_cache_size = size_in_mb * 1024 * 1024;
1668 }
1669 
1670 uint64_t
1671 spdk_fs_get_cache_size(void)
1672 {
1673 	return g_fs_cache_size / (1024 * 1024);
1674 }
1675 
1676 static void __file_flush(void *_args);
1677 
1678 static void *
1679 alloc_cache_memory_buffer(struct spdk_file *context)
1680 {
1681 	struct spdk_file *file;
1682 	void *buf;
1683 
1684 	buf = spdk_mempool_get(g_cache_pool);
1685 	if (buf != NULL) {
1686 		return buf;
1687 	}
1688 
1689 	pthread_spin_lock(&g_caches_lock);
1690 	TAILQ_FOREACH(file, &g_caches, cache_tailq) {
1691 		if (!file->open_for_writing &&
1692 		    file->priority == SPDK_FILE_PRIORITY_LOW &&
1693 		    file != context) {
1694 			break;
1695 		}
1696 	}
1697 	pthread_spin_unlock(&g_caches_lock);
1698 	if (file != NULL) {
1699 		cache_free_buffers(file);
1700 		buf = spdk_mempool_get(g_cache_pool);
1701 		if (buf != NULL) {
1702 			return buf;
1703 		}
1704 	}
1705 
1706 	pthread_spin_lock(&g_caches_lock);
1707 	TAILQ_FOREACH(file, &g_caches, cache_tailq) {
1708 		if (!file->open_for_writing && file != context) {
1709 			break;
1710 		}
1711 	}
1712 	pthread_spin_unlock(&g_caches_lock);
1713 	if (file != NULL) {
1714 		cache_free_buffers(file);
1715 		buf = spdk_mempool_get(g_cache_pool);
1716 		if (buf != NULL) {
1717 			return buf;
1718 		}
1719 	}
1720 
1721 	pthread_spin_lock(&g_caches_lock);
1722 	TAILQ_FOREACH(file, &g_caches, cache_tailq) {
1723 		if (file != context) {
1724 			break;
1725 		}
1726 	}
1727 	pthread_spin_unlock(&g_caches_lock);
1728 	if (file != NULL) {
1729 		cache_free_buffers(file);
1730 		buf = spdk_mempool_get(g_cache_pool);
1731 		if (buf != NULL) {
1732 			return buf;
1733 		}
1734 	}
1735 
1736 	return NULL;
1737 }
1738 
1739 static struct cache_buffer *
1740 cache_insert_buffer(struct spdk_file *file, uint64_t offset)
1741 {
1742 	struct cache_buffer *buf;
1743 	int count = 0;
1744 
1745 	buf = calloc(1, sizeof(*buf));
1746 	if (buf == NULL) {
1747 		SPDK_DEBUGLOG(SPDK_LOG_BLOBFS, "calloc failed\n");
1748 		return NULL;
1749 	}
1750 
1751 	buf->buf = alloc_cache_memory_buffer(file);
1752 	while (buf->buf == NULL) {
1753 		/*
1754 		 * TODO: alloc_cache_memory_buffer() should eventually free
1755 		 *  some buffers.  Need a more sophisticated check here, instead
1756 		 *  of just bailing if 100 tries does not result in getting a
1757 		 *  free buffer.  This will involve using the sync channel's
1758 		 *  semaphore to block until a buffer becomes available.
1759 		 */
1760 		if (count++ == 100) {
1761 			SPDK_ERRLOG("could not allocate cache buffer\n");
1762 			assert(false);
1763 			free(buf);
1764 			return NULL;
1765 		}
1766 		buf->buf = alloc_cache_memory_buffer(file);
1767 	}
1768 
1769 	buf->buf_size = CACHE_BUFFER_SIZE;
1770 	buf->offset = offset;
1771 
1772 	pthread_spin_lock(&g_caches_lock);
1773 	if (file->tree->present_mask == 0) {
1774 		TAILQ_INSERT_TAIL(&g_caches, file, cache_tailq);
1775 	}
1776 	file->tree = spdk_tree_insert_buffer(file->tree, buf);
1777 	pthread_spin_unlock(&g_caches_lock);
1778 
1779 	return buf;
1780 }
1781 
1782 static struct cache_buffer *
1783 cache_append_buffer(struct spdk_file *file)
1784 {
1785 	struct cache_buffer *last;
1786 
1787 	assert(file->last == NULL || file->last->bytes_filled == file->last->buf_size);
1788 	assert((file->append_pos % CACHE_BUFFER_SIZE) == 0);
1789 
1790 	last = cache_insert_buffer(file, file->append_pos);
1791 	if (last == NULL) {
1792 		SPDK_DEBUGLOG(SPDK_LOG_BLOBFS, "cache_insert_buffer failed\n");
1793 		return NULL;
1794 	}
1795 
1796 	file->last = last;
1797 
1798 	return last;
1799 }
1800 
1801 static void
1802 __wake_caller(struct spdk_fs_cb_args *args)
1803 {
1804 	sem_post(args->sem);
1805 }
1806 
1807 static void __check_sync_reqs(struct spdk_file *file);
1808 
1809 static void
1810 __file_cache_finish_sync(struct spdk_file *file)
1811 {
1812 	struct spdk_fs_request *sync_req;
1813 	struct spdk_fs_cb_args *sync_args;
1814 
1815 	pthread_spin_lock(&file->lock);
1816 	sync_req = TAILQ_FIRST(&file->sync_requests);
1817 	sync_args = &sync_req->args;
1818 	assert(sync_args->op.sync.offset <= file->length_flushed);
1819 	BLOBFS_TRACE(file, "sync done offset=%jx\n", sync_args->op.sync.offset);
1820 	TAILQ_REMOVE(&file->sync_requests, sync_req, args.op.sync.tailq);
1821 	pthread_spin_unlock(&file->lock);
1822 
1823 	sync_args->fn.file_op(sync_args->arg, 0);
1824 	__check_sync_reqs(file);
1825 
1826 	pthread_spin_lock(&file->lock);
1827 	free_fs_request(sync_req);
1828 	pthread_spin_unlock(&file->lock);
1829 }
1830 
1831 static void
1832 __file_cache_finish_sync_bs_cb(void *ctx, int bserrno)
1833 {
1834 	struct spdk_file *file = ctx;
1835 
1836 	__file_cache_finish_sync(file);
1837 }
1838 
1839 static void
1840 __free_args(struct spdk_fs_cb_args *args)
1841 {
1842 	struct spdk_fs_request *req;
1843 
1844 	if (!args->from_request) {
1845 		free(args);
1846 	} else {
1847 		/* Depends on args being at the start of the spdk_fs_request structure. */
1848 		req = (struct spdk_fs_request *)args;
1849 		free_fs_request(req);
1850 	}
1851 }
1852 
1853 static void
1854 __check_sync_reqs(struct spdk_file *file)
1855 {
1856 	struct spdk_fs_request *sync_req;
1857 
1858 	pthread_spin_lock(&file->lock);
1859 
1860 	TAILQ_FOREACH(sync_req, &file->sync_requests, args.op.sync.tailq) {
1861 		if (sync_req->args.op.sync.offset <= file->length_flushed) {
1862 			break;
1863 		}
1864 	}
1865 
1866 	if (sync_req != NULL && !sync_req->args.op.sync.xattr_in_progress) {
1867 		BLOBFS_TRACE(file, "set xattr length 0x%jx\n", file->length_flushed);
1868 		sync_req->args.op.sync.xattr_in_progress = true;
1869 		spdk_blob_set_xattr(file->blob, "length", &file->length_flushed,
1870 				    sizeof(file->length_flushed));
1871 
1872 		pthread_spin_unlock(&file->lock);
1873 		spdk_blob_sync_md(file->blob, __file_cache_finish_sync_bs_cb, file);
1874 	} else {
1875 		pthread_spin_unlock(&file->lock);
1876 	}
1877 }
1878 
1879 static void
1880 __file_flush_done(void *arg, int bserrno)
1881 {
1882 	struct spdk_fs_cb_args *args = arg;
1883 	struct spdk_file *file = args->file;
1884 	struct cache_buffer *next = args->op.flush.cache_buffer;
1885 
1886 	BLOBFS_TRACE(file, "length=%jx\n", args->op.flush.length);
1887 
1888 	pthread_spin_lock(&file->lock);
1889 	next->in_progress = false;
1890 	next->bytes_flushed += args->op.flush.length;
1891 	file->length_flushed += args->op.flush.length;
1892 	if (file->length_flushed > file->length) {
1893 		file->length = file->length_flushed;
1894 	}
1895 	if (next->bytes_flushed == next->buf_size) {
1896 		BLOBFS_TRACE(file, "write buffer fully flushed 0x%jx\n", file->length_flushed);
1897 		next = spdk_tree_find_buffer(file->tree, file->length_flushed);
1898 	}
1899 
1900 	/*
1901 	 * Assert that there is no cached data that extends past the end of the underlying
1902 	 *  blob.
1903 	 */
1904 	assert(next == NULL || next->offset < __file_get_blob_size(file) ||
1905 	       next->bytes_filled == 0);
1906 
1907 	pthread_spin_unlock(&file->lock);
1908 
1909 	__check_sync_reqs(file);
1910 
1911 	__file_flush(args);
1912 }
1913 
1914 static void
1915 __file_flush(void *_args)
1916 {
1917 	struct spdk_fs_cb_args *args = _args;
1918 	struct spdk_file *file = args->file;
1919 	struct cache_buffer *next;
1920 	uint64_t offset, length, start_page, num_pages;
1921 	uint32_t page_size;
1922 
1923 	pthread_spin_lock(&file->lock);
1924 	next = spdk_tree_find_buffer(file->tree, file->length_flushed);
1925 	if (next == NULL || next->in_progress) {
1926 		/*
1927 		 * There is either no data to flush, or a flush I/O is already in
1928 		 *  progress.  So return immediately - if a flush I/O is in
1929 		 *  progress we will flush more data after that is completed.
1930 		 */
1931 		__free_args(args);
1932 		if (next == NULL) {
1933 			/*
1934 			 * For cases where a file's cache was evicted, and then the
1935 			 *  file was later appended, we will write the data directly
1936 			 *  to disk and bypass cache.  So just update length_flushed
1937 			 *  here to reflect that all data was already written to disk.
1938 			 */
1939 			file->length_flushed = file->append_pos;
1940 		}
1941 		pthread_spin_unlock(&file->lock);
1942 		if (next == NULL) {
1943 			/*
1944 			 * There is no data to flush, but we still need to check for any
1945 			 *  outstanding sync requests to make sure metadata gets updated.
1946 			 */
1947 			__check_sync_reqs(file);
1948 		}
1949 		return;
1950 	}
1951 
1952 	offset = next->offset + next->bytes_flushed;
1953 	length = next->bytes_filled - next->bytes_flushed;
1954 	if (length == 0) {
1955 		__free_args(args);
1956 		pthread_spin_unlock(&file->lock);
1957 		return;
1958 	}
1959 	args->op.flush.length = length;
1960 	args->op.flush.cache_buffer = next;
1961 
1962 	__get_page_parameters(file, offset, length, &start_page, &page_size, &num_pages);
1963 
1964 	next->in_progress = true;
1965 	BLOBFS_TRACE(file, "offset=%jx length=%jx page start=%jx num=%jx\n",
1966 		     offset, length, start_page, num_pages);
1967 	pthread_spin_unlock(&file->lock);
1968 	spdk_blob_io_write(file->blob, file->fs->sync_target.sync_fs_channel->bs_channel,
1969 			   next->buf + (start_page * page_size) - next->offset,
1970 			   start_page, num_pages, __file_flush_done, args);
1971 }
1972 
1973 static void
1974 __file_extend_done(void *arg, int bserrno)
1975 {
1976 	struct spdk_fs_cb_args *args = arg;
1977 
1978 	__wake_caller(args);
1979 }
1980 
1981 static void
1982 __file_extend_resize_cb(void *_args, int bserrno)
1983 {
1984 	struct spdk_fs_cb_args *args = _args;
1985 	struct spdk_file *file = args->file;
1986 
1987 	spdk_blob_sync_md(file->blob, __file_extend_done, args);
1988 }
1989 
1990 static void
1991 __file_extend_blob(void *_args)
1992 {
1993 	struct spdk_fs_cb_args *args = _args;
1994 	struct spdk_file *file = args->file;
1995 
1996 	spdk_blob_resize(file->blob, args->op.resize.num_clusters, __file_extend_resize_cb, args);
1997 }
1998 
1999 static void
2000 __rw_from_file_done(void *arg, int bserrno)
2001 {
2002 	struct spdk_fs_cb_args *args = arg;
2003 
2004 	__wake_caller(args);
2005 	__free_args(args);
2006 }
2007 
2008 static void
2009 __rw_from_file(void *_args)
2010 {
2011 	struct spdk_fs_cb_args *args = _args;
2012 	struct spdk_file *file = args->file;
2013 
2014 	if (args->op.rw.is_read) {
2015 		spdk_file_read_async(file, file->fs->sync_target.sync_io_channel, args->op.rw.user_buf,
2016 				     args->op.rw.offset, args->op.rw.length,
2017 				     __rw_from_file_done, args);
2018 	} else {
2019 		spdk_file_write_async(file, file->fs->sync_target.sync_io_channel, args->op.rw.user_buf,
2020 				      args->op.rw.offset, args->op.rw.length,
2021 				      __rw_from_file_done, args);
2022 	}
2023 }
2024 
2025 static int
2026 __send_rw_from_file(struct spdk_file *file, sem_t *sem, void *payload,
2027 		    uint64_t offset, uint64_t length, bool is_read)
2028 {
2029 	struct spdk_fs_cb_args *args;
2030 
2031 	args = calloc(1, sizeof(*args));
2032 	if (args == NULL) {
2033 		sem_post(sem);
2034 		return -ENOMEM;
2035 	}
2036 
2037 	args->file = file;
2038 	args->sem = sem;
2039 	args->op.rw.user_buf = payload;
2040 	args->op.rw.offset = offset;
2041 	args->op.rw.length = length;
2042 	args->op.rw.is_read = is_read;
2043 	file->fs->send_request(__rw_from_file, args);
2044 	return 0;
2045 }
2046 
2047 int
2048 spdk_file_write(struct spdk_file *file, struct spdk_io_channel *_channel,
2049 		void *payload, uint64_t offset, uint64_t length)
2050 {
2051 	struct spdk_fs_channel *channel = spdk_io_channel_get_ctx(_channel);
2052 	struct spdk_fs_cb_args *args;
2053 	uint64_t rem_length, copy, blob_size, cluster_sz;
2054 	uint32_t cache_buffers_filled = 0;
2055 	uint8_t *cur_payload;
2056 	struct cache_buffer *last;
2057 
2058 	BLOBFS_TRACE_RW(file, "offset=%jx length=%jx\n", offset, length);
2059 
2060 	if (length == 0) {
2061 		return 0;
2062 	}
2063 
2064 	if (offset != file->append_pos) {
2065 		BLOBFS_TRACE(file, " error offset=%jx append_pos=%jx\n", offset, file->append_pos);
2066 		return -EINVAL;
2067 	}
2068 
2069 	pthread_spin_lock(&file->lock);
2070 	file->open_for_writing = true;
2071 
2072 	if (file->last == NULL) {
2073 		if (file->append_pos % CACHE_BUFFER_SIZE == 0) {
2074 			cache_append_buffer(file);
2075 		} else {
2076 			int rc;
2077 
2078 			file->append_pos += length;
2079 			pthread_spin_unlock(&file->lock);
2080 			rc = __send_rw_from_file(file, &channel->sem, payload,
2081 						 offset, length, false);
2082 			sem_wait(&channel->sem);
2083 			return rc;
2084 		}
2085 	}
2086 
2087 	blob_size = __file_get_blob_size(file);
2088 
2089 	if ((offset + length) > blob_size) {
2090 		struct spdk_fs_cb_args extend_args = {};
2091 
2092 		cluster_sz = file->fs->bs_opts.cluster_sz;
2093 		extend_args.sem = &channel->sem;
2094 		extend_args.op.resize.num_clusters = __bytes_to_clusters((offset + length), cluster_sz);
2095 		extend_args.file = file;
2096 		BLOBFS_TRACE(file, "start resize to %u clusters\n", extend_args.op.resize.num_clusters);
2097 		pthread_spin_unlock(&file->lock);
2098 		file->fs->send_request(__file_extend_blob, &extend_args);
2099 		sem_wait(&channel->sem);
2100 	}
2101 
2102 	last = file->last;
2103 	rem_length = length;
2104 	cur_payload = payload;
2105 	while (rem_length > 0) {
2106 		copy = last->buf_size - last->bytes_filled;
2107 		if (copy > rem_length) {
2108 			copy = rem_length;
2109 		}
2110 		BLOBFS_TRACE_RW(file, "  fill offset=%jx length=%jx\n", file->append_pos, copy);
2111 		memcpy(&last->buf[last->bytes_filled], cur_payload, copy);
2112 		file->append_pos += copy;
2113 		if (file->length < file->append_pos) {
2114 			file->length = file->append_pos;
2115 		}
2116 		cur_payload += copy;
2117 		last->bytes_filled += copy;
2118 		rem_length -= copy;
2119 		if (last->bytes_filled == last->buf_size) {
2120 			cache_buffers_filled++;
2121 			last = cache_append_buffer(file);
2122 			if (last == NULL) {
2123 				BLOBFS_TRACE(file, "nomem\n");
2124 				pthread_spin_unlock(&file->lock);
2125 				return -ENOMEM;
2126 			}
2127 		}
2128 	}
2129 
2130 	pthread_spin_unlock(&file->lock);
2131 
2132 	if (cache_buffers_filled == 0) {
2133 		return 0;
2134 	}
2135 
2136 	args = calloc(1, sizeof(*args));
2137 	if (args == NULL) {
2138 		return -ENOMEM;
2139 	}
2140 
2141 	args->file = file;
2142 	file->fs->send_request(__file_flush, args);
2143 	return 0;
2144 }
2145 
2146 static void
2147 __readahead_done(void *arg, int bserrno)
2148 {
2149 	struct spdk_fs_cb_args *args = arg;
2150 	struct cache_buffer *cache_buffer = args->op.readahead.cache_buffer;
2151 	struct spdk_file *file = args->file;
2152 
2153 	BLOBFS_TRACE(file, "offset=%jx\n", cache_buffer->offset);
2154 
2155 	pthread_spin_lock(&file->lock);
2156 	cache_buffer->bytes_filled = args->op.readahead.length;
2157 	cache_buffer->bytes_flushed = args->op.readahead.length;
2158 	cache_buffer->in_progress = false;
2159 	pthread_spin_unlock(&file->lock);
2160 
2161 	__free_args(args);
2162 }
2163 
2164 static void
2165 __readahead(void *_args)
2166 {
2167 	struct spdk_fs_cb_args *args = _args;
2168 	struct spdk_file *file = args->file;
2169 	uint64_t offset, length, start_page, num_pages;
2170 	uint32_t page_size;
2171 
2172 	offset = args->op.readahead.offset;
2173 	length = args->op.readahead.length;
2174 	assert(length > 0);
2175 
2176 	__get_page_parameters(file, offset, length, &start_page, &page_size, &num_pages);
2177 
2178 	BLOBFS_TRACE(file, "offset=%jx length=%jx page start=%jx num=%jx\n",
2179 		     offset, length, start_page, num_pages);
2180 	spdk_blob_io_read(file->blob, file->fs->sync_target.sync_fs_channel->bs_channel,
2181 			  args->op.readahead.cache_buffer->buf,
2182 			  start_page, num_pages, __readahead_done, args);
2183 }
2184 
2185 static uint64_t
2186 __next_cache_buffer_offset(uint64_t offset)
2187 {
2188 	return (offset + CACHE_BUFFER_SIZE) & ~(CACHE_TREE_LEVEL_MASK(0));
2189 }
2190 
2191 static void
2192 check_readahead(struct spdk_file *file, uint64_t offset)
2193 {
2194 	struct spdk_fs_cb_args *args;
2195 
2196 	offset = __next_cache_buffer_offset(offset);
2197 	if (spdk_tree_find_buffer(file->tree, offset) != NULL || file->length <= offset) {
2198 		return;
2199 	}
2200 
2201 	args = calloc(1, sizeof(*args));
2202 	if (args == NULL) {
2203 		return;
2204 	}
2205 
2206 	BLOBFS_TRACE(file, "offset=%jx\n", offset);
2207 
2208 	args->file = file;
2209 	args->op.readahead.offset = offset;
2210 	args->op.readahead.cache_buffer = cache_insert_buffer(file, offset);
2211 	args->op.readahead.cache_buffer->in_progress = true;
2212 	if (file->length < (offset + CACHE_BUFFER_SIZE)) {
2213 		args->op.readahead.length = file->length & (CACHE_BUFFER_SIZE - 1);
2214 	} else {
2215 		args->op.readahead.length = CACHE_BUFFER_SIZE;
2216 	}
2217 	file->fs->send_request(__readahead, args);
2218 }
2219 
2220 static int
2221 __file_read(struct spdk_file *file, void *payload, uint64_t offset, uint64_t length, sem_t *sem)
2222 {
2223 	struct cache_buffer *buf;
2224 	int rc;
2225 
2226 	buf = spdk_tree_find_filled_buffer(file->tree, offset);
2227 	if (buf == NULL) {
2228 		pthread_spin_unlock(&file->lock);
2229 		rc = __send_rw_from_file(file, sem, payload, offset, length, true);
2230 		pthread_spin_lock(&file->lock);
2231 		return rc;
2232 	}
2233 
2234 	if ((offset + length) > (buf->offset + buf->bytes_filled)) {
2235 		length = buf->offset + buf->bytes_filled - offset;
2236 	}
2237 	BLOBFS_TRACE(file, "read %p offset=%ju length=%ju\n", payload, offset, length);
2238 	memcpy(payload, &buf->buf[offset - buf->offset], length);
2239 	if ((offset + length) % CACHE_BUFFER_SIZE == 0) {
2240 		pthread_spin_lock(&g_caches_lock);
2241 		spdk_tree_remove_buffer(file->tree, buf);
2242 		if (file->tree->present_mask == 0) {
2243 			TAILQ_REMOVE(&g_caches, file, cache_tailq);
2244 		}
2245 		pthread_spin_unlock(&g_caches_lock);
2246 	}
2247 
2248 	sem_post(sem);
2249 	return 0;
2250 }
2251 
2252 int64_t
2253 spdk_file_read(struct spdk_file *file, struct spdk_io_channel *_channel,
2254 	       void *payload, uint64_t offset, uint64_t length)
2255 {
2256 	struct spdk_fs_channel *channel = spdk_io_channel_get_ctx(_channel);
2257 	uint64_t final_offset, final_length;
2258 	uint32_t sub_reads = 0;
2259 	int rc = 0;
2260 
2261 	pthread_spin_lock(&file->lock);
2262 
2263 	BLOBFS_TRACE_RW(file, "offset=%ju length=%ju\n", offset, length);
2264 
2265 	file->open_for_writing = false;
2266 
2267 	if (length == 0 || offset >= file->append_pos) {
2268 		pthread_spin_unlock(&file->lock);
2269 		return 0;
2270 	}
2271 
2272 	if (offset + length > file->append_pos) {
2273 		length = file->append_pos - offset;
2274 	}
2275 
2276 	if (offset != file->next_seq_offset) {
2277 		file->seq_byte_count = 0;
2278 	}
2279 	file->seq_byte_count += length;
2280 	file->next_seq_offset = offset + length;
2281 	if (file->seq_byte_count >= CACHE_READAHEAD_THRESHOLD) {
2282 		check_readahead(file, offset);
2283 		check_readahead(file, offset + CACHE_BUFFER_SIZE);
2284 	}
2285 
2286 	final_length = 0;
2287 	final_offset = offset + length;
2288 	while (offset < final_offset) {
2289 		length = NEXT_CACHE_BUFFER_OFFSET(offset) - offset;
2290 		if (length > (final_offset - offset)) {
2291 			length = final_offset - offset;
2292 		}
2293 		rc = __file_read(file, payload, offset, length, &channel->sem);
2294 		if (rc == 0) {
2295 			final_length += length;
2296 		} else {
2297 			break;
2298 		}
2299 		payload += length;
2300 		offset += length;
2301 		sub_reads++;
2302 	}
2303 	pthread_spin_unlock(&file->lock);
2304 	while (sub_reads-- > 0) {
2305 		sem_wait(&channel->sem);
2306 	}
2307 	if (rc == 0) {
2308 		return final_length;
2309 	} else {
2310 		return rc;
2311 	}
2312 }
2313 
2314 static void
2315 _file_sync(struct spdk_file *file, struct spdk_fs_channel *channel,
2316 	   spdk_file_op_complete cb_fn, void *cb_arg)
2317 {
2318 	struct spdk_fs_request *sync_req;
2319 	struct spdk_fs_request *flush_req;
2320 	struct spdk_fs_cb_args *sync_args;
2321 	struct spdk_fs_cb_args *flush_args;
2322 
2323 	BLOBFS_TRACE(file, "offset=%jx\n", file->append_pos);
2324 
2325 	pthread_spin_lock(&file->lock);
2326 	if (file->append_pos <= file->length_flushed) {
2327 		BLOBFS_TRACE(file, "done - no data to flush\n");
2328 		pthread_spin_unlock(&file->lock);
2329 		cb_fn(cb_arg, 0);
2330 		return;
2331 	}
2332 
2333 	sync_req = alloc_fs_request(channel);
2334 	assert(sync_req != NULL);
2335 	sync_args = &sync_req->args;
2336 
2337 	flush_req = alloc_fs_request(channel);
2338 	assert(flush_req != NULL);
2339 	flush_args = &flush_req->args;
2340 
2341 	sync_args->file = file;
2342 	sync_args->fn.file_op = cb_fn;
2343 	sync_args->arg = cb_arg;
2344 	sync_args->op.sync.offset = file->append_pos;
2345 	sync_args->op.sync.xattr_in_progress = false;
2346 	TAILQ_INSERT_TAIL(&file->sync_requests, sync_req, args.op.sync.tailq);
2347 	pthread_spin_unlock(&file->lock);
2348 
2349 	flush_args->file = file;
2350 	channel->send_request(__file_flush, flush_args);
2351 }
2352 
2353 int
2354 spdk_file_sync(struct spdk_file *file, struct spdk_io_channel *_channel)
2355 {
2356 	struct spdk_fs_channel *channel = spdk_io_channel_get_ctx(_channel);
2357 
2358 	_file_sync(file, channel, __sem_post, &channel->sem);
2359 	sem_wait(&channel->sem);
2360 
2361 	return 0;
2362 }
2363 
2364 void
2365 spdk_file_sync_async(struct spdk_file *file, struct spdk_io_channel *_channel,
2366 		     spdk_file_op_complete cb_fn, void *cb_arg)
2367 {
2368 	struct spdk_fs_channel *channel = spdk_io_channel_get_ctx(_channel);
2369 
2370 	_file_sync(file, channel, cb_fn, cb_arg);
2371 }
2372 
2373 void
2374 spdk_file_set_priority(struct spdk_file *file, uint32_t priority)
2375 {
2376 	BLOBFS_TRACE(file, "priority=%u\n", priority);
2377 	file->priority = priority;
2378 
2379 }
2380 
2381 /*
2382  * Close routines
2383  */
2384 
2385 static void
2386 __file_close_async_done(void *ctx, int bserrno)
2387 {
2388 	struct spdk_fs_request *req = ctx;
2389 	struct spdk_fs_cb_args *args = &req->args;
2390 	struct spdk_file *file = args->file;
2391 
2392 	if (file->is_deleted) {
2393 		spdk_fs_delete_file_async(file->fs, file->name, blob_delete_cb, ctx);
2394 		return;
2395 	}
2396 
2397 	args->fn.file_op(args->arg, bserrno);
2398 	free_fs_request(req);
2399 }
2400 
2401 static void
2402 __file_close_async(struct spdk_file *file, struct spdk_fs_request *req)
2403 {
2404 	struct spdk_blob *blob;
2405 
2406 	pthread_spin_lock(&file->lock);
2407 	if (file->ref_count == 0) {
2408 		pthread_spin_unlock(&file->lock);
2409 		__file_close_async_done(req, -EBADF);
2410 		return;
2411 	}
2412 
2413 	file->ref_count--;
2414 	if (file->ref_count > 0) {
2415 		pthread_spin_unlock(&file->lock);
2416 		req->args.fn.file_op(req->args.arg, 0);
2417 		free_fs_request(req);
2418 		return;
2419 	}
2420 
2421 	pthread_spin_unlock(&file->lock);
2422 
2423 	blob = file->blob;
2424 	file->blob = NULL;
2425 	spdk_blob_close(blob, __file_close_async_done, req);
2426 }
2427 
2428 static void
2429 __file_close_async__sync_done(void *arg, int fserrno)
2430 {
2431 	struct spdk_fs_request *req = arg;
2432 	struct spdk_fs_cb_args *args = &req->args;
2433 
2434 	__file_close_async(args->file, req);
2435 }
2436 
2437 void
2438 spdk_file_close_async(struct spdk_file *file, spdk_file_op_complete cb_fn, void *cb_arg)
2439 {
2440 	struct spdk_fs_request *req;
2441 	struct spdk_fs_cb_args *args;
2442 
2443 	req = alloc_fs_request(file->fs->md_target.md_fs_channel);
2444 	if (req == NULL) {
2445 		cb_fn(cb_arg, -ENOMEM);
2446 		return;
2447 	}
2448 
2449 	args = &req->args;
2450 	args->file = file;
2451 	args->fn.file_op = cb_fn;
2452 	args->arg = cb_arg;
2453 
2454 	spdk_file_sync_async(file, file->fs->md_target.md_io_channel, __file_close_async__sync_done, req);
2455 }
2456 
2457 static void
2458 __file_close_done(void *arg, int fserrno)
2459 {
2460 	struct spdk_fs_cb_args *args = arg;
2461 
2462 	args->rc = fserrno;
2463 	sem_post(args->sem);
2464 }
2465 
2466 static void
2467 __file_close(void *arg)
2468 {
2469 	struct spdk_fs_request *req = arg;
2470 	struct spdk_fs_cb_args *args = &req->args;
2471 	struct spdk_file *file = args->file;
2472 
2473 	__file_close_async(file, req);
2474 }
2475 
2476 int
2477 spdk_file_close(struct spdk_file *file, struct spdk_io_channel *_channel)
2478 {
2479 	struct spdk_fs_channel *channel = spdk_io_channel_get_ctx(_channel);
2480 	struct spdk_fs_request *req;
2481 	struct spdk_fs_cb_args *args;
2482 
2483 	req = alloc_fs_request(channel);
2484 	assert(req != NULL);
2485 
2486 	args = &req->args;
2487 
2488 	spdk_file_sync(file, _channel);
2489 	BLOBFS_TRACE(file, "name=%s\n", file->name);
2490 	args->file = file;
2491 	args->sem = &channel->sem;
2492 	args->fn.file_op = __file_close_done;
2493 	args->arg = req;
2494 	channel->send_request(__file_close, req);
2495 	sem_wait(&channel->sem);
2496 
2497 	return args->rc;
2498 }
2499 
2500 static void
2501 cache_free_buffers(struct spdk_file *file)
2502 {
2503 	BLOBFS_TRACE(file, "free=%s\n", file->name);
2504 	pthread_spin_lock(&file->lock);
2505 	pthread_spin_lock(&g_caches_lock);
2506 	if (file->tree->present_mask == 0) {
2507 		pthread_spin_unlock(&g_caches_lock);
2508 		pthread_spin_unlock(&file->lock);
2509 		return;
2510 	}
2511 	spdk_tree_free_buffers(file->tree);
2512 
2513 	TAILQ_REMOVE(&g_caches, file, cache_tailq);
2514 	/* If not freed, put it in the end of the queue */
2515 	if (file->tree->present_mask != 0) {
2516 		TAILQ_INSERT_TAIL(&g_caches, file, cache_tailq);
2517 	}
2518 	file->last = NULL;
2519 	pthread_spin_unlock(&g_caches_lock);
2520 	pthread_spin_unlock(&file->lock);
2521 }
2522 
2523 SPDK_LOG_REGISTER_COMPONENT("blobfs", SPDK_LOG_BLOBFS)
2524 SPDK_LOG_REGISTER_COMPONENT("blobfs_rw", SPDK_LOG_BLOBFS_RW)
2525