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