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