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 #include "spdk/blob.h" 36 #include "spdk/log.h" 37 #include "blobstore.h" 38 39 static void 40 blob_bs_dev_write(struct spdk_bs_dev *dev, struct spdk_io_channel *channel, void *payload, 41 uint64_t lba, uint32_t lba_count, 42 struct spdk_bs_dev_cb_args *cb_args) 43 { 44 cb_args->cb_fn(cb_args->channel, cb_args->cb_arg, -EPERM); 45 assert(false); 46 } 47 48 static void 49 blob_bs_dev_writev(struct spdk_bs_dev *dev, struct spdk_io_channel *channel, 50 struct iovec *iov, int iovcnt, 51 uint64_t lba, uint32_t lba_count, 52 struct spdk_bs_dev_cb_args *cb_args) 53 { 54 cb_args->cb_fn(cb_args->channel, cb_args->cb_arg, -EPERM); 55 assert(false); 56 } 57 58 static void 59 blob_bs_dev_write_zeroes(struct spdk_bs_dev *dev, struct spdk_io_channel *channel, 60 uint64_t lba, uint32_t lba_count, 61 struct spdk_bs_dev_cb_args *cb_args) 62 { 63 cb_args->cb_fn(cb_args->channel, cb_args->cb_arg, -EPERM); 64 assert(false); 65 } 66 67 static void 68 blob_bs_dev_unmap(struct spdk_bs_dev *dev, struct spdk_io_channel *channel, 69 uint64_t lba, uint32_t lba_count, 70 struct spdk_bs_dev_cb_args *cb_args) 71 { 72 cb_args->cb_fn(cb_args->channel, cb_args->cb_arg, -EPERM); 73 assert(false); 74 } 75 76 static void 77 blob_bs_dev_read_cpl(void *cb_arg, int bserrno) 78 { 79 struct spdk_bs_dev_cb_args *cb_args = (struct spdk_bs_dev_cb_args *)cb_arg; 80 81 cb_args->cb_fn(cb_args->channel, cb_args->cb_arg, bserrno); 82 } 83 84 static inline void 85 blob_bs_dev_read(struct spdk_bs_dev *dev, struct spdk_io_channel *channel, void *payload, 86 uint64_t lba, uint32_t lba_count, struct spdk_bs_dev_cb_args *cb_args) 87 { 88 struct spdk_blob_bs_dev *b = (struct spdk_blob_bs_dev *)dev; 89 90 spdk_blob_io_read(b->blob, channel, payload, lba, lba_count, 91 blob_bs_dev_read_cpl, cb_args); 92 } 93 94 static inline void 95 blob_bs_dev_readv(struct spdk_bs_dev *dev, struct spdk_io_channel *channel, 96 struct iovec *iov, int iovcnt, 97 uint64_t lba, uint32_t lba_count, struct spdk_bs_dev_cb_args *cb_args) 98 { 99 struct spdk_blob_bs_dev *b = (struct spdk_blob_bs_dev *)dev; 100 101 spdk_blob_io_readv(b->blob, channel, iov, iovcnt, lba, lba_count, 102 blob_bs_dev_read_cpl, cb_args); 103 } 104 105 static void 106 blob_bs_dev_destroy_cpl(void *cb_arg, int bserrno) 107 { 108 if (bserrno != 0) { 109 SPDK_ERRLOG("Error on blob_bs_dev destroy: %d", bserrno); 110 } 111 112 /* Free blob_bs_dev */ 113 free(cb_arg); 114 } 115 116 static void 117 blob_bs_dev_destroy(struct spdk_bs_dev *bs_dev) 118 { 119 struct spdk_blob_bs_dev *b = (struct spdk_blob_bs_dev *)bs_dev; 120 121 spdk_blob_close(b->blob, blob_bs_dev_destroy_cpl, b); 122 } 123 124 125 struct spdk_bs_dev * 126 bs_create_blob_bs_dev(struct spdk_blob *blob) 127 { 128 struct spdk_blob_bs_dev *b; 129 130 b = calloc(1, sizeof(*b)); 131 if (b == NULL) { 132 return NULL; 133 } 134 /* snapshot blob */ 135 b->bs_dev.blockcnt = blob->active.num_clusters * 136 blob->bs->pages_per_cluster * bs_io_unit_per_page(blob->bs); 137 b->bs_dev.blocklen = spdk_bs_get_io_unit_size(blob->bs); 138 b->bs_dev.create_channel = NULL; 139 b->bs_dev.destroy_channel = NULL; 140 b->bs_dev.destroy = blob_bs_dev_destroy; 141 b->bs_dev.write = blob_bs_dev_write; 142 b->bs_dev.writev = blob_bs_dev_writev; 143 b->bs_dev.read = blob_bs_dev_read; 144 b->bs_dev.readv = blob_bs_dev_readv; 145 b->bs_dev.write_zeroes = blob_bs_dev_write_zeroes; 146 b->bs_dev.unmap = blob_bs_dev_unmap; 147 b->blob = blob; 148 149 return &b->bs_dev; 150 } 151