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