xref: /spdk/lib/blob/blob_bs_dev.c (revision 927f1fd57bd004df581518466ec4c1b8083e5d23)
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright (c) Intel Corporation.
5  *   All rights reserved.
6  *   Copyright (c) 2022 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
7  *
8  *   Redistribution and use in source and binary forms, with or without
9  *   modification, are permitted provided that the following conditions
10  *   are met:
11  *
12  *     * Redistributions of source code must retain the above copyright
13  *       notice, this list of conditions and the following disclaimer.
14  *     * Redistributions in binary form must reproduce the above copyright
15  *       notice, this list of conditions and the following disclaimer in
16  *       the documentation and/or other materials provided with the
17  *       distribution.
18  *     * Neither the name of Intel Corporation nor the names of its
19  *       contributors may be used to endorse or promote products derived
20  *       from this software without specific prior written permission.
21  *
22  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  */
34 
35 #include "spdk/stdinc.h"
36 #include "spdk/blob.h"
37 #include "spdk/log.h"
38 #include "blobstore.h"
39 
40 static void
41 blob_bs_dev_write(struct spdk_bs_dev *dev, struct spdk_io_channel *channel, void *payload,
42 		  uint64_t lba, uint32_t lba_count,
43 		  struct spdk_bs_dev_cb_args *cb_args)
44 {
45 	cb_args->cb_fn(cb_args->channel, cb_args->cb_arg, -EPERM);
46 	assert(false);
47 }
48 
49 static void
50 blob_bs_dev_writev(struct spdk_bs_dev *dev, struct spdk_io_channel *channel,
51 		   struct iovec *iov, int iovcnt,
52 		   uint64_t lba, uint32_t lba_count,
53 		   struct spdk_bs_dev_cb_args *cb_args)
54 {
55 	cb_args->cb_fn(cb_args->channel, cb_args->cb_arg, -EPERM);
56 	assert(false);
57 }
58 
59 static void
60 blob_bs_dev_writev_ext(struct spdk_bs_dev *dev, struct spdk_io_channel *channel,
61 		       struct iovec *iov, int iovcnt,
62 		       uint64_t lba, uint32_t lba_count,
63 		       struct spdk_bs_dev_cb_args *cb_args,
64 		       struct spdk_blob_ext_io_opts *ext_opts)
65 {
66 	cb_args->cb_fn(cb_args->channel, cb_args->cb_arg, -EPERM);
67 	assert(false);
68 }
69 
70 static void
71 blob_bs_dev_write_zeroes(struct spdk_bs_dev *dev, struct spdk_io_channel *channel,
72 			 uint64_t lba, uint64_t lba_count,
73 			 struct spdk_bs_dev_cb_args *cb_args)
74 {
75 	cb_args->cb_fn(cb_args->channel, cb_args->cb_arg, -EPERM);
76 	assert(false);
77 }
78 
79 static void
80 blob_bs_dev_unmap(struct spdk_bs_dev *dev, struct spdk_io_channel *channel,
81 		  uint64_t lba, uint64_t lba_count,
82 		  struct spdk_bs_dev_cb_args *cb_args)
83 {
84 	cb_args->cb_fn(cb_args->channel, cb_args->cb_arg, -EPERM);
85 	assert(false);
86 }
87 
88 static void
89 blob_bs_dev_read_cpl(void *cb_arg, int bserrno)
90 {
91 	struct spdk_bs_dev_cb_args *cb_args = (struct spdk_bs_dev_cb_args *)cb_arg;
92 
93 	cb_args->cb_fn(cb_args->channel, cb_args->cb_arg, bserrno);
94 }
95 
96 static inline void
97 blob_bs_dev_read(struct spdk_bs_dev *dev, struct spdk_io_channel *channel, void *payload,
98 		 uint64_t lba, uint32_t lba_count, struct spdk_bs_dev_cb_args *cb_args)
99 {
100 	struct spdk_blob_bs_dev *b = (struct spdk_blob_bs_dev *)dev;
101 
102 	spdk_blob_io_read(b->blob, channel, payload, lba, lba_count,
103 			  blob_bs_dev_read_cpl, cb_args);
104 }
105 
106 static inline void
107 blob_bs_dev_readv(struct spdk_bs_dev *dev, struct spdk_io_channel *channel,
108 		  struct iovec *iov, int iovcnt,
109 		  uint64_t lba, uint32_t lba_count, struct spdk_bs_dev_cb_args *cb_args)
110 {
111 	struct spdk_blob_bs_dev *b = (struct spdk_blob_bs_dev *)dev;
112 
113 	spdk_blob_io_readv(b->blob, channel, iov, iovcnt, lba, lba_count,
114 			   blob_bs_dev_read_cpl, cb_args);
115 }
116 
117 static inline void
118 blob_bs_dev_readv_ext(struct spdk_bs_dev *dev, struct spdk_io_channel *channel,
119 		      struct iovec *iov, int iovcnt,
120 		      uint64_t lba, uint32_t lba_count, struct spdk_bs_dev_cb_args *cb_args,
121 		      struct spdk_blob_ext_io_opts *ext_opts)
122 {
123 	struct spdk_blob_bs_dev *b = (struct spdk_blob_bs_dev *)dev;
124 
125 	spdk_blob_io_readv_ext(b->blob, channel, iov, iovcnt, lba, lba_count,
126 			       blob_bs_dev_read_cpl, cb_args, ext_opts);
127 }
128 
129 static void
130 blob_bs_dev_destroy_cpl(void *cb_arg, int bserrno)
131 {
132 	if (bserrno != 0) {
133 		SPDK_ERRLOG("Error on blob_bs_dev destroy: %d", bserrno);
134 	}
135 
136 	/* Free blob_bs_dev */
137 	free(cb_arg);
138 }
139 
140 static void
141 blob_bs_dev_destroy(struct spdk_bs_dev *bs_dev)
142 {
143 	struct spdk_blob_bs_dev *b = (struct spdk_blob_bs_dev *)bs_dev;
144 
145 	spdk_blob_close(b->blob, blob_bs_dev_destroy_cpl, b);
146 }
147 
148 
149 struct spdk_bs_dev *
150 bs_create_blob_bs_dev(struct spdk_blob *blob)
151 {
152 	struct spdk_blob_bs_dev  *b;
153 
154 	b = calloc(1, sizeof(*b));
155 	if (b == NULL) {
156 		return NULL;
157 	}
158 	/* snapshot blob */
159 	b->bs_dev.blockcnt = blob->active.num_clusters *
160 			     blob->bs->pages_per_cluster * bs_io_unit_per_page(blob->bs);
161 	b->bs_dev.blocklen = spdk_bs_get_io_unit_size(blob->bs);
162 	b->bs_dev.create_channel = NULL;
163 	b->bs_dev.destroy_channel = NULL;
164 	b->bs_dev.destroy = blob_bs_dev_destroy;
165 	b->bs_dev.write = blob_bs_dev_write;
166 	b->bs_dev.writev = blob_bs_dev_writev;
167 	b->bs_dev.writev_ext = blob_bs_dev_writev_ext;
168 	b->bs_dev.read = blob_bs_dev_read;
169 	b->bs_dev.readv = blob_bs_dev_readv;
170 	b->bs_dev.readv_ext = blob_bs_dev_readv_ext;
171 	b->bs_dev.write_zeroes = blob_bs_dev_write_zeroes;
172 	b->bs_dev.unmap = blob_bs_dev_unmap;
173 	b->blob = blob;
174 
175 	return &b->bs_dev;
176 }
177